libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
dpm.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 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
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "aaruformat.h"
24#include "internal.h"
25#include "log.h"
26
43AARU_EXPORT int32_t AARU_CALL aaruf_get_dpm(const void *context, uint8_t *buffer, size_t *length)
44{
45 TRACE("Entering aaruf_get_dpm(%p, %p, %p)", context, buffer, length);
46
47 if(context == NULL || length == NULL)
48 {
49 FATAL("Invalid context");
50
51 TRACE("Exiting aaruf_get_dpm() = AARUF_ERROR_NOT_AARUFORMAT");
53 }
54
55 const aaruformat_context *ctx = context;
56
57 if(ctx->magic != AARU_MAGIC)
58 {
59 FATAL("Invalid context");
60
61 TRACE("Exiting aaruf_get_dpm() = AARUF_ERROR_NOT_AARUFORMAT");
63 }
64
65 if(ctx->dpm_block == NULL || ctx->dpm_block_length == 0)
66 {
67 TRACE("No DPM present");
68 *length = 0;
69
70 TRACE("Exiting aaruf_get_dpm() = AARUF_ERROR_METADATA_NOT_PRESENT");
72 }
73
74 if(buffer == NULL || *length < ctx->dpm_block_length)
75 {
76 *length = ctx->dpm_block_length;
77
78 TRACE("Exiting aaruf_get_dpm() = AARUF_ERROR_BUFFER_TOO_SMALL");
80 }
81
82 memcpy(buffer, ctx->dpm_block, ctx->dpm_block_length);
83 *length = ctx->dpm_block_length;
84
85 TRACE("Exiting aaruf_get_dpm() = AARUF_STATUS_OK");
86 return AARUF_STATUS_OK;
87}
88
102AARU_EXPORT int32_t AARU_CALL aaruf_set_dpm(void *context, const uint8_t *data, size_t length)
103{
104 TRACE("Entering aaruf_set_dpm(%p, %p, %zu)", context, data, length);
105
106 if(context == NULL)
107 {
108 FATAL("Invalid context");
109
110 TRACE("Exiting aaruf_set_dpm() = AARUF_ERROR_NOT_AARUFORMAT");
112 }
113
114 aaruformat_context *ctx = context;
115
116 if(ctx->magic != AARU_MAGIC)
117 {
118 FATAL("Invalid context");
119
120 TRACE("Exiting aaruf_set_dpm() = AARUF_ERROR_NOT_AARUFORMAT");
122 }
123
124 if(!ctx->is_writing)
125 {
126 FATAL("Trying to write a read-only image");
127
128 TRACE("Exiting aaruf_set_dpm() = AARUF_READ_ONLY");
129 return AARUF_READ_ONLY;
130 }
131
132 const int32_t status = validate_dpm_block(data, length);
133
134 if(status != AARUF_STATUS_OK)
135 {
136 FATAL("Invalid DPM block");
137
138 TRACE("Exiting aaruf_set_dpm() = %d", status);
139 return status;
140 }
141
142 uint8_t *copy = malloc(length);
143
144 if(copy == NULL)
145 {
146 FATAL("Could not allocate memory for DPM block");
147
148 TRACE("Exiting aaruf_set_dpm() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
150 }
151
152 memcpy(copy, data, length);
153 free(ctx->dpm_block);
154 ctx->dpm_block = copy;
155 ctx->dpm_block_length = length;
156 ctx->dirty_dpm_block = true;
157
158 TRACE("Exiting aaruf_set_dpm() = AARUF_STATUS_OK");
159 return AARUF_STATUS_OK;
160}
161
173{
174 TRACE("Entering aaruf_clear_dpm(%p)", context);
175
176 if(context == NULL)
177 {
178 FATAL("Invalid context");
179
180 TRACE("Exiting aaruf_clear_dpm() = AARUF_ERROR_NOT_AARUFORMAT");
182 }
183
184 aaruformat_context *ctx = context;
185
186 if(ctx->magic != AARU_MAGIC)
187 {
188 FATAL("Invalid context");
189
190 TRACE("Exiting aaruf_clear_dpm() = AARUF_ERROR_NOT_AARUFORMAT");
192 }
193
194 if(!ctx->is_writing)
195 {
196 FATAL("Trying to write a read-only image");
197
198 TRACE("Exiting aaruf_clear_dpm() = AARUF_READ_ONLY");
199 return AARUF_READ_ONLY;
200 }
201
202 free(ctx->dpm_block);
203 ctx->dpm_block = NULL;
204 ctx->dpm_block_length = 0;
205 ctx->dirty_dpm_block = true;
206
207 TRACE("Exiting aaruf_clear_dpm() = AARUF_STATUS_OK");
208 return AARUF_STATUS_OK;
209}
#define AARU_MAGIC
Magic identifier for AaruFormat container (ASCII "AARUFRMT").
Definition consts.h:64
#define AARU_CALL
Definition decls.h:46
int32_t aaruf_get_dpm(const void *context, uint8_t *buffer, size_t *length)
Retrieves the Data Position Measurement block.
Definition dpm.c:43
int32_t aaruf_set_dpm(void *context, const uint8_t *data, size_t length)
Sets the Data Position Measurement block.
Definition dpm.c:102
#define AARU_EXPORT
Definition decls.h:55
int32_t aaruf_clear_dpm(void *context)
Removes the Data Position Measurement block.
Definition dpm.c:172
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:82
#define AARUF_READ_ONLY
Operation requires write mode but context is read-only.
Definition errors.h:61
#define AARUF_ERROR_NOT_ENOUGH_MEMORY
Memory allocation failure (critical).
Definition errors.h:48
#define AARUF_ERROR_METADATA_NOT_PRESENT
Requested metadata not present in image.
Definition errors.h:69
#define AARUF_ERROR_NOT_AARUFORMAT
Input file/stream failed magic or structural validation.
Definition errors.h:40
#define AARUF_ERROR_BUFFER_TOO_SMALL
Caller-supplied buffer insufficient for data.
Definition errors.h:49
int32_t validate_dpm_block(const uint8_t *data, size_t length)
Validates a serialized Data Position Measurement block.
Definition dpm.c:53
#define FATAL(fmt,...)
Definition log.h:75
#define TRACE(fmt,...)
Definition log.h:69
Master context representing an open or in‑creation Aaru image.
Definition context.h:178
size_t dpm_block_length
Length of dpm_block in bytes, 0 if not present.
Definition context.h:224
bool dirty_dpm_block
True if DPM block should be written or removed during close.
Definition context.h:352
bool is_writing
True if context opened/created for writing.
Definition context.h:302
uint64_t magic
File magic (AARU_MAGIC) post-open.
Definition context.h:180
uint8_t * dpm_block
Data Position Measurement block, header included.
Definition context.h:223