xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/asn1/der_cmp.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: der_cmp.c,v 1.2 2017/01/28 21:31:45 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2003-2005 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "der_locl.h"
37 
38 int
39 der_heim_oid_cmp(const heim_oid *p, const heim_oid *q)
40 {
41     if (p->length != q->length)
42 	return (int)(p->length - q->length);
43     return memcmp(p->components,
44 		  q->components,
45 		  p->length * sizeof(*p->components));
46 }
47 
48 int
49 der_heim_octet_string_cmp(const heim_octet_string *p,
50 			  const heim_octet_string *q)
51 {
52     if (p->length != q->length)
53 	return (int)(p->length - q->length);
54     return memcmp(p->data, q->data, p->length);
55 }
56 
57 int
58 der_printable_string_cmp(const heim_printable_string *p,
59 			 const heim_printable_string *q)
60 {
61     return der_heim_octet_string_cmp(p, q);
62 }
63 
64 int
65 der_ia5_string_cmp(const heim_ia5_string *p,
66 		   const heim_ia5_string *q)
67 {
68     return der_heim_octet_string_cmp(p, q);
69 }
70 
71 int
72 der_heim_bit_string_cmp(const heim_bit_string *p,
73 			const heim_bit_string *q)
74 {
75     int r1, r2;
76     size_t i;
77     if (p->length != q->length)
78 	return (int)(p->length - q->length);
79     i = memcmp(p->data, q->data, p->length / 8);
80     if (i)
81 	return (int)i;
82     if ((p->length % 8) == 0)
83 	return 0;
84     i = (p->length / 8);
85     r1 = ((unsigned char *)p->data)[i];
86     r2 = ((unsigned char *)q->data)[i];
87     i = 8 - (p->length % 8);
88     r1 = r1 >> i;
89     r2 = r2 >> i;
90     return r1 - r2;
91 }
92 
93 int
94 der_heim_integer_cmp(const heim_integer *p,
95 		     const heim_integer *q)
96 {
97     if (p->negative != q->negative)
98 	return q->negative - p->negative;
99     if (p->length != q->length)
100 	return (int)(p->length - q->length);
101     return memcmp(p->data, q->data, p->length);
102 }
103 
104 int
105 der_heim_bmp_string_cmp(const heim_bmp_string *p, const heim_bmp_string *q)
106 {
107     if (p->length != q->length)
108 	return (int)(p->length - q->length);
109     return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
110 }
111 
112 int
113 der_heim_universal_string_cmp(const heim_universal_string *p,
114 			      const heim_universal_string *q)
115 {
116     if (p->length != q->length)
117 	return (int)(p->length - q->length);
118     return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
119 }
120