This file is indexed.

/usr/share/perl5/Data/Validate.pm is in libdata-validate-perl 0.09-1.

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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
package Data::Validate;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

require Exporter;
use AutoLoader 'AUTOLOAD';

use POSIX;
use Scalar::Util qw(looks_like_number);
use Math::BigInt;
use Config;

@ISA = qw(Exporter);



# no functions are exported by default.  See EXPORT_OK
@EXPORT = qw();

@EXPORT_OK = qw(
		is_integer
		is_numeric
		is_hex
		is_oct
		is_between
		is_greater_than
		is_less_than
		is_equal_to
		is_even
		is_odd
		is_alphanumeric
		is_printable
		length_is_between
);

%EXPORT_TAGS = (
		math	=>	[qw(is_integer is_numeric is_hex is_oct is_between is_greater_than is_less_than is_equal_to is_even is_odd)],
		string	=>	[qw(is_equal_to is_alphanumeric is_printable length_is_between)],
);

$VERSION = '0.09';


# No preloads

1;
__END__

=head1 NAME

Data::Validate - common data validation methods

=head1 SYNOPSIS

  use Data::Validate qw(:math);
  
  if(defined(is_integer($suspect))){
  	print "Looks like an integer\n";
  }
  
  my $name = is_alphanumeric($suspect);
  if(defined($name)){
  	print "$name is alphanumeric, and has been untainted\n";
  } else {
  	print "$suspect was not alphanumeric"
  }
  
  # or as an object
  my $v = Data::Validate->new();
  
  die "'foo' is not an integer" unless defined($v->is_integer('foo'));

=head1 DESCRIPTION

This module collects common validation routines to make input validation,
and untainting easier and more readable.  Most of the functions are not much
shorter than their direct perl equivalent (and are much longer in some cases),
but their names make it clear what you're trying to test for.

Almost all functions return an untainted value if the test passes, and undef if
it fails.  This means that you should always check for a defined status explicitly.
Don't assume the return will be true. (e.g. is_integer(0))

The value to test is always the first (and often only) argument.

=head1 FUNCTIONS

=over 4

=cut

# -------------------------------------------------------------------------------

=pod

=item B<new> - constructor for OO usage

  new();

=over 4

=item I<Description>

Returns a Data::Validator object.  This lets you access all the validator function
calls as methods without importing them into your namespace or using the clumsy
Data::Validate::function_name() format.

=item I<Arguments>

None

=item I<Returns>

Returns a Data::Validate object

=back

=cut

sub new{
	my $class = shift;
	
	return bless {}, $class;
}

# -------------------------------------------------------------------------------

=pod

=item B<is_integer> - is the value an integer?

  is_integer($value);

=over 4

=item I<Description>

Returns the untainted number if the test value is an integer, or can be cast to
one without a loss of precision.  (i.e. 1.0 is considered an integer, but 1.0001 is not.)

=item I<Arguments>

=over 4

=item $value

The potential integer to test.

=back

=item I<Returns>

Returns the untainted integer on success, undef on failure.  Note that the return
can be 0, so always check with defined()

=item I<Notes, Exceptions, & Bugs>

Number translation is done by POSIX casting tools (strtol).

=back

=cut

sub is_integer{
	my $self = shift if ref($_[0]); 
	my $value = shift;
	
	return unless defined($value);
	return unless defined(is_numeric($value)); # for efficiency
	
	# see if we can parse it to an number without loss
	my($int, $leftover) = POSIX::strtod($value);
	
	return if $leftover;
	
	# we're having issues testing very large integers.  Math::BigInt
	# can do this for us, but defeats the purpose of being
	# lightweight. So, we're going to try a huristic method to choose
	# how to test for integernesss
	if(!$Config{uselongdouble} && length($int) > 10){
		my $i = Math::BigInt->new($value);
		return unless $i->is_int();
		
		# untaint
		($int) = $i->bstr() =~ /(.+)/;
		return $int;
	}
		
	 
	# shorter integer must be identical to the raw cast
	return unless (($int + 0) == ($value + 0));
	
	# could still be a float at this point.
	return if $value =~ /[^0-9\-]/;
	
	# looks like it really is an integer.  Untaint it and return
	($value) = $int =~ /([\d\-]+)/;
	
	return $value + 0;
}


