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

名前

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 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+/=])が使われ、1つの表示可能な文字に6ビットが割り当てられます。

The following functions are provided:

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

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 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 set is silently ignored. Characters occuring 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 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 might 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 anyway be as if the padding was there.

デコードする文字の数が4の倍数ではありません。正しいbase64データは 長さが4の整数倍になるよう、1つまたは2つの"="文字で調整すべきです。 デコードの結果は、 埋め込み文字がそこに存在するような形で 行なわれます。

Premature padding of base64 data

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

埋め込み文字'='が、base64の4文字の組での先頭または2番目として 存在します。

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 all the lines contains a multiple of 4 base64 chars:

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

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

コピーライト

Copyright 1995-1999, 2001 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 use code from metamail. Copyright 1991 Bell Communications Research, Inc. (Bellcore)

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

翻訳者

川合孝典 (GCD00051@nifty.ne.jp)