⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmsdlg.cpp

📁 实现了彩信编程协议中的MIME协议的封装
💻 CPP
字号:
// MMSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MMS.h"
#include "MMSDlg.h"
#include <string.h> 
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMMSDlg dialog

CMMSDlg::CMMSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMMSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMMSDlg)
	m_filename = _T("");
	m_DestNumber = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMMSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMMSDlg)
	DDX_Text(pDX, IDC_EDIT3, m_filename);
	DDX_Text(pDX, IDC_EDIT1, m_DestNumber);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMMSDlg, CDialog)
	//{{AFX_MSG_MAP(CMMSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Open, OnOpen)
	ON_BN_CLICKED(IDC_Save, OnSave)
	ON_BN_CLICKED(IDC_Code, OnCode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMMSDlg message handlers

BOOL CMMSDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMMSDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMMSDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMMSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMMSDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    CFileDialog filedlg(TRUE);
	filedlg.DoModal();
	m_filename = filedlg.GetPathName();
	UpdateData(FALSE);
    
}

void CMMSDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	  
	  CFileDialog  saveDlg(FALSE);
	  saveDlg.DoModal();
      m_SaveFileName = saveDlg.GetPathName();
	  WriteBinFile(m_MMSData,30000,m_SaveFileName);
      free(m_MMSData);
}

long CMMSDlg::ReadBinFile(char* buf,LPCTSTR filename)
{ 
    
	FILE *fp = NULL;
	long i;
	if ((fp = fopen(filename, "rb" ) )==NULL)
	{
		return 0;
	}

	fseek(fp, 0L, SEEK_END);
	i = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	i = fread(buf, 1, i, fp);
	fclose(fp);
	return i;

}
int CMMSDlg::WriteBinFile(unsigned char* buf,long FileLen,LPCSTR  filename)
{
    FILE *fp=NULL;
	if((fp = fopen(filename,"wb") )==NULL)
	{
	     return 0;
	}
	fwrite(buf,1,FileLen,fp);
	fclose(fp);
	return 1;
}
int CMMSDlg::MMSCode(unsigned char mmsData[90000],long fileLen,const char * strFile)
{
   	
	unsigned char *pzMmsData=mmsData;
	char str[1000],strTmp[1000];
	int i=0,j=0;
	int byLength=0;
	IN DWORD dwUintvar;
	memset(pzMmsData,0,90000);//初始化
   	strcpy(strTmp,"<smil xmlns=\"http://www.w3.org/2001/SMIL20/Languae\">\n\
   <head>\n\
     <layout>\n\
      <root-layout width=\"160\" height=\"140\"/>\n\
	    <region id=\"Image\" width=\"160\" height=\"120\" left=\"0\" top=\"0\"/>\n\
        <region id=\"Text\" width=\"160\" height=\"20\" left=\"0\" top=\"120\"/>\n\
      </layout>\n\
   </head>\n\
   <body>\n\
    <par>\n\
     <img src=\"test.jpg\"  region=\"Image\"/>\n\
     <text src=\"HelloWorld.txt\" region=\"Text\"/>\n\
	</par>\n\
   </body>\n\
</smil>\r\n");
  
	//标题编码
	mmsData[0]=0x8C;mmsData[1]=0x80;
	mmsData[2]=0x98;mmsData[3]=0x30;
	mmsData[4]=0x31;mmsData[5]=0x32;
	mmsData[6]=0x33;mmsData[7]=0x34;
	mmsData[8]=0x35;mmsData[9]=0x36;
	mmsData[10]=0x37;mmsData[11]=0x38;
	mmsData[12]=0x39;
	mmsData[14]=0x8D; 
	//源地址
	mmsData[15]=0x90;
	mmsData[16]=0x89;mmsData[17]=0x1A;//地址长度
	mmsData[18]=0x80;
	pzMmsData = &mmsData[19];
	strcpy(str,"+8613533337171/TYPE=PLMN");
	memcpy(pzMmsData,str,strlen(str));
	//目的地址
	mmsData[44]=0x97;
	pzMmsData =&mmsData[45];
	strcpy(str,m_DestNumber);
	strcat(str,"/TYPE=PLMN");
	memcpy(pzMmsData,str,strlen(str));
	mmsData[70]=0x96;
	//消息主题
	pzMmsData=&mmsData[71];
	strcpy(str,"My first test message!");
	memcpy(pzMmsData,str,strlen(str));
    i=71+strlen(str)+1;
	//Content-type
	mmsData[i++]=0x84;mmsData[i++]=0x1B;//值的长度
	j=i+0x1B;//指向正文部分;
	mmsData[i++]=0xB3;mmsData[i++]=0x89;
	//类型参数
	pzMmsData=&mmsData[i];
	strcpy(str,"application/smil");
    memcpy(pzMmsData,str,strlen(str));
    i+=strlen(str)+1;
	//起始参数
	mmsData[i++]=0x8A;
    pzMmsData=&mmsData[i];
	strcpy(str,"<0000>");
	memcpy(pzMmsData,str,strlen(str));
	//正文编码
	i=j;
    mmsData[i++]=0x03;//正文只有三部分
	//第一部分(smil部分)
	mmsData[i++]=0x23;//内容类型+其他可能标题的长度
	//数据长度
    pzMmsData=&mmsData[i];
	dwUintvar = strlen(strTmp);
    byLength = GetUintvarLength(dwUintvar);
    UintvarEncode(dwUintvar,byLength,&pzMmsData);
	i=i+byLength;
	j=i+0x23;//指向数据部分
	//内容类型+其他可能标题(文本编码)
	pzMmsData=&mmsData[i];
	strcpy(str,"application/smil");
	memcpy(pzMmsData,str,strlen(str));
	i+=strlen(str)+1;
	pzMmsData=&mmsData[i];
	strcpy(str,"Content-ID");
	memcpy(pzMmsData,str,strlen(str));
	i+=strlen(str)+1;
	pzMmsData=&mmsData[i];
	strcpy(str,"<0000>");
    memcpy(pzMmsData,str,strlen(str));
	//实际数据
	i=j;
	j=j+dwUintvar;//指向第二部分
	pzMmsData=&mmsData[i];
   memcpy(pzMmsData,strTmp,dwUintvar);
//正文第二部分(image/jpg)编码
   i=j;
   mmsData[i++]=0x11;//内容类型+其他可能标题长度
   //实际数据长度
   pzMmsData=&mmsData[i];
   dwUintvar = fileLen;
   byLength = GetUintvarLength(dwUintvar);
   UintvarEncode(dwUintvar,byLength,&pzMmsData);
   i=i+byLength;
   j=i+0x11;//指向数据部分
   //内容类型+其他可能标题
   mmsData[i++]=0x9D;  //image/jpg的码值
   mmsData[i++]=0x8E;//内容位置
   pzMmsData=&mmsData[i];
   strcpy(str,"test.jpg");
   memcpy(pzMmsData,str,strlen(str));
   //实际数据
   i=j;
   j=j+dwUintvar;//指向第三部分
   pzMmsData=&mmsData[i];
   memcpy(pzMmsData,strFile,fileLen);
//正文第三部分(text/plain)编码
   i=j;
   mmsData[i++]=0x11;//内容类型+其他可能标题长度
   mmsData[i++]=0x00;mmsData[i++]=0x0E;//实际数据长度
   j=i+0x11;
 //内容类型+其他可能标题
   mmsData[i++]=0x83;//text/plain码值
   mmsData[i++]=0x8E;//内容位置
   pzMmsData=&mmsData[i];
   strcpy(str,"HelloWorld.txt");
   memcpy(pzMmsData,str,strlen(str));
   //实际数据
   i=j;
   pzMmsData=&mmsData[i];
   strcpy(str,"HelloWorld!");
   memcpy(pzMmsData,str,strlen(str));
return 1;
}

