int main(int argc, char * argv[]) { if (argc != 2) usage(); FILE * f = fopen(argv[1], "rb"); if (f == NULL) usage(); fseek(f, 0, SEEK_END); size_t length = (size_t)ftell(f); fseek(f, 0, SEEK_SET); unsigned char * buffer = malloc(length); fread(buffer, length, 1, f); struct cbor_callbacks callbacks = cbor_empty_callbacks; struct cbor_decoder_result decode_result; size_t bytes_read = 0; callbacks.string = find_string; while (bytes_read < length) { decode_result = cbor_stream_decode(buffer + bytes_read, length - bytes_read, &callbacks, NULL); bytes_read += decode_result.read; } free(buffer); fclose(f); }
/* Exercise the default callbacks */ static void test_default_callbacks(void **state) { size_t read = 0; while (read < 79) { struct cbor_decoder_result result = cbor_stream_decode(data + read, 79 - read, &cbor_empty_callbacks, NULL); read += result.read; } }
/** * Manual test for testing the cbor_stream_decode function */ void test_stream_decode(void) { cbor_clear(&stream); cbor_serialize_int(&stream, 1); cbor_serialize_uint64_t(&stream, 2llu); cbor_serialize_int64_t(&stream, 3); cbor_serialize_int64_t(&stream, -5); cbor_serialize_bool(&stream, true); #ifndef CBOR_NO_FLOAT cbor_serialize_float_half(&stream, 1.1f); cbor_serialize_float(&stream, 1.5f); cbor_serialize_double(&stream, 2.0); #endif /* CBOR_NO_FLOAT */ cbor_serialize_byte_string(&stream, "abc"); cbor_serialize_unicode_string(&stream, "def"); cbor_serialize_array(&stream, 2); cbor_serialize_int(&stream, 0); cbor_serialize_int(&stream, 1); cbor_serialize_array_indefinite(&stream); cbor_serialize_int(&stream, 10); cbor_serialize_int(&stream, 11); cbor_write_break(&stream); cbor_serialize_map(&stream, 2); cbor_serialize_int(&stream, 1); cbor_serialize_byte_string(&stream, "1"); cbor_serialize_int(&stream, 2); cbor_serialize_byte_string(&stream, "2"); cbor_serialize_map_indefinite(&stream); cbor_serialize_int(&stream, 10); cbor_serialize_byte_string(&stream, "10"); cbor_serialize_int(&stream, 11); cbor_serialize_byte_string(&stream, "11"); cbor_write_break(&stream); #ifndef CBOR_NO_SEMANTIC_TAGGING #ifndef CBOR_NO_CTIME time_t rawtime; time(&rawtime); struct tm *timeinfo = localtime(&rawtime); cbor_serialize_date_time(&stream, timeinfo); cbor_serialize_date_time_epoch(&stream, rawtime); #endif /* CBOR_NO_CTIME */ /* decoder should skip the tag and print 'unsupported' here */ cbor_write_tag(&stream, 2); cbor_serialize_byte_string(&stream, "1"); #endif /* CBOR_NO_SEMANTIC_TAGGING */ cbor_stream_decode(&stream); }