<commit_before>#include <node.h>
#include <node_buffer.h>
#include <nan.h>
#include <sodium.h>
#include "src/crypto_generichash_wrap.h"
#include "src/crypto_onetimeauth_wrap.h"
#include "src/crypto_hash_sha256_wrap.h"
#include "src/crypto_hash_sha512_wrap.h"
#include "src/crypto_stream_xor_wrap.h"
#include "src/crypto_stream_chacha20_xor_wrap.h"
#include "src/crypto_secretstream_xchacha20poly1305_state_wrap.h"
#include "src/crypto_pwhash_async.cc"
#include "src/crypto_pwhash_str_async.cc"
#include "src/crypto_pwhash_str_verify_async.cc"
#include "src/macros.h"

// memory management

NAN_METHOD(sodium_memzero) {
  ASSERT_BUFFER(info[0], buf)

  sodium_memzero(CDATA(buf), CLENGTH(buf));
}

NAN_METHOD(sodium_mlock) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mlock(CDATA(buf), CLENGTH(buf)))
}

NAN_METHOD(sodium_munlock) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_munlock(CDATA(buf), CLENGTH(buf)))
}

static void SodiumFreeCallback (char * data, void * hint) {
  sodium_free((void *) data);
}

NAN_GETTER(SodiumMemorySecureAccessor) {
  info.GetReturnValue().Set(Nan::True());
}

NAN_METHOD(sodium_malloc) {
  ASSERT_UINT_BOUNDS(info[0], size, 0, 0, `buffer.constants.MAX_LENGTH`, node::Buffer::kMaxLength)

  void* ptr = sodium_malloc(size);

  if (ptr == NULL) {
    Nan::ThrowError(ERRNO_EXCEPTION(errno));
    return;
  }

  v8::Local<v8::Object> buf = Nan::NewBuffer(
    (char *)ptr,
    size,
    SodiumFreeCallback,
    NULL
  ).ToLocalChecked();

  Nan::SetAccessor(buf, LOCAL_STRING("secure"), SodiumMemorySecureAccessor);

  info.GetReturnValue().Set(buf);
}

NAN_METHOD(sodium_mprotect_noaccess) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mprotect_noaccess(node::Buffer::Data(buf)))
}

NAN_METHOD(sodium_mprotect_readonly) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mprotect_readonly(node::Buffer::Data(buf)))
}

NAN_METHOD(sodium_mprotect_readwrite) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mprotect_readwrite(node::Buffer::Data(buf)))
}

// randombytes

NAN_METHOD(randombytes_random) {
  info.GetReturnValue().Set(Nan::New(randombytes_random()));
}

NAN_METHOD(randombytes_uniform) {
  ASSERT_UINT_BOUNDS(info[0], upper_bound, 0, 0, 0xffffffff, 0xffffffff)

  info.GetReturnValue().Set(Nan::New(randombytes_uniform(upper_bound)));
}

NAN_METHOD(randombytes_buf) {
  ASSERT_BUFFER(info[0], random)

  randombytes_buf(CDATA(random), CLENGTH(random));
}

NAN_METHOD(randombytes_buf_deterministic) {
  ASSERT_BUFFER(info[0], random)
  ASSERT_BUFFER_MIN_LENGTH(info[1], seed, randombytes_SEEDBYTES, randombytes_seedbytes())

  randombytes_buf_deterministic(CDATA(random), CLENGTH(random), CDATA(seed));
}

// helpers

NAN_METHOD(sodium_memcmp) {
  ASSERT_BUFFER(info[0], b1)
  ASSERT_BUFFER(info[1], b2)
  ASSERT_UINT(info[2], length)

  CALL_SODIUM_BOOL(sodium_memcmp(CDATA(b1), CDATA(b2), length))
}

NAN_METHOD(sodium_compare) {
  ASSERT_BUFFER(info[0], b1)
  ASSERT_BUFFER(info[1], b2)
  ASSERT_UINT(info[2], length)

  info.GetReturnValue().Set(Nan::New<v8::Number>(sodium_compare(CDATA(b1), CDATA(b2), length)));
}

