Advertisement
cr88192

FeLZ32 Tool 0: Updated

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