Encode a packet and inspect a validation finding
Native SDK. Start with the built example from Getting started. Append --roundtrip to its run command. For example, from examples/go on Linux run go run . ../../lib/misb0601.so ../../example.klv --roundtrip.
Each example encodes tag 2 (timestamp) and tag 3 (mission ID) as a revision 19 packet. The encoder writes the version and checksum. Decode the result and look for "3":"SURVEY". Use the full packet when validating real input. Here the example deliberately removes its last byte to produce a finding you can recognize.
- C
- C++
- C#
- Go
- Python
The example driver calls this helper when you supply the flag.
#pragma once
#include "misb0601.h"
#include <stdio.h>
static int roundtrip(void) {
uint8_t input[] = "{\"2\":1700000000000000,\"3\":\"SURVEY\"}";
misb0601_buffer packet = {0}, fields = {0}, report = {0};
int ok = Misb0601EncodeJSON(input, sizeof(input)-1, MISB0601_REVISION_19,
MISB0601_SORT_BY_TAG, &packet) == MISB0601_STATUS_OK;
if (ok) ok = Misb0601DecodeJSON(packet.data, packet.len, MISB0601_SORT_BY_TAG,
MISB0601_JSON_KEYS_TAGS, MISB0601_JSON_COMPACT, MISB0601_TIMESTAMPS_RAW, &fields) == MISB0601_STATUS_OK;
if (ok) ok = Misb0601ValidatePacket(packet.data, packet.len - 1, NULL, &report) == MISB0601_STATUS_OK;
if (ok) {
fwrite(fields.data, 1, fields.len, stdout);
putchar('\n');
fwrite(report.data, 1, report.len, stdout);
putchar('\n');
}
Misb0601BufferFree(&report);
Misb0601BufferFree(&fields);
Misb0601BufferFree(&packet);
return ok;
}
The example driver calls this helper when you supply the flag.
#pragma once
#include "misb0601.hpp"
#include <iostream>
inline void roundtrip(misb0601::Library &lib, misb0601::DecoderOptions options) {
auto packet = lib.EncodeJson(R"({"2":1700000000000000,"3":"SURVEY"})");
std::cout << lib.DecodeJson(packet, options).ReadString() << '\n';
std::cout << lib.ValidatePacketJson(packet.Data(), packet.Size() - 1) << '\n';
}
The example driver calls this helper when you supply the flag.
using System.Text.Json;
using LibMisb.Misb0601;
static class RoundTrip
{
internal static void Run(Misb0601Library library, Misb0601Decoder decoder)
{
using var packet = library.CreateEncoder().EncodeJson("""{"2":1700000000000000,"3":"SURVEY"}"""u8);
using var fields = decoder.DecodeJson(packet);
Console.WriteLine(fields.ReadUtf8());
using var report = JsonDocument.Parse(library.ValidatePacketJson(packet.AsSpan()[..^1]));
var root = report.RootElement;
Console.WriteLine(root.GetProperty("valid").GetBoolean() ? "valid" : "invalid");
if (root.TryGetProperty("findings", out var findings) && findings.ValueKind == JsonValueKind.Array)
foreach (var finding in findings.EnumerateArray())
Console.WriteLine($"{finding.GetProperty("rule_id")} {finding.GetProperty("message")}");
}
}
The example driver calls this helper when you supply the flag.
package main
import (
"fmt"
sdk "github.com/impleotv/libmisb-sdk-go/misb0601abi"
)
func roundtrip(lib *sdk.Library, decoder *sdk.Decoder) {
encoder := lib.CreateEncoder(sdk.DefaultEncoderOptions())
packet, err := encoder.EncodeJSON([]byte(`{"2":1700000000000000,"3":"SURVEY"}`))
must(err)
fields, err := decoder.DecodeJSON(packet)
must(err)
fmt.Println(string(fields))
report, err := lib.ValidatePacket(packet[:len(packet)-1], sdk.ValidationOptions{})
must(err)
if report.Valid {
fmt.Println("valid")
} else {
fmt.Println("invalid")
}
for _, finding := range report.Findings {
fmt.Println(finding.RuleID, finding.Message)
}
}
The example driver calls this helper when you supply the flag.
def roundtrip(library, decoder):
packet = library.create_encoder().encode_json(b'{"2":1700000000000000,"3":"SURVEY"}')
print(decoder.decode_json(packet).decode())
report = library.validate_packet(packet[:-1])
print("valid" if report.valid else "invalid")
for finding in report.findings:
print(finding.rule_id, finding.message)
The report has valid: false. The first finding has rule ID packet-decode, severity error, and a message identifying the packet decode failure. The C and C++ examples print the JSON report. The C#, Go, and Python examples print the verdict followed by each finding. A successful validation call means a report was produced. Use its valid field to decide whether to accept the packet.