File: sciMain.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 (706 lines) | stat: -rw-r--r-- 24,796 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "sci.h"
#include "sciInterface.h"
#include "sciReport.h"

#include "../md5.h"
#include "../common/gsRC4.h"

/* PUBLIC INTERFACE FUNCTIONS - SCINTERFACE.C CONTAINS PRIVATE INTERFACE */


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scInitialize(int theGameId, SCInterfacePtr* theInterfaceOut)
{
	SCInterface* anInterface  = NULL;
	SCResult     anInitResult = SCResult_NO_ERROR;

	// Check parameters
	if (theInterfaceOut == NULL)
	{
		return SCResult_INVALID_PARAMETERS;
	}

	// Clear out parameter
	*theInterfaceOut = NULL;

	// Obtain the internal interface
	anInitResult = sciInterfaceCreate(&anInterface);
	if (anInitResult != SCResult_NO_ERROR)
	{
		return anInitResult;
	}

	// Init the internal interface
	anInitResult = sciInterfaceInit(anInterface);
	if (anInitResult != SCResult_NO_ERROR)
	{
		return anInitResult;
	}

	anInterface->mGameId = (gsi_u32)theGameId;
	//anInterface->mOptionsFlags = (gsi_u32)theOptionsFlags;

	// Set the out parameter and return
	*theInterfaceOut = anInterface;
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scShutdown(SCInterfacePtr theInterface)
{
	SCInterface* anInterface = (SCInterface*)theInterface;

	// Check parameters
	if (anInterface == NULL)
	{
		return SCResult_INVALID_PARAMETERS;
	}

	// Destroy interface if necessary
	if (anInterface->mInit)
	{
		sciInterfaceDestroy(anInterface);
		gsifree(anInterface);
	}

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scThink(SCInterfacePtr theInterface)
{
	SCInterface* anInterface = (SCInterface*)theInterface;

	// Check parameters
	if (anInterface == NULL)
	{
		return SCResult_INVALID_PARAMETERS;
	}
	if (!anInterface->mInit)
	{
		return SCResult_NOT_INITIALIZED;
	}

	sciWsThink(&anInterface->mWebServices);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
const char * scGetSessionId(const SCInterfacePtr theInterface)
{
	SCInterface * anInterface = (SCInterface*)theInterface;
	GS_ASSERT(theInterface != NULL);
	GS_ASSERT(gsi_is_true(anInterface->mInit));
	return (char *)anInterface->mSessionId;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
const char * scGetConnectionId(const SCInterfacePtr theInterface)
{
	SCInterface * anInterface = (SCInterface*)theInterface;
	GS_ASSERT(theInterface != NULL);
	GS_ASSERT(gsi_is_true(anInterface->mInit));
	return (char *)anInterface->mConnectionId;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scSetSessionId(const SCInterfacePtr theInterface, const gsi_u8 theSessionId[SC_SESSION_GUID_SIZE])
{
	SCInterface * anInterface = (SCInterface*)theInterface;
	GS_ASSERT(theInterface != NULL);
	GS_ASSERT(gsi_is_true(anInterface->mInit));
	
	sciInterfaceSetSessionId(anInterface, (char *)theSessionId);
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// The certificate and private data may be NULL if the local client
// is an unauthenticated dedicated server
SCResult SC_CALL scCreateSession(SCInterfacePtr             theInterface,
							     const GSLoginCertificate * theCertificate,
							     const GSLoginPrivateData * thePrivateData,
							     SCCreateSessionCallback    theCallback,
							     gsi_time                   theTimeoutMs,
								 void *                     theUserData)
{
	SCInterface * anInterface = (SCInterface*)theInterface;
	GS_ASSERT(theInterface != NULL);
	GS_ASSERT(theCertificate != NULL || (theCertificate==NULL && thePrivateData==NULL));
	
	if (!wsLoginCertIsValid(theCertificate))
		return SCResult_INVALID_PARAMETERS;

	return sciWsCreateSession(&anInterface->mWebServices, anInterface->mGameId, theCertificate, thePrivateData, theCallback, theTimeoutMs, theUserData);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scCreateMatchlessSession(SCInterfacePtr    theInterface,
								 const GSLoginCertificate * theCertificate,
								 const GSLoginPrivateData * thePrivateData,
								 SCCreateSessionCallback    theCallback,
								 gsi_time                   theTimeoutMs,
								 void *                     theUserData)
{
	SCInterface * anInterface = (SCInterface*)theInterface;
	GS_ASSERT(theInterface != NULL);
	GS_ASSERT(theCertificate != NULL || (theCertificate==NULL && thePrivateData==NULL));

	if (!wsLoginCertIsValid(theCertificate))
		return SCResult_INVALID_PARAMETERS;

	return sciWsCreateMatchlessSession(&anInterface->mWebServices, anInterface->mGameId, theCertificate, thePrivateData, theCallback, theTimeoutMs, theUserData);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/*
SCResult SC_CALL scJoinSession(SCInterfacePtr     theInterface,
							   GSLoginCertificate * theCertificate,
							   GSLoginPrivateData * thePrivateData,
							   gsi_u8             theSessionId[SC_SESSION_GUID_SIZE],
							   SCSetReportIntentionCallback theCallback,
							   gsi_time           theTimeoutMs)
{
	SCInterface * anInterface = (SCInterface*)theInterface;
	GS_ASSERT(theInterface != NULL);

	memcpy(anInterface->mSessionId, theSessionId, SC_SESSION_GUID_SIZE);
	return SCResult_NO_ERROR;
}*/


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scSetReportIntention(const SCInterfacePtr         theInterface,
									  const gsi_u8                 theConnectionId[SC_CONNECTION_GUID_SIZE],
									  gsi_bool                     isAuthoritative,
									  const GSLoginCertificate *   theCertificate,
									  const GSLoginPrivateData *   thePrivateData,
									  SCSetReportIntentionCallback theCallback,
									  gsi_time                     theTimeoutMs,
									  void *                 theUserData)
{
	SCInterface* anInterface = (SCInterface*)theInterface;

	// Check parameters
	if (anInterface == NULL)
	{
		return SCResult_INVALID_PARAMETERS;
	}
	if (!theCertificate)
	{
		return SCResult_INVALID_PARAMETERS;
	}
	if (!thePrivateData)
	{
		return SCResult_INVALID_PARAMETERS;
	}
	if (!anInterface->mInit)
	{
		return SCResult_NOT_INITIALIZED;
	}

	sciInterfaceSetConnectionId(anInterface, (const char *)theConnectionId);
	// Call web service
	return sciWsSetReportIntention(&anInterface->mWebServices, anInterface->mGameId,
		(char *)anInterface->mSessionId, (char *)anInterface->mConnectionId, isAuthoritative,
		theCertificate, thePrivateData, theCallback, theTimeoutMs, theUserData);
}

									 
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scSubmitReport(const SCInterfacePtr  theInterface,
								const SCReportPtr      theReport,
								gsi_bool              isAuthoritative,
								const GSLoginCertificate * theCertificate,
								const GSLoginPrivateData * thePrivateData,
								SCSubmitReportCallback theCallback,
								gsi_time               theTimeoutMs,
								void *           theUserData)
{
	SCInterface* anInterface = (SCInterface*)theInterface;
	SCIReport *aReport = (SCIReport *)theReport;
	//SCResult aResult = SCResult_NO_ERROR;
	
	// Check parameters
	if (anInterface == NULL)
	{
		return SCResult_INVALID_PARAMETERS;
	}
	if (!anInterface->mInit)
	{
		return SCResult_NOT_INITIALIZED;
	}

	// Generate report from report data
	//	aResult = sciGenerateReport(theReportData, theProfileID, &aReport);
	//if (aResult != SCResult_NO_ERROR)
	//	return aResult;

	// Prepare the report hash
	{
		SCIReportHeader * header = (SCIReportHeader*)aReport->mBuffer.mData;
		MD5_CTX md5;
		// Clear out the checksum portion of the header so that the 
		// MD5 hash is calculated on the entire report without a checksum
		memset(header->mChecksum, 0, sizeof(header->mChecksum));
		
		MD5Init(&md5);
		MD5Update(&md5, (unsigned char *)aReport->mBuffer.mData, aReport->mBuffer.mPos);
		MD5Final(header->mChecksum, &md5);	
	}
	
	// Call web service
	return sciWsSubmitReport(&anInterface->mWebServices, anInterface->mGameId,
		(char *)anInterface->mSessionId, (char *)anInterface->mConnectionId, aReport, isAuthoritative,
		theCertificate, thePrivateData, theCallback, theTimeoutMs, theUserData);

}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scCreateReport(const SCInterfacePtr theInterface, 
								gsi_u32 theHeaderVersion,
								gsi_u32 thePlayerCount,
								gsi_u32 theTeamCount,
								SCReportPtr * theReportDataOut)
{
	SCResult aResult = SCResult_NO_ERROR;
	SCIReport * aReport = NULL;
	SCInterface * aInterface = (SCInterface*)theInterface;

	GS_ASSERT(theInterface != NULL);
	GS_ASSERT(theReportDataOut != NULL);
	if (theInterface == NULL)
		return SCResult_INVALID_PARAMETERS;
	if (theReportDataOut == NULL)
		return SCResult_INVALID_PARAMETERS;
	
	aResult = sciCreateReport(aInterface->mSessionId, theHeaderVersion, 
		thePlayerCount, theTeamCount, &aReport);
	if (aResult == SCResult_NO_ERROR)
		*theReportDataOut = (SCReportPtr)aReport;

	return aResult;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportBeginGlobalData(SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;

	return sciReportBeginGlobalData(aReport);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportBeginPlayerData(SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;

	return sciReportBeginPlayerData(aReport);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportBeginTeamData(SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;

	return sciReportBeginTeamData(aReport);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportBeginNewTeam(SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	//return SCResult_NO_ERROR;
	return sciReportBeginNewTeam(aReport);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportBeginNewPlayer(SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	//return SCResult_NO_ERROR;
	return sciReportBeginNewPlayer(aReport);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportEnd(SCReportPtr theReport, gsi_bool isAuth, SCGameStatus theStatus)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;

	return sciReportEnd(aReport, isAuth, theStatus);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportSetPlayerData(SCReportPtr theReport,
								       gsi_u32 thePlayerIndex,
								       const gsi_u8  thePlayerConnectionId[SC_CONNECTION_GUID_SIZE],
								       gsi_u32 thePlayerTeamId,
								       SCGameResult theResult,
								       gsi_u32 theProfileId,
								       const GSLoginCertificate * theCertificate,
								       const gsi_u8  theAuthHash[16])
{
	SCIReport * aReport = NULL;
	SCResult aResult = SCResult_NO_ERROR;
	gsi_u32 i;
	gsi_bool isNewTeam = gsi_true;

	GS_ASSERT(theReport != NULL);
	GS_ASSERT(theCertificate != NULL);
	GSI_UNUSED(theProfileId);
	aReport = (SCIReport*)theReport;
	
	// store the team ID here and use this in order to index the team game results
	for (i=0; i<aReport->mNumTeamsReported; i++)
	{
		// has the team already been reported by another player?
		if (aReport->mTeamIds[i] == thePlayerTeamId)
		{
			isNewTeam = gsi_false;
			break;
		}
	}
	
	if (isNewTeam)
	{	
		aReport->mNumTeamsReported++;

		// have they gone over the limit of teams? If so, this is an error
		GS_ASSERT(aReport->mNumTeamsReported < SC_MAX_NUM_TEAMS);
		if (aReport->mNumTeamsReported > SC_MAX_NUM_TEAMS)
			return SCResult_OUT_OF_MEMORY;	
		
		// if no problems, store the teamId
		aReport->mTeamIds[aReport->mNumTeamsReported-1] = thePlayerTeamId;
	}


	if (aResult == SCResult_NO_ERROR) aResult = sciReportSetPlayerConnectionId(aReport, thePlayerIndex, thePlayerConnectionId);
	if (aResult == SCResult_NO_ERROR) aResult = sciReportSetPlayerTeamIndex   (aReport, thePlayerIndex, thePlayerTeamId);
	if (aResult == SCResult_NO_ERROR) aResult = sciReportSetPlayerGameResult  (aReport, thePlayerIndex, theResult);
	if (aResult == SCResult_NO_ERROR) aResult = sciReportSetPlayerAuthInfo    (aReport, thePlayerIndex, theCertificate, theAuthHash);

	return aResult;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportSetTeamData(const SCReportPtr theReport,
									 gsi_u32     theTeamId,
									 SCGameResult theResult)
{
	SCIReport * aReport = NULL;
	gsi_u32 i;
	gsi_i32 aTeamIndex = -1;
	GS_ASSERT(theReport != NULL);
	GS_ASSERT(theResult < SCGameResultMax);

	aReport = (SCIReport*)theReport;

	// redundancy check - have they gone over the max limit of teams?
	GS_ASSERT(aReport->mNumTeamsReported < SC_MAX_NUM_TEAMS);
		
	// find the proper team index based on the teamId
	for (i=0; i<aReport->mNumTeamsReported; i++)
	{
		if (aReport->mTeamIds[i] == theTeamId)
		{
			aTeamIndex = (gsi_i32)i;
			break;
		}
	}

	// if no such team exists in our list, an invalid ID was given
	if (aTeamIndex == -1)
		return SCResult_INVALID_PARAMETERS;
	
	// the teamindex reported here needs to be 0 based, which is why we subtract 1
	return sciReportSetTeamGameResult(aReport, (gsi_u32)aTeamIndex, theResult);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportSetAsMatchless(const SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	return(sciReportSetAsMatchless(aReport));
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportAddIntValue(const SCReportPtr theReport, 
									 gsi_u16     theKeyId, 
									 gsi_i32     theValue)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	return sciReportAddIntValue(aReport, theKeyId, theValue);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportAddInt64Value(SCReportPtr theReport, 
									   gsi_u16 theKeyId, 
									   gsi_i64 theValue)
{
	SCIReport *aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport *)theReport;
	return sciReportAddInt64Value(aReport, theKeyId, theValue);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportAddShortValue(const SCReportPtr theReport, 
									   gsi_u16     theKeyId, 
									   gsi_i16     theValue)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	return sciReportAddShortValue(aReport, theKeyId, theValue);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportAddByteValue(const SCReportPtr theReport, 
									  gsi_u16     theKeyId, 
									  gsi_i8      theValue)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	return sciReportAddByteValue(aReport, theKeyId, theValue);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportAddFloatValue(const SCReportPtr  theReport, 
									   gsi_u16      theKeyId, 
									   float		theValue)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;
	return sciReportAddFloatValue(aReport, theKeyId, theValue);

}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scReportAddStringValue(const SCReportPtr  theReport, 
									    gsi_u16      theKeyId, 
									    const gsi_char * theValue)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	GS_ASSERT(theValue != NULL);
	aReport = (SCIReport*)theReport;
	return sciReportAddStringValue(aReport, theKeyId, theValue);

}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scDestroyReport(SCReportPtr theReport)
{
	SCIReport * aReport = NULL;
	GS_ASSERT(theReport != NULL);
	aReport = (SCIReport*)theReport;

	return sciDestroyReport(aReport);
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Create a random key for this cipher
SCResult SC_CALL scPeerCipherInit(const GSLoginCertificate * theLocalCert, SCPeerCipher * theCipher)
{
	// hardcoded to a 256-bit key block, although some ciphers will not use the entire block

	// What to use for a random seed?
	//     1) 16 bytes from our Util_Rand seeded with current time
	//     2) Hashed with 16 bytes from the cert public key (for a little extra randomness)
	//     3) Repeat 1-2 for every 16 bytes of key block needed
	unsigned char randBytes[32];
	int i=0;
	MD5_CTX md5;

	GS_ASSERT(theCipher != NULL);
	//CANNOT assert on constant expressions, they are meaningless.
	//GS_ASSERT(GS_CRYPT_RSA_BYTE_SIZE >= 32); // crypt lib must support 128-bit keys.

	#if defined (GS_CRYPT_NO_RANDOM)
		Util_RandSeed(0x2d2d2d2d);
	#else
		Util_RandSeed(current_time());
	#endif

	for (i=0; i < 32; i++)
		randBytes[i] = (unsigned char)Util_RandInt(0x00, 0xFF);

	// Calc the first half, bytes 0-15
	MD5Init(&md5);
	MD5Update(&md5, randBytes, 16);
	MD5Update(&md5, (unsigned char*)theLocalCert->mPeerPublicKey.modulus.mData, 16); // first 16 bytes of the key
	MD5Final(&theCipher->mKey[0], &md5);

	// Calc the second half, bytes 16-31
	MD5Init(&md5);
	MD5Update(&md5, &randBytes[16], 16);
	MD5Update(&md5, (unsigned char*)&theLocalCert->mPeerPublicKey.modulus.mData[16], 16); // last 16 bytes of the key
	MD5Final(&theCipher->mKey[16], &md5);

	theCipher->mKeyLen = 32;

	RC4Init(&theCipher->mRC4, theCipher->mKey, (int)theCipher->mKeyLen);
	theCipher->mInitialized = gsi_true;

	gsDebugFormat(GSIDebugCat_App, GSIDebugType_Misc, GSIDebugLevel_Notice, "Created PeerCipher key block\r\n");
	gsDebugBinary(GSIDebugCat_App, GSIDebugType_Misc, GSIDebugLevel_Notice, (char *)theCipher->mKey, (gsi_i32)theCipher->mKeyLen);
	
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scPeerCipherCreateKeyExchangeMsg(const GSLoginCertificate * theRemoteCert, const SCPeerCipher * theCipher, SCPeerKeyExchangeMsg theMsgOut)
{
	// Encrypt the key with the recipients public key, this makes it safe to transmit
	if (0 == gsCryptRSAEncryptBuffer(&theRemoteCert->mPeerPublicKey, theCipher->mKey, theCipher->mKeyLen, (unsigned char *)theMsgOut))
		return SCResult_NO_ERROR;
	else
		return SCResult_UNKNOWN_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scPeerCipherParseKeyExchangeMsg (const GSLoginCertificate * theLocalCert, const GSLoginPrivateData * theCertPrivateData, const SCPeerKeyExchangeMsg theMsg, SCPeerCipher * theCipherOut)
{
	GSI_UNUSED(theLocalCert);
	// Decrypt the key with the local player's private key
	if (0 == gsCryptRSADecryptBuffer(&theCertPrivateData->mPeerPrivateKey, (unsigned char *)theMsg, theCipherOut->mKey, &theCipherOut->mKeyLen))
	{
		gsDebugFormat(GSIDebugCat_App, GSIDebugType_Misc, GSIDebugLevel_Notice, "Decrypted PeerCipher key block\r\n");
		gsDebugBinary(GSIDebugCat_App, GSIDebugType_Misc, GSIDebugLevel_Notice, (char *)theCipherOut->mKey, (gsi_i32)theCipherOut->mKeyLen);
		RC4Init(&theCipherOut->mRC4, theCipherOut->mKey, (int)theCipherOut->mKeyLen);
		theCipherOut->mInitialized = gsi_true;
		return SCResult_NO_ERROR;
	}
	else
		return SCResult_UNKNOWN_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scPeerCipherEncryptBufferIV(SCPeerCipher * theCipher, gsi_u32 theMessageNum, gsi_u8 * theData, gsi_u32 theLen)
{
	RC4Context rc4;
	MD5_CTX md5;
	char tempHash[16];

	GS_ASSERT(gsi_is_true(theCipher->mInitialized));

	// Construct a new key for this message
	//    - Using the same key for all messages would be extremely unsafe
	MD5Init(&md5);
	MD5Update(&md5, theCipher->mKey, theCipher->mKeyLen);
	MD5Update(&md5, (unsigned char*)&theMessageNum, sizeof(theMessageNum));
	MD5Final((unsigned char *)tempHash, &md5);

	RC4Init(&rc4, (unsigned char *)tempHash, GS_CRYPT_MD5_HASHSIZE);
	RC4Encrypt(&rc4, (const unsigned char*)theData, theData, (int)theLen);
	return SCResult_NO_ERROR; 
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scPeerCipherDecryptBufferIV(SCPeerCipher * theCipher, gsi_u32 theMessageNum, gsi_u8 * theData, gsi_u32 theLen)
{
	return scPeerCipherEncryptBufferIV(theCipher, theMessageNum, theData, theLen);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scPeerCipherEncryptBuffer(SCPeerCipher * theCipher, gsi_u8 * theData, gsi_u32 theLen)
{
	// Encrypt the data using the RC4 context
	GS_ASSERT(gsi_is_true(theCipher->mInitialized));
	RC4Encrypt(&theCipher->mRC4, (const unsigned char*)theData, theData, (int)theLen);
	return SCResult_NO_ERROR; 
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL scPeerCipherDecryptBuffer(SCPeerCipher * theCipher, gsi_u8 * theData, gsi_u32 theLen)
{
	return scPeerCipherEncryptBuffer(theCipher, theData, theLen);
}