Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / krb5 / asn.1 / asn1buf.h
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* Coding Buffer Specifications */
3 #ifndef __ASN1BUF_H__
4 #define __ASN1BUF_H__
5
6 #include "k5-int.h"
7 #include "krbasn1.h"
8
9 typedef struct code_buffer_rep {
10     char *base, *bound, *next;
11 } asn1buf;
12
13
14 /**************** Private Procedures ****************/
15
16 #if (__GNUC__ >= 2) && !defined(CONFIG_SMALL)
17 unsigned int asn1buf_free(const asn1buf *buf);
18 /*
19  * requires  *buf is allocated
20  * effects   Returns the number of unused, allocated octets in *buf.
21  */
22 #define asn1buf_free(buf)                               \
23     (((buf) == NULL || (buf)->base == NULL)             \
24      ? 0U                                               \
25      : (unsigned int)((buf)->bound - (buf)->next + 1))
26
27
28 asn1_error_code asn1buf_ensure_space(asn1buf *buf, const unsigned int amount);
29 /*
30  * requires  *buf is allocated
31  * modifies  *buf
32  * effects  If buf has less than amount octets of free space, then it is
33  *          expanded to have at least amount octets of free space.
34  *          Returns ENOMEM memory is exhausted.
35  */
36 #define asn1buf_ensure_space(buf,amount)                        \
37     ((asn1buf_free(buf) < (amount))                             \
38      ? (asn1buf_expand((buf), (amount)-asn1buf_free(buf)))      \
39      : 0)
40
41 asn1_error_code asn1buf_expand(asn1buf *buf, unsigned int inc);
42 /*
43  * requires  *buf is allocated
44  * modifies  *buf
45  * effects   Expands *buf by allocating space for inc more octets.
46  *            Returns ENOMEM if memory is exhausted.
47  */
48 #endif
49
50 int asn1buf_len(const asn1buf *buf);
51 /*
52  * requires  *buf is allocated
53  * effects   Returns the length of the encoding in *buf.
54  */
55 #define asn1buf_len(buf)        ((buf)->next - (buf)->base)
56
57 /****** End of private procedures *****/
58
59 /*
60  * Overview
61  *
62  *  The coding buffer is an array of char (to match a krb5_data structure)
63  *   with 3 reference pointers:
64  *   1) base - The bottom of the octet array.  Used for memory management
65  *             operations on the array (e.g. alloc, realloc, free).
66  *   2) next - Points to the next available octet position in the array.
67  *             During encoding, this is the next free position, and it
68  *               advances as octets are added to the array.
69  *             During decoding, this is the next unread position, and it
70  *               advances as octets are read from the array.
71  *   3) bound - Points to the top of the array. Used for bounds-checking.
72  *
73  *  All pointers to encoding buffers should be initalized to NULL.
74  *
75  * Operations
76  *
77  *  asn1buf_create
78  *  asn1buf_wrap_data
79  *  asn1buf_destroy
80  *  asn1buf_insert_octet
81  *  asn1buf_insert_charstring
82  *  asn1buf_remove_octet
83  *  asn1buf_remove_charstring
84  *  asn1buf_unparse
85  *  asn1buf_hex_unparse
86  *  asn12krb5_buf
87  *  asn1buf_remains
88  *
89  *  (asn1buf_size)
90  *  (asn1buf_free)
91  *  (asn1buf_ensure_space)
92  *  (asn1buf_expand)
93  *  (asn1buf_len)
94  */
95
96 asn1_error_code asn1buf_create(asn1buf **buf);
97 /*
98  * effects   Creates a new encoding buffer pointed to by *buf.
99  *           Returns ENOMEM if the buffer can't be created.
100  */
101
102 void asn1buf_destroy(asn1buf **buf);
103 /* effects   Deallocates **buf, sets *buf to NULL. */
104
105 /*
106  * requires  *buf is allocated
107  * effects   Inserts o into the buffer *buf, expanding the buffer if
108  *           necessary.  Returns ENOMEM memory is exhausted.
109  */
110 #if ((__GNUC__ >= 2) && !defined(ASN1BUF_OMIT_INLINE_FUNCS)) && !defined(CONFIG_SMALL)
111 static inline asn1_error_code
112 asn1buf_insert_octet(asn1buf *buf, const int o)
113 {
114     asn1_error_code retval;
115
116     retval = asn1buf_ensure_space(buf,1U);
117     if (retval) return retval;
118     *(buf->next) = (char)o;
119     (buf->next)++;
120     return 0;
121 }
122 #else
123 asn1_error_code asn1buf_insert_octet(asn1buf *buf, const int o);
124 #endif
125
126 asn1_error_code
127 asn1buf_insert_bytestring(
128     asn1buf *buf,
129     const unsigned int len,
130     const void *s);
131 /*
132  * requires  *buf is allocated
133  * modifies  *buf
134  * effects   Inserts the contents of s (an array of length len)
135  *            into the buffer *buf, expanding the buffer if necessary.
136  *           Returns ENOMEM if memory is exhausted.
137  */
138
139 #define asn1buf_insert_octetstring asn1buf_insert_bytestring
140
141 asn1_error_code asn12krb5_buf(const asn1buf *buf, krb5_data **code);
142 /*
143  * modifies  *code
144  * effects   Instantiates **code with the krb5_data representation of **buf.
145  */
146
147 #endif