cr88192

FeLZ32 Tool 0

Apr 17th, 2017
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.80 KB | None | 0 0
  1. /*
  2. FELZ32: LZ in terms of 32-bit DWORDs
  3.  
  4. tag:
  5.   bits  0..15: md, match distance (DWORDs)
  6.   bits 16..22: ml, match length (DWORDs)
  7.   bits 23..29: rl, raw length (DWORDs)
  8.   bits 30..31: al, tag/align
  9.  
  10. ml!=0:
  11.   md=distance
  12.   ml=length
  13.   mr=raw
  14.   al=align (0=DWORD aligned, others: byte offset)
  15.  
  16. ml==0, rl==0, al==0:
  17.     md==0: EOB
  18.     md!=0: long run of raw DWORDs
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. #include <time.h>
  26.  
  27. #ifdef __linux
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <termios.h>
  31. #endif
  32.  
  33. #ifdef __EMSCRIPTEN__
  34. #include <emscripten.h>
  35. #include <SDL/SDL.h>
  36. #include <SDL/SDL_mouse.h>
  37. #include <SDL/SDL_keyboard.h>
  38. #endif
  39.  
  40. #if defined(__linux) || defined(__EMSCRIPTEN__)
  41. #include <sys/time.h>
  42. #endif
  43.  
  44.  
  45. #if defined(__x86_64__) || defined(_M_X64)
  46. #ifndef X86_64
  47. #define X86_64
  48. #endif
  49.  
  50. #ifndef ARCH_64BIT
  51. #define ARCH_64BIT
  52. #endif
  53. #endif
  54.  
  55. #if defined(__i386__) || defined(_M_IX86)
  56. #ifndef X86
  57. #define X86
  58. #endif
  59. #endif
  60.  
  61. #if defined(__arm__) || defined(_M_ARM)
  62. #ifndef ARM
  63. #define ARM
  64. #endif
  65. #endif
  66.  
  67. #ifdef __BIG_ENDIAN__
  68. #define BIGENDIAN
  69. #endif
  70.  
  71. #ifdef __LITTLE_ENDIAN__
  72. #define LTLENDIAN
  73. #endif
  74.  
  75. #if defined(X86) || defined(X86_64)
  76. #ifndef LTLENDIAN
  77. #define LTLENDIAN
  78. #endif
  79. #endif
  80.  
  81.  
  82. typedef unsigned char byte;
  83. typedef signed char sbyte;
  84. typedef unsigned short u16;
  85. typedef signed short s16;
  86. typedef unsigned int u32;
  87. typedef signed int s32;
  88. #if defined(__linux) && defined(__x86_64__)
  89. typedef unsigned long u64;
  90. typedef signed long s64;
  91. #else
  92. typedef unsigned long long u64;
  93. typedef signed long long s64;
  94. #endif
  95.  
  96. typedef struct BGBDT_FeLzContext_s BGBDT_FeLzContext;
  97.  
  98. struct BGBDT_FeLzContext_s {
  99. u32 mark1;
  100.  
  101. byte use_lvm;       //use multi-level matching (log2)
  102. byte use_lvb;       //enable byte matching
  103.  
  104. u32 *css;
  105. u32 *cse;
  106. //u32 lzhash[16384];
  107. u32 lzhash[65536];
  108. byte lzhrov[4096];
  109. };
  110.  
  111. #if defined(X86) || defined(X86_64)
  112.  
  113. #define felz_getu32(ptr)            (*(u32 *)(ptr))
  114. #define felz_getu32le(ptr)          (*(u32 *)(ptr))
  115. #define felz_setu32le(ptr, val)     (*(u32 *)(ptr)=(val))
  116.  
  117. #define felz_getu32lea(ptr)         (*(u32 *)(ptr))
  118. #define felz_setu32lea(ptr, val)    (*(u32 *)(ptr)=(val))
  119.  
  120. #else
  121.  
  122. u32 bgbdt_felz_getu32(byte *ptr)
  123. {
  124.     u32 v;
  125. #if defined(BIGENDIAN)
  126.     v=ptr[3]|(ptr[2]<<8)|(ptr[1]<<16)|(ptr[0]<<24);
  127. #elif defined(LTLENDIAN)
  128.     v=ptr[0]|(ptr[1]<<8)|(ptr[2]<<16)|(ptr[3]<<24);
  129. #else
  130.     static int tv=1;
  131.     if(*(byte *)(&tv)==1)
  132.         { v=ptr[0]|(ptr[1]<<8)|(ptr[2]<<16)|(ptr[3]<<24); }
  133.     else
  134.         { v=ptr[3]|(ptr[2]<<8)|(ptr[1]<<16)|(ptr[0]<<24); }
  135. #endif
  136.     return(v);
  137. }
  138.  
  139. u32 bgbdt_felz_getu32le(byte *ptr)
  140. {
  141.     u32 v;
  142.     v=ptr[0]|(ptr[1]<<8)|(ptr[2]<<16)|(ptr[3]<<24);
  143.     return(v);
  144. }
  145.  
  146. void bgbdt_felz_setu32le(byte *ptr, u32 val)
  147. {   ptr[0]=val;     ptr[1]=val>>8;
  148.     ptr[2]=val>>16; ptr[3]=val>>24;     }
  149.  
  150. #define felz_getu32(ptr)            bgbdt_felz_getu32((byte *)(ptr))
  151. #define felz_getu32le(ptr)          bgbdt_felz_getu32le((byte *)(ptr))
  152. #define felz_setu32le(ptr, val)     bgbdt_felz_setu32le((byte *)(ptr), (val))
  153.  
  154. #if defined(LTLENDIAN)
  155. #define felz_getu32lea(ptr)         (*(u32 *)(ptr))
  156. #define felz_setu32lea(ptr, val)    (*(u32 *)(ptr)=(val))
  157. #else
  158. #define felz_getu32lea(ptr)         bgbdt_felz_getu32le((byte *)(ptr))
  159. #define felz_setu32lea(ptr, val)    bgbdt_felz_setu32le((byte *)(ptr), (val))
  160. #endif
  161.  
  162. #endif
  163.  
  164. int FRGL_TimeMS()
  165. {
  166. #ifdef _WIN32
  167.     static unsigned int init;
  168.     unsigned int t;
  169.  
  170.     t=timeGetTime();
  171.     if(!init)init=t;
  172.  
  173.     return((unsigned int)(t-init));
  174. #else
  175.  
  176. #ifdef __EMSCRIPTEN__
  177.     struct timeval  tp;
  178.     static int      secbase;
  179.  
  180.     gettimeofday(&tp, NULL);  
  181.     if(!secbase)secbase=tp.tv_sec;
  182.     return(((tp.tv_sec-secbase)*1000)+tp.tv_usec/1000);
  183. #endif
  184.  
  185. #ifndef linux
  186.     static int init;
  187.     int t;
  188.  
  189.     t=clock();
  190.     t*=CLOCKS_PER_SEC/1000.0;
  191. //  t=FRGL_TimeMS();
  192.  
  193.     if(!init)init=t;
  194.  
  195.     return((unsigned int)(t-init));
  196. #endif
  197. #ifdef linux
  198.     struct timeval  tp;
  199.     static int      secbase;
  200.  
  201.     gettimeofday(&tp, NULL);  
  202.     if(!secbase)secbase=tp.tv_sec;
  203.     return(((tp.tv_sec-secbase)*1000)+tp.tv_usec/1000);
  204. #endif
  205. #endif
  206. }
  207.  
  208.  
  209. int BGBDT_FeLz32_LookupMatch(BGBDT_FeLzContext *ctx,
  210.     u32 *cs, u32 *cse, int *rml, int *rmd)
  211. {
  212.     u32 *s, *se, *t, *te, *ct;
  213.     byte *ctb, *tb;
  214.     u32 v0, v, h, h1, o;
  215.     int hr, hm, sd;
  216.     int l, d, al, bl, bd, bal;
  217.     int i, j, k;
  218.  
  219.     if(ctx->use_lvb)
  220.     {
  221.         v0=*cs;
  222.         h=((v0*65521)>>16)&4095;
  223.         hr=ctx->lzhrov[h];
  224.         h1=h<<ctx->use_lvm;
  225.         sd=1<<ctx->use_lvm;
  226.         hm=sd-1;
  227.  
  228.         bl=0; bd=0;
  229.  
  230. #if 1
  231.         for(i=0; i<sd; i++)
  232.         {
  233.             o=ctx->lzhash[h1|((hr+i)&hm)]^v0;
  234.             ct=ctx->css+(o>>2);
  235.             ctb=(byte *)ct;
  236.             al=0;
  237.             if((ct>=cs) || (ct<ctx->css))
  238.                 continue;
  239.  
  240.             s=cs; se=s+126;
  241.             if(cse<se)se=cse;
  242.             tb=ctb;
  243.  
  244.             while(s<se)
  245.             {
  246.                 v=felz_getu32(tb);
  247.                 if(*s!=v)
  248.                     break;
  249.                 s++; tb+=4;
  250.             }
  251.            
  252.             l=s-cs;
  253.             d=cs-ct;
  254.            
  255.             if((l>bl) && (d<65536))
  256.                 { bl=l; bd=d; bal=al; }
  257.         }
  258. #endif
  259.  
  260. //      bl=0; bd=0;
  261.         for(i=0; i<sd; i++)
  262.         {
  263.             o=ctx->lzhash[h1|((hr+i)&hm)]^v0;
  264.             ct=ctx->css+(o>>2);
  265.             ctb=((byte *)ctx->css)+o;
  266.             al=o&3;
  267.             if((ct>=cs) || (ct<ctx->css))
  268.                 continue;
  269.  
  270.             s=cs; se=s+126;
  271.             if(cse<se)se=cse;
  272.             tb=ctb;
  273.  
  274.             while(s<se)
  275.             {
  276.                 v=felz_getu32(tb);
  277.                 if(*s!=v)
  278.                     break;
  279.                 s++; tb+=4;
  280.             }
  281.            
  282.             l=s-cs;
  283.             d=cs-ct;
  284.            
  285.             if((l>bl) && (d<65536))
  286.                 { bl=l; bd=d; bal=al; }
  287.         }
  288.        
  289.         *rml=bl|(bal<<14);
  290.         *rmd=bd;
  291.         return((bl>1) && (bd<65536));
  292.     }
  293.  
  294.     if(ctx->use_lvm)
  295.     {
  296.         v0=*cs;
  297.         h=((v0*65521)>>16)&4095;
  298.         hr=ctx->lzhrov[h];
  299.         h1=h<<ctx->use_lvm;
  300.         sd=1<<ctx->use_lvm;
  301.         hm=sd-1;
  302.  
  303.         bl=0; bd=0;
  304.         for(i=0; i<sd; i++)
  305.         {
  306.             o=ctx->lzhash[h1|((hr+i)&hm)]^v0;
  307.             ct=ctx->css+o;
  308.             if((ct>=cs) || (ct<ctx->css))
  309.                 continue;
  310.  
  311.             s=cs; se=s+126;
  312.             if(cse<se)se=cse;
  313.             t=ct;
  314.  
  315.             while(s<se)
  316.             {
  317.                 if(*s!=*t)
  318.                     break;
  319.                 s++; t++;
  320.             }
  321.            
  322.             l=s-cs;
  323.             d=cs-ct;
  324.            
  325.             if((l>bl) && (d<65536))
  326.                 { bl=l; bd=d; }
  327.         }
  328.        
  329.         *rml=bl;
  330.         *rmd=bd;
  331.         return((bl>1) && (bd<65536));
  332.     }else
  333.     {
  334.         v0=*cs;
  335.         h=((v0*65521)>>16)&4095;
  336.    
  337.         o=ctx->lzhash[h]^v0;
  338.         ct=ctx->css+o;
  339.        
  340.         if((ct>=cs) || (ct<ctx->css))
  341.         {
  342.             *rml=0; *rmd=0;
  343.             return(0);
  344.         }
  345.        
  346.         s=cs; se=s+126;
  347.         if(cse<se)se=cse;
  348.         t=ct;
  349.  
  350.         while(s<se)
  351.         {
  352.             if(*s!=*t)
  353.                 break;
  354.             s++; t++;
  355.         }
  356.        
  357.         l=s-cs;
  358.         d=cs-ct;
  359.         *rml=l;
  360.         *rmd=d;
  361.         return((l>1) && (d<65536));
  362.     }
  363. }
  364.  
  365. int BGBDT_FeLz32_UpdateString(BGBDT_FeLzContext *ctx,
  366.     u32 *cs, u32 *cse, int len)
  367. {
  368.     byte *csb;
  369.     u32 v;
  370.     int i, h, h1, hr, hm;
  371.  
  372.     if(ctx->use_lvb)
  373.     {
  374.         hm=(1<<ctx->use_lvm)-1;
  375.  
  376.         csb=(byte *)cs;
  377.         i=len*4;
  378.         while(i--)
  379.         {
  380.             v=felz_getu32(csb);
  381.             h=((v*65521)>>16)&4095;
  382.             h1=h<<ctx->use_lvm;
  383.  
  384.             hr=ctx->lzhrov[h];
  385.             hr=(hr-1)&hm;
  386.             ctx->lzhrov[h]=hr;
  387.             ctx->lzhash[h1|hr]=(csb-((byte *)ctx->css))^v;
  388.             csb++;
  389.         }
  390.  
  391.         return(0);
  392.     }
  393.  
  394.     if(ctx->use_lvm)
  395.     {
  396.         hm=(1<<ctx->use_lvm)-1;
  397.  
  398.         i=len;
  399.         while(i--)
  400.         {
  401.             v=*cs;
  402.             h=((v*65521)>>16)&4095;
  403.             h1=h<<ctx->use_lvm;
  404.  
  405.             hr=ctx->lzhrov[h];
  406.             hr=(hr-1)&hm;
  407.             ctx->lzhrov[h]=hr;
  408.             ctx->lzhash[h1|hr]=(cs-ctx->css)^v;
  409.             cs++;
  410.         }
  411.     }else
  412.     {
  413.         i=len;
  414.         while(i--)
  415.         {
  416.             v=*cs;
  417.             h=((v*65521)>>16)&4095;
  418.             ctx->lzhash[h]=(cs-ctx->css)^v;
  419.             cs++;
  420.         }
  421.     }
  422.     return(0);
  423. }
  424.  
  425. int BGBDT_FeLz32_EncodeBufferCtx(BGBDT_FeLzContext *ctx,
  426.     u32 *ibuf, int ibsz, u32 *obuf, int obsz, int lvl)
  427. {
  428.     u32 *cs, *cse, *csrb;
  429.     u32 *ct, *cte;
  430.     u32 tg;
  431.     int ml, md, rl, al;
  432.     int i, j, k;
  433.  
  434.     switch(lvl)
  435.     {
  436.     case 0:
  437.     case 1:
  438.         ctx->use_lvb=0;
  439.         ctx->use_lvm=0;
  440.         break;
  441.     case 2:
  442.         ctx->use_lvb=0;
  443.         ctx->use_lvm=1;
  444.         break;
  445.     case 3:
  446.         ctx->use_lvb=0;
  447.         ctx->use_lvm=2;
  448.         break;
  449.     case 4:
  450.         ctx->use_lvb=0;
  451.         ctx->use_lvm=3;
  452.         break;
  453.     case 5:
  454.         ctx->use_lvb=0;
  455.         ctx->use_lvm=4;
  456.         break;
  457.  
  458.     case 6:
  459.         ctx->use_lvb=1;
  460.         ctx->use_lvm=2;
  461.         break;
  462.     case 7:
  463.         ctx->use_lvb=1;
  464.         ctx->use_lvm=3;
  465.         break;
  466.     case 8:
  467.     case 9:
  468.         ctx->use_lvb=1;
  469.         ctx->use_lvm=4;
  470.         break;
  471.     }
  472.  
  473. //  ctx->use_lvb=0;
  474. //  ctx->use_lvm=0;
  475.  
  476. //  ctx->use_lvm=4;
  477. //  ctx->use_lvm=2;
  478. //  ctx->use_lvb=1;
  479. //  ctx->use_lvm=0;
  480.  
  481.     ml=4096<<ctx->use_lvm;
  482.  
  483. //  for(i=0; i<4096; i++)
  484. //  for(i=0; i<16384; i++)
  485.     for(i=0; i<ml; i++)
  486.     {
  487. //      ctx->lzhash[i]=NULL;
  488.         ctx->lzhash[i]=0;
  489.     }
  490.  
  491.     for(i=0; i<4096; i++)
  492.         ctx->lzhrov[i]=0;
  493.        
  494.     ctx->css=ibuf;
  495.     ctx->cse=ibuf+ibsz;
  496.    
  497.     cs=ibuf; cse=cs+ibsz;
  498.     ct=obuf; cte=ct+obsz;
  499.     csrb=cs;
  500.    
  501.     while(cs<cse)
  502.     {  
  503.         if(BGBDT_FeLz32_LookupMatch(ctx, cs, cse, &ml, &md))
  504.         {
  505.             rl=cs-csrb;
  506.             while(rl>=127)
  507.             {
  508.                 if(rl>=65536)
  509.                     rl=65535;
  510.                 *ct++=rl;
  511. //              felz_setu32lea(ct, rl);
  512. //              ct++;
  513.                 i=rl;
  514.                 while(i--)
  515.                     { *ct++=*csrb++; }
  516.                 rl=cs-csrb;
  517.             }
  518.            
  519. //          al=ml>>14;
  520. //          ml&=65535;
  521.            
  522.             tg=md|(ml<<16)|(rl<<23);
  523.             ml&=16383;
  524.             *ct++=tg;
  525. //          felz_setu32le(ct, tg);
  526. //          ct++;
  527.             i=rl;
  528.             while(i--)
  529.                 { *ct++=*csrb++; }
  530.             BGBDT_FeLz32_UpdateString(ctx, cs, cse, ml);
  531.             cs+=ml;
  532.             csrb=cs;
  533.         }else
  534.         {
  535.             BGBDT_FeLz32_UpdateString(ctx, cs, cse, 1);
  536.             cs++;
  537.         }
  538.     }
  539.  
  540.     rl=cs-csrb;
  541.     while(rl>0)
  542.     {
  543.         if(rl>=65536)
  544.             rl=65535;
  545.         *ct++=rl;
  546. //      felz_setu32lea(ct, rl);
  547. //      ct++;
  548.         i=rl;
  549.         while(i--)
  550.             { *ct++=*csrb++; }
  551.         rl=cs-csrb;
  552.     }
  553.    
  554.     *ct++=0;
  555.    
  556.     return(ct-obuf);
  557. }
  558.  
  559. int BGBDT_FeLz32_EncodeBuffer(
  560.     u32 *ibuf, int ibsz, u32 *obuf, int obsz, int lvl)
  561. {
  562.     BGBDT_FeLzContext tctx;
  563.     return(BGBDT_FeLz32_EncodeBufferCtx(&tctx, ibuf, ibsz, obuf, obsz, lvl));
  564. }
  565.  
  566. int BGBDT_FeLz32_DecodeBuffer(
  567.     u32 *ibuf, int ibsz, u32 *obuf, int obsz)
  568. {
  569.     u32 *cs, *cse, *cs1;
  570.     u32 *ct, *ct1, *cte;
  571.     byte *cs1b;
  572.     u64 lv;
  573.     u32 tg, v, v0, v1, v2;
  574.     int ml, md, rl, al;
  575.     int i, j, k;
  576.  
  577.     cs=ibuf; cse=cs+ibsz;
  578.     ct=obuf; cte=ct+obsz;
  579.  
  580.     while(cs<cse)
  581.     {
  582.         tg=*cs++;
  583. //      tg=felz_getu32lea(cs);
  584. //      cs++;
  585.  
  586.         ml=(tg>>16)&127;
  587.         md=tg&65535;
  588.  
  589.         if(ml)
  590.         {
  591.             rl=(tg>>23)&127;
  592.             al=(tg>>30)&3;
  593.  
  594.             i=rl;
  595.  
  596. #if defined(X86) || defined(X86_64)
  597.             if(rl)
  598.             {
  599.                 cs1=cs;     ct1=ct;
  600.                 ct+=rl;     cs+=rl;
  601.                 while(ct1<ct)
  602.                 {
  603.                     *ct1++=*cs1++; *ct1++=*cs1++;
  604.                 }
  605.             }
  606. #else
  607.             while(i--)
  608.                 { *ct++=*cs++; }
  609. #endif
  610.  
  611.             if(al)
  612.             {
  613.                 cs1b=((byte *)(ct-md))+al;
  614.                 i=ml;
  615.                 if(md<2)
  616.                 {
  617. #if defined(X86) || defined(X86_64)
  618.                     switch(al)
  619.                     {
  620.                     case 3:
  621.                         v=cs1b[0];
  622.                         v=v|(v<<8);
  623.                         v=v|(v<<16);
  624.                         ct1=ct; ct+=ml;
  625.                         while(ct1<ct)
  626.                             { *ct1++=v; *ct1++=v; }
  627.                         break;
  628.                     case 2:
  629.                         v=cs1b[0]|(cs1b[1]<<8);
  630.                         v=v|(v<<16);
  631.                         while(i--)
  632.                             { *ct++=v; }
  633.                         break;
  634. #if 1
  635.                     case 1:
  636.                         v=cs1b[0]|(cs1b[1]<<8)|(cs1b[2]<<8);
  637.                         v0=v|(v<<24);
  638.                         v1=(v>>8)|(v<<16);
  639.                         v2=(v>>16)|(v<<8);
  640.                         ct1=ct; ct+=ml;
  641.                         while(ct1<ct)
  642.                         {
  643.                             *ct1++=v0;
  644.                             *ct1++=v1;
  645.                             *ct1++=v2;
  646.                         }
  647.                         break;
  648. #endif
  649.                     default:
  650.                         while(i--)
  651.                         {
  652.                             ((byte *)ct)[0]=cs1b[0];
  653.                             ((byte *)ct)[1]=cs1b[1];
  654.                             ((byte *)ct)[2]=cs1b[2];
  655.                             ((byte *)ct)[3]=cs1b[3];
  656.                             cs1b+=4;
  657.                             ct++;
  658.                         }
  659.                         break;
  660.                     }
  661. #else
  662.                     while(i--)
  663.                     {
  664.                         ((byte *)ct)[0]=cs1b[0];
  665.                         ((byte *)ct)[1]=cs1b[1];
  666.                         ((byte *)ct)[2]=cs1b[2];
  667.                         ((byte *)ct)[3]=cs1b[3];
  668.                         cs1b+=4;
  669.                         ct++;
  670.                     }
  671. #endif
  672.                 }else
  673.                 {
  674.                     while(i--)
  675.                     {
  676.                         j=felz_getu32(cs1b);
  677.                         cs1b+=4;
  678.                         *ct++=j;
  679.                     }
  680.                 }
  681.             }else
  682.             {
  683.                 cs1=ct-md;
  684.                 i=ml;
  685.  
  686. #if defined(X86) || defined(X86_64)
  687.                 if(md>1)
  688.                 {
  689.                     ct1=ct;     ct+=ml;
  690.                     while(ct1<ct)
  691.                         { *ct1++=*cs1++; *ct1++=*cs1++; }
  692.                 }else
  693.                 {
  694.                     v=*cs1;
  695.                     ct1=ct; ct+=ml;
  696.                     while(ct1<ct)
  697.                         { *ct1++=v; *ct1++=v; }
  698.                 }
  699. #else
  700.                 while(i--)
  701.                     { *ct++=*cs1++; }
  702. #endif
  703.             }
  704.             continue;
  705.         }
  706.        
  707.         if(!tg)
  708.             break;
  709.        
  710.         if(!(tg&0xFFFF0000U))
  711.         {
  712. #if defined(X86) || defined(X86_64)
  713.             cs1=cs;     ct1=ct;
  714.             ct+=md;     cs+=md;
  715.             while(ct1<ct)
  716.                 { *ct1++=*cs1++; *ct1++=*cs1++; }
  717. #else
  718.             i=md;
  719.             while(i--)
  720.                 { *ct++=*cs++; }
  721. #endif
  722.             continue;
  723.         }
  724.        
  725. //      FRGL_DBGBREAK_SOFT
  726.     }
  727.    
  728.     return(ct-obuf);
  729. }
  730.  
  731. byte *loadfile(char *path, int *rsz)
  732. {
  733.     byte *buf;
  734.     FILE *fd;
  735.     int sz, i;
  736.    
  737.     fd=fopen(path, "rb");
  738.     if(!fd)
  739.         return(NULL);
  740.  
  741.     fseek(fd, 0, 2);
  742.     sz=ftell(fd);
  743.     fseek(fd, 0, 0);
  744.     buf=malloc(sz);
  745.     i=fread(buf, 1, sz, fd);
  746.     fclose(fd);
  747.    
  748.     *rsz=sz;
  749.     return(buf);
  750. }
  751.  
  752. int storefile(char *name, byte *ibuf, int isz)
  753. {
  754.     byte *tbuf;
  755.     FILE *fd;
  756.     int sz;
  757.  
  758.     fd=fopen(name, "wb");
  759.     if(!fd)
  760.     {
  761.         printf("Fail Open Write %s\n", name);
  762.         return(-1);
  763.     }
  764.  
  765.     fwrite(ibuf, 1, isz, fd);
  766.     fclose(fd);
  767.     return(0);
  768. }
  769.  
  770.  
  771. int help(char *prg)
  772. {
  773.     printf("usage: %s opts* infile outfile\n", prg);
  774.     printf("\t-d Decode\n");
  775.     printf("\t-1 .. -9 Compression Level\n");
  776.     return(0);
  777. }
  778.  
  779. int main(int argc, char *argv[])
  780. {
  781.     byte *ibuf, *obuf, *tbuf;
  782.     byte *cs, *cse;
  783.     char *ifn, *ofn;
  784.     byte dec, lvl;
  785.     s64 li0;
  786.     double f, g;
  787.     int t0, t1, t2, t3;
  788.     int bd0, bl0, bd1, bl1;
  789.     int bl, bd;
  790.     int sz, csz, dsz, h, rb;
  791.     int i, j, k;
  792.    
  793.     ifn=NULL; ofn=NULL; dec=0; lvl=1;
  794.     for(i=1; i<argc; i++)
  795.     {
  796.         if(argv[i][0]=='-')
  797.         {
  798.             if(!strcmp(argv[i], "-d"))
  799.                 { dec=1; continue; }
  800.             if(!strcmp(argv[i], "-t"))
  801.                 { dec=2; continue; }
  802.  
  803.             if(!strcmp(argv[i], "-1"))
  804.                 { lvl=1; continue; }
  805.             if(!strcmp(argv[i], "-2"))
  806.                 { lvl=2; continue; }
  807.             if(!strcmp(argv[i], "-3"))
  808.                 { lvl=3; continue; }
  809.             if(!strcmp(argv[i], "-4"))
  810.                 { lvl=4; continue; }
  811.             if(!strcmp(argv[i], "-5"))
  812.                 { lvl=5; continue; }
  813.             if(!strcmp(argv[i], "-6"))
  814.                 { lvl=6; continue; }
  815.             if(!strcmp(argv[i], "-7"))
  816.                 { lvl=7; continue; }
  817.             if(!strcmp(argv[i], "-8"))
  818.                 { lvl=8; continue; }
  819.             if(!strcmp(argv[i], "-9"))
  820.                 { lvl=9; continue; }
  821.        
  822.             continue;
  823.         }
  824.         if(!ifn)
  825.             { ifn=argv[i]; continue; }
  826.         if(!ofn)
  827.             { ofn=argv[i]; continue; }
  828.     }
  829.  
  830.     if(!ifn || !ofn)
  831.     {
  832.         help(argv[0]);
  833.         return(-1);
  834.     }
  835.    
  836.     if(dec==0)
  837.     {
  838.         ibuf=loadfile(ifn, &sz);
  839.         obuf=malloc(sz*2);
  840.  
  841.         i=BGBDT_FeLz32_EncodeBuffer(
  842.             (u32 *)ibuf, (sz+3)/4,
  843.             (u32 *)(obuf+16), (2*sz+3)/4, lvl);
  844.         csz=i*4+16;
  845.         obuf[0]='F';    obuf[1]='e';
  846.         obuf[2]='L';    obuf[3]='Z';
  847.         obuf[4]='3';    obuf[5]='2';
  848.         obuf[6]=1;      obuf[7]=0;
  849.         felz_setu32le(obuf+ 8, csz);
  850.         felz_setu32le(obuf+12, sz);
  851.        
  852.         printf("Enc %d -> %d (%.2f%%)\n", sz, csz, (100.0*csz)/(sz+1));
  853.        
  854.         if(ofn)
  855.         {
  856.             storefile(ofn, obuf, csz);
  857.         }
  858.        
  859.         return(0);
  860.     }
  861.  
  862.     if(dec==1)
  863.     {
  864.         ibuf=loadfile(ifn, &csz);
  865.  
  866.         if(memcmp(ibuf, "FeLZ32", 6))
  867.         {
  868.             printf("FeLZ Magic Failed\n");
  869.             return(-1);
  870.         }
  871.  
  872.         csz=felz_getu32le(ibuf+ 8);
  873.         dsz=felz_getu32le(ibuf+12);
  874.         obuf=malloc(dsz+4096);
  875.        
  876.         BGBDT_FeLz32_DecodeBuffer(
  877.             (u32 *)(ibuf+16), ((csz-16)+3)/4,
  878.             (u32 *)(obuf), (dsz+3)/4);
  879.         storefile(ofn, obuf, dsz);
  880.  
  881.         return(0);
  882.     }
  883.  
  884.     if(dec==2)
  885.     {
  886.         ibuf=loadfile(ifn, &sz);
  887.         obuf=malloc(sz*2);
  888.         tbuf=malloc(sz*2);
  889.        
  890.         dsz=sz;
  891.  
  892.         i=BGBDT_FeLz32_EncodeBuffer(
  893.             (u32 *)ibuf, (sz+3)/4,
  894.             (u32 *)(obuf+16), (2*sz+3)/4, lvl);
  895.         csz=i*4+16;
  896.         obuf[0]='F';    obuf[1]='e';
  897.         obuf[2]='L';    obuf[3]='Z';
  898.         obuf[4]='3';    obuf[5]='2';
  899.         obuf[6]=1;      obuf[7]=0;
  900.         felz_setu32le(obuf+ 8, csz);
  901.         felz_setu32le(obuf+12, sz);
  902.        
  903.         printf("Test %d -> %d (%.2f%%)\n", sz, csz, (100.0*csz)/(sz+1));
  904.        
  905.         if(ofn)
  906.         {
  907.             storefile(ofn, obuf, csz);
  908.         }
  909.        
  910. //      printf("A0A\n");
  911.  
  912.         i=BGBDT_FeLz32_DecodeBuffer(
  913.             (u32 *)(obuf+16), ((csz-16)+3)/4,
  914.             (u32 *)(tbuf), (dsz+3)/4);
  915.  
  916. //      printf("A0B\n");
  917.        
  918.         if(i!=((dsz+3)/4))
  919.         {
  920.             printf("Size Mismatch %d -> %d\n", dsz, i*4);
  921.         }
  922.        
  923.         if(memcmp(ibuf, tbuf, dsz))
  924.         {
  925.             printf("Data Mismatch\n");
  926.             return(-1);
  927.         }
  928.        
  929.         t0=FRGL_TimeMS();
  930.         t2=t0+10000;
  931.         t1=t0;
  932.        
  933. //      printf("A1\n");
  934.        
  935.         li0=0;
  936.         while((t1<t2) && (t1>=t0))
  937.         {
  938.             i=BGBDT_FeLz32_DecodeBuffer(
  939.                 (u32 *)(obuf+16), ((csz-16)+3)/4,
  940.                 (u32 *)(tbuf), (dsz+3)/4);
  941.             li0+=i;
  942.  
  943.             f=(t1-t0)/(1000.0);
  944.             g=(4*li0)/(f*1000000.0);
  945.             printf("%.2fs %.2fMB/s\r", f, g);
  946.             t1=FRGL_TimeMS();
  947.         }
  948.  
  949. //      printf("A2\n");
  950.  
  951.         printf("\n");
  952.        
  953.         return(0);
  954.     }
  955. }
Advertisement
Add Comment
Please, Sign In to add comment