2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #ifndef BR_BEARSSL_PEM_H__
26 #define BR_BEARSSL_PEM_H__
32 * Context for a PEM decoder.
35 /* CPU for the T0 virtual machine. */
39 const unsigned char *ip
;
41 uint32_t dp_stack
[32];
42 uint32_t rp_stack
[32];
45 const unsigned char *hbuf
;
48 void (*dest
)(void *dest_ctx
, const void *src
, size_t len
);
53 unsigned char buf
[255];
56 } br_pem_decoder_context
;
59 * Initialise a PEM decoder structure.
61 void br_pem_decoder_init(br_pem_decoder_context
*ctx
);
64 * Push some bytes into the decoder. Returned value is the number of
65 * bytes actually consumed; this may be less than the number of provided
66 * bytes if an event is produced. When an event is produced, it must
67 * be read (with br_pem_decoder_event()); until the event is read, this
68 * function will return 0.
70 size_t br_pem_decoder_push(br_pem_decoder_context
*ctx
,
71 const void *data
, size_t len
);
74 * Set the receiver for decoded data. The provided function (with opaque
75 * context pointer) will be called with successive data chunks.
78 br_pem_decoder_setdest(br_pem_decoder_context
*ctx
,
79 void (*dest
)(void *dest_ctx
, const void *src
, size_t len
),
83 ctx
->dest_ctx
= dest_ctx
;
87 * Get the last event. This is 0 if no event has been produced. Calling
88 * ths function clears the event and allows new source bytes to be
91 int br_pem_decoder_event(br_pem_decoder_context
*ctx
);
94 * This event is called when the start of a new object has been detected.
95 * The object name (normalised to uppercase) can be accessed with
96 * br_pem_decoder_name(). The caller MUST provide an appropriate receiver
97 * (with br_pem_decoder_setdest()) before sending new data bytes.
99 #define BR_PEM_BEGIN_OBJ 1
102 * This event is called when the end of the current object is reached
105 #define BR_PEM_END_OBJ 2
108 * This event is called when decoding fails while decoding an object.
109 * This formally closes the current object and brings the decoder back
110 * to the "out of any object" state. The offending line in the source
113 #define BR_PEM_ERROR 3
116 * Get the name of the encountered object. That name is normalised to
117 * uppercase (for ASCII characters).
119 static inline const char *
120 br_pem_decoder_name(br_pem_decoder_context
*ctx
)