]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
dtable/etable initialization code in libcitadel was
[citadel.git] / libcitadel / lib / tools.c
index 016ec9ab82c884984274a317114f51321eb07f43..61e49f0d089de8fc528fab6ee3e6e54e4c5a29d2 100644 (file)
@@ -1,5 +1,6 @@
 /*
- *
+ * A basic toolset containing miscellaneous functions for string manipluation,
+ * encoding/decoding, and a bunch of other stuff.
  */
 
 
@@ -35,12 +36,6 @@ typedef unsigned char byte;        /* Byte type */
 static byte dtable[256] = "\0";              /* base64 decode table */
 static byte etable[256] = "\0";              /* base64 encode table */
 
-/* Month strings for date conversions */
-char *ascmonths[12] = {
-       "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
 char *safestrncpy(char *dest, const char *src, size_t n)
 {
        int i = 0;
@@ -233,8 +228,10 @@ int extract_int(const char *source, int parmnum)
 {
        char buf[32];
        
-       extract_token(buf, source, parmnum, '|', sizeof buf);
-       return(atoi(buf));
+       if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
+               return(atoi(buf));
+       else
+               return 0;
 }
 
 /*
@@ -244,8 +241,10 @@ long extract_long(const char *source, int parmnum)
 {
        char buf[32];
        
-       extract_token(buf, source, parmnum, '|', sizeof buf);
-       return(atol(buf));
+       if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
+               return(atol(buf));
+       else
+               return 0;
 }
 
 
@@ -256,8 +255,10 @@ unsigned long extract_unsigned_long(const char *source, int parmnum)
 {
        char buf[32];
 
-       extract_token(buf, source, parmnum, '|', sizeof buf);
-       return strtoul(buf, NULL, 10);
+       if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
+               return strtoul(buf, NULL, 10);
+       else 
+               return 0;
 }
 
 
@@ -308,18 +309,6 @@ size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int li
        int dpos = 0;
        int thisline = 0;
 
-       /**  Fill dtable with character encodings.  */
-
-       for (i = 0; i < 26; i++) {
-               dtable[i] = 'A' + i;
-               dtable[26 + i] = 'a' + i;
-       }
-       for (i = 0; i < 10; i++) {
-               dtable[52 + i] = '0' + i;
-       }
-       dtable[62] = '+';
-       dtable[63] = '/';
-
        while (!hiteof) {
                byte igroup[3], ogroup[4];
                int c, n;
@@ -334,16 +323,16 @@ size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int li
                        igroup[n] = (byte) c;
                }
                if (n > 0) {
-                       ogroup[0] = dtable[igroup[0] >> 2];
+                       ogroup[0] = etable[igroup[0] >> 2];
                        ogroup[1] =
-                           dtable[((igroup[0] & 3) << 4) |
+                           etable[((igroup[0] & 3) << 4) |
                                   (igroup[1] >> 4)];
                        ogroup[2] =
-                           dtable[((igroup[1] & 0xF) << 2) |
+                           etable[((igroup[1] & 0xF) << 2) |
                                   (igroup[2] >> 6)];
-                       ogroup[3] = dtable[igroup[2] & 0x3F];
+                       ogroup[3] = etable[igroup[2] & 0x3F];
 
-                       /**
+                       /*
                         * Replace characters in output stream with "=" pad
                         * characters if fewer than three characters were
                         * read from the end of the input stream. 
@@ -390,8 +379,6 @@ int CtdlDecodeBase64(char *dest, const char *source, size_t length)
     int dpos = 0;
     int spos = 0;
 
-
-    /*CONSTANTCONDITION*/
     while (TRUE) {
        byte a[4], b[4], o[3];
 
@@ -518,6 +505,11 @@ void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
        struct tm tm;
        int hour;
 
+       /* Month strings for date conversions ... this needs to be localized eventually */
+       char *fmt_date_months[12] = {
+               "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+       };
+
        strcpy(buf, "");
        localtime_r(&thetime, &tm);
 
@@ -527,7 +519,7 @@ void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
 
        if (seconds) {
                snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
-                       ascmonths[tm.tm_mon],
+                       fmt_date_months[tm.tm_mon],
                        tm.tm_mday,
                        tm.tm_year + 1900,
                        hour,
@@ -537,7 +529,7 @@ void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
                );
        } else {
                snprintf(buf, n, "%s %d %4d %d:%02d%s",
-                       ascmonths[tm.tm_mon],
+                       fmt_date_months[tm.tm_mon],
                        tm.tm_mday,
                        tm.tm_year + 1900,
                        hour,
@@ -698,29 +690,35 @@ char *myfgets(char *s, int size, FILE *stream) {
        return ret;
 }
 
-/*
- * Escape a string for feeding out as a URL.
- * Output buffer must be big enough to handle escape expansion!
+/** 
+ * \brief Escape a string for feeding out as a URL.
+ * \param outbuf the output buffer
+ * \param oblen the size of outbuf to sanitize
+ * \param strbuf the input buffer
  */
-void urlesc(char *outbuf, char *strbuf)
+void urlesc(char *outbuf, size_t oblen, char *strbuf)
 {
-       int a, b, c;
-       char *ec = " #&;`'|*?-~<>^()[]{}$\\";
+       int a, b, c, len, eclen, olen;
+       char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
 
        strcpy(outbuf, "");
-
-       for (a = 0; a < (int)strlen(strbuf); ++a) {
+       len = strlen(strbuf);
+       eclen = strlen(ec);
+       olen = 0;
+       for (a = 0; a < len; ++a) {
                c = 0;
-               for (b = 0; b < strlen(ec); ++b) {
+               for (b = 0; b < eclen; ++b) {
                        if (strbuf[a] == ec[b])
                                c = 1;
                }
-               b = strlen(outbuf);
-               if (c == 1)
-                       sprintf(&outbuf[b], "%%%02x", strbuf[a]);
-               else
-                       sprintf(&outbuf[b], "%c", strbuf[a]);
+               if (c == 1) {
+                       snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
+                       olen += 3;
+               }
+               else 
+                       outbuf[olen ++] = strbuf[a];
        }
+       outbuf[olen] = '\0';
 }
 
 
@@ -869,7 +867,7 @@ int is_msg_in_mset(char *mset, long msgnum) {
 }
 
 
-/**
+/*
  * \brief searches for a  paternn within asearch string
  * \param search the string to search 
  * \param patn the pattern to find in string