GNUnet  0.11.x
gnunet-base32.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 
29 
37 int
38 main (int argc,
39  char *const *argv)
40 {
41  int decode = 0;
42  const struct GNUNET_GETOPT_CommandLineOption options[] = {
44  "decode",
45  gettext_noop ("run decoder modus, otherwise runs as encoder"),
46  &decode),
47  GNUNET_GETOPT_option_help ("Crockford base32 encoder/decoder"),
48  GNUNET_GETOPT_option_version (PACKAGE_VERSION),
50  };
51  int ret;
52  char *in;
53  unsigned int in_size;
54  ssize_t iret;
55  char *out;
56  size_t out_size;
57 
58  if (GNUNET_OK !=
59  GNUNET_STRINGS_get_utf8_args (argc, argv,
60  &argc, &argv))
61  return 2;
62  ret = GNUNET_GETOPT_run ("gnunet-base32",
63  options,
64  argc,
65  argv);
66  if (ret < 0)
67  return 1;
68  if (0 == ret)
69  return 0;
70  in_size = 0;
71  in = NULL;
72  iret = 1;
73  while (iret > 0)
74  {
75  /* read in blocks of 4k */
76  char buf[4092];
77 
78  iret = read (0,
79  buf,
80  sizeof (buf));
81  if (iret < 0)
82  {
83  GNUNET_free (in);
84  return 2;
85  }
86  if (iret > 0)
87  {
88  if (iret + in_size < in_size)
89  {
90  GNUNET_break (0);
91  GNUNET_free (in);
92  return 1;
93  }
95  in_size,
96  in_size + iret);
97  memcpy (&in[in_size - iret],
98  buf,
99  iret);
100  }
101  }
102  if (decode)
103  {
104  /* This formula can overestimate by 1 byte, so we try both
105  out_size and out_size-1 below */
106  out_size = in_size * 5 / 8;
107  out = GNUNET_malloc (out_size);
108  if (GNUNET_OK !=
110  in_size,
111  out,
112  out_size))
113  {
114  out_size--;
115  if (GNUNET_OK !=
117  in_size,
118  out,
119  out_size))
120  {
121  GNUNET_free (out);
122  GNUNET_free (in);
123  return 3;
124  }
125  }
126  }
127  else
128  {
130  in_size);
131  out_size = strlen (out);
132  }
133  {
134  size_t pos = 0;
135 
136  while (pos < out_size)
137  {
138  iret = write (1,
139  &out[pos],
140  out_size - pos);
141  if (iret <= 0)
142  return 4;
143  pos += iret;
144  }
145  }
146  GNUNET_free (out);
147  GNUNET_free_nz ((void *) argv);
148  return 0;
149 }
150 
151 
152 /* end of gnunet-uri.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_help(const char *about)
Defining the option to print the command line help text (-h option).
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1459
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Definition of a command line option.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_version(const char *version)
Define the option to print the version of the application (-v option)
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
static char buf[2048]
int GNUNET_GETOPT_run(const char *binaryOptions, const struct GNUNET_GETOPT_CommandLineOption *allOptions, unsigned int argc, char *const *argv)
Parse the command line.
Definition: getopt.c:883
#define GNUNET_free_nz(ptr)
Wrapper around free.
int main(int argc, char *const *argv)
The main function of gnunet-base32.
Definition: gnunet-base32.c:38
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:937
#define GNUNET_malloc(size)
Wrapper around malloc.
int GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:972
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69