This file is indexed.

/usr/bin/dvdvideo-backup-image is in python3-dvdvideo 0.20130117+nmu1.

This file is owned by root:root, with mode 0o755.

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
#! /usr/bin/python3
"""
A tool to create an image of a video DVD.

@copyright: 2009 Bastian Blank <waldi@debian.org>
@license: GNU GPL-3
"""
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import logging

from dvdvideo.media import MediaUdf
from dvdvideo.volume import MalformedVolumePartError


class Part(object):
    def __lt__(self, other):
        if isinstance(other, Part):
            if self.begin < other.begin: return True
            return self.end < other.end
        return NotImplemented

    def __repr__(self):
        return '<%s object: begin: %d; end: %d>' % (self.__class__.__name__,
                self.begin,
                self.end,
                )

    def _dump_iter(self, media):
        length = self.end - self.begin
        cur = 0
        while cur < length:
            count = min(512, length - cur)
            r = self._dump_read(media, cur, count)
            if not len(r):
                logging.debug('got eof')
                return
            cur += len(r) // 2048
            yield r

    def _dump_read(self, media, cur, count):
        try:
            return media.read(count)
        except IOError:
            logging.info('Read error at %d, padding with zeroes', self.begin + cur)
            self._dump_seek(media, cur + count)
            return bytes(count * 2048)

    def _dump_seek(self, media, count, **kw):
        media.seek(self.begin + count, **kw)

    def dump(self, media):
        self._dump_seek(media, 0)
        return self._dump_iter(media)


class PartFile(Part):
    def adjust_other(self, other, all):
        if isinstance(other, self.__class__):
            if self.begin <= other.begin and other.end <= self.end:
                logging.debug('Overlap between %r and %r, removing', self, other)
                all.remove(other)
                return True
            if self.begin <= other.begin and other.begin < self.end:
                logging.debug('Partial overlap between %r and %r, adjusting and removing', self, other)
                self.end = other.end
                all.remove(other)
                return True
        return self.adjust_other_special(other, all)

    def adjust_other_special(self, other, all):
        pass

    def adjust_self(self, all):
        if self.begin == self.end:
            logging.debug('Size 0 in %r, removing', self)
            all.remove(self)
            return True

    def check(self, all):
        for other in all:
            if self is other:
                continue
            if (self.begin <= other.begin and other.begin < self.end or
                self.begin < other.end and other.end <= self.end):
                raise RuntimeError('Overlap between %r and %r' % (self, other))


class PartIfo(PartFile):
    def __init__(self, file):
        self.begin = file.location
        self.end = file.location + file.length


class PartIfoVmg(PartIfo):
    def _dump_iter(self, media):
        r = bytearray(self._dump_read(media, 0, 1))
        r[35] = 0
        yield r

        length = self.end - self.begin
        cur = 1
        while cur < length:
            count = min(512, length - cur)
            r = self._dump_read(media, cur, count)
            if not len(r):
                logging.debug('got eof')
                return
            cur += len(r) // 2048
            yield r


class PartIfoVts(PartIfo):
    pass


class PartBupVmg(PartIfoVmg):
    pass


class PartBupVts(PartIfoVts):
    pass


class PartVob(PartFile):
    def adjust_other_special(self, other, all):
        if isinstance(other, PartIfo):
            if self.begin <= other.begin and other.begin < self.end and self.end <= other.end:
                logging.debug('Overlap between %r and %r, adjusting', self, other)
                self.end = other.begin
                return True

    def _dump_read(self, media, cur, count):
        try:
            return media.read(count, encrypted=True)
        except IOError:
            logging.debug('Read error at %d', self.begin + cur)
            self._dump_seek(media, cur + count)
            return bytes(count * 2048)

    def dump(self, media):
        self._dump_seek(media, 0, start_encrypted=True)
        return self._dump_iter(media)


class PartMenuVob(PartVob):
    def __init__(self, file):
        self.begin = file.location
        self.end = file.location + file.length


class PartTitleVob(PartVob):
    def __init__(self, file):
        self.begin = file[0].location
        file = file[-1]
        self.end = file.location + file.length


class PartMeta(Part):
    def __init__(self, begin, end):
        self.begin, self.end = begin, end


def main(stream, input, output):
    media = MediaUdf(input)
    part = media.udf.volume.partitions[0]

    vmg = media.vmg()
    vts = []
    for i in range(1, vmg.ifo.header.number_titlesets + 1):
        try:
            vts.append(media.vts(i))
        except MalformedVolumePartError as e:
            logging.debug('Ignore VTS %d because of errors (%s)', i, e)
            pass

    image_length = part.location + part.length

    progress = ProgressMeter(stream, image_length // 512)

    parts = []
    parts.append(PartIfoVmg(vmg.fileset.ifo))
    if vmg.fileset.menu_vob:
        parts.append(PartMenuVob(vmg.fileset.menu_vob))
    parts.append(PartBupVmg(vmg.fileset.bup))

    for i in vts:
        parts.append(PartIfoVts(i.fileset.ifo))
        if i.fileset.menu_vob:
            parts.append(PartMenuVob(i.fileset.menu_vob))
        parts.append(PartTitleVob(i.fileset.title_vob))
        if i.bup:
            parts.append(PartBupVts(i.fileset.bup))

    logging.debug('parts: %r', parts)

    parts_removed = []
    changed = True
    while changed:
        changed = False
        for i in parts:
            if i.adjust_self(parts):
                changed = True
            for j in parts:
                if i is not j:
                    if i.adjust_other(j, parts):
                        changed = True
    for i in parts:
        i.check(parts)

    logging.debug('parts: %r', parts)

    parts_complete = []
    part_end = 0
    while parts:
        cur = parts.pop(0)
        if cur.begin > part_end:
            parts_complete.append(PartMeta(part_end, cur.begin))
        parts_complete.append(cur)
        part_end = cur.end
    parts_complete.append(PartMeta(part_end, image_length))

    logging.debug('overall parts: %r', parts_complete)

    image = open(output, 'wb')

    cur = 0
    for part in parts_complete:
        logging.debug('part: %r', part)

        for r in part.dump(media):
            count_real = len(r) // 2048
            progress.update(count_real // 512)
            cur += count_real
            image.write(r)

        logging.debug('part end, written %d', cur)

if __name__ == '__main__':
    import optparse
    from dvdvideo.utils import ProgressMeter, ProgressStream

    class OptionParser(optparse.OptionParser):
        def error(self, msg):
            self.exit(2, "%s: %s\n" % (self.get_prog_name(), msg))
        def exit(self, status=0, msg=None):
            if msg:
                sys.stderr.write(msg)
                sys.stderr.write("Try `%s --help' for more information.\n" % self.get_prog_name())
            sys.exit(status)

    optparser = OptionParser('%prog [OPTION]... INPUT OUTPUT')
    optparser.add_option('-d', '--debug', dest='debug', action='store_true')
    options, args = optparser.parse_args()
    if len(args) != 2:
        optparser.error('incorrect number of arguments')

    stream = ProgressStream(sys.stdout)
    logging.basicConfig(level=options.debug and logging.DEBUG or logging.INFO, stream=stream)

    try:
        try:
            main(stream, *args)
        finally:
            stream.clear_meter()
    except KeyboardInterrupt as e:
        logging.warning('Interrupted')
    except Exception as e:
        logging.exception("Got exception")