misc.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 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. #pragma once
  18. #include "compat.h"
  19. #include <jansson.h>
  20. #include <stdbool.h>
  21. #include <stdint.h>
  22. #define KEYMAX 1024
  23. #define containerof(ptr, type, member) \
  24. ((type *)((char *) ptr - offsetof(type, member)))
  25. #define declare_cleanup_full(type, prfx) \
  26. static inline void \
  27. type ## _auto(type **p) { \
  28. if (!p) return; \
  29. prfx ## _free(*p); \
  30. *p = NULL; \
  31. }
  32. #define declare_cleanup(type) declare_cleanup_full(type, type)
  33. #define openssl_auto(type) type __attribute__((cleanup(type ## _auto)))
  34. declare_cleanup_full(BIGNUM, BN_clear)
  35. size_t
  36. str2enum(const char *str, ...);
  37. BIGNUM *
  38. bn_decode(const uint8_t buf[], size_t len);
  39. BIGNUM *
  40. bn_decode_json(const json_t *json);
  41. bool
  42. bn_encode(const BIGNUM *bn, uint8_t buf[], size_t len);
  43. json_t *
  44. bn_encode_json(const BIGNUM *bn, size_t len);
  45. bool
  46. add_entity(json_t *root, json_t *obj, const char *plural, ...);
  47. bool
  48. copy_val(const json_t *from, json_t *into, ...);