File: endian.c

package info (click to toggle)
jigit 1.21-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 1,812 kB
  • sloc: sh: 10,437; ansic: 6,125; perl: 566; makefile: 134
file content (176 lines) | stat: -rw-r--r-- 4,694 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
/*
 * endian.c
 *
 * Simple helper functions for reading and writing BE and LE numbers
 *
 * Copyright (c) 2004 Steve McIntyre <steve@einval.com>
 *
 * GPL v2 - see COPYING
 */

#include "endian.h"

/* Write a 64-bit quantity out into memory in BIG ENDIAN order */
void write_be64(unsigned long long 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 */
unsigned long long read_be64(unsigned char *in)
{
    unsigned long long result = 0;

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

/* Write a 64-bit quantity out into memory in LITTLE ENDIAN order */
void write_le64(unsigned long long 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 */
unsigned long long read_le64(unsigned char *in)
{
    unsigned long long result = 0;

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

/* Write a 48-bit quantity out into memory in LITTLE ENDIAN order */
void write_le48(unsigned long long 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 */
unsigned long long read_le48(unsigned char *in)
{
    unsigned long long result = 0;

    result |= (unsigned long long)in[0];
    result |= (unsigned long long)in[1] << 8;
    result |= (unsigned long long)in[2] << 16;
    result |= (unsigned long long)in[3] << 24;
    result |= (unsigned long long)in[4] << 32;
    result |= (unsigned long long)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;
}