| 1 | # | ||||||||||||||||||||||
| 2 | # $Id: UTF7.pm,v 2.11 2026/10/26 01:42:18 dankogai Exp $ | ||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||
| 4 | package Encode::Unicode::UTF7; | ||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 6 | ▶ | use parent qw(Encode::Encoding); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 6 | __PACKAGE__->Define('UTF-7'); | |||||||||||||||||||||
| 9 | 6 | our $VERSION = '2.11'; | |||||||||||||||||||||
| 10 | 6 | ▶ | use MIME::Base64; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 6 | ▶ | use Encode qw(find_encoding); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||
| 14 | # Algorithms taken from Unicode::String by Gisle Aas | ||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 6 | our $OPTIONAL_DIRECT_CHARS = 1; | |||||||||||||||||||||
| 18 | 6 | my $specials = quotemeta "\'(),-./:?"; | |||||||||||||||||||||
| 19 | 6 | ▶ | $OPTIONAL_DIRECT_CHARS | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 20 | and $specials .= quotemeta "!\"#$%&*;<=>@[]^_`{|}"; | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | # \s will not work because it matches U+3000 DEOGRAPHIC SPACE | ||||||||||||||||||||||
| 23 | # We use qr/[\n\r\t\ ] instead | ||||||||||||||||||||||
| 24 | 6 | my $re_asis = qr/(?:[\n\r\t\ A-Za-z0-9$specials])/; | |||||||||||||||||||||
| 25 | 6 | my $re_encoded = qr/(?:[^\n\r\t\ A-Za-z0-9$specials])/; | |||||||||||||||||||||
| 26 | 6 | my $e_utf16 = find_encoding("UTF-16BE"); | |||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 0 | ▶ | sub needs_lines { 1 } | ||||||||||||||||||||
|
Subroutine
needs_lines: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 11 | ▶ | sub encode($$;$) { | ||||||||||||||||||||
|
Subroutine
encode: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||
| 31 | 11 | my ( $obj, $str, $chk ) = @_; | |||||||||||||||||||||
| 32 | 11 | ▶ | return undef unless defined $str; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 33 | 9 | my $len = length($str); | |||||||||||||||||||||
| 34 | 9 | pos($str) = 0; | |||||||||||||||||||||
| 35 | 9 | my $bytes = substr($str, 0, 0); # to propagate taintedness | |||||||||||||||||||||
| 36 | 9 | ▶ | while ( pos($str) < $len ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 37 | 4211 | ▶ | if ( $str =~ /\G($re_asis+)/ogc ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 38 | 2109 | my $octets = $1; | |||||||||||||||||||||
| 39 | 2109 | utf8::downgrade($octets); | |||||||||||||||||||||
| 40 | 2109 | $bytes .= $octets; | |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | elsif ( $str =~ /\G($re_encoded+)/ogsc ) { | ||||||||||||||||||||||
| 43 | 2102 | ▶ | if ( $1 eq "+" ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 44 | 0 | $bytes .= "+-"; | |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||
| 47 | 2102 | my $s = $1; | |||||||||||||||||||||
| 48 | 2102 | my $base64 = encode_base64( $e_utf16->encode($s), '' ); | |||||||||||||||||||||
| 49 | 2102 | $base64 =~ s/=+$//; | |||||||||||||||||||||
| 50 | 2102 | $bytes .= "+$base64-"; | |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||||
| 54 | 0 | die "This should not happen! (pos=" . pos($str) . ")"; | |||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | 9 | ▶ | $_[1] = '' if $chk; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 58 | 9 | return $bytes; | |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | 11 | ▶ | sub decode($$;$) { | ||||||||||||||||||||
|
Subroutine
decode: called CC=10 SCAR=24.7
Pod
documented
| |||||||||||||||||||||||
| 62 | 6 | ▶ | use re 'taint'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 63 | 11 | my ( $obj, $bytes, $chk ) = @_; | |||||||||||||||||||||
| 64 | 11 | ▶ | return undef unless defined $bytes; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 65 | 9 | my $len = length($bytes); | |||||||||||||||||||||
| 66 | 9 | my $str = substr($bytes, 0, 0); # to propagate taintedness; | |||||||||||||||||||||
| 67 | 9 | pos($bytes) = 0; | |||||||||||||||||||||
| 68 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 69 | 9 | ▶ | while ( pos($bytes) < $len ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 70 | 4211 | ▶ | if ( $bytes =~ /\G([^+]+)/ogc ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 71 | 2109 | $str .= $1; | |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | elsif ( $bytes =~ /\G\+-/ogc ) { | ||||||||||||||||||||||
| 74 | 0 | $str .= "+"; | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | elsif ( $bytes =~ /\G\+([A-Za-z0-9+\/]+)-?/ogsc ) { | ||||||||||||||||||||||
| 77 | 2102 | my $base64 = $1; | |||||||||||||||||||||
| 78 | 2102 | my $pad = length($base64) % 4; | |||||||||||||||||||||
| 79 | 2102 | ▶ | $base64 .= "=" x ( 4 - $pad ) if $pad; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 80 | 2102 | $str .= $e_utf16->decode( decode_base64($base64) ); | |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | elsif ( $bytes =~ /\G\+/ogc ) { | ||||||||||||||||||||||
| 83 | 0 | ▶ | $^W and warn "Bad UTF7 data escape"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 84 | 0 | $str .= "+"; | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||
| 87 | 0 | die "This should not happen " . pos($bytes); | |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | 9 | ▶ | $_[1] = '' if $chk; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 91 | 9 | return $str; | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | 6 | 1; | |||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||