2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
|
// following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2013-05-01 15:40:43 -07:00
|
|
|
#ifndef SRC_STRING_BYTES_H_
|
|
|
|
|
#define SRC_STRING_BYTES_H_
|
|
|
|
|
|
2016-05-24 09:54:05 +02:00
|
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
|
|
2015-07-18 11:34:16 +02:00
|
|
|
// Decodes a v8::Local<v8::String> or Buffer to a raw char*
|
2013-05-01 15:40:43 -07:00
|
|
|
|
|
|
|
|
#include "v8.h"
|
2017-10-30 08:54:49 +01:00
|
|
|
#include "env.h"
|
2013-05-01 15:40:43 -07:00
|
|
|
|
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
|
|
class StringBytes {
|
|
|
|
|
public:
|
2016-04-23 22:04:30 +02:00
|
|
|
class InlineDecoder : public MaybeStackBuffer<char> {
|
2015-01-30 15:35:11 +03:00
|
|
|
public:
|
|
|
|
|
inline bool Decode(Environment* env,
|
2015-07-18 11:34:16 +02:00
|
|
|
v8::Local<v8::String> string,
|
|
|
|
|
v8::Local<v8::Value> encoding,
|
2015-01-30 15:35:11 +03:00
|
|
|
enum encoding _default) {
|
|
|
|
|
enum encoding enc = ParseEncoding(env->isolate(), encoding, _default);
|
2017-05-17 17:33:35 -04:00
|
|
|
if (!StringBytes::IsValidString(string, enc)) {
|
2015-01-30 15:35:11 +03:00
|
|
|
env->ThrowTypeError("Bad input string");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 22:04:30 +02:00
|
|
|
const size_t storage = StringBytes::StorageSize(env->isolate(),
|
|
|
|
|
string,
|
|
|
|
|
enc);
|
|
|
|
|
AllocateSufficientStorage(storage);
|
|
|
|
|
const size_t length = StringBytes::Write(env->isolate(),
|
|
|
|
|
out(),
|
|
|
|
|
storage,
|
|
|
|
|
string,
|
|
|
|
|
enc);
|
|
|
|
|
|
|
|
|
|
// No zero terminator is included when using this method.
|
|
|
|
|
SetLength(length);
|
2015-01-30 15:35:11 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 22:04:30 +02:00
|
|
|
inline size_t size() const { return length(); }
|
2015-01-30 15:35:11 +03:00
|
|
|
};
|
|
|
|
|
|
2013-07-30 14:26:11 +02:00
|
|
|
// Does the string match the encoding? Quick but non-exhaustive.
|
|
|
|
|
// Example: a HEX string must have a length that's a multiple of two.
|
|
|
|
|
// FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...
|
2017-05-17 17:33:35 -04:00
|
|
|
static bool IsValidString(v8::Local<v8::String> string,
|
2014-02-21 17:02:42 +04:00
|
|
|
enum encoding enc);
|
2013-07-30 14:26:11 +02:00
|
|
|
|
2013-05-01 15:40:43 -07:00
|
|
|
// Fast, but can be 2 bytes oversized for Base64, and
|
|
|
|
|
// as much as triple UTF-8 strings <= 65536 chars in length
|
2014-02-21 17:02:42 +04:00
|
|
|
static size_t StorageSize(v8::Isolate* isolate,
|
2015-07-18 11:34:16 +02:00
|
|
|
v8::Local<v8::Value> val,
|
2014-02-21 17:02:42 +04:00
|
|
|
enum encoding enc);
|
2013-05-01 15:40:43 -07:00
|
|
|
|
|
|
|
|
// Precise byte count, but slightly slower for Base64 and
|
|
|
|
|
// very much slower for UTF-8
|
2014-02-21 17:02:42 +04:00
|
|
|
static size_t Size(v8::Isolate* isolate,
|
2015-07-18 11:34:16 +02:00
|
|
|
v8::Local<v8::Value> val,
|
2014-02-21 17:02:42 +04:00
|
|
|
enum encoding enc);
|
2013-05-01 15:40:43 -07:00
|
|
|
|
|
|
|
|
// Write the bytes from the string or buffer into the char*
|
|
|
|
|
// returns the number of bytes written, which will always be
|
|
|
|
|
// <= buflen. Use StorageSize/Size first to know how much
|
|
|
|
|
// memory to allocate.
|
2014-02-21 17:02:42 +04:00
|
|
|
static size_t Write(v8::Isolate* isolate,
|
|
|
|
|
char* buf,
|
2013-05-01 15:40:43 -07:00
|
|
|
size_t buflen,
|
2015-07-18 11:34:16 +02:00
|
|
|
v8::Local<v8::Value> val,
|
2013-05-01 15:40:43 -07:00
|
|
|
enum encoding enc,
|
2014-10-22 03:29:32 +02:00
|
|
|
int* chars_written = nullptr);
|
2013-05-01 15:40:43 -07:00
|
|
|
|
|
|
|
|
// Take the bytes in the src, and turn it into a Buffer or String.
|
2014-12-10 17:33:56 +01:00
|
|
|
// Don't call with encoding=UCS2.
|
2017-04-30 18:53:04 +02:00
|
|
|
static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
|
|
|
|
|
const char* buf,
|
|
|
|
|
size_t buflen,
|
|
|
|
|
enum encoding encoding,
|
|
|
|
|
v8::Local<v8::Value>* error);
|
2014-02-21 17:02:42 +04:00
|
|
|
|
2014-12-10 17:45:50 +01:00
|
|
|
// The input buffer should be in host endianness.
|
2017-04-30 18:53:04 +02:00
|
|
|
static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
|
|
|
|
|
const uint16_t* buf,
|
|
|
|
|
size_t buflen,
|
|
|
|
|
v8::Local<v8::Value>* error);
|
|
|
|
|
|
|
|
|
|
static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
|
|
|
|
|
const char* buf,
|
|
|
|
|
enum encoding encoding,
|
|
|
|
|
v8::Local<v8::Value>* error);
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
|
2015-08-20 16:57:14 -07:00
|
|
|
private:
|
|
|
|
|
static size_t WriteUCS2(char* buf,
|
|
|
|
|
size_t buflen,
|
|
|
|
|
v8::Local<v8::String> str,
|
|
|
|
|
int flags,
|
|
|
|
|
size_t* chars_written);
|
2013-05-01 15:40:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace node
|
|
|
|
|
|
2016-05-24 09:54:05 +02:00
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
|
|
2013-05-01 15:40:43 -07:00
|
|
|
#endif // SRC_STRING_BYTES_H_
|