zd_util.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* zd_util.c
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  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. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * Utility program
  18. */
  19. #include "zd_def.h"
  20. #include "zd_util.h"
  21. #ifdef DEBUG
  22. static char hex(u8 v)
  23. {
  24. v &= 0xf;
  25. return (v < 10 ? '0' : 'a' - 10) + v;
  26. }
  27. static char hex_print(u8 c)
  28. {
  29. return (0x20 <= c && c < 0x7f) ? c : '.';
  30. }
  31. static void dump_line(const u8 *bytes, size_t size)
  32. {
  33. char c;
  34. size_t i;
  35. size = size <= 8 ? size : 8;
  36. printk(KERN_DEBUG "zd1211 %p ", bytes);
  37. for (i = 0; i < 8; i++) {
  38. switch (i) {
  39. case 1:
  40. case 5:
  41. c = '.';
  42. break;
  43. case 3:
  44. c = ':';
  45. break;
  46. default:
  47. c = ' ';
  48. }
  49. if (i < size) {
  50. printk("%c%c%c", hex(bytes[i] >> 4), hex(bytes[i]), c);
  51. } else {
  52. printk(" %c", c);
  53. }
  54. }
  55. for (i = 0; i < size; i++)
  56. printk("%c", hex_print(bytes[i]));
  57. printk("\n");
  58. }
  59. void zd_hexdump(const void *bytes, size_t size)
  60. {
  61. size_t i = 0;
  62. do {
  63. dump_line((u8 *)bytes + i, size-i);
  64. i += 8;
  65. } while (i < size);
  66. }
  67. #endif /* DEBUG */
  68. void *zd_tail(const void *buffer, size_t buffer_size, size_t tail_size)
  69. {
  70. if (buffer_size < tail_size)
  71. return NULL;
  72. return (u8 *)buffer + (buffer_size - tail_size);
  73. }