forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snis_marshal.h
92 lines (80 loc) · 3.87 KB
/
snis_marshal.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
#ifndef __SNIS_MARSHAL_C__
#define __SNIS_MARSHAL_C__
#pragma pack(1)
struct packed_double
{
int64_t fractional;
int32_t exponent;
};
#pragma pack()
struct packed_buffer
{
unsigned char *buffer;
uint16_t buffer_size; /* size of space allocated for buffer */
int buffer_cursor;
};
struct packed_buffer_queue_entry {
struct packed_buffer *buffer;
struct packed_buffer_queue_entry *next;
};
struct packed_buffer_queue {
struct packed_buffer_queue_entry *head, *tail;
};
#ifdef DEFINE_SNIS_MARSHAL_GLOBALS
#define GLOBAL
#else
#define GLOBAL extern
#endif
GLOBAL struct packed_buffer * packed_buffer_allocate(int size);
GLOBAL void packed_buffer_free(struct packed_buffer *pb);
/* For packed_buffer_append/extract, format is like:
* "b" = u8 (byte)
* "h" = u16 (half-word) ( ugh, I hate propagating the notion that "word == 16 bits")
* "w" = u32 (word)
* "q" = u64 (quad word)
* "s" = string (char *)
* "r" = raw (char *) (unsigned short len)
* "d" = double
* "DS" = 32-bit signed integer encoded double (takes 2 params, double + scale )
* "DU" = 32-bit unsigned integer encoded double (takes 2 params, double + scale )
*/
GLOBAL int packed_buffer_append(struct packed_buffer *pb, const char *format, ...);
GLOBAL int packed_buffer_append_va(struct packed_buffer *pb, const char *format,
va_list ap);
GLOBAL int packed_buffer_unpack(void*, const char *format, ...);
GLOBAL int packed_buffer_extract(struct packed_buffer *pb, const char *format, ...);
GLOBAL int packed_buffer_extract_va(struct packed_buffer *pb, const char *format,
va_list ap);
GLOBAL struct packed_buffer *packed_buffer_new(const char *format, ...);
GLOBAL int packed_buffer_append_double(struct packed_buffer *pb, double value);
GLOBAL int packed_buffer_append_u16(struct packed_buffer *pb, uint16_t value);
GLOBAL int packed_buffer_append_u8(struct packed_buffer *pb, uint8_t value);
GLOBAL int packed_buffer_append_u32(struct packed_buffer *pb, uint32_t value);
GLOBAL int packed_buffer_append_u64(struct packed_buffer *pb, uint64_t value);
GLOBAL uint16_t packed_buffer_extract_u16(struct packed_buffer *pb);
GLOBAL uint8_t packed_buffer_extract_u8(struct packed_buffer *pb);
GLOBAL uint32_t packed_buffer_extract_u32(struct packed_buffer *pb);
GLOBAL uint64_t packed_buffer_extract_u64(struct packed_buffer *pb);
GLOBAL double packed_buffer_extract_double(struct packed_buffer *pb);
GLOBAL int packed_buffer_append_string(struct packed_buffer *pb, unsigned char *str, unsigned short len);
GLOBAL int packed_buffer_extract_string(struct packed_buffer *pb, char *buffer, int buflen);
GLOBAL int packed_buffer_append_raw(struct packed_buffer *pb, char *buffer, unsigned short len);
GLOBAL int packed_buffer_extract_raw(struct packed_buffer *pb, char *buffer, unsigned short len);
GLOBAL struct packed_buffer *packed_buffer_queue_combine(struct packed_buffer_queue *pbqh, pthread_mutex_t *mutex);
GLOBAL void packed_buffer_queue_add(struct packed_buffer_queue *pbqh, struct packed_buffer *pb,
pthread_mutex_t *mutex);
GLOBAL void packed_buffer_queue_init(struct packed_buffer_queue *pbq);
GLOBAL void packed_buffer_queue_print(struct packed_buffer_queue *pbg);
GLOBAL struct packed_buffer *packed_buffer_copy(struct packed_buffer *pb);
GLOBAL void packed_buffer_init(struct packed_buffer * pb, void *buffer, int size);
GLOBAL uint32_t dtou32(double d, uint32_t scale);
GLOBAL double u32tod(uint32_t u, uint32_t scale);
GLOBAL int32_t dtos32(double d, int32_t scale);
GLOBAL double s32tod(int32_t u, int32_t scale);
GLOBAL int packed_buffer_append_du32(struct packed_buffer *pb, double d, uint32_t scale);
GLOBAL int packed_buffer_append_ds32(struct packed_buffer *pb, double d, int32_t scale);
GLOBAL double packed_buffer_extract_du32(struct packed_buffer *pb, uint32_t scale);
GLOBAL double packed_buffer_extract_ds32(struct packed_buffer *pb, int32_t scale);
GLOBAL int calculate_buffer_size(const char *format);
#undef GLOBAL
#endif