Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/graphite/src/FileFace.cpp
Line
Count
Source (jump to first uncovered line)
1
/*  GRAPHITE2 LICENSING
2
3
    Copyright 2012, SIL International
4
    All rights reserved.
5
6
    This library is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU Lesser General Public License as published
8
    by the Free Software Foundation; either version 2.1 of License, or
9
    (at your option) any later version.
10
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    Lesser General Public License for more details.
15
16
    You should also have received a copy of the GNU Lesser General Public
17
    License along with this library in the file named "LICENSE".
18
    If not, write to the Free Software Foundation, 51 Franklin Street,
19
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20
    internet at http://www.fsf.org/licenses/lgpl.html.
21
22
Alternatively, the contents of this file may be used under the terms of the
23
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24
License, as published by the Free Software Foundation, either version 2
25
of the License or (at your option) any later version.
26
*/
27
#include <cstring>
28
#include "inc/FileFace.h"
29
30
31
#ifndef GRAPHITE2_NFILEFACE
32
33
using namespace graphite2;
34
35
FileFace::FileFace(const char *filename)
36
0
: _file(fopen(filename, "rb")),
37
0
  _file_len(0),
38
  _header_tbl(NULL),
39
  _table_dir(NULL)
40
0
{
41
0
    if (!_file) return;
42
43
0
    if (fseek(_file, 0, SEEK_END)) return;
44
0
    _file_len = ftell(_file);
45
0
    if (fseek(_file, 0, SEEK_SET)) return;
46
47
0
    size_t tbl_offset, tbl_len;
48
49
    // Get the header.
50
0
    if (!TtfUtil::GetHeaderInfo(tbl_offset, tbl_len)) return;
51
0
    if (fseek(_file, long(tbl_offset), SEEK_SET)) return;
52
0
    _header_tbl = (TtfUtil::Sfnt::OffsetSubTable*)gralloc<char>(tbl_len);
53
0
    if (_header_tbl)
54
0
    {
55
0
        if (fread(_header_tbl, 1, tbl_len, _file) != tbl_len) return;
56
0
        if (!TtfUtil::CheckHeader(_header_tbl)) return;
57
0
    }
58
59
    // Get the table directory
60
0
    if (!TtfUtil::GetTableDirInfo(_header_tbl, tbl_offset, tbl_len)) return;
61
0
    _table_dir = (TtfUtil::Sfnt::OffsetSubTable::Entry*)gralloc<char>(tbl_len);
62
0
    if (fseek(_file, long(tbl_offset), SEEK_SET)) return;
63
0
    if (_table_dir && fread(_table_dir, 1, tbl_len, _file) != tbl_len)
64
0
    {
65
0
        free(_table_dir);
66
0
        _table_dir = NULL;
67
0
    }
68
0
    return;
69
0
}
70
71
FileFace::~FileFace()
72
0
{
73
0
    free(_table_dir);
74
0
    free(_header_tbl);
75
0
    if (_file)
76
0
        fclose(_file);
77
0
}
78
79
80
const void *FileFace::get_table_fn(const void* appFaceHandle, unsigned int name, size_t *len)
81
0
{
82
0
    if (appFaceHandle == 0)     return 0;
83
0
    const FileFace & file_face = *static_cast<const FileFace *>(appFaceHandle);
84
85
0
    void *tbl;
86
0
    size_t tbl_offset, tbl_len;
87
0
    if (!TtfUtil::GetTableInfo(name, file_face._header_tbl, file_face._table_dir, tbl_offset, tbl_len))
88
0
        return 0;
89
90
0
    if (tbl_offset > file_face._file_len || tbl_len > file_face._file_len - tbl_offset
91
0
            || fseek(file_face._file, long(tbl_offset), SEEK_SET) != 0)
92
0
        return 0;
93
94
0
    tbl = malloc(tbl_len);
95
0
    if (!tbl || fread(tbl, 1, tbl_len, file_face._file) != tbl_len)
96
0
    {
97
0
        free(tbl);
98
0
        return 0;
99
0
    }
100
101
0
    if (len) *len = tbl_len;
102
0
    return tbl;
103
0
}
104
105
void FileFace::rel_table_fn(const void* appFaceHandle, const void *table_buffer)
106
0
{
107
0
    if (appFaceHandle == 0)     return;
108
109
0
    free(const_cast<void *>(table_buffer));
110
0
}
111
112
const gr_face_ops FileFace::ops = { sizeof FileFace::ops, &FileFace::get_table_fn, &FileFace::rel_table_fn };
113
114
115
#endif                  //!GRAPHITE2_NFILEFACE