1
1
// --------------------------------------------------------------------------------------------------
2
2
// SAP Source Code Decompressor
3
3
// --------------------------------------------------------------------------------------------------
4
- // Written by Daniel Berlin <[email protected] > - http ://www.daniel-berlin.de/
4
+ // Written by Daniel Berlin <[email protected] > - https ://www.daniel-berlin.de/
5
5
// Inspired by code from Dennis Yurichev <[email protected] > - http://www.yurichev.com/
6
- // This program uses portions of the MaxDB 7.6.00.34 source code, which are licensed under
7
- // the GNU General Public License (see file headers in "lib/" folder).
6
+ // This program uses portions of the MaxDB 7.6 source code, which are licensed under
7
+ // the GNU General Public License v2 (see file headers in "lib/" folder).
8
8
// Comments, suggestions and questions are welcome!
9
9
// --------------------------------------------------------------------------------------------------
10
10
// The source code stored in REPOSRC-DATA is basically compressed using the LZH algorithm
18
18
// 2012-06-23 - 1.0.0 - Initial version
19
19
// 2012-06-24 - 1.0.1 - Revert modifications to MaxDB library
20
20
// 2015-01-05 - 1.1.0 - Major UTF-16 enhancement by Uwe Lindemann
21
- // 2015-07-24 - 1.1.1 - Add option for non-unicode SAP systems by Bastian Preißler
21
+ // 2015-07-24 - 1.1.1 - Add option for non-Unicode SAP systems by Bastian Preißler
22
+ // 2017-02-09 - 1.1.2 - Tune output buffer factor; various minor adjustments
22
23
// --------------------------------------------------------------------------------------------------
23
24
24
- #define VERSION " 1.1.1 "
25
+ #define VERSION " 1.1.2 "
25
26
26
27
// Silence MS Visual C++ ("... consider using fopen_s instead ...")
27
28
#ifdef _MSC_VER
@@ -44,7 +45,7 @@ int main(int argc, char *argv[]) {
44
45
FILE *fin, *fout; // In-/output file handles
45
46
long fin_size; // Input file size
46
47
SAP_BYTE *bin, *bout; // In-/output buffers
47
- short factor = 10 ; // Initial output buffer size factor
48
+ short factor = 5 ; // Initial output buffer size factor
48
49
class CsObjectInt o; // Class containing the decompressor
49
50
SAP_INT byte_read, byte_decomp; // Number of bytes read and decompressed
50
51
long i; // Loop counter
@@ -53,8 +54,8 @@ int main(int argc, char *argv[]) {
53
54
char cuc[] = " -u" ; // Unicode parameter
54
55
int funicode; // Unicode flag
55
56
56
- char nuc[] = " -n" ; // Non -Unicode SAP system parameter
57
- int fnuc; // Non -Unicode SAP system flag
57
+ char nuc[] = " -n" ; // non -Unicode SAP system parameter
58
+ int fnuc; // non -Unicode SAP system flag
58
59
59
60
unsigned char cbom1 = (unsigned char ) 0xFE ; // BOM for UTF-16: 0xFEFF
60
61
unsigned char cbom2 = (unsigned char ) 0xFF ; // ...
@@ -64,22 +65,22 @@ int main(int argc, char *argv[]) {
64
65
printf (" ------------------------------------\n\n " );
65
66
66
67
// Check command line parameters
67
- if (argc < 3 || argc > 4 || argv[1 ] == NULL || argv[2 ] == NULL ) {
68
+ if (argc < 3 || argc > 4 || argv[1 ] == NULL || argv[2 ] == NULL ) {
68
69
printf (" Usage:\n %s <infile> <outfile> [-u] [-n]\n\n " , argv[0 ]);
69
70
printf (" Options:\n -u : create UTF-16 output; defaults to ASCII\n " );
70
- printf (" -n : assume input from non-unicode SAP system\n\n " );
71
+ printf (" -n : assume input from non-Unicode SAP system\n\n " );
71
72
return 0 ;
72
73
}
73
74
74
- if (argc == 4 && strcmp (argv[3 ], cuc) == 0 ) { funicode = 1 ; }
75
- else { funicode = 0 ; }
75
+ if (argc == 4 && strcmp (argv[3 ], cuc) == 0 ) { funicode = 1 ; }
76
+ else { funicode = 0 ; }
76
77
77
- if (argc == 4 && strcmp (argv[3 ], nuc) == 0 ) { fnuc = 1 ; }
78
- else { fnuc = 0 ; }
78
+ if (argc == 4 && strcmp (argv[3 ], nuc) == 0 ) { fnuc = 1 ; }
79
+ else { fnuc = 0 ; }
79
80
80
81
// Open input file
81
82
fin = fopen (argv[1 ], " rb" );
82
- if (fin == NULL ) {
83
+ if (fin == NULL ) {
83
84
printf (" Error opening input file '%s'\n " , argv[1 ]);
84
85
return 1 ;
85
86
}
@@ -103,7 +104,7 @@ int main(int argc, char *argv[]) {
103
104
104
105
// Create output file
105
106
fout = fopen (argv[2 ], " wb" );
106
- if (fout == NULL ) {
107
+ if (fout == NULL ) {
107
108
printf (" Error creating output file '%s'\n " , argv[2 ]);
108
109
free (bin);
109
110
return 2 ;
@@ -115,13 +116,13 @@ int main(int argc, char *argv[]) {
115
116
ret = o.CsGetAlgorithm (bin + 1 );
116
117
printf (" Algorithm: %i (1 = LZC, 2 = LZH)\n " , ret);
117
118
118
- for (;;) {
119
- // Create output buffer with an initial size factor of 10 x input buffer
119
+ for (;;) {
120
+ // Create output buffer with an initial size of <factor> x < input buffer size>
120
121
bout = (SAP_BYTE*) malloc (fin_size * factor);
121
122
122
123
// Perform decompression
123
124
ret = o.CsDecompr (
124
- bin + 1 // Skip 1st byte (-> strange ! )
125
+ bin + 1 // Skip 1st byte (-> strange)
125
126
, fin_size - 1 // Input size
126
127
, bout // Output buffer
127
128
, fin_size * factor // Output buffer size
@@ -132,15 +133,16 @@ int main(int argc, char *argv[]) {
132
133
133
134
// Output buffer too small -> increase size factor and retry
134
135
if (ret == CS_END_OUTBUFFER || ret == CS_E_OUT_BUFFER_LEN) {
136
+ // printf("Output buffer adjusted: factor %i -> %i\n", factor, factor + 5);
137
+ factor += 5 ;
135
138
free (bout);
136
- factor += 10 ;
137
139
continue ;
138
140
}
139
141
140
142
free (bin);
141
143
142
144
// Handle all other return codes
143
- switch (ret) {
145
+ switch (ret) {
144
146
case CS_END_OF_STREAM :
145
147
case CS_END_INBUFFER : printf (" Decompression successful\n " ); break ;
146
148
case CS_E_IN_BUFFER_LEN : printf (" Error: CS_E_IN_BUFFER_LEN\n " ); return 3 ;
@@ -157,49 +159,50 @@ int main(int argc, char *argv[]) {
157
159
}
158
160
159
161
// In case of Unicode output: write UTF-16 BOM
160
- if (funicode) {
162
+ if (funicode) {
161
163
fwrite (&cbom1, 1 , 1 , fout);
162
164
fwrite (&cbom2, 1 , 1 , fout);
163
165
}
164
166
165
167
// The 2nd byte contains the length of the first line.
166
- // For non-unicode SAP systems the 1st byte contains the length of the first line.
168
+ // For non-Unicode SAP systems the 1st byte contains the length of the first line.
167
169
// Compute position of next length field.
168
- if (fnuc) {
169
- nextpos = (long )bout[0 ];
170
+ if (fnuc) {
171
+ nextpos = (long ) bout[0 ];
170
172
}
171
- else {
172
- nextpos = ((long )bout[1 ]) * 2 + 3 ;
173
+ else {
174
+ nextpos = ((long ) bout[1 ]) * 2 + 3 ;
173
175
}
174
176
175
- for (i = 1 ; i < byte_decomp; i++) {
176
- if (i == 1 && !fnuc) { continue ; }
177
- if ((i % 2 ) == 0 && !fnuc) {
178
- if (funicode) {
177
+ for (i = 1 ; i < byte_decomp; i++) {
178
+ if (i == 1 && !fnuc) { continue ; }
179
+
180
+ if ((i % 2 ) == 0 && !fnuc) {
181
+ if (funicode) {
179
182
// In case of Unicode output: write Big Endian byte
180
183
ret = fwrite (bout + i, 1 , 1 , fout);
181
184
}
182
185
}
183
186
else {
184
- if (i == nextpos) {
187
+ if (i == nextpos) {
185
188
// Write line feed
186
189
ret = fwrite (" \n " , 1 , 1 , fout);
187
190
188
191
// Compute position of next length field
189
- if (fnuc) {
190
- nextpos = nextpos + (long )bout[0 ] + 1 ;
192
+ if (fnuc) {
193
+ nextpos = nextpos + (long ) bout[0 ] + 1 ;
191
194
i += 1 ;
192
195
}
193
196
else {
194
- nextpos = nextpos + (((long )bout[i]) * 2 + 2 );
197
+ nextpos = nextpos + (((long ) bout[i]) * 2 + 2 );
195
198
}
196
199
}
197
200
else {
198
201
ret = fwrite (bout + i, 1 , 1 , fout);
199
202
}
200
203
}
201
204
202
- if (ret != 1 ) {
205
+ if (ret != 1 ) {
203
206
printf (" Error writing to output file '%s'\n " , argv[2 ]);
204
207
return 11 ;
205
208
}
@@ -209,8 +212,8 @@ int main(int argc, char *argv[]) {
209
212
fwrite (" \n " , 1 , 1 , fout);
210
213
fclose (fout);
211
214
212
- if (funicode) { printf (" Unicode" ); }
213
- else { printf (" Plain text" ); }
215
+ if (funicode) { printf (" Unicode" ); }
216
+ else { printf (" Plain text" ); }
214
217
printf (" source written to '%s'\n Have a nice day\n\n " , argv[2 ]);
215
218
216
219
free (bout);
0 commit comments