-
Notifications
You must be signed in to change notification settings - Fork 8
/
BSON.pm
321 lines (206 loc) · 6.48 KB
/
BSON.pm
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
class BSON:ver<0.2>;
use BSON::ObjectId;
method encode ( %h ) {
return self._document( %h );
}
method decode ( Buf $b ) {
return self._document( $b.list );
}
# BSON Document
# document ::= int32 e_list "\x00"
# The int32 is the total number of bytes comprising the document.
multi method _document ( %h ) {
my $l = self._e_list( %h.pairs );
return self._int32( $l.elems + 5 ) ~ $l ~ Buf.new( 0x00 );
}
multi method _document ( Array $a ) {
my $s = $a.elems;
my $i = self._int32( $a );
my %h = self._e_list( $a );
die 'Parse error' unless $a.shift ~~ 0x00;
die 'Parse error' unless $s ~~ $a.elems + $i;
return %h;
}
# Sequence of elements
# e_list ::= element e_list
# | ""
multi method _e_list ( @p ) {
my Buf $b = Buf.new( );
for @p -> $p {
$b = $b ~ self._element( $p );
}
return $b;
}
multi method _e_list ( Array $a ) {
my @p;
while $a[ 0 ] !~~ 0x00 {
push @p, self._element( $a );
}
return @p;
}
multi method _element ( Pair $p ) {
given $p.value {
when not .defined {
# Null value
# "\x0A" e_name
return Buf.new( 0x0A ) ~ self._e_name( $p.key );
}
when Str {
# UTF-8 string
# "\x02" e_name string
return Buf.new( 0x02 ) ~ self._e_name( $p.key ) ~ self._string( $p.value );
}
when Int {
# 32-bit Integer
# "\x10" e_name int32
return Buf.new( 0x10 ) ~ self._e_name( $p.key ) ~ self._int32( $p.value );
}
when Bool {
if .Bool {
# Boolean "true"
# "\x08" e_name "\x01
return Buf.new( 0x08 ) ~ self._e_name( $p.key ) ~ Buf.new( 0x01 );
}
else {
# Boolean "false"
# "\x08" e_name "\x00
return Buf.new( 0x08 ) ~ self._e_name( $p.key ) ~ Buf.new( 0x00 );
}
}
when Array {
# Array
# "\x04" e_name document
# The document for an array is a normal BSON document
# with integer values for the keys,
# starting with 0 and continuing sequentially.
# For example, the array ['red', 'blue']
# would be encoded as the document {'0': 'red', '1': 'blue'}.
# The keys must be in ascending numerical order.
my %h = .kv;
return Buf.new( 0x04 ) ~ self._e_name( $p.key ) ~ self._document( %h );
}
when Hash {
# Embedded document
# "\x03" e_name document
return Buf.new( 0x03 ) ~ self._e_name( $p.key ) ~ self._document( $_ );
}
when BSON::ObjectId {
# ObjectId
# "\x07" e_name (byte*12)
return Buf.new( 0x07 ) ~ self._e_name( $p.key ) ~ .Buf;
}
default {
die 'Sorry, not yet supported type: ' ~ .WHAT;
}
}
}
multi method _element ( Array $a ) {
given $a.shift {
when 0x0A {
# Null value
# "\x0A" e_name
return self._e_name( $a ) => Any;
}
when 0x02 {
# UTF-8 string
# "\x02" e_name string
return self._e_name( $a ) => self._string( $a );
}
when 0x10 {
# 32-bit Integer
# "\x10" e_name int32
return self._e_name( $a ) => self._int32( $a );
}
when 0x08 {
my $n = self._e_name( $a );
given $a.shift {
when 0x01 {
# Boolean "true"
# "\x08" e_name "\x01
return $n => Bool::True;
}
when 0x00 {
# Boolean "false"
# "\x08" e_name "\x00
return $n => Bool::False;
}
default {
die 'Parse error';
}
}
}
when 0x04 {
# Array
# "\x04" e_name document
# The document for an array is a normal BSON document
# with integer values for the keys,
# starting with 0 and continuing sequentially.
# For example, the array ['red', 'blue']
# would be encoded as the document {'0': 'red', '1': 'blue'}.
# The keys must be in ascending numerical order.
return self._e_name( $a ) => [ self._document( $a ).values ];
}
when 0x03 {
# Embedded document
# "\x03" e_name document
return self._e_name( $a ) => self._document( $a );
}
when 0x07 {
# ObjectId
# "\x07" e_name (byte*12)
my $n = self._e_name( $a );
my @a;
@a.push( $a.shift ) for ^ 12;
return $n => BSON::ObjectId.new( Buf.new( @a ) );
}
default {
die 'Sorry, not yet supported type: ' ~ $_;
}
}
}
# 4 bytes (32-bit signed integer)
multi method _int32 ( Int $i ) {
return Buf.new( $i % 0x100, $i +> 0x08 % 0x100, $i +> 0x10 % 0x100, $i +> 0x18 % 0x100 );
}
multi method _int32 ( Array $a ) {
return [+] $a.shift, $a.shift +< 0x08, $a.shift +< 0x10, $a.shift +< 0x18;
}
# Key name
# e_name ::= cstring
multi method _e_name ( Str $s ) {
return self._cstring( $s );
}
multi method _e_name ( Array $a ) {
return self._cstring( $a );
}
# String
# string ::= int32 (byte*) "\x00"
# The int32 is the number bytes in the (byte*) + 1 (for the trailing '\x00').
# The (byte*) is zero or more UTF-8 encoded characters.
multi method _string ( Str $s ) {
my $b = $s.encode( 'UTF-8' );
return self._int32( $b.bytes + 1 ) ~ $b ~ Buf.new( 0x00 );
}
multi method _string ( Array $a ) {
my $i = self._int32( $a );
my @a;
@a.push( $a.shift ) for ^ ( $i - 1 );
die 'Parse error' unless $a.shift ~~ 0x00;
return Buf.new( @a ).decode( );
}
# CString
# cstring ::= (byte*) "\x00"
# Zero or more modified UTF-8 encoded characters followed by '\x00'.
# The (byte*) MUST NOT contain '\x00', hence it is not full UTF-8.
multi method _cstring ( Str $s ) {
die "Forbidden 0x00 sequence in $s" if $s ~~ /\x00/;
return $s.encode( ) ~ Buf.new( 0x00 );
}
multi method _cstring ( Array $a ) {
my @a;
while $a[ 0 ] !~~ 0x00 {
@a.push( $a.shift );
}
die 'Parse error' unless $a.shift ~~ 0x00;
return Buf.new( @a ).decode( );
}