Mercurial > trustbridge
comparison ui/listutil.c @ 4:9849250f50f2
Start implementation of certificatelist parser
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 11 Feb 2014 16:46:02 +0000 |
parents | |
children | 992c0ec57660 |
comparison
equal
deleted
inserted
replaced
3:b684e25adbbb | 4:9849250f50f2 |
---|---|
1 #include "listutil.h" | |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <errno.h> | |
6 #include <fcntl.h> | |
7 #include <unistd.h> | |
8 #include <sys/types.h> | |
9 #include <sys/stat.h> | |
10 | |
11 #define MAX_FILESIZE_KB 1024 | |
12 | |
13 void handle_errno() | |
14 { | |
15 printf("Error: %s \n", strerror(errno)); | |
16 } | |
17 | |
18 list_status_t readList(char *fileName, void *data, size_t *size) | |
19 { | |
20 int fd = -1; | |
21 struct stat fileStat; | |
22 int rc = 0; | |
23 ssize_t bRead = 0; | |
24 list_status_t retval = UnknownError; | |
25 | |
26 fd = open(fileName, O_RDONLY); | |
27 if (fd == -1) { | |
28 handle_errno(); | |
29 retval = StatFailed; | |
30 goto failure; | |
31 } | |
32 | |
33 rc = fstat(fd, &fileStat); | |
34 if (rc < 0) { | |
35 printf ("Stat failed with rc: %i\n", rc); | |
36 retval = StatFailed; | |
37 goto failure; | |
38 } | |
39 | |
40 // Check the size of the file | |
41 if (fileStat.st_size) { | |
42 printf("Size zero\n"); | |
43 retval = StatFailed; | |
44 goto failure; | |
45 } | |
46 | |
47 if (fileStat.st_size / 1024 > MAX_FILESIZE_KB || | |
48 fileStat.st_size >= (size_t)-1) { | |
49 printf("File too large\n"); | |
50 retval = TooLarge; | |
51 goto failure; | |
52 } | |
53 | |
54 // We can cast here as we already checked | |
55 // that this size is not too large for size_t | |
56 *size = (size_t) fileStat.st_size; | |
57 | |
58 data = malloc(*size); | |
59 | |
60 if (data == NULL) { | |
61 printf("Malloc failed\n"); | |
62 retval = UnknownError; | |
63 goto failure; | |
64 } | |
65 | |
66 bRead = read(fd, data, *size); | |
67 | |
68 if (bRead != *size) { | |
69 printf("Read failed"); | |
70 if (bRead == -1) { | |
71 handle_errno(); | |
72 } | |
73 retval = UnknownError; | |
74 *size = 0; | |
75 if (data) { | |
76 free(data); | |
77 data = NULL; | |
78 } | |
79 goto failure; | |
80 } | |
81 | |
82 retval = Unknown; | |
83 failure: | |
84 | |
85 if (fd && fd != -1) { | |
86 close(fd); | |
87 fd = -1; | |
88 } | |
89 | |
90 return retval; | |
91 } | |
92 | |
93 list_status_t readAndVerifyList(char *fileName, void *data, size_t *size) | |
94 { | |
95 int validSig = 0; | |
96 char * firstChar = NULL; | |
97 int i = 0; | |
98 | |
99 list_status_t retval = UnknownError; | |
100 data = NULL; | |
101 size = NULL; | |
102 | |
103 retval = readList(fileName, data, size); | |
104 | |
105 if (retval != Unknown) { | |
106 return retval; | |
107 } | |
108 | |
109 if (!data || !size) { | |
110 // should not have happend if readList works as specified | |
111 return UnknownError; | |
112 } | |
113 | |
114 firstChar = (char*) data; | |
115 | |
116 if (*firstChar != 'S') { | |
117 printf("Does not start with S\n"); | |
118 retval = InvalidFormat; | |
119 } | |
120 | |
121 for (i=0; i < *size; i++) { | |
122 printf("%c", firstChar + i); | |
123 } | |
124 | |
125 failure: | |
126 if (retval != Valid && data) { | |
127 free(data); | |
128 data = NULL; | |
129 *size = 0; | |
130 } | |
131 return retval; | |
132 } | |
133 |