1 |
dashley |
172 |
//$Header$ |
2 |
|
|
//******************************************************************************** |
3 |
dashley |
175 |
//Copyright (c) 2003, 2018 David T. Ashley. |
4 |
dashley |
172 |
//******************************************************************************** |
5 |
dashley |
175 |
//This file is part of "arith_large_cgi", a program that is designed to be |
6 |
|
|
//invoked by a PHP script as part of serving a web page that performs |
7 |
|
|
//calculations involving large integers. (A secondary compiled program is |
8 |
|
|
//used because a compiled program can perform certain calculation-intensive |
9 |
|
|
//tasks far more efficiently than a PHP script.) This program is provided by |
10 |
|
|
//David T. Ashley (dashley@gmail.com) under the MIT License (reproduced |
11 |
|
|
//immediately below). |
12 |
dashley |
172 |
//******************************************************************************** |
13 |
dashley |
175 |
//Permission is hereby granted, free of charge, to any person obtaining a copy |
14 |
|
|
//of this software and associated documentation files (the "Software"), to deal |
15 |
|
|
//in the Software without restriction, including without limitation the rights |
16 |
|
|
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17 |
|
|
//copies of the Software, and to permit persons to whom the Software is |
18 |
|
|
//furnished to do so, subject to the following conditions: |
19 |
dashley |
172 |
// |
20 |
dashley |
175 |
//The above copyright notice and this permission notice shall be included in all |
21 |
|
|
//copies or substantial portions of the Software. |
22 |
dashley |
172 |
// |
23 |
dashley |
175 |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24 |
|
|
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25 |
|
|
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26 |
|
|
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27 |
|
|
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28 |
|
|
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29 |
|
|
//SOFTWARE. |
30 |
dashley |
172 |
//******************************************************************************** |
31 |
|
|
//This program is written exclusively to support a PHP script which must calculate and |
32 |
|
|
//display a number of number theory results. The necessity of this script |
33 |
|
|
//(rather than using BCMATH) comes about because SourceForge does not have |
34 |
|
|
//bcmath installed on its PHP system. |
35 |
|
|
// |
36 |
|
|
//This program is meant to be called by the PHP script using the exec() |
37 |
|
|
//call, but could also be run by hand (and in fact this method is used for testing). |
38 |
|
|
// |
39 |
|
|
//The first parameter after the program name is the suboperation to be performed |
40 |
|
|
//(each CGI page probably uses a different suboperation). This program |
41 |
|
|
//will return a non-zero exit code only if there is a missing or unrecognized |
42 |
|
|
//suboperation. Otherwise, error reporting is controlled by the rules of the |
43 |
|
|
//suboperation. |
44 |
|
|
// |
45 |
|
|
//The currently defined suboperations are: |
46 |
|
|
// |
47 |
|
|
// a)"help" |
48 |
|
|
// Display information about what the program is and how to use |
49 |
|
|
// it. |
50 |
|
|
// b)"gcd" |
51 |
|
|
// Calculate the gcd of two integers using Euclid's classic |
52 |
|
|
// algorithm and also display the intermediate results. |
53 |
|
|
// c)"gmp_prob_prime" |
54 |
|
|
// Use Miller-Rabin to determine to high certainty whther a |
55 |
|
|
// number is prime or composite. |
56 |
|
|
// d)"pfact_18" |
57 |
|
|
// Attempt to factor a number of 18 decimal digits or less into |
58 |
|
|
// component primes. |
59 |
|
|
// e)"cfbrap" |
60 |
|
|
// Finds the best rational approximation to a rational number subject |
61 |
|
|
// to constraints on the denominator and numerator using continued |
62 |
|
|
// fraction techniques. |
63 |
|
|
|
64 |
|
|
#include <stdio.h> |
65 |
|
|
#include <stdlib.h> |
66 |
|
|
#include <string.h> |
67 |
|
|
|
68 |
|
|
#include "auxfuncs.h" |
69 |
|
|
#include "subfunc_cfbrap.h" |
70 |
|
|
#include "subfunc_gcd.h" |
71 |
|
|
#include "subfunc_gmp_prob_prime.h" |
72 |
|
|
#include "subfunc_pfact_18.h" |
73 |
|
|
|
74 |
|
|
//Single function prototype needed for a forward reference. |
75 |
|
|
static void dump_subfunction_choices(int indent); |
76 |
|
|
|
77 |
|
|
//Implements the help. |
78 |
|
|
int SUBFUNC_HELP_main(int argc, char *argv[]) |
79 |
|
|
{ |
80 |
|
|
int i; |
81 |
|
|
char *help[] = |
82 |
|
|
{ |
83 |
|
|
"This program is a compiled 'C'-language program designed to be called from a", |
84 |
|
|
"PHP script to assist in calculating number theory and large integer", |
85 |
|
|
"arithmetic results. This scheme (of having a separate executable like this", |
86 |
|
|
"one) was used with SourceForge web content because SourceForge did not have", |
87 |
|
|
"the bcmath library compiled into PHP, and it was determined experimentally", |
88 |
|
|
"that writing large integer arithmetic functions in PHP directly resulted in", |
89 |
|
|
"unusably slow performance. This program makes use of the GMP library, which", |
90 |
|
|
"gives it superior performance.", |
91 |
|
|
"", |
92 |
|
|
"This program can of course be invoked from a shell or from another scripting", |
93 |
|
|
"language besides PHP. It is designed, however, for execution from a", |
94 |
|
|
"scripting language, as you might guess from the human-unfriendly output.", |
95 |
|
|
"", |
96 |
|
|
"Please contact Dave Ashley (dtashley@aol.com) with any questions or", |
97 |
|
|
"concerns.", |
98 |
|
|
"", |
99 |
|
|
"Dave Ashley, Detroit, Michigan, USA, April, 2003.", |
100 |
|
|
"", |
101 |
|
|
"The available subfunction choices (the first parameter on the command", |
102 |
|
|
"line) are:", |
103 |
|
|
}; |
104 |
|
|
|
105 |
|
|
for (i=0; i<sizeof(help)/sizeof(help[0]); i++) |
106 |
|
|
printf("%s\n", help[i]); |
107 |
|
|
dump_subfunction_choices(3); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
//Structure type used to hold the jump table of different functions |
111 |
|
|
//to handle different subcommands. |
112 |
|
|
struct struct_ARITH_LARGE_CGI_subcmd_jmp |
113 |
|
|
{ |
114 |
|
|
char *subfunc_string; |
115 |
|
|
int (*subfunc_ptr)(int argc, char *argv[]); |
116 |
|
|
}; |
117 |
|
|
|
118 |
|
|
//The jump table. The last element is defined to have both a |
119 |
|
|
//NULL string pointer and a NULL function pointer. |
120 |
|
|
static struct struct_ARITH_LARGE_CGI_subcmd_jmp subcmd_jump_tbl[] = |
121 |
|
|
{ |
122 |
|
|
{"cfbrap", SUBFUNC_CFBRAP_main }, |
123 |
|
|
{"gcd", SUBFUNC_GCD_main }, |
124 |
|
|
{"gmp_prob_prime", SUBFUNC_GMP_PROB_PRIME_main }, |
125 |
|
|
{"pfact_18", SUBFUNC_PFACT_18_main }, |
126 |
|
|
{"help", SUBFUNC_HELP_main }, |
127 |
|
|
{NULL, NULL } |
128 |
|
|
}; |
129 |
|
|
|
130 |
|
|
//Dumps the available choices for the subfunction code to the |
131 |
|
|
//standard output, each indented by "indent" characters. |
132 |
|
|
static void dump_subfunction_choices(int indent) |
133 |
|
|
{ |
134 |
|
|
int i, j; |
135 |
|
|
|
136 |
|
|
for (i=0; subcmd_jump_tbl[i].subfunc_string != NULL; i++) |
137 |
|
|
{ |
138 |
|
|
for (j=0; j<indent; j++) |
139 |
|
|
printf(" "); |
140 |
|
|
printf("%s\n", subcmd_jump_tbl[i].subfunc_string); |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
int main(int argc, char *argv[]) |
146 |
|
|
{ |
147 |
|
|
int i; |
148 |
dashley |
177 |
int rv = 0; |
149 |
dashley |
172 |
|
150 |
|
|
if (argc < 2) |
151 |
|
|
{ |
152 |
|
|
printf("Missing subfunction code. Choices are:\n"); |
153 |
|
|
dump_subfunction_choices(3); |
154 |
|
|
exit(4); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
for (i=0; subcmd_jump_tbl[i].subfunc_string != NULL; i++) |
158 |
|
|
{ |
159 |
|
|
if (!strcmp(argv[1], subcmd_jump_tbl[i].subfunc_string)) |
160 |
|
|
{ |
161 |
|
|
rv = (subcmd_jump_tbl[i].subfunc_ptr)(argc, argv); |
162 |
|
|
goto normal_return; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
printf("Invalid subfunction code. Choices are:\n"); |
167 |
|
|
dump_subfunction_choices(3); |
168 |
|
|
exit(4); |
169 |
|
|
|
170 |
|
|
normal_return: |
171 |
|
|
exit(rv); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
//******************************************************************************** |
175 |
|
|
//End of file ARITH_LARGE_CGI.C. |
176 |
|
|
//******************************************************************************** |