<commit_before>#include "spdy_stream.h"
#include "spdy_syn_stream.h"
#include "spdy_syn_reply.h"
#include "spdy_rst_stream.h"
#include "spdy_log.h"
#include "spdy_error.h"

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

/**
 * Initialize a spdy_stream to null values.
 * @param stream - Stream to initialize.
 * @param store_received_data - Whether to store received data.
 * @param store_frames - Store frames of the stream.
 * @return Errorcode
 */
int spdy_stream_init(
		spdy_stream *stream,
		_Bool store_received_data,
		_Bool store_frames) {
	stream->store_received_data = store_received_data;
	stream->store_frames = store_frames;
	stream->stream_id = 0;
	stream->associated_to = 0;
	stream->unidirectional = 0;
	stream->data_received_length = 0;
	stream->data_sent_length = 0;
	stream->data_received = NULL;
	stream->data_sent = NULL;
	stream->fin_received = 0;
	stream->fin_sent = 0;
	stream->rst_received = 0;
	stream->rst_sent = 0;
	stream->frames_count = 0;
	stream->frames = NULL;

	return 0;
}

int spdy_stream_handle_frame(spdy_stream *stream, spdy_frame *frame) {
	if(stream->fin_received) {
		SPDYDEBUG("Already received FIN.");
		return SPDY_ERROR_STREAM_FIN;
	} else if(stream->rst_received) {
		SPDYDEBUG("Already received RST.");
		return SPDY_ERROR_STREAM_RST;
	}
	if(stream->store_frames) {
		// Reallocate space for frames pointer.
		spdy_frame **frames_new = realloc(
				stream->frames,
				sizeof(spdy_stream*)*(stream->frames_count+1));

		// If allocation failed, we return but keep the frames pointer untouched.
		if(!frames_new) {
			SPDYDEBUG("Reallocating space for frames pointer failed.");
			return SPDY_ERROR_MALLOC_FAILED;
		}
		stream->frames = frames_new;
		stream->frames[stream->frames_count] = frame;
	}

	// Keep the number of frames used in the stream.
	stream->frames_count++;

	switch(frame->type) {
		case SPDY_DATA_FRAME:
			return spdy_stream_handle_data_frame(stream, frame->frame);
			break;
		case SPDY_CONTROL_FRAME:
			break;
		default:
			// Should _never_ happen.
			return SPDY_ERROR_INVALID_DATA;
	}

	return 0;
}

int spdy_stream_handle_data_frame(
		spdy_stream *stream,
		spdy_data_frame *frame) {

	// Store received data?
	if(stream->store_received_data) {
		// Reallocate buffer for received data
		char *data_received_new = realloc(
				stream->data_received,
				sizeof(char)*(stream->data_received_length+frame->length));
		if(!data_received_new) {
			SPDYDEBUG("Reallocating data_received failed.");
			return SPDY_ERROR_MALLOC_FAILED;
		}
		stream->data_received = data_received_new;
		// Copy frame data to end of data_received
		memcpy(
				stream->data_received+stream->data_received_length,
				frame->data,
				frame->length);
		stream->data_received_length += frame->length;
	}

	// Check if FIN was received.
	stream->fin_received = (frame->flags & SPDY_DATA_FLAG_FIN);

	return 0;
}

int spdy_stream_handle_control_frame(
		spdy_stream *stream,
		spdy_control_frame *frame) {
	int ret;
	(void)stream;
	// Handle control frame types
	switch(frame->type) {
		case SPDY_CTRL_SYN_STREAM:
			frame->type_obj = malloc(sizeof(spdy_syn_stream));
			if(!frame->type_obj) {
				SPDYDEBUG("Failed to allocate space for SYN_STREAM.");
				return SPDY_ERROR_MALLOC_FAILED;
			}
	/*			ret = spdy_syn_stream_parse(
					frame->type_obj,
					frame->length,
					frame->data,
					frame->length);*/
			(void)ret;
			break;
		case SPDY_CTRL_SYN_REPLY:
			break;
		case SPDY_CTRL_RST_STREAM:
	/*			spdy_rst_stream rst_stream;
			spdy_rst_stream_parse(
			stream->rst_received = 1;
			stream->status_code = frame->->status_code;*/

			break;
		default:
			SPDYDEBUG("Unhandled control frame type.");
			return SPDY_ERROR_INVALID_DATA;
	}
	return 0;
}

<commit_msg>spdy_stream: Doxygen documentation.<commit_after>#include "spdy_stream.h"
#include "spdy_syn_stream.h"
#include "spdy_syn_reply.h"
#include "spdy_rst_stream.h"
#include "spdy_log.h"
#include "spdy_error.h"

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

