Advertisement
r57shell

aplib pack without header modification

Apr 13th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.64 KB | None | 0 0
  1. /*
  2.  * aPLib compression library  -  the smaller the better :)
  3.  *
  4.  * C example
  5.  *
  6.  * Copyright (c) 1998-2009 by Joergen Ibsen / Jibz
  7.  * All Rights Reserved
  8.  *
  9.  * http://www.ibsensoftware.com/
  10.  */
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <stddef.h>
  16. #include <limits.h>
  17.  
  18. #include "aplib.h"
  19.  
  20. /*
  21.  * Calling convention for the callback.
  22.  */
  23. #ifndef CB_CALLCONV
  24. # if defined(AP_DLL)
  25. #  define CB_CALLCONV __stdcall
  26. # elif defined(__GNUC__)
  27. #  define CB_CALLCONV
  28. # else
  29. #  define CB_CALLCONV __cdecl
  30. # endif
  31. #endif
  32.  
  33. /*
  34.  * Unsigned char type.
  35.  */
  36. typedef unsigned char byte;
  37.  
  38. /*
  39.  * Compute ratio between two numbers.
  40.  */
  41. unsigned int ratio(unsigned int x, unsigned int y)
  42. {
  43.     if (x <= UINT_MAX / 100) x *= 100; else y /= 100;
  44.  
  45.     if (y == 0) y = 1;
  46.  
  47.     return x / y;
  48. }
  49.  
  50. /*
  51.  * Compression callback.
  52.  */
  53. int CB_CALLCONV callback(unsigned int insize, unsigned int inpos, unsigned int outpos, void *cbparam)
  54. {
  55.    printf("\rcompressed %u -> %u bytes (%u%% done)", inpos, outpos, ratio(inpos, insize));
  56.  
  57.    return 1;
  58. }
  59.  
  60. /*
  61.  * Compress a file.
  62.  */
  63. int compress_file(const char *oldname, const char *packedname)
  64. {
  65.     FILE *oldfile;
  66.     FILE *packedfile;
  67.     unsigned int insize, outsize;
  68.     clock_t clocks;
  69.     byte *data, *packed, *workmem;
  70.  
  71.     /* open input file */
  72.     if ((oldfile = fopen(oldname, "rb")) == NULL)
  73.     {
  74.         printf("\nERR: unable to open input file\n");
  75.         return 1;
  76.     }
  77.  
  78.     /* get size of input file */
  79.     fseek(oldfile, 0, SEEK_END);
  80.     insize = (unsigned int) ftell(oldfile);
  81.     fseek(oldfile, 0, SEEK_SET);
  82.  
  83.     /* allocate memory */
  84.     if ((data    = (byte *) malloc(insize))                     == NULL ||
  85.         (packed  = (byte *) malloc(aP_max_packed_size(insize))) == NULL ||
  86.         (workmem = (byte *) malloc(aP_workmem_size(insize)))    == NULL)
  87.     {
  88.         printf("\nERR: not enough memory\n");
  89.         return 1;
  90.     }
  91.  
  92.     if (fread(data, 1, insize, oldfile) != insize)
  93.     {
  94.         printf("\nERR: error reading from input file\n");
  95.         return 1;
  96.     }
  97.  
  98.     clocks = clock();
  99.  
  100.     /* compress data block */
  101.     outsize = aP_pack(data, packed, insize, workmem, callback, NULL);
  102.  
  103.     clocks = clock() - clocks;
  104.  
  105.     /* check for compression error */
  106.     if (outsize == APLIB_ERROR)
  107.     {
  108.         printf("\nERR: an error occured while compressing\n");
  109.         return 1;
  110.     }
  111.  
  112.     /* create output file */
  113.     if ((packedfile = fopen(packedname, "wb")) == NULL)
  114.     {
  115.         printf("\nERR: unable to create output file\n");
  116.         return 1;
  117.     }
  118.  
  119.     fwrite(packed, 1, outsize, packedfile);
  120.  
  121.     /* show result */
  122.     printf("\rcompressed %u -> %u bytes (%u%%) in %.2f seconds\n",
  123.            insize, outsize, ratio(outsize, insize),
  124.            (double)clocks / (double)CLOCKS_PER_SEC);
  125.  
  126.     /* close files */
  127.     fclose(packedfile);
  128.     fclose(oldfile);
  129.  
  130.     /* free memory */
  131.     free(workmem);
  132.     free(packed);
  133.     free(data);
  134.  
  135.     return 0;
  136. }
  137.  
  138. /*
  139.  * Decompress a file.
  140.  */
  141. int decompress_file(const char *packedname, const char *newname)
  142. {
  143.     FILE *newfile;
  144.     FILE *packedfile;
  145.     unsigned int insize, outsize;
  146.     clock_t clocks;
  147.     byte *data, *packed;
  148.     unsigned int depackedsize;
  149.  
  150.     /* open input file */
  151.     if ((packedfile = fopen(packedname, "rb")) == NULL)
  152.     {
  153.         printf("\nERR: unable to open input file\n");
  154.         return 1;
  155.     }
  156.  
  157.     /* get size of input file */
  158.     fseek(packedfile, 0, SEEK_END);
  159.     insize = (unsigned int) ftell(packedfile);
  160.     fseek(packedfile, 0, SEEK_SET);
  161.  
  162.     /* allocate memory */
  163.     if ((packed = (byte *) malloc(insize)) == NULL)
  164.     {
  165.         printf("\nERR: not enough memory\n");
  166.         return 1;
  167.     }
  168.  
  169.     if (fread(packed, 1, insize, packedfile) != insize)
  170.     {
  171.         printf("\nERR: error reading from input file\n");
  172.         return 1;
  173.     }
  174.  
  175.     depackedsize = 0x800000;//aPsafe_get_orig_size(packed);
  176.  
  177.     /*if (depackedsize == APLIB_ERROR)
  178.     {
  179.         printf("\nERR: compressed data error\n");
  180.         return 1;
  181.     }*/
  182.  
  183.     /* allocate memory */
  184.     if ((data = (byte *) malloc(depackedsize)) == NULL)
  185.     {
  186.         printf("\nERR: not enough memory\n");
  187.         return 1;
  188.     }
  189.  
  190.     clocks = clock();
  191.  
  192.     /* decompress data */
  193.     outsize = aP_depack_asm(packed, data);
  194.  
  195.     clocks = clock() - clocks;
  196.  
  197.     /* check for decompression error */
  198.     /*if (outsize != depackedsize)
  199.     {
  200.         printf("\nERR: an error occured while decompressing\n");
  201.         return 1;
  202.     }*/
  203.  
  204.     /* create output file */
  205.     if ((newfile = fopen(newname, "wb")) == NULL)
  206.     {
  207.         printf("\nERR: unable to create output file\n");
  208.         return 1;
  209.     }
  210.  
  211.     /* write decompressed data */
  212.     fwrite(data, 1, outsize, newfile);
  213.  
  214.     /* show result */
  215.     printf("decompressed %u -> %u bytes in %.2f seconds\n",
  216.            insize, outsize,
  217.            (double)clocks / (double)CLOCKS_PER_SEC);
  218.  
  219.     /* close files */
  220.     fclose(packedfile);
  221.     fclose(newfile);
  222.  
  223.     /* free memory */
  224.     free(packed);
  225.     free(data);
  226.  
  227.     return 0;
  228. }
  229.  
  230. /*
  231.  * Show program syntax.
  232.  */
  233. void show_syntax(void)
  234. {
  235.     printf("syntax:\n\n"
  236.            "   compress    :  appack c <file> <packed_file>\n"
  237.            "   decompress  :  appack d <packed_file> <depacked_file>\n\n");
  238. }
  239.  
  240. /*
  241.  * Main.
  242.  */
  243. int main(int argc, char *argv[])
  244. {
  245.     /* show banner */
  246.     printf("===============================================================================\n"
  247.            "aPLib example                   Copyright (c) 1998-2009 by Joergen Ibsen / Jibz\n"
  248.            "                                                            All Rights Reserved\n\n"
  249.            "                                                  http://www.ibsensoftware.com/\n"
  250.            "===============================================================================\n\n");
  251.  
  252.     /* check number of arguments */
  253.     if (argc != 4)
  254.     {
  255.         show_syntax();
  256.         return 1;
  257.     }
  258.  
  259. #ifdef __WATCOMC__
  260.     /* OpenWatcom 1.2 line buffers stdout, so we unbuffer stdout manually
  261.        to make the progress indication in the callback work.
  262.     */
  263.     setbuf(stdout, NULL);
  264. #endif
  265.  
  266.     /* check first character of first argument to determine action */
  267.     if (argv[1][0] && argv[1][1] == '\0')
  268.     {
  269.         switch (argv[1][0])
  270.         {
  271.         /* compress file */
  272.         case 'c':
  273.         case 'C': return compress_file(argv[2], argv[3]);
  274.  
  275.         /* decompress file */
  276.         case 'd':
  277.         case 'D': return decompress_file(argv[2], argv[3]);
  278.         }
  279.     }
  280.  
  281.     /* show program syntax */
  282.     show_syntax();
  283.     return 1;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement