-
Notifications
You must be signed in to change notification settings - Fork 16
/
endianio.c
140 lines (127 loc) · 3.34 KB
/
endianio.c
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
/*
This file is Copyright © 1999-2005 André Majorel,
Copyright © 2006-2021 contributors to the DeuTex project.
Those functions allow to read little-endian and big-endian integers
from a file regardless of the endianness of the CPU.
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "deutex.h"
#include "endianio.h"
/*
* fread_i16_be
* Read a big-endian 16-bit signed integer from file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fread_i16_be(FILE * fd, int16_t * buf)
{
*buf = (getc(fd) << 8)
| getc(fd);
return feof(fd) || ferror(fd);
}
/*
* fread_i16_le
* Read a little-endian 16-bit signed integer from file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fread_i16_le(FILE * fd, int16_t * buf)
{
*buf = getc(fd)
| (getc(fd) << 8);
return feof(fd) || ferror(fd);
}
/*
* fread_i32_be
* Read a big-endian 32-bit signed integer from file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fread_i32_be(FILE * fd, int32_t * buf)
{
*buf = ((int32_t) getc(fd) << 24)
| ((int32_t) getc(fd) << 16)
| ((uint16_t) getc(fd) << 8)
| getc(fd);
return feof(fd) || ferror(fd);
}
/*
* fread_i32_le
* Read a little-endian 32-bit signed integer from file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fread_i32_le(FILE * fd, int32_t * buf)
{
*buf = getc(fd)
| ((uint16_t) getc(fd) << 8)
| ((int32_t) getc(fd) << 16)
| ((int32_t) getc(fd) << 24);
return feof(fd) || ferror(fd);
}
/*
* fread_u16_le
* Read a little-endian 16-bit unsigned integer from file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fread_u16_le(FILE * fd, uint16_t * buf)
{
*buf = getc(fd)
| (getc(fd) << 8);
return feof(fd) || ferror(fd);
}
/*
* fwrite_i16_le
* Write a little-endian 16-bit signed integer to file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fwrite_i16_le(FILE * fd, int16_t buf)
{
putc(buf & 0xff, fd);
putc((buf >> 8) & 0xff, fd);
return feof(fd) || ferror(fd);
}
/*
* fwrite_i16_be
* Write a big-endian 16-bit signed integer to file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fwrite_i16_be(FILE * fd, int16_t buf)
{
putc((buf >> 8) & 0xff, fd);
putc(buf & 0xff, fd);
return feof(fd) || ferror(fd);
}
/*
* fwrite_i32_le
* Write a little-endian 32-bit signed integer to file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fwrite_i32_le(FILE * fd, int32_t buf)
{
putc(buf & 0xff, fd);
putc((buf >> 8) & 0xff, fd);
putc((buf >> 16) & 0xff, fd);
putc((buf >> 24) & 0xff, fd);
return feof(fd) || ferror(fd);
}
/*
* fwrite_i32_be
* Write a big-endian 32-bit signed integer to file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fwrite_i32_be(FILE * fd, int32_t buf)
{
putc((buf >> 24) & 0xff, fd);
putc((buf >> 16) & 0xff, fd);
putc((buf >> 8) & 0xff, fd);
putc(buf & 0xff, fd);
return feof(fd) || ferror(fd);
}
/*
* fwrite_u16_le
* Write a little-endian 16-bit unsigned integer to file <fd>.
* Returns 0 on success, != 0 on failure.
*/
int fwrite_u16_le(FILE * fd, uint16_t buf)
{
putc(buf & 0xff, fd);
putc((buf >> 8) & 0xff, fd);
return feof(fd) || ferror(fd);
}