@@ -95,21 +95,22 @@ int main(int argc, char **argv)
95
95
DEBUG_LOG ((" IN:'%s' OUT:'%s' Compression:'%s'\n " ,
96
96
inFile.c_str (), outFile.c_str (), CompressionManager::getCompressionNameByType (compressType)));
97
97
98
+ // just check compression on the input file if we have no output specified
98
99
if (outFile.empty ())
99
100
{
100
- // just check compression
101
- FILE *fp = fopen (inFile.c_str (), " rb" );
102
- if (!fp)
101
+ FILE *fpIn = fopen (inFile.c_str (), " rb" );
102
+ if (!fpIn)
103
103
{
104
104
DEBUG_LOG ((" Cannot open '%s'\n " , inFile.c_str ()));
105
105
return EXIT_FAILURE;
106
106
}
107
- fseek (fp , 0 , SEEK_END);
108
- int size = ftell (fp );
109
- fseek (fp , 0 , SEEK_SET);
107
+ fseek (fpIn , 0 , SEEK_END);
108
+ int size = ftell (fpIn );
109
+ fseek (fpIn , 0 , SEEK_SET);
110
110
111
111
char data[8 ];
112
- int numRead = fread (data, 1 , 8 , fp);
112
+ int numRead = fread (data, 1 , 8 , fpIn);
113
+ fclose (fpIn);
113
114
114
115
if (numRead != 8 )
115
116
{
@@ -121,7 +122,7 @@ int main(int argc, char **argv)
121
122
if (usedType == COMPRESSION_NONE)
122
123
{
123
124
DEBUG_LOG ((" No compression on '%s'\n " , inFile.c_str ()));
124
- return EXIT_FAILURE ;
125
+ return EXIT_SUCCESS ;
125
126
}
126
127
127
128
int uncompressedSize = CompressionManager::getUncompressedSize (data, 8 );
@@ -133,6 +134,66 @@ int main(int argc, char **argv)
133
134
return EXIT_SUCCESS;
134
135
}
135
136
136
- // compress file
137
- return EXIT_FAILURE;
137
+ // Open the input file
138
+ FILE *fpIn = fopen (inFile.c_str (), " rb" );
139
+ if (!fpIn)
140
+ {
141
+ DEBUG_LOG ((" Cannot open input '%s'\n " , inFile.c_str ()));
142
+ return EXIT_FAILURE;
143
+ }
144
+
145
+ // Read the input file
146
+ fseek (fpIn, 0 , SEEK_END);
147
+ int inputSize = ftell (fpIn);
148
+ fseek (fpIn, 0 , SEEK_SET);
149
+
150
+ char *inputData = new char [inputSize];
151
+ int numRead = fread (inputData, 1 , inputSize, fpIn);
152
+ fclose (fpIn);
153
+ if (numRead != inputSize)
154
+ {
155
+ DEBUG_LOG ((" Cannot read input '%s'\n " , inFile.c_str ()));
156
+ delete[] inputData;
157
+ return EXIT_FAILURE;
158
+ }
159
+
160
+ DEBUG_LOG ((" Read %d bytes from '%s'\n " , numRead, inFile.c_str ()));
161
+
162
+ // Open the output file
163
+ FILE *fpOut = fopen (outFile.c_str (), " wb" );
164
+ if (!fpOut)
165
+ {
166
+ DEBUG_LOG ((" Cannot open output '%s'\n " , outFile.c_str ()));
167
+ delete[] inputData;
168
+ return EXIT_FAILURE;
169
+ }
170
+
171
+
172
+ if (compressType == COMPRESSION_NONE)
173
+ {
174
+ DEBUG_LOG ((" No compression requested, writing uncompressed data\n " ));
175
+ int outSize = CompressionManager::getUncompressedSize (inputData, inputSize);
176
+ char *outData = new char [outSize];
177
+ CompressionManager::decompressData (inputData, inputSize, outData, outSize);
178
+
179
+ // Write the output file
180
+ fwrite (outData, 1 , outSize, fpOut);
181
+ }
182
+ else
183
+ {
184
+ DEBUG_LOG ((" Compressing data using %s\n " , CompressionManager::getCompressionNameByType (compressType)));
185
+ // Allocate the output buffer
186
+ int outSize = CompressionManager::getMaxCompressedSize (inputSize, compressType);
187
+ char *outData = new char [outSize];
188
+ int compressedSize = CompressionManager::compressData (compressType, inputData, inputSize, outData, outSize);
189
+
190
+ // Write the output file
191
+ fwrite (outData, 1 , compressedSize, fpOut);
192
+ delete[] outData;
193
+ }
194
+
195
+ fclose (fpOut);
196
+ delete[] inputData;
197
+
198
+ return EXIT_SUCCESS;
138
199
}
0 commit comments