File: DataProcessing.c

package info (click to toggle)
hashrat 1.8.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,700 kB
  • ctags: 1,677
  • sloc: ansic: 17,713; sh: 323; makefile: 157
file content (1062 lines) | stat: -rw-r--r-- 21,464 bytes parent folder | download
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
#include "DataProcessing.h"
#include "SpawnPrograms.h"
#include "FileSystem.h"
#include "Hash.h"
#include "file.h"

#ifdef HAVE_LIBSSL

#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>


typedef struct
{
char *Key;
int KeyLen;
char *InputVector;
int InputVectorLen;
int BlockSize;
const EVP_CIPHER *Cipher;
EVP_CIPHER_CTX *enc_ctx;
EVP_CIPHER_CTX *dec_ctx;
} libCryptoProcessorData;
#endif


void DataProcessorDestroy(void *In)
{
TProcessingModule *Mod;

Mod=(TProcessingModule *) In;
if (! Mod) return;
if (Mod->Close) Mod->Close(Mod);
DestroyString(Mod->Name);
DestroyString(Mod->Args);
DestroyString(Mod->ReadBuff);
DestroyString(Mod->WriteBuff);
free(Mod);
}



char *DataProcessorGetValue(TProcessingModule *M, const char *Name)
{
ListNode *Curr;

if (! M->Values) return(NULL);
Curr=ListFindNamedItem(M->Values,Name);
if (Curr) return(Curr->Item);
return(NULL);
}


void DataProcessorSetValue(TProcessingModule *M, const char *Name, const char *Value)
{
ListNode *Curr;

if (! M->Values) M->Values=ListCreate();
Curr=ListFindNamedItem(M->Values,Name);
if (Curr) Curr->Item = (void *) CopyStr( (char *) Curr->Item, Value);
else ListAddNamedItem(M->Values,Name,CopyStr(NULL,Value));
}



void DataProcessorUpdateBuffer(char **Buffer, int *Used, int *Size, const char *Data, int DataLen)
{
int len;

if (DataLen < 1) return;

len=*Used+DataLen;


if (len > *Size)
{
 *Buffer=(char *) realloc(*Buffer,len);
 *Size=len;
}

//if we've been supplied actual data to put in the buffer, then do so
//otherwise just expand it if needed
if (Data) 
{
	memcpy((*Buffer) + (*Used) ,Data,DataLen);
	*Used=len;
}
}


int PipeCommandProcessorInit(TProcessingModule *ProcMod, const char *Args)
{
int result=FALSE;
char *Tempstr=NULL;
char *Name=NULL, *Value=NULL, *ptr;
STREAM *S;

ptr=GetNameValuePair(Args,"\\S","=",&Name,&Value);
while (ptr)
{
  if (strcasecmp(Name,"Command")==0) Tempstr=CopyStr(Tempstr,Value);

ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value);
}

if (! StrLen(Tempstr) )
{
	DestroyString(Name);
	DestroyString(Value);
	DestroyString(Tempstr);
	return(FALSE);
}

GetToken(Tempstr,"\\S",&Name,0);
Value=FindFileInPath(Value,Name,getenv("PATH"));

if (! StrLen(Value) )
{
	DestroyString(Name);
	DestroyString(Value);
	DestroyString(Tempstr);
	return(FALSE);
}


S=STREAMSpawnCommand(Value, "", "",  COMMS_BY_PIPE);
ProcMod->Data=(void *) S;
result=TRUE;

DestroyString(Name);
DestroyString(Value);
DestroyString(Tempstr);

return(result);
}


int PipeCommandProcessorWrite(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush)
{
STREAM *S;

S=(STREAM *) ProcMod->Data;
if (InLen > 0)
{
	STREAMWriteBytes(S,InData,InLen);
	STREAMFlush(S);
}

if (Flush) 
{
	if (S->out_fd > -1) close(S->out_fd);
	S->out_fd=-1;
}
else if (! STREAMCheckForBytes(S)) return(0);
return(STREAMReadBytes(S,*OutData,*OutLen));
}


int PipeCommandProcessorClose(TProcessingModule *ProcMod)
{
STREAMClose((STREAM *) ProcMod->Data);
ProcMod->Data=NULL;

return(TRUE);
}