# -------------------------------------------------------------------------------

=pod

=item B<is_numeric> - is the value numeric?

  is_numeric($value);

=over 4

=item I<Description>

Returns the untainted number if the test value is numeric according to
Perl's own internal rules.  (actually a wrapper on Scalar::Util::looks_like_number)

=item I<Arguments>

=over 4

=item $value

The potential number to test.

=back

=item I<Returns>

Returns the untainted number on success, undef on failure.  Note that the return
can be 0, so always check with defined()

=item I<Notes, Exceptions, & Bugs>

Number translation is done by POSIX casting tools (strtol).

=back

=cut

sub is_numeric{
	my $self = shift if ref($_[0]);
	my $value = shift;
	
	return unless defined($value);
	
	return unless looks_like_number($value);
	
	# looks like it really is a number.  Untaint it and return
	($value) = $value =~ /([\d\.\-+e]+)/;
	
	return $value  + 0;
}


# -------------------------------------------------------------------------------

=pod

=item B<is_hex> - is the value a hex number?

  is_hex($value);

=over 4

=item I<Description>

Returns the untainted number if the test value is a hex number.

=item I<Arguments>

=over 4

=item $value

The potential number to test.

=back

=item I<Returns>

Returns the untainted number on success, undef on failure.  Note that the return
can be 0, so always check with defined()

=item I<Notes, Exceptions, & Bugs>

None

=back

=cut

sub is_hex {
	my $self = shift if ref($_[0]); 
	my $value = shift;
	
	return unless defined $value;
	
	return if $value =~ /[^0-9a-f]/i;
	$value = lc($value);
	
	my $int = hex($value);
	return unless (defined $int);
	my $hex = sprintf "%x", $int;
	return $hex if ($hex eq $value);
	
	# handle zero stripping
	if (my ($z) = $value =~ /^(0+)/) {
		return "$z$hex" if ("$z$hex" eq $value);
	}
	
	return;
}

# -------------------------------------------------------------------------------

=pod

=item B<is_oct> - is the value an octal number?

  is_oct($value);

=over 4

=item I<Description>

Returns the untainted number if the test value is a octal number.

=item I<Arguments>

=over 4

=item $value

The potential number to test.

=back

=item I<Returns>

Returns the untainted number on success, undef on failure.  Note that the return
can be 0, so always check with defined()

=item I<Notes, Exceptions, & Bugs>

None

=back

=cut

sub is_oct {
	my $self = shift if ref($_[0]);
	my $value = shift;
	
	return unless defined $value;
	
	return if $value =~ /[^0-7]/;
		
	my $int = oct($value);
	return unless (defined $int);
	my $oct = sprintf "%o", $int;
	return $oct if ($oct eq $value);
	
	# handle zero stripping
	if (my ($z) = $value =~ /^(0+)/) {
		return "$z$oct" if ("$z$oct" eq $value);
	}
	
	return;
}


# -------------------------------------------------------------------------------

=pod

=item B<is_between> - is the value between two numbers?

  is_between($value, $min, $max);

=over 4

=item I<Description>

Returns the untainted number if the test value is numeric, and falls between
$min and $max inclusive.  Note that either $min or $max can be undef, which 
means 'unlimited'.  i.e. is_between($val, 0, undef) would pass for any number
zero or larger.

=item I<Arguments>

=over 4

=item $value

The potential number to test.

=item $min

The minimum valid value.  Unlimited if set to undef

=item $max

The maximum valid value.  Unlimited if set to undef

=back

=item I<Returns>

Returns the untainted number on success, undef on failure.  Note that the return
can be 0, so always check with defined()


=back

=cut

