aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c')
-rw-r--r--libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c113
1 files changed, 76 insertions, 37 deletions
diff --git a/libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c b/libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c
index fa6fab2..54e28d5 100644
--- a/libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c
+++ b/libraries/evas/src/modules/loaders/wbmp/evas_image_load_wbmp.c
@@ -19,21 +19,22 @@ static Evas_Image_Load_Func evas_image_load_wbmp_func =
19 EINA_TRUE, 19 EINA_TRUE,
20 evas_image_load_file_head_wbmp, 20 evas_image_load_file_head_wbmp,
21 evas_image_load_file_data_wbmp, 21 evas_image_load_file_data_wbmp,
22 NULL 22 NULL,
23 EINA_FALSE
23}; 24};
24 25
25 26
26static int 27static int
27read_mb(unsigned int *data, FILE *f) 28read_mb(unsigned int *data, void *map, size_t length, size_t *position)
28{ 29{
29 int ac = 0, ct; 30 int ac = 0, ct;
30 unsigned char buf; 31 unsigned char buf;
31 32
32 for (ct = 0;;) 33 for (ct = 0;;)
33 { 34 {
34 if ((ct++) == 5) return -1; 35 if ((ct++) == 5) return -1;
35 if ((fread(&buf, 1, 1, f)) < 1) 36 if (*position > length) return -1;
36 return -1; 37 buf = ((unsigned char *) map)[(*position)++];
37 ac = (ac << 7) | (buf & 0x7f); 38 ac = (ac << 7) | (buf & 0x7f);
38 if ((buf & 0x80) == 0) break; 39 if ((buf & 0x80) == 0) break;
39 } 40 }
@@ -44,70 +45,105 @@ read_mb(unsigned int *data, FILE *f)
44static Eina_Bool 45static Eina_Bool
45evas_image_load_file_head_wbmp(Image_Entry *ie, const char *file, const char *key __UNUSED__, int *error) 46evas_image_load_file_head_wbmp(Image_Entry *ie, const char *file, const char *key __UNUSED__, int *error)
46{ 47{
47 FILE *f; 48 Eina_File *f;
49 void *map = NULL;
50 size_t position = 0;
51 size_t length;
48 unsigned int type, w, h; 52 unsigned int type, w, h;
49 unsigned char fixed_header; 53
50 struct stat statbuf;
51
52 *error = EVAS_LOAD_ERROR_GENERIC; 54 *error = EVAS_LOAD_ERROR_GENERIC;
53 f = fopen(file, "rb"); 55 f = eina_file_open(file, 0);
54 if (!f) 56 if (!f)
55 { 57 {
56 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; 58 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
57 return EINA_FALSE; 59 return EINA_FALSE;
58 } 60 }
59 61
60 if (stat(file, &statbuf) == -1) goto bail; 62 length = eina_file_size_get(f);
61 if (read_mb(&type, f) < 0) goto bail; 63 if (length <= 4) goto bail;
62 64
65 map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
66 if (!map) goto bail;
67
68 if (read_mb(&type, map, length, &position) < 0) goto bail;
69
63 if (type != 0) 70 if (type != 0)
64 { 71 {
65 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; 72 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
66 goto bail; 73 goto bail;
67 } 74 }
68 75
69 if (fread(&fixed_header, 1, 1, f) != 1) goto bail; 76 position++; /* skipping one byte */
70 if (read_mb(&w, f) < 0) goto bail; 77 if (read_mb(&w, map, length, &position) < 0) goto bail;
71 if (read_mb(&h, f) < 0) goto bail; 78 if (read_mb(&h, map, length, &position) < 0) goto bail;
72 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) || 79 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
73 IMG_TOO_BIG(w, h)) 80 IMG_TOO_BIG(w, h))
74 { 81 {
75 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; 82 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
76 goto bail; 83 goto bail;
77 } 84 }
78 85
79 fclose(f); 86 eina_file_map_free(f, map);
87 eina_file_close(f);
80 ie->w = w; 88 ie->w = w;
81 ie->h = h; 89 ie->h = h;
82 90
83 *error = EVAS_LOAD_ERROR_NONE; 91 *error = EVAS_LOAD_ERROR_NONE;
84 return EINA_TRUE; 92 return EINA_TRUE;
85bail: 93bail:
86 fclose(f); 94 if (map) eina_file_map_free(f, map);
95 eina_file_close(f);
87 return EINA_FALSE; 96 return EINA_FALSE;
88} 97}
89 98
90static Eina_Bool 99static Eina_Bool
91evas_image_load_file_data_wbmp(Image_Entry *ie, const char *file, const char *key __UNUSED__, int *error) 100evas_image_load_file_data_wbmp(Image_Entry *ie, const char *file, const char *key __UNUSED__, int *error)
92{ 101{
93 FILE *f; 102 Eina_File *f;
94 unsigned int dummy, line_length; 103 void *map = NULL;
104 size_t position = 0;
105 size_t length;
106 unsigned int type, w, h;
107 unsigned int line_length;
95 unsigned char *line = NULL; 108 unsigned char *line = NULL;
96 int cur = 0, x, y; 109 int cur = 0, x, y;
97 DATA32 *dst_data; 110 DATA32 *dst_data;
98 111
99 *error = EVAS_LOAD_ERROR_GENERIC; 112 *error = EVAS_LOAD_ERROR_GENERIC;
100 f = fopen(file, "rb"); 113 f = eina_file_open(file, EINA_FALSE);
101 if (!f) 114 if (!f)
102 { 115 {
103 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; 116 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
104 return EINA_FALSE; 117 return EINA_FALSE;
105 } 118 }
106 if (read_mb(&dummy, f) < 0) goto bail; 119
107 if (fread(&dummy, 1, 1, f) != 1) goto bail; 120 length = eina_file_size_get(f);
108 if (read_mb(&dummy, f) < 0) goto bail; 121 if (length <= 4) goto bail;
109 if (read_mb(&dummy, f) < 0) goto bail; 122
110 123 map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
124 if (!map) goto bail;
125
126 if (read_mb(&type, map, length, &position) < 0) goto bail;
127 position++; /* skipping one byte */
128 if (read_mb(&w, map, length, &position) < 0) goto bail;
129 if (read_mb(&h, map, length, &position) < 0) goto bail;
130
131 if (type != 0)
132 {
133 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
134 goto bail;
135 }
136
137 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
138 IMG_TOO_BIG(w, h))
139 {
140 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
141 goto bail;
142 }
143
144 ie->w = w;
145 ie->h = h;
146
111 evas_cache_image_surface_alloc(ie, ie->w, ie->h); 147 evas_cache_image_surface_alloc(ie, ie->w, ie->h);
112 dst_data = evas_cache_image_pixels(ie); 148 dst_data = evas_cache_image_pixels(ie);
113 if (!dst_data) 149 if (!dst_data)
@@ -115,13 +151,14 @@ evas_image_load_file_data_wbmp(Image_Entry *ie, const char *file, const char *ke
115 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; 151 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
116 goto bail; 152 goto bail;
117 } 153 }
118 154
119 line_length = (ie->w + 7) >> 3; 155 line_length = (ie->w + 7) >> 3;
120 line = alloca(line_length); 156
121
122 for (y = 0; y < (int)ie->h; y++) 157 for (y = 0; y < (int)ie->h; y++)
123 { 158 {
124 if (fread(line, 1, line_length, f) != line_length) goto bail; 159 if (position + line_length > length) goto bail;
160 line = ((unsigned char*) map) + position;
161 position += line_length;
125 for (x = 0; x < (int)ie->w; x++) 162 for (x = 0; x < (int)ie->w; x++)
126 { 163 {
127 int idx = x >> 3; 164 int idx = x >> 3;
@@ -131,11 +168,13 @@ evas_image_load_file_data_wbmp(Image_Entry *ie, const char *file, const char *ke
131 cur++; 168 cur++;
132 } 169 }
133 } 170 }
134 fclose(f); 171 eina_file_map_free(f, map);
172 eina_file_close(f);
135 *error = EVAS_LOAD_ERROR_NONE; 173 *error = EVAS_LOAD_ERROR_NONE;
136 return EINA_TRUE; 174 return EINA_TRUE;
137bail: 175bail:
138 fclose(f); 176 if (map) eina_file_map_free(f, map);
177 eina_file_close(f);
139 return EINA_FALSE; 178 return EINA_FALSE;
140} 179}
141 180