Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | struct hash { |
Mike Frysinger | 85cffcc | 2005-06-11 00:08:50 +0000 | [diff] [blame] | 20 | int num; |
| 21 | const char *string; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 24 | static const struct hash hash_list[] = { |
Mike Frysinger | 85cffcc | 2005-06-11 00:08:50 +0000 | [diff] [blame] | 25 | { EXT2_HASH_LEGACY, "legacy" }, |
| 26 | { EXT2_HASH_HALF_MD4, "half_md4" }, |
| 27 | { EXT2_HASH_TEA, "tea" }, |
| 28 | { 0, 0 }, |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | const char *e2p_hash2string(int num) |
| 32 | { |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 33 | const struct hash *p; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 34 | 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 | */ |
| 47 | int e2p_string2hash(char *string) |
| 48 | { |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 49 | const struct hash *p; |
Mike Frysinger | 85cffcc | 2005-06-11 00:08:50 +0000 | [diff] [blame] | 50 | char *eptr; |
| 51 | int num; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 52 | |
| 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 | } |