Skip to content

Commit

Permalink
Byte swap key-value lengths (issue #40). (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCallow committed Jun 30, 2018
1 parent b08a3a2 commit 1327c01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ ktxHashTable_Serialize(KTX_hash_table This, unsigned int* pKvdLen, unsigned char
*
* The caller is responsible for freeing the returned hash table.
*
* @note The bytes of the 32-bit key-value lengths within the serialized data
* are expected to be in native endianness.
*
* @param [in] kvdLen the length of the serialized key-value data.
* @param [in] pKvd pointer to the serialized key-value data.
* @param [in,out] pHt @p *pHt is set to point to the created hash
Expand Down
12 changes: 12 additions & 0 deletions lib/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,18 @@ ktxLoadTextureS(struct ktxStream* stream, GLuint* pTexture, GLenum* pTarget,

return errorCode;
}
if (header.endianness == KTX_ENDIAN_REF_REV) {
/* Swap the counts inside the key & value data. */
unsigned char* src = *ppKvd;
unsigned char* end = *ppKvd + *pKvdLen;
while (src < end) {
khronos_uint32_t keyAndValueLen = *((khronos_uint32_t*)src);
_ktxSwapEndian32(&keyAndValueLen, 1);
/* Pad keyAndValueByteSize to multiple of 4. */
keyAndValueLen = (keyAndValueLen+3) & ~(khronos_uint32_t)3;
src += keyAndValueLen;
}
}
}
} else {
/* skip key/value metadata */
Expand Down

0 comments on commit 1327c01

Please sign in to comment.