FileDocCategorySizeDatePackage
WspTypeDecoder.javaAPI DocAndroid 5.1 API31600Thu Mar 12 22:22:54 GMT 2015com.android.internal.telephony

WspTypeDecoder

public class WspTypeDecoder extends Object
Implement the WSP data type decoder.
hide

Fields Summary
private static final int
WAP_PDU_SHORT_LENGTH_MAX
private static final int
WAP_PDU_LENGTH_QUOTE
public static final int
PDU_TYPE_PUSH
public static final int
PDU_TYPE_CONFIRMED_PUSH
private static final HashMap
WELL_KNOWN_MIME_TYPES
private static final HashMap
WELL_KNOWN_PARAMETERS
public static final int
PARAMETER_ID_X_WAP_APPLICATION_ID
private static final int
Q_VALUE
public static final String
CONTENT_TYPE_B_PUSH_CO
public static final String
CONTENT_TYPE_B_MMS
public static final String
CONTENT_TYPE_B_PUSH_SYNCML_NOTI
byte[]
mWspData
int
mDataLength
long
mUnsigned32bit
String
mStringValue
HashMap
mContentParameters
Constructors Summary
public WspTypeDecoder(byte[] pdu)


       
        mWspData = pdu;
    
Methods Summary
public booleandecodeConstrainedEncoding(int startIndex)
Decode the "Constrained-encoding" type for WSP pdu

param
startIndex The starting position of the "Constrained-encoding" in this pdu
return
false when error(not a Constrained-encoding) occur return value can be retrieved first by getValueString() and second by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        if (decodeShortInteger(startIndex) == true) {
            mStringValue = null;
            return true;
        }
        return decodeExtensionMedia(startIndex);
    
public booleandecodeContentLength(int startIndex)
Decode the "Content length" type for WSP pdu

param
startIndex The starting position of the "Content length" in this pdu
return
false when error(not a Content length) occur return value can be retrieved by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        return decodeIntegerValue(startIndex);
    
public booleandecodeContentLocation(int startIndex)
Decode the "Content location" type for WSP pdu

param
startIndex The starting position of the "Content location" in this pdu
return
false when error(not a Content location) occur return value can be retrieved by getValueString() method length of data in pdu can be retrieved by getDecodedDataLength() method

        return decodeTextString(startIndex);
    
public booleandecodeContentType(int startIndex)
Decode the "Content-type" type for WSP pdu

