From 5830f4225cf5e7019b4380e61afad71a9fe629b2 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Mon, 18 Jun 2018 16:51:06 +0200 Subject: [PATCH] bugfix: fixed writing mask for charsize increase --- src/bitpack.cc | 66 +++++++++++++++++++---------------------------- src/uncompress.cc | 3 --- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/src/bitpack.cc b/src/bitpack.cc index a0a9cd6..8740cda 100644 --- a/src/bitpack.cc +++ b/src/bitpack.cc @@ -52,12 +52,6 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin, } const int max_value = max(t_n); // max value held within t_n bits -#ifdef Debug - std::printf("%d bits! %ld chars remaining\n", t_n, - std::distance(t_input_begin, t_input_end)); - std::printf("max: %d\n", max_value); -#endif - int step = t_n % 8; int left_shift = 0; int right_shift = 0; @@ -66,28 +60,30 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin, // pour chaque élément for (auto it = t_input_begin; it != t_input_end; ++it) { + // si on a atteint ou dépassé la valeur maximale, on change de nombre de bits + if (*it >= max_value) { + // écriture du masque pour notifier à la décompression du changement de bits + if ((left_shift += step) >= t_n) { + left_shift = (left_shift - t_n) + step; + } + const auto mask = masks[t_n] >> left_shift; + t_res.push_back( + static_cast(current_char | mask)); + bool zero_rs = (right_shift == 0); - // si on a atteint ou dépassé la valeur maximale, on change de nombre de bits - if (*it >= max_value) { - // écriture du masque pour notifier à la décompression du changement de bits - if ((left_shift += step) >= t_n) { - left_shift = (left_shift - t_n) + step; - } - const auto mask = masks[t_n] >> left_shift; - t_res.push_back( - static_cast(current_char | mask)); - bool zero_rs = (right_shift == 0); - right_shift -= 0; - if(right_shift < 0) { - if(!zero_rs) { - current_char = static_cast(masks[t_n] >> (-right_shift) & 0xffu); - t_res.push_back(current_char); - } - } - t_res.push_back(static_cast(masks[t_n])); - return pack_n(it, t_input_end, t_res, t_n + 1); - } + right_shift -= step; + if(right_shift < 0) { // si right_shift est inférieur à zéro + // si right_shift était différent de zéro, alors extra octet + if (!zero_rs) { + current_char = static_cast(masks[t_n] >> (-right_shift) & 0xffu); + t_res.push_back(current_char); + } + } + t_res.push_back(static_cast(masks[t_n])); + return pack_n(it, t_input_end, t_res, t_n + 1); + } + // écriture normale if ((left_shift += step) >= t_n) { left_shift = (left_shift - t_n) + step; } @@ -122,13 +118,9 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin, vuchar pack_16(const vuint16::const_iterator t_input_begin, const vuint16::const_iterator t_input_end, vuchar t_res) { -#ifdef Debug - std::printf("16 bits! %ld chars remaining\n", - std::distance(t_input_begin, t_input_end)); -#endif std::for_each(t_input_begin, t_input_end, [&](const auto value) { - t_res.push_back((value >> 8) & 0xFFu); - t_res.push_back(value & 0xFFu); + t_res.push_back(static_cast(value >> 8 & 0xFFu)); + t_res.push_back(static_cast(value & 0xFFu)); }); return t_res; } @@ -143,10 +135,6 @@ vuint16 unpack(ustring &&t_input) { vuint16 unpack_n(const ustring::const_iterator t_begin, const ustring::const_iterator t_end, vuint16 t_res, int t_n) { -#ifdef Debug - std::printf("Chunk! %d bits, %ld compressed chars\n", t_n, - std::distance(t_begin, t_end)); -#endif if (t_n == 16) { return unpack_16(t_begin, t_end, vector()); } @@ -157,9 +145,9 @@ vuint16 unpack_n(const ustring::const_iterator t_begin, for (auto it = t_begin; it < t_end - 1; /* nope */) { uint16_t current_char = 0; // left bits - if ((left_shift += step) >= t_n) { - left_shift = (left_shift - t_n) + step; - } + if ((left_shift += step) >= t_n) { + left_shift = (left_shift - t_n) + step; + } current_char = static_cast(*it << left_shift) & masks[t_n]; // right bits bool zero_rs = (right_shift == 0); diff --git a/src/uncompress.cc b/src/uncompress.cc index 28ca44f..b1427a2 100644 --- a/src/uncompress.cc +++ b/src/uncompress.cc @@ -24,9 +24,6 @@ ustring lzw_uncompress(vuint16 &&t_compressed) { for (auto it = t_compressed.begin() + 1; it != t_compressed.end(); ++it) { v = *it; const auto uncompressed{dico_uncompress(dict, v, old)}; -#ifdef Debug - std::printf("%d = %s\n", v, uncompressed.c_str()); -#endif ret.insert(ret.end(), uncompressed.begin(), uncompressed.end()); old = v; }