|
|
| version 1.52, 2008/06/16 09:37:47 | version 1.53, 2008/06/16 12:38:41 |
|---|---|
| Line 491 static void _crc32(Request& r, MethodPar | Line 491 static void _crc32(Request& r, MethodPar |
| 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) { | |
| unsigned long l=(unsigned long)trunc(params.as_double(0, "parameter must be expression", r)); | |
| static const int ip_cstr_bufsize=3*4+3+1; | |
| char* ip_cstr=new(PointerFreeGC) char[ip_cstr_bufsize]; | |
| snprintf(ip_cstr, ip_cstr_bufsize, "%d.%d.%d.%d", | |
| (l>>24) & 0xFF, | |
| (l>>16) & 0xFF, | |
| (l>>8) & 0xFF, | |
| l & 0xFF); | |
| 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 590 MMath::MMath(): Methoded("math") { | Line 528 MMath::MMath(): Methoded("math") { |
| // ^math:crc32[string] | // ^math:crc32[string] |
| ADD1(crc32); | ADD1(crc32); |
| ADD1(long2ip); | |
| ADD1(ip2long); | |
| // ^math:uuid[] | // ^math:uuid[] |
| ADD0(uuid); | ADD0(uuid); |