]> git.tdb.fi Git - ext/subsurface.git/blob - file.c
Import: always open and read the file before checking the filename extension
[ext/subsurface.git] / file.c
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7
8 #include "dive.h"
9
10 struct memblock {
11         void *buffer;
12         size_t size;
13 };
14
15 static int readfile(const char *filename, struct memblock *mem)
16 {
17         int ret, fd = open(filename, O_RDONLY);
18         struct stat st;
19         char *buf;
20
21         mem->buffer = NULL;
22         mem->size = 0;
23
24         fd = open(filename, O_RDONLY);
25         if (fd < 0)
26                 return fd;
27         ret = fstat(fd, &st);
28         if (ret < 0)
29                 goto out;
30         ret = -EINVAL;
31         if (!S_ISREG(st.st_mode))
32                 goto out;
33         ret = 0;
34         if (!st.st_size)
35                 goto out;
36         buf = malloc(st.st_size+1);
37         ret = -1;
38         errno = ENOMEM;
39         if (!buf)
40                 goto out;
41         mem->buffer = buf;
42         mem->size = st.st_size;
43         ret = read(fd, buf, mem->size);
44         if (ret < 0)
45                 goto free;
46         buf[ret] = 0;
47         if (ret == mem->size)
48                 goto out;
49         errno = EIO;
50         ret = -1;
51 free:
52         free(mem->buffer);
53         mem->buffer = NULL;
54         mem->size = 0;
55 out:
56         close(fd);
57         return ret;
58 }
59
60 #ifdef LIBZIP
61 #include <zip.h>
62
63 static void suunto_read(struct zip_file *file, GError **error)
64 {
65         int size = 1024, n, read = 0;
66         char *mem = malloc(size);
67
68         while ((n = zip_fread(file, mem+read, size-read)) > 0) {
69                 read += n;
70                 size = read * 3 / 2;
71                 mem = realloc(mem, size);
72         }
73         parse_xml_buffer("SDE file", mem, read, error);
74         free(mem);
75 }
76 #endif
77
78 static int try_to_open_suunto(const char *filename, struct memblock *mem, GError **error)
79 {
80         int success = 0;
81 #ifdef LIBZIP
82         /* Grr. libzip needs to re-open the file, it can't take a buffer */
83         struct zip *zip = zip_open(filename, ZIP_CHECKCONS, NULL);
84
85         if (zip) {
86                 int index;
87                 for (index = 0; ;index++) {
88                         struct zip_file *file = zip_fopen_index(zip, index, 0);
89                         if (!file)
90                                 break;
91                         suunto_read(file, error);
92                         zip_fclose(file);
93                         success++;
94                 }
95                 zip_close(zip);
96         }
97 #endif
98         return success;
99 }
100
101 static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem, GError **error)
102 {
103         /* Suunto Dive Manager files: SDE */
104         if (!strcasecmp(fmt, "SDE"))
105                 return try_to_open_suunto(filename, mem, error);
106
107         return 0;
108 }
109
110 static void parse_file_buffer(const char *filename, struct memblock *mem, GError **error)
111 {
112         char *fmt = strrchr(filename, '.');
113         if (fmt && open_by_filename(filename, fmt+1, mem, error))
114                 return;
115
116         parse_xml_buffer(filename, mem->buffer, mem->size, error);
117 }
118
119 void parse_file(const char *filename, GError **error)
120 {
121         struct memblock mem;
122
123         if (readfile(filename, &mem) < 0) {
124                 fprintf(stderr, "Failed to read '%s'.\n", filename);
125                 if (error) {
126                         *error = g_error_new(g_quark_from_string("subsurface"),
127                                              DIVE_ERROR_PARSE,
128                                              "Failed to read '%s'",
129                                              filename);
130                 }
131                 return;
132         }
133
134         parse_file_buffer(filename, &mem, error);
135         free(mem.buffer);
136 }