param
startIndex The starting position of the "Content-type" in this pdu
return
false when error(not a Content-type) occurs If a content type exists in the headers (either as inline string, or as well-known value), getValueString() will return it. If a 'well known value' is encountered that cannot be mapped to a string mime type, getValueString() will return null, and getValue32() will return the unknown content type value. length of data in pdu can be retrieved by getDecodedDataLength() method Any content type parameters will be accessible via getContentParameters()

        int mediaPrefixLength;
        mContentParameters = new HashMap<String, String>();

        try {
            if (decodeValueLength(startIndex) == false) {
                boolean found = decodeConstrainedEncoding(startIndex);
                if (found) {
                    expandWellKnownMimeType();
                }
                return found;
            }
            int headersLength = (int) mUnsigned32bit;
            mediaPrefixLength = getDecodedDataLength();
            if (decodeIntegerValue(startIndex + mediaPrefixLength) == true) {
                mDataLength += mediaPrefixLength;
                int readLength = mDataLength;
                mStringValue = null;
                expandWellKnownMimeType();
                long wellKnownValue = mUnsigned32bit;
                String mimeType = mStringValue;
                if (readContentParameters(startIndex + mDataLength,
                        (headersLength - (mDataLength - mediaPrefixLength)), 0)) {
                    mDataLength += readLength;
                    mUnsigned32bit = wellKnownValue;
                    mStringValue = mimeType;
                    return true;
                }
                return false;
            }
            if (decodeExtensionMedia(startIndex + mediaPrefixLength) == true) {
                mDataLength += mediaPrefixLength;
                int readLength = mDataLength;
                expandWellKnownMimeType();
                long wellKnownValue = mUnsigned32bit;
                String mimeType = mStringValue;
                if (readContentParameters(startIndex + mDataLength,
                        (headersLength - (mDataLength - mediaPrefixLength)), 0)) {
                    mDataLength += readLength;
                    mUnsigned32bit = wellKnownValue;
                    mStringValue = mimeType;
                    return true;
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            //something doesn't add up
            return false;
        }
        return false;
    
public booleandecodeExtensionMedia(int startIndex)
Decode the "Extension-media" type for WSP PDU.

param
startIndex The starting position of the "Extension-media" in this PDU.
return
false on error, such as if there is no Extension-media at startIndex. Side-effects: updates stringValue (available with getValueString()), which will be null on error. The length of the data in the PDU is available with getValue32(), 0 on error.

        int index = startIndex;
        mDataLength = 0;
        mStringValue = null;
        int length = mWspData.length;
        boolean rtrn = index < length;

        while (index < length && mWspData[index] != 0) {
            index++;
        }

        mDataLength = index - startIndex + 1;
        mStringValue = new String(mWspData, startIndex, mDataLength - 1);

        return rtrn;
    
public booleandecodeIntegerValue(int startIndex)
Decode the "Integer-Value" type for WSP pdu

param
startIndex The starting position of the "Integer-Value" in this pdu
return
false when error(not a Integer-Value) occur return value can be retrieved by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        if (decodeShortInteger(startIndex) == true) {
            return true;
        }
        return decodeLongInteger(startIndex);
    
public booleandecodeLongInteger(int startIndex)
Decode the "Long-integer" type for WSP pdu

param
startIndex The starting position of the "Long-integer" in this pdu
return
false when error(not a Long-integer) occur return value can be retrieved by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        int lengthMultiOctet = mWspData[startIndex] & 0xff;

        if (lengthMultiOctet > WAP_PDU_SHORT_LENGTH_MAX) {
            return false;
        }
        mUnsigned32bit = 0;
        for (int i = 1; i <= lengthMultiOctet; i++) {
            mUnsigned32bit = (mUnsigned32bit << 8) | (mWspData[startIndex + i] & 0xff);
        }
        mDataLength = 1 + lengthMultiOctet;
        return true;
    
private booleandecodeNoValue(int startIndex)
Check if the next byte is No-Value

param
startIndex The starting position of the "Content length" in this pdu
return
true if and only if the next byte is 0x00

        if (mWspData[startIndex] == 0) {
            mDataLength = 1;
            return true;
        } else {
            return false;
        }
    
public booleandecodeShortInteger(int startIndex)
Decode the "Short-integer" type for WSP pdu

param
startIndex The starting position of the "Short-integer" in this pdu
return
false when error(not a Short-integer) occur return value can be retrieved by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        if ((mWspData[startIndex] & 0x80) == 0) {
            return false;
        }
        mUnsigned32bit = mWspData[startIndex] & 0x7f;
        mDataLength = 1;
        return true;
    
public booleandecodeTextString(int startIndex)
Decode the "Text-string" type for WSP pdu

param
startIndex The starting position of the "Text-string" in this pdu
return
false when error(not a Text-string) occur return value can be retrieved by getValueString() method length of data in pdu can be retrieved by getDecodedDataLength() method

        int index = startIndex;
        while (mWspData[index] != 0) {
            index++;
        }
        mDataLength = index - startIndex + 1;
        if (mWspData[startIndex] == 127) {
            mStringValue = new String(mWspData, startIndex + 1, mDataLength - 2);
        } else {
            mStringValue = new String(mWspData, startIndex, mDataLength - 1);
        }
        return true;
    
public booleandecodeTokenText(int startIndex)
Decode the "Token-text" type for WSP pdu

param
startIndex The starting position of the "Token-text" in this pdu
return
always true return value can be retrieved by getValueString() method length of data in pdu can be retrieved by getDecodedDataLength() method

        int index = startIndex;
        while (mWspData[index] != 0) {
            index++;
        }
        mDataLength = index - startIndex + 1;
        mStringValue = new String(mWspData, startIndex, mDataLength - 1);

        return true;
    
public booleandecodeUintvarInteger(int startIndex)
Decode the "Uintvar-integer" type for WSP pdu

param
startIndex The starting position of the "Uintvar-integer" in this pdu
return
false when error(not a Uintvar-integer) occur return value can be retrieved by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        int index = startIndex;

        mUnsigned32bit = 0;
        while ((mWspData[index] & 0x80) != 0) {
            if ((index - startIndex) >= 4) {
                return false;
            }
            mUnsigned32bit = (mUnsigned32bit << 7) | (mWspData[index] & 0x7f);
            index++;
        }
        mUnsigned32bit = (mUnsigned32bit << 7) | (mWspData[index] & 0x7f);
        mDataLength = index - startIndex + 1;
        return true;
    
public booleandecodeValueLength(int startIndex)
Decode the "Value-length" type for WSP pdu

param
startIndex The starting position of the "Value-length" in this pdu
return
false when error(not a Value-length) occur return value can be retrieved by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        if ((mWspData[startIndex] & 0xff) > WAP_PDU_LENGTH_QUOTE) {
            return false;
        }
        if (mWspData[startIndex] < WAP_PDU_LENGTH_QUOTE) {
            mUnsigned32bit = mWspData[startIndex];
            mDataLength = 1;
        } else {
            decodeUintvarInteger(startIndex + 1);
            mDataLength++;
        }
        return true;
    
public booleandecodeXWapApplicationId(int startIndex)
Decode the "X-Wap-Application-Id" type for WSP pdu

param
startIndex The starting position of the "X-Wap-Application-Id" in this pdu
return
false when error(not a X-Wap-Application-Id) occur return value can be retrieved first by getValueString() and second by getValue32() method length of data in pdu can be retrieved by getDecodedDataLength() method

        if (decodeIntegerValue(startIndex) == true) {
            mStringValue = null;
            return true;
        }
        return decodeTextString(startIndex);
    
public booleandecodeXWapContentURI(int startIndex)
Decode the "X-Wap-Content-URI" type for WSP pdu

param
startIndex The starting position of the "X-Wap-Content-URI" in this pdu
return
false when error(not a X-Wap-Content-URI) occur return value can be retrieved by getValueString() method length of data in pdu can be retrieved by getDecodedDataLength() method

        return decodeTextString(startIndex);
    
public booleandecodeXWapInitiatorURI(int startIndex)
Decode the "X-Wap-Initiator-URI" type for WSP pdu

param
startIndex The starting position of the "X-Wap-Initiator-URI" in this pdu
return
false when error(not a X-Wap-Initiator-URI) occur return value can be retrieved by getValueString() method length of data in pdu can be retrieved by getDecodedDataLength() method

        return decodeTextString(startIndex);
    
private voidexpandWellKnownMimeType()
Populate stringValue with the mime type corresponding to the value in unsigned32bit Sets unsigned32bit to -1 if stringValue is already populated

        if (mStringValue == null) {
            int binaryContentType = (int) mUnsigned32bit;
            mStringValue = WELL_KNOWN_MIME_TYPES.get(binaryContentType);
        } else {
            mUnsigned32bit = -1;
        }
    
public java.util.HashMapgetContentParameters()
Any parameters encountered as part of a decodeContentType() invocation.

return
a map of content parameters keyed by their names, or null if decodeContentType() has not been called If any unassigned well-known parameters are encountered, the key of the map will be 'unassigned/0x...', where '...' is the hex value of the unassigned parameter. If a parameter has No-Value the value will be null.

        return mContentParameters;
    
public intgetDecodedDataLength()
The data length of latest operation.

        return mDataLength;
    
public longgetValue32()
The 32-bits result of latest operation.

        return mUnsigned32bit;
    
public java.lang.StringgetValueString()
The String result of latest operation.

        return mStringValue;
    
private booleanreadContentParameters(int startIndex, int leftToRead, int accumulator)


        int totalRead = 0;

        if (leftToRead > 0) {
            byte nextByte = mWspData[startIndex];
            String value = null;
            String param = null;
            if ((nextByte & 0x80) == 0x00 && nextByte > 31) { // untyped
                decodeTokenText(startIndex);
                param = mStringValue;
                totalRead += mDataLength;
            } else { // typed
                if (decodeIntegerValue(startIndex)) {
                    totalRead += mDataLength;
                    int wellKnownParameterValue = (int) mUnsigned32bit;
                    param = WELL_KNOWN_PARAMETERS.get(wellKnownParameterValue);
                    if (param == null) {
                        param = "unassigned/0x" + Long.toHexString(wellKnownParameterValue);
                    }
                    // special case for the "Q" parameter, value is a uintvar
                    if (wellKnownParameterValue == Q_VALUE) {
                        if (decodeUintvarInteger(startIndex + totalRead)) {
                            totalRead += mDataLength;
                            value = String.valueOf(mUnsigned32bit);
                            mContentParameters.put(param, value);
                            return readContentParameters(startIndex + totalRead, leftToRead
                                                            - totalRead, accumulator + totalRead);
                        } else {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            }

            if (decodeNoValue(startIndex + totalRead)) {
                totalRead += mDataLength;
                value = null;
            } else if (decodeIntegerValue(startIndex + totalRead)) {
                totalRead += mDataLength;
                int intValue = (int) mUnsigned32bit;
                value = String.valueOf(intValue);
            } else {
                decodeTokenText(startIndex + totalRead);
                totalRead += mDataLength;
                value = mStringValue;
                if (value.startsWith("\"")) {
                    // quoted string, so remove the quote
                    value = value.substring(1);
                }
            }
            mContentParameters.put(param, value);
            return readContentParameters(startIndex + totalRead, leftToRead - totalRead,
                                            accumulator + totalRead);

        } else {
            mDataLength = accumulator;
            return true;
        }
    
public booleanseekXWapApplicationId(int startIndex, int endIndex)
Seek for the "X-Wap-Application-Id" field for WSP pdu

param
startIndex The starting position of seek pointer
param
endIndex Valid seek area end point
return
false when error(not a X-Wap-Application-Id) occur return value can be retrieved by getValue32()

        int index = startIndex;

        try {
            for (index = startIndex; index <= endIndex; ) {
                /**
                 * 8.4.1.1  Field name
                 * Field name is integer or text.
                 */
                if (decodeIntegerValue(index)) {
                    int fieldValue = (int) getValue32();

                    if (fieldValue == PARAMETER_ID_X_WAP_APPLICATION_ID) {
                        mUnsigned32bit = index + 1;
                        return true;
                    }
                } else {
                    if (!decodeTextString(index)) return false;
                }
                index += getDecodedDataLength();
                if (index > endIndex) return false;

                /**
                 * 8.4.1.2 Field values
                 * Value Interpretation of First Octet
                 * 0 - 30 This octet is followed by the indicated number (0 - 30)
                        of data octets
                 * 31 This octet is followed by a uintvar, which indicates the number
                 *      of data octets after it
                 * 32 - 127 The value is a text string, terminated by a zero octet
                        (NUL character)
                 * 128 - 255 It is an encoded 7-bit value; this header has no more data
                 */
                byte val = mWspData[index];
                if (0 <= val && val <= WAP_PDU_SHORT_LENGTH_MAX) {
                    index += mWspData[index] + 1;
                } else if (val == WAP_PDU_LENGTH_QUOTE) {
                    if (index + 1 >= endIndex) return false;
                    index++;
                    if (!decodeUintvarInteger(index)) return false;
                    index += getDecodedDataLength();
                } else if (WAP_PDU_LENGTH_QUOTE < val && val <= 127) {
                    if (!decodeTextString(index)) return false;
                    index += getDecodedDataLength();
                } else {
                    index++;
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            //seek application ID failed. WSP header might be corrupted
            return false;
        }
        return false;