nan_string_bytes.h 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #ifndef NAN_STRING_BYTES_H_
  22. #define NAN_STRING_BYTES_H_
  23. // Decodes a v8::Local<v8::String> or Buffer to a raw char*
  24. namespace imp {
  25. using v8::Local;
  26. using v8::Object;
  27. using v8::String;
  28. using v8::Value;
  29. //// Base 64 ////
  30. #define base64_encoded_size(size) ((size + 2 - ((size + 2) % 3)) / 3 * 4)
  31. //// HEX ////
  32. static bool contains_non_ascii_slow(const char* buf, size_t len) {
  33. for (size_t i = 0; i < len; ++i) {
  34. if (buf[i] & 0x80) return true;
  35. }
  36. return false;
  37. }
  38. static bool contains_non_ascii(const char* src, size_t len) {
  39. if (len < 16) {
  40. return contains_non_ascii_slow(src, len);
  41. }
  42. const unsigned bytes_per_word = sizeof(void*);
  43. const unsigned align_mask = bytes_per_word - 1;
  44. const unsigned unaligned = reinterpret_cast<uintptr_t>(src) & align_mask;
  45. if (unaligned > 0) {
  46. const unsigned n = bytes_per_word - unaligned;
  47. if (contains_non_ascii_slow(src, n)) return true;
  48. src += n;
  49. len -= n;
  50. }
  51. #if defined(__x86_64__) || defined(_WIN64)
  52. const uintptr_t mask = 0x8080808080808080ll;
  53. #else
  54. const uintptr_t mask = 0x80808080l;
  55. #endif
  56. const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src);
  57. for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) {
  58. if (srcw[i] & mask) return true;
  59. }
  60. const unsigned remainder = len & align_mask;
  61. if (remainder > 0) {
  62. const size_t offset = len - remainder;
  63. if (contains_non_ascii_slow(src + offset, remainder)) return true;
  64. }
  65. return false;
  66. }
  67. static void force_ascii_slow(const char* src, char* dst, size_t len) {
  68. for (size_t i = 0; i < len; ++i) {
  69. dst[i] = src[i] & 0x7f;
  70. }
  71. }
  72. static void force_ascii(const char* src, char* dst, size_t len) {
  73. if (len < 16) {
  74. force_ascii_slow(src, dst, len);
  75. return;
  76. }
  77. const unsigned bytes_per_word = sizeof(void*);
  78. const unsigned align_mask = bytes_per_word - 1;
  79. const unsigned src_unalign = reinterpret_cast<uintptr_t>(src) & align_mask;
  80. const unsigned dst_unalign = reinterpret_cast<uintptr_t>(dst) & align_mask;
  81. if (src_unalign > 0) {
  82. if (src_unalign == dst_unalign) {
  83. const unsigned unalign = bytes_per_word - src_unalign;
  84. force_ascii_slow(src, dst, unalign);
  85. src += unalign;
  86. dst += unalign;
  87. len -= src_unalign;
  88. } else {
  89. force_ascii_slow(src, dst, len);
  90. return;
  91. }
  92. }
  93. #if defined(__x86_64__) || defined(_WIN64)
  94. const uintptr_t mask = ~0x8080808080808080ll;
  95. #else
  96. const uintptr_t mask = ~0x80808080l;
  97. #endif
  98. const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src);
  99. uintptr_t* dstw = reinterpret_cast<uintptr_t*>(dst);
  100. for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) {
  101. dstw[i] = srcw[i] & mask;
  102. }
  103. const unsigned remainder = len & align_mask;
  104. if (remainder > 0) {
  105. const size_t offset = len - remainder;
  106. force_ascii_slow(src + offset, dst + offset, remainder);
  107. }
  108. }
  109. static size_t base64_encode(const char* src,
  110. size_t slen,
  111. char* dst,
  112. size_t dlen) {
  113. // We know how much we'll write, just make sure that there's space.
  114. assert(dlen >= base64_encoded_size(slen) &&
  115. "not enough space provided for base64 encode");
  116. dlen = base64_encoded_size(slen);
  117. unsigned a;
  118. unsigned b;
  119. unsigned c;
  120. unsigned i;
  121. unsigned k;
  122. unsigned n;
  123. static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  124. "abcdefghijklmnopqrstuvwxyz"
  125. "0123456789+/";
  126. i = 0;
  127. k = 0;
  128. n = slen / 3 * 3;
  129. while (i < n) {
  130. a = src[i + 0] & 0xff;
  131. b = src[i + 1] & 0xff;
  132. c = src[i + 2] & 0xff;
  133. dst[k + 0] = table[a >> 2];
  134. dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
  135. dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
  136. dst[k + 3] = table[c & 0x3f];
  137. i += 3;
  138. k += 4;
  139. }
  140. if (n != slen) {
  141. switch (slen - n) {
  142. case 1:
  143. a = src[i + 0] & 0xff;
  144. dst[k + 0] = table[a >> 2];
  145. dst[k + 1] = table[(a & 3) << 4];
  146. dst[k + 2] = '=';
  147. dst[k + 3] = '=';
  148. break;
  149. case 2:
  150. a = src[i + 0] & 0xff;
  151. b = src[i + 1] & 0xff;
  152. dst[k + 0] = table[a >> 2];
  153. dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
  154. dst[k + 2] = table[(b & 0x0f) << 2];
  155. dst[k + 3] = '=';
  156. break;
  157. }
  158. }
  159. return dlen;
  160. }
  161. static size_t hex_encode(const char* src, size_t slen, char* dst, size_t dlen) {
  162. // We know how much we'll write, just make sure that there's space.
  163. assert(dlen >= slen * 2 &&
  164. "not enough space provided for hex encode");
  165. dlen = slen * 2;
  166. for (uint32_t i = 0, k = 0; k < dlen; i += 1, k += 2) {
  167. static const char hex[] = "0123456789abcdef";
  168. uint8_t val = static_cast<uint8_t>(src[i]);
  169. dst[k + 0] = hex[val >> 4];
  170. dst[k + 1] = hex[val & 15];
  171. }
  172. return dlen;
  173. }
  174. static Local<Value> Encode(const char* buf,
  175. size_t buflen,
  176. enum Encoding encoding) {
  177. assert(buflen <= node::Buffer::kMaxLength);
  178. if (!buflen && encoding != BUFFER)
  179. return New("").ToLocalChecked();
  180. Local<String> val;
  181. switch (encoding) {
  182. case BUFFER:
  183. return CopyBuffer(buf, buflen).ToLocalChecked();
  184. case ASCII:
  185. if (contains_non_ascii(buf, buflen)) {
  186. char* out = new char[buflen];
  187. force_ascii(buf, out, buflen);
  188. val = New<String>(out, buflen).ToLocalChecked();
  189. delete[] out;
  190. } else {
  191. val = New<String>(buf, buflen).ToLocalChecked();
  192. }
  193. break;
  194. case UTF8:
  195. val = New<String>(buf, buflen).ToLocalChecked();
  196. break;
  197. case BINARY: {
  198. // TODO(isaacs) use ExternalTwoByteString?
  199. const unsigned char *cbuf = reinterpret_cast<const unsigned char*>(buf);
  200. uint16_t * twobytebuf = new uint16_t[buflen];
  201. for (size_t i = 0; i < buflen; i++) {
  202. // XXX is the following line platform independent?
  203. twobytebuf[i] = cbuf[i];
  204. }
  205. val = New<String>(twobytebuf, buflen).ToLocalChecked();
  206. delete[] twobytebuf;
  207. break;
  208. }
  209. case BASE64: {
  210. size_t dlen = base64_encoded_size(buflen);
  211. char* dst = new char[dlen];
  212. size_t written = base64_encode(buf, buflen, dst, dlen);
  213. assert(written == dlen);
  214. val = New<String>(dst, dlen).ToLocalChecked();
  215. delete[] dst;
  216. break;
  217. }
  218. case UCS2: {
  219. const uint16_t* data = reinterpret_cast<const uint16_t*>(buf);
  220. val = New<String>(data, buflen / 2).ToLocalChecked();
  221. break;
  222. }
  223. case HEX: {
  224. size_t dlen = buflen * 2;
  225. char* dst = new char[dlen];
  226. size_t written = hex_encode(buf, buflen, dst, dlen);
  227. assert(written == dlen);
  228. val = New<String>(dst, dlen).ToLocalChecked();
  229. delete[] dst;
  230. break;
  231. }
  232. default:
  233. assert(0 && "unknown encoding");
  234. break;
  235. }
  236. return val;
  237. }
  238. #undef base64_encoded_size
  239. } // end of namespace imp
  240. #endif // NAN_STRING_BYTES_H_