def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): tdesc = annot.container or tdesc if issubclass(tdesc.type(), java.util.Collection): edesc = annot.elements or tdesc.resolve_targs( java.util.Collection)['E'] else: raise TypeError( "Can't encode using type %r on repeated field" % (tdesc, )) n = PacketCodec.build_chain_for_type(pktCls, fld, edesc, codec, chain) class repeated_field_codec: def encode_field(self, buf, pkt, val): for e in val: n.encode_field(buf, pkt, e) def decode_field(self, pkt, buf, count, factory): it = itertools.count() if count is None else xrange( count) t = tdesc.type() result = factory.new_collection(t) for i in it: # @UnusedVariable if codec.measure_decode_remaining(buf) == 0: break t.add_to(result, n.decode_field(pkt, buf, None, factory)) return result return repeated_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): nextMap = OrderedDict() for td in annot.types: t = td.type() if issubclass(t, Packet): codec.register_packet_cls(t) nextMap[t] = PacketCodec.build_chain_for_type( pktCls, fld, td, codec, copy(chain)) class typed_field_codec: def encode_field(self, buf, pkt, val): nextMap[val.__class__].encode_field(buf, pkt, val) def decode_field(self, pkt, buf, count, factory): errs = OrderedDict() backup = codec.backup(buf) for t, n in nextMap.items(): try: return n.decode_field(pkt, buf, count, factory) except Exception as e: codec.restore(buf, backup) errs[t] = e.message raise PacketDecodeError( "No selectable lookahead type succeeded " + "for %r: %r" % (fld, errs)) return typed_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type( pktCls, fld, tdesc, codec, chain) class terminated_field_codec: def encode_field(self, buf, pkt, val): n.encode_field(buf, pkt, val) if annot.cond != '': if getattr(pkt, annot.cond) is None: return buf.buf.extend(annot.tok) def decode_field(self, pkt, buf, count, factory): i = buf.buf.find(annot.tok) if i == -1: if annot.cond != '': return n.decode_field( pkt, buf, count, factory) elif len(buf.buf) == 0: raise PacketDecodeError("Buffer is empty") else: raise PacketDecodeError( "Missing terminator sequence %r" % (annot.tok)) part = BinaryBuffer(buf.buf[:i]) buf.buf[:] = buf.buf[i + len(annot.tok):] ret = n.decode_field(pkt, part, count, factory) return ret return terminated_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) injector = self class flags_field_codec: def encode_field(self, buf, pkt, val): flagged_val = pkt._vals[annot.field.name] if val is None: val = BitmaskSet(injector.universe, 0) setattr(pkt, annot.by, val) if (flagged_val is not None) ^ (annot.mode != WithFlag_Mode.PRESENT): val.add(injector.flag) else: val.discard(injector.flag) return n.encode_field(buf, pkt, val) def decode_field(self, buf, pkt, count, factory): return n.decode_field(pkt, buf, count, factory) return flags_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): byField = pktCls._fields_by_name[annot.by] bme = BitmaskEncoded.findOnField(byField) universe = bme.universe.type() codec.inject_chain(pktCls, byField, InjectedFlags(universe), fld, annot) flag = getattr(universe, annot.flag) n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class flagged_field_codec: def encode_field(self, buf, pkt, val): if val is not None: n.encode_field(buf, pkt, val) def decode_field(self, pkt, buf, count, factory): flags = pkt._vals[annot.by] if (flag in flags) ^ (annot.mode != WithFlag_Mode.PRESENT): return n.decode_field(pkt, buf, count, factory) else: return None return flagged_field_codec()
def get_field_codec_for_type(self, pktCls, fld, tdesc): if issubclass(tdesc.type(), Packet): return PacketCodec.get_field_codec_for_type( self, pktCls, fld, tdesc) elif tdesc == bytearray_t: class bytearray_field_codec: def encode_field(self, buf, pkt, val): buf.buf.extend(val) def decode_field(self, pkt, buf, count, factory): ret = bytearray(buf.buf) buf.buf[:] = '' return ret return bytearray_field_codec() else: class field_codec: def encode_field(self, buf, pkt, val): tdesc.encode_binary(buf, val) def decode_field(self, pkt, buf, count, factory): return tdesc.decode_binary(buf, count) return field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): codec.inject_chain(pktCls, pktCls._fields_by_name[annot.by], InjectedCounter(), fld, annot) n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class counted_field_codec: def encode_field(self, buf, pkt, val): n.encode_field(buf, pkt, val) def decode_field(self, pkt, buf, count, factory): count = pkt._vals[annot.by] return n.decode_field(pkt, buf, count, factory) return counted_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class counter_field_codec: def encode_field(self, buf, pkt, val): counted_val = pkt._vals[annot.field.name] count = codec.measure_count(counted_val) setattr(pkt, annot.by, count) n.encode_field(buf, pkt, count) def decode_field(self, pkt, buf, count, factory): return n.decode_field(pkt, buf, count, factory) return counter_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): if not issubclass(tdesc.type(), BitmaskSet): return None n = PacketCodec.build_chain_for_type(pktCls, fld, annot.type, codec, chain) class encoded_field_codec: def encode_field(self, buf, pkt, val): n.encode_field(buf, pkt, val.bitmask) def decode_field(self, pkt, buf, count, factory): return BitmaskSet( annot.universe.type(), n.decode_field(pkt, buf, count, factory)) return encoded_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class optional_field_codec: def encode_field(self, buf, pkt, val): if val is None: return n.encode_field(buf, pkt, val) def decode_field(self, pkt, buf, count, factory): if codec.measure_decode_remaining(buf) == 0: return None return n.decode_field(pkt, buf, count, factory) return optional_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): if not issubclass(tdesc.type(), java.lang.String): return None # Test that the charset exists ''.encode(self.charset) n = PacketCodec.build_chain_for_type(pktCls, fld, bytearray_t, chain) class encoded_field_codec: def encode_field(self, buf, pkt, val): n.encode_field(buf, pkt, val.encode(annot.charset)) def decode_field(self, pkt, buf, count, factory): return n.decode_field(pkt, buf, count, factory).decode(annot.charset) return encoded_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class sizer_field_codec: def encode_field(self, buf, pkt, val): sized_val = pkt._vals[annot.field.name] part = codec.new_encode_buffer() codec.encode_field(part, pkt, annot.field, sized_val) size = codec.measure_encode_size(part) annot.set_size(pkt, size) n.encode_field(buf, pkt, pkt._vals[annot.by]) def decode_field(self, pkt, buf, count, factory): return n.decode_field(pkt, buf, count, factory) return sizer_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type( pktCls, fld, tdesc, codec, chain) class byte_reversed_field_codec: def encode_field(self, buf, pkt, val): orig = buf.order() buf.order('<' if orig == '>' else '>') n.encode_field(buf, pkt, val) buf.order(orig) def decode_field(self, pkt, buf, count, factory): orig = buf.order() buf.order('<' if orig == '>' else '>') ret = n.decode_field(pkt, buf, count, factory) buf.order(orig) return ret return byte_reversed_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): codec.inject_chain(pktCls, pktCls._fields_by_name[annot.by], InjectedSizer(), fld, annot) n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class sized_field_codec: def encode_field(self, buf, pkt, val): n.encode_field(buf, pkt, val) def decode_field(self, pkt, buf, count, factory): size = annot.get_size(pkt) part = codec.split_buffer(buf, size) ret = n.decode_field(pkt, part, count, factory) if codec.measure_decode_remaining(part) != 0: # TODO: Consider padding raise ValueError("Gratuitous data in sized field") return ret return sized_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type(pktCls, fld, tdesc, codec, chain) class typer_field_codec: def encode_field(self, buf, pkt, val): typed_val = pkt._vals[annot.field.name] try: idx = annot.inv_map[typed_val.__class__] except KeyError as e: cls = annot.find_accepting_type(typed_val) if cls is None: raise e idx = annot.inv_map[cls] setattr(pkt, annot.by, idx) n.encode_field(buf, pkt, idx) def decode_field(self, pkt, buf, count, factory): return n.decode_field(pkt, buf, count, factory) return typer_field_codec()
def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): annot.fwd_map = OrderedDict() if annot.map != '': annot.fwd_map.update(getattr(pktCls, annot.map)) for ts in annot.types: annot.fwd_map[ts.key] = ts.tdesc annot.inv_map = OrderedDict() nextMap = OrderedDict() for k, v in annot.fwd_map.items(): t = v.type() annot.inv_map[t] = k if issubclass(t, Packet): codec.register_packet_cls(t) nextMap[t] = PacketCodec.build_chain_for_type( pktCls, fld, v, codec, copy(chain)) codec.inject_chain(pktCls, pktCls._fields_by_name[annot.by], InjectedTyper(), fld, annot) class typed_field_codec: def encode_field(self, buf, pkt, val): try: n = nextMap[val.__class__] except KeyError as e: cls = annot.find_accepting_type(val) if cls is None: raise e n = nextMap[cls] n.encode_field(buf, pkt, val) def decode_field(self, pkt, buf, count, factory): idx = pkt._vals[annot.by] tdesc = annot.fwd_map[idx] n = nextMap[tdesc.type()] return n.decode_field(pkt, buf, count, factory) return typed_field_codec()
def get_factory_for_annotation(self, pktCls, fld, annot): if isinstance(annot, SequenceTerminated): class Terminated: def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type( pktCls, fld, tdesc, codec, chain) class terminated_field_codec: def encode_field(self, buf, pkt, val): n.encode_field(buf, pkt, val) if annot.cond != '': if getattr(pkt, annot.cond) is None: return buf.buf.extend(annot.tok) def decode_field(self, pkt, buf, count, factory): i = buf.buf.find(annot.tok) if i == -1: if annot.cond != '': return n.decode_field( pkt, buf, count, factory) elif len(buf.buf) == 0: raise PacketDecodeError("Buffer is empty") else: raise PacketDecodeError( "Missing terminator sequence %r" % (annot.tok)) part = BinaryBuffer(buf.buf[:i]) buf.buf[:] = buf.buf[i + len(annot.tok):] ret = n.decode_field(pkt, part, count, factory) return ret return terminated_field_codec() return Terminated() elif isinstance(annot, ReverseByteOrder): class Reversed: def get_wrapped_field_codec_for_type(self, pktCls, fld, tdesc, codec, chain): n = PacketCodec.build_chain_for_type( pktCls, fld, tdesc, codec, chain) class byte_reversed_field_codec: def encode_field(self, buf, pkt, val): orig = buf.order() buf.order('<' if orig == '>' else '>') n.encode_field(buf, pkt, val) buf.order(orig) def decode_field(self, pkt, buf, count, factory): orig = buf.order() buf.order('<' if orig == '>' else '>') ret = n.decode_field(pkt, buf, count, factory) buf.order(orig) return ret return byte_reversed_field_codec() return Reversed() return PacketCodec.get_factory_for_annotation(self, pktCls, fld, annot)