hexio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Temporaly use displayhex and parsehex from LGPL libbitmask
  3. * Start of libbitmask code
  4. */
  5. /*
  6. * bitmask user library implementation.
  7. *
  8. * Copyright (c) 2004-2006 Silicon Graphics, Inc. All rights reserved.
  9. *
  10. * Paul Jackson <pj@sgi.com>
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include "bit_array.h"
  32. #include "hexio.h"
  33. /*
  34. * Write hex word representation of bmp to buf, 32 bits per
  35. * comma-separated, zero-filled hex word. Do not write more
  36. * than buflen chars to buf.
  37. *
  38. * Return number of chars that would have been written
  39. * if buf were large enough.
  40. */
  41. const char *nexttoken(const char *q, int sep)
  42. {
  43. if (q)
  44. q = strchr(q, sep);
  45. if (q)
  46. q++;
  47. return q;
  48. }
  49. char _getbit(const BIT_ARRAY* bitarr, bit_index_t b)
  50. {
  51. if (b >= bitarr->num_of_bits)
  52. return 0;
  53. return bit_array_get_bit(bitarr, b);
  54. }
  55. int bitmask_displayhex(char *buf, int buflen, const BIT_ARRAY *bmp)
  56. {
  57. int chunk;
  58. int cnt = 0;
  59. const char *sep = "";
  60. if (buflen < 1)
  61. return 0;
  62. buf[0] = 0;
  63. for (chunk = howmany(bmp->num_of_bits, HEXCHUNKSZ); chunk >= 0; chunk--) {
  64. uint32_t val = 0;
  65. int bit;
  66. for (bit = HEXCHUNKSZ - 1; bit >= 0; bit--)
  67. val = val << 1 | _getbit(bmp, chunk * HEXCHUNKSZ + bit);
  68. if (val != 0 || cnt != 0 || chunk == 0 )
  69. {
  70. cnt += snprintf(buf + cnt, max(buflen - cnt, 0), "%s%0*x",
  71. sep, HEXCHARSZ, val);
  72. sep = ",";
  73. }
  74. }
  75. return cnt;
  76. }
  77. /*
  78. * Parse hex word representation in buf to bmp.
  79. *
  80. * Returns -1 on error, leaving unspecified results in bmp.
  81. */
  82. int bitmask_parsehex(const char *buf, BIT_ARRAY *bmp)
  83. {
  84. const char *p, *q;
  85. int nchunks = 0, chunk;
  86. bit_array_clear_all(bmp);
  87. q = buf;
  88. while (p = q, q = nexttoken(q, ','), p)
  89. nchunks++;
  90. chunk = nchunks - 1;
  91. q = buf;
  92. while (p = q, q = nexttoken(q, ','), p) {
  93. uint32_t val;
  94. int bit;
  95. char *endptr;
  96. int nchars_read, nchars_unread;
  97. val = strtoul(p, &endptr, 16);
  98. /* We should have consumed 1 to 8 (HEXCHARSZ) chars */
  99. nchars_read = endptr - p;
  100. if (nchars_read < 1 || nchars_read > HEXCHARSZ)
  101. goto err;
  102. /* We should have consumed up to next comma */
  103. nchars_unread = q - endptr;
  104. if (q != NULL && nchars_unread != 1)
  105. goto err;
  106. for (bit = HEXCHUNKSZ - 1; bit >= 0; bit--) {
  107. int n = chunk * HEXCHUNKSZ + bit;
  108. if (n >= bmp->num_of_bits)
  109. goto err;
  110. bit_array_assign_bit(bmp, n, (val >> bit) & 1);
  111. }
  112. chunk--;
  113. }
  114. return 0;
  115. err:
  116. bit_array_clear_all(bmp);
  117. return -1;
  118. }