GNUnet  0.20.0
gnunet-base32.c File Reference

tool to encode/decode from/to the Crockford Base32 encoding GNUnet uses More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for gnunet-base32.c:

Go to the source code of this file.

Functions

int main (int argc, char *const *argv)
 The main function of gnunet-base32. More...
 

Detailed Description

tool to encode/decode from/to the Crockford Base32 encoding GNUnet uses

Author
Christian Grothoff

Definition in file gnunet-base32.c.

Function Documentation

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function of gnunet-base32.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 39 of file gnunet-base32.c.

41 {
42  int decode = 0;
43  const struct GNUNET_GETOPT_CommandLineOption options[] = {
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),
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  }
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 !=
112  in_size,
113  out,
114  out_size)) &&
115  (out_size > 0) )
116  {
117  out_size--;
118  if (GNUNET_OK !=
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  {
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 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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:884
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_help(const char *about)
Defining the option to print the command line help text (-h option).
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.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_version(const char *version)
Define the option to print the version of the application (-v option)
@ GNUNET_OK
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:763
enum GNUNET_GenericReturnValue 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:788
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
Definition of a command line option.

References buf, gettext_noop, GNUNET_array_grow, GNUNET_break, GNUNET_free, GNUNET_free_nz, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_help(), GNUNET_GETOPT_option_version(), GNUNET_GETOPT_run(), GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_data_to_string_alloc(), GNUNET_STRINGS_get_utf8_args(), GNUNET_STRINGS_string_to_data(), options, and ret.

Here is the call graph for this function: