You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

96 lines
2.9 KiB

  1. // VariantKey
  2. //
  3. // hex.h
  4. //
  5. // @category Libraries
  6. // @author Nicola Asuni <nicola.asuni@genomicsplc.com>
  7. // @copyright 2017-2018 GENOMICS plc
  8. // @license MIT (see LICENSE)
  9. // @link https://github.com/genomicsplc/variantkey
  10. //
  11. // LICENSE
  12. //
  13. // Copyright (c) 2017-2018 GENOMICS plc
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining a copy
  16. // of this software and associated documentation files (the "Software"), to deal
  17. // in the Software without restriction, including without limitation the rights
  18. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. // copies of the Software, and to permit persons to whom the Software is
  20. // furnished to do so, subject to the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be included in
  23. // all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  31. // THE SOFTWARE.
  32. /**
  33. * @file hex.h
  34. * @brief Utility functions to manipulate strings.
  35. *
  36. * Collection of utility functions to manipulate strings.
  37. */
  38. #ifndef ASTRING_H
  39. #define ASTRING_H
  40. #include <inttypes.h>
  41. #include <stdio.h>
  42. /** @brief Returns uint64_t hexadecimal string (16 characters).
  43. *
  44. * @param n Number to parse
  45. * @param str String buffer to be returned (it must be sized 17 bytes at least).
  46. *
  47. * @return Upon successful return, these function returns the number of characters processed
  48. * (excluding the null byte used to end output to strings).
  49. * If the buffer size is not sufficient, then the return value is the number of characters required for
  50. * buffer string, including the terminating null byte.
  51. */
  52. static inline size_t hex_uint64_t(uint64_t n, char *str)
  53. {
  54. return sprintf(str, "%016" PRIx64, n);
  55. }
  56. /** @brief Parses a 16 chars hexadecimal string and returns the code.
  57. *
  58. * @param s Hexadecimal string to parse (it must contain 16 hexadecimal characters).
  59. *
  60. * @return uint64_t unsigned integer number.
  61. */
  62. static inline uint64_t parse_hex_uint64_t(const char *s)
  63. {
  64. uint64_t v = 0;
  65. uint8_t b;
  66. size_t i;
  67. for (i = 0; i < 16; i++)
  68. {
  69. b = s[i];
  70. if (b >= 'a')
  71. {
  72. b -= ('a' - 10); // a-f
  73. }
  74. else
  75. {
  76. if (b >= 'A')
  77. {
  78. b -= ('A' - 10); // A-F
  79. }
  80. else
  81. {
  82. b -= '0'; // 0-9
  83. }
  84. }
  85. v = ((v << 4) | b);
  86. }
  87. return v;
  88. }
  89. #endif // ASTRING_H