Removed unreachable code.
[BearSSL] / inc / bearssl_pem.h
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 */
24
25 #ifndef BR_BEARSSL_PEM_H__
26 #define BR_BEARSSL_PEM_H__
27
28 #include <stddef.h>
29 #include <stdint.h>
30
31 /*
32 * Context for a PEM decoder.
33 */
34 typedef struct {
35 /* CPU for the T0 virtual machine. */
36 struct {
37 uint32_t *dp;
38 uint32_t *rp;
39 const unsigned char *ip;
40 } cpu;
41 uint32_t dp_stack[32];
42 uint32_t rp_stack[32];
43 int err;
44
45 const unsigned char *hbuf;
46 size_t hlen;
47
48 void (*dest)(void *dest_ctx, const void *src, size_t len);
49 void *dest_ctx;
50
51 unsigned char event;
52 char name[128];
53 unsigned char buf[255];
54 size_t ptr;
55
56 } br_pem_decoder_context;
57
58 /*
59 * Initialise a PEM decoder structure.
60 */
61 void br_pem_decoder_init(br_pem_decoder_context *ctx);
62
63 /*
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.
69 */
70 size_t br_pem_decoder_push(br_pem_decoder_context *ctx,
71 const void *data, size_t len);
72
73 /*
74 * Set the receiver for decoded data. The provided function (with opaque
75 * context pointer) will be called with successive data chunks.
76 */
77 static inline void
78 br_pem_decoder_setdest(br_pem_decoder_context *ctx,
79 void (*dest)(void *dest_ctx, const void *src, size_t len),
80 void *dest_ctx)
81 {
82 ctx->dest = dest;
83 ctx->dest_ctx = dest_ctx;
84 }
85
86 /*
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
89 * processed.
90 */
91 int br_pem_decoder_event(br_pem_decoder_context *ctx);
92
93 /*
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.
98 */
99 #define BR_PEM_BEGIN_OBJ 1
100
101 /*
102 * This event is called when the end of the current object is reached
103 * (normally).
104 */
105 #define BR_PEM_END_OBJ 2
106
107 /*
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
111 * is consumed.
112 */
113 #define BR_PEM_ERROR 3
114
115 /*
116 * Get the name of the encountered object. That name is normalised to
117 * uppercase (for ASCII characters).
118 */
119 static inline const char *
120 br_pem_decoder_name(br_pem_decoder_context *ctx)
121 {
122 return ctx->name;
123 }
124
125 #endif