Package mms :: Module wsp_pdu :: Class Decoder
[hide private]
[frames] | no frames]

Class Decoder

source code

Known Subclasses:
mms_pdu.MMSDecoder

A WSP Data unit decoder

Static Methods [hide private]
int
decodeUint8(byteIter)
Decodes an 8-bit unsigned integer from the byte pointed to by byteIter.next()
int
decodeUintvar(byteIter)
Decodes the variable-length unsigned integer starting at the byte pointed to by byteIter.next()
int
decodeShortInteger(byteIter)
Decodes the short-integer value starting at the byte pointed to by byteIter.next().
int
decodeShortIntegerFromByte(byte)
Decodes the short-integer value contained in the specified byte value
int
decodeLongInteger(byteIter)
Decodes the long integer value starting at the byte pointed to by byteIter.next().
str
decodeTextString(byteIter)
Decodes the null-terminated, binary-encoded string value starting at the byte pointed to by dataIter.next().
str
decodeQuotedString(byteIter)
From [5], section 8.4.2.1: Quoted-string = <Octet 34> *TEXT End-of-string The TEXT encodes an RFC2616 Quoted-string with the enclosing quotation-marks <"> removed
str or int
decodeTokenText(byteIter)
From [5], section 8.4.2.1: Token-text = Token End-of-string
str
decodeExtensionMedia(byteIter)
From [5], section 8.4.2.1: Extension-media = *TEXT End-of-string This encoding is used for media values, which have no well-known binary encoding
str or int
decodeConstrainedEncoding(byteIter)
Constrained-encoding = Extension-Media --or-- Short-integer This encoding is used for token values, which have no well-known binary encoding, or when the assigned number of the well-known encoding is small enough to fit into Short-integer.
int
decodeShortLength(byteIter)
From [5], section 8.4.2.2: Short-length = <Any octet 0-30>
int
decodeValueLength(byteIter)
Decodes the value length indicator starting at the byte pointed to by byteIter.next().
int
decodeIntegerValue(byteIter)
From [5], section 8.4.2.3: Integer-Value = Short-integer | Long-integer
tuple
decodeContentTypeValue(byteIter)
Decodes an encoded content type value.
str
decodeWellKnownMedia(byteIter)
From [5], section 8.4.2.7: Well-known-media = Integer-value It is encoded using values from the "Content Type Assignments" table (see [5], table 40).
str
decodeMediaType(byteIter)
From [5], section 8.2.4.24: Media-type = (Well-known-media | Extension-Media) *(Parameter)
str
decodeConstrainedMedia(byteIter)
From [5], section 8.4.2.7: Constrained-media = Constrained-encoding It is encoded using values from the "Content Type Assignments" table.
tuple
decodeContentGeneralForm(byteIter)
From [5], section 8.4.2.24: Content-general-form = Value-length Media-type
tuple
decodeParameter(byteIter)
From [5], section 8.4.2.4: Parameter = Typed-parameter | Untyped-parameter
tuple
decodeTypedParameter(byteIter)
From [5], section 8.4.2.4: Typed-parameter = Well-known-parameter-token Typed-value The actual expected type of the value is implied by the well-known parameter.
tuple
decodeUntypedParameter(byteIter)
From [5], section 8.4.2.4: Untyped-parameter = Token-text Untyped-value The type of the value is unknown, but it shall be encoded as an integer, if that is possible.
int or str
decodeUntypedValue(byteIter)
From [5], section 8.4.2.4: Untyped-value = Integer-value | Text-value
tuple
decodeWellKnownParameter(byteIter, encodingVersion='1.2')
Decodes the name and expected value type of a parameter of (for example) a "Content-Type" header entry, taking into account the WSP short form (assigned numbers) of well-known parameter names, as specified in section 8.4.2.4 and table 38 of [5].
str
decodeTypedValue(byteIter)
From [5], section 8.4.2.4: Typed-value = Compact-value | Text-value In addition to the expected type, there may be no value.
str or int
decodeCompactValue(byteIter)
From [5], section 8.4.2.4: Compact-value = Integer-value | Date-value | Delta-seconds-value | Q-value | Version-value | Uri-value
str
decodeDateValue(byteIter)
From [5], section 8.4.2.3: Date-value = Long-integer The encoding of dates shall be done in number of seconds from 1970-01-01, 00:00:00 GMT.
int
decodeDeltaSecondsValue(byteIter)
From [5], section 8.4.2.3: Delta-seconds-value = Integer-value
float
decodeQValue(byteIter)
From [5], section 8.4.2.1: The encoding is the same as in Uintvar-integer, but with restricted size.
str
decodeVersionValue(byteIter)
Decodes the version-value.
 
