Changeset d56a0bc for server


Ignore:
Timestamp:
Nov 6, 2010, 5:00:53 PM (13 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
8403f61
Parents:
4ec7caa
git-author:
Jessica B. Hamrick <jhamrick@…> (11/06/10 17:00:53)
git-committer:
Jessica B. Hamrick <jhamrick@…> (11/06/10 17:00:53)
Message:

Add toBinaryData functions in ipprequest.py; add ipplib.py for reference (unsupported)

Location:
server/lib
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/ipprequest.py

    r4ec7caa rd56a0bc  
    1010   
    1111    # various tags
    12     ZERO_NAME_LENGTH                  = 0X00
    13     OPERATION_ATTRIBUTES_TAG          = 0X01
    14     JOB_ATTRIBUTES_TAG                = 0X02
    15     END_OF_ATTRIBUTES_TAG             = 0X03
    16     PRINTER_ATTRIBUTES_TAG            = 0X04
    17     UNSUPPORTED_ATTRIBUTES_TAG        = 0X05
     12    ZERO_NAME_LENGTH                  = 0x00
     13    OPERATION_ATTRIBUTES_TAG          = 0x01
     14    JOB_ATTRIBUTES_TAG                = 0x02
     15    END_OF_ATTRIBUTES_TAG             = 0x03
     16    PRINTER_ATTRIBUTES_TAG            = 0x04
     17    UNSUPPORTED_ATTRIBUTES_TAG        = 0x05
    1818   
    1919    # "out of band" value tags
    20     UNSUPPORTED                       = 0X10
    21     DEFAULT                           = 0X11
    22     UNKNOWN                           = 0X12
    23     NO_VALUE                          = 0X13
     20    UNSUPPORTED                       = 0x10
     21    DEFAULT                           = 0x11
     22    UNKNOWN                           = 0x12
     23    NO_VALUE                          = 0x13
    2424   
    2525    # integer value tags
    26     GENERIC_INTEGER                   = 0X20
    27     INTEGER                           = 0X21
    28     BOOLEAN                           = 0X22
    29     ENUM                              = 0X23
     26    GENERIC_INTEGER                   = 0x20
     27    INTEGER                           = 0x21
     28    BOOLEAN                           = 0x22
     29    ENUM                              = 0x23
    3030
    3131    # octetstring value tags
    32     UNSPECIFIED_OCTETSTRING           = 0X30
    33     DATETIME                          = 0X31
    34     RESOLUTION                        = 0X32
    35     RANGEOFINTEGER                    = 0X33
    36     COLLECTION                        = 0X34
    37     TEXTWITHLANGUAGE                  = 0X35
    38     NAMEWITHLANGUAGE                  = 0X36
     32    UNSPECIFIED_OCTETSTRING           = 0x30
     33    DATETIME                          = 0x31
     34    RESOLUTION                        = 0x32
     35    RANGEOFINTEGER                    = 0x33
     36    COLLECTION                        = 0x34
     37    TEXTWITHLANGUAGE                  = 0x35
     38    NAMEWITHLANGUAGE                  = 0x36
    3939
    4040    # character-string value tags
    41     GENERIC_CHAR_STRING               = 0X40
    42     TEXTWITHOUTLANGUAGE               = 0X41
    43     NAMEWITHOUTLANGUAGE               = 0X42
    44     KEYWORD                           = 0X44
    45     URI                               = 0X45
    46     URISCHEME                         = 0X46
    47     CHARSET                           = 0X47
    48     NATURALLANGUAGE                   = 0X48
    49     MIMEMEDIATYPE                     = 0X49                                   
     41    GENERIC_CHAR_STRING               = 0x40
     42    TEXTWITHOUTLANGUAGE               = 0x41
     43    NAMEWITHOUTLANGUAGE               = 0x42
     44    KEYWORD                           = 0x44
     45    URI                               = 0x45
     46    URISCHEME                         = 0x46
     47    CHARSET                           = 0x47
     48    NATURALLANGUAGE                   = 0x48
     49    MIMEMEDIATYPE                     = 0x49                                   
    5050
    5151class IPPValue():
    5252    """
    5353    An IPP value consists of a tag and a value, and optionally, a name.
     54
     55    From RFC 2565:
     56     -----------------------------------------------
     57     |                   value-tag                 |   1 byte
     58     -----------------------------------------------
     59     |               name-length  (value is u)     |   2 bytes
     60     -----------------------------------------------
     61     |                     name                    |   u bytes
     62     -----------------------------------------------
     63     |              value-length  (value is v)     |   2 bytes
     64     -----------------------------------------------
     65     |                     value                   |   v bytes
     66     -----------------------------------------------   
    5467    """
    5568
     
    7891        self.name = str(name)
    7992        self.value = str(value)
     93
     94    def toBinaryData(self):
     95        """
     96        Packs the value data into binary data.
     97        """
     98       
     99        # get the length in bytes of the name
     100        name_length = sys.getsizeof(name)
     101        # get the length in bytes of the value
     102        value_length = sys.getsizeof(value)
     103
     104        # if the name is of length zero, then don't include it in the
     105        # binary data
     106        if name_length == IPPTags.ZERO_NAME_LENGTH:
     107            binary = struct.pack('>bhhs',
     108                                 self.value_tag,
     109                                 name_length,
     110                                 value_length,
     111                                 self.value)
     112        else:
     113            binary = struct.pack('>bhshs',
     114                                 self.value_tag,
     115                                 name_length,
     116                                 self.name,
     117                                 value_length,
     118                                 self.value)
     119
     120        return binary
    80121
    81122class IPPAttribute():
     
    135176         self.attribute_tag = hex(attribute_tag)
    136177         self.values = values
     178
     179    def toBinaryData(self):
     180        """
     181        Packs the attribute data into binary data.
     182        """
     183
     184        # pack the attribute tag
     185        attribute_tag = struct.pack('>b', self.attribute_tag)
     186        # get the binary data for all the values
     187        values = [v.toBinaryData() for v in self.values]
     188
     189        # concatenate everything together and return it
     190        return attribute_tag + ''.join(values)
    137191
    138192class IPPRequest():
     
    216270        if request is not None:
    217271            # read the version-number (two signed chars)
    218             self.version        = struct.unpack('bb', request.read(2))
     272            self.version        = struct.unpack('>bb', request.read(2))
    219273
    220274            # read the operation-id (or status-code, but that's only
    221275            # for a response) (signed short)
    222             self.operation_id   = struct.unpack('h', request.read(2))
     276            self.operation_id   = struct.unpack('>h', request.read(2))
    223277
    224278            # read the request-id (signed int)
    225             self.request_id     = struct.unpack('i', request.read(4))
     279            self.request_id     = struct.unpack('>i', request.read(4))
    226280
    227281            # now we have to read in the attributes.  Each attribute
     
    230284
    231285            # read in the next byte
    232             next_byte = struct.unpack('b', request.read(1))
     286            next_byte = struct.unpack('>b', request.read(1))
    233287
    234288            # as long as the next byte isn't signaling the end of the
     
    242296                    attribute_tag = next_byte
    243297                    # read in the value tag (signed char)
    244                     value_tag     = struct.unpack('b', request.read(1))
     298                    value_tag     = struct.unpack('>b', request.read(1))
    245299                    # read in the length of the name (signed short)
    246                     name_length   = struct.unpack('h', request.read(2))
     300                    name_length   = struct.unpack('>h', request.read(2))
    247301                    # read the name (a string of name_length bytes)
    248                     name          = struct.unpack('s', request.read(name_length))
     302                    name          = struct.unpack('>s', request.read(name_length))
    249303                    # read in the length of the value (signed short)
    250                     value_length  = struct.unpack('h', request.read(2))
     304                    value_length  = struct.unpack('>h', request.read(2))
    251305                    # read in the value (string of value_length bytes)
    252                     value         = struct.unpack('b'*value_length, request.read(value_length))
     306                    value         = struct.unpack('>%sb' % value_length, request.read(value_length))
    253307
    254308                    # create a new IPPAttribute from the data we just
     
    265319                    # read in the length of the name (two bytes) --
    266320                    # this should be 0x0
    267                     name_length   = struct.unpack('h', request.read(2))
    268                     assert name_length == zero_name_length
     321                    name_length   = struct.unpack('>h', request.read(2))
     322                    assert name_length == IPPTags.ZERO_NAME_LENGTH
    269323                    # name should be empty
    270324                    name          = ''
    271325                    # read in the length of the value (two bytes)
    272                     value_length  = struct.unpack('h', request.read(2))
     326                    value_length  = struct.unpack('>h', request.read(2))
    273327                    # read in the value (value_length bytes)
    274                     value         = struct.unpack('b'*value_length, request.read(value_length))
     328                    value         = struct.unpack('>%sb' % value_length, request.read(value_length))
    275329
    276330                    # add another value to the last attribute
     
    278332
    279333                # read another byte
    280                 next_byte = struct.unpack('b', request.read(1))
     334                next_byte = struct.unpack('>b', request.read(1))
    281335
    282336            # once we hit the end-of-attributes tag, the only thing
    283337            # left is the data, so go ahead and read all of it
    284338            buff = request.read()
    285             self.data = struct.unpack('b'*len(buff), sys.getsizeof(buff))
     339            self.data = struct.unpack('>%sb' % len(buff), sys.getsizeof(buff))
    286340
    287341        # otherwise, just set the class variables to the keyword
     
    293347            self.attributes = attributes
    294348            self.data = data
     349
     350    def toBinaryData(self):
     351        """
     352        Packs the value data into binary data.
     353        """
     354
     355        # convert the version, operation id, and request id to binary
     356        preattributes = struct.pack('>bbhi',
     357                                    self.version[0],
     358                                    self.version[1],
     359                                    self.operation_id,
     360                                    self.request_id)
     361
     362        # convert the attributes to binary
     363        attributes = ''.join([a.toBinaryData() for a in attributes])
     364
     365        # conver the end-of-attributes-tag to binary
     366        end_of_attributes_tag = struct.pack('>b', IPPTags.END_OF_ATTRIBUTES_TAG)
     367
     368        # convert the data to binary
     369        data = ''.join([struct.pack('>b', x) for x in self.data])
     370
     371        # append everything together and return it
     372        return preattributes + attributes + end_of_attributes_tag + data
Note: See TracChangeset for help on using the changeset viewer.