I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1 /* Copyright (C) 2007 MySQL AB & Michael Widenius
tax 0:66300c77c6e9 2
tax 0:66300c77c6e9 3 This program is free software; you can redistribute it and/or modify
tax 0:66300c77c6e9 4 it under the terms of the GNU General Public License as published by
tax 0:66300c77c6e9 5 the Free Software Foundation; version 2 of the License.
tax 0:66300c77c6e9 6
tax 0:66300c77c6e9 7 This program is distributed in the hope that it will be useful,
tax 0:66300c77c6e9 8 but WITHOUT ANY WARRANTY; without even the implied warranty of
tax 0:66300c77c6e9 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tax 0:66300c77c6e9 10 GNU General Public License for more details.
tax 0:66300c77c6e9 11
tax 0:66300c77c6e9 12 You should have received a copy of the GNU General Public License
tax 0:66300c77c6e9 13 along with this program; if not, write to the Free Software
tax 0:66300c77c6e9 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
tax 0:66300c77c6e9 15
tax 0:66300c77c6e9 16 #define SCRAMBLE_LENGTH_323 8
tax 0:66300c77c6e9 17
tax 0:66300c77c6e9 18 #include <string.h>
tax 0:66300c77c6e9 19 #include <math.h>
tax 0:66300c77c6e9 20
tax 0:66300c77c6e9 21 typedef unsigned int uint;
tax 0:66300c77c6e9 22 typedef unsigned long ulong;
tax 0:66300c77c6e9 23 typedef unsigned char uchar;
tax 0:66300c77c6e9 24
tax 0:66300c77c6e9 25 struct my_rnd_struct {
tax 0:66300c77c6e9 26 unsigned long seed1,seed2,max_value;
tax 0:66300c77c6e9 27 double max_value_dbl;
tax 0:66300c77c6e9 28 };
tax 0:66300c77c6e9 29
tax 0:66300c77c6e9 30 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2);
tax 0:66300c77c6e9 31 static double my_rnd(struct my_rnd_struct *rand_st);
tax 0:66300c77c6e9 32 static void hash_password(ulong *result, const char *password, uint password_len);
tax 0:66300c77c6e9 33
tax 0:66300c77c6e9 34 /*
tax 0:66300c77c6e9 35 Initialize random generator
tax 0:66300c77c6e9 36
tax 0:66300c77c6e9 37 NOTES
tax 0:66300c77c6e9 38 MySQL's password checks depends on this, so don't do any changes
tax 0:66300c77c6e9 39 that changes the random numbers that are generated!
tax 0:66300c77c6e9 40 */
tax 0:66300c77c6e9 41
tax 0:66300c77c6e9 42 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
tax 0:66300c77c6e9 43 {
tax 0:66300c77c6e9 44 rand_st->max_value= 0x3FFFFFFFL;
tax 0:66300c77c6e9 45 rand_st->max_value_dbl=(double) rand_st->max_value;
tax 0:66300c77c6e9 46 rand_st->seed1=seed1%rand_st->max_value ;
tax 0:66300c77c6e9 47 rand_st->seed2=seed2%rand_st->max_value;
tax 0:66300c77c6e9 48 }
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 /*
tax 0:66300c77c6e9 51 Generate random number.
tax 0:66300c77c6e9 52
tax 0:66300c77c6e9 53 SYNOPSIS
tax 0:66300c77c6e9 54 my_rnd()
tax 0:66300c77c6e9 55 rand_st INOUT Structure used for number generation
tax 0:66300c77c6e9 56
tax 0:66300c77c6e9 57 RETURN VALUE
tax 0:66300c77c6e9 58 generated pseudo random number
tax 0:66300c77c6e9 59 */
tax 0:66300c77c6e9 60
tax 0:66300c77c6e9 61 static double my_rnd(struct my_rnd_struct *rand_st)
tax 0:66300c77c6e9 62 {
tax 0:66300c77c6e9 63 rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
tax 0:66300c77c6e9 64 rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
tax 0:66300c77c6e9 65 return (((double) rand_st->seed1)/rand_st->max_value_dbl);
tax 0:66300c77c6e9 66 }
tax 0:66300c77c6e9 67
tax 0:66300c77c6e9 68 /*
tax 0:66300c77c6e9 69 Generate binary hash from raw text string
tax 0:66300c77c6e9 70 Used for Pre-4.1 password handling
tax 0:66300c77c6e9 71 SYNOPSIS
tax 0:66300c77c6e9 72 hash_password()
tax 0:66300c77c6e9 73 result OUT store hash in this location
tax 0:66300c77c6e9 74 password IN plain text password to build hash
tax 0:66300c77c6e9 75 password_len IN password length (password may be not null-terminated)
tax 0:66300c77c6e9 76 */
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 static void hash_password(ulong *result, const char *password, uint password_len)
tax 0:66300c77c6e9 79 {
tax 0:66300c77c6e9 80 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
tax 0:66300c77c6e9 81 ulong tmp;
tax 0:66300c77c6e9 82 const char *password_end= password + password_len;
tax 0:66300c77c6e9 83 for (; password < password_end; password++)
tax 0:66300c77c6e9 84 {
tax 0:66300c77c6e9 85 if (*password == ' ' || *password == '\t')
tax 0:66300c77c6e9 86 continue; /* skip space in password */
tax 0:66300c77c6e9 87 tmp= (ulong) (uchar) *password;
tax 0:66300c77c6e9 88 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
tax 0:66300c77c6e9 89 nr2+=(nr2 << 8) ^ nr;
tax 0:66300c77c6e9 90 add+=tmp;
tax 0:66300c77c6e9 91 }
tax 0:66300c77c6e9 92 result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
tax 0:66300c77c6e9 93 result[1]=nr2 & (((ulong) 1L << 31) -1L);
tax 0:66300c77c6e9 94 }
tax 0:66300c77c6e9 95
tax 0:66300c77c6e9 96
tax 0:66300c77c6e9 97
tax 0:66300c77c6e9 98 /*
tax 0:66300c77c6e9 99 Scramble string with password.
tax 0:66300c77c6e9 100 Used in pre 4.1 authentication phase.
tax 0:66300c77c6e9 101 SYNOPSIS
tax 0:66300c77c6e9 102 scramble_323()
tax 0:66300c77c6e9 103 to OUT Store scrambled message here. Buffer must be at least
tax 0:66300c77c6e9 104 SCRAMBLE_LENGTH_323+1 bytes long
tax 0:66300c77c6e9 105 message IN Message to scramble. Message must be at least
tax 0:66300c77c6e9 106 SRAMBLE_LENGTH_323 bytes long.
tax 0:66300c77c6e9 107 password IN Password to use while scrambling
tax 0:66300c77c6e9 108 */
tax 0:66300c77c6e9 109
tax 0:66300c77c6e9 110 void scramble_323(char *to, const char *message, const char *password)
tax 0:66300c77c6e9 111 {
tax 0:66300c77c6e9 112 struct my_rnd_struct rand_st;
tax 0:66300c77c6e9 113 ulong hash_pass[2], hash_message[2];
tax 0:66300c77c6e9 114
tax 0:66300c77c6e9 115 if (password && password[0])
tax 0:66300c77c6e9 116 {
tax 0:66300c77c6e9 117 char extra, *to_start=to;
tax 0:66300c77c6e9 118 const char *message_end= message + SCRAMBLE_LENGTH_323;
tax 0:66300c77c6e9 119 hash_password(hash_pass,password, (uint) strlen(password));
tax 0:66300c77c6e9 120 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
tax 0:66300c77c6e9 121 my_rnd_init(&rand_st,hash_pass[0] ^ hash_message[0],
tax 0:66300c77c6e9 122 hash_pass[1] ^ hash_message[1]);
tax 0:66300c77c6e9 123 for (; message < message_end; message++)
tax 0:66300c77c6e9 124 *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
tax 0:66300c77c6e9 125 extra=(char) (floor(my_rnd(&rand_st)*31));
tax 0:66300c77c6e9 126 while (to_start != to)
tax 0:66300c77c6e9 127 *(to_start++)^=extra;
tax 0:66300c77c6e9 128 }
tax 0:66300c77c6e9 129 *to= 0;
tax 0:66300c77c6e9 130 }
tax 0:66300c77c6e9 131