Move get_first_data_offset to luks2_segment.c

This commit is contained in:
Ondrej Kozina
2019-02-25 17:39:33 +01:00
committed by Milan Broz
parent 36ac5fe735
commit 203fe0f4bf
4 changed files with 44 additions and 36 deletions

View File

@@ -277,6 +277,7 @@ json_object *json_segments_get_segment(json_object *jobj_segments, int segment);
int json_segments_count(json_object *jobj_segments); int json_segments_count(json_object *jobj_segments);
json_object *json_segments_get_segment_by_flag(json_object *jobj_segments, const char *flag); json_object *json_segments_get_segment_by_flag(json_object *jobj_segments, const char *flag);
void json_segment_remove_flag(json_object *jobj_segment, const char *flag); void json_segment_remove_flag(json_object *jobj_segment, const char *flag);
uint64_t json_segments_get_minimal_offset(json_object *jobj_segments, unsigned blockwise);
/* /*
* Generic LUKS2 digest * Generic LUKS2 digest

View File

@@ -54,6 +54,7 @@ json_object *LUKS2_get_token_jobj(struct luks2_hdr *hdr, int token);
json_object *LUKS2_get_digest_jobj(struct luks2_hdr *hdr, int digest); json_object *LUKS2_get_digest_jobj(struct luks2_hdr *hdr, int digest);
json_object *LUKS2_get_segment_jobj(struct luks2_hdr *hdr, int segment); json_object *LUKS2_get_segment_jobj(struct luks2_hdr *hdr, int segment);
json_object *LUKS2_get_tokens_jobj(struct luks2_hdr *hdr); json_object *LUKS2_get_tokens_jobj(struct luks2_hdr *hdr);
json_object *LUKS2_get_segments_jobj(struct luks2_hdr *hdr);
void hexprint_base64(struct crypt_device *cd, json_object *jobj, void hexprint_base64(struct crypt_device *cd, json_object *jobj,
const char *sep, const char *line_sep); const char *sep, const char *line_sep);

View File

@@ -174,6 +174,11 @@ json_object *LUKS2_get_segment_jobj(struct luks2_hdr *hdr, int segment)
return hdr ? json_segments_get_segment(json_get_segments_jobj(hdr->jobj), segment) : NULL; return hdr ? json_segments_get_segment(json_get_segments_jobj(hdr->jobj), segment) : NULL;
} }
json_object *LUKS2_get_segments_jobj(struct luks2_hdr *hdr)
{
return hdr ? json_get_segments_jobj(hdr->jobj) : NULL;
}
/* /*
* json_type_int needs to be validated first. * json_type_int needs to be validated first.
* See validate_json_uint32() * See validate_json_uint32()
@@ -251,34 +256,6 @@ json_object *json_contains(struct crypt_device *cd, json_object *jobj, const cha
return sobj; return sobj;
} }
/* use only on already validated 'segments' object */
static uint64_t get_first_data_offset(json_object *jobj_segs, const char *type)
{
json_object *jobj_offset, *jobj_type;
uint64_t tmp, min = UINT64_MAX;
json_object_object_foreach(jobj_segs, key, val) {
UNUSED(key);
if (type) {
json_object_object_get_ex(val, "type", &jobj_type);
if (strcmp(type, json_object_get_string(jobj_type)))
continue;
}
json_object_object_get_ex(val, "offset", &jobj_offset);
tmp = json_object_get_uint64(jobj_offset);
if (!tmp)
return tmp;
if (tmp < min)
min = tmp;
}
return min;
}
static json_bool validate_json_uint32(json_object *jobj) static json_bool validate_json_uint32(json_object *jobj)
{ {
int64_t tmp; int64_t tmp;
@@ -770,8 +747,7 @@ static int hdr_validate_config(struct crypt_device *cd, json_object *hdr_jobj)
* validate keyslots_size fits in between (2 * metadata_size) and first * validate keyslots_size fits in between (2 * metadata_size) and first
* segment_offset (except detached header) * segment_offset (except detached header)
*/ */
json_object_object_get_ex(hdr_jobj, "segments", &jobj); segment_offset = json_segments_get_minimal_offset(json_get_segments_jobj(hdr_jobj), 0);
segment_offset = get_first_data_offset(jobj, "crypt");
if (segment_offset && if (segment_offset &&
(segment_offset < keyslots_size || (segment_offset < keyslots_size ||
(segment_offset - keyslots_size) < (2 * metadata_size))) { (segment_offset - keyslots_size) < (2 * metadata_size))) {
@@ -1618,12 +1594,7 @@ int LUKS2_hdr_dump(struct crypt_device *cd, struct luks2_hdr *hdr)
uint64_t LUKS2_get_data_offset(struct luks2_hdr *hdr) uint64_t LUKS2_get_data_offset(struct luks2_hdr *hdr)
{ {
json_object *jobj1; return json_segments_get_minimal_offset(LUKS2_get_segments_jobj(hdr), 1);
if (!json_object_object_get_ex(hdr->jobj, "segments", &jobj1))
return 0;
return get_first_data_offset(jobj1, "crypt") / SECTOR_SIZE;
} }
const char *LUKS2_get_cipher(struct luks2_hdr *hdr, int segment) const char *LUKS2_get_cipher(struct luks2_hdr *hdr, int segment)

View File

@@ -21,6 +21,34 @@
#include "luks2_internal.h" #include "luks2_internal.h"
/* use only on already validated 'segments' object */
static uint64_t get_first_data_offset(json_object *jobj_segs, const char *type)
{
json_object *jobj_offset, *jobj_type;
uint64_t tmp, min = UINT64_MAX;
json_object_object_foreach(jobj_segs, key, val) {
UNUSED(key);
if (type) {
json_object_object_get_ex(val, "type", &jobj_type);
if (strcmp(type, json_object_get_string(jobj_type)))
continue;
}
json_object_object_get_ex(val, "offset", &jobj_offset);
tmp = json_object_get_uint64(jobj_offset);
if (!tmp)
return tmp;
if (tmp < min)
min = tmp;
}
return min;
}
json_object *json_get_segments_jobj(json_object *hdr_jobj) json_object *json_get_segments_jobj(json_object *hdr_jobj)
{ {
json_object *jobj_segments; json_object *jobj_segments;
@@ -31,6 +59,13 @@ json_object *json_get_segments_jobj(json_object *hdr_jobj)
return jobj_segments; return jobj_segments;
} }
uint64_t json_segments_get_minimal_offset(json_object *jobj_segments, unsigned blockwise)
{
if (!jobj_segments)
return 0;
return blockwise ? get_first_data_offset(jobj_segments, NULL) >> SECTOR_SHIFT : get_first_data_offset(jobj_segments, NULL);
}
uint64_t json_segment_get_offset(json_object *jobj_segment, unsigned blockwise) uint64_t json_segment_get_offset(json_object *jobj_segment, unsigned blockwise)
{ {
json_object *jobj; json_object *jobj;