📄 parsemime.c
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: ParseMime.c,v 1.5 2002/08/06 20:10:28 dallen Exp $
____________________________________________________________________________*/
// System Headers
#include <windows.h>
// Shared Headers
#include "ParseMime.h"
static BOOL GetNextMimePart(MimePart *pMimeList, char **szLinePtr);
static char * GetNextLine(char *szStart,
char *szOutput,
size_t nOutputSize);
static void MatchContentType(char *szLine, MimeContentType *pnContentType);
PGPError ParseMime(char *szInput, MimePart **ppMimeList)
{
char szLine[256];
char *szLinePtr;
MimePart *pMimeList;
MimePart *pHead;
if ((szInput == NULL) || (ppMimeList == NULL))
return kPGPError_BadParams;
*ppMimeList = NULL;
szLinePtr = szInput;
szLinePtr = GetNextLine(szLinePtr, szLine, sizeof(szLine));
pMimeList = (MimePart *) calloc(sizeof(MimePart), 1);
pHead = pMimeList;
pMimeList->szHeader = szInput;
if (szLinePtr != NULL)
{
if (strstr(szLine, "Mime-Version:"))
GetNextMimePart(pMimeList, &szLinePtr);
else
GetNextMimePart(pMimeList, &szInput);
}
if (pMimeList->szBody == NULL)
{
if (pMimeList->previousPart != NULL)
pMimeList->previousPart->nextPart = NULL;
free(pMimeList);
}
else if (pMimeList->szFooter == NULL)
pMimeList->nBodyLength = strlen(pMimeList->szBody);
*ppMimeList = pHead;
return kPGPError_NoErr;
}
static BOOL GetNextMimePart(MimePart *pMimeList, char **szParentLinePtr)
{
char szLine[256];
char *szLinePtr;
char *szLastLinePtr;
char *szBoundary;
BOOL bFoundBoundary = FALSE;
BOOL bNextPart = FALSE;
szLinePtr = *szParentLinePtr;
pMimeList->szHeader = szLinePtr;
szLastLinePtr = szLinePtr;
szLinePtr = GetNextLine(szLinePtr, szLine, sizeof(szLine));
if (strstr(szLine, "Content-Type:"))
MatchContentType(szLine, &(pMimeList->nContentType));
else
pMimeList->nContentType = ContentType_TextPlain;
if ((pMimeList->nContentType == ContentType_MultipartMixed) ||
(pMimeList->nContentType == ContentType_MultipartAlternative))
{
while (szLinePtr != NULL)
{
if ((pMimeList->szBody == NULL) && (strlen(szLine) < 3))
{
pMimeList->nHeaderLength = szLinePtr - pMimeList->szHeader;
pMimeList->szBody = szLinePtr;
}
if (!bFoundBoundary)
{
if ((szBoundary = strstr(szLine, "boundary=")) != NULL)
{
bFoundBoundary = TRUE;
szBoundary += strlen("boundary=");
if (*szBoundary == '\"')
szBoundary++;
pMimeList->nBoundaryLength = strcspn(szBoundary, "\";");
pMimeList->szBoundary = (char *) calloc(1,
pMimeList->nBoundaryLength + 1);
strncpy(pMimeList->szBoundary, szBoundary,
pMimeList->nBoundaryLength);
}
}
else if (!strncmp(&(szLine[2]), pMimeList->szBoundary,
pMimeList->nBoundaryLength))
break;
szLastLinePtr = szLinePtr;
szLinePtr = GetNextLine(szLinePtr, szLine, sizeof(szLine));
}
if (bFoundBoundary)
{
MimePart *pSubPart;
MimePart headSub;
headSub.subPart = (MimePart *) calloc(sizeof(MimePart), 1);
headSub.subPart->parentPart = pMimeList;
pSubPart = headSub.subPart;
do
{
pSubPart->nextPart = (MimePart *) calloc(sizeof(MimePart), 1);
pSubPart->nextPart->parentPart = pSubPart->parentPart;
pSubPart->nextPart->previousPart = pSubPart;
pSubPart = pSubPart->nextPart;
}
while (GetNextMimePart(pSubPart, &szLinePtr));
pMimeList->subPart = headSub.subPart->nextPart;
pMimeList->subPart->previousPart = NULL;
free(headSub.subPart);
szLastLinePtr = szLinePtr;
szLinePtr = GetNextLine(szLinePtr, szLine, sizeof(szLine));
}
}
while (szLinePtr != NULL)
{
if (pMimeList->parentPart != NULL)
{
if (strlen(szLine) >= pMimeList->parentPart->nBoundaryLength)
{
if (!strncmp(&(szLine[2]), pMimeList->parentPart->szBoundary,
pMimeList->parentPart->nBoundaryLength))
{
pMimeList->nBodyLength = szLastLinePtr - pMimeList->szBody;
pMimeList->szFooter = szLastLinePtr;
pMimeList->nFooterLength = szLinePtr - szLastLinePtr;
bNextPart = TRUE;
if (strlen(szLine) >=
(pMimeList->parentPart->nBoundaryLength + 4))
{
if (!strncmp(&(szLine[pMimeList->parentPart->nBoundaryLength+2]),
"--", 2))
bNextPart = FALSE;
}
break;
}
}
}
if (pMimeList->szBody == NULL)
{
if (strlen(szLine) < 3)
{
pMimeList->nHeaderLength = szLinePtr - pMimeList->szHeader;
pMimeList->szBody = szLinePtr;
}
else if (strstr(szLine, "Content-Disposition: "))
{
if (strstr(szLine, "attachment"))
pMimeList->bAttachment = TRUE;
}
}
szLastLinePtr = szLinePtr;
szLinePtr = GetNextLine(szLinePtr, szLine, sizeof(szLine));
}
*szParentLinePtr = szLinePtr;
return bNextPart;
}
static char * GetNextLine(char *szStart,
char *szOutput,
size_t nOutputSize)
{
char *szEnd = NULL;
BOOL bStop = FALSE;
if (szStart == NULL)
return NULL;
szEnd = strchr(szStart, '\n');
if (szEnd == NULL)
{
szEnd = strchr(szStart, '\0');
bStop = TRUE;
}
if ((szOutput != NULL) && (nOutputSize > 0))
{
size_t nLineSize;
size_t nBuffSize;
nLineSize = szEnd - szStart;
if (nLineSize == 0)
szOutput[0] = 0;
else
{
if (nLineSize > nOutputSize)
nBuffSize = nOutputSize;
else
nBuffSize = nLineSize;
strncpy(szOutput, szStart, nBuffSize);
szOutput[nBuffSize-1] = 0;
}
}
if (bStop)
return NULL;
else
return szEnd+1;
}
static void MatchContentType(char *szLine, MimeContentType *pnContentType)
{
if (strstr(szLine, "text/plain"))
*pnContentType = ContentType_TextPlain;
else if (strstr(szLine, "text/html"))
*pnContentType = ContentType_TextHTML;
else if (strstr(szLine, "multipart/mixed"))
*pnContentType = ContentType_MultipartMixed;
else if (strstr(szLine, "multipart/alternative"))
*pnContentType = ContentType_MultipartAlternative;
else
*pnContentType = ContentType_Other;
return;
}
void FreeMimeList(MimePart *pMimeList)
{
MimePart *pPreviousList;
while (pMimeList != NULL)
{
if (pMimeList->subPart != NULL)
FreeMimeList(pMimeList->subPart);
if (pMimeList->szBoundary != NULL)
free(pMimeList->szBoundary);
pPreviousList = pMimeList;
pMimeList = pMimeList->nextPart;
free(pPreviousList);
}
return;
}
/*__Editor_settings____
Local Variables:
tab-width: 4
End:
vi: ts=4 sw=4
vim: si
_____________________*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -