Class BerJsonEncoder

java.lang.Object
org.snmp4j.mcp.tool.snmp.ber.BerJsonEncoder

public final class BerJsonEncoder extends Object
Encodes a BER node tree — expressed in the same JSON shape that snmp_message_ber emits (tag, tagName, value, oid, children) — back into raw bytes, with explicit support for producing non-canonical and invalid encodings for fuzzing.

This deliberately does not reuse the (decode-only) BERNode/SNMP4J encoders: it writes tag/length/value octets directly so callers can inject faults that a conformant encoder would never emit.

Node fields

  • tag — the tag, as a hex string ("0x30", "30") or an integer. Multi-byte (high-tag-number) tags are supported by giving multiple hex bytes.
  • children — array of child nodes; when present and non-empty the node is constructed and its content is the concatenation of the encoded children.
  • valueHex — exact content octets for a leaf (authoritative when present).
  • value / tagName / oid — fallback leaf content derived from the tag for standard SNMP types when valueHex is absent.

Per-node fault-injection fields (all optional)

  • overrideLength (int) — emit this length value instead of the true content length.
  • lengthForm"short" | "longN" (N=1..126) | "indefinite".
  • truncateContentTo (int) — emit only the first N content bytes.
  • appendHex — raw bytes appended after this node's TLV (trailing garbage).
  • omit (bool) — skip this node entirely.
  • rawHex — emit these bytes verbatim for the whole node, bypassing tag/length/value.
Since:
0.1.0
  • Method Details

    • encode

      public static byte[] encode(Object node)
      Encodes a node tree to bytes.
      Parameters:
      node - the root node (a JSON object parsed into a Map)
      Returns:
      the encoded bytes
      Throws:
      IllegalArgumentException - if the tree is malformed
    • isNonCanonical

      public static boolean isNonCanonical(Object node)
      Returns true if any node in the tree carries a fault-injection field or would otherwise produce a non-canonical encoding.
      Parameters:
      node - the root node
      Returns:
      whether the encoding is non-canonical