Total Complexity | 381 |
Total Lines | 1036 |
Duplicated Lines | 8.69 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like pyasn1.type.univ often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # ASN.1 "universal" data types |
||
2 | import operator, sys |
||
3 | from pyasn1.type import base, tag, constraint, namedtype, namedval, tagmap |
||
4 | from pyasn1.codec.ber import eoo |
||
5 | from pyasn1.compat import octets |
||
6 | from pyasn1 import error |
||
7 | |||
8 | # "Simple" ASN.1 types (yet incomplete) |
||
9 | |||
10 | class Integer(base.AbstractSimpleAsn1Item): |
||
11 | tagSet = baseTagSet = tag.initTagSet( |
||
12 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x02) |
||
13 | ) |
||
14 | namedValues = namedval.NamedValues() |
||
15 | def __init__(self, value=None, tagSet=None, subtypeSpec=None, |
||
16 | namedValues=None): |
||
17 | if namedValues is None: |
||
18 | self.__namedValues = self.namedValues |
||
19 | else: |
||
20 | self.__namedValues = namedValues |
||
21 | base.AbstractSimpleAsn1Item.__init__( |
||
22 | self, value, tagSet, subtypeSpec |
||
23 | ) |
||
24 | |||
25 | def __and__(self, value): return self.clone(self._value & value) |
||
26 | def __rand__(self, value): return self.clone(value & self._value) |
||
27 | def __or__(self, value): return self.clone(self._value | value) |
||
28 | def __ror__(self, value): return self.clone(value | self._value) |
||
29 | def __xor__(self, value): return self.clone(self._value ^ value) |
||
30 | def __rxor__(self, value): return self.clone(value ^ self._value) |
||
31 | def __lshift__(self, value): return self.clone(self._value << value) |
||
32 | def __rshift__(self, value): return self.clone(self._value >> value) |
||
33 | |||
34 | def __add__(self, value): return self.clone(self._value + value) |
||
35 | def __radd__(self, value): return self.clone(value + self._value) |
||
36 | def __sub__(self, value): return self.clone(self._value - value) |
||
37 | def __rsub__(self, value): return self.clone(value - self._value) |
||
38 | def __mul__(self, value): return self.clone(self._value * value) |
||
39 | def __rmul__(self, value): return self.clone(value * self._value) |
||
40 | def __mod__(self, value): return self.clone(self._value % value) |
||
41 | def __rmod__(self, value): return self.clone(value % self._value) |
||
42 | def __pow__(self, value, modulo=None): return self.clone(pow(self._value, value, modulo)) |
||
43 | def __rpow__(self, value): return self.clone(pow(value, self._value)) |
||
44 | |||
45 | if sys.version_info[0] <= 2: |
||
46 | def __div__(self, value): return self.clone(self._value // value) |
||
47 | def __rdiv__(self, value): return self.clone(value // self._value) |
||
48 | else: |
||
49 | def __truediv__(self, value): return self.clone(self._value / value) |
||
50 | def __rtruediv__(self, value): return self.clone(value / self._value) |
||
51 | def __divmod__(self, value): return self.clone(self._value // value) |
||
52 | def __rdivmod__(self, value): return self.clone(value // self._value) |
||
53 | |||
54 | __hash__ = base.AbstractSimpleAsn1Item.__hash__ |
||
55 | |||
56 | def __int__(self): return int(self._value) |
||
57 | def __long__(self): return int(self._value) |
||
58 | def __float__(self): return float(self._value) |
||
59 | def __abs__(self): return abs(self._value) |
||
60 | def __index__(self): return int(self._value) |
||
61 | |||
62 | def __lt__(self, value): return self._value < value |
||
63 | def __le__(self, value): return self._value <= value |
||
64 | def __eq__(self, value): return self._value == value |
||
65 | def __ne__(self, value): return self._value != value |
||
66 | def __gt__(self, value): return self._value > value |
||
67 | def __ge__(self, value): return self._value >= value |
||
68 | |||
69 | def prettyIn(self, value): |
||
70 | if not isinstance(value, str): |
||
71 | return int(value) |
||
72 | r = self.__namedValues.getValue(value) |
||
73 | if r is not None: |
||
74 | return r |
||
75 | try: |
||
76 | return int(value) |
||
77 | except ValueError: |
||
78 | raise error.PyAsn1Error( |
||
79 | 'Can\'t coerce %s into integer: %s' % (value, sys.exc_info()[1]) |
||
80 | ) |
||
81 | |||
82 | def prettyOut(self, value): |
||
83 | r = self.__namedValues.getName(value) |
||
84 | if r is not None: |
||
85 | return '%s(%s)' % (r, value) |
||
86 | else: |
||
87 | return str(value) |
||
88 | |||
89 | def getNamedValues(self): return self.__namedValues |
||
90 | |||
91 | View Code Duplication | def clone(self, value=None, tagSet=None, subtypeSpec=None, |
|
|
|||
92 | namedValues=None): |
||
93 | if value is None and tagSet is None and subtypeSpec is None \ |
||
94 | and namedValues is None: |
||
95 | return self |
||
96 | if value is None: |
||
97 | value = self._value |
||
98 | if tagSet is None: |
||
99 | tagSet = self._tagSet |
||
100 | if subtypeSpec is None: |
||
101 | subtypeSpec = self._subtypeSpec |
||
102 | if namedValues is None: |
||
103 | namedValues = self.__namedValues |
||
104 | return self.__class__(value, tagSet, subtypeSpec, namedValues) |
||
105 | |||
106 | View Code Duplication | def subtype(self, value=None, implicitTag=None, explicitTag=None, |
|
107 | subtypeSpec=None, namedValues=None): |
||
108 | if value is None: |
||
109 | value = self._value |
||
110 | if implicitTag is not None: |
||
111 | tagSet = self._tagSet.tagImplicitly(implicitTag) |
||
112 | elif explicitTag is not None: |
||
113 | tagSet = self._tagSet.tagExplicitly(explicitTag) |
||
114 | else: |
||
115 | tagSet = self._tagSet |
||
116 | if subtypeSpec is None: |
||
117 | subtypeSpec = self._subtypeSpec |
||
118 | else: |
||
119 | subtypeSpec = subtypeSpec + self._subtypeSpec |
||
120 | if namedValues is None: |
||
121 | namedValues = self.__namedValues |
||
122 | else: |
||
123 | namedValues = namedValues + self.__namedValues |
||
124 | return self.__class__(value, tagSet, subtypeSpec, namedValues) |
||
125 | |||
126 | class Boolean(Integer): |
||
127 | tagSet = baseTagSet = tag.initTagSet( |
||
128 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x01), |
||
129 | ) |
||
130 | subtypeSpec = Integer.subtypeSpec+constraint.SingleValueConstraint(0,1) |
||
131 | namedValues = Integer.namedValues.clone(('False', 0), ('True', 1)) |
||
132 | |||
133 | class BitString(base.AbstractSimpleAsn1Item): |
||
134 | tagSet = baseTagSet = tag.initTagSet( |
||
135 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x03) |
||
136 | ) |
||
137 | namedValues = namedval.NamedValues() |
||
138 | def __init__(self, value=None, tagSet=None, subtypeSpec=None, |
||
139 | namedValues=None): |
||
140 | if namedValues is None: |
||
141 | self.__namedValues = self.namedValues |
||
142 | else: |
||
143 | self.__namedValues = namedValues |
||
144 | base.AbstractSimpleAsn1Item.__init__( |
||
145 | self, value, tagSet, subtypeSpec |
||
146 | ) |
||
147 | |||
148 | View Code Duplication | def clone(self, value=None, tagSet=None, subtypeSpec=None, |
|
149 | namedValues=None): |
||
150 | if value is None and tagSet is None and subtypeSpec is None \ |
||
151 | and namedValues is None: |
||
152 | return self |
||
153 | if value is None: |
||
154 | value = self._value |
||
155 | if tagSet is None: |
||
156 | tagSet = self._tagSet |
||
157 | if subtypeSpec is None: |
||
158 | subtypeSpec = self._subtypeSpec |
||
159 | if namedValues is None: |
||
160 | namedValues = self.__namedValues |
||
161 | return self.__class__(value, tagSet, subtypeSpec, namedValues) |
||
162 | |||
163 | View Code Duplication | def subtype(self, value=None, implicitTag=None, explicitTag=None, |
|
164 | subtypeSpec=None, namedValues=None): |
||
165 | if value is None: |
||
166 | value = self._value |
||
167 | if implicitTag is not None: |
||
168 | tagSet = self._tagSet.tagImplicitly(implicitTag) |
||
169 | elif explicitTag is not None: |
||
170 | tagSet = self._tagSet.tagExplicitly(explicitTag) |
||
171 | else: |
||
172 | tagSet = self._tagSet |
||
173 | if subtypeSpec is None: |
||
174 | subtypeSpec = self._subtypeSpec |
||
175 | else: |
||
176 | subtypeSpec = subtypeSpec + self._subtypeSpec |
||
177 | if namedValues is None: |
||
178 | namedValues = self.__namedValues |
||
179 | else: |
||
180 | namedValues = namedValues + self.__namedValues |
||
181 | return self.__class__(value, tagSet, subtypeSpec, namedValues) |
||
182 | |||
183 | def __str__(self): return str(tuple(self)) |
||
184 | |||
185 | # Immutable sequence object protocol |
||
186 | |||
187 | def __len__(self): |
||
188 | if self._len is None: |
||
189 | self._len = len(self._value) |
||
190 | return self._len |
||
191 | def __getitem__(self, i): |
||
192 | if isinstance(i, slice): |
||
193 | return self.clone(operator.getitem(self._value, i)) |
||
194 | else: |
||
195 | return self._value[i] |
||
196 | |||
197 | def __add__(self, value): return self.clone(self._value + value) |
||
198 | def __radd__(self, value): return self.clone(value + self._value) |
||
199 | def __mul__(self, value): return self.clone(self._value * value) |
||
200 | def __rmul__(self, value): return self * value |
||
201 | |||
202 | def prettyIn(self, value): |
||
203 | r = [] |
||
204 | if not value: |
||
205 | return () |
||
206 | elif isinstance(value, str): |
||
207 | if value[0] == '\'': |
||
208 | if value[-2:] == '\'B': |
||
209 | for v in value[1:-2]: |
||
210 | if v == '0': |
||
211 | r.append(0) |
||
212 | elif v == '1': |
||
213 | r.append(1) |
||
214 | else: |
||
215 | raise error.PyAsn1Error( |
||
216 | 'Non-binary BIT STRING initializer %s' % (v,) |
||
217 | ) |
||
218 | return tuple(r) |
||
219 | elif value[-2:] == '\'H': |
||
220 | for v in value[1:-2]: |
||
221 | i = 4 |
||
222 | v = int(v, 16) |
||
223 | while i: |
||
224 | i = i - 1 |
||
225 | r.append((v>>i)&0x01) |
||
226 | return tuple(r) |
||
227 | else: |
||
228 | raise error.PyAsn1Error( |
||
229 | 'Bad BIT STRING value notation %s' % value |
||
230 | ) |
||
231 | else: |
||
232 | for i in value.split(','): |
||
233 | j = self.__namedValues.getValue(i) |
||
234 | if j is None: |
||
235 | raise error.PyAsn1Error( |
||
236 | 'Unknown bit identifier \'%s\'' % i |
||
237 | ) |
||
238 | if j >= len(r): |
||
239 | r.extend([0]*(j-len(r)+1)) |
||
240 | r[j] = 1 |
||
241 | return tuple(r) |
||
242 | elif isinstance(value, (tuple, list)): |
||
243 | r = tuple(value) |
||
244 | for b in r: |
||
245 | if b and b != 1: |
||
246 | raise error.PyAsn1Error( |
||
247 | 'Non-binary BitString initializer \'%s\'' % (r,) |
||
248 | ) |
||
249 | return r |
||
250 | elif isinstance(value, BitString): |
||
251 | return tuple(value) |
||
252 | else: |
||
253 | raise error.PyAsn1Error( |
||
254 | 'Bad BitString initializer type \'%s\'' % (value,) |
||
255 | ) |
||
256 | |||
257 | def prettyOut(self, value): |
||
258 | return '\"\'%s\'B\"' % ''.join([str(x) for x in value]) |
||
259 | |||
260 | class OctetString(base.AbstractSimpleAsn1Item): |
||
261 | tagSet = baseTagSet = tag.initTagSet( |
||
262 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x04) |
||
263 | ) |
||
264 | defaultBinValue = defaultHexValue = base.noValue |
||
265 | encoding = 'us-ascii' |
||
266 | def __init__(self, value=None, tagSet=None, subtypeSpec=None, |
||
267 | encoding=None, binValue=None, hexValue=None): |
||
268 | if encoding is None: |
||
269 | self._encoding = self.encoding |
||
270 | else: |
||
271 | self._encoding = encoding |
||
272 | if binValue is not None: |
||
273 | value = self.fromBinaryString(binValue) |
||
274 | if hexValue is not None: |
||
275 | value = self.fromHexString(hexValue) |
||
276 | if value is None or value is base.noValue: |
||
277 | value = self.defaultHexValue |
||
278 | if value is None or value is base.noValue: |
||
279 | value = self.defaultBinValue |
||
280 | self.__intValue = None |
||
281 | base.AbstractSimpleAsn1Item.__init__(self, value, tagSet, subtypeSpec) |
||
282 | |||
283 | def clone(self, value=None, tagSet=None, subtypeSpec=None, |
||
284 | encoding=None, binValue=None, hexValue=None): |
||
285 | if value is None and tagSet is None and subtypeSpec is None and \ |
||
286 | encoding is None and binValue is None and hexValue is None: |
||
287 | return self |
||
288 | if value is None and binValue is None and hexValue is None: |
||
289 | value = self._value |
||
290 | if tagSet is None: |
||
291 | tagSet = self._tagSet |
||
292 | if subtypeSpec is None: |
||
293 | subtypeSpec = self._subtypeSpec |
||
294 | if encoding is None: |
||
295 | encoding = self._encoding |
||
296 | return self.__class__( |
||
297 | value, tagSet, subtypeSpec, encoding, binValue, hexValue |
||
298 | ) |
||
299 | |||
300 | if sys.version_info[0] <= 2: |
||
301 | def prettyIn(self, value): |
||
302 | if isinstance(value, str): |
||
303 | return value |
||
304 | elif isinstance(value, (tuple, list)): |
||
305 | try: |
||
306 | return ''.join([ chr(x) for x in value ]) |
||
307 | except ValueError: |
||
308 | raise error.PyAsn1Error( |
||
309 | 'Bad OctetString initializer \'%s\'' % (value,) |
||
310 | ) |
||
311 | else: |
||
312 | return str(value) |
||
313 | else: |
||
314 | def prettyIn(self, value): |
||
315 | if isinstance(value, bytes): |
||
316 | return value |
||
317 | elif isinstance(value, OctetString): |
||
318 | return value.asOctets() |
||
319 | elif isinstance(value, (tuple, list, map)): |
||
320 | try: |
||
321 | return bytes(value) |
||
322 | except ValueError: |
||
323 | raise error.PyAsn1Error( |
||
324 | 'Bad OctetString initializer \'%s\'' % (value,) |
||
325 | ) |
||
326 | else: |
||
327 | try: |
||
328 | return str(value).encode(self._encoding) |
||
329 | except UnicodeEncodeError: |
||
330 | raise error.PyAsn1Error( |
||
331 | 'Can\'t encode string \'%s\' with \'%s\' codec' % (value, self._encoding) |
||
332 | ) |
||
333 | |||
334 | |||
335 | def fromBinaryString(self, value): |
||
336 | bitNo = 8; byte = 0; r = () |
||
337 | for v in value: |
||
338 | if bitNo: |
||
339 | bitNo = bitNo - 1 |
||
340 | else: |
||
341 | bitNo = 7 |
||
342 | r = r + (byte,) |
||
343 | byte = 0 |
||
344 | if v == '0': |
||
345 | v = 0 |
||
346 | elif v == '1': |
||
347 | v = 1 |
||
348 | else: |
||
349 | raise error.PyAsn1Error( |
||
350 | 'Non-binary OCTET STRING initializer %s' % (v,) |
||
351 | ) |
||
352 | byte = byte | (v << bitNo) |
||
353 | return octets.ints2octs(r + (byte,)) |
||
354 | |||
355 | def fromHexString(self, value): |
||
356 | r = p = () |
||
357 | for v in value: |
||
358 | if p: |
||
359 | r = r + (int(p+v, 16),) |
||
360 | p = () |
||
361 | else: |
||
362 | p = v |
||
363 | if p: |
||
364 | r = r + (int(p+'0', 16),) |
||
365 | return octets.ints2octs(r) |
||
366 | |||
367 | def prettyOut(self, value): |
||
368 | if sys.version_info[0] <= 2: |
||
369 | numbers = tuple([ ord(x) for x in value ]) |
||
370 | else: |
||
371 | numbers = tuple(value) |
||
372 | if [ x for x in numbers if x < 32 or x > 126 ]: |
||
373 | return '0x' + ''.join([ '%x' % x for x in numbers ]) |
||
374 | else: |
||
375 | return str(value) |
||
376 | |||
377 | def __repr__(self): |
||
378 | if self._value is base.noValue: |
||
379 | return self.__class__.__name__ + '()' |
||
380 | if [ x for x in self.asNumbers() if x < 32 or x > 126 ]: |
||
381 | return self.__class__.__name__ + '(hexValue=\'' + ''.join([ '%x' % x for x in self.asNumbers() ])+'\')' |
||
382 | else: |
||
383 | return self.__class__.__name__ + '(' + self.prettyOut(self._value) + ')' |
||
384 | |||
385 | if sys.version_info[0] <= 2: |
||
386 | def __str__(self): return self._value |
||
387 | def __unicode__(self): |
||
388 | return self._value.decode(self._encoding, 'ignore') |
||
389 | def asOctets(self): return self._value |
||
390 | def asNumbers(self): |
||
391 | if self.__intValue is None: |
||
392 | self.__intValue = tuple([ ord(x) for x in self._value ]) |
||
393 | return self.__intValue |
||
394 | else: |
||
395 | def __str__(self): return self._value.decode(self._encoding, 'ignore') |
||
396 | def __bytes__(self): return self._value |
||
397 | def asOctets(self): return self._value |
||
398 | def asNumbers(self): |
||
399 | if self.__intValue is None: |
||
400 | self.__intValue = tuple(self._value) |
||
401 | return self.__intValue |
||
402 | |||
403 | # Immutable sequence object protocol |
||
404 | |||
405 | def __len__(self): |
||
406 | if self._len is None: |
||
407 | self._len = len(self._value) |
||
408 | return self._len |
||
409 | def __getitem__(self, i): |
||
410 | if isinstance(i, slice): |
||
411 | return self.clone(operator.getitem(self._value, i)) |
||
412 | else: |
||
413 | return self._value[i] |
||
414 | |||
415 | def __add__(self, value): return self.clone(self._value + self.prettyIn(value)) |
||
416 | def __radd__(self, value): return self.clone(self.prettyIn(value) + self._value) |
||
417 | def __mul__(self, value): return self.clone(self._value * value) |
||
418 | def __rmul__(self, value): return self * value |
||
419 | |||
420 | class Null(OctetString): |
||
421 | defaultValue = ''.encode() # This is tightly constrained |
||
422 | tagSet = baseTagSet = tag.initTagSet( |
||
423 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x05) |
||
424 | ) |
||
425 | subtypeSpec = OctetString.subtypeSpec+constraint.SingleValueConstraint(''.encode()) |
||
426 | |||
427 | if sys.version_info[0] <= 2: |
||
428 | intTypes = (int, long) |
||
429 | else: |
||
430 | intTypes = int |
||
431 | |||
432 | class ObjectIdentifier(base.AbstractSimpleAsn1Item): |
||
433 | tagSet = baseTagSet = tag.initTagSet( |
||
434 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x06) |
||
435 | ) |
||
436 | def __add__(self, other): return self.clone(self._value + other) |
||
437 | def __radd__(self, other): return self.clone(other + self._value) |
||
438 | |||
439 | def asTuple(self): return self._value |
||
440 | |||
441 | # Sequence object protocol |
||
442 | |||
443 | def __len__(self): |
||
444 | if self._len is None: |
||
445 | self._len = len(self._value) |
||
446 | return self._len |
||
447 | def __getitem__(self, i): |
||
448 | if isinstance(i, slice): |
||
449 | return self.clone( |
||
450 | operator.getitem(self._value, i) |
||
451 | ) |
||
452 | else: |
||
453 | return self._value[i] |
||
454 | |||
455 | def index(self, suboid): return self._value.index(suboid) |
||
456 | |||
457 | def isPrefixOf(self, value): |
||
458 | """Returns true if argument OID resides deeper in the OID tree""" |
||
459 | l = len(self) |
||
460 | if l <= len(value): |
||
461 | if self._value[:l] == value[:l]: |
||
462 | return 1 |
||
463 | return 0 |
||
464 | |||
465 | def prettyIn(self, value): |
||
466 | """Dotted -> tuple of numerics OID converter""" |
||
467 | if isinstance(value, tuple): |
||
468 | pass |
||
469 | elif isinstance(value, ObjectIdentifier): |
||
470 | return tuple(value) |
||
471 | elif isinstance(value, str): |
||
472 | r = [] |
||
473 | for element in [ x for x in value.split('.') if x != '' ]: |
||
474 | try: |
||
475 | r.append(int(element, 0)) |
||
476 | except ValueError: |
||
477 | raise error.PyAsn1Error( |
||
478 | 'Malformed Object ID %s at %s: %s' % |
||
479 | (str(value), self.__class__.__name__, sys.exc_info()[1]) |
||
480 | ) |
||
481 | value = tuple(r) |
||
482 | |||
483 | pass |
||
484 | else: |
||
485 | value = tuple(value) |
||
486 | |||
487 | for x in value: |
||
488 | if not isinstance(x, intTypes) or x < 0: |
||
489 | raise error.PyAsn1Error( |
||
490 | 'Invalid sub-ID in %s at %s' % (value, self.__class__.__name__) |
||
491 | ) |
||
492 | |||
493 | return value |
||
494 | |||
495 | def prettyOut(self, value): |
||
496 | r = [] |
||
497 | for subOid in value: |
||
498 | r.append(str(subOid)) |
||
499 | if r[-1] and r[-1][-1] == 'L': |
||
500 | r[-1][-1] = r[-1][:-1] |
||
501 | return '.'.join(r) |
||
502 | |||
503 | class Real(base.AbstractSimpleAsn1Item): |
||
504 | try: |
||
505 | _plusInf = float('inf') |
||
506 | _minusInf = float('-inf') |
||
507 | _inf = (_plusInf, _minusInf) |
||
508 | except ValueError: |
||
509 | # Infinity support is platform and Python dependent |
||
510 | _plusInf = _minusInf = None |
||
511 | _inf = () |
||
512 | |||
513 | tagSet = baseTagSet = tag.initTagSet( |
||
514 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x09) |
||
515 | ) |
||
516 | |||
517 | def __normalizeBase10(self, value): |
||
518 | m, b, e = value |
||
519 | while m and m % 10 == 0: |
||
520 | m = m / 10 |
||
521 | e = e + 1 |
||
522 | return m, b, e |
||
523 | |||
524 | def prettyIn(self, value): |
||
525 | if isinstance(value, tuple) and len(value) == 3: |
||
526 | for d in value: |
||
527 | if not isinstance(d, intTypes): |
||
528 | raise error.PyAsn1Error( |
||
529 | 'Lame Real value syntax: %s' % (value,) |
||
530 | ) |
||
531 | if value[1] not in (2, 10): |
||
532 | raise error.PyAsn1Error( |
||
533 | 'Prohibited base for Real value: %s' % value[1] |
||
534 | ) |
||
535 | if value[1] == 10: |
||
536 | value = self.__normalizeBase10(value) |
||
537 | return value |
||
538 | elif isinstance(value, intTypes): |
||
539 | return self.__normalizeBase10((value, 10, 0)) |
||
540 | elif isinstance(value, float): |
||
541 | if self._inf and value in self._inf: |
||
542 | return value |
||
543 | else: |
||
544 | e = 0 |
||
545 | while int(value) != value: |
||
546 | value = value * 10 |
||
547 | e = e - 1 |
||
548 | return self.__normalizeBase10((int(value), 10, e)) |
||
549 | elif isinstance(value, Real): |
||
550 | return tuple(value) |
||
551 | elif isinstance(value, str): # handle infinite literal |
||
552 | try: |
||
553 | return float(value) |
||
554 | except ValueError: |
||
555 | pass |
||
556 | raise error.PyAsn1Error( |
||
557 | 'Bad real value syntax: %s' % (value,) |
||
558 | ) |
||
559 | |||
560 | def prettyOut(self, value): |
||
561 | if value in self._inf: |
||
562 | return '\'%s\'' % value |
||
563 | else: |
||
564 | return str(value) |
||
565 | |||
566 | def isPlusInfinity(self): return self._value == self._plusInf |
||
567 | def isMinusInfinity(self): return self._value == self._minusInf |
||
568 | def isInfinity(self): return self._value in self._inf |
||
569 | |||
570 | def __str__(self): return str(float(self)) |
||
571 | |||
572 | def __add__(self, value): return self.clone(float(self) + value) |
||
573 | def __radd__(self, value): return self + value |
||
574 | def __mul__(self, value): return self.clone(float(self) * value) |
||
575 | def __rmul__(self, value): return self * value |
||
576 | def __sub__(self, value): return self.clone(float(self) - value) |
||
577 | def __rsub__(self, value): return self.clone(value - float(self)) |
||
578 | def __mod__(self, value): return self.clone(float(self) % value) |
||
579 | def __rmod__(self, value): return self.clone(value % float(self)) |
||
580 | def __pow__(self, value, modulo=None): return self.clone(pow(float(self), value, modulo)) |
||
581 | def __rpow__(self, value): return self.clone(pow(value, float(self))) |
||
582 | |||
583 | if sys.version_info[0] <= 2: |
||
584 | def __div__(self, value): return self.clone(float(self) / value) |
||
585 | def __rdiv__(self, value): return self.clone(value / float(self)) |
||
586 | else: |
||
587 | def __truediv__(self, value): return self.clone(float(self) / value) |
||
588 | def __rtruediv__(self, value): return self.clone(value / float(self)) |
||
589 | def __divmod__(self, value): return self.clone(float(self) // value) |
||
590 | def __rdivmod__(self, value): return self.clone(value // float(self)) |
||
591 | |||
592 | def __int__(self): return int(float(self)) |
||
593 | def __long__(self): return int(float(self)) |
||
594 | def __float__(self): |
||
595 | if self._value in self._inf: |
||
596 | return self._value |
||
597 | else: |
||
598 | return float( |
||
599 | self._value[0] * pow(self._value[1], self._value[2]) |
||
600 | ) |
||
601 | def __abs__(self): return abs(float(self)) |
||
602 | |||
603 | def __lt__(self, value): return float(self) < value |
||
604 | def __le__(self, value): return float(self) <= value |
||
605 | def __eq__(self, value): return float(self) == value |
||
606 | def __ne__(self, value): return float(self) != value |
||
607 | def __gt__(self, value): return float(self) > value |
||
608 | def __ge__(self, value): return float(self) >= value |
||
609 | |||
610 | if sys.version_info[0] <= 2: |
||
611 | def __nonzero__(self): return bool(float(self)) |
||
612 | else: |
||
613 | def __bool__(self): return bool(float(self)) |
||
614 | __hash__ = base.AbstractSimpleAsn1Item.__hash__ |
||
615 | |||
616 | def __getitem__(self, idx): |
||
617 | if self._value in self._inf: |
||
618 | raise error.PyAsn1Error('Invalid infinite value operation') |
||
619 | else: |
||
620 | return self._value[idx] |
||
621 | |||
622 | class Enumerated(Integer): |
||
623 | tagSet = baseTagSet = tag.initTagSet( |
||
624 | tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x0A) |
||
625 | ) |
||
626 | |||
627 | # "Structured" ASN.1 types |
||
628 | |||
629 | class SetOf(base.AbstractConstructedAsn1Item): |
||
630 | componentType = None |
||
631 | tagSet = baseTagSet = tag.initTagSet( |
||
632 | tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) |
||
633 | ) |
||
634 | typeId = 1 |
||
635 | |||
636 | View Code Duplication | def _cloneComponentValues(self, myClone, cloneValueFlag): |
|
637 | idx = 0; l = len(self._componentValues) |
||
638 | while idx < l: |
||
639 | c = self._componentValues[idx] |
||
640 | if c is not None: |
||
641 | if isinstance(c, base.AbstractConstructedAsn1Item): |
||
642 | myClone.setComponentByPosition( |
||
643 | idx, c.clone(cloneValueFlag=cloneValueFlag) |
||
644 | ) |
||
645 | else: |
||
646 | myClone.setComponentByPosition(idx, c.clone()) |
||
647 | idx = idx + 1 |
||
648 | |||
649 | def _verifyComponent(self, idx, value): |
||
650 | if self._componentType is not None and \ |
||
651 | not self._componentType.isSuperTypeOf(value): |
||
652 | raise error.PyAsn1Error('Component type error %s' % value) |
||
653 | |||
654 | def getComponentByPosition(self, idx): return self._componentValues[idx] |
||
655 | def setComponentByPosition(self, idx, value=None, verifyConstraints=True): |
||
656 | l = len(self._componentValues) |
||
657 | if idx >= l: |
||
658 | self._componentValues = self._componentValues + (idx-l+1)*[None] |
||
659 | if value is None: |
||
660 | if self._componentValues[idx] is None: |
||
661 | if self._componentType is None: |
||
662 | raise error.PyAsn1Error('Component type not defined') |
||
663 | self._componentValues[idx] = self._componentType.clone() |
||
664 | self._componentValuesSet = self._componentValuesSet + 1 |
||
665 | return self |
||
666 | elif not isinstance(value, base.Asn1Item): |
||
667 | if self._componentType is None: |
||
668 | raise error.PyAsn1Error('Component type not defined') |
||
669 | if isinstance(self._componentType, base.AbstractSimpleAsn1Item): |
||
670 | value = self._componentType.clone(value=value) |
||
671 | else: |
||
672 | raise error.PyAsn1Error('Instance value required') |
||
673 | if verifyConstraints: |
||
674 | if self._componentType is not None: |
||
675 | self._verifyComponent(idx, value) |
||
676 | self._verifySubtypeSpec(value, idx) |
||
677 | if self._componentValues[idx] is None: |
||
678 | self._componentValuesSet = self._componentValuesSet + 1 |
||
679 | self._componentValues[idx] = value |
||
680 | return self |
||
681 | |||
682 | def getComponentTagMap(self): |
||
683 | if self._componentType is not None: |
||
684 | return self._componentType.getTagMap() |
||
685 | |||
686 | def prettyPrint(self, scope=0): |
||
687 | scope = scope + 1 |
||
688 | r = self.__class__.__name__ + ':\n' |
||
689 | for idx in range(len(self._componentValues)): |
||
690 | r = r + ' '*scope |
||
691 | if self._componentValues[idx] is None: |
||
692 | r = r + '<empty>' |
||
693 | else: |
||
694 | r = r + self._componentValues[idx].prettyPrint(scope) |
||
695 | return r |
||
696 | |||
697 | class SequenceOf(SetOf): |
||
698 | tagSet = baseTagSet = tag.initTagSet( |
||
699 | tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10) |
||
700 | ) |
||
701 | typeId = 2 |
||
702 | |||
703 | class SequenceAndSetBase(base.AbstractConstructedAsn1Item): |
||
704 | componentType = namedtype.NamedTypes() |
||
705 | def __init__(self, componentType=None, tagSet=None, |
||
706 | subtypeSpec=None, sizeSpec=None): |
||
707 | base.AbstractConstructedAsn1Item.__init__( |
||
708 | self, componentType, tagSet, subtypeSpec, sizeSpec |
||
709 | ) |
||
710 | if self._componentType is None: |
||
711 | self._componentTypeLen = 0 |
||
712 | else: |
||
713 | self._componentTypeLen = len(self._componentType) |
||
714 | |||
715 | def __getitem__(self, idx): |
||
716 | if isinstance(idx, str): |
||
717 | return self.getComponentByName(idx) |
||
718 | else: |
||
719 | return base.AbstractConstructedAsn1Item.__getitem__(self, idx) |
||
720 | |||
721 | def __setitem__(self, idx, value): |
||
722 | if isinstance(idx, str): |
||
723 | self.setComponentByName(idx, value) |
||
724 | else: |
||
725 | base.AbstractConstructedAsn1Item.__setitem__(self, idx, value) |
||
726 | |||
727 | View Code Duplication | def _cloneComponentValues(self, myClone, cloneValueFlag): |
|
728 | idx = 0; l = len(self._componentValues) |
||
729 | while idx < l: |
||
730 | c = self._componentValues[idx] |
||
731 | if c is not None: |
||
732 | if isinstance(c, base.AbstractConstructedAsn1Item): |
||
733 | myClone.setComponentByPosition( |
||
734 | idx, c.clone(cloneValueFlag=cloneValueFlag) |
||
735 | ) |
||
736 | else: |
||
737 | myClone.setComponentByPosition(idx, c.clone()) |
||
738 | idx = idx + 1 |
||
739 | |||
740 | def _verifyComponent(self, idx, value): |
||
741 | if idx >= self._componentTypeLen: |
||
742 | raise error.PyAsn1Error( |
||
743 | 'Component type error out of range' |
||
744 | ) |
||
745 | t = self._componentType[idx].getType() |
||
746 | if not t.isSuperTypeOf(value): |
||
747 | raise error.PyAsn1Error('Component type error %s vs %s' % |
||
748 | (repr(t), repr(value))) |
||
749 | |||
750 | def getComponentByName(self, name): |
||
751 | return self.getComponentByPosition( |
||
752 | self._componentType.getPositionByName(name) |
||
753 | ) |
||
754 | def setComponentByName(self, name, value=None, verifyConstraints=True): |
||
755 | return self.setComponentByPosition( |
||
756 | self._componentType.getPositionByName(name), value, |
||
757 | verifyConstraints |
||
758 | ) |
||
759 | |||
760 | def getComponentByPosition(self, idx): |
||
761 | try: |
||
762 | return self._componentValues[idx] |
||
763 | except IndexError: |
||
764 | if idx < self._componentTypeLen: |
||
765 | return |
||
766 | raise |
||
767 | def setComponentByPosition(self, idx, value=None, verifyConstraints=True): |
||
768 | l = len(self._componentValues) |
||
769 | if idx >= l: |
||
770 | self._componentValues = self._componentValues + (idx-l+1)*[None] |
||
771 | if value is None: |
||
772 | if self._componentValues[idx] is None: |
||
773 | self._componentValues[idx] = self._componentType.getTypeByPosition(idx).clone() |
||
774 | self._componentValuesSet = self._componentValuesSet + 1 |
||
775 | return self |
||
776 | elif not isinstance(value, base.Asn1Item): |
||
777 | t = self._componentType.getTypeByPosition(idx) |
||
778 | if isinstance(t, base.AbstractSimpleAsn1Item): |
||
779 | value = t.clone(value=value) |
||
780 | else: |
||
781 | raise error.PyAsn1Error('Instance value required') |
||
782 | if verifyConstraints: |
||
783 | if self._componentTypeLen: |
||
784 | self._verifyComponent(idx, value) |
||
785 | self._verifySubtypeSpec(value, idx) |
||
786 | if self._componentValues[idx] is None: |
||
787 | self._componentValuesSet = self._componentValuesSet + 1 |
||
788 | self._componentValues[idx] = value |
||
789 | return self |
||
790 | |||
791 | def getNameByPosition(self, idx): |
||
792 | if self._componentTypeLen: |
||
793 | return self._componentType.getNameByPosition(idx) |
||
794 | |||
795 | def getDefaultComponentByPosition(self, idx): |
||
796 | if self._componentTypeLen and self._componentType[idx].isDefaulted: |
||
797 | return self._componentType[idx].getType() |
||
798 | |||
799 | def getComponentType(self): |
||
800 | if self._componentTypeLen: |
||
801 | return self._componentType |
||
802 | |||
803 | def setDefaultComponents(self): |
||
804 | if self._componentTypeLen == self._componentValuesSet: |
||
805 | return |
||
806 | idx = self._componentTypeLen |
||
807 | while idx: |
||
808 | idx = idx - 1 |
||
809 | if self._componentType[idx].isDefaulted: |
||
810 | if self.getComponentByPosition(idx) is None: |
||
811 | self.setComponentByPosition(idx) |
||
812 | elif not self._componentType[idx].isOptional: |
||
813 | if self.getComponentByPosition(idx) is None: |
||
814 | raise error.PyAsn1Error( |
||
815 | 'Uninitialized component #%s at %s' % (idx, repr(self)) |
||
816 | ) |
||
817 | |||
818 | def prettyPrint(self, scope=0): |
||
819 | scope = scope + 1 |
||
820 | r = self.__class__.__name__ + ':\n' |
||
821 | for idx in range(len(self._componentValues)): |
||
822 | if self._componentValues[idx] is not None: |
||
823 | r = r + ' '*scope |
||
824 | componentType = self.getComponentType() |
||
825 | if componentType is None: |
||
826 | r = r + '<no-name>' |
||
827 | else: |
||
828 | r = r + componentType.getNameByPosition(idx) |
||
829 | r = '%s=%s\n' % ( |
||
830 | r, self._componentValues[idx].prettyPrint(scope) |
||
831 | ) |
||
832 | return r |
||
833 | |||
834 | class Sequence(SequenceAndSetBase): |
||
835 | tagSet = baseTagSet = tag.initTagSet( |
||
836 | tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10) |
||
837 | ) |
||
838 | typeId = 3 |
||
839 | |||
840 | def getComponentTagMapNearPosition(self, idx): |
||
841 | if self._componentType: |
||
842 | return self._componentType.getTagMapNearPosition(idx) |
||
843 | |||
844 | def getComponentPositionNearType(self, tagSet, idx): |
||
845 | if self._componentType: |
||
846 | return self._componentType.getPositionNearType(tagSet, idx) |
||
847 | else: |
||
848 | return idx |
||
849 | |||
850 | class Set(SequenceAndSetBase): |
||
851 | tagSet = baseTagSet = tag.initTagSet( |
||
852 | tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) |
||
853 | ) |
||
854 | typeId = 4 |
||
855 | |||
856 | def getComponent(self, innerFlag=0): return self |
||
857 | |||
858 | def getComponentByType(self, tagSet, innerFlag=0): |
||
859 | c = self.getComponentByPosition( |
||
860 | self._componentType.getPositionByType(tagSet) |
||
861 | ) |
||
862 | if innerFlag and isinstance(c, Set): |
||
863 | # get inner component by inner tagSet |
||
864 | return c.getComponent(1) |
||
865 | else: |
||
866 | # get outer component by inner tagSet |
||
867 | return c |
||
868 | |||
869 | def setComponentByType(self, tagSet, value=None, innerFlag=0, |
||
870 | verifyConstraints=True): |
||
871 | idx = self._componentType.getPositionByType(tagSet) |
||
872 | t = self._componentType.getTypeByPosition(idx) |
||
873 | if innerFlag: # set inner component by inner tagSet |
||
874 | if t.getTagSet(): |
||
875 | return self.setComponentByPosition( |
||
876 | idx, value, verifyConstraints |
||
877 | ) |
||
878 | else: |
||
879 | t = self.setComponentByPosition(idx).getComponentByPosition(idx) |
||
880 | return t.setComponentByType( |
||
881 | tagSet, value, innerFlag, verifyConstraints |
||
882 | ) |
||
883 | else: # set outer component by inner tagSet |
||
884 | return self.setComponentByPosition( |
||
885 | idx, value, verifyConstraints |
||
886 | ) |
||
887 | |||
888 | def getComponentTagMap(self): |
||
889 | if self._componentType: |
||
890 | return self._componentType.getTagMap(True) |
||
891 | |||
892 | def getComponentPositionByType(self, tagSet): |
||
893 | if self._componentType: |
||
894 | return self._componentType.getPositionByType(tagSet) |
||
895 | |||
896 | class Choice(Set): |
||
897 | tagSet = baseTagSet = tag.TagSet() # untagged |
||
898 | sizeSpec = constraint.ConstraintsIntersection( |
||
899 | constraint.ValueSizeConstraint(1, 1) |
||
900 | ) |
||
901 | typeId = 5 |
||
902 | _currentIdx = None |
||
903 | |||
904 | def __eq__(self, other): |
||
905 | if self._componentValues: |
||
906 | return self._componentValues[self._currentIdx] == other |
||
907 | return NotImplemented |
||
908 | def __ne__(self, other): |
||
909 | if self._componentValues: |
||
910 | return self._componentValues[self._currentIdx] != other |
||
911 | return NotImplemented |
||
912 | def __lt__(self, other): |
||
913 | if self._componentValues: |
||
914 | return self._componentValues[self._currentIdx] < other |
||
915 | return NotImplemented |
||
916 | def __le__(self, other): |
||
917 | if self._componentValues: |
||
918 | return self._componentValues[self._currentIdx] <= other |
||
919 | return NotImplemented |
||
920 | def __gt__(self, other): |
||
921 | if self._componentValues: |
||
922 | return self._componentValues[self._currentIdx] > other |
||
923 | return NotImplemented |
||
924 | def __ge__(self, other): |
||
925 | if self._componentValues: |
||
926 | return self._componentValues[self._currentIdx] >= other |
||
927 | return NotImplemented |
||
928 | if sys.version_info[0] <= 2: |
||
929 | def __nonzero__(self, other): return bool(self._componentValues) |
||
930 | else: |
||
931 | def __bool__(self, other): return bool(self._componentValues) |
||
932 | |||
933 | def __len__(self): return self._currentIdx is not None and 1 or 0 |
||
934 | |||
935 | def verifySizeSpec(self): |
||
936 | if self._currentIdx is None: |
||
937 | raise error.PyAsn1Error('Component not chosen') |
||
938 | else: |
||
939 | self._sizeSpec(' ') |
||
940 | |||
941 | def _cloneComponentValues(self, myClone, cloneValueFlag): |
||
942 | try: |
||
943 | c = self.getComponent() |
||
944 | except error.PyAsn1Error: |
||
945 | pass |
||
946 | else: |
||
947 | if isinstance(c, Choice): |
||
948 | tagSet = c.getEffectiveTagSet() |
||
949 | else: |
||
950 | tagSet = c.getTagSet() |
||
951 | if isinstance(c, base.AbstractConstructedAsn1Item): |
||
952 | myClone.setComponentByType( |
||
953 | tagSet, c.clone(cloneValueFlag=cloneValueFlag) |
||
954 | ) |
||
955 | else: |
||
956 | myClone.setComponentByType(tagSet, c.clone()) |
||
957 | |||
958 | def setComponentByPosition(self, idx, value=None, verifyConstraints=True): |
||
959 | l = len(self._componentValues) |
||
960 | if idx >= l: |
||
961 | self._componentValues = self._componentValues + (idx-l+1)*[None] |
||
962 | if self._currentIdx is not None: |
||
963 | self._componentValues[self._currentIdx] = None |
||
964 | if value is None: |
||
965 | if self._componentValues[idx] is None: |
||
966 | self._componentValues[idx] = self._componentType.getTypeByPosition(idx).clone() |
||
967 | self._componentValuesSet = 1 |
||
968 | self._currentIdx = idx |
||
969 | return self |
||
970 | elif not isinstance(value, base.Asn1Item): |
||
971 | value = self._componentType.getTypeByPosition(idx).clone( |
||
972 | value=value |
||
973 | ) |
||
974 | if verifyConstraints: |
||
975 | if self._componentTypeLen: |
||
976 | self._verifyComponent(idx, value) |
||
977 | self._verifySubtypeSpec(value, idx) |
||
978 | self._componentValues[idx] = value |
||
979 | self._currentIdx = idx |
||
980 | self._componentValuesSet = 1 |
||
981 | return self |
||
982 | |||
983 | def getMinTagSet(self): |
||
984 | if self._tagSet: |
||
985 | return self._tagSet |
||
986 | else: |
||
987 | return self._componentType.genMinTagSet() |
||
988 | |||
989 | def getEffectiveTagSet(self): |
||
990 | if self._tagSet: |
||
991 | return self._tagSet |
||
992 | else: |
||
993 | c = self.getComponent() |
||
994 | if isinstance(c, Choice): |
||
995 | return c.getEffectiveTagSet() |
||
996 | else: |
||
997 | return c.getTagSet() |
||
998 | |||
999 | def getTagMap(self): |
||
1000 | if self._tagSet: |
||
1001 | return Set.getTagMap(self) |
||
1002 | else: |
||
1003 | return Set.getComponentTagMap(self) |
||
1004 | |||
1005 | def getComponent(self, innerFlag=0): |
||
1006 | if self._currentIdx is None: |
||
1007 | raise error.PyAsn1Error('Component not chosen') |
||
1008 | else: |
||
1009 | c = self._componentValues[self._currentIdx] |
||
1010 | if innerFlag and isinstance(c, Choice): |
||
1011 | return c.getComponent(innerFlag) |
||
1012 | else: |
||
1013 | return c |
||
1014 | |||
1015 | def getName(self, innerFlag=0): |
||
1016 | if self._currentIdx is None: |
||
1017 | raise error.PyAsn1Error('Component not chosen') |
||
1018 | else: |
||
1019 | if innerFlag: |
||
1020 | c = self._componentValues[self._currentIdx] |
||
1021 | if isinstance(c, Choice): |
||
1022 | return c.getName(innerFlag) |
||
1023 | return self._componentType.getNameByPosition(self._currentIdx) |
||
1024 | |||
1025 | def setDefaultComponents(self): pass |
||
1026 | |||
1027 | class Any(OctetString): |
||
1028 | tagSet = baseTagSet = tag.TagSet() # untagged |
||
1029 | typeId = 6 |
||
1030 | |||
1031 | def getTagMap(self): |
||
1032 | return tagmap.TagMap( |
||
1033 | { self.getTagSet(): self }, |
||
1034 | { eoo.endOfOctets.getTagSet(): eoo.endOfOctets }, |
||
1035 | self |
||
1036 | ) |
||
1040 |