uuid.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/uuid.h>
  6. #include <aws/common/byte_buf.h>
  7. #include <aws/common/device_random.h>
  8. #include <inttypes.h>
  9. #include <stdio.h>
  10. #define HEX_CHAR_FMT "%02" SCNx8
  11. #define UUID_FORMAT \
  12. HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT \
  13. "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT \
  14. "-" HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT
  15. #include <stdio.h>
  16. #ifdef _MSC_VER
  17. /* disables warning non const declared initializers for Microsoft compilers */
  18. # pragma warning(disable : 4204)
  19. # pragma warning(disable : 4706)
  20. /* sscanf warning */
  21. # pragma warning(disable : 4996)
  22. #endif
  23. int aws_uuid_init(struct aws_uuid *uuid) {
  24. struct aws_byte_buf buf = aws_byte_buf_from_empty_array(uuid->uuid_data, sizeof(uuid->uuid_data));
  25. return aws_device_random_buffer(&buf);
  26. }
  27. int aws_uuid_init_from_str(struct aws_uuid *uuid, const struct aws_byte_cursor *uuid_str) {
  28. AWS_ERROR_PRECONDITION(uuid_str->len >= AWS_UUID_STR_LEN - 1, AWS_ERROR_INVALID_BUFFER_SIZE);
  29. char cpy[AWS_UUID_STR_LEN] = {0};
  30. memcpy(cpy, uuid_str->ptr, AWS_UUID_STR_LEN - 1);
  31. AWS_ZERO_STRUCT(*uuid);
  32. if (16 != sscanf(
  33. cpy,
  34. UUID_FORMAT,
  35. &uuid->uuid_data[0],
  36. &uuid->uuid_data[1],
  37. &uuid->uuid_data[2],
  38. &uuid->uuid_data[3],
  39. &uuid->uuid_data[4],
  40. &uuid->uuid_data[5],
  41. &uuid->uuid_data[6],
  42. &uuid->uuid_data[7],
  43. &uuid->uuid_data[8],
  44. &uuid->uuid_data[9],
  45. &uuid->uuid_data[10],
  46. &uuid->uuid_data[11],
  47. &uuid->uuid_data[12],
  48. &uuid->uuid_data[13],
  49. &uuid->uuid_data[14],
  50. &uuid->uuid_data[15])) {
  51. return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
  52. }
  53. return AWS_OP_SUCCESS;
  54. }
  55. int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output) {
  56. size_t space_remaining = output->capacity - output->len;
  57. AWS_ERROR_PRECONDITION(space_remaining >= AWS_UUID_STR_LEN, AWS_ERROR_SHORT_BUFFER);
  58. snprintf(
  59. (char *)(output->buffer + output->len),
  60. space_remaining,
  61. UUID_FORMAT,
  62. uuid->uuid_data[0],
  63. uuid->uuid_data[1],
  64. uuid->uuid_data[2],
  65. uuid->uuid_data[3],
  66. uuid->uuid_data[4],
  67. uuid->uuid_data[5],
  68. uuid->uuid_data[6],
  69. uuid->uuid_data[7],
  70. uuid->uuid_data[8],
  71. uuid->uuid_data[9],
  72. uuid->uuid_data[10],
  73. uuid->uuid_data[11],
  74. uuid->uuid_data[12],
  75. uuid->uuid_data[13],
  76. uuid->uuid_data[14],
  77. uuid->uuid_data[15]);
  78. output->len += AWS_UUID_STR_LEN - 1;
  79. return AWS_OP_SUCCESS;
  80. }
  81. bool aws_uuid_equals(const struct aws_uuid *a, const struct aws_uuid *b) {
  82. return 0 == memcmp(a->uuid_data, b->uuid_data, sizeof(a->uuid_data));
  83. }