This file is indexed.

/usr/lib/python3/dist-packages/audiotools/image.py is in audiotools 3.1.1-1+b1.

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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# Audio Tools, a module and set of tools for manipulating audio data
# Copyright (C) 2007-2015  Brian Langenberger

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

from audiotools.bitstream import BitstreamReader, format_size
from audiotools import InvalidImage


def image_metrics(file_data):
    """returns an ImageMetrics subclass from a string of file data

    raises InvalidImage if there is an error parsing the file
    or its type is unknown"""

    if file_data[0:3] == b"\xff\xd8\xff":
        return __JPEG__.parse(file_data)
    elif file_data[0:8] == b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A':
        return __PNG__.parse(file_data)
    elif file_data[0:3] == b'GIF':
        return __GIF__.parse(file_data)
    elif file_data[0:2] == b'BM':
        return __BMP__.parse(file_data)
    elif (file_data[0:2] == b'II') or (file_data[0:2] == b'MM'):
        return __TIFF__.parse(file_data)
    else:
        from audiotools.text import ERR_IMAGE_UNKNOWN_TYPE
        raise InvalidImage(ERR_IMAGE_UNKNOWN_TYPE)


#######################
# JPEG
#######################


class ImageMetrics:
    """a container for image data"""

    def __init__(self, width, height, bits_per_pixel, color_count, mime_type):
        """fields are as follows:

        width          - image width as an integer number of pixels
        height         - image height as an integer number of pixels
        bits_per_pixel - the number of bits per pixel as an integer
        color_count    - for palette-based images, the total number of colors
        mime_type      - the image's MIME type, as a unicode string

        all of the ImageMetrics subclasses implement these fields
        in addition, they all implement a parse() classmethod
        used to parse binary string data and return something
        imageMetrics compatible
        """

        self.width = width
        self.height = height
        self.bits_per_pixel = bits_per_pixel
        self.color_count = color_count
        self.mime_type = mime_type

    def __repr__(self):
        return "ImageMetrics(%s,%s,%s,%s,%s)" % \
               (repr(self.width),
                repr(self.height),
                repr(self.bits_per_pixel),
                repr(self.color_count),
                repr(self.mime_type))

    @classmethod
    def parse(cls, file_data):
        raise NotImplementedError()


class InvalidJPEG(InvalidImage):
    """raised if a JPEG cannot be parsed correctly"""

    pass


class __JPEG__(ImageMetrics):
    def __init__(self, width, height, bits_per_pixel):
        ImageMetrics.__init__(self, width, height, bits_per_pixel,
                              0, u'image/jpeg')

    @classmethod
    def parse(cls, file_data):
        def segments(reader):
            if reader.read(8) != 0xFF:
                from audiotools.text import ERR_IMAGE_INVALID_JPEG_MARKER
                raise InvalidJPEG(ERR_IMAGE_INVALID_JPEG_MARKER)
            segment_type = reader.read(8)

            while segment_type != 0xDA:
                if segment_type not in {0xD8, 0xD9}:
                    yield (segment_type, reader.substream(reader.read(16) - 2))
                else:
                    yield (segment_type, None)

                if reader.read(8) != 0xFF:
                    from audiotools.text import ERR_IMAGE_INVALID_JPEG_MARKER
                    raise InvalidJPEG(ERR_IMAGE_INVALID_JPEG_MARKER)
                segment_type = reader.read(8)

        try:
            for (segment_type,
                 segment_data) in segments(BitstreamReader(file_data, False)):
                if (segment_type in {0xC0, 0xC1, 0xC2, 0xC3,
                                     0xC5, 0XC5, 0xC6, 0xC7,
                                     0xC9, 0xCA, 0xCB, 0xCD,
                                     0xCE, 0xCF}):  # start of frame
                    (data_precision,
                     image_height,
                     image_width,
                     components) = segment_data.parse("8u 16u 16u 8u")
                    return __JPEG__(width=image_width,
                                    height=image_height,
                                    bits_per_pixel=data_precision * components)
        except IOError:
            from audiotools.text import ERR_IMAGE_IOERROR_JPEG
            raise InvalidJPEG(ERR_IMAGE_IOERROR_JPEG)

