mztools.c 8.0 KB

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