This file is indexed.

/usr/share/perl5/XML/XPath/Expr.pm is in libxml-xpath-perl 1.13-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# $Id: Expr.pm,v 1.20 2003/01/26 19:33:24 matt Exp $

package XML::XPath::Expr;
use strict;

sub new {
    my $class = shift;
    my ($pp) = @_;
    bless { predicates => [], pp => $pp }, $class;
}

sub as_string {
    my $self = shift;
    local $^W; # Use of uninitialized value! grrr
    my $string = "(" . $self->{lhs}->as_string;
    $string .= " " . $self->{op} . " " if defined $self->{op};
    $string .= $self->{rhs}->as_string if defined $self->{rhs};
    $string .= ")";
    foreach my $predicate (@{$self->{predicates}}) {
        $string .= "[" . $predicate->as_string . "]";
    }
    return $string;
}

sub as_xml {
    my $self = shift;
    local $^W; # Use of uninitialized value! grrr
    my $string;
    if (defined $self->{op}) {
        $string .= $self->op_xml();
    }
    else {
        $string .= $self->{lhs}->as_xml();
    }
    foreach my $predicate (@{$self->{predicates}}) {
        $string .= "<Predicate>\n" . $predicate->as_xml() . "</Predicate>\n";
    }
    return $string;
}

sub op_xml {
    my $self = shift;
    my $op = $self->{op};

    my $tag;    
    for ($op) {
        /^or$/    && do {
                    $tag = "Or";
                };
        /^and$/    && do {
                    $tag = "And";
                };
        /^=$/    && do {
                    $tag = "Equals";
                };
        /^!=$/    && do {
                    $tag = "NotEquals";
                };
        /^<=$/    && do {
                    $tag = "LessThanOrEquals";
                };
        /^>=$/    && do {
                    $tag = "GreaterThanOrEquals";
                };
        /^>$/    && do {
                    $tag = "GreaterThan";
                };
        /^<$/    && do {
                    $tag = "LessThan";
                };
        /^\+$/    && do {
                    $tag = "Plus";
                };
        /^-$/    && do {
                    $tag = "Minus";
                };
        /^div$/    && do {
                    $tag = "Div";
                };
        /^mod$/    && do {
                    $tag = "Mod";
                };
        /^\*$/    && do {
                    $tag = "Multiply";
                };
        /^\|$/    && do {
                    $tag = "Union";
                };
    }
    
    return "<$tag>\n" . $self->{lhs}->as_xml() . $self->{rhs}->as_xml() . "</$tag>\n";
}

sub set_lhs {
    my $self = shift;
    $self->{lhs} = $_[0];
}

sub set_op {
    my $self = shift;
    $self->{op} = $_[0];
}

sub set_rhs {
    my $self = shift;
    $self->{rhs} = $_[0];
}

sub push_predicate {
    my $self = shift;
    
    die "Only 1 predicate allowed on FilterExpr in W3C XPath 1.0"
            if @{$self->{predicates}};
    
    push @{$self->{predicates}}, $_[0];
}

sub get_lhs { $_[0]->{lhs}; }
sub get_rhs { $_[0]->{rhs}; }
sub get_op { $_[0]->{op}; }

sub evaluate {
    my $self = shift;
    my $node = shift;
    
    # If there's an op, result is result of that op.
    # If no op, just resolve Expr
    
#    warn "Evaluate Expr: ", $self->as_string, "\n";
    
    my $results;
    
    if ($self->{op}) {
        die ("No RHS of ", $self->as_string) unless $self->{rhs};
        $results = $self->op_eval($node);
    }
    else {
        $results = $self->{lhs}->evaluate($node);
    }
    
    if (my @predicates = @{$self->{predicates}}) {
        if (!$results->isa('XML::XPath::NodeSet')) {
            die "Can't have predicates execute on object type: " . ref($results);
        }
        
        # filter initial nodeset by each predicate
        foreach my $predicate (@{$self->{predicates}}) {
            $results = $self->filter_by_predicate($results, $predicate);
        }
    }
    
    return $results;
}

