def unpack(fmt, s): tfmt, fmt = __get_type(fmt) if not fmt: return () if tfmt != '.': raise ValueError( 'Only variable-length encoding is currently supported') result = [] for offset, havesize, size, f in __unpack_iter_fmt(fmt): if f == 'x': s = s[size:] # Note: no value, don't increment i elif f in 'SsUu': if not havesize: if f == 's': pass elif f == 'S': size = s.find(x00) elif f == 'u' and offset == len(fmt) - 1: # A WT_ITEM with a NULL data field will be appear as None. if s == None: s = empty_pack size = len(s) else: # Note: 'U' is used internally, and may be exposed to us. # It indicates that the size is always stored unless there # is a size in the format. size, s = unpack_int(s) if f in 'Ss': result.append(_string_result(s[:size])) if f == 'S' and not havesize: size += 1 else: result.append(s[:size]) s = s[size:] elif f in 't': # bit type, size is number of bits result.append(_ord(s[0])) s = s[1:] elif f in 'Bb': # byte type for i in range(size): v = _ord(s[0]) if f != 'B': v -= 0x80 result.append(v) s = s[1:] else: # integral type for j in range(size): v, s = unpack_int(s) result.append(v) return result
def unpack(fmt, s): tfmt, fmt = __get_type(fmt) if not fmt: return () if tfmt != '.': raise ValueError('Only variable-length encoding is currently supported') result = [] for offset, havesize, size, f in __unpack_iter_fmt(fmt): if f == 'x': s = s[size:] # Note: no value, don't increment i elif f in 'SsUu': if not havesize: if f == 's': pass elif f == 'S': size = s.find(x00) elif f == 'u' and offset == len(fmt) - 1: # A WT_ITEM with a NULL data field will be appear as None. if s == None: s = empty_pack size = len(s) else: # Note: 'U' is used internally, and may be exposed to us. # It indicates that the size is always stored unless there # is a size in the format. size, s = unpack_int(s) if f in 'Ss': result.append(_string_result(s[:size])) if f == 'S' and not havesize: size += 1 else: result.append(s[:size]) s = s[size:] elif f in 't': # bit type, size is number of bits result.append(_ord(s[0])) s = s[1:] elif f in 'Bb': # byte type for i in range(size): v = _ord(s[0]) if f != 'B': v -= 0x80 result.append(v) s = s[1:] else: # integral type for j in range(size): v, s = unpack_int(s) result.append(v) return result