Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Base64 extension

1 view
Skip to first unread message

Andy Moskoff

unread,
Aug 26, 1996, 3:00:00 AM8/26/96
to

I was looking for a built-in function or extension in tcl to do base64
encoding. Has anybody come across any way to do this in tcl7.5?


--
------------------------------------------------------------------------------
Andy Moskoff email: an...@wx.gtegsc.com tel: (805)497-5550
GTE Weather Systems Group 112 Lakeview Canyon Road, Thousand Oaks, CA. 91359

wi...@news.sns-felb.debis.de

unread,
Aug 27, 1996, 3:00:00 AM8/27/96
to

Andy Moskoff (an...@awds27.wx.gtegsc.com) wrote:
: I was looking for a built-in function or extension in tcl to do base64


The following code just encodes or decodes a string to/from
base64. The base64 encoding isn't split into separate lines
because the code wasn't intended to pack/unpack files. It
was written to encode/decode basic authentication for WWW.
If you want to pack/unpack large chunks, decide to write a
C extension or an external program.


Until later, Jan

--
#define OPINIONS "they are all mine - not those of debis or daimler-benz"

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================== wi...@sapserv.debis.de (Jan Wieck) #

---- cut here ----
#---------------------------------------------------------------------
# b64tools.tcl - Support procedures to encode/decode
# strings to/from base64
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# b64_init - Initialize global arrays for code lookup
#---------------------------------------------------------------------
proc b64_init {} {
upvar #0 b64_nibble2bits_table n2b
upvar #0 b64_bits2nibble_table b2n
upvar #0 b64_code2bits_table c2b
upvar #0 b64_bits2code_table b2c

#-----------------------------------------------------------
# Hex nibble to four bit conversion
#-----------------------------------------------------------
array set n2b {
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
}

#-----------------------------------------------------------
# Four bit to hex nibble conversion
#-----------------------------------------------------------
foreach n [array names n2b] {
set b2n($n2b($n)) $n
}

#-----------------------------------------------------------
# Base64 to six bits and reverse conversion
#-----------------------------------------------------------
set ct "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for {set i 0} {$i < 64} {incr i} {
set h [format %02X $i]
set b "$n2b([string index $h 0])$n2b([string index $h 1])"
set b [string range $b 2 7]
set c [string index $ct $i]

set c2b($c) $b
set b2c($b) $c
}
}

#---------------------------------------------------------------------
# b64_char2bits - Get eight bits for a character
#---------------------------------------------------------------------
proc b64_char2bits { c } {
upvar #0 b64_nibble2bits_table n2b

#-----------------------------------------------------------
# Get the hex representation of the characters ascii value
#-----------------------------------------------------------
set d 0
scan $c %c d
set h [format %02X $d]

#-----------------------------------------------------------
# Return the bits for the hex value
#-----------------------------------------------------------
return "$n2b([string index $h 0])$n2b([string index $h 1])"
}

#---------------------------------------------------------------------
# b64_bits2char - Get a character from eight bits
#---------------------------------------------------------------------
proc b64_bits2char { b } {
upvar #0 b64_bits2nibble_table b2n

#-----------------------------------------------------------
# Convert eight bits into a hex value
#-----------------------------------------------------------
set h "$b2n([string range $b 0 3])$b2n([string range $b 4 7])"

#-----------------------------------------------------------
# Return the ascii character for that value
#-----------------------------------------------------------
scan $h %x d
return [format %c $d]
}

