post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
/home/travis/build/hasufell/pqc/src/ntru_file.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 FH Bielefeld
00003  *
00004  * This file is part of a FH Bielefeld project.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019  * MA  02110-1301  USA
00020  */
00021 
00029 #include "ntru_common.h"
00030 #include "ntru_err.h"
00031 #include "ntru_mem.h"
00032 #include "ntru_string.h"
00033 
00034 #include <fcntl.h>
00035 #include <stdbool.h>
00036 #include <stdint.h>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <sys/stat.h>
00041 #include <sys/types.h>
00042 #include <unistd.h>
00043 
00044 #define STD_FILE_BUF 4096
00045 
00046 
00047 /*------------------------------------------------------------------------*/
00048 
00049 string *
00050 read_file(char const * const filename)
00051 {
00052     char buf[STD_FILE_BUF],
00053          *cstring = NULL;
00054     int fd = 0;
00055     size_t str_size = 0;
00056     ssize_t n;
00057     size_t file_length = 0;
00058     string *result_string;
00059 
00060     fd = open(filename, O_RDONLY);
00061     file_length = lseek(fd, 0, SEEK_END) + 1;
00062     lseek(fd, 0, SEEK_SET);
00063 
00064     cstring = malloc(sizeof(char) * file_length);
00065 
00066     if (fd != -1) {
00067         /* read and copy chunks */
00068         while ((n = read(fd, buf, STD_FILE_BUF)) != 0) {
00069 
00070             if (n == -1) {
00071                 NTRU_WARN_DEBUG("Failed while reading file descriptor %d",
00072                         fd);
00073                 goto failure_cleanup;
00074             }
00075 
00076             str_size += n; /* count total bytes read */
00077 
00078             /* append buffer to string */
00079             memcpy(cstring + (str_size - n), buf, (size_t)n);
00080         }
00081         /* add trailing NULL byte */
00082         cstring[str_size] = '\0';
00083 
00084         if (close(fd)) {
00085             NTRU_WARN_DEBUG("Failed to close file descripter %d\n", fd);
00086             goto failure_cleanup;
00087         }
00088 
00089         result_string = ntru_malloc(sizeof(*result_string));
00090         result_string->ptr = cstring;
00091         result_string->len = str_size;
00092 
00093         return result_string;
00094 
00095     } else {
00096         return NULL;
00097     }
00098 
00099 failure_cleanup:
00100     free(cstring);
00101     return NULL;
00102 }
00103 
00104 /*------------------------------------------------------------------------*/
00105 
00106 bool
00107 write_file(string const *wstring, char const * const filename)
00108 {
00109     FILE *fp;
00110 
00111     fp = fopen(filename, "w");
00112 
00113     if (!fp) {
00114         NTRU_WARN_DEBUG("Failed while creating file\n");
00115         return false;
00116     }
00117 
00118     for (uint32_t i = 0; i < wstring->len; i++)
00119         fprintf(fp, "%c", wstring->ptr[i]);
00120 
00121     if (fclose(fp)) {
00122         NTRU_WARN_DEBUG("Failed to close file descripter\n");
00123         return false;
00124     }
00125 
00126     return true;
00127 }
00128 
00129 /*------------------------------------------------------------------------*/
 All Data Structures Files Functions Variables Typedefs Defines