sub op_eval {
    my $self = shift;
    my $node = shift;
    
    my $op = $self->{op};
    
    for ($op) {
        /^or$/    && do {
                    return op_or($node, $self->{lhs}, $self->{rhs});
                };
        /^and$/    && do {
                    return op_and($node, $self->{lhs}, $self->{rhs});
                };
        /^=$/    && do {
                    return op_equals($node, $self->{lhs}, $self->{rhs});
                };
        /^!=$/    && do {
                    return op_nequals($node, $self->{lhs}, $self->{rhs});
                };
        /^<=$/    && do {
                    return op_le($node, $self->{lhs}, $self->{rhs});
                };
        /^>=$/    && do {
                    return op_ge($node, $self->{lhs}, $self->{rhs});
                };
        /^>$/    && do {
                    return op_gt($node, $self->{lhs}, $self->{rhs});
                };
        /^<$/    && do {
                    return op_lt($node, $self->{lhs}, $self->{rhs});
                };
        /^\+$/    && do {
                    return op_plus($node, $self->{lhs}, $self->{rhs});
                };
        /^-$/    && do {
                    return op_minus($node, $self->{lhs}, $self->{rhs});
                };
        /^div$/    && do {
                    return op_div($node, $self->{lhs}, $self->{rhs});
                };
        /^mod$/    && do {
                    return op_mod($node, $self->{lhs}, $self->{rhs});
                };
        /^\*$/    && do {
                    return op_mult($node, $self->{lhs}, $self->{rhs});
                };
        /^\|$/    && do {
                    return op_union($node, $self->{lhs}, $self->{rhs});
                };
        
        die "No such operator, or operator unimplemented in ", $self->as_string, "\n";
    }
}

# Operators

use XML::XPath::Boolean;

sub op_or {
    my ($node, $lhs, $rhs) = @_;
    if($lhs->evaluate($node)->to_boolean->value) {
        return XML::XPath::Boolean->True;
    }
    else {
        return $rhs->evaluate($node)->to_boolean;
    }
}

sub op_and {
    my ($node, $lhs, $rhs) = @_;
    if( ! $lhs->evaluate($node)->to_boolean->value ) {
        return XML::XPath::Boolean->False;
    }
    else {
        return $rhs->evaluate($node)->to_boolean;
    }
}

