1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* util/support/ipc_stream.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright 2006, 2007, 2009 Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All Rights Reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert
27*7f2fe78bSCy Schubert #ifdef _WIN32
28*7f2fe78bSCy Schubert #include <winsock2.h>
29*7f2fe78bSCy Schubert #endif
30*7f2fe78bSCy Schubert #include "k5-ipc_stream.h"
31*7f2fe78bSCy Schubert
32*7f2fe78bSCy Schubert #if !defined(htonll)
33*7f2fe78bSCy Schubert #define htonll(x) k5_htonll(x)
34*7f2fe78bSCy Schubert #endif
35*7f2fe78bSCy Schubert
36*7f2fe78bSCy Schubert #if !defined(ntohll)
37*7f2fe78bSCy Schubert #define ntohll(x) k5_ntohll(x)
38*7f2fe78bSCy Schubert #endif
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert /* Add debugging later */
41*7f2fe78bSCy Schubert #define k5_check_error(x) (x)
42*7f2fe78bSCy Schubert
43*7f2fe78bSCy Schubert struct k5_ipc_stream_s {
44*7f2fe78bSCy Schubert char *data;
45*7f2fe78bSCy Schubert uint64_t size;
46*7f2fe78bSCy Schubert uint64_t max_size;
47*7f2fe78bSCy Schubert };
48*7f2fe78bSCy Schubert
49*7f2fe78bSCy Schubert static const struct k5_ipc_stream_s k5_ipc_stream_initializer = { NULL, 0, 0 };
50*7f2fe78bSCy Schubert
51*7f2fe78bSCy Schubert #define K5_IPC_STREAM_SIZE_INCREMENT 128
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
54*7f2fe78bSCy Schubert
krb5int_ipc_stream_reallocate(k5_ipc_stream io_stream,uint64_t in_new_size)55*7f2fe78bSCy Schubert static uint32_t krb5int_ipc_stream_reallocate (k5_ipc_stream io_stream,
56*7f2fe78bSCy Schubert uint64_t in_new_size)
57*7f2fe78bSCy Schubert {
58*7f2fe78bSCy Schubert int32_t err = 0;
59*7f2fe78bSCy Schubert uint64_t new_max_size = 0;
60*7f2fe78bSCy Schubert
61*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
62*7f2fe78bSCy Schubert
63*7f2fe78bSCy Schubert if (!err) {
64*7f2fe78bSCy Schubert uint64_t old_max_size = io_stream->max_size;
65*7f2fe78bSCy Schubert new_max_size = io_stream->max_size;
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert if (in_new_size > old_max_size) {
68*7f2fe78bSCy Schubert /* Expand the stream */
69*7f2fe78bSCy Schubert while (in_new_size > new_max_size) {
70*7f2fe78bSCy Schubert new_max_size += K5_IPC_STREAM_SIZE_INCREMENT;
71*7f2fe78bSCy Schubert }
72*7f2fe78bSCy Schubert
73*7f2fe78bSCy Schubert
74*7f2fe78bSCy Schubert } else if ((in_new_size + K5_IPC_STREAM_SIZE_INCREMENT) < old_max_size) {
75*7f2fe78bSCy Schubert /* Shrink the array, but never drop below K5_IPC_STREAM_SIZE_INCREMENT */
76*7f2fe78bSCy Schubert while ((in_new_size + K5_IPC_STREAM_SIZE_INCREMENT) < new_max_size &&
77*7f2fe78bSCy Schubert (new_max_size > K5_IPC_STREAM_SIZE_INCREMENT)) {
78*7f2fe78bSCy Schubert new_max_size -= K5_IPC_STREAM_SIZE_INCREMENT;
79*7f2fe78bSCy Schubert }
80*7f2fe78bSCy Schubert }
81*7f2fe78bSCy Schubert }
82*7f2fe78bSCy Schubert
83*7f2fe78bSCy Schubert if (!err && new_max_size != io_stream->max_size) {
84*7f2fe78bSCy Schubert char *data = io_stream->data;
85*7f2fe78bSCy Schubert
86*7f2fe78bSCy Schubert if (!data) {
87*7f2fe78bSCy Schubert data = malloc (new_max_size * sizeof (*data));
88*7f2fe78bSCy Schubert } else {
89*7f2fe78bSCy Schubert data = realloc (data, new_max_size * sizeof (*data));
90*7f2fe78bSCy Schubert }
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert if (data) {
93*7f2fe78bSCy Schubert io_stream->data = data;
94*7f2fe78bSCy Schubert io_stream->max_size = new_max_size;
95*7f2fe78bSCy Schubert } else {
96*7f2fe78bSCy Schubert err = k5_check_error (ENOMEM);
97*7f2fe78bSCy Schubert }
98*7f2fe78bSCy Schubert }
99*7f2fe78bSCy Schubert
100*7f2fe78bSCy Schubert return k5_check_error (err);
101*7f2fe78bSCy Schubert }
102*7f2fe78bSCy Schubert
103*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
104*7f2fe78bSCy Schubert
krb5int_ipc_stream_new(k5_ipc_stream * out_stream)105*7f2fe78bSCy Schubert int32_t krb5int_ipc_stream_new (k5_ipc_stream *out_stream)
106*7f2fe78bSCy Schubert {
107*7f2fe78bSCy Schubert int32_t err = 0;
108*7f2fe78bSCy Schubert k5_ipc_stream stream = NULL;
109*7f2fe78bSCy Schubert
110*7f2fe78bSCy Schubert if (!out_stream) { err = k5_check_error (EINVAL); }
111*7f2fe78bSCy Schubert
112*7f2fe78bSCy Schubert if (!err) {
113*7f2fe78bSCy Schubert stream = malloc (sizeof (*stream));
114*7f2fe78bSCy Schubert if (stream) {
115*7f2fe78bSCy Schubert *stream = k5_ipc_stream_initializer;
116*7f2fe78bSCy Schubert } else {
117*7f2fe78bSCy Schubert err = k5_check_error (ENOMEM);
118*7f2fe78bSCy Schubert }
119*7f2fe78bSCy Schubert }
120*7f2fe78bSCy Schubert
121*7f2fe78bSCy Schubert if (!err) {
122*7f2fe78bSCy Schubert *out_stream = stream;
123*7f2fe78bSCy Schubert stream = NULL;
124*7f2fe78bSCy Schubert }
125*7f2fe78bSCy Schubert
126*7f2fe78bSCy Schubert krb5int_ipc_stream_release (stream);
127*7f2fe78bSCy Schubert
128*7f2fe78bSCy Schubert return k5_check_error (err);
129*7f2fe78bSCy Schubert }
130*7f2fe78bSCy Schubert
131*7f2fe78bSCy Schubert
132*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
133*7f2fe78bSCy Schubert
krb5int_ipc_stream_release(k5_ipc_stream io_stream)134*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_release (k5_ipc_stream io_stream)
135*7f2fe78bSCy Schubert {
136*7f2fe78bSCy Schubert int32_t err = 0;
137*7f2fe78bSCy Schubert
138*7f2fe78bSCy Schubert if (!err && io_stream) {
139*7f2fe78bSCy Schubert free (io_stream->data);
140*7f2fe78bSCy Schubert free (io_stream);
141*7f2fe78bSCy Schubert }
142*7f2fe78bSCy Schubert
143*7f2fe78bSCy Schubert return err;
144*7f2fe78bSCy Schubert }
145*7f2fe78bSCy Schubert
146*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
147*7f2fe78bSCy Schubert
krb5int_ipc_stream_size(k5_ipc_stream in_stream)148*7f2fe78bSCy Schubert uint64_t krb5int_ipc_stream_size (k5_ipc_stream in_stream)
149*7f2fe78bSCy Schubert {
150*7f2fe78bSCy Schubert return in_stream ? in_stream->size : 0;
151*7f2fe78bSCy Schubert }
152*7f2fe78bSCy Schubert
153*7f2fe78bSCy Schubert
154*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
155*7f2fe78bSCy Schubert
krb5int_ipc_stream_data(k5_ipc_stream in_stream)156*7f2fe78bSCy Schubert const char *krb5int_ipc_stream_data (k5_ipc_stream in_stream)
157*7f2fe78bSCy Schubert {
158*7f2fe78bSCy Schubert return in_stream ? in_stream->data : NULL;
159*7f2fe78bSCy Schubert }
160*7f2fe78bSCy Schubert
161*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
162*7f2fe78bSCy Schubert #pragma mark -
163*7f2fe78bSCy Schubert #endif
164*7f2fe78bSCy Schubert
165*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
166*7f2fe78bSCy Schubert
krb5int_ipc_stream_read(k5_ipc_stream io_stream,void * io_data,uint64_t in_size)167*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_read (k5_ipc_stream io_stream,
168*7f2fe78bSCy Schubert void *io_data,
169*7f2fe78bSCy Schubert uint64_t in_size)
170*7f2fe78bSCy Schubert {
171*7f2fe78bSCy Schubert int32_t err = 0;
172*7f2fe78bSCy Schubert
173*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
174*7f2fe78bSCy Schubert if (!io_data ) { err = k5_check_error (EINVAL); }
175*7f2fe78bSCy Schubert
176*7f2fe78bSCy Schubert if (!err) {
177*7f2fe78bSCy Schubert if (in_size > io_stream->size) {
178*7f2fe78bSCy Schubert err = k5_check_error (EINVAL);
179*7f2fe78bSCy Schubert }
180*7f2fe78bSCy Schubert }
181*7f2fe78bSCy Schubert
182*7f2fe78bSCy Schubert if (!err) {
183*7f2fe78bSCy Schubert memcpy (io_data, io_stream->data, in_size);
184*7f2fe78bSCy Schubert memmove (io_stream->data, &io_stream->data[in_size],
185*7f2fe78bSCy Schubert io_stream->size - in_size);
186*7f2fe78bSCy Schubert
187*7f2fe78bSCy Schubert err = krb5int_ipc_stream_reallocate (io_stream, io_stream->size - in_size);
188*7f2fe78bSCy Schubert
189*7f2fe78bSCy Schubert if (!err) {
190*7f2fe78bSCy Schubert io_stream->size -= in_size;
191*7f2fe78bSCy Schubert }
192*7f2fe78bSCy Schubert }
193*7f2fe78bSCy Schubert
194*7f2fe78bSCy Schubert return k5_check_error (err);
195*7f2fe78bSCy Schubert }
196*7f2fe78bSCy Schubert
197*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
198*7f2fe78bSCy Schubert
krb5int_ipc_stream_write(k5_ipc_stream io_stream,const void * in_data,uint64_t in_size)199*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_write (k5_ipc_stream io_stream,
200*7f2fe78bSCy Schubert const void *in_data,
201*7f2fe78bSCy Schubert uint64_t in_size)
202*7f2fe78bSCy Schubert {
203*7f2fe78bSCy Schubert int32_t err = 0;
204*7f2fe78bSCy Schubert
205*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
206*7f2fe78bSCy Schubert if (!in_data ) { err = k5_check_error (EINVAL); }
207*7f2fe78bSCy Schubert
208*7f2fe78bSCy Schubert if (!err) {
209*7f2fe78bSCy Schubert /* Security check: Do not let the caller overflow the length */
210*7f2fe78bSCy Schubert if (in_size > (UINT64_MAX - io_stream->size)) {
211*7f2fe78bSCy Schubert err = k5_check_error (EINVAL);
212*7f2fe78bSCy Schubert }
213*7f2fe78bSCy Schubert }
214*7f2fe78bSCy Schubert
215*7f2fe78bSCy Schubert if (!err) {
216*7f2fe78bSCy Schubert err = krb5int_ipc_stream_reallocate (io_stream, io_stream->size + in_size);
217*7f2fe78bSCy Schubert }
218*7f2fe78bSCy Schubert
219*7f2fe78bSCy Schubert if (!err) {
220*7f2fe78bSCy Schubert memcpy (&io_stream->data[io_stream->size], in_data, in_size);
221*7f2fe78bSCy Schubert io_stream->size += in_size;
222*7f2fe78bSCy Schubert }
223*7f2fe78bSCy Schubert
224*7f2fe78bSCy Schubert return k5_check_error (err);
225*7f2fe78bSCy Schubert }
226*7f2fe78bSCy Schubert
227*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
228*7f2fe78bSCy Schubert #pragma mark -
229*7f2fe78bSCy Schubert #endif
230*7f2fe78bSCy Schubert
231*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
232*7f2fe78bSCy Schubert
krb5int_ipc_stream_free_string(char * in_string)233*7f2fe78bSCy Schubert void krb5int_ipc_stream_free_string (char *in_string)
234*7f2fe78bSCy Schubert {
235*7f2fe78bSCy Schubert free (in_string);
236*7f2fe78bSCy Schubert }
237*7f2fe78bSCy Schubert
238*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
239*7f2fe78bSCy Schubert
krb5int_ipc_stream_read_string(k5_ipc_stream io_stream,char ** out_string)240*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_read_string (k5_ipc_stream io_stream,
241*7f2fe78bSCy Schubert char **out_string)
242*7f2fe78bSCy Schubert {
243*7f2fe78bSCy Schubert int32_t err = 0;
244*7f2fe78bSCy Schubert uint32_t length = 0;
245*7f2fe78bSCy Schubert char *string = NULL;
246*7f2fe78bSCy Schubert
247*7f2fe78bSCy Schubert if (!io_stream ) { err = k5_check_error (EINVAL); }
248*7f2fe78bSCy Schubert if (!out_string) { err = k5_check_error (EINVAL); }
249*7f2fe78bSCy Schubert
250*7f2fe78bSCy Schubert if (!err) {
251*7f2fe78bSCy Schubert err = krb5int_ipc_stream_read_uint32 (io_stream, &length);
252*7f2fe78bSCy Schubert }
253*7f2fe78bSCy Schubert
254*7f2fe78bSCy Schubert if (!err) {
255*7f2fe78bSCy Schubert string = malloc (length);
256*7f2fe78bSCy Schubert if (!string) { err = k5_check_error (ENOMEM); }
257*7f2fe78bSCy Schubert }
258*7f2fe78bSCy Schubert
259*7f2fe78bSCy Schubert if (!err) {
260*7f2fe78bSCy Schubert err = krb5int_ipc_stream_read (io_stream, string, length);
261*7f2fe78bSCy Schubert }
262*7f2fe78bSCy Schubert
263*7f2fe78bSCy Schubert if (!err) {
264*7f2fe78bSCy Schubert *out_string = string;
265*7f2fe78bSCy Schubert string = NULL;
266*7f2fe78bSCy Schubert }
267*7f2fe78bSCy Schubert
268*7f2fe78bSCy Schubert free (string);
269*7f2fe78bSCy Schubert
270*7f2fe78bSCy Schubert return k5_check_error (err);
271*7f2fe78bSCy Schubert }
272*7f2fe78bSCy Schubert
273*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
274*7f2fe78bSCy Schubert
krb5int_ipc_stream_write_string(k5_ipc_stream io_stream,const char * in_string)275*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_write_string (k5_ipc_stream io_stream,
276*7f2fe78bSCy Schubert const char *in_string)
277*7f2fe78bSCy Schubert {
278*7f2fe78bSCy Schubert int32_t err = 0;
279*7f2fe78bSCy Schubert uint32_t length = 0;
280*7f2fe78bSCy Schubert
281*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
282*7f2fe78bSCy Schubert if (!in_string) { err = k5_check_error (EINVAL); }
283*7f2fe78bSCy Schubert
284*7f2fe78bSCy Schubert if (!err) {
285*7f2fe78bSCy Schubert length = strlen (in_string) + 1;
286*7f2fe78bSCy Schubert
287*7f2fe78bSCy Schubert err = krb5int_ipc_stream_write_uint32 (io_stream, length);
288*7f2fe78bSCy Schubert }
289*7f2fe78bSCy Schubert
290*7f2fe78bSCy Schubert if (!err) {
291*7f2fe78bSCy Schubert err = krb5int_ipc_stream_write (io_stream, in_string, length);
292*7f2fe78bSCy Schubert }
293*7f2fe78bSCy Schubert
294*7f2fe78bSCy Schubert return k5_check_error (err);
295*7f2fe78bSCy Schubert }
296*7f2fe78bSCy Schubert
297*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
298*7f2fe78bSCy Schubert #pragma mark -
299*7f2fe78bSCy Schubert #endif
300*7f2fe78bSCy Schubert
301*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
302*7f2fe78bSCy Schubert
krb5int_ipc_stream_read_int32(k5_ipc_stream io_stream,int32_t * out_int32)303*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_read_int32 (k5_ipc_stream io_stream,
304*7f2fe78bSCy Schubert int32_t *out_int32)
305*7f2fe78bSCy Schubert {
306*7f2fe78bSCy Schubert int32_t err = 0;
307*7f2fe78bSCy Schubert int32_t int32 = 0;
308*7f2fe78bSCy Schubert
309*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
310*7f2fe78bSCy Schubert if (!out_int32) { err = k5_check_error (EINVAL); }
311*7f2fe78bSCy Schubert
312*7f2fe78bSCy Schubert if (!err) {
313*7f2fe78bSCy Schubert err = krb5int_ipc_stream_read (io_stream, &int32, sizeof (int32));
314*7f2fe78bSCy Schubert }
315*7f2fe78bSCy Schubert
316*7f2fe78bSCy Schubert if (!err) {
317*7f2fe78bSCy Schubert *out_int32 = ntohl (int32);
318*7f2fe78bSCy Schubert }
319*7f2fe78bSCy Schubert
320*7f2fe78bSCy Schubert return k5_check_error (err);
321*7f2fe78bSCy Schubert }
322*7f2fe78bSCy Schubert
323*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
324*7f2fe78bSCy Schubert
krb5int_ipc_stream_write_int32(k5_ipc_stream io_stream,int32_t in_int32)325*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_write_int32 (k5_ipc_stream io_stream,
326*7f2fe78bSCy Schubert int32_t in_int32)
327*7f2fe78bSCy Schubert {
328*7f2fe78bSCy Schubert int32_t err = 0;
329*7f2fe78bSCy Schubert int32_t int32 = htonl (in_int32);
330*7f2fe78bSCy Schubert
331*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
332*7f2fe78bSCy Schubert
333*7f2fe78bSCy Schubert if (!err) {
334*7f2fe78bSCy Schubert err = krb5int_ipc_stream_write (io_stream, &int32, sizeof (int32));
335*7f2fe78bSCy Schubert }
336*7f2fe78bSCy Schubert
337*7f2fe78bSCy Schubert return k5_check_error (err);
338*7f2fe78bSCy Schubert }
339*7f2fe78bSCy Schubert
340*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
341*7f2fe78bSCy Schubert #pragma mark -
342*7f2fe78bSCy Schubert #endif
343*7f2fe78bSCy Schubert
344*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
345*7f2fe78bSCy Schubert
krb5int_ipc_stream_read_uint32(k5_ipc_stream io_stream,uint32_t * out_uint32)346*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_read_uint32 (k5_ipc_stream io_stream,
347*7f2fe78bSCy Schubert uint32_t *out_uint32)
348*7f2fe78bSCy Schubert {
349*7f2fe78bSCy Schubert int32_t err = 0;
350*7f2fe78bSCy Schubert uint32_t uint32 = 0;
351*7f2fe78bSCy Schubert
352*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
353*7f2fe78bSCy Schubert if (!out_uint32) { err = k5_check_error (EINVAL); }
354*7f2fe78bSCy Schubert
355*7f2fe78bSCy Schubert if (!err) {
356*7f2fe78bSCy Schubert err = krb5int_ipc_stream_read (io_stream, &uint32, sizeof (uint32));
357*7f2fe78bSCy Schubert }
358*7f2fe78bSCy Schubert
359*7f2fe78bSCy Schubert if (!err) {
360*7f2fe78bSCy Schubert *out_uint32 = ntohl (uint32);
361*7f2fe78bSCy Schubert }
362*7f2fe78bSCy Schubert
363*7f2fe78bSCy Schubert return k5_check_error (err);
364*7f2fe78bSCy Schubert }
365*7f2fe78bSCy Schubert
366*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
367*7f2fe78bSCy Schubert
krb5int_ipc_stream_write_uint32(k5_ipc_stream io_stream,uint32_t in_uint32)368*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_write_uint32 (k5_ipc_stream io_stream,
369*7f2fe78bSCy Schubert uint32_t in_uint32)
370*7f2fe78bSCy Schubert {
371*7f2fe78bSCy Schubert int32_t err = 0;
372*7f2fe78bSCy Schubert int32_t uint32 = htonl (in_uint32);
373*7f2fe78bSCy Schubert
374*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
375*7f2fe78bSCy Schubert
376*7f2fe78bSCy Schubert if (!err) {
377*7f2fe78bSCy Schubert err = krb5int_ipc_stream_write (io_stream, &uint32, sizeof (uint32));
378*7f2fe78bSCy Schubert }
379*7f2fe78bSCy Schubert
380*7f2fe78bSCy Schubert return k5_check_error (err);
381*7f2fe78bSCy Schubert }
382*7f2fe78bSCy Schubert
383*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
384*7f2fe78bSCy Schubert #pragma mark -
385*7f2fe78bSCy Schubert #endif
386*7f2fe78bSCy Schubert
387*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
388*7f2fe78bSCy Schubert
krb5int_ipc_stream_read_int64(k5_ipc_stream io_stream,int64_t * out_int64)389*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_read_int64 (k5_ipc_stream io_stream,
390*7f2fe78bSCy Schubert int64_t *out_int64)
391*7f2fe78bSCy Schubert {
392*7f2fe78bSCy Schubert int32_t err = 0;
393*7f2fe78bSCy Schubert uint64_t int64 = 0;
394*7f2fe78bSCy Schubert
395*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
396*7f2fe78bSCy Schubert if (!out_int64) { err = k5_check_error (EINVAL); }
397*7f2fe78bSCy Schubert
398*7f2fe78bSCy Schubert if (!err) {
399*7f2fe78bSCy Schubert err = krb5int_ipc_stream_read (io_stream, &int64, sizeof (int64));
400*7f2fe78bSCy Schubert }
401*7f2fe78bSCy Schubert
402*7f2fe78bSCy Schubert if (!err) {
403*7f2fe78bSCy Schubert *out_int64 = ntohll (int64);
404*7f2fe78bSCy Schubert }
405*7f2fe78bSCy Schubert
406*7f2fe78bSCy Schubert return k5_check_error (err);
407*7f2fe78bSCy Schubert }
408*7f2fe78bSCy Schubert
409*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
410*7f2fe78bSCy Schubert
krb5int_ipc_stream_write_int64(k5_ipc_stream io_stream,int64_t in_int64)411*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_write_int64 (k5_ipc_stream io_stream,
412*7f2fe78bSCy Schubert int64_t in_int64)
413*7f2fe78bSCy Schubert {
414*7f2fe78bSCy Schubert int32_t err = 0;
415*7f2fe78bSCy Schubert int64_t int64 = htonll (in_int64);
416*7f2fe78bSCy Schubert
417*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
418*7f2fe78bSCy Schubert
419*7f2fe78bSCy Schubert if (!err) {
420*7f2fe78bSCy Schubert err = krb5int_ipc_stream_write (io_stream, &int64, sizeof (int64));
421*7f2fe78bSCy Schubert }
422*7f2fe78bSCy Schubert
423*7f2fe78bSCy Schubert return k5_check_error (err);
424*7f2fe78bSCy Schubert }
425*7f2fe78bSCy Schubert
426*7f2fe78bSCy Schubert
427*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
428*7f2fe78bSCy Schubert #pragma mark -
429*7f2fe78bSCy Schubert #endif
430*7f2fe78bSCy Schubert
431*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
432*7f2fe78bSCy Schubert
krb5int_ipc_stream_read_uint64(k5_ipc_stream io_stream,uint64_t * out_uint64)433*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_read_uint64 (k5_ipc_stream io_stream,
434*7f2fe78bSCy Schubert uint64_t *out_uint64)
435*7f2fe78bSCy Schubert {
436*7f2fe78bSCy Schubert int32_t err = 0;
437*7f2fe78bSCy Schubert uint64_t uint64 = 0;
438*7f2fe78bSCy Schubert
439*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
440*7f2fe78bSCy Schubert if (!out_uint64) { err = k5_check_error (EINVAL); }
441*7f2fe78bSCy Schubert
442*7f2fe78bSCy Schubert if (!err) {
443*7f2fe78bSCy Schubert err = krb5int_ipc_stream_read (io_stream, &uint64, sizeof (uint64));
444*7f2fe78bSCy Schubert }
445*7f2fe78bSCy Schubert
446*7f2fe78bSCy Schubert if (!err) {
447*7f2fe78bSCy Schubert *out_uint64 = ntohll (uint64);
448*7f2fe78bSCy Schubert }
449*7f2fe78bSCy Schubert
450*7f2fe78bSCy Schubert return k5_check_error (err);
451*7f2fe78bSCy Schubert }
452*7f2fe78bSCy Schubert
453*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
454*7f2fe78bSCy Schubert
krb5int_ipc_stream_write_uint64(k5_ipc_stream io_stream,uint64_t in_uint64)455*7f2fe78bSCy Schubert uint32_t krb5int_ipc_stream_write_uint64 (k5_ipc_stream io_stream,
456*7f2fe78bSCy Schubert uint64_t in_uint64)
457*7f2fe78bSCy Schubert {
458*7f2fe78bSCy Schubert int32_t err = 0;
459*7f2fe78bSCy Schubert int64_t uint64 = htonll (in_uint64);
460*7f2fe78bSCy Schubert
461*7f2fe78bSCy Schubert if (!io_stream) { err = k5_check_error (EINVAL); }
462*7f2fe78bSCy Schubert
463*7f2fe78bSCy Schubert if (!err) {
464*7f2fe78bSCy Schubert err = krb5int_ipc_stream_write (io_stream, &uint64, sizeof (uint64));
465*7f2fe78bSCy Schubert }
466*7f2fe78bSCy Schubert
467*7f2fe78bSCy Schubert return k5_check_error (err);
468*7f2fe78bSCy Schubert }
469