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