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