Annotation of parser3/src/classes/math.C, revision 1.54

1.1       parser      1: /** @file
                      2:        Parser: @b math parser class.
                      3: 
1.40      paf         4:        Copyright(c) 2001-2005 ArtLebedev Group(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.54    ! misha      11: static const char * const IDENT_MATH_C="$Date: 2008-06-16 12:38:41 $";
1.1       parser     12: 
1.34      paf        13: #include "pa_vmethod_frame.h"
1.1       parser     14: #include "pa_common.h"
                     15: #include "pa_vint.h"
1.2       parser     16: #include "pa_vmath.h"
1.1       parser     17: #include "pa_request.h"
1.19      paf        18: #include "pa_md5.h"
1.28      paf        19: #include "pa_threads.h"
1.1       parser     20: 
1.5       parser     21: #ifdef WIN32
1.28      paf        22: #      define _WIN32_WINNT 0x400
1.5       parser     23: #      include <windows.h>
1.28      paf        24: #      include <wincrypt.h>
1.5       parser     25: #endif
                     26: 
1.34      paf        27: #ifdef HAVE_CRYPT
                     28: #      ifdef HAVE_CRYPT_H
                     29: #              include <crypt.h>
                     30: #      endif
                     31: #else
                     32:        extern char *crypt(const char* , const char* );
1.20      paf        33: #endif
                     34: 
1.1       parser     35: // defines
                     36: 
1.20      paf        37: #define MAX_SALT 8
1.1       parser     38: 
                     39: // class
                     40: 
1.34      paf        41: class MMath: public Methoded {
1.1       parser     42: public:
1.34      paf        43:        MMath();
1.5       parser     44: 
1.1       parser     45: public: // Methoded
1.15      paf        46:        bool used_directly() { return false; }
1.1       parser     47: };
                     48: 
1.34      paf        49: // global variables
                     50: 
                     51: DECLARE_CLASS_VAR(math, 0 /*fictive*/, new MMath);
                     52: 
