Data-Binary-0.01000755000765000024 012314141025 13324 5ustar00stuartstaff000000000000README100644000765000024 51412314141025 14245 0ustar00stuartstaff000000000000Data-Binary-0.01 This archive contains the distribution Data-Binary, version 0.01: Simple detection of binary versus text in strings This software is Copyright (c) 2014 by Stuart Watt. This is free software, licensed under: The Artistic License 2.0 (GPL Compatible) This README file was generated by Dist::Zilla::Plugin::Readme v5.014. INSTALL100644000765000024 166312314141025 14444 0ustar00stuartstaff000000000000Data-Binary-0.01 This is the Perl distribution Data-Binary. Installing Data-Binary is straightforward. ## Installation with cpanm If you have cpanm, you only need one line: % cpanm Data::Binary If you are installing into a system-wide directory, you may need to pass the "-S" flag to cpanm, which uses sudo to install the module: % cpanm -S Data::Binary ## Installing with the CPAN shell Alternatively, if your CPAN shell is set up, you should just be able to do: % cpan Data::Binary ## Manual installation As a last resort, you can manually install it. Download the tarball, untar it, then build it: % perl Makefile.PL % make && make test Then install it: % make install If you are installing into a system-wide directory, you may need to run: % sudo make install ## Documentation Data-Binary documentation is available as POD. You can run perldoc from a shell to read the documentation: % perldoc Data::Binary META.yml100644000765000024 71112314141025 14635 0ustar00stuartstaff000000000000Data-Binary-0.01--- abstract: 'Simple detection of binary versus text in strings' author: - 'Stuart Watt ' build_requires: {} configure_requires: ExtUtils::MakeMaker: '6.30' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.014, CPAN::Meta::Converter version 2.140640' license: artistic_2 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Data-Binary requires: perl: '5.008000' version: '0.01' MANIFEST100644000765000024 37312314141025 14521 0ustar00stuartstaff000000000000Data-Binary-0.01# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.014. INSTALL MANIFEST META.json META.yml Makefile.PL README changes.txt lib/Data/Binary.pm readme.txt t/1-basic.t t/2-test.t xt/release/pod-coverage.t xt/release/pod-syntax.t META.json100644000765000024 160412314141025 15027 0ustar00stuartstaff000000000000Data-Binary-0.01{ "abstract" : "Simple detection of binary versus text in strings", "author" : [ "Stuart Watt " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 5.014, CPAN::Meta::Converter version 2.140640", "license" : [ "artistic_2" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Data-Binary", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.30" } }, "develop" : { "requires" : { "Pod::Coverage::TrustPod" : "0", "Test::Pod" : "1.41", "Test::Pod::Coverage" : "1.08" } }, "runtime" : { "requires" : { "perl" : "5.008000" } } }, "release_status" : "stable", "version" : "0.01" } readme.txt100644000765000024 14612314141025 15364 0ustar00stuartstaff000000000000Data-Binary-0.01Data::Binary Simple module to detect binary versus text data heuristically, like -T / -B for strings t000755000765000024 012314141025 13510 5ustar00stuartstaff000000000000Data-Binary-0.012-test.t100644000765000024 143012314141025 15151 0ustar00stuartstaff000000000000Data-Binary-0.01/tuse strict; use warnings; use Test::More tests => 8; use Data::Binary qw(is_text is_binary); my $text_test = '12345678' x 100; my $binary1_test = "\f234\f67\cg" x 100; my $binary2_test = "\f234\xff67\x80" x 100; my $binary3_test = "12345678" . "\x00" . $text_test; ok(is_text($text_test), "Plain text is text"); ok(! is_binary($text_test), "Plain text is not binary"); ok(! is_text($binary1_test), "Data >30% control codes is not text"); ok(is_binary($binary1_test), "Data >30% control codes is binary"); ok(! is_text($binary2_test), "Data with high-order bits is not text"); ok(is_binary($binary2_test), "Data with high-order bits is binary"); ok(! is_text($binary3_test), "Data with a single null is not text"); ok(is_binary($binary3_test), "Data with a single null is binary"); 1; changes.txt100644000765000024 5112314141025 15512 0ustar00stuartstaff000000000000Data-Binary-0.010.01 - 23rd March 2014 - Initial release1-basic.t100644000765000024 13312314141025 15231 0ustar00stuartstaff000000000000Data-Binary-0.01/tuse strict; use warnings; use Test::More tests => 1; BEGIN { use_ok 'Data::Binary' } 1; Makefile.PL100644000765000024 174412314141025 15365 0ustar00stuartstaff000000000000Data-Binary-0.01 # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.014. use strict; use warnings; use 5.008000; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Simple detection of binary versus text in strings", "AUTHOR" => "Stuart Watt ", "BUILD_REQUIRES" => {}, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Data-Binary", "EXE_FILES" => [], "LICENSE" => "artistic_2", "NAME" => "Data::Binary", "PREREQ_PM" => {}, "TEST_REQUIRES" => {}, "VERSION" => "0.01", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = (); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Data000755000765000024 012314141025 14664 5ustar00stuartstaff000000000000Data-Binary-0.01/libBinary.pm100644000765000024 431512314141025 16611 0ustar00stuartstaff000000000000Data-Binary-0.01/lib/Datapackage Data::Binary; use strict; use warnings; our $VERSION = 0.01; use base qw(Exporter); use Encode qw(decode_utf8); our @EXPORT_OK = qw(is_text is_binary); sub is_text { my ($string) = @_; if (length($string) > 512) { $string = substr($string, 0, 512); } return '' if (index($string, "\c@") != -1); my $length = length($string); my $odd = ($string =~ tr/\x01\x02\x03\x04\x05\x06\x07\x09\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f//d); # Detecting >=128 and non-UTF-8 is interesting. Note that all UTF-8 >=128 has several bytes with # >=128 set, so a quick test is possible by simply checking if any are >=128. However, the count # from that is typically wrong, if this is binary data, it'll not have been decoded. So we do this # in two steps. my $copy = $string; if (($copy =~ tr[\x80-\xff][]d) > 0) { my $modified = decode_utf8($string, Encode::FB_DEFAULT); my $substitions = ($modified =~ tr/\x{fffd}//d); $odd += $substitions; } return '' if ($odd * 3 > $length); return 1; } sub is_binary { my ($string) = @_; return ! is_text($string); } 1; =head1 NAME Data::Binary - Simple detection of binary versus text in strings =head1 SYNOPSIS use Data::Binary qw(is_text is_binary); my $text = File::Slurp::read_file("test1.doc"); my $is_text = is_text($text); # equivalent to -T "test1.doc" my $is_binary = is_binary($text); # equivalent to -B "test1.doc" =head1 DESCRIPTION This simple module provides string equivalents to the -T / -B operators. Since these only work on file names and file handles, this module provides the same functions but on strings. Note that the actual implementation is currently different, basically because the -T / -B functions are in C/XS, and this module is written in pure Perl. For now, anyway. =head1 FUNCTIONS =head2 is_text($string) Uses the same kind of heuristics in -T, but applies them to a string. Returns true if the string is basically text. =head2 is_binary($string) Uses the same kind of heuristics in -B, but applies them to a string. Returns true if the string is basically binary. =head1 AUTHOR Stuart Watt, stuart@morungos.com =head1 COPYRIGHT Copyright (c) 2014 Stuart Watt. All rights reserved. =cutrelease000755000765000024 012314141025 15320 5ustar00stuartstaff000000000000Data-Binary-0.01/xtpod-syntax.t100644000765000024 22012314141025 17725 0ustar00stuartstaff000000000000Data-Binary-0.01/xt/release#!perl # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use Test::More; use Test::Pod 1.41; all_pod_files_ok(); pod-coverage.t100644000765000024 33412314141025 20200 0ustar00stuartstaff000000000000Data-Binary-0.01/xt/release#!perl # This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. use Test::Pod::Coverage 1.08; use Pod::Coverage::TrustPod; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' });