src/file_class.c: add frama-c annotations

This commit is contained in:
Christophe Grenier 2021-02-14 15:21:25 +01:00
parent f2515294ab
commit 1ef4dc4b3e

View file

@ -32,8 +32,8 @@
#include "common.h" #include "common.h"
#include "filegen.h" #include "filegen.h"
/*@ requires \valid(file_stat); */
static void register_header_check_class(file_stat_t *file_stat); static void register_header_check_class(file_stat_t *file_stat);
static int header_check_class(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new);
const file_hint_t file_hint_class= { const file_hint_t file_hint_class= {
.extension="class", .extension="class",
@ -44,9 +44,6 @@ const file_hint_t file_hint_class= {
.register_header_check=&register_header_check_class .register_header_check=&register_header_check_class
}; };
/* http://en.wikipedia.org/wiki/Class_(file_format) */
static const unsigned char class_magic[4]= { 0xCA, 0xFE, 0xBA, 0xBE };
struct class_header { struct class_header {
uint32_t magic_number; uint32_t magic_number;
uint16_t minor_version; uint16_t minor_version;
@ -54,11 +51,17 @@ struct class_header {
uint16_t constant_pool_count; uint16_t constant_pool_count;
}; };
static void register_header_check_class(file_stat_t *file_stat) /*@
{ @ requires buffer_size >= sizeof(struct class_header);
register_header_check(0, class_magic, sizeof(class_magic), &header_check_class, file_stat); @ requires \valid_read(buffer+(0..buffer_size-1));
} @ requires valid_file_recovery(file_recovery);
@ requires \valid(file_recovery_new);
@ requires file_recovery_new->blocksize > 0;
@ requires separation: \separated(&file_hint_class, buffer+(..), file_recovery, file_recovery_new);
@ assigns *file_recovery_new;
@ ensures \result == 0 || \result == 1;
@ ensures \result!=0 ==> valid_file_recovery(file_recovery_new);
@*/
static int header_check_class(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new) static int header_check_class(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{ {
const struct class_header *cafe=(const struct class_header *)buffer; const struct class_header *cafe=(const struct class_header *)buffer;
@ -72,4 +75,11 @@ static int header_check_class(const unsigned char *buffer, const unsigned int bu
} }
return 0; return 0;
} }
static void register_header_check_class(file_stat_t *file_stat)
{
/* https://en.wikipedia.org/wiki/Class_(file_format) */
static const unsigned char class_magic[4]= { 0xCA, 0xFE, 0xBA, 0xBE };
register_header_check(0, class_magic, sizeof(class_magic), &header_check_class, file_stat);
}
#endif #endif