Renamed from `rsa-decrypt', since the name was no longer appropriate.
[u/mdw/catacomb] / genprimes.c
CommitLineData
8ecfe0dc 1/* -*-c-*-
2 *
2791153f 3 * $Id: genprimes.c,v 1.2 1999/12/22 15:48:39 mdw Exp $
8ecfe0dc 4 *
5 * Generate prime number table
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: genprimes.c,v $
2791153f 33 * Revision 1.2 1999/12/22 15:48:39 mdw
34 * Rename output file. Make output constants unsigned.
35 *
8ecfe0dc 36 * Revision 1.1 1999/11/19 13:19:37 mdw
37 * Generate small primes table.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include <ctype.h>
44#include <errno.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48
49#include <mLib/darray.h>
50#include <mLib/dstr.h>
51#include <mLib/mdwopt.h>
52#include <mLib/quis.h>
53#include <mLib/report.h>
54
55/*----- Data structures ---------------------------------------------------*/
56
57DA_DECL(intv, int);
58
59/*----- Main code ---------------------------------------------------------*/
60
61int main(int argc, char *argv[])
62{
63 int p_max = 0, p_n = 0;
64 char *type = "unsigned int";
2791153f 65 char *header = "primetab.h";
66 char *source = "primetab.c";
67 char *name = "primetab";
8ecfe0dc 68 intv p = DA_INIT;
69 int i;
70
71 ego(argv[0]);
72
73 for (;;) {
74 int i = getopt(argc, argv, "h:c:i:n:m:t:");
75 if (i < 0)
76 break;
77 switch (i) {
78 case 'h':
79 header = optarg;
80 break;
81 case 'c':
82 source = optarg;
83 break;
84 case 'i':
85 name = optarg;
86 break;
87 case 'n':
88 p_max = 0;
89 p_n = atoi(optarg);
90 break;
91 case 'm':
92 p_n = 0;
93 p_max = atoi(optarg);
94 break;
95 case 't':
96 type = optarg;
97 break;
98 default:
99 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
100 exit(EXIT_FAILURE);
101 }
102 }
103
104 if (!p_max && !p_n)
105 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
106
107 if (p_max >= 2)
108 DA_PUSH(&p, 2);
109 for (i = 3; (!p_max && !p_n) ||
110 (p_n && DA_LEN(&p) < p_n) ||
111 (p_max && i <= p_max);
112 i += 2) {
113 int j;
114 for (j = 0; j < DA_LEN(&p); j++) {
115 if (i % DA(&p)[j] == 0)
116 goto composite;
117 }
118 DA_PUSH(&p, i);
119 composite:;
120 }
121
122 {
123 FILE *fp = fopen(header, "w");
124 dstr d = DSTR_INIT;
125 char *q;
126 if (!fp)
127 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
128 for (q = header; *q; q++) {
129 int ch = (unsigned char)*q;
130 if (isalnum(ch))
131 ch = toupper(ch);
132 else
133 ch = '_';
134 DPUTC(&d, ch);
135 }
136 DPUTZ(&d);
137 fprintf(fp, "\
138/* -*-c-*-\n\
139 *\n\
140 * Table of small prime numbers [generated]\n\
141 */\n\
142\n\
143#ifndef %s\n\
144#define %s\n\
145\n\
2791153f 146#define NPRIME %luu\n\
147#define MAXPRIME %uu\n\
8ecfe0dc 148\n\
149extern %s %s[];\n\
150\n\
151#endif\n\
152",
153 d.buf, d.buf,
154 (unsigned long)DA_LEN(&p),
155 DA(&p)[DA_LEN(&p) - 1],
156 type, name);
157 dstr_destroy(&d);
158 if (fclose(fp) == EOF) {
159 remove(header);
160 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
161 }
162 }
163
164 {
165 FILE *fp = fopen(source, "w");
166 int i;
167 if (!fp)
168 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
169 fprintf(fp, "\
170/* -*-c-*-\n\
171 *\n\
172 * Table of small prime numbers [generated]\n\
173 */\n\
174\n\
175#include \"%s\"\n\
176\n\
177%s %s[] = {",
178 header, type, name);
179 for (i = 0; i < DA_LEN(&p); i++) {
180 if (i % 8 == 0)
181 fputs("\n ", fp);
182 fprintf(fp, "%5i, ", DA(&p)[i]);
183 }
184 fputs("\n\
185};\n\
186", fp);
187 if (fclose(fp) == EOF) {
188 remove(source);
189 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
190 }
191 }
192
193 return (0);
194}
195
196/*----- That's all, folks -------------------------------------------------*/