26 buffBytes =
new uchar[estimatedMaxBuffSize];
27 buffLens =
new uchar[estimatedMaxBuffSize];
29 buffSize = estimatedMaxBuffSize;
32 growthRate = estimatedMaxBuffSize;
55 if (buffSpaceUsed >= buffSize)
57 kkuint32 newBuffSize = buffSize + growthRate;
61 memcpy (newBuffBytes, buffBytes, buffSize);
62 memcpy (newBuffLens, buffLens, buffSize);
65 buffBytes = newBuffBytes;
66 buffLens = newBuffLens;
69 buffSize = newBuffSize;
72 if (buffSpaceUsed == 0)
82 if (b == buffBytes[lastBuffByteUsed])
84 if (buffLens[lastBuffByteUsed] > 126)
86 buffBytes[buffSpaceUsed] = b;
87 buffLens [buffSpaceUsed] = 1;
88 lastBuffByteUsed = buffSpaceUsed;
93 buffLens[lastBuffByteUsed]++;
98 buffBytes[buffSpaceUsed] = b;
99 buffLens [buffSpaceUsed] = 1;
100 lastBuffByteUsed = buffSpaceUsed;
112 compressedBuffUsed++;
113 compressedBuffUsed++;
117 while (idx < buffSpaceUsed)
119 while ((idx < buffSpaceUsed) && (buffLens[idx] > 1))
121 compressedBuffUsed++;
122 compressedBuffUsed++;
127 while ((idx < buffSpaceUsed) && (buffLens[idx] <= 1))
135 while ((endIdx < buffSpaceUsed) && (buffLens[endIdx] <= 1) && (rawBytesInARow < 127))
144 compressedBuffUsed++;
145 for (
kkuint32 x = 0; x < rawBytesInARow; x++)
147 compressedBuffUsed++;
153 return compressedBuffUsed;
166 compressedBuff[compressedBuffUsed] = b;
167 compressedBuffUsed++;
174 kkuint32 compressedBytesNeeded = CalcCompressedBytesNeeded ();
176 if (compressedBytesNeeded > (256 * 256))
178 ofstream f (
"c:\\Temp\\SimpleCompressor.txt", ios_base::app);
180 << osGetLocalDateTime () <<
"\t" 181 <<
"SimpleCompressor::CreateCompressedBuffer ***ERROR*** compressedBytesNeeded[" << compressedBytesNeeded <<
"] to large." << std::endl
185 compressedBuffserSize = compressedBytesNeeded;
190 uchar* compressedBuff =
new uchar[compressedBytesNeeded];
193 AddByteToCmpressedBuffer (compressedBuff, compressedBuffUsed, compressedBytesNeeded / 256);
194 AddByteToCmpressedBuffer (compressedBuff, compressedBuffUsed, compressedBytesNeeded % 256);
198 while (idx < buffSpaceUsed)
200 while ((idx < buffSpaceUsed) && (buffLens[idx] > 1))
202 AddByteToCmpressedBuffer (compressedBuff, compressedBuffUsed, 128 + buffLens[idx]);
203 AddByteToCmpressedBuffer (compressedBuff, compressedBuffUsed, buffBytes[idx]);
207 while ((idx < buffSpaceUsed) && (buffLens[idx] <= 1))
214 while ((endIdx < buffSpaceUsed) && (buffLens[endIdx] <= 1) && (rawBytesInARow < 127))
223 AddByteToCmpressedBuffer (compressedBuff, compressedBuffUsed, 0 + rawBytesInARow);
224 for (
kkuint32 x = 0; x < rawBytesInARow; x++)
226 AddByteToCmpressedBuffer (compressedBuff, compressedBuffUsed, buffBytes[idx]);
232 if (compressedBuffUsed != compressedBytesNeeded)
235 ofstream f (
"c:\\Temp\\SimpleCompressor.txt", ios_base::app);
237 << osGetLocalDateTime () <<
"\t" <<
"SimpleCompressor::CreateCompressedBuffer ***ERROR***" 239 <<
" " <<
"\t" <<
"Something has gone terribly wrong with the simpleCompression algorithm." << std::endl
246 compressedBuffserSize = compressedBuffUsed;
248 return compressedBuff;
260 if ((compressedBuff == NULL) || ((compressedBuffLen < 2)))
264 kkuint32 compressedBuffSize = compressedBuff[0] * 256 + compressedBuff[1];
266 if (compressedBuffSize > compressedBuffLen)
268 ofstream f (
"c:\\Temp\\SimpleCompressor.txt", ios_base::app);
270 << osGetLocalDateTime () <<
"\t" 271 <<
"compressedBuffSize[" << compressedBuffSize <<
"] > compressedBuffLen[" << compressedBuffLen <<
"]" <<
"\t" 276 compressedBuffSize = compressedBuffLen;
282 unCompressedSize = 0;
283 while (nextByte < (compressedBuffSize - 1))
285 uchar controlByte = compressedBuff[nextByte];
288 if (controlByte < 128)
291 unCompressedSize = unCompressedSize + controlByte;
292 nextByte = nextByte + controlByte;
297 unCompressedSize = unCompressedSize + (controlByte % 128);
298 nextByte = nextByte + 1;
302 if (nextByte > compressedBuffSize)
305 ofstream f (
"c:\\Temp\\SimpleCompressor.txt", ios_base::app);
307 << osGetLocalDateTime () <<
"\t" 308 <<
"nextByte[" << nextByte <<
"] > compressedBuffSize[" << compressedBuffSize <<
"]" 314 uchar* unCompressedBuff =
new uchar[unCompressedSize];
315 memset (unCompressedBuff, 0, unCompressedSize);
321 while (nextByte < (compressedBuffSize - 1))
323 uchar controlByte = compressedBuff[nextByte];
324 if (controlByte < 128)
329 for (
kkint32 x = 0; (x < controlByte) && (nextByte < compressedBuffSize); x++)
331 unCompressedBuff[unCompressedBuffIDX] = compressedBuff[nextByte];
332 unCompressedBuffIDX++;
339 controlByte = controlByte % 128;
341 for (
kkint32 x = 0; x < controlByte; x++)
343 unCompressedBuff[unCompressedBuffIDX] = compressedBuff[nextByte];
344 unCompressedBuffIDX++;
350 if (unCompressedBuffIDX != unCompressedSize)
354 <<
"SimpleCompressor::Decompress ***ERROR***" << std::endl
356 <<
" Did not decompress correctly." << std::endl
359 return unCompressedBuff;
void Add16BitInt(kkuint32 i)
static uchar * Decompress(const uchar *compressedBuff, kkuint32 compressedBuffLen, kkuint32 &unCompressedSize)
unsigned __int32 kkuint32
SimpleCompressor(kkuint32 estimatedMaxBuffSize)
unsigned char uchar
Unsigned character.
uchar * CreateCompressedBuffer(kkuint32 &compressedBuffserSize)