credentials_metadata.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "src/core/lib/security/credentials/credentials.h"
  34. #include <grpc/support/alloc.h>
  35. #include <string.h>
  36. static void store_ensure_capacity(grpc_credentials_md_store *store) {
  37. if (store->num_entries == store->allocated) {
  38. store->allocated = (store->allocated == 0) ? 1 : store->allocated * 2;
  39. store->entries = gpr_realloc(
  40. store->entries, store->allocated * sizeof(grpc_credentials_md));
  41. }
  42. }
  43. grpc_credentials_md_store *grpc_credentials_md_store_create(
  44. size_t initial_capacity) {
  45. grpc_credentials_md_store *store =
  46. gpr_malloc(sizeof(grpc_credentials_md_store));
  47. memset(store, 0, sizeof(grpc_credentials_md_store));
  48. if (initial_capacity > 0) {
  49. store->entries = gpr_malloc(initial_capacity * sizeof(grpc_credentials_md));
  50. store->allocated = initial_capacity;
  51. }
  52. gpr_ref_init(&store->refcount, 1);
  53. return store;
  54. }
  55. void grpc_credentials_md_store_add(grpc_credentials_md_store *store,
  56. grpc_slice key, grpc_slice value) {
  57. if (store == NULL) return;
  58. store_ensure_capacity(store);
  59. store->entries[store->num_entries].key = grpc_slice_ref(key);
  60. store->entries[store->num_entries].value = grpc_slice_ref(value);
  61. store->num_entries++;
  62. }
  63. void grpc_credentials_md_store_add_cstrings(grpc_credentials_md_store *store,
  64. const char *key,
  65. const char *value) {
  66. if (store == NULL) return;
  67. store_ensure_capacity(store);
  68. store->entries[store->num_entries].key = grpc_slice_from_copied_string(key);
  69. store->entries[store->num_entries].value =
  70. grpc_slice_from_copied_string(value);
  71. store->num_entries++;
  72. }
  73. grpc_credentials_md_store *grpc_credentials_md_store_ref(
  74. grpc_credentials_md_store *store) {
  75. if (store == NULL) return NULL;
  76. gpr_ref(&store->refcount);
  77. return store;
  78. }
  79. void grpc_credentials_md_store_unref(grpc_credentials_md_store *store) {
  80. if (store == NULL) return;
  81. if (gpr_unref(&store->refcount)) {
  82. if (store->entries != NULL) {
  83. size_t i;
  84. for (i = 0; i < store->num_entries; i++) {
  85. grpc_slice_unref(store->entries[i].key);
  86. grpc_slice_unref(store->entries[i].value);
  87. }
  88. gpr_free(store->entries);
  89. }
  90. gpr_free(store);
  91. }
  92. }