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