base32_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.*/
00013 
00014 /*  ### ### ### ######### ######### #########*/
00015 /*  Requisites*/
00016 
00017 package require  base32::core
00018 namespace ::base32 {}
00019 
00020 /*  ### ### ### ######### ######### #########*/
00021 /*  API & Implementation*/
00022 
00023 ret  ::base32::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::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 {
00052     /*  Initialize the maps*/
00053     variable forward
00054     variable backward
00055     variable invalid
00056 
00057     core::define {
00058     0 A    9 J   18 S   27 3
00059     1 B   10 K   19 T   28 4
00060     2 C   11 L   20 U   29 5
00061     3 D   12 M   21 V   30 6
00062     4 E   13 N   22 W   31 7
00063     5 F   14 O   23 X
00064     6 G   15 P   24 Y
00065     7 H   16 Q   25 Z
00066     8 I   17 R   26 2
00067     } forward backward invalid ; /*  {}*/
00068     /*  puts ///$forward///*/
00069     /*  puts ///$backward///*/
00070 }
00071 
00072 /*  ### ### ### ######### ######### #########*/
00073 /*  Ok*/
00074 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1