00001 #ifndef __NITRO_STRING_UTILITIES_CPP__
00002 #define __NITRO_STRING_UTILITIES_CPP__
00003
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007
00008 #include <utilities/string_utilities.h>
00009 #include <utilities/testing_utilities.h>
00010
00011 namespace nitro
00012 {
00013 void FSPath::AddEndSlash( const char * OldPath , char * NewPath )
00014 {
00015 try
00016 {
00017 strcpy( NewPath , OldPath );
00018
00019 if( NewPath[ strlen( NewPath ) - 1 ] != '/' && NewPath[ strlen( NewPath ) - 1 ] != '\\' )
00020 {
00021 strcat( NewPath , "/" );
00022 }
00023 }
00024 catch( nitro::exception e )
00025 {
00026 throw( nitro::exception( std::string( "FSPath::AddEndSlash( const char * OldPath , char * NewPath )::" ) + e.what() , e.code() ) );
00027 }
00028 catch( ... )
00029 {
00030 throw( nitro::exception( std::string( "FSPath::AddEndSlash( const char * OldPath , char * NewPath )::An error occured" ) , 1 ) );
00031 }
00032 }
00033
00034 void FSPath::DeleteEndSlash( const char * OldPath , char * NewPath )
00035 {
00036 try
00037 {
00038 if( OldPath[ strlen( OldPath ) - 1 ] == '/' || OldPath[ strlen( OldPath ) - 1 ] == '\\' )
00039 {
00040 memset( NewPath , 0 , strlen( OldPath ) );
00041 strncpy( NewPath , OldPath , strlen( OldPath ) - 1 );
00042 }
00043 else
00044 {
00045 strcpy( NewPath , OldPath );
00046 }
00047 }
00048 catch( nitro::exception e )
00049 {
00050 throw( nitro::exception( std::string( "FSPath::DeleteEndSlash( const char * OldPath , char * NewPath )::" ) + e.what() , e.code() ) );
00051 }
00052 catch( ... )
00053 {
00054 throw( nitro::exception( std::string( "FSPath::DeleteEndSlash( const char * OldPath , char * NewPath )::An error occured" ) , 1 ) );
00055 }
00056 }
00057
00058 char * FSPath::ExtractFilePath( const char * FilePath , char * ClearedFilePath )
00059 {
00060 try
00061 {
00062 int i( ( int )strlen( FilePath ) - 1 );
00063 ClearedFilePath[ 0 ] = '\0';
00064 for( ; i >= 0 ; i-- )
00065 {
00066 if( FilePath[ i ] == '/' || FilePath[ i ] == '\\' )
00067 {
00068 goto end_loop;
00069 }
00070 }
00071 end_loop:;
00072 if( i >= 0 )
00073 {
00074 strncpy( ClearedFilePath , FilePath , i );
00075 ClearedFilePath[ i ] = '\0';
00076 }
00077 else
00078 {
00079 ClearedFilePath[ 0 ] = '\0';
00080 }
00081 return( ClearedFilePath );
00082 }
00083 catch( nitro::exception e )
00084 {
00085 throw( nitro::exception( std::string( "FSPath::ExtractFilePath( const char * FilePath , char * ClearedFilePath )::" ) + e.what() , e.code() ) );
00086 }
00087 catch( ... )
00088 {
00089 throw( nitro::exception( std::string( "FSPath::ExtractFilePath( const char * FilePath , char * ClearedFilePath )::An error occured" ) , 1 ) );
00090 }
00091 }
00092
00093 char * FSPath::ExtractFileName( const char * FilePath , char * FileName )
00094 {
00095 try
00096 {
00097 int i( ( int )strlen( FilePath ) - 1 );
00098 FileName[ 0 ] = '\0';
00099
00100 for( ; i >= 0 ; i-- )
00101 {
00102 if( FilePath[ i ] == '/' || FilePath[ i ] == '\\' )
00103 {
00104 goto end_loop;
00105 }
00106 }
00107
00108
00109 strcpy( FileName , FilePath );
00110 return( FileName );
00111
00112 end_loop:;
00113
00114
00115 strncpy( FileName , & FilePath[ i + 1 ] , strlen( FilePath ) - i );
00116 return( FileName );
00117 }
00118 catch( nitro::exception e )
00119 {
00120 throw( nitro::exception( std::string( "FSPath::ExtractFileName( const char * FilePath , char * FileName )::" ) + e.what() , e.code() ) );
00121 }
00122 catch( ... )
00123 {
00124 throw( nitro::exception( std::string( "FSPath::ExtractFileName( const char * FilePath , char * FileName )::An error occured" ) , 1 ) );
00125 }
00126 }
00127
00128 void FSPath::ExtractModuleName( const char * file_name , char * module_name )
00129 {
00130 try
00131 {
00132 int i( ( int )strlen( file_name ) - 1 );
00133 module_name[ 0 ] = '\0';
00134 for( ; i >= 0 ; i-- )
00135 {
00136 if( file_name[ i ] == '.' )
00137 {
00138 goto end_loop;
00139 }
00140 }
00141 end_loop:;
00142 strncpy( module_name , file_name , i );
00143 module_name[ i ] = '\0';
00144 }
00145 catch( nitro::exception e )
00146 {
00147 throw( nitro::exception( std::string( "FSPath::ExtractModuleName( const char * file_name , char * module_name )::" ) + e.what() , e.code() ) );
00148 }
00149 catch( ... )
00150 {
00151 throw( nitro::exception( std::string( "FSPath::ExtractModuleName( const char * file_name , char * module_name )::An error occured" ) , 1 ) );
00152 }
00153 }
00154
00155 char * FSPath::ExtractFileExtension( const char * file_name , char * file_extension )
00156 {
00157 try
00158 {
00159 int i( ( int )strlen( file_name ) - 1 );
00160 file_extension[ 0 ] = '\0';
00161 for( ; i >= 0 ; i-- )
00162 {
00163 if( file_name[ i ] == '.' )
00164 {
00165 goto end_loop;
00166 }
00167 }
00168 end_loop:;
00169 strncpy( file_extension , &file_name[ i + 1 ] , strlen( file_name ) - i );
00170 return( file_extension );
00171 }
00172 catch( nitro::exception e )
00173 {
00174 throw( nitro::exception( std::string( "FSPath::ExtractFileExtension( const char * file_name , char * file_extension )::" ) + e.what() , e.code() ) );
00175 }
00176 catch( ... )
00177 {
00178 throw( nitro::exception( std::string( "FSPath::ExtractFileExtension( const char * file_name , char * file_extension )::An error occured" ) , 1 ) );
00179 }
00180 }
00181
00182 const char * SUtils::TrimRight( const char * Str , char * OutStr , char Ch )
00183 {
00184 try
00185 {
00186 memset( OutStr , 0 , strlen( Str ) + 1 );
00187
00188 int i( ( int )strlen( Str ) - 1 );
00189
00190 for( ; i >= 0 && Str[ i ] == Ch ; i-- );
00191
00192 strncpy( OutStr , Str , i + 1 );
00193
00194 return( OutStr );
00195 }
00196 catch( nitro::exception e )
00197 {
00198 throw( nitro::exception( std::string( "SUtils::TrimRight( const char * Str , char * OutStr , char Ch )::" ) + e.what() , e.code() ) );
00199 }
00200 catch( ... )
00201 {
00202 throw( nitro::exception( std::string( "SUtils::TrimRight( const char * Str , char * OutStr , char Ch )::An error occured" ) , 1 ) );
00203 }
00204 }
00205
00206 const char * SUtils::TrimLeft( const char * Str , char * OutStr , char Ch )
00207 {
00208 try
00209 {
00210 memset( OutStr , 0 , strlen( Str ) + 1 );
00211
00212 std::size_t i( 0 );
00213
00214 for( ; i < strlen( Str ) && Str[ i ] == Ch ; i++ );
00215
00216 strncpy( OutStr , Str + i , strlen( Str ) - i );
00217
00218 return( OutStr );
00219 }
00220 catch( nitro::exception e )
00221 {
00222 throw( nitro::exception( std::string( "SUtils::TrimLeft( const char * Str , char * OutStr , char Ch )::" ) + e.what() , e.code() ) );
00223 }
00224 catch( ... )
00225 {
00226 throw( nitro::exception( std::string( "SUtils::TrimLeft( const char * Str , char * OutStr , char Ch )::An error occured" ) , 1 ) );
00227 }
00228 }
00229
00230 void Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )
00231 {
00232 try
00233 {
00234 for( std::size_t i( 0 ) ; i < BufferSize ; i++ )
00235 {
00236 for( std::size_t j( 0 ) ; j < strlen( CodeWord ) ; j++ )
00237 {
00238 OutputBuffer[ i ] ^= CodeWord[ j ];
00239 }
00240
00241 OutputBuffer[ i ] ^= CodeWord[ i % strlen( CodeWord ) ];
00242 }
00243 }
00244 catch( nitro::exception e )
00245 {
00246 throw( nitro::exception( std::string( "Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )::" ) + e.what() , e.code() ) );
00247 }
00248 catch( ... )
00249 {
00250 throw( nitro::exception( std::string( "Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )::An error occured" ) , 1 ) );
00251 }
00252 }
00253
00254 void Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )
00255 {
00256 try
00257 {
00258 for( std::size_t i( 0 ) ; i < BufferSize ; i++ )
00259 {
00260 InputBuffer[ i ] ^= CodeWord[ i % strlen( CodeWord ) ];
00261
00262 for( int j( ( int )strlen( CodeWord ) - 1 ) ; j >= 0 ; j-- )
00263 {
00264 InputBuffer[ i ] ^= CodeWord[ j ];
00265 }
00266 }
00267 }
00268 catch( nitro::exception e )
00269 {
00270 throw( nitro::exception( std::string( "Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )::" ) + e.what() , e.code() ) );
00271 }
00272 catch( ... )
00273 {
00274 throw( nitro::exception( std::string( "Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )::An error occured" ) , 1 ) );
00275 }
00276 }
00277
00278 void Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )
00279 {
00280 try
00281 {
00282 std::string EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
00283 std::string Sout = "";
00284 unsigned char a , b , c , i;
00285
00286 for( std::size_t k( 0 ) ; k < count ; k++ )
00287 {
00288 if( count - k == 1 )
00289 {
00290 a = bf[ k ];
00291 b = 0;
00292 i = a >> 2;
00293 Sout += EncodeTable.substr( i , 1 );
00294 i = ( ( a & 3 ) << 4 ) | ( b >> 4 );
00295 Sout += EncodeTable.substr( i , 1 );
00296 Sout += EncodeTable.substr( 64 , 1 );
00297 Sout += EncodeTable.substr( 64 , 1 );
00298 }
00299 else if( count - k == 2 )
00300 {
00301 a = bf[ k ];
00302 b = bf[ ++k ];
00303 c = 0;
00304 i = a >> 2;
00305 Sout += EncodeTable.substr( i , 1 );
00306 i = ( ( a & 3 ) << 4 ) | ( b >> 4 );
00307 Sout += EncodeTable.substr( i , 1 );
00308 i = ( ( b & 15 ) << 2 ) | ( c >> 6 );
00309 Sout += EncodeTable.substr( i , 1 );
00310 Sout += EncodeTable.substr( 64 , 1 );
00311 }
00312 else
00313 {
00314 a = bf[ k ];
00315 b = bf[ ++k ];
00316 c = bf[ ++k ];
00317
00318 i = a >> 2;
00319 Sout += EncodeTable.substr( i , 1 );
00320 i = ( ( a & 3 ) << 4 ) | ( b >> 4 );
00321 Sout += EncodeTable.substr( i , 1 );
00322 i = ( ( b & 15 ) << 2 ) | ( c >> 6 );
00323 Sout += EncodeTable.substr( i , 1 );
00324 i = c & 63;
00325 Sout += EncodeTable.substr( i , 1 );
00326 }
00327 }
00328
00329 strcpy( outbf , Sout.c_str() );
00330 }
00331 catch( nitro::exception e )
00332 {
00333 throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )::" ) + e.what() , e.code() ) );
00334 }
00335 catch( ... )
00336 {
00337 throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )::An error occured while encoding string to base64" ) , 1 ) );
00338 }
00339 }
00340
00341 void Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )
00342 {
00343 try
00344 {
00345 std::string EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
00346
00347 char a , b , c , d;
00348 char x , y , z;
00349 int t = 0;
00350
00351 if( std::string( InBuf ) == "" )
00352 {
00353 Count = 0;
00354 return;
00355 }
00356
00357 if( strlen( InBuf ) % 4 != 0 )
00358 {
00359 throw( std::string( "Illegal input string" ) );
00360 }
00361
00362 for( std::size_t i( 0 ) ; i < strlen( InBuf ) / 4 ; i++ )
00363 {
00364 std::size_t r;
00365
00366 r = EncodeTable.find( InBuf[ 4 * i + 0 ] , 0 );
00367 a = ( r == std::string::npos ? -1 : ( char ) r );
00368
00369 r = EncodeTable.find( InBuf[ 4 * i + 1 ] , 0 );
00370 b = ( r == std::string::npos ? -1 : ( char ) r );
00371
00372 r = EncodeTable.find( InBuf[ 4 * i + 2 ] , 0 );
00373 c = ( r == std::string::npos ? -1 : ( char ) r );
00374
00375 r = EncodeTable.find( InBuf[ 4 * i + 3 ] , 0 );
00376 d = ( r == std::string::npos ? -1 : ( char ) r );
00377
00378 if( ( a == -1 ) || ( b == -1 ) || ( c == -1 ) || ( d == -1 ) || ( a == 64 ) || ( b == 64 ) )
00379 {
00380 throw( std::string( "An error occured while decoding" ) );
00381 }
00382 if( c == 64 )
00383 {
00384 x = ( a << 2 ) | ( b >> 4 );
00385 OutBuf[ t++ ] = x;
00386 }
00387 else if( d == 64 )
00388 {
00389 x = ( a << 2 ) | ( b >> 4 );
00390 y = ( b << 4 ) | ( c >> 2 );
00391 OutBuf[ t++ ] = x;
00392 OutBuf[ t++ ] = y;
00393 }
00394 else
00395 {
00396 x = ( a << 2 ) | ( b >> 4 );
00397 y = ( b << 4 ) | ( c >> 2 );
00398 z = ( c << 6 ) | d;
00399 OutBuf[ t++ ] = x;
00400 OutBuf[ t++ ] = y;
00401 OutBuf[ t++ ] = z;
00402 }
00403 }
00404
00405 Count = t;
00406 }
00407 catch( nitro::exception e )
00408 {
00409 throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )::" ) + e.what() , e.code() ) );
00410 }
00411 catch( ... )
00412 {
00413 throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )::An error occured while encoding string to base64" ) , 1 ) );
00414 }
00415 }
00416
00417 typedef unsigned char UC;
00418 typedef unsigned short UI;
00419 typedef unsigned long UINT4;
00420 typedef unsigned char * POINTER;
00421
00422 typedef struct
00423 {
00424 UINT4 state[ 4 ];
00425 UINT4 count[ 2 ];
00426 UC buffer[ 64 ];
00427 } MD5_CTX;
00428
00429
00430 #define S11 7
00431 #define S12 12
00432 #define S13 17
00433 #define S14 22
00434 #define S21 5
00435 #define S22 9
00436 #define S23 14
00437 #define S24 20
00438 #define S31 4
00439 #define S32 11
00440 #define S33 16
00441 #define S34 23
00442 #define S41 6
00443 #define S42 10
00444 #define S43 15
00445 #define S44 21
00446
00447 static UC PADDING[ 64 ] = {0x80 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00448 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00449 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00450 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00451 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00452 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00453 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
00454 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
00455
00456 #define F( x , y , z ) ( ( ( x ) & ( y ) ) | ( ( ~x ) & ( z ) ) )
00457 #define G( x , y , z ) ( ( ( x ) & ( z ) ) | ( ( y ) & ( ~z ) ) )
00458 #define H( x , y , z ) ( ( x ) ^ ( y ) ^ ( z ) )
00459 #define I( x , y , z ) ( ( y ) ^ ( ( x ) | ( ~z ) ) )
00460
00461 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
00462
00463
00464 #define FF(a, b, c, d, x, s, ac) {\
00465 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);\
00466 (a) = ROTATE_LEFT ((a), (s));\
00467 (a) += (b);}
00468 #define GG(a, b, c, d, x, s, ac) {\
00469 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac);\
00470 (a) = ROTATE_LEFT ((a), (s));\
00471 (a) += (b);}
00472 #define HH(a, b, c, d, x, s, ac) {\
00473 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac);\
00474 (a) = ROTATE_LEFT ((a), (s));\
00475 (a) += (b);}
00476 #define II(a, b, c, d, x, s, ac) {\
00477 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac);\
00478 (a) = ROTATE_LEFT ((a), (s));\
00479 (a) += (b);}
00480
00481
00482
00483 void Encode( UC * output , UINT4 * input , std::size_t len )
00484 {
00485 try
00486 {
00487 UI i, j;
00488 for (i = 0, j = 0; j < ( UI )len; i++, j += 4)
00489 {
00490 output[ j ] = (UC)(input[i] & 0xff);
00491 output[j+1] = (UC)((input[i] >> 8) & 0xff);
00492 output[j+2] = (UC)((input[i] >> 16) & 0xff);
00493 output[j+3] = (UC)((input[i] >> 24) & 0xff);
00494 }
00495 }
00496 catch( nitro::exception e )
00497 {
00498 throw( nitro::exception( std::string( "Encode( UC * output , UINT4 * input , UI len )::" ) + e.what() , e.code() ) );
00499 }
00500 catch( ... )
00501 {
00502 throw( nitro::exception( std::string( "Encode( UC * output , UINT4 * input , UI len )::An error occured while encoding string to base64" ) , 1 ) );
00503 }
00504 }
00505
00506
00507
00508 void Decode( UINT4 * output , UC * input , UI len )
00509 {
00510 try
00511 {
00512 UI i, j;
00513 for (i = 0, j = 0; j < len; i++, j += 4)
00514 output[i] = ((UINT4)input[ j ]) |
00515 (((UINT4)input[j+1]) << 8) |
00516 (((UINT4)input[j+2]) << 16) |
00517 (((UINT4)input[j+3]) << 24);
00518 }
00519 catch( nitro::exception e )
00520 {
00521 throw( nitro::exception( std::string( "Decode( UINT4 * output , UC * input , UI len )::" ) + e.what() , e.code() ) );
00522 }
00523 catch( ... )
00524 {
00525 throw( nitro::exception( std::string( "Decode( UINT4 * output , UC * input , UI len )::An error occured while encoding string to base64" ) , 1 ) );
00526 }
00527 }
00528
00529
00530 void MDTransformF( UINT4 s[ 4 ] , UINT4 x[ 16 ] )
00531 {
00532 FF(s[0], s[1], s[2], s[3], x[ 0], S11, 0xd76aa478);
00533 FF(s[3], s[0], s[1], s[2], x[ 1], S12, 0xe8c7b756);
00534 FF(s[2], s[3], s[0], s[1], x[ 2], S13, 0x242070db);
00535 FF(s[1], s[2], s[3], s[0], x[ 3], S14, 0xc1bdceee);
00536 FF(s[0], s[1], s[2], s[3], x[ 4], S11, 0xf57c0faf);
00537 FF(s[3], s[0], s[1], s[2], x[ 5], S12, 0x4787c62a);
00538 FF(s[2], s[3], s[0], s[1], x[ 6], S13, 0xa8304613);
00539 FF(s[1], s[2], s[3], s[0], x[ 7], S14, 0xfd469501);
00540 FF(s[0], s[1], s[2], s[3], x[ 8], S11, 0x698098d8);
00541 FF(s[3], s[0], s[1], s[2], x[ 9], S12, 0x8b44f7af);
00542 FF(s[2], s[3], s[0], s[1], x[10], S13, 0xffff5bb1);
00543 FF(s[1], s[2], s[3], s[0], x[11], S14, 0x895cd7be);
00544 FF(s[0], s[1], s[2], s[3], x[12], S11, 0x6b901122);
00545 FF(s[3], s[0], s[1], s[2], x[13], S12, 0xfd987193);
00546 FF(s[2], s[3], s[0], s[1], x[14], S13, 0xa679438e);
00547 FF(s[1], s[2], s[3], s[0], x[15], S14, 0x49b40821);
00548 }
00549
00550
00551 void MDTransformG( UINT4 s[ 4 ] , UINT4 x[ 16 ] )
00552 {
00553 GG(s[0], s[1], s[2], s[3], x[ 1], S21, 0xf61e2562);
00554 GG(s[3], s[0], s[1], s[2], x[ 6], S22, 0xc040b340);
00555 GG(s[2], s[3], s[0], s[1], x[11], S23, 0x265e5a51);
00556 GG(s[1], s[2], s[3], s[0], x[ 0], S24, 0xe9b6c7aa);
00557 GG(s[0], s[1], s[2], s[3], x[ 5], S21, 0xd62f105d);
00558 GG(s[3], s[0], s[1], s[2], x[10], S22, 0x02441453);
00559 GG(s[2], s[3], s[0], s[1], x[15], S23, 0xd8a1e681);
00560 GG(s[1], s[2], s[3], s[0], x[ 4], S24, 0xe7d3fbc8);
00561 GG(s[0], s[1], s[2], s[3], x[ 9], S21, 0x21e1cde6);
00562 GG(s[3], s[0], s[1], s[2], x[14], S22, 0xc33707d6);
00563 GG(s[2], s[3], s[0], s[1], x[ 3], S23, 0xf4d50d87);
00564 GG(s[1], s[2], s[3], s[0], x[ 8], S24, 0x455a14ed);
00565 GG(s[0], s[1], s[2], s[3], x[13], S21, 0xa9e3e905);
00566 GG(s[3], s[0], s[1], s[2], x[ 2], S22, 0xfcefa3f8);
00567 GG(s[2], s[3], s[0], s[1], x[ 7], S23, 0x676f02d9);
00568 GG(s[1], s[2], s[3], s[0], x[12], S24, 0x8d2a4c8a);
00569 }
00570
00571
00572 void MDTransformH( UINT4 s[ 4 ] , UINT4 x[ 16 ] )
00573 {
00574 HH(s[0], s[1], s[2], s[3], x[ 5], S31, 0xfffa3942);
00575 HH(s[3], s[0], s[1], s[2], x[ 8], S32, 0x8771f681);
00576 HH(s[2], s[3], s[0], s[1], x[11], S33, 0x6d9d6122);
00577 HH(s[1], s[2], s[3], s[0], x[14], S34, 0xfde5380c);
00578 HH(s[0], s[1], s[2], s[3], x[ 1], S31, 0xa4beea44);
00579 HH(s[3], s[0], s[1], s[2], x[ 4], S32, 0x4bdecfa9);
00580 HH(s[2], s[3], s[0], s[1], x[ 7], S33, 0xf6bb4b60);
00581 HH(s[1], s[2], s[3], s[0], x[10], S34, 0xbebfbc70);
00582 HH(s[0], s[1], s[2], s[3], x[13], S31, 0x289b7ec6);
00583 HH(s[3], s[0], s[1], s[2], x[ 0], S32, 0xeaa127fa);
00584 HH(s[2], s[3], s[0], s[1], x[ 3], S33, 0xd4ef3085);
00585 HH(s[1], s[2], s[3], s[0], x[ 6], S34, 0x04881d05);
00586 HH(s[0], s[1], s[2], s[3], x[ 9], S31, 0xd9d4d039);
00587 HH(s[3], s[0], s[1], s[2], x[12], S32, 0xe6db99e5);
00588 HH(s[2], s[3], s[0], s[1], x[15], S33, 0x1fa27cf8);
00589 HH(s[1], s[2], s[3], s[0], x[ 2], S34, 0xc4ac5665);
00590 }
00591
00592
00593 void MDTransformI( UINT4 s[ 4 ] , UINT4 x[ 16 ] )
00594 {
00595 II(s[0], s[1], s[2], s[3], x[ 0], S41, 0xf4292244);
00596 II(s[3], s[0], s[1], s[2], x[ 7], S42, 0x432aff97);
00597 II(s[2], s[3], s[0], s[1], x[14], S43, 0xab9423a7);
00598 II(s[1], s[2], s[3], s[0], x[ 5], S44, 0xfc93a039);
00599 II(s[0], s[1], s[2], s[3], x[12], S41, 0x655b59c3);
00600 II(s[3], s[0], s[1], s[2], x[ 3], S42, 0x8f0ccc92);
00601 II(s[2], s[3], s[0], s[1], x[10], S43, 0xffeff47d);
00602 II(s[1], s[2], s[3], s[0], x[ 1], S44, 0x85845dd1);
00603 II(s[0], s[1], s[2], s[3], x[ 8], S41, 0x6fa87e4f);
00604 II(s[3], s[0], s[1], s[2], x[15], S42, 0xfe2ce6e0);
00605 II(s[2], s[3], s[0], s[1], x[ 6], S43, 0xa3014314);
00606 II(s[1], s[2], s[3], s[0], x[13], S44, 0x4e0811a1);
00607 II(s[0], s[1], s[2], s[3], x[ 4], S41, 0xf7537e82);
00608 II(s[3], s[0], s[1], s[2], x[11], S42, 0xbd3af235);
00609 II(s[2], s[3], s[0], s[1], x[ 2], S43, 0x2ad7d2bb);
00610 II(s[1], s[2], s[3], s[0], x[ 9], S44, 0xeb86d391);
00611 }
00612
00613
00614
00615 void MDTransform( UINT4 state[ 4 ] , UC block[ 64 ] )
00616 {
00617 try
00618 {
00619 UINT4 a = state[ 0 ] , b = state[ 1 ] , c = state[ 2 ] , d = state[ 3 ] , x[ 16 ];
00620
00621 Decode( x , block , 64 );
00622 MDTransformF( state , x );
00623 MDTransformG( state , x );
00624 MDTransformH( state , x );
00625 MDTransformI( state , x );
00626
00627 state[ 0 ] += a;
00628 state[ 1 ] += b;
00629 state[ 2 ] += c;
00630 state[ 3 ] += d;
00631
00632 memset( ( POINTER ) x , 0 , sizeof( x ) );
00633 }
00634 catch( nitro::exception e )
00635 {
00636 throw( nitro::exception( std::string( "MDTransform( UINT4 state[ 4 ] , UC block[ 64 ] )::" ) + e.what() , e.code() ) );
00637 }
00638 catch( ... )
00639 {
00640 throw( nitro::exception( std::string( "MDTransform( UINT4 state[ 4 ] , UC block[ 64 ] )::An error occured while encoding string to base64" ) , 1 ) );
00641 }
00642 }
00643
00644
00645
00646 void MDUpdate( MD5_CTX * context , UC * input , std::size_t inputLen )
00647 {
00648 try
00649 {
00650 UI index, partLen;
00651 std::size_t i;
00652
00653 index = (UI)((context->count[0] >> 3) & 0x3F);
00654
00655 if ((context->count[0] += ((UINT4)inputLen << 3))
00656 < ((UINT4)inputLen << 3))
00657 context->count[1]++;
00658 context->count[1] += ((UINT4)inputLen >> 29);
00659 partLen = 64 - index;
00660
00661 if( ( UI )inputLen >= partLen )
00662 {
00663 memcpy( ( POINTER )&context->buffer[ index ] , ( POINTER )input , partLen );
00664 MDTransform (context->state, context->buffer);
00665 for( i = partLen ; i + 63 < inputLen ; i += 64)
00666 MDTransform(context->state, &input[i]);
00667 index = 0;
00668 }
00669 else
00670 i = 0;
00671
00672 memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i);
00673 }
00674 catch( nitro::exception e )
00675 {
00676 throw( nitro::exception( std::string( "MDUpdate( MD5_CTX * context , UC * input , UI inputLen )::" ) + e.what() , e.code() ) );
00677 }
00678 catch( ... )
00679 {
00680 throw( nitro::exception( std::string( "MDUpdate( MD5_CTX * context , UC * input , UI inputLen ))::An error occured while encoding string to base64" ) , 1 ) );
00681 }
00682 }
00683
00684
00685
00686
00687 void MDFinal( UC digest[ 16 ] , MD5_CTX * context )
00688 {
00689 try
00690 {
00691 UC bits[ 8 ];
00692 UI index , padLen;
00693
00694 Encode( bits , context->count , 8 );
00695
00696 index = ( UI )( ( context->count[ 0 ] >> 3 ) & 0x3f );
00697 padLen = ( index < 56 ) ? ( 56 - index ) : ( 120 - index );
00698 MDUpdate( context , PADDING , padLen );
00699
00700 MDUpdate( context , bits , 8 );
00701
00702 Encode( digest , context->state , 16 );
00703
00704 memset( ( POINTER )context , 0 , sizeof( * context ) );
00705 }
00706 catch( nitro::exception e )
00707 {
00708 throw( nitro::exception( std::string( "MDFinal( UC digest[ 16 ] , MD5_CTX * context )::" ) + e.what() , e.code() ) );
00709 }
00710 catch( ... )
00711 {
00712 throw( nitro::exception( std::string( "MDFinal( UC digest[ 16 ] , MD5_CTX * context )::An error occured while encoding string to base64" ) , 1 ) );
00713 }
00714 }
00715
00716 void MDInit( MD5_CTX * Context )
00717 {
00718 Context->count[ 0 ] = Context->count[ 1 ] = 0;
00719
00720 Context->state[ 0 ] = 0x67452301;
00721 Context->state[ 1 ] = 0xefcdab89;
00722 Context->state[ 2 ] = 0x98badcfe;
00723 Context->state[ 3 ] = 0x10325476;
00724 }
00725
00726 void Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )
00727 {
00728 try
00729 {
00730 MD5_CTX Context;
00731 UC Digest[ 16 ];
00732
00733 MDInit( & Context );
00734 MDUpdate( & Context , ( UC * )Data , DataLength );
00735 MDFinal( Digest , & Context );
00736
00737
00738 memset( Hash , 0 , 32 );
00739
00740 for( std::size_t i( 0 ) ; i < 16 ; i++ )
00741 {
00742 sprintf( & Hash[ i * 2 ] , "%02x" , Digest[ i ] );
00743 }
00744
00745 Hash[ 32 ] = '\0';
00746 }
00747 catch( nitro::exception e )
00748 {
00749 throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )::" ) + e.what() , e.code() ) );
00750 }
00751 catch( ... )
00752 {
00753 throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )::An error occured while encoding string to base64" ) , 1 ) );
00754 }
00755 }
00756
00757 bool Parsers::CommandLineParameterExists( const char * CommandLine , const char * ParameterName )
00758 {
00759 try
00760 {
00761 for( std::size_t i( 0 ) ; i < strlen( CommandLine ) ; i++ )
00762 {
00763
00764 if( Parsers::BeginsWith( CommandLine + i , ParameterName ) &&
00765 ( CommandLine[ i + strlen( ParameterName ) ] == ' ' || CommandLine[ i + strlen( ParameterName ) ] == '=' ||
00766 CommandLine[ i + strlen( ParameterName ) ] == '\0' ) )
00767 {
00768 if( i == 0 )
00769 {
00770
00771
00772 return( true );
00773 }
00774 else
00775 {
00776
00777
00778 if( CommandLine[ i - 1 ] == ' ' )
00779 {
00780 return( true );
00781 }
00782 }
00783 }
00784 }
00785
00786 return( false );
00787 }
00788 catch( nitro::exception e )
00789 {
00790 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const char * CommandLine , const char * ParameterName )::" ) + e.what() , e.code() ) );
00791 }
00792 catch( ... )
00793 {
00794 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const char * CommandLine , const char * ParameterName )::An error occured" ) , 1 ) );
00795 }
00796 }
00797
00798 bool Parsers::CommandLineParameterExists( int argc , const char * * argv , const char * ParameterName )
00799 {
00800 try
00801 {
00802 std::string tmpParamName( ParameterName );
00803 for( std::size_t i( 0 ) ; i < ( std::size_t )argc ; i++ )
00804 {
00805 if( tmpParamName == argv[ i ] )
00806 {
00807 return( true );
00808 }
00809 }
00810
00811 return( false );
00812 }
00813 catch( nitro::exception e )
00814 {
00815 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::" ) + e.what() , e.code() ) );
00816 }
00817 catch( ... )
00818 {
00819 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::An error occured" ) , 1 ) );
00820 }
00821 }
00822
00823 void Parsers::GetCommandLineParameter( const char * CommandLine , const char * ParameterName , const char * DefaultValue , char * ParameterValue )
00824 {
00825 try
00826 {
00827 ParameterValue[ 0 ] = '\0';
00828
00829 if( CommandLineParameterExists( CommandLine , ParameterName ) == false )
00830 {
00831 strcpy( ParameterValue , DefaultValue );
00832 return;
00833 }
00834
00835 bool SearchParameter( true );
00836 bool SpaceSeparatedValue( false );
00837 std::size_t StartValueCursor( 0 );
00838
00839 for( std::size_t i( 0 ) ; i < strlen( CommandLine ) ; i++ )
00840 {
00841 if( SearchParameter )
00842 {
00843
00844 if( Parsers::BeginsWith( CommandLine + i , ParameterName ) && ( CommandLine[ i + strlen( ParameterName ) ] == ' ' || CommandLine[ i + strlen( ParameterName ) ] == '=' ) )
00845 {
00846
00847
00848 SearchParameter = false;
00849 i += strlen( ParameterName );
00850 }
00851 }
00852 else
00853 {
00854
00855 if( ( CommandLine[ i - 1 ] == ' ' || CommandLine[ i - 1 ] == '=' ) && CommandLine[ i ] != ' ' && CommandLine[ i ] != '=' && StartValueCursor == 0 )
00856 {
00857 StartValueCursor = i;
00858
00859 if( CommandLine[ i ] == '"' && SpaceSeparatedValue == false )
00860 {
00861
00862 SpaceSeparatedValue = true;
00863 StartValueCursor = i + 1;
00864 }
00865
00866 continue;
00867 }
00868 if( StartValueCursor != 0 )
00869 {
00870 if( ( CommandLine[ i ] == '"' && SpaceSeparatedValue == true ) ||
00871 ( CommandLine[ i ] == ' ' && SpaceSeparatedValue == false ) ||
00872 ( strlen( CommandLine ) == i + 1 && SpaceSeparatedValue == false ) )
00873 {
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884 if( strlen( CommandLine ) == i + 1 && SpaceSeparatedValue == false )
00885 {
00886 strncpy( ParameterValue , CommandLine + StartValueCursor , i - StartValueCursor + 1 );
00887 ParameterValue[ i - StartValueCursor + 1 ] = '\0';
00888 }
00889 else
00890 {
00891 strncpy( ParameterValue , CommandLine + StartValueCursor , i - StartValueCursor );
00892 ParameterValue[ i - StartValueCursor ] = '\0';
00893 }
00894
00895 return;
00896 }
00897 }
00898 }
00899 }
00900 if( SpaceSeparatedValue )
00901 {
00902 throw( nitro::exception( "Illegal sintax" , 0 ) );
00903 }
00904 else
00905 {
00906 std::size_t StrLen( strlen( CommandLine ) );
00907 strncpy( ParameterValue , CommandLine + StartValueCursor , StrLen - StartValueCursor + 1 );
00908 ParameterValue[ StrLen - StartValueCursor + 1 ] = '\0';
00909 }
00910 }
00911 catch( nitro::exception e )
00912 {
00913 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const char * CommandLine , const char * ParameterName , const char * DefaultValue , char * ParameterValue )::" ) + e.what() , e.code() ) );
00914 }
00915 catch( ... )
00916 {
00917 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const char * CommandLine , const char * ParameterName , const char * DefaultValue , char * ParameterValue )::An error occured" ) , 1 ) );
00918 }
00919 }
00920
00921 void Parsers::GetCommandLineParameter( int argc , const char * * argv , const char * ParameterName , const char * DefaultValue , char * ParameterValue )
00922 {
00923 try
00924 {
00925 std::string tmpParamName( ParameterName );
00926 for( std::size_t i( 0 ) ; i < ( std::size_t )( argc - 1 ) ; i++ )
00927 {
00928 if( tmpParamName == argv[ i ] )
00929 {
00930 ParameterValue[ 0 ] = '\0';
00931 strcpy( ParameterValue , argv[ i + 1 ] );
00932
00933 return;
00934 }
00935 }
00936
00937 ParameterValue[ 0 ] = '\0';
00938 if( DefaultValue )
00939 {
00940 strcpy( ParameterValue , DefaultValue );
00941 }
00942 }
00943 catch( nitro::exception e )
00944 {
00945 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( int argc , const char * * argv , const char * ParameterName , const char * DefaultValue , const char * ParameterValue )::" ) + e.what() , e.code() ) );
00946 }
00947 catch( ... )
00948 {
00949 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( int argc , const char * * argv , const char * ParameterName , const char * DefaultValue , const char * ParameterValue )::An error occured" ) , 1 ) );
00950 }
00951 }
00952
00953 bool Parsers::BeginsWith( const char * String , const char * Prefix )
00954 {
00955 try
00956 {
00957 if( strlen( String ) < strlen( Prefix ) )
00958 {
00959 return( false );
00960 }
00961
00962 std::size_t i( 0 );
00963
00964 for( ; i < strlen( Prefix ) && Prefix[ i ] == String[ i ] ; i++ );
00965
00966 if( i == strlen( Prefix ) )
00967 {
00968 return( true );
00969 }
00970
00971 return( false );
00972 }
00973 catch( nitro::exception e )
00974 {
00975 throw( nitro::exception( std::string( "Parsers::BeginsWith( const char * String , const char * Prefix )::" ) + e.what() , e.code() ) );
00976 }
00977 catch( ... )
00978 {
00979 throw( nitro::exception( std::string( "Parsers::BeginsWith( const char * String , const char * Prefix )::An error occured" ) , 1 ) );
00980 }
00981 }
00982
00983 bool Parsers::EndsWith( const char * String , const char * Suffix )
00984 {
00985 try
00986 {
00987 std::size_t a( strlen( String ) );
00988 std::size_t b( strlen( Suffix ) );
00989
00990 if( a < b )
00991 {
00992 return( false );
00993 }
00994
00995 size_t i( 0 );
00996
00997 for( ; i < b && Suffix[ i ] == String[ a - b + i ] ; i++ );
00998
00999 if( i == strlen( Suffix ) )
01000 {
01001 return( true );
01002 }
01003
01004 return( false );
01005 }
01006 catch( nitro::exception e )
01007 {
01008 throw( nitro::exception( std::string( "Parsers::EndsWith( const char * String , const char * Suffix )::" ) + e.what() , e.code() ) );
01009 }
01010 catch( ... )
01011 {
01012 throw( nitro::exception( std::string( "Parsers::EndsWith( const char * String , const char * Suffix )::An error occured" ) , 1 ) );
01013 }
01014 }
01015
01016 int Converters::atoi( const std::string & StringValue , const std::size_t & Mode )
01017 {
01018 try
01019 {
01020 int Result( 0 );
01021
01022 switch( Mode )
01023 {
01024 case( nitro::OCTAL ):
01025 sscanf( StringValue.c_str() , "%o" , & Result );
01026 break;
01027
01028 case( nitro::DECIMAL ):
01029 sscanf( StringValue.c_str() , "%d" , & Result );
01030 break;
01031
01032 case( nitro::HEXADECIMAL ):
01033 sscanf( StringValue.c_str() , "%x" , & Result );
01034 break;
01035 }
01036
01037 return( Result );
01038 }
01039 catch( nitro::exception e )
01040 {
01041 throw( nitro::exception( std::string( "Converters::atoi( const std::string & StringValue , const std::size_t & Mode /* = nitro::Converters::DECIMAL */ )::" ) + e.what() , e.code() ) );
01042 }
01043 catch( ... )
01044 {
01045 throw( nitro::exception( std::string( "Converters::atoi( const std::string & StringValue , const std::size_t & Mode )::An error occured" ) , 0 ) );
01046 }
01047 }
01048
01049 double Converters::atof( const char * StringValue )
01050 {
01051 try
01052 {
01053 return( ::atof( StringValue ) );
01054 }
01055 catch( nitro::exception e )
01056 {
01057 throw( nitro::exception( std::string( "Converters::atof( const char * StringValue )::" ) + e.what() , e.code() ) );
01058 }
01059 catch( ... )
01060 {
01061 throw( nitro::exception( std::string( "Converters::atof( const char * StringValue )::An error occured " ) , 1 ) );
01062 }
01063 }
01064
01065 void Converters::itoa( int Value , char * OutputBuffer , const std::size_t & Mode )
01066 {
01067 try
01068 {
01069 OutputBuffer[ 0 ] = '\0';
01070
01071 switch( Mode )
01072 {
01073 case( nitro::OCTAL ):
01074 sprintf( OutputBuffer , "%o" , Value );
01075 break;
01076
01077 case( nitro::DECIMAL ):
01078 sprintf( OutputBuffer , "%d" , Value );
01079 break;
01080
01081 case( nitro::HEXADECIMAL ):
01082 sprintf( OutputBuffer , "%x" , Value );
01083 break;
01084 }
01085 }
01086 catch( nitro::exception e )
01087 {
01088 throw( nitro::exception( std::string( "Converters::itoa( int Value , char * OutputBuffer , const std::size_t & Mode /* = nitro::DECIMAL */ )::" ) + e.what() , e.code() ) );
01089 }
01090 catch( ... )
01091 {
01092 throw( nitro::exception( std::string( "Converters::itoa( int Value , char * OutputBuffer , const std::size_t & Mode /* = nitro::DECIMAL */ )::An error occured " ) , 1 ) );
01093 }
01094 }
01095
01096 void Converters::ftoa( double Value , char * OutputBuffer )
01097 {
01098 try
01099 {
01100 OutputBuffer[ 0 ] = '\0';
01101 sprintf( OutputBuffer , "%f" , Value );
01102 }
01103 catch( nitro::exception e )
01104 {
01105 throw( nitro::exception( std::string( "Converters::ftoa( double Value , char * OutputBuffer )::" ) + e.what() , e.code() ) );
01106 }
01107 catch( ... )
01108 {
01109 throw( nitro::exception( std::string( "Converters::ftoa( double Value , char * OutputBuffer )::An error occured " ) , 1 ) );
01110 }
01111 }
01112
01113
01114
01115
01116 BEGIN_TESTING_SECTION()
01117
01118 ENABLE_CLASS_TESTING( Parsers )
01119
01120 FUNCTION_TESTING_4( Parsers::GetCommandLineParameter , tstGetCommandLineParameter , const char * , const char * , const char * , char * , void , NO_RET )
01121 FUNCTION_TESTING_2( Parsers::CommandLineParameterExists , tstCommandLineParameterExists , const char * , const char * , bool , bool )
01122 FUNCTION_TESTING_3( Encoders::md5 , tstmd5 , const char * , std::size_t , char * , void , NO_RET )
01123
01124 END_TESTING_SECTION()
01125 }
01126
01127 #endif