void CMMSDlg::OnCode() 
{   
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    m_strFile = (char *)malloc(80000);
    m_FileLen = ReadBinFile(m_strFile,m_filename);
	m_MMSData=(unsigned char *)malloc(90000);
	MMSCode(m_MMSData,m_FileLen,m_strFile);
	free(m_strFile);
}
//////////////////////////////////////////////////////////////////////////
// BYTE GetUintvarLength(IN DWORD dwUintvar)
// 函数功能:
//	计算出变长无符号整数编码后的长度
// 参数说明:
//	dwUintvar:	输入参数,待编码的变长无符号整数
// 返回值:
//	变长无符号整数编码后的长度
// 备注:
//	
// 修改记录:
//	1:	谭伟基 2007/04/4撰写
//////////////////////////////////////////////////////////////////////////
BYTE CMMSDlg::GetUintvarLength(IN DWORD dwUintvar)
{
	int i;

	if (!dwUintvar)
	{
		return 1;
	}

	for (i = 4;(i >= 0)&&(!(dwUintvar>>i*7));i--)
	{
	}

	return i+1;
}
//////////////////////////////////////////////////////////////////////////
// void UintvarEncode(IN DWORD dwUintvar,IN BYTE byLength,IN OUT BYTE **ppPDU)
// 函数功能:
//	变长无符号整数编码,并填入PDU对应的位置
// 参数说明:
//	dwUintvar:	输入参数,待编码的变长无符号整数
//  byLength:	输入参数,变长无符号整数编码后的长度
//	ppPDU:		输入输出参数,指向PDU当前位置指针的指针
// 返回值:
//	
// 备注:
//	整数填入了PDU后,PDU当前位置指针将后移
// 修改记录:
//	1:	谭伟基 2007/04/01撰写
//	2:  谭伟基 2007/04/04修改
//////////////////////////////////////////////////////////////////////////
//void UintvarEncode(IN DWORD dwUintvar,
//				   IN OUT BYTE **ppPDU)
void CMMSDlg::UintvarEncode(IN DWORD dwUintvar,
				   IN BYTE byLength, 
				   IN OUT BYTE **ppPDU)
{
/*	int i;
	//找出编码结果的长度
	for (i = 4;(i >= 0)&&(!(dwUintvar>>i*7));i--)
	{
	}

	//对每7bit编码并填入PDU
	for(;i >= 0;i--,(*ppPDU)++)
*/
	for(int i = byLength - 1;i >=0;i--,(*ppPDU)++)
	{
		**ppPDU = (BYTE)((dwUintvar>>i*7)&0x7F)|(!i?0:0x80);
	}
}

⌨️ 快捷键说明

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