void InitialiseEncryptionComponents(const char *Args, char **Cipher, char **InputVector, int *IVLen,  char **Key, int *KeyLen, int *Flags)
{
char *TmpKey=NULL, *Tempstr=NULL;
int klen=0, slen=0;
char *Name=NULL, *Value=NULL, *ptr;
char *Salt=NULL;

*IVLen=0;
ptr=GetNameValuePair(Args,"\\S","=",&Name,&Value);
while (ptr)
{
  if (StrLen(Name))
  {
  if (strcasecmp(Name,"Cipher")==0)
  {
	  *Cipher=CopyStr(*Cipher,Value);
  }


  if (strcasecmp(Name,"Key")==0)
  {
	  TmpKey=CopyStr(TmpKey,Value);
	  klen=StrLen(TmpKey);
  }

  if (strcasecmp(Name,"Salt")==0)
  {
	  Salt=CopyStr(Salt,Value);
	  slen=StrLen(Salt);
  }



  if (
	(strcasecmp(Name,"iv")==0) ||
  	(strcasecmp(Name,"InputVector")==0)
    )
  {
    *InputVector=CopyStr(*InputVector,Value);
    *IVLen=StrLen(*InputVector);
  }

  if (strcasecmp(Name,"HexKey")==0)
  {
    klen=HexStrToBytes(&TmpKey, Value);
  }

  if (
	(strcasecmp(Name,"HexIV")==0) ||
  	(strcasecmp(Name,"HexInputVector")==0)
    )

  {
    *IVLen=HexStrToBytes(InputVector,Value);
  }

  if (strcasecmp(Name,"PadBlock")==0) 
  {
    if (strcasecmp(Value,"N")==0) *Flags |= DPM_NOPAD_DATA;
  }

  }

ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value);
}


Tempstr=SetStrLen(Tempstr,klen+slen);
memcpy(Tempstr,Salt,slen);
memcpy(Tempstr+slen,TmpKey,klen);

*KeyLen=HashBytes(Key,"md5",Tempstr,slen+klen,0);


DestroyString(Name);
DestroyString(Value);
DestroyString(Tempstr);
DestroyString(TmpKey);
DestroyString(Salt);
}



#ifdef HAVE_LIBCRYPTO

typedef enum {CI_BLOWFISH, CI_RC2, CI_RC4, CI_RC5, CI_DES, CI_DESX, CI_CAST,CI_IDEA,CI_AES, CI_AES_256} LIBUSEFUL_CRYPT_CIPHERS;

int libCryptoCipherAvailable(int CipherNum)
{
switch(CipherNum)
{
	case CI_BLOWFISH:
		#ifdef HAVE_EVP_BF_CBC
		return(TRUE);
		#endif
		break;

	case CI_RC2:
		#ifdef HAVE_EVP_RC2_CBC
		return(TRUE);
		#endif
		break;

	case CI_RC4:
		#ifdef HAVE_EVP_RC4_CBC
		return(TRUE);
		#endif
		break;

	case CI_RC5:
		#ifdef HAVE_EVP_RC5_CBC
		return(TRUE);
		#endif
		break;

	case CI_DES:
		#ifdef HAVE_EVP_DES_CBC
		return(TRUE);
		#endif
		break;

	case CI_DESX:
		#ifdef HAVE_EVP_DESX_CBC
		return(TRUE);
		#endif
		break;

	case CI_CAST:
		#ifdef HAVE_EVP_CAST5_CBC
		return(TRUE);
		#endif
		break;

	case CI_IDEA:
		#ifdef HAVE_EVP_IDEA_CBC
		return(TRUE);
		#endif
		break;

	case CI_AES:
		#ifdef HAVE_EVP_AES_129_CBC
		return(TRUE);
		#endif
		break;

	case CI_AES_256:
		#ifdef HAVE_EVP_AES_256_CBC
		return(TRUE);
		#endif
		break;
}
return(FALSE);
}


