# HG changeset patch # User Erik Quanstrom # Date 1329519746 0 # Node ID 1938bae8cbbc02a2c6c5fa253f695b32fadc8201 # Parent 00051c3d3125358716d3de95381f18231ce15cbf getbe, getle, putbe, putle: marshal any-sized intergers generic functions to marshal big or little endian integers, based on presotto's code from nsec(2). R=nixiedev, john, nemo CC=nix-dev http://codereview.appspot.com/5677045 Committer: John Floren diff -r 00051c3d3125 -r 1938bae8cbbc sys/include/libc.h --- a/sys/include/libc.h Fri Feb 17 22:51:48 2012 +0000 +++ b/sys/include/libc.h Fri Feb 17 23:02:26 2012 +0000 @@ -333,6 +333,14 @@ extern void cycles(uvlong*); /* 64-bit value of the cycle counter if there is one, 0 if there isn't */ /* + * endian conversion + */ +extern uvlong getbe(uchar*, int); +extern void putbe(uchar*, uvlong, int); +extern uvlong getle(uchar*, int); +extern void putle(uchar*, uvlong, int); + +/* * one-of-a-kind */ enum diff -r 00051c3d3125 -r 1938bae8cbbc sys/man/2/getbe --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/man/2/getbe Fri Feb 17 23:02:26 2012 +0000 @@ -0,0 +1,44 @@ +.TH GETBE 2 +getbe, setbe, getle, setle \- integer marshalling +.SH SYNOPSIS +.ta +\w'\fLuvlong 'u +.PP +.nf +.B +uvlong getbe(uchar *buf, int nbytes) +.PP +.nf +.B +void putbe(uchar *buf, uvlong v, int nbytes) +.PP +.nf +.B +uvlong getle(uchar *buf, int nbytes) +.PP +.nf +.B +void putle(uchar *buf, uvlong v, int nbytes) +.SH DESCRIPTION +These functions marshal a 1- to 8-byte integer to +or from little- or big-endian formats. The size +of the integer is specified in bytes, and is not restricted +to a power of two. +.SH SOURCE +.B /sys/src/libc/port/getbe.c +.SH "SEE ALSO" +.IR fcall (2), +.IR ip (2) +.SH BUGS +.IR GBIT * +and +.IR PBIT * +(from +.IR fcall (2)) +provide similar functionality for little-endian integers. +The +.IR nhget * +and +.IR nhput * +functions from +.IR ip (2) +provide similar functionality for big-endian integers. diff -r 00051c3d3125 -r 1938bae8cbbc sys/src/libc/port/getbe.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/src/libc/port/getbe.c Fri Feb 17 23:02:26 2012 +0000 @@ -0,0 +1,54 @@ +#include +#include + +static uvlong border = 0x0001020304050607ull; +static uvlong lorder = 0x0706050403020100ull; + +uvlong +getle(uchar *t, int w) +{ + uint i; + uvlong r; + + r = 0; + for(i = w; i != 0; ) + r = r<<8 | t[--i]; + return r; +} + +void +putle(uchar *t, uvlong r, int w) +{ + uchar *o, *f; + uint i; + + f = (uchar*)&r; + o = (uchar*)&lorder; + for(i = 0; i < w; i++) + t[o[i]] = f[i]; +} + +uvlong +getbe(uchar *t, int w) +{ + uint i; + uvlong r; + + r = 0; + for(i = 0; i < w; i++) + r = r<<8 | t[i]; + return r; +} + +void +putbe(uchar *t, uvlong r, int w) +{ + uchar *o, *f; + uint i; + + f = (uchar*)&r; + o = (uchar*)&border + (sizeof border-w); + for(i = 0; i < w; i++) + t[i] = f[o[i]]; +} + diff -r 00051c3d3125 -r 1938bae8cbbc sys/src/libc/port/mkfile --- a/sys/src/libc/port/mkfile Fri Feb 17 22:51:48 2012 +0000 +++ b/sys/src/libc/port/mkfile Fri Feb 17 23:02:26 2012 +0000 @@ -28,6 +28,7 @@ fmod.c\ frand.c\ frexp.c\ + getbe.c\ getcallerpc.c\ getfields.c\ getuser.c\