?login_element?

Subversion Repositories NedoOS

Rev

Rev 130 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7.  
  8.  
  9. #include "mhmt-types.h"
  10. #include "mhmt-globals.h"
  11. #include "mhmt-parsearg.h"
  12. #include "mhmt-pack.h"
  13. #include "mhmt-depack.h"
  14.  
  15.  
  16. void show_help(void);
  17. void dump_config(void);
  18. ULONG do_files(void);
  19.  
  20. int main( int argc, char* argv[] )
  21. {
  22.         int error=0;
  23.         ULONG parse_result;
  24.  
  25.  
  26.         init_globals();
  27.  
  28.  
  29.         // printf short info
  30.         printf("mhmt - MeHruMsT - MEgalz, HRUM and hruST (c) 2009-2016 lvd^nedopc\n\n");
  31.  
  32.         // parse arguments
  33.         parse_result = parse_args(argc, argv);
  34.  
  35.         if( parse_result&ARG_PARSER_SHOWHELP )
  36.         {
  37.                 if( parse_result&ARG_PARSER_ERROR )
  38.                         printf("\n");
  39.  
  40.                 show_help();
  41.         }
  42.  
  43.         if( parse_result&ARG_PARSER_ERROR )
  44.         {
  45.                 printf("There were errors in arguments.\n");
  46.                 error++;
  47.         }
  48.         else if( parse_result&ARG_PARSER_GO )
  49.         {
  50.                 if( do_files() )
  51.                 {
  52.                         dump_config();
  53.                         if( wrk.mode )
  54.                         {
  55.                                 error += depack() ? 0 : 1;
  56.                         }
  57.                         else
  58.                         {
  59.                                 error += pack() ? 0 : 1;
  60.                         }
  61.                 }
  62.                 else
  63.                 {
  64.                         error++;
  65.                 }
  66.         }
  67.  
  68.  
  69.  
  70.         free_globals();
  71.  
  72.         return error;
  73. }
  74.  
  75. void show_help(void)
  76. {
  77.         printf
  78.         (
  79.                 "======== mhmt help ========\n"
  80.                 "parameters:\n"
  81.                 "-mlz, -hrm, -hst, -zx7 - use MegaLZ, hrum3.5, hrust1.x or zx7 formats (default is MegaLZ)\n"
  82.                 "-g - greedy coding (default is optimal coding), not supported yet\n"
  83.                 "-d - depacking instead of packing (default is packing)\n"
  84.                 "\n"
  85.                 "-zxh - use zx-specific header for hrum or hrust. DEFAULT is NO HEADER!\n"
  86.                 "       Not applicable for MegaLZ. If -zxh is specified, -16, NO -bend and\n"
  87.                 "       NO -mlz is forced.\n"
  88.                 "\n"
  89.                 "-8, -16 - bitstream is in bytes or words in packed file.\n"
  90.                 "          Default for MegaLZ is -8, for hrum and hrust is -16.\n"
  91.                 "\n"
  92.                 "-bend - if -16 specified, this makes words big-endian. Default is little-endian.\n"
  93.                 "\n"
  94.                 "-maxwinN - maximum lookback window. N is decimal number, which can only be\n"
  95.                 "           256,512,1024,2048,4096,8192,16384,32768. Default is format-specific\n"
  96.                 "           maximum window: MegaLZ is 4352, hrum is 4096, hrust is 65536,\n"
  97.                 "                           zx7 is 2176.\n"
  98.                 "           For given format, window can't be greater than default value\n"
  99.                 "\n"
  100.                 "-prebin <filename> - use specified file as prebinary for packing and depacking.\n"
  101.                 "\n"
  102.                 "usage:\n"
  103.                 "mhmt [parameter list] <input filename> [<output filename>]\n"
  104.                 "\n"
  105.                 "if no output filename given, filename is appended with \".mlz\", \".hrm\", \".hst\" or \".zx7\"\n"
  106.                 "in accordance with the format chosen; for depacking \".dpk\" is appended\n"
  107.                 "====== mhmt help end ======\n"
  108.                 "\n"
  109.         );
  110. }
  111.  
  112. void dump_config(void)
  113. {
  114.         printf("Configuration review:\n");
  115.         printf("\n");
  116.  
  117.         printf("Pack format: ");
  118.         if( wrk.packtype==PK_MLZ )
  119.                 printf("MegaLZ.\n");
  120.         else if( wrk.packtype==PK_HRM )
  121.                 printf("Hrum3.5\n");
  122.         else if( wrk.packtype==PK_HST )
  123.                 printf("Hrust1.x\n");
  124.         else if( wrk.packtype==PK_ZX7 )
  125.                 printf("zx7\n");
  126.         else
  127.                 printf("unknown.\n"); // this should be actually never displayed
  128.  
  129.         printf("Mode:        ");
  130.         if( wrk.mode )
  131.                 printf("depacking.\n");
  132.         else
  133.                 printf("packing.\n");
  134.  
  135.         if( !wrk.mode )
  136.         {
  137.                 printf("Pack coding: ");
  138.                 if( wrk.greedy )
  139.                         printf("greedy (sub-optimal but faster).\n");
  140.                 else
  141.                         printf("optimal (slower).\n");
  142.         }
  143.  
  144.         if( wrk.zxheader )
  145.         {
  146.                 printf("Header for old ZX ");
  147.                 if( wrk.packtype==PK_HRM )
  148.                         printf("hrum3.5 ");
  149.                 else if( wrk.packtype==PK_HST )
  150.                         printf("hrust1.x ");
  151.  
  152.                 printf("depackers is on.\n");
  153.         }
  154.  
  155.         if( wrk.wordbit )
  156.         {
  157.                 printf("Bitstream is grouped in words -\n");
  158.                 if( wrk.bigend )
  159.                 {
  160.                         printf(" words are big-endian, %s","INCOMPATIBLE with old ZX depackers!\n");
  161.                 }
  162.                 else
  163.                 {
  164.                         printf(" words are little-endian, ");
  165.                         if( (wrk.packtype==PK_HRM) || (wrk.packtype==PK_HST) )
  166.                                 printf("compatible with old ZX depackers.\n");
  167.                         else
  168.                                 printf("INCOMPATIBLE with old ZX depackers!\n");
  169.                 }
  170.         }
  171.         else
  172.         {
  173.                 printf("Bitstream is grouped in bytes -\n");
  174.                 if( wrk.packtype==PK_MLZ || wrk.packtype==PK_HRM || wrk.packtype==PK_ZX7 )
  175.                         printf(" compatible with old ZX depackers.\n");
  176.                 else
  177.                         printf(" INCOMPATIBLE with old ZX depackers!\n");
  178.         }
  179.  
  180.         printf("Maximum lookback window size is %d bytes.\n\n",wrk.maxwin);
  181.  
  182.  
  183.         // files
  184.         printf("Input file \"%s\" (%d bytes) successfully loaded.\n", wrk.fname_in, wrk.inlen);
  185.         printf("Output file \"%s\" created.\n", wrk.fname_out );
  186.  
  187.         // prebin file
  188.         if( wrk.prebin )
  189.         {
  190.                 printf("Prebinary file \"%s\" (%d bytes) successfully loaded.\n", wrk.fname_prebin, wrk.prelen);
  191.         }
  192.         else
  193.         {
  194.                 printf("No prebinary file specified.\n");
  195.         }
  196. //      ...more info...?
  197. }
  198.  
  199. // create output filename, open files, load input file in memory
  200. // returns 1 if no errors, otherwise zero
  201. ULONG do_files(void)
  202. {
  203.         char * pack_ext;
  204.         char * depk_ext;
  205.         LONG ext_pos;
  206.  
  207.         struct stat stfile;
  208.  
  209.         UBYTE * tmp;
  210.         ULONG len;
  211.  
  212.         // if there is no output filename, create it
  213.         if( !wrk.fname_out )
  214.         {
  215.                 depk_ext = ".dpk";
  216.  
  217.                 if( wrk.packtype==PK_MLZ )
  218.                         pack_ext = ".mlz";
  219.                 else if( wrk.packtype==PK_HRM )
  220.                         pack_ext = ".hrm";
  221.                 else if( wrk.packtype==PK_HST )
  222.                         pack_ext = ".hst";
  223.                 else if( wrk.packtype==PK_ZX7 )
  224.                         pack_ext = ".zx7";
  225.                 else
  226.                         pack_ext = ".pak"; // all have the same size, as well as depk_ext - 4 bytes!
  227.  
  228.  
  229.                 wrk.fname_out = (char *)malloc( 5 + strlen(wrk.fname_in) );
  230.                 if( !wrk.fname_out )
  231.                 {
  232.                         printf("Can't allocate memory for output filename!\n");
  233.                         return 0;
  234.                 }
  235.  
  236.                 strcpy(wrk.fname_out, wrk.fname_in);
  237.  
  238.                 if( !wrk.mode ) // packing
  239.                 {
  240.                         strcat(wrk.fname_out, pack_ext);
  241.                 }
  242.                 else // depacking
  243.                 {
  244.                         ext_pos = strlen( wrk.fname_out ) - 4;
  245.  
  246.                         if( (ext_pos>=0) && (!strcmp(&wrk.fname_out[ext_pos], pack_ext)) )
  247.                                 strcpy( &wrk.fname_out[ext_pos], depk_ext );
  248.                         else
  249.                                 strcat( wrk.fname_out, depk_ext );
  250.                 }
  251.         }
  252.  
  253.  
  254.         //open files
  255.         wrk.file_in=fopen(wrk.fname_in,"rb");
  256.         if(!wrk.file_in)
  257.         {
  258.                 printf("Cannot open input file \"%s\"!\n",wrk.fname_in);
  259.                 return 0;
  260.         }
  261.  
  262.         wrk.file_out=fopen(wrk.fname_out,"wb");
  263.         if(!wrk.file_out)
  264.         {
  265.                 printf("Cannot create output file \"%s\"!\n",wrk.fname_out);
  266.                 return 0;
  267.         }
  268.  
  269.         if( wrk.prebin )
  270.         {
  271.                 wrk.file_prebin = fopen(wrk.fname_prebin,"rb");
  272.                 if(!wrk.file_prebin)
  273.                 {
  274.                         printf("Cannot open prebinary file \"%s\"!\n",wrk.fname_prebin);
  275.                         return 0;
  276.                 }
  277.         }
  278.  
  279.  
  280.  
  281.         // get lengths of files
  282.         if( fseek(wrk.file_in,0,SEEK_END) )
  283.         {
  284.                 printf("Cannot fseek() input file \"%s\"!\n",wrk.fname_in);
  285.                 return 0;
  286.         }
  287.         wrk.inlen=(ULONG)ftell(wrk.file_in);
  288.         if( wrk.inlen==(ULONG)(-1L)  )
  289.         {
  290.                 printf("Cannot ftell() length of input file \"%s\"!\n",wrk.fname_in);
  291.                 wrk.inlen=0;
  292.                 return 0;
  293.         }
  294.         else if( wrk.inlen<16 && !wrk.mode ) // do not pack files less than 16 bytes
  295.         {
  296.                 printf("Input file \"%s\" is smaller than 16 bytes - I won't process it!\n",wrk.fname_in);
  297.                 return 0;
  298.         }
  299.         if( fseek(wrk.file_in,0,SEEK_SET) )
  300.         {
  301.                 printf("Cannot fseek() input file \"%s\"!\n",wrk.fname_in);
  302.                 return 0;
  303.         }
  304.  
  305.         if( wrk.prebin )
  306.         {
  307.                 if( fstat( fileno(wrk.file_prebin), &stfile ) )
  308. //              if( stat( wrk.fname_prebin, &stfile ) )
  309.                 {
  310.                         printf("Cannot fstat() prebin file \"%s\"\n",wrk.fname_prebin);
  311.                         return 0;
  312.                 }
  313.  
  314.                 wrk.prelen = (ULONG)stfile.st_size;
  315.         }
  316.  
  317.  
  318.  
  319.         // load files in mem
  320.         //
  321.         // first allocate place for both prebin and input file
  322.         len = wrk.inlen + wrk.prelen; // wrk.prelen is 0 if wrk.prebin==0
  323.         tmp = (UBYTE *)malloc( len );
  324.         //
  325.         // check alloc is OK
  326.         if( !tmp )
  327.         {
  328.                 if( wrk.prebin )
  329.                 {
  330.                         printf("Cannot allocate %d bytes of memory for loading both input file \"%s\" and prebin file \"%s\"!\n", len, wrk.fname_in, wrk.fname_prebin );
  331.                 }
  332.                 else
  333.                 {
  334.                         printf("Cannot allocate %d bytes of memory for loading input file \"%s\"!\n", wrk.inlen, wrk.fname_in );
  335.                 }
  336.                 return 0;
  337.         }
  338.         //
  339.         // assign to wrk.indata
  340.         wrk.indata_raw = tmp;
  341.         wrk.indata     = tmp + (wrk.prebin ? wrk.prelen : 0); // so we access prebin using negative displacements into wrk.indata array
  342.         //
  343.         // load input file and prebin file
  344.         if( wrk.inlen!=fread(wrk.indata,1,wrk.inlen,wrk.file_in) )
  345.         {
  346.                 printf("Cannot successfully load input file \"%s\" in memory!\n",wrk.fname_in);
  347.                 return 0;
  348.         }
  349.         if( wrk.prebin )
  350.         {
  351.                 if( wrk.prelen!=fread(tmp,1,wrk.prelen,wrk.file_prebin) )
  352.                 {
  353.                         printf("Cannot successfully load prebin file \"%s\" in memory!\n",wrk.fname_prebin);
  354.                         return 0;
  355.                 }
  356.         }
  357.  
  358.  
  359.  
  360.         return 1;// no errors
  361. }
  362.  
  363.