<commit_before>/*
  Copyright (c) 2016 <NAME>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
 */

#include "pgrouter.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

MSG* pgr_m_new()
{
	MSG *m = calloc(1, sizeof(MSG));
	if (!m) {
		pgr_abort(ABORT_MEMFAIL);
	}
	m->free = sizeof(m->buf);
	return m;
}

void pgr_m_init(MSG *m)
{
	memset(m, 0, sizeof(MSG));
	m->free = sizeof(m->buf);
}

void pgr_m_skip(MSG *m, size_t n)
{
	assert(pgr_m_unread(m) >= n);
	m->offset += n;
}

void pgr_m_discard(MSG *m, int fd)
{
	int len;
	ssize_t n;

	if (pgr_m_u8_at(m,0) == 0) {
		len = pgr_m_u32_at(m,0);
	} else {
		len = 1 + pgr_m_u32_at(m,1);
	}

	n = pgr_m_unread(m);
	while (len > n) {
		/* there's more data on the wire.                 */
		/* drop what we've got, and then go read the rest */
		len -= n;

		m->offset = 0;
		m->free = MSG_BUFSIZ;
		n = read(fd, m->buf, m->free);
		if (n <= 0) {
			return;
		}
	}

	pgr_m_skip(m, len);
	pgr_m_flush(m);
}

void pgr_m_flush(MSG *m)
{
	if (m->offset > 0 && pgr_m_unread(m) > 0) {
		memmove(m->buf, m->buf + pgr_m_offset(m), pgr_m_unread(m));
		m->free += m->offset;
		m->offset = 0;
	}
}

int pgr_m_write(MSG *m, const void *buf, size_t len)
{
	if (len > m->free) {
		memcpy(m->buf + pgr_m_used(m), buf, m->free);
		len -= m->free;
		m->free = 0;
		return len;
	}

	memcpy(m->buf + pgr_m_used(m), buf, len);
	m->free -= len;
	return 0;
<commit_msg>Fix `pgr_m_flush` to always work<commit_after>/*
  Copyright (c) 2016 <NAME>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
 */

#include "pgrouter.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

MSG* pgr_m_new()
{
	MSG *m = calloc(1, sizeof(MSG));
	if (!m) {
		pgr_abort(ABORT_MEMFAIL);
	}
	m->free = sizeof(m->buf);
	return m;
}

void pgr_m_init(MSG *m)
{
	memset(m, 0, sizeof(MSG));
	m->free = sizeof(m->buf);
}

void pgr_m_skip(MSG *m, size_t n)
{
	assert(pgr_m_unread(m) >= n);
	m->offset += n;
}

void pgr_m_discard(MSG *m, int fd)
{
	int len;
	ssize_t n;

	if (pgr_m_u8_at(m,0) == 0) {
		len = pgr_m_u32_at(m,0);
	} else {
		len = 1 + pgr_m_u32_at(m,1);
	}

	n = pgr_m_unread(m);
	while (len > n) {
		/* there's more data on the wire.                 */
		/* drop what we've got, and then go read the rest */
		len -= n;

		m->offset = 0;
		m->free = MSG_BUFSIZ;
		n = read(fd, m->buf, m->free);
		if (n <= 0) {
			return;
		}
	}

	pgr_m_skip(m, len);
	pgr_m_flush(m);
}

void pgr_m_flush(MSG *m)
{
	if (m->offset > 0 && pgr_m_unread(m) > 0) {
		memmove(m->buf, m->buf + pgr_m_offset(m), pgr_m_unread(m));
	}
	m->free += m->offset;
	m->offset = 0;
}

int pgr_m_write(MSG *m, const void *buf, size_t len)
{
	if (len > m->free) {
		memcpy(m->buf + pgr_m_used(m), buf, m->free);
		len -= m->free;
		m->free = 0;
		return len;
	}

	memcpy(m->buf + pgr_m_used(m), buf, len);
	m->free -= len;
	return 0;

{'fraction_non_alphanumeric': 0.15388458656007994, 'fraction_numerical': 0.011491381463902073, 'mean_word_length': 3.443951165371809, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 71, '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': '2031978', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1663, 'n_words': 476}