1 #pragma ident "%Z%%M% %I% %E% SMI"
3 /* Coding Buffer Specifications */
10 typedef struct code_buffer_rep
{
11 char *base
, *bound
, *next
;
15 /**************** Private Procedures ****************/
19 /* requires *buf has been created and not destroyed
20 effects Returns the total size
21 (in octets) of buf's octet buffer. */
22 #define asn1buf_size(buf) \
23 (((buf) == NULL || (buf)->base == NULL) \
25 : ((buf)->bound - (buf)->base + 1))
29 /* requires *buf is allocated
30 effects Returns the number of unused, allocated octets in *buf. */
31 #define asn1buf_free(buf) \
32 (((buf) == NULL || (buf)->base == NULL) \
34 : ((buf)->bound - (buf)->next + 1))
37 asn1_error_code asn1buf_ensure_space
38 (asn1buf
*buf
, const unsigned int amount
);
39 /* requires *buf is allocated
41 effects If buf has less than amount octets of free space, then it is
42 expanded to have at least amount octets of free space.
43 Returns ENOMEM memory is exhausted. */
44 #define asn1buf_ensure_space(buf,amount) \
45 ((asn1buf_free(buf) < (amount)) \
46 ? (asn1buf_expand((buf), (amount)-asn1buf_free(buf))) \
50 asn1_error_code asn1buf_expand
51 (asn1buf
*buf
, unsigned int inc
);
52 /* requires *buf is allocated
54 effects Expands *buf by allocating space for inc more octets.
55 Returns ENOMEM if memory is exhausted. */
59 /* requires *buf is allocated
60 effects Returns the length of the encoding in *buf. */
61 #define asn1buf_len(buf) ((buf)->next - (buf)->base)
63 /****** End of private procedures *****/
68 The coding buffer is an array of char (to match a krb5_data structure)
69 with 3 reference pointers:
70 1) base - The bottom of the octet array. Used for memory management
71 operations on the array (e.g. alloc, realloc, free).
72 2) next - Points to the next available octet position in the array.
73 During encoding, this is the next free position, and it
74 advances as octets are added to the array.
75 During decoding, this is the next unread position, and it
76 advances as octets are read from the array.
77 3) bound - Points to the top of the array. Used for bounds-checking.
79 All pointers to encoding buffers should be initalized to NULL.
87 asn1buf_insert_charstring
89 asn1buf_remove_charstring
97 (asn1buf_ensure_space)
102 asn1_error_code asn1buf_create
104 /* effects Creates a new encoding buffer pointed to by *buf.
105 Returns ENOMEM if the buffer can't be created. */
107 asn1_error_code asn1buf_wrap_data
108 (asn1buf
*buf
, const krb5_data
*code
);
109 /* requires *buf has already been allocated
110 effects Turns *buf into a "wrapper" for *code. i.e. *buf is set up
111 such that its bottom is the beginning of *code, and its top
113 Returns ASN1_MISSING_FIELD if code is empty. */
115 asn1_error_code asn1buf_imbed
116 (asn1buf
*subbuf
, const asn1buf
*buf
,
117 const unsigned int length
,
119 /* requires *subbuf and *buf are allocated
120 effects *subbuf becomes a sub-buffer of *buf. *subbuf begins
121 at *buf's current position and is length octets long.
122 (Unless this would exceed the bounds of *buf -- in
123 that case, ASN1_OVERRUN is returned) *subbuf's current
124 position starts at the beginning of *subbuf. */
126 asn1_error_code asn1buf_sync
127 (asn1buf
*buf
, asn1buf
*subbuf
, const asn1_class Class
,
128 const asn1_tagnum lasttag
,
129 const unsigned int length
, const int indef
,
131 /* requires *subbuf is a sub-buffer of *buf, as created by asn1buf_imbed.
132 lasttag is the last tagnumber read.
133 effects Synchronizes *buf's current position to match that of *subbuf. */
135 asn1_error_code asn1buf_skiptail
136 (asn1buf
*buf
, const unsigned int length
,
138 /* requires *buf is a subbuffer used in a decoding of a
139 constructed indefinite sequence.
140 effects skips trailing fields. */
142 asn1_error_code asn1buf_destroy
144 /* effects Deallocates **buf, sets *buf to NULL. */
146 asn1_error_code asn1buf_insert_octet
147 (asn1buf
*buf
, const int o
);
148 /* requires *buf is allocated
149 effects Inserts o into the buffer *buf, expanding the buffer if
150 necessary. Returns ENOMEM memory is exhausted. */
151 #if ((__GNUC__ >= 2) && !defined(ASN1BUF_OMIT_INLINE_FUNCS))
152 extern __inline__ asn1_error_code
asn1buf_insert_octet(asn1buf
*buf
, const int o
)
154 asn1_error_code retval
;
156 retval
= asn1buf_ensure_space(buf
,1U);
157 if(retval
) return retval
;
158 *(buf
->next
) = (char)o
;
164 asn1_error_code asn1buf_insert_octetstring
165 (asn1buf
*buf
, const unsigned int len
, const asn1_octet
*s
);
166 /* requires *buf is allocated
168 effects Inserts the contents of s (an octet array of length len)
169 into the buffer *buf, expanding the buffer if necessary.
170 Returns ENOMEM if memory is exhausted. */
172 asn1_error_code asn1buf_insert_charstring
173 (asn1buf
*buf
, const unsigned int len
, const char *s
);
174 /* requires *buf is allocated
176 effects Inserts the contents of s (a character array of length len)
177 into the buffer *buf, expanding the buffer if necessary.
178 Returns ENOMEM if memory is exhausted. */
180 asn1_error_code asn1buf_remove_octet
181 (asn1buf
*buf
, asn1_octet
*o
);
182 /* requires *buf is allocated
183 effects Returns *buf's current octet in *o and advances to
185 Returns ASN1_OVERRUN if *buf has already been exhausted. */
186 #define asn1buf_remove_octet(buf,o) \
187 (((buf)->next > (buf)->bound) \
189 : ((*(o) = (asn1_octet)(*(((buf)->next)++))),0))
191 asn1_error_code asn1buf_remove_octetstring
192 (asn1buf
*buf
, const unsigned int len
, asn1_octet
**s
);
193 /* requires *buf is allocated
194 effects Removes the next len octets of *buf and returns them in **s.
195 Returns ASN1_OVERRUN if there are fewer than len unread octets
197 Returns ENOMEM if *s could not be allocated. */
199 asn1_error_code asn1buf_remove_charstring
200 (asn1buf
*buf
, const unsigned int len
,
202 /* requires *buf is allocated
203 effects Removes the next len octets of *buf and returns them in **s.
204 Returns ASN1_OVERRUN if there are fewer than len unread octets
206 Returns ENOMEM if *s could not be allocated. */
208 asn1_error_code asn1buf_unparse
209 (const asn1buf
*buf
, char **s
);
211 effects Returns a human-readable representation of *buf in *s,
212 where each octet in *buf is represented by a character in *s. */
214 asn1_error_code asn1buf_hex_unparse
215 (const asn1buf
*buf
, char **s
);
217 effects Returns a human-readable representation of *buf in *s,
218 where each octet in *buf is represented by a 2-digit
219 hexadecimal number in *s. */
221 asn1_error_code asn12krb5_buf
222 (const asn1buf
*buf
, krb5_data
**code
);
224 effects Instantiates **code with the krb5_data representation of **buf. */
228 (asn1buf
*buf
, int indef
);
229 /* requires *buf is a buffer containing an asn.1 structure or array
231 effects Returns the number of unprocessed octets remaining in *buf. */