base64::decode (public)
base64::decode string
Defined in packages/acs-tcl/tcl/tcllib-doc-procs.tcl
Decode standard Base64 text using Tcllib. Acceptance of malformed input depends on the loaded package version and implementation.
- Parameters:
- string (required)
- Base64-encoded input.
- Returns:
- Decoded binary data.
- See Also:
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
Source code: if {[string length $string] == 0} {return ""} set base64 $::base64::base64 set output "" ; # Fix for [Bug 821126] binary scan $string c* X foreach x $X { set bits [lindex $base64 $x] if {$bits >= 0} { if {[llength [lappend nums $bits]] == 4} { foreach {v w z y} $nums break set a [expr {($v << 2) | ($w >> 4)}] set b [expr {(($w & 0xF) << 4) | ($z >> 2)}] set c [expr {(($z & 0x3) << 6) | $y}] append output [binary format ccc $a $b $c] set nums {} } } elseif {$bits == -1} { # = indicates end of data. Output whatever chars are left. # The encoding algorithm dictates that we can only have 1 or 2 # padding characters. If x=={}, we must (*) have 12 bits of input # (enough for 1 8-bit output). If x!={}, we have 18 bits of # input (enough for 2 8-bit outputs). # # (*) If we don't then the input is broken (bug 2976290). foreach {v w z} $nums break # Bug 2976290 if {$w == {}} { return -code error "Not enough data to process padding" } set a [expr {($v << 2) | (($w & 0x30) >> 4)}] if {$z == {}} { append output [binary format c $a ] } else { set b [expr {(($w & 0xF) << 4) | (($z & 0x3C) >> 2)}] append output [binary format cc $a $b] } break } else { # RFC 2045 says that line breaks and other characters not part # of the Base64 alphabet must be ignored, and that the decoder # can optionally emit a warning or reject the message. We opt # not to do so, but to just ignore the character. continue } } return $outputXQL Not present: Generic, PostgreSQL, Oracle