-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants-io.js
167 lines (152 loc) · 4.74 KB
/
constants-io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* jshint strict: true */
var constants = require('./constants');
/**
* This private method creates a write method bound to the constants
*/
var createWriteMethod = function(constants, size, constantName) {
if (size < 0 || size > 2) throw new Error('invalid size argument');
var map = {};
Object.keys(constants).forEach(function(value) {
var name = constants[value];
map[name] = value;
});
return function(buf, name, strict, offset) {
if (strict === undefined) strict = true;
var value;
if (map[name] === undefined) {
if (!strict) {
value = name;
} else {
throw new Error('constant '+name+' of '+constantName+' not found');
}
} else {
value = map[name];
}
if (offset === undefined) {
if (buf.cursor !== undefined) {
offset = buf.cursor;
buf.cursor += size;
} else {
throw new Error('neither offset nor buf.cursor is defined');
}
}
if (!Buffer.isBuffer(buf)) {
throw new Error('invalid buffer argument');
} else if (offset + size > buf.length) {
throw new Error('buffer is to small');
} else {
if (size == 1) {
buf.writeUInt8(value, offset);
} else if (size == 2) {
buf.writeUInt16BE(value, offset);
} else {
throw new Error('invalid size argument');
}
}
};
};
/**
* This private method creates a read method bound to the constants
*/
var createReadMethod = function(constants, size, constantName) {
if (size < 0 || size > 2) throw new Error('invalid size argument');
return function(buf, strict, offset) {
if (strict === undefined) strict = true;
if (offset === undefined) {
if (buf.cursor !== undefined) {
offset = buf.cursor;
buf.cursor += size;
} else {
throw new Error('neither offset nor buf.cursor is defined');
}
}
if (!Buffer.isBuffer(buf)) {
throw new Error('invalid buffer argument');
} else if (offset + size > buf.length) {
throw new Error('buffer is to small');
} else {
var value = null;
if (size == 1) {
value = buf.readUInt8(offset);
} else if (size == 2) {
value = buf.readUInt16BE(offset);
} else {
throw new Error('invalid size argument');
}
if (constants[value]) {
return constants[value];
} else if (!strict) {
return value;
} else {
throw new Error('constant name for value '+value+' of '+constantName+' not found');
}
}
};
};
/**
* export for every constant a read and a write method
*/
Object.keys(constants).forEach(function(name) {
if (name != 'sizeOf' && constants.sizeOf[name]) {
exports['write'+name] = createWriteMethod(constants[name], constants.sizeOf[name], name);
exports['read'+name] = createReadMethod(constants[name], constants.sizeOf[name], name);
}
});
var offestAndCursor = function(buf, offset, size) {
if (offset === undefined) {
if (buf.cursor !== undefined) {
offset = buf.cursor;
buf.cursor += size;
} else {
throw new Error('neither offset nor buf.cursor is defined');
}
}
return offset;
};
exports.readUInt8 = function(buf, offset) {
offset = offestAndCursor(buf, offset, 1);
return buf.readUInt8(offset);
};
exports.writeUInt8 = function(buf, value, offset) {
offset = offestAndCursor(buf, offset, 1);
buf.writeUInt8(value, offset);
};
exports.readUInt16 = function(buf, offset) {
offset = offestAndCursor(buf, offset, 2);
return buf.readUInt16BE(offset);
};
exports.writeUInt16 = function(buf, value, offset) {
offset = offestAndCursor(buf, offset, 2);
buf.writeUInt16BE(value, offset);
};
exports.readUInt24 = function(buf, offset) {
offset = offestAndCursor(buf, offset, 3);
return (buf.readUInt8(offset) << 16) +
(buf.readUInt8(offset+1) << 8) +
buf.readUInt8(offset+2);
};
exports.writeUInt24 = function(buf, value, offset) {
offset = offestAndCursor(buf, offset, 3);
buf.writeUInt8((value >> 16) & 0xFF, offset);
buf.writeUInt8((value >> 8) & 0xFF, offset+1);
buf.writeUInt8(value & 0xFF, offset+2);
};
exports.readUInt32 = function(buf, offset) {
offset = offestAndCursor(buf, offset, 4);
return buf.readUInt32BE(offset);
};
exports.writeUInt32 = function(buf, value, offset) {
offset = offestAndCursor(buf, offset, 4);
buf.writeUInt32BE(value, offset);
};
exports.readBytes = function(buf, len, offset) {
if (len < 1) throw new Error('invalid length');
offset = offestAndCursor(buf, offset, len);
var targetBuffer = new Buffer(len);
buf.copy(targetBuffer, 0, offset, offset+len);
return targetBuffer;
};
exports.writeBytes = function(buf, bufValue, offset) {
offset = offestAndCursor(buf, offset, bufValue.length);
bufValue.copy(buf, offset, 0, bufValue.length);
};