Skip to content
esrrhsPublic

About

Lightweight XML-to-C++ message codegen with tag-based Marshal/Unmarshal (simple protobuf alternative).

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

libyang

A lightweight message codegen tool — a simple alternative to Protocol Buffers for C++.

English | Chinese


Overview

libyang reads an XML message schema and generates C++ structs with tag-based Marshal / Unmarshal helpers. Field tags enable forward/backward compatibility across versions: unknown tags are skipped on decode, and missing fields stay at their current values (call Clear() first for defaults).

The generator is a pure Go CLI (no external scripting runtime). Generated code depends only on the bundled header yang.h.

Install

Download a prebuilt binary from the Releases page, or build from source:

git clone https://github.com/esrrhs/libyang.git
cd libyang
go build -ldflags="-s -w" -o libyang .

Docker:

docker pull esrrhs/libyang

Usage

  1. Edit sample.xml (or your own schema) to define enums and structs.
  2. Generate the header:
./libyang -xml sample.xml -out sample.h
# or
./libyang sample.xml sample.h
# or
./run.sh
  1. Include yang.h and the generated header in your C++ project.

Show version:

./libyang -version

Schema sketch

<result>
  <enum name="ConstDefine" comment="constants">
    <member name="ACC_LEN" type="32" comment="account length"/>
    <member name="PWD_LEN" type="32" comment="password length"/>
  </enum>
  <enum name="MsgID" comment="message ids">
    <member name="CS_REQ_LOGIN" type="1" comment="login request"/>
  </enum>
  <struct name="CSReqLogin" comment="login request">
    <member name="Acc" type="char" length="ACC_LEN" comment="account" tag="1"/>
    <member name="Pwd" type="char" length="PWD_LEN" comment="password" tag="2"/>
  </struct>
</result>

Supported member shapes:

  • Scalar numeric types (uint32_t, float, …) and nested structs
  • Fixed-length char strings (type="char" + length)
  • Arrays with a runtime count field (length + ref)
  • Tagged unions (struct with type="union", members use ref as the case enum)

The generator validates the schema before writing a header. Tags must be in 1–255 and unique within a struct; char fields need a positive length (a number or a previously defined enum constant); arrays need ref; types and union case refs must be defined.

Wire format

Each field is encoded as:

tag: uint8
type: uint8   (ProtoDataType in yang.h)
payload:
  integer:  little-endian two's complement / unsigned value
  float:    IEEE-754 binary32, bits stored little-endian
  double:   IEEE-754 binary64, bits stored little-endian
  string:   len as little-endian uint16, then len bytes (no trailing NUL required on the wire)
  struct:   len as little-endian uint16, then len bytes of nested fields

Limits:

  • Tags are 1–255.
  • String and nested-struct payload lengths are at most 65535 bytes. Marshal returns -1 if a payload would exceed this, instead of truncating the length.

This is a portability-oriented wire-format evolution introduced in 0.2. Integers and lengths are now always little-endian, and values are copied with memcpy so unaligned buffers are well-defined. On little-endian machines the layout is the same as the previous native encoding. Big-endian machines no longer write values in native byte order, so records written by a pre-0.2 big-endian process are not compatible.

Field evolution and error semantics

  • Unknown tags are skipped on decode (forward compatible).
  • Missing fields are left unchanged (backward compatible if you Clear() first for defaults).
  • A known field with a truncated or corrupt payload causes Unmarshal to return -1. Type mismatches on a known tag skip that field's payload so later fields stay aligned.
  • Union objects require a valid discriminator (tag 1) and a matching branch payload (tag 2). Failure returns -1 and does not adopt a new m_Type or union data.
  • Marshal returns the number of bytes written on success, or -1 if the buffer is too small or a length exceeds 65535.
  • Unmarshal returns 0 on success, or -1 on failure. After a failed Unmarshal, the object may be partially updated; do not use it.

Test

go test -count=1 ./...
./run_cpp_tests.sh

go test also runs the C++ runtime tests when a C++ compiler is available. The C++ suite is built with ASan/UBSan when the compiler supports them.

Build release packages

./pack.sh

Cross-platform archives are written to pack/ (binary plus yang.h, sample.xml, docs). The Release workflow watches version.go: when its version string changes on master (and the tag does not exist yet), CI runs ./pack.sh and publishes a GitHub Release.

See CHANGELOG.md for version history.

License

MIT — see LICENSE.

About

Lightweight XML-to-C++ message codegen with tag-based Marshal/Unmarshal (simple protobuf alternative).

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages