aboutsummaryrefslogtreecommitdiff
path: root/src/cli/util/gnunet-base32.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/util/gnunet-base32.c')
-rw-r--r--src/cli/util/gnunet-base32.c155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/cli/util/gnunet-base32.c b/src/cli/util/gnunet-base32.c
new file mode 100644
index 000000000..209741740
--- /dev/null
+++ b/src/cli/util/gnunet-base32.c
@@ -0,0 +1,155 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/gnunet-base32.c
23 * @brief tool to encode/decode from/to the Crockford Base32 encoding GNUnet uses
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30
31/**
32 * The main function of gnunet-base32
33 *
34 * @param argc number of arguments from the command line
35 * @param argv command line arguments
36 * @return 0 ok, 1 on error
37 */
38int
39main (int argc,
40 char *const *argv)
41{
42 int decode = 0;
43 const struct GNUNET_GETOPT_CommandLineOption options[] = {
44 GNUNET_GETOPT_option_flag ('d',
45 "decode",
46 gettext_noop (
47 "run decoder modus, otherwise runs as encoder"),
48 &decode),
49 GNUNET_GETOPT_option_help ("Crockford base32 encoder/decoder"),
50 GNUNET_GETOPT_option_version (PACKAGE_VERSION),
51 GNUNET_GETOPT_OPTION_END
52 };
53 int ret;
54 char *in;
55 unsigned int in_size;
56 ssize_t iret;
57 char *out;
58 size_t out_size;
59
60 if (GNUNET_OK !=
61 GNUNET_STRINGS_get_utf8_args (argc, argv,
62 &argc, &argv))
63 return 2;
64 ret = GNUNET_GETOPT_run ("gnunet-base32",
65 options,
66 argc,
67 argv);
68 if (ret < 0)
69 return 1;
70 if (0 == ret)
71 return 0;
72 in_size = 0;
73 in = NULL;
74 iret = 1;
75 while (iret > 0)
76 {
77 /* read in blocks of 4k */
78 char buf[4092];
79
80 iret = read (0,
81 buf,
82 sizeof (buf));
83 if (iret < 0)
84 {
85 GNUNET_free (in);
86 return 2;
87 }
88 if (iret > 0)
89 {
90 if (iret + in_size < in_size)
91 {
92 GNUNET_break (0);
93 GNUNET_free (in);
94 return 1;
95 }
96 GNUNET_array_grow (in,
97 in_size,
98 in_size + iret);
99 memcpy (&in[in_size - iret],
100 buf,
101 iret);
102 }
103 }
104 if (decode)
105 {
106 /* This formula can overestimate by 1 byte, so we try both
107 out_size and out_size-1 below */
108 out_size = in_size * 5 / 8;
109 out = GNUNET_malloc (out_size);
110 if ( (GNUNET_OK !=
111 GNUNET_STRINGS_string_to_data (in,
112 in_size,
113 out,
114 out_size)) &&
115 (out_size > 0) )
116 {
117 out_size--;
118 if (GNUNET_OK !=
119 GNUNET_STRINGS_string_to_data (in,
120 in_size,
121 out,
122 out_size))
123 {
124 GNUNET_free (out);
125 GNUNET_free (in);
126 return 3;
127 }
128 }
129 }
130 else
131 {
132 out = GNUNET_STRINGS_data_to_string_alloc (in,
133 in_size);
134 out_size = strlen (out);
135 }
136 {
137 size_t pos = 0;
138
139 while (pos < out_size)
140 {
141 iret = write (1,
142 &out[pos],
143 out_size - pos);
144 if (iret <= 0)
145 return 4;
146 pos += iret;
147 }
148 }
149 GNUNET_free (out);
150 GNUNET_free_nz ((void *) argv);
151 return 0;
152}
153
154
155/* end of gnunet-uri.c */