暫無描述

mztools.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Additional tools for Minizip
  3. Code: Xavier Roche '2004
  4. License: Same as ZLIB (www.gzip.org)
  5. */
  6. /* Code */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "zlib.h"
  11. #include "unzip.h"
  12. #include "mztools.h"
  13. #define READ_8(adr) ((unsigned char)*(adr))
  14. #define READ_16(adr) ( READ_8(adr) | (READ_8(adr+1) << 8) )
  15. #define READ_32(adr) ( READ_16(adr) | (READ_16((adr)+2) << 16) )
  16. #define WRITE_8(buff, n) do { \
  17. *((unsigned char*)(buff)) = (unsigned char) ((n) & 0xff); \
  18. } while(0)
  19. #define WRITE_16(buff, n) do { \
  20. WRITE_8((unsigned char*)(buff), n); \
  21. WRITE_8(((unsigned char*)(buff)) + 1, (n) >> 8); \
  22. } while(0)
  23. #define WRITE_32(buff, n) do { \
  24. WRITE_16((unsigned char*)(buff), (n) & 0xffff); \
  25. WRITE_16((unsigned char*)(buff) + 2, (n) >> 16); \
  26. } while(0)
  27. extern int ZEXPORT unzRepair(file, fileOut, fileOutTmp, nRecovered, bytesRecovered)
  28. const char* file;
  29. const char* fileOut;
  30. const char* fileOutTmp;
  31. uLong* nRecovered;
  32. uLong* bytesRecovered;
  33. {
  34. int err = Z_OK;
  35. FILE* fpZip = fopen(file, "rb");
  36. FILE* fpOut = fopen(fileOut, "wb");
  37. FILE* fpOutCD = fopen(fileOutTmp, "wb");
  38. if (fpZip != NULL && fpOut != NULL) {
  39. int entries = 0;
  40. uLong totalBytes = 0;
  41. char header[30];
  42. char filename[256];
  43. char extra[1024];
  44. int offset = 0;
  45. int offsetCD = 0;
  46. while ( fread(header, 1, 30, fpZip) == 30 ) {
  47. int currentOffset = offset;
  48. /* File entry */
  49. if (READ_32(header) == 0x04034b50) {
  50. unsigned int version = READ_16(header + 4);
  51. unsigned int gpflag = READ_16(header + 6);
  52. unsigned int method = READ_16(header + 8);
  53. unsigned int filetime = READ_16(header + 10);
  54. unsigned int filedate = READ_16(header + 12);
  55. unsigned int crc = READ_32(header + 14); /* crc */
  56. unsigned int cpsize = READ_32(header + 18); /* compressed size */
  57. unsigned int uncpsize = READ_32(header + 22); /* uncompressed sz */
  58. unsigned int fnsize = READ_16(header + 26); /* file name length */
  59. unsigned int extsize = READ_16(header + 28); /* extra field length */
  60. filename[0] = extra[0] = '\0';
  61. /* Header */
  62. if (fwrite(header, 1, 30, fpOut) == 30) {
  63. offset += 30;
  64. } else {
  65. err = Z_ERRNO;
  66. break;
  67. }
  68. /* Filename */
  69. if (fnsize > 0) {
  70. if (fread(filename, 1, fnsize, fpZip) == fnsize) {
  71. if (fwrite(filename, 1, fnsize, fpOut) == fnsize) {
  72. offset += fnsize;
  73. } else {
  74. err = Z_ERRNO;
  75. break;
  76. }
  77. } else {
  78. err = Z_ERRNO;
  79. break;
  80. }
  81. } else {
  82. err = Z_STREAM_ERROR;
  83. break;
  84. }
  85. /* Extra field */
  86. if (extsize > 0) {
  87. if (fread(extra, 1, extsize, fpZip) == extsize) {
  88. if (fwrite(extra, 1, extsize, fpOut) == extsize) {
  89. offset += extsize;
  90. } else {
  91. err = Z_ERRNO;
  92. break;
  93. }
  94. } else {
  95. err = Z_ERRNO;
  96. break;
  97. }
  98. }
  99. /* Data */
  100. {
  101. int dataSize = cpsize;
  102. if (dataSize == 0) {
  103. dataSize = uncpsize;
  104. }
  105. if (dataSize > 0) {
  106. char* data = malloc(dataSize);
  107. if (data != NULL) {
  108. if ((int)fread(data, 1, dataSize, fpZip) == dataSize) {
  109. if ((int)fwrite(data, 1, dataSize, fpOut) == dataSize) {
  110. offset += dataSize;
  111. totalBytes += dataSize;
  112. } else {
  113. err = Z_ERRNO;
  114. }
  115. } else {
  116. err = Z_ERRNO;
  117. }
  118. free(data);
  119. if (err != Z_OK) {
  120. break;
  121. }
  122. } else {
  123. err = Z_MEM_ERROR;
  124. break;
  125. }
  126. }
  127. }
  128. /* Central directory entry */
  129. {
  130. char centralDirectoryEntryHeader[46];
  131. //char* comment = "";
  132. //int comsize = (int) strlen(comment);
  133. WRITE_32(centralDirectoryEntryHeader, 0x02014b50);
  134. WRITE_16(centralDirectoryEntryHeader + 4, version);
  135. WRITE_16(centralDirectoryEntryHeader + 6, version);
  136. WRITE_16(centralDirectoryEntryHeader + 8, gpflag);
  137. WRITE_16(centralDirectoryEntryHeader + 10, method);
  138. WRITE_16(centralDirectoryEntryHeader + 12, filetime);
  139. WRITE_16(centralDirectoryEntryHeader + 14, filedate);
  140. WRITE_32(centralDirectoryEntryHeader + 16, crc);
  141. WRITE_32(centralDirectoryEntryHeader + 20, cpsize);
  142. WRITE_32(centralDirectoryEntryHeader + 24, uncpsize);
  143. WRITE_16(centralDirectoryEntryHeader + 28, fnsize);
  144. WRITE_16(centralDirectoryEntryHeader + 30, extsize);
  145. WRITE_16(centralDirectoryEntryHeader + 32, 0 /*comsize*/);
  146. WRITE_16(centralDirectoryEntryHeader + 34, 0); /* disk # */
  147. WRITE_16(centralDirectoryEntryHeader + 36, 0); /* int attrb */
  148. WRITE_32(centralDirectoryEntryHeader + 38, 0); /* ext attrb */
  149. WRITE_32(centralDirectoryEntryHeader + 42, currentOffset);
  150. /* Header */
  151. if (fwrite(centralDirectoryEntryHeader, 1, 46, fpOutCD) == 46) {
  152. offsetCD += 46;
  153. /* Filename */
  154. if (fnsize > 0) {
  155. if (fwrite(filename, 1, fnsize, fpOutCD) == fnsize) {
  156. offsetCD += fnsize;
  157. } else {
  158. err = Z_ERRNO;
  159. break;
  160. }
  161. } else {
  162. err = Z_STREAM_ERROR;
  163. break;
  164. }
  165. /* Extra field */
  166. if (extsize > 0) {
  167. if (fwrite(extra, 1, extsize, fpOutCD) == extsize) {
  168. offsetCD += extsize;
  169. } else {
  170. err = Z_ERRNO;
  171. break;
  172. }
  173. }
  174. /* Comment field */
  175. /*
  176. if (comsize > 0) {
  177. if ((int)fwrite(comment, 1, comsize, fpOutCD) == comsize) {
  178. offsetCD += comsize;
  179. } else {
  180. err = Z_ERRNO;
  181. break;
  182. }
  183. }
  184. */
  185. } else {
  186. err = Z_ERRNO;
  187. break;
  188. }
  189. }
  190. /* Success */
  191. entries++;
  192. } else {
  193. break;
  194. }
  195. }
  196. /* Final central directory */
  197. {
  198. int entriesZip = entries;
  199. char finalCentralDirectoryHeader[22];
  200. //char* comment = ""; // "ZIP File recovered by zlib/minizip/mztools";
  201. //int comsize = (int) strlen(comment);
  202. if (entriesZip > 0xffff) {
  203. entriesZip = 0xffff;
  204. }
  205. WRITE_32(finalCentralDirectoryHeader, 0x06054b50);
  206. WRITE_16(finalCentralDirectoryHeader + 4, 0); /* disk # */
  207. WRITE_16(finalCentralDirectoryHeader + 6, 0); /* disk # */
  208. WRITE_16(finalCentralDirectoryHeader + 8, entriesZip); /* hack */
  209. WRITE_16(finalCentralDirectoryHeader + 10, entriesZip); /* hack */
  210. WRITE_32(finalCentralDirectoryHeader + 12, offsetCD); /* size of CD */
  211. WRITE_32(finalCentralDirectoryHeader + 16, offset); /* offset to CD */
  212. WRITE_16(finalCentralDirectoryHeader + 20, 0 /*comsize*/); /* comment */
  213. /* Header */
  214. if (fwrite(finalCentralDirectoryHeader, 1, 22, fpOutCD) == 22) {
  215. /* Comment field */
  216. /*
  217. if (comsize > 0) {
  218. if ((int)fwrite(comment, 1, comsize, fpOutCD) != comsize) {
  219. err = Z_ERRNO;
  220. }
  221. }
  222. */
  223. } else {
  224. err = Z_ERRNO;
  225. }
  226. }
  227. /* Final merge (file + central directory) */
  228. fclose(fpOutCD);
  229. if (err == Z_OK) {
  230. fpOutCD = fopen(fileOutTmp, "rb");
  231. if (fpOutCD != NULL) {
  232. int nRead;
  233. char buffer[8192];
  234. while ( (nRead = (int)fread(buffer, 1, sizeof(buffer), fpOutCD)) > 0) {
  235. if ((int)fwrite(buffer, 1, nRead, fpOut) != nRead) {
  236. err = Z_ERRNO;
  237. break;
  238. }
  239. }
  240. fclose(fpOutCD);
  241. }
  242. }
  243. /* Close */
  244. fclose(fpZip);
  245. fclose(fpOut);
  246. /* Wipe temporary file */
  247. (void)remove(fileOutTmp);
  248. /* Number of recovered entries */
  249. if (err == Z_OK) {
  250. if (nRecovered != NULL) {
  251. *nRecovered = entries;
  252. }
  253. if (bytesRecovered != NULL) {
  254. *bytesRecovered = totalBytes;
  255. }
  256. }
  257. } else {
  258. err = Z_STREAM_ERROR;
  259. }
  260. return err;
  261. }