ZOOM: throw present request out of range again YAZ-739
[yaz-moved-to-github.git] / src / copy_types.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file copy_types.c
6     \brief Copies various Z39.50 types
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <string.h>
13 #include <yaz/copy_types.h>
14
15 /** macro clone_z_type copies a given ASN.1 type */
16 #define clone_z_type(x) \
17 Z_##x *yaz_clone_z_##x(Z_##x *q, NMEM nmem_out) \
18 { \
19     Z_##x *q1 = 0; \
20     ODR enc = odr_createmem(ODR_ENCODE); \
21     ODR dec = odr_createmem(ODR_DECODE); \
22     if (z_##x(enc, &q, 0, 0)) \
23     { \
24         int len; \
25         char *buf = odr_getbuf(enc, &len, 0); \
26         if (buf) \
27         { \
28             odr_setbuf(dec, buf, len, 0); \
29             z_##x(dec, &q1, 0, 0); \
30             nmem_transfer(nmem_out, dec->mem);  \
31         } \
32     } \
33     odr_destroy(enc); \
34     odr_destroy(dec); \
35     return q1; \
36 } \
37 int yaz_compare_z_##x(Z_##x *a, Z_##x *b) \
38 { \
39     int ret = 0; \
40     ODR o_a = odr_createmem(ODR_ENCODE); \
41     ODR o_b = odr_createmem(ODR_ENCODE); \
42     int r_a = z_##x(o_a, &a, 1, 0); \
43     int r_b = z_##x(o_b, &b, 1, 0); \
44     if (r_a && r_b) \
45     { \
46         int len_a, len_b; \
47         char *buf_a = odr_getbuf(o_a, &len_a, 0); \
48         char *buf_b = odr_getbuf(o_b, &len_b, 0); \
49         if (buf_a && buf_b && len_a == len_b && !memcmp(buf_a, buf_b, len_a)) \
50             ret = 1; \
51         else if (!buf_a && !buf_b) \
52             ret = 1; \
53     } \
54     odr_destroy(o_a); \
55     odr_destroy(o_b); \
56     return ret; \
57 }
58
59 clone_z_type(NamePlusRecord)
60 clone_z_type(RPNQuery)
61 clone_z_type(Query)
62 clone_z_type(RecordComposition)
63 clone_z_type(OtherInformation)
64
65 Z_RPNQuery *yaz_copy_z_RPNQuery(Z_RPNQuery *q, ODR out)
66 {
67     return yaz_clone_z_RPNQuery(q, out->mem);
68 }
69
70 Z_Query *yaz_copy_Z_Query(Z_Query *q, ODR out)
71 {
72     return yaz_clone_z_Query(q, out->mem);
73 }
74
75 /*
76  * Local variables:
77  * c-basic-offset: 4
78  * c-file-style: "Stroustrup"
79  * indent-tabs-mode: nil
80  * End:
81  * vim: shiftwidth=4 tabstop=8 expandtab
82  */
83