1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
11 #include "common_util_log.h"
12 
13 /**
14  * @brief This function reads bytes from text file into provided buffer
15  *
16  * @param[in]
17  *
18  * @param[out]
19  *
20  * @return uint8_t -
21 
22  */
CC_CommonUtilCopyDataFromRawTextFile(uint8_t * fileName,uint8_t * outBuff,uint32_t * outBuffLen)23 int32_t CC_CommonUtilCopyDataFromRawTextFile (uint8_t *fileName, uint8_t *outBuff, uint32_t *outBuffLen)
24 {
25     int32_t status = 0;
26     FILE *fd;
27     int32_t actualFileLen=0;
28     size_t actualRead=0;
29     size_t maxBytesToRead = 0;
30 
31 
32     if ((NULL == fileName) ||
33         (NULL == outBuff) ||
34         (NULL == outBuffLen)) {
35         UTIL_LOG_ERR( "ilegal parameters for %s\n", __func__);
36         return 1;
37     }
38     if (0 == *outBuffLen) {
39         UTIL_LOG_ERR( "ilegal outBuffLen \n");
40         return 1;
41     }
42     fd = fopen(fileName, "rt");
43     if (NULL == fd) {
44         UTIL_LOG_ERR( "failed to open file %s for reading\n", fileName);
45         return 1;
46     }
47     memset(outBuff, 0, *outBuffLen);
48 
49     /* Get file length */
50     fseek(fd, 0, SEEK_END);
51     actualFileLen = ftell(fd);
52     if (actualFileLen == -1) {
53         UTIL_LOG_ERR( "ftell error actualFileLen == -1\n");
54         status = 1;
55         goto EXIT;
56     }
57     fseek(fd, 0, SEEK_SET);
58 
59     /* calculate max bytes to read. should be the min of bytes in file and buffer size*/
60     maxBytesToRead = (actualFileLen > (*outBuffLen))?(*outBuffLen):actualFileLen;
61     if (0 == maxBytesToRead) {
62         UTIL_LOG_ERR( "ilegal maxBytesToRead == 0\n");
63         status = 1;
64         goto EXIT;
65     }
66 
67     /* read file content */
68     actualRead = fread(outBuff, 1, maxBytesToRead, fd);
69 
70     while ((outBuff[actualRead-1] == ' ') ||
71            (outBuff[actualRead-1] == '\n') ||
72            (outBuff[actualRead-1] == '\0') ||
73            (outBuff[actualRead-1] == 0x0A) ||
74            (outBuff[actualRead-1] == 0x0D)) {
75         actualRead--;
76     }
77     *outBuffLen = actualRead;
78 
79     EXIT:
80     if (fd != NULL) {
81         fclose(fd);
82     }
83     return status;
84 }
85 
86 
87 /**
88  * @brief This function reads bytes from text file into provided buffer
89  *
90  * @param[in]
91  *
92  * @param[out]
93  *
94  * @return uint8_t -
95 
96  */
CC_CommonUtilCopyDataFromTextFile(uint8_t * fileName,uint8_t * outBuff,uint32_t * outBuffLen)97 int32_t CC_CommonUtilCopyDataFromTextFile (uint8_t *fileName, uint8_t *outBuff, uint32_t *outBuffLen)
98 {
99     #define NUM_OF_CHARS_FOR_BYTE 4
100     int32_t status = 0;
101     FILE *fd;
102     int32_t i = 0, j=0, k=0;
103     int32_t actualFileLen=0;
104     int32_t tempNum=0;
105     size_t actualRead=0;
106     size_t maxBytesToRead = 0;
107     int8_t *filebufptr = NULL;
108     int8_t str[NUM_OF_CHARS_FOR_BYTE+1];
109 
110 
111     if ((NULL == fileName) ||
112         (NULL == outBuff) ||
113         (NULL == outBuffLen)) {
114         UTIL_LOG_ERR( "ilegal parameters for %s\n", __func__);
115         return 1;
116     }
117     if (0 == *outBuffLen) {
118         UTIL_LOG_ERR( "ilegal outBuffLen \n");
119         return 1;
120     }
121     fd = fopen(fileName, "rt");
122     if (NULL == fd) {
123         UTIL_LOG_ERR( "failed to open file %s for reading\n", fileName);
124         return 1;
125     }
126     memset(outBuff, 0, *outBuffLen);
127 
128     /* Get file length */
129     fseek(fd, 0, SEEK_END);
130     actualFileLen = ftell(fd);
131     if (actualFileLen == -1) {
132         UTIL_LOG_ERR( "ftell error actualFileLen == -1\n");
133         status = 1;
134         goto EXIT;
135     }
136     fseek(fd, 0, SEEK_SET);
137 
138     /* calculate max bytes to read. should be the min of bytes in file and buffer size*/
139     maxBytesToRead = (actualFileLen > (*outBuffLen*5))?(*outBuffLen*5):actualFileLen;
140     if (0 == maxBytesToRead) {
141         UTIL_LOG_ERR( "ilegal maxBytesToRead == 0\n");
142         status = 1;
143         goto EXIT;
144     }
145 
146 
147     /* allocate buffer for data from file */
148     filebufptr = (int8_t*)malloc(maxBytesToRead+1);
149     if (filebufptr == NULL) {
150         UTIL_LOG_ERR( "failed to allocate memory\n");
151         status = 1;
152         goto EXIT;
153     }
154 
155     /* NULL terminated string to avoid buffer overflow of the sscanf that is used later */
156     filebufptr[maxBytesToRead] = '\0';
157 
158     /* read file content */
159     actualRead = fread(filebufptr, 1, maxBytesToRead, fd);
160     j=0;
161     k=0;
162     for (i=0; i<maxBytesToRead; i++) {
163         if (((filebufptr[i] >= '0') && (filebufptr[i] <= '9')) ||
164             ((filebufptr[i] >= 'a') && (filebufptr[i] <= 'f')) ||
165             ((filebufptr[i] >= 'A') && (filebufptr[i] <= 'F')) ||
166             (filebufptr[i] == 'x') || (filebufptr[i] == 'X') &&
167             (k<NUM_OF_CHARS_FOR_BYTE)) {
168             str[k++] = filebufptr[i];
169         } else {
170             if ((filebufptr[i] == ' ') ||
171                 (filebufptr[i] == '\n') ||
172                 (filebufptr[i] == '\0') ||
173                 (filebufptr[i] == ',')) {
174                 if (k>0) {
175                     str[k] = '\0';
176                     tempNum = strtol(str, NULL, 16);
177                     if ((LONG_MIN == tempNum) ||
178                         (LONG_MAX == tempNum)) {
179                         UTIL_LOG_ERR( "strtol failed. check file name %s\n", fileName);
180                         status = 1;
181                         goto EXIT_AND_FREE;
182                     }
183                     outBuff[j++] = tempNum;
184                     k = 0;
185                 }
186                 continue;
187             } else {
188                 UTIL_LOG_ERR( "ilegal uint8_t in file %c offset %d within file name %s\n", filebufptr[i], i, fileName);
189                 status = 1;
190                 goto EXIT_AND_FREE;
191             }
192         }
193     }
194     *outBuffLen = j;
195 
196     EXIT_AND_FREE:
197     if (filebufptr != NULL) {
198         free(filebufptr);
199     }
200     EXIT:
201     if (fd != NULL) {
202         fclose(fd);
203     }
204     return status;
205 }
206 
207 
208 /**
209  * @brief This function
210  *
211  * @param[in]
212  *
213  * @param[out]
214  *
215  * @return uint8_t -
216 
217  */
CC_CommonUtilCopyDataFromBinFile(uint8_t * fileName,uint8_t * outBuff,uint32_t * outBuffLen)218 int32_t CC_CommonUtilCopyDataFromBinFile(uint8_t *fileName, uint8_t *outBuff, uint32_t *outBuffLen)
219 {
220     int32_t rc = 0;
221     FILE *fd;
222     size_t actualRead = 0;
223     size_t actualFileLen = 0;
224 
225 
226     if ((NULL == fileName) ||
227         (NULL == outBuff) ||
228         (0 == *outBuffLen)) {
229         UTIL_LOG_ERR( "ilegal parameters for %s\n", __func__);
230         return 1;
231     }
232     UTIL_LOG_INFO( "opening %s\n", fileName);
233     fd = fopen(fileName, "rb");
234     if (NULL == fd) {
235         UTIL_LOG_ERR( "failed to open file %s for reading\n", fileName);
236         return 1;
237     }
238     /* Get file length */
239     fseek(fd, 0, SEEK_END);
240     actualFileLen=ftell(fd);
241     if (actualFileLen == -1)
242     {
243         UTIL_LOG_ERR( "failed to ftell file %s\n", fileName);
244         goto EXIT_AND_FREE;
245     }
246     fseek(fd, 0, SEEK_SET);
247     if (0 == actualFileLen) {
248         UTIL_LOG_ERR( "ilegal actualFileLen == 0\n");
249         rc = 3;
250         goto EXIT_AND_FREE;
251     }
252 
253     /* calculate max bytes to read. should be the min of bytes in file and buffer size*/
254     if (actualFileLen > *outBuffLen) {
255         UTIL_LOG_ERR( "ilegal actualFileLen %d > *outBuffLen %d\n", (int)actualFileLen, *outBuffLen);
256         rc = 2;
257         goto EXIT_AND_FREE;
258     }
259 
260     /* read file content */
261     actualRead = fread(outBuff, 1, actualFileLen, fd);
262     if (actualRead == 0)
263     {
264         UTIL_LOG_ERR( "failed to open fread %s for reading\n", fileName);
265         goto EXIT_AND_FREE;
266     }
267     if ((uint8_t)EOF == outBuff[actualRead-1]) {
268         actualRead--;
269     }
270     *outBuffLen = actualRead;
271 
272     EXIT_AND_FREE:
273     if (fd != NULL) {
274         fclose(fd);
275     }
276     return rc;
277 }
278 
279 
280 /**
281  * @brief This function
282  *
283  * @param[in]
284  *
285  * @param[out]
286  *
287  * @return uint8_t -
288 
289  */
CC_CommonUtilCopyBuffToBinFile(uint8_t * fileName,uint8_t * inBuff,uint32_t inBuffLen)290 int32_t CC_CommonUtilCopyBuffToBinFile(uint8_t *fileName, uint8_t *inBuff, uint32_t inBuffLen)
291 {
292     int32_t rc = 0;
293     int32_t actualWriten = 0;
294     FILE *fd;
295 
296 
297     if ((NULL == fileName) ||
298         (NULL == inBuff) ||
299         (0 == inBuffLen)) {
300         UTIL_LOG_ERR( "ilegal parameters for %s\n", __func__);
301         return 1;
302     }
303     fd = fopen(fileName, "wb");
304     if (NULL == fd) {
305         UTIL_LOG_ERR( "failed to open file %s for writing\n", fileName);
306         return 1;
307     }
308 
309     actualWriten = fwrite(inBuff, 1, inBuffLen, fd);
310     if (actualWriten != inBuffLen) {
311         UTIL_LOG_ERR( "failed to write data to file actual written %d, expected %d\n", actualWriten, inBuffLen);
312         rc = 1;
313     }
314 
315     if (fd != NULL) {
316         fclose(fd);
317     }
318     UTIL_LOG_ERR( "%d bytes were written %s\n", actualWriten, fileName);
319 
320 
321     return rc;
322 }
323 
324 
325 /**
326  * @brief The function reads the pwd file name gets the pwd and returns it
327  *
328  * @param[in] pPwdFileName - file name of the password
329  * @param[out] pwd - passphrase data
330  *
331  */
332 /*********************************************************/
CC_CommonGetPassphrase(int8_t * pPwdFileName,uint8_t ** pwd)333 int32_t CC_CommonGetPassphrase(int8_t *pPwdFileName, uint8_t **pwd)
334 {
335 
336     FILE *fp = NULL;
337     int32_t fsize = 0;
338     int32_t seek =0, i=0;
339     uint8_t *tmpBuf;
340     int32_t status = 0;
341 
342     if (pPwdFileName == NULL) {
343         UTIL_LOG_ERR("illegal file name\n");
344         return -1;
345     }
346 
347     if (pwd == NULL) {
348         UTIL_LOG_ERR("illegal pwd\n");
349         return -1;
350     }
351 
352     fp = fopen (pPwdFileName, "r");
353     if (fp == NULL) {
354         UTIL_LOG_ERR ("Cannot open file %s\n", pPwdFileName);
355         return -1;
356     }
357 
358 
359     /* Get the pwd file size */
360     seek = fseek(fp, 0, SEEK_END);
361     fsize = ftell(fp);
362     fseek(fp, 0, SEEK_SET);
363 
364     if (fsize == 0) {
365         UTIL_LOG_ERR("PWD file is empty!\n");
366         status = -1;
367         goto END;
368     }
369 
370     tmpBuf = (int8_t *)malloc(fsize+1);
371     if (tmpBuf == NULL) {
372         UTIL_LOG_ERR("failed to allocate memory\n");
373         status = -1;
374         goto END;
375     }
376 
377     memset(tmpBuf, 0, fsize+1);
378     /* get the file data */
379     for (i=0; i<fsize; i++) {
380         tmpBuf[i] = (uint8_t)fgetc(fp);
381         if (tmpBuf[i] == (uint8_t)EOF || tmpBuf[i] == '\n') {
382             tmpBuf[i] = '\0';
383         }
384     }
385     *pwd = tmpBuf;
386     status = 0;
387 
388     END:
389     fclose(fp);
390     return status;
391 }
392