@@ -102,6 +102,18 @@ values don't exist they will be created automatically, so for instance you can
102
102
assign into ` dst.addr ` and ` dst.port ` and the ` dst ` key in the variable stash
103
103
will be ` { addr : x, port : y } ` afterwards.
104
104
105
+ ``` js
106
+ var vars = binary .parse (new Buffer ([5 , 0 , 80 , 11 , 184 ]))
107
+ .word8lu (' count' )
108
+ .word16lu (' ports.src' )
109
+ .word16bu (' ports.dst' )
110
+ .vars ;
111
+ console .log (vars)
112
+
113
+ // { count: 5, ports: { src: 80, dst: 3000 } }
114
+ ```
115
+
116
+
105
117
b.buffer(key, size)
106
118
-------------------
107
119
@@ -110,6 +122,16 @@ slice in the variable stash at `key`. If `size` is a string, use the value at
110
122
` vars[size] ` . The key follows the same dotted address rules as the word
111
123
functions.
112
124
125
+ ``` js
126
+ var vars = binary .parse (new Buffer ([4 , 1 , 0 , 1 , 0 ]))
127
+ .word8lu (' dataLength' )
128
+ .buffer (' data' , ' dataLength' )
129
+ .vars ;
130
+ console .log (vars .data )
131
+
132
+ // <Buffer 01 00 01 00>
133
+ ```
134
+
113
135
b.scan(key, buffer)
114
136
-------------------
115
137
@@ -136,32 +158,166 @@ The callback `cb` is provided with the variable stash from all the previous
136
158
actions once they've all finished.
137
159
138
160
You can nest additional actions onto ` this ` inside the callback.
161
+ ``` js
162
+ binary .parse (new Buffer ([4 , 1 , 0 , 1 , 0 ]))
163
+ .word8lu (' dataLength' )
164
+ .tap (function (vars ) {
165
+ var getInt;
166
+ if (vars .dataLength <= 4 ) {
167
+ getInt = " word" + (8 * vars .dataLength ) + " lu" ;
168
+ this [getInt](' data' );
169
+ } else {
170
+ this .skip (vars .dataLength );
171
+ }
172
+ })
173
+ .vars ;
174
+ console .log (vars);
175
+
176
+ // { dataLength: 4, data: 65537 }
177
+ ```
139
178
140
179
b.into(key, cb)
141
180
---------------
142
181
143
182
Like ` .tap() ` , except all nested actions will assign into a ` key ` in the ` vars `
144
183
stash.
145
184
185
+ ``` js
186
+ var vars = binary .parse (new Buffer ([45 , 13 , 8 ]))
187
+ .word8lu (' avg' )
188
+ .into (' stdDev' , function (inner ) {
189
+ // e.g. q8.8 formatted std dev
190
+ this .word8lu (' integer' )
191
+ .word8lu (' decimal' );
192
+ console .log (" inner" , inner)
193
+ })
194
+ .vars ;
195
+ console .log (" outer" , vars);
196
+
197
+ // inner { integer: 13, decimal: 8 }
198
+ // outer { avg: 45, stdDev: { integer: 13, decimal: 8 } }
199
+ ```
200
+
201
+
146
202
b.loop(cb)
147
203
----------
148
204
149
205
Loop, each time calling ` cb(end, vars) ` for function ` end ` and the variable
150
206
stash with ` this ` set to a new chain for nested parsing. The loop terminates
151
207
once ` end ` is called.
152
208
209
+ ``` js
210
+ var entries = [];
211
+ var vars = binary .parse (new Buffer ([3 , 1 , 97 , 3 , 99 , 97 , 116 , 4 , 119 , 105 , 110 , 115 ]))
212
+ .word8lu (' wordCount' )
213
+ .loop (function (end , inner ) {
214
+ this .word8lu (' length' )
215
+ .string (' word' , inner .length )
216
+ .vars ;
217
+ entries .push (this .vars .word .toString (' ascii' ));
218
+ if (entries .length >= inner .wordCount ) {
219
+ end ();
220
+ }
221
+ console .log (" inner" , inner);
222
+ })
223
+ .vars ;
224
+ console .log (" outer" , vars);
225
+ console .log (" entries" , entries);
226
+
227
+ // inner { wordCount: 3, length: 1, word: 'a' }
228
+ // inner { wordCount: 3, length: 3, word: 'cat' }
229
+ // inner { wordCount: 3, length: 4, word: 'wins' }
230
+ // outer { wordCount: 3, length: 4, word: 'wins' }
231
+ // entries [ 'a', 'cat', 'wins' ]
232
+ ```
233
+
234
+ b.string(key, size)
235
+ ---------
236
+
237
+ Read <size > bytes as a utf8 string, or read until end of buffer if size not specified
238
+
239
+ ``` js
240
+ var vars = binary .parse (new Buffer ([97 , 32 , 99 , 97 , 116 , 32 , 119 , 105 , 110 , 115 ]))
241
+ .string (' res' )
242
+ .vars ;
243
+ console .log (vars);
244
+
245
+ // { res: 'a cat wins' }
246
+ ```
247
+
248
+ b.cstring(key, size)
249
+ ---------
250
+
251
+ Read <size > bytes as a null-terminated utf8 string (slices off the null character and anything after it, or last character if no null found)
252
+
253
+ ``` js
254
+ var vars = binary .parse (new Buffer ([97 , 32 , 99 , 97 , 116 , 32 , 119 , 105 , 110 , 115 , 0 ]))
255
+ .cstring (' res' )
256
+ .vars ;
257
+ console .log (vars);
258
+
259
+ // { res: 'a cat wins' }
260
+ ```
261
+
262
+ b.skip(size)
263
+ ---------
264
+
265
+ Skip <size > bytes
266
+
267
+ ``` js
268
+ var vars = binary .parse (new Buffer ([5 , 13 , 80 ]))
269
+ .word8lu (' count' )
270
+ .skip (1 )
271
+ .word8lu (' port' )
272
+ .vars ;
273
+ console .log (vars);
274
+
275
+ // { count: 5, port: 80 }
276
+ ```
277
+
278
+ b.tell()
279
+ ---------
280
+
281
+ Return the current offset in the buffer
282
+
283
+ ``` js
284
+ var pos = binary .parse (new Buffer ([5 , 0 , 80 , 11 , 184 ]))
285
+ .word8lu (' count' )
286
+ .tell ();
287
+ console .log (pos);
288
+
289
+ // 1
290
+ ```
291
+
153
292
b.flush()
154
293
---------
155
294
156
295
Clear the variable stash entirely.
296
+ ``` js
297
+ var pos = binary .parse (new Buffer ([5 , 0 , 80 , 11 , 184 ]))
298
+ .word8lu (' preFlush' )
299
+ .word8lu (' preFlush2)
300
+ .flush()
301
+ .word8lu(' postFlush' )
302
+ .word8lu(' postFlush2' )
303
+ .tap(function (inner) {
304
+ console.log("inner", inner);
305
+ })
306
+ .vars;
307
+
308
+ console.log("outer:", vars);
309
+
310
+ //inner { postFlush: 80, postFlush2: 11 }
311
+ //outer: { preFlush: 5 } //not sure why
312
+ ```
157
313
158
314
installation
159
315
============
160
316
161
- To install with [ npm] ( http://github.com/isaacs /npm ) :
317
+ To install with [npm](http://github.com/npm /npm):
162
318
163
319
```
164
- npm install binary
320
+ npm install binary@Casear/node-binary#0.3.0
165
321
```
166
322
167
323
notes
0 commit comments