Annotation of parser3/src/classes/math.C, revision 1.65
1.1 parser 1: /** @file
2: Parser: @b math parser class.
3:
1.59 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.20 paf 5: Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
1.28 paf 6:
7: portions from gen_uuid.c,
8: Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
1.22 paf 9: */
1.1 parser 10:
1.34 paf 11: #include "pa_vmethod_frame.h"
1.1 parser 12: #include "pa_common.h"
13: #include "pa_vint.h"
1.2 parser 14: #include "pa_vmath.h"
1.1 parser 15: #include "pa_request.h"
1.19 paf 16: #include "pa_md5.h"
1.65 ! moko 17: #include "pa_sha2.h"
1.55 misha 18: #include "pa_random.h"
1.1 parser 19:
1.5 parser 20: #ifdef WIN32
1.28 paf 21: # define _WIN32_WINNT 0x400
1.5 parser 22: # include <windows.h>
1.28 paf 23: # include <wincrypt.h>
1.5 parser 24: #endif
25:
1.34 paf 26: #ifdef HAVE_CRYPT
27: # ifdef HAVE_CRYPT_H
28: # include <crypt.h>
29: # endif
30: #else
31: extern char *crypt(const char* , const char* );
1.20 paf 32: #endif
33:
1.65 ! moko 34: volatile const char * IDENT_MATH_C="$Id: math.C,v 1.64 2012/09/25 11:31:03 moko Exp $";
1.59 moko 35:
1.1 parser 36: // defines
37:
1.20 paf 38: #define MAX_SALT 8
1.1 parser 39:
40: // class
41:
1.34 paf 42: class MMath: public Methoded {
1.1 parser 43: public:
1.34 paf 44: MMath();
1.5 parser 45:
1.1 parser 46: public: // Methoded
1.15 paf 47: bool used_directly() { return false; }
1.1 parser 48: };
49:
1.34 paf 50: // global variables
51:
52: DECLARE_CLASS_VAR(math, 0 /*fictive*/, new MMath);
53:
1.1 parser 54: // methods
1.34 paf 55:
56: static void _random(Request& r, MethodParams& params) {
1.41 paf 57: double top=params.as_double(0, "range must be expression", r);
1.34 paf 58: if(top<=0 || top>MAX_UINT)
1.45 misha 59: throw Exception(PARSER_RUNTIME,
1.34 paf 60: 0,
61: "top(%g) must be [1..%u]", top, MAX_UINT);
1.1 parser 62:
1.34 paf 63: r.write_no_lang(*new VInt(_random(uint(top))));
1.1 parser 64: }
65:
66:
1.20 paf 67: typedef double(*math1_func_ptr)(double);
1.3 parser 68: static double frac(double param) { return param-trunc(param); }
69: static double degrees(double param) { return param /PI *180; }
70: static double radians(double param) { return param /180 *PI; }
1.1 parser 71:
1.34 paf 72: static void math1(Request& r, MethodParams& params, math1_func_ptr func) {
1.41 paf 73: double param=params.as_double(0, "parameter must be expression", r);
74: double result=func(param);
1.34 paf 75: r.write_no_lang(*new VDouble(result));
1.1 parser 76: }
77:
78: #define MATH1(name) \
1.34 paf 79: static void _##name(Request& r, MethodParams& params) {\
80: math1(r, params, &name);\
1.1 parser 81: }
82: #define MATH1P(name_parser, name_c) \
1.34 paf 83: static void _##name_parser(Request& r, MethodParams& params) {\
84: math1(r, params, &name_c);\
1.1 parser 85: }
86: MATH1(round); MATH1(floor); MATH1P(ceiling, ceil);
1.3 parser 87: MATH1(trunc); MATH1(frac);
1.1 parser 88: MATH1P(abs, fabs); MATH1(sign);
1.46 misha 89: MATH1(exp);
1.48 misha 90: MATH1(log); MATH1(log10);
1.1 parser 91: MATH1(sin); MATH1(asin);
92: MATH1(cos); MATH1(acos);
93: MATH1(tan); MATH1(atan);
1.3 parser 94: MATH1(degrees); MATH1(radians);
1.1 parser 95: MATH1(sqrt);
96:
97:
98: typedef double (*math2_func_ptr)(double, double);
1.34 paf 99: static void math2(Request& r, MethodParams& params, math2_func_ptr func) {
1.41 paf 100: double a=params.as_double(0, "parameter must be expression", r);
101: double b=params.as_double(1, "parameter must be expression", r);
102: double result=func(a, b);
1.34 paf 103: r.write_no_lang(*new VDouble(result));
1.1 parser 104: }
105:
106: #define MATH2(name) \
1.34 paf 107: static void _##name(Request& r, MethodParams& params) {\
108: math2(r, params, &name);\
1.1 parser 109: }
110: MATH2(pow);
111:
1.20 paf 112: inline bool is_salt_body_char(int c) {
113: return isalnum(c) || c == '.' || c=='/';
114: }
1.34 paf 115: static size_t calc_prefix_size(const char* salt) {
1.37 paf 116: if(strlen(salt)) {
1.39 paf 117: if(!is_salt_body_char((unsigned char)salt[0])) { // $... {...
1.34 paf 118: const char* cur=salt+1; // skip
1.39 paf 119: while(is_salt_body_char((unsigned char)*cur++)) // ...$ ...}
1.20 paf 120: ;
121: return cur-salt;
122: } else
123: return 0;
124: } else
125: return 0;
126: }
1.34 paf 127: static void _crypt(Request& r, MethodParams& params) {
128: const char* password=params.as_string(0, "password must be string").cstr();
129: const char* maybe_bodyless_salt=params.as_string(1, "salt must be string").cstr();
1.20 paf 130:
131: size_t prefix_size=calc_prefix_size(maybe_bodyless_salt);
1.34 paf 132: const char* normal_salt;
1.20 paf 133: char normalize_buf[MAX_STRING];
134: if(prefix_size==strlen(maybe_bodyless_salt)) { // bodyless?
135: strncpy(normalize_buf, maybe_bodyless_salt, MAX_STRING-MAX_SALT-1);
136: char *cur=normalize_buf+strlen(normalize_buf);
137: // sould add up MAX_SALT random chars
138: static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
139: "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
140: for(int i=0; i<MAX_SALT; i++)
141: *cur++=itoa64[_random(64)];
142: *cur=0;
143: normal_salt=normalize_buf;
144: } else
145: normal_salt=maybe_bodyless_salt;
1.19 paf 146:
1.54 misha 147: /* FreeBSD style MD5 string
148: */
149: if(strncmp(normal_salt, PA_MD5PW_ID, PA_MD5PW_IDLEN) == 0) {
1.19 paf 150: const size_t sample_size=120;
1.34 paf 151: char *sample_buf=new(PointerFreeGC) char[sample_size];
152: pa_MD5Encode((const unsigned char *)password,
153: (const unsigned char *)normal_salt, sample_buf, sample_size);
154: String sample(sample_buf);
155: r.write_pass_lang(sample);
1.54 misha 156: } else {
1.20 paf 157: #ifdef HAVE_CRYPT
1.34 paf 158: const char* static_sample_buf=crypt(password, normal_salt);
159: if(!static_sample_buf // nothing generated
160: || !static_sample_buf[0] // generated nothing
161: || strncmp(static_sample_buf, normal_salt, prefix_size)!=0) // salt prefix not preserved
1.45 misha 162: throw Exception(PARSER_RUNTIME,
1.34 paf 163: 0,
164: "crypt on this platform does not support '%.*s' salt prefix", prefix_size, normal_salt);
1.20 paf 165:
1.34 paf 166: r.write_pass_lang(String(pa_strdup(static_sample_buf)));
1.20 paf 167: #else
1.45 misha 168: throw Exception(PARSER_RUNTIME,
1.34 paf 169: 0,
1.19 paf 170: "salt must start with '" PA_MD5PW_ID "'");
1.20 paf 171: #endif
172: }
1.19 paf 173: }
174:
1.34 paf 175: static void _md5(Request& r, MethodParams& params) {
1.51 misha 176: const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.27 paf 177:
178: PA_MD5_CTX context;
179: unsigned char digest[16];
1.34 paf 180: pa_MD5Init(&context);
181: pa_MD5Update(&context, (const unsigned char*)string, strlen(string));
182: pa_MD5Final(digest, &context);
1.27 paf 183:
1.34 paf 184: r.write_pass_lang(*new String(hex_string(digest, sizeof(digest), false)));
1.26 paf 185: }
186:
1.49 misha 187:
188: //SHA-1:
189:
1.50 misha 190: struct SHA1Context {
1.54 misha 191: unsigned Message_Digest[5], Length_Low, Length_High;
192: unsigned int Message_Block[64];
193: int Message_Block_Index, Computed, Corrupted;
1.50 misha 194: };
1.49 misha 195:
196: #define SHA1CircularShift(bits,word) ((((word) << (bits)) & 0xFFFFFFFF)|((word) >> (32-(bits))))
197: void SHA1ProcessMessageBlock(SHA1Context *);
198: void SHA1PadMessage(SHA1Context *);
1.50 misha 199: void SHA1Reset(SHA1Context *context) {
1.54 misha 200: context->Length_Low = context->Length_High = context->Message_Block_Index = 0;
201: context->Message_Digest[0] = 0x67452301;
202: context->Message_Digest[1] = 0xEFCDAB89;
203: context->Message_Digest[2] = 0x98BADCFE;
204: context->Message_Digest[3] = 0x10325476;
205: context->Message_Digest[4] = 0xC3D2E1F0;
206: context->Computed = context->Corrupted = 0;
1.50 misha 207: }
1.49 misha 208:
1.50 misha 209: int SHA1Result(SHA1Context *context) {
1.54 misha 210: if (context->Corrupted)
211: return 0;
212: if (!context->Computed) {
213: SHA1PadMessage(context);
214: context->Computed = 1;
215: }
216: return 1;
217: }
218:
219: void SHA1Input(SHA1Context *context, const unsigned char *message_array, unsigned length) {
220: if (!length)
221: return;
222: if (context->Computed || context->Corrupted) {
223: context->Corrupted = 1;
224: return;
225: }
226:
227: while(length-- && !context->Corrupted) {
228: context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
229: context->Length_Low += 8;
230: context->Length_Low &= 0xFFFFFFFF;
231: if (!context->Length_Low && !(context->Length_High=((1+context->Length_High)&0xFFFFFFFF)))
232: context->Corrupted = 1; // too long message
233: if (context->Message_Block_Index == 64)
234: SHA1ProcessMessageBlock(context);
235: message_array++;
1.49 misha 236: }
1.50 misha 237: }
1.49 misha 238:
1.50 misha 239: void SHA1ProcessMessageBlock(SHA1Context *context) {
1.54 misha 240: const unsigned K[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 };
241: int t;
242: unsigned temp, W[80], buf[5];
243: unsigned &A=buf[0], &B=buf[1], &C=buf[2], &D=buf[3], &E=buf[4];
1.50 misha 244:
1.54 misha 245: for(t = 0; t < 16; t++)
246: W[t] = (((unsigned) context->Message_Block[t * 4]) << 24) | (((unsigned) context->Message_Block[t * 4 + 1]) << 16) | (((unsigned) context->Message_Block[t * 4 + 2]) << 8) | ((unsigned) context->Message_Block[t * 4 + 3]);
1.50 misha 247:
1.54 misha 248: for(t = 16; t < 80; t++)
249: W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
1.49 misha 250:
1.54 misha 251: memcpy (buf, context->Message_Digest, sizeof(buf));
252: for(t = 0; t < 20; t++) {
253: temp = (SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0]) & 0xFFFFFFFF;
254: E = D; D = C;
255: C = SHA1CircularShift(30,B);
256: B = A; A = temp;
1.49 misha 257: }
258:
1.54 misha 259: for(t = 20; t < 40; t++) {
260: temp = (SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]) & 0xFFFFFFFF;
261: E = D; D = C;
262: C = SHA1CircularShift(30,B);
263: B = A; A = temp;
1.49 misha 264: }
265:
1.54 misha 266: for(t = 40; t < 60; t++) {
267: temp = (SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]) & 0xFFFFFFFF;
268: E = D; D = C;
269: C = SHA1CircularShift(30,B);
270: B = A; A = temp;
1.49 misha 271: }
272:
1.54 misha 273: for(t = 60; t < 80; t++) {
274: temp = (SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]) & 0xFFFFFFFF;
275: E = D; D = C;
276: C = SHA1CircularShift(30,B);
277: B = A; A = temp;
1.49 misha 278: }
279:
1.54 misha 280: for (t = 0; t < 5; t++)
281: context->Message_Digest[t] = (context->Message_Digest[t] + buf[t]) & 0xFFFFFFFF;
1.50 misha 282:
1.54 misha 283: context->Message_Block_Index = 0;
1.50 misha 284: }
1.49 misha 285:
1.50 misha 286: void SHA1PadMessage(SHA1Context *context) {
1.54 misha 287: context->Message_Block[context->Message_Block_Index++] = 0x80;
1.50 misha 288: if (context->Message_Block_Index > 56) {
289: //was 55, one shift
1.54 misha 290: while(context->Message_Block_Index < 64)
291: context->Message_Block[context->Message_Block_Index++] = 0;
292: SHA1ProcessMessageBlock(context);
293: while(context->Message_Block_Index < 56)
294: context->Message_Block[context->Message_Block_Index++] = 0;
1.50 misha 295: } else
1.54 misha 296: while(context->Message_Block_Index < 56)
297: context->Message_Block[context->Message_Block_Index++] = 0;
298: context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
299: context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
300: context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
301: context->Message_Block[59] = (context->Length_High) & 0xFF;
302: context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
303: context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
304: context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
305: context->Message_Block[63] = (context->Length_Low) & 0xFF;
306: SHA1ProcessMessageBlock(context);
1.50 misha 307: }
1.49 misha 308:
1.62 moko 309: #ifdef PA_BIG_ENDIAN
310: #define SWAP(n) (n)
311: #else
312: #define SWAP(n) (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
313: #endif
314:
1.64 moko 315: void SHA1ReadDigest(void *buf, SHA1Context *c)
1.62 moko 316: {
1.64 moko 317: if(!SHA1Result(c))
1.62 moko 318: throw Exception (PARSER_RUNTIME, 0, "Can not compute SHA1");
319:
1.64 moko 320: ((uint32_t *)buf)[0] = SWAP(c->Message_Digest[0]);
321: ((uint32_t *)buf)[1] = SWAP(c->Message_Digest[1]);
322: ((uint32_t *)buf)[2] = SWAP(c->Message_Digest[2]);
323: ((uint32_t *)buf)[3] = SWAP(c->Message_Digest[3]);
324: ((uint32_t *)buf)[4] = SWAP(c->Message_Digest[4]);
1.62 moko 325: }
326:
1.54 misha 327: static void _sha1(Request& r, MethodParams& params) {
328: const char *string = params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.49 misha 329:
1.54 misha 330: SHA1Context c;
1.64 moko 331: unsigned char digest[20];
1.54 misha 332: SHA1Reset (&c);
333: SHA1Input (&c, (const unsigned char*)string, strlen(string));
1.64 moko 334: SHA1ReadDigest(digest, &c);
1.54 misha 335:
1.62 moko 336: r.write_pass_lang(*new String(hex_string(digest, sizeof(digest), false)));
337: }
338:
1.64 moko 339: void memxor(char *dest, const char *src, size_t n){
340: for (;n>0;n--) *dest++ ^= *src++;
341: }
342:
1.62 moko 343: #define IPAD 0x36
344: #define OPAD 0x5c
345:
1.65 ! moko 346: #define HMAC(key,init,update,final,blocklen,digestlen){ \
1.64 moko 347: unsigned char tempdigest[digestlen], keydigest[digestlen]; \
348: size_t keylen=strlen(key); \
1.65 ! moko 349: /* Reduce the key's size, so that it becomes <= blocklen bytes. */ \
! 350: if (keylen > blocklen){ \
1.64 moko 351: init(&c); \
352: update(&c,(const unsigned char*)hmac, keylen); \
353: final(keydigest, &c); \
354: key = (char *)keydigest; \
355: keylen = digestlen; \
356: } \
357: /* Compute TEMP from KEY and STRING. */ \
1.65 ! moko 358: char block[blocklen]; \
! 359: memset (block, IPAD, blocklen); \
1.64 moko 360: memxor (block, key, keylen); \
361: init(&c); \
1.65 ! moko 362: update(&c, (const unsigned char*)block, blocklen); \
1.64 moko 363: update(&c, (const unsigned char*)string, strlen(string)); \
364: final(tempdigest, &c); \
365: /* Compute result from KEY and TEMP. */ \
1.65 ! moko 366: memset (block, OPAD, blocklen); \
1.64 moko 367: memxor (block, key, keylen); \
368: init(&c); \
1.65 ! moko 369: update(&c, (const unsigned char*)block, blocklen); \
1.64 moko 370: update(&c, (const unsigned char*)tempdigest, digestlen); \
1.62 moko 371: }
1.55 misha 372:
1.62 moko 373: static void _digest(Request& r, MethodParams& params) {
1.63 moko 374: const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING);
1.62 moko 375: const char *string = params.as_string(1, PARAMETER_MUST_BE_STRING).cstr();
376:
1.65 ! moko 377: enum Method { M_MD5, M_SHA1, M_SHA256, M_SHA512 } method;
1.62 moko 378:
1.63 moko 379: if (smethod == "md5") method = M_MD5;
380: else if (smethod == "sha1" ) method = M_SHA1;
1.65 ! moko 381: else if (smethod == "sha256" ) method = M_SHA256;
! 382: else if (smethod == "sha512" ) method = M_SHA512;
1.63 moko 383: else throw Exception(PARSER_RUNTIME, &smethod, "must be 'md5' or 'sha1'");
1.62 moko 384:
385: const char *hmac=0;
1.63 moko 386: enum Format { F_HEX, F_BASE64 } format = F_HEX;
1.62 moko 387:
388: if(params.count() == 3)
389: if(HashStringValue* options=params.as_hash(2)) {
390: int valid_options=0;
391: if(Value* value=options->get("hmac")) {
392: hmac=value->as_string().cstr();
393: valid_options++;
394: }
1.63 moko 395: if(Value* value=options->get("format")) {
396: const String& sformat=value->as_string();
397: if (sformat == "hex") format = F_HEX;
398: else if (sformat == "base64" ) format = F_BASE64;
399: else throw Exception(PARSER_RUNTIME, &sformat, "must be 'hex' or 'base64'");
1.62 moko 400: valid_options++;
401: }
402: if(valid_options!=options->count())
403: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
404: }
405:
406: String::C digest;
407:
1.63 moko 408: if(method == M_MD5){
1.64 moko 409: PA_MD5_CTX c;
410: if(hmac){
1.65 ! moko 411: HMAC(hmac, pa_MD5Init, pa_MD5Update, pa_MD5Final, 64, 16);
1.64 moko 412: } else {
413: pa_MD5Init(&c);
414: pa_MD5Update(&c, (const unsigned char*)string, strlen(string));
415: }
1.62 moko 416: char *str=(char *)pa_malloc(16);
1.64 moko 417: pa_MD5Final((unsigned char *)str, &c);
1.62 moko 418: digest = String::C(str, 16);
419: }
420:
1.63 moko 421: if(method == M_SHA1){
1.62 moko 422: SHA1Context c;
423: if(hmac){
1.65 ! moko 424: HMAC(hmac, SHA1Reset, SHA1Input, SHA1ReadDigest, 64, 20);
1.62 moko 425: } else {
1.64 moko 426: SHA1Reset(&c);
427: SHA1Input(&c, (const unsigned char*)string, strlen(string));
1.62 moko 428: }
429: char *str=(char *)pa_malloc(20);
1.64 moko 430: SHA1ReadDigest(str, &c);
1.62 moko 431: digest = String::C(str, 20);
432: }
1.49 misha 433:
1.65 ! moko 434: if(method == M_SHA256){
! 435: SHA256_CTX c;
! 436: if(hmac){
! 437: HMAC(hmac, pa_SHA256_Init, pa_SHA256_Update, pa_SHA256_Final, 64, SHA256_DIGEST_LENGTH);
! 438: } else {
! 439: pa_SHA256_Init(&c);
! 440: pa_SHA256_Update(&c, (const unsigned char*)string, strlen(string));
! 441: }
! 442: char *str=(char *)pa_malloc(SHA256_DIGEST_LENGTH);
! 443: pa_SHA256_Final((unsigned char *)str, &c);
! 444: digest = String::C(str, SHA256_DIGEST_LENGTH);
! 445: }
! 446:
! 447: if(method == M_SHA512){
! 448: SHA512_CTX c;
! 449: if(hmac){
! 450: HMAC(hmac, pa_SHA512_Init, pa_SHA512_Update, pa_SHA512_Final, 128, SHA512_DIGEST_LENGTH);
! 451: } else {
! 452: pa_SHA512_Init(&c);
! 453: pa_SHA512_Update(&c, (const unsigned char*)string, strlen(string));
! 454: }
! 455: char *str=(char *)pa_malloc(SHA512_DIGEST_LENGTH);
! 456: pa_SHA512_Final((unsigned char *)str, &c);
! 457: digest = String::C(str, SHA512_DIGEST_LENGTH);
! 458: }
! 459:
1.63 moko 460: if(format == F_HEX){
1.62 moko 461: r.write_pass_lang(*new String(hex_string((unsigned char *)digest.str, digest.length, false)));
462: }
1.63 moko 463: if(format == F_BASE64){
1.62 moko 464: r.write_pass_lang(*new String(pa_base64_encode(digest.str, digest.length)));
465: }
1.54 misha 466: }
1.49 misha 467:
1.55 misha 468: static void _uuid(Request& r, MethodParams& /*params*/) {
469: uuid uuid=get_uuid();
1.49 misha 470:
1.55 misha 471: const size_t bufsize=36+1/*zero-teminator*/+1/*for faulty snprintfs*/;
472: char* cstr=new(PointerFreeGC) char[bufsize];
1.28 paf 473:
1.55 misha 474: snprintf(cstr, bufsize,
475: "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
476: uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
477: uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
478: uuid.node[0], uuid.node[1], uuid.node[2],
479: uuid.node[3], uuid.node[4], uuid.node[5]);
1.28 paf 480:
1.55 misha 481: r.write_pass_lang(*new String(cstr));
1.28 paf 482: }
483:
1.34 paf 484: static void _uid64(Request& r, MethodParams& /*params*/) {
1.30 paf 485:
486: unsigned char id[64/8];
487: random(&id, sizeof(id));
488:
1.34 paf 489: r.write_pass_lang(*new String(hex_string(id, sizeof(id), true)));
1.30 paf 490: }
491:
1.43 misha 492: static void _crc32(Request& r, MethodParams& params) {
1.51 misha 493: const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.43 misha 494: r.write_no_lang(*new VInt(pa_crc32(string, strlen(string))));
495: }
496:
1.61 moko 497: static void toBase(unsigned int value, unsigned int base, char*& ptr){
1.57 misha 498: static const char* hex="0123456789ABCDEF";
499: int rest = value % base;
1.58 misha 500: if(value >= base)
1.57 misha 501: toBase( (value-rest)/base, base, ptr);
502: *ptr++=(char)hex[rest];
503: }
504:
505: static void _convert(Request& r, MethodParams& params) {
1.60 moko 506: const char *str=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
507:
1.57 misha 508: int base_from=params.as_int(1, "base from must be integer", r);
509: if(base_from < 2 || base_from > 16)
510: throw Exception(PARSER_RUNTIME, 0, "base from must be an integer from 2 to 16");
511:
1.60 moko 512: int base_to=params.as_int(2, "base to must be integer", r);
513: if(base_to < 2 || base_to > 16)
514: throw Exception(PARSER_RUNTIME, 0, "base to must be an integer from 2 to 16");
515:
1.61 moko 516: while(isspace(*str))
1.60 moko 517: str++;
518:
519: if(!*str)
520: return;
521:
522: bool negative=false;
523: if(str[0]=='-') {
524: negative=true;
525: str++;
526: } else if(str[0]=='+') {
527: str++;
528: }
529:
1.61 moko 530: unsigned int value=pa_atoui(str, base_from);
1.57 misha 531:
1.61 moko 532: char result_cstr[sizeof(unsigned int)*8+1/*minus for negative number*/+1/*terminator*/];
1.60 moko 533: char* ptr=result_cstr;
534: if(negative)
535: *ptr++='-';
1.57 misha 536:
537: toBase(value, base_to, ptr);
538: *ptr=0;
539: r.write_pass_lang(*new String(pa_strdup(result_cstr)));
540: }
541:
1.1 parser 542: // constructor
543:
1.34 paf 544: MMath::MMath(): Methoded("math") {
1.1 parser 545: // ^FUNC(expr)
1.28 paf 546: #define ADDX(name, X) \
547: add_native_method(#name, Method::CT_STATIC, _##name, X, X)
548: #define ADD0(name) ADDX(name, 0)
549: #define ADD1(name) ADDX(name, 1)
550: #define ADD2(name) ADDX(name, 2)
1.1 parser 551:
552: ADD1(round); ADD1(floor); ADD1(ceiling);
1.3 parser 553: ADD1(trunc); ADD1(frac);
1.1 parser 554: ADD1(abs); ADD1(sign);
1.46 misha 555: ADD1(exp);
1.48 misha 556: ADD1(log); ADD1(log10);
1.1 parser 557: ADD1(sin); ADD1(asin);
558: ADD1(cos); ADD1(acos);
559: ADD1(tan); ADD1(atan);
1.3 parser 560: ADD1(degrees); ADD1(radians);
1.1 parser 561: ADD1(sqrt);
1.3 parser 562: ADD1(random);
1.1 parser 563:
1.49 misha 564: // ^math:pow(x;y)
1.1 parser 565: ADD2(pow);
566:
1.49 misha 567: // ^math:crypt[password;salt]
1.19 paf 568: ADD2(crypt);
1.26 paf 569:
1.49 misha 570: // ^math:md5[string]
1.26 paf 571: ADD1(md5);
1.5 parser 572:
1.49 misha 573: // ^math:sha1[string]
574: ADD1(sha1);
575:
1.63 moko 576: // ^math:digest[method;string;options]
1.62 moko 577: add_native_method("digest", Method::CT_STATIC, _digest, 2, 3);
578:
1.49 misha 579: // ^math:crc32[string]
1.43 misha 580: ADD1(crc32);
581:
1.49 misha 582: // ^math:uuid[]
1.29 paf 583: ADD0(uuid);
584:
1.49 misha 585: // ^math:uid64[]
1.30 paf 586: ADD0(uid64);
1.57 misha 587:
588: // ^math:convert[number](base-from;base-to)
589: add_native_method("convert", Method::CT_STATIC, _convert, 3, 3);
1.1 parser 590: }
E-mail: