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

1.1       parser      1: /** @file
                      2:        Parser: @b math parser class.
                      3: 
1.108     moko        4:        Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.101     moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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"
1.90      moko       13: #include "pa_base64.h"
1.1       parser     14: #include "pa_vint.h"
1.2       parser     15: #include "pa_vmath.h"
1.69      moko       16: #include "pa_vfile.h"
1.1       parser     17: #include "pa_request.h"
1.19      paf        18: #include "pa_md5.h"
1.65      moko       19: #include "pa_sha2.h"
1.55      misha      20: #include "pa_random.h"
1.1       parser     21: 
1.34      paf        22: #ifdef HAVE_CRYPT
1.76      moko       23: extern "C" char *crypt(const char* , const char* );
1.20      paf        24: #endif
                     25: 
1.111   ! moko       26: volatile const char * IDENT_MATH_C="$Id: math.C,v 1.110 2025/08/31 18:46:29 moko Exp $";
1.59      moko       27: 
1.1       parser     28: // defines
                     29: 
1.20      paf        30: #define MAX_SALT 8
1.1       parser     31: 
                     32: // class
                     33: 
1.34      paf        34: class MMath: public Methoded {
1.81      moko       35: public: // Methoded
                     36:        bool used_directly() { return false; }
                     37: 
1.1       parser     38: public:
1.34      paf        39:        MMath();
1.1       parser     40: };
                     41: 
1.34      paf        42: // global variables
                     43: 
1.81      moko       44: DECLARE_CLASS_VAR(math, new MMath);
1.34      paf        45: 
1.1       parser     46: // methods
1.34      paf        47: 
                     48: static void _random(Request& r, MethodParams& params) {
1.41      paf        49:        double top=params.as_double(0, "range must be expression", r);
1.103     moko       50:        if(top<1 || top>INT_MAX)
                     51:                throw Exception(PARSER_RUNTIME, 0, "top(%.15g) must be [1..%u]", top, INT_MAX);
1.83      moko       52:        r.write(*new VInt(_random(uint(top))));
1.1       parser     53: }
                     54: 
                     55: 
1.20      paf        56: typedef double(*math1_func_ptr)(double);
1.3       parser     57: static double frac(double param) { return param-trunc(param); }
                     58: static double degrees(double param) { return param /PI *180; }
                     59: static double radians(double param) { return param /180 *PI; }
1.1       parser     60: 
1.34      paf        61: static void math1(Request& r, MethodParams& params, math1_func_ptr func) {
1.41      paf        62:        double param=params.as_double(0, "parameter must be expression", r);
                     63:        double result=func(param);
1.83      moko       64:        r.write(*new VDouble(result));
1.1       parser     65: }
                     66: 
                     67: #define MATH1(name) \
1.34      paf        68:        static void _##name(Request& r, MethodParams& params) {\
                     69:                math1(r, params, &name);\
1.1       parser     70:        }
1.74      moko       71: 
1.1       parser     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:        }
1.74      moko       76: 
                     77: MATH1(round)
                     78: MATH1(floor)
                     79: MATH1P(ceiling, ceil)
                     80: MATH1(trunc)
                     81: MATH1(frac)
                     82: MATH1P(abs, fabs)
                     83: MATH1(sign)
                     84: MATH1(exp)
                     85: MATH1(log)
                     86: MATH1(log10)
                     87: MATH1(sin)
                     88: MATH1(asin)
                     89: MATH1(cos)
                     90: MATH1(acos)
                     91: MATH1(tan)
                     92: MATH1(atan)
                     93: MATH1(degrees)
                     94: MATH1(radians)
                     95: MATH1(sqrt)
1.1       parser     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.83      moko      103:        r.write(*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:        }
1.74      moko      110: 
                    111: MATH2(pow)
1.105     moko      112: MATH2(atan2)
1.1       parser    113: 
1.110     moko      114: static inline uint64_t ulp_key_double(double x) {
1.111   ! moko      115:        union { double d; uint64_t u; } v; v.d = x;
1.110     moko      116:        return (v.u & (1ull << 63)) ? (~v.u + 1ull) : (v.u | (1ull << 63));
                    117: }
                    118: 
                    119: static inline uint64_t ulp_distance_double(double a, double b) {
                    120:        if (a == b) return 0;
                    121:        uint64_t ka = ulp_key_double(a);
                    122:        uint64_t kb = ulp_key_double(b);
                    123:        return (ka > kb) ? (ka - kb) : (kb - ka);
                    124: }
                    125: 
                    126: static void _eq(Request& r, MethodParams& params) {
                    127:        double a=params.as_double(0, "parameter must be expression", r);
                    128:        double b=params.as_double(1, "parameter must be expression", r);
                    129:        uint64_t max_ulp=3;
                    130:        if(params.count() == 3)
                    131:                max_ulp=params.as_int(2, "max distance must be integer", r);
                    132:        r.write(VBool::get(ulp_distance_double(a,b)<=max_ulp));
                    133: }
                    134: 
1.77      moko      135: inline bool is_salt_body_char(unsigned char c) {
1.73      moko      136:        return pa_isalnum(c) || c == '.' || c=='/';
1.20      paf       137: }
1.77      moko      138: 
1.34      paf       139: static size_t calc_prefix_size(const char* salt) {
1.37      paf       140:        if(strlen(salt)) {
1.39      paf       141:                if(!is_salt_body_char((unsigned char)salt[0])) { // $...  {...
1.34      paf       142:                        const char* cur=salt+1; // skip
1.39      paf       143:                        while(is_salt_body_char((unsigned char)*cur++)) // ...$  ...}
1.20      paf       144:                                ;
                    145:                        return cur-salt;
                    146:                } else
                    147:                        return 0;
                    148:        } else
                    149:                return 0;
                    150: }
1.34      paf       151: static void _crypt(Request& r, MethodParams& params) {
                    152:        const char* password=params.as_string(0, "password must be string").cstr();
                    153:        const char* maybe_bodyless_salt=params.as_string(1, "salt must be string").cstr();
1.20      paf       154: 
                    155:        size_t prefix_size=calc_prefix_size(maybe_bodyless_salt);
1.34      paf       156:        const char* normal_salt;
1.20      paf       157:        char normalize_buf[MAX_STRING];
                    158:        if(prefix_size==strlen(maybe_bodyless_salt)) { // bodyless?
1.99      moko      159:                pa_strncpy(normalize_buf, maybe_bodyless_salt, MAX_STRING-MAX_SALT);
1.20      paf       160:                char *cur=normalize_buf+strlen(normalize_buf);
                    161:                // sould add up MAX_SALT random chars
                    162:                static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
                    163:                "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    164:                for(int i=0; i<MAX_SALT; i++)
                    165:                        *cur++=itoa64[_random(64)];
                    166:                *cur=0;
                    167:                normal_salt=normalize_buf;
                    168:        } else
                    169:                normal_salt=maybe_bodyless_salt;
1.19      paf       170: 
1.54      misha     171:        /* FreeBSD style MD5 string 
                    172:        */
                    173:        if(strncmp(normal_salt, PA_MD5PW_ID, PA_MD5PW_IDLEN) == 0) {
1.19      paf       174:                const size_t sample_size=120;
1.34      paf       175:                char *sample_buf=new(PointerFreeGC) char[sample_size];
                    176:                pa_MD5Encode((const unsigned char *)password,
                    177:                                (const unsigned char *)normal_salt, sample_buf, sample_size);
                    178:                String sample(sample_buf);
1.83      moko      179:                r.write(sample);
1.54      misha     180:        } else {
1.20      paf       181: #ifdef HAVE_CRYPT
1.34      paf       182:                const char* static_sample_buf=crypt(password, normal_salt);
                    183:                if(!static_sample_buf  // nothing generated
                    184:                        || !static_sample_buf[0] // generated nothing
                    185:                        || strncmp(static_sample_buf, normal_salt, prefix_size)!=0) // salt prefix not preserved
1.45      misha     186:                        throw Exception(PARSER_RUNTIME,
1.34      paf       187:                                0,
                    188:                                "crypt on this platform does not support '%.*s' salt prefix", prefix_size, normal_salt);
1.20      paf       189:                
1.83      moko      190:                r.write(String(pa_strdup(static_sample_buf)));
1.20      paf       191: #else
1.45      misha     192:                throw Exception(PARSER_RUNTIME,
1.34      paf       193:                        0,
1.19      paf       194:                        "salt must start with '" PA_MD5PW_ID "'");
1.20      paf       195: #endif
                    196:        }
