| 1 |
/****************************************************************************** |
| 2 |
|
| 3 |
dgif_lib.c - GIF decoding |
| 4 |
|
| 5 |
The functions here and in egif_lib.c are partitioned carefully so that |
| 6 |
if you only require one of read and write capability, only one of these |
| 7 |
two modules will be linked. Preserve this property! |
| 8 |
|
| 9 |
*****************************************************************************/ |
| 10 |
// SPDX-License-Identifier: MIT |
| 11 |
// SPDX-FileCopyrightText: Copyright (C) Eric S. Raymond <[email protected]> |
| 12 |
|
| 13 |
#include <fcntl.h> |
| 14 |
#include <limits.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#ifdef _WIN32 |
| 21 |
#include <io.h> |
| 22 |
#else |
| 23 |
#include <unistd.h> |
| 24 |
#endif /* _WIN32 */ |
| 25 |
|
| 26 |
#include "gif_lib.h" |
| 27 |
#include "gif_lib_private.h" |
| 28 |
|
| 29 |
/* compose unsigned little endian value */ |
| 30 |
#define UNSIGNED_LITTLE_ENDIAN(lo, hi) ((lo) | ((hi) << 8)) |
| 31 |
|
| 32 |
/* avoid extra function call in case we use fread (TVT) */ |
| 33 |
static int InternalRead(GifFileType *gif, GifByteType *buf, int len) { |
| 34 |
// fprintf(stderr, "### Read: %d\n", len); |
| 35 |
return (((GifFilePrivateType *)gif->Private)->Read |
| 36 |
? ((GifFilePrivateType *)gif->Private)->Read(gif, buf, len) |
| 37 |
: fread(buf, 1, len, |
| 38 |
((GifFilePrivateType *)gif->Private)->File)); |
| 39 |
} |
| 40 |
|
| 41 |
static int DGifGetWord(GifFileType *GifFile, GifWord *Word); |
| 42 |
static int DGifSetupDecompress(GifFileType *GifFile); |
| 43 |
static int DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line, |
| 44 |
int LineLen); |
| 45 |
static int DGifGetPrefixChar(const GifPrefixType *Prefix, int Code, |
| 46 |
int ClearCode); |
| 47 |
static int DGifDecompressInput(GifFileType *GifFile, int *Code); |
| 48 |
static int DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf, |
| 49 |
GifByteType *NextByte); |
| 50 |
|
| 51 |
/****************************************************************************** |
| 52 |
Open a new GIF file for read, given by its name. |
| 53 |
Returns dynamically allocated GifFileType pointer which serves as the GIF |
| 54 |
info record. |
| 55 |
******************************************************************************/ |
| 56 |
GifFileType *DGifOpenFileName(const char *FileName, int *Error) { |
| 57 |
int FileHandle; |
| 58 |
GifFileType *GifFile; |
| 59 |
|
| 60 |
if ((FileHandle = open(FileName, O_RDONLY)) == -1) { |
| 61 |
if (Error != NULL) { |
| 62 |
*Error = D_GIF_ERR_OPEN_FAILED; |
| 63 |
} |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
GifFile = DGifOpenFileHandle(FileHandle, Error); |
| 68 |
return GifFile; |
| 69 |
} |
| 70 |
|
| 71 |
/****************************************************************************** |
| 72 |
Update a new GIF file, given its file handle. |
| 73 |
Returns dynamically allocated GifFileType pointer which serves as the GIF |
| 74 |
info record. |
| 75 |
******************************************************************************/ |
| 76 |
GifFileType *DGifOpenFileHandle(int FileHandle, int *Error) { |
| 77 |
char Buf[GIF_STAMP_LEN + 1]; |
| 78 |
GifFileType *GifFile; |
| 79 |
GifFilePrivateType *Private; |
| 80 |
FILE *f; |
| 81 |
|
| 82 |
GifFile = (GifFileType *)malloc(sizeof(GifFileType)); |
| 83 |
if (GifFile == NULL) { |
| 84 |
if (Error != NULL) { |
| 85 |
*Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 86 |
} |
| 87 |
(void)close(FileHandle); |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
/*@i1@*/ memset(GifFile, '\0', sizeof(GifFileType)); |
| 92 |
|
| 93 |
/* Belt and suspenders, in case the null pointer isn't zero */ |
| 94 |
GifFile->SavedImages = NULL; |
| 95 |
GifFile->SColorMap = NULL; |
| 96 |
|
| 97 |
Private = (GifFilePrivateType *)calloc(1, sizeof(GifFilePrivateType)); |
| 98 |
if (Private == NULL) { |
| 99 |
if (Error != NULL) { |
| 100 |
*Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 101 |
} |
| 102 |
(void)close(FileHandle); |
| 103 |
free((char *)GifFile); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
/*@i1@*/ memset(Private, '\0', sizeof(GifFilePrivateType)); |
| 108 |
|
| 109 |
#ifdef _WIN32 |
| 110 |
_setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */ |
| 111 |
#endif /* _WIN32 */ |
| 112 |
|
| 113 |
f = fdopen(FileHandle, "rb"); /* Make it into a stream: */ |
| 114 |
|
| 115 |
/*@-mustfreeonly@*/ |
| 116 |
GifFile->Private = (void *)Private; |
| 117 |
Private->FileHandle = FileHandle; |
| 118 |
Private->File = f; |
| 119 |
Private->FileState = FILE_STATE_READ; |
| 120 |
Private->Read = NULL; /* don't use alternate input method (TVT) */ |
| 121 |
GifFile->UserData = NULL; /* TVT */ |
| 122 |
/*@=mustfreeonly@*/ |
| 123 |
|
| 124 |
/* Let's see if this is a GIF file: */ |
| 125 |
/* coverity[check_return] */ |
| 126 |
if (InternalRead(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != |
| 127 |
GIF_STAMP_LEN) { |
| 128 |
if (Error != NULL) { |
| 129 |
*Error = D_GIF_ERR_READ_FAILED; |
| 130 |
} |
| 131 |
(void)fclose(f); |
| 132 |
free((char *)Private); |
| 133 |
free((char *)GifFile); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
/* Check for GIF prefix at start of file */ |
| 138 |
Buf[GIF_STAMP_LEN] = 0; |
| 139 |
if (strncmp(GIF_STAMP, Buf, GIF_VERSION_POS) != 0) { |
| 140 |
if (Error != NULL) { |
| 141 |
*Error = D_GIF_ERR_NOT_GIF_FILE; |
| 142 |
} |
| 143 |
(void)fclose(f); |
| 144 |
free((char *)Private); |
| 145 |
free((char *)GifFile); |
| 146 |
return NULL; |
| 147 |
} |
| 148 |
|
| 149 |
if (DGifGetScreenDesc(GifFile) == GIF_ERROR) { |
| 150 |
(void)fclose(f); |
| 151 |
free((char *)Private); |
| 152 |
free((char *)GifFile); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
GifFile->Error = 0; |
| 157 |
|
| 158 |
/* What version of GIF? */ |
| 159 |
Private->gif89 = (Buf[GIF_VERSION_POS + 1] == '9'); |
| 160 |
|
| 161 |
return GifFile; |
| 162 |
} |
| 163 |
|
| 164 |
/****************************************************************************** |
| 165 |
GifFileType constructor with user supplied input function (TVT) |
| 166 |
******************************************************************************/ |
| 167 |
GifFileType *DGifOpen(void *userData, InputFunc readFunc, int *Error) { |
| 168 |
char Buf[GIF_STAMP_LEN + 1]; |
| 169 |
GifFileType *GifFile; |
| 170 |
GifFilePrivateType *Private; |
| 171 |
|
| 172 |
GifFile = (GifFileType *)malloc(sizeof(GifFileType)); |
| 173 |
if (GifFile == NULL) { |
| 174 |
if (Error != NULL) { |
| 175 |
*Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 176 |
} |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
memset(GifFile, '\0', sizeof(GifFileType)); |
| 181 |
|
| 182 |
/* Belt and suspenders, in case the null pointer isn't zero */ |
| 183 |
GifFile->SavedImages = NULL; |
| 184 |
GifFile->SColorMap = NULL; |
| 185 |
|
| 186 |
Private = (GifFilePrivateType *)calloc(1, sizeof(GifFilePrivateType)); |
| 187 |
if (!Private) { |
| 188 |
if (Error != NULL) { |
| 189 |
*Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 190 |
} |
| 191 |
free((char *)GifFile); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
/*@i1@*/ memset(Private, '\0', sizeof(GifFilePrivateType)); |
| 195 |
|
| 196 |
GifFile->Private = (void *)Private; |
| 197 |
Private->FileHandle = 0; |
| 198 |
Private->File = NULL; |
| 199 |
Private->FileState = FILE_STATE_READ; |
| 200 |
|
| 201 |
Private->Read = readFunc; /* TVT */ |
| 202 |
GifFile->UserData = userData; /* TVT */ |
| 203 |
|
| 204 |
/* Lets see if this is a GIF file: */ |
| 205 |
/* coverity[check_return] */ |
| 206 |
if (InternalRead(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != |
| 207 |
GIF_STAMP_LEN) { |
| 208 |
if (Error != NULL) { |
| 209 |
*Error = D_GIF_ERR_READ_FAILED; |
| 210 |
} |
| 211 |
free((char *)Private); |
| 212 |
free((char *)GifFile); |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
/* Check for GIF prefix at start of file */ |
| 217 |
Buf[GIF_STAMP_LEN] = '\0'; |
| 218 |
if (strncmp(GIF_STAMP, Buf, GIF_VERSION_POS) != 0) { |
| 219 |
if (Error != NULL) { |
| 220 |
*Error = D_GIF_ERR_NOT_GIF_FILE; |
| 221 |
} |
| 222 |
free((char *)Private); |
| 223 |
free((char *)GifFile); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
if (DGifGetScreenDesc(GifFile) == GIF_ERROR) { |
| 228 |
free((char *)Private); |
| 229 |
free((char *)GifFile); |
| 230 |
if (Error != NULL) { |
| 231 |
*Error = D_GIF_ERR_NO_SCRN_DSCR; |
| 232 |
} |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
GifFile->Error = 0; |
| 237 |
|
| 238 |
/* What version of GIF? */ |
| 239 |
Private->gif89 = (Buf[GIF_VERSION_POS + 1] == '9'); |
| 240 |
|
| 241 |
return GifFile; |
| 242 |
} |
| 243 |
|
| 244 |
/****************************************************************************** |
| 245 |
This routine should be called before any other DGif calls. Note that |
| 246 |
this routine is called automatically from DGif file open routines. |
| 247 |
******************************************************************************/ |
| 248 |
int DGifGetScreenDesc(GifFileType *GifFile) { |
| 249 |
int BitsPerPixel; |
| 250 |
bool SortFlag; |
| 251 |
GifByteType Buf[3]; |
| 252 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 253 |
|
| 254 |
if (!IS_READABLE(Private)) { |
| 255 |
/* This file was NOT open for reading: */ |
| 256 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 257 |
return GIF_ERROR; |
| 258 |
} |
| 259 |
|
| 260 |
/* Put the screen descriptor into the file: */ |
| 261 |
if (DGifGetWord(GifFile, &GifFile->SWidth) == GIF_ERROR || |
| 262 |
DGifGetWord(GifFile, &GifFile->SHeight) == GIF_ERROR) { |
| 263 |
return GIF_ERROR; |
| 264 |
} |
| 265 |
|
| 266 |
if (InternalRead(GifFile, Buf, 3) != 3) { |
| 267 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 268 |
GifFreeMapObject(GifFile->SColorMap); |
| 269 |
GifFile->SColorMap = NULL; |
| 270 |
return GIF_ERROR; |
| 271 |
} |
| 272 |
GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1; |
| 273 |
SortFlag = (Buf[0] & 0x08) != 0; |
| 274 |
BitsPerPixel = (Buf[0] & 0x07) + 1; |
| 275 |
GifFile->SBackGroundColor = Buf[1]; |
| 276 |
GifFile->AspectByte = Buf[2]; |
| 277 |
if (Buf[0] & 0x80) { /* Do we have global color map? */ |
| 278 |
int i; |
| 279 |
|
| 280 |
GifFile->SColorMap = GifMakeMapObject(1 << BitsPerPixel, NULL); |
| 281 |
if (GifFile->SColorMap == NULL) { |
| 282 |
GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 283 |
return GIF_ERROR; |
| 284 |
} |
| 285 |
|
| 286 |
/* Get the global color map: */ |
| 287 |
GifFile->SColorMap->SortFlag = SortFlag; |
| 288 |
for (i = 0; i < GifFile->SColorMap->ColorCount; i++) { |
| 289 |
/* coverity[check_return] */ |
| 290 |
if (InternalRead(GifFile, Buf, 3) != 3) { |
| 291 |
GifFreeMapObject(GifFile->SColorMap); |
| 292 |
GifFile->SColorMap = NULL; |
| 293 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 294 |
return GIF_ERROR; |
| 295 |
} |
| 296 |
GifFile->SColorMap->Colors[i].Red = Buf[0]; |
| 297 |
GifFile->SColorMap->Colors[i].Green = Buf[1]; |
| 298 |
GifFile->SColorMap->Colors[i].Blue = Buf[2]; |
| 299 |
} |
| 300 |
} else { |
| 301 |
GifFile->SColorMap = NULL; |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* No check here for whether the background color is in range for the |
| 306 |
* screen color map. Possibly there should be. |
| 307 |
*/ |
| 308 |
|
| 309 |
return GIF_OK; |
| 310 |
} |
| 311 |
|
| 312 |
const char *DGifGetGifVersion(GifFileType *GifFile) { |
| 313 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 314 |
|
| 315 |
if (Private->gif89) { |
| 316 |
return GIF89_STAMP; |
| 317 |
} else { |
| 318 |
return GIF87_STAMP; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/****************************************************************************** |
| 323 |
This routine should be called before any attempt to read an image. |
| 324 |
******************************************************************************/ |
| 325 |
int DGifGetRecordType(GifFileType *GifFile, GifRecordType *Type) { |
| 326 |
GifByteType Buf; |
| 327 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 328 |
|
| 329 |
if (!IS_READABLE(Private)) { |
| 330 |
/* This file was NOT open for reading: */ |
| 331 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 332 |
return GIF_ERROR; |
| 333 |
} |
| 334 |
|
| 335 |
/* coverity[check_return] */ |
| 336 |
if (InternalRead(GifFile, &Buf, 1) != 1) { |
| 337 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 338 |
return GIF_ERROR; |
| 339 |
} |
| 340 |
|
| 341 |
// fprintf(stderr, "### DGifGetRecordType: %02x\n", Buf); |
| 342 |
switch (Buf) { |
| 343 |
case DESCRIPTOR_INTRODUCER: |
| 344 |
*Type = IMAGE_DESC_RECORD_TYPE; |
| 345 |
break; |
| 346 |
case EXTENSION_INTRODUCER: |
| 347 |
*Type = EXTENSION_RECORD_TYPE; |
| 348 |
break; |
| 349 |
case TERMINATOR_INTRODUCER: |
| 350 |
*Type = TERMINATE_RECORD_TYPE; |
| 351 |
break; |
| 352 |
default: |
| 353 |
*Type = UNDEFINED_RECORD_TYPE; |
| 354 |
GifFile->Error = D_GIF_ERR_WRONG_RECORD; |
| 355 |
return GIF_ERROR; |
| 356 |
} |
| 357 |
|
| 358 |
return GIF_OK; |
| 359 |
} |
| 360 |
|
| 361 |
int DGifGetImageHeader(GifFileType *GifFile) { |
| 362 |
unsigned int BitsPerPixel; |
| 363 |
GifByteType Buf[3]; |
| 364 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 365 |
|
| 366 |
if (!IS_READABLE(Private)) { |
| 367 |
/* This file was NOT open for reading: */ |
| 368 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 369 |
return GIF_ERROR; |
| 370 |
} |
| 371 |
|
| 372 |
if (DGifGetWord(GifFile, &GifFile->Image.Left) == GIF_ERROR || |
| 373 |
DGifGetWord(GifFile, &GifFile->Image.Top) == GIF_ERROR || |
| 374 |
DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR || |
| 375 |
DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR) { |
| 376 |
return GIF_ERROR; |
| 377 |
} |
| 378 |
if (InternalRead(GifFile, Buf, 1) != 1) { |
| 379 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 380 |
GifFreeMapObject(GifFile->Image.ColorMap); |
| 381 |
GifFile->Image.ColorMap = NULL; |
| 382 |
return GIF_ERROR; |
| 383 |
} |
| 384 |
BitsPerPixel = (Buf[0] & 0x07) + 1; |
| 385 |
GifFile->Image.Interlace = (Buf[0] & 0x40) ? true : false; |
| 386 |
|
| 387 |
/* Setup the colormap */ |
| 388 |
if (GifFile->Image.ColorMap) { |
| 389 |
GifFreeMapObject(GifFile->Image.ColorMap); |
| 390 |
GifFile->Image.ColorMap = NULL; |
| 391 |
} |
| 392 |
/* Does this image have local color map? */ |
| 393 |
if (Buf[0] & 0x80) { |
| 394 |
unsigned int i; |
| 395 |
|
| 396 |
GifFile->Image.ColorMap = |
| 397 |
GifMakeMapObject(1 << BitsPerPixel, NULL); |
| 398 |
if (GifFile->Image.ColorMap == NULL) { |
| 399 |
GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 400 |
return GIF_ERROR; |
| 401 |
} |
| 402 |
|
| 403 |
/* Get the image local color map: */ |
| 404 |
for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) { |
| 405 |
/* coverity[check_return] */ |
| 406 |
if (InternalRead(GifFile, Buf, 3) != 3) { |
| 407 |
GifFreeMapObject(GifFile->Image.ColorMap); |
| 408 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 409 |
GifFile->Image.ColorMap = NULL; |
| 410 |
return GIF_ERROR; |
| 411 |
} |
| 412 |
GifFile->Image.ColorMap->Colors[i].Red = Buf[0]; |
| 413 |
GifFile->Image.ColorMap->Colors[i].Green = Buf[1]; |
| 414 |
GifFile->Image.ColorMap->Colors[i].Blue = Buf[2]; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
Private->PixelCount = |
| 419 |
(unsigned long)GifFile->Image.Width * |
| 420 |
(unsigned long)GifFile->Image.Height; |
| 421 |
|
| 422 |
/* Reset decompress algorithm parameters. */ |
| 423 |
return DGifSetupDecompress(GifFile); |
| 424 |
} |
| 425 |
|
| 426 |
/****************************************************************************** |
| 427 |
This routine should be called before any attempt to read an image. |
| 428 |
Note it is assumed the Image desc. header has been read. |
| 429 |
******************************************************************************/ |
| 430 |
int DGifGetImageDesc(GifFileType *GifFile) { |
| 431 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 432 |
SavedImage *sp; |
| 433 |
|
| 434 |
if (!IS_READABLE(Private)) { |
| 435 |
/* This file was NOT open for reading: */ |
| 436 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 437 |
return GIF_ERROR; |
| 438 |
} |
| 439 |
|
| 440 |
if (DGifGetImageHeader(GifFile) == GIF_ERROR) { |
| 441 |
return GIF_ERROR; |
| 442 |
} |
| 443 |
|
| 444 |
if (GifFile->SavedImages) { |
| 445 |
SavedImage *new_saved_images = (SavedImage *)reallocarray( |
| 446 |
GifFile->SavedImages, (GifFile->ImageCount + 1), |
| 447 |
sizeof(SavedImage)); |
| 448 |
if (new_saved_images == NULL) { |
| 449 |
GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 450 |
return GIF_ERROR; |
| 451 |
} |
| 452 |
GifFile->SavedImages = new_saved_images; |
| 453 |
} else { |
| 454 |
if ((GifFile->SavedImages = |
| 455 |
(SavedImage *)malloc(sizeof(SavedImage))) == NULL) { |
| 456 |
GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 457 |
return GIF_ERROR; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
sp = &GifFile->SavedImages[GifFile->ImageCount]; |
| 462 |
memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc)); |
| 463 |
if (GifFile->Image.ColorMap != NULL) { |
| 464 |
sp->ImageDesc.ColorMap = |
| 465 |
GifMakeMapObject(GifFile->Image.ColorMap->ColorCount, |
| 466 |
GifFile->Image.ColorMap->Colors); |
| 467 |
if (sp->ImageDesc.ColorMap == NULL) { |
| 468 |
GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 469 |
return GIF_ERROR; |
| 470 |
} |
| 471 |
} |
| 472 |
sp->RasterBits = (unsigned char *)NULL; |
| 473 |
sp->ExtensionBlockCount = 0; |
| 474 |
sp->ExtensionBlocks = (ExtensionBlock *)NULL; |
| 475 |
|
| 476 |
GifFile->ImageCount++; |
| 477 |
|
| 478 |
return GIF_OK; |
| 479 |
} |
| 480 |
|
| 481 |
/****************************************************************************** |
| 482 |
Get one full scanned line (Line) of length LineLen from GIF file. |
| 483 |
******************************************************************************/ |
| 484 |
int DGifGetLine(GifFileType *GifFile, GifPixelType *Line, int LineLen) { |
| 485 |
GifByteType *Dummy; |
| 486 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 487 |
|
| 488 |
if (!IS_READABLE(Private)) { |
| 489 |
/* This file was NOT open for reading: */ |
| 490 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 491 |
return GIF_ERROR; |
| 492 |
} |
| 493 |
|
| 494 |
if (!LineLen) { |
| 495 |
LineLen = GifFile->Image.Width; |
| 496 |
} |
| 497 |
|
| 498 |
if (LineLen < 0 || Private->PixelCount < (unsigned long)LineLen) { |
| 499 |
GifFile->Error = D_GIF_ERR_DATA_TOO_BIG; |
| 500 |
return GIF_ERROR; |
| 501 |
} |
| 502 |
Private->PixelCount -= LineLen; |
| 503 |
|
| 504 |
if (DGifDecompressLine(GifFile, Line, LineLen) == GIF_OK) { |
| 505 |
if (Private->PixelCount == 0) { |
| 506 |
/* We probably won't be called any more, so let's clean |
| 507 |
* up everything before we return: need to flush out all |
| 508 |
* the rest of image until an empty block (size 0) |
| 509 |
* detected. We use GetCodeNext. |
| 510 |
*/ |
| 511 |
do { |
| 512 |
if (DGifGetCodeNext(GifFile, &Dummy) == |
| 513 |
GIF_ERROR) { |
| 514 |
return GIF_ERROR; |
| 515 |
} |
| 516 |
} while (Dummy != NULL); |
| 517 |
} |
| 518 |
return GIF_OK; |
| 519 |
} else { |
| 520 |
return GIF_ERROR; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
/****************************************************************************** |
| 525 |
Put one pixel (Pixel) into GIF file. |
| 526 |
******************************************************************************/ |
| 527 |
int DGifGetPixel(GifFileType *GifFile, GifPixelType Pixel) { |
| 528 |
GifByteType *Dummy; |
| 529 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 530 |
|
| 531 |
if (!IS_READABLE(Private)) { |
| 532 |
/* This file was NOT open for reading: */ |
| 533 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 534 |
return GIF_ERROR; |
| 535 |
} |
| 536 |
if (Private->PixelCount == 0) { |
| 537 |
GifFile->Error = D_GIF_ERR_DATA_TOO_BIG; |
| 538 |
return GIF_ERROR; |
| 539 |
} |
| 540 |
Private->PixelCount --; |
| 541 |
|
| 542 |
if (DGifDecompressLine(GifFile, &Pixel, 1) == GIF_OK) { |
| 543 |
if (Private->PixelCount == 0) { |
| 544 |
/* We probably won't be called any more, so let's clean |
| 545 |
* up everything before we return: need to flush out all |
| 546 |
* the rest of image until an empty block (size 0) |
| 547 |
* detected. We use GetCodeNext. |
| 548 |
*/ |
| 549 |
do { |
| 550 |
if (DGifGetCodeNext(GifFile, &Dummy) == |
| 551 |
GIF_ERROR) { |
| 552 |
return GIF_ERROR; |
| 553 |
} |
| 554 |
} while (Dummy != NULL); |
| 555 |
} |
| 556 |
return GIF_OK; |
| 557 |
} else { |
| 558 |
return GIF_ERROR; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
/****************************************************************************** |
| 563 |
Get an extension block (see GIF manual) from GIF file. This routine only |
| 564 |
returns the first data block, and DGifGetExtensionNext should be called |
| 565 |
after this one until NULL extension is returned. |
| 566 |
The Extension should NOT be freed by the user (not dynamically allocated). |
| 567 |
Note it is assumed the Extension description header has been read. |
| 568 |
******************************************************************************/ |
| 569 |
int DGifGetExtension(GifFileType *GifFile, int *ExtCode, |
| 570 |
GifByteType **Extension) { |
| 571 |
GifByteType Buf; |
| 572 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 573 |
|
| 574 |
// fprintf(stderr, "### -> DGifGetExtension:\n"); |
| 575 |
if (!IS_READABLE(Private)) { |
| 576 |
/* This file was NOT open for reading: */ |
| 577 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 578 |
return GIF_ERROR; |
| 579 |
} |
| 580 |
|
| 581 |
/* coverity[check_return] */ |
| 582 |
if (InternalRead(GifFile, &Buf, 1) != 1) { |
| 583 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 584 |
return GIF_ERROR; |
| 585 |
} |
| 586 |
*ExtCode = Buf; |
| 587 |
// fprintf(stderr, "### <- DGifGetExtension: %02x, about to call |
| 588 |
// next\n", Buf); |
| 589 |
|
| 590 |
return DGifGetExtensionNext(GifFile, Extension); |
| 591 |
} |
| 592 |
|
| 593 |
/****************************************************************************** |
| 594 |
Get a following extension block (see GIF manual) from GIF file. This |
| 595 |
routine should be called until NULL Extension is returned. |
| 596 |
The Extension should NOT be freed by the user (not dynamically allocated). |
| 597 |
******************************************************************************/ |
| 598 |
int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **Extension) { |
| 599 |
GifByteType Buf; |
| 600 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 601 |
|
| 602 |
// fprintf(stderr, "### -> DGifGetExtensionNext\n"); |
| 603 |
if (InternalRead(GifFile, &Buf, 1) != 1) { |
| 604 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 605 |
return GIF_ERROR; |
| 606 |
} |
| 607 |
// fprintf(stderr, "### DGifGetExtensionNext sees %d\n", Buf); |
| 608 |
|
| 609 |
if (Buf > 0) { |
| 610 |
*Extension = Private->Buf; /* Use private unused buffer. */ |
| 611 |
(*Extension)[0] = |
| 612 |
Buf; /* Pascal strings notation (pos. 0 is len.). */ |
| 613 |
/* coverity[tainted_data,check_return] */ |
| 614 |
if (InternalRead(GifFile, &((*Extension)[1]), Buf) != Buf) { |
| 615 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 616 |
return GIF_ERROR; |
| 617 |
} |
| 618 |
} else { |
| 619 |
*Extension = NULL; |
| 620 |
} |
| 621 |
// fprintf(stderr, "### <- DGifGetExtensionNext: %p\n", Extension); |
| 622 |
|
| 623 |
return GIF_OK; |
| 624 |
} |
| 625 |
|
| 626 |
/****************************************************************************** |
| 627 |
Extract a Graphics Control Block from raw extension data |
| 628 |
******************************************************************************/ |
| 629 |
|
| 630 |
int DGifExtensionToGCB(const size_t GifExtensionLength, |
| 631 |
const GifByteType *GifExtension, |
| 632 |
GraphicsControlBlock *GCB) { |
| 633 |
if (GifExtensionLength != 4) { |
| 634 |
return GIF_ERROR; |
| 635 |
} |
| 636 |
|
| 637 |
GCB->DisposalMode = (GifExtension[0] >> 2) & 0x07; |
| 638 |
GCB->UserInputFlag = (GifExtension[0] & 0x02) != 0; |
| 639 |
GCB->DelayTime = |
| 640 |
UNSIGNED_LITTLE_ENDIAN(GifExtension[1], GifExtension[2]); |
| 641 |
if (GifExtension[0] & 0x01) { |
| 642 |
GCB->TransparentColor = (int)GifExtension[3]; |
| 643 |
} else { |
| 644 |
GCB->TransparentColor = NO_TRANSPARENT_COLOR; |
| 645 |
} |
| 646 |
|
| 647 |
return GIF_OK; |
| 648 |
} |
| 649 |
|
| 650 |
/****************************************************************************** |
| 651 |
Extract the Graphics Control Block for a saved image, if it exists. |
| 652 |
******************************************************************************/ |
| 653 |
|
| 654 |
int DGifSavedExtensionToGCB(GifFileType *GifFile, int ImageIndex, |
| 655 |
GraphicsControlBlock *GCB) { |
| 656 |
int i; |
| 657 |
|
| 658 |
if (ImageIndex < 0 || ImageIndex > GifFile->ImageCount - 1) { |
| 659 |
return GIF_ERROR; |
| 660 |
} |
| 661 |
|
| 662 |
GCB->DisposalMode = DISPOSAL_UNSPECIFIED; |
| 663 |
GCB->UserInputFlag = false; |
| 664 |
GCB->DelayTime = 0; |
| 665 |
GCB->TransparentColor = NO_TRANSPARENT_COLOR; |
| 666 |
|
| 667 |
for (i = 0; i < GifFile->SavedImages[ImageIndex].ExtensionBlockCount; |
| 668 |
i++) { |
| 669 |
ExtensionBlock *ep = |
| 670 |
&GifFile->SavedImages[ImageIndex].ExtensionBlocks[i]; |
| 671 |
if (ep->Function == GRAPHICS_EXT_FUNC_CODE) { |
| 672 |
return DGifExtensionToGCB(ep->ByteCount, ep->Bytes, |
| 673 |
GCB); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
return GIF_ERROR; |
| 678 |
} |
| 679 |
|
| 680 |
/****************************************************************************** |
| 681 |
This routine should be called last, to close the GIF file. |
| 682 |
******************************************************************************/ |
| 683 |
int DGifCloseFile(GifFileType *GifFile, int *ErrorCode) { |
| 684 |
GifFilePrivateType *Private; |
| 685 |
|
| 686 |
if (GifFile == NULL || GifFile->Private == NULL) { |
| 687 |
return GIF_ERROR; |
| 688 |
} |
| 689 |
|
| 690 |
if (GifFile->Image.ColorMap) { |
| 691 |
GifFreeMapObject(GifFile->Image.ColorMap); |
| 692 |
GifFile->Image.ColorMap = NULL; |
| 693 |
} |
| 694 |
|
| 695 |
if (GifFile->SColorMap) { |
| 696 |
GifFreeMapObject(GifFile->SColorMap); |
| 697 |
GifFile->SColorMap = NULL; |
| 698 |
} |
| 699 |
|
| 700 |
if (GifFile->SavedImages) { |
| 701 |
GifFreeSavedImages(GifFile); |
| 702 |
GifFile->SavedImages = NULL; |
| 703 |
} |
| 704 |
|
| 705 |
GifFreeExtensions(&GifFile->ExtensionBlockCount, |
| 706 |
&GifFile->ExtensionBlocks); |
| 707 |
|
| 708 |
Private = (GifFilePrivateType *)GifFile->Private; |
| 709 |
|
| 710 |
if (!IS_READABLE(Private)) { |
| 711 |
/* This file was NOT open for reading: */ |
| 712 |
if (ErrorCode != NULL) { |
| 713 |
*ErrorCode = D_GIF_ERR_NOT_READABLE; |
| 714 |
} |
| 715 |
free((char *)GifFile->Private); |
| 716 |
free(GifFile); |
| 717 |
return GIF_ERROR; |
| 718 |
} |
| 719 |
|
| 720 |
if (Private->File && (fclose(Private->File) != 0)) { |
| 721 |
if (ErrorCode != NULL) { |
| 722 |
*ErrorCode = D_GIF_ERR_CLOSE_FAILED; |
| 723 |
} |
| 724 |
free((char *)GifFile->Private); |
| 725 |
free(GifFile); |
| 726 |
return GIF_ERROR; |
| 727 |
} |
| 728 |
|
| 729 |
free((char *)GifFile->Private); |
| 730 |
free(GifFile); |
| 731 |
if (ErrorCode != NULL) { |
| 732 |
*ErrorCode = D_GIF_SUCCEEDED; |
| 733 |
} |
| 734 |
return GIF_OK; |
| 735 |
} |
| 736 |
|
| 737 |
/****************************************************************************** |
| 738 |
Get 2 bytes (word) from the given file: |
| 739 |
******************************************************************************/ |
| 740 |
static int DGifGetWord(GifFileType *GifFile, GifWord *Word) { |
| 741 |
unsigned char c[2]; |
| 742 |
|
| 743 |
/* coverity[check_return] */ |
| 744 |
if (InternalRead(GifFile, c, 2) != 2) { |
| 745 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 746 |
return GIF_ERROR; |
| 747 |
} |
| 748 |
|
| 749 |
*Word = (GifWord)UNSIGNED_LITTLE_ENDIAN(c[0], c[1]); |
| 750 |
return GIF_OK; |
| 751 |
} |
| 752 |
|
| 753 |
/****************************************************************************** |
| 754 |
Get the image code in compressed form. This routine can be called if the |
| 755 |
information needed to be piped out as is. Obviously this is much faster |
| 756 |
than decoding and encoding again. This routine should be followed by calls |
| 757 |
to DGifGetCodeNext, until NULL block is returned. |
| 758 |
The block should NOT be freed by the user (not dynamically allocated). |
| 759 |
******************************************************************************/ |
| 760 |
int DGifGetCode(GifFileType *GifFile, int *CodeSize, GifByteType **CodeBlock) { |
| 761 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 762 |
|
| 763 |
if (!IS_READABLE(Private)) { |
| 764 |
/* This file was NOT open for reading: */ |
| 765 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 766 |
return GIF_ERROR; |
| 767 |
} |
| 768 |
|
| 769 |
*CodeSize = Private->BitsPerPixel; |
| 770 |
|
| 771 |
return DGifGetCodeNext(GifFile, CodeBlock); |
| 772 |
} |
| 773 |
|
| 774 |
/****************************************************************************** |
| 775 |
Continue to get the image code in compressed form. This routine should be |
| 776 |
called until NULL block is returned. |
| 777 |
The block should NOT be freed by the user (not dynamically allocated). |
| 778 |
******************************************************************************/ |
| 779 |
int DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock) { |
| 780 |
GifByteType Buf; |
| 781 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 782 |
|
| 783 |
/* coverity[tainted_data_argument] */ |
| 784 |
/* coverity[check_return] */ |
| 785 |
if (InternalRead(GifFile, &Buf, 1) != 1) { |
| 786 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 787 |
return GIF_ERROR; |
| 788 |
} |
| 789 |
|
| 790 |
/* coverity[lower_bounds] */ |
| 791 |
if (Buf > 0) { |
| 792 |
*CodeBlock = Private->Buf; /* Use private unused buffer. */ |
| 793 |
(*CodeBlock)[0] = |
| 794 |
Buf; /* Pascal strings notation (pos. 0 is len.). */ |
| 795 |
/* coverity[tainted_data] */ |
| 796 |
if (InternalRead(GifFile, &((*CodeBlock)[1]), Buf) != Buf) { |
| 797 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 798 |
return GIF_ERROR; |
| 799 |
} |
| 800 |
} else { |
| 801 |
*CodeBlock = NULL; |
| 802 |
Private->Buf[0] = 0; /* Make sure the buffer is empty! */ |
| 803 |
Private->PixelCount = |
| 804 |
0; /* And local info. indicate image read. */ |
| 805 |
} |
| 806 |
|
| 807 |
return GIF_OK; |
| 808 |
} |
| 809 |
|
| 810 |
/****************************************************************************** |
| 811 |
Setup the LZ decompression for this image: |
| 812 |
******************************************************************************/ |
| 813 |
static int DGifSetupDecompress(GifFileType *GifFile) { |
| 814 |
int i, BitsPerPixel; |
| 815 |
GifByteType CodeSize; |
| 816 |
GifPrefixType *Prefix; |
| 817 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 818 |
|
| 819 |
/* coverity[check_return] */ |
| 820 |
if (InternalRead(GifFile, &CodeSize, 1) < |
| 821 |
1) { /* Read Code size from file. */ |
| 822 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 823 |
return GIF_ERROR; /* Failed to read Code size. */ |
| 824 |
} |
| 825 |
BitsPerPixel = CodeSize; |
| 826 |
|
| 827 |
/* this can only happen on a severely malformed GIF */ |
| 828 |
if (BitsPerPixel < 2 || BitsPerPixel > 8) { |
| 829 |
GifFile->Error = |
| 830 |
D_GIF_ERR_READ_FAILED; /* somewhat bogus error code */ |
| 831 |
return GIF_ERROR; /* Failed to read Code size. */ |
| 832 |
} |
| 833 |
|
| 834 |
Private->Buf[0] = 0; /* Input Buffer empty. */ |
| 835 |
Private->BitsPerPixel = BitsPerPixel; |
| 836 |
Private->ClearCode = (1 << BitsPerPixel); |
| 837 |
Private->EOFCode = Private->ClearCode + 1; |
| 838 |
Private->RunningCode = Private->EOFCode + 1; |
| 839 |
Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */ |
| 840 |
Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */ |
| 841 |
Private->StackPtr = 0; /* No pixels on the pixel stack. */ |
| 842 |
Private->LastCode = NO_SUCH_CODE; |
| 843 |
Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */ |
| 844 |
Private->CrntShiftDWord = 0; |
| 845 |
|
| 846 |
Prefix = Private->Prefix; |
| 847 |
for (i = 0; i <= LZ_MAX_CODE; i++) { |
| 848 |
Prefix[i] = NO_SUCH_CODE; |
| 849 |
} |
| 850 |
|
| 851 |
return GIF_OK; |
| 852 |
} |
| 853 |
|
| 854 |
/****************************************************************************** |
| 855 |
The LZ decompression routine: |
| 856 |
This version decompress the given GIF file into Line of length LineLen. |
| 857 |
This routine can be called few times (one per scan line, for example), in |
| 858 |
order the complete the whole image. |
| 859 |
******************************************************************************/ |
| 860 |
static int DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line, |
| 861 |
int LineLen) { |
| 862 |
int i = 0; |
| 863 |
int j, CrntCode, EOFCode, ClearCode, CrntPrefix, LastCode, StackPtr; |
| 864 |
GifByteType *Stack, *Suffix; |
| 865 |
GifPrefixType *Prefix; |
| 866 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 867 |
|
| 868 |
StackPtr = Private->StackPtr; |
| 869 |
Prefix = Private->Prefix; |
| 870 |
Suffix = Private->Suffix; |
| 871 |
Stack = Private->Stack; |
| 872 |
EOFCode = Private->EOFCode; |
| 873 |
ClearCode = Private->ClearCode; |
| 874 |
LastCode = Private->LastCode; |
| 875 |
|
| 876 |
if (StackPtr > LZ_MAX_CODE) { |
| 877 |
return GIF_ERROR; |
| 878 |
} |
| 879 |
|
| 880 |
if (StackPtr != 0) { |
| 881 |
/* Let pop the stack off before continuing to read the GIF |
| 882 |
* file: */ |
| 883 |
while (StackPtr != 0 && i < LineLen) { |
| 884 |
Line[i++] = Stack[--StackPtr]; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
while (i < LineLen) { /* Decode LineLen items. */ |
| 889 |
if (DGifDecompressInput(GifFile, &CrntCode) == GIF_ERROR) { |
| 890 |
return GIF_ERROR; |
| 891 |
} |
| 892 |
|
| 893 |
if (CrntCode == EOFCode) { |
| 894 |
/* Note however that usually we will not be here as we |
| 895 |
* will stop decoding as soon as we got all the pixel, |
| 896 |
* or EOF code will not be read at all, and |
| 897 |
* DGifGetLine/Pixel clean everything. */ |
| 898 |
GifFile->Error = D_GIF_ERR_EOF_TOO_SOON; |
| 899 |
return GIF_ERROR; |
| 900 |
} else if (CrntCode == ClearCode) { |
| 901 |
/* We need to start over again: */ |
| 902 |
for (j = 0; j <= LZ_MAX_CODE; j++) { |
| 903 |
Prefix[j] = NO_SUCH_CODE; |
| 904 |
} |
| 905 |
Private->RunningCode = Private->EOFCode + 1; |
| 906 |
Private->RunningBits = Private->BitsPerPixel + 1; |
| 907 |
Private->MaxCode1 = 1 << Private->RunningBits; |
| 908 |
LastCode = Private->LastCode = NO_SUCH_CODE; |
| 909 |
} else { |
| 910 |
/* Its regular code - if in pixel range simply add it to |
| 911 |
* output stream, otherwise trace to codes linked list |
| 912 |
* until the prefix is in pixel range: */ |
| 913 |
if (CrntCode < ClearCode) { |
| 914 |
/* This is simple - its pixel scalar, so add it |
| 915 |
* to output: */ |
| 916 |
Line[i++] = CrntCode; |
| 917 |
} else { |
| 918 |
/* Its a code to needed to be traced: trace the |
| 919 |
* linked list until the prefix is a pixel, |
| 920 |
* while pushing the suffix pixels on our stack. |
| 921 |
* If we done, pop the stack in reverse (that's |
| 922 |
* what stack is good for!) order to output. */ |
| 923 |
if (Prefix[CrntCode] == NO_SUCH_CODE) { |
| 924 |
CrntPrefix = LastCode; |
| 925 |
|
| 926 |
/* Only allowed if CrntCode is exactly |
| 927 |
* the running code: In that case |
| 928 |
* CrntCode = XXXCode, CrntCode or the |
| 929 |
* prefix code is last code and the |
| 930 |
* suffix char is exactly the prefix of |
| 931 |
* last code! */ |
| 932 |
if (CrntCode == |
| 933 |
Private->RunningCode - 2) { |
| 934 |
Suffix[Private->RunningCode - |
| 935 |
2] = Stack[StackPtr++] = |
| 936 |
DGifGetPrefixChar( |
| 937 |
Prefix, LastCode, |
| 938 |
ClearCode); |
| 939 |
} else { |
| 940 |
Suffix[Private->RunningCode - |
| 941 |
2] = Stack[StackPtr++] = |
| 942 |
DGifGetPrefixChar( |
| 943 |
Prefix, CrntCode, |
| 944 |
ClearCode); |
| 945 |
} |
| 946 |
} else { |
| 947 |
CrntPrefix = CrntCode; |
| 948 |
} |
| 949 |
|
| 950 |
/* Now (if image is O.K.) we should not get a |
| 951 |
* NO_SUCH_CODE during the trace. As we might |
| 952 |
* loop forever, in case of defective image, we |
| 953 |
* use StackPtr as loop counter and stop before |
| 954 |
* overflowing Stack[]. */ |
| 955 |
while (StackPtr < LZ_MAX_CODE && |
| 956 |
CrntPrefix > ClearCode && |
| 957 |
CrntPrefix <= LZ_MAX_CODE) { |
| 958 |
Stack[StackPtr++] = Suffix[CrntPrefix]; |
| 959 |
CrntPrefix = Prefix[CrntPrefix]; |
| 960 |
} |
| 961 |
if (StackPtr >= LZ_MAX_CODE || |
| 962 |
CrntPrefix > LZ_MAX_CODE) { |
| 963 |
GifFile->Error = D_GIF_ERR_IMAGE_DEFECT; |
| 964 |
return GIF_ERROR; |
| 965 |
} |
| 966 |
/* Push the last character on stack: */ |
| 967 |
Stack[StackPtr++] = CrntPrefix; |
| 968 |
|
| 969 |
/* Now lets pop all the stack into output: */ |
| 970 |
while (StackPtr != 0 && i < LineLen) { |
| 971 |
Line[i++] = Stack[--StackPtr]; |
| 972 |
} |
| 973 |
} |
| 974 |
if (LastCode != NO_SUCH_CODE && |
| 975 |
Private->RunningCode - 2 < (LZ_MAX_CODE + 1) && |
| 976 |
Prefix[Private->RunningCode - 2] == NO_SUCH_CODE) { |
| 977 |
Prefix[Private->RunningCode - 2] = LastCode; |
| 978 |
|
| 979 |
if (CrntCode == Private->RunningCode - 2) { |
| 980 |
/* Only allowed if CrntCode is exactly |
| 981 |
* the running code: In that case |
| 982 |
* CrntCode = XXXCode, CrntCode or the |
| 983 |
* prefix code is last code and the |
| 984 |
* suffix char is exactly the prefix of |
| 985 |
* last code! */ |
| 986 |
Suffix[Private->RunningCode - 2] = |
| 987 |
DGifGetPrefixChar(Prefix, LastCode, |
| 988 |
ClearCode); |
| 989 |
} else { |
| 990 |
Suffix[Private->RunningCode - 2] = |
| 991 |
DGifGetPrefixChar(Prefix, CrntCode, |
| 992 |
ClearCode); |
| 993 |
} |
| 994 |
} |
| 995 |
LastCode = CrntCode; |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
Private->LastCode = LastCode; |
| 1000 |
Private->StackPtr = StackPtr; |
| 1001 |
|
| 1002 |
return GIF_OK; |
| 1003 |
} |
| 1004 |
|
| 1005 |
/****************************************************************************** |
| 1006 |
Routine to trace the Prefixes linked list until we get a prefix which is |
| 1007 |
not code, but a pixel value (less than ClearCode). Returns that pixel value. |
| 1008 |
If image is defective, we might loop here forever, so we limit the loops to |
| 1009 |
the maximum possible if image O.k. - LZ_MAX_CODE times. |
| 1010 |
******************************************************************************/ |
| 1011 |
static int DGifGetPrefixChar(const GifPrefixType *Prefix, int Code, |
| 1012 |
int ClearCode) { |
| 1013 |
int i = 0; |
| 1014 |
|
| 1015 |
while (Code > ClearCode && i++ <= LZ_MAX_CODE) { |
| 1016 |
if (Code > LZ_MAX_CODE) { |
| 1017 |
return NO_SUCH_CODE; |
| 1018 |
} |
| 1019 |
Code = Prefix[Code]; |
| 1020 |
} |
| 1021 |
return Code; |
| 1022 |
} |
| 1023 |
|
| 1024 |
/****************************************************************************** |
| 1025 |
Interface for accessing the LZ codes directly. Set Code to the real code |
| 1026 |
(12bits), or to -1 if EOF code is returned. |
| 1027 |
******************************************************************************/ |
| 1028 |
int DGifGetLZCodes(GifFileType *GifFile, int *Code) { |
| 1029 |
GifByteType *CodeBlock; |
| 1030 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 1031 |
|
| 1032 |
if (!IS_READABLE(Private)) { |
| 1033 |
/* This file was NOT open for reading: */ |
| 1034 |
GifFile->Error = D_GIF_ERR_NOT_READABLE; |
| 1035 |
return GIF_ERROR; |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (DGifDecompressInput(GifFile, Code) == GIF_ERROR) { |
| 1039 |
return GIF_ERROR; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (*Code == Private->EOFCode) { |
| 1043 |
/* Skip rest of codes (hopefully only NULL terminating block): |
| 1044 |
*/ |
| 1045 |
do { |
| 1046 |
if (DGifGetCodeNext(GifFile, &CodeBlock) == GIF_ERROR) { |
| 1047 |
return GIF_ERROR; |
| 1048 |
} |
| 1049 |
} while (CodeBlock != NULL); |
| 1050 |
|
| 1051 |
*Code = -1; |
| 1052 |
} else if (*Code == Private->ClearCode) { |
| 1053 |
/* We need to start over again: */ |
| 1054 |
Private->RunningCode = Private->EOFCode + 1; |
| 1055 |
Private->RunningBits = Private->BitsPerPixel + 1; |
| 1056 |
Private->MaxCode1 = 1 << Private->RunningBits; |
| 1057 |
} |
| 1058 |
|
| 1059 |
return GIF_OK; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/****************************************************************************** |
| 1063 |
The LZ decompression input routine: |
| 1064 |
This routine is responsible for the decompression of the bit stream from |
| 1065 |
8 bits (bytes) packets, into the real codes. |
| 1066 |
Returns GIF_OK if read successfully. |
| 1067 |
******************************************************************************/ |
| 1068 |
static int DGifDecompressInput(GifFileType *GifFile, int *Code) { |
| 1069 |
static const unsigned short CodeMasks[] = { |
| 1070 |
0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, |
| 1071 |
0x007f, 0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff}; |
| 1072 |
|
| 1073 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 1074 |
|
| 1075 |
GifByteType NextByte; |
| 1076 |
|
| 1077 |
/* The image can't contain more than LZ_BITS per code. */ |
| 1078 |
if (Private->RunningBits > LZ_BITS) { |
| 1079 |
GifFile->Error = D_GIF_ERR_IMAGE_DEFECT; |
| 1080 |
return GIF_ERROR; |
| 1081 |
} |
| 1082 |
|
| 1083 |
while (Private->CrntShiftState < Private->RunningBits) { |
| 1084 |
/* Needs to get more bytes from input stream for next code: */ |
| 1085 |
if (DGifBufferedInput(GifFile, Private->Buf, &NextByte) == |
| 1086 |
GIF_ERROR) { |
| 1087 |
return GIF_ERROR; |
| 1088 |
} |
| 1089 |
Private->CrntShiftDWord |= ((unsigned long)NextByte) |
| 1090 |
<< Private->CrntShiftState; |
| 1091 |
Private->CrntShiftState += 8; |
| 1092 |
} |
| 1093 |
*Code = Private->CrntShiftDWord & CodeMasks[Private->RunningBits]; |
| 1094 |
|
| 1095 |
Private->CrntShiftDWord >>= Private->RunningBits; |
| 1096 |
Private->CrntShiftState -= Private->RunningBits; |
| 1097 |
|
| 1098 |
/* If code cannot fit into RunningBits bits, must raise its size. Note |
| 1099 |
* however that codes above 4095 are used for special signaling. |
| 1100 |
* If we're using LZ_BITS bits already and we're at the max code, just |
| 1101 |
* keep using the table as it is, don't increment Private->RunningCode. |
| 1102 |
*/ |
| 1103 |
if (Private->RunningCode < LZ_MAX_CODE + 2 && |
| 1104 |
++Private->RunningCode > Private->MaxCode1 && |
| 1105 |
Private->RunningBits < LZ_BITS) { |
| 1106 |
Private->MaxCode1 <<= 1; |
| 1107 |
Private->RunningBits++; |
| 1108 |
} |
| 1109 |
return GIF_OK; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/****************************************************************************** |
| 1113 |
This routines read one GIF data block at a time and buffers it internally |
| 1114 |
so that the decompression routine could access it. |
| 1115 |
The routine returns the next byte from its internal buffer (or read next |
| 1116 |
block in if buffer empty) and returns GIF_OK if successful. |
| 1117 |
******************************************************************************/ |
| 1118 |
static int DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf, |
| 1119 |
GifByteType *NextByte) { |
| 1120 |
if (Buf[0] == 0) { |
| 1121 |
/* Needs to read the next buffer - this one is empty: */ |
| 1122 |
/* coverity[check_return] */ |
| 1123 |
if (InternalRead(GifFile, Buf, 1) != 1) { |
| 1124 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 1125 |
return GIF_ERROR; |
| 1126 |
} |
| 1127 |
/* There shouldn't be any empty data blocks here as the LZW spec |
| 1128 |
* says the LZW termination code should come first. Therefore |
| 1129 |
* we shouldn't be inside this routine at that point. |
| 1130 |
*/ |
| 1131 |
if (Buf[0] == 0) { |
| 1132 |
GifFile->Error = D_GIF_ERR_IMAGE_DEFECT; |
| 1133 |
return GIF_ERROR; |
| 1134 |
} |
| 1135 |
if (InternalRead(GifFile, &Buf[1], Buf[0]) != Buf[0]) { |
| 1136 |
GifFile->Error = D_GIF_ERR_READ_FAILED; |
| 1137 |
return GIF_ERROR; |
| 1138 |
} |
| 1139 |
*NextByte = Buf[1]; |
| 1140 |
Buf[1] = 2; /* We use now the second place as last char read! */ |
| 1141 |
Buf[0]--; |
| 1142 |
} else { |
| 1143 |
*NextByte = Buf[Buf[1]++]; |
| 1144 |
Buf[0]--; |
| 1145 |
} |
| 1146 |
|
| 1147 |
return GIF_OK; |
| 1148 |
} |
| 1149 |
|
| 1150 |
/****************************************************************************** |
| 1151 |
This routine is called in case of error during parsing image. We need to |
| 1152 |
decrease image counter and reallocate memory for saved images. Not decreasing |
| 1153 |
ImageCount may lead to null pointer dereference, because the last element in |
| 1154 |
SavedImages may point to the spoilt image and null pointer buffers. |
| 1155 |
*******************************************************************************/ |
| 1156 |
void DGifDecreaseImageCounter(GifFileType *GifFile) { |
| 1157 |
GifFile->ImageCount--; |
| 1158 |
if (GifFile->SavedImages[GifFile->ImageCount].RasterBits != NULL) { |
| 1159 |
free(GifFile->SavedImages[GifFile->ImageCount].RasterBits); |
| 1160 |
} |
| 1161 |
if (GifFile->SavedImages[GifFile->ImageCount].ImageDesc.ColorMap != NULL) { |
| 1162 |
GifFreeMapObject(GifFile->SavedImages[GifFile->ImageCount].ImageDesc.ColorMap); |
| 1163 |
} |
| 1164 |
|
| 1165 |
// Realloc array according to the new image counter. |
| 1166 |
SavedImage *correct_saved_images = (SavedImage *)reallocarray( |
| 1167 |
GifFile->SavedImages, GifFile->ImageCount, sizeof(SavedImage)); |
| 1168 |
if (correct_saved_images != NULL) { |
| 1169 |
GifFile->SavedImages = correct_saved_images; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/****************************************************************************** |
| 1174 |
This routine reads an entire GIF into core, hanging all its state info off |
| 1175 |
the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle() |
| 1176 |
first to initialize I/O. Its inverse is EGifSpew(). |
| 1177 |
*******************************************************************************/ |
| 1178 |
int DGifSlurp(GifFileType *GifFile) { |
| 1179 |
size_t ImageSize; |
| 1180 |
GifRecordType RecordType; |
| 1181 |
SavedImage *sp; |
| 1182 |
GifByteType *ExtData; |
| 1183 |
int ExtFunction; |
| 1184 |
|
| 1185 |
GifFile->ExtensionBlocks = NULL; |
| 1186 |
GifFile->ExtensionBlockCount = 0; |
| 1187 |
|
| 1188 |
do { |
| 1189 |
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) { |
| 1190 |
return (GIF_ERROR); |
| 1191 |
} |
| 1192 |
|
| 1193 |
switch (RecordType) { |
| 1194 |
case IMAGE_DESC_RECORD_TYPE: |
| 1195 |
if (DGifGetImageDesc(GifFile) == GIF_ERROR) { |
| 1196 |
return (GIF_ERROR); |
| 1197 |
} |
| 1198 |
|
| 1199 |
sp = &GifFile->SavedImages[GifFile->ImageCount - 1]; |
| 1200 |
/* Allocate memory for the image */ |
| 1201 |
if (sp->ImageDesc.Width <= 0 || |
| 1202 |
sp->ImageDesc.Height <= 0 || |
| 1203 |
sp->ImageDesc.Width > |
| 1204 |
(INT_MAX / sp->ImageDesc.Height)) { |
| 1205 |
DGifDecreaseImageCounter(GifFile); |
| 1206 |
return GIF_ERROR; |
| 1207 |
} |
| 1208 |
ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height; |
| 1209 |
|
| 1210 |
if (ImageSize > (SIZE_MAX / sizeof(GifPixelType))) { |
| 1211 |
DGifDecreaseImageCounter(GifFile); |
| 1212 |
return GIF_ERROR; |
| 1213 |
} |
| 1214 |
sp->RasterBits = (unsigned char *)reallocarray( |
| 1215 |
NULL, ImageSize, sizeof(GifPixelType)); |
| 1216 |
|
| 1217 |
if (sp->RasterBits == NULL) { |
| 1218 |
DGifDecreaseImageCounter(GifFile); |
| 1219 |
return GIF_ERROR; |
| 1220 |
} |
| 1221 |
|
| 1222 |
if (sp->ImageDesc.Interlace) { |
| 1223 |
int i, j; |
| 1224 |
/* |
| 1225 |
* The way an interlaced image should be read - |
| 1226 |
* offsets and jumps... |
| 1227 |
*/ |
| 1228 |
static const int InterlacedOffset[] = {0, 4, 2, |
| 1229 |
1}; |
| 1230 |
static const int InterlacedJumps[] = {8, 8, 4, |
| 1231 |
2}; |
| 1232 |
/* Need to perform 4 passes on the image */ |
| 1233 |
for (i = 0; i < 4; i++) { |
| 1234 |
for (j = InterlacedOffset[i]; |
| 1235 |
j < sp->ImageDesc.Height; |
| 1236 |
j += InterlacedJumps[i]) { |
| 1237 |
if (DGifGetLine( |
| 1238 |
GifFile, |
| 1239 |
sp->RasterBits + |
| 1240 |
j * sp->ImageDesc |
| 1241 |
.Width, |
| 1242 |
sp->ImageDesc.Width) == |
| 1243 |
GIF_ERROR) { |
| 1244 |
DGifDecreaseImageCounter( |
| 1245 |
GifFile); |
| 1246 |
return GIF_ERROR; |
| 1247 |
} |
| 1248 |
} |
| 1249 |
} |
| 1250 |
} else { |
| 1251 |
if (DGifGetLine(GifFile, sp->RasterBits, |
| 1252 |
ImageSize) == GIF_ERROR) { |
| 1253 |
DGifDecreaseImageCounter(GifFile); |
| 1254 |
return GIF_ERROR; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (GifFile->ExtensionBlocks) { |
| 1259 |
sp->ExtensionBlocks = GifFile->ExtensionBlocks; |
| 1260 |
sp->ExtensionBlockCount = |
| 1261 |
GifFile->ExtensionBlockCount; |
| 1262 |
|
| 1263 |
GifFile->ExtensionBlocks = NULL; |
| 1264 |
GifFile->ExtensionBlockCount = 0; |
| 1265 |
} |
| 1266 |
break; |
| 1267 |
|
| 1268 |
case EXTENSION_RECORD_TYPE: |
| 1269 |
if (DGifGetExtension(GifFile, &ExtFunction, &ExtData) == |
| 1270 |
GIF_ERROR) { |
| 1271 |
return (GIF_ERROR); |
| 1272 |
} |
| 1273 |
/* Create an extension block with our data */ |
| 1274 |
if (ExtData != NULL) { |
| 1275 |
if (GifAddExtensionBlock( |
| 1276 |
&GifFile->ExtensionBlockCount, |
| 1277 |
&GifFile->ExtensionBlocks, ExtFunction, |
| 1278 |
ExtData[0], &ExtData[1]) == GIF_ERROR) { |
| 1279 |
return (GIF_ERROR); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
for (;;) { |
| 1283 |
if (DGifGetExtensionNext(GifFile, &ExtData) == |
| 1284 |
GIF_ERROR) { |
| 1285 |
return (GIF_ERROR); |
| 1286 |
} |
| 1287 |
if (ExtData == NULL) { |
| 1288 |
break; |
| 1289 |
} |
| 1290 |
/* Continue the extension block */ |
| 1291 |
if (GifAddExtensionBlock( |
| 1292 |
&GifFile->ExtensionBlockCount, |
| 1293 |
&GifFile->ExtensionBlocks, |
| 1294 |
CONTINUE_EXT_FUNC_CODE, ExtData[0], |
| 1295 |
&ExtData[1]) == GIF_ERROR) { |
| 1296 |
return (GIF_ERROR); |
| 1297 |
} |
| 1298 |
} |
| 1299 |
break; |
| 1300 |
|
| 1301 |
case TERMINATE_RECORD_TYPE: |
| 1302 |
break; |
| 1303 |
|
| 1304 |
default: /* Should be trapped by DGifGetRecordType */ |
| 1305 |
break; |
| 1306 |
} |
| 1307 |
} while (RecordType != TERMINATE_RECORD_TYPE); |
| 1308 |
|
| 1309 |
/* Sanity check for corrupted file */ |
| 1310 |
if (GifFile->ImageCount == 0) { |
| 1311 |
GifFile->Error = D_GIF_ERR_NO_IMAG_DSCR; |
| 1312 |
return (GIF_ERROR); |
| 1313 |
} |
| 1314 |
|
| 1315 |
return (GIF_OK); |
| 1316 |
} |
| 1317 |
|
| 1318 |
/* end */ |