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 if(sector_address == 0 && !ctx->block_zero_written)
154 ctx->block_zero_written = true;
155 else
156 {
157 TRACE("Rewinded");
158 ctx->rewinded = true;
159
160 // Disable MD5 calculation
161 if(ctx->calculating_md5) ctx->calculating_md5 = false;
162 // Disable SHA1 calculation
163 if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
164 // Disable SHA256 calculation
165 if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
166 // Disable SpamSum calculation
167 if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
168 // Disable BLAKE3 calculation
169 if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
170 }
171 }
172 else
173 ctx->last_written_block = sector_address;
174 }
175
176 // Calculate MD5 on-the-fly if requested and sector is within user sectors (not negative or overflow)
177 if(ctx->calculating_md5 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
178 aaruf_md5_update(&ctx->md5_context, data, length);
179 // Calculate SHA1 on-the-fly if requested and sector is within user sectors (not negative or overflow)
180 if(ctx->calculating_sha1 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
181 aaruf_sha1_update(&ctx->sha1_context, data, length);
182 // Calculate SHA256 on-the-fly if requested and sector is within user sectors (not negative or overflow)
183 if(ctx->calculating_sha256 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
184 aaruf_sha256_update(&ctx->sha256_context, data, length);
185 // Calculate SpamSum on-the-fly if requested and sector is within user sectors (not negative or overflow)
186 if(ctx->calculating_spamsum && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
187 aaruf_spamsum_update(ctx->spamsum_context, data, length);
188 // Calculate BLAKE3 on-the-fly if requested and sector is within user sectors (not negative or overflow)
189 if(ctx->calculating_blake3 && !negative && sector_address <= ctx->image_info.Sectors && !ctx->writing_long)
190 blake3_hasher_update(ctx->blake3_context, data, length);
191
192 // Close current block first
193 if(ctx->writing_buffer != NULL &&
194 // When sector size changes or block reaches maximum size
195 (ctx->current_block_header.sectorSize != length ||
197 {
198 TRACE("Closing current block before writing new data");
199 int error = aaruf_close_current_block(ctx);
200
201 if(error != AARUF_STATUS_OK)
202 {
203 FATAL("Error closing current block: %d", error);
204
205 TRACE("Exiting aaruf_write_sector() = %d", error);
206 return error;
207 }
208 }
209
210 uint64_t ddt_entry = 0;
211 bool ddt_ok;
212
213 if(ctx->deduplicate)
214 {
215 // Calculate 64-bit XXH3 hash of the sector
216 TRACE("Hashing sector data for deduplication");
217 uint64_t hash = XXH3_64bits(data, length);
218
219 // Check if the hash is already in the map
220 bool existing = lookup_map(ctx->sector_hash_map, hash, &ddt_entry);
221 TRACE("Block does %s exist in deduplication map", existing ? "already" : "not yet");
222
223 ddt_ok = set_ddt_entry_v2(ctx, sector_address, negative, ctx->current_block_offset, ctx->next_block_position,
224 sector_status, &ddt_entry);
225 if(!ddt_ok)
226 {
227 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_CANNOT_SET_DDT_ENTRY");
229 }
230
231 if(existing)
232 {
233 TRACE("Sector exists, so not writing to image");
234 TRACE("Exiting aaruf_write_sector() = AARUF_STATUS_OK");
235 return AARUF_STATUS_OK;
236 }
237
238 TRACE("Inserting sector hash into deduplication map, proceeding to write into image as normal");
239 insert_map(ctx->sector_hash_map, hash, ddt_entry);
240 }
241 else
242 ddt_ok = set_ddt_entry_v2(ctx, sector_address, negative, ctx->current_block_offset, ctx->next_block_position,
243 sector_status, &ddt_entry);
244
245 if(!ddt_ok)
246 {
247 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_CANNOT_SET_DDT_ENTRY");
249 }
250
251 // No block set
252 if(ctx->writing_buffer_position == 0)
253 {
254 TRACE("Creating new writing block");
257 ctx->current_block_header.sectorSize = length;
258
259 // We need to save the track type for later compression
260 if(ctx->image_info.MetadataMediaType == OpticalDisc && ctx->track_entries != NULL)
261 {
262 const TrackEntry *track = NULL;
263 for(int i = 0; i < ctx->tracks_header.entries; i++)
264 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
265 {
266 track = &ctx->track_entries[i];
267 break;
268 }
269
270 if(track != NULL)
271 {
272 ctx->current_track_type = track->type;
273
274 if(track->sequence == 0 && track->start == 0 && track->end == 0) ctx->current_track_type = Data;
275 }
276 else
278
279 if(ctx->current_track_type == Audio &&
280 // JaguarCD stores data in audio tracks. FLAC is too inefficient, we need to use LZMA as data.
281 (ctx->image_info.MediaType == JaguarCD && track->session > 1 ||
282 // VideoNow stores video in audio tracks, and LZMA works better too.
286
287 if(ctx->compression_enabled)
288 {
289 if(ctx->current_track_type == Audio)
291 else
293 }
294 else
296 }
297 else
298 {
300 if(ctx->compression_enabled)
302 else
304 }
305
306 uint32_t max_buffer_size = (1 << ctx->user_data_ddt_header.dataShift) * ctx->current_block_header.sectorSize * 2;
307 TRACE("Setting max buffer size to %u bytes", max_buffer_size);
308
309 TRACE("Allocating memory for writing buffer");
310 ctx->writing_buffer = (uint8_t *)calloc(1, max_buffer_size);
311 if(ctx->writing_buffer == NULL)
312 {
313 FATAL("Could not allocate memory");
314
315 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
317 }
318 }
319
320 TRACE("Copying data to writing buffer at position %zu", ctx->writing_buffer_position);
321 memcpy(ctx->writing_buffer + ctx->writing_buffer_position, data, length);
322 TRACE("Advancing writing buffer position to %zu", ctx->writing_buffer_position + length);
323 ctx->writing_buffer_position += length;
324 TRACE("Advancing current block offset to %zu", ctx->current_block_offset + 1);
326
327 TRACE("Exiting aaruf_write_sector() = AARUF_STATUS_OK");
328 return AARUF_STATUS_OK;
329}
330
537AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_long(void *context, uint64_t sector_address, bool negative,
538 const uint8_t *data, uint8_t sector_status, uint32_t length)
539{
540 TRACE("Entering aaruf_write_sector_long(%p, %" PRIu64 ", %d, %p, %u, %u)", context, sector_address, negative, data,
541 sector_status, length);
542
543 // Check context is correct AaruFormat context
544 if(context == NULL)
545 {
546 FATAL("Invalid context");
547
548 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
550 }
551
552 aaruformat_context *ctx = context;
553
554 // Not a libaaruformat context
555 if(ctx->magic != AARU_MAGIC)
556 {
557 FATAL("Invalid context");
558
559 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
561 }
562
563 // Check we are writing
564 if(!ctx->is_writing)
565 {
566 FATAL("Trying to write a read-only image");
567
568 TRACE("Exiting aaruf_write_sector() = AARUF_READ_ONLY");
569 return AARUF_READ_ONLY;
570 }
571
572 if(negative && sector_address > ctx->user_data_ddt_header.negative - 1)
573 {
574 FATAL("Sector address out of bounds");
575
576 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
578 }
579
580 if(!negative && sector_address > ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow - 1)
581 {
582 FATAL("Sector address out of bounds");
583
584 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
586 }
587
588 switch(ctx->image_info.MetadataMediaType)
589 {
590 case OpticalDisc:
591 {
592 TrackEntry track = {0};
593
594 for(int i = 0; i < ctx->tracks_header.entries; i++)
595 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
596 {
597 track = ctx->track_entries[i];
598 break;
599 }
600
601 if(track.sequence == 0 && track.start == 0 && track.end == 0) track.type = Data;
602
603 uint64_t corrected_sector_address = sector_address;
604
605 // Calculate positive or negative sector
606 if(negative)
607 corrected_sector_address -= ctx->user_data_ddt_header.negative;
608 else
609 corrected_sector_address += ctx->user_data_ddt_header.negative;
610
611 uint64_t total_sectors =
613
614 // DVD long sector
615 if(length == 2064 && (ctx->image_info.MediaType == DVDROM || ctx->image_info.MediaType == PS2DVD ||
616 ctx->image_info.MediaType == SACD || ctx->image_info.MediaType == PS3DVD ||
617 ctx->image_info.MediaType == DVDR || ctx->image_info.MediaType == DVDRW ||
622 ctx->image_info.MediaType == Nuon))
623 {
624 if(ctx->sector_id == NULL) ctx->sector_id = calloc(1, 4 * total_sectors);
625 if(ctx->sector_ied == NULL) ctx->sector_ied = calloc(1, 2 * total_sectors);
626 if(ctx->sector_cpr_mai == NULL) ctx->sector_cpr_mai = calloc(1, 6 * total_sectors);
627 if(ctx->sector_edc == NULL) ctx->sector_edc = calloc(1, 4 * total_sectors);
628
629 memcpy(ctx->sector_id + corrected_sector_address * 4, data, 4);
630 memcpy(ctx->sector_ied + corrected_sector_address * 2, data + 4, 2);
631 memcpy(ctx->sector_cpr_mai + corrected_sector_address * 6, data + 6, 6);
632 memcpy(ctx->sector_edc + corrected_sector_address * 4, data + 2060, 4);
633
634 return aaruf_write_sector(context, sector_address, negative, data + 12, sector_status, 2048);
635 }
636
637 if(length != 2352)
638 {
639 FATAL("Incorrect sector size");
640 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_INCORRECT_DATA_SIZE");
642 }
643
644 ctx->writing_long = true;
645
646 if(!ctx->rewinded)
647 {
648 if(sector_address <= ctx->last_written_block)
649 {
650 if(sector_address == 0 && !ctx->block_zero_written)
651 ctx->block_zero_written = true;
652 else
653 {
654 TRACE("Rewinded");
655 ctx->rewinded = true;
656
657 // Disable MD5 calculation
658 if(ctx->calculating_md5) ctx->calculating_md5 = false;
659 // Disable SHA1 calculation
660 if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
661 // Disable SHA256 calculation
662 if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
663 // Disable SpamSum calculation
664 if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
665 // Disable BLAKE3 calculation
666 if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
667 }
668 }
669 else
670 ctx->last_written_block = sector_address;
671 }
672
673 // Calculate MD5 on-the-fly if requested and sector is within user sectors (not negative or overflow)
674 if(ctx->calculating_md5 && !negative && sector_address <= ctx->image_info.Sectors)
675 aaruf_md5_update(&ctx->md5_context, data, length);
676 // Calculate SHA1 on-the-fly if requested and sector is within user sectors (not negative or overflow)
677 if(ctx->calculating_sha1 && !negative && sector_address <= ctx->image_info.Sectors)
678 aaruf_sha1_update(&ctx->sha1_context, data, length);
679 // Calculate SHA256 on-the-fly if requested and sector is within user sectors (not negative or overflow)
680 if(ctx->calculating_sha256 && !negative && sector_address <= ctx->image_info.Sectors)
681 aaruf_sha256_update(&ctx->sha256_context, data, length);
682 // Calculate SpamSum on-the-fly if requested and sector is within user sectors (not negative or overflow)
683 if(ctx->calculating_spamsum && !negative && sector_address <= ctx->image_info.Sectors)
684 aaruf_spamsum_update(ctx->spamsum_context, data, length);
685 // Calculate BLAKE3 on-the-fly if requested and sector is within user sectors (not negative or overflow)
686 if(ctx->calculating_blake3 && !negative && sector_address <= ctx->image_info.Sectors)
687 blake3_hasher_update(ctx->blake3_context, data, length);
688
689 bool prefix_correct;
690
691 // Split raw cd sector data in prefix (sync, header), user data and suffix (edc, ecc p, ecc q)
692 switch(track.type)
693 {
694 case Audio:
695 case Data:
696 return aaruf_write_sector(context, sector_address, negative, data, sector_status, length);
697 case CdMode1:
698
699 // If we do not have a DDT V2 for sector prefix, create one
700 if(ctx->sector_prefix_ddt2 == NULL)
701 {
702 ctx->sector_prefix_ddt2 =
703 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
705
706 if(ctx->sector_prefix_ddt2 == NULL)
707 {
708 FATAL("Could not allocate memory for CD sector prefix DDT");
709
710 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
712 }
713 }
714
715 // If we do not have a DDT V2 for sector suffix, create one
716 if(ctx->sector_suffix_ddt2 == NULL)
717 {
718 ctx->sector_suffix_ddt2 =
719 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
721
722 if(ctx->sector_suffix_ddt2 == NULL)
723 {
724 FATAL("Could not allocate memory for CD sector prefix DDT");
725
726 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
728 }
729 }
730
731 if(ctx->sector_prefix == NULL)
732 {
735 ctx->sector_prefix = malloc(ctx->sector_prefix_length);
736
737 if(ctx->sector_prefix == NULL)
738 {
739 FATAL("Could not allocate memory for CD sector prefix buffer");
740
741 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
743 }
744 }
745
746 if(ctx->sector_suffix == NULL)
747 {
751 ctx->sector_suffix = malloc(ctx->sector_suffix_length);
752
753 if(ctx->sector_suffix == NULL)
754 {
755 FATAL("Could not allocate memory for CD sector suffix buffer");
756
757 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
759 }
760 }
761
762 bool empty = true;
763
764 for(int i = 0; i < length; i++)
765 if(data[i] != 0)
766 {
767 empty = false;
768 break;
769 }
770
771 if(empty)
772 {
773 ctx->sector_prefix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
774 ctx->sector_suffix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
775 return aaruf_write_sector(context, sector_address, negative, data + 16, SectorStatusNotDumped,
776 2048);
777 }
778
779 prefix_correct = true;
780
781 if(data[0x00] != 0x00 || data[0x01] != 0xFF || data[0x02] != 0xFF || data[0x03] != 0xFF ||
782 data[0x04] != 0xFF || data[0x05] != 0xFF || data[0x06] != 0xFF || data[0x07] != 0xFF ||
783 data[0x08] != 0xFF || data[0x09] != 0xFF || data[0x0A] != 0xFF || data[0x0B] != 0x00 ||
784 data[0x0F] != 0x01)
785 prefix_correct = false;
786
787 if(prefix_correct)
788 {
789 const int minute = (data[0x0C] >> 4) * 10 + (data[0x0C] & 0x0F);
790 const int second = (data[0x0D] >> 4) * 10 + (data[0x0D] & 0x0F);
791 const int frame = (data[0x0E] >> 4) * 10 + (data[0x0E] & 0x0F);
792 const int stored_lba = minute * 60 * 75 + second * 75 + frame - 150;
793 prefix_correct = stored_lba == sector_address;
794 }
795
796 if(prefix_correct)
797 ctx->sector_prefix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode1Correct << 60;
798 else
799 {
800 // Copy CD prefix from data buffer to prefix buffer
801 memcpy(ctx->sector_prefix + ctx->sector_prefix_offset, data, 16);
802 ctx->sector_prefix_ddt2[corrected_sector_address] = (uint64_t)(ctx->sector_prefix_offset / 16);
803 ctx->sector_prefix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
804 ctx->sector_prefix_offset += 16;
805
806 // Grow prefix buffer if needed
808 {
809 ctx->sector_prefix_length *= 2;
810 ctx->sector_prefix = realloc(ctx->sector_prefix, ctx->sector_prefix_length);
811
812 if(ctx->sector_prefix == NULL)
813 {
814 FATAL("Could not allocate memory for CD sector prefix buffer");
815
816 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
818 }
819 }
820 }
821
822 const bool suffix_correct = aaruf_ecc_cd_is_suffix_correct(ctx->ecc_cd_context, data);
823
824 if(suffix_correct)
825 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode1Correct << 60;
826 else
827 {
828 // Copy CD suffix from data buffer to suffix buffer
829 memcpy(ctx->sector_suffix + ctx->sector_suffix_offset, data + 2064, 288);
830 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)(ctx->sector_suffix_offset / 288);
831 ctx->sector_suffix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
832 ctx->sector_suffix_offset += 288;
833
834 // Grow suffix buffer if needed
836 {
837 ctx->sector_suffix_length *= 2;
838 ctx->sector_suffix = realloc(ctx->sector_suffix, ctx->sector_suffix_length);
839
840 if(ctx->sector_suffix == NULL)
841 {
842 FATAL("Could not allocate memory for CD sector suffix buffer");
843
844 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
846 }
847 }
848 }
849
850 return aaruf_write_sector(context, sector_address, negative, data + 16, SectorStatusMode1Correct,
851 2048);
852 case CdMode2Form1:
853 case CdMode2Form2:
854 case CdMode2Formless:
855 // If we do not have a DDT V2 for sector prefix, create one
856 if(ctx->sector_prefix_ddt2 == NULL)
857 {
858 ctx->sector_prefix_ddt2 =
859 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
861
862 if(ctx->sector_prefix_ddt2 == NULL)
863 {
864 FATAL("Could not allocate memory for CD sector prefix DDT");
865
866 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
868 }
869 }
870
871 // If we do not have a DDT V2 for sector suffix, create one
872 if(ctx->sector_suffix_ddt2 == NULL)
873 {
874 ctx->sector_suffix_ddt2 =
875 calloc(1, sizeof(uint64_t) * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
877
878 if(ctx->sector_suffix_ddt2 == NULL)
879 {
880 FATAL("Could not allocate memory for CD sector prefix DDT");
881
882 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
884 }
885 }
886
887 if(ctx->sector_prefix == NULL)
888 {
891 ctx->sector_prefix = malloc(ctx->sector_prefix_length);
892
893 if(ctx->sector_prefix == NULL)
894 {
895 FATAL("Could not allocate memory for CD sector prefix buffer");
896
897 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
899 }
900 }
901
902 if(ctx->sector_suffix == NULL)
903 {
907 ctx->sector_suffix = malloc(ctx->sector_suffix_length);
908
909 if(ctx->sector_suffix == NULL)
910 {
911 FATAL("Could not allocate memory for CD sector suffix buffer");
912
913 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
915 }
916 }
917
918 empty = true;
919
920 for(int i = 0; i < length; i++)
921 if(data[i] != 0)
922 {
923 empty = false;
924 break;
925 }
926
927 if(empty)
928 {
929 ctx->sector_prefix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
930 ctx->sector_suffix_ddt2[corrected_sector_address] = SectorStatusNotDumped;
931 return aaruf_write_sector(context, sector_address, negative, data + 16, SectorStatusNotDumped,
932 2328);
933 }
934
935 const bool form2 = (data[18] & 0x20) == 0x20 || (data[22] & 0x20) == 0x20;
936
937 prefix_correct = true;
938
939 if(data[0x00] != 0x00 || data[0x01] != 0xFF || data[0x02] != 0xFF || data[0x03] != 0xFF ||
940 data[0x04] != 0xFF || data[0x05] != 0xFF || data[0x06] != 0xFF || data[0x07] != 0xFF ||
941 data[0x08] != 0xFF || data[0x09] != 0xFF || data[0x0A] != 0xFF || data[0x0B] != 0x00 ||
942 data[0x0F] != 0x02)
943 prefix_correct = false;
944
945 if(prefix_correct)
946 {
947 const int minute = (data[0x0C] >> 4) * 10 + (data[0x0C] & 0x0F);
948 const int second = (data[0x0D] >> 4) * 10 + (data[0x0D] & 0x0F);
949 const int frame = (data[0x0E] >> 4) * 10 + (data[0x0E] & 0x0F);
950 const int stored_lba = minute * 60 * 75 + second * 75 + frame - 150;
951 prefix_correct = stored_lba == sector_address;
952 }
953
954 if(prefix_correct)
955 ctx->sector_prefix_ddt2[corrected_sector_address] =
956 (uint64_t)(form2 ? SectorStatusMode2Form2Ok : SectorStatusMode2Form1Ok) << 60;
957 else
958 {
959 // Copy CD prefix from data buffer to prefix buffer
960 memcpy(ctx->sector_prefix + ctx->sector_prefix_offset, data, 16);
961 ctx->sector_prefix_ddt2[corrected_sector_address] = (uint32_t)(ctx->sector_prefix_offset / 16);
962 ctx->sector_prefix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
963 ctx->sector_prefix_offset += 16;
964
965 // Grow prefix buffer if needed
967 {
968 ctx->sector_prefix_length *= 2;
969 ctx->sector_prefix = realloc(ctx->sector_prefix, ctx->sector_prefix_length);
970
971 if(ctx->sector_prefix == NULL)
972 {
973 FATAL("Could not allocate memory for CD sector prefix buffer");
974
975 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
977 }
978 }
979 }
980
981 if(ctx->mode2_subheaders == NULL)
982 {
983 ctx->mode2_subheaders =
984 calloc(1, 8 * (ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
986
987 if(ctx->mode2_subheaders == NULL)
988 {
989 FATAL("Could not allocate memory for CD mode 2 subheader buffer");
990
991 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
993 }
994 }
995
996 if(form2)
997 {
998 const uint32_t computed_edc = aaruf_edc_cd_compute(ctx->ecc_cd_context, 0, data, 0x91C, 0x10);
999 uint32_t edc = 0;
1000 memcpy(&edc, data + 0x92C, sizeof(edc));
1001 const bool correct_edc = computed_edc == edc;
1002
1003 if(correct_edc)
1004 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode2Form2Ok
1005 << 60;
1006 else if(edc == 0)
1007 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode2Form2NoCrc
1008 << 60;
1009 else
1010 {
1011 // Copy CD suffix from data buffer to suffix buffer
1012 memcpy(ctx->sector_suffix + ctx->sector_suffix_offset, data + 2348, 4);
1013 ctx->sector_suffix_ddt2[corrected_sector_address] =
1014 (uint64_t)(ctx->sector_suffix_offset / 288);
1015 ctx->sector_suffix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
1016 ctx->sector_suffix_offset += 288;
1017
1018 // Grow suffix buffer if needed
1020 {
1021 ctx->sector_suffix_length *= 2;
1022 ctx->sector_suffix = realloc(ctx->sector_suffix, ctx->sector_suffix_length);
1023
1024 if(ctx->sector_suffix == NULL)
1025 {
1026 FATAL("Could not allocate memory for CD sector suffix buffer");
1027
1028 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
1030 }
1031 }
1032 }
1033
1034 // Copy subheader from data buffer to subheader buffer
1035 memcpy(ctx->mode2_subheaders + corrected_sector_address * 8, data + 0x10, 8);
1036 return aaruf_write_sector(context, sector_address, negative, data + 24,
1038 : correct_edc ? SectorStatusMode2Form2Ok
1040 2324);
1041 }
1042
1043 const bool correct_ecc = aaruf_ecc_cd_is_suffix_correct_mode2(ctx->ecc_cd_context, data);
1044 const uint32_t computed_edc = aaruf_edc_cd_compute(ctx->ecc_cd_context, 0, data, 0x808, 0x10);
1045 uint32_t edc = 0;
1046 memcpy(&edc, data + 0x818, sizeof(edc));
1047 const bool correct_edc = computed_edc == edc;
1048
1049 if(correct_ecc && correct_edc)
1050 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)SectorStatusMode2Form1Ok << 60;
1051 else
1052 {
1053 // Copy CD suffix from data buffer to suffix buffer
1054 memcpy(ctx->sector_suffix + ctx->sector_suffix_offset, data + 2072, 280);
1055 ctx->sector_suffix_ddt2[corrected_sector_address] = (uint64_t)(ctx->sector_suffix_offset / 288);
1056 ctx->sector_suffix_ddt2[corrected_sector_address] |= (uint64_t)SectorStatusErrored << 60;
1057 ctx->sector_suffix_offset += 288;
1058
1059 // Grow suffix buffer if needed
1061 {
1062 ctx->sector_suffix_length *= 2;
1063 ctx->sector_suffix = realloc(ctx->sector_suffix, ctx->sector_suffix_length);
1064
1065 if(ctx->sector_suffix == NULL)
1066 {
1067 FATAL("Could not allocate memory for CD sector suffix buffer");
1068
1069 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
1071 }
1072 }
1073 }
1074
1075 // Copy subheader from data buffer to subheader buffer
1076 memcpy(ctx->mode2_subheaders + corrected_sector_address * 8, data + 0x10, 8);
1077 return aaruf_write_sector(
1078 context, sector_address, negative, data + 24,
1079 correct_edc && correct_ecc ? SectorStatusMode2Form1Ok : SectorStatusErrored, 2048);
1080 }
1081
1082 break;
1083 }
1084 case BlockMedia:
1085 switch(ctx->image_info.MediaType)
1086 {
1087 case AppleFileWare:
1088 case AppleProfile:
1089 case AppleSonyDS:
1090 case AppleSonySS:
1091 case AppleWidget:
1092 case PriamDataTower:
1093 {
1094 uint8_t *newTag;
1095 int newTagSize = 0;
1096
1097 switch(length - 512)
1098 {
1099 // Sony tag
1100 case 12:
1101 {
1102 const sony_tag decoded_sony_tag = bytes_to_sony_tag(data + 512);
1103
1105 {
1106 const profile_tag decoded_profile_tag = sony_tag_to_profile(decoded_sony_tag);
1107 newTag = profile_tag_to_bytes(decoded_profile_tag);
1108 newTagSize = 20;
1109 }
1110 else if(ctx->image_info.MediaType == PriamDataTower)
1111 {
1112 const priam_tag decoded_priam_tag = sony_tag_to_priam(decoded_sony_tag);
1113 newTag = priam_tag_to_bytes(decoded_priam_tag);
1114 newTagSize = 24;
1115 }
1116 else if(ctx->image_info.MediaType == AppleSonyDS ||
1118 {
1119 newTag = malloc(12);
1120 memcpy(newTag, data + 512, 12);
1121 newTagSize = 12;
1122 }
1123 break;
1124 }
1125 // Profile tag
1126 case 20:
1127 {
1128 const profile_tag decoded_profile_tag = bytes_to_profile_tag(data + 512);
1129
1131 {
1132 newTag = malloc(20);
1133 memcpy(newTag, data + 512, 20);
1134 newTagSize = 20;
1135 }
1136 else if(ctx->image_info.MediaType == PriamDataTower)
1137 {
1138 const priam_tag decoded_priam_tag = profile_tag_to_priam(decoded_profile_tag);
1139 newTag = priam_tag_to_bytes(decoded_priam_tag);
1140 newTagSize = 24;
1141 }
1142 else if(ctx->image_info.MediaType == AppleSonyDS ||
1144 {
1145 const sony_tag decoded_sony_tag = profile_tag_to_sony(decoded_profile_tag);
1146 newTag = sony_tag_to_bytes(decoded_sony_tag);
1147 newTagSize = 12;
1148 }
1149 break;
1150 }
1151 // Priam tag
1152 case 24:
1153 {
1154 const priam_tag decoded_priam_tag = bytes_to_priam_tag(data + 512);
1156 {
1157 const profile_tag decoded_profile_tag = priam_tag_to_profile(decoded_priam_tag);
1158 newTag = profile_tag_to_bytes(decoded_profile_tag);
1159 newTagSize = 20;
1160 }
1161 else if(ctx->image_info.MediaType == PriamDataTower)
1162 {
1163 newTag = malloc(24);
1164 memcpy(newTag, data + 512, 24);
1165 newTagSize = 24;
1166 }
1167 else if(ctx->image_info.MediaType == AppleSonyDS ||
1169 {
1170 const sony_tag decoded_sony_tag = priam_tag_to_sony(decoded_priam_tag);
1171 newTag = sony_tag_to_bytes(decoded_sony_tag);
1172 newTagSize = 12;
1173 }
1174 break;
1175 }
1176 case 0:
1177 newTagSize = 0;
1178 break;
1179 default:
1180 FATAL("Incorrect sector size");
1181 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_INCORRECT_DATA_SIZE");
1183 }
1184
1185 if(newTagSize == 0)
1186 return aaruf_write_sector(context, sector_address, negative, data, sector_status, 512);
1187
1188 if(ctx->sector_subchannel == NULL)
1189 {
1190 ctx->sector_subchannel =
1191 calloc(1, newTagSize * (ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow));
1192
1193 if(ctx->sector_subchannel == NULL)
1194 {
1195 FATAL("Could not allocate memory for sector subchannel DDT");
1196
1197 free(newTag);
1198
1199 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
1201 }
1202 }
1203
1204 memcpy(ctx->sector_subchannel + sector_address * newTagSize, newTag, newTagSize);
1205 free(newTag);
1206
1207 return aaruf_write_sector(context, sector_address, negative, data, sector_status, 512);
1208 }
1209 default:
1211 }
1212 default:
1213 TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
1215 }
1216
1217 // Fallback return when media type branch does not produce a value (satisfy non-void contract)
1219}
1220
1404{
1405 // Not a libaaruformat context
1406 if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
1407
1408 // Check we are writing
1409 if(!ctx->is_writing) return AARUF_READ_ONLY;
1410
1412
1413 TRACE("Initializing CRC64 context");
1415 TRACE("Updating CRC64");
1418
1419 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1420 uint8_t *cmp_buffer = NULL;
1421
1423 {
1424 case None:
1425 break;
1426 case Flac:
1427 cmp_buffer = malloc(ctx->current_block_header.length * 2);
1428 if(cmp_buffer == NULL)
1429 {
1430 FATAL("Could not allocate buffer for compressed data");
1432 }
1433 const uint32_t current_samples = ctx->current_block_offset * SAMPLES_PER_SECTOR;
1434 uint32_t flac_block_size = ctx->current_block_offset * SAMPLES_PER_SECTOR;
1435
1436 if(flac_block_size > MAX_FLAKE_BLOCK) flac_block_size = MAX_FLAKE_BLOCK;
1437 if(flac_block_size < MIN_FLAKE_BLOCK) flac_block_size = MIN_FLAKE_BLOCK;
1438
1439 const long remaining = current_samples % flac_block_size;
1440
1441 // Fill FLAC block
1442 if(remaining != 0)
1443 for(int r = 0; r < remaining * 4; r++) ctx->writing_buffer[ctx->writing_buffer_position + r] = 0;
1444
1446 cmp_buffer, ctx->current_block_header.length * 2, ctx->writing_buffer, ctx->current_block_header.length,
1447 flac_block_size, true, false, "hamming", 12, 15, true, false, 0, 8, "Aaru", 4);
1448
1450 {
1452 free(cmp_buffer);
1453 }
1454
1455 break;
1456 case Lzma:
1457 cmp_buffer = malloc(ctx->current_block_header.length * 2);
1458 if(cmp_buffer == NULL)
1459 {
1460 FATAL("Could not allocate buffer for compressed data");
1462 }
1463
1464 size_t dst_size = ctx->current_block_header.length * 2;
1465 size_t props_size = LZMA_PROPERTIES_LENGTH;
1466 aaruf_lzma_encode_buffer(cmp_buffer, &dst_size, ctx->writing_buffer, ctx->current_block_header.length,
1467 lzma_properties, &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1468
1469 ctx->current_block_header.cmpLength = (uint32_t)dst_size;
1470
1472 {
1474 free(cmp_buffer);
1475 }
1476
1477 break;
1478 default:
1479 FATAL("Invalid compression type");
1481 }
1482
1484 {
1487 }
1488 else
1490
1492
1493 // Add to index
1494 TRACE("Adding block to index");
1495 IndexEntry index_entry;
1496 index_entry.blockType = DataBlock;
1497 index_entry.dataType = UserData;
1498 index_entry.offset = ctx->next_block_position;
1499
1500 utarray_push_back(ctx->index_entries, &index_entry);
1501 TRACE("Block added to index at offset %" PRIu64, index_entry.offset);
1502
1503 // Write block header to file
1504
1505 // Move to expected block position
1506 fseek(ctx->imageStream, ctx->next_block_position, SEEK_SET);
1507
1508 // Write block header
1509 if(fwrite(&ctx->current_block_header, sizeof(BlockHeader), 1, ctx->imageStream) != 1)
1511
1512 // Write block data
1514 fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream) != 1)
1515 {
1516 free(cmp_buffer);
1518 }
1519
1521 {
1522 if(fwrite(ctx->writing_buffer, ctx->current_block_header.length, 1, ctx->imageStream) != 1)
1524 }
1525 else
1526 {
1527 if(fwrite(cmp_buffer, ctx->current_block_header.cmpLength, 1, ctx->imageStream) != 1)
1528 {
1529 free(cmp_buffer);
1531 }
1532
1533 free(cmp_buffer);
1534 }
1535
1536 // Update nextBlockPosition to point to the next available aligned position
1537 const uint64_t block_total_size = sizeof(BlockHeader) + ctx->current_block_header.cmpLength;
1538 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1539 ctx->next_block_position = ctx->next_block_position + block_total_size + alignment_mask & ~alignment_mask;
1540 TRACE("Updated nextBlockPosition to %" PRIu64, ctx->next_block_position);
1541
1542 // Clear values
1543 free(ctx->writing_buffer);
1544 ctx->writing_buffer = NULL;
1545 ctx->current_block_offset = 0;
1546 memset(&ctx->current_block_header, 0, sizeof(BlockHeader));
1548 ctx->writing_buffer_position = 0;
1549
1550 return AARUF_STATUS_OK;
1551}
1552
1800AARU_EXPORT int32_t AARU_CALL aaruf_write_media_tag(void *context, const uint8_t *data, const int32_t type,
1801 const uint32_t length)
1802{
1803 TRACE("Entering aaruf_write_media_tag(%p, %p, %d, %d)", context, data, type, length);
1804
1805 // Check context is correct AaruFormat context
1806 if(context == NULL)
1807 {
1808 FATAL("Invalid context");
1809
1810 TRACE("Exiting aaruf_write_media_tag() = AARUF_ERROR_NOT_AARUFORMAT");
1812 }
1813
1814 aaruformat_context *ctx = context;
1815
1816 // Not a libaaruformat context
1817 if(ctx->magic != AARU_MAGIC)
1818 {
1819 FATAL("Invalid context");
1820
1821 TRACE("Exiting aaruf_write_media_tag() = AARUF_ERROR_NOT_AARUFORMAT");
1823 }
1824
1825 // Check we are writing
1826 if(!ctx->is_writing)
1827 {
1828 FATAL("Trying to write a read-only image");
1829
1830 TRACE("Exiting aaruf_write_media_tag() = AARUF_READ_ONLY");
1831 return AARUF_READ_ONLY;
1832 }
1833
1834 if(data == NULL || length == 0)
1835 {
1836 FATAL("Invalid data or length");
1838 }
1839
1840 uint8_t *new_data = malloc(length);
1841
1842 if(new_data == NULL)
1843 {
1844 FATAL("Could not allocate memory for media tag");
1846 }
1847 memcpy(new_data, data, length);
1848
1849 mediaTagEntry *media_tag = malloc(sizeof(mediaTagEntry));
1850 mediaTagEntry *old_media_tag = NULL;
1851
1852 if(media_tag == NULL)
1853 {
1854 TRACE("Cannot allocate memory for media tag entry.");
1855 free(new_data);
1857 }
1858
1859 memset(media_tag, 0, sizeof(mediaTagEntry));
1860
1861 media_tag->type = type;
1862 media_tag->data = new_data;
1863 media_tag->length = length;
1864
1865 HASH_REPLACE_INT(ctx->mediaTags, type, media_tag, old_media_tag);
1866
1867 if(old_media_tag != NULL)
1868 {
1869 TRACE("Replaced media tag with type %d", old_media_tag->type);
1870 free(old_media_tag->data);
1871 free(old_media_tag);
1872 old_media_tag = NULL;
1873 }
1874
1875 TRACE("Exiting aaruf_write_media_tag() = AARUF_STATUS_OK");
1876 return AARUF_STATUS_OK;
1877}
1878
2069AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_tag(void *context, const uint64_t sector_address, const bool negative,
2070 const uint8_t *data, const size_t length, const int32_t tag)
2071{
2072 TRACE("Entering aaruf_write_sector_tag(%p, %" PRIu64 ", %d, %p, %zu, %d)", context, sector_address, negative, data,
2073 length, tag);
2074
2075 // Check context is correct AaruFormat context
2076 if(context == NULL)
2077 {
2078 FATAL("Invalid context");
2079
2080 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_AARUFORMAT");
2082 }
2083
2084 aaruformat_context *ctx = context;
2085
2086 // Not a libaaruformat context
2087 if(ctx->magic != AARU_MAGIC)
2088 {
2089 FATAL("Invalid context");
2090
2091 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_AARUFORMAT");
2093 }
2094
2095 // Check we are writing
2096 if(!ctx->is_writing)
2097 {
2098 FATAL("Trying to write a read-only image");
2099
2100 TRACE("Exiting aaruf_write_sector_tag() = AARUF_READ_ONLY");
2101 return AARUF_READ_ONLY;
2102 }
2103
2104 if(negative && sector_address > ctx->user_data_ddt_header.negative - 1)
2105 {
2106 FATAL("Sector address out of bounds");
2107
2108 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
2110 }
2111
2112 if(!negative && sector_address > ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow - 1)
2113 {
2114 FATAL("Sector address out of bounds");
2115
2116 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
2118 }
2119
2120 if(data == NULL || length == 0)
2121 {
2122 FATAL("Invalid data or length");
2124 }
2125
2126 uint64_t corrected_sector_address = sector_address;
2127
2128 // Calculate positive or negative sector
2129 if(negative)
2130 corrected_sector_address -= ctx->user_data_ddt_header.negative;
2131 else
2132 corrected_sector_address += ctx->user_data_ddt_header.negative;
2133
2134 const uint64_t total_sectors =
2136
2137 switch(tag)
2138 {
2139 case CdTrackFlags:
2141 {
2142 FATAL("Invalid media type for tag");
2143 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2145 }
2146
2147 if(length != 1)
2148 {
2149 FATAL("Incorrect tag size");
2150 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2152 }
2153
2154 for(int i = 0; i < ctx->tracks_header.entries; i++)
2155 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
2156 {
2157 ctx->track_entries[i].flags = data[0];
2158 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2159 return AARUF_STATUS_OK;
2160 }
2161
2162 FATAL("Track not found");
2164 case CdTrackIsrc:
2166 {
2167 FATAL("Invalid media type for tag");
2168 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2170 }
2171
2172 if(length != 12)
2173 {
2174 FATAL("Incorrect tag size");
2175 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2177 }
2178
2179 for(int i = 0; i < ctx->tracks_header.entries; i++)
2180 if(sector_address >= ctx->track_entries[i].start && sector_address <= ctx->track_entries[i].end)
2181 {
2182 memcpy(ctx->track_entries[i].isrc, data, 12);
2183 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2184 return AARUF_STATUS_OK;
2185 }
2186
2187 FATAL("Track not found");
2191 {
2192 FATAL("Invalid media type for tag");
2193 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2195 }
2196
2197 if(length != 96)
2198 {
2199 FATAL("Incorrect tag size");
2200 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2202 }
2203
2204 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 96 * total_sectors);
2205
2206 if(ctx->sector_subchannel == NULL)
2207 {
2208 FATAL("Could not allocate memory for sector subchannel");
2209
2210 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2212 }
2213
2214 memcpy(ctx->sector_subchannel + corrected_sector_address * 96, data, 96);
2215 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2216 return AARUF_STATUS_OK;
2217 case DvdCmi:
2219 {
2220 FATAL("Invalid media type for tag");
2221 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2223 }
2224
2225 if(length != 1)
2226 {
2227 FATAL("Incorrect tag size");
2228 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2230 }
2231
2232 if(ctx->sector_cpr_mai == NULL) ctx->sector_cpr_mai = calloc(1, 6 * total_sectors);
2233
2234 if(ctx->sector_cpr_mai == NULL)
2235 {
2236 FATAL("Could not allocate memory for sector CPR/MAI");
2237
2238 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2240 }
2241
2242 memcpy(ctx->sector_cpr_mai + corrected_sector_address * 6, data, 1);
2243 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2244 return AARUF_STATUS_OK;
2247 {
2248 FATAL("Invalid media type for tag");
2249 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2251 }
2252
2253 if(length != 1)
2254 {
2255 FATAL("Incorrect tag size");
2256 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2258 }
2259
2260 if(ctx->sector_id == NULL) ctx->sector_id = calloc(1, 4 * total_sectors);
2261
2262 if(ctx->sector_id == NULL)
2263 {
2264 FATAL("Could not allocate memory for sector ID");
2265
2266 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2268 }
2269
2270 memcpy(ctx->sector_id + corrected_sector_address * 4, data, 1);
2271 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2272 return AARUF_STATUS_OK;
2273 case DvdSectorNumber:
2275 {
2276 FATAL("Invalid media type for tag");
2277 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2279 }
2280
2281 if(length != 3)
2282 {
2283 FATAL("Incorrect tag size");
2284 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2286 }
2287
2288 if(ctx->sector_id == NULL) ctx->sector_id = calloc(1, 4 * total_sectors);
2289
2290 if(ctx->sector_id == NULL)
2291 {
2292 FATAL("Could not allocate memory for sector ID");
2293
2294 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2296 }
2297
2298 memcpy(ctx->sector_id + corrected_sector_address * 4 + 1, data, 3);
2299 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2300 return AARUF_STATUS_OK;
2301 case DvdSectorIedAaru:
2303 {
2304 FATAL("Invalid media type for tag");
2305 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2307 }
2308
2309 if(length != 2)
2310 {
2311 FATAL("Incorrect tag size");
2312 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2314 }
2315
2316 if(ctx->sector_ied == NULL) ctx->sector_ied = calloc(1, 2 * total_sectors);
2317
2318 if(ctx->sector_ied == NULL)
2319 {
2320 FATAL("Could not allocate memory for sector IED");
2321
2322 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2324 }
2325
2326 memcpy(ctx->sector_ied + corrected_sector_address * 2, data, 2);
2327 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2328 return AARUF_STATUS_OK;
2329 case DvdSectorEdcAaru:
2331 {
2332 FATAL("Invalid media type for tag");
2333 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2335 }
2336
2337 if(length != 4)
2338 {
2339 FATAL("Incorrect tag size");
2340 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2342 }
2343
2344 if(ctx->sector_edc == NULL) ctx->sector_edc = calloc(1, 4 * total_sectors);
2345
2346 if(ctx->sector_edc == NULL)
2347 {
2348 FATAL("Could not allocate memory for sector EDC");
2349
2350 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2352 }
2353
2354 memcpy(ctx->sector_edc + corrected_sector_address * 4, data, 4);
2355 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2356 return AARUF_STATUS_OK;
2359 {
2360 FATAL("Invalid media type for tag");
2361 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2363 }
2364
2365 if(length != 5)
2366 {
2367 FATAL("Incorrect tag size");
2368 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2370 }
2371
2372 if(ctx->sector_decrypted_title_key == NULL) ctx->sector_decrypted_title_key = calloc(1, 5 * total_sectors);
2373
2374 if(ctx->sector_decrypted_title_key == NULL)
2375 {
2376 FATAL("Could not allocate memory for sector decrypted title key");
2377
2378 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2380 }
2381
2382 memcpy(ctx->sector_decrypted_title_key + corrected_sector_address * 5, data, 5);
2383 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2384 return AARUF_STATUS_OK;
2385 case AppleSonyTagAaru:
2387 {
2388 FATAL("Invalid media type for tag");
2389 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2391 }
2392
2393 if(length != 12)
2394 {
2395 FATAL("Incorrect tag size");
2396 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2398 }
2399
2400 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 12 * total_sectors);
2401
2402 if(ctx->sector_subchannel == NULL)
2403 {
2404 FATAL("Could not allocate memory for Apple Sony tag");
2405
2406 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2408 }
2409
2410 memcpy(ctx->sector_subchannel + corrected_sector_address * 12, data, 12);
2411 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2412 return AARUF_STATUS_OK;
2415 {
2416 FATAL("Invalid media type for tag");
2417 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2419 }
2420
2421 if(length != 20)
2422 {
2423 FATAL("Incorrect tag size");
2424 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2426 }
2427
2428 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 20 * total_sectors);
2429
2430 if(ctx->sector_subchannel == NULL)
2431 {
2432 FATAL("Could not allocate memory for Apple Profile tag");
2433
2434 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2436 }
2437
2438 memcpy(ctx->sector_subchannel + corrected_sector_address * 20, data, 20);
2439 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2440 return AARUF_STATUS_OK;
2443 {
2444 FATAL("Invalid media type for tag");
2445 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
2447 }
2448
2449 if(length != 24)
2450 {
2451 FATAL("Incorrect tag size");
2452 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_INCORRECT_DATA_SIZE");
2454 }
2455
2456 if(ctx->sector_subchannel == NULL) ctx->sector_subchannel = calloc(1, 24 * total_sectors);
2457
2458 if(ctx->sector_subchannel == NULL)
2459 {
2460 FATAL("Could not allocate memory for Priam Data Tower tag");
2461
2462 TRACE("Exiting aaruf_write_sector_tag() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
2464 }
2465
2466 memcpy(ctx->sector_subchannel + corrected_sector_address * 24, data, 24);
2467 TRACE("Exiting aaruf_write_sector_tag() = AARUF_STATUS_OK");
2468 return AARUF_STATUS_OK;
2469 default:
2470 TRACE("Do not know how to write sector tag %d", tag);
2472 }
2473}
#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:299
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:300
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
bool block_zero_written
True if block zero has been written (writing path).
Definition context.h:295
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:298
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:1403
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:2069
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:1800
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:537