1.28      paf        53: #ifdef WIN32
                     54: class Random_provider {
                     55:        HCRYPTPROV fhProv;
                     56:        
                     57:        void acquire() {
                     58:                SYNCHRONIZED;
                     59: 
                     60:                if(fhProv)
                     61:                        return;
                     62: 
                     63:                if(!CryptAcquireContext(&fhProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
                     64:                        throw Exception(0,
                     65:                                0,
                     66:                                "CryptAcquireContext failed");
                     67:        }
                     68:        void release() {
                     69:                if(fhProv)
                     70:                        CryptReleaseContext(fhProv, 0);
                     71:        }
                     72:        
                     73: public:
                     74:        Random_provider(): fhProv(0) {}
                     75:        ~Random_provider() { release(); }
                     76:        void generate(void *buffer, size_t size) {
                     77:                acquire();
                     78: 
                     79:                if(!CryptGenRandom(fhProv, size, (BYTE*)buffer))
                     80:                        throw Exception(0,
                     81:                                0,
                     82:                                "CryptGenRandom failed");
                     83:        }
                     84: }
                     85:        random_provider;
                     86: 
                     87: #else
                     88: 
                     89: /// from gen_uuid.c
                     90: static int get_random_fd(void)
                     91: {
1.54    ! misha      92:        struct timeval  tv;
        !            93:        static int      fd = -2;
        !            94:        int             i;
        !            95: 
        !            96:        if (fd == -2) {
        !            97:                gettimeofday(&tv, 0);
        !            98:                fd = open("/dev/urandom", O_RDONLY);
        !            99:                if (fd == -1)
        !           100:                        fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
        !           101:                srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
        !           102:        }
        !           103:        /* Crank the random number generator a few times */
        !           104:        gettimeofday(&tv, 0);
        !           105:        for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
        !           106:                rand();
        !           107:        return fd;
1.28      paf       108: }
                    109: 
                    110: 
                    111: /*
                    112:  * Generate a series of random bytes.  Use /dev/urandom if possible,
                    113:  * and if not, use srandom/random.
                    114:  */
                    115: static void get_random_bytes(void *buf, int nbytes)
                    116: {
1.54    ! misha     117:        int i, fd = get_random_fd();
        !           118:        int lose_counter = 0;
        !           119:        char *cp = (char *) buf;
        !           120: 
        !           121:        if (fd >= 0) {
        !           122:                while (nbytes > 0) {
        !           123:                        i = read(fd, cp, nbytes);
        !           124:                        if (i <= 0) {
        !           125:                                if (lose_counter++ > 16)
        !           126:                                        break;
        !           127:                                continue;
        !           128:                        }
        !           129:                        nbytes -= i;
        !           130:                        cp += i;
        !           131:                        lose_counter = 0;
        !           132:                }
        !           133:        }
        !           134: 
        !           135:        /* XXX put something better here if no /dev/random! */
        !           136:        for (i = 0; i < nbytes; i++)
        !           137:                *cp++ = rand() & 0xFF;
        !           138:        return;
1.28      paf       139: }
                    140: 
                    141: 
                    142: #endif
                    143: 
                    144: 
                    145: // helpers
                    146: 
                    147: static void random(void *buffer, size_t size) {
                    148: #ifdef WIN32
                    149:        random_provider.generate(buffer, size);
                    150: #else
                    151:        get_random_bytes(buffer, size);
                    152: #endif
                    153: }
                    154: 
1.34      paf       155: 
1.1       parser    156: // methods
1.34      paf       157: 
                    158: #define MAX_UINT 0xFFFFFFFFu
                    159: 
1.20      paf       160: static inline int _random(uint top) {
1.28      paf       161:        uint raw;
                    162:        random(&raw, sizeof(raw));
1.34      paf       163:        return int(double(raw) / MAX_UINT * top );
1.20      paf       164: }
1.1       parser    165: 
1.34      paf       166: static void _random(Request& r, MethodParams& params) {
1.41      paf       167:        double top=params.as_double(0, "range must be expression", r);
1.34      paf       168:        if(top<=0 || top>MAX_UINT)
1.45      misha     169:                throw Exception(PARSER_RUNTIME,
1.34      paf       170:                        0,
                    171:                        "top(%g) must be [1..%u]", top, MAX_UINT);
1.1       parser    172:        
1.34      paf       173:        r.write_no_lang(*new VInt(_random(uint(top))));
1.1       parser    174: }
                    175: 
                    176: 
1.20      paf       177: typedef double(*math1_func_ptr)(double);
1.3       parser    178: static double frac(double param) { return param-trunc(param); }
                    179: static double degrees(double param) { return param /PI *180; }
                    180: static double radians(double param) { return param /180 *PI; }
1.1       parser    181: 
1.34      paf       182: static void math1(Request& r, MethodParams& params, math1_func_ptr func) {
1.41      paf       183:        double param=params.as_double(0, "parameter must be expression", r);
                    184:        double result=func(param);
1.34      paf       185:        r.write_no_lang(*new VDouble(result));
1.1       parser    186: }
                    187: 
                    188: #define MATH1(name) \
1.34      paf       189:        static void _##name(Request& r, MethodParams& params) {\
                    190:                math1(r, params, &name);\
1.1       parser    191:        }
                    192: #define MATH1P(name_parser, name_c) \
1.34      paf       193:        static void _##name_parser(Request& r, MethodParams& params) {\
                    194:                math1(r, params, &name_c);\
1.1       parser    195:        }
                    196: MATH1(round);  MATH1(floor);   MATH1P(ceiling, ceil);
1.3       parser    197: MATH1(trunc);  MATH1(frac);
1.1       parser    198: MATH1P(abs, fabs);     MATH1(sign);
1.46      misha     199: MATH1(exp);    
1.48      misha     200: MATH1(log);    MATH1(log10);
1.1       parser    201: MATH1(sin);    MATH1(asin);    
                    202: MATH1(cos);    MATH1(acos);    
                    203: MATH1(tan);    MATH1(atan);
1.3       parser    204: MATH1(degrees);        MATH1(radians);
1.1       parser    205: MATH1(sqrt);
                    206: 
                    207: 
                    208: typedef double (*math2_func_ptr)(double, double);
1.34      paf       209: static void math2(Request& r, MethodParams& params, math2_func_ptr func) {
1.41      paf       210:        double a=params.as_double(0, "parameter must be expression", r);
                    211:        double b=params.as_double(1, "parameter must be expression", r);
                    212:        double result=func(a, b);
1.34      paf       213:        r.write_no_lang(*new VDouble(result));
1.1       parser    214: }
                    215: 
                    216: #define MATH2(name) \
