-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket.h
91 lines (73 loc) · 2.75 KB
/
packet.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
/*
* Copyright (c) 2012 Martin Kleinhans <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _PACKET_H_
#define _PACKET_H_
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define bswap_64(x) OSSwapInt64(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_16(x) OSSwapInt16(x)
#else
#include <byteswap.h>
#ifndef HAVE_BYTE_SWAP_H
# ifndef bswap_64
extern unsigned long long bswap_64(unsigned long long v);
# endif
# ifndef bswap_32
extern unsigned long bswap_32(unsigned long v);
# endif
# ifndef bswap_16
extern unsigned short bswap_16(unsigned short v);
# endif
#endif
#endif
#ifndef uint8_t
#define uint8_t unsigned char
#endif
#ifndef uint16_t
#define uint16_t unsigned short
#endif
#ifndef uint32_t
#define uint32_t unsigned int
#endif
#ifndef uint64_t
#define uint64_t unsigned long long
#endif
struct sharcs_packet {
char *data;
int cursor;
int size,bufferSize;
};
struct sharcs_packet* packet_create_buffer(const char *src, int len);
struct sharcs_packet* packet_create();
void packet_delete(struct sharcs_packet *packet);
void packet_append8(struct sharcs_packet *packet,uint8_t byte);
void packet_append16(struct sharcs_packet *packet,uint16_t word);
void packet_append32(struct sharcs_packet *packet,uint32_t dword);
void packet_append64(struct sharcs_packet *packet,uint64_t qword);
void packet_append_string(struct sharcs_packet *packet,const char *string);
void packet_append_float(struct sharcs_packet *packet,float f);
uint8_t packet_read8(struct sharcs_packet *packet);
uint16_t packet_read16(struct sharcs_packet *packet);
uint32_t packet_read32(struct sharcs_packet *packet);
uint64_t packet_read64(struct sharcs_packet *packet);
const char* packet_read_string(struct sharcs_packet *packet);
float packet_read_float(struct sharcs_packet *packet);
const char* packet_buffer(struct sharcs_packet *packet);
int packet_size(struct sharcs_packet *packet);
void packet_seek(struct sharcs_packet *packet,int pos);
void packet_dump(struct sharcs_packet *packet);
#endif