Repository navigation
Expand file tree
/
Copy pathfdo.c
More file actions
678 lines (619 loc) · 20.5 KB
/
Copy pathfdo.c
File metadata and controls
678 lines (619 loc) · 20.5 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
* fdo.c - C89 implementation of FDO binary codec.
*
* Core compiler (encode) and decompiler (decode) for atom frame streams.
* Aims for exact byte parity with the Java reference implementation
* when using equivalent inputs (esp. full-style encoding).
*
* Style: K&R defs for implementation functions,
* ANSI prototypes only in the header, explicit casts, (void) for
* unchecked calls, plain C, no emojis, minimal deps.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fdo.h"
/* Encoding style codes (match Java EncodingStyle) */
#define FDO_STYLE_FULL 0
#define FDO_STYLE_LENGTH 1
#define FDO_STYLE_DATA 2
#define FDO_STYLE_ATOM 3
#define FDO_STYLE_CURRENT 4
#define FDO_STYLE_ZERO 5
#define FDO_STYLE_ONE 6
#define FDO_STYLE_PREFIX 7
#define FDO_STYLE_SHIFT 5
#define FDO_STYLE_MASK 0x1F
#define FDO_MAX_INLINE_ATOM 31
#define FDO_MAX_INLINE_DATA 7
#define FDO_MAX_SBLEN 127 /* single-byte length threshold */
/* Internal: write a single byte to output (with cap check) */
static int write_byte(unsigned char **p, unsigned char *end, int b)
{
if (*p >= end)
return -1;
*(*p)++ = (unsigned char)b;
return 0;
}
/* Internal: write raw bytes */
static int write_bytes(unsigned char **p, unsigned char *end,
const unsigned char *src, unsigned long n)
{
unsigned long i;
for (i = 0; i < n; i++) {
if (write_byte(p, end, src[i]) < 0)
return -1;
}
return 0;
}
/* Internal: write length (single byte if <=127, else 0x80|hi, lo) */
static int write_length(unsigned char **p, unsigned char *end, unsigned long len)
{
if (len <= FDO_MAX_SBLEN) {
return write_byte(p, end, (int)len);
} else {
int hi = 0x80 | ((len >> 8) & 0x7F);
int lo = (int)(len & 0xFF);
if (write_byte(p, end, hi) < 0)
return -1;
return write_byte(p, end, lo);
}
}
/* Internal K&R: emit one frame using FULL style only (or PREFIX for ext) */
static int emit_full(unsigned char **p, unsigned char *end,
const FdoFrame *f, int *cur_proto)
{
int proto = f->proto & 0x7F;
int atom = f->atom & 0xFF;
unsigned long dlen = f->len;
if ((proto & 0x60) != 0) {
/* extended protocol: use PREFIX + inner FULL */
int proto_high = (proto >> 5) & 0x03;
int proto_low = proto & 0x1F;
int prefix = (FDO_STYLE_PREFIX << FDO_STYLE_SHIFT) | (proto_high << 3) | 0;
if (write_byte(p, end, prefix) < 0) return -1;
if (write_byte(p, end, (FDO_STYLE_FULL << FDO_STYLE_SHIFT) | proto_low) < 0) return -1;
if (write_byte(p, end, atom) < 0) return -1;
if (write_length(p, end, dlen) < 0) return -1;
if (write_bytes(p, end, f->data, dlen) < 0) return -1;
*cur_proto = proto;
return 0;
}
/* normal FULL */
if (write_byte(p, end, (FDO_STYLE_FULL << FDO_STYLE_SHIFT) | proto) < 0)
return -1;
if (write_byte(p, end, atom) < 0)
return -1;
if (write_length(p, end, dlen) < 0)
return -1;
if (write_bytes(p, end, f->data, dlen) < 0)
return -1;
*cur_proto = proto;
return 0;
}
/* Internal K&R: full encoder with optional compact style selection */
static int encode_frames(const FdoFrame *frames, int n,
unsigned char *out, unsigned long cap,
unsigned long *written, int full_only)
{
unsigned char *p = out;
unsigned char *end = out + cap;
int cur_proto = 0;
int i;
for (i = 0; i < n; i++) {
const FdoFrame *f = &frames[i];
int proto = f->proto & 0x7F;
int atom = f->atom & 0xFF;
unsigned long dlen = f->len;
int same = (proto == cur_proto);
int atom_fits = (atom <= FDO_MAX_INLINE_ATOM);
if ((proto & 0x60) != 0) {
/* always prefix for ext protos */
if (emit_full(&p, end, f, &cur_proto) < 0)
return -1;
continue;
}
if (full_only) {
if (emit_full(&p, end, f, &cur_proto) < 0)
return -1;
continue;
}
/* compact selection (mirror Java heuristic) */
if (dlen == 0 && atom_fits && same) {
/* ATOM style */
if (write_byte(&p, end, (FDO_STYLE_ATOM << FDO_STYLE_SHIFT) | atom) < 0)
return -1;
continue;
}
if (dlen == 1 && f->data[0] == 0 && atom_fits && same) {
if (write_byte(&p, end, (FDO_STYLE_ZERO << FDO_STYLE_SHIFT) | atom) < 0)
return -1;
continue;
}
if (dlen == 1 && f->data[0] == 1 && atom_fits && same) {
if (write_byte(&p, end, (FDO_STYLE_ONE << FDO_STYLE_SHIFT) | atom) < 0)
return -1;
continue;
}
if (dlen == 1 && atom_fits) {
int dv = f->data[0] & 0xFF;
if (dv <= FDO_MAX_INLINE_DATA) {
if (write_byte(&p, end, (FDO_STYLE_DATA << FDO_STYLE_SHIFT) | proto) < 0)
return -1;
if (write_byte(&p, end, (dv << 5) | atom) < 0)
return -1;
cur_proto = proto;
continue;
}
}
if (dlen >= 1 && dlen <= FDO_MAX_INLINE_DATA && atom_fits) {
if (write_byte(&p, end, (FDO_STYLE_LENGTH << FDO_STYLE_SHIFT) | proto) < 0)
return -1;
if (write_byte(&p, end, ((int)dlen << 5) | atom) < 0)
return -1;
if (write_bytes(&p, end, f->data, dlen) < 0)
return -1;
cur_proto = proto;
continue;
}
if (same && atom_fits && dlen <= FDO_MAX_SBLEN) {
if (write_byte(&p, end, (FDO_STYLE_CURRENT << FDO_STYLE_SHIFT) | atom) < 0)
return -1;
if (write_length(&p, end, dlen) < 0)
return -1;
if (write_bytes(&p, end, f->data, dlen) < 0)
return -1;
continue;
}
/* fallback FULL */
if (emit_full(&p, end, f, &cur_proto) < 0)
return -1;
}
*written = (unsigned long)(p - out);
return 0;
}
int fdo_encode(const FdoFrame *frames, int nframes,
unsigned char *out, unsigned long out_cap,
unsigned long *written,
int full_only)
{
if (!frames || nframes < 0 || !out || !written)
return -1;
return encode_frames(frames, nframes, out, out_cap, written, full_only);
}
int fdo_compile_frames(const FdoFrame *frames, int nframes,
unsigned char *out, unsigned long out_cap,
unsigned long *written)
{
return fdo_encode(frames, nframes, out, out_cap, written, 1);
}
/* --- Decoder state and helpers (K&R style where local) --- */
struct dec {
const unsigned char *buf;
unsigned long len;
unsigned long pos;
int cur_proto;
/* large atom reassembly */
int large_proto;
int large_atom;
unsigned char *large_data;
unsigned long large_len;
unsigned long large_cap;
};
static int dec_read_byte(struct dec *d, int *out)
{
if (d->pos >= d->len)
return -1;
*out = d->buf[d->pos++] & 0xFF;
return 0;
}
static int dec_read_bytes(struct dec *d, unsigned char *dst, unsigned long n)
{
unsigned long i;
if (d->pos + n > d->len)
return -1;
for (i = 0; i < n; i++)
dst[i] = d->buf[d->pos++];
return 0;
}
static int dec_read_length(struct dec *d, unsigned long *out_len)
{
int b;
if (dec_read_byte(d, &b) < 0)
return -1;
if ((b & 0x80) == 0) {
*out_len = (unsigned long)b;
return 0;
}
{
int hi = b & 0x7F;
int lo;
if (dec_read_byte(d, &lo) < 0)
return -1;
*out_len = ((unsigned long)hi << 8) | (unsigned long)(lo & 0xFF);
}
return 0;
}
static int dec_grow_large(struct dec *d, unsigned long add)
{
unsigned long newcap = d->large_cap ? d->large_cap : 16;
unsigned char *nd;
while (newcap < d->large_len + add)
newcap *= 2;
nd = (unsigned char *) realloc(d->large_data, (size_t)newcap);
if (!nd)
return -1;
d->large_data = nd;
d->large_cap = newcap;
return 0;
}
static int dec_absorb_large(struct dec *d, const unsigned char *src, unsigned long n)
{
if (dec_grow_large(d, n) < 0)
return -1;
memcpy(d->large_data + d->large_len, src, (size_t)n);
d->large_len += n;
return 0;
}
static int dec_emit_large(struct dec *d, FdoFrame *out)
{
out->proto = (unsigned char)d->large_proto;
out->atom = (unsigned char)d->large_atom;
out->len = d->large_len;
out->data = d->large_data; /* transfer ownership */
/* reset */
d->large_proto = -1;
d->large_atom = -1;
d->large_data = 0;
d->large_len = 0;
d->large_cap = 0;
return 0;
}
static int decode_next(struct dec *d, FdoFrame *out);
/* Decode one frame. On large control frames that are absorbed, returns 1
(continue). When a completed large is ready, returns 0 with out filled. */
static int handle_large(struct dec *d, FdoFrame *tmp, FdoFrame *out)
{
if (tmp->proto != 0)
return 0; /* not large control */
if (tmp->atom == 4) {
/* START: [target_proto, target_atom, initial...] */
if (tmp->len < 2)
return -1;
d->large_proto = tmp->data[0] & 0xFF;
d->large_atom = tmp->data[1] & 0xFF;
d->large_len = 0;
d->large_cap = 0;
d->large_data = 0;
if (tmp->len > 2) {
if (dec_absorb_large(d, tmp->data + 2, tmp->len - 2) < 0)
return -1;
}
free(tmp->data);
tmp->data = 0;
return 1; /* absorbed */
}
if (tmp->atom == 5) {
/* SEGMENT */
if (d->large_data == 0)
return 0; /* stray, treat as normal? but for parity pass through rare */
if (dec_absorb_large(d, tmp->data, tmp->len) < 0)
return -1;
free(tmp->data);
tmp->data = 0;
return 1;
}
if (tmp->atom == 6) {
/* END */
if (d->large_data == 0) {
/* stray end: just return the frame as-is */
*out = *tmp;
return 0;
}
if (dec_absorb_large(d, tmp->data, tmp->len) < 0)
return -1;
free(tmp->data);
tmp->data = 0;
if (dec_emit_large(d, out) < 0)
return -1;
return 0;
}
return 0;
}
static int decode_style_full(struct dec *d, int first, FdoFrame *out)
{
int atom;
unsigned long ln;
out->proto = (unsigned char)(first & FDO_STYLE_MASK);
d->cur_proto = out->proto;
if (dec_read_byte(d, &atom) < 0) return -1;
out->atom = (unsigned char)atom;
if (dec_read_length(d, &ln) < 0) return -1;
out->len = ln;
if (ln > 0) {
out->data = (unsigned char *) malloc((size_t)ln);
if (!out->data) return -1;
if (dec_read_bytes(d, out->data, ln) < 0) {
free(out->data);
out->data = 0;
return -1;
}
} else {
out->data = 0;
}
return 0;
}
static int decode_style_length(struct dec *d, int first, FdoFrame *out)
{
int second;
out->proto = (unsigned char)(first & FDO_STYLE_MASK);
d->cur_proto = out->proto;
if (dec_read_byte(d, &second) < 0) return -1;
out->len = (unsigned long)((second >> 5) & 0x07);
out->atom = (unsigned char)(second & FDO_STYLE_MASK);
if (out->len > 0) {
out->data = (unsigned char *) malloc((size_t)out->len);
if (!out->data) return -1;
if (dec_read_bytes(d, out->data, out->len) < 0) {
free(out->data); out->data=0; return -1;
}
} else {
out->data = 0;
}
return 0;
}
static int decode_style_data(struct dec *d, int first, FdoFrame *out)
{
int second, dv;
out->proto = (unsigned char)(first & FDO_STYLE_MASK);
d->cur_proto = out->proto;
if (dec_read_byte(d, &second) < 0) return -1;
dv = (second >> 5) & 0x07;
out->atom = (unsigned char)(second & FDO_STYLE_MASK);
out->len = 1;
out->data = (unsigned char *) malloc(1);
if (!out->data) return -1;
out->data[0] = (unsigned char)dv;
return 0;
}
static int decode_style_atom(struct dec *d, int first, FdoFrame *out)
{
out->proto = (unsigned char)d->cur_proto;
out->atom = (unsigned char)(first & FDO_STYLE_MASK);
out->len = 0;
out->data = 0;
return 0;
}
static int decode_style_current(struct dec *d, int first, FdoFrame *out)
{
unsigned long ln;
out->proto = (unsigned char)d->cur_proto;
out->atom = (unsigned char)(first & FDO_STYLE_MASK);
if (dec_read_length(d, &ln) < 0) return -1;
out->len = ln;
if (ln > 0) {
out->data = (unsigned char *) malloc((size_t)ln);
if (!out->data) return -1;
if (dec_read_bytes(d, out->data, ln) < 0) { free(out->data); out->data=0; return -1; }
} else out->data = 0;
return 0;
}
static int decode_style_zero(struct dec *d, int first, FdoFrame *out)
{
out->proto = (unsigned char)d->cur_proto;
out->atom = (unsigned char)(first & FDO_STYLE_MASK);
out->len = 1;
out->data = (unsigned char *) malloc(1);
if (!out->data) return -1;
out->data[0] = 0;
return 0;
}
static int decode_style_one(struct dec *d, int first, FdoFrame *out)
{
out->proto = (unsigned char)d->cur_proto;
out->atom = (unsigned char)(first & FDO_STYLE_MASK);
out->len = 1;
out->data = (unsigned char *) malloc(1);
if (!out->data) return -1;
out->data[0] = 1;
return 0;
}
static int decode_style_prefix(struct dec *d, int first, FdoFrame *out)
{
int proto_high = (first >> 3) & 0x03;
int atom_high = (first >> 1) & 0x03;
int second, inner, proto_low, atomb, atom_num;
unsigned long ln;
if (dec_read_byte(d, &second) < 0) return -1;
inner = (second >> 5) & 0x07;
proto_low = second & FDO_STYLE_MASK;
out->proto = (unsigned char)((proto_high << 5) | proto_low);
d->cur_proto = out->proto;
if (dec_read_byte(d, &atomb) < 0) return -1;
if (inner == FDO_STYLE_FULL || inner == FDO_STYLE_CURRENT) {
atom_num = atomb;
if (dec_read_length(d, &ln) < 0) return -1;
out->atom = (unsigned char)atom_num;
out->len = ln;
if (ln > 0) {
out->data = (unsigned char*)malloc((size_t)ln);
if (!out->data) return -1;
if (dec_read_bytes(d, out->data, ln) < 0) { free(out->data); out->data=0; return -1; }
} else out->data = 0;
} else if (inner == FDO_STYLE_LENGTH) {
ln = (unsigned long)((atomb >> 5) & 0x07);
atom_num = (atom_high << 6) | (atomb & FDO_STYLE_MASK);
out->atom = (unsigned char)atom_num;
out->len = ln;
if (ln > 0) {
out->data = (unsigned char*)malloc((size_t)ln);
if (!out->data) return -1;
if (dec_read_bytes(d, out->data, ln) < 0) {free(out->data);out->data=0;return -1;}
} else out->data=0;
} else if (inner == FDO_STYLE_DATA) {
int dv = (atomb >> 5) & 0x07;
atom_num = (atom_high << 6) | (atomb & FDO_STYLE_MASK);
out->atom = (unsigned char)atom_num;
out->len = 1;
out->data = (unsigned char*)malloc(1);
if (!out->data) return -1;
out->data[0] = (unsigned char)dv;
} else if (inner == FDO_STYLE_ATOM) {
atom_num = (atom_high << 6) | (atomb & 0x3F);
out->atom = (unsigned char)atom_num;
out->len = 0;
out->data = 0;
} else if (inner == FDO_STYLE_ZERO) {
atom_num = (atom_high << 6) | (atomb & 0x3F);
out->atom = (unsigned char)atom_num;
out->len = 1;
out->data = (unsigned char*)malloc(1);
if (!out->data) return -1;
out->data[0] = 0;
} else if (inner == FDO_STYLE_ONE) {
atom_num = (atom_high << 6) | (atomb & 0x3F);
out->atom = (unsigned char)atom_num;
out->len = 1;
out->data = (unsigned char*)malloc(1);
if (!out->data) return -1;
out->data[0] = 1;
} else {
out->atom = (unsigned char)atomb;
out->len = 0;
out->data = 0;
}
return 0;
}
static int decode_next(struct dec *d, FdoFrame *out)
{
int first;
int style;
int rc;
if (dec_read_byte(d, &first) < 0)
return -1;
style = (first >> FDO_STYLE_SHIFT) & 0x07;
memset(out, 0, sizeof(*out));
switch (style) {
case FDO_STYLE_FULL: rc = decode_style_full(d, first, out); break;
case FDO_STYLE_LENGTH: rc = decode_style_length(d, first, out); break;
case FDO_STYLE_DATA: rc = decode_style_data(d, first, out); break;
case FDO_STYLE_ATOM: rc = decode_style_atom(d, first, out); break;
case FDO_STYLE_CURRENT: rc = decode_style_current(d, first, out); break;
case FDO_STYLE_ZERO: rc = decode_style_zero(d, first, out); break;
case FDO_STYLE_ONE: rc = decode_style_one(d, first, out); break;
case FDO_STYLE_PREFIX: rc = decode_style_prefix(d, first, out); break;
default: return -1;
}
if (rc < 0)
return -1;
return 0;
}
int fdo_decode(const unsigned char *in, unsigned long in_len,
FdoFrame **frames_out, int *n_out)
{
struct dec d;
FdoFrame *arr = 0;
int cap = 0, cnt = 0;
FdoFrame tmp;
int r;
if (!in || !frames_out || !n_out)
return -1;
memset(&d, 0, sizeof d);
d.buf = in;
d.len = in_len;
d.pos = 0;
d.cur_proto = 0;
d.large_proto = -1;
while (d.pos < d.len) {
if (decode_next(&d, &tmp) < 0) {
/* cleanup partial */
if (arr) {
int j;
for (j=0; j<cnt; j++) if (arr[j].data) free(arr[j].data);
free(arr);
}
if (d.large_data) free(d.large_data);
return -1;
}
/* large atom handling */
if (tmp.proto == 0 && (tmp.atom == 4 || tmp.atom == 5 || tmp.atom == 6)) {
FdoFrame completed;
memset(&completed, 0, sizeof completed);
r = handle_large(&d, &tmp, &completed);
if (r < 0) {
if (tmp.data) free(tmp.data);
if (d.large_data) free(d.large_data);
if (arr) { int j; for (j=0;j<cnt;j++) if(arr[j].data)free(arr[j].data); free(arr); }
return -1;
}
if (r == 1) {
/* absorbed control */
continue;
}
/* else completed or stray; use completed if filled */
if (completed.data || completed.len == 0) {
/* push completed instead of tmp */
tmp = completed;
}
}
/* append tmp to arr */
if (cnt >= cap) {
int newcap = cap ? cap * 2 : 8;
FdoFrame *na = (FdoFrame *) realloc(arr, (size_t)newcap * sizeof(FdoFrame));
if (!na) {
if (tmp.data) free(tmp.data);
if (arr) { int j; for (j=0;j<cnt;j++) if(arr[j].data)free(arr[j].data); free(arr); }
if (d.large_data) free(d.large_data);
return -1;
}
arr = na;
cap = newcap;
}
arr[cnt++] = tmp;
}
/* if large still open, treat as error or emit what we have? for strict: error */
if (d.large_data) {
free(d.large_data);
if (arr) { int j; for (j=0;j<cnt;j++) if(arr[j].data)free(arr[j].data); free(arr); }
return -1;
}
*frames_out = arr;
*n_out = cnt;
return 0;
}
void fdo_free_frames(FdoFrame *frames, int n)
{
int i;
if (!frames) return;
for (i = 0; i < n; i++) {
if (frames[i].data)
free(frames[i].data);
}
free(frames);
}
/* Minimal selftest: encode a couple frames, decode, compare logical content */
int fdo_selftest(void)
{
unsigned char buf[256];
unsigned long w;
FdoFrame frames[2];
FdoFrame *outf = 0;
int nout;
unsigned char d1[3] = {1,2,3};
int ok = 1;
frames[0].proto = 0; frames[0].atom = 0; frames[0].data = 0; frames[0].len = 0;
frames[1].proto = 0; frames[1].atom = 7; frames[1].data = d1; frames[1].len = 3;
if (fdo_compile_frames(frames, 2, buf, sizeof buf, &w) < 0)
return -1;
/* expect at least 1+1+0 + 1+1+1+3 = 8 bytes for two FULLs */
if (w < 6)
return -1;
if (fdo_decode(buf, w, &outf, &nout) < 0)
return -1;
if (nout != 2) ok = 0;
if (ok && (outf[0].proto != 0 || outf[0].atom != 0 || outf[0].len != 0)) ok=0;
if (ok && (outf[1].proto != 0 || outf[1].atom != 7 || outf[1].len != 3)) ok=0;
if (ok && outf[1].data && (outf[1].data[0]!=1 || outf[1].data[2]!=3)) ok=0;
fdo_free_frames(outf, nout);
return ok ? 0 : -1;
}