@@ -92,15 +92,27 @@ SlowBuffer.prototype.toString = function(encoding, start, end) {
92
92
} ;
93
93
94
94
95
- SlowBuffer . prototype . hexWrite = function ( string , offset ) {
96
- var len = string . length ;
95
+ SlowBuffer . prototype . hexWrite = function ( string , offset , length ) {
97
96
offset = + offset || 0 ;
97
+ var remaining = this . length - offset ;
98
+ if ( ! length ) {
99
+ length = remaining ;
100
+ } else {
101
+ length = + length ;
102
+ if ( length > remaining ) {
103
+ length = remaining ;
104
+ }
105
+ }
98
106
99
107
// must be an even number of digits
100
- if ( len % 2 ) {
108
+ var strLen = string . length ;
109
+ if ( strLen % 2 ) {
101
110
throw new Error ( 'Invalid hex string' ) ;
102
111
}
103
- for ( var i = 0 ; i < len / 2 ; i ++ ) {
112
+ if ( length > strLen / 2 ) {
113
+ length = strLen / 2 ;
114
+ }
115
+ for ( var i = 0 ; i < length ; i ++ ) {
104
116
var byte = parseInt ( string . substr ( i * 2 , 2 ) , 16 ) ;
105
117
if ( isNaN ( byte ) ) throw new Error ( 'Invalid hex string' ) ;
106
118
this [ offset + i ] = byte ;
@@ -109,38 +121,53 @@ SlowBuffer.prototype.hexWrite = function(string, offset) {
109
121
} ;
110
122
111
123
112
- SlowBuffer . prototype . write = function ( string , offset , encoding ) {
113
- // Support both (string, offset, encoding)
114
- // and the legacy (string, encoding, offset)
115
- if ( ! isFinite ( offset ) ) {
124
+ SlowBuffer . prototype . write = function ( string , offset , length , encoding ) {
125
+ // Support both (string, offset, length, encoding)
126
+ // and the legacy (string, encoding, offset, length)
127
+ if ( isFinite ( offset ) ) {
128
+ if ( ! isFinite ( length ) ) {
129
+ encoding = length ;
130
+ length = undefined ;
131
+ }
132
+ } else { // legacy
116
133
var swap = encoding ;
117
134
encoding = offset ;
118
- offset = swap ;
135
+ offset = length ;
136
+ length = swap ;
119
137
}
120
138
121
139
offset = + offset || 0 ;
140
+ var remaining = this . length - offset ;
141
+ if ( ! length ) {
142
+ length = remaining ;
143
+ } else {
144
+ length = + length ;
145
+ if ( length > remaining ) {
146
+ length = remaining ;
147
+ }
148
+ }
122
149
encoding = String ( encoding || 'utf8' ) . toLowerCase ( ) ;
123
150
124
151
switch ( encoding ) {
125
152
case 'hex' :
126
- return this . hexWrite ( string , offset ) ;
153
+ return this . hexWrite ( string , offset , length ) ;
127
154
128
155
case 'utf8' :
129
156
case 'utf-8' :
130
- return this . utf8Write ( string , offset ) ;
157
+ return this . utf8Write ( string , offset , length ) ;
131
158
132
159
case 'ascii' :
133
- return this . asciiWrite ( string , offset ) ;
160
+ return this . asciiWrite ( string , offset , length ) ;
134
161
135
162
case 'binary' :
136
- return this . binaryWrite ( string , offset ) ;
163
+ return this . binaryWrite ( string , offset , length ) ;
137
164
138
165
case 'base64' :
139
- return this . base64Write ( string , offset ) ;
166
+ return this . base64Write ( string , offset , length ) ;
140
167
141
168
case 'ucs2' :
142
169
case 'ucs-2' :
143
- return this . ucs2Write ( string , offset ) ;
170
+ return this . ucs2Write ( string , offset , length ) ;
144
171
145
172
default :
146
173
throw new Error ( 'Unknown encoding' ) ;
@@ -271,47 +298,61 @@ Buffer.prototype.set = function set(i, v) {
271
298
} ;
272
299
273
300
274
- // write(string, offset = 0, encoding = 'utf8')
275
- Buffer . prototype . write = function ( string , offset , encoding ) {
276
- if ( ! isFinite ( offset ) ) {
301
+ // write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8')
302
+ Buffer . prototype . write = function ( string , offset , length , encoding ) {
303
+ // Support both (string, offset, length, encoding)
304
+ // and the legacy (string, encoding, offset, length)
305
+ if ( isFinite ( offset ) ) {
306
+ if ( ! isFinite ( length ) ) {
307
+ encoding = length ;
308
+ length = undefined ;
309
+ }
310
+ } else { // legacy
277
311
var swap = encoding ;
278
312
encoding = offset ;
279
- offset = swap ;
313
+ offset = length ;
314
+ length = swap ;
280
315
}
281
316
282
317
offset = + offset || 0 ;
318
+ var remaining = this . length - offset ;
319
+ if ( ! length ) {
320
+ length = remaining ;
321
+ } else {
322
+ length = + length ;
323
+ if ( length > remaining ) {
324
+ length = remaining ;
325
+ }
326
+ }
283
327
encoding = String ( encoding || 'utf8' ) . toLowerCase ( ) ;
284
328
285
- // Make sure we are not going to overflow
286
- var maxLength = this . length - offset ;
287
-
288
329
var ret ;
289
330
switch ( encoding ) {
290
331
case 'hex' :
291
- ret = this . parent . hexWrite ( string , this . offset + offset , maxLength ) ;
332
+ ret = this . parent . hexWrite ( string , this . offset + offset , length ) ;
292
333
break ;
293
334
294
335
case 'utf8' :
295
336
case 'utf-8' :
296
- ret = this . parent . utf8Write ( string , this . offset + offset , maxLength ) ;
337
+ ret = this . parent . utf8Write ( string , this . offset + offset , length ) ;
297
338
break ;
298
339
299
340
case 'ascii' :
300
- ret = this . parent . asciiWrite ( string , this . offset + offset , maxLength ) ;
341
+ ret = this . parent . asciiWrite ( string , this . offset + offset , length ) ;
301
342
break ;
302
343
303
344
case 'binary' :
304
- ret = this . parent . binaryWrite ( string , this . offset + offset , maxLength ) ;
345
+ ret = this . parent . binaryWrite ( string , this . offset + offset , length ) ;
305
346
break ;
306
347
307
348
case 'base64' :
308
349
// Warning: maxLength not taken into account in base64Write
309
- ret = this . parent . base64Write ( string , this . offset + offset , maxLength ) ;
350
+ ret = this . parent . base64Write ( string , this . offset + offset , length ) ;
310
351
break ;
311
352
312
353
case 'ucs2' :
313
354
case 'ucs-2' :
314
- ret = this . parent . ucs2Write ( string , this . offset + offset , maxLength ) ;
355
+ ret = this . parent . ucs2Write ( string , this . offset + offset , length ) ;
315
356
break ;
316
357
317
358
default :
@@ -1019,3 +1060,4 @@ Buffer.prototype.writeDouble = function(value, offset, endian) {
1019
1060
verifIEEE754 ( value , 1.7976931348623157E+308 , - 1.7976931348623157E+308 ) ;
1020
1061
IEEE754 . writeIEEE754 ( buffer , value , offset , endian , 52 , 8 ) ;
1021
1062
} ;
1063
+
0 commit comments