Back to index

tetex-bin  3.0
writezip.c
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 1996-2002 Han The Thanh, <thanh@pdftex.org>
00003 
00004 This file is part of pdfTeX.
00005 
00006 pdfTeX is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 pdfTeX is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with pdfTeX; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 $Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/writezip.c#5 $
00021 */
00022 
00023 #include "ptexlib.h"
00024 #include "zlib.h"
00025 
00026 static const char perforce_id[] = 
00027     "$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/writezip.c#5 $";
00028 
00029 #define ZIP_BUF_SIZE  32768
00030 
00031 #define check_err(f, fn)                                   \
00032     if (f != Z_OK)                                         \
00033         pdftex_fail("zlib: %s() failed", fn)
00034 
00035 #define fixedcompresslevel  fixint(getintpar(cfgcompresslevelcode), 0, 9)
00036 
00037 static char zipbuf[ZIP_BUF_SIZE];
00038 static z_stream c_stream; /* compression stream */
00039 
00040 void writezip(boolean finish)
00041 {
00042     int err;
00043 
00044     if (!fixedcompresslevel) {
00045         if (pdfptr) {
00046             pdfgone += xfwrite(pdfbuf, 1, pdfptr, pdffile);
00047             pdfstreamlength += pdfptr;
00048             }
00049         return;
00050     }
00051 
00052     cur_file_name = NULL;
00053     if (pdfstreamlength == 0) {
00054         c_stream.zalloc = (alloc_func)0;
00055         c_stream.zfree = (free_func)0;
00056         c_stream.opaque = (voidpf)0;
00057         check_err(deflateInit(&c_stream, fixedcompresslevel), "deflateInit");
00058         c_stream.next_out = (Bytef*)zipbuf;
00059         c_stream.avail_out = ZIP_BUF_SIZE;
00060     }
00061     c_stream.next_in = pdfbuf;
00062     c_stream.avail_in = pdfptr;
00063     for(;;) {
00064         if (c_stream.avail_out == 0) {
00065             pdfgone += xfwrite(zipbuf, 1, ZIP_BUF_SIZE, pdffile);
00066             c_stream.next_out = (Bytef*)zipbuf;
00067             c_stream.avail_out = ZIP_BUF_SIZE;
00068         }
00069         err = deflate(&c_stream, finish ? Z_FINISH : Z_NO_FLUSH);
00070         if (finish && err == Z_STREAM_END)
00071             break;
00072         check_err(err, "deflate");
00073         if (!finish && c_stream.avail_in == 0)
00074             break;
00075     }
00076     if (finish) {
00077         if (c_stream.avail_out < ZIP_BUF_SIZE) /* at least one byte has been output */
00078             pdfgone += xfwrite(zipbuf, 1, ZIP_BUF_SIZE - c_stream.avail_out, pdffile);
00079         check_err(deflateEnd(&c_stream), "deflateEnd");
00080         xfflush(pdffile);
00081     }
00082     pdfstreamlength = c_stream.total_out;
00083 }