libaaruformat 1.0 Aaru Data Preservation Suite - Format Library

Loading...
Searching...
No Matches
close.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 */
36
37#include <errno.h>
38#include <stdio.h>
39#include <stdlib.h>
40
41#ifdef __linux__
42#include <sys/mman.h>
43#endif
44
45#include <aaruformat.h>
46
47#include "internal.h"
48#include "log.h"
49
77 static int32_t write_cached_secondary_ddt(aaruformat_context *ctx)
78{
79 // Write cached secondary table to file end and update primary table entry with its position
80 // Check if we have a cached table that needs to be written (either it has an offset or exists in memory)
81 bool has_cached_secondary_ddt =
82 ctx->user_data_ddt_header.tableShift > 0 && (ctx->cached_ddt_offset != 0 || ctx->cached_secondary_ddt2 != NULL);
83
84 if(!has_cached_secondary_ddt) return AARUF_STATUS_OK;
85
86 TRACE("Writing cached secondary DDT table to file");
87
88 fseek(ctx->imageStream, 0, SEEK_END);
89 long end_of_file = ftell(ctx->imageStream);
90
91 // Align the position according to block alignment shift
92 uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
93 if(end_of_file & alignment_mask)
94 {
95 // Calculate the next aligned position
96 uint64_t aligned_position = end_of_file + alignment_mask & ~alignment_mask;
97
98 // Seek to the aligned position and pad with zeros if necessary
99 fseek(ctx->imageStream, aligned_position, SEEK_SET);
100 end_of_file = aligned_position;
101
102 TRACE("Aligned DDT write position from %ld to %" PRIu64 " (alignment shift: %d)",
103 ftell(ctx->imageStream) - (aligned_position - end_of_file), aligned_position,
104 ctx->user_data_ddt_header.blockAlignmentShift);
105 }
106
107 // Prepare DDT header for the cached table
108 DdtHeader2 ddt_header = {0};
109 ddt_header.identifier = DeDuplicationTableSecondary;
110 ddt_header.type = UserData;
111 ddt_header.compression = ctx->compression_enabled ? Lzma : None;
112 ddt_header.levels = ctx->user_data_ddt_header.levels;
113 ddt_header.tableLevel = ctx->user_data_ddt_header.tableLevel + 1;
114 ddt_header.previousLevelOffset = ctx->primary_ddt_offset;
115 ddt_header.negative = ctx->user_data_ddt_header.negative;
116 ddt_header.overflow = ctx->user_data_ddt_header.overflow;
117 ddt_header.blockAlignmentShift = ctx->user_data_ddt_header.blockAlignmentShift;
118 ddt_header.dataShift = ctx->user_data_ddt_header.dataShift;
119 ddt_header.tableShift = 0; // Secondary tables are single level
120
121 uint64_t items_per_ddt_entry = 1 << ctx->user_data_ddt_header.tableShift;
122 ddt_header.blocks = items_per_ddt_entry;
123 ddt_header.entries = items_per_ddt_entry;
124 ddt_header.start = ctx->cached_ddt_position * items_per_ddt_entry;
125
126 // Calculate data size
127 ddt_header.length = items_per_ddt_entry * sizeof(uint64_t);
128
129 // Calculate CRC64 of the data
130 crc64_ctx *crc64_context = aaruf_crc64_init();
131 if(crc64_context != NULL)
132 {
133 aaruf_crc64_update(crc64_context, (uint8_t *)ctx->cached_secondary_ddt2, (uint32_t)ddt_header.length);
134
135 uint64_t crc64;
136 aaruf_crc64_final(crc64_context, &crc64);
137 ddt_header.crc64 = crc64;
138 }
139
140 uint8_t *buffer = NULL;
141 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
142
143 if(ddt_header.compression == None)
144 {
145 buffer = (uint8_t *)ctx->cached_secondary_ddt2;
146 ddt_header.cmpCrc64 = ddt_header.crc64;
147 }
148 else
149 {
150 buffer = malloc((size_t)ddt_header.length * 2); // Allocate double size for compression
151 if(buffer == NULL)
152 {
153 TRACE("Failed to allocate memory for secondary DDT v2 compression");
154 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
155 }
156
157 size_t dst_size = (size_t)ddt_header.length * 2 * 2;
158 size_t props_size = LZMA_PROPERTIES_LENGTH;
159 aaruf_lzma_encode_buffer(buffer, &dst_size,
160
161 (uint8_t *)ctx->cached_secondary_ddt2, ddt_header.length, lzma_properties, &props_size,
162 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
163
164 ddt_header.cmpLength = (uint32_t)dst_size;
165
166 if(ddt_header.cmpLength >= ddt_header.length)
167 {
168 ddt_header.compression = None;
169 free(buffer);
170 buffer = (uint8_t *)ctx->cached_secondary_ddt2;
171 }
172 }
173
174 if(ddt_header.compression == None)
175 {
176 ddt_header.cmpLength = ddt_header.length;
177 ddt_header.cmpCrc64 = ddt_header.crc64;
178 }
179 else
180 ddt_header.cmpCrc64 = aaruf_crc64_data(buffer, ddt_header.cmpLength);
181
182 if(ddt_header.compression == Lzma) ddt_header.cmpLength += LZMA_PROPERTIES_LENGTH;
183
184 // Write header
185 if(fwrite(&ddt_header, sizeof(DdtHeader2), 1, ctx->imageStream) == 1)
186 {
187 if(ddt_header.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
188
189 // Write data
190 if(fwrite(buffer, ddt_header.cmpLength, 1, ctx->imageStream) == 1)
191 {
192 // Update primary table entry to point to new location
193 const uint64_t new_secondary_table_block_offset =
194 end_of_file >> ctx->user_data_ddt_header.blockAlignmentShift;
195
196 ctx->user_data_ddt2[ctx->cached_ddt_position] = (uint64_t)new_secondary_table_block_offset;
197
198 // Update index: remove old entry for cached DDT and add new one
199 TRACE("Updating index for cached secondary DDT");
200
201 // Remove old index entry for the cached DDT
202 if(ctx->cached_ddt_offset != 0)
203 {
204 TRACE("Removing old index entry for DDT at offset %" PRIu64, ctx->cached_ddt_offset);
205 const IndexEntry *entry = NULL;
206
207 // Find and remove the old index entry
208 for(unsigned int k = 0; k < utarray_len(ctx->index_entries); k++)
209 {
210 entry = (IndexEntry *)utarray_eltptr(ctx->index_entries, k);
211 if(entry && entry->offset == ctx->cached_ddt_offset &&
212 entry->blockType == DeDuplicationTableSecondary)
213 {
214 TRACE("Found old DDT index entry at position %u, removing", k);
215 utarray_erase(ctx->index_entries, k, 1);
216 break;
217 }
218 }
219 }
220
221 // Add new index entry for the newly written secondary DDT
222 IndexEntry new_ddt_entry;
223 new_ddt_entry.blockType = DeDuplicationTableSecondary;
224 new_ddt_entry.dataType = UserData;
225 new_ddt_entry.offset = end_of_file;
226
227 utarray_push_back(ctx->index_entries, &new_ddt_entry);
228 TRACE("Added new DDT index entry at offset %" PRIu64, end_of_file);
229
230 // Write the updated primary table back to its original position in the file
231 long saved_pos = ftell(ctx->imageStream);
232 fseek(ctx->imageStream, ctx->primary_ddt_offset + sizeof(DdtHeader2), SEEK_SET);
233
234 size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t);
235
236 size_t primary_written_bytes = 0;
237 primary_written_bytes = fwrite(ctx->user_data_ddt2, primary_table_size, 1, ctx->imageStream);
238
239 if(primary_written_bytes != 1)
240 {
241 TRACE("Could not flush primary DDT table to file.");
242 return AARUF_ERROR_CANNOT_WRITE_HEADER;
243 }
244
245 fseek(ctx->imageStream, saved_pos, SEEK_SET);
246 }
247 else
248 TRACE("Failed to write cached secondary DDT data");
249 }
250 else
251 TRACE("Failed to write cached secondary DDT header");
252
253 // Free the cached table
254 free(ctx->cached_secondary_ddt2);
255 ctx->cached_secondary_ddt2 = NULL;
256 ctx->cached_ddt_offset = 0;
257
258 // Set position
259 fseek(ctx->imageStream, 0, SEEK_END);
260
261 if(ddt_header.compression == Lzma) free(buffer);
262
263 return AARUF_STATUS_OK;
264}
265
283 static int32_t write_primary_ddt(aaruformat_context *ctx)
284{
285 // Write the cached primary DDT table back to its position in the file
286 if(ctx->user_data_ddt_header.tableShift <= 0 || ctx->user_data_ddt2 == NULL) return AARUF_STATUS_OK;
287
288 TRACE("Writing cached primary DDT table back to file");
289
290 // Calculate CRC64 of the primary DDT table data first
291 crc64_ctx *crc64_context = aaruf_crc64_init();
292 if(crc64_context != NULL)
293 {
294 size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t);
295
296 aaruf_crc64_update(crc64_context, (uint8_t *)ctx->user_data_ddt2, primary_table_size);
297
298 uint64_t crc64;
299 aaruf_crc64_final(crc64_context, &crc64);
300
301 // Properly populate all header fields for multi-level DDT primary table
302 ctx->user_data_ddt_header.identifier = DeDuplicationTable2;
303 ctx->user_data_ddt_header.type = UserData;
304 ctx->user_data_ddt_header.compression = None;
305 // levels, tableLevel, previousLevelOffset, negative, overflow, blockAlignmentShift,
306 // dataShift, tableShift, sizeType, entries, blocks, start are already set during creation
307 ctx->user_data_ddt_header.crc64 = crc64;
308 ctx->user_data_ddt_header.cmpCrc64 = crc64;
309 ctx->user_data_ddt_header.length = primary_table_size;
310 ctx->user_data_ddt_header.cmpLength = primary_table_size;
311
312 TRACE("Calculated CRC64 for primary DDT: 0x%16lX", crc64);
313 }
314
315 // First write the DDT header
316 fseek(ctx->imageStream, ctx->primary_ddt_offset, SEEK_SET);
317
318 size_t headerWritten = fwrite(&ctx->user_data_ddt_header, sizeof(DdtHeader2), 1, ctx->imageStream);
319 if(headerWritten != 1)
320 {
321 TRACE("Failed to write primary DDT header to file");
322 return AARUF_ERROR_CANNOT_WRITE_HEADER;
323 }
324
325 // Then write the table data (position is already after the header)
326 size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t);
327
328 // Write the primary table data
329 size_t written_bytes = 0;
330 written_bytes = fwrite(ctx->user_data_ddt2, primary_table_size, 1, ctx->imageStream);
331
332 if(written_bytes == 1)
333 {
334 TRACE("Successfully wrote primary DDT header and table to file (%" PRIu64 " entries, %zu bytes)",
335 ctx->user_data_ddt_header.entries, primary_table_size);
336
337 // Add primary DDT to index
338 TRACE("Adding primary DDT to index");
339 IndexEntry primary_ddt_entry;
340 primary_ddt_entry.blockType = DeDuplicationTable2;
341 primary_ddt_entry.dataType = UserData;
342 primary_ddt_entry.offset = ctx->primary_ddt_offset;
343
344 utarray_push_back(ctx->index_entries, &primary_ddt_entry);
345 TRACE("Added primary DDT index entry at offset %" PRIu64, ctx->primary_ddt_offset);
346 }
347 else
348 TRACE("Failed to write primary DDT table to file");
349
350 return AARUF_STATUS_OK;
351}
352
369 static int32_t write_single_level_ddt(aaruformat_context *ctx)
370{
371 // Write the single level DDT table block aligned just after the header
372 if(ctx->user_data_ddt_header.tableShift != 0 || ctx->user_data_ddt2 == NULL) return AARUF_STATUS_OK;
373
374 TRACE("Writing single-level DDT table to file");
375
376 // Calculate CRC64 of the primary DDT table data
377 const size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t);
378
379 // Properly populate all header fields
380 ctx->user_data_ddt_header.identifier = DeDuplicationTable2;
381 ctx->user_data_ddt_header.type = UserData;
382 ctx->user_data_ddt_header.compression = ctx->compression_enabled ? Lzma : None;
383 ctx->user_data_ddt_header.levels = 1; // Single level
384 ctx->user_data_ddt_header.tableLevel = 0; // Top level
385 ctx->user_data_ddt_header.previousLevelOffset = 0; // No previous level for single-level DDT
386 // negative and overflow are already set during creation
387 // blockAlignmentShift, dataShift, tableShift, sizeType, entries, blocks, start are already set
388 ctx->user_data_ddt_header.length = primary_table_size;
389 ctx->user_data_ddt_header.cmpLength = primary_table_size;
390
391 ctx->user_data_ddt_header.crc64 = aaruf_crc64_data((uint8_t *)ctx->user_data_ddt2, primary_table_size);
392
393 TRACE("Calculated CRC64 for single-level DDT: 0x%16lX", ctx->user_data_ddt_header.crc64);
394
395 uint8_t *cmp_buffer = NULL;
396 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
397
398 if(ctx->user_data_ddt_header.compression == None)
399 {
400
401 cmp_buffer = (uint8_t *)ctx->user_data_ddt2;
402 ctx->user_data_ddt_header.cmpCrc64 = ctx->user_data_ddt_header.crc64;
403 }
404 else
405 {
406 cmp_buffer = malloc((size_t)ctx->user_data_ddt_header.length * 2); // Allocate double size for compression
407 if(cmp_buffer == NULL)
408 {
409 TRACE("Failed to allocate memory for secondary DDT v2 compression");
410 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
411 }
412
413 size_t dst_size = (size_t)ctx->user_data_ddt_header.length * 2 * 2;
414 size_t props_size = LZMA_PROPERTIES_LENGTH;
415 aaruf_lzma_encode_buffer(cmp_buffer, &dst_size, (uint8_t *)ctx->user_data_ddt2,
416 ctx->user_data_ddt_header.length, lzma_properties, &props_size, 9, ctx->lzma_dict_size,
417 4, 0, 2, 273, 8);
418
419 ctx->user_data_ddt_header.cmpLength = (uint32_t)dst_size;
420
421 if(ctx->user_data_ddt_header.cmpLength >= ctx->user_data_ddt_header.length)
422 {
423 ctx->user_data_ddt_header.compression = None;
424 free(cmp_buffer);
425
426 cmp_buffer = (uint8_t *)ctx->user_data_ddt2;
427 }
428 }
429
430 if(ctx->user_data_ddt_header.compression == None)
431 {
432 ctx->user_data_ddt_header.cmpLength = ctx->user_data_ddt_header.length;
433 ctx->user_data_ddt_header.cmpCrc64 = ctx->user_data_ddt_header.crc64;
434 }
435 else
436 ctx->user_data_ddt_header.cmpCrc64 =
437 aaruf_crc64_data(cmp_buffer, (uint32_t)ctx->user_data_ddt_header.cmpLength);
438
439 if(ctx->user_data_ddt_header.compression == Lzma) ctx->user_data_ddt_header.cmpLength += LZMA_PROPERTIES_LENGTH;
440
441 // Write the DDT header first
442 fseek(ctx->imageStream, 0, SEEK_END);
443 long ddt_position = ftell(ctx->imageStream);
444 // Align index position to block boundary if needed
445 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
446 if(ddt_position & alignment_mask)
447 {
448 const uint64_t aligned_position = ddt_position + alignment_mask & ~alignment_mask;
449 fseek(ctx->imageStream, aligned_position, SEEK_SET);
450 ddt_position = aligned_position;
451 }
452
453 const size_t header_written = fwrite(&ctx->user_data_ddt_header, sizeof(DdtHeader2), 1, ctx->imageStream);
454 if(header_written != 1)
455 {
456 TRACE("Failed to write single-level DDT header to file");
457 return AARUF_ERROR_CANNOT_WRITE_HEADER;
458 }
459
460 // Write the primary table data
461 size_t written_bytes = 0;
462 if(ctx->user_data_ddt_header.compression == Lzma)
463 fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
464
465 written_bytes = fwrite(cmp_buffer, ctx->user_data_ddt_header.cmpLength, 1, ctx->imageStream);
466
467 if(written_bytes == 1)
468 {
469 TRACE("Successfully wrote single-level DDT header and table to file (%" PRIu64
470 " entries, %zu bytes, %zu compressed bytes)",
471 ctx->user_data_ddt_header.entries, ctx->user_data_ddt_header.length, ctx->user_data_ddt_header.cmpLength);
472
473 // Add single-level DDT to index
474 TRACE("Adding single-level DDT to index");
475 IndexEntry single_ddt_entry;
476 single_ddt_entry.blockType = DeDuplicationTable2;
477 single_ddt_entry.dataType = UserData;
478 single_ddt_entry.offset = ddt_position;
479
480 utarray_push_back(ctx->index_entries, &single_ddt_entry);
481 TRACE("Added single-level DDT index entry at offset %" PRIu64, ddt_position);
482 }
483 else
484 TRACE("Failed to write single-level DDT table data to file");
485
486 return AARUF_STATUS_OK;
487}
488
596 static int32_t write_tape_ddt(aaruformat_context *ctx)
597{
598 if(!ctx->is_tape) return AARUF_STATUS_INVALID_CONTEXT;
599
600 // Traverse the tape DDT uthash and find the biggest key
601 uint64_t max_key = 0;
602 TapeDdtHashEntry *entry, *tmp;
603 HASH_ITER(hh, ctx->tape_ddt, entry, tmp)
604 if(entry->key > max_key) max_key = entry->key;
605
606 // Initialize context user data DDT header
607 ctx->user_data_ddt_header.identifier = DeDuplicationTable2;
608 ctx->user_data_ddt_header.type = UserData;
609 ctx->user_data_ddt_header.compression = ctx->compression_enabled ? Lzma : None;
610 ctx->user_data_ddt_header.levels = 1; // Single level
611 ctx->user_data_ddt_header.tableLevel = 0; // Top level
612 ctx->user_data_ddt_header.previousLevelOffset = 0; // No previous level for single-level DDT
613 ctx->user_data_ddt_header.negative = 0;
614 ctx->user_data_ddt_header.overflow = 0;
615 ctx->user_data_ddt_header.tableShift = 0; // Single level
616 ctx->user_data_ddt_header.entries = max_key + 1;
617 ctx->user_data_ddt_header.blocks = max_key + 1;
618 ctx->user_data_ddt_header.start = 0;
619 ctx->user_data_ddt_header.length = ctx->user_data_ddt_header.entries * sizeof(uint64_t);
620 ctx->user_data_ddt_header.cmpLength = ctx->user_data_ddt_header.length;
621
622 // Initialize memory for user data DDT
623 ctx->user_data_ddt2 = calloc(ctx->user_data_ddt_header.entries, sizeof(uint64_t));
624 if(ctx->user_data_ddt2 == NULL)
625 {
626 TRACE("Failed to allocate memory for tape DDT table");
627 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
628 }
629
630 // Populate user data DDT from tape DDT uthash
631 HASH_ITER(hh, ctx->tape_ddt, entry, tmp)
632 if(entry->key < ctx->user_data_ddt_header.blocks) ctx->user_data_ddt2[entry->key] = entry->value;
633
634 // Do not repeat code
635 return write_single_level_ddt(ctx);
636}
637
654 static void write_checksum_block(aaruformat_context *ctx)
655{
656 uint64_t alignment_mask;
657 uint64_t aligned_position;
658
659 // Finalize pending checksums
660 if(ctx->calculating_md5)
661 {
662 ctx->checksums.hasMd5 = true;
663 aaruf_md5_final(&ctx->md5_context, ctx->checksums.md5);
664 }
665 if(ctx->calculating_sha1)
666 {
667 ctx->checksums.hasSha1 = true;
668 aaruf_sha1_final(&ctx->sha1_context, ctx->checksums.sha1);
669 }
670 if(ctx->calculating_sha256)
671 {
672 ctx->checksums.hasSha256 = true;
673 aaruf_sha256_final(&ctx->sha256_context, ctx->checksums.sha256);
674 }
675 if(ctx->calculating_spamsum)
676 {
677 ctx->checksums.hasSpamSum = true;
678 ctx->checksums.spamsum = calloc(1, FUZZY_MAX_RESULT);
679 aaruf_spamsum_final(ctx->spamsum_context, ctx->checksums.spamsum);
680 aaruf_spamsum_free(ctx->spamsum_context);
681 }
682 if(ctx->calculating_blake3)
683 {
684 ctx->checksums.hasBlake3 = true;
685 blake3_hasher_finalize(ctx->blake3_context, ctx->checksums.blake3, BLAKE3_OUT_LEN);
686 free(ctx->blake3_context);
687 }
688
689 // Write the checksums block
690 bool has_checksums = ctx->checksums.hasMd5 || ctx->checksums.hasSha1 || ctx->checksums.hasSha256 ||
691 ctx->checksums.hasSpamSum || ctx->checksums.hasBlake3;
692
693 if(!has_checksums) return;
694
695 ChecksumHeader checksum_header = {0};
696 checksum_header.identifier = ChecksumBlock;
697
698 fseek(ctx->imageStream, 0, SEEK_END);
699 long checksum_position = ftell(ctx->imageStream);
700 // Align index position to block boundary if needed
701 alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
702 if(checksum_position & alignment_mask)
703 {
704 aligned_position = checksum_position + alignment_mask & ~alignment_mask;
705 fseek(ctx->imageStream, aligned_position, SEEK_SET);
706 checksum_position = aligned_position;
707 }
708
709 // Skip checksum_header
710 fseek(ctx->imageStream, sizeof(checksum_header), SEEK_CUR);
711
712 if(ctx->checksums.hasMd5)
713 {
714 TRACE("Writing MD5 checksum entry");
715 ChecksumEntry md5_entry = {0};
716 md5_entry.length = MD5_DIGEST_LENGTH;
717 md5_entry.type = Md5;
718 fwrite(&md5_entry, sizeof(ChecksumEntry), 1, ctx->imageStream);
719 fwrite(&ctx->checksums.md5, MD5_DIGEST_LENGTH, 1, ctx->imageStream);
720 checksum_header.length += sizeof(ChecksumEntry) + MD5_DIGEST_LENGTH;
721 checksum_header.entries++;
722 }
723
724 if(ctx->checksums.hasSha1)
725 {
726 TRACE("Writing SHA1 checksum entry");
727 ChecksumEntry sha1_entry = {0};
728 sha1_entry.length = SHA1_DIGEST_LENGTH;
729 sha1_entry.type = Sha1;
730 fwrite(&sha1_entry, sizeof(ChecksumEntry), 1, ctx->imageStream);
731 fwrite(&ctx->checksums.sha1, SHA1_DIGEST_LENGTH, 1, ctx->imageStream);
732 checksum_header.length += sizeof(ChecksumEntry) + SHA1_DIGEST_LENGTH;
733 checksum_header.entries++;
734 }
735
736 if(ctx->checksums.hasSha256)
737 {
738 TRACE("Writing SHA256 checksum entry");
739 ChecksumEntry sha256_entry = {0};
740 sha256_entry.length = SHA256_DIGEST_LENGTH;
741 sha256_entry.type = Sha256;
742 fwrite(&sha256_entry, sizeof(ChecksumEntry), 1, ctx->imageStream);
743 fwrite(&ctx->checksums.sha256, SHA256_DIGEST_LENGTH, 1, ctx->imageStream);
744 checksum_header.length += sizeof(ChecksumEntry) + SHA256_DIGEST_LENGTH;
745 checksum_header.entries++;
746 }
747
748 if(ctx->checksums.hasSpamSum)
749 {
750 TRACE("Writing SpamSum checksum entry");
751 ChecksumEntry spamsum_entry = {0};
752 spamsum_entry.length = strlen((const char *)ctx->checksums.spamsum);
753 spamsum_entry.type = SpamSum;
754 fwrite(&spamsum_entry, sizeof(ChecksumEntry), 1, ctx->imageStream);
755 fwrite(ctx->checksums.spamsum, spamsum_entry.length, 1, ctx->imageStream);
756 checksum_header.length += sizeof(ChecksumEntry) + spamsum_entry.length;
757 checksum_header.entries++;
758 }
759
760 if(ctx->checksums.hasBlake3)
761 {
762 TRACE("Writing BLAKE3 checksum entry");
763 ChecksumEntry blake3_entry = {0};
764 blake3_entry.length = BLAKE3_OUT_LEN;
765 blake3_entry.type = Blake3;
766 fwrite(&blake3_entry, sizeof(ChecksumEntry), 1, ctx->imageStream);
767 fwrite(&ctx->checksums.blake3, BLAKE3_OUT_LEN, 1, ctx->imageStream);
768 checksum_header.length += sizeof(ChecksumEntry) + BLAKE3_OUT_LEN;
769 checksum_header.entries++;
770 ctx->header.featureCompatible |= AARU_FEATURE_RW_BLAKE3;
771 }
772
773 fseek(ctx->imageStream, checksum_position, SEEK_SET);
774 TRACE("Writing checksum header");
775 fwrite(&checksum_header, sizeof(ChecksumHeader), 1, ctx->imageStream);
776
777 // Add checksum block to index
778 TRACE("Adding checksum block to index");
779 IndexEntry checksum_index_entry;
780 checksum_index_entry.blockType = ChecksumBlock;
781 checksum_index_entry.dataType = 0;
782 checksum_index_entry.offset = checksum_position;
783
784 utarray_push_back(ctx->index_entries, &checksum_index_entry);
785 TRACE("Added checksum block index entry at offset %" PRIu64, checksum_position);
786}
787
798 static void write_tracks_block(aaruformat_context *ctx)
799{
800 // Write tracks block
801 if(ctx->tracks_header.entries <= 0 || ctx->track_entries == NULL) return;
802
803 fseek(ctx->imageStream, 0, SEEK_END);
804 long tracks_position = ftell(ctx->imageStream);
805 // Align index position to block boundary if needed
806 uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
807 if(tracks_position & alignment_mask)
808 {
809 uint64_t aligned_position = tracks_position + alignment_mask & ~alignment_mask;
810 fseek(ctx->imageStream, aligned_position, SEEK_SET);
811 tracks_position = aligned_position;
812 }
813
814 TRACE("Writing tracks block at position %ld", tracks_position);
815 // Write header
816 if(fwrite(&ctx->tracks_header, sizeof(TracksHeader), 1, ctx->imageStream) == 1)
817 {
818 // Write entries
819 size_t written_entries =
820 fwrite(ctx->track_entries, sizeof(TrackEntry), ctx->tracks_header.entries, ctx->imageStream);
821
822 if(written_entries == ctx->tracks_header.entries)
823 {
824 TRACE("Successfully wrote tracks block with %u entries", ctx->tracks_header.entries);
825 // Add tracks block to index
826 TRACE("Adding tracks block to index");
827
828 IndexEntry tracks_index_entry;
829 tracks_index_entry.blockType = TracksBlock;
830 tracks_index_entry.dataType = 0;
831 tracks_index_entry.offset = tracks_position;
832 utarray_push_back(ctx->index_entries, &tracks_index_entry);
833 TRACE("Added tracks block index entry at offset %" PRIu64, tracks_position);
834 }
835 }
836}
837
850 static void write_mode2_subheaders_block(aaruformat_context *ctx)
851{
852 // Write MODE 2 subheader data block
853 if(ctx->mode2_subheaders == NULL) return;
854
855 fseek(ctx->imageStream, 0, SEEK_END);
856 long mode2_subheaders_position = ftell(ctx->imageStream);
857 // Align index position to block boundary if needed
858 uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
859 if(mode2_subheaders_position & alignment_mask)
860 {
861 uint64_t aligned_position = mode2_subheaders_position + alignment_mask & ~alignment_mask;
862 fseek(ctx->imageStream, aligned_position, SEEK_SET);
863 mode2_subheaders_position = aligned_position;
864 }
865
866 TRACE("Writing MODE 2 subheaders block at position %ld", mode2_subheaders_position);
867 BlockHeader subheaders_block = {0};
868 subheaders_block.identifier = DataBlock;
869 subheaders_block.type = CompactDiscMode2Subheader;
870 subheaders_block.compression = ctx->compression_enabled ? Lzma : None;
871 subheaders_block.length =
872 (uint32_t)(ctx->user_data_ddt_header.negative + ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow) *
873 8;
874
875 // Calculate CRC64
876 subheaders_block.crc64 = aaruf_crc64_data(ctx->mode2_subheaders, subheaders_block.length);
877
878 uint8_t *buffer = NULL;
879 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
880
881 if(subheaders_block.compression == None)
882 {
883 buffer = ctx->mode2_subheaders;
884 subheaders_block.cmpCrc64 = subheaders_block.crc64;
885 }
886 else
887 {
888 buffer = malloc((size_t)subheaders_block.length * 2); // Allocate double size for compression
889 if(buffer == NULL)
890 {
891 TRACE("Failed to allocate memory for MODE 2 subheaders compression");
892 return;
893 }
894
895 size_t dst_size = (size_t)subheaders_block.length * 2 * 2;
896 size_t props_size = LZMA_PROPERTIES_LENGTH;
897 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->mode2_subheaders, subheaders_block.length, lzma_properties,
898 &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
899
900 subheaders_block.cmpLength = (uint32_t)dst_size;
901
902 if(subheaders_block.cmpLength >= subheaders_block.length)
903 {
904 subheaders_block.compression = None;
905 free(buffer);
906 buffer = ctx->mode2_subheaders;
907 }
908 }
909
910 if(subheaders_block.compression == None)
911 {
912 subheaders_block.cmpLength = subheaders_block.length;
913 subheaders_block.cmpCrc64 = subheaders_block.crc64;
914 }
915 else
916 subheaders_block.cmpCrc64 = aaruf_crc64_data(buffer, subheaders_block.cmpLength);
917
918 if(subheaders_block.compression == Lzma) subheaders_block.cmpLength += LZMA_PROPERTIES_LENGTH;
919
920 // Write header
921 if(fwrite(&subheaders_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
922 {
923 if(subheaders_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
924
925 // Write data
926 size_t written_bytes = fwrite(buffer, subheaders_block.cmpLength, 1, ctx->imageStream);
927 if(written_bytes == 1)
928 {
929 TRACE("Successfully wrote MODE 2 subheaders block (%" PRIu64 " bytes)", subheaders_block.cmpLength);
930 // Add MODE 2 subheaders block to index
931 TRACE("Adding MODE 2 subheaders block to index");
932 IndexEntry mode2_subheaders_index_entry;
933 mode2_subheaders_index_entry.blockType = DataBlock;
934 mode2_subheaders_index_entry.dataType = CompactDiscMode2Subheader;
935 mode2_subheaders_index_entry.offset = mode2_subheaders_position;
936 utarray_push_back(ctx->index_entries, &mode2_subheaders_index_entry);
937 TRACE("Added MODE 2 subheaders block index entry at offset %" PRIu64, mode2_subheaders_position);
938 }
939 }
940
941 if(subheaders_block.compression == Lzma) free(buffer);
942}
943
966 static void write_sector_prefix(aaruformat_context *ctx)
967{
968 if(ctx->sector_prefix == NULL) return;
969
970 fseek(ctx->imageStream, 0, SEEK_END);
971 long prefix_position = ftell(ctx->imageStream);
972 // Align index position to block boundary if needed
973 uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
974 if(prefix_position & alignment_mask)
975 {
976 uint64_t aligned_position = prefix_position + alignment_mask & ~alignment_mask;
977 fseek(ctx->imageStream, aligned_position, SEEK_SET);
978 prefix_position = aligned_position;
979 }
980
981 TRACE("Writing sector prefix block at position %ld", prefix_position);
982 BlockHeader prefix_block = {0};
983 prefix_block.identifier = DataBlock;
984 prefix_block.type = CdSectorPrefix;
985 prefix_block.compression = ctx->compression_enabled ? Lzma : None;
986 prefix_block.length = (uint32_t)ctx->sector_prefix_offset;
987
988 // Calculate CRC64
989 prefix_block.crc64 = aaruf_crc64_data(ctx->sector_prefix, prefix_block.length);
990
991 uint8_t *buffer = NULL;
992 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
993
994 if(prefix_block.compression == None)
995 {
996 buffer = ctx->sector_prefix;
997 prefix_block.cmpCrc64 = prefix_block.crc64;
998 }
999 else
1000 {
1001 buffer = malloc((size_t)prefix_block.length * 2); // Allocate double size for compression
1002 if(buffer == NULL)
1003 {
1004 TRACE("Failed to allocate memory for CD sector prefix compression");
1005 return;
1006 }
1007
1008 size_t dst_size = (size_t)prefix_block.length * 2 * 2;
1009 size_t props_size = LZMA_PROPERTIES_LENGTH;
1010 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_prefix, prefix_block.length, lzma_properties,
1011 &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1012
1013 prefix_block.cmpLength = (uint32_t)dst_size;
1014
1015 if(prefix_block.cmpLength >= prefix_block.length)
1016 {
1017 prefix_block.compression = None;
1018 free(buffer);
1019 buffer = ctx->sector_prefix;
1020 }
1021 }
1022
1023 if(prefix_block.compression == None)
1024 {
1025 prefix_block.cmpLength = prefix_block.length;
1026 prefix_block.cmpCrc64 = prefix_block.crc64;
1027 }
1028 else
1029 prefix_block.cmpCrc64 = aaruf_crc64_data(buffer, prefix_block.cmpLength);
1030
1031 if(prefix_block.compression == Lzma) prefix_block.cmpLength += LZMA_PROPERTIES_LENGTH;
1032
1033 // Write header
1034 if(fwrite(&prefix_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
1035 {
1036 if(prefix_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1037
1038 // Write data
1039 const size_t written_bytes = fwrite(buffer, prefix_block.cmpLength, 1, ctx->imageStream);
1040 if(written_bytes == 1)
1041 {
1042 TRACE("Successfully wrote CD sector prefix block (%" PRIu64 " bytes)", prefix_block.cmpLength);
1043 // Add prefix block to index
1044 TRACE("Adding CD sector prefix block to index");
1045 IndexEntry prefix_index_entry;
1046 prefix_index_entry.blockType = DataBlock;
1047 prefix_index_entry.dataType = CdSectorPrefix;
1048 prefix_index_entry.offset = prefix_position;
1049 utarray_push_back(ctx->index_entries, &prefix_index_entry);
1050 TRACE("Added CD sector prefix block index entry at offset %" PRIu64, prefix_position);
1051 }
1052 }
1053
1054 if(prefix_block.compression == Lzma) free(buffer);
1055}
1056
1088 static void write_sector_suffix(aaruformat_context *ctx)
1089{
1090 if(ctx->sector_suffix == NULL) return;
1091
1092 fseek(ctx->imageStream, 0, SEEK_END);
1093 long suffix_position = ftell(ctx->imageStream);
1094 // Align index position to block boundary if needed
1095 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1096 if(suffix_position & alignment_mask)
1097 {
1098 const uint64_t aligned_position = suffix_position + alignment_mask & ~alignment_mask;
1099 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1100 suffix_position = aligned_position;
1101 }
1102
1103 TRACE("Writing sector suffix block at position %ld", suffix_position);
1104 BlockHeader suffix_block = {0};
1105 suffix_block.identifier = DataBlock;
1106 suffix_block.type = CdSectorSuffix;
1107 suffix_block.compression = ctx->compression_enabled ? Lzma : None;
1108 suffix_block.length = (uint32_t)ctx->sector_suffix_offset;
1109
1110 // Calculate CRC64
1111 suffix_block.crc64 = aaruf_crc64_data(ctx->sector_suffix, suffix_block.length);
1112
1113 uint8_t *buffer = NULL;
1114 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1115
1116 if(suffix_block.compression == None)
1117 {
1118 buffer = ctx->sector_suffix;
1119 suffix_block.cmpCrc64 = suffix_block.crc64;
1120 }
1121 else
1122 {
1123 buffer = malloc((size_t)suffix_block.length * 2); // Allocate double size for compression
1124 if(buffer == NULL)
1125 {
1126 TRACE("Failed to allocate memory for CD sector suffix compression");
1127 return;
1128 }
1129
1130 size_t dst_size = (size_t)suffix_block.length * 2 * 2;
1131 size_t props_size = LZMA_PROPERTIES_LENGTH;
1132 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_suffix, suffix_block.length, lzma_properties,
1133 &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1134
1135 suffix_block.cmpLength = (uint32_t)dst_size;
1136
1137 if(suffix_block.cmpLength >= suffix_block.length)
1138 {
1139 suffix_block.compression = None;
1140 free(buffer);
1141 buffer = ctx->sector_suffix;
1142 }
1143 }
1144
1145 if(suffix_block.compression == None)
1146 {
1147 suffix_block.cmpLength = suffix_block.length;
1148 suffix_block.cmpCrc64 = suffix_block.crc64;
1149 }
1150 else
1151 suffix_block.cmpCrc64 = aaruf_crc64_data(buffer, suffix_block.cmpLength);
1152
1153 if(suffix_block.compression == Lzma) suffix_block.cmpLength += LZMA_PROPERTIES_LENGTH;
1154
1155 // Write header
1156 if(fwrite(&suffix_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
1157 {
1158 if(suffix_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1159
1160 // Write data
1161 const size_t written_bytes = fwrite(buffer, suffix_block.cmpLength, 1, ctx->imageStream);
1162 if(written_bytes == 1)
1163 {
1164 TRACE("Successfully wrote CD sector suffix block (%" PRIu64 " bytes)", suffix_block.cmpLength);
1165 // Add suffix block to index
1166 TRACE("Adding CD sector suffix block to index");
1167 IndexEntry suffix_index_entry;
1168 suffix_index_entry.blockType = DataBlock;
1169 suffix_index_entry.dataType = CdSectorSuffix;
1170 suffix_index_entry.offset = suffix_position;
1171 utarray_push_back(ctx->index_entries, &suffix_index_entry);
1172 TRACE("Added CD sector suffix block index entry at offset %" PRIu64, suffix_position);
1173 }
1174 }
1175
1176 if(suffix_block.compression == Lzma) free(buffer);
1177}
1178
1206 static void write_sector_prefix_ddt(aaruformat_context *ctx)
1207{
1208 if(ctx->sector_prefix_ddt2 == NULL) return;
1209
1210 fseek(ctx->imageStream, 0, SEEK_END);
1211 long prefix_ddt_position = ftell(ctx->imageStream);
1212 // Align index position to block boundary if needed
1213 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1214 if(prefix_ddt_position & alignment_mask)
1215 {
1216 const uint64_t aligned_position = prefix_ddt_position + alignment_mask & ~alignment_mask;
1217 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1218 prefix_ddt_position = aligned_position;
1219 }
1220
1221 TRACE("Writing sector prefix DDT v2 at position %ld", prefix_ddt_position);
1222 DdtHeader2 ddt_header2 = {0};
1223 ddt_header2.identifier = DeDuplicationTable2;
1224 ddt_header2.type = CdSectorPrefix;
1225 ddt_header2.compression = ctx->compression_enabled ? Lzma : None;
1226 ddt_header2.levels = 1;
1227 ddt_header2.tableLevel = 0;
1228 ddt_header2.negative = ctx->user_data_ddt_header.negative;
1229 ddt_header2.overflow = ctx->user_data_ddt_header.overflow;
1230 ddt_header2.blockAlignmentShift = ctx->user_data_ddt_header.blockAlignmentShift;
1231 ddt_header2.dataShift = ctx->user_data_ddt_header.dataShift;
1232 ddt_header2.tableShift = 0; // Single-level DDT
1233 ddt_header2.entries =
1234 ctx->image_info.Sectors + ctx->user_data_ddt_header.negative + ctx->user_data_ddt_header.overflow;
1235 ddt_header2.blocks = ctx->user_data_ddt_header.blocks;
1236 ddt_header2.start = 0;
1237 ddt_header2.length = ddt_header2.entries * sizeof(uint64_t);
1238 // Calculate CRC64
1239 ddt_header2.crc64 = aaruf_crc64_data((uint8_t *)ctx->sector_prefix_ddt2, (uint32_t)ddt_header2.length);
1240
1241 uint8_t *buffer = NULL;
1242 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1243
1244 if(ddt_header2.compression == None)
1245 {
1246 buffer = (uint8_t *)ctx->sector_prefix_ddt2;
1247 ddt_header2.cmpCrc64 = ddt_header2.crc64;
1248 }
1249 else
1250 {
1251 buffer = malloc((size_t)ddt_header2.length * 2); // Allocate double size for compression
1252 if(buffer == NULL)
1253 {
1254 TRACE("Failed to allocate memory for sector prefix DDT v2 compression");
1255 return;
1256 }
1257
1258 size_t dst_size = (size_t)ddt_header2.length * 2 * 2;
1259 size_t props_size = LZMA_PROPERTIES_LENGTH;
1260 aaruf_lzma_encode_buffer(buffer, &dst_size, (uint8_t *)ctx->sector_prefix_ddt2, ddt_header2.length,
1261 lzma_properties, &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1262
1263 ddt_header2.cmpLength = (uint32_t)dst_size;
1264
1265 if(ddt_header2.cmpLength >= ddt_header2.length)
1266 {
1267 ddt_header2.compression = None;
1268 free(buffer);
1269 buffer = (uint8_t *)ctx->sector_prefix_ddt2;
1270 }
1271 }
1272
1273 if(ddt_header2.compression == None)
1274 {
1275 ddt_header2.cmpLength = ddt_header2.length;
1276 ddt_header2.cmpCrc64 = ddt_header2.crc64;
1277 }
1278 else
1279 ddt_header2.cmpCrc64 = aaruf_crc64_data(buffer, (uint32_t)ddt_header2.cmpLength);
1280
1281 if(ddt_header2.compression == Lzma) ddt_header2.cmpLength += LZMA_PROPERTIES_LENGTH;
1282
1283 // Write header
1284 if(fwrite(&ddt_header2, sizeof(DdtHeader2), 1, ctx->imageStream) == 1)
1285 {
1286 if(ddt_header2.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1287
1288 // Write data
1289 const size_t written_bytes = fwrite(buffer, ddt_header2.cmpLength, 1, ctx->imageStream);
1290 if(written_bytes == 1)
1291 {
1292 TRACE("Successfully wrote sector prefix DDT v2 (%" PRIu64 " bytes)", ddt_header2.cmpLength);
1293 // Add prefix block to index
1294 TRACE("Adding sector prefix DDT v2 to index");
1295 IndexEntry prefix_ddt_index_entry;
1296 prefix_ddt_index_entry.blockType = DeDuplicationTable2;
1297 prefix_ddt_index_entry.dataType = CdSectorPrefix;
1298 prefix_ddt_index_entry.offset = prefix_ddt_position;
1299 utarray_push_back(ctx->index_entries, &prefix_ddt_index_entry);
1300 TRACE("Added sector prefix DDT v2 index entry at offset %" PRIu64, prefix_ddt_position);
1301 }
1302 }
1303
1304 if(ddt_header2.compression == Lzma) free(buffer);
1305}
1306
1350 static void write_sector_suffix_ddt(aaruformat_context *ctx)
1351{
1352 if(ctx->sector_suffix_ddt2 == NULL) return;
1353
1354 fseek(ctx->imageStream, 0, SEEK_END);
1355 long suffix_ddt_position = ftell(ctx->imageStream);
1356 // Align index position to block boundary if needed
1357 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1358 if(suffix_ddt_position & alignment_mask)
1359 {
1360 const uint64_t aligned_position = suffix_ddt_position + alignment_mask & ~alignment_mask;
1361 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1362 suffix_ddt_position = aligned_position;
1363 }
1364
1365 TRACE("Writing sector suffix DDT v2 at position %ld", suffix_ddt_position);
1366 DdtHeader2 ddt_header2 = {0};
1367 ddt_header2.identifier = DeDuplicationTable2;
1368 ddt_header2.type = CdSectorSuffix;
1369 ddt_header2.compression = ctx->compression_enabled ? Lzma : None;
1370 ddt_header2.levels = 1;
1371 ddt_header2.tableLevel = 0;
1372 ddt_header2.negative = ctx->user_data_ddt_header.negative;
1373 ddt_header2.overflow = ctx->user_data_ddt_header.overflow;
1374 ddt_header2.blockAlignmentShift = ctx->user_data_ddt_header.blockAlignmentShift;
1375 ddt_header2.dataShift = ctx->user_data_ddt_header.dataShift;
1376 ddt_header2.tableShift = 0; // Single-level DDT
1377 ddt_header2.entries =
1378 ctx->image_info.Sectors + ctx->user_data_ddt_header.negative + ctx->user_data_ddt_header.overflow;
1379 ddt_header2.blocks = ctx->user_data_ddt_header.blocks;
1380 ddt_header2.start = 0;
1381 ddt_header2.length = ddt_header2.entries * sizeof(uint64_t);
1382 // Calculate CRC64
1383 ddt_header2.crc64 = aaruf_crc64_data((uint8_t *)ctx->sector_suffix_ddt2, (uint32_t)ddt_header2.length);
1384
1385 uint8_t *buffer = NULL;
1386 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1387
1388 if(ddt_header2.compression == None)
1389 {
1390 buffer = (uint8_t *)ctx->sector_suffix_ddt2;
1391 ddt_header2.cmpCrc64 = ddt_header2.crc64;
1392 }
1393 else
1394 {
1395 buffer = malloc((size_t)ddt_header2.length * 2); // Allocate double size for compression
1396 if(buffer == NULL)
1397 {
1398 TRACE("Failed to allocate memory for sector suffix DDT v2 compression");
1399 return;
1400 }
1401
1402 size_t dst_size = (size_t)ddt_header2.length * 2 * 2;
1403 size_t props_size = LZMA_PROPERTIES_LENGTH;
1404 aaruf_lzma_encode_buffer(buffer, &dst_size, (uint8_t *)ctx->sector_suffix_ddt2, ddt_header2.length,
1405 lzma_properties, &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1406
1407 ddt_header2.cmpLength = (uint32_t)dst_size;
1408
1409 if(ddt_header2.cmpLength >= ddt_header2.length)
1410 {
1411 ddt_header2.compression = None;
1412 free(buffer);
1413 buffer = (uint8_t *)ctx->sector_suffix_ddt2;
1414 }
1415 }
1416
1417 if(ddt_header2.compression == None)
1418 {
1419 ddt_header2.cmpLength = ddt_header2.length;
1420 ddt_header2.cmpCrc64 = ddt_header2.crc64;
1421 }
1422 else
1423 ddt_header2.cmpCrc64 = aaruf_crc64_data(buffer, (uint32_t)ddt_header2.cmpLength);
1424
1425 if(ddt_header2.compression == Lzma) ddt_header2.cmpLength += LZMA_PROPERTIES_LENGTH;
1426
1427 // Write header
1428 if(fwrite(&ddt_header2, sizeof(DdtHeader2), 1, ctx->imageStream) == 1)
1429 {
1430 if(ddt_header2.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1431
1432 // Write data
1433 const size_t written_bytes = fwrite(buffer, ddt_header2.cmpLength, 1, ctx->imageStream);
1434 if(written_bytes == 1)
1435 {
1436 TRACE("Successfully wrote sector suffix DDT v2 (%" PRIu64 " bytes)", ddt_header2.cmpLength);
1437 // Add suffix block to index
1438 TRACE("Adding sector suffix DDT v2 to index");
1439 IndexEntry suffix_ddt_index_entry;
1440 suffix_ddt_index_entry.blockType = DeDuplicationTable2;
1441 suffix_ddt_index_entry.dataType = CdSectorSuffix;
1442 suffix_ddt_index_entry.offset = suffix_ddt_position;
1443 utarray_push_back(ctx->index_entries, &suffix_ddt_index_entry);
1444 TRACE("Added sector suffix DDT v2 index entry at offset %" PRIu64, suffix_ddt_position);
1445 }
1446 }
1447
1448 if(ddt_header2.compression == Lzma) free(buffer);
1449}
1450
1508 static void write_sector_subchannel(const aaruformat_context *ctx)
1509{
1510 if(ctx->sector_subchannel == NULL) return;
1511
1512 fseek(ctx->imageStream, 0, SEEK_END);
1513 long block_position = ftell(ctx->imageStream);
1514 // Align index position to block boundary if needed
1515 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1516 if(block_position & alignment_mask)
1517 {
1518 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
1519 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1520 block_position = aligned_position;
1521 }
1522
1523 TRACE("Writing sector subchannel block at position %ld", block_position);
1524 BlockHeader subchannel_block = {0};
1525 subchannel_block.identifier = DataBlock;
1526 subchannel_block.compression = None;
1527
1528 uint8_t *buffer = ctx->sector_subchannel;
1529 bool owns_buffer = false;
1530 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1531
1532 subchannel_block.cmpLength = subchannel_block.length;
1533
1534 if(ctx->image_info.MetadataMediaType == OpticalDisc)
1535 {
1536 subchannel_block.type = CdSectorSubchannel;
1537 subchannel_block.length = (uint32_t)(ctx->user_data_ddt_header.negative + ctx->image_info.Sectors +
1538 ctx->user_data_ddt_header.overflow) *
1539 96;
1540
1541 if(ctx->compression_enabled)
1542 {
1543 uint8_t *cst_buffer = malloc(subchannel_block.length);
1544
1545 if(cst_buffer == NULL)
1546 {
1547 TRACE("Failed to allocate memory for Claunia Subchannel Transform output");
1548 return;
1549 }
1550
1551 uint8_t *dst_buffer = malloc(subchannel_block.length);
1552
1553 if(dst_buffer == NULL)
1554 {
1555 TRACE("Failed to allocate memory for LZMA output");
1556 free(cst_buffer);
1557 return;
1558 }
1559
1560 aaruf_cst_transform(ctx->sector_subchannel, cst_buffer, subchannel_block.length);
1561 size_t dst_size = subchannel_block.length;
1562 size_t props_size = LZMA_PROPERTIES_LENGTH;
1563
1564 aaruf_lzma_encode_buffer(dst_buffer, &dst_size, cst_buffer, subchannel_block.length, lzma_properties,
1565 &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1566
1567 free(cst_buffer);
1568
1569 if(dst_size < subchannel_block.length)
1570 {
1571 subchannel_block.compression = LzmaClauniaSubchannelTransform;
1572 subchannel_block.cmpLength = (uint32_t)dst_size;
1573 buffer = dst_buffer;
1574 owns_buffer = true;
1575 }
1576 else
1577 {
1578 subchannel_block.compression = None;
1579 free(dst_buffer);
1580 subchannel_block.cmpLength = subchannel_block.length;
1581 }
1582 }
1583 }
1584 else if(ctx->image_info.MetadataMediaType == BlockMedia)
1585 {
1586 switch(ctx->image_info.MediaType)
1587 {
1588 case AppleProfile:
1589 case AppleFileWare:
1590 subchannel_block.type = AppleProfileTag;
1591 subchannel_block.length = (uint32_t)(ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow) * 20;
1592 break;
1593 case AppleSonyDS:
1594 case AppleSonySS:
1595 subchannel_block.type = AppleSonyTag;
1596 subchannel_block.length = (uint32_t)(ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow) * 12;
1597 break;
1598 case PriamDataTower:
1599 subchannel_block.type = PriamDataTowerTag;
1600 subchannel_block.length = (uint32_t)(ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow) * 24;
1601 break;
1602 default:
1603 TRACE("Incorrect media type, not writing sector subchannel block");
1604 return; // Incorrect media type
1605 }
1606 subchannel_block.compression = Lzma;
1607
1608 uint8_t *dst_buffer = malloc(subchannel_block.length);
1609
1610 if(dst_buffer == NULL)
1611 {
1612 TRACE("Failed to allocate memory for LZMA output");
1613 return;
1614 }
1615
1616 size_t dst_size = subchannel_block.length;
1617 size_t props_size = LZMA_PROPERTIES_LENGTH;
1618
1619 aaruf_lzma_encode_buffer(dst_buffer, &dst_size, ctx->sector_subchannel, subchannel_block.length,
1620 lzma_properties, &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1621
1622 if(dst_size < subchannel_block.length)
1623 {
1624 subchannel_block.cmpLength = (uint32_t)dst_size;
1625 buffer = dst_buffer;
1626 owns_buffer = true;
1627 }
1628 else
1629 {
1630 subchannel_block.compression = None;
1631 free(dst_buffer);
1632 subchannel_block.cmpLength = subchannel_block.length;
1633 }
1634 }
1635 else
1636 {
1637 TRACE("Incorrect media type, not writing sector subchannel block");
1638 return; // Incorrect media type
1639 }
1640
1641 // Calculate CRC64 for raw subchannel data and compressed payload when present
1642 subchannel_block.crc64 = aaruf_crc64_data(ctx->sector_subchannel, subchannel_block.length);
1643 if(subchannel_block.compression == None)
1644 subchannel_block.cmpCrc64 = subchannel_block.crc64;
1645 else
1646 subchannel_block.cmpCrc64 = aaruf_crc64_data(buffer, subchannel_block.cmpLength);
1647
1648 // Write header
1649 if(fwrite(&subchannel_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
1650 {
1651 if(subchannel_block.compression != None) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1652
1653 // Write data
1654 const size_t written_bytes = fwrite(buffer, subchannel_block.cmpLength, 1, ctx->imageStream);
1655 if(written_bytes == 1)
1656 {
1657 TRACE("Successfully wrote sector subchannel block (%" PRIu64 " bytes)", subchannel_block.cmpLength);
1658 // Add subchannel block to index
1659 TRACE("Adding sector subchannel block to index");
1660 IndexEntry subchannel_index_entry;
1661 subchannel_index_entry.blockType = DataBlock;
1662 subchannel_index_entry.dataType = subchannel_block.type;
1663 subchannel_index_entry.offset = block_position;
1664 utarray_push_back(ctx->index_entries, &subchannel_index_entry);
1665 TRACE("Added sector subchannel block index entry at offset %" PRIu64, block_position);
1666 }
1667 }
1668
1669 if(owns_buffer) free(buffer);
1670}
1671
1808 void write_dvd_long_sector_blocks(aaruformat_context *ctx)
1809{
1810 if(ctx->sector_id == NULL || ctx->sector_ied == NULL || ctx->sector_cpr_mai == NULL || ctx->sector_edc == NULL)
1811 return;
1812
1813 uint64_t total_sectors =
1814 ctx->user_data_ddt_header.negative + ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow;
1815
1816 // Write DVD sector ID block
1817 fseek(ctx->imageStream, 0, SEEK_END);
1818 long id_position = ftell(ctx->imageStream);
1819 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
1820 if(id_position & alignment_mask)
1821 {
1822 const uint64_t aligned_position = id_position + alignment_mask & ~alignment_mask;
1823 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1824 id_position = aligned_position;
1825 }
1826 TRACE("Writing DVD sector ID block at position %ld", id_position);
1827 BlockHeader id_block = {0};
1828 id_block.identifier = DataBlock;
1829 id_block.type = DvdSectorId;
1830 id_block.compression = ctx->compression_enabled ? Lzma : None;
1831 id_block.length = (uint32_t)total_sectors * 4;
1832
1833 // Calculate CRC64
1834 id_block.crc64 = aaruf_crc64_data(ctx->sector_id, id_block.length);
1835
1836 uint8_t *buffer = NULL;
1837 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
1838
1839 if(id_block.compression == None)
1840 {
1841 buffer = ctx->sector_id;
1842 id_block.cmpCrc64 = id_block.crc64;
1843 }
1844 else
1845 {
1846 buffer = malloc((size_t)id_block.length * 2); // Allocate double size for compression
1847 if(buffer == NULL)
1848 {
1849 TRACE("Failed to allocate memory for DVD sector ID compression");
1850 return;
1851 }
1852
1853 size_t dst_size = (size_t)id_block.length * 2 * 2;
1854 size_t props_size = LZMA_PROPERTIES_LENGTH;
1855 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_id, id_block.length, lzma_properties, &props_size, 9,
1856 ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1857
1858 id_block.cmpLength = (uint32_t)dst_size;
1859
1860 if(id_block.cmpLength >= id_block.length)
1861 {
1862 id_block.compression = None;
1863 free(buffer);
1864 buffer = ctx->sector_id;
1865 }
1866 }
1867
1868 if(id_block.compression == None)
1869 {
1870 id_block.cmpLength = id_block.length;
1871 id_block.cmpCrc64 = id_block.crc64;
1872 }
1873 else
1874 id_block.cmpCrc64 = aaruf_crc64_data(buffer, id_block.cmpLength);
1875
1876 if(id_block.compression == Lzma) id_block.cmpLength += LZMA_PROPERTIES_LENGTH;
1877
1878 // Write header
1879 if(fwrite(&id_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
1880 {
1881 if(id_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1882
1883 // Write data
1884 const size_t written_bytes = fwrite(buffer, id_block.cmpLength, 1, ctx->imageStream);
1885 if(written_bytes == 1)
1886 {
1887 TRACE("Successfully wrote DVD sector ID block (%" PRIu64 " bytes)", id_block.cmpLength);
1888 // Add ID block to index
1889 TRACE("Adding DVD sector ID block to index");
1890 IndexEntry id_index_entry;
1891 id_index_entry.blockType = DataBlock;
1892 id_index_entry.dataType = DvdSectorId;
1893 id_index_entry.offset = id_position;
1894 utarray_push_back(ctx->index_entries, &id_index_entry);
1895 TRACE("Added DVD sector ID block index entry at offset %" PRIu64, id_position);
1896 }
1897 }
1898
1899 if(id_block.compression == Lzma) free(buffer);
1900
1901 // Write DVD sector IED block
1902 fseek(ctx->imageStream, 0, SEEK_END);
1903 long ied_position = ftell(ctx->imageStream);
1904 if(ied_position & alignment_mask)
1905 {
1906 const uint64_t aligned_position = ied_position + alignment_mask & ~alignment_mask;
1907 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1908 ied_position = aligned_position;
1909 }
1910 TRACE("Writing DVD sector IED block at position %ld", ied_position);
1911 BlockHeader ied_block = {0};
1912 ied_block.identifier = DataBlock;
1913 ied_block.type = DvdSectorIed;
1914 ied_block.compression = ctx->compression_enabled ? Lzma : None;
1915 ied_block.length = (uint32_t)total_sectors * 2;
1916 // Calculate CRC64
1917 ied_block.crc64 = aaruf_crc64_data(ctx->sector_ied, ied_block.length);
1918
1919 buffer = NULL;
1920
1921 if(ied_block.compression == None)
1922 {
1923 buffer = ctx->sector_ied;
1924 ied_block.cmpCrc64 = ied_block.crc64;
1925 }
1926 else
1927 {
1928 buffer = malloc((size_t)ied_block.length * 2); // Allocate double size for compression
1929 if(buffer == NULL)
1930 {
1931 TRACE("Failed to allocate memory for DVD sector IED compression");
1932 return;
1933 }
1934
1935 size_t dst_size = (size_t)ied_block.length * 2 * 2;
1936 size_t props_size = LZMA_PROPERTIES_LENGTH;
1937 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_ied, ied_block.length, lzma_properties, &props_size, 9,
1938 ctx->lzma_dict_size, 4, 0, 2, 273, 8);
1939
1940 ied_block.cmpLength = (uint32_t)dst_size;
1941
1942 if(ied_block.cmpLength >= ied_block.length)
1943 {
1944 ied_block.compression = None;
1945 free(buffer);
1946 buffer = ctx->sector_ied;
1947 }
1948 }
1949
1950 if(ied_block.compression == None)
1951 {
1952 ied_block.cmpLength = ied_block.length;
1953 ied_block.cmpCrc64 = ied_block.crc64;
1954 }
1955 else
1956 ied_block.cmpCrc64 = aaruf_crc64_data(buffer, ied_block.cmpLength);
1957
1958 if(ied_block.compression == Lzma) ied_block.cmpLength += LZMA_PROPERTIES_LENGTH;
1959
1960 // Write header
1961 if(fwrite(&ied_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
1962 {
1963 if(ied_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
1964
1965 // Write data
1966 const size_t written_bytes = fwrite(buffer, ied_block.cmpLength, 1, ctx->imageStream);
1967 if(written_bytes == 1)
1968 {
1969 TRACE("Successfully wrote DVD sector IED block (%" PRIu64 " bytes)", ied_block.cmpLength);
1970 // Add IED block to index
1971 TRACE("Adding DVD sector IED block to index");
1972 IndexEntry ied_index_entry;
1973 ied_index_entry.blockType = DataBlock;
1974 ied_index_entry.dataType = DvdSectorIed;
1975 ied_index_entry.offset = ied_position;
1976 utarray_push_back(ctx->index_entries, &ied_index_entry);
1977 TRACE("Added DVD sector IED block index entry at offset %" PRIu64, ied_position);
1978 }
1979 }
1980
1981 if(ied_block.compression == Lzma) free(buffer);
1982
1983 // Write DVD sector CPR/MAI block
1984 fseek(ctx->imageStream, 0, SEEK_END);
1985 long cpr_mai_position = ftell(ctx->imageStream);
1986 if(cpr_mai_position & alignment_mask)
1987 {
1988 const uint64_t aligned_position = cpr_mai_position + alignment_mask & ~alignment_mask;
1989 fseek(ctx->imageStream, aligned_position, SEEK_SET);
1990 cpr_mai_position = aligned_position;
1991 }
1992 TRACE("Writing DVD sector CPR/MAI block at position %ld", cpr_mai_position);
1993 BlockHeader cpr_mai_block = {0};
1994 cpr_mai_block.identifier = DataBlock;
1995 cpr_mai_block.type = DvdSectorCprMai;
1996 cpr_mai_block.compression = ctx->compression_enabled ? Lzma : None;
1997 cpr_mai_block.length = (uint32_t)total_sectors * 6;
1998 // Calculate CRC64
1999 cpr_mai_block.crc64 = aaruf_crc64_data(ctx->sector_cpr_mai, cpr_mai_block.length);
2000
2001 buffer = NULL;
2002
2003 if(cpr_mai_block.compression == None)
2004 {
2005 buffer = ctx->sector_cpr_mai;
2006 cpr_mai_block.cmpCrc64 = cpr_mai_block.crc64;
2007 }
2008 else
2009 {
2010 buffer = malloc((size_t)cpr_mai_block.length * 2); // Allocate double size for compression
2011 if(buffer == NULL)
2012 {
2013 TRACE("Failed to allocate memory for DVD sector CPR/MAI compression");
2014 return;
2015 }
2016
2017 size_t dst_size = (size_t)cpr_mai_block.length * 2 * 2;
2018 size_t props_size = LZMA_PROPERTIES_LENGTH;
2019 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_cpr_mai, cpr_mai_block.length, lzma_properties,
2020 &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
2021
2022 cpr_mai_block.cmpLength = (uint32_t)dst_size;
2023
2024 if(cpr_mai_block.cmpLength >= cpr_mai_block.length)
2025 {
2026 cpr_mai_block.compression = None;
2027 free(buffer);
2028 buffer = ctx->sector_cpr_mai;
2029 }
2030 }
2031
2032 if(cpr_mai_block.compression == None)
2033 {
2034 cpr_mai_block.cmpLength = cpr_mai_block.length;
2035 cpr_mai_block.cmpCrc64 = cpr_mai_block.crc64;
2036 }
2037 else
2038 cpr_mai_block.cmpCrc64 = aaruf_crc64_data(buffer, cpr_mai_block.cmpLength);
2039
2040 if(cpr_mai_block.compression == Lzma) cpr_mai_block.cmpLength += LZMA_PROPERTIES_LENGTH;
2041
2042 // Write header
2043 if(fwrite(&cpr_mai_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
2044 {
2045 if(cpr_mai_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
2046
2047 // Write data
2048 const size_t written_bytes = fwrite(buffer, cpr_mai_block.cmpLength, 1, ctx->imageStream);
2049 if(written_bytes == 1)
2050 {
2051 TRACE("Successfully wrote DVD sector CPR/MAI block (%" PRIu64 " bytes)", cpr_mai_block.cmpLength);
2052 // Add CPR/MAI block to index
2053 TRACE("Adding DVD sector CPR/MAI block to index");
2054 IndexEntry cpr_mai_index_entry;
2055 cpr_mai_index_entry.blockType = DataBlock;
2056 cpr_mai_index_entry.dataType = DvdSectorCprMai;
2057 cpr_mai_index_entry.offset = cpr_mai_position;
2058 utarray_push_back(ctx->index_entries, &cpr_mai_index_entry);
2059 TRACE("Added DVD sector CPR/MAI block index entry at offset %" PRIu64, cpr_mai_position);
2060 }
2061 }
2062
2063 if(cpr_mai_block.compression == Lzma) free(buffer);
2064
2065 // Write DVD sector EDC block
2066 fseek(ctx->imageStream, 0, SEEK_END);
2067 long edc_position = ftell(ctx->imageStream);
2068 if(edc_position & alignment_mask)
2069 {
2070 const uint64_t aligned_position = edc_position + alignment_mask & ~alignment_mask;
2071 fseek(ctx->imageStream, aligned_position, SEEK_SET);
2072 edc_position = aligned_position;
2073 }
2074 TRACE("Writing DVD sector EDC block at position %ld", edc_position);
2075 BlockHeader edc_block = {0};
2076 edc_block.identifier = DataBlock;
2077 edc_block.type = DvdSectorEdc;
2078 edc_block.compression = ctx->compression_enabled ? Lzma : None;
2079 edc_block.length = (uint32_t)total_sectors * 4;
2080 // Calculate CRC64
2081 edc_block.crc64 = aaruf_crc64_data(ctx->sector_edc, edc_block.length);
2082
2083 buffer = NULL;
2084
2085 if(edc_block.compression == None)
2086 {
2087 buffer = ctx->sector_edc;
2088 edc_block.cmpCrc64 = edc_block.crc64;
2089 }
2090 else
2091 {
2092 buffer = malloc((size_t)edc_block.length * 2); // Allocate double size for compression
2093 if(buffer == NULL)
2094 {
2095 TRACE("Failed to allocate memory for DVD sector EDC compression");
2096 return;
2097 }
2098
2099 size_t dst_size = (size_t)edc_block.length * 2 * 2;
2100 size_t props_size = LZMA_PROPERTIES_LENGTH;
2101 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_edc, edc_block.length, lzma_properties, &props_size, 9,
2102 ctx->lzma_dict_size, 4, 0, 2, 273, 8);
2103
2104 edc_block.cmpLength = (uint32_t)dst_size;
2105
2106 if(edc_block.cmpLength >= edc_block.length)
2107 {
2108 edc_block.compression = None;
2109 free(buffer);
2110 buffer = ctx->sector_edc;
2111 }
2112 }
2113
2114 if(edc_block.compression == None)
2115 {
2116 edc_block.cmpLength = edc_block.length;
2117 edc_block.cmpCrc64 = edc_block.crc64;
2118 }
2119 else
2120 edc_block.cmpCrc64 = aaruf_crc64_data(buffer, edc_block.cmpLength);
2121
2122 if(edc_block.compression == Lzma) edc_block.cmpLength += LZMA_PROPERTIES_LENGTH;
2123
2124 // Write header
2125 if(fwrite(&edc_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
2126 {
2127 if(edc_block.compression == Lzma) fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
2128
2129 // Write data
2130 const size_t written_bytes = fwrite(buffer, edc_block.cmpLength, 1, ctx->imageStream);
2131 if(written_bytes == 1)
2132 {
2133 TRACE("Successfully wrote DVD sector EDC block (%" PRIu64 " bytes)", edc_block.cmpLength);
2134 // Add EDC block to index
2135 TRACE("Adding DVD sector EDC block to index");
2136 IndexEntry edc_index_entry;
2137 edc_index_entry.blockType = DataBlock;
2138 edc_index_entry.dataType = DvdSectorEdc;
2139 edc_index_entry.offset = edc_position;
2140 utarray_push_back(ctx->index_entries, &edc_index_entry);
2141 TRACE("Added DVD sector EDC block index entry at offset %" PRIu64, edc_position);
2142 }
2143 }
2144
2145 if(edc_block.compression == Lzma) free(buffer);
2146}
2147
2245 static void write_dvd_title_key_decrypted_block(const aaruformat_context *ctx)
2246{
2247 if(ctx->sector_decrypted_title_key == NULL) return;
2248
2249 fseek(ctx->imageStream, 0, SEEK_END);
2250 long block_position = ftell(ctx->imageStream);
2251 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
2252 if(block_position & alignment_mask)
2253 {
2254 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
2255 fseek(ctx->imageStream, aligned_position, SEEK_SET);
2256 block_position = aligned_position;
2257 }
2258 TRACE("Writing DVD decrypted title key block at position %ld", block_position);
2259 BlockHeader decrypted_title_key_block = {0};
2260 decrypted_title_key_block.identifier = DataBlock;
2261 decrypted_title_key_block.type = DvdSectorTitleKeyDecrypted;
2262 decrypted_title_key_block.compression = ctx->compression_enabled ? Lzma : None;
2263 decrypted_title_key_block.length =
2264 (uint32_t)(ctx->user_data_ddt_header.negative + ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow) *
2265 5;
2266 // Calculate CRC64
2267 decrypted_title_key_block.crc64 =
2268 aaruf_crc64_data(ctx->sector_decrypted_title_key, decrypted_title_key_block.length);
2269
2270 uint8_t *buffer = NULL;
2271 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
2272
2273 if(decrypted_title_key_block.compression == None)
2274 {
2275 buffer = ctx->sector_decrypted_title_key;
2276 decrypted_title_key_block.cmpCrc64 = decrypted_title_key_block.crc64;
2277 }
2278 else
2279 {
2280 buffer = malloc((size_t)decrypted_title_key_block.length * 2); // Allocate double size for compression
2281 if(buffer == NULL)
2282 {
2283 TRACE("Failed to allocate memory for DVD decrypted title key compression");
2284 return;
2285 }
2286
2287 size_t dst_size = (size_t)decrypted_title_key_block.length * 2 * 2;
2288 size_t props_size = LZMA_PROPERTIES_LENGTH;
2289 aaruf_lzma_encode_buffer(buffer, &dst_size, ctx->sector_decrypted_title_key, decrypted_title_key_block.length,
2290 lzma_properties, &props_size, 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
2291
2292 decrypted_title_key_block.cmpLength = (uint32_t)dst_size;
2293
2294 if(decrypted_title_key_block.cmpLength >= decrypted_title_key_block.length)
2295 {
2296 decrypted_title_key_block.compression = None;
2297 free(buffer);
2298 buffer = ctx->sector_decrypted_title_key;
2299 }
2300 }
2301
2302 if(decrypted_title_key_block.compression == None)
2303 {
2304 decrypted_title_key_block.cmpLength = decrypted_title_key_block.length;
2305 decrypted_title_key_block.cmpCrc64 = decrypted_title_key_block.crc64;
2306 }
2307 else
2308 decrypted_title_key_block.cmpCrc64 = aaruf_crc64_data(buffer, decrypted_title_key_block.cmpLength);
2309
2310 if(decrypted_title_key_block.compression == Lzma) decrypted_title_key_block.cmpLength += LZMA_PROPERTIES_LENGTH;
2311
2312 // Write header
2313 if(fwrite(&decrypted_title_key_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
2314 {
2315 if(decrypted_title_key_block.compression == Lzma)
2316 fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream);
2317
2318 // Write data
2319 const size_t written_bytes = fwrite(buffer, decrypted_title_key_block.cmpLength, 1, ctx->imageStream);
2320 if(written_bytes == 1)
2321 {
2322 TRACE("Successfully wrote DVD decrypted title key block (%" PRIu64 " bytes)",
2323 decrypted_title_key_block.cmpLength);
2324 // Add decrypted title key block to index
2325 TRACE("Adding DVD decrypted title key block to index");
2326 IndexEntry decrypted_title_key_index_entry;
2327 decrypted_title_key_index_entry.blockType = DataBlock;
2328 decrypted_title_key_index_entry.dataType = DvdSectorTitleKeyDecrypted;
2329 decrypted_title_key_index_entry.offset = block_position;
2330 utarray_push_back(ctx->index_entries, &decrypted_title_key_index_entry);
2331 TRACE("Added DVD decrypted title key block index entry at offset %" PRIu64, block_position);
2332 }
2333 }
2334
2335 if(decrypted_title_key_block.compression == Lzma) free(buffer);
2336}
2337
2409 static void write_media_tags(const aaruformat_context *ctx)
2410{
2411 if(ctx->mediaTags == NULL) return;
2412
2413 mediaTagEntry *media_tag = NULL;
2414 mediaTagEntry *tmp_media_tag = NULL;
2415
2416 HASH_ITER(hh, ctx->mediaTags, media_tag, tmp_media_tag)
2417 {
2418 fseek(ctx->imageStream, 0, SEEK_END);
2419 long tag_position = ftell(ctx->imageStream);
2420 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
2421 if(tag_position & alignment_mask)
2422 {
2423 const uint64_t aligned_position = tag_position + alignment_mask & ~alignment_mask;
2424 fseek(ctx->imageStream, aligned_position, SEEK_SET);
2425 tag_position = aligned_position;
2426 }
2427
2428 TRACE("Writing media tag block type %d at position %ld", aaruf_get_datatype_for_media_tag_type(media_tag->type),
2429 tag_position);
2430 BlockHeader tag_block = {0};
2431 tag_block.identifier = DataBlock;
2432 tag_block.type = (uint16_t)aaruf_get_datatype_for_media_tag_type(media_tag->type);
2433 tag_block.compression = ctx->compression_enabled ? Lzma : None;
2434 tag_block.length = media_tag->length;
2435
2436 // Calculate CRC64
2437 tag_block.crc64 = aaruf_crc64_data(media_tag->data, tag_block.length);
2438
2439 uint8_t *buffer = NULL;
2440 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH] = {0};
2441
2442 if(tag_block.compression == None)
2443 {
2444 buffer = media_tag->data;
2445 tag_block.cmpCrc64 = tag_block.crc64;
2446 }
2447 else
2448 {
2449 buffer = malloc((size_t)tag_block.length * 2); // Allocate double size for compression
2450 if(buffer == NULL)
2451 {
2452 TRACE("Failed to allocate memory for media tag compression");
2453 return;
2454 }
2455
2456 size_t dst_size = (size_t)tag_block.length * 2 * 2;
2457 size_t props_size = LZMA_PROPERTIES_LENGTH;
2458 aaruf_lzma_encode_buffer(buffer, &dst_size, media_tag->data, tag_block.length, lzma_properties, &props_size,
2459 9, ctx->lzma_dict_size, 4, 0, 2, 273, 8);
2460
2461 tag_block.cmpLength = (uint32_t)dst_size;
2462
2463 if(tag_block.cmpLength >= tag_block.length)
2464 {
2465 tag_block.compression = None;
2466 free(buffer);
2467 buffer = media_tag->data;
2468 }
2469 }
2470
2471 if(tag_block.compression == None)
2472 {
2473 tag_block.cmpLength = tag_block.length;
2474 tag_block.cmpCrc64 = tag_block.crc64;
2475 }
2476 else
2477 tag_block.cmpCrc64 = aaruf_crc64_data(buffer, tag_block.cmpLength);
2478
2479 if(tag_block.compression == Lzma) tag_block.cmpLength += LZMA_PROPERTIES_LENGTH;
2480
2481 // Write header
2482 if(fwrite(&tag_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
2483 {
2484 if(tag_block.compression == Lzma)
2485 fwrite(lzma_properties, LZMA_PROPERTIES_LENGTH, 1, ctx->imageStream); // Write data
2486
2487 const size_t written_bytes = fwrite(buffer, tag_block.cmpLength, 1, ctx->imageStream);
2488 if(written_bytes == 1)
2489 {
2490 TRACE("Successfully wrote media tag block type %d (%" PRIu64 " bytes)", tag_block.type,
2491 tag_block.cmpLength);
2492 // Add media tag block to index
2493 TRACE("Adding media tag type %d block to index", tag_block.type);
2494 IndexEntry tag_index_entry;
2495 tag_index_entry.blockType = DataBlock;
2496 tag_index_entry.dataType = tag_block.type;
2497 tag_index_entry.offset = tag_position;
2498 utarray_push_back(ctx->index_entries, &tag_index_entry);
2499 TRACE("Added media tag block type %d index entry at offset %" PRIu64, tag_block.type, tag_position);
2500 }
2501 }
2502
2503 if(tag_block.compression == Lzma) free(buffer);
2504 }
2505}
2506
2669 static void write_tape_file_block(const aaruformat_context *ctx)
2670{
2671 if(ctx->tape_files == NULL) return;
2672
2673 // Iterate the uthash and count how many entries do we have
2674 const tapeFileHashEntry *tape_file = NULL;
2675 const tapeFileHashEntry *tmp_tape_file = NULL;
2676 size_t tape_file_count = 0;
2677 HASH_ITER(hh, ctx->tape_files, tape_file, tmp_tape_file) tape_file_count++;
2678
2679 // Create a memory buffer to copy all the file entries
2680 const size_t buffer_size = tape_file_count * sizeof(TapeFileEntry);
2681 TapeFileEntry *buffer = malloc(buffer_size);
2682 if(buffer == NULL)
2683 {
2684 TRACE("Failed to allocate memory for tape file entries");
2685 return;
2686 }
2687 memset(buffer, 0, buffer_size);
2688 size_t index = 0;
2689 HASH_ITER(hh, ctx->tape_files, tape_file, tmp_tape_file)
2690 {
2691 if(index >= tape_file_count) break;
2692 memcpy(&buffer[index], &tape_file->fileEntry, sizeof(TapeFileEntry));
2693 index++;
2694 }
2695
2696 // Create the tape file block in memory
2697 TapeFileHeader tape_file_block = {0};
2698 tape_file_block.identifier = TapeFileBlock;
2699 tape_file_block.length = (uint32_t)buffer_size;
2700 tape_file_block.crc64 = aaruf_crc64_data((uint8_t *)buffer, (uint32_t)tape_file_block.length);
2701
2702 // Write tape file block to file, block aligned
2703 fseek(ctx->imageStream, 0, SEEK_END);
2704 long block_position = ftell(ctx->imageStream);
2705 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
2706 if(block_position & alignment_mask)
2707 {
2708 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
2709 fseek(ctx->imageStream, aligned_position, SEEK_SET);
2710 block_position = aligned_position;
2711 }
2712 TRACE("Writing tape file block at position %ld", block_position);
2713 if(fwrite(&tape_file_block, sizeof(TapeFileHeader), 1, ctx->imageStream) == 1)
2714 {
2715 const size_t written_bytes = fwrite(buffer, tape_file_block.length, 1, ctx->imageStream);
2716 if(written_bytes == 1)
2717 {
2718 TRACE("Successfully wrote tape file block (%" PRIu64 " bytes)", tape_file_block.length);
2719 // Add tape file block to index
2720 TRACE("Adding tape file block to index");
2721 IndexEntry index_entry;
2722 index_entry.blockType = TapeFileBlock;
2723 index_entry.dataType = 0;
2724 index_entry.offset = block_position;
2725 utarray_push_back(ctx->index_entries, &index_entry);
2726 TRACE("Added tape file block index entry at offset %" PRIu64, block_position);
2727 }
2728 }
2729
2730 free(buffer);
2731}
2732
2901 static void write_tape_partition_block(const aaruformat_context *ctx)
2902{
2903 if(ctx->tape_partitions == NULL) return;
2904
2905 // Iterate the uthash and count how many entries do we have
2906 const TapePartitionHashEntry *tape_partition = NULL;
2907 const TapePartitionHashEntry *tmp_tape_partition = NULL;
2908 size_t tape_partition_count = 0;
2909 HASH_ITER(hh, ctx->tape_partitions, tape_partition, tmp_tape_partition) tape_partition_count++;
2910
2911 // Create a memory buffer to copy all the partition entries
2912 const size_t buffer_size = tape_partition_count * sizeof(TapePartitionEntry);
2913 TapePartitionEntry *buffer = malloc(buffer_size);
2914 if(buffer == NULL)
2915 {
2916 TRACE("Failed to allocate memory for tape partition entries");
2917 return;
2918 }
2919 memset(buffer, 0, buffer_size);
2920 size_t index = 0;
2921 HASH_ITER(hh, ctx->tape_partitions, tape_partition, tmp_tape_partition)
2922 {
2923 if(index >= tape_partition_count) break;
2924 memcpy(&buffer[index], &tape_partition->partitionEntry, sizeof(TapePartitionEntry));
2925 index++;
2926 }
2927
2928 // Create the tape partition block in memory
2929 TapePartitionHeader tape_partition_block = {0};
2930 tape_partition_block.identifier = TapePartitionBlock;
2931 tape_partition_block.length = (uint32_t)buffer_size;
2932 tape_partition_block.crc64 = aaruf_crc64_data((uint8_t *)buffer, (uint32_t)tape_partition_block.length);
2933
2934 // Write tape partition block to partition, block aligned
2935 fseek(ctx->imageStream, 0, SEEK_END);
2936 long block_position = ftell(ctx->imageStream);
2937 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
2938 if(block_position & alignment_mask)
2939 {
2940 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
2941 fseek(ctx->imageStream, aligned_position, SEEK_SET);
2942 block_position = aligned_position;
2943 }
2944 TRACE("Writing tape partition block at position %ld", block_position);
2945 if(fwrite(&tape_partition_block, sizeof(TapePartitionHeader), 1, ctx->imageStream) == 1)
2946 {
2947 const size_t written_bytes = fwrite(buffer, tape_partition_block.length, 1, ctx->imageStream);
2948 if(written_bytes == 1)
2949 {
2950 TRACE("Successfully wrote tape partition block (%" PRIu64 " bytes)", tape_partition_block.length);
2951 // Add tape partition block to index
2952 TRACE("Adding tape partition block to index");
2953 IndexEntry index_entry;
2954 index_entry.blockType = TapePartitionBlock;
2955 index_entry.dataType = 0;
2956 index_entry.offset = block_position;
2957 utarray_push_back(ctx->index_entries, &index_entry);
2958 TRACE("Added tape partition block index entry at offset %" PRIu64, block_position);
2959 }
2960 }
2961
2962 free(buffer);
2963}
2964
3026 static void write_geometry_block(const aaruformat_context *ctx)
3027{
3028 if(ctx->geometry_block.identifier != GeometryBlock) return;
3029
3030 fseek(ctx->imageStream, 0, SEEK_END);
3031 long block_position = ftell(ctx->imageStream);
3032 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
3033 if(block_position & alignment_mask)
3034 {
3035 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
3036 fseek(ctx->imageStream, aligned_position, SEEK_SET);
3037 block_position = aligned_position;
3038 }
3039
3040 TRACE("Writing geometry block at position %ld", block_position);
3041
3042 // Write header
3043 if(fwrite(&ctx->geometry_block, sizeof(GeometryBlockHeader), 1, ctx->imageStream) == 1)
3044 {
3045 TRACE("Successfully wrote geometry block");
3046
3047 // Add geometry block to index
3048 TRACE("Adding geometry block to index");
3049 IndexEntry index_entry;
3050 index_entry.blockType = GeometryBlock;
3051 index_entry.dataType = 0;
3052 index_entry.offset = block_position;
3053 utarray_push_back(ctx->index_entries, &index_entry);
3054 TRACE("Added geometry block index entry at offset %" PRIu64, block_position);
3055 }
3056}
3057
3162 static void write_metadata_block(aaruformat_context *ctx)
3163{
3164 if(ctx->metadata_block_header.identifier != MetadataBlock && ctx->metadata_block_header.mediaSequence == 0 &&
3165 ctx->metadata_block_header.lastMediaSequence == 0 && ctx->creator == NULL && ctx->comments == NULL &&
3166 ctx->media_title == NULL && ctx->media_manufacturer == NULL && ctx->media_model == NULL &&
3167 ctx->media_serial_number == NULL && ctx->media_barcode == NULL && ctx->media_part_number == NULL &&
3168 ctx->drive_manufacturer == NULL && ctx->drive_model == NULL && ctx->drive_serial_number == NULL &&
3169 ctx->drive_firmware_revision == NULL)
3170 return;
3171
3172 ctx->metadata_block_header.blockSize =
3173 sizeof(MetadataBlockHeader) + ctx->metadata_block_header.creatorLength +
3174 ctx->metadata_block_header.commentsLength + ctx->metadata_block_header.mediaTitleLength +
3175 ctx->metadata_block_header.mediaManufacturerLength + ctx->metadata_block_header.mediaModelLength +
3176 ctx->metadata_block_header.mediaSerialNumberLength + ctx->metadata_block_header.mediaBarcodeLength +
3177 ctx->metadata_block_header.mediaPartNumberLength + ctx->metadata_block_header.driveManufacturerLength +
3178 ctx->metadata_block_header.driveModelLength + ctx->metadata_block_header.driveSerialNumberLength +
3179 ctx->metadata_block_header.driveFirmwareRevisionLength;
3180
3181 ctx->metadata_block_header.identifier = MetadataBlock;
3182
3183 int pos = sizeof(MetadataBlockHeader);
3184
3185 uint8_t *buffer = calloc(1, ctx->metadata_block_header.blockSize);
3186 if(buffer == NULL) return;
3187
3188 if(ctx->creator != NULL && ctx->metadata_block_header.creatorLength > 0)
3189 {
3190 memcpy(buffer + pos, ctx->creator, ctx->metadata_block_header.creatorLength);
3191 ctx->metadata_block_header.creatorOffset = pos;
3192 pos += ctx->metadata_block_header.creatorLength;
3193 }
3194
3195 if(ctx->comments != NULL && ctx->metadata_block_header.commentsLength > 0)
3196 {
3197 memcpy(buffer + pos, ctx->comments, ctx->metadata_block_header.commentsLength);
3198 ctx->metadata_block_header.commentsOffset = pos;
3199 pos += ctx->metadata_block_header.commentsLength;
3200 }
3201
3202 if(ctx->media_title != NULL && ctx->metadata_block_header.mediaTitleLength > 0)
3203 {
3204 memcpy(buffer + pos, ctx->media_title, ctx->metadata_block_header.mediaTitleLength);
3205 ctx->metadata_block_header.mediaTitleOffset = pos;
3206 pos += ctx->metadata_block_header.mediaTitleLength;
3207 }
3208
3209 if(ctx->media_manufacturer != NULL && ctx->metadata_block_header.mediaManufacturerLength > 0)
3210 {
3211 memcpy(buffer + pos, ctx->media_manufacturer, ctx->metadata_block_header.mediaManufacturerLength);
3212 ctx->metadata_block_header.mediaManufacturerOffset = pos;
3213 pos += ctx->metadata_block_header.mediaManufacturerLength;
3214 }
3215
3216 if(ctx->media_model != NULL && ctx->metadata_block_header.mediaModelLength > 0)
3217 {
3218 memcpy(buffer + pos, ctx->media_model, ctx->metadata_block_header.mediaModelLength);
3219 ctx->metadata_block_header.mediaModelOffset = pos;
3220 pos += ctx->metadata_block_header.mediaModelLength;
3221 }
3222
3223 if(ctx->media_serial_number != NULL && ctx->metadata_block_header.mediaSerialNumberLength > 0)
3224 {
3225 memcpy(buffer + pos, ctx->media_serial_number, ctx->metadata_block_header.mediaSerialNumberLength);
3226 ctx->metadata_block_header.mediaSerialNumberOffset = pos;
3227 pos += ctx->metadata_block_header.mediaSerialNumberLength;
3228 }
3229
3230 if(ctx->media_barcode != NULL && ctx->metadata_block_header.mediaBarcodeLength > 0)
3231 {
3232 memcpy(buffer + pos, ctx->media_barcode, ctx->metadata_block_header.mediaBarcodeLength);
3233 ctx->metadata_block_header.mediaBarcodeOffset = pos;
3234 pos += ctx->metadata_block_header.mediaBarcodeLength;
3235 }
3236
3237 if(ctx->media_part_number != NULL && ctx->metadata_block_header.mediaPartNumberLength > 0)
3238 {
3239 memcpy(buffer + pos, ctx->media_part_number, ctx->metadata_block_header.mediaPartNumberLength);
3240 ctx->metadata_block_header.mediaPartNumberOffset = pos;
3241 pos += ctx->metadata_block_header.mediaPartNumberLength;
3242 }
3243
3244 if(ctx->drive_manufacturer != NULL && ctx->metadata_block_header.driveManufacturerLength > 0)
3245 {
3246 memcpy(buffer + pos, ctx->drive_manufacturer, ctx->metadata_block_header.driveManufacturerLength);
3247 ctx->metadata_block_header.driveManufacturerOffset = pos;
3248 pos += ctx->metadata_block_header.driveManufacturerLength;
3249 }
3250
3251 if(ctx->drive_model != NULL && ctx->metadata_block_header.driveModelLength > 0)
3252 {
3253 memcpy(buffer + pos, ctx->drive_model, ctx->metadata_block_header.driveModelLength);
3254 ctx->metadata_block_header.driveModelOffset = pos;
3255 pos += ctx->metadata_block_header.driveModelLength;
3256 }
3257
3258 if(ctx->drive_serial_number != NULL && ctx->metadata_block_header.driveSerialNumberLength > 0)
3259 {
3260 memcpy(buffer + pos, ctx->drive_serial_number, ctx->metadata_block_header.driveSerialNumberLength);
3261 ctx->metadata_block_header.driveSerialNumberOffset = pos;
3262 pos += ctx->metadata_block_header.driveSerialNumberLength;
3263 }
3264
3265 if(ctx->drive_firmware_revision != NULL && ctx->metadata_block_header.driveFirmwareRevisionLength > 0)
3266 {
3267 memcpy(buffer + pos, ctx->drive_firmware_revision, ctx->metadata_block_header.driveFirmwareRevisionLength);
3268 ctx->metadata_block_header.driveFirmwareRevisionOffset = pos;
3269 }
3270
3271 fseek(ctx->imageStream, 0, SEEK_END);
3272 long block_position = ftell(ctx->imageStream);
3273 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
3274 if(block_position & alignment_mask)
3275 {
3276 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
3277 fseek(ctx->imageStream, aligned_position, SEEK_SET);
3278 block_position = aligned_position;
3279 }
3280
3281 TRACE("Writing metadata block at position %ld", block_position);
3282
3283 if(fwrite(buffer, ctx->metadata_block_header.blockSize, 1, ctx->imageStream) == 1)
3284 {
3285 TRACE("Successfully wrote metadata block");
3286
3287 // Add metadata block to index
3288 TRACE("Adding metadata block to index");
3289 IndexEntry index_entry;
3290 index_entry.blockType = MetadataBlock;
3291 index_entry.dataType = 0;
3292 index_entry.offset = block_position;
3293 utarray_push_back(ctx->index_entries, &index_entry);
3294 TRACE("Added metadata block index entry at offset %" PRIu64, block_position);
3295 }
3296
3297 free(buffer);
3298}
3299
3445 static void write_dumphw_block(aaruformat_context *ctx)
3446{
3447
3448 if(ctx->dump_hardware_entries_with_data == NULL || ctx->dump_hardware_header.entries == 0 ||
3449 ctx->dump_hardware_header.identifier != DumpHardwareBlock)
3450 return;
3451
3452 const size_t required_length = sizeof(DumpHardwareHeader) + ctx->dump_hardware_header.length;
3453
3454 uint8_t *buffer = calloc(1, required_length);
3455
3456 if(buffer == NULL) return;
3457
3458 // Start to iterate and copy the data
3459 size_t offset = 0;
3460 for(int i = 0; i < ctx->dump_hardware_header.entries; i++)
3461 {
3462 size_t entry_size = sizeof(DumpHardwareEntry) +
3463 ctx->dump_hardware_entries_with_data[i].entry.manufacturerLength +
3464 ctx->dump_hardware_entries_with_data[i].entry.modelLength +
3465 ctx->dump_hardware_entries_with_data[i].entry.revisionLength +
3466 ctx->dump_hardware_entries_with_data[i].entry.firmwareLength +
3467 ctx->dump_hardware_entries_with_data[i].entry.serialLength +
3468 ctx->dump_hardware_entries_with_data[i].entry.softwareNameLength +
3469 ctx->dump_hardware_entries_with_data[i].entry.softwareVersionLength +
3470 ctx->dump_hardware_entries_with_data[i].entry.softwareOperatingSystemLength +
3471 ctx->dump_hardware_entries_with_data[i].entry.extents * sizeof(DumpExtent);
3472
3473 if(offset + entry_size > required_length)
3474 {
3475 FATAL("Calculated size exceeds provided buffer length");
3476 free(buffer);
3477 return;
3478 }
3479
3480 memcpy(buffer + offset, &ctx->dump_hardware_entries_with_data[i].entry, sizeof(DumpHardwareEntry));
3481 offset += sizeof(DumpHardwareEntry);
3482 if(ctx->dump_hardware_entries_with_data[i].entry.manufacturerLength > 0 &&
3483 ctx->dump_hardware_entries_with_data[i].manufacturer != NULL)
3484 {
3485 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].manufacturer,
3486 ctx->dump_hardware_entries_with_data[i].entry.manufacturerLength);
3487 offset += ctx->dump_hardware_entries_with_data[i].entry.manufacturerLength;
3488 }
3489 if(ctx->dump_hardware_entries_with_data[i].entry.modelLength > 0 &&
3490 ctx->dump_hardware_entries_with_data[i].model != NULL)
3491 {
3492 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].model,
3493 ctx->dump_hardware_entries_with_data[i].entry.modelLength);
3494 offset += ctx->dump_hardware_entries_with_data[i].entry.modelLength;
3495 }
3496 if(ctx->dump_hardware_entries_with_data[i].entry.revisionLength > 0 &&
3497 ctx->dump_hardware_entries_with_data[i].revision != NULL)
3498 {
3499 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].revision,
3500 ctx->dump_hardware_entries_with_data[i].entry.revisionLength);
3501 offset += ctx->dump_hardware_entries_with_data[i].entry.revisionLength;
3502 }
3503 if(ctx->dump_hardware_entries_with_data[i].entry.firmwareLength > 0 &&
3504 ctx->dump_hardware_entries_with_data[i].firmware != NULL)
3505 {
3506 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].firmware,
3507 ctx->dump_hardware_entries_with_data[i].entry.firmwareLength);
3508 offset += ctx->dump_hardware_entries_with_data[i].entry.firmwareLength;
3509 }
3510 if(ctx->dump_hardware_entries_with_data[i].entry.serialLength > 0 &&
3511 ctx->dump_hardware_entries_with_data[i].serial != NULL)
3512 {
3513 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].serial,
3514 ctx->dump_hardware_entries_with_data[i].entry.serialLength);
3515 offset += ctx->dump_hardware_entries_with_data[i].entry.serialLength;
3516 }
3517 if(ctx->dump_hardware_entries_with_data[i].entry.softwareNameLength > 0 &&
3518 ctx->dump_hardware_entries_with_data[i].softwareName != NULL)
3519 {
3520 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].softwareName,
3521 ctx->dump_hardware_entries_with_data[i].entry.softwareNameLength);
3522 offset += ctx->dump_hardware_entries_with_data[i].entry.softwareNameLength;
3523 }
3524 if(ctx->dump_hardware_entries_with_data[i].entry.softwareVersionLength > 0 &&
3525 ctx->dump_hardware_entries_with_data[i].softwareVersion != NULL)
3526 {
3527 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].softwareVersion,
3528 ctx->dump_hardware_entries_with_data[i].entry.softwareVersionLength);
3529 offset += ctx->dump_hardware_entries_with_data[i].entry.softwareVersionLength;
3530 }
3531 if(ctx->dump_hardware_entries_with_data[i].entry.softwareOperatingSystemLength > 0 &&
3532 ctx->dump_hardware_entries_with_data[i].softwareOperatingSystem != NULL)
3533 {
3534 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].softwareOperatingSystem,
3535 ctx->dump_hardware_entries_with_data[i].entry.softwareOperatingSystemLength);
3536 offset += ctx->dump_hardware_entries_with_data[i].entry.softwareOperatingSystemLength;
3537 }
3538 if(ctx->dump_hardware_entries_with_data[i].entry.extents > 0 &&
3539 ctx->dump_hardware_entries_with_data[i].extents != NULL)
3540 {
3541 memcpy(buffer + offset, ctx->dump_hardware_entries_with_data[i].extents,
3542 ctx->dump_hardware_entries_with_data[i].entry.extents * sizeof(DumpExtent));
3543 offset += ctx->dump_hardware_entries_with_data[i].entry.extents * sizeof(DumpExtent);
3544 }
3545 }
3546
3547 // Calculate CRC64
3548 ctx->dump_hardware_header.crc64 =
3549 aaruf_crc64_data(buffer + sizeof(DumpHardwareHeader), ctx->dump_hardware_header.length);
3550
3551 // Copy header
3552 memcpy(buffer, &ctx->dump_hardware_header, sizeof(DumpHardwareHeader));
3553
3554 fseek(ctx->imageStream, 0, SEEK_END);
3555 long block_position = ftell(ctx->imageStream);
3556 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
3557 if(block_position & alignment_mask)
3558 {
3559 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
3560 fseek(ctx->imageStream, aligned_position, SEEK_SET);
3561 block_position = aligned_position;
3562 }
3563 TRACE("Writing dump hardware block at position %ld", block_position);
3564 if(fwrite(buffer, required_length, 1, ctx->imageStream) == 1)
3565 {
3566 TRACE("Successfully wrote dump hardware block");
3567
3568 // Add dump hardware block to index
3569 TRACE("Adding dump hardware block to index");
3570 IndexEntry index_entry;
3571 index_entry.blockType = DumpHardwareBlock;
3572 index_entry.dataType = 0;
3573 index_entry.offset = block_position;
3574 utarray_push_back(ctx->index_entries, &index_entry);
3575 TRACE("Added dump hardware block index entry at offset %" PRIu64, block_position);
3576 }
3577
3578 free(buffer);
3579}
3580
3675 static void write_cicm_block(const aaruformat_context *ctx)
3676{
3677 if(ctx->cicm_block == NULL || ctx->cicm_block_header.length == 0 || ctx->cicm_block_header.identifier != CicmBlock)
3678 return;
3679
3680 fseek(ctx->imageStream, 0, SEEK_END);
3681 long block_position = ftell(ctx->imageStream);
3682 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
3683
3684 if(block_position & alignment_mask)
3685 {
3686 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
3687 fseek(ctx->imageStream, aligned_position, SEEK_SET);
3688 block_position = aligned_position;
3689 }
3690
3691 TRACE("Writing CICM XML block at position %ld", block_position);
3692 if(fwrite(&ctx->cicm_block_header, sizeof(CicmMetadataBlock), 1, ctx->imageStream) == 1)
3693 if(fwrite(ctx->cicm_block, ctx->cicm_block_header.length, 1, ctx->imageStream) == 1)
3694 {
3695 TRACE("Successfully wrote CICM XML block");
3696
3697 // Add CICM block to index
3698 TRACE("Adding CICM XML block to index");
3699 IndexEntry index_entry;
3700 index_entry.blockType = CicmBlock;
3701 index_entry.dataType = 0;
3702 index_entry.offset = block_position;
3703 utarray_push_back(ctx->index_entries, &index_entry);
3704 TRACE("Added CICM XML block index entry at offset %" PRIu64, block_position);
3705 }
3706}
3707
3812 static void write_aaru_json_block(const aaruformat_context *ctx)
3813{
3814 if(ctx->json_block == NULL || ctx->json_block_header.length == 0 ||
3815 ctx->json_block_header.identifier != AaruMetadataJsonBlock)
3816 return;
3817
3818 fseek(ctx->imageStream, 0, SEEK_END);
3819 long block_position = ftell(ctx->imageStream);
3820 const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
3821
3822 if(block_position & alignment_mask)
3823 {
3824 const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask;
3825 fseek(ctx->imageStream, aligned_position, SEEK_SET);
3826 block_position = aligned_position;
3827 }
3828
3829 TRACE("Writing Aaru metadata JSON block at position %ld", block_position);
3830 if(fwrite(&ctx->json_block_header, sizeof(AaruMetadataJsonBlockHeader), 1, ctx->imageStream) == 1)
3831 if(fwrite(ctx->json_block, ctx->json_block_header.length, 1, ctx->imageStream) == 1)
3832 {
3833 TRACE("Successfully wrote Aaru metadata JSON block");
3834
3835 // Add Aaru metadata JSON block to index
3836 TRACE("Adding Aaru metadata JSON block to index");
3837 IndexEntry index_entry;
3838 index_entry.blockType = AaruMetadataJsonBlock;
3839 index_entry.dataType = 0;
3840 index_entry.offset = block_position;
3841 utarray_push_back(ctx->index_entries, &index_entry);
3842 TRACE("Added Aaru metadata JSON block index entry at offset %" PRIu64, block_position);
3843 }
3844}
3845
3862 static int32_t write_index_block(aaruformat_context *ctx)
3863{
3864 // Write the complete index at the end of the file
3865 TRACE("Writing index at the end of the file");
3866 fseek(ctx->imageStream, 0, SEEK_END);
3867 long index_position = ftell(ctx->imageStream);
3868
3869 // Align index position to block boundary if needed
3870 uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1;
3871 if(index_position & alignment_mask)
3872 {
3873 uint64_t aligned_position = index_position + alignment_mask & ~alignment_mask;
3874 fseek(ctx->imageStream, aligned_position, SEEK_SET);
3875 index_position = aligned_position;
3876 TRACE("Aligned index position to %" PRIu64, aligned_position);
3877 }
3878
3879 // Prepare index header
3880 IndexHeader3 index_header;
3881 index_header.identifier = IndexBlock3;
3882 index_header.entries = utarray_len(ctx->index_entries);
3883 index_header.previous = 0; // No previous index for now
3884
3885 TRACE("Writing index with %" PRIu64 " entries at position %ld", index_header.entries, index_position);
3886
3887 // Calculate CRC64 of index entries
3888 crc64_ctx *index_crc64_context = aaruf_crc64_init();
3889 if(index_crc64_context != NULL && index_header.entries > 0)
3890 {
3891 size_t index_data_size = index_header.entries * sizeof(IndexEntry);
3892 aaruf_crc64_update(index_crc64_context, utarray_front(ctx->index_entries), index_data_size);
3893 aaruf_crc64_final(index_crc64_context, &index_header.crc64);
3894 TRACE("Calculated index CRC64: 0x%16lX", index_header.crc64);
3895 }
3896 else
3897 index_header.crc64 = 0;
3898
3899 // Write index header
3900 if(fwrite(&index_header, sizeof(IndexHeader3), 1, ctx->imageStream) == 1)
3901 {
3902 TRACE("Successfully wrote index header");
3903
3904 // Write index entries
3905 if(index_header.entries > 0)
3906 {
3907 size_t entries_written = 0;
3908 IndexEntry *entry = NULL;
3909
3910 for(entry = (IndexEntry *)utarray_front(ctx->index_entries); entry != NULL;
3911 entry = (IndexEntry *)utarray_next(ctx->index_entries, entry))
3912 if(fwrite(entry, sizeof(IndexEntry), 1, ctx->imageStream) == 1)
3913 {
3914 entries_written++;
3915 TRACE("Wrote index entry: blockType=0x%08X dataType=%u offset=%" PRIu64, entry->blockType,
3916 entry->dataType, entry->offset);
3917 }
3918 else
3919 {
3920 TRACE("Failed to write index entry %zu", entries_written);
3921 break;
3922 }
3923
3924 if(entries_written == index_header.entries)
3925 {
3926 TRACE("Successfully wrote all %zu index entries", entries_written);
3927
3928 // Update header with index offset and rewrite it
3929 ctx->header.indexOffset = index_position;
3930 TRACE("Updating header with index offset: %" PRIu64, ctx->header.indexOffset);
3931
3932 // Seek back to beginning and rewrite header
3933 fseek(ctx->imageStream, 0, SEEK_SET);
3934 if(fwrite(&ctx->header, sizeof(AaruHeaderV2), 1, ctx->imageStream) == 1)
3935 TRACE("Successfully updated header with index offset");
3936 else
3937 {
3938 TRACE("Failed to update header with index offset");
3939 return AARUF_ERROR_CANNOT_WRITE_HEADER;
3940 }
3941 }
3942 else
3943 {
3944 TRACE("Failed to write all index entries (wrote %zu of %" PRIu64 ")", entries_written,
3945 index_header.entries);
3946 return AARUF_ERROR_CANNOT_WRITE_HEADER;
3947 }
3948 }
3949 }
3950 else
3951 {
3952 TRACE("Failed to write index header");
3953 return AARUF_ERROR_CANNOT_WRITE_HEADER;
3954 }
3955
3956 return AARUF_STATUS_OK;
3957}
3958
3995 AARU_EXPORT int AARU_CALL aaruf_close(void *context)
3996{
3997 TRACE("Entering aaruf_close(%p)", context);
3998
3999 mediaTagEntry *media_tag = NULL;
4000 mediaTagEntry *tmp_media_tag = NULL;
4001
4002 if(context == NULL)
4003 {
4004 FATAL("Invalid context");
4005 errno = EINVAL;
4006 return -1;
4007 }
4008
4009 aaruformat_context *ctx = context;
4010
4011 // Not a libaaruformat context
4012 if(ctx->magic != AARU_MAGIC)
4013 {
4014 FATAL("Invalid context");
4015 errno = EINVAL;
4016 return -1;
4017 }
4018
4019 if(ctx->is_writing)
4020 {
4021 TRACE("File is writing");
4022
4023 TRACE("Seeking to start of image");
4024 // Write the header at the beginning of the file
4025 fseek(ctx->imageStream, 0, SEEK_SET);
4026
4027 TRACE("Writing header at position 0");
4028 if(fwrite(&ctx->header, sizeof(AaruHeaderV2), 1, ctx->imageStream) != 1)
4029 {
4030 fclose(ctx->imageStream);
4031 ctx->imageStream = NULL;
4032 errno = AARUF_ERROR_CANNOT_WRITE_HEADER;
4033 return -1;
4034 }
4035
4036 // Close current block first
4037 TRACE("Closing current block if any");
4038 if(ctx->writing_buffer != NULL)
4039 {
4040 int error = aaruf_close_current_block(ctx);
4041
4042 if(error != AARUF_STATUS_OK) return error;
4043 }
4044
4045 int32_t res;
4046 if(ctx->is_tape)
4047 {
4048 // Write tape DDT
4049 res = write_tape_ddt(ctx);
4050 if(res != AARUF_STATUS_OK) return res;
4051 }
4052 else
4053 {
4054 // Write cached secondary DDT table if any
4055 res = write_cached_secondary_ddt(ctx);
4056 if(res != AARUF_STATUS_OK) return res;
4057
4058 // Write primary DDT table (multi-level) if applicable
4059 res = write_primary_ddt(ctx);
4060 if(res != AARUF_STATUS_OK) return res;
4061
4062 // Write single-level DDT table if applicable
4063 res = write_single_level_ddt(ctx);
4064 if(res != AARUF_STATUS_OK) return res;
4065 }
4066
4067 // Finalize checksums and write checksum block
4068 write_checksum_block(ctx);
4069
4070 // Write tracks block
4071 write_tracks_block(ctx);
4072
4073 // Write MODE 2 subheader data block
4074 write_mode2_subheaders_block(ctx);
4075
4076 // Write CD sector prefix data block
4077 write_sector_prefix(ctx);
4078
4079 // Write sector prefix DDT (statuses + optional indexes)
4080 write_sector_prefix_ddt(ctx);
4081
4082 // Write CD sector suffix data block (EDC/ECC captures)
4083 write_sector_suffix(ctx);
4084
4085 // Write sector prefix DDT (EDC/ECC captures)
4086 write_sector_suffix_ddt(ctx);
4087
4088 // Write sector subchannel data block
4089 write_sector_subchannel(ctx);
4090
4091 // Write DVD long sector data blocks
4092 write_dvd_long_sector_blocks(ctx);
4093
4094 // Write DVD decrypted title keys
4095 write_dvd_title_key_decrypted_block(ctx);
4096
4097 // Write media tags data blocks
4098 write_media_tags(ctx);
4099
4100 // Write tape files
4101 write_tape_file_block(ctx);
4102
4103 // Write tape partitions
4104 write_tape_partition_block(ctx);
4105
4106 // Write geometry block if any
4107 write_geometry_block(ctx);
4108
4109 // Write metadata block
4110 write_metadata_block(ctx);
4111
4112 // Write dump hardware block if any
4113 write_dumphw_block(ctx);
4114
4115 // Write CICM XML block if any
4116 write_cicm_block(ctx);
4117
4118 // Write Aaru metadata JSON block if any
4119 write_aaru_json_block(ctx);
4120
4121 // Write the complete index at the end of the file
4122 res = write_index_block(ctx);
4123 if(res != AARUF_STATUS_OK) return res;
4124
4125 if(ctx->deduplicate && ctx->sector_hash_map != NULL)
4126 {
4127 TRACE("Clearing sector hash map");
4128 // Clear sector hash map
4129 free_map(ctx->sector_hash_map);
4130 ctx->sector_hash_map = NULL;
4131 }
4132 }
4133
4134 TRACE("Freeing memory pointers");
4135 // This may do nothing if imageStream is NULL, but as the behaviour is undefined, better sure than sorry
4136 if(ctx->imageStream != NULL)
4137 {
4138 fclose(ctx->imageStream);
4139 ctx->imageStream = NULL;
4140 }
4141
4142 // Free index entries array
4143 if(ctx->index_entries != NULL)
4144 {
4145 utarray_free(ctx->index_entries);
4146 ctx->index_entries = NULL;
4147 }
4148
4149 free(ctx->sector_prefix);
4150 ctx->sector_prefix = NULL;
4151 free(ctx->sector_prefix_corrected);
4152 ctx->sector_prefix_corrected = NULL;
4153 free(ctx->sector_suffix);
4154 ctx->sector_suffix = NULL;
4155 free(ctx->sector_suffix_corrected);
4156 ctx->sector_suffix_corrected = NULL;
4157 free(ctx->sector_subchannel);
4158 ctx->sector_subchannel = NULL;
4159 free(ctx->mode2_subheaders);
4160 ctx->mode2_subheaders = NULL;
4161
4162 TRACE("Freeing media tags");
4163 if(ctx->mediaTags != NULL) HASH_ITER(hh, ctx->mediaTags, media_tag, tmp_media_tag)
4164 {
4165 HASH_DEL(ctx->mediaTags, media_tag);
4166 free(media_tag->data);
4167 free(media_tag);
4168 }
4169
4170#ifdef __linux__ // TODO: Implement
4171 TRACE("Unmapping user data DDT if it is not in memory");
4172 if(!ctx->in_memory_ddt)
4173 {
4174 munmap(ctx->user_data_ddt, ctx->mapped_memory_ddt_size);
4175 ctx->user_data_ddt = NULL;
4176 }
4177#endif
4178
4179 free(ctx->sector_prefix_ddt2);
4180 ctx->sector_prefix_ddt2 = NULL;
4181 free(ctx->sector_prefix_ddt);
4182 ctx->sector_prefix_ddt = NULL;
4183 free(ctx->sector_suffix_ddt2);
4184 ctx->sector_suffix_ddt2 = NULL;
4185 free(ctx->sector_suffix_ddt);
4186 ctx->sector_suffix_ddt = NULL;
4187
4188 free(ctx->metadata_block);
4189 ctx->metadata_block = NULL;
4190 free(ctx->track_entries);
4191 ctx->track_entries = NULL;
4192 free(ctx->cicm_block);
4193 ctx->cicm_block = NULL;
4194
4195 if(ctx->dump_hardware_entries_with_data != NULL)
4196 {
4197 for(int i = 0; i < ctx->dump_hardware_header.entries; i++)
4198 {
4199 free(ctx->dump_hardware_entries_with_data[i].extents);
4200 ctx->dump_hardware_entries_with_data[i].extents = NULL;
4201 free(ctx->dump_hardware_entries_with_data[i].manufacturer);
4202 ctx->dump_hardware_entries_with_data[i].manufacturer = NULL;
4203 free(ctx->dump_hardware_entries_with_data[i].model);
4204 ctx->dump_hardware_entries_with_data[i].model = NULL;
4205 free(ctx->dump_hardware_entries_with_data[i].revision);
4206 ctx->dump_hardware_entries_with_data[i].revision = NULL;
4207 free(ctx->dump_hardware_entries_with_data[i].firmware);
4208 ctx->dump_hardware_entries_with_data[i].firmware = NULL;
4209 free(ctx->dump_hardware_entries_with_data[i].serial);
4210 ctx->dump_hardware_entries_with_data[i].serial = NULL;
4211 free(ctx->dump_hardware_entries_with_data[i].softwareName);
4212 ctx->dump_hardware_entries_with_data[i].softwareName = NULL;
4213 free(ctx->dump_hardware_entries_with_data[i].softwareVersion);
4214 ctx->dump_hardware_entries_with_data[i].softwareVersion = NULL;
4215 free(ctx->dump_hardware_entries_with_data[i].softwareOperatingSystem);
4216 ctx->dump_hardware_entries_with_data[i].softwareOperatingSystem = NULL;
4217 }
4218 ctx->dump_hardware_entries_with_data = NULL;
4219 }
4220
4221 free(ctx->readableSectorTags);
4222 ctx->readableSectorTags = NULL;
4223
4224 free(ctx->ecc_cd_context);
4225 ctx->ecc_cd_context = NULL;
4226
4227 free(ctx->checksums.spamsum);
4228 ctx->checksums.spamsum = NULL;
4229
4230 free(ctx->sector_id);
4231 free(ctx->sector_ied);
4232 free(ctx->sector_cpr_mai);
4233 free(ctx->sector_edc);
4234
4235 // TODO: Free caches
4236
4237 free(context);
4238
4239 TRACE("Exiting aaruf_close() = 0");
4240 return 0;
4241}
aaruformat.h
write_dvd_long_sector_blocks
void write_dvd_long_sector_blocks(aaruformat_context *ctx)
Serialize DVD long sector auxiliary data blocks to the image file.
Definition close.c:1808
write_primary_ddt
static int32_t write_primary_ddt(aaruformat_context *ctx)
Write (flush) the multi-level primary DDT table header and data back to its file offset.
Definition close.c:283
write_index_block
static int32_t write_index_block(aaruformat_context *ctx)
Serialize the accumulated index entries at the end of the image and back-patch the header.
Definition close.c:3862
write_media_tags
static void write_media_tags(const aaruformat_context *ctx)
Serialize all accumulated media tags to the image file.
Definition close.c:2409
write_geometry_block
static void write_geometry_block(const aaruformat_context *ctx)
Serialize the geometry metadata block to the image file.
Definition close.c:3026
aaruf_close
int aaruf_close(void *context)
Close an Aaru image context, flushing pending data structures and releasing resources.
Definition close.c:3995
write_single_level_ddt
static int32_t write_single_level_ddt(aaruformat_context *ctx)
Serialize a single-level DDT (tableShift == 0) directly after its header.
Definition close.c:369
write_dumphw_block
static void write_dumphw_block(aaruformat_context *ctx)
Serialize the dump hardware block containing acquisition environment information.
Definition close.c:3445
write_tape_partition_block
static void write_tape_partition_block(const aaruformat_context *ctx)
Serialize the tape partition metadata block to the image file.
Definition close.c:2901
write_checksum_block
static void write_checksum_block(aaruformat_context *ctx)
Finalize any active checksum calculations and append a checksum block.
Definition close.c:654
write_sector_suffix
static void write_sector_suffix(aaruformat_context *ctx)
Serialize the optional CD sector suffix block (EDC/ECC region capture).
Definition close.c:1088
write_sector_suffix_ddt
static void write_sector_suffix_ddt(aaruformat_context *ctx)
Serialize the per-sector CD suffix status / index DeDuplication Table (DDT v2, suffix variant).
Definition close.c:1350
write_tracks_block
static void write_tracks_block(aaruformat_context *ctx)
Serialize the tracks metadata block and add it to the index.
Definition close.c:798
write_cicm_block
static void write_cicm_block(const aaruformat_context *ctx)
Serialize the CICM XML metadata block to the image file.
Definition close.c:3675
write_aaru_json_block
static void write_aaru_json_block(const aaruformat_context *ctx)
Serialize the Aaru metadata JSON block to the image file.
Definition close.c:3812
write_mode2_subheaders_block
static void write_mode2_subheaders_block(aaruformat_context *ctx)
Serialize a MODE 2 (XA) subheaders data block.
Definition close.c:850
write_sector_subchannel
static void write_sector_subchannel(const aaruformat_context *ctx)
Serialize the per-sector subchannel or tag data block.
Definition close.c:1508
write_sector_prefix_ddt
static void write_sector_prefix_ddt(aaruformat_context *ctx)
Serialize the per-sector CD prefix status / index DeDuplication Table (DDT v2, prefix variant).
Definition close.c:1206
write_tape_ddt
static int32_t write_tape_ddt(aaruformat_context *ctx)
Converts tape DDT hash table to array format and writes it as a single-level DDT.
Definition close.c:596
write_cached_secondary_ddt
static int32_t write_cached_secondary_ddt(aaruformat_context *ctx)
Flush a cached secondary (child) DeDuplication Table (DDT) to the image.
Definition close.c:77
write_metadata_block
static void write_metadata_block(aaruformat_context *ctx)
Serialize the metadata block containing image and media descriptive information.
Definition close.c:3162
write_sector_prefix
static void write_sector_prefix(aaruformat_context *ctx)
Serialize the optional CD sector prefix block.
Definition close.c:966
write_dvd_title_key_decrypted_block
static void write_dvd_title_key_decrypted_block(const aaruformat_context *ctx)
Serialize the DVD decrypted title key data block to the image file.
Definition close.c:2245
write_tape_file_block
static void write_tape_file_block(const aaruformat_context *ctx)
Serialize the tape file metadata block to the image file.
Definition close.c:2669
LZMA_PROPERTIES_LENGTH
#define LZMA_PROPERTIES_LENGTH
Size in bytes of the fixed LZMA properties header (lc/lp/pb + dictionary size).
Definition consts.h:82
AARU_MAGIC
#define AARU_MAGIC
Magic identifier for AaruFormat container (ASCII "AARUFRMT").
Definition consts.h:64
MD5_DIGEST_LENGTH
#define MD5_DIGEST_LENGTH
Definition context.h:69
tapeFileHashEntry
struct TapeFileHashEntry tapeFileHashEntry
AARU_CALL
#define AARU_CALL
Definition decls.h:45
aaruf_sha1_final
void aaruf_sha1_final(sha1_ctx *ctx, unsigned char *result)
Definition sha1.c:124
aaruf_cst_transform
int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, size_t length)
Transforms interleaved subchannel data to sequential format.
Definition cst.c:35
aaruf_lzma_encode_buffer
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
aaruf_crc64_data
uint64_t aaruf_crc64_data(const uint8_t *data, uint32_t len)
Definition crc64.c:160
aaruf_crc64_update
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
aaruf_sha256_final
void aaruf_sha256_final(sha256_ctx *ctx, unsigned char *result)
Definition sha256.c:115
aaruf_crc64_init
crc64_ctx * aaruf_crc64_init()
Initializes a CRC64 context.
Definition crc64.c:32
aaruf_md5_final
void aaruf_md5_final(md5_ctx *ctx, unsigned char *result)
Definition md5.c:485
aaruf_spamsum_free
void aaruf_spamsum_free(spamsum_ctx *ctx)
Frees a spamsum (fuzzy hash) context.
Definition spamsum.c:75
aaruf_get_datatype_for_media_tag_type
int32_t aaruf_get_datatype_for_media_tag_type(int32_t tag_type)
Converts an Aaru media tag type to an image data type.
Definition helpers.c:197
AARU_EXPORT
#define AARU_EXPORT
Definition decls.h:54
aaruf_spamsum_final
int aaruf_spamsum_final(spamsum_ctx *ctx, uint8_t *result)
Definition spamsum.c:191
aaruf_crc64_final
int aaruf_crc64_final(crc64_ctx *ctx, uint64_t *crc)
Computes the final CRC64 value from the context.
Definition crc64.c:141
IndexBlock3
@ IndexBlock3
Block containing the index v3.
Definition enums.h:147
ChecksumBlock
@ ChecksumBlock
Block containing contents checksums.
Definition enums.h:152
DataBlock
@ DataBlock
Block containing data.
Definition enums.h:141
TapePartitionBlock
@ TapePartitionBlock
Block containing list of partitions for a tape image.
Definition enums.h:158
GeometryBlock
@ GeometryBlock
Block containing logical geometry.
Definition enums.h:148
DeDuplicationTableSecondary
@ DeDuplicationTableSecondary
Block containing a secondary deduplication table (v2).
Definition enums.h:144
AaruMetadataJsonBlock
@ AaruMetadataJsonBlock
Block containing JSON version of Aaru Metadata.
Definition enums.h:159
CicmBlock
@ CicmBlock
Block containing CICM XML metadata.
Definition enums.h:151
DeDuplicationTable2
@ DeDuplicationTable2
Block containing a deduplication table v2.
Definition enums.h:143
TapeFileBlock
@ TapeFileBlock
Block containing list of files for a tape image.
Definition enums.h:157
DumpHardwareBlock
@ DumpHardwareBlock
Block containing an array of hardware used to create the image.
Definition enums.h:156
MetadataBlock
@ MetadataBlock
Block containing metadata.
Definition enums.h:149
TracksBlock
@ TracksBlock
Block containing optical disc tracks.
Definition enums.h:150
OpticalDisc
@ OpticalDisc
Purely optical discs.
Definition enums.h:218
BlockMedia
@ BlockMedia
Media that is physically block-based or abstracted like that.
Definition enums.h:219
Blake3
@ Blake3
BLAKE3 hash.
Definition enums.h:173
Sha1
@ Sha1
SHA-1 hash.
Definition enums.h:170
Md5
@ Md5
MD5 hash.
Definition enums.h:169
Sha256
@ Sha256
SHA-256 hash.
Definition enums.h:171
SpamSum
@ SpamSum
SpamSum (context-triggered piecewise hash).
Definition enums.h:172
CdSectorSubchannel
@ CdSectorSubchannel
Compact Disc subchannel data.
Definition enums.h:116
AppleProfileTag
@ AppleProfileTag
Apple Profile (20‑byte) tag.
Definition enums.h:117
DvdSectorIed
@ DvdSectorIed
DVD ID Error Detection Code (IED)
Definition enums.h:129
DvdSectorCprMai
@ DvdSectorCprMai
DVD Copyright Management Information (CPR_MAI)
Definition enums.h:126
AppleSonyTag
@ AppleSonyTag
Apple Sony (12‑byte) tag.
Definition enums.h:118
CdSectorPrefix
@ CdSectorPrefix
Compact Disc sector prefix (sync, header).
Definition enums.h:114
PriamDataTowerTag
@ PriamDataTowerTag
Priam Data Tower (24‑byte) tag.
Definition enums.h:119
UserData
@ UserData
User (main) data.
Definition enums.h:46
DvdSectorEdc
@ DvdSectorEdc
DVD Error Detection Code (EDC)
Definition enums.h:130
DvdSectorTitleKeyDecrypted
@ DvdSectorTitleKeyDecrypted
Decrypted DVD Title Key.
Definition enums.h:127
DvdSectorId
@ DvdSectorId
DVD Identification Data (ID)
Definition enums.h:128
CdSectorSuffix
@ CdSectorSuffix
Compact Disc sector suffix (EDC, ECC P, ECC Q).
Definition enums.h:115
CompactDiscMode2Subheader
@ CompactDiscMode2Subheader
Compact Disc MODE 2 subheader.
Definition enums.h:123
AARU_FEATURE_RW_BLAKE3
@ AARU_FEATURE_RW_BLAKE3
BLAKE3 checksum is present (read/write support for BLAKE3 hashes).
Definition enums.h:265
AARUF_STATUS_INVALID_CONTEXT
@ AARUF_STATUS_INVALID_CONTEXT
Provided context/handle is invalid.
Definition enums.h:209
Lzma
@ Lzma
LZMA compression.
Definition enums.h:34
LzmaClauniaSubchannelTransform
@ LzmaClauniaSubchannelTransform
LZMA applied to Claunia Subchannel Transform processed data.
Definition enums.h:36
None
@ None
Not compressed.
Definition enums.h:33
AARUF_STATUS_OK
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:75
AARUF_ERROR_NOT_ENOUGH_MEMORY
#define AARUF_ERROR_NOT_ENOUGH_MEMORY
Memory allocation failure (critical).
Definition errors.h:48
AARUF_ERROR_CANNOT_WRITE_HEADER
#define AARUF_ERROR_CANNOT_WRITE_HEADER
Failure writing container header.
Definition errors.h:60
AppleProfile
@ AppleProfile
Definition aaru.h:698
AppleSonySS
@ AppleSonySS
3.5", SS, DD, 80 tracks, 8 to 12 spt, 512 bytes/sector, GCR
Definition aaru.h:247
PriamDataTower
@ PriamDataTower
Definition aaru.h:701
AppleFileWare
@ AppleFileWare
5.25", DS, ?D, ?? tracks, ?? spt, 512 bytes/sector, GCR, opposite side heads, aka Twiggy
Definition aaru.h:249
AppleSonyDS
@ AppleSonyDS
3.5", DS, DD, 80 tracks, 8 to 12 spt, 512 bytes/sector, GCR
Definition aaru.h:248
free_map
void free_map(hash_map_t *map)
Frees all memory associated with a hash map.
Definition hash_map.c:73
internal.h
aaruf_close_current_block
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
log.h
FATAL
#define FATAL(fmt,...)
Definition log.h:40
TRACE
#define TRACE(fmt,...)
Definition log.h:25
SHA1_DIGEST_LENGTH
#define SHA1_DIGEST_LENGTH
Definition sha1.h:39
SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH
Definition sha256.h:38
FUZZY_MAX_RESULT
#define FUZZY_MAX_RESULT
Definition spamsum.h:30
AaruHeaderV2
Version 2 container header with GUID, alignment shifts, and feature negotiation bitmaps.
Definition header.h:107
AaruHeaderV2::featureCompatible
uint64_t featureCompatible
Feature bits: unimplemented bits are ignorable (still R/W safe).
Definition header.h:122
AaruHeaderV2::indexOffset
uint64_t indexOffset
Absolute byte offset to primary index block (MUST be > 0; 0 => corrupt/unreadable).
Definition header.h:115
AaruMetadataJsonBlockHeader
Header for an Aaru metadata JSON block (identifier == BlockType::AaruMetadataJsonBlock).
Definition metadata.h:120
AaruMetadataJsonBlockHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::AaruMetadataJsonBlock.
Definition metadata.h:121
AaruMetadataJsonBlockHeader::length
uint32_t length
Length in bytes of the Aaru metadata JSON payload that follows.
Definition metadata.h:122
BlockHeader
Header preceding the compressed data payload of a data block (BlockType::DataBlock).
Definition data.h:71
BlockHeader::cmpLength
uint32_t cmpLength
Size in bytes of the compressed payload immediately following this header.
Definition data.h:76
BlockHeader::length
uint32_t length
Size in bytes of the uncompressed payload resulting after decompression.
Definition data.h:77
BlockHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::DataBlock.
Definition data.h:72
BlockHeader::cmpCrc64
uint64_t cmpCrc64
CRC64-ECMA of the compressed payload (cmpLength bytes).
Definition data.h:78
BlockHeader::crc64
uint64_t crc64
CRC64-ECMA of the uncompressed payload (length bytes).
Definition data.h:79
BlockHeader::type
uint16_t type
Logical data classification (value from DataType).
Definition data.h:73
BlockHeader::compression
uint16_t compression
Compression algorithm used (value from CompressionType).
Definition data.h:74
ChecksumEntry
Per-checksum metadata immediately followed by the digest / signature bytes.
Definition checksum.h:91
ChecksumEntry::length
uint32_t length
Length in bytes of the digest that immediately follows this structure.
Definition checksum.h:93
ChecksumEntry::type
uint8_t type
Algorithm used (value from ChecksumAlgorithm).
Definition checksum.h:92
ChecksumHeader
Header that precedes the sequence of checksum entries for a checksum block.
Definition checksum.h:74
ChecksumHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::ChecksumBlock.
Definition checksum.h:75
ChecksumHeader::length
uint32_t length
Length in bytes of the payload (all entries + their digest data, excluding this header).
Definition checksum.h:76
ChecksumHeader::entries
uint8_t entries
Number of checksum entries that follow in the payload.
Definition checksum.h:77
Checksums::spamsum
uint8_t * spamsum
SpamSum fuzzy hash (ASCII), allocated length+1 with trailing 0.
Definition context.h:110
Checksums::hasSha256
bool hasSha256
True if sha256[] buffer populated.
Definition context.h:103
Checksums::sha1
uint8_t sha1[20]
SHA-1 digest (20 bytes).
Definition context.h:107
Checksums::sha256
uint8_t sha256[32]
SHA-256 digest (32 bytes).
Definition context.h:108
Checksums::md5
uint8_t md5[16]
MD5 digest (16 bytes).
Definition context.h:106
Checksums::hasSpamSum
bool hasSpamSum
True if spamsum pointer allocated and signature read.
Definition context.h:105
Checksums::hasSha1
bool hasSha1
True if sha1[] buffer populated.
Definition context.h:102
Checksums::blake3
uint8_t blake3[BLAKE3_OUT_LEN]
BLAKE3 digest (32 bytes).
Definition context.h:109
Checksums::hasMd5
bool hasMd5
True if md5[] buffer populated.
Definition context.h:101
Checksums::hasBlake3
bool hasBlake3
True if blake3[] buffer populated.
Definition context.h:104
CicmMetadataBlock
Header for a CICM XML metadata block (identifier == BlockType::CicmBlock).
Definition metadata.h:108
CicmMetadataBlock::length
uint32_t length
Length in bytes of the CICM metadata payload that follows.
Definition metadata.h:110
CicmMetadataBlock::identifier
uint32_t identifier
Block identifier, must be BlockType::CicmBlock.
Definition metadata.h:109
DdtHeader2
Header preceding a version 2 hierarchical deduplication table.
Definition ddt.h:142
DdtHeader2::cmpCrc64
uint64_t cmpCrc64
CRC64-ECMA of compressed table payload.
Definition ddt.h:161
DdtHeader2::type
uint16_t type
Data classification (DataType) for sectors referenced by this table.
Definition ddt.h:144
DdtHeader2::start
uint64_t start
Base internal index covered by this table (used for secondary tables; currently informational).
Definition ddt.h:153
DdtHeader2::overflow
uint16_t overflow
Trailing dumped sectors beyond user area (overflow range), still mapped with entries.
Definition ddt.h:151
DdtHeader2::crc64
uint64_t crc64
CRC64-ECMA of uncompressed table payload.
Definition ddt.h:162
DdtHeader2::entries
uint64_t entries
Number of entries contained in (uncompressed) table payload.
Definition ddt.h:158
DdtHeader2::levels
uint8_t levels
Total number of hierarchy levels (root depth); > 0.
Definition ddt.h:146
DdtHeader2::length
uint64_t length
Uncompressed payload size in bytes.
Definition ddt.h:160
DdtHeader2::identifier
uint32_t identifier
Block identifier, must be BlockType::DeDuplicationTable2.
Definition ddt.h:143
DdtHeader2::tableShift
uint8_t tableShift
2^tableShift = number of logical sectors per primary entry (multi-level only; 0 for single-level or s...
Definition ddt.h:156
DdtHeader2::blocks
uint64_t blocks
Total internal span (negative + usable + overflow) in logical sectors.
Definition ddt.h:150
DdtHeader2::negative
uint16_t negative
Leading negative LBA count; added to external L to build internal index.
Definition ddt.h:149
DdtHeader2::blockAlignmentShift
uint8_t blockAlignmentShift
2^blockAlignmentShift = block alignment boundary in bytes.
Definition ddt.h:154
DdtHeader2::tableLevel
uint8_t tableLevel
Zero-based level index of this table (0 = root, increases downward).
Definition ddt.h:147
DdtHeader2::compression
uint16_t compression
Compression algorithm for this table body (CompressionType).
Definition ddt.h:145
DdtHeader2::dataShift
uint8_t dataShift
2^dataShift = sectors represented per increment in blockIndex field.
Definition ddt.h:155
DdtHeader2::cmpLength
uint64_t cmpLength
Compressed payload size in bytes.
Definition ddt.h:159
DdtHeader2::previousLevelOffset
uint64_t previousLevelOffset
Absolute byte offset of the parent (previous) level table; 0 if root.
Definition ddt.h:148
DumpExtent
Inclusive [start,end] logical sector range contributed by a single hardware environment.
Definition context.h:333
DumpHardwareEntriesWithData::firmware
uint8_t * firmware
Firmware version string or NULL.
Definition context.h:320
DumpHardwareEntriesWithData::revision
uint8_t * revision
Hardware revision string or NULL.
Definition context.h:319
DumpHardwareEntriesWithData::model
uint8_t * model
Model string or NULL.
Definition context.h:318
DumpHardwareEntriesWithData::softwareName
uint8_t * softwareName
Dump software name or NULL.
Definition context.h:322
DumpHardwareEntriesWithData::extents
struct DumpExtent * extents
Array of extents (entry.extents elements) or NULL.
Definition context.h:316
DumpHardwareEntriesWithData::manufacturer
uint8_t * manufacturer
Manufacturer string (UTF-8) or NULL.
Definition context.h:317
DumpHardwareEntriesWithData::softwareVersion
uint8_t * softwareVersion
Dump software version or NULL.
Definition context.h:323
DumpHardwareEntriesWithData::serial
uint8_t * serial
Serial number string or NULL.
Definition context.h:321
DumpHardwareEntriesWithData::entry
DumpHardwareEntry entry
Fixed-size header with lengths & counts.
Definition context.h:315
DumpHardwareEntriesWithData::softwareOperatingSystem
uint8_t * softwareOperatingSystem
Host operating system string or NULL.
Definition context.h:324
DumpHardwareEntry
Per-environment length table describing subsequent UTF-8 strings and optional extent array.
Definition dump.h:113
DumpHardwareEntry::softwareNameLength
uint32_t softwareNameLength
Length in bytes of dumping software name string.
Definition dump.h:119
DumpHardwareEntry::manufacturerLength
uint32_t manufacturerLength
Length in bytes of manufacturer UTF-8 string.
Definition dump.h:114
DumpHardwareEntry::softwareVersionLength
uint32_t softwareVersionLength
Length in bytes of dumping software version string.
Definition dump.h:120
DumpHardwareEntry::firmwareLength
uint32_t firmwareLength
Length in bytes of firmware version string.
Definition dump.h:117
DumpHardwareEntry::extents
uint32_t extents
Number of DumpExtent records following the strings (0 = none).
Definition dump.h:122
DumpHardwareEntry::modelLength
uint32_t modelLength
Length in bytes of model UTF-8 string.
Definition dump.h:115
DumpHardwareEntry::serialLength
uint32_t serialLength
Length in bytes of device serial number string.
Definition dump.h:118
DumpHardwareEntry::revisionLength
uint32_t revisionLength
Length in bytes of revision / hardware revision string.
Definition dump.h:116
DumpHardwareEntry::softwareOperatingSystemLength
uint32_t softwareOperatingSystemLength
Length in bytes of host operating system string.
Definition dump.h:121
DumpHardwareHeader
Header that precedes a sequence of dump hardware entries and their variable-length payload.
Definition dump.h:91
DumpHardwareHeader::crc64
uint64_t crc64
CRC64-ECMA of the payload (byte-swapped for legacy v1 images, handled automatically).
Definition dump.h:95
DumpHardwareHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::DumpHardwareBlock.
Definition dump.h:92
DumpHardwareHeader::length
uint32_t length
Total payload bytes after this header (sum of entries, strings, and extents arrays).
Definition dump.h:94
DumpHardwareHeader::entries
uint16_t entries
Number of DumpHardwareEntry records that follow.
Definition dump.h:93
GeometryBlockHeader
Legacy CHS style logical geometry metadata (BlockType::GeometryBlock).
Definition data.h:91
GeometryBlockHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::GeometryBlock.
Definition data.h:92
ImageInfo::MediaType
uint32_t MediaType
Media type identifier (see MediaType enum; 0=Unknown)
Definition aaru.h:881
ImageInfo::MetadataMediaType
uint8_t MetadataMediaType
Media type for sidecar generation (internal archival use)
Definition aaru.h:882
ImageInfo::Sectors
uint64_t Sectors
Total count of addressable logical sectors/blocks.
Definition aaru.h:874
IndexEntry
Single index entry describing a block's type, (optional) data classification, and file offset.
Definition index.h:109
IndexEntry::blockType
uint32_t blockType
Block identifier of the referenced block (value from BlockType).
Definition index.h:110
IndexEntry::offset
uint64_t offset
Absolute byte offset in the image where the referenced block header begins.
Definition index.h:112
IndexEntry::dataType
uint16_t dataType
Data classification (value from DataType) or unused for untyped blocks.
Definition index.h:111
IndexHeader3
Index header (version 3) adding hierarchical chaining (identifier == IndexBlock3).
Definition index.h:93
IndexHeader3::previous
uint64_t previous
File offset of a previous IndexBlock3 header (0 if none / root segment).
Definition index.h:97
IndexHeader3::identifier
uint32_t identifier
Block identifier (must be BlockType::IndexBlock3).
Definition index.h:94
IndexHeader3::crc64
uint64_t crc64
CRC64-ECMA of the local entries array (does NOT cover subindexes or previous chains).
Definition index.h:96
IndexHeader3::entries
uint64_t entries
Number of IndexEntry records that follow in this (sub)index block.
Definition index.h:95
MetadataBlockHeader
Header for a metadata block containing offsets and lengths to UTF-16LE descriptive strings.
Definition metadata.h:69
MetadataBlockHeader::commentsLength
uint32_t commentsLength
Length in bytes (including null) of comments string.
Definition metadata.h:78
MetadataBlockHeader::mediaSequence
int32_t mediaSequence
Sequence number within a multi-disc / multi-volume set (0-based or 1-based as producer defines).
Definition metadata.h:72
MetadataBlockHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::MetadataBlock.
Definition metadata.h:70
MetadataBlockHeader::mediaTitleOffset
uint32_t mediaTitleOffset
Offset to UTF-16LE media title string.
Definition metadata.h:79
MetadataBlockHeader::driveModelLength
uint32_t driveModelLength
Length in bytes (including null) of drive model string.
Definition metadata.h:94
MetadataBlockHeader::driveManufacturerLength
uint32_t driveManufacturerLength
Length in bytes (including null) of drive manufacturer string.
Definition metadata.h:92
MetadataBlockHeader::blockSize
uint32_t blockSize
Total size in bytes of the entire metadata block (header + strings).
Definition metadata.h:71
MetadataBlockHeader::driveModelOffset
uint32_t driveModelOffset
Offset to UTF-16LE drive model string.
Definition metadata.h:93
MetadataBlockHeader::mediaModelOffset
uint32_t mediaModelOffset
Offset to UTF-16LE media model string.
Definition metadata.h:83
MetadataBlockHeader::creatorOffset
uint32_t creatorOffset
Offset to UTF-16LE creator string (or undefined if creatorLength==0).
Definition metadata.h:75
MetadataBlockHeader::mediaTitleLength
uint32_t mediaTitleLength
Length in bytes (including null) of media title string.
Definition metadata.h:80
MetadataBlockHeader::mediaManufacturerOffset
uint32_t mediaManufacturerOffset
Offset to UTF-16LE media manufacturer string.
Definition metadata.h:81
MetadataBlockHeader::driveSerialNumberLength
uint32_t driveSerialNumberLength
Length in bytes (including null) of drive serial number string.
Definition metadata.h:96
MetadataBlockHeader::driveSerialNumberOffset
uint32_t driveSerialNumberOffset
Offset to UTF-16LE drive serial number string.
Definition metadata.h:95
MetadataBlockHeader::mediaManufacturerLength
uint32_t mediaManufacturerLength
Length in bytes (including null) of media manufacturer string.
Definition metadata.h:82
MetadataBlockHeader::mediaModelLength
uint32_t mediaModelLength
Length in bytes (including null) of media model string.
Definition metadata.h:84
MetadataBlockHeader::driveFirmwareRevisionOffset
uint32_t driveFirmwareRevisionOffset
Offset to UTF-16LE drive firmware revision string.
Definition metadata.h:97
MetadataBlockHeader::lastMediaSequence
int32_t lastMediaSequence
Total number of media in the set; 0 or 1 if single item.
Definition metadata.h:74
MetadataBlockHeader::commentsOffset
uint32_t commentsOffset
Offset to UTF-16LE comments string.
Definition metadata.h:77
MetadataBlockHeader::driveManufacturerOffset
uint32_t driveManufacturerOffset
Offset to UTF-16LE drive manufacturer string.
Definition metadata.h:91
MetadataBlockHeader::mediaSerialNumberOffset
uint32_t mediaSerialNumberOffset
Offset to UTF-16LE media serial number string.
Definition metadata.h:85
MetadataBlockHeader::mediaSerialNumberLength
uint32_t mediaSerialNumberLength
Length in bytes (including null) of media serial number string.
Definition metadata.h:86
MetadataBlockHeader::mediaPartNumberOffset
uint32_t mediaPartNumberOffset
Offset to UTF-16LE media part number string.
Definition metadata.h:89
MetadataBlockHeader::mediaPartNumberLength
uint32_t mediaPartNumberLength
Length in bytes (including null) of media part number string.
Definition metadata.h:90
MetadataBlockHeader::mediaBarcodeLength
uint32_t mediaBarcodeLength
Length in bytes (including null) of media barcode string.
Definition metadata.h:88
MetadataBlockHeader::creatorLength
uint32_t creatorLength
Length in bytes (including null) of creator string (0 if absent).
Definition metadata.h:76
MetadataBlockHeader::driveFirmwareRevisionLength
uint32_t driveFirmwareRevisionLength
Length in bytes (including null) of drive firmware revision string.
Definition metadata.h:98
MetadataBlockHeader::mediaBarcodeOffset
uint32_t mediaBarcodeOffset
Offset to UTF-16LE media barcode string.
Definition metadata.h:87
TapeDdtHashEntry
Definition context.h:141
TapeDdtHashEntry::key
uint64_t key
Key: sector address.
Definition context.h:142
TapeDdtHashEntry::value
uint64_t value
Value: DDT entry.
Definition context.h:143
TapeFileEntry
Describes a single logical file on a tape medium.
Definition tape.h:134
TapeFileHashEntry::fileEntry
TapeFileEntry fileEntry
The actual tape file data.
Definition context.h:129
TapeFileHeader
Header for a tape file metadata block containing file layout information.
Definition tape.h:238
TapeFileHeader::identifier
uint32_t identifier
Block type identifier.
Definition tape.h:239
TapeFileHeader::crc64
uint64_t crc64
CRC64-ECMA checksum of the entry data.
Definition tape.h:245
TapeFileHeader::length
uint64_t length
Size of entry data in bytes (excluding this header).
Definition tape.h:243
TapePartitionEntry
Describes a single physical partition on a tape medium.
Definition tape.h:320
TapePartitionHashEntry
Definition context.h:134
TapePartitionHashEntry::partitionEntry
TapePartitionEntry partitionEntry
The actual tape partition data.
Definition context.h:136
TapePartitionHeader
Header for a tape partition metadata block containing partition layout information.
Definition tape.h:441
TapePartitionHeader::crc64
uint64_t crc64
CRC64-ECMA checksum of the entry data.
Definition tape.h:448
TapePartitionHeader::length
uint64_t length
Size of entry data in bytes (excluding this header).
Definition tape.h:446
TapePartitionHeader::identifier
uint32_t identifier
Block type identifier.
Definition tape.h:442
TrackEntry
Single optical disc track descriptor (sequence, type, LBAs, session, ISRC, flags).
Definition optical.h:72
TracksHeader
Header for an optical tracks block listing track entries.
Definition optical.h:62
TracksHeader::entries
uint16_t entries
Number of TrackEntry records following this header.
Definition optical.h:64
aaruformat_context
Master context representing an open or in‑creation Aaru image.
Definition context.h:172
aaruformat_context::media_barcode
uint8_t * media_barcode
Barcode of the media represented by the image.
Definition context.h:222
aaruformat_context::user_data_ddt_header
DdtHeader2 user_data_ddt_header
Active user data DDT v2 header (primary table meta).
Definition context.h:189
aaruformat_context::checksums
Checksums checksums
Whole-image checksums discovered.
Definition context.h:269
aaruformat_context::creator
uint8_t * creator
Who (person) created the image?
Definition context.h:216
aaruformat_context::deduplicate
bool deduplicate
Storage deduplication active (duplicates coalesce).
Definition context.h:298
aaruformat_context::compression_enabled
bool compression_enabled
True if block compression enabled (writing path).
Definition context.h:299
aaruformat_context::cicm_block
uint8_t * cicm_block
CICM XML payload.
Definition context.h:214
aaruformat_context::sector_cpr_mai
uint8_t * sector_cpr_mai
DVD sector CPR_MAI (6 bytes) if present.
Definition context.h:207
aaruformat_context::sector_hash_map
hash_map_t * sector_hash_map
Deduplication hash map (fingerprint->entry mapping).
Definition context.h:253
aaruformat_context::sector_prefix_corrected
uint8_t * sector_prefix_corrected
Corrected variant (post error correction) if stored.
Definition context.h:200
aaruformat_context::user_data_ddt
uint64_t * user_data_ddt
Legacy flat DDT pointer (NULL when using v2 mini/big arrays).
Definition context.h:181
aaruformat_context::sha256_context
sha256_ctx sha256_context
Opaque SHA-256 context for streaming updates.
Definition context.h:272
aaruformat_context::calculating_sha256
bool calculating_sha256
True if whole-image SHA-256 being calculated on-the-fly.
Definition context.h:275
aaruformat_context::drive_firmware_revision
uint8_t * drive_firmware_revision
Firmware revision of the drive used to read the media represented by the image.
Definition context.h:228
aaruformat_context::media_serial_number
uint8_t * media_serial_number
Serial number of the media represented by the image.
Definition context.h:221
aaruformat_context::sector_ied
uint8_t * sector_ied
DVD sector IED (2 bytes) if present.
Definition context.h:206
aaruformat_context::md5_context
md5_ctx md5_context
Opaque MD5 context for streaming updates.
Definition context.h:270
aaruformat_context::user_data_ddt2
uint64_t * user_data_ddt2
DDT entries (big variant) primary/secondary current.
Definition context.h:187
aaruformat_context::metadata_block_header
MetadataBlockHeader metadata_block_header
Metadata block header.
Definition context.h:230
aaruformat_context::sector_prefix
uint8_t * sector_prefix
Raw per-sector prefix (e.g., sync+header) uncorrected.
Definition context.h:199
aaruformat_context::sector_suffix_ddt2
uint64_t * sector_suffix_ddt2
CD sector suffix DDT V2.
Definition context.h:186
aaruformat_context::tape_files
tapeFileHashEntry * tape_files
Hash table root for tape files.
Definition context.h:302
aaruformat_context::cached_ddt_offset
uint64_t cached_ddt_offset
File offset of currently cached secondary DDT (0=none).
Definition context.h:190
aaruformat_context::is_tape
bool is_tape
True if the image is a tape image.
Definition context.h:304
aaruformat_context::sector_edc
uint8_t * sector_edc
DVD sector EDC (4 bytes) if present.
Definition context.h:208
aaruformat_context::calculating_sha1
bool calculating_sha1
True if whole-image SHA-1 being calculated on-the-fly.
Definition context.h:274
aaruformat_context::media_model
uint8_t * media_model
Model of the media represented by the image.
Definition context.h:220
aaruformat_context::drive_serial_number
uint8_t * drive_serial_number
Serial number of the drive used to read the media represented by the image.
Definition context.h:226
aaruformat_context::ecc_cd_context
CdEccContext * ecc_cd_context
CD ECC/EDC helper tables (allocated on demand).
Definition context.h:248
aaruformat_context::sector_suffix_ddt
uint32_t * sector_suffix_ddt
Legacy CD sector suffix DDT.
Definition context.h:184
aaruformat_context::drive_manufacturer
uint8_t * drive_manufacturer
Manufacturer of the drive used to read the media represented by the image.
Definition context.h:224
aaruformat_context::in_memory_ddt
bool in_memory_ddt
True if primary (and possibly secondary) DDT loaded.
Definition context.h:196
aaruformat_context::sector_suffix
uint8_t * sector_suffix
Raw per-sector suffix (EDC/ECC) uncorrected.
Definition context.h:201
aaruformat_context::header
AaruHeaderV2 header
Parsed container header (v2).
Definition context.h:175
aaruformat_context::is_writing
bool is_writing
True if context opened/created for writing.
Definition context.h:292
aaruformat_context::tape_ddt
TapeDdtHashEntry * tape_ddt
Hash table root for tape DDT entries.
Definition context.h:182
aaruformat_context::spamsum_context
spamsum_ctx * spamsum_context
Opaque SpamSum context for streaming updates.
Definition context.h:267
aaruformat_context::cicm_block_header
CicmMetadataBlock cicm_block_header
CICM metadata header (if present).
Definition context.h:231
aaruformat_context::sector_prefix_offset
size_t sector_prefix_offset
Current position in sector_prefix.
Definition context.h:286
aaruformat_context::drive_model
uint8_t * drive_model
Model of the drive used to read the media represented by the image.
Definition context.h:225
aaruformat_context::magic
uint64_t magic
File magic (AARU_MAGIC) post-open.
Definition context.h:174
aaruformat_context::writing_buffer
uint8_t * writing_buffer
Accumulation buffer for current block data.
Definition context.h:280
aaruformat_context::sector_prefix_ddt2
uint64_t * sector_prefix_ddt2
CD sector prefix DDT V2.
Definition context.h:185
aaruformat_context::calculating_spamsum
bool calculating_spamsum
True if whole-image SpamSum being calculated on-the-fly.
Definition context.h:276
aaruformat_context::primary_ddt_offset
uint64_t primary_ddt_offset
File offset of the primary DDT v2 table.
Definition context.h:192
aaruformat_context::mediaTags
mediaTagEntry * mediaTags
Hash table of extra media tags (uthash root).
Definition context.h:264
aaruformat_context::blake3_context
blake3_hasher * blake3_context
Opaque BLAKE3 context for streaming updates.
Definition context.h:268
aaruformat_context::calculating_blake3
bool calculating_blake3
True if whole-image BLAKE3 being calculated on-the-fly.
Definition context.h:277
aaruformat_context::dump_hardware_entries_with_data
struct DumpHardwareEntriesWithData * dump_hardware_entries_with_data
Array of dump hardware entries + strings.
Definition context.h:212
aaruformat_context::calculating_md5
bool calculating_md5
True if whole-image MD5 being calculated on-the-fly.
Definition context.h:273
aaruformat_context::geometry_block
GeometryBlockHeader geometry_block
Logical geometry block (if present).
Definition context.h:229
aaruformat_context::sector_suffix_offset
size_t sector_suffix_offset
Current position in sector_suffix.
Definition context.h:287
aaruformat_context::cached_secondary_ddt2
uint64_t * cached_secondary_ddt2
Cached secondary table (big entries) or NULL.
Definition context.h:188
aaruformat_context::json_block
uint8_t * json_block
JSON metadata block payload (UTF-8).
Definition context.h:215
aaruformat_context::media_part_number
uint8_t * media_part_number
Part number of the media represented by the image.
Definition context.h:223
aaruformat_context::sector_decrypted_title_key
uint8_t * sector_decrypted_title_key
DVD decrypted title key (5 bytes) if present.
Definition context.h:209
aaruformat_context::json_block_header
AaruMetadataJsonBlockHeader json_block_header
JSON metadata block header (if present).
Definition context.h:233
aaruformat_context::sector_subchannel
uint8_t * sector_subchannel
Raw 96-byte subchannel (if captured).
Definition context.h:203
aaruformat_context::comments
uint8_t * comments
Image comments.
Definition context.h:218
aaruformat_context::imageStream
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:176
aaruformat_context::index_entries
UT_array * index_entries
Flattened index entries (UT_array of IndexEntry).
Definition context.h:252
aaruformat_context::mode2_subheaders
uint8_t * mode2_subheaders
MODE2 Form1/Form2 8-byte subheaders (concatenated).
Definition context.h:204
aaruformat_context::image_info
ImageInfo image_info
Exposed high-level image info summary.
Definition context.h:260
aaruformat_context::sector_id
uint8_t * sector_id
DVD sector ID (4 bytes) if present.
Definition context.h:205
aaruformat_context::dump_hardware_header
DumpHardwareHeader dump_hardware_header
Dump hardware header.
Definition context.h:232
aaruformat_context::sha1_context
sha1_ctx sha1_context
Opaque SHA-1 context for streaming updates.
Definition context.h:271
aaruformat_context::readableSectorTags
bool * readableSectorTags
Per-sector boolean array (optical tags read successfully?).
Definition context.h:263
aaruformat_context::tape_partitions
TapePartitionHashEntry * tape_partitions
Hash table root for tape partitions.
Definition context.h:303
aaruformat_context::sector_prefix_ddt
uint32_t * sector_prefix_ddt
Legacy CD sector prefix DDT (deprecated by *2).
Definition context.h:183
aaruformat_context::lzma_dict_size
uint32_t lzma_dict_size
LZMA dictionary size (writing path).
Definition context.h:297
aaruformat_context::track_entries
TrackEntry * track_entries
Full track list (tracksHeader.entries elements).
Definition context.h:242
aaruformat_context::sector_suffix_corrected
uint8_t * sector_suffix_corrected
Corrected suffix if stored separately.
Definition context.h:202
aaruformat_context::metadata_block
uint8_t * metadata_block
Raw metadata UTF-16LE concatenated strings.
Definition context.h:213
aaruformat_context::cached_ddt_position
uint64_t cached_ddt_position
Position index of cached secondary DDT.
Definition context.h:191
aaruformat_context::media_title
uint8_t * media_title
Title of the media represented by the image.
Definition context.h:217
aaruformat_context::mapped_memory_ddt_size
size_t mapped_memory_ddt_size
Length of mmapped DDT if userDataDdt is mmapped.
Definition context.h:193
aaruformat_context::media_manufacturer
uint8_t * media_manufacturer
Manufacturer of the media represented by the image.
Definition context.h:219
aaruformat_context::tracks_header
TracksHeader tracks_header
Tracks header (optical) if present.
Definition context.h:244
crc64_ctx
Minimal ECMA-182 CRC64 incremental state container (running value only).
Definition crc64.h:56
mediaTagEntry
Hash table entry for an arbitrary media tag (e.g., proprietary drive/medium descriptor).
Definition context.h:119
mediaTagEntry::data
uint8_t * data
Tag data blob (opaque to library core); length bytes long.
Definition context.h:120
mediaTagEntry::type
int32_t type
Numeric type identifier.
Definition context.h:121
mediaTagEntry::length
uint32_t length
Length in bytes of data.
Definition context.h:122