Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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