1
0

cfg.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <jose/cfg.h>
  18. #undef jose_cfg_err
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. struct jose_cfg {
  24. size_t refs;
  25. jose_cfg_err_t *err;
  26. void *misc;
  27. };
  28. static struct {
  29. uint64_t nmbr;
  30. const char *name;
  31. } errnames[] = {
  32. #define XX(n) { n, # n }
  33. XX(JOSE_CFG_ERR_JWK_INVALID),
  34. XX(JOSE_CFG_ERR_JWK_MISMATCH),
  35. XX(JOSE_CFG_ERR_JWK_DENIED),
  36. XX(JOSE_CFG_ERR_ALG_NOTSUP),
  37. XX(JOSE_CFG_ERR_ALG_NOINFER),
  38. XX(JOSE_CFG_ERR_JWS_INVALID),
  39. #undef XX
  40. {}
  41. };
  42. static const char *
  43. getname(uint64_t err)
  44. {
  45. if (err < _JOSE_CFG_ERR_BASE)
  46. return strerror(err);
  47. for (size_t i = 0; errnames[i].name; i++) {
  48. if (errnames[i].nmbr == err)
  49. return errnames[i].name;
  50. }
  51. return "UNKNOWN";
  52. }
  53. static void
  54. dflt_err(void *misc, const char *file, int line, uint64_t err,
  55. const char *fmt, va_list ap)
  56. {
  57. fprintf(stderr, "%s:%d:", file, line);
  58. if (err != 0)
  59. fprintf(stderr, "%s:", getname(err));
  60. vfprintf(stderr, fmt, ap);
  61. fprintf(stderr, "\n");
  62. }
  63. static const jose_cfg_t dflt = { .err = dflt_err };
  64. jose_cfg_t *
  65. jose_cfg(void)
  66. {
  67. jose_cfg_t *cfg = NULL;
  68. cfg = calloc(1, sizeof(*cfg));
  69. if (cfg)
  70. *cfg = dflt;
  71. return jose_cfg_incref(cfg);
  72. }
  73. void
  74. jose_cfg_auto(jose_cfg_t **cfg)
  75. {
  76. if (cfg)
  77. jose_cfg_decref(*cfg);
  78. }
  79. jose_cfg_t *
  80. jose_cfg_incref(jose_cfg_t *cfg)
  81. {
  82. if (cfg)
  83. cfg->refs++;
  84. return cfg;
  85. }
  86. void
  87. jose_cfg_decref(jose_cfg_t *cfg)
  88. {
  89. if (cfg->refs-- == 1)
  90. free(cfg);
  91. }
  92. void
  93. jose_cfg_set_err_func(jose_cfg_t *cfg, jose_cfg_err_t *err, void *misc)
  94. {
  95. cfg->err = err ? err : dflt.err;
  96. cfg->misc = misc;
  97. }
  98. void *
  99. jose_cfg_get_err_misc(jose_cfg_t *cfg)
  100. {
  101. return cfg->err;
  102. }
  103. void
  104. jose_cfg_err(jose_cfg_t *cfg, const char *file, int line, uint64_t err,
  105. const char *fmt, ...)
  106. {
  107. const jose_cfg_t *c = cfg ? cfg : &dflt;
  108. va_list ap;
  109. va_start(ap, fmt);
  110. c->err(c->misc, file, line, err, fmt, ap);
  111. va_end(ap);
  112. }