1 /* Simple Plugin API
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_POD_COMPARE_H
26 #define SPA_POD_COMPARE_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <stdarg.h>
33 #include <errno.h>
34 #include <stdint.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include <spa/param/props.h>
40 #include <spa/pod/iter.h>
41 #include <spa/pod/builder.h>
42 
43 /**
44  * \addtogroup spa_pod
45  * \{
46  */
47 
spa_pod_compare_value(uint32_t type,const void * r1,const void * r2,uint32_t size)48 static inline int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
49 {
50 	switch (type) {
51 	case SPA_TYPE_None:
52 		return 0;
53 	case SPA_TYPE_Bool:
54 	case SPA_TYPE_Id:
55 		return *(uint32_t *) r1 == *(uint32_t *) r2 ? 0 : 1;
56 	case SPA_TYPE_Int:
57 		return *(int32_t *) r1 - *(int32_t *) r2;
58 	case SPA_TYPE_Long:
59 		return *(int64_t *) r1 - *(int64_t *) r2;
60 	case SPA_TYPE_Float:
61 		return *(float *) r1 - *(float *) r2;
62 	case SPA_TYPE_Double:
63 		return *(double *) r1 - *(double *) r2;
64 	case SPA_TYPE_String:
65 		return strcmp((char *)r1, (char *)r2);
66 	case SPA_TYPE_Bytes:
67 		return memcmp((char *)r1, (char *)r2, size);
68 	case SPA_TYPE_Rectangle:
69 	{
70 		const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
71 		    *rec2 = (struct spa_rectangle *) r2;
72 		if (rec1->width == rec2->width && rec1->height == rec2->height)
73 			return 0;
74 		else if (rec1->width < rec2->width || rec1->height < rec2->height)
75 			return -1;
76 		else
77 			return 1;
78 	}
79 	case SPA_TYPE_Fraction:
80 	{
81 		const struct spa_fraction *f1 = (struct spa_fraction *) r1,
82 		    *f2 = (struct spa_fraction *) r2;
83 		int64_t n1, n2;
84 		n1 = ((int64_t) f1->num) * f2->denom;
85 		n2 = ((int64_t) f2->num) * f1->denom;
86 		if (n1 < n2)
87 			return -1;
88 		else if (n1 > n2)
89 			return 1;
90 		else
91 			return 0;
92 	}
93 	default:
94 		break;
95 	}
96 	return 0;
97 }
98 
spa_pod_compare(const struct spa_pod * pod1,const struct spa_pod * pod2)99 static inline int spa_pod_compare(const struct spa_pod *pod1,
100 				  const struct spa_pod *pod2)
101 {
102 	int res = 0;
103 	uint32_t n_vals1, n_vals2;
104 	uint32_t choice1, choice2;
105 
106         spa_return_val_if_fail(pod1 != NULL, -EINVAL);
107         spa_return_val_if_fail(pod2 != NULL, -EINVAL);
108 
109 	pod1 = spa_pod_get_values(pod1,  &n_vals1, &choice1);
110 	pod2 = spa_pod_get_values(pod2,  &n_vals2, &choice2);
111 
112 	if (n_vals1 != n_vals2)
113 		return -EINVAL;
114 
115 	if (SPA_POD_TYPE(pod1) != SPA_POD_TYPE(pod2))
116 		return -EINVAL;
117 
118 	switch (SPA_POD_TYPE(pod1)) {
119 	case SPA_TYPE_Struct:
120 	{
121 		const struct spa_pod *p1, *p2;
122 		size_t p1s, p2s;
123 
124 		p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1);
125 		p1s = SPA_POD_BODY_SIZE(pod1);
126 		p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2);
127 		p2s = SPA_POD_BODY_SIZE(pod2);
128 
129 		while (true) {
130 			if (!spa_pod_is_inside(pod1, p1s, p1) ||
131 			    !spa_pod_is_inside(pod2, p2s, p2))
132 				return -EINVAL;
133 
134 			if ((res = spa_pod_compare(p1, p2)) != 0)
135 				return res;
136 
137 			p1 = (const struct spa_pod*)spa_pod_next(p1);
138 			p2 = (const struct spa_pod*)spa_pod_next(p2);
139 		}
140 		break;
141 	}
142 	case SPA_TYPE_Object:
143 	{
144 		const struct spa_pod_prop *p1, *p2;
145 		const struct spa_pod_object *o1, *o2;
146 
147 		o1 = (const struct spa_pod_object*)pod1;
148 		o2 = (const struct spa_pod_object*)pod2;
149 
150 		p2 = NULL;
151 		SPA_POD_OBJECT_FOREACH(o1, p1) {
152 			if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL)
153 				return 1;
154 			if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0)
155 				return res;
156 		}
157 		p1 = NULL;
158 		SPA_POD_OBJECT_FOREACH(o2, p2) {
159 			if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL)
160 				return -1;
161 		}
162 		break;
163 	}
164 	case SPA_TYPE_Array:
165 	{
166 		if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
167 			return -EINVAL;
168 		res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY_SIZE(pod2));
169 		break;
170 	}
171 	default:
172 		if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
173 			return -EINVAL;
174 		res = spa_pod_compare_value(SPA_POD_TYPE(pod1),
175 				SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
176 				SPA_POD_BODY_SIZE(pod1));
177 		break;
178 	}
179 	return res;
180 }
181 
182 /**
183  * \}
184  */
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif
191