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/FileFace.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 2012, SIL International, All rights reserved.
3
4
#include <cstring>
5
#include "inc/FileFace.h"
6
7
8
#ifndef GRAPHITE2_NFILEFACE
9
10
using namespace graphite2;
11
12
FileFace::FileFace(const char *filename)
13
0
: _file(fopen(filename, "rb")),
14
0
  _file_len(0),
15
0
  _header_tbl(NULL),
16
0
  _table_dir(NULL)
17
0
{
18
0
    if (!_file) return;
19
20
0
    if (fseek(_file, 0, SEEK_END)) return;
21
0
    _file_len = ftell(_file);
22
0
    if (fseek(_file, 0, SEEK_SET)) return;
23
24
0
    size_t tbl_offset, tbl_len;
25
26
    // Get the header.
27
0
    if (!TtfUtil::GetHeaderInfo(tbl_offset, tbl_len)) return;
28
0
    if (fseek(_file, long(tbl_offset), SEEK_SET)) return;
29
0
    _header_tbl = (TtfUtil::Sfnt::OffsetSubTable*)gralloc<char>(tbl_len);
30
0
    if (_header_tbl)
31
0
    {
32
0
        if (fread(_header_tbl, 1, tbl_len, _file) != tbl_len) return;
33
0
        if (!TtfUtil::CheckHeader(_header_tbl)) return;
34
0
    }
35
36
    // Get the table directory
37
0
    if (!TtfUtil::GetTableDirInfo(_header_tbl, tbl_offset, tbl_len)) return;
38
0
    _table_dir = (TtfUtil::Sfnt::OffsetSubTable::Entry*)gralloc<char>(tbl_len);
39
0
    if (fseek(_file, long(tbl_offset), SEEK_SET)) return;
40
0
    if (_table_dir && fread(_table_dir, 1, tbl_len, _file) != tbl_len)
41
0
    {
42
0
        free(_table_dir);
43
0
        _table_dir = NULL;
44
0
    }
45
0
    return;
46
0
}
47
48
FileFace::~FileFace()
49
0
{
50
0
    free(_table_dir);
51
0
    free(_header_tbl);
52
0
    if (_file)
53
0
        fclose(_file);
54
0
}
55
56
57
const void *FileFace::get_table_fn(const void* appFaceHandle, unsigned int name, size_t *len)
58
0
{
59
0
    if (appFaceHandle == 0)     return 0;
60
0
    const FileFace & file_face = *static_cast<const FileFace *>(appFaceHandle);
61
62
0
    void *tbl;
63
0
    size_t tbl_offset, tbl_len;
64
0
    if (!TtfUtil::GetTableInfo(name, file_face._header_tbl, file_face._table_dir, tbl_offset, tbl_len))
65
0
        return 0;
66
67
0
    if (tbl_offset > file_face._file_len || tbl_len > file_face._file_len - tbl_offset
68
0
            || fseek(file_face._file, long(tbl_offset), SEEK_SET) != 0)
69
0
        return 0;
70
71
0
    tbl = malloc(tbl_len);
72
0
    if (!tbl || fread(tbl, 1, tbl_len, file_face._file) != tbl_len)
73
0
    {
74
0
        free(tbl);
75
0
        return 0;
76
0
    }
77
78
0
    if (len) *len = tbl_len;
79
0
    return tbl;
80
0
}
81
82
void FileFace::rel_table_fn(const void* appFaceHandle, const void *table_buffer)
83
0
{
84
0
    if (appFaceHandle == 0)     return;
85
86
0
    free(const_cast<void *>(table_buffer));
87
0
}
88
89
const gr_face_ops FileFace::ops = { sizeof FileFace::ops, &FileFace::get_table_fn, &FileFace::rel_table_fn };
90
91
92
#endif                  //!GRAPHITE2_NFILEFACE