File: parse-number.c

package info (click to toggle)
syslog-ng 3.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,320 kB
  • ctags: 43,937
  • sloc: ansic: 159,432; yacc: 25,059; sh: 13,574; makefile: 4,669; python: 3,468; java: 3,218; xml: 2,309; perl: 318; lex: 316; awk: 184
file content (184 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2013-2014 Balabit
 * Copyright (c) 2013 Gergely Nagy <algernon@balabit.hu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "parse-number.h"

#include <string.h>
#include <errno.h>
#include <stdlib.h>

static gboolean
_valid_unit(const gchar unit_char)
{
  return (unit_char == 'B' || unit_char == 'b');
}

static gboolean
_valid_exponent(const gchar exponent_char)
{
  gchar e = exponent_char;

  return e == 'k' || e == 'K' ||
         e == 'm' || e == 'M' ||
         e == 'g' || e == 'G';
}

static gboolean
_parse_suffix(const gchar *suffix, gchar *exponent_char, gchar *base_char, gchar *unit_char)
{
  gint suffix_len;

  suffix_len = strlen(suffix);
  if (suffix_len > 3)
    return FALSE;
  if (suffix_len == 0)
    return TRUE;

  if (suffix_len == 3)
    {
      *exponent_char = suffix[0];
      *base_char = suffix[1];
      *unit_char = suffix[2];
    }
  else if (suffix_len == 2)
    {
      *exponent_char = suffix[0];
      if (_valid_unit(suffix[1]))
        *unit_char = suffix[1];
      else
        *base_char = suffix[1];
    }
  else if (suffix_len == 1)
    {
      if (_valid_exponent(suffix[0]))
        *exponent_char = suffix[0];
      else if (_valid_unit(suffix[0]))
        *unit_char = suffix[0];
      else
        return FALSE;
    }
  return TRUE;
}

static gboolean
_determine_multiplier(gchar base_char, gint *multiplier)
{
  if (base_char == 0)
    *multiplier = 1000;
  else if (base_char == 'I' || base_char == 'i')
    *multiplier = 1024;
  else
    return FALSE;
  return TRUE;
}

static gboolean
_validate_unit(gchar unit_char)
{
  if (unit_char && !_valid_unit(unit_char))
    return FALSE;
  return TRUE;
}

static gboolean
_process_exponent(gchar exponent_char, gint64 *d, gint multiplier)
{
  switch (exponent_char)
    {
    case 'G':
    case 'g':
      (*d) *= multiplier;
    case 'm':
    case 'M':
      (*d) *= multiplier;
    case 'K':
    case 'k':
      (*d) *= multiplier;
    case 0:
      return TRUE;
    default:
      return FALSE;
    }
}

static gboolean
_process_suffix(const gchar *suffix, gint64 *d)
{
  gchar exponent_char = 0, base_char = 0, unit_char = 0;
  gint multiplier = 0;

  if (!_parse_suffix(suffix, &exponent_char, &base_char, &unit_char))
    return FALSE;

  if (!_determine_multiplier(base_char, &multiplier))
    return FALSE;

  if (!_validate_unit(unit_char))
    return FALSE;

  if (!_process_exponent(exponent_char, d, multiplier))
    return FALSE;

  return TRUE;
}

static gboolean
_parse_number(const gchar *s, gchar **endptr, gint64 *d)
{
  gint64 val;

  errno = 0;
  val = strtoll(s, endptr, 10);

  if (errno == ERANGE || errno == EINVAL)
    return FALSE;

  if (*endptr == s)
    return FALSE;

  *d = val;
  return TRUE;
}

gboolean
parse_number(const gchar *s, gint64 *d)
{
  gchar *endptr;

  if (!_parse_number(s, &endptr, d))
    return FALSE;
  if (*endptr)
    return FALSE;
  return TRUE;
}

gboolean
parse_number_with_suffix(const gchar *s, gint64 *d)
{
  gchar *endptr;

  if (!_parse_number(s, &endptr, d))
    return FALSE;
  return _process_suffix(endptr, d);
}