Data-Buffer-0.04/0040755000076400007640000000000007330466130012774 5ustar btrottusersData-Buffer-0.04/README0100644000076400007640000000113307271233375013656 0ustar btrottusers$Id: README,v 1.2 2001/03/22 20:23:03 btrott Exp $ This is Data::Buffer. PREREQUISITES None. INSTALLATION Data::Buffer installation is straightforward. If your cpan shell is set up, you should just be able to do % perl -MCPAN -e 'install Data::Buffer' If you don't like that, you can download the distribution; the latest version on CPAN can be found in ftp://ftp.cpan.org/pub/CPAN/authors/id/B/BT/BTROTT/ Download it, unpack it, then build it as per the usual: % perl Makefile.PL % make && make test Then install it: % make install Benjamin Trott / ben@rhumba.pair.com Data-Buffer-0.04/Makefile.PL0100644000076400007640000000044407271233375014754 0ustar btrottusers# $Id: Makefile.PL,v 1.2 2001/03/22 20:23:03 btrott Exp $ use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Data::Buffer', DISTNAME => 'Data-Buffer', VERSION_FROM => 'Buffer.pm', AUTHOR => 'Benjamin Trott ', ABSTRACT => 'Read/write buffer class', ); Data-Buffer-0.04/Changes0100644000076400007640000000136207330456602014271 0ustar btrottusers$Id: Changes,v 1.6 2001/07/28 06:36:50 btrott Exp $ Revision history for Data::Buffer 0.04 2001.07.27 - Added new_with_init. - Added set_offset and reset_offset. 0.03 2001.07.13 - Fixed bug in get_int8 and put_int8; was using signed char ('c'), should have been using unsigned char ('C'). - Added get_bytes and put_bytes; the first grabs a number of bytes from the buffer and returns them, the latter appends bytes into the buffer. - Added extract, which grabs a piece of the buffer and returns a new buffer containing that chunk, updating the offset in the original buffer. 0.02 2001.05.02 - Fixed number of tests in test suite. 0.01 2001.03.01 - original version; created by h2xs 1.19 Data-Buffer-0.04/MANIFEST0100644000076400007640000000006607330466126014131 0ustar btrottusersBuffer.pm Changes MANIFEST Makefile.PL README test.pl Data-Buffer-0.04/Buffer.pm0100644000076400007640000002713407330456602014552 0ustar btrottusers# $Id: Buffer.pm,v 1.9 2001/07/28 06:36:50 btrott Exp $ package Data::Buffer; use strict; use vars qw( $VERSION ); $VERSION = '0.04'; sub new { my $class = shift; my %arg = @_; bless { buf => "", offset => 0, template => "" }, $class; } sub new_with_init { my $class = shift; my $buf = $class->new; $buf->append($_) for @_; $buf; } sub extract { my $buf = shift; my($nbytes) = @_; my $new = ref($buf)->new; $new->append( $buf->get_bytes($nbytes) ); $new; } sub empty { my $buf = shift; $buf->{buf} = ""; $buf->{offset} = 0; $buf->{template} = ""; } sub set_offset { $_[0]->{offset} = $_[1] } sub reset_offset { $_[0]->set_offset(0) } sub insert_template { my $buf = shift; $buf->bytes(0, 0, $buf->{template} . chr(0)); } sub append { my $buf = shift; $buf->{buf} .= $_[0]; } sub bytes { my $buf = shift; my($off, $len, $rep) = @_; $off ||= 0; $len = length $buf->{buf} unless defined $len; return defined $rep ? substr($buf->{buf}, $off, $len, $rep) : substr($buf->{buf}, $off, $len); } sub length { length $_[0]->{buf} } sub offset { $_[0]->{offset} } sub template { $_[0]->{template} } sub dump { my $buf = shift; my @r; for my $c (split //, $buf->bytes(@_)) { push @r, sprintf "%02x", ord $c; push @r, "\n" unless @r % 24; } join ' ', @r } sub get_all { my $buf = shift; my($tmpl, $data) = $buf->{buf} =~ /^([NYaCn\d]+)\0(.+)$/s or die "Buffer $buf does not appear to contain a template"; my $b = __PACKAGE__->new; $b->append($data); my @tmpl = split //, $tmpl; my @data; while (@tmpl) { my $el = shift @tmpl; if ($el eq "N") { next if $tmpl[0] eq "Y"; ## Peek ahead: is it a string? push @data, $b->get_int32; } elsif ($el eq "n") { push @data, $b->get_int16; } elsif ($el eq "C") { push @data, $b->get_int8; } elsif ($el eq "a") { my $len = shift @tmpl; push @data, $b->get_char for 1..$len; } elsif ($el eq "Y") { push @data, $b->get_str; } else { die "Unrecognized template token: $el"; } } @data; } sub get_int8 { my $buf = shift; my $off = defined $_[0] ? shift : $buf->{offset}; $buf->{offset} += 1; unpack "C", $buf->bytes($off, 1); } sub put_int8 { my $buf = shift; $buf->{buf} .= pack "C", $_[0]; $buf->{template} .= "C"; } sub get_int16 { my $buf = shift; my $off = defined $_[0] ? shift : $buf->{offset}; $buf->{offset} += 2; unpack "n", $buf->bytes($off, 2); } sub put_int16 { my $buf = shift; $buf->{buf} .= pack "n", $_[0]; $buf->{template} .= "n"; } sub get_int32 { my $buf = shift; my $off = defined $_[0] ? shift : $buf->{offset}; $buf->{offset} += 4; unpack "N", $buf->bytes($off, 4); } sub put_int32 { my $buf = shift; $buf->{buf} .= pack "N", $_[0]; $buf->{template} .= "N"; } sub get_char { my $buf = shift; my $off = defined $_[0] ? shift : $buf->{offset}; $buf->{offset}++; $buf->bytes($off, 1); } sub put_char { my $buf = shift; $buf->{buf} .= $_[0]; $buf->{template} .= "a" . CORE::length($_[0]); } sub get_bytes { my $buf = shift; my($nbytes) = @_; my $d = $buf->bytes($buf->{offset}, $nbytes); $buf->{offset} += $nbytes; $d; } sub put_bytes { my $buf = shift; my($str, $nbytes) = @_; $buf->{buf} .= $nbytes ? substr($str, 0, $nbytes) : $str; $buf->{template} .= "a" . ($nbytes ? $nbytes : CORE::length($str)); } *put_chars = \&put_char; sub get_str { my $buf = shift; my $off = defined $_[0] ? shift : $buf->{offset}; my $len = $buf->get_int32; $buf->{offset} += $len; $buf->bytes($off+4, $len); } sub put_str { my $buf = shift; my $str = shift; $str = "" unless defined $str; $buf->put_int32(CORE::length($str)); $buf->{buf} .= $str; $buf->{template} .= "Y"; } 1; __END__ =head1 NAME Data::Buffer - Read/write buffer class =head1 SYNOPSIS use Data::Buffer; my $buffer = Data::Buffer->new; ## Add a 32-bit integer. $buffer->put_int32(10932930); ## Get it back. my $int = $buffer->get_int32; =head1 DESCRIPTION I implements a low-level binary buffer in which you can get and put integers, strings, and other data. Internally the implementation is based on C and C, such that I is really a layer on top of those built-in functions. All of the I and I methods respect the internal offset state in the buffer object. This means that you should read data out of the buffer in the same order that you put it in. For example: $buf->put_int16(24); $buf->put_int32(1233455); $buf->put_int16(99); $buf->get_int16; # 24 $buf->get_int32; # 1233455 $buf->get_int16; # 99 Of course, this assumes that you I the order of the data items in the buffer. If your setup is such that your sending and receiving processes won't necessarily know what's inside the buffers they receive, take a look at the I