1.34      paf       217:        static void _##name(Request& r, MethodParams& params) {\
                    218:                math2(r, params, &name);\
1.1       parser    219:        }
                    220: MATH2(pow);
                    221: 
1.20      paf       222: inline bool is_salt_body_char(int c) {
                    223:        return isalnum(c) || c == '.' || c=='/';
                    224: }
1.34      paf       225: static size_t calc_prefix_size(const char* salt) {
1.37      paf       226:        if(strlen(salt)) {
1.39      paf       227:                if(!is_salt_body_char((unsigned char)salt[0])) { // $...  {...
1.34      paf       228:                        const char* cur=salt+1; // skip
1.39      paf       229:                        while(is_salt_body_char((unsigned char)*cur++)) // ...$  ...}
1.20      paf       230:                                ;
                    231:                        return cur-salt;
                    232:                } else
                    233:                        return 0;
                    234:        } else
                    235:                return 0;
                    236: }
1.34      paf       237: static void _crypt(Request& r, MethodParams& params) {
                    238:        const char* password=params.as_string(0, "password must be string").cstr();
                    239:        const char* maybe_bodyless_salt=params.as_string(1, "salt must be string").cstr();
1.20      paf       240: 
                    241:        size_t prefix_size=calc_prefix_size(maybe_bodyless_salt);
1.34      paf       242:        const char* normal_salt;
1.20      paf       243:        char normalize_buf[MAX_STRING];
                    244:        if(prefix_size==strlen(maybe_bodyless_salt)) { // bodyless?
                    245:                strncpy(normalize_buf, maybe_bodyless_salt, MAX_STRING-MAX_SALT-1);
                    246:                char *cur=normalize_buf+strlen(normalize_buf);
                    247:                // sould add up MAX_SALT random chars
                    248:                static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
                    249:                "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    250:                for(int i=0; i<MAX_SALT; i++)
                    251:                        *cur++=itoa64[_random(64)];
                    252:                *cur=0;
                    253:                normal_salt=normalize_buf;
                    254:        } else
                    255:                normal_salt=maybe_bodyless_salt;
1.19      paf       256: 
1.54    ! misha     257:        /* FreeBSD style MD5 string 
        !           258:        */
        !           259:        if(strncmp(normal_salt, PA_MD5PW_ID, PA_MD5PW_IDLEN) == 0) {
1.19      paf       260:                const size_t sample_size=120;
1.34      paf       261:                char *sample_buf=new(PointerFreeGC) char[sample_size];
                    262:                pa_MD5Encode((const unsigned char *)password,
                    263:                                (const unsigned char *)normal_salt, sample_buf, sample_size);
                    264:                String sample(sample_buf);
                    265:                r.write_pass_lang(sample);
1.54    ! misha     266:        } else {
1.20      paf       267: #ifdef HAVE_CRYPT
1.34      paf       268:                const char* static_sample_buf=crypt(password, normal_salt);
                    269:                if(!static_sample_buf  // nothing generated
                    270:                        || !static_sample_buf[0] // generated nothing
                    271:                        || strncmp(static_sample_buf, normal_salt, prefix_size)!=0) // salt prefix not preserved
1.45      misha     272:                        throw Exception(PARSER_RUNTIME,
1.34      paf       273:                                0,
                    274:                                "crypt on this platform does not support '%.*s' salt prefix", prefix_size, normal_salt);
1.20      paf       275:                
1.34      paf       276:                r.write_pass_lang(String(pa_strdup(static_sample_buf)));
1.20      paf       277: #else
1.45      misha     278:                throw Exception(PARSER_RUNTIME,
1.34      paf       279:                        0,
1.19      paf       280:                        "salt must start with '" PA_MD5PW_ID "'");
1.20      paf       281: #endif
                    282:        }
1.19      paf       283: }
                    284: 
1.34      paf       285: static void _md5(Request& r, MethodParams& params) {
1.51      misha     286:        const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.27      paf       287: 
                    288:        PA_MD5_CTX context;
                    289:        unsigned char digest[16];
1.34      paf       290:        pa_MD5Init(&context);
                    291:        pa_MD5Update(&context, (const unsigned char*)string, strlen(string));
                    292:        pa_MD5Final(digest, &context);
1.27      paf       293: 
1.34      paf       294:        r.write_pass_lang(*new String(hex_string(digest, sizeof(digest), false)));
1.26      paf       295: }
                    296: 
