<commit_before>/* Copyright 2012 <NAME>.
   This file is part of SGLib.  SGLib is licensed under the terms of the
   2-clause BSD license.  For more information, see LICENSE.txt. */
#include "sg/defs.h"
#include "sg/error.h"
#include "sg/net.h"
#include "private.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#if !defined(_WIN32)
#include <arpa/inet.h>
#include <netdb.h>
#else
#include <Windows.h>
#endif

#define DYNAMIC_PROCS 0

#if !defined(_WIN32)

int
sg_net_init(void)
{
    return 1;
}

#else

#pragma comment(lib, "Ws2_32.lib")

#if NTDDI_VERSION < NTDDI_VISTA
typedef int (WSAAPI *inet_pton_t)(int, const char *, void *);
typedef const char *(WSAAPI *inet_ntop_t)(int, const void *, char *, size_t);
static inet_pton_t inet_pton;
static inet_ntop_t inet_ntop;

static int WSAAPI
sg_net_inet_pton(int af, const char *src, void *dest)
{
    DWORD alen;
    int r;
    union {
        struct sockaddr addr;
        struct sockaddr_in in;
        struct sockaddr_in6 in6;
    } a;

    switch (af) {
    case AF_INET:
        alen = sizeof(a.in);
        r = WSAStringToAddressA((char *) src, af, NULL, &a.addr, &alen);
        if (r) return -1;
        memcpy(dest, &a.in.sin_addr, sizeof(struct in_addr));
        return 1;

    case AF_INET6:
        alen = sizeof(a.in6);
        r = WSAStringToAddressA((char *) src, af, NULL, &a.addr, &alen);
        if (r) return -1;
        memcpy(dest, &a.in6.sin6_addr, sizeof(struct in6_addr));
        return 1;

    default:
        WSASetLastError(WSAEINVAL);
        return -1;
    }
}

static const char * WSAAPI
sg_net_inet_ntop(int af, const void *src, char *dest, size_t len)
{
    DWORD slen = (DWORD) len;
    int r;
    union {
        struct sockaddr addr;
        struct sockaddr_in in;
        struct sockaddr_in6 in6;
    } a;

    switch (af) {
    case AF_INET:
        memset(&a.in, 0, sizeof(a.in));
        a.in.sin_family = AF_INET;
        memcpy(&a.in.sin_addr, src, sizeof(struct in_addr));
        r = WSAAddressToStringA(&a.addr, sizeof(a.in), NULL, dest, &slen);
        if (r) return NULL;
        return dest;

    case AF_INET6:
        memset(&a.in6, 0, sizeof(a.in6));
        a.in6.sin6_family = AF_INET6;
        memcpy(&a.in6.sin6_addr, src, sizeof(struct in6_addr));
        r = WSAAddressToStringA(&a.addr, sizeof(a.in6), NULL, dest, &slen);
        if (r) return NULL;
        return dest;

    default:
        WSASetLastError(WSAEINVAL);
        return NULL;
    }
}

static void
sg_net_getprocs(void)
{
    HANDLE h = GetModuleHandleA("Ws2_32.dll");
    if (h) {
        inet_pton = (inet_pton_t) GetProcAddress(h, "inet_pton");
        inet_ntop = (inet_ntop_t) GetProcAddress(h, "inet_ntop");
    }
    if (!inet_pton) inet_pton = sg_net_inet_pton;
    if (!inet_ntop) inet_ntop = sg_net_inet_ntop;
}
#else
#define sg_net_getprocs(x) (void)0
#endif

