base32hex_tcl.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*-*/
00002 /*  This code is hereby put into the public domain.*/
00003 /*  ### ### ### ######### ######### #########*/
00004 /*  Overview*/
00005 /*  Base32 encoding and decoding of small strings.*/
00006 
00007 /*  ### ### ### ######### ######### #########*/
00008 /*  Notes*/
00009 
00010 /*  A binary string is split into groups of 5 bits (2^5 == 32), and each*/
00011 /*  group is converted into a printable character as is specified in RFC*/
00012 /*  3548 for the extended hex encoding.*/
00013 
00014 /*  ### ### ### ######### ######### #########*/
00015 /*  Requisites*/
00016 
00017 package require  base32::core
00018 namespace ::base32::hex {}
00019 
00020 /*  ### ### ### ######### ######### #########*/
00021 /*  API & Implementation*/
00022 
00023 ret  ::base32::hex::tcl_encode (type bitstring) {
00024     variable forward
00025 
00026     binary scan $bitstring B* bits
00027     set len [string length $bits]
00028     set rem [expr {$len % 5}]
00029     if {$rem} {append bits =/$rem}
00030     #puts "($bitstring) => <$bits>"
00031 
00032     return [string map $forward $bits]
00033 }
00034 
00035 ret  ::base32::hex::tcl_decode (type estring) {
00036     variable backward
00037     variable invalid
00038 
00039     if {![core::valid $estring $invalid msg]} {
00040     return -code error $msg
00041     }
00042     #puts "I<$estring>"
00043     #puts "M<[string map $backward $estring]>"
00044 
00045     return [binary format B* [string map $backward [string toupper $estring]]]
00046 }
00047 
00048 /*  ### ### ### ######### ######### #########*/
00049 /*  Data structures*/
00050 
00051 namespace ::base32::hex {
00052     namespace core {
00053     namespace import ::base32::core::define
00054     namespace import ::base32::core::valid
00055     }
00056 
00057     namespace export encode decode
00058     /*  Initialize the maps*/
00059     variable forward
00060     variable backward
00061     variable invalid
00062 
00063     core::define {
00064     0 0    9 9        18 I   27 R
00065     1 1   10 A        19 J   28 S
00066     2 2   11 B        20 K   29 T
00067     3 3   12 C        21 L   30 U
00068     4 4   13 D        22 M   31 V
00069     5 5   14 E        23 N
00070     6 6   15 F        24 O
00071     7 7        16 G   25 P
00072     8 8        17 H   26 Q
00073     } forward backward invalid ; /*  {}*/
00074     /*  puts ///$forward///*/
00075     /*  puts ///$backward///*/
00076 }
00077 
00078 /*  ### ### ### ######### ######### #########*/
00079 /*  Ok*/
00080 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1