Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

50 lines
1.2 KiB

  1. #include "fmt.h"
  2. #include "textcode.h"
  3. size_t fmt_yenc(char* dest,const char* src,size_t len) {
  4. register const unsigned char* s=(const unsigned char*) src;
  5. size_t written=0,i;
  6. int linelen=0;
  7. for (i=0; i<len; ++i) {
  8. unsigned char c=s[i]+42;
  9. switch (c) {
  10. case ' ': /* escape space at line ending */
  11. if (linelen==253) {
  12. if (dest) dest[written]='\n';
  13. ++written; linelen=0;
  14. }
  15. goto dontescape;
  16. case 'F': /* escape "^From " */
  17. if (s[i+1]+42!='r' || s[i+2]+42!='o' || s[i+3]+42!='m' || s[i+4]+42!=' ') goto dontescape;
  18. /* fall through */
  19. case '.': /* dot at start of line needs to be escaped */
  20. if (!linelen) goto dontescape;
  21. /* fall through */
  22. case 0:
  23. case '\n':
  24. case '\r':
  25. case '=':
  26. if (dest) dest[written]='=';
  27. ++written;
  28. c+=64;
  29. /* fall through */
  30. default:
  31. dontescape:
  32. ++linelen;
  33. if (dest) dest[written]=c;
  34. ++written;
  35. if (linelen>253) {
  36. if (dest) dest[written]='\n';
  37. ++written; linelen=0;
  38. }
  39. }
  40. /* in case someone gives us malicious input */
  41. if (written>((size_t)-1)/2) return (size_t)-1;
  42. }
  43. if (linelen) {
  44. if (dest) dest[written]='\n';
  45. ++written;
  46. }
  47. return written;
  48. }