Exemple #1
0
def pack(fmt, *values):
    tfmt, fmt = __get_type(fmt)
    if not fmt:
        return ()
    if tfmt != '.':
        raise ValueError('Only variable-length encoding is currently supported')
    result = ''
    i = 0
    for offset, havesize, size, f, val in __pack_iter_fmt(fmt, values):
        if f == 'x':
            if not havesize:
                result += '\0'
            else:
                result += '\0' * size
            # Note: no value, don't increment i
        elif f in 'SsUu':
            if f == 'S' and '\0' in val:
                l = val.find('\0')
            else:
                l = len(val)
            if havesize or f == 's':
                if l > size:
                    l = size
            elif (f == 'u' and offset != len(fmt) - 1) or f == 'U':
                result += pack_int(l)
            if type(val) is unicode and f in 'Ss':
                result += str(val[:l])
            else:
                result += val[:l]
            if f == 'S' and not havesize:
                result += '\0'
            elif size > l:
                result += '\0' * (size - l)
        elif f in 't':
            # bit type, size is number of bits
            if not havesize:
                size = 1
            if size > 8:
                raise ValueError("bit count cannot be greater than 8 for 't' encoding")
            mask = (1 << size) - 1
            if (mask & val) != val:
                raise ValueError("value out of range for 't' encoding")
            result += chr(val)
        elif f in 'Bb':
            # byte type
            if not havesize:
                size = 1
            for i in xrange(size):
                if f == 'B':
                    v = val
                else:
                    # Translate to maintain ordering with the sign bit.
                    v = val + 0x80
                if v > 255 or v < 0:
                    raise ValueError("value out of range for 'B' encoding")
                result += chr(v)
        else:
            # integral type
            result += pack_int(val)
    return result
def pack(fmt, *values):
    tfmt, fmt = __get_type(fmt)
    if not fmt:
        return ()
    if tfmt != '.':
        raise ValueError(
            'Only variable-length encoding is currently supported')
    result = ''
    havesize = i = 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:
                result += '\0'
            else:
                result += '\0' * size
            # Note: no value, don't increment i
        elif f in 'Ssu':
            if f == 'S' and '\0' in values[i]:
                l = values[i].find('\0')
            else:
                l = len(values[i])
            if havesize:
                if l > size:
                    l = size
            elif f == 's':
                havesize = size = 1
            elif f == 'u' and offset != len(fmt) - 1:
                result += pack_int(l)
            result += values[i][:l]
            if f == 'S' and not havesize:
                result += '\0'
            elif size > l:
                result += '\0' * (size - l)
            i += 1
        elif f in 't':
            # bit type, size is number of bits
            if not havesize:
                size = 1
            if size > 8:
                raise ValueError(
                    "bit count cannot be greater than 8 for 't' encoding")
            mask = (1 << size) - 1
            val = values[i]
            if (mask & val) != val:
                raise ValueError("value out of range for 't' encoding")
            result += chr(val)
            i += 1
        else:
            # integral type
            if not havesize:
                size = 1
            for j in xrange(size):
                result += pack_int(values[i])
                i += 1
        havesize = size = 0
    return result
def pack(fmt, *values):
    tfmt, fmt = __get_type(fmt)
    if not fmt:
        return ()
    if tfmt != '.':
        raise ValueError('Only variable-length encoding is currently supported')
    result = ''
    i = 0
    for offset, havesize, size, f, val in __pack_iter_fmt(fmt, values):
        if f == 'x':
            if not havesize:
                result += '\0'
            else:
                result += '\0' * size
            # Note: no value, don't increment i
        elif f in 'SsUu':
            if f == 'S' and '\0' in val:
                l = val.find('\0')
            else:
                l = len(val)
            if havesize or f == 's':
                if l > size:
                    l = size
            elif (f == 'u' and offset != len(fmt) - 1) or f == 'U':
                result += pack_int(l)
            if type(val) is unicode and f in 'Ss':
                result += str(val[:l])
            else:
                result += val[:l]
            if f == 'S' and not havesize:
                result += '\0'
            elif size > l and havesize:
                result += '\0' * (size - l)
        elif f in 't':
            # bit type, size is number of bits
            if not havesize:
                size = 1
            if size > 8:
                raise ValueError("bit count cannot be greater than 8 for 't' encoding")
            mask = (1 << size) - 1
            if (mask & val) != val:
                raise ValueError("value out of range for 't' encoding")
            result += chr(val)
        elif f in 'Bb':
            # byte type
            if not havesize:
                size = 1
            for i in xrange(size):
                if f == 'B':
                    v = val
                else:
                    # Translate to maintain ordering with the sign bit.
                    v = val + 0x80
                if v > 255 or v < 0:
                    raise ValueError("value out of range for 'B' encoding")
                result += chr(v)
        else:
            # integral type
            result += pack_int(val)
    return result
Exemple #4
0
def pack(fmt, *values):
	tfmt, fmt = __get_type(fmt)
	if not fmt:
		return ()
	if tfmt != '.':
		raise ValueError('Only variable-length encoding is currently supported')
	result = ''
	havesize = i = 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:
				result += '\0'
			else:
				result += '\0' * size
			# Note: no value, don't increment i
		elif f in 'Ssu':
			if f == 'S' and '\0' in values[i]:
				l = values[i].find('\0')
			else:
				l = len(values[i])
			if havesize:
				if l > size:
					l = size
			elif f == 's':
				havesize = size = 1
			elif f == 'u' and offset != len(fmt) - 1:
				result += pack_int(l)
			result += values[i][:l]
			if f == 'S' and not havesize:
				result += '\0'
			elif size > l:
				result += '\0' * (size - l)
			i += 1
		elif f in 't':
			# bit type, size is number of bits
			if not havesize:
				size = 1
                        if size > 8:
				raise ValueError("bit count cannot be greater than 8 for 't' encoding")
			mask = (1 << size) - 1
			val = values[i]
                        if (mask & val) != val:
				raise ValueError("value out of range for 't' encoding")
			result += chr(val)
			i += 1
		else:
			# integral type
			if not havesize:
				size = 1
			for j in xrange(size):
				result += pack_int(values[i])
				i += 1
		havesize = size = 0
	return result
Exemple #5
0
def pack(fmt, *values):
    tfmt, fmt = __get_type(fmt)
    if not fmt:
        return ()
    if tfmt != '.':
        raise ValueError(
            'Only variable-length encoding is currently supported')
    result = ''
    i = 0
    for offset, havesize, size, f, val in __pack_iter_fmt(fmt, values):
        if f == 'x':
            if not havesize:
                result += '\0'
            else:
                result += '\0' * size
            # Note: no value, don't increment i
        elif f in 'Ssu':
            if f == 'S' and '\0' in val:
                l = val.find('\0')
            else:
                l = len(val)
            if havesize:
                if l > size:
                    l = size
            elif f == 's':
                havesize = size = 1
            elif f == 'u' and offset != len(fmt) - 1:
                result += pack_int(l)
            if type(val) is unicode and f in 'Ss':
                result += str(val[:l])
            else:
                result += val[:l]
            if f == 'S' and not havesize:
                result += '\0'
            elif size > l:
                result += '\0' * (size - l)
        elif f in 't':
            # bit type, size is number of bits
            if not havesize:
                size = 1
            if size > 8:
                raise ValueError(
                    "bit count cannot be greater than 8 for 't' encoding")
            mask = (1 << size) - 1
            if (mask & val) != val:
                raise ValueError("value out of range for 't' encoding")
            result += chr(val)
        else:
            # integral type
            result += pack_int(val)
    return result