File: base32hex_tcl.tcl

package info (click to toggle)
tcllib 1.21%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 69,456 kB
  • sloc: tcl: 266,493; ansic: 14,259; sh: 2,936; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 112; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (79 lines) | stat: -rw-r--r-- 1,945 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- tcl -*-
# This code is hereby put into the public domain.
# ### ### ### ######### ######### #########
## Overview
# Base32 encoding and decoding of small strings.

# ### ### ### ######### ######### #########
## Notes

# A binary string is split into groups of 5 bits (2^5 == 32), and each
# group is converted into a printable character as is specified in RFC
# 3548 for the extended hex encoding.

# ### ### ### ######### ######### #########
## Requisites

package require  base32::core
namespace eval ::base32::hex {}

# ### ### ### ######### ######### #########
## API & Implementation

proc ::base32::hex::tcl_encode {bitstring} {
    variable forward

    binary scan $bitstring B* bits
    set len [string length $bits]
    set rem [expr {$len % 5}]
    if {$rem} {append bits =/$rem}
    #puts "($bitstring) => <$bits>"

    return [string map $forward $bits]
}

proc ::base32::hex::tcl_decode {estring} {
    variable backward
    variable invalid

    if {![core::valid $estring $invalid msg]} {
	return -code error $msg
    }
    #puts "I<$estring>"
    #puts "M<[string map $backward $estring]>"

    return [binary format B* [string map $backward [string toupper $estring]]]
}

# ### ### ### ######### ######### #########
## Data structures

namespace eval ::base32::hex {
    namespace eval core {
	namespace import ::base32::core::define
	namespace import ::base32::core::valid
    }

    namespace export encode decode
    # Initialize the maps
    variable forward
    variable backward
    variable invalid

    core::define {
	0 0    9 9        18 I   27 R
	1 1   10 A        19 J   28 S
	2 2   11 B        20 K   29 T
	3 3   12 C        21 L   30 U
	4 4   13 D        22 M   31 V
	5 5   14 E        23 N
	6 6   15 F        24 O
	7 7        16 G   25 P
	8 8        17 H   26 Q
    } forward backward invalid ; # {}
    # puts ///$forward///
    # puts ///$backward///
}

# ### ### ### ######### ######### #########
## Ok