File: sciSerialize.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (171 lines) | stat: -rw-r--r-- 4,754 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "sciSerialize.h"



///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeInt8(gsi_u8* theCursor, gsi_i8 theValue)
{
	// Copy int8 value to possibly misaligned destination
	gsi_u8* dst = (gsi_u8*)theCursor;
	
	*dst++ = (gsi_u8)theValue;

	return (gsi_u8*)dst;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeInt16(gsi_u8* theCursor, gsi_i16 theValue)
{
	// Convert to network byte order
	gsi_i16 netValue = (gsi_i16)htons(theValue);

	// Copy int16 value to possibly misaligned destination
	gsi_u8* dst = (gsi_u8*)theCursor;
	gsi_u8* src = (gsi_u8*)&netValue;
	
	*dst++ = *src++;
	*dst++ = *src++;

	return (gsi_u8*)dst;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeInt32(gsi_u8* theCursor, gsi_i32 theValue)
{
	// Convert to network byte order
	gsi_i32 netValue = (gsi_i32)htonl(theValue);

	// Copy int32 value to possibly misaligned destination
	gsi_u8* dst = (gsi_u8*)theCursor;
	gsi_u8* src = (gsi_u8*)&netValue;
	
	*dst++ = *src++;
	*dst++ = *src++;
	*dst++ = *src++;
	*dst++ = *src++;

	return (gsi_u8*)dst;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeInt64(gsi_u8* theCursor, gsi_i64 theValue)
{
	// Convert to network byte order
	//gsi_i32 netValue = (gsi_i32)htonl(theValue);

	// Copy int32 value to possibly misaligned destination
	gsi_u8* dst = (gsi_u8*)theCursor;
	gsi_u8* src = (gsi_u8*)&theValue;

#ifdef GSI_BIG_ENDIAN
	dst[0] = src[0];
	dst[1] = src[1];
	dst[2] = src[2];
	dst[3] = src[3];

	dst[4] = src[4];
	dst[5] = src[5];
	dst[6] = src[6];
	dst[7] = src[7];
#else
	dst[0] = src[7];
	dst[1] = src[6];
	dst[2] = src[5];
	dst[3] = src[4];

	dst[4] = src[3];
	dst[5] = src[2];
	dst[6] = src[1];
	dst[7] = src[0];
#endif 

	return (gsi_u8*)dst;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeFloat(gsi_u8* theCursor, float theValue)
{
	unsigned char netValue[4];
	gsi_u8 *dst, *src;

#if defined(GSI_BIG_ENDIAN)
	//swap byte ordering for floats to little endian - htonl doesn't work
	gsiFloatSwap(netValue, theValue);
#else
	memcpy(netValue, &theValue, 4);
#endif

	dst = (gsi_u8*)theCursor;
	src = (gsi_u8*)&netValue;
	
	*dst++ = *src++;
	*dst++ = *src++;
	*dst++ = *src++;
	*dst++ = *src++;

	return (gsi_u8*)dst;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeKey(gsi_u8* theCursor, gsi_u16 theKey)
{
	return sciSerializeInt16(theCursor, (gsi_i16)theKey);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeDataLength(gsi_u8* theCursor, gsi_u32 theDataLength)
{
	return sciSerializeInt32(theCursor, (gsi_i32)theDataLength);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u8* sciSerializeGUID(gsi_u8* theCursor, const gsi_u8 theGUID[SC_CONNECTION_GUID_SIZE])
{
	// a GUID is a string comprised of an int, two shorts and eight bytes
	// 6B29FC40-CA47-1067-B31D-00DD010662DA
	gsi_u32 anInt = 0;
	gsi_u32 aShort1 = 0;
	gsi_u32 aShort2 = 0;
	gsi_u32 aIntArray[8];
	gsi_u8 aByteArray[8]; 
	int i=0;

	sscanf((char *)theGUID, "%8x", &anInt);
	sscanf((char *)theGUID+9, "%4x", &aShort1);
	sscanf((char *)theGUID+14, "%4x", &aShort2);

	sscanf((char *)theGUID+19, "%2x", &aIntArray[0]);
	sscanf((char *)theGUID+21, "%2x", &aIntArray[1]);

	sscanf((char *)theGUID+24, "%2x%2x%2x%2x%2x%2x", &aIntArray[2],
		&aIntArray[3], &aIntArray[4], &aIntArray[5],
		&aIntArray[6], &aIntArray[7]);

	for (i=0; i < 8; i++)
		aByteArray[i] = (gsi_u8)aIntArray[i];

	theCursor = sciSerializeInt32(theCursor, (gsi_i32)anInt);
	theCursor = sciSerializeInt16(theCursor, (gsi_i16)aShort1);
	theCursor = sciSerializeInt16(theCursor, (gsi_i16)aShort2);
	memcpy(theCursor, aByteArray, 8);
	theCursor += 8;

	return theCursor;
}