int libCryptoProcessorInit(TProcessingModule *ProcMod, const char *Args)
{
int result=FALSE;

#ifdef HAVE_LIBSSL
libCryptoProcessorData *Data;
EVP_CIPHER_CTX *ctx;
const char *CipherList[]={"blowfish","rc2","rc4","rc5","des","desx","cast","idea","aes","aes-256",NULL};
int val;
char *Tempstr=NULL;

val=MatchTokenFromList(ProcMod->Name,CipherList,0);
if (val==-1) return(FALSE);
if (! libCryptoCipherAvailable(val)) return(FALSE);
Data=(libCryptoProcessorData *) calloc(1,sizeof(libCryptoProcessorData));

//Tempstr here holds the cipher name
InitialiseEncryptionComponents(Args, &Tempstr, &Data->InputVector, &Data->InputVectorLen, & Data->Key, &Data->KeyLen,&ProcMod->Flags);

if (StrLen(ProcMod->Name)==0) ProcMod->Name=CopyStr(ProcMod->Name,Tempstr);

switch(val)
{
/*
	case CI_NONE:
	Data->Cipher=EVP_enc_null();
	break;
*/

	case CI_BLOWFISH:
		#ifdef HAVE_EVP_BF_CBC
		Data->Cipher=EVP_bf_cbc();
		#endif
		break;

	case CI_RC2:
		#ifdef HAVE_EVP_RC2_CBC
		Data->Cipher=EVP_rc2_cbc();
		#endif
		break;

	case CI_RC4:
		#ifdef HAVE_EVP_RC4_CBC
		Data->Cipher=EVP_rc4();
		#endif
		break;

	case CI_RC5:
		#ifdef HAVE_EVP_RC5_32_12_16_CBC
		//Data->Cipher=EVP_rc5_32_12_16_cbc();
		#endif
		break;

	case CI_DES:
		#ifdef HAVE_EVP_DES_CBC
		Data->Cipher=EVP_des_cbc();
		#endif
		break;

	case CI_DESX:
		#ifdef HAVE_EVP_DESX_CBC
		Data->Cipher=EVP_desx_cbc();
		#endif
		break;

	case CI_CAST:
		#ifdef HAVE_EVP_CAST5_CBC
		Data->Cipher=EVP_cast5_cbc();
		#endif
		break;

	case CI_IDEA:
		#ifdef HAVE_EVP_IDEA_CBC
		Data->Cipher=EVP_idea_cbc();
		#endif
		break;

	case CI_AES:
		#ifdef HAVE_EVP_AES_128_CBC
		Data->Cipher=EVP_aes_128_cbc();
		#endif
		break;

	case CI_AES_256:
		#ifdef HAVE_EVP_AES_256_CBC
		Data->Cipher=EVP_aes_256_cbc();
		#endif
		break;
}


if (Data->Cipher)
{
Data->enc_ctx=(EVP_CIPHER_CTX *) calloc(1,sizeof(EVP_CIPHER_CTX));
Data->dec_ctx=(EVP_CIPHER_CTX *) calloc(1,sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(Data->enc_ctx);
EVP_CIPHER_CTX_init(Data->dec_ctx);
Data->BlockSize=EVP_CIPHER_block_size(Data->Cipher);

EVP_EncryptInit_ex(Data->enc_ctx,Data->Cipher,NULL,Data->Key,Data->InputVector);
EVP_DecryptInit_ex(Data->dec_ctx,Data->Cipher,NULL,Data->Key,Data->InputVector);

if (ProcMod->Flags & DPM_NOPAD_DATA) EVP_CIPHER_CTX_set_padding(Data->enc_ctx,FALSE);

ProcMod->Data=Data;
result=TRUE;

DataProcessorSetValue(ProcMod,"Cipher",Tempstr);
Tempstr=FormatStr(Tempstr,"%d",Data->BlockSize);
DataProcessorSetValue(ProcMod,"BlockSize",Tempstr);
}

DestroyString(Tempstr);
#endif
return(result);
}


int libCryptoProcessorClose(TProcessingModule *ProcMod)
{
#ifdef HAVE_LIBSSL
libCryptoProcessorData *Data;
EVP_CIPHER_CTX *ctx;

Data=(libCryptoProcessorData *) ProcMod->Data;
if (Data)
{
EVP_CIPHER_CTX_cleanup(Data->enc_ctx);
EVP_CIPHER_CTX_cleanup(Data->dec_ctx);

DestroyString(Data->Key);
DestroyString(Data->InputVector);
free(Data);
}
ProcMod->Data=NULL;
#endif
return(TRUE);
}







int libCryptoProcessorWrite(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush)
{
int wrote=0;

#ifdef HAVE_LIBSSL
/*
int len, result=0, val;
libCryptoProcessorData *Data;
EVP_CIPHER_CTX *ctx;
char *ptr, *Tempstr=NULL;

if (ProcMod->Flags & DPM_WRITE_FINAL) return(0);
ptr=OutData;

Data=(libCryptoProcessorData *) ProcMod->Data;
ctx=Data->enc_ctx;

ProcMod->Flags = ProcMod->Flags & ~DPM_WRITE_FINAL;

if (ProcMod->Flags & DPM_NOPAD_DATA)
{
	val=InLen % Data->BlockSize;
	Tempstr=CopyStrLen(Tempstr,InData,InLen);
	if (val !=0) 
	{
		Tempstr=SetStrLen(Tempstr,InLen + (Data->BlockSize-val));
		memset(Tempstr+InLen,' ', (Data->BlockSize-val));
		val=InLen+(Data->BlockSize-val);
	}
	else val=InLen;

	result=EVP_EncryptUpdate(ctx, ptr, &len, Tempstr, val);
}
else 
{
	result=EVP_EncryptUpdate(ctx, ptr, &len, InData, InLen);
}


if (! result) wrote=0;
else wrote=len;

DestroyString(Tempstr);
*/
#endif
return(wrote);
}



int libCryptoProcessorFlush(TProcessingModule *ProcMod, const char *InData, int InLen, char *OutData, int OutLen)
{
int wrote=0;

/*
int result=0, len;
libCryptoProcessorData *Data;

if (ProcMod->Flags & DPM_WRITE_FINAL) return(0);
Data=(libCryptoProcessorData *) ProcMod->Data;

if (Data)
{
if (InLen > 0)
{
result=libCryptoProcessorWrite(ProcMod, InData, InLen, OutData, OutLen,TRUE);
if (result > 0) return(result);
}

len=OutLen;
result=EVP_EncryptFinal_ex(Data->enc_ctx, OutData, &len);
ProcMod->Flags |= DPM_WRITE_FINAL;
}
if (! result) wrote=0;
else wrote=len;

*/

return(wrote);
}


int libCryptoProcessorRead(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush)
{
int bytes_read=0;
#ifdef HAVE_LIBSSL
/*
int len, ivlen, result, val;
libCryptoProcessorData *Data;
EVP_CIPHER_CTX *ctx;
char *ptr;

ptr=OutData;

Data=(libCryptoProcessorData *) ProcMod->Data;
if (!Data) return(0);

if (ProcMod->Flags & DPM_READ_FINAL)
{
  if (InLen==0)	return(0);
  EVP_DecryptInit_ex(Data->dec_ctx,Data->Cipher,NULL,Data->Key,Data->InputVector);

}

ctx=Data->dec_ctx;

if (InLen==0) 
{
  len=0;
  result=EVP_DecryptFinal_ex(ctx, ptr, &len);
  ProcMod->Flags |= DPM_READ_FINAL; //this so we don't try 
				    //another read
	
}
else 
{
	len=OutLen;
	result=EVP_DecryptUpdate(ctx, ptr, &len, InData, InLen);
}

if (! result) bytes_read=-1;
else bytes_read+=InLen; //should be 'len' but DecryptUpdate returns the
												//number of bytes output, not the number consumed
*/

#endif
return(bytes_read);
}

#endif



#ifdef HAVE_LIBZ

#include <zlib.h>

typedef struct
{
z_stream z_in;
z_stream z_out;
} zlibData;
#endif


int zlibProcessorInit(TProcessingModule *ProcMod, const char *Args)
{
int result=FALSE;

#ifdef HAVE_LIBZ
zlibData *ZData;
int CompressionLevel=5;
char *ptr, *Name=NULL, *Value=NULL;

ptr=GetNameValuePair(Args,"\\S","=",&Name,&Value);
while (ptr)
{
  if (strcasecmp(Name,"CompressionLevel")==0) CompressionLevel=atoi(Value);
  if (strcasecmp(Name,"Level")==0) CompressionLevel=atoi(Value);
ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value);
}


ProcMod->ReadMax=4096;
ProcMod->WriteMax=4096;


ZData=(zlibData *) calloc(1,sizeof(zlibData));
ZData->z_in.avail_in=0;
ZData->z_in.avail_out=0;
result=inflateInit(&ZData->z_in);

ZData->z_out.avail_in=0;
ZData->z_out.avail_out=0;
deflateInit(&ZData->z_out,CompressionLevel);


ProcMod->Data=(void *) ZData;
result=TRUE;

DestroyString(Name);
DestroyString(Value);

#endif
return(result);
}


