* duplicate memreadline & bmstrcasestr to a const and non const version
authorWilfried Göesgens <willi@citadel.org>
Mon, 19 Jul 2010 20:56:54 +0000 (20:56 +0000)
committerWilfried Göesgens <willi@citadel.org>
Mon, 19 Jul 2010 20:56:54 +0000 (20:56 +0000)
* follow changes in citserver where appropriate.

.gitignore
citadel/internet_addressing.c
citadel/modules/imap/imap_fetch.c
citadel/modules/imap/imap_store.c
citadel/modules/imap/serv_imap.c
citadel/modules/openid/serv_openid_rp.c
citadel/modules/pop3/serv_pop3.c
citadel/modules/smtp/serv_smtp.c
citadel/modules/wiki/serv_wiki.c
libcitadel/lib/libcitadel.h
libcitadel/lib/tools.c

index ba73ec511fb9d42e6966950003bed299667e176f..0966e021dfefb3b552711f088a9192d1e0284d67 100644 (file)
@@ -3,3 +3,4 @@
 *.changes
 *.deb
 *.tar.gz
+webcit-*
index c448001d17300b29858429be60f18b0693a89386..7a9f4b07816810967e106731f4e2781ddb675c36 100644 (file)
@@ -864,13 +864,13 @@ char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
        snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
 
        /* Locate the end of the headers, so we don't run past that point */
-       end_of_headers = bmstrcasestr(rfc822, "\n\r\n");
+       end_of_headers = cbmstrcasestr(rfc822, "\n\r\n");
        if (end_of_headers == NULL) {
-               end_of_headers = bmstrcasestr(rfc822, "\n\n");
+               end_of_headers = cbmstrcasestr(rfc822, "\n\n");
        }
        if (end_of_headers == NULL) return (NULL);
 
-       field_start = bmstrcasestr(rfc822, fieldhdr);
+       field_start = cbmstrcasestr(rfc822, fieldhdr);
        if (field_start == NULL) return(NULL);
        if (field_start > end_of_headers) return(NULL);
 
@@ -878,11 +878,11 @@ char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
        strcpy(fieldbuf, "");
 
        ptr = field_start;
-       ptr = memreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
+       ptr = cmemreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
        while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
                strcat(fieldbuf, " ");
                cont = &fieldbuf[strlen(fieldbuf)];
-               ptr = memreadline(ptr, cont, SIZ-strlen(fieldbuf) );
+               ptr = cmemreadline(ptr, cont, SIZ-strlen(fieldbuf) );
                striplt(cont);
        }
 
