-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.h
88 lines (74 loc) · 1.49 KB
/
util.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
#ifndef UTIL_H
#define UTIL_H
#include "rhd/heap_string.h"
#include <stdint.h>
#include "types.h"
static int dd(heap_string *s, uint32_t i)
{
int sz = heap_string_size( s );
union
{
uint32_t i;
uint8_t b[4];
} u = { .i = i };
for ( size_t i = 0; i < 4; ++i )
heap_string_push( s, u.b[i] );
return sz;
}
static int dq(heap_string *s, uint64_t i)
{
int sz = heap_string_size( s );
union
{
uint64_t i;
uint8_t b[8];
} u = { .i = i };
for ( size_t i = 0; i < 8; ++i )
heap_string_push( s, u.b[i] );
return sz;
}
static int dw( heap_string* s, uint16_t i )
{
int sz = heap_string_size( s );
union
{
uint16_t s;
uint8_t b[2];
} u = { .s = i };
heap_string_push( s, u.b[0] );
heap_string_push( s, u.b[1] );
return sz;
}
static int db(heap_string *s, u8 op)
{
heap_string_push(s, op);
return heap_string_size(s) - 1;
}
static void pad(heap_string *s, u32 n)
{
for(int i = 0; i < n; ++i)
heap_string_push(s, 0x0);
}
static int align_to(int pos, int align)
{
if(pos % align == 0)
return pos; //no alignment needed
return align - (pos % align);
}
static void pad_align(heap_string *s, int align)
{
int pos = heap_string_size(s);
if(pos % align == 0)
return; //no alignment needed
int m = align - (pos % align);
for(int i = 0; i < m; ++i)
heap_string_push(s, 0);
}
static void buf(heap_string *s, const char *buf, size_t len)
{
for(size_t i = 0; i < len; ++i)
{
heap_string_push(s, buf[i] & 0xff);
}
}
#endif