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