Commit 376bd657 authored by Per Cederqvist's avatar Per Cederqvist

patch text-stat-sha256.patch

parent 94b5710a
import-nettle
use-nettle
From: Per Cederqvist <ceder@lysator.liu.se>
Subject: patch use-nettle.patch
Subject: patch text-stat-sha256.patch
......@@ -236,6 +236,9 @@ typedef struct {
/* Where the text can be found. */
struct generation_position text_store;
/* Checksum of the text mass. This is undefined for generation 0. */
unsigned char sha256_sum[256/8];
Pers_no author;
unsigned short no_of_lines;
String_size no_of_chars;
......
......@@ -415,6 +415,8 @@ cached_flush_text(Text_no text_no,
text_arr[ text_no ]->text_store.generation = 0;
text_arr[ text_no ]->text_store.file_pos = ftell(new_text_file);
text_arr[ text_no ]->text_store.file_size = 0;
memset(text_arr[text_no]->sha256_sum, 0,
sizeof(text_arr[text_no]->sha256_sum));
if ( fwrite(message.string, sizeof(char), message.len, new_text_file)
!= (size_t)message.len )
......
......@@ -611,6 +611,7 @@ copy_text_stat(const Text_stat *t)
c->text_store.generation = t->text_store.generation;
c->text_store.file_pos = t->text_store.file_pos;
c->text_store.file_size = t->text_store.file_size;
memcpy(c->sha256_sum, t->sha256_sum, sizeof(t->sha256_sum));
c->author = t->author;
c->no_of_lines = t->no_of_lines;
c->no_of_chars = t->no_of_chars;
......@@ -631,6 +632,7 @@ init_text_stat (Text_stat *t)
t->text_store.generation = 0;
t->text_store.file_pos = 0;
t->text_store.file_size = 0;
memset(t->sha256_sum, 0, sizeof(t->sha256_sum));
t->author = 0;
t->no_of_lines = 0;
t->no_of_chars = 0;
......
......@@ -315,6 +315,35 @@ void foutput_conference(struct dbfile *fp,
}
static unsigned char
bin2hex(int bin)
{
if (bin < 10)
return bin + '0';
else
return bin - 10 + 'a';
}
void
foutput_sha256_sum(const unsigned char *sha256,
struct dbfile *fp)
{
String hashval = EMPTY_STRING;
int ix;
if (s_size_crea_str(&hashval, 256/4) != OK)
restart_kom("Out of memory in foutput_sha256_sum\n.");
for (ix = 0; ix < 256/8; ix++)
{
hashval.string[2*ix] = bin2hex((sha256[ix] & 0xf0) >> 4);
hashval.string[2*ix+1] = bin2hex(sha256[ix] & 0x0f);
}
foutput_string(fp, s_fcrea_str("SHA-256"));
foutput_string(fp, hashval);
s_clear(&hashval);
}
static void
foutput_text_stat_0(struct dbfile *fp,
......@@ -383,6 +412,15 @@ foutput_text_stat_3(struct dbfile *fp,
foutput_ulong((unsigned long) t_stat->text_store.generation, fp);
foutput_ulong((unsigned long) t_stat->text_store.file_pos, fp);
foutput_ulong((unsigned long) t_stat->text_store.file_size, fp);
if (t_stat->text_store.generation > 0)
foutput_sha256_sum(t_stat->sha256_sum, fp);
else
{
foutput_string(fp, s_fcrea_str("unknown"));
foutput_string(fp, s_fcrea_str(""));
}
foutput_ulong((unsigned long) t_stat->no_of_lines, fp);
foutput_ulong((unsigned long) t_stat->no_of_chars, fp);
foutput_ulong((unsigned long) t_stat->no_of_marks, fp);
......
......@@ -62,3 +62,6 @@ foutput_atsign(struct dbfile *fp);
extern void
foutput_newline(struct dbfile *fp);
extern void
foutput_sha256_sum(const unsigned char *sha256,
struct dbfile *fp);
......@@ -40,6 +40,7 @@
#include "timewrap.h"
#include <sys/types.h>
#include <errno.h>
#include <assert.h>
#include "s-string.h"
#include "kom-types.h"
......@@ -1032,6 +1033,101 @@ fparse_mark_list(struct dbfile *fp,
return OK;
}
static Success
hex2dec(int hexdigit,
int *res)
{
if (hexdigit >= '0' && hexdigit <= '9')
*res = hexdigit - '0';
else if (hexdigit >= 'a' && hexdigit <= 'f')
*res = hexdigit - 'a' + 10;
else
return FAILURE;
return OK;
}
Success
fparse_sha256_sum(struct dbfile *fp,
unsigned char *sha256_sum)
{
String hashtype = EMPTY_STRING;
String hashval = EMPTY_STRING;
Success retval = OK;
int ix;
int hi;
int lo;
if (fparse_string(fp, &hashtype) != OK)
retval = FAILURE;
if (retval == OK && s_strcmp(hashtype, s_fcrea_str("SHA-256")) != 0)
{
kom_log("Unsupported hash type found in db file.\n");
retval = FAILURE;
}
if (fparse_string(fp, &hashval) != OK)
retval = FAILURE;
if (retval == OK && s_strlen(hashval) != 256/4)
{
kom_log("Broken sha256 hash found in db file: got bad length %lu.\n",
(unsigned long)s_strlen(hashval));
retval = FAILURE;
}
for (ix = 0; retval == OK && ix < 256/8; ++ix)
{
assert(2*ix <= s_strlen(hashval));
if (hex2dec(hashval.string[2*ix], &hi) != OK
|| hex2dec(hashval.string[2*ix+1], &lo) != OK)
{
kom_log("Bad hash digit found in db file.\n");
retval = FAILURE;
}
sha256_sum[ix] = 16 * hi + lo;
}
s_clear(&hashtype);
s_clear(&hashval);
return retval;
}
static Success
fparse_no_sum(struct dbfile *fp)
{
Success retval = OK;
String hashtype = EMPTY_STRING;
String hashval = EMPTY_STRING;
if (fparse_string(fp, &hashtype) != OK)
retval = FAILURE;
if (retval == OK && s_strcmp(hashtype, s_fcrea_str("unknown")) != 0)
{
kom_log("Unsupported hash type found in db file.\n");
retval = FAILURE;
}
if (fparse_string(fp, &hashval) != OK)
retval = FAILURE;
if (retval == OK && s_strlen(hashval) != 0)
{
kom_log("Broken hash found in db file: got bad length %lu.\n",
(unsigned long)s_strlen(hashval));
retval = FAILURE;
}
s_clear(&hashtype);
s_clear(&hashval);
return retval;
}
static Success
fparse_text_stat_3(struct dbfile *fp,
Text_stat *result)
......@@ -1052,6 +1148,16 @@ fparse_text_stat_3(struct dbfile *fp,
result->text_store.generation = fparse_long(fp);
result->text_store.file_pos = fparse_long(fp);
result->text_store.file_size = fparse_long(fp);
if (result->text_store.generation == 0)
{
if (fparse_no_sum(fp) != OK)
return FAILURE;
}
else
{
if (fparse_sha256_sum(fp, result->sha256_sum) != OK)
return FAILURE;
}
result->no_of_lines = fparse_long(fp);
result->no_of_chars = fparse_long(fp);
result->no_of_marks = fparse_long(fp);
......@@ -1170,6 +1276,7 @@ fparse_text_stat_2(struct dbfile *fp,
result->text_store.generation = 0;
result->text_store.file_pos = fparse_long(fp);
result->text_store.file_size = 0;
memset(result->sha256_sum, 0, sizeof(result->sha256_sum));
result->no_of_lines = fparse_long(fp);
result->no_of_chars = fparse_long(fp);
result->no_of_marks = fparse_long(fp);
......@@ -1288,6 +1395,7 @@ fparse_text_stat_0(struct dbfile *fp,
result->text_store.generation = 0;
result->text_store.file_pos = fparse_long(fp);
result->text_store.file_size = 0;
memset(result->sha256_sum, 0, sizeof(result->sha256_sum));
result->no_of_lines = fparse_long(fp);
result->no_of_chars = fparse_long(fp);
result->no_of_marks = fparse_long(fp);
......
......@@ -66,6 +66,10 @@ fparse_mark_list(struct dbfile *fp,
Mark_list *result);
extern Success
fparse_sha256_sum(struct dbfile *fp,
unsigned char *sha256_sum);
extern Success
fparse_text_stat(struct dbfile *fp,
Text_stat *result);
......
......@@ -1208,6 +1208,7 @@ cached_create_text(const String message)
((Text_stat *)node->ptr)->no_of_chars = 0;
((Text_stat *)node->ptr)->text_store.generation = 0;
((Text_stat *)node->ptr)->text_store.file_pos = file_pos;
compute_sha256_sum(((Text_stat *)node->ptr)->sha256_sum, message);
text_set_mru( tno );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment