zip.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2015 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. //
  15. // zip.h -- .zip (.jar) file reading/writing routines.
  16. //
  17. // This file specifies the interface to use the ZIP implementation of ijar.
  18. //
  19. #ifndef INCLUDED_THIRD_PARTY_IJAR_ZIP_H
  20. #define INCLUDED_THIRD_PARTY_IJAR_ZIP_H
  21. #include <sys/stat.h>
  22. #include "third_party/ijar/common.h"
  23. namespace devtools_ijar {
  24. // Tells if this is a directory entry from the mode. This method
  25. // is safer than zipattr_to_mode(attr) & S_IFDIR because the unix
  26. // mode might not be set in DOS zip files.
  27. inline bool zipattr_is_dir(u4 attr) { return (attr & 0x10) != 0; }
  28. // Convert a ZIP file attribute to a Unix file permission mask.
  29. inline mode_t zipattr_to_perm(u4 attr) {
  30. return ((mode_t)((attr >> 16) & 0777));
  31. }
  32. //
  33. // Class interface for building ZIP files
  34. //
  35. class ZipBuilder {
  36. public:
  37. virtual ~ZipBuilder() {}
  38. // Returns the text for the last error, or null on no last error.
  39. virtual const char* GetError() = 0;
  40. // Add a new file to the ZIP, the file will have path "filename"
  41. // and external attributes "attr". This function returns a pointer
  42. // to a memory buffer to write the data of the file into. This buffer
  43. // is owned by ZipBuilder and should not be free'd by the caller. The
  44. // file length is then specified when the files is finished written
  45. // using the FinishFile(size_t) function.
  46. // On failure, returns NULL and GetError() will return an non-empty message.
  47. virtual u1* NewFile(const char* filename, const u4 attr) = 0;
  48. // Finish writing a file and specify its length. After calling this method
  49. // one should not reuse the pointer given by NewFile. The file can be
  50. // compressed using the deflate algorithm by setting `compress` to true.
  51. // By default, CRC32 are not computed as java tooling doesn't care, but
  52. // computing it can be activated by setting `compute_crc` to true.
  53. // On failure, returns -1 and GetError() will return an non-empty message.
  54. virtual int FinishFile(size_t filelength,
  55. bool compress = false,
  56. bool compute_crc = false) = 0;
  57. // Write an empty file, it is equivalent to:
  58. // NewFile(filename, 0);
  59. // FinishFile(0);
  60. // On failure, returns -1 and GetError() will return an non-empty message.
  61. virtual int WriteEmptyFile(const char* filename) = 0;
  62. // Finish writing the ZIP file. This method can be called only once
  63. // (subsequent calls will do nothing) and none of
  64. // NewFile/FinishFile/WriteEmptyFile should be called after calling Finish. If
  65. // this method was not called when the object is destroyed, it will be called.
  66. // It is here as a convenience to get information on the final generated ZIP
  67. // file.
  68. // On failure, returns -1 and GetError() will return an non-empty message.
  69. virtual int Finish() = 0;
  70. // Get the current size of the ZIP file. This size will not be matching the
  71. // final ZIP file until Finish() has been called because Finish() is actually
  72. // writing the central directory of the ZIP File.
  73. virtual size_t GetSize() = 0;
  74. // Returns the current number of files stored in the ZIP.
  75. virtual int GetNumberFiles() = 0;
  76. // Create a new ZipBuilder writing the file zip_file and the size of the
  77. // output will be at most estimated_size. Use ZipBuilder::EstimateSize() or
  78. // ZipExtractor::CalculateOuputLength() to have an estimated_size depending on
  79. // a list of file to store.
  80. // On failure, returns NULL. Refer to errno for error code.
  81. static ZipBuilder* Create(const char* zip_file, size_t estimated_size);
  82. // Estimate the maximum size of the ZIP files containing files in the "files"
  83. // null-terminated array.
  84. // Returns 0 on error.
  85. static u8 EstimateSize(char const* const* files, char const* const* zip_paths,
  86. int nb_entries);
  87. };
  88. //
  89. // An abstract class to process data from a ZipExtractor.
  90. // Derive from this class if you wish to process data from a ZipExtractor.
  91. //
  92. class ZipExtractorProcessor {
  93. public:
  94. virtual ~ZipExtractorProcessor() {}
  95. // Tells whether to skip or process the file "filename". "attr" is the
  96. // external file attributes and can be converted to unix mode using the
  97. // zipattr_to_mode() function. This method is suppoed to returns true
  98. // if the file should be processed and false if it should be skipped.
  99. virtual bool Accept(const char* filename, const u4 attr) = 0;
  100. // Process a file accepted by Accept. The file "filename" has external
  101. // attributes "attr" and length "size". The file content is accessible
  102. // in the buffer pointed by "data".
  103. virtual void Process(const char* filename, const u4 attr,
  104. const u1* data, const size_t size) = 0;
  105. };
  106. //
  107. // Class interface for reading ZIP files
  108. //
  109. class ZipExtractor {
  110. public:
  111. virtual ~ZipExtractor() {}
  112. // Returns the text for the last error, or null on no last error.
  113. virtual const char* GetError() = 0;
  114. // Process the next files, returns false if the end of ZIP file has been
  115. // reached. The processor provided by the Create method will be called
  116. // if a file is encountered. If false is returned, check the return value
  117. // of GetError() for potential errors.
  118. virtual bool ProcessNext() = 0;
  119. // Process the all files, returns -1 on error (GetError() will be populated
  120. // on error).
  121. virtual int ProcessAll();
  122. // Reset the file pointer to the beginning.
  123. virtual void Reset() = 0;
  124. // Return the size of the ZIP file.
  125. virtual size_t GetSize() = 0;
  126. // Return the size of the resulting zip file by keeping only file
  127. // accepted by the processor and storing them uncompressed. This
  128. // method can be used to create a ZipBuilder for storing a subset
  129. // of the input files.
  130. // On error, 0 is returned and GetError() returns a non-empty message.
  131. virtual u8 CalculateOutputLength() = 0;
  132. // Create a ZipExtractor that extract the zip file "filename" and process
  133. // it with "processor".
  134. // On error, a null pointer is returned and the value of errno should be
  135. // checked.
  136. static ZipExtractor* Create(const char* filename,
  137. ZipExtractorProcessor *processor);
  138. };
  139. } // namespace devtools_ijar
  140. #endif // INCLUDED_THIRD_PARTY_IJAR_ZIP_H