ObjFW  Diff

Differences From Artifact [61acef2124]:

To Artifact [d68323165e]:


1
2
3
4

5
6
7
8
9
10
11
1



2
3
4
5
6
7
8
9

-
-
-
+







/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018, 2019, 2020
 *   Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
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
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







-
+
-


-
+
-


-
+
-









-
+
-






-
+
-




-
+
-

-
+
-



-
+
-

-
+
-










-
+
-







-
+
-



-
+
-







-
+
-



-
+
-


-
+
-










-
+
-













-
+
-







-
+
-




-
+
-












-
+
-
-
+
-







	[data increaseCountBy: 2];

	[data addItems: [_compressionMethod
			    cStringWithEncoding: OF_STRING_ENCODING_ASCII]
		 count: 5];

	tmp32 = OF_BSWAP32_IF_BE(_compressedSize);
	[data addItems: &tmp32
	[data addItems: &tmp32 count: sizeof(tmp32)];
		 count: sizeof(tmp32)];

	tmp32 = OF_BSWAP32_IF_BE(_uncompressedSize);
	[data addItems: &tmp32
	[data addItems: &tmp32 count: sizeof(tmp32)];
		 count: sizeof(tmp32)];

	tmp32 = OF_BSWAP32_IF_BE((uint32_t)_date.timeIntervalSince1970);
	[data addItems: &tmp32
	[data addItems: &tmp32 count: sizeof(tmp32)];
		 count: sizeof(tmp32)];

	/* Reserved */
	[data increaseCountBy: 1];

	/* Header level */
	[data addItem: "\x02"];

	/* CRC16 */
	tmp16 = OF_BSWAP16_IF_BE(_CRC16);
	[data addItems: &tmp16
	[data addItems: &tmp16 count: sizeof(tmp16)];
		 count: sizeof(tmp16)];

	/* Operating system identifier */
	[data addItem: "U"];

	/* Common header. Contains CRC16, which is written at the end. */
	tmp16 = OF_BSWAP16_IF_BE(5);
	[data addItems: &tmp16
	[data addItems: &tmp16 count: sizeof(tmp16)];
		 count: sizeof(tmp16)];
	[data addItem: "\x00"];
	[data increaseCountBy: 2];

	tmp16 = OF_BSWAP16_IF_BE((uint16_t)fileNameLength + 3);
	[data addItems: &tmp16
	[data addItems: &tmp16 count: sizeof(tmp16)];
		 count: sizeof(tmp16)];
	[data addItem: "\x01"];
	[data addItems: fileName
	[data addItems: fileName count: fileNameLength];
		 count: fileNameLength];

	if (directoryNameLength > 0) {
		tmp16 = OF_BSWAP16_IF_BE((uint16_t)directoryNameLength + 3);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x02"];
		[data addItems: directoryName
		[data addItems: directoryName count: directoryNameLength];
			 count: directoryNameLength];
	}

	if (_fileComment != nil) {
		size_t fileCommentLength =
		    [_fileComment cStringLengthWithEncoding: encoding];

		if (fileCommentLength > UINT16_MAX - 3)
			@throw [OFOutOfRangeException exception];

		tmp16 = OF_BSWAP16_IF_BE((uint16_t)fileCommentLength + 3);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x3F"];
		[data addItems: [_fileComment cStringWithEncoding: encoding]
			 count: fileCommentLength];
	}

	if (_mode != nil) {
		tmp16 = OF_BSWAP16_IF_BE(5);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x50"];

		tmp16 = OF_BSWAP16_IF_BE(_mode.unsignedShortValue);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
	}

	if (_UID != nil || _GID != nil) {
		if (_UID == nil || _GID == nil)
			@throw [OFInvalidArgumentException exception];

		tmp16 = OF_BSWAP16_IF_BE(7);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x51"];

		tmp16 = OF_BSWAP16_IF_BE(_GID.unsignedShortValue);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];

		tmp16 = OF_BSWAP16_IF_BE(_UID.unsignedShortValue);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
	}

	if (_group != nil) {
		size_t groupLength =
		    [_group cStringLengthWithEncoding: encoding];

		if (groupLength > UINT16_MAX - 3)
			@throw [OFOutOfRangeException exception];

		tmp16 = OF_BSWAP16_IF_BE((uint16_t)groupLength + 3);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x52"];
		[data addItems: [_group cStringWithEncoding: encoding]
			 count: groupLength];
	}

	if (_owner != nil) {
		size_t ownerLength =
		    [_owner cStringLengthWithEncoding: encoding];

		if (ownerLength > UINT16_MAX - 3)
			@throw [OFOutOfRangeException exception];

		tmp16 = OF_BSWAP16_IF_BE((uint16_t)ownerLength + 3);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x53"];
		[data addItems: [_owner cStringWithEncoding: encoding]
			 count: ownerLength];
	}

	if (_modificationDate != nil) {
		tmp16 = OF_BSWAP16_IF_BE(7);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItem: "\x54"];

		tmp32 = OF_BSWAP32_IF_BE(
		    (uint32_t)_modificationDate.timeIntervalSince1970);
		[data addItems: &tmp32
		[data addItems: &tmp32 count: sizeof(tmp32)];
			 count: sizeof(tmp32)];
	}

	for (OFData *extension in _extensions) {
		size_t extensionLength = extension.count;

		if (extension.itemSize != 1)
			@throw [OFInvalidArgumentException exception];

		if (extensionLength > UINT16_MAX - 2)
			@throw [OFOutOfRangeException exception];

		tmp16 = OF_BSWAP16_IF_BE((uint16_t)extensionLength + 2);
		[data addItems: &tmp16
		[data addItems: &tmp16 count: sizeof(tmp16)];
			 count: sizeof(tmp16)];
		[data addItems: extension.items
		[data addItems: extension.items count: extension.count];
			 count: extension.count];
	}

	/* Zero-length extension to terminate */
	[data increaseCountBy: 2];

	headerSize = data.count;