1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783 | //
// This file is part of the aMule Project.
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2005-2011 Froenchenko Leonid ( lfroen@gmail.com / http://www.amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "config.h"
#include <string> // Do_not_auto_remove (g++-4.0.1)
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef PHP_STANDALONE_EN
# include <map>
# include <string>
# include <list>
# include <regex.h>
#else
# include "WebServer.h"
# include <ec/cpp/ECSpecialTags.h>
# include <wx/regex.h>
# include <wx/datetime.h>
#endif
#include "php_syntree.h"
#include "php_core_lib.h"
#include <stdarg.h>
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
/*
* Built-in php functions. Those are both library and core internals.
*
* I'm not going event to get near to what Zend provide, but
* at least base things must be here
*/
/*
* Print info about variable: php var_dump()
*/
void php_var_dump(PHP_VALUE_NODE *node, int ident, int ref)
{
for(int i = 0; i < ident;i++) {
printf("\t");
}
if ( ref ) printf("&");
switch(node->type) {
case PHP_VAL_BOOL: printf("bool(%s)\n", node->int_val ? "true" : "false"); break;
case PHP_VAL_INT: printf("int(%" PRIu64 ")\n", node->int_val); break;<--- %llu in format string (no. 1) requires 'unsigned long long' but the argument type is 'unsigned long'.<--- syntax error
case PHP_VAL_FLOAT: printf("float(%f)\n", node->float_val); break;
case PHP_VAL_STRING: printf("string(%d) \"%s\"\n", (int)strlen(node->str_val), node->str_val); break;
case PHP_VAL_OBJECT: printf("Object(%s)\n", node->obj_val.class_name); break;
case PHP_VAL_ARRAY: {
int arr_size = array_get_size(node);
printf("array(%d) {\n", arr_size);
for(int i = 0; i < arr_size;i++) {
const std::string &curr_key = array_get_ith_key(node, i);
PHP_VAR_NODE *curr_val = array_get_by_str_key(node, curr_key);
printf("\t[%s]=>\n", curr_key.c_str());
php_var_dump(&curr_val->value, ident+1, curr_val->ref_count > 1);
}
for(int i = 0; i < ident;i++) {
printf("\t");
}
printf("}\n");
break;
}
case PHP_VAL_NONE: printf("NULL\n"); break;
case PHP_VAL_VAR_NODE:
case PHP_VAL_INT_DATA: assert(0); break;
}
}
void php_native_var_dump(PHP_VALUE_NODE *)
{
PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
if ( si ) {
assert((si->type == PHP_SCOPE_VAR)||(si->type == PHP_SCOPE_PARAM));
php_var_dump(&si->var->value, 0, 0);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument");
}
}
/*
* Sorting stl-way requires operator ">"
*/
class SortElem {
public:
SortElem() {}
SortElem(PHP_VAR_NODE *p) { obj = p; }<--- Class 'SortElem' has a constructor with 1 argument that is not explicit. [+]Class 'SortElem' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
PHP_VAR_NODE *obj;
static PHP_SYN_FUNC_DECL_NODE *callback;
friend bool operator<(const SortElem &o1, const SortElem &o2);
};
PHP_SYN_FUNC_DECL_NODE *SortElem::callback = 0;
bool operator<(const SortElem &o1, const SortElem &o2)
{
PHP_VALUE_NODE result;
value_value_assign(&SortElem::callback->params[0].si_var->var->value, &o1.obj->value);
value_value_assign(&SortElem::callback->params[1].si_var->var->value, &o2.obj->value);
switch_push_scope_table((PHP_SCOPE_TABLE_TYPE *)SortElem::callback->scope);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
//
// params passed by-value, all & notations ignored
//
result.type = PHP_VAL_NONE;
php_execute(SortElem::callback->code, &result);
cast_value_dnum(&result);
//
// restore stack, free arg list
//
switch_pop_scope_table(0);
value_value_free(&SortElem::callback->params[0].si_var->var->value);
value_value_free(&SortElem::callback->params[1].si_var->var->value);
return result.int_val != 0;
}
void php_native_usort(PHP_VALUE_NODE *)
{
PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
if ( !si || (si->var->value.type != PHP_VAL_ARRAY)) {
php_report_error(PHP_ERROR, "Invalid or missing argument (array)");
return;
}
PHP_VAR_NODE *array = si->var;
si = get_scope_item(g_current_scope, "__param_1");
if ( !si || (si->var->value.type != PHP_VAL_STRING)) {
php_report_error(PHP_ERROR, "Invalid or missing argument (func name)");
return;
}
char *cmp_func_name = si->var->value.str_val;
si = get_scope_item(g_global_scope, cmp_func_name);
if ( !si || (si->type != PHP_SCOPE_FUNC)) {
php_report_error(PHP_ERROR, "Compare function [%s] not found", cmp_func_name);
return;
}
PHP_SYN_FUNC_DECL_NODE *func_decl = si->func->func_decl;
//
// usort invalidates keys, and sorts values
//
PHP_ARRAY_TYPE *arr_obj = (PHP_ARRAY_TYPE *)array->value.ptr_val;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
//
// create vector of values
//
if ( arr_obj->array.empty() ) {
php_report_error(PHP_WARNING, "Sorting array of size 0");
return;
}
std::list<SortElem> sort_list;
for(PHP_ARRAY_ITER_TYPE i = arr_obj->array.begin(); i != arr_obj->array.end(); ++i) {
sort_list.push_back(SortElem(i->second));
}
SortElem::callback = func_decl;
sort_list.sort();
arr_obj->array.clear();
arr_obj->sorted_keys.clear();
unsigned int key = 0;
for(std::list<SortElem>::iterator i = sort_list.begin(); i != sort_list.end(); ++i) {
array_add_to_int_key(&array->value, key++, i->obj);
}
}
/*
* String functions
*/
void php_native_strlen(PHP_VALUE_NODE *result)
{
PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
if ( si ) {
PHP_VALUE_NODE *param = &si->var->value;
cast_value_str(param);
if ( result ) {
cast_value_dnum(result);
result->int_val = strlen(param->str_val);
}
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument");
}
}
void php_native_count(PHP_VALUE_NODE *result)
{
PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
if ( si ) {
PHP_VALUE_NODE *param = &si->var->value;
if ( result ) {
cast_value_dnum(result);
if ( si->var->value.type != PHP_VAL_ARRAY ) {
result->int_val = 0;
} else {
result->int_val = array_get_size(param);
}
}
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument");
}
}
void php_native_isset(PHP_VALUE_NODE *result)
{
PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
if ( si ) {
PHP_VALUE_NODE *param = &si->var->value;
cast_value_str(param);
if ( result ) {
cast_value_bool(result);
result->int_val = (si->var->value.type == PHP_VAL_NONE) ? 0 : 1;
}
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument");
}
}
void php_native_substr(PHP_VALUE_NODE * /*result*/)
{
PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0");
PHP_VALUE_NODE *str = &si_str->var->value;
if ( si_str ) {
cast_value_str(str);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'str' for 'substr'");
return;
}
PHP_SCOPE_ITEM *si_start = get_scope_item(g_current_scope, "__param_1");
PHP_VALUE_NODE *start = &si_start->var->value;
if ( si_start ) {
cast_value_dnum(start);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'start' for 'substr'");
return;
}
// 3-rd is optional
PHP_SCOPE_ITEM *si_end = get_scope_item(g_current_scope, "__param_2");
PHP_VALUE_NODE end = { PHP_VAL_INT, { 0 } };
if ( si_end ) {
end = si_end->var->value;
}
cast_value_dnum(&end);
}
void php_native_split(PHP_VALUE_NODE *result)
{
if ( result ) {
cast_value_array(result);
} else {
return;
}
PHP_VALUE_NODE *pattern, *string_to_split;
PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
if ( si ) {
pattern = &si->var->value;
cast_value_str(pattern);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument: pattern");
return;
}
si = get_scope_item(g_current_scope, "__param_1");
if ( si ) {
string_to_split = &si->var->value;
cast_value_str(string_to_split);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument: string");
return;
}
si = get_scope_item(g_current_scope, "__param_2");
if ( si ) {
PHP_VALUE_NODE *split_limit = &si->var->value;
cast_value_dnum(split_limit);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument: string");
return;
}
#ifdef PHP_STANDALONE_EN
regex_t preg;
int reg_result = regcomp(&preg, pattern->str_val, REG_EXTENDED);
if ( reg_result ) {
char error_buff[256];
regerror(reg_result, &preg, error_buff, sizeof(error_buff));
php_report_error(PHP_ERROR, "Failed in regcomp: %s", error_buff);
#else
wxRegEx preg;
if (!preg.Compile(wxString(char2unicode(pattern->str_val)), wxRE_EXTENDED)) {
php_report_error(PHP_ERROR, "Failed in Compile of: %s", pattern->str_val);
#endif
return;
}
#ifdef PHP_STANDALONE_EN
size_t nmatch = strlen(string_to_split->str_val);
regmatch_t *pmatch = new regmatch_t[nmatch];
#endif
char *str_2_match = string_to_split->str_val;
char *tmp_buff = new char[strlen(string_to_split->str_val)+1];
while ( 1 ) {
// printf("matching: %s\n", str_2_match);
#ifdef PHP_STANDALONE_EN
reg_result = regexec(&preg, str_2_match, nmatch, pmatch, 0);
if ( reg_result ) {
#else
if (!preg.Matches(wxString(char2unicode(str_2_match)))) {
#endif
// no match
break;
}
#ifndef PHP_STANDALONE_EN
// get matching position
size_t start, len;
if (!preg.GetMatch(&start, &len)) {
break; // shouldn't happen
}
#endif
/*
* I will use only first match, since I don't see any sense to have more
* then 1 match in split() call
*/
#ifdef PHP_STANDALONE_EN
for(int i = 0; i < pmatch[0].rm_so; i++) {
#else
for(size_t i = 0; i < start; i++) {
#endif
tmp_buff[i] = str_2_match[i];
}
#ifdef PHP_STANDALONE_EN
tmp_buff[pmatch[0].rm_so] = 0;
#else
tmp_buff[start] = 0;
#endif
// printf("Match added [%s]\n", tmp_buff);
PHP_VAR_NODE *match_val = array_push_back(result);
match_val->value.type = PHP_VAL_STRING;
match_val->value.str_val = strdup(tmp_buff);
#ifdef PHP_STANDALONE_EN
str_2_match += pmatch[0].rm_eo;
#else
str_2_match += start + len;
#endif
}
PHP_VAR_NODE *match_val = array_push_back(result);
match_val->value.type = PHP_VAL_STRING;
match_val->value.str_val = strdup(str_2_match);
delete [] tmp_buff;
#ifdef PHP_STANDALONE_EN
delete [] pmatch;
regfree(&preg);
#endif
}
#ifdef ENABLE_NLS
void php_native_gettext(PHP_VALUE_NODE *result)
{
PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0");
PHP_VALUE_NODE *str = &si_str->var->value;
if ( si_str ) {
cast_value_str(str);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'gettext'");
return;
}
if ( result ) {
cast_value_dnum(result);
result->type = PHP_VAL_STRING;
result->str_val = strdup(gettext(str->str_val));
}
}
void php_native_gettext_noop(PHP_VALUE_NODE *result)
{
PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0");
PHP_VALUE_NODE *str = &si_str->var->value;
if ( si_str ) {
cast_value_str(str);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'gettext_noop'");
return;
}
if ( result ) {
cast_value_dnum(result);
result->type = PHP_VAL_STRING;
result->str_val = strdup(str->str_val);
}
}
void php_native_ngettext(PHP_VALUE_NODE *result)
{
PHP_SCOPE_ITEM *si_msgid = get_scope_item(g_current_scope, "__param_0");
PHP_VALUE_NODE *msgid = &si_msgid->var->value;
if ( si_msgid ) {
cast_value_str(msgid);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'ngettext'");
return;
}
PHP_SCOPE_ITEM *si_msgid_plural = get_scope_item(g_current_scope, "__param_1");
PHP_VALUE_NODE *msgid_plural = &si_msgid_plural->var->value;
if ( si_msgid_plural ) {
cast_value_str(msgid_plural);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid_plural' for 'ngettext'");
return;
}
PHP_SCOPE_ITEM *si_count = get_scope_item(g_current_scope, "__param_2");
PHP_VALUE_NODE *count = &si_count->var->value;
if ( si_count ) {
cast_value_dnum(count);
} else {
php_report_error(PHP_ERROR, "Invalid or missing argument 'count' for 'ngettext'");
return;
}
if ( result ) {
cast_value_dnum(result);
result->type = PHP_VAL_STRING;
result->str_val = strdup(ngettext(msgid->str_val, msgid_plural->str_val, count->int_val));
}
}
#endif
PHP_BLTIN_FUNC_DEF core_lib_funcs[] = {
{
"var_dump",
1,
php_native_var_dump,
},
{
"strlen",
1, php_native_strlen,
},
{
"count",
1, php_native_count,
},
{
"isset",
1, php_native_isset,
},
{
"usort",
2,
php_native_usort,
},
{
"split",
3,
php_native_split,
},
#ifdef ENABLE_NLS
{
"_",
1, php_native_gettext,
},
{
"gettext",
1, php_native_gettext,
},
{
"gettext_noop",
1, php_native_gettext_noop,
},
{
"ngettext",
3, php_native_ngettext,
},
#endif
{ 0, 0, 0, },
};
void php_init_core_lib()
{
// load function definitions
PHP_BLTIN_FUNC_DEF *curr_def = core_lib_funcs;
while ( curr_def->name ) {
php_add_native_func(curr_def);
curr_def++;
}
}
//
// lexer has no include file
//
extern "C"
void php_set_input_buffer(char *buf, int len);
CPhPLibContext::CPhPLibContext(CWebServerBase *server, const char *file)<--- Member variable 'CPhPLibContext::m_curr_str_buffer' is not initialized in the constructor. [+]Member variable 'CPhPLibContext::m_curr_str_buffer' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
{
g_curr_context = this;
m_server = server;
php_engine_init();
phpin = fopen(file, "r");
if ( !phpin ) {
return;
}
phpparse();
m_syn_tree_top = g_syn_tree_top;
m_global_scope = g_global_scope;
}
CPhPLibContext::CPhPLibContext(CWebServerBase *server, char *php_buf, int len)<--- Member variable 'CPhPLibContext::m_curr_str_buffer' is not initialized in the constructor. [+]Member variable 'CPhPLibContext::m_curr_str_buffer' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
{
g_curr_context = this;
m_server = server;
php_engine_init();
m_global_scope = g_global_scope;
php_set_input_buffer(php_buf, len);
phpparse();
m_syn_tree_top = g_syn_tree_top;
}
CPhPLibContext::~CPhPLibContext()
{
SetContext();
php_engine_free();
}
void CPhPLibContext::SetContext()
{
g_syn_tree_top = m_syn_tree_top;
g_global_scope = m_global_scope;
}
void CPhPLibContext::Execute(CWriteStrBuffer *buf)
{
m_curr_str_buffer = buf;
PHP_VALUE_NODE val;
php_execute(g_syn_tree_top, &val);
}
CPhPLibContext *CPhPLibContext::g_curr_context = 0;
/*
* For simplicity and performance sake, this function can
* only handle limited-length printf's. In should be NOT be used
* for string concatenation like printf("xyz %s %s", s1, s2).
*
* Engine will call Print for "print" and "echo"
*/
void CPhPLibContext::Printf(const char *str, ...)
{
va_list args;
va_start(args, str);
if ( !g_curr_context || !g_curr_context->m_curr_str_buffer ) {
vprintf(str, args);
} else {
char buf[4096];
vsnprintf(buf, sizeof(buf), str, args);
g_curr_context->m_curr_str_buffer->Write(buf);
}
va_end(args);
}
void CPhPLibContext::Print(const char *str)
{
if ( !g_curr_context || !g_curr_context->m_curr_str_buffer ) {
printf("%s", str);
} else {
g_curr_context->m_curr_str_buffer->Write(str);
}
}
CPhpFilter::CPhpFilter(CWebServerBase *server, CSession *sess,
const char *file, CWriteStrBuffer *buff)
{
FILE *f = fopen(file, "r");
if ( !f ) {
printf("ERROR: php can not open source file [%s]\n", file);
return;
}
if ( fseek(f, 0, SEEK_END) != 0 ) {
printf("ERROR: fseek failed on php source file [%s]\n", file);
fclose(f);
return;
}
int size = ftell(f);
char *buf = new char [size+1];
rewind(f);
// fread may actually read less if it is a CR-LF-file in Windows
size = fread(buf, 1, size, f);
buf[size] = 0;
fclose(f);
char *scan_ptr = buf;
char *curr_code_end = buf;
while ( strlen(scan_ptr) ) {
scan_ptr = strstr(scan_ptr, "<?php");
if ( !scan_ptr ) {
buff->Write(curr_code_end);
break;
}
if ( scan_ptr != curr_code_end ) {
buff->Write(curr_code_end, scan_ptr - curr_code_end);
}
curr_code_end = strstr(scan_ptr, "?>");
if ( !curr_code_end ) {
break;
}
curr_code_end += 2; // include "?>" in buffer
int len = curr_code_end - scan_ptr;
CPhPLibContext *context = new CPhPLibContext(server, scan_ptr, len);
#ifndef PHP_STANDALONE_EN
load_session_vars("HTTP_GET_VARS", sess->m_get_vars);
load_session_vars("_SESSION", sess->m_vars);
#endif
context->Execute(buff);
#ifndef PHP_STANDALONE_EN
save_session_vars(sess->m_vars);
#endif
delete context;
scan_ptr = curr_code_end;
}
#ifndef PHP_STANDALONE_EN
sess->m_get_vars.clear();
#endif
delete [] buf;
}
/*
* String buffer: almost same as regular 'string' class, but,
* without reallocation when full. Instead, new buffer is
* allocated, and added to list
*/
CWriteStrBuffer::CWriteStrBuffer()
{
m_alloc_size = 1024;
m_total_length = 0;
AllocBuf();
}
CWriteStrBuffer::~CWriteStrBuffer()
{
for(std::list<char *>::iterator i = m_buf_list.begin(); i != m_buf_list.end(); ++i) {
delete [] *i;
}
delete [] m_curr_buf;
}
void CWriteStrBuffer::AllocBuf()
{
m_curr_buf = new char [m_alloc_size];
m_buf_ptr = m_curr_buf;
m_curr_buf_left = m_alloc_size;
}
void CWriteStrBuffer::Write(const char *s, int len)
{
if ( len == -1 ) {
len = strlen(s);
}
m_total_length += len;
while ( len ) {
if ( (len + 1) <= m_curr_buf_left ) {
strncpy(m_buf_ptr, s, len);
m_buf_ptr += len;
m_curr_buf_left -= len;
len = 0;
} else {
memcpy(m_buf_ptr, s, m_curr_buf_left);
int rem_len = len - m_curr_buf_left;
s += m_curr_buf_left;
len = rem_len;
m_buf_list.push_back(m_curr_buf);
AllocBuf();
}
}
}
void CWriteStrBuffer::CopyAll(char *dst_buffer)
{
char *curr_ptr = dst_buffer;
int rem_size = m_total_length;
for(std::list<char *>::iterator i = m_buf_list.begin(); i != m_buf_list.end(); ++i) {
memcpy(curr_ptr, *i, m_alloc_size);
rem_size -= m_alloc_size;
curr_ptr += m_alloc_size;
}
if ( rem_size ) {
memcpy(curr_ptr, m_curr_buf, rem_size);
}
*(curr_ptr + rem_size) = 0;
}
void load_session_vars(const char *target, std::map<std::string, std::string> &varmap)
{
PHP_EXP_NODE *sess_vars_exp_node = get_var_node(target);
PHP_VAR_NODE *sess_vars = sess_vars_exp_node->var_si_node->var;
// i'm not building exp tree, node not needed
delete sess_vars_exp_node;
cast_value_array(&sess_vars->value);
for(std::map<std::string, std::string>::iterator i = varmap.begin(); i != varmap.end(); ++i) {
PHP_VAR_NODE *curr_var = array_get_by_str_key(&sess_vars->value, i->first);
PHP_VALUE_NODE val;
val.type = PHP_VAL_STRING;
val.str_val = const_cast<char *>(i->second.c_str());
value_value_assign(&curr_var->value, &val);
}
}
void save_session_vars(std::map<std::string, std::string> &varmap)
{
PHP_EXP_NODE *sess_vars_exp_node = get_var_node("_SESSION");
PHP_VAR_NODE *sess_vars = sess_vars_exp_node->var_si_node->var;
delete sess_vars_exp_node;
if ( sess_vars->value.type != PHP_VAL_ARRAY ) {
return;
}
for(int i = 0; i < array_get_size(&sess_vars->value); i++) {
std::string s = array_get_ith_key(&sess_vars->value, i);
PHP_VAR_NODE *var = array_get_by_str_key(&sess_vars->value, s);
cast_value_str(&var->value);
varmap[s] = var->value.str_val;
}
}
// File_checked_for_headers
|