Octane v1.01.20 - The Open Compression Toolkit for C++ | http://octane.sourceforge.net/ |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 //--------------------------------------------------------------------------- 00013 00014 //--------------------------------------------------------------------------- 00015 // application includes 00016 #include "samplecompressor.hpp" 00017 // system includes 00018 #include <iomanip> 00019 using namespace std; 00020 //--------------------------------------------------------------------------- 00021 00022 00023 //--------------------------------------------------------------------------- 00024 // Create a global instance in order to register it automatically with the global manager 00025 SampleCompressor GloballyInstantiated_SampleCompressor(true); 00026 //--------------------------------------------------------------------------- 00027 00028 00029 //--------------------------------------------------------------------------- 00030 // Constructor 00031 SampleCompressor::SampleCompressor(bool registerme) 00032 :OctaneCompressor(registerme) 00033 { 00034 // REGISTER WITH COMPRESSOR MANAGER 00035 // this use a trick to make sure the global manager is ready for registration 00036 // and at the same time automatically register this instance with the global manager 00037 // note that the insurer is just temporary, and deletes when we exit the procedure 00038 if (registerme) 00039 CompressorManager_SingletonInsurer managerinsurance(this); 00040 00041 // set default parameters for the compressor 00042 SetDefaultParameters(); 00043 } 00044 //--------------------------------------------------------------------------- 00045 00046 00047 00048 //--------------------------------------------------------------------------- 00049 // OCTANE PUBLIC API - AUXILIARY FUNCTIONS - these are supported by all derived classes 00050 void SampleCompressor::ShowDebuggingInfo() 00051 { 00052 // OPTIONAL: Here we can show any information that could be useful during testing. 00053 // If this function is not defined, nothing is shown, but we implement it here for fun. 00054 cout << "This is the sample compressor, there is no useful debugging information to display. "; 00055 cout << "But for fun, i'll tell you that this compressor lives at the following memory address: "<< (void *)this<<endl; 00056 } 00057 00058 unsigned int SampleCompressor::GetMemoryUsed() 00059 { 00060 // here we return an estimate of the memory used by this compressor 00061 return sizeof(this); 00062 } 00063 00064 unsigned int SampleCompressor::GetDiskspaceUsed(bool fortempdecompressiononly) 00065 { 00066 // Here we return an estimate of the disk space used to save our state 00067 // in this case, we have a single char variable which makes up our state 00068 // The flag fortempdecompressiononly will be set if the state is being saved for decompression *only* 00069 // see SaveState() for how this is used, and adjust disk space appropriately if you save different information. 00070 return sizeof(Parameter_xorbyte); 00071 } 00072 00073 std::string SampleCompressor::GetParametersInformation() 00074 { 00075 // Show user available variables and their current values 00076 // ATTN: this has not been converted from cout to string yet, so just send output to cout and return an empty string 00077 cout << setiosflags(ios::left) << setw(17) << "xorbyte" << setiosflags(ios::left) << " " << setw(10) << (int)Parameter_xorbyte << " " << "byte (0-255) to xor text with during processing" << endl; 00078 return ""; 00079 } 00080 00081 void SampleCompressor::SetDefaultParameters() 00082 { 00083 // Set parameters to their defaults 00084 Parameter_xorbyte=65; 00085 } 00086 00087 bool SampleCompressor::SetParameter(const std::string ¶metername,const std::string ¶metervalue) 00088 { 00089 // Generic function for user-interactive modification of parameters 00090 // Return true if we know this variable and use it 00091 bool bretv=false; 00092 00093 // we have 1 variable we use, called xorbyte 00094 if (parametername=="xorbyte") 00095 bretv=ParseParameter(parametervalue,Parameter_xorbyte); 00096 00097 // return success 00098 return bretv; 00099 } 00100 //--------------------------------------------------------------------------- 00101 00102 00103 //--------------------------------------------------------------------------- 00104 // COMPRESSOR PUBLIC API - PREPARATION FOR COMPRESSING/DECOMPRESSING 00105 bool SampleCompressor::IsReadyToCompress() 00106 { 00107 // Are we ready to compress? 00108 // This sample compressor is always ready (needs no training) 00109 return true; 00110 } 00111 //--------------------------------------------------------------------------- 00112 00113 00114 00115 //--------------------------------------------------------------------------- 00116 // COMPRESSOR DERIVED COMPRESSION/DECOMPRESSING - implement these in derived classes 00117 bool SampleCompressor::DoProtectedCompress(bitreader &from, bitwriter &to) 00118 { 00119 // Here is the main compression routine 00120 // Our sample compressor doesn't actually compress, instead it just xors 00121 00122 // loop through input and 'compress' (xor) it 00123 unsigned char c; 00124 while (!from.empty()) 00125 { 00126 // grab a character from input 00127 c=from.get_byte(); 00128 // xor it with our parameter 00129 c= c ^ Parameter_xorbyte; 00130 // send it to output 00131 to.put_byte(c); 00132 } 00133 00134 // return success 00135 return true; 00136 } 00137 00138 00139 bool SampleCompressor::DoProtectedDecompress(bitreader &from, bitwriter &to) 00140 { 00141 // Here is the main decompression routine 00142 // Our sample compressor doesn't actually decompress, instead it just xors 00143 00144 // loop through input and 'compress' (xor) it 00145 unsigned char c; 00146 while (!from.empty()) 00147 { 00148 // grab a character from input 00149 c=from.get_byte(); 00150 // xor it with our parameter 00151 c= Parameter_xorbyte ^ c; 00152 // send it to output 00153 to.put_byte(c); 00154 } 00155 00156 // return success 00157 return true; 00158 } 00159 //--------------------------------------------------------------------------- 00160 00161 00162 00163 //--------------------------------------------------------------------------- 00164 // OCTANE PROTECTED DERIVED STATE LOADING AND SAVING 00165 bool SampleCompressor::DoProtectedCreateSymbolsAndModelsUsingStream(bitreader &from) 00166 { 00167 // OPTIONAL: this is called to 'train' a compressor, to let a compressor gather statistics from an input stream. 00168 // Our sample compressor does not need to implement this, but we include it here just for fun so you see what it looks like. 00169 // return false only on an error. 00170 00171 // let's read the training input stream, just to show you how its done. 00172 unsigned char c; 00173 while (!from.empty()) 00174 { 00175 // grab a character, but we dont do anything with it. 00176 c=from.get_byte(); 00177 } 00178 00179 // return success 00180 return true; 00181 } 00182 00183 bool SampleCompressor::DoProtectedSaveState(bitwriter &to,bool fortempdecompressiononly) 00184 { 00185 // OPTIONAL: save any compressor internal state (like variables or learned statistics). 00186 // Many compressors have no interenal state to save, but this sample compressor has a variable 00187 // which should be saved. 00188 // The flag fortempdecompressiononly will be set if the state is being saved for decompression *only* 00189 // so that if there is some information is only needed for compression, we dont have to save it if we dont need it. 00190 00191 // just tell the bitwriter to save the variables 00192 to.put(Parameter_xorbyte); 00193 00194 // return true on success 00195 return true; 00196 } 00197 00198 bool SampleCompressor::DoProtectedLoadState(bitreader &from) 00199 { 00200 // OPTIONAL: load any compressor internal state (like variables or learned statistics). 00201 // Many compressors have no interenal state to save, but this sample compressor has a variable 00202 // which should be saved. 00203 00204 // just tell the bitwriter to save the variables 00205 from.get(Parameter_xorbyte); 00206 00207 // return true on success 00208 return true; 00209 } 00210 //---------------------------------------------------------------------------