]> jfr.im git - irc/SurrealServices/srsv.git/blob - tags/0.4.3.1-pre2/SrSv/Insp/decodeUUID.pl
cut of branches/0.4.3
[irc/SurrealServices/srsv.git] / tags / 0.4.3.1-pre2 / SrSv / Insp / decodeUUID.pl
1 #!/usr/bin/perl
2
3
4 # This file is part of SurrealServices.
5 #
6 # SurrealServices is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License version 2,
8 # as published by the Free Software Foundation.
9 #
10 # SurrealServices is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with SurrealServices; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20 =cut
21
22 THIS CODE IS alpha only, and untested. Don't just trust it blindly.
23
24 =cut
25
26 use strict;
27 use warnings;
28
29 sub isAlpha($) {
30 my ($char) = @_;
31 return ($char =~ /[A-Z]/);
32 }
33
34 sub getBase36($) {
35 my ($char) = @_;
36 if(isAlpha($char)) {
37 my $val = (ord($char) - ord('A')) + 10;
38 #print "$val\n";
39 return $val;
40 } else {
41 return int($char);
42 }
43 }
44
45 sub decodeUUID($) {
46 my ($UUID) = @_;
47 my @chars = split(//, $UUID);
48 my @sidC = @chars[0..2];
49 my @uidC = @chars[3..8];
50 my $sidN = int($sidC[0]) << (4 + (6 * 2));
51 $sidN |= getBase36($sidC[1]) << (4 + (6 * 1));
52 $sidN |= getBase36($sidC[2]) << (4 + (6 * 0));
53 my $uidN = 0;
54 foreach my $char (@uidC) {
55 #print "$char\n";
56 $uidN <<= 6;
57 $uidN |= getBase36($char);
58 }
59 return (($sidN << 48) | $uidN);
60 }
61
62 my $int = decodeUUID('751AAAAAA');
63 print "$int\n";
64 print log($int)/log(2), "\n";