Loading...
Searching...
No Matches
options.c
Go to the documentation of this file.
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2026 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs/options.h"
#include "log.h"
aaru_options parse_options(const char *options, bool *table_shift_found)
{
const char *options_str = options != NULL ? options : "(null)";
TRACE("Entering parse_options(%s)", options_str);
aaru_options parsed = {.compress = true,
.deduplicate = true,
.dictionary = 33554432,
.table_shift = -1,
.data_shift = 12,
.block_alignment = 9,
.md5 = false,
.sha1 = false,
.sha256 = false,
.blake3 = false,
.spamsum = false};
if(options == NULL)
{
TRACE("Exiting parse_options() = {compress: %d, deduplicate: %d, dictionary: %u, table_shift: %d, "
"data_shift: %u, block_alignment: %u, md5: %d, sha1: %d, sha256: %d, blake3: %d, spamsum: %d}",
parsed.compress, parsed.deduplicate, parsed.dictionary, parsed.table_shift, parsed.data_shift,
parsed.block_alignment, parsed.md5, parsed.sha1, parsed.sha256, parsed.blake3, parsed.spamsum);
return parsed;
}
char buffer[1024];
strncpy(buffer, options, sizeof(buffer));
buffer[sizeof(buffer) - 1] = '\0';
char *saveptr = NULL;
char *token = strtok_r(buffer, ";", &saveptr);
while(token != NULL)
{
char *equal = strchr(token, '=');
if(equal)
{
*equal = '\0';
const char *key = token;
const char *value = equal + 1;
const bool bval = strncmp(value, "true", 4) == 0;
if(strncmp(key, "compress", 8) == 0)
parsed.compress = bval;
else if(strncmp(key, "deduplicate", 11) == 0)
parsed.deduplicate = bval;
else if(strncmp(key, "dictionary", 10) == 0)
{
parsed.dictionary = (uint32_t)strtoul(value, NULL, 10);
if(parsed.dictionary == 0) parsed.dictionary = 33554432;
}
else if(strncmp(key, "table_shift", 11) == 0)
{
errno = 0;
char *endptr = NULL;
long parsed_value = strtol(value, &endptr, 10);
if(errno == 0 && endptr != value)
{
if(parsed_value < INT8_MIN) parsed_value = INT8_MIN;
if(parsed_value > INT8_MAX) parsed_value = INT8_MAX;
parsed.table_shift = (int8_t)parsed_value;
*table_shift_found = true;
}
}
else if(strncmp(key, "data_shift", 10) == 0)
{
errno = 0;
char *endptr = NULL;
long parsed_value = strtol(value, &endptr, 10);
if(errno == 0 && endptr != value && parsed_value >= 0)
{
if(parsed_value > UINT8_MAX) parsed_value = UINT8_MAX;
parsed.data_shift = (uint8_t)parsed_value;
if(parsed.data_shift == 0) parsed.data_shift = 12;
}
}
else if(strncmp(key, "block_alignment", 15) == 0)
{
errno = 0;
char *endptr = NULL;
long parsed_value = strtol(value, &endptr, 10);
if(errno == 0 && endptr != value && parsed_value >= 0)
{
if(parsed_value > UINT8_MAX) parsed_value = UINT8_MAX;
parsed.block_alignment = (uint8_t)parsed_value;
if(parsed.block_alignment == 0) parsed.block_alignment = 9;
}
}
else if(strncmp(key, "md5", 3) == 0)
parsed.md5 = bval;
else if(strncmp(key, "sha1", 4) == 0)
parsed.sha1 = bval;
else if(strncmp(key, "sha256", 6) == 0)
parsed.sha256 = bval;
else if(strncmp(key, "blake3", 6) == 0)
parsed.blake3 = bval;
else if(strncmp(key, "spamsum", 7) == 0)
parsed.spamsum = bval;
}
token = strtok_r(NULL, ";", &saveptr);
}
TRACE("Exiting parse_options() = {compress: %d, deduplicate: %d, dictionary: %u, table_shift: %d, "
"data_shift: %u, block_alignment: %u, md5: %d, sha1: %d, sha256: %d, blake3: %d, spamsum: %d}",
parsed.compress, parsed.deduplicate, parsed.dictionary, parsed.table_shift, parsed.data_shift,
parsed.block_alignment, parsed.md5, parsed.sha1, parsed.sha256, parsed.blake3, parsed.spamsum);
return parsed;
}
log.h
TRACE
#define TRACE(fmt,...)
Definition log.h:25
parse_options
aaru_options parse_options(const char *options, bool *table_shift_found)
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