| 1 | package BSON::Decode; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | BSON::Decode - Decode BSON file and return a perl data structure! | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.04 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | A small module to parse BSON file to a perl structure | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $file = shift; | ||||||||||||||||||||||||||||
| 25 | open( my $INF ,$file); | ||||||||||||||||||||||||||||
| 26 | my $bs = BSON::Decode->new($INF); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | say Dumper $bs->fetch(); # read first element | ||||||||||||||||||||||||||||
| 29 | say Dumper $bs->fetch(); # read second element | ||||||||||||||||||||||||||||
| 30 | $bs->rewind();# rewind the filehandle | ||||||||||||||||||||||||||||
| 31 | say Dumper $bs->fetch_all(); # read all element in an ARRAY | ||||||||||||||||||||||||||||
| 32 | close $INF; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | This is a pure perl BSON decoder. It is simple and without any dependencies. | ||||||||||||||||||||||||||||
| 41 | (It was mandatory to allow use in an embeded system) | ||||||||||||||||||||||||||||
| 42 | The main use is to compare 2 BSON files | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Some of the BSON grammar's element are not implemented | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | \x06 Undefined (value) Deprecated | ||||||||||||||||||||||||||||
| 47 | \x0C DBPointer Deprecated | ||||||||||||||||||||||||||||
| 48 | \x0E Deprecated | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | For binary content (\x05) | ||||||||||||||||||||||||||||
| 51 | The data is uuencoded as value of the subtype. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 METHOD | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 new | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Instanciate the parser. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | By default the parser read from STDIN (unix filter). | ||||||||||||||||||||||||||||
| 62 | In that case no rewind is possible. Only sequential read. | ||||||||||||||||||||||||||||
| 63 | my $bs = BSON::Decode->new(); | ||||||||||||||||||||||||||||
| 64 | It is possible to pass an argument to the constructor to chose another source. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head3 filehandle | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | an open filehandle from where to read the BSON data: | ||||||||||||||||||||||||||||
| 69 | open( my $INF ,$file); | ||||||||||||||||||||||||||||
| 70 | my $bs = BSON::Decode->new($INF); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head3 file name | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | this a path to an existing BSON file: | ||||||||||||||||||||||||||||
| 77 | my $bs = BSON::Decode->new($file); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head3 scalar | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | a perl scalar with the BSON data: | ||||||||||||||||||||||||||||
| 84 | my $bs = BSON::Decode->new($bson); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub new | ||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 93 | 1 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 94 | 1 | ▶ | $self->{fh} = shift // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 95 | 1 | my $valid = 0; | |||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 1 | local $@ = ""; | |||||||||||||||||||||||||||
| 98 | 1 | my $fd = eval {fileno $self->{fh}}; | |||||||||||||||||||||||||||
| 99 | 1 | ▶ | $valid = !$@ && defined $fd; | ||||||||||||||||||||||||||
|
Condition33%1/3
!$@ && defined $fd
MC/DC0%0/2
!$@ && defined $fd
!$@
defined $fd
Truth table33%1/3
not(A) && B
A= !$@B= defined $fd
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 1 | ▶ | if ( !$valid ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 1 | ▶ | if ( -f $self->{fh} ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | my $f_name = delete $self->{fh}; | |||||||||||||||||||||||||||
| 106 | 0 | open $self->{fh}, '<', $f_name; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | elsif ( length $self->{fh} ) | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 1 | $self->{buffer} = $self->{buffer_bck} = delete $self->{fh}; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else | ||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 0 | $self->{fh} = *STDIN; | |||||||||||||||||||||||||||
| 115 | 0 | $self->{stdin} = 1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 1 | ▶ | binmode( $self->{fh} ) if exists $self->{fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | $self->{func} = { | ||||||||||||||||||||||||||||
| 121 | 1 => sub { ## 64-bit float | ||||||||||||||||||||||||||||
| 122 | 3 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | 3 | my $data = substr( $$bson, 0, 8, '' ); | |||||||||||||||||||||||||||
| 124 | 3 | return unpack( "d", $data ); | |||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||
| 126 | 2 => sub { ## UTF-8 string | ||||||||||||||||||||||||||||
| 127 | 10 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 10 | my $size = unpack( "i", substr( $$bson, 0, 4, '' ) ); | |||||||||||||||||||||||||||
| 129 | 10 | my $data = substr( $$bson, 0, $size - 1, '' ); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 10 | return unpack( "a*", $data ); | |||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||
| 133 | 3 => sub { ## Embedded document | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 135 | 0 | my $size = unpack( "i", substr( $$bson, 0, 4, '' ) ); | |||||||||||||||||||||||||||
| 136 | 0 | return $self->document( $bson, $size - 4 ); | |||||||||||||||||||||||||||
| 137 | }, | ||||||||||||||||||||||||||||
| 138 | 4 => sub { ## Array | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 140 | 0 | my $size = unpack( "i", substr( $$bson, 0, 4, '' ) ); | |||||||||||||||||||||||||||
| 141 | 0 | my $r = $self->document( $bson, $size - 4 ); | |||||||||||||||||||||||||||
| 142 | 0 | my @t; | |||||||||||||||||||||||||||
| 143 | 0 | foreach my $k ( sort keys %$r ) | |||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | push @t, $r->{$k} if $k =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | return \@t; | |||||||||||||||||||||||||||
| 148 | }, | ||||||||||||||||||||||||||||
| 149 | 5 => sub { ## Binary data | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 151 | 0 | my $size = unpack( "i", substr( $$bson, 0, 4, '' ) ); | |||||||||||||||||||||||||||
| 152 | 0 | my $subtype = substr( $$bson, 0, 1, '' ); | |||||||||||||||||||||||||||
| 153 | 0 | my $binary = pack( 'u', substr( $$bson, 0, $size, '' ) ); | |||||||||||||||||||||||||||
| 154 | 0 | return ( $subtype => $binary ); | |||||||||||||||||||||||||||
| 155 | }, | ||||||||||||||||||||||||||||
| 156 | 7 => sub { ## ObjectId | ||||||||||||||||||||||||||||
| 157 | 5 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 158 | 5 | my $data = substr( $$bson, 0, 12, '' ); | |||||||||||||||||||||||||||
| 159 | 5 | return unpack( "H*", $data ); | |||||||||||||||||||||||||||
| 160 | }, | ||||||||||||||||||||||||||||
| 161 | 8 => sub { ## Boolean | ||||||||||||||||||||||||||||
| 162 | 2 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | 2 | my $data = substr( $$bson, 0, 1, '' ); | |||||||||||||||||||||||||||
| 164 | 2 | return oct( "0x" . unpack( "H*", $data ) ); | |||||||||||||||||||||||||||
| 165 | }, | ||||||||||||||||||||||||||||
| 166 | 9 => sub { ## UTC datetime | ||||||||||||||||||||||||||||
| 167 | 3 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 3 | my $data = substr( $$bson, 0, 8, '' ); | |||||||||||||||||||||||||||
| 169 | 3 | return unpack( "q", $data ); | |||||||||||||||||||||||||||
| 170 | }, | ||||||||||||||||||||||||||||
| 171 | 10 => sub { ## Null value | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 173 | 0 | return 'null'; | |||||||||||||||||||||||||||
| 174 | }, | ||||||||||||||||||||||||||||
| 175 | 11 => sub { ## REGEX | ||||||||||||||||||||||||||||
| 176 | 2 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 177 | 2 | my $regex = unpack( "Z*", $$bson ); | |||||||||||||||||||||||||||
| 178 | 2 | substr $$bson, 0, length( $regex ) + 1, ''; | |||||||||||||||||||||||||||
| 179 | 2 | my $regex_options = unpack( "Z*", $$bson ); | |||||||||||||||||||||||||||
| 180 | 2 | substr $$bson, 0, length( $regex_options ) + 1, ''; | |||||||||||||||||||||||||||
| 181 | 2 | return '/' . $regex . '/' . $regex_options; | |||||||||||||||||||||||||||
| 182 | }, | ||||||||||||||||||||||||||||
| 183 | 13 => sub { ## javascript code | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 185 | 0 | my $size = unpack( "i", substr( $$bson, 0, 4, '' ) ); | |||||||||||||||||||||||||||
| 186 | 0 | my $data = substr( $$bson, 0, $size - 1, '' ); | |||||||||||||||||||||||||||
| 187 | 0 | return unpack( "a*", $data ); | |||||||||||||||||||||||||||
| 188 | }, | ||||||||||||||||||||||||||||
| 189 | 16 => sub { ## 32-bit integer | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 191 | 0 | my $data = substr( $$bson, 0, 4, '' ); | |||||||||||||||||||||||||||
| 192 | 0 | return unpack( "i", $data ); | |||||||||||||||||||||||||||
| 193 | }, | ||||||||||||||||||||||||||||
| 194 | 17 => sub { ## timestamp | ||||||||||||||||||||||||||||
| 195 | 2 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 196 | 2 | my $data = substr( $$bson, 0, 8, '' ); | |||||||||||||||||||||||||||
| 197 | 2 | return unpack( "Q", $data ); | |||||||||||||||||||||||||||
| 198 | }, | ||||||||||||||||||||||||||||
| 199 | 18 => sub { ## 64-bit integer | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 201 | 0 | my $data = substr( $$bson, 0, 8, '' ); | |||||||||||||||||||||||||||
| 202 | 0 | return unpack( "Q", $data ); | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | }, | ||||||||||||||||||||||||||||
| 205 | 19 => sub { ## 128-bit integer | ||||||||||||||||||||||||||||
| 206 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 207 | 0 | my $data = substr( $$bson, 0, 16, '' ); | |||||||||||||||||||||||||||
| 208 | 0 | return unpack( "Q", $data ); | |||||||||||||||||||||||||||
| 209 | }, | ||||||||||||||||||||||||||||
| 210 | 127 => sub { ## MAX key | ||||||||||||||||||||||||||||
| 211 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 212 | 0 | return 'maxkey'; | |||||||||||||||||||||||||||
| 213 | }, | ||||||||||||||||||||||||||||
| 214 | 255 => sub { ## MIN key | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my ( $bson, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 216 | 0 | return 'minkey'; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 1 | }; | |||||||||||||||||||||||||||
| 219 | 1 | return $self; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 fetch_all | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Parse the BSON data and return an ARRAY with each element in a perl structure. | ||||||||||||||||||||||||||||
| 225 | The parsing is done from the current position. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =cut | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | sub fetch_all | ||||||||||||||||||||||||||||
| 230 | { | ||||||||||||||||||||||||||||
| 231 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fetch_all: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 1 | my @all; | |||||||||||||||||||||||||||
| 233 | 1 | ▶ | if ( $self->{fh} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | { | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | push @all, $self->fetch() while ( !eof $self->{fh} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | else | ||||||||||||||||||||||||||||
| 238 | { | ||||||||||||||||||||||||||||
| 239 | 1 | ▶ | push @all, $self->fetch() while ( $self->{buffer} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 1 | return \@all; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 fetch | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Parse the BSON data and return the next element in a perl structure. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =cut | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | sub fetch | ||||||||||||||||||||||||||||
| 251 | { | ||||||||||||||||||||||||||||
| 252 | 5 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 5 | my $res; | |||||||||||||||||||||||||||
| 254 | my $sizebits; | ||||||||||||||||||||||||||||
| 255 | 5 | my $bson; | |||||||||||||||||||||||||||
| 256 | 5 | my $size; | |||||||||||||||||||||||||||
| 257 | 5 | ▶ | if ( $self->{fh} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||
| 259 | 0 | my $n = read( $self->{fh}, $sizebits, 4 ); | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | carp "error reading size\n" if ( $n != 4 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 0 | $size = unpack( "i", $sizebits ); | |||||||||||||||||||||||||||
| 263 | 0 | $size -= 4; # -4 because the size includes itself | |||||||||||||||||||||||||||
| 264 | 0 | $n = read( $self->{fh}, $bson, $size ); | |||||||||||||||||||||||||||
| 265 | 0 | ▶ | carp "error reading bson string\n" if ( $n != $size ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | else | ||||||||||||||||||||||||||||
| 268 | { | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 5 | $sizebits = substr( $self->{buffer}, 0, 4, '' ); | |||||||||||||||||||||||||||
| 271 | 5 | $size = unpack( "i", $sizebits ); | |||||||||||||||||||||||||||
| 272 | 5 | $size -= 4; | |||||||||||||||||||||||||||
| 273 | 5 | $bson = substr( $self->{buffer}, 0, $size, '' ); | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | 5 | ▶ | if ( length $bson ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | { | ||||||||||||||||||||||||||||
| 277 | 5 | my $sep = substr( $bson, -1, 1 ); | |||||||||||||||||||||||||||
| 278 | 5 | ▶ | croak( "Bad record seperator '".unpack( "H*",$sep)."'" ) if ( $sep ne "\x00" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 5 | $res = $self->document( \$bson, $size ); | |||||||||||||||||||||||||||
| 281 | 5 | return $res; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head2 rewind | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Rewind the file descriptor ( or buffer ). | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | sub rewind | ||||||||||||||||||||||||||||
| 291 | { | ||||||||||||||||||||||||||||
| 292 | 2 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 293 | 2 | ▶ | if ( $self->{stdin} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | { | ||||||||||||||||||||||||||||
| 295 | 0 | carp( "No rewind for STDIN" ); | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | else | ||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||
| 299 | 2 | ▶ | if ( exists $self->{fh} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | { | ||||||||||||||||||||||||||||
| 301 | 0 | seek( $self->{fh}, 0, 0 ); | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | else | ||||||||||||||||||||||||||||
| 304 | { | ||||||||||||||||||||||||||||
| 305 | 2 | $self->{buffer} = $self->{buffer_bck}; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 2 | return; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | sub document | ||||||||||||||||||||||||||||
| 312 | { | ||||||||||||||||||||||||||||
| 313 | 5 | ▶ | my ( $self, $str, $size ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
document: called CC=6 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 314 | 5 | my $res = {}; | |||||||||||||||||||||||||||
| 315 | 5 | my $bson = substr( $$str, 0, $size, '' ); | |||||||||||||||||||||||||||
| 316 | 5 | ▶ | if ( length( $bson ) != $size ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | { | ||||||||||||||||||||||||||||
| 318 | 0 | die "error reading bson string " . length( $bson ) . " != $size\n"; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | 5 | my $sep = substr( $bson, -1, 1 ); | |||||||||||||||||||||||||||
| 321 | 5 | ▶ | if ( $sep ne "\x00" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | { | ||||||||||||||||||||||||||||
| 323 | 0 | die( "Bad record seperator '$sep'" ); | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | 5 | ▶ | while ( length( $bson ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | { | ||||||||||||||||||||||||||||
| 327 | 42 | my $element = oct( "0x" . unpack( "H*", substr( $bson, 0, 1, '' ) ) ); | |||||||||||||||||||||||||||
| 328 | 42 | my $name; | |||||||||||||||||||||||||||
| 329 | 42 | ▶ | next if ( $element == 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 27 | $name = unpack( "Z*", $bson ); | |||||||||||||||||||||||||||
| 331 | 27 | substr $bson, 0, length( $name ) + 1, ''; | |||||||||||||||||||||||||||
| 332 | 27 | ▶ | if ( exists $self->{func}{$element} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||
| 334 | 27 | $res->{$name} = $self->{func}{$element}->( \$bson, $name ); | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | else | ||||||||||||||||||||||||||||
| 337 | { | ||||||||||||||||||||||||||||
| 338 | 0 | warn "Type $element not implemented for $name"; | |||||||||||||||||||||||||||
| 339 | 0 | last; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | 5 | return $res; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | sub delete_hash_deep | ||||||||||||||||||||||||||||
| 347 | { | ||||||||||||||||||||||||||||
| 348 | 0 | ▶ | my ( $hash, $allowed_keys ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete_hash_deep: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if ( ref( $hash ) && ref( $hash ) eq "ARRAY" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $hash and ref $hash eq "ARRAY"
MC/DC0%0/2
ref $hash and ref $hash eq "ARRAY"
ref $hash
ref $hash eq "ARRAY"
Truth table0%0/3
A and B
A= ref $hashB= ref $hash eq "ARRAY"
| |||||||||||||||||||||||||||||
| 351 | { | ||||||||||||||||||||||||||||
| 352 | 0 | foreach my $h ( @$hash ) | |||||||||||||||||||||||||||
| 353 | { | ||||||||||||||||||||||||||||
| 354 | 0 | delete_hash_deep( $h, $allowed_keys ); | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | else | ||||||||||||||||||||||||||||
| 358 | { | ||||||||||||||||||||||||||||
| 359 | 0 | ▶ | if ( ref( $hash ) eq 'HASH' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | { | ||||||||||||||||||||||||||||
| 361 | 0 | foreach my $k ( keys %{$hash} ) | |||||||||||||||||||||||||||
| 362 | { | ||||||||||||||||||||||||||||
| 363 | 0 | ▶ | if ( ( grep {$k =~ /$_/} @{$allowed_keys} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | { | ||||||||||||||||||||||||||||
| 365 | 0 | delete $hash->{$k}; | |||||||||||||||||||||||||||
| 366 | 0 | next; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | else | ||||||||||||||||||||||||||||
| 369 | { | ||||||||||||||||||||||||||||
| 370 | 0 | ▶ | if ( ref( $hash->{$k} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | { | ||||||||||||||||||||||||||||
| 372 | 0 | ▶ | if ( ref( $hash->{$k} ) eq "HASH" || ref( $hash->{$k} ) eq "ARRAY" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $hash->{$k} eq "HASH" or ref $hash->{$k} eq "ARRAY"
MC/DC0%0/2
ref $hash->{$k} eq "HASH" or ref $hash->{$k} eq "ARRAY"
ref $hash->{$k} eq "HASH"
ref $hash->{$k} eq "ARRAY"
Truth table0%0/3
A or B
A= ref $hash->{$k} eq "HASH"B= ref $hash->{$k} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 373 | { | ||||||||||||||||||||||||||||
| 374 | 0 | delete_hash_deep( $hash->{$k}, $allowed_keys ); | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | DULAUNOY Fabrice, C<< <fabrice at dulaunoy.com> >> | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head1 BUGS | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | Please report any bugs or feature requests to C<bug-bson-decode at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 391 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=BSON-Decode>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 392 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | perldoc BSON::Decode | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | You can also look for information at: | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =over 4 | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=BSON-Decode> | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | L<http://annocpan.org/dist/BSON-Decode> | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | L<http://cpanratings.perl.org/d/BSON-Decode> | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | L<http://search.cpan.org/dist/BSON-Decode/> | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =back | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | Copyright 2016 DULAUNOY Fabrice. | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 435 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 436 | copy of the full license at: | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||||||||||||||||
| 441 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||||||||||||||||
| 442 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||||||||||||||||
| 443 | or distribute the Package, if you do not accept this license. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||||||||||||||||
| 446 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||||||||||||||||
| 447 | your Modified Version complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | This license does not grant you the right to use any trademark, service | ||||||||||||||||||||||||||||
| 450 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||||||||||||||||
| 453 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||||||||||||||||
| 454 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||||||||||||||||
| 455 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||||||||||||||||
| 456 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||||||||||||||||
| 457 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||||||||||||||||
| 458 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||||||||||||||||
| 459 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||||||||||||||||
| 462 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||||||||||||||||
| 463 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||
| 464 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||||||||||||||||
| 465 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||||||||||||||||
| 466 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||||||||||||||||
| 467 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||||||||||||||||
| 468 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 2 | 1; # End of BSON::Decode | |||||||||||||||||||||||||||