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')
78 * Figure out how many characters are in the input buffer. If this
79 * would decode into more bytes than would fit into the output
80 * buffer, adjust the number of input bytes downwards.
83 while (DEC(*(bufin++)) <= MAXVAL);
84 nprbytes = bufin - bufcoded - 1;
85 nbytesdecoded = ((nprbytes + 3) / 4) * 3;
86 if (nbytesdecoded > outbufsize)
87 nprbytes = (outbufsize * 4) / 3;
91 while (nprbytes > 0) {
92 *(bufout++) = (unsigned char) (DEC(*bufin) << 2 | DEC(bufin[1]) >> 4);
93 *(bufout++) = (unsigned char) (DEC(bufin[1]) << 4 | DEC(bufin[2]) >> 2);
94 *(bufout++) = (unsigned char) (DEC(bufin[2]) << 6 | DEC(bufin[3]));
99 if (DEC(bufin[-2]) > MAXVAL)
104 return (nbytesdecoded);
107 typedef unsigned char my_u_char;
108 typedef unsigned int my_u_int32_t;
109 typedef unsigned short my_u_short;
111 /* Nasty macros from BIND-4.9.2 */
113 #define GETSHORT(s, cp) { \
114 register my_u_char *t_cp = (my_u_char*)(cp); \
115 (s) = (((my_u_short)t_cp[0]) << 8) \
116 | (((my_u_short)t_cp[1])) \
121 #define GETLONG(l, cp) { \
122 register my_u_char *t_cp = (my_u_char*)(cp); \
123 (l) = (((my_u_int32_t)t_cp[0]) << 24) \
124 | (((my_u_int32_t)t_cp[1]) << 16) \
125 | (((my_u_int32_t)t_cp[2]) << 8) \
126 | (((my_u_int32_t)t_cp[3])) \
131 #define PUTSHORT(s, cp) { \
132 register my_u_short t_s = (my_u_short)(s); \
133 register my_u_char *t_cp = (my_u_char*)(cp); \
134 *t_cp++ = t_s >> 8; \
139 #define PUTLONG(l, cp) { \
140 register my_u_int32_t t_l = (my_u_int32_t)(l); \
141 register my_u_char *t_cp = (my_u_char*)(cp); \
142 *t_cp++ = t_l >> 24; \
143 *t_cp++ = t_l >> 16; \
144 *t_cp++ = t_l >> 8; \
149 #define GETSTRING(s, p, p_l) { \
150 register char* p_targ = (p) + p_l; \
151 register char* s_c = (s); \
152 register char* p_c = (p); \
153 while (*p_c && (p_c < p_targ)) { \
156 if (p_c == p_targ) { \
160 (p_l) = (p_l) - (p_c - (p)); \
166 creds_to_radix(CREDENTIALS *creds, unsigned char *buf)
173 *p++ = 1; /* version */
195 /* Null string to repeat the realm. */
198 PUTLONG(creds->issue_date, p);
200 unsigned int endTime;
201 endTime = (unsigned int) krb_life_to_time(creds->issue_date,
206 memcpy(p, &creds->session, sizeof(creds->session));
207 p += sizeof(creds->session);
209 PUTSHORT(creds->kvno, p);
210 PUTLONG(creds->ticket_st.length, p);
212 memcpy(p, creds->ticket_st.dat, creds->ticket_st.length);
213 p += creds->ticket_st.length;
216 return (uuencode((unsigned char *)temp, len, (char *)buf));
220 radix_to_creds(const char *buf, CREDENTIALS *creds)
228 if (!(len = uudecode(buf, (unsigned char *)temp, sizeof(temp))))
233 /* check version and length! */
240 GETSTRING(creds->service, p, len);
241 GETSTRING(creds->instance, p, len);
242 GETSTRING(creds->realm, p, len);
244 GETSTRING(creds->pname, p, len);
245 GETSTRING(creds->pinst, p, len);
246 /* Ignore possibly different realm. */
253 /* Enough space for remaining fixed-length parts? */
254 if (len < (4 + 4 + sizeof(creds->session) + 2 + 4))
257 GETLONG(creds->issue_date, p);
260 unsigned int endTime;
263 creds->lifetime = krb_time_to_life(creds->issue_date, endTime);
266 memcpy(&creds->session, p, sizeof(creds->session));
267 p += sizeof(creds->session);
268 len -= sizeof(creds->session);
270 GETSHORT(creds->kvno, p);
272 GETLONG(creds->ticket_st.length, p);
275 tl = creds->ticket_st.length;
276 if (tl < 0 || tl > len || tl > sizeof(creds->ticket_st.dat))
279 memcpy(creds->ticket_st.dat, p, tl);