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
spa_pod_compare_value(uint32_t type,const void * r1,const void * r2,uint32_t size)43 static inline int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
44 {
45 switch (type) {
46 case SPA_TYPE_None:
47 return 0;
48 case SPA_TYPE_Bool:
49 case SPA_TYPE_Id:
50 return *(uint32_t *) r1 == *(uint32_t *) r2 ? 0 : 1;
51 case SPA_TYPE_Int:
52 return *(int32_t *) r1 - *(int32_t *) r2;
53 case SPA_TYPE_Long:
54 return *(int64_t *) r1 - *(int64_t *) r2;
55 case SPA_TYPE_Float:
56 return *(float *) r1 - *(float *) r2;
57 case SPA_TYPE_Double:
58 return *(double *) r1 - *(double *) r2;
59 case SPA_TYPE_String:
60 return strcmp((char *)r1, (char *)r2);
61 case SPA_TYPE_Bytes:
62 return memcmp((char *)r1, (char *)r2, size);
63 case SPA_TYPE_Rectangle:
64 {
65 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
66 *rec2 = (struct spa_rectangle *) r2;
67 if (rec1->width == rec2->width && rec1->height == rec2->height)
68 return 0;
69 else if (rec1->width < rec2->width || rec1->height < rec2->height)
70 return -1;
71 else
72 return 1;
73 }
74 case SPA_TYPE_Fraction:
75 {
76 const struct spa_fraction *f1 = (struct spa_fraction *) r1,
77 *f2 = (struct spa_fraction *) r2;
78 int64_t n1, n2;
79 n1 = ((int64_t) f1->num) * f2->denom;
80 n2 = ((int64_t) f2->num) * f1->denom;
81 if (n1 < n2)
82 return -1;
83 else if (n1 > n2)
84 return 1;
85 else
86 return 0;
87 }
88 default:
89 break;
90 }
91 return 0;
92 }
93
spa_pod_compare(const struct spa_pod * pod1,const struct spa_pod * pod2)94 static inline int spa_pod_compare(const struct spa_pod *pod1,
95 const struct spa_pod *pod2)
96 {
97 int res = 0;
98 uint32_t n_vals1, n_vals2;
99 uint32_t choice1, choice2;
100
101 spa_return_val_if_fail(pod1 != NULL, -EINVAL);
102 spa_return_val_if_fail(pod2 != NULL, -EINVAL);
103
104 pod1 = spa_pod_get_values(pod1, &n_vals1, &choice1);
105 pod2 = spa_pod_get_values(pod2, &n_vals2, &choice2);
106
107 if (n_vals1 != n_vals2)
108 return -EINVAL;
109
110 if (SPA_POD_TYPE(pod1) != SPA_POD_TYPE(pod2))
111 return -EINVAL;
112
113 switch (SPA_POD_TYPE(pod1)) {
114 case SPA_TYPE_Struct:
115 {
116 const struct spa_pod *p1, *p2;
117 size_t p1s, p2s;
118
119 p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1);
120 p1s = SPA_POD_BODY_SIZE(pod1);
121 p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2);
122 p2s = SPA_POD_BODY_SIZE(pod2);
123
124 while (true) {
125 if (!spa_pod_is_inside(pod1, p1s, p1) ||
126 !spa_pod_is_inside(pod2, p2s, p2))
127 return -EINVAL;
128
129 if ((res = spa_pod_compare(p1, p2)) != 0)
130 return res;
131
132 p1 = (const struct spa_pod*)spa_pod_next(p1);
133 p2 = (const struct spa_pod*)spa_pod_next(p2);
134 }
135 break;
136 }
137 case SPA_TYPE_Object:
138 {
139 const struct spa_pod_prop *p1, *p2;
140 const struct spa_pod_object *o1, *o2;
141
142 o1 = (const struct spa_pod_object*)pod1;
143 o2 = (const struct spa_pod_object*)pod2;
144
145 p2 = NULL;
146 SPA_POD_OBJECT_FOREACH(o1, p1) {
147 if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL)
148 return 1;
149 if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0)
150 return res;
151 }
152 p1 = NULL;
153 SPA_POD_OBJECT_FOREACH(o2, p2) {
154 if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL)
155 return -1;
156 }
157 break;
158 }
159 case SPA_TYPE_Array:
160 {
161 if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
162 return -EINVAL;
163 res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY_SIZE(pod2));
164 break;
165 }
166 default:
167 if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
168 return -EINVAL;
169 res = spa_pod_compare_value(SPA_POD_TYPE(pod1),
170 SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
171 SPA_POD_BODY_SIZE(pod1));
172 break;
173 }
174 return res;
175 }
176
177 #ifdef __cplusplus
178 }
179 #endif
180
181 #endif
182