-
Notifications
You must be signed in to change notification settings - Fork 2
/
udf_core.c
5113 lines (4223 loc) · 132 KB
/
udf_core.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
/* $NetBSD: udf_core.c,v 1.9 2022/04/26 15:11:42 reinoud Exp $ */
/*
* Copyright (c) 2006, 2008, 2021, 2022 Reinoud Zandijk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
//__RCSID("$NetBSD: udf_core.c,v 1.9 2022/04/26 15:11:42 reinoud Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include "util.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include "newfs_udf.h"
#include "unicode.h"
#include "udf_core.h"
/* disk partition support */
#if !HAVE_NBTOOL_CONFIG_H
//#include "../fsck/partutil.h"
//#include "../fsck/partutil.c"
#endif
/* queue for temporary storage of sectors to be written out */
struct wrpacket {
uint64_t start_sectornr;
uint8_t *packet_data;
uint64_t present;
TAILQ_ENTRY(wrpacket) next;
};
/* global variables describing disc and format requests */
struct udf_create_context context;
struct udf_disclayout layout;
int dev_fd_rdonly; /* device: open readonly! */
int dev_fd; /* device: file descriptor */
struct stat dev_fd_stat; /* device: last stat info */
char *dev_name; /* device: name */
int emul_mmc_profile; /* for files */
int emul_packetsize; /* for discs and files */
int emul_sectorsize; /* for files */
off_t emul_size; /* for files */
struct mmc_discinfo mmc_discinfo; /* device: disc info */
union dscrptr *terminator_dscr; /* generic terminator descriptor*/
/* write queue and track blocking skew */
TAILQ_HEAD(wrpacket_list, wrpacket) write_queue;
int write_queuelen;
int write_queue_suspend;
uint32_t wrtrack_skew; /* offset for writing sector0 */
static void udf_init_writequeue(int write_strategy);
static int udf_writeout_writequeue(bool complete);
/*
* NOTE that there is some overlap between this code and the udf kernel fs.
* This is intentionally though it might better be factored out one day.
*/
void
udf_init_create_context(void)
{
/* clear */
memset(&context, 0, sizeof(struct udf_create_context));
/* fill with defaults currently known */
context.dscrver = 3;
context.min_udf = 0x0102;
context.max_udf = 0x0250;
context.serialnum = 1; /* default */
context.gmtoff = 0;
context.meta_perc = UDF_META_PERC;
context.check_surface = 0;
context.create_new_session = 0;
context.sector_size = 512; /* minimum for UDF */
context.media_accesstype = UDF_ACCESSTYPE_NOT_SPECIFIED;
context.format_flags = FORMAT_INVALID;
context.write_strategy = UDF_WRITE_PACKET;
context.logvol_name = NULL;
context.primary_name = NULL;
context.volset_name = NULL;
context.fileset_name = NULL;
/* most basic identification */
context.app_name = "*NetBSD";
context.app_version_main = 0;
context.app_version_sub = 0;
context.impl_name = "*NetBSD";
context.vds_seq = 0; /* first one starts with zero */
/* Minimum value of 16 : UDF 3.2.1.1, 3.3.3.4. */
context.unique_id = 0x10;
context.num_files = 0;
context.num_directories = 0;
context.data_part = 0;
context.metadata_part = 0;
}
/* version can be specified as 0xabc or a.bc */
static int
parse_udfversion(const char *pos, uint32_t *version) {
int hex = 0;
char c1, c2, c3, c4;
*version = 0;
if (*pos == '0') {
pos++;
/* expect hex format */
hex = 1;
if (*pos++ != 'x')
return 1;
}
c1 = *pos++;
if (c1 < '0' || c1 > '9')
return 1;
c1 -= '0';
c2 = *pos++;
if (!hex) {
if (c2 != '.')
return 1;
c2 = *pos++;
}
if (c2 < '0' || c2 > '9')
return 1;
c2 -= '0';
c3 = *pos++;
if (c3 < '0' || c3 > '9')
return 1;
c3 -= '0';
c4 = *pos++;
if (c4 != 0)
return 1;
*version = c1 * 0x100 + c2 * 0x10 + c3;
return 0;
}
/*
* Parse a given string for an udf version.
* May exit.
*/
int
a_udf_version(const char *s, const char *id_type)
{
uint32_t version;
if (parse_udfversion(s, &version))
errx(1, "unknown %s version %s; specify as hex or float", id_type, s);
switch (version) {
case 0x102:
case 0x150:
case 0x200:
case 0x201:
case 0x250:
break;
case 0x260:
/* we don't support this one */
errx(1, "UDF version 0x260 is not supported");
break;
default:
errx(1, "unknown %s version %s, choose from "
"0x102, 0x150, 0x200, 0x201, 0x250",
id_type, s);
}
return version;
}
static uint32_t
udf_space_bitmap_len(uint32_t part_size)
{
return sizeof(struct space_bitmap_desc)-1 +
part_size/8;
}
uint32_t
udf_bytes_to_sectors(uint64_t bytes)
{
uint32_t sector_size = context.sector_size;
return (bytes + sector_size -1) / sector_size;
}
void
udf_dump_layout(void) {
#ifdef DEBUG
int format_flags = context.format_flags;
int sector_size = context.sector_size;
printf("Summary so far\n");
printf("\tiso9660_vrs\t\t%d\n", layout.iso9660_vrs);
printf("\tanchor0\t\t\t%d\n", layout.anchors[0]);
printf("\tanchor1\t\t\t%d\n", layout.anchors[1]);
printf("\tanchor2\t\t\t%d\n", layout.anchors[2]);
printf("\tvds1_size\t\t%d\n", layout.vds1_size);
printf("\tvds2_size\t\t%d\n", layout.vds2_size);
printf("\tvds1\t\t\t%d\n", layout.vds1);
printf("\tvds2\t\t\t%d\n", layout.vds2);
printf("\tlvis_size\t\t%d\n", layout.lvis_size);
printf("\tlvis\t\t\t%d\n", layout.lvis);
if (format_flags & FORMAT_SPAREABLE) {
printf("\tspareable size\t\t%d\n", layout.spareable_area_size);
printf("\tspareable\t\t%d\n", layout.spareable_area);
}
printf("\tpartition start lba\t%d\n", layout.part_start_lba);
printf("\tpartition size\t\t%ld KiB, %ld MiB\n",
((uint64_t) layout.part_size_lba * sector_size) / 1024,
((uint64_t) layout.part_size_lba * sector_size) / (1024*1024));
if ((format_flags & FORMAT_SEQUENTIAL) == 0) {
printf("\tpart bitmap start\t%d\n", layout.unalloc_space);
printf("\t\tfor %d lba\n", layout.alloc_bitmap_dscr_size);
}
if (format_flags & FORMAT_META) {
printf("\tmeta blockingnr\t\t%d\n", layout.meta_blockingnr);
printf("\tmeta alignment\t\t%d\n", layout.meta_alignment);
printf("\tmeta size\t\t%ld KiB, %ld MiB\n",
((uint64_t) layout.meta_part_size_lba * sector_size) / 1024,
((uint64_t) layout.meta_part_size_lba * sector_size) / (1024*1024));
printf("\tmeta file\t\t%d\n", layout.meta_file);
printf("\tmeta mirror\t\t%d\n", layout.meta_mirror);
printf("\tmeta bitmap\t\t%d\n", layout.meta_bitmap);
printf("\tmeta bitmap start\t%d\n", layout.meta_bitmap_space);
printf("\t\tfor %d lba\n", layout.meta_bitmap_dscr_size);
printf("\tmeta space start\t%d\n", layout.meta_part_start_lba);
printf("\t\tfor %d lba\n", layout.meta_part_size_lba);
}
printf("\n");
#endif
}
int
udf_calculate_disc_layout(int min_udf,
uint32_t first_lba, uint32_t last_lba,
uint32_t sector_size, uint32_t blockingnr)
{
uint64_t kbsize, bytes;
uint32_t spareable_blockingnr;
uint32_t align_blockingnr;
uint32_t pos, mpos;
int format_flags = context.format_flags;
/* clear */
memset(&layout, 0, sizeof(layout));
/* fill with parameters */
layout.wrtrack_skew = wrtrack_skew;
layout.first_lba = first_lba;
layout.last_lba = last_lba;
layout.blockingnr = blockingnr;
layout.spareable_blocks = udf_spareable_blocks();
/* start disc layouting */
/*
* location of iso9660 vrs is defined as first sector AFTER 32kb,
* minimum `sector size' 2048
*/
layout.iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size)
+ first_lba;
/* anchor starts at specified offset in sectors */
layout.anchors[0] = first_lba + 256;
if (format_flags & FORMAT_TRACK512)
layout.anchors[0] = first_lba + 512;
layout.anchors[1] = last_lba - 256;
layout.anchors[2] = last_lba;
/* update workable space */
first_lba = layout.anchors[0] + blockingnr;
last_lba = layout.anchors[1] - 1;
/* XXX rest of anchor packet can be added to unallocated space descr */
/* reserve space for VRS and VRS copy and associated tables */
layout.vds1_size = MAX(16, blockingnr); /* UDF 2.2.3.1+2 */
layout.vds1 = first_lba;
first_lba += layout.vds1_size; /* next packet */
layout.vds2_size = layout.vds1_size;
if (format_flags & FORMAT_SEQUENTIAL) {
/* for sequential, append them ASAP */
layout.vds2 = first_lba;
first_lba += layout.vds2_size;
} else {
layout.vds2 = layout.anchors[1] +1 - layout.vds2_size;
last_lba = layout.vds2 - 1;
}
/*
* Reserve space for logvol integrity sequence, at least 8192 bytes
* for overwritable and rewritable media UDF 2.2.4.6, ECMA 3/10.6.12.
*/
layout.lvis_size = MAX(8192.0/sector_size, 2 * blockingnr);
if (layout.lvis_size * sector_size < 8192)
layout.lvis_size++;
if (format_flags & FORMAT_VAT)
layout.lvis_size = 2;
if (format_flags & FORMAT_WORM)
layout.lvis_size = 64 * blockingnr;
/* TODO skip bad blocks in LVID sequence */
layout.lvis = first_lba;
first_lba += layout.lvis_size;
/* initial guess of UDF partition size */
layout.part_start_lba = first_lba;
layout.part_size_lba = last_lba - layout.part_start_lba;
/* all non sequential media needs an unallocated space bitmap */
layout.alloc_bitmap_dscr_size = 0;
if ((format_flags & (FORMAT_SEQUENTIAL | FORMAT_READONLY)) == 0) {
bytes = udf_space_bitmap_len(layout.part_size_lba);
layout.alloc_bitmap_dscr_size = udf_bytes_to_sectors(bytes);
/* XXX freed space map when applicable */
}
spareable_blockingnr = udf_spareable_blockingnr();
align_blockingnr = blockingnr;
if (format_flags & (FORMAT_SPAREABLE | FORMAT_META))
align_blockingnr = spareable_blockingnr;
layout.align_blockingnr = align_blockingnr;
layout.spareable_blockingnr = spareable_blockingnr;
/*
* Align partition LBA space to blocking granularity. Not strictly
* necessary for non spareables but safer for the VRS data since it is
* updated sporadically
*/
#ifdef DEBUG
printf("Lost %lu slack sectors at start\n", UDF_ROUNDUP(
first_lba, align_blockingnr) -
first_lba);
printf("Lost %lu slack sectors at end\n",
last_lba - UDF_ROUNDDOWN(
last_lba, align_blockingnr));
#endif
first_lba = UDF_ROUNDUP(first_lba, align_blockingnr);
last_lba = UDF_ROUNDDOWN(last_lba, align_blockingnr);
if ((format_flags & FORMAT_SPAREABLE) == 0)
layout.spareable_blocks = 0;
if (format_flags & FORMAT_SPAREABLE) {
layout.spareable_area_size =
layout.spareable_blocks * spareable_blockingnr;
/* a sparing table descriptor is a whole blockingnr sectors */
layout.sparing_table_dscr_lbas = spareable_blockingnr;
/* place the descriptors at the start and end of the area */
layout.spt_1 = first_lba;
first_lba += layout.sparing_table_dscr_lbas;
layout.spt_2 = last_lba - layout.sparing_table_dscr_lbas;
last_lba -= layout.sparing_table_dscr_lbas;
/* allocate spareable section */
layout.spareable_area = first_lba;
first_lba += layout.spareable_area_size;
}
/* update guess of UDF partition size */
layout.part_start_lba = first_lba;
layout.part_size_lba = last_lba - layout.part_start_lba;
/* determine partition selection for data and metadata */
context.data_part = 0;
context.metadata_part = context.data_part;
if ((format_flags & FORMAT_VAT) || (format_flags & FORMAT_META))
context.metadata_part = context.data_part + 1;
context.fids_part = context.metadata_part;
if (format_flags & FORMAT_VAT)
context.fids_part = context.data_part;
/*
* Pick fixed logical space sector numbers for main FSD, rootdir and
* unallocated space. The reason for this pre-allocation is that they
* are referenced in the volume descriptor sequence and hence can't be
* allocated later.
*/
pos = 0;
layout.unalloc_space = pos;
pos += layout.alloc_bitmap_dscr_size;
/* claim metadata descriptors and partition space [UDF 2.2.10] */
if (format_flags & FORMAT_META) {
/* note: all in backing partition space */
layout.meta_file = pos++;
layout.meta_bitmap = 0xffffffff;
if (!(context.format_flags & FORMAT_READONLY))
layout.meta_bitmap = pos++;
layout.meta_mirror = layout.part_size_lba-1;
layout.meta_alignment = MAX(blockingnr, spareable_blockingnr);
layout.meta_blockingnr = MAX(layout.meta_alignment, 32);
/* calculate our partition length and store in sectors */
layout.meta_part_size_lba = layout.part_size_lba *
((float) context.meta_perc / 100.0);
layout.meta_part_size_lba = MAX(layout.meta_part_size_lba, 32);
layout.meta_part_size_lba =
UDF_ROUNDDOWN(layout.meta_part_size_lba, layout.meta_blockingnr);
if (!(context.format_flags & FORMAT_READONLY)) {
/* metadata partition free space bitmap */
bytes = udf_space_bitmap_len(layout.meta_part_size_lba);
layout.meta_bitmap_dscr_size = udf_bytes_to_sectors(bytes);
layout.meta_bitmap_space = pos;
pos += layout.meta_bitmap_dscr_size;
}
layout.meta_part_start_lba = UDF_ROUNDUP(pos, layout.meta_alignment);
pos = layout.meta_part_start_lba + layout.meta_part_size_lba;
}
if (context.metadata_part == context.data_part) {
mpos = pos;
layout.fsd = mpos; mpos += 1;
layout.rootdir = mpos;
pos = mpos;
} else {
mpos = 0;
layout.fsd = mpos; mpos += 1;
layout.rootdir = mpos;
}
/* pos and mpos now refer to the rootdir block */
context.alloc_pos[context.data_part] = pos;
context.alloc_pos[context.metadata_part] = mpos;
udf_dump_layout();
kbsize = (uint64_t) last_lba * sector_size;
printf("Total space on this medium approx. "
"%llu KiB, %llu MiB\n",
kbsize/1024, kbsize/(1024*1024));
kbsize = (uint64_t)(layout.part_size_lba - layout.alloc_bitmap_dscr_size
- layout.meta_bitmap_dscr_size) * sector_size;
printf("Recordable free space on this volume approx. "
"%llu KiB, %llu MiB\n\n",
kbsize/1024, kbsize/(1024*1024));
return 0;
}
/*
* Check if the blob starts with a good UDF tag. Tags are protected by a
* checksum over the header, except one byte at position 4 that is the
* checksum itself.
*/
int
udf_check_tag(void *blob)
{
struct desc_tag *tag = blob;
uint8_t *pos, sum, cnt;
/* check TAG header checksum */
pos = (uint8_t *) tag;
sum = 0;
for(cnt = 0; cnt < 16; cnt++) {
if (cnt != 4)
sum += *pos;
pos++;
}
if (sum != tag->cksum) {
/* bad tag header checksum; this is not a valid tag */
return EINVAL;
}
return 0;
}
/*
* check tag payload will check descriptor CRC as specified.
* If the descriptor is too long, it will return EIO otherwise EINVAL.
*/
int
udf_check_tag_payload(void *blob, uint32_t max_length)
{
struct desc_tag *tag = blob;
uint16_t crc, crc_len;
crc_len = udf_rw16(tag->desc_crc_len);
/* check payload CRC if applicable */
if (crc_len == 0)
return 0;
if (crc_len > max_length)
return EIO;
crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
if (crc != udf_rw16(tag->desc_crc)) {
/* bad payload CRC; this is a broken tag */
return EINVAL;
}
return 0;
}
int
udf_check_tag_and_location(void *blob, uint32_t location)
{
struct desc_tag *tag = blob;
if (udf_check_tag(blob))
return 1;
if (udf_rw32(tag->tag_loc) != location)
return 1;
return 0;
}
int
udf_validate_tag_sum(union dscrptr *dscr)
{
struct desc_tag *tag = &dscr->tag;
uint8_t *pos, sum, cnt;
/* calculate TAG header checksum */
pos = (uint8_t *) tag;
sum = 0;
for (cnt = 0; cnt < 16; cnt++) {
if (cnt != 4) sum += *pos;
pos++;
};
tag->cksum = sum; /* 8 bit */
return 0;
}
/* assumes sector number of descriptor to be already present */
int
udf_validate_tag_and_crc_sums(union dscrptr *dscr)
{
struct desc_tag *tag = &dscr->tag;
uint16_t crc;
/* check payload CRC if applicable */
if (udf_rw16(tag->desc_crc_len) > 0) {
crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH,
udf_rw16(tag->desc_crc_len));
tag->desc_crc = udf_rw16(crc);
};
/* calculate TAG header checksum */
return udf_validate_tag_sum(dscr);
}
void
udf_inittag(struct desc_tag *tag, int tagid, uint32_t loc)
{
tag->id = udf_rw16(tagid);
tag->descriptor_ver = udf_rw16(context.dscrver);
tag->cksum = 0;
tag->reserved = 0;
tag->serial_num = udf_rw16(context.serialnum);
tag->tag_loc = udf_rw32(loc);
}
int
udf_create_anchor(int num)
{
struct anchor_vdp *avdp;
uint32_t vds1_extent_len = layout.vds1_size * context.sector_size;
uint32_t vds2_extent_len = layout.vds2_size * context.sector_size;
avdp = context.anchors[num];
if (!avdp)
if ((avdp = calloc(1, context.sector_size)) == NULL)
return ENOMEM;
udf_inittag(&avdp->tag, TAGID_ANCHOR, layout.anchors[num]);
avdp->main_vds_ex.loc = udf_rw32(layout.vds1);
avdp->main_vds_ex.len = udf_rw32(vds1_extent_len);
avdp->reserve_vds_ex.loc = udf_rw32(layout.vds2);
avdp->reserve_vds_ex.len = udf_rw32(vds2_extent_len);
/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
avdp->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
context.anchors[num] = avdp;
return 0;
}
void
udf_create_terminator(union dscrptr *dscr, uint32_t loc)
{
memset(dscr, 0, context.sector_size);
udf_inittag(&dscr->tag, TAGID_TERM, loc);
/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
}
void
udf_osta_charset(struct charspec *charspec)
{
memset(charspec, 0, sizeof(*charspec));
charspec->type = 0;
strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
}
/* ---- shared from kernel's udf_subr.c, slightly modified ---- */
void
udf_to_unix_name(char *result, int result_len, char *id, int len,
struct charspec *chsp)
{
uint16_t *raw_name, *unix_name;
uint16_t *inchp, ch;
char *outchp;
const char *osta_id = "OSTA Compressed Unicode";
int ucode_chars, nice_uchars, is_osta_typ0, nout;
raw_name = malloc(2048 * sizeof(uint16_t));
assert(raw_name);
unix_name = raw_name + 1024; /* split space in half */
assert(sizeof(char) == sizeof(uint8_t));
outchp = result;
is_osta_typ0 = (chsp->type == 0);
is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
if (is_osta_typ0) {
/* TODO clean up */
*raw_name = *unix_name = 0;
ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
/* output UTF8 */
for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
ch = *inchp;
nout = wput_utf8(outchp, result_len, ch);
outchp += nout; result_len -= nout;
if (!ch) break;
}
*outchp++ = 0;
} else {
/* assume 8bit char length byte latin-1 */
assert(*id == 8);
assert(strlen((char *) (id+1)) <= NAME_MAX);
memcpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
}
free(raw_name);
}
void
unix_to_udf_name(char *result, uint8_t *result_len, char const *name, int name_len,
struct charspec *chsp)
{
uint16_t *raw_name;
uint16_t *outchp;
const char *inchp;
const char *osta_id = "OSTA Compressed Unicode";
int udf_chars, is_osta_typ0, bits;
size_t cnt;
/* allocate temporary unicode-16 buffer */
raw_name = malloc(1024);
assert(raw_name);
/* convert utf8 to unicode-16 */
*raw_name = 0;
inchp = name;
outchp = raw_name;
bits = 8;
for (cnt = name_len, udf_chars = 0; cnt;) {
*outchp = wget_utf8(&inchp, &cnt);
if (*outchp > 0xff)
bits=16;
outchp++;
udf_chars++;
}
/* null terminate just in case */
*outchp++ = 0;
is_osta_typ0 = (chsp->type == 0);
is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
if (is_osta_typ0) {
udf_chars = udf_CompressUnicode(udf_chars, bits,
(unicode_t *) raw_name,
(byte *) result);
} else {
printf("unix to udf name: no CHSP0 ?\n");
/* XXX assume 8bit char length byte latin-1 */
*result++ = 8; udf_chars = 1;
strncpy(result, name + 1, name_len);
udf_chars += name_len;
}
*result_len = udf_chars;
free(raw_name);
}
/* first call udf_set_regid and then the suffix */
void
udf_set_regid(struct regid *regid, char const *name)
{
memset(regid, 0, sizeof(*regid));
regid->flags = 0; /* not dirty and not protected */
strcpy((char *) regid->id, name);
}
void
udf_add_domain_regid(struct regid *regid)
{
uint16_t *ver;
ver = (uint16_t *) regid->id_suffix;
*ver = udf_rw16(context.min_udf);
}
void
udf_add_udf_regid(struct regid *regid)
{
uint16_t *ver;
ver = (uint16_t *) regid->id_suffix;
*ver = udf_rw16(context.min_udf);
regid->id_suffix[2] = 4; /* unix */
regid->id_suffix[3] = 8; /* NetBSD */
}
void
udf_add_impl_regid(struct regid *regid)
{
regid->id_suffix[0] = 4; /* unix */
regid->id_suffix[1] = 8; /* NetBSD */
}
void
udf_add_app_regid(struct regid *regid)
{
regid->id_suffix[0] = context.app_version_main;
regid->id_suffix[1] = context.app_version_sub;
}
/*
* Timestamp to timespec conversion code is taken with small modifications
* from FreeBSD /sys/fs/udf by Scott Long <[email protected]>
*/
static int mon_lens[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static int
udf_isaleapyear(int year)
{
int i;
i = (year % 4) ? 0 : 1;
i &= (year % 100) ? 1 : 0;
i |= (year % 400) ? 0 : 1;
return i;
}
void
udf_timestamp_to_timespec(struct timestamp *timestamp, struct timespec *timespec)
{
uint32_t usecs, secs, nsecs;
uint16_t tz;
int i, lpyear, daysinyear, year;
timespec->tv_sec = secs = 0;
timespec->tv_nsec = nsecs = 0;
/*
* DirectCD seems to like using bogus year values.
* Distrust time->month especially, since it will be used for an array
* index.
*/
year = udf_rw16(timestamp->year);
if ((year < 1970) || (timestamp->month > 12)) {
return;
}
/* Calculate the time and day */
usecs = timestamp->usec + 100*timestamp->hund_usec + 10000*timestamp->centisec;
nsecs = usecs * 1000;
secs = timestamp->second;
secs += timestamp->minute * 60;
secs += timestamp->hour * 3600;
secs += (timestamp->day-1) * 3600 * 24; /* day : 1-31 */
/* Calclulate the month */
lpyear = udf_isaleapyear(year);
for (i = 1; i < timestamp->month; i++)
secs += mon_lens[lpyear][i-1] * 3600 * 24; /* month: 1-12 */
for (i = 1970; i < year; i++) {
daysinyear = udf_isaleapyear(i) + 365 ;
secs += daysinyear * 3600 * 24;
}
/*
* Calculate the time zone. The timezone is 12 bit signed 2's
* compliment, so we gotta do some extra magic to handle it right.
*/
tz = udf_rw16(timestamp->type_tz);
tz &= 0x0fff; /* only lower 12 bits are significant */
if (tz & 0x0800) /* sign extention */
tz |= 0xf000;
/* TODO check timezone conversion */
#if 1
/* check if we are specified a timezone to convert */
if (udf_rw16(timestamp->type_tz) & 0x1000)
if ((int16_t) tz != -2047)
secs -= (int16_t) tz * 60;
#endif
timespec->tv_sec = secs;
timespec->tv_nsec = nsecs;
}
/*
* Fill in timestamp structure based on clock_gettime(). Time is reported back
* as a time_t accompanied with a nano second field.
*
* The husec, usec and csec could be relaxed in type.
*/
void
udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp)
{
struct tm tm;
uint64_t husec, usec, csec;
memset(timestamp, 0, sizeof(*timestamp));
gmtime_r(×pec->tv_sec, &tm);
/*
* Time type and time zone : see ECMA 1/7.3, UDF 2., 2.1.4.1, 3.1.1.
*
* Lower 12 bits are two complement signed timezone offset if bit 12
* (method 1) is clear. Otherwise if bit 12 is set, specify timezone
* offset to -2047 i.e. unsigned `zero'
*/
/* set method 1 for CUT/GMT */
timestamp->type_tz = udf_rw16((1<<12) + 0);
timestamp->year = udf_rw16(tm.tm_year + 1900);
timestamp->month = tm.tm_mon + 1; /* `tm' uses 0..11 for months */
timestamp->day = tm.tm_mday;
timestamp->hour = tm.tm_hour;
timestamp->minute = tm.tm_min;
timestamp->second = tm.tm_sec;
usec = (timespec->tv_nsec + 500) / 1000; /* round */
husec = usec / 100;
usec -= husec * 100; /* only 0-99 in usec */
csec = husec / 100; /* only 0-99 in csec */
husec -= csec * 100; /* only 0-99 in husec */
/* in rare cases there is overflow in csec */
csec = MIN(99, csec);
husec = MIN(99, husec);
usec = MIN(99, usec);
timestamp->centisec = csec;
timestamp->hund_usec = husec;
timestamp->usec = usec;
}
static void
udf_set_timestamp(struct timestamp *timestamp, time_t value)
{
struct timespec t;
memset(&t, 0, sizeof(struct timespec));
t.tv_sec = value;
t.tv_nsec = 0;
udf_timespec_to_timestamp(&t, timestamp);
}
static uint32_t
unix_mode_to_udf_perm(mode_t mode)
{
uint32_t perm;
perm = ((mode & S_IRWXO) );
perm |= ((mode & S_IRWXG) << 2);
perm |= ((mode & S_IRWXU) << 4);
perm |= ((mode & S_IWOTH) << 3);
perm |= ((mode & S_IWGRP) << 5);
perm |= ((mode & S_IWUSR) << 7);
return perm;
}
/* end of copied code */
void
udf_encode_osta_id(char *osta_id, uint16_t len, char *text)
{
struct charspec osta_charspec;
uint8_t result_len;
memset(osta_id, 0, len);
if (!text || (strlen(text) == 0)) return;
udf_osta_charset(&osta_charspec);
unix_to_udf_name(osta_id, &result_len, text, strlen(text),
&osta_charspec);
/* Ecma 167/7.2.13 states that length is recorded in the last byte */
osta_id[len-1] = strlen(text)+1;
}