File: qp-encode.c

package info (click to toggle)
vm 7.19-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,816 kB
  • ctags: 2,098
  • sloc: lisp: 31,152; sh: 322; ansic: 275; makefile: 202
file content (84 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download | duplicates (2)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* public domain */

/*
 * arbitrary data on stdin -> Quoted-Printable data on stdout
 *
 * UNIX's newline convention is used, i.e. one ASCII control-j (10 decimal).
 */

#include <stdio.h>

#ifdef _WIN32
#ifndef WIN32
#define WIN32
#endif
#endif

#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif

char *hexdigits  = "0123456789ABCDEF";

int
main()
{
    int c;
    int cols = 0;

#ifdef WIN32
    _setmode( _fileno(stdout), _O_BINARY);
#endif

    while ((c = getchar()) != EOF) {
	if (c == '\n') {
	    putchar(c);
	    cols = 0;
	} else if (c == ' ') {
	    int nextc;
	    nextc = getchar();
	    if (nextc != '\n' && nextc != EOF) {
		putchar(c);
		cols++;
	    } else {
		putchar('=');
		putchar(hexdigits[c >> 4]);
		putchar(hexdigits[c & 0xf]);
		cols += 3;
	    }
	    if (nextc != EOF)
	      ungetc(nextc, stdin);
	} else if (c < 33 || c > 126 || c == '=' ||
		   /* these are for RFC 2047 Q encoding */
		   c == '?' || c == '_') {
	    putchar('=');
	    putchar(hexdigits[c >> 4]);
	    putchar(hexdigits[c & 0xf]);
	    cols += 3;
	} else if (c == '.' && cols == 0) {
	    int nextc;
	    nextc = getchar();
	    if (nextc == EOF || nextc == '\n') {
		putchar('=');
		putchar(hexdigits[c >> 4]);
		putchar(hexdigits[c & 0xf]);
		cols += 3;
	    } else {
		putchar(c);
		cols++;
	    }
	    if (nextc != EOF)
	      ungetc(nextc, stdin);
	} else {
	    putchar(c);
	    cols++;
	}
	if (cols > 70) {
	    putchar('=');
	    putchar('\n');
	    cols = 0;
	}
    }
    exit(0);
}