3 * $Id: tx.c,v 1.3 1998/01/12 16:46:31 mdw Exp $
5 * Transfer for keys and other large integers
7 * (c) 1998 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1998/01/12 16:46:31 mdw
35 * Revision 1.2 1997/08/04 10:24:25 mdw
36 * Sources placed under CVS control.
38 * Revision 1.1 1997/07/21 13:47:43 mdw
43 /*----- Header files ------------------------------------------------------*/
45 /* --- ANSI headers --- */
52 /* --- Local headers --- */
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @tx_getBits@ --- *
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
67 * Use: Reads a number of bits into an array. The least significant
68 * bits of the final word are cleared to zero.
71 void tx_getBits(unsigned char *k
, size_t sz
, FILE *fp
)
76 size_t wsz
= (size_t)((sz
+ 7ul) & ~7ul);
79 while ((t
= getc(fp
)) != EOF
) {
81 /* --- Allow separators for readbility --- */
86 /* --- Standard converting-from-hex-digit code --- *
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.
95 if (ch
> 9) ch
-= 'A' - '0' - 10;
96 if (ch
> 15) ch
-= 'a' - 'A';
99 /* --- Accumulate and maybe store --- */
110 /* --- Pad the rest out with zeros --- */
121 /* --- @tx_putBits@ --- *
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
129 * Use: Complements @tx_getBits@ above. Writes a number of bits
130 * to a file in an easy-to-read and transportable format (hex!)
133 void tx_putBits(unsigned char *k
, size_t sz
, FILE *fp
)
135 const static char hex
[16] = "0123456789abcdef";
140 /* --- Don't do anything unless we have to --- */
145 /* --- Now decide where to `dash' the output --- */
149 else if (sz
% 40 == 0)
154 /* --- Start writing values out --- */
160 /* --- Write next byte out --- */
163 putc(hex
[(i
>> 4) & 0x0fu
], fp
);
164 putc(hex
[(i
>> 0) & 0x0fu
], fp
);
166 /* --- If done, stop now --- */
168 if (sz
-= 8, sz
== 0)
171 /* --- If need a dash, print one --- */
179 /* --- Print the final newline --- */
184 /*----- That's all, folks -------------------------------------------------*/