]> Dogcows Code - chaz/thecheat/blob - cheat_net.m
The Cheat 1.1.1
[chaz/thecheat] / cheat_net.m
1
2 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 // Project: The Cheat
4 //
5 // File: cheat_net.m
6 // Created: Mon Sep 08 2003
7 //
8 // Copyright: 2003 Chaz McGarvey. All rights reserved.
9 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
11 #include "cheat_net.h"
12
13
14 u_int32_t RandomChecksum()
15 {
16 u_int8_t byte1, byte2, byte3, byte4;
17 char *ptr;
18
19 u_int32_t checksum;
20 long randomValue = random();
21
22 byte1 = randomValue & 0xFF;
23 byte2 = (randomValue >> 8) & 0xFF;
24 byte3 = (randomValue >> 16) & 0xFF;
25 byte4 = CountBits( byte1 ) + CountBits( byte2 ) + CountBits( byte3 );
26
27 ptr = (char *)(&checksum);
28
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) );
33
34 return checksum;
35 }
36
37 char VerifyChecksum( u_int32_t checksum )
38 {
39 u_int8_t byte1, byte2, byte3, byte4;
40 char *ptr = (char *)(&checksum);
41
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) );
46
47 if ( (CountBits( byte1 ) + CountBits( byte2 ) + CountBits( byte3 )) == byte4 )
48 {
49 return 1;
50 }
51
52 NSLog( @"checksum failed" );
53
54 return 0;
55 }
56
57 int CountBits( u_int8_t byte )
58 {
59 int count = 0;
60 int i;
61
62 for ( i = 0; i < 8; i++ )
63 {
64 count += byte & 1;
65
66 byte >>= 1;
67 }
68
69 return count;
70 }
71
72
73 int SendBuffer( int sockfd, char const *buffer, int *length )
74 {
75 int bytesSent = 0;
76 int bytesLeft = *length;
77 int n = 0;
78
79 while( bytesSent < *length )
80 {
81 if ( (n = send( sockfd, buffer+bytesSent, bytesLeft, 0 )) == -1 )
82 {
83 break;
84 }
85
86 bytesSent += n;
87 bytesLeft -= n;
88 }
89
90 *length = bytesSent;
91
92 return (n == -1)? -1:0;
93 }
94
95 int ReadBuffer( int sockfd, char *buffer, int length )
96 {
97 int bytesRead = 0;
98 int bytesLeft = length;
99 int n;
100
101 while( bytesRead < length )
102 {
103 if ( (n = recv( sockfd, buffer+bytesRead, bytesLeft, 0 )) == -1 || n == 0 )
104 {
105 return (bytesRead > 0)? bytesRead:n;
106 }
107
108 bytesRead += n;
109 bytesLeft -= n;
110 }
111
112 return bytesRead;
113 }
This page took 0.036513 seconds and 4 git commands to generate.