<commit_before>/* Copyright (c) 2008-2014, Avian Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

#include "jni.h"
#include "avian/machine.h"
#include "sockets.h"
#include "jni-util.h"

using namespace avian::classpath::sockets;

extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_init(JNIEnv* e, jclass)
{
  init(e);
}

extern "C" JNIEXPORT SOCKET JNICALL
    Java_java_net_Socket_create(JNIEnv* e, jclass)
{
  return create(e);
}

extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_connect(JNIEnv* e,
                                                               jclass,
                                                               SOCKET sock,
                                                               long addr,
                                                               short port)
{
  connect(e, sock, addr, port);
}

extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_bind(JNIEnv* e,
                                                            jclass,
                                                            SOCKET sock,
                                                            long addr,
                                                            short port)
{
  bind(e, sock, addr, port);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_abort(JNIEnv* e, jclass, SOCKET sock)
{
  abort(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_close(JNIEnv* e, jclass, SOCKET sock)
{
  close(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_closeOutput(JNIEnv* e, jclass, SOCKET sock)
{
  close_output(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_closeInput(JNIEnv* e, jclass, SOCKET sock)
{
  close_input(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Avian_java_net_Socket_send(vm::Thread* t, vm::object, uintptr_t* arguments)
{ /* SOCKET s, object buffer_obj, int start_pos, int count  */
  SOCKET& s = *(reinterpret_cast<SOCKET*>(&arguments[0]));
  vm::GcByteArray* buffer_obj = vm::cast<vm::GcByteArray>(
      t, reinterpret_cast<vm::object>(arguments[2]));
  int32_t& start_pos = *(reinterpret_cast<int32_t*>(&arguments[3]));
  int32_t& count = *(reinterpret_cast<int32_t*>(&arguments[4]));
  char* buffer = reinterpret_cast<char*>(&buffer_obj->body()[start_pos]);
  avian::classpath::sockets::send((JNIEnv*)t, s, buffer, count);
}

extern "C" JNIEXPORT int64_t JNICALL
    Avian_java_net_Socket_recv(vm::Thread* t, vm::object, uintptr_t* arguments)
{ /* SOCKET s, object buffer_obj, int start_pos, int count  */
  SOCKET& s = *(reinterpret_cast<SOCKET*>(&arguments[0]));
  vm::GcByteArray* buffer_obj = vm::cast<vm::GcByteArray>(
      t, reinterpret_cast<vm::object>(arguments[2]));
  int32_t& start_pos = *(reinterpret_cast<int32_t*>(&arguments[3]));
  int32_t& count = *(reinterpret_cast<int32_t*>(&arguments[4]));
  char* buffer = reinterpret_cast<char*>(&buffer_obj->body()[start_pos]);
  return avian::classpath::sockets::recv((JNIEnv*)t, s, buffer, count);
}

extern "C" JNIEXPORT jint JNICALL
    Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e,
                                                 jclass,
                                                 jstring name)
{
  const char* chars = e->GetStringUTFChars(name, 0);
  if (chars) {
#ifdef PLATFORM_WINDOWS
    hostent* host = gethostbyname(chars);
    e->ReleaseStringUTFChars(name, chars);
    if (host) {
      return ntohl(reinterpret_cast<in_addr*>(host->h_addr_list[0])->s_addr);
    } else {<commit_msg>cp/avian/java-net.cpp: fix segfault<commit_after>/* Copyright (c) 2008-2014, Avian Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

#include "jni.h"
#include "avian/machine.h"
#include "sockets.h"
#include "jni-util.h"

using namespace avian::classpath::sockets;

extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_init(JNIEnv* e, jclass)
{
  init(e);
}

extern "C" JNIEXPORT SOCKET JNICALL
    Java_java_net_Socket_create(JNIEnv* e, jclass)
{
  return create(e);
}

extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_connect(JNIEnv* e,
                                                               jclass,
                                                               SOCKET sock,
                                                               long addr,
                                                               short port)
{
  connect(e, sock, addr, port);
}

extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_bind(JNIEnv* e,
                                                            jclass,
                                                            SOCKET sock,
                                                            long addr,
                                                            short port)
{
  bind(e, sock, addr, port);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_abort(JNIEnv* e, jclass, SOCKET sock)
{
  abort(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_close(JNIEnv* e, jclass, SOCKET sock)
{
  close(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_closeOutput(JNIEnv* e, jclass, SOCKET sock)
{
  close_output(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Java_java_net_Socket_closeInput(JNIEnv* e, jclass, SOCKET sock)
{
  close_input(e, sock);
}

extern "C" JNIEXPORT void JNICALL
    Avian_java_net_Socket_send(vm::Thread* t, vm::object, uintptr_t* arguments)
{ /* SOCKET s, object buffer_obj, int start_pos, int count  */
  SOCKET& s = *(reinterpret_cast<SOCKET*>(&arguments[0]));
  vm::GcByteArray* buffer_obj = vm::cast<vm::GcByteArray>(
      t, reinterpret_cast<vm::object>(arguments[2]));
  int32_t& start_pos = *(reinterpret_cast<int32_t*>(&arguments[3]));
  int32_t& count = *(reinterpret_cast<int32_t*>(&arguments[4]));
  char* buffer = reinterpret_cast<char*>(&buffer_obj->body()[start_pos]);
  avian::classpath::sockets::send((JNIEnv*)t, s, buffer, count);
}

extern "C" JNIEXPORT int64_t JNICALL
    Avian_java_net_Socket_recv(vm::Thread* t, vm::object, uintptr_t* arguments)
{ /* SOCKET s, object buffer_obj, int start_pos, int count  */
  SOCKET& s = *(reinterpret_cast<SOCKET*>(&arguments[0]));
  vm::GcByteArray* buffer_obj = vm::cast<vm::GcByteArray>(
      t, reinterpret_cast<vm::object>(arguments[2]));
  int32_t& start_pos = *(reinterpret_cast<int32_t*>(&arguments[3]));
  int32_t& count = *(reinterpret_cast<int32_t*>(&arguments[4]));
  char* buffer = reinterpret_cast<char*>(&buffer_obj->body()[start_pos]);
  return avian::classpath::sockets::recv((JNIEnv*)t, s, buffer, count);
}

extern "C" JNIEXPORT jint JNICALL
    Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e,
                                                 jclass,
                                                 jstring name)
{
  if(!name) {
    throwNew(e, "java/lang/NullPointerException", 0);
    return 0;
  }

  const char* chars = e->GetStringUTFChars(name, 0);
  if (chars) {
#ifdef PLATFORM_WINDOWS
    hostent* host = gethostbyname(chars);
    e->ReleaseStringUTFChars(name, chars);
    if (host) {
      return ntohl(reinterpret_cast<in_addr*>(host->h_addr_list[0])->s_addr);
    } else {
{'fraction_non_alphanumeric': 0.11512857329330374, 'fraction_numerical': 0.009920375930035244, 'mean_word_length': 2.521139705882353, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '622595', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2437, 'n_words': 552}