2 ** Copyright 2003-2004 Double Precision, Inc. See COPYING for
3 ** distribution information.
7 ** $Id: encodeautodetect.c,v 1.1 2004/10/21 00:10:51 mrsam Exp $
13 #include "../unicode/unicode.h"
16 static const char *libmail_encode_autodetect(const char *charset
,
17 int (*func
)(void *), void *arg
)
19 const char *encoding
="7bit";
24 const struct unicode_info
*ci
= unicode_find(charset
);
27 while ((c
= (*func
)(arg
)) != EOF
)
29 unsigned char ch
= (unsigned char)c
;
35 if (!charset
|| !*charset
)
37 else if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
38 encoding
="quoted-printable";
39 else if (!ci
|| ci
->flags
& UNICODE_BODY_BASE64
)
49 ch
!= '\t' && ch
!= '\r' && ch
!= '\n')
52 if (!charset
|| !*charset
)
54 else if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
55 encoding
="quoted-printable";
56 else if (!ci
|| ci
->flags
& UNICODE_BODY_BASE64
)
59 if (charset
&& *charset
)
60 encoding
="quoted-printable";
72 if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
73 encoding
="quoted-printable";
75 if (charset
&& *charset
)
76 encoding
="quoted-printable";
85 if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
86 encoding
="quoted-printable";
90 if (charset
&& *charset
)
91 encoding
="quoted-printable";
105 static int read_file(void *arg
)
108 struct file_info
*fi
= (struct file_info
*)arg
;
109 if (fi
->end
>= 0 && fi
->pos
> fi
->end
)
116 static int read_string(void * arg
)
119 unsigned char **strp
= (unsigned char **)arg
;
127 const char *libmail_encode_autodetect_fp(FILE *fp
, int okQp
)
130 return libmail_encode_autodetect_fppos(fp
, "ISO-8859-1", 0, -1);
132 return libmail_encode_autodetect_fppos(fp
, NULL
, 0, -1);
135 const char *libmail_encode_autodetect_fppos(FILE *fp
, const char *charset
,
136 off_t start_pos
, off_t end_pos
)
139 off_t orig_pos
= ftell(fp
);
140 off_t pos
= orig_pos
;
145 if (fseek(fp
, start_pos
, SEEK_SET
) == (off_t
)-1)
154 rc
= libmail_encode_autodetect(charset
, &read_file
, &fi
);
156 if (fseek(fp
, orig_pos
, SEEK_SET
) == (off_t
)-1)
161 const char *libmail_encode_autodetect_str(const char *str
, const char *charset
)
163 return libmail_encode_autodetect(charset
, &read_string
, &str
);