blob: 705f63ae0fc8f8a28e4b8591a5d6ad75e0a0c50d [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * feature.c --- convert between features and strings
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00004 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00005 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00006 * This file can be redistributed under the terms of the GNU Library General
7 * Public License
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00008 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00009 */
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
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000024static const 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{
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000033 const 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{
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000049 const struct hash *p;
Mike Frysinger85cffcc2005-06-11 00:08:50 +000050 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}