File: endian.c

package info (click to toggle)
jigit 1.22-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,360 kB
  • sloc: ansic: 7,227; sh: 4,433; perl: 611; makefile: 157; lisp: 4
file content (188 lines) | stat: -rw-r--r-- 4,520 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * endian.c
 *
 * Copyright (c) 2004-2006 Steve McIntyre <steve@einval.com>
 *
 * Simple helper routines for marshalling data
 *
 * GNU GPL v2+
 */

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#endif

#include "endianconv.h"

/* Write a 64-bit quantity out into memory in BIG ENDIAN order */
void write_be64(uint64_t in, unsigned char *out)
{
    out[0] = (in >> 56) & 0xFF;
    out[1] = (in >> 48) & 0xFF;
    out[2] = (in >> 40) & 0xFF;
    out[3] = (in >> 32) & 0xFF;
    out[4] = (in >> 24) & 0xFF;
    out[5] = (in >> 16) & 0xFF;
    out[6] = (in >> 8) & 0xFF;
    out[7] = in & 0xFF;
}

/* Read in a 64-bit BIG ENDIAN quantity */
uint64_t read_be64(unsigned char *in)
{
    uint64_t result = 0;

    result |= (uint64_t)in[0] << 56;
    result |= (uint64_t)in[1] << 48;
    result |= (uint64_t)in[2] << 40;
    result |= (uint64_t)in[3] << 32;
    result |= (uint64_t)in[4] << 24;
    result |= (uint64_t)in[5] << 16;
    result |= (uint64_t)in[6] << 8;
    result |= (uint64_t)in[7];
    
    return result;
}

/* Write a 64-bit quantity out into memory in LITTLE ENDIAN order */
void write_le64(uint64_t in, unsigned char *out)
{
    out[0] = in & 0xFF;
    out[1] = (in >> 8) & 0xFF;
    out[2] = (in >> 16) & 0xFF;
    out[3] = (in >> 24) & 0xFF;
    out[4] = (in >> 32) & 0xFF;
    out[5] = (in >> 40) & 0xFF;
    out[6] = (in >> 48) & 0xFF;
    out[7] = (in >> 56) & 0xFF;
}

/* Read in a 64-bit LITTLE ENDIAN quantity */
uint64_t read_le64(unsigned char *in)
{
    uint64_t result = 0;

    result |= (uint64_t)in[0];
    result |= (uint64_t)in[1] << 8;
    result |= (uint64_t)in[2] << 16;
    result |= (uint64_t)in[3] << 24;
    result |= (uint64_t)in[4] << 32;
    result |= (uint64_t)in[5] << 40;
    result |= (uint64_t)in[6] << 48;
    result |= (uint64_t)in[7] << 56;
    
    return result;
}

/* Write a 48-bit quantity out into memory in LITTLE ENDIAN order */
void write_le48(uint64_t in, unsigned char *out)
{
    out[0] = in & 0xFF;
    out[1] = (in >> 8) & 0xFF;
    out[2] = (in >> 16) & 0xFF;
    out[3] = (in >> 24) & 0xFF;
    out[4] = (in >> 32) & 0xFF;
    out[5] = (in >> 40) & 0xFF;
}

/* Read in a 48-bit LITTLE ENDIAN quantity */
uint64_t read_le48(unsigned char *in)
{
    uint64_t result = 0;

    result |= (uint64_t)in[0];
    result |= (uint64_t)in[1] << 8;
    result |= (uint64_t)in[2] << 16;
    result |= (uint64_t)in[3] << 24;
    result |= (uint64_t)in[4] << 32;
    result |= (uint64_t)in[5] << 40;
    
    return result;
}

/* Write a 32-bit quantity out into memory in BIG ENDIAN order */
void write_be32(unsigned long in, unsigned char *out)
{
    out[0] = (in >> 24) & 0xFF;
    out[1] = (in >> 16) & 0xFF;
    out[2] = (in >> 8) & 0xFF;
    out[3] = in & 0xFF;
}

/* Read in a 32-bit BIG ENDIAN quantity */
unsigned long read_be32(unsigned char *in)
{
    unsigned long result = 0;

    result |= (unsigned long)in[0] << 24;
    result |= (unsigned long)in[1] << 16;
    result |= (unsigned long)in[2] << 8;
    result |= (unsigned long)in[3];
    
    return result;
}

/* Write a 32-bit quantity out into memory in LITTLE ENDIAN order */
void write_le32(unsigned long in, unsigned char *out)
{
    out[0] = in & 0xFF;
    out[1] = (in >> 8) & 0xFF;
    out[2] = (in >> 16) & 0xFF;
    out[3] = (in >> 24) & 0xFF;
}

/* Read in a 32-bit LITTLE ENDIAN quantity */
unsigned long read_le32(unsigned char *in)
{
    unsigned long result = 0;

    result |= (unsigned long)in[0];
    result |= (unsigned long)in[1] << 8;
    result |= (unsigned long)in[2] << 16;
    result |= (unsigned long)in[3] << 24;
    
    return result;
}

/* Write a 16-bit quantity out into memory in BIG ENDIAN order */
void write_be16(unsigned short in, unsigned char *out)
{
    out[0] = (in >> 8) & 0xFF;
    out[1] = in & 0xFF;
}
    
/* Read in a 16-bit BIG ENDIAN quantity */
unsigned short read_be16(unsigned char *in)
{
    unsigned short result = 0;
    
    result |= (unsigned short)in[0] << 8;
    result |= (unsigned short)in[1];
    return result;
}

/* Write a 16-bit quantity out into memory in LITTLE ENDIAN order */
void write_le16(unsigned short in, unsigned char *out)
{
    out[0] = in & 0xFF;
    out[1] = in & 0xFF >> 8;
}
    
/* Read in a 16-bit LITTLE ENDIAN quantity */
unsigned short read_le16(unsigned char *in)
{
    unsigned short result = 0;
    
    result |= (unsigned short)in[0];
    result |= (unsigned short)in[1] << 8;
    return result;
}