-
Notifications
You must be signed in to change notification settings - Fork 78
/
common.h
executable file
·98 lines (79 loc) · 2.43 KB
/
common.h
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
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#define TRUE 1
#define FALSE 0
#define FLIPENDIAN(x) flipEndian((unsigned char *)(&(x)), sizeof(x))
#define FLIPENDIANLE(x) flipEndianLE((unsigned char *)(&(x)), sizeof(x))
#define IS_BIG_ENDIAN 0
#define IS_LITTLE_ENDIAN 1
#define TIME_OFFSET_FROM_UNIX 2082844800L
#define APPLE_TO_UNIX_TIME(x) ((x) - TIME_OFFSET_FROM_UNIX)
#define UNIX_TO_APPLE_TIME(x) ((x) + TIME_OFFSET_FROM_UNIX)
#define ASSERT(x, m) if(!(x)) { fflush(stdout); fprintf(stderr, "error: %s\n", m); perror("error"); fflush(stderr); exit(1); }
extern char endianness;
static inline void flipEndian(unsigned char* x, int length) {
int i;
unsigned char tmp;
if(endianness == IS_BIG_ENDIAN) {
return;
} else {
for(i = 0; i < (length / 2); i++) {
tmp = x[i];
x[i] = x[length - i - 1];
x[length - i - 1] = tmp;
}
}
}
static inline void flipEndianLE(unsigned char* x, int length) {
int i;
unsigned char tmp;
if(endianness == IS_LITTLE_ENDIAN) {
return;
} else {
for(i = 0; i < (length / 2); i++) {
tmp = x[i];
x[i] = x[length - i - 1];
x[length - i - 1] = tmp;
}
}
}
static inline void hexToBytes(const char* hex, uint8_t** buffer, size_t* bytes) {
*bytes = strlen(hex) / 2;
*buffer = (uint8_t*) malloc(*bytes);
size_t i;
for(i = 0; i < *bytes; i++) {
uint32_t byte;
sscanf(hex, "%2x", &byte);
(*buffer)[i] = byte;
hex += 2;
}
}
static inline void hexToInts(const char* hex, unsigned int** buffer, size_t* bytes) {
*bytes = strlen(hex) / 2;
*buffer = (unsigned int*) malloc((*bytes) * sizeof(int));
size_t i;
for(i = 0; i < *bytes; i++) {
sscanf(hex, "%2x", &((*buffer)[i]));
hex += 2;
}
}
struct io_func_struct;
typedef int (*readFunc)(struct io_func_struct* io, off_t location, size_t size, void *buffer);
typedef int (*writeFunc)(struct io_func_struct* io, off_t location, size_t size, void *buffer);
typedef void (*closeFunc)(struct io_func_struct* io);
typedef struct io_func_struct {
void* data;
readFunc read;
writeFunc write;
closeFunc close;
} io_func;
struct AbstractFile;
unsigned char* decodeBase64(char* toDecode, size_t* dataLength);
void writeBase64(struct AbstractFile* file, unsigned char* data, size_t dataLength, int tabLength, int width);
char* convertBase64(unsigned char* data, size_t dataLength, int tabLength, int width);
#endif