index 0ee327cb87ade858d3cbeb4d5d34b7f06659ce83..0a6ee4f1eea60ae03749535332ac0af19be90b71 100644 (file)
@@ -946,7 +946,7 @@ void imap_fetch_bodystructure (long msgnum, const char *item,
 
                ptr = rfc822;
                do {
-                       ptr = memreadline(ptr, buf, sizeof buf);
+                       ptr = cmemreadline(ptr, buf, sizeof buf);
                        ++lines;
                        if ((IsEmptyStr(buf)) && (rfc822_body == NULL)) {
                                rfc822_body = ptr;
index 845690df7c613bbcdfbeb443369ce03475bf5f97..4b2f1b45d30d2cd8d20ad2624c564dc973c73f0d 100644 (file)
@@ -105,7 +105,7 @@ void imap_do_store(citimap_command *Cmd) {
 
        if (Cmd->num_parms < 2) return;
        oper = Cmd->Params[0].Key;
-       if (bmstrcasestr(oper, ".SILENT")) {
+       if (cbmstrcasestr(oper, ".SILENT")) {
                silent = 1;
        }
 
index 5ddae608a15b3beb450efd062779376e2143df50..baee87c1abc0fe933815a6123685e2ab24c5d6b0 100644 (file)
@@ -1426,7 +1426,7 @@ void imap_command_loop(void)
                CtdlLogPrintf(CTDL_INFO, "IMAP: <plain_auth>\n");
        }
        else if ((Imap->authstate == imap_as_expecting_multilineusername) || 
-                bmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
+                cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
                CtdlLogPrintf(CTDL_INFO, "IMAP: LOGIN...\n");
        }
        else {
index 384e2b6e08d8cfaf6931f36f562f975ee91e4a34..638a77bdd5dc031505a5ca696c293c8f87077afd 100644 (file)
@@ -503,7 +503,7 @@ void extract_link(StrBuf *target_buf, const char *rel, long repllen, StrBuf *sou
        ptr = ChrPtr(source_buf);
 
        FlushStrBuf(target_buf);
-       while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
+       while (ptr = cbmstrcasestr(ptr, "<link"), ptr != NULL) {
 
                link_tag_start = ptr;
                link_tag_end = strchr(ptr, '>');
@@ -512,7 +512,7 @@ void extract_link(StrBuf *target_buf, const char *rel, long repllen, StrBuf *sou
                for (i=0; i < 1; i++ ){
                        len = link_tag_end - link_tag_start;
 
-                       rel_start = bmstrcasestr(link_tag_start, "rel=");
+                       rel_start = cbmstrcasestr(link_tag_start, "rel=");
                        if ((rel_start == NULL) ||
                            (rel_start > link_tag_end)) 
                                continue;
@@ -530,7 +530,7 @@ void extract_link(StrBuf *target_buf, const char *rel, long repllen, StrBuf *sou
                        if (strncasecmp(rel, rel_start, repllen)!= 0)
                                continue; /* didn't match? never mind... */
                        
-                       href_start = bmstrcasestr(link_tag_start, "href=");
+                       href_start = cbmstrcasestr(link_tag_start, "href=");
                        if ((href_start == NULL) || 
                            (href_start >= link_tag_end)) 
                                continue;
@@ -842,7 +842,7 @@ void cmd_oidf(char *argbuf) {
        curl_easy_cleanup(curl);
        curl_formfree(formpost);
 
-       if (bmstrcasestr(ChrPtr(ReplyBuf), "is_valid:true")) {
+       if (cbmstrcasestr(ChrPtr(ReplyBuf), "is_valid:true")) {
                oiddata->verified = 1;
        }
        FreeStrBuf(&ReplyBuf);
index 33fa1203d36d72f4d21f5173e1388d799e04405e..2c1402dbde6ff72cbc1aca20fdbbb4448204570b 100644 (file)
@@ -420,7 +420,7 @@ void pop3_top(char *argbuf) {
        cprintf("+OK Message %d:\r\n", which_one);
        
        ptr = ChrPtr(msgtext);
-       while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
+       while (ptr = cmemreadline(ptr, buf, (sizeof buf - 2)),
              ( (*ptr != 0) && (done == 0))) {
                strcat(buf, "\r\n");
                if (in_body == 1) {
index 7d1e26f49df302ed3d87319bdf61b491c9202e58..1d7dd0e7ebb558f1bf9e4732921c2cb3c4cb1ca8 100644 (file)
@@ -1002,7 +1002,7 @@ void smtp_try(const char *key, const char *addr, int *status,
                scan_done = 0;
                ptr = msgtext;
                do {
-                       if (ptr = memreadline(ptr, buf, sizeof buf), *ptr == 0) {
+                       if (ptr = cmemreadline(ptr, buf, sizeof buf), *ptr == 0) {
                                scan_done = 1;
                        }
                        if (!strncasecmp(buf, "From:", 5)) {
index 1011546cfdded663f6691b530f0d2d95d5f4e7e1..624138b1cc936845742a711452494312b5a6d075 100644 (file)
@@ -95,7 +95,7 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
        int nbytes = 0;
        char *diffbuf = NULL;
        size_t diffbuf_len = 0;
-       const char *ptr = NULL;
+       char *ptr = NULL;
 
        if (!CCC->logged_in) return(0); /* Only do this if logged in. */
 
index 92ab07f3ee096d78c23ed59b9e5a8c90867c395d..44e1544e77c9438a799b3365ed7472e16f64c271 100644 (file)
@@ -319,8 +319,10 @@ int haschar(const char *st, int ch);
 void remove_token(char *source, int parmnum, char separator);
 void fmt_date(char *buf, size_t n, time_t thetime, int seconds);
 int is_msg_in_sequence_set(const char *mset, long msgnum);
-const char *memreadline(const char *start, char *buf, int maxlen);
-const char *memreadlinelen(const char *start, char *buf, int maxlen, int *retlen);
+char *memreadline(char *start, char *buf, int maxlen);
+char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen);
+const char *cmemreadline(const char *start, char *buf, int maxlen);
+const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retlen);
 #define IsEmptyStr(a) ((a)[0] == '\0')
 #define num_parms(source)              num_tokens(source,(char)'|')
 int stripout(char *str, char leftboundary, char rightboundary);
@@ -330,8 +332,10 @@ void urlesc(char *outbuf, size_t oblen, char *strbuf);
 char *CtdlTempFileName(char *prefix1, int prefix2);
 FILE *CtdlTempFile(void);
 void generate_uuid(char *buf);
-const char *bmstrcasestr(const char *text, const char *pattern);
-const char *bmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen);
+char *bmstrcasestr(char *text, const char *pattern);
+char *bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen);
+const char *cbmstrcasestr(const char *text, const char *pattern);
+const char *cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen);
 void CtdlMakeTempFileName(char *name, int len);
 char *rfc2047encode(char *line, long length);
 int is_msg_in_mset(const char *mset, long msgnum);
index da37f7f080673977a96a77d0e6a5a5769e7d5ce5..73f8e778f75418b4b94379c120d8192eed18e091 100644 (file)
@@ -629,7 +629,65 @@ int is_msg_in_sequence_set(const char *mset, long msgnum) {
  * \param maxlen Size of string buffer
  * \return Pointer to the source memory right after we stopped reading.
  */
-const char *memreadline(const char *start, char *buf, int maxlen)
+char *memreadline(char *start, char *buf, int maxlen)
+{
+       char ch;
+       char *ptr;
+       int len = 0;            /**< tally our own length to avoid strlen() delays */
+
+       ptr = start;
+
+       while (1) {
+               ch = *ptr++;
+               if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
+                       buf[len++] = ch;
+               }
+               if ((ch == 10) || (ch == 0)) {
+                       buf[len] = 0;
+                       return ptr;
+               }
+       }
+}
+
+
+/** 
+ * \brief Utility function to "readline" from memory
+ * \param start Location in memory from which we are reading.
+ * \param buf the buffer to place the string in.
+ * \param maxlen Size of string buffer
+ * \param retlen the length of the returned string
+ * \return Pointer to the source memory right after we stopped reading.
+ */
+char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen)
+{
+       char ch;
+       char *ptr;
+       int len = 0;            /**< tally our own length to avoid strlen() delays */
+
+       ptr = start;
+
+       while (1) {
+               ch = *ptr++;
+               if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
+                       buf[len++] = ch;
+               }
+               if ((ch == 10) || (ch == 0)) {
+                       buf[len] = 0;
+                       *retlen = len;
+                       return ptr;
+               }
+       }
+}
+
+
+/** 
+ * \brief Utility function to "readline" from memory
+ * \param start Location in memory from which we are reading.
+ * \param buf the buffer to place the string in.
+ * \param maxlen Size of string buffer
+ * \return Pointer to the source memory right after we stopped reading.
+ */
+const char *cmemreadline(const char *start, char *buf, int maxlen)
 {
        char ch;
        const char *ptr;
@@ -658,7 +716,7 @@ const char *memreadline(const char *start, char *buf, int maxlen)
  * \param retlen the length of the returned string
  * \return Pointer to the source memory right after we stopped reading.
  */
-const char *memreadlinelen(const char *start, char *buf, int maxlen, int *retlen)
+const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retlen)
 {
        char ch;
        const char *ptr;
@@ -805,7 +863,7 @@ void generate_uuid(char *buf) {
  * The code is roughly based on the strstr() replacement from 'tin' written
  * by Urs Jannsen.
  */
-inline static const char *_bmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
+inline static char *_bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
 
        register unsigned char *p, *t;
        register int i, j, *delta;
@@ -863,7 +921,7 @@ inline static const char *_bmstrcasestr_len(const char *text, size_t textlen, co
  * The code is roughly based on the strstr() replacement from 'tin' written
  * by Urs Jannsen.
  */
-const char *bmstrcasestr(const char *text, const char *pattern) {
+char *bmstrcasestr(char *text, const char *pattern) {
        size_t textlen;
        size_t patlen;
 
@@ -876,10 +934,95 @@ const char *bmstrcasestr(const char *text, const char *pattern) {
        return _bmstrcasestr_len(text, textlen, pattern, patlen);
 }
 
-const char *bmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
+char *bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
        return _bmstrcasestr_len(text, textlen, pattern, patlen);
 }
 
+
+
+
+/*
+ * bmstrcasestr() -- case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ */
+inline static const char *_cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
+
+       register unsigned char *p, *t;
+       register int i, j, *delta;
+       register size_t p1;
+       int deltaspace[256];
+
+       if (!text) return(NULL);
+       if (!pattern) return(NULL);
+
+       /* algorithm fails if pattern is empty */
+       if ((p1 = patlen) == 0)
+               return (text);
+
+       /* code below fails (whenever i is unsigned) if pattern too long */
+       if (p1 > textlen)
+               return (NULL);
+
+       /* set up deltas */
+       delta = deltaspace;
+       for (i = 0; i <= 255; i++)
+               delta[i] = p1;
+       for (p = (unsigned char *) pattern, i = p1; --i > 0;)
+               delta[tolower(*p++)] = i;
+
+       /*
+        * From now on, we want patlen - 1.
+        * In the loop below, p points to the end of the pattern,
+        * t points to the end of the text to be tested against the
+        * pattern, and i counts the amount of text remaining, not
+        * including the part to be tested.
+        */
+       p1--;
+       p = (unsigned char *) pattern + p1;
+       t = (unsigned char *) text + p1;
+       i = textlen - patlen;
+       while(1) {
+               if (tolower(p[0]) == tolower(t[0])) {
+                       if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
+                               return ((char *)t - p1);
+                       }
+               }
+               j = delta[tolower(t[0])];
+               if (i < j)
+                       break;
+               i -= j;
+               t += j;
+       }
+       return (NULL);
+}
+
+/*
+ * bmstrcasestr() -- case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ */
+const char *cbmstrcasestr(const char *text, const char *pattern) {
+       size_t textlen;
+       size_t patlen;
+
+       if (!text) return(NULL);
+       if (!pattern) return(NULL);
+
+       textlen = strlen (text);
+       patlen = strlen (pattern);
+
+       return _cbmstrcasestr_len(text, textlen, pattern, patlen);
+}
+
+const char *cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
+       return _cbmstrcasestr_len(text, textlen, pattern, patlen);
+}
+
 /*
  * Local replacement for controversial C library function that generates
  * names for temporary files.  Included to shut up compiler warnings.