1.19      paf       197: }
                    198: 
1.34      paf       199: static void _md5(Request& r, MethodParams& params) {
1.72      moko      200:        const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets).cstr();
1.27      paf       201: 
                    202:        PA_MD5_CTX context;
                    203:        unsigned char digest[16];
1.34      paf       204:        pa_MD5Init(&context);
                    205:        pa_MD5Update(&context, (const unsigned char*)string, strlen(string));
                    206:        pa_MD5Final(digest, &context);
1.27      paf       207: 
1.83      moko      208:        r.write(*new String(hex_string(digest, sizeof(digest), false)));
1.26      paf       209: }
                    210: 
1.49      misha     211: 
                    212: //SHA-1:
                    213: 
1.50      misha     214: struct SHA1Context {
1.54      misha     215:        unsigned Message_Digest[5], Length_Low, Length_High;
                    216:        unsigned int Message_Block[64];
                    217:        int Message_Block_Index, Computed, Corrupted;
1.50      misha     218: };
1.49      misha     219: 
                    220: #define SHA1CircularShift(bits,word) ((((word) << (bits)) & 0xFFFFFFFF)|((word) >> (32-(bits))))
                    221: void SHA1ProcessMessageBlock(SHA1Context *);
                    222: void SHA1PadMessage(SHA1Context *);
