From f7791ba1890640d996e35cb682fc5a3781581838 Mon Sep 17 00:00:00 2001 From: Fanda Vacek Date: Fri, 6 Jan 2023 11:11:03 +0100 Subject: [PATCH] UNPACK_TAKE_BYTE() UNPACK_PEEK_BYTE() macros made more explicit --- libshvchainpack/c/cchainpack.c | 18 +++++----- libshvchainpack/c/ccpcp.h | 4 +-- libshvchainpack/c/ccpon.c | 62 +++++++++++++++++----------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/libshvchainpack/c/cchainpack.c b/libshvchainpack/c/cchainpack.c index 90b302341..9c971e68b 100644 --- a/libshvchainpack/c/cchainpack.c +++ b/libshvchainpack/c/cchainpack.c @@ -492,7 +492,7 @@ static void unpack_uint(ccpcp_unpack_context* unpack_context, uint64_t *pval, in int bitlen = 0; const char *p; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); uint8_t head = (uint8_t)(*p); int bytes_to_read_cnt; @@ -506,7 +506,7 @@ static void unpack_uint(ccpcp_unpack_context* unpack_context, uint64_t *pval, in } int i; for (i = 0; i < bytes_to_read_cnt; ++i) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); uint8_t r = (uint8_t)(*p); num = (num << 8) + r; }; @@ -549,9 +549,9 @@ void unpack_string(ccpcp_unpack_context* unpack_context) bool is_cstr = it->string_size < 0; if(is_cstr) { for(it->chunk_size = 0; it->chunk_size < it->chunk_buff_len; ) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == '\\') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(!p) return; switch (*p) { @@ -576,7 +576,7 @@ void unpack_string(ccpcp_unpack_context* unpack_context) else { it->chunk_size = 0; while(it->size_to_load > 0 && it->chunk_size < it->chunk_buff_len) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); (it->chunk_start)[it->chunk_size++] = *p; it->size_to_load--; } @@ -595,7 +595,7 @@ void unpack_blob(ccpcp_unpack_context* unpack_context) it->chunk_size = 0; while(it->size_to_load > 0 && it->chunk_size < it->chunk_buff_len) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); (it->chunk_start)[it->chunk_size++] = *p; it->size_to_load--; } @@ -617,7 +617,7 @@ void cchainpack_unpack_next (ccpcp_unpack_context* unpack_context) } const char *p; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); uint8_t packing_schema = (uint8_t)(*p); @@ -679,13 +679,13 @@ void cchainpack_unpack_next (ccpcp_unpack_context* unpack_context) if(*(char *)&n == 1) { // little endian if true for (i=0; i=0; i--) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); bytes[i] = (uint8_t)(*p); } } diff --git a/libshvchainpack/c/ccpcp.h b/libshvchainpack/c/ccpcp.h index ae302cbdd..a909742a3 100644 --- a/libshvchainpack/c/ccpcp.h +++ b/libshvchainpack/c/ccpcp.h @@ -198,14 +198,14 @@ bool ccpcp_item_is_map_val(ccpcp_unpack_context* unpack_context); return; \ } -#define UNPACK_TAKE_BYTE() \ +#define UNPACK_TAKE_BYTE(p) \ { \ p = ccpcp_unpack_take_byte(unpack_context); \ if(!p) \ return; \ } -#define UNPACK_PEEK_BYTE() \ +#define UNPACK_PEEK_BYTE(p) \ { \ p = ccpcp_unpack_peek_byte(unpack_context); \ if(!p) \ diff --git a/libshvchainpack/c/ccpon.c b/libshvchainpack/c/ccpon.c index 6ad89e569..d8569a5b8 100644 --- a/libshvchainpack/c/ccpon.c +++ b/libshvchainpack/c/ccpon.c @@ -961,7 +961,7 @@ void ccpon_unpack_date_time(ccpcp_unpack_context *unpack_context, struct tm *tm, */ tm->tm_year = (int)val - 1900; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p != '-') { unpack_context->err_no = CCPCP_RC_MALFORMED_INPUT; unpack_context->err_msg = "Malformed year-month separator in DateTime"; @@ -976,7 +976,7 @@ void ccpon_unpack_date_time(ccpcp_unpack_context *unpack_context, struct tm *tm, } tm->tm_mon = (int)val - 1; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p != '-') { unpack_context->err_no = CCPCP_RC_MALFORMED_INPUT; unpack_context->err_msg = "Malformed month-day separator in DateTime"; @@ -991,7 +991,7 @@ void ccpon_unpack_date_time(ccpcp_unpack_context *unpack_context, struct tm *tm, } tm->tm_mday = (int)val; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(!(*p == 'T' || *p == ' ')) { unpack_context->err_no = CCPCP_RC_MALFORMED_INPUT; unpack_context->err_msg = "Malformed date-time separator in DateTime"; @@ -1006,7 +1006,7 @@ void ccpon_unpack_date_time(ccpcp_unpack_context *unpack_context, struct tm *tm, } tm->tm_hour = (int)val; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); n = unpack_int(unpack_context, &val); if(n <= 0) { @@ -1016,7 +1016,7 @@ void ccpon_unpack_date_time(ccpcp_unpack_context *unpack_context, struct tm *tm, } tm->tm_min = (int)val; - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); n = unpack_int(unpack_context, &val); if(n <= 0) { @@ -1078,14 +1078,14 @@ static void ccpon_unpack_blob_hex(ccpcp_unpack_context* unpack_context) ccpcp_string *it = &unpack_context->item.as.String; if(it->chunk_cnt == 0) { // must start with '"' - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if (*p != '"') { UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Blob should start with 'x\"' ."); } } for(it->chunk_size = 0; it->chunk_size < it->chunk_buff_len; ) { do { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); } while(*p <= ' '); if (*p == '"') { // end of string @@ -1097,7 +1097,7 @@ static void ccpon_unpack_blob_hex(ccpcp_unpack_context* unpack_context) int b1 = unhex((uint8_t)(*p)); if(b1 < 0) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Invalid HEX char, first digit."); - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(!p) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Invalid HEX char, second digit missing."); //printf("ch2: %c\n", *p); @@ -1118,13 +1118,13 @@ static void ccpon_unpack_blob_esc(ccpcp_unpack_context* unpack_context) ccpcp_string *it = &unpack_context->item.as.String; if(it->chunk_cnt == 0) { // must start with '"' - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if (*p != '"') { UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Blob should start with 'b\"' ."); } } for(it->chunk_size = 0; it->chunk_size < it->chunk_buff_len; ) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); uint8_t b = (uint8_t)(*p); if (b == '"') { // end of string @@ -1132,7 +1132,7 @@ static void ccpon_unpack_blob_esc(ccpcp_unpack_context* unpack_context) break; } if(b == '\\') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); switch((uint8_t)*p) { case 't': (it->chunk_start)[it->chunk_size++] = '\t'; break; case 'r': (it->chunk_start)[it->chunk_size++] = '\r'; break; @@ -1143,7 +1143,7 @@ static void ccpon_unpack_blob_esc(ccpcp_unpack_context* unpack_context) int hi = unhex((uint8_t)(*p)); if(hi < 0) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Invalid HEX char."); - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); int lo = unhex((uint8_t)(*p)); if(lo < 0) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Invalid HEX char."); @@ -1171,15 +1171,15 @@ static void ccpon_unpack_string(ccpcp_unpack_context* unpack_context) ccpcp_string *it = &unpack_context->item.as.String; if(it->chunk_cnt == 0) { // must start with '"' - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if (*p != '"') { UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "String should start with '\"' character."); } } for(it->chunk_size = 0; it->chunk_size < it->chunk_buff_len; ) { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == '\\') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(!p) return; switch (*p) { @@ -1260,14 +1260,14 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) unpack_context->item.type = CCPCP_ITEM_LIST; break; case 'i': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p != '{') UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "IMap should start with '{'.") unpack_context->item.type = CCPCP_ITEM_IMAP; break; } case 'a': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p != '[') UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "List should start with '['.") // unpack unsupported ARRAY type as list @@ -1275,14 +1275,14 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) break; } case 'd': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(!p || *p != '"') UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "DateTime should start with 'd'.") struct tm tm; int msec; int utc_offset; ccpon_unpack_date_time(unpack_context, &tm, &msec, &utc_offset); - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(!p || *p != '"') UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "DateTime should start with 'd\"'.") break; @@ -1294,11 +1294,11 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) } */ case 'n': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'u') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'l') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'l') { unpack_context->item.type = CCPCP_ITEM_NULL; break; @@ -1308,13 +1308,13 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Malformed 'null' literal.") } case 'f': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'a') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'l') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 's') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'e') { unpack_context->item.type = CCPCP_ITEM_BOOLEAN; unpack_context->item.as.Bool = false; @@ -1326,11 +1326,11 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Malformed 'false' literal.") } case 't': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'r') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'u') { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p == 'e') { unpack_context->item.type = CCPCP_ITEM_BOOLEAN; unpack_context->item.as.Bool = true; @@ -1341,7 +1341,7 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "Malformed 'true' literal.") } case 'x': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p != '"') UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "HEX string should start with 'x\"'.") unpack_context->item.type = CCPCP_ITEM_BLOB; @@ -1353,7 +1353,7 @@ void ccpon_unpack_next (ccpcp_unpack_context* unpack_context) break; } case 'b': { - UNPACK_TAKE_BYTE(); + UNPACK_TAKE_BYTE(p); if(*p != '"') UNPACK_ERROR(CCPCP_RC_MALFORMED_INPUT, "BLOB string should start with 'b\"'.") unpack_context->item.type = CCPCP_ITEM_BLOB;