[8979f90] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | import sys, struct, logging |
---|
| 4 | |
---|
| 5 | # initialize logger |
---|
| 6 | logger = logging.getLogger("ippLogger") |
---|
| 7 | |
---|
| 8 | class IPPAttribute(): |
---|
| 9 | """ |
---|
| 10 | In addition to what the RFC reports, an attribute has an |
---|
| 11 | 'attribute tag', which specifies what type of attribute it is. |
---|
| 12 | It is 1 bytes long, and comes before the list of values. |
---|
| 13 | |
---|
| 14 | From RFC 2565: |
---|
| 15 | |
---|
| 16 | Each attribute consists of: |
---|
| 17 | ----------------------------------------------- |
---|
| 18 | | value-tag | 1 byte |
---|
| 19 | ----------------------------------------------- |
---|
| 20 | | name-length (value is u) | 2 bytes |
---|
| 21 | ----------------------------------------------- |
---|
| 22 | | name | u bytes |
---|
| 23 | ----------------------------------------------- |
---|
| 24 | | value-length (value is v) | 2 bytes |
---|
| 25 | ----------------------------------------------- |
---|
| 26 | | value | v bytes |
---|
| 27 | ----------------------------------------------- |
---|
| 28 | |
---|
| 29 | An additional value consists of: |
---|
| 30 | ----------------------------------------------------------- |
---|
| 31 | | value-tag | 1 byte | |
---|
| 32 | ----------------------------------------------- | |
---|
| 33 | | name-length (value is 0x0000) | 2 bytes | |
---|
| 34 | ----------------------------------------------- |-0 or more |
---|
| 35 | | value-length (value is w) | 2 bytes | |
---|
| 36 | ----------------------------------------------- | |
---|
| 37 | | value | w bytes | |
---|
| 38 | ----------------------------------------------------------- |
---|
| 39 | """ |
---|
| 40 | |
---|
| 41 | def __init__(self, name, values): |
---|
| 42 | """ |
---|
| 43 | Initialize an IPPAttribute. |
---|
| 44 | |
---|
| 45 | Arguments: |
---|
| 46 | |
---|
| 47 | name -- the name of the attribute |
---|
| 48 | |
---|
| 49 | values -- a list of IPPValues. May not be empty. |
---|
| 50 | """ |
---|
| 51 | |
---|
| 52 | # make sure name isn't empty |
---|
| 53 | assert name is not None |
---|
| 54 | |
---|
| 55 | # make sure the list of values isn't empty |
---|
| 56 | assert len(values) > 0 |
---|
| 57 | # make sure each value is an IPPValue |
---|
| 58 | for value in values: assert isinstance(value, IPPValue) |
---|
| 59 | |
---|
| 60 | self.name = name |
---|
| 61 | self.values = values |
---|
| 62 | |
---|
| 63 | def toBinaryData(self): |
---|
| 64 | """ |
---|
| 65 | Packs the attribute data into binary data. |
---|
| 66 | """ |
---|
| 67 | |
---|
| 68 | # get the binary data for all the values |
---|
| 69 | values = [] |
---|
| 70 | for v, i in zip(self.values, xrange(len(self.values))): |
---|
| 71 | |
---|
| 72 | # get the name length (0 for everything but the first |
---|
| 73 | # value) |
---|
| 74 | if i == 0: |
---|
| 75 | name_length = len(self.name) |
---|
| 76 | else: |
---|
| 77 | name_length = 0 |
---|
| 78 | |
---|
| 79 | # get the value length and binary value |
---|
| 80 | value_length, value_bin = v.valueToBinary() |
---|
| 81 | |
---|
| 82 | logger.debug("dumping name_length : %i" % name_length) |
---|
| 83 | logger.debug("dumping name : %s" % self.name) |
---|
| 84 | logger.debug("dumping value_length : %i" % value_length) |
---|
| 85 | logger.debug("dumping value : %s" % v.value) |
---|
| 86 | |
---|
| 87 | # the value tag in binary |
---|
| 88 | value_tag_bin = struct.pack('>b', v.value_tag) |
---|
| 89 | |
---|
| 90 | # the name length in binary |
---|
| 91 | name_length_bin = struct.pack('>h', name_length) |
---|
| 92 | |
---|
| 93 | # the name in binary |
---|
| 94 | name_bin = self.name |
---|
| 95 | |
---|
| 96 | # the value length in binary |
---|
| 97 | value_length_bin = struct.pack('>h', value_length) |
---|
| 98 | |
---|
| 99 | if i == 0: |
---|
| 100 | values.append(''.join([value_tag_bin, |
---|
| 101 | name_length_bin, |
---|
| 102 | name_bin, |
---|
| 103 | value_length_bin, |
---|
| 104 | value_bin])) |
---|
| 105 | else: |
---|
| 106 | values.append(''.join([value_tag_bin, |
---|
| 107 | name_length_bin, |
---|
| 108 | value_length_bin, |
---|
| 109 | value_bin])) |
---|
| 110 | |
---|
| 111 | # concatenate everything together and return it |
---|
| 112 | return ''.join(values) |
---|