libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
write.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2025 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18#include <errno.h>
19#include <stdbool.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "aaruformat.h"
26#include "internal.h"
27#include "log.h"
28#include "structs/lisa_tag.h"
29#include "xxhash.h"
30
98AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sector_address, bool negative,
99 const uint8_t *data, uint8_t sector_status, uint32_t length)
100{
101 TRACE("Entering aaruf_write_sector(%p, %" PRIu64 ", %d, %p, %u, %u)", context, sector_address, negative, data,
102 sector_status, length);
103
104 // Check context is correct AaruFormat context
105 if(context == NULL)
106 {
107 FATAL("Invalid context");
108
109 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
111 }
112
113 aaruformat_context *ctx = context;
114
115 // Not a libaaruformat context
116 if(ctx->magic != AARU_MAGIC)
117 {
118 FATAL("Invalid context");
119
120 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
122 }
123
124 // Check we are writing
125 if(!ctx->is_writing)
126 {
127 FATAL("Trying to write a read-only image");
128
129 TRACE("Exiting aaruf_write_sector() = AARUF_READ_ONLY");
130 return AARUF_READ_ONLY;
131 }
132
133 if(negative && sector_address > ctx->user_data_ddt_header.negative - 1)
134 {
135 FATAL("Sector address out of bounds");
136
137 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
139 }
140
141 if(!negative && sector_address > ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow - 1)
142 {
143 FATAL("Sector address out of bounds");
144
145 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
147 }
148
149 if(!ctx->rewinded)
150 {
151 if(sector_address <= ctx->last_written_block)
152 {
153 TRACE("Rewinded");
154 ctx->rewinded = true;
155
156 // Disable MD5 calculation
157 if(ctx->calculating_md5) ctx->calculating_md5 = false;
158 // Disable SHA1 calculation
159 if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
160 // Disable SHA256 calculation
161 if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
162 // Disable SpamSum calculation
163 if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
164 // Disable BLAKE3 calculation
165 if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
166 }
167 else
168 ctx->last_written_block = sector_address;
169 }
170
171 // Calculate MD5 on-the-fly if requested and sector is within user sectors (not negative or overflow)
172 if(ctx->calculating_md5 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
173 aaruf_md5_update(&ctx->md5_context, data, length);
174 // Calculate SHA1 on-the-fly if requested and sector is within user sectors (not negative or overflow)
175 if(ctx->calculating_sha1 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
176 aaruf_sha1_update(&ctx->sha1_context, data, length);
177 // Calculate SHA256 on-the-fly if requested and sector is within user sectors (not negative or overflow)
178 if(ctx->calculating_sha256 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
179 aaruf_sha256_update(&ctx->sha256_context, data, length);
180 // Calculate SpamSum on-the-fly if requested and sector is within user sectors (not negative or overflow)
181 if(ctx->calculating_spamsum && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
182 aaruf_spamsum_update(ctx->spamsum_context, data, length);
183 // Calculate BLAKE3 on-the-fly if requested and sector is within user sectors (not negative or overflow)
184 if(ctx->calculating_blake3 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
185 blake3_hasher_update(ctx->blake3_context, data, length);
186
187 // Close current block first
188 if(ctx->writing_buffer != NULL &&
189 // When sector size changes or block reaches maximum size
190 (ctx->current_block_header.sectorSize != length ||
192 {
193 TRACE("Closing current block before writing new data");
194 int error = aaruf_close_current_block(ctx);
195
196 if(error != AARUF_STATUS_OK)
197 {
198 FATAL("Error closing current block: %d", error);
199
200 TRACE("Exiting aaruf_write_sector() = %d", error);
201 return error;
202 }
203 }
204
205 uint64_t ddt_entry = 0;
206 bool ddt_ok;
207
208 if(ctx->deduplicate)
209 {
210 // Calculate 64-bit XXH3 hash of the sector
211 TRACE("Hashing sector data for deduplication");
212 uint64_t hash = XXH3_64bits(data, length);
213
214 // Check if the hash is already in the map
215 bool existing = lookup_map(ctx->sector_hash_map, hash, &ddt_entry);
216 TRACE("Block does %s exist in deduplication map", existing ? "already" : "not yet");
217
218 ddt_ok = set_ddt_entry_v2(ctx, sector_address, negative, ctx->current_block_offset, ctx->next_block_position,
219 sector_status, &ddt_entry);
220 if(!ddt_ok)
221 {
222 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_CANNOT_SET_DDT_ENTRY");
224 }
225
226 if(existing)
227 {
228 TRACE("Sector exists, so not writing to image");
229 TRACE("Exiting aaruf_write_sector() = AARUF_STATUS_OK");
230 return AARUF_STATUS_OK;
231 }
232
233 TRACE("Inserting sector hash into deduplication map, proceeding to write into image as normal");
234 insert_map(ctx->sector_hash_map, hash, ddt_entry);
235 }
236 else
237 ddt_ok = set_ddt_entry_v2(ctx, sector_address, negative, ctx->current_block_offset, ctx->next_block_position,
238 sector_status, &ddt_entry);
239
240 if(!ddt_ok)
241 {
242 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_CANNOT_SET_DDT_ENTRY");
244 }
245
246 // No block set
247 if(ctx->writing_buffer_position == 0)
248 {
249 TRACE("Creating new writing block");
252 ctx->current_block_header.sectorSize = length;
253
254 // We need to save the track type for later compression
255 if(ctx->image_info.MetadataMediaType == OpticalDisc && ctx->track_entries != NULL)
256 {
257 const TrackEntry *track = NULL;
258 for(int i = 0; i < ctx->tracks_header.entries; i++)
259 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
260 {
261 track = &ctx->track_entries[i];
262 break;
263 }
264
265 if(track != NULL)
266 {
267 ctx->current_track_type = track->type;
268
269 if(track->sequence == 0 && track->start == 0 && track->end == 0) ctx->current_track_type = Data;
270 }
271 else
273
274 if(ctx->current_track_type == Audio &&
275 // JaguarCD stores data in audio tracks. FLAC is too inefficient, we need to use LZMA as data.
276 (ctx->image_info.MediaType == JaguarCD && track->session > 1 ||
277 // VideoNow stores video in audio tracks, and LZMA works better too.
281
282 if(ctx->compression_enabled)
283 {
284 if(ctx->current_track_type == Audio)
286 else
288 }
289 else
291 }
292 else
293 {
295 if(ctx->compression_enabled)
297 else
299 }
300
301 uint32_t max_buffer_size = (1 << ctx->user_data_ddt_header.dataShift) * ctx->current_block_header.sectorSize * 2;
302 TRACE("Setting max buffer size to %u bytes", max_buffer_size);
303
304 TRACE("Allocating memory for writing buffer");
305 ctx->writing_buffer = (uint8_t *)calloc(1, max_buffer_size);
306 if(ctx->writing_buffer == NULL)
307 {
308 FATAL("Could not allocate memory");
309
310 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
312 }
313 }
314
315 TRACE("Copying data to writing buffer at position %zu", ctx->writing_buffer_position);
316 memcpy(ctx->writing_buffer + ctx->writing_buffer_position, data, length);
317 TRACE("Advancing writing buffer position to %zu", ctx->writing_buffer_position + length);
318 ctx->writing_buffer_position += length;
319 TRACE("Advancing current block offset to %zu", ctx->current_block_offset + 1);
321
322 TRACE("Exiting aaruf_write_sector() = AARUF_STATUS_OK");
323 return AARUF_STATUS_OK;
324}
325
532AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_long(void *context, uint64_t sector_address, bool negative,
533 const uint8_t *data, uint8_t sector_status, uint32_t length)
534{
535 TRACE("Entering aaruf_write_sector_long(%p, %" PRIu64 ", %d, %p, %u, %u)", context, sector_address, negative, data,
536 sector_status, length);
537
538 // Check context is correct AaruFormat context
539 if(context == NULL)
540 {
541 FATAL("Invalid context");
542
543 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
545 }
546
547 aaruformat_context *ctx = context;
548
549 // Not a libaaruformat context
550 if(ctx->magic != AARU_MAGIC)
551 {
552 FATAL("Invalid context");
553
554 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
556 }
557
558 // Check we are writing
559 if(!ctx->is_writing)
560 {
561 FATAL("Trying to write a read-only image");
562
563 TRACE("Exiting aaruf_write_sector() = AARUF_READ_ONLY");
564 return AARUF_READ_ONLY;
565 }
566
567 if(negative && sector_address > ctx->user_data_ddt_header.negative - 1)
568 {
569 FATAL("Sector address out of bounds");
570
571 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
573 }
574
575 if(!negative && sector_address > ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow - 1)
576 {
577 FATAL("Sector address out of bounds");
578
579 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
581 }
582
583 switch(ctx->image_info.MetadataMediaType)
584 {
585 case OpticalDisc:
586 {
587 TrackEntry track = {0};
588
589 for(int i = 0; i < ctx->tracks_header.entries; i++)
590 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
591 {
592 track = ctx->track_entries[i];
593 break;
594 }
595
596 if(track.sequence == 0 && track.start == 0 && track.end == 0) track.type = Data;
597
598 uint64_t corrected_sector_address = sector_address;
599
600 // Calculate positive or negative sector
601 if(negative)
602 corrected_sector_address -= ctx->user_data_ddt_header.negative;
603 else
604 corrected_sector_address += ctx->user_data_ddt_header.negative;
605
606 uint64_t total_sectors =
608
609 // DVD long sector
610 if(length == 2064 && (ctx->image_info.MediaType == DVDROM || ctx->image_info.MediaType == PS2DVD ||
611 ctx->image_info.MediaType == SACD || ctx->image_info.MediaType == PS3DVD ||
612 ctx->image_info.MediaType == DVDR || ctx->image_info.MediaType == DVDRW ||
617 ctx->image_info.MediaType == Nuon))
618 {
619 if(ctx->sector_id == NULL) ctx->sector_id = calloc(1, 4 * total_sectors);
620 if(ctx->sector_ied == NULL) ctx->sector_ied = calloc(1, 2 * total_sectors);
621 if(ctx->sector_cpr_mai == NULL) ctx->sector_cpr_mai = calloc(1, 6 * total_sectors);
622 if(ctx->sector_edc == NULL) ctx->sector_edc = calloc(1, 4 * total_sectors);
623
624 memcpy(ctx->sector_id + corrected_sector_address * 4, data, 4);
625 memcpy(ctx->sector_ied + corrected_sector_address * 2, data + 4, 2);
626 memcpy(ctx->sector_cpr_mai + corrected_sector_address * 6, data + 6, 6);
627 memcpy(ctx->sector_edc + corrected_sector_address * 4, data + 2060, 4);
628
629 return aaruf_write_sector(context, sector_address, negative, data + 12, sector_status, 2048);
630 }
631
632 if(length != 2352)
633 {
634 FATAL("Incorrect sector size");
635 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_INCORRECT_DATA_SIZE");
637 }
638
639 ctx->writing_long = true;
640
641 if(!ctx->rewinded)
642 {
643 if(sector_address <= ctx->last_written_block)
644 {
645 TRACE("Rewinded");
646 ctx->rewinded = true;
647
648 // Disable MD5 calculation
649 if(ctx->calculating_md5) ctx->calculating_md5 = false;
650 // Disable SHA1 calculation
651 if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
652 // Disable SHA256 calculation
653 if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
654 // Disable SpamSum calculation
655 if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
656 // Disable BLAKE3 calculation
657 if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
658 }
659 else
660 ctx->last_written_block = sector_address;
661 }
662
663 // Calculate MD5 on-the-fly if requested and sector is within user sectors (not negative or overflow)
664 if(ctx->calculating_md5 && !negative && sector_address <= ctx->image_info.Sectors)
665 aaruf_md5_update(&ctx->md5_context, data, length);
666 // Calculate SHA1 on-the-fly if requested and sector is within user sectors (not negative or overflow)
667 if(ctx->calculating_sha1 && !negative && sector_address <= ctx->image_info.Sectors)
668 aaruf_sha1_update(&ctx->sha1_context, data, length);
669 // Calculate SHA256 on-the-fly if requested and sector is within user sectors (not negative or overflow)
670 if(ctx->calculating_sha256 && !negative && sector_address <= ctx->image_info.Sectors)
671 aaruf_sha256_update(&ctx->sha256_context, data, length);
672 // Calculate SpamSum on-the-fly if requested and sector is within user sectors (not negative or overflow)
673 if(ctx->calculating_spamsum && !negative && sector_address <= ctx->image_info.Sectors)
674 aaruf_spamsum_update(ctx->spamsum_context, data, length);
675 // Calculate BLAKE3 on-the-fly if requested and sector is within user sectors (not negative or overflow)
676 if(ctx->calculating_blake3 && !negative && sector_address <= ctx->image_info.Sectors)
677 blake3_hasher_update(ctx->blake3_context, data, length);
678
679 bool prefix_correct;
680
681 // Split raw cd sector data in prefix (sync, header), user data and suffix (edc, ecc p, ecc q)
682 switch(track.type)
683 {
684 case Audio:
685 case Data:
686 return aaruf_write_sector(context, sector_address, negative, data, sector_status, length);
687 case CdMode1:
688
689 // If we do not have a DDT V2 for sector prefix, create one
690 if(ctx->sector_prefix_ddt2 == NULL)
691 {
692 ctx->sector_prefix_ddt2 =
693 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
695
696 if(ctx->sector_prefix_ddt2 == NULL)
697 {
698 FATAL("Could not allocate memory for CD sector prefix DDT");
699
700 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
702 }
703 }
704
705 // If we do not have a DDT V2 for sector suffix, create one
706 if(ctx->sector_suffix_ddt2 == NULL)
707 {
708 ctx->sector_suffix_ddt2 =
709 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
711
712 if(ctx->sector_suffix_ddt2 == NULL)
713 {
714 FATAL("Could not allocate memory for CD sector prefix DDT");
715
716 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
718 }
719 }
720
721 if(ctx->sector_prefix == NULL)
722 {
725 ctx->sector_prefix = malloc(ctx->sector_prefix_length);
726
727 if(ctx->sector_prefix == NULL)
728 {
729 FATAL("Could not allocate memory for CD sector prefix buffer");
730
731 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
733 }
734 }
735
736 if(ctx->sector_suffix == NULL)
737 {
741 ctx->sector_suffix = malloc(ctx->sector_suffix_length);
742
743 if(ctx->sector_suffix == NULL)
744 {
745 FATAL("Could not allocate memory for CD sector suffix buffer");
746
747 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
749 }
750 }
751
752 bool empty = true;
753
754 for(int i = 0; i < length; i++)
755 if(data[i] != 0)
756 {
757 empty = false;
758 break;
759 }
760
761 if(empty)
762 {
763 ctx->sector_prefix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
764 ctx->sector_suffix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
765 return aaruf_write_sector(context, sector_address, negative, data + 16, SectorStatusNotDumped,
766 2048);
767 }
768
769 prefix_correct = true;
770
771 if(data[0x00] != 0x00 || data[0x01] != 0xFF || data[0x02] != 0xFF || data[0x03] != 0xFF ||
772 data[0x04] != 0xFF || data[0x05] != 0xFF || data[0x06] != 0xFF || data[0x07] != 0xFF ||
773 data[0x08] != 0xFF || data[0x09] != 0xFF || data[0x0A] != 0xFF || data[0x0B] != 0x00 ||
774 data[0x0F] != 0x01)
775 prefix_correct = false;
776
777 if(prefix_correct)
778 {
779 const int minute = (data[0x0C] >> 4) * 10 + (data[0x0C] & 0x0F);
780 const int second = (data[0x0D] >> 4) * 10 + (data[0x0D] & 0x0F);
781 const int frame = (data[0x0E] >> 4) * 10 + (data[0x0E] & 0x0F);
782 const int stored_lba = minute * 60 * 75 + second * 75 + frame - 150;
783 prefix_correct = stored_lba == sector_address;
784 }
785
786 if(prefix_correct)
787 ctx->sector_prefix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode1Correct << 60;
788 else
789 {
790 // Copy CD prefix from data buffer to prefix buffer
791 memcpy(ctx->sector_prefix + ctx->sector_prefix_offset, data, 16);
792 ctx->sector_prefix_ddt2[corrected_sector_address] = (uint64_t)(ctx->sector_prefix_offset / 16);
793 ctx->sector_prefix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
794 ctx->sector_prefix_offset += 16;
795
796 // Grow prefix buffer if needed
798 {
799 ctx->sector_prefix_length *= 2;
800 ctx->sector_prefix = realloc(ctx->sector_prefix, ctx->sector_prefix_length);
801
802 if(ctx->sector_prefix == NULL)
803 {
804 FATAL("Could not allocate memory for CD sector prefix buffer");
805
806 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
808 }
809 }
810 }
811
812 const bool suffix_correct = aaruf_ecc_cd_is_suffix_correct(ctx->ecc_cd_context, data);
813
814 if(suffix_correct)
815 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode1Correct << 60;
816 else
817 {
818 // Copy CD suffix from data buffer to suffix buffer
819 memcpy(ctx->sector_suffix + ctx->sector_suffix_offset, data + 2064, 288);
820 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)(ctx->sector_suffix_offset / 288);
821 ctx->sector_suffix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
822 ctx->sector_suffix_offset += 288;
823
824 // Grow suffix buffer if needed
826 {
827 ctx->sector_suffix_length *= 2;
828 ctx->sector_suffix = realloc(ctx->sector_suffix, ctx->sector_suffix_length);
829
830 if(ctx->sector_suffix == NULL)
831 {
832 FATAL("Could not allocate memory for CD sector suffix buffer");
833
834 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
836 }
837 }
838 }
839
840 return aaruf_write_sector(context, sector_address, negative, data + 16, SectorStatusMode1Correct,
841 2048);
842 case CdMode2Form1:
843 case CdMode2Form2:
844 case CdMode2Formless:
845 // If we do not have a DDT V2 for sector prefix, create one
846 if(ctx->sector_prefix_ddt2 == NULL)
847 {
848 ctx->sector_prefix_ddt2 =
849 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
851
852 if(ctx->sector_prefix_ddt2 == NULL)
853 {
854 FATAL("Could not allocate memory for CD sector prefix DDT");
855
856 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
858 }
859 }
860
861 // If we do not have a DDT V2 for sector suffix, create one
862 if(ctx->sector_suffix_ddt2 == NULL)
863 {
864 ctx->sector_suffix_ddt2 =
865 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
867
868 if(ctx->sector_suffix_ddt2 == NULL)
869 {
870 FATAL("Could not allocate memory for CD sector prefix DDT");
871
872 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
874 }
875 }
876
877 if(ctx->sector_prefix == NULL)
878 {
881 ctx->sector_prefix = malloc(ctx->sector_prefix_length);
882
883 if(ctx->sector_prefix == NULL)
884 {
885 FATAL("Could not allocate memory for CD sector prefix buffer");
886
887 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
889 }
890 }
891
892 if(ctx->sector_suffix == NULL)
893 {
897 ctx->sector_suffix = malloc(ctx->sector_suffix_length);
898
899 if(ctx->sector_suffix == NULL)
900 {
901 FATAL("Could not allocate memory for CD sector suffix buffer");
902
903 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
905 }
906 }
907
908 empty = true;
909
910 for(int i = 0; i < length; i++)
911 if(data[i] != 0)
912 {
913 empty = false;
914 break;
915 }
916
917 if(empty)
918 {
919 ctx->sector_prefix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
920 ctx->sector_suffix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
921 return aaruf_write_sector(context, sector_address, negative, data + 16, SectorStatusNotDumped,
922 2328);
923 }
924
925 const bool form2 = (data[18] & 0x20) == 0x20 || (data[22] & 0x20) == 0x20;
926
927 prefix_correct = true;
928
929 if(data[0x00] != 0x00 || data[0x01] != 0xFF || data[0x02] != 0xFF || data[0x03] != 0xFF ||
930 data[0x04] != 0xFF || data[0x05] != 0xFF || data[0x06] != 0xFF || data[0x07] != 0xFF ||
931 data[0x08] != 0xFF || data[0x09] != 0xFF || data[0x0A] != 0xFF || data[0x0B] != 0x00 ||
932 data[0x0F] != 0x02)
933 prefix_correct = false;
934
935 if(prefix_correct)
936 {
937 const int minute = (data[0x0C] >> 4) * 10 + (data[0x0C] & 0x0F);
938 const int second = (data[0x0D] >> 4) * 10 + (data[0x0D] & 0x0F);
939 const int frame = (data[0x0E] >> 4) * 10 + (data[0x0E] & 0x0F);
940 const int stored_lba = minute * 60 * 75 + second * 75 + frame - 150;
941 prefix_correct = stored_lba == sector_address;
942 }
943
944 if(prefix_correct)
945 ctx->sector_prefix_ddt2[corrected_sector_address] =
946 (uint64_t)(form2 ? SectorStatusMode2Form2Ok : SectorStatusMode2Form1Ok) << 60;
947 else
948 {
949 // Copy CD prefix from data buffer to prefix buffer
950 memcpy(ctx->sector_prefix + ctx->sector_prefix_offset, data, 16);
951 ctx->sector_prefix_ddt2[corrected_sector_address] = (uint32_t)(ctx->sector_prefix_offset / 16);
952 ctx->sector_prefix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
953 ctx->sector_prefix_offset += 16;
954
955 // Grow prefix buffer if needed
957 {
958 ctx->sector_prefix_length *= 2;
959 ctx->sector_prefix = realloc(ctx->sector_prefix, ctx->sector_prefix_length);
960
961 if(ctx->sector_prefix == NULL)
962 {
963 FATAL("Could not allocate memory for CD sector prefix buffer");
964
965 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
967 }
968 }
969 }
970
971 if(ctx->mode2_subheaders == NULL)
972 {
973 ctx->mode2_subheaders =
974 calloc(1, 8 * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
976
977 if(ctx->mode2_subheaders == NULL)
978 {
979 FATAL("Could not allocate memory for CD mode 2 subheader buffer");
980
981 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
983 }
984 }
985
986 if(form2)
987 {
988 const uint32_t computed_edc = aaruf_edc_cd_compute(ctx->ecc_cd_context, 0, data, 0x91C, 0x10);
989 uint32_t edc = 0;
990 memcpy(&edc, data + 0x92C, sizeof(edc));
991 const bool correct_edc = computed_edc == edc;
992
993 if(correct_edc)
994 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode2Form2Ok
995 << 60;
996 else if(edc == 0)
997 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode2Form2NoCrc
998 << 60;
999 else
1000 {
1001 // Copy CD suffix from data buffer to suffix buffer
1002 memcpy(ctx->sector_suffix + ctx->sector_suffix_offset, data + 2348, 4);
1003 ctx->sector_suffix_ddt2[corrected_sector_address] =
1004 (uint64_t)(ctx->sector_suffix_offset / 288);
1005 ctx->sector_suffix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
1006 ctx->sector_suffix_offset += 288;
1007
1008 // Grow suffix buffer if needed
1010 {
1011 ctx->sector_suffix_length *= 2;
1012 ctx->sector_suffix = realloc(ctx->sector_suffix, ctx->sector_suffix_length);
1013
1014 if(ctx->sector_suffix == NULL)
1015 {
1016 FATAL("Could not allocate memory for CD sector suffix buffer");
1017
1018 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
1020 }
1021 }
1022 }
1023
1024 // Copy subheader from data buffer to subheader buffer
1025 memcpy(ctx->mode2_subheaders + corrected_sector_address * 8, data + 0x10, 8);
1026 return aaruf_write_sector(context, sector_address, negative, data + 24,
1028 : correct_edc ? SectorStatusMode2Form2Ok
1030 2324);
1031 }
1032
1033 const bool correct_ecc = aaruf_ecc_cd_is_suffix_correct_mode2(ctx->ecc_cd_context, data);
1034 const uint32_t computed_edc = aaruf_edc_cd_compute(ctx->ecc_cd_context, 0, data, 0x808, 0x10);
1035 uint32_t edc = 0;
1036 memcpy(&edc, data + 0x818, sizeof(edc));
1037 const bool correct_edc = computed_edc == edc;
1038
1039 if(correct_ecc && correct_edc)
1040 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode2Form1Ok << 60;
1041 else
1042 {
1043 // Copy CD suffix from data buffer to suffix buffer
1044 memcpy(ctx->sector_suffix + ctx->sector_suffix_offset, data + 2072, 280);
1045 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)(ctx->sector_suffix_offset / 288);
1046 ctx->sector_suffix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
1047 ctx->sector_suffix_offset += 288;
1048
1049 // Grow suffix buffer if needed
1051 {
1052 ctx->sector_suffix_length *= 2;
1053 ctx->sector_suffix = realloc(ctx->sector_suffix, ctx->sector_suffix_length);
1054
1055 if(ctx->sector_suffix == NULL)
1056 {
1057 FATAL("Could not allocate memory for CD sector suffix buffer");
1058
1059 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
1061 }
1062 }
1063 }
1064
1065 // Copy subheader from data buffer to subheader buffer
1066 memcpy(ctx->mode2_subheaders + corrected_sector_address * 8, data + 0x10, 8);
1067 return aaruf_write_sector(
1068 context, sector_address, negative, data + 24,
1069 correct_edc && correct_ecc ? SectorStatusMode2Form1Ok : SectorStatusErrored, 2048);
1070 }
1071
1072 break;
1073 }
1074 case BlockMedia:
1075 switch(ctx->image_info.MediaType)
1076 {
1077 case AppleFileWare:
1078 case AppleProfile:
1079 case AppleSonyDS:
1080 case AppleSonySS:
1081 case AppleWidget:
1082 case PriamDataTower:
1083 {
1084 uint8_t *newTag;
1085 int newTagSize = 0;
1086
1087 switch(length - 512)
1088 {
1089 // Sony tag
1090 case 12:
1091 {
1092 const sony_tag decoded_sony_tag = bytes_to_sony_tag(data + 512);
1093
1095 {
1096 const profile_tag decoded_profile_tag = sony_tag_to_profile(decoded_sony_tag);
1097 newTag = profile_tag_to_bytes(decoded_profile_tag);
1098 newTagSize = 20;
1099 }
1100 else if(ctx->image_info.MediaType == PriamDataTower)
1101 {
1102 const priam_tag decoded_priam_tag = sony_tag_to_priam(decoded_sony_tag);
1103 newTag = priam_tag_to_bytes(decoded_priam_tag);
1104 newTagSize = 24;
1105 }
1106 else if(ctx->image_info.MediaType == AppleSonyDS ||
1108 {
1109 newTag = malloc(12);
1110 memcpy(newTag, data + 512, 12);
1111 newTagSize = 12;
1112 }
1113 break;
1114 }
1115 // Profile tag
1116 case 20:
1117 {
1118 const profile_tag decoded_profile_tag = bytes_to_profile_tag(data + 512);
1119
1121 {
1122 newTag = malloc(20);
1123 memcpy(newTag, data + 512, 20);
1124 newTagSize = 20;
1125 }
1126 else if(ctx->image_info.MediaType == PriamDataTower)
1127 {
1128 const priam_tag decoded_priam_tag = profile_tag_to_priam(decoded_profile_tag);
1129 newTag = priam_tag_to_bytes(decoded_priam_tag);
1130 newTagSize = 24;
1131 }
1132 else if(ctx->image_info.MediaType == AppleSonyDS ||
1134 {
1135 const sony_tag decoded_sony_tag = profile_tag_to_sony(decoded_profile_tag);
1136 newTag = sony_tag_to_bytes(decoded_sony_tag);
1137 newTagSize = 12;
1138 }
1139 break;
1140 }
1141 // Priam tag
1142 case 24:
1143 {
1144 const priam_tag decoded_priam_tag = bytes_to_priam_tag(data + 512);
1146 {
1147 const profile_tag decoded_profile_tag = priam_tag_to_profile(decoded_priam_tag);
1148 newTag = profile_tag_to_bytes(decoded_profile_tag);
1149 newTagSize = 20;
1150 }
1151 else if(ctx->image_info.MediaType == PriamDataTower)
1152 {
1153 newTag = malloc(24);
1154 memcpy(newTag, data + 512, 24);
1155 newTagSize = 24;
1156 }
1157 else if(ctx->image_info.MediaType == AppleSonyDS ||
1159 {
1160 const sony_tag decoded_sony_tag = priam_tag_to_sony(decoded_priam_tag);
1161 newTag = sony_tag_to_bytes(decoded_sony_tag);
1162 newTagSize = 12;
1163 }
1164 break;
1165 }
1166 case 0:
1167 newTagSize = 0;
1168 break;
1169 default:
1170 FATAL("Incorrect sector size");
1171 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_INCORRECT_DATA_SIZE");
1173 }
1174
1175 if(newTagSize == 0)
1176 return aaruf_write_sector(context, sector_address, negative, data, sector_status, 512);
1177
1178 if(ctx->sector_subchannel == NULL)
1179 {
1180 ctx->sector_subchannel =
1181 calloc(1, newTagSize * (ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow));
1182
1183 if(ctx->sector_subchannel == NULL)
1184 {
1185 FATAL("Could not allocate memory for sector subchannel DDT");
1186
1187 free(newTag);
1188
1189 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
1191 }
1192 }
1193
1194 memcpy(ctx->sector_subchannel + sector_address * newTagSize, newTag, newTagSize);
1195 free(newTag);
1196
1197 return aaruf_write_sector(context, sector_address, negative, data, sector_status, 512);
1198 }
1199 default:
1201 }
1202 default:
1203 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
1205 }
1206
1207 // Fallback return when media type branch does not produce a value (satisfy non-void contract)
1209}
1210
1394{
1395 // Not a libaaruformat context
1396 if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
1397
1398 // Check we are writing
1399 if(!ctx->is_writing) return AARUF_READ_ONLY;
1400
1402
1403 TRACE("Initializing CRC64 context");
1405 TRACE("Updating CRC64");
1408
1409 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1410 uint8_t *cmp_buffer = NULL;
1411
1413 {
1414 case None:
1415 break;
1416 case Flac:
1417 cmp_buffer = malloc(ctx->current_block_header.length * 2);
1418 if(cmp_buffer == NULL)
1419 {
1420 FATAL("Could not allocate buffer for compressed data");
1422 }
1423 const uint32_t current_samples = ctx->current_block_offset * SAMPLES_PER_SECTOR;
1424 uint32_t flac_block_size = ctx->current_block_offset * SAMPLES_PER_SECTOR;
1425
1426 if(flac_block_size > MAX_FLAKE_BLOCK) flac_block_size = MAX_FLAKE_BLOCK;
1427 if(flac_block_size < MIN_FLAKE_BLOCK) flac_block_size = MIN_FLAKE_BLOCK;
1428
1429 const long remaining = current_samples % flac_block_size;
1430
1431 // Fill FLAC block
1432 if(remaining != 0)
1433 for(int r = 0; r < remaining * 4; r++) ctx->writing_buffer[ctx->writing_buffer_position + r] = 0;
1434
1436 cmp_buffer, ctx->current_block_header.length * 2, ctx->writing_buffer, ctx->current_block_header.length,
1437 flac_block_size, true, false, "hamming", 12, 15, true, false, 0, 8, "Aaru", 4);
1438
1440 {
1442 free(cmp_buffer);
1443 }
1444
1445 break;
1446 case Lzma:
1447 cmp_buffer = malloc(ctx->current_block_header.length * 2);
1448 if(cmp_buffer == NULL)
1449 {
1450 FATAL("Could not allocate buffer for compressed data");
1452 }
1453
1454 size_t dst_size = ctx->current_block_header.length * 2;
1455 size_t props_size = LZMA_PROPERTIES_LENGTH;
1456 aaruf_lzma_encode_buffer(cmp_buffer, &dst_size, ctx->writing_buffer, ctx->current_block_header.length,
1457 lzma_properties, &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1458
1459 ctx->current_block_header.cmpLength = (uint32_t)dst_size;
1460
1462 {
1464 free(cmp_buffer);
1465 }
1466
1467 break;
1468 default:
1469 FATAL("Invalid compression type");
1471 }
1472
1474 {
1477 }
1478 else
1480
1482
1483 // Add to index
1484 TRACE("Adding block to index");
1485 IndexEntry index_entry;
1486 index_entry.blockType = DataBlock;
1487 index_entry.dataType = UserData;
1488 index_entry.offset = ctx->next_block_position;
1489
1490 utarray_push_back(ctx->index_entries, &index_entry);
1491 TRACE("Block added to index at offset %" PRIu64, index_entry.offset);
1492
1493 // Write block header to file
1494
1495 // Move to expected block position
1496 fseek(ctx->imageStream, ctx->next_block_position, SEEK_SET);
1497
1498 // Write block header
1499 if(fwrite(&ctx->current_block_header, sizeof(BlockHeader), 1, ctx->imageStream) != 1)
1501
1502 // Write block data
1504 fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream) != 1)
1505 {
1506 free(cmp_buffer);
1508 }
1509
1511 {
1512 if(fwrite(ctx->writing_buffer, ctx->current_block_header.length, 1, ctx->imageStream) != 1)
1514 }
1515 else
1516 {
1517 if(fwrite(cmp_buffer, ctx->current_block_header.cmpLength, 1, ctx->imageStream) != 1)
1518 {
1519 free(cmp_buffer);
1521 }
1522
1523 free(cmp_buffer);
1524 }
1525
1526 // Update nextBlockPosition to point to the next available aligned position
1527 const uint64_t block_total_size = sizeof(BlockHeader) + ctx->current_block_header.cmpLength;
1528 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1529 ctx->next_block_position = ctx->next_block_position + block_total_size + alignment_mask & ~alignment_mask;
1530 TRACE("Updated nextBlockPosition to %" PRIu64, ctx->next_block_position);
1531
1532 // Clear values
1533 free(ctx->writing_buffer);
1534 ctx->writing_buffer = NULL;
1535 ctx->current_block_offset = 0;
1536 memset(&ctx->current_block_header, 0, sizeof(BlockHeader));
1538 ctx->writing_buffer_position = 0;
1539
1540 return AARUF_STATUS_OK;
1541}
1542
1790AARU_EXPORT int32_t AARU_CALL aaruf_write_media_tag(void *context, const uint8_t *data, const int32_t type,
1791 const uint32_t length)
1792{
1793 TRACE("Entering aaruf_write_media_tag(%p, %p, %d, %d)", context, data, type, length);
1794
1795 // Check context is correct AaruFormat context
1796 if(context == NULL)
1797 {
1798 FATAL("Invalid context");
1799
1800 TRACE("Exiting aaruf_write_media_tag() = AARUF_ERROR_NOT_AARUFORMAT");
1802 }
1803
1804 aaruformat_context *ctx = context;
1805
1806 // Not a libaaruformat context
1807 if(ctx->magic != AARU_MAGIC)
1808 {
1809 FATAL("Invalid context");
1810
1811 TRACE("Exiting aaruf_write_media_tag() = AARUF_ERROR_NOT_AARUFORMAT");
1813 }
1814
1815 // Check we are writing
1816 if(!ctx->is_writing)
1817 {
1818 FATAL("Trying to write a read-only image");
1819
1820 TRACE("Exiting aaruf_write_media_tag() = AARUF_READ_ONLY");
1821 return AARUF_READ_ONLY;
1822 }
1823
1824 if(data == NULL || length == 0)
1825 {
1826 FATAL("Invalid data or length");
1828 }
1829
1830 uint8_t *new_data = malloc(length);
1831
1832 if(new_data == NULL)
1833 {
1834 FATAL("Could not allocate memory for media tag");
1836 }
1837 memcpy(new_data, data, length);
1838
1839 mediaTagEntry *media_tag = malloc(sizeof(mediaTagEntry));
1840 mediaTagEntry *old_media_tag = NULL;
1841
1842 if(media_tag == NULL)
1843 {
1844 TRACE("Cannot allocate memory for media tag entry.");
1845 free(new_data);
1847 }
1848
1849 memset(media_tag, 0, sizeof(mediaTagEntry));
1850
1851 media_tag->type = type;
1852 media_tag->data = new_data;
1853 media_tag->length = length;
1854
1855 HASH_REPLACE_INT(ctx->mediaTags, type, media_tag, old_media_tag);
1856
1857 if(old_media_tag != NULL)
1858 {
1859 TRACE("Replaced media tag with type %d", old_media_tag->type);
1860 free(old_media_tag->data);
1861 free(old_media_tag);
1862 old_media_tag = NULL;
1863 }
1864
1865 TRACE("Exiting aaruf_write_media_tag() = AARUF_STATUS_OK");
1866 return AARUF_STATUS_OK;
1867}
1868
2059AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_tag(void *context, const uint64_t sector_address, const bool negative,
2060 const uint8_t *data, const size_t length, const int32_t tag)
2061{
2062 TRACE("Entering aaruf_write_sector_tag(%p, %" PRIu64 ", %d, %p, %zu, %d)", context, sector_address, negative, data,
2063 length, tag);
2064
2065 // Check context is correct AaruFormat context
2066 if(context == NULL)
2067 {
2068 FATAL("Invalid context");
2069
2070 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_AARUFORMAT");
2072 }
2073
2074 aaruformat_context *ctx = context;
2075
2076 // Not a libaaruformat context
2077 if(ctx->magic != AARU_MAGIC)
2078 {
2079 FATAL("Invalid context");
2080
2081 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_AARUFORMAT");
2083 }
2084
2085 // Check we are writing
2086 if(!ctx->is_writing)
2087 {
2088 FATAL("Trying to write a read-only image");
2089
2090 TRACE("Exiting aaruf_write_sector_tag() = AARUF_READ_ONLY");
2091 return AARUF_READ_ONLY;
2092 }
2093
2094 if(negative && sector_address > ctx->user_data_ddt_header.negative - 1)
2095 {
2096 FATAL("Sector address out of bounds");
2097
2098 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
2100 }
2101
2102 if(!negative && sector_address > ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow - 1)
2103 {
2104 FATAL("Sector address out of bounds");
2105
2106 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
2108 }
2109
2110 if(data == NULL || length == 0)
2111 {
2112 FATAL("Invalid data or length");
2114 }
2115
2116 uint64_t corrected_sector_address = sector_address;
2117
2118 // Calculate positive or negative sector
2119 if(negative)
2120 corrected_sector_address -= ctx->user_data_ddt_header.negative;
2121 else
2122 corrected_sector_address += ctx->user_data_ddt_header.negative;
2123
2124 const uint64_t total_sectors =
2126
2127 switch(tag)
2128 {
2129 case CdTrackFlags:
2131 {
2132 FATAL("Invalid media type for tag");
2133 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2135 }
2136
2137 if(length != 1)
2138 {
2139 FATAL("Incorrect tag size");
2140 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2142 }
2143
2144 for(int i = 0; i < ctx->tracks_header.entries; i++)
2145 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
2146 {
2147 ctx->track_entries[i].flags = data[0];
2148 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2149 return AARUF_STATUS_OK;
2150 }
2151
2152 FATAL("Track not found");
2154 case CdTrackIsrc:
2156 {
2157 FATAL("Invalid media type for tag");
2158 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2160 }
2161
2162 if(length != 12)
2163 {
2164 FATAL("Incorrect tag size");
2165 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2167 }
2168
2169 for(int i = 0; i < ctx->tracks_header.entries; i++)
2170 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
2171 {
2172 memcpy(ctx->track_entries[i].isrc, data, 12);
2173 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2174 return AARUF_STATUS_OK;
2175 }
2176
2177 FATAL("Track not found");
2181 {
2182 FATAL("Invalid media type for tag");
2183 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2185 }
2186
2187 if(length != 96)
2188 {
2189 FATAL("Incorrect tag size");
2190 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2192 }
2193
2194 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 96 * total_sectors);
2195
2196 if(ctx->sector_subchannel == NULL)
2197 {
2198 FATAL("Could not allocate memory for sector subchannel");
2199
2200 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2202 }
2203
2204 memcpy(ctx->sector_subchannel + corrected_sector_address * 96, data, 96);
2205 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2206 return AARUF_STATUS_OK;
2207 case DvdCmi:
2209 {
2210 FATAL("Invalid media type for tag");
2211 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2213 }
2214
2215 if(length != 1)
2216 {
2217 FATAL("Incorrect tag size");
2218 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2220 }
2221
2222 if(ctx->sector_cpr_mai == NULL) ctx->sector_cpr_mai = calloc(1, 6 * total_sectors);
2223
2224 if(ctx->sector_cpr_mai == NULL)
2225 {
2226 FATAL("Could not allocate memory for sector CPR/MAI");
2227
2228 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2230 }
2231
2232 memcpy(ctx->sector_cpr_mai + corrected_sector_address * 6, data, 1);
2233 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2234 return AARUF_STATUS_OK;
2237 {
2238 FATAL("Invalid media type for tag");
2239 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2241 }
2242
2243 if(length != 1)
2244 {
2245 FATAL("Incorrect tag size");
2246 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2248 }
2249
2250 if(ctx->sector_id == NULL) ctx->sector_id = calloc(1, 4 * total_sectors);
2251
2252 if(ctx->sector_id == NULL)
2253 {
2254 FATAL("Could not allocate memory for sector ID");
2255
2256 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2258 }
2259
2260 memcpy(ctx->sector_id + corrected_sector_address * 4, data, 1);
2261 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2262 return AARUF_STATUS_OK;
2263 case DvdSectorNumber:
2265 {
2266 FATAL("Invalid media type for tag");
2267 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2269 }
2270
2271 if(length != 3)
2272 {
2273 FATAL("Incorrect tag size");
2274 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2276 }
2277
2278 if(ctx->sector_id == NULL) ctx->sector_id = calloc(1, 4 * total_sectors);
2279
2280 if(ctx->sector_id == NULL)
2281 {
2282 FATAL("Could not allocate memory for sector ID");
2283
2284 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2286 }
2287
2288 memcpy(ctx->sector_id + corrected_sector_address * 4 + 1, data, 3);
2289 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2290 return AARUF_STATUS_OK;
2291 case DvdSectorIedAaru:
2293 {
2294 FATAL("Invalid media type for tag");
2295 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2297 }
2298
2299 if(length != 2)
2300 {
2301 FATAL("Incorrect tag size");
2302 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2304 }
2305
2306 if(ctx->sector_ied == NULL) ctx->sector_ied = calloc(1, 2 * total_sectors);
2307
2308 if(ctx->sector_ied == NULL)
2309 {
2310 FATAL("Could not allocate memory for sector IED");
2311
2312 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2314 }
2315
2316 memcpy(ctx->sector_ied + corrected_sector_address * 2, data, 2);
2317 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2318 return AARUF_STATUS_OK;
2319 case DvdSectorEdcAaru:
2321 {
2322 FATAL("Invalid media type for tag");
2323 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2325 }
2326
2327 if(length != 4)
2328 {
2329 FATAL("Incorrect tag size");
2330 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2332 }
2333
2334 if(ctx->sector_edc == NULL) ctx->sector_edc = calloc(1, 4 * total_sectors);
2335
2336 if(ctx->sector_edc == NULL)
2337 {
2338 FATAL("Could not allocate memory for sector EDC");
2339
2340 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2342 }
2343
2344 memcpy(ctx->sector_edc + corrected_sector_address * 4, data, 4);
2345 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2346 return AARUF_STATUS_OK;
2349 {
2350 FATAL("Invalid media type for tag");
2351 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2353 }
2354
2355 if(length != 5)
2356 {
2357 FATAL("Incorrect tag size");
2358 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2360 }
2361
2362 if(ctx->sector_decrypted_title_key == NULL) ctx->sector_decrypted_title_key = calloc(1, 5 * total_sectors);
2363
2364 if(ctx->sector_decrypted_title_key == NULL)
2365 {
2366 FATAL("Could not allocate memory for sector decrypted title key");
2367
2368 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2370 }
2371
2372 memcpy(ctx->sector_decrypted_title_key + corrected_sector_address * 5, data, 5);
2373 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2374 return AARUF_STATUS_OK;
2375 case AppleSonyTagAaru:
2377 {
2378 FATAL("Invalid media type for tag");
2379 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2381 }
2382
2383 if(length != 12)
2384 {
2385 FATAL("Incorrect tag size");
2386 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2388 }
2389
2390 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 12 * total_sectors);
2391
2392 if(ctx->sector_subchannel == NULL)
2393 {
2394 FATAL("Could not allocate memory for Apple Sony tag");
2395
2396 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2398 }
2399
2400 memcpy(ctx->sector_subchannel + corrected_sector_address * 12, data, 12);
2401 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2402 return AARUF_STATUS_OK;
2405 {
2406 FATAL("Invalid media type for tag");
2407 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2409 }
2410
2411 if(length != 20)
2412 {
2413 FATAL("Incorrect tag size");
2414 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2416 }
2417
2418 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 20 * total_sectors);
2419
2420 if(ctx->sector_subchannel == NULL)
2421 {
2422 FATAL("Could not allocate memory for Apple Profile tag");
2423
2424 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2426 }
2427
2428 memcpy(ctx->sector_subchannel + corrected_sector_address * 20, data, 20);
2429 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2430 return AARUF_STATUS_OK;
2433 {
2434 FATAL("Invalid media type for tag");
2435 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2437 }
2438
2439 if(length != 24)
2440 {
2441 FATAL("Incorrect tag size");
2442 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2444 }
2445
2446 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 24 * total_sectors);
2447
2448 if(ctx->sector_subchannel == NULL)
2449 {
2450 FATAL("Could not allocate memory for Priam Data Tower tag");
2451
2452 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2454 }
2455
2456 memcpy(ctx->sector_subchannel + corrected_sector_address * 24, data, 24);
2457 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2458 return AARUF_STATUS_OK;
2459 default:
2460 TRACE("Do not know how to write sector tag %d", tag);
2462 }
2463}
#define MAX_FLAKE_BLOCK
FLAC maximum block size used for encoding audio sectors.
Definition consts.h:94
#define LZMA_PROPERTIES_LENGTH
Size in bytes of the fixed LZMA properties header (lc/lp/pb + dictionary size).
Definition consts.h:82
#define AARU_MAGIC
Magic identifier for AaruFormat container (ASCII "AARUFRMT").
Definition consts.h:64
#define MIN_FLAKE_BLOCK
FLAC minimum block size.
Definition consts.h:96
#define SAMPLES_PER_SECTOR
Red Book (CD‑DA) PCM samples per 2352‑byte sector: 44,100 Hz / 75 sectors per second = 588 samples.
Definition consts.h:90
#define AARU_CALL
Definition decls.h:45
int32_t aaruf_lzma_encode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t *src_buffer, size_t src_size, uint8_t *out_props, size_t *out_props_size, int32_t level, uint32_t dict_size, int32_t lc, int32_t lp, int32_t pb, int32_t fb, int32_t num_threads)
Encodes a buffer using LZMA compression.
Definition lzma.c:65
uint64_t aaruf_crc64_data(const uint8_t *data, uint32_t len)
Definition crc64.c:160
size_t aaruf_flac_encode_redbook_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer, size_t src_size, uint32_t blocksize, int32_t do_mid_side_stereo, int32_t loose_mid_side_stereo, const char *apodization, uint32_t max_lpc_order, uint32_t qlp_coeff_precision, int32_t do_qlp_coeff_prec_search, int32_t do_exhaustive_model_search, uint32_t min_residual_partition_order, uint32_t max_residual_partition_order, const char *application_id, uint32_t application_id_len)
Encodes a Red Book audio buffer to FLAC format.
Definition flac.c:175
int aaruf_crc64_update(crc64_ctx *ctx, const uint8_t *data, uint32_t len)
Updates the CRC64 context with new data.
Definition crc64.c:55
void aaruf_crc64_free(crc64_ctx *ctx)
Frees a CRC64 context.
Definition crc64.c:155
int aaruf_spamsum_update(spamsum_ctx *ctx, const uint8_t *data, uint32_t len)
Updates the spamsum context with new data.
Definition spamsum.c:59
crc64_ctx * aaruf_crc64_init()
Initializes a CRC64 context.
Definition crc64.c:32
uint32_t aaruf_edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, int size, int pos)
Computes the EDC (Error Detection Code) for a CD sector.
Definition ecc_cd.c:547
void aaruf_md5_update(md5_ctx *ctx, const void *data, unsigned long size)
Definition md5.c:447
#define AARU_EXPORT
Definition decls.h:54
void aaruf_sha256_update(sha256_ctx *ctx, const void *data, unsigned long size)
Definition sha256.c:90
bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
Checks if the suffix (EDC/ECC) of a CD sector is correct (Mode 2).
Definition ecc_cd.c:165
void aaruf_sha1_update(sha1_ctx *ctx, const void *data, unsigned long size)
Definition sha1.c:89
int aaruf_crc64_final(crc64_ctx *ctx, uint64_t *crc)
Computes the final CRC64 value from the context.
Definition crc64.c:141
bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
Checks if the suffix (EDC/ECC) of a CD sector is correct (Mode 1).
Definition ecc_cd.c:101
@ DataBlock
Block containing data.
Definition enums.h:141
@ SectorStatusNotDumped
Sector(s) not yet acquired during image dumping.
Definition enums.h:230
@ SectorStatusMode2Form2NoCrc
Suffix matches MODE 2 Form 2 but CRC empty/missing.
Definition enums.h:236
@ SectorStatusMode1Correct
Valid MODE 1 data with regenerable suffix/prefix.
Definition enums.h:233
@ SectorStatusMode2Form2Ok
Suffix matches MODE 2 Form 2 with valid CRC.
Definition enums.h:235
@ SectorStatusErrored
Error during dumping; data may be incomplete or corrupt.
Definition enums.h:232
@ SectorStatusMode2Form1Ok
Suffix verified/regenerable for MODE 2 Form 1.
Definition enums.h:234
@ OpticalDisc
Purely optical discs.
Definition enums.h:218
@ BlockMedia
Media that is physically block-based or abstracted like that.
Definition enums.h:219
@ CdMode1
Compact Disc Mode 1 data track.
Definition enums.h:197
@ Data
Generic data track (not further specified).
Definition enums.h:196
@ CdMode2Form2
Compact Disc Mode 2 Form 2 data track.
Definition enums.h:200
@ Audio
Audio track.
Definition enums.h:195
@ CdMode2Form1
Compact Disc Mode 2 Form 1 data track.
Definition enums.h:199
@ CdMode2Formless
Compact Disc Mode 2 (formless) data track.
Definition enums.h:198
@ UserData
User (main) data.
Definition enums.h:46
@ Lzma
LZMA compression.
Definition enums.h:34
@ None
Not compressed.
Definition enums.h:33
@ Flac
FLAC compression.
Definition enums.h:35
#define AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA
Failure writing block payload.
Definition errors.h:63
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:75
#define AARUF_READ_ONLY
Operation requires write mode but context is read-only.
Definition errors.h:61
#define AARUF_ERROR_INCORRECT_MEDIA_TYPE
Operation incompatible with image media type.
Definition errors.h:51
#define AARUF_ERROR_TRACK_NOT_FOUND
Referenced track number not present.
Definition errors.h:52
#define AARUF_ERROR_NOT_ENOUGH_MEMORY
Memory allocation failure (critical).
Definition errors.h:48
#define AARUF_ERROR_SECTOR_OUT_OF_BOUNDS
Requested logical sector outside media bounds.
Definition errors.h:44
#define AARUF_ERROR_INCORRECT_DATA_SIZE
Data size does not match expected size.
Definition errors.h:65
#define AARUF_ERROR_CANNOT_SET_DDT_ENTRY
Failed to encode/store a DDT entry (overflow or IO).
Definition errors.h:64
#define AARUF_ERROR_NOT_AARUFORMAT
Input file/stream failed magic or structural validation.
Definition errors.h:40
#define AARUF_ERROR_INVALID_TAG
Invalid or unsupported media or sector tag format.
Definition errors.h:66
#define AARUF_ERROR_CANNOT_WRITE_BLOCK_HEADER
Failure writing block header.
Definition errors.h:62
@ AppleProfile
Definition aaru.h:698
@ VideoNowColor
Hasbro VideoNow Color disc.
Definition aaru.h:772
@ AppleSonySS
3.5", SS, DD, 80 tracks, 8 to 12 spt, 512 bytes/sector, GCR
Definition aaru.h:247
@ DVDPRWDL
DVD+RW DL.
Definition aaru.h:141
@ DVDRW
DVD-RW.
Definition aaru.h:138
@ DVDRWDL
DVD-RW DL.
Definition aaru.h:145
@ SACD
Super Audio CD (Scarlet Book)
Definition aaru.h:121
@ DVDPRDL
DVD+R DL.
Definition aaru.h:143
@ DVDR
DVD-R.
Definition aaru.h:137
@ PS3DVD
Sony PlayStation 3 game DVD.
Definition aaru.h:206
@ JaguarCD
Atari Jaguar CD.
Definition aaru.h:232
@ PS2DVD
Sony PlayStation 2 game DVD.
Definition aaru.h:205
@ VideoNow
Hasbro VideoNow 85 mm proprietary video disc.
Definition aaru.h:771
@ VideoNowXp
Hasbro VideoNow XP higher capacity disc.
Definition aaru.h:773
@ PriamDataTower
Definition aaru.h:701
@ AppleFileWare
5.25", DS, ?D, ?? tracks, ?? spt, 512 bytes/sector, GCR, opposite side heads, aka Twiggy
Definition aaru.h:249
@ DVDPR
DVD+R.
Definition aaru.h:139
@ AppleWidget
Definition aaru.h:699
@ DVDPRW
DVD+RW.
Definition aaru.h:140
@ Nuon
Nuon (DVD based videogame console)
Definition aaru.h:238
@ DVDDownload
DVD-Download.
Definition aaru.h:146
@ DVDRDL
DVD-R DL.
Definition aaru.h:142
@ AppleSonyDS
3.5", DS, DD, 80 tracks, 8 to 12 spt, 512 bytes/sector, GCR
Definition aaru.h:248
@ DVDROM
DVD-ROM (applies to DVD Video and DVD Audio)
Definition aaru.h:136
@ DVDRAM
DVD-RAM.
Definition aaru.h:144
@ DvdSectorIedAaru
DVD sector ID error detection, 2 bytes.
Definition aaru.h:914
@ CdTrackFlags
Track flags (audio/data, copy permitted, pre-emphasis)
Definition aaru.h:907
@ DvdSectorEdcAaru
DVD sector EDC, 4 bytes.
Definition aaru.h:915
@ DvdCmi
DVD Copyright Management Information (CSS)
Definition aaru.h:908
@ CdSectorSubchannelAaru
96 raw subchannel bytes (P-W)
Definition aaru.h:904
@ DvdSectorNumber
DVD sector number, 3 bytes.
Definition aaru.h:913
@ CdTrackIsrc
Track ISRC (12 ASCII chars, no terminator)
Definition aaru.h:905
@ PriamDataTowerTagAaru
Priam DataTower sector tags, 24 bytes.
Definition aaru.h:917
@ AppleProfileTagAaru
Apple's Profile sector tags, 20 bytes.
Definition aaru.h:916
@ DvdSectorInformation
DVD sector information, 1 bytes.
Definition aaru.h:912
@ DvdTitleKeyDecrypted
Decrypted DVD sector title key, 5 bytes.
Definition aaru.h:911
@ AppleSonyTagAaru
Apple's Sony sector tags, 12 bytes (address prolog + checksum)
Definition aaru.h:896
bool lookup_map(const hash_map_t *map, uint64_t key, uint64_t *out_value)
Looks up a value by key in the hash map.
Definition hash_map.c:196
bool insert_map(hash_map_t *map, uint64_t key, uint64_t value)
Inserts a key-value pair into the hash map.
Definition hash_map.c:153
bool set_ddt_entry_v2(aaruformat_context *ctx, uint64_t sector_address, bool negative, uint64_t offset, uint64_t block_offset, uint8_t sector_status, uint64_t *ddt_entry)
Sets a DDT v2 entry for a given sector address.
Definition ddt_v2.c:988
Structure definitions and conversion/serialization function declarations for Lisa family disk tags.
uint8_t * priam_tag_to_bytes(priam_tag tag)
Serialize a priam_tag into a newly allocated 24-byte big-endian on-disk representation.
Definition lisa_tag.c:409
priam_tag bytes_to_priam_tag(const uint8_t *bytes)
Parse a 24-byte Priam tag record into a priam_tag structure.
Definition lisa_tag.c:112
sony_tag profile_tag_to_sony(profile_tag tag)
Convert a profile_tag to a sony_tag.
Definition lisa_tag.c:257
uint8_t * sony_tag_to_bytes(sony_tag tag)
Serialize a sony_tag into a newly allocated 12-byte big-endian on-disk representation.
Definition lisa_tag.c:466
profile_tag priam_tag_to_profile(priam_tag tag)
Convert a priam_tag to a profile_tag.
Definition lisa_tag.c:325
uint8_t * profile_tag_to_bytes(profile_tag tag)
Serialize a profile_tag into a newly allocated 20-byte big-endian on-disk representation.
Definition lisa_tag.c:357
profile_tag sony_tag_to_profile(sony_tag tag)
Convert a sony_tag to a profile_tag representation.
Definition lisa_tag.c:173
sony_tag bytes_to_sony_tag(const uint8_t *bytes)
Parse a 12-byte Sony tag record into a sony_tag structure.
Definition lisa_tag.c:82
profile_tag bytes_to_profile_tag(const uint8_t *bytes)
Parse a 20-byte Profile tag record into a profile_tag structure.
Definition lisa_tag.c:142
priam_tag sony_tag_to_priam(sony_tag tag)
Convert a sony_tag to a priam_tag representation.
Definition lisa_tag.c:201
priam_tag profile_tag_to_priam(profile_tag tag)
Convert a profile_tag to a priam_tag.
Definition lisa_tag.c:228
sony_tag priam_tag_to_sony(priam_tag tag)
Convert a priam_tag to a sony_tag.
Definition lisa_tag.c:291
#define FATAL(fmt,...)
Definition log.h:40
#define TRACE(fmt,...)
Definition log.h:25
Header preceding the compressed data payload of a data block (BlockType::DataBlock).
Definition data.h:71
uint32_t cmpLength
Size in bytes of the compressed payload immediately following this header.
Definition data.h:76
uint32_t length
Size in bytes of the uncompressed payload resulting after decompression.
Definition data.h:77
uint32_t identifier
Block identifier, must be BlockType::DataBlock.
Definition data.h:72
uint32_t sectorSize
Size in bytes of each logical sector represented in this block.
Definition data.h:75
uint64_t cmpCrc64
CRC64-ECMA of the compressed payload (cmpLength bytes).
Definition data.h:78
uint64_t crc64
CRC64-ECMA of the uncompressed payload (length bytes).
Definition data.h:79
uint16_t type
Logical data classification (value from DataType).
Definition data.h:73
uint16_t compression
Compression algorithm used (value from CompressionType).
Definition data.h:74
uint16_t overflow
Trailing dumped sectors beyond user area (overflow range), still mapped with entries.
Definition ddt.h:151
uint16_t negative
Leading negative LBA count; added to external L to build internal index.
Definition ddt.h:149
uint8_t blockAlignmentShift
2^blockAlignmentShift = block alignment boundary in bytes.
Definition ddt.h:154
uint8_t dataShift
2^dataShift = sectors represented per increment in blockIndex field.
Definition ddt.h:155
uint32_t MediaType
Media type identifier (see MediaType enum; 0=Unknown)
Definition aaru.h:881
uint8_t MetadataMediaType
Media type for sidecar generation (internal archival use)
Definition aaru.h:882
uint64_t Sectors
Total count of addressable logical sectors/blocks.
Definition aaru.h:874
Single index entry describing a block's type, (optional) data classification, and file offset.
Definition index.h:109
uint32_t blockType
Block identifier of the referenced block (value from BlockType).
Definition index.h:110
uint64_t offset
Absolute byte offset in the image where the referenced block header begins.
Definition index.h:112
uint16_t dataType
Data classification (value from DataType) or unused for untyped blocks.
Definition index.h:111
Single optical disc track descriptor (sequence, type, LBAs, session, ISRC, flags).
Definition optical.h:72
uint8_t session
Session number (1-based). 1 for single-session discs.
Definition optical.h:78
uint8_t flags
Control / attribute bitfield (see file documentation for suggested bit mapping).
Definition optical.h:80
int64_t end
Inclusive ending LBA of the track.
Definition optical.h:76
uint8_t sequence
Track number (1..99 typical for CD audio/data). 0 may indicate placeholder/non-standard.
Definition optical.h:73
int64_t start
Inclusive starting LBA of the track.
Definition optical.h:75
uint8_t type
Track type (value from TrackType).
Definition optical.h:74
uint8_t isrc[13]
ISRC raw 13-byte code (no null terminator). All zeros if not present.
Definition optical.h:79
uint16_t entries
Number of TrackEntry records following this header.
Definition optical.h:64
Master context representing an open or in‑creation Aaru image.
Definition context.h:172
DdtHeader2 user_data_ddt_header
Active user data DDT v2 header (primary table meta).
Definition context.h:189
bool deduplicate
Storage deduplication active (duplicates coalesce).
Definition context.h:298
size_t sector_suffix_length
Length of sector_suffix.
Definition context.h:285
bool compression_enabled
True if block compression enabled (writing path).
Definition context.h:299
uint64_t last_written_block
Last written block number (write path).
Definition context.h:283
uint8_t * sector_cpr_mai
DVD sector CPR_MAI (6 bytes) if present.
Definition context.h:207
hash_map_t * sector_hash_map
Deduplication hash map (fingerprint->entry mapping).
Definition context.h:253
sha256_ctx sha256_context
Opaque SHA-256 context for streaming updates.
Definition context.h:272
bool calculating_sha256
True if whole-image SHA-256 being calculated on-the-fly.
Definition context.h:275
uint8_t * sector_ied
DVD sector IED (2 bytes) if present.
Definition context.h:206
md5_ctx md5_context
Opaque MD5 context for streaming updates.
Definition context.h:270
uint8_t * sector_prefix
Raw per-sector prefix (e.g., sync+header) uncorrected.
Definition context.h:199
uint64_t * sector_suffix_ddt2
CD sector suffix DDT V2.
Definition context.h:186
uint8_t * sector_edc
DVD sector EDC (4 bytes) if present.
Definition context.h:208
bool calculating_sha1
True if whole-image SHA-1 being calculated on-the-fly.
Definition context.h:274
CdEccContext * ecc_cd_context
CD ECC/EDC helper tables (allocated on demand).
Definition context.h:248
bool rewinded
True if stream has been rewound after open (write path).
Definition context.h:293
uint8_t * sector_suffix
Raw per-sector suffix (EDC/ECC) uncorrected.
Definition context.h:201
int current_block_offset
Logical offset inside block (units: bytes or sectors depending on path).
Definition context.h:288
bool is_writing
True if context opened/created for writing.
Definition context.h:292
spamsum_ctx * spamsum_context
Opaque SpamSum context for streaming updates.
Definition context.h:267
size_t sector_prefix_offset
Current position in sector_prefix.
Definition context.h:286
BlockHeader current_block_header
Header for block currently being assembled (write path).
Definition context.h:281
uint64_t magic
File magic (AARU_MAGIC) post-open.
Definition context.h:174
uint8_t * writing_buffer
Accumulation buffer for current block data.
Definition context.h:280
uint64_t * sector_prefix_ddt2
CD sector prefix DDT V2.
Definition context.h:185
bool calculating_spamsum
True if whole-image SpamSum being calculated on-the-fly.
Definition context.h:276
size_t sector_prefix_length
Length of sector_prefix.
Definition context.h:284
mediaTagEntry * mediaTags
Hash table of extra media tags (uthash root).
Definition context.h:264
blake3_hasher * blake3_context
Opaque BLAKE3 context for streaming updates.
Definition context.h:268
bool calculating_blake3
True if whole-image BLAKE3 being calculated on-the-fly.
Definition context.h:277
uint64_t next_block_position
Absolute file offset where next block will be written.
Definition context.h:282
bool calculating_md5
True if whole-image MD5 being calculated on-the-fly.
Definition context.h:273
size_t sector_suffix_offset
Current position in sector_suffix.
Definition context.h:287
uint8_t * sector_decrypted_title_key
DVD decrypted title key (5 bytes) if present.
Definition context.h:209
int writing_buffer_position
Current size / position within writingBuffer.
Definition context.h:289
crc64_ctx * crc64_context
Opaque CRC64 context for streaming updates.
Definition context.h:249
uint8_t * sector_subchannel
Raw 96-byte subchannel (if captured).
Definition context.h:203
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:176
UT_array * index_entries
Flattened index entries (UT_array of IndexEntry).
Definition context.h:252
uint8_t * mode2_subheaders
MODE2 Form1/Form2 8-byte subheaders (concatenated).
Definition context.h:204
ImageInfo image_info
Exposed high-level image info summary.
Definition context.h:260
uint8_t * sector_id
DVD sector ID (4 bytes) if present.
Definition context.h:205
sha1_ctx sha1_context
Opaque SHA-1 context for streaming updates.
Definition context.h:271
uint32_t lzma_dict_size
LZMA dictionary size (writing path).
Definition context.h:297
TrackEntry * track_entries
Full track list (tracksHeader.entries elements).
Definition context.h:242
uint8_t current_track_type
Current track type (when writing optical images with tracks, needed for block compression type).
Definition context.h:290
bool writing_long
True if writing long sectors.
Definition context.h:294
TracksHeader tracks_header
Tracks header (optical) if present.
Definition context.h:244
Hash table entry for an arbitrary media tag (e.g., proprietary drive/medium descriptor).
Definition context.h:119
uint8_t * data
Tag data blob (opaque to library core); length bytes long.
Definition context.h:120
int32_t type
Numeric type identifier.
Definition context.h:121
uint32_t length
Length in bytes of data.
Definition context.h:122
int32_t aaruf_close_current_block(aaruformat_context *ctx)
Finalizes and writes the current data block to the AaruFormat image file.
Definition write.c:1393
int32_t aaruf_write_sector_tag(void *context, const uint64_t sector_address, const bool negative, const uint8_t *data, const size_t length, const int32_t tag)
Writes per-sector tag data (auxiliary metadata) for a specific sector.
Definition write.c:2059
int32_t aaruf_write_media_tag(void *context, const uint8_t *data, const int32_t type, const uint32_t length)
Writes a media tag to the AaruFormat image, storing medium-specific metadata and descriptors.
Definition write.c:1790
int32_t aaruf_write_sector(void *context, uint64_t sector_address, bool negative, const uint8_t *data, uint8_t sector_status, uint32_t length)
Writes a sector to the AaruFormat image.
Definition write.c:98
int32_t aaruf_write_sector_long(void *context, uint64_t sector_address, bool negative, const uint8_t *data, uint8_t sector_status, uint32_t length)
Writes a full ("long") raw sector from optical or block media, parsing structure and validating conte...
Definition write.c:532