decodeUriValue(byteIter)
Stub for Uri-value decoding; this is a wrapper to decodeTextString
str
decodeTextValue(byteIter)
Stub for Parameter Text-value decoding.
int
decodeNoValue(byteIter)
Basically verifies that the byte pointed to by byteIter.next() is 0x00.
str
decodeAcceptValue(byteIter)
From [5], section 8.4.2.7: Accept-value = Constrained-media | Accept-general-form Accept-general-form = Value-length Media-range [Accept-parameters] Media-range = (Well-known-media | Extension-Media) *(Parameter) Accept-parameters = Q-token Q-value *(Accept-extension) Accept-extension = Parameter Q-token = <Octet 128>
tuple
decodePragmaValue(byteIter)
Defined in [5], section 8.4.2.38: Pragma-value = No-cache | (Value-length Parameter)
 
decodeWellKnownCharset(byteIter)
From [5], section 8.4.2.8: Well-known-charset = Any-charset | Integer-value It is encoded using values from "Character Set Assignments" table.
tuple
decodeWellKnownHeader(byteIter)
From [5], section 8.4.2.6: Well-known-header = Well-known-field-name Wap-value Well-known-field-name = Short-integer Wap-value = <many different headers value, most not implemented>
tuple
decodeApplicationHeader(byteIter)
From [5], section 8.4.2.6: Application-header = Token-text Application-specific-value
tuple
decodeHeader(byteIter)
Decodes a WSP header entry
Method Details [hide private]

decodeUint8(byteIter)
Static Method

 
Decodes an 8-bit unsigned integer from the byte pointed to by byteIter.next()
Parameters:
  • byteIter (iter) - an iterator over a sequence of bytes
Returns: int
the decoded 8-bit unsigned integer

Note: this function will move the iterator passed as byteIter one byte forward.

decodeUintvar(byteIter)
Static Method

 

Decodes the variable-length unsigned integer starting at the byte pointed to by byteIter.next()

See wsp.Encoder.encodeUintvar() for a detailed description of the encoding scheme used for Uintvar sequences.
Parameters:
  • byteIter (iter) - an iterator over a sequence of bytes
Returns: int
the decoded unsigned integer

Note: this function will move the iterator passed as byteIter to the last octet in the uintvar sequence; thus, after calling this, that iterator's next() function will return the first byte afterthe uintvar sequence.

decodeShortInteger(byteIter)
Static Method

 

Decodes the short-integer value starting at the byte pointed to by byteIter.next().

The encoding for a long integer is specified in [5], section 8.4.2.1: Short-integer = OCTET Integers in range 0-127 shall be encoded as a one octet value with the most significant bit set to one (1xxx xxxx) and with the value in the remaining least significant bits.
Returns: int
The decoded short integer
Raises:
  • DecodeError - Not a valid short-integer; the most significant isn't set to 1. byteIter will not be modified if this is raised

decodeShortIntegerFromByte(byte)
Static Method

 
Decodes the short-integer value contained in the specified byte value
Parameters:
  • byte (int) - the byte value to decode
Returns: int
The decoded short integer
Raises:
  • DecodeError - Not a valid short-integer; the most significant isn't set to 1.

decodeLongInteger(byteIter)
Static Method

 

Decodes the long integer value starting at the byte pointed to by byteIter.next().

The encoding for a long integer is specified in [5], section 8.4.2.1, and follows the form:
Long-integer = [Short-length] [Multi-octet-integer]
                   ^^^^^^     ^^^^^^^^^^^^^^^^^^^^^
                   1 byte     <Short-length> bytes

The Short-length indicates the length of the Multi-octet-integer.
Parameters:
  • byteIter (iter) - an iterator over a sequence of bytes
Returns: int
The decoded long integer
Raises:
  • DecodeError - The byte pointed to by byteIter.next() does not indicate the start of a valid long-integer sequence (short-length is invalid). If this is raised, the iterator passed as byteIter will not be modified.

Note: If this function returns successfully, it will move the iterator passed as byteIter to the last octet in the encoded long integer sequence; thus, after calling this, that iterator's next() function will return the first byte afterthe encoded long integer sequence.

decodeTextString(byteIter)
Static Method

 

Decodes the null-terminated, binary-encoded string value starting at the byte pointed to by dataIter.next().

