2013-05-09 13 views
7

UTF8フラグが設定されているが、無効なUTF8バイトシーケンスが含まれているperl文字列を作成するにはどうすればよいですか?無効なUTF8 perl文字列を作成しますか?

(例えば、utf8::upgradeを呼び出したときに起こる)UTF-X変換のネイティブエンコーディングを実行せずに、perl文字列にUTF8フラグを設定する方法はありますか?

DBIドライバのバグを追跡するには、この処理が必要です。

+0

UnicodeとPerlをボニーとクライドのような - あなたの時間を盗んで、あなたに素晴らしい夜と夜を与える:) – gaussblurinc

答えて

7

エンコードの_utf8_onはまったく同じものです。

use Encode qw(_utf8_on); 

my $s = "abc\xC0def"; # String to use as raw buffer content. 
utf8::downgrade($s); # Make sure each char is stored as a byte. 
_utf8_on($s);   # Set UTF8 flag. 

(あなたが悪いスカラーを生成したいとき以外は_utf8_onを使用しないでください。)

あなたは

use Devel::Peek qw(Dump); 
Dump($s); 

出力使用してダメージ表示することができます。

SV = PV(0x24899c) at 0x4a9294 
    REFCNT = 1 
    FLAGS = (PADMY,POK,pPOK,UTF8) 
    PV = 0x24ab04 "abc\300def"\0Malformed UTF-8 character (unexpected non-continuation byte 0x64, immediately after start byte 0xc0) in subroutine entry at script.pl line 9. 
[UTF8 "abc\x{0}ef"] 
    CUR = 7 
    LEN = 12 
8

UTF8フラグを文字列のハッシュでハッキングしても、任意のバイトシーケンスを設定できます。

use Inline C; 
use Devel::Peek; 
utf8::upgrade($str = ""); 
Dump($str); 
twiddle($str, "\x{BD}\x{BE}\x{BF}\x{C0}\x{C1}\x{C2}"); 
Dump($str); 
__DATA__ 
__C__ 
/** append arbitrary bytes to a Perl scalar **/ 
void twiddle(SV *s, const char *t) 
{ 
    sv_catpv(s, t); 
} 

典型的な出力:

SV = PV(0x80029bb0) at 0x80072008 
    REFCNT = 1 
    FLAGS = (POK,pPOK,UTF8) 
    PV = 0x80155098 ""\0 [UTF8 ""] 
    CUR = 0 
    LEN = 12 
SV = PV(0x80029bb0) at 0x80072008 
    REFCNT = 1 
    FLAGS = (POK,pPOK,UTF8) 
    PV = 0x80155098 "\275\276\277\300\301\302"\0Malformed UTF-8 character (unexpected continuation byte 0xbd, with no preceding start byte) in subroutine entry at ./invalidUTF.pl line 6. 
Malformed UTF-8 character (unexpected continuation byte 0xbe, with no preceding start byte) in subroutine entry at ./invalidUTF.pl line 6. 
Malformed UTF-8 character (unexpected continuation byte 0xbf, with no preceding start byte) in subroutine entry at ./invalidUTF.pl line 6. 
Malformed UTF-8 character (unexpected non-continuation byte 0xc1, immediately after start byte 0xc0) in subroutine entry at ./invalidUTF.pl line 6. 
Malformed UTF-8 character (unexpected non-continuation byte 0x00, immediately after start byte 0xc2) in subroutine entry at ./invalidUTF.pl line 6. 
[UTF8 "\x{0}\x{0}\x{0}\x{0}\x{0}"] 
    CUR = 6 
    LEN = 12 
関連する問題