Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/tiff/libtiff/tif_compress.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and 
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 * 
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
16
 * 
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library
27
 *
28
 * Compression Scheme Configuration Support.
29
 */
30
#include "tiffiop.h"
31
32
static int
33
TIFFNoEncode(TIFF* tif, const char* method)
34
0
{
35
0
  const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
36
37
0
  if (c) {
38
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
39
0
           "%s %s encoding is not implemented",
40
0
           c->name, method);
41
0
  } else {
42
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
43
0
      "Compression scheme %"PRIu16" %s encoding is not implemented",
44
0
           tif->tif_dir.td_compression, method);
45
0
  }
46
0
  return (-1);
47
0
}
48
49
int
50
_TIFFNoRowEncode(TIFF* tif, uint8_t* pp, tmsize_t cc, uint16_t s)
51
0
{
52
0
  (void) pp; (void) cc; (void) s;
53
0
  return (TIFFNoEncode(tif, "scanline"));
54
0
}
55
56
int
57
_TIFFNoStripEncode(TIFF* tif, uint8_t* pp, tmsize_t cc, uint16_t s)
58
0
{
59
0
  (void) pp; (void) cc; (void) s;
60
0
  return (TIFFNoEncode(tif, "strip"));
61
0
}
62
63
int
64
_TIFFNoTileEncode(TIFF* tif, uint8_t* pp, tmsize_t cc, uint16_t s)
65
0
{
66
0
  (void) pp; (void) cc; (void) s;
67
0
  return (TIFFNoEncode(tif, "tile"));
68
0
}
69
70
static int
71
TIFFNoDecode(TIFF* tif, const char* method)
72
0
{
73
0
  const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
74
75
0
  if (c)
76
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
77
0
           "%s %s decoding is not implemented",
78
0
           c->name, method);
79
0
  else
80
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
81
0
           "Compression scheme %"PRIu16" %s decoding is not implemented",
82
0
           tif->tif_dir.td_compression, method);
83
0
  return (0);
84
0
}
85
86
static int
87
_TIFFNoFixupTags(TIFF* tif)
88
0
{
89
0
  (void) tif;
90
0
  return (1);
91
0
}
92
93
int
94
_TIFFNoRowDecode(TIFF* tif, uint8_t* pp, tmsize_t cc, uint16_t s)
95
0
{
96
0
  (void) pp; (void) cc; (void) s;
97
0
  return (TIFFNoDecode(tif, "scanline"));
98
0
}
99
100
int
101
_TIFFNoStripDecode(TIFF* tif, uint8_t* pp, tmsize_t cc, uint16_t s)
102
0
{
103
0
  (void) pp; (void) cc; (void) s;
104
0
  return (TIFFNoDecode(tif, "strip"));
105
0
}
106
107
int
108
_TIFFNoTileDecode(TIFF* tif, uint8_t* pp, tmsize_t cc, uint16_t s)
109
0
{
110
0
  (void) pp; (void) cc; (void) s;
111
0
  return (TIFFNoDecode(tif, "tile"));
112
0
}
113
114
int
115
_TIFFNoSeek(TIFF* tif, uint32_t off)
116
0
{
117
0
  (void) off;
118
0
  TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
119
0
         "Compression algorithm does not support random access");
120
0
  return (0);
121
0
}
122
123
int
124
_TIFFNoPreCode(TIFF* tif, uint16_t s)
125
0
{
126
0
  (void) tif; (void) s;
127
0
  return (1);
128
0
}
129
130
0
static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
131
5.65k
static void _TIFFvoid(TIFF* tif) { (void) tif; }
132
133
void
134
_TIFFSetDefaultCompressionState(TIFF* tif)
135
10.9k
{
136
10.9k
  tif->tif_fixuptags = _TIFFNoFixupTags; 
137
10.9k
  tif->tif_decodestatus = TRUE;
138
10.9k
  tif->tif_setupdecode = _TIFFtrue;
139
10.9k
  tif->tif_predecode = _TIFFNoPreCode;
140
10.9k
  tif->tif_decoderow = _TIFFNoRowDecode;  
141
10.9k
  tif->tif_decodestrip = _TIFFNoStripDecode;
142
10.9k
  tif->tif_decodetile = _TIFFNoTileDecode;  
143
10.9k
  tif->tif_encodestatus = TRUE;
144
10.9k
  tif->tif_setupencode = _TIFFtrue;
145
10.9k
  tif->tif_preencode = _TIFFNoPreCode;
146
10.9k
  tif->tif_postencode = _TIFFtrue;
147
10.9k
  tif->tif_encoderow = _TIFFNoRowEncode;
148
10.9k
  tif->tif_encodestrip = _TIFFNoStripEncode;  
149
10.9k
  tif->tif_encodetile = _TIFFNoTileEncode;  
150
10.9k
  tif->tif_close = _TIFFvoid;
151
10.9k
  tif->tif_seek = _TIFFNoSeek;
152
10.9k
  tif->tif_cleanup = _TIFFvoid;
153
10.9k
  tif->tif_defstripsize = _TIFFDefaultStripSize;
154
10.9k
  tif->tif_deftilesize = _TIFFDefaultTileSize;
155
10.9k
  tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
156
10.9k
}
157
158
int
159
TIFFSetCompressionScheme(TIFF* tif, int scheme)
160
6.57k
{
161
6.57k
  const TIFFCodec *c = TIFFFindCODEC((uint16_t) scheme);
162
163
6.57k
  _TIFFSetDefaultCompressionState(tif);
164
  /*
165
   * Don't treat an unknown compression scheme as an error.
166
   * This permits applications to open files with data that
167
   * the library does not have builtin support for, but which
168
   * may still be meaningful.
169
   */
170
6.57k
  return (c ? (*c->init)(tif, scheme) : 1);
171
6.57k
}
172
173
/*
174
 * Other compression schemes may be registered.  Registered
175
 * schemes can also override the builtin versions provided
176
 * by this library.
177
 */
