Comparing version 0.2.1 to 0.3.0
70
ctype.js
@@ -220,5 +220,4 @@ /* | ||
/* | ||
* Construct a per parser specific set of the basic types. That way if this is | ||
* modified, it's okay. The values themselves should never be modified; however, | ||
* the set of keys may change over time. | ||
* Each parser has their own set of types. We want to make sure that they each | ||
* get their own copy as they may need to modify it. | ||
*/ | ||
@@ -351,3 +350,4 @@ function ctGetBasicTypes() | ||
* Create a new instance of the parser. Each parser has its own store of | ||
* typedefs and endianness. Conf is an object with the following values: | ||
* typedefs and endianness. Conf is an object with the following required | ||
* values: | ||
* | ||
@@ -357,7 +357,12 @@ * endian Either 'big' or 'little' do determine the endianness we | ||
* | ||
* And the following optional values: | ||
* | ||
* char-type Valid options here are uint8 and int8. If uint8 is | ||
* specified this changes the default behavior of a single | ||
* char from being a buffer of a single character to being | ||
* a uint8_t. If int8, it becomes an int8_t instead. | ||
*/ | ||
function CTypeParser(conf) | ||
{ | ||
if (!conf) | ||
throw (new Error('missing required argument')); | ||
if (!conf) throw (new Error('missing required argument')); | ||
@@ -370,4 +375,19 @@ if (!('endian' in conf)) | ||
if ('char-type' in conf && (conf['char-type'] != 'uint8' && | ||
conf['char-type'] != 'int8')) | ||
throw (new Error('invalid option for char-type: ' + | ||
conf['char-type'])); | ||
this.endian = conf['endian']; | ||
this.types = ctGetBasicTypes(); | ||
/* | ||
* This may be a more graceful way to do this, but this will have to | ||
* serve. | ||
*/ | ||
if ('char-type' in conf && conf['char-type'] == 'uint8') | ||
this.types['char'] = this.types['uint8_t']; | ||
if ('char-type' in conf && conf['char-type'] == 'int8') | ||
this.types['char'] = this.types['int8_t']; | ||
} | ||
@@ -525,3 +545,3 @@ | ||
else if (dispatch == 'write') | ||
return (this.writeStruct(value, this.types[type], | ||
return (this.readStruct(value, this.types[type], | ||
buffer, offset)); | ||
@@ -562,3 +582,3 @@ else | ||
if (type['type'] == 'char') | ||
parse = deftypes['char[]']['read'](len, | ||
parse = this.types['char[]']['read'](len, | ||
this.endian, buffer, offset); | ||
@@ -570,3 +590,3 @@ else | ||
if (type['type'] in deftypes) | ||
parse = deftypes[type['type']]['read'](this.endian, | ||
parse = this.types[type['type']]['read'](this.endian, | ||
buffer, offset); | ||
@@ -650,7 +670,7 @@ else | ||
if (buffer === undefined) | ||
throw (new Error('missing buffer for what we should be' + | ||
throw (new Error('missing buffer for what we should be ' + | ||
'parsing')); | ||
if (offset === undefined) | ||
throw (new Error('missing offset for what we should be' + | ||
throw (new Error('missing offset for what we should be ' + | ||
'parsing')); | ||
@@ -700,10 +720,10 @@ | ||
if (type['type'] == 'char') | ||
ret = deftypes['char[]']['write'](value, len, | ||
ret = this.types['char[]']['write'](value, len, | ||
this.endian, buffer, offset); | ||
else | ||
ret = this.writeArray(value, type['type'], | ||
buffer, offset); | ||
len, buffer, offset); | ||
} else { | ||
if (type['type'] in deftypes) | ||
ret = deftypes[type['type']]['write'](value, | ||
ret = this.types[type['type']]['write'](value, | ||
this.endian, buffer, offset); | ||
@@ -721,3 +741,3 @@ else | ||
*/ | ||
CTypeParser.prototype.writeStruct = function (value, def, buffer, offset) | ||
CTypeParser.prototype.writeStruct = function (def, buffer, offset) | ||
{ | ||
@@ -737,5 +757,5 @@ var ii, entry, type, key; | ||
offset += this.writeEntry(value[ii], type, buffer, offset); | ||
offset += this.writeEntry(entry['value'], type, buffer, offset); | ||
/* Keep track of types for array length resolution */ | ||
/* Now that we've written it out, we can use it for arrays */ | ||
vals[key] = entry['value']; | ||
@@ -755,9 +775,5 @@ } | ||
* offset The offset in the buffer to write to | ||
* | ||
* TODO This endpoint really is just awful. See ticket CTYPE-6 | ||
*/ | ||
CTypeParser.prototype.writeData = function (def, buffer, offset) | ||
{ | ||
var ii, vals, key; | ||
if (def === undefined) | ||
@@ -768,16 +784,12 @@ throw (new Error('missing definition for what we should be' + | ||
if (buffer === undefined) | ||
throw (new Error('missing buffer for what we should be' + | ||
throw (new Error('missing buffer for what we should be ' + | ||
'parsing')); | ||
if (offset === undefined) | ||
throw (new Error('missing offset for what we should be' + | ||
throw (new Error('missing offset for what we should be ' + | ||
'parsing')); | ||
ctCheckReq(def, this.types, [ 'value' ]); | ||
vals = []; | ||
for (ii = 0; ii < def.length; ii++) { | ||
key = Object.keys(def[ii])[0]; | ||
vals.push(def[ii][key]['value']); | ||
} | ||
this.writeStruct(vals, def, buffer, offset); | ||
this.writeStruct(def, buffer, offset); | ||
}; | ||
@@ -784,0 +796,0 @@ |
{ | ||
"name": "ctype", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "read and write binary structures and data types", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rmustacc/node-ctype", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
177577
30
4352
0
1