chiark / gitweb /
281902fd1b7579b651af14be41acd4f5967bcc26
[become] / src / tx.c
1 /* -*-c-*-
2  *
3  * $Id: tx.c,v 1.3 1998/01/12 16:46:31 mdw Exp $
4  *
5  * Transfer for keys and other large integers
6  *
7  * (c) 1998 Mark Wooding
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of `become'
13  *
14  * `Become' is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * `Become' 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 General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with `become'; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: tx.c,v $
32  * Revision 1.3  1998/01/12 16:46:31  mdw
33  * Fix copyright date.
34  *
35  * Revision 1.2  1997/08/04 10:24:25  mdw
36  * Sources placed under CVS control.
37  *
38  * Revision 1.1  1997/07/21  13:47:43  mdw
39  * Initial revision
40  *
41  */
42
43 /*----- Header files ------------------------------------------------------*/
44
45 /* --- ANSI headers --- */
46
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 /* --- Local headers --- */
53
54 #include "config.h"
55 #include "tx.h"
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 /* --- @tx_getBits@ --- *
60  *
61  * Arguments:   @unsigned char *k@ = pointer to key array to unpack into
62  *              @size_t sz@ = number of bits to read (elements in array)
63  *              @FILE *fp@ = stream to read from
64  *
65  * Returns:     ---
66  *
67  * Use:         Reads a number of bits into an array.  The least significant
68  *              bits of the final word are cleared to zero.
69  */
70
71 void tx_getBits(unsigned char *k, size_t sz, FILE *fp)
72 {
73   int i = 0;
74   unsigned char a = 0;
75   unsigned int ch;
76   size_t wsz = (size_t)((sz + 7ul) & ~7ul);
77   int t;
78
79   while ((t = getc(fp)) != EOF) {
80
81     /* --- Allow separators for readbility --- */
82
83     if (t == '-')
84       continue;
85
86     /* --- Standard converting-from-hex-digit code --- *
87      *
88      * Assumes that 'a'--'f' and 'A'--'F' are contiguous and in order, in
89      * addition to the guarantee that '0'--'9' are like this.  The assumption
90      * is true in ASCII (and character sets based thereon) and EBCDIC.
91      */
92
93     ch = (unsigned)t;
94     ch -= '0';
95     if (ch > 9) ch -= 'A' - '0' - 10;
96     if (ch > 15) ch -= 'a' - 'A';
97     if (ch > 15) break;
98
99     /* --- Accumulate and maybe store --- */
100
101     a = (a << 4) | ch;
102     i++;
103     sz -= 4, wsz -= 4;
104     if ((i & 1) == 0)
105       *k++ = a, a = 0;
106     if (!sz)
107       break;
108   }
109
110   /* --- Pad the rest out with zeros --- */
111
112   while (wsz) {
113     a <<= 4;
114     i++;
115     wsz -= 4;
116     if ((i & 1) == 0)
117       *k++ = a, a = 0;
118   }
119 }
120
121 /* --- @tx_putBits@ --- *
122  *
123  * Arguments:   @unsigned char *k@ = pointer to key block
124  *              @size_t sz@ = number of bits to write
125  *              @FILE *fp@ = pointer to stream to write on
126  *
127  * Returns:     ---
128  *
129  * Use:         Complements @tx_getBits@ above.  Writes a number of bits
130  *              to a file in an easy-to-read and transportable format (hex!)
131  */
132
133 void tx_putBits(unsigned char *k, size_t sz, FILE *fp)
134 {
135   const static char hex[16] = "0123456789abcdef";
136   size_t dash;
137   size_t d;
138   unsigned char i;
139
140   /* --- Don't do anything unless we have to --- */
141
142   if (!sz)
143     return;
144
145   /* --- Now decide where to `dash' the output --- */
146
147   if (sz % 32 == 0)
148     dash = 4;
149   else if (sz % 40 == 0)
150     dash = 5;
151   else
152     dash = 0;
153
154   /* --- Start writing values out --- */
155
156   d = dash;
157
158   for (;;) {
159
160     /* --- Write next byte out --- */
161
162     i = *k++;
163     putc(hex[(i >> 4) & 0x0fu], fp);
164     putc(hex[(i >> 0) & 0x0fu], fp);
165
166     /* --- If done, stop now --- */
167
168     if (sz -= 8, sz == 0)
169       break;
170
171     /* --- If need a dash, print one --- */
172
173     if (!--d) {
174       putc('-', fp);
175       d = dash;
176     }
177   }
178
179   /* --- Print the final newline --- */
180
181   fputc('\n', fp);
182 }
183
184 /*----- That's all, folks -------------------------------------------------*/