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
|
# -*- tcl -*-
# # ## ### ##### ######## #############
# (C) 2009 Andreas Kupries
# @@ Meta Begin
# Package tcl::transform::base64 1
# Meta as::author {Andreas Kupries}
# Meta as::copyright 2009
# Meta as::license BSD
# Meta as::notes Possibilities for extension: Currently
# Meta as::notes the mapping between read/write and
# Meta as::notes decode/encode is fixed. Allow it to be
# Meta as::notes configured at construction time.
# Meta description Implementation of a base64
# Meta description transformation (RFC 4648). Based on Tcl
# Meta description 8.6's transformation reflection support
# Meta description (TIP 230) and binary en/decode (TIP 317).
# Meta description Exports a single command adding a new
# Meta description transformation of this type to a channel.
# Meta description One argument, the channel to extend. No
# Meta description result.
# Meta platform tcl
# Meta require tcl::transform::core
# Meta require {Tcl 8.6}
# @@ Meta End
# # ## ### ##### ######## #############
package require Tcl 8.6
package require tcl::transform::core
# # ## ### ##### ######## #############
namespace eval ::tcl::transform {}
proc ::tcl::transform::base64 {chan} {
::chan push $chan [base64::implementation new]
return
}
oo::class create ::tcl::transform::base64::implementation {
superclass tcl::transform::core ;# -> initialize, finalize, destructor
method write {c data} {
my Code encodebuf encode $data 3
}
method read {c data} {
my Code decodebuf decode $data 4
}
method flush {c} {
set data [binary encode base64 $encodebuf]
set encodebuf {}
return $data
}
method drain {c} {
set data [binary decode base64 $decodebuf]
set decodebuf {}
return $data
}
method clear {c} {
set decodebuf {}
return
}
# # ## ### ##### ######## #############
constructor {} {
set encodebuf {}
set decodebuf {}
return
}
# # ## ### ##### ######## #############
variable encodebuf decodebuf
# # ## ### ##### ######## #############
method Code {bufvar op data n} {
upvar 1 $bufvar buffer
append buffer $data
set n [my Complete $buffer $n]
if {$n < 0} {
return {}
}
set result \
[binary $op base64 \
[string range $buffer 0 $n]]
incr n
set buffer \
[string range $buffer $n end]
return $result
}
method Complete {buffer n} {
set len [string length $buffer]
return [expr {(($len / $n) * $n)-1}]
}
}
# # ## ### ##### ######## #############
package provide tcl::transform::base64 1
return
|