int
sg_net_init(void)
{
    static int initted;
    WSADATA d;
    int r;

    if (initted)
        return initted - 1;
    r = WSAStartup(MAKEWORD(2, 2), &d);
    if (r) {
        initted = 1;<commit_msg>windows: remove usage of deprecated functions<commit_after>/* Copyright 2012-2014 <NAME>.
   This file is part of SGLib.  SGLib is licensed under the terms of the
   2-clause BSD license.  For more information, see LICENSE.txt. */
#include "sg/defs.h"
#include "sg/error.h"
#include "sg/net.h"
#include "private.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#if !defined(_WIN32)
#include <arpa/inet.h>
#include <netdb.h>
#else
#include <Windows.h>
#endif

#define DYNAMIC_PROCS 0

#if !defined(_WIN32)

int
sg_net_init(void)
{
    return 1;
}

#else

#pragma comment(lib, "Ws2_32.lib")

#if NTDDI_VERSION < NTDDI_VISTA
typedef int (WSAAPI *inet_pton_t)(int, const char *, void *);
typedef const char *(WSAAPI *inet_ntop_t)(int, const void *, char *, size_t);
static inet_pton_t inet_pton;
static inet_ntop_t inet_ntop;

static int WSAAPI
sg_net_inet_pton(int af, const char *src, void *dest)
{
    wchar_t wsrc[INET6_ADDRSTRLEN];
    int wsrclen;
    DWORD alen;
    int r;
    union {
        struct sockaddr addr;
        struct sockaddr_in in;
        struct sockaddr_in6 in6;
    } a;

    if (!*src)
        return -1;

    wsrclen = MultiByteToWideChar(
        CP_UTF8, 0, src, strlen(src), wsrc, INET6_ADDRSTRLEN - 1);
    if (wsrclen == 0 || wsrclen >= INET6_ADDRSTRLEN) {
        WSASetLastError(WSAEINVAL);
        return -1;
    }

    switch (af) {
    case AF_INET:
        alen = sizeof(a.in);
        r = WSAStringToAddress(wsrc, af, NULL, &a.addr, &alen);
        if (r)
            return -1;
        memcpy(dest, &a.in.sin_addr, sizeof(struct in_addr));
        return 1;

    case AF_INET6:
        alen = sizeof(a.in6);
        r = WSAStringToAddress(wsrc, af, NULL, &a.addr, &alen);
        if (r)
            return -1;
        memcpy(dest, &a.in6.sin6_addr, sizeof(struct in6_addr));
        return 1;

    default:
        WSASetLastError(WSAEINVAL);
        return -1;
    }
}

static const char * WSAAPI
sg_net_inet_ntop(int af, const void *src, char *dest, size_t len)
{
    int r;
    wchar_t wdest[INET_ADDRSTRLEN];
    DWORD wdestlen = INET_ADDRSTRLEN;
    union {
        struct sockaddr addr;
        struct sockaddr_in in;
        struct sockaddr_in6 in6;
    } a;

    switch (af) {
    case AF_INET:
        memset(&a.in, 0, sizeof(a.in));
        a.in.sin_family = AF_INET;
        memcpy(&a.in.sin_addr, src, sizeof(struct in_addr));
        r = WSAAddressToString(
            &a.addr, sizeof(a.in), NULL, wdest, &wdestlen);
        if (r)
            return NULL;
        break;

    case AF_INET6:
        memset(&a.in6, 0, sizeof(a.in6));
        a.in6.sin6_family = AF_INET6;
        memcpy(&a.in6.sin6_addr, src, sizeof(struct in6_addr));
        r = WSAAddressToString(
            &a.addr, sizeof(a.in6), NULL, wdest, &wdestlen);
        if (r)
            return NULL;
        break;

    default:
        WSASetLastError(WSAEINVAL);
        return NULL;
    }

    r = WideCharToMultiByte(
        CP_UTF8, 0, wdest, wdestlen, dest, (int)len, NULL, NULL);
    if (!r) {
        WSASetLastError(WSAEINVAL);
        return NULL;
    }

    return dest;
}

static void
sg_net_getprocs(void)
{
    HANDLE h = GetModuleHandleA("Ws2_32.dll");
    if (h) {
        inet_pton = (inet_pton_t) GetProcAddress(h, "inet_pton");
        inet_ntop = (inet_ntop_t) GetProcAddress(h, "inet_ntop");
    }
    if (!inet_pton) inet_pton = sg_net_inet_pton;
    if (!inet_ntop) inet_ntop = sg_net_inet_ntop;
}
#else
#define sg_net_getprocs(x) (void)0
#endif

int
sg_net_init(void)
{
    static int initted;
    WSADATA d;
    int r;

    if (initted)
        return initted - 1;
    r = WSAStartup(MAKEWORD(2, 2), &d);
    if (r) {
        initted = 1;
{'fraction_non_alphanumeric': 0.12593681116825864, 'fraction_numerical': 0.016311535635562088, 'mean_word_length': 2.595351294241944, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4575427', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2631, 'n_words': 637}