1.50      misha     223: void SHA1Reset(SHA1Context *context) {
1.54      misha     224:        context->Length_Low = context->Length_High = context->Message_Block_Index = 0;
                    225:        context->Message_Digest[0]      = 0x67452301;
                    226:        context->Message_Digest[1]      = 0xEFCDAB89;
                    227:        context->Message_Digest[2]      = 0x98BADCFE;
                    228:        context->Message_Digest[3]      = 0x10325476;
                    229:        context->Message_Digest[4]      = 0xC3D2E1F0;
                    230:        context->Computed = context->Corrupted  = 0;
1.50      misha     231: }
1.49      misha     232: 
1.50      misha     233: int SHA1Result(SHA1Context *context) {
1.54      misha     234:        if (context->Corrupted)
                    235:                return 0;
                    236:        if (!context->Computed) {
                    237:                SHA1PadMessage(context);
                    238:                context->Computed = 1;
                    239:        }
                    240:        return 1;
                    241: }
                    242: 
                    243: void SHA1Input(SHA1Context *context, const unsigned char *message_array, unsigned length) {
                    244:        if (!length)
                    245:                return;
                    246:        if (context->Computed || context->Corrupted) {
                    247:                context->Corrupted = 1;
                    248:                return;
                    249:        }
                    250: 
                    251:        while(length-- && !context->Corrupted) {
                    252:                context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
                    253:                context->Length_Low += 8;
                    254:                context->Length_Low &= 0xFFFFFFFF;
                    255:                if (!context->Length_Low && !(context->Length_High=((1+context->Length_High)&0xFFFFFFFF)))
                    256:                        context->Corrupted = 1; // too long message
                    257:                if (context->Message_Block_Index == 64)
                    258:                        SHA1ProcessMessageBlock(context);
                    259:                message_array++;
1.49      misha     260:        }
1.50      misha     261: }
1.49      misha     262: 
1.50      misha     263: void SHA1ProcessMessageBlock(SHA1Context *context) {
1.54      misha     264:        const unsigned K[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 };
                    265:        int t;
                    266:        unsigned    temp, W[80], buf[5];
                    267:        unsigned &A=buf[0], &B=buf[1], &C=buf[2], &D=buf[3], &E=buf[4];
1.50      misha     268: 
1.54      misha     269:        for(t = 0; t < 16; t++)
                    270:                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     271: 
1.54      misha     272:        for(t = 16; t < 80; t++)
                    273:                W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
1.49      misha     274: 
1.54      misha     275:        memcpy (buf, context->Message_Digest, sizeof(buf));
                    276:        for(t = 0; t < 20; t++) {
                    277:                temp =  (SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0]) & 0xFFFFFFFF;
                    278:                E = D; D = C;
                    279:                C = SHA1CircularShift(30,B);
                    280:                B = A; A = temp;
1.49      misha     281:        }
                    282: 
1.54      misha     283:        for(t = 20; t < 40; t++) {
                    284:                temp = (SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]) & 0xFFFFFFFF;
                    285:                E = D; D = C;
                    286:                C = SHA1CircularShift(30,B);
                    287:                B = A; A = temp;
1.49      misha     288:        }
                    289: 
1.54      misha     290:        for(t = 40; t < 60; t++) {
                    291:                temp = (SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]) & 0xFFFFFFFF;
                    292:                E = D; D = C;
                    293:                C = SHA1CircularShift(30,B);
                    294:                B = A; A = temp;
1.49      misha     295:        }
                    296: 
1.54      misha     297:        for(t = 60; t < 80; t++) {
                    298:                temp = (SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]) & 0xFFFFFFFF;
                    299:                E = D; D = C;
                    300:                C = SHA1CircularShift(30,B);
                    301:                B = A; A = temp;
1.49      misha     302:        }
                    303: 
1.54      misha     304:        for (t = 0; t < 5; t++)
                    305:                context->Message_Digest[t] = (context->Message_Digest[t] + buf[t]) & 0xFFFFFFFF;
1.50      misha     306: 
1.54      misha     307:        context->Message_Block_Index = 0;
1.50      misha     308: }
1.49      misha     309: 
1.50      misha     310: void SHA1PadMessage(SHA1Context *context) {
1.54      misha     311:        context->Message_Block[context->Message_Block_Index++] = 0x80;
1.50      misha     312:        if (context->Message_Block_Index > 56) {
                    313:                //was 55, one shift
1.54      misha     314:                while(context->Message_Block_Index < 64)
                    315:                        context->Message_Block[context->Message_Block_Index++] = 0;
                    316:                SHA1ProcessMessageBlock(context);
                    317:                while(context->Message_Block_Index < 56)
                    318:                        context->Message_Block[context->Message_Block_Index++] = 0;
1.50      misha     319:        } else
1.54      misha     320:                while(context->Message_Block_Index < 56)
                    321:                        context->Message_Block[context->Message_Block_Index++] = 0;
                    322:        context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
                    323:        context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
                    324:        context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
                    325:        context->Message_Block[59] = (context->Length_High) & 0xFF;
                    326:        context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
                    327:        context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
                    328:        context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
                    329:        context->Message_Block[63] = (context->Length_Low) & 0xFF;
                    330:        SHA1ProcessMessageBlock(context);
1.50      misha     331: }
1.49      misha     332: 
1.62      moko      333: #ifdef PA_BIG_ENDIAN
                    334: #define SWAP(n) (n)
                    335: #else
                    336: #define SWAP(n) (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
                    337: #endif
                    338: 
