summaryrefslogtreecommitdiffstats
path: root/libkmime/tests/test_kmime_codec.cpp
blob: 7417c8aea20951329f1df4a47e67ee7e616e9536 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* test program for KMime::Codec's:
   compile with:
   g++ -I$TQTDIR/include -I$TDEDIR/include -L$TQTDIR/lib -L$TDEDIR/lib \
   -lqt-mt -ltdecore -ltdenetwork -O2 -pthread -DTQT_THREAD_SUPPORT \
   -o test_kmime_codec{,.cpp}
*/

// return codes:
#define USAGE_DISPLAYED 1
#define UNKNOWN_CODEC 2
#define INFILE_READ_ERR 3
#define OUTFILE_WRITE_ERR 4

#include <../kmime_codecs.h>

#include <kdebug.h>

#include <cstdlib>
#include <iostream>

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <getopt.h>
#include <cassert>

#include <tqfile.h>
#include <tqcstring.h> // TQByteArray

using namespace KMime;
using namespace std;

static struct option long_options[] = {
  { "encode", 1, 0, 0 },
  { "decode", 1, 0, 0 },
  { "output-buffer-size", 1, 0, 0 },
  { "input-buffer-size", 1, 0, 0 },
  { "outfile", 1, 0, 0 },
  { "with-crlf", 0, 0, 0 },
  { "iterations", 1, 0, 0 },
  { "without-finish", 0, 0, 0 },
  { "verbose", 0, 0, 0 },
  { "usage-pattern", 1, 0, 0 },
  { 0, 0, 0, 0 }
};

void usage( const char * msg=0 ) {
  if ( msg && *msg )
    cerr << msg << endl;
  cerr << "usage: test_kmime_codec (--encode|--decode) "
    "<encoding-name> [options] infile\n"
    "where options include:\n\n"
    " --outfile <outfile>          write output into file <outfile>\n"
    " --output-buffer-size <size>  en/decode into chunks of <size> bytes\n"
    "                              default: 4096\n"
    " --input-buffer-size <size>   en/decode from chunks of <size> bytes\n"
    "                              default: slurp in whole file\n"
    " --with-crlf                  use CRLF instead of LF in output\n"
    " --iterations <number>        do more than one iteration\n"
    "                              default: 1\n"
    " --usage-pattern { kio | chunkwise | convenience-qba }\n"
    "                              use a certain usage pattern to be tested\n"
    "                              (default: chunkwise)\n"
    " --without-finish             don't call the finish() method\n"
    " --verbose                    output detailed progress information\n"
       << endl;
  exit(USAGE_DISPLAYED);
}

void missingParameterTo( const char * option ) {
  cerr << "Missing or malformed parameter to " << option << endl;
  usage();
}

static enum { Kio = 0, ChunkWise = 1, ConvenienceTQBA = 3 }
pattern = ChunkWise;
static int outbufsize = 4096;
static int inbufsize = -1; // whole file
static bool writing = false;
static bool withCRLF = false;
static bool withFinish = true;
static bool verbose = false;

void encode_decode_kio( bool, const Codec *, const TQByteArray &, TQFile & );
void encode_decode_chunkwise( bool, const Codec *,
			      const TQByteArray &, TQFile & );
void encode_decode_convenience_qba( bool, const Codec *, const TQByteArray &,
				    TQFile & );

