-
Notifications
You must be signed in to change notification settings - Fork 45
/
XADARCCrunchHandle.m
129 lines (103 loc) · 2.89 KB
/
XADARCCrunchHandle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* XADARCCrunchHandle.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "XADARCCrunchHandle.h"
#import "XADException.h"
@implementation XADARCCrunchHandle
-(id)initWithHandle:(CSHandle *)handle useFastHash:(BOOL)usefast
{
return [self initWithHandle:handle length:CSHandleMaxLength useFastHash:usefast];
}
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length useFastHash:(BOOL)usefast
{
if((self=[super initWithInputBufferForHandle:handle length:length]))
{
fast=usefast;
}
return self;
}
-(void)resetByteStream
{
sp=0;
numfreecodes=4096-256;
for(int i=0;i<256;i++) [self updateTableWithParent:-1 byteValue:i];
int code=CSInputNextBitString(input,12);
int byte=table[code].byte;
stack[sp++]=byte;
lastcode=code;
lastbyte=byte;
}
-(uint8_t)produceByteAtOffset:(off_t)pos
{
if(!sp)
{
if(CSInputAtEOF(input)) CSByteStreamEOF(self);
int code=CSInputNextBitString(input,12);
XADARCCrunchEntry *entry=&table[code];
if(!entry->used)
{
entry=&table[lastcode];
stack[sp++]=lastbyte;
}
while(entry->parent!=-1)
{
if(sp>=4095) [XADException raiseDecrunchException];
stack[sp++]=entry->byte;
entry=&table[entry->parent];
}
uint8_t byte=entry->byte;
stack[sp++]=byte;
if(numfreecodes!=0)
{
[self updateTableWithParent:lastcode byteValue:byte];
numfreecodes--;
}
lastcode=code;
lastbyte=byte;
}
return stack[--sp];
}
-(void)updateTableWithParent:(int)parentcode byteValue:(int)byte
{
// Find hash table position.
int index;
if(fast) index=(((parentcode+byte)&0xffff)*15073)&0xfff;
else
{
index=((parentcode+byte)|0x0800)&0xffff;
index=(index*index>>6)&0xfff;
}
if(table[index].used) // Check for collision.
{
// Go through the list of already marked collisions.
while(table[index].next) index=table[index].next;
// Then skip ahead, and do a linear search for an unused index.
int next=(index+101)&0xfff;
while(table[next].used) next=(next+1)&0xfff;
// Save the new index so we can skip the process next time.
table[index].next=next;
index=next;
}
table[index].used=YES;
table[index].next=0;
table[index].parent=parentcode;
table[index].byte=byte;
}
@end