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

Class Encoder

source code

Known Subclasses:
mms_pdu.MMSEncoder

A WSP Data unit decoder

Static Methods [hide private]
list
encodeUintvar(uint)
Variable Length Unsigned Integer encoding algorithm
list
encodeTextString(string)
Encodes a "Text-string" value.
list
encodeShortInteger(integer)
Encodes the specified short-integer value
list
encodeLongInteger(integer)
Encodes a Long-integer value
list
encodeVersionValue(version)
Encodes the version-value.
list
encodeMediaType(contentType)
Encodes the specified MIME content type ("Media-type" value)
list
encodeParameter(parameterName, parameterValue, encodingVersion='1.2')
Binary-encodes the name of a parameter of (for example) a "Content-Type" header entry, taking into account the WSP short form of well-known parameter names, as specified in section 8.4.2.4 and table 38 of [5].
list
encodeTokenText(text)
From [5], section 8.4.2.1: Token-text = Token End-of-string
list
encodeIntegerValue(integer)
Encodes an integer value
 
encodeTextValue(text)
Stub for encoding Text-values; this is equivalent to encodeTextString
list
encodeNoValue(value=None)
Encodes a No-value, which is 0x00
list
encodeHeader(headerFieldName, headerValue)
Encodes a WSP header entry, and its value
list
encodeContentTypeValue(mediaType, parameters)
Encodes a content type, and its parameters
list
encodeConstrainedMedia(mediaType)
From [5], section 8.4.2.7: Constrained-media = Constrained-encoding It is encoded using values from the "Content Type Assignments" table.
list
encodeConstrainedEncoding(value)
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.
str
encodeExtensionMedia(mediaValue)
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
list
encodeContentGeneralForm(mediaType, parameters)
From [5], section 8.4.2.24: Content-general-form = Value-length Media-type
list
encodeValueLength(length)
Encodes the specified length value as a value length indicator
list
encodeShortLength(length)
From [5], section 8.4.2.2: Short-length = <Any octet 0-30>
list
encodeAcceptValue(acceptValue)
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>
Method Details [hide private]

encodeUintvar(uint)
Static Method

 

Variable Length Unsigned Integer encoding algorithm

This binary-encodes the given unsigned integer number as specified in section 8.1.2 of [5]. Basically, each encoded byte has the following structure:
   [0][ Payload ]
    |   ^^^^^^^
    |   7 bits (actual data)
    |
   Continue bit
The uint is split into 7-bit segments, and the "continue bit" of each used octet is set to '1' to indicate more is to follow; the last used octet's "continue bit" is set to 0.
Returns: list
the binary-encoded Uintvar, as a list of byte values

encodeTextString(string)
Static Method

 

Encodes a "Text-string" value.

This follows the basic encoding rules specified in [5], section 8.4.2.1
Parameters:
  • string (str) - The text string to encode
Returns: list
the null-terminated, binary-encoded version of the specified Text-string, as a list of byte values

encodeShortInteger(integer)
Static Method

 

Encodes the specified short-integer value

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.
Parameters:
  • integer (int) - The short-integer value to encode
Returns: list
The encoded short integer, as a list of byte values
Raises:
  • EncodeError - Not a valid short-integer; the integer must be in the range of 0-127

encodeLongInteger(integer)
Static Method

 

Encodes a Long-integer value

The encoding for a long integer is specified in [5], section 8.4.2.1; for a description of this encoding scheme, see wsp.Decoder.decodeLongIntger().

Basically: From [5], section 8.4.2.2: Long-integer = Short-length Multi-octet-integer Short-length = <Any octet 0-30>
Parameters:
  • integer (int) - The integer value to encode
Returns: list
The encoded Long-integer, as a sequence of byte values
Raises:

encodeVersionValue(version)
Static Method

 

Encodes the version-value. From [5], section 8.4.2.3: Version-value = Short-integer | Text-string

Example: An MMS version of "1.0" consists of a major version of 1 and a minor version of 0, and would be encoded as 0x90. However, a version of "1.2.4" would be encoded as the Text-string "1.2.4".
Parameters:
  • version (str) - The version number to encode, e.g. "1.0"
Returns: list
the encoded version value, as a list of byte values
Raises:
  • TypeError - The specified version value was not of type str

encodeMediaType(contentType)
Static Method

 

Encodes the specified MIME content type ("Media-type" value)

From [5], section 8.2.4.24: Media-type = (Well-known-media | Extension-Media) *(Parameter)

