]> git.proxmox.com Git - ceph.git/blob - ceph/src/erasure-code/jerasure/gf-complete/tools/gf_mult.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / erasure-code / jerasure / gf-complete / tools / gf_mult.c
1 /*
2 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
3 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
4 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
5 *
6 * gf_mult.c
7 *
8 * Multiplies two numbers in gf_2^w
9 */
10
11 #include <stdio.h>
12 #include <getopt.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include "gf_complete.h"
18 #include "gf_method.h"
19 #include "gf_general.h"
20
21 void usage(int why)
22 {
23 fprintf(stderr, "usage: gf_mult a b w [method] - does multiplication of a and b in GF(2^w)\n");
24 if (why == 'W') {
25 fprintf(stderr, "Bad w.\n");
26 fprintf(stderr, "Legal w are: 1 - 32, 64 and 128.\n");
27 fprintf(stderr, "Append 'h' to w to treat a, b and the product as hexadecimal.\n");
28 fprintf(stderr, "w=128 is hex only (i.e. '128' will be an error - do '128h')\n");
29 }
30 if (why == 'A') fprintf(stderr, "Bad a\n");
31 if (why == 'B') fprintf(stderr, "Bad b\n");
32 if (why == 'M') {
33 fprintf(stderr, "Bad Method Specification: ");
34 gf_error();
35 }
36 exit(1);
37 }
38
39 int main(int argc, char **argv)
40 {
41 int hex, w;
42 gf_t gf;
43 gf_general_t a, b, c;
44 char output[50];
45
46 if (argc < 4) usage(' ');
47
48 if (sscanf(argv[3], "%d", &w) == 0) usage('W');
49 if (w <= 0 || (w > 32 && w != 64 && w != 128)) usage('W');
50
51 hex = (strchr(argv[3], 'h') != NULL);
52 if (!hex && w == 128) usage('W');
53
54 if (argc == 4) {
55 if (gf_init_easy(&gf, w) == 0) usage('M');
56 } else {
57 if (create_gf_from_argv(&gf, w, argc, argv, 4) == 0) usage('M');
58 }
59
60 if (!gf_general_s_to_val(&a, w, argv[1], hex)) usage('A');
61 if (!gf_general_s_to_val(&b, w, argv[2], hex)) usage('B');
62
63 gf_general_multiply(&gf, &a, &b, &c);
64 gf_general_val_to_s(&c, w, output, hex);
65
66 printf("%s\n", output);
67 exit(0);
68 }