]>
Dogcows Code - chaz/thecheat/blob - cheat_net.m
be27b7c5315c812b4eeeeb30cde499ac85fc90cd
2 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 // Created: Mon Sep 08 2003
8 // Copyright: 2003 Chaz McGarvey. All rights reserved.
9 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #include "cheat_net.h"
14 u_int32_t
RandomChecksum()
16 u_int8_t byte1
, byte2
, byte3
, byte4
;
20 long randomValue
= random();
22 byte1
= randomValue
& 0xFF;
23 byte2
= (randomValue
>> 8) & 0xFF;
24 byte3
= (randomValue
>> 16) & 0xFF;
25 byte4
= CountBits( byte1
) + CountBits( byte2
) + CountBits( byte3
);
27 ptr
= (char *)(&checksum
);
29 COPY_TO_BUFFER( ptr
, &byte1
, sizeof(byte1
) );
30 COPY_TO_BUFFER( ptr
, &byte2
, sizeof(byte2
) );
31 COPY_TO_BUFFER( ptr
, &byte3
, sizeof(byte3
) );
32 COPY_TO_BUFFER( ptr
, &byte4
, sizeof(byte4
) );
37 char VerifyChecksum( u_int32_t checksum
)
39 u_int8_t byte1
, byte2
, byte3
, byte4
;
40 char *ptr
= (char *)(&checksum
);
42 COPY_FROM_BUFFER( &byte1
, ptr
, sizeof(byte1
) );
43 COPY_FROM_BUFFER( &byte2
, ptr
, sizeof(byte2
) );
44 COPY_FROM_BUFFER( &byte3
, ptr
, sizeof(byte3
) );
45 COPY_FROM_BUFFER( &byte4
, ptr
, sizeof(byte4
) );
47 if ( (CountBits( byte1
) + CountBits( byte2
) + CountBits( byte3
)) == byte4
)
52 NSLog( @
"checksum failed" );
57 int CountBits( u_int8_t byte
)
62 for ( i
= 0; i
< 8; i
++ )
73 int SendBuffer( int sockfd
, char const *buffer
, int *length
)
76 int bytesLeft
= *length
;
79 while( bytesSent
< *length
)
81 if ( (n
= send( sockfd
, buffer
+bytesSent
, bytesLeft
, 0 )) == -1 )
92 return (n
== -1)?
-1:0;
95 int ReadBuffer( int sockfd
, char *buffer
, int length
)
98 int bytesLeft
= length
;
101 while( bytesRead
< length
)
103 if ( (n
= recv( sockfd
, buffer
+bytesRead
, bytesLeft
, 0 )) == -1 || n
== 0 )
105 return (bytesRead
> 0)? bytesRead
:n
;
This page took 0.038045 seconds and 4 git commands to generate.