]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-lpc21xx/tools/tolpc/uuencode.c
cmdproc: Make backspace work even in sterm
[sysless.git] / arch / arm / mach-lpc21xx / tools / tolpc / uuencode.c
1 /*
2 *  C Implementation: uuencode
3 *
4 * Description: 
5 *
6 *
7 * Author: Michal Sojka <sojkam1@fel.cvut.cz>, (C) 2005
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12
13 #include <inttypes.h>
14 #include "uuencode.h"
15
16 static char uuencode_table[64];
17 static char initialized = 0;
18 /**
19  * Initializes uuencode_table.
20  */
21 void uuencode_init() {
22     int i;
23     uuencode_table[0] = 0x60;
24
25     for(i = 1; i < 64; i++) {
26         uuencode_table[i] = (char)(0x20 + i);
27     }
28     initialized = 1;
29 }
30
31 /**
32  * Uuencodes up to 45 bytes of data. The output string is not terminated by any newline character.
33  * @param dest Where to store output string. There should be space at least for 62 bytes.
34  * @param source 
35  * @param length 
36  * @return Zero on success, -1 on error.
37  */
38 int uuencode_line(char *dest, unsigned char *source, int length) {
39     int i;
40     int data = 0;
41
42     if (!initialized)
43         uuencode_init();
44
45     if (length > 45 || length < 0)
46         return -1;
47
48     *dest++ = uuencode_table[length];
49     for (i = 0; i < length; i+=3) {
50         data = *source++;
51         data = (data << 8) | ((i+1 < length) ? *source++ : 0);
52         data = (data << 8) | ((i+2 < length) ? *source++ : 0);
53
54         *dest++ = uuencode_table[(data >> 18) & 0x3f];
55         *dest++ = uuencode_table[(data >> 12) & 0x3f];
56         *dest++ = uuencode_table[(data >>  6) & 0x3f];
57         *dest++ = uuencode_table[ data        & 0x3f];
58     }
59     *dest = '\0';
60     return 0;
61 }