sub op_equals {
    my ($node, $lhs, $rhs) = @_;

    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    if ($lh_results->isa('XML::XPath::NodeSet') &&
            $rh_results->isa('XML::XPath::NodeSet')) {
        # True if and only if there is a node in the
        # first set and a node in the second set such
        # that the result of performing the comparison
        # on the string-values of the two nodes is true.
        foreach my $lhnode ($lh_results->get_nodelist) {
            foreach my $rhnode ($rh_results->get_nodelist) {
                if ($lhnode->string_value eq $rhnode->string_value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        return XML::XPath::Boolean->False;
    }
    elsif (($lh_results->isa('XML::XPath::NodeSet') ||
            $rh_results->isa('XML::XPath::NodeSet')) &&
            (!$lh_results->isa('XML::XPath::NodeSet') ||
             !$rh_results->isa('XML::XPath::NodeSet'))) {
        # (that says: one is a nodeset, and one is not a nodeset)
        
        my ($nodeset, $other);
        if ($lh_results->isa('XML::XPath::NodeSet')) {
            $nodeset = $lh_results;
            $other = $rh_results;
        }
        else {
            $nodeset = $rh_results;
            $other = $lh_results;
        }
        
        # True if and only if there is a node in the
        # nodeset such that the result of performing
        # the comparison on <type>(string_value($node))
        # is true.
        if ($other->isa('XML::XPath::Number')) {
            foreach my $node ($nodeset->get_nodelist) {
                if ($node->string_value == $other->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        elsif ($other->isa('XML::XPath::Literal')) {
            foreach my $node ($nodeset->get_nodelist) {
                if ($node->string_value eq $other->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        elsif ($other->isa('XML::XPath::Boolean')) {
            if ($nodeset->to_boolean->value == $other->value) {
                return XML::XPath::Boolean->True;
            }
        }

        return XML::XPath::Boolean->False;
    }
    else { # Neither is a nodeset
        if ($lh_results->isa('XML::XPath::Boolean') ||
            $rh_results->isa('XML::XPath::Boolean')) {
            # if either is a boolean
            if ($lh_results->to_boolean->value == $rh_results->to_boolean->value) {
                return XML::XPath::Boolean->True;
            }
            return XML::XPath::Boolean->False;
        }
        elsif ($lh_results->isa('XML::XPath::Number') ||
                $rh_results->isa('XML::XPath::Number')) {
            # if either is a number
            local $^W; # 'number' might result in undef
            if ($lh_results->to_number->value == $rh_results->to_number->value) {
                return XML::XPath::Boolean->True;
            }
            return XML::XPath::Boolean->False;
        }
        else {
            if ($lh_results->to_literal->value eq $rh_results->to_literal->value) {
                return XML::XPath::Boolean->True;
            }
            return XML::XPath::Boolean->False;
        }
    }
}

sub op_nequals {
    my ($node, $lhs, $rhs) = @_;
    if (op_equals($node, $lhs, $rhs)->value) {
        return XML::XPath::Boolean->False;
    }
    return XML::XPath::Boolean->True;
}

sub op_le {
    my ($node, $lhs, $rhs) = @_;
    op_ge($node, $rhs, $lhs);
}

sub op_ge {
    my ($node, $lhs, $rhs) = @_;

    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    if ($lh_results->isa('XML::XPath::NodeSet') &&
        $rh_results->isa('XML::XPath::NodeSet')) {

        foreach my $lhnode ($lh_results->get_nodelist) {
            foreach my $rhnode ($rh_results->get_nodelist) {
                my $lhNum = XML::XPath::Number->new($lhnode->string_value);
                my $rhNum = XML::XPath::Number->new($rhnode->string_value);
                if ($lhNum->value >= $rhNum->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        return XML::XPath::Boolean->False;
    }
    elsif (($lh_results->isa('XML::XPath::NodeSet') ||
            $rh_results->isa('XML::XPath::NodeSet')) &&
            (!$lh_results->isa('XML::XPath::NodeSet') ||
             !$rh_results->isa('XML::XPath::NodeSet'))) {
        # (that says: one is a nodeset, and one is not a nodeset)

        if ($lh_results->isa('XML::XPath::NodeSet')) {
            foreach my $node ($lh_results->get_nodelist) {
                if ($node->to_number->value >= $rh_results->to_number->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        else {
            foreach my $node ($rh_results->get_nodelist) {
                if ( $lh_results->to_number->value >= $node->to_number->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        return XML::XPath::Boolean->False;
    }
    else { # Neither is a nodeset
        if ($lh_results->isa('XML::XPath::Boolean') ||
            $rh_results->isa('XML::XPath::Boolean')) {
            # if either is a boolean
            if ($lh_results->to_boolean->to_number->value
                    >= $rh_results->to_boolean->to_number->value) {
                return XML::XPath::Boolean->True;
            }
        }
        else {
            if ($lh_results->to_number->value >= $rh_results->to_number->value) {
                return XML::XPath::Boolean->True;
            }
        }
        return XML::XPath::Boolean->False;
    }
}

sub op_gt {
    my ($node, $lhs, $rhs) = @_;

    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    if ($lh_results->isa('XML::XPath::NodeSet') &&
        $rh_results->isa('XML::XPath::NodeSet')) {

        foreach my $lhnode ($lh_results->get_nodelist) {
            foreach my $rhnode ($rh_results->get_nodelist) {
                my $lhNum = XML::XPath::Number->new($lhnode->string_value);
                my $rhNum = XML::XPath::Number->new($rhnode->string_value);
                if ($lhNum->value > $rhNum->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        return XML::XPath::Boolean->False;
    }
    elsif (($lh_results->isa('XML::XPath::NodeSet') ||
            $rh_results->isa('XML::XPath::NodeSet')) &&
            (!$lh_results->isa('XML::XPath::NodeSet') ||
             !$rh_results->isa('XML::XPath::NodeSet'))) {
        # (that says: one is a nodeset, and one is not a nodeset)

        if ($lh_results->isa('XML::XPath::NodeSet')) {
            foreach my $node ($lh_results->get_nodelist) {
                if ($node->to_number->value > $rh_results->to_number->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        else {
            foreach my $node ($rh_results->get_nodelist) {
                if ( $lh_results->to_number->value > $node->to_number->value) {
                    return XML::XPath::Boolean->True;
                }
            }
        }
        return XML::XPath::Boolean->False;
    }
    else { # Neither is a nodeset
        if ($lh_results->isa('XML::XPath::Boolean') ||
            $rh_results->isa('XML::XPath::Boolean')) {
            # if either is a boolean
            if ($lh_results->to_boolean->value > $rh_results->to_boolean->value) {
                return XML::XPath::Boolean->True;
            }
        }
        else {
            if ($lh_results->to_number->value > $rh_results->to_number->value) {
                return XML::XPath::Boolean->True;
            }
        }
        return XML::XPath::Boolean->False;
    }
}

sub op_lt {
    my ($node, $lhs, $rhs) = @_;
    op_gt($node, $rhs, $lhs);
}

sub op_plus {
    my ($node, $lhs, $rhs) = @_;
    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    my $result =
        $lh_results->to_number->value
            +
        $rh_results->to_number->value
            ;
    return XML::XPath::Number->new($result);
}

sub op_minus {
    my ($node, $lhs, $rhs) = @_;
    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    my $result =
        $lh_results->to_number->value
            -
        $rh_results->to_number->value
            ;
    return XML::XPath::Number->new($result);
}

sub op_div {
    my ($node, $lhs, $rhs) = @_;
    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);

    my $result = eval {
        $lh_results->to_number->value
            /
        $rh_results->to_number->value
            ;
    };
    if ($@) {
        # assume divide by zero
        # This is probably a terrible way to handle this! 
        # Ah well... who wants to live forever...
        return XML::XPath::Literal->new('Infinity');
    }
    return XML::XPath::Number->new($result);
}

sub op_mod {
    my ($node, $lhs, $rhs) = @_;
    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    my $result =
        $lh_results->to_number->value
            %
        $rh_results->to_number->value
            ;
    return XML::XPath::Number->new($result);
}

sub op_mult {
    my ($node, $lhs, $rhs) = @_;
    my $lh_results = $lhs->evaluate($node);
    my $rh_results = $rhs->evaluate($node);
    
    my $result =
        $lh_results->to_number->value
            *
        $rh_results->to_number->value
            ;
    return XML::XPath::Number->new($result);
}

sub op_union {
    my ($node, $lhs, $rhs) = @_;
    my $lh_result = $lhs->evaluate($node);
    my $rh_result = $rhs->evaluate($node);
    
    if ($lh_result->isa('XML::XPath::NodeSet') &&
            $rh_result->isa('XML::XPath::NodeSet')) {
        my %found;
        my $results = XML::XPath::NodeSet->new;
        foreach my $lhnode ($lh_result->get_nodelist) {
            $found{"$lhnode"}++;
            $results->push($lhnode);
        }
        foreach my $rhnode ($rh_result->get_nodelist) {
            $results->push($rhnode)
                    unless exists $found{"$rhnode"};
        }
                $results->sort;
        return $results;
    }
    die "Both sides of a union must be Node Sets\n";
}

sub filter_by_predicate {
    my $self = shift;
    my ($nodeset, $predicate) = @_;
    
    # See spec section 2.4, paragraphs 2 & 3:
    # For each node in the node-set to be filtered, the predicate Expr
    # is evaluated with that node as the context node, with the number
    # of nodes in the node set as the context size, and with the
    # proximity position of the node in the node set with respect to
    # the axis as the context position.
    
    if (!ref($nodeset)) { # use ref because nodeset has a bool context
        die "No nodeset!!!";
    }
    
#    warn "Filter by predicate: $predicate\n";
    
    my $newset = XML::XPath::NodeSet->new();
    
    for(my $i = 1; $i <= $nodeset->size; $i++) {
        # set context set each time 'cos a loc-path in the expr could change it
        $self->{pp}->set_context_set($nodeset);
        $self->{pp}->set_context_pos($i);
        my $result = $predicate->evaluate($nodeset->get_node($i));
        if ($result->isa('XML::XPath::Boolean')) {
            if ($result->value) {
                $newset->push($nodeset->get_node($i));
            }
        }
        elsif ($result->isa('XML::XPath::Number')) {
            if ($result->value == $i) {
                $newset->push($nodeset->get_node($i));
            }
        }
        else {
            if ($result->to_boolean->value) {
                $newset->push($nodeset->get_node($i));
            }
        }
    }
    
    return $newset;
}

1;