• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

saitoha / libsixel / 20609368106

31 Dec 2025 12:57AM UTC coverage: 52.011% (-6.3%) from 58.281%
20609368106

push

github

saitoha
tests: split converter option tap suites

14741 of 45141 branches covered (32.66%)

21394 of 41134 relevant lines covered (52.01%)

3932390.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.83
/src/loader-common.c
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright (c) 2021-2025 libsixel developers. See `AUTHORS`.
5
 * Copyright (c) 2014-2019 Hayaki Saito
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
 * DEALINGS IN THE SOFTWARE.
24
 *
25
 * Shared loader helpers used across backend implementations.  This module
26
 * centralizes trace logging, thumbnail size hints, and small detection
27
 * helpers so backend files stay narrow and platform headers remain isolated.
28
 */
29

30
#if defined(HAVE_CONFIG_H)
31
#include "config.h"
32
#endif
33

34
#include <stdio.h>
35
#include <stdlib.h>
36

37
#if HAVE_STRING_H
38
# include <string.h>
39
#endif
40
#if HAVE_STDARG_H
41
# include <stdarg.h>
42
#endif
43
#if HAVE_LIMITS_H
44
# include <limits.h>
45
#endif
46
#if HAVE_ERRNO_H
47
# include <errno.h>
48
#endif
49

50
#include <sixel.h>
51

52
#include "compat_stub.h"
53
#include "loader-common.h"
54
#include "logger.h"
55

56
static int loader_trace_enabled;
57
static int thumbnailer_default_size_hint = SIXEL_THUMBNAILER_DEFAULT_SIZE;
58
static int thumbnailer_size_hint = SIXEL_THUMBNAILER_DEFAULT_SIZE;
59
static int thumbnailer_size_hint_initialized;
60

61
void
62
loader_thumbnailer_initialize_size_hint(void)
684✔
63
{
64
    char const *env_value;
684✔
65
    char *endptr;
684✔
66
    long parsed;
684✔
67

68
    if (thumbnailer_size_hint_initialized) {
684✔
69
        return;
684✔
70
    }
71

72
    thumbnailer_size_hint_initialized = 1;
656✔
73
    thumbnailer_default_size_hint = SIXEL_THUMBNAILER_DEFAULT_SIZE;
656✔
74
    thumbnailer_size_hint = thumbnailer_default_size_hint;
656✔
75

76
    env_value = sixel_compat_getenv("SIXEL_THUMBNAILER_HINT_SIZE");
656✔
77
    if (env_value == NULL || env_value[0] == '\0') {
656!
78
        return;
79
    }
80

81
    errno = 0;
×
82
    parsed = strtol(env_value, &endptr, 10);
×
83
    if (errno != 0) {
×
84
        return;
85
    }
86
    if (endptr == env_value || *endptr != '\0') {
×
87
        return;
88
    }
89
    if (parsed <= 0) {
×
90
        return;
91
    }
92
    if (parsed > (long)INT_MAX) {
×
93
        parsed = (long)INT_MAX;
94
    }
95

96
    thumbnailer_default_size_hint = (int)parsed;
×
97
    thumbnailer_size_hint = thumbnailer_default_size_hint;
×
98
}
1!
99

100
int
101
loader_thumbnailer_get_size_hint(void)
×
102
{
103
    loader_thumbnailer_initialize_size_hint();
×
104

105
    return thumbnailer_size_hint;
×
106
}
107

108
int
109
loader_thumbnailer_get_default_size_hint(void)
×
110
{
111
    loader_thumbnailer_initialize_size_hint();
×
112

113
    return thumbnailer_default_size_hint;
×
114
}
115

116
void
117
sixel_helper_set_loader_trace(int enable)
732✔
118
{
119
    loader_trace_enabled = enable ? 1 : 0;
732✔
120
}
732✔
121

122
void
123
sixel_helper_set_thumbnail_size_hint(int size)
684✔
124
{
125
    loader_thumbnailer_initialize_size_hint();
684✔
126

127
    if (size > 0) {
684✔
128
        thumbnailer_size_hint = size;
136✔
129
    } else {
130
        thumbnailer_size_hint = thumbnailer_default_size_hint;
548✔
131
    }
132
}
684✔
133

