1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /* lcGenConv.c 1.5 - Fujitsu source for CDEnext 96/03/14 15:45:41 */
- /* $XConsortium: _fallcGenConv.c /main/1 1996/04/08 15:17:00 cde-fuj $ */
- /*
- * Copyright 1992, 1993 by TOSHIBA Corp.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of TOSHIBA not be used in advertising
- * or publicity pertaining to distribution of the software without specific,
- * written prior permission. TOSHIBA make no representations about the
- * suitability of this software for any purpose. It is provided "as is"
- * without express or implied warranty.
- *
- * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
- * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
- * TOSHIBA 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.
- *
- * Author: Katsuhisa Yano TOSHIBA Corp.
- * mopi@osa.ilab.toshiba.co.jp
- */
- /*
- * (c) Copyright 1995 FUJITSU LIMITED
- * This is source code modified by FUJITSU LIMITED under the Joint
- * Development Agreement for the CDEnext PST.
- * This is unpublished proprietary source code of FUJITSU LIMITED
- *
- * Modifier: Masayoshi Shimamura FUJITSU LIMITED
- *
- */
- #include "_fallibint.h"
- #include "_fallcGeneric.h"
- #include <stdio.h>
- typedef struct _CTDataRec {
- char *name;
- char *encoding; /* Compound Text encoding */
- } CTDataRec, *CTData;
- static CTDataRec default_ct_data[] =
- {
- { "ISO8859-1:GL", "\033(B" },
- { "ISO8859-1:GR", "\033-A" },
- { "ISO8859-2:GR", "\033-B" },
- { "ISO8859-3:GR", "\033-C" },
- { "ISO8859-4:GR", "\033-D" },
- { "ISO8859-7:GR", "\033-F" },
- { "ISO8859-6:GR", "\033-G" },
- { "ISO8859-8:GR", "\033-H" },
- { "ISO8859-5:GR", "\033-L" },
- { "ISO8859-9:GR", "\033-M" },
- { "JISX0201.1976-0:GL", "\033(J" },
- { "JISX0201.1976-0:GR", "\033)I" },
- { "GB2312.1980-0:GL", "\033$(A" },
- { "GB2312.1980-0:GR", "\033$)A" },
- { "JISX0208.1983-0:GL", "\033$(B" },
- { "JISX0208.1983-0:GR", "\033$)B" },
- { "KSC5601.1987-0:GL", "\033$(C" },
- { "KSC5601.1987-0:GR", "\033$)C" },
- } ;
- static CTDataRec directionality_data[] =
- {
- { "BEGIN_LEFT-TO-RIGHT_TEXT", "\2331]" },
- { "BEGIN_RIGHT-TO-LEFT_TEXT", "\2332]" },
- { "END_OF_STRING", "\233]" },
- };
- typedef struct _StateRec {
- XLCd lcd;
- XlcCharSet charset; /* charset of current state */
- XlcCharSet GL_charset; /* charset of initial state in GL */
- XlcCharSet GR_charset; /* charset of initial state in GR */
- } StateRec, *State;
- #define GR 0x80 /* begins right-side (non-ascii) region */
- #define GL 0x7f /* ends left-side (ascii) region */
- #define ESC 0x1b
- #define CSI 0x9b
- #define STX 0x02
- #define isleftside(c) (((c) & GR) ? 0 : 1)
- #define isrightside(c) (!isleftside(c))
- /* -------------------------------------------------------------------------- */
- /* Misc */
- /* -------------------------------------------------------------------------- */
- static int
- compare(char *src, char *encoding, int length)
- {
- char *start = src;
- while (length-- > 0) {
- if (*src++ != *encoding++)
- return 0;
- if (*encoding == '\0')
- return src - start;
- }
- return 0;
- }
- static unsigned long
- conv_to_dest(Conversion conv, unsigned long code)
- {
- int i;
- int conv_num = conv->conv_num;
- FontScope convlist = conv->convlist;
- for (i = 0; i < conv_num; i++) {
- if (convlist[i].start <= code && code <= convlist[i].end) {
- switch (convlist[i].shift_direction) {
- case '+':
- return(code + convlist[i].shift);
- case '-':
- return(code - convlist[i].shift);
- default:
- return(code);
- }
- }
- }
- return(code);
- }
- static unsigned long
- conv_to_source(Conversion conv, unsigned long code)
- {
- int i;
- int conv_num;
- FontScope convlist;
- unsigned long start_p;
- unsigned long start_m;
- unsigned long end_p;
- unsigned long end_m;
- if (!conv)
- return(code);
- conv_num = conv->conv_num;
- convlist = conv->convlist;
- for (i = 0; i < conv_num; i++) {
- start_p = convlist[i].start + convlist[i].shift;
- start_m = convlist[i].start - convlist[i].shift;
- end_p = convlist[i].end + convlist[i].shift;
- end_m = convlist[i].end - convlist[i].shift;
- switch (convlist[i].shift_direction) {
- case '+':
- if (start_p <= code && code <= end_p)
- return(code - convlist[i].shift);
- break;
- case '-':
- if (start_m <= code && code <= end_m)
- return(code + convlist[i].shift);
- break;
- default:
- continue;
- }
- }
- return(code);
- }
- static unsigned long
- mb_to_gi(unsigned long mb, CodeSet codeset)
- {
- int i;
- unsigned long mb_tmp, mask = 0;
- if (codeset->mbconv) {
- mb_tmp = conv_to_dest(codeset->mbconv, mb);
- if (mb_tmp != mb)
- return(mb_tmp);
- }
- if (codeset->side == XlcC0 || codeset->side == XlcGL ||
- codeset->side == XlcC1 || codeset->side == XlcGR) {
- for (i = 0; i < codeset->length; i++)
- mask = (mask << 8) | GL;
- mb = mb & mask;
- }
- return(mb);
- }
- static unsigned long
- gi_to_mb(unsigned long glyph_index, CodeSet codeset)
- {
- int i;
- unsigned long mask = 0;
- if (codeset->side == XlcC1 || codeset->side == XlcGR) {
- for (i = 0; i < codeset->length; i++)
- mask = (mask << 8) | GR;
- glyph_index = glyph_index | mask;
- }
- if (codeset->mbconv)
- return( conv_to_source(codeset->mbconv, glyph_index) );
- return(glyph_index);
- }
- static Bool
- gi_to_wc(XLCd lcd, unsigned long glyph_index, CodeSet codeset, wchar_t *wc)
- {
- unsigned char mask = 0;
- unsigned long wc_encoding = codeset->wc_encoding;
- int i, length = codeset->length;
- unsigned long wc_shift_bits = XLC_GENERIC(lcd, wc_shift_bits);
- for (i = wc_shift_bits; i > 0; i--)
- mask = (mask << 1) | 0x01;
- for (*wc = 0, length--; length >= 0; length--)
- *wc = (*wc << wc_shift_bits) | ((glyph_index >> (length * 8 )) & mask);
- *wc = *wc | wc_encoding;
- return(True);
- }
- static Bool
- wc_to_gi(XLCd lcd, wchar_t wc, unsigned long *glyph_index, CodeSet *codeset)
- {
- int i;
- unsigned char mask = 0;
- unsigned long wc_encoding;
- unsigned long wc_encode_mask = XLC_GENERIC(lcd, wc_encode_mask);
- unsigned long wc_shift_bits = XLC_GENERIC(lcd, wc_shift_bits);
- int codeset_num = XLC_GENERIC(lcd, codeset_num);
- CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
- wc_encoding = wc & wc_encode_mask;
- for (*codeset = NULL, i = 0; i < codeset_num; i++) {
- if (wc_encoding == codeset_list[i]->wc_encoding) {
- *codeset = codeset_list[i];
- break;
- }
- }
- if (*codeset == NULL)
- return(False);
- for (i = wc_shift_bits; i > 0; i--)
- mask = (mask << 1) | 0x01;
- wc = wc & ~wc_encode_mask;
- for (*glyph_index = 0, i = (*codeset)->length - 1; i >= 0; i--)
- *glyph_index = (*glyph_index << 8) |
- ( ((unsigned long)wc >> (i * wc_shift_bits)) & mask );
- return(True);
- }
- static CodeSet
- byteM_parse_codeset(XLCd lcd, XPointer inbufptr)
- {
- unsigned char ch;
- CodeSet codeset;
- ByteInfoList byteM;
- ByteInfoListRec byteM_rec;
- ByteInfo byteinfo;
- ByteInfoRec byteinfo_rec;
- Bool hit;
- int i, j, k;
- int codeset_num = XLC_GENERIC(lcd, codeset_num);
- CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
- for (i = 0; i < codeset_num; i++) {
- codeset = codeset_list[i];
- byteM = codeset->byteM;
- if (codeset->side != XlcNONE || byteM == NULL)
- continue;
- for (j = 0; j < codeset->length; j++) {
- ch = *((unsigned char *)(inbufptr + j));
- byteM_rec = byteM[j];
- byteinfo = byteM_rec.byteinfo;
- for (hit=False,k=0; k < byteM_rec.byteinfo_num; k++) {
- byteinfo_rec = byteinfo[k];
- if (byteinfo_rec.start <= ch && ch <= byteinfo_rec.end) {
- hit = True;
- break;
- }
- }
- if (!hit)
- break;
- }
- if (hit)
- return(codeset);
- }
- return(NULL);
- }
- static CodeSet
- GLGR_parse_codeset(XLCd lcd, unsigned char ch)
- {
- int i;
- CodeSet initial_state_GL = XLC_GENERIC(lcd, initial_state_GL);
- CodeSet initial_state_GR = XLC_GENERIC(lcd, initial_state_GR);
- CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
- int codeset_num = XLC_GENERIC(lcd, codeset_num);
- XlcSide side = XlcGL;
- CodeSet codeset = initial_state_GL;
- if (isrightside(ch)) {
- side = XlcGR;
- codeset = initial_state_GR;
- }
- if (codeset)
- return(codeset);
- for (i = 0; i < codeset_num; i++) {
- codeset = codeset_list[i];
- if (codeset->side == side)
- return(codeset);
- }
- return(NULL);
- }
- static XlcCharSet
- gi_parse_charset(unsigned long glyph_index, CodeSet codeset)
- {
- int i;
- size_t table_size = sizeof(default_ct_data) / sizeof(default_ct_data[0]);
- XlcCharSet *charset_list = codeset->charset_list;
- int num_charsets = codeset->num_charsets;
- ExtdSegment ctextseg = codeset->ctextseg;
- XlcCharSet charset;
- int area_num;
- FontScope area;
- /* lockup ct sequence */
- for (i = 0; i < num_charsets; i++) {
- charset = charset_list[i];
- if (*charset->ct_sequence != '\0')
- break;
- }
- if (i >= num_charsets)
- return(NULL);
- /* Standard Character Set Encoding ? */
- for (i = 0; i < table_size; i++){
- if (compare(charset->ct_sequence,
- default_ct_data[i].encoding, strlen(charset->ct_sequence))){
- if (!ctextseg)
- return(charset);
- area = ctextseg->area;
- area_num = ctextseg->area_num;
- for (i = 0; i < area_num; i++) {
- if (area[i].start <= glyph_index && glyph_index <= area[i].end) {
- charset = ctextseg->charset;
- if (*charset->ct_sequence == '\0')
- return(NULL);
- break;
- }
- }
- }
- }
- return(charset);
- }
- static Bool
- ct_parse_csi(XPointer inbufptr, int *ctr_seq_len)
- {
- int i;
- int num = sizeof(directionality_data) / sizeof(directionality_data[0]);
- for (i = 0; i < num; i++) {
- if ( !(*ctr_seq_len = strlen(directionality_data[i].encoding)) )
- continue;
- if ( strncmp(inbufptr, directionality_data[i].encoding,
- *ctr_seq_len) == 0)
- return(True);
- }
- return(False);
- }
- static int
- cmp_esc_sequence(XPointer inbufptr, char *ct_sequence, char *encoding_name)
- {
- size_t table_size = sizeof(default_ct_data) / sizeof(default_ct_data[0]);
- int i, seq_len, name_len, total_len;
- unsigned char byte_m, byte_l;
- /* check esc sequence */
- if ( !(seq_len = strlen(ct_sequence) ) )
- return(0);
- if ( strncmp(inbufptr, ct_sequence, seq_len) != 0)
- return(0);
- /* Standard Character Set Encoding ? */
- for (i = 0; i < table_size; i++) {
- if (compare(ct_sequence,
- default_ct_data[i].encoding, strlen(ct_sequence)))
- return(seq_len);
- }
- /*
- * Non-Standard Character Set Encoding
- *
- * +-----+-----+-----+-----+-----+-----+-----+---- ----+-----+-----+
- * | esc sequence | M | L | encoding name | STX |
- * +-----+-----+-----+-----+-----+-----+-----+---- ----+-----+-----+
- * 4bytes 1byte 1byte variable length 1byte
- * | |
- * +-----------------------------------------+
- * name length = ((M - 128) * 128) + (L - 128)
- */
- /* get length of encoding name */
- inbufptr += seq_len;
- byte_m = *inbufptr++;
- byte_l = *inbufptr++;
- name_len = ((byte_m - 128) * 128) + (byte_l - 128);
- total_len = seq_len + name_len;
- /* compare encoding names */
- if ( strncmp(inbufptr, encoding_name, name_len - 3) != 0 )
- return(0);
- /* check STX (Start of Text) */
- inbufptr = inbufptr + name_len - 3;
- if ( *inbufptr != STX )
- return(0);
- return(total_len);
- }
- static Bool
- ct_parse_charset(
- XLCd lcd,
- XPointer inbufptr,
- XlcCharSet *charset,
- int *ctr_seq_len)
- {
- int i, j;
- ExtdSegment ctextseg;
- int num_charsets;
- XlcCharSet *charset_list;
- CodeSet codeset;
- int codeset_num = XLC_GENERIC(lcd, codeset_num);
- CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
- int segment_conv_num = XLC_GENERIC(lcd, segment_conv_num);
- SegConv segment_conv = XLC_GENERIC(lcd, segment_conv);
- /* get charset from XLC_XLOCALE by escape sequence */
- for (i = 0; i < codeset_num; i++) {
- codeset = codeset_list[i];
- num_charsets = codeset->num_charsets;
- charset_list = codeset->charset_list;
- ctextseg = codeset->ctextseg;
- for (j = 0; j < num_charsets; j++) {
- *charset = charset_list[j];
- if ( *ctr_seq_len = cmp_esc_sequence(inbufptr,
- (*charset)->ct_sequence, (*charset)->encoding_name) )
- return(True);
- }
- if (ctextseg) {
- *charset = ctextseg->charset;
- if ( *ctr_seq_len = cmp_esc_sequence(inbufptr,
- (*charset)->ct_sequence, (*charset)->encoding_name) )
- return(True);
- }
- }
- /* get charset from XLC_SEGMENTCONVERSION by escape sequence */
- if (!segment_conv)
- return(False);
- for (i = 0; i < segment_conv_num; i++) {
- *charset = segment_conv[i].source;
- if ( *ctr_seq_len = cmp_esc_sequence(inbufptr,
- (*charset)->ct_sequence, (*charset)->encoding_name) )
- return(True);
- *charset = segment_conv[i].dest;
- if ( *ctr_seq_len = cmp_esc_sequence(inbufptr,
- (*charset)->ct_sequence, (*charset)->encoding_name) )
- return(True);
- }
- return(False);
- }
- static Bool
- segment_conversion(XLCd lcd, XlcCharSet *charset, unsigned long *glyph_index)
- {
- int i;
- int segment_conv_num = XLC_GENERIC(lcd, segment_conv_num);
- SegConv segment_conv = XLC_GENERIC(lcd, segment_conv);
- FontScopeRec range;
- ConversionRec conv_rec;
- if (!segment_conv)
- return(True);
- for (i = 0; i < segment_conv_num; i++) {
- if (segment_conv[i].source == *charset)
- break;
- }
- if (i >= segment_conv_num)
- return(True);
- range = segment_conv[i].range;
- if (*glyph_index < range.start || range.end < *glyph_index)
- return(True);
- *charset = segment_conv[i].dest;
- conv_rec.conv_num = segment_conv[i].conv_num;
- conv_rec.convlist = segment_conv[i].conv;
- *glyph_index = conv_to_dest(&conv_rec, *glyph_index);
- return(True);
- }
- CodeSet
- _fallcGetCodeSetFromName(XLCd lcd, char *name)
- {
- int i, j;
- XlcCharSet charset;
- int num_charsets;
- XlcCharSet *charset_list;
- CodeSet codeset;
- int codeset_num = XLC_GENERIC(lcd, codeset_num);
- CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
- for (i = 0; i < codeset_num; i++) {
- codeset = codeset_list[i];
- num_charsets = codeset->num_charsets;
- charset_list = codeset->charset_list;
- for (j = 0; j < num_charsets; j++) {
- charset = charset_list[j];
- if (!strlen(charset->name))
- continue;
- if ( strcmp(charset->name, name) == 0)
- return(codeset);
- }
- }
- return(NULL);
- }
- static Bool
- _XlcGetCodeSetFromCharSet(
- XLCd lcd,
- XlcCharSet charset,
- CodeSet *codeset,
- unsigned long *glyph_index)
- {
- int i, j, num;
- CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
- XlcCharSet *charset_list;
- int codeset_num, num_charsets;
- Conversion ctconv;
- unsigned long glyph_index_tmp;
- ExtdSegment ctextseg;
- size_t table_size = sizeof(default_ct_data) / sizeof(default_ct_data[0]);
- codeset_num = XLC_GENERIC(lcd, codeset_num);
- for (num = 0 ; num < codeset_num; num++) {
- *codeset = codeset_list[num];
- ctconv = (*codeset)->ctconv;
- ctextseg = (*codeset)->ctextseg;
- num_charsets = (*codeset)->num_charsets;
- charset_list = (*codeset)->charset_list;
- glyph_index_tmp = conv_to_source(ctconv, *glyph_index);
- /* Standard Character Set Encoding ? */
- for (i = 0; i < table_size; i++) {
- if (compare(charset->ct_sequence,
- default_ct_data[i].encoding, strlen(charset->ct_sequence)))
- break;
- }
- if (i < table_size) {
- /* Standard Character Set Encoding */
- if (glyph_index_tmp == *glyph_index) {
- for (j = 0; j < num_charsets; j++) {
- if (charset_list[j] == charset) {
- goto end_loop;
- }
- }
- }
- } else {
- /* Non-Standard Character Set Encoding */
- for (j = 0; j < num_charsets; j++) {
- if (charset_list[j] == charset) {
- goto end_loop;
- }
- }
- if (glyph_index_tmp != *glyph_index) {
- if (ctextseg->charset == charset) {
- goto end_loop;
- }
- }
- }
- }
- end_loop:
- if (num < codeset_num) {
- *glyph_index = glyph_index_tmp;
- return(True);
- }
- return(False);
- }
- static Bool
- check_string_encoding(CodeSet codeset)
- {
- int i;
- XlcCharSet charset;
- XlcCharSet *charset_list = codeset->charset_list;
- int num_charsets = codeset->num_charsets;
- for (i = 0; i < num_charsets; i++) {
- charset = charset_list[i];
- if ( strcmp(charset->encoding_name, "ISO8859-1") == 0 ||
- charset->string_encoding)
- return(True);
- }
- return(False);
- }
- /* -------------------------------------------------------------------------- */
- /* Init */
- /* -------------------------------------------------------------------------- */
- static void
- init_state(XlcConv conv)
- {
- State state = (State) conv->state;
- /* for CT */
- state->charset = NULL;
- state->GL_charset = _fallcGetCharSet("ISO8859-1:GL");
- state->GR_charset = _fallcGetCharSet("ISO8859-1:GR");
- }
- /* -------------------------------------------------------------------------- */
- /* Convert */
- /* -------------------------------------------------------------------------- */
- static int
- mbstowcs_org(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- unsigned char ch;
- unsigned long mb = 0;
- wchar_t wc;
- int length = 0, len_left = 0;
- int unconv_num = 0;
- int num;
- Bool ss_flag = 0;
- CodeSet codeset = NULL;
- ParseInfo parse_info;
- XPointer inbufptr = *from;
- wchar_t *outbufptr = (wchar_t *) *to;
- int from_size = *from_left;
- unsigned char *mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
- ParseInfo *mb_parse_list = XLC_GENERIC(lcd, mb_parse_list);
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- ch = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!ch) {
- if (outbufptr) {*outbufptr++ = L'\0';}
- (*to_left)--;
- /* error check */
- if (len_left) {
- unconv_num += (length - len_left);
- len_left = 0;
- }
- continue;
- }
- /* same mb char data */
- if (len_left){
- mb = (mb << 8) | ch; /* 1 byte left shift */
- len_left--;
- /* last of one mb char data */
- if (!len_left) {
- gi_to_wc(lcd, mb_to_gi(mb, codeset), codeset, &wc);
- if (outbufptr) {*outbufptr++ = wc;}
- (*to_left)--;
- }
- }
- /* next mb char data for single shift ? */
- if (mb_parse_table) {
- if ((num = mb_parse_table[ch]) > 0) {
- parse_info = mb_parse_list[num - 1];
- codeset = parse_info->codeset;
- length = len_left = codeset->length;
- mb = 0;
- ss_flag = 1;
- continue;
- }
- }
- /* next mb char data for byteM ? */
- if ((codeset = byteM_parse_codeset(lcd, (inbufptr - 1))) ||
- /* next mb char data for GL or GR side ? */
- (codeset = GLGR_parse_codeset(lcd, ch))){
- length = len_left = codeset->length;
- mb = 0;
- ss_flag = 0;
- }
- /* can't find codeset for the ch */
- unconv_num++;
- continue;
- } /* end of while */
- /* error check on last char */
- if (len_left) {
- inbufptr -= (length - len_left + ss_flag);
- (*from_left) += (length - len_left + ss_flag);
- unconv_num += (length - len_left + ss_flag);
- }
- *from = *from + from_size;
- *from_left = 0;
- *to = (XPointer)outbufptr;
- return unconv_num;
- }
- static int
- wcstombs_org(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- char *encoding;
- unsigned long mb, glyph_index;
- wchar_t wc;
- int length;
- int unconv_num = 0;
- CodeSet codeset;
- wchar_t *inbufptr = (wchar_t *) *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- char *default_string = XLC_PUBLIC(lcd, default_string);
- int defstr_len = strlen(default_string);
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- wc = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!wc) {
- if (outbufptr) {*outbufptr++ = '\0';}
- (*to_left)--;
- continue;
- }
- /* convert */
- if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
- /* output default_string of falDefaultString() */
- if (*to_left < defstr_len)
- break;
- if (outbufptr) {strncpy((char *)outbufptr, default_string, defstr_len);}
- if (outbufptr) {outbufptr += defstr_len;}
- (*to_left) -= defstr_len;
- unconv_num++;
- } else {
- mb = gi_to_mb(glyph_index, codeset);
- if (codeset->parse_info) {
- /* output shift sequence */
- encoding = codeset->parse_info->encoding;
- length = strlen(encoding);
- if (*to_left < length)
- break;
- if (outbufptr) {strncpy((char *)outbufptr, encoding, length);}
- if (outbufptr) {outbufptr += length;}
- (*to_left) -= length;
- }
- /* output characters */
- length = codeset->length;
- if (*to_left < length)
- break;
- if (outbufptr) {strncpy((char *)outbufptr, ((char *)&mb)+sizeof(mb)-length, length);}
- if (outbufptr) {outbufptr += length;}
- (*to_left) -= length;
- }
- } /* end of while */
- *from = *from + from_size;
- *from_left = 0;
- *to = outbufptr;
- return unconv_num;
- }
- static int
- wcstocts(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- size_t table_size = sizeof(default_ct_data) / sizeof(default_ct_data[0]);
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- unsigned long glyph_index;
- wchar_t wc;
- int i, total_len, seq_len, name_len;
- int unconv_num = 0;
- Bool first_flag = True, standard_flag;
- XlcSide side;
- CodeSet codeset;
- XlcCharSet charset, old_charset = NULL;
- char *ct_sequence;
- XPointer p;
- wchar_t *inbufptr = (wchar_t *) *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- wc = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!wc) {
- if (outbufptr) {*outbufptr++ = '\0';}
- (*to_left)--;
- continue;
- }
- /* convert */
- if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
- unconv_num++;
- continue;
- }
- /* parse charset */
- if ( !(charset = gi_parse_charset(glyph_index, codeset)) ) {
- unconv_num++;
- continue;
- }
- /* Standard Character Set Encoding ? */
- standard_flag = False;
- for (i = 0; i < table_size; i++)
- if (compare(charset->ct_sequence,
- default_ct_data[i].encoding, strlen(charset->ct_sequence)))
- standard_flag = True;
- /*
- * Non-Standard Character Set Encoding
- *
- * +-----+-----+-----+-----+-----+-----+-----+---- ----+-----+-----+
- * | esc sequence | M | L | encoding name | STX |
- * +-----+-----+-----+-----+-----+-----+-----+---- ----+-----+-----+
- * 4bytes 1byte 1byte variable length 1byte
- * | |
- * +-----------------------------------------+
- * name length = ((M - 128) * 128) + (L - 128)
- */
- /* make encoding data */
- ct_sequence = charset->ct_sequence;
- side = charset->side;
- seq_len = strlen(charset->ct_sequence);
- if (standard_flag) {
- name_len = 0;
- total_len = seq_len;
- } else {
- name_len = 2 + strlen(charset->encoding_name) + 1;
- total_len = seq_len + name_len;
- }
- /* output escape sequence of CT */
- if ( (charset != old_charset) &&
- !(first_flag && (strcmp(charset->encoding_name,"ISO8859-1")==0))) {
- if (*to_left < total_len + 1) {
- unconv_num++;
- break;
- }
- if (outbufptr) {
- strcpy((char *)outbufptr, charset->ct_sequence);
- outbufptr += seq_len;
- if (!standard_flag) {
- *outbufptr++ = name_len / 128 + 128;
- *outbufptr++ = name_len % 128 + 128;
- strcpy((char *)outbufptr, charset->encoding_name);
- outbufptr = outbufptr + name_len - 2 - 1;
- *outbufptr++ = STX;
- }
- }
- (*to_left) -= total_len;
- first_flag = False;
- old_charset = charset;
- }
- /* output glyph index */
- if (codeset->ctconv)
- glyph_index = conv_to_dest(codeset->ctconv, glyph_index);
- if (*to_left < charset->char_size) {
- unconv_num++;
- break;
- }
- p = ((XPointer)&glyph_index) + sizeof(glyph_index) - charset->char_size;
- for (i = 0; i < charset->char_size; i++) {
- if (side == XlcC0 || side == XlcGL) {
- if (outbufptr) {*outbufptr++ = *p++ & GL;}
- } else if (side == XlcC1 || side == XlcGR) {
- if (outbufptr) {*outbufptr++ = *p++ | GR;}
- } else {
- if (outbufptr) {*outbufptr++ = *p++;}
- }
- }
- (*to_left) -= charset->char_size;
- } /* end of while */
- *from = *from + from_size;
- *from_left = 0;
- *to = outbufptr;
- return unconv_num;
- }
- static int
- ctstowcs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- unsigned char ch;
- unsigned long glyph_index = 0;
- wchar_t wc;
- int ctr_seq_len = 0, gi_len_left = 0, gi_len = 0;
- int unconv_num = 0;
- CodeSet codeset = NULL;
- XlcCharSet charset_tmp;
- XPointer inbufptr = *from;
- wchar_t *outbufptr = (wchar_t *) *to;
- int from_size = *from_left;
- init_state(conv);
- if (from == NULL || *from == NULL) {
- init_state(conv);
- return( 0 );
- }
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- ch = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!ch) {
- if (outbufptr) {*outbufptr++ = L'\0';}
- (*to_left)--;
- /* error check */
- if (gi_len_left) {
- unconv_num += (gi_len - gi_len_left);
- gi_len_left = 0;
- }
- continue;
- }
- /* same glyph_index data */
- if (gi_len_left){
- if (state->charset->side == XlcC1 || state->charset->side == XlcGR)
- glyph_index = (glyph_index << 8) | (ch & GL);
- else
- glyph_index = (glyph_index << 8) | ch;
- gi_len_left--;
- /* last of one glyph_index data */
- if (!gi_len_left) {
- /* segment conversion */
- charset_tmp = state->charset;
- if ( !segment_conversion(lcd, &charset_tmp, &glyph_index) ) {
- unconv_num += gi_len;
- continue;
- }
- /* get codeset */
- if ( !_XlcGetCodeSetFromCharSet(lcd, charset_tmp,
- &codeset, &glyph_index) ) {
- unconv_num += gi_len;
- continue;
- }
- /* convert glyph index to wicd char */
- gi_to_wc(lcd, glyph_index, codeset, &wc);
- if (outbufptr) {*outbufptr++ = wc;}
- (*to_left)--;
- }
- continue;
- }
- /* control sequence ? */
- if (ch == CSI) {
- if ( !ct_parse_csi(inbufptr - 1, &ctr_seq_len) )
- goto skip_the_seg;
- if (*from_left < ctr_seq_len) {
- inbufptr--;
- (*from_left)++;
- unconv_num += *from_left;
- break;
- }
- /* skip the control sequence */
- inbufptr += (ctr_seq_len - 1);
- *from_left -= (ctr_seq_len - 1);
- continue;
- }
- /* escape sequence ? */
- if (ch == ESC) {
- if ( !ct_parse_charset(lcd,
- inbufptr - 1, &state->charset, &ctr_seq_len) )
- goto skip_the_seg;
- if (*from_left < ctr_seq_len) {
- inbufptr--;
- (*from_left)++;
- unconv_num += *from_left;
- break;
- }
- /* skip the escape sequence */
- inbufptr += (ctr_seq_len - 1);
- *from_left -= (ctr_seq_len - 1);
- continue;
- }
- /* check current state */
- if (state->charset) {
- if (state->charset->side == XlcC0 ||
- state->charset->side == XlcGL) {
- state->GL_charset = state->charset;
- } else if (state->charset->side == XlcC1 ||
- state->charset->side == XlcGR) {
- state->GR_charset = state->charset;
- }
- } else {
- if (isleftside(ch))
- state->charset = state->GL_charset;
- else
- state->charset = state->GR_charset;
- }
- gi_len = gi_len_left = state->charset->char_size;
- glyph_index = 0;
- skip_the_seg:
- /* skip until next escape or control sequence */
- while ( *from_left ) {
- ch = *inbufptr++;
- (*from_left)--;
- unconv_num++;
- if (ch == ESC || ch == CSI) {
- inbufptr--;
- (*from_left)++;
- unconv_num--;
- break;
- }
- }
- if ( !(*from_left) )
- break;
- } /* end of while */
- /* error check on last char */
- if (gi_len_left) {
- inbufptr -= (gi_len - gi_len_left);
- (*from_left) += (gi_len - gi_len_left);
- unconv_num += (gi_len - gi_len_left);
- }
- *from = *from + from_size;
- *from_left = 0;
- *to = (XPointer)outbufptr;
- return unconv_num;
- }
- static int
- mbstocts(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- XPointer buf = Xmalloc((*from_left) * sizeof(wchar_t));
- XPointer buf_ptr1 = buf;
- int buf_left1 = (*from_left);
- XPointer buf_ptr2 = buf_ptr1;
- int buf_left2;
- int unconv_num;
- unconv_num = mbstowcs_org(conv,
- from, from_left, &buf_ptr1, &buf_left1, args, num_args);
- if (unconv_num < 0){
- if (buf)
- Xfree((char *)buf);
- return unconv_num;
- }
- buf_left2 = (buf_ptr1 - buf_ptr2) / sizeof(wchar_t);
- unconv_num += wcstocts(conv,
- &buf_ptr2, &buf_left2, to, to_left, args, num_args);
- if (unconv_num < 0){
- if (buf)
- Xfree((char *)buf);
- return unconv_num;
- }
- }
- static int
- mbstostr(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- unsigned char ch;
- unsigned long mb = 0;
- int length = 0, len_left = 0;
- int unconv_num = 0;
- int num;
- Bool ss_flag = 0;
- CodeSet codeset = NULL;
- ParseInfo parse_info;
- XPointer inbufptr = *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- unsigned char *mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
- ParseInfo *mb_parse_list = XLC_GENERIC(lcd, mb_parse_list);
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- ch = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!ch) {
- if (outbufptr) {*outbufptr++ = '\0';}
- (*to_left)--;
- /* error check */
- if (len_left) {
- unconv_num += (length - len_left);
- len_left = 0;
- }
- continue;
- }
- /* same mb char data */
- if (len_left){
- mb = (mb << 8) | ch; /* 1 byte left shift */
- len_left--;
- /* last of one mb char data */
- if (!len_left) {
- if (check_string_encoding(codeset)) {
- if (outbufptr) {*outbufptr++ = mb & 0xff;}
- (*to_left)--;
- } else {
- unconv_num++;
- }
- }
- }
- /* next mb char data for single shift ? */
- if (mb_parse_table) {
- if ((num = mb_parse_table[ch]) > 0) {
- parse_info = mb_parse_list[num - 1];
- codeset = parse_info->codeset;
- length = len_left = codeset->length;
- mb = 0;
- ss_flag = 1;
- continue;
- }
- }
- /* next char data : byteM ? */
- if ((codeset = byteM_parse_codeset(lcd, (inbufptr - 1))) ||
- /* next char data : GL or GR side ? */
- (codeset = GLGR_parse_codeset(lcd, ch))){
- length = len_left = codeset->length;
- mb = 0;
- ss_flag = 0;
- }
- /* can't find codeset for the ch */
- unconv_num++;
- continue;
- } /* end of while */
- /* error check on last char */
- if (len_left) {
- inbufptr -= (length - len_left + ss_flag);
- (*from_left) += (length - len_left + ss_flag);
- unconv_num += (length - len_left + ss_flag);
- }
- *from = *from + from_size;
- *from_left = 0;
- *to = (XPointer)outbufptr;
- return unconv_num;
- }
- static int
- mbtocs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- unsigned char ch;
- unsigned long mb = 0;
- unsigned long glyph_index;
- int length = 0, len_left = 0, char_len;
- int unconv_num = 0;
- int i, num;
- XlcSide side;
- CodeSet codeset = NULL;
- XlcCharSet charset = NULL;
- ParseInfo parse_info;
- XPointer p;
- XPointer inbufptr = *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- unsigned char *mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
- ParseInfo *mb_parse_list = XLC_GENERIC(lcd, mb_parse_list);
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- ch = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!ch) {
- unconv_num = 1;
- if (len_left)
- unconv_num += (length - len_left);
- break;
- }
- /* same mb char data */
- if (len_left)
- goto output;
- /* next mb char data for single shift ? */
- if (mb_parse_table) {
- if ((num = mb_parse_table[ch]) > 0) {
- parse_info = mb_parse_list[num - 1];
- codeset = parse_info->codeset;
- length = len_left = codeset->length;
- mb = 0;
- continue;
- }
- }
- /* next mb char data for byteM ? */
- if ((codeset = byteM_parse_codeset(lcd, (inbufptr - 1))) ||
- /* next mb char data for GL or GR side ? */
- (codeset = GLGR_parse_codeset(lcd, ch))){
- length = len_left = codeset->length;
- mb = 0;
- }
- /* can't find codeset for the ch */
- unconv_num = 1;
- break;
- output:
- mb = (mb << 8) | ch; /* 1 byte left shift */
- len_left--;
- /* last of one mb char data */
- if (!len_left) {
- glyph_index = mb_to_gi(mb, codeset);
- if (!(charset = gi_parse_charset(glyph_index, codeset))) {
- unconv_num = length;
- break;
- }
- char_len = charset->char_size;
- side = charset->side;
- /* output glyph index */
- if (codeset->ctconv)
- glyph_index = conv_to_dest(codeset->ctconv, glyph_index);
- if (*to_left < char_len) {
- unconv_num = length;
- break;
- }
- p = ((XPointer)&glyph_index) + sizeof(glyph_index) - char_len;
- for (i = 0; i < char_len; i++) {
- if (side == XlcC0 || side == XlcGL) {
- if (outbufptr) {*outbufptr++ = *p++ & GL;}
- } else if (side == XlcC1 || side == XlcGR) {
- if (outbufptr) {*outbufptr++ = *p++ | GR;}
- } else {
- if (outbufptr) {*outbufptr++ = *p++;}
- }
- }
- (*to_left) -= char_len;
- break;
- }
- } /* end of while */
- /* error end */
- if (unconv_num) {
- *from = *from + from_size;
- *from_left = 0;
- *to = outbufptr;
- return -1;
- }
- /* nomal end */
- *from = inbufptr;
- *to = outbufptr;
- if (num_args > 0)
- *((XlcCharSet *) args[0]) = charset;
- return 0;
- }
- static int
- mbstocs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- int ret;
- XlcCharSet charset_old, charset = NULL;
- XPointer tmp_args[1];
- XPointer inbufptr;
- int in_left;
- XPointer outbufptr;
- int out_left;
- tmp_args[0] = (XPointer) &charset;
- ret = mbtocs(conv, from, from_left, to, to_left, tmp_args, 1);
- charset_old = charset;
- inbufptr = *from;
- in_left = *from_left;
- outbufptr = *to;
- out_left = *to_left;
- while ( ret == 0 && *from_left && *to_left && charset_old == charset ) {
- charset_old = charset;
- inbufptr = *from;
- in_left = *from_left;
- outbufptr = *to;
- out_left = *to_left;
- ret = mbtocs(conv, from, from_left, to, to_left, tmp_args, 1);
- }
- *from = inbufptr;
- *from_left = in_left;
- *to = outbufptr;
- *to_left = out_left;
- if (num_args > 0)
- *((XlcCharSet *) args[0]) = charset_old;
- /* error end */
- if (ret != 0)
- return( -1 );
- return(0);
- }
- static int
- wcstostr(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- char *encoding;
- unsigned long mb, glyph_index;
- wchar_t wc;
- int length;
- int unconv_num = 0;
- CodeSet codeset;
- wchar_t *inbufptr = (wchar_t *) *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- char *default_string = XLC_PUBLIC(lcd, default_string);
- int defstr_len = strlen(default_string);
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- wc = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!wc) {
- if (outbufptr) {*outbufptr++ = '\0';}
- (*to_left)--;
- continue;
- }
- /* convert */
- if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
- /* output default_string of falDefaultString() */
- if (*to_left < defstr_len)
- break;
- if (outbufptr) {strncpy((char *)outbufptr, default_string, defstr_len);}
- if (outbufptr) {outbufptr += defstr_len;}
- (*to_left) -= defstr_len;
- unconv_num++;
- } else {
- mb = gi_to_mb(glyph_index, codeset);
- if (codeset->parse_info) {
- /* output shift sequence */
- encoding = codeset->parse_info->encoding;
- length = strlen(encoding);
- if (*to_left < length)
- break;
- if (check_string_encoding(codeset)) {
- if (outbufptr) {strncpy((char *)outbufptr, encoding, length);}
- if (outbufptr) {outbufptr += length;}
- (*to_left) -= length;
- }
- }
- /* output characters */
- length = codeset->length;
- if (*to_left < length)
- break;
- if (check_string_encoding(codeset)) {
- if (outbufptr) {strncpy((char *)outbufptr,
- ((char *)&mb) + sizeof(mb) - length, length);}
- if (outbufptr) {outbufptr += length;}
- (*to_left) -= length;
- } else {
- unconv_num++;
- }
- }
- } /* end of while */
- *from = *from + from_size;
- *from_left = 0;
- *to = outbufptr;
- return unconv_num;
- }
- static int
- wctocs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- wchar_t wc;
- unsigned long glyph_index;
- int i, char_len;
- int unconv_num = 0;
- XlcSide side;
- CodeSet codeset;
- XlcCharSet charset;
- XPointer p;
- wchar_t *inbufptr = (wchar_t *) *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- if (*from_left > *to_left)
- *from_left = *to_left;
- if (*from_left && *to_left) {
- wc = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!wc) {
- unconv_num = 1;
- goto end;
- }
- /* convert */
- if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
- unconv_num = 1;
- goto end;
- }
- if ( !(charset = gi_parse_charset(glyph_index, codeset)) ) {
- unconv_num = 1;
- goto end;
- }
- char_len = charset->char_size;
- side = charset->side;
- /* output glyph index */
- if (codeset->ctconv)
- glyph_index = conv_to_dest(codeset->ctconv, glyph_index);
- if (*to_left < char_len) {
- unconv_num++;
- goto end;
- }
- p = ((XPointer)&glyph_index) + sizeof(glyph_index) - char_len;
- for (i = 0; i < char_len; i++) {
- if (side == XlcC0 || side == XlcGL) {
- if (outbufptr) {*outbufptr++ = *p++ & GL;}
- } else if (side == XlcC1 || side == XlcGR) {
- if (outbufptr) {*outbufptr++ = *p++ | GR;}
- } else {
- if (outbufptr) {*outbufptr++ = *p++;}
- }
- }
- (*to_left) -= char_len;
- }
- end:
- /* error end */
- if (unconv_num) {
- *from = *from + from_size;
- *from_left = 0;
- *to = outbufptr;
- return -1;
- }
- /* nomal end */
- *from = (XPointer)inbufptr;
- *to = outbufptr;
- if (num_args > 0)
- *((XlcCharSet *) args[0]) = charset;
- return 0;
- }
- static int
- wcstocs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- int ret;
- XlcCharSet charset_old, charset = NULL;
- XPointer tmp_args[1];
- wchar_t *inbufptr;
- int in_left;
- XPointer outbufptr;
- int out_left;
- tmp_args[0] = (XPointer) &charset;
- ret = wctocs(conv, from, from_left, to, to_left, tmp_args, 1);
- charset_old = charset;
- inbufptr = (wchar_t *)(*from);
- in_left = *from_left;
- outbufptr = *to;
- out_left = *to_left;
- while ( ret == 0 && *from_left && *to_left && charset_old == charset ) {
- charset_old = charset;
- inbufptr = (wchar_t *)(*from);
- in_left = *from_left;
- outbufptr = *to;
- out_left = *to_left;
- ret = wctocs(conv, from, from_left, to, to_left, tmp_args, 1);
- }
- *from = (XPointer)inbufptr;
- *from_left = in_left;
- *to = outbufptr;
- *to_left = out_left;
- if (num_args > 0)
- *((XlcCharSet *) args[0]) = charset_old;
- /* error end */
- if (ret != 0)
- return( -1 );
- return(0);
- }
- static int
- ctstombs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- XPointer buf = Xmalloc((*from_left) * sizeof(wchar_t));
- XPointer buf_ptr1 = buf;
- int buf_left1 = (*from_left);
- XPointer buf_ptr2 = buf_ptr1;
- int buf_left2;
- int unconv_num;
- unconv_num = ctstowcs(conv,
- from, from_left, &buf_ptr1, &buf_left1, args, num_args);
- if (unconv_num < 0){
- if (buf)
- Xfree((char *)buf);
- return unconv_num;
- }
- buf_left2 = (buf_ptr1 - buf_ptr2) / sizeof(wchar_t);
- unconv_num += wcstombs_org(conv,
- &buf_ptr2, &buf_left2, to, to_left, args, num_args);
- if (unconv_num < 0){
- if (buf)
- Xfree((char *)buf);
- return unconv_num;
- }
- }
- static int
- strtombs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- char *encoding;
- unsigned long mb, glyph_index;
- unsigned char ch;
- int length;
- int unconv_num = 0;
- CodeSet codeset;
- XPointer inbufptr = *from;
- XPointer outbufptr = *to;
- int from_size = *from_left;
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- ch = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!ch) {
- if (outbufptr) {*outbufptr++ = '\0';}
- (*to_left)--;
- continue;
- }
- /* convert */
- if (isleftside(ch)) {
- glyph_index = ch;
- codeset = _fallcGetCodeSetFromName(lcd, "ISO8859-1:GL");
- } else {
- glyph_index = ch & GL;
- codeset = _fallcGetCodeSetFromName(lcd, "ISO8859-1:GR");
- }
- if (!codeset) {
- unconv_num++;
- continue;
- }
- mb = gi_to_mb(glyph_index, codeset);
- if (codeset->parse_info) {
- /* output shift sequence */
- encoding = codeset->parse_info->encoding;
- length = strlen(encoding);
- if (*to_left < length)
- break;
- if (outbufptr) {strncpy((char *)outbufptr, encoding, length);}
- if (outbufptr) {outbufptr += length;}
- (*to_left) -= length;
- }
- /* output characters */
- length = codeset->length;
- if (*to_left < length)
- break;
- if (outbufptr) {strncpy((char *)outbufptr, ((char *)&mb)+sizeof(mb)-length, length);}
- if (outbufptr) {outbufptr += length;}
- (*to_left) -= length;
- } /* end of while */
- *from = *from + from_size;
- *from_left = 0;
- *to = outbufptr;
- return unconv_num;
- }
- static int
- strtowcs(
- XlcConv conv,
- XPointer *from,
- int *from_left,
- XPointer *to,
- int *to_left,
- XPointer *args,
- int num_args)
- {
- State state = (State) conv->state;
- XLCd lcd = state->lcd;
- unsigned char ch;
- unsigned long glyph_index;
- wchar_t wc;
- int unconv_num = 0;
- CodeSet codeset;
- XPointer inbufptr = *from;
- wchar_t *outbufptr = (wchar_t *)*to;
- int from_size = *from_left;
- if (*from_left > *to_left)
- *from_left = *to_left;
- while (*from_left && *to_left) {
- ch = *inbufptr++;
- (*from_left)--;
- /* null ? */
- if (!ch) {
- if (outbufptr) {*outbufptr++ = L'\0';}
- (*to_left)--;
- continue;
- }
- /* convert */
- if (isleftside(ch)) {
- glyph_index = ch;
- codeset = _fallcGetCodeSetFromName(lcd, "ISO8859-1:GL");
- } else {
- glyph_index = ch & GL;
- codeset = _fallcGetCodeSetFromName(lcd, "ISO8859-1:GR");
- }
- if (!codeset) {
- unconv_num++;
- continue;
- }
- gi_to_wc(lcd, glyph_index, codeset, &wc);
- if (outbufptr) {*outbufptr++ = wc;}
- (*to_left)--;
- } /* end of while */
- *from = *from + from_size;
- *from_left = 0;
- *to = (XPointer)outbufptr;
- return unconv_num;
- }
- /* -------------------------------------------------------------------------- */
- /* Close */
- /* -------------------------------------------------------------------------- */
- static void
- close_converter(XlcConv conv)
- {
- if (conv->state) {
- Xfree((char *) conv->state);
- }
- if (conv->methods) {
- Xfree((char *) conv->methods);
- }
- Xfree((char *) conv);
- }
- /* -------------------------------------------------------------------------- */
- /* Open */
- /* -------------------------------------------------------------------------- */
- static XlcConv
- create_conv(XLCd lcd, XlcConvMethods methods)
- {
- XlcConv conv;
- State state;
- conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
- if (conv == NULL)
- return (XlcConv) NULL;
- conv->methods = (XlcConvMethods) Xmalloc(sizeof(XlcConvMethodsRec));
- if (conv->methods == NULL){
- close_converter(conv);
- return (XlcConv) NULL;
- }
- *conv->methods = *methods;
- if (XLC_PUBLIC(lcd, is_state_depend))
- conv->methods->reset = init_state;
- conv->state = (XPointer) Xmalloc(sizeof(StateRec));
- if (conv->state == NULL){
- close_converter(conv);
- return (XlcConv) NULL;
- }
- bzero((char *) conv->state, sizeof(StateRec));
- state = (State) conv->state;
- state->lcd = lcd;
- init_state(conv);
- return conv;
- }
- static XlcConvMethodsRec mbstowcs_methods = {
- close_converter,
- mbstowcs_org,
- NULL
- } ;
- static XlcConv
- open_mbstowcs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &mbstowcs_methods);
- }
- static XlcConvMethodsRec mbstocts_methods = {
- close_converter,
- mbstocts,
- NULL
- } ;
- static XlcConv
- open_mbstocts(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &mbstocts_methods);
- }
- static XlcConvMethodsRec mbstostr_methods = {
- close_converter,
- mbstostr,
- NULL
- } ;
- static XlcConv
- open_mbstostr(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &mbstostr_methods);
- }
- static XlcConvMethodsRec mbstocs_methods = {
- close_converter,
- mbstocs,
- NULL
- } ;
- static XlcConv
- open_mbstocs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &mbstocs_methods);
- }
- static XlcConvMethodsRec mbtocs_methods = {
- close_converter,
- mbtocs,
- NULL
- } ;
- static XlcConv
- open_mbtocs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &mbtocs_methods);
- }
- static XlcConvMethodsRec wcstombs_methods = {
- close_converter,
- wcstombs_org,
- NULL
- } ;
- static XlcConv
- open_wcstombs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &wcstombs_methods);
- }
- static XlcConvMethodsRec wcstocts_methods = {
- close_converter,
- wcstocts,
- NULL
- } ;
- static XlcConv
- open_wcstocts(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &wcstocts_methods);
- }
- static XlcConvMethodsRec wcstostr_methods = {
- close_converter,
- wcstostr,
- NULL
- } ;
- static XlcConv
- open_wcstostr(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &wcstostr_methods);
- }
- static XlcConvMethodsRec wcstocs_methods = {
- close_converter,
- wcstocs,
- NULL
- } ;
- static XlcConv
- open_wcstocs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &wcstocs_methods);
- }
- static XlcConvMethodsRec wctocs_methods = {
- close_converter,
- wctocs,
- NULL
- } ;
- static XlcConv
- open_wctocs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &wctocs_methods);
- }
- static XlcConvMethodsRec ctstombs_methods = {
- close_converter,
- ctstombs,
- NULL
- } ;
- static XlcConv
- open_ctstombs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &ctstombs_methods);
- }
- static XlcConvMethodsRec ctstowcs_methods = {
- close_converter,
- ctstowcs,
- NULL
- } ;
- static XlcConv
- open_ctstowcs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &ctstowcs_methods);
- }
- static XlcConvMethodsRec strtombs_methods = {
- close_converter,
- strtombs,
- NULL
- } ;
- static XlcConv
- open_strtombs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &strtombs_methods);
- }
- static XlcConvMethodsRec strtowcs_methods = {
- close_converter,
- strtowcs,
- NULL
- } ;
- static XlcConv
- open_strtowcs(XLCd from_lcd, char *from_type, XLCd to_lcd, char *to_type)
- {
- return create_conv(from_lcd, &strtowcs_methods);
- }
- /* -------------------------------------------------------------------------- */
- /* Loader */
- /* -------------------------------------------------------------------------- */
- XLCd
- _fallcGenericLoader(char *name)
- {
- XLCd lcd;
- lcd = _fallcCreateLC(name, _fallcGenericMethods);
- if (lcd == NULL)
- return lcd;
- _fallcSetConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar, open_mbstowcs);
- _fallcSetConverter(lcd, XlcNMultiByte, lcd, XlcNCompoundText, open_mbstocts);
- _fallcSetConverter(lcd, XlcNMultiByte, lcd, XlcNString, open_mbstostr);
- _fallcSetConverter(lcd, XlcNMultiByte, lcd, XlcNCharSet, open_mbstocs);
- _fallcSetConverter(lcd, XlcNMultiByte, lcd, XlcNChar, open_mbtocs);
- _fallcSetConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte, open_wcstombs);
- _fallcSetConverter(lcd, XlcNWideChar, lcd, XlcNCompoundText, open_wcstocts);
- _fallcSetConverter(lcd, XlcNWideChar, lcd, XlcNString, open_wcstostr);
- _fallcSetConverter(lcd, XlcNWideChar, lcd, XlcNCharSet, open_wcstocs);
- _fallcSetConverter(lcd, XlcNWideChar, lcd, XlcNChar, open_wctocs);
- _fallcSetConverter(lcd, XlcNCompoundText, lcd, XlcNMultiByte, open_ctstombs);
- _fallcSetConverter(lcd, XlcNCompoundText, lcd, XlcNWideChar, open_ctstowcs);
- _fallcSetConverter(lcd, XlcNString, lcd, XlcNMultiByte, open_strtombs);
- _fallcSetConverter(lcd, XlcNString, lcd, XlcNWideChar, open_strtowcs);
- return lcd;
- }
|