| 1 |
/*
|
| 2 |
* endian.c
|
| 3 |
*
|
| 4 |
* Copyright (c) Steve McIntyre <steve@einval.com>
|
| 5 |
*
|
| 6 |
* Simple helper routines for marshalling data
|
| 7 |
*
|
| 8 |
* GNU GPL v2
|
| 9 |
*/
|
| 10 |
|
| 11 |
#include <mconfig.h>
|
| 12 |
#include "endianconv.h"
|
| 13 |
|
| 14 |
/* Write a 64-bit quantity out into memory in BIG ENDIAN order */
|
| 15 |
void write_be64(unsigned long long in, unsigned char *out)
|
| 16 |
{
|
| 17 |
out[0] = (in >> 56) & 0xFF;
|
| 18 |
out[1] = (in >> 48) & 0xFF;
|
| 19 |
out[2] = (in >> 40) & 0xFF;
|
| 20 |
out[3] = (in >> 32) & 0xFF;
|
| 21 |
out[4] = (in >> 24) & 0xFF;
|
| 22 |
out[5] = (in >> 16) & 0xFF;
|
| 23 |
out[6] = (in >> 8) & 0xFF;
|
| 24 |
out[7] = in & 0xFF;
|
| 25 |
}
|
| 26 |
|
| 27 |
/* Read in a 64-bit BIG ENDIAN quantity */
|
| 28 |
unsigned long long read_be64(unsigned char *in)
|
| 29 |
{
|
| 30 |
unsigned long long result = 0;
|
| 31 |
|
| 32 |
result |= (unsigned long long)in[0] << 56;
|
| 33 |
result |= (unsigned long long)in[1] << 48;
|
| 34 |
result |= (unsigned long long)in[2] << 40;
|
| 35 |
result |= (unsigned long long)in[3] << 32;
|
| 36 |
result |= (unsigned long long)in[4] << 24;
|
| 37 |
result |= (unsigned long long)in[5] << 16;
|
| 38 |
result |= (unsigned long long)in[6] << 8;
|
| 39 |
result |= (unsigned long long)in[7];
|
| 40 |
|
| 41 |
return result;
|
| 42 |
}
|
| 43 |
|
| 44 |
/* Write a 64-bit quantity out into memory in LITTLE ENDIAN order */
|
| 45 |
void write_le64(unsigned long long in, unsigned char *out)
|
| 46 |
{
|
| 47 |
out[0] = in & 0xFF;
|
| 48 |
out[1] = (in >> 8) & 0xFF;
|
| 49 |
out[2] = (in >> 16) & 0xFF;
|
| 50 |
out[3] = (in >> 24) & 0xFF;
|
| 51 |
out[4] = (in >> 32) & 0xFF;
|
| 52 |
out[5] = (in >> 40) & 0xFF;
|
| 53 |
out[6] = (in >> 48) & 0xFF;
|
| 54 |
out[7] = (in >> 56) & 0xFF;
|
| 55 |
}
|
| 56 |
|
| 57 |
/* Read in a 64-bit LITTLE ENDIAN quantity */
|
| 58 |
unsigned long long read_le64(unsigned char *in)
|
| 59 |
{
|
| 60 |
unsigned long long result = 0;
|
| 61 |
|
| 62 |
result |= (unsigned long long)in[0];
|
| 63 |
result |= (unsigned long long)in[1] << 8;
|
| 64 |
result |= (unsigned long long)in[2] << 16;
|
| 65 |
result |= (unsigned long long)in[3] << 24;
|
| 66 |
result |= (unsigned long long)in[4] << 32;
|
| 67 |
result |= (unsigned long long)in[5] << 40;
|
| 68 |
result |= (unsigned long long)in[6] << 48;
|
| 69 |
result |= (unsigned long long)in[7] << 56;
|
| 70 |
|
| 71 |
return result;
|
| 72 |
}
|
| 73 |
|
| 74 |
/* Write a 48-bit quantity out into memory in LITTLE ENDIAN order */
|
| 75 |
void write_le48(unsigned long long in, unsigned char *out)
|
| 76 |
{
|
| 77 |
out[0] = in & 0xFF;
|
| 78 |
out[1] = (in >> 8) & 0xFF;
|
| 79 |
out[2] = (in >> 16) & 0xFF;
|
| 80 |
out[3] = (in >> 24) & 0xFF;
|
| 81 |
out[4] = (in >> 32) & 0xFF;
|
| 82 |
out[5] = (in >> 40) & 0xFF;
|
| 83 |
}
|
| 84 |
|
| 85 |
/* Read in a 48-bit LITTLE ENDIAN quantity */
|
| 86 |
unsigned long long read_le48(unsigned char *in)
|
| 87 |
{
|
| 88 |
unsigned long long result = 0;
|
| 89 |
|
| 90 |
result |= (unsigned long long)in[0];
|
| 91 |
result |= (unsigned long long)in[1] << 8;
|
| 92 |
result |= (unsigned long long)in[2] << 16;
|
| 93 |
result |= (unsigned long long)in[3] << 24;
|
| 94 |
result |= (unsigned long long)in[4] << 32;
|
| 95 |
result |= (unsigned long long)in[5] << 40;
|
| 96 |
|
| 97 |
return result;
|
| 98 |
}
|
| 99 |
|
| 100 |
/* Write a 32-bit quantity out into memory in BIG ENDIAN order */
|
| 101 |
void write_be32(unsigned long in, unsigned char *out)
|
| 102 |
{
|
| 103 |
out[0] = (in >> 24) & 0xFF;
|
| 104 |
out[1] = (in >> 16) & 0xFF;
|
| 105 |
out[2] = (in >> 8) & 0xFF;
|
| 106 |
out[3] = in & 0xFF;
|
| 107 |
}
|
| 108 |
|
| 109 |
/* Read in a 32-bit BIG ENDIAN quantity */
|
| 110 |
unsigned long read_be32(unsigned char *in)
|
| 111 |
{
|
| 112 |
unsigned long result = 0;
|
| 113 |
|
| 114 |
result |= (unsigned long)in[0] << 24;
|
| 115 |
result |= (unsigned long)in[1] << 16;
|
| 116 |
result |= (unsigned long)in[2] << 8;
|
| 117 |
result |= (unsigned long)in[3];
|
| 118 |
|
| 119 |
return result;
|
| 120 |
}
|
| 121 |
|
| 122 |
/* Write a 32-bit quantity out into memory in LITTLE ENDIAN order */
|
| 123 |
void write_le32(unsigned long in, unsigned char *out)
|
| 124 |
{
|
| 125 |
out[0] = in & 0xFF;
|
| 126 |
out[1] = (in >> 8) & 0xFF;
|
| 127 |
out[2] = (in >> 16) & 0xFF;
|
| 128 |
out[3] = (in >> 24) & 0xFF;
|
| 129 |
}
|
| 130 |
|
| 131 |
/* Read in a 32-bit LITTLE ENDIAN quantity */
|
| 132 |
unsigned long read_le32(unsigned char *in)
|
| 133 |
{
|
| 134 |
unsigned long result = 0;
|
| 135 |
|
| 136 |
result |= (unsigned long)in[0];
|
| 137 |
result |= (unsigned long)in[1] << 8;
|
| 138 |
result |= (unsigned long)in[2] << 16;
|
| 139 |
result |= (unsigned long)in[3] << 24;
|
| 140 |
|
| 141 |
return result;
|
| 142 |
}
|
| 143 |
|
| 144 |
/* Write a 16-bit quantity out into memory in BIG ENDIAN order */
|
| 145 |
void write_be16(unsigned short in, unsigned char *out)
|
| 146 |
{
|
| 147 |
out[0] = (in >> 8) & 0xFF;
|
| 148 |
out[1] = in & 0xFF;
|
| 149 |
}
|
| 150 |
|
| 151 |
/* Read in a 16-bit BIG ENDIAN quantity */
|
| 152 |
unsigned short read_be16(unsigned char *in)
|
| 153 |
{
|
| 154 |
unsigned short result = 0;
|
| 155 |
|
| 156 |
result |= (unsigned short)in[0] << 8;
|
| 157 |
result |= (unsigned short)in[1];
|
| 158 |
return result;
|
| 159 |
}
|
| 160 |
|
| 161 |
/* Write a 16-bit quantity out into memory in LITTLE ENDIAN order */
|
| 162 |
void write_le16(unsigned short in, unsigned char *out)
|
| 163 |
{
|
| 164 |
out[0] = in & 0xFF;
|
| 165 |
out[1] = in & 0xFF >> 8;
|
| 166 |
}
|
| 167 |
|
| 168 |
/* Read in a 16-bit LITTLE ENDIAN quantity */
|
| 169 |
unsigned short read_le16(unsigned char *in)
|
| 170 |
{
|
| 171 |
unsigned short result = 0;
|
| 172 |
|
| 173 |
result |= (unsigned short)in[0];
|
| 174 |
result |= (unsigned short)in[1] << 8;
|
| 175 |
return result;
|
| 176 |
}
|
| 177 |
|