Znv: a totally compatible port of FreeBSD's nv library

znv

znv is a totally compatible Zig port of FreeBSD’s nv library. As the original, znv is FreeBSD-only and will continue to be for the foreseeable future.

The nv library permits creating and managing name value pairs as well as sending and receiving them over sockets.

Some of the notable features and divergences are:

  • Byte-identical packed output
    Ensured by a specific test suite which uses the system nv.
  • Descriptors are duplicated when unpacking
    A maliciously crafted buffer can’t cause double free.
  • No more errno and bailing
    Idiomatic Zig is used for error handling.
  • Diagnostics on failures
    Currently opt-in and only for unpacking.
  • Allocators and IO
    Bring your own and reap the benefits.
  • Smaller surface
    dnv.h and cnv.h are obsolete because we have simpler APIs.
  • Performance
    znv is generally faster but for unpacking large list it’s blazingly faster. Run zig build bench to see for yourself.

The library is available here. You might want to head to the “Surface coverage” section to see how the original API maps to the new and improved one.

Library documentation is available here.

My long-term vision is to use znv as a platform to also port casper and make Zig a very ergonomic choice for writing sandboxed programs on FreeBSD.

Supported Zig versions

Version 0.17.0 and onwards.

AI / LLM usage disclosure

I was familiar with the source code of the nv library but not as intimately as needed for this port so I used AI to help me research it properly.

2 Likes