intl/uconv/tools/umaptable.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <stdio.h>
michael@0 8 #include <string.h>
michael@0 9 #include <stdlib.h>
michael@0 10 #include <stdint.h>
michael@0 11
michael@0 12 #define NOMAPPING 0xfffd
michael@0 13
michael@0 14 typedef struct {
michael@0 15 uint16_t srcBegin; /* 2 byte */
michael@0 16 uint16_t srcEnd; /* 2 byte */
michael@0 17 uint16_t destBegin; /* 2 byte */
michael@0 18 } uFormat0;
michael@0 19
michael@0 20 typedef struct {
michael@0 21 uint16_t srcBegin; /* 2 byte */
michael@0 22 uint16_t srcEnd; /* 2 byte */
michael@0 23 uint16_t mappingOffset; /* 2 byte */
michael@0 24 } uFormat1;
michael@0 25
michael@0 26 typedef struct {
michael@0 27 uint16_t srcBegin; /* 2 byte */
michael@0 28 uint16_t srcEnd; /* 2 byte -waste */
michael@0 29 uint16_t destBegin; /* 2 byte */
michael@0 30 } uFormat2;
michael@0 31
michael@0 32 typedef struct {
michael@0 33 union {
michael@0 34 uFormat0 format0;
michael@0 35 uFormat1 format1;
michael@0 36 uFormat2 format2;
michael@0 37 } fmt;
michael@0 38 } uMapCell;
michael@0 39
michael@0 40 /* =================================================
michael@0 41 uTable
michael@0 42 ================================================= */
michael@0 43 typedef struct {
michael@0 44 uint16_t itemOfList;
michael@0 45 uint16_t offsetToFormatArray;
michael@0 46 uint16_t offsetToMapCellArray;
michael@0 47 uint16_t offsetToMappingTable;
michael@0 48 uint16_t data[1];
michael@0 49 } uTable;
michael@0 50
michael@0 51 uint16_t umap[256][256];
michael@0 52 int bInitFromOrTo = 0;
michael@0 53 int bGenerateFromUnicodeTable = 0;
michael@0 54
michael@0 55 #define MAXCELLNUM 1000
michael@0 56
michael@0 57 static int numOfItem = 0;
michael@0 58 uMapCell cell[MAXCELLNUM];
michael@0 59 uint16_t format[MAXCELLNUM / 4];
michael@0 60 uint16_t mapping[256*256];
michael@0 61 static int mappinglen = 0;
michael@0 62 static int formatcount[4] = {0,0,0,0};
michael@0 63
michael@0 64 #define SetFormat(n,f) { format[(n >> 2)] |= ((f) << ((n & 0x0003) << 2)); formatcount[f]++; }
michael@0 65 #define GetFormat(n) ( format[(n >> 2)] >> ((n & 0x0003) << 2)) &0x00FF)
michael@0 66 #define MAPVALUE(i) (umap[(i >> 8) & 0xFF][(i) & 0xFF])
michael@0 67
michael@0 68 int FORMAT1CNST = 10 ;
michael@0 69 int FORMAT0CNST = 5 ;
michael@0 70 void initmaps()
michael@0 71 {
michael@0 72 int i,j;
michael@0 73 for(i=0;i<256;i++)
michael@0 74 for(j=0;j<256;j++)
michael@0 75 {
michael@0 76 umap[i][j]= NOMAPPING;
michael@0 77 }
michael@0 78 for(i=0;i<MAXCELLNUM / 4;i++)
michael@0 79 format[i]=0;
michael@0 80 }
michael@0 81 void SetMapValue(short u,short c)
michael@0 82 {
michael@0 83 if(NOMAPPING == MAPVALUE(u))
michael@0 84 MAPVALUE(u) = c & 0x0000FFFF;
michael@0 85 else {
michael@0 86 fprintf(stderr, "warning- duplicate mapping %x map to both %x and %x\n", u, MAPVALUE(u), c);
michael@0 87 }
michael@0 88 }
michael@0 89 void AddFormat2(uint16_t srcBegin)
michael@0 90 {
michael@0 91 uint16_t destBegin = MAPVALUE(srcBegin);
michael@0 92 printf("Begin of Item %04X\n",numOfItem);
michael@0 93 printf(" Format 2\n");
michael@0 94 printf(" srcBegin = %04X\n", srcBegin);
michael@0 95 printf(" destBegin = %04X\n", destBegin );
michael@0 96 SetFormat(numOfItem,2);
michael@0 97 cell[numOfItem].fmt.format2.srcBegin = srcBegin;
michael@0 98 cell[numOfItem].fmt.format2.srcEnd = 0;
michael@0 99 cell[numOfItem].fmt.format2.destBegin = destBegin;
michael@0 100 printf("End of Item %04X \n\n",numOfItem);
michael@0 101 numOfItem++;
michael@0 102 /* Unmark the umap */
michael@0 103 MAPVALUE(srcBegin) = NOMAPPING;
michael@0 104 }
michael@0 105 void AddFormat1(uint16_t srcBegin, uint16_t srcEnd)
michael@0 106 {
michael@0 107 uint16_t i;
michael@0 108 printf("Begin of Item %04X\n",numOfItem);
michael@0 109 printf(" Format 1\n");
michael@0 110 printf(" srcBegin = %04X\n", srcBegin);
michael@0 111 printf(" srcEnd = %04X\n", srcEnd );
michael@0 112 printf(" mappingOffset = %04X\n", mappinglen);
michael@0 113 printf(" Mapping = " );
michael@0 114 SetFormat(numOfItem,1);
michael@0 115 cell[numOfItem].fmt.format1.srcBegin = srcBegin;
michael@0 116 cell[numOfItem].fmt.format1.srcEnd = srcEnd;
michael@0 117 cell[numOfItem].fmt.format1.mappingOffset = mappinglen;
michael@0 118 for(i=srcBegin ; i <= srcEnd ; i++,mappinglen++)
michael@0 119 {
michael@0 120 if( ((i-srcBegin) % 8) == 0)
michael@0 121 printf("\n ");
michael@0 122 mapping[mappinglen]= MAPVALUE(i);
michael@0 123 printf("%04X ",(mapping[mappinglen] ));
michael@0 124 /* Unmark the umap */
michael@0 125 MAPVALUE(i) = NOMAPPING;
michael@0 126 }
michael@0 127 printf("\n");
michael@0 128 printf("End of Item %04X \n\n",numOfItem);
michael@0 129 numOfItem++;
michael@0 130 }
michael@0 131 void AddFormat0(uint16_t srcBegin, uint16_t srcEnd)
michael@0 132 {
michael@0 133 uint16_t i;
michael@0 134 uint16_t destBegin = MAPVALUE(srcBegin);
michael@0 135 printf("Begin of Item %04X\n",numOfItem);
michael@0 136 printf(" Format 0\n");
michael@0 137 printf(" srcBegin = %04X\n", srcBegin);
michael@0 138 printf(" srcEnd = %04X\n", srcEnd );
michael@0 139 printf(" destBegin = %04X\n", destBegin );
michael@0 140 SetFormat(numOfItem,0);
michael@0 141 cell[numOfItem].fmt.format0.srcBegin = srcBegin;
michael@0 142 cell[numOfItem].fmt.format0.srcEnd = srcEnd;
michael@0 143 cell[numOfItem].fmt.format0.destBegin = destBegin;
michael@0 144 for(i=srcBegin ; i <= srcEnd ; i++)
michael@0 145 {
michael@0 146 /* Unmark the umap */
michael@0 147 MAPVALUE(i) = NOMAPPING;
michael@0 148 }
michael@0 149 printf("End of Item %04X \n\n",numOfItem);
michael@0 150 numOfItem++;
michael@0 151 }
michael@0 152 void printnpl()
michael@0 153 {
michael@0 154 printf(
michael@0 155 "/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */\n"
michael@0 156 "/* This Source Code Form is subject to the terms of the Mozilla Public\n"
michael@0 157 " * License, v. 2.0. If a copy of the MPL was not distributed with this\n"
michael@0 158 " * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n");
michael@0 159 }
michael@0 160 void gentable()
michael@0 161 {
michael@0 162 /* OK! For now, we just use format 1 for each row */
michael@0 163 /* We need to chage this to use other format to save the space */
michael@0 164 uint16_t begin,end;
michael@0 165 uint16_t ss,gs,gp,state,gc;
michael@0 166 uint16_t diff, lastdiff;
michael@0 167
michael@0 168 printnpl();
michael@0 169 printf("/*========================================================\n");
michael@0 170 printf(" This is a Generated file. Please don't edit it.\n");
michael@0 171 printf("\n");
michael@0 172 printf(" The tool which used to generate this file is called umaptable.\n");
michael@0 173 printf(" You can find this tool under mozilla/intl/uconv/tools/umaptable.c.\n");
michael@0 174
michael@0 175 printf(" If you have any problem of this file. Please contact \n");
michael@0 176 printf(" Netscape Client International Team or \n");
michael@0 177 printf(" ftang@netscape <Frank Tang> \n");
michael@0 178 printf("\n");
michael@0 179 printf(" Table in Debug form \n");
michael@0 180
michael@0 181 for(begin = 0; MAPVALUE(begin) ==NOMAPPING; begin++)
michael@0 182 ;
michael@0 183 for(end = 0xFFFF; MAPVALUE(end) ==NOMAPPING; end--)
michael@0 184 ;
michael@0 185 if(end != begin)
michael@0 186 {
michael@0 187 lastdiff = MAPVALUE(begin) - begin;
michael@0 188 for(gp=begin+1,state = 0 ; gp<=end; gp++)
michael@0 189 {
michael@0 190 int input ;
michael@0 191 diff = MAPVALUE(gp) - gp;
michael@0 192 input = (diff == lastdiff);
michael@0 193 switch(state)
michael@0 194 {
michael@0 195 case 0:
michael@0 196 if(input)
michael@0 197 {
michael@0 198 state = 1;
michael@0 199 ss = gp -1;
michael@0 200 gc = 2;
michael@0 201 }
michael@0 202 break;
michael@0 203 case 1:
michael@0 204 if(input)
michael@0 205 {
michael@0 206 if(gc++ >= FORMAT0CNST)
michael@0 207 {
michael@0 208 state = 2;
michael@0 209 }
michael@0 210 }
michael@0 211 else
michael@0 212 {
michael@0 213 state = 0;
michael@0 214 }
michael@0 215 break;
michael@0 216 case 2:
michael@0 217 if(input)
michael@0 218 {
michael@0 219 }
michael@0 220 else
michael@0 221 {
michael@0 222 AddFormat0(ss,gp-1);
michael@0 223 state = 0;
michael@0 224 }
michael@0 225 break;
michael@0 226 }
michael@0 227
michael@0 228 lastdiff = diff;
michael@0 229 }
michael@0 230 }
michael@0 231 if(state == 2)
michael@0 232 AddFormat0(ss,end);
michael@0 233
michael@0 234 for(;(MAPVALUE(begin) ==NOMAPPING) && (begin <= end); begin++)
michael@0 235 ;
michael@0 236 if(begin <= end)
michael@0 237 {
michael@0 238 for(;(MAPVALUE(end)==NOMAPPING) && (end >= begin); end--)
michael@0 239 ;
michael@0 240 for(ss=gp=begin,state = 0 ; gp<=end; gp++)
michael@0 241 {
michael@0 242 int input = (MAPVALUE(gp) == NOMAPPING);
michael@0 243 switch(state)
michael@0 244 {
michael@0 245 case 0:
michael@0 246 if(input)
michael@0 247 {
michael@0 248 gc = 1;
michael@0 249 gs = gp;
michael@0 250 state = 1;
michael@0 251 }
michael@0 252 break;
michael@0 253 case 1:
michael@0 254 if(input)
michael@0 255 {
michael@0 256 if(gc++ >= FORMAT1CNST)
michael@0 257 state = 2;
michael@0 258 }
michael@0 259 else
michael@0 260 state = 0;
michael@0 261 break;
michael@0 262 case 2:
michael@0 263 if(input)
michael@0 264 {
michael@0 265 }
michael@0 266 else
michael@0 267 {
michael@0 268 if(gs == (ss+1))
michael@0 269 AddFormat2(ss);
michael@0 270 else
michael@0 271 AddFormat1(ss ,gs-1);
michael@0 272 state = 0;
michael@0 273 ss = gp;
michael@0 274 }
michael@0 275 break;
michael@0 276 }
michael@0 277 }
michael@0 278 if(end == ss)
michael@0 279 AddFormat2(ss );
michael@0 280 else
michael@0 281 AddFormat1(ss ,end );
michael@0 282 }
michael@0 283 printf("========================================================*/\n");
michael@0 284 }
michael@0 285 void writetable()
michael@0 286 {
michael@0 287 uint16_t i;
michael@0 288 uint16_t off1,off2,off3;
michael@0 289 uint16_t cur = 0;
michael@0 290 uint16_t formatitem = (((numOfItem)>>2) + 1);
michael@0 291 off1 = 4;
michael@0 292 off2 = off1 + formatitem ;
michael@0 293 off3 = off2 + numOfItem * sizeof(uMapCell) / sizeof(uint16_t);
michael@0 294 /* write itemOfList */
michael@0 295 printf("/* Offset=0x%04X ItemOfList */\n 0x%04X,\n", cur++, numOfItem);
michael@0 296
michael@0 297 /* write offsetToFormatArray */
michael@0 298 printf("/*-------------------------------------------------------*/\n");
michael@0 299 printf("/* Offset=0x%04X offsetToFormatArray */\n 0x%04X,\n", cur++,off1);
michael@0 300
michael@0 301 /* write offsetToMapCellArray */
michael@0 302 printf("/*-------------------------------------------------------*/\n");
michael@0 303 printf("/* Offset=0x%04X offsetToMapCellArray */ \n 0x%04X,\n", cur++,off2);
michael@0 304
michael@0 305 /* write offsetToMappingTable */
michael@0 306 printf("/*-------------------------------------------------------*/\n");
michael@0 307 printf("/* Offset=0x%04X offsetToMappingTable */ \n 0x%04X,\n", cur++,off3);
michael@0 308
michael@0 309 /* write FormatArray */
michael@0 310 printf("/*-------------------------------------------------------*/\n");
michael@0 311 printf("/* Offset=0x%04X Start of Format Array */ \n",cur);
michael@0 312 printf("/* Total of Format 0 : 0x%04X */\n"
michael@0 313 , formatcount[0]);
michael@0 314 printf("/* Total of Format 1 : 0x%04X */\n"
michael@0 315 , formatcount[1]);
michael@0 316 printf("/* Total of Format 2 : 0x%04X */\n"
michael@0 317 , formatcount[2]);
michael@0 318 printf("/* Total of Format 3 : 0x%04X */\n"
michael@0 319 , formatcount[3]);
michael@0 320 for(i=0;i<formatitem;i++,cur++)
michael@0 321 {
michael@0 322 if((i%8) == 0)
michael@0 323 printf("\n");
michael@0 324 printf("0x%04X, ",format[i]);
michael@0 325 }
michael@0 326 printf("\n");
michael@0 327
michael@0 328 /* write MapCellArray */
michael@0 329 printf("/*-------------------------------------------------------*/\n");
michael@0 330 printf("/* Offset=0x%04X Start of MapCell Array */ \n",cur);
michael@0 331 for(i=0;i<numOfItem;i++,cur+=3)
michael@0 332 {
michael@0 333 printf("/* %04X */ 0x%04X, 0x%04X, 0x%04X, \n",
michael@0 334 i,
michael@0 335 cell[i].fmt.format0.srcBegin,
michael@0 336 cell[i].fmt.format0.srcEnd,
michael@0 337 cell[i].fmt.format0.destBegin
michael@0 338 );
michael@0 339 }
michael@0 340
michael@0 341 /* write MappingTable */
michael@0 342 printf("/*-------------------------------------------------------*/\n");
michael@0 343 printf("/* Offset=0x%04X Start of MappingTable */ \n",cur);
michael@0 344 for(i=0;i<mappinglen;i++,cur++)
michael@0 345 {
michael@0 346 if((i%8) == 0)
michael@0 347 printf("\n/* %04X */ ",i);
michael@0 348 printf("0x%04X, ",mapping[i] );
michael@0 349 }
michael@0 350 printf("\n");
michael@0 351 printf("/* End of table Total Length = 0x%04X * 2 */\n",cur);
michael@0 352 }
michael@0 353
michael@0 354 void usage()
michael@0 355 {
michael@0 356 fprintf(stderr, "please indicate what kind of mapping mapping table you want to generate:\n");
michael@0 357 fprintf(stderr, "\t-uf : generate *.uf (from unicode) table, or\n");
michael@0 358 fprintf(stderr, "\t-ut : generate *.ut (to unicode) table\n");
michael@0 359 }
michael@0 360 void parsearg(int argc, char* argv[])
michael@0 361 {
michael@0 362 int i;
michael@0 363 for(i=0;i<argc;i++)
michael@0 364 {
michael@0 365 if(strncmp("-uf", argv[i],3) == 0) {
michael@0 366 if(! bInitFromOrTo) {
michael@0 367 bGenerateFromUnicodeTable = 1;
michael@0 368 bInitFromOrTo = 1;
michael@0 369 } else {
michael@0 370 usage();
michael@0 371 exit(-1);
michael@0 372 }
michael@0 373 }
michael@0 374 if(strncmp("-ut", argv[i],3) == 0) {
michael@0 375 if(! bInitFromOrTo) {
michael@0 376 bGenerateFromUnicodeTable = 0;
michael@0 377 bInitFromOrTo = 1;
michael@0 378 } else {
michael@0 379 usage();
michael@0 380 exit(-1);
michael@0 381 }
michael@0 382 }
michael@0 383 if((strncmp("-0", argv[i],2) == 0) && ((i+1) < argc))
michael@0 384 {
michael@0 385 int cnst0;
michael@0 386 if(sscanf(argv[i+1], "%d", &cnst0) == 1)
michael@0 387 {
michael@0 388 if(cnst0 > 0)
michael@0 389 {
michael@0 390 FORMAT0CNST = cnst0;
michael@0 391 }
michael@0 392 }
michael@0 393 else
michael@0 394 {
michael@0 395 fprintf(stderr, "argc error !!!!\n");
michael@0 396 exit(-1);
michael@0 397 }
michael@0 398 i++;
michael@0 399 }
michael@0 400 if((strncmp("-1", argv[i],2) == 0) && ((i+1) < argc))
michael@0 401 {
michael@0 402 int cnst1;
michael@0 403 if(sscanf(argv[i+1], "%d", &cnst1) == 1)
michael@0 404 {
michael@0 405 if(cnst1 > 0)
michael@0 406 {
michael@0 407 FORMAT1CNST = cnst1;
michael@0 408 }
michael@0 409 }
michael@0 410 else
michael@0 411 {
michael@0 412 fprintf(stderr, "argc error !!!!\n");
michael@0 413 exit(-1);
michael@0 414 }
michael@0 415 i++;
michael@0 416 }
michael@0 417 }
michael@0 418 if(! bInitFromOrTo)
michael@0 419 {
michael@0 420 usage();
michael@0 421 exit(-1);
michael@0 422 }
michael@0 423 fprintf(stderr, "format 0 cnst = %d\n", FORMAT0CNST);
michael@0 424 fprintf(stderr, "format 1 cnst = %d\n", FORMAT1CNST);
michael@0 425 fprintf(stderr, "generate u%c table\n",
michael@0 426 bGenerateFromUnicodeTable ? 'f' : 't');
michael@0 427 }
michael@0 428 void getinput()
michael@0 429 {
michael@0 430 char buf[256];
michael@0 431 short c,u;
michael@0 432 for (; fgets(buf,sizeof(buf),stdin);)
michael@0 433 {
michael@0 434 if(buf[0]=='0' && buf[1] == 'x')
michael@0 435 {
michael@0 436 sscanf(buf,"%hx %hx",&c,&u);
michael@0 437 if(bGenerateFromUnicodeTable)
michael@0 438 SetMapValue(u, c);
michael@0 439 else
michael@0 440 SetMapValue(c, u);
michael@0 441 }
michael@0 442 }
michael@0 443 }
michael@0 444 int main(int argc, char* argv[])
michael@0 445 {
michael@0 446 parsearg(argc, argv);
michael@0 447 initmaps();
michael@0 448 getinput();
michael@0 449 gentable();
michael@0 450 writetable();
michael@0 451 return 0;
michael@0 452 }

mercurial