base64::encode

 base64::encode

Defined in

Testcases:
No testcase defined.
Source code:
    set base64_en $::base64::base64_en
    
    # Set the default wrapchar and maximum line length to match
    # the settings for MIME encoding (RFC 3548, RFC 2045). These
    # are the settings used by Trf as well. Various RFCs allow for
    # different wrapping characters and wraplengths, so these may
    # be overridden by command line options.
    set wrapchar "\n"
    set maxlen 76

    if { [llength $args] == 0 } {
        error "wrong # args: should be \"[lindex [info level 0] 0] ?-maxlen maxlen? ?-wrapchar wrapchar? string\""
    }

    set optionStrings [list "-maxlen" "-wrapchar"]
    for {set i 0} {$i < [llength $args] - 1} {incr i} {
        set arg [lindex $args $i]
        set index [lsearch -glob $optionStrings "${arg}*"]
        if { $index == -1 } {
        error "unknown option \"$arg\": must be -maxlen or -wrapchar"
        }
        incr i
        if { $i >= [llength $args] - 1 } {
        error "value for \"$arg\" missing"
        }
        set val [lindex $args $i]

        # The name of the variable to assign the value to is extracted
        # from the list of known options, all of which have an
        # associated variable of the same name as the option without
        # a leading "-". The [string range] command is used to strip
        # of the leading "-" from the name of the option.
        #
        # FRINK: nocheck
        set [string range [lindex $optionStrings $index] 1 end] $val
    }
    
    # [string is] requires Tcl8.2; this works with 8.0 too
    if {[catch {expr {$maxlen % 2}}]} {
        return -code error "expected integer but got \"$maxlen\""
    } elseif {$maxlen < 0} {
        return -code error "expected positive integer but got \"$maxlen\""
    }

    set string [lindex $args end]

    set result {}
    set state 0
    set length 0


    # Process the input bytes 3-by-3

    binary scan $string c* X

    foreach {x y z} $X {
        ADD [lindex $base64_en [expr {($x >>2) & 0x3F}]]
        if {$y != {}} {
        ADD [lindex $base64_en [expr {(($x << 4) & 0x30) | (($y >> 4) & 0xF)}]]
        if {$z != {}} {
            ADD [lindex $base64_en [expr {(($y << 2) & 0x3C) | (($z >> 6) & 0x3)}]]
            ADD [lindex $base64_en [expr {($z & 0x3F)}]]
        } else {
            set state 2
            break
        }
        } else {
        set state 1
        break
        }
    }
    if {$state == 1} {
        ADD [lindex $base64_en [expr {(($x << 4) & 0x30)}]]
        ADD =
        ADD =
    } elseif {$state == 2} {
        ADD [lindex $base64_en [expr {(($y << 2) & 0x3C)}]]
        ADD =
    }
    return $result
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: