2 ** Copyright 2003-2009 Double Precision, Inc. See COPYING for
3 ** distribution information.
7 ** $Id: encodeautodetect.c,v 1.2 2009/11/08 18:14:47 mrsam Exp $
12 #include "../unicode/unicode.h"
14 static const char *libmail_encode_autodetect(const char *charset
,
15 int (*func
)(void *), void *arg
)
17 const char *encoding
="7bit";
21 const struct unicode_info
*ci
= unicode_find(charset
);
23 while ((c
= (*func
)(arg
)) != EOF
)
25 unsigned char ch
= (unsigned char)c
;
30 if (!charset
|| !*charset
)
32 else if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
33 encoding
="quoted-printable";
34 else if (!ci
|| ci
->flags
& UNICODE_BODY_BASE64
)
41 ch
!= '\t' && ch
!= '\r' && ch
!= '\n')
43 if (!charset
|| !*charset
)
45 else if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
46 encoding
="quoted-printable";
47 else if (!ci
|| ci
->flags
& UNICODE_BODY_BASE64
)
58 if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
59 encoding
="quoted-printable";
66 if (ci
&& ci
->flags
& UNICODE_BODY_QUOPRI
)
67 encoding
="quoted-printable";
80 static int read_file(void *arg
)
83 struct file_info
*fi
= (struct file_info
*)arg
;
84 if (fi
->end
>= 0 && fi
->pos
> fi
->end
)
91 static int read_string(void * arg
)
94 unsigned char **strp
= (unsigned char **)arg
;
102 const char *libmail_encode_autodetect_fp(FILE *fp
, int okQp
)
105 return libmail_encode_autodetect_fppos(fp
, "ISO-8859-1", 0, -1);
107 return libmail_encode_autodetect_fppos(fp
, NULL
, 0, -1);
110 const char *libmail_encode_autodetect_fppos(FILE *fp
, const char *charset
,
111 off_t start_pos
, off_t end_pos
)
114 off_t orig_pos
= ftell(fp
);
115 off_t pos
= orig_pos
;
120 if (fseek(fp
, start_pos
, SEEK_SET
) == (off_t
)-1)
129 rc
= libmail_encode_autodetect(charset
, &read_file
, &fi
);
131 if (fseek(fp
, orig_pos
, SEEK_SET
) == (off_t
)-1)
136 const char *libmail_encode_autodetect_str(const char *str
, const char *charset
)
138 return libmail_encode_autodetect(charset
, &read_string
, &str
);