极利恒河 CMPP2 MSGID反解JAVA和PHP版本

最近做了个短信接口,MSGID是二进制加base64提供的,看文档云里雾里,从接口提供方要了JAVA版本的反解代码,凭着对java的一点点了解,将其从原代码里单独出来,方便测试,并给转换了对应的PHP版本,因为我的程序是PHP的。

 

独立出来的JAVA代码如下:

 

package com;

import com.mindprod.base64.Base64;

public class main {

	static private  boolean bOutPutException = false;
 
	
	static public void main(String _arg[])throws Exception{
		
		String[] nMsgID = new String[30];
		byte[] sStat = new byte[8];
		byte[] sSubmitTime = new byte[11];
		byte[] sDoneTime = new byte[11];
		byte[] sDestTermID = new byte[32];
		int[] aiSmscSeq = new int[2];
		byte[] sMsgContent = new byte[160];
		Base64 base64 = new Base64();
		base64.setLineLength( 160 );
		String test = "Ptp9QAPpkrhERUxJVlJEMTIwMzI5MjIzODEyMDMyOTIyMzgxNTgxMDY3MjI0NgAAAAAAAAAAAAAAAAAA";
		sMsgContent = base64.decode(test);
		///System.out.println(sMsgContent);
		//String test2 = base64.encode(sMsgContent);
		//System.out.println(test2);
		//sMsgContent = test2.getBytes(); 
	 
		CMPPAnalyzeStatusReport(
				sMsgContent, nMsgID, sStat,
				sSubmitTime, sDoneTime, sDestTermID, aiSmscSeq);
	//	String reconstitutedString =
      //      new String( nMsgID, "8859_1"/* encoding */ );
	//	System.out.println(reconstitutedString);
	}
	static public int CMPPAnalyzeStatusReport(byte[] abMsgContent, String[] asMsgId,
			byte[] abStat, byte[] abSubmitTime, byte[] abDoneTime,
			byte[] abDestTerminalId, int[] aiSmscSeq) {
		if ((160 < abMsgContent.length) || (8 > abMsgContent.length)) {
			return -1;
		}
		byte[] lbEncodeMsgId = new byte[8];
		int[] liMsgID = new int[1];

		try {
			System.arraycopy(abMsgContent, 0, lbEncodeMsgId, 0, 8);
			System.arraycopy(abMsgContent, 8, abStat, 0, 7);
			System.arraycopy(abMsgContent, 15, abSubmitTime, 0, 10);
			System.arraycopy(abMsgContent, 25, abDoneTime, 0, 10);
			System.arraycopy(abMsgContent, 35, abDestTerminalId, 0, 21);
			aiSmscSeq[0] = 0;
		} catch (NullPointerException e) {
			if (bOutPutException == true) {
				System.err.println("NullPointerException " + e.toString());
			}
			return 1;
		} catch (IndexOutOfBoundsException e) {
			if (bOutPutException == true) {
				System.err.println("IndexOutOfBoundsException" + e.toString());
			}
			return 1;
		}

		// return ( theCmppClass.DecodeCMPPMsgID ( lbEncodeMsgId, liMsgID,
		// asMsgId ) ) ;
		DecodeCMPPMsgID(lbEncodeMsgId, liMsgID, asMsgId);
		// System.out.println ( "kkkkkkkkkkkk" + asMsgId[0].getBytes() ) ;
		return 0;
	}
	static public int DecodeCMPPMsgID(byte[] sCMPPMsgID, int[] nMsgID, String[] sMsgID) {
		int nISMGNo, nMonth, nDay, nHour, nMin, nSecond, nSeqNo;
		StringBuffer sMessageID = new StringBuffer();
		sMsgID[0] = new String();

		if (sCMPPMsgID.length != 8)
			return -1;

		long lMsgID = getLongByBytesBe(sCMPPMsgID);
//System.out.println(lMsgID);
		nSeqNo = (int) (lMsgID & (long) 0xFFFF);
//System.out.println(nSeqNo);
		lMsgID = lMsgID >>> 16;
//System.out.println(lMsgID);
		nISMGNo = (int) (lMsgID & (long) 0x3FFFFF);
		lMsgID = lMsgID >>> 22;
		nSecond = (int) (lMsgID & (long) 0x3F);
		lMsgID = lMsgID >>> 6;
		nMin = (int) (lMsgID & (long) 0x3F);
		lMsgID = lMsgID >>> 6;
		nHour = (int) (lMsgID & (long) 0x1F);
		lMsgID = lMsgID >>> 5;
		nDay = (int) (lMsgID & (long) 0x1F);
		lMsgID = lMsgID >>> 5;
		nMonth = (int) (lMsgID & (long) 0xF);

		//amended at 02/11/15, for bug 2933, fixed the wrong time of msgid
		if (nSecond >= 60) {
			nMin += nSecond / 60;
			nSecond = nSecond % 60;
		}
		if (nMin >= 60) {
			nHour += nMin / 60;
			nMin = nSecond % 60;
		}
		if (nHour >= 24) {
			nDay += nHour / 24;
			nHour = nHour % 24;
		}

		sMessageID.setLength(0);
		nMsgID[0] = nSeqNo;
		sMessageID.append(FormatInt(nMonth, 2));
		sMessageID.append(FormatInt(nDay, 2));
		sMessageID.append(FormatInt(nHour, 2));
		sMessageID.append(FormatInt(nMin, 2));
		sMessageID.append(FormatInt(nSecond, 2));
		sMessageID.append(FormatInt(nISMGNo, 5));
		sMessageID.append(FormatInt(nSeqNo, 5));

		sMsgID[0] = sMessageID.toString();
System.out.println(sMsgID[0]);
		return (0);
	}
	static public long getLongByBytesBe(byte[] bytes) {
		int len = bytes.length < 8 ? bytes.length : 8;
		long num = 0;
		for (int i = 0; i < len; ++i) {
			num <<= 8;
			num |= 0xFFL & (long) bytes[i];
		}
		return num;
	}

	static private String FormatInt(int a, int len) {
		String result = "" + a;
		while (result.length() < len) {
			result = "0" + result;
		}
		if (result.length() > 5) {
			result = result.substring(0, 5);
		}
		return result;
	}

}

 

 

 

PHP代码就不贴出来了,需要的下载附件即可。

2012-10-15更新----

decode_unix.rar的php的反解有点错误,更新包在testsms121015.rar

testsms121015.rar (7.1 K, 下载次数:184, 上传时间:2012-10-15 18:24)
base64.zip (98.82 K, 下载次数:111, 上传时间:2012-03-30 10:21)
decode_unit.rar (2.19 K, 下载次数:108, 上传时间:2012-03-30 10:18)

Tags: msgid , cmpp2

上一篇: webservice服务增加header认证   下一篇: SMPP 3.3时间格式

你问我答

  1. #1 头像 hahalwxha 2012-05-16 11:41:35
    下载来看看php版本的

提交疑问

回顶部