Add TinyDeflate as dependency This is a combination of the following commits from the current master, rebased onto the Release_1_9_6 tag: 966d69c60 Add TinyDeflate as dependency 7b2a60277 issue #9319: Doc build fails with cairo 1.17.6 eba7dee87 Update README.md added link to the original source f3514d578 Fix and work around compiler warnings and remove dead code 8129939c3 issue #9319: Doc build fails with cairo 1.17.6 - Improve detection of "flate" encoded streams This adds patched deflate detection and support to doxygen-1.9.6, to fix Gentoo bug #884185 (doxygen fails to emerge with cairo-1.17.6). diff --git a/TinyDeflate/README.md b/TinyDeflate/README.md new file mode 100644 --- /dev/null +++ b/TinyDeflate/README.md @@ -0,0 +1,362 @@ +# TinyDeflate + +See https://github.com/bisqwit/TinyDeflate for the original version. + +A deflate/gzip decompressor, as a C++17 template function, +that requires minimal amount of memory to work. + + Terms of use: Zlib + Copyright © 2018 Joel Yliluoma + +## Memory usage at aggressive settings (backtrackable input) + +* 408 bytes of automatic storage for three huffman trees (384 elements, 5…9 bits each, 88 % space efficiency) +* 24 bytes of temporary automatic storage while a huffman tree is being generated (15 elements, 9 bits each, 66 % space efficiency) +* An assortment of automatic variables for various purposes (may be register variables, depending on the architecture and of the compiler wits) +* ABI mandated alignment losses + +Total: 408 bytes minimum, 432+N bytes maximum + +Theoretical minimum at 100 % efficiency: 357.1 + 15.32 ≃ 373 bytes (not yet attained by this library). + +## Memory usage at aggressive settings (non-backtrackable input) + +* 144 bytes of automatic storage for length tables (288 elements, 4 bits each, 100 % space efficiency) +* 384 bytes of automatic storage for two huffman trees (350 elements, 5…9 bits each, 88 % space efficiency) +* 24 bytes of temporary automatic storage while a huffman tree is being generated (15 elements, 9 bits each, 66 % space efficiency) +* An assortment of automatic variables for various purposes (may be register variables, depending on the architecture and of the compiler wits) +* ABI mandated alignment losses + +Total: 528 bytes minimum, 552+N bytes maximum + +Theoretical minimum at 100 % efficiency: 144 + 338.9 + 15.32 ≃ 499 bytes (not yet attained by this library). + +## Memory usage at default settings (backtrackable input) + +* 687 bytes of automatic storage for three huffman trees (52 % space efficiency) +* 30 bytes of temporary automatic storage while a huffman tree is being generated (53 % space efficiency) +* An assortment of automatic variables for various purposes (may be register variables, depending on the architecture and of the compiler wits) +* ABI mandated alignment losses + +Total: 687 bytes minimum, 717+N bytes maximum + +## Memory usage at default settings (non-backtrackable input) + +* 288 bytes of automatic storage for length tables (50 % space efficiency) +* 653 bytes of automatic storage for two huffman trees (52 % space efficiency) +* 30 bytes of temporary automatic storage while a huffman tree is being generated (53 % space efficiency) +* An assortment of automatic variables for various purposes (may be register variables, depending on the architecture and of the compiler wits) +* ABI mandated alignment losses + +Total: 941 bytes minimum, 971+N bytes maximum + +## Tuning + +To adjust the memory usage, there are three settings in gunzip.hh you can change: + +| Setting name | 'false' memory use bytes | 'true' memory use bytes | 'true' performance impact +| ------------------------------------------- | ---:| ----:|-------------- +| `USE_BITARRAY_TEMPORARY_IN_HUFFMAN_CREATION` | 30 | 24 | Negligible +| `USE_BITARRAY_FOR_LENGTHS` | 288 or 0 | 144 or 0 | Noticeable +| `USE_BITARRAY_FOR_HUFFNODES` | 653 or 687 | 384 or 408 | Significant +| **Total** | 971 or 717 | 552 or 432 | _Plus alignment losses, callframes and spills_ + +In addition, if you neither decompress into a raw memory area nor supply your own window function, +32768 bytes of automatic storage is allocated for the look-behind window. + +You can also change the memory allocation scheme: + +| `#define` name | Meaning +| --- | --- +| `DEFLATE_ALLOCATION_AUTOMATIC` | Automatic allocation (usually stack) +| `DEFLATE_ALLOCATION_STATIC` | Static `thread_local` allocation (memory remains allocated throughout the program, and different threads have their own copy of the data). Note that this scheme excludes running multiple decompressions in parallel, unless you do it in different threads. +| `DEFLATE_ALLOCATION_DYNAMIC` | Storage duration is the same as with automatic allocation, but the `new` keyword is explicitly used (which usually means heap/bss allocation). + +There is also a constant `MAX_WINDOW_SIZE`, which is 32768 by default, +but you can reduce it to use less memory for the automatically allocated +window in situations where one is allocated (see note 9 below). +Note that this value must not be smaller than the maximum backreference +distance used by your compressed data. + +## Unrequirements + +* No dynamic memory is allocated under any circumstances, unless your user-supplied functors do it, or you `#define DEFLATE_ALLOCATION_DYNAMIC`. +* Aside from assert() in assert.h and some template metaprogramming tools in type_traits, no standard library functions are used. +* No global variables. +* Compatible with -fno-exceptions -fno-rtti compilation. +* Option to compile without constant arrays. + +## Rationale + +* Embedded platforms (Arduino, STM32 etc). +* ROM hacking + +## Caveats + +* Decompressor only. Deflate and GZIP streams are supported. +* Slower than your average inflate function. The template uses densely bitpacked arrays, which require plenty of bit-shifting operations for every access. +* The code obviously performs best on 32-bit or 64-bit platforms. Platforms where 32-bit entities must be synthesized from a number of 8-bit entities are at a disadvantage. +* Decompressed data integrity is not verified. Any checksum fields are totally ignored. +* On most systems, automatic storage means ‘stack allocation’. Depending on your circumstances, you may want to change the memory allocation scheme. See the Tuning chapter for details. + +## Definitions + +```C++ +struct DeflateTrackNoSize{}; +struct DeflateTrackInSize{}; +struct DeflateTrackOutSize{}; +struct DeflateTrackBothSize{}; + +int/*exit status*/ Deflate(InputParams..., OutputParams..., DeflateTrackNoSize = {}); + +std::pair + Deflate(InputParams..., OutputParams..., DeflateTrackInSize); // (11) + +std::pair + Deflate(InputParams..., OutputParams..., DeflateTrackOutSize); // (12) + +std::pair> + Deflate(InputParams..., OutputParams..., DeflateTrackBothSize); // (13) + +// A counter for sizes is only allocated if explicitly requested +// by using one of the former three tracking overloads. +``` + +`InputParams` may be one of the following sets of parameters: + +* InputFunctor input `(5)` `(14)` +* InputIterator begin `(7)` `(14)` +* InputIterator begin, InputIterator end `(6)` `(14)` +* InputIterator begin, SizeType length `(8)` `(14)` +* BidirectionalIterator begin, SizeType length `(8)` `(15)` +* ForwardIterator begin `(7)` `(14)` +* BidirectionalIterator begin `(7)` `(15)` +* RandomAccessIterator begin `(7)` `(15)` +* ForwardIterator begin, ForwardIterator end `(6)` `(15)` +* BidirectionalIterator begin, BidirectionalIterator end `(6)` `(15)` +* RandomAccessIterator begin, RandomAccessIterator end `(6)` `(15)` + +`OutputParams` may be one of the following sets of parameters: + +* OutputFunctor output `(1)` `(9)` +* OutputFunctor output, WindowFunctor window `(2)` +* OutputIterator target `(9)` +* RandomAccessIterator target `(10)` +* RandomAccessIterator target, SizeType target_limit `(3)` `(10)` +* RandomAccessIterator target, RandomAccessIterator target_end `(4)` `(10)` + + +1) If the output functor (`output`) returns a `bool`, and the returned value is `true`, the decompression aborts with return value -3 +without writing any more data. + +2) If the output functor (`output`) returns a `bool`, and the returned value is `true`, the decompression aborts with return value -3 +without writing any more data. +If the window function returns an integer type, and the returned value is other than 0, the decompression aborts with return value -4 +without writing any more data. +If either the window function returns `void`, or the output functor does not return a `bool`, aborting on output-full will not be compiled. + +3) If `target_limit` bytes have been written into `target` and the decompression is not yet complete, the decompression aborts with return value -3 +without writing any more data. + +4) If `target_begin == target_end`, the decompression aborts with return value -3 +without writing any more data. + +5) If the input functor (`input`) returns an integer type other than a `char`, `signed char`, or `unsigned char`, +and the returned value is smaller than 0 or larger than 255, the decompression aborts with return value -2 +without reading any more data. + +6) If `begin == end`, the decompression aborts with return value -2. + +7) If the input iterator deferences into a value outside the 0 — 255 range, the decompression aborts with return value -2 +without reading any more data. + +8) If `length` bytes have been read from `begin` and the decompression is not yet complete, the decompression aborts with return value -2 +without reading any more data. + +9) A separate 32768-byte sliding window will be automatically and separately allocated for the decompression. + +10) The output data buffer is assumed to persist during the call and doubles as the sliding window for the decompression. + +11) The `first` field in the return value has the same meaning as the `int` type return value described earlier. +The `second` field in the return value contains the number of bytes that were consumed from the input. + +12) The `first` field in the return value has the same meaning as the `int` type return value described earlier. +The `second` field in the return value contains the number of bytes that were written to the output. + +13) The `first` field in the return value has the same meaning as the `int` type return value described earlier. +The `second.first` field in the return value contains the number of bytes that were consumed from the input. +The `second.second` field in the return value contains the number of bytes that were written to the output. + +14) This method is non-backtrackable, and uses a bit more memory than the backtrackable ones. + +15) This method is backtrackable, meaning that some bytes in the input may be read twice. It uses less memory than the non-backtrackable calls. + +### Tips + +Some of these definitions may be ambiguous. +If you hit a compiler error, choose a different call method. +To help distinguish between (`InputIterator`,`RandomAccessIterator`,`RandomAccessIterator`) +and (`ForwardIterator`,`ForwardIterator`,`OutputIterator`), make sure the input iterators +are _const_. + +If you do multiple decompression calls in your program in different spots, +it may be wise to make sure they all use the same type of parameters, +to avoid having to instantiate multiple copies of `Deflate()`. +Lambda functors are an offender in this respect, because each lambda has a +unique type even if their contents and calling conventions are identical. +In the worst case, you can use `std::function` to wrap your calls +into a common interface. Check out this video for more about this topic: https://www.youtube.com/watch?v=rUB5Hlm9AaQ + +## Requirements + +```C++ +// An InputFunctor has the following prototype, +// wherein type1 is convertible into unsigned char: +type1 input() + +// An OutputFunctor has one of the following two prototypes, +// wherein type1 can accept unsigned int parameters in range 0-255: +void output(type1 byte_to_output) +bool output(type1 byte_to_output) + +// A WindowFunctor has one of the following two prototypes, +// wherein type1 can accept unsigned int parameters in range 0-258, +// and type2 can accept unsigned int parameters: +void outputcopy(type1 length, type2 offs) +type2 outputcopy(type1 length, type2 offs) + +// An InputIterator must have at least the following operations defined, +// where type1 is convertible into unsigned char: +const type1& operator*() const +InputIterator& operator++() + +// A OutputIterator must have at least the following operations defined, +// where type1 is convertible into unsigned char: +type1& operator*() const +OutputIterator& operator++() + +// A ForwardIterator must have at least the following operations defined, +// where type1 is convertible into unsigned char: +const type1& operator*() const +ForwardIterator& operator++() +bool operator==(const ForwardIterator&) const + +// A RandomAccessIterator must have at least the following operations defined, +// where type1 is convertible into unsigned char, +// and type2 is a signed integer type (may be negative): +type1& operator*() +type1& operator[] (type2) +RandomAccessIterator operator- (type2) +RandomAccessIterator& operator++() +bool operator==(const RandomAccessIterator&) const +``` + +## Example use: + +Decompress the standard input into the standard output (uses 32 kB automatically allocated window): + +```C++ + Deflate([]() { return std::getchar(); }, + [](unsigned char byte) { std::putchar(byte); }); + + // Or more simply: + + Deflate(std::getchar, std::putchar); +``` + +Decompress an array containing gzipped data into another array that must be large enough to hold the result. +A window buffer will not be allocated. + +```C++ + extern const char compressed_data[]; + extern unsigned char outbuffer[131072]; + + Deflate(compressed_data+0, outbuffer+0); +``` + +Same as above, but with range checking for output, and reporting of written size: + +```C++ + extern const char compressed_data[]; + extern unsigned char outbuffer[131072]; + + auto result = Deflate(compressed_data+0, outbuffer+0, sizeof(outbuffer), DeflateTrackOutSize{}); + if(result.first != 0) std::fprintf(stderr, "Error %d\n", result.first); + std::fprintf(stderr, "%u bytes written\n", unsigned(result.second)); +``` + +Same as above, but with range checking for both input and output: + +```C++ + extern const char compressed_data[]; + extern unsigned compressed_data_length; + extern unsigned char outbuffer[131072]; + + int result = Deflate(compressed_data+0, compressed_data_length, outbuffer, outbuffer + sizeof(outbuffer)); + if(result != 0) std::fprintf(stderr, "Error\n"); +``` + +Decompress using a custom window function (the separate 32 kB window buffer will not be allocated): + +```C++ + std::vector result; + + Deflate(std::getchar, + [&](unsigned byte) + { + result.push_back(byte); + }, + [&](unsigned length, unsigned offset) + { + if(!length) + { + // offset contains the maximum look-behind distance. + // You could use this information to allocate a buffer of a particular size. + // length=0 case is invoked exactly once before any length!=0 cases are. + } + while(length-- > 0) + { + result.push_back( result[result.size()-offset] ); + } + }); +``` + +Same as above, but stop decompressing once 4096 bytes have been written: + +```C++ + std::vector result; + + Deflate(std::getchar, + [&](unsigned byte) + { + if(result.size() >= 4096) return true; + result.push_back(byte); + return false; + }, + [&](unsigned length, unsigned offset) + { + if(!length) + { + // offset contains the maximum look-behind distance. + // You could use this information to allocate a buffer of a particular size. + // length=0 case is invoked exactly once before any length!=0 cases are. + } + for(; result.size() < 4096 && length > 0; --length) + { + result.push_back( result[result.size()-offset] ); + } + return length; + }); +``` + +## Misnomer + +Yes, I am aware that the project is technically named misleadingly. +This project implements the _inflate_ algorithm (decompression), +not _deflate_ (compression). + +In my defense, the _compression format_ is called deflate. There is no _inflate_ format. +This library decompresses data that has been compressed with _deflate_. + +Think name, not verb. + diff --git a/TinyDeflate/gunzip.hh b/TinyDeflate/gunzip.hh new file mode 100644 --- /dev/null +++ b/TinyDeflate/gunzip.hh @@ -0,0 +1,1434 @@ +/* My tiny gzip decompressor without using zlib. - Joel Yliluoma + * http://iki.fi/bisqwit/ , http://youtube.com/user/Bisqwit + * Inspired and influenced by a 13th IOCCC winner program by Ron McFarland */ +/* Further optimized based on ideas from tinf library by Joergen Ibsen */ +/** @file gunzip.hh @brief TinyDeflate */ + +/* Fun fact: Contains zero new/delete, and no STL data structures */ +/* Distributed under the terms of the Zlib license: + + Copyright (C) 2018 Joel Yliluoma + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include +#include // std::forward +#include // integer sizes +#include +#include + +#if !1 //Documentation purposes only; the actual prototypes are littered with std::enable_ifs. +/// Deflate(): This is the public method declared (later) in this file. +/// Decompresses (inflates) deflate-compressed data, with a gzip or deflate header. +/// User-supplied functors: +/// @param input() returns the next byte from the (compressed) input. +/// @param output(byte) outputs one uncompressed byte. +/// @param outputcopy(length, offset) copies length uncompressed bytes from offset, +/// Offset is always >= 1. +/// offset 1 means previous byte, +/// offset 2 means previous before that and so on. +/// Note that (offset < length) is not an error and in fact happens frequently. +/// If length=0, offset indicates the largest look-behind window length that +/// you need to be prepared for. The length is a power-of-two in range 256..32768. +// +/// If you want to implement range checking in input, return a negative value +/// from input() when there is no more input. +// +/// If you want to implement range checking in output, add a return value +/// in output(): false=ok, true=abort; and a return value in outputcopy(): +/// 0=ok, nonzero=one or more bytes were not writable. +// +/// @returns: +/// 0 = decompression complete +/// -1 = data error +/// -2 = input functor returned a value outside 0x00..0xFF range +/// -3 = output functor returned nonzero / bool true value +/// -4 = outputcopy functor returned nonzero remaining length value +// +template +int Deflate(InputFunctor&& input, OutputFunctor&& output, WindowFunctor&& outputcopy); + +/// Check README.md for the full list of versions of Deflate() available. + +#endif + +struct DeflateTrackTagBase{}; +struct DeflateTrackNoSize: public DeflateTrackTagBase{}; +struct DeflateTrackInSize: public DeflateTrackTagBase{}; +struct DeflateTrackOutSize: public DeflateTrackTagBase{}; +struct DeflateTrackBothSize: public DeflateTrackTagBase{}; + + +/// The rest of the file is just for the curious about implementation. +#ifndef DOXYGEN_SHOULD_SKIP_THIS +namespace gunzip_ns +{ + //#define DO_DEFDB_DUMPING + + // If you want more performance at the expense of RAM use, + // Turn one or more of these settings to false: + static constexpr bool USE_BITARRAY_TEMPORARY_IN_HUFFMAN_CREATION = false; /* 8 bytes save */ + static constexpr bool USE_BITARRAY_FOR_LENGTHS = false; /* 160 bytes save */ + static constexpr bool USE_BITARRAY_FOR_HUFFNODES = false; /* 392 bytes save */ + + static constexpr unsigned MAX_WINDOW_SIZE = 32768u; + + static_assert(MAX_WINDOW_SIZE >= 1, "Max window size should be >= 1"); + static_assert(MAX_WINDOW_SIZE <= 32768u, "Window sizes larger than 32768 are not supported by deflate standard. Edit the source code to remove this assert if you need it."); + + // + #define DEFLATE_USE_DATA_TABLES + + #if !defined(DEFLATE_ALLOCATION_AUTOMATIC) && !defined(DEFLATE_ALLOCATION_STATIC) && !defined(DEFLATE_ALLOCATION_DYNAMIC) + // Choose one: + #define DEFLATE_ALLOCATION_AUTOMATIC + //#define DEFLATE_ALLOCATION_STATIC + //#define DEFLATE_ALLOCATION_DYNAMIC + #endif + + constexpr unsigned Flag_InputAbortable = 0x01; + constexpr unsigned Flag_OutputAbortable = 0x02; + constexpr unsigned Flag_TrackIn = 0x40; + constexpr unsigned Flag_TrackOut = 0x80; + constexpr unsigned Flag_NoTrackFlagMask = 0x03; +} + +#ifdef DEFLATE_ALLOCATION_DYNAMIC +# include +#endif + +// RandomAccessBitArray: An engine for arrays of data items that are not byte-aligned +template +struct RandomAccessBitArrayBase +{ +private: + static constexpr unsigned Ubytes = sizeof(U), Ubits = Ubytes*8; + + static std::uint_fast64_t Get_Unclean(unsigned Size, const U* data, unsigned index) throw() + { + unsigned bitpos = index*Size, unitpos = bitpos / Ubits, shift = bitpos % Ubits; + std::uint_fast64_t result = data[unitpos] >> shift; + //assert(Size <= sizeof(result)*8); + unsigned acquired = Ubits - shift; + for(; acquired < Size; acquired += Ubits) + { + result += (std::uint_fast64_t)data[++unitpos] << acquired; + } + return result; + } +public: + template + static std::uint_fast64_t Get(const U* data, unsigned index) throw() + { + std::uint_fast64_t result = Get_Unclean(Size,data,index); + return (Size >= sizeof(result)*8) ? result : (result & ((std::uint64_t(1) << Size)-1)); + } + + template + static void Set(U* data, unsigned index, std::uint_fast64_t value) throw() + { + unsigned bitpos = index*Size, unitpos = bitpos / Ubits, eat = 0; + // Make sure our storage unit is at least as bit as value + //assert(Ubits >= sizeof(value)*8); + //assert(Size >= sizeof(value)*8 || value < (std::uint64_t(1) << Size)); + + if(Size % Ubits != 0) + { + unsigned shift = bitpos % Ubits; + eat = Ubits - shift; if(eat > Size) eat = Size; + + //assert(eat < sizeof(std::uint_fast64_t)*8); + //assert(shift + eat <= Ubits); + std::uint_fast64_t vmask = (std::uint64_t(1) << eat)-1; + if(update) + data[unitpos] = (data[unitpos] & ~(vmask << shift)) | (value << shift); + else + data[unitpos] |= value << shift; + //assert(eat < sizeof(value)*8); + value >>= eat; + ++unitpos; + } + if(eat < Size) + for(unsigned remain = Size-eat; ; ++unitpos) + { + eat = Ubits; + if(eat > remain) + { + eat = remain; + if(update) + { + std::uint_fast64_t vmask = ((std::uint64_t(1) << eat)-1); + data[unitpos] = (data[unitpos] & ~vmask) | value; + } + else + { + data[unitpos] |= value; + } + break; + } + else + { + data[unitpos] = value; + value >>= Ubits/2; value >>= Ubits/2; + remain -= Ubits; + if(!remain) break; + } + } + } +}; + +template +struct RandomAccessBitArray +{ + static constexpr unsigned Ubytes = sizeof(U), Ubits = Ubytes*8, Nunits = (Nbits+Ubits-1)/Ubits; + U data[Nunits]; + + template + inline std::uint_fast64_t Get(unsigned index) const throw() + { + return RandomAccessBitArrayBase::template Get(data, index); + } + + template + inline void Set(unsigned index, std::uint_fast64_t value) throw() + { + RandomAccessBitArrayBase::template Set(data, index, value); + } +}; + +namespace gunzip_ns +{ + struct dummy{}; + + /// Utility: ceil(log2(n)) + template struct CeilLog2_s{ static constexpr unsigned result = 1+CeilLog2_s<(N+1)/2>::result; }; + template<> struct CeilLog2_s<0> { static constexpr unsigned result = 0; }; + template<> struct CeilLog2_s<1> { static constexpr unsigned result = 0; }; + template static constexpr unsigned CeilLog2 = CeilLog2_s::result; + + /// Utility: floor(log2(n)) + template struct FloorLog2_s{ static constexpr unsigned result = 1+FloorLog2_s::result; }; + template<> struct FloorLog2_s<0> { static constexpr unsigned result = 0; }; + template<> struct FloorLog2_s<1> { static constexpr unsigned result = 0; }; + template static constexpr unsigned FloorLog2 = FloorLog2_s::result; + + /// Utility: smallest unsigned integer type that can store n-bit value + template + using SmallestType = std::conditional_t< (bits<=16), + std::conditional_t< (bits<= 8), std::uint_least8_t, std::uint_least16_t>, + std::conditional_t< (bits<=32), std::uint_least32_t, std::uint_least64_t>>; + + /// testcases + static_assert(FloorLog2<1> == 0, "FloorLog2 fail"); static_assert(CeilLog2<1> == 0, "CeilLog2 fail"); + static_assert(FloorLog2<2> == 1, "FloorLog2 fail"); static_assert(CeilLog2<2> == 1, "CeilLog2 fail"); + static_assert(FloorLog2<3> == 1, "FloorLog2 fail"); static_assert(CeilLog2<3> == 2, "CeilLog2 fail"); + static_assert(FloorLog2<4> == 2, "FloorLog2 fail"); static_assert(CeilLog2<4> == 2, "CeilLog2 fail"); + static_assert(FloorLog2<5> == 2, "FloorLog2 fail"); static_assert(CeilLog2<5> == 3, "CeilLog2 fail"); + static_assert(FloorLog2<6> == 2, "FloorLog2 fail"); static_assert(CeilLog2<6> == 3, "CeilLog2 fail"); + static_assert(FloorLog2<7> == 2, "FloorLog2 fail"); static_assert(CeilLog2<7> == 3, "CeilLog2 fail"); + static_assert(FloorLog2<8> == 3, "FloorLog2 fail"); static_assert(CeilLog2<8> == 3, "CeilLog2 fail"); + static_assert(FloorLog2<9> == 3, "FloorLog2 fail"); static_assert(CeilLog2<9> == 4, "CeilLog2 fail"); + + template + struct RandomAccessArray {}; + + template + struct RandomAccessArray + { + RandomAccessBitArray impl; + inline std::uint_fast64_t Get(unsigned index) const { return impl.template Get(index); } + inline void Set(unsigned index, std::uint_fast32_t value) { impl.template Set(index, value); } + inline void QSet(unsigned index, std::uint_fast32_t value) { impl.template Set(index, value); } + template + inline void WSet(unsigned index, std::uint_fast64_t value) { impl.template Set(index, value); } + }; + + template + struct RandomAccessArray + { + typedef SmallestType E; + E data[Dim]; + inline E Get(unsigned index) const { return data[index]; } + inline void Set(unsigned index, E value) { data[index] = value; } + inline void QSet(unsigned index, E value) { data[index] = value; } + template + inline void WSet(unsigned index, std::uint_fast64_t value) + { + index *= Bits/Elem; + for(unsigned b=0; b>=Elem) + QSet(index++, (value % (1u << Elem))); + } + }; +} + + +namespace gunzip_ns +{ + //#define DEFL_DO_HUFF_STATS + template + bool CreateHuffmanTree(const char* + #ifdef DEFL_DO_HUFF_STATS + why + #endif + , RandomAccessArray& tree, + unsigned num_values, + LengthFunctor&& ReadLength) throw() + { + /* Lengths[] needs to be scanned exactly twice, in forward order. + * Technically we could use a functor instead of a table, + * but this would require that the dynamic tree generator + * can read sections of the compressed data twice, + * which we currently do not support. + */ + + constexpr unsigned ElemBits = CeilLog2; // ceil(log2(A-15)) where A-15 is max value of num_values + static_assert((1u << B) >= (A-15), "B is too small"); + assert(num_values <= (A-15)); + + RandomAccessArray offs{}; // 24 or 16 bytes. + // Theoretically 15.32 bytes for 288 num_values, 9.375 for 32 num_values, 7.97 for 19 num_values. + + // Clear code length count table + tree.template WSet<(15*B + 63) & ~63>(0, 0); // First 15 needed, but round to nice unit + // Scan symbol length, and sum code length counts + #ifdef DEFL_DO_HUFF_STATS + unsigned largest_treetable_value = 0, largest_offs = 0, smallest_treetable_value = ~0u; + unsigned largest_treetrans_index=0, largest_treetrans_value=0; + unsigned longest_length = 0; + #endif + for(unsigned a = 0; a < num_values; ++a) + { + int length = ReadLength(a); // Note: Can be zero. + if(Abortable && length < 0) return true; + if(length) + { + unsigned v = tree.Get(0 + length-1)+1; + #ifdef DEFL_DO_HUFF_STATS + largest_treetable_value = std::max(largest_treetable_value, v); + longest_length = std::max(longest_length, unsigned(length)); + #endif + //fprintf(stderr, " [%d]%3d CLL (val: %d)\n", length, v, v); + tree.Set(0 + length-1, v); + } + else + { + //fprintf(stderr, " [_]%3d CLL (val: 0)\n", 0); + } + } + + // Compute offset table for distribution sort + for(unsigned sum=0, a = 1; a < 16; ++a) + { + offs.QSet(a-1, sum); // starting offset for values that have length "a" + sum += tree.Get(0 + a-1); // number of values that have length "a" + } + #ifdef DEFL_DO_HUFF_STATS + for(unsigned a=1; a<=longest_length; ++a) + smallest_treetable_value = std::min(smallest_treetable_value, (unsigned)tree.Get(0 + a-1)); + #endif + + // Create code->symbol translation table (symbols sorted by code) + for(unsigned value = 0; value < num_values; ++value) + { + int length = ReadLength(value); // Note: Can be zero. + if(Abortable && length < 0) return true; + if(length) + { + unsigned q = offs.Get(length-1); offs.Set(length-1, q+1); // q = offset[length]++; + #ifdef DEFL_DO_HUFF_STATS + largest_offs = std::max(q, largest_offs); + largest_treetrans_index = std::max(largest_treetrans_index, q); + largest_treetrans_value = std::max(largest_treetrans_value, value); + #endif + assert(q < num_values /*&& value < num_values*/); + //fprintf(stderr, " [x]%3d CLL %d\n", 15+q, value); + tree.Set(15 + q, value); + } + } + #ifdef DEFL_DO_HUFF_STATS + std::fprintf(stderr, "Largest \"%12s\"(treetable_value=%4u..%4u, offs=%4u, treetrans_index=%4u, treetrans_value=%4u)\n", + why, smallest_treetable_value,largest_treetable_value, + largest_offs, largest_treetrans_index, largest_treetrans_value); + #endif + + // Largest values observed in the wild: + + // Dyn Lengths: Max treetable_value =255, max offs =285, max treetrans_index =285, max treetrans_value =285 + // Stat Lengths:Max treetable_value =152, max offs =287, max treetrans_index =287, max treetrans_value =287 + + // Len Lengths: Max treetable_value = 13, max offs = 18, max treetrans_index = 18, max treetrans_value = 18 + // Dyn Dists: Max treetable_value = 19, max offs = 29, max treetrans_index = 29, max treetrans_value = 29 + // Stat Dists: Max treetable_value = 32, max offs = 31, max treetrans_index = 31, max treetrans_value = 31 + return false; + } + +#ifdef DEFLATE_USE_DATA_TABLES + template // Using a dummy template parameter makes this function and its data weak, + inline const std::uint_least8_t* GetBTable() throw() // removing linker problems in multi-module use + { + static const std::uint_least8_t data[] { + // Length bases (0-31) + 0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224,255, 0,0,0, + // Length bits and distance bits (29-60) (overlap 3 bytes) + // 0x00,0x01,0x01,0x02,0x02,0x13,0x13,0x14,0x14,0x25,0x25,0x26,0x26, + //0x37,0x37,0x38,0x38,0x49,0x49,0x4A,0x4A,0x5B,0x5B,0x5C,0x5C,0x0D,0x0D,0x00,0x00 + // Reverse-order table + 3*3,17*3,15*3,13*3,11*3,9*3,7*3,5*3,4*3,6*3,8*3,10*3,12*3,14*3,16*3,18*3,0*3,1*3,2*3 + }; + return data; + } + //template + //inline const std::uint_least16_t* GetWTable() throw() + //{ + // static const std::uint_least16_t data[32] { + // 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 0,0 }; + // return data; + //} + + //inline unsigned dbase(unsigned distcode) { return GetWTable<>()[distcode]; } + inline unsigned lbase(unsigned lencode) { return GetBTable<>()[lencode-257+0] + 3; } + //inline unsigned dbits(unsigned distcode) { return GetBTable<>()[distcode+29] & 0xF; } + //inline unsigned lbits(unsigned lencode) { return GetBTable<>()[lencode-257+29] >> 4; } + inline unsigned rshift(unsigned a) { return GetBTable<>()[a + 32]; } +#else + inline unsigned lbase(unsigned lencode) { return (lencode > 285 ? 3 : ((lencode >= 265) ? (((lencode-257)%4+4) << ((lencode-257)/4-1)) + (lencode==285 ? 2 : 3) : (lencode-254))); } + inline unsigned rshift(unsigned a) { if(!a) return 3*3; else if(a>=16) return (a-16)*3; int r = 12 + 7*(a<8) - a*2; return (r<0 ? -r : r)*3; } +#endif + inline unsigned dbase(unsigned distcode) { return (1 + (distcode>=4 ? ((2+distcode%2) << (distcode/2-1)) : distcode)); } + inline unsigned dbits(unsigned distcode) { return distcode>=4 ? distcode/2-1 : 0; } + inline unsigned lbits(unsigned lencode) { return ((lencode>=265 && lencode<285) ? ((lencode-265)/4+1) : 0); } + //inline unsigned order(unsigned index) { return index<3 ? (index+16) : ((index%2) ? (1-index/2)&7 : (6+index/2)); } + + // Cortex-M0+ Cortex-M4 x86_64 + // dbase with table 12+64 = 76 12+64 = 76 14+64 = 78 + // dbase with func 24 22 26 + // lbase with table 12+32 = 48 12+32 = 48 21+64 = 76 + // lbase with func 54 56 64 + // dbits+lbits with table 12+16+29 = 57 12+16+29 = 57 17+21+29 = 67 + // dbits+lbits with func 14+18 = 32 14+18 = 32 13+20 = 33 + + // Support for pre-c++20 + template + using remove_cvref_t = std::remove_reference_t>; + // Support for pre-c++20 (result_of is removed in c++20, invoke_result is added in c++17, so neither can be used exclusively) + template + struct result_of { // explain usage + static_assert((T)false, "result_of is invalid; use " + "result_of instead."); + }; + #if __cplusplus > 202000UL + template + struct result_of : std::invoke_result {}; + #else + template + struct result_of : std::result_of {}; + #endif + template + using result_of_t = typename result_of::type; + + #define BEGIN_COND(name) \ + template struct name : public std::false_type {}; \ + template struct name> : public std::true_type {}; \ + template \ + inline constexpr bool name ## _v = name::value; \ + + // Input parameter condition testers: + BEGIN_COND(is_input_functor) + std::is_convertible_v()>,int> + END_COND(is_input_functor) + BEGIN_COND(is_input_iterator) + std::is_convertible_v>::value_type, unsigned char> + && std::is_same_v>::iterator_category, std::input_iterator_tag> + END_COND(is_input_iterator) + BEGIN_COND(is_bidir_input) + std::is_convertible_v>::value_type, unsigned char> + && (std::is_same_v>::iterator_category, std::forward_iterator_tag> + || std::is_same_v>::iterator_category, std::bidirectional_iterator_tag> + || std::is_same_v>::iterator_category, std::random_access_iterator_tag>) + END_COND(is_bidir_input) + BEGIN_COND(is_size_type) + std::is_convertible_v, std::size_t> && !std::is_pointer_v> + END_COND(is_size_type) + // Output parameter condition testers: + BEGIN_COND(is_random_iterator) + std::is_convertible_v>::value_type, unsigned char> + && !std::is_const_v>::reference> + && std::is_same_v>::iterator_category, std::random_access_iterator_tag> + END_COND(is_random_iterator) + BEGIN_COND(is_output_iterator) + std::is_convertible_v>::value_type, unsigned char> + && !std::is_const_v>::reference> + && !std::is_pointer_v> + && (std::is_same_v>::iterator_category, std::output_iterator_tag> + || std::is_same_v>::iterator_category, std::forward_iterator_tag> + || std::is_same_v>::iterator_category, std::bidirectional_iterator_tag>) + END_COND(is_output_iterator) + // Output functor & window functor: Returns void or something that can be converted to bool + BEGIN_COND(is_output_functor) + std::is_same_v(int)>,void> || std::is_convertible_v(int)>,bool> + END_COND(is_output_functor) + BEGIN_COND(is_window_functor) + std::is_same_v(int,int)>,void> || std::is_convertible_v(int,int)>,int> + END_COND(is_window_functor) + + BEGIN_COND(is_abortable_input_type) + !(std::is_same_v || std::is_same_v || std::is_same_v) + END_COND(is_abortable_input_type) + + #undef END_COND + #undef BEGIN_COND + + template + constexpr bool DeflAbortable_InFun = is_abortable_input_type_v>>; + template + constexpr bool DeflAbortable_OutFun = std::is_same_v, bool>; + template + constexpr bool DeflAbortable_WinFun = std::is_convertible_v>, int>; + + template + struct OutputHelper + { + template + static inline bool output(OutputFunctor&& output, unsigned char byte) + { + output(byte); + return false; + } + template + static inline bool outputcopy(WindowFunctor&& outputcopy, std::uint_least16_t length, std::uint_fast32_t offset) + { + outputcopy(length, offset); + return false; + } + }; + + template<> + struct OutputHelper + { + template + static inline bool output(OutputFunctor&& output, unsigned char byte) + { + return output(byte); + } + template + static inline bool outputcopy(WindowFunctor&& outputcopy, std::uint_least16_t& length, std::uint_fast32_t offset) + { + length = outputcopy(length, offset); + return length != 0; + } + }; + + struct SizeTracker_NoOutput + { + inline void OutByte() { } + inline void OutBytes(std::uint_fast64_t) { } + + // Dummy forwarders. Do the same as std::forward. + template + static inline constexpr T&& ForwardOutput(std::remove_reference_t& fun) { return static_cast(fun); } + template + static inline constexpr T&& ForwardOutput(std::remove_reference_t&& fun) { return static_cast(fun); } + + template + static inline constexpr T&& ForwardWindow(std::remove_reference_t& fun) { return static_cast(fun); } + template + static inline constexpr T&& ForwardWindow(std::remove_reference_t&& fun) { return static_cast(fun); } + }; + struct SizeTracker_NoInput + { + inline void InByte() { } + inline void InBytes(std::uint_fast64_t) { } + + template + static inline constexpr T&& ForwardInput(std::remove_reference_t& fun) { return static_cast(fun); } + template + static inline constexpr T&& ForwardInput(std::remove_reference_t&& fun) { return static_cast(fun); } + }; + struct SizeTracker_DoInput + { + std::uint_fast64_t insize=0; + + inline void InByte() { ++insize; } + inline void InBytes(std::uint_fast64_t n) { insize += n; } + + template,gunzip_ns::dummy>...> + auto ForwardInput(const InputFunctor& input) + { + return [&]() { InByte(); return input(); }; + } + + template,gunzip_ns::dummy>...> + auto ForwardInput(const InputFunctor& input) + { + return [&]() { auto r = input(); if(!(r & ~0xFF)) { InByte(); } return r; }; + } + }; + struct SizeTracker_DoOutput + { + std::uint_fast64_t outsize=0; + + inline void OutByte() { ++outsize; } + inline void OutBytes(std::uint_fast64_t n) { outsize += n; } + + template,gunzip_ns::dummy>...> + auto ForwardOutput(const OutputFunctor& output) + { + return [&](unsigned char byte) { OutByte(); return output(byte); }; + } + + template,gunzip_ns::dummy>...> + auto ForwardOutput(const OutputFunctor& output) + { + return [&](unsigned char byte) { auto r = output(byte); if(!r) { OutByte(); } return r; }; + } + + template,gunzip_ns::dummy>...> + auto ForwardWindow(const WindowFunctor& outputcopy) + { + return [&](std::uint_least16_t length, std::uint_fast32_t offset) + { + OutBytes(length); + return outputcopy(length, offset); + }; + } + + template,gunzip_ns::dummy>...> + auto ForwardWindow(const WindowFunctor& outputcopy) + { + return [&](std::uint_least16_t length, std::uint_fast32_t offset) + { + auto remain = outputcopy(length, offset); + OutBytes(length - remain); + return remain; + }; + } + }; + + template + struct SizeTracker: public SizeTracker_NoOutput, public SizeTracker_NoInput + { + inline int operator() (int returncode) const { return returncode; } + }; + template<> + struct SizeTracker: public SizeTracker_NoInput, public SizeTracker_DoOutput + { + typedef std::pair result; + inline result operator() (int returncode) const { return result{returncode,outsize}; } + }; + template<> + struct SizeTracker: public SizeTracker_NoOutput, public SizeTracker_DoInput + { + typedef std::pair result; + inline result operator() (int returncode) const { return result{returncode,insize}; } + }; + template<> + struct SizeTracker: public SizeTracker_DoInput, public SizeTracker_DoOutput + { + typedef std::pair> result; + inline result operator() (int returncode) const { return result{returncode,std::make_pair(insize,outsize)}; } + }; + + #ifdef DO_DEFDB_DUMPING + unsigned bitcounter=0; + #endif + struct DeflateBitCache + { + std::uint_least8_t BitCache = 0, BitCount = 0; + + template + std::uint_least64_t GetBits(InputFunctor&& input, unsigned numbits) + { + #ifdef DO_DEFDB_DUMPING + bitcounter += numbits; + #endif + std::uint_fast64_t result = BitCache; + if(numbits <= BitCount) + { + BitCount -= numbits; + BitCache >>= numbits; + result &= ((1u << numbits)-1); // 0-8 + return result; + } + for(unsigned acquired = BitCount; ; acquired += 8) + { + unsigned byte = input(); + if(Abortable && (byte & ~0xFFu)) + { + // Note: Throws away bits already eaten from BitCache + return ~std::uint_least64_t(0); // error + } + unsigned eat = numbits-acquired; + byte &= 0xFF; + if(eat <= 8) + { + result |= ((std::uint_fast64_t)(byte & ((1u << eat)-1))) << acquired; + BitCount = 8-eat; + BitCache = byte >> eat; + return result; + } + result |= ((std::uint_fast64_t)(byte)) << acquired; + } + } + + template + std::uint_least32_t HuffRead(InputFunctor&& input, + RandomAccessArray& tree) + { + int sum=0, cur=0, len=0; + #ifdef DEFL_DO_HUFF_STATS + static int maxlen = 0; + #endif + // Get more bits while code value is above sum + do { + auto p = GetBits(std::forward(input), 1); + if(Abortable && !~p) + { + // Note: Throws away progress already made traversing the tree + return ~std::uint_least32_t(0); // error flag + } + cur = (unsigned(cur) << 1) | unsigned(bool(p)); + #ifdef DEFL_DO_HUFF_STATS + if(len > maxlen) + { + maxlen = len; + std::fprintf(stderr, "maxlen access: %d (%d)\n", maxlen, (int)tree.Get(0 + len)); + } + #endif + auto v = tree.Get(0 + len++); + sum += v; + cur -= v; + } while(cur >= 0); + return tree.Get(15 + sum + cur); + } + }; + + template + struct DeflateState: public DeflateBitCache + { + std::uint_least8_t lencode = 0, num = 0; // used in DynTreeFunc + + // Lengths are in 0..15 range. + RandomAccessArray> Lengths; // 144 bytes + // Length tree + // Values up to 288 in indexes 0-14. (Table) (255 is max observed in wild) + // Values up to 287 in indexes 15-302. (Trans) + RandomAccessArray> ltree; // 341->344 bytes + // Distance tree + // Values up to 32 in indexes 0-14. (Table) + // Values up to 31 in indexes 15-46. (Trans) + RandomAccessArray> dtree; // 36->40 bytes + + RandomAccessArray>& lltree = dtree; + + // Theoretical minimum memory use: + // (15*log2(289) + 288*log2(288))/8 = 309.45 bytes for ltree + // (15*log2(33) + 32 *log2(32))/8 = 29.46 bytes for dtree + // 144.00 bytes for lengths + // total 482.91 bytes + + template + auto DynTreeFunc(InputFunctor&& input, std::uint_fast16_t length, BacktrackFunctor&& /*backtrack*/, + bool + #ifdef DO_DEFDB_DUMPING + dists + #endif + ) + { + Lengths = {}; // clear at least length nibbles; easiest to clear it all + bool error = false; + for(std::uint_fast16_t code = 0; ; ) + { + #ifdef DO_DEFDB_DUMPING + unsigned bits_before=bitcounter; + #endif + if(!num) + { + auto p = HuffRead(std::forward(input), lltree); + if(Abortable && !~p) { error = true; goto done; } + std::uint_least8_t what = p; // 0-18 + + if(!(what & 16)) { lencode = what * 0x11u; what = 0x01; } // 1 times (what < 16) (use what, set prev) + else if(what < 17) { lencode = (lencode >> 4) * 0x11u; what = 0x23; } // 3..6 (use prev) + else if(what == 17) { lencode = 0; what = 0x33; } // 3..10 (use 0, set prev) + else { lencode = 0; what = 0x7B; } // 11..138 (use 0, set prev) + + p = GetBits(std::forward(input), what >> 4); // 0, 2, 3 or 7 bits + #ifdef DO_DEFDB_DUMPING + if(what>>4) + { + char tmp[64]="[_]"; sprintf(tmp, "[%d]", int(bitcounter-bits_before)); + fprintf(stderr, "%4s %cREP (%d times)\n", tmp, (lencode&0xF)?'L':'Z', p+(what&0xF)); + } + #endif + + if(Abortable && !~p) { error = true; goto done; } + num = p + (what & 0xF); // 1..138 + } + + #ifdef DO_DEFDB_DUMPING + bool rep=num>1; + #endif + do { + #ifdef DO_DEFDB_DUMPING + char tmp[64]="[_]"; if(!rep) sprintf(tmp, "[%d]", int(bitcounter-bits_before)); + if(code == 0x100) + fprintf(stderr, "%4s EofB CL (val:%2d)\n", tmp, int(lencode&0xF)); + else if(dists) + fprintf(stderr, "%4s d_%02d CL (val:%2d)\n", tmp, int(code), int(lencode&0xF)); + else if(code > 0x100) + fprintf(stderr, "%4s l_%02d CL (val:%2d)\n", tmp, int(code- 0x101), int(lencode&0xF)); + else + fprintf(stderr, "%4s 0x%02X CL (val:%2d)\n", tmp, (int)code, int(lencode&0xF)); + #endif + + --num; + Lengths.QSet(code++, lencode & 0xF); + if(code == length) { goto done; } + } while(num > 0); + } + done:; + return [this,error](unsigned index) -> std::conditional_t + { + if(Abortable && error) return -1; + return Lengths.Get(index); + }; + } + }; + + template<> + struct DeflateState: public DeflateBitCache + { + // Length tree + // Values up to 288 in indexes 0-14. (Table) (255 is max observed in wild) + // Values up to 287 in indexes 15-302. (Trans) + RandomAccessArray> ltree; // 341->344 bytes + + // Distance tree + // Values up to 32 in indexes 0-14. (Table) + // Values up to 31 in indexes 15-46. (Trans) + RandomAccessArray> dtree; // 36->40 bytes + + // Length-lengths tree + // Values up to 19 in indexes 0-14. (Table) (13 is max observed in wild) + // Values up to 18 in indexes 15-33. (Trans) + RandomAccessArray> lltree; // 22->24 bytes + + // Theoretical minimum memory use: + // (15*log2(289) + 288*log2(288))/8 = 309.45 bytes for ltree + // (15*log2(33) + 32 *log2(32))/8 = 29.46 bytes for dtree + // (15*log2(20) + 19 *log2(19))/8 = 18.19 bytes for lltree + // total 357.10 bytes + + std::uint_least8_t lencode, num; // used in DynTreeFunc + std::uint_least8_t checkpoint_lencode, checkpoint_num; + std::uint_least8_t checkpoint_BitCache, checkpoint_BitCount; + + template + auto DynTreeFunc(InputFunctor&& input, std::uint_fast16_t /*length*/, BacktrackFunctor&& backtrack, + bool + #ifdef DO_DEFDB_DUMPING + dists + #endif + ) + { + // Create checkpoint + checkpoint_lencode = 0; + checkpoint_num = 0; + checkpoint_BitCache = BitCache; + checkpoint_BitCount = BitCount; + backtrack(false); + + return [this,&input,&backtrack](unsigned index) -> std::conditional_t + { + if(index == 0) + { + // Restore checkpoint + lencode = checkpoint_lencode; + num = checkpoint_num; + BitCache = checkpoint_BitCache; + BitCount = checkpoint_BitCount; + backtrack(true); + } + + if(Abortable && (num==0xFF)) return -1; + + if(!num) + { + auto p = HuffRead(std::forward(input), lltree); + if(Abortable && !~p) { num = 0xFF; return -1; } // If p== ~uint64_t() + std::uint_least8_t what = p; // 0-18 + + if(!(what & 16)) { lencode = what * 0x11u; what = 0x01; } // 1 times (what < 16) (use what, set prev) + else if(what < 17) { lencode = (lencode >> 4) * 0x11u; what = 0x23; } // 3..6 (use prev) + else if(what == 17) { lencode = 0; what = 0x33; } // 3..10 (use 0, set prev) + else { lencode = 0; what = 0x7B; } // 11..138 (use 0, set prev) + + p = GetBits(std::forward(input), what >> 4); // 0, 2, 3 or 7 bits + + if(Abortable && !~p) { num = 0xFF; return -1; } // If p== ~uint64_t() + num = p + (what & 0xF); // 1..138 + } + --num; + return (lencode & 0xF); + }; + } + }; + + struct DeflateWindow + { + unsigned char Data[gunzip_ns::MAX_WINDOW_SIZE]; + SmallestType> Head = 0; + }; + + #ifdef DEFLATE_ALLOCATION_STATIC + template + ObjectType& GetStaticObj() + { + static thread_local ObjectType obj; + obj.~ObjectType(); + new(&obj) ObjectType(); + return obj; + } + #endif + + /* Values of Abortable: + * Input abortable = &1 + * Output abortable = &2 + * Resumable = &4 + * + * Input abortable Output abortable Resumable Value + * no no no 0 + * yes no no 1 + * no yes no 2 + * yes yes no 3 + * 4 = invalid + * yes no yes 5 + * no yes yes 6 + * yes yes yes 7 + */ + template + int Gunzip(InputFunctor&& input, + OutputFunctor&& output, + WindowFunctor&& outputcopy, + BacktrackFunctor&& backtrack) + { + using namespace gunzip_ns; + + typedef DeflateState,dummy>> StateType; +#ifdef DEFLATE_ALLOCATION_AUTOMATIC + StateType state; +#elif defined(DEFLATE_ALLOCATION_STATIC) + auto& state = gunzip_ns::GetStaticObj(); +#elif defined(DEFLATE_ALLOCATION_DYNAMIC) + std::unique_ptr stateptr(new StateType); + auto& state = *stateptr; +#endif + + // The following routines are macros rather than e.g. lambda functions, + // in order to make them inlined in the function structure, and breakable/resumable. + #define CONCAT(a, b) a##b + + // Bit-by-bit input routine + #define DummyGetBits_(line,numbits) do { \ + auto CONCAT(pd,line) = state.template GetBits(std::forward(input), numbits); \ + if((Abortable & Flag_InputAbortable) && !~CONCAT(pd,line)) return -2; \ + } while(0) + #define DummyGetBits(numbits) DummyGetBits_(__LINE__, numbits) + + #define GetBits_(line,numbits, target) \ + auto CONCAT(pb,line) = state.template GetBits(std::forward(input), numbits); \ + if((Abortable & Flag_InputAbortable) && !~CONCAT(pb,line)) return -2; \ + target = CONCAT(pb,line) + #define GetBits(numbits, target) GetBits_(__LINE__, numbits, target) + + // Huffman tree read routine. + #define HuffRead_(line, tree, target) \ + auto CONCAT(ph,line) = state.template HuffRead(std::forward(input), tree); \ + if((Abortable & Flag_InputAbortable) && !~CONCAT(ph,line)) return -2; \ + target = CONCAT(ph,line) + #define HuffRead(tree, target) HuffRead_(__LINE__, tree, target) + + #define Fail_If(condition) do { \ + /*assert(!(condition));*/ \ + if(condition) return -1; \ + } while(0) + + // Read stream header + GetBits(16, std::uint_least16_t header); + // ^ Read deflate header: method[4] ; winsize[4] ; checksum[8] + if(header == 0x8B1F) // Is it actually a gzip header? + { + // Get format identifier, flags, MTIME, XFL and OS + GetBits(64, header); Fail_If((header & 0xFF) != 8); // Format identifier should be 8 + if(header&0x0400) // Skip extra fields as indicated by FEXTRA + { GetBits(16, std::uint_fast16_t q); DummyGetBits(8*q); } + if(header&0x0800) for(;;) { GetBits(8, bool q); if(!q) break; } // NAME: Skip filename if FNAME was present + if(header&0x1000) for(;;) { GetBits(8, bool q); if(!q) break; } // COMMENT: Skip comment if FCOMMENT was present + if(header&0x0200) { DummyGetBits(16); } // HCRC: Skip FCRC if was present + outputcopy(0, 32768u); // GZIP always uses 32k window + } + else // No. Deflate header? + { + Fail_If((header & 0x208F) != 0x0008 || ((((header<<8)+(header>>8))&0xFFFF)%31) != 0); + outputcopy(0, 256 << ((header >> 4) & 0xF)); // Preset dictionary (0x2000) is not supported + } + + // Read compressed blocks + for(;;) + { + GetBits(3, header); + //fprintf(stderr, "header=%d\n", header); + if(header & 4) // Dynamic block + { + Fail_If(header & 2); + std::uint_least16_t nlen_ndist_ncode; + GetBits(14, nlen_ndist_ncode); + + #define nlen (((nlen_ndist_ncode >> 0u) & 0x1Fu) + 257u) // 257..288 + #define ndist (((nlen_ndist_ncode >> 5u) & 0x1Fu) + 1u) // 1..32 + + + std::uint_least8_t ncode = ((nlen_ndist_ncode >> 10u) + 4u); // 4..19 + {std::uint_fast64_t lenlens; GetBits(ncode*3, lenlens); // Max: 19*3 = 57 bits + #ifdef DO_DEFDB_DUMPING + fprintf(stderr, " [5] HLIT%5d (val:%d)\n [5] HDIST%4d (val:%d)\n [4] HCLEN%4d (val:%d)\n", + nlen,nlen-257, ndist,ndist-1, ncode,ncode-4); + for(unsigned a=0; a<19; ++a) + for(unsigned b=0; b<19; ++b) + if(rshift(b) == 3*a) + { + if(a < ncode) + fprintf(stderr, " [3]%3d CLL (val: %d)\n", b, int((lenlens >> rshift(b)) & 7)); + else + fprintf(stderr, " [_]%3d CLL (val: %d)\n", b, int((lenlens >> rshift(b)) & 7)); + } + #endif + + auto lltree_fun = [=](unsigned a) -> unsigned char { return (lenlens >> rshift(a)) & 7; }; + while(CreateHuffmanTree("Len Lengths", state.lltree, 19, lltree_fun)) { return -2; }} + + {auto ltree_fun = state.template DynTreeFunc(std::forward(input), nlen, std::forward(backtrack), false); + while(CreateHuffmanTree("Dyn Lengths", state.ltree, nlen, ltree_fun)) { return -2; }} + + {auto dtree_fun = state.template DynTreeFunc(std::forward(input), ndist, std::forward(backtrack), true); + while(CreateHuffmanTree("Dyn Dists", state.dtree, ndist, dtree_fun)) { return -2; }} + + #undef nlen + #undef ndist + } + else // Fixed block + { + if(header < 2) // Copy stored block data + { + DummyGetBits(state.BitCount%8); // Go to byte boundary (discard a few bits) + GetBits(32, std::uint_least32_t a); + Fail_If(((a ^ (a >> 16)) & 0xFFFF) != 0xFFFF); + #ifdef DO_DEFDB_DUMPING + fprintf(stderr, "raw block of %d bytes (0x%X)\n", (unsigned short)a, a); + #endif + // Note: It is valid for (lower 16 bits of) "a" to be 0 here. + // It is sometimes used for aligning the stream to byte boundary. + while(a-- & 0xFFFF) + { + GetBits(8, unsigned char octet); + while(OutputHelper::output(output, octet)) { return -3; } + } + goto skipdef; + } + + unsigned char (*ltree_fun)(unsigned) = [](unsigned n)->unsigned char{return (n<0x90 || n>=0x118) ? 8u : (n<0x100 ? 9u : 7u); }; + unsigned char (*dtree_fun)(unsigned) = [](unsigned )->unsigned char{return 5u;}; + while(CreateHuffmanTree("Stat Lengths", state.ltree, 288, ltree_fun)) { return -2; } + while(CreateHuffmanTree("Stat Dists", state.dtree, 32, dtree_fun)) { return -2; } + } + // Do actual deflating. + for(;;) + { + #ifdef DO_DEFDB_DUMPING + unsigned a=bitcounter; + #endif + HuffRead(state.ltree, std::uint_least16_t lencode); // 0..287 + if(!(lencode & -256)) // 0..255: literal byte + { + #ifdef DO_DEFDB_DUMPING + {char tmp[64];sprintf(tmp,"[%d]",bitcounter-a); fprintf(stderr, "%4s %02X\n", tmp, lencode);} + #endif + while(OutputHelper::output(output, lencode)) { return -3; } + } + else if(!(lencode & 0xFF)) break; // 256=end + else // 257..287: length code for backwards reference + { + GetBits(lbits(lencode), std::uint_least16_t length); length += lbase(lencode); + {HuffRead(state.dtree, std::uint_least8_t distcode); // Read distance code (0..31) + {GetBits(dbits(distcode), std::uint_least32_t offset); offset += dbase(distcode); + #ifdef DO_DEFDB_DUMPING + {char tmp[64];sprintf(tmp,"[%d]",bitcounter-a); fprintf(stderr, "%4s (%d,%d)\n", tmp,length,offset);} + #endif + while(OutputHelper::outputcopy(outputcopy,length,offset)) { return -4; }}} + } + } + skipdef:if(header & 1) break; // last block flag + } + // Note: after this, may come a checksum, and a trailer. Ignoring them. + #undef GetBits + #undef DummyGetBits + #undef Fail_If + #undef HuffRead + return 0; + } +}//ns + + +/* +`InputParams` may be one of the following sets of parameters: + +* InputFunctor input `(5)` `(14)` +* InputIterator begin `(7)` `(14)` +* InputIterator begin, InputIterator end `(6)` `(14)` +* InputIterator begin, SizeType length `(8)` `(14)` +* BidirectionalIterator begin, SizeType length `(8)` `(15)` +* ForwardIterator begin `(7)` `(14)` +* BidirectionalIterator begin `(7)` `(15)` +* RandomAccessIterator begin `(7)` `(15)` +* ForwardIterator begin, ForwardIterator end `(6)` `(15)` +* BidirectionalIterator begin, BidirectionalIterator end `(6)` `(15)` +* RandomAccessIterator begin, RandomAccessIterator end `(6)` `(15)` + +`OutputParams` may be one of the following sets of parameters: + +* OutputFunctor output `(1)` `(9)` +* OutputFunctor output, WindowFunctor window `(2)` +* OutputIterator target `(9)` +* RandomAccessIterator target `(10)` +* RandomAccessIterator target, SizeType target_limit `(3)` `(10)` +* RandomAccessIterator target, RandomAccessIterator target_end `(4)` `(10)` +*/ + +namespace gunzip_ns +{ + #ifdef DEFLATE_ALLOCATION_AUTOMATIC + #define DeflDeclWindow gunzip_ns::DeflateWindow window; + #elif defined(DEFLATE_ALLOCATION_STATIC) + #define DeflDeclWindow auto& window = gunzip_ns::GetStaticObj(); + #elif defined(DEFLATE_ALLOCATION_DYNAMIC) + #define DeflDeclWindow std::unique_ptr winptr(new gunzip_ns::DeflateWindow); \ + auto& window = *winptr; + #endif + + template + auto DeflateDispatchFinal(I&& i, O&& o, C&& c, B&& b) + { + if constexpr(code & (Flag_TrackIn | Flag_TrackOut)) + { + //fprintf(stderr, "both track flag\n"); + SizeTracker tracker; + return tracker(Gunzip + (tracker.template ForwardInput(i), tracker.template ForwardOutput(o), tracker.template ForwardWindow(c), std::forward(b))); + } + else if constexpr(code & Flag_TrackIn) + { + //fprintf(stderr, "in track flag\n"); + SizeTracker tracker; + return tracker(Gunzip + (tracker.template ForwardInput(i),std::forward(o),std::forward(c),std::forward(b))); + } + else if constexpr(code & Flag_TrackOut) + { + //fprintf(stderr, "out track flag\n"); + SizeTracker tracker; + return tracker(Gunzip + (std::forward(i), tracker.template ForwardOutput(o), tracker.template ForwardWindow(c), std::forward(b))); + } + else + { + //fprintf(stderr, "no track flag\n"); + return Gunzip(std::forward(i),std::forward(o),std::forward(c),std::forward(b)); + } + } + + // One-parameter output dispatch: + template + auto DeflateOutputDispatch(BtFun&& bt, InFun&& infun, T1&& param1) + { + // Is param1 a random access iterator? + if constexpr(is_random_iterator_v) + { + //fprintf(stderr, "random iterator\n"); + auto output = [&](unsigned char l) { *param1 = l; ++param1; }; + auto outputcopy = [&](std::uint_least16_t length, std::uint_fast32_t offs) + { + /* length=0 means that offs is the size of the window. */ + for(; length--; ++param1) { *param1 = *(param1-offs); } + }; + return DeflateDispatchFinal(std::forward(infun), output, outputcopy, std::forward(bt)); + } + // Is param1 an output iterator? + else if constexpr(is_output_iterator_v) + { + //fprintf(stderr, "output iterator\n"); + DeflDeclWindow + auto output = [&](unsigned char l) + { + window.Data[window.Head++ % MAX_WINDOW_SIZE] = l; + *param1 = l; ++param1; + }; + auto outputcopy = [&](std::uint_least16_t length, std::uint_fast32_t offs) + { + /* length=0 means that offs is the size of the window. */ + for(; length>0; --length) + { + unsigned char byte = window.Data[(window.Head - offs) % MAX_WINDOW_SIZE]; + output(byte); + } + return false; + }; + return DeflateDispatchFinal(std::forward(infun), output, outputcopy, std::forward(bt)); + } + // param1 must be an output functor, then. + else if constexpr(is_output_functor_v) + { + //fprintf(stderr, "output functor\n"); + DeflDeclWindow + auto output = [&](unsigned char l) + { + window.Data[window.Head++ % MAX_WINDOW_SIZE] = l; + return param1(l); + }; + auto outputcopy = [&](std::uint_least16_t length, std::uint_fast32_t offs) + { + /* length=0 means that offs is the size of the window. */ + for(; length>0; --length) + { + unsigned char byte = window.Data[(window.Head - offs) % MAX_WINDOW_SIZE]; + if(OutputHelper>::output(output, byte)) + break; + } + return length; + }; + return DeflateDispatchFinal + ? Flag_OutputAbortable : 0)> + (std::forward(infun), output, outputcopy, std::forward(bt)); + } + else + { + //fprintf(stderr, "unreached code 1\n"); + static_assert(code==0xFF, "Deflate: Unknown output parameter type"); + } + } + + // Two-parameter output dispatch: + template + auto DeflateOutputDispatch(BtFun&& bt, InFun&& infun, T1&& param1, T2&& param2) + { + if constexpr(std::is_same_v, DeflateTrackNoSize>) + { + //fprintf(stderr, "no track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(param1)); + } + else if constexpr(std::is_same_v, DeflateTrackInSize>) + { + //fprintf(stderr, "in track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(param1)); + } + else if constexpr(std::is_same_v, DeflateTrackOutSize>) + { + //fprintf(stderr, "out track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(param1)); + } + else if constexpr(std::is_same_v, DeflateTrackBothSize>) + { + //fprintf(stderr, "both track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(param1)); + } + // Are param1 and param2 both random access iterators? + else if constexpr(std::is_same_v && is_random_iterator_v) + { + //fprintf(stderr, "random iterator + random iterator\n"); + auto output = [&](unsigned char l) + { + if(param1 == param2) return true; + *param1 = l; ++param1; + return false; + }; + auto outputcopy = [&](std::uint_least16_t length, std::uint_fast32_t offs) + { + /* length=0 means that offs is the size of the window. */ + for(; length > 0 && !(param1 == param2); --length, ++param1) + { + *param1 = *(param1 - offs); + } + return length; + }; + return DeflateDispatchFinal(std::forward(infun), output, outputcopy, std::forward(bt)); + } + // Is param1 a random access iterator and param2 a size? + else if constexpr(is_size_type_v && is_random_iterator_v) + { + //fprintf(stderr, "random iterator + size\n"); + typename std::iterator_traits>::difference_type used{}, cap=param2; + auto output = [&](unsigned char l) + { + if(used >= cap) return true; + param1[used] = l; ++used; + return false; + }; + auto outputcopy = [&](std::uint_least16_t length, std::uint_fast32_t offs) + { + /* length=0 means that offs is the size of the window. */ + for(; length > 0 && used < cap; ++used, --length) + { + param1[used] = param1[used - offs]; + } + return length; + }; + return DeflateDispatchFinal(std::forward(infun), output, outputcopy, std::forward(bt)); + } + // Then, param1 must be an output functor and param2 a window functor. + else if constexpr(is_output_functor_v && is_window_functor_v) + { + //fprintf(stderr, "output functor + window functor\n"); + return DeflateDispatchFinal + && DeflAbortable_WinFun) ? Flag_OutputAbortable : 0 ) > + (std::forward(infun), std::forward(param1), std::forward(param2), std::forward(bt)); + } + else + { + //fprintf(stderr, "unreached code 2\n"); + static_assert(code==0xFF, "Deflate: Unknown output parameter type"); + } + } + + // Three-parameter output dispatch: + template + auto DeflateOutputDispatch(BtFun&& bt, InFun&& infun, T1&& p1, T2&& p2, T3) + { + if constexpr(std::is_same_v, DeflateTrackNoSize>) + { + //fprintf(stderr, "no track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(p1), std::forward(p2)); + } + else if constexpr(std::is_same_v, DeflateTrackInSize>) + { + //fprintf(stderr, "in track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(p1), std::forward(p2)); + } + else if constexpr(std::is_same_v, DeflateTrackOutSize>) + { + //fprintf(stderr, "out track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(p1), std::forward(p2)); + } + else if constexpr(std::is_same_v, DeflateTrackBothSize>) + { + //fprintf(stderr, "both track flag...\n"); + return DeflateOutputDispatch (std::forward(bt), std::forward(infun), std::forward(p1), std::forward(p2)); + } + else + { + //fprintf(stderr, "unreached code 3\n"); + static_assert(code==0xFF, "Deflate: Mismatched parameters. Expected last parameter to be a DeflateTrack option."); + } + } + + // One or two parameter input dispatch: + template + auto DeflateInputDispatch(BtFun&& bt, T1&& param1, T2&& param2, T&&... args) + { + using namespace gunzip_ns; + // Are param1 and param2 an input iterator pair? + if constexpr(std::is_same_v && is_input_iterator_v) + { + //fprintf(stderr, "input iterator + input iterator\n"); + auto inputfun = [&]() -> std::common_type_t + { if(param1 == param2) { return -1; } int r = *param1; ++param1; return r; }; + return DeflateOutputDispatch(std::forward(bt), inputfun, std::forward(args)...); + } + // Are param1 and param2 a pair of bidirectional input iterators (forward, bidir, random)? + else if constexpr(std::is_same_v && is_bidir_input_v) + { + //fprintf(stderr, "bidir input + bidir input\n"); + remove_cvref_t saved{param1}; + auto btfun = [&](bool act) { if(act) param1 = saved; else saved = std::move(param1); }; + auto inputfun = [&]() -> std::common_type_t + { if(param1 == param2) { return -1; } int r = *param1; ++param1; return r; }; + return DeflateOutputDispatch(btfun, inputfun, std::forward(args)...); + } + // Is param1 an input iterator and param2 a size? + else if constexpr(is_size_type_v && is_input_iterator_v) + { + //fprintf(stderr, "input iterator + size\n"); + typename std::iterator_traits>::difference_type remain{param2}; + auto inputfun = [&]() -> std::common_type_t + { if(!remain) return -1; --remain; int r = *param1; ++param1; return r; }; + return DeflateOutputDispatch(std::forward(bt), inputfun, std::forward(args)...); + } + // Is param1 a bidirectional input iterator (forward, bidir, random) and param2 a size? + else if constexpr(is_size_type_v && is_bidir_input_v) + { + //fprintf(stderr, "bidir input + size\n"); + typename std::iterator_traits>::difference_type remain{param2}, savestate{}; + auto btfun = [&](bool act) { if(act) { param1 -= (savestate-remain); remain = savestate; } else savestate = remain; }; + auto inputfun = [&]() -> std::common_type_t + { if(!remain) return -1; --remain; int r = *param1; ++param1; return r; }; + return DeflateOutputDispatch(btfun, inputfun, std::forward(args)...); + } + // Is param1 an input iterator? + else if constexpr(is_input_iterator_v) + { + //fprintf(stderr, "input iterator\n"); + auto inputfun = [&]() -> std::remove_cv_t { auto r = *param1; ++param1; return r; }; + return DeflateOutputDispatch + > ? Flag_InputAbortable : 0 ) > + (std::forward(bt), inputfun, std::forward(param2), std::forward(args)...); + } + // Is param1 a bidirectional input iterator (forward, bidir, random)? + else if constexpr(is_bidir_input_v) + { + //fprintf(stderr, "bidir input\n"); + remove_cvref_t saved{param1}; + auto btfun = [&](bool act) { if(act) param1 = saved; else saved = std::move(param1); }; + auto inputfun = [&]() -> std::remove_cv_t { auto r = *param1; ++param1; return r; }; + return DeflateOutputDispatch(btfun, inputfun, std::forward(param2), std::forward(args)...); + } + // param1 must be an input functor, then. Let's move on to param2 testing! + else if constexpr(is_input_functor_v) + { + //fprintf(stderr, "input functor\n"); + return DeflateOutputDispatch + ? Flag_InputAbortable : 0 ) > + (std::forward(bt), std::forward(param1), std::forward(param2), std::forward(args)...); + } + else + { + //fprintf(stderr, "unreached code 0\n"); + static_assert(code==0xFF, "Deflate: Mismatched parameters. Expected something for an input."); + } + } + #undef DeflDeclWindow +} + + +template +auto Deflate(T&&... args) +{ + return gunzip_ns::DeflateInputDispatch<0>(gunzip_ns::dummy{}, std::forward(args)...); +} + +#endif /* #ifndef DOXYGEN_SHOULD_SKIP_THIS */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ # vim:ts=4:sw=4:expandtab:autoindent: include_directories( + ${PROJECT_SOURCE_DIR}/TinyDeflate ${PROJECT_SOURCE_DIR}/filesystem ${PROJECT_SOURCE_DIR}/libmd5 ${PROJECT_SOURCE_DIR}/liblodepng diff --git a/src/dotrunner.cpp b/src/dotrunner.cpp --- a/src/dotrunner.cpp +++ b/src/dotrunner.cpp @@ -16,6 +16,18 @@ #include #include +#ifdef _MSC_VER +#pragma warning( push ) +#pragma warning( disable : 4242 ) +#pragma warning( disable : 4244 ) +#pragma warning( disable : 4996 ) +#endif +#include +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + + #include "dotrunner.h" #include "util.h" #include "portable.h" @@ -31,6 +43,9 @@ #define MAX_LATEX_GRAPH_INCH 150 #define MAX_LATEX_GRAPH_SIZE (MAX_LATEX_GRAPH_INCH * 72) +//#define DBG(x) printf x +#define DBG(x) do {} while(0) + //----------------------------------------------------------------------------------------- // since dot silently reproduces the input file when it does not @@ -108,37 +123,120 @@ static bool resetPDFSize(const int width,const int height, const QCString &base) bool DotRunner::readBoundingBox(const QCString &fileName,int *width,int *height,bool isEps) { - const char *bb = isEps ? "%%PageBoundingBox:" : "/MediaBox ["; - size_t bblen = strlen(bb); - FILE *f = Portable::fopen(fileName,"rb"); - if (!f) + std::ifstream f = Portable::openInputStream(fileName); + if (!f.is_open()) { - //printf("readBoundingBox: could not open %s\n",fileName); - return FALSE; + err("Failed to open file %s for extracting bounding box\n",qPrint(fileName)); + return false; } - const int maxLineLen=1024; - char buf[maxLineLen]; - while (fgets(buf,maxLineLen,f)!=NULL) + + // read file contents into string 'contents' + std::stringstream buffer; + buffer << f.rdbuf(); + std::string contents = buffer.str(); + + // start of bounding box marker we are looking for + const std::string boundingBox = isEps ? "%%PageBoundingBox:" : "/MediaBox ["; + + // helper routine to extract the bounding boxes width and height + auto extractBoundingBox = [&fileName,&boundingBox,&width,&height](const char *s) -> bool { - const char *p = strstr(buf,bb); - if (p) // found PageBoundingBox or /MediaBox string - { - int x,y; - double w,h; - fclose(f); - if (sscanf(p+bblen,"%d %d %lf %lf",&x,&y,&w,&h)!=4) - { - //printf("readBoundingBox sscanf fail\n"); - return FALSE; - } - *width = static_cast(std::ceil(w)); - *height = static_cast(std::ceil(h)); - return TRUE; - } + int x,y; + double w,h; + if (sscanf(s+boundingBox.length(),"%d %d %lf %lf",&x,&y,&w,&h)==4) + { + *width = static_cast(std::ceil(w)); + *height = static_cast(std::ceil(h)); + return true; + } + err("Failed to extract bounding box from generated diagram file %s\n",qPrint(fileName)); + return false; + }; + + // compressed segment start and end markers + const std::string streamStart = "stream\n"; + const std::string streamEnd = "\nendstream"; + + auto detectDeflateStreamStart = [&streamStart](const char *s) + { + size_t len = streamStart.length(); + bool streamOK = strncmp(s,streamStart.c_str(),len)==0; + if (streamOK) // ASCII marker matches, check stream header bytes as well + { + unsigned short header1 = static_cast(s[len])<<8; // CMF byte + if (header1) // not end of string + { + unsigned short header = (static_cast(s[len+1])) | header1; // FLG byte + // check for correct header (see https://www.rfc-editor.org/rfc/rfc1950) + return ((header&0x8F20)==0x0800) && (header%31)==0; + } + } + return false; + }; + + const size_t l = contents.length(); + size_t i=0; + while (i decompressBuf; + const char *source = &contents[start]; + const size_t sourceLen = i-start; + size_t sourcePos = 0; + decompressBuf.reserve(sourceLen*2); + auto getter = [source,&sourcePos,sourceLen]() -> int { + return sourcePos(source[sourcePos++]) : EOF; + }; + auto putter = [&decompressBuf](const char c) -> int { + decompressBuf.push_back(c); return c; + }; + Deflate(getter,putter); + // convert decompression buffer to string + std::string s(decompressBuf.begin(), decompressBuf.end()); + DBG(("decompressed_data=[[[\n%s\n]]]\n",s.c_str())); + // search for bounding box marker + const size_t idx = s.find(boundingBox); + if (idx!=std::string::npos) // found bounding box in uncompressed data + { + return extractBoundingBox(s.c_str()+idx); + } + // continue searching after end stream marker + i+=streamEnd.length(); + break; + } + else // compressed stream character + { + if (col>16) { col=0; DBG(("\n%08x: ",static_cast(i))); } + DBG(("%02x ",static_cast(contents[i]))); + col++; + i++; + } + } + } + else if (((isEps && contents[i]=='%') || (!isEps && contents[i]=='/')) && + strncmp(&contents[i],boundingBox.c_str(),boundingBox.length())==0) + { // uncompressed bounding box + return extractBoundingBox(&contents[i]); + } + else // uncompressed stream character + { + i++; + } } - err("Failed to extract bounding box from generated diagram file %s\n",qPrint(fileName)); - fclose(f); - return FALSE; + err("Failed to find bounding box in generated diagram file %s\n",qPrint(fileName)); + // nothing found + return false; } //--------------------------------------------------------------------------------- diff --git a/src/doxygen.cpp b/src/doxygen.cpp --- a/src/doxygen.cpp +++ b/src/doxygen.cpp @@ -11920,8 +11920,6 @@ void parseInput() { Portable::setenv("DOTFONTPATH",qPrint(curFontPath)); } - // issue 9319 - Portable::setenv("CAIRO_DEBUG_PDF","1"); }