intl/uconv/util/umap.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.

     1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /* #include "PRIntlpriv.h" */
     6 #include "unicpriv.h" 
     9 typedef uint16_t (* MapFormatFunc)(uint16_t in,const uTable *uT,const uMapCell *cell);
    10 typedef int (* HitFormateFunc)(uint16_t in,const uMapCell *cell);
    11 typedef void (* FillInfoFormateFunc)(const uTable *uT, const uMapCell *cell, uint32_t* info);
    14 int uHitFormate0(uint16_t in,const uMapCell *cell);
    15 int uHitFormate2(uint16_t in,const uMapCell *cell);
    16 uint16_t uMapFormate0(uint16_t in,const uTable *uT,const uMapCell *cell);
    17 uint16_t uMapFormate1(uint16_t in,const uTable *uT,const uMapCell *cell);
    18 uint16_t uMapFormate2(uint16_t in,const uTable *uT,const uMapCell *cell);
    19 void uFillInfoFormate0(const uTable *uT,const uMapCell *cell,uint32_t* aInfo);
    20 void uFillInfoFormate1(const uTable *uT,const uMapCell *cell,uint32_t* aInfo);
    21 void uFillInfoFormate2(const uTable *uT,const uMapCell *cell,uint32_t* aInfo);
    24 const uMapCell *uGetMapCell(const uTable *uT, int16_t item);
    25 char uGetFormat(const uTable *uT, int16_t item);
    28 /*=================================================================================
    30 =================================================================================*/
    31 const MapFormatFunc m_map[uNumFormatTag] =
    32 {
    33     uMapFormate0,
    34     uMapFormate1,
    35     uMapFormate2,
    36 };
    38 /*=================================================================================
    40 =================================================================================*/
    41 const FillInfoFormateFunc m_fillinfo[uNumFormatTag] =
    42 {
    43     uFillInfoFormate0,
    44     uFillInfoFormate1,
    45     uFillInfoFormate2,
    46 };
    48 /*=================================================================================
    50 =================================================================================*/
    51 const HitFormateFunc m_hit[uNumFormatTag] =
    52 {
    53     uHitFormate0,
    54     uHitFormate0,
    55     uHitFormate2,
    56 };
    58 #define uHit(format,in,cell)   (* m_hit[(format)])((in),(cell))
    59 #define uMap(format,in,uT,cell)  (* m_map[(format)])((in),(uT),(cell))
    60 #define uGetMapCell(uT, item) ((uMapCell *)(((uint16_t *)uT) + (uT)->offsetToMapCellArray + (item)*(UMAPCELL_SIZE/sizeof(uint16_t))))
    61 #define uGetFormat(uT, item) (((((uint16_t *)uT) + (uT)->offsetToFormatArray)[(item)>> 2 ] >> (((item)% 4 ) << 2)) & 0x0f)
    63 /*=================================================================================
    65 =================================================================================*/
    66 int uMapCode(const uTable *uT, uint16_t in, uint16_t* out)
    67 {
    68   int done = 0;
    69   uint16_t itemOfList = uT->itemOfList;
    70   uint16_t i;
    71   *out = NOMAPPING;
    72   for(i=0;i<itemOfList;i++)
    73   {
    74     const uMapCell* uCell;
    75     int8_t format = uGetFormat(uT,i);
    76     uCell = uGetMapCell(uT,i);
    77     if(uHit(format, in, uCell))
    78     {
    79       *out = uMap(format, in, uT,uCell);
    80       done = 1;
    81       break;
    82     }
    83   }
    84   return ( done && (*out != NOMAPPING));
    85 }
    88 /*
    89 member function
    90 */
    91 /*=================================================================================
    93 =================================================================================*/
    94 int uHitFormate0(uint16_t in,const uMapCell *cell)
    95 {
    96   return ( (in >= cell->fmt.format0.srcBegin) &&
    97     (in <= cell->fmt.format0.srcEnd) ) ;
    98 }
    99 /*=================================================================================
   101 =================================================================================*/
   102 int uHitFormate2(uint16_t in,const uMapCell *cell)
   103 {
   104   return (in == cell->fmt.format2.srcBegin);
   105 }
   106 /*=================================================================================
   108 =================================================================================*/
   109 uint16_t uMapFormate0(uint16_t in,const uTable *uT,const uMapCell *cell)
   110 {
   111   return ((in - cell->fmt.format0.srcBegin) + cell->fmt.format0.destBegin);
   112 }
   113 /*=================================================================================
   115 =================================================================================*/
   116 uint16_t uMapFormate1(uint16_t in,const uTable *uT,const uMapCell *cell)
   117 {
   118   return (*(((uint16_t *)uT) + uT->offsetToMappingTable
   119     + cell->fmt.format1.mappingOffset + in - cell->fmt.format1.srcBegin));
   120 }
   121 /*=================================================================================
   123 =================================================================================*/
   124 uint16_t uMapFormate2(uint16_t in,const uTable *uT,const uMapCell *cell)
   125 {
   126   return (cell->fmt.format2.destBegin);
   127 }
   129 #define SET_REPRESENTABLE(info, c)  (info)[(c) >> 5] |= (1L << ((c) & 0x1f))
   130 /*=================================================================================
   132 =================================================================================*/
   133 void uFillInfoFormate0(const uTable *uT,const uMapCell *cell,uint32_t* info)
   134 {
   135   uint16_t begin, end, i;
   136   begin = cell->fmt.format0.srcBegin;
   137   end = cell->fmt.format0.srcEnd;
   138   if( (begin >> 5) == (end >> 5)) /* High 17 bits are the same */
   139   {
   140     for(i = begin; i <= end; i++)
   141       SET_REPRESENTABLE(info, i);
   142   } 
   143   else {
   144     uint32_t b = begin >> 5;
   145     uint32_t e = end >> 5;
   146     info[ b ] |= (0xFFFFFFFFL << ((begin) & 0x1f)); 
   147     info[ e ] |= (0xFFFFFFFFL >> (31 - ((end) & 0x1f)));
   148     for(b++ ; b < e ; b++)
   149       info[b] |= 0xFFFFFFFFL;
   150   }
   151 }
   152 /*=================================================================================
   154 =================================================================================*/
   155 void uFillInfoFormate1(const uTable *uT,const uMapCell *cell,uint32_t* info)
   156 {
   157   uint16_t begin, end, i;
   158   uint16_t *base;
   159   begin = cell->fmt.format0.srcBegin;
   160   end = cell->fmt.format0.srcEnd;
   161   base = (((uint16_t *)uT) + uT->offsetToMappingTable + cell->fmt.format1.mappingOffset);
   162   for(i = begin; i <= end; i++) 
   163   {
   164     if(0xFFFD != base[i - begin])  /* check every item */
   165       SET_REPRESENTABLE(info, i);
   166   }
   167 }
   168 /*=================================================================================
   170 =================================================================================*/
   171 void uFillInfoFormate2(const uTable *uT,const uMapCell *cell,uint32_t* info)
   172 {
   173   SET_REPRESENTABLE(info, cell->fmt.format2.srcBegin);
   174 }

mercurial