base64_decode.c 842 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "misc.h"
  2. void base64_decode_bs(BinarySink *bs, ptrlen input)
  3. {
  4. BinarySource src[1];
  5. BinarySource_BARE_INIT_PL(src, input);
  6. while (get_avail(src)) {
  7. char b64atom[4];
  8. unsigned char binatom[3];
  9. for (size_t i = 0; i < 4 ;) {
  10. char c = get_byte(src);
  11. if (get_err(src))
  12. c = '=';
  13. if (c == '\n' || c == '\r')
  14. continue;
  15. b64atom[i++] = c;
  16. }
  17. put_data(bs, binatom, base64_decode_atom(b64atom, binatom));
  18. }
  19. }
  20. void base64_decode_fp(FILE *fp, ptrlen input)
  21. {
  22. stdio_sink ss;
  23. stdio_sink_init(&ss, fp);
  24. base64_decode_bs(BinarySink_UPCAST(&ss), input);
  25. }
  26. strbuf *base64_decode_sb(ptrlen input)
  27. {
  28. strbuf *sb = strbuf_new_nm();
  29. base64_decode_bs(BinarySink_UPCAST(sb), input);
  30. return sb;
  31. }