MMSDlg.cpp
上传用户:szhqbq
上传日期:2014-07-08
资源大小:46k
文件大小:11k
开发平台:

Visual C++

  1. // MMSDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MMS.h"
  5. #include "MMSDlg.h"
  6. #include <string.h> 
  7. #include <stdio.h>
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMMSDlg dialog
  52. CMMSDlg::CMMSDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CMMSDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CMMSDlg)
  56. m_filename = _T("");
  57. m_DestNumber = _T("");
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CMMSDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CMMSDlg)
  66. DDX_Text(pDX, IDC_EDIT3, m_filename);
  67. DDX_Text(pDX, IDC_EDIT1, m_DestNumber);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(CMMSDlg, CDialog)
  71. //{{AFX_MSG_MAP(CMMSDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_BN_CLICKED(IDC_Open, OnOpen)
  76. ON_BN_CLICKED(IDC_Save, OnSave)
  77. ON_BN_CLICKED(IDC_Code, OnCode)
  78. //}}AFX_MSG_MAP
  79. END_MESSAGE_MAP()
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CMMSDlg message handlers
  82. BOOL CMMSDlg::OnInitDialog()
  83. {
  84. CDialog::OnInitDialog();
  85. // Add "About..." menu item to system menu.
  86. // IDM_ABOUTBOX must be in the system command range.
  87. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  88. ASSERT(IDM_ABOUTBOX < 0xF000);
  89. CMenu* pSysMenu = GetSystemMenu(FALSE);
  90. if (pSysMenu != NULL)
  91. {
  92. CString strAboutMenu;
  93. strAboutMenu.LoadString(IDS_ABOUTBOX);
  94. if (!strAboutMenu.IsEmpty())
  95. {
  96. pSysMenu->AppendMenu(MF_SEPARATOR);
  97. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  98. }
  99. }
  100. // Set the icon for this dialog.  The framework does this automatically
  101. //  when the application's main window is not a dialog
  102. SetIcon(m_hIcon, TRUE); // Set big icon
  103. SetIcon(m_hIcon, FALSE); // Set small icon
  104. // TODO: Add extra initialization here
  105. return TRUE;  // return TRUE  unless you set the focus to a control
  106. }
  107. void CMMSDlg::OnSysCommand(UINT nID, LPARAM lParam)
  108. {
  109. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  110. {
  111. CAboutDlg dlgAbout;
  112. dlgAbout.DoModal();
  113. }
  114. else
  115. {
  116. CDialog::OnSysCommand(nID, lParam);
  117. }
  118. }
  119. // If you add a minimize button to your dialog, you will need the code below
  120. //  to draw the icon.  For MFC applications using the document/view model,
  121. //  this is automatically done for you by the framework.
  122. void CMMSDlg::OnPaint() 
  123. {
  124. if (IsIconic())
  125. {
  126. CPaintDC dc(this); // device context for painting
  127. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  128. // Center icon in client rectangle
  129. int cxIcon = GetSystemMetrics(SM_CXICON);
  130. int cyIcon = GetSystemMetrics(SM_CYICON);
  131. CRect rect;
  132. GetClientRect(&rect);
  133. int x = (rect.Width() - cxIcon + 1) / 2;
  134. int y = (rect.Height() - cyIcon + 1) / 2;
  135. // Draw the icon
  136. dc.DrawIcon(x, y, m_hIcon);
  137. }
  138. else
  139. {
  140. CDialog::OnPaint();
  141. }
  142. }
  143. // The system calls this to obtain the cursor to display while the user drags
  144. //  the minimized window.
  145. HCURSOR CMMSDlg::OnQueryDragIcon()
  146. {
  147. return (HCURSOR) m_hIcon;
  148. }
  149. void CMMSDlg::OnOpen() 
  150. {
  151. // TODO: Add your control notification handler code here
  152. UpdateData(TRUE);
  153.     CFileDialog filedlg(TRUE);
  154. filedlg.DoModal();
  155. m_filename = filedlg.GetPathName();
  156. UpdateData(FALSE);
  157.     
  158. }
  159. void CMMSDlg::OnSave() 
  160. {
  161. // TODO: Add your control notification handler code here
  162.   
  163.   CFileDialog  saveDlg(FALSE);
  164.   saveDlg.DoModal();
  165.       m_SaveFileName = saveDlg.GetPathName();
  166.   WriteBinFile(m_MMSData,30000,m_SaveFileName);
  167.       free(m_MMSData);
  168. }
  169. long CMMSDlg::ReadBinFile(char* buf,LPCTSTR filename)
  170.     
  171. FILE *fp = NULL;
  172. long i;
  173. if ((fp = fopen(filename, "rb" ) )==NULL)
  174. {
  175. return 0;
  176. }
  177. fseek(fp, 0L, SEEK_END);
  178. i = ftell(fp);
  179. fseek(fp, 0L, SEEK_SET);
  180. i = fread(buf, 1, i, fp);
  181. fclose(fp);
  182. return i;
  183. }
  184. int CMMSDlg::WriteBinFile(unsigned char* buf,long FileLen,LPCSTR  filename)
  185. {
  186.     FILE *fp=NULL;
  187. if((fp = fopen(filename,"wb") )==NULL)
  188. {
  189.      return 0;
  190. }
  191. fwrite(buf,1,FileLen,fp);
  192. fclose(fp);
  193. return 1;
  194. }
  195. int CMMSDlg::MMSCode(unsigned char mmsData[90000],long fileLen,const char * strFile)
  196. {
  197.    
  198. unsigned char *pzMmsData=mmsData;
  199. char str[1000],strTmp[1000];
  200. int i=0,j=0;
  201. int byLength=0;
  202. IN DWORD dwUintvar;
  203. memset(pzMmsData,0,90000);//初始化
  204.     strcpy(strTmp,"<smil xmlns="http://www.w3.org/2001/SMIL20/Languae">n
  205.    <head>n
  206.      <layout>n
  207.       <root-layout width="160" height="140"/>n
  208.     <region id="Image" width="160" height="120" left="0" top="0"/>n
  209.         <region id="Text" width="160" height="20" left="0" top="120"/>n
  210.       </layout>n
  211.    </head>n
  212.    <body>n
  213.     <par>n
  214.      <img src="test.jpg"  region="Image"/>n
  215.      <text src="HelloWorld.txt" region="Text"/>n
  216. </par>n
  217.    </body>n
  218. </smil>rn");
  219.   
  220. //标题编码
  221. mmsData[0]=0x8C;mmsData[1]=0x80;
  222. mmsData[2]=0x98;mmsData[3]=0x30;
  223. mmsData[4]=0x31;mmsData[5]=0x32;
  224. mmsData[6]=0x33;mmsData[7]=0x34;
  225. mmsData[8]=0x35;mmsData[9]=0x36;
  226. mmsData[10]=0x37;mmsData[11]=0x38;
  227. mmsData[12]=0x39;
  228. mmsData[14]=0x8D; 
  229. //源地址
  230. mmsData[15]=0x90;
  231. mmsData[16]=0x89;mmsData[17]=0x1A;//地址长度
  232. mmsData[18]=0x80;
  233. pzMmsData = &mmsData[19];
  234. strcpy(str,"+8613533337171/TYPE=PLMN");
  235. memcpy(pzMmsData,str,strlen(str));
  236. //目的地址
  237. mmsData[44]=0x97;
  238. pzMmsData =&mmsData[45];
  239. strcpy(str,m_DestNumber);
  240. strcat(str,"/TYPE=PLMN");
  241. memcpy(pzMmsData,str,strlen(str));
  242. mmsData[70]=0x96;
  243. //消息主题
  244. pzMmsData=&mmsData[71];
  245. strcpy(str,"My first test message!");
  246. memcpy(pzMmsData,str,strlen(str));
  247.     i=71+strlen(str)+1;
  248. //Content-type
  249. mmsData[i++]=0x84;mmsData[i++]=0x1B;//值的长度
  250. j=i+0x1B;//指向正文部分;
  251. mmsData[i++]=0xB3;mmsData[i++]=0x89;
  252. //类型参数
  253. pzMmsData=&mmsData[i];
  254. strcpy(str,"application/smil");
  255.     memcpy(pzMmsData,str,strlen(str));
  256.     i+=strlen(str)+1;
  257. //起始参数
  258. mmsData[i++]=0x8A;
  259.     pzMmsData=&mmsData[i];
  260. strcpy(str,"<0000>");
  261. memcpy(pzMmsData,str,strlen(str));
  262. //正文编码
  263. i=j;
  264.     mmsData[i++]=0x03;//正文只有三部分
  265. //第一部分(smil部分)
  266. mmsData[i++]=0x23;//内容类型+其他可能标题的长度
  267. //数据长度
  268.     pzMmsData=&mmsData[i];
  269. dwUintvar = strlen(strTmp);
  270.     byLength = GetUintvarLength(dwUintvar);
  271.     UintvarEncode(dwUintvar,byLength,&pzMmsData);
  272. i=i+byLength;
  273. j=i+0x23;//指向数据部分
  274. //内容类型+其他可能标题(文本编码)
  275. pzMmsData=&mmsData[i];
  276. strcpy(str,"application/smil");
  277. memcpy(pzMmsData,str,strlen(str));
  278. i+=strlen(str)+1;
  279. pzMmsData=&mmsData[i];
  280. strcpy(str,"Content-ID");
  281. memcpy(pzMmsData,str,strlen(str));
  282. i+=strlen(str)+1;
  283. pzMmsData=&mmsData[i];
  284. strcpy(str,"<0000>");
  285.     memcpy(pzMmsData,str,strlen(str));
  286. //实际数据
  287. i=j;
  288. j=j+dwUintvar;//指向第二部分
  289. pzMmsData=&mmsData[i];
  290.    memcpy(pzMmsData,strTmp,dwUintvar);
  291. //正文第二部分(image/jpg)编码
  292.    i=j;
  293.    mmsData[i++]=0x11;//内容类型+其他可能标题长度
  294.    //实际数据长度
  295.    pzMmsData=&mmsData[i];
  296.    dwUintvar = fileLen;
  297.    byLength = GetUintvarLength(dwUintvar);
  298.    UintvarEncode(dwUintvar,byLength,&pzMmsData);
  299.    i=i+byLength;
  300.    j=i+0x11;//指向数据部分
  301.    //内容类型+其他可能标题
  302.    mmsData[i++]=0x9D;  //image/jpg的码值
  303.    mmsData[i++]=0x8E;//内容位置
  304.    pzMmsData=&mmsData[i];
  305.    strcpy(str,"test.jpg");
  306.    memcpy(pzMmsData,str,strlen(str));
  307.    //实际数据
  308.    i=j;
  309.    j=j+dwUintvar;//指向第三部分
  310.    pzMmsData=&mmsData[i];
  311.    memcpy(pzMmsData,strFile,fileLen);
  312. //正文第三部分(text/plain)编码
  313.    i=j;
  314.    mmsData[i++]=0x11;//内容类型+其他可能标题长度
  315.    mmsData[i++]=0x00;mmsData[i++]=0x0E;//实际数据长度
  316.    j=i+0x11;
  317.  //内容类型+其他可能标题
  318.    mmsData[i++]=0x83;//text/plain码值
  319.    mmsData[i++]=0x8E;//内容位置
  320.    pzMmsData=&mmsData[i];
  321.    strcpy(str,"HelloWorld.txt");
  322.    memcpy(pzMmsData,str,strlen(str));
  323.    //实际数据
  324.    i=j;
  325.    pzMmsData=&mmsData[i];
  326.    strcpy(str,"HelloWorld!");
  327.    memcpy(pzMmsData,str,strlen(str));
  328. return 1;
  329. }
  330. void CMMSDlg::OnCode() 
  331. {   
  332. // TODO: Add your control notification handler code here
  333. UpdateData(TRUE);
  334.     m_strFile = (char *)malloc(80000);
  335.     m_FileLen = ReadBinFile(m_strFile,m_filename);
  336. m_MMSData=(unsigned char *)malloc(90000);
  337. MMSCode(m_MMSData,m_FileLen,m_strFile);
  338. free(m_strFile);
  339. }
  340. //////////////////////////////////////////////////////////////////////////
  341. // BYTE GetUintvarLength(IN DWORD dwUintvar)
  342. // 函数功能:
  343. // 计算出变长无符号整数编码后的长度
  344. // 参数说明:
  345. // dwUintvar: 输入参数,待编码的变长无符号整数
  346. // 返回值:
  347. // 变长无符号整数编码后的长度
  348. // 备注:
  349. //
  350. // 修改记录:
  351. // 1: 谭伟基 2007/04/4撰写
  352. //////////////////////////////////////////////////////////////////////////
  353. BYTE CMMSDlg::GetUintvarLength(IN DWORD dwUintvar)
  354. {
  355. int i;
  356. if (!dwUintvar)
  357. {
  358. return 1;
  359. }
  360. for (i = 4;(i >= 0)&&(!(dwUintvar>>i*7));i--)
  361. {
  362. }
  363. return i+1;
  364. }
  365. //////////////////////////////////////////////////////////////////////////
  366. // void UintvarEncode(IN DWORD dwUintvar,IN BYTE byLength,IN OUT BYTE **ppPDU)
  367. // 函数功能:
  368. // 变长无符号整数编码,并填入PDU对应的位置
  369. // 参数说明:
  370. // dwUintvar: 输入参数,待编码的变长无符号整数
  371. //  byLength: 输入参数,变长无符号整数编码后的长度
  372. // ppPDU: 输入输出参数,指向PDU当前位置指针的指针
  373. // 返回值:
  374. //
  375. // 备注:
  376. // 整数填入了PDU后,PDU当前位置指针将后移
  377. // 修改记录:
  378. // 1: 谭伟基 2007/04/01撰写
  379. // 2:  谭伟基 2007/04/04修改
  380. //////////////////////////////////////////////////////////////////////////
  381. //void UintvarEncode(IN DWORD dwUintvar,
  382. //    IN OUT BYTE **ppPDU)
  383. void CMMSDlg::UintvarEncode(IN DWORD dwUintvar,
  384.    IN BYTE byLength, 
  385.    IN OUT BYTE **ppPDU)
  386. {
  387. /* int i;
  388. //找出编码结果的长度
  389. for (i = 4;(i >= 0)&&(!(dwUintvar>>i*7));i--)
  390. {
  391. }
  392. //对每7bit编码并填入PDU
  393. for(;i >= 0;i--,(*ppPDU)++)
  394. */
  395. for(int i = byLength - 1;i >=0;i--,(*ppPDU)++)
  396. {
  397. **ppPDU = (BYTE)((dwUintvar>>i*7)&0x7F)|(!i?0:0x80);
  398. }
  399. }