Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/nss/nat.c
blob: dc6b648f83e455ba8458d81b05546bd77102d994 (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
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>

#include "cgen.h"
#include "nat.h"

#include "config/debug.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;
}