4 #include <stdio.h> /* for FILE typedef*/
5 #include <stdint.h> /* for uint16_t, uint32_t */
9 /* Read/write "x" from/to "file" as bigendian representation of "size" bits. */
10 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size);
11 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size);
15 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
21 int16_t bitshift = size - 8;
25 for (; bitshift >= 0; bitshift = bitshift - 8)
32 * x = * x + ((uint32_t) test << bitshift);
39 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size)
45 int16_t bitshift = size - 8;
47 for (; bitshift >= 0; bitshift = bitshift - 8)
49 if (EOF == fputc((x >> bitshift) & 0xFF, file))
59 extern uint8_t read_uint8(FILE * file, uint8_t * x)
61 /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, work
62 * on values of fewer bytes corrupts their immediate neighbor bytes. This
63 * necessitates working on newly acquired separate memory areas (* y), only
64 * copying the sufficiently small end result to * x.
67 uint8_t err = read_uintX_bigendian(file, &y, 8);
74 extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x)
76 /* See read_uint8() introductory comment for rationale. */
78 uint8_t err = read_uintX_bigendian(file, &y, 16);
85 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x)
87 return read_uintX_bigendian(file, x, 32);
92 extern uint8_t write_uint8(uint8_t x, FILE * file)
94 return write_uintX_bigendian(file, x, 8);
99 extern uint8_t write_uint16_bigendian(uint16_t x, FILE * file)
101 return write_uintX_bigendian(file, x, 16);
106 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file)
108 return write_uintX_bigendian(file, x, 32);