1.49      misha     297: 
                    298: //SHA-1:
                    299: 
1.50      misha     300: struct SHA1Context {
1.54    ! misha     301:        unsigned Message_Digest[5], Length_Low, Length_High;
        !           302:        unsigned int Message_Block[64];
        !           303:        int Message_Block_Index, Computed, Corrupted;
1.50      misha     304: };
1.49      misha     305: 
                    306: #define SHA1CircularShift(bits,word) ((((word) << (bits)) & 0xFFFFFFFF)|((word) >> (32-(bits))))
                    307: void SHA1ProcessMessageBlock(SHA1Context *);
                    308: void SHA1PadMessage(SHA1Context *);
1.50      misha     309: void SHA1Reset(SHA1Context *context) {
1.54    ! misha     310:        context->Length_Low = context->Length_High = context->Message_Block_Index = 0;
        !           311:        context->Message_Digest[0]      = 0x67452301;
        !           312:        context->Message_Digest[1]      = 0xEFCDAB89;
        !           313:        context->Message_Digest[2]      = 0x98BADCFE;
        !           314:        context->Message_Digest[3]      = 0x10325476;
        !           315:        context->Message_Digest[4]      = 0xC3D2E1F0;
        !           316:        context->Computed = context->Corrupted  = 0;
1.50      misha     317: }
1.49      misha     318: 
1.50      misha     319: int SHA1Result(SHA1Context *context) {
1.54    ! misha     320:        if (context->Corrupted)
        !           321:                return 0;
        !           322:        if (!context->Computed) {
        !           323:                SHA1PadMessage(context);
        !           324:                context->Computed = 1;
        !           325:        }
        !           326:        return 1;
        !           327: }
        !           328: 
        !           329: void SHA1Input(SHA1Context *context, const unsigned char *message_array, unsigned length) {
        !           330:        if (!length)
        !           331:                return;
        !           332:        if (context->Computed || context->Corrupted) {
        !           333:                context->Corrupted = 1;
        !           334:                return;
        !           335:        }
        !           336: 
        !           337:        while(length-- && !context->Corrupted) {
        !           338:                context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
        !           339:                context->Length_Low += 8;
        !           340:                context->Length_Low &= 0xFFFFFFFF;
        !           341:                if (!context->Length_Low && !(context->Length_High=((1+context->Length_High)&0xFFFFFFFF)))
        !           342:                        context->Corrupted = 1; // too long message
        !           343:                if (context->Message_Block_Index == 64)
        !           344:                        SHA1ProcessMessageBlock(context);
        !           345:                message_array++;
1.49      misha     346:        }
1.50      misha     347: }
1.49      misha     348: 
1.50      misha     349: void SHA1ProcessMessageBlock(SHA1Context *context) {
1.54    ! misha     350:        const unsigned K[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 };
        !           351:        int t;
        !           352:        unsigned    temp, W[80], buf[5];
        !           353:        unsigned &A=buf[0], &B=buf[1], &C=buf[2], &D=buf[3], &E=buf[4];
1.50      misha     354: 
1.54    ! misha     355:        for(t = 0; t < 16; t++)
        !           356:                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     357: 
1.54    ! misha     358:        for(t = 16; t < 80; t++)
        !           359:                W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
1.49      misha     360: 
1.54    ! misha     361:        memcpy (buf, context->Message_Digest, sizeof(buf));
        !           362:        for(t = 0; t < 20; t++) {
        !           363:                temp =  (SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0]) & 0xFFFFFFFF;
        !           364:                E = D; D = C;
        !           365:                C = SHA1CircularShift(30,B);
        !           366:                B = A; A = temp;
1.49      misha     367:        }
                    368: 
1.54    ! misha     369:        for(t = 20; t < 40; t++) {
        !           370:                temp = (SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]) & 0xFFFFFFFF;
        !           371:                E = D; D = C;
        !           372:                C = SHA1CircularShift(30,B);
        !           373:                B = A; A = temp;
1.49      misha     374:        }
                    375: 
