Imported Upstream version 3.1.0
[debian/amanda] / ndmp-src / ndmp_translate.c
1 /*
2  * Copyright (c) 2000
3  *      Traakan, Inc., Los Altos, CA
4  *      All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 /*
30  * Project:  NDMJOB
31  * Ident:    $Id: $
32  *
33  * Description:
34  *
35  */
36
37
38 #include "ndmos.h"              /* rpc/rpc.h */
39 #include "ndmprotocol.h"
40 #include "ndmp_msg_buf.h"
41 #include "ndmp_translate.h"
42
43
44
45
46 /*
47  * enum_conversion tables
48  ****************************************************************
49  * Used to make enum conversion convenient and dense.
50  * The first row is the default case in both directions,
51  * and is skipped while attempting precise conversion.
52  * The search stops with the first match.
53  */
54
55 int     /* ndmp9_.... */
56 convert_enum_to_9 (struct enum_conversion *ectab, int enum_x)
57 {
58         struct enum_conversion *        ec = &ectab[1];
59
60         for (; !IS_END_ENUM_CONVERSION_TABLE(ec); ec++) {
61                 if (ec->enum_x == enum_x)
62                         return ec->enum_9;
63         }
64
65         return ectab[0].enum_9;
66 }
67
68 int     /* ndmpx_.... */
69 convert_enum_from_9 (struct enum_conversion *ectab, int enum_9)
70 {
71         struct enum_conversion *        ec = &ectab[1];
72
73         for (; !IS_END_ENUM_CONVERSION_TABLE(ec); ec++) {
74                 if (ec->enum_9 == enum_9)
75                         return ec->enum_x;
76         }
77
78         return ectab[0].enum_x;
79 }
80
81 int
82 convert_valid_u_long_to_9 (u_long *valx, ndmp9_valid_u_long *val9)
83 {
84         val9->value = *valx;
85
86         if (*valx == NDMP_INVALID_U_LONG)
87                 val9->valid = NDMP9_VALIDITY_INVALID;
88         else
89                 val9->valid = NDMP9_VALIDITY_VALID;
90
91         return 0;
92 }
93
94 int
95 convert_valid_u_long_from_9 (u_long *valx, ndmp9_valid_u_long *val9)
96 {
97         if (!val9->valid)
98                 *valx = NDMP_INVALID_U_LONG;
99         else
100                 *valx = val9->value;
101
102         return 0;
103 }
104
105 int
106 convert_invalid_u_long_9 (struct ndmp9_valid_u_long *val9)
107 {
108         val9->value = NDMP_INVALID_U_LONG;
109         val9->valid = NDMP9_VALIDITY_INVALID;
110
111         return 0;
112 }
113
114 int
115 convert_valid_u_quad_to_9 (ndmp9_u_quad *valx, ndmp9_valid_u_quad *val9)
116 {
117         val9->value = *valx;
118
119         if (*valx == NDMP_INVALID_U_QUAD)
120                 val9->valid = NDMP9_VALIDITY_INVALID;
121         else
122                 val9->valid = NDMP9_VALIDITY_VALID;
123
124         return 0;
125 }
126
127 int
128 convert_valid_u_quad_from_9 (ndmp9_u_quad *valx, ndmp9_valid_u_quad *val9)
129 {
130         if (!val9->valid)
131                 *valx = NDMP_INVALID_U_QUAD;
132         else
133                 *valx = val9->value;
134
135         return 0;
136 }
137
138 int
139 convert_invalid_u_quad_9 (struct ndmp9_valid_u_quad *val9)
140 {
141         val9->value = NDMP_INVALID_U_QUAD;
142         val9->valid = NDMP9_VALIDITY_INVALID;
143
144         return 0;
145 }
146
147 int
148 convert_strdup (char *src, char **dstp)
149 {
150         if (src == 0) {
151                 *dstp = 0;
152                 return 0;
153         }
154         *dstp = NDMOS_API_STRDUP (src);
155         if (!*dstp)
156                 return -1;
157
158         return 0;
159 }
160
161
162 /*
163  * request/reply translation tables
164  ****************************************************************
165  */
166
167 struct reqrep_xlate_version_table reqrep_xlate_version_table[] = {
168 #ifndef NDMOS_OPTION_NO_NDMP2
169         { NDMP2VER,     ndmp2_reqrep_xlate_table },
170 #endif /* !NDMOS_OPTION_NO_NDMP2 */
171 #ifndef NDMOS_OPTION_NO_NDMP3
172         { NDMP3VER,     ndmp3_reqrep_xlate_table },
173 #endif /* !NDMOS_OPTION_NO_NDMP3 */
174 #ifndef NDMOS_OPTION_NO_NDMP4
175         { NDMP4VER,     ndmp4_reqrep_xlate_table },
176 #endif /* !NDMOS_OPTION_NO_NDMP4 */
177         { 0 }
178 };
179
180 struct reqrep_xlate *
181 reqrep_xlate_lookup_version (
182   struct reqrep_xlate_version_table *rrvt,
183   unsigned protocol_version)
184 {
185         for (; rrvt->protocol_version > 0; rrvt++) {
186                 if (rrvt->protocol_version == (int)protocol_version) {
187                         return rrvt->reqrep_xlate_table;
188                 }
189         }
190
191         return 0;
192 }
193
194
195 struct reqrep_xlate *
196 ndmp_reqrep_by_v9 (struct reqrep_xlate *table, ndmp9_message v9_message)
197 {
198         struct reqrep_xlate *   rrx = table;
199
200         for (; rrx->v9_message; rrx++)
201                 if (rrx->v9_message == v9_message)
202                         return rrx;
203
204         return 0;
205 }
206
207 struct reqrep_xlate *
208 ndmp_reqrep_by_vx (struct reqrep_xlate *table, int vx_message)
209 {
210         struct reqrep_xlate *   rrx = table;
211
212         for (; rrx->v9_message; rrx++)
213                 if (rrx->vx_message == vx_message)
214                         return rrx;
215
216         return 0;
217 }
218
219 int
220 ndmp_xtox_no_arguments (void *vxbody, void *vybody)
221 {
222         return 0;
223 }
224
225
226 int
227 ndmp_xtox_no_memused (void *vxbody)
228 {
229         return 0;
230 }