PageRenderTime 25ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/browser/utils.c

https://github.com/nexgenta/redbutton
C | 269 lines | 170 code | 47 blank | 52 comment | 66 complexity | e719db34dce0b6ec1abd38b97b3f5f58 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * utils.c
  3. */
  4. /*
  5. * Copyright (C) 2005, Simon Kilvington
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include "utils.h"
  29. /*
  30. * returns a PIX_FMT_xxx type that matches the given bits per pixel and RGB bit mask values
  31. * returns PIX_FMT_NONE if none match
  32. */
  33. enum PixelFormat
  34. find_av_pix_fmt(int bpp, unsigned long rmask, unsigned long gmask, unsigned long bmask)
  35. {
  36. enum PixelFormat fmt;
  37. fmt = PIX_FMT_NONE;
  38. switch(bpp)
  39. {
  40. case 32:
  41. if(rmask == 0xff0000 && gmask == 0xff00 && bmask == 0xff)
  42. fmt = PIX_FMT_RGBA32;
  43. break;
  44. case 24:
  45. if(rmask == 0xff0000 && gmask == 0xff00 && bmask == 0xff)
  46. fmt = PIX_FMT_RGB24;
  47. else if(rmask == 0xff && gmask == 0xff00 && bmask == 0xff0000)
  48. fmt = PIX_FMT_BGR24;
  49. break;
  50. case 16:
  51. if(rmask == 0xf800 && gmask == 0x07e0 && bmask == 0x001f)
  52. fmt = PIX_FMT_RGB565;
  53. else if(rmask == 0x7c00 && gmask == 0x03e0 && bmask == 0x001f)
  54. fmt = PIX_FMT_RGB555;
  55. break;
  56. default:
  57. break;
  58. }
  59. return fmt;
  60. }
  61. /*
  62. * returns 15 for 'f' etc
  63. */
  64. unsigned int
  65. char2hex(unsigned char c)
  66. {
  67. if(!isxdigit(c))
  68. return 0;
  69. else if(c >= '0' && c <= '9')
  70. return c - '0';
  71. else
  72. return 10 + (tolower(c) - 'a');
  73. }
  74. /*
  75. * returns the next UTF8 character in the given text
  76. * size should be the amount of data available in text
  77. * sets *used to the number of bytes in the UTF8 character
  78. * gives up if the char is more than 4 bytes long
  79. */
  80. int
  81. next_utf8(unsigned char *text, int size, int *used)
  82. {
  83. if(size >= 1 && (text[0] & 0x80) == 0)
  84. {
  85. *used = 1;
  86. return text[0];
  87. }
  88. else if(size >= 2 && (text[0] & 0xe0) == 0xc0)
  89. {
  90. *used = 2;
  91. return ((text[0] & 0x1f) << 6) + (text[1] & 0x3f);
  92. }
  93. else if(size >= 3 && (text[0] & 0xf0) == 0xe0)
  94. {
  95. *used = 3;
  96. return ((text[0] & 0x0f) << 12) + ((text[1] & 0x3f) << 6) + (text[2] & 0x3f);
  97. }
  98. else if(size >= 4 && (text[0] & 0xf8) == 0xf0)
  99. {
  100. *used = 4;
  101. return ((text[0] & 0x07) << 18) + ((text[1] & 0x3f) << 12) + ((text[2] & 0x3f) << 6) + (text[3] & 0x3f);
  102. }
  103. else if(size > 0)
  104. {
  105. /* error, return the next char */
  106. *used = 1;
  107. return text[0];
  108. }
  109. else
  110. {
  111. *used = 0;
  112. return 0;
  113. }
  114. }
  115. /*
  116. * I don't want to double the size of my code just to deal with malloc failures
  117. * if you've run out of memory you're fscked anyway, me trying to recover is not gonna help...
  118. */
  119. #ifdef DEBUG_ALLOC
  120. static int _nallocs = 0;
  121. #endif
  122. void *
  123. safe_malloc(size_t nbytes)
  124. {
  125. void *buf;
  126. #ifdef DEBUG_ALLOC
  127. _nallocs ++;
  128. fprintf(stderr, "safe_malloc: %d\n", _nallocs);
  129. #endif
  130. if((buf = malloc(nbytes)) == NULL)
  131. fatal("Out of memory");
  132. return buf;
  133. }
  134. void *
  135. safe_mallocz(size_t nbytes)
  136. {
  137. void *buf = safe_malloc(nbytes);
  138. bzero(buf, nbytes);
  139. return buf;
  140. }
  141. /*
  142. * safe_realloc(NULL, n) == safe_malloc(n)
  143. * safe_realloc(x, 0) == safe_free(x) and returns NULL
  144. */
  145. void *
  146. safe_realloc(void *oldbuf, size_t nbytes)
  147. {
  148. void *newbuf;
  149. if(nbytes == 0)
  150. {
  151. safe_free(oldbuf);
  152. return NULL;
  153. }
  154. if(oldbuf == NULL)
  155. return safe_malloc(nbytes);
  156. if((newbuf = realloc(oldbuf, nbytes)) == NULL)
  157. fatal("Out of memory");
  158. return newbuf;
  159. }
  160. /*
  161. * only calls safe_realloc if nbytes > *oldsize
  162. * updates *oldsize if it calls safe_realloc
  163. */
  164. void *
  165. safe_fast_realloc(void *oldbuf, size_t *oldsize, size_t nbytes)
  166. {
  167. if(nbytes > *oldsize)
  168. {
  169. oldbuf = safe_realloc(oldbuf, nbytes);
  170. *oldsize = nbytes;
  171. }
  172. return oldbuf;
  173. }
  174. /*
  175. * safe_free(NULL) is okay
  176. */
  177. void
  178. safe_free(void *buf)
  179. {
  180. if(buf != NULL)
  181. {
  182. free(buf);
  183. #ifdef DEBUG_ALLOC
  184. _nallocs--;
  185. fprintf(stderr, "safe_free: %d\n", _nallocs);
  186. #endif
  187. }
  188. return;
  189. }
  190. /*
  191. * safe_strdup(NULL) == NULL
  192. */
  193. char *
  194. safe_strdup(const char *src)
  195. {
  196. char *dst;
  197. if(src == NULL)
  198. return NULL;
  199. dst = (char *) safe_malloc(strlen(src) + 1);
  200. strcpy(dst, src);
  201. return dst;
  202. }
  203. void
  204. error(char *message, ...)
  205. {
  206. va_list ap;
  207. va_start(ap, message);
  208. vprintf(message, ap);
  209. printf("\n");
  210. va_end(ap);
  211. return;
  212. }
  213. void
  214. fatal(char *message, ...)
  215. {
  216. va_list ap;
  217. va_start(ap, message);
  218. vprintf(message, ap);
  219. printf("\n");
  220. va_end(ap);
  221. abort();
  222. }