The Pubnub C-core library. It's home is on https://github.com/pubnub/c_core, this is a copy

Dependents:   Pubnub_c_core_mbed2_pal Pubnub_c_core_mbed2_pal Pubnub_c_core_mbed2_pal2

Committer:
sveljko
Date:
Fri Nov 11 22:44:58 2016 +0000
Revision:
1:929314a174af
Added the UUID module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sveljko 1:929314a174af 1 /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
sveljko 1:929314a174af 2 #include "pubnub_config.h"
sveljko 1:929314a174af 3 #include "pubnub_internal.h"
sveljko 1:929314a174af 4 #include "pubnub_generate_uuid.h"
sveljko 1:929314a174af 5
sveljko 1:929314a174af 6 #include "pubnub_assert.h"
sveljko 1:929314a174af 7
sveljko 1:929314a174af 8 #include <string.h>
sveljko 1:929314a174af 9 #include <stdio.h>
sveljko 1:929314a174af 10
sveljko 1:929314a174af 11 #if PUBNUB_HAVE_MD5
sveljko 1:929314a174af 12 #include "md5.h"
sveljko 1:929314a174af 13 #endif
sveljko 1:929314a174af 14
sveljko 1:929314a174af 15 #if PUBNUB_HAVE_SHA1
sveljko 1:929314a174af 16 #include "sha1.h"
sveljko 1:929314a174af 17 #endif
sveljko 1:929314a174af 18
sveljko 1:929314a174af 19
sveljko 1:929314a174af 20 /** Here we provide functions that are not dependent on the platform */
sveljko 1:929314a174af 21
sveljko 1:929314a174af 22 struct Pubnub_UUID_decomposed {
sveljko 1:929314a174af 23 uint32_t time_low;
sveljko 1:929314a174af 24 uint16_t time_mid;
sveljko 1:929314a174af 25 uint16_t time_hi_and_version;
sveljko 1:929314a174af 26 uint8_t clock_seq_hi_and_reserved;
sveljko 1:929314a174af 27 uint8_t clock_seq_low;
sveljko 1:929314a174af 28 uint8_t node[6];
sveljko 1:929314a174af 29 };
sveljko 1:929314a174af 30
sveljko 1:929314a174af 31
sveljko 1:929314a174af 32 int pubnub_generate_uuid_v1_time(
sveljko 1:929314a174af 33 struct Pubnub_UUID *o_uuid,
sveljko 1:929314a174af 34 uint16_t *io_clock_seq,
sveljko 1:929314a174af 35 uint8_t const i_timestamp[8],
sveljko 1:929314a174af 36 uint8_t const i_node[6]
sveljko 1:929314a174af 37 )
sveljko 1:929314a174af 38 {
sveljko 1:929314a174af 39 static uint8_t s_timestamp[8];
sveljko 1:929314a174af 40 struct Pubnub_UUID_decomposed *ud = (struct Pubnub_UUID_decomposed *)o_uuid;
sveljko 1:929314a174af 41
sveljko 1:929314a174af 42 if (0 < memcmp(i_timestamp, s_timestamp, sizeof s_timestamp)) {
sveljko 1:929314a174af 43 (*io_clock_seq)++;
sveljko 1:929314a174af 44 }
sveljko 1:929314a174af 45
sveljko 1:929314a174af 46 ud->time_low = *(uint32_t*)i_timestamp;
sveljko 1:929314a174af 47 ud->time_mid = *(uint16_t*)(i_timestamp + 4);
sveljko 1:929314a174af 48 ud->time_hi_and_version = *(uint16_t*)(i_timestamp + 6);
sveljko 1:929314a174af 49 ud->time_hi_and_version |= (1 << 12);
sveljko 1:929314a174af 50 ud->clock_seq_low = *io_clock_seq & 0xFF;
sveljko 1:929314a174af 51 ud->clock_seq_hi_and_reserved = (*io_clock_seq & 0x3F00) >> 8;
sveljko 1:929314a174af 52 ud->clock_seq_hi_and_reserved |= 0x80;
sveljko 1:929314a174af 53 memcpy(&ud->node, &i_node, sizeof ud->node);
sveljko 1:929314a174af 54
sveljko 1:929314a174af 55 memcpy(s_timestamp, i_timestamp, sizeof s_timestamp);
sveljko 1:929314a174af 56
sveljko 1:929314a174af 57 return 0;
sveljko 1:929314a174af 58 }
sveljko 1:929314a174af 59
sveljko 1:929314a174af 60
sveljko 1:929314a174af 61 int pubnub_generate_uuid_v3_name_md5(
sveljko 1:929314a174af 62 struct Pubnub_UUID *uuid,
sveljko 1:929314a174af 63 struct Pubnub_UUID *nsid,
sveljko 1:929314a174af 64 void *name,
sveljko 1:929314a174af 65 unsigned namelen
sveljko 1:929314a174af 66 )
sveljko 1:929314a174af 67 {
sveljko 1:929314a174af 68 #if PUBNUB_HAVE_MD5
sveljko 1:929314a174af 69 MD5_CTX ctx;
sveljko 1:929314a174af 70
sveljko 1:929314a174af 71 MD5Init(&ctx);
sveljko 1:929314a174af 72 MD5Update(&ctx, nsid, sizeof *nsid);
sveljko 1:929314a174af 73 MD5Update(&ctx, name, namelen);
sveljko 1:929314a174af 74 MD5Final(uuid, &ctx);
sveljko 1:929314a174af 75
sveljko 1:929314a174af 76 uuid->uuid[6] &= 0x0F;
sveljko 1:929314a174af 77 uuid->uuid[6] |= 0x30;
sveljko 1:929314a174af 78 uuid->uuid[8] &= 0x3F;
sveljko 1:929314a174af 79 uuid->uuid[8] |= 0x80;
sveljko 1:929314a174af 80
sveljko 1:929314a174af 81 return 0;
sveljko 1:929314a174af 82 #else
sveljko 1:929314a174af 83 PUBNUB_UNUSED(uuid);
sveljko 1:929314a174af 84 PUBNUB_UNUSED(nsid);
sveljko 1:929314a174af 85 PUBNUB_UNUSED(name);
sveljko 1:929314a174af 86 PUBNUB_UNUSED(namelen);
sveljko 1:929314a174af 87
sveljko 1:929314a174af 88 return -1;
sveljko 1:929314a174af 89 #endif
sveljko 1:929314a174af 90 }
sveljko 1:929314a174af 91
sveljko 1:929314a174af 92
sveljko 1:929314a174af 93 int pubnub_generate_uuid_v5_name_sha1(
sveljko 1:929314a174af 94 struct Pubnub_UUID *uuid,
sveljko 1:929314a174af 95 struct Pubnub_UUID *nsid,
sveljko 1:929314a174af 96 void *name,
sveljko 1:929314a174af 97 unsigned namelen
sveljko 1:929314a174af 98 )
sveljko 1:929314a174af 99 {
sveljko 1:929314a174af 100 #if PUBNUB_HAVE_SHA1
sveljko 1:929314a174af 101 SHA1Context ctx;
sveljko 1:929314a174af 102
sveljko 1:929314a174af 103 SHA1Reset(&ctx);
sveljko 1:929314a174af 104 SHAInput(&ctx, nsid, sizeof *nsid);
sveljko 1:929314a174af 105 SHA1Input(&ctx, name, namelen);
sveljko 1:929314a174af 106 if (0 == SHA1Result(&ctx)) {
sveljko 1:929314a174af 107 return -1;
sveljko 1:929314a174af 108 }
sveljko 1:929314a174af 109 memcpy(uuid->uuid, ctx.Message_Digest, sizeof uuid->uuid);
sveljko 1:929314a174af 110
sveljko 1:929314a174af 111 uuid->uuid[6] &= 0x0F;
sveljko 1:929314a174af 112 uuid->uuid[6] |= 0x50;
sveljko 1:929314a174af 113 uuid->uuid[8] &= 0x3F;
sveljko 1:929314a174af 114 uuid->uuid[8] |= 0x80;
sveljko 1:929314a174af 115
sveljko 1:929314a174af 116 return 0;
sveljko 1:929314a174af 117 #else
sveljko 1:929314a174af 118 PUBNUB_UNUSED(uuid);
sveljko 1:929314a174af 119 PUBNUB_UNUSED(nsid);
sveljko 1:929314a174af 120 PUBNUB_UNUSED(name);
sveljko 1:929314a174af 121 PUBNUB_UNUSED(namelen);
sveljko 1:929314a174af 122 return -1;
sveljko 1:929314a174af 123 #endif
sveljko 1:929314a174af 124 }
sveljko 1:929314a174af 125
sveljko 1:929314a174af 126
sveljko 1:929314a174af 127 struct Pubnub_UUID_String pubnub_uuid_to_string(struct Pubnub_UUID const *uuid)
sveljko 1:929314a174af 128 {
sveljko 1:929314a174af 129 struct Pubnub_UUID_String rslt;
sveljko 1:929314a174af 130 struct Pubnub_UUID_decomposed const*u = (struct Pubnub_UUID_decomposed const*)uuid;
sveljko 1:929314a174af 131
sveljko 1:929314a174af 132 snprintf(rslt.uuid, sizeof rslt.uuid,
sveljko 1:929314a174af 133 "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
sveljko 1:929314a174af 134 u->time_low, u->time_mid, u->time_hi_and_version,
sveljko 1:929314a174af 135 u->clock_seq_hi_and_reserved, u->clock_seq_low,
sveljko 1:929314a174af 136 u->node[0], u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]
sveljko 1:929314a174af 137 );
sveljko 1:929314a174af 138
sveljko 1:929314a174af 139 return rslt;
sveljko 1:929314a174af 140 }
sveljko 1:929314a174af 141
sveljko 1:929314a174af 142
sveljko 1:929314a174af 143 int pubnub_uuid_compare(struct Pubnub_UUID const *left, struct Pubnub_UUID const *right)
sveljko 1:929314a174af 144 {
sveljko 1:929314a174af 145 /* maybe this is not really the greatest way to compare, but it works...*/
sveljko 1:929314a174af 146 return memcmp(left, right, sizeof *left);
sveljko 1:929314a174af 147 }