File Coverage

blib/lib/Encode/MIME/Header/ISO_2022_JP.pm
Criterion Covered Total %
statement 66 69 95.6
branch 16 20 80.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 1 1 100.0
total 94 102 92.1


line stmt bran cond sub pod time code
1             package Encode::MIME::Header::ISO_2022_JP;
2              
3 4     4   478 use strict;
  4         9  
  4         113  
4 4     4   19 use warnings;
  4         7  
  4         122  
5              
6 4     4   19 use parent qw(Encode::MIME::Header);
  4         8  
  4         25  
7              
8             my $obj =
9             bless { decode_b => '1', decode_q => '1', encode => 'B', bpl => 76, Name => 'MIME-Header-ISO_2022_JP' } =>
10             __PACKAGE__;
11             Encode::define_encoding($obj, 'MIME-Header-ISO_2022_JP');
12              
13 4     4   394 use constant HEAD => '=?ISO-2022-JP?B?';
  4         10  
  4         329  
14 4     4   26 use constant TAIL => '?=';
  4         18  
  4         185  
15              
16 4     4   326 use Encode::CJKConstants qw(%RE);
  4         9  
  4         3440  
17              
18             our $VERSION = do { my @r = ( q$Revision: 1.9 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
19              
20             # I owe the below codes totally to
21             # Jcode by Dan Kogai & http://www.din.or.jp/~ohzaki/perl.htm#JP_Base64
22              
23             sub encode {
24 9     9 1 28 my $self = shift;
25 9         21 my $str = shift;
26 9 100       42 return undef unless defined $str;
27              
28 8 50       78 utf8::encode($str) if ( Encode::is_utf8($str) );
29 8         32 Encode::from_to( $str, 'utf8', 'euc-jp' );
30              
31 8         49 my ($trailing_crlf) = ( $str =~ /(\n|\r|\x0d\x0a)$/o );
32              
33 8         39 $str = _mime_unstructured_header( $str, $self->{bpl} );
34              
35 8 100       38 not $trailing_crlf and $str =~ s/(\n|\r|\x0d\x0a)$//o;
36              
37 8         25 return $str;
38             }
39              
40             sub _mime_unstructured_header {
41 8     8   22 my ( $oldheader, $bpl ) = @_;
42 8         25 my $crlf = $oldheader =~ /\n$/;
43 8         24 my ( $header, @words, @wordstmp, $i ) = ('');
44              
45 8         32 $oldheader =~ s/\s+$//;
46              
47 8         41 @wordstmp = split /\s+/, $oldheader;
48              
49 8         37 for ( $i = 0 ; $i < $#wordstmp ; $i++ ) {
50 4 50 66     37 if ( $wordstmp[$i] !~ /^[\x21-\x7E]+$/
51             and $wordstmp[ $i + 1 ] !~ /^[\x21-\x7E]+$/ )
52             {
53 0         0 $wordstmp[ $i + 1 ] = "$wordstmp[$i] $wordstmp[$i + 1]";
54             }
55             else {
56 4         17 push( @words, $wordstmp[$i] );
57             }
58             }
59              
60 8         24 push( @words, $wordstmp[-1] );
61              
62 8         25 for my $word (@words) {
63 12 100       46 if ( $word =~ /^[\x21-\x7E]+$/ ) {
64 6         21 $header =~ /(?:.*\n)*(.*)/;
65 6 50       20 if ( length($1) + length($word) > $bpl ) {
66 0         0 $header .= "\n $word";
67             }
68             else {
69 6         12 $header .= $word;
70             }
71             }
72             else {
73 6         22 $header = _add_encoded_word( $word, $header, $bpl );
74             }
75              
76 12         47 $header =~ /(?:.*\n)*(.*)/;
77              
78 12 50       42 if ( length($1) == $bpl ) {
79 0         0 $header .= "\n ";
80             }
81             else {
82 12         30 $header .= ' ';
83             }
84             }
85              
86 8         40 $header =~ s/\n? $//mg;
87              
88 8 100       37 $crlf ? "$header\n" : $header;
89             }
90              
91             sub _add_encoded_word {
92 6     6   17 my ( $str, $line, $bpl ) = @_;
93 6         13 my $result = '';
94              
95 6         24 while ( length($str) ) {
96 8         18 my $target = $str;
97 8         15 $str = '';
98              
99 8 100       101 if (
100             length($line) + 22 +
101             ( $target =~ /^(?:$RE{EUC_0212}|$RE{EUC_C})/o ) * 8 > $bpl )
102             {
103 2         22 $line =~ s/[ \t\n\r]*$/\n/;
104 2         6 $result .= $line;
105 2         4 $line = ' ';
106             }
107              
108 8         20 while (1) {
109 18         36 my $iso_2022_jp = $target;
110 18         53 Encode::from_to( $iso_2022_jp, 'euc-jp', 'iso-2022-jp' );
111              
112 18         81 my $encoded =
113             HEAD . MIME::Base64::encode_base64( $iso_2022_jp, '' ) . TAIL;
114              
115 18 100       52 if ( length($encoded) + length($line) > $bpl ) {
116 10         91 $target =~
117             s/($RE{EUC_0212}|$RE{EUC_KANA}|$RE{EUC_C}|$RE{ASCII})$//o;
118 10         33 $str = $1 . $str;
119             }
120             else {
121 8         131 $line .= $encoded;
122 8         33 last;
123             }
124             }
125              
126             }
127              
128 6         21 $result . $line;
129             }
130              
131             1;
132             __END__