/**
 * Initialize a spdy_stream to null values.
 * @param stream - Stream to initialize.
 * @param store_received_data - Whether to store received data.
 * @param store_frames - Store frames of the stream.
 * @return Errorcode
 */
int spdy_stream_init(
		spdy_stream *stream,
		_Bool store_received_data,
		_Bool store_frames) {
	/* TODO memset? */
	stream->store_received_data = store_received_data;
	stream->store_frames = store_frames;
	stream->stream_id = 0;
	stream->associated_to = 0;
	stream->unidirectional = 0;
	stream->data_received_length = 0;
	stream->data_sent_length = 0;
	stream->data_received = NULL;
	stream->data_sent = NULL;
	stream->fin_received = 0;
	stream->fin_sent = 0;
	stream->rst_received = 0;
	stream->rst_sent = 0;
	stream->frames_count = 0;
	stream->frames = NULL;

	return 0;
}

/**
 * Handle frame on stream.
 * @param stream - Target stream.
 * @param frame - Frame to handle.
 * @return Errorcode
 */
int spdy_stream_handle_frame(spdy_stream *stream, spdy_frame *frame) {
	if(stream->fin_received) {
		SPDYDEBUG("Already received FIN.");
		return SPDY_ERROR_STREAM_FIN;
	} else if(stream->rst_received) {
		SPDYDEBUG("Already received RST.");
		return SPDY_ERROR_STREAM_RST;
	}
	if(stream->store_frames) {
		// Reallocate space for frames pointer.
		spdy_frame **frames_new = realloc(
				stream->frames,
				sizeof(spdy_stream*)*(stream->frames_count+1));

		// If allocation failed, we return but keep the frames pointer untouched.
		if(!frames_new) {
			SPDYDEBUG("Reallocating space for frames pointer failed.");
			return SPDY_ERROR_MALLOC_FAILED;
		}
		stream->frames = frames_new;
		stream->frames[stream->frames_count] = frame;
	}

	// Keep the number of frames used in the stream.
	stream->frames_count++;

	switch(frame->type) {
		case SPDY_DATA_FRAME:
			return spdy_stream_handle_data_frame(stream, frame->frame);
			break;
		case SPDY_CONTROL_FRAME:
			break;
		default:
			// Should _never_ happen.
			return SPDY_ERROR_INVALID_DATA;
	}

	return 0;
}

/**
 * Handle data frame on stream.
 * @param stream - Target stream.
 * @param frame - Data frame to handle.
 * @return Errorcode
 */
int spdy_stream_handle_data_frame(
		spdy_stream *stream,
		spdy_data_frame *frame) {

	// Store received data?
	if(stream->store_received_data) {
		// Reallocate buffer for received data
		char *data_received_new = realloc(
				stream->data_received,
				sizeof(char)*(stream->data_received_length+frame->length));
		if(!data_received_new) {
			SPDYDEBUG("Reallocating data_received failed.");
			return SPDY_ERROR_MALLOC_FAILED;
		}
		stream->data_received = data_received_new;
		// Copy frame data to end of data_received
		memcpy(
				stream->data_received+stream->data_received_length,
				frame->data,
				frame->length);
		stream->data_received_length += frame->length;
	}

	// Check if FIN was received.
	stream->fin_received = (frame->flags & SPDY_DATA_FLAG_FIN);

	return 0;
}

int spdy_stream_handle_control_frame(
		spdy_stream *stream,
		spdy_control_frame *frame) {
	int ret;
	(void)stream;
	// Handle control frame types
	switch(frame->type) {
		case SPDY_CTRL_SYN_STREAM:
			frame->type_obj = malloc(sizeof(spdy_syn_stream));
			if(!frame->type_obj) {
				SPDYDEBUG("Failed to allocate space for SYN_STREAM.");
				return SPDY_ERROR_MALLOC_FAILED;
			}
	/*			ret = spdy_syn_stream_parse(
					frame->type_obj,
					frame->length,
					frame->data,
					frame->length);*/
			(void)ret;
			break;
		case SPDY_CTRL_SYN_REPLY:
			break;
		case SPDY_CTRL_RST_STREAM:
	/*			spdy_rst_stream rst_stream;
			spdy_rst_stream_parse(
			stream->rst_received = 1;
			stream->status_code = frame->->status_code;*/

			break;
		default:
			SPDYDEBUG("Unhandled control frame type.");
			return SPDY_ERROR_INVALID_DATA;
	}
	return 0;
}


{'fraction_non_alphanumeric': 0.11540449728612044, 'fraction_numerical': 0.004135435513052468, 'mean_word_length': 4.246779661016949, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 107, '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': '4394600', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2690, 'n_words': 580}