diff --git a/lib/encode.cpp b/lib/encode.cpp index 32bd8da6..41609a6f 100644 --- a/lib/encode.cpp +++ b/lib/encode.cpp @@ -102,6 +102,16 @@ namespace Encodings { return r; } + /// Decodes a hex-encoded std::string to a raw binary std::string. + std::string Hex::decode(const std::string & in){ + std::string ret(in.size()/2, '\000'); + for (size_t i = 0; i < in.size(); ++i){ + char c = in[i]; + ret[i>>1] |= ((c&15) + (((c&64)>>6) | ((c&64)>>3))) << ((~i&1) << 2); + } + return ret; + } + /// urlencodes std::string data, leaving only the characters A-Za-z0-9~!&()' alone. std::string URL::encode(const std::string & c){ std::string escaped = ""; diff --git a/lib/encode.h b/lib/encode.h index c7fb2eac..a604bb42 100644 --- a/lib/encode.h +++ b/lib/encode.h @@ -33,6 +33,9 @@ namespace Encodings { } /// Encodes a single character as two hex digits in string form. static std::string chr(char dec); + + /// Decodes a hex-encoded std::string to a raw binary std::string. + static std::string decode(const std::string & in); }; }