#---------------------------------------------------------------------
# b64_encode - Encode a string to base64
#---------------------------------------------------------------------
proc b64_encode { s } {
upvar #0 b64_bits2code_table b2c

set l [string length $s]
set res ""

#-----------------------------------------------------------
# Process the string in three character chunks
#-----------------------------------------------------------
for {set i 0} {$i < $l} {incr i 3} {
set three [string range $s $i [expr $i + 2]]
set tl [string length $three]

#-----------------------------------------------------------
# Convert the characters into a six-padded bitstream
#-----------------------------------------------------------
set bits ""
for {set ti 0} {$ti < $tl} {incr ti} {
append bits [b64_char2bits [string index $three $ti]]
}
switch $tl {
1 {set bits "${bits}0000"}
2 {set bits "${bits}00"}
}

#-----------------------------------------------------------
# Append the base64 codes for the bitstream to the result
#-----------------------------------------------------------
set bl [string length $bits]
for {set bi 0} {$bi < 24} {incr bi 6} {
if {$bi < $bl} {
append res $b2c([string range $bits $bi [expr $bi + 5]])
} else {
append res "="
}
}
}

#-----------------------------------------------------------
# Thats it
#-----------------------------------------------------------
return $res
}

#---------------------------------------------------------------------
# b64_decode - Decode base64 to a string
#---------------------------------------------------------------------
proc b64_decode { c } {
upvar #0 b64_code2bits_table c2b

set l [string length $c]
set res ""

#-----------------------------------------------------------
# Process the code in four character chunks
#-----------------------------------------------------------
for {set i 0} {$i < $l} {incr i 4} {
set four [string range $c $i [expr $i + 3]]
set fl [string length $four]

#-----------------------------------------------------------
# Convert the codes into a bitstream
#-----------------------------------------------------------
set bits ""
for {set fi 0} {$fi < $fl} {incr fi} {
set fc [string index $four $fi]
if {$fc == "="} continue
if {[catch {append bits $c2b($fc)}]} {
return -code error "Illegal b64 code character '$fc'"
}
}

#-----------------------------------------------------------
# Append the ascii characters from the bitstream to the result
#-----------------------------------------------------------
set bl [string length $bits]
for {set bi 0} {$bi < $bl} {incr bi 8} {
catch {
append res [b64_bits2char [string range $bits $bi [expr $bi + 7]]]
}
}
}

#-----------------------------------------------------------
# Thats it
#-----------------------------------------------------------
return $res
}

#---------------------------------------------------------------------
# Call b64_init on source
#---------------------------------------------------------------------
b64_init

Frank Pilhofer

unread,
Aug 27, 1996, 3:00:00 AM8/27/96
to

>I was looking for a built-in function or extension in tcl to do base64
>encoding. Has anybody come across any way to do this in tcl7.5?

Well, there are tcl wrapper functions for my UUDeview library. Its
main purpose is decoding, but there are also functions to do UU/Base64
encoding. They would probably need some (C-level) work to be really
useful.
You can find the package at
http://www.uni-frankfurt.de/~fp/uudeview/

The Tcl part isn't documented yet.

Frank

--
+ Frank Pilhofer f...@informatik.uni-frankfurt.de +
| Darmstaedter Str. 22 http://www.uni-frankfurt.de/~fp/ |
| 63225 Langen, Germany RAD Host |
+---------------------------------------------------------------------------+
| "It's no use. She'll never see me as anything - but a monster" -- Beast |


Andreas Kupries

unread,
Aug 29, 1996, 3:00:00 AM8/29/96
to

In article <DwrJ8...@wlbr.iipo.gtegsc.com> an...@awds27.wx.gtegsc.com (Andy Moskoff) writes:

I was looking for a built-in function or extension in tcl to do base64
encoding. Has anybody come across any way to do this in tcl7.5?

you might want to take a look at the Tcl-BLOB extension written by me.
It supports several encodings actually, not only base64.

Locations:
<URL: ftp//ftp.kisters.de/pub/lang/tcl/blob1.1p1.tar.gz>
<URL: ftp//ftp.neosoft.com/pub/NEW/blob1.1.tar.gz>
--
Sincerely
Andreas Kupries <a...@kisters.de>

------------------------------------+------------------------------------------
Don't believe that I'm speaking | pgp-key: via keyserver
for my employer, not even if the | finger print: 3E C8 C9 6B 2A EC 05 B1
contrary is stated in message above | EE 49 15 0B 62 DC 23 06
------------------------------------+------------------------------------------

0 new messages