1// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "private-lib-core.h"
16
17#include "ssl_stack.h"
18#include "ssl_dbg.h"
19#include "ssl_port.h"
20
21#ifndef CONFIG_MIN_NODES
22    #define MIN_NODES 4
23#else
24    #define MIN_NODES CONFIG_MIN_NODES
25#endif
26
27/**
28 * @brief create a openssl stack object
29 */
30OPENSSL_STACK* OPENSSL_sk_new(OPENSSL_sk_compfunc c)
31{
32    OPENSSL_STACK *stack;
33    char **data;
34
35    stack = ssl_mem_zalloc(sizeof(OPENSSL_STACK));
36    if (!stack) {
37        SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (stack)");
38        goto no_mem1;
39    }
40
41    data = ssl_mem_zalloc(sizeof(*data) * MIN_NODES);
42    if (!data) {
43        SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (data)");
44        goto no_mem2;
45    }
46
47    stack->data = data;
48    stack->num_alloc = MIN_NODES;
49    stack->c = c;
50
51    return stack;
52
53no_mem2:
54    ssl_mem_free(stack);
55no_mem1:
56    return NULL;
57}
58
59/**
60 * @brief create a NULL function openssl stack object
61 */
62OPENSSL_STACK *OPENSSL_sk_new_null(void)
63{
64    return OPENSSL_sk_new((OPENSSL_sk_compfunc)NULL);
65}
66
67/**
68 * @brief free openssl stack object
69 */
70void OPENSSL_sk_free(OPENSSL_STACK *stack)
71{
72    SSL_ASSERT3(stack);
73
74    ssl_mem_free(stack->data);
75    ssl_mem_free(stack);
76}
77