]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/e2p/hashstr.c
Shorten compile commands run by the build system
[thirdparty/e2fsprogs.git] / lib / e2p / hashstr.c
CommitLineData
f61fc0b5
TT
1/*
2 * feature.c --- convert between features and strings
efc6f628 3 *
f61fc0b5 4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
efc6f628 5 *
543547a5
TT
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
f61fc0b5
TT
10 */
11
d1154eb4 12#include "config.h"
f61fc0b5
TT
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <ctype.h>
17#include <errno.h>
18
19#include "e2p.h"
20
21struct hash {
22 int num;
23 const char *string;
24};
25
26static struct hash hash_list[] = {
27 { EXT2_HASH_LEGACY, "legacy" },
28 { EXT2_HASH_HALF_MD4, "half_md4" },
29 { EXT2_HASH_TEA, "tea" },
30 { 0, 0 },
31};
32
33const char *e2p_hash2string(int num)
34{
35 struct hash *p;
36 static char buf[20];
f61fc0b5
TT
37
38 for (p = hash_list; p->string; p++) {
39 if (num == p->num)
40 return p->string;
41 }
42 sprintf(buf, "HASHALG_%d", num);
43 return buf;
44}
45
46/*
47 * Returns the hash algorithm, or -1 on error
48 */
49int e2p_string2hash(char *string)
50{
51 struct hash *p;
52 char *eptr;
53 int num;
54
55 for (p = hash_list; p->string; p++) {
56 if (!strcasecmp(string, p->string)) {
57 return p->num;
58 }
59 }
60 if (strncasecmp(string, "HASHALG_", 8))
61 return -1;
62
63 if (string[8] == 0)
64 return -1;
65 num = strtol(string+8, &eptr, 10);
66 if (num > 255 || num < 0)
67 return -1;
68 if (*eptr)
69 return -1;
70 return num;
71}
72