Beispiel #1
0
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':
            if not havesize:
                size = 1
            s = s[size:]
            # Note: no value, don't increment i
        elif f in 'SsUu':
            if not havesize:
                if f == 's':
                    size = 1
                elif f == 'S':
                    size = s.find('\0')
                elif f == 'u' and offset == len(fmt) - 1:
                    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)
            result.append(s[:size])
            if f == 'S' and not havesize:
                size += 1
            s = s[size:]
        elif f in 't':
            # bit type, size is number of bits
            if not havesize:
                size = 1
            result.append(ord(s[0:1]))
            s = s[1:]
        elif f in 'Bb':
            # byte type
            if not havesize:
                size = 1
            for i in xrange(size):
                v = ord(s[0:1])
                if f != 'B':
                    v -= 0x80
                result.append(v)
                s = s[1:]
        else:
            # integral type
            if not havesize:
                size = 1
            for j in xrange(size):
                v, s = unpack_int(s)
                result.append(v)
    return result
Beispiel #2
0
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 = []
    havesize = size = 0
    for offset, f in enumerate(fmt):
        if f.isdigit():
            size = (size * 10) + int(f)
            havesize = 1
            continue
        elif f == 'x':
            if not havesize:
                size = 1
            s = s[size:]
            # Note: no value, don't increment i
        elif f in 'Ssu':
            if not havesize:
                if f == 's':
                    size = 1
                elif f == 'S':
                    size = s.find('\0')
                elif f == 'u':
                    if offset == len(fmt) - 1:
                        size = len(s)
                    else:
                        size, s = unpack_int(s)
            result.append(s[:size])
            if f == 'S' and not havesize:
                size += 1
            s = s[size:]
        elif f in 't':
            # bit type, size is number of bits
            if not havesize:
                size = 1
            result.append(ord(s[0:1]))
            s = s[1:]
        else:
            # integral type
            if not havesize:
                size = 1
            for j in xrange(size):
                v, s = unpack_int(s)
                result.append(v)
        havesize = size = 0
    return result
Beispiel #3
0
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 = []
	havesize = size = 0
	for offset, f in enumerate(fmt):
		if f.isdigit():
			size = (size * 10) + int(f)
			havesize = 1
			continue
		elif f == 'x':
			if not havesize:
				size = 1
			s = s[size:]
			# Note: no value, don't increment i
		elif f in 'Ssu':
			if not havesize:
				if f == 's':
					size = 1
				elif f == 'S':
					size = s.find('\0')
				elif f == 'u':
					if offset == len(fmt) - 1:
						size = len(s)
					else:
						size, s = unpack_int(s)
			result.append(s[:size])
			if f == 'S' and not havesize:
				size += 1
			s = s[size:]
		elif f in 't':
			# bit type, size is number of bits
			if not havesize:
				size = 1
			result.append(ord(s[0:1]))
			s = s[1:]
		else:
			# integral type
			if not havesize:
				size = 1
			for j in xrange(size):
				v, s = unpack_int(s)
				result.append(v)
		havesize = size = 0
	return result