Skip to content

Commit 3b85b9e

Browse files
committed
Tune output buffer factor; various minor adjustments
1 parent cdeada1 commit 3b85b9e

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

SAP-Report-Source-Decompressor/sap-reposrc-decompressor.cpp

+41-38
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//--------------------------------------------------------------------------------------------------
22
// SAP Source Code Decompressor
33
//--------------------------------------------------------------------------------------------------
4-
// Written by Daniel Berlin <[email protected]> - http://www.daniel-berlin.de/
4+
// Written by Daniel Berlin <[email protected]> - https://www.daniel-berlin.de/
55
// 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).
88
// Comments, suggestions and questions are welcome!
99
//--------------------------------------------------------------------------------------------------
1010
// The source code stored in REPOSRC-DATA is basically compressed using the LZH algorithm
@@ -18,10 +18,11 @@
1818
// 2012-06-23 - 1.0.0 - Initial version
1919
// 2012-06-24 - 1.0.1 - Revert modifications to MaxDB library
2020
// 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
2223
//--------------------------------------------------------------------------------------------------
2324

24-
#define VERSION "1.1.1"
25+
#define VERSION "1.1.2"
2526

2627
// Silence MS Visual C++ ("... consider using fopen_s instead ...")
2728
#ifdef _MSC_VER
@@ -44,7 +45,7 @@ int main(int argc, char *argv[]) {
4445
FILE *fin, *fout; // In-/output file handles
4546
long fin_size; // Input file size
4647
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
4849
class CsObjectInt o; // Class containing the decompressor
4950
SAP_INT byte_read, byte_decomp; // Number of bytes read and decompressed
5051
long i; // Loop counter
@@ -53,8 +54,8 @@ int main(int argc, char *argv[]) {
5354
char cuc[] = "-u"; // Unicode parameter
5455
int funicode; // Unicode flag
5556

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
5859

5960
unsigned char cbom1 = (unsigned char) 0xFE; // BOM for UTF-16: 0xFEFF
6061
unsigned char cbom2 = (unsigned char) 0xFF; // ...
@@ -64,22 +65,22 @@ int main(int argc, char *argv[]) {
6465
printf("------------------------------------\n\n");
6566

6667
// 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) {
6869
printf("Usage:\n %s <infile> <outfile> [-u] [-n]\n\n", argv[0]);
6970
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");
7172
return 0;
7273
}
7374

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; }
7677

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; }
7980

8081
// Open input file
8182
fin = fopen(argv[1], "rb");
82-
if (fin == NULL) {
83+
if(fin == NULL) {
8384
printf("Error opening input file '%s'\n", argv[1]);
8485
return 1;
8586
}
@@ -103,7 +104,7 @@ int main(int argc, char *argv[]) {
103104

104105
// Create output file
105106
fout = fopen(argv[2], "wb");
106-
if (fout == NULL) {
107+
if(fout == NULL) {
107108
printf("Error creating output file '%s'\n", argv[2]);
108109
free(bin);
109110
return 2;
@@ -115,13 +116,13 @@ int main(int argc, char *argv[]) {
115116
ret = o.CsGetAlgorithm(bin + 1);
116117
printf("Algorithm: %i (1 = LZC, 2 = LZH)\n", ret);
117118

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>
120121
bout = (SAP_BYTE*) malloc(fin_size * factor);
121122

122123
// Perform decompression
123124
ret = o.CsDecompr(
124-
bin + 1 // Skip 1st byte (-> strange !)
125+
bin + 1 // Skip 1st byte (-> strange)
125126
, fin_size - 1 // Input size
126127
, bout // Output buffer
127128
, fin_size * factor // Output buffer size
@@ -132,15 +133,16 @@ int main(int argc, char *argv[]) {
132133

133134
// Output buffer too small -> increase size factor and retry
134135
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;
135138
free(bout);
136-
factor += 10;
137139
continue;
138140
}
139141

140142
free(bin);
141143

142144
// Handle all other return codes
143-
switch (ret) {
145+
switch(ret) {
144146
case CS_END_OF_STREAM :
145147
case CS_END_INBUFFER : printf("Decompression successful\n" ); break;
146148
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[]) {
157159
}
158160

159161
// In case of Unicode output: write UTF-16 BOM
160-
if (funicode) {
162+
if(funicode) {
161163
fwrite(&cbom1, 1, 1, fout);
162164
fwrite(&cbom2, 1, 1, fout);
163165
}
164166

165167
// 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.
167169
// Compute position of next length field.
168-
if (fnuc) {
169-
nextpos = (long)bout[0];
170+
if(fnuc) {
171+
nextpos = (long) bout[0];
170172
}
171-
else {
172-
nextpos = ((long)bout[1]) * 2 + 3;
173+
else {
174+
nextpos = ((long) bout[1]) * 2 + 3;
173175
}
174176

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) {
179182
// In case of Unicode output: write Big Endian byte
180183
ret = fwrite(bout + i, 1, 1, fout);
181184
}
182185
}
183186
else {
184-
if (i == nextpos) {
187+
if(i == nextpos) {
185188
// Write line feed
186189
ret = fwrite("\n", 1, 1, fout);
187190

188191
// 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;
191194
i += 1;
192195
}
193196
else {
194-
nextpos = nextpos + (((long)bout[i]) * 2 + 2);
197+
nextpos = nextpos + (((long) bout[i]) * 2 + 2);
195198
}
196199
}
197200
else {
198201
ret = fwrite(bout + i, 1, 1, fout);
199202
}
200203
}
201204

202-
if (ret != 1) {
205+
if(ret != 1) {
203206
printf("Error writing to output file '%s'\n", argv[2]);
204207
return 11;
205208
}
@@ -209,8 +212,8 @@ int main(int argc, char *argv[]) {
209212
fwrite("\n", 1, 1, fout);
210213
fclose(fout);
211214

212-
if (funicode) { printf("Unicode" ); }
213-
else { printf("Plain text"); }
215+
if(funicode) { printf("Unicode" ); }
216+
else { printf("Plain text"); }
214217
printf(" source written to '%s'\nHave a nice day\n\n", argv[2]);
215218

216219
free(bout);

0 commit comments

Comments
 (0)