14#include "keyple/core/util/ByteArrayUtil.hpp"
19#include "keyple/core/util/HexUtil.hpp"
20#include "keyple/core/util/KeypleAssert.hpp"
21#include "keyple/core/util/cpp/Character.hpp"
22#include "keyple/core/util/cpp/Pattern.hpp"
23#include "keyple/core/util/cpp/StringBuilder.hpp"
24#include "keyple/core/util/cpp/System.hpp"
25#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
26#include "keyple/core/util/cpp/exception/NegativeArraySizeException.hpp"
32using keyple::core::util::cpp::System;
33using keyple::core::util::cpp::exception::ArrayIndexOutOfBoundsException;
34using keyple::core::util::cpp::exception::IllegalArgumentException;
35using keyple::core::util::cpp::exception::NegativeArraySizeException;
37const std::vector<uint8_t>
38ByteArrayUtil::extractBytes(
39 const std::vector<uint8_t>& src,
const int bitOffset,
const int nbBytes)
42 throw ArrayIndexOutOfBoundsException(
"negative index");
46 throw NegativeArraySizeException(
"negative array size");
49 const int byteOffset = bitOffset / 8;
50 int lBitOffset = bitOffset % 8;
52 if (byteOffset >=
static_cast<int>((src.size() - (lBitOffset ? 1 : 0)))) {
53 throw ArrayIndexOutOfBoundsException(
"pos + offset > src size");
56 std::vector<uint8_t> dest(nbBytes);
58 if (lBitOffset == 0) {
59 System::arraycopy(src, byteOffset, dest, 0, nbBytes);
61 const int rightShift = 8 - bitOffset;
62 for (
int i = 0, j = byteOffset; j < byteOffset + nbBytes; i++, j++) {
64 = ((src[j] << lBitOffset)
65 | ((src[j + 1] & 0xFF) >> rightShift));
72const std::vector<uint8_t>
73ByteArrayUtil::extractBytes(
const uint64_t src,
const int nbBytes)
76 throw NegativeArraySizeException(
"negative array size");
79 std::vector<uint8_t> data(nbBytes);
84 data[i] = ((src >> shift) & 0xFF);
93ByteArrayUtil::extractShort(
const std::vector<uint8_t>& src,
const int offset)
95 if (offset < 0 || offset >
static_cast<int>(src.size() - 2)) {
96 throw ArrayIndexOutOfBoundsException(
97 "offset not in range [0...(src.size() - 2)");
100 return ((src[offset] & 0xFF) << 8) | (src[offset + 1] & 0xFF);
104ByteArrayUtil::extractInt(
105 const std::vector<uint8_t>& src,
111 throw ArrayIndexOutOfBoundsException(
"negative offset");
114 if ((offset + nbBytes) >
static_cast<int>(src.size())) {
115 throw ArrayIndexOutOfBoundsException(
"offset + nbBytes > src size");
120 throw IllegalArgumentException(
"nbBytes can't be bigger than 4");
124 uint32_t complement = 0xFFFFFFFF;
125 int lOffset = offset;
126 int lNbBytes = nbBytes;
127 bool negative =
false;
130 negative = src[lOffset] > 0x7F;
133 while (lNbBytes > 0) {
134 val |= ((src[lOffset++] & 0xFF) << (8 * (--lNbBytes)));
136 0xFF << 8 * lNbBytes);
140 if (isSigned && negative) {
148ByteArrayUtil::extractLong(
149 const std::vector<uint8_t>& src,
154 int lOffset = offset;
155 int lNbBytes = nbBytes;
157 if (lOffset < 0 || lOffset >
static_cast<int>(src.size() - lNbBytes)) {
158 throw ArrayIndexOutOfBoundsException(
159 "offset not in range [0...(src.size() - nbBytes)");
163 uint64_t complement = 0xFFFFFFFFFFFFFFFF;
164 bool negative =
false;
167 while (lNbBytes > 0) {
170 = negative ? true : ((src[lOffset] & 0xFF) > 0x7F ?
true :
false);
172 |= ((
static_cast<uint64_t
>(src[lOffset++] & 0xFF))
173 << (8 * (--lNbBytes)));
174 complement &= ~((
static_cast<uint64_t
>(0xFF)) << 8 * lNbBytes);
178 if (isSigned && negative) {
182 return static_cast<uint64_t
>(val);
186ByteArrayUtil::copyBytes(
188 std::vector<uint8_t>& dest,
192 if (offset < 0 || offset >
static_cast<int>(dest.size() - nbBytes)) {
193 throw ArrayIndexOutOfBoundsException(
194 "offset not in range [0...(dest.size() - nbBytes)");
197 System::arraycopy(extractBytes(src, nbBytes), 0, dest, offset, nbBytes);
201ByteArrayUtil::isValidHexString(
const std::string& hex)
203 return HexUtil::isValid(hex);
207ByteArrayUtil::normalizeHexString(
const std::string& hex)
209 if (hex.length() % 2 != 0) {
217ByteArrayUtil::fromHex(
const std::string& hex)
219 Assert::getInstance()
220 .notEmpty(hex,
"hex")
221 .isEqual(hex.length() % 2, 0,
"hex size");
223 return HexUtil::toByteArray(hex);
227ByteArrayUtil::toHex(
const std::vector<uint8_t>& src)
233 return HexUtil::toHex(src);
237ByteArrayUtil::twoBytesToInt(
238 const std::vector<uint8_t>& bytes,
const int offset)
240 return extractInt(bytes, offset, 2,
false);
244ByteArrayUtil::twoBytesSignedToInt(
245 const std::vector<uint8_t>& bytes,
const int offset)
247 return extractInt(bytes, offset, 2,
true);
251ByteArrayUtil::threeBytesToInt(
252 const std::vector<uint8_t>& bytes,
const int offset)
254 return extractInt(bytes, offset, 3,
false);
258ByteArrayUtil::threeBytesSignedToInt(
259 const std::vector<uint8_t>& bytes,
const int offset)
261 return extractInt(bytes, offset, 3,
true);
265ByteArrayUtil::fourBytesToInt(
266 const std::vector<uint8_t>& bytes,
const int offset)
268 return extractInt(bytes, offset, 4,
true);