This follows the basic encoding rules specified in [5], section 8.4.2.1
Parameters:
  • byteIter (iter) - an iterator over a sequence of bytes
Returns: str
The decoded text string

Note: this function will move the iterator passed as byteIter to the last octet in the encoded string sequence; thus, after calling this, that iterator's next() function will return the first byte afterthe encoded string sequence.

decodeQuotedString(byteIter)
Static Method

 
From [5], section 8.4.2.1: Quoted-string = <Octet 34> *TEXT End-of-string The TEXT encodes an RFC2616 Quoted-string with the enclosing quotation-marks <"> removed
Returns: str
The decoded text string

decodeTokenText(byteIter)
Static Method

 
From [5], section 8.4.2.1: Token-text = Token End-of-string
Returns: str or int
The token string if successful, or the byte that was read if not
Raises:
  • DecodeError - invalid token; in this case, byteIter is not modified

decodeExtensionMedia(byteIter)
Static Method

 
From [5], section 8.4.2.1: Extension-media = *TEXT End-of-string This encoding is used for media values, which have no well-known binary encoding
Returns: str
The decoded media type value
Raises:
  • DecodeError - The TEXT started with an invalid character. byteIter is not modified if this happens.

decodeConstrainedEncoding(byteIter)
Static Method

 
Constrained-encoding = Extension-Media --or-- Short-integer This encoding is used for token values, which have no well-known binary encoding, or when the assigned number of the well-known encoding is small enough to fit into Short-integer.
Returns: str or int
The decoding constrained-encoding token value

decodeShortLength(byteIter)
Static Method

 
From [5], section 8.4.2.2: Short-length = <Any octet 0-30>
Returns: int
The decoded short-length
Raises:
  • DecodeError - The byte is not a valid short-length value; it is not in octet range 0-30. In this case, the iterator passed as byteIter is not modified.

Note: If this function returns successfully, the iterator passed as byteIter is moved one byte forward.

decodeValueLength(byteIter)
Static Method

 

Decodes the value length indicator starting at the byte pointed to by byteIter.next().

"Value length" is used to indicate the length of a value to follow, as used in the Content-Type header in the MMS body, for example.

The encoding for a value length indicator is specified in [5], section 8.4.2.2, and follows the form:
Value-length = [Short-length]  --or--  [Length-quote] [Length]
                   ^^^^^^                  ^^^^^^      ^^^^^^
                   1 byte                  1 byte      x bytes
              <Any octet 0-30>          <Octet 31>   Uintvar-integer
Returns: int
The decoded value length indicator
Raises:
  • DecodeError - The ValueLength could not be decoded. If this happens, byteIter is not modified.

decodeIntegerValue(byteIter)
Static Method

 
From [5], section 8.4.2.3: Integer-Value = Short-integer | Long-integer
Returns: int
The decoded integer value
Raises:
  • DecodeError - The sequence of bytes starting at byteIter.next() does not contain a valid integervalue. If this is raised, the iterator passed as byteIter is not modified.

Note: If successful, this function will move the iterator passed as byteIter to the last octet in the integer value sequence; thus, after calling this, that iterator's next() function will return the first byte afterthe integer value sequence.

decodeContentTypeValue(byteIter)
Static Method

 

Decodes an encoded content type value.

From [5], section 8.4.2.24: Content-type-value = Constrained-media | Content-general-form

The short form of the Content-type-value MUST only be used when the well-known media is in the range of 0-127 or a text string. In all other cases the general form MUST be used.
Returns: tuple
The media type (content type), and a dictionary of parameters to this content type (which is empty if there are no parameters). This parameter dictionary is in the format: {<str:parameter_name>: <str/int/float:parameter_value>}. The final returned tuple is in the format: (<str:media_type>, <dict:parameter_dict>)

decodeWellKnownMedia(byteIter)
Static Method

 
From [5], section 8.4.2.7: Well-known-media = Integer-value It is encoded using values from the "Content Type Assignments" table (see [5], table 40).
Parameters:
  • byteIter (iter) - an iterator over a sequence of bytes
Returns: str
the decoded MIME content type name
Raises:
  • DecodeError - This is raised if the integer value representing the well-known media type cannot be decoded correctly, or the well-known media type value could not be found in the table of assigned content types. If this exception is raised, the iterator passed as byteIter is not modified.

Note: If successful, this function will move the iterator passed as byteIter to the last octet in the content type value sequence; thus, after calling this, that iterator's next() function will return the first byte afterthe content type value sequence.