#######################
# PNG
#######################


class InvalidPNG(InvalidImage):
    """raised if a PNG cannot be parsed correctly"""

    pass


class __PNG__(ImageMetrics):
    def __init__(self, width, height, bits_per_pixel, color_count):
        ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count,
                              u'image/png')

    @classmethod
    def parse(cls, file_data):
        def chunks(reader):
            if reader.read_bytes(8) != b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A':
                from audiotools.text import ERR_IMAGE_INVALID_PNG
                raise InvalidPNG(ERR_IMAGE_INVALID_PNG)
            (chunk_length, chunk_type) = reader.parse("32u 4b")
            while chunk_type != b'IEND':
                yield (chunk_type,
                       chunk_length,
                       reader.substream(chunk_length))
                chunk_crc = reader.read(32)
                (chunk_length, chunk_type) = reader.parse("32u 4b")

        ihdr = None
        plte_length = 0

        try:
            for (chunk_type,
                 chunk_length,
                 chunk_data) in chunks(BitstreamReader(file_data, False)):
                if chunk_type == b'IHDR':
                    ihdr = chunk_data
                elif chunk_type == b'PLTE':
                    plte_length = chunk_length

            if ihdr is None:
                from audiotools.text import ERR_IMAGE_INVALID_PNG
                raise InvalidPNG(ERR_IMAGE_INVALID_PNG)

            (width,
             height,
             bit_depth,
             color_type,
             compression_method,
             filter_method,
             interlace_method) = ihdr.parse("32u 32u 8u 8u 8u 8u 8u")
        except IOError:
            from audiotools.text import ERR_IMAGE_IOERROR_PNG
            raise InvalidPNG(ERR_IMAGE_IOERROR_PNG)

        if color_type == 0:    # grayscale
            return cls(width=width,
                       height=height,
                       bits_per_pixel=bit_depth,
                       color_count=0)
        elif color_type == 2:  # RGB
            return cls(width=width,
                       height=height,
                       bits_per_pixel=bit_depth * 3,
                       color_count=0)
        elif color_type == 3:  # palette
            if (plte_length % 3) != 0:
                from audiotools.text import ERR_IMAGE_INVALID_PLTE
                raise InvalidPNG(ERR_IMAGE_INVALID_PLTE)
            else:
                return cls(width=width,
                           height=height,
                           bits_per_pixel=8,
                           color_count=plte_length // 3)
        elif color_type == 4:  # grayscale + alpha
            return cls(width=width,
                       height=height,
                       bits_per_pixel=bit_depth * 2,
                       color_count=0)
        elif color_type == 6:  # RGB + alpha
            return cls(width=width,
                       height=height,
                       bits_per_pixel=bit_depth * 4,
                       color_count=0)

#######################
# BMP
#######################


class InvalidBMP(InvalidImage):
    """raised if a BMP cannot be parsed correctly"""

    pass


class __BMP__(ImageMetrics):
    def __init__(self, width, height, bits_per_pixel, color_count):
        ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count,
                              u'image/x-ms-bmp')

    @classmethod
    def parse(cls, file_data):
        try:
            (magic_number,
             file_size,
             data_offset,
             header_size,
             width,
             height,
             color_planes,
             bits_per_pixel,
             compression_method,
             image_size,
             horizontal_resolution,
             vertical_resolution,
             colors_used,
             important_colors_used) = BitstreamReader(file_data, True).parse(
                "2b 32u 16p 16p 32u " +
                "32u 32u 32u 16u 16u 32u 32u 32u 32u 32u 32u")
        except IOError:
            from audiotools.text import ERR_IMAGE_IOERROR_BMP
            raise InvalidBMP(ERR_IMAGE_IOERROR_BMP)

        if magic_number != b'BM':
            from audiotools.text import ERR_IMAGE_INVALID_BMP
            raise InvalidBMP(ERR_IMAGE_INVALID_BMP)
        else:
            return cls(width=width,
                       height=height,
                       bits_per_pixel=bits_per_pixel,
                       color_count=colors_used)


