xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/asn1/der_cmp.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: der_cmp.c,v 1.3 2023/06/19 21:41:42 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
der_heim_oid_cmp(const heim_oid * p,const heim_oid * q)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
der_heim_octet_string_cmp(const heim_octet_string * p,const heim_octet_string * q)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
der_printable_string_cmp(const heim_printable_string * p,const heim_printable_string * q)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
der_ia5_string_cmp(const heim_ia5_string * p,const heim_ia5_string * q)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
der_heim_bit_string_cmp(const heim_bit_string * p,const heim_bit_string * q)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     if (q->length)
80         i = memcmp(p->data, q->data, p->length / 8);
81     else
82         i = 0;
83     if (i != 0)
84 	return (int)i;
85     if ((p->length % 8) == 0)
86 	return 0;
87     i = (p->length / 8);
88     r1 = ((unsigned char *)p->data)[i];
89     r2 = ((unsigned char *)q->data)[i];
90     i = 8 - (p->length % 8);
91     r1 = r1 >> i;
92     r2 = r2 >> i;
93     return r1 - r2;
94 }
95 
96 int
der_heim_integer_cmp(const heim_integer * p,const heim_integer * q)97 der_heim_integer_cmp(const heim_integer *p,
98 		     const heim_integer *q)
99 {
100     if (p->negative != q->negative)
101 	return q->negative - p->negative;
102     if (p->length != q->length)
103 	return (int)(p->length - q->length);
104     return memcmp(p->data, q->data, p->length);
105 }
106 
107 int
der_heim_bmp_string_cmp(const heim_bmp_string * p,const heim_bmp_string * q)108 der_heim_bmp_string_cmp(const heim_bmp_string *p, const heim_bmp_string *q)
109 {
110     if (p->length != q->length)
111 	return (int)(p->length - q->length);
112     return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
113 }
114 
115 int
der_heim_universal_string_cmp(const heim_universal_string * p,const heim_universal_string * q)116 der_heim_universal_string_cmp(const heim_universal_string *p,
117 			      const heim_universal_string *q)
118 {
119     if (p->length != q->length)
120 	return (int)(p->length - q->length);
121     return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
122 }
123