int gzipProcessorInit(TProcessingModule *ProcMod, const char *Args)
{
int result=FALSE;

#ifdef HAVE_LIBZ
zlibData *ZData;
int CompressionLevel=5;
char *ptr, *Name=NULL, *Value=NULL;

ptr=GetNameValuePair(Args,"\\S","=",&Name,&Value);
while (ptr)
{
  if (strcasecmp(Name,"CompressionLevel")==0) CompressionLevel=atoi(Value);
  if (strcasecmp(Name,"Level")==0) CompressionLevel=atoi(Value);
ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value);
}

ProcMod->ReadMax=4096;
ProcMod->WriteMax=4096;

ZData=(zlibData *) calloc(1,sizeof(zlibData));
ZData->z_in.avail_in=0;
ZData->z_in.avail_out=0;
result=inflateInit2(&ZData->z_in,47);

ZData->z_out.avail_in=0;
ZData->z_out.avail_out=0;
deflateInit2(&ZData->z_out,5,Z_DEFLATED,30,8,Z_DEFAULT_STRATEGY);

ProcMod->Data=(void *) ZData;
result=TRUE;

DestroyString(Name);
DestroyString(Value);

#endif
return(result);
}


//Zlib is a little weird. It accepts a pointer to a buffer (next_in) and a buffer length (avail_in) to specify the input
//and another buffer (next_out) and length (avail_out) to write data into. When called it reads bytes from next_in, updates 
//next_in to point to the end of what it read, and subtracts the number of bytes it read from avail_in so that avail_in
//now says how many UNUSED bytes there are pointed to by next_in. Similarly it writes to next_out, updating that pointer
//to point to the end of the write, and updating avail_out to say how much room is LEFT usused in the output buffer
//
//However, if zlib doesn't use all avail_in, then you can't mess with that buffer until it has. Hence you can't take the unusued
//data from next_in/avail_in and copy it to a new buffer and pass that buffer into deflate/inflate on the next call. If zlib
//doesn't use all the input the only way to handle it is to grow the output buffer and call inflate/deflate again, so that it
//can write into the expanded buffer until it's used up all input. 
//
//Finally, when you've supplied all the input you've got, you have to call deflate with 'Z_FINISH' so that it knows there's no
//more data coming. 

