"Fossies" - the Fresh Open Source Software Archive

Member "delegate9.9.13/filters/m17nccx.c" (30 Jan 2010, 8964 Bytes) of package /linux/misc/old/delegate9.9.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "m17nccx.c" see the Fossies "Dox" file reference documentation.

    1 /*////////////////////////////////////////////////////////////////////////
    2 Copyright (c) 2009 National Institute of Advanced Industrial Science and Technology (AIST)
    3 
    4 Permission to use this material for noncommercial and/or evaluation
    5 purpose, copy this material for your own use, and distribute the copies
    6 via publicly accessible on-line media, without fee, is hereby granted
    7 provided that the above copyright notice and this permission notice
    8 appear in all copies.
    9 AIST MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY OF THIS
   10 MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", WITHOUT ANY EXPRESS
   11 OR IMPLIED WARRANTIES.
   12 /////////////////////////////////////////////////////////////////////////
   13 Content-Type:   program/C; charset=US-ASCII
   14 Program:    m17nccx.c (character code converter powered by m17n-lib)
   15 Author:     Yutaka Sato <ysato@delegate.org>
   16 Description:
   17   Utilizing the m17n library just for character code conversion.
   18 
   19     typedef strucnt _M17N *M17N;
   20     int m17n_Init();
   21     int m17n_Fini();
   22     int m17n_known_code(const char *name);
   23     M17N m17n_ccx_new(const char *icode,const char *ocode,void *buf,int siz);
   24     int m17n_ccx_free(M17N m17n);
   25     int m17n_ccx_init(M17N m17n,const char *icode,const char *ocode);
   26     int m17n_ccx_opts(M17N m17n,const char *opts);
   27     int m17n_ccx_filter(M17N m17n,FILE *in,FILE *out);
   28     int m17n_ccx_string(M17N m17n,const char *istr,int len,char *ostr,int siz);
   29 
   30   as a command:
   31     delegated -FdeMime CHARCODE=iso-2022-jp
   32     delegated -Fm17n -iutf-8 -oiso-2022-jp
   33   as a filter of DeleGate server:
   34     FTOCL="-m17n -iutf-8 -oiso-2022-jp"
   35   as a filter in CFI:
   36     Body-Filter: -m17n -iutf-8 -oiso-2022-jp
   37   as a server:
   38     delegated SERVER=m17n M17N="-iutf-8 -oiso-2022-jp"
   39 
   40 History:
   41     090103  created
   42 //////////////////////////////////////////////////////////////////////#*/
   43 #include "ystring.h"
   44 #include "dglib.h"
   45 
   46 /*BEGIN_STAB(m17n)*/
   47 #ifdef __cplusplus
   48 extern "C" {
   49 #endif
   50     typedef void *MSymbol;
   51     extern MSymbol Mcoding_utf_8;
   52     typedef void MText;
   53     MSymbol msymbol(const char *sym);
   54     MText *mtext();
   55     typedef struct {
   56         int lenient;
   57         int last_block;
   58            unsigned at_most;
   59         int nchars;
   60         int nbytes;
   61         int result;
   62     } MConverter;
   63     void m17n_init();
   64     MSymbol mconv_resolve_coding(MSymbol symbol);
   65     MConverter *mconv_stream_converter(MSymbol coding,FILE *fp);
   66     MConverter *mconv_buffer_converter(MSymbol coding,const unsigned char *buf,int n);
   67     MText *mconv_decode(MConverter *converter,MText *mt);
   68     int mconv_encode(MConverter *converter,MText *mt);
   69     void mconv_free_converter(MConverter *converter);
   70     int m17n_object_unref(void *object);
   71     void m17n_fini();
   72     enum MErrorCode { MERROR_NONE };
   73     /*
   74     extern MErrorCode merror_code;
   75     */
   76 #ifdef __cplusplus
   77 }
   78 #endif
   79 /*END_STAB*/
   80 
   81 static int m17n_enabled = 0;
   82 void scan_M17N(DGC*ctx,PCStr(conf)){
   83     IStr(nam,128);
   84     IStr(val,128);
   85     fieldScan(conf,nam,val);
   86     if( strcaseeq(nam,"on") ){
   87         m17n_enabled = 1;
   88     }
   89     else
   90     if( strcaseeq(nam,"off") ){
   91         m17n_enabled = 0;
   92     }
   93     else
   94     if( strcaseeq(nam,"lib") ){
   95         /* add the path to the default search path of dylib */
   96     }
   97 }
   98 
   99 int dl_library(const char *libname,DLMap *dlmap,const char *mode);
  100 static int m17n_dlstat;
  101 int m17n_lib_init(){
  102     if( m17n_enabled == 0 ){
  103         return -3;
  104     }
  105     if( m17n_dlstat < 0 ){
  106         return -2;
  107     }
  108     if( m17n_dlstat == 0 ){
  109         int code;
  110         code = dl_library("m17n",dlmap_m17n,"");
  111         if( code != 0 ){
  112             fprintf(stderr,"--M17n cannot load m17n dylib.\n");
  113             m17n_dlstat = -1;
  114             return -1;
  115         }
  116         m17n_dlstat = 1;
  117     }
  118     return 0;
  119 }
  120 static int m17n_stat;
  121 int m17n_Active(){
  122     return 0 < m17n_stat;
  123 }
  124 int m17n_Init(){
  125     if( m17n_stat < 0 )
  126         return -1;
  127     if( m17n_stat == 0 ){
  128         m17n_init();
  129         /*if( merror_code != MERROR_NONE )*/
  130         if( mconv_resolve_coding(msymbol("iso-2022-jp")) == 0 ){
  131             fprintf(stderr,"--M17n Failed to initialize");
  132             m17n_stat = -1;
  133         }else{
  134             m17n_stat = 1;
  135         }
  136     }
  137     return 0 < m17n_stat;
  138 }
  139 int m17n_Fini(){
  140     if( 0 < m17n_stat ){
  141         m17n_stat = 0;
  142         return 0;
  143     }
  144     return -1;
  145 }
  146 int m17n_known_code(const char *name){
  147     if( m17n_lib_init() < 0 ){
  148         return 0;
  149     }
  150     m17n_Init();
  151     if( mconv_resolve_coding(msymbol(name)) ){
  152         return 1;
  153     }else{
  154         return 0;
  155     }
  156 }
  157 
  158 typedef struct _M17N {
  159     void        *m_ctx;   /* opaque */
  160     double       m_alloc; /* to be free() on finish */
  161     int      m_trace;
  162     double       m_Start;
  163     const char  *m_icnam; /* the name of the input code */
  164     const char  *m_ocnam; /* the name of the output code */
  165     MSymbol      m_icode;
  166     MSymbol      m_ocode;
  167     MConverter  *m_dec;
  168     MConverter  *m_enc;
  169     MText       *m_mtext;
  170 } M17nCCX;
  171 static void Lap(M17nCCX *m17n,const char *wh){
  172     if( m17n->m_trace ){
  173         fprintf(stderr,"--M17N %.3f %s\n",
  174             Time()-m17n->m_Start,wh);
  175     }
  176 }
  177 #define LAP(wh) Lap(m17n,wh)
  178 
  179 int m17n_ccx_init(M17nCCX *m17n,const char *icode,const char *ocode);
  180 M17nCCX *m17n_ccx_new(const char *icode,const char *ocode,void *buf,int siz){
  181     M17nCCX *m17n;
  182     if( buf ){
  183         if( siz < sizeof(M17nCCX) ){
  184             fprintf(stderr,"--M17n buf. size %d < %d\n",
  185                 siz,isizeof(M17nCCX));
  186             return 0;
  187         }
  188         m17n = (M17nCCX*)buf;
  189         bzero(m17n,sizeof(M17nCCX));
  190     }else{
  191         m17n = (M17nCCX*)malloc(sizeof(M17nCCX));
  192         bzero(m17n,sizeof(M17nCCX));
  193         m17n->m_alloc = 1;
  194     }
  195     if( icode && ocode ){
  196         m17n_ccx_init(m17n,icode,ocode);
  197     }
  198     return m17n;
  199 }
  200 int m17n_ccx_free(M17nCCX *m17n){
  201     if( m17n->m_alloc ){
  202         free(m17n);
  203         m17n->m_alloc = 0;
  204     }
  205     return 0;
  206 }
  207 int m17n_ccx_init(M17nCCX *m17n,const char *icode,const char *ocode){
  208     m17n->m_Start = Time();
  209     if( m17n_lib_init() < 0 ){
  210         return -1;
  211     }
  212     LAP("dylib-done");
  213     m17n_Init();
  214     LAP("init-done");
  215     if( icode == 0 ) icode = "utf-8";
  216     if( (m17n->m_icode = mconv_resolve_coding(msymbol(icode))) == 0 ){
  217         fprintf(stderr,"--M17N unknown code: %s\n",icode);
  218         return -2;
  219     }
  220     if( ocode == 0 ) ocode = "utf-8";
  221     if( (m17n->m_ocode = mconv_resolve_coding(msymbol(ocode))) == 0 ){
  222         fprintf(stderr,"--M17N unknown code: %s\n",ocode);
  223         return -3;
  224     }
  225     LAP("ccx-init-done");
  226     return 0;
  227 }
  228 int m17n_ccx_opts(M17nCCX *m17n,const char *opts){
  229     return -1;
  230 }
  231 int m17n_ccx_freeconv(M17nCCX *m17n){
  232     if( m17n->m_mtext ){
  233         m17n_object_unref(m17n->m_mtext);
  234         m17n->m_mtext = 0;
  235     }
  236     if( m17n->m_dec ){
  237         mconv_free_converter(m17n->m_dec);
  238         m17n->m_dec = 0;
  239     }
  240     if( m17n->m_enc ){
  241         mconv_free_converter(m17n->m_enc);
  242         m17n->m_enc = 0;
  243     }
  244     return 0;
  245 }
  246 int m17n_ccx_string(M17nCCX *m17n,const char *istr,int len,char *ostr,int siz){
  247     const unsigned char *uistr = (const unsigned char*)istr;
  248     const unsigned char *uostr = (const unsigned char*)ostr;
  249     int rcode = -1;
  250     int ecode;
  251     int oleng = 1;
  252 
  253     m17n->m_mtext = 0;
  254     m17n->m_dec = mconv_buffer_converter(m17n->m_icode,uistr,len);
  255     if( m17n->m_dec == 0 ){
  256         m17n_ccx_freeconv(m17n);
  257         return -1;
  258     }
  259     m17n->m_enc = mconv_buffer_converter(m17n->m_ocode,uostr,siz-1);
  260     if( m17n->m_enc == 0 ){
  261         m17n_ccx_freeconv(m17n);
  262         return -2;
  263     }
  264     LAP("converter-set");
  265 
  266     m17n->m_mtext = mtext();
  267     m17n->m_dec->lenient = 1;
  268     mconv_decode(m17n->m_dec,m17n->m_mtext);
  269     LAP("decoded");
  270     m17n->m_enc->lenient = 1;
  271     m17n->m_enc->last_block = 1;
  272     ecode = mconv_encode(m17n->m_enc,m17n->m_mtext);
  273 
  274     oleng = m17n->m_enc->nbytes;
  275     ostr[oleng] = 0;
  276     rcode = oleng;
  277 
  278     LAP("encoded");
  279     m17n_ccx_freeconv(m17n);
  280     LAP("filter-done");
  281     return rcode;
  282 }
  283 int m17n_ccx_filter(M17nCCX *m17n,FILE *in,FILE *out){
  284     int rcode = -1;
  285     int ecode;
  286 
  287     m17n->m_mtext = 0;
  288     m17n->m_dec = mconv_stream_converter(m17n->m_icode,in);
  289     if( m17n->m_dec == 0 ){
  290         m17n_ccx_freeconv(m17n);
  291         return -1;
  292     }
  293     m17n->m_enc = mconv_stream_converter(m17n->m_ocode,out);
  294     if( m17n->m_enc == 0 ){
  295         m17n_ccx_freeconv(m17n);
  296         return -2;
  297     }
  298 
  299     LAP("converter-set");
  300     m17n->m_mtext = mtext();
  301     m17n->m_dec->lenient = 1;
  302     mconv_decode(m17n->m_dec,m17n->m_mtext);
  303     LAP("decoded");
  304     m17n->m_enc->lenient = 1;
  305     m17n->m_enc->last_block = 1;
  306     ecode = mconv_encode(m17n->m_enc,m17n->m_mtext);
  307     rcode = 0;
  308 
  309     LAP("encoded");
  310     m17n_ccx_freeconv(m17n);
  311     LAP("filter-done");
  312     return rcode;
  313 }
  314 
  315 int m17n_mainX(int ac,const char *av[],FILE *in,FILE *out){
  316     int ai;
  317     const char *arg;
  318     M17nCCX *m17n;
  319     int fini = 0;
  320 
  321     m17n = (M17nCCX*)m17n_ccx_new(0,0,0,0);
  322     m17n->m_icnam = "iso-2022-jp";
  323     m17n->m_ocnam = "iso-2022-jp";
  324     for( ai = 1; ai < ac; ai++ ){
  325         arg = av[ai];
  326         if( strneq(arg,"-i",2) ){
  327             m17n->m_icnam = arg+2;
  328         }else
  329         if( strneq(arg,"-o",2) ){
  330             m17n->m_ocnam = arg+2;
  331         }else
  332         if( strneq(arg,"-d",2) ){
  333             sscanf(arg+2,"%X",&m17n->m_trace);
  334         }else
  335         if( strneq(arg,"-x",2) ){
  336             fini = 1;
  337         }
  338     }
  339     if( m17n_ccx_init(m17n,m17n->m_icnam,m17n->m_ocnam) != 0 ){
  340         return -1;
  341     }
  342     m17n_ccx_filter(m17n,in,out);
  343     if( fini ){
  344         m17n_Fini();
  345     }
  346     m17n_ccx_free(m17n);
  347     LAP("finish");
  348     fflush(out);
  349     return 0;
  350 }
  351 int m17n_main(int ac,const char *av[]){
  352     return m17n_mainX(ac,av,stdin,stdout);
  353 }
  354 int m17n_ccx_Filter(PCStr(filter),FILE *in,FILE *out){
  355     IStr(ab,1024);
  356     const char *av[32];
  357     int ac;
  358     int ai;
  359 
  360     ac = decomp_args(av,elnumof(av),filter,AVStr(ab));
  361     for( ai = 0; ai < ac; ai++ ){
  362         syslog_DEBUG("-m17n[%d] %s\n",ai,av[ai]);
  363     }
  364     m17n_mainX(ac,av,in,out);
  365     return 1;
  366 }