~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/extras/FreeType/lib/extend/ftxcmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
 *
 
3
 *  ftxcmap.h                                                   1.0
 
4
 *
 
5
 *    API extension for iterating over Cmaps
 
6
 *
 
7
 *  Copyright 1996-1999 by Juliusz Chroboczek,
 
8
 *  David Turner, Robert Wilhelm, and Werner Lemberg.
 
9
 *
 
10
 *  This file is part of the FreeType project, and may only be used
 
11
 *  modified and distributed under the terms of the FreeType project
 
12
 *  license, LICENSE.TXT.  By continuing to use, modify, or distribute
 
13
 *  this file you indicate that you have read the license and
 
14
 *  understand and accept it fully.
 
15
 *
 
16
 *
 
17
 ******************************************************************/
 
18
 
 
19
#include "ftxcmap.h"
 
20
 
 
21
#include "tttypes.h"
 
22
#include "ttobjs.h"
 
23
#include "tttables.h"
 
24
 
 
25
static Long    charmap_first4  ( PCMap4, UShort* );
 
26
static Long    charmap_next4   ( PCMap4, UShort, UShort* );
 
27
static Long    charmap_last4   ( PCMap4, UShort* );
 
28
static UShort  charmap_find_id4( PCMap4, UShort, TCMap4Segment*, UShort );
 
29
 
 
30
 
 
31
/*******************************************************************
 
32
 *
 
33
 *  Function    :  TT_CharMap_First
 
34
 *
 
35
 *  Description :  Returns the first valid character code in a
 
36
 *                 given character map.  Also returns the corresponding
 
37
 *                 glyph index.
 
38
 *
 
39
 *  Input  :  charMap     handle to the target character map
 
40
 *            id          address where the glyph index will be
 
41
 *                        be returned in case of success
 
42
 *
 
43
 *  Output :  First valid character code.  -1 in case of failure.
 
44
 *
 
45
 *  Notes  :
 
46
 *
 
47
 ******************************************************************/
 
48
 
 
49
EXPORT_FUNC
 
50
TT_Long  TT_CharMap_First( TT_CharMap  charMap,
 
51
                           TT_UShort*  id )
 
52
{
 
53
  PCMapTable  cmap;
 
54
  UShort      i, c;
 
55
 
 
56
 
 
57
  if ( !( cmap = HANDLE_CharMap( charMap ) ) )
 
58
    return -1;
 
59
 
 
60
  switch ( cmap->format )
 
61
  {
 
62
  case 0:
 
63
    if ( id )
 
64
      *id = cmap->c.cmap0.glyphIdArray[0];
 
65
    return 0;
 
66
 
 
67
  case 4:
 
68
    return charmap_first4( &cmap->c.cmap4, id );
 
69
 
 
70
  case 6:
 
71
    if ( cmap->c.cmap6.entryCount < 1 )
 
72
      return -1;
 
73
 
 
74
    if ( id )
 
75
      *id = cmap->c.cmap6.glyphIdArray[0];
 
76
    return cmap->c.cmap6.firstCode;
 
77
 
 
78
  default:
 
79
    /* Now loop from 0 to 65535. We can't use a simple "for' on */
 
80
    /* 16-bits systems, hence the "strange" loop here..         */
 
81
    i = 0;
 
82
    do
 
83
    {
 
84
      c = TT_Char_Index( charMap, i );
 
85
      if ( c > 0 )
 
86
      {
 
87
        if ( id )
 
88
          *id = c;
 
89
        return i;
 
90
      }
 
91
      i++;
 
92
    } while ( i != 0 );  /* because i is UShort! */
 
93
 
 
94
    return -1;
 
95
  }
 
96
}
 
97
 
 
98
 
 
99
static Long  charmap_first4( PCMap4   cmap4,
 
100
                             UShort*  id )
 
101
{
 
102
  UShort firstCode;
 
103
 
 
104
 
 
105
  if ( cmap4->segCountX2 / 2 < 1 )
 
106
    return -1;
 
107
 
 
108
  firstCode = cmap4->segments[0].startCount;
 
109
 
 
110
  if ( id )
 
111
    *id = charmap_find_id4( cmap4, firstCode, &(cmap4->segments[0]), 0 );
 
112
 
 
113
  return firstCode;
 
114
}
 