1.54    ! misha     376:        for(t = 40; t < 60; t++) {
        !           377:                temp = (SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]) & 0xFFFFFFFF;
        !           378:                E = D; D = C;
        !           379:                C = SHA1CircularShift(30,B);
        !           380:                B = A; A = temp;
1.49      misha     381:        }
                    382: 
1.54    ! misha     383:        for(t = 60; t < 80; t++) {
        !           384:                temp = (SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]) & 0xFFFFFFFF;
        !           385:                E = D; D = C;
        !           386:                C = SHA1CircularShift(30,B);
        !           387:                B = A; A = temp;
1.49      misha     388:        }
                    389: 
1.54    ! misha     390:        for (t = 0; t < 5; t++)
        !           391:                context->Message_Digest[t] = (context->Message_Digest[t] + buf[t]) & 0xFFFFFFFF;
1.50      misha     392: 
1.54    ! misha     393:        context->Message_Block_Index = 0;
1.50      misha     394: }
1.49      misha     395: 
1.50      misha     396: void SHA1PadMessage(SHA1Context *context) {
1.54    ! misha     397:        context->Message_Block[context->Message_Block_Index++] = 0x80;
1.50      misha     398:        if (context->Message_Block_Index > 56) {
                    399:                //was 55, one shift
1.54    ! misha     400:                while(context->Message_Block_Index < 64)
        !           401:                        context->Message_Block[context->Message_Block_Index++] = 0;
        !           402:                SHA1ProcessMessageBlock(context);
        !           403:                while(context->Message_Block_Index < 56)
        !           404:                        context->Message_Block[context->Message_Block_Index++] = 0;
1.50      misha     405:        } else
1.54    ! misha     406:                while(context->Message_Block_Index < 56)
        !           407:                        context->Message_Block[context->Message_Block_Index++] = 0;
        !           408:        context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
        !           409:        context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
        !           410:        context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
        !           411:        context->Message_Block[59] = (context->Length_High) & 0xFF;
        !           412:        context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
        !           413:        context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
        !           414:        context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
        !           415:        context->Message_Block[63] = (context->Length_Low) & 0xFF;
        !           416:        SHA1ProcessMessageBlock(context);
1.50      misha     417: }
1.49      misha     418: 
1.54    ! misha     419: static void _sha1(Request& r, MethodParams& params) {
        !           420:        const char *string = params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.49      misha     421: 
1.54    ! misha     422:        SHA1Context c;
        !           423:        SHA1Reset (&c);
        !           424:        SHA1Input (&c, (const unsigned char*)string, strlen(string));
        !           425:        if(!SHA1Result(&c))
        !           426:                throw Exception (PARSER_RUNTIME, 0, "Can not compute SHA1");
        !           427: 
        !           428:        size_t sha1_bufsize=40+/*for zero-teminator*/+1/*for faulty snprintfs*/;
        !           429:        char *sha1_cstr=new(PointerFreeGC) char[sha1_bufsize];
        !           430:        snprintf(sha1_cstr, sha1_bufsize,
        !           431:                        "%08x%08x%08x%08x%08x",
        !           432:                        c.Message_Digest[0],
        !           433:                        c.Message_Digest[1],
        !           434:                        c.Message_Digest[2],
        !           435:                        c.Message_Digest[3],
        !           436:                        c.Message_Digest[4]);
1.49      misha     437: 
1.54    ! misha     438:        r.write_pass_lang(*new String(sha1_cstr));
        !           439: }
1.49      misha     440: 
                    441: 
