"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 2 | /* |
| 3 | * feature.c --- convert between features and strings |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu> |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 6 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 7 | * This file can be redistributed under the terms of the GNU Library General |
| 8 | * Public License |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 9 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <ctype.h> |
| 16 | #include <errno.h> |
| 17 | |
| 18 | #include "e2p.h" |
| 19 | |
| 20 | struct hash { |
Mike Frysinger | 85cffcc | 2005-06-11 00:08:50 +0000 | [diff] [blame] | 21 | int num; |
| 22 | const char *string; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 25 | static const struct hash hash_list[] = { |
Mike Frysinger | 85cffcc | 2005-06-11 00:08:50 +0000 | [diff] [blame] | 26 | { EXT2_HASH_LEGACY, "legacy" }, |
| 27 | { EXT2_HASH_HALF_MD4, "half_md4" }, |
| 28 | { EXT2_HASH_TEA, "tea" }, |
| 29 | { 0, 0 }, |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | const char *e2p_hash2string(int num) |
| 33 | { |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 34 | const struct hash *p; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 35 | static char buf[20]; |
| 36 | |
| 37 | for (p = hash_list; p->string; p++) { |
| 38 | if (num == p->num) |
| 39 | return p->string; |
| 40 | } |
| 41 | sprintf(buf, "HASHALG_%d", num); |
| 42 | return buf; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Returns the hash algorithm, or -1 on error |
| 47 | */ |
| 48 | int e2p_string2hash(char *string) |
| 49 | { |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 50 | const struct hash *p; |
Mike Frysinger | 85cffcc | 2005-06-11 00:08:50 +0000 | [diff] [blame] | 51 | char *eptr; |
| 52 | int num; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 53 | |
| 54 | for (p = hash_list; p->string; p++) { |
| 55 | if (!strcasecmp(string, p->string)) { |
| 56 | return p->num; |
| 57 | } |
| 58 | } |
| 59 | if (strncasecmp(string, "HASHALG_", 8)) |
| 60 | return -1; |
| 61 | |
| 62 | if (string[8] == 0) |
| 63 | return -1; |
| 64 | num = strtol(string+8, &eptr, 10); |
| 65 | if (num > 255 || num < 0) |
| 66 | return -1; |
| 67 | if (*eptr) |
| 68 | return -1; |
| 69 | return num; |
| 70 | } |