-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsds.c
179 lines (147 loc) · 3.2 KB
/
sds.c
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
#include "sds.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define __SERIALIZE_INCLUDE_AUDIO_BYTES 1
static int
sds_read(
int fd,
size_t length,
unsigned char *buf,
size_t buf_size,
err_t err
);
int
sds_open_file(
const char *filename,
int *fd_r,
err_t err
) {
int fd;
fd = open(filename, O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd == -1) {
err_set2(err, "open \"%s\": %s", filename, strerror(errno));
return 0;
}
*fd_r = fd;
return 1;
}
int
sds_get_file_size(
int fd,
size_t *size,
err_t err
) {
struct stat buf;
if (fstat(fd, &buf) == -1) {
err_set2(err, "stat: %s", strerror(errno));
return 0;
}
*size = buf.st_size;
return 1;
}
int
sds_file_size_is_ok(
size_t size,
err_t err
) {
if (
(size < SDS_HEADER_LENGTH) ||
((size - SDS_HEADER_LENGTH) % SDS_PACKET_LENGTH != 0)
) {
err_set2(err, "bad file size (%d)", size);
return 0;
}
return 1;
}
/* assumes sds_file_size_is_ok(size, ...) */
unsigned int
sds_calc_num_packets(
size_t size
) {
return (size - SDS_HEADER_LENGTH) / SDS_PACKET_LENGTH;
}
int
sds_read_header(
int fd,
unsigned char *buf,
size_t buf_size,
err_t err
) {
return sds_read(fd, SDS_HEADER_LENGTH, buf, buf_size, err);
}
int
sds_serialize_header(char *str, unsigned char *buf) {
int i;
int chars = 0;
char cstr[4]; cstr[0] = '\0';
for (i = 0; i < SDS_HEADER_LENGTH; i++) {
chars += sprintf(cstr, "%02X ", buf[i]);
str = strcat(str, cstr);
}
// write null to the last space
str[chars - 1] = '\0';
return 1;
}
int
sds_read_packet(
int fd,
unsigned char *buf,
size_t buf_size,
err_t err
) {
return sds_read(fd, SDS_PACKET_LENGTH, buf, buf_size, err);
}
int
sds_serialize_packet(char *str, unsigned char *buf, int packet_length) {
int i;
int chars = 0;
char cstr[8]; cstr[0] = '\0';
for (i = 0; i < 5; i++) {
chars += sprintf(cstr, "%02X ", buf[i]);
str = strcat(str, cstr);
}
if (__SERIALIZE_INCLUDE_AUDIO_BYTES) {
for (i = 5; i < 125; i++) {
chars += sprintf(cstr, "%02X ", buf[i]);
str = strcat(str, cstr);
}
} else {
chars += sprintf(cstr, " ... ");
str = strcat(str, cstr);
}
for (i = 125; i < packet_length; i++) {
chars += sprintf(cstr, "%02X ", buf[i]);
str = strcat(str, cstr);
}
str[chars - 1] = '\0';
return 1;
}
static int
sds_read(
int fd,
size_t length,
unsigned char *buf,
size_t buf_size,
err_t err
) {
ssize_t r;
if (length > buf_size) {
err_set2(err, "supposed to read %d bytes, buffer size only %d", length, buf_size);
return 0;
}
r = read(fd, buf, length);
if ((size_t)r != length) {
if (r == -1) {
err_set2(err, "read: %s", strerror(errno));
} else {
err_set2(err, "read: tried to read %d bytes, actually read %d", length, r);
}
return 0;
}
return 1;
}