File: qp-decode.c

package info (click to toggle)
vm 7.19-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,876 kB
  • ctags: 2,117
  • sloc: lisp: 31,154; sh: 354; ansic: 292; makefile: 202
file content (99 lines) | stat: -rw-r--r-- 2,154 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* public domain */

/* Quoted Printable on stdin -> converted data on stdout */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

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

const char *hexdigits  = "0123456789ABCDEF";
const char *hexdigits2 = "0123456789abcdef";

int
main(void)
{
    char line[2000], *start, *stop, *copy;
    char *d1, *d2, c;
    int lineno;

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

    line[sizeof line - 1] = 0;
    lineno = 1;
    while (fgets(line, sizeof line - 1, stdin)) {
	lineno++;
	start = line;
      keep_processing:
	for (stop = start; *stop && *stop != '=' && *stop != '\n'; stop++)
	  ;
	if (stop != line && *stop == '\n') {
	    copy = stop;
	    do {
		copy--;
		if (*copy != ' ' && *copy != '\t') {
		    copy++;
		    break;
		}
	    } while (copy != line);
	} else {
	    copy = stop;
	}
	while (start != copy) {
	    putchar(*start);
	    start++;
	}
	if (*stop == '\n') {
	    putchar(*stop);
	    lineno++;
	    continue;
	} else if (*stop == 0) {
	    continue;
	} else { /* *stop == '=' */
	    stop++;
	    if ((d1 = strchr(hexdigits, *(stop))) &&
		(d2 = strchr(hexdigits, *(stop+1)))) {
		c = (d1 - hexdigits) * 16 + (d2 - hexdigits);
		putchar(c);
		stop += 2;
	    } else if ((d1 = strchr(hexdigits2, *(stop))) &&
		       (d2 = strchr(hexdigits2, *(stop+1)))) {
		c = (d1 - hexdigits2) * 16 + (d2 - hexdigits2);
		putchar(c);
		stop += 2;
	    } else if (*stop == '\n') {
		/* soft line break */
		stop++;
	    } else if (*stop == '\r') {
		/*
		 * Assume the user's lousy delivery software
		 * didn't convert from Internet's CRLF newline
		 * convention to the local LF convention.
		 */
		stop++;
	    } else if (*stop == ' ' || *stop == '\t') {
	      /* garbage added in transit */
	      for (stop++; *stop && (*stop == ' ' || *stop == '\t'); stop++)
		;
	    } else {
		fprintf(stderr, "qp-decode:line %d: something other than line break or hex digits after = in quoted-printable encoding\n", lineno);
		exit(1);
	    }
	    start = stop;
	    goto keep_processing;
	}
    }
    exit(0);
}