-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsr.c
More file actions
245 lines (186 loc) · 5.29 KB
/
libsr.c
File metadata and controls
245 lines (186 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <err.h>
#include <zip.h>
#include "libsr.h"
/*
This file is part of libsr, a small helper for processing Sigrok-captures (.sr) in C
(c) 2023 kittennbfive
https://github.com/kittennbfive/
AGPLv3+ and NO WARRANTY!
Please read the fine manual.
version 0.02
*/
typedef struct
{
uint_fast8_t number;
char name[LENGTH_NAME_MAX+1];
} channel_t;
static channel_t * channels;
static uint_fast8_t nb_channels_parsed=0;
static uint_fast8_t unitsize=0; //bytes per sample
static zip_t * zip=NULL;
static uint8_t * data=NULL;
static uint64_t bytes_in_buffer_total=0;
static bool get_file_size(char const * const fname, zip_uint64_t * const filesize)
{
zip_stat_t stat;
if(zip_stat(zip, fname, 0, &stat))
{
zip_error_t * error=zip_get_error(zip);
if(error->zip_err==ZIP_ER_NOENT)
{
(*filesize)=0;
zip_error_fini(error);
return false;
}
else
errx(1, "libsr: zip_stat(%s) failed", fname);
}
if(!(stat.valid&ZIP_STAT_SIZE))
errx(1, "libsr: zip_stat(%s) did not return a valid filesize", fname);
(*filesize)=stat.size;
return true;
}
static bool get_file(char const * const fname, uint8_t * const buffer, zip_uint64_t * const filesize, const bool c_string)
{
zip_uint64_t fsize;
if(!get_file_size(fname, &fsize))
return false;
if(filesize)
(*filesize)=fsize;
zip_file_t * file=zip_fopen(zip, fname, 0);
if(!file)
errx(1, "libsr: zip_open(%s) failed", fname);
zip_int64_t bytes_read=zip_fread(file, buffer, fsize);
if(bytes_read<0 || (zip_uint64_t)bytes_read!=fsize)
errx(1, "libsr: zip_fread(%s) failed", fname);
if(zip_fclose(file))
errx(1, "libsr: zip_fclose(%s) failed", fname);
if(c_string)
buffer[fsize]='\0';
return true;
}
static bool get_logic_file(const uint_fast32_t nb_logic_file, uint8_t * buffer, zip_uint64_t * const filesize)
{
char name_logic_file[50];
sprintf(name_logic_file, "logic-1-%lu", nb_logic_file);
return get_file(name_logic_file, buffer, filesize, false);
}
static void check_version(void)
{
uint8_t * version=NULL;
zip_uint64_t sz_version;
if(!get_file_size("version", &sz_version))
errx(1, "libsr: file \"version\" not found");
version=malloc(sz_version*sizeof(uint8_t));
get_file("version", version, NULL, false);
if(sz_version!=1 || version[0]!='2')
errx(1, "libsr: version %u of sr-format not supported (only version 2 supported)", version[0]);
free(version);
}
static void parse_line(char * l)
{
if(!strlen(l))
return;
unsigned int number;
char name[LENGTH_NAME_MAX+1];
if(sscanf(l, "probe%u=%s", &number, name)==2)
{
//that's not pretty but we don't know the max number of channels until the last line (unitsize)
//the number of channels should be pretty small so this way it should be ok...
channels=realloc(channels, (nb_channels_parsed+1)*sizeof(channel_t));
channels[nb_channels_parsed].number=number;
strncpy(channels[nb_channels_parsed].name, name, LENGTH_NAME_MAX);
channels[nb_channels_parsed].name[LENGTH_NAME_MAX]='\0';
nb_channels_parsed++;
//printf("parsed channel %s, now %u channels\n", name, nb_channels_parsed);
}
else if(sscanf(l, "unitsize=%hhu", &unitsize)==1)
{
//printf("unitsize: %u\n", unitsize);
}
}
static void parse_metadata(void)
{
char * metadata=NULL;
zip_uint64_t sz_metadata;
if(!get_file_size("metadata", &sz_metadata))
errx(1, "libsr: file \"metadata\" not found");
metadata=malloc(sz_metadata*sizeof(uint8_t));
if(!get_file("metadata", (uint8_t*)metadata, NULL, true))
errx(1, "libsr: could not read metadata");
char * l;
l=strtok(metadata, "\n");
parse_line(l);
while((l=strtok(NULL, "\n")))
parse_line(l);
free(metadata);
if(unitsize==0)
errx(1, "libsr: invalid unitsize 0, parser problem?");
}
static void parse_data(void)
{
char name_logic_file[50];
uint_fast32_t nb_logic_file=1;
uint64_t fsize;
do
{
sprintf(name_logic_file, "logic-1-%lu", nb_logic_file);
if(!get_file_size(name_logic_file, &fsize))
break;
bytes_in_buffer_total+=fsize;
nb_logic_file++;
} while(1);
//printf("bytes_in_buffer_total %lu\n", bytes_in_buffer_total);
data=malloc(bytes_in_buffer_total*sizeof(uint8_t));
uint_fast32_t nb_files=nb_logic_file;
uint64_t pos=0;
for(nb_logic_file=1; nb_logic_file<nb_files; nb_logic_file++)
{
get_logic_file(nb_logic_file, &data[pos], &fsize);
pos+=fsize;
}
}
void sr_open(char const * const filename)
{
int err;
zip=zip_open(filename, ZIP_RDONLY, &err);
if(zip==NULL)
errx(1, "libsr: zip_open(%s) failed with error %d", filename, err); //TODO: is there a way to get a string-representation for err?
check_version();
parse_metadata();
parse_data();
}
uint_fast8_t sr_get_nb_channels(void)
{
return nb_channels_parsed;
}
uint_fast8_t sr_get_channel_bitpos(char const * const name)
{
uint_fast8_t i;
for(i=0; i<nb_channels_parsed; i++)
{
if(!strcmp(channels[i].name, name))
return channels[i].number-1;
}
errx(1, "libsr: could not find channel %s", name);
}
uint64_t sr_get_nb_samples(void)
{
return bytes_in_buffer_total/unitsize;
}
bool sr_get_sample(const uint_fast8_t pos_channel, const uint64_t sample)
{
uint_fast8_t offset=pos_channel/8;
uint_fast8_t shift=pos_channel%8;
return (data[unitsize*sample+offset]>>shift)&1;
}
void sr_close(void)
{
free(data);
free(channels);
}