int main( int argc, char * argv[] ) {

  int iterations = 1;
  bool encode = false;
  bool decode = false;
  TQCString outfilename, infilename;
  TQCString encodingName;

  // options parsing:
  while( 1 ) {
    int option_index = 0;
    if ( getopt_long( argc, argv, "", long_options, &option_index ) )
      break;
    switch ( option_index ) {
    case 0: // encode
      if ( !optarg || !*optarg ) missingParameterTo( "--encode." );
      encode = true;
      encodingName = TQCString(optarg);
      break;
    case 1: // decode
      if ( !optarg || !*optarg ) missingParameterTo( "--decode" );
      decode = true;
      encodingName = TQCString(optarg);
      break;
    case 2: // output-buffer-size
      if ( !optarg || (outbufsize = atoi( optarg )) < 1 )
	missingParameterTo( "--output-buffer-size" );
      break;
    case 3: // input-buffer-size
      if ( !optarg || (inbufsize = atoi( optarg )) < 1 )
	missingParameterTo( "--input-buffer-size" );
      break;
    case 4: // outfile
      if ( !optarg || !*optarg ) missingParameterTo( "--outfile" );
      outfilename = TQCString(optarg);
      writing = true;
      break;
    case 5: // with-crlf
      withCRLF = true;
      break;
    case 6: // iterations
      if ( !optarg || (iterations = atoi( optarg )) < 1 )
	missingParameterTo( "--iterations" );
      break;
    case 7: // without-finish
      withFinish = false;
      break;
    case 8: // verbose
      verbose = true;
      break;
    case 9: // usage-pattern
      if ( !tqstricmp( "kio", optarg ) )
	pattern = Kio;
      else if ( !tqstricmp( "chunkwise", optarg ) )
	pattern = ChunkWise;
      else if ( !tqstricmp( "convenience-qba", optarg ) )
	pattern = ConvenienceTQBA;
      else {
	cerr << "Unknown usage pattern \"" << optarg << "\"" << endl;
	usage();
      }
      break;
    default: usage( "Unknown option" );
    }
  }

  if ( !decode && !encode )
    usage( "You must specify exactly one of --encode, --decode." );
  if ( decode && encode )
    usage( "You must specify exactly one of --encode, --decode.");

  if ( verbose ) {
    if ( encode )
      kdDebug() << "encoding as " << encodingName << endl;
    else if ( decode )
      kdDebug() << "decoding " << encodingName << endl;
  }

  if ( optind != argc - 1 ) usage();

  TQFile infile( argv[ optind ] );
  if (!infile.exists()) {
    kdDebug() << "infile \"" << infile.name() << "\" does not exist!" << endl;
    return INFILE_READ_ERR;
  }
  if (!infile.open( IO_ReadOnly )) {
    kdDebug() << "cannot open " << infile.name() << " for reading!"
	      << endl;
    return INFILE_READ_ERR;
  }

  TQFile outfile( outfilename );
  if ( !outfilename.isEmpty() ) {
    if (!outfile.open( IO_WriteOnly|IO_Truncate )) {
      kdDebug() << "cannot open " << outfile.name() << " for writing!"
		<< endl;
      return OUTFILE_WRITE_ERR;
    }
  }

  if ( verbose ) {
    kdDebug() << "using output buffer size of " << outbufsize << endl;
    kdDebug() << "using  input buffer size of " << inbufsize << endl;
  }
  if ( !withFinish )
    kdWarning() << "omitting finish calls. Results may be truncated!" << endl;

  if ( inbufsize <= 0 )
    inbufsize = infile.size();

  // get a codec. Don't delete it later!!
  kdDebug( verbose ) << "obtaining codec for \""
		     << encodingName << "\"" << endl;
  Codec * codec = Codec::codecForName( encodingName );
  if ( !codec ) {
    kdDebug() << "unknown codec \"" << encodingName << "\"" << endl;
    return UNKNOWN_CODEC;
  }

  TQByteArray infile_buffer = infile.readAll();

  for ( int i = 0 ; i < iterations ; ++i ) {
    kdDebug( verbose ) << "starting iteration " << i+1
		       << " of " << iterations << endl;
    switch ( pattern ) {
    case ChunkWise:
      encode_decode_chunkwise( encode, codec, infile_buffer, outfile );
      break;
    case Kio:
      encode_decode_kio( encode, codec, infile_buffer, outfile );
      break;
    case ConvenienceTQBA:
      encode_decode_convenience_qba( encode, codec, infile_buffer, outfile );
      break;
    default:
      usage();
    }
  }

  return 0;
}

void encode_decode_convenience_qba( bool encode, const Codec * codec,
				    const TQByteArray & infile_buffer,
				    TQFile & outfile )
{
  TQByteArray out;
  if ( encode )
    out = codec->encode( infile_buffer, withCRLF );
  else
    out = codec->decode( infile_buffer, withCRLF );
  if ( writing ) {
    TQ_LONG written = outfile.writeBlock( out );
    assert( written == (TQ_LONG)out.size() );
  }
}

void encode_kio_internal( Encoder * enc, TQByteArray::ConstIterator & iit,
			  TQByteArray::ConstIterator & iend,
			  TQByteArray & out )
{
  out.resize( outbufsize );
  TQByteArray::Iterator oit = out.begin();
  TQByteArray::ConstIterator oend = out.end();

  while ( !enc->encode( iit, iend, oit, oend ) )
    if ( oit == oend ) return;

  while ( !enc->finish( oit, oend ) )
    if ( oit == oend ) return;

  out.truncate( oit - out.begin() );
}

void decode_kio_internal( Decoder * dec, TQByteArray::ConstIterator & iit,
			  TQByteArray::ConstIterator & iend,
			  TQByteArray & out ) {
  out.resize( outbufsize );
  TQByteArray::Iterator oit = out.begin();
  TQByteArray::ConstIterator oend = out.end();

  while ( !dec->decode( iit, iend, oit, oend ) )
    if ( oit == oend ) return;

  while ( !dec->finish( oit, oend ) )
    if ( oit == oend ) return;

  out.truncate( oit - out.begin() );
}

