/usr/share/tcltk/tcllib1.17/base64/ascii85.tcl is in tcllib 1.17-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | # ascii85.tcl --
#
# Encode/Decode ascii85 for a string
#
# Copyright (c) Emiliano Gavilan
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
package require Tcl 8.4
namespace eval ascii85 {
namespace export encode encodefile decode
# default values for encode options
variable options
array set options [list -wrapchar \n -maxlen 76]
}
# ::ascii85::encode --
#
# Ascii85 encode a given string.
#
# Arguments:
# args ?-maxlen maxlen? ?-wrapchar wrapchar? string
#
# If maxlen is 0, the output is not wrapped.
#
# Results:
# A Ascii85 encoded version of $string, wrapped at $maxlen characters
# by $wrapchar.
proc ascii85::encode {args} {
variable options
set alen [llength $args]
if {$alen != 1 && $alen != 3 && $alen != 5} {
return -code error "wrong # args:\
should be \"[lindex [info level 0] 0]\
?-maxlen maxlen?\
?-wrapchar wrapchar? string\""
}
set data [lindex $args end]
array set opts [array get options]
array set opts [lrange $args 0 end-1]
foreach key [array names opts] {
if {[lsearch -exact [array names options] $key] == -1} {
return -code error "unknown option \"$key\":\
must be -maxlen or -wrapchar"
}
}
if {![string is integer -strict $opts(-maxlen)]
|| $opts(-maxlen) < 0} {
return -code error "expected positive integer but got\
\"$opts(-maxlen)\""
}
# perform this check early
if {[string length $data] == 0} {
return ""
}
# shorten the names
set ml $opts(-maxlen)
set wc $opts(-wrapchar)
# if maxlen is zero, don't wrap the output
if {$ml == 0} {
set wc ""
}
set encoded {}
binary scan $data c* X
set len [llength $X]
set rest [expr {$len % 4}]
set lastidx [expr {$len - $rest - 1}]
foreach {b1 b2 b3 b4} [lrange $X 0 $lastidx] {
# calculate the 32 bit value
# this is an inlined version of the [encode4bytes] proc
# included here for performance reasons
set val [expr {
( (($b1 & 0xff) << 24)
|(($b2 & 0xff) << 16)
|(($b3 & 0xff) << 8)
| ($b4 & 0xff)
) & 0xffffffff }]
if {$val == 0} {
# four \0 bytes encodes as "z" instead of "!!!!!"
append current "z"
} else {
# no magic numbers here.
# 52200625 -> 85 ** 4
# 614125 -> 85 ** 3
# 7225 -> 85 ** 2
append current [binary format ccccc \
[expr { ( $val / 52200625) + 33 }] \
[expr { (($val % 52200625) / 614125) + 33 }] \
[expr { (($val % 614125) / 7225) + 33 }] \
[expr { (($val % 7225) / 85) + 33 }] \
[expr { ( $val % 85) + 33 }]]
}
if {[string length $current] >= $ml} {
append encoded [string range $current 0 [expr {$ml - 1}]] $wc
set current [string range $current $ml end]
}
}
if { $rest } {
# there are remaining bytes.
# pad with \0 and encode not using the "z" convention.
# finally, add ($rest + 1) chars.
set val 0
foreach {b1 b2 b3 b4} [pad [lrange $X [incr lastidx] end] 4 0] break
append current [string range [encode4bytes $b1 $b2 $b3 $b4] 0 $rest]
}
append encoded [regsub -all -- ".{$ml}" $current "&$wc"]
return $encoded
}
proc ascii85::encode4bytes {b1 b2 b3 b4} {
set val [expr {
( (($b1 & 0xff) << 24)
|(($b2 & 0xff) << 16)
|(($b3 & 0xff) << 8)
| ($b4 & 0xff)
) & 0xffffffff }]
return [binary format ccccc \
[expr { ( $val / 52200625) + 33 }] \
[expr { (($val % 52200625) / 614125) + 33 }] \
[expr { (($val % 614125) / 7225) + 33 }] \
[expr { (($val % 7225) / 85) + 33 }] \
[expr { ( $val % 85) + 33 }]]
}
# ::ascii85::encodefile --
#
# Ascii85 encode the contents of a file using default values
# for maxlen and wrapchar parameters.
#
# Arguments:
# fname The name of the file to encode.
#
# Results:
# An Ascii85 encoded version of the contents of the file.
# This is a convenience command
proc ascii85::encodefile {fname} {
set fd [open $fname]
fconfigure $fd -encoding binary -translation binary
return [encode [read $fd]][close $fd]
}
# ::ascii85::decode --
#
# Ascii85 decode a given string.
#
# Arguments:
# string The string to decode.
# Leading spaces and tabs are removed, along with trailing newlines
#
# Results:
# The decoded value.
proc ascii85::decode {data} {
# get rid of leading spaces/tabs and trailing newlines
set data [string map [list \n {} \t {} { } {}] $data]
set len [string length $data]
# perform this ckeck early
if {! $len} {
return ""
}
set decoded {}
set count 0
set group [list]
binary scan $data c* X
foreach char $X {
# we must check that every char is in the allowed range
if {$char < 33 || $char > 117 } {
# "z" is an exception
if {$char == 122} {
if {$count == 0} {
# if a "z" char appears at the beggining of a group,
# it decodes as four null bytes
append decoded \x00\x00\x00\x00
continue
} else {
# if not, is an error
return -code error \
"error decoding data: \"z\" char misplaced"
}
}
# char is not in range and not a "z" at the beggining of a group
return -code error \
"error decoding data: chars outside the allowed range"
}
lappend group $char
incr count
if {$count == 5} {
# this is an inlined version of the [decode5chars] proc
# included here for performance reasons
set val [expr {
([lindex $group 0] - 33) * wide(52200625) +
([lindex $group 1] - 33) * 614125 +
([lindex $group 2] - 33) * 7225 +
([lindex $group 3] - 33) * 85 +
([lindex $group 4] - 33) }]
if {$val > 0xffffffff} {
return -code error "error decoding data: decoded group overflow"
} else {
append decoded [binary format I $val]
incr count -5
set group [list]
}
}
}
set len [llength $group]
switch -- $len {
0 {
# all input has been consumed
# do nothing
}
1 {
# a single char is a condition error, there should be at least 2
return -code error \
"error decoding data: trailing char"
}
default {
# pad with "u"s, decode and add ($len - 1) bytes
append decoded [string range \
[decode5chars [pad $group 5 122]] \
0 \
[expr {$len - 2}]]
}
}
return $decoded
}
proc ascii85::decode5chars {group} {
set val [expr {
([lindex $group 0] - 33) * wide(52200625) +
([lindex $group 1] - 33) * 614125 +
([lindex $group 2] - 33) * 7225 +
([lindex $group 3] - 33) * 85 +
([lindex $group 4] - 33) }]
if {$val > 0xffffffff} {
return -code error "error decoding data: decoded group overflow"
}
return [binary format I $val]
}
proc ascii85::pad {chars len padchar} {
while {[llength $chars] < $len} {
lappend chars $padchar
}
return $chars
}
package provide ascii85 1.0
|