* tiny tool for message retrieval, first draft.
[citadel.git] / citadel / tools.c
index dbb8c046d5813eed324b3ea3d9590d7587d7f37c..b864642591c6cb205475f3e673129545ac81920b 100644 (file)
@@ -128,6 +128,8 @@ long extract_token(char *dest, const char *source, int parmnum, char separator,
        if (s == NULL) {
                return(-1);
        }
+       
+       maxlen--;
 
        while (*s) {
                if (*s == separator) {
@@ -414,6 +416,43 @@ int CtdlDecodeBase64(char *dest, const char *source, size_t length)
     }
 }
 
+/*
+ * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
+ * according to RFC2045 section 6.7
+ */
+int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
+       unsigned int ch;
+       int decoded_length = 0;
+       int pos = 0;
+
+       while (pos < sourcelen)
+       {
+               if (!strncmp(&encoded[pos], "=\r\n", 3))
+               {
+                       pos += 3;
+               }
+               else if (!strncmp(&encoded[pos], "=\n", 2))
+               {
+                       pos += 2;
+               }
+               else if (encoded[pos] == '=')
+               {
+                       ch = 0;
+                       sscanf(&encoded[pos+1], "%02x", &ch);
+                       pos += 3;
+                       decoded[decoded_length++] = ch;
+               }
+               else
+               {
+                       decoded[decoded_length++] = encoded[pos];
+                       pos += 1;
+               }
+       }
+       decoded[decoded_length] = 0;
+       return(decoded_length);
+}
+
+
 /*
  * if we send out non ascii subjects, we encode it this way.
  */
@@ -451,6 +490,8 @@ void striplt(char *buf)
 {
        size_t len;
        int a;
+
+       if (buf==NULL) return;
        if (IsEmptyStr(buf)) return;
        len = strlen(buf);
         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1])))
@@ -592,6 +633,36 @@ char *memreadline(char *start, char *buf, int maxlen)
 }
 
 
+/** 
+ * \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;
+               }
+       }
+}
+
+
 
 
 /*