/usr/lib/python2.7/dist-packages/kaitaistruct.py is in python-kaitaistruct 0.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | import itertools
import sys
from struct import unpack
try:
import cStringIO
BytesIO = cStringIO.StringIO
except ImportError:
from io import BytesIO # noqa
PY2 = sys.version_info[0] == 2
# Kaitai Struct runtime streaming API version, defined as per PEP-0396
# standard. Used for two purposes:
#
# * .py files generated by ksc from .ksy check that they import proper
# KS runtime library by this version number;
# * distribution utils (setup.py) use this when packaging for PyPI
#
__version__ = '0.7'
class KaitaiStruct(object):
def __init__(self, stream):
self._io = stream
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
self.close()
@classmethod
def from_file(cls, filename):
f = open(filename, 'rb')
try:
return cls(KaitaiStream(f))
except Exception:
# close file descriptor, then reraise the exception
f.close()
raise
def close(self):
self._io.close()
class KaitaiStream(object):
def __init__(self, io):
self._io = io
self.align_to_byte()
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
self.close()
def close(self):
self._io.close()
# ========================================================================
# Stream positioning
# ========================================================================
def is_eof(self):
io = self._io
t = io.read(1)
if t == b'':
return True
else:
io.seek(io.tell() - 1)
return False
def seek(self, n):
self._io.seek(n)
def pos(self):
return self._io.tell()
def size(self):
# Python has no internal File object API function to get
# current file / StringIO size, thus we use the following
# trick.
io = self._io
# Remember our current position
cur_pos = io.tell()
# Seek to the end of the File object
io.seek(0, 2)
# Remember position, which is equal to the full length
full_size = io.tell()
# Seek back to the current position
io.seek(cur_pos)
return full_size
# ========================================================================
# Integer numbers
# ========================================================================
# ------------------------------------------------------------------------
# Signed
# ------------------------------------------------------------------------
def read_s1(self):
return unpack('b', self.read_bytes(1))[0]
# ........................................................................
# Big-endian
# ........................................................................
def read_s2be(self):
return unpack('>h', self.read_bytes(2))[0]
def read_s4be(self):
return unpack('>i', self.read_bytes(4))[0]
def read_s8be(self):
return unpack('>q', self.read_bytes(8))[0]
# ........................................................................
# Little-endian
# ........................................................................
def read_s2le(self):
return unpack('<h', self.read_bytes(2))[0]
def read_s4le(self):
return unpack('<i', self.read_bytes(4))[0]
def read_s8le(self):
return unpack('<q', self.read_bytes(8))[0]
# ------------------------------------------------------------------------
# Unsigned
# ------------------------------------------------------------------------
def read_u1(self):
return unpack('B', self.read_bytes(1))[0]
# ........................................................................
# Big-endian
# ........................................................................
def read_u2be(self):
return unpack('>H', self.read_bytes(2))[0]
def read_u4be(self):
return unpack('>I', self.read_bytes(4))[0]
def read_u8be(self):
return unpack('>Q', self.read_bytes(8))[0]
# ........................................................................
# Little-endian
# ........................................................................
def read_u2le(self):
return unpack('<H', self.read_bytes(2))[0]
def read_u4le(self):
return unpack('<I', self.read_bytes(4))[0]
def read_u8le(self):
return unpack('<Q', self.read_bytes(8))[0]
# ========================================================================
# Floating point numbers
# ========================================================================
# ........................................................................
# Big-endian
# ........................................................................
def read_f4be(self):
return unpack('>f', self.read_bytes(4))[0]
def read_f8be(self):
return unpack('>d', self.read_bytes(8))[0]
# ........................................................................
# Little-endian
# ........................................................................
def read_f4le(self):
return unpack('<f', self.read_bytes(4))[0]
def read_f8le(self):
return unpack('<d', self.read_bytes(8))[0]
# ========================================================================
# Unaligned bit values
# ========================================================================
def align_to_byte(self):
self.bits = 0
self.bits_left = 0
def read_bits_int(self, n):
bits_needed = n - self.bits_left
if bits_needed > 0:
# 1 bit => 1 byte
# 8 bits => 1 byte
# 9 bits => 2 bytes
bytes_needed = ((bits_needed - 1) // 8) + 1
buf = self.read_bytes(bytes_needed)
for byte in buf:
# Python 2 will get "byte" as one-character str, thus
# we need to convert it to integer manually; Python 3
# is fine as is.
if isinstance(byte, str):
byte = ord(byte)
self.bits <<= 8
self.bits |= byte
self.bits_left += 8
# raw mask with required number of 1s, starting from lowest bit
mask = (1 << n) - 1
# shift mask to align with highest bits available in self.bits
shift_bits = self.bits_left - n
mask <<= shift_bits
# derive reading result
res = (self.bits & mask) >> shift_bits
# clear top bits that we've just read => AND with 1s
self.bits_left -= n
mask = (1 << self.bits_left) - 1
self.bits &= mask
return res
# ========================================================================
# Byte arrays
# ========================================================================
def read_bytes(self, n):
r = self._io.read(n)
if len(r) < n:
raise EOFError(
"requested %d bytes, but got only %d bytes" % (n, len(r))
)
return r
def read_bytes_full(self):
return self._io.read()
def read_bytes_term(self, term, include_term, consume_term, eos_error):
r = b''
while True:
c = self._io.read(1)
if c == b'':
if eos_error:
raise Exception(
"End of stream reached, but no terminator %d found" %
(term,)
)
else:
return r
elif ord(c) == term:
if include_term:
r += c
if not consume_term:
self._io.seek(self._io.tell() - 1)
return r
else:
r += c
def ensure_fixed_contents(self, expected):
actual = self._io.read(len(expected))
if actual != expected:
raise Exception(
"Unexpected fixed contents: got %s, was waiting for %s" %
(str(actual), str(expected))
)
return actual
@staticmethod
def bytes_strip_right(src, pad_byte):
new_len = len(src)
if PY2:
data = bytearray(src)
else:
data = src
while data[new_len - 1] == pad_byte:
new_len -= 1
return data[:new_len]
@staticmethod
def bytes_terminate(src, term, include_term):
new_len = 0
max_len = len(src)
if PY2:
data = bytearray(src)
else:
data = src
while new_len < max_len and data[new_len] != term:
new_len += 1
if include_term and new_len < max_len:
new_len += 1
return data[:new_len]
# ========================================================================
# Byte array processing
# ========================================================================
@staticmethod
def process_xor_one(data, key):
if PY2:
r = bytearray(data)
for i in range(len(r)):
r[i] ^= key
return bytes(r)
else:
return bytes(v ^ key for v in data)
@staticmethod
def process_xor_many(data, key):
if PY2:
r = bytearray(data)
k = bytearray(key)
ki = 0
kl = len(k)
for i in range(len(r)):
r[i] ^= k[ki]
ki += 1
if ki >= kl:
ki = 0
return bytes(r)
else:
return bytes(a ^ b for a, b in zip(data, itertools.cycle(key)))
@staticmethod
def process_rotate_left(data, amount, group_size):
if group_size != 1:
raise Exception(
"unable to rotate group of %d bytes yet" %
(group_size,)
)
mask = group_size * 8 - 1
anti_amount = -amount & mask
r = bytearray(data)
for i in range(len(r)):
r[i] = (r[i] << amount) & 0xff | (r[i] >> anti_amount)
return bytes(r)
|