]> git.tdb.fi Git - ext/subsurface.git/blob - uemis.c
7f431cba6b82e2599e5abb21a010a3082ca28237
[ext/subsurface.git] / uemis.c
1 /*
2  * uemis.c
3  *
4  * UEMIS SDA file importer
5  * AUTHOR:  Dirk Hohndel - Copyright 2011
6  *
7  * Licensed under the MIT license.
8  */
9
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #define __USE_XOPEN
16 #include <time.h>
17 #include <regex.h>
18
19 #include <gtk/gtk.h>
20
21 #include "dive.h"
22 #include "display.h"
23 #include "uemis.h"
24
25 /*
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
30  *                 the MIT license.
31  */
32 /*
33  * Translation Table to decode (created by Bob Trower)
34  */
35 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
36
37 /*
38  * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
39  */
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]);
44 }
45
46 /*
47  * decode a base64 encoded stream discarding padding, line breaks and noise
48  */
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;
52
53         while (indx_in < inbuf_len) {
54                 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
55                         v = 0;
56                         while ((indx_in < inbuf_len) && v == 0) {
57                                 v = inbuf[indx_in++];
58                                 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
59                                 if (v)
60                                         v = ((v == '$') ? 0 : v - 61);
61                         }
62                         if (indx_in < inbuf_len) {
63                                 len++;
64                                 if (v)
65                                         in[i] = (v - 1);
66                         }
67                         else
68                                 in[i] = 0;
69                 }
70                 if( len ) {
71                         decodeblock( in, out );
72                         for( i = 0; i < len - 1; i++ )
73                                 outbuf[indx_out++] = out[i];
74                 }
75         }
76 }
77 /* end code from Bob Trower */
78
79 /* small helper functions */
80 /* simpleregex allocates (and reallocates) the found buffer
81  * don't forget to free it when you are done
82  */
83 static int simpleregex(char *buffer, char *regex, char **found) {
84         int status;
85         regex_t re;
86         regmatch_t match[5];
87
88         if (regcomp(&re, regex, 0) !=0) {
89                 fprintf(stderr,"internal error, regex failed!\n");
90                 exit(1);
91         }
92         status = regexec(&re,buffer,5,match,0);
93         if(status == 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';
97         }
98         return(status == 0);
99 }
100
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
104  */
105 #define MYGETL_INCR 1024
106 static char * mygetline(FILE * f) {
107         size_t size = 0;
108         size_t len  = 0;
109         char * buf  = NULL;
110
111         do {
112                 size += MYGETL_INCR;
113                 if ((buf = realloc(buf,size)) == NULL)
114                         break;
115                 fgets(buf+len,MYGETL_INCR,f);
116                 len = strlen(buf);
117         } while (!feof(f) && buf[len-1]!='\n');
118         return buf;
119 }
120
121 /* text matching, used to build very poor man's XML parser */
122 int matchit(FILE *infd, char *regex, char *typeregex, char **found) {
123         char *buffer;
124
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)) {
130                                 return 1;
131                         }
132                 }
133         }
134         return 0;
135 }
136
137
138 /*
139  * convert the base64 data blog
140  */
141 int uemis_convert_base64(char *base64, uint8_t **data) {
142         int len,datalen;
143
144         len = strlen(base64);
145         datalen = (len/4 + 1)*3;
146         if (datalen < 0x123+0x25) {
147                 /* less than header + 1 sample??? */
148                 fprintf(stderr,"suspiciously short data block\n");
149         }
150         *data = malloc(datalen);
151         if (! *data) {
152                 datalen = 0;
153                 fprintf(stderr,"Out of memory\n");
154                 goto bail;
155         }
156         decode(base64, *data, len);
157
158         if (memcmp(*data,"Dive\01\00\00",7))
159                 fprintf(stderr,"Missing Dive100 header\n");
160
161 bail:
162         return datalen;
163 }
164
165 /*
166  * parse uemis base64 data blob into struct dive
167  */
168 static void parse_divelog_binary(char *base64, struct dive **divep) {
169         int datalen;
170         int i;
171         uint8_t *data;
172         struct sample *sample;
173
174         datalen = uemis_convert_base64(base64, &data);
175
176         /* first byte of divelog data is at offset 0x123 */
177         i = 0x123;
178         while ((i < datalen) && (*(uint16_t *)(data+i))) {
179                 /* it seems that a dive_time of 0 indicates the end of the valid readings */
180                 sample = prepare_sample(divep);
181                 sample->time.seconds = *(uint16_t *)(data+i);
182                 sample->depth.mm = (*(uint16_t *)(data+i+2) - 100) / 0.101428571 + 0.5;
183                 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
184                 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
185                 sample->cylinderindex = *(uint8_t *)(data+i+22);
186                 finish_sample(*divep, sample);
187                 i += 0x25;
188         }
189         return;
190 }
191
192 void
193 parse_uemis_file(char *divelogfilename,GError **error) {
194         char *found=NULL;
195         struct tm tm;
196         struct dive *dive;
197
198         FILE *divelogfile = fopen(divelogfilename,"r");
199
200         dive = alloc_dive();
201
202         if (! matchit(divelogfile,"val key=\"date\"","<ts>\\([^<]*\\)</ts>",&found)) {
203                 /* some error handling */
204                 goto bail;
205         }
206         strptime(found, "%Y-%m-%dT%H:%M:%S", &tm);
207         dive->when = utc_mktime(&tm);
208         if (! matchit(divelogfile,"<val key=\"duration\">",
209                         "<float>\\([0-9.]*\\)</float>", &found)) {
210                 /* some error handling */
211                 goto bail;
212         }
213         dive->duration.seconds = 60.0 * atof(found);
214
215         if (! matchit(divelogfile,"<val key=\"depth\">",
216                         "<int>\\([0-9.]*\\)</int>", &found)) {
217                 /* some error handling */
218                 goto bail;
219         }
220         dive->maxdepth.mm = atof(found) / 0.10143 + 0.5;
221
222         if (! matchit(divelogfile,"<val key=\"file_content\">",
223                         ">\\([a-zA-Z0-9+/]*\\)<", &found)) {
224                 /* some error handling */
225                 goto bail;
226         }
227         parse_divelog_binary(found,&dive);
228         record_dive(dive);
229 bail:
230         if (found)
231                 free(found);
232 }
233
234 /*
235  * parse the two files extracted from the SDA
236  */
237 void
238 uemis_import() {
239         GtkWidget *dialog;
240         GtkFileFilter *filter = gtk_file_filter_new ();
241         gtk_file_filter_add_pattern (filter, "*.SDA");
242         gtk_file_filter_set_name(filter, "uemis Zurich SDA files");
243         dialog = gtk_file_chooser_dialog_new("Open File",
244                                         GTK_WINDOW(main_window),
245                                         GTK_FILE_CHOOSER_ACTION_OPEN,
246                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
247                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
248                                         NULL);
249         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
250         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
251
252         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
253                 GSList *filenames;
254                 char *filename;
255                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
256
257                 GError *error = NULL;
258                 while(filenames != NULL) {
259                         filename = (char *)filenames->data;
260                         parse_uemis_file(filename, &error);
261                         if (error != NULL)
262                         {
263                                 report_error(error);
264                                 g_error_free(error);
265                                 error = NULL;
266                         }
267
268                         g_free(filename);
269                         filenames = g_slist_next(filenames);
270                 }
271                 g_slist_free(filenames);
272                 report_dives();
273         }
274         gtk_widget_destroy(dialog);
275 }