1 |
argrath |
1.1 |
|
2 |
|
|
=encoding euc-jp |
3 |
|
|
|
4 |
|
|
=head1 NAME |
5 |
|
|
|
6 |
|
|
=begin original |
7 |
|
|
|
8 |
|
|
perlfilter - Source Filters |
9 |
|
|
|
10 |
|
|
=end original |
11 |
|
|
|
12 |
|
|
perlfilter - ソースフィルタ |
13 |
|
|
|
14 |
|
|
=head1 DESCRIPTION |
15 |
|
|
|
16 |
|
|
=begin original |
17 |
|
|
|
18 |
|
|
This article is about a little-known feature of Perl called |
19 |
|
|
I<source filters>. Source filters alter the program text of a module |
20 |
|
|
before Perl sees it, much as a C preprocessor alters the source text of |
21 |
|
|
a C program before the compiler sees it. This article tells you more |
22 |
|
|
about what source filters are, how they work, and how to write your |
23 |
|
|
own. |
24 |
|
|
|
25 |
|
|
=end original |
26 |
|
|
|
27 |
|
|
この記事は、ほとんど知られていない Perl の機能である I<ソースフィルタ> に |
28 |
|
|
関するものです。 |
29 |
|
|
C プリプロセッサが C プログラムのソーステキストをコンパイラが見る前に |
30 |
|
|
変更するように、ソースフィルタはモジュールのプログラム文を Perl が |
31 |
|
|
見る前に変更します。 |
32 |
|
|
この記事は、ソースフィルタとは何か、どのように動作するのか、自分自身で |
33 |
|
|
書くにはどうすればいいかについての情報を提供します。 |
34 |
|
|
|
35 |
|
|
=begin original |
36 |
|
|
|
37 |
|
|
The original purpose of source filters was to let you encrypt your |
38 |
|
|
program source to prevent casual piracy. This isn't all they can do, as |
39 |
|
|
you'll soon learn. But first, the basics. |
40 |
|
|
|
41 |
|
|
=end original |
42 |
|
|
|
43 |
|
|
ソースフィルタの本来の目的は、カジュアルな盗み見を防ぐためにプログラム |
44 |
|
|
ソースを暗号化するためでした。 |
45 |
|
|
これから学ぶように、出来ることはこれだけではありません。 |
46 |
|
|
しかしまずは基本からです。 |
47 |
|
|
|
48 |
|
|
=head1 CONCEPTS |
49 |
|
|
|
50 |
|
|
(コンセプト) |
51 |
|
|
|
52 |
|
|
=begin original |
53 |
|
|
|
54 |
|
|
Before the Perl interpreter can execute a Perl script, it must first |
55 |
|
|
read it from a file into memory for parsing and compilation. If that |
56 |
|
|
script itself includes other scripts with a C<use> or C<require> |
57 |
|
|
statement, then each of those scripts will have to be read from their |
58 |
|
|
respective files as well. |
59 |
|
|
|
60 |
|
|
=end original |
61 |
|
|
|
62 |
|
|
Perl インタプリタが Perl スクリプトを実行できるようにする前に、 |
63 |
|
|
パースとコンパイルのためにまずファイルをメモリに読み込まなければなりません。 |
64 |
|
|
このスクリプト自身が C<use> 文や C<require> 文で他のスクリプトを |
65 |
|
|
インクルードしているなら、それらのスクリプトも同様にファイルから読み込む |
66 |
|
|
必要があります。 |
67 |
|
|
|
68 |
|
|
=begin original |
69 |
|
|
|
70 |
|
|
Now think of each logical connection between the Perl parser and an |
71 |
|
|
individual file as a I<source stream>. A source stream is created when |
72 |
|
|
the Perl parser opens a file, it continues to exist as the source code |
73 |
|
|
is read into memory, and it is destroyed when Perl is finished parsing |
74 |
|
|
the file. If the parser encounters a C<require> or C<use> statement in |
75 |
|
|
a source stream, a new and distinct stream is created just for that |
76 |
|
|
file. |
77 |
|
|
|
78 |
|
|
=end original |
79 |
|
|
|
80 |
|
|
ここで、Perl パーサと個々のファイルとの論理的な接続を I<ソースストリーム> |
81 |
|
|
(source stream) として考えます。 |
82 |
|
|
ソースストリームは Perl パーサがファイルを開いたときに作成され、 |
83 |
|
|
ソースコードがメモリに読み込まれている間存在し、Perl がファイルを |
84 |
|
|
パースし終えたときに破壊されます。 |
85 |
|
|
パーサがソースストリーム中に C<require> 文や C<use> 文に出会うと、 |
86 |
|
|
新しく異なったストリームがそのファイルのために作成されます。 |
87 |
|
|
|
88 |
|
|
=begin original |
89 |
|
|
|
90 |
|
|
The diagram below represents a single source stream, with the flow of |
91 |
|
|
source from a Perl script file on the left into the Perl parser on the |
92 |
|
|
right. This is how Perl normally operates. |
93 |
|
|
|
94 |
|
|
=end original |
95 |
|
|
|
96 |
|
|
以下の図は単一のソースストリームを表現していて、左側の Perl スクリプト |
97 |
|
|
ファイルから右側の Perl パーサへのソースの流れです。 |
98 |
|
|
これは Perl が普通処理する方法です。 |
99 |
|
|
|
100 |
|
|
file -------> parser |
101 |
|
|
|
102 |
|
|
=begin original |
103 |
|
|
|
104 |
|
|
There are two important points to remember: |
105 |
|
|
|
106 |
|
|
=end original |
107 |
|
|
|
108 |
|
|
覚えておくべき重要なポイントが二つあります: |
109 |
|
|
|
110 |
|
|
=over 5 |
111 |
|
|
|
112 |
|
|
=item 1. |
113 |
|
|
|
114 |
|
|
=begin original |
115 |
|
|
|
116 |
|
|
Although there can be any number of source streams in existence at any |
117 |
|
|
given time, only one will be active. |
118 |
|
|
|
119 |
|
|
=end original |
120 |
|
|
|
121 |
|
|
同時に任意の数のソースストリームが存在できますが、一つだけが |
122 |
|
|
有効となります。 |
123 |
|
|
|
124 |
|
|
=item 2. |
125 |
|
|
|
126 |
|
|
=begin original |
127 |
|
|
|
128 |
|
|
Every source stream is associated with only one file. |
129 |
|
|
|
130 |
|
|
=end original |
131 |
|
|
|
132 |
|
|
各ソースストリームはただ一つのファイルと関連づけられます。 |
133 |
|
|
|
134 |
|
|
=back |
135 |
|
|
|
136 |
|
|
=begin original |
137 |
|
|
|
138 |
|
|
A source filter is a special kind of Perl module that intercepts and |
139 |
|
|
modifies a source stream before it reaches the parser. A source filter |
140 |
|
|
changes our diagram like this: |
141 |
|
|
|
142 |
|
|
=end original |
143 |
|
|
|
144 |
|
|
ソースフィルタは、ソースストリームがパーサに届く前に捕まえて修正する、 |
145 |
|
|
特別な種類の Perl モジュールです。 |
146 |
|
|
ソースフィルタは以下のようにダイアグラムを変更します: |
147 |
|
|
|
148 |
|
|
file ----> filter ----> parser |
149 |
|
|
|
150 |
|
|
=begin original |
151 |
|
|
|
152 |
|
|
If that doesn't make much sense, consider the analogy of a command |
153 |
|
|
pipeline. Say you have a shell script stored in the compressed file |
154 |
|
|
I<trial.gz>. The simple pipeline command below runs the script without |
155 |
|
|
needing to create a temporary file to hold the uncompressed file. |
156 |
|
|
|
157 |
|
|
=end original |
158 |
|
|
|
159 |
|
|
これにあまり納得が出来ないなら、コマンドパイプラインの例えを |
160 |
|
|
考えてみてください。 |
161 |
|
|
圧縮されたファイル I<trial.gz> に補完されたシェルスクリプトを |
162 |
|
|
考えてみてください。 |
163 |
|
|
後述の単純なパイプラインコマンドは展開されたファイルを保管するための |
164 |
|
|
一時ファイルを作ることなくスクリプトを実行します。 |
165 |
|
|
|
166 |
|
|
gunzip -c trial.gz | sh |
167 |
|
|
|
168 |
|
|
=begin original |
169 |
|
|
|
170 |
|
|
In this case, the data flow from the pipeline can be represented as follows: |
171 |
|
|
|
172 |
|
|
=end original |
173 |
|
|
|
174 |
|
|
この場合、パイプラインからのデータフローは以下のように表現できます: |
175 |
|
|
|
176 |
|
|
trial.gz ----> gunzip ----> sh |
177 |
|
|
|
178 |
|
|
=begin original |
179 |
|
|
|
180 |
|
|
With source filters, you can store the text of your script compressed and use a source filter to uncompress it for Perl's parser: |
181 |
|
|
|
182 |
|
|
=end original |
183 |
|
|
|
184 |
|
|
ソースフィルタがあると、スクリプトのテキストを圧縮して、Perl パーサのために |
185 |
|
|
展開するソースフィルタを使います: |
186 |
|
|
|
187 |
|
|
compressed gunzip |
188 |
|
|
Perl program ---> source filter ---> parser |
189 |
|
|
|
190 |
|
|
=head1 USING FILTERS |
191 |
|
|
|
192 |
|
|
(フィルタを使う) |
193 |
|
|
|
194 |
|
|
=begin original |
195 |
|
|
|
196 |
|
|
So how do you use a source filter in a Perl script? Above, I said that |
197 |
|
|
a source filter is just a special kind of module. Like all Perl |
198 |
|
|
modules, a source filter is invoked with a use statement. |
199 |
|
|
|
200 |
|
|
=end original |
201 |
|
|
|
202 |
|
|
それで、どうやって Perl スクリプトでソースフィルタを使うのでしょう? |
203 |
|
|
先に、ソースフィルタは単に特別な種類のモジュールであると言いました。 |
204 |
|
|
その他全ての Perl モジュールと同様、ソースフィルタは use 文で |
205 |
|
|
起動されます。 |
206 |
|
|
|
207 |
|
|
=begin original |
208 |
|
|
|
209 |
|
|
Say you want to pass your Perl source through the C preprocessor before |
210 |
|
|
execution. As it happens, the source filters distribution comes with a C |
211 |
|
|
preprocessor filter module called Filter::cpp. |
212 |
|
|
|
213 |
|
|
=end original |
214 |
|
|
|
215 |
|
|
Perl のソースを実行前に C のプリプロセッサを通したいとします。 |
216 |
|
|
たまたまソースフィルタ配布には Filter::cpp と呼ばれる C プリプロセッサ |
217 |
|
|
フィルタモジュールが含まれています。 |
218 |
|
|
|
219 |
|
|
=begin original |
220 |
|
|
|
221 |
|
|
Below is an example program, C<cpp_test>, which makes use of this filter. |
222 |
|
|
Line numbers have been added to allow specific lines to be referenced |
223 |
|
|
easily. |
224 |
|
|
|
225 |
|
|
=end original |
226 |
|
|
|
227 |
|
|
以下は、このフィルタを使うためのサンプルプログラムである C<cpp_test> です。 |
228 |
|
|
行番号は、特定の行を参照しやすくするために追加されています。 |
229 |
|
|
|
230 |
|
|
1: use Filter::cpp; |
231 |
|
|
2: #define TRUE 1 |
232 |
|
|
3: $a = TRUE; |
233 |
|
|
4: print "a = $a\n"; |
234 |
|
|
|
235 |
|
|
=begin original |
236 |
|
|
|
237 |
|
|
When you execute this script, Perl creates a source stream for the |
238 |
|
|
file. Before the parser processes any of the lines from the file, the |
239 |
|
|
source stream looks like this: |
240 |
|
|
|
241 |
|
|
=end original |
242 |
|
|
|
243 |
|
|
このスクリプトを実行すると、Perl はこのファイルのためのソースストリームを |
244 |
|
|
作成します。 |
245 |
|
|
パーサがファイルから行を処理する前、ソースストリームは以下のように |
246 |
|
|
なります: |
247 |
|
|
|
248 |
|
|
cpp_test ---------> parser |
249 |
|
|
|
250 |
|
|
=begin original |
251 |
|
|
|
252 |
|
|
Line 1, C<use Filter::cpp>, includes and installs the C<cpp> filter |
253 |
|
|
module. All source filters work this way. The use statement is compiled |
254 |
|
|
and executed at compile time, before any more of the file is read, and |
255 |
|
|
it attaches the cpp filter to the source stream behind the scenes. Now |
256 |
|
|
the data flow looks like this: |
257 |
|
|
|
258 |
|
|
=end original |
259 |
|
|
|
260 |
|
|
1 行目の C<use Filter::cpp> で、C<cpp> モジュールをインクルードして |
261 |
|
|
インストールします。 |
262 |
|
|
全てのソースフィルタはこのようにして動作します。 |
263 |
|
|
use 文はコンパイルされてコンパイル時に、ファイルの残りの部分が読み込まれる |
264 |
|
|
前に実行され、背後でソースフィルタに cpp フィルタをくっつけます。 |
265 |
|
|
ここでデータフローは以下のようになります: |
266 |
|
|
|
267 |
|
|
cpp_test ----> cpp filter ----> parser |
268 |
|
|
|
269 |
|
|
=begin original |
270 |
|
|
|
271 |
|
|
As the parser reads the second and subsequent lines from the source |
272 |
|
|
stream, it feeds those lines through the C<cpp> source filter before |
273 |
|
|
processing them. The C<cpp> filter simply passes each line through the |
274 |
|
|
real C preprocessor. The output from the C preprocessor is then |
275 |
|
|
inserted back into the source stream by the filter. |
276 |
|
|
|
277 |
|
|
=end original |
278 |
|
|
|
279 |
|
|
パーサがソースストリームから 2 行目以降を読むにつれて、処理する前に |
280 |
|
|
C<cpp> ソースフィルタを通して行が供給されます。 |
281 |
|
|
C<cpp> フィルタは単に各行を実際の C プリプロセッサに通します。 |
282 |
|
|
C プリプロセッサからの出力はそれからフィルタによってソースストリームに |
283 |
|
|
再挿入されます。 |
284 |
|
|
|
285 |
|
|
.-> cpp --. |
286 |
|
|
| | |
287 |
|
|
| | |
288 |
|
|
| <-' |
289 |
|
|
cpp_test ----> cpp filter ----> parser |
290 |
|
|
|
291 |
|
|
=begin original |
292 |
|
|
|
293 |
|
|
The parser then sees the following code: |
294 |
|
|
|
295 |
|
|
=end original |
296 |
|
|
|
297 |
|
|
それからパーサは以下のコードを見ます: |
298 |
|
|
|
299 |
|
|
use Filter::cpp; |
300 |
|
|
$a = 1; |
301 |
|
|
print "a = $a\n"; |
302 |
|
|
|
303 |
|
|
=begin original |
304 |
|
|
|
305 |
|
|
Let's consider what happens when the filtered code includes another |
306 |
|
|
module with use: |
307 |
|
|
|
308 |
|
|
=end original |
309 |
|
|
|
310 |
|
|
フィルタされたコードに use を使ったもう一つのモジュールを含んでいる |
311 |
|
|
場合に何が起きるかを考えてみましょう: |
312 |
|
|
|
313 |
|
|
1: use Filter::cpp; |
314 |
|
|
2: #define TRUE 1 |
315 |
|
|
3: use Fred; |
316 |
|
|
4: $a = TRUE; |
317 |
|
|
5: print "a = $a\n"; |
318 |
|
|
|
319 |
|
|
=begin original |
320 |
|
|
|
321 |
|
|
The C<cpp> filter does not apply to the text of the Fred module, only |
322 |
|
|
to the text of the file that used it (C<cpp_test>). Although the use |
323 |
|
|
statement on line 3 will pass through the cpp filter, the module that |
324 |
|
|
gets included (C<Fred>) will not. The source streams look like this |
325 |
|
|
after line 3 has been parsed and before line 4 is parsed: |
326 |
|
|
|
327 |
|
|
=end original |
328 |
|
|
|
329 |
|
|
C<cpp> フィルタは Fred モジュールのテキストには適用されず、 |
330 |
|
|
フィルタが使われているファイル (C<cpp_test>) のテキストにのみ |
331 |
|
|
適用されます。 |
332 |
|
|
3 行目の use 文は cpp フィルタに渡されますが、インクルードされる |
333 |
|
|
モジュール (C<Fred>) は渡されません。 |
334 |
|
|
3 行目がパースされ、4 行目がパースされる前のソースストリームは |
335 |
|
|
以下のようになります: |
336 |
|
|
|
337 |
|
|
cpp_test ---> cpp filter ---> parser (INACTIVE) |
338 |
|
|
|
339 |
|
|
Fred.pm ----> parser |
340 |
|
|
|
341 |
|
|
=begin original |
342 |
|
|
|
343 |
|
|
As you can see, a new stream has been created for reading the source |
344 |
|
|
from C<Fred.pm>. This stream will remain active until all of C<Fred.pm> |
345 |
|
|
has been parsed. The source stream for C<cpp_test> will still exist, |
346 |
|
|
but is inactive. Once the parser has finished reading Fred.pm, the |
347 |
|
|
source stream associated with it will be destroyed. The source stream |
348 |
|
|
for C<cpp_test> then becomes active again and the parser reads line 4 |
349 |
|
|
and subsequent lines from C<cpp_test>. |
350 |
|
|
|
351 |
|
|
=end original |
352 |
|
|
|
353 |
|
|
見て分かるように、C<Fred.pm> からソースを読み込むための新しいストリームが |
354 |
|
|
作成されます。 |
355 |
|
|
このストリームは C<Fred.pm> を全て読み込むまで有効のままです。 |
356 |
|
|
C<cpp_test> のためのソースストリームは存在したままですが、無効に |
357 |
|
|
なっています。 |
358 |
|
|
パーサが Fred.pm からの読み込みを終了すると、これに関連づけられた |
359 |
|
|
ソースストリームは破壊されます。 |
360 |
|
|
それから C<cpp_test> のためのソースストリームが再び有効になり、 |
361 |
|
|
パーサは C<cpp_test> から 4 行目以降を読み込みます。 |
362 |
|
|
|
363 |
|
|
=begin original |
364 |
|
|
|
365 |
|
|
You can use more than one source filter on a single file. Similarly, |
366 |
|
|
you can reuse the same filter in as many files as you like. |
367 |
|
|
|
368 |
|
|
=end original |
369 |
|
|
|
370 |
|
|
一つのファイルに複数のソースフィルタを使うことができます。 |
371 |
|
|
同様に、好きなだけ多くのファイルに対して同じフィルタを |
372 |
|
|
再使用することができます。 |
373 |
|
|
|
374 |
|
|
=begin original |
375 |
|
|
|
376 |
|
|
For example, if you have a uuencoded and compressed source file, it is |
377 |
|
|
possible to stack a uudecode filter and an uncompression filter like |
378 |
|
|
this: |
379 |
|
|
|
380 |
|
|
=end original |
381 |
|
|
|
382 |
|
|
例えば、uuencode されて圧縮されているソースファイルがある場合、 |
383 |
|
|
次のようにして uudecode フィルタと uncompress フィルタを |
384 |
|
|
スタックさせることができます: |
385 |
|
|
|
386 |
|
|
use Filter::uudecode; use Filter::uncompress; |
387 |
|
|
M'XL(".H<US4''V9I;F%L')Q;>7/;1I;_>_I3=&E=%:F*I"T?22Q/ |
388 |
|
|
M6]9*<IQCO*XFT"0[PL%%'Y+IG?WN^ZYN-$'J.[.JE$,20/?K=_[> |
389 |
|
|
... |
390 |
|
|
|
391 |
|
|
=begin original |
392 |
|
|
|
393 |
|
|
Once the first line has been processed, the flow will look like this: |
394 |
|
|
|
395 |
|
|
=end original |
396 |
|
|
|
397 |
|
|
最初の行が処理されると、フローは以下のようになります: |
398 |
|
|
|
399 |
|
|
file ---> uudecode ---> uncompress ---> parser |
400 |
|
|
filter filter |
401 |
|
|
|
402 |
|
|
=begin original |
403 |
|
|
|
404 |
|
|
Data flows through filters in the same order they appear in the source |
405 |
|
|
file. The uudecode filter appeared before the uncompress filter, so the |
406 |
|
|
source file will be uudecoded before it's uncompressed. |
407 |
|
|
|
408 |
|
|
=end original |
409 |
|
|
|
410 |
|
|
データはソースファイルに現れたのと同じ順でフィルタを流れます。 |
411 |
|
|
uudecode フィルタは uncompress フィルタの前に現れるので、ソースファイルは |
412 |
|
|
展開される前に uudecode されます。 |
413 |
|
|
|
414 |
|
|
=head1 WRITING A SOURCE FILTER |
415 |
|
|
|
416 |
|
|
(ソースフィルタを書く) |
417 |
|
|
|
418 |
|
|
=begin original |
419 |
|
|
|
420 |
|
|
There are three ways to write your own source filter. You can write it |
421 |
|
|
in C, use an external program as a filter, or write the filter in Perl. |
422 |
|
|
I won't cover the first two in any great detail, so I'll get them out |
423 |
|
|
of the way first. Writing the filter in Perl is most convenient, so |
424 |
|
|
I'll devote the most space to it. |
425 |
|
|
|
426 |
|
|
=end original |
427 |
|
|
|
428 |
|
|
独自のソースフィルタを書くには三つの方法があります。 |
429 |
|
|
C で書くか、フィルタとして外部プログラムを使うか、Perl でフィルタを |
430 |
|
|
書くかです。 |
431 |
|
|
最初の二つについてはあまり詳しくは記述しないので、先にこれらについて |
432 |
|
|
触れます。 |
433 |
|
|
Perl でフィルタを書くのが一番便利なので、これに最大のスペースを割きます。 |
434 |
|
|
|
435 |
|
|
=head1 WRITING A SOURCE FILTER IN C |
436 |
|
|
|
437 |
|
|
(C でソースフィルタを書く) |
438 |
|
|
|
439 |
|
|
=begin original |
440 |
|
|
|
441 |
|
|
The first of the three available techniques is to write the filter |
442 |
|
|
completely in C. The external module you create interfaces directly |
443 |
|
|
with the source filter hooks provided by Perl. |
444 |
|
|
|
445 |
|
|
=end original |
446 |
|
|
|
447 |
|
|
利用可能な三つのテクニックのうちの一つ目は、フィルタを完全に C で |
448 |
|
|
書くことです。 |
449 |
|
|
作成した外部モジュールは Perl によって提供されるソースフィルタフックと |
450 |
|
|
直接接続されます。 |
451 |
|
|
|
452 |
|
|
=begin original |
453 |
|
|
|
454 |
|
|
The advantage of this technique is that you have complete control over |
455 |
|
|
the implementation of your filter. The big disadvantage is the |
456 |
|
|
increased complexity required to write the filter - not only do you |
457 |
|
|
need to understand the source filter hooks, but you also need a |
458 |
|
|
reasonable knowledge of Perl guts. One of the few times it is worth |
459 |
|
|
going to this trouble is when writing a source scrambler. The |
460 |
|
|
C<decrypt> filter (which unscrambles the source before Perl parses it) |
461 |
|
|
included with the source filter distribution is an example of a C |
462 |
|
|
source filter (see Decryption Filters, below). |
463 |
|
|
|
464 |
|
|
=end original |
465 |
|
|
|
466 |
|
|
このテクニックの利点は、フィルタの実装を完全に制御できることです。 |
467 |
|
|
大きな弱点は、フィルタを書くために必要な複雑性が増すことです - |
468 |
|
|
ソースフィルタフックについて理解するだけでなく、Perl の内部に関する |
469 |
|
|
ある程度の知識も必要です。 |
470 |
|
|
この困難に向かう価値のある数回に一回はソースのスクランブル化を |
471 |
|
|
書くときです。 |
472 |
|
|
(Perl がパースする前にソースのスクランブルを解除する) C<decrypt> フィルタは |
473 |
|
|
C ソースフィルタの例です (以下の Decryption Filters を参照してください)。 |
474 |
|
|
|
475 |
|
|
=over 5 |
476 |
|
|
|
477 |
|
|
=item B<Decryption Filters> |
478 |
|
|
|
479 |
|
|
(B<復号フィルタ>) |
480 |
|
|
|
481 |
|
|
=begin original |
482 |
|
|
|
483 |
|
|
All decryption filters work on the principle of "security through |
484 |
|
|
obscurity." Regardless of how well you write a decryption filter and |
485 |
|
|
how strong your encryption algorithm is, anyone determined enough can |
486 |
|
|
retrieve the original source code. The reason is quite simple - once |
487 |
|
|
the decryption filter has decrypted the source back to its original |
488 |
|
|
form, fragments of it will be stored in the computer's memory as Perl |
489 |
|
|
parses it. The source might only be in memory for a short period of |
490 |
|
|
time, but anyone possessing a debugger, skill, and lots of patience can |
491 |
|
|
eventually reconstruct your program. |
492 |
|
|
|
493 |
|
|
=end original |
494 |
|
|
|
495 |
|
|
全ての復号フィルタは「不明瞭さによるセキュリティ」の原則に則っています。 |
496 |
|
|
どれだけうまく復号フィルタを書いて、どんなに強い暗号化アルゴリズムを |
497 |
|
|
使っても、十分な決意があれば元のソースコードを取得できます。 |
498 |
|
|
その理由はとても単純です - 一旦復号フィルタがソースを元の形に戻すと、その |
499 |
|
|
一部は Perl がパースするためにコンピュータのメモリに保管されます。 |
500 |
|
|
ソースは短い時間の間だけしかメモリにないかもしれませんが、デバッガ、スキル、 |
501 |
|
|
多大な忍耐がある人なら最終的にはプログラムを再構成できます。 |
502 |
|
|
|
503 |
|
|
=begin original |
504 |
|
|
|
505 |
|
|
That said, there are a number of steps that can be taken to make life |
506 |
|
|
difficult for the potential cracker. The most important: Write your |
507 |
|
|
decryption filter in C and statically link the decryption module into |
508 |
|
|
the Perl binary. For further tips to make life difficult for the |
509 |
|
|
potential cracker, see the file I<decrypt.pm> in the source filters |
510 |
|
|
distribution. |
511 |
|
|
|
512 |
|
|
=end original |
513 |
|
|
|
514 |
|
|
潜在的なクラッカーに対して物事を難しくするために取るいくつかのステップが |
515 |
|
|
あります。 |
516 |
|
|
最も重要なのは: 復号フィルタを C で書いて、復号モジュールを Perl バイナリと |
517 |
|
|
静的にリンクすることです。 |
518 |
|
|
潜在的なクラッカーに対して物事を難しくするために取るさらなるステップは、 |
519 |
|
|
ソースフィルタ配布の I<decrypt.pm> ファイルを参照してください。 |
520 |
|
|
|
521 |
|
|
=back |
522 |
|
|
|
523 |
|
|
=head1 CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE |
524 |
|
|
|
525 |
|
|
(独立した実行ファイルとしてソースフィルタとして作成する) |
526 |
|
|
|
527 |
|
|
=begin original |
528 |
|
|
|
529 |
|
|
An alternative to writing the filter in C is to create a separate |
530 |
|
|
executable in the language of your choice. The separate executable |
531 |
|
|
reads from standard input, does whatever processing is necessary, and |
532 |
|
|
writes the filtered data to standard output. C<Filter::cpp> is an |
533 |
|
|
example of a source filter implemented as a separate executable - the |
534 |
|
|
executable is the C preprocessor bundled with your C compiler. |
535 |
|
|
|
536 |
|
|
=end original |
537 |
|
|
|
538 |
|
|
C でフィルタを書くための代替案は、好みの言語で独立した実行ファイルを |
539 |
|
|
作ることです。 |
540 |
|
|
独立した実行ファイルは標準出力から読み込み、何か必要な処理を行い、 |
541 |
|
|
フィルタされたデータを標準出力に書き込みます。 |
542 |
|
|
C<Filter::cpp> は、独立した実行ファイルとして実行されたソースフィルタの |
543 |
|
|
例です - 実行ファイルは C コンパイラに付いている C プリプロセッサです。 |
544 |
|
|
|
545 |
|
|
=begin original |
546 |
|
|
|
547 |
|
|
The source filter distribution includes two modules that simplify this |
548 |
|
|
task: C<Filter::exec> and C<Filter::sh>. Both allow you to run any |
549 |
|
|
external executable. Both use a coprocess to control the flow of data |
550 |
|
|
into and out of the external executable. (For details on coprocesses, |
551 |
|
|
see Stephens, W.R., "Advanced Programming in the UNIX Environment." |
552 |
|
|
Addison-Wesley, ISBN 0-210-56317-7, pages 441-445.) The difference |
553 |
|
|
between them is that C<Filter::exec> spawns the external command |
554 |
|
|
directly, while C<Filter::sh> spawns a shell to execute the external |
555 |
|
|
command. (Unix uses the Bourne shell; NT uses the cmd shell.) Spawning |
556 |
|
|
a shell allows you to make use of the shell metacharacters and |
557 |
|
|
redirection facilities. |
558 |
|
|
|
559 |
|
|
=end original |
560 |
|
|
|
561 |
|
|
ソースフィルタ配布にはこのタスクを簡単にするための二つのモジュールが |
562 |
|
|
あります: C<Filter::exec> と C<Filter::sh> です。 |
563 |
|
|
どちらも外部実行ファイルを実行します。 |
564 |
|
|
どちらも外部実行ファイルとのデータのやりとりを制御するのにコプロセスを |
565 |
|
|
使います。 |
566 |
|
|
(コプロセスの詳細については、Stephens, W.R. による |
567 |
|
|
"Advanced Programming in the UNIX Environment." |
568 |
|
|
Addison-Wesley, ISBN 0-210-56317-7, 441-445 ページ を参照してください。) |
569 |
|
|
二つの違いは、C<Filter::exec> は外部コマンドを直接起動しますが、 |
570 |
|
|
C<Filter::sh> は外部コマンドを起動するためのシェルを起動します。 |
571 |
|
|
(Unix は Bourne シェルを使います; NT は cmd シェルを使います。) |
572 |
|
|
シェルを起動することにより、シェルのメタ文字とリダイレクト機構を |
573 |
|
|
使えるようになります。 |
574 |
|
|
|
575 |
|
|
=begin original |
576 |
|
|
|
577 |
|
|
Here is an example script that uses C<Filter::sh>: |
578 |
|
|
|
579 |
|
|
=end original |
580 |
|
|
|
581 |
|
|
以下は C<Filter::sh> を使ったスクリプトの例です: |
582 |
|
|
|
583 |
|
|
use Filter::sh 'tr XYZ PQR'; |
584 |
|
|
$a = 1; |
585 |
|
|
print "XYZ a = $a\n"; |
586 |
|
|
|
587 |
|
|
=begin original |
588 |
|
|
|
589 |
|
|
The output you'll get when the script is executed: |
590 |
|
|
|
591 |
|
|
=end original |
592 |
|
|
|
593 |
|
|
スクリプトが実行されたときに得られる出力は: |
594 |
|
|
|
595 |
|
|
PQR a = 1 |
596 |
|
|
|
597 |
|
|
=begin original |
598 |
|
|
|
599 |
|
|
Writing a source filter as a separate executable works fine, but a |
600 |
|
|
small performance penalty is incurred. For example, if you execute the |
601 |
|
|
small example above, a separate subprocess will be created to run the |
602 |
|
|
Unix C<tr> command. Each use of the filter requires its own subprocess. |
603 |
|
|
If creating subprocesses is expensive on your system, you might want to |
604 |
|
|
consider one of the other options for creating source filters. |
605 |
|
|
|
606 |
|
|
=end original |
607 |
|
|
|
608 |
|
|
独立した実行ファイルとしてソースフィルタを書くとうまく動作しますが、 |
609 |
|
|
小さい性能上のペナルティがあります。 |
610 |
|
|
例えば、上述の小さい例を実行すると、Unix の C<tr> コマンドを実行するために |
611 |
|
|
別々のサブプロセスが作られます。 |
612 |
|
|
サブシステムの作成のコストが高いシステムでは、ソースフィルタを作るための |
613 |
|
|
その他の選択肢を考えたいかもしれません。 |
614 |
|
|
|
615 |
|
|
=head1 WRITING A SOURCE FILTER IN PERL |
616 |
|
|
|
617 |
|
|
(Perl でソースフィルタを書く) |
618 |
|
|
|
619 |
|
|
=begin original |
620 |
|
|
|
621 |
|
|
The easiest and most portable option available for creating your own |
622 |
|
|
source filter is to write it completely in Perl. To distinguish this |
623 |
|
|
from the previous two techniques, I'll call it a Perl source filter. |
624 |
|
|
|
625 |
|
|
=end original |
626 |
|
|
|
627 |
|
|
独自のソースフィルタを作成するためのもっとも簡単でもっとも移植性のある |
628 |
|
|
選択肢は、完全に Perl で書くことです。 |
629 |
|
|
これを前述の二つのテクニックと区別するために、ここではこれを |
630 |
|
|
Perl ソースフィルタと呼びます。 |
631 |
|
|
|
632 |
|
|
=begin original |
633 |
|
|
|
634 |
|
|
To help understand how to write a Perl source filter we need an example |
635 |
|
|
to study. Here is a complete source filter that performs rot13 |
636 |
|
|
decoding. (Rot13 is a very simple encryption scheme used in Usenet |
637 |
|
|
postings to hide the contents of offensive posts. It moves every letter |
638 |
|
|
forward thirteen places, so that A becomes N, B becomes O, and Z |
639 |
|
|
becomes M.) |
640 |
|
|
|
641 |
|
|
=end original |
642 |
|
|
|
643 |
|
|
Perl ソースフィルタの書き方を理解するのを助けるために、学習するための |
644 |
|
|
例が必要です。 |
645 |
|
|
以下は rot13 復号を行う完全なソースフィルタです。 |
646 |
|
|
(rot13 は、攻撃的な投稿を隠すために Usenet 投稿で使われたとても簡単な |
647 |
|
|
暗号スキームです。 |
648 |
|
|
これはそれぞれの英文字を 13 ずらします; 従って A は N に、B は O に、 |
649 |
|
|
Z は M になります。) |
650 |
|
|
|
651 |
|
|
package Rot13; |
652 |
|
|
|
653 |
|
|
use Filter::Util::Call; |
654 |
|
|
|
655 |
|
|
sub import { |
656 |
|
|
my ($type) = @_; |
657 |
|
|
my ($ref) = []; |
658 |
|
|
filter_add(bless $ref); |
659 |
|
|
} |
660 |
|
|
|
661 |
|
|
sub filter { |
662 |
|
|
my ($self) = @_; |
663 |
|
|
my ($status); |
664 |
|
|
|
665 |
|
|
tr/n-za-mN-ZA-M/a-zA-Z/ |
666 |
|
|
if ($status = filter_read()) > 0; |
667 |
|
|
$status; |
668 |
|
|
} |
669 |
|
|
|
670 |
|
|
1; |
671 |
|
|
|
672 |
|
|
=begin original |
673 |
|
|
|
674 |
|
|
All Perl source filters are implemented as Perl classes and have the |
675 |
|
|
same basic structure as the example above. |
676 |
|
|
|
677 |
|
|
=end original |
678 |
|
|
|
679 |
|
|
全ての Perl ソースフィルタは Perl クラスとして実装され、上述の例と |
680 |
|
|
同じ基本構造を持ちます。 |
681 |
|
|
|
682 |
|
|
=begin original |
683 |
|
|
|
684 |
|
|
First, we include the C<Filter::Util::Call> module, which exports a |
685 |
|
|
number of functions into your filter's namespace. The filter shown |
686 |
|
|
above uses two of these functions, C<filter_add()> and |
687 |
|
|
C<filter_read()>. |
688 |
|
|
|
689 |
|
|
=end original |
690 |
|
|
|
691 |
|
|
まず、C<Filter::Util::Call> をインクルードして、多くの関数をフィルタの |
692 |
|
|
名前空間にエクスポートします。 |
693 |
|
|
上述のフィルタはこれらの関数の内 C<filter_add()> と C<filter_read()> の |
694 |
|
|
二つの関数を使います。 |
695 |
|
|
|
696 |
|
|
=begin original |
697 |
|
|
|
698 |
|
|
Next, we create the filter object and associate it with the source |
699 |
|
|
stream by defining the C<import> function. If you know Perl well |
700 |
|
|
enough, you know that C<import> is called automatically every time a |
701 |
|
|
module is included with a use statement. This makes C<import> the ideal |
702 |
|
|
place to both create and install a filter object. |
703 |
|
|
|
704 |
|
|
=end original |
705 |
|
|
|
706 |
|
|
次に、フィルタオブジェクトを作って、C<import> 関数を定義することによって |
707 |
|
|
これをソースストリームと結びつけます。 |
708 |
|
|
Perl のことを十分知っているなら、 |
709 |
|
|
C<import> は use 文でモジュールがインクルードされる旅に自動的に |
710 |
|
|
呼び出されることを知っているでしょう。 |
711 |
|
|
これにより、C<import> はフィルタオブジェクトの作成とインストールに |
712 |
|
|
最適の場所と言えます。 |
713 |
|
|
|
714 |
|
|
=begin original |
715 |
|
|
|
716 |
|
|
In the example filter, the object (C<$ref>) is blessed just like any |
717 |
|
|
other Perl object. Our example uses an anonymous array, but this isn't |
718 |
|
|
a requirement. Because this example doesn't need to store any context |
719 |
|
|
information, we could have used a scalar or hash reference just as |
720 |
|
|
well. The next section demonstrates context data. |
721 |
|
|
|
722 |
|
|
=end original |
723 |
|
|
|
724 |
|
|
例のフィルタでは、オブジェクト (C<$ref>) はその他の Perl オブジェクトと |
725 |
|
|
同じように bless されます。 |
726 |
|
|
この例では無名配列を使っていますが、これは必須ではありません。 |
727 |
|
|
この例では内容の情報を補完する必要がないので、スカラリファレンスや |
728 |
|
|
ハッシュリファレンスでを使うこともできます。 |
729 |
|
|
次の節ではコンテキストデータを図示します。 |
730 |
|
|
|
731 |
|
|
=begin original |
732 |
|
|
|
733 |
|
|
The association between the filter object and the source stream is made |
734 |
|
|
with the C<filter_add()> function. This takes a filter object as a |
735 |
|
|
parameter (C<$ref> in this case) and installs it in the source stream. |
736 |
|
|
|
737 |
|
|
=end original |
738 |
|
|
|
739 |
|
|
フィルタオブジェクトとソースストリームの関連付けは C<filter_add()> 関数で |
740 |
|
|
行われます。 |
741 |
|
|
これはフィルタオブジェクト (今回の場合では C<$ref>) を引数に取って、 |
742 |
|
|
これをソースストリームに取り付けます。 |
743 |
|
|
|
744 |
|
|
=begin original |
745 |
|
|
|
746 |
|
|
Finally, there is the code that actually does the filtering. For this |
747 |
|
|
type of Perl source filter, all the filtering is done in a method |
748 |
|
|
called C<filter()>. (It is also possible to write a Perl source filter |
749 |
|
|
using a closure. See the C<Filter::Util::Call> manual page for more |
750 |
|
|
details.) It's called every time the Perl parser needs another line of |
751 |
|
|
source to process. The C<filter()> method, in turn, reads lines from |
752 |
|
|
the source stream using the C<filter_read()> function. |
753 |
|
|
|
754 |
|
|
=end original |
755 |
|
|
|
756 |
|
|
最後に、実際にフィルタリングを行うコードがあります。 |
757 |
|
|
この種の Perl ソースフィルタのために、フィルタリングの全ては |
758 |
|
|
C<filter()> と呼ばれるメソッドで行われます。 |
759 |
|
|
(クロージャを使って Perl ソースフィルタを書くことも可能です。 |
760 |
|
|
さらなる詳細については C<Filter::Util::Call> マニュアルページを |
761 |
|
|
参照してください。) |
762 |
|
|
これは Perl パーサが処理するソースの行が必要になる度に毎回呼び出されます。 |
763 |
|
|
C<filter()> メソッドは、C<filter_read()> 関数を使ってソースストリームから |
764 |
|
|
順番に行を読み込みます。 |
765 |
|
|
|
766 |
|
|
=begin original |
767 |
|
|
|
768 |
|
|
If a line was available from the source stream, C<filter_read()> |
769 |
|
|
returns a status value greater than zero and appends the line to C<$_>. |
770 |
|
|
A status value of zero indicates end-of-file, less than zero means an |
771 |
|
|
error. The filter function itself is expected to return its status in |
772 |
|
|
the same way, and put the filtered line it wants written to the source |
773 |
|
|
stream in C<$_>. The use of C<$_> accounts for the brevity of most Perl |
774 |
|
|
source filters. |
775 |
|
|
|
776 |
|
|
=end original |
777 |
|
|
|
778 |
|
|
ソースストリームから 1 行が利用可能になったら、C<filter_read()> は |
779 |
|
|
0 より大きいステータス値を返して、C<$_> に行を追加します。 |
780 |
|
|
ステータス値が 0 の場合はファイル末尾を示し、0 より小さい場合は |
781 |
|
|
エラーを意味します。 |
782 |
|
|
filter 関数自身はステータスを同じ方法で返し、ソースストリームに |
783 |
|
|
書き込みたいフィルタリングされた行を C<$_> に入れることを |
784 |
|
|
想定されています。 |
785 |
|
|
C<$_> の使い方はほとんどの Perl ソースフィルタの簡潔さを考慮に |
786 |
|
|
入れています。 |
787 |
|
|
|
788 |
|
|
=begin original |
789 |
|
|
|
790 |
|
|
In order to make use of the rot13 filter we need some way of encoding |
791 |
|
|
the source file in rot13 format. The script below, C<mkrot13>, does |
792 |
|
|
just that. |
793 |
|
|
|
794 |
|
|
=end original |
795 |
|
|
|
796 |
|
|
rot13 フィルタを使うには、ソースファイルを rot13 形式で符号化する |
797 |
|
|
方法が必要です。 |
798 |
|
|
以下のスクリプト C<mkrot13> はそれを行います。 |
799 |
|
|
|
800 |
|
|
die "usage mkrot13 filename\n" unless @ARGV; |
801 |
|
|
my $in = $ARGV[0]; |
802 |
|
|
my $out = "$in.tmp"; |
803 |
|
|
open(IN, "<$in") or die "Cannot open file $in: $!\n"; |
804 |
|
|
open(OUT, ">$out") or die "Cannot open file $out: $!\n"; |
805 |
|
|
|
806 |
|
|
print OUT "use Rot13;\n"; |
807 |
|
|
while (<IN>) { |
808 |
|
|
tr/a-zA-Z/n-za-mN-ZA-M/; |
809 |
|
|
print OUT; |
810 |
|
|
} |
811 |
|
|
|
812 |
|
|
close IN; |
813 |
|
|
close OUT; |
814 |
|
|
unlink $in; |
815 |
|
|
rename $out, $in; |
816 |
|
|
|
817 |
|
|
=begin original |
818 |
|
|
|
819 |
|
|
If we encrypt this with C<mkrot13>: |
820 |
|
|
|
821 |
|
|
=end original |
822 |
|
|
|
823 |
|
|
これを C<mkrot13> で暗号化すると: |
824 |
|
|
|
825 |
|
|
print " hello fred \n"; |
826 |
|
|
|
827 |
|
|
=begin original |
828 |
|
|
|
829 |
|
|
the result will be this: |
830 |
|
|
|
831 |
|
|
=end original |
832 |
|
|
|
833 |
|
|
結果は以下のようになります: |
834 |
|
|
|
835 |
|
|
use Rot13; |
836 |
|
|
cevag "uryyb serq\a"; |
837 |
|
|
|
838 |
|
|
=begin original |
839 |
|
|
|
840 |
|
|
Running it produces this output: |
841 |
|
|
|
842 |
|
|
=end original |
843 |
|
|
|
844 |
|
|
これを実行すると以下の出力を生成します: |
845 |
|
|
|
846 |
|
|
hello fred |
847 |
|
|
|
848 |
|
|
=head1 USING CONTEXT: THE DEBUG FILTER |
849 |
|
|
|
850 |
|
|
(コンテキストを使う: デバッグフィルタ) |
851 |
|
|
|
852 |
|
|
=begin original |
853 |
|
|
|
854 |
|
|
The rot13 example was a trivial example. Here's another demonstration |
855 |
|
|
that shows off a few more features. |
856 |
|
|
|
857 |
|
|
=end original |
858 |
|
|
|
859 |
|
|
rot13 の例はつまらない例でした。 |
860 |
|
|
以下は、もういくつかの機能を見せるための例です。 |
861 |
|
|
|
862 |
|
|
=begin original |
863 |
|
|
|
864 |
|
|
Say you wanted to include a lot of debugging code in your Perl script |
865 |
|
|
during development, but you didn't want it available in the released |
866 |
|
|
product. Source filters offer a solution. In order to keep the example |
867 |
|
|
simple, let's say you wanted the debugging output to be controlled by |
868 |
|
|
an environment variable, C<DEBUG>. Debugging code is enabled if the |
869 |
|
|
variable exists, otherwise it is disabled. |
870 |
|
|
|
871 |
|
|
=end original |
872 |
|
|
|
873 |
|
|
開発中に Perl スクリプトに大量のデバッグコードを含めておきたいけれども、 |
874 |
|
|
リリース製品では利用可能にしたくないとします。 |
875 |
|
|
ソースフィルタが解決法を提供します。 |
876 |
|
|
例を単純なままにするために、環境変数 C<DEBUG> で制御されるデバッグ出力が |
877 |
|
|
ほしいとします。 |
878 |
|
|
デバッグコードは、環境変数が存在すれば有効になり、さもなければ |
879 |
|
|
無効になります。 |
880 |
|
|
|
881 |
|
|
=begin original |
882 |
|
|
|
883 |
|
|
Two special marker lines will bracket debugging code, like this: |
884 |
|
|
|
885 |
|
|
=end original |
886 |
|
|
|
887 |
|
|
次のように、二つの特殊なマーカー行でデバッグするコードを囲みます: |
888 |
|
|
|
889 |
|
|
## DEBUG_BEGIN |
890 |
|
|
if ($year > 1999) { |
891 |
|
|
warn "Debug: millennium bug in year $year\n"; |
892 |
|
|
} |
893 |
|
|
## DEBUG_END |
894 |
|
|
|
895 |
|
|
=begin original |
896 |
|
|
|
897 |
|
|
The filter ensures that Perl parses the code between the <DEBUG_BEGIN> |
898 |
|
|
and C<DEBUG_END> markers only when the C<DEBUG> environment variable |
899 |
|
|
exists. That means that when C<DEBUG> does exist, the code above |
900 |
|
|
should be passed through the filter unchanged. The marker lines can |
901 |
|
|
also be passed through as-is, because the Perl parser will see them as |
902 |
|
|
comment lines. When C<DEBUG> isn't set, we need a way to disable the |
903 |
|
|
debug code. A simple way to achieve that is to convert the lines |
904 |
|
|
between the two markers into comments: |
905 |
|
|
|
906 |
|
|
=end original |
907 |
|
|
|
908 |
|
|
C<DEBUG> 環境変数が存在するとき、フィルタは Perl が C<DEBUG_BEGIN> と |
909 |
|
|
C<DEBUG_END> のマーカーの間のコードだけをパースするようにします。 |
910 |
|
|
これにより、C<DEBUG> が存在すると、上述のコードはフィルタを変更なしで |
911 |
|
|
通過して渡されます。 |
912 |
|
|
マーカー行もそのまま渡されます; Perl パーサはこれらをコメント行として |
913 |
|
|
扱うからです。 |
914 |
|
|
C<DEBUG> が設定されていないとき、デバッグコードを無効にする方法が |
915 |
|
|
必要になります。 |
916 |
|
|
これを達成する簡単な方法は、二つのマーカーの間の行をコメントに |
917 |
|
|
変換することです: |
918 |
|
|
|
919 |
|
|
## DEBUG_BEGIN |
920 |
|
|
#if ($year > 1999) { |
921 |
|
|
# warn "Debug: millennium bug in year $year\n"; |
922 |
|
|
#} |
923 |
|
|
## DEBUG_END |
924 |
|
|
|
925 |
|
|
=begin original |
926 |
|
|
|
927 |
|
|
Here is the complete Debug filter: |
928 |
|
|
|
929 |
|
|
=end original |
930 |
|
|
|
931 |
|
|
以下は完全な Debug フィルタです: |
932 |
|
|
|
933 |
|
|
package Debug; |
934 |
|
|
|
935 |
|
|
use strict; |
936 |
|
|
use warnings; |
937 |
|
|
use Filter::Util::Call; |
938 |
|
|
|
939 |
|
|
use constant TRUE => 1; |
940 |
|
|
use constant FALSE => 0; |
941 |
|
|
|
942 |
|
|
sub import { |
943 |
|
|
my ($type) = @_; |
944 |
|
|
my (%context) = ( |
945 |
|
|
Enabled => defined $ENV{DEBUG}, |
946 |
|
|
InTraceBlock => FALSE, |
947 |
|
|
Filename => (caller)[1], |
948 |
|
|
LineNo => 0, |
949 |
|
|
LastBegin => 0, |
950 |
|
|
); |
951 |
|
|
filter_add(bless \%context); |
952 |
|
|
} |
953 |
|
|
|
954 |
|
|
sub Die { |
955 |
|
|
my ($self) = shift; |
956 |
|
|
my ($message) = shift; |
957 |
|
|
my ($line_no) = shift || $self->{LastBegin}; |
958 |
|
|
die "$message at $self->{Filename} line $line_no.\n" |
959 |
|
|
} |
960 |
|
|
|
961 |
|
|
sub filter { |
962 |
|
|
my ($self) = @_; |
963 |
|
|
my ($status); |
964 |
|
|
$status = filter_read(); |
965 |
|
|
++ $self->{LineNo}; |
966 |
|
|
|
967 |
|
|
# deal with EOF/error first |
968 |
|
|
if ($status <= 0) { |
969 |
|
|
$self->Die("DEBUG_BEGIN has no DEBUG_END") |
970 |
|
|
if $self->{InTraceBlock}; |
971 |
|
|
return $status; |
972 |
|
|
} |
973 |
|
|
|
974 |
|
|
if ($self->{InTraceBlock}) { |
975 |
|
|
if (/^\s*##\s*DEBUG_BEGIN/ ) { |
976 |
|
|
$self->Die("Nested DEBUG_BEGIN", $self->{LineNo}) |
977 |
|
|
} elsif (/^\s*##\s*DEBUG_END/) { |
978 |
|
|
$self->{InTraceBlock} = FALSE; |
979 |
|
|
} |
980 |
|
|
|
981 |
|
|
# comment out the debug lines when the filter is disabled |
982 |
|
|
s/^/#/ if ! $self->{Enabled}; |
983 |
|
|
} elsif ( /^\s*##\s*DEBUG_BEGIN/ ) { |
984 |
|
|
$self->{InTraceBlock} = TRUE; |
985 |
|
|
$self->{LastBegin} = $self->{LineNo}; |
986 |
|
|
} elsif ( /^\s*##\s*DEBUG_END/ ) { |
987 |
|
|
$self->Die("DEBUG_END has no DEBUG_BEGIN", $self->{LineNo}); |
988 |
|
|
} |
989 |
|
|
return $status; |
990 |
|
|
} |
991 |
|
|
|
992 |
|
|
1; |
993 |
|
|
|
994 |
|
|
=begin original |
995 |
|
|
|
996 |
|
|
The big difference between this filter and the previous example is the |
997 |
|
|
use of context data in the filter object. The filter object is based on |
998 |
|
|
a hash reference, and is used to keep various pieces of context |
999 |
|
|
information between calls to the filter function. All but two of the |
1000 |
|
|
hash fields are used for error reporting. The first of those two, |
1001 |
|
|
Enabled, is used by the filter to determine whether the debugging code |
1002 |
|
|
should be given to the Perl parser. The second, InTraceBlock, is true |
1003 |
|
|
when the filter has encountered a C<DEBUG_BEGIN> line, but has not yet |
1004 |
|
|
encountered the following C<DEBUG_END> line. |
1005 |
|
|
|
1006 |
|
|
=end original |
1007 |
|
|
|
1008 |
|
|
このフィルタと以前の例との大きな違いは、フィルタオブジェクトの |
1009 |
|
|
コンテキストデータの使用です。 |
1010 |
|
|
フィルタオブジェクトはハッシュリファレンスを基礎としていて、フィルタ関数の |
1011 |
|
|
呼び出し間のコンテキスト情報の様々な断片を保持するために使われます。 |
1012 |
|
|
二つを除いた全てのハッシュフィールドはエラー報告のために使われます。 |
1013 |
|
|
二つの内一番目である Enabled は、デバッグコードが Perl パーサに |
1014 |
|
|
与えられるべきかどうかを決定するために使われます。 |
1015 |
|
|
二番目である InTraceBlock は、フィルタが C<DEBUG_BEGIN> に遭遇したけれども |
1016 |
|
|
まだ引き続く C<DEBUG_END> 行に出会っていないときに真となります。 |
1017 |
|
|
|
1018 |
|
|
=begin original |
1019 |
|
|
|
1020 |
|
|
If you ignore all the error checking that most of the code does, the |
1021 |
|
|
essence of the filter is as follows: |
1022 |
|
|
|
1023 |
|
|
=end original |
1024 |
|
|
|
1025 |
|
|
コードのほとんどが行っているエラーチェックの全てを無視すると、フィルタの |
1026 |
|
|
本質は以下のようになります: |
1027 |
|
|
|
1028 |
|
|
sub filter { |
1029 |
|
|
my ($self) = @_; |
1030 |
|
|
my ($status); |
1031 |
|
|
$status = filter_read(); |
1032 |
|
|
|
1033 |
|
|
# deal with EOF/error first |
1034 |
|
|
return $status if $status <= 0; |
1035 |
|
|
if ($self->{InTraceBlock}) { |
1036 |
|
|
if (/^\s*##\s*DEBUG_END/) { |
1037 |
|
|
$self->{InTraceBlock} = FALSE |
1038 |
|
|
} |
1039 |
|
|
|
1040 |
|
|
# comment out debug lines when the filter is disabled |
1041 |
|
|
s/^/#/ if ! $self->{Enabled}; |
1042 |
|
|
} elsif ( /^\s*##\s*DEBUG_BEGIN/ ) { |
1043 |
|
|
$self->{InTraceBlock} = TRUE; |
1044 |
|
|
} |
1045 |
|
|
return $status; |
1046 |
|
|
} |
1047 |
|
|
|
1048 |
|
|
=begin original |
1049 |
|
|
|
1050 |
|
|
Be warned: just as the C-preprocessor doesn't know C, the Debug filter |
1051 |
|
|
doesn't know Perl. It can be fooled quite easily: |
1052 |
|
|
|
1053 |
|
|
=end original |
1054 |
|
|
|
1055 |
|
|
警告: C プリプロセッサが C のことを知らないのと同様、Debug フィルタは |
1056 |
|
|
Perl のことを知りません。 |
1057 |
|
|
簡単にだませます: |
1058 |
|
|
|
1059 |
|
|
print <<EOM; |
1060 |
|
|
##DEBUG_BEGIN |
1061 |
|
|
EOM |
1062 |
|
|
|
1063 |
|
|
=begin original |
1064 |
|
|
|
1065 |
|
|
Such things aside, you can see that a lot can be achieved with a modest |
1066 |
|
|
amount of code. |
1067 |
|
|
|
1068 |
|
|
=end original |
1069 |
|
|
|
1070 |
|
|
そのようなことを置いておいても、それほどでもない量のコードで多くのことが |
1071 |
|
|
達成できるのが分かります。 |
1072 |
|
|
|
1073 |
|
|
=head1 CONCLUSION |
1074 |
|
|
|
1075 |
|
|
(結び) |
1076 |
|
|
|
1077 |
|
|
=begin original |
1078 |
|
|
|
1079 |
|
|
You now have better understanding of what a source filter is, and you |
1080 |
|
|
might even have a possible use for them. If you feel like playing with |
1081 |
|
|
source filters but need a bit of inspiration, here are some extra |
1082 |
|
|
features you could add to the Debug filter. |
1083 |
|
|
|
1084 |
|
|
=end original |
1085 |
|
|
|
1086 |
|
|
これで、ソースフィルタとは何かについてよりよく理解できたと思います; |
1087 |
|
|
さらにこれらの可能性のある使い方を持っているかもしれません。 |
1088 |
|
|
もしソースフィルタで遊んでみたいと思っているけれどもちょっとした |
1089 |
|
|
インスピレーションが必要なら、以下はデバッグフィルタに加えることが出来る |
1090 |
|
|
追加機能です。 |
1091 |
|
|
|
1092 |
|
|
=begin original |
1093 |
|
|
|
1094 |
|
|
First, an easy one. Rather than having debugging code that is |
1095 |
|
|
all-or-nothing, it would be much more useful to be able to control |
1096 |
|
|
which specific blocks of debugging code get included. Try extending the |
1097 |
|
|
syntax for debug blocks to allow each to be identified. The contents of |
1098 |
|
|
the C<DEBUG> environment variable can then be used to control which |
1099 |
|
|
blocks get included. |
1100 |
|
|
|
1101 |
|
|
=end original |
1102 |
|
|
|
1103 |
|
|
まずは簡単なものです。 |
1104 |
|
|
デバッグコードをオールオアナッシングにするのではなく、どのブロックを |
1105 |
|
|
デバッグコードとして使うかを制御できるようにすればもっと便利です。 |
1106 |
|
|
それぞれのデバッグブロックを識別できるように文法に文法を |
1107 |
|
|
拡張してみてください。 |
1108 |
|
|
C<DEBUG> 環境変数の内容はどのブロックを使うかを制御するのに使えます。 |
1109 |
|
|
|
1110 |
|
|
=begin original |
1111 |
|
|
|
1112 |
|
|
Once you can identify individual blocks, try allowing them to be |
1113 |
|
|
nested. That isn't difficult either. |
1114 |
|
|
|
1115 |
|
|
=end original |
1116 |
|
|
|
1117 |
|
|
個々のブロックを識別できるようになったら、ネストできるように |
1118 |
|
|
してみてください。 |
1119 |
|
|
これも難しくはありません。 |
1120 |
|
|
|
1121 |
|
|
=begin original |
1122 |
|
|
|
1123 |
|
|
Here is an interesting idea that doesn't involve the Debug filter. |
1124 |
|
|
Currently Perl subroutines have fairly limited support for formal |
1125 |
|
|
parameter lists. You can specify the number of parameters and their |
1126 |
|
|
type, but you still have to manually take them out of the C<@_> array |
1127 |
|
|
yourself. Write a source filter that allows you to have a named |
1128 |
|
|
parameter list. Such a filter would turn this: |
1129 |
|
|
|
1130 |
|
|
=end original |
1131 |
|
|
|
1132 |
|
|
これはデバッグフィルタに関係ない面白いアイデアです。 |
1133 |
|
|
今のところ Perl サブルーチンは公式な引数リストに限定的に対応しています。 |
1134 |
|
|
パラメータの数とその型は指定できますが、自力で C<@_> 配列から取り出す |
1135 |
|
|
必要があります。 |
1136 |
|
|
名前付き引数リストを使えるようなソースフィルタを書きましょう。 |
1137 |
|
|
そのようなフィルタは以下のようなものを: |
1138 |
|
|
|
1139 |
|
|
sub MySub ($first, $second, @rest) { ... } |
1140 |
|
|
|
1141 |
|
|
=begin original |
1142 |
|
|
|
1143 |
|
|
into this: |
1144 |
|
|
|
1145 |
|
|
=end original |
1146 |
|
|
|
1147 |
|
|
次のように変更します: |
1148 |
|
|
|
1149 |
|
|
sub MySub($$@) { |
1150 |
|
|
my ($first) = shift; |
1151 |
|
|
my ($second) = shift; |
1152 |
|
|
my (@rest) = @_; |
1153 |
|
|
... |
1154 |
|
|
} |
1155 |
|
|
|
1156 |
|
|
=begin original |
1157 |
|
|
|
1158 |
|
|
Finally, if you feel like a real challenge, have a go at writing a |
1159 |
|
|
full-blown Perl macro preprocessor as a source filter. Borrow the |
1160 |
|
|
useful features from the C preprocessor and any other macro processors |
1161 |
|
|
you know. The tricky bit will be choosing how much knowledge of Perl's |
1162 |
|
|
syntax you want your filter to have. |
1163 |
|
|
|
1164 |
|
|
=end original |
1165 |
|
|
|
1166 |
|
|
最後に、本当の挑戦を好むなら、本格的な Perl マクロプリプロセッサを |
1167 |
|
|
ソースフィルタとして書いてみてください。 |
1168 |
|
|
C プリプロセッサやその他のマクロプロセッサから便利な機能を |
1169 |
|
|
借りてきてください。 |
1170 |
|
|
トリッキーなところは、Perl の文法のどれくらいの知識をフィルタに持たせるかを |
1171 |
|
|
選ぶところです。 |
1172 |
|
|
|
1173 |
|
|
=head1 THINGS TO LOOK OUT FOR |
1174 |
|
|
|
1175 |
|
|
(注意するべきこと) |
1176 |
|
|
|
1177 |
|
|
=over 5 |
1178 |
|
|
|
1179 |
|
|
=item Some Filters Clobber the C<DATA> Handle |
1180 |
|
|
|
1181 |
|
|
(一部のフィルタは C<DATA> ハンドルを上書きします) |
1182 |
|
|
|
1183 |
|
|
=begin original |
1184 |
|
|
|
1185 |
|
|
Some source filters use the C<DATA> handle to read the calling program. |
1186 |
|
|
When using these source filters you cannot rely on this handle, nor expect |
1187 |
|
|
any particular kind of behavior when operating on it. Filters based on |
1188 |
|
|
Filter::Util::Call (and therefore Filter::Simple) do not alter the C<DATA> |
1189 |
|
|
filehandle. |
1190 |
|
|
|
1191 |
|
|
=end original |
1192 |
|
|
|
1193 |
|
|
一部のソースフィルタは、呼び出したプログラムを読み込むために |
1194 |
|
|
C<DATA> ハンドルを使います。 |
1195 |
|
|
これらのソースフィルタを使うときには、このハンドルに依存したり、これを |
1196 |
|
|
操作したときに何らかの特定の振る舞いを想定できません。 |
1197 |
|
|
Filter::Util::Call (従って Filter::Simple) を基礎としたフィルタは |
1198 |
|
|
C<DATA> ファイルハンドルを変更しません。 |
1199 |
|
|
|
1200 |
|
|
=back |
1201 |
|
|
|
1202 |
|
|
=head1 REQUIREMENTS |
1203 |
|
|
|
1204 |
|
|
(必要なもの) |
1205 |
|
|
|
1206 |
|
|
=begin original |
1207 |
|
|
|
1208 |
|
|
The Source Filters distribution is available on CPAN, in |
1209 |
|
|
|
1210 |
|
|
=end original |
1211 |
|
|
|
1212 |
|
|
ソースフィルタディストリビューションは CPAN の以下から利用可能です |
1213 |
|
|
|
1214 |
|
|
CPAN/modules/by-module/Filter |
1215 |
|
|
|
1216 |
|
|
=begin original |
1217 |
|
|
|
1218 |
|
|
Starting from Perl 5.8 Filter::Util::Call (the core part of the |
1219 |
|
|
Source Filters distribution) is part of the standard Perl distribution. |
1220 |
|
|
Also included is a friendlier interface called Filter::Simple, by |
1221 |
|
|
Damian Conway. |
1222 |
|
|
|
1223 |
|
|
=end original |
1224 |
|
|
|
1225 |
|
|
Perl 5.8 から、Filter::Util::Call (ソースフィルタ配布のコアの部分) は |
1226 |
|
|
標準 Perl 配布の一部です。 |
1227 |
|
|
また、Damian Conway によるより親しみやすいインターフェースである |
1228 |
|
|
Filter::Simple も含まれています。 |
1229 |
|
|
|
1230 |
|
|
=head1 AUTHOR |
1231 |
|
|
|
1232 |
|
|
Paul Marquess E<lt>Paul.Marquess@btinternet.comE<gt> |
1233 |
|
|
|
1234 |
|
|
=head1 Copyrights |
1235 |
|
|
|
1236 |
|
|
This article originally appeared in The Perl Journal #11, and is |
1237 |
|
|
copyright 1998 The Perl Journal. It appears courtesy of Jon Orwant and |
1238 |
|
|
The Perl Journal. This document may be distributed under the same terms |
1239 |
|
|
as Perl itself. |
1240 |
|
|
|
1241 |
|
|
=begin meta |
1242 |
|
|
|
1243 |
|
|
Translate: SHIRAKATA Kentaro <argrath@ub32.org> |
1244 |
|
|
Status: completed |
1245 |
|
|
|
1246 |
|
|
=end meta |
1247 |
|
|
|