📄 ospmime.c
字号:
/**########################################################################*########################################################################*########################################################################* * COPYRIGHT (c) 1998, 1999 by TransNexus, LLC * * This software contains proprietary and confidential information * of TransNexus, LLC. Except as may be set forth in the license * agreement under which this software is supplied, use, disclosure, * or reproduction is prohibited without the prior, express, written* consent of TransNexus, LLC. * *******#########################################################################*#########################################################################*#########################################################################*//* * ospmime.cpp - mime stuff */#include "ospmime.h"voidOSPPMimeBodyFree( OSPTMIMEBODY *ospvMimeBody){ int count = 0; OSPM_DBGENTER(("ENTER: OSPPMimeBodyFree()\n")); if(ospvMimeBody != (OSPTMIMEBODY *)NULL) { if(ospvMimeBody->Boundary != (OSPTMIMEPART *)NULL) { OSPPMimePartFree(ospvMimeBody->Boundary); } while(count < OSPC_MAX_BODYPARTS) { if(ospvMimeBody->BodyParts[count] != (OSPTMIMEPART *)NULL) { OSPPMimePartFree(ospvMimeBody->BodyParts[count]); } count++; } } OSPM_DBGEXIT(("EXIT : OSPPMimeBodyFree()\n")); return;}intOSPPMimeBodyPartsParse( OSPTMIMEPART *ospvBodyData, OSPTMIMEBODY *ospvMimeBody, OSPTMIMEPART *ospvBoundary){ int errorcode = OSPC_ERR_NO_ERROR; unsigned char *buf = ospvBodyData->Content; unsigned currpos = 0; unsigned bufend = ospvBodyData->Length; unsigned start = 0; int found = 0, bfound = 0; int bresult = -1; OSPTMIMEPART header; int partsfound=0; OSPM_DBGENTER(("ENTER: OSPPMimeBodyPartsParse() bufend == [%d] buf = [%*s]\n", bufend, bufend, (char *)buf)); OSPM_MEMSET(&header, 0, sizeof(OSPTMIMEPART)); /* strip off body parts, send to parser */ while((partsfound < OSPC_MAX_BODYPARTS) && (currpos < bufend)) { /* if there is no boundary set, we have text/plain */ if((ospvBoundary != OSPC_OSNULL) && (ospvBoundary->Content != OSPC_OSNULL)) { while((currpos < bufend) && (!found)) { if(buf[currpos] == ospvBoundary->Content[0]) { /* make sure we really are at the boundary */ bresult = OSPM_MEMCMP(&buf[currpos], ospvBoundary->Content, ospvBoundary->Length); if(bresult == 0) { found = 1; break; } } currpos++; } /* end while(currpos < bufend... */ if(found) { /* get past the boundary, CRLF */ currpos += ospvBoundary->Length; while(currpos < bufend) { if((buf[currpos] != '\n') && (buf[currpos] != ' ') && (buf[currpos] != '\t') && (buf[currpos] != '\r')) { bfound = 1; break; } currpos++; } if(bfound) { int cresult = -1; start = currpos; /* get to the next boundary (CRLF--boundary) */ while(currpos < bufend) { if((buf[currpos] == '\r') && (currpos+3 < bufend) && (buf[currpos+1] == '\n') && (buf[currpos + 2] == '-') && (buf[currpos + 3] == '-')) { /* make sure we really are at the boundary */ cresult = OSPM_MEMCMP(&buf[currpos+4], ospvBoundary->Content, ospvBoundary->Length); if(cresult == 0) { /* move past the CRLF */ currpos+=2; break; } } currpos++; } } } } else { /* get to the next CRLFCRLF (CRLFCRLF) */ while(currpos < bufend) { if((buf[currpos] == '\r') && (currpos+3 < bufend) && (buf[currpos+1] == '\n') && (buf[currpos + 2] == '\r') && (buf[currpos + 3] == '\n')) { /*now we are in the right spot */ break; } currpos++; } start = currpos; /* set currpos to bufend, so we can parse the whole buffer. this is text/plain */ currpos = bufend; } if(currpos <= bufend) { OSPM_MALLOC(ospvMimeBody->BodyParts[partsfound], OSPTMIMEPART, sizeof(OSPTMIMEPART)); if(ospvMimeBody->BodyParts[partsfound] != (OSPTMIMEPART *)NULL) { OSPM_MEMSET(ospvMimeBody->BodyParts[partsfound], 0, sizeof(ospvMimeBody->BodyParts[partsfound])); errorcode = OSPPMimeBufferParse( &buf[start], currpos - start, &header, ospvMimeBody->BodyParts[partsfound]); if (header.Content) { OSPM_FREE(header.Content); header.Content = OSPC_OSNULL; } if(errorcode == OSPC_ERR_NO_ERROR) { partsfound++; found = 0; bfound = 0; } else { break; } } else { errorcode = OSPC_ERR_MIME_MALLOC_FAILED; OSPM_DBGERRORLOG(errorcode, "malloc failed for body part"); break; } } } if(errorcode == OSPC_ERR_NO_ERROR) { ospvMimeBody->NumBodyParts = partsfound; } OSPM_DBGEXIT(("EXIT : OSPPMimeBodyPartsParse()\n")); return errorcode;}/* Pulls apart header from body */int OSPPMimeBufferParse( unsigned char *ospvInputbuffer, /* In - buffer to be parsed */ unsigned ospvSizeOfInputbuffer, /* In - size of buffer to be parsed */ OSPTMIMEPART *ospvHeader, /* Out - Mime header data */ OSPTMIMEPART *ospvBody) /* Out - Mime body data */{ unsigned char* buffin = ospvInputbuffer; unsigned buffinend = ospvSizeOfInputbuffer; unsigned currpos = 0; unsigned hdrstart = 0; unsigned hdrlen = 0; unsigned bdystart = 0; unsigned bdylen = 0; int errorcode = OSPC_ERR_NO_ERROR; OSPM_DBGENTER(("ENTER: OSPPMimeBufferParse()\n")); /* If first character is a LF (ANSI C or UNIX) * or if first two characters are CR LF (MIME or DOS), * there are no headers. */ if ((currpos < buffinend) && (buffin[currpos] != '\n') && ! ((buffin[currpos] == '\r') && (currpos+1 < buffinend) && (buffin[currpos+1] == '\n'))) { while (currpos < buffinend) { /* Is it LF LF ? */ if ((buffin[currpos] == '\n') && (currpos+1 < buffinend) && (buffin[currpos+1] == '\n')) { ++hdrlen; ++currpos; break; } /* Is it CR LF CR LF? */ else if ((buffin[currpos] == '\r') && (currpos+3 < buffinend) && (buffin[currpos+1] == '\n') && (buffin[currpos+2] == '\r') && (buffin[currpos+3] == '\n')) { hdrlen += 2; currpos += 2; break; } ++currpos; ++hdrlen; } } errorcode = OSPPUtilMallocAndCopySubString(ospvInputbuffer, &(ospvHeader->Content), hdrstart, hdrlen); ospvHeader->Length = hdrlen; if(errorcode == OSPC_ERR_NO_ERROR) { /* Skip blank line * LF (ANSI C or UNIX) */ if (currpos < buffinend && buffin[currpos] == '\n') { ++currpos; } /* CR LF (MIME or DOS) */ else if (currpos < buffinend && buffin[currpos] == '\r' && currpos+1 < buffinend && buffin[currpos+1] == '\n') { currpos += 2; } bdystart = currpos; bdylen = ospvSizeOfInputbuffer; /* get rid of whitspace, CRLFs at end of body string */ while (bdylen > bdystart) { if ((!isspace(buffin[bdylen-1])) && (buffin[bdylen-1] != '\n') && (buffin[bdylen-1] != '\r')) { break; } --bdylen; } bdylen = bdylen - bdystart; errorcode = OSPPUtilMallocAndCopySubString(ospvInputbuffer, &(ospvBody->Content), bdystart, bdylen); ospvBody->Length = bdylen; } OSPM_DBGEXIT(("EXIT : OSPPMimeBufferParse()\n")); return errorcode;}voidOSPPMimeDataFree( OSPTMIMEMSG *ospvMessage){ OSPM_DBGENTER(("ENTER: OSPPMimeDataFree()\n")); OSPPMimePartFree(ospvMessage->MsgPart); OSPPMimePartFree(ospvMessage->SigPart); OSPM_DBGEXIT(("EXIT : OSPPMimeDataFree()\n")); return;}voidOSPPMimeFieldFree( OSPTMIMEFIELD *ospvField){ int count = 0; OSPM_DBGENTER(("ENTER: OSPPMimeFieldFree()\n")); if(ospvField != (OSPTMIMEFIELD *)NULL) { if(ospvField->Params != (OSPTMIMEPARAM **)NULL) { for(count = 0; count < OSPC_MAX_PARAMS;count++) { if(ospvField->Params[count] != (OSPTMIMEPARAM *)NULL) { OSPPMimeParamFree(ospvField->Params[count]); OSPM_FREE(ospvField->Params[count]); ospvField->Params[count] = NULL; } } } } OSPM_DBGEXIT(("EXIT : OSPPMimeFieldFree()\n")); return;}intOSPPMimeMessageBuild( OSPTMIMEMSG *ospvMessage, unsigned char **ospvMimeOut, unsigned *ospvSizeOfMimeOut){ int errorcode = OSPC_ERR_NO_ERROR; unsigned char *next = NULL; char msgtmp[20]; char sigtmp[20]; int boundln = 0, mshdrln = 0, msszln = 0, sighdrln = 0, sigszln = 0, count = 0, numdelims = 0, numcrlfs = 0; if(ospvMessage->SigPart->Length > 0) { msszln = sprintf(msgtmp, "%d", ospvMessage->MsgPart->Length); mshdrln = strlen(OSPC_MIME_MSG_HEADER); boundln = strlen(OSPC_MIME_BOUNDARY); sigszln = sprintf(sigtmp, "%d", ospvMessage->SigPart->Length), sighdrln = strlen(OSPC_MIME_SIG_HEADER); numdelims = 4; numcrlfs = 12; } OSPM_DBGENTER(("ENTER: OSPPMimeMessageBuild()\n")); count = (boundln*3) + mshdrln + msszln + ospvMessage->MsgPart->Length + sighdrln + sigszln + ospvMessage->SigPart->Length + 2*numdelims + 2*numcrlfs; /* CRLFs, boundary delimiters */ ospvMessage->Length = count; *ospvSizeOfMimeOut = ospvMessage->Length; OSPM_MALLOC(*ospvMimeOut, unsigned char, *ospvSizeOfMimeOut); if(*ospvMimeOut != (unsigned char *)NULL) { OSPM_MEMSET(*ospvMimeOut, 0, *ospvSizeOfMimeOut); next = *ospvMimeOut; if(ospvMessage->SigPart->Length > 0) { /* CRLF for beginning of boundary */ OSPM_MEMCPY(next, OSPC_MIME_CRLF, 2); next += 2; /* -- for start of boundary */ OSPM_MEMCPY(next, OSPC_MIME_BOUNDARY_DELIM, 2); next += 2; /* boundary */ OSPM_MEMCPY(next, OSPC_MIME_BOUNDARY, boundln); next += boundln; /* CRLF */ OSPM_MEMCPY(next, OSPC_MIME_CRLF, 2); next += 2; /* Message header */ OSPM_MEMCPY(next, OSPC_MIME_MSG_HEADER, mshdrln); next += mshdrln; /* Message length */ OSPM_MEMCPY(next, msgtmp, msszln); next+=msszln; /* CRLF for end of message header */ OSPM_MEMCPY(next, OSPC_MIME_CRLF, 2); next += 2; /* CRLF for beginning of message */ OSPM_MEMCPY(next, OSPC_MIME_CRLF, 2); next += 2; } /* Message content */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -