File: Marshal.cpp

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (427 lines) | stat: -rw-r--r-- 13,352 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#include "EmulatorCommon.h"
#include "Marshal.h"

#include "Byteswapping.h"		// Canonical

// -------------------------
// ----- Binary buffer -----
// -------------------------

void* Marshal::GetBuffer (uaecptr p, long len)
{
	void*	result = NULL;

	if (p)
	{
		result = Platform::AllocateMemory (len);

		if (result)
		{
			uae_memcpy (result, p, len);
		}
	}

	return result;
}

#if 0	// inline
template <class T>
void Marshal::PutBuffer (uaecptr p, T*& buf, long len)
{
	if (p)
	{
		uae_memcpy (p, (void*) buf, len);
		Platform::DisposeMemory (buf);
		buf = NULL;
	}
}
#endif


// ---------------------
// ----- EventType -----
// ---------------------

EventType Marshal::GetEventType (uaecptr p)
{
	EventType	event;
	memset (&event, 0, sizeof (event));

	if (p)
	{
		event.eType		= get_word (p + offsetof (EventType, eType));
		event.penDown	= get_byte (p + offsetof (EventType, penDown));
		event.screenX	= get_word (p + offsetof (EventType, screenX));
		event.screenY	= get_word (p + offsetof (EventType, screenY));

		// Aargh! Need to do all the union structs!
	}

	return event;
}


void Marshal::PutEventType (uaecptr p, EventType& event)
{
	if (p)
	{
		put_word (p + offsetof (EventType, eType),		event.eType);
		put_byte (p + offsetof (EventType, penDown),	event.penDown);
		put_word (p + offsetof (EventType, screenX),	event.screenX);
		put_word (p + offsetof (EventType, screenY),	event.screenY);

		// Aargh! Need to do all the union structs!
	}
}


// -----------------------------
// ----- NetSocketAddrType -----
// -----------------------------

NetSocketAddrType Marshal::GetNetSocketAddrType (uaecptr p)
{
	NetSocketAddrType	netSocketAddr;
	memset (&netSocketAddr, 0, sizeof (netSocketAddr));

	if (p)
	{
		// We should be able to pretty much just block-copy this sucker without
		// having to worry too much about byte-swapping the contents.  A generic
		// address block is a 2-byte family in HBO followed by 14 bytes of data.
		// For Internet addresses, this 14 bytes of data consists of an IP address
		// and a socket in NBO, so no byteswapping is needed.  For Raw addresses,
		// Palm OS has defined the contents to be a 2-byte instance and a 4-byte
		// creator; there's no mention of their byte-ordering, so for now let's
		// assume NBO as well.
		//
		// By doing a blind memcpy of the contents, we relieve ourselves of the
		// responsibity of determining if they're actually initialized (if they're
		// not initialized, the family field may contain garbage, and we may go
		// off in the weeds trying to interpret the rest of the contents).

		switch (get_word (p))
		{
			case netSocketAddrRaw:
				uae_memcpy ((void*) &netSocketAddr, p, sizeof (NetSocketAddrRawType));
				break;

			case netSocketAddrINET:
				uae_memcpy ((void*) &netSocketAddr, p, sizeof (NetSocketAddrINType));
				break;

			default:
				// Do the best we can...
				uae_memcpy ((void*) &netSocketAddr, p, sizeof (NetSocketAddrType));
				break;
		}

		Canonical (netSocketAddr.family);
	}

	return netSocketAddr;
}


void Marshal::PutNetSocketAddrType (uaecptr p, NetSocketAddrType& netSocketAddr)
{
	if (p)
	{
		SWord	family = netSocketAddr.family;

		Canonical (netSocketAddr.family);

		switch (family)
		{
			case netSocketAddrRaw:
				uae_memcpy (p, (void*) &netSocketAddr, sizeof (NetSocketAddrRawType));
				break;

			case netSocketAddrINET:
				uae_memcpy (p, (void*) &netSocketAddr, sizeof (NetSocketAddrINType));
				break;

			default:
				// Do the best we can...
				uae_memcpy (p, (void*) &netSocketAddr, sizeof (NetSocketAddrType));
				break;
		}

		Canonical (netSocketAddr.family);
	}
}


// --------------------------
// ----- NetIOParamType -----
// --------------------------

NetIOParamType Marshal::GetNetIOParamType (uaecptr p)
{
	NetIOParamType	netIOParam;
	memset (&netIOParam, 0, sizeof (netIOParam));

	if (p)
	{
		netIOParam.addrLen			= get_word (p + offsetof (NetIOParamType, addrLen));
		netIOParam.iovLen			= get_word (p + offsetof (NetIOParamType, iovLen));
		netIOParam.accessRightsLen	= get_word (p + offsetof (NetIOParamType, accessRightsLen));

		netIOParam.addrP			= (BytePtr) GetBuffer (p + offsetof (NetIOParamType, addrP), netIOParam.addrLen);
		if (netIOParam.addrP)
			Canonical (((NetSocketAddrType*) (netIOParam.addrP))->family);

		netIOParam.accessRights		= (BytePtr) GetBuffer (p + offsetof (NetIOParamType, accessRights), netIOParam.accessRightsLen);

		netIOParam.iov				= (NetIOVecPtr) Platform::AllocateMemory (netIOParam.iovLen * sizeof (NetIOVecType));
		for (Word ii = 0; ii < netIOParam.iovLen; ++ii)
		{
			netIOParam.iov[ii].bufLen	= get_word (p + offsetof (NetIOParamType, iovLen) + ii * sizeof (NetIOVecType) + offsetof (NetIOVecType, bufLen));
			netIOParam.iov[ii].bufP		= (BytePtr) GetBuffer (p + offsetof (NetIOParamType, iovLen) + ii * sizeof (NetIOVecType) + offsetof (NetIOVecType, bufP), netIOParam.iov[ii].bufLen);
		}
	}

	return netIOParam;
}


void Marshal::PutNetIOParamType (uaecptr p, NetIOParamType& netIOParam)
{
	if (p)
	{
		put_word (p + offsetof (NetIOParamType, addrLen),			netIOParam.addrLen);
		put_word (p + offsetof (NetIOParamType, iovLen),			netIOParam.iovLen);
		put_word (p + offsetof (NetIOParamType, accessRightsLen),	netIOParam.accessRightsLen);

		if (netIOParam.addrP)
			Canonical (((NetSocketAddrType*) (netIOParam.addrP))->family);
		PutBuffer (p + offsetof (NetIOParamType, addrP),			netIOParam.addrP,			netIOParam.addrLen);
		if (netIOParam.addrP)
			Canonical (((NetSocketAddrType*) (netIOParam.addrP))->family);

//		PutBuffer (p + offsetof (NetIOParamType, iov),				netIOParam.iov,				netIOParam.iovLen);
		PutBuffer (p + offsetof (NetIOParamType, accessRights),		netIOParam.accessRights,	netIOParam.accessRightsLen);

		for (Word ii = 0; ii < netIOParam.iovLen; ++ii)
		{
			put_word (p + offsetof (NetIOParamType, iovLen) + ii * sizeof (NetIOVecType) + offsetof (NetIOVecType, bufLen), netIOParam.iov[ii].bufLen);
			PutBuffer (p + offsetof (NetIOParamType, iovLen) + ii * sizeof (NetIOVecType) + offsetof (NetIOVecType, bufP), netIOParam.iov[ii].bufP, netIOParam.iov[ii].bufLen);
		}
		Platform::DisposeMemory (netIOParam.iov);
	}
}


// ------------------------------
// ----- NetHostInfoBufType -----
// ------------------------------

/*
typedef struct {
	NetHostInfoType	hostInfo;
	{
		CharPtr			nameP; -------------------------+
		CharPtr*		nameAliasesP;-------------------|---+
		Word			addrType;                       |   |
		Word			addrLen;                        |   |
		BytePtr*		addrListP;----------------------|---|---+
	}                                                   |   |   |
										                |   |   |
	Char			name[netDNSMaxDomainName+1];   <----+   |   |
                                                            |   |
	CharPtr			aliasList[netDNSMaxAliases+1];   <------+   |
	Char			aliases[netDNSMaxAliases][netDNSMaxDomainName+1];
                                                                |
	NetIPAddr*		addressList[netDNSMaxAddresses];   <--------+
	NetIPAddr		address[netDNSMaxAddresses];

	} NetHostInfoBufType, *NetHostInfoBufPtr;
*/

NetHostInfoBufType	Marshal::GetNetHostInfoBufType (uaecptr p)
{
	NetHostInfoBufType	netHostInfoBufType;
	memset (&netHostInfoBufType, 0, sizeof (netHostInfoBufType));

	if (p)
	{
	}

	return netHostInfoBufType;
}


