makesfx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 or later
  4. (the contents of which are also included in unzip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*
  9. * makesfx - Makes a QDOS sfx zip file
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. * created by Jonathan Hudson, 04/09/95
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #define SFXFLAG "??Special Flag for unzipsfx hack ??"
  19. #ifdef QDOS
  20. # include <qdos.h>
  21. # define ZMODE (X_OK|R_OK)
  22. # define rev_long(x) (x)
  23. # define XFLAG 0x4afb
  24. #else
  25. # define ZMODE (R_OK)
  26. # define getchid(p1) p1
  27. # include <sys/stat.h>
  28. long rev_long(long l);
  29. # define XFLAG 0xfb4a
  30. typedef struct
  31. {
  32. long id;
  33. long dlen;
  34. } NTC;
  35. struct qdirect {
  36. long d_length __attribute__ ((packed)); /* file length */
  37. unsigned char d_access __attribute__ ((packed)); /* file access type */
  38. unsigned char d_type __attribute__ ((packed)); /* file type */
  39. long d_datalen __attribute__ ((packed)); /* data length */
  40. long d_reserved __attribute__ ((packed));/* Unused */
  41. short d_szname __attribute__ ((packed)); /* size of name */
  42. char d_name[36] __attribute__ ((packed));/* name area */
  43. long d_update __attribute__ ((packed)); /* last update */
  44. long d_refdate __attribute__ ((packed));
  45. long d_backup __attribute__ ((packed)); /* EOD */
  46. } ;
  47. int fs_headr (int fd, long t, struct qdirect *qs, short size)
  48. {
  49. NTC ntc;
  50. int r = -1;
  51. struct stat s;
  52. fstat(fd, &s);
  53. qs->d_length = s.st_size;
  54. lseek(fd, -8, SEEK_END);
  55. read(fd, &ntc, 8);
  56. if(ntc.id == *(long *)"XTcc")
  57. {
  58. qs->d_datalen = ntc.dlen; /* This is big endian */
  59. qs->d_type = 1;
  60. r = 0;
  61. }
  62. lseek(fd, 0, 0);
  63. return 42; /* why not ??? */
  64. }
  65. typedef unsigned char uch;
  66. long rev_long (long l)
  67. {
  68. uch cc[4];
  69. cc[0] = (uch)(l >> 24);
  70. cc[1] = (uch)((l >> 16) & 0xff);
  71. cc[2] = (uch)((l >> 8) & 0xff);
  72. cc[3] = (uch)(l & 0xff);
  73. return *(long *)cc;
  74. }
  75. #endif
  76. #define RBUFSIZ 4096
  77. void usage(void)
  78. {
  79. fputs("makesfx -o outfile -z zipfile -xunzipsfx -sstubfile\n", stderr);
  80. exit(0);
  81. }
  82. int main (int ac, char **av)
  83. {
  84. int fd, fo;
  85. static char local_sig[4] = "PK\003\004";
  86. char *p, tmp[4];
  87. short ok = 0;
  88. char *of = NULL;
  89. char *xf = NULL;
  90. char *zf = NULL;
  91. char *sf = NULL;
  92. int c;
  93. while((c = getopt(ac, av, "o:z:x:s:h")) != EOF)
  94. {
  95. switch(c)
  96. {
  97. case 'o':
  98. of = optarg;
  99. break;
  100. case 'z':
  101. zf = optarg;
  102. break;
  103. case 'x':
  104. xf = optarg;
  105. break;
  106. case 's':
  107. sf = optarg;
  108. break;
  109. case 'h':
  110. usage();
  111. break;
  112. }
  113. }
  114. if(zf && xf && of && sf)
  115. {
  116. if((fd = open(zf, O_RDONLY)) > 0)
  117. {
  118. if((read(fd, tmp, 4) == 4))
  119. {
  120. if(*(long *)tmp == *(long *)local_sig)
  121. {
  122. ok = 1;
  123. }
  124. }
  125. close(fd);
  126. }
  127. if(!ok)
  128. {
  129. fprintf(stderr,
  130. "Huum, %s doesn't look like a ZIP file to me\n", zf);
  131. exit(0);
  132. }
  133. if(strstr(xf, "unzipsfx"))
  134. {
  135. if(access(xf, ZMODE))
  136. {
  137. fprintf(stderr, "Sorry, don't like the look of %s\n", xf);
  138. exit(0);
  139. }
  140. }
  141. if((fo = open(of, O_CREAT|O_TRUNC|O_RDWR, 0666)) != -1)
  142. {
  143. struct qdirect sd,xd;
  144. int n;
  145. int dsoff = 0;
  146. int nfoff = 0;
  147. int zlen = 0;
  148. if((fd = open(sf, O_RDONLY)) != -1)
  149. {
  150. if(fs_headr(getchid(fd), -1, &sd, sizeof(sd)) > 0)
  151. {
  152. unsigned short *q;
  153. p = malloc(sd.d_length);
  154. n = read(fd, p, sd.d_length);
  155. for(q = (unsigned short *)p;
  156. q != (unsigned short *)(p+sd.d_length); q++)
  157. {
  158. if(*q == XFLAG && *(q+1) == XFLAG)
  159. {
  160. dsoff = (int)q-(int)p;
  161. break;
  162. }
  163. }
  164. write(fo, p, n);
  165. close(fd);
  166. }
  167. }
  168. if(dsoff == 0)
  169. {
  170. puts("Fails");
  171. exit(0);
  172. }
  173. if((fd = open(xf, O_RDONLY)) != -1)
  174. {
  175. char *q;
  176. if(fs_headr(getchid(fd), -1, &xd, sizeof(xd)) > 0)
  177. {
  178. p = realloc(p, xd.d_length);
  179. n = read(fd, p, xd.d_length);
  180. {
  181. for(q = p; q < p+xd.d_length ; q++)
  182. {
  183. if(*q == '?')
  184. {
  185. if(memcmp(q, SFXFLAG, sizeof(SFXFLAG)-1) == 0)
  186. {
  187. nfoff = (int)(q-p);
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. write(fo, p, n);
  194. close(fd);
  195. if((fd = open(zf, O_RDONLY)) > 0)
  196. {
  197. p = realloc(p, RBUFSIZ);
  198. while((n = read(fd, p, RBUFSIZ)) > 0)
  199. {
  200. write(fo, p, n);
  201. zlen += n;
  202. }
  203. close(fd);
  204. }
  205. lseek(fo, dsoff+4, SEEK_SET);
  206. n = rev_long((sd.d_length-dsoff));
  207. write(fo, &n, sizeof(long));
  208. n = rev_long(xd.d_length);
  209. write(fo, &n, sizeof(long));
  210. write(fo, &xd.d_datalen, sizeof(long));
  211. n = rev_long(nfoff);
  212. write(fo, &n, sizeof(long));
  213. n = rev_long(zlen);
  214. write(fo, &n, sizeof(long));
  215. close(fo);
  216. }
  217. else
  218. {
  219. close(fd);
  220. fputs("Can't read unzipsfx header", stderr);
  221. exit(0);
  222. }
  223. }
  224. free(p);
  225. }
  226. }
  227. else
  228. usage();
  229. return 0;
  230. }