00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
00028 #include <SFML/AdvancedSystem/Compressor.hpp>
00029 #include <zlib.h>
00030 #include <iostream>
00031
00032
00036 bool sfCompressor::Compress(const char* Source, std::size_t Size, unsigned int Level)
00037 {
00038
00039 if (!Source || !Size)
00040 {
00041 std::cerr << "Failed to uncompress data (invalid parameter)" << std::endl;
00042 return false;
00043 }
00044
00045
00046 if (!myData.empty() && (Source == GetData()))
00047 {
00048 std::cerr << "Failed to compress data (source buffer is the internal buffer)" << std::endl;
00049 return false;
00050 }
00051
00052
00053 if (Level > 9)
00054 Level = 9;
00055
00056
00057 uLong CompressedSize = compressBound(static_cast<uLong>(Size));
00058 myData.resize(CompressedSize);
00059
00060
00061 int Error = compress2(reinterpret_cast<Byte*>(&myData[0]), &CompressedSize, reinterpret_cast<const Byte*>(Source), static_cast<uLong>(Size), Level);
00062
00063
00064 if (Error != Z_OK)
00065 {
00066 std::cerr << "Failed to compress data (" << zError(Error) << ")" << std::endl;
00067 return false;
00068 }
00069
00070
00071 myData.resize(CompressedSize);
00072
00073 return true;
00074 }
00075
00076
00080 bool sfCompressor::Uncompress(const char* Source, std::size_t Size, std::size_t UncompressedSize)
00081 {
00082
00083 if (!Source || !Size || !UncompressedSize)
00084 {
00085 std::cerr << "Failed to uncompress data (invalid parameter)" << std::endl;
00086 return false;
00087 }
00088
00089
00090 if (!myData.empty() && (Source == GetData()))
00091 {
00092 std::cerr << "Failed to uncompress data (source buffer is the internal buffer)" << std::endl;
00093 return false;
00094 }
00095
00096
00097 uLong EstimatedSize = static_cast<uLong>(UncompressedSize);
00098 myData.resize(EstimatedSize);
00099
00100
00101 int Error = uncompress(reinterpret_cast<Byte*>(&myData[0]), &EstimatedSize, reinterpret_cast<const Byte*>(Source), static_cast<uLong>(Size));
00102
00103
00104 if (Error != Z_OK)
00105 {
00106 std::cerr << "Failed to compress data (" << zError(Error) << ")" << std::endl;
00107 return false;
00108 }
00109
00110
00111 myData.resize(EstimatedSize);
00112
00113 return true;
00114 }
00115
00116
00122 const char* sfCompressor::GetData() const
00123 {
00124 return myData.empty() ? NULL : &myData[0];
00125 }
00126
00127
00132 std::size_t sfCompressor::GetDataSize() const
00133 {
00134 return myData.size();
00135 }