commit 8485af4277ea7b35e660db9c41442175c6f882e6
parent e89af522b8931165d0c5f1d413e1b37263319d0d
Author: sin <sin@2f30.org>
Date: Thu, 18 Apr 2019 22:16:39 +0100
Flatten algomap structure
Diffstat:
M | compress.c | | | 39 | ++++++++++++++------------------------- |
M | hash.c | | | 41 | +++++++++++++++-------------------------- |
2 files changed, 29 insertions(+), 51 deletions(-)
diff --git a/compress.c b/compress.c
@@ -41,14 +41,10 @@ static struct compr_ops {
},
};
-static struct algomap {
- char *name;
- int type;
-} algomap[] = {
- { .name = "none", .type = COMPR_NONE },
- { .name = "lz4", .type = COMPR_LZ4 },
- { .name = "snappy", .type = COMPR_SNAPPY },
- { .name = NULL, .type = -1 },
+static char *algomap[NR_COMPRS] = {
+ [COMPR_NONE] = "none",
+ [COMPR_LZ4] = "lz4",
+ [COMPR_SNAPPY] = "snappy",
};
int
@@ -90,34 +86,27 @@ compr_final(struct compr_ctx *ctx)
int
compr_name2type(char *name)
{
- struct algomap *algo;
+ size_t i;
- for (algo = &algomap[0]; algo->name != NULL; algo++)
- if (strcasecmp(algo->name, name) == 0)
- break;
- if (algo->name == NULL)
- return -1;
- return algo->type;
+ for (i = 0; i < NR_COMPRS; i++)
+ if (strcasecmp(algomap[i], name) == 0)
+ return i;
+ return -1;
}
char *
compr_type2name(int type)
{
- struct algomap *algo;
-
- for (algo = &algomap[0]; algo->name != NULL; algo++)
- if (algo->type == type)
- break;
- if (algo->name == NULL)
+ if (type < 0 || type >= NR_HASHES)
return NULL;
- return algo->name;
+ return algomap[type];
}
void
compr_list(int fd)
{
- struct algomap *algo;
+ size_t i;
- for (algo = &algomap[0]; algo->name != NULL; algo++)
- dprintf(fd, "%s\n", algo->name);
+ for (i = 0; i < NR_COMPRS; i++)
+ dprintf(fd, "%s\n", algomap[i]);
}
diff --git a/hash.c b/hash.c
@@ -36,15 +36,11 @@ static struct hash_ops {
},
};
-static struct algomap {
- char *name;
- int type;
-} algomap[] = {
- { .name = "blake2b", .type = HASH_BLAKE2B },
- { .name = "blake2bp", .type = HASH_BLAKE2BP },
- { .name = "blake2s", .type = HASH_BLAKE2S },
- { .name = "blake2sp", .type = HASH_BLAKE2SP },
- { .name = NULL, .type = -1 },
+static char *algomap[NR_HASHES] = {
+ [HASH_BLAKE2B] = "blake2b",
+ [HASH_BLAKE2BP] = "blake2bp",
+ [HASH_BLAKE2S] = "blake2s",
+ [HASH_BLAKE2SP] = "blake2sp",
};
int
@@ -72,34 +68,27 @@ hash_final(struct hash_ctx *ctx, void *buf, size_t n)
int
hash_name2type(char *name)
{
- struct algomap *algo;
+ size_t i;
- for (algo = &algomap[0]; algo->name != NULL; algo++)
- if (strcasecmp(algo->name, name) == 0)
- break;
- if (algo->name == NULL)
- return -1;
- return algo->type;
+ for (i = 0; i < NR_HASHES; i++)
+ if (strcasecmp(algomap[i], name) == 0)
+ return i;
+ return -1;
}
char *
hash_type2name(int type)
{
- struct algomap *algo;
-
- for (algo = &algomap[0]; algo->name != NULL; algo++)
- if (algo->type == type)
- break;
- if (algo->name == NULL)
+ if (type < 0 || type >= NR_HASHES)
return NULL;
- return algo->name;
+ return algomap[type];
}
void
hash_list(int fd)
{
- struct algomap *algo;
+ size_t i;
- for (algo = &algomap[0]; algo->name != NULL; algo++)
- dprintf(fd, "%s\n", algo->name);
+ for (i = 0; i < NR_HASHES; i++)
+ dprintf(fd, "%s\n", algomap[i]);
}