guid_bsd.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (C) 2021-2021 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <pj/guid.h>
  19. #include <pj/assert.h>
  20. #include <pj/os.h>
  21. #include <pj/string.h>
  22. #include <uuid.h>
  23. PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH = 36;
  24. PJ_DEF(unsigned) pj_GUID_STRING_LENGTH()
  25. {
  26. return PJ_GUID_STRING_LENGTH;
  27. }
  28. PJ_DEF(pj_str_t *) pj_generate_unique_string(pj_str_t *str)
  29. {
  30. uint32_t status;
  31. uuid_t u;
  32. char *s = NULL;
  33. pj_str_t sguid;
  34. PJ_ASSERT_RETURN(str && str->ptr, NULL);
  35. PJ_CHECK_STACK();
  36. uuid_create(&u, &status);
  37. PJ_ASSERT_RETURN(status == uuid_s_ok, NULL);
  38. uuid_to_string(&u, &s, &status);
  39. PJ_ASSERT_RETURN(status == uuid_s_ok, NULL);
  40. sguid = pj_str(s);
  41. pj_strncpy(str, &sguid, PJ_GUID_STRING_LENGTH);
  42. free(s);
  43. return str;
  44. }