Hex.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Hex.h"
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. static const uint8_t numForAscii[] =
  19. {
  20. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  21. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  22. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  23. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,99,99,99,99,99,99,
  24. 99,10,11,12,13,14,15,99,99,99,99,99,99,99,99,99,
  25. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  26. 99,10,11,12,13,14,15,99,99,99,99,99,99,99,99,99,
  27. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  28. };
  29. int Hex_encode(uint8_t* output,
  30. const uint32_t outputLength,
  31. const uint8_t* in,
  32. const uint32_t inputLength)
  33. {
  34. if (outputLength < inputLength * 2) {
  35. return Hex_TOO_BIG;
  36. } else if (outputLength > inputLength * 2) {
  37. output[inputLength * 2] = '\0';
  38. }
  39. static const char* hexEntities = "0123456789abcdef";
  40. for (uint32_t i = 0; i < inputLength; i++) {
  41. output[i * 2] = hexEntities[in[i] >> 4];
  42. output[i * 2 + 1] = hexEntities[in[i] & 15];
  43. }
  44. return inputLength * 2;
  45. }
  46. int Hex_decodeByte(const uint8_t highNibble, const uint8_t lowNibble)
  47. {
  48. if ((highNibble & 0x80) | (lowNibble & 0x80)) {
  49. return -1;
  50. }
  51. int high = numForAscii[highNibble];
  52. int low = numForAscii[lowNibble];
  53. return (high + low < 31) ? (high << 4) | low : -1;
  54. }
  55. bool Hex_isHexEntity(const uint8_t character)
  56. {
  57. return !(character & 0x80) && (numForAscii[character] < 99);
  58. }
  59. int Hex_decode(uint8_t* output,
  60. const uint32_t outLength,
  61. const uint8_t* hex,
  62. const uint32_t length)
  63. {
  64. if (length % 1) {
  65. return Hex_BAD_INPUT;
  66. } else if (outLength < (length / 2)) {
  67. return Hex_TOO_BIG;
  68. } else if (outLength > (length / 2)) {
  69. output[length / 2] = '\0';
  70. }
  71. for (uint32_t i = 0; i < length; i += 2) {
  72. int byte = Hex_decodeByte(hex[i], hex[i + 1]);
  73. if (byte == -1) {
  74. return Hex_BAD_INPUT;
  75. }
  76. output[i / 2] = (uint8_t) byte;
  77. }
  78. return length / 2;
  79. }