NAN_METHOD(sodium_pad) {<commit_msg>Guard against overflow<commit_after>#include <node.h>
#include <node_buffer.h>
#include <nan.h>
#include <sodium.h>
#include "src/crypto_generichash_wrap.h"
#include "src/crypto_onetimeauth_wrap.h"
#include "src/crypto_hash_sha256_wrap.h"
#include "src/crypto_hash_sha512_wrap.h"
#include "src/crypto_stream_xor_wrap.h"
#include "src/crypto_stream_chacha20_xor_wrap.h"
#include "src/crypto_secretstream_xchacha20poly1305_state_wrap.h"
#include "src/crypto_pwhash_async.cc"
#include "src/crypto_pwhash_str_async.cc"
#include "src/crypto_pwhash_str_verify_async.cc"
#include "src/macros.h"

// memory management

NAN_METHOD(sodium_memzero) {
  ASSERT_BUFFER(info[0], buf)

  sodium_memzero(CDATA(buf), CLENGTH(buf));
}

NAN_METHOD(sodium_mlock) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mlock(CDATA(buf), CLENGTH(buf)))
}

NAN_METHOD(sodium_munlock) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_munlock(CDATA(buf), CLENGTH(buf)))
}

static void SodiumFreeCallback (char * data, void * hint) {
  sodium_free((void *) data);
}

NAN_GETTER(SodiumMemorySecureAccessor) {
  info.GetReturnValue().Set(Nan::True());
}

NAN_METHOD(sodium_malloc) {
  ASSERT_UINT_BOUNDS(info[0], size, 0, 0, `buffer.constants.MAX_LENGTH`, node::Buffer::kMaxLength)

  void* ptr = sodium_malloc(size);

  if (ptr == NULL) {
    Nan::ThrowError(ERRNO_EXCEPTION(errno));
    return;
  }

  v8::Local<v8::Object> buf = Nan::NewBuffer(
    (char *)ptr,
    size,
    SodiumFreeCallback,
    NULL
  ).ToLocalChecked();

  Nan::SetAccessor(buf, LOCAL_STRING("secure"), SodiumMemorySecureAccessor);

  info.GetReturnValue().Set(buf);
}

NAN_METHOD(sodium_mprotect_noaccess) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mprotect_noaccess(node::Buffer::Data(buf)))
}

NAN_METHOD(sodium_mprotect_readonly) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mprotect_readonly(node::Buffer::Data(buf)))
}

NAN_METHOD(sodium_mprotect_readwrite) {
  ASSERT_BUFFER(info[0], buf)

  CALL_SODIUM(sodium_mprotect_readwrite(node::Buffer::Data(buf)))
}

// randombytes

NAN_METHOD(randombytes_random) {
  info.GetReturnValue().Set(Nan::New(randombytes_random()));
}

NAN_METHOD(randombytes_uniform) {
  ASSERT_UINT_BOUNDS(info[0], upper_bound, 0, 0, 0xffffffff, 0xffffffff)

  info.GetReturnValue().Set(Nan::New(randombytes_uniform(upper_bound)));
}

NAN_METHOD(randombytes_buf) {
  ASSERT_BUFFER(info[0], random)

  randombytes_buf(CDATA(random), CLENGTH(random));
}

NAN_METHOD(randombytes_buf_deterministic) {
  ASSERT_BUFFER(info[0], random)
  ASSERT_BUFFER_MIN_LENGTH(info[1], seed, randombytes_SEEDBYTES, randombytes_seedbytes())

  randombytes_buf_deterministic(CDATA(random), CLENGTH(random), CDATA(seed));
}

// helpers

NAN_METHOD(sodium_memcmp) {
  ASSERT_UINT(info[2], length)
  ASSERT_BUFFER_MIN_LENGTH(info[0], b1, length)
  ASSERT_BUFFER_MIN_LENGTH(info[1], b2, length)

  CALL_SODIUM_BOOL(sodium_memcmp(CDATA(b1), CDATA(b2), length))
}

NAN_METHOD(sodium_compare) {
  ASSERT_UINT(info[2], length)
  ASSERT_BUFFER_MIN_LENGTH(info[0], b1, length)
  ASSERT_BUFFER_MIN_LENGTH(info[1], b2, length)

  info.GetReturnValue().Set(Nan::New<v8::Number>(sodium_compare(CDATA(b1), CDATA(b2), length)));
}

NAN_METHOD(sodium_pad) {
{'fraction_non_alphanumeric': 0.14548889938592346, 'fraction_numerical': 0.015115729806329713, 'mean_word_length': 7.572199730094467, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6733724', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2472, 'n_words': 298}