FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node_buffer.cc at v1.8.0-commit · afshing/node · GitHub
afshing
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
node_buffer.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
770 lines (563 loc) · 21.8 KB
Breadcrumbs
node
/
src
/
node_buffer.cc
Copy path
File metadata and controls
770 lines (563 loc) · 21.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#
include
"
node.h
"
#
include
"
node_buffer.h
"
#
include
"
env.h
"
#
include
"
env-inl.h
"
#
include
"
smalloc.h
"
#
include
"
string_bytes.h
"
#
include
"
v8-profiler.h
"
#
include
"
v8.h
"
#
include
<
string.h
>
#
include
<
limits.h
>
#
define
MIN
(
a, b
) ((a) < (b) ? (a) : (b))
#
define
CHECK_NOT_OOB
(
r
) \
do
{ \
if
(!(r))
return
env->
ThrowRangeError
(
"
out of range index
"
); \
}
while
(
0
)
#
define
ARGS_THIS
(
argT
) \
Local<Object> obj = argT; \
size_t
obj_length = obj->
GetIndexedPropertiesExternalArrayDataLength
(); \
char
* obj_data =
static_cast
<
char
*>( \
obj->
GetIndexedPropertiesExternalArrayData
()); \
if
(obj_length >
0
) \
CHECK_NE
(obj_data,
nullptr
);
#
define
SLICE_START_END
(
start_arg, end_arg, end_max
) \
size_t
start; \
size_t
end; \
CHECK_NOT_OOB
(ParseArrayIndex(start_arg,
0
, &start)); \
CHECK_NOT_OOB
(ParseArrayIndex(end_arg, end_max, &end)); \
if
(end < start) end = start; \
CHECK_NOT_OOB
(end <= end_max); \
size_t
length = end - start;
namespace
node
{
namespace
Buffer
{
using
v8::Context;
using
v8::EscapableHandleScope;
using
v8::Function;
using
v8::FunctionCallbackInfo;
using
v8::FunctionTemplate;
using
v8::Handle;
using
v8::HandleScope;
using
v8::Isolate;
using
v8::Local;
using
v8::Number;
using
v8::Object;
using
v8::String;
using
v8::Uint32;
using
v8::Value;
bool
HasInstance
(Handle<Value> val) {
return
val->
IsObject
() &&
HasInstance
(val.
As
<Object>());
}
bool
HasInstance
(Handle<Object> obj) {
if
(!obj->
HasIndexedPropertiesInExternalArrayData
())
return
false
;
v8::ExternalArrayType type = obj->
GetIndexedPropertiesExternalArrayDataType
();
return
type == v8::
kExternalUint8Array
;
}
char
*
Data
(Handle<Value> val) {
CHECK
(val->
IsObject
());
//
Use a fully qualified name here to work around a bug in gcc 4.2.
//
It mistakes an unadorned call to Data() for the v8::String::Data type.
return
node::Buffer::Data
(val.
As
<Object>());
}
char
*
Data
(Handle<Object> obj) {
CHECK
(obj->
HasIndexedPropertiesInExternalArrayData
());
return
static_cast
<
char
*>(obj->
GetIndexedPropertiesExternalArrayData
());
}
size_t
Length
(Handle<Value> val) {
CHECK
(val->
IsObject
());
return
Length
(val.
As
<Object>());
}
size_t
Length
(Handle<Object> obj) {
CHECK
(obj->
HasIndexedPropertiesInExternalArrayData
());
return
obj->
GetIndexedPropertiesExternalArrayDataLength
();
}
Local<Object>
New
(Isolate* isolate, Handle<String> string,
enum
encoding enc) {
EscapableHandleScope
scope
(isolate);
size_t
length =
StringBytes::Size
(isolate, string, enc);
Local<Object> buf =
New
(length);
char
* data =
Buffer::Data
(buf);
StringBytes::Write
(isolate, data, length, string, enc);
return
scope.
Escape
(buf);
}
Local<Object>
New
(Isolate* isolate,
size_t
length) {
EscapableHandleScope
handle_scope
(isolate);
Local<Object> obj =
Buffer::New
(
Environment::GetCurrent
(isolate), length);
return
handle_scope.
Escape
(obj);
}
//
TODO(trevnorris): these have a flaw by needing to call the Buffer inst then
//
Alloc. continue to look for a better architecture.
Local<Object>
New
(Environment* env,
size_t
length) {
EscapableHandleScope
scope
(env->
isolate
());
CHECK_LE
(length,
kMaxLength
);
Local<Value> arg =
Uint32::NewFromUnsigned
(env->
isolate
(), length);
Local<Object> obj = env->
buffer_constructor_function
()->
NewInstance
(
1
, &arg);
smalloc::Alloc
(env, obj, length);
return
scope.
Escape
(obj);
}
Local<Object>
New
(Isolate* isolate,
const
char
* data,
size_t
length) {
Environment* env =
Environment::GetCurrent
(isolate);
EscapableHandleScope
handle_scope
(env->
isolate
());
Local<Object> obj =
Buffer::New
(env, data, length);
return
handle_scope.
Escape
(obj);
}
//
TODO(trevnorris): for backwards compatibility this is left to copy the data,
//
but for consistency w/ the other should use data. And a copy version renamed
//
to something else.
Local<Object>
New
(Environment* env,
const
char
* data,
size_t
length) {
EscapableHandleScope
scope
(env->
isolate
());
CHECK_LE
(length,
kMaxLength
);
Local<Value> arg =
Uint32::NewFromUnsigned
(env->
isolate
(), length);
Local<Object> obj = env->
buffer_constructor_function
()->
NewInstance
(
1
, &arg);
//
TODO(trevnorris): done like this to handle HasInstance since only checks
//
if external array data has been set, but would like to use a better
//
approach if v8 provided one.
char
* new_data;
if
(length >
0
) {
new_data =
static_cast
<
char
*>(
malloc
(length));
if
(new_data ==
nullptr
)
FatalError
(
"
node::Buffer::New(const char*, size_t)
"
,
"
Out Of Memory
"
);
memcpy
(new_data, data, length);
}
else
{
new_data =
nullptr
;
}
smalloc::Alloc
(env, obj, new_data, length);
return
scope.
Escape
(obj);
}
Local<Object>
New
(Isolate* isolate,
char
* data,
size_t
length,
smalloc::FreeCallback callback,
void
* hint) {
Environment* env =
Environment::GetCurrent
(isolate);
EscapableHandleScope
handle_scope
(env->
isolate
());
Local<Object> obj =
Buffer::New
(env, data, length, callback, hint);
return
handle_scope.
Escape
(obj);
}
Local<Object>
New
(Environment* env,
char
* data,
size_t
length,
smalloc::FreeCallback callback,
void
* hint) {
EscapableHandleScope
scope
(env->
isolate
());
CHECK_LE
(length,
kMaxLength
);
Local<Value> arg =
Uint32::NewFromUnsigned
(env->
isolate
(), length);
Local<Object> obj = env->
buffer_constructor_function
()->
NewInstance
(
1
, &arg);
smalloc::Alloc
(env, obj, data, length, callback, hint);
return
scope.
Escape
(obj);
}
Local<Object>
Use
(Isolate* isolate,
char
* data,
uint32_t
length) {
Environment* env =
Environment::GetCurrent
(isolate);
EscapableHandleScope
handle_scope
(env->
isolate
());
Local<Object> obj =
Buffer::Use
(env, data, length);
return
handle_scope.
Escape
(obj);
}
Local<Object>
Use
(Environment* env,
char
* data,
uint32_t
length) {
EscapableHandleScope
scope
(env->
isolate
());
CHECK_LE
(length,
kMaxLength
);
Local<Value> arg =
Uint32::NewFromUnsigned
(env->
isolate
(), length);
Local<Object> obj = env->
buffer_constructor_function
()->
NewInstance
(
1
, &arg);
smalloc::Alloc
(env, obj, data, length);
return
scope.
Escape
(obj);
}
template
<encoding encoding>
void
StringSlice
(
const
FunctionCallbackInfo<Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
ARGS_THIS
(args.
This
())
SLICE_START_END
(args[
0
], args[
1
], obj_length)
args.
GetReturnValue
().
Set
(
StringBytes::Encode
(env->
isolate
(), obj_data + start, length, encoding));
}
template
<>
void
StringSlice<
UCS2
>(
const
FunctionCallbackInfo<Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
ARGS_THIS
(args.
This
())
SLICE_START_END
(args[
0
], args[
1
], obj_length)
length /=
2
;
const
char
* data = obj_data + start;
const
uint16_t
* buf;
bool
release =
false
;
//
Node's "ucs2" encoding expects LE character data inside a Buffer, so we
//
need to reorder on BE platforms. See http://nodejs.org/api/buffer.html
//
regarding Node's "ucs2" encoding specification.
const
bool
aligned = (
reinterpret_cast
<
uintptr_t
>(data) %
sizeof
(*buf) ==
0
);
if
(
IsLittleEndian
() && aligned) {
buf =
reinterpret_cast
<
const
uint16_t
*>(data);
}
else
{
//
Make a copy to avoid unaligned accesses in v8::String::NewFromTwoByte().
uint16_t
* copy =
new
uint16_t
[length];
for
(
size_t
i =
0
, k =
0
; i < length; i +=
1
, k +=
2
) {
//
Assumes that the input is little endian.
const
uint8_t
lo =
static_cast
<
uint8_t
>(data[k +
0
]);
const
uint8_t
hi =
static_cast
<
uint8_t
>(data[k +
1
]);
copy[i] = lo | hi <<
8
;
}
buf = copy;
release =
true
;
}
args.
GetReturnValue
().
Set
(
StringBytes::Encode
(env->
isolate
(), buf, length));
if
(release)
delete[]
buf;
}
void
BinarySlice
(
const
FunctionCallbackInfo<Value>& args) {
StringSlice<
BINARY
>(args);
}
void
AsciiSlice
(
const
FunctionCallbackInfo<Value>& args) {
StringSlice<
ASCII
>(args);
}
void
Utf8Slice
(
const
FunctionCallbackInfo<Value>& args) {
StringSlice<
UTF8
>(args);
}
void
Ucs2Slice
(
const
FunctionCallbackInfo<Value>& args) {
StringSlice<
UCS2
>(args);
}
void
HexSlice
(
const
FunctionCallbackInfo<Value>& args) {
StringSlice<
HEX
>(args);
}
void
Base64Slice
(
const
FunctionCallbackInfo<Value>& args) {
StringSlice<
BASE64
>(args);
}
//
bytesCopied = buffer.copy(target[, targetStart][, sourceStart][, sourceEnd]);
void
Copy
(
const
FunctionCallbackInfo<Value> &args) {
Environment* env =
Environment::GetCurrent
(args);
Local<Object> target = args[
0
]->
ToObject
(env->
isolate
());
if
(!
HasInstance
(target))
return
env->
ThrowTypeError
(
"
first arg should be a Buffer
"
);
ARGS_THIS
(args.
This
())
size_t
target_length = target->
GetIndexedPropertiesExternalArrayDataLength
();
char
* target_data =
static_cast
<
char
*>(
target->
GetIndexedPropertiesExternalArrayData
());
size_t
target_start;
size_t
source_start;
size_t
source_end;
CHECK_NOT_OOB
(
ParseArrayIndex
(args[
1
],
0
, &target_start));
CHECK_NOT_OOB
(
ParseArrayIndex
(args[
2
],
0
, &source_start));
CHECK_NOT_OOB
(
ParseArrayIndex
(args[
3
], obj_length, &source_end));
//
Copy 0 bytes; we're done
if
(target_start >= target_length || source_start >= source_end)
return
args.
GetReturnValue
().
Set
(
0
);
if
(source_start > obj_length)
return
env->
ThrowRangeError
(
"
out of range index
"
);
if
(source_end - source_start > target_length - target_start)
source_end = source_start + target_length - target_start;
uint32_t
to_copy =
MIN
(
MIN
(source_end - source_start,
target_length - target_start),
obj_length - source_start);
memmove
(target_data + target_start, obj_data + source_start, to_copy);
args.
GetReturnValue
().
Set
(to_copy);
}
void
Fill
(
const
FunctionCallbackInfo<Value>& args) {
ARGS_THIS
(args[
0
].
As
<Object>())
size_t
start = args[
2
]->
Uint32Value
();
size_t
end = args[
3
]->
Uint32Value
();
size_t
length = end - start;
CHECK
(length + start <= obj_length);
if
(args[
1
]->
IsNumber
()) {
int
value = args[
1
]->
Uint32Value
() &
255
;
memset
(obj_data + start, value, length);
return
;
}
node::Utf8Value
str
(args.
GetIsolate
(), args[
1
]);
size_t
str_length = str.
length
();
size_t
in_there = str_length;
char
* ptr = obj_data + start + str_length;
if
(str_length ==
0
)
return
;
memcpy
(obj_data + start, *str,
MIN
(str_length, length));
if
(str_length >= length)
return
;
while
(in_there < length - in_there) {
memcpy
(ptr, obj_data + start, in_there);
ptr += in_there;
in_there *=
2
;
}
if
(in_there < length) {
memcpy
(ptr, obj_data + start, length - in_there);
in_there = length;
}
}
template
<encoding encoding>
void
StringWrite
(
const
FunctionCallbackInfo<Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
ARGS_THIS
(args.
This
())
if
(!args[
0
]->
IsString
())
return
env->
ThrowTypeError
(
"
Argument must be a string
"
);
Local<String> str = args[
0
]->
ToString
(env->
isolate
());
if
(encoding ==
HEX
&& str->
Length
() %
2
!=
0
)
return
env->
ThrowTypeError
(
"
Invalid hex string
"
);
size_t
offset;
size_t
max_length;
CHECK_NOT_OOB
(
ParseArrayIndex
(args[
1
],
0
, &offset));
CHECK_NOT_OOB
(
ParseArrayIndex
(args[
2
], obj_length - offset, &max_length));
max_length =
MIN
(obj_length - offset, max_length);
if
(max_length ==
0
)
return
args.
GetReturnValue
().
Set
(
0
);
if
(offset >= obj_length)
return
env->
ThrowRangeError
(
"
Offset is out of bounds
"
);
uint32_t
written =
StringBytes::Write
(env->
isolate
(),
obj_data + offset,
max_length,
str,
encoding,
nullptr
);
args.
GetReturnValue
().
Set
(written);
}
void
Base64Write
(
const
FunctionCallbackInfo<Value>& args) {
StringWrite<
BASE64
>(args);
}
void
BinaryWrite
(
const
FunctionCallbackInfo<Value>& args) {
StringWrite<
BINARY
>(args);
}
void
Utf8Write
(
const
FunctionCallbackInfo<Value>& args) {
StringWrite<
UTF8
>(args);
}
void
Ucs2Write
(
const
FunctionCallbackInfo<Value>& args) {
StringWrite<
UCS2
>(args);
}
void
HexWrite
(
const
FunctionCallbackInfo<Value>& args) {
StringWrite<
HEX
>(args);
}
void
AsciiWrite
(
const
FunctionCallbackInfo<Value>& args) {
StringWrite<
ASCII
>(args);
}
static
inline
void
Swizzle
(
char
* start,
unsigned
int
len) {
char
* end = start + len -
1
;
while
(start < end) {
char
tmp = *start;
*start++ = *end;
*end-- = tmp;
}
}
template
<
typename
T,
enum
Endianness endianness>
void
ReadFloatGeneric
(
const
FunctionCallbackInfo<Value>& args) {
ARGS_THIS
(args[
0
].
As
<Object>());
uint32_t
offset = args[
1
]->
Uint32Value
();
CHECK_LE
(offset +
sizeof
(T), obj_length);
union
NoAlias {
T val;
char
bytes[
sizeof
(T)];
};
union
NoAlias na;
const
char
* ptr =
static_cast
<
const
char
*>(obj_data) + offset;
memcpy
(na.
bytes
, ptr,
sizeof
(na.
bytes
));
if
(endianness !=
GetEndianness
())
Swizzle
(na.
bytes
,
sizeof
(na.
bytes
));
args.
GetReturnValue
().
Set
(na.
val
);
}
void
ReadFloatLE
(
const
FunctionCallbackInfo<Value>& args) {
ReadFloatGeneric<
float
,
kLittleEndian
>(args);
}
void
ReadFloatBE
(
const
FunctionCallbackInfo<Value>& args) {
ReadFloatGeneric<
float
,
kBigEndian
>(args);
}
void
ReadDoubleLE
(
const
FunctionCallbackInfo<Value>& args) {
ReadFloatGeneric<
double
,
kLittleEndian
>(args);
}
void
ReadDoubleBE
(
const
FunctionCallbackInfo<Value>& args) {
ReadFloatGeneric<
double
,
kBigEndian
>(args);
}
template
<
typename
T,
enum
Endianness endianness>
uint32_t
WriteFloatGeneric
(
const
FunctionCallbackInfo<Value>& args) {
ARGS_THIS
(args[
0
].
As
<Object>())
T val = args[
1
]->
NumberValue
();
uint32_t
offset = args[
2
]->
Uint32Value
();
CHECK_LE
(offset +
sizeof
(T), obj_length);
union
NoAlias {
T val;
char
bytes[
sizeof
(T)];
};
union
NoAlias na = { val };
char
* ptr =
static_cast
<
char
*>(obj_data) + offset;
if
(endianness !=
GetEndianness
())
Swizzle
(na.
bytes
,
sizeof
(na.
bytes
));
memcpy
(ptr, na.
bytes
,
sizeof
(na.
bytes
));
return
offset +
sizeof
(na.
bytes
);
}
void
WriteFloatLE
(
const
FunctionCallbackInfo<Value>& args) {
args.
GetReturnValue
().
Set
(WriteFloatGeneric<
float
,
kLittleEndian
>(args));
}
void
WriteFloatBE
(
const
FunctionCallbackInfo<Value>& args) {
args.
GetReturnValue
().
Set
(WriteFloatGeneric<
float
,
kBigEndian
>(args));
}
void
WriteDoubleLE
(
const
FunctionCallbackInfo<Value>& args) {
args.
GetReturnValue
().
Set
(WriteFloatGeneric<
double
,
kLittleEndian
>(args));
}
void
WriteDoubleBE
(
const
FunctionCallbackInfo<Value>& args) {
args.
GetReturnValue
().
Set
(WriteFloatGeneric<
double
,
kBigEndian
>(args));
}
void
ByteLength
(
const
FunctionCallbackInfo<Value> &args) {
Environment* env =
Environment::GetCurrent
(args);
if
(!args[
0
]->
IsString
())
return
env->
ThrowTypeError
(
"
Argument must be a string
"
);
Local<String> s = args[
0
]->
ToString
(env->
isolate
());
enum
encoding e =
ParseEncoding
(env->
isolate
(), args[
1
],
UTF8
);
uint32_t
size =
StringBytes::Size
(env->
isolate
(), s, e);
args.
GetReturnValue
().
Set
(size);
}
void
Compare
(
const
FunctionCallbackInfo<Value> &args) {
Local<Object> obj_a = args[
0
].
As
<Object>();
char
* obj_a_data =
static_cast
<
char
*>(obj_a->
GetIndexedPropertiesExternalArrayData
());
size_t
obj_a_len = obj_a->
GetIndexedPropertiesExternalArrayDataLength
();
Local<Object> obj_b = args[
1
].
As
<Object>();
char
* obj_b_data =
static_cast
<
char
*>(obj_b->
GetIndexedPropertiesExternalArrayData
());
size_t
obj_b_len = obj_b->
GetIndexedPropertiesExternalArrayDataLength
();
size_t
cmp_length =
MIN
(obj_a_len, obj_b_len);
int32_t
val =
memcmp
(obj_a_data, obj_b_data, cmp_length);
//
Normalize val to be an integer in the range of [1, -1] since
//
implementations of memcmp() can vary by platform.
if
(val ==
0
) {
if
(obj_a_len > obj_b_len)
val =
1
;
else
if
(obj_a_len < obj_b_len)
val = -
1
;
}
else
{
if
(val >
0
)
val =
1
;
else
val = -
1
;
}
args.
GetReturnValue
().
Set
(val);
}
int32_t
IndexOf
(
const
char
* haystack,
size_t
h_length,
const
char
* needle,
size_t
n_length) {
CHECK_GE
(h_length, n_length);
//
TODO(trevnorris): Implement Boyer-Moore string search algorithm.
for
(
size_t
i =
0
; i < h_length - n_length +
1
; i++) {
if
(haystack[i] == needle[
0
]) {
if
(
memcmp
(haystack + i, needle, n_length) ==
0
)
return
i;
}
}
return
-
1
;
}
void
IndexOfString
(
const
FunctionCallbackInfo<Value>& args) {
ASSERT
(args[
0
]->
IsObject
());
ASSERT
(args[
1
]->
IsString
());
ASSERT
(args[
2
]->
IsNumber
());
ARGS_THIS
(args[
0
].
As
<Object>());
node::Utf8Value
str
(args.
GetIsolate
(), args[
1
]);
int32_t
offset_i32 = args[
2
]->
Int32Value
();
uint32_t
offset;
if
(offset_i32 <
0
) {
if
(offset_i32 +
static_cast
<
int32_t
>(obj_length) <
0
)
offset =
0
;
else
offset =
static_cast
<
uint32_t
>(obj_length + offset_i32);
}
else
{
offset =
static_cast
<
uint32_t
>(offset_i32);
}
if
(str.
length
() ==
0
||
obj_length ==
0
||
(offset !=
0
&& str.
length
() + offset <= str.
length
()) ||
str.
length
() + offset > obj_length)
return
args.
GetReturnValue
().
Set
(-
1
);
int32_t
r =
IndexOf
(obj_data + offset, obj_length - offset, *str, str.
length
());
args.
GetReturnValue
().
Set
(r == -
1
? -
1
:
static_cast
<
int32_t
>(r + offset));
}
void
IndexOfBuffer
(
const
FunctionCallbackInfo<Value>& args) {
ASSERT
(args[
0
]->
IsObject
());
ASSERT
(args[
1
]->
IsObject
());
ASSERT
(args[
2
]->
IsNumber
());
ARGS_THIS
(args[
0
].
As
<Object>());
Local<Object> buf = args[
1
].
As
<Object>();
int32_t
offset_i32 = args[
2
]->
Int32Value
();
size_t
buf_length = buf->
GetIndexedPropertiesExternalArrayDataLength
();
char
* buf_data =
static_cast
<
char
*>(buf->
GetIndexedPropertiesExternalArrayData
());
uint32_t
offset;
if
(buf_length >
0
)
CHECK_NE
(buf_data,
nullptr
);
if
(offset_i32 <
0
) {
if
(offset_i32 +
static_cast
<
int32_t
>(obj_length) <
0
)
offset =
0
;
else
offset =
static_cast
<
uint32_t
>(obj_length + offset_i32);
}
else
{
offset =
static_cast
<
uint32_t
>(offset_i32);
}
if
(buf_length ==
0
||
obj_length ==
0
||
(offset !=
0
&& buf_length + offset <= buf_length) ||
buf_length + offset > obj_length)
return
args.
GetReturnValue
().
Set
(-
1
);
int32_t
r =
IndexOf
(obj_data + offset, obj_length - offset, buf_data, buf_length);
args.
GetReturnValue
().
Set
(r == -
1
? -
1
:
static_cast
<
int32_t
>(r + offset));
}
void
IndexOfNumber
(
const
FunctionCallbackInfo<Value>& args) {
ASSERT
(args[
0
]->
IsObject
());
ASSERT
(args[
1
]->
IsNumber
());
ASSERT
(args[
2
]->
IsNumber
());
ARGS_THIS
(args[
0
].
As
<Object>());
uint32_t
needle = args[
1
]->
Uint32Value
();
int32_t
offset_i32 = args[
2
]->
Int32Value
();
uint32_t
offset;
if
(offset_i32 <
0
) {
if
(offset_i32 +
static_cast
<
int32_t
>(obj_length) <
0
)
offset =
0
;
else
offset =
static_cast
<
uint32_t
>(obj_length + offset_i32);
}
else
{
offset =
static_cast
<
uint32_t
>(offset_i32);
}
if
(obj_length ==
0
|| offset +
1
> obj_length)
return
args.
GetReturnValue
().
Set
(-
1
);
void
* ptr =
memchr
(obj_data + offset, needle, obj_length - offset);
char
* ptr_char =
static_cast
<
char
*>(ptr);
args.
GetReturnValue
().
Set
(
ptr ?
static_cast
<
int32_t
>(ptr_char - obj_data) : -
1
);
}
//
pass Buffer object to load prototype methods
void
SetupBufferJS
(
const
FunctionCallbackInfo<Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
CHECK
(args[
0
]->
IsFunction
());
Local<Function> bv = args[
0
].
As
<Function>();
env->
set_buffer_constructor_function
(bv);
Local<Value> proto_v = bv->
Get
(env->
prototype_string
());
CHECK
(proto_v->
IsObject
());
Local<Object> proto = proto_v.
As
<Object>();
env->
SetMethod
(proto,
"
asciiSlice
"
, AsciiSlice);
env->
SetMethod
(proto,
"
base64Slice
"
, Base64Slice);
env->
SetMethod
(proto,
"
binarySlice
"
, BinarySlice);
env->
SetMethod
(proto,
"
hexSlice
"
, HexSlice);
env->
SetMethod
(proto,
"
ucs2Slice
"
, Ucs2Slice);
env->
SetMethod
(proto,
"
utf8Slice
"
, Utf8Slice);
env->
SetMethod
(proto,
"
asciiWrite
"
, AsciiWrite);
env->
SetMethod
(proto,
"
base64Write
"
, Base64Write);
env->
SetMethod
(proto,
"
binaryWrite
"
, BinaryWrite);
env->
SetMethod
(proto,
"
hexWrite
"
, HexWrite);
env->
SetMethod
(proto,
"
ucs2Write
"
, Ucs2Write);
env->
SetMethod
(proto,
"
utf8Write
"
, Utf8Write);
env->
SetMethod
(proto,
"
copy
"
, Copy);
//
for backwards compatibility
proto->
ForceSet
(env->
offset_string
(),
Uint32::New
(env->
isolate
(),
0
),
v8::ReadOnly);
}
void
Initialize
(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Environment* env =
Environment::GetCurrent
(context);
env->
SetMethod
(target,
"
setupBufferJS
"
, SetupBufferJS);
env->
SetMethod
(target,
"
byteLength
"
, ByteLength);
env->
SetMethod
(target,
"
compare
"
, Compare);
env->
SetMethod
(target,
"
fill
"
, Fill);
env->
SetMethod
(target,
"
indexOfBuffer
"
, IndexOfBuffer);
env->
SetMethod
(target,
"
indexOfNumber
"
, IndexOfNumber);
env->
SetMethod
(target,
"
indexOfString
"
, IndexOfString);
env->
SetMethod
(target,
"
readDoubleBE
"
, ReadDoubleBE);
env->
SetMethod
(target,
"
readDoubleLE
"
, ReadDoubleLE);
env->
SetMethod
(target,
"
readFloatBE
"
, ReadFloatBE);
env->
SetMethod
(target,
"
readFloatLE
"
, ReadFloatLE);
env->
SetMethod
(target,
"
writeDoubleBE
"
, WriteDoubleBE);
env->
SetMethod
(target,
"
writeDoubleLE
"
, WriteDoubleLE);
env->
SetMethod
(target,
"
writeFloatBE
"
, WriteFloatBE);
env->
SetMethod
(target,
"
writeFloatLE
"
, WriteFloatLE);
}
}
//
namespace Buffer
}
//
namespace node
NODE_MODULE_CONTEXT_AWARE_BUILTIN
(buffer, node::Buffer::Initialize)
Back
|
FazBrowse Home
|
New Git URL