]>
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 //! Copy of the original file handle.
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
;
167 nread
=BZ2_bzread(BData
->BzFile
,Buffer
,Size
);
168 if (nread
==0) BData
->Eof
=true;
173 * Check if end of file is reached.
175 * \param Data The file object.
177 * \return \c True if end of file is reached.
179 static int Bzip_Eof(void *Data
)
181 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
186 * Return to the beginnig of the file.
188 * \param Data The file object.
190 static void Bzip_Rewind(void *Data
)
192 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
195 BZ2_bzclose(BData
->BzFile
);
199 debuga(__FILE__
,__LINE__
,_("Cannot rewind bzip file\n"));
202 BData
->BzFile
=BZ2_bzdopen(fd
,"rb");
205 debuga(__FILE__
,__LINE__
,_("Cannot rewind bzip file\n"));
213 * \param Data File to close.
215 * \return 0 on success or -1 on error.
217 static int Bzip_Close(void *Data
)
219 struct BzlibInternalFile
*BData
=(struct BzlibInternalFile
*)Data
;
221 BZ2_bzclose(BData
->BzFile
);
228 * Open a file object to read from a bzip file.
230 * \return The object to pass to other function in this module.
232 static FileObject
*Bzip_Open(int fd
)
235 struct BzlibInternalFile
*BData
;
237 FileObject_SetLastOpenError(NULL
);
238 File
=calloc(1,sizeof(*File
));
241 FileObject_SetLastOpenError(_("Not enough memory"));
244 BData
=calloc(1,sizeof(*BData
));
248 FileObject_SetLastOpenError(_("Not enough memory"));
256 FileObject_SetLastOpenError(_("Error duplicating file descriptor"));
260 BData
->BzFile
=BZ2_bzdopen(fd
,"rb");
266 FileObject_SetLastOpenError(_("Error opening bzip file"));
269 File
->Read
=Bzip_Read
;
271 File
->Rewind
=Bzip_Rewind
;
272 File
->Close
=Bzip_Close
;
279 Open the log file. If it is compressed, uncompress it through a pipe.
281 Log files compressed with gzip, bzip2 or compress are uncompressed with zcat or bzcat.
283 If the log file does not exist, the process terminates with an error message.
285 \param arq The log file to process.
286 \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.
288 \date 2009-09-24 - F Marchal\n This function used to uncompress .Z files in
289 place using uncompress but that required a write access to the log directory,
290 could conflict with logrotate and could leave the log file uncompressed if sarg
291 crashed. According to the documentation, zcat is capable of uncompressing .Z
292 files so it is now used.
294 \date 2010-05-10 - F Marchal\n
295 The function doesn't use a temporary file any more and read the compressed file through a pipe.
297 FileObject
*decomp(const char *arq
)
301 unsigned char buf
[3];
305 fd
=open(arq
,O_RDONLY
| O_LARGEFILE
);
307 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"),arq
,strerror(errno
));
310 nread
=read(fd
,buf
,sizeof(buf
));
312 debuga(__FILE__
,__LINE__
,_("Error while reading \"%s\" to guess its type: %s\n"),arq
,strerror(errno
));
316 debuga(__FILE__
,__LINE__
,_("File \"%s\" is too small to guess its type\n"),arq
);
319 if (lseek(fd
,0,SEEK_SET
)==-1) {
320 debuga(__FILE__
,__LINE__
,_("Cannot return to the beginning of file \"%s\": %s"),arq
,strerror(errno
));
324 if (buf
[0]==0x1F && buf
[1]==0x8B && buf
[2]==0x08)//gzip file
329 debuga(__FILE__
,__LINE__
,_("Sarg was not compiled with gzip support to read file \"%s\"\n"),arq
);
333 else if (buf
[0]==0x42 && buf
[1]==0x5A && buf
[2]==0x68)//bzip2 file
338 debuga(__FILE__
,__LINE__
,_("Sarg was not compiled with bzip support to read file \"%s\"\n"),arq
);
342 else if (buf
[0]==0x1F && (buf
[1]==0x9D || buf
[1]==0xA0))//LZW and LZH compressed file
344 debuga(__FILE__
,__LINE__
,_("Support for LZW and LZH compressed files was removed in sarg 2.4.\n"
345 "You can still read such a file with a command like this:\n"
346 " zcat \"%s\" | sarg - [your usual options here]\n"
347 "If you think it is important for sarg to read those files, open a bug ticket at <http://sourceforge.net/p/sarg/bugs/>.\n"),
353 fi
=FileObject_FdOpen(fd
);