File indexing completed on 2025-01-26 04:57:23

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    Code based in v4_rice.cc
0005 
0006    SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "riceencodingdecoder.h"
0010 #include "webengineviewer_debug.h"
0011 
0012 #ifdef WIN32
0013 #include <Winsock2.h>
0014 #else
0015 #include <netinet/in.h>
0016 #endif
0017 
0018 namespace
0019 {
0020 const int kBitsPerByte = 8;
0021 const unsigned int kMaxBitIndex = kBitsPerByte * sizeof(uint32_t);
0022 }
0023 
0024 using namespace WebEngineViewer;
0025 RiceEncodingDecoder::RiceEncodingDecoder() = default;
0026 
0027 RiceEncodingDecoder::~RiceEncodingDecoder() = default;
0028 
0029 QList<quint32> RiceEncodingDecoder::decodeRiceIndiceDelta(const RiceDeltaEncoding &riceDeltaEncoding)
0030 {
0031     bool ok;
0032     QList<quint32> list;
0033     if (riceDeltaEncoding.firstValue.isEmpty()) {
0034         return list;
0035     }
0036     quint64 firstValue = riceDeltaEncoding.firstValue.toInt(&ok);
0037     if (!ok) {
0038         qCWarning(WEBENGINEVIEWER_LOG) << "First value is not a int value " << riceDeltaEncoding.firstValue;
0039         return {};
0040     }
0041     list.reserve(riceDeltaEncoding.numberEntries + 1);
0042     list << firstValue;
0043     if (riceDeltaEncoding.numberEntries == 0) {
0044         return list;
0045     }
0046 
0047     RiceDecoder decoder(riceDeltaEncoding.riceParameter, riceDeltaEncoding.numberEntries, riceDeltaEncoding.encodingData);
0048     int lastValue(firstValue);
0049     while (decoder.hasOtherEntries()) {
0050         quint32 offset;
0051         bool result = decoder.nextValue(&offset);
0052         if (!result) {
0053             return {};
0054         }
0055         lastValue += offset;
0056 #if 0
0057 
0058         if (!last_value.IsValid()) {
0059             return false;
0060         }
0061 #endif
0062 
0063         list << lastValue;
0064     }
0065     return list;
0066 }
0067 
0068 QList<quint32> RiceEncodingDecoder::decodeRiceHashesDelta(const RiceDeltaEncoding &riceDeltaEncoding)
0069 {
0070     QList<quint32> list;
0071     bool ok = false;
0072     quint64 firstValue = riceDeltaEncoding.firstValue.toInt(&ok);
0073     if (!ok) {
0074         qCWarning(WEBENGINEVIEWER_LOG) << "First value is not a int value " << riceDeltaEncoding.firstValue;
0075         return list;
0076     }
0077 
0078     list.reserve(riceDeltaEncoding.numberEntries + 1);
0079     RiceDecoder decoder(riceDeltaEncoding.riceParameter, riceDeltaEncoding.numberEntries, riceDeltaEncoding.encodingData);
0080     int lastValue(firstValue);
0081     list << htonl(lastValue);
0082 
0083     while (decoder.hasOtherEntries()) {
0084         quint32 offset;
0085         bool result = decoder.nextValue(&offset);
0086         if (!result) {
0087             return {};
0088         }
0089 
0090         lastValue += offset;
0091 #if 0
0092         if (!last_value) {
0093             return false;
0094         }
0095 #endif
0096         // This flipping is done so that the decoded uint32 is interpreted
0097         // correctly as a string of 4 bytes.
0098         list << htonl(lastValue);
0099     }
0100 
0101     // Flipping the bytes, as done above, destroys the sort order. Sort the
0102     // values back.
0103     std::sort(list.begin(), list.end());
0104 
0105     // This flipping is done so that when the vector is interpreted as a string,
0106     // the bytes are in the correct order.
0107     QList<quint32> newList;
0108     newList.reserve(list.count());
0109     const int listCount(list.count());
0110     for (int i = 0; i < listCount; ++i) {
0111         newList << ntohl(list.at(i));
0112     }
0113     return newList;
0114 }
0115 
0116 RiceDecoder::RiceDecoder(int riceParameter, int numberEntries, const QByteArray &encodingData)
0117     : mEncodingData(encodingData)
0118     , mRiceParameter(riceParameter)
0119     , mNumberEntries(numberEntries)
0120     , mCurrentWord(0)
0121 {
0122     mDataByteIndex = 0;
0123     mCurrentWordBitIndex = kMaxBitIndex;
0124 }
0125 
0126 RiceDecoder::~RiceDecoder() = default;
0127 
0128 bool RiceDecoder::hasOtherEntries() const
0129 {
0130     return mNumberEntries > 0;
0131 }
0132 
0133 bool RiceDecoder::nextValue(uint32_t *value)
0134 {
0135     if (!hasOtherEntries()) {
0136         return false;
0137     }
0138     bool result;
0139     uint32_t q = 0;
0140     uint32_t bit;
0141     do {
0142         result = nextBits(1, &bit);
0143         if (!result) {
0144             return false;
0145         }
0146         q += bit;
0147     } while (bit);
0148     uint32_t r = 0;
0149     result = nextBits(mRiceParameter, &r);
0150     if (!result) {
0151         return false;
0152     }
0153 
0154     *value = (q << mRiceParameter) + r;
0155     mNumberEntries--;
0156     return true;
0157 }
0158 
0159 bool RiceDecoder::nextBits(unsigned int numRequestedBits, uint32_t *x)
0160 {
0161     if (numRequestedBits > kMaxBitIndex) {
0162         return false;
0163     }
0164     if (mCurrentWordBitIndex == kMaxBitIndex) {
0165         bool result = nextWord(&mCurrentWord);
0166         if (!result) {
0167             return false;
0168         }
0169     }
0170     unsigned int num_bits_left_in_current_word = kMaxBitIndex - mCurrentWordBitIndex;
0171     if (num_bits_left_in_current_word >= numRequestedBits) {
0172         // All the bits that we need are in |mCurrentWord|.
0173         *x = bitsFromCurrentWord(numRequestedBits);
0174     } else {
0175         // |mCurrentWord| contains fewer bits than we need so read the remaining
0176         // bits from |mCurrentWord| into |lower|, and then call nextBits on the
0177         // remaining number of bits, which will read in a new word into
0178         // |mCurrentWord|.
0179         uint32_t lower = bitsFromCurrentWord(num_bits_left_in_current_word);
0180 
0181         unsigned int num_bits_from_next_word = numRequestedBits - num_bits_left_in_current_word;
0182         uint32_t upper;
0183         bool result = nextBits(num_bits_from_next_word, &upper);
0184         if (!result) {
0185             return false;
0186         }
0187         *x = (upper << num_bits_left_in_current_word) | lower;
0188     }
0189     return true;
0190 }
0191 
0192 bool RiceDecoder::nextWord(uint32_t *word)
0193 {
0194     if (mDataByteIndex >= mEncodingData.size()) {
0195         return false;
0196     }
0197 
0198     const size_t mask = 0xFF;
0199     *word = (mEncodingData[mDataByteIndex] & mask);
0200     mDataByteIndex++;
0201     mCurrentWordBitIndex = 0;
0202 
0203     if (mDataByteIndex < mEncodingData.size()) {
0204         *word |= ((mEncodingData[mDataByteIndex] & mask) << 8);
0205         mDataByteIndex++;
0206 
0207         if (mDataByteIndex < mEncodingData.size()) {
0208             *word |= ((mEncodingData[mDataByteIndex] & mask) << 16);
0209             mDataByteIndex++;
0210 
0211             if (mDataByteIndex < mEncodingData.size()) {
0212                 *word |= ((mEncodingData[mDataByteIndex] & mask) << 24);
0213                 mDataByteIndex++;
0214             }
0215         }
0216     }
0217     return true;
0218 }
0219 
0220 uint32_t RiceDecoder::bitsFromCurrentWord(unsigned int numRequestedBits)
0221 {
0222     uint32_t mask = 0xFFFFFFFF >> (kMaxBitIndex - numRequestedBits);
0223     uint32_t x = mCurrentWord & mask;
0224     mCurrentWord = mCurrentWord >> numRequestedBits;
0225     mCurrentWordBitIndex += numRequestedBits;
0226     return x;
0227 }