This file is indexed.

/usr/share/pyshared/netaddr/strategy/__init__.py is in python-netaddr 0.7.5-4build2.

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
#-----------------------------------------------------------------------------
#   Copyright (c) 2008-2010, David P. D. Moss. All rights reserved.
#
#   Released under the BSD license. See the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
Shared logic for various address types.
"""
import re as _re

from netaddr.compat import _range

#-----------------------------------------------------------------------------
def bytes_to_bits():
    """
    @return: A 256 element list containing 8-bit binary digit strings. The
        list index value is equivalent to its bit string value.
    """
    lookup = []
    bits_per_byte = _range(7, -1, -1)
    for num in range(256):
        bits = 8 * [None]
        for i in bits_per_byte:
            bits[i] = '01'[num & 1]
            num >>= 1
        lookup.append(''.join(bits))
    return lookup

#: A lookup table of 8-bit integer values to their binary digit bit strings.
BYTES_TO_BITS = bytes_to_bits()

#-----------------------------------------------------------------------------
def valid_words(words, word_size, num_words):
    """
    @param words: A sequence of unsigned integer word values.

    @param word_size: Width (in bits) of each unsigned integer word value.

    @param num_words: Number of unsigned integer words expected.

    @return: C{True} if word sequence is valid for this address type,
        C{False} otherwise.
    """
    if not hasattr(words, '__iter__'):
        return False

    if len(words) != num_words:
        return False

    max_word = 2 ** word_size - 1

    for i in words:
        if not 0 <= i <= max_word:
            return False

    return True

#-----------------------------------------------------------------------------
def int_to_words(int_val, word_size, num_words):
    """
    @param int_val: Unsigned integer to be divided into words of equal size.

    @param word_size: Width (in bits) of each unsigned integer word value.

    @param num_words: Number of unsigned integer words expected.

    @return: A tuple contain unsigned integer word values split according
        to provided arguments.
    """
    max_int = 2 ** (num_words * word_size) - 1

    if not 0 <= int_val <= max_int:
        raise IndexError('integer out of bounds: %r!' % hex(int_val))

    max_word = 2 ** word_size - 1

    words = []
    for _ in range(num_words):
        word = int_val & max_word
        words.append(int(word))
        int_val >>= word_size

    return tuple(reversed(words))

#-----------------------------------------------------------------------------
def words_to_int(words, word_size, num_words):
    """
    @param words: A sequence of unsigned integer word values.

    @param word_size: Width (in bits) of each unsigned integer word value.

    @param num_words: Number of unsigned integer words expected.

    @return: An unsigned integer that is equivalent to value represented
        by word sequence.
    """
    if not valid_words(words, word_size, num_words):
        raise ValueError('invalid integer word sequence: %r!' % words)

    int_val = 0
    for i, num in enumerate(reversed(words)):
        word = num
        word = word << word_size * i
        int_val = int_val | word

    return int_val

#-----------------------------------------------------------------------------
def valid_bits(bits, width, word_sep=''):
    """
    @param bits: A network address in a delimited binary string format.

    @param width: Maximum width (in bits) of a network address (excluding
        delimiters).

    @param word_sep: (optional) character or string used to delimit word
        groups (default: '', no separator).

    @return: C{True} if network address is valid, C{False} otherwise.
    """
    if not hasattr(bits, 'replace'):
        return False

    if word_sep != '':
        bits = bits.replace(word_sep, '')

    if len(bits) != width:
        return False

    max_int = 2 ** width - 1

    try:
        if 0 <= int(bits, 2) <= max_int:
            return True
    except ValueError:
        pass

    return False

#-----------------------------------------------------------------------------
def bits_to_int(bits, width, word_sep=''):
    """
    @param bits: A network address in a delimited binary string format.

    @param width: Maximum width (in bits) of a network address (excluding
        delimiters).

    @param word_sep: (optional) character or string used to delimit word
        groups (default: '', no separator).

    @return: An unsigned integer that is equivalent to value represented
        by network address in readable binary form.
    """
    if not valid_bits(bits, width, word_sep):
        raise ValueError('invalid readable binary string: %r!' % bits)

    if word_sep != '':
        bits = bits.replace(word_sep, '')

    return int(bits, 2)

#-----------------------------------------------------------------------------
def int_to_bits(int_val, word_size, num_words, word_sep=''):
    """
    @param int_val: An unsigned integer.

    @param word_size: Width (in bits) of each unsigned integer word value.

    @param num_words: Number of unsigned integer words expected.

    @param word_sep: (optional) character or string used to delimit word
        groups (default: '', no separator).

    @return: A network address in a delimited binary string format that is
        equivalent in value to unsigned integer.
    """
    bit_words = []

    for word in int_to_words(int_val, word_size, num_words):
        bits = []
        while word:
            bits.append(BYTES_TO_BITS[word & 255])
            word >>= 8
        bits.reverse()
        bit_str = ''.join(bits) or '0' * word_size
        bits = ('0' * word_size + bit_str)[-word_size:]
        bit_words.append(bits)

    if word_sep is not '':
        #   Check custom separator.
        if not hasattr(word_sep, 'join'):
            raise ValueError('word separator is not a string: %r!' % word_sep)

    return word_sep.join(bit_words)

#-----------------------------------------------------------------------------
def valid_bin(bin_val, width):
    """
    @param bin_val: A network address in Python's binary representation format
        ('0bxxx').

    @param width: Maximum width (in bits) of a network address (excluding
        delimiters).

    @return: C{True} if network address is valid, C{False} otherwise.
    """
    if not hasattr(bin_val, 'startswith'):
        return False

    if not bin_val.startswith('0b'):
        return False

    bin_val = bin_val.replace('0b', '')

    if len(bin_val) > width:
        return False

    max_int = 2 ** width - 1

    try:
        if 0 <= int(bin_val, 2) <= max_int:
            return True
    except ValueError:
        pass

    return False

#-----------------------------------------------------------------------------
def int_to_bin(int_val, width):
    """
    @param int_val: An unsigned integer.

    @param width: Maximum allowed width (in bits) of a unsigned integer.

    @return: Equivalent string value in Python's binary representation format
        ('0bxxx').
    """
    bin_tokens = []

    try:
        #   Python 2.6.x and upwards.
        bin_val = bin(int_val)
    except NameError:
        #   Python 2.4.x and 2.5.x
        i = int_val
        while i > 0:
            word = i & 0xff
            bin_tokens.append(BYTES_TO_BITS[word])
            i >>= 8

        bin_tokens.reverse()
        bin_val = '0b' + _re.sub(r'^[0]+([01]+)$', r'\1', ''.join(bin_tokens))

    if len(bin_val[2:]) > width:
        raise IndexError('binary string out of bounds: %s!' % bin_val)

    return bin_val

#-----------------------------------------------------------------------------
def bin_to_int(bin_val, width):
    """
    @param bin_val: A string containing an unsigned integer in Python's binary
        representation format ('0bxxx').

    @param width: Maximum allowed width (in bits) of a unsigned integer.

    @return: An unsigned integer that is equivalent to value represented
        by Python binary string format.
    """
    if not valid_bin(bin_val, width):
        raise ValueError('not a valid Python binary string: %r!' % bin_val)

    return int(bin_val.replace('0b', ''), 2)