2 Copyright (C) 1989 by the Massachusetts Institute of Technology
4 Export of this software from the United States of America is assumed
5 to require a specific license from the United States Government.
6 It is the responsibility of any person or organization contemplating
7 export to obtain such a license before exporting.
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission. M.I.T. makes no representations about the suitability of
17 this software for any purpose. It is provided "as is" without express
23 * Stream conversion functions for Kerberos administration server
28 this holds the stream support routines for the kerberos administration server
30 vals_to_stream: converts a vals struct to a stream for transmission
31 internals build_field_header, vts_[string, char, long, short]
32 stream_to_vals: converts a stream to a vals struct
33 internals check_field_header, stv_[string, char, long, short]
34 error: prints out a kadm error message, returns
35 fatal: prints out a kadm fatal error message, exits
38 #include "kadm_locl.h"
40 RCSID("$Id: kadm_stream.c,v 1.13 1998/10/22 15:38:01 joda Exp $");
43 build_field_header(u_char *cont, /* container for fields data */
44 u_char **st) /* stream */
50 return 4; /* return pointer to current stream location */
54 check_field_header(u_char *st, /* stream */
55 u_char *cont, /* container for fields data */
61 return 4; /* return pointer to current stream location */
65 vts_string(char *dat, /* a string to put on the stream */
66 u_char **st, /* base pointer to the stream */
67 int loc) /* offset into the stream for current data */
71 tmp = realloc(*st, loc + strlen(dat) + 1);
74 memcpy((char *)tmp + loc, dat, strlen(dat)+1);
81 vts_short(u_int16_t dat, /* the attributes field */
82 u_char **st, /* a base pointer to the stream */
83 int loc) /* offset into the stream for current data */
87 p = realloc(*st, loc + 2);
90 p[loc] = (dat >> 8) & 0xff;
91 p[loc+1] = dat & 0xff;
97 vts_char(u_char dat, /* the attributes field */
98 u_char **st, /* a base pointer to the stream */
99 int loc) /* offset into the stream for current data */
103 p = realloc(*st, loc + 1);
113 vts_long(u_int32_t dat, /* the attributes field */
114 u_char **st, /* a base pointer to the stream */
115 int loc) /* offset into the stream for current data */
119 p = realloc(*st, loc + 4);
122 p[loc] = (dat >> 24) & 0xff;
123 p[loc+1] = (dat >> 16) & 0xff;
124 p[loc+2] = (dat >> 8) & 0xff;
125 p[loc+3] = dat & 0xff;
131 stv_string(u_char *st, /* base pointer to the stream */
132 char *dat, /* a string to read from the stream */
133 int loc, /* offset into the stream for current data */
134 int stlen, /* max length of string to copy in */
135 int maxlen) /* max length of input stream */
137 int maxcount; /* max count of chars to copy */
140 maxcount = min(maxlen - loc, stlen);
145 len = strnlen ((char *)st + loc, maxlen - loc);
150 memcpy(dat, st + loc, len);
156 stv_short(u_char *st, /* a base pointer to the stream */
157 u_int16_t *dat, /* the attributes field */
158 int loc, /* offset into the stream for current data */
161 if (maxlen - loc < 2)
164 *dat = (st[loc] << 8) | st[loc + 1];
169 stv_long(u_char *st, /* a base pointer to the stream */
170 u_int32_t *dat, /* the attributes field */
171 int loc, /* offset into the stream for current data */
172 int maxlen) /* maximum length of st */
174 if (maxlen - loc < 4)
177 *dat = (st[loc] << 24) | (st[loc+1] << 16) | (st[loc+2] << 8) | st[loc+3];
182 stv_char(u_char *st, /* a base pointer to the stream */
183 u_char *dat, /* the attributes field */
184 int loc, /* offset into the stream for current data */
187 if (maxlen - loc < 1)
196 recieves : kadm_vals *, u_char *
197 returns : a realloced and filled in u_char *
199 this function creates a byte-stream representation of the kadm_vals structure
202 vals_to_stream(Kadm_vals *dt_in, u_char **dt_out)
204 int vsloop, stsize; /* loop counter, stream size */
206 stsize = build_field_header(dt_in->fields, dt_out);
209 for (vsloop=31; vsloop>=0; vsloop--)
210 if (IS_FIELD(vsloop,dt_in->fields)) {
215 tmp = vts_string(dt_in->name, dt_out, stsize);
218 tmp = vts_string(dt_in->instance, dt_out, stsize);
221 tmp = vts_long(dt_in->exp_date, dt_out, stsize);
224 tmp = vts_short(dt_in->attributes, dt_out, stsize);
227 tmp = vts_char(dt_in->max_life, dt_out, stsize);
230 tmp = vts_long(dt_in->key_high, dt_out, stsize);
232 tmp += vts_long(dt_in->key_low, dt_out, stsize + tmp);
236 tmp = vts_long(dt_in->mod_date, dt_out, stsize);
239 tmp = vts_string(dt_in->mod_name, dt_out, stsize);
242 tmp = vts_string(dt_in->mod_instance, dt_out, stsize);
245 tmp = vts_char(dt_in->key_version, dt_out, stsize);
262 recieves : u_char *, kadm_vals *
263 returns : a kadm_vals filled in according to u_char *
265 this decodes a byte stream represntation of a vals struct into kadm_vals
268 stream_to_vals(u_char *dt_in,
270 int maxlen) /* max length to use */
272 int vsloop, stsize; /* loop counter, stream size */
275 memset(dt_out, 0, sizeof(*dt_out));
277 stsize = check_field_header(dt_in, dt_out->fields, maxlen);
280 for (vsloop=31; vsloop>=0; vsloop--)
281 if (IS_FIELD(vsloop,dt_out->fields))
284 if ((status = stv_string(dt_in, dt_out->name, stsize,
285 sizeof(dt_out->name), maxlen)) < 0)
290 if ((status = stv_string(dt_in, dt_out->instance, stsize,
291 sizeof(dt_out->instance), maxlen)) < 0)
296 if ((status = stv_long(dt_in, &dt_out->exp_date, stsize,
302 if ((status = stv_short(dt_in, &dt_out->attributes, stsize,
308 if ((status = stv_char(dt_in, &dt_out->max_life, stsize,
314 if ((status = stv_long(dt_in, &dt_out->key_high, stsize,
318 if ((status = stv_long(dt_in, &dt_out->key_low, stsize,
325 if ((status = stv_long(dt_in, &dt_out->mod_date, stsize,
331 if ((status = stv_string(dt_in, dt_out->mod_name, stsize,
332 sizeof(dt_out->mod_name), maxlen)) < 0)
337 if ((status = stv_string(dt_in, dt_out->mod_instance, stsize,
338 sizeof(dt_out->mod_instance), maxlen)) < 0)
343 if ((status = stv_char(dt_in, &dt_out->key_version, stsize,