xLib/gz_tool.h File Reference

gzツールプログラム ヘッダ (-lz) More...

#include "tools.h"
#include "buffer.h"
#include <zlib.h>
Include dependency graph for gz_tool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define GZIP_DEFLATE_ID1   0x78
#define GZIP_DEFLATE_ID2   0xda

Functions

Buffer gz_decode_data (Buffer enc)
 圧縮データ encを解凍する.
void deflate2gzip (Buffer *def)
 deflateデータを gzipのデータ構造に変換する.
int gz_decode_fp (FILE *infp, FILE *otfp)
 ファイルポインタによるファイルの解凍
int gz_encode_gzfp (FILE *fp, gzFile *gf)
 GZIPのファイルポインタを用いた ファイルの圧縮.
int gz_decode_gzfp (gzFile *gf, FILE *fp)
 GZIPのファイルポインタを用いた ファイルの解凍.
int gz_encode_file (const char *ffn, const char *tfn)
 ファイル名による ファイルの圧縮
int gz_decode_file (const char *ffn, const char *tfn)
 ファイル名による ファイルの解凍
int gz_decode_file_replace (const char *fn, const char *dir)
 ファイル名による ファイルの解凍.ファイルを置き換える.

Detailed Description

Author:
Fumi.Iseki (C)

Definition in file gz_tool.h.


Define Documentation

#define GZIP_DEFLATE_ID1   0x78

Definition at line 24 of file gz_tool.h.

Referenced by deflate2gzip().

#define GZIP_DEFLATE_ID2   0xda

Definition at line 25 of file gz_tool.h.

Referenced by deflate2gzip().


Function Documentation

void deflate2gzip ( Buffer def  ) 

deflateデータを gzipのデータ構造に変換する.

zlibの関数群は deflateを自動判別するようなので,zlibの関数を直接使用する場合はこの関数は必要ない.
ファイルとして保存する場合は,gzipの headerとtailerを付加する必要があるが,圧縮前のCRC32とISIZEは通常は計算できない.
従ってこの headerと tailerを付加したファイルは gunzuipコマンドでは解凍できない.zcat はエラーを出すが,解凍自体は可能.
headerと tailerを付加しても,zlibの関数群では使用可能

Parameters:
[out] def 変換する deflateのデータが保存された Buffer変数へのポインタ.
See also:
http://www.ietf.org/rfc/rfc1952.txt

Definition at line 100 of file gz_tool.c.

References Buffer::buf, cat_b2Buffer(), free_Buffer(), GZIP_DEFLATE_ID1, GZIP_DEFLATE_ID2, init_Buffer(), make_Buffer(), and Buffer::vldsz.

