summaryrefslogtreecommitdiff
path: root/libextra/opencdk/stream.c
blob: c158efe8d41bf878cb16e68898a51cc90db78c8e (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
/* -*- Mode: C; c-file-style: "bsd" -*-
 * stream.c - provides a STREAM object
 *        Copyright (C) 2002, 2003 Timo Schulz
 *
 * This file is part of OpenCDK.
 *
 * OpenCDK 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.
 *
 * OpenCDK 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 OpenCDK; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include "opencdk.h"
#include "main.h"
#include "filters.h"
#include "stream.h"
#include "types.h"


static int stream_flush( cdk_stream_t s );
static int stream_filter_write( cdk_stream_t s );
static int stream_cache_flush( cdk_stream_t s, FILE * fp );


/**
 * cdk_stream_open: create a new stream based on an existing file.
 * @file: The file to open
 * @ret_s: The new STREAM object
 **/
cdk_error_t
cdk_stream_open( const char * file, cdk_stream_t * ret_s )
{
    cdk_stream_t s;

    if( !file || !ret_s )
        return CDK_Inv_Value;

    _cdk_log_debug( "open stream `%s'\n", file );
    *ret_s = NULL;
    s = cdk_calloc( 1, sizeof *s );
    if( !s )
        return CDK_Out_Of_Core;
    s->fname = cdk_strdup( file );
    if( !s->fname ) {
        cdk_free( s );
        return CDK_Out_Of_Core;
    }
    s->fp = fopen( file, "rb" );
    if( !s->fp ) {
        cdk_free( s->fname );
        cdk_free( s );
        return CDK_File_Error;
    }
    s->flags.write = 0;
    *ret_s = s;
    return 0;
}


/**
 * cdk_stream_new: Create a new stream into into the given file.
 * @file: The name of the new file
 * @ret_s: The new STREAM object
 **/
cdk_error_t
cdk_stream_new( const char * file, cdk_stream_t * ret_s )
{
    cdk_stream_t s;

    if( !ret_s )
        return CDK_Inv_Value;

    _cdk_log_debug( "new stream `%s'\n", file? file : "[temp]" );
    *ret_s = NULL;
    s = cdk_calloc( 1, sizeof *s );
    if( !s )
        return CDK_Out_Of_Core;  
    s->flags.write = 1;
    if( !file )
        s->flags.temp = 1;
    else {
        s->fname = cdk_strdup( file );
        if( !s->fname ) {
            cdk_free( s );
            return CDK_Out_Of_Core;
        }
    }
    s->fp = tmpfile( );
    if( !s->fp ) {
        cdk_free( s->fname );
        cdk_free( s );
        return CDK_File_Error;
    }
    *ret_s = s;
    return 0;
}


/**
 * cdk_stream_create: create a new stream.
 * @file: the filename
 * @ret_s: the object
 *
 * The difference to cdk_stream_new is, that no filtering can be used with
 * this kind of stream and everything is written directly to the stream.
 **/
cdk_error_t
cdk_stream_create( const char * file, cdk_stream_t * ret_s )
{
    cdk_stream_t s;

    if( !file || !ret_s )
        return CDK_Inv_Value;
    
    _cdk_log_debug( "create stream `%s'\n", file );
    *ret_s = NULL;
    s = cdk_calloc( 1, sizeof * s );
    if( !s )
        return CDK_Out_Of_Core;
    s->flags.write = 1;
    s->flags.filtrated = 1;
    s->fname = cdk_strdup( file );
    if( !s->fname ) {
        cdk_free( s );
        return CDK_Out_Of_Core;
    }
    s->fp = fopen( file, "w+b" );
    if( !s->fp ) {
        cdk_free( s->fname );
        cdk_free( s );
        return CDK_Out_Of_Core;
    }
    *ret_s = s;
    return 0;
}
    

cdk_stream_t
cdk_stream_tmp( void )
{
    cdk_stream_t s;
    int rc = cdk_stream_new( NULL, &s );
    if( !rc )
        return s;
    return NULL;
}


cdk_stream_t
cdk_stream_tmp_from_mem( const void * buf, size_t count )
{
    cdk_stream_t s;
    int nwritten;

    s = cdk_stream_tmp( );
    if( !s )
        return NULL;
  
    nwritten = cdk_stream_write( s, buf, count );
    if( nwritten == EOF ) {
        cdk_stream_close( s );
        return NULL;
    }
    cdk_stream_seek( s, 0 );
    return s;
}

  

cdk_stream_t
_cdk_stream_fpopen( FILE * fp, unsigned write_mode )
{
  cdk_stream_t s;

  s = cdk_calloc( 1, sizeof *s );
  if( !s )
    return NULL;
  
  s->fp = fp;
  s->flags.filtrated = 1;
  s->flags.write = write_mode;

  return s;
}


cdk_error_t
_cdk_stream_append( const char * file, cdk_stream_t * ret_s )
{
    cdk_stream_t s;
    FILE * fp;
    int rc;

    if( !ret_s )
        return CDK_Inv_Value;
    rc = cdk_stream_open( file, &s );
    if( rc )
        return rc;
    fp = fopen( file, "a+b" );
    if( !fp ) {
        cdk_stream_close( s );
        return CDK_File_Error;
    }
    fclose( s->fp );
    s->fp = fp;
    s->flags.write = 1;
    *ret_s = s;
    return 0;
}

      
int
cdk_stream_control( cdk_stream_t s, int ctl, int val )
{
    if( !s )
        return CDK_Inv_Value;

    if( val == -1 ) {
        switch( ctl ) {
        case CDK_STREAMCTL_COMPRESSED: return s->flags.compressed;
        }
        return 0;
    }
    switch( ctl ) {
    case CDK_STREAMCTL_DISABLE: s->flags.no_filter = val; break;
    case CDK_STREAMCTL_COMPRESSED: s->flags.compressed = val; break;
    default                   : return CDK_Inv_Mode;
    }
    return 0;
}


cdk_error_t
cdk_stream_flush( cdk_stream_t s )
{
    int rc = 0;
  
    if( !s )
        return CDK_Inv_Value;

    if( !s->flags.filtrated ) {
        if( !cdk_stream_get_length( s ) )
            return 0;
        rc = cdk_stream_seek( s, 0 );
        if( !rc )
            rc = stream_flush( s );
        if( !rc ) {
            rc = stream_filter_write( s );
            if( rc )
                s->error = rc;
        }
        s->flags.filtrated = 1;
    }
    return rc;
}


void
cdk_stream_tmp_set_mode( cdk_stream_t s, int val )
{
    if( s && s->flags.temp )
        s->fmode = val;
}


/**
 * cdk_stream_close: Close a stream and flush all buffers.
 * @s: The STREAM object.
 *
 * This function work different for read or write streams. When the
 * stream is for reading, the filtering is already done and we can
 * simply close the file and all buffers.
 * But for the case it's a write stream, we need to apply all registered
 * filters now. The file is closed in the filter function and not here.
 **/
cdk_error_t
cdk_stream_close( cdk_stream_t s )
{
    struct stream_filter_s * f, * f2;
    int rc = 0;

    if( !s )
        return CDK_Inv_Value;
    
    _cdk_log_debug( "close stream `%s'\n", s->fname? s->fname : "[temp]" );
    
    if( !s->flags.filtrated && !s->error )
        rc = cdk_stream_flush( s );
    if( s->fname || s->flags.temp ) {
        rc = fclose( s->fp );
        s->fp = NULL;
        if( rc )
            rc = CDK_File_Error;
    }
    f = s->filters;
    while( f ) {
        f2 = f->next;
        if( f->fnct )
            f->fnct( f->opaque, STREAMCTL_FREE, NULL, NULL );
        cdk_free( f );
        f = f2;
    }
    if( s->fname ) {
        cdk_free( s->fname );
        s->fname = NULL;
    }
    cdk_free( s );
    return rc;
}


/**
 * cdk_stream_eof: Return if the associated file handle was set to EOF.
 * @s: The STREAM object.
 *
 * This function will only work with read streams.
 **/
int
cdk_stream_eof( cdk_stream_t s )
{
    return s? s->flags.eof : -1;
}


const char *
_cdk_stream_get_fname( cdk_stream_t s )
{
    return s? s->fname : NULL;
}


FILE *
_cdk_stream_get_fp( cdk_stream_t s )
{
    return s? s->fp : NULL;
}


int
_cdk_stream_get_errno( cdk_stream_t s )
{
    return s? s->error : CDK_Inv_Value;
}


/**
 * cdk_stream_get_length: Return the length of the associated file handle.
 * @s: The STREAM object.
 *
 * This file only works for read stream because it's likely that the
 * write stream is not flushed or even no data was inserted.
 **/
unsigned
cdk_stream_get_length( cdk_stream_t s )
{
    struct stat statbuf;
    int rc;

    if( !s )
        return (unsigned)-1;
    
    rc = stream_flush( s );
    if( rc ) {
        s->error = CDK_File_Error;
        return (unsigned)-1;
    }
    if( fstat( fileno( s->fp ), &statbuf ) ) {
        s->error = CDK_File_Error;
        return (unsigned)-1;
    }
    return statbuf.st_size;
}


static struct stream_filter_s *
filter_add2( cdk_stream_t s )
{
    struct stream_filter_s * f;

    assert( s );
    
    f = cdk_calloc( 1, sizeof *f );
    if( !f )
        return NULL;
    f->next = s->filters;
    s->filters = f;
    return f;
}


static struct stream_filter_s *
filter_search( cdk_stream_t s, filter_fnct_t fnc )
{
    struct stream_filter_s * f;

    assert( s );
    
    for( f = s->filters; f; f = f->next ) {
        if( f->fnct == fnc )
            return f;
    }
    return NULL;
}


struct stream_filter_s *
filter_add( cdk_stream_t s, filter_fnct_t fnc, int type )
{
    struct stream_filter_s * f;

    assert( s );
    
    s->flags.filtrated = 0;
    f = filter_search( s, fnc );
    if( f )
        return f;
    f = filter_add2( s );
    if( !f )
        return NULL;
    f->fnct = fnc;
    f->flags.enabled = 1;
    f->tmp = NULL;
    f->type = type;
    switch( type ) {
    case fARMOR    : f->opaque = &f->u.afx; break;
    case fCIPHER   : f->opaque = &f->u.cfx; break;
    case fLITERAL: f->opaque = &f->u.pfx; break;
    case fCOMPRESS : f->opaque = &f->u.zfx; break;
    case fHASH     : f->opaque = &f->u.mfx; break;
    case fTEXT     : f->opaque = &f->u.tfx; break;
    default        : f->opaque = NULL;
    }
    return f;
}


static int
stream_get_mode( cdk_stream_t s )
{
    assert( s );
    
    if( s->flags.temp )
        return s->fmode;
    return s->flags.write;
}


static filter_fnct_t
stream_id_to_filter( int type )
{
    switch( type ) {
    case fARMOR    : return _cdk_filter_armor;
    case fLITERAL: return _cdk_filter_literal;
    case fTEXT     : return _cdk_filter_text;
    case fCIPHER   : return _cdk_filter_cipher;
    case fCOMPRESS : return _cdk_filter_compress;
    default        : return NULL;
    }
}


/**
 * cdk_stream_filter_disable: Disable the filter with the type 'type'
 * @s: The STREAM object
 * @type: The numberic filter ID.
 *
 **/
cdk_error_t
cdk_stream_filter_disable( cdk_stream_t s, int type )
{
    struct stream_filter_s * f;
    filter_fnct_t fnc;

    if( !s )
        return CDK_Inv_Value;
    fnc = stream_id_to_filter( type  );
    f = filter_search( s, fnc );
    if( f )
        f->flags.enabled = 0;
    return 0;
}


static int
stream_fp_replace( cdk_stream_t s, FILE ** tmp )
{
    int rc;

    assert( s );
  
    rc = fclose( s->fp );
    if( rc )
        return CDK_File_Error;
    s->fp = *tmp;
    *tmp = NULL;
    return 0;
}


/* This function is exactly like filter_read, except the fact that we can't
   use tmpfile () all the time. That's why we open the real file when there
   is no last filter. */
static int
stream_filter_write( cdk_stream_t s )
{
    struct stream_filter_s * f;
    int rc = 0;

    assert( s );
    
    if( s->flags.filtrated )
        return CDK_Inv_Value;

    for( f = s->filters; f; f = f->next ) {
        if( !f->flags.enabled )
            continue;
        /* if there is no next filter, create the final output file */
        _cdk_log_debug( "filter [write]: last filter=%d fname=%s\n",
                        f->next? 1 : 0, s->fname );
        if( !f->next && s->fname )
            f->tmp = fopen( s->fname, "w+b" );
        else
            f->tmp = tmpfile( );
        if( !f->tmp ) {
            rc = CDK_File_Error;
            break;            
        }
        /* If there is no next filter, flush the cache. We also do this
           when the next filter is the armor filter because this filter
           is special and before it starts, all data should be written. */
        if( (!f->next || f->next->type == fARMOR) && s->cache.size ) {
            rc = stream_cache_flush( s, f->tmp );
            if( rc )
                break;
        }
        rc = f->fnct( f->opaque, f->ctl, s->fp, f->tmp );
        _cdk_log_debug( "filter [write]: type=%d rc=%d\n", f->type, rc );
        if( !rc )
            rc = stream_fp_replace( s, &f->tmp );
        if( !rc )
            rc = cdk_stream_seek( s, 0 );
        if( rc ) {
            fclose( f->tmp );
            break;
        }
    }
    return rc;
}


/* Here all data from the file handle is passed through all filters.
   The scheme works like this:
   Create a tempfile and use it for the output of the filter. Then the
   original file handle will be closed and replace with the temp handle.
   The file pointer will be set to the begin and the game starts again. */
static int
stream_filter_read( cdk_stream_t s )
{
    struct stream_filter_s * f;
    int rc = 0;

    assert( s );
    
    if( s->flags.filtrated )
        return 0;

    for( f = s->filters; f; f = f->next ) {
        if( !f->flags.enabled )
            continue;
        f->tmp = tmpfile( );
        if( !f->tmp ) {
            rc = CDK_File_Error;
            break;
        }
        rc = f->fnct( f->opaque, f->ctl, s->fp, f->tmp );
        _cdk_log_debug( "filter %s [read]: type=%d rc=%d\n",
                        s->fname? s->fname : "[temp]", f->type, rc );
        if( rc )
            break;
      
        /* if the filter is read-only, do not replace the FP because
           the contents were not altered in any way. */
        if( !f->flags.rdonly ) {
            rc = stream_fp_replace( s, &f->tmp );
            if( rc )
                break;
        }
        else {
            fclose( f->tmp );
            f->tmp = NULL;
        }
        rc = cdk_stream_seek( s, 0 );
        if( rc )
            break;
        /* Disable the filter after it was successfully used. The idea
           is the following: let's say the armor filter was pushed and
           later more filters were added. The second time the filter code
           will be executed, only the new filter should be started but
           not the old because we already used it. */
        f->flags.enabled = 0;
    }
    return rc;
}


void *
_cdk_stream_get_opaque( cdk_stream_t s, int fid )
{
    struct stream_filter_s * f;

    if( !s )
        return NULL;
    
    for( f = s->filters; f; f = f->next ) {
        if( f->type == fid )
            return f->opaque;
    }
    return NULL;
}


/**
 * cdk_stream_read: Try to read count bytes from the STREAM object.
 * @s: The STREAM object.
 * @buf: The buffer to insert the readed bytes.
 * @count: Request so much bytes.
 *
 * When this function is called the first time, it can take a while
 * because all filters need to be processed. Please remember that you
 * need to add the filters in reserved order.
 **/
int
cdk_stream_read( cdk_stream_t s, void * buf, size_t count )
{
    int nread;
    int rc;

    if( !s )
        return EOF;
    
    if( s->flags.write && !s->flags.temp )
        return EOF; /* this is a write stream */
  
    if( !s->flags.no_filter && !s->cache.on && !s->flags.filtrated ) {
        rc = stream_filter_read( s );
        if( rc ) {
            s->error = rc;
            return EOF;
        }
        s->flags.filtrated = 1;
    }
    if( !buf && !count )
        return 0;
    nread = fread( buf, 1, count, s->fp );  
    if( !nread )
        nread = EOF;
    if( feof( s->fp ) )
        s->flags.eof = 1;
    return nread;
}

      
int
cdk_stream_getc( cdk_stream_t s )
{
    unsigned char buf[2];
    int nread;

    if( !s )
        return EOF;
    
    nread = cdk_stream_read( s, buf, 1 );
    if( nread == EOF ) {
        s->error = CDK_File_Error;
        return EOF;
    }
    return buf[0];
}


/**
 * cdk_stream_write: Try to write count bytes into the stream.
 * @s: The STREAM object
 * @buf: The buffer with the values to write.
 * @count: The size of the buffer.
 *
 * In this function we simply write the bytes to the stream. We can't
 * use the filters here because it would mean they have to support
 * partial flushing.
 **/
int
cdk_stream_write( cdk_stream_t s, const void * buf, size_t count )
{
    int nwritten;

    if( !s )
        return CDK_Inv_Value;
    
    if( !s->flags.write )
        return CDK_Inv_Mode; /* this is a read stream */

    if( !buf && !count )
        return stream_flush( s );

    if( s->cache.on ) {
        if( s->cache.size + count > sizeof( s->cache.buf ) )
            return CDK_EOF;
        memcpy( s->cache.buf + s->cache.size, buf, count  );
        s->cache.size += count;
        return 0;
    }
  
    nwritten = fwrite( buf, 1, count, s->fp );
    if( !nwritten )
        nwritten = EOF;  
    return nwritten;
}


int
cdk_stream_putc( cdk_stream_t s, int c )
{
    unsigned char buf[2];
    int nwritten;

    if( !s )
        return EOF;
    buf[0] = c;
    nwritten = cdk_stream_write( s, buf, 1 );
    if( nwritten == EOF ) {
        s->error = CDK_File_Error;
        return EOF;
    }
    return 0;
}


long
cdk_stream_tell( cdk_stream_t s )
{
    return s? ftell( s->fp ): (long)-1;
}


cdk_error_t
cdk_stream_seek( cdk_stream_t s, long offset )
{
    int rc;

    if( !s )
        return CDK_Inv_Value;    
    if( offset < cdk_stream_get_length( s ) )
        s->flags.eof = 0;
    rc = fseek( s->fp, offset, SEEK_SET );
    if( rc )
        rc = CDK_File_Error;
    return rc;
}


static int
stream_flush( cdk_stream_t s )
{
    int rc;
    
    assert( s );
    
    rc = fflush( s->fp  );
    if( rc )
        rc = CDK_File_Error;
    return rc;
}


cdk_error_t
cdk_stream_set_armor_flag( cdk_stream_t s, int type )
{
    struct stream_filter_s * f;

    if( !s )
        return CDK_Inv_Value;
    f = filter_add( s, _cdk_filter_armor, fARMOR );
    if( !f )
        return CDK_Out_Of_Core;
    f->u.afx.idx = f->u.afx.idx2 = type;
    f->ctl = stream_get_mode( s );
    return 0;
}


cdk_error_t
cdk_stream_set_literal_flag( cdk_stream_t s, int mode, const char * fname )
{
    struct stream_filter_s * f;

    if( !s )
        return CDK_Inv_Value;
    f = filter_add( s, _cdk_filter_literal, fLITERAL );
    if( !f )
        return CDK_Out_Of_Core;
    f->u.pfx.mode = mode;
    f->u.pfx.filename = fname? cdk_strdup( fname ) : NULL;
    f->ctl = stream_get_mode( s );
    if( s->blkmode ) {
        f->u.pfx.blkmode.on = 1;
        f->u.pfx.blkmode.size = s->blkmode;
    }
    return 0;
}


cdk_error_t
cdk_stream_set_cipher_flag( cdk_stream_t s, cdk_dek_t dek, int use_mdc )
{
    struct stream_filter_s * f;

    if( !s )
        return CDK_Inv_Value;
    f = filter_add( s, _cdk_filter_cipher, fCIPHER );
    if( !f )
        return CDK_Out_Of_Core;
    dek->use_mdc = use_mdc;
    f->ctl = stream_get_mode( s );
    f->u.cfx.dek = dek;
    f->u.cfx.mdc_method = use_mdc? GCRY_MD_SHA1 : 0;
    if( s->blkmode ) {
        f->u.cfx.blkmode.on = 1;
        f->u.cfx.blkmode.size = s->blkmode;
    }
    return 0;
}


cdk_error_t
cdk_stream_set_compress_flag( cdk_stream_t s, int algo, int level )
{
    struct stream_filter_s * f;

    if( !s )
        return CDK_Inv_Value;
    f = filter_add( s, _cdk_filter_compress, fCOMPRESS );
    if( !f )
        return CDK_Out_Of_Core;
    f->ctl = stream_get_mode( s );
    f->u.zfx.algo = algo;
    f->u.zfx.level = level;
    return 0;
}


cdk_error_t
cdk_stream_set_text_flag( cdk_stream_t s, const char * lf )
{
    struct stream_filter_s * f;

    if( !s )
        return CDK_Inv_Value;
    f = filter_add( s, _cdk_filter_text, fTEXT );
    if( !f )
        return CDK_Out_Of_Core;
    f->ctl = stream_get_mode( s );
    f->u.tfx.lf = lf;
    return 0;
}


cdk_error_t
cdk_stream_set_hash_flag( cdk_stream_t s, int algo )
{
    struct stream_filter_s * f;

    if( !s )
        return CDK_Inv_Value;    
    if( stream_get_mode( s ) )
        return CDK_Inv_Mode;
    f = filter_add( s, _cdk_filter_hash, fHASH );
    if( !f )
        return CDK_Out_Of_Core;
    f->ctl = stream_get_mode( s );
    f->u.mfx.digest_algo = algo;
    f->flags.rdonly = 1;
    return 0;
}


cdk_error_t
cdk_stream_set_cache( cdk_stream_t s, int val )
{
    if( !s )
        return CDK_Inv_Value;
    if( !s->flags.write )
        return CDK_Inv_Mode;
    s->cache.on = val;
    return 0;
}


static int
stream_cache_flush( cdk_stream_t s, FILE * fp )
{
    int nwritten;

    assert( s );
    
    if( s->cache.size > 0 ) {
        nwritten = fwrite( s->cache.buf, 1, s->cache.size, fp  );
        if( !nwritten )
            return CDK_File_Error;
        s->cache.size = 0;
        s->cache.on = 0;
        memset( s->cache.buf, 0, sizeof s->cache.buf  );
    }
    return 0;
}


cdk_error_t
cdk_stream_kick_off( cdk_stream_t inp, cdk_stream_t out )
{
    byte buf[8192];
    int nread, nwritten;
    int rc = 0;

    if( !inp || !out )
        return CDK_Inv_Value;
    while( !cdk_stream_eof( inp ) ) {
        nread = cdk_stream_read( inp, buf, sizeof buf-1 );
        if( nread == EOF )
            break;
        nwritten = cdk_stream_write( out, buf, nread );
        if( nwritten == EOF )
            rc = CDK_File_Error;
    }
    wipemem( buf, sizeof buf );
    return rc;
}


/**
 * cdk_stream_mmap:
 * @s: the stream
 * @ret_buf: the buffer to store the content
 * @ret_count: length of the buffer
 *
 * Map the data of the given stream into a memory section. @ret_count
 * contains the length of the buffer.
 **/
cdk_error_t
cdk_stream_mmap( cdk_stream_t s, byte ** ret_buf, size_t * ret_count )
{
    const u32 max_filesize = 16777216;
    u32 len, oldpos;
    int n, rc;
    char * p;

    if( !s || !ret_buf || !ret_count )
        return CDK_Inv_Value;

    *ret_count = 0;
    *ret_buf = NULL;
    oldpos = cdk_stream_tell( s );
    rc = cdk_stream_flush( s );
    if( !rc )
        rc = cdk_stream_seek( s, 0 );
    if( rc )
        return rc;
    len = cdk_stream_get_length( s );
    if( !len || len > max_filesize )
        return 0;
    p = *ret_buf = cdk_calloc( 1, len+1 );
    if( !p )
        return 0;
    *ret_count = len;
    n = cdk_stream_read( s, p, len );
    if( n != len )
        *ret_count = n;
    rc = cdk_stream_seek( s, oldpos );
    return rc;
}


/**
 * cdk_stream_peek:
 * @inp: the input stream handle
 * @s: buffer
 * @count: number of bytes to peek
 *
 * The function acts like cdk_stream_read with the difference that
 * the file pointer is moved to the old position after the bytes were read.
 **/
int
cdk_stream_peek( cdk_stream_t inp, byte *s, size_t count )
{
    unsigned off;
    int nbytes, rc;

    if( !inp || !s )
        return CDK_Inv_Value;
    off = cdk_stream_tell( inp );
    nbytes = _cdk_stream_gets( inp, s, count );
    rc = cdk_stream_seek( inp, off );
    if( rc )
        return 0;
    return nbytes;
}


int
_cdk_stream_gets( cdk_stream_t s, char * buf, size_t count )
{
    int c, i = 0;

    if( !s )
        return CDK_Inv_Value;
    while( !cdk_stream_eof( s ) && count > 0 ) {
        c = cdk_stream_getc( s );
        if( c == EOF || c == '\r' || c == '\n' ) {
            buf[i++] = '\0';
            break;
        }
        buf[i++] = c;
        count--;   
    }
    return i;
}


int
_cdk_stream_puts( cdk_stream_t s, const char * buf )
{
    return cdk_stream_write( s, buf, strlen( buf ) );
}


int
_cdk_stream_set_blockmode( cdk_stream_t s, size_t nbytes )
{
    if( !s )
        return CDK_Inv_Value;
    _cdk_log_debug( "set block mode for stream (size=%d)\n", nbytes );
    s->blkmode = nbytes;  
    return 0;
}


int
_cdk_stream_get_blockmode( cdk_stream_t s )
{
    return s? s->blkmode : 0;
}