home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / MACUNPACK / macbinary.c < prev    next >
C/C++ Source or Header  |  1995-09-18  |  14KB  |  556 lines

  1. #include "macunpack.h"
  2. #include "globals.h"
  3. #include "../fileio/machdr.h"
  4. #include "../fileio/wrfile.h"
  5. #include "../fileio/kind.h"
  6. #include "zmahdr.h"
  7. #include "../util/util.h"
  8.  
  9. extern void dir();
  10. extern void mcb();
  11. #ifdef BIN
  12. extern void bin();
  13. #endif /* BIN */
  14. #ifdef JDW
  15. extern void jdw();
  16. #endif /* JDW */
  17. #ifdef STF
  18. extern void stf();
  19. #endif /* STF */
  20. #ifdef LZC
  21. extern void lzc();
  22. #endif /* LZC */
  23. #ifdef ASQ
  24. extern void asq();
  25. #endif /* ASQ */
  26. #ifdef ARC
  27. extern void arc();
  28. #endif /* ARC */
  29. #ifdef PIT
  30. extern void pit();
  31. #endif /* PIT */
  32. #ifdef SIT
  33. extern void sit();
  34. #endif /* SIT */
  35. #ifdef DIA
  36. extern void dia();
  37. #endif /* DIA */
  38. #ifdef CPT
  39. extern void cpt();
  40. #endif /* CPT */
  41. #ifdef ZMA
  42. extern void zma();
  43. #endif /* ZMA */
  44. #ifdef LZH
  45. extern void lzh();
  46. #endif /* LZH */
  47. #ifdef DD
  48. extern void dd_file();
  49. extern void dd_arch();
  50. #endif /* DD */
  51.  
  52. static void skip_file();
  53. #ifdef SCAN
  54. static void get_idf();
  55. #endif /* SCAN */
  56.  
  57. #define Z    (ZMAHDRS2 + 1)
  58.  
  59. static int info_given;
  60.  
  61. void macbinary()
  62. {
  63.     char header[INFOBYTES];
  64.     int c;
  65.  
  66.     while(1) {
  67.     if((c = fgetc(infp)) == EOF) {
  68.         break;
  69.     }
  70.     (void)ungetc(c, infp);
  71.     if(fread(header, 1, Z, infp) != Z) {
  72.         (void)fprintf(stderr, "Can't read MacBinary header.\n");
  73. #ifdef SCAN
  74.         do_error("macunpack: Can't read MacBinary header");
  75. #endif /* SCAN */
  76.         exit(1);
  77.     }
  78. #ifdef ZMA
  79.     if(!strncmp(header + 1, ZMAHDR, ZMAHDRS2)) {
  80.         /* Can distinguish zoom data forks only this way from macbinary */
  81.         if(verbose) {
  82.         (void)fprintf(stderr, "This is a \"Zoom\" archive.\n");
  83.         }
  84.         zma(header, (unsigned long)0);
  85.         exit(0);
  86.     }
  87. #endif /* ZMA */
  88.     if(fread(header + Z, 1, INFOBYTES - Z, infp) != INFOBYTES - Z) {
  89.         (void)fprintf(stderr, "Can't read MacBinary header.\n");
  90. #ifdef SCAN
  91.         do_error("macunpack: Can't read MacBinary header");
  92. #endif /* SCAN */
  93.         exit(1);
  94.     }
  95.     if(verbose && !info_given) {
  96.         do_indent(indent);
  97.         (void)fprintf(stderr, "This is \"MacBinary\" input.\n");
  98.         info_given = 1;
  99.     }
  100.     if(header[I_NAMEOFF] & 0x80) {
  101.         dir(header);
  102.         continue;
  103.     }
  104.     in_data_size = get4(header + I_DLENOFF);
  105.     in_rsrc_size = get4(header + I_RLENOFF);
  106.     in_ds = (((in_data_size + 127) >> 7) << 7);
  107.     in_rs = (((in_rsrc_size + 127) >> 7) << 7);
  108.     ds_skip = in_ds - in_data_size;
  109.     rs_skip = in_rs - in_rsrc_size;
  110.     if(dir_skip != 0) {
  111.         skip_file(in_ds + in_rs);
  112.         continue;
  113.     }
  114. #ifdef SCAN
  115.     if(header[I_NAMEOFF] == 0) {
  116.         get_idf((int)header[I_NAMEOFF + 1]);
  117.         skip_file(ds_skip + in_rs);
  118.         continue;
  119.     }
  120. #endif /* SCAN */
  121.     header[I_NAMEOFF + 1 + header[I_NAMEOFF]] = 0;
  122. #ifdef BIN
  123.     if(!strncmp(header + I_TYPEOFF, "TEXT", 4) &&
  124.        !strncmp(header + I_AUTHOFF, "BnHq", 4)) {
  125.         if(verbose) {
  126.         do_indent(indent);
  127.         (void)fprintf(stderr,
  128.             "This is a \"BinHex 5.0\" packed file.\n");
  129.         }
  130. #ifdef SCAN
  131.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  132. #endif /* SCAN */
  133.         bin(header, in_data_size, 0);
  134.         skip_file(ds_skip + in_rs);
  135.         continue;
  136.     }
  137.     if(!strncmp(header + I_TYPEOFF, "TEXT", 4) &&
  138.        !strncmp(header + I_AUTHOFF, "GJBU", 4)) {
  139.         if(verbose) {
  140.         do_indent(indent);
  141.         (void)fprintf(stderr,
  142.             "This is a \"MacBinary 1.0\" packed file.\n");
  143.         }
  144. #ifdef SCAN
  145.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  146. #endif /* SCAN */
  147.         bin(header, in_data_size, 0);
  148.         skip_file(ds_skip + in_rs);
  149.         continue;
  150.     }
  151.     /* Recognize only if creator is UMcp.  UMCP uses ttxt as default. */
  152.     if(!strncmp(header + I_TYPEOFF, "TEXT", 4) &&
  153.        !strncmp(header + I_AUTHOFF, "UMcp", 4)) {
  154.         if(verbose) {
  155.         do_indent(indent);
  156.         (void)fprintf(stderr, "This is a \"UMCP\" packed file.\n");
  157.         }
  158. #ifdef SCAN
  159.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  160. #endif /* SCAN */
  161.         bin(header, in_data_size, 1);
  162.         skip_file(ds_skip + in_rs);
  163.         continue;
  164.     }
  165. #endif /* BIN */
  166. #ifdef JDW
  167.     if(!strncmp(header + I_TYPEOFF, "Smal", 4) &&
  168.        !strncmp(header + I_AUTHOFF, "Jdw ", 4)) {
  169.         if(verbose) {
  170.         do_indent(indent);
  171.         (void)fprintf(stderr,
  172.             "This is a \"Compress It\" compressed file.\n");
  173.         }
  174. #ifdef SCAN
  175.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  176. #endif /* SCAN */
  177.         jdw((unsigned long)in_data_size);
  178.         skip_file(ds_skip + in_rs);
  179.         continue;
  180.     }
  181. #endif /* JDW */
  182. #ifdef STF
  183.     if(!strncmp(header + I_TYPEOFF, "COMP", 4) &&
  184.        !strncmp(header + I_AUTHOFF, "STF ", 4)) {
  185.         if(verbose) {
  186.         do_indent(indent);
  187.         (void)fprintf(stderr,
  188.             "This is a \"ShrinkToFit\" compressed file.\n");
  189.         }
  190. #ifdef SCAN
  191.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  192. #endif /* SCAN */
  193.         stf((unsigned long)in_data_size);
  194.         skip_file(ds_skip + in_rs);
  195.         continue;
  196.     }
  197. #endif /* STF */
  198. #ifdef LZC
  199.     if(!strncmp(header + I_TYPEOFF, "ZIVM", 4) &&
  200.        !strncmp(header + I_AUTHOFF, "LZIV", 4)) {
  201.         if(verbose) {
  202.         do_indent(indent);
  203.         (void)fprintf(stderr,
  204.             "This is a \"MacCompress(M)\" compressed file.\n");
  205.         }
  206. #ifdef SCAN
  207.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  208. #endif /* SCAN */
  209.         lzc(header);
  210.         skip_file(ds_skip + in_rs);
  211.         continue;
  212.     }
  213.     if(!strncmp(header + I_TYPEOFF, "ZIVU", 4) &&
  214.        !strncmp(header + I_AUTHOFF, "LZIV", 4)) {
  215.         if(verbose) {
  216.         do_indent(indent);
  217.         (void)fprintf(stderr,
  218.             "This is a \"MacCompress(U)\" compressed file.\n");
  219.         }
  220. #ifdef SCAN
  221.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  222. #endif /* SCAN */
  223.         lzc(header);
  224.         continue;
  225.     }
  226. #endif /* LZC */
  227. #ifdef ASQ
  228.     if(!strncmp(header + I_TYPEOFF, "ArCv", 4) &&
  229.        !strncmp(header + I_AUTHOFF, "TrAS", 4)) {
  230.         if(verbose) {
  231.         do_indent(indent);
  232.         (void)fprintf(stderr,
  233.             "This is a \"AutoSqueeze\" compressed file.\n");
  234.         }
  235. #ifdef SCAN
  236.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  237. #endif /* SCAN */
  238.         lzh(0);
  239.         skip_file(ds_skip + in_rs);
  240.         continue;
  241.     }
  242. #endif /* ASQ */
  243. #ifdef ARC
  244.     if(!strncmp(header + I_TYPEOFF, "mArc", 4) &&
  245.        !strncmp(header + I_AUTHOFF, "arc*", 4)) {
  246.         if(verbose) {
  247.         do_indent(indent);
  248.         (void)fprintf(stderr, "This is a \"ArcMac\" archive.\n");
  249.         }
  250. #ifdef SCAN
  251.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  252. #endif /* SCAN */
  253.         arc();
  254.         skip_file(ds_skip + in_rs);
  255.         continue;
  256.     }
  257.     if(!strncmp(header + I_TYPEOFF, "APPL", 4) &&
  258.        !strncmp(header + I_AUTHOFF, "arc@", 4)) {
  259.         if(verbose) {
  260.         do_indent(indent);
  261.         (void)fprintf(stderr,
  262.             "This is a \"ArcMac\" self extracting archive.\n");
  263.         }
  264. #ifdef SCAN
  265.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  266. #endif /* SCAN */
  267.         arc();
  268.         skip_file(ds_skip + in_rs);
  269.         continue;
  270.     }
  271. #endif /* ARC */
  272. #ifdef PIT
  273.     if(!strncmp(header + I_TYPEOFF, "PIT ", 4) &&
  274.        !strncmp(header + I_AUTHOFF, "PIT ", 4)) {
  275.         if(verbose) {
  276.         do_indent(indent);
  277.         (void)fprintf(stderr, "This is a \"PackIt\" archive.\n");
  278.         }
  279. #ifdef SCAN
  280.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  281. #endif /* SCAN */
  282.         pit();
  283.         skip_file(ds_skip + in_rs);
  284.         continue;
  285.     }
  286. #endif /* PIT */
  287. #ifdef SIT
  288.     if(!strncmp(header + I_TYPEOFF, "SIT!", 4) &&
  289.        !strncmp(header + I_AUTHOFF, "SIT!", 4)) {
  290.         if(verbose) {
  291.         do_indent(indent);
  292.         (void)fprintf(stderr, "This is a \"StuffIt\" archive.\n");
  293.         }
  294. #ifdef SCAN
  295.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  296. #endif /* SCAN */
  297.         sit();
  298.         skip_file(ds_skip + in_rs);
  299.         continue;
  300.     }
  301.     if(!strncmp(header + I_TYPEOFF, "SITD", 4) &&
  302.        !strncmp(header + I_AUTHOFF, "SIT!", 4)) {
  303.         if(verbose) {
  304.         do_indent(indent);
  305.         (void)fprintf(stderr,
  306.             "This is a \"StuffIt Deluxe\" archive.\n");
  307.         }
  308. #ifdef SCAN
  309.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  310. #endif /* SCAN */
  311.         sit();
  312.         skip_file(ds_skip + in_rs);
  313.         continue;
  314.     }
  315.     if(!strncmp(header + I_TYPEOFF, "APPL", 4) &&
  316.        !strncmp(header + I_AUTHOFF, "aust", 4)) {
  317.         if(verbose) {
  318.         do_indent(indent);
  319.         (void)fprintf(stderr,
  320.             "This is a \"StuffIt\" self extracting archive.\n");
  321.         }
  322. #ifdef SCAN
  323.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  324. #endif /* SCAN */
  325.         sit();
  326.         skip_file(ds_skip + in_rs);
  327.         continue;
  328.     }
  329. #endif /* SIT */
  330. #ifdef DIA
  331.     if(!strncmp(header + I_TYPEOFF, "Pack", 4) &&
  332.        !strncmp(header + I_AUTHOFF, "Pack", 4)) {
  333.         if(verbose) {
  334.         do_indent(indent);
  335.         (void)fprintf(stderr, "This is a \"Diamond\" archive.\n");
  336.         }
  337. #ifdef SCAN
  338.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  339. #endif /* SCAN */
  340.         dia((unsigned char *)header);
  341.         skip_file(ds_skip + in_rs);
  342.         continue;
  343.     }
  344.     if(!strncmp(header + I_TYPEOFF, "APPL", 4) &&
  345.        !strncmp(header + I_AUTHOFF, "Pack", 4) &&
  346.        in_data_size != 0) {
  347.         if(verbose) {
  348.         do_indent(indent);
  349.         (void)fprintf(stderr,
  350.             "This is a \"Diamond\" self extracting archive.\n");
  351.         }
  352. #ifdef SCAN
  353.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  354. #endif /* SCAN */
  355.         dia((unsigned char *)header);
  356.         skip_file(ds_skip + in_rs);
  357.         continue;
  358.     }
  359. #endif /* DIA */
  360. #ifdef CPT
  361.     if(!strncmp(header + I_TYPEOFF, "PACT", 4) &&
  362.        !strncmp(header + I_AUTHOFF, "CPCT", 4)) {
  363.         if(verbose) {
  364.         do_indent(indent);
  365.         (void)fprintf(stderr, "This is a \"Compactor\" archive.\n");
  366.         }
  367. #ifdef SCAN
  368.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  369. #endif /* SCAN */
  370.         cpt();
  371.         skip_file(ds_skip + in_rs);
  372.         continue;
  373.     }
  374.     if(!strncmp(header + I_TYPEOFF, "APPL", 4) &&
  375.        !strncmp(header + I_AUTHOFF, "EXTR", 4)) {
  376.         if(verbose) {
  377.         do_indent(indent);
  378.         (void)fprintf(stderr,
  379.             "This is a \"Compactor\" self extracting archive.\n");
  380.         }
  381. #ifdef SCAN
  382.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  383. #endif /* SCAN */
  384.         cpt();
  385.         skip_file(ds_skip + in_rs);
  386.         continue;
  387.     }
  388. #endif /* CPT */
  389. #ifdef ZMA
  390.     if(!strncmp(header + I_TYPEOFF, "zooM", 4) &&
  391.        !strncmp(header + I_AUTHOFF, "zooM", 4)) {
  392.         if(verbose) {
  393.         do_indent(indent);
  394.         (void)fprintf(stderr, "This is a \"Zoom\" archive.\n");
  395.         }
  396. #ifdef SCAN
  397.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  398. #endif /* SCAN */
  399.         zma((char *)NULL, (unsigned long)in_data_size);
  400.         skip_file(ds_skip + in_rs);
  401.         continue;
  402.     }
  403.     if(!strncmp(header + I_TYPEOFF, "APPL", 4) &&
  404.        !strncmp(header + I_AUTHOFF, "Mooz", 4)) {
  405.         if(verbose) {
  406.         do_indent(indent);
  407.         (void)fprintf(stderr,
  408.             "This is a \"Zoom\" self extracting archive.\n");
  409.         }
  410. #ifdef SCAN
  411.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  412. #endif /* SCAN */
  413.         zma((char *)NULL, (unsigned long)in_data_size);
  414.         skip_file(ds_skip + in_rs);
  415.         continue;
  416.     }
  417. #endif /* ZMA */
  418. #ifdef LZH
  419.     if(!strncmp(header + I_TYPEOFF, "LARC", 4) &&
  420.        !strncmp(header + I_AUTHOFF, "LARC", 4)) {
  421.         if(verbose) {
  422.         do_indent(indent);
  423.         (void)fprintf(stderr,
  424.             "This is a \"MacLHa (LHARC)\" archive.\n");
  425.         }
  426. #ifdef SCAN
  427.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  428. #endif /* SCAN */
  429.         lzh(0);
  430.         skip_file(ds_skip + in_rs);
  431.         continue;
  432.     }
  433.     if(!strncmp(header + I_TYPEOFF, "LHA ", 4) &&
  434.        !strncmp(header + I_AUTHOFF, "LARC", 4)) {
  435.         if(verbose) {
  436.         do_indent(indent);
  437.         (void)fprintf(stderr,
  438.             "This is a \"MacLHa (LHA)\" archive.\n");
  439.         }
  440. #ifdef SCAN
  441.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  442. #endif /* SCAN */
  443.         lzh(1);
  444.         skip_file(ds_skip + in_rs);
  445.         continue;
  446.     }
  447. #endif /* LZH */
  448. #ifdef DD
  449.     if((!strncmp(header + I_TYPEOFF, "DD01", 4) ||
  450.         !strncmp(header + I_TYPEOFF, "DDF?", 3) ||
  451.         !strncmp(header + I_TYPEOFF, "DDf?", 3)) &&
  452.        !strncmp(header + I_AUTHOFF, "DDAP", 4)) {
  453.         if(verbose) {
  454.         do_indent(indent);
  455.         (void)fprintf(stderr,
  456.             "This is a \"DiskDoubler\" compressed file.\n");
  457.         }
  458. #ifdef SCAN
  459.         do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  460. #endif /* SCAN */
  461.         dd_file((unsigned char *)header);
  462.         skip_file(ds_skip + in_rs);
  463.         continue;
  464.     }
  465.     if(!strncmp(header + I_TYPEOFF, "DDAR", 4) &&
  466.        !strncmp(header + I_AUTHOFF, "DDAP", 4)) {
  467.         if(verbose) {
  468.         do_indent(indent);
  469.         (void)fprintf(stderr, "This is a \"DiskDoubler\" archive.\n");
  470.         }
  471. #ifdef SCAN
  472.         do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  473. #endif /* SCAN */
  474.         dd_arch((unsigned char *)header);
  475.         skip_file(ds_skip + in_rs);
  476.         continue;
  477.     }
  478.     if(!strncmp(header + I_TYPEOFF, "APPL", 4) &&
  479.        !strncmp(header + I_AUTHOFF, "DSEA", 4)) {
  480.         if(verbose) {
  481.         do_indent(indent);
  482.         }
  483.         c = getc(infp);
  484.         (void)ungetc(c, infp);
  485.         if(c == 'D') {
  486.         if(verbose) {
  487.             (void)fprintf(stderr,
  488.             "This is a \"DiskDoubler\" self extracting archive.\n");
  489. #ifdef SCAN
  490.             do_idf(header + I_NAMEOFF + 1, ARCH_NAME);
  491. #endif /* SCAN */
  492.         }
  493.         dd_arch((unsigned char *)header);
  494.         } else {
  495.         if(verbose) {
  496.             (void)fprintf(stderr,
  497.             "This is a \"DiskDoubler\" self decompressing file.\n");
  498. #ifdef SCAN
  499.             do_idf(header + I_NAMEOFF + 1, PACK_NAME);
  500. #endif /* SCAN */
  501.         }
  502.         dd_file((unsigned char *)header);
  503.         }
  504.         skip_file(ds_skip + in_rs);
  505.         continue;
  506.     }
  507. #endif /* DD */
  508.     if(header[0] == 0 /* MORE CHECKS HERE! */) {
  509.         mcb(header, (unsigned long)in_rsrc_size,
  510.             (unsigned long)in_data_size, in_ds + in_rs);
  511.         continue;
  512.     } else {
  513.         (void)fprintf(stderr, "Unrecognized archive type.\n");
  514.         exit(1);
  515.     }
  516.     }
  517. }
  518.  
  519. static void skip_file(skip)
  520. int skip;
  521. {
  522.     char buff[1024];
  523.     int n;
  524.  
  525.     while(skip > 0) {
  526.     n = (skip < 1024 ? skip : 1024);
  527.     if(fread(buff, 1, n, infp) != n) {
  528.         (void)fprintf(stderr, "Incomplete file.\n");
  529. #ifdef SCAN
  530.         do_error("macunpack: Incomplete file");
  531. #endif /* SCAN */
  532.         exit(1);
  533.     }
  534.     skip -= n;
  535.     }
  536. }
  537.  
  538. #ifdef SCAN
  539. static void get_idf(kind)
  540. int kind;
  541. {
  542.     char filename[255];
  543.  
  544.     if(fread(filename, 1, in_data_size, infp) != in_data_size) {
  545.     (void)fprintf(stderr, "Incomplete file.\n");
  546. #ifdef SCAN
  547.         do_error("macunpack: Incomplete file");
  548. #endif /* SCAN */
  549.     exit(1);
  550.     }
  551.     filename[in_data_size] = 0;
  552.     do_idf(filename, kind);
  553. }
  554. #endif /* SCAN */
  555.  
  556.