00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036
00037 #include "sysdep.h"
00038 #include "uuid.h"
00039 #include "uuidlib.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #define MASK_1_2 0x03
00051
00052 #define MASK_1_4 0x0f
00053
00054 #define MASK_1_6 0x3f
00055 #define MASK_3_6 0x3c
00056 #define MASK_5_6 0x30
00057
00058 #define MASK_3_8 0xfc
00059 #define MASK_5_8 0xf0
00060 #define MASK_7_8 0xc0
00061
00062 #define MASK_5_10 0x03f0
00063
00064 #define MASK_7_12 0x0fc0
00065
00066 #define MASK_13_16 0xf000
00067 #define MASK_11_16 0xfc00
00068
00069 #define MASK_13_18 0x0003f000
00070
00071 #define MASK_19_24 0x00fc0000
00072
00073 #define MASK_25_30 0x3f000000
00074
00075 #define MASK_31_32 0xc0000000
00076
00077 static char urlchars[] = "!*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 char* fn_uuid_create(char* uuid)
00088 {
00089 uuid__t _uuid;
00090
00091 uuid_create(&_uuid);
00092
00093 return _uuid_to_uuid(&_uuid, uuid);
00094 }
00095
00096
00097
00098
00099
00100 char* fn_guid_create(char* guid)
00101 {
00102 uuid__t _uuid;
00103
00104 uuid_create(&_uuid);
00105
00106 return _uuid_to_guid(&_uuid, guid);
00107 }
00108
00109
00110
00111
00112
00113 char* fn_squash_guid(char* guid, char* uuid)
00114 {
00115 uuid__t _uuid;
00116
00117 guid_to__uuid(guid, &_uuid);
00118 return _uuid_to_uuid(&_uuid, uuid);
00119 }
00120
00121
00122
00123
00124
00125 char* fn_expand_uuid(char* uuid, char* guid)
00126 {
00127 uuid__t _uuid;
00128
00129 uuid_to__uuid(uuid, &_uuid);
00130
00131 return _uuid_to_guid(&_uuid, guid);
00132 }
00133
00134
00135
00136
00137
00138 char* _uuid_to_guid(uuid__t* _uuid, char* guid)
00139 {
00140 int i, j;
00141
00142 sprintf(guid,"%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", _uuid->time_low, _uuid->time_mid,
00143 _uuid->time_hi_and_version, _uuid->clock_seq_hi_and_reserved,
00144 _uuid->clock_seq_low);
00145 for (i = 0, j=24; i < 6; i++, j+=2)
00146 sprintf(guid+j,"%2.2x", _uuid->node[i]);
00147
00148 return guid;
00149 }
00150
00151
00152
00153
00154
00155
00156 char* guid_to__uuid(char* guid, uuid__t* _uuid)
00157 {
00158 int i,j;
00159
00160 char *time_low, *time_mid, *time_hi_and_version;
00161 char *clock_seq_hi_and_reserved, *clock_seq_low;
00162 char *node[6];
00163 char *eptr;
00164 char *hi_time_low, *lo_time_low;
00165
00166 time_low = (char*) malloc(8);
00167 time_mid = (char*) malloc(4);
00168 time_hi_and_version = (char*) malloc(4);
00169 clock_seq_hi_and_reserved = (char*) malloc(2);
00170 clock_seq_low = (char*) malloc(2);
00171 for(i=0;i<6;i++) {
00172 node[i] = (char*) malloc(2);
00173 }
00174
00175 strncpy(time_low,guid, 8);
00176 strncpy(time_mid,guid+9,4);
00177 strncpy(time_hi_and_version,guid+14,4);
00178 strncpy(clock_seq_hi_and_reserved,guid+19,2);
00179 strncpy(clock_seq_low,guid+21,2);
00180 for(i=0,j=24;i<6;i++,j+=2) {
00181 strncpy(node[i],guid+j,2);
00182 }
00183 #ifdef _WINDOWS_ //Nasty hack to fix lack of strtoll()
00184 hi_time_low = (char*) malloc(4);
00185 lo_time_low = (char*) malloc(4);
00186 strncpy(hi_time_low,guid,4);
00187 strncpy(lo_time_low,guid+4,4);
00188 _uuid->time_low = strtol(hi_time_low,&eptr,16);
00189 _uuid->time_low <<= 16;
00190 _uuid->time_low |= strtol(lo_time_low,&eptr,16);
00191 free(hi_time_low);
00192 free(lo_time_low);
00193 #else
00194 _uuid->time_low = strtoll(time_low,&eptr,16);
00195 #endif
00196 _uuid->time_mid = strtol(time_mid,&eptr,16);
00197 _uuid->time_hi_and_version = strtol(time_hi_and_version,&eptr,16);
00198 _uuid->clock_seq_hi_and_reserved = strtol(clock_seq_hi_and_reserved,&eptr,16);
00199 _uuid->clock_seq_low = strtol(clock_seq_low,&eptr,16);
00200 for(i=0;i<6;i++) {
00201 _uuid->node[i] = strtol(node[i],&eptr,16);
00202 }
00203
00204 free(time_low);
00205 free(time_mid);
00206 free(time_hi_and_version);
00207 free(clock_seq_hi_and_reserved);
00208 free(clock_seq_low);
00209 for(i=0;i<6;i++) {
00210 free(node[i]);
00211 }
00212 char* chuuid = (char*) _uuid;
00213 return chuuid;
00214 }
00215
00216
00217
00218
00219
00220 char* _uuid_to_uuid(uuid__t* _uuid, char* uuid)
00221 {
00222 char sq_u[22];
00223 int i;
00224
00225 sq_u[21] = _uuid->time_low & MASK_1_6;
00226 sq_u[20] = (_uuid->time_low & MASK_7_12) >> 6;
00227 sq_u[19] = (_uuid->time_low & MASK_13_18) >> 12;
00228 sq_u[18] = (_uuid->time_low & MASK_19_24) >> 18;
00229 sq_u[17] = (_uuid->time_low & MASK_25_30) >> 24;
00230 sq_u[16] = (_uuid->time_low & MASK_31_32) >> 30;
00231
00232 sq_u[16] |= (_uuid->time_mid & MASK_1_4) << 2;
00233 sq_u[15] = (_uuid->time_mid & MASK_5_10) >> 4;
00234 sq_u[14] = (_uuid->time_mid & MASK_11_16) >> 10;
00235
00236 sq_u[13] = _uuid->time_hi_and_version & MASK_1_6;
00237 sq_u[12] = (_uuid->time_hi_and_version & MASK_7_12) >> 6;
00238 sq_u[11] = (_uuid->time_hi_and_version & MASK_13_16) >> 12;
00239
00240 sq_u[11] |= (_uuid->clock_seq_hi_and_reserved & MASK_1_2) << 4;
00241 sq_u[10] = (_uuid->clock_seq_hi_and_reserved & MASK_3_8) >> 2;
00242
00243 sq_u[9] = (_uuid->clock_seq_low & MASK_1_6);
00244 sq_u[8] = (_uuid->clock_seq_low & MASK_7_8) >> 6;
00245
00246 sq_u[8] |= (_uuid->node[5] & MASK_1_4) << 2;
00247 sq_u[7] = ((_uuid->node[5] & MASK_5_8) >> 4) | ((_uuid->node[4] & MASK_1_2) << 4);
00248 sq_u[6] = (_uuid->node[4] & MASK_3_8) >> 2;
00249 sq_u[5] = (_uuid->node[3] & MASK_1_6);
00250 sq_u[4] = ((_uuid->node[3] & MASK_7_8) >> 6) | ((_uuid->node[2] & MASK_1_4) << 2);
00251 sq_u[3] = ((_uuid->node[2] & MASK_5_8) >> 4) | ((_uuid->node[1] & MASK_1_2) << 4);
00252 sq_u[2] = ((_uuid->node[1] & MASK_3_8) >> 2);
00253 sq_u[1] = (_uuid->node[0] & MASK_1_6);
00254 sq_u[0] = (_uuid->node[0] & MASK_7_8) >> 6;
00255
00256 for (i=0; i<22; i++) {
00257 uuid[i] = urlchars[(int)sq_u[i]];
00258 }
00259
00260 return uuid;
00261 }
00262
00263
00264
00265
00266
00267 uuid__t* uuid_to__uuid(char* uuid, uuid__t* _uuid)
00268 {
00269 int i;
00270
00271 _uuid->time_low = sq_to_int(uuid[16]);
00272 _uuid->time_low <<= 6;
00273 _uuid->time_low |= sq_to_int(uuid[17]);
00274 _uuid->time_low <<= 6;
00275 _uuid->time_low |= sq_to_int(uuid[18]);
00276 _uuid->time_low <<= 6;
00277 _uuid->time_low |= sq_to_int(uuid[19]);
00278 _uuid->time_low <<= 6;
00279 _uuid->time_low |= sq_to_int(uuid[20]);
00280 _uuid->time_low <<= 6;
00281 _uuid->time_low |= sq_to_int(uuid[21]);
00282
00283 _uuid->time_mid = sq_to_int(uuid[14]);
00284 _uuid->time_mid <<= 6;
00285 _uuid->time_mid |= sq_to_int(uuid[15]);
00286 _uuid->time_mid <<= 4;
00287 _uuid->time_mid |= ((sq_to_int(uuid[16]) & MASK_3_6) >> 2);
00288
00289 _uuid->time_hi_and_version = sq_to_int(uuid[11]);
00290 _uuid->time_hi_and_version <<= 6;
00291 _uuid->time_hi_and_version |= sq_to_int(uuid[12]);
00292 _uuid->time_hi_and_version <<= 6;
00293 _uuid->time_hi_and_version |= sq_to_int(uuid[13]);
00294
00295 _uuid->clock_seq_hi_and_reserved = sq_to_int(uuid[10]);
00296 _uuid->clock_seq_hi_and_reserved <<= 2;
00297 _uuid->clock_seq_hi_and_reserved |= ((sq_to_int(uuid[11]) & MASK_5_6) >> 4);
00298
00299 _uuid->clock_seq_low = sq_to_int(uuid[8]);
00300 _uuid->clock_seq_low <<= 6;
00301 _uuid->clock_seq_low |= sq_to_int(uuid[9]);
00302
00303 _uuid->node[0] = (sq_to_int(uuid[0]) << 6);
00304 _uuid->node[0] |= sq_to_int(uuid[1]);
00305
00306 _uuid->node[1] = (sq_to_int(uuid[2]) << 2);
00307 _uuid->node[1] |= (sq_to_int(uuid[3]) >> 4);
00308
00309 _uuid->node[2] = (sq_to_int(uuid[3]) << 4);
00310 _uuid->node[2] |= (sq_to_int(uuid[4]) >> 2);
00311
00312 _uuid->node[3] = (sq_to_int(uuid[4]) << 6);
00313 _uuid->node[3] |= sq_to_int(uuid[5]);
00314
00315 _uuid->node[4] = (sq_to_int(uuid[6]) << 2);
00316 _uuid->node[4] |= (sq_to_int(uuid[7]) >> 4);
00317
00318 _uuid->node[5] = (sq_to_int(uuid[7]) << 4);
00319 _uuid->node[5] |= (sq_to_int(uuid[8]) >> 2);
00320
00321 return _uuid;
00322 }
00323
00324 int sq_to_int(char i)
00325 {
00326 int range=0;
00327
00328 if (i=='!') range=1;
00329 if (i=='*') range=2;
00330 if (i>='0' && i<='9') range=3;
00331 if (i>='A' && i<='Z') range=4;
00332 if (i>='a' && i<='z') range=5;
00333
00334 switch(range) {
00335 case 1 : return 0;
00336 break;
00337 case 2 : return 1;
00338 break;
00339 case 3 : return (int) i-46;
00340 break;
00341 case 4 : return (int) i-53;
00342 break;
00343 case 5 : return (int) i-59;
00344 break;
00345 case 0 :
00346 default : return -1;
00347 break;
00348 }
00349 }