Path: blob/main/crypto/krb5/src/tests/asn.1/utility.c
34890 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/asn.1/utility.c */2/*3* Copyright (C) 1994 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "utility.h"27#include "krb5.h"28#include <stdlib.h>29#include <stdio.h>30#include <ctype.h>3132krb5int_access acc;3334char hexchar (const unsigned int digit);3536void *37ealloc(size_t size)38{39void *ptr = calloc(1, size);4041if (ptr == NULL)42abort();43return ptr;44}4546char *47estrdup(const char *str)48{49char *newstr = strdup(str);5051if (newstr == NULL)52abort();53return newstr;54}5556void57asn1_krb5_data_unparse(const krb5_data *code, char **s)58{59if (*s != NULL) free(*s);6061if (code==NULL) {62*s = estrdup("<NULL>");63} else if (code->data == NULL || ((int) code->length) <= 0) {64*s = estrdup("<EMPTY>");65} else {66unsigned int i;6768*s = ealloc(3 * code->length);69for (i = 0; i < code->length; i++) {70(*s)[3*i] = hexchar((unsigned char) (((code->data)[i]&0xF0)>>4));71(*s)[3*i+1] = hexchar((unsigned char) ((code->data)[i]&0x0F));72(*s)[3*i+2] = ' ';73}74(*s)[3*(code->length)-1] = '\0';75}76}7778char79hexchar(const unsigned int digit)80{81if (digit<=9)82return '0'+digit;83else if (digit<=15)84return 'A'+digit-10;85else86return 'X';87}8889void90krb5_data_parse(krb5_data *d, const char *s)91{92d->length = strlen(s);93d->data = ealloc(d->length);94memcpy(d->data, s, d->length);95}9697krb5_error_code98krb5_data_hex_parse(krb5_data *d, const char *s)99{100int lo;101long v;102const char *cp;103char *dp;104char buf[2];105106d->data = ealloc(strlen(s) / 2 + 1);107d->length = 0;108buf[1] = '\0';109for (lo = 0, dp = d->data, cp = s; *cp; cp++) {110if (*cp < 0)111return ASN1_PARSE_ERROR;112else if (isspace((unsigned char) *cp))113continue;114else if (isxdigit((unsigned char) *cp)) {115buf[0] = *cp;116v = strtol(buf, NULL, 16);117} else118return ASN1_PARSE_ERROR;119if (lo) {120*dp++ |= v;121lo = 0;122} else {123*dp = v << 4;124lo = 1;125}126}127128d->length = dp - d->data;129return 0;130}131132void133init_access(const char *progname)134{135krb5_error_code ret;136ret = krb5int_accessor(&acc, KRB5INT_ACCESS_VERSION);137if (ret) {138com_err(progname, ret, "while initializing accessor");139exit(1);140}141}142143144