class BooleanEncoder(AbstractItemEncoder): supportIndefLenMode = 0 _true = ints2octs((1, )) _false = ints2octs((0, )) def encodeValue(self, encodeFun, value, defMode, maxChunkSize): return value and self._true or self._false, 0
def encodeValue(self, encodeFun, value, defMode, maxChunkSize): oid = value.asTuple() if oid[:5] in self.precomputedValues: octets = self.precomputedValues[oid[:5]] index = 5 else: if len(oid) < 2: raise error.PyAsn1Error('Short OID %s' % (value, )) # Build the first twos if oid[0] > 6 or oid[1] > 39 or oid[0] == 6 and oid[1] > 15: raise error.PyAsn1Error( 'Initial sub-ID overflow %s in OID %s' % (oid[:2], value)) octets = (oid[0] * 40 + oid[1], ) index = 2 # Cycle through subids for subid in oid[index:]: if subid > -1 and subid < 128: # Optimize for the common case octets = octets + (subid & 0x7f, ) elif subid < 0 or subid > 0xFFFFFFFF: raise error.PyAsn1Error('SubId overflow %s in %s' % (subid, value)) else: # Pack large Sub-Object IDs res = (subid & 0x7f, ) subid = subid >> 7 while subid > 0: res = (0x80 | (subid & 0x7f), ) + res subid = subid >> 7 # Add packed Sub-Object ID to resulted Object ID octets += res return ints2octs(octets), 0
def fromHexString(self, value): r = p = () for v in value: if p: r = r + (int(p + v, 16), ) p = () else: p = v if p: r = r + (int(p + '0', 16), ) return octets.ints2octs(r)
def encodeValue(self, encodeFun, value, defMode, maxChunkSize): if value == 0: # shortcut for zero value if self.supportCompactZero: # this seems to be a correct way for encoding zeros return null, 0 else: # this seems to be a widespread way for encoding zeros return ints2octs((0, )), 0 octets = [] value = int(value) # to save on ops on asn1 type while 1: octets.insert(0, value & 0xff) if value == 0 or value == -1: break value = value >> 8 if value == 0 and octets[0] & 0x80: octets.insert(0, 0) while len(octets) > 1 and \ (octets[0] == 0 and octets[1] & 0x80 == 0 or \ octets[0] == 0xff and octets[1] & 0x80 != 0): del octets[0] return ints2octs(octets), 0
def fromBinaryString(self, value): bitNo = 8 byte = 0 r = () for v in value: if bitNo: bitNo = bitNo - 1 else: bitNo = 7 r = r + (byte, ) byte = 0 if v == '0': v = 0 elif v == '1': v = 1 else: raise error.PyAsn1Error( 'Non-binary OCTET STRING initializer %s' % (v, )) byte = byte | (v << bitNo) return octets.ints2octs(r + (byte, ))
def encodeValue(self, encodeFun, value, defMode, maxChunkSize): if not maxChunkSize or len(value) <= maxChunkSize * 8: r = {} l = len(value) p = 0 j = 7 while p < l: i, j = divmod(p, 8) r[i] = r.get(i, 0) | value[p] << (7 - j) p = p + 1 keys = list(r) keys.sort() return int2oct(7 - j) + ints2octs([r[k] for k in keys]), 0 else: pos = 0 substrate = null while 1: # count in octets v = value.clone(value[pos * 8:pos * 8 + maxChunkSize * 8]) if not v: break substrate = substrate + encodeFun(v, defMode, maxChunkSize) pos = pos + maxChunkSize return substrate, 1