From 1255b2e83bab85af7b6b56a654e66b37f8989f17 Mon Sep 17 00:00:00 2001 From: Sidorenko Vladimir Date: Tue, 26 Jul 2011 11:04:26 +0300 Subject: [PATCH 1/3] custom base types definitions allowed --- ctype.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/ctype.js b/ctype.js index 5128737..31ca3bd 100644 --- a/ctype.js +++ b/ctype.js @@ -494,9 +494,10 @@ function ctResolveArray(str, values) } /* - * [private] Either the typedef resolves to another type string or to a struct. - * If it resolves to a struct, we just pass it off to read struct. If not, we - * can just pass it off to read entry. + * [private] Typedef resolves to another type string, base type definition or struct. + * If it resolves to a struct, we just pass it off to read struct. + * If it resolves to type definition - we use it's read/write functions. + * If it resolves to another type string we can just pass it off to read entry. */ CTypeParser.prototype.resolveTypedef = function (type, dispatch, buffer, offset, value) @@ -514,18 +515,23 @@ CTypeParser.prototype.resolveTypedef = function (type, dispatch, buffer, else throw (new Error('invalid dispatch type to ' + 'resolveTypedef')); - } else { - if (dispatch == 'read') - return (this.readStruct(this.types[type], buffer, - offset)); - else if (dispatch == 'write') - return (this.readStruct(value, this.types[type], - buffer, offset)); - else - throw (new Error('invalid dispatch type to ' + - 'resolveTypedef')); - } - + } else if ('read' in this.types[type] && 'write' in this.types[type]) { + if (dispatch == 'read') { + return this.types[type]['read'](this.endian, buffer, offset); + } else if (dispatch == 'write'){ + return this.types[type]['write'](value, this.endian, buffer, offset); + } + } else { + if (dispatch == 'read') + return (this.readStruct(this.types[type], buffer, + offset)); + else if (dispatch == 'write') + return (this.readStruct(value, this.types[type], + buffer, offset)); + else + throw (new Error('invalid dispatch type to ' + + 'resolveTypedef')); + } }; /* From 456729e55d1595a92db48b67f1260eaedfe126db Mon Sep 17 00:00:00 2001 From: Sidorenko Vladimir Date: Tue, 26 Jul 2011 16:12:31 +0300 Subject: [PATCH 2/3] allow adding custom types with parser.typedef --- ctype.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ctype.js b/ctype.js index 31ca3bd..4270acc 100644 --- a/ctype.js +++ b/ctype.js @@ -423,10 +423,13 @@ CTypeParser.prototype.typedef = function (name, value) if (name in this.types) throw (new Error('typedef name already present: ' + name)); + if ('read' in value && 'write' in value) { + this.types[name] = value; + return; + } if (typeof (value) != 'string' && !(value instanceof Array)) throw (new Error('typedef value must either be a string or ' + 'struct')); - if (typeof (value) == 'string') { type = ctParseType(value); if (type['len'] !== undefined) { From 0c66a0bb57801c377d7db524f3a8379c810332b5 Mon Sep 17 00:00:00 2001 From: Sidorenko Vladimir Date: Tue, 26 Jul 2011 16:12:49 +0300 Subject: [PATCH 3/3] make writeData return data size --- ctype.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ctype.js b/ctype.js index 4270acc..b1a0358 100644 --- a/ctype.js +++ b/ctype.js @@ -739,6 +739,7 @@ CTypeParser.prototype.writeStruct = function (def, buffer, offset) /* Now that we've written it out, we can use it for arrays */ vals[key] = entry['value']; } + return offset - baseOffset; }; /* @@ -768,7 +769,7 @@ CTypeParser.prototype.writeData = function (def, buffer, offset) ctCheckReq(def, this.types, [ 'value' ]); - this.writeStruct(def, buffer, offset); + return this.writeStruct(def, buffer, offset); }; /*