/* * Copyright (C) 2011 Research In Motion Limited. All rights reserved. * Copyright (C) 2016 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #pragma once #include namespace WTF { enum HexConversionMode { Lowercase, Uppercase }; namespace Internal { const LChar lowerHexDigits[17] = "0123456789abcdef"; const LChar upperHexDigits[17] = "0123456789ABCDEF"; inline const LChar* hexDigitsForMode(HexConversionMode mode) { return mode == Lowercase ? lowerHexDigits : upperHexDigits; } }; // namespace Internal template inline void appendByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase) { const LChar* hexDigits = Internal::hexDigitsForMode(mode); destination.append(hexDigits[byte >> 4]); destination.append(hexDigits[byte & 0xF]); } template inline void placeByteAsHexCompressIfPossible(unsigned char byte, T& destination, unsigned& index, HexConversionMode mode = Uppercase) { const LChar* hexDigits = Internal::hexDigitsForMode(mode); if (byte >= 0x10) destination[index++] = hexDigits[byte >> 4]; destination[index++] = hexDigits[byte & 0xF]; } template inline void placeByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase) { const LChar* hexDigits = Internal::hexDigitsForMode(mode); *destination++ = hexDigits[byte >> 4]; *destination++ = hexDigits[byte & 0xF]; } template inline void appendUnsignedAsHex(unsigned number, T& destination, HexConversionMode mode = Uppercase) { const LChar* hexDigits = Internal::hexDigitsForMode(mode); Vector result; do { result.append(hexDigits[number % 16]); number >>= 4; } while (number > 0); result.reverse(); destination.append(result.data(), result.size()); } template inline void appendUnsigned64AsHex(uint64_t number, T& destination, HexConversionMode mode = Uppercase) { const LChar* hexDigits = Internal::hexDigitsForMode(mode); Vector result; do { result.append(hexDigits[number % 16]); number >>= 4; } while (number > 0); result.reverse(); destination.append(result.data(), result.size()); } // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the conversion. template inline void appendUnsignedAsHexFixedSize(unsigned number, T& destination, unsigned desiredDigits, HexConversionMode mode = Uppercase) { ASSERT(desiredDigits); const LChar* hexDigits = Internal::hexDigitsForMode(mode); Vector result; do { result.append(hexDigits[number % 16]); number >>= 4; } while (result.size() < desiredDigits); ASSERT(result.size() == desiredDigits); result.reverse(); destination.append(result.data(), result.size()); } inline bool isHexDigit(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); } inline char uncheckedHexDigit(int i) { if (i < 0 || i > 16) return '0'; return (i >= 10) ? i - 10 + 'A' : i + '0'; } inline bool hexDigitValue(char c, char& result) { if (c >= '0' && c <= '9') { result = c - '0'; return true; } if (c >= 'A' && c <= 'F') { result = c - 'A' + 10; return true; } if (c >= 'a' && c <= 'f') { result = c - 'a' + 10; return true; } return false; } inline int uncheckedHexDigitValue(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'A' && c <= 'F') return c - 'A' + 10; if (c >= 'a' && c <= 'f') return c - 'a' + 10; return 0; } } // namespace WTF using WTF::appendByteAsHex; using WTF::appendUnsignedAsHex; using WTF::appendUnsignedAsHexFixedSize; using WTF::placeByteAsHex; using WTF::placeByteAsHexCompressIfPossible; using WTF::Lowercase; using WTF::isHexDigit; using WTF::uncheckedHexDigit; using WTF::hexDigitValue; using WTF::uncheckedHexDigitValue;