110ff414cSEd Maste /*
210ff414cSEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste  *
410ff414cSEd Maste  * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste  * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste  */
710ff414cSEd Maste 
810ff414cSEd Maste #ifndef LIBCBOR_STACK_H
910ff414cSEd Maste #define LIBCBOR_STACK_H
1010ff414cSEd Maste 
1110ff414cSEd Maste #include "cbor/common.h"
1210ff414cSEd Maste 
1310ff414cSEd Maste #ifdef __cplusplus
1410ff414cSEd Maste extern "C" {
1510ff414cSEd Maste #endif
1610ff414cSEd Maste 
1710ff414cSEd Maste /** Simple stack record for the parser */
1810ff414cSEd Maste struct _cbor_stack_record {
19*5d3e7166SEd Maste   /** Pointer to the parent stack frame */
2010ff414cSEd Maste   struct _cbor_stack_record *lower;
21*5d3e7166SEd Maste   /** Item under construction */
2210ff414cSEd Maste   cbor_item_t *item;
23*5d3e7166SEd Maste   /**
24*5d3e7166SEd Maste    * How many outstanding subitems are expected.
25*5d3e7166SEd Maste    *
26*5d3e7166SEd Maste    * For example, when we see a new definite array, `subitems` is initialized to
27*5d3e7166SEd Maste    * the array length. With every item added, the counter is decreased. When it
28*5d3e7166SEd Maste    * reaches zero, the stack is popped and the complete item is propagated
29*5d3e7166SEd Maste    * upwards.
30*5d3e7166SEd Maste    */
3110ff414cSEd Maste   size_t subitems;
3210ff414cSEd Maste };
3310ff414cSEd Maste 
3410ff414cSEd Maste /** Stack handle - contents and size */
3510ff414cSEd Maste struct _cbor_stack {
3610ff414cSEd Maste   struct _cbor_stack_record *top;
3710ff414cSEd Maste   size_t size;
3810ff414cSEd Maste };
3910ff414cSEd Maste 
40*5d3e7166SEd Maste _CBOR_NODISCARD
41*5d3e7166SEd Maste struct _cbor_stack _cbor_stack_init(void);
4210ff414cSEd Maste 
4310ff414cSEd Maste void _cbor_stack_pop(struct _cbor_stack *);
4410ff414cSEd Maste 
45*5d3e7166SEd Maste _CBOR_NODISCARD
4610ff414cSEd Maste struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *, cbor_item_t *,
4710ff414cSEd Maste                                             size_t);
4810ff414cSEd Maste 
4910ff414cSEd Maste #ifdef __cplusplus
5010ff414cSEd Maste }
5110ff414cSEd Maste #endif
5210ff414cSEd Maste 
5310ff414cSEd Maste #endif  // LIBCBOR_STACK_H
54