| #define _CRT_SECURE_NO_WARNINGS 1 |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <cn-cbor/cn-cbor.h> |
| |
| #include "json.h" |
| |
| #ifdef USE_CBOR_CONTEXT |
| extern cn_cbor_context * allocator; |
| #define CBOR_CONTEXT_PARAM , allocator |
| #define CBOR_CONTEXT_PARAM_COMMA allocator, |
| #else |
| #define CBOR_CONTEXT_PARAM |
| #define CBOR_CONTEXT_PARAM_COMMA |
| #endif |
| |
| |
| const cn_cbor * ParseString(char * rgch, int ib, int cch) |
| { |
| char ch; |
| int ib2; |
| cn_cbor * node = NULL; |
| cn_cbor * parent = NULL; |
| cn_cbor * root = NULL; |
| |
| for (; ib < cch; ib++) { |
| node = NULL; |
| ch = rgch[ib]; |
| switch (ch) { |
| case '{': |
| node = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA NULL); |
| break; |
| |
| case '}': |
| parent = parent->parent; |
| break; |
| |
| case '[': |
| node = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA NULL); |
| break; |
| |
| case ']': |
| parent = parent->parent; |
| break; |
| |
| case ' ': |
| case '\r': |
| case '\n': |
| case':': |
| case ',': |
| break; |
| |
| case '"': |
| for (ib2 = ib + 1; ib2 < cch; ib2++) if (rgch[ib2] == '"') break; |
| rgch[ib2] = 0; |
| node = cn_cbor_string_create(&rgch[ib+1], CBOR_CONTEXT_PARAM_COMMA NULL); |
| // rgch[ib2] = '"'; |
| ib = ib2; |
| break; |
| |
| case't': |
| if (strncmp(&rgch[ib], "true", 4) != 0) goto error; |
| node = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA NULL); |
| node->type = CN_CBOR_TRUE; |
| ib += 3; |
| break; |
| |
| case'f': |
| if (strncmp(&rgch[ib], "false", 5) != 0) goto error; |
| node = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA NULL); |
| node->type = CN_CBOR_FALSE; |
| ib += 4; |
| break; |
| |
| default: |
| error: |
| fprintf(stderr, "Parse failure @ '%s'\n", &rgch[ib]); |
| break; |
| } |
| |
| if ((node != NULL) && (parent != NULL)) { |
| node->parent = parent; |
| if (parent->last_child != NULL) { |
| parent->last_child->next = node; |
| parent->last_child = node; |
| } |
| else { |
| parent->first_child = node; |
| } |
| parent->last_child = node; |
| parent->length++; |
| |
| if ((node->type == CN_CBOR_MAP) || (node->type == CN_CBOR_ARRAY)) { |
| parent = node; |
| } |
| } |
| if (parent == NULL) { |
| parent = node; |
| if (root == NULL) root = node; |
| } |
| } |
| |
| return root; |
| } |
| |
| const cn_cbor * ParseJson(const char * fileName) |
| { |
| int cch; |
| char * rgch = malloc(8 * 1024); |
| FILE * fp = fopen(fileName, "r"); |
| |
| if (fp == NULL) { |
| fprintf(stderr, "Cannot open file '%s'\n", fileName); |
| exit(1); |
| } |
| fprintf(stderr, "Processing '%s'\n", fileName); |
| |
| cch = (int) fread(rgch, 1, 8*1024, fp); |
| fclose(fp); |
| |
| return ParseString(rgch, 0, cch); |
| } |
| |
| |
| extern void build_decoding_table(); |
| |
| |
| static char encoding_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| '4', '5', '6', '7', '8', '9', '-', '_' }; |
| static char *decoding_table = NULL; |
| static int mod_table[] = { 0, 2, 1 }; |
| |
| |
| char *base64_encode(const unsigned char *data, |
| size_t input_length, |
| size_t *output_length) { |
| |
| *output_length = 4 * ((input_length + 2) / 3); |
| |
| char *encoded_data = malloc(*output_length); |
| if (encoded_data == NULL) return NULL; |
| |
| for (size_t i = 0, j = 0; i < input_length;) { |
| |
| uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; |
| uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; |
| uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0; |
| |
| uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; |
| |
| encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; |
| encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; |
| encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; |
| encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; |
| } |
| |
| for (int i = 0; i < mod_table[input_length % 3]; i++) |
| encoded_data[*output_length - 1 - i] = '='; |
| |
| return encoded_data; |
| } |
| |
| |
| unsigned char *base64_decode(const char *data, |
| size_t input_length, |
| size_t *output_length) { |
| char * p = NULL; |
| |
| if (decoding_table == NULL) build_decoding_table(); |
| |
| if (input_length % 4 != 0) { |
| int c = 4 - (input_length % 4); |
| p = malloc(input_length + c); |
| memcpy(p, data, input_length); |
| memcpy(p + input_length, "====", c); |
| input_length += c; |
| data = p; |
| } |
| |
| *output_length = input_length / 4 * 3; |
| if (data[input_length - 1] == '=') (*output_length)--; |
| if (data[input_length - 2] == '=') (*output_length)--; |
| |
| unsigned char *decoded_data = malloc(*output_length); |
| if (decoded_data == NULL) return NULL; |
| |
| for (unsigned int i = 0, j = 0; i < input_length;) { |
| |
| uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(int) data[i++]]; |
| uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(int) data[i++]]; |
| uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(int) data[i++]]; |
| uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(int) data[i++]]; |
| |
| uint32_t triple = (sextet_a << 3 * 6) |
| + (sextet_b << 2 * 6) |
| + (sextet_c << 1 * 6) |
| + (sextet_d << 0 * 6); |
| |
| if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF; |
| if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF; |
| if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF; |
| } |
| |
| free(p); |
| return decoded_data; |
| } |
| |
| |
| void build_decoding_table() { |
| |
| decoding_table = malloc(256); |
| |
| for (int i = 0; i < 64; i++) |
| decoding_table[encoding_table[i]] = (char) i; |
| } |
| |
| |
| void base64_cleanup() { |
| free(decoding_table); |
| } |