| File: | tvlserver/./../util/uuid.c |
| Location: | line 333, column 10 |
| Description: | The left operand to '+' is always 0 |
| 1 | /* |
| 2 | * Copyright 2000, International Business Machines Corporation and others. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This software has been released under the terms of the IBM Public |
| 6 | * License. For details, see the LICENSE file in the top-level source |
| 7 | * directory or online at http://www.openafs.org/dl/license10.html |
| 8 | */ |
| 9 | |
| 10 | /* String conversion routines have the following copyright */ |
| 11 | |
| 12 | /* |
| 13 | * Copyright (c) 2002 Kungliga Tekniska Högskolan |
| 14 | * (Royal Institute of Technology, Stockholm, Sweden). |
| 15 | * All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * |
| 21 | * 1. Redistributions of source code must retain the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer. |
| 23 | * |
| 24 | * 2. Redistributions in binary form must reproduce the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer in the |
| 26 | * documentation and/or other materials provided with the distribution. |
| 27 | * |
| 28 | * 3. Neither the name of the Institute nor the names of its contributors |
| 29 | * may be used to endorse or promote products derived from this software |
| 30 | * without specific prior written permission. |
| 31 | * |
| 32 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 33 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 34 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 35 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 36 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 38 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 40 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 41 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 42 | * SUCH DAMAGE. |
| 43 | */ |
| 44 | |
| 45 | #include <afsconfig.h> |
| 46 | #include <afs/param.h> |
| 47 | |
| 48 | #ifndef KERNEL |
| 49 | # include <roken.h> |
| 50 | |
| 51 | # ifdef AFS_NT40_ENV |
| 52 | # include <rpc.h> |
| 53 | # include <process.h> |
| 54 | # else |
| 55 | # include <net/if.h> |
| 56 | # if !defined(AFS_LINUX20_ENV) && !defined(AFS_ARM_DARWIN_ENV) |
| 57 | # include <netinet/if_ether.h> |
| 58 | # endif |
| 59 | # endif |
| 60 | |
| 61 | #include "afsutil.h" |
| 62 | |
| 63 | #else |
| 64 | # include "afs/sysincludes.h" |
| 65 | # include "afsincludes.h" |
| 66 | #endif |
| 67 | |
| 68 | typedef struct { |
| 69 | char eaddr[6]; /* 6 bytes of ethernet hardware address */ |
| 70 | } uuid_address_t, *uuid_address_p_t; |
| 71 | |
| 72 | |
| 73 | typedef struct { |
| 74 | afs_uint32 lo; |
| 75 | afs_uint32 hi; |
| 76 | } uuid_time_t, *uuid_time_p_t; |
| 77 | |
| 78 | static int uuid_get_address(uuid_address_p_t addr); |
| 79 | void uuid__get_os_time(uuid_time_t * os_time); |
| 80 | |
| 81 | /* |
| 82 | * |<------------------------- 32 bits -------------------------->| |
| 83 | * |
| 84 | * +--------------------------------------------------------------+ |
| 85 | * | low 32 bits of time | 0-3 .time_low |
| 86 | * +-------------------------------+------------------------------- |
| 87 | * | mid 16 bits of time | 4-5 .time_mid |
| 88 | * +-------+-----------------------+ |
| 89 | * | vers. | hi 12 bits of time | 6-7 .time_hi_and_version |
| 90 | * +-------+-------+---------------+ |
| 91 | * |Res| clkSeqHi | 8 .clock_seq_hi_and_reserved |
| 92 | * +---------------+ |
| 93 | * | clkSeqLow | 9 .clock_seq_low |
| 94 | * +---------------+----------...-----+ |
| 95 | * | node ID | 8-16 .node |
| 96 | * +--------------------------...-----+ |
| 97 | */ |
| 98 | |
| 99 | afsUUID afs_uuid_g_nil_uuid = { 0 }; |
| 100 | static uuid_time_t time_now, time_last; |
| 101 | static u_short uuid_time_adjust, clock_seq; |
| 102 | static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0; |
| 103 | |
| 104 | #define uuid_create_nil(uuid)memset(uuid, 0, sizeof(afsUUID)) memset(uuid, 0, sizeof(afsUUID)) |
| 105 | |
| 106 | afs_int32 |
| 107 | afs_uuid_equal(afsUUID * u1, afsUUID * u2) |
| 108 | { |
| 109 | return (memcmp(u1, u2, sizeof(afsUUID)) == 0); |
| 110 | } |
| 111 | |
| 112 | afs_int32 |
| 113 | afs_uuid_is_nil(afsUUID * u1) |
| 114 | { |
| 115 | if (!u1) |
| 116 | return 1; |
| 117 | return afs_uuid_equal(u1, &afs_uuid_g_nil_uuid); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | afs_htonuuid(afsUUID * uuidp) |
| 122 | { |
| 123 | uuidp->time_low = htonl(uuidp->time_low)(__builtin_constant_p(uuidp->time_low) ? ((((__uint32_t)(uuidp ->time_low)) >> 24) | ((((__uint32_t)(uuidp->time_low )) & (0xff << 16)) >> 8) | ((((__uint32_t)(uuidp ->time_low)) & (0xff << 8)) << 8) | (((__uint32_t )(uuidp->time_low)) << 24)) : __bswap32_var(uuidp-> time_low)); |
| 124 | uuidp->time_mid = htons(uuidp->time_mid)(__builtin_constant_p(uuidp->time_mid) ? (__uint16_t)(((__uint16_t )(uuidp->time_mid)) << 8 | ((__uint16_t)(uuidp->time_mid )) >> 8) : __bswap16_var(uuidp->time_mid)); |
| 125 | uuidp->time_hi_and_version = htons(uuidp->time_hi_and_version)(__builtin_constant_p(uuidp->time_hi_and_version) ? (__uint16_t )(((__uint16_t)(uuidp->time_hi_and_version)) << 8 | ( (__uint16_t)(uuidp->time_hi_and_version)) >> 8) : __bswap16_var (uuidp->time_hi_and_version)); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | afs_ntohuuid(afsUUID * uuidp) |
| 130 | { |
| 131 | uuidp->time_low = ntohl(uuidp->time_low)(__builtin_constant_p(uuidp->time_low) ? ((((__uint32_t)(uuidp ->time_low)) >> 24) | ((((__uint32_t)(uuidp->time_low )) & (0xff << 16)) >> 8) | ((((__uint32_t)(uuidp ->time_low)) & (0xff << 8)) << 8) | (((__uint32_t )(uuidp->time_low)) << 24)) : __bswap32_var(uuidp-> time_low)); |
| 132 | uuidp->time_mid = ntohs(uuidp->time_mid)(__builtin_constant_p(uuidp->time_mid) ? (__uint16_t)(((__uint16_t )(uuidp->time_mid)) << 8 | ((__uint16_t)(uuidp->time_mid )) >> 8) : __bswap16_var(uuidp->time_mid)); |
| 133 | uuidp->time_hi_and_version = ntohs(uuidp->time_hi_and_version)(__builtin_constant_p(uuidp->time_hi_and_version) ? (__uint16_t )(((__uint16_t)(uuidp->time_hi_and_version)) << 8 | ( (__uint16_t)(uuidp->time_hi_and_version)) >> 8) : __bswap16_var (uuidp->time_hi_and_version)); |
| 134 | } |
| 135 | |
| 136 | static u_short |
| 137 | true_random(void) |
| 138 | { |
| 139 | rand_m += 7; |
| 140 | rand_ia += 1907; |
| 141 | rand_ib += 73939; |
| 142 | if (rand_m >= 9973) |
| 143 | rand_m -= 9871; |
| 144 | if (rand_ia >= 99991) |
| 145 | rand_ia -= 89989; |
| 146 | if (rand_ib >= 224729) |
| 147 | rand_ib -= 96233; |
| 148 | rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib; |
| 149 | return (((rand_irand) >> 16) ^ (rand_irand & 0x3fff)); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | static afs_int32 |
| 154 | time_cmp(uuid_time_p_t time1, uuid_time_p_t time2) |
| 155 | { |
| 156 | if (time1->hi < time2->hi) |
| 157 | return (-1); |
| 158 | if (time1->hi > time2->hi) |
| 159 | return (1); |
| 160 | if (time1->lo < time2->lo) |
| 161 | return (-1); |
| 162 | if (time1->lo > time2->lo) |
| 163 | return (1); |
| 164 | return (0); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Converts a string UUID to binary representation. |
| 169 | */ |
| 170 | |
| 171 | #if !defined(KERNEL) && !defined(UKERNEL) |
| 172 | int |
| 173 | afsUUID_from_string(const char *str, afsUUID * uuid) |
| 174 | { |
| 175 | unsigned int time_low, time_mid, time_hi_and_version; |
| 176 | unsigned int clock_seq_hi_and_reserved, clock_seq_low; |
| 177 | unsigned int node[6]; |
| 178 | int i; |
| 179 | |
| 180 | i = sscanf(str, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x", |
| 181 | &time_low, &time_mid, &time_hi_and_version, |
| 182 | &clock_seq_hi_and_reserved, &clock_seq_low, &node[0], &node[1], |
| 183 | &node[2], &node[3], &node[4], &node[5]); |
| 184 | if (i != 11) |
| 185 | return -1; |
| 186 | |
| 187 | uuid->time_low = time_low; |
| 188 | uuid->time_mid = time_mid; |
| 189 | uuid->time_hi_and_version = time_hi_and_version; |
| 190 | uuid->clock_seq_hi_and_reserved = clock_seq_hi_and_reserved; |
| 191 | uuid->clock_seq_low = clock_seq_low; |
| 192 | |
| 193 | for (i = 0; i < 6; i++) |
| 194 | uuid->node[i] = node[i]; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Converts a UUID from binary representation to a string representation. |
| 201 | */ |
| 202 | |
| 203 | int |
| 204 | afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz) |
| 205 | { |
| 206 | snprintf(str, strsz, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x", |
| 207 | uuid->time_low, uuid->time_mid, uuid->time_hi_and_version, |
| 208 | (unsigned char)uuid->clock_seq_hi_and_reserved, |
| 209 | (unsigned char)uuid->clock_seq_low, (unsigned char)uuid->node[0], |
| 210 | (unsigned char)uuid->node[1], (unsigned char)uuid->node[2], |
| 211 | (unsigned char)uuid->node[3], (unsigned char)uuid->node[4], |
| 212 | (unsigned char)uuid->node[5]); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | afs_int32 |
| 219 | afs_uuid_create(afsUUID * uuid) |
| 220 | { |
| 221 | #ifdef AFS_NT40_ENV |
| 222 | UuidCreate((UUID *) uuid); |
| 223 | #else /* AFS_NT40_ENV */ |
| 224 | uuid_address_t eaddr; |
| 225 | afs_int32 got_no_time = 0, code; |
| 226 | |
| 227 | if (!uuid_init_done) { |
| 228 | uuid_time_t t; |
| 229 | u_short seedp[4], seed = 0; |
| 230 | rand_m = 971;; |
| 231 | rand_ia = 11113; |
| 232 | rand_ib = 104322; |
| 233 | rand_irand = 4181; |
| 234 | /* |
| 235 | * Generating our 'seed' value |
| 236 | * |
| 237 | * We start with the current time, but, since the resolution of clocks is |
| 238 | * system hardware dependent (eg. Ultrix is 10 msec.) and most likely |
| 239 | * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing |
| 240 | * all the bits together. This will have the effect of involving all of |
| 241 | * the bits in the determination of the seed value while remaining system |
| 242 | * independent. Then for good measure to ensure a unique seed when there |
| 243 | * are multiple processes creating UUID's on a system, we add in the PID. |
| 244 | */ |
| 245 | uuid__get_os_time(&t); |
| 246 | memcpy(&seedp, &t, sizeof(seedp)); |
| 247 | seed ^= seedp[0]; |
| 248 | seed ^= seedp[1]; |
| 249 | seed ^= seedp[2]; |
| 250 | seed ^= seedp[3]; |
| 251 | #if defined(KERNEL) && defined(AFS_XBSD_ENV1) |
| 252 | rand_irand += seed + (afs_uint32) curproc->p_pid; |
| 253 | #elif defined(UKERNEL) |
| 254 | rand_irand += seed + (afs_uint32) osi_getpid(); |
| 255 | #else |
| 256 | rand_irand += seed + (afs_uint32) getpid(); |
| 257 | #endif |
| 258 | uuid__get_os_time(&time_last); |
| 259 | clock_seq = true_random(); |
| 260 | #ifdef AFS_NT40_ENV |
| 261 | if (afs_winsockInit() < 0) { |
| 262 | return WSAGetLastError(); |
| 263 | } |
| 264 | #endif |
| 265 | uuid_init_done = 1; |
| 266 | } |
| 267 | if ((code = uuid_get_address(&eaddr))) |
| 268 | return code; /* get our hardware network address */ |
| 269 | do { |
| 270 | /* get the current time */ |
| 271 | uuid__get_os_time(&time_now); |
| 272 | /* |
| 273 | * check that our clock hasn't gone backwards and handle it |
| 274 | * accordingly with clock_seq |
| 275 | * check that we're not generating uuid's faster than we |
| 276 | * can accommodate with our uuid_time_adjust fudge factor |
| 277 | */ |
| 278 | if ((code = time_cmp(&time_now, &time_last)) == -1) { |
| 279 | /* A clock_seq value of 0 indicates that it hasn't been initialized. */ |
| 280 | if (clock_seq == 0) { |
| 281 | clock_seq = true_random(); |
| 282 | } |
| 283 | clock_seq = (clock_seq + 1) & 0x3fff; |
| 284 | if (clock_seq == 0) |
| 285 | clock_seq = clock_seq + 1; |
| 286 | uuid_time_adjust = 0; |
| 287 | } else if (code == 1) { |
| 288 | uuid_time_adjust = 0; |
| 289 | } else { |
| 290 | if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */ |
| 291 | got_no_time = 1; |
| 292 | else |
| 293 | uuid_time_adjust++; |
| 294 | } |
| 295 | } while (got_no_time); |
| 296 | time_last.lo = time_now.lo; |
| 297 | time_last.hi = time_now.hi; |
| 298 | if (uuid_time_adjust != 0) { |
| 299 | if (time_now.lo & 0x80000000) { |
| 300 | time_now.lo += uuid_time_adjust; |
| 301 | if (!(time_now.lo & 0x80000000)) |
| 302 | time_now.hi++; |
| 303 | } else |
| 304 | time_now.lo += uuid_time_adjust; |
| 305 | } |
| 306 | uuid->time_low = time_now.lo; |
| 307 | uuid->time_mid = time_now.hi & 0x0000ffff; |
| 308 | uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16; |
| 309 | uuid->time_hi_and_version |= (1 << 12); |
| 310 | uuid->clock_seq_low = clock_seq & 0xff; |
| 311 | uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8; |
| 312 | uuid->clock_seq_hi_and_reserved |= 0x80; |
| 313 | memcpy(uuid->node, &eaddr, sizeof(uuid_address_t)); |
| 314 | #endif /* AFS_NT40_ENV */ |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | u_short |
| 319 | afs_uuid_hash(afsUUID * uuid) |
| 320 | { |
| 321 | short c0 = 0, c1 = 0, x, y; |
| 322 | char *next_uuid = (char *)uuid; |
| 323 | |
| 324 | /* |
| 325 | * For speed lets unroll the following loop: |
| 326 | * |
| 327 | * for (i = 0; i < UUID_K_LENGTH; i++) |
| 328 | * { |
| 329 | * c0 = c0 + *next_uuid++; |
| 330 | * c1 = c1 + c0; |
| 331 | * } |
| 332 | */ |
| 333 | c0 = c0 + *next_uuid++; |
The left operand to '+' is always 0 | |
| 334 | c1 = c1 + c0; |
| 335 | c0 = c0 + *next_uuid++; |
| 336 | c1 = c1 + c0; |
| 337 | c0 = c0 + *next_uuid++; |
| 338 | c1 = c1 + c0; |
| 339 | c0 = c0 + *next_uuid++; |
| 340 | c1 = c1 + c0; |
| 341 | c0 = c0 + *next_uuid++; |
| 342 | c1 = c1 + c0; |
| 343 | c0 = c0 + *next_uuid++; |
| 344 | c1 = c1 + c0; |
| 345 | c0 = c0 + *next_uuid++; |
| 346 | c1 = c1 + c0; |
| 347 | c0 = c0 + *next_uuid++; |
| 348 | c1 = c1 + c0; |
| 349 | c0 = c0 + *next_uuid++; |
| 350 | c1 = c1 + c0; |
| 351 | c0 = c0 + *next_uuid++; |
| 352 | c1 = c1 + c0; |
| 353 | c0 = c0 + *next_uuid++; |
| 354 | c1 = c1 + c0; |
| 355 | c0 = c0 + *next_uuid++; |
| 356 | c1 = c1 + c0; |
| 357 | c0 = c0 + *next_uuid++; |
| 358 | c1 = c1 + c0; |
| 359 | c0 = c0 + *next_uuid++; |
| 360 | c1 = c1 + c0; |
| 361 | c0 = c0 + *next_uuid++; |
| 362 | c1 = c1 + c0; |
| 363 | c0 = c0 + *next_uuid++; |
| 364 | c1 = c1 + c0; |
| 365 | /* Calculate the value for "First octet" of the hash */ |
| 366 | x = -c1 % 255; |
| 367 | if (x < 0) { |
| 368 | x = x + 255; |
| 369 | } |
| 370 | /* Calculate the value for "second octet" of the hash */ |
| 371 | y = (c1 - c0) % 255; |
| 372 | if (y < 0) { |
| 373 | y = y + 255; |
| 374 | } |
| 375 | return ((y * 256) + x); |
| 376 | } |
| 377 | |
| 378 | #ifdef KERNEL |
| 379 | |
| 380 | extern struct interfaceAddr afs_cb_interface; |
| 381 | |
| 382 | static int |
| 383 | uuid_get_address(uuid_address_p_t addr) |
| 384 | { |
| 385 | memcpy(addr->eaddr, &afs_cb_interface.addr_in[0], 4); |
| 386 | addr->eaddr[4] = 0xaa; |
| 387 | addr->eaddr[5] = 0x77; |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | uuid__get_os_time(uuid_time_t * os_time) |
| 393 | { |
| 394 | osi_timeval_t tp; |
| 395 | |
| 396 | osi_GetTime(&tp); |
| 397 | os_time->hi = tp.tv_sec; |
| 398 | os_time->lo = tp.tv_usec * 10; |
| 399 | } |
| 400 | |
| 401 | #else /* KERNEL */ |
| 402 | |
| 403 | char hostName1[128] = "localhost"; |
| 404 | static int |
| 405 | uuid_get_address(uuid_address_p_t addr) |
| 406 | { |
| 407 | afs_int32 code; |
| 408 | afs_uint32 addr1; |
| 409 | struct hostent *he; |
| 410 | |
| 411 | code = gethostname(hostName1, 64); |
| 412 | if (code) { |
| 413 | printf("gethostname() failed\n"); |
| 414 | #ifdef AFS_NT40_ENV |
| 415 | return ENOENT2; |
| 416 | #else |
| 417 | return errno(* __error()); |
| 418 | #endif |
| 419 | } |
| 420 | he = gethostbyname(hostName1); |
| 421 | if (!he) { |
| 422 | printf("Can't find address for '%s'\n", hostName1); |
| 423 | #ifdef AFS_NT40_ENV |
| 424 | return ENOENT2; |
| 425 | #else |
| 426 | return errno(* __error()); |
| 427 | #endif |
| 428 | } else { |
| 429 | memcpy(&addr1, he->h_addr_list[0], 4); |
| 430 | addr1 = ntohl(addr1)(__builtin_constant_p(addr1) ? ((((__uint32_t)(addr1)) >> 24) | ((((__uint32_t)(addr1)) & (0xff << 16)) >> 8) | ((((__uint32_t)(addr1)) & (0xff << 8)) << 8) | (((__uint32_t)(addr1)) << 24)) : __bswap32_var(addr1 )); |
| 431 | memcpy(addr->eaddr, &addr1, 4); |
| 432 | addr->eaddr[4] = 0xaa; |
| 433 | addr->eaddr[5] = 0x77; |
| 434 | #ifdef UUID_DEBUG |
| 435 | printf("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n", |
| 436 | addr->eaddr[0], addr->eaddr[1], addr->eaddr[2], addr->eaddr[3], |
| 437 | addr->eaddr[4], addr->eaddr[5]); |
| 438 | #endif |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | uuid__get_os_time(uuid_time_t * os_time) |
| 445 | { |
| 446 | struct timeval tp; |
| 447 | |
| 448 | if (gettimeofday(&tp, NULL((void *)0))) { |
| 449 | perror("uuid__get_time"); |
| 450 | exit(-1); |
| 451 | } |
| 452 | os_time->hi = tp.tv_sec; |
| 453 | os_time->lo = tp.tv_usec * 10; |
| 454 | } |
| 455 | |
| 456 | #endif /* KERNEL */ |