- Timestamp:
- Nov 9, 2006, 6:20:47 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/cucul/import.c
r1313 r1314 68 68 * Valid values for \c format are: 69 69 * - \c "": attempt to autodetect the file format. 70 * - \c "caca": import native libcaca files. 70 71 * - \c "text": import ASCII text files. 71 72 * - \c "ansi": import ANSI files. 72 73 * - \c "utf8": import UTF-8 files with ANSI colour codes. 73 * - \c "caca": import native libcaca files. 74 * 75 * The number of bytes read is returned. If the file format is valid, but 76 * not enough data was available, 0 is returned. 74 77 * 75 78 * If an error occurs, -1 is returned and \b errno is set accordingly: … … 81 84 * \param len The size in bytes of the memory area. 82 85 * \param format A string describing the input format. 83 * \return The number of bytes read, or -1 if an error occurred. 86 * \return The number of bytes read, or 0 if there was not enough data, 87 * or -1 if an error occurred. 84 88 */ 85 89 long int cucul_import_memory(cucul_canvas_t *cv, void const *data, … … 129 133 * Valid values for \c format are: 130 134 * - \c "": attempt to autodetect the file format. 135 * - \c "caca": import native libcaca files. 131 136 * - \c "text": import ASCII text files. 132 137 * - \c "ansi": import ANSI files. 133 138 * - \c "utf8": import UTF-8 files with ANSI colour codes. 134 * - \c "caca": import native libcaca files. 139 * 140 * The number of bytes read is returned. If the file format is valid, but 141 * not enough data was available, 0 is returned. 135 142 * 136 143 * If an error occurs, -1 is returned and \b errno is set accordingly: … … 144 151 * \param filename The name of the file to load. 145 152 * \param format A string describing the input format. 146 * \return The number of bytes read, or -1 if an error occurred. 153 * \return The number of bytes read, or 0 if there was not enough data, 154 * or -1 if an error occurred. 147 155 */ 148 156 long int cucul_import_file(cucul_canvas_t *cv, char const *filename, … … 202 210 { 203 211 static char const * const list[] = 204 { 205 "", "autodetect", 206 "text", "plain text", 207 "caca", "native libcaca format", 208 "ansi", "ANSI coloured text", 209 NULL, NULL 210 }; 212 { 213 "", "autodetect", 214 "caca", "native libcaca format", 215 "text", "plain text", 216 "ansi", "ANSI coloured text", 217 "utf8", "UTF-8 files with ANSI colour codes", 218 NULL, NULL 219 }; 211 220 212 221 return list; … … 221 230 { 222 231 uint8_t const *buf = (uint8_t const *)data; 223 unsigned int control_size, data_size, full_size, frames, f, n;232 unsigned int control_size, data_size, expected_size, frames, f, n; 224 233 uint16_t version, flags; 225 234 226 cucul_set_canvas_size(cv, 0, 0);227 228 235 if(size < 20) 229 goto invalid_caca;236 return 0; 230 237 231 238 if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V') … … 238 245 flags = sscanu16(buf + 18); 239 246 240 if(size !=4 + control_size + data_size)241 goto invalid_caca;247 if(size < 4 + control_size + data_size) 248 return 0; 242 249 243 250 if(control_size < 16 + frames * 24) 244 251 goto invalid_caca; 245 252 246 for( full_size = 0, f = 0; f < frames; f++)253 for(expected_size = 0, f = 0; f < frames; f++) 247 254 { 248 255 unsigned int width, height, duration; … … 257 264 y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20); 258 265 259 full_size += width * height * 8;260 } 261 262 if( full_size != data_size)266 expected_size += width * height * 8; 267 } 268 269 if(expected_size != data_size) 263 270 goto invalid_caca; 264 271 265 272 /* FIXME: read all frames, not only the first one */ 273 cucul_set_canvas_size(cv, 0, 0); 266 274 cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16), 267 275 sscanu32(buf + 4 + 16 + 4)); … … 277 285 cv->curattr = sscanu32(buf + 4 + 16 + 12); 278 286 279 return size;287 return 4 + control_size + data_size; 280 288 281 289 invalid_caca:
Note: See TracChangeset
for help on using the changeset viewer.