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