Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/security/credentials.h"
35 :
36 : #include <grpc/support/alloc.h>
37 :
38 : #include <string.h>
39 :
40 1180 : static void store_ensure_capacity(grpc_credentials_md_store *store) {
41 1180 : if (store->num_entries == store->allocated) {
42 11 : store->allocated = (store->allocated == 0) ? 1 : store->allocated * 2;
43 22 : store->entries = gpr_realloc(
44 22 : store->entries, store->allocated * sizeof(grpc_credentials_md));
45 : }
46 1180 : }
47 :
48 160 : grpc_credentials_md_store *grpc_credentials_md_store_create(
49 : size_t initial_capacity) {
50 160 : grpc_credentials_md_store *store =
51 : gpr_malloc(sizeof(grpc_credentials_md_store));
52 160 : memset(store, 0, sizeof(grpc_credentials_md_store));
53 160 : if (initial_capacity > 0) {
54 156 : store->entries = gpr_malloc(initial_capacity * sizeof(grpc_credentials_md));
55 156 : store->allocated = initial_capacity;
56 : }
57 160 : gpr_ref_init(&store->refcount, 1);
58 160 : return store;
59 : }
60 :
61 6 : void grpc_credentials_md_store_add(grpc_credentials_md_store *store,
62 : gpr_slice key, gpr_slice value) {
63 12 : if (store == NULL) return;
64 6 : store_ensure_capacity(store);
65 6 : store->entries[store->num_entries].key = gpr_slice_ref(key);
66 6 : store->entries[store->num_entries].value = gpr_slice_ref(value);
67 6 : store->num_entries++;
68 : }
69 :
70 1174 : void grpc_credentials_md_store_add_cstrings(grpc_credentials_md_store *store,
71 : const char *key,
72 : const char *value) {
73 2348 : if (store == NULL) return;
74 1174 : store_ensure_capacity(store);
75 1174 : store->entries[store->num_entries].key = gpr_slice_from_copied_string(key);
76 1174 : store->entries[store->num_entries].value =
77 : gpr_slice_from_copied_string(value);
78 1174 : store->num_entries++;
79 : }
80 :
81 7 : grpc_credentials_md_store *grpc_credentials_md_store_ref(
82 : grpc_credentials_md_store *store) {
83 7 : if (store == NULL) return NULL;
84 7 : gpr_ref(&store->refcount);
85 7 : return store;
86 : }
87 :
88 169 : void grpc_credentials_md_store_unref(grpc_credentials_md_store *store) {
89 338 : if (store == NULL) return;
90 166 : if (gpr_unref(&store->refcount)) {
91 159 : if (store->entries != NULL) {
92 : size_t i;
93 1335 : for (i = 0; i < store->num_entries; i++) {
94 1179 : gpr_slice_unref(store->entries[i].key);
95 1179 : gpr_slice_unref(store->entries[i].value);
96 : }
97 157 : gpr_free(store->entries);
98 : }
99 159 : gpr_free(store);
100 : }
101 : }
|