00101 {
00102     unsigned char gzip_header[] = { 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
00103     unsigned char gzip_tailer[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};    // CRC32 4Byte, ISIZE 4Byte
00104 
00105     Buffer gzp = init_Buffer();
00106 
00107     if (def->buf[0]==GZIP_DEFLATE_ID1 && def->buf[1]==GZIP_DEFLATE_ID2) {
00108         gzp = make_Buffer(def->vldsz+16);
00109         cat_b2Buffer(gzip_header, &gzp, 10);
00110         cat_b2Buffer(def->buf+2, &gzp, def->vldsz-2);       
00111         cat_b2Buffer(gzip_tailer, &gzp, 8);
00112         free_Buffer(def);
00113         *def = gzp;
00114     }
00115 
00116     return;
00117 }

Here is the call graph for this function:

Buffer gz_decode_data ( Buffer  enc  ) 

Buffer gz_decode_data(Buffer enc)

gzipデータ encを解凍して返す.データエラーが発生しても,解凍可能なところまでは解凍する.

Parameters:
enc 解凍する gzipデータが格納された Buffer変数.
Returns:
解凍データが格納された Buffer変数.

Definition at line 33 of file gz_tool.c.

References Buffer::buf, Buffer::bufsz, BUFSZ, cat_Buffer(), DEBUG_MODE, free_Buffer(), init_Buffer(), JBXL_ERROR, make_Buffer(), PRINT_MESG, Buffer::state, and Buffer::vldsz.

00034 {
00035     Buffer dec = init_Buffer();
00036 
00037     if (enc.buf==NULL) return dec;
00038 
00039     z_stream zstrm;
00040     zstrm.zalloc    = Z_NULL;
00041     zstrm.zfree     = Z_NULL;
00042     zstrm.opaque    = Z_NULL;
00043     zstrm.next_in   = Z_NULL;
00044     zstrm.avail_in  = 0;
00045 
00046     int ret = inflateInit2(&zstrm, 47);
00047     if (ret!=Z_OK) return dec;
00048 
00049     //
00050     dec = make_Buffer(enc.vldsz*5);
00051     Buffer wrk = make_Buffer(BUFSZ);
00052 
00053     zstrm.next_in  = enc.buf;
00054     zstrm.avail_in = (size_t)enc.vldsz;
00055 
00056     do {
00057         zstrm.next_out  = wrk.buf;
00058         zstrm.avail_out = (size_t)wrk.bufsz; 
00059 
00060         ret = inflate(&zstrm, Z_NO_FLUSH);
00061         if (ret!=Z_OK && ret!=Z_STREAM_END) {
00062             wrk.vldsz = wrk.bufsz - zstrm.avail_out;
00063             cat_Buffer(&wrk, &dec);
00064             inflateEnd(&zstrm);
00065             free_Buffer(&wrk);
00066             //
00067             DEBUG_MODE PRINT_MESG("GZ_DECODE_DATA: ERROR: inflate error = %d\n", ret);
00068             dec.state = JBXL_ERROR;
00069             return dec;
00070         }
00071 
00072         wrk.vldsz = wrk.bufsz - zstrm.avail_out; 
00073         cat_Buffer(&wrk, &dec);
00074 
00075         if (ret==Z_STREAM_END && zstrm.avail_in!=0) {
00076             ret = inflateReset(&zstrm);
00077         } 
00078 
00079     } while (ret!=Z_STREAM_END && zstrm.avail_in>0);
00080     //
00081 
00082     inflateEnd(&zstrm);
00083     free_Buffer(&wrk);
00084 
00085     return dec;
00086 }

Here is the call graph for this function:

int gz_decode_file ( const char *  fmfn,
const char *  tofn 
)

int gz_decode_file(const char* fmfn, const char* tofn)

gzipファイル fmfnを解凍して ファイル tofnへ保存する.

Parameters:
fmfn 解凍する gzipファイル名
tofn 解凍されたファイル名
Returns:
書き込んだファイル(tofn)のサイズ.

Definition at line 297 of file gz_tool.c.

References gz_decode_fp(), JBXL_FILE_DESTOPEN_ERROR, and JBXL_FILE_OPEN_ERROR.

Referenced by gz_decode_file_replace().

00298 {
00299     FILE* infp;
00300     FILE* otfp;
00301 
00302     infp = fopen(fmfn, "rb");
00303     if (infp==NULL) return JBXL_FILE_OPEN_ERROR;
00304 
00305     otfp = fopen(tofn, "wb");
00306     if (otfp==NULL) {
00307         fclose(infp);
00308         return JBXL_FILE_DESTOPEN_ERROR;
00309     }
00310 
00311     int sz = gz_decode_fp(infp, otfp);
00312 
00313     fclose(infp);
00314     fclose(otfp);
00315 
00316     return sz;
00317 
00318 /*
00319     int  cc, sz;
00320     unsigned char buf[RECVBUFSZ];
00321     gzFile gf;
00322     FILE*  fp;
00323 
00324     gf = gzopen(fmfn, "rb");
00325     if (gf==NULL) return JBXL_FILE_OPEN_ERROR;
00326 
00327     fp = fopen(tofn, "wb");
00328     if (fp==NULL) {
00329         gzclose(gf);
00330         return JBXL_FILE_DESTOPEN_ERROR;
00331     }
00332 
00333     memset(buf, 0, RECVBUFSZ);
00334     sz = cc = gzread(gf, (voidp)buf, RECVBUFSZ);
00335     while(cc>0) {
00336         fwrite(buf, cc, 1, fp);
00337         memset(buf, 0, cc);
00338         cc = gzread(gf, (voidp)buf, RECVBUFSZ);
00339         sz += cc;
00340     }
00341 
00342     gzclose(gf);    
00343     fclose(fp);
00344 
00345     return sz;
00346 */
00347 }

Here is the call graph for this function:

Here is the caller graph for this function:

int gz_decode_file_replace ( const char *  fn,
const char *  tempdir 
)

int gz_decode_file_replace(const char* fn, const char* tempdir)

gzipファイル fnを解凍して,通常のファイルで置き換える.

Parameters:
fn 解凍する gzipファイル名
tempdir 作業用ディレクトリ.NULLも可.
Returns:
書き込んだファイル(tofn)のサイズ.

Definition at line 359 of file gz_tool.c.

References freeNull, gz_decode_file(), and temp_filename().

Referenced by recv_http_file(), and recv_https_file().

00360 {
00361     int cc;
00362     char* tempfn;
00363 
00364     tempfn = temp_filename(tempdir, 16);
00365     cc = gz_decode_file(fn, tempfn);
00366     if (cc<=0) {
00367         unlink(tempfn);
00368         free(tempfn);
00369         return cc;
00370     }
00371 
00372     unlink(fn);
00373     rename(tempfn, fn);
00374     freeNull(tempfn);
00375 
00376     return cc;
00377 } 

Here is the call graph for this function:

Here is the caller graph for this function:

int gz_decode_fp ( FILE *  infp,
FILE *  otfp 
)

int gz_decode_fp(FILE* infp, FILE* otfp)

gzipファイル infnを解凍して ファイル otfnへ保存する.データエラーが発生しても,解凍可能なところまでは解凍する.

Parameters:
infp 解凍する gzipファイルのファイルポインタ.
otfp 解凍されたファイルのファイルポインタ.
Returns:
書き込んだファイルのサイズ.

Definition at line 134 of file gz_tool.c.

References BUFSZ, DEBUG_MODE, JBXL_ERROR, LBUF, and PRINT_MESG.

Referenced by gz_decode_file().

00135 {
00136     if (infp==NULL || otfp==NULL) return 0;
00137     
00138     int sz = 0;
00139     unsigned char inbuf[LBUF];
00140     unsigned char otbuf[BUFSZ];
00141 
00142     //
00143     z_stream zstrm;
00144     zstrm.zalloc    = Z_NULL;
00145     zstrm.zfree     = Z_NULL;
00146     zstrm.opaque    = Z_NULL;
00147     zstrm.next_in   = Z_NULL;
00148     zstrm.avail_in  = 0;
00149 
00150     int ret = inflateInit2(&zstrm, 47);
00151     if (ret!=Z_OK) return JBXL_ERROR;
00152 
00153     do {
00154         zstrm.next_in  = inbuf;
00155         zstrm.avail_in = (int)fread(inbuf, 1, LBUF, infp);
00156 
00157         do {
00158             zstrm.next_out  = otbuf;
00159             zstrm.avail_out = BUFSZ; 
00160 
00161             ret = inflate(&zstrm, Z_NO_FLUSH);
00162             if (ret!=Z_OK && ret!=Z_STREAM_END) {
00163                 fwrite(otbuf, BUFSZ-zstrm.avail_out, 1, otfp);          
00164                 //
00165                 inflateEnd(&zstrm);
00166                 DEBUG_MODE PRINT_MESG("GZ_DECODE_FP: ERROR: inflate error = %d\n", ret);
00167                 if (sz==0) sz = JBXL_ERROR;
00168                 return sz;
00169             }
00170 
00171             int otsz = BUFSZ - zstrm.avail_out; 
00172             fwrite(otbuf, otsz, 1, otfp);           
00173             sz += otsz;
00174 
00175             if (ret==Z_STREAM_END && zstrm.avail_in!=0) {
00176                 ret = inflateReset(&zstrm);
00177             } 
00178 
00179         } while (ret!=Z_STREAM_END && zstrm.avail_in>0);
00180         //
00181     } while(!feof(infp));
00182 
00183     inflateEnd(&zstrm);
00184 
00185     return sz;
00186 }

Here is the caller graph for this function:

int gz_decode_gzfp ( gzFile *  gf,
FILE *  fp 
)

int gz_decode_gzfp(gzFile* gf, FILE* fp)

gzip ファイル gfを解凍して ファイル fpへ保存する. gzread()で読んで fwrite()で書き込む.

Parameters:
gf 読み込み用の gzipファイルへのポインタ.
fp 書き込み用の通常ファイルへのポインタ.
Returns:
書き込んだファイルのサイズ.

Definition at line 227 of file gz_tool.c.

References JBXL_ARGS_ERROR, and RECVBUFSZ.

00228 {
00229     int  cc, sz;
00230     unsigned char buf[RECVBUFSZ];
00231 
00232     if (*gf==NULL) return JBXL_ARGS_ERROR;
00233 
00234     memset(buf, 0, RECVBUFSZ);
00235     sz = cc = gzread(*gf, (voidp)buf, RECVBUFSZ);
00236     while(cc>0) {
00237         fwrite(buf, cc, 1, fp);
00238         memset(buf, 0, cc);
00239         cc = gzread(*gf, (voidp)buf, RECVBUFSZ);
00240         sz += cc;
00241     }
00242 
00243     return sz;
00244 }

int gz_encode_file ( const char *  fmfn,
const char *  tofn 
)

int gz_encode_file(const char* fmfn, const char* tofn)

ファイル fmfnを圧縮して gzipファイル tofnへ保存する.fread()で読んで,gzwrite()で書き込む.

Parameters:
fmfn 圧縮するファイル名
tofn 圧縮された gzip ファイル名
Returns:
読み込んだファイル(fmfn)のサイズ.

Definition at line 256 of file gz_tool.c.

References JBXL_FILE_DESTOPEN_ERROR, JBXL_FILE_OPEN_ERROR, and RECVBUFSZ.

00257 {
00258     size_t cc, sz;
00259     unsigned char buf[RECVBUFSZ];
00260     gzFile gf;
00261     FILE*  fp;
00262 
00263     fp = fopen(fmfn, "rb");
00264     if (fp==NULL) return JBXL_FILE_OPEN_ERROR;
00265 
00266     gf = gzopen(tofn, "wb");
00267     if (gf==NULL) {
00268         fclose(fp);
00269         return JBXL_FILE_DESTOPEN_ERROR;
00270     }
00271 
00272     memset(buf, 0, RECVBUFSZ);
00273     sz = cc = fread(buf, RECVBUFSZ, 1, fp);
00274     while(cc>0) {
00275         gzwrite(gf, (voidp)buf, (unsigned int)cc);
00276         memset(buf, 0, cc);
00277         cc = fread(buf, RECVBUFSZ, 1, fp);
00278         sz += cc;
00279     }
00280 
00281     gzclose(gf);    
00282     fclose(fp);
00283 
00284     return (int)sz;
00285 }

int gz_encode_gzfp ( FILE *  fp,
gzFile *  gf 
)

Definition at line 198 of file gz_tool.c.

References JBXL_ARGS_ERROR, and RECVBUFSZ.

00199 {
00200     size_t  cc, sz;
00201     unsigned char buf[RECVBUFSZ];
00202 
00203     if (*gf==NULL) return JBXL_ARGS_ERROR;
00204 
00205     memset(buf, 0, RECVBUFSZ);
00206     sz = cc = (int)fread(buf, RECVBUFSZ, 1, fp);
00207     while(cc>0) {
00208         gzwrite(*gf, (voidp)buf, (unsigned int)cc);
00209         memset(buf, 0, cc);
00210         cc = fread(buf, RECVBUFSZ, 1, fp);
00211         sz += cc;
00212     }
00213 
00214     return (int)sz;
00215 }


Generated on 15 Nov 2023 for JunkBox_Lib by  doxygen 1.6.1