xref: /netbsd-src/usr.bin/moduli/qsieve/qfile.c (revision e31e6187a943d4eb49f8fa6c83ddec426763b06d)
1*e31e6187Selad /* $NetBSD: qfile.c,v 1.1 2006/01/24 18:59:23 elad Exp $ */
2*e31e6187Selad 
3*e31e6187Selad /*-
4*e31e6187Selad  * Copyright 1994 Phil Karn <karn@qualcomm.com>
5*e31e6187Selad  * Copyright 1996-1998, 2003 William Allen Simpson <wsimpson@greendragon.com>
6*e31e6187Selad  * All rights reserved.
7*e31e6187Selad  *
8*e31e6187Selad  * Redistribution and use in source and binary forms, with or without
9*e31e6187Selad  * modification, are permitted provided that the following conditions
10*e31e6187Selad  * are met:
11*e31e6187Selad  * 1. Redistributions of source code must retain the above copyright
12*e31e6187Selad  *    notice, this list of conditions and the following disclaimer.
13*e31e6187Selad  * 2. Redistributions in binary form must reproduce the above copyright
14*e31e6187Selad  *    notice, this list of conditions and the following disclaimer in the
15*e31e6187Selad  *    documentation and/or other materials provided with the distribution.
16*e31e6187Selad  *
17*e31e6187Selad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*e31e6187Selad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*e31e6187Selad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*e31e6187Selad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*e31e6187Selad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*e31e6187Selad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*e31e6187Selad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*e31e6187Selad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*e31e6187Selad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*e31e6187Selad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*e31e6187Selad  */
28*e31e6187Selad 
29*e31e6187Selad /*
30*e31e6187Selad  * File I/O for qsafe and qsieve.
31*e31e6187Selad  *
32*e31e6187Selad  * 1996 May     William Allen Simpson extracted from earlier code by Phil Karn,
33*e31e6187Selad  * April 1994. save large primes list for later processing. 1998 May
34*e31e6187Selad  * William Allen Simpson parameterized. 2003 Jun     William Allen Simpson
35*e31e6187Selad  * move common file i/o to own file for better documentation.
36*e31e6187Selad  */
37*e31e6187Selad 
38*e31e6187Selad #include <stdio.h>
39*e31e6187Selad #include <stdlib.h>
40*e31e6187Selad #include <time.h>
41*e31e6187Selad #include <openssl/bn.h>
42*e31e6187Selad #include "qfile.h"
43*e31e6187Selad 
44*e31e6187Selad /*
45*e31e6187Selad  * print moduli out in consistent form,
46*e31e6187Selad  * normalizing error returns to printf-like expectations.
47*e31e6187Selad  */
48*e31e6187Selad int
qfileout(FILE * ofile,uint32_t otype,uint32_t otests,uint32_t otries,uint32_t osize,uint32_t ogenerator,BIGNUM * omodulus)49*e31e6187Selad qfileout(FILE * ofile, uint32_t otype, uint32_t otests, uint32_t otries,
50*e31e6187Selad 	 uint32_t osize, uint32_t ogenerator, BIGNUM * omodulus)
51*e31e6187Selad {
52*e31e6187Selad 	struct tm      *gtm;
53*e31e6187Selad 	time_t          time_now;
54*e31e6187Selad 
55*e31e6187Selad 	time(&time_now);
56*e31e6187Selad 	gtm = gmtime(&time_now);
57*e31e6187Selad 
58*e31e6187Selad 	if (0 > fprintf(ofile,
59*e31e6187Selad 			"%04d%02d%02d%02d%02d%02d %u %u %u %u %x ",
60*e31e6187Selad 			gtm->tm_year + 1900,
61*e31e6187Selad 			gtm->tm_mon + 1,
62*e31e6187Selad 			gtm->tm_mday,
63*e31e6187Selad 			gtm->tm_hour,
64*e31e6187Selad 			gtm->tm_min,
65*e31e6187Selad 			gtm->tm_sec,
66*e31e6187Selad 			otype,
67*e31e6187Selad 			otests,
68*e31e6187Selad 			otries,
69*e31e6187Selad 			osize,
70*e31e6187Selad 			ogenerator)) {
71*e31e6187Selad 
72*e31e6187Selad 		return (-1);
73*e31e6187Selad 	}
74*e31e6187Selad 
75*e31e6187Selad 	if (1 > BN_print_fp(ofile, omodulus)) {
76*e31e6187Selad 		return (-1);
77*e31e6187Selad 	}
78*e31e6187Selad 
79*e31e6187Selad 	return (fprintf(ofile, "\n"));
80*e31e6187Selad }
81