xref: /freebsd/contrib/libcbor/src/cbor/internal/stack.h (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #ifndef LIBCBOR_STACK_H
9 #define LIBCBOR_STACK_H
10 
11 #include "cbor/common.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /** Simple stack record for the parser */
18 struct _cbor_stack_record {
19   /** Pointer to the parent stack frame */
20   struct _cbor_stack_record *lower;
21   /** Item under construction */
22   cbor_item_t *item;
23   /**
24    * How many outstanding subitems are expected.
25    *
26    * For example, when we see a new definite array, `subitems` is initialized to
27    * the array length. With every item added, the counter is decreased. When it
28    * reaches zero, the stack is popped and the complete item is propagated
29    * upwards.
30    */
31   size_t subitems;
32 };
33 
34 /** Stack handle - contents and size */
35 struct _cbor_stack {
36   struct _cbor_stack_record *top;
37   size_t size;
38 };
39 
40 _CBOR_NODISCARD
41 struct _cbor_stack _cbor_stack_init(void);
42 
43 void _cbor_stack_pop(struct _cbor_stack *);
44 
45 _CBOR_NODISCARD
46 struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *, cbor_item_t *,
47                                             size_t);
48 
49 #ifdef __cplusplus
50 }
51 #endif
52 
53 #endif  // LIBCBOR_STACK_H
54