1.64      moko      339: void SHA1ReadDigest(void *buf, SHA1Context *c)
1.62      moko      340: {
1.64      moko      341:        if(!SHA1Result(c))
1.106     moko      342:                throw Exception (PARSER_RUNTIME, 0, "Cannot compute SHA1");
1.62      moko      343: 
1.64      moko      344:        ((uint32_t *)buf)[0] = SWAP(c->Message_Digest[0]);
                    345:        ((uint32_t *)buf)[1] = SWAP(c->Message_Digest[1]);
                    346:        ((uint32_t *)buf)[2] = SWAP(c->Message_Digest[2]);
                    347:        ((uint32_t *)buf)[3] = SWAP(c->Message_Digest[3]);
                    348:        ((uint32_t *)buf)[4] = SWAP(c->Message_Digest[4]);
1.62      moko      349: }
                    350: 
1.54      misha     351: static void _sha1(Request& r, MethodParams& params) {
1.72      moko      352:        const char *string = params.as_string(0, PARAMETER_MUST_BE_STRING).cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets).cstr();
1.49      misha     353: 
1.54      misha     354:        SHA1Context c;
1.64      moko      355:        unsigned char digest[20];
1.54      misha     356:        SHA1Reset (&c);
                    357:        SHA1Input (&c, (const unsigned char*)string, strlen(string));
1.64      moko      358:        SHA1ReadDigest(digest, &c);
1.54      misha     359: 
1.83      moko      360:        r.write(*new String(hex_string(digest, sizeof(digest), false)));
1.62      moko      361: }
                    362: 
1.87      moko      363: String::C getData(Value& vdata, Request& r){
                    364:        if(const String* sdata=vdata.get_string()){
                    365:                String::Body body=sdata->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets); // explode content, honor tainting changes
                    366:                return String::C(body.cstr(), body.length());
                    367:        } else {
1.98      moko      368:                VFile *file=vdata.as_vfile();
1.87      moko      369:                return String::C(file->value_ptr(),file->value_size());
                    370:        }
                    371: }
                    372: 
1.64      moko      373: void memxor(char *dest, const char *src, size_t n){
                    374:        for (;n>0;n--) *dest++ ^= *src++;
                    375: }
                    376: 
1.62      moko      377: #define IPAD 0x36
                    378: #define OPAD 0x5c
                    379: 
1.109     moko      380: #define HMAC(key,keylen,init,update,final,blocklen,digestlen){                 \
1.64      moko      381:        unsigned char tempdigest[digestlen], keydigest[digestlen];              \
1.65      moko      382:        /* Reduce the key's size, so that it becomes <= blocklen bytes. */      \
                    383:        if (keylen > blocklen){                                                 \
1.64      moko      384:                init(&c);                                                       \
1.109     moko      385:                update(&c,(const unsigned char*)key, keylen);                   \
1.64      moko      386:                final(keydigest, &c);                                           \
                    387:                key = (char *)keydigest;                                        \
                    388:                keylen = digestlen;                                             \
                    389:        }                                                                       \
                    390:        /* Compute TEMP from KEY and STRING. */                                 \
1.65      moko      391:        char block[blocklen];                                                   \
                    392:        memset (block, IPAD, blocklen);                                         \
1.64      moko      393:        memxor (block, key, keylen);                                            \
                    394:        init(&c);                                                               \
1.65      moko      395:        update(&c, (const unsigned char*)block, blocklen);                      \
1.69      moko      396:        update(&c, (const unsigned char*)data.str, data.length);                \
1.64      moko      397:        final(tempdigest, &c);                                                  \
                    398:        /* Compute result from KEY and TEMP. */                                 \
1.65      moko      399:        memset (block, OPAD, blocklen);                                         \
1.64      moko      400:        memxor (block, key, keylen);                                            \
                    401:        init(&c);                                                               \
1.65      moko      402:        update(&c, (const unsigned char*)block, blocklen);                      \
1.64      moko      403:        update(&c, (const unsigned char*)tempdigest, digestlen);                \
1.62      moko      404: }
1.55      misha     405: 
1.62      moko      406: static void _digest(Request& r, MethodParams& params) {
1.63      moko      407:        const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING);
1.69      moko      408: 
1.87      moko      409:        String::C data=getData(params.as_no_junction(1, "parameter must be string or file"), r);
1.62      moko      410: 
1.65      moko      411:        enum Method { M_MD5, M_SHA1, M_SHA256, M_SHA512 } method;
1.62      moko      412: 
1.63      moko      413:        if (smethod == "md5") method = M_MD5;
                    414:        else if (smethod == "sha1" ) method = M_SHA1;
