forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emb-file.c
1338 lines (1246 loc) · 39 KB
/
emb-file.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
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
/**
Type of sector
*/
#define CompoundFileSector_MaxRegSector 0xFFFFFFFA
#define CompoundFileSector_DIFAT_Sector 0xFFFFFFFC
#define CompoundFileSector_FAT_Sector 0xFFFFFFFD
#define CompoundFileSector_EndOfChain 0xFFFFFFFE
#define CompoundFileSector_FreeSector 0xFFFFFFFF
/**
Type of directory object
*/
#define ObjectTypeUnknown 0x00 /*!< Probably unallocated */
#define ObjectTypeStorage 0x01 /*!< a directory type object */
#define ObjectTypeStream 0x02 /*!< a file type object */
#define ObjectTypeRootEntry 0x05 /*!< the root entry */
/**
Special values for Stream Identifiers
*/
#define CompoundFileStreamId_MaxRegularStreamId 0xFFFFFFFA /*!< All real stream Ids are less than this */
#define CompoundFileStreamId_NoStream 0xFFFFFFFF /*!< There is no valid stream Id */
/* TODO: This list needs reviewed in case some stitch formats also can contain object data (EMBFORMAT_STCHANDOBJ). */
const EmbFormatList formatTable[100] = {
{ ".10o", "Toyota Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".100", "Toyota Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".art", "Bernina Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".bmc", "Bitmap Cache Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".bro", "Bits & Volts Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".cnd", "Melco Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".col", "Embroidery Thread Color Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".csd", "Singer Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".csv", "Comma Separated Values Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".dat", "Barudan Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".dem", "Melco Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".dsb", "Barudan Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".dst", "Tajima Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".dsz", "ZSK USA Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".dxf", "Drawing Exchange Format", ' ', ' ', EMBFORMAT_OBJECTONLY },
{ ".edr", "Embird Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".emd", "Elna Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".exp", "Melco Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".exy", "Eltac Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".eys", "Sierra Expanded Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".fxy", "Fortron Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".gc", "Smoothie G-Code Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".gnc", "Great Notions Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".gt", "Gold Thread Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".hus", "Husqvarna Viking Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".inb", "Inbro Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".inf", "Embroidery Color Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".jef", "Janome Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".ksm", "Pfaff Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".max", "Pfaff Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".mit", "Mitsubishi Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".new", "Ameco Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".ofm", "Melco Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".pcd", "Pfaff Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".pcm", "Pfaff Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".pcq", "Pfaff Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".pcs", "Pfaff Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".pec", "Brother Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".pel", "Brother Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".pem", "Brother Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".pes", "Brother Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".phb", "Brother Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".phc", "Brother Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".plt", "AutoCAD Plot Drawing Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".rgb", "RGB Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".sew", "Janome Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".shv", "Husqvarna Viking Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".sst", "Sunstar Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".stx", "Data Stitch Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".svg", "Scalable Vector Graphics", 'U', 'U', EMBFORMAT_OBJECTONLY },
{ ".t01", "Pfaff Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".t09", "Pfaff Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".tap", "Happy Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".thr", "ThredWorks Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".txt", "Text File", ' ', 'U', EMBFORMAT_STITCHONLY },
{ ".u00", "Barudan Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".u01", "Barudan Embroidery Format", ' ', ' ', EMBFORMAT_STITCHONLY },
{ ".vip", "Pfaff Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ ".vp3", "Pfaff Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".xxx", "Singer Embroidery Format", 'U', 'U', EMBFORMAT_STITCHONLY },
{ ".zsk", "ZSK USA Embroidery Format", 'U', ' ', EMBFORMAT_STITCHONLY },
{ "END", "END", ' ', ' ', 0 }
};
static bcf_file_difat* bcf_difat_create(EmbFile* file, unsigned int fatSectors, const unsigned int sectorSize);
static unsigned int readFullSector(EmbFile* file, bcf_file_difat* bcfFile, unsigned int* numberOfDifatEntriesStillToRead);
static unsigned int numberOfEntriesInDifatSector(bcf_file_difat* fat);
static void bcf_file_difat_free(bcf_file_difat* difat);
static bcf_file_fat* bcfFileFat_create(const unsigned int sectorSize);
static void loadFatFromSector(bcf_file_fat* fat, EmbFile* file);
static void bcf_file_fat_free(bcf_file_fat* fat);
static bcf_directory_entry* CompoundFileDirectoryEntry(EmbFile* file);
static bcf_directory* CompoundFileDirectory(const unsigned int maxNumberOfDirectoryEntries);
static void readNextSector(EmbFile* file, bcf_directory* dir);
static void bcf_directory_free(bcf_directory* dir);
static bcf_file_header bcfFileHeader_read(EmbFile* file);
static int bcfFileHeader_isValid(bcf_file_header header);
int numberOfFormats = 61;
/*! Constant representing the number of Double Indirect FAT entries in a single header */
static const unsigned int NumberOfDifatEntriesInHeader = 109;
static const unsigned int sizeOfFatEntry = sizeof(unsigned int);
static const unsigned int sizeOfDifatEntry = 4;
static const unsigned int sizeOfChainingEntryAtEndOfDifatSector = 4;
static const unsigned int sizeOfDirectoryEntry = 128;
/* static const int supportedMinorVersion = 0x003E;
static const int littleEndianByteOrderMark = 0xFFFE; */
int embPattern_write(EmbPattern* pattern, const char* fileName, int format)
{
EmbFile *file;
int r;
if (!fileName) {
embLog("ERROR: ");
embLog(fileName);
embLog("(), fileName argument is null\n");
return 0;
}
if (!pattern->stitchList) {
embLog("ERROR: embPattern_write(), pattern contains no stitches\n");
return 0;
}
if (!pattern->stitchList->count) {
embLog("ERROR: embPattern_write(), pattern contains no stitches\n");
return 0;
}
embPattern_end(pattern);
file = embFile_open(fileName, "wb", 0);
if (!file) {
embLog("ERROR: embPattern_write(), failed to open file\n");
return 0;
}
r = 0;
switch (format) {
case EMB_FORMAT_100:
r = write100(pattern, file, fileName);
break;
case EMB_FORMAT_10O:
r = write10o(pattern, file, fileName);
break;
case EMB_FORMAT_ART:
r = writeArt(pattern, file, fileName);
break;
case EMB_FORMAT_BMC:
r = writeBmc(pattern, file, fileName);
break;
case EMB_FORMAT_BRO:
r = writeBro(pattern, file, fileName);
break;
case EMB_FORMAT_CND:
r = writeCnd(pattern, file, fileName);
break;
case EMB_FORMAT_COL:
r = writeCol(pattern, file, fileName);
break;
case EMB_FORMAT_CSD:
r = writeCsd(pattern, file, fileName);
break;
case EMB_FORMAT_CSV:
r = writeCsv(pattern, file, fileName);
break;
case EMB_FORMAT_DAT:
r = writeDat(pattern, file, fileName);
break;
case EMB_FORMAT_DEM:
r = writeArt(pattern, file, fileName);
break;
case EMB_FORMAT_DSB:
r = writeDsb(pattern, file, fileName);
break;
case EMB_FORMAT_DST:
r = writeDst(pattern, file, fileName);
break;
case EMB_FORMAT_DSZ:
r = writeDsz(pattern, file, fileName);
break;
case EMB_FORMAT_DXF:
r = writeDxf(pattern, file, fileName);
break;
case EMB_FORMAT_EDR:
r = writeEdr(pattern, file, fileName);
break;
case EMB_FORMAT_EMD:
r = writeEmd(pattern, file, fileName);
break;
case EMB_FORMAT_EXP:
r = writeExp(pattern, file, fileName);
break;
case EMB_FORMAT_EXY:
r = writeExy(pattern, file, fileName);
break;
case EMB_FORMAT_EYS:
r = writeEys(pattern, file, fileName);
break;
case EMB_FORMAT_FXY:
r = writeFxy(pattern, file, fileName);
break;
case EMB_FORMAT_GC:
r = writeGc(pattern, file, fileName);
break;
case EMB_FORMAT_GNC:
r = writeGnc(pattern, file, fileName);
break;
case EMB_FORMAT_GT:
r = writeGt(pattern, file, fileName);
break;
case EMB_FORMAT_HUS:
r = writeHus(pattern, file, fileName);
break;
case EMB_FORMAT_INB:
r = writeInb(pattern, file, fileName);
break;
case EMB_FORMAT_INF:
r = writeInf(pattern, file, fileName);
break;
case EMB_FORMAT_JEF:
r = writeJef(pattern, file, fileName);
break;
case EMB_FORMAT_KSM:
r = writeKsm(pattern, file, fileName);
break;
case EMB_FORMAT_MAX:
r = writeMax(pattern, file, fileName);
break;
case EMB_FORMAT_MIT:
r = writeMit(pattern, file, fileName);
break;
case EMB_FORMAT_NEW:
r = writeNew(pattern, file, fileName);
break;
case EMB_FORMAT_OFM:
r = writeOfm(pattern, file, fileName);
break;
case EMB_FORMAT_PCD:
r = writePcd(pattern, file, fileName);
break;
case EMB_FORMAT_PCM:
r = writePcm(pattern, file, fileName);
break;
case EMB_FORMAT_PCQ:
r = writePcq(pattern, file, fileName);
break;
case EMB_FORMAT_PCS:
r = writePcs(pattern, file, fileName);
break;
case EMB_FORMAT_PEC:
r = writePec(pattern, file, fileName);
break;
case EMB_FORMAT_PEL:
r = writePel(pattern, file, fileName);
break;
case EMB_FORMAT_PEM:
r = writePem(pattern, file, fileName);
break;
case EMB_FORMAT_PES:
r = writePes(pattern, file, fileName);
break;
case EMB_FORMAT_PHB:
r = writePhb(pattern, file, fileName);
break;
case EMB_FORMAT_PHC:
r = writePhc(pattern, file, fileName);
break;
case EMB_FORMAT_PLT:
r = writePlt(pattern, file, fileName);
break;
case EMB_FORMAT_RGB:
r = writeRgb(pattern, file, fileName);
break;
case EMB_FORMAT_SEW:
r = writeSew(pattern, file, fileName);
break;
case EMB_FORMAT_SHV:
r = writeShv(pattern, file, fileName);
break;
case EMB_FORMAT_SST:
r = writeSst(pattern, file, fileName);
break;
case EMB_FORMAT_STX:
r = writeStx(pattern, file, fileName);
break;
case EMB_FORMAT_SVG:
r = writeSvg(pattern, file, fileName);
break;
case EMB_FORMAT_T01:
r = writeT01(pattern, file, fileName);
break;
case EMB_FORMAT_T09:
r = writeT09(pattern, file, fileName);
break;
case EMB_FORMAT_TAP:
r = writeTap(pattern, file, fileName);
break;
case EMB_FORMAT_THR:
r = writeThr(pattern, file, fileName);
break;
case EMB_FORMAT_TXT:
r = writeTxt(pattern, file, fileName);
break;
case EMB_FORMAT_U00:
r = writeU00(pattern, file, fileName);
break;
case EMB_FORMAT_U01:
r = writeU01(pattern, file, fileName);
break;
case EMB_FORMAT_VIP:
r = writeVip(pattern, file, fileName);
break;
case EMB_FORMAT_VP3:
r = writeVp3(pattern, file, fileName);
break;
case EMB_FORMAT_XXX:
r = writeXxx(pattern, file, fileName);
break;
case EMB_FORMAT_ZSK:
r = writeZsk(pattern, file, fileName);
break;
default:
embLog("Emb format not recognised.");
break;
}
if (!r) {
return r;
}
embFile_close(file);
return 0;
}
/**
* Argument validator for reading patterns.
*/
int embPattern_read(EmbPattern* pattern, const char* fileName, int format)
{
EmbFile *file;
int r;
if (!pattern) {
embLog("ERROR: embPattern_read(), pattern argument is null\n");
return 0;
}
if (!fileName) {
embLog("ERROR: embPattern_read(), fileName argument is null\n");
return 0;
}
file = embFile_open(fileName, "rb", 0);
if (!file) {
embLog("ERROR: embPattern_write(), failed to open file\n");
return 0;
}
r = 0;
switch (format) {
case EMB_FORMAT_100:
r = read100(pattern, file, fileName);
break;
case EMB_FORMAT_10O:
r = read10o(pattern, file, fileName);
break;
case EMB_FORMAT_ART:
r = readArt(pattern, file, fileName);
break;
case EMB_FORMAT_BMC:
r = readBmc(pattern, file, fileName);
break;
case EMB_FORMAT_BRO:
r = readBro(pattern, file, fileName);
break;
case EMB_FORMAT_CND:
r = readCnd(pattern, file, fileName);
break;
case EMB_FORMAT_COL:
r = readCol(pattern, file, fileName);
break;
case EMB_FORMAT_CSD:
r = readCsd(pattern, file, fileName);
break;
case EMB_FORMAT_CSV:
r = readCsv(pattern, file, fileName);
break;
case EMB_FORMAT_DAT:
r = readDat(pattern, file, fileName);
break;
case EMB_FORMAT_DEM:
r = readArt(pattern, file, fileName);
break;
case EMB_FORMAT_DSB:
r = readDsb(pattern, file, fileName);
break;
case EMB_FORMAT_DST:
r = readDst(pattern, file, fileName);
break;
case EMB_FORMAT_DSZ:
r = readDsz(pattern, file, fileName);
break;
case EMB_FORMAT_DXF:
r = readDxf(pattern, file, fileName);
break;
case EMB_FORMAT_EDR:
r = readEdr(pattern, file, fileName);
break;
case EMB_FORMAT_EMD:
r = readEmd(pattern, file, fileName);
break;
case EMB_FORMAT_EXP:
r = readExp(pattern, file, fileName);
break;
case EMB_FORMAT_EXY:
r = readExy(pattern, file, fileName);
break;
case EMB_FORMAT_EYS:
r = readEys(pattern, file, fileName);
break;
case EMB_FORMAT_FXY:
r = readFxy(pattern, file, fileName);
break;
case EMB_FORMAT_GC:
r = readGc(pattern, file, fileName);
break;
case EMB_FORMAT_GNC:
r = readGnc(pattern, file, fileName);
break;
case EMB_FORMAT_GT:
r = readGt(pattern, file, fileName);
break;
case EMB_FORMAT_HUS:
r = readHus(pattern, file, fileName);
break;
case EMB_FORMAT_INB:
r = readInb(pattern, file, fileName);
break;
case EMB_FORMAT_INF:
r = readInf(pattern, file, fileName);
break;
case EMB_FORMAT_JEF:
r = readJef(pattern, file, fileName);
break;
case EMB_FORMAT_KSM:
r = readKsm(pattern, file, fileName);
break;
case EMB_FORMAT_MAX:
r = readMax(pattern, file, fileName);
break;
case EMB_FORMAT_MIT:
r = readMit(pattern, file, fileName);
break;
case EMB_FORMAT_NEW:
r = readNew(pattern, file, fileName);
break;
case EMB_FORMAT_OFM:
r = readOfm(pattern, file, fileName);
break;
case EMB_FORMAT_PCD:
r = readPcd(pattern, file, fileName);
break;
case EMB_FORMAT_PCM:
r = readPcm(pattern, file, fileName);
break;
case EMB_FORMAT_PCQ:
r = readPcq(pattern, file, fileName);
break;
case EMB_FORMAT_PCS:
r = readPcs(pattern, file, fileName);
break;
case EMB_FORMAT_PEC:
r = readPec(pattern, file, fileName);
break;
case EMB_FORMAT_PEL:
r = readPel(pattern, file, fileName);
break;
case EMB_FORMAT_PEM:
r = readPem(pattern, file, fileName);
break;
case EMB_FORMAT_PES:
r = readPes(pattern, file, fileName);
break;
case EMB_FORMAT_PHB:
r = readPhb(pattern, file, fileName);
break;
case EMB_FORMAT_PHC:
r = readPhc(pattern, file, fileName);
break;
case EMB_FORMAT_PLT:
r = readPlt(pattern, file, fileName);
break;
case EMB_FORMAT_RGB:
r = readRgb(pattern, file, fileName);
break;
case EMB_FORMAT_SEW:
r = readSew(pattern, file, fileName);
break;
case EMB_FORMAT_SHV:
r = readShv(pattern, file, fileName);
break;
case EMB_FORMAT_SST:
r = readSst(pattern, file, fileName);
break;
case EMB_FORMAT_STX:
r = readStx(pattern, file, fileName);
break;
case EMB_FORMAT_SVG:
r = readSvg(pattern, file, fileName);
break;
case EMB_FORMAT_T01:
r = readT01(pattern, file, fileName);
break;
case EMB_FORMAT_T09:
r = readT09(pattern, file, fileName);
break;
case EMB_FORMAT_TAP:
r = readTap(pattern, file, fileName);
break;
case EMB_FORMAT_THR:
r = readThr(pattern, file, fileName);
break;
case EMB_FORMAT_TXT:
r = readTxt(pattern, file, fileName);
break;
case EMB_FORMAT_U00:
r = readU00(pattern, file, fileName);
break;
case EMB_FORMAT_U01:
r = readU01(pattern, file, fileName);
break;
case EMB_FORMAT_VIP:
r = readVip(pattern, file, fileName);
break;
case EMB_FORMAT_VP3:
r = readVp3(pattern, file, fileName);
break;
case EMB_FORMAT_XXX:
r = readXxx(pattern, file, fileName);
break;
case EMB_FORMAT_ZSK:
r = readZsk(pattern, file, fileName);
break;
default:
embLog("Emb format not recognised.");
break;
}
if (!r) {
return r;
}
embFile_close(file);
embPattern_end(pattern);
return 1;
}
/**
* TODO: documentation.
*/
EmbFile* embFile_open(const char* fileName, const char* mode, int optional)
{
#ifdef ARDUINO
return inoFile_open(fileName, mode);
#else
EmbFile* eFile = 0;
FILE* oFile = fopen(fileName, mode);
if (!oFile) {
if (!optional) {
embLog("ERROR: Cannot open ");
embLog(fileName);
embLog(" in mode ");
embLog(mode);
embLog(".");
}
return 0;
}
eFile = (EmbFile*)malloc(sizeof(EmbFile));
if (!eFile) {
if (!optional) {
embLog("ERROR: Cannot allocate memory for EmbFile with arguments (");
embLog(fileName);
embLog(", ");
embLog(mode);
embLog(", 0).");
}
fclose(oFile);
return 0;
}
eFile->file = oFile;
return eFile;
#endif
}
/**
* TODO: documentation.
*/
int embFile_close(EmbFile* stream)
{
#ifdef ARDUINO
return inoFile_close(stream);
#else /* ARDUINO */
int retVal = fclose(stream->file);
free(stream);
return retVal;
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
int embFile_eof(EmbFile* stream)
{
#ifdef ARDUINO
return inoFile_eof(stream);
#else /* ARDUINO */
return feof(stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
char embFile_getc(EmbFile* stream)
{
#ifdef ARDUINO
return inoFile_getc(stream);
#else /* ARDUINO */
return fgetc(stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
void embFile_readline(EmbFile* stream, char* line, int maxLength)
{
int i;
char c;
for (i = 0; i < maxLength - 1; i++) {
c = embFile_getc(stream);
if (embFile_eof(stream)) {
break;
}
if (c == '\r') {
c = embFile_getc(stream);
if (c != '\n') {
embFile_seek(stream, -1L, SEEK_CUR);
}
break;
}
if (c == '\n') {
break;
}
*line = c;
line++;
}
*line = 0;
}
/**
* In order to remove uses of printf (which use more memory and variable
* arguments which is bad for embedded systems) call this instead whenever
* possible.
*
* Note that fputs does not append a \n like puts does.
*/
int embFile_puts(EmbFile* stream, char* buff)
{
#ifdef ARDUINO
return inoFile_printf(stream, buff);
#else /* ARDUINO */
return fputs(buff, stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
size_t embFile_read(void* ptr, size_t size, size_t nmemb, EmbFile* stream)
{
#ifdef ARDUINO
/* ARDUINO TODO: SD File read() doesn't appear to return the same way as fread(). This will need work. */
return 0;
#else /* ARDUINO */
return fread(ptr, size, nmemb, stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
size_t embFile_write(const void* ptr, size_t size, size_t nmemb, EmbFile* stream)
{
#ifdef ARDUINO
return 0; /* ARDUINO TODO: Implement inoFile_write. */
#else /* ARDUINO */
return fwrite(ptr, size, nmemb, stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
void embFile_pad(EmbFile* stream, char c, int n)
{
int i;
for (i=0; i<n; i++) {
embFile_write(&c, 1, 1, stream);
}
}
/**
* TODO: documentation.
*/
int embFile_seek(EmbFile* stream, long offset, int origin)
{
#ifdef ARDUINO
return inoFile_seek(stream, offset, origin);
#else /* ARDUINO */
return fseek(stream->file, offset, origin);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
long embFile_tell(EmbFile* stream)
{
#ifdef ARDUINO
return inoFile_tell(stream);
#else /* ARDUINO */
return ftell(stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
EmbFile* embFile_tmpfile(void)
{
#ifdef ARDUINO
return inoFile_tmpfile();
#else
EmbFile* eFile = 0;
FILE* tFile = tmpfile();
if (!tFile)
return 0;
eFile = (EmbFile*)malloc(sizeof(EmbFile));
if (!eFile) {
fclose(tFile);
return 0;
}
eFile->file = tFile;
return eFile;
#endif
}
/**
* TODO: documentation.
*/
int embFile_putc(int ch, EmbFile* stream)
{
#ifdef ARDUINO
return inoFile_putc(ch, stream);
#else /* ARDUINO */
return fputc(ch, stream->file);
#endif /* ARDUINO */
}
/**
* TODO: documentation.
*/
void embFile_print(EmbFile* stream, const char* str)
{
embFile_write(str, 1, strlen(str), stream);
}
/**
* TODO: documentation.
*/
static unsigned int sectorSize(bcf_file* bcfFile)
{
/* version 3 uses 512 byte */
if (bcfFile->header.majorVersion == 3) {
return 512;
} else if (bcfFile->header.majorVersion == 4) {
return 4096;
}
return 4096;
}
/**
* TODO: documentation.
*/
static int haveExtraDIFATSectors(bcf_file* file)
{
return (int)(numberOfEntriesInDifatSector(file->difat) > 0);
}
/**
* TODO: documentation.
*/
static int seekToOffset(EmbFile* file, const unsigned int offset)
{
return embFile_seek(file, offset, SEEK_SET);
}
/**
* TODO: documentation.
*/
static int seekToSector(bcf_file* bcfFile, EmbFile* file, const unsigned int sector)
{
unsigned int offset = sector * sectorSize(bcfFile) + sectorSize(bcfFile);
return seekToOffset(file, offset);
}
/**
* TODO: documentation.
*/
static void parseDIFATSectors(EmbFile* file, bcf_file* bcfFile)
{
unsigned int numberOfDifatEntriesStillToRead = bcfFile->header.numberOfFATSectors - NumberOfDifatEntriesInHeader;
unsigned int difatSectorNumber = bcfFile->header.firstDifatSectorLocation;
while ((difatSectorNumber != CompoundFileSector_EndOfChain) && (numberOfDifatEntriesStillToRead > 0)) {
seekToSector(bcfFile, file, difatSectorNumber);
difatSectorNumber = readFullSector(file, bcfFile->difat, &numberOfDifatEntriesStillToRead);
}
}
/**
* TODO: documentation.
*/
int bcfFile_read(EmbFile* file, bcf_file* bcfFile)
{
unsigned int i, numberOfDirectoryEntriesPerSector, directorySectorToReadFrom;
bcfFile->header = bcfFileHeader_read(file);
if (!bcfFileHeader_isValid(bcfFile->header)) {
embLog("Failed to parse header\n");
return 0;
}
bcfFile->difat = bcf_difat_create(file, bcfFile->header.numberOfFATSectors, sectorSize(bcfFile));
if (haveExtraDIFATSectors(bcfFile)) {
parseDIFATSectors(file, bcfFile);
}
bcfFile->fat = bcfFileFat_create(sectorSize(bcfFile));
for (i = 0; i < bcfFile->header.numberOfFATSectors; ++i) {
unsigned int fatSectorNumber = bcfFile->difat->fatSectorEntries[i];
seekToSector(bcfFile, file, fatSectorNumber);
loadFatFromSector(bcfFile->fat, file);
}
numberOfDirectoryEntriesPerSector = sectorSize(bcfFile) / sizeOfDirectoryEntry;
bcfFile->directory = CompoundFileDirectory(numberOfDirectoryEntriesPerSector);
directorySectorToReadFrom = bcfFile->header.firstDirectorySectorLocation;
while (directorySectorToReadFrom != CompoundFileSector_EndOfChain) {
seekToSector(bcfFile, file, directorySectorToReadFrom);
readNextSector(file, bcfFile->directory);
directorySectorToReadFrom = bcfFile->fat->fatEntries[directorySectorToReadFrom];
}
return 1;
}
/**
* TODO: documentation.
*/
EmbFile* GetFile(bcf_file* bcfFile, EmbFile* file, char* fileToFind)
{
int filesize, sectorSize, currentSector, sizeToWrite, currentSize, totalSectors, i;
char* input = 0;
EmbFile* fileOut = embFile_tmpfile();
bcf_directory_entry* pointer = bcfFile->directory->dirEntries;
while (pointer) {
if (strcmp(fileToFind, pointer->directoryEntryName) == 0)
break;
pointer = pointer->next;
}
filesize = pointer->streamSize;
sectorSize = bcfFile->difat->sectorSize;
input = (char*)malloc(sectorSize);
if (!input) {
embLog("ERROR: compound-file.c GetFile(), cannot allocate memory for input\n");
} /* TODO: avoid crashing. null pointer will be accessed */
currentSize = 0;
currentSector = pointer->startingSectorLocation;
totalSectors = (int)ceil((float)filesize / sectorSize);
for (i = 0; i < totalSectors; i++) {
seekToSector(bcfFile, file, currentSector);
sizeToWrite = filesize - currentSize;
if (sectorSize < sizeToWrite) {
sizeToWrite = sectorSize;
}
embFile_read(input, 1, sizeToWrite, file);
embFile_write(input, 1, sizeToWrite, fileOut);
currentSize += sizeToWrite;
currentSector = bcfFile->fat->fatEntries[currentSector];
}
free(input);
input = 0;
return fileOut;
}
/**
* TODO: documentation.
*/
void bcf_file_free(bcf_file* bcfFile)
{
bcf_file_difat_free(bcfFile->difat);
bcfFile->difat = 0;
bcf_file_fat_free(bcfFile->fat);
bcfFile->fat = 0;
bcf_directory_free(bcfFile->directory);
bcfFile->directory = 0;
free(bcfFile);
bcfFile = 0;
}
/**
* TODO: documentation.
*/
bcf_file_difat* bcf_difat_create(EmbFile* file, unsigned int fatSectors, const unsigned int sectorSize)
{
unsigned int i;
bcf_file_difat* difat = 0;
unsigned int sectorRef;
difat = (bcf_file_difat*)malloc(sizeof(bcf_file_difat));
if (!difat) {
embLog("ERROR: compound-file-difat.c bcf_difat_create(), cannot allocate memory for difat\n");
} /* TODO: avoid crashing. null pointer will be accessed */
difat->sectorSize = sectorSize;
if (fatSectors > NumberOfDifatEntriesInHeader) {
fatSectors = NumberOfDifatEntriesInHeader;
}
for (i = 0; i < fatSectors; ++i) {
sectorRef = binaryReadUInt32(file);
difat->fatSectorEntries[i] = sectorRef;
}
difat->fatSectorCount = fatSectors;
for (i = fatSectors; i < NumberOfDifatEntriesInHeader; ++i) {
sectorRef = binaryReadUInt32(file);
if (sectorRef != CompoundFileSector_FreeSector) {
embLog("ERROR: compound-file-difat.c bcf_difat_create(), Unexpected sector value\n");
/* TODO " %x at DIFAT[%d]\n", sectorRef, i); */
}
}
return difat;
}
/**
* TODO: documentation.
*/
unsigned int numberOfEntriesInDifatSector(bcf_file_difat* fat)
{
return (fat->sectorSize - sizeOfChainingEntryAtEndOfDifatSector) / sizeOfDifatEntry;
}
/**
* TODO: documentation.
*/
unsigned int readFullSector(EmbFile* file, bcf_file_difat* bcfFile, unsigned int* numberOfDifatEntriesStillToRead)
{
unsigned int i;
unsigned int sectorRef;
unsigned int nextDifatSectorInChain;
unsigned int entriesToReadInThisSector = 0;
if (*numberOfDifatEntriesStillToRead > numberOfEntriesInDifatSector(bcfFile)) {
entriesToReadInThisSector = numberOfEntriesInDifatSector(bcfFile);
*numberOfDifatEntriesStillToRead -= entriesToReadInThisSector;
} else {
entriesToReadInThisSector = *numberOfDifatEntriesStillToRead;
*numberOfDifatEntriesStillToRead = 0;
}
for (i = 0; i < entriesToReadInThisSector; ++i) {
sectorRef = binaryReadUInt32(file);
bcfFile->fatSectorEntries[bcfFile->fatSectorCount] = sectorRef;
bcfFile->fatSectorCount++;