summaryrefslogtreecommitdiff
path: root/src/util/pin.c
blob: 0285bb02c91ba2366085f197348da7b9c683f3a1 (plain)
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
/*
     This file is part of GNU Anastasis.
     Copyright (C) 2021 Anastasis SARL

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

     Anastasis 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with Anastasis; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file anastasis/src/util/pin.c
 * @brief pin conversion functions
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis_util_lib.h"


bool
ANASTASIS_scan_pin (const char *as,
                    unsigned long long *pin)
{
  char dummy;
  char s[16];

  if ( (NULL != as) &&
       (0 == strncasecmp ("A-", as, 2)) )
    as += 2; /* skip "A-" prefix if present */
  if (strlen (as) != 18)
    return false;
  if ( ('-' != as[5]) ||
       ('-' != as[9]) ||
       ('-' != as[14]) )
    return false;
  GNUNET_snprintf (s,
                   sizeof (s),
                   "%.5s%.3s%.4s%.3s",
                   as,
                   &as[6],
                   &as[10],
                   &as[15]);
  if (1 != sscanf (s,
                   "%llu%c",
                   pin,
                   &dummy))
  {
    GNUNET_break (0);
    return false;
  }
  return true;
}


const char *
ANASTASIS_pin2s (uint64_t pin)
{
  static char buf[22];
  char tmp[16];

  GNUNET_assert (pin < ANASTASIS_PIN_MAX_VALUE);
  GNUNET_snprintf (tmp,
                   sizeof (tmp),
                   "%015llu",
                   (unsigned long long) pin);
  GNUNET_snprintf (buf,
                   sizeof (buf),
                   "A-%.5s-%.3s-%.4s-%.3s",
                   tmp,
                   &tmp[5],
                   &tmp[8],
                   &tmp[12]);
  return buf;
}