4 * base-64 encoding pinched from lynx2-7-2, who pinched it from rpem.
5 * Originally written by Mark Riordan 12 August 1990 and 17 Feb 1991
6 * and placed in the public domain.
8 * Dug Song <dugsong@UMICH.EDU>
17 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
18 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
19 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
20 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
21 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
24 unsigned char pr2six[256];
27 uuencode(unsigned char *bufin, unsigned int nbytes, char *bufcoded)
29 /* ENC is the basic 1 character encoding function to make a char printing */
30 #define ENC(c) six2pr[c]
32 register char *outptr = bufcoded;
35 for (i = 0; i < nbytes; i += 3) {
36 *(outptr++) = ENC(*bufin >> 2); /* c1 */
37 *(outptr++) = ENC(((*bufin << 4) & 060) | ((bufin[1] >> 4) & 017)); /* c2 */
38 *(outptr++) = ENC(((bufin[1] << 2) & 074) | ((bufin[2] >> 6) & 03)); /* c3 */
39 *(outptr++) = ENC(bufin[2] & 077); /* c4 */
42 if (i == nbytes + 1) {
44 } else if (i == nbytes + 2) {
49 return (outptr - bufcoded);
53 uudecode(const char *bufcoded, unsigned char *bufplain, int outbufsize)
55 /* single character decode */
56 #define DEC(c) pr2six[(unsigned char)c]
61 const char *bufin = bufcoded;
62 register unsigned char *bufout = bufplain;
63 register int nprbytes;
65 /* If this is the first call, initialize the mapping table. */
68 for (j = 0; j < 256; j++)
69 pr2six[j] = MAXVAL + 1;
70 for (j = 0; j < 64; j++)
71 pr2six[(unsigned char) six2pr[j]] = (unsigned char) j;
73 /* Strip leading whitespace. */
74 while (*bufcoded == ' ' || *bufcoded == '\t')
77 /* Figure out how many characters are in the input buffer. If this
78 would decode into more bytes than would fit into the output
79 buffer, adjust the number of input bytes downwards. */
81 while (DEC(*(bufin++)) <= MAXVAL);
82 nprbytes = bufin - bufcoded - 1;
83 nbytesdecoded = ((nprbytes + 3) / 4) * 3;
84 if (nbytesdecoded > outbufsize)
85 nprbytes = (outbufsize * 4) / 3;
89 while (nprbytes > 0) {
90 *(bufout++) = (unsigned char) (DEC(*bufin) << 2 | DEC(bufin[1]) >> 4);
91 *(bufout++) = (unsigned char) (DEC(bufin[1]) << 4 | DEC(bufin[2]) >> 2);
92 *(bufout++) = (unsigned char) (DEC(bufin[2]) << 6 | DEC(bufin[3]));
97 if (DEC(bufin[-2]) > MAXVAL)
102 return (nbytesdecoded);
105 typedef unsigned char my_u_char;
106 typedef unsigned int my_u_int32_t;
107 typedef unsigned short my_u_short;
109 /* Nasty macros from BIND-4.9.2 */
111 #define GETSHORT(s, cp) { \
112 register my_u_char *t_cp = (my_u_char*)(cp); \
113 (s) = (((my_u_short)t_cp[0]) << 8) \
114 | (((my_u_short)t_cp[1])) \
119 #define GETLONG(l, cp) { \
120 register my_u_char *t_cp = (my_u_char*)(cp); \
121 (l) = (((my_u_int32_t)t_cp[0]) << 24) \
122 | (((my_u_int32_t)t_cp[1]) << 16) \
123 | (((my_u_int32_t)t_cp[2]) << 8) \
124 | (((my_u_int32_t)t_cp[3])) \
129 #define PUTSHORT(s, cp) { \
130 register my_u_short t_s = (my_u_short)(s); \
131 register my_u_char *t_cp = (my_u_char*)(cp); \
132 *t_cp++ = t_s >> 8; \
137 #define PUTLONG(l, cp) { \
138 register my_u_int32_t t_l = (my_u_int32_t)(l); \
139 register my_u_char *t_cp = (my_u_char*)(cp); \
140 *t_cp++ = t_l >> 24; \
141 *t_cp++ = t_l >> 16; \
142 *t_cp++ = t_l >> 8; \
147 #define GETSTRING(s, p, p_l) { \
148 register char* p_targ = (p) + p_l; \
149 register char* s_c = (s); \
150 register char* p_c = (p); \
151 while (*p_c && (p_c < p_targ)) { \
154 if (p_c == p_targ) { \
158 (p_l) = (p_l) - (p_c - (p)); \
164 creds_to_radix(CREDENTIALS *creds, unsigned char *buf)
171 *p++ = 1; /* version */
193 /* Null string to repeat the realm. */
196 PUTLONG(creds->issue_date, p);
198 unsigned int endTime;
199 endTime = (unsigned int) krb_life_to_time(creds->issue_date,
204 memcpy(p, &creds->session, sizeof(creds->session));
205 p += sizeof(creds->session);
207 PUTSHORT(creds->kvno, p);
208 PUTLONG(creds->ticket_st.length, p);
210 memcpy(p, creds->ticket_st.dat, creds->ticket_st.length);
211 p += creds->ticket_st.length;
214 return (uuencode(temp, len, buf));
218 radix_to_creds(const char *buf, CREDENTIALS *creds)
226 if (!(len = uudecode(buf, temp, sizeof(temp))))
231 /* check version and length! */
238 GETSTRING(creds->service, p, len);
239 GETSTRING(creds->instance, p, len);
240 GETSTRING(creds->realm, p, len);
242 GETSTRING(creds->pname, p, len);
243 GETSTRING(creds->pinst, p, len);
244 /* Ignore possibly different realm. */
251 /* Enough space for remaining fixed-length parts? */
252 if (len < (4 + 4 + sizeof(creds->session) + 2 + 4))
255 GETLONG(creds->issue_date, p);
258 unsigned int endTime;
261 creds->lifetime = krb_time_to_life(creds->issue_date, endTime);
264 memcpy(&creds->session, p, sizeof(creds->session));
265 p += sizeof(creds->session);
266 len -= sizeof(creds->session);
268 GETSHORT(creds->kvno, p);
270 GETLONG(creds->ticket_st.length, p);
273 tl = creds->ticket_st.length;
274 if (tl < 0 || tl > len || tl > sizeof(creds->ticket_st.dat))
277 memcpy(creds->ticket_st.dat, p, tl);