/* -*- tab-width: 4; -*- */1/* vi: set sw=2 ts=4 expandtab: */23/*4* Copyright 2010-2020 The Khronos Group Inc.5* SPDX-License-Identifier: Apache-2.06*/78/**9* @internal10* @file11* @~English12*13* @brief Functions for creating and using a hash list of key-value14* pairs.15*16* @author Mark Callow, HI Corporation17*/1819#include <stdio.h>20#include <stdlib.h>21#include <string.h>22#include <assert.h>2324// This is to avoid compile warnings. strlen is defined as returning25// size_t and is used by the uthash macros. This avoids having to26// make changes to uthash and a bunch of casts in this file. The27// casts would be required because the key and value lengths in KTX28// are specified as 4 byte quantities so we can't change _keyAndValue29// below to use size_t.30#define strlen(x) ((unsigned int)strlen(x))3132#include "uthash.h"3334#include "ktx.h"35#include "ktxint.h"363738/**39* @internal40* @struct ktxKVListEntry41* @brief Hash list entry structure42*/43typedef struct ktxKVListEntry {44unsigned int keyLen; /*!< Length of the key */45char* key; /*!< Pointer to key string */46unsigned int valueLen; /*!< Length of the value */47void* value; /*!< Pointer to the value */48UT_hash_handle hh; /*!< handle used by UT hash */49} ktxKVListEntry;505152/**53* @memberof ktxHashList @public54* @~English55* @brief Construct an empty hash list for storing key-value pairs.56*57* @param [in] pHead pointer to the location to write the list head.58*/59void60ktxHashList_Construct(ktxHashList* pHead)61{62*pHead = NULL;63}646566/**67* @memberof ktxHashList @public68* @~English69* @brief Construct a hash list by copying another.70*71* @param [in] pHead pointer to head of the list.72* @param [in] orig head of the original hash list.73*/74void75ktxHashList_ConstructCopy(ktxHashList* pHead, ktxHashList orig)76{77ktxHashListEntry* entry = orig;78*pHead = NULL;79for (; entry != NULL; entry = ktxHashList_Next(entry)) {80(void)ktxHashList_AddKVPair(pHead,81entry->key, entry->valueLen, entry->value);82}83}848586/**87* @memberof ktxHashList @public88* @~English89* @brief Destruct a hash list.90*91* All memory associated with the hash list's keys and values92* is freed.93*94* @param [in] pHead pointer to the hash list to be destroyed.95*/96void97ktxHashList_Destruct(ktxHashList* pHead)98{99ktxKVListEntry* kv;100ktxKVListEntry* head = *pHead;101102for(kv = head; kv != NULL;) {103ktxKVListEntry* tmp = (ktxKVListEntry*)kv->hh.next;104HASH_DELETE(hh, head, kv);105free(kv);106kv = tmp;107}108}109110111/**112* @memberof ktxHashList @public113* @~English114* @brief Create an empty hash list for storing key-value pairs.115*116* @param [in,out] ppHl address of a variable in which to set a pointer to117* the newly created hash list.118*119* @return KTX_SUCCESS or one of the following error codes.120* @exception KTX_OUT_OF_MEMORY if not enough memory.121*/122KTX_error_code123ktxHashList_Create(ktxHashList** ppHl)124{125ktxHashList* hl = (ktxHashList*)malloc(sizeof (ktxKVListEntry*));126if (hl == NULL)127return KTX_OUT_OF_MEMORY;128129ktxHashList_Construct(hl);130*ppHl = hl;131return KTX_SUCCESS;132}133134135/**136* @memberof ktxHashList @public137* @~English138* @brief Create a copy of a hash list.139*140* @param [in,out] ppHl address of a variable in which to set a pointer to141* the newly created hash list.142* @param [in] orig head of the ktxHashList to copy.143*144* @return KTX_SUCCESS or one of the following error codes.145* @exception KTX_OUT_OF_MEMORY if not enough memory.146*/147KTX_error_code148ktxHashList_CreateCopy(ktxHashList** ppHl, ktxHashList orig)149{150ktxHashList* hl = (ktxHashList*)malloc(sizeof (ktxKVListEntry*));151if (hl == NULL)152return KTX_OUT_OF_MEMORY;153154ktxHashList_ConstructCopy(hl, orig);155*ppHl = hl;156return KTX_SUCCESS;157}158159160/**161* @memberof ktxHashList @public162* @~English163* @brief Destroy a hash list.164*165* All memory associated with the hash list's keys and values166* is freed. The hash list is also freed.167*168* @param [in] pHead pointer to the hash list to be destroyed.169*/170void171ktxHashList_Destroy(ktxHashList* pHead)172{173ktxHashList_Destruct(pHead);174free(pHead);175}176177#if !__clang__ && __GNUC__ // Grumble clang grumble178// These are in uthash.h macros. I don't want to change that file.179#pragma GCC diagnostic push180#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"181#endif182183/**184* @memberof ktxHashList @public185* @~English186* @brief Add a key value pair to a hash list.187*188* The value can be empty, i.e, its length can be 0.189*190* @param [in] pHead pointer to the head of the target hash list.191* @param [in] key pointer to the UTF8 NUL-terminated string to be used as the key.192* @param [in] valueLen the number of bytes of data in @p value.193* @param [in] value pointer to the bytes of data constituting the value.194*195* @return KTX_SUCCESS or one of the following error codes.196* @exception KTX_INVALID_VALUE if @p pHead, @p key or @p value are NULL, @p key is an197* empty string or @p valueLen == 0.198*/199KTX_error_code200ktxHashList_AddKVPair(ktxHashList* pHead, const char* key, unsigned int valueLen, const void* value)201{202if (pHead && key && (valueLen == 0 || value)) {203unsigned int keyLen = (unsigned int)strlen(key) + 1;204ktxKVListEntry* kv;205206if (keyLen == 1)207return KTX_INVALID_VALUE; /* Empty string */208209/* Allocate all the memory as a block */210kv = (ktxKVListEntry*)malloc(sizeof(ktxKVListEntry) + keyLen + valueLen);211/* Put key first */212kv->key = (char *)kv + sizeof(ktxKVListEntry);213kv->keyLen = keyLen;214memcpy(kv->key, key, keyLen);215/* then value */216kv->valueLen = valueLen;217if (valueLen > 0) {218kv->value = kv->key + keyLen;219memcpy(kv->value, value, valueLen);220} else {221kv->value = 0;222}223224HASH_ADD_KEYPTR( hh, *pHead, kv->key, kv->keyLen-1, kv);225return KTX_SUCCESS;226} else227return KTX_INVALID_VALUE;228}229230231/**232* @memberof ktxHashList @public233* @~English234* @brief Delete a key value pair in a hash list.235*236* Is a nop if the key is not in the hash.237*238* @param [in] pHead pointer to the head of the target hash list.239* @param [in] key pointer to the UTF8 NUL-terminated string to be used as the key.240*241* @return KTX_SUCCESS or one of the following error codes.242* @exception KTX_INVALID_VALUE if @p pHead is NULL or @p key is an empty243* string.244*/245KTX_error_code246ktxHashList_DeleteKVPair(ktxHashList* pHead, const char* key)247{248if (pHead && key) {249ktxKVListEntry* kv;250251HASH_FIND_STR( *pHead, key, kv ); /* kv: pointer to target entry. */252if (kv != NULL)253HASH_DEL(*pHead, kv);254return KTX_SUCCESS;255} else256return KTX_INVALID_VALUE;257}258259260/**261* @memberof ktxHashList @public262* @~English263* @brief Delete an entry from a hash list.264*265* @param [in] pHead pointer to the head of the target hash list.266* @param [in] pEntry pointer to the ktxHashListEntry to delete.267*268* @return KTX_SUCCESS or one of the following error codes.269* @exception KTX_INVALID_VALUE if @p pHead is NULL or @p key is an empty270* string.271*/272KTX_error_code273ktxHashList_DeleteEntry(ktxHashList* pHead, ktxHashListEntry* pEntry)274{275if (pHead && pEntry) {276HASH_DEL(*pHead, pEntry);277return KTX_SUCCESS;278} else279return KTX_INVALID_VALUE;280}281282283/**284* @memberof ktxHashList @public285* @~English286* @brief Looks up a key in a hash list and returns the entry.287*288* @param [in] pHead pointer to the head of the target hash list.289* @param [in] key pointer to a UTF8 NUL-terminated string to find.290* @param [in,out] ppEntry @p *ppEntry is set to the point at the291* ktxHashListEntry.292*293* @return KTX_SUCCESS or one of the following error codes.294*295* @exception KTX_INVALID_VALUE if @p This, @p key or @p pValueLen or @p ppValue296* is NULL.297* @exception KTX_NOT_FOUND an entry matching @p key was not found.298*/299KTX_error_code300ktxHashList_FindEntry(ktxHashList* pHead, const char* key,301ktxHashListEntry** ppEntry)302{303if (pHead && key) {304ktxKVListEntry* kv;305306HASH_FIND_STR( *pHead, key, kv ); /* kv: output pointer */307308if (kv) {309*ppEntry = kv;310return KTX_SUCCESS;311} else312return KTX_NOT_FOUND;313} else314return KTX_INVALID_VALUE;315}316317318/**319* @memberof ktxHashList @public320* @~English321* @brief Looks up a key in a hash list and returns the value.322*323* @param [in] pHead pointer to the head of the target hash list.324* @param [in] key pointer to a UTF8 NUL-terminated string to find.325* @param [in,out] pValueLen @p *pValueLen is set to the number of bytes of326* data in the returned value.327* @param [in,out] ppValue @p *ppValue is set to the point to the value for328* @p key.329*330* @return KTX_SUCCESS or one of the following error codes.331*332* @exception KTX_INVALID_VALUE if @p This, @p key or @p pValueLen or @p ppValue333* is NULL.334* @exception KTX_NOT_FOUND an entry matching @p key was not found.335*/336KTX_error_code337ktxHashList_FindValue(ktxHashList *pHead, const char* key, unsigned int* pValueLen, void** ppValue)338{339if (pValueLen && ppValue) {340ktxHashListEntry* pEntry;341KTX_error_code result;342343result = ktxHashList_FindEntry(pHead, key, &pEntry);344if (result == KTX_SUCCESS) {345ktxHashListEntry_GetValue(pEntry, pValueLen, ppValue);346return KTX_SUCCESS;347} else348return result;349} else350return KTX_INVALID_VALUE;351}352353#if !__clang__ && __GNUC__354#pragma GCC diagnostic pop355#endif356357/**358* @memberof ktxHashList @public359* @~English360* @brief Returns the next entry in a ktxHashList.361*362* Use for iterating through the list:363* @code364* ktxHashListEntry* entry;365* for (entry = listHead; entry != NULL; entry = ktxHashList_Next(entry)) {366* ...367* };368* @endcode369*370* Note371*372* @param [in] entry pointer to a hash list entry. Note that a ktxHashList*,373* i.e. the list head, is also a pointer to an entry so374* can be passed to this function.375*376* @return a pointer to the next entry or NULL.377*378*/379ktxHashListEntry*380ktxHashList_Next(ktxHashListEntry* entry)381{382if (entry) {383return ((ktxKVListEntry*)entry)->hh.next;384} else385return NULL;386}387388389/**390* @memberof ktxHashList @public391* @~English392* @brief Serialize a hash list to a block of data suitable for writing393* to a file.394*395* The caller is responsible for freeing the data block returned by this396* function.397*398* @param [in] pHead pointer to the head of the target hash list.399* @param [in,out] pKvdLen @p *pKvdLen is set to the number of bytes of400* data in the returned data block.401* @param [in,out] ppKvd @p *ppKvd is set to the point to the block of402* memory containing the serialized data or403* NULL. if the hash list is empty.404*405* @return KTX_SUCCESS or one of the following error codes.406*407* @exception KTX_INVALID_VALUE if @p This, @p pKvdLen or @p ppKvd is NULL.408* @exception KTX_OUT_OF_MEMORY there was not enough memory to serialize the409* data.410*/411KTX_error_code412ktxHashList_Serialize(ktxHashList* pHead,413unsigned int* pKvdLen, unsigned char** ppKvd)414{415416if (pHead && pKvdLen && ppKvd) {417ktxKVListEntry* kv;418unsigned int bytesOfKeyValueData = 0;419unsigned int keyValueLen;420unsigned char* sd;421char padding[4] = {0, 0, 0, 0};422423for (kv = *pHead; kv != NULL; kv = kv->hh.next) {424/* sizeof(sd) is to make space to write keyAndValueByteSize */425keyValueLen = kv->keyLen + kv->valueLen + sizeof(ktx_uint32_t);426/* Add valuePadding */427keyValueLen = _KTX_PAD4(keyValueLen);428bytesOfKeyValueData += keyValueLen;429}430431if (bytesOfKeyValueData == 0) {432*pKvdLen = 0;433*ppKvd = NULL;434} else {435sd = malloc(bytesOfKeyValueData);436if (!sd)437return KTX_OUT_OF_MEMORY;438439*pKvdLen = bytesOfKeyValueData;440*ppKvd = sd;441442for (kv = *pHead; kv != NULL; kv = kv->hh.next) {443int padLen;444445keyValueLen = kv->keyLen + kv->valueLen;446*(ktx_uint32_t*)sd = keyValueLen;447sd += sizeof(ktx_uint32_t);448memcpy(sd, kv->key, kv->keyLen);449sd += kv->keyLen;450if (kv->valueLen > 0)451memcpy(sd, kv->value, kv->valueLen);452sd += kv->valueLen;453padLen = _KTX_PAD4_LEN(keyValueLen);454memcpy(sd, padding, padLen);455sd += padLen;456}457}458return KTX_SUCCESS;459} else460return KTX_INVALID_VALUE;461}462463464int sort_by_key_codepoint(ktxKVListEntry* a, ktxKVListEntry* b) {465return strcmp(a->key, b->key);466}467468/**469* @memberof ktxHashList @public470* @~English471* @brief Sort a hash list in order of the UTF8 codepoints.472*473* @param [in] pHead pointer to the head of the target hash list.474*475* @return KTX_SUCCESS or one of the following error codes.476*477* @exception KTX_INVALID_VALUE if @p This is NULL.478*/479KTX_error_code480ktxHashList_Sort(ktxHashList* pHead)481{482if (pHead) {483//ktxKVListEntry* kv = (ktxKVListEntry*)pHead;484485HASH_SORT(*pHead, sort_by_key_codepoint);486return KTX_SUCCESS;487} else {488return KTX_INVALID_VALUE;489}490}491492493/**494* @memberof ktxHashList @public495* @~English496* @brief Construct a hash list from a block of serialized key-value497* data read from a file.498* @note The bytes of the 32-bit key-value lengths within the serialized data499* are expected to be in native endianness.500*501* @param [in] pHead pointer to the head of the target hash list.502* @param [in] kvdLen the length of the serialized key-value data.503* @param [in] pKvd pointer to the serialized key-value data.504* table.505*506* @return KTX_SUCCESS or one of the following error codes.507*508* @exception KTX_INVALID_OPERATION if @p pHead does not point to an empty list.509* @exception KTX_INVALID_VALUE if @p pKvd or @p pHt is NULL or kvdLen == 0.510* @exception KTX_OUT_OF_MEMORY there was not enough memory to create the hash511* table.512*/513KTX_error_code514ktxHashList_Deserialize(ktxHashList* pHead, unsigned int kvdLen, void* pKvd)515{516char* src = pKvd;517KTX_error_code result;518519if (kvdLen == 0 || pKvd == NULL || pHead == NULL)520return KTX_INVALID_VALUE;521522if (*pHead != NULL)523return KTX_INVALID_OPERATION;524525result = KTX_SUCCESS;526while (result == KTX_SUCCESS && src < (char *)pKvd + kvdLen) {527if (src + 6 > (char *)pKvd + kvdLen) {528// Not enough space for another entry529return KTX_FILE_DATA_ERROR;530}531532char* key;533unsigned int keyLen, valueLen;534void* value;535ktx_uint32_t keyAndValueByteSize = *((ktx_uint32_t*)src);536537if (src + 4 + keyAndValueByteSize > (char *)pKvd + kvdLen) {538// Not enough space for this entry539return KTX_FILE_DATA_ERROR;540}541542src += sizeof(keyAndValueByteSize);543key = src;544keyLen = 0;545546while (keyLen < keyAndValueByteSize && key[keyLen] != '\0') keyLen++;547548if (keyLen == keyAndValueByteSize || key[keyLen] != '\0') {549// Missing NULL terminator or no value550return KTX_FILE_DATA_ERROR;551}552553if (keyLen >= 3 && key[0] == '\xEF' && key[1] == '\xBB' && key[2] == '\xBF') {554// Forbidden BOM555return KTX_FILE_DATA_ERROR;556}557558keyLen += 1;559value = key + keyLen;560561valueLen = keyAndValueByteSize - keyLen;562result = ktxHashList_AddKVPair(pHead, key, valueLen,563valueLen > 0 ? value : NULL);564if (result == KTX_SUCCESS) {565src += _KTX_PAD4(keyAndValueByteSize);566}567}568return result;569}570571572/**573* @memberof ktxHashListEntry @public574* @~English575* @brief Return the key of a ktxHashListEntry576*577* @param [in] This The target hash list entry.578* @param [in,out] pKeyLen @p *pKeyLen is set to the byte length of579* the returned key.580* @param [in,out] ppKey @p *ppKey is set to the point to the value of581* @p the key.582*583* @return KTX_SUCCESS or one of the following error codes.584*585* @exception KTX_INVALID_VALUE if @p pKvd or @p pHt is NULL or kvdLen == 0.586*/587KTX_error_code588ktxHashListEntry_GetKey(ktxHashListEntry* This,589unsigned int* pKeyLen, char** ppKey)590{591if (pKeyLen && ppKey) {592ktxKVListEntry* kv = (ktxKVListEntry*)This;593*pKeyLen = kv->keyLen;594*ppKey = kv->key;595return KTX_SUCCESS;596} else597return KTX_INVALID_VALUE;598}599600601/**602* @memberof ktxHashListEntry @public603* @~English604* @brief Return the value from a ktxHashListEntry605*606* @param [in] This The target hash list entry.607* @param [in,out] pValueLen @p *pValueLen is set to the number of bytes of608* data in the returned value.609* @param [in,out] ppValue @p *ppValue is set to point to the value of610* of the target entry.611*612* @return KTX_SUCCESS or one of the following error codes.613*614* @exception KTX_INVALID_VALUE if @p pKvd or @p pHt is NULL or kvdLen == 0.615*/616KTX_error_code617ktxHashListEntry_GetValue(ktxHashListEntry* This,618unsigned int* pValueLen, void** ppValue)619{620if (pValueLen && ppValue) {621ktxKVListEntry* kv = (ktxKVListEntry*)This;622*pValueLen = kv->valueLen;623*ppValue = kv->valueLen > 0 ? kv->value : NULL;624return KTX_SUCCESS;625} else626return KTX_INVALID_VALUE;627}628629630