lzmat 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +39 -0
- data/ext/extconf.rb +29 -0
- data/ext/lzmat.c +97 -0
- data/ext/lzmat.h +107 -0
- data/ext/lzmat_dec.c +342 -0
- data/ext/lzmat_enc.c +466 -0
- metadata +73 -0
data/README
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= lzmat
|
2
|
+
|
3
|
+
Copyright (c) 2012 Issei Naruta
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
Ruby bindings for LZMAT.
|
8
|
+
|
9
|
+
LZMAT is an fast real-time lossless data compression library.
|
10
|
+
|
11
|
+
== License
|
12
|
+
|
13
|
+
* GPLv2
|
14
|
+
* http://www.gnu.org/licenses/gpl.txt
|
15
|
+
|
16
|
+
== Example
|
17
|
+
|
18
|
+
require "lzmat"
|
19
|
+
|
20
|
+
input = "hogehogemoge"
|
21
|
+
puts "== input =="
|
22
|
+
p input
|
23
|
+
|
24
|
+
enc = Lzmat.encode(input)
|
25
|
+
puts "== encoded =="
|
26
|
+
p enc
|
27
|
+
|
28
|
+
dec = Lzmat.decode(enc)
|
29
|
+
puts "== decoded =="
|
30
|
+
p dec
|
31
|
+
|
32
|
+
== LZMAT
|
33
|
+
|
34
|
+
lzmat contains LZMAT C source.
|
35
|
+
|
36
|
+
* distribution site:
|
37
|
+
* http://www.matcode.com/lzmat.htm
|
38
|
+
* License
|
39
|
+
* GPLv2
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright (C) 2012 Issei Naruta
|
2
|
+
#
|
3
|
+
# This program is free software; you can redistribute it and/or modify
|
4
|
+
# it under the terms of the GNU General Public License as published by
|
5
|
+
# the Free Software Foundation; either version 2 of the License, or
|
6
|
+
# (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This program is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU General Public License
|
14
|
+
# along with this program; if not, write to the Free Software
|
15
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
16
|
+
#
|
17
|
+
# In addition, as a special exception, <name of copyright
|
18
|
+
# holder> gives permission to link the code of this program with
|
19
|
+
# the FOO library (or with modified versions of FOO that use the
|
20
|
+
# same license as FOO), and distribute linked combinations including
|
21
|
+
# the two. You must obey the GNU General Public License in all
|
22
|
+
# respects for all of the code used other than FOO. If you modify
|
23
|
+
# this file, you may extend this exception to your version of the
|
24
|
+
# file, but you are not obligated to do so. If you do not wish to
|
25
|
+
# do so, delete this exception statement from your version.
|
26
|
+
|
27
|
+
require "mkmf"
|
28
|
+
|
29
|
+
create_makefile("lzmat")
|
data/ext/lzmat.c
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (C) 2012 Issei Naruta
|
3
|
+
|
4
|
+
This program is free software; you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program; if not, write to the Free Software
|
16
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
In addition, as a special exception, <name of copyright
|
19
|
+
holder> gives permission to link the code of this program with
|
20
|
+
the FOO library (or with modified versions of FOO that use the
|
21
|
+
same license as FOO), and distribute linked combinations including
|
22
|
+
the two. You must obey the GNU General Public License in all
|
23
|
+
respects for all of the code used other than FOO. If you modify
|
24
|
+
this file, you may extend this exception to your version of the
|
25
|
+
file, but you are not obligated to do so. If you do not wish to
|
26
|
+
do so, delete this exception statement from your version.
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include "ruby.h"
|
30
|
+
#include "lzmat.h"
|
31
|
+
#include <stdlib.h>
|
32
|
+
|
33
|
+
void lzmat_raise_error(int err) {
|
34
|
+
switch (err) {
|
35
|
+
case LZMAT_STATUS_INTEGRITY_FAILURE:
|
36
|
+
rb_raise(rb_eRuntimeError, "lzmat: status integrity failure");
|
37
|
+
break;
|
38
|
+
case LZMAT_STATUS_BUFFER_TOO_SMALL:
|
39
|
+
rb_raise(rb_eRuntimeError, "lzmat: buffer too small");
|
40
|
+
break;
|
41
|
+
default:
|
42
|
+
rb_raise(rb_eRuntimeError, "lzmat: unknown error");
|
43
|
+
break;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
static VALUE rb_lzmat_encode(VALUE self, VALUE str_in) {
|
48
|
+
unsigned char *pbOut, *pbIn;
|
49
|
+
int cbOut, cbIn;
|
50
|
+
int err;
|
51
|
+
VALUE str_out;
|
52
|
+
|
53
|
+
pbIn = StringValuePtr(str_in);
|
54
|
+
cbIn = RSTRING(str_in)->len;
|
55
|
+
cbOut = MAX_LZMAT_ENCODED_SIZE(cbIn);
|
56
|
+
str_out = rb_str_new(0, cbOut + sizeof(int));
|
57
|
+
pbOut = StringValuePtr(str_out);
|
58
|
+
|
59
|
+
err = lzmat_encode(pbOut + sizeof(int), &cbOut, pbIn, cbIn);
|
60
|
+
if (err) {
|
61
|
+
lzmat_raise_error(err);
|
62
|
+
}
|
63
|
+
|
64
|
+
*(int*)pbOut = cbIn;
|
65
|
+
|
66
|
+
rb_str_resize(str_out, cbOut + sizeof(int));
|
67
|
+
|
68
|
+
return str_out;
|
69
|
+
}
|
70
|
+
|
71
|
+
static VALUE rb_lzmat_decode(VALUE self, VALUE str_in) {
|
72
|
+
unsigned char *pbOut, *pbIn;
|
73
|
+
int cbOut, cbIn;
|
74
|
+
int err;
|
75
|
+
VALUE str_out;
|
76
|
+
|
77
|
+
pbIn = StringValuePtr(str_in);
|
78
|
+
cbIn = RSTRING(str_in)->len - sizeof(int);
|
79
|
+
cbOut = *(int*)pbIn;
|
80
|
+
str_out = rb_str_new(0, cbOut);
|
81
|
+
pbOut = StringValuePtr(str_out);
|
82
|
+
|
83
|
+
err = lzmat_decode(pbOut, &cbOut, pbIn + sizeof(int), cbIn);
|
84
|
+
if (err) {
|
85
|
+
lzmat_raise_error(err);
|
86
|
+
}
|
87
|
+
|
88
|
+
return str_out;
|
89
|
+
}
|
90
|
+
|
91
|
+
void Init_lzmat() {
|
92
|
+
VALUE rb_mLzmat;
|
93
|
+
|
94
|
+
rb_mLzmat = rb_define_module("Lzmat");
|
95
|
+
rb_define_module_function(rb_mLzmat, "decode", rb_lzmat_decode, 1);
|
96
|
+
rb_define_module_function(rb_mLzmat, "encode", rb_lzmat_encode, 1);
|
97
|
+
}
|
data/ext/lzmat.h
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
/*
|
2
|
+
**
|
3
|
+
** $Revision: 1.5 $
|
4
|
+
** $Date: 2008/07/19 15:21:55 $
|
5
|
+
**
|
6
|
+
** $Author: Vitaly $
|
7
|
+
***************************************************************************
|
8
|
+
** lzmat.h - header file for the LZMAT real-time data compression library
|
9
|
+
**
|
10
|
+
** This file is part of the LZMAT real-time data compression library.
|
11
|
+
**
|
12
|
+
** LZMAT ANSI-C encoder/decoder 1.01
|
13
|
+
** Copyright (C) 2007,2008 Vitaly Evseenko. All Rights Reserved.
|
14
|
+
**
|
15
|
+
** The LZMAT library is free software; you can redistribute it and/or
|
16
|
+
** modify it under the terms of the GNU General Public License as
|
17
|
+
** published by the Free Software Foundation; either version 2 of
|
18
|
+
** the License, or (at your option) any later version.
|
19
|
+
**
|
20
|
+
** The LZMAT library is distributed WITHOUT ANY WARRANTY;
|
21
|
+
** without even the implied warranty of MERCHANTABILITY or
|
22
|
+
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
23
|
+
** License for more details.
|
24
|
+
**
|
25
|
+
** You should have received a copy of the GNU General Public License
|
26
|
+
** along with the LZMAT library; see the file GPL.TXT.
|
27
|
+
** If not, write to the Free Software Foundation, Inc.,
|
28
|
+
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
29
|
+
**
|
30
|
+
** Vitaly Evseenko
|
31
|
+
** <ve@matcode.com>
|
32
|
+
** http://www.matcode.com/lzmat.htm
|
33
|
+
** lzmat.h
|
34
|
+
***************************************************************************
|
35
|
+
*/
|
36
|
+
|
37
|
+
#ifndef _LZMAT_H
|
38
|
+
#define _LZMAT_H
|
39
|
+
|
40
|
+
#ifdef _WINDOWS
|
41
|
+
typedef unsigned __int64 MP_U64;
|
42
|
+
typedef unsigned long MP_U32;
|
43
|
+
typedef __int64 MP_S64;
|
44
|
+
typedef long MP_S32;
|
45
|
+
#else
|
46
|
+
typedef unsigned long MP_U64;
|
47
|
+
typedef unsigned int MP_U32;
|
48
|
+
typedef long MP_S64;
|
49
|
+
typedef int MP_S32;
|
50
|
+
#endif
|
51
|
+
|
52
|
+
typedef unsigned short MP_U16;
|
53
|
+
typedef unsigned char MP_U8;
|
54
|
+
typedef unsigned short MP_WC;
|
55
|
+
typedef short MP_S16;
|
56
|
+
typedef char MP_S8;
|
57
|
+
#if defined(_WIN64)
|
58
|
+
typedef unsigned __int64 MP_PTR, *PMP_PTR;
|
59
|
+
#else
|
60
|
+
typedef unsigned long MP_PTR, *PMP_PTR;
|
61
|
+
#endif
|
62
|
+
|
63
|
+
#define GET_LE64(_p_) (*((MP_U64 *)(_p_)))
|
64
|
+
#define GET_LE32(_p_) (*((MP_U32 *)(_p_)))
|
65
|
+
#define GET_LE16(_p_) (*((MP_U16 *)(_p_)))
|
66
|
+
|
67
|
+
#define SET_LE64(_p_,_v_) (*((MP_U64 *)(_p_)) = (_v_))
|
68
|
+
#define SET_LE32(_p_,_v_) (*((MP_U32 *)(_p_)) = (_v_))
|
69
|
+
#define SET_LE16(_p_,_v_) (*((MP_U16 *)(_p_)) = (_v_))
|
70
|
+
|
71
|
+
#ifndef LZMAT_CALLCONV
|
72
|
+
#define LZMAT_CALLCONV
|
73
|
+
#endif
|
74
|
+
|
75
|
+
#define MAX_LZMAT_ENCODED_SIZE(_sz_) ((_sz_)+(((_sz_)+7)>>3)+0x21)
|
76
|
+
|
77
|
+
#ifdef __cplusplus
|
78
|
+
extern "C" {
|
79
|
+
#endif
|
80
|
+
|
81
|
+
int LZMAT_CALLCONV lzmat_encode(MP_U8 *pbOut, MP_U32 *pcbOut,
|
82
|
+
MP_U8 *pbIn, MP_U32 cbIn
|
83
|
+
#ifdef LZMAT_SMALL_STACK
|
84
|
+
, void *pVocabularyBuf
|
85
|
+
#endif
|
86
|
+
);
|
87
|
+
|
88
|
+
#ifdef mkstub
|
89
|
+
int __fastcall lzmat_decode(MP_U8 *pbOut, MP_U8 *pbIn, MP_U32 cbIn);
|
90
|
+
#else //!mkstub
|
91
|
+
int LZMAT_CALLCONV lzmat_decode(MP_U8 *pbOut, MP_U32 *pcbOut,
|
92
|
+
MP_U8 *pbIn, MP_U32 cbIn);
|
93
|
+
#endif
|
94
|
+
|
95
|
+
MP_U32 lzmat_dictionary_size(void);
|
96
|
+
|
97
|
+
#ifdef __cplusplus
|
98
|
+
} /* extern "C" */
|
99
|
+
#endif
|
100
|
+
|
101
|
+
// Error codes
|
102
|
+
#define LZMAT_STATUS_OK 0
|
103
|
+
#define LZMAT_STATUS_ERROR (-1)
|
104
|
+
#define LZMAT_STATUS_INTEGRITY_FAILURE 0x100
|
105
|
+
#define LZMAT_STATUS_BUFFER_TOO_SMALL 0x110
|
106
|
+
|
107
|
+
#endif // _LZMAT_H
|
data/ext/lzmat_dec.c
ADDED
@@ -0,0 +1,342 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lzmat_dec.c,v 1.1 2008/07/08 16:58:35 Vitaly Exp $
|
3
|
+
** $Revision: 1.1 $
|
4
|
+
** $Date: 2008/07/08 16:58:35 $
|
5
|
+
**
|
6
|
+
** $Author: Vitaly $
|
7
|
+
**
|
8
|
+
***************************************************************************
|
9
|
+
** LZMAT ANSI-C decoder 1.01
|
10
|
+
** Copyright (C) 2007,2008 Vitaly Evseenko. All Rights Reserved.
|
11
|
+
** lzmat_dec.c
|
12
|
+
**
|
13
|
+
** This file is part of the LZMAT real-time data compression library.
|
14
|
+
**
|
15
|
+
** The LZMAT library is free software; you can redistribute it and/or
|
16
|
+
** modify it under the terms of the GNU General Public License as
|
17
|
+
** published by the Free Software Foundation; either version 2 of
|
18
|
+
** the License, or (at your option) any later version.
|
19
|
+
**
|
20
|
+
** The LZMAT library is distributed WITHOUT ANY WARRANTY;
|
21
|
+
** without even the implied warranty of MERCHANTABILITY or
|
22
|
+
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
23
|
+
** License for more details.
|
24
|
+
**
|
25
|
+
** You should have received a copy of the GNU General Public License
|
26
|
+
** along with the LZMAT library; see the file GPL.TXT.
|
27
|
+
** If not, write to the Free Software Foundation, Inc.,
|
28
|
+
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
29
|
+
**
|
30
|
+
** Vitaly Evseenko
|
31
|
+
** <ve@matcode.com>
|
32
|
+
** http://www.matcode.com/lzmat.htm
|
33
|
+
***************************************************************************
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
#include "lzmat.h"
|
38
|
+
|
39
|
+
#define LZMAT_SAVE
|
40
|
+
|
41
|
+
|
42
|
+
#define LZMAT_DEFAULT_CNT (0x12)
|
43
|
+
#define LZMAT_1BYTE_CNT (0xFF + LZMAT_DEFAULT_CNT)
|
44
|
+
#define LZMAT_2BYTE_CNT (0xFFFF + LZMAT_1BYTE_CNT)
|
45
|
+
#define LZMAT_MAX_2BYTE_CNT (LZMAT_2BYTE_CNT-1)
|
46
|
+
|
47
|
+
#define LZMAT_GET_U4(_p_,_i_,_n_) \
|
48
|
+
((_n_^=1)?(((MP_U8 *)(_p_))[_i_]&0xF):(((MP_U8 *)(_p_))[_i_++]>>4))
|
49
|
+
|
50
|
+
#define LZMAT_GET_U8(_p_,_n_) (MP_U8)(((_n_)?((((MP_U8 *)(_p_))[0]>>4)|(((MP_U8 *)(_p_))[1]<<4)):((MP_U8 *)(_p_))[0]))
|
51
|
+
|
52
|
+
#define LZMAT_GET_LE16(_p_,_n_) \
|
53
|
+
(MP_U16)((_n_)?((((MP_U8 *)(_p_))[0]>>4)|((MP_U16)(GET_LE16((_p_)+1))<<4)):GET_LE16(_p_))
|
54
|
+
|
55
|
+
#define MAX_LZMAT_SHORT_DIST0 0x80
|
56
|
+
#define MAX_LZMAT_SHORT_DIST1 (0x800|MAX_LZMAT_SHORT_DIST0)
|
57
|
+
#define MAX_LZMAT_LONG_DIST0 0x40
|
58
|
+
#define MAX_LZMAT_LONG_DIST1 (0x400|MAX_LZMAT_LONG_DIST0)
|
59
|
+
#define MAX_LZMAT_LONG_DIST2 (0x4000|MAX_LZMAT_LONG_DIST1)
|
60
|
+
#define MAX_LZMAT_LONG_DIST3 (0x40000|MAX_LZMAT_LONG_DIST2)
|
61
|
+
#define MAX_LZMAT_GAMMA_DIST (MAX_LZMAT_LONG_DIST3-1)
|
62
|
+
|
63
|
+
#define LZMAT_DIST_MSK0 0x3F
|
64
|
+
#define LZMAT_DIST_MSK1 0x3FF
|
65
|
+
|
66
|
+
#ifdef LZMAT_SAVE
|
67
|
+
|
68
|
+
int LZMAT_CALLCONV lzmat_decode(MP_U8 *pbOut, MP_U32 *pcbOut,
|
69
|
+
MP_U8 *pbIn, MP_U32 cbIn)
|
70
|
+
{
|
71
|
+
MP_U32 inPos, outPos;
|
72
|
+
MP_U32 cbOutBuf = *pcbOut;
|
73
|
+
MP_U8 cur_nib;
|
74
|
+
*pbOut = *pbIn;
|
75
|
+
for(inPos=1, outPos=1, cur_nib=0; inPos<(cbIn-cur_nib);)
|
76
|
+
{
|
77
|
+
int bc;
|
78
|
+
MP_U8 tag;
|
79
|
+
tag = LZMAT_GET_U8(pbIn+inPos,cur_nib);
|
80
|
+
inPos++;
|
81
|
+
for(bc=0; bc<8 && inPos<(cbIn-cur_nib) && outPos<cbOutBuf; bc++, tag<<=1)
|
82
|
+
{
|
83
|
+
if(tag&0x80) // gamma
|
84
|
+
{
|
85
|
+
MP_U32 r_pos, r_cnt, dist;
|
86
|
+
#define cflag r_cnt
|
87
|
+
cflag = LZMAT_GET_LE16(pbIn+inPos,cur_nib);
|
88
|
+
inPos++;
|
89
|
+
if(outPos>MAX_LZMAT_SHORT_DIST1)
|
90
|
+
{
|
91
|
+
dist = cflag>>2;
|
92
|
+
switch(cflag&3)
|
93
|
+
{
|
94
|
+
case 0:
|
95
|
+
dist=(dist&LZMAT_DIST_MSK0)+1;
|
96
|
+
break;
|
97
|
+
case 1:
|
98
|
+
inPos+=cur_nib;
|
99
|
+
dist = (dist&LZMAT_DIST_MSK1)+0x41;
|
100
|
+
cur_nib^=1;
|
101
|
+
break;
|
102
|
+
case 2:
|
103
|
+
inPos++;
|
104
|
+
dist += 0x441;
|
105
|
+
break;
|
106
|
+
case 3:
|
107
|
+
if((inPos+2+cur_nib)>cbIn)
|
108
|
+
return LZMAT_STATUS_INTEGRITY_FAILURE+1;
|
109
|
+
inPos++;
|
110
|
+
dist = (dist +
|
111
|
+
((MP_U32)LZMAT_GET_U4(pbIn,inPos,cur_nib)<<14))
|
112
|
+
+0x4441;
|
113
|
+
break;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
else
|
117
|
+
{
|
118
|
+
dist = cflag>>1;
|
119
|
+
if(cflag&1)
|
120
|
+
{
|
121
|
+
inPos+=cur_nib;
|
122
|
+
dist = (dist&0x7FF)+0x81;
|
123
|
+
cur_nib^=1;
|
124
|
+
}
|
125
|
+
else
|
126
|
+
dist = (dist&0x7F)+1;
|
127
|
+
}
|
128
|
+
#undef cflag
|
129
|
+
r_cnt = LZMAT_GET_U4(pbIn,inPos,cur_nib);
|
130
|
+
if(r_cnt!=0xF)
|
131
|
+
{
|
132
|
+
r_cnt += 3;
|
133
|
+
}
|
134
|
+
else
|
135
|
+
{
|
136
|
+
if((inPos+1+cur_nib)>cbIn)
|
137
|
+
return LZMAT_STATUS_INTEGRITY_FAILURE+2;
|
138
|
+
r_cnt = LZMAT_GET_U8(pbIn+inPos,cur_nib);
|
139
|
+
inPos++;
|
140
|
+
if(r_cnt!=0xFF)
|
141
|
+
{
|
142
|
+
r_cnt += LZMAT_DEFAULT_CNT;
|
143
|
+
}
|
144
|
+
else
|
145
|
+
{
|
146
|
+
if((inPos+2+cur_nib)>cbIn)
|
147
|
+
return LZMAT_STATUS_INTEGRITY_FAILURE+3;
|
148
|
+
r_cnt = LZMAT_GET_LE16(pbIn+inPos,cur_nib)+LZMAT_1BYTE_CNT;
|
149
|
+
inPos+=2;
|
150
|
+
if(r_cnt==LZMAT_2BYTE_CNT)
|
151
|
+
{
|
152
|
+
// copy chunk
|
153
|
+
if(cur_nib)
|
154
|
+
{
|
155
|
+
r_cnt = ((MP_U32)pbIn[inPos-4]&0xFC)<<5;
|
156
|
+
inPos++;
|
157
|
+
cur_nib = 0;
|
158
|
+
}
|
159
|
+
else
|
160
|
+
{
|
161
|
+
r_cnt = (GET_LE16(pbIn+inPos-5)&0xFC0)<<1;
|
162
|
+
}
|
163
|
+
r_cnt+=(tag&0x7F)+4;
|
164
|
+
r_cnt<<=1;
|
165
|
+
if((outPos+(r_cnt<<2))>cbOutBuf)
|
166
|
+
return LZMAT_STATUS_BUFFER_TOO_SMALL;
|
167
|
+
while(r_cnt-- && outPos<cbOutBuf)
|
168
|
+
{
|
169
|
+
*(MP_U32 *)(pbOut+outPos)=*(MP_U32 *)(pbIn+inPos);
|
170
|
+
inPos+=4;
|
171
|
+
outPos+=4;
|
172
|
+
}
|
173
|
+
break;
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
if(outPos<dist)
|
178
|
+
return LZMAT_STATUS_INTEGRITY_FAILURE+4;
|
179
|
+
if((outPos+r_cnt)>cbOutBuf)
|
180
|
+
return LZMAT_STATUS_BUFFER_TOO_SMALL+1;
|
181
|
+
r_pos = outPos-dist;
|
182
|
+
while(r_cnt-- && outPos<cbOutBuf)
|
183
|
+
pbOut[outPos++]=pbOut[r_pos++];
|
184
|
+
}
|
185
|
+
else
|
186
|
+
{
|
187
|
+
pbOut[outPos++]=LZMAT_GET_U8(pbIn+inPos,cur_nib);
|
188
|
+
inPos++;
|
189
|
+
}
|
190
|
+
}
|
191
|
+
}
|
192
|
+
*pcbOut = outPos;
|
193
|
+
return LZMAT_STATUS_OK;
|
194
|
+
}
|
195
|
+
|
196
|
+
#else //!LZMAT_SAVE
|
197
|
+
|
198
|
+
|
199
|
+
#ifdef mkstub
|
200
|
+
int __fastcall lzmat_decode(MP_U8 *pbOut, MP_U8 *pbIn, MP_U32 cbIn)
|
201
|
+
#else //!mkstub
|
202
|
+
int LZMAT_CALLCONV lzmat_decode(MP_U8 *pbOut, MP_U32 *pcbOut,
|
203
|
+
MP_U8 *pbIn, MP_U32 cbIn)
|
204
|
+
#endif
|
205
|
+
{
|
206
|
+
MP_U32 inPos, outPos;
|
207
|
+
MP_U8 cur_nib;
|
208
|
+
#ifndef mkstub
|
209
|
+
MP_U32 cbOutBuf = *pcbOut;
|
210
|
+
#endif
|
211
|
+
*pbOut = *pbIn;
|
212
|
+
for(inPos=1, outPos=1, cur_nib=0; inPos<(cbIn-cur_nib);)
|
213
|
+
{
|
214
|
+
int bc;
|
215
|
+
MP_U8 tag;
|
216
|
+
tag = LZMAT_GET_U8(pbIn+inPos,cur_nib);
|
217
|
+
inPos++;
|
218
|
+
#ifdef mkstub
|
219
|
+
for(bc=0; bc<8 && inPos<(cbIn-cur_nib); bc++, tag<<=1)
|
220
|
+
#else //!mkstub
|
221
|
+
for(bc=0; bc<8 && inPos<(cbIn-cur_nib) && outPos<cbOutBuf; bc++, tag<<=1)
|
222
|
+
#endif
|
223
|
+
{
|
224
|
+
if(tag&0x80) // gamma
|
225
|
+
{
|
226
|
+
MP_U32 r_pos, r_cnt, dist;
|
227
|
+
#define cflag r_cnt
|
228
|
+
cflag = LZMAT_GET_LE20_UNSAVE(pbIn+inPos,cur_nib);
|
229
|
+
inPos++;
|
230
|
+
if(outPos<0x881)
|
231
|
+
{
|
232
|
+
dist = cflag>>1;
|
233
|
+
if(cflag&1)
|
234
|
+
{
|
235
|
+
inPos+=cur_nib;
|
236
|
+
dist = (dist&0x7FF)+0x81;
|
237
|
+
cur_nib^=1;
|
238
|
+
}
|
239
|
+
else
|
240
|
+
dist = (dist&0x7F)+1;
|
241
|
+
}
|
242
|
+
else
|
243
|
+
{
|
244
|
+
dist = cflag>>2;
|
245
|
+
switch(cflag&3)
|
246
|
+
{
|
247
|
+
case 0:
|
248
|
+
dist=(dist&0x3F)+1;
|
249
|
+
break;
|
250
|
+
case 1:
|
251
|
+
inPos+=cur_nib;
|
252
|
+
dist = (dist&0x3FF)+0x41;
|
253
|
+
cur_nib^=1;
|
254
|
+
break;
|
255
|
+
case 2:
|
256
|
+
dist = (dist&0x3FFF)+0x441;
|
257
|
+
inPos++;
|
258
|
+
break;
|
259
|
+
case 3:
|
260
|
+
inPos+=(1+cur_nib);
|
261
|
+
dist = (dist&0x3FFFF)+0x4441;
|
262
|
+
cur_nib^=1;
|
263
|
+
break;
|
264
|
+
}
|
265
|
+
}
|
266
|
+
#undef cflag
|
267
|
+
r_cnt = LZMAT_GET_LE12_UNSAVE(pbIn+inPos,cur_nib);
|
268
|
+
inPos+=cur_nib;
|
269
|
+
cur_nib^=1;
|
270
|
+
if((r_cnt&0xF)!=0xF)
|
271
|
+
{
|
272
|
+
r_cnt = (r_cnt&0xF)+3;
|
273
|
+
}
|
274
|
+
else
|
275
|
+
{
|
276
|
+
inPos++;
|
277
|
+
if(r_cnt!=0xFFF)
|
278
|
+
{
|
279
|
+
//r_cnt = LZMAT_GET_U8(pbIn+inPos-1,cur_nib)+0x12;
|
280
|
+
r_cnt=(r_cnt>>4)+0x12;
|
281
|
+
}
|
282
|
+
else
|
283
|
+
{
|
284
|
+
r_cnt = LZMAT_GET_LE16_UNSAVE(pbIn+inPos,cur_nib)+0x111;
|
285
|
+
inPos+=2;
|
286
|
+
if(r_cnt==LZMAT_2BYTE_CNT)
|
287
|
+
{
|
288
|
+
// copy chunk
|
289
|
+
if(cur_nib)
|
290
|
+
{
|
291
|
+
r_cnt = ((MP_U32)pbIn[inPos-4]&0xFC)<<5;
|
292
|
+
inPos++;
|
293
|
+
cur_nib = 0;
|
294
|
+
}
|
295
|
+
else
|
296
|
+
{
|
297
|
+
r_cnt = (GET_LE16(pbIn+inPos-5)&0xFC0)<<1;
|
298
|
+
//((MP_U32)(pbIn[inPos-5]&0xC0)+(pbIn[inPos-4]<<4))<<1;
|
299
|
+
}
|
300
|
+
r_cnt+=(tag&0x7F)+4;
|
301
|
+
r_cnt<<=1;
|
302
|
+
#ifdef mkstub
|
303
|
+
while(r_cnt--)
|
304
|
+
#else //!mkstub
|
305
|
+
while(r_cnt-- && outPos<cbOutBuf)
|
306
|
+
#endif
|
307
|
+
{
|
308
|
+
*(MP_U32 *)(pbOut+outPos)=*(MP_U32 *)(pbIn+inPos);
|
309
|
+
inPos+=4;
|
310
|
+
outPos+=4;
|
311
|
+
}
|
312
|
+
break;
|
313
|
+
}
|
314
|
+
}
|
315
|
+
}
|
316
|
+
r_pos = outPos-dist;
|
317
|
+
#ifdef mkstub
|
318
|
+
while(r_cnt--)
|
319
|
+
#else //!mkstub
|
320
|
+
while(r_cnt-- && outPos<cbOutBuf)
|
321
|
+
#endif
|
322
|
+
pbOut[outPos++]=pbOut[r_pos++];
|
323
|
+
}
|
324
|
+
else
|
325
|
+
{
|
326
|
+
pbOut[outPos++]=LZMAT_GET_U8(pbIn+inPos,cur_nib);
|
327
|
+
inPos++;
|
328
|
+
}
|
329
|
+
}
|
330
|
+
}
|
331
|
+
#ifdef mkstub
|
332
|
+
return outPos;
|
333
|
+
#else //!mkstub
|
334
|
+
if(inPos<(cbIn-cur_nib))
|
335
|
+
return -1;
|
336
|
+
*pcbOut = outPos;
|
337
|
+
return LZMAT_STATUS_OK;
|
338
|
+
#endif
|
339
|
+
}
|
340
|
+
|
341
|
+
#endif
|
342
|
+
|
data/ext/lzmat_enc.c
ADDED
@@ -0,0 +1,466 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lzmat_enc.c,v 1.1 2008/07/08 16:58:35 Vitaly Exp $
|
3
|
+
** $Revision: 1.1 $
|
4
|
+
** $Date: 2008/07/08 16:58:35 $
|
5
|
+
**
|
6
|
+
** $Author: Vitaly $
|
7
|
+
**
|
8
|
+
***************************************************************************
|
9
|
+
** LZMAT ANSI-C encoder 1.01
|
10
|
+
** Copyright (C) 2007,2008 Vitaly Evseenko. All Rights Reserved.
|
11
|
+
** lzmat_enc.c
|
12
|
+
**
|
13
|
+
** This file is part of the LZMAT real-time data compression library.
|
14
|
+
**
|
15
|
+
** The LZMAT library is free software; you can redistribute it and/or
|
16
|
+
** modify it under the terms of the GNU General Public License as
|
17
|
+
** published by the Free Software Foundation; either version 2 of
|
18
|
+
** the License, or (at your option) any later version.
|
19
|
+
**
|
20
|
+
** The LZMAT library is distributed WITHOUT ANY WARRANTY;
|
21
|
+
** without even the implied warranty of MERCHANTABILITY or
|
22
|
+
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
23
|
+
** License for more details.
|
24
|
+
**
|
25
|
+
** You should have received a copy of the GNU General Public License
|
26
|
+
** along with the LZMAT library; see the file GPL.TXT.
|
27
|
+
** If not, write to the Free Software Foundation, Inc.,
|
28
|
+
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
29
|
+
**
|
30
|
+
** Vitaly Evseenko
|
31
|
+
** <ve@matcode.com>
|
32
|
+
** http://www.matcode.com/lzmat.htm
|
33
|
+
***************************************************************************
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
#include "lzmat.h"
|
38
|
+
#include <memory.h>
|
39
|
+
|
40
|
+
|
41
|
+
#define CB_LZMAT_IDX 0x20000
|
42
|
+
#define CB_LZMAT_DICT 0x20000
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
#ifdef LZMAT_SMALL_STACK
|
47
|
+
#define LZMAT_DICTIONATY_SIZE ((CB_LZMAT_IDX+CB_LZMAT_DICT))
|
48
|
+
MP_U32 lzmat_dictionary_size(void) { return LZMAT_DICTIONATY_SIZE; }
|
49
|
+
#endif
|
50
|
+
|
51
|
+
|
52
|
+
#define RS_HASH_BITS 9
|
53
|
+
|
54
|
+
|
55
|
+
#define MAX_LZMAT_DICT (CB_LZMAT_DICT/sizeof(MP_U32))
|
56
|
+
#define MAX_LZMAT_IDX (CB_LZMAT_IDX/sizeof(MP_U32))
|
57
|
+
|
58
|
+
|
59
|
+
#define DICT_MSK (MAX_LZMAT_DICT-1)
|
60
|
+
#define IDX_MSK (MAX_LZMAT_IDX-1)
|
61
|
+
|
62
|
+
#define LZMAT_HASH(p) \
|
63
|
+
((*(MP_U16 *)(p)+(MP_U16)((*(MP_U32 *)(p))>>RS_HASH_BITS))&DICT_MSK)
|
64
|
+
|
65
|
+
typedef struct _LZMAT_HASH_CTL {
|
66
|
+
MP_S32 ptr[MAX_LZMAT_DICT];
|
67
|
+
MP_S32 idx[MAX_LZMAT_IDX];
|
68
|
+
} LZMAT_HASH_CTL, *PLZMAT_HASH_CTL;
|
69
|
+
|
70
|
+
#define LZMAT_DEFAULT_CNT (0x12)
|
71
|
+
#define LZMAT_1BYTE_CNT (0xFF + LZMAT_DEFAULT_CNT)
|
72
|
+
#define LZMAT_2BYTE_CNT (0xFFFF + LZMAT_1BYTE_CNT)
|
73
|
+
#define LZMAT_MAX_2BYTE_CNT (LZMAT_2BYTE_CNT-1)
|
74
|
+
|
75
|
+
#define MAX_LZMAT_SHORT_DIST0 0x80
|
76
|
+
#define MAX_LZMAT_SHORT_DIST1 (0x800|MAX_LZMAT_SHORT_DIST0)
|
77
|
+
#define MAX_LZMAT_LONG_DIST0 0x40
|
78
|
+
#define MAX_LZMAT_LONG_DIST1 (0x400|MAX_LZMAT_LONG_DIST0)
|
79
|
+
#define MAX_LZMAT_LONG_DIST2 (0x4000|MAX_LZMAT_LONG_DIST1)
|
80
|
+
#define MAX_LZMAT_LONG_DIST3 (0x40000|MAX_LZMAT_LONG_DIST2)
|
81
|
+
#define MAX_LZMAT_GAMMA_DIST (MAX_LZMAT_LONG_DIST3-1)
|
82
|
+
|
83
|
+
#ifdef LZMAT_SMALL_STACK
|
84
|
+
#define LZMATHDICT (((PLZMAT_HASH_CTL)pVocabularyBuf)[0])
|
85
|
+
#else
|
86
|
+
#define LZMATHDICT (lzh)
|
87
|
+
#endif
|
88
|
+
|
89
|
+
|
90
|
+
#define MAX_LZMAT_UNCOMP_BLOCK 0x280
|
91
|
+
|
92
|
+
#define MP_CMP_DISTANCE(_ptr_,_d1_,_d2_,_c1_,_c2_) \
|
93
|
+
(_c2_>_c1_ && (_c2_>(_c1_+1) || (_ptr_>0x880 && (_d1_<<7)>_d2_) || ((_d1_<<3)>_d2_)))
|
94
|
+
|
95
|
+
|
96
|
+
#define LZMAT_GET_U4(_p_,_i_,_n_) \
|
97
|
+
((_n_^=1)?(((MP_U8 *)(_p_))[_i_]&0xF):(((MP_U8 *)(_p_))[_i_++]>>4))
|
98
|
+
|
99
|
+
#define LZMAT_GET_U8(_p_,_n_) (MP_U8)(((_n_)?((((MP_U8 *)(_p_))[0]>>4)|(((MP_U8 *)(_p_))[1]<<4)):((MP_U8 *)(_p_))[0]))
|
100
|
+
|
101
|
+
#define LZMAT_GET_LE12(_p_,_n_) \
|
102
|
+
((_n_^=1)?((((MP_U16)((MP_U8 *)(_p_))[1]&0xF)<<8)|((MP_U8 *)(_p_))[0]):((((MP_U8 *)(_p_))[0]>>4)|((MP_U16)((MP_U8 *)(_p_))[1]<<4)))
|
103
|
+
|
104
|
+
#define LZMAT_GET_LE16(_p_,_n_) \
|
105
|
+
(MP_U16)((_n_)?((((MP_U8 *)(_p_))[0]>>4)|((MP_U16)(GET_LE16((_p_)+1))<<4)):GET_LE16(_p_))
|
106
|
+
|
107
|
+
|
108
|
+
#define LZMAT_SET_U4(_p_,_n_,_v_) { \
|
109
|
+
if(_n_^=1) *(_p_) = (MP_U8)((_v_)&0xF); \
|
110
|
+
else *(_p_)++ |= (MP_U8)(_v_<<4); }
|
111
|
+
|
112
|
+
#define LZMAT_SET_U8(_p_,_n_,_v_) { \
|
113
|
+
if(_n_) { \
|
114
|
+
*(_p_)++ |= (MP_U8)(_v_<<4); \
|
115
|
+
*(_p_) = (MP_U8)((_v_)>>4); \
|
116
|
+
} else \
|
117
|
+
*(_p_)++ = (MP_U8)(_v_); \
|
118
|
+
}
|
119
|
+
|
120
|
+
#define LZMAT_SET_LE12(_p_,_n_,_v_) { \
|
121
|
+
if(_n_^=1) { \
|
122
|
+
*(_p_)++ = (MP_U8)(_v_); \
|
123
|
+
*(_p_) = (MP_U8)((_v_)>>8); \
|
124
|
+
} else { \
|
125
|
+
*(_p_)++ |= (MP_U8)(_v_<<4); \
|
126
|
+
*(_p_)++ = (MP_U8)((_v_)>>4); \
|
127
|
+
} }
|
128
|
+
|
129
|
+
#define LZMAT_SET_LE16(_p_,_n_,_v_) { \
|
130
|
+
if(_n_) { \
|
131
|
+
*(_p_)++ |= (MP_U8)(_v_<<4); \
|
132
|
+
*(_p_)++ = (MP_U8)((_v_)>>4); \
|
133
|
+
*(_p_) = (MP_U8)((_v_)>>12); \
|
134
|
+
} else { \
|
135
|
+
SET_LE16(_p_,(MP_U16)(_v_)); (_p_)+=2; \
|
136
|
+
} }
|
137
|
+
|
138
|
+
#define LZMAT_SET_LE20(_p_,_n_,_v_) { \
|
139
|
+
if(_n_^=1) { \
|
140
|
+
SET_LE16(_p_,(MP_U16)(_v_)); (_p_)+=2; \
|
141
|
+
*(_p_) = (MP_U8)((_v_)>>16); \
|
142
|
+
} else { \
|
143
|
+
*(_p_)++ |= (MP_U8)((_v_)<<4); \
|
144
|
+
SET_LE16(_p_,((MP_U16)((_v_)>>4))); (_p_)+=2; \
|
145
|
+
} }
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
#define LZMAT_GET_LE12_UNSAVE(_p_,_n_) \
|
151
|
+
((_n_)?((GET_LE16(_p_)>>4)&0xFFF):(GET_LE16(_p_)&0xFFF))
|
152
|
+
|
153
|
+
#define LZMAT_GET_LE16_UNSAVE(_p_,_n_) \
|
154
|
+
((_n_)?((GET_LE32(_p_)>>4)&0xFFFF):GET_LE16(_p_))
|
155
|
+
|
156
|
+
#define LZMAT_GET_LE20_UNSAVE(_p_,_n_) \
|
157
|
+
((_n_)?((GET_LE32(_p_)>>4)&0xFFFFF):(GET_LE32(_p_)&0xFFFFF))
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
int LZMAT_CALLCONV lzmat_encode(MP_U8 *pbOut, MP_U32 *pcbOut,
|
165
|
+
MP_U8 *pbIn, MP_U32 cbIn
|
166
|
+
#ifdef LZMAT_SMALL_STACK
|
167
|
+
, void *pVocabularyBuf
|
168
|
+
#endif
|
169
|
+
)
|
170
|
+
{
|
171
|
+
MP_U32 i, match_cnt, inPtr, cpy_tag, cbUCData;
|
172
|
+
MP_U8 *pOut, *pTag, *pEndOut, *pInp;
|
173
|
+
MP_U32 Gamma_dist;
|
174
|
+
MP_U8 bit_msk, ThisTag, cur_nib, tag_nib, uc_nib;
|
175
|
+
MP_U8 *pUC_Tag;
|
176
|
+
MP_U32 processed_data;
|
177
|
+
|
178
|
+
#ifndef LZMAT_SMALL_STACK
|
179
|
+
LZMAT_HASH_CTL lzh;
|
180
|
+
#endif
|
181
|
+
|
182
|
+
uc_nib = cur_nib = tag_nib = 0;
|
183
|
+
memset(&LZMATHDICT, -1, sizeof(LZMAT_HASH_CTL));
|
184
|
+
pTag = pbOut+1;
|
185
|
+
pUC_Tag = pTag;
|
186
|
+
|
187
|
+
Gamma_dist = 0;
|
188
|
+
bit_msk = 0x80;
|
189
|
+
ThisTag = 0x00;
|
190
|
+
|
191
|
+
pEndOut = pbOut + *pcbOut - 0x21;
|
192
|
+
|
193
|
+
pOut = pTag+1;
|
194
|
+
*pbOut = *pbIn;
|
195
|
+
|
196
|
+
cpy_tag = 0;
|
197
|
+
|
198
|
+
cbUCData = 0;
|
199
|
+
processed_data = inPtr = 1;
|
200
|
+
|
201
|
+
LZMATHDICT.ptr[LZMAT_HASH(pbIn)] = 0;
|
202
|
+
|
203
|
+
while(cbIn > inPtr)
|
204
|
+
{
|
205
|
+
MP_U8 *pITmp;
|
206
|
+
MP_U32 store_dist;
|
207
|
+
MP_U16 hash_Idx;
|
208
|
+
|
209
|
+
pInp = pbIn + inPtr;
|
210
|
+
pITmp = pInp - 1;
|
211
|
+
if( (cbIn-inPtr)>=4 && *(MP_U32 *)pInp == *(MP_U32 *)pITmp )
|
212
|
+
{
|
213
|
+
MP_U32 in_Reminder = cbIn-4-inPtr;
|
214
|
+
MP_U8 *pCurPtr = pInp+4;
|
215
|
+
Gamma_dist = 0;
|
216
|
+
pITmp+=4;
|
217
|
+
match_cnt = 4;
|
218
|
+
if(in_Reminder>(LZMAT_MAX_2BYTE_CNT-4))
|
219
|
+
in_Reminder = (LZMAT_MAX_2BYTE_CNT-4);
|
220
|
+
while(in_Reminder-- && *pCurPtr++ == *pITmp++)
|
221
|
+
match_cnt++;
|
222
|
+
}
|
223
|
+
else
|
224
|
+
{
|
225
|
+
MP_S32 dict_ptr, cur_idx, start_pos;
|
226
|
+
MP_U16 cmp_val;
|
227
|
+
match_cnt = 1;
|
228
|
+
if ( (unsigned int)inPtr < MAX_LZMAT_GAMMA_DIST)
|
229
|
+
start_pos = 0;
|
230
|
+
else
|
231
|
+
start_pos = inPtr - MAX_LZMAT_GAMMA_DIST;
|
232
|
+
dict_ptr = LZMATHDICT.ptr[LZMAT_HASH(pInp)];
|
233
|
+
if ( dict_ptr < start_pos)
|
234
|
+
goto skip_search;
|
235
|
+
cmp_val = *(MP_U16 *)(pbIn + inPtr + 1);
|
236
|
+
pITmp = pbIn + 1;
|
237
|
+
cur_idx = inPtr;
|
238
|
+
while ( dict_ptr < cur_idx )
|
239
|
+
{
|
240
|
+
cur_idx = dict_ptr;
|
241
|
+
if ( *(MP_U16 *)(pITmp + dict_ptr) == cmp_val )
|
242
|
+
{
|
243
|
+
MP_U32 in_Reminder, new_dist, match_found=0;
|
244
|
+
MP_U8 *pIdxPtr, *pCurPtr;
|
245
|
+
pIdxPtr = pbIn + dict_ptr; // + sizeof(MP_U16);
|
246
|
+
pCurPtr = pInp; // + sizeof(MP_U16);
|
247
|
+
in_Reminder = cbIn-inPtr;
|
248
|
+
if(in_Reminder>LZMAT_MAX_2BYTE_CNT)
|
249
|
+
in_Reminder = LZMAT_MAX_2BYTE_CNT;
|
250
|
+
while(in_Reminder-- && *pIdxPtr++ == *pCurPtr++)
|
251
|
+
match_found++;
|
252
|
+
new_dist = inPtr - dict_ptr - 1;
|
253
|
+
if(MP_CMP_DISTANCE(inPtr,Gamma_dist,new_dist,match_cnt,match_found))
|
254
|
+
{
|
255
|
+
Gamma_dist = new_dist;
|
256
|
+
match_cnt = match_found;
|
257
|
+
cmp_val = *(MP_U16 *)(match_found + pInp - 1);
|
258
|
+
pITmp = pbIn + match_found - 1;
|
259
|
+
if (match_found >= LZMAT_MAX_2BYTE_CNT)
|
260
|
+
{
|
261
|
+
match_found = LZMAT_MAX_2BYTE_CNT;
|
262
|
+
break;
|
263
|
+
}
|
264
|
+
}
|
265
|
+
}
|
266
|
+
dict_ptr = LZMATHDICT.idx[dict_ptr & IDX_MSK];
|
267
|
+
if (dict_ptr < start_pos)
|
268
|
+
break;
|
269
|
+
}
|
270
|
+
}
|
271
|
+
skip_search:
|
272
|
+
if(match_cnt > (cbIn-inPtr))
|
273
|
+
match_cnt = cbIn-inPtr;
|
274
|
+
|
275
|
+
if(match_cnt<3)
|
276
|
+
{
|
277
|
+
match_cnt = 1;
|
278
|
+
LZMAT_SET_U8(pOut, cur_nib, pInp[0]);
|
279
|
+
goto set_next_tag;
|
280
|
+
}
|
281
|
+
|
282
|
+
if(inPtr>MAX_LZMAT_SHORT_DIST1)
|
283
|
+
{
|
284
|
+
store_dist = Gamma_dist<<2;
|
285
|
+
if(Gamma_dist<MAX_LZMAT_LONG_DIST0)
|
286
|
+
{
|
287
|
+
LZMAT_SET_U8(pOut,cur_nib,store_dist);
|
288
|
+
}
|
289
|
+
else if(Gamma_dist<MAX_LZMAT_LONG_DIST1)
|
290
|
+
{
|
291
|
+
store_dist-=(MAX_LZMAT_LONG_DIST0<<2);
|
292
|
+
store_dist|= 1;
|
293
|
+
LZMAT_SET_LE12(pOut,cur_nib,store_dist);
|
294
|
+
}
|
295
|
+
else if(Gamma_dist<MAX_LZMAT_LONG_DIST2)
|
296
|
+
{
|
297
|
+
store_dist-=(MAX_LZMAT_LONG_DIST1<<2);
|
298
|
+
store_dist|= 2;
|
299
|
+
LZMAT_SET_LE16(pOut,cur_nib,store_dist);
|
300
|
+
}
|
301
|
+
else
|
302
|
+
{
|
303
|
+
if(match_cnt<4)
|
304
|
+
{
|
305
|
+
match_cnt = 1;
|
306
|
+
LZMAT_SET_U8(pOut, cur_nib, pInp[0]);
|
307
|
+
goto set_next_tag;
|
308
|
+
}
|
309
|
+
store_dist-=(MAX_LZMAT_LONG_DIST2<<2);
|
310
|
+
store_dist|= 3;
|
311
|
+
LZMAT_SET_LE20(pOut,cur_nib,store_dist);
|
312
|
+
}
|
313
|
+
}
|
314
|
+
else // short distance
|
315
|
+
{
|
316
|
+
store_dist = Gamma_dist<<1;
|
317
|
+
if(Gamma_dist>=MAX_LZMAT_SHORT_DIST0)
|
318
|
+
{
|
319
|
+
store_dist-=(MAX_LZMAT_SHORT_DIST0<<1);
|
320
|
+
store_dist|=1;
|
321
|
+
LZMAT_SET_LE12(pOut,cur_nib,store_dist);
|
322
|
+
}
|
323
|
+
else
|
324
|
+
LZMAT_SET_U8(pOut,cur_nib,store_dist);
|
325
|
+
}
|
326
|
+
#define Stored_Cnt Gamma_dist
|
327
|
+
if(match_cnt<LZMAT_DEFAULT_CNT)
|
328
|
+
{
|
329
|
+
Stored_Cnt = match_cnt-3;
|
330
|
+
LZMAT_SET_U4(pOut,cur_nib, Stored_Cnt);
|
331
|
+
}
|
332
|
+
else if(match_cnt<LZMAT_1BYTE_CNT)
|
333
|
+
{
|
334
|
+
Stored_Cnt = ((match_cnt-0x12)<<4)|0xF;
|
335
|
+
LZMAT_SET_LE12(pOut,cur_nib,Stored_Cnt);
|
336
|
+
}
|
337
|
+
else
|
338
|
+
{
|
339
|
+
LZMAT_SET_LE12(pOut,cur_nib, 0xFFF);
|
340
|
+
Stored_Cnt = match_cnt-0x111;
|
341
|
+
LZMAT_SET_LE16(pOut,cur_nib, Stored_Cnt);
|
342
|
+
}
|
343
|
+
|
344
|
+
#undef Stored_Cnt
|
345
|
+
|
346
|
+
#define cbCompressed Gamma_dist
|
347
|
+
ThisTag |= bit_msk;
|
348
|
+
set_next_tag:
|
349
|
+
bit_msk >>= 1;
|
350
|
+
if ( bit_msk == 0 )
|
351
|
+
{
|
352
|
+
if(cpy_tag && cbUCData>0xFFF8)
|
353
|
+
{
|
354
|
+
MP_U32 *pdwIn, *pdwOut;
|
355
|
+
MP_U32 cbCopy;
|
356
|
+
copy_uncmp:
|
357
|
+
cbCopy = (MP_U16)(cbUCData>>3);
|
358
|
+
cbCompressed = cbCopy-4;
|
359
|
+
cbCompressed = (cbCompressed&0xFF)|0x80|((cbCompressed<<3)&0xFC00);
|
360
|
+
pUC_Tag[0]&=0xF; // !!! zero upper nibble in case cur_nib
|
361
|
+
LZMAT_SET_LE16(pUC_Tag,uc_nib, cbCompressed);
|
362
|
+
if(uc_nib)
|
363
|
+
{
|
364
|
+
LZMAT_SET_LE12(pUC_Tag, uc_nib, 0xFFF);
|
365
|
+
}
|
366
|
+
else
|
367
|
+
{
|
368
|
+
LZMAT_SET_LE16(pUC_Tag, uc_nib, 0xFFFF);
|
369
|
+
}
|
370
|
+
LZMAT_SET_LE16(pUC_Tag, uc_nib, 0xFFFF);
|
371
|
+
pdwOut = (MP_U32 *)pUC_Tag;
|
372
|
+
cpy_tag = cbCopy<<1;
|
373
|
+
pdwIn = (MP_U32 *)(pbIn + processed_data);
|
374
|
+
inPtr = processed_data+(cpy_tag<<2);
|
375
|
+
while(cpy_tag--)
|
376
|
+
*pdwOut++ = *pdwIn++;
|
377
|
+
pUC_Tag = pTag = (MP_U8 *)pdwOut;
|
378
|
+
pOut = pTag+1;
|
379
|
+
match_cnt = 1;
|
380
|
+
cpy_tag = 0;
|
381
|
+
tag_nib = cur_nib = 0;
|
382
|
+
processed_data = inPtr;
|
383
|
+
bit_msk = 0x80;
|
384
|
+
ThisTag = 0;
|
385
|
+
pInp = pbIn + inPtr;
|
386
|
+
cbUCData = 0;
|
387
|
+
continue;
|
388
|
+
}
|
389
|
+
i=inPtr-(processed_data+match_cnt);
|
390
|
+
cbCompressed = (MP_U32)(pOut-pUC_Tag);
|
391
|
+
if(ThisTag)
|
392
|
+
{
|
393
|
+
if(cpy_tag>0xff)
|
394
|
+
goto copy_uncmp;
|
395
|
+
else if(cbCompressed<i)
|
396
|
+
{
|
397
|
+
if(cpy_tag>0x3F)
|
398
|
+
goto copy_uncmp;
|
399
|
+
cpy_tag = 0;
|
400
|
+
pUC_Tag = pOut;
|
401
|
+
uc_nib = cur_nib;
|
402
|
+
processed_data = inPtr+match_cnt;
|
403
|
+
}
|
404
|
+
}
|
405
|
+
else
|
406
|
+
{
|
407
|
+
if(cpy_tag || (i+4)<cbCompressed)
|
408
|
+
{
|
409
|
+
cbUCData = inPtr - processed_data;
|
410
|
+
cpy_tag++;
|
411
|
+
}
|
412
|
+
}
|
413
|
+
if(tag_nib&1)
|
414
|
+
{
|
415
|
+
*pTag++ |= (ThisTag<<4);
|
416
|
+
pTag[0] |= (ThisTag>>4);
|
417
|
+
}
|
418
|
+
else
|
419
|
+
*pTag = ThisTag;
|
420
|
+
bit_msk = 0x80;
|
421
|
+
ThisTag = 0;
|
422
|
+
pTag = pOut++;
|
423
|
+
if(cur_nib)
|
424
|
+
*pOut=0;
|
425
|
+
tag_nib = cur_nib;
|
426
|
+
if ( (MP_U32)pOut >= (MP_U32)pEndOut )
|
427
|
+
return LZMAT_STATUS_INTEGRITY_FAILURE;
|
428
|
+
}
|
429
|
+
#undef cbCompressed
|
430
|
+
if(match_cnt==1)
|
431
|
+
{
|
432
|
+
hash_Idx = LZMAT_HASH(pInp);
|
433
|
+
LZMATHDICT.idx[inPtr & IDX_MSK] = LZMATHDICT.ptr[hash_Idx];
|
434
|
+
LZMATHDICT.ptr[hash_Idx] = inPtr++;
|
435
|
+
}
|
436
|
+
else
|
437
|
+
{
|
438
|
+
i = inPtr;
|
439
|
+
inPtr += match_cnt;
|
440
|
+
if ( (unsigned int)match_cnt > 0x38 )
|
441
|
+
match_cnt = 0x38;
|
442
|
+
while ( match_cnt-- )
|
443
|
+
{
|
444
|
+
hash_Idx = LZMAT_HASH(pInp);
|
445
|
+
pInp++;
|
446
|
+
LZMATHDICT.idx[i & IDX_MSK] = LZMATHDICT.ptr[hash_Idx];
|
447
|
+
LZMATHDICT.ptr[hash_Idx] = (MP_U32)(i++); //((i++)-start_dict);
|
448
|
+
}
|
449
|
+
}
|
450
|
+
}
|
451
|
+
if(bit_msk)
|
452
|
+
{
|
453
|
+
ThisTag |= bit_msk;
|
454
|
+
ThisTag |= (bit_msk-1);
|
455
|
+
}
|
456
|
+
if(tag_nib&1)
|
457
|
+
{
|
458
|
+
*pTag++ |= (ThisTag<<4);
|
459
|
+
pTag[0] |= (ThisTag>>4);
|
460
|
+
}
|
461
|
+
else
|
462
|
+
*pTag=ThisTag;
|
463
|
+
*pcbOut = ((MP_U32)pOut - (MP_U32)pbOut)+cur_nib;
|
464
|
+
return 0;
|
465
|
+
}
|
466
|
+
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lzmat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- mirakui
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-04-19 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ruby bindings for LZMAT. LZMAT is an fast real-time lossless data compression library.
|
22
|
+
email: mimitako@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions:
|
26
|
+
- ext/extconf.rb
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README
|
29
|
+
files:
|
30
|
+
- ext/lzmat.c
|
31
|
+
- ext/lzmat_dec.c
|
32
|
+
- ext/lzmat_enc.c
|
33
|
+
- ext/lzmat.h
|
34
|
+
- ext/extconf.rb
|
35
|
+
- README
|
36
|
+
homepage: https://github.com/winebarrel/infra-study/tree/master/4th/gems/lzmat
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --title
|
42
|
+
- lzmat - Ruby bindings for LZMAT.
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Ruby bindings for LZMAT.
|
72
|
+
test_files: []
|
73
|
+
|