File: rfc2047u.c

package info (click to toggle)
swi-prolog 7.2.3%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,180 kB
  • ctags: 45,684
  • sloc: ansic: 330,358; perl: 268,104; sh: 6,795; java: 4,904; makefile: 4,561; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (109 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (8)
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
100
101
102
103
104
105
106
107
108
109
/*
** Copyright 1998 - 2000 Double Precision, Inc.  See COPYING for
** distribution information.
*/


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

#include	"rfc822.h"
#include	"rfc2047.h"

static const char rcsid[]="$Id$";

#if HAVE_LIBUNICODE

#include "../unicode/unicode.h"

struct decode_unicode_s {
	const struct unicode_info *mychset;
	int options;

	char *bufptr;
	size_t bufsize;
} ;

static void save_unicode_text(const char *p, int l, struct decode_unicode_s *s)
{
	if (s->bufptr)
		memcpy(s->bufptr+s->bufsize, p, l);

	s->bufsize += l;
}

static int save_unicode(const char *txt, int len, const char *chset,
		void *arg)
{
	struct decode_unicode_s *p=(struct decode_unicode_s *)arg;
	char *txts=malloc(len+1);
	char *s;

	if (!txts)
		return (-1);
	memcpy(txts, txt, len);
	txts[len]=0;

	if (!chset)
		chset=unicode_ISO8859_1.chset;

	s=unicode_convert_fromchset(txts, chset, p->mychset);
	free(txts);
	if (s)
	{
		save_unicode_text(s, strlen(s), p);
		free(s);
		return (0);
	}

	if (p->options & RFC2047_DECODE_ABORT)
	{
		errno=EINVAL;
		return (-1);
	}

	if (p->options & RFC2047_DECODE_DISCARD)
		return (0);

	save_unicode_text(" [", 2, p);
	save_unicode_text(chset, strlen(chset), p);
	save_unicode_text("] ", 2, p);
	save_unicode_text(txt, len, p);
	return (0);
}

char *rfc2047_decode_unicode(const char *text,
	const struct unicode_info *mychset,
	int options)
{
	struct decode_unicode_s s;
	char *p=0;

	s.mychset=mychset;
	s.options=0;

	s.bufptr=0;
	s.bufsize=1;


	if (rfc2047_decode(text, &save_unicode, &s))
		return (0);

	s.bufptr=p=malloc(s.bufsize);
	if (!s.bufptr)
		return (0);

	s.bufsize=0;
	if (rfc2047_decode(text, &save_unicode, &s))
	{
		free(p);
		return (0);
	}
	save_unicode_text("", 1, (void *)&s);
	return (p);
}

#endif