zip.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <string.h>
  24. #include <zlib.h>
  25. #if !defined (INT32_MAX)
  26. #define INT32_MAX 2147483647L
  27. #endif
  28. #define ZIP_LOCAL_FILE_SIG 0x04034b50
  29. #define ZIP_CENTRAL_DIRECTORY_SIG 0x02014b50
  30. #define ZIP_END_OF_CENTRAL_DIRECTORY_SIG 0x06054b50
  31. struct fz_zip_writer
  32. {
  33. fz_output *output;
  34. fz_buffer *central;
  35. int count;
  36. int closed;
  37. };
  38. void
  39. fz_write_zip_entry(fz_context *ctx, fz_zip_writer *zip, const char *name, fz_buffer *buf, int compress)
  40. {
  41. int offset = fz_tell_output(ctx, zip->output);
  42. int sum;
  43. sum = crc32(0, NULL, 0);
  44. sum = crc32(sum, buf->data, (uInt)buf->len);
  45. /* bit 11 of general purpose bit flag indicates UTF-8. */
  46. fz_append_int32_le(ctx, zip->central, ZIP_CENTRAL_DIRECTORY_SIG);
  47. fz_append_int16_le(ctx, zip->central, 0); /* version made by: MS-DOS */
  48. fz_append_int16_le(ctx, zip->central, 20); /* version to extract: 2.0 */
  49. fz_append_int16_le(ctx, zip->central, 1<<11); /* general purpose bit flag */
  50. fz_append_int16_le(ctx, zip->central, 0); /* compression method: store */
  51. fz_append_int16_le(ctx, zip->central, 0); /* TODO: last mod file time */
  52. fz_append_int16_le(ctx, zip->central, 0); /* TODO: last mod file date */
  53. fz_append_int32_le(ctx, zip->central, sum); /* crc-32 */
  54. fz_append_int32_le(ctx, zip->central, (int)buf->len); /* csize */
  55. fz_append_int32_le(ctx, zip->central, (int)buf->len); /* usize */
  56. fz_append_int16_le(ctx, zip->central, (int)strlen(name)); /* file name length */
  57. fz_append_int16_le(ctx, zip->central, 0); /* extra field length */
  58. fz_append_int16_le(ctx, zip->central, 0); /* file comment length */
  59. fz_append_int16_le(ctx, zip->central, 0); /* disk number start */
  60. fz_append_int16_le(ctx, zip->central, 0); /* internal file attributes */
  61. fz_append_int32_le(ctx, zip->central, 0); /* external file attributes */
  62. fz_append_int32_le(ctx, zip->central, offset); /* relative offset of local header */
  63. fz_append_string(ctx, zip->central, name);
  64. fz_write_int32_le(ctx, zip->output, ZIP_LOCAL_FILE_SIG);
  65. fz_write_int16_le(ctx, zip->output, 20); /* version to extract: 2.0 */
  66. fz_write_int16_le(ctx, zip->output, 1<<11); /* general purpose bit flag */
  67. fz_write_int16_le(ctx, zip->output, 0); /* compression method: store */
  68. fz_write_int16_le(ctx, zip->output, 0); /* TODO: last mod file time */
  69. fz_write_int16_le(ctx, zip->output, 0); /* TODO: last mod file date */
  70. fz_write_int32_le(ctx, zip->output, sum); /* crc-32 */
  71. fz_write_int32_le(ctx, zip->output, (int)buf->len); /* csize */
  72. fz_write_int32_le(ctx, zip->output, (int)buf->len); /* usize */
  73. fz_write_int16_le(ctx, zip->output, (int)strlen(name)); /* file name length */
  74. fz_write_int16_le(ctx, zip->output, 0); /* extra field length */
  75. fz_write_data(ctx, zip->output, name, strlen(name));
  76. fz_write_data(ctx, zip->output, buf->data, buf->len);
  77. ++zip->count;
  78. }
  79. void
  80. fz_close_zip_writer(fz_context *ctx, fz_zip_writer *zip)
  81. {
  82. int64_t offset = fz_tell_output(ctx, zip->output);
  83. fz_write_data(ctx, zip->output, zip->central->data, zip->central->len);
  84. fz_write_int32_le(ctx, zip->output, ZIP_END_OF_CENTRAL_DIRECTORY_SIG);
  85. fz_write_int16_le(ctx, zip->output, 0); /* number of this disk */
  86. fz_write_int16_le(ctx, zip->output, 0); /* number of disk where central directory starts */
  87. fz_write_int16_le(ctx, zip->output, zip->count); /* entries in central directory in this disk */
  88. fz_write_int16_le(ctx, zip->output, zip->count); /* entries in central directory in total */
  89. fz_write_int32_le(ctx, zip->output, (int)zip->central->len); /* size of the central directory */
  90. fz_write_int32_le(ctx, zip->output, (int)offset); /* offset of the central directory */
  91. fz_write_int16_le(ctx, zip->output, 5); /* zip file comment length */
  92. fz_write_data(ctx, zip->output, "MuPDF", 5);
  93. fz_close_output(ctx, zip->output);
  94. zip->closed = 1;
  95. }
  96. void
  97. fz_drop_zip_writer(fz_context *ctx, fz_zip_writer *zip)
  98. {
  99. if (!zip)
  100. return;
  101. if (!zip->closed)
  102. fz_warn(ctx, "dropping unclosed zip writer");
  103. fz_drop_output(ctx, zip->output);
  104. fz_drop_buffer(ctx, zip->central);
  105. fz_free(ctx, zip);
  106. }
  107. fz_zip_writer *
  108. fz_new_zip_writer_with_output(fz_context *ctx, fz_output *out)
  109. {
  110. fz_zip_writer *zip = NULL;
  111. fz_var(zip);
  112. fz_try(ctx)
  113. {
  114. zip = fz_malloc_struct(ctx, fz_zip_writer);
  115. zip->output = out;
  116. zip->central = fz_new_buffer(ctx, 0);
  117. }
  118. fz_catch(ctx)
  119. {
  120. fz_drop_output(ctx, out);
  121. if (zip)
  122. fz_drop_buffer(ctx, zip->central);
  123. fz_free(ctx, zip);
  124. fz_rethrow(ctx);
  125. }
  126. return zip;
  127. }
  128. fz_zip_writer *
  129. fz_new_zip_writer(fz_context *ctx, const char *filename)
  130. {
  131. fz_output *out = fz_new_output_with_path(ctx, filename, 0);
  132. fz_zip_writer *zip = NULL;
  133. fz_try(ctx)
  134. zip = fz_new_zip_writer_with_output(ctx, out);
  135. fz_catch(ctx)
  136. {
  137. fz_drop_output(ctx, out);
  138. fz_rethrow(ctx);
  139. }
  140. return zip;
  141. }