Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmustacc/node-ctype#64 Handle deferred CTF type definitions #65

Merged
merged 1 commit into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ in it. The commit message also has the corresponding github issue. i.e. CTYPE-42
would be issue 42. Each issue can be found at:
https://github.com/rmustacc/node-ctype/issues/%d.

v0.5.6
node-ctype#64 Handle deferred CTF type definitions

v0.5.5
node-ctype#56 Correct SPDX identifier
ctf2json#55 remove sys
Expand Down
2 changes: 2 additions & 0 deletions ctf.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ function ctfParseJson(json, ctype)

for (ii = 0; ii < json['data'].length; ii++)
ctfParseEntry(json['data'][ii], ctype);

ctype.checkDeferred();
}

exports.ctfParseJson = ctfParseJson;
19 changes: 18 additions & 1 deletion ctype.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ function CTypeParser(conf)

this.endian = conf['endian'];
this.types = ctGetBasicTypes();
this.deferredCheckReqTypes = [];

/*
* There may be a more graceful way to do this, but this will have to
Expand Down Expand Up @@ -486,7 +487,14 @@ CTypeParser.prototype.typedef = function (name, value)
this.types[name] = value;
} else {
/* We have a struct, validate it */
ctCheckReq(value, this.types);
try {
ctCheckReq(value, this.types);
} catch (ex) {
var notFoundMsg = 'type not found or typdefed: ';
if ((ex.message || '').substr(notFoundMsg.length) === notFoundMsg) {
this.deferredCheckReqTypes.push(value);
}
}
this.types[name] = value;
}
};
Expand Down Expand Up @@ -846,6 +854,15 @@ CTypeParser.prototype.writeData = function (def, buffer, offset, values)
this.writeStruct(hv ? values : getValues(def), def, buffer, offset);
};

CTypeParser.prototype.checkDeferred = function ()
{
var ii;

for (ii = 0; ii < this.deferredCheckReqTypes.length; ii++) {
ctCheckReq(this.deferredCheckReqTypes[ii], this.types);
}
};

/*
* Functions to go to and from 64 bit numbers in a way that is compatible with
* Javascript limitations. There are two sets. One where the user is okay with
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ctype",
"version": "0.5.5",
"version": "0.5.6",
"description": "read and write binary structures and data types",
"homepage": "https://github.com/rmustacc/node-ctype",
"author": "Robert Mustacchi <rm@fingolfin.org>",
Expand Down
9 changes: 7 additions & 2 deletions tst/ctf/struct.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
{ "name": "time_t", "typedef": "long" },
{ "name": "struct timespec", "struct": [
{ "name": "tv_sec", "type": "time_t" },
{ "name": "tv_nsec", "type": "long" }
{ "name": "tv_nsec", "type": "long" },
{ "name": "ctxoption", "type": "contextoption_t" }
] },
{ "name": "timestruc_t", "typedef": "struct timespec" }
{ "name": "timestruc_t", "typedef": "struct timespec" },
{ "name": "struct contextoption", "struct": [
{ "name": "options", "type": "long" }
] },
{ "name": "contextoption_t", "typedef": "struct contextoption" }
]
}
53 changes: 32 additions & 21 deletions tst/ctf/tst.fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,45 @@ var mod_assert = require('assert');
var mod_ctype = require('../../ctype.js');

var cases = [
{ json: { }, msg: 'bad JSON - no metadata or data' },
{ json: { metadata: {} }, msg: 'bad JSON - bad metadata section' },
{ json: { metadata: { 'JSON version': [] } },
msg: 'bad JSON - bad JSON version' },
{ json: { metadata: { 'JSON version': 2 } },
msg: 'bad JSON - bad JSON version' },
{ json: { metadata: { 'JSON version': '100.20' } },
msg: 'bad JSON - bad JSON version' },
{ json: { metadata: { 'JSON version': '1.0' } },
msg: 'missing data section' },
{ json: { metadata: { 'JSON version': '1.0' }, data: 1 },
msg: 'invalid data section' },
{ json: { metadata: { 'JSON version': '1.0' }, data: 1.1 },
msg: 'invalid data section' },
{ json: { metadata: { 'JSON version': '1.0' }, data: '1.1' },
msg: 'invalid data section' },
{ json: { metadata: { 'JSON version': '1.0' }, data: {} },
msg: 'invalid data section' }
{ json: { }, msg: 'Invalid CTF JSON: missing metadata section' },
{ json: { metadata: {} }, msg: 'Invalid CTF JSON: missing ctf2json_version' },
{ json: { metadata: { 'ctf2json_version': [] } },
msg: 'Unsuported ctf2json_version: ' },
{ json: { metadata: { 'ctf2json_version': 2 } },
msg: 'Unsuported ctf2json_version: 2' },
{ json: { metadata: { 'ctf2json_version': '100.20' } },
msg: 'Unsuported ctf2json_version: 100.20' },
{ json: { metadata: { 'ctf2json_version': '1.0' } },
msg: 'Invalid CTF JSON: missing data section' },
{ json: { metadata: { 'ctf2json_version': '1.0' }, data: 1 },
msg: 'Malformed CTF JSON: data section is not an array' },
{ json: { metadata: { 'ctf2json_version': '1.0' }, data: 1.1 },
msg: 'Malformed CTF JSON: data section is not an array' },
{ json: { metadata: { 'ctf2json_version': '1.0' }, data: '1.1' },
msg: 'Malformed CTF JSON: data section is not an array' },
{ json: { metadata: { 'ctf2json_version': '1.0' }, data: {} },
msg: 'Malformed CTF JSON: data section is not an array' },
{ json: { metadata: { 'ctf2json_version': '1.0' },
data: [ {
'name': 'struct DataHolder', 'struct': [
{ 'name': 'unknown', 'type': 'missing_t' }
] } ]
},
msg: 'type not found or typdefed: missing_t' }
];

function test()
{
var conf = { endian: 'little' };
var ii;

for (ii = 0; ii < cases.length; ii++) {
mod_assert.throws(function () {
mod_ctype.parseCTF(cases[ii].json);
}, Error, cases[ii].msg);
try {
mod_ctype.parseCTF(cases[ii].json, conf);
mod_assert.fail('expected test to fail: ' + cases[ii].json);
} catch (ex) {
mod_assert.equal(ex.message, cases[ii].msg);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions tst/ctf/tst.struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ function test()
parser = mod_ctype.parseCTF(data, { endian: 'big' });
mod_assert.deepEqual(parser.lstypes(), { 'long': 'int32_t',
'time_t': 'long',
'struct timespec': [ { tv_sec: { type: 'time_t' } },
{ tv_nsec: { type: 'long' } },
{ ctxoption: { type: 'contextoption_t' } } ],
'timestruc_t': 'struct timespec',
'struct timespec': [ { 'tv_sec': { 'type': 'time_t' } },
{ 'tv_nsec': { 'type': 'long' } } ] });
'struct contextoption': [ { options: { type: 'long' } } ],
'contextoption_t': 'struct contextoption'
});
}

test();