mime::word_encode (private)
mime::word_encode charset method string [ args... ]
Defined in packages/acs-tcl/tcl/tcllib-doc-procs.tcl
Encode an RFC 2047 header value using a Tcllib MIME implementation helper. This non-exported helper is version-dependent.
- Parameters:
- charset (required)
- MIME charset name.
- method (required)
- base64 or quoted-printable.
- string (required)
- Data to encode.
- Returns:
- Encoded header text.
- See Also:
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
Source code: variable encodings if {![info exists encodings($charset)]} { error "unknown charset '$charset'" } if {$encodings($charset) eq {}} { error "invalid charset '$charset'" } if {$method ne "base64" && $method ne "quoted-printable"} { error "unknown method '$method', must be base64 or quoted-printable" } # default to encoded and a length that won't make the Subject header to long array set options [list -charset_encoded 1 -maxlength 66] array set options $args if {$options(-charset_encoded)} { set unencoded_string [::encoding convertfrom $charset $string] } else { set unencoded_string $string } set string_length [string length $unencoded_string] if {!$string_length} { return {} } set string_bytelength [string bytelength $unencoded_string] # the 7 is for =?, ?Q?, ?= delimiters of the encoded word set maxlength [expr {$options(-maxlength) - [string length $encodings($charset)] - 7}] switch -exact -- $method { base64 { if {$maxlength < 4} { error "maxlength $options(-maxlength) too short for chosen charset and encoding" } set count 0 set maxlength [expr {($maxlength / 4) * 3}] while {$count < $string_length} { set length 0 set enc_string {} while {($length < $maxlength) && ($count < $string_length)} { set char [string range $unencoded_string $count $count] set enc_char [::encoding convertto $charset $char] if {($length + [string length $enc_char]) > $maxlength} { set length $maxlength } else { append enc_string $enc_char incr count incr length [string length $enc_char] } } set encoded_word [string map [ list \n {}] [base64 -mode encode -- $enc_string]] append result "=?$encodings($charset)?B?$encoded_word?=\n " } # Trim off last "\n ", since the above code has the side-effect # of adding an extra "\n " to the encoded string. set result [string range $result 0 end-2] } quoted-printable { if {$maxlength < 1} { error "maxlength $options(-maxlength) too short for chosen charset and encoding" } set count 0 while {$count < $string_length} { set length 0 set encoded_word {} while {($length < $maxlength) && ($count < $string_length)} { set char [string range $unencoded_string $count $count] set enc_char [::encoding convertto $charset $char] set qp_enc_char [qp_encode $enc_char 1] set qp_enc_char_length [string length $qp_enc_char] if {$qp_enc_char_length > $maxlength} { error "maxlength $options(-maxlength) too short for chosen charset and encoding" } if {($length + [ string length $qp_enc_char]) > $maxlength} { set length $maxlength } else { append encoded_word $qp_enc_char incr count incr length [string length $qp_enc_char] } } append result "=?$encodings($charset)?Q?$encoded_word?=\n " } # Trim off last "\n ", since the above code has the side-effect # of adding an extra "\n " to the encoded string. set result [string range $result 0 end-2] } {} { # Go ahead } default { error "Can't handle content encoding \"$method\"" } } return $resultXQL Not present: Generic, PostgreSQL, Oracle