• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 1997 David Mosberger-Tang
3    This file is part of the SANE package.
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 
18    As a special exception, the authors of SANE give permission for
19    additional uses of the libraries contained in this release of SANE.
20 
21    The exception is that, if you link a SANE library with other files
22    to produce an executable, this does not by itself cause the
23    resulting executable to be covered by the GNU General Public
24    License.  Your use of that executable is in no way restricted on
25    account of linking the SANE library code into it.
26 
27    This exception does not, however, invalidate any other reasons why
28    the executable file might be covered by the GNU General Public
29    License.
30 
31    If you submit changes to SANE to the maintainers to be included in
32    a subsequent release, you agree by submitting the changes that
33    those changes may be distributed with this exception intact.
34 
35    If you write modifications of your own for SANE, it is your choice
36    whether to permit this exception to apply to your modifications.
37    If you do not wish that, delete this exception notice.  */
38 
39 #include "../include/sane/config.h"
40 
41 #include <errno.h>
42 #include <stdlib.h>
43 
44 #include "../include/sane/sane.h"
45 #include "../include/sane/sanei_net.h"
46 
47 void
sanei_w_init_req(Wire * w,SANE_Init_Req * req)48 sanei_w_init_req (Wire *w, SANE_Init_Req *req)
49 {
50   sanei_w_word (w, &req->version_code);
51   sanei_w_string (w, &req->username);
52 }
53 
54 void
sanei_w_init_reply(Wire * w,SANE_Init_Reply * reply)55 sanei_w_init_reply (Wire *w, SANE_Init_Reply *reply)
56 {
57   sanei_w_status (w, &reply->status);
58   sanei_w_word (w, &reply->version_code);
59 }
60 
61 void
sanei_w_get_devices_reply(Wire * w,SANE_Get_Devices_Reply * reply)62 sanei_w_get_devices_reply (Wire *w, SANE_Get_Devices_Reply *reply)
63 {
64   SANE_Word len;
65 
66   if (w->direction != WIRE_DECODE)
67     {
68       if (reply->device_list)
69 	{
70 	  for (len = 0; reply->device_list[len]; ++len);
71 	  ++len;
72 	}
73       else
74 	len = 0;
75     }
76   sanei_w_status (w, &reply->status);
77   sanei_w_array (w, &len, (void *) &reply->device_list,
78 		 (WireCodecFunc) sanei_w_device_ptr,
79 		 sizeof (reply->device_list[0]));
80 }
81 
82 void
sanei_w_open_reply(Wire * w,SANE_Open_Reply * reply)83 sanei_w_open_reply (Wire *w, SANE_Open_Reply *reply)
84 {
85   sanei_w_status (w, &reply->status);
86   sanei_w_word (w, &reply->handle);
87   sanei_w_string (w, &reply->resource_to_authorize);
88 }
89 
90 static void
w_option_value(Wire * w,SANE_Word type,SANE_Word size,void ** value)91 w_option_value (Wire *w, SANE_Word type, SANE_Word size, void **value)
92 {
93   SANE_Word len, element_size;
94   WireCodecFunc w_value;
95 
96   switch (type)
97     {
98     case SANE_TYPE_BOOL:
99     case SANE_TYPE_INT:
100     case SANE_TYPE_FIXED:
101       w_value = (WireCodecFunc) sanei_w_word;
102       element_size = sizeof (SANE_Word);
103       len = size / element_size;
104       break;
105 
106     case SANE_TYPE_STRING:
107       w_value = (WireCodecFunc) sanei_w_char;
108       element_size = sizeof (SANE_Char);
109       len = size;
110       break;
111 
112     case SANE_TYPE_BUTTON:
113     case SANE_TYPE_GROUP:
114       w_value = (WireCodecFunc) sanei_w_void;
115       len = 0;
116       element_size = 0;
117       break;
118 
119     default:
120       w->status = EINVAL;
121       return;
122     }
123   sanei_w_array (w, &len, value, w_value, element_size);
124 }
125 
126 void
sanei_w_option_descriptor_array(Wire * w,SANE_Option_Descriptor_Array * a)127 sanei_w_option_descriptor_array (Wire *w, SANE_Option_Descriptor_Array *a)
128 {
129   sanei_w_array (w, &a->num_options, (void **) &a->desc,
130 		 (WireCodecFunc) sanei_w_option_descriptor_ptr,
131 		 sizeof (a->desc[0]));
132 }
133 
134 void
sanei_w_control_option_req(Wire * w,SANE_Control_Option_Req * req)135 sanei_w_control_option_req (Wire *w, SANE_Control_Option_Req *req)
136 {
137   sanei_w_word (w, &req->handle);
138   sanei_w_word (w, &req->option);
139   sanei_w_word (w, &req->action);
140 
141   /* Up to and including version 2, we incorrectly attempted to encode
142      the option value even the action was SANE_ACTION_SET_AUTO.  */
143   if (w->version < 3 || req->action != SANE_ACTION_SET_AUTO)
144     {
145       sanei_w_word (w, &req->value_type);
146       sanei_w_word (w, &req->value_size);
147       w_option_value (w, req->value_type, req->value_size, &req->value);
148     }
149 }
150 
151 void
sanei_w_control_option_reply(Wire * w,SANE_Control_Option_Reply * reply)152 sanei_w_control_option_reply (Wire *w, SANE_Control_Option_Reply *reply)
153 {
154   sanei_w_status (w, &reply->status);
155   sanei_w_word (w, &reply->info);
156   sanei_w_word (w, &reply->value_type);
157   sanei_w_word (w, &reply->value_size);
158   w_option_value (w, reply->value_type, reply->value_size, &reply->value);
159   sanei_w_string (w, &reply->resource_to_authorize);
160 }
161 
162 void
sanei_w_get_parameters_reply(Wire * w,SANE_Get_Parameters_Reply * reply)163 sanei_w_get_parameters_reply (Wire *w, SANE_Get_Parameters_Reply *reply)
164 {
165   sanei_w_status (w, &reply->status);
166   sanei_w_parameters (w, &reply->params);
167 }
168 
169 void
sanei_w_start_reply(Wire * w,SANE_Start_Reply * reply)170 sanei_w_start_reply (Wire *w, SANE_Start_Reply *reply)
171 {
172   sanei_w_status (w, &reply->status);
173   sanei_w_word (w, &reply->port);
174   sanei_w_word (w, &reply->byte_order);
175   sanei_w_string (w, &reply->resource_to_authorize);
176 }
177 
178 void
sanei_w_authorization_req(Wire * w,SANE_Authorization_Req * req)179 sanei_w_authorization_req (Wire *w, SANE_Authorization_Req *req)
180 {
181   sanei_w_string (w, &req->resource);
182   sanei_w_string (w, &req->username);
183   sanei_w_string (w, &req->password);
184 }
185