void encode_decode_kio( bool encode, const Codec * codec,
			const TQByteArray & infile_buffer, TQFile & outfile )
{

  Encoder * enc = 0;
  Decoder * dec = 0;

  // Get an encoder. This one you have to delete!
  if ( encode ) {
    enc = codec->makeEncoder( withCRLF );
    assert( enc );
  } else {
    dec = codec->makeDecoder( withCRLF );
    assert( dec );
  }

  TQByteArray::ConstIterator iit = infile_buffer.begin();
  TQByteArray::ConstIterator iend = infile_buffer.end();

  TQByteArray out;
  do {
    out = TQByteArray();
    if ( encode )
      encode_kio_internal( enc, iit, iend, out );
    else
      decode_kio_internal( dec, iit, iend, out );
    if ( writing && out.size() ) {
      TQ_LONG written = outfile.writeBlock( out );
      assert( written == (TQ_LONG)out.size() );
    }
  } while ( out.size() );

  if ( encode )
    delete enc;
  else
    delete dec;
}

void encode_decode_chunkwise( bool encode, const Codec * codec,
			      const TQByteArray & infile_buffer, TQFile & outfile )
{
  Encoder * enc = 0;
  Decoder * dec = 0;


  TQByteArray indata( inbufsize );
  TQByteArray outdata( outbufsize );

  // we're going to need this below:
#define write_full_outdata_then_reset  do { \
     kdDebug( verbose ) << "  flushing output buffer." << endl; \
     if ( writing ) { \
       TQ_LONG outlen = outfile.writeBlock( outdata.data(), \
					   outdata.size() ); \
       if ( outlen != (int)outdata.size() ) \
         exit(OUTFILE_WRITE_ERR); \
     } \
     oit = outdata.begin(); \
   } while ( false )

#define report_status(x,y) do { \
     kdDebug( verbose ) << "  " #x "() returned " #y " after processing " \
                        << iit - indata.begin() << " bytes of input.\n" \
			<< "   output iterator now at position " \
			<< oit - outdata.begin() << " of " \
			<< outdata.size() << endl; \
  } while ( false )

#define report_finish_status(y) do { \
     kdDebug( verbose ) << "  finish() returned " #y "\n" \
			<< "   output iterator now at position " \
			<< oit - outdata.begin() << " of " \
			<< outdata.size() << endl; \
  } while ( false )


  // Initialize the output iterators:
  TQByteArray::Iterator oit = outdata.begin();
  TQByteArray::Iterator oend = outdata.end();

  // Get an encoder. This one you have to delete!
  if ( encode ) {
    enc = codec->makeEncoder( withCRLF );
    assert( enc );
  } else {
    dec = codec->makeDecoder( withCRLF );
    assert( dec );
  }

  //
  // Loop over input chunks:
  //
  uint offset = 0;
  while ( offset < infile_buffer.size() ) {
    uint reallyRead = TQMIN( indata.size(), infile_buffer.size() - offset );
    indata.duplicate( infile_buffer.begin() + offset, reallyRead );
    offset += reallyRead;

    kdDebug( verbose ) << " read " << reallyRead << " bytes (max: "
		       << indata.size() << ") from input." << endl;
    
    // setup input iterators:
    TQByteArray::ConstIterator iit = indata.begin();
    TQByteArray::ConstIterator iend = indata.begin() + reallyRead;
    
    if ( encode ) {
      //
      // Loop over encode() calls:
      //
      while ( !enc->encode( iit, iend, oit, oend ) ) {
	report_status( encode, false );
	if ( oit == oend )
	  // output buffer full:
	  write_full_outdata_then_reset;
      }
      report_status( encode, true );
    } else {
      //
      // Loop over decode() calls:
      //
      while ( !dec->decode( iit, iend, oit, oend ) ) {
	report_status( decode, false );
	if ( oit == oend )
	  // output buffer full:
	  write_full_outdata_then_reset;
      }
      report_status( decode, true );
    }
  } // end loop over input chunks

  //
  // Now finish the encoding/decoding:
  // (same loops as above, just s/encode|decode/finish())
  //
  if ( withFinish )
    if ( encode ) {
      while ( !enc->finish( oit, oend ) ) {
	report_finish_status( false );
	if ( oit == oend )
	  write_full_outdata_then_reset;
      }
      report_finish_status( true );
    } else {
      while ( !dec->finish( oit, oend ) ) {
	report_finish_status( false );
	if ( oit == oend )
	  write_full_outdata_then_reset;
      }
      report_finish_status( true );
    }
  
  //
  // Write out last (partial) output chunk:
  //
  if ( writing ) {
    TQ_LONG outlen = outfile.writeBlock( outdata.data(),
					oit - outdata.begin() );
    if ( outlen != oit - outdata.begin() )
      exit(OUTFILE_WRITE_ERR);
  }
  
  //
  // Delete en/decoder:
  //
  if ( encode )
    delete enc;
  else
    delete dec;
}