]>
git.ipfire.org Git - thirdparty/sarg.git/blob - decomp.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 #include "include/conf.h"
28 #include "include/defs.h"
38 * Read from gzip file.
40 * \param Data The file object.
41 * \param Buffer The boffer to store the data read.
42 * \param Size How many bytes to read.
44 * \return The number of bytes read.
46 static int Gzip_Read(void *Data
,void *Buffer
,int Size
)
48 return(gzread((gzFile
)Data
,Buffer
,Size
));
52 * Check if end of file is reached.
54 * \param Data The file object.
56 * \return \c True if end of file is reached.
58 static int Gzip_Eof(void *Data
)
60 return(gzeof((gzFile
)Data
));
64 * Return to the beginnig of the file.
66 * \param Data The file object.
68 static void Gzip_Rewind(void *Data
)
70 gzrewind((gzFile
)Data
);
76 * \param Data File to close.
78 * \return 0 on success or -1 on error.
80 static int Gzip_Close(void *Data
)
85 Status
=gzclose((gzFile
)Data
);
92 FileObject_SetLastCloseError(_("Invalid gzip file"));
95 FileObject_SetLastCloseError(_("File operation error"));
98 FileObject_SetLastCloseError(_("Not enough memory"));
101 FileObject_SetLastCloseError(_("Truncated gzip stream"));
104 FileObject_SetLastCloseError(_("Unknown error returned by zlib"));
111 * Open a file object to read from a gzip file.
113 * \return The object to pass to other function in this module.
115 static FileObject
*Gzip_Open(int fd
)
119 FileObject_SetLastOpenError(NULL
);
120 File
=calloc(1,sizeof(*File
));
123 FileObject_SetLastOpenError(_("Not enough memory"));
126 File
->Data
=gzdopen(fd
,"rb");
130 FileObject_SetLastOpenError(_("Error opening gzip file"));
133 File
->Read
=Gzip_Read
;
135 File
->Rewind
=Gzip_Rewind
;
136 File
->Close
=Gzip_Close
;
143 struct BzlibInternalFile
147 //! \c True if end of file is reached.
149 //! Original file in case a rewind is necessary.
154 * Read from bzip file.
156 * \param Data The file object.
157 * \param Buffer The boffer to store the data read.
158 * \param Size How many bytes to read.
160 * \return The number of bytes read.
162 static int Bzip_Read(void *Data
,void *Buffer
,int Size
)
164 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
168 if (BData
->Eof
) return(0);
169 nread
=BZ2_bzRead(&bzerr
,BData
->BzFile
,Buffer
,Size
);
170 if (bzerr
==BZ_STREAM_END
)
172 else if (bzerr
!=BZ_OK
)
178 * Check if end of file is reached.
180 * \param Data The file object.
182 * \return \c True if end of file is reached.
184 static int Bzip_Eof(void *Data
)
186 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
191 * Return to the beginnig of the file.
193 * \param Data The file object.
195 static void Bzip_Rewind(void *Data
)
197 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
200 BZ2_bzReadClose(&bzerr
,BData
->BzFile
);
202 BData
->BzFile
=BZ2_bzReadOpen(&bzerr
,BData
->File
,0,0,NULL
,0);
205 debuga(__FILE__
,__LINE__
,_("Cannot rewind bzip file\n"));
214 * \param Data File to close.
216 * \return 0 on success or -1 on error.
218 static int Bzip_Close(void *Data
)
220 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
223 BZ2_bzReadClose(&bzerr
,BData
->BzFile
);
230 * Open a file object to read from a bzip file.
232 * \return The object to pass to other function in this module.
234 static FileObject
*Bzip_Open(int fd
)
237 struct BzlibInternalFile
*BData
;
240 FileObject_SetLastOpenError(NULL
);
241 File
=calloc(1,sizeof(*File
));
244 FileObject_SetLastOpenError(_("Not enough memory"));
247 BData
=calloc(1,sizeof(*BData
));
251 FileObject_SetLastOpenError(_("Not enough memory"));
254 BData
->File
=fdopen(fd
,"rb");
255 if (BData
->File
==NULL
)
259 FileObject_SetLastOpenError(_("Error duplicating file descriptor"));
263 BData
->BzFile
=BZ2_bzReadOpen(&bzerr
,BData
->File
,0,0,NULL
,0);
269 FileObject_SetLastOpenError(_("Error opening bzip file"));
272 File
->Read
=Bzip_Read
;
274 File
->Rewind
=Bzip_Rewind
;
275 File
->Close
=Bzip_Close
;
282 Open the log file. If it is compressed, uncompress it through a pipe.
284 Log files compressed with gzip, bzip2 or compress are uncompressed with zcat or bzcat.
286 If the log file does not exist, the process terminates with an error message.
288 \param arq The log file to process.
289 \param pipe A variable set to \c true if the log file is opened through a pipe or set to \c false if the file is open directly.
291 \date 2009-09-24 - F Marchal\n This function used to uncompress .Z files in
292 place using uncompress but that required a write access to the log directory,
293 could conflict with logrotate and could leave the log file uncompressed if sarg
294 crashed. According to the documentation, zcat is capable of uncompressing .Z
295 files so it is now used.
297 \date 2010-05-10 - F Marchal\n
298 The function doesn't use a temporary file any more and read the compressed file through a pipe.
300 FileObject
*decomp(const char *arq
)
304 unsigned char buf
[3];
308 fd
=open(arq
,O_RDONLY
| O_LARGEFILE
);
310 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"),arq
,strerror(errno
));
313 nread
=read(fd
,buf
,sizeof(buf
));
315 debuga(__FILE__
,__LINE__
,_("Error while reading \"%s\" to guess its type: %s\n"),arq
,strerror(errno
));
319 debuga(__FILE__
,__LINE__
,_("File \"%s\" is too small to guess its type\n"),arq
);
322 if (lseek(fd
,0,SEEK_SET
)==-1) {
323 debuga(__FILE__
,__LINE__
,_("Cannot return to the beginning of file \"%s\": %s"),arq
,strerror(errno
));
327 if (buf
[0]==0x1F && buf
[1]==0x8B && buf
[2]==0x08)//gzip file
332 debuga(__FILE__
,__LINE__
,_("Sarg was not compiled with gzip support to read file \"%s\"\n"),arq
);
336 else if (buf
[0]==0x42 && buf
[1]==0x5A && buf
[2]==0x68)//bzip2 file
341 debuga(__FILE__
,__LINE__
,_("Sarg was not compiled with bzip support to read file \"%s\"\n"),arq
);
345 else if (buf
[0]==0x1F && (buf
[1]==0x9D || buf
[1]==0xA0))//LZW and LZH compressed file
347 debuga(__FILE__
,__LINE__
,_("Support for LZW and LZH compressed files was removed in sarg 2.4.\n"
348 "You can still read such a file with a command like this:\n"
349 " zcat \"%s\" | sarg - [your usual options here]\n"
350 "If you think it is important for sarg to read those files, open a bug ticket at <http://sourceforge.net/p/sarg/bugs/>.\n"),
356 fi
=FileObject_FdOpen(fd
);