|
|
| version 1.50, 2007/08/17 09:00:50 | version 1.52, 2008/06/16 09:37:47 |
|---|---|
| Line 283 static void _crypt(Request& r, MethodPar | Line 283 static void _crypt(Request& r, MethodPar |
| } | } |
| static void _md5(Request& r, MethodParams& params) { | static void _md5(Request& r, MethodParams& params) { |
| const char *string=params.as_string(0, "parameter must be string").cstr(); | const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr(); |
| PA_MD5_CTX context; | PA_MD5_CTX context; |
| unsigned char digest[16]; | unsigned char digest[16]; |
| Line 418 void SHA1PadMessage(SHA1Context *context | Line 418 void SHA1PadMessage(SHA1Context *context |
| static void _sha1(Request& r, MethodParams& params) { | static void _sha1(Request& r, MethodParams& params) { |
| const char *string = params.as_string(0, "parameter must be string").cstr(); | const char *string = params.as_string(0, PARAMETER_MUST_BE_STRING).cstr(); |
| SHA1Context c; | SHA1Context c; |
| SHA1Reset (&c); | SHA1Reset (&c); |
| Line 487 static void _uid64(Request& r, MethodPar | Line 487 static void _uid64(Request& r, MethodPar |
| } | } |
| static void _crc32(Request& r, MethodParams& params) { | static void _crc32(Request& r, MethodParams& params) { |
| const char *string=params.as_string(0, "parameter must be string").cstr(); | const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr(); |
| r.write_no_lang(*new VInt(pa_crc32(string, strlen(string)))); | r.write_no_lang(*new VInt(pa_crc32(string, strlen(string)))); |
| } | } |
| static void _long2ip(Request& r, MethodParams& params) { | static void _long2ip(Request& r, MethodParams& params) { |
| unsigned long l=(unsigned long)trunc(params.as_double(0, "parameter must be expression", r)); | unsigned long l=(unsigned long)trunc(params.as_double(0, "parameter must be expression", r)); |
| static const int ip_cstr_bufsize=15+1+1; | static const int ip_cstr_bufsize=3*4+3+1; |
| char* ip_cstr=new(PointerFreeGC) char[ip_cstr_bufsize]; | char* ip_cstr=new(PointerFreeGC) char[ip_cstr_bufsize]; |
| snprintf(ip_cstr, ip_cstr_bufsize, "%d.%d.%d.%d", | snprintf(ip_cstr, ip_cstr_bufsize, "%d.%d.%d.%d", |
| Line 505 static void _long2ip(Request& r, MethodP | Line 505 static void _long2ip(Request& r, MethodP |
| r.write_no_lang(*new String(ip_cstr)); | r.write_no_lang(*new String(ip_cstr)); |
| } | } |
| static void _ip2long(Request& r, MethodParams& params) { | |
| const String ip=params.as_string(0, PARAMETER_MUST_BE_STRING); | |
| if(ip.is_empty()) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "IP address must not be empty."); | |
| const char* ip_cstr=ip.cstr(); | |
| ulong result=0; | |
| uint byte_value=0; | |
| uint dot_cnt=0; | |
| bool byte_start=true; | |
| bool err=false; | |
| const char* p=ip_cstr; | |
| while(char c=*p++){ | |
| uint digit=(uint)(c-'0'); // assume ascii | |
| if(digit>=0 && digit<=9){ | |
| byte_start=false; | |
| if((byte_value=byte_value*10+digit) > 255){ | |
| err=true; | |
| break; | |
| } | |
| } else if(c=='.'){ | |
| if(byte_start){ // two dots in row or IP started with dot | |
| err=true; | |
| break; | |
| } else { | |
| byte_start=true; | |
| dot_cnt++; | |
| result=(result << 8)+(ulong)byte_value; | |
| byte_value=0; | |
| } | |
| } else { // invalid char | |
| err=true; | |
| break; | |
| } | |
| } | |
| if(err || dot_cnt!=3 || byte_start){ | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "Invalid IP address '%s' specified.", ip_cstr); | |
| } else { | |
| result=(result << 8)+(ulong)byte_value; | |
| r.write_no_lang(*new VDouble(result)); | |
| } | |
| } | |
| // constructor | // constructor |
| MMath::MMath(): Methoded("math") { | MMath::MMath(): Methoded("math") { |
| Line 543 MMath::MMath(): Methoded("math") { | Line 591 MMath::MMath(): Methoded("math") { |
| ADD1(crc32); | ADD1(crc32); |
| ADD1(long2ip); | ADD1(long2ip); |
| ADD1(ip2long); | |
| // ^math:uuid[] | // ^math:uuid[] |
| ADD0(uuid); | ADD0(uuid); |