void Marshal::PutNetHostInfoBufType (uaecptr p, NetHostInfoBufType& netHostInfoBuf)
{
	if (p)
	{
		int	ii;	// Goddampieceoshit VC++ doesn't scope loop variables right!

		// First copy over the easy fields (the ones with real values in them).

			// NetHostInfoBufType.hostInfo.addrType
		put_word (p + offsetof (NetHostInfoBufType, hostInfo.addrType),
					netHostInfoBuf.hostInfo.addrType);

			// NetHostInfoBufType.hostInfo.addrLen
		put_word (p + offsetof (NetHostInfoBufType, hostInfo.addrLen),
					netHostInfoBuf.hostInfo.addrLen);

			// NetHostInfoBufType.name
		uae_strcpy (p + offsetof (NetHostInfoBufType, name), netHostInfoBuf.name);

			// NetHostInfoBufType.aliases
		uae_memcpy (p + offsetof (NetHostInfoBufType, aliases),
					(void*) netHostInfoBuf.aliases,
					sizeof (netHostInfoBuf.aliases));

			// NetHostInfoBufType.address
			// Copy them one at a time to sort out endian issues.  Just how this
			// is supposed to be done is not clear (NetLib documentation says that
			// the addresses are in HBO, while the WinSock header says that the
			// addresses are supplied in NBO but returned in HBO), but the following
			// seems to work.
		for (ii = 0; ii < netDNSMaxAddresses; ++ii)
		{
			NetIPAddr	addr = ntohl (netHostInfoBuf.address[ii]);
			put_long (p + offsetof (NetHostInfoBufType, address[ii]), NetHToNL (addr));
		}

		// Second, set up the pointers to those values.

			// NetHostInfoType.hostInfo.nameP
		put_long (p + offsetof (NetHostInfoBufType, hostInfo.nameP),
					p + offsetof (NetHostInfoBufType, name));

			// NetHostInfoType.hostInfo.nameAliasesP
		put_long (p + offsetof (NetHostInfoBufType, hostInfo.nameAliasesP),
					p + offsetof (NetHostInfoBufType, aliasList));

			// NetHostInfoType.hostInfo.addrListP
		put_long (p + offsetof (NetHostInfoBufType, hostInfo.addrListP),
					p + offsetof (NetHostInfoBufType, addressList));

			// NetHostInfoType.aliasList
		for (ii = 0; ii < netDNSMaxAliases+1; ++ii)
		{
			if (netHostInfoBuf.aliasList[ii] != NULL)
			{
				put_long (p + offsetof (NetHostInfoBufType, aliasList[ii]),
					p + netHostInfoBuf.aliasList[ii] - (CharPtr) &netHostInfoBuf);
			}
			else
			{
				put_long (p + offsetof (NetHostInfoBufType, aliasList[ii]), UAE_NULL);
				break;
			}
		}

			// NetHostInfoType.addressList
		for (ii = 0; ii < netDNSMaxAddresses; ++ii)
		{
			if (netHostInfoBuf.addressList[ii] != NULL)
			{
				put_long (p + offsetof (NetHostInfoBufType, addressList[ii]),
							p + offsetof (NetHostInfoBufType, address[ii]));
			}
			else
			{
				put_long (p + offsetof (NetHostInfoBufType, addressList[ii]),
							UAE_NULL);
				break;
			}
		}
	}
}


// ------------------------------
// ----- NetServInfoBufType -----
// ------------------------------

NetServInfoBufType	Marshal::GetNetServInfoBufType (uaecptr p)
{
	NetServInfoBufType	netServInfoBuf;
	memset (&netServInfoBuf, 0, sizeof (netServInfoBuf));

	if (p)
	{
	}

	return netServInfoBuf;
}

void Marshal::PutNetServInfoBufType (uaecptr p, NetServInfoBufType& netServInfoBuf)
{
	if (p)
	{
		// =======================================================
		// Convert NetHostInfoBufType.hostInfo
		// =======================================================

		// Get a handy pointer to the servInfo field.
		uaecptr	p2 = p + offsetof (NetServInfoBufType, servInfo);

		// -------------------------------------------------------
		// NetServInfoBufType.servInfo.nameP = &NetServInfoBufType.name
		// -------------------------------------------------------

		put_long (p2 + offsetof (NetServInfoType, nameP), p + offsetof (NetServInfoBufType, name));

		// -------------------------------------------------------
		// NetServInfoBufType.servInfo.nameAliasesP = &NetServInfoBufType.aliasList
		// -------------------------------------------------------

		put_long (p2 + offsetof (NetServInfoType, nameAliasesP), p + offsetof (NetServInfoBufType, aliasList));

		Word	port = ntohs (netServInfoBuf.servInfo.port);
		put_word (p2 + offsetof (NetServInfoType, port), NetHToNS (port));

		// -------------------------------------------------------
		// NetServInfoBufType.servInfo.protoP = &NetServInfoBufType.protoName
		// -------------------------------------------------------

		put_long (p2 + offsetof (NetServInfoType, protoP), p + offsetof (NetServInfoBufType, protoName));


		// =======================================================
		// Convert remaining NetHostInfoBufType fields
		// =======================================================

		// -------------------------------------------------------
		// name, protoName
		// -------------------------------------------------------

		uae_memcpy (p + offsetof (NetServInfoBufType, name), (void*) &netServInfoBuf.name, netServMaxName+1);
		uae_memcpy (p + offsetof (NetServInfoBufType, protoName), (void*) &netServInfoBuf.protoName, netProtoMaxName+1);

		// -------------------------------------------------------
		// aliasList, addressList
		// -------------------------------------------------------

		long	offset;
		long	index = 0;
		while (netServInfoBuf.aliasList[index])
		{
			// aliasList contains a list of string addresses.  Calculate the offset
			// of that string from the beginning of the NetHostInfoBufType.
			offset = ((char*) &netServInfoBuf.aliasList[index]) - (char*) &netServInfoBuf;

			// Use that offset to calculate the address of the corresponding location
			// in the destination.
			put_long (p + offsetof (NetServInfoBufType, aliasList) + index * sizeof (CharPtr), p + offset);

			// Copy over the string.
			uae_strcpy (p + offset, netServInfoBuf.aliasList[index]);

			++index;
		}
		put_long (p + offsetof (NetServInfoBufType, aliasList) + index * sizeof (CharPtr), UAE_NULL);
	}
}