forked from cory/tildefriends
Move some things to C that probably should have never been in JS, especially sha1. Minor refactors, cleanup, and deletes along the way.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4154 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -72,15 +72,9 @@ static void _file_read_read_callback(uv_fs_t* req)
|
||||
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
|
||||
if (req->result >= 0)
|
||||
{
|
||||
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)fsreq->buffer, req->result);
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
|
||||
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
|
||||
JS_FreeValue(context, constructor);
|
||||
JS_FreeValue(context, global);
|
||||
JS_FreeValue(context, arrayBuffer);
|
||||
tf_task_resolve_promise(task, promise, typedArray);
|
||||
JS_FreeValue(context, typedArray);
|
||||
JSValue array = tf_util_new_uint8_array(context, (const uint8_t*)fsreq->buffer, req->result);
|
||||
tf_task_resolve_promise(task, promise, array);
|
||||
JS_FreeValue(context, array);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -823,25 +823,13 @@ void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffe
|
||||
JS_FreeValue(context, ref);
|
||||
}
|
||||
|
||||
static JSValue _newUint8Array(JSContext* context, const void* data, size_t length)
|
||||
{
|
||||
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)data, length);
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
|
||||
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
|
||||
JS_FreeValue(context, constructor);
|
||||
JS_FreeValue(context, global);
|
||||
JS_FreeValue(context, arrayBuffer);
|
||||
return typedArray;
|
||||
}
|
||||
|
||||
void _socket_notifyDataRead(socket_t* socket, const char* data, size_t length)
|
||||
{
|
||||
if (data && length > 0)
|
||||
{
|
||||
JSContext* context = tf_task_get_context(socket->_task);
|
||||
JSValue ref = JS_DupValue(context, socket->_object);
|
||||
JSValue typedArray = _newUint8Array(context, data, length);
|
||||
JSValue typedArray = tf_util_new_uint8_array(context, (const uint8_t*)data, length);
|
||||
JSValue args[] = { typedArray };
|
||||
if (!JS_IsUndefined(socket->_onRead))
|
||||
{
|
||||
@ -1072,7 +1060,7 @@ JSValue _socket_getPeerCertificate(JSContext* context, JSValueConst this_val, in
|
||||
int result = tf_tls_session_get_peer_certificate(socket->_tls, buffer, sizeof(buffer));
|
||||
if (result > 0)
|
||||
{
|
||||
return _newUint8Array(tf_task_get_context(socket->_task), buffer, sizeof(buffer));
|
||||
return tf_util_new_uint8_array(tf_task_get_context(socket->_task), (const uint8_t*)buffer, sizeof(buffer));
|
||||
}
|
||||
}
|
||||
return JS_UNDEFINED;
|
||||
|
103
src/util.js.c
103
src/util.js.c
@ -5,6 +5,8 @@
|
||||
#include "trace.h"
|
||||
|
||||
#include <base64c.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <picohttpparser.h>
|
||||
#include <quickjs-libc.h>
|
||||
#include <uv.h>
|
||||
@ -15,15 +17,9 @@ static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int
|
||||
{
|
||||
size_t length = 0;
|
||||
const char* value = JS_ToCStringLen(context, &length, argv[0]);
|
||||
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)value, length);
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
|
||||
JS_FreeValue(context, global);
|
||||
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
|
||||
JS_FreeValue(context, constructor);
|
||||
JS_FreeValue(context, arrayBuffer);
|
||||
JSValue typed_array = tf_util_new_uint8_array(context, (const uint8_t*)value, length);
|
||||
JS_FreeCString(context, value);
|
||||
return typedArray;
|
||||
return typed_array;
|
||||
}
|
||||
|
||||
static JSValue _util_utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
@ -70,17 +66,29 @@ static JSValue _util_base64_encode(JSContext* context, JSValueConst this_val, in
|
||||
{
|
||||
JSValue result = JS_UNDEFINED;
|
||||
size_t length = 0;
|
||||
const char* value = JS_ToCStringLen(context, &length, argv[0]);
|
||||
char* encoded = tf_malloc(length * 4);
|
||||
|
||||
int r = base64c_encode((const uint8_t*)value, length, (uint8_t*)encoded, length * 4);
|
||||
if (r >= 0)
|
||||
uint8_t* array = tf_util_try_get_array_buffer(context, &length, argv[0]);
|
||||
if (array)
|
||||
{
|
||||
result = JS_NewStringLen(context, encoded, r);
|
||||
char* encoded = tf_malloc(length * 4);
|
||||
int r = base64c_encode(array, length, (uint8_t*)encoded, length * 4);
|
||||
if (r >= 0)
|
||||
{
|
||||
result = JS_NewStringLen(context, encoded, r);
|
||||
}
|
||||
tf_free(encoded);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char* value = JS_ToCStringLen(context, &length, argv[0]);
|
||||
char* encoded = tf_malloc(length * 4);
|
||||
int r = base64c_encode((const uint8_t*)value, length, (uint8_t*)encoded, length * 4);
|
||||
if (r >= 0)
|
||||
{
|
||||
result = JS_NewStringLen(context, encoded, r);
|
||||
}
|
||||
tf_free(encoded);
|
||||
JS_FreeCString(context, value);
|
||||
}
|
||||
|
||||
tf_free(encoded);
|
||||
JS_FreeCString(context, value);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -291,6 +299,65 @@ static JSValue _util_parseHttp(JSContext* context, JSValueConst this_val, int ar
|
||||
|
||||
}
|
||||
|
||||
static JSValue _util_sha1_digest(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
size_t length = 0;
|
||||
const char* value = JS_ToCStringLen(context, &length, argv[0]);
|
||||
unsigned char digest[SHA_DIGEST_LENGTH] = { 0 };
|
||||
SHA1((const unsigned char*)value, length, digest);
|
||||
return JS_NewArrayBufferCopy(context, digest, sizeof(digest));
|
||||
}
|
||||
|
||||
JSValue tf_util_new_uint8_array(JSContext* context, const uint8_t* data, size_t size)
|
||||
{
|
||||
JSValue array_buffer = JS_NewArrayBufferCopy(context, data, size);
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
|
||||
JSValue result = JS_CallConstructor(context, constructor, 1, &array_buffer);
|
||||
JS_FreeValue(context, constructor);
|
||||
JS_FreeValue(context, global);
|
||||
JS_FreeValue(context, array_buffer);
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue _util_mask_bytes(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
JSValue result = JS_UNDEFINED;
|
||||
uint32_t mask = 0;
|
||||
JS_ToUint32(context, &mask, argv[1]);
|
||||
uint64_t double_mask = ((uint64_t)mask << 32) | mask;
|
||||
|
||||
size_t offset = 0;
|
||||
size_t length = 0;
|
||||
size_t element_size = 0;
|
||||
JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size);
|
||||
if (!JS_IsException(buffer))
|
||||
{
|
||||
size_t size = 0;
|
||||
const uint8_t* array = tf_util_try_get_array_buffer(context, &size, buffer);
|
||||
if (array)
|
||||
{
|
||||
uint8_t* copy = tf_malloc(size);
|
||||
size_t i = 0;
|
||||
for (; i + sizeof(double_mask) < size; i += sizeof(double_mask))
|
||||
{
|
||||
((uint64_t*)copy)[i / sizeof(double_mask)] = ((const uint64_t*)array)[i / sizeof(double_mask)] ^ double_mask;
|
||||
}
|
||||
for (; i + sizeof(mask) < size; i += sizeof(mask))
|
||||
{
|
||||
((uint32_t*)copy)[i / sizeof(mask)] = ((const uint32_t*)array)[i / sizeof(mask)] ^ mask;
|
||||
}
|
||||
for (; i < size; i++)
|
||||
{
|
||||
copy[i] = array[i] ^ ((mask >> (8 * (i % 4))) & 0xff);
|
||||
}
|
||||
result = tf_util_new_uint8_array(context, copy, size);
|
||||
tf_free(copy);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void tf_util_register(JSContext* context)
|
||||
{
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
@ -301,6 +368,8 @@ void tf_util_register(JSContext* context)
|
||||
JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1));
|
||||
JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _util_setTimeout, "setTimeout", 2));
|
||||
JS_SetPropertyStr(context, global, "parseHttp", JS_NewCFunction(context, _util_parseHttp, "parseHttp", 2));
|
||||
JS_SetPropertyStr(context, global, "sha1Digest", JS_NewCFunction(context, _util_sha1_digest, "sha1Digest", 1));
|
||||
JS_SetPropertyStr(context, global, "maskBytes", JS_NewCFunction(context, _util_mask_bytes, "maskBytes", 2));
|
||||
JS_FreeValue(context, global);
|
||||
}
|
||||
|
||||
|
@ -11,3 +11,4 @@ JSValue tf_util_try_get_typed_array_buffer(JSContext* context, JSValueConst obj,
|
||||
bool tf_util_report_error(JSContext* context, JSValue value);
|
||||
int tf_util_get_length(JSContext* context, JSValue value);
|
||||
int tf_util_insert_index(const void* key, const void* base, size_t count, size_t size, int (*compare)(const void*, const void*));
|
||||
JSValue tf_util_new_uint8_array(JSContext* context, const uint8_t* data, size_t size);
|
||||
|
Reference in New Issue
Block a user