forked from mstone/vscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nat.c
50 lines (45 loc) · 1.14 KB
/
nat.c
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
/* Copyright (c) 2011 Akamai Technologies, Inc. */
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include "cgen.h"
#include "nat.h"
int nat_add(unsigned long* result, unsigned long arg)
{
unsigned long tmp = *result + arg;
if (tmp < arg) {
errno = EOVERFLOW;
return 1;
}
*result = tmp;
return 0;
}
int nat_mult(unsigned long* result, unsigned long long arg)
{
unsigned long long tmp = arg * (*result);
if (tmp > (unsigned long)-1) {
errno = EOVERFLOW;
return 1;
}
*result = ((unsigned long) -1) & tmp;
return 0;
}
int parse_nat(unsigned long* result, const char* buf, size_t buflen) {
unsigned long total = 0;
for (size_t i = 0; i < buflen; i++)
{
LET(char num = buf[i] - '0', num < 0 || num > 9,
"Non-decimal character in string.", out_error_inval);
CHK(nat_mult(&total, 10) == 1, "Multiplication overflow.", out_error_errno);
CHK(nat_add(&total, num) == 1, "Addition overflow.", out_error_errno);
}
*result = total;
return 0;
out_error_inval:
errno = EINVAL;
out_error_errno:
return 1;
}