Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/graphite/src/Silf.cpp
Line
Count
Source
1
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2
// Copyright 2010, SIL International, All rights reserved.
3
4
#include <cstdlib>
5
#include "graphite2/Segment.h"
6
#include "inc/debug.h"
7
#include "inc/Endian.h"
8
#include "inc/Silf.h"
9
#include "inc/Segment.h"
10
#include "inc/Rule.h"
11
#include "inc/Error.h"
12
13
14
using namespace graphite2;
15
16
namespace { static const uint32 ERROROFFSET = 0xFFFFFFFF; }
17
18
Silf::Silf() throw()
19
0
: m_passes(0),
20
0
  m_pseudos(0),
21
0
  m_classOffsets(0),
22
0
  m_classData(0),
23
0
  m_justs(0),
24
0
  m_numPasses(0),
25
0
  m_numJusts(0),
26
0
  m_sPass(0),
27
0
  m_pPass(0),
28
0
  m_jPass(0),
29
0
  m_bPass(0),
30
0
  m_flags(0),
31
0
  m_dir(0),
32
0
  m_aPseudo(0),
33
0
  m_aBreak(0),
34
0
  m_aUser(0),
35
0
  m_aBidi(0),
36
0
  m_aMirror(0),
37
0
  m_aPassBits(0),
38
0
  m_iMaxComp(0),
39
0
  m_aCollision(0),
40
0
  m_aLig(0),
41
0
  m_numPseudo(0),
42
0
  m_nClass(0),
43
0
  m_nLinear(0),
44
0
  m_gEndLine(0)
