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.

83 lines
2.7 KiB

  1. /* Copyright (C) 2009 Codership Oy <info@codersihp.com>
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  12. */
  13. /*! @file Helper functions to deal with history UUID string representations */
  14. #include <errno.h>
  15. #include <ctype.h>
  16. #include <stdio.h>
  17. #include "wsrep_api.h"
  18. /*!
  19. * Read UUID from string
  20. * @return length of UUID string representation or -EINVAL in case of error
  21. */
  22. int
  23. wsrep_uuid_scan (const char* str, size_t str_len, wsrep_uuid_t* uuid)
  24. {
  25. unsigned int uuid_len = 0;
  26. unsigned int uuid_offt = 0;
  27. while (uuid_len + 1 < str_len) {
  28. /* We are skipping potential '-' after uuid_offt == 4, 6, 8, 10
  29. * which means
  30. * (uuid_offt >> 1) == 2, 3, 4, 5,
  31. * which in turn means
  32. * (uuid_offt >> 1) - 2 <= 3
  33. * since it is always >= 0, because uuid_offt is unsigned */
  34. if (((uuid_offt >> 1) - 2) <= 3 && str[uuid_len] == '-') {
  35. // skip dashes after 4th, 6th, 8th and 10th positions
  36. uuid_len += 1;
  37. continue;
  38. }
  39. if (isxdigit(str[uuid_len]) && isxdigit(str[uuid_len + 1])) {
  40. // got hex digit, scan another byte to uuid, increment uuid_offt
  41. sscanf (str + uuid_len, "%2hhx", uuid->data + uuid_offt);
  42. uuid_len += 2;
  43. uuid_offt += 1;
  44. if (sizeof (uuid->data) == uuid_offt)
  45. return uuid_len;
  46. }
  47. else {
  48. break;
  49. }
  50. }
  51. *uuid = WSREP_UUID_UNDEFINED;
  52. return -EINVAL;
  53. }
  54. /*!
  55. * Write UUID to string
  56. * @return length of UUID string representation or -EMSGSIZE if string is too
  57. * short
  58. */
  59. int
  60. wsrep_uuid_print (const wsrep_uuid_t* uuid, char* str, size_t str_len)
  61. {
  62. if (str_len > 36) {
  63. const unsigned char* u = uuid->data;
  64. return snprintf(str, str_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
  65. "%02x%02x-%02x%02x%02x%02x%02x%02x",
  66. u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
  67. u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15]);
  68. }
  69. else {
  70. return -EMSGSIZE;
  71. }
  72. }