Skip to content

Commit

Permalink
UNPACK_TAKE_BYTE() UNPACK_PEEK_BYTE() macros made more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Jan 6, 2023
1 parent 3f5734f commit f7791ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
18 changes: 9 additions & 9 deletions libshvchainpack/c/cchainpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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--;
}
Expand All @@ -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--;
}
Expand All @@ -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);

Expand Down Expand Up @@ -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<len; i++) {
UNPACK_TAKE_BYTE();
UNPACK_TAKE_BYTE(p);
bytes[i] = (uint8_t)(*p);
}
}
else {
for (i=len-1; i>=0; i--) {
UNPACK_TAKE_BYTE();
UNPACK_TAKE_BYTE(p);
bytes[i] = (uint8_t)(*p);
}
}
Expand Down
4 changes: 2 additions & 2 deletions libshvchainpack/c/ccpcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
62 changes: 31 additions & 31 deletions libshvchainpack/c/ccpon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -1118,21 +1118,21 @@ 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
it->last_chunk = 1;
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;
Expand All @@ -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.");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1260,29 +1260,29 @@ 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
unpack_context->item.type = CCPCP_ITEM_LIST;
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit f7791ba

Please sign in to comment.