decodeMediaType(byteIter)
Static Method

 
From [5], section 8.2.4.24: Media-type = (Well-known-media | Extension-Media) *(Parameter)
Parameters:
  • byteIter (iter) - an iterator over a sequence of bytes
Returns: str
The decoded media type

Note: Used by decodeContentGeneralForm()

decodeConstrainedMedia(byteIter)
Static Method

 
From [5], section 8.4.2.7: Constrained-media = Constrained-encoding It is encoded using values from the "Content Type Assignments" table.
Returns: str
The decoded media type
Raises:

decodeContentGeneralForm(byteIter)
Static Method

 
From [5], section 8.4.2.24: Content-general-form = Value-length Media-type
Returns: tuple
The media type (content type), and a dictionary of parameters to this content type (which is empty if there are no parameters). This parameter dictionary is in the format: {<str:parameter_name>: <str/int/float:parameter_value>}. The final returned tuple is in the format: (<str:media_type>, <dict:parameter_dict>)
Notes:
  • Used in decoding Content-type fields and their parameters; see decodeContentTypeValue
  • Used by decodeContentTypeValue()

decodeParameter(byteIter)
Static Method

 
From [5], section 8.4.2.4: Parameter = Typed-parameter | Untyped-parameter
Returns: tuple
The name of the parameter, and its value, in the format: (<parameter name>, <parameter value>)

decodeTypedParameter(byteIter)
Static Method

 
From [5], section 8.4.2.4: Typed-parameter = Well-known-parameter-token Typed-value The actual expected type of the value is implied by the well-known parameter.
Returns: tuple
The name of the parameter, and its value, in the format: (<parameter name>, <parameter value>)

Note: This is used in decoding parameters; see decodeParameter

decodeUntypedParameter(byteIter)
Static Method

 
From [5], section 8.4.2.4: Untyped-parameter = Token-text Untyped-value The type of the value is unknown, but it shall be encoded as an integer, if that is possible.
Returns: tuple
The name of the parameter, and its value, in the format: (<parameter name>, <parameter value>)

Note: This is used in decoding parameters; see decodeParameter

decodeUntypedValue(byteIter)
Static Method

 
From [5], section 8.4.2.4: Untyped-value = Integer-value | Text-value
Returns: int or str
The decoded untyped-value

Note: This is used in decoding parameter values; see decodeUntypedParameter

decodeWellKnownParameter(byteIter, encodingVersion='1.2')
Static Method

 

Decodes the name and expected value type of a parameter of (for example) a "Content-Type" header entry, taking into account the WSP short form (assigned numbers) of well-known parameter names, as specified in section 8.4.2.4 and table 38 of [5].

From [5], section 8.4.2.4: Well-known-parameter-token = Integer-value The code values used for parameters are specified in [5], table 38
Parameters:
  • encodingVersion (str) - The WSP encoding version to use. This defaults to "1.2", but may be "1.1", "1.2", "1.3" or "1.4" (see table 39 in [5] for details).
Returns: tuple
the decoded parameter name, and its expected value type, in the format (<parameter name>, <expected type>)
Raises:
  • ValueError - The specified encoding version is invalid.
  • DecodeError - This is raised if the integer value representing the well-known parameter name cannot be decoded correctly, or the well-known paramter token value could not be found in the table of assigned content types. If this exception is raised, the iterator passed as byteIter is not modified.

decodeTypedValue(byteIter)
Static Method

 
From [5], section 8.4.2.4: Typed-value = Compact-value | Text-value In addition to the expected type, there may be no value. If the value cannot be encoded using the expected type, it shall be encoded as text.
Returns: str
The decoded Parameter Typed-value

Note: This is used in decoding parameters, see decodeParameter()

