| 1 | |
| 2 | |
| 3 | /*************************************************************************** |
| 4 | * Copyright (C) 2006 by Dominik Seichter * |
| 5 | * [email protected] * |
| 6 | * * |
| 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program; if not, write to the * |
| 19 | * Free Software Foundation, Inc., * |
| 20 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 21 | ***************************************************************************/ |
| 22 | |
| 23 | #include "../PdfTest.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <cstdio> |
| 27 | |
| 28 | using namespace PoDoFo; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | const char pTestBuffer1[] = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; |
| 33 | |
| 34 | // We treat the buffer as _excluding_ the trailing \0 |
| 35 | const pdf_long lTestLength1 = strlen(pTestBuffer1); |
| 36 | |
| 37 | const char pTestBuffer2[] = { |
| 38 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, |
| 39 | static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x01, |
| 40 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, |
| 41 | static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x03, |
| 42 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, |
| 43 | static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x02, |
| 44 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, |
| 45 | static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x00, |
| 46 | 0x01, 0x64, 0x65, static_cast<char>(0xFE), 0x6B, static_cast<char>(0x80), 0x45, 0x32, |
| 47 | static_cast<char>(0x88), 0x12, static_cast<char>(0x71), static_cast<char>(0xEA), 0x00, |
| 48 | 0x00, 0x00, 0x00, 0x00, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 | }; |
| 50 | |
| 51 | const pdf_long lTestLength2 = 6*13; |
| 52 | |
| 53 | void test_filter( EPdfFilter eFilter, const char * pTestBuffer, const pdf_long lTestLength ) |
| 54 | { |
| 55 | char* pEncoded; |
| 56 | char* pDecoded; |
| 57 | pdf_long lEncoded; |
| 58 | pdf_long lDecoded; |
| 59 | |
| 60 | PODOFO_UNIQUEU_PTR<PdfFilter> pFilter( PdfFilterFactory::Create( eFilter ) ); |
| 61 | if( !pFilter.get() ) |
| 62 | { |
| 63 | printf("!!! Filter %i not implemented.\n", eFilter); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | printf("Testing Algorithm %i:\n", eFilter); |
| 68 | printf("\t-> Testing Encoding\n"); |
| 69 | try { |
| 70 | pFilter->Encode( pTestBuffer, lTestLength, &pEncoded, &lEncoded ); |
| 71 | } catch( PdfError & e ) { |
| 72 | if( e == ePdfError_UnsupportedFilter ) |
| 73 | { |
| 74 | printf("\t-> Encoding not supported for filter %i.\n", eFilter ); |
| 75 | return; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | e.AddToCallstack( __FILE__, __LINE__ ); |
| 80 | throw e; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | printf("\t-> Testing Decoding\n"); |
| 85 | try { |
| 86 | pFilter->Decode( pEncoded, lEncoded, &pDecoded, &lDecoded ); |
| 87 | } catch( PdfError & e ) { |
| 88 | if( e == ePdfError_UnsupportedFilter ) |
| 89 | { |
| 90 | printf("\t-> Decoding not supported for filter %i.\n", eFilter); |
| 91 | return; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | e.AddToCallstack( __FILE__, __LINE__ ); |
| 96 | throw e; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | printf("\t-> Original Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lTestLength) ); |
| 101 | printf("\t-> Encoded Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lEncoded) ); |
| 102 | printf("\t-> Decoded Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lDecoded) ); |
| 103 | |
| 104 | if( static_cast<pdf_long>(lTestLength) != lDecoded ) |
| 105 | { |
| 106 | fprintf( stderr, "Error: Decoded Length != Original Length\n"); |
| 107 | |
| 108 | /* |
| 109 | fprintf( stderr, "Data:\n%s\n", pEncoded ); |
| 110 | |
| 111 | fprintf( stderr, "DecodedData:\n%s\n", pDecoded ); |
| 112 | */ |
| 113 | |
| 114 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 115 | } |
| 116 | |
| 117 | if( memcmp( pTestBuffer, pDecoded, lTestLength ) != 0 ) |
| 118 | { |
| 119 | printf("\t-> Original Data: <%s>\n", pTestBuffer ); |
| 120 | printf("\t-> Encoded Data: <%s>\n", pEncoded ); |
| 121 | printf("\t-> Decoded Data: <%s>\n", pDecoded ); |
| 122 | |
| 123 | fprintf( stderr, "Error: Decoded Data does not match original data.\n"); |
| 124 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 125 | } |
| 126 | |
| 127 | printf("\t-> Test succeeded!\n"); |
| 128 | } |
| 129 | |
| 130 | void test_filter_queue( const char* pBuffer, long lLen ) |
| 131 | { |
| 132 | char* pEncoded; |
| 133 | pdf_long lEncoded; |
| 134 | |
| 135 | char* pDecoded; |
| 136 | pdf_long lDecoded; |
| 137 | |
| 138 | TVecFilters filters; |
| 139 | filters.push_back( ePdfFilter_ASCIIHexDecode ); |
| 140 | filters.push_back( ePdfFilter_ASCII85Decode ); |
| 141 | filters.push_back( ePdfFilter_FlateDecode ); |
| 142 | |
| 143 | printf("Testing queque of filters:\n"); |
| 144 | printf("\tePdfFilter_ASCIIHexDecode\n"); |
| 145 | printf("\tePdfFilter_ASCII85Decode\n"); |
| 146 | printf("\tePdfFilter_FlateDecode\n"); |
| 147 | |
| 148 | PdfMemoryOutputStream stream; |
| 149 | PdfOutputStream* pEncode = PdfFilterFactory::CreateEncodeStream( filters, &stream ); |
| 150 | |
| 151 | pEncode->Write( pBuffer, lLen ); |
| 152 | pEncode->Close(); |
| 153 | |
| 154 | delete pEncode; |
| 155 | |
| 156 | lEncoded = stream.GetLength(); |
| 157 | pEncoded = stream.TakeBuffer(); |
| 158 | |
| 159 | PdfMemoryOutputStream stream2; |
| 160 | PdfOutputStream* pDecode = PdfFilterFactory::CreateDecodeStream( filters, &stream2 ); |
| 161 | |
| 162 | pDecode->Write( pEncoded, lEncoded ); |
| 163 | pDecode->Close(); |
| 164 | |
| 165 | delete pDecode; |
| 166 | |
| 167 | lDecoded = stream2.GetLength(); |
| 168 | pDecoded = stream2.TakeBuffer(); |
| 169 | |
| 170 | printf("\t-> Original Data Length: %li\n", lLen ); |
| 171 | printf("\t-> Encoded Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lEncoded) ); |
| 172 | printf("\t-> Decoded Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lDecoded) ); |
| 173 | |
| 174 | if( lDecoded != lLen ) |
| 175 | { |
| 176 | fprintf( stderr, "Error: Decoded data length does not match original data length.\n"); |
| 177 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 178 | } |
| 179 | |
| 180 | if( memcmp( pBuffer, pDecoded, lLen ) != 0 ) |
| 181 | { |
| 182 | printf("\t-> Original Data: <%s>\n", pBuffer ); |
| 183 | printf("\t-> Encoded Data: \n<%s>\n", pEncoded ); |
| 184 | printf("\t-> Decoded Data: \n<%s>\n", pDecoded ); |
| 185 | |
| 186 | fprintf( stderr, "Error: Decoded Data does not match original data.\n"); |
| 187 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 188 | } |
| 189 | |
| 190 | free( pDecoded ); |
| 191 | free( pEncoded ); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | void test_stream( const char* pBuffer, pdf_long lLen ) |
| 196 | { |
| 197 | char* pDecoded; |
| 198 | pdf_long lDecoded; |
| 199 | |
| 200 | PdfObject object; |
| 201 | PdfMemStream stream( &object ); |
| 202 | |
| 203 | printf("Testing PdfStream:\n"); |
| 204 | |
| 205 | stream.Set( const_cast<char*>(pBuffer), lLen ); |
| 206 | stream.GetFilteredCopy( &pDecoded, &lDecoded ); |
| 207 | |
| 208 | printf("\t-> Original Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lLen) ); |
| 209 | printf("\t-> Encoded Data Length: %" PDF_FORMAT_UINT64 "\n", static_cast<pdf_uint64>(stream.GetLength()) ); |
| 210 | printf("\t-> Decoded Data Length: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lDecoded) ); |
| 211 | |
| 212 | if( lDecoded != lLen ) |
| 213 | { |
| 214 | fprintf( stderr, "Error: Decoded data length does not match original data length.\n"); |
| 215 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 216 | } |
| 217 | |
| 218 | if( memcmp( pBuffer, pDecoded, lLen ) != 0 ) |
| 219 | { |
| 220 | printf("\t-> Original Data: <%s>\n", pBuffer ); |
| 221 | printf("\t-> Decoded Data: \n<%s>\n", pDecoded ); |
| 222 | |
| 223 | fprintf( stderr, "Error: Decoded Data does not match original data.\n"); |
| 224 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 225 | } |
| 226 | |
| 227 | free( pDecoded ); |
| 228 | |
| 229 | |
| 230 | } |
| 231 | |
| 232 | } // end anon namespace |
| 233 | |
| 234 | int main() |
| 235 | { |
| 236 | printf("This test tests all filters of PoDoFo\n"); |
| 237 | printf("---\n"); |
| 238 | |
| 239 | printf("ePdfFilter_ASCIIHexDecode = 0\n"); |
| 240 | printf("ePdfFilter_ASCII85Decode = 1\n"); |
| 241 | printf("ePdfFilter_LZWDecode = 2\n"); |
| 242 | printf("ePdfFilter_FlateDecode = 3\n"); |
| 243 | printf("ePdfFilter_RunLengthDecode = 4\n"); |
| 244 | printf("ePdfFilter_CCITTFaxDecode = 5\n"); |
| 245 | printf("ePdfFilter_JBIG2Decode = 6\n"); |
| 246 | printf("ePdfFilter_DCTDecode = 7\n"); |
| 247 | printf("ePdfFilter_JPXDecode = 8\n"); |
| 248 | printf("ePdfFilter_Crypt = 9\n"); |
| 249 | |
| 250 | // Data from stream of obj 9 0 R |
| 251 | const char pszInputAscii85Lzw[] = "J..)6T`?q0\"W37&!thJ^C,m/iL/?:-g&uFOK1b,*F;>>qM[VuU#oJ230p2o6!o^dK\r=tpu7Tr'VZ1gWb9&Im[N#Q~>"; |
| 252 | |
| 253 | pdf_long lLargeBufer1 = strlen(pszInputAscii85Lzw) * 6; |
| 254 | pdf_long lLargeBufer2 = strlen(pszInputAscii85Lzw) * 6; |
| 255 | char* pLargeBuffer1 = static_cast<char*>(malloc( strlen(pszInputAscii85Lzw) * 6 )); |
| 256 | char* pLargeBuffer2 = static_cast<char*>(malloc( strlen(pszInputAscii85Lzw) * 6 )); |
| 257 | |
| 258 | try { |
| 259 | PODOFO_UNIQUEU_PTR<PdfFilter> pFilter( PdfFilterFactory::Create( ePdfFilter_ASCII85Decode ) ); |
| 260 | pFilter->Decode( pszInputAscii85Lzw, strlen(pszInputAscii85Lzw), |
| 261 | &pLargeBuffer1, &lLargeBufer1 ); |
| 262 | pFilter->Encode( pLargeBuffer1, lLargeBufer1, |
| 263 | &pLargeBuffer2, &lLargeBufer2 ); |
| 264 | |
| 265 | if( memcmp( pszInputAscii85Lzw, pLargeBuffer2, lLargeBufer2 ) != 0 ) |
| 266 | { |
| 267 | printf("\tROACH -> Original Data: <%s>\n", pszInputAscii85Lzw ); |
| 268 | printf("\tROACH -> Encoded Data: <%s>\n", pLargeBuffer1 ); |
| 269 | printf("\tROACH -> Decoded Data: <%s>\n", pLargeBuffer2 ); |
| 270 | |
| 271 | fprintf( stderr, "Error: Decoded Data does not match original data.\n"); |
| 272 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 273 | } |
| 274 | |
| 275 | if( static_cast<pdf_long>(strlen(pszInputAscii85Lzw)) != lLargeBufer2 ) |
| 276 | { |
| 277 | fprintf( stderr, "ROACH Error: Decoded Length != Original Length\n"); |
| 278 | fprintf( stderr, "ROACH Original: %" PDF_FORMAT_UINT64 "\n", static_cast<pdf_uint64>(strlen(pszInputAscii85Lzw)) ); |
| 279 | fprintf( stderr, "ROACH Encode: %" PDF_FORMAT_INT64 "\n", static_cast<pdf_int64>(lLargeBufer2) ); |
| 280 | PODOFO_RAISE_ERROR( ePdfError_TestFailed ); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | // ASCII 85 decode and re-encode delivers same results |
| 285 | printf("ROACH ASCII encode/decode OK\n"); |
| 286 | |
| 287 | for( int i =0; i<=ePdfFilter_Crypt; i++ ) |
| 288 | { |
| 289 | test_filter( static_cast<EPdfFilter>(i), pTestBuffer1, lTestLength1 ); |
| 290 | test_filter( static_cast<EPdfFilter>(i), pTestBuffer2, lTestLength2 ); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | test_filter_queue( pTestBuffer1, lTestLength1 ); |
| 295 | test_filter_queue( pTestBuffer2, lTestLength2 ); |
| 296 | |
| 297 | test_stream( pTestBuffer1, lTestLength1 ); |
| 298 | test_stream( pTestBuffer2, lTestLength2 ); |
| 299 | |
| 300 | } catch( PdfError & e ) { |
| 301 | e.PrintErrorMsg(); |
| 302 | return e.GetError(); |
| 303 | } |
| 304 | |
| 305 | printf("All tests sucessfull!\n"); |
| 306 | return 0; |
| 307 | } |