diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index ea94a6c..9788a04 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -93,8 +93,7 @@ QrCode QrCode::encodeSegments(const vector &segs, const Ecc &ecl, for (const QrSegment &seg : segs) { bb.appendBits(seg.mode.modeBits, 4); bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version)); - for (int i = 0; i < seg.bitLength; i++) - bb.push_back(((seg.data.at(i >> 3) >> (7 - (i & 7))) & 1) != 0); + bb.insert(bb.end(), seg.data.begin(), seg.data.end()); } // Add terminator and pad up to a byte if applicable diff --git a/cpp/QrSegment.cpp b/cpp/QrSegment.cpp index 9fd42ff..c17294d 100644 --- a/cpp/QrSegment.cpp +++ b/cpp/QrSegment.cpp @@ -146,16 +146,11 @@ QrSegment QrSegment::makeEci(long assignVal) { } -QrSegment::QrSegment(const Mode &md, int numCh, const BitBuffer &data) - : QrSegment(md, numCh, data.getBytes(), data.size()) {} - - -QrSegment::QrSegment(const Mode &md, int numCh, const vector &b, int bitLen) : +QrSegment::QrSegment(const Mode &md, int numCh, const BitBuffer &dt) : mode(md), numChars(numCh), - data(b), - bitLength(bitLen) { - if (numCh < 0 || bitLen < 0 || b.size() != static_cast((bitLen + 7) / 8)) + data(dt) { + if (numCh < 0) throw "Invalid value"; } @@ -167,9 +162,9 @@ int QrSegment::getTotalBits(const vector &segs, int version) { for (const QrSegment &seg : segs) { int ccbits = seg.mode.numCharCountBits(version); // Fail if segment length value doesn't fit in the length field's bit-width - if (seg.numChars >= (1L << ccbits) || seg.bitLength > INT16_MAX) + if (seg.numChars >= (1L << ccbits) || seg.data.size() > INT16_MAX) return -1; - long temp = (long)result + 4 + ccbits + seg.bitLength; + long temp = (long)result + 4 + ccbits + seg.data.size(); if (temp > INT_MAX) return -1; result = temp; diff --git a/cpp/QrSegment.hpp b/cpp/QrSegment.hpp index ef99d55..65ba012 100644 --- a/cpp/QrSegment.hpp +++ b/cpp/QrSegment.hpp @@ -137,11 +137,8 @@ class QrSegment final { /* The length of this segment's unencoded data, measured in characters. Always zero or positive. */ public: const int numChars; - /* The bits of this segment packed into a byte array in big endian. */ - public: const std::vector data; - - /* The length of this segment's encoded data, measured in bits. Satisfies ceil(bitLength / 8) = data.size(). */ - public: const int bitLength; + /* The data bits of this segment. */ + public: const BitBuffer data; /*---- Constructor ----*/ @@ -149,10 +146,7 @@ class QrSegment final { /* * Creates a new QR Code data segment with the given parameters and data. */ - public: QrSegment(const Mode &md, int numCh, const BitBuffer &data); - - - public: QrSegment(const Mode &md, int numCh, const std::vector &b, int bitLen); + public: QrSegment(const Mode &md, int numCh, const BitBuffer &dt); // Package-private helper function.