1.65      moko      415:        else if (smethod == "sha256" ) method = M_SHA256;
                    416:        else if (smethod == "sha512" ) method = M_SHA512;
1.86      moko      417:        else throw Exception(PARSER_RUNTIME, &smethod, "must be 'md5' or 'sha1' or 'sha256' or 'sha512'");
1.62      moko      418: 
                    419:        const char *hmac=0;
1.109     moko      420:        size_t hmac_len=0;
                    421: 
                    422:        enum Format { F_HEX, F_BASE64, F_FILE } format = F_HEX;
1.62      moko      423: 
                    424:        if(params.count() == 3)
                    425:                if(HashStringValue* options=params.as_hash(2)) {
                    426:                        int valid_options=0;
                    427:                        if(Value* value=options->get("hmac")) {
1.109     moko      428:                                if(VFile* vfile=dynamic_cast<VFile *>(value)){
                    429:                                        hmac=(const char* )vfile->value_ptr();
                    430:                                        hmac_len=vfile->value_size();
                    431:                                } else {
                    432:                                        hmac=value->as_string().cstr();
                    433:                                        hmac_len=strlen(hmac);
                    434:                                }
1.62      moko      435:                                valid_options++;
                    436:                        }
1.63      moko      437:                        if(Value* value=options->get("format")) {
                    438:                                const String& sformat=value->as_string();
                    439:                                if (sformat == "hex") format = F_HEX;
                    440:                                else if (sformat == "base64" ) format = F_BASE64;
1.109     moko      441:                                else if (sformat == "file" ) format = F_FILE;
1.63      moko      442:                                else throw Exception(PARSER_RUNTIME, &sformat, "must be 'hex' or 'base64'");
1.62      moko      443:                                valid_options++;
                    444:                        }
                    445:                        if(valid_options!=options->count())
                    446:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    447:                }
                    448: 
                    449:        String::C digest;
                    450: 
1.63      moko      451:        if(method == M_MD5){
1.64      moko      452:                PA_MD5_CTX c;
                    453:                if(hmac){
1.109     moko      454:                        HMAC(hmac, hmac_len, pa_MD5Init, pa_MD5Update, pa_MD5Final, 64, 16);
1.64      moko      455:                } else {
                    456:                        pa_MD5Init(&c);
1.69      moko      457:                        pa_MD5Update(&c, (const unsigned char*)data.str, data.length);
1.64      moko      458:                }
1.62      moko      459:                char *str=(char *)pa_malloc(16);
1.64      moko      460:                pa_MD5Final((unsigned char *)str, &c);
1.62      moko      461:                digest = String::C(str, 16);
                    462:        }
                    463: 
1.63      moko      464:        if(method == M_SHA1){
1.62      moko      465:                SHA1Context c;
                    466:                if(hmac){
1.109     moko      467:                        HMAC(hmac, hmac_len, SHA1Reset, SHA1Input, SHA1ReadDigest, 64, 20);
1.62      moko      468:                } else {
1.64      moko      469:                        SHA1Reset(&c);
1.69      moko      470:                        SHA1Input(&c, (const unsigned char*)data.str, data.length);
1.62      moko      471:                }
                    472:                char *str=(char *)pa_malloc(20);
1.64      moko      473:                SHA1ReadDigest(str, &c);
1.62      moko      474:                digest = String::C(str, 20);
                    475:        }
1.49      misha     476: 
1.65      moko      477:        if(method == M_SHA256){
                    478:                SHA256_CTX c;
                    479:                if(hmac){
1.109     moko      480:                        HMAC(hmac, hmac_len, pa_SHA256_Init, pa_SHA256_Update, pa_SHA256_Final, 64, SHA256_DIGEST_LENGTH);
1.65      moko      481:                } else {
                    482:                        pa_SHA256_Init(&c);
1.69      moko      483:                        pa_SHA256_Update(&c, (const unsigned char*)data.str, data.length);
1.65      moko      484:                }
                    485:                char *str=(char *)pa_malloc(SHA256_DIGEST_LENGTH);
                    486:                pa_SHA256_Final((unsigned char *)str, &c);
                    487:                digest = String::C(str, SHA256_DIGEST_LENGTH);
                    488:        }
                    489: 
                    490:        if(method == M_SHA512){
                    491:                SHA512_CTX c;
                    492:                if(hmac){
1.109     moko      493:                        HMAC(hmac, hmac_len, pa_SHA512_Init, pa_SHA512_Update, pa_SHA512_Final, 128, SHA512_DIGEST_LENGTH);
1.65      moko      494:                } else {
                    495:                        pa_SHA512_Init(&c);
1.69      moko      496:                        pa_SHA512_Update(&c, (const unsigned char*)data.str, data.length);
1.65      moko      497:                }
                    498:                char *str=(char *)pa_malloc(SHA512_DIGEST_LENGTH);
                    499:                pa_SHA512_Final((unsigned char *)str, &c);
                    500:                digest = String::C(str, SHA512_DIGEST_LENGTH);
                    501:        }
                    502: 
