diff options
| author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-08-05 09:56:49 -0700 |
|---|---|---|
| committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-08-05 09:56:49 -0700 |
| commit | e3c5ada3a70f71fe117319cd335a4462387c744d (patch) | |
| tree | 795a9b772147b9ecf48471991ca5e5f7b341c316 /node/Utils.cpp | |
| parent | 7adf0eac7e99337befb421d1b3cf2d64888a9ea2 (diff) | |
| download | infinitytier-e3c5ada3a70f71fe117319cd335a4462387c744d.tar.gz infinitytier-e3c5ada3a70f71fe117319cd335a4462387c744d.zip | |
Add signatures to Dictionary, and fix unhex() API in Utils to be a little safer.
Diffstat (limited to 'node/Utils.cpp')
| -rw-r--r-- | node/Utils.cpp | 46 |
1 files changed, 14 insertions, 32 deletions
diff --git a/node/Utils.cpp b/node/Utils.cpp index a1cdc8df..b1912198 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -97,12 +97,16 @@ std::string Utils::hex(const void *data,unsigned int len) return r; } -std::string Utils::unhex(const char *hex) +std::string Utils::unhex(const char *hex,unsigned int maxlen) { int n = 1; unsigned char c,b = 0; + const char *eof = hex + maxlen; std::string r; + if (!maxlen) + return r; + while ((c = (unsigned char)*(hex++))) { if ((c >= 48)&&(c <= 57)) { // 0..9 if ((n ^= 1)) @@ -117,48 +121,24 @@ std::string Utils::unhex(const char *hex) r.push_back((char)(b | (c - (97 - 10)))); else b = (c - (97 - 10)) << 4; } + if (hex == eof) + break; } return r; } -unsigned int Utils::unhex(const char *hex,void *buf,unsigned int len) +unsigned int Utils::unhex(const char *hex,unsigned int maxlen,void *buf,unsigned int len) { int n = 1; unsigned char c,b = 0; unsigned int l = 0; + const char *eof = hex + maxlen; - while ((c = (unsigned char)*(hex++))) { - if ((c >= 48)&&(c <= 57)) { // 0..9 - if ((n ^= 1)) { - if (l >= len) break; - ((unsigned char *)buf)[l++] = (b | (c - 48)); - } else b = (c - 48) << 4; - } else if ((c >= 65)&&(c <= 70)) { // A..F - if ((n ^= 1)) { - if (l >= len) break; - ((unsigned char *)buf)[l++] = (b | (c - (65 - 10))); - } else b = (c - (65 - 10)) << 4; - } else if ((c >= 97)&&(c <= 102)) { // a..f - if ((n ^= 1)) { - if (l >= len) break; - ((unsigned char *)buf)[l++] = (b | (c - (97 - 10))); - } else b = (c - (97 - 10)) << 4; - } - } - - return l; -} - -unsigned int Utils::unhex(const char *hex,unsigned int hexlen,void *buf,unsigned int len) -{ - int n = 1; - unsigned char c,b = 0; - unsigned int l = 0; - const char *const end = hex + hexlen; + if (!maxlen) + return 0; - while (hex != end) { - c = (unsigned char)*(hex++); + while ((c = (unsigned char)*(hex++))) { if ((c >= 48)&&(c <= 57)) { // 0..9 if ((n ^= 1)) { if (l >= len) break; @@ -175,6 +155,8 @@ unsigned int Utils::unhex(const char *hex,unsigned int hexlen,void *buf,unsigned ((unsigned char *)buf)[l++] = (b | (c - (97 - 10))); } else b = (c - (97 - 10)) << 4; } + if (hex == eof) + break; } return l; |
