4 * UEMIS SDA file importer
5 * AUTHOR: Dirk Hohndel - Copyright 2011
7 * Licensed under the MIT license.
26 * following code is based on code found in at base64.sourceforge.net/b64.c
27 * AUTHOR: Bob Trower 08/04/01
28 * COPYRIGHT: Copyright (c) Trantor Standard Systems Inc., 2001
29 * NOTE: This source code may be used as you wish, subject to
33 * Translation Table to decode (created by Bob Trower)
35 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
38 * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
40 static void decodeblock( unsigned char in[4], unsigned char out[3] ) {
41 out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
42 out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
43 out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
47 * decode a base64 encoded stream discarding padding, line breaks and noise
49 static void decode( uint8_t *inbuf, uint8_t *outbuf, int inbuf_len ) {
50 uint8_t in[4], out[3], v;
51 int i,len,indx_in=0,indx_out=0;
53 while (indx_in < inbuf_len) {
54 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
56 while ((indx_in < inbuf_len) && v == 0) {
58 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
60 v = ((v == '$') ? 0 : v - 61);
62 if (indx_in < inbuf_len) {
71 decodeblock( in, out );
72 for( i = 0; i < len - 1; i++ )
73 outbuf[indx_out++] = out[i];
77 /* end code from Bob Trower */
79 /* small helper functions */
80 /* simpleregex allocates (and reallocates) the found buffer
81 * don't forget to free it when you are done
83 static int simpleregex(char *buffer, char *regex, char **found) {
88 if (regcomp(&re, regex, 0) !=0) {
89 fprintf(stderr,"internal error, regex failed!\n");
92 status = regexec(&re,buffer,5,match,0);
94 *found = realloc(*found,match[1].rm_eo-match[1].rm_so + 1);
95 strncpy(*found,buffer+match[1].rm_so,match[1].rm_eo-match[1].rm_so);
96 (*found)[match[1].rm_eo-match[1].rm_so] = '\0';
101 /* read in line of arbitrary length (important for SDA files that can
102 * have lines that are tens of kB long
103 * don't forget to free it when you are done
105 #define MYGETL_INCR 1024
106 static char * mygetline(FILE * f) {
113 if ((buf = realloc(buf,size)) == NULL)
115 fgets(buf+len,MYGETL_INCR,f);
117 } while (!feof(f) && buf[len-1]!='\n');
121 /* text matching, used to build very poor man's XML parser */
122 int matchit(FILE *infd, char *regex, char *typeregex, char **found) {
125 while (!feof(infd)) {
126 buffer = mygetline(infd);
127 if (buffer && simpleregex(buffer,regex,found)) {
128 buffer = mygetline(infd);
129 if (buffer && simpleregex(buffer,typeregex,found)) {
138 * pressure_to_depth: In centibar. And when converting to
139 * depth, I'm just going to always use saltwater, because I
140 * think "true depth" is just stupid. From a diving standpoint,
141 * "true depth" is pretty much completely pointless, unless
142 * you're doing some kind of underwater surveying work.
144 * So I give water depths in "pressure depth", always assuming
145 * salt water. So one atmosphere per 10m.
147 static int pressure_to_depth(uint16_t value)
151 atm = (value / 100.0) / 1.01325;
152 cm = 100 * atm + 0.5;
153 return( (cm > 0) ? 10 * (long)cm : 0);
157 * convert the base64 data blog
159 int uemis_convert_base64(char *base64, uint8_t **data) {
162 len = strlen(base64);
163 datalen = (len/4 + 1)*3;
164 if (datalen < 0x123+0x25) {
165 /* less than header + 1 sample??? */
166 fprintf(stderr,"suspiciously short data block\n");
168 *data = malloc(datalen);
171 fprintf(stderr,"Out of memory\n");
174 decode(base64, *data, len);
176 if (memcmp(*data,"Dive\01\00\00",7))
177 fprintf(stderr,"Missing Dive100 header\n");
184 * parse uemis base64 data blob into struct dive
186 static void parse_divelog_binary(char *base64, struct dive **divep) {
190 struct sample *sample;
191 struct dive *dive = *divep;
193 datalen = uemis_convert_base64(base64, &data);
195 /* first byte of divelog data is at offset 0x123 */
197 while ((i < datalen) && (*(uint16_t *)(data+i))) {
198 /* it seems that a dive_time of 0 indicates the end of the valid readings */
199 /* the SDA usually records more samples after the end of the dive --
200 * we want to discard those, but not cut the dive short; sadly the dive
201 * duration in the header is a) in minutes and b) up to 3 minutes short */
202 if (*(uint16_t *)(data+i) > dive->duration.seconds + 180)
204 sample = prepare_sample(divep);
205 sample->time.seconds = *(uint16_t *)(data+i);
206 sample->depth.mm = pressure_to_depth(*(uint16_t *)(data+i+2));
207 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
208 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
209 sample->cylinderindex = *(uint8_t *)(data+i+22);
210 finish_sample(*divep, sample);
213 dive->duration.seconds = sample->time.seconds - 1;
218 parse_uemis_file(char *divelogfilename,GError **error) {
223 FILE *divelogfile = fopen(divelogfilename,"r");
227 if (! matchit(divelogfile,"val key=\"date\"","<ts>\\([^<]*\\)</ts>",&found)) {
228 /* some error handling */
231 strptime(found, "%Y-%m-%dT%H:%M:%S", &tm);
232 dive->when = utc_mktime(&tm);
233 if (! matchit(divelogfile,"<val key=\"duration\">",
234 "<float>\\([0-9.]*\\)</float>", &found)) {
235 /* some error handling */
238 dive->duration.seconds = 60.0 * atof(found);
240 if (! matchit(divelogfile,"<val key=\"depth\">",
241 "<int>\\([0-9.]*\\)</int>", &found)) {
242 /* some error handling */
245 dive->maxdepth.mm = pressure_to_depth(atoi(found));
247 if (! matchit(divelogfile,"<val key=\"file_content\">",
248 ">\\([a-zA-Z0-9+/]*\\)<", &found)) {
249 /* some error handling */
252 parse_divelog_binary(found,&dive);
260 * parse the two files extracted from the SDA
265 GtkFileFilter *filter = gtk_file_filter_new ();
266 gtk_file_filter_add_pattern (filter, "*.SDA");
267 gtk_file_filter_set_name(filter, "uemis Zurich SDA files");
268 dialog = gtk_file_chooser_dialog_new("Open File",
269 GTK_WINDOW(main_window),
270 GTK_FILE_CHOOSER_ACTION_OPEN,
271 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
272 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
274 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
275 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
277 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
280 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
282 GError *error = NULL;
283 while(filenames != NULL) {
284 filename = (char *)filenames->data;
285 parse_uemis_file(filename, &error);
294 filenames = g_slist_next(filenames);
296 g_slist_free(filenames);
299 gtk_widget_destroy(dialog);