FILE: add file subsystem and move string to ntru_string subs.

This commit is contained in:
hasufell 2014-05-26 21:30:42 +02:00
parent 7a5b899825
commit 4dbfe1e663
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
12 changed files with 248 additions and 25 deletions

View File

@ -37,8 +37,9 @@ endif
LIBS += -L. -lgmp -lmpfr -lm
# objects
PQC_OBJS = poly.o mem.o encrypt.o decrypt.o keypair.o ascii_poly.o
PQC_HEADERS = err.h poly.h context.h encrypt.h decrypt.h keypair.h ascii_poly.h common.h
PQC_OBJS = poly.o mem.o encrypt.o decrypt.o keypair.o ascii_poly.o file.o
PQC_HEADERS = err.h poly.h context.h encrypt.h decrypt.h keypair.h \
ascii_poly.h common.h file.h ntru_string.h
# CUNIT_OBJS = cunit.o
# includes

View File

@ -26,11 +26,12 @@
* @brief asci->poly and poly->ascii
*/
#include "ascii_poly.h"
#include "common.h"
#include "context.h"
#include "mem.h"
#include "ntru_string.h"
#include "poly.h"
#include "ascii_poly.h"
#include <stdint.h>
#include <stdlib.h>

View File

@ -31,6 +31,7 @@
#include "common.h"
#include "context.h"
#include "ntru_string.h"
#include <fmpz_poly.h>
#include <fmpz.h>

View File

@ -38,23 +38,4 @@
#define ASCII_BITS 8
typedef struct string string;
/**
* Represents a string.
*/
struct string {
/**
* Pointer to the char array,
* holding the actual string.
*/
char *ptr;
/**
* Length of the string.
*/
size_t len;
};
#endif /* NTRU_COMMON_H */

View File

@ -28,6 +28,7 @@
#include "ascii_poly.h"
#include "decrypt.h"
#include "ntru_string.h"
#include <string.h>

View File

@ -28,8 +28,8 @@
#ifndef NTRU_DECRYPT_H
#define NTRU_DECRYPT_H
#include "common.h"
#include "context.h"
#include "ntru_string.h"
#include "poly.h"
#include <fmpz_poly.h>

View File

@ -29,6 +29,7 @@
#include "ascii_poly.h"
#include "encrypt.h"
#include "mem.h"
#include "ntru_string.h"
#include <string.h>

View File

@ -29,8 +29,8 @@
#define PQC_ENCRYPT_H
#include "common.h"
#include "context.h"
#include "ntru_string.h"
#include "poly.h"
#include <fmpz_poly.h>
@ -68,7 +68,7 @@ ntru_encrypt_poly(
ntru_context *ctx);
/**
* Encrypt a message int he form of a null-terminated char array and
* Encrypt a message in the form of a null-terminated char array and
* return a string.
*
* @param msg the message

107
src/file.c Normal file
View File

@ -0,0 +1,107 @@
/*
* Copyright (C) 2014 FH Bielefeld
*
* This file is part of a FH Bielefeld project.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
/**
* @file file.c
* Allows operations of files, such as reading
* and writing.
* @brief file operations
*/
#include "common.h"
#include "err.h"
#include "mem.h"
#include "ntru_string.h"
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define STD_FILE_BUF 4096
string *
read_file(char const * const filename)
{
char buf[STD_FILE_BUF],
*cstring = NULL;
int fd = 0;
size_t str_size = 0;
ssize_t n;
string *result_string;
fd = open(filename, O_RDONLY);
if (fd != -1) {
/* read and copy chunks */
while ((n = read(fd, buf, STD_FILE_BUF)) != 0) {
if (n == -1)
NTRU_ABORT("Failed while reading file descriptor %d\n", fd);
str_size += n; /* count total bytes read */
REALLOC( /* allocate correct size */
cstring, /* pointer to realloc */
str_size /* total bytes read */
+ 1); /* space for trailing NULL byte */
/* append buffer to string */
memcpy(cstring + (str_size - n), buf, (size_t)n);
}
/* add trailing NULL byte */
cstring[str_size] = '\0';
if (close(fd))
NTRU_ABORT("Failed to close file descripter %d\n", fd);
result_string = ntru_malloc(sizeof(*result_string));
result_string->ptr = cstring;
result_string->len = str_size;
return result_string;
} else {
return NULL;
}
}
void
write_file(string const *file_content, char const * const filename)
{
FILE *fp;
fp = fopen(filename, "w");
if (!fp)
NTRU_ABORT("Failed while creating file\n");
for (uint32_t i = 0; i < file_content->len; i++)
fprintf(fp, "%c", file_content->ptr[i]);
fclose(fp);
}

49
src/file.h Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2014 FH Bielefeld
*
* This file is part of a FH Bielefeld project.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
/**
* @file file.h
* Header for the external API of file.c.
* @brief header for file.c
*/
#ifndef NTRU_FILE_H
#define NTRU_FILE_H
#include "common.h"
/**
* Reads a file and returns a newly allocated string.
*
* @param filename file to open
* @return a newly allocated string which must be freed by the caller
* or NULL on failure
*/
string *
read_file(char const * const filename);
void
write_file(string const *file_content, char const * const filename);
#endif /* NTRU_FILE_H */

27
src/ntru_string.c Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2014 FH Bielefeld
*
* This file is part of a FH Bielefeld project.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
/**
* @file ntru_string.c
* Sets up a string type that does not suck
* like C strings and provides operations on it.
* @brief string type and operations
*/

54
src/ntru_string.h Normal file
View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2014 FH Bielefeld
*
* This file is part of a FH Bielefeld project.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
/**
* @file ntru_string.h
* Header for the external API of ntru_string.c.
* @brief header for ntru_string.c
*/
#ifndef NTRU_STRING_H
#define NTRU_STRING_H
#include <stdlib.h>
typedef struct string string;
/**
* Represents a string.
*/
struct string {
/**
* Pointer to the char array,
* holding the actual string.
*/
char *ptr;
/**
* Length of the string.
*/
size_t len;
};
#endif /* NTRU_STRING_H */