📄 mmsparser.cpp
字号:
p += 2;
else
p ++; //the length of the subject < 0x1f
// skip the code byte
code = (MIME_STRING_CODE) (unsigned char)*p++;
// skip the 0x7f, this may occured when in some unknow cases
if (code == UTF_8)
if (*p == 0x7f)
p++;
}
while (*p !=0 && j < MAX_SUBJECT_LENGTH)
{
tmp_str[j++] = *p++;
}
string_decode(tmp_str, j, sub_final, code);
if (sub_final == NULL)
return -1;
pList->AddTail(create_new_itemnode(head_mark,
get_mms_item_name((unsigned char)head_mark),
sub_final));
free(sub_final);
while (*p !=0)
{
p++;
}
p++; // move to the next header mark
}
break;
case MMS_HEAD_TO:
{
int i;
p++;
i = 0;
while (*p !=0)
{
tmp_str[i++] = *p++;
}
tmp_str[i] = 0;
pList->AddTail(create_new_itemnode(head_mark,
get_mms_item_name((unsigned char)head_mark),
tmp_str));
p++;
}
break;
case MMS_HEAD_TRANS_ID:
{
int j = 0;
p++;
while (*p !=0 && j < MAX_SUBJECT_LENGTH)
{
tmp_str[j++] = *p++;
}
tmp_str[j] = 0;
pList->AddTail(create_new_itemnode(head_mark,
get_mms_item_name((unsigned char)head_mark),
tmp_str));
while (*p++ !=0);
}
break;
case MMS_HEAD_RETRV_STAT:
p+=2;
break;
case MMS_HEAD_ETRV_TXT:
while (*p++ !=0);
break;
case MMS_HEAD_READ_STAT:
p+=2;
break;
case MMS_HEAD_REPLY_CHG:
p+=2;
break;
case MMS_HEAD_REPLY_CHG_DL:
{
long len;
int ptr_mov;
p++;
len = read_wsp_value_length(p, ptr_mov);
p += len + 1;
}
break;
case MMS_HEAD_REPLY_CHG_ID:
while (*p++ !=0);
break;
case MMS_HEAD_REPLY_CHG_SIZE:
{
long len;
int ptr_mov;
p++;
len = read_wsp_value_length(p, ptr_mov);
p += len + 1;
}
break;
case MMS_HEAD_PREV_SEND_BY:
{
long len;
int ptr_mov;
p++;
len = read_wsp_value_length(p, ptr_mov);
p += len + 1;
}
break;
case MMS_HEAD_PREV_SEND_DATE:
{
long len;
int ptr_mov;
p++;
len = read_wsp_value_length(p, ptr_mov);
p += len + 1;
}
break;
default:
mplog->write(LOG_LEVEL_DEBUG, __FILE__, __LINE__, "Find default char, please check the parser.");
p++;
}
}
return msg_type;
}
int MmsParser::get_mms_content_from_http(char * &begin)
{
char *ch_begin, *ch_end;
int data_len;
/***
* find the boundary
*************/
if (buf == NULL) return -1;
ch_begin = strstr(buf, MMS_KEY_BOUNDARY);
if (ch_begin == NULL) {
mplog->write(LOG_LEVEL_DEBUG, __FILE__, __LINE__,
"Parser message error, no boundary.");
return -1;
}
ch_begin += strlen(MMS_KEY_BOUNDARY);
ch_end = strstr(ch_begin, "\r\n\r\n");
data_len = ch_end - ch_begin;
begin = ch_begin;
return data_len;
}
/****
* get the parser result.
******************/
void MmsParser::get_parser_result(MmsListNode *pNode)
{
char *bin_buf;
int len;
int msg_type;
len = get_bin_block(bin_buf);
if (len == -1)
{
pNode->msg_type = INVALID_TYPE;
pNode->item_list = NULL;
return ;
}
buf = bin_buf;
buf_length = len;
msg_type = fill_mms_item_list(bin_buf, len, pNode->item_list);
pNode->msg_type = msg_type;
// create a random int as the internal id
pNode->internal_id = time(NULL) * 1000 + rand()%1000;
mplog->writeBIN(LOG_LEVEL_DEBUG, __FILE__, __LINE__,
"Decoded result:", bin_buf, len);
free(buf);
}
/***
* decode from base64 string
**********/
int MmsParser::get_bin_block(char *&bin_buf)
{
char *encoded_buf;
int len,dst_len;
if (buf == NULL)
return -1;
len = get_mms_content_from_http(encoded_buf);
dst_len = CMmsCoder::Base64DecodeSize(len);
bin_buf = (char *)malloc(dst_len);
if (bin_buf == NULL)
{
mplog->write(LOG_LEVEL_DEBUG, __FILE__, __LINE__,
"Out of memory.");
}
dst_len = CMmsCoder::base64_decode(encoded_buf, len, bin_buf);
return dst_len;
}
char *MmsParser::get_item_contents(CPtrList *pList, int item_type)
{
POSITION pos;
MmsItemListNode *pItemNode;
if (pList == NULL)
return "";
pos = pList->GetHeadPosition();
while (pos)
{
pItemNode = (MmsItemListNode *)pList->GetNext(pos);
if (pItemNode->item_type == item_type)
return pItemNode->content;
}
return "NONE EXIT";
}
void MmsParser::string_decode(char *sub, int len, char *&sub_final, int code)
{
WCHAR* strA;
char *strB, *tmp_str;
int i;
tmp_str = (char *)malloc(len + 2);
if (tmp_str == NULL)
{
mplog->write(LOG_LEVEL_ERROR, __FILE__, __LINE__,
"Out of memory.");
return;
}
memcpy(tmp_str, sub, len);
tmp_str[len] = 0;
sub = tmp_str;
sub_final = NULL;
switch(code)
{
case UTF_8:
i = MultiByteToWideChar ( CP_UTF8 , 0 ,(char*) sub ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_UTF8 , 0 ,( char * ) sub, -1, strA , i);
i= WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);
strB = (char *)malloc(i);
if (strB == NULL)
return;
WideCharToMultiByte (CP_ACP,0,strA,-1,strB,i,NULL,NULL);
break;
default: //no coding
strB = (char *)malloc(strlen(sub) + 1);
if (strB == NULL)
return;
memcpy(strB, sub, strlen(sub));
break;
}
free(tmp_str);
sub_final = strB;
}
/******
* get necessary information for retrieving mms.
************/
void MmsParser::get_mms_req_info(MmsListNode *pNode, char *svr_ip, int &svr_port,
char *msg_id)
{
CPtrList *p;
MmsItemListNode *pItem;
POSITION pos;
char loc_buf[MAX_SIZE_LOCATION];
char tmp[MAX_SIZE_RSPNO];
int len;
p = pNode->item_list;
for (pos = p->GetHeadPosition(); pos != NULL;)
{
pItem = (MmsItemListNode *)p->GetNext(pos);
if (pItem->item_type == MMS_HEAD_CON_LOCA)
{
strcpy(loc_buf, pItem->content);
strupr(loc_buf);
break;
}
}
if (pos == NULL)
{
mplog->write(LOG_LEVEL_ERROR, __FILE__, __LINE__,
"Can not find [%s] item.", pItem->name);
return;
}
char *ch_start, *ch_end;
ch_start = strstr(loc_buf, PPG_HTTP_HEADER);
ch_start += strlen(PPG_HTTP_HEADER);
//find the end of ip address
ch_end = strchr(ch_start, ':');
len = ch_end - ch_start;
memcpy(svr_ip, ch_start, len);
*(svr_ip + len) = 0;
//skip the ':'
ch_start = ch_end + 1;
ch_end = strchr(ch_start, '/');
len = ch_end - ch_start;
memcpy(tmp, ch_start, len);
*(tmp + len) = 0;
svr_port = atoi(tmp);
//find the msg_id
ch_start = ch_end + 1;
strcpy(msg_id, ch_start);
ch_start ++;
}
int MmsParser::deal_with_mms_body(CListCtrl *msgList, unsigned int internal_id)
{
char *ch;
int msg_type;
CPtrList *pList;
MmsListNode *pMsgNode;
char *msg_id, *mms_buf;
int mms_buf_length, i;
POSITION pos;
MmsItemListNode *pItem;
ch = NULL;
// find the tail of http header
for (i = 0; i < buf_length - 4; i++)
{
if (buf[i] == '\r' &&
buf[i + 1] == '\n' &&
buf[i + 2] == '\r' &&
buf[i + 3] == '\n')
{
ch = buf + i;
break;
}
}
// do not find the http header tail flag
if (ch == NULL)
{
mplog->write(LOG_LEVEL_ERROR, __FILE__, __LINE__,
"Not found http header in mms_retrieve package.");
return -1;
}
mms_buf = ch + 4;
mms_buf_length = buf_length - (mms_buf - buf);
mplog->writeBIN(LOG_LEVEL_DEBUG, __FILE__, __LINE__,
"Message Content Body:", mms_buf, mms_buf_length);
// get the item list from mms body
msg_type = fill_mms_item_list(mms_buf, mms_buf_length, pList);
if (msg_type != MMS_MSGTYPE_RETRIEVE_CONF)
{
mplog->write(LOG_LEVEL_ERROR, __FILE__, __LINE__,
"The received mms body msg is not a retrieve.conf.");
}
msg_id = NULL;
for (pos = pList->GetHeadPosition(); pos != NULL;)
{
pItem = (MmsItemListNode *)pList->GetNext(pos);
mplog->write(LOG_LEVEL_DEBUG, __FILE__, __LINE__,
"Key Items in MMS Header:%s:%s.", pItem->name, pItem->content);
if (pItem->item_type == MMS_HEAD_MSG_ID)
{
msg_id = pItem->content;
break;
}
}
if (msg_id == NULL) {
mplog->write(LOG_LEVEL_ERROR, __FILE__, __LINE__,
"Do not find message_id node from mms body.");
return -1;
}
// find the proper msg node(through msg_id) and insert the mms body in it
pMsgNode = find_proper_msgnode(msgList, internal_id);
if (pMsgNode == NULL)
return -1;
pMsgNode->body_item_list = pList;
// set read reply item
pMsgNode->read_reply_flag = read_reply_flag;
// set the from_msisdn, which is got when parsing mms
strcpy(pMsgNode->from_msisdn, from_msisdn);
// set the msg_id, which is got when parsing mms
strcpy(pMsgNode->msg_id, msg_id);
MmsContent contentItem;
contentItem.set_content_body_buf(attachments_area_ptr, attachments_area_length);
contentItem.deal_with_attachments(pMsgNode);
mplog->writeBIN(LOG_LEVEL_DEBUG, __FILE__, __LINE__,
"Attachments Area:", attachments_area_ptr, attachments_area_length);
return 0;
}
/******
* Get proper msg node according to message_id
**************/
MmsListNode * MmsParser::find_proper_msgnode(CListCtrl *msgList,
unsigned long internal_id)
{
int count, i;
MmsListNode *pNode;
count = msgList->GetItemCount();
for (i = 0; i < count; i++)
{
pNode = (MmsListNode *)msgList->GetItemData(i);
if (pNode->internal_id == internal_id)
return pNode;
}
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -