post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_file.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 FH Bielefeld
3  *
4  * This file is part of a FH Bielefeld project.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21 
29 #include "ntru_common.h"
30 #include "ntru_err.h"
31 #include "ntru_mem.h"
32 #include "ntru_string.h"
33 
34 #include <fcntl.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 
44 #define STD_FILE_BUF 4096
45 
46 
47 /*------------------------------------------------------------------------*/
48 
49 string *
50 read_file(char const * const filename)
51 {
52  char buf[STD_FILE_BUF],
53  *cstring = NULL;
54  int fd = 0;
55  size_t str_size = 0;
56  ssize_t n;
57  size_t file_length = 0;
58  string *result_string;
59 
60  fd = open(filename, O_RDONLY);
61  file_length = lseek(fd, 0, SEEK_END) + 1;
62  lseek(fd, 0, SEEK_SET);
63 
64  cstring = malloc(sizeof(char) * file_length);
65 
66  if (fd != -1) {
67  /* read and copy chunks */
68  while ((n = read(fd, buf, STD_FILE_BUF)) != 0) {
69 
70  if (n == -1) {
71  NTRU_WARN_DEBUG("Failed while reading file descriptor %d",
72  fd);
73  goto failure_cleanup;
74  }
75 
76  str_size += n; /* count total bytes read */
77 
78  /* append buffer to string */
79  memcpy(cstring + (str_size - n), buf, (size_t)n);
80  }
81  /* add trailing NULL byte */
82  cstring[str_size] = '\0';
83 
84  if (close(fd)) {
85  NTRU_WARN_DEBUG("Failed to close file descripter %d\n", fd);
86  goto failure_cleanup;
87  }
88 
89  result_string = ntru_malloc(sizeof(*result_string));
90  result_string->ptr = cstring;
91  result_string->len = str_size;
92 
93  return result_string;
94 
95  } else {
96  return NULL;
97  }
98 
99 failure_cleanup:
100  free(cstring);
101  return NULL;
102 }
103 
104 /*------------------------------------------------------------------------*/
105 
106 bool
107 write_file(string const *wstring, char const * const filename)
108 {
109  FILE *fp;
110 
111  fp = fopen(filename, "w");
112 
113  if (!fp) {
114  NTRU_WARN_DEBUG("Failed while creating file\n");
115  return false;
116  }
117 
118  for (uint32_t i = 0; i < wstring->len; i++)
119  fprintf(fp, "%c", wstring->ptr[i]);
120 
121  if (fclose(fp)) {
122  NTRU_WARN_DEBUG("Failed to close file descripter\n");
123  return false;
124  }
125 
126  return true;
127 }
128 
129 /*------------------------------------------------------------------------*/
size_t len
Definition: ntru_string.h:53
bool write_file(string const *wstring, char const *const filename)
Definition: ntru_file.c:107
header for ntru_mem.c
#define STD_FILE_BUF
Definition: ntru_file.c:44
string * read_file(char const *const filename)
Definition: ntru_file.c:50
common macros/functions
#define NTRU_WARN_DEBUG(...)
Definition: ntru_err.h:52
error handling
void * ntru_malloc(size_t size)
Definition: ntru_mem.c:38
header for ntru_string.c
char * ptr
Definition: ntru_string.h:49