WINGs: fix possible NULL pointer dereference (Coverity #50197)
[wmaker-crm.git] / wrlib / load_webp.c
blobf45ea8f8a992ea2552c592e83d292af69beb778d
1 /* load_webp.c - load WEBP image from file
3 * Raster graphics library
5 * Copyright (c) 2014 Window Maker Team
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <webp/decode.h>
31 #include "wraster.h"
32 #include "imgformat.h"
35 RImage *RLoadWEBP(const char *file_name)
37 FILE *file;
38 RImage *image = NULL;
39 char buffer[20];
40 int raw_data_size;
41 int r;
42 uint8_t *raw_data;
43 WebPBitstreamFeatures features;
44 uint8_t *ret = NULL;
46 file = fopen(file_name, "rb");
47 if (!file) {
48 RErrorCode = RERR_OPEN;
49 return NULL;
52 if (!fread(buffer, sizeof(buffer), 1, file)) {
53 RErrorCode = RERR_BADIMAGEFILE;
54 fclose(file);
55 return NULL;
58 if (!(buffer[ 0] == 'R' && buffer[ 1] == 'I' && buffer[ 2] == 'F' && buffer[ 3] == 'F' &&
59 buffer[ 8] == 'W' && buffer[ 9] == 'E' && buffer[10] == 'B' && buffer[11] == 'P' &&
60 buffer[12] == 'V' && buffer[13] == 'P' && buffer[14] == '8' &&
61 #if WEBP_DECODER_ABI_VERSION < 0x0003 /* old versions don't support WEBPVP8X and WEBPVP8L */
62 buffer[15] == ' ')) {
63 #else
64 (buffer[15] == ' ' || buffer[15] == 'X' || buffer[15] == 'L'))) {
65 #endif
66 RErrorCode = RERR_BADFORMAT;
67 fclose(file);
68 return NULL;
71 fseek(file, 0L, SEEK_END);
72 raw_data_size = ftell(file);
74 if (raw_data_size <= 0) {
75 fprintf(stderr, "wrlib: Failed to find the WEBP file size for \"%s\"\n", file_name);
76 RErrorCode = RERR_BADIMAGEFILE;
77 fclose(file);
78 return NULL;
81 raw_data = (uint8_t *) malloc(raw_data_size);
83 if (!raw_data) {
84 RErrorCode = RERR_NOMEMORY;
85 fclose(file);
86 return NULL;
89 fseek(file, 0L, SEEK_SET);
90 r = fread(raw_data, 1, raw_data_size, file);
91 fclose(file);
93 if (r != raw_data_size) {
94 RErrorCode = RERR_READ;
95 free(raw_data);
96 return NULL;
99 if (WebPGetFeatures(raw_data, raw_data_size, &features) != VP8_STATUS_OK) {
100 fprintf(stderr, "wrlib: WebPGetFeatures has failed on \"%s\"\n", file_name);
101 RErrorCode = RERR_BADIMAGEFILE;
102 free(raw_data);
103 return NULL;
106 if (features.has_alpha) {
107 image = RCreateImage(features.width, features.height, True);
108 if (!image) {
109 RErrorCode = RERR_NOMEMORY;
110 free(raw_data);
111 return NULL;
113 ret = WebPDecodeRGBAInto(raw_data, raw_data_size, image->data,
114 features.width * features.height * 4,
115 features.width * 4);
116 } else {
117 image = RCreateImage(features.width, features.height, False);
118 if (!image) {
119 RErrorCode = RERR_NOMEMORY;
120 free(raw_data);
121 return NULL;
123 ret = WebPDecodeRGBInto(raw_data, raw_data_size, image->data,
124 features.width * features.height * 3,
125 features.width * 3);
128 free(raw_data);
130 if (!ret) {
131 fprintf(stderr, "wrlib: Failed to decode WEBP from file \"%s\"\n", file_name);
132 RErrorCode = RERR_BADIMAGEFILE;
133 RReleaseImage(image);
134 return NULL;
137 return image;