178
typedef struct _codec {
179
  struct _codec* next;
180
  TIFFCodec* info;
181
} codec_t;
182
static codec_t* registeredCODECS = NULL;
183
184
const TIFFCodec*
185
TIFFFindCODEC(uint16_t scheme)
186
6.57k
{
187
6.57k
  const TIFFCodec* c;
188
6.57k
  codec_t* cd;
189
190
6.57k
  for (cd = registeredCODECS; cd; cd = cd->next)
191
0
    if (cd->info->scheme == scheme)
192
0
      return ((const TIFFCodec*) cd->info);
193
16.9k
  for (c = _TIFFBuiltinCODECS; c->name; c++)
194
16.9k
    if (c->scheme == scheme)
195
6.57k
      return (c);
196
0
  return ((const TIFFCodec*) 0);
197
6.57k
}
198
199
TIFFCodec*
200
TIFFRegisterCODEC(uint16_t scheme, const char* name, TIFFInitMethod init)
201
0
{
202
0
  codec_t* cd = (codec_t*)
203
0
      _TIFFmalloc((tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
204
205
0
  if (cd != NULL) {
206
0
    cd->info = (TIFFCodec*) ((uint8_t*) cd + sizeof (codec_t));
207
0
    cd->info->name = (char*)
208
0
        ((uint8_t*) cd->info + sizeof (TIFFCodec));
209
0
    strcpy(cd->info->name, name);
210
0
    cd->info->scheme = scheme;
211
0
    cd->info->init = init;
212
0
    cd->next = registeredCODECS;
213
0
    registeredCODECS = cd;
214
0
  } else {
215
0
    TIFFErrorExt(0, "TIFFRegisterCODEC",
216
0
        "No space to register compression scheme %s", name);
217
0
    return NULL;
218
0
  }
219
0
  return (cd->info);
220
0
}
221
222
void
223
TIFFUnRegisterCODEC(TIFFCodec* c)
224
0
{
225
0
  codec_t* cd;
226
0
  codec_t** pcd;
227
228
0
  for (pcd = &registeredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
229
0
    if (cd->info == c) {
230
0
      *pcd = cd->next;
231
0
      _TIFFfree(cd);
232
0
      return;
233
0
    }
234
0
  TIFFErrorExt(0, "TIFFUnRegisterCODEC",
235
0
      "Cannot remove compression scheme %s; not registered", c->name);
236
0
}
237
238
/************************************************************************/
239
/*                       TIFFGetConfisuredCODECs()                      */
240
/************************************************************************/
241
242
/**
243
 * Get list of configured codecs, both built-in and registered by user.
244
 * Caller is responsible to free this structure.
245
 * 
246
 * @return returns array of TIFFCodec records (the last record should be NULL)
247
 * or NULL if function failed.
248
 */
249
250
TIFFCodec*
251
TIFFGetConfiguredCODECs()
252
0
{
253
0
  int i = 1;
254
0
  codec_t *cd;
255
0
  const TIFFCodec* c;
256
0
  TIFFCodec* codecs = NULL;
257
0
  TIFFCodec* new_codecs;
258
259
0
  for (cd = registeredCODECS; cd; cd = cd->next) {
260
0
    new_codecs = (TIFFCodec *)
261
0
      _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
262
0
    if (!new_codecs) {
263
0
      _TIFFfree (codecs);
264
0
      return NULL;
265
0
    }
266
0
    codecs = new_codecs;
267
0
    _TIFFmemcpy(codecs + i - 1, cd->info, sizeof(TIFFCodec));
268
0
    i++;
269
0
  }
270
0
  for (c = _TIFFBuiltinCODECS; c->name; c++) {
271
0
    if (TIFFIsCODECConfigured(c->scheme)) {
272
0
      new_codecs = (TIFFCodec *)
273
0
        _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
274
0
      if (!new_codecs) {
275
0
        _TIFFfree (codecs);
276
0
        return NULL;
277
0
      }
278
0
      codecs = new_codecs;
279
0
      _TIFFmemcpy(codecs + i - 1, (const void*)c, sizeof(TIFFCodec));
280
0
      i++;
281
0
    }
282
0
  }
283
284
0
  new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
285
0
  if (!new_codecs) {
286
0
    _TIFFfree (codecs);
287
0
    return NULL;
288
0
  }
289
0
  codecs = new_codecs;
290
0
  _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
291
292
0
  return codecs;
293
0
}
294
295
/* vim: set ts=8 sts=8 sw=8 noet: */
296
/*
297
 * Local Variables:
298
 * mode: c
299
 * c-basic-offset: 8
300
 * fill-column: 78
301
 * End:
302
 */