Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions ctype.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -494,9 +497,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)
Expand All @@ -514,18 +518,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'));
}
};

/*
Expand Down Expand Up @@ -730,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;
};

/*
Expand Down Expand Up @@ -759,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);
};

/*
Expand Down