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"
30 //! The size, in bytes, to allocate from the start.
31 #define INITIAL_LINE_BUFFER_SIZE 32768
33 The amount by which the line buffer size is increased when it turns out to be too small to accomodate
36 #define LINE_BUFFER_SIZE_INCREMENT 8192
38 Maximum size of the line buffer.
40 A text line read from the file must be smaller than this value or the functions fails
41 and aborts the program.
43 10MB should not be a problem as most of the line is filled with the URL and squid 3
44 limits the URL to 4096 bytes (see MAX_URL). Squid has reportedly been tested with
45 MAX_URL set up to 32KB so I'm not expecting URL much longer than that.
47 Other proxies might handle longer URLs but certainly not longer than 10MB.
49 Now, why put a limit? Sarg version 2.3 originaly had no limits until sarg 2.3.3. At
50 that point a user with a defective network mount point reported that sarg was eating
51 up 8GB of memory available on the server triggering the OOM killer. So the limit is
52 here to prevent sarg from choking on an invalid file.
54 #define MAX_LINE_BUFFER_SIZE (10*1024*1024)
58 //! The buffer to store the data read from the log file.
60 //! The size of the buffer.
62 //! The number of bytes stored in the buffer.
64 //! The position of the beginning of the current string.
66 //! The position of the end of the current string.
70 longline
longline_create(void)
74 line
=malloc(sizeof(*line
));
75 if (line
==NULL
) return(NULL
);
76 line
->size
=INITIAL_LINE_BUFFER_SIZE
;
77 line
->buffer
=malloc(line
->size
);
78 if (line
->buffer
==NULL
) {
88 void longline_reset(longline line
)
97 char *longline_read(FILE *fp_in
,longline line
)
103 if (line
==NULL
|| line
->buffer
==NULL
) return(NULL
);
106 for (i
=line
->end
; i
<line
->length
&& (line
->buffer
[i
]=='\n' || line
->buffer
[i
]=='\r') ; i
++);
107 if (i
<line
->length
) {
111 nread
=(feof(fp_in
)!=0) ? 0 : fread(line
->buffer
,1,line
->size
,fp_in
);
112 if (nread
==0) return(NULL
);
117 line
->start
=line
->end
;
119 for (i
=line
->end
; i
<line
->length
; i
++) {
120 if ((unsigned char)line
->buffer
[i
]>=' ') continue;
121 if (line
->buffer
[i
]=='\n' || line
->buffer
[i
]=='\r') break;
125 if (line
->end
<line
->length
) break;
128 for (i
=line
->start
; i
<line
->length
; i
++) line
->buffer
[i
-line
->start
]=line
->buffer
[i
];
129 line
->length
-=line
->start
;
130 line
->end
-=line
->start
;
133 if (line
->length
>=line
->size
) {
134 line
->size
+=LINE_BUFFER_SIZE_INCREMENT
;
135 if (line
->size
>=MAX_LINE_BUFFER_SIZE
) {
136 debuga(_("A text line is more than %d bytes long denoting a corrupted file\n"),MAX_LINE_BUFFER_SIZE
);
139 newbuf
=realloc(line
->buffer
,line
->size
);
141 debuga(_("Not enough memory to read one more line from the file\n"));
146 nread
=(feof(fp_in
)!=0) ? 0 : fread(line
->buffer
+line
->length
,1,line
->size
-line
->length
,fp_in
);
148 if (line
->end
<=line
->start
) return(NULL
);
149 if (line
->end
>=line
->size
) {
150 line
->end
=line
->size
;
152 newbuf
=realloc(line
->buffer
,line
->size
);
154 debuga(_("Not enough memory to read one more line from the file\n"));
159 line
->buffer
[line
->end
]='\0';
160 return(line
->buffer
+line
->start
);
164 line
->buffer
[line
->end
++]='\0';
165 return(line
->buffer
+line
->start
);
168 void longline_destroy(longline
*line_ptr
)
172 if (line_ptr
==NULL
|| *line_ptr
==NULL
) return;
175 if (line
->buffer
!=NULL
) free(line
->buffer
);