mime.c

来自「C/C++语言的CGI接口库」· C语言 代码 · 共 676 行 · 第 1/2 页

C
676
字号
	else if (!strncasecmp(property, "charset", 7))	    *charset = value;	else if (!strncasecmp(property, "name", 4))	    *name = value;	else if (!strncasecmp(property, "boundary", 8))	    *boundary = value;	else if (!strncasecmp(property, "format", 6))	    *format = value;	else	    XFREE(value);	XFREE(property);    }    freeStringArray(tokens);}void mimeParseContentTransferEncoding(char *text, char **encoding){    char **tokens;    char  *property, *value;    int    tlen;    *encoding = NULL;    text? (tlen = strlen(text)) : (tlen = 0);    tokenizeString(text, tlen, &tokens);    parseToken(tokens[1], &property, &value);    (*encoding) = value;}void mimeParseContentDisposition(char *text, char **disp, char **filename, char **name){    int   tlen;    int   i;    char  *property, *value, *p;    char **tokens;    *disp = *filename = *name = NULL;    text? (tlen = strlen(text)) : (tlen = 0);    tokenizeString(text, tlen, &tokens);    parseToken(tokens[1], &property, &value);    (*disp) = value;    for (i = 2; tokens[i] != '\0'; i++) {	parseToken(tokens[i], &property, &value);	if (property == NULL)	    XFREE(value);		if (!strncasecmp(property, "filename", 8))	    *filename = value;	else if (!strncasecmp(property, "name", 4))	    *name = value;	else	    XFREE(value);	XFREE(property);    }    /*        Some browsers, like MS Internet Explorer send the entire path       for the file.  The standard states that the only the filename should       be included, so we check for the full path and remove it if present.    */    if (*filename && strlen(*filename) > 0) {	/* Forward to the end of the string. */	for (p = *filename; *p != '\0'; p++);	/* Iterate backwards until a backslash or forward slash or the beginning of the string is found. */	for (; *p != '\\' && *p != '/' && p > *filename; p--);	if (*p == '\\' || *p == '/') {	    strcpy(*filename, p+1);	}    }    freeStringArray(tokens);}void mimeParseContentDescription(char *text, char **desc){    char *p, *q, *offset;    int   tlen;    *desc = NULL;    text? (tlen = strlen(text)) : (tlen = 0);    offset = text + tlen;    for (q = text; *q != '\r' && q < offset; p++);        for (p = text; isspace((int)*p); p++);  /* Remove whitespace from beggining of string. */    *desc = XMALLOC(char, q - p + 1);    strncpy(*desc, q, p-q);    (*desc)[p-q] = 0;}size_t mimeParseMimeBody(CRLFStringBuf *mimeIn, char *sboundary, char *eboundary, 		  MimePart *part, size_t mimePartLimit, size_t mimeLimit){    char  *line, *peek;    int    i, endOfPart;    int    llen, size;    /* This message is MIME encoded, look for s/eboundary as end of Message. */    endOfPart = 0;        part->bodylen  = 0;    part->bodysize = 256;    part->body     = XMALLOC(char, part->bodysize);    memset(part->body, 0, part->bodysize);    do {        crlfsbuf_readdata(mimeIn, &line, &size, &llen);        /* Check if we have reached the end of the MIME body. */	peek = crlfsbuf_peek(mimeIn);        if ((strncmp(peek, eboundary, strlen(eboundary)) == 0) || 	    (strncmp(peek, sboundary, strlen(sboundary)) == 0))            endOfPart++;		/* If reading the previous line pushes us over the limit,  *	 * only copy enough into the MIME part to equal the limit. */	if (mimePartLimit > 0 && (part->bodylen + llen) >= mimePartLimit) {	    llen -= (part->bodylen + llen) - mimePartLimit;	}	if (!part->truncated) {	    if (part->bodylen + llen <= part->bodysize - 1) {		memcpy(part->body + part->bodylen, line, llen);		part->bodylen += llen;	    }	    else {		part->bodysize = (part->bodylen + llen) * 2;		part->body     = XREALLOC(char, part->body, part->bodysize);		memcpy(part->body + part->bodylen, line, llen);		part->bodylen += llen;	    }	}	/* If the input was too long and truncated, read *	 * and discard the rest of the MIME part.        */	if (mimePartLimit > 0 && part->bodylen >= mimePartLimit)	    part->truncated++;	/* If the entire MIME input was greater than the limit, exit. */	if (mimeLimit > 0 && crlfsbuf_truncated(mimeIn)) {	    part->truncated++;	    break;	}    } while (!endOfPart && line != NULL);        /* If binary encoded, remove the final CRLF that seperates the MIME boundary. */    if (part->encoding == binary || part->encoding == unknown) {	if (part->body[part->bodylen - 1] == '\n' && part->body[part->bodylen - 2] == '\r') {	    part->body[part->bodylen - 1] = '\0';	    part->body[part->bodylen - 2] = '\0';	    part->bodylen -= 2;	}    }    /* Else, if not text, but encoded, remove CRLF endings. */    else if (part->encoding == basesixtyfour && part->encoding == quotedprintable) {	i = removePat(part->body, "\r\n", part->bodylen);	part->bodylen -= i*2;    }    return part->bodylen;}MimeHeadermimeHeaderName(char *text){    MimeHeader htype;    htype = unknownHeader;    switch (text[0]) {    case 'c':    case 'C':	if (strncasecmp("Content-Type: ", text, 14) == 0)	    htype = contentType;	else if (strncasecmp("Content-Transfer-Encoding: ", text, 27) == 0)	    htype = contentEncoding;	else if (strncasecmp("Content-Disposition: ", text, 21) == 0)	    htype = contentDisposition;	else if (strncasecmp("Content-Description: ", text, 21) == 0)	    htype = contentDescription;	break;    case 'm':    case 'M':	if (strncasecmp("Mime-Version: ", text, 14) == 0)	    htype = mimeVersion;	break;    default:	htype = unknownHeader;    }    return htype;}/* Returns the next header from the incomming stream. */int mimeReturnHeader(CRLFStringBuf *mimeIn, char **text, int *size){    int   lsize;    int   llen, tlen;    char  *line, *peek;    int   endOfHeaders;        llen  = 0;    lsize = 0;        tlen  = 0;    *size = 256;    *text = XMALLOC(char, *size);    memset(*text, 0, *size);    endOfHeaders = 0;    do {        line = crlfsbuf_read(mimeIn);        /* Check if we have reached the end of the headers. */	peek = crlfsbuf_peek(mimeIn);        if (peek[0] == '\r' && peek[1] == '\n' && peek[2] == '\0')            endOfHeaders++;		llen = strlen(line);	if (tlen + llen <= *size - 1) {	    strcat(*text, line);	    tlen += llen;	}	else {	    *size  = (*size+lsize) * 2;	    tlen  += llen;	    *text  = XREALLOC(char, *text, *size);	    strcat(*text, line);	}    } while (!endOfHeaders && isspace((int)peek[0]) && line != NULL);    replacePat(text, "\r\n", " ", size);    if (endOfHeaders || line == NULL)        return -1;    else         return 0;}int mimeReturnHeaderByName(FILE *mimeIn, char *header, char **text, int *size){    off_t pos;    int   hsize, lsize;    int   llen, tlen;    char  *line;    int   endOfHeaders, foundHeader;        pos   = ftell(mimeIn);    header? (hsize = strlen(header)) : (hsize = 0);    llen  = 0;    lsize = 256;    line  = XMALLOC(char, lsize);    memset(line, 0, lsize);    tlen  = 0;    *size = 256;    *text = XMALLOC(char, *size);    memset(*text, 0, *size);    endOfHeaders = 0;    foundHeader  = 0;    do {        readCRLFLine(mimeIn, &line, &lsize);        /* Check if we have reached the end of the headers. */        if (line[0] == '\r' && line[1] == '\n' && line[2] == '\0')            endOfHeaders++;        /* Found the header. */        if (strncasecmp(line, header, hsize) == 0 || foundHeader) {            if (foundHeader && !isspace((int)line[0]))                break;            foundHeader++;            llen = strlen(line);            if (tlen + llen <= *size - 1) {                strcat(*text, line);                tlen += llen;            }            else {                *size  = (*size+lsize) * 2;                tlen  += llen;                *text  = XREALLOC(char, *text, *size);                strcat(*text, line);            }        }    } while (!endOfHeaders && !feof(mimeIn));    replacePat(text, "\r\n", " ", size);    replacePat(text, header, "", size);    /* Put the file pointer back where we got it. */    fseek(mimeIn, pos - ftell(mimeIn), SEEK_CUR);    XFREE(line);    if (endOfHeaders && !foundHeader)         return -1;    else         return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?