45
0
{
46
0
    memset(&m_silfinfo, 0, sizeof m_silfinfo);
47
0
}
48
49
Silf::~Silf() throw()
50
0
{
51
0
    releaseBuffers();
52
0
}
53
54
void Silf::releaseBuffers() throw()
55
0
{
56
0
    delete [] m_passes;
57
0
    delete [] m_pseudos;
58
0
    free(m_classOffsets);
59
0
    free(m_classData);
60
0
    free(m_justs);
61
0
    m_passes= 0;
62
0
    m_pseudos = 0;
63
0
    m_classOffsets = 0;
64
0
    m_classData = 0;
65
0
    m_justs = 0;
66
0
}
67
68
69
bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, Face& face, uint32 version)
70
0
{
71
0
    const byte * p = silf_start,
72
0
               * const silf_end = p + lSilf;
73
0
    Error e;
74
75
0
    if (e.test(version >= 0x00060000, E_BADSILFVERSION))
76
0
    {
77
0
        releaseBuffers(); return face.error(e);
78
0
    }
79
0
    if (version >= 0x00030000)
80
0
    {
81
0
        if (e.test(lSilf < 28, E_BADSIZE)) { releaseBuffers(); return face.error(e); }
82
0
        be::skip<int32>(p);    // ruleVersion
83
0
        be::skip<uint16>(p,2); // passOffset & pseudosOffset
84
0
    }
85
0
    else if (e.test(lSilf < 20, E_BADSIZE)) { releaseBuffers(); return face.error(e); }
86
0
    const uint16 maxGlyph = be::read<uint16>(p);
87
0
    m_silfinfo.extra_ascent = be::read<uint16>(p);
88
0
    m_silfinfo.extra_descent = be::read<uint16>(p);
89
0
    m_numPasses = be::read<uint8>(p);
90
0
    m_sPass     = be::read<uint8>(p);
91
0
    m_pPass     = be::read<uint8>(p);
92
0
    m_jPass     = be::read<uint8>(p);
93
0
    m_bPass     = be::read<uint8>(p);
94
0
    m_flags     = be::read<uint8>(p);
95
0
    be::skip<uint8>(p,2); //  max{Pre,Post}Context.
96
0
    m_aPseudo   = be::read<uint8>(p);
97
0
    m_aBreak    = be::read<uint8>(p);
98
0
    m_aBidi     = be::read<uint8>(p);
99
0
    m_aMirror   = be::read<uint8>(p);
100
0
    m_aPassBits = be::read<uint8>(p);
101
102
    // Read Justification levels.
103
0
    m_numJusts  = be::read<uint8>(p);
104
0
    if (e.test(maxGlyph >= face.glyphs().numGlyphs(), E_BADMAXGLYPH)
105
0
        || e.test(p + m_numJusts * 8 >= silf_end, E_BADNUMJUSTS))
106
0
    {
107
0
        releaseBuffers(); return face.error(e);
108
0
    }
109
110
0
    if (m_numJusts)
111
0
    {
112
0
        m_justs = gralloc<Justinfo>(m_numJusts);
113
0
        if (e.test(!m_justs, E_OUTOFMEM)) return face.error(e);
114
115
0
        for (uint8 i = 0; i < m_numJusts; i++)
116
0
        {
117
0
            ::new(m_justs + i) Justinfo(p[0], p[1], p[2], p[3]);
118
0
            be::skip<byte>(p,8);
119
0
        }
120
0
    }
121
122
0
    if (e.test(p + sizeof(uint16) + sizeof(uint8)*8 >= silf_end, E_BADENDJUSTS)) { releaseBuffers(); return face.error(e); }
123
0
    m_aLig       = be::read<uint16>(p);
124
0
    m_aUser      = be::read<uint8>(p);
125
0
    m_iMaxComp   = be::read<uint8>(p);
126
0
    m_dir        = be::read<uint8>(p) - 1;
127
0
    m_aCollision = be::read<uint8>(p);
128
0
    be::skip<byte>(p,3);
129
0
    be::skip<uint16>(p, be::read<uint8>(p));    // don't need critical features yet
130
0
    be::skip<byte>(p);                          // reserved
131
0
    if (e.test(p >= silf_end, E_BADCRITFEATURES))   { releaseBuffers(); return face.error(e); }
132
0
    be::skip<uint32>(p, be::read<uint8>(p));    // don't use scriptTag array.
133
0
    if (e.test(p + sizeof(uint16) + sizeof(uint32) >= silf_end, E_BADSCRIPTTAGS)) { releaseBuffers(); return face.error(e); }
134
0
    m_gEndLine  = be::read<uint16>(p);          // lbGID
135
0
    const byte * o_passes = p;
136
0
    uint32 passes_start = be::read<uint32>(p);
137
138
0
    const size_t num_attrs = face.glyphs().numAttrs();
139
0
    if (e.test(m_aPseudo   >= num_attrs, E_BADAPSEUDO)
140
0
        || e.test(m_aBreak >= num_attrs, E_BADABREAK)
141
0
        || e.test(m_aBidi  >= num_attrs, E_BADABIDI)
142
0
        || e.test(m_aMirror>= num_attrs, E_BADAMIRROR)
143
0
        || e.test(m_aCollision && m_aCollision >= num_attrs - 5, E_BADACOLLISION)
144
0
        || e.test(m_numPasses > 128, E_BADNUMPASSES) || e.test(passes_start >= lSilf, E_BADPASSESSTART)
145
0
        || e.test(m_pPass < m_sPass, E_BADPASSBOUND) || e.test(m_pPass > m_numPasses, E_BADPPASS) || e.test(m_sPass > m_numPasses, E_BADSPASS)
146
0
        || e.test(m_jPass < m_pPass, E_BADJPASSBOUND) || e.test(m_jPass > m_numPasses, E_BADJPASS)
147
0
        || e.test((m_bPass != 0xFF && (m_bPass < m_jPass || m_bPass > m_numPasses)), E_BADBPASS)
148
0
        || e.test(m_aLig > 127, E_BADALIG))
149
0
    {
150
0
        releaseBuffers();
151
0
        return face.error(e);
152
0
    }
153
0
    be::skip<uint32>(p, m_numPasses);
154
0
    if (e.test(unsigned(p - silf_start) + sizeof(uint16) >= passes_start, E_BADPASSESSTART)) { releaseBuffers(); return face.error(e); }
155
0
    m_numPseudo = be::read<uint16>(p);
156
0
    be::skip<uint16>(p, 3); // searchPseudo, pseudoSelector, pseudoShift
157
0
    m_pseudos = new Pseudo[m_numPseudo];
158
0
    if (e.test(unsigned(p - silf_start) + m_numPseudo*(sizeof(uint32) + sizeof(uint16)) >= passes_start, E_BADNUMPSEUDO)
159
0
        || e.test(!m_pseudos, E_OUTOFMEM))
160
0
    {
161
0
        releaseBuffers(); return face.error(e);
162
0
    }
163
0
    for (int i = 0; i < m_numPseudo; i++)
164
0
    {
165
0
        m_pseudos[i].uid = be::read<uint32>(p);
166
0
        m_pseudos[i].gid = be::read<uint16>(p);
167
0
    }
168
169
0
    const size_t clen = readClassMap(p, passes_start + silf_start - p, version, e);
170
0
    m_passes = new Pass[m_numPasses];
171
0
    if (e || e.test(clen > unsigned(passes_start + silf_start - p), E_BADPASSESSTART)
172
0
          || e.test(!m_passes, E_OUTOFMEM))
173
0
    { releaseBuffers(); return face.error(e); }
174
175
0
    for (size_t i = 0; i < m_numPasses; ++i)
176
0
    {
177
0
        uint32 pass_start = be::read<uint32>(o_passes);
178
0
        uint32 pass_end = be::peek<uint32>(o_passes);
179
0
        face.error_context((face.error_context() & 0xFF00) + EC_ASILF + unsigned(i << 16));
180
0
        if (e.test(pass_start > pass_end, E_BADPASSSTART)
181
0
                || e.test(pass_start < passes_start, E_BADPASSSTART)
182
0
                || e.test(pass_end > lSilf, E_BADPASSEND)) {
183
0
            releaseBuffers(); return face.error(e);
184
0
        }
185
186
0
        enum passtype pt = PASS_TYPE_UNKNOWN;
187
0
        if (i >= m_jPass) pt = PASS_TYPE_JUSTIFICATION;
188
0
        else if (i >= m_pPass) pt = PASS_TYPE_POSITIONING;
189
0
        else if (i >= m_sPass) pt = PASS_TYPE_SUBSTITUTE;
190
0
        else pt = PASS_TYPE_LINEBREAK;
191
192
0
        m_passes[i].init(this);
193
0
        if (!m_passes[i].readPass(silf_start + pass_start, pass_end - pass_start, pass_start, face, pt,
194
0
            version, e))
195
0
        {
196
0
            releaseBuffers();
197
0
            return false;
198
0
        }
199
0
    }
200
201
    // fill in gr_faceinfo
202
0
    m_silfinfo.upem = face.glyphs().unitsPerEm();
203
0
    m_silfinfo.has_bidi_pass = (m_bPass != 0xFF);
204
0
    m_silfinfo.justifies = (m_numJusts != 0) || (m_jPass < m_pPass);
205
0
    m_silfinfo.line_ends = (m_flags & 1);
206
0
    m_silfinfo.space_contextuals = gr_faceinfo::gr_space_contextuals((m_flags >> 2) & 0x7);
207
0
    return true;
208
0
}
209
210
template<typename T> inline uint32 Silf::readClassOffsets(const byte *&p, size_t data_len, Error &e)
211
0
{
212
0
    const T cls_off = 2*sizeof(uint16) + sizeof(T)*(m_nClass+1);
213
0
    const uint32 max_off = (be::peek<T>(p + sizeof(T)*m_nClass) - cls_off)/sizeof(uint16);
214
    // Check that the last+1 offset is less than or equal to the class map length.
215
0
    if (e.test(be::peek<T>(p) != cls_off, E_MISALIGNEDCLASSES)
216
0
            || e.test(max_off > (data_len - cls_off)/sizeof(uint16), E_HIGHCLASSOFFSET))
217
0
        return ERROROFFSET;
218
219
    // Read in all the offsets.
220
0
    m_classOffsets = gralloc<uint32>(m_nClass+1);
221
0
    if (e.test(!m_classOffsets, E_OUTOFMEM)) return ERROROFFSET;
222
0
    for (uint32 * o = m_classOffsets, * const o_end = o + m_nClass + 1; o != o_end; ++o)
223
0
    {
224
0
        *o = (be::read<T>(p) - cls_off)/sizeof(uint16);
225
0
        if (e.test(*o > max_off, E_HIGHCLASSOFFSET))
226
0
            return ERROROFFSET;
227
0
    }
228
0
    return max_off;
229
0
}
Unexecuted instantiation: unsigned int graphite2::Silf::readClassOffsets<unsigned int>(unsigned char const*&, unsigned long, graphite2::Error&)
Unexecuted instantiation: unsigned int graphite2::Silf::readClassOffsets<unsigned short>(unsigned char const*&, unsigned long, graphite2::Error&)
230
231
size_t Silf::readClassMap(const byte *p, size_t data_len, uint32 version, Error &e)
232
0
{
233
0
    if (e.test(data_len < sizeof(uint16)*2, E_BADCLASSSIZE)) return ERROROFFSET;
234
235
0
    m_nClass  = be::read<uint16>(p);
236
0
    m_nLinear = be::read<uint16>(p);
237
238
    // Check that numLinear < numClass,
239
    // that there is at least enough data for numClasses offsets.
240
0
    if (e.test(m_nLinear > m_nClass, E_TOOMANYLINEAR)
241
0
     || e.test((m_nClass + 1) * (version >= 0x00040000 ? sizeof(uint32) : sizeof(uint16)) > (data_len - 4), E_CLASSESTOOBIG))
242
0
        return ERROROFFSET;
243
244
0
    uint32 max_off;
245
0
    if (version >= 0x00040000)
246
0
        max_off = readClassOffsets<uint32>(p, data_len, e);
247
0
    else
248
0
        max_off = readClassOffsets<uint16>(p, data_len, e);
249
250
0
    if (max_off == ERROROFFSET) return ERROROFFSET;
251
252
0
    if (e.test((int)max_off < m_nLinear + (m_nClass - m_nLinear) * 6, E_CLASSESTOOBIG))
253
0
        return ERROROFFSET;
254
255
    // Check the linear offsets are sane, these must be monotonically increasing.
256
0
    assert(m_nClass >= m_nLinear);
257
0
    for (const uint32 *o = m_classOffsets, * const o_end = o + m_nLinear; o != o_end; ++o)
258
0
        if (e.test(o[0] > o[1], E_BADCLASSOFFSET))
259
0
            return ERROROFFSET;
260
261
    // Fortunately the class data is all uint16s so we can decode these now
262
0
    m_classData = gralloc<uint16>(max_off);
263
0
    if (e.test(!m_classData, E_OUTOFMEM)) return ERROROFFSET;
264
0
    for (uint16 *d = m_classData, * const d_end = d + max_off; d != d_end; ++d)
265
0
        *d = be::read<uint16>(p);
266
267
    // Check the lookup class invariants for each non-linear class
268
0
    for (const uint32 *o = m_classOffsets + m_nLinear, * const o_end = m_classOffsets + m_nClass; o != o_end; ++o)
269
0
    {
270
0
        const uint16 * lookup = m_classData + *o;
271
0
        if (e.test(*o + 4 > max_off, E_HIGHCLASSOFFSET)                        // LookupClass doesn't stretch over max_off
272
0
         || e.test(lookup[0] == 0                                                   // A LookupClass with no looks is a suspicious thing ...
273
0
                    || lookup[0] * 2 + *o + 4 > max_off                             // numIDs lookup pairs fits within (start of LookupClass' lookups array, max_off]
274
0
                    || lookup[3] + lookup[1] != lookup[0], E_BADCLASSLOOKUPINFO)    // rangeShift:   numIDs  - searchRange
275
0
         || e.test(((o[1] - *o) & 1) != 0, ERROROFFSET))                         // glyphs are in pairs so difference must be even.
276
0
            return ERROROFFSET;
277
0
    }
278
279
0
    return max_off;
280
0
}
281
282
uint16 Silf::findPseudo(uint32 uid) const
283
0
{
284
0
    for (int i = 0; i < m_numPseudo; i++)
285
0
        if (m_pseudos[i].uid == uid) return m_pseudos[i].gid;
286
0
    return 0;
287
0
}
288
289
uint16 Silf::findClassIndex(uint16 cid, uint16 gid) const
290
0
{
291
0
    if (cid > m_nClass) return -1;
292
293
0
    const uint16 * cls = m_classData + m_classOffsets[cid];
294
0
    if (cid < m_nLinear)        // output class being used for input, shouldn't happen
295
0
    {
296
0
        for (unsigned int i = 0, n = m_classOffsets[cid + 1] - m_classOffsets[cid]; i < n; ++i, ++cls)
297
0
            if (*cls == gid) return i;
298
0
        return -1;
299
0
    }
300
0
    else
301
0
    {
302
0
        const uint16 *  min = cls + 4,      // lookups array
303
0
                     *  max = min + cls[0]*2; // lookups aray is numIDs (cls[0]) uint16 pairs long
304
0
        do
305
0
        {
306
0
            const uint16 * p = min + (-2 & ((max-min)/2));
307
0
            if  (p[0] > gid)    max = p;
308
0
            else                min = p;
309
0
        }
310
0
        while (max - min > 2);
311
0
        return min[0] == gid ? min[1] : -1;
312
0
    }
313
0
}
314
315
uint16 Silf::getClassGlyph(uint16 cid, unsigned int index) const
316
0
{
317
0
    if (cid > m_nClass) return 0;
318
319
0
    uint32 loc = m_classOffsets[cid];
320
0
    if (cid < m_nLinear)
321
0
    {
322
0
        if (index < m_classOffsets[cid + 1] - loc)
323
0
            return m_classData[index + loc];
324
0
    }
325
0
    else        // input class being used for output. Shouldn't happen
326
0
    {
327
0
        for (unsigned int i = loc + 4; i < m_classOffsets[cid + 1]; i += 2)
328
0
            if (m_classData[i + 1] == index) return m_classData[i];
329
0
    }
330
0
    return 0;
331
0
}
332
333
334
bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass, int dobidi) const
335
0
{
336
0
    assert(seg != 0);
337
0
    size_t             maxSize = seg->slotCount() * MAX_SEG_GROWTH_FACTOR;
338
0
    SlotMap            map(*seg, m_dir, maxSize);
339
0
    FiniteStateMachine fsm(map, seg->getFace()->logger());
340
0
    vm::Machine        m(map);
341
0
    uint8              lbidi = m_bPass;
342
#if !defined GRAPHITE2_NTRACING
343
    json * const dbgout = seg->getFace()->logger();
344
#endif
345
346
0
    if (lastPass == 0)
347
0
    {
348
0
        if (firstPass == lastPass && lbidi == 0xFF)
349
0
            return true;
350
0
        lastPass = m_numPasses;
351
0
    }
352
0
    if ((firstPass < lbidi || (dobidi && firstPass == lbidi)) && (lastPass >= lbidi || (dobidi && lastPass + 1 == lbidi)))
353
0
        lastPass++;
354
0
    else
355
0
        lbidi = 0xFF;
356
357
0
    for (size_t i = firstPass; i < lastPass; ++i)
358
0
    {
359
        // bidi and mirroring
360
0
        if (i == lbidi)
361
0
        {
362
#if !defined GRAPHITE2_NTRACING
363
            if (dbgout)
364
            {
365
                *dbgout << json::item << json::object
366
//              << "pindex" << i   // for debugging
367
                            << "id"     << -1
368
                            << "slotsdir" << (seg->currdir() ? "rtl" : "ltr")
369
                            << "passdir" << (m_dir & 1 ? "rtl" : "ltr")
370
                            << "slots"  << json::array;
371
                seg->positionSlots(0, 0, 0, seg->currdir());
372
                for(Slot * s = seg->first(); s; s = s->next())
373
                    *dbgout     << dslot(seg, s);
374
                *dbgout         << json::close
375
                            << "rules"  << json::array << json::close
376
                            << json::close;
377
            }
378
#endif
379
0
            if (seg->currdir() != (m_dir & 1))
380
0
                seg->reverseSlots();
381
0
            if (m_aMirror && (seg->dir() & 3) == 3)
382
0
                seg->doMirror(m_aMirror);
383
0
        --i;
384
0
        lbidi = lastPass;
385
0
        --lastPass;
386
0
        continue;
387
0
        }
388
389
#if !defined GRAPHITE2_NTRACING
390
        if (dbgout)
391
        {
392
            *dbgout << json::item << json::object
393
//            << "pindex" << i   // for debugging
394
                        << "id"     << i+1
395
                        << "slotsdir" << (seg->currdir() ? "rtl" : "ltr")
396
                        << "passdir" << ((m_dir & 1) ^ m_passes[i].reverseDir() ? "rtl" : "ltr")
397
                        << "slots"  << json::array;
398
            seg->positionSlots(0, 0, 0, seg->currdir());
399
            for(Slot * s = seg->first(); s; s = s->next())
400
                *dbgout     << dslot(seg, s);
401
            *dbgout         << json::close;
402
        }
403
#endif
404
405
        // test whether to reorder, prepare for positioning
406
0
        bool reverse = (lbidi == 0xFF) && (seg->currdir() != ((m_dir & 1) ^ m_passes[i].reverseDir()));
407
0
        if ((i >= 32 || (seg->passBits() & (1 << i)) == 0 || m_passes[i].collisionLoops())
408
0
                && !m_passes[i].runGraphite(m, fsm, reverse))
409
0
            return false;
410
        // only subsitution passes can change segment length, cached subsegments are short for their text
411
0
        if (m.status() != vm::Machine::finished
412
0
            || (seg->slotCount() && seg->slotCount() > maxSize))
413
0
            return false;
414
0
    }
415
0
    return true;
416
0
}