-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathbasic_functions.c
2593 lines (2171 loc) · 63.2 KB
/
basic_functions.c
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <[email protected]> |
| Zeev Suraski <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_assert.h"
#include "php_crypt.h"
#include "php_streams.h"
#include "php_main.h"
#include "php_globals.h"
#include "php_variables.h"
#include "php_ini.h"
#include "php_image.h"
#include "php_standard.h"
#include "php_math.h"
#include "php_http.h"
#include "php_incomplete_class.h"
#include "php_getopt.h"
#include "php_ext_syslog.h"
#include "ext/standard/info.h"
#include "ext/session/php_session.h"
#include "zend_exceptions.h"
#include "zend_attributes.h"
#include "zend_ini.h"
#include "zend_operators.h"
#include "ext/standard/php_dns.h"
#include "ext/standard/php_uuencode.h"
#include "ext/standard/crc32_x86.h"
#ifdef PHP_WIN32
#include "win32/php_win32_globals.h"
#include "win32/time.h"
#include "win32/ioutil.h"
#endif
typedef struct yy_buffer_state *YY_BUFFER_STATE;
#include "zend.h"
#include "zend_ini_scanner.h"
#include "zend_language_scanner.h"
#include <zend_language_parser.h>
#include "zend_portability.h"
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#ifndef PHP_WIN32
#include <sys/types.h>
#include <sys/stat.h>
#endif
#ifndef PHP_WIN32
# include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <string.h>
#include <locale.h>
#ifdef HAVE_LANGINFO_H
# include <langinfo.h>
#endif
#ifdef HAVE_SYS_MMAN_H
# include <sys/mman.h>
#endif
#ifdef HAVE_SYS_LOADAVG_H
# include <sys/loadavg.h>
#endif
#ifdef PHP_WIN32
# include "win32/unistd.h"
#endif
#ifndef INADDR_NONE
# define INADDR_NONE ((zend_ulong) -1)
#endif
#include "zend_globals.h"
#include "php_globals.h"
#include "SAPI.h"
#include "php_ticks.h"
#ifdef ZTS
PHPAPI int basic_globals_id;
#else
PHPAPI php_basic_globals basic_globals;
#endif
#include "php_fopen_wrappers.h"
#include "streamsfuncs.h"
#include "zend_frameless_function.h"
#include "basic_functions_arginfo.h"
typedef struct _user_tick_function_entry {
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
bool calling;
} user_tick_function_entry;
#if HAVE_PUTENV
typedef struct {
char *putenv_string;
char *previous_value;
zend_string *key;
} putenv_entry;
#endif
/* some prototypes for local functions */
static void user_shutdown_function_dtor(zval *zv);
static void user_tick_function_dtor(user_tick_function_entry *tick_function_entry);
static const zend_module_dep standard_deps[] = { /* {{{ */
ZEND_MOD_OPTIONAL("session")
ZEND_MOD_END
};
/* }}} */
zend_module_entry basic_functions_module = { /* {{{ */
STANDARD_MODULE_HEADER_EX,
NULL,
standard_deps,
"standard", /* extension name */
ext_functions, /* function list */
PHP_MINIT(basic), /* process startup */
PHP_MSHUTDOWN(basic), /* process shutdown */
PHP_RINIT(basic), /* request startup */
PHP_RSHUTDOWN(basic), /* request shutdown */
PHP_MINFO(basic), /* extension info */
PHP_STANDARD_VERSION, /* extension version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef HAVE_PUTENV
static void php_putenv_destructor(zval *zv) /* {{{ */
{
putenv_entry *pe = Z_PTR_P(zv);
if (pe->previous_value) {
# ifdef PHP_WIN32
/* MSVCRT has a bug in putenv() when setting a variable that
* is already set; if the SetEnvironmentVariable() API call
* fails, the Crt will double free() a string.
* We try to avoid this by setting our own value first */
SetEnvironmentVariable(ZSTR_VAL(pe->key), "bugbug");
# endif
putenv(pe->previous_value);
# ifdef PHP_WIN32
efree(pe->previous_value);
# endif
} else {
# ifdef HAVE_UNSETENV
unsetenv(ZSTR_VAL(pe->key));
# elif defined(PHP_WIN32)
SetEnvironmentVariable(ZSTR_VAL(pe->key), NULL);
# ifndef ZTS
_putenv_s(ZSTR_VAL(pe->key), "");
# endif
# else
char **env;
for (env = environ; env != NULL && *env != NULL; env++) {
if (!strncmp(*env, ZSTR_VAL(pe->key), ZSTR_LEN(pe->key))
&& (*env)[ZSTR_LEN(pe->key)] == '=') { /* found it */
*env = "";
break;
}
}
# endif
}
#ifdef HAVE_TZSET
/* don't forget to reset the various libc globals that
* we might have changed by an earlier call to tzset(). */
if (zend_string_equals_literal_ci(pe->key, "TZ")) {
tzset();
}
#endif
free(pe->putenv_string);
zend_string_release(pe->key);
efree(pe);
}
/* }}} */
#endif
static void basic_globals_ctor(php_basic_globals *basic_globals_p) /* {{{ */
{
BG(umask) = -1;
BG(user_tick_functions) = NULL;
BG(user_filter_map) = NULL;
BG(serialize_lock) = 0;
memset(&BG(serialize), 0, sizeof(BG(serialize)));
memset(&BG(unserialize), 0, sizeof(BG(unserialize)));
memset(&BG(url_adapt_session_ex), 0, sizeof(BG(url_adapt_session_ex)));
memset(&BG(url_adapt_output_ex), 0, sizeof(BG(url_adapt_output_ex)));
BG(url_adapt_session_ex).type = 1;
BG(url_adapt_output_ex).type = 0;
zend_hash_init(&BG(url_adapt_session_hosts_ht), 0, NULL, NULL, 1);
zend_hash_init(&BG(url_adapt_output_hosts_ht), 0, NULL, NULL, 1);
#if defined(_REENTRANT)
memset(&BG(mblen_state), 0, sizeof(BG(mblen_state)));
#endif
BG(page_uid) = -1;
BG(page_gid) = -1;
BG(syslog_device) = NULL;
}
/* }}} */
static void basic_globals_dtor(php_basic_globals *basic_globals_p) /* {{{ */
{
if (basic_globals_p->url_adapt_session_ex.tags) {
zend_hash_destroy(basic_globals_p->url_adapt_session_ex.tags);
free(basic_globals_p->url_adapt_session_ex.tags);
}
if (basic_globals_p->url_adapt_output_ex.tags) {
zend_hash_destroy(basic_globals_p->url_adapt_output_ex.tags);
free(basic_globals_p->url_adapt_output_ex.tags);
}
zend_hash_destroy(&basic_globals_p->url_adapt_session_hosts_ht);
zend_hash_destroy(&basic_globals_p->url_adapt_output_hosts_ht);
}
/* }}} */
PHPAPI double php_get_nan(void) /* {{{ */
{
return ZEND_NAN;
}
/* }}} */
PHPAPI double php_get_inf(void) /* {{{ */
{
return ZEND_INFINITY;
}
/* }}} */
#define BASIC_MINIT_SUBMODULE(module) \
if (PHP_MINIT(module)(INIT_FUNC_ARGS_PASSTHRU) != SUCCESS) {\
return FAILURE; \
}
#define BASIC_RINIT_SUBMODULE(module) \
PHP_RINIT(module)(INIT_FUNC_ARGS_PASSTHRU);
#define BASIC_MINFO_SUBMODULE(module) \
PHP_MINFO(module)(ZEND_MODULE_INFO_FUNC_ARGS_PASSTHRU);
#define BASIC_RSHUTDOWN_SUBMODULE(module) \
PHP_RSHUTDOWN(module)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
#define BASIC_MSHUTDOWN_SUBMODULE(module) \
PHP_MSHUTDOWN(module)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
PHP_MINIT_FUNCTION(basic) /* {{{ */
{
#ifdef ZTS
ts_allocate_id(&basic_globals_id, sizeof(php_basic_globals), (ts_allocate_ctor) basic_globals_ctor, (ts_allocate_dtor) basic_globals_dtor);
# ifdef PHP_WIN32
ts_allocate_id(&php_win32_core_globals_id, sizeof(php_win32_core_globals), (ts_allocate_ctor)php_win32_core_globals_ctor, (ts_allocate_dtor)php_win32_core_globals_dtor );
# endif
#else
basic_globals_ctor(&basic_globals);
# ifdef PHP_WIN32
php_win32_core_globals_ctor(&the_php_win32_core_globals);
# endif
#endif
register_basic_functions_symbols(module_number);
php_ce_incomplete_class = register_class___PHP_Incomplete_Class();
php_register_incomplete_class_handlers();
assertion_error_ce = register_class_AssertionError(zend_ce_error);
BASIC_MINIT_SUBMODULE(var)
BASIC_MINIT_SUBMODULE(file)
BASIC_MINIT_SUBMODULE(pack)
BASIC_MINIT_SUBMODULE(browscap)
BASIC_MINIT_SUBMODULE(standard_filters)
BASIC_MINIT_SUBMODULE(user_filters)
BASIC_MINIT_SUBMODULE(password)
#ifdef ZTS
BASIC_MINIT_SUBMODULE(localeconv)
#endif
#ifdef ZEND_INTRIN_SSE4_2_FUNC_PTR
BASIC_MINIT_SUBMODULE(string_intrin)
#endif
#ifdef ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PTR
BASIC_MINIT_SUBMODULE(crc32_x86_intrin)
#endif
#if defined(ZEND_INTRIN_AVX2_FUNC_PTR) || defined(ZEND_INTRIN_SSSE3_FUNC_PTR)
BASIC_MINIT_SUBMODULE(base64_intrin)
#endif
BASIC_MINIT_SUBMODULE(crypt)
BASIC_MINIT_SUBMODULE(dir)
#ifdef HAVE_SYSLOG_H
BASIC_MINIT_SUBMODULE(syslog)
#endif
BASIC_MINIT_SUBMODULE(array)
BASIC_MINIT_SUBMODULE(assert)
BASIC_MINIT_SUBMODULE(url_scanner_ex)
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
BASIC_MINIT_SUBMODULE(proc_open)
#endif
BASIC_MINIT_SUBMODULE(exec)
BASIC_MINIT_SUBMODULE(user_streams)
php_register_url_stream_wrapper("php", &php_stream_php_wrapper);
php_register_url_stream_wrapper("file", &php_plain_files_wrapper);
#ifdef HAVE_GLOB
php_register_url_stream_wrapper("glob", &php_glob_stream_wrapper);
#endif
php_register_url_stream_wrapper("data", &php_stream_rfc2397_wrapper);
php_register_url_stream_wrapper("http", &php_stream_http_wrapper);
php_register_url_stream_wrapper("ftp", &php_stream_ftp_wrapper);
return SUCCESS;
}
/* }}} */
PHP_MSHUTDOWN_FUNCTION(basic) /* {{{ */
{
#ifdef ZTS
ts_free_id(basic_globals_id);
#ifdef PHP_WIN32
ts_free_id(php_win32_core_globals_id);
#endif
#else
basic_globals_dtor(&basic_globals);
#ifdef PHP_WIN32
php_win32_core_globals_dtor(&the_php_win32_core_globals);
#endif
#endif
php_unregister_url_stream_wrapper("php");
php_unregister_url_stream_wrapper("http");
php_unregister_url_stream_wrapper("ftp");
BASIC_MSHUTDOWN_SUBMODULE(browscap)
BASIC_MSHUTDOWN_SUBMODULE(array)
BASIC_MSHUTDOWN_SUBMODULE(assert)
BASIC_MSHUTDOWN_SUBMODULE(url_scanner_ex)
BASIC_MSHUTDOWN_SUBMODULE(file)
BASIC_MSHUTDOWN_SUBMODULE(standard_filters)
#ifdef ZTS
BASIC_MSHUTDOWN_SUBMODULE(localeconv)
#endif
BASIC_MSHUTDOWN_SUBMODULE(crypt)
BASIC_MSHUTDOWN_SUBMODULE(password)
return SUCCESS;
}
/* }}} */
PHP_RINIT_FUNCTION(basic) /* {{{ */
{
memset(BG(strtok_table), 0, 256);
BG(serialize_lock) = 0;
memset(&BG(serialize), 0, sizeof(BG(serialize)));
memset(&BG(unserialize), 0, sizeof(BG(unserialize)));
BG(strtok_string) = NULL;
BG(strtok_last) = NULL;
BG(ctype_string) = NULL;
BG(locale_changed) = 0;
BG(user_compare_fci) = empty_fcall_info;
BG(user_compare_fci_cache) = empty_fcall_info_cache;
BG(page_uid) = -1;
BG(page_gid) = -1;
BG(page_inode) = -1;
BG(page_mtime) = -1;
#ifdef HAVE_PUTENV
zend_hash_init(&BG(putenv_ht), 1, NULL, php_putenv_destructor, 0);
#endif
BG(user_shutdown_function_names) = NULL;
PHP_RINIT(filestat)(INIT_FUNC_ARGS_PASSTHRU);
BASIC_RINIT_SUBMODULE(dir)
BASIC_RINIT_SUBMODULE(url_scanner_ex)
/* Initialize memory for last http headers */
ZVAL_UNDEF(&BG(last_http_headers));
/* Setup default context */
FG(default_context) = NULL;
/* Default to global wrappers only */
FG(stream_wrappers) = NULL;
/* Default to global filters only */
FG(stream_filters) = NULL;
return SUCCESS;
}
/* }}} */
PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */
{
if (BG(strtok_string)) {
zend_string_release(BG(strtok_string));
BG(strtok_string) = NULL;
}
#ifdef HAVE_PUTENV
tsrm_env_lock();
zend_hash_destroy(&BG(putenv_ht));
tsrm_env_unlock();
#endif
if (BG(umask) != -1) {
umask(BG(umask));
}
/* Check if locale was changed and change it back
* to the value in startup environment */
if (BG(locale_changed)) {
setlocale(LC_ALL, "C");
zend_reset_lc_ctype_locale();
zend_update_current_locale();
if (BG(ctype_string)) {
zend_string_release_ex(BG(ctype_string), 0);
BG(ctype_string) = NULL;
}
}
/* FG(stream_wrappers) and FG(stream_filters) are destroyed
* during php_request_shutdown() */
PHP_RSHUTDOWN(filestat)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
#ifdef HAVE_SYSLOG_H
BASIC_RSHUTDOWN_SUBMODULE(syslog);
#endif
BASIC_RSHUTDOWN_SUBMODULE(assert)
BASIC_RSHUTDOWN_SUBMODULE(url_scanner_ex)
BASIC_RSHUTDOWN_SUBMODULE(streams)
#ifdef PHP_WIN32
BASIC_RSHUTDOWN_SUBMODULE(win32_core_globals)
#endif
if (BG(user_tick_functions)) {
zend_llist_destroy(BG(user_tick_functions));
efree(BG(user_tick_functions));
BG(user_tick_functions) = NULL;
}
BASIC_RSHUTDOWN_SUBMODULE(user_filters)
BASIC_RSHUTDOWN_SUBMODULE(browscap)
/* Free last http headers */
zval_ptr_dtor(&BG(last_http_headers));
BG(page_uid) = -1;
BG(page_gid) = -1;
return SUCCESS;
}
/* }}} */
PHP_MINFO_FUNCTION(basic) /* {{{ */
{
php_info_print_table_start();
BASIC_MINFO_SUBMODULE(dl)
BASIC_MINFO_SUBMODULE(mail)
php_info_print_table_end();
BASIC_MINFO_SUBMODULE(assert)
}
/* }}} */
/* {{{ Given the name of a constant this function will return the constant's associated value */
PHP_FUNCTION(constant)
{
zend_string *const_name;
zval *c;
zend_class_entry *scope;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(const_name)
ZEND_PARSE_PARAMETERS_END();
scope = zend_get_executed_scope();
c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_EXCEPTION);
if (!c) {
RETURN_THROWS();
}
ZVAL_COPY_OR_DUP(return_value, c);
if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) {
RETURN_THROWS();
}
}
}
/* }}} */
/* {{{ Converts a packed inet address to a human readable IP address string */
PHP_FUNCTION(inet_ntop)
{
char *address;
size_t address_len;
int af = AF_INET;
char buffer[40];
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(address, address_len)
ZEND_PARSE_PARAMETERS_END();
#ifdef HAVE_IPV6
if (address_len == 16) {
af = AF_INET6;
} else
#endif
if (address_len != 4) {
RETURN_FALSE;
}
if (!inet_ntop(af, address, buffer, sizeof(buffer))) {
RETURN_FALSE;
}
RETURN_STRING(buffer);
}
/* }}} */
/* {{{ Converts a human readable IP address to a packed binary string */
PHP_FUNCTION(inet_pton)
{
int ret, af = AF_INET;
char *address;
size_t address_len;
char buffer[17];
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(address, address_len)
ZEND_PARSE_PARAMETERS_END();
memset(buffer, 0, sizeof(buffer));
#ifdef HAVE_IPV6
if (strchr(address, ':')) {
af = AF_INET6;
} else
#endif
if (!strchr(address, '.')) {
RETURN_FALSE;
}
ret = inet_pton(af, address, buffer);
if (ret <= 0) {
RETURN_FALSE;
}
RETURN_STRINGL(buffer, af == AF_INET ? 4 : 16);
}
/* }}} */
/* {{{ Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address */
PHP_FUNCTION(ip2long)
{
char *addr;
size_t addr_len;
struct in_addr ip;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(addr, addr_len)
ZEND_PARSE_PARAMETERS_END();
if (addr_len == 0 || inet_pton(AF_INET, addr, &ip) != 1) {
RETURN_FALSE;
}
RETURN_LONG(ntohl(ip.s_addr));
}
/* }}} */
/* {{{ Converts an (IPv4) Internet network address into a string in Internet standard dotted format */
PHP_FUNCTION(long2ip)
{
zend_ulong ip;
zend_long sip;
struct in_addr myaddr;
char str[40];
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(sip)
ZEND_PARSE_PARAMETERS_END();
/* autoboxes on 32bit platforms, but that's expected */
ip = (zend_ulong)sip;
myaddr.s_addr = htonl(ip);
const char* result = inet_ntop(AF_INET, &myaddr, str, sizeof(str));
ZEND_ASSERT(result != NULL);
RETURN_STRING(str);
}
/* }}} */
/********************
* System Functions *
********************/
PHPAPI zend_string *php_getenv(const char *str, size_t str_len) {
#ifdef PHP_WIN32
{
wchar_t *keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P);
if (!keyw) {
return NULL;
}
SetLastError(0);
/* If the given buffer is not large enough to hold the data, the return value is
* the buffer size, in characters, required to hold the string and its terminating
* null character. We use this return value to alloc the final buffer. */
wchar_t dummybuf;
DWORD size = GetEnvironmentVariableW(keyw, &dummybuf, 0);
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
/* The environment variable doesn't exist. */
free(keyw);
return NULL;
}
if (size == 0) {
/* env exists, but it is empty */
free(keyw);
return ZSTR_EMPTY_ALLOC();
}
wchar_t *valw = emalloc((size + 1) * sizeof(wchar_t));
size = GetEnvironmentVariableW(keyw, valw, size);
if (size == 0) {
/* has been removed between the two calls */
free(keyw);
efree(valw);
return ZSTR_EMPTY_ALLOC();
} else {
char *ptr = php_win32_cp_w_to_any(valw);
zend_string *result = zend_string_init(ptr, strlen(ptr), 0);
free(ptr);
free(keyw);
efree(valw);
return result;
}
}
#else
tsrm_env_lock();
/* system method returns a const */
char *ptr = getenv(str);
zend_string *result = NULL;
if (ptr) {
result = zend_string_init(ptr, strlen(ptr), 0);
}
tsrm_env_unlock();
return result;
#endif
}
/* {{{ Get the value of an environment variable or every available environment variable
if no varname is present */
PHP_FUNCTION(getenv)
{
char *str = NULL;
size_t str_len;
bool local_only = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_OR_NULL(str, str_len)
Z_PARAM_BOOL(local_only)
ZEND_PARSE_PARAMETERS_END();
if (!str) {
array_init(return_value);
php_load_environment_variables(return_value);
return;
}
if (!local_only) {
/* SAPI method returns an emalloc()'d string */
char *ptr = sapi_getenv(str, str_len);
if (ptr) {
// TODO: avoid reallocation ???
RETVAL_STRING(ptr);
efree(ptr);
return;
}
}
zend_string *res = php_getenv(str, str_len);
if (res) {
RETURN_STR(res);
}
RETURN_FALSE;
}
/* }}} */
#ifdef HAVE_PUTENV
/* {{{ Set the value of an environment variable */
PHP_FUNCTION(putenv)
{
char *setting;
size_t setting_len;
char *p, **env;
putenv_entry pe;
#ifdef PHP_WIN32
const char *value = NULL;
int error_code;
#endif
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(setting, setting_len)
ZEND_PARSE_PARAMETERS_END();
if (setting_len == 0 || setting[0] == '=') {
zend_argument_value_error(1, "must have a valid syntax");
RETURN_THROWS();
}
pe.putenv_string = zend_strndup(setting, setting_len);
if ((p = strchr(setting, '='))) {
pe.key = zend_string_init(setting, p - setting, 0);
#ifdef PHP_WIN32
value = p + 1;
#endif
} else {
pe.key = zend_string_init(setting, setting_len, 0);
}
tsrm_env_lock();
zend_hash_del(&BG(putenv_ht), pe.key);
/* find previous value */
pe.previous_value = NULL;
for (env = environ; env != NULL && *env != NULL; env++) {
if (!strncmp(*env, ZSTR_VAL(pe.key), ZSTR_LEN(pe.key))
&& (*env)[ZSTR_LEN(pe.key)] == '=') { /* found it */
#ifdef PHP_WIN32
/* must copy previous value because MSVCRT's putenv can free the string without notice */
pe.previous_value = estrdup(*env);
#else
pe.previous_value = *env;
#endif
break;
}
}
#ifdef HAVE_UNSETENV
if (!p) { /* no '=' means we want to unset it */
unsetenv(pe.putenv_string);
}
if (!p || putenv(pe.putenv_string) == 0) { /* success */
#else
# ifndef PHP_WIN32
if (putenv(pe.putenv_string) == 0) { /* success */
# else
wchar_t *keyw, *valw = NULL;
keyw = php_win32_cp_any_to_w(ZSTR_VAL(pe.key));
if (value) {
valw = php_win32_cp_any_to_w(value);
}
/* valw may be NULL, but the failed conversion still needs to be checked. */
if (!keyw || !valw && value) {
tsrm_env_unlock();
free(pe.putenv_string);
zend_string_release(pe.key);
free(keyw);
free(valw);
RETURN_FALSE;
}
error_code = SetEnvironmentVariableW(keyw, valw);
if (error_code != 0
# ifndef ZTS
/* We need both SetEnvironmentVariable and _putenv here as some
dependency lib could use either way to read the environment.
Obviously the CRT version will be useful more often. But
generally, doing both brings us on the safe track at least
in NTS build. */
&& _wputenv_s(keyw, valw ? valw : L"") == 0
# endif
) { /* success */
# endif
#endif
zend_hash_add_mem(&BG(putenv_ht), pe.key, &pe, sizeof(putenv_entry));
#ifdef HAVE_TZSET
if (zend_string_equals_literal_ci(pe.key, "TZ")) {
tzset();
}
#endif
tsrm_env_unlock();
#ifdef PHP_WIN32
free(keyw);
free(valw);
#endif
RETURN_TRUE;
} else {
free(pe.putenv_string);
zend_string_release(pe.key);
#ifdef PHP_WIN32
free(keyw);
free(valw);
#endif
RETURN_FALSE;
}
}
/* }}} */
#endif
/* {{{ free_argv()
Free the memory allocated to an argv array. */
static void free_argv(char **argv, int argc)
{
int i;
if (argv) {
for (i = 0; i < argc; i++) {
if (argv[i]) {
efree(argv[i]);
}
}
efree(argv);
}
}
/* }}} */
/* {{{ free_longopts()
Free the memory allocated to an longopt array. */
static void free_longopts(opt_struct *longopts)
{
opt_struct *p;
if (longopts) {
for (p = longopts; p && p->opt_char != '-'; p++) {
if (p->opt_name != NULL) {
efree((char *)(p->opt_name));
}
}
}
}
/* }}} */
/* {{{ parse_opts()
Convert the typical getopt input characters to the php_getopt struct array */
static int parse_opts(char * opts, opt_struct ** result)
{
opt_struct * paras = NULL;
unsigned int i, count = 0;
unsigned int opts_len = (unsigned int)strlen(opts);
for (i = 0; i < opts_len; i++) {
if ((opts[i] >= 48 && opts[i] <= 57) ||
(opts[i] >= 65 && opts[i] <= 90) ||
(opts[i] >= 97 && opts[i] <= 122)
) {
count++;
}
}
paras = safe_emalloc(sizeof(opt_struct), count, 0);
memset(paras, 0, sizeof(opt_struct) * count);
*result = paras;
while ( (*opts >= 48 && *opts <= 57) || /* 0 - 9 */
(*opts >= 65 && *opts <= 90) || /* A - Z */
(*opts >= 97 && *opts <= 122) /* a - z */
) {
paras->opt_char = *opts;
paras->need_param = *(++opts) == ':';
paras->opt_name = NULL;
if (paras->need_param == 1) {
opts++;
if (*opts == ':') {
paras->need_param++;
opts++;
}
}
paras++;
}
return count;
}
/* }}} */
/* {{{ Get options from the command line argument list */
PHP_FUNCTION(getopt)
{
char *options = NULL, **argv = NULL;
char opt[2] = { '\0' };
char *optname;
int argc = 0, o;
size_t options_len = 0, len;
char *php_optarg = NULL;
int php_optind = 1;
zval val, *args = NULL, *p_longopts = NULL;
zval *zoptind = NULL;
size_t optname_len = 0;
opt_struct *opts, *orig_opts;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STRING(options, options_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(p_longopts)
Z_PARAM_ZVAL(zoptind)
ZEND_PARSE_PARAMETERS_END();
/* Init zoptind to 1 */
if (zoptind) {
ZEND_TRY_ASSIGN_REF_LONG(zoptind, 1);
}
/* Get argv from the global symbol table. We calculate argc ourselves
* in order to be on the safe side, even though it is also available
* from the symbol table. */
if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) &&
((args = zend_hash_find_ex_ind(Z_ARRVAL_P(&PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL ||
(args = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL)
) {
int pos = 0;
zval *entry;
if (Z_TYPE_P(args) != IS_ARRAY) {
RETURN_FALSE;
}
argc = zend_hash_num_elements(Z_ARRVAL_P(args));
/* Attempt to allocate enough memory to hold all of the arguments
* and a trailing NULL */
argv = (char **) safe_emalloc(sizeof(char *), (argc + 1), 0);
/* Iterate over the hash to construct the argv array. */
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), entry) {
zend_string *tmp_arg_str;
zend_string *arg_str = zval_get_tmp_string(entry, &tmp_arg_str);
argv[pos++] = estrdup(ZSTR_VAL(arg_str));
zend_tmp_string_release(tmp_arg_str);
} ZEND_HASH_FOREACH_END();
/* The C Standard requires argv[argc] to be NULL - this might
* keep some getopt implementations happy. */
argv[argc] = NULL;
} else {
/* Return false if we can't find argv. */
RETURN_FALSE;
}
len = parse_opts(options, &opts);
if (p_longopts) {
int count;
zval *entry;
count = zend_hash_num_elements(Z_ARRVAL_P(p_longopts));
/* the first <len> slots are filled by the one short ops
* we now extend our array and jump to the new added structs */
opts = (opt_struct *) safe_erealloc(opts, sizeof(opt_struct), (len + count + 1), 0);
orig_opts = opts;
opts += len;