xref: /aosp_15_r20/external/selinux/libsepol/cil/src/cil_stack.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker  * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3*2d543d20SAndroid Build Coastguard Worker  *
4*2d543d20SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
5*2d543d20SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions are met:
6*2d543d20SAndroid Build Coastguard Worker  *
7*2d543d20SAndroid Build Coastguard Worker  *    1. Redistributions of source code must retain the above copyright notice,
8*2d543d20SAndroid Build Coastguard Worker  *       this list of conditions and the following disclaimer.
9*2d543d20SAndroid Build Coastguard Worker  *
10*2d543d20SAndroid Build Coastguard Worker  *    2. Redistributions in binary form must reproduce the above copyright notice,
11*2d543d20SAndroid Build Coastguard Worker  *       this list of conditions and the following disclaimer in the documentation
12*2d543d20SAndroid Build Coastguard Worker  *       and/or other materials provided with the distribution.
13*2d543d20SAndroid Build Coastguard Worker  *
14*2d543d20SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15*2d543d20SAndroid Build Coastguard Worker  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16*2d543d20SAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17*2d543d20SAndroid Build Coastguard Worker  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18*2d543d20SAndroid Build Coastguard Worker  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19*2d543d20SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*2d543d20SAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21*2d543d20SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22*2d543d20SAndroid Build Coastguard Worker  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23*2d543d20SAndroid Build Coastguard Worker  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*2d543d20SAndroid Build Coastguard Worker  *
25*2d543d20SAndroid Build Coastguard Worker  * The views and conclusions contained in the software and documentation are those
26*2d543d20SAndroid Build Coastguard Worker  * of the authors and should not be interpreted as representing official policies,
27*2d543d20SAndroid Build Coastguard Worker  * either expressed or implied, of Tresys Technology, LLC.
28*2d543d20SAndroid Build Coastguard Worker  */
29*2d543d20SAndroid Build Coastguard Worker 
30*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
31*2d543d20SAndroid Build Coastguard Worker 
32*2d543d20SAndroid Build Coastguard Worker #include "cil_internal.h"
33*2d543d20SAndroid Build Coastguard Worker #include "cil_log.h"
34*2d543d20SAndroid Build Coastguard Worker #include "cil_mem.h"
35*2d543d20SAndroid Build Coastguard Worker #include "cil_stack.h"
36*2d543d20SAndroid Build Coastguard Worker 
37*2d543d20SAndroid Build Coastguard Worker 
38*2d543d20SAndroid Build Coastguard Worker #define CIL_STACK_INIT_SIZE 16
39*2d543d20SAndroid Build Coastguard Worker 
cil_stack_init(struct cil_stack ** stack)40*2d543d20SAndroid Build Coastguard Worker void cil_stack_init(struct cil_stack **stack)
41*2d543d20SAndroid Build Coastguard Worker {
42*2d543d20SAndroid Build Coastguard Worker 	struct cil_stack *new_stack = cil_malloc(sizeof(*new_stack));
43*2d543d20SAndroid Build Coastguard Worker 	new_stack->stack = cil_malloc(sizeof(*(new_stack->stack)) * CIL_STACK_INIT_SIZE);
44*2d543d20SAndroid Build Coastguard Worker 	new_stack->size = CIL_STACK_INIT_SIZE;
45*2d543d20SAndroid Build Coastguard Worker 	new_stack->pos = -1;
46*2d543d20SAndroid Build Coastguard Worker 	*stack = new_stack;
47*2d543d20SAndroid Build Coastguard Worker }
48*2d543d20SAndroid Build Coastguard Worker 
cil_stack_destroy(struct cil_stack ** stack)49*2d543d20SAndroid Build Coastguard Worker void cil_stack_destroy(struct cil_stack **stack)
50*2d543d20SAndroid Build Coastguard Worker {
51*2d543d20SAndroid Build Coastguard Worker 	if (stack == NULL || *stack == NULL) {
52*2d543d20SAndroid Build Coastguard Worker 		return;
53*2d543d20SAndroid Build Coastguard Worker 	}
54*2d543d20SAndroid Build Coastguard Worker 
55*2d543d20SAndroid Build Coastguard Worker 	free((*stack)->stack);
56*2d543d20SAndroid Build Coastguard Worker 	free(*stack);
57*2d543d20SAndroid Build Coastguard Worker 	*stack = NULL;
58*2d543d20SAndroid Build Coastguard Worker }
59*2d543d20SAndroid Build Coastguard Worker 
cil_stack_empty(struct cil_stack * stack)60*2d543d20SAndroid Build Coastguard Worker void cil_stack_empty(struct cil_stack *stack)
61*2d543d20SAndroid Build Coastguard Worker {
62*2d543d20SAndroid Build Coastguard Worker 	stack->pos = -1;
63*2d543d20SAndroid Build Coastguard Worker }
64*2d543d20SAndroid Build Coastguard Worker 
cil_stack_is_empty(struct cil_stack * stack)65*2d543d20SAndroid Build Coastguard Worker int cil_stack_is_empty(struct cil_stack *stack)
66*2d543d20SAndroid Build Coastguard Worker {
67*2d543d20SAndroid Build Coastguard Worker 	return (stack->pos == -1);
68*2d543d20SAndroid Build Coastguard Worker }
69*2d543d20SAndroid Build Coastguard Worker 
cil_stack_number_of_items(struct cil_stack * stack)70*2d543d20SAndroid Build Coastguard Worker int cil_stack_number_of_items(struct cil_stack *stack)
71*2d543d20SAndroid Build Coastguard Worker {
72*2d543d20SAndroid Build Coastguard Worker 	return stack->pos + 1;
73*2d543d20SAndroid Build Coastguard Worker }
74*2d543d20SAndroid Build Coastguard Worker 
cil_stack_push(struct cil_stack * stack,enum cil_flavor flavor,void * data)75*2d543d20SAndroid Build Coastguard Worker void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data)
76*2d543d20SAndroid Build Coastguard Worker {
77*2d543d20SAndroid Build Coastguard Worker 	stack->pos++;
78*2d543d20SAndroid Build Coastguard Worker 
79*2d543d20SAndroid Build Coastguard Worker 	if (stack->pos == stack->size) {
80*2d543d20SAndroid Build Coastguard Worker 		stack->size *= 2;
81*2d543d20SAndroid Build Coastguard Worker 		stack->stack = cil_realloc(stack->stack, sizeof(*stack->stack) * stack->size);
82*2d543d20SAndroid Build Coastguard Worker 	}
83*2d543d20SAndroid Build Coastguard Worker 
84*2d543d20SAndroid Build Coastguard Worker 	stack->stack[stack->pos].flavor = flavor;
85*2d543d20SAndroid Build Coastguard Worker 	stack->stack[stack->pos].data = data;
86*2d543d20SAndroid Build Coastguard Worker }
87*2d543d20SAndroid Build Coastguard Worker 
cil_stack_pop(struct cil_stack * stack)88*2d543d20SAndroid Build Coastguard Worker struct cil_stack_item *cil_stack_pop(struct cil_stack *stack)
89*2d543d20SAndroid Build Coastguard Worker {
90*2d543d20SAndroid Build Coastguard Worker 	if (stack->pos == -1) {
91*2d543d20SAndroid Build Coastguard Worker 		return NULL;
92*2d543d20SAndroid Build Coastguard Worker 	}
93*2d543d20SAndroid Build Coastguard Worker 
94*2d543d20SAndroid Build Coastguard Worker 	stack->pos--;
95*2d543d20SAndroid Build Coastguard Worker 	return &stack->stack[stack->pos + 1];
96*2d543d20SAndroid Build Coastguard Worker }
97*2d543d20SAndroid Build Coastguard Worker 
cil_stack_peek(struct cil_stack * stack)98*2d543d20SAndroid Build Coastguard Worker struct cil_stack_item *cil_stack_peek(struct cil_stack *stack)
99*2d543d20SAndroid Build Coastguard Worker {
100*2d543d20SAndroid Build Coastguard Worker 	if (stack->pos < 0) {
101*2d543d20SAndroid Build Coastguard Worker 		return NULL;
102*2d543d20SAndroid Build Coastguard Worker 	}
103*2d543d20SAndroid Build Coastguard Worker 
104*2d543d20SAndroid Build Coastguard Worker 	return &stack->stack[stack->pos];
105*2d543d20SAndroid Build Coastguard Worker }
106*2d543d20SAndroid Build Coastguard Worker 
cil_stack_peek_at(struct cil_stack * stack,int pos)107*2d543d20SAndroid Build Coastguard Worker struct cil_stack_item *cil_stack_peek_at(struct cil_stack *stack, int pos)
108*2d543d20SAndroid Build Coastguard Worker {
109*2d543d20SAndroid Build Coastguard Worker 	int peekpos = stack->pos - pos;
110*2d543d20SAndroid Build Coastguard Worker 
111*2d543d20SAndroid Build Coastguard Worker 	if (peekpos < 0 || peekpos > stack->pos) {
112*2d543d20SAndroid Build Coastguard Worker 		return NULL;
113*2d543d20SAndroid Build Coastguard Worker 	}
114*2d543d20SAndroid Build Coastguard Worker 
115*2d543d20SAndroid Build Coastguard Worker 	return &stack->stack[peekpos];
116*2d543d20SAndroid Build Coastguard Worker }
117