693044d45e97bef79e967980c893f8bf320e8c88
[platform/core/security/yaca.git] / examples / misc.c
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file misc.c
21  * @brief
22  */
23
24 #define _POSIX_C_SOURCE 200809L
25
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <string.h>
29
30 #include <openssl/bio.h>
31
32 #include <yaca_crypto.h>
33 #include <yaca_error.h>
34
35 #include "misc.h"
36
37 void dump_hex(const char *buf, size_t dump_size, const char *fmt, ...)
38 {
39         va_list ap;
40
41         va_start(ap, fmt);
42         vprintf(fmt, ap);
43         va_end(ap);
44         putchar('\n');
45         BIO_dump_fp(stdout, buf, dump_size);
46 }
47
48 void debug_func(const char *buf)
49 {
50         puts(buf);
51 }
52
53 int write_file(const char *path, char *data, size_t data_len)
54 {
55         size_t written = 0;
56         FILE *f;
57
58         f = fopen(path, "w");
59         if (f == NULL)
60                 return -1;
61
62         while (written != data_len) {
63                 int ret = fwrite(data + written, 1, data_len - written, f);
64
65                 if (ferror(f) != 0) {
66                         fclose(f);
67                         return -1;
68                 }
69
70                 written += ret;
71         }
72
73         fclose(f);
74         return 0;
75 }
76
77 #define BUF_SIZE 512
78
79 int read_file(const char *path, char **data, size_t *data_len)
80 {
81         int ret;
82         char tmp[BUF_SIZE];
83         char *buf = NULL;
84         size_t buf_len = 0;
85         FILE *f;
86
87         f = fopen(path, "r");
88         if (f == NULL)
89                 return -1;
90
91         for (;;) {
92                 size_t read = fread(tmp, 1, BUF_SIZE, f);
93
94                 if (read > 0) {
95                         if (yaca_realloc(buf_len + read, (void**)&buf) != YACA_ERROR_NONE) {
96                                 ret = -1;
97                                 break;
98                         }
99
100                         memcpy(buf + buf_len, tmp, read);
101                         buf_len += read;
102                 }
103
104                 if (ferror(f) != 0) {
105                         ret = -1;
106                         break;
107                 }
108
109                 if (feof(f)) {
110                         *data = buf;
111                         *data_len = buf_len;
112                         buf = NULL;
113                         ret = 0;
114                         break;
115                 }
116         }
117
118         fclose(f);
119         free(buf);
120         return ret;
121 }
122
123 int read_stdin_line(const char *prompt, char **string)
124 {
125         char *buf = NULL;
126         size_t size;
127         ssize_t read;
128
129         if (prompt != NULL)
130                 printf("%s", prompt);
131
132         read = getline(&buf, &size, stdin);
133         if (read <= 0) {
134                 free(buf);
135                 return -1;
136         }
137
138         if (yaca_realloc(read, (void**)&buf) != YACA_ERROR_NONE) {
139                 free(buf);
140                 return -1;
141         }
142         buf[read - 1] = '\0';
143
144         *string = buf;
145         return 0;
146 }