1.63      moko      503:        if(format == F_HEX){
1.83      moko      504:                r.write(*new String(hex_string((unsigned char *)digest.str, digest.length, false)));
1.62      moko      505:        }
1.63      moko      506:        if(format == F_BASE64){
1.94      moko      507:                r.write(*new String(pa_base64_encode(digest.str, digest.length, Base64Options(false /*no wrap*/))));
1.62      moko      508:        }
1.109     moko      509:        if(format == F_FILE){
                    510:                VFile& result=*new VFile;
                    511:                result.set_binary(true, digest.str, digest.length);
                    512:                r.write(result);
                    513:        }
1.54      misha     514: }
1.49      misha     515: 
1.97      moko      516: static void _uuid(Request& r, MethodParams& params) {
                    517:        bool lower=false;
                    518:        bool solid=false;
                    519: 
                    520:        if (params.count() == 1)
                    521:                if (HashStringValue* options = params.as_hash(0)) {
                    522:                        int valid_options = 0;
                    523:                        if (Value* vlower = options->get("lower")) {
                    524:                                lower = r.process(*vlower).as_bool();
                    525:                                valid_options++;
                    526:                        }
                    527:                        if (Value* vsolid = options->get("solid")) {
                    528:                                solid = r.process(*vsolid).as_bool();
                    529:                                valid_options++;
                    530:                        }
                    531:                        if (valid_options != options->count())
                    532:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    533:                }
                    534:        
                    535:        r.write(*new String(get_uuid_cstr(lower, solid)));
1.28      paf       536: }
                    537: 
1.100     moko      538: static void _uuid7(Request& r, MethodParams& params) {
                    539:        bool lower=false;
                    540:        bool solid=false;
                    541: 
                    542:        if (params.count() == 1)
                    543:                if (HashStringValue* options = params.as_hash(0)) {
                    544:                        int valid_options = 0;
                    545:                        if (Value* vlower = options->get("lower")) {
                    546:                                lower = r.process(*vlower).as_bool();
                    547:                                valid_options++;
                    548:                        }
                    549:                        if (Value* vsolid = options->get("solid")) {
                    550:                                solid = r.process(*vsolid).as_bool();
                    551:                                valid_options++;
                    552:                        }
                    553:                        if (valid_options != options->count())
                    554:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    555:                }
                    556: 
                    557:        r.write(*new String(get_uuid7_cstr(lower, solid)));
                    558: }
                    559: 
1.97      moko      560: static void _uid64(Request& r, MethodParams& params) {
                    561:        bool lower = false;
                    562: 
                    563:        if (params.count() == 1)
                    564:                if (HashStringValue* options = params.as_hash(0)) {
                    565:                        int valid_options = 0;
                    566:                        if (Value* vlower = options->get("lower")) {
                    567:                                lower = r.process(*vlower).as_bool();
                    568:                                valid_options++;
                    569:                        }
                    570:                        if (valid_options != options->count())
                    571:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    572:                }
                    573: 
1.30      paf       574:        unsigned char id[64/8];
                    575:        random(&id, sizeof(id));
                    576: 
1.97      moko      577:        r.write(*new String(hex_string(id, sizeof(id), !lower)));
1.30      paf       578: }
                    579: 
1.43      misha     580: static void _crc32(Request& r, MethodParams& params) {
1.51      misha     581:        const char *string=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.102     moko      582:        r.write(*new VDouble((uint)pa_crc32(string, strlen(string))));
1.43      misha     583: }
                    584: 
1.89      moko      585: static const char* abc_hex = "0123456789ABCDEF";
1.87      moko      586: 
1.89      moko      587: static unsigned char hex_lookup[256] = {
1.87      moko      588:         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    589:         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    590:         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    591:         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
                    592:         0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    593:         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    594:         0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0
                    595: };
                    596: 
1.89      moko      597: static unsigned char abc_lookup[256] = {};
                    598: static unsigned char abc_256_lookup[256] = {};
