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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
/* Copyright (c) 1997-2004 Miller Puckette, krzYszcz, and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/* LATER verify endianness transparency */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#define BINPORT_MAXSTRING 1000
#define BINPORT_SYMGROW 64
#ifndef BINPORT_STANDALONE
/* load a max binary file into a Pd binbuf */
#include "m_pd.h"
#else
/* make a max-textual listing from a max binary file */
/* This is a standalone version of a ``max binary to binbuf'' module.
It uses certain Pd calls and structs, which are duplicated below.
LATER should be linked to the Pd API library. */
#define BINPORT_VERBOSE
//#define BINPORT_DEBUG
#endif
#include "binport.h"
static void binport_error(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "ERROR (binport): ");
vfprintf(stderr, fmt, ap);
putc('\n', stderr);
va_end(ap);
}
static void binport_warning(char *fmt, ...)
{
#if defined (BINPORT_STANDALONE) || defined(BINPORT_VERBOSE)
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "warning (binport): ");
vfprintf(stderr, fmt, ap);
putc('\n', stderr);
va_end(ap);
#endif
}
static void binport_bug(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "BUG (binport): ");
vfprintf(stderr, fmt, ap);
putc('\n', stderr);
va_end(ap);
}
static void binport_failure(char *filename)
{
binport_error("\"%s\" doesn't look like a patch file", filename);
}
static void binpold_failure(char *filename)
{
binport_error("tried reading \"%s\" as an old format file, but failed",
filename);
}
#ifdef BINPORT_STANDALONE
typedef int t_int;
typedef float t_float;
typedef struct _symbol
{
char *s_name;
void *s_thing;
struct _symbol *s_next;
} t_symbol;
typedef union word
{
t_float w_float;
t_symbol *w_symbol;
int w_index;
} t_word;
typedef enum
{
A_NULL,
A_FLOAT,
A_SYMBOL,
A_POINTER,
A_SEMI,
A_COMMA,
A_DEFFLOAT,
A_DEFSYM,
A_DOLLAR,
A_DOLLSYM,
A_GIMME,
A_CANT
} t_atomtype;
typedef struct _atom
{
t_atomtype a_type;
union word a_w;
} t_atom;
void *getbytes(size_t nbytes)
{
void *ret;
if (nbytes < 1) nbytes = 1;
ret = (void *)calloc(nbytes, 1);
if (!ret)
binport_error("getbytes() failed -- out of memory");
return (ret);
}
void *resizebytes(void *old, size_t oldsize, size_t newsize)
{
void *ret;
if (newsize < 1) newsize = 1;
if (oldsize < 1) oldsize = 1;
ret = (void *)realloc((char *)old, newsize);
if (newsize > oldsize && ret)
memset(((char *)ret) + oldsize, 0, newsize - oldsize);
if (!ret)
binport_error("resizebytes() failed -- out of memory");
return (ret);
}
void freebytes(void *fatso, size_t nbytes)
{
free(fatso);
}
#define HASHSIZE 1024
static t_symbol *symhash[HASHSIZE];
t_symbol *dogensym(char *s, t_symbol *oldsym)
{
t_symbol **sym1, *sym2;
unsigned int hash1 = 0, hash2 = 0;
int length = 0;
char *s2 = s;
while (*s2)
{
hash1 += *s2;
hash2 += hash1;
length++;
s2++;
}
sym1 = symhash + (hash2 & (HASHSIZE-1));
while (sym2 = *sym1)
{
if (!strcmp(sym2->s_name, s)) return(sym2);
sym1 = &sym2->s_next;
}
if (oldsym) sym2 = oldsym;
else
{
sym2 = (t_symbol *)getbytes(sizeof(*sym2));
sym2->s_name = getbytes(length+1);
sym2->s_next = 0;
sym2->s_thing = 0;
strcpy(sym2->s_name, s);
}
*sym1 = sym2;
return (sym2);
}
t_symbol *gensym(char *s)
{
return(dogensym(s, 0));
}
#endif /* end of Pd API */
enum {
BINPORT_NULLTYPE,
BINPORT_INTTYPE = 1, BINPORT_FLOATTYPE, BINPORT_SYMTYPE,
BINPORT_DEFINTTYPE = 5, BINPORT_DEFFLOATTYPE, BINPORT_DEFSYMTYPE,
BINPORT_SEMITYPE = 10, BINPORT_COMMATYPE,
BINPORT_DOLLARTYPE, BINPORT_DOLLSYMTYPE
};
/* We use A_INT atom type not only for listing, but for import too --
the parser passes ints to individual token handlers, so that any
required conversion has to be done during Pd message generation. */
#define A_INT A_DEFFLOAT
static int binport_readbuf(FILE *fp, char *buf, size_t sz)
{
return (fread(buf, 1, sz, fp) == sz ? sz : 0);
}
static int binport_readbyte(FILE *fp, unsigned char *buf)
{
int c;
if ((c = fgetc(fp)) == EOF)
return (0);
*buf = (unsigned char)c;
return (1);
}
static int binport_readint(FILE *fp, int *iptr)
{
unsigned char word[4];
if (fread(word, 1, 4, fp) == 4)
{
*iptr = ((word[0] << 24) | (word[1] << 16) | (word[2] << 8) | word[3]);
return (4);
}
else return (0);
}
/* LATER more testing */
/* make it binpold_readfloat() */
static int binport_readfloat(FILE *fp, float *fptr)
{
unsigned char word[10];
if (fread(word, 1, 10, fp) == 10)
{
int ex;
unsigned hi, lo;
ex = ((word[0] & 0x7F) << 8) | word[1];
hi = ((unsigned)word[2] << 24) | ((unsigned)word[3] << 16) |
((unsigned)word[4] << 8) | (unsigned)word[5];
lo = ((unsigned)word[6] << 24) | ((unsigned)word[7] << 16) |
((unsigned)word[8] << 8) | (unsigned)word[9];
if (ex == 0x7FFF)
{
binport_warning("NaN atom bashed to zero");
*fptr = 0.;
}
else if (ex || hi || lo)
{
double d;
ex -= 0x401e;
hi = ((hi - 0x7fffffff) - 1) + ((float)0x7fffffff + 1.);
lo = ((lo - 0x7fffffff) - 1) + ((float)0x7fffffff + 1.);
d = ldexp(hi, ex) + ldexp(lo, ex - 32);
*fptr = ((word[0] & 0x80) ? -(float)d : (float)d);
}
else *fptr = 0.;
#ifdef BINPORT_DEBUG
fprintf(stderr, "%02x%02x", (int)word[0], (int)word[1]);
fprintf(stderr, " %02x%02x%02x%02x",
(int)word[2], (int)word[3], (int)word[4], (int)word[5]);
fprintf(stderr, " %02x%02x%02x%02x",
(int)word[6], (int)word[7], (int)word[8], (int)word[9]);
fprintf(stderr, " == %g\n", *fptr);
#endif
return (10);
}
else return (0);
}
static int binport_readstring(FILE *fp, char *buf)
{
int c, i = 1;
while (c = fgetc(fp))
{
if (c == EOF)
return (0);
if (++i < BINPORT_MAXSTRING)
*buf++ = (unsigned char)c;
}
*buf = '\0';
if (i >= BINPORT_MAXSTRING)
binport_warning("symbol string too long, skipped");
return (i);
}
typedef struct _binpold
{
FILE *o_fp;
int o_natoms;
int o_bodysize;
int o_nsymbols;
int o_symbolid;
int o_ndx;
t_atom *o_atombuf;
} t_binpold;
#define BINPOLD_NATOMTYPES 16
#define BINPOLD_MAXATOMS 1000000 /* LATER rethink */
static t_atomtype binpold_atomtypes[BINPOLD_NATOMTYPES] = {
A_NULL, A_INT, A_FLOAT, A_SYMBOL,
A_CANT, A_CANT, A_CANT, A_CANT, A_CANT, A_CANT,
A_SEMI, A_COMMA, A_DOLLAR, A_CANT, A_CANT, A_CANT
};
static int binpold_gettype(t_binpold *old, t_atom *ap)
{
int typecode;
if ((typecode = fgetc(old->o_fp)) != EOF)
{
if (typecode > 0 && typecode < BINPOLD_NATOMTYPES)
{
ap->a_type = binpold_atomtypes[typecode];
if (ap->a_type != A_CANT)
return (1);
else binport_warning("unsupported type of atom %d: %d",
old->o_ndx, typecode);
}
else binport_warning("bad type of atom %d: %d", old->o_ndx, typecode);
}
else binport_warning("failed reading type of atom %d", old->o_ndx);
return (0);
}
static int binpold_getvalue(t_binpold *old, t_atom *ap, int *countp)
{
int ival;
float fval;
*countp = 0;
switch (ap->a_type)
{
case A_INT:
case A_SYMBOL:
if (*countp = binport_readint(old->o_fp, &ival))
ap->a_w.w_index = ival;
else
goto valuefailed;
if (ap->a_type == A_SYMBOL)
{
if (ival >= old->o_nsymbols)
old->o_nsymbols = ival + 1;
ap->a_type = A_DEFSYM; /* invalidate, until w_symbol is known */
}
break;
case A_FLOAT:
if (*countp = binport_readfloat(old->o_fp, &fval))
ap->a_w.w_float = fval;
else
goto valuefailed;
break;
case A_SEMI:
case A_COMMA:
break;
case A_DOLLAR:
if (*countp = binport_readint(old->o_fp, &ival))
ap->a_w.w_index = ival;
else
goto valuefailed;
break;
default:
goto valuefailed;
}
return (1);
valuefailed:
binport_warning("failed reading value of atom %d (type %d)",
old->o_ndx, ap->a_type);
return (0);
}
static int binpold_load(t_binpold *old)
{
char buf[BINPORT_MAXSTRING];
t_atom *ap;
int total;
#ifdef BINPORT_DEBUG
fprintf(stderr, "old format: %d atoms, %d-byte chunk of atom values\n",
old->o_natoms, old->o_bodysize);
#endif
for (old->o_ndx = 0, ap = old->o_atombuf;
old->o_ndx < old->o_natoms; old->o_ndx++, ap++)
if (!binpold_gettype(old, ap))
return (0);
old->o_nsymbols = 0;
total = 0;
for (old->o_ndx = 0, ap = old->o_atombuf;
old->o_ndx < old->o_natoms; old->o_ndx++, ap++)
{
int count;
if (!binpold_getvalue(old, ap, &count))
return (0);
total += count;
}
if (total != old->o_bodysize)
{
binport_warning("actual chunk size %d inconsistent with declared %d",
total, old->o_bodysize);
return (0);
}
for (old->o_symbolid = 0;
old->o_symbolid < old->o_nsymbols; old->o_symbolid++)
{
if (binport_readstring(old->o_fp, buf))
{
t_symbol *s = gensym(buf);
for (old->o_ndx = 0, ap = old->o_atombuf;
old->o_ndx < old->o_natoms; old->o_ndx++, ap++)
{
if (ap->a_type == A_DEFSYM &&
ap->a_w.w_index == old->o_symbolid)
{
ap->a_type = A_SYMBOL;
ap->a_w.w_symbol = s;
}
}
}
else
{
binport_warning("failed reading string for symbol %d",
old->o_symbolid);
return (0);
}
}
for (old->o_ndx = 0, ap = old->o_atombuf;
old->o_ndx < old->o_natoms; old->o_ndx++, ap++)
{
if (ap->a_type == A_DEFSYM)
{
binport_warning("unknown string for symbol %d", ap->a_w.w_index);
return (0);
}
else if (ap->a_type == A_DOLLAR)
{
sprintf(buf, "#%d", ap->a_w.w_index);
ap->a_type = A_SYMBOL;
ap->a_w.w_symbol = gensym(buf);
}
/* CHECKME A_DOLLSYM */
}
return (1);
}
static int binpold_nextatom(t_binpold *old, t_atom *ap)
{
if (old->o_ndx < old->o_natoms)
{
*ap = old->o_atombuf[old->o_ndx++];
return (1);
}
else return (0);
}
static void binpold_free(t_binpold *old)
{
if (old->o_fp)
fclose(old->o_fp);
if (old->o_atombuf)
freebytes(old->o_atombuf, old->o_natoms * sizeof(*old->o_atombuf));
freebytes(old, sizeof(*old));
}
static t_binpold *binpold_new(FILE *fp)
{
int natoms, bodysize;
if (binport_readint(fp, &natoms))
{
if (natoms < 0 || natoms > BINPOLD_MAXATOMS)
binport_warning("bad number of atoms: %d", natoms);
else if (binport_readint(fp, &bodysize))
{
if (bodysize < 0)
binport_warning("negative chunk size: %d", bodysize);
else
{
t_binpold *old = getbytes(sizeof(*old));
old->o_fp = fp;
old->o_natoms = natoms;
old->o_bodysize = bodysize;
if (!(old->o_atombuf =
getbytes(old->o_natoms * sizeof(*old->o_atombuf))))
{
binport_error("could not allocate %d atoms", old->o_natoms);
freebytes(old, sizeof(*old));
fclose(fp);
return (0);
}
return (old);
}
}
}
else binport_warning("file too short");
fclose(fp);
return (0);
}
typedef struct _binport
{
FILE *b_fp;
int b_ftype;
int b_nsymbols;
int b_symsize;
t_symbol **b_symtable;
t_binpold *b_old;
} t_binport;
static void binport_setint(t_atom *ap, int i)
{
ap->a_type = A_INT;
ap->a_w.w_index = i;
}
static void binport_setfloat(t_atom *ap, float f)
{
ap->a_type = A_FLOAT;
ap->a_w.w_float = f;
}
static void binport_setsymbol(t_atom *ap, t_symbol *s)
{
ap->a_type = A_SYMBOL;
ap->a_w.w_symbol = s;
}
static t_symbol *binport_makesymbol(t_binport *bp, int id)
{
char s[BINPORT_MAXSTRING];
if (id < bp->b_nsymbols)
binport_bug("symbol id mismatch");
else if (id > bp->b_nsymbols)
binport_error("unexpected symbol id");
else if (binport_readstring(bp->b_fp, s))
{
int reqsize = ++bp->b_nsymbols;
if (reqsize > bp->b_symsize)
{
reqsize += (BINPORT_SYMGROW - 1);
#ifdef BINPORT_DEBUG
binport_warning("resizing symbol table to %d elements", reqsize);
#endif
if (bp->b_symtable =
resizebytes(bp->b_symtable,
bp->b_symsize * sizeof(*bp->b_symtable),
reqsize * sizeof(*bp->b_symtable)))
bp->b_symsize = reqsize;
else
{
bp->b_nsymbols = bp->b_symsize = 0;
return (0);
}
}
return (bp->b_symtable[id] = gensym(s));
}
return (0);
}
static int binport_setbysymtable(t_binport *bp, t_atom *ap, int id)
{
t_symbol *s;
if (id < bp->b_nsymbols)
s = bp->b_symtable[id];
else
s = binport_makesymbol(bp, id);
if (s)
{
ap->a_type = A_SYMBOL;
ap->a_w.w_symbol = s;
}
return (s != 0);
}
/* single pass of binbuf_text(), int-preserving version */
static int maxtext_nextatom(FILE *fp, t_atom *ap)
{
char buf[BINPORT_MAXSTRING + 1], *bufp, *ebuf = buf + BINPORT_MAXSTRING;
int ready;
unsigned char ch;
ap->a_type = A_NULL;
while ((ready = binport_readbyte(fp, &ch)) &&
(ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'));
if (!ready)
return (0);
if (ch == ';')
ap->a_type = A_SEMI;
else if (ch == ',')
ap->a_type = A_COMMA;
else
{
int floatstate = 0, slash = 0, lastslash = 0, firstslash = (ch == '\\');
bufp = buf;
do
{
*bufp = ch;
lastslash = slash;
slash = (ch == '\\');
if (floatstate >= 0)
{
int digit = (ch >= '0' && ch <= '9'),
dot = (ch == '.'), minus = (ch == '-'),
plusminus = (minus || (ch == '+')),
expon = (ch == 'e' || ch == 'E');
if (floatstate == 0) /* beginning */
{
if (minus) floatstate = 1;
else if (digit) floatstate = 2;
else if (dot) floatstate = 3;
else floatstate = -1;
}
else if (floatstate == 1) /* got minus */
{
if (digit) floatstate = 2;
else if (dot) floatstate = 3;
else floatstate = -1;
}
else if (floatstate == 2) /* got digits */
{
if (dot) floatstate = 4;
else if (expon) floatstate = 6;
else if (!digit) floatstate = -1;
}
else if (floatstate == 3) /* got '.' without digits */
{
if (digit) floatstate = 5;
else floatstate = -1;
}
else if (floatstate == 4) /* got '.' after digits */
{
if (digit) floatstate = 5;
else if (expon) floatstate = 6;
else floatstate = -1;
}
else if (floatstate == 5) /* got digits after . */
{
if (expon) floatstate = 6;
else if (!digit) floatstate = -1;
}
else if (floatstate == 6) /* got 'e' */
{
if (plusminus) floatstate = 7;
else if (digit) floatstate = 8;
else floatstate = -1;
}
else if (floatstate == 7) /* got plus or minus */
{
if (digit) floatstate = 8;
else floatstate = -1;
}
else if (floatstate == 8) /* got digits */
{
if (!digit) floatstate = -1;
}
}
if (!slash) bufp++;
}
while ((ready = binport_readbyte(fp, &ch)) && bufp != ebuf
&& (slash || (ch != ' ' && ch != '\n' && ch != '\r'
&& ch != '\t' && ch != ',' && ch != ';')));
if (ready && (ch == ',' || ch == ';'))
ungetc(ch, fp);
*bufp = 0;
#if 0
fprintf(stderr, "buf %s\n", buf);
#endif
if (*buf == '$' && buf[1] >= '0' && buf[1] <= '9' && !firstslash)
{
for (bufp = buf+2; *bufp; bufp++)
{
if (*bufp < '0' || *bufp > '9')
{
ap->a_type = A_DOLLSYM;
ap->a_w.w_symbol = gensym(buf+1);
break;
}
}
if (ap->a_type == A_NULL)
{
ap->a_type = A_DOLLAR;
ap->a_w.w_index = atoi(buf+1);
}
}
else if (floatstate == 2)
binport_setint(ap, atoi(buf));
else if (floatstate == 4 || floatstate == 5 || floatstate == 8)
binport_setfloat(ap, atof(buf));
else
binport_setsymbol(ap, gensym(buf));
}
return (1);
}
static int binport_nextatom(t_binport *bp, t_atom *ap)
{
unsigned char opcode;
int opval;
char buf[64];
if (bp->b_ftype == BINPORT_MAXTEXT)
return (maxtext_nextatom(bp->b_fp, ap));
else if (bp->b_ftype == BINPORT_MAXOLD && bp->b_old)
return (binpold_nextatom(bp->b_old, ap));
if (!binport_readbyte(bp->b_fp, &opcode))
goto badbin;
opval = opcode & 0x0f;
switch (opcode >> 4)
{
case BINPORT_INTTYPE: /* variable length int,
opval: length (number of bytes that follow) */
if (!binport_readbuf(bp->b_fp, buf, opval))
goto badbin;
else
{
unsigned char *p = (unsigned char *)buf + opval;
int i = 0;
while (opval--) i = (i << 8) | *--p;
if (opcode == 0x12) /* FIXME */
i = (short)i;
binport_setint(ap, i);
}
break;
case BINPORT_FLOATTYPE: /* variable length float,
opval: length (number of bytes that follow) */
if (!binport_readbuf(bp->b_fp, buf, opval))
goto badbin;
else
{
unsigned char *p = (unsigned char *)buf + opval;
int i = 0;
while (opval--) i = (i << 8) | *--p;
binport_setfloat(ap, *(t_float *)&i);
}
break;
case BINPORT_SYMTYPE: /* variable length symbol id,
opval: length (number of bytes that follow) */
if (!binport_readbuf(bp->b_fp, buf, opval))
goto badbin;
else
{
unsigned char *p = (unsigned char *)buf + opval;
int i = 0;
while (opval--) i = (i << 8) | *--p;
if (!binport_setbysymtable(bp, ap, i))
goto badbin;
}
break;
case BINPORT_DEFINTTYPE: /* half-byte int */
binport_setint(ap, opval);
break;
case BINPORT_DEFSYMTYPE: /* half-byte symbol id */
if (!binport_setbysymtable(bp, ap, opval))
goto badbin;
break;
case BINPORT_SEMITYPE:
/* LATER warn about nonzero opval */
ap->a_type = A_SEMI;
break;
case BINPORT_COMMATYPE:
/* CHECKME apparently never used? */
binport_warning("found the comma type in max binary...");
/* LATER warn about nonzero opval */
ap->a_type = A_COMMA;
break;
case BINPORT_DOLLARTYPE: /* #number */
sprintf(buf, "#%d", opval);
ap->a_type = A_SYMBOL;
ap->a_w.w_symbol = gensym(buf);
break;
case BINPORT_DOLLSYMTYPE: /* #symbol id,
opval: length (number of bytes that follow) */
if (!binport_readbuf(bp->b_fp, buf, opval))
goto badbin;
else
{
unsigned char *p = (unsigned char *)buf + opval;
int i = 0;
while (opval--) i = (i << 8) | *--p;
if (!binport_setbysymtable(bp, ap, i))
goto badbin;
}
sprintf(buf, "#%s", ap->a_w.w_symbol->s_name);
#ifdef BINPORT_DEBUG
binport_warning(buf);
#endif
ap->a_w.w_symbol = gensym(buf);
break;
default:
binport_error("unknown opcode %x", (int)opcode);
goto badbin;
}
return (1);
badbin:
return (0);
}
static int binport_alike(char *header, int *ftypep)
{
static char bin_header[4] = { 2, 0, 0, 0 }; /* CHECKME any others? */
static char old_header[4] = { 0, 0, 0, 1 }; /* CHECKME any others? */
static char text_header[4] = { 'm', 'a', 'x', ' ' };
static char pd_header[3] = { '#', 'N', ' ' }; /* canvas or struct */
if (memcmp(header, bin_header, 4) == 0)
*ftypep = BINPORT_OK;
else if (memcmp(header, text_header, 4) == 0)
*ftypep = BINPORT_MAXTEXT;
else if (memcmp(header, old_header, 4) == 0)
*ftypep = BINPORT_MAXOLD;
else
{
if (memcmp(header, pd_header, 3) == 0)
*ftypep = BINPORT_PDFILE;
else
*ftypep = BINPORT_INVALID;
return (0);
}
return (1);
}
static void binport_free(t_binport *bp)
{
fclose(bp->b_fp);
if (bp->b_symtable)
freebytes(bp->b_symtable, bp->b_symsize * sizeof(*bp->b_symtable));
if (bp->b_old)
{
bp->b_old->o_fp = 0;
binpold_free(bp->b_old);
}
freebytes(bp, sizeof(*bp));
}
static t_binport *binport_new(FILE *fp, int *ftypep)
{
t_binport *bp = 0;
char header[4];
if (fread(header, 1, 4, fp) == 4)
{
int alike = binport_alike(header, ftypep);
if (alike)
{
bp = getbytes(sizeof(*bp));
bp->b_fp = fp;
bp->b_ftype = *ftypep;
bp->b_nsymbols = 0;
if (*ftypep == BINPORT_OK)
{
bp->b_symsize = BINPORT_SYMGROW;
bp->b_symtable =
getbytes(bp->b_symsize * sizeof(*bp->b_symtable));
}
else
{
bp->b_symsize = 0;
bp->b_symtable = 0;
}
bp->b_old = 0;
}
else if (*ftypep != BINPORT_PDFILE)
binport_warning("unknown header: %02x%02x%02x%02x",
(int)header[0], (int)header[1],
(int)header[2], (int)header[3]);
}
else
{
binport_warning("file too short");
*ftypep = BINPORT_INVALID;
}
if (!bp) fclose(fp);
return (bp);
}
static void binport_atomstring(t_atom *ap, char *buf, int bufsize)
{
char *sp, *bp, *ep;
switch(ap->a_type)
{
case A_SEMI:
strcpy(buf, ";"); break;
case A_COMMA:
strcpy(buf, ","); break;
case A_INT:
sprintf(buf, "%d", ap->a_w.w_index); break;
case A_FLOAT:
sprintf(buf, "%#f", ap->a_w.w_float);
ep = buf + strlen(buf) - 1;
while (ep > buf && *ep == '0') *ep-- = 0;
break;
case A_SYMBOL:
sp = ap->a_w.w_symbol->s_name;
bp = buf;
ep = buf + (bufsize-5);
while (bp < ep && *sp)
{
if (*sp == ';' || *sp == ',' || *sp == '\\' ||
(*sp == '$' && bp == buf && sp[1] >= '0' && sp[1] <= '9'))
*bp++ = '\\';
if ((unsigned char)*sp < 127)
*bp++ = *sp++;
else
/* FIXME this is temporary -- codepage horror */
sprintf(bp, "\\%.3o", (unsigned char)*sp++), bp += 4;
}
if (*sp) *bp++ = '*';
*bp = 0;
break;
case A_DOLLAR:
sprintf(buf, "$%d", ap->a_w.w_index);
break;
case A_DOLLSYM:
sprintf(buf, "$%s", ap->a_w.w_symbol->s_name);
break;
default:
binport_bug("bad atom type");
strcpy(buf, "???");
}
}
static void binport_print(t_binport *bp, FILE *fp)
{
char buf[BINPORT_MAXSTRING];
t_atom at;
int cnt = 0;
if (bp->b_old)
bp->b_old->o_ndx = 0;
while (binport_nextatom(bp, &at))
{
if (at.a_type == A_SEMI)
{
fputs(";\n", fp);
cnt = 0;
}
else if (at.a_type != A_NULL)
{
if (cnt++) fputc(' ', fp);
binport_atomstring(&at, buf, BINPORT_MAXSTRING);
fputs(buf, fp);
}
}
}
#ifndef BINPORT_STANDALONE
static int binport_tobinbuf(t_binport *bp, t_binbuf *bb)
{
t_atom at;
if (bp->b_old)
bp->b_old->o_ndx = 0;
while (binport_nextatom(bp, &at))
if (at.a_type != A_NULL)
binbuf_add(bb, 1, &at);
return (1);
}
/* LATER deal with corrupt binary files? */
int binport_read(t_binbuf *bb, char *filename, char *dirname)
{
int result;
FILE *fp;
char namebuf[MAXPDSTRING];
namebuf[0] = 0;
if (*dirname)
strcat(namebuf, dirname), strcat(namebuf, "/");
strcat(namebuf, filename);
sys_bashfilename(namebuf, namebuf);
if (fp = fopen(namebuf, "rb"))
{
int ftype;
t_binport *bp = binport_new(fp, &ftype);
if (bp)
{
if (ftype == BINPORT_OK)
result = (binport_tobinbuf(bp, bb)
? BINPORT_OK : BINPORT_CORRUPT);
else if (ftype == BINPORT_MAXTEXT)
{
t_atom at;
while (binport_nextatom(bp, &at))
if (at.a_type == A_SEMI)
break;
binbuf_addv(bb, "ss;", gensym("max"), gensym("v2"));
result = (binport_tobinbuf(bp, bb)
? BINPORT_OK : BINPORT_CORRUPT);
}
else if (ftype == BINPORT_MAXOLD)
{
t_binpold *old = binpold_new(fp);
result = BINPORT_FAILED;
if (old)
{
bp->b_old = old;
if (binpold_load(old) && binport_tobinbuf(bp, bb))
result = BINPORT_OK;
}
else binpold_failure(filename);
}
else result = BINPORT_FAILED;
binport_free(bp);
}
else if (ftype == BINPORT_PDFILE)
result = (binbuf_read(bb, filename, dirname, 0)
? BINPORT_FAILED : BINPORT_PDFILE);
else
{
binport_failure(filename);
result = BINPORT_INVALID;
}
}
else
{
binport_bug("cannot open file");
result = BINPORT_FAILED;
}
return (result);
}
/* save as MAXTEXT */
void binport_write(t_binbuf *bb, char *filename, char *dirname)
{
int result;
FILE *fp;
char namebuf[MAXPDSTRING];
namebuf[0] = 0;
if (*dirname)
strcat(namebuf, dirname), strcat(namebuf, "/");
strcat(namebuf, filename);
sys_bashfilename(namebuf, namebuf);
if (fp = fopen(namebuf, "w"))
{
char buf[BINPORT_MAXSTRING];
t_atom *ap = binbuf_getvec(bb);
int cnt = 0, ac = binbuf_getnatom(bb);
while (ac--)
{
if (ap->a_type == A_SEMI)
{
fputs(";\n", fp);
cnt = 0;
}
else if (ap->a_type != A_NULL)
{
if (cnt++) fputc(' ', fp);
binport_atomstring(ap, buf, BINPORT_MAXSTRING);
fputs(buf, fp);
}
ap++;
}
fclose(fp);
}
}
#else
int main(int ac, char **av)
{
if (ac > 1)
{
FILE *fp = fopen(av[1], "rb");
if (fp)
{
int ftype;
t_binport *bp = binport_new(fp, &ftype);
if (bp)
{
if (ftype == BINPORT_OK)
binport_print(bp, stdout);
else if (ftype == BINPORT_MAXTEXT)
binport_warning("\"%s\" looks like a Max text file", av[1]);
else if (ftype == BINPORT_MAXOLD)
{
t_binpold *old = binpold_new(fp);
if (old)
{
bp->b_old = old;
if (binpold_load(old))
binport_print(bp, stdout);
else
ftype = BINPORT_FAILED;
}
else ftype = BINPORT_FAILED;
if (ftype == BINPORT_FAILED) binpold_failure(av[1]);
}
binport_free(bp);
}
else if (ftype == BINPORT_PDFILE)
binport_warning("\"%s\" looks like a Pd patch file", av[1]);
else
binport_failure(av[1]);
}
else binport_error("cannot open file \"%s\"", av[1]);
}
else binport_error("what file?");
return (0);
}
#endif
|