xref: /freebsd/crypto/krb5/src/include/k5-buf.h (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* include/k5-buf.h - k5buf interface declarations */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright 2008 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 #ifndef K5_BUF_H
28*7f2fe78bSCy Schubert #define K5_BUF_H
29*7f2fe78bSCy Schubert 
30*7f2fe78bSCy Schubert #include <stdarg.h>
31*7f2fe78bSCy Schubert #include <string.h>
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * The k5buf module is intended to allow multi-step string construction in a
35*7f2fe78bSCy Schubert  * fixed or dynamic buffer without the need to check for a failure at each step
36*7f2fe78bSCy Schubert  * (and without aborting on malloc failure).  If an allocation failure occurs
37*7f2fe78bSCy Schubert  * or the fixed buffer runs out of room, the buffer will be set to an error
38*7f2fe78bSCy Schubert  * state which can be detected with k5_buf_status.  Data in a buffer is not
39*7f2fe78bSCy Schubert  * automatically terminated with a zero byte; call k5_buf_cstring() to use the
40*7f2fe78bSCy Schubert  * contents as a C string.
41*7f2fe78bSCy Schubert  *
42*7f2fe78bSCy Schubert  * k5buf structures are usually stack-allocated.  Do not put k5buf structure
43*7f2fe78bSCy Schubert  * pointers into public APIs.  It is okay to reference the data and len fields
44*7f2fe78bSCy Schubert  * of a buffer (they will be NULL/0 if the buffer is in an error state), but do
45*7f2fe78bSCy Schubert  * not change them.
46*7f2fe78bSCy Schubert  */
47*7f2fe78bSCy Schubert 
48*7f2fe78bSCy Schubert /* Buffer type values */
49*7f2fe78bSCy Schubert enum k5buftype { K5BUF_ERROR, K5BUF_FIXED, K5BUF_DYNAMIC, K5BUF_DYNAMIC_ZAP };
50*7f2fe78bSCy Schubert 
51*7f2fe78bSCy Schubert struct k5buf {
52*7f2fe78bSCy Schubert     enum k5buftype buftype;
53*7f2fe78bSCy Schubert     void *data;
54*7f2fe78bSCy Schubert     size_t space;
55*7f2fe78bSCy Schubert     size_t len;
56*7f2fe78bSCy Schubert };
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert #define EMPTY_K5BUF { K5BUF_ERROR }
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert /* Initialize a k5buf using a fixed-sized, existing buffer.  SPACE must be
61*7f2fe78bSCy Schubert  * more than zero, or an assertion failure will result. */
62*7f2fe78bSCy Schubert void k5_buf_init_fixed(struct k5buf *buf, void *data, size_t space);
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert /* Initialize a k5buf using an internally allocated dynamic buffer. */
65*7f2fe78bSCy Schubert void k5_buf_init_dynamic(struct k5buf *buf);
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert /* Initialize a k5buf using an internally allocated dynamic buffer, zeroing
68*7f2fe78bSCy Schubert  * memory when reallocating or freeing. */
69*7f2fe78bSCy Schubert void k5_buf_init_dynamic_zap(struct k5buf *buf);
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert /* Add a C string to BUF. */
72*7f2fe78bSCy Schubert void k5_buf_add(struct k5buf *buf, const char *data);
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert /* Add a counted series of bytes to BUF. */
75*7f2fe78bSCy Schubert void k5_buf_add_len(struct k5buf *buf, const void *data, size_t len);
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert /* Add sprintf-style formatted data to BUF.  For a fixed-length buffer this
78*7f2fe78bSCy Schubert  * operation will fail if there isn't room for a zero terminator. */
79*7f2fe78bSCy Schubert void k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
80*7f2fe78bSCy Schubert #if !defined(__cplusplus) && (__GNUC__ > 2)
81*7f2fe78bSCy Schubert     __attribute__((__format__(__printf__, 2, 3)))
82*7f2fe78bSCy Schubert #endif
83*7f2fe78bSCy Schubert     ;
84*7f2fe78bSCy Schubert 
85*7f2fe78bSCy Schubert /* Add sprintf-style formatted data to BUF, with a va_list.  The value of ap is
86*7f2fe78bSCy Schubert  * undefined after the call. */
87*7f2fe78bSCy Schubert void k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap)
88*7f2fe78bSCy Schubert #if !defined(__cplusplus) && (__GNUC__ > 2)
89*7f2fe78bSCy Schubert     __attribute__((__format__(__printf__, 2, 0)))
90*7f2fe78bSCy Schubert #endif
91*7f2fe78bSCy Schubert     ;
92*7f2fe78bSCy Schubert 
93*7f2fe78bSCy Schubert /* Without changing the length of buf, ensure that there is a zero byte after
94*7f2fe78bSCy Schubert  * buf.data and return it.  Return NULL on error. */
95*7f2fe78bSCy Schubert char *k5_buf_cstring(struct k5buf *buf);
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert /* Extend the length of buf by len and return a pointer to the reserved space,
98*7f2fe78bSCy Schubert  * to be filled in by the caller.  Return NULL on error. */
99*7f2fe78bSCy Schubert void *k5_buf_get_space(struct k5buf *buf, size_t len);
100*7f2fe78bSCy Schubert 
101*7f2fe78bSCy Schubert /* Truncate BUF.  LEN must be between 0 and the existing buffer
102*7f2fe78bSCy Schubert  * length, or an assertion failure will result. */
103*7f2fe78bSCy Schubert void k5_buf_truncate(struct k5buf *buf, size_t len);
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert /* Return ENOMEM if buf is in an error state, 0 otherwise. */
106*7f2fe78bSCy Schubert int k5_buf_status(struct k5buf *buf);
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert /*
109*7f2fe78bSCy Schubert  * Free the storage used in the dynamic buffer BUF.  The caller may choose to
110*7f2fe78bSCy Schubert  * take responsibility for freeing the data pointer instead of using this
111*7f2fe78bSCy Schubert  * function.  If BUF is a fixed buffer, an assertion failure will result.
112*7f2fe78bSCy Schubert  * Freeing a buffer in the error state, a buffer initialized with EMPTY_K5BUF,
113*7f2fe78bSCy Schubert  * or a zeroed k5buf structure is a no-op.
114*7f2fe78bSCy Schubert  */
115*7f2fe78bSCy Schubert void k5_buf_free(struct k5buf *buf);
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert static inline void
k5_buf_add_byte(struct k5buf * buf,uint8_t val)118*7f2fe78bSCy Schubert k5_buf_add_byte(struct k5buf *buf, uint8_t val)
119*7f2fe78bSCy Schubert {
120*7f2fe78bSCy Schubert     k5_buf_add_len(buf, &val, 1);
121*7f2fe78bSCy Schubert }
122*7f2fe78bSCy Schubert 
123*7f2fe78bSCy Schubert static inline void
k5_buf_add_uint16_be(struct k5buf * buf,uint16_t val)124*7f2fe78bSCy Schubert k5_buf_add_uint16_be(struct k5buf *buf, uint16_t val)
125*7f2fe78bSCy Schubert {
126*7f2fe78bSCy Schubert     void *p = k5_buf_get_space(buf, 2);
127*7f2fe78bSCy Schubert 
128*7f2fe78bSCy Schubert     if (p != NULL)
129*7f2fe78bSCy Schubert         store_16_be(val, p);
130*7f2fe78bSCy Schubert }
131*7f2fe78bSCy Schubert 
132*7f2fe78bSCy Schubert static inline void
k5_buf_add_uint16_le(struct k5buf * buf,uint16_t val)133*7f2fe78bSCy Schubert k5_buf_add_uint16_le(struct k5buf *buf, uint16_t val)
134*7f2fe78bSCy Schubert {
135*7f2fe78bSCy Schubert     void *p = k5_buf_get_space(buf, 2);
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert     if (p != NULL)
138*7f2fe78bSCy Schubert         store_16_le(val, p);
139*7f2fe78bSCy Schubert }
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert static inline void
k5_buf_add_uint32_be(struct k5buf * buf,uint32_t val)142*7f2fe78bSCy Schubert k5_buf_add_uint32_be(struct k5buf *buf, uint32_t val)
143*7f2fe78bSCy Schubert {
144*7f2fe78bSCy Schubert     void *p = k5_buf_get_space(buf, 4);
145*7f2fe78bSCy Schubert 
146*7f2fe78bSCy Schubert     if (p != NULL)
147*7f2fe78bSCy Schubert         store_32_be(val, p);
148*7f2fe78bSCy Schubert }
149*7f2fe78bSCy Schubert 
150*7f2fe78bSCy Schubert static inline void
k5_buf_add_uint32_le(struct k5buf * buf,uint32_t val)151*7f2fe78bSCy Schubert k5_buf_add_uint32_le(struct k5buf *buf, uint32_t val)
152*7f2fe78bSCy Schubert {
153*7f2fe78bSCy Schubert     void *p = k5_buf_get_space(buf, 4);
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     if (p != NULL)
156*7f2fe78bSCy Schubert         store_32_le(val, p);
157*7f2fe78bSCy Schubert }
158*7f2fe78bSCy Schubert 
159*7f2fe78bSCy Schubert static inline void
k5_buf_add_uint64_be(struct k5buf * buf,uint64_t val)160*7f2fe78bSCy Schubert k5_buf_add_uint64_be(struct k5buf *buf, uint64_t val)
161*7f2fe78bSCy Schubert {
162*7f2fe78bSCy Schubert     void *p = k5_buf_get_space(buf, 8);
163*7f2fe78bSCy Schubert 
164*7f2fe78bSCy Schubert     if (p != NULL)
165*7f2fe78bSCy Schubert         store_64_be(val, p);
166*7f2fe78bSCy Schubert }
167*7f2fe78bSCy Schubert 
168*7f2fe78bSCy Schubert static inline void
k5_buf_add_uint64_le(struct k5buf * buf,uint64_t val)169*7f2fe78bSCy Schubert k5_buf_add_uint64_le(struct k5buf *buf, uint64_t val)
170*7f2fe78bSCy Schubert {
171*7f2fe78bSCy Schubert     void *p = k5_buf_get_space(buf, 8);
172*7f2fe78bSCy Schubert 
173*7f2fe78bSCy Schubert     if (p != NULL)
174*7f2fe78bSCy Schubert         store_64_le(val, p);
175*7f2fe78bSCy Schubert }
176*7f2fe78bSCy Schubert 
177*7f2fe78bSCy Schubert #endif /* K5_BUF_H */
178