|
| 1 | +/* |
| 2 | +Copyright (c) 2010 Tim Caswell <tim@creationix.com> |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +var Buffer = module.exports = require('buffer').Buffer; |
| 24 | +var proto = Buffer.prototype; |
| 25 | + |
| 26 | +// Cleans the buffer to be all zeroes |
| 27 | +proto.zeroOut = function zeroOut() { |
| 28 | + for (var i = 0, l = this.length; i < l; i++) { |
| 29 | + this[i] = 0; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +// Writes a 32 bit integer at offset |
| 34 | +proto.int32Write = function int32Write(number, offset) { |
| 35 | + offset = offset || 0; |
| 36 | + var unsigned = (number < 0) ? (number + 0x100000000) : number; |
| 37 | + this[offset] = Math.floor(unsigned / 0xffffff); |
| 38 | + unsigned &= 0xffffff; |
| 39 | + this[offset + 1] = Math.floor(unsigned / 0xffff); |
| 40 | + unsigned &= 0xffff; |
| 41 | + this[offset + 2] = Math.floor(unsigned / 0xff); |
| 42 | + unsigned &= 0xff; |
| 43 | + this[offset + 3] = Math.floor(unsigned); |
| 44 | +}; |
| 45 | + |
| 46 | +// Writes a 16 bit integer at offset |
| 47 | +proto.int16Write = function int16Write(number, offset) { |
| 48 | + offset = offset || 0; |
| 49 | + var unsigned = (number < 0) ? (number + 0x10000) : number; |
| 50 | + this[offset] = Math.floor(unsigned / 0xff); |
| 51 | + unsigned &= 0xff; |
| 52 | + this[offset + 1] = Math.floor(unsigned); |
| 53 | +} |
| 54 | + |
| 55 | +// Reads a 32 bit integer from offset |
| 56 | +proto.int32Read = function int32Read(offset) { |
| 57 | + offset = offset || 0; |
| 58 | + var unsigned = this[offset] * 0x1000000 + |
| 59 | + this[offset + 1] * 0x10000 + |
| 60 | + this[offset + 2] * 0x100 + |
| 61 | + this[offset + 3]; |
| 62 | + return (unsigned & 0x80000000) ? (unsigned - 0x100000000) : unsigned |
| 63 | +}; |
| 64 | + |
| 65 | +// Reads a 32 bit integer from offset |
| 66 | +proto.int16Read = function int16Read(offset) { |
| 67 | + offset = offset || 0; |
| 68 | + var unsigned = this[offset] * 0x100 + |
| 69 | + this[offset + 1]; |
| 70 | + return (unsigned & 0x8000) ? (unsigned - 0x10000) : unsigned |
| 71 | +}; |
| 72 | + |
| 73 | +Buffer.fromString = function fromString(string) { |
| 74 | + var b = new Buffer(Buffer.byteLength(string)); |
| 75 | + b.write(string, 'utf8'); |
| 76 | + return b; |
| 77 | +} |
| 78 | + |
| 79 | +Buffer.makeWriter = function makeWriter() { |
| 80 | + var data = []; |
| 81 | + var writer; |
| 82 | + var push = { |
| 83 | + int32: function pushInt32(number) { |
| 84 | + var b = new Buffer(4); |
| 85 | + b.int32Write(number); |
| 86 | + data.push(b); |
| 87 | + return writer; |
| 88 | + }, |
| 89 | + int16: function pushInt16(number) { |
| 90 | + var b = new Buffer(2); |
| 91 | + b.int16Write(number); |
| 92 | + data.push(b); |
| 93 | + return writer; |
| 94 | + }, |
| 95 | + string: function pushString(string) { |
| 96 | + data.push(Buffer.fromString(string)); |
| 97 | + return writer; |
| 98 | + }, |
| 99 | + cstring: function pushCstring(string) { |
| 100 | + data.push(Buffer.fromString(string + "\0")); |
| 101 | + return writer; |
| 102 | + }, |
| 103 | + multicstring: function pushMulticstring(fields) { |
| 104 | + data.push(Buffer.fromString(fields.join("\0") + "\0\0")); |
| 105 | + return writer; |
| 106 | + }, |
| 107 | + hash: function pushHash(hash) { |
| 108 | + var keys = Object.keys(hash); |
| 109 | + var pairs = []; |
| 110 | + for (var i = 0, l = keys.length; i < l; i++) { |
| 111 | + var key = keys[i]; |
| 112 | + pairs.push(key + "\0" + hash[key] + "\0"); |
| 113 | + } |
| 114 | + data.push(Buffer.fromString(pairs.join("") + "\0")); |
| 115 | + return writer; |
| 116 | + } |
| 117 | + }; |
| 118 | + writer = { |
| 119 | + data: data, |
| 120 | + push: push, |
| 121 | + |
| 122 | + // Convert an array of buffers into a single buffer using memcopy |
| 123 | + toBuffer: function toBuffer() { |
| 124 | + var total = 0; |
| 125 | + var i, l = data.length; |
| 126 | + for (i = 0; i < l; i++) { |
| 127 | + total += data[i].length; |
| 128 | + } |
| 129 | + var b = new Buffer(total); |
| 130 | + var offset = 0; |
| 131 | + for (i = 0; i < l; i++) { |
| 132 | + data[i].copy(b, offset); |
| 133 | + offset += data[i].length; |
| 134 | + } |
| 135 | + return b; |
| 136 | + } |
| 137 | + }; |
| 138 | + return writer; |
| 139 | +} |
| 140 | + |
| 141 | +proto.toReader = function toReader() { |
| 142 | + var offset = 0, length = this.length, buffer = this; |
| 143 | + return { |
| 144 | + empty: function empty() { |
| 145 | + return offset >= length; |
| 146 | + }, |
| 147 | + int32: function shiftInt32() { |
| 148 | + var number = buffer.int32Read(offset); |
| 149 | + offset += 4; |
| 150 | + return number; |
| 151 | + }, |
| 152 | + int16: function shiftInt16() { |
| 153 | + var number = buffer.int16Read(offset); |
| 154 | + offset += 2; |
| 155 | + return number; |
| 156 | + }, |
| 157 | + buffer: function shiftBuffer(length) { |
| 158 | + var b = buffer.slice(offset, offset + length); |
| 159 | + offset += length; |
| 160 | + return b; |
| 161 | + }, |
| 162 | + string: function shiftString(length) { |
| 163 | + var string = buffer.toString('utf8', offset, offset + length); |
| 164 | + offset += length; |
| 165 | + return string; |
| 166 | + }, |
| 167 | + cstring: function shiftCstring() { |
| 168 | + var end = offset; |
| 169 | + while (buffer[end] > 0 && end < length) { end++; } |
| 170 | + var string = buffer.toString('utf8', offset, end); |
| 171 | + offset = end + 1; |
| 172 | + return string; |
| 173 | + }, |
| 174 | + multicstring: function shiftMulticstring() { |
| 175 | + var fields = []; |
| 176 | + while (buffer[offset] > 0) { |
| 177 | + fields.push(this.cstring()); |
| 178 | + } |
| 179 | + offset++; |
| 180 | + return fields; |
| 181 | + }, |
| 182 | + hash: function shiftHash() { |
| 183 | + var hash = {}; |
| 184 | + while (buffer[offset] > 0) { |
| 185 | + hash[this.cstring()] = this.cstring(); |
| 186 | + } |
| 187 | + offset++; |
| 188 | + return hash; |
| 189 | + } |
| 190 | + } |
| 191 | +}; |
| 192 | + |
| 193 | +// Test It |
| 194 | +// var sys = require('sys'); |
| 195 | +// |
| 196 | +// var w = Buffer.makeWriter(); |
| 197 | +// w.push.int32(305419896); |
| 198 | +// w.push.int16(4660); |
| 199 | +// w.push.string("12345"); |
| 200 | +// w.push.cstring("Héllø Wø®l∂"); |
| 201 | +// w.push.multicstring(["Hello", "World"]); |
| 202 | +// w.push.hash({name: "Tim", age: "28"}); |
| 203 | +// w.push.int32(0x12345678); |
| 204 | +// w.push.int16(0x1234); |
| 205 | +// var b = w.toBuffer(); |
| 206 | +// // sys.p(b); |
| 207 | +// var r = b.toReader(); |
| 208 | +// sys.p([ |
| 209 | +// r.int32(), |
| 210 | +// r.int16(), |
| 211 | +// r.string(5), |
| 212 | +// r.cstring(), |
| 213 | +// r.multicstring(), |
| 214 | +// r.hash(), |
| 215 | +// r.int32(), |
| 216 | +// r.int16() |
| 217 | +// ]); |
| 218 | +// |
0 commit comments