Skip to content

Commit c912551

Browse files
committed
remove tskit/c code
1 parent a8a9107 commit c912551

3 files changed

Lines changed: 20 additions & 77 deletions

File tree

c/tests/test_core.c

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ test_json_struct_metadata_get_blob(void)
109109
tsk_size_t metadata_length;
110110
size_t header_length;
111111
size_t json_length;
112-
size_t padding_length;
113112
size_t payload_length;
114113
size_t total_length;
115114
char json_payload[] = "{\"a\":1}";
@@ -119,9 +118,8 @@ test_json_struct_metadata_get_blob(void)
119118
bytes = (uint8_t *) metadata;
120119
header_length = 4 + 1 + 8 + 8;
121120
json_length = strlen(json_payload);
122-
padding_length = (8 - ((header_length + json_length) & 0x07)) % 8;
123121
payload_length = sizeof(binary_payload);
124-
total_length = header_length + json_length + padding_length + payload_length;
122+
total_length = header_length + json_length + payload_length;
125123
CU_ASSERT_FATAL(total_length <= sizeof(metadata));
126124
memset(metadata, 0, sizeof(metadata));
127125
bytes[0] = 'J';
@@ -132,64 +130,54 @@ test_json_struct_metadata_get_blob(void)
132130
set_u64_le(bytes + 5, (uint64_t) json_length);
133131
set_u64_le(bytes + 13, (uint64_t) payload_length);
134132
memcpy(bytes + header_length, json_payload, json_length);
135-
memset(bytes + header_length + json_length, 0, padding_length);
136-
memcpy(bytes + header_length + json_length + padding_length, binary_payload,
137-
payload_length);
133+
memcpy(bytes + header_length + json_length, binary_payload, payload_length);
138134
metadata_length = (tsk_size_t) total_length;
139135
ret = tsk_json_struct_metadata_get_blob(
140136
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
141137
CU_ASSERT_EQUAL(ret, 0);
142138
CU_ASSERT_PTR_EQUAL(json, (char *) bytes + header_length);
143-
CU_ASSERT_EQUAL(json + json_buffer_length + padding_length, blob);
144139
CU_ASSERT_EQUAL(json_buffer_length, (tsk_size_t) json_length);
145140
if (json_length > 0) {
146141
CU_ASSERT_EQUAL(memcmp(json, json_payload, json_length), 0);
147142
}
148-
CU_ASSERT_PTR_EQUAL(blob, bytes + header_length + json_length + padding_length);
143+
CU_ASSERT_PTR_EQUAL(blob, bytes + header_length + json_length);
149144
CU_ASSERT_EQUAL(blob_length, (tsk_size_t) payload_length);
150145
CU_ASSERT_EQUAL(memcmp(blob, binary_payload, payload_length), 0);
151-
CU_ASSERT((tsk_size_t) (blob - json) < json_buffer_length + 8);
152146

153147
payload_length = 0;
154-
total_length = header_length + json_length + padding_length + payload_length;
148+
total_length = header_length + json_length + payload_length;
155149
CU_ASSERT_FATAL(total_length <= sizeof(metadata));
156150
set_u64_le(bytes + 13, (uint64_t) payload_length);
157151
metadata_length = (tsk_size_t) total_length;
158152
ret = tsk_json_struct_metadata_get_blob(
159153
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
160154
CU_ASSERT_EQUAL(ret, 0);
161-
CU_ASSERT_EQUAL(json + json_buffer_length + padding_length, blob);
162155
CU_ASSERT_PTR_EQUAL(json, (char *) bytes + header_length);
163156
CU_ASSERT_EQUAL(json_buffer_length, (tsk_size_t) json_length);
164157
CU_ASSERT_EQUAL(blob_length, (tsk_size_t) payload_length);
165-
CU_ASSERT_PTR_EQUAL(blob, bytes + header_length + json_length + padding_length);
166-
CU_ASSERT((tsk_size_t) (blob - json) < json_buffer_length + 8);
158+
CU_ASSERT_PTR_EQUAL(blob, bytes + header_length + json_length);
167159

168160
json_length = 0;
169161
payload_length = sizeof(empty_payload);
170-
padding_length = (8 - ((header_length + json_length) & 0x07)) % 8;
171-
total_length = header_length + json_length + padding_length + payload_length;
162+
total_length = header_length + json_length + payload_length;
172163
CU_ASSERT_FATAL(total_length <= sizeof(metadata));
173164
set_u64_le(bytes + 5, (uint64_t) json_length);
174165
set_u64_le(bytes + 13, (uint64_t) payload_length);
175-
memset(bytes + header_length + json_length, 0, padding_length);
176-
memcpy(bytes + header_length + json_length + padding_length, empty_payload,
177-
payload_length);
166+
memcpy(bytes + header_length + json_length, empty_payload, payload_length);
178167
metadata_length = (tsk_size_t) total_length;
179168
ret = tsk_json_struct_metadata_get_blob(
180169
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
181170
CU_ASSERT_EQUAL(ret, 0);
182171
CU_ASSERT_PTR_EQUAL(json, (char *) bytes + header_length);
183172
CU_ASSERT_EQUAL(json_buffer_length, (tsk_size_t) json_length);
184173
CU_ASSERT_EQUAL(blob_length, (tsk_size_t) payload_length);
185-
CU_ASSERT_PTR_EQUAL(blob, bytes + header_length + json_length + padding_length);
174+
CU_ASSERT_PTR_EQUAL(blob, bytes + header_length + json_length);
186175
CU_ASSERT_EQUAL(memcmp(blob, empty_payload, payload_length), 0);
187-
CU_ASSERT((tsk_size_t) (blob - json) < json_buffer_length + 8);
188176

189177
blob = NULL;
190178
blob_length = 0;
191179
json = NULL;
192-
json_length = 0;
180+
json_buffer_length = 0;
193181
metadata_length = header_length - 1;
194182
ret = tsk_json_struct_metadata_get_blob(
195183
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
@@ -208,19 +196,7 @@ test_json_struct_metadata_get_blob(void)
208196
CU_ASSERT_EQUAL(ret, TSK_ERR_JSON_STRUCT_METADATA_BAD_VERSION);
209197
bytes[4] = 1;
210198

211-
set_u64_le(bytes + 5, (uint64_t) json_length + 9);
212-
ret = tsk_json_struct_metadata_get_blob(
213-
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
214-
CU_ASSERT_EQUAL(ret, TSK_ERR_JSON_STRUCT_METADATA_UNEXPECTED_SIZE);
215-
set_u64_le(bytes + 5, (uint64_t) json_length);
216-
217-
bytes[header_length + 1] = 1;
218-
ret = tsk_json_struct_metadata_get_blob(
219-
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
220-
CU_ASSERT_EQUAL(ret, TSK_ERR_JSON_STRUCT_METADATA_NONZERO_PADDING);
221-
bytes[header_length + 1] = 0;
222-
223-
metadata_length = (tsk_size_t) (header_length - 1);
199+
metadata_length = (tsk_size_t) (total_length - 1);
224200
ret = tsk_json_struct_metadata_get_blob(
225201
metadata, metadata_length, &json, &json_buffer_length, &blob, &blob_length);
226202
CU_ASSERT_EQUAL(ret, TSK_ERR_JSON_STRUCT_METADATA_TRUNCATED);

c/tskit/core.c

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,10 @@ tsk_json_struct_metadata_get_blob(char *metadata, tsk_size_t metadata_length,
150150
uint64_t json_length_u64;
151151
uint64_t binary_length_u64;
152152
uint64_t header_and_json_length;
153-
uint64_t padding_length;
154-
uint64_t header_and_json_and_padding_length;
155153
uint64_t total_length;
156154
uint8_t *bytes;
157-
char *json_start;
158-
char *padding_start;
159155
char *blob_start;
156+
char *json_start;
160157

161158
if (metadata == NULL || json == NULL || json_length == NULL || blob == NULL
162159
|| blob_length == NULL) {
@@ -184,28 +181,17 @@ tsk_json_struct_metadata_get_blob(char *metadata, tsk_size_t metadata_length,
184181
goto out;
185182
}
186183
header_and_json_length = (uint64_t) TSK_JSON_BINARY_HEADER_SIZE + json_length_u64;
187-
padding_length = (8 - (header_and_json_length & 0x07)) % 8;
188-
header_and_json_and_padding_length = header_and_json_length + padding_length;
189-
if (binary_length_u64 > UINT64_MAX - header_and_json_and_padding_length) {
184+
if (binary_length_u64 > UINT64_MAX - header_and_json_length) {
190185
ret = tsk_trace_error(TSK_ERR_JSON_STRUCT_METADATA_INVALID_LENGTH);
191186
goto out;
192187
}
193-
total_length = header_and_json_and_padding_length + binary_length_u64;
194-
if ((uint64_t) metadata_length != total_length) {
195-
ret = tsk_trace_error(TSK_ERR_JSON_STRUCT_METADATA_UNEXPECTED_SIZE);
188+
total_length = header_and_json_length + binary_length_u64;
189+
if ((uint64_t) metadata_length < total_length) {
190+
ret = tsk_trace_error(TSK_ERR_JSON_STRUCT_METADATA_TRUNCATED);
196191
goto out;
197192
}
198-
padding_start = (char *) bytes + TSK_JSON_BINARY_HEADER_SIZE + json_length_u64;
199-
for (uint64_t padding_index = 0; padding_index < padding_length; ++padding_index) {
200-
// require padding bytes to be zero, for a bit more safety
201-
if (*(padding_start + padding_index) != (char) 0) {
202-
ret = tsk_trace_error(TSK_ERR_JSON_STRUCT_METADATA_NONZERO_PADDING);
203-
goto out;
204-
}
205-
}
206193
json_start = (char *) bytes + TSK_JSON_BINARY_HEADER_SIZE;
207-
blob_start = (char *) bytes + TSK_JSON_BINARY_HEADER_SIZE + json_length_u64
208-
+ padding_length;
194+
blob_start = (char *) bytes + TSK_JSON_BINARY_HEADER_SIZE + json_length_u64;
209195
*json = json_start;
210196
*json_length = (tsk_size_t) json_length_u64;
211197
*blob = blob_start;
@@ -297,14 +283,6 @@ tsk_strerror_internal(int err)
297283
ret = "JSON binary struct metadata uses an unsupported version number. "
298284
"(TSK_ERR_JSON_STRUCT_METADATA_BAD_VERSION)";
299285
break;
300-
case TSK_ERR_JSON_STRUCT_METADATA_UNEXPECTED_SIZE:
301-
ret = "JSON binary struct metadata is not equal to the expected size. "
302-
"(TSK_ERR_JSON_STRUCT_METADATA_UNEXPECTED_SIZE)";
303-
break;
304-
case TSK_ERR_JSON_STRUCT_METADATA_NONZERO_PADDING:
305-
ret = "JSON binary struct metadata has non-zero padding bytes between the "
306-
"JSON and struct. (TSK_ERR_JSON_STRUCT_METADATA_NONZERO_PADDING)";
307-
break;
308286

309287
/* Out of bounds errors */
310288
case TSK_ERR_BAD_OFFSET:

c/tskit/core.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,6 @@ A length field in the JSON binary struct metadata header is invalid.
329329
The JSON binary struct metadata uses an unsupported version number.
330330
*/
331331
#define TSK_ERR_JSON_STRUCT_METADATA_BAD_VERSION -109
332-
333-
/**
334-
The JSON binary struct metadata is not equal to the expected size.
335-
*/
336-
#define TSK_ERR_JSON_STRUCT_METADATA_UNEXPECTED_SIZE -110
337-
338-
/**
339-
The JSON binary struct metadata padding bytes are not zeroed.
340-
*/
341-
#define TSK_ERR_JSON_STRUCT_METADATA_NONZERO_PADDING -111
342332
/** @} */
343333

344334
/**
@@ -1146,11 +1136,10 @@ int tsk_generate_uuid(char *dest, int flags);
11461136
@brief Extract the binary payload from ``json+struct`` encoded metadata.
11471137
11481138
@rst
1149-
Metadata produced by the JSONStructCodec consists of a fixed-size header followed
1150-
by canonical JSON bytes, a variable number of padding bytes (which should be zero)
1151-
to bring the length to a multiple of 8 bytes for alignment, and finally an optional
1152-
binary payload. This helper validates the framing, returning pointers to the
1153-
embedded JSON and binary sections without copying.
1139+
Metadata produced by the JSONStructCodec consists of a fixed-size
1140+
header followed by canonical JSON bytes and an optional binary payload. This helper
1141+
validates the framing, returning pointers to the embedded JSON and binary sections
1142+
without copying.
11541143
11551144
The output pointers reference memory owned by the caller and remain valid only while
11561145
the original metadata buffer is alive.

0 commit comments

Comments
 (0)