Skip to content

Commit

Permalink
Parse and serialize skipped arrays.
Browse files Browse the repository at this point in the history
Closes #438.
Closes #212.
Closes #211.
  • Loading branch information
flatheadmill committed May 29, 2020
1 parent 913fc8a commit 256bb24
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 24 deletions.
2 changes: 1 addition & 1 deletion parse.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function generate (packet) {
return $(`
case ${$step++}:
$_ = ${literal.value.length / 2}
$_ = ${literal.value.length / 2 * literal.repeat}
$step = ${$step}
case ${$step++}:
Expand Down
6 changes: 4 additions & 2 deletions serialize.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ function generate (packet, bff) {
}
}

// TODO You need to test incrementing step correctly when contained variable
// is variable length and not fixed. Not yet implemented.
function literal (path, field) {
function write (literal) {
switch (literal.repeat) {
Expand All @@ -106,9 +108,9 @@ function generate (packet, bff) {
$start += ${literal.value.length / 2}
`)
default:
const i = `$i${$i + 1}]`, I = `$I${$i + 1}]`
variables.i = true
return $(`
for (${i} = 0; ${i} < ${literal.repeat}; i++) {
for ($i[${$i + 1}] = 0; $i[${$i + 1}] < ${literal.repeat}; $i[${$i + 1}]++) {
$buffer.write(${JSON.stringify(literal.value)}, $start, $start + ${literal.value.length / 2}, 'hex')
$start += ${literal.value.length / 2}
}
Expand Down
50 changes: 36 additions & 14 deletions serialize.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,54 @@ function generate (packet) {
for (let i = 0, I = literal.value.length; i < I; i += 2) {
bytes.push(parseInt(literal.value.substring(i, i + 2), 16))
}
return $(`
case ${$step++}:
switch (literal.repeat) {
case 0:
return null
case 1:
return $(`
case ${$step++}:
$step = ${$step}
$bite = 0
$_ = ${JSON.stringify(bytes)}
$step = ${$step}
$bite = 0
$_ = ${JSON.stringify(bytes)}
case ${$step++}:
case ${$step++}:
while ($bite != ${literal.value.length / 2}) {
if ($start == $end) {
return { start: $start, serialize }
while ($bite != ${literal.value.length / 2}) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = $_[$bite++]
}
$buffer[$start++] = $_[$bite++]
}
`)
`)
case 2: {
variables.i = true
const redo = $step
return $(`
case ${$step++}:
$i[${$i + 1}] = 0
`, write({ ...literal, repeat: 1 }), `
case ${$step++}:
if (++$i[${$i + 1}] < ${literal.repeat}) {
$step = ${redo + 1}
continue
}
`)
}
}
}
return $(`
`, write(field.before), 1, `
`, write(field.before), -1, `
`, map(dispatch, path, field.fields), `
`, write(field.after), -1, `
`)
// TODO Remove that line?
}

// TODO I don't need to push and pop $i.
Expand Down
5 changes: 2 additions & 3 deletions test/cycle/skip.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ require('proof')(0, prove)

function prove (okay) {
require('./cycle')(okay, {
name: 'literal',
name: 'skip',
define: {
object: {
padded: [[ '0faded', 2 ], 16, [ 'facade', 2 ]]
}
},
objects: [{ padded: 0xabcd }],
stopAt: 'sizeof'
objects: [{ padded: 0xabcd }]
})
}
12 changes: 8 additions & 4 deletions test/generated/literal.serializer.all.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module.exports = function (serializers) {
serializers.all.object = function (object) {
return function ($buffer, $start, $end) {
$buffer.write("0faded", $start, $start + 3, 'hex')
$start += 3
for ($i0] = 0; $i0] < 2; i++) {
$buffer.write("0faded", $start, $start + 3, 'hex')
$start += 3
}

$buffer[$start++] = object.padded >>> 8 & 0xff
$buffer[$start++] = object.padded & 0xff

$buffer.write("facade", $start, $start + 3, 'hex')
$start += 3
for ($i0] = 0; $i0] < 2; i++) {
$buffer.write("facade", $start, $start + 3, 'hex')
$start += 3
}

return { start: $start, serialize: null }
}
Expand Down
17 changes: 17 additions & 0 deletions test/generated/skip.parser.all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function (parsers) {
parsers.all.object = function ($buffer, $start) {
const object = {
padded: 0
}

$start += 6

object.padded =
$buffer[$start++] * 0x100 +
$buffer[$start++]

$start += 6

return object
}
}
23 changes: 23 additions & 0 deletions test/generated/skip.parser.bff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = function (parsers) {
parsers.bff.object = function () {
return function parse ($buffer, $start, $end) {
const object = {
padded: 0
}

if ($end - $start < 14) {
return parsers.inc.object(object, 1)($buffer, $start, $end)
}

$start += 6

object.padded =
$buffer[$start++] * 0x100 +
$buffer[$start++]

$start += 6

return { start: $start, object: object, parse: null }
}
}
}
69 changes: 69 additions & 0 deletions test/generated/skip.parser.inc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module.exports = function (parsers) {
parsers.inc.object = function (object = {}, $step = 0) {
let $_, $bite
return function parse ($buffer, $start, $end) {
switch ($step) {
case 0:

object = {
padded: 0
}

$step = 1

case 1:

$_ = 6
$step = 2

case 2:

$bite = Math.min($end - $start, $_)
$_ -= $bite
$start += $bite

if ($_ != 0) {
return { start: $start, object: null, parse }
}

case 3:

$_ = 0
$step = 4
$bite = 1

case 4:

while ($bite != -1) {
if ($start == $end) {
return { start: $start, object: null, parse }
}
$_ += $buffer[$start++] << $bite * 8 >>> 0
$bite--
}

object.padded = $_


case 5:

$_ = 6
$step = 6

case 6:

$bite = Math.min($end - $start, $_)
$_ -= $bite
$start += $bite

if ($_ != 0) {
return { start: $start, object: null, parse }
}

case 7:

return { start: $start, object: object, parse: null }
}
}
}
}
22 changes: 22 additions & 0 deletions test/generated/skip.serializer.all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = function (serializers) {
serializers.all.object = function (object) {
return function ($buffer, $start, $end) {
let $i = []

for ($i[0] = 0; $i[0] < 2; $i[0]++) {
$buffer.write("0faded", $start, $start + 3, 'hex')
$start += 3
}

$buffer[$start++] = object.padded >>> 8 & 0xff
$buffer[$start++] = object.padded & 0xff

for ($i[0] = 0; $i[0] < 2; $i[0]++) {
$buffer.write("facade", $start, $start + 3, 'hex')
$start += 3
}

return { start: $start, serialize: null }
}
}
}
29 changes: 29 additions & 0 deletions test/generated/skip.serializer.bff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = function (serializers) {
serializers.bff.object = function (object) {
return function ($buffer, $start, $end) {
let $i = []

if ($end - $start < 14) {
return {
start: $start,
serialize: serializers.inc.object(object, 0, [])
}
}

for ($i[0] = 0; $i[0] < 2; $i[0]++) {
$buffer.write("0faded", $start, $start + 3, 'hex')
$start += 3
}

$buffer[$start++] = object.padded >>> 8 & 0xff
$buffer[$start++] = object.padded & 0xff

for ($i[0] = 0; $i[0] < 2; $i[0]++) {
$buffer.write("facade", $start, $start + 3, 'hex')
$start += 3
}

return { start: $start, serialize: null }
}
}
}
93 changes: 93 additions & 0 deletions test/generated/skip.serializer.inc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
module.exports = function (serializers) {
serializers.inc.object = function (object, $step = 0, $i = []) {
let $bite, $stop, $_

return function serialize ($buffer, $start, $end) {
for (;;) {
switch ($step) {
case 0:

$i[0] = 0

case 1:

$step = 2
$bite = 0
$_ = [15,173,237]

case 2:

while ($bite != 3) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = $_[$bite++]
}


case 3:

if (++$i[0] < 2) {
$step = 1
continue
}

case 4:

$step = 5
$bite = 1
$_ = object.padded

case 5:

while ($bite != -1) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = $_ >>> $bite * 8 & 0xff
$bite--
}


case 6:

$i[0] = 0

case 7:

$step = 8
$bite = 0
$_ = [250,202,222]

case 8:

while ($bite != 3) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = $_[$bite++]
}


case 9:

if (++$i[0] < 2) {
$step = 7
continue
}

$step = 10

case 10:

break

}

break
}

return { start: $start, serialize: null }
}
}
}
Loading

0 comments on commit 256bb24

Please sign in to comment.