aboutsummaryrefslogtreecommitdiffstats
path: root/libpwman/fileobj.py
blob: 3b87de0c74026e84df0215c38b71e1e2c3d13fdc (plain)
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
# -*- coding: utf-8 -*-
"""
# Simple object file format.
# Copyright (c) 2011-2024 Michael Büsch <m@bues.ch>
# Licensed under the GNU/GPL version 2 or later.
"""

import errno

__all__ = [
	"FileObjError",
	"FileObj",
	"FileObjCollection",
]

class FileObjError(Exception):
	pass

class FileObj:
	# Raw object layout:
	#   [ 1 byte  ] => Name length
	#   [ x bytes ] => Name
	#   [ 4 bytes ] => Payload data length
	#   [ x bytes ] => Payload data

	__slots__ = (
		"__name",
		"__data",
	)

	def __init__(self, name, data):
		"""Construct FileObj().
		name: The object name. Must be bytes-like.
		data: The object payload. Must be bytes-like.
		"""
		assert isinstance(name, (bytes, bytearray, memoryview)),\
		       "FileObj: Invalid 'name' type."
		assert isinstance(data, (bytes, bytearray, memoryview)),\
		       "FileObj: Invalid 'data' type."
		self.__name = memoryview(name)
		self.__data = memoryview(data)
		if len(self.__name) > 0x7F:
			raise FileObjError("FileObj: Name too long")
		if len(self.__data) > 0x7FFFFFFF:
			raise FileObjError("FileObj: Data too long")

	def getName(self):
		return self.__name

	def getData(self):
		return self.__data

	def getRaw(self, buffer):
		nameLen = len(self.__name)
		assert nameLen <= 0x7F
		buffer += b"%c" % (nameLen & 0xFF)
		buffer += self.__name
		dataLen = len(self.__data)
		assert dataLen <= 0x7FFFFFFF
		buffer += b"%c" % (dataLen & 0xFF)
		buffer += b"%c" % ((dataLen >> 8) & 0xFF)
		buffer += b"%c" % ((dataLen >> 16) & 0xFF)
		buffer += b"%c" % ((dataLen >> 24) & 0xFF)
		buffer += self.__data

	@classmethod
	def parseRaw(cls, raw):
		assert isinstance(raw, (bytes, bytearray, memoryview)),\
		       "FileObj: Invalid 'raw' type."
		raw = memoryview(raw)
		try:
			off = 0
			nameLen = raw[off]
			if nameLen & 0x80:
				raise FileObjError("FileObj: Name length extension bit is set, "
						   "but not supported by this pwman version.")
			off += 1
			name = raw[off : off + nameLen]
			off += nameLen
			dataLen = (raw[off] |
				   (raw[off + 1] << 8) |
				   (raw[off + 2] << 16) |
				   (raw[off + 3] << 24))
			if dataLen & 0x80000000:
				raise FileObjError("FileObj: Data length extension bit is set, "
						   "but not supported by this pwman version.")
			off += 4
			data = raw[off : off + dataLen]
			off += dataLen
		except (IndexError, KeyError) as e:
			raise FileObjError("Failed to parse file object")
		return (cls(name, data), off)

class FileObjCollection:
	__slots__ = (
		"__objects",
	)

	def __init__(self, objects):
		if isinstance(objects, dict):
			self.__objects = objects
		elif isinstance(objects, (list, tuple)):
			self.__objects = { obj.getName() : obj for obj in objects }
		else:
			assert False

	def writeFile(self, filepath):
		try:
			with open(filepath, "wb") as f:
				f.write(self.getRaw())
				f.flush()
		except IOError as e:
			raise FileObjError("Failed to write file: %s" % e.strerror)

	def getRaw(self):
		raw = bytearray()
		for obj in self.__objects.values():
			obj.getRaw(raw)
		return raw

	@property
	def objects(self):
		return self.__objects.values()

	def get(self, name, error=None, default=None):
		obj = self.__objects.get(name, None)
		if obj is None:
			if error:
				raise FileObjError(error)
			return default
		return bytes(obj.getData())

	@classmethod
	def parseRaw(cls, raw):
		assert isinstance(raw, (bytes, bytearray, memoryview)),\
		       "FileObjCollection: Invalid 'raw' type."
		raw = memoryview(raw)
		offset = 0
		objects = {}
		while offset < len(raw):
			obj, objLen = FileObj.parseRaw(raw[offset:])
			objects[obj.getName()] = obj
			offset += objLen
		return cls(objects)

	@classmethod
	def parseFile(cls, filepath):
		try:
			with open(filepath, "rb") as f:
				rawData = f.read()
		except IOError as e:
			if e.errno != errno.ENOENT:
				raise FileObjError("Failed to read file: %s" % e.strerror)
			return None
		return cls.parseRaw(rawData)
bues.ch cgit interface