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