1.87      moko      599: 
1.89      moko      600: inline unsigned char *init_abc_256() {
                    601:        if(!abc_256_lookup[255])
                    602:                for(int i=0; i<256; i++) abc_256_lookup[i] = (unsigned char)i;
1.87      moko      603:        return abc_256_lookup;
1.57      misha     604: }
                    605: 
                    606: static void _convert(Request& r, MethodParams& params) {
1.87      moko      607:        String::C data=getData(params.as_no_junction(0, "parameter must be string or file"), r);
1.60      moko      608: 
1.87      moko      609:        bool abc_mode = true;
                    610:        unsigned char *lookup;
                    611:        const char *abc_from;
                    612:        int base_from;
                    613: 
1.89      moko      614:        if(params[1].is_string()) {
1.87      moko      615:                abc_from = params[1].get_string()->cstr();
                    616:                base_from = strlen(abc_from);
                    617:                if(base_from < 2)
                    618:                        throw Exception(PARSER_RUNTIME, 0, "alphabet 'from' must contain at least 2 characters");
                    619:                lookup = abc_lookup;
                    620:                memset(abc_lookup,0,sizeof(abc_lookup));
                    621:                for(int i=0; i<base_from; i++) abc_lookup[(unsigned char)abc_from[i]] = (unsigned char)i;
                    622:        } else {
                    623:                base_from=params.as_int(1, "base 'from' must be integer or string", r);
                    624:                if(base_from < 2 || base_from > 16 && base_from != 256)
                    625:                        throw Exception(PARSER_RUNTIME, 0, "base 'from' must be an integer from 2 to 16 or 256");
1.89      moko      626:                if (base_from == 256) {
1.87      moko      627:                        abc_from = "";
                    628:                        lookup = init_abc_256();
                    629:                } else {
                    630:                        abc_mode = false;
                    631:                        abc_from = abc_hex;
                    632:                        lookup = hex_lookup;
                    633:                }
                    634:        }
1.57      misha     635: 
1.87      moko      636:        const char *abc_to;
                    637:        int base_to;
1.60      moko      638: 
1.89      moko      639:        if(params[2].is_string()) {
1.87      moko      640:                abc_to=params[2].get_string()->cstr();
                    641:                base_to=strlen(abc_to);
                    642:                if(base_to < 2)
                    643:                        throw Exception(PARSER_RUNTIME, 0, "alphabet 'to' must contain at least 2 characters");
                    644:        } else {
                    645:                base_to=params.as_int(2, "base 'to' must be integer or string", r);
                    646:                if(base_to < 2 || base_to > 16 && base_to != 256)
                    647:                        throw Exception(PARSER_RUNTIME, 0, "base 'to' must be an integer from 2 to 16 or 256");
1.89      moko      648:                if (base_to == 256) {
1.87      moko      649:                        abc_to = (char *)init_abc_256();
                    650:                } else {
                    651:                        abc_to = abc_hex;
                    652:                }
                    653:        }
                    654: 
                    655:        VFile* result_file = 0;
1.60      moko      656: 
1.87      moko      657:        if(params.count() == 4)
                    658:                if(HashStringValue* options=params.as_hash(3)) {
                    659:                        int valid_options=0;
                    660:                        if(Value* value=options->get("format")) {
                    661:                                const String& sformat=value->as_string();
                    662:                                if (sformat == "file" ) result_file = new VFile;
                    663:                                else if (sformat != "string") throw Exception(PARSER_RUNTIME, &sformat, "must be 'string' or 'file'");
                    664:                                valid_options++;
                    665:                        }
                    666:                        if(valid_options!=options->count())
                    667:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    668:                }
1.60      moko      669: 
                    670:        bool negative=false;
1.87      moko      671: 
                    672:        // converting digits to their numeric values
                    673: 
                    674:        unsigned char *src=(unsigned char *)pa_strdup(data.str, data.length);
                    675:        const unsigned char *src_end = src + data.length;
                    676: 
                    677:        unsigned char *c;
                    678: 
                    679:        if(abc_mode){
                    680: 
1.89      moko      681:                for(c=src;c<src_end;c++) {
1.87      moko      682:                        unsigned char digit=lookup[*c];
                    683:                        if(!digit && *c != abc_from[0])
1.107     moko      684:                                throw Exception("number.format", 0, "'%c' is an invalid digit", *c);
1.87      moko      685:                        *c=digit;
                    686:                }
                    687: 
                    688:        } else {
                    689:                // numbers mode, allow whitespace and sign
                    690: 
                    691:                while(isspace(*src))
                    692:                        src++;
                    693: 
                    694:                if(src[0]=='-') {
                    695:                        negative=true;
                    696:                        src++;
1.104     moko      697:                        if(!*src || isspace(*src))
1.107     moko      698:                                throw Exception("number.format", 0,  "'-' is an invalid number");
1.87      moko      699:                } else if(src[0]=='+') {
                    700:                        src++;
1.104     moko      701:                        if(!*src || isspace(*src))
1.107     moko      702:                                throw Exception("number.format", 0,  "'+' is an invalid number");
1.87      moko      703:                }
                    704: 
                    705:                for(c=src;c<src_end;c++) {
                    706:                        unsigned char digit=lookup[*c];
                    707:                        if(!digit && *c != abc_from[0] || digit>=base_from) {
                    708:                                for(unsigned char *s=c;s<src_end;s++)
                    709:                                        if(!isspace(*s))
1.107     moko      710:                                                throw Exception("number.format", 0, "'%c' is an invalid digit", *s);
1.87      moko      711:                                src_end=c;
                    712:                                break;
                    713:                        }
                    714:                        *c=digit;
                    715:                }
                    716: 
1.60      moko      717:        }
                    718: 
1.89      moko      719:        if(src==src_end) {
1.87      moko      720:                if(result_file)
                    721:                        r.write(*result_file);
                    722:                return;
                    723:        }
                    724: 
1.92      moko      725:        // core, using log since log2 is not present in FreeBSD < 8.X
1.87      moko      726: 
1.95      moko      727:        Array<char> remainders((size_t)round(data.length * log((double)base_from) / log((double)base_to)) + 1);
1.57      misha     728: 
1.87      moko      729:        do {
                    730:                int carry = 0;
                    731:                unsigned char *dst = src;
                    732:                for (c=src; c<src_end; c++) {
                    733:                        carry = carry * base_from + *c;
                    734:                        if (carry >= base_to) {
1.95      moko      735:                                *(dst++) = (unsigned char)(carry / base_to);
1.87      moko      736:                                carry %= base_to;
                    737:                        } else if (dst > src) {
                    738:                                *(dst++) = 0;
                    739:                        }
                    740:                }
                    741:                src_end = dst;
                    742:                remainders += abc_to[carry];
                    743:        } while (src_end > src);
                    744: 
                    745:        // result processing
                    746: 
                    747:        size_t result_length = negative + remainders.count();
                    748:        char *result_str = (char *)pa_malloc_atomic(result_length+1);
1.60      moko      749:        if(negative)
1.87      moko      750:                result_str[0] = '-';
1.95      moko      751:        for(size_t i=0; i<remainders.count(); i++)
1.87      moko      752:                result_str[result_length - 1 - i] = remainders[i];
                    753:        result_str[result_length]='\0';
                    754: 
1.89      moko      755:        if(result_file) {
1.88      moko      756:                result_file->set(true /*tainted*/, 0 /*binary*/, result_str, result_length, 0, 0, &r);
1.87      moko      757:                r.write(*result_file);
                    758:        } else {
1.91      moko      759:                if(memchr(result_str, 0, result_length))
                    760:                        throw Exception(PARSER_RUNTIME, 0, "Invalid \\x00 character found while converting to string. Convert to file instead.");
                    761: 
                    762:                fix_line_breaks(result_str, result_length);
                    763: 
                    764:                if(result_length)
                    765:                        r.write(*new String(result_str, String::L_TAINTED));
1.87      moko      766:        }
1.57      misha     767: }
                    768: 
