Initial checkin.
3 Copyright (c) 2000 Oren Tirosh <oren@hishome.net>
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 * Calculate the number of words required to encode data using mnemonic
37 * size - Size in bytes of data to be encoded
40 * number of words required for the encoding
44 mn_words_required (int size)
46 return ((size + 1) * 3) / 4;
51 * mn_encode_word_index
54 * Perform one step of encoding binary data into words. Returns word index.
57 * src - Pointer to data buffer to encode
58 * srcsize - Size in bytes of data to encode
59 * n - Sequence number of word to encode
60 * 0 <= n < mn_words_required(srcsize)
63 * 0 - no more words to encode / n is out of range
64 * 1..MN_WORDS - word index. May be used as index to the mn_words[] array
67 mn_index mn_encode_word_index (void *src, int srcsize, int n)
69 mn_word32 x = 0; /* Temporary for MN_BASE arithmetic */
70 int offset; /* Offset into src */
71 int remaining; /* Octets remaining to end of src */
72 int extra = 0; /* Index 7 extra words for 24 bit data */
75 if (n < 0 || n >= mn_words_required (srcsize))
76 return 0; /* word out of range */
77 offset = (n / 3) * 4; /* byte offset into src */
78 remaining = srcsize - offset;
83 for (i = 0; i < remaining; i++)
84 x |= ((mn_byte *) src)[offset + i] << (i * 8); /* endianness-agnostic */
88 case 2: /* Third word of group */
89 if (remaining == 3) /* special case for 24 bits */
90 extra = MN_BASE; /* use one of the 7 3-letter words */
91 x /= (MN_BASE * MN_BASE);
93 case 1: /* Second word of group */
96 return x % MN_BASE + extra + 1;
104 * Perform one step of encoding binary data into words. Returns pointer
108 * src - Pointer to data buffer to encode
109 * srcsize - Size of data to encode in bytes
110 * n - Sequence number of word to encode.
111 * 0 <= n < mn_words_required(srcsize)
114 * NULL - no more words to encode / n is out of range
115 * valid pointer - pointer to null-terminated lowercase word. length<=7
119 mn_encode_word (void *src, int srcsize, int n)
121 return mn_words[mn_encode_word_index (src, srcsize, n)];
127 * Utility function - returns nonzero if character c is an ASCII letter.
133 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
140 * Perform one step of decoding a null-terminated buffer into word indices.
141 * A word is defined as a sequence of letter character separated by one
142 * or more non-letter separator characters.
145 * ptr - Pointer to a pointer to the next character in the buffer.
146 * *ptr is modified by the function; see Return Value below.
149 * 0 - If *ptr==0 (points to the null at the end of the buffer) no more
150 * words were found in the buffer. Otherwise *ptr points to beginning
151 * of an unrecognized word.
152 * >0 - index of word found, suitable for decoding with mn_decode_word_index
153 * or comparison to values returned from mn_encode_index. *ptr points
154 * to first character of next word or to terminating null.
158 mn_next_word_index (char **ptr)
161 char wordbuf[MN_WORD_BUFLEN];
166 while (**ptr && !isletter (**ptr)) /* skip separator chars */
168 wordstart = *ptr; /* save for error reporting */
169 while (**ptr && isletter (**ptr) && i < MN_WORD_BUFLEN - 1)
172 if (c >= 'A' && c <= 'Z')
173 c += 'a' - 'A'; /* convert to lowercase */
177 while (**ptr && isletter (**ptr)) /* skip tail of long words */
179 while (**ptr && !isletter (**ptr)) /* skip separators */
182 if (wordbuf[0] == '\0')
183 return 0; /* EOF, no word found */
185 for (idx = 1; idx <= MN_WORDS; idx++)
187 if (!strcmp (wordbuf, mn_words[idx]))
189 /* FIXME: some fancy code should go here
190 to accept misspellings and soundalikes.
191 (replacing the linear search would also be nice) */
194 return 0; /* not found */
199 * mn_decode_word_index
202 * Perform one step of decoding a sequence of words into binary data.
205 * index - Index of word, e.g. return value of mn_next_word_index. Use
206 * the value MN_EOF(=0) to signal the end of input.
207 * dest - Points to buffer to receive decoded binary result.
208 * destsize - Size of buffer
209 * offset - Pointer to an integer offset into the destination buffer for
210 * next data byte. Initialize *offset to 0 before first call to
211 * function. Modified by function and may be used as an
212 * indication for the amount of data actually decoded.
215 * The return value indicates the status of the decoding function. It is
216 * ok to ignore this value on all calls to the function except the last
217 * one (with index=MN_EOF). Any errors encountered will be reported on.
218 * the last call. The error code is also returned in *offset (negative
219 * values indicate error).
222 * for index!=MN_EOF a return value of MN_OK means that
223 * decoding has been successful so far.
224 * for index==MN_EOF a return value of MN_OK means that decoding
225 * of the entire buffer has been successful and the decoder is in
226 * a valid state for the end of the message. A total of *offset
227 * valid decoded bytes is in the buffer.
229 * returned on MN_EOF when an unaccounted arithmetic remainder is
230 * in the decoder. Most likely indicates a truncated word sequence.
232 * Not enough room in buffer for decoded data.
234 * Returned when decoding of data is attempted after decoding one
235 * of the 7 words reserved for 24 bit remainders at the end of the
236 * message. Probably indicates a garbled messages.
238 * Bad input index. Naturally this should not happen when using
239 * the result of mn_next_word_index.
241 * Returned when one of the 7 words reserved for 24 bit remainders
242 * is received at an offset inappropriate for a 24 bit remainder.
244 * Indicates an overflow in MN_BASE arithmetic. Approximately 0.09%
245 * of the 3 word combinations are unused and will generate this error.
249 mn_decode_word_index (mn_index index, void *dest, int destsize, int *offset)
251 mn_word32 x; /* Temporary for MN_BASE arithmetic */
255 if (*offset < 0) /* Error from previous call? report it */
258 if (index < 0 || index > MN_WORDS) /* Word index out of range */
264 if (*offset > destsize) /* out of range? */
266 *offset = MN_EOVERRUN;
270 if (index > MN_BASE && *offset % 4 != 2)
271 { /* Unexpected 24 bit remainder word */
272 *offset = MN_EINDEX24;
276 groupofs = *offset & ~3; /* Offset of 4 byte group containing offet */
278 for (i = 0; i < 4; i++)
279 if (groupofs + i < destsize) /* Ignore any bytes outside buffer */
280 x |= ((mn_byte *) dest)[groupofs + i] << (i * 8); /* assemble number */
282 if (index == MN_EOF) /* Got EOF signal */
286 case 3: /* group was three words and the last */
287 return MN_OK; /* word was a 24 bit remainder */
288 case 2: /* last group has two words */
289 if (x <= 0xFFFF) /* should encode 16 bit data */
296 case 1: /* last group has just one word */
297 if (x <= 0xFF) /* should encode 8 bits */
305 case 0: /* last group was full 3 words */
309 if (*offset == destsize) /* At EOF but didn't get MN_EOF */
311 *offset = MN_EOVERRUN;
315 index--; /* 1 based to 0 based index */
319 case 3: /* Got data past 24 bit remainder */
320 *offset = MN_EOVERRUN24;
323 if (index >= MN_BASE)
324 { /* 24 bit remainder */
325 x += (index - MN_BASE) * MN_BASE * MN_BASE;
326 (*offset)++; /* *offset%4 == 3 for next time */
329 { /* catch invalid encodings */
330 if (index >= 1625 || (index == 1624 && x > 1312671))
332 *offset = MN_EENCODING;
335 x += index * MN_BASE * MN_BASE;
336 (*offset) += 2; /* *offset%4 == 0 for next time */
340 x += index * MN_BASE;
349 for (i = 0; i < 4; i++)
350 if (groupofs + i < destsize) /* Don't step outside the buffer */
352 ((mn_byte *) dest)[groupofs + i] = (mn_byte) x % 256;
362 * Encode a binary data buffer into a null-terminated sequence of words.
363 * The word separators are taken from the format string.
366 * src - Pointer to the beginning of the binary data buffer.
367 * srcsize - Size in bytes of binary data buffer
368 * dest - Pointer to the beginning of a character buffer
369 * destsize - Size in characters of character buffer
370 * format - Null-terminated string describing the output format.
371 * In the format string any letter or sequence of letters
372 * acts as a placeholder for the encoded words. The word
373 * placeholders are separated by one or more non-letter
374 * characters. When the encoder reaches the end of the
375 * format string it starts reading it again.
376 * For sample formats see MN_F* constants in mnemonic.h
377 * If format is empty or NULL the format MN_FDEFAULT
382 * Encoding was successful.
384 * Output size exceeds size of destination buffer
386 * Invalid format string. This function enforces formats which
387 * will result in a string which can be successfully decoded by
388 * the mn_decode function.
392 mn_encode (void *src, int srcsize, char *dest, int destsize, char *format)
396 char *destend = dest + destsize;
399 if (format == 0 || format[0] == '\0')
400 format = MN_FDEFAULT;
402 for (n = 0; n < mn_words_required (srcsize); n++)
404 while (dest < destend && *fmt != '\0' && !isletter (*fmt))
410 if (isletter (fmt[-1]) && isletter (format[0]))
413 while (dest < destend && *fmt != '\0' && !isletter (*fmt))
415 if (!isletter (*fmt))
418 word = mn_encode_word (src, srcsize, n);
420 return MN_EOVERRUN; /* shouldn't happen, actually */
422 while (isletter (*fmt))
424 while (dest < destend && *word != '\0')
438 * Decode a text representation in null-terminated character buffer src to
439 * binary buffer dest.
442 * src - Pointer to null-terminated character buffer
443 * dest - Pointer to beginning of destination buffer
444 * destsize - Size in bytes of destination buffer
447 * This function may return all the value returned by mn_decode_word_index
448 * plus the following result code:
450 * MN_EWORD - Unrecognized word.
454 mn_decode (char *src, void *dest, int destsize)
459 while ((index = mn_next_word_index (&src)) != 0)
461 if (index == 0 && *src != 0)
463 (void) mn_decode_word_index (index, dest, destsize, &offset);
465 (void) mn_decode_word_index (MN_EOF, dest, destsize, &offset);