int zlibProcessorWrite(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush)
{
int wrote=0;
#ifdef HAVE_LIBZ
int val=0;
zlibData *ZData;

if (ProcMod->Flags & DPM_WRITE_FINAL) return(STREAM_CLOSED);
ZData=(zlibData *) ProcMod->Data;


	ZData->z_out.avail_in=InLen;
	ZData->z_out.next_in=(char *) InData;
	ZData->z_out.avail_out=*OutLen;
	ZData->z_out.next_out=*OutData;

	while ((ZData->z_out.avail_in > 0) || Flush)
	{
		if (Flush) val=deflate(& ZData->z_out, Z_FINISH);
		else val=deflate(& ZData->z_out, Z_NO_FLUSH);

		wrote=*OutLen-ZData->z_out.avail_out;
		if (val==Z_STREAM_END) 
		{
			ProcMod->Flags |= DPM_WRITE_FINAL;
			break;
		}

		if ((ZData->z_out.avail_in > 0) || Flush)
		{
			*OutLen+=BUFSIZ;
			*OutData=(char *) realloc(*OutData,*OutLen);
			ZData->z_out.avail_out+=BUFSIZ;
		}

	}



#endif
return(wrote);
}


int zlibProcessorRead(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush)
{
int wrote=0;
#ifdef HAVE_LIBZ
int result=0;
zlibData *ZData;

if (ProcMod->Flags & DPM_READ_FINAL) return(STREAM_CLOSED);
ZData=(zlibData *) ProcMod->Data;


	ZData->z_in.avail_in=InLen;
	ZData->z_in.next_in=(char *) InData;
	ZData->z_in.avail_out=*OutLen;
	ZData->z_in.next_out=*OutData;

	while ((ZData->z_in.avail_in > 0) || Flush)
	{
		if (Flush) result=inflate(& ZData->z_in, Z_FINISH);
		else result=inflate(& ZData->z_in, Z_NO_FLUSH);

		wrote=(*OutLen)-ZData->z_in.avail_out;

		fprintf(stderr,"result=%d %d %d\n",result,InLen,Flush);

		switch (result)
		{
		case Z_DATA_ERROR: inflateSync(&ZData->z_in); break;
		case Z_ERRNO: if (Flush) ProcMod->Flags |= DPM_READ_FINAL; break;
		case Z_STREAM_ERROR:
		case Z_STREAM_END: ProcMod->Flags |= DPM_READ_FINAL; break;
		}

		if (ProcMod->Flags & DPM_READ_FINAL) break;
		if ((ZData->z_in.avail_in > 0) || Flush)
		{
			(*OutLen)+=BUFSIZ;
			*OutData=(char *) realloc(*OutData,*OutLen);
			ZData->z_in.next_out=(*OutData) + wrote;
			ZData->z_in.avail_out=(*OutLen) - wrote;
		}

	}


#endif
return(wrote);
}


