MCPcopy Create free account
hub / github.com/KhronosGroup/KTX-Software / ktxHashList_Serialize

Function ktxHashList_Serialize

lib/hashlist.c:412–462  ·  view source on GitHub ↗

* @memberof ktxHashList @public * @~English * @brief Serialize a hash list to a block of data suitable for writing * to a file. * * The caller is responsible for freeing the data block returned by this * function. * * @param [in] pHead pointer to the head of the target hash list. * @param [in,out] pKvdLen @p *pKvdLen is set to the number of bytes of *

Source from the content-addressed store, hash-verified

410 * data.
411 */
412KTX_error_code
413ktxHashList_Serialize(ktxHashList* pHead,
414 unsigned int* pKvdLen, unsigned char** ppKvd)
415{
416
417 if (pHead && pKvdLen && ppKvd) {
418 ktxKVListEntry* kv;
419 unsigned int bytesOfKeyValueData = 0;
420 unsigned int keyValueLen;
421 unsigned char* sd;
422 char padding[4] = {0, 0, 0, 0};
423
424 for (kv = *pHead; kv != NULL; kv = kv->hh.next) {
425 /* sizeof(sd) is to make space to write keyAndValueByteSize */
426 keyValueLen = kv->keyLen + kv->valueLen + sizeof(ktx_uint32_t);
427 /* Add valuePadding */
428 keyValueLen = _KTX_PAD4(keyValueLen);
429 bytesOfKeyValueData += keyValueLen;
430 }
431
432 if (bytesOfKeyValueData == 0) {
433 *pKvdLen = 0;
434 *ppKvd = NULL;
435 } else {
436 sd = malloc(bytesOfKeyValueData);
437 if (!sd)
438 return KTX_OUT_OF_MEMORY;
439
440 *pKvdLen = bytesOfKeyValueData;
441 *ppKvd = sd;
442
443 for (kv = *pHead; kv != NULL; kv = kv->hh.next) {
444 int padLen;
445
446 keyValueLen = kv->keyLen + kv->valueLen;
447 *(ktx_uint32_t*)sd = keyValueLen;
448 sd += sizeof(ktx_uint32_t);
449 memcpy(sd, kv->key, kv->keyLen);
450 sd += kv->keyLen;
451 if (kv->valueLen > 0)
452 memcpy(sd, kv->value, kv->valueLen);
453 sd += kv->valueLen;
454 padLen = _KTX_PAD4_LEN(keyValueLen);
455 memcpy(sd, padding, padLen);
456 sd += padLen;
457 }
458 }
459 return KTX_SUCCESS;
460 } else
461 return KTX_INVALID_VALUE;
462}
463
464
465int sort_by_key_codepoint(ktxKVListEntry* a, ktxKVListEntry* b) {

Callers 5

writer2.cFile · 0.85
runTestMethod · 0.85
resizeMethod · 0.85

Calls

no outgoing calls

Tested by 1

runTestMethod · 0.68