115
 
 
116
 
 
117
/*******************************************************************
 
118
 *
 
119
 *  Function    :  TT_CharMap_Next
 
120
 *
 
121
 *  Description :  Returns the next valid character code in a given
 
122
 *                 charMap.
 
123
 *
 
124
 *  Input  : charMap    handle to the target char. map
 
125
 *           index      starting character code
 
126
 *           id         address where the glyph index of the next
 
127
 *                      character will be returned
 
128
 *
 
129
 *  Output : Next valid character code after 'index'.  -1 in case
 
130
 *           of failure.
 
131
 *
 
132
 *  Notes  :
 
133
 *
 
134
 ******************************************************************/
 
135
 
 
136
EXPORT_FUNC
 
137
TT_Long  TT_CharMap_Next( TT_CharMap  charMap,
 
138
                          TT_UShort   index,
 
139
                          TT_UShort*  id )
 
140
{
 
141
  PCMapTable  cmap;
 
142
  UShort      i, c;
 
143
 
 
144
 
 
145
  cmap = HANDLE_CharMap( charMap );
 
146
  if ( !cmap )
 
147
    return -1;
 
148
 
 
149
  switch ( cmap->format )
 
150
  {
 
151
  case 0:
 
152
    if ( index < 255 )
 
153
    {
 
154
      if ( id )
 
155
        *id = cmap->c.cmap0.glyphIdArray[index + 1];
 
156
      return index + 1;
 
157
    }
 
158
    else
 
159
      return -1;
 
160
 
 
161
  case 4:
 
162
    return charmap_next4( &cmap->c.cmap4, index, id );
 
163
 
 
164
  case 6:
 
165
    {
 
166
      UShort  firstCode = cmap->c.cmap6.firstCode;
 
167
 
 
168
 
 
169
      if ( index + 1 < firstCode + cmap->c.cmap6.entryCount )
 
170
      {
 
171
        if ( id )
 
172
          *id = cmap->c.cmap6.glyphIdArray[index + 1 - firstCode];
 
173
        return index + 1;
 
174
      }
 
175
      else
 
176
        return -1;
 
177
    }
 
178
 
 
179
  default:
 
180
    /* Now loop from 0 to 65535. We can't use a simple "for" on */
 
181
    /* 16-bits systems, hence the "strange" loop here..         */
 
182
    i = 0;
 
183
    do
 
184
    {
 
185
      c = TT_Char_Index( charMap, i );
 
186
      if ( c > 0 )
 
187
      {
 
188
        if ( id )
 
189
          *id = c;
 
190
        return i;
 
191
      }
 
192
      i++;
 
193
    } while ( i != 0 );  /* because i is UShort! */
 
194
 
 
195
    return -1;
 
196
  }
 
197
}
 
198
 
 
199
 
 
200
static Long  charmap_next4( PCMap4   cmap4,
 
201
                            UShort   charCode,
 
202
                            UShort*  id)
 
203
{
 
204
  UShort         segCount, nextCode;
 
205
  UShort         i;
 
206
  TCMap4Segment  seg4;
 
207
 
 
208
 
 
209
  if ( charCode == 0xFFFF )
 
210
    return -1;                /* get it out of the way now */
 
211
 
 
212
  segCount = cmap4->segCountX2 / 2;
 
213
 
 
214
  for ( i = 0; i < segCount; i++ )
 
215
    if ( charCode < cmap4->segments[i].endCount )
 
216
      break;
 
217
 
 
218
  /* Safety check - even though the last endCount should be 0xFFFF */
 
219
  if ( i >= segCount )
 
220
    return -1;
 
221
 
 
222
  seg4 = cmap4->segments[i];
 
223
 
 
224
  if ( charCode < seg4.startCount )
 
225
    nextCode = seg4.startCount;
 
226
  else
 
227
    nextCode = charCode + 1;
 
228
 
 
229
  if ( id )
 
230
    *id = charmap_find_id4( cmap4, nextCode, &seg4, i );
 
231
 
 
232
  return nextCode;
 
233
}
 
