<commit_before>/**
 * WarfaceBot, a blind XMPP client for Warface (FPS)
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "def.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __MINGW32__
# include <Winsock.h>
#else
# include <sys/socket.h>
#endif

char *read_stream_keep(int fd)
{
    struct stream_hdr hdr;

    if (recv(fd, &hdr, sizeof (hdr), 0) != sizeof (hdr))
        return NULL;

    if (hdr.magic != 0xFEEDDEAD)
        return NULL;

    if (hdr.len == 0)
        return NULL;

    char *msg = calloc(hdr.len + 1, 1);
    char *curr_pos = msg;
    ssize_t read_size = 0;

    do {
        ssize_t size = recv(fd, curr_pos, hdr.len - (curr_pos - msg), 0);

        if (size <= 0)
        {
            free(msg);
            return NULL;
        }

        read_size += size;
        curr_pos += size;
    } while (read_size < hdr.len);
<commit_msg>Fix possible missed header chunk in stream_recv<commit_after>/**
 * WarfaceBot, a blind XMPP client for Warface (FPS)
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "def.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __MINGW32__
# include <Winsock.h>
#else
# include <sys/socket.h>
#endif

#include <errno.h>

char *read_stream_keep(int fd)
{
    struct stream_hdr hdr;
    char *hdr_pos = (char *) &hdr;
    size_t hdr_read = 0;

    do {
        ssize_t size = recv(fd, hdr_pos, sizeof(hdr) - (hdr_pos - (char *) &hdr), 0);

        if (size <= 0)
        {
            perror("read");
            return NULL;
        }
        hdr_read += size;
        hdr_pos += size;
    }
    while (hdr_read < sizeof (hdr));

    if (hdr.magic != 0xFEEDDEAD)
        return NULL;

    if (hdr.len == 0)
        return NULL;

    char *msg = calloc(hdr.len + 1, 1);
    char *curr_pos = msg;
    ssize_t read_size = 0;

    do {
        ssize_t size = recv(fd, curr_pos, hdr.len - (curr_pos - msg), 0);

        if (size <= 0)
        {
            free(msg);
            return NULL;
        }

        read_size += size;
        curr_pos += size;
    } while (read_size < hdr.len);

{'fraction_non_alphanumeric': 0.10963356973995272, 'fraction_numerical': 0.009456264775413711, 'mean_word_length': 2.5445026178010473, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7175201', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1075, 'n_words': 405}