/*
int zlibProcessorRead(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush)
{
int wrote=0, result;
#ifdef HAVE_LIBZ
zlibData *ZData;
int len;

ZData=(zlibData *) ProcMod->Data;

if (InLen > 0)
{
DataProcessorUpdateBuffer(&ProcMod->ReadBuff, &ProcMod->ReadUsed, &ProcMod->ReadSize, InData, InLen);

ZData->z_in.next_in=ProcMod->ReadBuff;
ZData->z_in.avail_in=ProcMod->ReadUsed;

ZData->z_in.avail_out=*OutLen;
ZData->z_in.next_out=*OutData;

if (InLen==0) result=inflate(& ZData->z_in, Z_FINISH);
else result=inflate(& ZData->z_in, Z_NO_FLUSH);

if (ZData->z_in.avail_in > 0) memmove(ProcMod->ReadBuff,ZData->z_in.next_in,ZData->z_in.avail_in);
ProcMod->ReadUsed=ZData->z_in.avail_in;

wrote=OutLen-ZData->z_in.avail_out;
}

#endif
return(wrote);
}
*/


int zlibProcessorClose(TProcessingModule *ProcMod)
{
#ifdef HAVE_LIBZ
zlibData *ZData;

ZData=(zlibData *) ProcMod->Data;
if (ZData)
{
inflateEnd(&ZData->z_in);
deflateEnd(&ZData->z_out);

free(ZData);
ProcMod->Data=NULL;
}
#endif
return(TRUE);
}



