base32_tcl.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package require base32::core
00018 namespace ::base32 {}
00019
00020
00021
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
00050
00051 namespace ::base32 {
00052
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
00069
00070
00071
00072
00073
00074