1/*
2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
3 * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#pragma once
22
23#include <array>
24#include <wtf/text/StringBuilder.h>
25#include <wtf/text/StringConcatenate.h>
26
27namespace WTF {
28
29enum HexConversionMode { Lowercase, Uppercase };
30
31namespace Internal {
32
33inline const LChar* hexDigitsForMode(HexConversionMode mode)
34{
35 static const LChar lowercaseHexDigits[17] = "0123456789abcdef";
36 static const LChar uppercaseHexDigits[17] = "0123456789ABCDEF";
37 return mode == Lowercase ? lowercaseHexDigits : uppercaseHexDigits;
38}
39
40WTF_EXPORT std::pair<LChar*, unsigned> appendHex(LChar* buffer, unsigned bufferSize, std::uintmax_t number, unsigned minimumDigits, HexConversionMode);
41
42template<size_t arraySize, typename NumberType>
43inline std::pair<LChar*, unsigned> appendHex(std::array<LChar, arraySize>& buffer, NumberType number, unsigned minimumDigits, HexConversionMode mode)
44{
45 return appendHex(&buffer.front(), buffer.size(), static_cast<typename std::make_unsigned<NumberType>::type>(number), minimumDigits, mode);
46}
47
48} // namespace Internal
49
50template<typename T>
51inline void appendByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase)
52{
53 auto* hexDigits = Internal::hexDigitsForMode(mode);
54 destination.append(hexDigits[byte >> 4]);
55 destination.append(hexDigits[byte & 0xF]);
56}
57
58// FIXME: Consider renaming to appendHex.
59template<typename NumberType, typename DestinationType>
60inline void appendUnsignedAsHex(NumberType number, DestinationType& destination, HexConversionMode mode = Uppercase)
61{
62 appendUnsignedAsHexFixedSize(number, destination, 0, mode);
63}
64
65// FIXME: Consider renaming to appendHex.
66// Same as appendUnsignedAsHex, but zero-padding to get at least the desired number of digits.
67template<typename NumberType, typename DestinationType>
68inline void appendUnsignedAsHexFixedSize(NumberType number, DestinationType& destination, unsigned minimumDigits, HexConversionMode mode = Uppercase)
69{
70 // Each byte can generate up to two digits.
71 std::array<LChar, sizeof(NumberType) * 2> buffer;
72 auto result = Internal::appendHex(buffer, number, minimumDigits, mode);
73 destination.append(result.first, result.second);
74}
75
76// FIXME: Consider renaming to appendHex.
77// Same as appendUnsignedAsHex, but zero-padding to get at least the desired number of digits.
78template<typename NumberType>
79inline void appendUnsignedAsHexFixedSize(NumberType number, StringBuilder& destination, unsigned minimumDigits, HexConversionMode mode = Uppercase)
80{
81 // Each byte can generate up to two digits.
82 std::array<LChar, sizeof(NumberType) * 2> buffer;
83 auto result = Internal::appendHex(buffer, number, minimumDigits, mode);
84 destination.appendCharacters(result.first, result.second);
85}
86
87struct HexNumberBuffer {
88 WTF_MAKE_STRUCT_FAST_ALLOCATED;
89
90 std::array<LChar, 16> characters;
91 unsigned length;
92};
93
94template<typename NumberType> HexNumberBuffer hex(NumberType number, unsigned minimumDigits = 0, HexConversionMode mode = Uppercase)
95{
96 // Each byte can generate up to two digits.
97 HexNumberBuffer buffer;
98 static_assert(sizeof(buffer.characters) >= sizeof(NumberType) * 2, "number too large for hexNumber");
99 auto result = Internal::appendHex(buffer.characters, number, minimumDigits, mode);
100 buffer.length = result.second;
101 return buffer;
102}
103
104template<typename NumberType> HexNumberBuffer hex(NumberType number, HexConversionMode mode)
105{
106 return hex(number, 0, mode);
107}
108
109template<> class StringTypeAdapter<HexNumberBuffer> {
110public:
111 StringTypeAdapter(const HexNumberBuffer& buffer)
112 : m_buffer { buffer }
113 {
114 }
115
116 unsigned length() const { return m_buffer.length; }
117 bool is8Bit() const { return true; }
118 template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, characters(), length()); }
119
120private:
121 const LChar* characters() const { return &*(m_buffer.characters.end() - length()); }
122
123 const HexNumberBuffer& m_buffer;
124};
125
126} // namespace WTF
127
128using WTF::appendByteAsHex;
129using WTF::appendUnsignedAsHex;
130using WTF::appendUnsignedAsHexFixedSize;
131using WTF::hex;
132using WTF::Lowercase;
133