Wing Commander
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
lzo1x.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup ixlib
4 * @brief Hand-written LZO1X decoder for packed streams.
5 *
6 * Address range 4614c0-46162f. The retail routine is hand-written x86: it
7 * keeps input and token state in EBX and EDX and performs overlapping copies
8 * with REP MOVS. It was an /Od function around an __asm block; its ALIGN 4
9 * directives account for the padding inside it (nop, lea ecx,[ecx]).
10 * Preserve that code directly so packed stream chunks decode exactly as they
11 * did originally.
12 *
13 * The decoder came from a library of its own, searched before the file
14 * module's (ixfile.cpp): both are pulled only on LINK's second search pass,
15 * after the RtlUnwind thunk and before the CRT members reached through
16 * OLDNAMES.
17 */
18#include "ix.h"
19#include <stdlib.h>
20
21/* Function start: 0x004614C0 */
22extern "C" void ix_lzo1x_decompress(
23 unsigned char *source, unsigned char *destination,
24 unsigned int destinationBytes)
25{
26 __asm {
27 mov ebx, source
28 mov edi, destination
29 or ebx, ebx
30 je lzo_finish_copy
31 xor ecx, ecx
32 jmp lzo_read_token
33 ALIGN 4
34
35lzo_copy_literals:
36 lea esi, [ebx + 2]
37 rep movsb
38 mov ebx, esi
39 mov ch, dl
40 mov cl, dh
41 and edx, 1ch
42 shr ch, 5
43 shr edx, 2
44 neg ecx
45 lea esi, [edi + ecx - 1]
46 lea ecx, [edx + 3]
47 rep movsb
48 or cl, byte ptr [ebx]
49 mov edx, dword ptr [ebx]
50 js lzo_large_token
51
52lzo_match_tail:
53 and ecx, 3
54 jne lzo_copy_literals
55 lea ebx, [ebx + 2]
56 mov ch, dl
57 mov cl, dh
58 and edx, 1ch
59 shr ch, 5
60 shr edx, 2
61 neg ecx
62 lea esi, [edi + ecx - 1]
63 lea ecx, [edx + 3]
64 rep movsb
65
66lzo_read_token:
67 or cl, byte ptr [ebx]
68 mov edx, dword ptr [ebx]
69 jns lzo_match_tail
70
71lzo_large_token:
72 add cl, cl
73 js lzo_literal_token
74 mov cl, dh
75 lea esi, [ebx + 3]
76 shr ecx, 6
77 and ecx, 3
78 rep movsb
79 mov ebx, esi
80 mov ecx, edx
81 shr ecx, 10h
82 mov ch, dh
83 and ch, 3fh
84 neg ecx
85 lea esi, [edi + ecx - 1]
86 and edx, 3fh
87 lea ecx, [edx + 4]
88 rep movsb
89 or cl, byte ptr [ebx]
90 mov edx, dword ptr [ebx]
91 jns lzo_match_tail
92 jmp lzo_large_token
93 ALIGN 4
94
95lzo_literal_token:
96 add cl, cl
97 jns lzo_long_match
98 cmp dl, 0fch
99 jae lzo_final_literals
100 and edx, 1fh
101 lea esi, [ebx + 1]
102 lea ecx, [edx + 1]
103 rep movsd
104 mov ebx, esi
105 or cl, byte ptr [esi]
106 mov edx, dword ptr [esi]
107 jns lzo_match_tail
108 jmp lzo_large_token
109 ALIGN 4
110
111lzo_final_literals:
112 mov ecx, edx
113 lea esi, [ebx + 1]
114 and ecx, 3
115 rep movsb
116
117lzo_finish_copy:
118 jmp lzo_finish
119 ALIGN 4
120
121lzo_long_match:
122 mov ecx, edx
123 lea esi, [ebx + 4]
124 and ecx, 3
125 rep movsb
126 mov ebx, esi
127 mov ecx, edx
128 mov eax, edx
129 and ecx, 10h
130 shr eax, 8
131 shl ecx, 0ch
132 mov cl, ah
133 mov ch, al
134 neg ecx
135 lea esi, [edi + ecx - 1]
136 rol edx, 8
137 shr dh, 2
138 and edx, 3ffh
139 cmp ecx, -4
140 jge lzo_short_long_match
141 lea ecx, [edx + 5]
142 lea edx, [edx + 5]
143 shr ecx, 2
144 and edx, 3
145 rep movsd
146 mov ecx, edx
147 rep movsb
148 or cl, byte ptr [ebx]
149 mov edx, dword ptr [ebx]
150 jns lzo_match_tail
151 jmp lzo_large_token
152 ALIGN 4
153
154lzo_short_long_match:
155 lea ecx, [edx + 5]
156 rep movsb
157 or cl, byte ptr [ebx]
158 mov edx, dword ptr [ebx]
159 jns lzo_match_tail
160 jmp lzo_large_token
161
162lzo_finish:
163 }
164}
The library's classes and shared declarations.
void ix_lzo1x_decompress(unsigned char *source, unsigned char *destination, unsigned int destinationBytes)
Definition lzo1x.cpp:22