add uint64 pack and unpack routines
parent
a38b7946a4
commit
58a07defd7
@ -0,0 +1,66 @@
|
||||
#include <assert.h>
|
||||
#include <uint16.h>
|
||||
#include <uint32.h>
|
||||
#include <uint64.h>
|
||||
#include <byte.h>
|
||||
|
||||
int main() {
|
||||
char buf[8];
|
||||
|
||||
{
|
||||
uint16 a;
|
||||
|
||||
buf[0]=buf[1]=0;
|
||||
uint16_pack_big(buf,0x1234);
|
||||
assert(buf[0]==0x12 && buf[1]==0x34);
|
||||
uint16_unpack_big(buf,&a);
|
||||
assert(a==0x1234);
|
||||
assert(uint16_read_big(buf)==0x1234);
|
||||
|
||||
buf[0]=buf[1]=0;
|
||||
uint16_pack(buf,0x1234);
|
||||
assert(buf[0]==0x34 && buf[1]==0x12);
|
||||
uint16_unpack(buf,&a);
|
||||
assert(a==0x1234);
|
||||
assert(uint16_read(buf)==0x1234);
|
||||
}
|
||||
|
||||
{
|
||||
uint32 a;
|
||||
|
||||
buf[0]=buf[1]=buf[2]=buf[3]=0;
|
||||
uint32_pack_big(buf,0x12345678);
|
||||
assert(buf[0]==0x12 && buf[1]==0x34 && buf[2]==0x56 && buf[3]==0x78);
|
||||
uint32_unpack_big(buf,&a);
|
||||
assert(a==0x12345678);
|
||||
assert(uint32_read_big(buf)==0x12345678);
|
||||
|
||||
buf[0]=buf[1]=buf[2]=buf[3]=0;
|
||||
uint32_pack(buf,0x12345678);
|
||||
assert(buf[0]==0x78 && buf[1]==0x56 && buf[2]==0x34 && buf[3]==0x12);
|
||||
uint32_unpack(buf,&a);
|
||||
assert(a==0x12345678);
|
||||
assert(uint32_read(buf)==0x12345678);
|
||||
}
|
||||
|
||||
{
|
||||
uint64 a;
|
||||
unsigned int i;
|
||||
|
||||
byte_zero(buf,sizeof(buf));
|
||||
uint64_pack_big(buf,0x0102030405060708ull);
|
||||
for (i=0; i<8; ++i) assert(buf[i]==i+1);
|
||||
uint64_unpack_big(buf,&a);
|
||||
assert(a==0x0102030405060708ull);
|
||||
assert(uint64_read_big(buf)==0x0102030405060708ull);
|
||||
|
||||
byte_zero(buf,sizeof(buf));
|
||||
uint64_pack(buf,0x0102030405060708ull);
|
||||
for (i=0; i<8; ++i) assert(buf[7-i]==i+1);
|
||||
uint64_unpack(buf,&a);
|
||||
assert(a==0x0102030405060708ull);
|
||||
assert(uint64_read(buf)==0x0102030405060708ull);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
.TH uint64_pack 3
|
||||
.SH NAME
|
||||
uint64_pack \- write an unsigned little-endian 64-bit integer
|
||||
.SH SYNTAX
|
||||
.B #include <uint64.h>
|
||||
|
||||
uint64 \fIu\fR;
|
||||
|
||||
void \fBuint64_pack\fP(char \fIs\fR[4],uint64 \fIu\fR);
|
||||
.SH DESCRIPTION
|
||||
uint64 is a 64-bit unsigned integer type.
|
||||
|
||||
uint64_pack portably writes a uint64 \fIu\fR to \fIs\fR in
|
||||
little-endian byte order.
|
||||
|
||||
.SH "SEE ALSO"
|
||||
uint64_unpack(3), uint64_pack_big(3)
|
@ -0,0 +1,8 @@
|
||||
#define NO_UINT64_MACROS
|
||||
#include "uint64.h"
|
||||
#include "uint32.h"
|
||||
|
||||
void uint64_pack(char *out,uint64 in) {
|
||||
uint32_pack(out,in&0xffffffff);
|
||||
uint32_pack(out+4,in>>32);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
.TH uint64_pack_big 3
|
||||
.SH NAME
|
||||
uint64_pack_big \- write an unsigned big-endian 64-bit integer
|
||||
.SH SYNTAX
|
||||
.B #include <uint64.h>
|
||||
|
||||
uint64 \fIu\fR;
|
||||
|
||||
void \fBuint64_pack_big\fP(char \fIs\fR[4],uint64 \fIu\fR);
|
||||
.SH DESCRIPTION
|
||||
uint64 is a 64-bit unsigned integer type.
|
||||
|
||||
uint64_pack_big portably writes a uint64 \fIu\fR to \fIs\fR in
|
||||
big-endian (i.e. network) byte order.
|
||||
|
||||
.SH "SEE ALSO"
|
||||
uint64_unpack_big(3), uint64_pack(3)
|
@ -0,0 +1,8 @@
|
||||
#define NO_UINT64_MACROS
|
||||
#include "uint64.h"
|
||||
#include "uint32.h"
|
||||
|
||||
void uint64_pack_big(char *out,uint64 in) {
|
||||
uint32_pack_big(out,in>>32);
|
||||
uint32_pack_big(out+4,in&0xffffffff);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
.TH uint64_read 3
|
||||
.SH NAME
|
||||
uint64_read \- read an unsigned little-endian 64-bit integer
|
||||
.SH SYNTAX
|
||||
.B #include <uint64.h>
|
||||
|
||||
uint64 \fBuint64_read\fP(const char \fIs\fR[4]);
|
||||
.SH DESCRIPTION
|
||||
uint64 is a 64-bit unsigned integer type.
|
||||
|
||||
uint64_read portably reads a uint64 as stored on a little-endian
|
||||
architecture from \fIs\fR and returns it.
|
||||
|
||||
.SH "SEE ALSO"
|
||||
uint64_unpack(3), uint64_unpack_big(3)
|
@ -0,0 +1,7 @@
|
||||
#define NO_UINT64_MACROS
|
||||
#include "uint64.h"
|
||||
#include "uint32.h"
|
||||
|
||||
uint64 uint64_read(const char *in) {
|
||||
return uint32_read(in) | ((uint64)uint32_read(in+4)<<32);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
.TH uint64_read_big 3
|
||||
.SH NAME
|
||||
uint64_read_big \- read an unsigned big-endian 64-bit integer
|
||||
.SH SYNTAX
|
||||
.B #include <uint64.h>
|
||||
|
||||
uint64 \fBuint64_read_big\fP(const char \fIs\fR[4]);
|
||||
.SH DESCRIPTION
|
||||
uint64 is a 64-bit unsigned integer type.
|
||||
|
||||
uint64_read_big portably reads a uint64 as stored on a big-endian
|
||||
architecture from \fIs\fR and returns it.
|
||||
|
||||
.SH "SEE ALSO"
|
||||
uint64_unpack(3), uint64_unpack_big(3)
|
@ -0,0 +1,7 @@
|
||||
#define NO_UINT64_MACROS
|
||||
#include "uint64.h"
|
||||
#include "uint32.h"
|
||||
|
||||
uint64 uint64_read_big(const char *in) {
|
||||
return ((uint64)uint32_read_big(in)<<32) | uint32_read_big(in+4);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
.TH uint64_unpack 3
|
||||
.SH NAME
|
||||
uint64_unpack \- read an unsigned little-endian 64-bit integer
|
||||
.SH SYNTAX
|
||||
.B #include <uint64.h>
|
||||
|
||||
uint64 \fIu\fR;
|
||||
|
||||
void \fBuint64_unpack\fP(const char \fIs\fR[4],uint64 *\fIu\fR);
|
||||
.SH DESCRIPTION
|
||||
uint64 is a 64-bit unsigned integer type.
|
||||
|
||||
uint64_unpack portably reads a uint64 as stored on a little-endian
|
||||
architecture from \fIs\fR and writes it into \fIu\fR in the native byte order.
|
||||
|
||||
.SH "SEE ALSO"
|
||||
uint64_pack(3), uint64_unpack_big(3)
|
@ -0,0 +1,7 @@
|
||||
#define NO_UINT64_MACROS
|
||||
#include "uint64.h"
|
||||
#include "uint32.h"
|
||||
|
||||
void uint64_unpack(const char *in,uint64 *out) {
|
||||
*out = uint64_read(in);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
.TH uint64_unpack_big 3
|
||||
.SH NAME
|
||||
uint64_unpack_big \- read an unsigned big-endian 64-bit integer
|
||||
.SH SYNTAX
|
||||
.B #include <uint64.h>
|
||||
|
||||
uint64 \fIu\fR;
|
||||
|
||||
void \fBuint64_unpack_big\fP(const char \fIs\fR[4],uint64 *\fIu\fR);
|
||||
.SH DESCRIPTION
|
||||
uint64 is a 64-bit unsigned integer type.
|
||||
|
||||
uint64_unpack_big portably reads a uint64 as stored on a big-endian
|
||||
architecture (i.e. in network byte order) from \fIs\fR and writes it
|
||||
into \fIu\fR in the native byte order.
|
||||
|
||||
.SH "SEE ALSO"
|
||||
uint64_pack_big(3), uint64_unpack(3)
|
@ -0,0 +1,7 @@
|
||||
#define NO_UINT64_MACROS
|
||||
#include "uint64.h"
|
||||
#include "uint32.h"
|
||||
|
||||
void uint64_unpack_big(const char *in,uint64 *out) {
|
||||
*out = uint64_read_big(in);
|
||||
}
|
Loading…
Reference in New Issue