1"""
2@copyright: 2009 Bastian Blank <waldi@debian.org>
3@license: GNU GPL-3
4"""
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 3 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17import struct
18
19
20class DescriptorTag(object):
21    _struct = struct.Struct('<HHBxHHHI')
22
23    def __init__(self, buf):
24        data = self._struct.unpack(buf[:16])
25        (self.identifier, self.version, self.checksum, serial_number,
26                crc, self.crc_length, self.location) = data
27
28    def __repr__(self):
29        return '<DescriptorTag with identifier: %d; version: %d; location: %d>' % (
30                self.identifier,
31                self.version,
32                self.location,
33                )
34
35
36class OSTACompressedUnicode(str):
37    def __new__(self, buf):
38        stype = buf[0]
39
40        if stype == 8:
41            return buf[1:].decode()
42        elif stype == 16:
43            raise NotImplementedError
44        raise RuntimeError
45
46
47class ExtentAD(object):
48    _struct = struct.Struct('<II8xH2x')
49    size = _struct.size
50
51    def __init__(self, buf):
52        data = self._struct.unpack(buf)
53        length, self.location, self.partition = data
54        self.length = length & 0x3fffffff
55        self.flags = length >> 30
56
57    def __repr__(self):
58        return '<ExtentAD with location: %d; length: %d; flags: %d; partition: %d>' % (
59                self.location,
60                self.length,
61                self.flags,
62                self.partition,
63                )
64
65
66class LongAD(object):
67    _struct = struct.Struct('<IIH6x')
68    size = _struct.size
69
70    def __init__(self, buf):
71        data = self._struct.unpack(buf)
72        length, self.location, self.partition = data
73        self.length = length & 0x3fffffff
74        self.flags = length >> 30
75
76    def __repr__(self):
77        return '<LongAD with location: %d; length: %d; flags: %d; partition: %d>' % (
78                self.location,
79                self.length,
80                self.flags,
81                self.partition,
82                )
83
84
85class ShortAD(object):
86    _struct = struct.Struct('<II')
87    size = _struct.size
88
89    def __init__(self, buf):
90        data = self._struct.unpack(buf)
91        length, self.location = data
92        self.length = length & 0x3fffffff
93        self.flags = length >> 30
94
95    def __repr__(self):
96        return '<ShortAD with location: %d; length: %d; flags: %x' % (
97                self.location,
98                self.length,
99                self.flags,
100                )
101