view f-demime/qpdec.c @ 8:a92d0d59b669 default tip

f-demime: indicate X-backslash-escapes encoding in output
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 17:00:23 +0000
parents 612c4d0df768
children
line wrap: on
line source

/*
 * This module implements quoted-printable decoding.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "defs.h"

extern void (*dec_outf)();

int qpdec_err_flag;

static void
strip_trailing_lwsp(line)
	char *line;
{
	char *cp;

	cp = index(line, '\0');
	while (cp > line && isspace(cp[-1]))
		cp--;
	*cp = '\0';
}

static int
decode_hex_digit(c)
{
	if (isdigit(c))
		return(c - '0');
	else if (isupper(c))
		return(c - 'A' + 10);
	else
		return(c - 'a' + 10);
}

static int
decode_hex_byte(cp)
	char *cp;
{
	int u, l;

	u = decode_hex_digit(cp[0]);
	l = decode_hex_digit(cp[1]);
	return (u << 4) | l;
}

void
qpdec_input_line(line, unterm)
	char *line;
{
	char *cp;
	int c;

	strip_trailing_lwsp(line);
	for (cp = line; *cp; ) {
		c = *cp++ & 0xFF;
		if (c != '=') {
			dec_outf(c);
			continue;
		}
		if (!*cp) {
			if (unterm)
				qpdec_err_flag = 1;
			return;
		}
		if (isxdigit(cp[0]) && isxdigit(cp[1])) {
			c = decode_hex_byte(cp);
			cp += 2;
			dec_outf(c);
			continue;
		}
		qpdec_err_flag = 1;
		dec_outf('=');
	}
	if (!unterm)
		dec_outf('\n');
}