<commit_before>/* 
 * libdivecomputer
 * 
 * Copyright (C) 2008 <NAME>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>	// memcmp
#include <assert.h>

#include "suunto_d9.h"
#include "parser-private.h"
#include "utils.h"
#include "array.h"

#define SKIP 4

typedef struct suunto_d9_parser_t suunto_d9_parser_t;

struct suunto_d9_parser_t {
	parser_t base;
	unsigned int model;
};

static parser_status_t suunto_d9_parser_set_data (parser_t *abstract, const unsigned char *data, unsigned int size);
static parser_status_t suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback, void *userdata);
static parser_status_t suunto_d9_parser_destroy (parser_t *abstract);

static const parser_backend_t suunto_d9_parser_backend = {
	PARSER_TYPE_SUUNTO_D9,
	suunto_d9_parser_set_data, /* set_data */
	suunto_d9_parser_samples_foreach, /* samples_foreach */
	suunto_d9_parser_destroy /* destroy */
};


static int
parser_is_suunto_d9 (parser_t *abstract)
{
	if (abstract == NULL)
		return 0;

    return abstract->backend == &suunto_d9_parser_backend;
}


parser_status_t
suunto_d9_parser_create (parser_t **out, unsigned int model)
{
	if (out == NULL)
		return PARSER_STATUS_ERROR;

	// Allocate memory.
	suunto_d9_parser_t *parser = (suunto_d9_parser_t *) malloc (sizeof (suunto_d9_parser_t));
	if (parser == NULL) {
		WARNING ("Failed to allocate memory.");
		return PARSER_STATUS_MEMORY;
	}

	// Initialize the base class.
	parser_init (&parser->base, &suunto_d9_parser_backend);

	// Set the default values.
	parser->model = model;

	*out = (parser_t*) parser;

	return PARSER_STATUS_SUCCESS;
}


static parser_status_t
suunto_d9_parser_destroy (parser_t *abstract)
{
	if (! parser_is_suunto_d9 (abstract))
		return PARSER_STATUS_TYPE_MISMATCH;

	// Free memory.	
	free (abstract);

	return PARSER_STATUS_SUCCESS;
}


static parser_status_t
suunto_d9_parser_set_data (parser_t *abstract, const unsigned char *data, unsigned int size)
{
	if (! parser_is_suunto_d9 (abstract))
		return PARSER_STATUS_TYPE_MISMATCH;

	return PARSER_STATUS_SUCCESS;
}


static parser_status_t
suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback, void *userdata)
{
	suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract;

	if (! parser_is_suunto_d9 (abstract))
		return PARSER_STATUS_TYPE_MISMATCH;

	const unsigned char *data = abstract->data;
	unsigned int size = abstract->size;

	if (size < 0x4E - SKIP)
		return PARSER_STATUS_ERROR;

	unsigned int pressure_samples = 1;
	unsigned int pressure_offset = 0;

	switch (parser->model) {
	case 0x0E: // D9
	case 0x0F: // D6
	case 0x10: // Vyper 2
	case 0x11: // Cobra 2
	case 0x13: // Vyper Air
	case 0x14: // Cobra 3
		if (data[0x3E - SKIP] == 0x03 && data[0x3F - SKIP] == 0x07) {
			pressure_samples = 1;
			pressure_offset = 0;
		} else if (data[0x3E - SKIP] == 0x02 && data[0x3F - SKIP] == 0x05) {
			pressure_samples = 0;
			pressure_offset = 3;
		} else {
			return PARSER_STATUS_ERROR;
		}
		break;
	case 0x12: // D4
		pressure_samples = 0;
		pressure_offset = 2;
		break;
	default:
		return PARSER_STATUS_ERROR;
	}

	// Sample recording interval.
	unsigned int interval_sample = data[0x1C - SKIP];

	// Temperature recording interval.
	unsigned int interval_temperature = data[0x47 - SKIP - pressure_offset];

	// Offset to the first marker position.
	unsigned int marker = array_uint16_le (data + 0x4C - SKIP - pressure_offset);

	unsigned int time = 0;
	unsigned int nsamples = 0;
	unsigned int offset = 0x4E - SKIP - pressure_offset;
	while (offset + 2 <= size) {
		parser_sample_value_t sample = {0};

		// Time (seconds).
		sample.time = time;
		if (callback) callback (SAMPLE_TYPE_TIME, sample, userdata);

		// Depth (cm).
		unsigned int depth = array_uint16_le (data + offset);
		sample.depth = depth / 100.0;
		if (callback) callback (SAMPLE_TYPE_DEPTH, sample, userdata);
		offset += 2;

		// Tank pressure (1/100 bar).
		if (pressure_samples) {
			assert (offset + 2 <= size);<commit_msg>Reduce the number of hardcoded offsets.<commit_after>/* 
 * libdivecomputer
 * 
 * Copyright (C) 2008 <NAME>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>	// memcmp
#include <assert.h>

#include "suunto_d9.h"
#include "parser-private.h"
#include "utils.h"
#include "array.h"

#define SKIP 4

typedef struct suunto_d9_parser_t suunto_d9_parser_t;

struct suunto_d9_parser_t {
	parser_t base;
	unsigned int model;
};

static parser_status_t suunto_d9_parser_set_data (parser_t *abstract, const unsigned char *data, unsigned int size);
static parser_status_t suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback, void *userdata);
static parser_status_t suunto_d9_parser_destroy (parser_t *abstract);

static const parser_backend_t suunto_d9_parser_backend = {
	PARSER_TYPE_SUUNTO_D9,
	suunto_d9_parser_set_data, /* set_data */
	suunto_d9_parser_samples_foreach, /* samples_foreach */
	suunto_d9_parser_destroy /* destroy */
};


static int
parser_is_suunto_d9 (parser_t *abstract)
{
	if (abstract == NULL)
		return 0;

    return abstract->backend == &suunto_d9_parser_backend;
}


parser_status_t
suunto_d9_parser_create (parser_t **out, unsigned int model)
{
	if (out == NULL)
		return PARSER_STATUS_ERROR;

	// Allocate memory.
	suunto_d9_parser_t *parser = (suunto_d9_parser_t *) malloc (sizeof (suunto_d9_parser_t));
	if (parser == NULL) {
		WARNING ("Failed to allocate memory.");
		return PARSER_STATUS_MEMORY;
	}

	// Initialize the base class.
	parser_init (&parser->base, &suunto_d9_parser_backend);

	// Set the default values.
	parser->model = model;

	*out = (parser_t*) parser;

	return PARSER_STATUS_SUCCESS;
}


static parser_status_t
suunto_d9_parser_destroy (parser_t *abstract)
{
	if (! parser_is_suunto_d9 (abstract))
		return PARSER_STATUS_TYPE_MISMATCH;

	// Free memory.	
	free (abstract);

	return PARSER_STATUS_SUCCESS;
}


static parser_status_t
suunto_d9_parser_set_data (parser_t *abstract, const unsigned char *data, unsigned int size)
{
	if (! parser_is_suunto_d9 (abstract))
		return PARSER_STATUS_TYPE_MISMATCH;

	return PARSER_STATUS_SUCCESS;
}


static parser_status_t
suunto_d9_parser_samples_foreach (parser_t *abstract, sample_callback_t callback, void *userdata)
{
	suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract;

	if (! parser_is_suunto_d9 (abstract))
		return PARSER_STATUS_TYPE_MISMATCH;

	const unsigned char *data = abstract->data;
	unsigned int size = abstract->size;

	// Offset to the configuration data.
	unsigned int config = 0x3E - SKIP;
	if (parser->model == 0x12)
		config += 1; // D4
	assert (config + 1 <= size);

	// Number of parameters in the configuration data.
	unsigned int nparams = data[config];

	// Offset to the profile data.
	unsigned int profile = config + 2 + nparams * 3;
	assert (profile + 5 <= size);

	// Sample recording interval.
	unsigned int interval_sample = data[0x1C - SKIP];
	assert (interval_sample > 0);

	// Temperature recording interval.
	unsigned int interval_temperature = data[config + 2 + (nparams - 1) * 3 + 1];
	assert (interval_temperature > 0);

	// Offset to the first marker position.
	unsigned int marker = array_uint16_le (data + profile + 3);

	unsigned int time = 0;
	unsigned int nsamples = 0;
	unsigned int offset = profile + 5;
	while (offset + 2 <= size) {
		parser_sample_value_t sample = {0};

		// Time (seconds).
		sample.time = time;
		if (callback) callback (SAMPLE_TYPE_TIME, sample, userdata);

		// Depth (cm).
		unsigned int depth = array_uint16_le (data + offset);
		sample.depth = depth / 100.0;
		if (callback) callback (SAMPLE_TYPE_DEPTH, sample, userdata);
		offset += 2;

		// Tank pressure (1/100 bar).
		if (nparams == 3) {
			assert (offset + 2 <= size);
{'fraction_non_alphanumeric': 0.09516041326808047, 'fraction_numerical': 0.023273518216421967, 'mean_word_length': 4.387229056824839, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 25, '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': '7622846', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3087, 'n_words': 939}