MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / encodeValue

Method encodeValue

pager_lib/pyasn1/codec/ber/encoder.py:452–551  ·  view source on GitHub ↗
(self, value, asn1Spec, encodeFun, **options)

Source from the content-addressed store, hash-verified

450 return sign, m, encbase, e
451
452 def encodeValue(self, value, asn1Spec, encodeFun, **options):
453 if asn1Spec is not None:
454 value = asn1Spec.clone(value)
455
456 if value.isPlusInf:
457 return (0x40,), False, False
458
459 if value.isMinusInf:
460 return (0x41,), False, False
461
462 m, b, e = value
463
464 if not m:
465 return b'', False, True
466
467 if b == 10:
468 if LOG:
469 LOG('encoding REAL into character form')
470
471 return b'\x03%dE%s%d' % (m, e == 0 and b'+' or b'', e), False, True
472
473 elif b == 2:
474 fo = 0x80 # binary encoding
475 ms, m, encbase, e = self._chooseEncBase(value)
476
477 if ms < 0: # mantissa sign
478 fo |= 0x40 # sign bit
479
480 # exponent & mantissa normalization
481 if encbase == 2:
482 while m & 0x1 == 0:
483 m >>= 1
484 e += 1
485
486 elif encbase == 8:
487 while m & 0x7 == 0:
488 m >>= 3
489 e += 1
490 fo |= 0x10
491
492 else: # encbase = 16
493 while m & 0xf == 0:
494 m >>= 4
495 e += 1
496 fo |= 0x20
497
498 sf = 0 # scale factor
499
500 while m & 0x1 == 0:
501 m >>= 1
502 sf += 1
503
504 if sf > 3:
505 raise error.PyAsn1Error('Scale factor overflow') # bug if raised
506
507 fo |= sf << 2
508 eo = b''
509 if e == 0 or e == -1:

Callers

nothing calls this directly

Calls 2

_chooseEncBaseMethod · 0.95
cloneMethod · 0.45

Tested by

no test coverage detected