summaryrefslogtreecommitdiff
path: root/src/gf_method.c
blob: 2210305d8c324fb6caed6369fde16cb5b0721c3f (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
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
185
186
187
188
189
190
191
192
193
/*
 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
 *
 * gf_method.c
 *
 * Parses argv to figure out the mult_type and arguments.  Returns the gf.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "gf_complete.h"
#include "gf_int.h"
#include "gf_method.h"

int create_gf_from_argv(gf_t *gf, int w, int argc, char **argv, int starting)
{
  int mult_type, divide_type, region_type;
  int arg1, arg2;
  uint64_t prim_poly;
  gf_t *base;

  mult_type = GF_MULT_DEFAULT;
  region_type = GF_REGION_DEFAULT;
  divide_type = GF_DIVIDE_DEFAULT;
  prim_poly = 0;
  base = NULL;
  arg1 = 0;
  arg2 = 0;
  while (1) {
    if (argc > starting) {
      if (strcmp(argv[starting], "-m") == 0) {
        starting++;
        if (mult_type != GF_MULT_DEFAULT) {
          if (base != NULL) gf_free(base, 1);
          _gf_errno = GF_E_TWOMULT;
          return 0;
        }
        if (strcmp(argv[starting], "SHIFT") == 0) {
          mult_type = GF_MULT_SHIFT;
          starting++;
        } else if (strcmp(argv[starting], "CARRY_FREE") == 0) {
          mult_type = GF_MULT_CARRY_FREE;
          starting++;
        } else if (strcmp(argv[starting], "CARRY_FREE_GK") == 0) {
          mult_type = GF_MULT_CARRY_FREE_GK;
          starting++;
        } else if (strcmp(argv[starting], "GROUP") == 0) {
          mult_type = GF_MULT_GROUP;
          if (argc < starting + 3) {
            _gf_errno = GF_E_GROUPAR;
            return 0;
          }
          if (sscanf(argv[starting+1], "%d", &arg1) == 0 ||
              sscanf(argv[starting+2], "%d", &arg2) == 0) {
            _gf_errno = GF_E_GROUPNU;
            return 0;
          }
          starting += 3;
        } else if (strcmp(argv[starting], "BYTWO_p") == 0) {
          mult_type = GF_MULT_BYTWO_p;
          starting++;
        } else if (strcmp(argv[starting], "BYTWO_b") == 0) {
          mult_type = GF_MULT_BYTWO_b;
          starting++;
        } else if (strcmp(argv[starting], "TABLE") == 0) {
          mult_type = GF_MULT_TABLE;
          starting++;
        } else if (strcmp(argv[starting], "LOG") == 0) {
          mult_type = GF_MULT_LOG_TABLE;
          starting++;
        } else if (strcmp(argv[starting], "LOG_ZERO") == 0) {
          mult_type = GF_MULT_LOG_ZERO;
          starting++;
        } else if (strcmp(argv[starting], "LOG_ZERO_EXT") == 0) {
          mult_type = GF_MULT_LOG_ZERO_EXT;
          starting++;
        } else if (strcmp(argv[starting], "SPLIT") == 0) {
          mult_type = GF_MULT_SPLIT_TABLE;
          if (argc < starting + 3) {
            _gf_errno = GF_E_SPLITAR;
            return 0;
          }
          if (sscanf(argv[starting+1], "%d", &arg1) == 0 ||
              sscanf(argv[starting+2], "%d", &arg2) == 0) {
            _gf_errno = GF_E_SPLITNU;
            return 0;
          }
          starting += 3;
        } else if (strcmp(argv[starting], "COMPOSITE") == 0) {
          mult_type = GF_MULT_COMPOSITE;
          if (argc < starting + 2) { _gf_errno = GF_E_FEWARGS; return 0; }
          if (sscanf(argv[starting+1], "%d", &arg1) == 0) {
            _gf_errno = GF_E_COMP_A2;
            return 0;
          }
          starting += 2;
          base = (gf_t *) malloc(sizeof(gf_t));
          starting = create_gf_from_argv(base, w/arg1, argc, argv, starting);
          if (starting == 0) {
            free(base);
            return 0;
          }
        } else {
          _gf_errno = GF_E_UNKNOWN;
          return 0;
        }
      } else if (strcmp(argv[starting], "-r") == 0) {
        starting++;
        if (strcmp(argv[starting], "DOUBLE") == 0) {
          region_type |= GF_REGION_DOUBLE_TABLE;
          starting++;
        } else if (strcmp(argv[starting], "QUAD") == 0) {
          region_type |= GF_REGION_QUAD_TABLE;
          starting++;
        } else if (strcmp(argv[starting], "LAZY") == 0) {
          region_type |= GF_REGION_LAZY;
          starting++;
        } else if (strcmp(argv[starting], "SIMD") == 0) {
          region_type |= GF_REGION_SIMD;
          starting++;
        } else if (strcmp(argv[starting], "NOSIMD") == 0) {
          region_type |= GF_REGION_NOSIMD;
          starting++;
        } else if (strcmp(argv[starting], "SSE") == 0) {
          region_type |= GF_REGION_SIMD;
          starting++;
        } else if (strcmp(argv[starting], "NOSSE") == 0) {
          region_type |= GF_REGION_NOSIMD;
          starting++;
        } else if (strcmp(argv[starting], "CAUCHY") == 0) {
          region_type |= GF_REGION_CAUCHY;
          starting++;
        } else if (strcmp(argv[starting], "ALTMAP") == 0) {
          region_type |= GF_REGION_ALTMAP;
          starting++;
        } else {
          if (base != NULL) gf_free(base, 1);
          _gf_errno = GF_E_UNK_REG;
          return 0;
        }
      } else if (strcmp(argv[starting], "-p") == 0) {
        starting++;
        if (sscanf(argv[starting], "%llx", (long long unsigned int *)(&prim_poly)) == 0) {
          if (base != NULL) gf_free(base, 1);
          _gf_errno = GF_E_POLYSPC;
          return 0;
        }
        starting++;
      } else if (strcmp(argv[starting], "-d") == 0) {
        starting++;
        if (divide_type != GF_DIVIDE_DEFAULT) {
          if (base != NULL) gf_free(base, 1);
          _gf_errno = GF_E_TWO_DIV;
          return 0;
        } else if (strcmp(argv[starting], "EUCLID") == 0) {
          divide_type = GF_DIVIDE_EUCLID;
          starting++;
        } else if (strcmp(argv[starting], "MATRIX") == 0) {
          divide_type = GF_DIVIDE_MATRIX;
          starting++;
        } else {
          _gf_errno = GF_E_UNK_DIV;
          return 0;
        }
      } else if (strcmp(argv[starting], "-") == 0) {
         /*
         printf("Scratch size: %d\n", gf_scratch_size(w, 
                                      mult_type, region_type, divide_type, arg1, arg2));
         */
        if (gf_init_hard(gf, w, mult_type, region_type, divide_type, 
                         prim_poly, arg1, arg2, base, NULL) == 0) {
          if (base != NULL) gf_free(base, 1);
          return 0;
        } else
          return starting + 1;
      } else {
        if (base != NULL) gf_free(base, 1);
        _gf_errno = GF_E_UNKFLAG;
        return 0;
      }
    } else {
      if (base != NULL) gf_free(base, 1);
      _gf_errno = GF_E_FEWARGS;
      return 0;
    }
  }
}