#######################
# GIF
#######################


class InvalidGIF(InvalidImage):
    """raised if a GIF cannot be parsed correctly"""

    pass


class __GIF__(ImageMetrics):
    def __init__(self, width, height, color_count):
        ImageMetrics.__init__(self, width, height, 8, color_count,
                              u'image/gif')

    @classmethod
    def parse(cls, file_data):
        try:
            (gif,
             version,
             width,
             height,
             color_table_size) = BitstreamReader(file_data, True).parse(
                "3b 3b 16u 16u 3u 5p")
        except IOError:
            from audiotools.text import ERR_IMAGE_IOERROR_GIF
            raise InvalidGIF(ERR_IMAGE_IOERROR_GIF)

        if gif != b'GIF':
            from audiotools.text import ERR_IMAGE_INVALID_GIF
            raise InvalidGIF(ERR_IMAGE_INVALID_GIF)
        else:
            return cls(width=width,
                       height=height,
                       color_count=2 ** (color_table_size + 1))


#######################
# TIFF
#######################


class InvalidTIFF(InvalidImage):
    """raised if a TIFF cannot be parsed correctly"""

    pass


class __TIFF__(ImageMetrics):
    def __init__(self, width, height, bits_per_pixel, color_count):
        ImageMetrics.__init__(self, width, height,
                              bits_per_pixel, color_count,
                              u'image/tiff')

    @classmethod
    def parse(cls, file_data):
        from io import BytesIO

        def tags(file, order):
            while True:
                reader = BitstreamReader(file, order)
                # read all the tags in an IFD
                tag_count = reader.read(16)
                sub_reader = reader.substream(tag_count * 12)
                next_ifd = reader.read(32)

                for i in range(tag_count):
                    (tag_code,
                     tag_datatype,
                     tag_value_count) = sub_reader.parse("16u 16u 32u")
                    if tag_datatype == 1:    # BYTE type
                        tag_struct = "8u" * tag_value_count
                    elif tag_datatype == 3:  # SHORT type
                        tag_struct = "16u" * tag_value_count
                    elif tag_datatype == 4:  # LONG type
                        tag_struct = "32u" * tag_value_count
                    else:                      # all other types
                        tag_struct = "4b"
                    if format_size(tag_struct) <= 32:
                        yield (tag_code, sub_reader.parse(tag_struct))
                        sub_reader.skip(32 - format_size(tag_struct))
                    else:
                        offset = sub_reader.read(32)
                        file.seek(offset, 0)
                        yield (tag_code,
                               BitstreamReader(file, order).parse(tag_struct))

                if next_ifd != 0:
                    file.seek(next_ifd, 0)
                else:
                    break

        file = BytesIO(file_data)
        try:
            byte_order = file.read(2)
            if byte_order == b'II':
                order = 1
            elif byte_order == b'MM':
                order = 0
            else:
                from audiotools.text import ERR_IMAGE_INVALID_TIFF
                raise InvalidTIFF(ERR_IMAGE_INVALID_TIFF)
            reader = BitstreamReader(file, order)
            if reader.read(16) != 42:
                from audiotools.text import ERR_IMAGE_INVALID_TIFF
                raise InvalidTIFF(ERR_IMAGE_INVALID_TIFF)

            initial_ifd = reader.read(32)
            file.seek(initial_ifd, 0)

            width = 0
            height = 0
            bits_per_pixel = 0
            color_count = 0
            for (tag_id, tag_values) in tags(file, order):
                if tag_id == 0x0100:
                    width = tag_values[0]
                elif tag_id == 0x0101:
                    height = tag_values[0]
                elif tag_id == 0x0102:
                    bits_per_pixel = sum(tag_values)
                elif tag_id == 0x0140:
                    color_count = len(tag_values) // 3
        except IOError:
            from audiotools.text import ERR_IMAGE_IOERROR_TIFF
            raise InvalidTIFF(ERR_IMAGE_IOERROR_TIFF)

        return cls(width=width,
                   height=height,
                   bits_per_pixel=bits_per_pixel,
                   color_count=color_count)