From ee99fe39428005dae7554a3a80c16f46eb9e945c Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 27 May 2024 18:42:36 +0900 Subject: [PATCH] common/compressor: added djb2 encoder This is useful for encoding the string into one long type value. --- src/common/tvgCompressor.cpp | 15 +++++++++++++++ src/common/tvgCompressor.h | 1 + 2 files changed, 16 insertions(+) diff --git a/src/common/tvgCompressor.cpp b/src/common/tvgCompressor.cpp index 709cb22f..e7560e79 100644 --- a/src/common/tvgCompressor.cpp +++ b/src/common/tvgCompressor.cpp @@ -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; +} + } \ No newline at end of file diff --git a/src/common/tvgCompressor.h b/src/common/tvgCompressor.h index 0756127e..b043cc77 100644 --- a/src/common/tvgCompressor.h +++ b/src/common/tvgCompressor.h @@ -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_