From 6f5e76ce47699ec5b8c00bc1960979ef2c032565 Mon Sep 17 00:00:00 2001 From: Bron Gondwana Date: Wed, 18 Mar 2009 21:56:01 -0400 Subject: [PATCH] Charset tool --- imap/.cvsignore | 1 + imap/Makefile.in | 6 ++- imap/cyr_charset.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 1 deletions(-) create mode 100644 imap/cyr_charset.c diff --git a/imap/.cvsignore b/imap/.cvsignore index a1f5359..5ed2ad6 100644 --- a/imap/.cvsignore +++ b/imap/.cvsignore @@ -7,6 +7,7 @@ ctl_deliver ctl_mboxlist cvt_cyrusdb cyrdump +cyr_charset cyr_dbtool cyr_df cyr_expire diff --git a/imap/Makefile.in b/imap/Makefile.in index 69fd3db..d7d071e 100644 --- a/imap/Makefile.in +++ b/imap/Makefile.in @@ -115,7 +115,7 @@ SERVICE=../master/service.o SERVICETHREAD=../master/service-thread.o PROGS = imapd lmtpd pop3d \ - fud smmapd reconstruct quota mbpath ipurge cyr_dbtool cyr_synclog \ + fud smmapd reconstruct quota mbpath ipurge cyr_charset cyr_dbtool cyr_synclog \ cyrdump chk_cyrus cvt_cyrusdb deliver ctl_mboxlist hammer_skiplist \ ctl_deliver ctl_cyrusdb squatter mbexamine cyr_expire arbitron \ unexpunge cyr_df @IMAP_PROGS@ @@ -258,6 +258,10 @@ arbitron: arbitron.o $(CLIOBJS) libimap.a $(DEPLIBS) $(CC) $(LDFLAGS) -o arbitron arbitron.o $(CLIOBJS) \ libimap.a $(DEPLIBS) $(LIBS) +cyr_charset: cyr_charset.o mutex_fake.o libimap.a $(DEPLIBS) + $(CC) $(LDFLAGS) -o cyr_charset cyr_charset.o $(CLIOBJS) \ + libimap.a $(DEPLIBS) $(LIBS) + cyr_dbtool: cyr_dbtool.o mutex_fake.o libimap.a $(DEPLIBS) $(CC) $(LDFLAGS) -o cyr_dbtool cyr_dbtool.o $(CLIOBJS) \ libimap.a $(DEPLIBS) $(LIBS) diff --git a/imap/cyr_charset.c b/imap/cyr_charset.c new file mode 100644 index 0000000..ab8ce61 --- /dev/null +++ b/imap/cyr_charset.c @@ -0,0 +1,160 @@ +/* cyr_charset.c -- test cyrus charset conversion routines + * + * Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The name "Carnegie Mellon University" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For permission or any legal + * details, please contact + * Carnegie Mellon University + * Center for Technology Transfer and Enterprise Creation + * 4615 Forbes Avenue + * Suite 302 + * Pittsburgh, PA 15213 + * (412) 268-7393, fax: (412) 268-7395 + * innovation@andrew.cmu.edu + * + * 4. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by Computing Services + * at Carnegie Mellon University (http://www.cmu.edu/computing/)." + * + * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO + * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE + * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $Id: cyr_charset.c,v 1.7 2008/10/14 14:53:40 murch Exp $ + */ + +#include + +#include +#include +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#include +#include +#include +#include +#include +#include +#include + +#include "acl.h" +#include "assert.h" +#include "auth.h" +#include "cyrusdb.h" +#include "exitcodes.h" +#include "glob.h" +#include "imap_err.h" +#include "global.h" +#include "mailbox.h" +#include "util.h" +#include "xmalloc.h" +#include "charset.h" + +/* config.c stuff */ +const int config_need_data = 0; + +char *read_stdin() +{ + char c; + int i = 0; + int alloced = 100; + char *buf = xmalloc(100); + + while ((c = getchar()) != EOF) { + /* ensure space for trailing '\0' */ + if (i >= (alloced-1)) { + alloced += 100; + buf = xrealloc(buf, alloced); + } + buf[i++] = c; + } + buf[i] = '\0'; + + return buf; +} + +char *do_translate(char *input, char *charset, int is_mime, int is_search, int is_qp) +{ + char *res = NULL; + int charset_num = 0; + + if (is_mime) { + res = charset_parse_mimeheader(input); + } + else { + if (charset) charset_num = charset_lookupname(charset); + res = charset_to_utf8(input, strlen(input), charset_num, is_qp ? ENCODING_QP : ENCODING_NONE); + } + + return res; +} + +int main(int argc, char *argv[]) +{ + char opt; + char *charset = NULL; + char *input = NULL; + char *output = NULL; + char *alt_config = NULL; + const char *res = NULL; + int is_mime = 0; + int is_search = 0; + int is_qp = 0; + + while ((opt = getopt(argc, argv, "C:c:msq")) != EOF) { + switch (opt) { + case 'C': /* alt config file */ + alt_config = optarg; + break; + case 'c': + charset = optarg; + break; + case 'm': /* create new */ + is_mime = 1; + break; + case 's': /* create new */ + is_search = 1; + break; + case 'q': + is_qp = 1; + } + } + + cyrus_init(alt_config, "cyr_charset", 0); + + input = read_stdin(); + if (input) { + output = do_translate(input, charset, is_mime, is_search, is_qp); + free(input); + + if (output) { + printf("%s\n", output); + free(output); + } + } + + cyrus_done(); + + return 0; +} -- 1.5.6.5