OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gt2Encode.h
1//TODO: Address Byte order & Byte alignment issues
2#ifndef _GT_ENCODE_H
3#define _GT_ENCODE_H
4
5#include <stdarg.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#if defined(UNDER_CE) || defined(__mips64) || defined(_PSP)
12#define ALIGNED_COPY
13//the aligned copy code all needs to be optimized
14#endif
15
16
17
18
19// Used to identify the type of message so you can look up the correct format string
20// and pass the correct parameters
21// You should use 1 msgType for each unique format string/parameter combination
23typedef unsigned short GTMessageType;
24
25// Encode a message into outBuffer
26// Returns the length of the encoded message, or -1 to indicate insufficient space
27// You must make sure the number of arguments match the fmtString list
29int gtEncode(GTMessageType msgType, const char *fmtString, char *outBuffer, int outLength, ...);
30int gtEncodeV(GTMessageType msgType, const char *fmtString, char *outBuffer, int outLength, va_list *args);
31int gtEncodeNoType(const char *fmtString, char *outBuffer, int outLength, ...);
32int gtEncodeNoTypeV(const char *fmtString, char *outBuffer, int outLength, va_list *args);
33
34// Decode the message from inBuffer into the vars provided
35// Returns -1 if there was a problem with the buffer
36// Vars should all be pointers (as if using scanf)
37// You must make sure the number of arguments match the fmtString list
39int gtDecode(const char *fmtString, char *inBuffer, int inLength, ...);
40int gtDecodeV(const char *fmtString, char *inBuffer, int inLength, va_list *args);
41int gtDecodeNoType(const char *fmtString, char *inBuffer, int inLength, ...);
42int gtDecodeNoTypeV(const char *fmtString, char *inBuffer, int inLength, va_list *args);
43
44// Retrieve the message type for an encoded message
46GTMessageType gtEncodedMessageType (char *inBuffer);
47// change the message type for an encoded message
48void gtEncodedMessageTypeSet (char *inBuffer, GTMessageType newtype);
49
50// This handles alignment issues and endianess
52void gt2MemCopy16(char *out, char const *in);
53void gt2MemCopy32(char *out, char const *in);
54void gt2MemCopy64(char *out, char const *in);
55void gt2MemCopy(char *out, char const *in, int size);
56
57/****************************
58Types that can be sent using the encode/decode functions
59Most are self-explanatory, but the following require some clarification:
60
61GT_CSTR: This is a NUL terminated C-string. The length is determined automatically.
62The NUL character is restored in decode. You simply pass the char * string as
63the arguement for Encode. Note that in the pointer passed to Decode must have enough
64memory allocated to it for the max string that will be encoded - otherwise
65the destination may get trashed. If you cannot guarantee them max length of the
66string, you should use GT_RAW (see below).
67
68GT_DBSTR: Same as a GT_CSTR, except with 2-byte characters instead of single byte.
69String must be terminated with a double NUL character.
70
71GT_RAW: Use raw to send data blocks, structures, arrays, etc - although you must
72make sure they're the same on all platforms!
73Requires you to pass both a buffer and a length arguement to Encode and Decode.
74You should pass the buffer first, then the length.
75For Decode, you must initialize the length pointer to the max length of the buffer.
76If the decoded data exceeds this length, the Decode function will return -1, and the
77length pointer will be set to the actual length. You can then call Decode again with
78a buffer that is at least the required length.
79Example:
80gtEncode(0, "r", buf, buflen, "somerawdata",10);
81char *rawbuffer = malloc(5);
82char rawlen = 5;
83ret = gtDecode(0, "r", buf, buflen, rawbuffer, &rawlen);
84//gtDecode will return -1, and rawlen will be set to 10
85if (ret == -1)
86{
87 rawbuffer = realloc(rawbuffer, rawlen);
88 gtDecode(0, "r", buf, buflen, rawbuffer, &rawlen);
89 //gtDecode will now succeed
90}
91
92GT_CSTR_PTR, GT_DBSTR_PTR, GT_RAW_PTR: For Decode, instead of copying the data from
93input buffer into the pointer provided, the pointer is simply set to the
94offset of the data in the input buffer. This removes the need to allocate
95memory for the pointers before hand, and elimantes an extra memory copy.
96You will need to pass in double pointers (e.g. char **) so that the pointer
97can be changed.
98However, you must make sure you don't try to use the pointers after the
99input buffer is freed/changed.
100Note that the buffer passed in gtReceivedCallback must be copied off if
101you want to continue using it after you return from the callback (or, you
102can use the non-PTR versions that copy off into the buffers you provide
103automatically)
104You can pass the _PTR versions to Encode and they will behave exactly as
105the regular versions.
106
107GT_CSTR_PTR, GT_CSTR_ARRAY_PTR: Same as GT_CSTR and GT_CSTR_PTR, but uses
108an array of strings instead of a single string. The array of strings is
109terminated by an empty string (a single NUL character).
110
111GT_BIT: If you pass all your bits together in the format string, they will be
112packed together to save space. So, the format string "zzzzzzzz" will only take
1131 byte for the data (+2 bytes for the message type).
114Note that if you have other types between the bits, the packing will NOT occur,
115e.g.: "ziz" will use 6 bytes (2 for the bits, 4 for the int), whereas "zzi" would use
116only 5 bytes (1 for the bits, 4 for the int)
117The argument type for bits is char for Encode and char * for Decode -
118If the char is 0, the bit will not be set, if it's non-zero, the bit will be set.
119Note that in Decode, the set bit will always be returned as 1 (not the non-zero value
120you set)
121*/
122
123#define GT_INT 'i'
124#define GT_INT_ "i"
125#define GT_INT_TYPE int
126#define GT_UINT 'u'
127#define GT_UINT_ "u"
128#define GT_UINT_TYPE unsigned int
129#define GT_SHORT 'o'
130#define GT_SHORT_ "o"
131#define GT_SHORT_TYPE short
132#define GT_USHORT 'p'
133#define GT_USHORT_ "p"
134#define GT_USHORT_TYPE unsigned short
135#define GT_CHAR 'c'
136#define GT_CHAR_ "c"
137#define GT_CHAR_TYPE signed char
138#define GT_UCHAR 'b'
139#define GT_UCHAR_ "b"
140#define GT_UCHAR_TYPE unsigned char
141#define GT_FLOAT 'f'
142#define GT_FLOAT_ "f"
143#define GT_FLOAT_TYPE float
144#define GT_DOUBLE 'd'
145#define GT_DOUBLE_ "d"
146#define GT_DOUBLE_TYPE double
147#define GT_CSTR 's'
148#define GT_CSTR_ "s"
149#define GT_CSTR_TYPE char *
150#define GT_CSTR_PTR 'S'
151#define GT_CSTR_PTR_ "S"
152#define GT_CSTR_PTR_TYPE char **
153#define GT_DBSTR 'w'
154#define GT_DBSTR_ "w"
155#define GT_DBSTR_TYPE short *
156#define GT_DBSTR_PTR 'W'
157#define GT_DBSTR_PTR_ "W"
158#define GT_DBSTR_PTR_TYPE short **
159#define GT_CSTR_ARRAY 'a'
160#define GT_CSTR_ARRAY_ "a"
161#define GT_CSTR_ARRAY_TYPE char *
162#define GT_CSTR_ARRAY_PTR 'A'
163#define GT_CSTR_ARRAY_PTR_ "A"
164#define GT_CSTR_ARRAY_PTR_TYPE char **
165#define GT_RAW 'r' //two parameters! (data, then length)
166#define GT_RAW_ "r" //two parameters!
167#define GT_RAW_TYPE char *
168#define GT_RAW_PTR 'R'
169#define GT_RAW_PTR_ "R"
170#define GT_RAW_PTR_TYPE char **
171#define GT_BIT 'z'
172#define GT_BIT_ "z"
173#define GT_BIT_TYPE unsigned char
174
175
176#ifdef __cplusplus
177}
178#endif
179
180#endif