-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.c
53 lines (41 loc) · 1.1 KB
/
pack.c
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
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "pack.h"
uint8_t unpack_u8(const uint8_t **buf) {
uint8_t val = **buf;
(*buf)++;
return val;
}
uint16_t unpack_u16(const uint8_t **buf) {
uint16_t val;
memcpy(&val,*buf,sizeof(uint16_t));
(*buf) += sizeof(uint16_t);
return ntohs(val);
}
uint32_t unpack_u32(const uint8_t **buf) {
uint32_t val;
memcpy(&val, *buf, sizeof(uint32_t));
(*buf) += sizeof(uint32_t);
return ntohl(val);
}
uint8_t *unpack_bytes(const uint8_t **buf,size_t len,uint8_t *str) {
memcpy(str, *buf, len);
str[len]='\0';
(*buf) += len;
return str;
}
void pack_u8(uint8_t **buf, uint8_t val) {
**buf = val;
(*buf) += sizeof(uint8_t);
}
void pack_u16(uint8_t **buf, uint16_t val) {
uint16_t htonsval = htons(val);
memcpy(*buf, &htonsval , sizeof(uint16_t));
*buf += sizeof(uint16_t);
}
void pack_u32(uint8_t **buf, uint32_t val) {
uint32_t htonlval = htonl(val);
memcpy(*buf, &htonlval, sizeof(uint32_t));
(*buf) += sizeof(uint8_t);
}