ClientCollectors/ARODataCollector/external/tcpdump-4.1.1/print-esp.c
static int
espprint_decode_encalgo(netdissect_options *ndo,
			char *decode, struct sa_list *sa)
{
	int len;
	size_t i;
	const EVP_CIPHER *evp;
	int authlen = 0;
	char *colon, *p;
	
	colon = strchr(decode, ':');
	if (colon == NULL) {
		(*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
		return 0;
	}
	*colon = '\0';
	
	len = colon - decode;
	if (strlen(decode) > strlen("-hmac96") &&
	    !strcmp(decode + strlen(decode) - strlen("-hmac96"),
		    "-hmac96")) {
		p = strstr(decode, "-hmac96");
		*p = '\0';
		authlen = 12;
	}
	if (strlen(decode) > strlen("-cbc") &&
	    !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
		p = strstr(decode, "-cbc");
		*p = '\0';
	}
	evp = EVP_get_cipherbyname(decode);

	if (!evp) {
		(*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
		sa->evp = NULL;
		sa->authlen = 0;
		sa->ivlen = 0;
		return 0;
	}
	
	sa->evp = evp;
	sa->authlen = authlen;
	sa->ivlen = EVP_CIPHER_iv_length(evp);
	
	colon++;
	if (colon[0] == '0' && colon[1] == 'x') {
		/* decode some hex! */

		colon += 2;
		sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
		if(sa->secretlen == 0) return 0;
	} else {
		i = strlen(colon);
		
		if (i < sizeof(sa->secret)) {
			memcpy(sa->secret, colon, i);
			sa->secretlen = i;
		} else {
			memcpy(sa->secret, colon, sizeof(sa->secret));
			sa->secretlen = sizeof(sa->secret);
		}
	}

	return 1;
}
/*
 * decode the form:    SPINUM@IP <tab> ALGONAME:0xsecret
 */
decode the form:    SPINUM@IP  ALGONAME:0xsecret
decode the form : SPINUM@IP ALGONAME : 0xsecret
static int
espprint_decode_encalgo(netdissect_options *ndo,
			char *decode, struct sa_list *sa)
{
	int len;
	size_t i;
	const EVP_CIPHER *evp;
	int authlen = 0;
	char *colon, *p;
	colon = strchr(decode, ':');
	if (colon == NULL) {
		(*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
		return 0;
	}
	*colon = '\0';
	len = colon - decode;
	if (strlen(decode) > strlen("-hmac96") &&
	    !strcmp(decode + strlen(decode) - strlen("-hmac96"),
		    "-hmac96")) {
		p = strstr(decode, "-hmac96");
		*p = '\0';
		authlen = 12;
	}
	if (strlen(decode) > strlen("-cbc") &&
	    !strcmp(decode + strlen(decode) - strlen("-cbc"), "-cbc")) {
		p = strstr(decode, "-cbc");
		*p = '\0';
	}
	evp = EVP_get_cipherbyname(decode);
	if (!evp) {
		(*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
		sa->evp = NULL;
		sa->authlen = 0;
		sa->ivlen = 0;
		return 0;
	}
	sa->evp = evp;
	sa->authlen = authlen;
	sa->ivlen = EVP_CIPHER_iv_length(evp);
	colon++;
	if (colon[0] == '0' && colon[1] == 'x') {
		colon += 2;
		sa->secretlen = espprint_decode_hex(ndo, sa->secret, sizeof(sa->secret), colon);
		if(sa->secretlen == 0) return 0;
	} else {
		i = strlen(colon);
		if (i < sizeof(sa->secret)) {
			memcpy(sa->secret, colon, i);
			sa->secretlen = i;
		} else {
			memcpy(sa->secret, colon, sizeof(sa->secret));
			sa->secretlen = sizeof(sa->secret);
		}
	}
	return 1;
}
static int espprint_decode_encalgo ( netdissect_options * ndo , char * decode , struct sa_list * sa ) { int len ; size_t i ; const EVP_CIPHER * evp ; int authlen = 0 ; char * colon , * p ; colon = strchr ( decode , ' ' ) ; if ( colon == NULL ) { ( * ndo -> ndo_warning ) ( ndo , " \n " , decode ) ; return 0 ; } * colon = ' \0 ' ; len = colon - decode ; if ( strlen ( decode ) > strlen ( " " ) && ! strcmp ( decode + strlen ( decode ) - strlen ( " " ) , " " ) ) { p = strstr ( decode , " " ) ; * p = ' \0 ' ; authlen = 12 ; } if ( strlen ( decode ) > strlen ( " " ) && ! strcmp ( decode + strlen ( decode ) - strlen ( " " ) , " " ) ) { p = strstr ( decode , " " ) ; * p = ' \0 ' ; } evp = EVP_get_cipherbyname ( decode ) ; if ( ! evp ) { ( * ndo -> ndo_warning ) ( ndo , " \n " , decode ) ; sa -> evp = NULL ; sa -> authlen = 0 ; sa -> ivlen = 0 ; return 0 ; } sa -> evp = evp ; sa -> authlen = authlen ; sa -> ivlen = EVP_CIPHER_iv_length ( evp ) ; colon ++ ; if ( colon [ 0 ] == ' ' && colon [ 1 ] == ' ' ) { colon += 2 ; sa -> secretlen = espprint_decode_hex ( ndo , sa -> secret , sizeof ( sa -> secret ) , colon ) ; if ( sa -> secretlen == 0 ) return 0 ; } else { i = strlen ( colon ) ; if ( i < sizeof ( sa -> secret ) ) { memcpy ( sa -> secret , colon , i ) ; sa -> secretlen = i ; } else { memcpy ( sa -> secret , colon , sizeof ( sa -> secret ) ) ; sa -> secretlen = sizeof ( sa -> secret ) ; } } return 1 ; }
decode the form:    SPINUM@IP <tab> ALGONAME:0xsecret
decode the form : SPINUM@IP <tab > ALGONAME : 0xsecret
/* decode some hex! */