1 /******************************************************************************
2 *
3 * Copyright (C) 2009, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 #include "lzge.h"
15 //#include "..\GEA\gea.h"
16 /*#ifdef LAUNCHER
17 #include "K:\Gentee\Gentee Language\Opti Launcher\memory.h"
18 #else
19 #include "K:\Gentee\Gentee Language\VM Lib\memory.h"
20 #endif*/
21
22 //--------------------------------------------------------------------------
23
24 dword STDCALL lzge_decode( pbyte in, pbyte out, dword size, pslzge lzge )
25 {
26 shuf huf;
27 pbyte end = out + size;
28 dword result;
29 dword value, len, offset, lenfooter, footer;
30 dword numblock = 0;
31
32 out += lzge->solidoff;
33
34 if ( !lzge->hufblock ) lzge->hufblock = HUF_BLOCK;
35 lzge_ranges( lzge, (1 << min( 21, lzge_bits( size ))) - 1 );
36 lzge->mft[0] = 3;
37 lzge->mft[1] = 4;
38 lzge->mft[2] = 5;
39
40 huf_new( &huf, LZGE_ALPHABET + SLOT_LEN * lzge->numoff + 1 );
41 // huf.fixed = 1;
42
43 huf.ptr = in;
44 huf.bit = 7;
45
46 while ( out < end )
47 {
48 numblock = 0;
49 // Строим дерево
50 huf_detree( &huf );
51 while ( numblock != lzge->hufblock && out < end )
52 {
53 // printf("%i\r", size - ( end - out ));
54 value = huf_inbits( &huf );
55 if ( value < LZGE_ALPHABET )
56 {
57 *out++ = ( byte )value;
58 // printf("%c", *( out - 1 ));
59 }
60 else
61 {
62 value -= LZGE_ALPHABET;
63 if ( value == SLOT_LEN * lzge->numoff )
64 {
65 footer = 0;
66 lenfooter = SLOT_LEN;
67 }
68 else
69 {
70 lenfooter = value % SLOT_LEN;
71 footer = value / SLOT_LEN;
72 }
73 len = huf_unpacknum( &huf, lenbits[ lenfooter ] )+ lenmin[ lenfooter ];
74 if ( lenfooter == SLOT_LEN )
75 offset = huf_unpacknum( &huf, lzge->maxbit );
76 else
77 {
78 offset = huf_unpacknum( &huf, rngbits[ footer ] ) + rngmin[ footer ];
79
80 if ( !footer ) offset = lzge->mft[0];
81 else
82 if ( footer == 1 )
83 {
84 offset = lzge->mft[1];
85 lzge->mft[1] = lzge->mft[ 0 ];
86 lzge->mft[0] = offset;
87 }
88 else
89 {
90 if ( footer == 2 )
91 offset = lzge->mft[2];
92 lzge->mft[2] = lzge->mft[1];
93 lzge->mft[1] = lzge->mft[0];
94 lzge->mft[0] = offset;
95 }
96 }
97 // if ( numblock < 100 )
98 // printf("cur = %i footer = %i lenfooter = %i Off=%i Len=%i\n",
99 // size - ( end - out ), footer, lenfooter, offset, len );
100 if ( offset < len )
101 {
102 while ( len-- )
103 {
104 *out = *( out - offset );
105 out++;
106 }
107 }
108 else
109 {
110 mem_copy( out, out - offset, len );
111 out += len;
112 }
113 // printf("o");
114 }
115 //*out++ = ( byte )huf_inbits( &huf );
116 numblock++;
117 }
118 // printf("\n-------------%i\n", size );
119
120 // if ( out - prev >= GU_STEP )
121 // {
122 fgeauser( size - ( end - out ) - lzge->solidoff, lzge->userfunc,
123 lzge->pgeaparam );
124 // }
125 }
126 // fgeauser( GUProcess, size, 0 );
127 // if ( huf.bit ) huf.ptr++;
128 result = huf.ptr - in + 1;//out;
129
130 huf_destroy( &huf );
131
132 return result;
133 }
134
135 //--------------------------------------------------------------------------