base64_decode.c 933 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. { // WINSCP
  10. size_t i;
  11. for (i = 0; i < 4 ;) {
  12. char c = get_byte(src);
  13. if (get_err(src))
  14. c = '=';
  15. if (c == '\n' || c == '\r')
  16. continue;
  17. b64atom[i++] = c;
  18. }
  19. } // WINSCP
  20. put_data(bs, binatom, base64_decode_atom(b64atom, binatom));
  21. }
  22. }
  23. void base64_decode_fp(FILE *fp, ptrlen input)
  24. {
  25. stdio_sink ss;
  26. stdio_sink_init(&ss, fp);
  27. base64_decode_bs(BinarySink_UPCAST(&ss), input);
  28. }
  29. strbuf *base64_decode_sb(ptrlen input)
  30. {
  31. strbuf *sb = strbuf_new_nm();
  32. base64_decode_bs(BinarySink_UPCAST(sb), input);
  33. return sb;
  34. }