util.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (C) 2006 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "util.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. void dump(const char *data,
  17. size_t size,
  18. const char *description)
  19. {
  20. size_t i;
  21. char c;
  22. fprintf(stderr, "Data dump (%s, %zd bytes):",
  23. description, size);
  24. for (i = 0; i < size; i++) {
  25. c = data[i];
  26. if (i % 8 == 0) {
  27. fprintf(stderr, "\n0x%08zx: 0x%02x, ",
  28. i, c & 0xff);
  29. } else
  30. fprintf(stderr, "0x%02x, ", c & 0xff);
  31. }
  32. fprintf(stderr, "\n");
  33. }
  34. void * xmalloc(size_t size)
  35. {
  36. void *p;
  37. p = malloc(size);
  38. if (!p) {
  39. fprintf(stderr, "Out of memory\n");
  40. exit(1);
  41. }
  42. memset(p, 0, size);
  43. return p;
  44. }
  45. char * xstrdup(const char *str)
  46. {
  47. char *c;
  48. c = strdup(str);
  49. if (!c) {
  50. fprintf(stderr, "Out of memory\n");
  51. exit(1);
  52. }
  53. return c;
  54. }
  55. void * xrealloc(void *in, size_t size)
  56. {
  57. void *out;
  58. out = realloc(in, size);
  59. if (!out) {
  60. fprintf(stderr, "Out of memory\n");
  61. exit(1);
  62. }
  63. return out;
  64. }