#include "webserver.h"
#include "mime_parser.h"
-/**
- * \brief get mime key
- * \param target where to put the mime buffer at???
- * \param source where to extract the mimetype from
- * \param key what???
- */
+
void extract_key(char *target, char *source, char *key)
{
int a, b;
}
-/**
- * \brief For non-multipart messages, we need to generate a quickie partnum of "1"
+/*
+ * For non-multipart messages, we need to generate a quickie partnum of "1"
* to return to callback functions. Some callbacks demand it.
- * \param supplied_partnum partnum to convert
- * \return the converted num
*/
char *fixed_partnum(char *supplied_partnum) {
if (supplied_partnum == NULL) return "1";
-/**
- * \brief Convert "quoted-printable" to binary. Returns number of bytes decoded.
- * \param decoded the buffer with the decoded output
- * \param encoded the encoded string to decode
- * \param sourcelen length of the decoded buffer
+/*
+ * Convert "quoted-printable" to binary. Returns number of bytes decoded.
*/
int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
char buf[SIZ];
decoded[0] = 0;
decoded_length = 0;
- buf[0] = 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')
/*** begin -- process one line ***/
- if (buf[strlen(buf)-1] == '\n') {
- buf[strlen(buf)-1] = 0;
+ if (buf[buflen-1] == '\n') {
+ buf[buflen-1] = 0;
+ buflen --;
}
- if (buf[strlen(buf)-1] == '\r') {
- buf[strlen(buf)-1] = 0;
+ if (buf[buflen-1] == '\r') {
+ buf[buflen-1] = 0;
+ buflen --;
}
- while (isspace(buf[strlen(buf)-1])) {
- buf[strlen(buf)-1] = 0;
+ while (isspace(buf[buflen-1])) {
+ buf[buflen-1] = 0;
+ buflen --;
}
soft_line_break = 0;
- while (strlen(buf) > 0) {
- if (!strcmp(buf, "=")) {
+ while (buflen > 0) {
+ if ((buf[0] == '=') && (buf[1] == '\0')) {
soft_line_break = 1;
- strcpy(buf, "");
- } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
+ 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) {
return(decoded_length);
}
-/**
- * \brief fully decode a message
+/*
* Given a message or message-part body and a length, handle any necessary
* decoding and pass the request up the stack.
- * \param partnum todo ?????
- * \param part_start todo
- * \param length todo
- * \param content_type todo
- * \param charset todo
- * \param encoding todo
- * \param disposition todo
- * \param name todo
- * \param filename todo
- * \param CallBack todo
- * \param PreMultiPartCallBack todo
- * \param PostMultiPartCallBack todo
- * \param userdata todo
- * \param dont_decode todo
*/
void mime_decode(char *partnum,
char *part_start, size_t length,
return;
}
+ /* Fail silently if we hit an unknown encoding. */
if ((strcasecmp(encoding, "base64"))
&& (strcasecmp(encoding, "quoted-printable"))) {
return;
}
- /**
+
+ /*
* Allocate a buffer for the decoded data. The output buffer is slightly
* larger than the input buffer; this assumes that the decoded data
* will never be significantly larger than the encoded data. This is a
bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
}
else if (!strcasecmp(encoding, "quoted-printable")) {
- bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
- part_start, length);
+ bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
}
if (bytes_decoded > 0) if (CallBack != NULL) {
free(decoded);
}
-/**
- * \brief Break out the components of a multipart message
+/*
+ * Break out the components of a multipart message
* (This function expects to be fed HEADERS + CONTENT)
* Note: NULL can be supplied as content_end; in this case, the message is
* considered to have ended when the parser encounters a 0x00 byte.
- * \param partnum todo
- * \param content_start todo ?????
- * \param content_end todo
- * \param CallBack todo
- * \param PreMultiPartCallBack
- * \param PostMultiPartCallBack
- * \param userdata todo
- * \param dont_decode todo
*/
void the_mime_parser(char *partnum,
char *content_start, char *content_end,
disposition = malloc(SIZ);
memset(disposition, 0, SIZ);
- /** If the caller didn't supply an endpointer, generate one by measure */
+ /* If the caller didn't supply an endpointer, generate one by measure */
if (content_end == NULL) {
content_end = &content_start[strlen(content_start)];
}
- /** Learn interesting things from the headers */
+ /* Learn interesting things from the headers */
strcpy(header, "");
do {
ptr = memreadline(ptr, buf, SIZ);
strcpy(content_type, &header[14]);
extract_key(content_type_name, content_type, "name");
extract_key(charset, content_type, "charset");
- /** Deal with weird headers */
+ /* Deal with weird headers */
if (strchr(content_type, ' '))
*(strchr(content_type, ' ')) = '\0';
if (strchr(content_type, ';'))
is_multipart = 0;
}
- /** If this is a multipart message, then recursively process it */
+ /* If this is a multipart message, then recursively process it */
part_start = NULL;
if (is_multipart) {
- /** Tell the client about this message's multipartedness */
+ /* Tell the client about this message's multipartedness */
if (PreMultiPartCallBack != NULL) {
PreMultiPartCallBack("", "", partnum, "",
NULL, content_type, charset,
0, encoding, userdata);
}
- /** Figure out where the boundaries are */
+ /* Figure out where the boundaries are */
snprintf(startary, SIZ, "--%s", boundary);
snprintf(endary, SIZ, "--%s--", boundary);
startary_len = strlen(startary);
}
if (next_boundary != NULL) {
- /**
- * If we pass out of scope, don't attempt to
+ /* If we pass out of scope, don't attempt to
* read past the end boundary. */
if (!strcmp(next_boundary, endary)) {
ptr = content_end;
}
else {
- /** Set up for the next part. */
+ /* Set up for the next part. */
part_start = strstr(next_boundary, "\n");
++part_start;
ptr = part_start;
}
}
else {
- /** Invalid end of multipart. Bail out! */
+ /* Invalid end of multipart. Bail out! */
ptr = content_end;
}
} while ( (ptr < content_end) && (next_boundary != NULL) );
goto end_parser;
}
- /** If it's not a multipart message, then do something with it */
+ /* If it's not a multipart message, then do something with it */
if (!is_multipart) {
part_start = ptr;
length = 0;
++length;
}
part_end = content_end;
- /** fix an off-by-one error */
+ /* fix an off-by-one error */
--part_end;
--length;
- /** Truncate if the header told us to */
+ /* Truncate if the header told us to */
if ( (content_length > 0) && (length > content_length) ) {
length = content_length;
}
- /**
- * Sometimes the "name" field is tacked on to Content-type,
+ /* Sometimes the "name" field is tacked on to Content-type,
* and sometimes it's tacked on to Content-disposition. Use
* whichever one we have.
*/
name = content_type_name;
}
- /*
- lprintf(9, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
- partnum, length, content_type, charset, encoding);
- */
+ /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
+ partnum, length, content_type, charset, encoding); */
- /**
- * Ok, we've got a non-multipart part here, so do something with it.
+ /* Ok, we've got a non-multipart part here, so do something with it.
*/
mime_decode(partnum,
part_start, length,
userdata, dont_decode
);
- /**
+ /*
* Now if it's an encapsulated message/rfc822 then we have to recurse into it
*/
if (!strcasecmp(content_type, "message/rfc822")) {
}
-end_parser: /** free the buffers! end the oppression!! */
+end_parser: /* free the buffers! end the oppression!! */
free(boundary);
free(startary);
free(endary);
-/**
- * \brief Entry point for the MIME parser.
+/*
+ * Entry point for the MIME parser.
* (This function expects to be fed HEADERS + CONTENT)
* Note: NULL can be supplied as content_end; in this case, the message is
* considered to have ended when the parser encounters a 0x00 byte.
- * \param content_start todo ?????????
- * \param content_end todo
- * \param CallBack todo
- * \param PreMultiPartCallBack todo
- * \param PostMultiPartCallBack todo
- * \param userdata todo
- * \param dont_decode todo
*/
void mime_parser(char *content_start,
char *content_end,
}
-
/*@}*/