* tiny tool for message retrieval, first draft.
[citadel.git] / webcit / tools.c
index c0a6b49c7126a4bcc1e36aedfc19c2584cc7ee05..591af0d977a0d73c101d8e9467be93ac9f92549a 100644 (file)
@@ -85,6 +85,8 @@ long extract_token(char *dest, const char *source, int parmnum, char separator,
                return(-1);
        }
 
+       maxlen--;
+       
        while (*s) {
                if (*s == separator) {
                        ++current_token;
@@ -239,6 +241,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;
+               }
+       }
+}
+
+
 
 /**
  * \brief searches for a  paternn within asearch string
@@ -600,6 +632,43 @@ void generate_uuid(char *buf) {
 }
 
 
+/*
+ * 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);
+}
+
+
 /**
  * \brief Local replacement for controversial C library function that generates
  * names for temporary files.  Included to shut up compiler warnings.