]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - util.c
Merge remote branch 'origin/v2.3'
[thirdparty/sarg.git] / util.c
diff --git a/util.c b/util.c
index b259265d79f896e8422a6af21fe3797b14f78fe8..0c4083bc9e35b9efd0569e4912665a1d9c962e73 100644 (file)
--- a/util.c
+++ b/util.c
@@ -246,6 +246,85 @@ int getword_atoi(int *number, struct getwordstruct *gwarea, char stop)
        return(0);
 }
 
+int getword_atol(long int *number, struct getwordstruct *gwarea, char stop)
+{
+       long int x;
+       long int sign=+1;
+       int digit;
+
+       if (gwarea->current[0] == '-') {
+               gwarea->current++;
+               sign=-1;
+       } else if (gwarea->current[0] == '+') {
+               gwarea->current++;
+       }
+       *number=0;
+       for(x=0;isdigit(gwarea->current[x]);x++) {
+               digit=gwarea->current[x]-'0';
+               if (*number > (LONG_MAX-digit)/10) {
+                       debuga(_("Integer overflow detected in getword_atol in line %s\n"),gwarea->beginning);
+                       return(-1);
+               }
+               *number=(*number * 10) + digit;
+       }
+       if(gwarea->current[x] && gwarea->current[x]!=stop) {
+               debuga(_("getword_atol loop detected after %ld bytes.\n"),x);
+               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(_("Record=\"%s\"\n"),gwarea->current);
+               debuga(_("searching for \'x%x\'\n"),stop);
+               //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+#if USE_GETWORD_BACKTRACE
+               getword_backtrace();
+#endif
+               return(-1);
+       }
+       *number*=sign;
+
+       if (gwarea->current[x]) ++x;
+       gwarea->current+=x;
+       return(0);
+}
+
+int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char stop)
+{
+       unsigned long int x;
+       int digit;
+
+       if (gwarea->current[0] == '-') {
+               debuga(_("getword_atolu got a negative number.\n"));
+               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(_("Record=\"%s\"\n"),gwarea->current);
+               return(-1);
+       }
+       if (gwarea->current[0] == '+') {
+               gwarea->current++;
+       }
+       *number=0;
+       for(x=0;isdigit(gwarea->current[x]);x++) {
+               digit=gwarea->current[x]-'0';
+               if (*number > (ULONG_MAX-digit)/10) {
+                       debuga(_("Integer overflow detected in getword_atolu in line %s\n"),gwarea->beginning);
+                       return(-1);
+               }
+               *number=(*number * 10) + digit;
+       }
+       if(gwarea->current[x] && gwarea->current[x]!=stop) {
+               debuga(_("getword_atolu loop detected after %ld bytes.\n"),x);
+               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(_("Record=\"%s\"\n"),gwarea->current);
+               debuga(_("searching for \'x%x\'\n"),stop);
+               //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+#if USE_GETWORD_BACKTRACE
+               getword_backtrace();
+#endif
+               return(-1);
+       }
+
+       if (gwarea->current[x]) ++x;
+       gwarea->current+=x;
+       return(0);
+}
+
 
 int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop)
 {