uuid.c 991 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "uuid.h"
  2. #include "math/kmath.h"
  3. #ifndef UUID_QUICK_AND_DIRTY
  4. #define UUID_QUICK_AND_DIRTY
  5. #endif
  6. #ifndef UUID_QUICK_AND_DIRTY
  7. #error "Full implementation of uuid does not exist"
  8. #endif
  9. void uuid_seed(u64 seed){
  10. #ifdef UUID_QUICK_AND_DIRTY
  11. // NOTE: does nothing, for now...
  12. #endif
  13. }
  14. uuid uuid_generate(void) {
  15. uuid buf = {0};
  16. #ifdef UUID_QUICK_AND_DIRTY
  17. // NOTE: this implementation does not guarantee any form of uniqueness as it just
  18. // uses random numbers.
  19. // TODO: implement a real uuid generator.
  20. static char v[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  21. for (int i = 0; i < 36; ++i) {
  22. if (i == 8 || i == 13 || i == 18 || i == 23) {
  23. // Put a dash
  24. buf.value[i] = '-';
  25. } else {
  26. i32 offset = krandom() % 16;
  27. buf.value[i] = v[offset];
  28. }
  29. }
  30. #endif
  31. // Make sure the string is terminated.
  32. buf.value[36] = '\0';
  33. return buf;
  34. }