MIME-Base64-3.08 > MIME::Base64
MIME-Base64-3.08
Other versions:
MIME-Base64-2.12

名前

MIME::Base64 - Encoding and decoding of base64 strings

MIME::Base64 - base64文字列のエンコードとデコード

概要

 use MIME::Base64;

 $encoded = encode_base64('Aladdin:open sesame');
 $decoded = decode_base64($encoded);

説明

This module provides functions to encode and decode strings into and from the base64 encoding specified in RFC 2045 - MIME (Multipurpose Internet Mail Extensions). The base64 encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used, enabling 6 bits to be represented per printable character.

このモジュールは、RFC 2045 - MIME (Multipurpose Internet Mail Extensions) で定義されている base64 エンコーディングと文字列の間のエンコードとデコードを 行なう関数を提供するモジュールです。 base64 エンコーディングは、任意のオクテット列を、 人間可読である必要のない形式で表現するために設計されています。 65 文字の US-ASCII の部分集合 ([A-Za-z0-9+/=]) が使われ、一つの表示可能文字に 6 ビットが割り当てられます。

The following functions are provided:

次の関数が提供されています:

encode_base64($str)
encode_base64($str, $eol);

Encode data by calling the encode_base64() function. The first argument is the string to encode. The second argument is the line-ending sequence to use. It is optional and defaults to "\n". The returned encoded string is broken into lines of no more than 76 characters each and it will end with $eol unless it is empty. Pass an empty string as second argument if you do not want the encoded string to be broken into lines.

encode_base64() 関数を呼び出す事によってデータをエンコードします。 最初の引数はエンコードされる文字列です。 2 番目の引数はシーケンスを終了する時に使われる文字です。 これはオプションで、デフォルトは "\n" です。 返されるエンコードされた文字列は 76 文字を越えないように分割され、 空文字列でなければ $eol が最後に付きます。 エンコードされた文字列を複数の行に分割したくない場合は、2 番目の引数として 空文字列を渡してください。

decode_base64($str)

Decode a base64 string by calling the decode_base64() function. This function takes a single argument which is the string to decode and returns the decoded data.

decode_base64() 関数を呼び出す事によってデータをデコードします。 この関数はデコードする文字列である単独の引数をとり、デコードしたデータを 返します。

Any character not part of the 65-character base64 subset is silently ignored. Characters occurring after a '=' padding character are never decoded.

65 文字の base64 部分集合の中にない文字は、単に無視されます。 詰め物文字 '=' の後ろの文字は決してデコードされません。

If the length of the string to decode, after ignoring non-base64 chars, is not a multiple of 4 or if padding occurs too early, then a warning is generated if perl is running under -w.

base64 文字以外を無視した後、デコードする文字列の長さが 4 の整数倍ではないか、詰め物文字があまりにも早く見付かった場合、 perl が -w で動作していると警告が表示されます。

If you prefer not to import these routines into your namespace, you can call them as:

これらの関数をあなたの名前空間にインポートすることを好ましく 思わないのであれば、以下のように呼ぶことが出来ます:

    use MIME::Base64 ();
    $encoded = MIME::Base64::encode($decoded);
    $decoded = MIME::Base64::decode($encoded);

診断

The following warnings can be generated if perl is invoked with the -w switch:

perl を -w スイッチを付けて起動すると、以下の警告が 表示されることがあります:

Premature end of base64 data

The number of characters to decode is not a multiple of 4. Legal base64 data should be padded with one or two "=" characters to make its length a multiple of 4. The decoded result will be the same whether the padding is present or not.

デコードする文字の数が 4 の倍数ではありません。 正当な base64 データは 長さが 4 の整数倍になるよう、一つまたは二つの "=" 文字で調整されている 必要があります。 デコードの結果は、詰め物文字があるかどうかに関わらず同じです。

Premature padding of base64 data

The '=' padding character occurs as the first or second character in a base64 quartet.

詰め物文字 '=' が、base64 の 4 文字の組での先頭または 2 番目として 現れています。

The following exception can be raised:

次の例外が発生することがあります:

Wide character in subroutine entry

The string passed to encode_base64() contains characters with code above 255. The base64 encoding is only defined for single-byte characters. Use the Encode module to select the byte encoding you want.

encode_base64() に渡された文字列に、255 を超える符号位置を持つ文字が 含まれています。 base64 エンコーディングは単一バイト文字に対してのみ定義されています。 求めているバイトエンコーディングを選ぶために Encode モジュールを 使ってください。

If you want to encode a large file, you should encode it in chunks that are a multiple of 57 bytes. This ensures that the base64 lines line up and that you do not end up with padding in the middle. 57 bytes of data fills one complete base64 line (76 == 57*4/3):

大きなファイルをエンコードしたい場合、、57 バイトの倍数の固まりで エンコードすべきです。 これは base64 の行を確保し、中間の行が詰め物文字で 終わらないことを保証にします。 57 バイトのデータは 1 行の完全な base64 行 (76 == 57*4/3) になります:

   use MIME::Base64 qw(encode_base64);

   open(FILE, "/var/log/wtmp") or die "$!";
   while (read(FILE, $buf, 60*57)) {
       print encode_base64($buf);
   }

or if you know you have enough memory

あるいは十分なメモリがあれば:

   use MIME::Base64 qw(encode_base64);
   local($/) = undef;  # slurp
   print encode_base64(<STDIN>);

The same approach as a command line:

コマンド行からの同じアプローチは以下の通りです:

   perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file

Decoding does not need slurp mode if every line contains a multiple of four base64 chars:

すべての行に base64 文字が 4 の倍数個入っていれば、デコードは 吸い込みモードを必要としません:

   perl -MMIME::Base64 -ne 'print decode_base64($_)' <file

Perl v5.8 and better allow extended Unicode characters in strings. Such strings cannot be encoded directly, as the base64 encoding is only defined for single-byte characters. The solution is to use the Encode module to select the byte encoding you want. For example:

Perl v5.8 以降では文字列に拡張 Unicode 文字を使えます。 そのような文字列は直接エンコードできません; base64 エンコーディングは単一バイト文字に対してのみ定義されているからです。 解決法は、求めているバイトエンコーディングを選ぶために Encode モジュールを 使うことです。 例えば:

    use MIME::Base64 qw(encode_base64);
    use Encode qw(encode);

    $encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
    print $encoded;

コピーライト

Copyright 1995-1999, 2001-2004 Gisle Aas.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Distantly based on LWP::Base64 written by Martijn Koster <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans Mulder <hansm@wsinti07.win.tue.nl>

Martijn Koster<m.koster@nexor.co.uk> と Joerg Reichelt <j.reichelt@nexor.co.uk>によって書かれた LWP::Base64、 そして Hans Mulder <hansm@wsinti07.win.tue.nl> により comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl>に投稿されたコードを 少しだけベースにしています。

The XS implementation uses code from metamail. Copyright 1991 Bell Communications Research, Inc. (Bellcore)

XS の実装は metamail のコードを使っています。 Copyright 1991 Bell Communications Research, Inc. (Bellcore)

SEE ALSO

MIME::QuotedPrint