decodeCompactValue(byteIter)
Static Method

 
From [5], section 8.4.2.4: Compact-value = Integer-value | Date-value | Delta-seconds-value | Q-value | Version-value | Uri-value
Returns: str or int
The decoded Compact-value (this is specific to the parameter type
Raises:
  • DecodeError - Failed to decode the Parameter Compact-value; if this happens, byteIter is unmodified

Note: This is used in decoding parameters, see decodeTypeValue()

decodeDateValue(byteIter)
Static Method

 
From [5], section 8.4.2.3: Date-value = Long-integer The encoding of dates shall be done in number of seconds from 1970-01-01, 00:00:00 GMT.
Returns: str
The date, in a format such as: Tue Nov 27 16:12:21 2007
Raises:
  • DecodeError - This method uses decodeLongInteger, and thus raises this under the same conditions.

decodeDeltaSecondsValue(byteIter)
Static Method

 
From [5], section 8.4.2.3: Delta-seconds-value = Integer-value
Returns: int
the decoded delta-seconds-value
Raises:
  • DecodeError - This method uses decodeIntegerValue, and thus raises this under the same conditions.

decodeQValue(byteIter)
Static Method

 
From [5], section 8.4.2.1: The encoding is the same as in Uintvar-integer, but with restricted size. When quality factor 0 and quality factors with one or two decimal digits are encoded, they shall be multiplied by 100 and incremented by one, so that they encode as a one-octet value in range 1-100, ie, 0.1 is encoded as 11 (0x0B) and 0.99 encoded as 100 (0x64). Three decimal quality factors shall be multiplied with 1000 and incremented by 100, and the result shall be encoded as a one-octet or two-octet uintvar, eg, 0.333 shall be encoded as 0x83 0x31. Quality factor 1 is the default value and shall never be sent.
Returns: float
The decode quality factor (Q-value)

decodeVersionValue(byteIter)
Static Method

 
Decodes the version-value. From [5], section 8.4.2.3: Version-value = Short-integer | Text-string
Returns: str
the decoded version value in the format, usually in the format: "<major_version>.<minor_version>"

decodeTextValue(byteIter)
Static Method

 

Stub for Parameter Text-value decoding. From [5], section 8.4.2.3: Text-value = No-value | Token-text | Quoted-string

This is used when decoding parameter values; see decodeTypedValue()
Returns: str
The decoded Parameter Text-value

decodeNoValue(byteIter)
Static Method

 
Basically verifies that the byte pointed to by byteIter.next() is 0x00.
Returns: int
No-value, which is 0x00
Raises:
  • DecodeError - If 0x00 is not found; byteIter is not modified if this is raised.

Note: If successful, this function will move byteIter one byte forward.

decodeAcceptValue(byteIter)
Static Method

 
From [5], section 8.4.2.7: Accept-value = Constrained-media | Accept-general-form Accept-general-form = Value-length Media-range [Accept-parameters] Media-range = (Well-known-media | Extension-Media) *(Parameter) Accept-parameters = Q-token Q-value *(Accept-extension) Accept-extension = Parameter Q-token = <Octet 128>
Returns: str
the decoded Accept-value (media/content type)
Raises:
  • DecodeError - The decoding failed. byteIter will not be modified in this case.

Note: most of these things are currently decoded, but discarded (e.g accept-parameters); we only return the media type

decodePragmaValue(byteIter)
Static Method

 

Defined in [5], section 8.4.2.38: Pragma-value = No-cache | (Value-length Parameter)

From [5], section 8.4.2.15: No-cache = <Octet 128>
Returns: tuple
the decoded Pragma-value, in the format: (<parameter name>, <parameter value>)
Raises:
  • DecodeError - The decoding failed. byteIter will not be modified in this case.

decodeWellKnownCharset(byteIter)
Static Method

 
From [5], section 8.4.2.8: Well-known-charset = Any-charset | Integer-value It is encoded using values from "Character Set Assignments" table. Any-charset = <Octet 128> Equivalent to the special RFC2616 charset value "*"

decodeWellKnownHeader(byteIter)
Static Method

 
From [5], section 8.4.2.6: Well-known-header = Well-known-field-name Wap-value Well-known-field-name = Short-integer Wap-value = <many different headers value, most not implemented>
Returns: tuple
The header name, and its value, in the format: (<str:header_name>, <str:header_value>)

To Do: Currently, "Wap-value" is decoded as a Text-string in most cases

decodeApplicationHeader(byteIter)
Static Method

 

From [5], section 8.4.2.6: Application-header = Token-text Application-specific-value

From [4], section 7.1: Application-header = Token-text Application-specific-value Application-specific-value = Text-string
Returns: tuple
The application-header, and its value, in the format: (<str:application_header>, <str:application_specific_value>)
Notes:
  • This is used when decoding generic WSP headers; see decodeHeader().
  • We follow [4], and decode the "Application-specific-value" as a Text-string

decodeHeader(byteIter)
Static Method

 

Decodes a WSP header entry

From [5], section 8.4.2.6: Header = Message-header | Shift-sequence Message-header = Well-known-header | Application-header Well-known-header = Well-known-field-name Wap-value Application-header = Token-text Application-specific-value
Returns: tuple
The decoded headername, and its value, in the format: (<str:header_name>, <str:header_value>)
Notes:
  • "Shift-sequence" encoding has not been implemented
  • Currently, almost all header values are treated as text-strings