#include "citadel.h"
#include "server.h"
-#include "serv_extensions.h"
#include "sysdep_decls.h"
#include "tools.h"
/*
* Convert "quoted-printable" to binary. Returns number of bytes decoded.
+ * according to RFC2045 section 6.7
*/
int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
- char buf[SIZ];
- int buf_length = 0;
- int soft_line_break = 0;
unsigned int ch;
int decoded_length = 0;
- int i;
-
- decoded[0] = 0;
- decoded_length = 0;
- memset(buf, '\0', SIZ);
- buf_length = 0;
-
- for (i = 0; i < sourcelen; ++i) {
- size_t buflen = strlen(buf);
- buf[buf_length++] = encoded[i];
-
- if ( (encoded[i] == '\n')
- || (encoded[i] == 0)
- || (i == (sourcelen-1)) ) {
- buf[buf_length++] = 0;
-
- /*** begin -- process one line ***/
+ int pos = 0;
- if (buf[buflen-1] == '\n') {
- buf[buflen-1] = 0;
- buflen --;
- }
- if (buf[buflen-1] == '\r') {
- buf[buflen-1] = 0;
- buflen --;
- }
- while (isspace(buf[buflen-1])) {
- buf[buflen-1] = 0;
- buflen --;
- }
- soft_line_break = 0;
-
- while (buflen > 0) {
- if ((buf[0] == '=') && (buf[1] == '\0')) {
- soft_line_break = 1;
- buf[0] = '\0';
- buflen = 0;
- } else if ((buflen>=3) && (buf[0]=='=')) {
- sscanf(&buf[1], "%02x", &ch);
- decoded[decoded_length++] = ch;
- strcpy(buf, &buf[3]);
- buflen -=3;
- } else {
- decoded[decoded_length++] = buf[0];
- strcpy(buf, &buf[1]);
- buflen --;
- }
- }
- if (soft_line_break == 0) {
- decoded[decoded_length++] = '\r';
- decoded[decoded_length++] = '\n';
- }
- buf_length = 0;
- /*** end -- process one line ***/
+ 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;
+ decoded[decoded_length] = 0;
return(decoded_length);
}
+
/*
* Given a message or message-part body and a length, handle any necessary
* decoding and pass the request up the stack.
int part_seq = 0;
int i;
size_t length;
- char nested_partnum[SIZ];
+ char nested_partnum[256];
+ int crlf_in_use = 0;
+ char *evaluate_crlf_ptr = NULL;
ptr = content_start;
content_length = 0;
}
if (!isspace(buf[0])) {
- if (!strncasecmp(header, "Content-type: ", 14)) {
- strcpy(content_type, &header[14]);
+ if (!strncasecmp(header, "Content-type:", 13)) {
+ strcpy(content_type, &header[13]);
+ striplt(content_type);
extract_key(content_type_name, content_type, "name");
extract_key(charset, content_type, "charset");
/* Deal with weird headers */
if (strchr(content_type, ';'))
*(strchr(content_type, ';')) = '\0';
}
- if (!strncasecmp(header, "Content-Disposition: ", 21)) {
- strcpy(disposition, &header[21]);
+ if (!strncasecmp(header, "Content-Disposition:", 20)) {
+ strcpy(disposition, &header[20]);
+ striplt(disposition);
extract_key(content_disposition_name, disposition, "name");
extract_key(filename, disposition, "filename");
}
- if (!strncasecmp(header, "Content-length: ", 16)) {
- content_length = (size_t) atol(&header[16]);
+ if (!strncasecmp(header, "Content-length: ", 15)) {
+ char clbuf[10];
+ safestrncpy(clbuf, &header[15], sizeof clbuf);
+ striplt(clbuf);
+ content_length = (size_t) atol(clbuf);
+ }
+ if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
+ strcpy(encoding, &header[26]);
+ striplt(encoding);
}
- if (!strncasecmp(header,
- "Content-transfer-encoding: ", 27))
- strcpy(encoding, &header[27]);
if (strlen(boundary) == 0)
extract_key(boundary, header, "boundary");
strcpy(header, "");
}
- if ((strlen(header) + strlen(buf) + 2) < SIZ)
+ if ((strlen(header) + strlen(buf) + 2) < SIZ) {
strcat(header, buf);
+ }
} while ((strlen(buf) > 0) && (*ptr != 0));
if (strchr(disposition, ';'))
if ( (part_start != NULL) && (next_boundary != NULL) ) {
part_end = next_boundary;
- --part_end;
+ --part_end; /* omit the trailing LF */
+ if (crlf_in_use) {
+ --part_end; /* omit the trailing CR */
+ }
if (strlen(partnum) > 0) {
snprintf(nested_partnum,
else {
/* Set up for the next part. */
part_start = strstr(next_boundary, "\n");
+
+ /* Determine whether newlines are LF or CRLF */
+ evaluate_crlf_ptr = part_start;
+ --evaluate_crlf_ptr;
+ if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
+ crlf_in_use = 1;
+ }
+ else {
+ crlf_in_use = 0;
+ }
+
+ /* Advance past the LF ... now we're in the next part */
++part_start;
ptr = part_start;
}
++length;
}
part_end = content_end;
- /* fix an off-by-one error */
+
+ /******
+ * I thought there was an off-by-one error here, but there isn't.
+ * This probably means that there's an off-by-one error somewhere
+ * else ... or maybe only in certain messages?
--part_end;
--length;
+ ******/
/* Truncate if the header told us to */
if ( (content_length > 0) && (length > content_length) ) {