zlib_client.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2016 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef THIRD_PARTY_IJAR_ZLIB_CLIENT_H_
  15. #define THIRD_PARTY_IJAR_ZLIB_CLIENT_H_
  16. #include <limits.h>
  17. #include "third_party/ijar/common.h"
  18. namespace devtools_ijar {
  19. // Try to compress a file entry in memory using the deflate algorithm.
  20. // It will compress buf (of size length) unless the compressed size is bigger
  21. // than the input size. The result will overwrite the content of buf and the
  22. // final size is returned.
  23. size_t TryDeflate(u1* buf, size_t length);
  24. u4 ComputeCrcChecksum(u1* buf, size_t length);
  25. struct DecompressedFile {
  26. u1* uncompressed_data;
  27. u4 uncompressed_size;
  28. u4 compressed_size;
  29. };
  30. class Decompressor {
  31. public:
  32. Decompressor();
  33. ~Decompressor();
  34. DecompressedFile* UncompressFile(const u1* buffer, size_t bytes_avail);
  35. char* GetError();
  36. private:
  37. // Administration of memory reserved for decompressed data. We use the same
  38. // buffer for each file to avoid some malloc()/free() calls and free the
  39. // memory only in the dtor. C-style memory management is used so that we
  40. // can call realloc.
  41. u1* uncompressed_data_;
  42. size_t uncompressed_data_allocated_;
  43. // last error
  44. char errmsg[4 * PATH_MAX];
  45. int error(const char* fmt, ...);
  46. // Buffer size is initially INITIAL_BUFFER_SIZE. It doubles in size every
  47. // time it is found too small, until it reaches MAX_BUFFER_SIZE. If that is
  48. // not enough, we bail out. We only decompress class files, so they should
  49. // be smaller than 64K anyway, but we give a little leeway.
  50. // MAX_BUFFER_SIZE must be bigger than the size of the biggest file in the
  51. // ZIP. It is set to 2GB here because no one has audited the code for 64-bit
  52. // cleanliness.
  53. static const size_t INITIAL_BUFFER_SIZE = 256 * 1024; // 256K
  54. static const size_t MAX_BUFFER_SIZE = std::numeric_limits<int32_t>::max();
  55. };
  56. } // namespace devtools_ijar
  57. #endif // THIRD_PARTY_IJAR_ZLIB_CLIENT_H_