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_int(b): marker = _ord(b[0]) if marker < NEG_2BYTE_MARKER: sz = 8 - getbits(marker, 4) return ((-1 << (sz << 3)) | get_int(b[1:], sz), b[sz+1:]) elif marker < NEG_1BYTE_MARKER: return (NEG_2BYTE_MIN + ((getbits(marker, 5) << 8) | _ord(b[1])), b[2:]) elif marker < POS_1BYTE_MARKER: return (NEG_1BYTE_MIN + getbits(marker, 6), b[1:]) elif marker < POS_2BYTE_MARKER: return (getbits(marker, 6), b[1:]) elif marker < POS_MULTI_MARKER: return (POS_1BYTE_MAX + 1 + ((getbits(marker, 5) << 8) | _ord(b[1])), b[2:]) else: sz = getbits(marker, 4) return (POS_2BYTE_MAX + 1 + get_int(b[1:], sz), b[sz+1:])
def get_int(b, size): r = 0 for i in range(size): r = (r << 8) | _ord(b[i]) return r