zlib_client.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <limits>
  18. #include <stdexcept>
  19. #include "third_party/ijar/common.h"
  20. namespace devtools_ijar {
  21. // Try to compress a file entry in memory using the deflate algorithm.
  22. // It will compress buf (of size length) unless the compressed size is bigger
  23. // than the input size. The result will overwrite the content of buf and the
  24. // final size is returned.
  25. size_t TryDeflate(u1* buf, size_t length);
  26. u4 ComputeCrcChecksum(u1* buf, size_t length);
  27. struct DecompressedFile {
  28. u1* uncompressed_data;
  29. u4 uncompressed_size;
  30. u4 compressed_size;
  31. };
  32. class Decompressor {
  33. public:
  34. Decompressor();
  35. ~Decompressor();
  36. DecompressedFile* UncompressFile(const u1* buffer, size_t bytes_avail);
  37. char* GetError();
  38. private:
  39. // Administration of memory reserved for decompressed data. We use the same
  40. // buffer for each file to avoid some malloc()/free() calls and free the
  41. // memory only in the dtor. C-style memory management is used so that we
  42. // can call realloc.
  43. u1* uncompressed_data_;
  44. size_t uncompressed_data_allocated_;
  45. // last error
  46. char errmsg[4 * PATH_MAX];
  47. int error(const char* fmt, ...);
  48. // Buffer size is initially INITIAL_BUFFER_SIZE. It doubles in size every
  49. // time it is found too small, until it reaches MAX_BUFFER_SIZE. If that is
  50. // not enough, we bail out. We only decompress class files, so they should
  51. // be smaller than 64K anyway, but we give a little leeway.
  52. // MAX_BUFFER_SIZE must be bigger than the size of the biggest file in the
  53. // ZIP. It is set to 2GB here because no one has audited the code for 64-bit
  54. // cleanliness.
  55. static const size_t INITIAL_BUFFER_SIZE = 256 * 1024; // 256K
  56. static const size_t MAX_BUFFER_SIZE = std::numeric_limits<int32_t>::max();
  57. };
  58. } // namespace devtools_ijar
  59. #endif // THIRD_PARTY_IJAR_ZLIB_CLIENT_H_