blob: 7c0552f098eaca60ff63b2faa2472bf734ff5f12 [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * feature.c --- convert between features and strings
3 *
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
5 *
6 * This file can be redistributed under the terms of the GNU Library General
7 * Public License
8 *
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <ctype.h>
15#include <errno.h>
16
17#include "e2p.h"
18
19struct hash {
Mike Frysinger85cffcc2005-06-11 00:08:50 +000020 int num;
21 const char *string;
Mike Frysingerd89e6292005-04-24 05:07:59 +000022};
23
24static struct hash hash_list[] = {
Mike Frysinger85cffcc2005-06-11 00:08:50 +000025 { EXT2_HASH_LEGACY, "legacy" },
26 { EXT2_HASH_HALF_MD4, "half_md4" },
27 { EXT2_HASH_TEA, "tea" },
28 { 0, 0 },
Mike Frysingerd89e6292005-04-24 05:07:59 +000029};
30
31const char *e2p_hash2string(int num)
32{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000033 struct hash *p;
Mike Frysingerd89e6292005-04-24 05:07:59 +000034 static char buf[20];
35
36 for (p = hash_list; p->string; p++) {
37 if (num == p->num)
38 return p->string;
39 }
40 sprintf(buf, "HASHALG_%d", num);
41 return buf;
42}
43
44/*
45 * Returns the hash algorithm, or -1 on error
46 */
47int e2p_string2hash(char *string)
48{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000049 struct hash *p;
50 char *eptr;
51 int num;
Mike Frysingerd89e6292005-04-24 05:07:59 +000052
53 for (p = hash_list; p->string; p++) {
54 if (!strcasecmp(string, p->string)) {
55 return p->num;
56 }
57 }
58 if (strncasecmp(string, "HASHALG_", 8))
59 return -1;
60
61 if (string[8] == 0)
62 return -1;
63 num = strtol(string+8, &eptr, 10);
64 if (num > 255 || num < 0)
65 return -1;
66 if (*eptr)
67 return -1;
68 return num;
69}