77 if ( AT.SS != AT.S0 )
return(0);
78 if ( AR.NoCompress == 1 )
return(0);
79 if ( AR.gzipCompress <= 0 )
return(0);
81 if ( f->ziobuffer == 0 ) {
85 f->zsp = Malloc1(
sizeof(z_stream),
"output zstream");
90 (Bytef *)Malloc1(f->ziosize*
sizeof(
char),
"output zbuffer");
91 if ( f->zsp == 0 || f->ziobuffer == 0 ) {
92 MLOCK(ErrorMessageLock);
93 MesCall(
"SetupOutputGZIP");
94 MUNLOCK(ErrorMessageLock);
101 f->zsp->zalloc = Z_NULL;
102 f->zsp->zfree = Z_NULL;
103 f->zsp->opaque = Z_NULL;
107 f->zsp->next_out = f->ziobuffer;
108 f->zsp->avail_out = f->ziosize;
109 f->zsp->total_out = 0;
113 f->zsp->next_in = (Bytef *)(f->PObuffer);
114 f->zsp->avail_in = (Bytef *)(f->POfill) - (Bytef *)(f->PObuffer);
115 f->zsp->total_in = 0;
119 if ( deflateInit(f->zsp,AR.gzipCompress) != Z_OK ) {
121 MLOCK(ErrorMessageLock);
122 MesPrint(
"!>Error from zlib: %s",f->zsp->msg);
123 MesCall(
"SetupOutputGZIP");
124 MUNLOCK(ErrorMessageLock);
150 f->zsp->next_in = (Bytef *)(f->PObuffer);
151 f->zsp->avail_in = (Bytef *)(f->POfill) - (Bytef *)(f->PObuffer);
152 f->zsp->total_in = 0;
154 while ( ( zerror = deflate(f->zsp,Z_NO_FLUSH) ) == Z_OK ) {
155 if ( f->zsp->avail_out == 0 ) {
161 char *s = (
char *)((UBYTE *)(f->ziobuffer)+f->ziosize);
162 MLOCK(ErrorMessageLock);
163 MesPrint(
"%wWriting %l bytes at %10p: %d %d %d %d %d"
164 ,f->ziosize,&(f->POposition),s[-5],s[-4],s[-3],s[-2],s[-1]);
165 MUNLOCK(ErrorMessageLock);
169 LOCK(f->pthreadslock);
171 if ( f == AR.hidefile ) {
174 SeekFile(f->
handle,&(f->POposition),SEEK_SET);
175 if ( WriteFile(f->
handle,(UBYTE *)(f->ziobuffer),f->ziosize)
177 if ( f == AR.hidefile ) {
178 UNLOCK(AS.inputslock);
181 UNLOCK(f->pthreadslock);
183 MLOCK(ErrorMessageLock);
184 MesPrint(
"%wWrite error during compressed sort. Disk full?");
185 MUNLOCK(ErrorMessageLock);
188 if ( f == AR.hidefile ) {
189 UNLOCK(AS.inputslock);
192 UNLOCK(f->pthreadslock);
194 ADDPOS(f->filesize,f->ziosize);
195 ADDPOS(f->POposition,f->ziosize);
197 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
204 f->zsp->next_out = f->ziobuffer;
205 f->zsp->avail_out = f->ziosize;
206 f->zsp->total_out = 0;
208 else if ( f->zsp->avail_in == 0 ) {
216 MLOCK(ErrorMessageLock);
217 MesPrint(
"!>%w avail_in = %d, avail_out = %d.",f->zsp->avail_in,f->zsp->avail_out);
218 MUNLOCK(ErrorMessageLock);
224 MLOCK(ErrorMessageLock);
225 MesPrint(
"!>%wError in gzip handling of output. zerror = %d",zerror);
226 MUNLOCK(ErrorMessageLock);
247 f->zsp->next_in = (Bytef *)(f->PObuffer);
248 f->zsp->avail_in = (Bytef *)(f->POfill) - (Bytef *)(f->PObuffer);
249 f->zsp->total_in = 0;
251 while ( ( zerror = deflate(f->zsp,Z_FINISH) ) == Z_OK ) {
252 if ( f->zsp->avail_out == 0 ) {
257 MLOCK(ErrorMessageLock);
258 MesPrint(
"%wWriting %l bytes at %10p",f->ziosize,&(f->POposition));
259 MUNLOCK(ErrorMessageLock);
262 LOCK(f->pthreadslock);
264 if ( f == AR.hidefile ) {
265 UNLOCK(AS.inputslock);
267 SeekFile(f->
handle,&(f->POposition),SEEK_SET);
268 if ( WriteFile(f->
handle,(UBYTE *)(f->ziobuffer),f->ziosize)
270 if ( f == AR.hidefile ) {
271 UNLOCK(AS.inputslock);
274 UNLOCK(f->pthreadslock);
276 MLOCK(ErrorMessageLock);
277 MesPrint(
"%wWrite error during compressed sort. Disk full?");
278 MUNLOCK(ErrorMessageLock);
281 if ( f == AR.hidefile ) {
282 UNLOCK(AS.inputslock);
285 UNLOCK(f->pthreadslock);
287 ADDPOS(f->filesize,f->ziosize);
288 ADDPOS(f->POposition,f->ziosize);
290 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
297 f->zsp->next_out = f->ziobuffer;
298 f->zsp->avail_out = f->ziosize;
299 f->zsp->total_out = 0;
302 if ( zerror == Z_STREAM_END ) {
307 MLOCK(ErrorMessageLock);
308 MesPrint(
"%wWriting %l bytes at %10p",(LONG)(f->zsp->avail_out),&(f->POposition));
309 MUNLOCK(ErrorMessageLock);
312 LOCK(f->pthreadslock);
314 if ( f == AR.hidefile ) {
317 SeekFile(f->
handle,&(f->POposition),SEEK_SET);
318 if ( WriteFile(f->
handle,(UBYTE *)(f->ziobuffer),f->zsp->total_out)
319 != (LONG)(f->zsp->total_out) ) {
320 if ( f == AR.hidefile ) {
321 UNLOCK(AS.inputslock);
324 UNLOCK(f->pthreadslock);
326 MLOCK(ErrorMessageLock);
327 MesPrint(
"%wWrite error during compressed sort. Disk full?");
328 MUNLOCK(ErrorMessageLock);
331 if ( f == AR.hidefile ) {
335 UNLOCK(f->pthreadslock);
337 ADDPOS(f->filesize,f->zsp->total_out);
338 ADDPOS(f->POposition,f->zsp->total_out);
340 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
345 MLOCK(ErrorMessageLock);
346 {
char *s = f->ziobuffer+f->zsp->total_out;
347 MesPrint(
"%w Last bytes written: %d %d %d %d %d",s[-5],s[-4],s[-3],s[-2],s[-1]);
349 MesPrint(
"%w Perceived position in FlushOutputGZIP is %10p",&(f->POposition));
350 MUNLOCK(ErrorMessageLock);
355 f->zsp->next_out = f->ziobuffer;
356 f->zsp->avail_out = f->ziosize;
357 f->zsp->total_out = 0;
358 if ( ( zerror = deflateEnd(f->zsp) ) == Z_OK )
return(0);
360 MLOCK(ErrorMessageLock);
362 MesPrint(
"!>%wError in finishing gzip handling of output: %s",f->zsp->msg);
365 MesPrint(
"!>%wError in finishing gzip handling of output.");
367 MUNLOCK(ErrorMessageLock);
370 MLOCK(ErrorMessageLock);
371 MesPrint(
"!>%wError in gzip handling of output.");
372 MUNLOCK(ErrorMessageLock);
389int SetupAllInputGZIP(
SORTING *S)
392 int i, NumberOpened = 0;
398 for ( i = 0; i < S->inNum; i++ ) {
399 if ( S->fpincompressed[i] )
break;
401 if ( i >= S->inNum )
return(0);
403 if ( S->zsparray == 0 ) {
404 S->zsparray = (z_streamp)Malloc1(
sizeof(z_stream)*S->MaxFpatches,
"input zstreams");
405 if ( S->zsparray == 0 ) {
406 MLOCK(ErrorMessageLock);
407 MesCall(
"SetupAllInputGZIP");
408 MUNLOCK(ErrorMessageLock);
415 AN.ziobuffers = (Bytef *)Malloc1(S->MaxFpatches*(S->file.ziosize+128)*
sizeof(Bytef),
"input raw buffers");
421 AN.ziobufnum = (Bytef **)Malloc1(S->MaxFpatches*
sizeof(Bytef *),
"input raw pointers");
422 if ( AN.ziobuffers == 0 || AN.ziobufnum == 0 ) {
423 MLOCK(ErrorMessageLock);
424 MesCall(
"SetupAllInputGZIP");
425 MUNLOCK(ErrorMessageLock);
428 for ( i = 0 ; i < S->MaxFpatches; i++ ) {
429 AN.ziobufnum[i] = AN.ziobuffers + i * (S->file.ziosize+128);
432 for ( i = 0; i < S->inNum; i++ ) {
434 MLOCK(ErrorMessageLock);
435 MesPrint(
"%wPreparing z-stream %d with compression %d",i,S->fpincompressed[i]);
436 MUNLOCK(ErrorMessageLock);
438 if ( S->fpincompressed[i] ) {
439 zsp = &(S->zsparray[i]);
443 zsp->zalloc = Z_NULL;
445 zsp->opaque = Z_NULL;
449 zsp->next_out = Z_NULL;
455 zsp->next_in = Z_NULL;
461 if ( inflateInit(zsp) != Z_OK ) {
463 MLOCK(ErrorMessageLock);
464 if ( zsp->msg ) MesPrint(
"!>%wError from inflateInit: %s",zsp->msg);
465 else MesPrint(
"!>%wError from inflateInit");
466 MesCall(
"SetupAllInputGZIP");
467 MUNLOCK(ErrorMessageLock);
474 return(NumberOpened);
487LONG FillInputGZIP(
FILEHANDLE *f,
POSITION *position, UBYTE *buffer, LONG buffersize,
int numstream)
491 LONG readsize, toread = 0;
495 if ( S->fpincompressed[numstream] ) {
496 zsp = &(S->zsparray[numstream]);
497 zsp->next_out = (Bytef *)buffer;
498 zsp->avail_out = buffersize;
500 if ( zsp->avail_in == 0 ) {
504 if ( ISGEPOSINC(S->fPatchesStop[numstream],*position,f->ziosize) ) {
508 DIFPOS(pos,S->fPatchesStop[numstream],*position);
509 toread = (LONG)(BASEPOSITION(pos));
513 MLOCK(ErrorMessageLock);
514 MesPrint(
"%w-+Reading %l bytes in stream %d at position %10p; stop at %10p",toread,numstream,position,&(S->fPatchesStop[numstream]));
515 MUNLOCK(ErrorMessageLock);
518 LOCK(f->pthreadslock);
520 SeekFile(f->
handle,position,SEEK_SET);
521 readsize = ReadFile(f->
handle,(UBYTE *)(AN.ziobufnum[numstream]),toread);
522 SeekFile(f->
handle,position,SEEK_CUR);
524 UNLOCK(f->pthreadslock);
527 MLOCK(ErrorMessageLock);
528 {
char *s = AN.ziobufnum[numstream]+readsize;
529 MesPrint(
"%w read: %l +Last bytes read: %d %d %d %d %d in %s, newpos = %10p",readsize,s[-5],s[-4],s[-3],s[-2],s[-1],f->name,position);
531 MUNLOCK(ErrorMessageLock);
533 if ( readsize == 0 ) {
534 zsp->next_in = AN.ziobufnum[numstream];
535 zsp->avail_in = f->ziosize;
537 return(zsp->total_out);
539 if ( readsize < 0 ) {
541 MLOCK(ErrorMessageLock);
542 MesPrint(
"!>%wFillInputGZIP: Read error during compressed sort.");
543 MUNLOCK(ErrorMessageLock);
547 ADDPOS(f->filesize,readsize);
548 ADDPOS(f->POposition,readsize);
552 zsp->next_in = AN.ziobufnum[numstream];
553 zsp->avail_in = readsize;
557 if ( toread > 0 || zsp->avail_in ) {
558 while ( ( zerror = inflate(zsp,Z_NO_FLUSH) ) == Z_OK ) {
559 if ( zsp->avail_out == 0 ) {
563 return((LONG)(zsp->total_out));
565 if ( zsp->avail_in == 0 ) {
567 if ( ISEQUALPOS(S->fPatchesStop[numstream],*position) ) {
571 zerror = Z_STREAM_END;
604 if ( numstream == 0 ) {
605 MLOCK(ErrorMessageLock);
606 MesPrint(
"%wWant to read in stream 0 at position %10p",position);
607 MUNLOCK(ErrorMessageLock);
610 if ( ISGEPOSINC(S->fPatchesStop[numstream],*position,f->ziosize) ) {
614 DIFPOS(pos,S->fPatchesStop[numstream],*position);
615 toread = (LONG)(BASEPOSITION(pos));
618 MLOCK(ErrorMessageLock);
619 MesPrint(
"%w--Reading %l bytes in stream %d at position %10p",toread,numstream,position);
620 MUNLOCK(ErrorMessageLock);
623 LOCK(f->pthreadslock);
625 SeekFile(f->
handle,position,SEEK_SET);
626 readsize = ReadFile(f->
handle,(UBYTE *)(AN.ziobufnum[numstream]),toread);
627 SeekFile(f->
handle,position,SEEK_CUR);
629 UNLOCK(f->pthreadslock);
632 MLOCK(ErrorMessageLock);
633 {
char *s = AN.ziobufnum[numstream]+readsize;
634 MesPrint(
"%w Last bytes read: %d %d %d %d %d",s[-5],s[-4],s[-3],s[-2],s[-1]);
636 MUNLOCK(ErrorMessageLock);
638 if ( readsize == 0 ) {
639 zsp->next_in = AN.ziobufnum[numstream];
640 zsp->avail_in = f->ziosize;
642 return(zsp->total_out);
644 if ( readsize < 0 ) {
646 MLOCK(ErrorMessageLock);
647 MesPrint(
"!>%wFillInputGZIP: Read error during compressed sort.");
648 MUNLOCK(ErrorMessageLock);
652 ADDPOS(f->filesize,readsize);
653 ADDPOS(f->POposition,readsize);
657 zsp->next_in = AN.ziobufnum[numstream];
658 zsp->avail_in = readsize;
667 zerror = Z_STREAM_END;
671 MLOCK(ErrorMessageLock);
672 MesPrint(
"%w zerror = %d in stream %d. At position %10p",zerror,numstream,position);
673 MUNLOCK(ErrorMessageLock);
675 if ( zerror == Z_STREAM_END ) {
679 zsp->next_in = Z_NULL;
686 MLOCK(ErrorMessageLock);
687 MesPrint(
"%wClosing stream %d",numstream);
689 readsize = zsp->total_out;
691 if ( readsize > 0 ) {
692 WORD *s = (WORD *)(buffer+zsp->total_out);
693 MesPrint(
"%w -Last words: %d %d %d %d %d",s[-5],s[-4],s[-3],s[-2],s[-1]);
696 MesPrint(
"%w No words");
698 MUNLOCK(ErrorMessageLock);
700 if ( zsp->zalloc != Z_NULL ) {
701 zerror = inflateEnd(zsp);
702 zsp->zalloc = Z_NULL;
704 if ( zerror == Z_OK || zerror == Z_STREAM_END )
return(readsize);
708 MLOCK(ErrorMessageLock);
709 MesPrint(
"!>%wFillInputGZIP: Error in gzip handling of input. zerror = %d",zerror);
710 MUNLOCK(ErrorMessageLock);
716 MLOCK(ErrorMessageLock);
717 MesPrint(
"%w++Reading %l bytes at position %10p",buffersize,position);
718 MUNLOCK(ErrorMessageLock);
721 LOCK(f->pthreadslock);
723 SeekFile(f->
handle,position,SEEK_SET);
724 readsize = ReadFile(f->
handle,buffer,buffersize);
725 SeekFile(f->
handle,position,SEEK_CUR);
727 UNLOCK(f->pthreadslock);
729 if ( readsize < 0 ) {
731 MLOCK(ErrorMessageLock);
732 MesPrint(
"!>%wFillInputGZIP: Read error during uncompressed sort.");
733 MesPrint(
"%w++Reading %l bytes at position %10p",buffersize,position);
734 MUNLOCK(ErrorMessageLock);
748 if ( f->ziobuffer ) {
749 M_free(f->ziobuffer,
"output zbuffer");
750 M_free(f->zsp,
"output zstream");