1.1       parser    769: // constructor
                    770: 
1.34      paf       771: MMath::MMath(): Methoded("math") {
1.1       parser    772:        // ^FUNC(expr)  
1.105     moko      773: #define ADDN(name, N) \
                    774:        add_native_method(#name, Method::CT_STATIC, _##name, N, N)
                    775: #define ADD1(name) ADDN(name, 1)
1.1       parser    776: 
                    777:        ADD1(round);    ADD1(floor);    ADD1(ceiling);
1.3       parser    778:        ADD1(trunc);    ADD1(frac);
1.1       parser    779:        ADD1(abs);      ADD1(sign);
1.46      misha     780:        ADD1(exp);
1.48      misha     781:        ADD1(log);      ADD1(log10);
1.87      moko      782:        ADD1(sin);      ADD1(asin);
                    783:        ADD1(cos);      ADD1(acos);
1.105     moko      784:        ADD1(tan);      ADD1(atan);     ADDN(atan2, 2);
1.3       parser    785:        ADD1(degrees);  ADD1(radians);
1.1       parser    786:        ADD1(sqrt);
1.3       parser    787:        ADD1(random);
1.1       parser    788: 
1.49      misha     789:        // ^math:pow(x;y)
1.105     moko      790:        ADDN(pow, 2);
1.1       parser    791: 
1.110     moko      792:        // ^math:eq(a;b[;precision])
                    793:        add_native_method("eq", Method::CT_STATIC, _eq, 2, 3);
                    794: 
1.49      misha     795:        // ^math:crypt[password;salt]
1.105     moko      796:        ADDN(crypt, 2);
1.26      paf       797: 
1.49      misha     798:        // ^math:md5[string]
1.26      paf       799:        ADD1(md5);
1.5       parser    800: 
1.49      misha     801:        // ^math:sha1[string]
                    802:        ADD1(sha1);
                    803:        
1.71      moko      804:        // ^math:digest[method;string|file;options]
1.62      moko      805:        add_native_method("digest", Method::CT_STATIC, _digest, 2, 3);
                    806:        
1.49      misha     807:        // ^math:crc32[string]
1.43      misha     808:        ADD1(crc32);
                    809: 
1.49      misha     810:        // ^math:uuid[]
1.97      moko      811:        // ^math:uuid[options hash]
                    812:        add_native_method("uuid", Method::CT_STATIC, _uuid, 0, 1);
1.29      paf       813: 
1.100     moko      814:        // ^math:uuid7[]
                    815:        // ^math:uuid7[options hash]
                    816:        add_native_method("uuid7", Method::CT_STATIC, _uuid7, 0, 1);
                    817: 
1.49      misha     818:        // ^math:uid64[]
1.97      moko      819:        // ^math:uid64[options hash]
                    820:        add_native_method("uid64", Method::CT_STATIC, _uid64, 0, 1);
1.57      misha     821: 
1.87      moko      822:        // ^math:convert[number|file](base-from)|[abc_from](base-to)|[abc_to][options]
                    823:        add_native_method("convert", Method::CT_STATIC, _convert, 3, 4);
1.1       parser    824: }

E-mail: