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   493 use strict;
  4         7  
  4         98  
4 4     4   18 use warnings;
  4         8  
  4         97  
5              
6 4     4   18 use parent qw(Encode::MIME::Header);
  4         5  
  4         18  
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   312 use constant HEAD => '=?ISO-2022-JP?B?';
  4         7  
  4         240  
14 4     4   20 use constant TAIL => '?=';
  4         9  
  4         153  
15              
16 4     4   241 use Encode::CJKConstants qw(%RE);
  4         7  
  4         2951  
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 20 my $self = shift;
25 9         15 my $str = shift;
26 9 100       24 return undef unless defined $str;
27              
28 8 50       37 utf8::encode($str) if ( Encode::is_utf8($str) );
29 8         27 Encode::from_to( $str, 'utf8', 'euc-jp' );
30              
31 8         44 my ($trailing_crlf) = ( $str =~ /(\n|\r|\x0d\x0a)$/o );
32              
33 8         29 $str = _mime_unstructured_header( $str, $self->{bpl} );
34              
35 8 100       35 not $trailing_crlf and $str =~ s/(\n|\r|\x0d\x0a)$//o;
36              
37 8         27 return $str;
38             }
39              
40             sub _mime_unstructured_header {
41 8     8   18 my ( $oldheader, $bpl ) = @_;
42 8         25 my $crlf = $oldheader =~ /\n$/;
43 8         18 my ( $header, @words, @wordstmp, $i ) = ('');
44              
45 8         28 $oldheader =~ s/\s+$//;
46              
47 8         32 @wordstmp = split /\s+/, $oldheader;
48              
49 8         29 for ( $i = 0 ; $i < $#wordstmp ; $i++ ) {
50 4 50 66     27 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         19 push( @words, $wordstmp[-1] );
61              
62 8         20 for my $word (@words) {
63 12 100       45 if ( $word =~ /^[\x21-\x7E]+$/ ) {
64 6         22 $header =~ /(?:.*\n)*(.*)/;
65 6 50       21 if ( length($1) + length($word) > $bpl ) {
66 0         0 $header .= "\n $word";
67             }
68             else {
69 6         16 $header .= $word;
70             }
71             }
72             else {
73 6         17 $header = _add_encoded_word( $word, $header, $bpl );
74             }
75              
76 12         83 $header =~ /(?:.*\n)*(.*)/;
77              
78 12 50       48 if ( length($1) == $bpl ) {
79 0         0 $header .= "\n ";
80             }
81             else {
82 12         39 $header .= ' ';
83             }
84             }
85              
86 8         48 $header =~ s/\n? $//mg;
87              
88 8 100       42 $crlf ? "$header\n" : $header;
89             }
90              
91             sub _add_encoded_word {
92 6     6   17 my ( $str, $line, $bpl ) = @_;
93 6         15 my $result = '';
94              
95 6         18 while ( length($str) ) {
96 8         14 my $target = $str;
97 8         17 $str = '';
98              
99 8 100       89 if (
100             length($line) + 22 +
101             ( $target =~ /^(?:$RE{EUC_0212}|$RE{EUC_C})/o ) * 8 > $bpl )
102             {
103 2         20 $line =~ s/[ \t\n\r]*$/\n/;
104 2         5 $result .= $line;
105 2         2 $line = ' ';
106             }
107              
108 8         16 while (1) {
109 18         31 my $iso_2022_jp = $target;
110 18         57 Encode::from_to( $iso_2022_jp, 'euc-jp', 'iso-2022-jp' );
111              
112 18         77 my $encoded =
113             HEAD . MIME::Base64::encode_base64( $iso_2022_jp, '' ) . TAIL;
114              
115 18 100       50 if ( length($encoded) + length($line) > $bpl ) {
116 10         82 $target =~
117             s/($RE{EUC_0212}|$RE{EUC_KANA}|$RE{EUC_C}|$RE{ASCII})$//o;
118 10         35 $str = $1 . $str;
119             }
120             else {
121 8         15 $line .= $encoded;
122 8         29 last;
123             }
124             }
125              
126             }
127              
128 6         19 $result . $line;
129             }
130              
131             1;
132             __END__