234
 
 
235
 
 
236
static UShort
 
237
charmap_find_id4( PCMap4          cmap4,
 
238
                  UShort          charCode,
 
239
                  TCMap4Segment*  seg4,
 
240
                  UShort          i )
 
241
{
 
242
  UShort  index1;
 
243
 
 
244
 
 
245
  if ( seg4->idRangeOffset == 0 )
 
246
    return (charCode + seg4->idDelta) & 0xFFFF;
 
247
  else
 
248
  {
 
249
    index1 = seg4->idRangeOffset / 2 + charCode-seg4->startCount -
 
250
             ( cmap4->segCountX2 / 2 - i );
 
251
 
 
252
    if ( index1 >= cmap4->numGlyphId || cmap4->glyphIdArray[index1] == 0 )
 
253
      return 0;
 
254
    else
 
255
      return (cmap4->glyphIdArray[index1] + seg4->idDelta) & 0xFFFF;
 
256
  }
 
257
}
 
258
 
 
259
 
 
260
/*******************************************************************
 
261
 *
 
262
 *  Function    :  TT_CharMap_Last
 
263
 *
 
264
 *  Description :  Returns the last valid character code in a
 
265
 *                 given character map.  Also returns the corresponding
 
266
 *                 glyph index.
 
267
 *
 
268
 *  Input  :  charMap     handle to the target character map
 
269
 *            id          address where the glyph index will be
 
270
 *                        be returned in case of success
 
271
 *
 
272
 *  Output :  Last valid character code.  -1 in case of failure.
 
273
 *
 
274
 *  Notes  :
 
275
 *
 
276
 ******************************************************************/
 
277
 
 
278
EXPORT_FUNC
 
279
TT_Long  TT_CharMap_Last( TT_CharMap  charMap,
 
280
                          TT_UShort*  id )
 
281
{
 
282
  PCMapTable  cmap;
 
283
  UShort      i, c;
 
284
 
 
285
 
 
286
  if ( !( cmap = HANDLE_CharMap( charMap ) ) )
 
287
    return -1;
 
288
 
 
289
  switch ( cmap->format )
 
290
  {
 
291
  case 0:
 
292
    if ( id )
 
293
      *id = cmap->c.cmap0.glyphIdArray[255];
 
294
    return 255;
 
295
 
 
296
  case 4:
 
297
    return charmap_last4( &cmap->c.cmap4, id );
 
298
 
 
299
  case 6:
 
300
    if ( cmap->c.cmap6.entryCount < 1 )
 
301
      return -1;
 
302
 
 
303
    if ( id )
 
304
      *id = cmap->c.cmap6.glyphIdArray[cmap->c.cmap6.entryCount - 1];
 
305
    return cmap->c.cmap6.firstCode + cmap->c.cmap6.entryCount - 1;
 
306
 
 
307
  default:
 
308
    i = 65535;
 
309
    do
 
310
    {
 
311
      c = TT_Char_Index( charMap, i );
 
312
      if ( c > 0 )
 
313
      {
 
314
        if ( id )
 
315
          *id = c;
 
316
        return i;
 
317
      }
 
318
      i--;
 
319
    } while ( i != 0 );
 
320
 
 
321
    return -1;
 
322
  }
 
323
}
 
324
 
 
325
 
 
326
static Long  charmap_last4( PCMap4   cmap4,
 
327
                            UShort*  id )
 
328
{
 
329
  UShort lastCode;
 
330
 
 
331
 
 
332
  if ( cmap4->segCountX2 / 2 < 1 )
 
333
    return -1;
 
334
 
 
335
  lastCode = cmap4->segments[cmap4->segCountX2 / 2 - 1].endCount;
 
336
 
 
337
  if ( id )
 
338
    *id = charmap_find_id4( cmap4,
 
339
                            lastCode,
 
340
                            &(cmap4->segments[cmap4->segCountX2 / 2 - 1]),
 
341
                            0 );
 
342
 
 
343
  return lastCode;
 
344
}
 
345
 
 
346
 
 
347
/* END */