SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataCompressor.cpp
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/DataCompressor.h"
17 #include "slikenet/DS_HuffmanEncodingTree.h"
18 #include "slikenet/assert.h"
19 #include <string.h> // Use string.h rather than memory.h for a console
20
21 using namespace SLNet;
22
23 STATIC_FACTORY_DEFINITIONS(DataCompressor,DataCompressor)
24
25 void DataCompressor::Compress( unsigned char *userData, unsigned sizeInBytes, SLNet::BitStream * output )
26 {
27 // Don't use this for small files as you will just make them bigger!
28 RakAssert(sizeInBytes > 2048);
29
30 unsigned int frequencyTable[ 256 ];
31 unsigned int i;
32 memset(frequencyTable,0,256*sizeof(unsigned int));
33 for (i=0; i < sizeInBytes; i++)
34 ++frequencyTable[userData[i]];
35 HuffmanEncodingTree tree;
36 BitSize_t writeOffset1, writeOffset2, bitsUsed1, bitsUsed2;
37 tree.GenerateFromFrequencyTable(frequencyTable);
38 output->WriteCompressed(sizeInBytes);
39 for (i=0; i < 256; i++)
40 output->WriteCompressed(frequencyTable[i]);
41 output->AlignWriteToByteBoundary();
42 writeOffset1=output->GetWriteOffset();
43 output->Write((unsigned int)0); // Dummy value
44 bitsUsed1=output->GetNumberOfBitsUsed();
45 tree.EncodeArray(userData, sizeInBytes, output);
46 bitsUsed2=output->GetNumberOfBitsUsed();
47 writeOffset2=output->GetWriteOffset();
48 output->SetWriteOffset(writeOffset1);
49 output->Write(bitsUsed2-bitsUsed1); // Go back and write how many bits were used for the encoding
50 output->SetWriteOffset(writeOffset2);
51 }
52
53 unsigned DataCompressor::DecompressAndAllocate(SLNet::BitStream * input, unsigned char **output )
54 {
55 HuffmanEncodingTree tree;
56 unsigned int bitsUsed, destinationSizeInBytes;
57 unsigned int decompressedBytes;
58 unsigned int frequencyTable[ 256 ];
59 unsigned i;
60
61 input->ReadCompressed(destinationSizeInBytes);
62 for (i=0; i < 256; i++)
63 input->ReadCompressed(frequencyTable[i]);
64 input->AlignReadToByteBoundary();
65 if (input->Read(bitsUsed)==false)
66 {
67 // Read error
68 #ifdef _DEBUG
69 RakAssert(0);
70 #endif
71 return 0;
72 }
73 *output = (unsigned char*) rakMalloc_Ex(destinationSizeInBytes, _FILE_AND_LINE_);
74 tree.GenerateFromFrequencyTable(frequencyTable);
75 decompressedBytes=tree.DecodeArray(input, bitsUsed, destinationSizeInBytes, *output );
76 RakAssert(decompressedBytes==destinationSizeInBytes);
77 return destinationSizeInBytes;
78 }