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