common/compressor: added djb2 encoder

This is useful for encoding the string into one long type value.
This commit is contained in:
Hermet Park 2024-05-27 18:42:36 +09:00 committed by Hermet Park
parent 7ce8db4d34
commit ee99fe3942
2 changed files with 16 additions and 0 deletions

View file

@ -472,4 +472,19 @@ size_t b64Decode(const char* encoded, const size_t len, char** decoded)
}
/************************************************************************/
/* DJB2 Implementation */
/************************************************************************/
unsigned long djb2Encode(const char* str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; // hash * 33 + c
}
return hash;
}
}

View file

@ -30,6 +30,7 @@ namespace tvg
uint8_t* lzwEncode(const uint8_t* uncompressed, uint32_t uncompressedSizeBytes, uint32_t* compressedSizeBytes, uint32_t* compressedSizeBits);
uint8_t* lzwDecode(const uint8_t* compressed, uint32_t compressedSizeBytes, uint32_t compressedSizeBits, uint32_t uncompressedSizeBytes);
size_t b64Decode(const char* encoded, const size_t len, char** decoded);
unsigned long djb2Encode(const char* str);
}
#endif //_TVG_COMPRESSOR_H_