1 /*	$NetBSD: der_cmp.c,v 1.1.1.2 2014/04/24 12:45:28 pettai 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 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 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 i, r1, r2;
76     if (p->length != q->length)
77 	return p->length - q->length;
78     i = memcmp(p->data, q->data, p->length / 8);
79     if (i)
80 	return i;
81     if ((p->length % 8) == 0)
82 	return 0;
83     i = (p->length / 8);
84     r1 = ((unsigned char *)p->data)[i];
85     r2 = ((unsigned char *)q->data)[i];
86     i = 8 - (p->length % 8);
87     r1 = r1 >> i;
88     r2 = r2 >> i;
89     return r1 - r2;
90 }
91 
92 int
der_heim_integer_cmp(const heim_integer * p,const heim_integer * q)93 der_heim_integer_cmp(const heim_integer *p,
94 		     const heim_integer *q)
95 {
96     if (p->negative != q->negative)
97 	return q->negative - p->negative;
98     if (p->length != q->length)
99 	return p->length - q->length;
100     return memcmp(p->data, q->data, p->length);
101 }
102 
103 int
der_heim_bmp_string_cmp(const heim_bmp_string * p,const heim_bmp_string * q)104 der_heim_bmp_string_cmp(const heim_bmp_string *p, const heim_bmp_string *q)
105 {
106     if (p->length != q->length)
107 	return p->length - q->length;
108     return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
109 }
110 
111 int
der_heim_universal_string_cmp(const heim_universal_string * p,const heim_universal_string * q)112 der_heim_universal_string_cmp(const heim_universal_string *p,
113 			      const heim_universal_string *q)
114 {
115     if (p->length != q->length)
116 	return p->length - q->length;
117     return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
118 }
119