| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include "azure_c_shared_utility/uniqueid.h" |
| 7 |
#include "azure_c_shared_utility/xlogging.h" |
| 8 |
#include "rpc.h" |
| 9 |
|
| 10 |
MU_DEFINE_ENUM_STRINGS(UNIQUEID_RESULT, UNIQUEID_RESULT_VALUES); |
| 11 |
|
| 12 |
UNIQUEID_RESULT UniqueId_Generate(char* uid, size_t len) |
| 13 |
{ |
| 14 |
UNIQUEID_RESULT result; |
| 15 |
|
| 16 |
/* Codes_SRS_UNIQUEID_07_002: [If uid is NULL then UniqueId_Generate shall return UNIQUEID_INVALID_ARG] */ |
| 17 |
/* Codes_SRS_UNIQUEID_07_003: [If len is less then 37 then UniqueId_Generate shall return UNIQUEID_INVALID_ARG] */ |
| 18 |
if (uid == NULL || len < 37) |
| 19 |
{ |
| 20 |
result = UNIQUEID_INVALID_ARG; |
| 21 |
LogError("Buffer Size is Null or length is less then 37 bytes"); |
| 22 |
} |
| 23 |
else |
| 24 |
{ |
| 25 |
UUID uuidVal; |
| 26 |
RPC_STATUS status = UuidCreate(&uuidVal); |
| 27 |
if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) |
| 28 |
{ |
| 29 |
LogError("Unable to aquire unique Id"); |
| 30 |
result = UNIQUEID_ERROR; |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
unsigned char* randomResult; |
| 35 |
status = UuidToStringA(&uuidVal, &randomResult); |
| 36 |
if (status != RPC_S_OK) |
| 37 |
{ |
| 38 |
LogError("Unable to convert Id to string"); |
| 39 |
result = UNIQUEID_ERROR; |
| 40 |
} |
| 41 |
else |
| 42 |
{ |
| 43 |
size_t cpyLen; |
| 44 |
/* Codes_SRS_UNIQUEID_07_001: [UniqueId_Generate shall create a unique Id 36 character long string.] */ |
| 45 |
memset(uid, 0, len); |
| 46 |
cpyLen = strlen((char*)randomResult); |
| 47 |
if (cpyLen > len - 1) |
| 48 |
{ |
| 49 |
cpyLen = len - 1; |
| 50 |
} |
| 51 |
(void)memcpy(uid, randomResult, cpyLen); |
| 52 |
RpcStringFreeA(&randomResult); |
| 53 |
result = UNIQUEID_OK; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
return result; |
| 58 |
} |