ucl_msgpack.c (b626f5a73a48f44a31a200291b141e1da408a2ff) ucl_msgpack.c (d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1/*
2 * Copyright (c) 2015, Vsevolod Stakhov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 99 unchanged lines hidden (view full) ---

108 unsigned len;
109
110 if (val >= 0) {
111 if (val <= 0x7f) {
112 /* Fixed num 7 bits */
113 len = 1;
114 buf[0] = mask_positive & val;
115 }
1/*
2 * Copyright (c) 2015, Vsevolod Stakhov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 99 unchanged lines hidden (view full) ---

108 unsigned len;
109
110 if (val >= 0) {
111 if (val <= 0x7f) {
112 /* Fixed num 7 bits */
113 len = 1;
114 buf[0] = mask_positive & val;
115 }
116 else if (val <= 0xff) {
116 else if (val <= UINT8_MAX) {
117 len = 2;
118 buf[0] = uint8_ch;
119 buf[1] = val & 0xff;
120 }
117 len = 2;
118 buf[0] = uint8_ch;
119 buf[1] = val & 0xff;
120 }
121 else if (val <= 0xffff) {
121 else if (val <= UINT16_MAX) {
122 uint16_t v = TO_BE16 (val);
123
124 len = 3;
125 buf[0] = uint16_ch;
126 memcpy (&buf[1], &v, sizeof (v));
127 }
122 uint16_t v = TO_BE16 (val);
123
124 len = 3;
125 buf[0] = uint16_ch;
126 memcpy (&buf[1], &v, sizeof (v));
127 }
128 else if (val <= 0xffffffff) {
128 else if (val <= UINT32_MAX) {
129 uint32_t v = TO_BE32 (val);
130
131 len = 5;
132 buf[0] = uint32_ch;
133 memcpy (&buf[1], &v, sizeof (v));
134 }
135 else {
136 uint64_t v = TO_BE64 (val);

--- 7 unchanged lines hidden (view full) ---

144 uint64_t uval;
145 /* Bithack abs */
146 uval = ((val ^ (val >> 63)) - (val >> 63));
147
148 if (val > -(1 << 5)) {
149 len = 1;
150 buf[0] = (mask_negative | uval) & 0xff;
151 }
129 uint32_t v = TO_BE32 (val);
130
131 len = 5;
132 buf[0] = uint32_ch;
133 memcpy (&buf[1], &v, sizeof (v));
134 }
135 else {
136 uint64_t v = TO_BE64 (val);

--- 7 unchanged lines hidden (view full) ---

144 uint64_t uval;
145 /* Bithack abs */
146 uval = ((val ^ (val >> 63)) - (val >> 63));
147
148 if (val > -(1 << 5)) {
149 len = 1;
150 buf[0] = (mask_negative | uval) & 0xff;
151 }
152 else if (uval <= 0xff) {
152 else if (uval <= INT8_MAX) {
153 uint8_t v = (uint8_t)val;
153 len = 2;
154 buf[0] = int8_ch;
154 len = 2;
155 buf[0] = int8_ch;
155 buf[1] = (unsigned char)val;
156 buf[1] = v;
156 }
157 }
157 else if (uval <= 0xffff) {
158 else if (uval <= INT16_MAX) {
158 uint16_t v = TO_BE16 (val);
159
160 len = 3;
161 buf[0] = int16_ch;
162 memcpy (&buf[1], &v, sizeof (v));
163 }
159 uint16_t v = TO_BE16 (val);
160
161 len = 3;
162 buf[0] = int16_ch;
163 memcpy (&buf[1], &v, sizeof (v));
164 }
164 else if (uval <= 0xffffffff) {
165 else if (uval <= INT32_MAX) {
165 uint32_t v = TO_BE32 (val);
166
167 len = 5;
168 buf[0] = int32_ch;
169 memcpy (&buf[1], &v, sizeof (v));
170 }
171 else {
172 uint64_t v = TO_BE64 (val);

--- 572 unchanged lines hidden (view full) ---

745ucl_msgpack_get_parser_from_type (unsigned char t)
746{
747 unsigned int i, shift, mask;
748
749 for (i = 0; i < sizeof (parsers) / sizeof (parsers[0]); i ++) {
750 shift = CHAR_BIT - parsers[i].prefixlen;
751 mask = parsers[i].prefix >> shift;
752
166 uint32_t v = TO_BE32 (val);
167
168 len = 5;
169 buf[0] = int32_ch;
170 memcpy (&buf[1], &v, sizeof (v));
171 }
172 else {
173 uint64_t v = TO_BE64 (val);

--- 572 unchanged lines hidden (view full) ---

746ucl_msgpack_get_parser_from_type (unsigned char t)
747{
748 unsigned int i, shift, mask;
749
750 for (i = 0; i < sizeof (parsers) / sizeof (parsers[0]); i ++) {
751 shift = CHAR_BIT - parsers[i].prefixlen;
752 mask = parsers[i].prefix >> shift;
753
753 if (mask == (t >> shift)) {
754 if (mask == (((unsigned int)t) >> shift)) {
754 return &parsers[i];
755 }
756 }
757
758 return NULL;
759}
760
761static inline struct ucl_stack *

--- 202 unchanged lines hidden (view full) ---

964 start_array,
965 read_assoc_key,
966 read_assoc_value,
967 finish_assoc_value,
968 read_array_value,
969 finish_array_value,
970 error_state
971 } state = read_type, next_state = error_state;
755 return &parsers[i];
756 }
757 }
758
759 return NULL;
760}
761
762static inline struct ucl_stack *

--- 202 unchanged lines hidden (view full) ---

965 start_array,
966 read_assoc_key,
967 read_assoc_value,
968 finish_assoc_value,
969 read_array_value,
970 finish_array_value,
971 error_state
972 } state = read_type, next_state = error_state;
972 struct ucl_msgpack_parser *obj_parser;
973 uint64_t len;
973 struct ucl_msgpack_parser *obj_parser = NULL;
974 uint64_t len = 0;
974 ssize_t ret, remain, keylen = 0;
975#ifdef MSGPACK_DEBUG_PARSER
976 uint64_t i;
977 enum e_msgpack_parser_state hist[256];
978#endif
979
980 p = parser->chunks->begin;
981 remain = parser->chunks->remain;

--- 431 unchanged lines hidden (view full) ---

1413}
1414
1415static ssize_t
1416ucl_msgpack_parse_int (struct ucl_parser *parser,
1417 struct ucl_stack *container, size_t len, enum ucl_msgpack_format fmt,
1418 const unsigned char *pos, size_t remain)
1419{
1420 ucl_object_t *obj;
975 ssize_t ret, remain, keylen = 0;
976#ifdef MSGPACK_DEBUG_PARSER
977 uint64_t i;
978 enum e_msgpack_parser_state hist[256];
979#endif
980
981 p = parser->chunks->begin;
982 remain = parser->chunks->remain;

--- 431 unchanged lines hidden (view full) ---

1414}
1415
1416static ssize_t
1417ucl_msgpack_parse_int (struct ucl_parser *parser,
1418 struct ucl_stack *container, size_t len, enum ucl_msgpack_format fmt,
1419 const unsigned char *pos, size_t remain)
1420{
1421 ucl_object_t *obj;
1422 int8_t iv8;
1423 int16_t iv16;
1424 int32_t iv32;
1425 int64_t iv64;
1421
1422 if (len > remain) {
1423 return -1;
1424 }
1425
1426 obj = ucl_object_new_full (UCL_INT, parser->chunks->priority);
1427
1428 switch (fmt) {

--- 5 unchanged lines hidden (view full) ---

1434 obj->value.iv = - (*pos & 0x1f);
1435 len = 1;
1436 break;
1437 case msgpack_uint8:
1438 obj->value.iv = (unsigned char)*pos;
1439 len = 1;
1440 break;
1441 case msgpack_int8:
1426
1427 if (len > remain) {
1428 return -1;
1429 }
1430
1431 obj = ucl_object_new_full (UCL_INT, parser->chunks->priority);
1432
1433 switch (fmt) {

--- 5 unchanged lines hidden (view full) ---

1439 obj->value.iv = - (*pos & 0x1f);
1440 len = 1;
1441 break;
1442 case msgpack_uint8:
1443 obj->value.iv = (unsigned char)*pos;
1444 len = 1;
1445 break;
1446 case msgpack_int8:
1442 obj->value.iv = (signed char)*pos;
1447 memcpy (&iv8, pos, sizeof (iv8));
1448 obj->value.iv = iv8;
1443 len = 1;
1444 break;
1445 case msgpack_int16:
1449 len = 1;
1450 break;
1451 case msgpack_int16:
1446 obj->value.iv = FROM_BE16 (*(int16_t *)pos);
1452 memcpy (&iv16, pos, sizeof (iv16));
1453 iv16 = FROM_BE16 (iv16);
1454 obj->value.iv = iv16;
1447 len = 2;
1448 break;
1449 case msgpack_uint16:
1450 obj->value.iv = FROM_BE16 (*(uint16_t *)pos);
1451 len = 2;
1452 break;
1453 case msgpack_int32:
1455 len = 2;
1456 break;
1457 case msgpack_uint16:
1458 obj->value.iv = FROM_BE16 (*(uint16_t *)pos);
1459 len = 2;
1460 break;
1461 case msgpack_int32:
1454 obj->value.iv = FROM_BE32 (*(int32_t *)pos);
1462 memcpy (&iv32, pos, sizeof (iv32));
1463 iv32 = FROM_BE32 (iv32);
1464 obj->value.iv = iv32;
1455 len = 4;
1456 break;
1457 case msgpack_uint32:
1458 obj->value.iv = FROM_BE32 (*(uint32_t *)pos);
1459 len = 4;
1460 break;
1461 case msgpack_int64:
1465 len = 4;
1466 break;
1467 case msgpack_uint32:
1468 obj->value.iv = FROM_BE32 (*(uint32_t *)pos);
1469 len = 4;
1470 break;
1471 case msgpack_int64:
1462 obj->value.iv = FROM_BE64 (*(int64_t *)pos);
1472 memcpy (&iv64, pos, sizeof (iv64));
1473 iv64 = FROM_BE64 (iv64);
1474 obj->value.iv = iv64;
1463 len = 8;
1464 break;
1465 case msgpack_uint64:
1466 obj->value.iv = FROM_BE64 (*(uint64_t *)pos);
1467 len = 8;
1468 break;
1469 default:
1470 assert (0);

--- 130 unchanged lines hidden ---
1475 len = 8;
1476 break;
1477 case msgpack_uint64:
1478 obj->value.iv = FROM_BE64 (*(uint64_t *)pos);
1479 len = 8;
1480 break;
1481 default:
1482 assert (0);

--- 130 unchanged lines hidden ---