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