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