cfg.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  18. * \brief José Configuration
  19. * \defgroup jose_cfg Config
  20. * @{
  21. */
  22. #pragma once
  23. #include <stddef.h>
  24. #include <stdarg.h>
  25. #include <stdint.h>
  26. enum {
  27. _JOSE_CFG_ERR_BASE = 0x1053000000000000ULL,
  28. JOSE_CFG_ERR_JWK_INVALID,
  29. JOSE_CFG_ERR_JWK_MISMATCH,
  30. JOSE_CFG_ERR_JWK_DENIED,
  31. JOSE_CFG_ERR_ALG_NOTSUP,
  32. JOSE_CFG_ERR_ALG_NOINFER,
  33. JOSE_CFG_ERR_JWS_INVALID,
  34. };
  35. #ifdef DOXYGEN
  36. /**
  37. * Defines a jose_cfg_t which calls jose_cfg_decref() at end of scope.
  38. *
  39. * For example:
  40. *
  41. * void foo() {
  42. * jose_cfg_auto_t *cfg = jose_cfg();
  43. * // jose_cfg_decref() implicitly called
  44. * }
  45. */
  46. typedef jose_cfg_t jose_cfg_auto_t;
  47. #else
  48. #define jose_cfg_auto_t jose_cfg_t __attribute__((cleanup(jose_cfg_auto)))
  49. #endif
  50. typedef struct jose_cfg jose_cfg_t;
  51. typedef void (jose_cfg_err_t)(void *misc, const char *file, int line,
  52. uint64_t err, const char *fmt, va_list ap);
  53. /**
  54. * Creates a new configuration instance.
  55. *
  56. * \return A newly-allocated configuration instance.
  57. */
  58. jose_cfg_t *
  59. jose_cfg(void);
  60. void
  61. jose_cfg_auto(jose_cfg_t **cfg);
  62. /**
  63. * Increases the reference count of a configuration instance.
  64. *
  65. * This function always succeeds.
  66. *
  67. * \param cfg The configuration context.
  68. * \return The value of \p cfg (for convenience).
  69. */
  70. jose_cfg_t *
  71. jose_cfg_incref(jose_cfg_t *cfg);
  72. /**
  73. * Decreases the reference count of a configuration instance.
  74. *
  75. * When the reference count reaches zero, the configuration instance is freed.
  76. *
  77. * \param cfg The configuration context.
  78. */
  79. void
  80. jose_cfg_decref(jose_cfg_t *cfg);
  81. /**
  82. * Sets the error handler function for this configuration instance.
  83. *
  84. * The value of \p misc will be passed to the error handler function.
  85. *
  86. * You may pass NULL to \p err to return to the default error handler.
  87. *
  88. * \param cfg The configuration context.
  89. * \param err The error handler function you wish to enable.
  90. * \param misc The miscellaneous data you wish to pass to the error handler.
  91. */
  92. void
  93. jose_cfg_set_err_func(jose_cfg_t *cfg, jose_cfg_err_t *err, void *misc);
  94. /**
  95. * Gets the miscellaneous data associated with the current error handler.
  96. *
  97. * \param cfg The configuration context.
  98. * \return The miscellaneous data associated with the error handler.
  99. */
  100. void *
  101. jose_cfg_get_err_misc(jose_cfg_t *cfg);
  102. #ifdef DOXYGEN
  103. /**
  104. * Submit an error.
  105. *
  106. * The error handler will be called with the error provided.
  107. *
  108. * \param cfg The configuration context (optional).
  109. * \param err The number corresponding to this error type.
  110. * \param fmt A printf()-style format string.
  111. * \param ... The printf()-style arguments.
  112. */
  113. void
  114. jose_cfg_err(jose_cfg_t *cfg, uint64_t err, const char *fmt, ...);
  115. #else
  116. void __attribute__((format(printf, 5, 6)))
  117. jose_cfg_err(jose_cfg_t *cfg, const char *file, int line, uint64_t err,
  118. const char *fmt, ...);
  119. #define jose_cfg_err(cfg, err, ...) \
  120. jose_cfg_err(cfg, __FILE__, __LINE__, err, __VA_ARGS__)
  121. #endif
  122. /** @} */