1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.  See the License for the specific language governing
15  * permissions and limitations under the License.
16  */
17 
18 #include <stdlib.h>
19 #include <errno.h>
20 
21 #include "avro/errors.h"
22 #include "avro/io.h"
23 #include "avro/legacy.h"
24 #include "avro/resolver.h"
25 #include "avro/schema.h"
26 #include "avro/value.h"
27 #include "avro_private.h"
28 
29 int
avro_schema_match(avro_schema_t wschema,avro_schema_t rschema)30 avro_schema_match(avro_schema_t wschema, avro_schema_t rschema)
31 {
32 	check_param(0, is_avro_schema(wschema), "writer schema");
33 	check_param(0, is_avro_schema(rschema), "reader schema");
34 
35 	avro_value_iface_t  *resolver =
36 	    avro_resolved_writer_new(wschema, rschema);
37 	if (resolver != NULL) {
38 		avro_value_iface_decref(resolver);
39 		return 1;
40 	}
41 
42 	return 0;
43 }
44 
45 int
avro_read_data(avro_reader_t reader,avro_schema_t writers_schema,avro_schema_t readers_schema,avro_datum_t * datum)46 avro_read_data(avro_reader_t reader, avro_schema_t writers_schema,
47 	       avro_schema_t readers_schema, avro_datum_t * datum)
48 {
49 	int rval;
50 
51 	check_param(EINVAL, reader, "reader");
52 	check_param(EINVAL, is_avro_schema(writers_schema), "writer schema");
53 	check_param(EINVAL, datum, "datum pointer");
54 
55 	if (!readers_schema) {
56 		readers_schema = writers_schema;
57 	}
58 
59 	avro_datum_t  result = avro_datum_from_schema(readers_schema);
60 	if (!result) {
61 		return EINVAL;
62 	}
63 
64 	avro_value_t  value;
65 	check(rval, avro_datum_as_value(&value, result));
66 
67 	avro_value_iface_t  *resolver =
68 	    avro_resolved_writer_new(writers_schema, readers_schema);
69 	if (!resolver) {
70 		avro_value_decref(&value);
71 		avro_datum_decref(result);
72 		return EINVAL;
73 	}
74 
75 	avro_value_t  resolved_value;
76 	rval = avro_resolved_writer_new_value(resolver, &resolved_value);
77 	if (rval) {
78 		avro_value_iface_decref(resolver);
79 		avro_value_decref(&value);
80 		avro_datum_decref(result);
81 		return rval;
82 	}
83 
84 	avro_resolved_writer_set_dest(&resolved_value, &value);
85 	rval = avro_value_read(reader, &resolved_value);
86 	if (rval) {
87 		avro_value_decref(&resolved_value);
88 		avro_value_iface_decref(resolver);
89 		avro_value_decref(&value);
90 		avro_datum_decref(result);
91 		return rval;
92 	}
93 
94 	avro_value_decref(&resolved_value);
95 	avro_value_iface_decref(resolver);
96 	avro_value_decref(&value);
97 	*datum = result;
98 	return 0;
99 }
100