aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-base32.c
blob: 2c797f56e9ee4d5caf3f754cb5bd53e62450c1aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
     This file is part of GNUnet.
     Copyright (C) 2021 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file util/gnunet-base32.c
 * @brief tool to encode/decode from/to the Crockford Base32 encoding GNUnet uses
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"


/**
 * The main function of gnunet-base32
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc,
      char *const *argv)
{
  int decode = 0;
  const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_flag ('d',
                               "decode",
                               gettext_noop ("run decoder modus, otherwise runs as encoder"),
                               &decode),
    GNUNET_GETOPT_option_help ("Crockford base32 encoder/decoder"),
    GNUNET_GETOPT_option_version (PACKAGE_VERSION),
    GNUNET_GETOPT_OPTION_END
  };
  int ret;
  char *in;
  unsigned int in_size;
  ssize_t iret;
  char *out;
  size_t out_size;

  if (GNUNET_OK !=
      GNUNET_STRINGS_get_utf8_args (argc, argv,
                                    &argc, &argv))
    return 2;
  ret = GNUNET_GETOPT_run ("gnunet-base32",
                           options,
                           argc,
                           argv);
  if (ret < 0)
    return 1;
  if (0 == ret)
    return 0;
  in_size = 0;
  in = NULL;
  iret = 1;
  while (iret > 0)
  {
    /* read in blocks of 4k */
    char buf[4092];

    iret = read (0,
                 buf,
                 sizeof (buf));
    if (iret < 0)
    {
      GNUNET_free (in);
      return 2;
    }
    if (iret > 0)
    {
      if (iret + in_size < in_size)
      {
        GNUNET_break (0);
        GNUNET_free (in);
        return 1;
      }
      GNUNET_array_grow (in,
                         in_size,
                         in_size + iret);
      memcpy (&in[in_size - iret],
              buf,
              iret);
    }
  }
  if (decode)
  {
    /* This formula can overestimate by 1 byte, so we try both
       out_size and out_size-1 below */
    out_size = in_size * 5 / 8;
    out = GNUNET_malloc (out_size);
    if (GNUNET_OK !=
        GNUNET_STRINGS_string_to_data (in,
                                       in_size,
                                       out,
                                       out_size))
    {
      out_size--;
      if (GNUNET_OK !=
          GNUNET_STRINGS_string_to_data (in,
                                         in_size,
                                         out,
                                         out_size))
      {
        GNUNET_free (out);
        GNUNET_free (in);
        return 3;
      }
    }
  }
  else
  {
    out = GNUNET_STRINGS_data_to_string_alloc (in,
                                               in_size);
    out_size = strlen (out);
  }
  {
    size_t pos = 0;

    while (pos < out_size)
    {
      iret = write (1,
                    &out[pos],
                    out_size - pos);
      if (iret <= 0)
        return 4;
      pos += iret;
    }
  }
  GNUNET_free (out);
  GNUNET_free_nz ((void *) argv);
  return 0;
}


/* end of gnunet-uri.c */