b64.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  18. * \brief URL-safe Base64 Encoding & Decoding
  19. * \defgroup jose_b64 Base64
  20. * @{
  21. */
  22. #pragma once
  23. #include "io.h"
  24. #include <jansson.h>
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #define JOSE_B64_MAP "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
  28. /**
  29. * Decodes a URL-safe Base64 JSON string to a buffer.
  30. *
  31. * If \p o is NULL, the number of output bytes necessary is returned.
  32. *
  33. * This function will never write more than \p ol bytes. If the output buffer
  34. * is too small, an error will occur.
  35. *
  36. * \param i The input URL-safe Base64 JSON string.
  37. * \param o The output buffer (may be NULL).
  38. * \param ol The size of the output buffer.
  39. * \return The number of bytes that were (or would be) written.
  40. * If an error occurs, SIZE_MAX is returned.
  41. */
  42. size_t
  43. jose_b64_dec(const json_t *i, void *o, size_t ol);
  44. /**
  45. * Creates a new IO object which performs URL-safe Base64 decoding.
  46. *
  47. * All data written to the returned IO object will be decoded before
  48. * passing it on to the next IO object in the chain.
  49. *
  50. * \param next The next IO object in the chain.
  51. * \return The new IO object or NULL on error.
  52. */
  53. jose_io_t *
  54. jose_b64_dec_io(jose_io_t *next);
  55. /**
  56. * Decodes a URL-safe Base64 buffer to an output buffer.
  57. *
  58. * If \p o is NULL, the number of output bytes necessary is returned.
  59. *
  60. * This function will never write more than \p ol bytes. If the output buffer
  61. * is too small, an error will occur.
  62. *
  63. * \param i The input URL-safe Base64 buffer.
  64. * \param il The size of the data in the input buffer.
  65. * \param o The output buffer.
  66. * \param ol The size of the output buffer.
  67. * \return The number of bytes that were (or would be) written.
  68. * If an error occurs, SIZE_MAX is returned.
  69. */
  70. size_t
  71. jose_b64_dec_buf(const void *i, size_t il, void *o, size_t ol);
  72. /**
  73. * Decodes a JSON string from a URL-safe Base64 JSON string.
  74. *
  75. * \param i The input URL-safe Base64 JSON string containing JSON data.
  76. * \return The output JSON data.
  77. */
  78. json_t *
  79. jose_b64_dec_load(const json_t *i);
  80. /**
  81. * Encodes data to a URL-safe Base64 JSON string.
  82. *
  83. * \param i The input buffer.
  84. * \param il The size of the data in the input buffer.
  85. * \return The decoded JSON data. If an error occurs, NULL is returned.
  86. */
  87. json_t *
  88. jose_b64_enc(const void *i, size_t il);
  89. /**
  90. * Creates a new IO object which performs URL-safe Base64 encoding.
  91. *
  92. * All data written to the returned IO object will be encoded before passing
  93. * it on to the next IO object in the chain.
  94. *
  95. * \param next The next IO object in the chain.
  96. * \return The new IO object or NULL on error.
  97. */
  98. jose_io_t *
  99. jose_b64_enc_io(jose_io_t *next);
  100. /**
  101. * Encodes data to a URL-safe Base64 buffer.
  102. *
  103. * If \p o is NULL, the number of output bytes necessary is returned.
  104. *
  105. * This function will never write more than \p ol bytes. If the output buffer
  106. * is too small, an error will occur.
  107. *
  108. * \param i The input buffer.
  109. * \param il The size of the data in the input buffer.
  110. * \param o The output URL-safe Base64 buffer.
  111. * \param ol The size of the output buffer.
  112. * \return The number of bytes that were (or would be) written.
  113. * If an error occurs, SIZE_MAX is returned.
  114. */
  115. size_t
  116. jose_b64_enc_buf(const void *i, size_t il, void *o, size_t ol);
  117. /**
  118. * Encodes the input JSON as a URL-safe Base64 JSON string.
  119. *
  120. * \param i The input JSON data.
  121. * \return The output URL-safe Base64 JSON string.
  122. */
  123. json_t *
  124. jose_b64_enc_dump(const json_t *i);
  125. /** @} */