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