1.28      paf       442: /// to hell with extra bytes on 64bit platforms
                    443: struct uuid {
1.54    ! misha     444:                unsigned int   time_low;
        !           445:                unsigned short   time_mid;
        !           446:                unsigned short   time_hi_and_version;
        !           447:                unsigned short   clock_seq;
        !           448:                unsigned char    node[6];
1.28      paf       449: };
1.34      paf       450: static void _uuid(Request& r, MethodParams& /*params*/) {
1.28      paf       451: 
                    452:        // random
                    453:        struct uuid uuid;
                    454:        random(&uuid, sizeof(uuid));
                    455: 
                    456:        // http://www.opengroup.org/onlinepubs/9629399/apdxa.htm#tagtcjh_35
                    457:        // ~
                    458:        // version = DCE Security version, with embedded POSIX UIDs.  
                    459:        // variant = DCE
                    460:        //
                    461:        // DCE=Distributed Computing Environment
                    462:        // http://www.opengroup.org/dce/
                    463:        //
                    464:        // they say this influences comparison&such,
                    465:        // but could not figure out how, hence structure layout specified strictly
                    466:        // anyhow, uuidgen on Win32 yield those values
                    467:        // 
                    468:        // xxxxxxxx-xxxx-4xxx-{8,9,A,B}xxx-xxxxxxxxxxxx
                    469:        uuid.clock_seq = (uuid.clock_seq & 0x3FFF) | 0x8000;
                    470:         uuid.time_hi_and_version = (uuid.time_hi_and_version & 0x0FFF) | 0x4000;
                    471:  
                    472:        // format 
1.34      paf       473:        const int uuid_cstr_bufsize=36+1/*for zero-teminator*/+1/*for faulty snprintfs*/;
                    474:        char *uuid_cstr=new(PointerFreeGC) char[uuid_cstr_bufsize];
1.54    ! misha     475:                snprintf(uuid_cstr, uuid_cstr_bufsize,
        !           476:                                "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
        !           477:                                uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
        !           478:                                uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
        !           479:                                uuid.node[0], uuid.node[1], uuid.node[2],
        !           480:                                uuid.node[3], uuid.node[4], uuid.node[5]);
1.28      paf       481: 
1.34      paf       482:        r.write_pass_lang(*new String(uuid_cstr));
1.28      paf       483: }
                    484: 
1.34      paf       485: static void _uid64(Request& r, MethodParams& /*params*/) {
1.30      paf       486: 
                    487:        unsigned char id[64/8];
                    488:        random(&id, sizeof(id));
                    489: 
1.34      paf       490:        r.write_pass_lang(*new String(hex_string(id, sizeof(id), true)));
1.30      paf       491: }
                    492: 
1.43      misha     493: static void _crc32(Request& r, MethodParams& params) {
1.51      misha     494:        const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.43      misha     495:        r.write_no_lang(*new VInt(pa_crc32(string, strlen(string))));
                    496: }
                    497: 
1.1       parser    498: // constructor
                    499: 
1.34      paf       500: MMath::MMath(): Methoded("math") {
1.1       parser    501:        // ^FUNC(expr)  
1.28      paf       502: #define ADDX(name, X) \
                    503:        add_native_method(#name, Method::CT_STATIC, _##name, X, X)
                    504: #define ADD0(name) ADDX(name, 0)
                    505: #define ADD1(name) ADDX(name, 1)
                    506: #define ADD2(name) ADDX(name, 2)
1.1       parser    507: 
                    508:        ADD1(round);    ADD1(floor);    ADD1(ceiling);
1.3       parser    509:        ADD1(trunc);    ADD1(frac);
1.1       parser    510:        ADD1(abs);      ADD1(sign);
1.46      misha     511:        ADD1(exp);
1.48      misha     512:        ADD1(log);      ADD1(log10);
1.1       parser    513:        ADD1(sin);      ADD1(asin);     
                    514:        ADD1(cos);      ADD1(acos);     
                    515:        ADD1(tan);      ADD1(atan);
1.3       parser    516:        ADD1(degrees);  ADD1(radians);
1.1       parser    517:        ADD1(sqrt);
1.3       parser    518:        ADD1(random);
1.1       parser    519: 
1.49      misha     520:        // ^math:pow(x;y)
1.1       parser    521:        ADD2(pow);
                    522: 
1.49      misha     523:        // ^math:crypt[password;salt]
1.19      paf       524:        ADD2(crypt);
1.26      paf       525: 
1.49      misha     526:        // ^math:md5[string]
1.26      paf       527:        ADD1(md5);
1.5       parser    528: 
1.49      misha     529:        // ^math:sha1[string]
                    530:        ADD1(sha1);
                    531:        
                    532:        // ^math:crc32[string]
1.43      misha     533:        ADD1(crc32);
                    534: 
1.49      misha     535:        // ^math:uuid[]
1.29      paf       536:        ADD0(uuid);
                    537: 
1.49      misha     538:        // ^math:uid64[]
1.30      paf       539:        ADD0(uid64);
1.1       parser    540: }

E-mail: