diff options
author | Charlie Swanson <charlie.swanson@mongodb.com> | 2018-04-09 14:12:05 -0400 |
---|---|---|
committer | Charlie Swanson <charlie.swanson@mongodb.com> | 2018-04-13 16:18:35 -0400 |
commit | a820491e9402a52d7575157a9897306d49129370 (patch) | |
tree | 61ed25fdec3912a8fc1407bcb52380b110b697fa /src/mongo/util/hex.h | |
parent | 4b894b4a55467c38bb7910317af00793b493de37 (diff) | |
download | mongo-a820491e9402a52d7575157a9897306d49129370.tar.gz |
SERVER-34313 Use hex-encoded string for resume token
Diffstat (limited to 'src/mongo/util/hex.h')
-rw-r--r-- | src/mongo/util/hex.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/mongo/util/hex.h b/src/mongo/util/hex.h index 802d6b13737..382eb0bd86c 100644 --- a/src/mongo/util/hex.h +++ b/src/mongo/util/hex.h @@ -29,6 +29,8 @@ #pragma once +#include <algorithm> +#include <cctype> #include <string> #include "mongo/base/string_data.h" @@ -53,6 +55,27 @@ inline char fromHex(StringData c) { return (char)((fromHex(c[0]) << 4) | fromHex(c[1])); } +/** + * Decodes 'hexString' into raw bytes, appended to the out parameter 'buf'. Callers must first + * ensure that 'hexString' is a valid hex encoding. + */ +inline void fromHexString(StringData hexString, BufBuilder* buf) { + invariant(hexString.size() % 2 == 0); + // Combine every pair of two characters into one byte. + for (std::size_t i = 0; i < hexString.size(); i += 2) { + buf->appendChar(fromHex(StringData(&hexString.rawData()[i], 2))); + } +} + +/** + * Returns true if 'hexString' is a valid hexadecimal encoding. + */ +inline bool isValidHex(StringData hexString) { + // There must be an even number of characters, since each pair encodes a single byte. + return hexString.size() % 2 == 0 && + std::all_of(hexString.begin(), hexString.end(), [](char c) { return std::isxdigit(c); }); +} + inline std::string toHex(const void* inRaw, int len) { static const char hexchars[] = "0123456789ABCDEF"; |