michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define NOMAPPING 0xfffd michael@0: michael@0: typedef struct { michael@0: uint16_t srcBegin; /* 2 byte */ michael@0: uint16_t srcEnd; /* 2 byte */ michael@0: uint16_t destBegin; /* 2 byte */ michael@0: } uFormat0; michael@0: michael@0: typedef struct { michael@0: uint16_t srcBegin; /* 2 byte */ michael@0: uint16_t srcEnd; /* 2 byte */ michael@0: uint16_t mappingOffset; /* 2 byte */ michael@0: } uFormat1; michael@0: michael@0: typedef struct { michael@0: uint16_t srcBegin; /* 2 byte */ michael@0: uint16_t srcEnd; /* 2 byte -waste */ michael@0: uint16_t destBegin; /* 2 byte */ michael@0: } uFormat2; michael@0: michael@0: typedef struct { michael@0: union { michael@0: uFormat0 format0; michael@0: uFormat1 format1; michael@0: uFormat2 format2; michael@0: } fmt; michael@0: } uMapCell; michael@0: michael@0: /* ================================================= michael@0: uTable michael@0: ================================================= */ michael@0: typedef struct { michael@0: uint16_t itemOfList; michael@0: uint16_t offsetToFormatArray; michael@0: uint16_t offsetToMapCellArray; michael@0: uint16_t offsetToMappingTable; michael@0: uint16_t data[1]; michael@0: } uTable; michael@0: michael@0: uint16_t umap[256][256]; michael@0: int bInitFromOrTo = 0; michael@0: int bGenerateFromUnicodeTable = 0; michael@0: michael@0: #define MAXCELLNUM 1000 michael@0: michael@0: static int numOfItem = 0; michael@0: uMapCell cell[MAXCELLNUM]; michael@0: uint16_t format[MAXCELLNUM / 4]; michael@0: uint16_t mapping[256*256]; michael@0: static int mappinglen = 0; michael@0: static int formatcount[4] = {0,0,0,0}; michael@0: michael@0: #define SetFormat(n,f) { format[(n >> 2)] |= ((f) << ((n & 0x0003) << 2)); formatcount[f]++; } michael@0: #define GetFormat(n) ( format[(n >> 2)] >> ((n & 0x0003) << 2)) &0x00FF) michael@0: #define MAPVALUE(i) (umap[(i >> 8) & 0xFF][(i) & 0xFF]) michael@0: michael@0: int FORMAT1CNST = 10 ; michael@0: int FORMAT0CNST = 5 ; michael@0: void initmaps() michael@0: { michael@0: int i,j; michael@0: for(i=0;i<256;i++) michael@0: for(j=0;j<256;j++) michael@0: { michael@0: umap[i][j]= NOMAPPING; michael@0: } michael@0: for(i=0;i \n"); michael@0: printf("\n"); michael@0: printf(" Table in Debug form \n"); michael@0: michael@0: for(begin = 0; MAPVALUE(begin) ==NOMAPPING; begin++) michael@0: ; michael@0: for(end = 0xFFFF; MAPVALUE(end) ==NOMAPPING; end--) michael@0: ; michael@0: if(end != begin) michael@0: { michael@0: lastdiff = MAPVALUE(begin) - begin; michael@0: for(gp=begin+1,state = 0 ; gp<=end; gp++) michael@0: { michael@0: int input ; michael@0: diff = MAPVALUE(gp) - gp; michael@0: input = (diff == lastdiff); michael@0: switch(state) michael@0: { michael@0: case 0: michael@0: if(input) michael@0: { michael@0: state = 1; michael@0: ss = gp -1; michael@0: gc = 2; michael@0: } michael@0: break; michael@0: case 1: michael@0: if(input) michael@0: { michael@0: if(gc++ >= FORMAT0CNST) michael@0: { michael@0: state = 2; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: state = 0; michael@0: } michael@0: break; michael@0: case 2: michael@0: if(input) michael@0: { michael@0: } michael@0: else michael@0: { michael@0: AddFormat0(ss,gp-1); michael@0: state = 0; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: lastdiff = diff; michael@0: } michael@0: } michael@0: if(state == 2) michael@0: AddFormat0(ss,end); michael@0: michael@0: for(;(MAPVALUE(begin) ==NOMAPPING) && (begin <= end); begin++) michael@0: ; michael@0: if(begin <= end) michael@0: { michael@0: for(;(MAPVALUE(end)==NOMAPPING) && (end >= begin); end--) michael@0: ; michael@0: for(ss=gp=begin,state = 0 ; gp<=end; gp++) michael@0: { michael@0: int input = (MAPVALUE(gp) == NOMAPPING); michael@0: switch(state) michael@0: { michael@0: case 0: michael@0: if(input) michael@0: { michael@0: gc = 1; michael@0: gs = gp; michael@0: state = 1; michael@0: } michael@0: break; michael@0: case 1: michael@0: if(input) michael@0: { michael@0: if(gc++ >= FORMAT1CNST) michael@0: state = 2; michael@0: } michael@0: else michael@0: state = 0; michael@0: break; michael@0: case 2: michael@0: if(input) michael@0: { michael@0: } michael@0: else michael@0: { michael@0: if(gs == (ss+1)) michael@0: AddFormat2(ss); michael@0: else michael@0: AddFormat1(ss ,gs-1); michael@0: state = 0; michael@0: ss = gp; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: if(end == ss) michael@0: AddFormat2(ss ); michael@0: else michael@0: AddFormat1(ss ,end ); michael@0: } michael@0: printf("========================================================*/\n"); michael@0: } michael@0: void writetable() michael@0: { michael@0: uint16_t i; michael@0: uint16_t off1,off2,off3; michael@0: uint16_t cur = 0; michael@0: uint16_t formatitem = (((numOfItem)>>2) + 1); michael@0: off1 = 4; michael@0: off2 = off1 + formatitem ; michael@0: off3 = off2 + numOfItem * sizeof(uMapCell) / sizeof(uint16_t); michael@0: /* write itemOfList */ michael@0: printf("/* Offset=0x%04X ItemOfList */\n 0x%04X,\n", cur++, numOfItem); michael@0: michael@0: /* write offsetToFormatArray */ michael@0: printf("/*-------------------------------------------------------*/\n"); michael@0: printf("/* Offset=0x%04X offsetToFormatArray */\n 0x%04X,\n", cur++,off1); michael@0: michael@0: /* write offsetToMapCellArray */ michael@0: printf("/*-------------------------------------------------------*/\n"); michael@0: printf("/* Offset=0x%04X offsetToMapCellArray */ \n 0x%04X,\n", cur++,off2); michael@0: michael@0: /* write offsetToMappingTable */ michael@0: printf("/*-------------------------------------------------------*/\n"); michael@0: printf("/* Offset=0x%04X offsetToMappingTable */ \n 0x%04X,\n", cur++,off3); michael@0: michael@0: /* write FormatArray */ michael@0: printf("/*-------------------------------------------------------*/\n"); michael@0: printf("/* Offset=0x%04X Start of Format Array */ \n",cur); michael@0: printf("/* Total of Format 0 : 0x%04X */\n" michael@0: , formatcount[0]); michael@0: printf("/* Total of Format 1 : 0x%04X */\n" michael@0: , formatcount[1]); michael@0: printf("/* Total of Format 2 : 0x%04X */\n" michael@0: , formatcount[2]); michael@0: printf("/* Total of Format 3 : 0x%04X */\n" michael@0: , formatcount[3]); michael@0: for(i=0;i 0) michael@0: { michael@0: FORMAT0CNST = cnst0; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: fprintf(stderr, "argc error !!!!\n"); michael@0: exit(-1); michael@0: } michael@0: i++; michael@0: } michael@0: if((strncmp("-1", argv[i],2) == 0) && ((i+1) < argc)) michael@0: { michael@0: int cnst1; michael@0: if(sscanf(argv[i+1], "%d", &cnst1) == 1) michael@0: { michael@0: if(cnst1 > 0) michael@0: { michael@0: FORMAT1CNST = cnst1; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: fprintf(stderr, "argc error !!!!\n"); michael@0: exit(-1); michael@0: } michael@0: i++; michael@0: } michael@0: } michael@0: if(! bInitFromOrTo) michael@0: { michael@0: usage(); michael@0: exit(-1); michael@0: } michael@0: fprintf(stderr, "format 0 cnst = %d\n", FORMAT0CNST); michael@0: fprintf(stderr, "format 1 cnst = %d\n", FORMAT1CNST); michael@0: fprintf(stderr, "generate u%c table\n", michael@0: bGenerateFromUnicodeTable ? 'f' : 't'); michael@0: } michael@0: void getinput() michael@0: { michael@0: char buf[256]; michael@0: short c,u; michael@0: for (; fgets(buf,sizeof(buf),stdin);) michael@0: { michael@0: if(buf[0]=='0' && buf[1] == 'x') michael@0: { michael@0: sscanf(buf,"%hx %hx",&c,&u); michael@0: if(bGenerateFromUnicodeTable) michael@0: SetMapValue(u, c); michael@0: else michael@0: SetMapValue(c, u); michael@0: } michael@0: } michael@0: } michael@0: int main(int argc, char* argv[]) michael@0: { michael@0: parsearg(argc, argv); michael@0: initmaps(); michael@0: getinput(); michael@0: gentable(); michael@0: writetable(); michael@0: return 0; michael@0: }