diff options
author | George Hazan <george.hazan@gmail.com> | 2015-12-21 14:26:08 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2015-12-21 14:26:08 +0000 |
commit | 3986b91d0e344e11b5953657fa1fb3e85d6da869 (patch) | |
tree | 8e27f4bb37db4fe8e279a9daab389bafb2498a21 /src | |
parent | 8b126dc48d0e4fc3585a723140f8da83a55eb5b3 (diff) |
hex2bin / hex2bin -> fast hex decoder from char / WCHAR to binary data
git-svn-id: http://svn.miranda-ng.org/main/trunk@15925 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src')
-rw-r--r-- | src/mir_core/src/mir_core.def | 2 | ||||
-rw-r--r-- | src/mir_core/src/mir_core64.def | 2 | ||||
-rw-r--r-- | src/mir_core/src/utils.cpp | 50 |
3 files changed, 54 insertions, 0 deletions
diff --git a/src/mir_core/src/mir_core.def b/src/mir_core/src/mir_core.def index fb89dde82b..c5ff0c1fbc 100644 --- a/src/mir_core/src/mir_core.def +++ b/src/mir_core/src/mir_core.def @@ -988,3 +988,5 @@ mir_sha256_hash @1145 mir_sha256_init @1146
mir_sha256_write @1147
mir_forkthreadowner @1148
+hex2bin @1149
+hex2binW @1150
diff --git a/src/mir_core/src/mir_core64.def b/src/mir_core/src/mir_core64.def index f7cb356fdb..3d660b330c 100644 --- a/src/mir_core/src/mir_core64.def +++ b/src/mir_core/src/mir_core64.def @@ -988,3 +988,5 @@ mir_sha256_hash @1145 mir_sha256_init @1146
mir_sha256_write @1147
mir_forkthreadowner @1148
+hex2bin @1149
+hex2binW @1150
diff --git a/src/mir_core/src/utils.cpp b/src/mir_core/src/utils.cpp index 91b057b301..a2f1ab0171 100644 --- a/src/mir_core/src/utils.cpp +++ b/src/mir_core/src/utils.cpp @@ -282,6 +282,56 @@ MIR_CORE_DLL(WCHAR*) bin2hexW(const void *pData, size_t len, WCHAR *dest) return dest;
}
+static int hex2dec(int iHex)
+{
+ if (iHex >= '0' && iHex <= '9')
+ return iHex - '0';
+ if (iHex >= 'a' && iHex <= 'f')
+ return iHex - 'a' + 10;
+ if (iHex >= 'A' && iHex <= 'F')
+ return iHex - 'A' + 10;
+ return 0;
+}
+
+MIR_CORE_DLL(bool) hex2bin(const char *pSrc, void *pData, size_t len)
+{
+ if (pSrc == NULL || pData == NULL || len == 0)
+ return false;
+
+ size_t bufLen = strlen(pSrc)/2;
+ if (pSrc[bufLen*2] != 0 || bufLen > len)
+ return false;
+
+ BYTE *pDest = (BYTE*)pData;
+ const char *p = (const char *)pSrc;
+ for (size_t i = 0; i < bufLen; i++, p += 2)
+ pDest[i] = hex2dec(p[0]) * 16 + hex2dec(p[1]);
+
+ if (bufLen < len)
+ memset(pDest + bufLen, 0, len - bufLen);
+ return true;
+}
+
+MIR_CORE_DLL(bool) hex2binW(const wchar_t *pSrc, void *pData, size_t len)
+{
+ if (pSrc == NULL || pData == NULL || len == 0)
+ return false;
+
+ size_t bufLen = wcslen(pSrc);
+ if (pSrc[bufLen * 2] != 0 || bufLen > len)
+ return false;
+
+ BYTE *pDest = (BYTE*)pData;
+ const wchar_t *p = (const wchar_t *)pSrc;
+ for (size_t i = 0; i < bufLen; i++, p += 2)
+ pDest[i] = hex2dec(p[0]) * 16 + hex2dec(p[1]);
+
+ if (bufLen < len)
+ memset(pDest+bufLen, 0, len - bufLen);
+ return true;
+}
+
+
/////////////////////////////////////////////////////////////////////////////////////////
#pragma intrinsic(strlen, strcpy, strcat, strcmp, wcslen, wcscpy, wcscat, wcscmp)
|