File: gf_div.c

package info (click to toggle)
gf-complete 1.0.2%2B2017.04.10.git.ea75cdf-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,272 kB
  • sloc: ansic: 16,714; sh: 573; makefile: 110
file content (68 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download | duplicates (7)
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
/*
 * 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_div.c
 *
 * Multiplies two numbers in gf_2^w
 */

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

#include "gf_complete.h"
#include "gf_method.h"
#include "gf_general.h"

void usage(int why)
{
  fprintf(stderr, "usage: gf_div a b w [method] - does division of a and b in GF(2^w)\n");
  if (why == 'W') {
    fprintf(stderr, "Bad w.\n");
    fprintf(stderr, "Legal w are: 1 - 32, 64 and 128.\n");
    fprintf(stderr, "Append 'h' to w to treat a, b and the quotient as hexadecimal.\n");
    fprintf(stderr, "w=128 is hex only (i.e. '128' will be an error - do '128h')\n");
  }
  if (why == 'A') fprintf(stderr, "Bad a\n");
  if (why == 'B') fprintf(stderr, "Bad b\n");
  if (why == 'M') {
    fprintf(stderr, "Bad Method Specification: ");
    gf_error();
  }
  exit(1);
}

int main(int argc, char **argv)
{
  int hex, w;
  gf_t gf;
  gf_general_t a, b, c;
  char output[50];

  if (argc < 4) usage(' ');

  if (sscanf(argv[3], "%d", &w) == 0) usage('W');
  if (w <= 0 || (w > 32 && w != 64 && w != 128)) usage('W');

  hex = (strchr(argv[3], 'h') != NULL);
  if (!hex && w == 128) usage('W');

  if (argc == 4) {
    if (gf_init_easy(&gf, w) == 0) usage('M');
  } else {
    if (create_gf_from_argv(&gf, w, argc, argv, 4) == 0) usage('M');
  }
 
  if (!gf_general_s_to_val(&a, w, argv[1], hex)) usage('A');
  if (!gf_general_s_to_val(&b, w, argv[2], hex)) usage('B');

  gf_general_divide(&gf, &a, &b, &c);
  gf_general_val_to_s(&c, w, output, hex);
  
  printf("%s\n", output);
  exit(0);
}