libaaruformat 1.0 Aaru Data Preservation Suite - Format Library

Loading...
Searching...
No Matches
options.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2025 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18#include <errno.h>
19#include <limits.h>
20#include <stdbool.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "structs/options.h"
27
28#include "log.h"
29
38 aaru_options parse_options(const char *options)
39{
40 const char *options_str = options != NULL ? options : "(null)";
41 TRACE("Entering parse_options(%s)", options_str);
42
43 aaru_options parsed = {.compress = true,
44 .deduplicate = true,
45 .dictionary = 33554432,
46 .table_shift = -1,
47 .data_shift = 12,
48 .block_alignment = 9,
49 .md5 = false,
50 .sha1 = false,
51 .sha256 = false,
52 .blake3 = false,
53 .spamsum = false};
54
55 if(options == NULL)
56 {
57 TRACE("Exiting parse_options() = {compress: %d, deduplicate: %d, dictionary: %u, table_shift: %d, "
58 "data_shift: %u, block_alignment: %u, md5: %d, sha1: %d, sha256: %d, blake3: %d, spamsum: %d}",
59 parsed.compress, parsed.deduplicate, parsed.dictionary, parsed.table_shift, parsed.data_shift,
60 parsed.block_alignment, parsed.md5, parsed.sha1, parsed.sha256, parsed.blake3, parsed.spamsum);
61 return parsed;
62 }
63
64 char buffer[1024];
65 strncpy(buffer, options, sizeof(buffer));
66 buffer[sizeof(buffer) - 1] = '\0';
67
68 char *saveptr = NULL;
69 char *token = strtok_r(buffer, ";", &saveptr);
70 while(token != NULL)
71 {
72 char *equal = strchr(token, '=');
73 if(equal)
74 {
75 *equal = '\0';
76 const char *key = token;
77 const char *value = equal + 1;
78
79 const bool bval = strncmp(value, "true", 4) == 0;
80
81 if(strncmp(key, "compress", 8) == 0)
82 parsed.compress = bval;
83 else if(strncmp(key, "deduplicate", 11) == 0)
84 parsed.deduplicate = bval;
85 else if(strncmp(key, "dictionary", 10) == 0)
86 {
87 parsed.dictionary = (uint32_t)strtoul(value, NULL, 10);
88 if(parsed.dictionary == 0) parsed.dictionary = 33554432;
89 }
90 else if(strncmp(key, "table_shift", 11) == 0)
91 {
92 errno = 0;
93 char *endptr = NULL;
94 long parsed_value = strtol(value, &endptr, 10);
95 if(errno == 0 && endptr != value)
96 {
97 if(parsed_value < INT8_MIN) parsed_value = INT8_MIN;
98 if(parsed_value > INT8_MAX) parsed_value = INT8_MAX;
99 parsed.table_shift = (int8_t)parsed_value;
100 if(parsed.table_shift == 0) parsed.table_shift = 9;
101 }
102 }
103 else if(strncmp(key, "data_shift", 10) == 0)
104 {
105 errno = 0;
106 char *endptr = NULL;
107 long parsed_value = strtol(value, &endptr, 10);
108 if(errno == 0 && endptr != value && parsed_value >= 0)
109 {
110 if(parsed_value > UINT8_MAX) parsed_value = UINT8_MAX;
111 parsed.data_shift = (uint8_t)parsed_value;
112 if(parsed.data_shift == 0) parsed.data_shift = 12;
113 }
114 }
115 else if(strncmp(key, "block_alignment", 15) == 0)
116 {
117 errno = 0;
118 char *endptr = NULL;
119 long parsed_value = strtol(value, &endptr, 10);
120 if(errno == 0 && endptr != value && parsed_value >= 0)
121 {
122 if(parsed_value > UINT8_MAX) parsed_value = UINT8_MAX;
123 parsed.block_alignment = (uint8_t)parsed_value;
124 if(parsed.block_alignment == 0) parsed.block_alignment = 9;
125 }
126 }
127 else if(strncmp(key, "md5", 3) == 0)
128 parsed.md5 = bval;
129 else if(strncmp(key, "sha1", 4) == 0)
130 parsed.sha1 = bval;
131 else if(strncmp(key, "sha256", 6) == 0)
132 parsed.sha256 = bval;
133 else if(strncmp(key, "blake3", 6) == 0)
134 parsed.blake3 = bval;
135 else if(strncmp(key, "spamsum", 7) == 0)
136 parsed.spamsum = bval;
137 }
138 token = strtok_r(NULL, ";", &saveptr);
139 }
140
141 TRACE("Exiting parse_options() = {compress: %d, deduplicate: %d, dictionary: %u, table_shift: %d, "
142 "data_shift: %u, block_alignment: %u, md5: %d, sha1: %d, sha256: %d, blake3: %d, spamsum: %d}",
143 parsed.compress, parsed.deduplicate, parsed.dictionary, parsed.table_shift, parsed.data_shift,
144 parsed.block_alignment, parsed.md5, parsed.sha1, parsed.sha256, parsed.blake3, parsed.spamsum);
145 return parsed;
146}
log.h
TRACE
#define TRACE(fmt,...)
Definition log.h:25
parse_options
aaru_options parse_options(const char *options)
Parses the options string for AaruFormat image creation/opening.
Definition options.c:38
options.h
< For bool type used in aaru_options.
aaru_options
Parsed user-specified tunables controlling compression, deduplication, hashing and DDT geometry.
Definition options.h:217
aaru_options::deduplicate
bool deduplicate
Storage dedup flag (DDT always exists).
Definition options.h:219
aaru_options::data_shift
uint8_t data_shift
Global data shift: low bits encode sector offset inside a block (2^data_shift span).
Definition options.h:223
aaru_options::dictionary
uint32_t dictionary
LZMA dictionary size in bytes (>= 4096 recommended). Default: 33554432 (32 MiB).
Definition options.h:221
aaru_options::compress
bool compress
Enable adaptive compression (LZMA for data blocks, FLAC for audio). Default: true.
Definition options.h:218
aaru_options::sha256
bool sha256
Generate SHA-256 checksum (ChecksumAlgorithm::Sha256) when finalizing image.
Definition options.h:227
aaru_options::spamsum
bool spamsum
Generate SpamSum fuzzy hash (ChecksumAlgorithm::SpamSum) if enabled.
Definition options.h:229
aaru_options::table_shift
int8_t table_shift
DDT table shift (multi-level fan-out exponent). Default: heuristically calculated.
Definition options.h:222
aaru_options::md5
bool md5
Generate MD5 checksum (ChecksumAlgorithm::Md5) when finalizing image.
Definition options.h:225
aaru_options::blake3
bool blake3
Generate BLAKE3 checksum if supported (not stored if algorithm unavailable).
Definition options.h:228
aaru_options::sha1
bool sha1
Generate SHA-1 checksum (ChecksumAlgorithm::Sha1) when finalizing image.
Definition options.h:226
aaru_options::block_alignment
uint8_t block_alignment
log2 underlying block alignment (2^n bytes). Default: 9 (512 bytes).
Definition options.h:224