Home | History | Annotate | Download | only in e2p
      1 /*
      2  * feature.c --- convert between features and strings
      3  *
      4  * Copyright (C) 1999  Theodore Ts'o <tytso (at) 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 {
     20 	int		num;
     21 	const char	*string;
     22 };
     23 
     24 static struct hash hash_list[] = {
     25 	{	EXT2_HASH_LEGACY, 	"legacy" },
     26 	{	EXT2_HASH_HALF_MD4, 	"half_md4" },
     27 	{	EXT2_HASH_TEA, 		"tea" },
     28 	{	0, 0 },
     29 };
     30 
     31 const char *e2p_hash2string(int num)
     32 {
     33 	struct hash  *p;
     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 {
     49 	struct hash	*p;
     50 	char		*eptr;
     51 	int		num;
     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 }
     70 
     71