GNUnet 0.22.2
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;
45 'd',
46 "decode",
48 "run decoder modus, otherwise runs as encoder"),
49 &decode),
51 "Crockford base32 encoder/decoder"),
52 GNUNET_GETOPT_option_version (PACKAGE_VERSION),
54 };
55 int ret;
56 char *in;
57 unsigned int in_size;
58 ssize_t iret;
59 char *out;
60 size_t out_size;
61
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 (out_size > 0) )
114 {
115 out_size--;
116 if (GNUNET_OK !=
118 in_size,
119 out,
120 out_size))
121 {
122 GNUNET_free (out);
123 GNUNET_free (in);
124 return 3;
125 }
126 }
127 }
128 else
129 {
131 in_size);
132 out_size = strlen (out);
133 }
134 {
135 size_t pos = 0;
136
137 while (pos < out_size)
138 {
139 iret = write (1,
140 &out[pos],
141 out_size - pos);
142 if (iret <= 0)
143 return 4;
144 pos += iret;
145 }
146 }
147 GNUNET_free (out);
148 return 0;
149}
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:74
static int ret
Final status code.
Definition: gnunet-arm.c:93
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 struct GNUNET_OS_ProjectData *pd, 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.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:787
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:812
Definition of a command line option.

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

Here is the call graph for this function: