Annotation of parser3/src/classes/root.C, revision 1.54
1.1 paf 1: /*
1.10 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.12 paf 4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.10 paf 5:
1.54 ! paf 6: $Id: root.C,v 1.53 2001/03/30 05:51:12 paf Exp $
1.1 paf 7: */
8:
1.48 paf 9: #include "pa_config_includes.h"
1.29 paf 10: #include <math.h>
1.18 paf 11:
1.46 paf 12: #include "pa_common.h"
1.1 paf 13: #include "pa_request.h"
1.8 paf 14: #include "_root.h"
1.31 paf 15: #include "pa_vint.h"
1.1 paf 16:
1.34 paf 17: static void _if(Request& r, const String& method_name, Array *params) {
18: Value& condition_code=*static_cast<Value *>(params->get(0));
19: // forcing ^if(this param type)
20: r.fail_if_junction_(false, condition_code,
21: method_name, "condition must be junction");
1.36 paf 22:
1.34 paf 23: bool condition=r.process(condition_code,
24: 0/*no name*/,
1.51 paf 25: false/*don't intercept string*/).as_bool();
1.6 paf 26: if(condition) {
1.34 paf 27: Value& then_code=*static_cast<Value *>(params->get(1));
1.52 paf 28: // forcing ^if{this param type}
1.34 paf 29: r.fail_if_junction_(false, then_code,
30: method_name, "then-parameter must be junction");
31: r.write_pass_lang(r.process(then_code));
1.6 paf 32: } else if(params->size()==3) {
1.34 paf 33: Value& else_code=*static_cast<Value *>(params->get(2));
1.52 paf 34: // forcing ^if{this param type}
1.34 paf 35: r.fail_if_junction_(false, else_code,
36: method_name, "else-parameter must be junction");
37: r.write_pass_lang(r.process(else_code));
1.6 paf 38: }
1.1 paf 39: }
40:
1.22 paf 41: static void _untaint(Request& r, const String& method_name, Array *params) {
1.18 paf 42: const String& lang_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
1.15 paf 43: String::Untaint_lang lang=static_cast<String::Untaint_lang>(
1.23 paf 44: untaint_lang_name2enum->get_int(lang_name));
1.15 paf 45: if(!lang)
1.39 paf 46: RTHROW(0, 0,
1.15 paf 47: &lang_name,
1.18 paf 48: "invalid untaint language");
1.15 paf 49:
1.24 paf 50: {
51: Value *vbody=static_cast<Value *>(params->get(1));
52: // forcing ^untaint[]{this param type}
1.26 paf 53: r.fail_if_junction_(false, *vbody,
54: method_name, "body must be junction");
1.24 paf 55:
1.42 paf 56: Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
57: r.write_pass_lang(r.process(*vbody)); // process marking tainted with that lang
58: }
59: }
60:
61: static void _taint(Request& r, const String& method_name, Array *params) {
1.54 ! paf 62: String::Untaint_lang lang;
! 63: if(params->size()==1)
! 64: lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
! 65: else {
! 66: const String& lang_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
! 67: lang=static_cast<String::Untaint_lang>(
! 68: untaint_lang_name2enum->get_int(lang_name));
! 69: if(!lang)
! 70: RTHROW(0, 0,
1.42 paf 71: &lang_name,
72: "invalid taint language");
1.54 ! paf 73: }
1.42 paf 74:
75: {
1.54 ! paf 76: Value *vbody=static_cast<Value *>(params->get(params->size()-1));
! 77: // forcing {this param type}
! 78: r.fail_if_junction_(true, *vbody, method_name, "body must not be junction");
1.42 paf 79:
1.44 paf 80: // set temporarily as-is language
1.54 ! paf 81: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.44 paf 82: String result(r.pool());
83: result.append(
84: r.process(*vbody).as_string(), // process marking tainted with that lang
1.45 paf 85: lang, true); // force result language to specified
1.42 paf 86: r.write_pass_lang(result);
1.24 paf 87: }
1.15 paf 88: }
89:
1.22 paf 90: static void _process(Request& r, const String& method_name, Array *params) {
1.20 paf 91: // calculate pseudo file name of processed chars
92: // would be something like "/some/file(4) process"
1.18 paf 93: char place[MAX_STRING];
94: #ifndef NO_STRING_ORIGIN
1.25 paf 95: const Origin& origin=method_name.origin();
1.18 paf 96: snprintf(place, MAX_STRING, "%s(%d) %s",
97: origin.file, 1+origin.line,
1.22 paf 98: method_name.cstr());
1.18 paf 99: #else
1.22 paf 100: strncpy(place, MAX_STRING, method_name.cstr());
1.18 paf 101: #endif
102:
1.38 paf 103: VStateless_class& self_class=*r.self->get_class();
1.22 paf 104: {
105: // temporary zero @main so to maybe-replace it in processed code
1.50 paf 106: Temp_method temp_method_main(self_class, *main_method_name, 0);
107: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
108: Temp_method temp_method_auto(self_class, *auto_method_name, 0);
1.22 paf 109:
1.25 paf 110: // evaluate source to process
111: const String& source=
112: r.process(*static_cast<Value *>(params->get(0))).as_string();
113:
1.22 paf 114: // process source code, append processed methods to 'self' class
115: // maybe-define new @main
116: r.use_buf(source.cstr(), place, &self_class);
117:
118: // maybe-execute @main[]
119: if(const Method *method=self_class.get_method(*main_method_name)) {
120: // execute!
121: r.execute(*method->parser_code);
122: }
1.18 paf 123: }
124: }
125:
1.26 paf 126: static void _rem(Request& r, const String& method_name, Array *params) {
1.27 paf 127: // forcing ^rem{this param type}
1.26 paf 128: r.fail_if_junction_(false, *static_cast<Value *>(params->get(0)),
129: method_name, "body must be junction");
130: }
1.18 paf 131:
1.27 paf 132: static void _while(Request& r, const String& method_name, Array *params) {
133: Value& vcondition=*static_cast<Value *>(params->get(0));
134: // forcing ^while(this param type){}
135: r.fail_if_junction_(false, vcondition,
136: method_name, "condition must be junction");
137:
138: Value& body=*static_cast<Value *>(params->get(1));
139: // forcing ^while(){this param type}
140: r.fail_if_junction_(false, body,
141: method_name, "body must be junction");
142:
143: // while...
144: int endless_loop_count=0;
145: while(true) {
146: if(++endless_loop_count>=1973) // endless loop?
1.39 paf 147: RTHROW(0, 0,
1.27 paf 148: &method_name,
149: "endless loop detected");
150:
151: bool condition=
152: r.process(
153: vcondition,
154: 0/*no name*/,
1.51 paf 155: false/*don't intercept string*/).as_bool();
1.27 paf 156: if(!condition) // ...condition is true
157: break;
158:
159: // write processed body
160: r.write_pass_lang(r.process(body));
161: }
162: }
163:
1.28 paf 164: static void _use(Request& r, const String& method_name, Array *params) {
165: Value& vfile=*static_cast<Value *>(params->get(0));
166: // forcing ^rem{this param type}
167: r.fail_if_junction_(true, vfile,
168: method_name, "file name must not be junction");
169:
1.49 paf 170: r.use_file(r.absolute(vfile.as_string()));
1.28 paf 171: }
172:
1.31 paf 173: static void _for(Request& r, const String& method_name, Array *params) {
174: // ^for[i;from-number;to-number-inclusive]{code}[delim]
175:
176: Pool& pool=r.pool();
177: const String& var_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
1.51 paf 178: int from=(int)r.process(*static_cast<Value *>(params->get(1))).as_double();
179: int to=(int)r.process(*static_cast<Value *>(params->get(2))).as_double();
1.31 paf 180: Value& body_code=*static_cast<Value *>(params->get(3));
181: // forcing ^menu{this param type}
182: r.fail_if_junction_(false, body_code,
183: method_name, "body must be junction");
184: Value *delim_code=params->size()==3+1+1?static_cast<Value *>(params->get(3+1)):0;
185:
186: bool need_delim=false;
1.37 paf 187: VInt *vint=new(pool) VInt(pool, 0);
1.31 paf 188: int endless_loop_count=0;
1.37 paf 189: for(int i=from; i<=to; i++) {
1.31 paf 190: if(++endless_loop_count>=2001) // endless loop?
1.39 paf 191: RTHROW(0, 0,
1.31 paf 192: &method_name,
193: "endless loop detected");
1.37 paf 194: vint->set_int(i);
1.31 paf 195: r.wcontext->put_element(var_name, vint);
196:
197: Value& processed_body=r.process(body_code);
198: if(delim_code) { // delimiter set?
199: const String *string=processed_body.get_string();
200: if(need_delim && string && string->size()) // need delim & iteration produced string?
201: r.write_pass_lang(r.process(*delim_code));
202: need_delim=true;
203: }
204: r.write_pass_lang(processed_body);
205: }
206: }
207:
1.33 paf 208: static void _eval(Request& r, const String& method_name, Array *params) {
209: Value& expr=*static_cast<Value *>(params->get(0));
210: r.fail_if_junction_(false, expr,
211: method_name, "need expression");
212: // evaluate expresion
213: Value *result=r.process(expr,
214: 0/*no name*/,
1.51 paf 215: true/*don't intercept string*/).as_expr_result();
1.33 paf 216: if(params->size()==2) {
217: Value& fmt=*static_cast<Value *>(params->get(1));
218: // forcing ^format[this param type]
219: r.fail_if_junction_(true, fmt,
220: method_name, "fmt must not be junction");
221:
222: Pool& pool=r.pool();
1.41 paf 223: String& string=*new(pool) String(pool);
1.51 paf 224: string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
1.41 paf 225: result=new(pool) VString(string);
1.33 paf 226: }
1.40 paf 227: r.write_no_lang(*result);
1.33 paf 228: }
1.31 paf 229:
230:
1.29 paf 231: typedef double (*math_one_double_op_func_ptr)(double);
232: static double round(double op) { return floor(op+0.5); }
233: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
234:
1.37 paf 235: static void double_one_op(
1.30 paf 236: Request& r,
237: const String& method_name, Array *params,
238: math_one_double_op_func_ptr func) {
1.29 paf 239: Pool& pool=r.pool();
240: Value& param=*static_cast<Value *>(params->get(0));
241:
242: // forcing ^round(this param type)
243: r.fail_if_junction_(false, param,
244: method_name, "parameter must be expression");
245:
1.51 paf 246: Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).as_double()));
1.40 paf 247: r.write_no_lang(result);
1.29 paf 248: }
249:
250: static void _round(Request& r, const String& method_name, Array *params) {
1.37 paf 251: double_one_op(r, method_name, params, &round);
1.29 paf 252: }
253:
254: static void _floor(Request& r, const String& method_name, Array *params) {
1.37 paf 255: double_one_op(r, method_name, params, &floor);
1.29 paf 256: }
257:
258: static void _ceiling(Request& r, const String& method_name, Array *params) {
1.37 paf 259: double_one_op(r, method_name, params, &ceil);
1.29 paf 260: }
261:
262: static void _abs(Request& r, const String& method_name, Array *params) {
1.37 paf 263: double_one_op(r, method_name, params, &fabs);
1.29 paf 264: }
265:
266: static void _sign(Request& r, const String& method_name, Array *params) {
1.37 paf 267: double_one_op(r, method_name, params, &sign);
1.29 paf 268: }
1.40 paf 269:
270: // initialize
1.29 paf 271:
1.38 paf 272: void initialize_root_class(Pool& pool, VStateless_class& vclass) {
1.15 paf 273: // ^if(condition){code-when-true}
274: // ^if(condition){code-when-true}{code-when-false}
1.53 paf 275: vclass.add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.15 paf 276:
1.42 paf 277: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.53 paf 278: vclass.add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
1.42 paf 279:
280: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.54 ! paf 281: vclass.add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.18 paf 282:
283: // ^process[code]
1.53 paf 284: vclass.add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.26 paf 285:
286: // ^rem{code}
1.53 paf 287: vclass.add_native_method("rem", Method::CT_ANY, _rem, 1, 1);
1.27 paf 288:
289: // ^while(condition){code}
1.53 paf 290: vclass.add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.28 paf 291:
292: // ^use[file]
1.53 paf 293: vclass.add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.29 paf 294:
1.31 paf 295: // ^for[i;from-number;to-number-inclusive]{code}[delim]
1.53 paf 296: vclass.add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.33 paf 297:
298: // ^eval(expr)
299: // ^eval(expr)[format]
1.53 paf 300: vclass.add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.31 paf 301:
1.29 paf 302:
303: // math functions
304:
305: // ^round(expr)
1.53 paf 306: vclass.add_native_method("round", Method::CT_ANY, _round, 1, 1);
1.29 paf 307:
308: // ^floor(expr)
1.53 paf 309: vclass.add_native_method("floor", Method::CT_ANY, _floor, 1, 1);
1.29 paf 310:
311: // ^ceiling(expr)
1.53 paf 312: vclass.add_native_method("ceiling", Method::CT_ANY, _ceiling, 1, 1);
1.29 paf 313:
314: // ^abs(expr)
1.53 paf 315: vclass.add_native_method("abs", Method::CT_ANY, _abs, 1, 1);
1.29 paf 316:
317: // ^sign(expr)
1.53 paf 318: vclass.add_native_method("sign", Method::CT_ANY, _sign, 1, 1);
1.1 paf 319: }
E-mail: