view f-demime/qpdec.c @ 0:7e0d08176f32

f-demime starting code
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 06:14:03 +0000
parents
children 05651a1b8ba8
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)
	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)
			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('=');
	}
	dec_outf('\n');
}