| 1 |
#ifndef CARYLL_SUPPORT_BIN_IO_H |
| 2 |
#define CARYLL_SUPPORT_BIN_IO_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#ifndef INLINE |
| 9 |
#ifdef _MSC_VER |
| 10 |
#define INLINE __forceinline /* use __forceinline (VC++ specific) */ |
| 11 |
#else |
| 12 |
#define INLINE inline /* use standard inline */ |
| 13 |
#endif |
| 14 |
#endif |
| 15 |
|
| 16 |
static INLINE bool otfcc_check_endian(void) { |
| 17 |
union { |
| 18 |
uint8_t i1[2]; |
| 19 |
uint16_t i2; |
| 20 |
} check_union = {.i2 = 1}; // if you don't have a new VC, upgrade it |
| 21 |
|
| 22 |
return (check_union.i1[0] == 1); |
| 23 |
} |
| 24 |
|
| 25 |
static INLINE uint16_t otfcc_endian_convert16(uint16_t i) { |
| 26 |
if (otfcc_check_endian()) { |
| 27 |
union { |
| 28 |
uint8_t i1[2]; |
| 29 |
uint16_t i2; |
| 30 |
} src, des; |
| 31 |
|
| 32 |
src.i2 = i; |
| 33 |
|
| 34 |
des.i1[0] = src.i1[1]; |
| 35 |
des.i1[1] = src.i1[0]; |
| 36 |
|
| 37 |
return des.i2; |
| 38 |
} else { |
| 39 |
return i; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
static INLINE uint32_t otfcc_endian_convert32(uint32_t i) { |
| 44 |
if (otfcc_check_endian()) { |
| 45 |
union { |
| 46 |
uint8_t i1[4]; |
| 47 |
uint32_t i4; |
| 48 |
} src, des; |
| 49 |
|
| 50 |
src.i4 = i; |
| 51 |
|
| 52 |
des.i1[0] = src.i1[3]; |
| 53 |
des.i1[1] = src.i1[2]; |
| 54 |
des.i1[2] = src.i1[1]; |
| 55 |
des.i1[3] = src.i1[0]; |
| 56 |
|
| 57 |
return des.i4; |
| 58 |
} else { |
| 59 |
return i; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static INLINE uint64_t otfcc_endian_convert64(uint64_t i) { |
| 64 |
if (otfcc_check_endian()) { |
| 65 |
union { |
| 66 |
uint8_t i1[8]; |
| 67 |
uint64_t i8; |
| 68 |
} src, des; |
| 69 |
|
| 70 |
src.i8 = i; |
| 71 |
|
| 72 |
des.i1[0] = src.i1[7]; |
| 73 |
des.i1[1] = src.i1[6]; |
| 74 |
des.i1[2] = src.i1[5]; |
| 75 |
des.i1[3] = src.i1[4]; |
| 76 |
des.i1[4] = src.i1[3]; |
| 77 |
des.i1[5] = src.i1[2]; |
| 78 |
des.i1[6] = src.i1[1]; |
| 79 |
des.i1[7] = src.i1[0]; |
| 80 |
|
| 81 |
return des.i8; |
| 82 |
} else { |
| 83 |
return i; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static INLINE uint16_t otfcc_get16u(FILE *file) { |
| 88 |
uint16_t tmp; |
| 89 |
size_t sizeRead = fread(&tmp, 2, 1, file); |
| 90 |
if (!sizeRead) { |
| 91 |
fprintf(stderr, "File corruption of terminated unexpectedly.\n"); |
| 92 |
exit(EXIT_FAILURE); |
| 93 |
} |
| 94 |
return otfcc_endian_convert16(tmp); |
| 95 |
} |
| 96 |
|
| 97 |
static INLINE uint32_t otfcc_get32u(FILE *file) { |
| 98 |
uint32_t tmp; |
| 99 |
size_t sizeRead = fread(&tmp, 4, 1, file); |
| 100 |
if (!sizeRead) { |
| 101 |
fprintf(stderr, "File corruption of terminated unexpectedly.\n"); |
| 102 |
exit(EXIT_FAILURE); |
| 103 |
} |
| 104 |
return otfcc_endian_convert32(tmp); |
| 105 |
} |
| 106 |
|
| 107 |
static INLINE uint64_t otfcc_get64u(FILE *file) { |
| 108 |
uint64_t tmp; |
| 109 |
size_t sizeRead = fread(&tmp, 8, 1, file); |
| 110 |
if (!sizeRead) { |
| 111 |
fprintf(stderr, "File corruption of terminated unexpectedly.\n"); |
| 112 |
exit(EXIT_FAILURE); |
| 113 |
} |
| 114 |
return otfcc_endian_convert64(tmp); |
| 115 |
} |
| 116 |
|
| 117 |
// data reader |
| 118 |
static INLINE uint8_t read_8u(const uint8_t *src) { |
| 119 |
return src[0]; |
| 120 |
} |
| 121 |
static INLINE uint16_t read_16u(const uint8_t *src) { |
| 122 |
uint16_t b0 = ((uint16_t)src[0]) << 8; |
| 123 |
uint16_t b1 = ((uint16_t)src[1]); |
| 124 |
return (b0 | b1); |
| 125 |
} |
| 126 |
static INLINE uint32_t read_24u(const uint8_t *src) { |
| 127 |
uint32_t b0 = ((uint32_t)src[0]) << 16; |
| 128 |
uint32_t b1 = ((uint32_t)src[1]) << 8; |
| 129 |
uint32_t b2 = ((uint32_t)src[2]); |
| 130 |
return (b0 | b1 | b2); |
| 131 |
} |
| 132 |
static INLINE uint32_t read_32u(const uint8_t *src) { |
| 133 |
uint32_t b0 = ((uint32_t)src[0]) << 24; |
| 134 |
uint32_t b1 = ((uint32_t)src[1]) << 16; |
| 135 |
uint32_t b2 = ((uint32_t)src[2]) << 8; |
| 136 |
uint32_t b3 = ((uint32_t)src[3]); |
| 137 |
return (b0 | b1 | b2 | b3); |
| 138 |
} |
| 139 |
static INLINE uint64_t read_64u(const uint8_t *src) { |
| 140 |
uint64_t b0 = ((uint64_t)src[0]) << 56; |
| 141 |
uint64_t b1 = ((uint64_t)src[1]) << 48; |
| 142 |
uint64_t b2 = ((uint64_t)src[2]) << 40; |
| 143 |
uint64_t b3 = ((uint64_t)src[3]) << 32; |
| 144 |
uint64_t b4 = ((uint64_t)src[4]) << 24; |
| 145 |
uint64_t b5 = ((uint64_t)src[5]) << 16; |
| 146 |
uint64_t b6 = ((uint64_t)src[6]) << 8; |
| 147 |
uint64_t b7 = ((uint64_t)src[7]); |
| 148 |
return (b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7); |
| 149 |
} |
| 150 |
static INLINE int8_t read_8s(const uint8_t *src) { |
| 151 |
return (int8_t)read_8u(src); |
| 152 |
} |
| 153 |
static INLINE int16_t read_16s(const uint8_t *src) { |
| 154 |
return (int16_t)read_16u(src); |
| 155 |
} |
| 156 |
static INLINE int32_t read_32s(const uint8_t *src) { |
| 157 |
return (int32_t)read_32u(src); |
| 158 |
} |
| 159 |
static INLINE int64_t read_64s(const uint8_t *src) { |
| 160 |
return (int64_t)read_64u(src); |
| 161 |
} |
| 162 |
|
| 163 |
static INLINE uint16_t be16(uint16_t x) { |
| 164 |
return ((x & 0xff) << 8) | ((x & 0xFF00) >> 8); |
| 165 |
} |
| 166 |
static INLINE uint32_t be32(uint32_t x) { |
| 167 |
return ((x & 0x000000ff) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) | |
| 168 |
((x & 0xFF000000) >> 24); |
| 169 |
} |
| 170 |
|
| 171 |
#endif |