t_bitst.c revision 296465
1244197Sgonzo/* t_bitst.c */
2330897Seadler/*
3330897Seadler * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4330897Seadler * 1999.
5244197Sgonzo */
6244197Sgonzo/* ====================================================================
7244197Sgonzo * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
8244197Sgonzo *
9244197Sgonzo * Redistribution and use in source and binary forms, with or without
10244197Sgonzo * modification, are permitted provided that the following conditions
11244197Sgonzo * are met:
12244197Sgonzo *
13244197Sgonzo * 1. Redistributions of source code must retain the above copyright
14244197Sgonzo *    notice, this list of conditions and the following disclaimer.
15244197Sgonzo *
16244197Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
17244197Sgonzo *    notice, this list of conditions and the following disclaimer in
18244197Sgonzo *    the documentation and/or other materials provided with the
19244197Sgonzo *    distribution.
20244197Sgonzo *
21244197Sgonzo * 3. All advertising materials mentioning features or use of this
22244197Sgonzo *    software must display the following acknowledgment:
23244197Sgonzo *    "This product includes software developed by the OpenSSL Project
24244197Sgonzo *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25244197Sgonzo *
26244197Sgonzo * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27244197Sgonzo *    endorse or promote products derived from this software without
28244197Sgonzo *    prior written permission. For written permission, please contact
29244197Sgonzo *    licensing@OpenSSL.org.
30244197Sgonzo *
31244197Sgonzo * 5. Products derived from this software may not be called "OpenSSL"
32244197Sgonzo *    nor may "OpenSSL" appear in their names without prior written
33244197Sgonzo *    permission of the OpenSSL Project.
34244197Sgonzo *
35244197Sgonzo * 6. Redistributions of any form whatsoever must retain the following
36244197Sgonzo *    acknowledgment:
37244197Sgonzo *    "This product includes software developed by the OpenSSL Project
38330897Seadler *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39244197Sgonzo *
40244197Sgonzo * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41244197Sgonzo * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42244197Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43244197Sgonzo * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44244197Sgonzo * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45244197Sgonzo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46244197Sgonzo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47330897Seadler * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48330897Seadler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49244197Sgonzo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50244197Sgonzo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51244197Sgonzo * OF THE POSSIBILITY OF SUCH DAMAGE.
52244197Sgonzo * ====================================================================
53244197Sgonzo *
54244197Sgonzo * This product includes cryptographic software written by Eric Young
55244197Sgonzo * (eay@cryptsoft.com).  This product includes software written by Tim
56244197Sgonzo * Hudson (tjh@cryptsoft.com).
57244197Sgonzo *
58244197Sgonzo */
59244197Sgonzo
60244197Sgonzo#include <stdio.h>
61244197Sgonzo#include "cryptlib.h"
62244197Sgonzo#include <openssl/conf.h>
63244197Sgonzo#include <openssl/x509v3.h>
64244197Sgonzo
65244197Sgonzoint ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs,
66330897Seadler                               BIT_STRING_BITNAME *tbl, int indent)
67330897Seadler{
68330897Seadler    BIT_STRING_BITNAME *bnam;
69330897Seadler    char first = 1;
70330897Seadler    BIO_printf(out, "%*s", indent, "");
71330897Seadler    for (bnam = tbl; bnam->lname; bnam++) {
72330897Seadler        if (ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) {
73244197Sgonzo            if (!first)
74330897Seadler                BIO_puts(out, ", ");
75330897Seadler            BIO_puts(out, bnam->lname);
76244197Sgonzo            first = 0;
77330897Seadler        }
78330897Seadler    }
79330897Seadler    BIO_puts(out, "\n");
80244197Sgonzo    return 1;
81244197Sgonzo}
82330897Seadler
83330897Seadlerint ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, char *name, int value,
84330897Seadler                            BIT_STRING_BITNAME *tbl)
85330897Seadler{
86244197Sgonzo    int bitnum;
87330897Seadler    bitnum = ASN1_BIT_STRING_num_asc(name, tbl);
88244197Sgonzo    if (bitnum < 0)
89244197Sgonzo        return 0;
90330897Seadler    if (bs) {
91330897Seadler        if (!ASN1_BIT_STRING_set_bit(bs, bitnum, value))
92330897Seadler            return 0;
93330897Seadler    }
94330897Seadler    return 1;
95330897Seadler}
96330897Seadler
97330897Seadlerint ASN1_BIT_STRING_num_asc(char *name, BIT_STRING_BITNAME *tbl)
98330897Seadler{
99330897Seadler    BIT_STRING_BITNAME *bnam;
100330897Seadler    for (bnam = tbl; bnam->lname; bnam++) {
101330897Seadler        if (!strcmp(bnam->sname, name) || !strcmp(bnam->lname, name))
102330897Seadler            return bnam->bitnum;
103330897Seadler    }
104330897Seadler    return -1;
105330897Seadler}
106330897Seadler