diff --git a/caca/caca.c b/caca/caca.c index 3c96901..d437dda 100644 --- a/caca/caca.c +++ b/caca/caca.c @@ -24,9 +24,6 @@ # include # include # include -# if defined(HAVE_ERRNO_H) -# include -# endif # if defined(USE_PLUGINS) # if defined(HAVE_DLFCN_H) # include @@ -69,9 +66,7 @@ caca_display_t * caca_create_display(cucul_canvas_t * cv) if(!dp) { -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -87,9 +82,7 @@ caca_display_t * caca_create_display(cucul_canvas_t * cv) dlclose(dp->plugin); #endif free(dp); -#if defined(HAVE_ERRNO_H) - errno = ENODEV; -#endif + seterrno(ENODEV); return NULL; } @@ -100,9 +93,7 @@ caca_display_t * caca_create_display(cucul_canvas_t * cv) dlclose(dp->plugin); #endif free(dp); -#if defined(HAVE_ERRNO_H) - errno = ENODEV; -#endif + seterrno(ENODEV); return NULL; } diff --git a/caca/graphics.c b/caca/graphics.c index 641bcf2..7dc304d 100644 --- a/caca/graphics.c +++ b/caca/graphics.c @@ -18,12 +18,6 @@ #include "config.h" #include "common.h" -#if !defined(__KERNEL__) -# if defined(HAVE_ERRNO_H) -# include -# endif -#endif - #include "caca.h" #include "caca_internals.h" #include "cucul.h" @@ -45,10 +39,8 @@ int caca_set_display_title(caca_display_t *dp, char const *title) { int ret = dp->drv.set_display_title(dp, title); -#if defined(HAVE_ERRNO_H) if(ret) - errno = ENOSYS; -#endif + seterrno(ENOSYS); return ret; } @@ -195,9 +187,7 @@ int caca_set_mouse(caca_display_t *dp, int flag) { if(!dp->drv.set_mouse) { -#if defined(HAVE_ERRNO_H) - errno = ENOSYS; -#endif + seterrno(ENOSYS); return -1; } diff --git a/common.h b/common.h index daef8d4..84d817c 100644 --- a/common.h +++ b/common.h @@ -17,6 +17,7 @@ * function prototypes that are sometimes missing. */ +/* C99 types */ #if defined HAVE_INTTYPES_H && !defined __KERNEL__ # include #else @@ -32,31 +33,36 @@ typedef long int intptr_t; typedef unsigned long int uintptr_t; #endif +/* errno handling */ +#if defined HAVE_ERRNO_H && !defined __KERNEL__ +# include +static inline void seterrno(int e) { errno = e; } +static inline int geterrno(void) { return errno; } +#else +# define seterrno(x) do {} while(0) +# define geterrno(x) 0 +#endif + +/* debug messages */ #if defined DEBUG && !defined __KERNEL__ # include # include -# if defined(HAVE_ERRNO_H) -# include -# endif static inline void debug(const char *format, ...) { -# if defined(HAVE_ERRNO_H) - int saved_errno = errno; -# endif + int saved_errno = geterrno(); va_list args; va_start(args, format); fprintf(stderr, "** libcaca debug ** "); vfprintf(stderr, format, args); fprintf(stderr, "\n"); va_end(args); -# if defined(HAVE_ERRNO_H) - errno = saved_errno; -# endif + seterrno(saved_errno); } #else # define debug(format, ...) do {} while(0) #endif +/* hton16() and hton32() */ #if defined HAVE_HTONS # if defined __KERNEL__ /* Nothing to do */ @@ -101,7 +107,3 @@ static inline uint32_t hton32(uint32_t x) } #endif -#if defined __KERNEL__ -# undef HAVE_ERRNO_H -#endif - diff --git a/cucul/attr.c b/cucul/attr.c index 4648173..22a03a0 100644 --- a/cucul/attr.c +++ b/cucul/attr.c @@ -19,10 +19,6 @@ #include "config.h" #include "common.h" -#if defined(HAVE_ERRNO_H) -# include -#endif - #include "cucul.h" #include "cucul_internals.h" @@ -89,9 +85,7 @@ int cucul_set_attr(cucul_canvas_t *cv, unsigned long int attr) { if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -131,9 +125,7 @@ int cucul_put_attr(cucul_canvas_t *cv, int x, int y, unsigned long int attr) if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -177,9 +169,7 @@ int cucul_set_color_ansi(cucul_canvas_t *cv, unsigned char fg, unsigned char bg) if(fg > 0x20 || bg > 0x20) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -213,9 +203,7 @@ int cucul_set_color_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg) if(fg > 0xffff || bg > 0xffff) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } diff --git a/cucul/canvas.c b/cucul/canvas.c index 8400b3e..40a9b7e 100644 --- a/cucul/canvas.c +++ b/cucul/canvas.c @@ -24,9 +24,6 @@ # include # include # include -# if defined(HAVE_ERRNO_H) -# include -# endif # if defined(HAVE_UNISTD_H) # include # endif @@ -384,9 +381,7 @@ int cucul_blit(cucul_canvas_t *dst, int x, int y, if(mask && (src->width != mask->width || src->height != mask->height)) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -471,9 +466,7 @@ int cucul_set_canvas_boundaries(cucul_canvas_t *cv, int x, int y, if(cv->refcount) { -#if defined(HAVE_ERRNO_H) - errno = EBUSY; -#endif + seterrno(EBUSY); return -1; } diff --git a/cucul/cucul.c b/cucul/cucul.c index b552231..8894e4c 100644 --- a/cucul/cucul.c +++ b/cucul/cucul.c @@ -24,9 +24,6 @@ # include # include # include -# if defined(HAVE_ERRNO_H) -# include -# endif # include # if defined(HAVE_UNISTD_H) # include @@ -87,23 +84,17 @@ cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height) if(_cucul_set_canvas_size(cv, width, height) < 0) { -#if defined(HAVE_ERRNO_H) - int saved_errno = errno; -#endif + int saved_errno = geterrno(); free(cv->frames); free(cv); -#if defined(HAVE_ERRNO_H) - errno = saved_errno; -#endif + seterrno(saved_errno); return NULL; } return cv; nomem: -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -137,9 +128,7 @@ int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width, { if(cv->refcount) { -#if defined(HAVE_ERRNO_H) - errno = EBUSY; -#endif + seterrno(EBUSY); return -1; } @@ -192,9 +181,7 @@ int cucul_free_canvas(cucul_canvas_t *cv) if(cv->refcount) { -#if defined(HAVE_ERRNO_H) - errno = EBUSY; -#endif + seterrno(EBUSY); return -1; } @@ -262,9 +249,7 @@ int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width, new_size * sizeof(uint32_t)); if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs)) { -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return -1; } } @@ -356,9 +341,7 @@ int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width, new_size * sizeof(uint32_t)); if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs)) { -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return -1; } } diff --git a/cucul/dither.c b/cucul/dither.c index 71055f6..73756d1 100644 --- a/cucul/dither.c +++ b/cucul/dither.c @@ -26,9 +26,6 @@ # include # include # include -# if defined(HAVE_ERRNO_H) -# include -# endif #endif #include "cucul.h" @@ -271,18 +268,14 @@ cucul_dither_t *cucul_create_dither(unsigned int bpp, unsigned int w, /* Minor sanity test */ if(!w || !h || !pitch || bpp > 32 || bpp < 8) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return NULL; } d = malloc(sizeof(cucul_dither_t)); if(!d) { -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -377,9 +370,7 @@ int cucul_set_dither_palette(cucul_dither_t *d, if(d->bpp != 8) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -387,9 +378,7 @@ int cucul_set_dither_palette(cucul_dither_t *d, { if((red[i] | green[i] | blue[i] | alpha[i]) >= 0x1000) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } } @@ -448,9 +437,7 @@ int cucul_set_dither_gamma(cucul_dither_t *d, float gamma) if(gamma <= 0.0) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -520,9 +507,7 @@ int cucul_set_dither_antialias(cucul_dither_t *d, char const *str) d->antialias = 1; else { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -596,9 +581,7 @@ int cucul_set_dither_color(cucul_dither_t *d, char const *str) d->color_mode = COLOR_MODE_FULL16; else { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -675,9 +658,7 @@ int cucul_set_dither_charset(cucul_dither_t *d, char const *str) } else { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -770,9 +751,7 @@ int cucul_set_dither_mode(cucul_dither_t *d, char const *str) } else { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } diff --git a/cucul/export.c b/cucul/export.c index dbf1a9b..ded5a67 100644 --- a/cucul/export.c +++ b/cucul/export.c @@ -20,9 +20,6 @@ #include "common.h" #if !defined(__KERNEL__) -# if defined(HAVE_ERRNO_H) -# include -# endif # include # include # include @@ -117,9 +114,7 @@ void *cucul_export_memory(cucul_canvas_t *cv, char const *format, if(!strcasecmp("tga", format)) return export_tga(cv, bytes); -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return NULL; } @@ -276,8 +271,8 @@ static void *export_utf8(cucul_canvas_t *cv, unsigned long int *bytes, int cr) } /* Crop to really used size */ - debug("utf8 export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("utf8 export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -356,8 +351,8 @@ static void *export_ansi(cucul_canvas_t *cv, unsigned long int *bytes) } /* Crop to really used size */ - debug("ansi export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("ansi export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -432,8 +427,8 @@ static void *export_html(cucul_canvas_t *cv, unsigned long int *bytes) cur += sprintf(cur, "\n"); /* Crop to really used size */ - debug("html export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("html export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -527,8 +522,8 @@ static void *export_html3(cucul_canvas_t *cv, unsigned long int *bytes) cur += sprintf(cur, "\n"); /* Crop to really used size */ - debug("html3 export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("html3 export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -634,8 +629,8 @@ static void *export_irc(cucul_canvas_t *cv, unsigned long int *bytes) } /* Crop to really used size */ - debug("IRC export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("IRC export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -743,8 +738,8 @@ static void *export_ps(cucul_canvas_t *cv, unsigned long int *bytes) cur += sprintf(cur, "showpage\n"); /* Crop to really used size */ - debug("PS export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("PS export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -829,8 +824,8 @@ static void *export_svg(cucul_canvas_t *cv, unsigned long int *bytes) cur += sprintf(cur, "\n"); /* Crop to really used size */ - debug("SVG export: alloc %li bytes, realloc %li\n", - (long int)*bytes, (long int)(uintptr_t)(cur - data)); + debug("SVG export: alloc %lu bytes, realloc %lu", + (unsigned long int)*bytes, (unsigned long int)(cur - data)); *bytes = (uintptr_t)(cur - data); data = realloc(data, *bytes); @@ -848,9 +843,7 @@ static void *export_tga(cucul_canvas_t *cv, unsigned long int *bytes) fontlist = cucul_get_font_list(); if(!fontlist[0]) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return NULL; } diff --git a/cucul/font.c b/cucul/font.c index e20f4ae..ba06d12 100644 --- a/cucul/font.c +++ b/cucul/font.c @@ -22,9 +22,6 @@ # if defined(HAVE_ENDIAN_H) # include # endif -# if defined(HAVE_ERRNO_H) -# include -# endif # include # include # include @@ -126,26 +123,22 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) if(!strcasecmp(data, "Monospace Bold 12")) return cucul_load_font((char *)&monobold12_data, monobold12_size); -#if defined(HAVE_ERRNO_H) - errno = ENOENT; -#endif + seterrno(ENOENT); return NULL; } if(size < sizeof(struct font_header)) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + debug("font error: data size %i < header size %i", + size, (int)sizeof(struct font_header)); + seterrno(EINVAL); return NULL; } f = malloc(sizeof(cucul_font_t)); if(!f) { -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -167,10 +160,18 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) f->header.bpp != 2 && f->header.bpp != 1) || (f->header.flags & 1) == 0) { - free(f); -#if defined(HAVE_ERRNO_H) - errno = EINVAL; +#if defined DEBUG + if(size != 4 + f->header.control_size + f->header.data_size) + debug("font error: data size %i < expected size %i", + size, 4 + f->header.control_size + f->header.data_size); + else if(f->header.bpp != 8 && f->header.bpp != 4 && + f->header.bpp != 2 && f->header.bpp != 1) + debug("font error: invalid bpp %i", f->header.bpp); + else if((f->header.flags & 1) == 0) + debug("font error: invalid flags %.04x", f->header.flags); #endif + free(f); + seterrno(EINVAL); return NULL; } @@ -178,9 +179,7 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) if(!f->block_list) { free(f); -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -190,9 +189,7 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) { free(f->block_list); free(f); -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -209,12 +206,21 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) || (i > 0 && f->block_list[i].start < f->block_list[i - 1].stop) || f->block_list[i].index >= f->header.glyphs) { +#if defined DEBUG + if(f->block_list[i].start > f->block_list[i].stop) + debug("font error: block %i has start %i > stop %i", + i, f->block_list[i].start, f->block_list[i].stop); + else if(i > 0 && f->block_list[i].start < f->block_list[i - 1].stop) + debug("font error: block %i has start %i < previous stop %i", + f->block_list[i].start, f->block_list[i - 1].stop); + else if(f->block_list[i].index >= f->header.glyphs) + debug("font error: block %i has index >= glyph count %i", + f->block_list[i].index, f->header.glyphs); +#endif free(f->user_block_list); free(f->block_list); free(f); -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return NULL; } @@ -231,9 +237,7 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) free(f->user_block_list); free(f->block_list); free(f); -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; } @@ -252,13 +256,24 @@ cucul_font_t *cucul_load_font(void const *data, unsigned int size) + (f->glyph_list[i].width * f->glyph_list[i].height * f->header.bpp + 7) / 8 > f->header.data_size) { +#if defined DEBUG + if(f->glyph_list[i].data_offset >= f->header.data_size) + debug("font error: glyph %i has data start %i > " + "data end %i", + f->glyph_list[i].data_offset, f->header.data_size); + else if(f->glyph_list[i].data_offset + + (f->glyph_list[i].width * f->glyph_list[i].height * + f->header.bpp + 7) / 8 > f->header.data_size) + debug("font error: glyph %i has data end %i > " + "data end %i", f->glyph_list[i].data_offset + + (f->glyph_list[i].width * f->glyph_list[i].height * + f->header.bpp + 7) / 8, f->header.data_size); +#endif free(f->glyph_list); free(f->user_block_list); free(f->block_list); free(f); -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return NULL; } } diff --git a/cucul/frame.c b/cucul/frame.c index 9372684..8ab5737 100644 --- a/cucul/frame.c +++ b/cucul/frame.c @@ -22,9 +22,6 @@ # include # include # include -# if defined(HAVE_ERRNO_H) -# include -# endif #endif #include "cucul.h" @@ -66,9 +63,7 @@ int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame) { if(frame >= cv->framecount) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -160,17 +155,13 @@ int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int id) if(id >= cv->framecount) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } if(cv->framecount == 1) { -#if defined(HAVE_ERRNO_H) - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } diff --git a/cucul/import.c b/cucul/import.c index 417429d..96e0cb9 100644 --- a/cucul/import.c +++ b/cucul/import.c @@ -19,9 +19,6 @@ #include "common.h" #if !defined __KERNEL__ -# if defined HAVE_ERRNO_H -# include -# endif # include # include # include @@ -118,9 +115,7 @@ long int cucul_import_memory(cucul_canvas_t *cv, void const *data, return import_text(cv, data, len); } -#if defined HAVE_ERRNO_H - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } @@ -157,9 +152,7 @@ long int cucul_import_file(cucul_canvas_t *cv, char const *filename, char const *format) { #if defined __KERNEL__ -# if defined HAVE_ERRNO_H - errno = ENOSYS; -# endif + seterrno(ENOSYS); return -1; #else FILE *fp; @@ -178,9 +171,7 @@ long int cucul_import_file(cucul_canvas_t *cv, char const *filename, if(!data) { fclose(fp); -# if defined HAVE_ERRNO_H - errno = ENOMEM; -# endif + seterrno(ENOMEM); return -1; } @@ -236,7 +227,10 @@ static long int import_caca(cucul_canvas_t *cv, return 0; if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V') + { + debug("caca import error: expected \\xca\\xcaCV header"); goto invalid_caca; + } control_size = sscanu32(buf + 4); data_size = sscanu32(buf + 8); @@ -248,7 +242,11 @@ static long int import_caca(cucul_canvas_t *cv, return 0; if(control_size < 16 + frames * 32) + { + debug("caca import error: control size %lu < expected %lu", + (unsigned long int)control_size, 16 + frames * 32); goto invalid_caca; + } for(expected_size = 0, f = 0; f < frames; f++) { @@ -269,7 +267,11 @@ static long int import_caca(cucul_canvas_t *cv, } if(expected_size != data_size) + { + debug("caca import error: data size %lu < expected %lu", + (unsigned long int)data_size, (unsigned long int)expected_size); goto invalid_caca; + } /* FIXME: read all frames, not only the first one */ cucul_set_canvas_size(cv, 0, 0); @@ -293,9 +295,7 @@ static long int import_caca(cucul_canvas_t *cv, return 4 + control_size + data_size; invalid_caca: -#if defined HAVE_ERRNO_H - errno = EINVAL; -#endif + seterrno(EINVAL); return -1; } diff --git a/cucul/legacy.c b/cucul/legacy.c index 4b8f1df..9e8ba6e 100644 --- a/cucul/legacy.c +++ b/cucul/legacy.c @@ -20,9 +20,6 @@ #include "common.h" #if !defined(__KERNEL__) -# if defined(HAVE_ERRNO_H) -# include -# endif # include # include # include @@ -88,9 +85,7 @@ cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format) ex = malloc(sizeof(cucul_buffer_t)); if(!ex) { -#if defined(HAVE_ERRNO_H) - errno = ENOMEM; -#endif + seterrno(ENOMEM); return NULL; }