"Well-known-media" takes into account the WSP short form of well-known content types, as specified in section 8.4.2.24 and table 40 of [5].
Parameters:
  • contentType (str) - The MIME content type to encode
Returns: list
The binary-encoded content type, as a list of (integer) byte values

encodeParameter(parameterName, parameterValue, encodingVersion='1.2')
Static Method

 

Binary-encodes the name of a parameter of (for example) a "Content-Type" header entry, taking into account the WSP short form 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: Parameter = Typed-parameter | Untyped-parameter Typed-parameter = Well-known-parameter-token Typed-value Untyped-parameter = Token-text Untyped-value Untyped-value = Integer-value | Text-value
Parameters:
  • parameterName (str) - The name of the parameter to encode
  • parameterValue (str or int) - The value of the parameter
  • 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 38 in [5] for details).
Returns: list
The binary-encoded parameter name, as a list of (integer) byte values
Raises:
  • ValueError - The specified encoding version is invalid.

encodeTokenText(text)
Static Method

 
From [5], section 8.4.2.1: Token-text = Token End-of-string
Returns: list
The encoded token string, as a list of byte values
Raises:
  • EncodeError - Specified text cannot be encoding as a token

encodeIntegerValue(integer)
Static Method

 

Encodes an integer value

From [5], section 8.4.2.3: Integer-Value = Short-integer | Long-integer

This function will first try to encode the specified integer value into a short-integer, and failing that, will encode into a long-integer value.
Parameters:
  • integer (int) - The integer to encode
Returns: list
The encoded integer value, as a list of byte values
Raises:
  • EncodeError - The <integer> parameter is not of type int

encodeNoValue(value=None)
Static Method

 
Encodes a No-value, which is 0x00
Parameters:
  • value - This value is ignored; it is present so that this method complies with the format of the other encode methods.
Returns: list
A list containing a single "No-value", which is 0x00

Note: This function mainly exists for use by automatically-selected encoding routines (see encodeParameter() for an example.

encodeHeader(headerFieldName, headerValue)
Static Method

 

Encodes a WSP header entry, and its value

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: list
The encoded header, and its value, as a sequence of byte values
Notes:
  • "Shift-sequence" encoding has not been implemented
  • Currently, almost all header values are encoded as text-strings

encodeContentTypeValue(mediaType, parameters)
Static Method

 

Encodes a content type, and its parameters

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: list
The encoded Content-type-value (including parameters, if any), as a sequence of bytes

encodeConstrainedMedia(mediaType)
Static Method

 
From [5], section 8.4.2.7: Constrained-media = Constrained-encoding It is encoded using values from the "Content Type Assignments" table.
Parameters:
  • mediaType (str) - The media type to encode
Returns: list
The encoded media type, as a sequence of bytes
Raises:
  • EncodeError - Media value is unsuitable for Constrained-encoding

encodeConstrainedEncoding(value)
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.
Parameters:
  • value (int or str) - The value to encode
Returns: list
The encoded constrained-encoding token value, as a sequence of bytes
Raises:
  • EncodeError - <value> cannot be encoded as a Constrained-encoding sequence

encodeExtensionMedia(mediaValue)
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
Parameters:
  • mediaValue (str) - The media value (string) to encode
Returns: str
The encoded media type value, as a sequence of bytes
Raises:
  • EncodeError - The value cannot be encoded as TEXT; probably it starts with/contains an invalid character

encodeContentGeneralForm(mediaType, parameters)
Static Method

 
From [5], section 8.4.2.24: Content-general-form = Value-length Media-type
Returns: list
The encoded Content-general-form, as a sequence of bytes
Notes:
  • Used in decoding Content-type fields and their parameters; see decodeContentTypeValue
  • Used by decodeContentTypeValue()

encodeValueLength(length)
Static Method

 

Encodes the specified length value as a value length indicator

"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: list
The encoded value length indicator, as a sequence of bytes
Raises:

encodeShortLength(length)
Static Method

 
From [5], section 8.4.2.2: Short-length = <Any octet 0-30>
Returns: list
The encoded short-length, as a sequence of bytes
Raises:
  • EmcodeError - The specified <length> cannot be encoded as a short-length value; it is not in octet range 0-30.

encodeAcceptValue(acceptValue)
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>
Parameters:
  • acceptValue (str) - The Accept-value to encode (media/content type)
Returns: list
The encoded Accept-value, as a sequence of bytes
Raises:

Note: This implementation does not currently support encoding of "Accept-parameters".