|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <err.h> |
| 4 | +#include <string.h> |
| 5 | +#include <tskit.h> |
| 6 | + |
| 7 | +// these are properties of the ``json+struct`` codec, documented in tskit |
| 8 | +#define JSON_STRUCT_HEADER_SIZE 21 |
| 9 | + |
| 10 | +const uint8_t json_struct_codec_magic[4] = { 'J', 'B', 'L', 'B' }; |
| 11 | +const uint8_t json_struct_codec_version = 1; |
| 12 | + |
| 13 | +// little-endian read of a uint64_t from an address |
| 14 | +static uint64_t |
| 15 | +load_u64_le(const uint8_t *p) |
| 16 | +{ |
| 17 | + uint64_t value = (uint64_t) p[0]; |
| 18 | + value |= (uint64_t) p[1] << 8; |
| 19 | + value |= (uint64_t) p[2] << 16; |
| 20 | + value |= (uint64_t) p[3] << 24; |
| 21 | + value |= (uint64_t) p[4] << 32; |
| 22 | + value |= (uint64_t) p[5] << 40; |
| 23 | + value |= (uint64_t) p[6] << 48; |
| 24 | + value |= (uint64_t) p[7] << 56; |
| 25 | + return value; |
| 26 | +} |
| 27 | + |
| 28 | +// little-endian write of a uint64_t to an address |
| 29 | +static void |
| 30 | +set_u64_le(uint8_t *dest, uint64_t value) |
| 31 | +{ |
| 32 | + dest[0] = (uint8_t) (value & 0xFF); |
| 33 | + dest[1] = (uint8_t) ((value >> 8) & 0xFF); |
| 34 | + dest[2] = (uint8_t) ((value >> 16) & 0xFF); |
| 35 | + dest[3] = (uint8_t) ((value >> 24) & 0xFF); |
| 36 | + dest[4] = (uint8_t) ((value >> 32) & 0xFF); |
| 37 | + dest[5] = (uint8_t) ((value >> 40) & 0xFF); |
| 38 | + dest[6] = (uint8_t) ((value >> 48) & 0xFF); |
| 39 | + dest[7] = (uint8_t) ((value >> 56) & 0xFF); |
| 40 | +} |
| 41 | + |
| 42 | +// Extract the json and binary payloads from the `json+struct` codec data buffer. |
| 43 | +// Note that the output pointers `json` and `binary` reference memory |
| 44 | +// inside the `metadata` buffer passed in. |
| 45 | +void |
| 46 | +json_struct_codec_get_components(uint8_t *metadata, tsk_size_t metadata_length, |
| 47 | + uint8_t **json, tsk_size_t *json_length, uint8_t **binary, tsk_size_t *binary_length) |
| 48 | +{ |
| 49 | + // check the structure of the codec header and the sizes it specifies |
| 50 | + if (metadata == NULL || json == NULL || json_length == NULL || binary == NULL |
| 51 | + || binary_length == NULL) |
| 52 | + errx(EXIT_FAILURE, "bad parameter value."); |
| 53 | + if (metadata_length < JSON_STRUCT_HEADER_SIZE) |
| 54 | + errx(EXIT_FAILURE, "metadata truncated."); |
| 55 | + if (memcmp(metadata, json_struct_codec_magic, sizeof(json_struct_codec_magic)) != 0) |
| 56 | + errx(EXIT_FAILURE, "bad magic bytes."); |
| 57 | + |
| 58 | + uint8_t version = metadata[4]; |
| 59 | + if (version != json_struct_codec_version) |
| 60 | + errx(EXIT_FAILURE, "bad version number."); |
| 61 | + |
| 62 | + uint64_t json_length_u64 = load_u64_le(metadata + 5); |
| 63 | + uint64_t binary_length_u64 = load_u64_le(metadata + 13); |
| 64 | + if (json_length_u64 > UINT64_MAX - (uint64_t) JSON_STRUCT_HEADER_SIZE) |
| 65 | + errx(EXIT_FAILURE, "invalid length."); |
| 66 | + |
| 67 | + // determine the number of padding bytes and do more safety checks |
| 68 | + uint64_t length = (uint64_t) JSON_STRUCT_HEADER_SIZE + json_length_u64; |
| 69 | + uint64_t padding_length = (8 - (length & 0x07)) % 8; |
| 70 | + length += padding_length; |
| 71 | + if (binary_length_u64 > UINT64_MAX - length) |
| 72 | + errx(EXIT_FAILURE, "invalid length."); |
| 73 | + |
| 74 | + length += binary_length_u64; |
| 75 | + if ((uint64_t) metadata_length != length) |
| 76 | + errx(EXIT_FAILURE, "unexpected size."); |
| 77 | + |
| 78 | + uint8_t *padding_start = metadata + JSON_STRUCT_HEADER_SIZE + json_length_u64; |
| 79 | + for (uint64_t j = 0; j < padding_length; ++j) |
| 80 | + if (*(padding_start + j) != 0) |
| 81 | + errx(EXIT_FAILURE, "padding bytes are nonzero."); |
| 82 | + |
| 83 | + // the structure of the codec data seems valid; return components |
| 84 | + *json = metadata + JSON_STRUCT_HEADER_SIZE; |
| 85 | + *json_length = (tsk_size_t) json_length_u64; |
| 86 | + |
| 87 | + *binary = metadata + JSON_STRUCT_HEADER_SIZE + json_length_u64 + padding_length; |
| 88 | + *binary_length = (tsk_size_t) binary_length_u64; |
| 89 | +} |
| 90 | + |
| 91 | +// malloc and return a data buffer for the `json+struct` codec |
| 92 | +// that contains the given components |
| 93 | +void |
| 94 | +json_struct_codec_create_buffer(const uint8_t *json, tsk_size_t json_length, |
| 95 | + const uint8_t *binary, tsk_size_t binary_length, uint8_t **buffer, |
| 96 | + tsk_size_t *buffer_length) |
| 97 | +{ |
| 98 | + // figure out the total length of the codec's data and allocate the buffer for it |
| 99 | + tsk_size_t header_length = JSON_STRUCT_HEADER_SIZE; |
| 100 | + tsk_size_t padding_length = (8 - ((header_length + json_length) & 0x07)) % 8; |
| 101 | + tsk_size_t total_length |
| 102 | + = header_length + json_length + padding_length + binary_length; |
| 103 | + uint8_t *bytes = malloc(total_length); |
| 104 | + if (!bytes) |
| 105 | + errx(EXIT_FAILURE, "memory for buffer could not be allocated."); |
| 106 | + |
| 107 | + // then set up the bytes for the codec header |
| 108 | + memcpy(bytes, json_struct_codec_magic, 4); |
| 109 | + bytes[4] = json_struct_codec_version; |
| 110 | + set_u64_le(bytes + 5, (uint64_t) json_length); |
| 111 | + set_u64_le(bytes + 13, (uint64_t) binary_length); |
| 112 | + |
| 113 | + // copy in the JSON and binary data, separated by the padding bytes; the goal of the |
| 114 | + // padding bytes is to ensure that the binary data is 8-byte-aligned relative to the |
| 115 | + // start of the buffer |
| 116 | + memcpy(bytes + header_length, json, json_length); |
| 117 | + memset(bytes + header_length + json_length, 0, padding_length); |
| 118 | + memcpy(bytes + header_length + json_length + padding_length, binary, binary_length); |
| 119 | + |
| 120 | + // return the buffer and its length; the caller takes ownership of the buffer |
| 121 | + *buffer = bytes; |
| 122 | + *buffer_length = total_length; |
| 123 | +} |
| 124 | + |
| 125 | +int |
| 126 | +main(int argc, char **argv) |
| 127 | +{ |
| 128 | + // we start with JSON and binary payloads that we encode into a new buffer |
| 129 | + // note that the JSON payload does not have to end with a trailing NULL |
| 130 | + const char json_payload[] = { '{', '"', 'a', '"', ':', '1', '}' }; |
| 131 | + const uint8_t binary_payload[] = { 0x01, 0x02, 0x03, 0x04 }; |
| 132 | + uint8_t *metadata; |
| 133 | + tsk_size_t metadata_length; |
| 134 | + |
| 135 | + json_struct_codec_create_buffer((const uint8_t *) json_payload, sizeof(json_payload), |
| 136 | + binary_payload, sizeof(binary_payload), &metadata, &metadata_length); |
| 137 | + |
| 138 | + // then we decode that buffer to recover the json and binary data |
| 139 | + uint8_t *decoded_json, *decoded_binary; |
| 140 | + tsk_size_t decoded_json_length, decoded_binary_length; |
| 141 | + |
| 142 | + json_struct_codec_get_components(metadata, metadata_length, &decoded_json, |
| 143 | + &decoded_json_length, &decoded_binary, &decoded_binary_length); |
| 144 | + |
| 145 | + // print the recovered data to demonstrate that the round-trip worked |
| 146 | + // note that the JSON data is not NULL-terminated unless you put a NULL there! |
| 147 | + printf("JSON: %.*s\n", (int) decoded_json_length, decoded_json); |
| 148 | + |
| 149 | + printf("Binary data:"); |
| 150 | + for (tsk_size_t j = 0; j < decoded_binary_length; j++) |
| 151 | + printf(" %#04x", decoded_binary[j]); |
| 152 | + printf("\n"); |
| 153 | + |
| 154 | + free(metadata); |
| 155 | + return EXIT_SUCCESS; |
| 156 | +} |
0 commit comments