Zip.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibArchive/Zip.h>
  27. namespace Archive {
  28. bool Zip::find_end_of_central_directory_offset(const ReadonlyBytes& buffer, size_t& offset)
  29. {
  30. for (size_t backwards_offset = 0; backwards_offset <= UINT16_MAX; backwards_offset++) // the file may have a trailing comment of an arbitrary 16 bit length
  31. {
  32. if (buffer.size() < (sizeof(EndOfCentralDirectory) - sizeof(u8*)) + backwards_offset)
  33. return false;
  34. auto signature_offset = (buffer.size() - (sizeof(EndOfCentralDirectory) - sizeof(u8*)) - backwards_offset);
  35. if (memcmp(buffer.data() + signature_offset, end_of_central_directory_signature, sizeof(end_of_central_directory_signature)) == 0) {
  36. offset = signature_offset;
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. Optional<Zip> Zip::try_create(const ReadonlyBytes& buffer)
  43. {
  44. size_t end_of_central_directory_offset;
  45. if (!find_end_of_central_directory_offset(buffer, end_of_central_directory_offset))
  46. return {};
  47. EndOfCentralDirectory end_of_central_directory {};
  48. if (!end_of_central_directory.read(buffer.slice(end_of_central_directory_offset)))
  49. return {};
  50. if (end_of_central_directory.disk_number != 0 || end_of_central_directory.central_directory_start_disk != 0 || end_of_central_directory.disk_records_count != end_of_central_directory.total_records_count)
  51. return {}; // TODO: support multi-volume zip archives
  52. size_t member_offset = end_of_central_directory.central_directory_offset;
  53. for (size_t i = 0; i < end_of_central_directory.total_records_count; i++) {
  54. CentralDirectoryRecord central_directory_record {};
  55. if (!central_directory_record.read(buffer.slice(member_offset)))
  56. return {};
  57. if (central_directory_record.general_purpose_flags & 1)
  58. return {}; // TODO: support encrypted zip members
  59. if (central_directory_record.general_purpose_flags & 3)
  60. return {}; // TODO: support zip data descriptors
  61. if (central_directory_record.compression_method != ZipCompressionMethod::Store && central_directory_record.compression_method != ZipCompressionMethod::Deflate)
  62. return {}; // TODO: support obsolete zip compression methods
  63. if (central_directory_record.compression_method == ZipCompressionMethod::Store && central_directory_record.uncompressed_size != central_directory_record.compressed_size)
  64. return {};
  65. if (central_directory_record.start_disk != 0)
  66. return {}; // TODO: support multi-volume zip archives
  67. if (memchr(central_directory_record.name, 0, central_directory_record.name_length) != nullptr)
  68. return {};
  69. LocalFileHeader local_file_header {};
  70. if (!local_file_header.read(buffer.slice(central_directory_record.local_file_header_offset)))
  71. return {};
  72. if (buffer.size() - (local_file_header.compressed_data - buffer.data()) < central_directory_record.compressed_size)
  73. return {};
  74. member_offset += central_directory_record.size();
  75. }
  76. Zip zip;
  77. zip.m_input_data = buffer;
  78. zip.member_count = end_of_central_directory.total_records_count;
  79. zip.members_start_offset = end_of_central_directory.central_directory_offset;
  80. return zip;
  81. }
  82. bool Zip::for_each_member(Function<IterationDecision(const ZipMember&)> callback)
  83. {
  84. size_t member_offset = members_start_offset;
  85. for (size_t i = 0; i < member_count; i++) {
  86. CentralDirectoryRecord central_directory_record {};
  87. VERIFY(central_directory_record.read(m_input_data.slice(member_offset)));
  88. LocalFileHeader local_file_header {};
  89. VERIFY(local_file_header.read(m_input_data.slice(central_directory_record.local_file_header_offset)));
  90. ZipMember member;
  91. char null_terminated_name[central_directory_record.name_length + 1];
  92. memcpy(null_terminated_name, central_directory_record.name, central_directory_record.name_length);
  93. null_terminated_name[central_directory_record.name_length] = 0;
  94. member.name = String { null_terminated_name };
  95. member.compressed_data = { local_file_header.compressed_data, central_directory_record.compressed_size };
  96. member.compression_method = static_cast<ZipCompressionMethod>(central_directory_record.compression_method);
  97. member.uncompressed_size = central_directory_record.uncompressed_size;
  98. member.crc32 = central_directory_record.crc32;
  99. member.is_directory = central_directory_record.external_attributes & zip_directory_external_attribute || member.name.ends_with('/'); // FIXME: better directory detection
  100. if (callback(member) == IterationDecision::Break)
  101. return false;
  102. member_offset += central_directory_record.size();
  103. }
  104. return true;
  105. }
  106. }