base64_encode.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "misc.h"
  2. void base64_encode_bs(BinarySink *bs, ptrlen input, int cpl)
  3. {
  4. BinarySource src[1];
  5. BinarySource_BARE_INIT_PL(src, input);
  6. { // WINSCP
  7. int linelen = 0;
  8. while (get_avail(src)) {
  9. size_t n = get_avail(src) < 3 ? get_avail(src) : 3;
  10. ptrlen binatom = get_data(src, n);
  11. char b64atom[4];
  12. base64_encode_atom(binatom.ptr, binatom.len, b64atom);
  13. { // WINSCP
  14. size_t i;
  15. for (i = 0; i < 4; i++) {
  16. if (cpl > 0 && linelen >= cpl) {
  17. linelen = 0;
  18. put_byte(bs, '\n');
  19. }
  20. put_byte(bs, b64atom[i]);
  21. linelen++;
  22. }
  23. } // WINSCP
  24. }
  25. if (cpl > 0)
  26. put_byte(bs, '\n');
  27. } // WINSCP
  28. }
  29. void base64_encode_fp(FILE *fp, ptrlen input, int cpl)
  30. {
  31. stdio_sink ss;
  32. stdio_sink_init(&ss, fp);
  33. base64_encode_bs(BinarySink_UPCAST(&ss), input, cpl);
  34. }
  35. strbuf *base64_encode_sb(ptrlen input, int cpl)
  36. {
  37. strbuf *sb = strbuf_new_nm();
  38. base64_encode_bs(BinarySink_UPCAST(sb), input, cpl);
  39. return sb;
  40. }