def get_size(self, d): """Get total octet count of a body specified by new length header octs. :Parameters: - `d`: string of "new" length octets, note that this string cannot be made up of arbitrary bytes, and must conform to the rules of new length values (rfc2440 4.2.2) :Returns: XXXXXXX TODO Example: >>> newlength_octets = [0xff, 0x92, 0xdf, 0x7c, 0xbb] >>> newlength_data_string = ''.join(map(chr, length_octet_list)) >>> newlength = NewLength() >>> newlength.get_size(newlength_data_string) 2464119995 """ idx = size = 0 # the length_list is to keep track of data (d) actually used - # it's possible that a string of partials could terminate with # leftover octets length_list = [] if 0 < len(d): for L in [(x, ord(x)) for x in d]: if L[1] < 192: size = size + L[1] length_list.append(L[0]) break elif 192 <= L[1] <= 223: slice = d[idx:idx + 2] s = STN.doubleoct2int(slice) if 192 <= s <= 8383: # ?? dunno about this restriction size = size + s length_list.append(slice) else: raise PGPFormatError( "New double octet lengths are confined to the range 192 <= x <= 8383. Received: data->(%s) length->(%s)" % (d, size)) break elif 255 == L[1]: slice = d[idx:idx + 5] size = size + STN.pentoct2int(slice) length_list.append(slice) break elif 224 <= L[1] <= 254: # partials slice = d[idx:idx + 1] size = size + STN.partial2int(slice) length_list.append(slice) idx = idx + 1 else: raise PGPFormatError( "Length must have at least one octet. Received len(d) == 0.") return size, length_list
def get_size(self, d): """Get total octet count of a body specified by new length header octs. :Parameters: - `d`: string of "new" length octets, note that this string cannot be made up of arbitrary bytes, and must conform to the rules of new length values (rfc2440 4.2.2) :Returns: XXXXXXX TODO Example: >>> newlength_octets = [0xff, 0x92, 0xdf, 0x7c, 0xbb] >>> newlength_data_string = ''.join(map(chr, length_octet_list)) >>> newlength = NewLength() >>> newlength.get_size(newlength_data_string) 2464119995 """ idx = size = 0 # the length_list is to keep track of data (d) actually used - # it's possible that a string of partials could terminate with # leftover octets length_list = [] if 0 < len(d): for L in [(x, ord(x)) for x in d]: if L[1] < 192: size = size + L[1] length_list.append(L[0]) break elif 192 <= L[1] <= 223: slice = d[idx:idx+2] s = STN.doubleoct2int(slice) if 192 <= s <= 8383: # ?? dunno about this restriction size = size + s length_list.append(slice) else: raise PGPFormatError("New double octet lengths are confined to the range 192 <= x <= 8383. Received: data->(%s) length->(%s)" % (d, size)) break elif 255 == L[1]: slice = d[idx:idx+5] size = size + STN.pentoct2int(slice) length_list.append(slice) break elif 224 <= L[1] <= 254: # partials slice = d[idx:idx+1] size = size + STN.partial2int(slice) length_list.append(slice) idx = idx + 1 else: raise PGPFormatError("Length must have at least one octet. Received len(d) == 0.") return size, length_list
def testC4Inversion(self): """strnum: number to partial inversion (int2partial/partial2int)""" for p in self.good_partials: self.assertEqual(p[0], partial2int(int2partial(p[0])))