|
| 1 | +# -*- python -*- |
| 2 | +################################################################# |
| 3 | +# |
| 4 | +# Module : CODEC |
| 5 | +# Purpose: Base encoders/decoders |
| 6 | +# |
| 7 | +################################################################# |
| 8 | +# |
| 9 | +# Copyright (c) 2011 SEQUANS Communications. |
| 10 | +# All rights reserved. |
| 11 | +# |
| 12 | +# This is confidential and proprietary source code of SEQUANS |
| 13 | +# Communications. The use of the present source code and all |
| 14 | +# its derived forms is exclusively governed by the restricted |
| 15 | +# terms and conditions set forth in the SEQUANS |
| 16 | +# Communications' EARLY ADOPTER AGREEMENT and/or LICENCE |
| 17 | +# AGREEMENT. The present source code and all its derived |
| 18 | +# forms can ONLY and EXCLUSIVELY be used with SEQUANS |
| 19 | +# Communications' products. The distribution/sale of the |
| 20 | +# present source code and all its derived forms is EXCLUSIVELY |
| 21 | +# RESERVED to regular LICENCE holder and otherwise STRICTLY |
| 22 | +# PROHIBITED. |
| 23 | +# |
| 24 | +################################################################# |
| 25 | +import struct, array |
| 26 | + |
| 27 | +LITTLE_ENDIAN = "<" |
| 28 | +NATIVE_ENDIAN = "=" |
| 29 | +BIG_ENDIAN = ">" |
| 30 | + |
| 31 | +# -------------------------------------------------// Utility /__________________________________ |
| 32 | +class encode: |
| 33 | + @staticmethod |
| 34 | + def u32 (value, endian = BIG_ENDIAN): |
| 35 | + return array.array("c", struct.pack(endian + "I", value)) |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def s32 (value, endian = BIG_ENDIAN): |
| 39 | + if value < 0: |
| 40 | + value = 0x100000000 + value |
| 41 | + return encode.u32(value, endian) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def u16 (value, endian = BIG_ENDIAN): |
| 45 | + return array.array("c", struct.pack(endian + "H", value)) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def u8 (value, endian = None): |
| 49 | + return array.array("c", chr(value)) |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def string (value, endian = None): |
| 53 | + return array.array("c", value + "\x00") |
| 54 | + |
| 55 | +class decode: |
| 56 | + @staticmethod |
| 57 | + def u32 (value, endian = BIG_ENDIAN): |
| 58 | + return struct.unpack(endian + "I", value)[0] |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def s32 (value, endian = BIG_ENDIAN): |
| 62 | + v = decode.u32(value, endian) |
| 63 | + if v & (1 << 31): |
| 64 | + return v - 0x100000000 |
| 65 | + return v |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def u16 (value, endian = BIG_ENDIAN): |
| 69 | + return struct.unpack(endian + "H", value)[0] |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def u8 (value, endian = None): |
| 73 | + return ord(value) |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def string (value, endian = None): |
| 77 | + offset = 0 |
| 78 | + str = "" |
| 79 | + c = value[offset] |
| 80 | + while c != '\x00': |
| 81 | + offset += 1 |
| 82 | + str += c |
| 83 | + c = value[offset] |
| 84 | + |
| 85 | + return str |
| 86 | + |
0 commit comments