sub is_between{
	my $self = shift if ref($_[0]);
	my $value = shift;
	my $min = shift;
	my $max = shift;
	
	# must be a number
	my $untainted = is_numeric($value);
	return unless defined($untainted);
	
	# issues with very large numbers.  Fall over to using 
	# arbitrary precisions math.
	if(length($value) > 10){
		
		my $i = Math::BigInt->new($value);
		
		# minimum bound
		if(defined($min)){
			$min = Math::BigInt->new($min);
			return unless $i >= $min;
		}
		
		# maximum bound
		if(defined($max)){
			$max = Math::BigInt->new($max);
			return unless $i <= $max;
		}
		
		# untaint
		($value) = $i->bstr() =~ /(.+)/;
		return $value;
	}
	
	
	# minimum bound
	if(defined($min)){
		return unless $value >= $min;
	}
	
	# maximum bound
	if(defined($max)){
		return unless $value <= $max;
	}
	
	return $untainted;
}


# -------------------------------------------------------------------------------

=pod

=item B<is_greater_than> - is the value greater than a threshold?

  is_greater_than($value, $threshold);

=over 4

=item I<Description>

Returns the untainted number if the test value is numeric, and is greater than
$threshold. (not inclusive)

=item I<Arguments>

=over 4

=item $value

The potential number to test.

=item $threshold

The minimum value (non-inclusive)

=back

=item I<Returns>

Returns the untainted number on success, undef on failure.  Note that the return
can be 0, so always check with defined()


=back

=cut

sub is_greater_than{
	my $self = shift if ref($_[0]);
	my $value = shift;
	my $threshold = shift;
		
	# must be a number
	my $untainted = is_numeric($value);
	return unless defined($untainted);
	
	# threshold must be defined
	return unless defined $threshold;
	
	return unless $value > $threshold;
		
	return $untainted;
}

# -------------------------------------------------------------------------------

=pod

=item B<is_less_than> - is the value less than a threshold?

  is_less_than($value, $threshold);

=over 4

=item I<Description>

Returns the untainted number if the test value is numeric, and is less than
$threshold. (not inclusive)

=item I<Arguments>

=over 4

=item $value

The potential number to test.

=item $threshold

The maximum value (non-inclusive)

=back

=item I<Returns>

Returns the untainted number on success, undef on failure.  Note that the return
can be 0, so always check with defined()


=back

=cut

sub is_less_than{	
	my $self = shift if ref($_[0]);
	my $value = shift;
	my $threshold = shift;
		
	# must be a number
	my $untainted = is_numeric($value);
	return unless defined($untainted);
	
	# threshold must be defined
	return unless defined $threshold;
	
	return unless $value < $threshold;
		
	return $untainted;
}


# -------------------------------------------------------------------------------

=pod

=item B<is_equal_to> - do a string/number neutral ==

  is_equal_to($value, $target);

=over 4

=item I<Description>

Returns the target if $value is equal to it.  Does a math comparison if
both $value and $target are numeric, or a string comparison otherwise. 
Both the $value and $target must be defined to get a true return.  (i.e.
undef != undef)

=item I<Arguments>

=over 4

=item $value

The  value to test.

=item $target

The value to test against

=back

=item I<Returns>

Unlike most validator routines, this one does not necessarily untaint its return value,
it just returns $target.  This has the effect of untainting if the target is a constant or
other clean value.  (i.e. is_equal_to($bar, 'foo')).  Note that the return
can be 0, so always check with defined()


=back

=cut

sub is_equal_to{
	my $self = shift if ref($_[0]);
	my $value = shift;
	my $target = shift;
	
	# value and target must be defined
	return unless defined $value;
	return unless defined $target;
	
	if(defined(is_numeric($value)) && defined(is_numeric($target))){
		return $target if $value == $target;
	} else {
		# string comparison
		return $target if $value eq $target;
	}
	
	return;
}

# -------------------------------------------------------------------------------

=pod

=item B<is_even> - is a number even?

  is_even($value);

=over 4

=item I<Description>

Returns the untainted $value if it's numeric, an integer, and even.

=item I<Arguments>

