-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlzpmem.pas
More file actions
121 lines (107 loc) · 3.84 KB
/
lzpmem.pas
File metadata and controls
121 lines (107 loc) · 3.84 KB
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
{ MIT License
Copyright (c) 2025 Viacheslav Komenda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. }
{$A+,I-,S-,R-,D+,L+,Q-,F-,G-,O-,B-}
UNIT LZPMEM;
INTERFACE
FUNCTION LZP_encmem(input: PCHAR; len : WORD; output: PCHAR) : WORD;
FUNCTION LZP_decmem(input: PCHAR; len : WORD; output: PCHAR) : WORD;
IMPLEMENTATION
CONST
HASH_BITS = 12;
HASH_SIZE = (1 SHL HASH_BITS);
HASH_MASK = HASH_SIZE - 1;
TYPE
THashTable = ARRAY[0..HASH_SIZE - 1] Of CHAR;
{
FUNCTION HashFunc(h: WORD; x: CHAR) : WORD;
BEGIN
HashFunc := ((h * 160) XOR (ORD(x) AND $FF)) AND (HASH_SIZE - 1);
END;
}
FUNCTION HashFunc(h: WORD; x: CHAR) : WORD; ASSEMBLER;
ASM
MOV AX, [h]
XOR DX, DX
MOV CL, 160
MUL CL
XOR AL, [x]
AND AX, HASH_MASK
END;
FUNCTION LZP_encmem(input: PCHAR; len : WORD; output: PCHAR) : WORD;
VAR hash, r, i : WORD;
c : CHAR;
mask : BYTE;
HashTable : THashTable;
maskPtr : PCHAR;
BEGIN
FillChar(HashTable, SizeOf(THashTable), #0);
hash := 0;
r := ofs(output^);
WHILE (len <> 0) DO BEGIN
mask := 0;
i := 0;
maskPtr := output;
INC(output);
WHILE (i <= 7) AND (len <> 0) DO BEGIN
c := input^;
INC(input);
DEC(len);
IF c = HashTable[hash] THEN mask := mask OR (1 SHL i)
ELSE BEGIN
HashTable[hash] := c;
output^ := c;
INC(output);
END;
hash := HashFunc(hash, c);
INC(i);
END;
maskPtr^ := CHR(mask);
END;
LZP_encmem := ofs(output^) - r;
END;
FUNCTION LZP_decmem(input: PCHAR; len : WORD; output: PCHAR) : WORD;
VAR hash, i, r : WORD;
mask : BYTE;
c : CHAR;
HashTable : THashTable;
BEGIN
FillChar(HashTable, SizeOf(THashTable), #0);
r := ofs(output^);
hash := 0;
WHILE (len <> 0) DO BEGIN
mask := ORD(input^);
INC(input);
DEC(len);
i := 0;
WHILE (i <= 7) AND (len <> 0) DO BEGIN
IF (mask AND (1 SHL i)) <> 0 THEN c := HashTable[hash]
ELSE BEGIN
c := input^;
HashTable[hash] := c;
INC(input);
DEC(len);
END;
output^ := c;
INC(output);
hash := HashFunc(hash, c);
INC(i);
END;
END;
LZP_decmem := ofs(output^) - r;
END;
END.