Skip to content

Commit fafa97f

Browse files
committed
refactor(codec): use ssize_t for fl_base64_decode/encode return type
Change return type from int to ssize_t for consistency with POSIX read/write conventions. Callers use ssize_t n instead of int n. Print format stays %d with (int) cast for embedded libc compatibility.
1 parent 6eb7e60 commit fafa97f

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

App/func_loader/fl_cmd_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ fl_error_t fl_cmd_fwrite(fl_context_t* ctx, const cmd_args_t* args) {
9191

9292
/* Write to file */
9393
ssize_t written = fl_file_write(&ctx->file_ctx, ctx->buf, n);
94-
if (written < 0 || (int)written != n) {
95-
fl_response(false, "Write failed, expected %d bytes, actual %d bytes", n, (int)written);
94+
if (written < 0 || written != n) {
95+
fl_response(false, "Write failed, expected %d bytes, actual %d bytes", (int)n, (int)written);
9696
return FL_ERR_IO;
9797
}
9898

App/func_loader/fl_cmd_mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fl_error_t fl_cmd_upload(fl_context_t* ctx, const cmd_args_t* args) {
7676
uint8_t* buf = ctx->buf;
7777
bool verify = args->crc >= 0;
7878

79-
int n = fl_base64_decode(args->data, buf, FL_BUF_SIZE);
79+
ssize_t n = fl_base64_decode(args->data, buf, FL_BUF_SIZE);
8080
if (n < 0) {
8181
fl_response(false, "Invalid base64 data");
8282
return FL_ERR_ENCODE;
@@ -187,7 +187,7 @@ fl_error_t fl_cmd_write(fl_context_t* ctx, const cmd_args_t* args) {
187187
uint8_t* buf = ctx->buf;
188188
bool verify = args->crc >= 0;
189189

190-
int n = fl_base64_decode(args->data, buf, FL_BUF_SIZE);
190+
ssize_t n = fl_base64_decode(args->data, buf, FL_BUF_SIZE);
191191
if (n < 0) {
192192
fl_response(false, "Invalid base64 data");
193193
return FL_ERR_ENCODE;

App/func_loader/fl_codec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static const uint8_t s_b64_dec[128] = {
7575
static const char s_b64_enc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
7676
/* clang-format on */
7777

78-
int fl_base64_decode(const char* b64, uint8_t* out, size_t max) {
78+
ssize_t fl_base64_decode(const char* b64, uint8_t* out, size_t max) {
7979
if (!b64 || !out) {
8080
return -1;
8181
}
@@ -129,10 +129,10 @@ int fl_base64_decode(const char* b64, uint8_t* out, size_t max) {
129129
i += 4;
130130
}
131131

132-
return (int)out_len;
132+
return (ssize_t)out_len;
133133
}
134134

135-
int fl_base64_encode(const uint8_t* data, size_t len, char* out, size_t max) {
135+
ssize_t fl_base64_encode(const uint8_t* data, size_t len, char* out, size_t max) {
136136
if (!data || !out || max == 0) {
137137
return -1;
138138
}
@@ -155,5 +155,5 @@ int fl_base64_encode(const uint8_t* data, size_t len, char* out, size_t max) {
155155
}
156156
out[j] = '\0';
157157

158-
return (int)j;
158+
return (ssize_t)j;
159159
}

App/func_loader/fl_codec.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C" {
3636
#include <stdint.h>
3737
#include <stddef.h>
3838
#include <string.h>
39+
#include <sys/types.h>
3940

4041
/**
4142
* @brief CRC-16-CCITT incremental calculation
@@ -65,7 +66,7 @@ static inline uint16_t fl_crc16_base_str(uint16_t crc, const char* str) {
6566
* @param max Output buffer size
6667
* @return Number of decoded bytes, or -1 on error
6768
*/
68-
int fl_base64_decode(const char* b64, uint8_t* out, size_t max);
69+
ssize_t fl_base64_decode(const char* b64, uint8_t* out, size_t max);
6970

7071
/**
7172
* @brief Encode bytes to Base64 string
@@ -75,7 +76,7 @@ int fl_base64_decode(const char* b64, uint8_t* out, size_t max);
7576
* @param max Output buffer size
7677
* @return Number of Base64 characters written, or -1 on error
7778
*/
78-
int fl_base64_encode(const uint8_t* data, size_t len, char* out, size_t max);
79+
ssize_t fl_base64_encode(const uint8_t* data, size_t len, char* out, size_t max);
7980

8081
#ifdef __cplusplus
8182
}

0 commit comments

Comments
 (0)