=over 4

=item $value

The  value to test.

=back

=item I<Returns>

Returns $value (untainted). Note that the return can be 0, so always
check with defined().


=back

=cut

sub is_even{
	my $self = shift if ref($_[0]);
	my $value = shift;
	
	return unless defined(is_numeric($value));
	my $untainted = is_integer($value);
	return unless defined($untainted);
	
	return $untainted unless $value % 2;
	
	return;
}

# -------------------------------------------------------------------------------

=pod

=item B<is_odd> - is a number odd?

  is_odd($value);

=over 4

=item I<Description>

Returns the untainted $value if it's numeric, an integer, and odd.

=item I<Arguments>

=over 4

=item $value

The value to test.

=back

=item I<Returns>

Returns $value (untainted). Note that the return can be 0, so always
check with defined().

=back

=cut

sub is_odd{
	my $self = shift if ref($_[0]);
	my $value = shift;
	
	return unless defined(is_numeric($value));
	my $untainted = is_integer($value);
	return unless defined($untainted);
	
	return $untainted if $value % 2;
	
	return;
}



# -------------------------------------------------------------------------------

=pod

=item B<is_alphanumeric> - does it only contain letters and numbers?

  is_alphanumeric($value);

=over 4

=item I<Description>

Returns the untainted $value if it is defined and only contains letters (upper
or lower case) and numbers.  Also allows an empty string - ''.

=item I<Arguments>

=over 4

=item $value

The value to test.

=back

=item I<Returns>

Returns $value (untainted). Note that the return can be 0, so always
check with defined().

=back

=cut

sub is_alphanumeric{
	my $self = shift if ref($_[0]);
	my $value = shift;
	
	return unless defined($value);
	return '' if $value eq ''; # allow for empty string
	
	my($untainted) = $value =~ /([a-z0-9]+)/i;
	
	return unless defined($untainted);
	return unless $untainted eq $value;
	
	return $untainted;
	
}


# -------------------------------------------------------------------------------

=pod

=item B<is_printable> - does it only contain printable characters?

  is_alphanumeric($value);

=over 4

=item I<Description>

Returns the untainted $value if it is defined and only contains printable characters
as defined by the composite POSIX character class [[:print:][:space:]].  Also allows an empty string - ''.

=item I<Arguments>

=over 4

=item $value

The value to test.

=back

=item I<Returns>

Returns $value (untainted). Note that the return can be 0, so always
check with defined().

=back

=cut

sub is_printable{
	my $self = shift if ref($_[0]);
	my $value = shift;
	
	return unless defined($value);
	return '' if $value eq ''; # allow for empty string
	
	my($untainted) = $value =~ /([[:print:][:space:]]+)/i;
	
	return unless defined($untainted);
	return unless $untainted eq $value;

	return $untainted;
	
}


# -------------------------------------------------------------------------------

=pod

=item B<length_is_between> - is the string length between two limits?

  length_is_between($value, $min, $max);

=over 4

=item I<Description>

Returns $value if it is defined and its length
is between $min and $max inclusive.  Note that this function does not
untaint the value.

If either $min or $max are undefined they are treated as no-limit.

=item I<Arguments>

=over 4

=item $value

The value to test.

=item $min

The minimum length of the string (inclusive).

=item $max

The maximum length of the string (inclusive).

=back

=item I<Returns>

Returns $value.  Note that the return can be 0, so always check with
defined().  The value is not automatically untainted.

=back

=cut

sub length_is_between{
	my $self = shift if ref($_[0]);
	my $value = shift;
	my $min = shift;
	my $max = shift;
	
	return unless defined($value);
	
	if(defined($min)){
		return unless length($value) >= $min;
	}
	
	if(defined($max)){
		return unless length($value) <= $max;
	}
	
	return $value;
	
}


=pod

=back

=head1 AUTHOR

Richard Sonnen <F<sonnen@richardsonnen.com>>.

=head1 COPYRIGHT

Copyright (c) 2004 Richard Sonnen. All rights reserved.

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

=cut