Java pdu SMS decoding full parsing

  • 2020-05-12 02:37:32
  • OfStack

Short and short letters do not verify the interested can try

It was rewritten according to the python method


/**
* PDU Text parsing 
*
*
* @param pduPayload
* @return
*/
public static String retrieveSMSInfo(byte[] pduPayload) throws UnsupportedEncodingException {
int startPos = 3;
//#Originator address
int mRP_OA_len = pduPayload[startPos];
byte[] mRP_OA = new byte[mRP_OA_len];
System.arraycopy(pduPayload, startPos + 1, mRP_OA, 0, mRP_OA_len);
startPos = startPos + 1 + mRP_OA_len;
int mTPDU_len = pduPayload[startPos];
//#BIT No. 7 6 5 4 3 2 1 0 
//#uplink TP-RP TP-UDHI TP-SPR TP-VPF TP-RD TP-MTI 
//#downlink TP-RP TP-UDHI TP-SRI TP-MMS TP-MTI 
byte TP_Header = pduPayload[startPos + 1];
byte TP_Msg_Ref = pduPayload[startPos + 2];
int TP_UDHI = (TP_Header >> 6) & 1; //# Whether the SMS content contains the protocol header information, 0  Does not contain,  1  Contains (long and short letters, push Text message) 
int TP_VPF = (TP_Header >> 3) & 3; //# Whether it contains valid bytes, 0  Does not contain,   other   contains  
// #00 No validity period, TP-VP Set to 00 .  
// #10 Represents the relative format, TP-VP Take up 1 Bytes.  
// #01 Means add format, TP-VP Take up 7 Bytes.  
// #11 Absolute format, TP-VP Take up 7 byte 
int TP_MMS = (TP_Header >> 2) & 1;//# TP-MMS(TP-More-Message-to-Send) : 1  No more messages were sent from the SMS center 
startPos = startPos + 3;
//# The other number 
byte smsNumberLen = pduPayload[startPos];
int mTP_DA_len = (smsNumberLen + 1) / 2 + 1;
byte[] mTP_DA = new byte[mTP_DA_len];
System.arraycopy(pduPayload, startPos + 1, mTP_DA, 0, mTP_DA_len * 1);
byte mTP_DA_format = mTP_DA[0];
byte[] smsNumberRaw = new byte[mTP_DA.length - 1];
System.arraycopy(mTP_DA, 1, smsNumberRaw, 0, mTP_DA.length - 1);
String smsNumber = "";
int j = 0;
for (int i = 0; i < smsNumberLen; i++) {
if ((i & 1) == 0) {
smsNumber = smsNumber + (int) (smsNumberRaw[j] & 0xF);
} else {
smsNumber = smsNumber + (int) ((smsNumberRaw[j] & 0x0FF) >> 4);
j++;
}
}
startPos = startPos + 1 + mTP_DA_len;
byte mTP_PID = pduPayload[startPos];
byte mTP_DCS = pduPayload[startPos + 1];//# " 00 "Means using 7 Bit encoding, set to" 02 "The use of 8 Bit encoding, set to" 08 "The use of UCS2 Encoding. 
startPos = startPos + 2;
if (TP_VPF == 2) {
startPos = startPos + 1;
} else if (TP_VPF == 1 || TP_VPF == 3) {
startPos = startPos + 7;
}
//#  Short message: need to be added before the content 6 A field  
//# 1 ,   byte 1 : length of baotou, fixed 0x05 ;  
//# 2 ,   byte 2 : baotou type identification, fixed fill 0x00 , long and short letters;  
//# 3 ,   byte 3 : length of subpackage, fixed 0x03 , after 3 The length of a byte;  
//# 4 ,   byte 4 The byte 6 : package contents:  
//# a )   byte 4 : long message reference number, each SP Each reference number should be different for each user and can be sent from 0 So let's start with each one 1 The biggest 255 For the same 1 The two terminals are identical 1 a SP Messages of different length letters are identified;  
//# b )   byte 5 : the total number of long messages in this article from 1 to 255 . 1 The general value should be greater than 2 ;  
//# c )   byte 6 : the position or sequence number of this message in a long message from 1 to 255 In the first 1 Article for the 1 In the first 2 Article for the 2 And the last 1 Article is equal to the first 4 The value of the byte.  
//#  Example:  
//# 05 00 03 00 02 01 
//# 05 00 03 00 02 02 
int smsPayloadLen = pduPayload[startPos];
startPos = startPos + 1;
String smsContent = "";
if (TP_UDHI == 1) {
//# Length of the letter -- No validation   You might want to go unsigned 
byte smsTotal = pduPayload[startPos + 4];
byte smsIdx = pduPayload[startPos + 5];
startPos = startPos + 6;
smsContent = " Length of the letter (" + byteToHex(smsIdx) + "/" + byteToHex(smsTotal) + "}";
smsContent = new String(smsContent.getBytes("gbk"));
smsPayloadLen = smsPayloadLen - 6;
}
byte[] smsPayload = new byte[pduPayload.length - startPos];
System.arraycopy(pduPayload, startPos, smsPayload, 0, pduPayload.length - startPos);
if (mTP_DCS == 0) {
//#7 A coding -- The authenticated 
smsPayloadLen = (smsPayloadLen * 7 + 7) / 8;
int asciiData = 0;
int lastByteRemain = 0;
for (int i = 0; i < smsPayloadLen; i++) {
asciiData = asciiData + ((smsPayload[i] & 0x0FF) << lastByteRemain);
smsContent = smsContent + (char) ((asciiData & 0x0FF) & 0x7f);
asciiData = asciiData >> 7;
lastByteRemain = lastByteRemain + 1;
if (lastByteRemain >= 7) {
smsContent = smsContent + (char) ((asciiData & 0x0FF) & 0x7f);
asciiData = asciiData >> 7;
lastByteRemain = lastByteRemain - 7;
}
}
} else if (mTP_DCS == 8) {
//# UCS-2 -- The authenticated   Normally resolvable 
for (int i = 0; i < smsPayloadLen; i = i + 2) {
int cc1 = (smsPayload[i] & 0x0FF) * 256;
int cc2 = smsPayload[i + 1] & 0x0FF;
smsContent = smsContent + (char) (cc1 + cc2);
}
}
return smsNumber + ":" + smsContent;
}

Related articles: