subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

mares_iconhd_parser.c (37548B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2010 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <stdlib.h>
     23 #include <stdio.h>	/* for snprintf */
     24 #include <string.h>	/* for strdup */
     25 
     26 #include <libdivecomputer/units.h>
     27 
     28 #include "mares_iconhd.h"
     29 #include "context-private.h"
     30 #include "parser-private.h"
     31 #include "platform.h"
     32 #include "array.h"
     33 #include "checksum.h"
     34 
     35 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &mares_iconhd_parser_vtable)
     36 
     37 #define OBJVERSION(major,minor) ( \
     38 		(((major) & 0xFF) << 8) | \
     39 		((minor) & 0xFF))
     40 
     41 #define UNSUPPORTED 0xFFFFFFFF
     42 
     43 #define SMART      0x000010
     44 #define SMARTAPNEA 0x010010
     45 #define ICONHD    0x14
     46 #define ICONHDNET 0x15
     47 #define GENIUS    0x1C
     48 #define QUADAIR   0x23
     49 #define SMARTAIR  0x24
     50 #define HORIZON   0x2C
     51 #define PUCKAIR2  0x2D
     52 #define SIRIUS    0x2F
     53 #define QUADCI    0x31
     54 #define QUAD2     0x32
     55 #define SIRIUS_L  0x33
     56 #define PUCK4     0x35
     57 
     58 #define ISSMART(model) ( \
     59 	(model) == SMART || \
     60 	(model) == SMARTAPNEA || \
     61 	(model) == SMARTAIR)
     62 
     63 #define ISGENIUS(model) ( \
     64 	(model) == GENIUS || \
     65 	(model) == HORIZON || \
     66 	(model) == PUCKAIR2 || \
     67 	(model) == SIRIUS || \
     68 	(model) == QUADCI || \
     69 	(model) == QUAD2 || \
     70 	(model) == SIRIUS_L || \
     71 	(model) == PUCK4)
     72 
     73 #define NGASMIXES_ICONHD 3
     74 #define NGASMIXES_GENIUS 5
     75 #define NGASMIXES        NGASMIXES_GENIUS
     76 
     77 #define NTANKS_ICONHD NGASMIXES_ICONHD
     78 #define NTANKS_GENIUS NGASMIXES_GENIUS
     79 #define NTANKS        NGASMIXES
     80 
     81 #define ICONHD_AIR       0
     82 #define ICONHD_GAUGE     1
     83 #define ICONHD_NITROX    2
     84 #define ICONHD_FREEDIVE  3
     85 
     86 #define GENIUS_AIR           0
     87 #define GENIUS_NITROX_SINGLE 1
     88 #define GENIUS_NITROX_MULTI  2
     89 #define GENIUS_TRIMIX        3
     90 #define GENIUS_GAUGE         4
     91 #define GENIUS_FREEDIVE      5
     92 #define GENIUS_SCR           6
     93 #define GENIUS_OC            7
     94 
     95 // Record types and sizes
     96 #define DSTR_TYPE 0x44535452 // Dive start record
     97 #define DSTR_SIZE 58
     98 #define TISS_TYPE 0x54495353 // Tissue record
     99 #define TISS_SIZE 138
    100 #define DPRS_TYPE 0x44505253 // Sample record
    101 #define DPRS_SIZE 34
    102 #define SDPT_TYPE 0x53445054 // SCR sample record
    103 #define SDPT_SIZE 78
    104 #define AIRS_TYPE 0x41495253 // Air integration record
    105 #define AIRS_SIZE 16
    106 #define DEND_TYPE 0x44454E44 // Dive end record
    107 #define DEND_SIZE 162
    108 
    109 #define GASMIX_OFF   0
    110 #define GASMIX_READY 1
    111 #define GASMIX_INUSE 2
    112 #define GASMIX_IGNRD 3
    113 
    114 #define WATER_FRESH   0
    115 #define WATER_SALT    1
    116 #define WATER_EN13319 2
    117 
    118 #define ALARM_NONE                 0
    119 #define ALARM_SLOW_DOWN            1
    120 #define ALARM_FAST_ASCENT          2
    121 #define ALARM_UNCONTROLLED_ASCENT  3
    122 #define ALARM_MOD_REACHED          4
    123 #define ALARM_CNS_DANGER           5
    124 #define ALARM_CNS_EXTREME          6
    125 #define ALARM_MISSED_DECO          7
    126 #define ALARM_DIVE_VIOLATION_DECO  8
    127 #define ALARM_LOW_BATTERY          9
    128 #define ALARM_VERY_LOW_BATTERY     10
    129 #define ALARM_PROBE_LOW_BATTERY    11
    130 #define ALARM_LOW_TANK_PRESSURE    12
    131 #define ALARM_TANK_RESERVE_REACHED 13
    132 #define ALARM_TANK_LOST_LINK       14
    133 #define ALARM_MAX_DIVE_DEPTH       15
    134 #define ALARM_RUN_AWAY_DECO        16
    135 #define ALARM_TANK_HALF_REACHED    17
    136 #define ALARM_NODECO_2MIN          18
    137 #define ALARM_NODECO_DECO          19
    138 #define ALARM_MULTIGAS_ATANKISLOW  20
    139 #define ALARM_DIVETIME_HALFTIME    21
    140 #define ALARM_DIVETIME_FULLTIME    22
    141 #define ALARM_GAS_SWITCHPOINT      23
    142 #define ALARM_GAS_IGNORED          24
    143 #define ALARM_GAS_CHANGED          25
    144 #define ALARM_GAS_NOTCHANGED       26
    145 #define ALARM_GAS_ADDED            27
    146 
    147 typedef struct mares_iconhd_parser_t mares_iconhd_parser_t;
    148 
    149 typedef struct mares_iconhd_layout_t {
    150 	unsigned int settings;
    151 	unsigned int datetime;
    152 	unsigned int divetime;
    153 	unsigned int maxdepth;
    154 	unsigned int atmospheric;
    155 	unsigned int atmospheric_divisor;
    156 	unsigned int temperature_min;
    157 	unsigned int temperature_max;
    158 	unsigned int gasmixes;
    159 	unsigned int tanks;
    160 } mares_iconhd_layout_t;
    161 
    162 typedef struct mares_iconhd_gasmix_t {
    163 	unsigned int oxygen;
    164 	unsigned int helium;
    165 } mares_iconhd_gasmix_t;
    166 
    167 typedef struct mares_iconhd_tank_t {
    168 	unsigned int volume;
    169 	unsigned int workpressure;
    170 	unsigned int beginpressure;
    171 	unsigned int endpressure;
    172 } mares_iconhd_tank_t;
    173 
    174 struct mares_iconhd_parser_t {
    175 	dc_parser_t base;
    176 	unsigned int model;
    177 	// Cached fields.
    178 	unsigned int cached;
    179 	unsigned int logformat;
    180 	unsigned int mode;
    181 	unsigned int nsamples;
    182 	unsigned int samplesize;
    183 	unsigned int headersize;
    184 	unsigned int settings;
    185 	unsigned int surftime;
    186 	unsigned int interval;
    187 	unsigned int samplerate;
    188 	unsigned int ntanks;
    189 	unsigned int ngasmixes;
    190 	unsigned int serial;
    191 	mares_iconhd_gasmix_t gasmix[NGASMIXES];
    192 	mares_iconhd_tank_t tank[NTANKS];
    193 	const mares_iconhd_layout_t *layout;
    194 };
    195 
    196 static const mares_iconhd_layout_t iconhd = {
    197 	0x0C, /* settings */
    198 	0x02, /* datetime */
    199 	UNSUPPORTED, /* divetime */
    200 	0x00, /* maxdepth */
    201 	0x22, 8, /* atmospheric */
    202 	0x42, /* temperature_min */
    203 	0x44, /* temperature_max */
    204 	0x10, /* gasmixes */
    205 	UNSUPPORTED, /* tanks */
    206 };
    207 
    208 static const mares_iconhd_layout_t iconhdnet = {
    209 	0x0C, /* settings */
    210 	0x02, /* datetime */
    211 	UNSUPPORTED, /* divetime */
    212 	0x00, /* maxdepth */
    213 	0x22, 8, /* atmospheric */
    214 	0x42, /* temperature_min */
    215 	0x44, /* temperature_max */
    216 	0x10, /* gasmixes */
    217 	0x58, /* tanks */
    218 };
    219 
    220 static const mares_iconhd_layout_t smartair = {
    221 	0x0C, /* settings */
    222 	0x02, /* datetime */
    223 	UNSUPPORTED, /* divetime */
    224 	0x00, /* maxdepth */
    225 	0x22, 8, /* atmospheric */
    226 	0x42, /* temperature_min */
    227 	0x44, /* temperature_max */
    228 	0x10, /* gasmixes */
    229 	0x5C, /* tanks */
    230 };
    231 
    232 static const mares_iconhd_layout_t smartapnea = {
    233 	0x1C, /* settings */
    234 	0x40, /* datetime */
    235 	0x24, /* divetime */
    236 	0x3A, /* maxdepth */
    237 	0x38, 1, /* atmospheric */
    238 	0x3E, /* temperature_min */
    239 	0x3C, /* temperature_max */
    240 	UNSUPPORTED, /* gasmixes */
    241 	UNSUPPORTED, /* tanks */
    242 };
    243 
    244 static const mares_iconhd_layout_t smart_freedive = {
    245 	0x08, /* settings */
    246 	0x20, /* datetime */
    247 	0x0C, /* divetime */
    248 	0x1A, /* maxdepth */
    249 	0x18, 1, /* atmospheric */
    250 	0x1C, /* temperature_min */
    251 	0x1E, /* temperature_max */
    252 	UNSUPPORTED, /* gasmixes */
    253 	UNSUPPORTED, /* tanks */
    254 };
    255 
    256 static const mares_iconhd_layout_t smartair_freedive = {
    257 	0x08, /* settings */
    258 	0x22, /* datetime */
    259 	0x0E, /* divetime */
    260 	0x1C, /* maxdepth */
    261 	0x1A, 1, /* atmospheric */
    262 	0x20, /* temperature_min */
    263 	0x1E, /* temperature_max */
    264 	UNSUPPORTED, /* gasmixes */
    265 	UNSUPPORTED, /* tanks */
    266 };
    267 
    268 static const mares_iconhd_layout_t genius = {
    269 	0x0C, /* settings */
    270 	0x08, /* datetime */
    271 	UNSUPPORTED, /* divetime */
    272 	0x22, /* maxdepth */
    273 	0x3E, 1, /* atmospheric */
    274 	0x28, /* temperature_min */
    275 	0x26, /* temperature_max */
    276 	0x54, /* gasmixes */
    277 	0x54, /* tanks */
    278 };
    279 
    280 static const mares_iconhd_layout_t horizon = {
    281 	0x0C, /* settings */
    282 	0x08, /* datetime */
    283 	UNSUPPORTED, /* divetime */
    284 	0x22 + 8, /* maxdepth */
    285 	0x3E + 8, 1, /* atmospheric */
    286 	0x28 + 8, /* temperature_min */
    287 	0x26 + 8, /* temperature_max */
    288 	0x54 + 8, /* gasmixes */
    289 	0x54 + 8, /* tanks */
    290 };
    291 
    292 static dc_status_t mares_iconhd_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
    293 static dc_status_t mares_iconhd_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
    294 static dc_status_t mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
    295 
    296 static const dc_parser_vtable_t mares_iconhd_parser_vtable = {
    297 	sizeof(mares_iconhd_parser_t),
    298 	DC_FAMILY_MARES_ICONHD,
    299 	NULL, /* set_clock */
    300 	NULL, /* set_atmospheric */
    301 	NULL, /* set_density */
    302 	mares_iconhd_parser_get_datetime, /* datetime */
    303 	mares_iconhd_parser_get_field, /* fields */
    304 	mares_iconhd_parser_samples_foreach, /* samples_foreach */
    305 	NULL /* destroy */
    306 };
    307 
    308 static dc_status_t
    309 mares_iconhd_cache (mares_iconhd_parser_t *parser)
    310 {
    311 	dc_parser_t *abstract = (dc_parser_t *) parser;
    312 	const unsigned char *data = parser->base.data;
    313 	unsigned int size = parser->base.size;
    314 
    315 	unsigned int header = 0x5C;
    316 	if (parser->model == ICONHDNET)
    317 		header = 0x80;
    318 	else if (parser->model == QUADAIR)
    319 		header = 0x84;
    320 	else if (parser->model == SMART || parser->model == SMARTAIR)
    321 		header = 4; // Type and number of samples only!
    322 	else if (parser->model == SMARTAPNEA)
    323 		header = 6; // Type and number of samples only!
    324 
    325 	if (size < 4) {
    326 		ERROR (abstract->context, "Buffer overflow detected!");
    327 		return DC_STATUS_DATAFORMAT;
    328 	}
    329 
    330 	unsigned int length = array_uint32_le (data);
    331 	if (length < 4 + header || length > size) {
    332 		ERROR (abstract->context, "Buffer overflow detected!");
    333 		return DC_STATUS_DATAFORMAT;
    334 	}
    335 
    336 	// Get the number of samples in the profile data.
    337 	unsigned int type = 0, nsamples = 0;
    338 	if (ISSMART (parser->model)) {
    339 		type     = array_uint16_le (data + length - header + 2);
    340 		nsamples = array_uint16_le (data + length - header + 0);
    341 	} else {
    342 		type     = array_uint16_le (data + length - header + 0);
    343 		nsamples = array_uint16_le (data + length - header + 2);
    344 	}
    345 
    346 	// Get the dive mode.
    347 	unsigned int mode = type & 0x03;
    348 
    349 	// Get the header and sample size.
    350 	unsigned int headersize = 0x5C;
    351 	unsigned int samplesize = 8;
    352 	const mares_iconhd_layout_t *layout = &iconhd;
    353 	if (parser->model == ICONHDNET) {
    354 		headersize = 0x80;
    355 		samplesize = 12;
    356 		layout = &iconhdnet;
    357 	} else if (parser->model == QUADAIR) {
    358 		headersize = 0x84;
    359 		samplesize = 12;
    360 		layout = &smartair;
    361 	} else if (parser->model == SMART) {
    362 		if (mode == ICONHD_FREEDIVE) {
    363 			headersize = 0x2E;
    364 			samplesize = 6;
    365 			layout = &smart_freedive;
    366 		} else {
    367 			headersize = 0x5C;
    368 			samplesize = 8;
    369 			layout = &iconhd;
    370 		}
    371 	} else if (parser->model == SMARTAPNEA) {
    372 		headersize = 0x50;
    373 		samplesize = 14;
    374 		layout = &smartapnea;
    375 	} else if (parser->model == SMARTAIR) {
    376 		if (mode == ICONHD_FREEDIVE) {
    377 			headersize = 0x30;
    378 			samplesize = 6;
    379 			layout = &smartair_freedive;
    380 		} else {
    381 			headersize = 0x84;
    382 			samplesize = 12;
    383 			layout = &smartair;
    384 		}
    385 	}
    386 
    387 	if (length < 4 + headersize) {
    388 		ERROR (abstract->context, "Buffer overflow detected!");
    389 		return DC_STATUS_DATAFORMAT;
    390 	}
    391 
    392 	const unsigned char *p = data + length - headersize;
    393 	if (!ISSMART(parser->model)) {
    394 		p += 4;
    395 	}
    396 
    397 	// Get the dive settings.
    398 	unsigned int settings = array_uint16_le (p + layout->settings);
    399 
    400 	// Get the sample interval.
    401 	unsigned int interval = 0;
    402 	unsigned int samplerate = 0;
    403 	if (parser->model == SMARTAPNEA) {
    404 		unsigned int idx = (settings & 0x0600) >> 9;
    405 		samplerate = 1 << idx;
    406 		interval = 1000 / samplerate;
    407 	} else {
    408 		const unsigned int intervals[] = {1, 5, 10, 20};
    409 		unsigned int idx = (settings & 0x0C00) >> 10;
    410 		interval = intervals[idx] * 1000;
    411 		samplerate = 1;
    412 	}
    413 
    414 	// Calculate the total number of bytes for this dive.
    415 	unsigned int nbytes = 4 + headersize + nsamples * samplesize;
    416 	if (layout->tanks != UNSUPPORTED) {
    417 		nbytes += (nsamples / 4) * 8;
    418 	} else if (parser->model == SMARTAPNEA) {
    419 		unsigned int divetime = array_uint32_le (p + 0x24);
    420 		nbytes += divetime * samplerate * 2;
    421 	}
    422 	if (length != nbytes) {
    423 		ERROR (abstract->context, "Calculated and stored size are not equal.");
    424 		return DC_STATUS_DATAFORMAT;
    425 	}
    426 
    427 	// Gas mixes
    428 	unsigned int ngasmixes = 0;
    429 	mares_iconhd_gasmix_t gasmix[NGASMIXES_ICONHD] = {0};
    430 	if (layout->gasmixes != UNSUPPORTED) {
    431 		if (mode == ICONHD_GAUGE || mode == ICONHD_FREEDIVE) {
    432 			ngasmixes = 0;
    433 		} else if (mode == ICONHD_AIR) {
    434 			gasmix[0].oxygen = 21;
    435 			gasmix[0].helium = 0;
    436 			ngasmixes = 1;
    437 		} else {
    438 			// Count the number of active gas mixes. The active gas
    439 			// mixes are always first, so we stop counting as soon
    440 			// as the first gas marked as disabled is found.
    441 			ngasmixes = 0;
    442 			while (ngasmixes < NGASMIXES_ICONHD) {
    443 				unsigned int offset = layout->gasmixes + ngasmixes * 4;
    444 				if (p[offset + 1] & 0x80)
    445 					break;
    446 				gasmix[ngasmixes].oxygen = p[offset];
    447 				gasmix[ngasmixes].helium = 0;
    448 				ngasmixes++;
    449 			}
    450 		}
    451 	}
    452 
    453 	// Tanks
    454 	unsigned int ntanks = 0;
    455 	mares_iconhd_tank_t tank[NTANKS_ICONHD] = {0};
    456 	if (layout->tanks != UNSUPPORTED) {
    457 		unsigned int tankoffset = layout->tanks;
    458 		while (ntanks < NTANKS_ICONHD) {
    459 			tank[ntanks].volume        = array_uint16_le (p + tankoffset + 0x0C + ntanks * 8 + 0);
    460 			tank[ntanks].workpressure  = array_uint16_le (p + tankoffset + 0x0C + ntanks * 8 + 2);
    461 			tank[ntanks].beginpressure = array_uint16_le (p + tankoffset + ntanks * 4 + 0);
    462 			tank[ntanks].endpressure   = array_uint16_le (p + tankoffset + ntanks * 4 + 2);
    463 			if (tank[ntanks].beginpressure == 0 && (tank[ntanks].endpressure == 0 || tank[ntanks].endpressure == 36000))
    464 				break;
    465 			ntanks++;
    466 		}
    467 	}
    468 
    469 	// Limit the size to the actual length.
    470 	parser->base.size = length;
    471 
    472 	// Cache the data for later use.
    473 	parser->logformat = 0;
    474 	parser->mode = mode;
    475 	parser->nsamples = nsamples;
    476 	parser->samplesize = samplesize;
    477 	parser->headersize = headersize;
    478 	parser->settings = settings;
    479 	parser->surftime = 3 * 60;
    480 	parser->interval = interval;
    481 	parser->samplerate = samplerate;
    482 	parser->ntanks = ntanks;
    483 	parser->ngasmixes = ngasmixes;
    484 	for (unsigned int i = 0; i < ngasmixes; ++i) {
    485 		parser->gasmix[i] = gasmix[i];
    486 	}
    487 	for (unsigned int i = 0; i < ntanks; ++i) {
    488 		parser->tank[i] = tank[i];
    489 	}
    490 	parser->layout = layout;
    491 	parser->cached = 1;
    492 
    493 	return DC_STATUS_SUCCESS;
    494 }
    495 
    496 static dc_status_t
    497 mares_genius_cache (mares_iconhd_parser_t *parser)
    498 {
    499 	dc_parser_t *abstract = (dc_parser_t *) parser;
    500 	const unsigned char *data = parser->base.data;
    501 	unsigned int size = parser->base.size;
    502 
    503 	if (size < 20) {
    504 		ERROR (abstract->context, "Buffer overflow detected!");
    505 		return DC_STATUS_DATAFORMAT;
    506 	}
    507 
    508 	// Check the header type and version.
    509 	unsigned int type = array_uint16_le (data);
    510 	unsigned int minor = data[2];
    511 	unsigned int major = data[3];
    512 	if (type != 1 || OBJVERSION(major,minor) > OBJVERSION(2,0)) {
    513 		ERROR (abstract->context, "Unsupported object type (%u) or version (%u.%u).",
    514 			type, major, minor);
    515 		return DC_STATUS_DATAFORMAT;
    516 	}
    517 
    518 	// Get the data format.
    519 	unsigned int logformat = data[0x10];
    520 
    521 	// The Horizon header has 8 bytes extra at offset 0x18.
    522 	unsigned int extra = 0;
    523 	const mares_iconhd_layout_t * layout = &genius;
    524 	if (logformat == 1) {
    525 		extra = 8;
    526 		layout = &horizon;
    527 	}
    528 
    529 	// The Genius header (v1.x) has 10 bytes more at the end.
    530 	unsigned int more = 0;
    531 	if (major >= 1) {
    532 		more = 16;
    533 	}
    534 
    535 	// Get the header size.
    536 	unsigned int headersize = 0xB8 + extra + more;
    537 	if (headersize > size) {
    538 		ERROR (abstract->context, "Buffer overflow detected!");
    539 		return DC_STATUS_DATAFORMAT;
    540 	}
    541 
    542 	// Get the number of samples in the profile data.
    543 	unsigned int nsamples = array_uint16_le (data + 0x20 + extra);
    544 
    545 	// Get the dive settings.
    546 	unsigned int settings = array_uint32_le (data + layout->settings);
    547 
    548 	// Get the dive mode.
    549 	unsigned int mode = settings & 0xF;
    550 
    551 	// Get the surface timeout setting (in minutes).
    552 	// For older firmware versions the value is hardcoded to 3 minutes, but
    553 	// starting with the newer v01.02.00 firmware the value is configurable and
    554 	// stored in the settings. To detect whether the setting is available, we
    555 	// need to check the profile version instead of the header version.
    556 	unsigned int surftime = 3;
    557 	if (headersize + 4 <= size) {
    558 		// Get the profile type and version.
    559 		unsigned int profile_type = array_uint16_le (data + headersize);
    560 		unsigned int profile_minor = data[headersize + 2];
    561 		unsigned int profile_major = data[headersize + 3];
    562 
    563 		if (profile_type == 0 &&
    564 			OBJVERSION(profile_major,profile_minor) >= OBJVERSION(1,0)) {
    565 			surftime = (settings >> 13) & 0x3F;
    566 		}
    567 	}
    568 
    569 	// Gas mixes and tanks.
    570 	unsigned int ntanks = 0;
    571 	unsigned int ngasmixes = 0;
    572 	mares_iconhd_gasmix_t gasmix[NGASMIXES_GENIUS] = {0};
    573 	mares_iconhd_tank_t tank[NTANKS_GENIUS] = {0};
    574 	for (unsigned int i = 0; i < NGASMIXES_GENIUS; i++) {
    575 		unsigned int offset = layout->tanks + i * 20;
    576 		unsigned int gasmixparams  = array_uint32_le(data + offset + 0);
    577 		unsigned int beginpressure = array_uint16_le(data + offset + 4);
    578 		unsigned int endpressure   = array_uint16_le(data + offset + 6);
    579 		unsigned int volume        = array_uint16_le(data + offset + 8);
    580 		unsigned int workpressure  = array_uint16_le(data + offset + 10);
    581 
    582 		unsigned int o2      = (gasmixparams      ) & 0x7F;
    583 		unsigned int n2      = (gasmixparams >>  7) & 0x7F;
    584 		unsigned int he      = (gasmixparams >> 14) & 0x7F;
    585 		unsigned int state   = (gasmixparams >> 21) & 0x03;
    586 		unsigned int DC_ATTR_UNUSED changed = (gasmixparams >> 23) & 0x01;
    587 
    588 		if (o2 + n2 + he != 100) {
    589 			WARNING (abstract->context, "Invalid gas mix (%u%% He, %u%% O2, %u%% N2).", he, o2, n2);
    590 		}
    591 
    592 		// The active gas mixes are always first, so we stop processing
    593 		// as soon as the first gas mix marked as disabled is found.
    594 		if (state != GASMIX_OFF && ngasmixes == i) {
    595 			gasmix[i].oxygen = o2;
    596 			gasmix[i].helium = he;
    597 			ngasmixes++;
    598 		}
    599 
    600 		// Assume the active transmitters are always first, so we can
    601 		// stop processing as soon as the first inactive transmitter is
    602 		// found.
    603 		if ((beginpressure != 0 || (endpressure != 0 && endpressure != 36000)) &&
    604 			(ntanks == i)) {
    605 			tank[i].volume = volume;
    606 			tank[i].workpressure = workpressure;
    607 			tank[i].beginpressure = beginpressure;
    608 			tank[i].endpressure = endpressure;
    609 			ntanks++;
    610 		}
    611 	}
    612 
    613 	// Cache the data for later use.
    614 	parser->logformat = logformat;
    615 	parser->mode = mode;
    616 	parser->nsamples = nsamples;
    617 	parser->samplesize = 0;
    618 	parser->headersize = headersize;
    619 	parser->settings = settings;
    620 	parser->surftime = surftime * 60;
    621 	parser->interval = 5000;
    622 	parser->samplerate = 1;
    623 	parser->ntanks = ntanks;
    624 	parser->ngasmixes = ngasmixes;
    625 	for (unsigned int i = 0; i < ngasmixes; ++i) {
    626 		parser->gasmix[i] = gasmix[i];
    627 	}
    628 	for (unsigned int i = 0; i < ntanks; ++i) {
    629 		parser->tank[i] = tank[i];
    630 	}
    631 	parser->layout = layout;
    632 	parser->cached = 1;
    633 
    634 	return DC_STATUS_SUCCESS;
    635 }
    636 
    637 static dc_status_t
    638 mares_iconhd_parser_cache (mares_iconhd_parser_t *parser)
    639 {
    640 	if (parser->cached) {
    641 		return DC_STATUS_SUCCESS;
    642 	}
    643 
    644 	if (ISGENIUS(parser->model)) {
    645 		return mares_genius_cache (parser);
    646 	} else {
    647 		return mares_iconhd_cache (parser);
    648 	}
    649 }
    650 
    651 dc_status_t
    652 mares_iconhd_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model, unsigned int serial)
    653 {
    654 	mares_iconhd_parser_t *parser = NULL;
    655 
    656 	if (out == NULL)
    657 		return DC_STATUS_INVALIDARGS;
    658 
    659 	// Allocate memory.
    660 	parser = (mares_iconhd_parser_t *) dc_parser_allocate (context, &mares_iconhd_parser_vtable, data, size);
    661 	if (parser == NULL) {
    662 		ERROR (context, "Failed to allocate memory.");
    663 		return DC_STATUS_NOMEMORY;
    664 	}
    665 
    666 	// Set the default values.
    667 	parser->model = model;
    668 	parser->cached = 0;
    669 	parser->logformat = 0;
    670 	parser->mode = ISGENIUS(model) ? GENIUS_AIR : ICONHD_AIR;
    671 	parser->nsamples = 0;
    672 	parser->samplesize = 0;
    673 	parser->headersize = 0;
    674 	parser->settings = 0;
    675 	parser->surftime = 0;
    676 	parser->interval = 0;
    677 	parser->samplerate = 0;
    678 	parser->ntanks = 0;
    679 	parser->ngasmixes = 0;
    680 	parser->serial = serial;
    681 	for (unsigned int i = 0; i < NGASMIXES; ++i) {
    682 		parser->gasmix[i].oxygen = 0;
    683 		parser->gasmix[i].helium = 0;
    684 	}
    685 	for (unsigned int i = 0; i < NTANKS; ++i) {
    686 		parser->tank[i].volume = 0;
    687 		parser->tank[i].workpressure = 0;
    688 		parser->tank[i].beginpressure = 0;
    689 		parser->tank[i].endpressure = 0;
    690 	}
    691 	parser->layout = NULL;
    692 
    693 	*out = (dc_parser_t*) parser;
    694 
    695 	return DC_STATUS_SUCCESS;
    696 }
    697 
    698 static dc_status_t
    699 mares_iconhd_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    700 {
    701 	mares_iconhd_parser_t *parser = (mares_iconhd_parser_t *) abstract;
    702 
    703 	// Cache the parser data.
    704 	dc_status_t rc = mares_iconhd_parser_cache (parser);
    705 	if (rc != DC_STATUS_SUCCESS)
    706 		return rc;
    707 
    708 	// Pointer to the header data.
    709 	const unsigned char *p = abstract->data;
    710 	if (!ISGENIUS(parser->model)) {
    711 		p += abstract->size - parser->headersize;
    712 		if (!ISSMART(parser->model)) {
    713 			p += 4;
    714 		}
    715 	}
    716 
    717 	// Offset to the date/time field.
    718 	p += parser->layout->datetime;
    719 
    720 	if (datetime) {
    721 		if (ISGENIUS(parser->model)) {
    722 			unsigned int timestamp = array_uint32_le (p);
    723 			datetime->hour   = (timestamp     ) & 0x1F;
    724 			datetime->minute = (timestamp >> 5) & 0x3F;
    725 			datetime->second = 0;
    726 			datetime->day    = (timestamp >> 11) & 0x1F;
    727 			datetime->month  = (timestamp >> 16) & 0x0F;
    728 			datetime->year   = (timestamp >> 20) & 0x0FFF;
    729 		} else {
    730 			datetime->hour   = array_uint16_le (p + 0);
    731 			datetime->minute = array_uint16_le (p + 2);
    732 			datetime->second = 0;
    733 			datetime->day    = array_uint16_le (p + 4);
    734 			datetime->month  = array_uint16_le (p + 6) + 1;
    735 			datetime->year   = array_uint16_le (p + 8) + 1900;
    736 		}
    737 		datetime->timezone = DC_TIMEZONE_NONE;
    738 	}
    739 
    740 	return DC_STATUS_SUCCESS;
    741 }
    742 
    743 #define BUFLEN 16
    744 
    745 static dc_status_t
    746 mares_iconhd_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    747 {
    748 	mares_iconhd_parser_t *parser = (mares_iconhd_parser_t *) abstract;
    749 
    750 	// Cache the parser data.
    751 	dc_status_t rc = mares_iconhd_parser_cache (parser);
    752 	if (rc != DC_STATUS_SUCCESS)
    753 		return rc;
    754 
    755 	// Pointer to the header data.
    756 	const unsigned char *p = abstract->data;
    757 	if (!ISGENIUS(parser->model)) {
    758 		p += abstract->size - parser->headersize;
    759 		if (!ISSMART(parser->model)) {
    760 			p += 4;
    761 		}
    762 	}
    763 
    764 	// The Horizon header has 8 bytes extra at offset 0x18.
    765 	unsigned int extra = 0;
    766 	if (parser->logformat == 1) {
    767 		extra = 8;
    768 	}
    769 
    770 	unsigned int metric = ISGENIUS(parser->model) ?
    771 		p[0x34 + extra] : parser->settings & 0x0100;
    772 
    773 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    774 	dc_tank_t *tank = (dc_tank_t *) value;
    775 	dc_salinity_t *water = (dc_salinity_t *) value;
    776 	dc_field_string_t *string = (dc_field_string_t *) value;
    777 	char buf[BUFLEN];
    778 
    779 	if (value) {
    780 		switch (type) {
    781 		case DC_FIELD_DIVETIME:
    782 			if (parser->layout->divetime != UNSUPPORTED) {
    783 				*((unsigned int *) value) = array_uint16_le (p + parser->layout->divetime);
    784 			} else {
    785 				*((unsigned int *) value) = parser->nsamples * parser->interval / 1000 - parser->surftime;
    786 			}
    787 			break;
    788 		case DC_FIELD_MAXDEPTH:
    789 			*((double *) value) = array_uint16_le (p + parser->layout->maxdepth) / 10.0;
    790 			break;
    791 		case DC_FIELD_GASMIX_COUNT:
    792 			*((unsigned int *) value) = parser->ngasmixes;
    793 			break;
    794 		case DC_FIELD_GASMIX:
    795 			gasmix->usage = DC_USAGE_NONE;
    796 			gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0;
    797 			gasmix->helium = parser->gasmix[flags].helium / 100.0;
    798 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    799 			break;
    800 		case DC_FIELD_TANK_COUNT:
    801 			*((unsigned int *) value) = parser->ntanks;
    802 			break;
    803 		case DC_FIELD_TANK:
    804 			if (metric) {
    805 				tank->type = DC_TANKVOLUME_METRIC;
    806 				tank->volume = parser->tank[flags].volume;
    807 				tank->workpressure = parser->tank[flags].workpressure;
    808 			} else {
    809 				if (parser->tank[flags].workpressure == 0)
    810 					return DC_STATUS_DATAFORMAT;
    811 				tank->type = DC_TANKVOLUME_IMPERIAL;
    812 				tank->volume = parser->tank[flags].volume * CUFT * 1000.0;
    813 				tank->volume /= parser->tank[flags].workpressure * PSI / ATM;
    814 				tank->workpressure = parser->tank[flags].workpressure * PSI / BAR;
    815 			}
    816 			tank->beginpressure = parser->tank[flags].beginpressure / 100.0;
    817 			tank->endpressure   = parser->tank[flags].endpressure / 100.0;
    818 			if (flags < parser->ngasmixes) {
    819 				tank->gasmix = flags;
    820 			} else {
    821 				tank->gasmix = DC_GASMIX_UNKNOWN;
    822 			}
    823 			tank->usage = DC_TANK_USAGE_NONE;
    824 			break;
    825 		case DC_FIELD_ATMOSPHERIC:
    826 			*((double *) value) = array_uint16_le (p + parser->layout->atmospheric) / (1000.0 * parser->layout->atmospheric_divisor);
    827 			break;
    828 		case DC_FIELD_SALINITY:
    829 			if (ISGENIUS(parser->model)) {
    830 				unsigned int salinity = (parser->settings >> 5) & 0x03;
    831 				switch (salinity) {
    832 				case WATER_FRESH:
    833 					water->type = DC_WATER_FRESH;
    834 					water->density = 0.0;
    835 					break;
    836 				case WATER_SALT:
    837 					water->type = DC_WATER_SALT;
    838 					water->density = 0.0;
    839 					break;
    840 				case WATER_EN13319:
    841 					water->type = DC_WATER_SALT;
    842 					water->density = MSW / GRAVITY;
    843 					break;
    844 				default:
    845 					return DC_STATUS_DATAFORMAT;
    846 				}
    847 			} else if (parser->model == SMARTAPNEA) {
    848 				unsigned int salinity = parser->settings & 0x003F;
    849 				if (salinity == 0) {
    850 					water->type = DC_WATER_FRESH;
    851 				} else {
    852 					water->type = DC_WATER_SALT;
    853 				}
    854 				water->density = 1000.0 + salinity;
    855 			} else {
    856 				if (parser->settings & 0x0010) {
    857 					water->type = DC_WATER_FRESH;
    858 				} else {
    859 					water->type = DC_WATER_SALT;
    860 				}
    861 				water->density = 0.0;
    862 			}
    863 			break;
    864 		case DC_FIELD_TEMPERATURE_MINIMUM:
    865 			*((double *) value) = (signed short) array_uint16_le (p + parser->layout->temperature_min) / 10.0;
    866 			break;
    867 		case DC_FIELD_TEMPERATURE_MAXIMUM:
    868 			*((double *) value) = (signed short) array_uint16_le (p + parser->layout->temperature_max) / 10.0;
    869 			break;
    870 		case DC_FIELD_DIVEMODE:
    871 			if (ISGENIUS(parser->model)) {
    872 				switch (parser->mode) {
    873 				case GENIUS_AIR:
    874 				case GENIUS_NITROX_SINGLE:
    875 				case GENIUS_NITROX_MULTI:
    876 				case GENIUS_TRIMIX:
    877 				case GENIUS_OC:
    878 					*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    879 					break;
    880 				case GENIUS_GAUGE:
    881 					*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    882 					break;
    883 				case GENIUS_FREEDIVE:
    884 					*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
    885 					break;
    886 				case GENIUS_SCR:
    887 					*((dc_divemode_t *) value) = DC_DIVEMODE_SCR;
    888 					break;
    889 				default:
    890 					return DC_STATUS_DATAFORMAT;
    891 				}
    892 			} else {
    893 				switch (parser->mode) {
    894 				case ICONHD_AIR:
    895 				case ICONHD_NITROX:
    896 					*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    897 					break;
    898 				case ICONHD_GAUGE:
    899 					*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    900 					break;
    901 				case ICONHD_FREEDIVE:
    902 					*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
    903 					break;
    904 				default:
    905 					return DC_STATUS_DATAFORMAT;
    906 				}
    907 			}
    908 			break;
    909 		case DC_FIELD_STRING:
    910 			switch(flags) {
    911 			case 0: /* serial */
    912 				string->desc = STRING_KEY_SERIAL_NUMBER;
    913 				snprintf(buf, BUFLEN, "%u", parser->serial);
    914 				break;
    915 			default:
    916 				return DC_STATUS_UNSUPPORTED;
    917 			}
    918 			string->value = strdup(buf);
    919 			break;
    920 		default:
    921 			return DC_STATUS_UNSUPPORTED;
    922 		}
    923 	}
    924 
    925 	return DC_STATUS_SUCCESS;
    926 }
    927 
    928 
    929 static dc_status_t
    930 mares_iconhd_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    931 {
    932 	mares_iconhd_parser_t *parser = (mares_iconhd_parser_t *) abstract;
    933 	const unsigned char *data = abstract->data;
    934 
    935 	// Previous gas mix - initialize with impossible value
    936 	unsigned int gasmix_previous = 0xFFFFFFFF;
    937 
    938 	unsigned int offset = 4;
    939 	unsigned int marker = 0;
    940 	unsigned int time = 0;
    941 	unsigned int nsamples = 0;
    942 	while (nsamples < parser->nsamples) {
    943 		dc_sample_value_t sample = {0};
    944 
    945 		if (parser->model == SMARTAPNEA) {
    946 			unsigned int DC_ATTR_UNUSED maxdepth = array_uint16_le (data + offset + 0);
    947 			unsigned int divetime = array_uint16_le (data + offset + 2);
    948 			unsigned int surftime = array_uint16_le (data + offset + 4);
    949 
    950 			// Surface Time (seconds).
    951 			time += surftime * 1000;
    952 			sample.time = time;
    953 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    954 
    955 			// Surface Depth (0 m).
    956 			sample.depth = 0.0;
    957 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    958 
    959 			offset += parser->samplesize;
    960 			nsamples++;
    961 
    962 			unsigned int count = divetime * parser->samplerate;
    963 			for (unsigned int i = 0; i < count; ++i) {
    964 				// Time (seconds).
    965 				time += parser->interval;
    966 				sample.time = time;
    967 				if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    968 
    969 				// Depth (1/10 m).
    970 				unsigned int depth = array_uint16_le (data + offset);
    971 				sample.depth = depth / 10.0;
    972 				if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    973 
    974 				offset += 2;
    975 			}
    976 		} else if (parser->mode == ICONHD_FREEDIVE) {
    977 			unsigned int maxdepth = array_uint16_le (data + offset + 0);
    978 			unsigned int divetime = array_uint16_le (data + offset + 2);
    979 			unsigned int surftime = array_uint16_le (data + offset + 4);
    980 
    981 			// Surface Time (seconds).
    982 			time += surftime * 1000;
    983 			sample.time = time;
    984 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    985 
    986 			// Surface Depth (0 m).
    987 			sample.depth = 0.0;
    988 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    989 
    990 			// Dive Time (seconds).
    991 			time += divetime * 1000;
    992 			sample.time = time;
    993 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    994 
    995 			// Maximum Depth (1/10 m).
    996 			sample.depth = maxdepth / 10.0;
    997 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    998 
    999 			offset += parser->samplesize;
   1000 			nsamples++;
   1001 		} else {
   1002 			unsigned int depth = array_uint16_le (data + offset + 0);
   1003 			unsigned int temperature = array_uint16_le (data + offset + 2) & 0x0FFF;
   1004 			unsigned int gasmix = (data[offset + 3] & 0xF0) >> 4;
   1005 
   1006 			// Time (seconds).
   1007 			time += parser->interval;
   1008 			sample.time = time;
   1009 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
   1010 
   1011 			// Depth (1/10 m).
   1012 			sample.depth = depth / 10.0;
   1013 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
   1014 
   1015 			// Temperature (1/10 °C).
   1016 			sample.temperature = temperature / 10.0;
   1017 			if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
   1018 
   1019 			// Current gas mix
   1020 			if (parser->ngasmixes > 0) {
   1021 				if (gasmix >= parser->ngasmixes) {
   1022 					ERROR (abstract->context, "Invalid gas mix index.");
   1023 					return DC_STATUS_DATAFORMAT;
   1024 				}
   1025 				if (gasmix != gasmix_previous) {
   1026 					sample.gasmix = gasmix;
   1027 					if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
   1028 					gasmix_previous = gasmix;
   1029 				}
   1030 			}
   1031 
   1032 			offset += parser->samplesize;
   1033 			nsamples++;
   1034 
   1035 			// Some extra data.
   1036 			if (parser->layout->tanks != UNSUPPORTED && (nsamples % 4) == 0) {
   1037 				// Pressure (1/100 bar).
   1038 				unsigned int pressure = array_uint16_le(data + offset + marker + 0);
   1039 				if (gasmix < parser->ntanks) {
   1040 					sample.pressure.tank = gasmix;
   1041 					sample.pressure.value = pressure / 100.0;
   1042 					if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata);
   1043 				} else if (pressure != 0) {
   1044 					WARNING (abstract->context, "Invalid tank with non-zero pressure.");
   1045 				}
   1046 
   1047 				offset += 8;
   1048 			}
   1049 		}
   1050 	}
   1051 
   1052 	return DC_STATUS_SUCCESS;
   1053 }
   1054 
   1055 static dc_status_t
   1056 mares_genius_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
   1057 {
   1058 	mares_iconhd_parser_t *parser = (mares_iconhd_parser_t *) abstract;
   1059 	const unsigned char *data = abstract->data;
   1060 	const unsigned int size = abstract->size;
   1061 
   1062 	// Previous gas mix - initialize with impossible value
   1063 	unsigned int gasmix_previous = 0xFFFFFFFF;
   1064 	unsigned int tank = 0xFFFFFFFF;
   1065 
   1066 	// Skip the dive header.
   1067 	unsigned int offset = parser->headersize;
   1068 
   1069 	if (offset + 4 > size) {
   1070 		ERROR (abstract->context, "Buffer overflow detected!");
   1071 		return DC_STATUS_DATAFORMAT;
   1072 	}
   1073 
   1074 	// Check the profile type and version.
   1075 	unsigned int profile_type = array_uint16_le (data + offset);
   1076 	unsigned int profile_minor = data[offset + 2];
   1077 	unsigned int profile_major = data[offset + 3];
   1078 	if (profile_type > 1 ||
   1079 		(profile_type == 0 && OBJVERSION(profile_major,profile_minor) > OBJVERSION(2,0)) ||
   1080 		(profile_type == 1 && OBJVERSION(profile_major,profile_minor) > OBJVERSION(0,2))) {
   1081 		ERROR (abstract->context, "Unsupported object type (%u) or version (%u.%u).",
   1082 			profile_type, profile_major, profile_minor);
   1083 		return DC_STATUS_DATAFORMAT;
   1084 	}
   1085 	offset += 4;
   1086 
   1087 	unsigned int time = 0;
   1088 	unsigned int marker = 4;
   1089 	while (offset < size) {
   1090 		dc_sample_value_t sample = {0};
   1091 
   1092 		if (offset + 10 > size) {
   1093 			ERROR (abstract->context, "Buffer overflow detected!");
   1094 			return DC_STATUS_DATAFORMAT;
   1095 		}
   1096 
   1097 		// Get the record type and length.
   1098 		unsigned int type = array_uint32_be(data + offset);
   1099 		unsigned int length = 0;
   1100 		switch (type) {
   1101 		case DSTR_TYPE:
   1102 			length = DSTR_SIZE;
   1103 			break;
   1104 		case TISS_TYPE:
   1105 			length = TISS_SIZE;
   1106 			break;
   1107 		case DPRS_TYPE:
   1108 			length = DPRS_SIZE;
   1109 			break;
   1110 		case SDPT_TYPE:
   1111 			length = SDPT_SIZE;
   1112 			break;
   1113 		case AIRS_TYPE:
   1114 			length = AIRS_SIZE;
   1115 			break;
   1116 		case DEND_TYPE:
   1117 			length = DEND_SIZE;
   1118 			break;
   1119 		default:
   1120 			ERROR (abstract->context, "Unknown record type (%08x).", type);
   1121 			return DC_STATUS_DATAFORMAT;
   1122 		}
   1123 
   1124 		if (offset + length > size) {
   1125 			ERROR (abstract->context, "Buffer overflow detected!");
   1126 			return DC_STATUS_DATAFORMAT;
   1127 		}
   1128 
   1129 		unsigned int etype = array_uint32_be(data + offset + length - 4);
   1130 		if (etype != type) {
   1131 			ERROR (abstract->context, "Invalid record end type (%08x).", etype);
   1132 			return DC_STATUS_DATAFORMAT;
   1133 		}
   1134 
   1135 		unsigned short crc = array_uint16_le(data + offset + length - 6);
   1136 		unsigned short ccrc = checksum_crc16_ccitt(data + offset + 4, length - 10, 0x0000, 0x0000);
   1137 		if (crc != ccrc) {
   1138 			ERROR (abstract->context, "Invalid record checksum (%04x %04x).", crc, ccrc);
   1139 			return DC_STATUS_DATAFORMAT;
   1140 		}
   1141 
   1142 		if (type == DPRS_TYPE || type == SDPT_TYPE) {
   1143 			unsigned int depth = 0, temperature = 0;
   1144 			unsigned int gasmix = 0, alarms = 0;
   1145 			unsigned int decostop = 0, decodepth = 0, decotime = 0, tts = 0;
   1146 			unsigned int bookmark = 0;
   1147 			if (type == SDPT_TYPE) {
   1148 				unsigned int misc = 0, deco = 0;
   1149 				depth       = array_uint16_le (data + offset + marker + 2);
   1150 				temperature = array_uint16_le (data + offset + marker + 6);
   1151 				alarms      = array_uint32_le (data + offset + marker + 0x14);
   1152 				misc        = array_uint32_le (data + offset + marker + 0x18);
   1153 				deco        = array_uint32_le (data + offset + marker + 0x1C);
   1154 				bookmark    = (misc >>  2) & 0x0F;
   1155 				gasmix      = (misc >>  6) & 0x0F;
   1156 				decostop    = (misc >> 10) & 0x01;
   1157 				if (decostop) {
   1158 					decodepth = (deco >>  3) & 0x7F;
   1159 					decotime  = (deco >> 10) & 0xFF;
   1160 					tts       = (deco >> 18) & 0x3FFF;
   1161 				} else {
   1162 					decotime  = deco & 0xFF;
   1163 				}
   1164 			} else {
   1165 				unsigned int misc = 0;
   1166 				depth       = array_uint16_le (data + offset + marker + 0);
   1167 				temperature = array_uint16_le (data + offset + marker + 4);
   1168 				decotime    = array_uint16_le (data + offset + marker + 0x0A);
   1169 				alarms      = array_uint32_le (data + offset + marker + 0x0C);
   1170 				misc        = array_uint32_le (data + offset + marker + 0x14);
   1171 				bookmark    = (misc >>  2) & 0x0F;
   1172 				gasmix      = (misc >>  6) & 0x0F;
   1173 				decostop    = (misc >> 18) & 0x01;
   1174 				decodepth   = (misc >> 19) & 0x7F;
   1175 			}
   1176 
   1177 			// Time (seconds).
   1178 			time += parser->interval;
   1179 			sample.time = time;
   1180 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
   1181 
   1182 			// Depth (1/10 m).
   1183 			sample.depth = depth / 10.0;
   1184 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
   1185 
   1186 			// Temperature (1/10 °C).
   1187 			sample.temperature = temperature / 10.0;
   1188 			if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
   1189 
   1190 			// Current gas mix
   1191 			if (parser->ngasmixes > 0) {
   1192 				if (gasmix >= parser->ngasmixes) {
   1193 					ERROR (abstract->context, "Invalid gas mix index.");
   1194 					return DC_STATUS_DATAFORMAT;
   1195 				}
   1196 				if (gasmix != gasmix_previous) {
   1197 					sample.gasmix = gasmix;
   1198 					if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
   1199 					gasmix_previous = gasmix;
   1200 				}
   1201 			}
   1202 
   1203 			// Current tank
   1204 			tank = gasmix;
   1205 
   1206 			// Bookmark
   1207 			if (bookmark) {
   1208 				sample.event.type = SAMPLE_EVENT_BOOKMARK;
   1209 				sample.event.time = 0;
   1210 				sample.event.flags = 0;
   1211 				sample.event.value = bookmark;
   1212 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
   1213 			}
   1214 
   1215 			// Deco stop / NDL.
   1216 			if (decostop) {
   1217 				sample.deco.type = DC_DECO_DECOSTOP;
   1218 				sample.deco.depth = decodepth;
   1219 			} else {
   1220 				sample.deco.type = DC_DECO_NDL;
   1221 				sample.deco.depth = 0.0;
   1222 			}
   1223 			sample.deco.time = decotime * 60;
   1224 			sample.deco.tts = tts;
   1225 			if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
   1226 
   1227 			// Alarms
   1228 			for (unsigned int v = alarms, i = 0; v; v >>= 1, ++i) {
   1229 				if ((v & 1) == 0) {
   1230 					continue;
   1231 				}
   1232 
   1233 				switch (i) {
   1234 				case ALARM_FAST_ASCENT:
   1235 				case ALARM_UNCONTROLLED_ASCENT:
   1236 					sample.event.type = SAMPLE_EVENT_ASCENT;
   1237 					break;
   1238 				case ALARM_MISSED_DECO:
   1239 				case ALARM_DIVE_VIOLATION_DECO:
   1240 					sample.event.type = SAMPLE_EVENT_CEILING;
   1241 					break;
   1242 				default:
   1243 					sample.event.type = SAMPLE_EVENT_NONE;
   1244 					break;
   1245 				}
   1246 
   1247 				if (sample.event.type != SAMPLE_EVENT_NONE) {
   1248 					sample.event.time = 0;
   1249 					sample.event.flags = 0;
   1250 					sample.event.value = 0;
   1251 					if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
   1252 				}
   1253 			}
   1254 		} else if (type == AIRS_TYPE) {
   1255 			// Pressure (1/100 bar).
   1256 			unsigned int pressure = array_uint16_le(data + offset + marker + 0);
   1257 			if (tank < parser->ntanks) {
   1258 				sample.pressure.tank = tank;
   1259 				sample.pressure.value = pressure / 100.0;
   1260 				if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata);
   1261 			} else if (pressure != 0) {
   1262 				WARNING (abstract->context, "Invalid tank with non-zero pressure.");
   1263 			}
   1264 		}
   1265 
   1266 		offset += length;
   1267 	}
   1268 
   1269 	return DC_STATUS_SUCCESS;
   1270 }
   1271 
   1272 static dc_status_t
   1273 mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
   1274 {
   1275 	mares_iconhd_parser_t *parser = (mares_iconhd_parser_t *) abstract;
   1276 
   1277 	// Cache the parser data.
   1278 	dc_status_t rc = mares_iconhd_parser_cache (parser);
   1279 	if (rc != DC_STATUS_SUCCESS)
   1280 		return rc;
   1281 
   1282 	if (ISGENIUS(parser->model)) {
   1283 		return mares_genius_foreach (abstract, callback, userdata);
   1284 	} else {
   1285 		return mares_iconhd_foreach (abstract, callback, userdata);
   1286 	}
   1287 }