forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
191 lines (154 loc) · 5.21 KB
/
buffer.c
File metadata and controls
191 lines (154 loc) · 5.21 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
#include "devs_internal.h"
#include "jd_numfmt.h"
#include <limits.h>
#include <math.h>
static value_t invalid_numfmt(devs_ctx_t *ctx) {
return devs_throw_range_error(ctx, "buffer numfmt invalid");
}
value_t devs_buffer_op(devs_ctx_t *ctx, uint32_t fmt0, uint32_t offset, value_t buffer,
value_t *setv) {
unsigned sz = jd_numfmt_bytes(fmt0);
if (!jd_numfmt_is_valid(fmt0))
return invalid_numfmt(ctx);
unsigned bufsz;
uint8_t *data = (void *)devs_bufferish_data(ctx, buffer, &bufsz);
JD_ASSERT(data != NULL);
if (offset + sz > bufsz) {
// DMESG("gv NAN at pc=%d sz=%d %x", frame->pc, pkt->service_size, pkt->service_command);
if (setv)
devs_throw_range_error(ctx, "buffer store out of range");
return devs_undefined;
}
data += offset;
if (setv) {
JD_ASSERT(devs_buffer_is_writable(ctx, buffer));
value_t q = *setv;
if (devs_is_tagged_int(q)) {
jd_numfmt_write_i32(data, fmt0, q.val_int32);
} else {
jd_numfmt_write_float(data, fmt0, devs_value_to_double(ctx, q));
}
return devs_void;
} else {
if (jd_numfmt_is_plain_int(fmt0)) {
int32_t q = jd_numfmt_read_i32(data, fmt0);
// if it was out of range, it would get clamped
if (INT_MIN < q && q < INT_MAX)
return devs_value_from_int(q);
}
return devs_value_from_double(jd_numfmt_read_float(data, fmt0));
}
}
double devs_read_number(void *data, unsigned bufsz, uint16_t fmt0) {
unsigned sz = jd_numfmt_bytes(fmt0);
if (sz > bufsz)
return NAN;
return jd_numfmt_read_float(data, fmt0);
}
value_t devs_buffer_decode(devs_ctx_t *ctx, uint32_t fmt0, uint8_t **buf, unsigned len) {
int sp = jd_numfmt_special_idx(fmt0);
const uint8_t *data = *buf;
unsigned p;
switch (sp) {
case -1: {
if (!jd_numfmt_is_valid(fmt0))
return invalid_numfmt(ctx);
unsigned sz = jd_numfmt_bytes(fmt0);
if (sz > len)
return devs_undefined;
*buf += sz;
if (jd_numfmt_is_plain_int(fmt0)) {
int32_t q = jd_numfmt_read_i32(data, fmt0);
// if it was out of range, it would get clamped
if (INT_MIN < q && q < INT_MAX)
return devs_value_from_int(q);
}
return devs_value_from_double(jd_numfmt_read_float(data, fmt0));
}
case DEVS_NUMFMT_SPECIAL_BOOL:
if (len == 0)
return devs_undefined;
*buf += 1;
return *data ? devs_true : devs_false;
case DEVS_NUMFMT_SPECIAL_EMPTY:
return devs_undefined;
case DEVS_NUMFMT_SPECIAL_BYTES: {
devs_buffer_t *block = devs_buffer_try_alloc(ctx, len);
if (block == NULL)
return devs_undefined;
*buf += len;
memcpy(block->data, data, len);
return devs_value_from_gc_obj(ctx, block);
}
case DEVS_NUMFMT_SPECIAL_STRING0:
p = 0;
while (p < len && data[p])
p++;
*buf += p;
if (p < len)
*buf += 1; // final '\0'
return devs_value_from_gc_obj(ctx, devs_string_try_alloc_init(ctx, data, p));
case DEVS_NUMFMT_SPECIAL_STRING:
*buf += len;
return devs_value_from_gc_obj(ctx, devs_string_try_alloc_init(ctx, data, len));
case DEVS_NUMFMT_SPECIAL_PIPE:
case DEVS_NUMFMT_SPECIAL_PIPE_PORT:
return devs_throw_not_supported_error(ctx, "pipes in specs");
default:
return devs_throw_not_supported_error(ctx, "this numfmt");
}
}
unsigned devs_buffer_encode(devs_ctx_t *ctx, uint32_t fmt0, uint8_t *data, unsigned len,
value_t v) {
int sp = jd_numfmt_special_idx(fmt0);
switch (sp) {
case -1: {
if (!jd_numfmt_is_valid(fmt0)) {
invalid_numfmt(ctx);
return 0;
}
unsigned sz = jd_numfmt_bytes(fmt0);
if (sz <= len) {
if (devs_is_tagged_int(v)) {
jd_numfmt_write_i32(data, fmt0, v.val_int32);
} else {
jd_numfmt_write_float(data, fmt0, devs_value_to_double(ctx, v));
}
}
return sz;
}
case DEVS_NUMFMT_SPECIAL_BOOL:
if (1 <= len)
*data = devs_value_to_bool(ctx, v) ? 0xff : 0;
return 1;
case DEVS_NUMFMT_SPECIAL_EMPTY:
return 0;
case DEVS_NUMFMT_SPECIAL_STRING0:
case DEVS_NUMFMT_SPECIAL_STRING:
case DEVS_NUMFMT_SPECIAL_BYTES: {
unsigned sz;
const void *d = devs_bufferish_data(ctx, v, &sz);
if (d == NULL) {
v = devs_value_to_string(ctx, v);
d = devs_bufferish_data(ctx, v, &sz);
if (d == NULL)
return 0; // ???
}
if (sz > len)
sz = len;
memcpy(data, d, sz);
if (sp == DEVS_NUMFMT_SPECIAL_STRING0 && sz < len) {
data[sz] = 0;
sz++;
}
return sz;
}
case DEVS_NUMFMT_SPECIAL_PIPE:
case DEVS_NUMFMT_SPECIAL_PIPE_PORT:
devs_throw_not_supported_error(ctx, "pipes in specs");
return 0;
default:
devs_throw_not_supported_error(ctx, "this numfmt");
return 0;
}
}