134
void
135
loader_trace_message(char const *format, ...)
162✔
136
{
137
    va_list args;
162✔
138

139
    if (!loader_trace_enabled) {
162✔
140
        return;
110✔
141
    }
142

143
    fprintf(stderr, "libsixel: ");
52✔
144

145
    va_start(args, format);
52✔
146
    sixel_compat_vfprintf(stderr, format, args);
52✔
147
    va_end(args);
52✔
148

149
    fprintf(stderr, "\n");
52✔
150
}
1!
151

152
void
153
loader_trace_try(char const *name)
834✔
154
{
155
    if (loader_trace_enabled) {
834✔
156
        fprintf(stderr, "libsixel: trying %s loader\n", name);
48✔
157
    }
158
}
834✔
159

160
void
161
loader_trace_result(char const *name, SIXELSTATUS status)
834✔
162
{
163
    if (!loader_trace_enabled) {
834✔
164
        return;
165
    }
166
    if (SIXEL_SUCCEEDED(status)) {
48!
167
        fprintf(stderr, "libsixel: loader %s succeeded\n", name);
48✔
168
    } else {
169
        fprintf(stderr, "libsixel: loader %s failed (%s)\n",
×
170
                name, sixel_helper_format_error(status));
171
    }
172
}
173

174
int
175
loader_trace_is_enabled(void)
828✔
176
{
177
    return loader_trace_enabled;
828✔
178
}
179

180
int
181
chunk_is_png(sixel_chunk_t const *chunk)
×
182
{
183
    if (chunk == NULL || chunk->size < 8) {
×
184
        return 0;
185
    }
186

187
    /*
188
     * PNG streams begin with an 8-byte signature.  Checking the fixed magic
189
     * sequence keeps the detection fast and avoids depending on libpng
190
     * helpers when only the signature is needed.
191
     */
192
    if (chunk->buffer[0] == (unsigned char)0x89 &&
×
193
        chunk->buffer[1] == 'P' &&
×
194
        chunk->buffer[2] == 'N' &&
×
195
        chunk->buffer[3] == 'G' &&
×
196
        chunk->buffer[4] == (unsigned char)0x0d &&
×
197
        chunk->buffer[5] == (unsigned char)0x0a &&
×
198
        chunk->buffer[6] == (unsigned char)0x1a &&
×
199
        chunk->buffer[7] == (unsigned char)0x0a) {
×
200
        return 1;
×
201
    }
202

203
    return 0;
204
}
205

206
int
207
chunk_is_jpeg(sixel_chunk_t const *chunk)
×
208
{
209
    if (chunk == NULL || chunk->size < 2) {
×
210
        return 0;
211
    }
212

213
    /*
214
     * JPEG files start with SOI (Start of Image) marker 0xFF 0xD8.  The GD
215
     * loader uses this to decide whether libgd should attempt JPEG decoding.
216
     */
217
    if (chunk->buffer[0] == (unsigned char)0xff &&
×
218
        chunk->buffer[1] == (unsigned char)0xd8) {
×
219
        return 1;
×
220
    }
221

222
    return 0;
223
}
224

225
int
226
chunk_is_bmp(sixel_chunk_t const *chunk)
×
227
{
228
    if (chunk == NULL || chunk->size < 2) {
×
229
        return 0;
230
    }
231

232
    /* BMP headers begin with the literal characters 'B' 'M'. */
233
    if (chunk->buffer[0] == 'B' && chunk->buffer[1] == 'M') {
×
234
        return 1;
×
235
    }
236

237
    return 0;
238
}
239

240
int
241
chunk_is_gif(sixel_chunk_t const *chunk)
542✔
242
{
243
    if (chunk->size < 6) {
542✔
244
        return 0;
245
    }
246
    if (chunk->buffer[0] == 'G' &&
534!
247
        chunk->buffer[1] == 'I' &&
32!
248
        chunk->buffer[2] == 'F' &&
32!
249
        chunk->buffer[3] == '8' &&
32!
250
        (chunk->buffer[4] == '7' || chunk->buffer[4] == '9') &&
32!
251
        chunk->buffer[5] == 'a') {
32!
252
        return 1;
32✔
253
    }
254
    return 0;
255
}
256

257
/* emacs Local Variables:      */
258
/* emacs mode: c               */
259
/* emacs tab-width: 4          */
260
/* emacs indent-tabs-mode: nil */
261
/* emacs c-basic-offset: 4     */
262
/* emacs End:                  */
263
/* vim: set expandtab ts=4 sts=4 sw=4 : */
264
/* EOF */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc