Standard byteorder functions across BSD / Linux

Nanno Langstraat nlcom_os at
Wed Jun 4 22:55:13 UTC 2008


I think it would be nice to have standard byteorder conversion functions for user applications across the BSDs and Linux.
betoh64(), htobe64(), etc.  (like OpenBSD's sys/endian.h)

Just small convenience functions/macros, but useful because it's such a common requirement; applications currently have to roll their own over&over with an annoying tangle of #ifdefs.

Summary of the story so far:

    * glibc added the basic macros to <endian.h>

    * OpenBSD already had the macros in <sys/endian.h>. They are for
      both kernel and application use according to the manpage.
      OpenBSD didn't want my patch that made <endian.h> the standard
      include file for user applications.

    * FreeBSD has similar but incompatible macros in <sys/endian.h>.
      They are for kernel use, not application use according to the manpage.

Full details below. (pretty long story for such a tiny feature)


My original proposal to the GNU glibc maintainers:

* glibc Bugzilla 6442 - Adding cross-Unix endianness functions:
  betoh() / htobe() 64,32,16

* Pro/con:


I planned to have a little coordination between glibc/FreeBSD/OpenBSD before starting on patches. That didn't happen, the glibc maintainers gave zero response for a full month, then created the basic macros in /usr/include/endian.h without any discussion.

  * ('endian.h' pre-existed in glibc to define __LITTLE_ENDIAN etc. The
    file does not pre-exist on OpenBSD / FreeBSD)

  * (glibc didn't adopt the "swap64()" etc. functions. Glibc already
    contains variants with a different naming pattern: "bswap_64()"

  * (be32enc() and friends only got a small mention by me, not adopted
    by glibc)


The discussion on the OpenBSD mailinglist can be read here:

Of particular interest to FreeBSD: this charmless but informative email by Theo de Raadt, which outlines the history of the BSD kernel byteorder functions:


My question to FreeBSD: 
I don't use FreeBSD myself, but I'll prepare a patch if you like the idea and if you indicate what you'll accept:

    * <endian.h> or <sys/endian.h> ?
      I maintain that it should be <endian.h> for user applications:
      IMHO <sys/> is for the user-kernel API, and byteorder belongs to
      libc not the kernel API.
      glibc apparently agrees, OpenBSD disagreed.

    * You're OK with userspace applications standardizing on OpenBSD's
      original betoh64() instead of FreeBSD's derivate be64toh() ?


More information about the freebsd-hackers mailing list