File Coverage

blib/lib/Encode/Unicode/UTF7.pm
Criterion Covered Total %
statement 52 59 88.1
branch 16 26 61.5
condition n/a
subroutine 9 10 90.0
pod 3 3 100.0
total 80 98 81.6


line stmt bran cond sub pod time code
1             #
2             # $Id: UTF7.pm,v 2.10 2017/06/10 17:23:50 dankogai Exp $
3             #
4             package Encode::Unicode::UTF7;
5 6     6   46 use strict;
  6         16  
  6         193  
6 6     6   37 use warnings;
  6         15  
  6         213  
7 6     6   35 use parent qw(Encode::Encoding);
  6         15  
  6         48  
8             __PACKAGE__->Define('UTF-7');
9             our $VERSION = do { my @r = ( q$Revision: 2.10 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
10 6     6   2351 use MIME::Base64;
  6         1835  
  6         473  
11 6     6   49 use Encode qw(find_encoding);
  6         19  
  6         2882  
12              
13             #
14             # Algorithms taken from Unicode::String by Gisle Aas
15             #
16              
17             our $OPTIONAL_DIRECT_CHARS = 1;
18             my $specials = quotemeta "\'(),-./:?";
19             $OPTIONAL_DIRECT_CHARS
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             my $re_asis = qr/(?:[\n\r\t\ A-Za-z0-9$specials])/;
25             my $re_encoded = qr/(?:[^\n\r\t\ A-Za-z0-9$specials])/;
26             my $e_utf16 = find_encoding("UTF-16BE");
27              
28 0     0 1 0 sub needs_lines { 1 }
29              
30             sub encode($$;$) {
31 10     10 1 96 my ( $obj, $str, $chk ) = @_;
32 10 100       59 return undef unless defined $str;
33 9         386 my $len = length($str);
34 9         46 pos($str) = 0;
35 9         46 my $bytes = substr($str, 0, 0); # to propagate taintedness
36 9         45 while ( pos($str) < $len ) {
37 4211 100       17727 if ( $str =~ /\G($re_asis+)/ogc ) {
    50          
38 2109         4713 my $octets = $1;
39 2109         5415 utf8::downgrade($octets);
40 2109         7005 $bytes .= $octets;
41             }
42             elsif ( $str =~ /\G($re_encoded+)/ogsc ) {
43 2102 50       5570 if ( $1 eq "+" ) {
44 0         0 $bytes .= "+-";
45             }
46             else {
47 2102         4088 my $s = $1;
48 2102         13001 my $base64 = encode_base64( $e_utf16->encode($s), '' );
49 2102         8157 $base64 =~ s/=+$//;
50 2102         9003 $bytes .= "+$base64-";
51             }
52             }
53             else {
54 0         0 die "This should not happen! (pos=" . pos($str) . ")";
55             }
56             }
57 9 50       29 $_[1] = '' if $chk;
58 9         108 return $bytes;
59             }
60              
61             sub decode($$;$) {
62 6     6   50 use re 'taint';
  6         18  
  6         557  
63 10     10 1 40 my ( $obj, $bytes, $chk ) = @_;
64 10 100       50 return undef unless defined $bytes;
65 9         813 my $len = length($bytes);
66 9         31 my $str = substr($bytes, 0, 0); # to propagate taintedness;
67 9         36 pos($bytes) = 0;
68 6     6   43 no warnings 'uninitialized';
  6         16  
  6         1838  
69 9         45 while ( pos($bytes) < $len ) {
70 4211 100       19658 if ( $bytes =~ /\G([^+]+)/ogc ) {
    50          
    50          
    0          
71 2109         7107 $str .= $1;
72             }
73             elsif ( $bytes =~ /\G\+-/ogc ) {
74 0         0 $str .= "+";
75             }
76             elsif ( $bytes =~ /\G\+([A-Za-z0-9+\/]+)-?/ogsc ) {
77 2102         5272 my $base64 = $1;
78 2102         5201 my $pad = length($base64) % 4;
79 2102 100       6791 $base64 .= "=" x ( 4 - $pad ) if $pad;
80 2102         17791 $str .= $e_utf16->decode( decode_base64($base64) );
81             }
82             elsif ( $bytes =~ /\G\+/ogc ) {
83 0 0       0 $^W and warn "Bad UTF7 data escape";
84 0         0 $str .= "+";
85             }
86             else {
87 0         0 die "This should not happen " . pos($bytes);
88             }
89             }
90 9 50       35 $_[1] = '' if $chk;
91 9         111 return $str;
92             }
93             1;
94             __END__