base64.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #ifndef GMSSL_BASE64_H
  10. #define GMSSL_BASE64_H
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /*
  17. BASE64 Public API
  18. BASE64_CTX
  19. base64_encode_init
  20. base64_encode_update
  21. base64_encode_finish
  22. base64_decode_init
  23. base64_decode_update
  24. base64_decode_finish
  25. */
  26. typedef struct {
  27. /* number saved in a partial encode/decode */
  28. int num;
  29. /*
  30. * The length is either the output line length (in input bytes) or the
  31. * shortest input line length that is ok. Once decoding begins, the
  32. * length is adjusted up each time a longer line is decoded
  33. */
  34. int length;
  35. /* data to encode */
  36. unsigned char enc_data[80];
  37. /* number read on current line */
  38. int line_num;
  39. int expect_nl;
  40. } BASE64_CTX;
  41. # define BASE64_ENCODE_LENGTH(l) (((l+2)/3*4)+(l/48+1)*2+80)
  42. # define BASE64_DECODE_LENGTH(l) ((l+3)/4*3+80)
  43. void base64_encode_init(BASE64_CTX *ctx);
  44. int base64_encode_update(BASE64_CTX *ctx, const uint8_t *in, int inlen, uint8_t *out, int *outlen);
  45. void base64_encode_finish(BASE64_CTX *ctx, uint8_t *out, int *outlen);
  46. void base64_decode_init(BASE64_CTX *ctx);
  47. int base64_decode_update(BASE64_CTX *ctx, const uint8_t *in, int inlen, uint8_t *out, int *outlen);
  48. int base64_decode_finish(BASE64_CTX *ctx, uint8_t *out, int *outlen);
  49. int base64_encode_block(unsigned char *t, const unsigned char *f, int dlen);
  50. int base64_decode_block(unsigned char *t, const unsigned char *f, int n);
  51. #ifdef __cplusplus
  52. }
  53. #endif
  54. #endif