File: scale.c

package info (click to toggle)
libreswan 5.2-2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,656 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (107 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (2)
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
/* scales, for libreswan
 *
 * Copyright (C) 2022-2024 Andrew Cagney <cagney@gnu.org>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
 *
 * 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 Library General Public
 * License for more details.
 *
 */

#include <string.h>

#include "scale.h"
#include "lswcdefs.h"
#include "jambuf.h"
#include "lswlog.h"

const struct scale *ttoscale(shunk_t cursor, const struct scales *scales,
			     unsigned default_scale)
{
	if (cursor.len == 0) {
		/* default scaling */
		return &scales->scale.list[default_scale];
	}

	FOR_EACH_ITEM(scale, &scales->scale) {
		if (hunk_strcaseeq(cursor, scale->suffix)) {
			return scale;
		}
	}

	return NULL;
}

/*
 * This does not work for things like 30/60 seconds
 */

size_t jam_decimal(struct jambuf *buf, uintmax_t decimal,
		   uintmax_t numerator, uintmax_t denominator)
{
	size_t s = 0;
	const unsigned base = 10;

	s += jam(buf, "%ju", decimal);

	if (numerator == 0) {
		return s;
	}

	if (numerator >= denominator) {
		/* should not be called */
		s += jam(buf, ".%ju/%ju", numerator, denominator);
		return s;
	}

	/* strip trailing zeros */
	while (numerator % base == 0 &&
	       denominator % base == 0) {
		numerator /= base;
		denominator /= base;
	}

	/* determine precision */
	unsigned precision = 0;
	while (denominator % base == 0) {
		denominator /= base;
		precision += 1;
	}

	s += jam(buf, ".%0*ju", precision, numerator);
	return s;
}

err_t scale_decimal(const struct scale *scale, uintmax_t decimal,
		    uintmax_t numerator, uintmax_t denominator,
		    uintmax_t *value)
{
	ldbgf(DBG_TMI, &global_logger,
	      "%s() decimal=%ju numerator=%ju denominator=%ju "PRI_SCALE"\n",
	      __func__, decimal, numerator, denominator, pri_scale(scale));

	/*
	 * Check that scaling DECIMAL doesn't overflow.
	 */

	if (UINTMAX_MAX / scale->multiplier < decimal) {
		return "overflow";
	}

	*value = decimal * scale->multiplier;
	if (numerator > 0 && denominator > 0) {
		if (denominator > scale->multiplier) {
			return "underflow";
		}
		/* fails on really small fractions? */
		(*value) += (numerator * (scale->multiplier / denominator));
	}

	return NULL;
}