# Copyright (c) 2008 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Word.pm,v 1.1 2008/01/25 22:39:26 nagler Exp $ package Bivio::MIME::Word; use strict; use Bivio::Base 'Bivio::UNIVERSAL'; use MIME::Base64 (); # See RFC 2047 our($VERSION) = sprintf('%d.%02d', q$Revision: 1.1 $ =~ /\d+/g); sub decode { my(undef, $value) = @_; $value =~ s{(\?\=)\s*(\=\?)}{$1$2}gs; $value =~ s{\=\?([^?]*)\?([bq])\?([^?]+)\?\=}{ _strip( $1, lc($2) eq 'q' ? _decode_q($3) : MIME::Base64::decode_base64($3), ); }egisx; return $value; } sub _decode_q { my($v) = @_; $v =~ s/_/ /g; $v =~ s{=([\da-fA-F]{2})}{pack('C', hex($1))}ge; return $v; } sub _strip { my($encoding, $value) = @_; $value =~ s{[\x80-\xFF]}{\?}g unless $encoding =~ /^(ISO-8859-1|US-ASCII)$/i; $value =~ s{\x00}{\?}g; return $value; } 1;