-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaxman.bas
More file actions
131 lines (112 loc) · 5.23 KB
/
saxman.bas
File metadata and controls
131 lines (112 loc) · 5.23 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
122
123
124
125
126
127
128
129
130
131
Attribute VB_Name = "SaxmanModule"
' /*-----------------------------------------------------------------------------*\
' | |
' | Saxman.dll: Compression / Decompression of data in Saxman format |
' | Copyright © 2002-2004 The KENS Project Development Team |
' | |
' | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
' | |
' \*-----------------------------------------------------------------------------*/
'
' VB6-port by Valley Bell (04. May 2011)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(ByRef hpvDest As Any, ByRef hpvSource As Any, ByVal cbCopy As Long)
Public Const SUCCESS As Byte = &H0
Public Const ERROR_UNKNOWN As Byte = &H1
Public Const ERROR_SOURCE_FILE_DOES_NOT_EXIST As Byte = &H2
'-----------------------------------------------------------------------------------------------
' Name: SDecomp(char *SrcFile, char *DstFile, long Location, unsigned short Size)
' Desc: Decompresses the data using the Saxman compression format
'-----------------------------------------------------------------------------------------------
Public Function SDecomp(ByVal SrcSize As Long, ByRef SrcFile() As Byte, ByRef DstSize As Long, ByRef DstFile() As Byte, ByVal Location As Long) As Long
' Files
Dim SrcPos As Long
Dim DstPos As Long
Dim SrcEnd As Long
' Info Byte, Flag, Count and Offset (with initial values)
Dim InfoByte As Byte
Dim IBP As Byte
Dim IBPMask As Integer
Dim Flag As Boolean
Dim Count As Byte
Dim Offset As Long
Count = 0
InfoByte = 0
IBP = 8
IBPMask = &H100 ' 2^8
Flag = False
Offset = 0
' Other info
Dim i As Long ' Counter
'------------------------------------------------------------------------------------------------
SrcPos = Location
DstPos = &H0
Call CopyMemory(SrcSize, SrcFile(SrcPos), &H2)
SrcSize = SrcSize And &HFFFF&
SrcPos = SrcPos + &H2
SrcEnd = SrcPos + SrcSize
ReDim DstFile(&H0 To &H7FFF - 1)
Do While True
If IBP = 8 Then
IBP = 0
IBPMask = &H1
If SrcPos >= SrcEnd Then
Exit Do
End If
InfoByte = SrcFile(SrcPos)
SrcPos = SrcPos + &H1
End If
'Flag = (InfoByte >> IBP) And 1
Flag = CBool(InfoByte And IBPMask)
IBP = IBP + &H1
IBPMask = IBPMask * &H2
Select Case Flag
Case False
If SrcPos + &H1 >= SrcEnd Then Exit Do
Offset = SrcFile(SrcPos + &H0)
Count = SrcFile(SrcPos + &H1)
SrcPos = SrcPos + &H2
Offset = (Offset Or ((Count And &HF0) * &H10)) + &H12 ' Can be improved
Offset = Offset Or (DstPos And &HF000&)
Count = (Count And &HF) + &H3
If Offset >= DstPos Then
Offset = (Offset - &H1000&) And &HFFFF&
End If
If DstPos + Count > &H8000& Then Exit Do
If Offset < DstPos Then
For i = 0 To Count - 1
If Offset + i >= DstPos Then Exit For
DstFile(DstPos) = DstFile(Offset + i)
DstPos = DstPos + &H1
Next i
Else
For i = 0 To Count - 1
DstFile(DstPos) = &H0
DstPos = DstPos + &H1
Next i
End If
Case True
If SrcPos >= SrcEnd Then Exit Do
If DstPos > &H8000& Then Exit Do
DstFile(DstPos) = SrcFile(SrcPos)
SrcPos = SrcPos + &H1
DstPos = DstPos + &H1
End Select
Loop
'------------------------------------------------------------------------------------------------
DstSize = DstPos
ReDim Preserve DstFile(&H0 To DstSize - 1)
SDecomp = SUCCESS
End Function