Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 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 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 34 | #include "src/core/lib/security/credentials/credentials.h" |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 35 | |
| 36 | #include <grpc/support/alloc.h> |
| 37 | |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 40 | #include "src/core/lib/slice/slice_internal.h" |
| 41 | |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 42 | static void store_ensure_capacity(grpc_credentials_md_store *store) { |
| 43 | if (store->num_entries == store->allocated) { |
| 44 | store->allocated = (store->allocated == 0) ? 1 : store->allocated * 2; |
| 45 | store->entries = gpr_realloc( |
| 46 | store->entries, store->allocated * sizeof(grpc_credentials_md)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | grpc_credentials_md_store *grpc_credentials_md_store_create( |
| 51 | size_t initial_capacity) { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 52 | grpc_credentials_md_store *store = |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 53 | gpr_zalloc(sizeof(grpc_credentials_md_store)); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 54 | if (initial_capacity > 0) { |
| 55 | store->entries = gpr_malloc(initial_capacity * sizeof(grpc_credentials_md)); |
| 56 | store->allocated = initial_capacity; |
| 57 | } |
| 58 | gpr_ref_init(&store->refcount, 1); |
| 59 | return store; |
| 60 | } |
| 61 | |
| 62 | void grpc_credentials_md_store_add(grpc_credentials_md_store *store, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 63 | grpc_slice key, grpc_slice value) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 64 | if (store == NULL) return; |
| 65 | store_ensure_capacity(store); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 66 | store->entries[store->num_entries].key = grpc_slice_ref_internal(key); |
| 67 | store->entries[store->num_entries].value = grpc_slice_ref_internal(value); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 68 | store->num_entries++; |
| 69 | } |
| 70 | |
| 71 | void grpc_credentials_md_store_add_cstrings(grpc_credentials_md_store *store, |
| 72 | const char *key, |
| 73 | const char *value) { |
| 74 | if (store == NULL) return; |
| 75 | store_ensure_capacity(store); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 76 | store->entries[store->num_entries].key = grpc_slice_from_copied_string(key); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 77 | store->entries[store->num_entries].value = |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 78 | grpc_slice_from_copied_string(value); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 79 | store->num_entries++; |
| 80 | } |
| 81 | |
| 82 | grpc_credentials_md_store *grpc_credentials_md_store_ref( |
| 83 | grpc_credentials_md_store *store) { |
| 84 | if (store == NULL) return NULL; |
| 85 | gpr_ref(&store->refcount); |
| 86 | return store; |
| 87 | } |
| 88 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 89 | void grpc_credentials_md_store_unref(grpc_exec_ctx *exec_ctx, |
| 90 | grpc_credentials_md_store *store) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 91 | if (store == NULL) return; |
| 92 | if (gpr_unref(&store->refcount)) { |
| 93 | if (store->entries != NULL) { |
| 94 | size_t i; |
| 95 | for (i = 0; i < store->num_entries; i++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 96 | grpc_slice_unref_internal(exec_ctx, store->entries[i].key); |
| 97 | grpc_slice_unref_internal(exec_ctx, store->entries[i].value); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 98 | } |
| 99 | gpr_free(store->entries); |
| 100 | } |
| 101 | gpr_free(store); |
| 102 | } |
| 103 | } |