TProcessingModule *StandardDataProcessorCreate(const char *Class, const char *Name, const char *iArgs)
{
char *Args=NULL;
TProcessingModule *Mod=NULL;

Args=CopyStr(Args,iArgs);
#ifdef HAVE_LIBSSL
	#ifdef HAVE_LIBCRYPTO
	if (strcasecmp(Class,"crypto")==0)
	{
 	  Mod=(TProcessingModule *) calloc(1,sizeof(TProcessingModule));
 	  Mod->Args=CopyStr(Mod->Args,Args);
	  Mod->Name=CopyStr(Mod->Name,Name);
	  Mod->Init=libCryptoProcessorInit;
 	  Mod->Write=libCryptoProcessorWrite;
	  Mod->Read=libCryptoProcessorRead;
	  Mod->Close=libCryptoProcessorClose;
	}
	#endif
#endif



if (strcasecmp(Class,"compress")==0)
{
   Mod=(TProcessingModule *) calloc(1,sizeof(TProcessingModule));
   Mod->Args=CopyStr(Mod->Args,Args);
   Mod->Name=CopyStr(Mod->Name,Name);

   if (strcasecmp(Name,"zlib")==0) 
	 {
		#ifdef HAVE_LIBZ 
		Mod->Init=zlibProcessorInit;
   	Mod->Write=zlibProcessorWrite;
   	Mod->Close=zlibProcessorClose;
		#endif
   }
	 else if (strcasecmp(Name,"gzip")==0)
	 {
		#ifdef HAVE_LIBZ 
		Mod->Init=gzipProcessorInit;
   	Mod->Write=zlibProcessorWrite;
   	Mod->Close=zlibProcessorClose;
		#endif
   }
 	 else if (strcasecmp(Name,"bzip2")==0)
	 {
		Args=MCopyStr(Args,"Command='bzip2 --stdout -' ",iArgs,NULL);
		Mod->Init=PipeCommandProcessorInit;
   	Mod->Write=PipeCommandProcessorWrite;
   	Mod->Close=PipeCommandProcessorClose;
   }
 	 else if (strcasecmp(Name,"xz")==0)
	 {
		Args=MCopyStr(Args,"Command='xz --stdout -' ",iArgs,NULL);
		Mod->Init=PipeCommandProcessorInit;
   	Mod->Write=PipeCommandProcessorWrite;
   	Mod->Close=PipeCommandProcessorClose;
   }

}


if (strcasecmp(Class,"uncompress")==0)
{
   Mod=(TProcessingModule *) calloc(1,sizeof(TProcessingModule));
   Mod->Args=CopyStr(Mod->Args,Args);
   Mod->Name=CopyStr(Mod->Name,Name);

   if (strcasecmp(Name,"zlib")==0) 
	 {
		#ifdef HAVE_LIBZ 
		Mod->Init=zlibProcessorInit;
   	Mod->Read=zlibProcessorRead;
   	Mod->Close=zlibProcessorClose;
		#endif
   }
	 else if (strcasecmp(Name,"gzip")==0)
	 {
		#ifdef HAVE_LIBZ 
		Mod->Init=gzipProcessorInit;
   	Mod->Read=zlibProcessorRead;
   	Mod->Close=zlibProcessorClose;
		#endif
   }
 	 else if (strcasecmp(Name,"bzip2")==0)
	 {
		Args=MCopyStr(Args,"Command='bzip2 -d --stdout -' ",iArgs,NULL);
		Mod->Init=PipeCommandProcessorInit;
   	Mod->Read=PipeCommandProcessorWrite;
   	Mod->Close=PipeCommandProcessorClose;
   }
 	 else if (strcasecmp(Name,"xz")==0)
	 {
		Args=MCopyStr(Args,"Command='xz -d --stdout -' ",iArgs,NULL);
		Mod->Init=PipeCommandProcessorInit;
   	Mod->Read=PipeCommandProcessorWrite;
   	Mod->Close=PipeCommandProcessorClose;
   }

}



if (Mod && Mod->Init && Mod->Init(Mod, Args)) return(Mod);

DestroyString(Args);

DataProcessorDestroy(Mod);
return(NULL);
}





int STREAMAddDataProcessor(STREAM *S, TProcessingModule *Mod, const char *Args)
{
ListNode *Curr;
char *Tempstr=NULL;
int len;

STREAMFlush(S);

if (! S->ProcessingModules) S->ProcessingModules=ListCreate();
Tempstr=MCopyStr(Tempstr,Mod->Name,NULL);
ListAddNamedItem(S->ProcessingModules,Tempstr,Mod);

len=S->InEnd - S->InStart;
Tempstr=SetStrLen(Tempstr,len);
memcpy(Tempstr,S->InputBuff + S->InStart,len);
STREAMResetInputBuffers(S);

Curr=ListGetNext(Mod->Values);
while (Curr)
{
	STREAMSetValue(S,Curr->Tag,(char *) Curr->Item);
	Curr=ListGetNext(Curr);
}

STREAMReadThroughProcessors(S, Tempstr, len);

DestroyString(Tempstr);
return(TRUE);
}


int STREAMDeleteDataProcessor(STREAM *S, char *Class, char *Name)
{
ListNode *Curr;
char *Tempstr=NULL;

STREAMFlush(S);

Tempstr=MCopyStr(Tempstr,Class,":",Name,NULL);
Curr=ListFindNamedItem(S->ProcessingModules,Tempstr);
ListDeleteNode(Curr);

DestroyString(Tempstr);
return(TRUE);
}



int DataProcessorAvailable(const char *Class, const char *Name)
{
int result=FALSE;
TProcessingModule *Mod;

Mod=StandardDataProcessorCreate(Class,Name,"");
if (Mod) result=TRUE;

DataProcessorDestroy(Mod);
return(result);
}


int STREAMAddStandardDataProcessor(STREAM *S, const char *Class, const char *Name, const char *Args)
{
TProcessingModule *Mod=NULL;

Mod=StandardDataProcessorCreate(Class,Name,Args);
if (Mod) 
{
STREAMAddDataProcessor(S, Mod, Args);
return(TRUE);
}

return(FALSE);
}


void STREAMClearDataProcessors(STREAM *S)
{
STREAMFlush(S);
STREAMResetInputBuffers(S);
ListDestroy(S->ProcessingModules, DataProcessorDestroy);
}