-
Notifications
You must be signed in to change notification settings - Fork 5
/
php_big_int.c
2226 lines (1857 loc) · 52.9 KB
/
php_big_int.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 2004, 2005 Alexander Valyalkin
These sources is free software. You can redistribute it and/or
modify it freely. You can use it with any free or commercial
software.
These sources 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.
You may contact the author by:
e-mail: valyala@gmail.com
*************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#if HAVE_BIG_INT
#include "php_big_int.h"
#include "ext/standard/info.h" /* for phpinfo() functions */
#include "big_int_full.h"
#include <stdlib.h> /* for rand() */
#include <time.h> /* for time() */
typedef struct {
big_int *num;
char is_not_ref;
} args_entry;
typedef enum {LEFT, RIGHT} shift_direction;
typedef int (*tri_op_func) (const big_int *, const big_int *, const big_int *, big_int *);
typedef int (*tri_op1_func) (const big_int *, const big_int *, size_t, big_int *);
typedef int (*bin_op_func) (const big_int *, const big_int *, big_int *);
typedef int (*bin_op1_func) (const big_int *, size_t, big_int *);
typedef void (*bin_op2_func) (const big_int *, const big_int *, int *);
typedef int (*un_op_func) (const big_int *, big_int *);
typedef void (*un_op1_func) (const big_int *, unsigned int *);
typedef void (*un_op2_func) (const big_int *, int *);
/* compiled function list so Zend knows what's in this module */
zend_function_entry bi_functions[] =
{
ZEND_FE(bi_from_str, NULL)
ZEND_FE(bi_to_str, NULL)
ZEND_FE(bi_base_convert, NULL)
ZEND_FE(bi_add, NULL)
ZEND_FE(bi_sub, NULL)
ZEND_FE(bi_mul, NULL)
ZEND_FE(bi_div, NULL)
ZEND_FE(bi_mod, NULL)
ZEND_FE(bi_cmp, NULL)
ZEND_FE(bi_cmp_abs, NULL)
ZEND_FE(bi_or, NULL)
ZEND_FE(bi_xor, NULL)
ZEND_FE(bi_and, NULL)
ZEND_FE(bi_andnot, NULL)
ZEND_FE(bi_is_zero, NULL)
ZEND_FE(bi_is_one, NULL)
ZEND_FE(bi_abs, NULL)
ZEND_FE(bi_neg, NULL)
ZEND_FE(bi_inc, NULL)
ZEND_FE(bi_dec, NULL)
ZEND_FE(bi_sqr, NULL)
ZEND_FE(bi_sqrt, NULL)
ZEND_FE(bi_sqrt_rem, NULL)
ZEND_FE(bi_muladd, NULL)
ZEND_FE(bi_bit_len, NULL)
ZEND_FE(bi_bit1_cnt, NULL)
ZEND_FE(bi_addmod, NULL)
ZEND_FE(bi_submod, NULL)
ZEND_FE(bi_mulmod, NULL)
ZEND_FE(bi_divmod, NULL)
ZEND_FE(bi_powmod, NULL)
ZEND_FE(bi_factmod, NULL)
ZEND_FE(bi_absmod, NULL)
ZEND_FE(bi_invmod, NULL)
ZEND_FE(bi_sqrmod, NULL)
ZEND_FE(bi_gcd, NULL)
ZEND_FE(bi_next_prime, NULL)
ZEND_FE(bi_div_extended, NULL)
ZEND_FE(bi_sign, NULL)
ZEND_FE(bi_rand, NULL)
ZEND_FE(bi_lshift, NULL)
ZEND_FE(bi_rshift, NULL)
ZEND_FE(bi_set_bit, NULL)
ZEND_FE(bi_clr_bit, NULL)
ZEND_FE(bi_inv_bit, NULL)
ZEND_FE(bi_test_bit, NULL)
ZEND_FE(bi_scan0_bit, NULL)
ZEND_FE(bi_scan1_bit, NULL)
ZEND_FE(bi_hamming_distance, NULL)
ZEND_FE(bi_subint, NULL)
ZEND_FE(bi_cmpmod, NULL)
ZEND_FE(bi_miller_test, NULL)
ZEND_FE(bi_is_prime, NULL)
ZEND_FE(bi_jacobi, NULL)
ZEND_FE(bi_fact, NULL)
ZEND_FE(bi_pow, NULL)
ZEND_FE(bi_serialize, NULL)
ZEND_FE(bi_unserialize, NULL)
ZEND_FE(bi_gcd_extended, NULL)
ZEND_FE(bi_info, NULL)
{NULL, NULL, NULL}
};
/* compiled module information */
zend_module_entry bi_module_entry =
{
STANDARD_MODULE_HEADER,
BI_MODULE_NAME,
bi_functions,
ZEND_MINIT(bi),
ZEND_MSHUTDOWN(bi),
NULL,
NULL,
ZEND_MINFO(bi),
BI_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
#if defined(COMPILE_DL_BIG_INT)
ZEND_GET_MODULE(bi)
#endif
/* resource type for [big_int] */
static int resource_type;
static int zval_to_big_int(const char *func_name, zval **tmp, args_entry *arg, int arg_pos TSRMLS_DC)
{
int rsrc_type, rsrc_id;
char errbuf[200];
big_int_str s;
if (Z_TYPE_PP(tmp) == IS_RESOURCE) {
arg->is_not_ref = 0; /* arg will not deleted in free_args() */
rsrc_id = Z_LVAL_PP(tmp);
arg->num = (big_int *) zend_list_find(rsrc_id, &rsrc_type);
if (arg->num == NULL) {
snprintf(errbuf, 200, BI_INTERNAL_ERROR);
goto error;
}
if (rsrc_type != resource_type) {
snprintf(errbuf, 200, "%s(): wrong resource type passed for argument number [%d] in function. Expected big_int",
func_name, arg_pos + 1);
goto error;
}
} else {
/* try to convert argument to [string] */
arg->is_not_ref = 1; /* this element will be deleted in free_args() */
arg->num = big_int_create(1);
if (arg->num == NULL) {
snprintf(errbuf, 200, BI_INTERNAL_ERROR);
goto error;
}
if (Z_TYPE_PP(tmp) != IS_STRING) {
SEPARATE_ZVAL(tmp);
convert_to_string(*tmp);
}
s.str = Z_STRVAL_PP(tmp);
s.len = Z_STRLEN_PP(tmp);
switch (big_int_from_str(&s, 10, arg->num)) {
case 0 : break; /* all ok */
case 2 :
snprintf(errbuf, 200, "%s(): argument number [%d] contains illegal chars. It can contain only decimal digits 0-9",
func_name, arg_pos + 1);
goto error;
case 3 :
snprintf(errbuf, 200, "%s(): argument number [%d] cannot be empty",
func_name, arg_pos + 1);
goto error;
default :
snprintf(errbuf, 200, "%s(): cannot convert argument number [%d] to big_int resource",
func_name, arg_pos + 1);
goto error;
}
}
return SUCCESS;
error:
zend_error(E_WARNING, errbuf);
return FAILURE;
}
static int get_func_args(const char *func_name, int args_cnt_min, int args_cnt_max,
int *args_cnt, args_entry *nums TSRMLS_DC)
{
zval **args[BI_MAX_FUNC_ARGS_CNT];
int i = 0;
char errbuf[200];
if (func_name == NULL) {
func_name = "unknown";
}
assert(args_cnt_min <= args_cnt_max);
assert(args_cnt_max <= BI_MAX_FUNC_ARGS_CNT);
*errbuf = '\0'; /* clear error buffer */
if (*args_cnt < args_cnt_min || *args_cnt > args_cnt_max) {
snprintf(errbuf, 200, "%s(): wrong numer of parameters. Function expected from %d to %d parameters",
func_name, args_cnt_min, args_cnt_max);
goto error;
}
/* read parameters of function */
if (zend_get_parameters_array_ex(*args_cnt, args) == FAILURE) {
snprintf(errbuf, 200, "%s(): wrong number of parameters", func_name);
goto error;
}
for (i = 0; i < *args_cnt; i++) {
if (zval_to_big_int(func_name, args[i], &nums[i], i TSRMLS_CC) == FAILURE) {
/* error message is already sent by zval_to_big_int() */
goto error;
}
}
return SUCCESS;
error:
*args_cnt = i;
if (*errbuf) {
zend_error(E_WARNING, errbuf);
}
return FAILURE;
}
static void free_args(args_entry *args, int cnt)
{
int i;
for (i = 0; i < cnt; i++) {
if (args[i].is_not_ref) {
big_int_destroy(args[i].num);
}
}
}
/**
Calculates big_int func_name(big_int a)
*/
static void un_op(const char *func_name, un_op_func func, int err_cnt,
char *const*err, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
big_int *answer = NULL;
int args_cnt;
args_entry args[1] = {0};
int err_no;
args_cnt = ZEND_NUM_ARGS();
if (get_func_args(func_name, 1, 1, &args_cnt, args TSRMLS_CC) == FAILURE) {
goto error;
}
answer = big_int_create(1);
if (answer == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
err_no = func(args[0].num, answer);
if (err_no) {
errstr = (err_no > err_cnt) ? BI_INTERNAL_ERROR : err[err_no - 1];
goto error;
}
free_args(args, 1);
/* register [answer] as resource */
ZEND_REGISTER_RESOURCE(return_value, answer, resource_type);
/* do not free [answer], because it is already registered as resource */
return;
error:
big_int_destroy(answer);
free_args(args, 1);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates int func_name(big_int a)
*/
static void un_op1(const char *func_name, un_op1_func func, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
int args_cnt;
args_entry args[1] = {0};
unsigned int ans;
args_cnt = ZEND_NUM_ARGS();
if (get_func_args(func_name, 1, 1, &args_cnt, args TSRMLS_CC) == FAILURE) {
goto error;
}
func(args[0].num, &ans);
free_args(args, args_cnt);
RETVAL_LONG(ans);
return;
error:
free_args(args, args_cnt);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates bool func_name(big_int a)
(big_int_is_zero, big_int_is_one)
*/
static void un_op2(const char *func_name, un_op2_func func, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
int args_cnt;
args_entry args[1] = {0};
int ans;
args_cnt = ZEND_NUM_ARGS();
if (get_func_args(func_name, 1, 1, &args_cnt, args TSRMLS_CC) == FAILURE) {
goto error;
}
func(args[0].num, &ans);
free_args(args, args_cnt);
RETVAL_LONG(ans);
return;
error:
free_args(args, args_cnt);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates big_int func_name(big_int a, big_int b)
*/
static void bin_op(const char *func_name, bin_op_func func, int err_cnt,
char *const*err, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
big_int *answer = NULL;
int args_cnt;
args_entry args[2] = {0};
int err_no;
args_cnt = ZEND_NUM_ARGS();
if (get_func_args(func_name, 2, 2, &args_cnt, args TSRMLS_CC) == FAILURE) {
goto error;
}
answer = big_int_create(1);
if (answer == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
err_no = func(args[0].num, args[1].num, answer);
if (err_no) {
errstr = (err_no > err_cnt) ? BI_INTERNAL_ERROR : err[err_no - 1];
goto error;
}
free_args(args, 2);
/* register [answer] as resource */
ZEND_REGISTER_RESOURCE(return_value, answer, resource_type);
/* do not free [answer], because it is already registered as resource */
return;
error:
big_int_destroy(answer);
free_args(args, 2);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates big_int func_name(big_int a, int c);
*/
static void bin_op1(const char *func_name, bin_op1_func func, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
int n_bit;
big_int *answer = NULL;
zval *tmp;
args_entry arg = {0};
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &tmp, &n_bit) == FAILURE) {
/* error message was sent by zend_parse_parameters() */
goto error;
}
answer = big_int_create(1);
if (answer == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
if (zval_to_big_int(func_name, &tmp, &arg, 0 TSRMLS_CC) == FAILURE) {
/* error message is already sent by zval_to_big_int() */
goto error;
}
if (n_bit >= 0) {
if (func(arg.num, (size_t) n_bit, answer)) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
}
ZEND_REGISTER_RESOURCE(return_value, answer, resource_type);
free_args(&arg, 1);
/* do not free answer, because it is registered as resource */
return;
error:
big_int_destroy(answer);
free_args(&arg, 1);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates int func_name(big_int a, big_int b);
*/
static void bin_op2(const char *func_name, bin_op2_func func, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
int args_cnt;
args_entry args[2] = {0};
int ans;
args_cnt = ZEND_NUM_ARGS();
if (get_func_args(func_name, 2, 2, &args_cnt, args TSRMLS_CC) == FAILURE) {
goto error;
}
func(args[0].num, args[1].num, &ans);
free_args(args, args_cnt);
RETVAL_LONG(ans);
return;
error:
free_args(args, args_cnt);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates big_int func_name(big_int a, big_int b, big_int c)
*/
static void tri_op(const char *func_name, tri_op_func func, int err_cnt,
char *const*err, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
big_int *answer = NULL;
int args_cnt;
args_entry args[3] = {0};
int err_no;
args_cnt = ZEND_NUM_ARGS();
if (get_func_args(func_name, 3, 3, &args_cnt, args TSRMLS_CC) == FAILURE) {
goto error;
}
answer = big_int_create(1);
if (answer == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
err_no = func(args[0].num, args[1].num, args[2].num, answer);
if (err_no) {
errstr = (err_no > err_cnt) ? BI_INTERNAL_ERROR : err[err_no - 1];
goto error;
}
free_args(args, 3);
/* register [answer] as resource */
ZEND_REGISTER_RESOURCE(return_value, answer, resource_type);
/* do not free [answer], because it is already registered as resource */
return;
error:
big_int_destroy(answer);
free_args(args, 3);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
Calculates big_int func_name(big_int a, big_int b [, int c]);
(big_int_or, big_int_xor, big_int_and, big_int_andnot)
*/
static void tri_op1(const char *func_name, tri_op1_func func, INTERNAL_FUNCTION_PARAMETERS)
{
const char *errstr = NULL;
int start_pos = 0;
big_int *answer = NULL;
zval *tmp[2];
args_entry args[2] = {0};
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &tmp[0], &tmp[1], &start_pos) == FAILURE) {
/* error message was sent by zend_parse_parameters() */
goto error;
}
if (start_pos < 0) {
start_pos = 0;
}
answer = big_int_create(1);
if (answer == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
if (zval_to_big_int(func_name, &tmp[0], &args[0], 0 TSRMLS_CC) == FAILURE) {
/* error message is already sent by zval_to_big_int() */
goto error;
}
if (zval_to_big_int(func_name, &tmp[1], &args[1], 1 TSRMLS_CC) == FAILURE) {
/* error message is already sent by zval_to_big_int() */
goto error;
}
if (func(args[0].num, args[1].num, (size_t) start_pos, answer)) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
ZEND_REGISTER_RESOURCE(return_value, answer, resource_type);
free_args(args, 2);
/* do not free answer, because it is registered as resource */
return;
error:
big_int_destroy(answer);
free_args(args, 2);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
static void do_shift(const char *func_name, shift_direction dir, INTERNAL_FUNCTION_PARAMETERS)
{
bin_op1_func func;
const char *errstr = NULL;
int n_bit;
big_int *answer = NULL;
zval *tmp;
args_entry arg = {0};
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &tmp, &n_bit) == FAILURE) {
/* error message was sent by zend_parse_parameters() */
goto error;
}
answer = big_int_create(1);
if (answer == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
if (zval_to_big_int(func_name, &tmp, &arg, 0 TSRMLS_CC) == FAILURE) {
/* error message is already sent by zval_to_big_int() */
goto error;
}
switch (dir) {
case RIGHT:
func = big_int_rshift;
break;
case LEFT:
func = big_int_lshift;
break;
default:
errstr = BI_INTERNAL_ERROR;
goto error;
}
if (func(arg.num, n_bit, answer)) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
ZEND_REGISTER_RESOURCE(return_value, answer, resource_type);
free_args(&arg, 1);
/* do not free answer, because it is registered as resource */
return;
error:
big_int_destroy(answer);
free_args(&arg, 1);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/*******************************************************************/
static void bi_destruction_handler(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
/* free allocated memory */
big_int_destroy((big_int *) rsrc->ptr);
}
ZEND_MINIT_FUNCTION(bi)
{
time_t t;
/* register big_int resource type */
resource_type = zend_register_list_destructors_ex(bi_destruction_handler, NULL, BI_RESOURCE_NAME, module_number);
/* seed pseudorandom generator */
t = time(NULL);
srand((unsigned) t);
return SUCCESS;
}
ZEND_MSHUTDOWN_FUNCTION(bi)
{
return SUCCESS;
}
ZEND_MINFO_FUNCTION(bi)
{
php_info_print_table_start();
php_info_print_table_row(2, "big_int support", "enabled");
php_info_print_table_row(2, "big_int library version", BIG_INT_VERSION);
php_info_print_table_row(2, "php module version", BI_VERSION);
php_info_print_table_end();
}
/*******************************************************************/
/**
resource bi_from_str(string number [, int base])
Creates big_int number from string [number].
[base] - is base of number. It can be from 2 to 36 inclusive
Default value of [base] is 10.
*/
ZEND_FUNCTION(bi_from_str)
{
char *str = NULL;
int str_len;
int base;
big_int_str s;
big_int *num = NULL;
const char *errstr = NULL;
/* read parameters of function */
base = 10;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l",
&str, &str_len, &base) == FAILURE) {
/* error message was sent by zend_parse_parameters() */
goto error;
}
/* initialize [num] */
num = big_int_create(1);
if (num == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
/* convert string to big_int */
s.str = str;
s.len = str_len;
switch (big_int_from_str(&s, base, num)) {
case 0: break; /* no errors */
case 1: /* wrong base */
errstr = "bi_from_str(): wrong [base]. It can be from 2 to 36 inclusive";
goto error;
case 2: /* string contains wrong chars for chosen base */
errstr = "bi_from_str(): string contains wrong chars for chosen base";
goto error;
case 3:
errstr = "bi_from_str(): length of the string must be greater than 0";
goto error;
default:
errstr = BI_INTERNAL_ERROR;
goto error;
}
/* register [num] as resource */
ZEND_REGISTER_RESOURCE(return_value, num, resource_type);
/* do not free [num], because it is already registered as resource */
return;
error:
/* free allocated memory and print error */
big_int_destroy(num);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
str bi_to_str(resource number [, int base])
Converts [num] to string with base [base].
[base] - is base of number. It can be from 2 to 36 inclusive
Default value of [base] is 10.
*/
ZEND_FUNCTION(bi_to_str)
{
zval *tmp = NULL;
int base;
big_int_str *s_ptr = NULL;
const char *errstr = NULL;
args_entry arg = {0};
/* read parameters of function */
base = 10;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l",
&tmp, &base) == FAILURE) {
/* error message was sent by zend_parse_parameters() */
goto error;
}
/* initialize [s_ptr] */
s_ptr = big_int_str_create(1);
if (s_ptr == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
if (zval_to_big_int("bi_to_str", &tmp, &arg, 0 TSRMLS_CC) == FAILURE) {
/* error message is already sent by zval_to_big_int() */
goto error;
}
switch (big_int_to_str(arg.num, base, s_ptr)) {
case 0: break; /* no errors */
case 1: /* wrong base */
errstr = "bi_to_str(): wrong [base]. It can be from 2 to 36 inclusive";
goto error;
default:
errstr = BI_INTERNAL_ERROR;
goto error;
}
RETVAL_STRINGL(s_ptr->str, (int) s_ptr->len, 1);
/* free allocated memory */
free_args(&arg, 1);
big_int_str_destroy(s_ptr);
return;
error:
/* free allocated memory and print error */
free_args(&arg, 1);
big_int_str_destroy(s_ptr);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
string bi_base_convert(string number, int base_from, int base_to)
Returns a string containing number represented in base [base_to].
[base_from] - is base of [number].
[base_from] and [base_to] can be from 2 to 36 inclusive.
*/
ZEND_FUNCTION(bi_base_convert)
{
char *str = NULL;
int str_len;
int base_from, base_to;
big_int_str s, *s_ptr = NULL;
const char *errstr = NULL;
/* read parameters of function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll",
&str, &str_len, &base_from, &base_to) == FAILURE) {
/* error message was sent by zend_parse_parameters() */
goto error;
}
/* initialize [s_ptr] */
s_ptr = big_int_str_create(1);
if (s_ptr == NULL) {
errstr = BI_INTERNAL_ERROR;
goto error;
}
/* call big_int_base_convert() */
s.str = str;
s.len = str_len;
switch (big_int_base_convert(&s, s_ptr, base_from, base_to)) {
case 0: break; /* no errors */
case 1: /* wrong base_from */
errstr = "bi_base_convert(): wrong [base_from]. It can be from 2 to 36 inclusive";
goto error;
case 2: /* wrong base_to */
errstr = "bi_base_convert(): wrong [base_to]. It can be from 2 to 36 inclusive";
goto error;
case 3: /* string contains wrong chars for chosen base */
errstr = "bi_base_convert(): string contains wrong chars for [base_from]";
goto error;
case 4:
errstr = "bi_base_convert(): length of the string must be greater than 0";
goto error;
default:
errstr = BI_INTERNAL_ERROR;
goto error;
}
/* do not free [num], because it is already registered as resource */
RETVAL_STRINGL(s_ptr->str, (int) s_ptr->len, 1);
/* free allocated memory */
big_int_str_destroy(s_ptr);
return;
error:
/* free allocated memory and print error */
big_int_str_destroy(s_ptr);
if (errstr != NULL) {
zend_error(E_WARNING, errstr);
}
RETVAL_NULL();
}
/**
resource bi_add(resource a, resource b)
Calculates a + b.
*/
ZEND_FUNCTION(bi_add)
{
bin_op("bi_add", big_int_add, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_sub(resource a, resource b)
Calculates a - b.
*/
ZEND_FUNCTION(bi_sub)
{
bin_op("bi_sub", big_int_sub, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_mul(resource a, resource b)
Calculates a * b.
*/
ZEND_FUNCTION(bi_mul)
{
bin_op("bi_mul", big_int_mul, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_div(resource a, resource b)
Calculates a / b.
*/
ZEND_FUNCTION(bi_div)
{
char *err[] = {
"bi_div(): division by zero",
};
bin_op("bi_div", big_int_div, 1, err, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_mod(resource a, resource b)
Calculates a % b.
*/
ZEND_FUNCTION(bi_mod)
{
char *err[] = {
"bi_mod(): division by zero",
};
bin_op("bi_mod", big_int_mod, 1, err, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_or(resource a, resource b [, int start_pos])
Calculates a or b, starting with start_pos.
*/
ZEND_FUNCTION(bi_or)
{
tri_op1("bi_or", big_int_or, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_and(resource a, resource b [, int start_pos])
Calculates a and b, starting with start_pos.
*/
ZEND_FUNCTION(bi_and)
{
tri_op1("bi_and", big_int_and, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_xor(resource a, resource b [, int start_pos])
Calculates a xor b, starting with start_pos.
*/
ZEND_FUNCTION(bi_xor)
{
tri_op1("bi_xor", big_int_xor, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_andnot(resource a, resource b [, int start_pos])
Calculates a andnot b, starting with start_pos.
*/
ZEND_FUNCTION(bi_andnot)
{
tri_op1("bi_andnot", big_int_andnot, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_abs(resource a)
Calculates abs(a).
*/
ZEND_FUNCTION(bi_abs)
{
un_op("bi_abs", big_int_abs, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_neg(resource a)
Calculates neg(a).
*/
ZEND_FUNCTION(bi_neg)
{
un_op("bi_neg", big_int_neg, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_inc(resource a)
Calculates inc(a).
*/
ZEND_FUNCTION(bi_inc)
{
un_op("bi_inc", big_int_inc, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_dec(resource a)
Calculates dec(a).
*/
ZEND_FUNCTION(bi_dec)
{
un_op("bi_dec", big_int_dec, 0, NULL, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/**
resource bi_sqr(resource a)