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 #include <string.h>
44
45 #include "../include/sane/sane.h"
46 #include "../include/sane/sanei_wire.h"
47 #include "../include/sane/sanei_codec_bin.h"
48
49 static void
bin_w_byte(Wire * w,void * v)50 bin_w_byte (Wire *w, void *v)
51 {
52 SANE_Byte *b = v;
53
54 sanei_w_space (w, 1);
55 if (w->status)
56 return;
57
58 switch (w->direction)
59 {
60 case WIRE_ENCODE:
61 *w->buffer.curr++ = *b;
62 break;
63
64 case WIRE_DECODE:
65 *b = *w->buffer.curr++;
66 break;
67
68 case WIRE_FREE:
69 break;
70 }
71 }
72
73 static void
bin_w_string(Wire * w,void * v)74 bin_w_string (Wire *w, void *v)
75 {
76 SANE_Word len;
77 SANE_String *s = v;
78
79 if (w->direction != WIRE_DECODE)
80 {
81 len = 0;
82 if (*s)
83 len = strlen (*s) + 1;
84 }
85 sanei_w_array (w, &len, v, w->codec.w_byte, 1);
86
87 if (w->direction == WIRE_DECODE)
88 {
89 if (len == 0)
90 *s = 0;
91 else if (w->status == 0)
92 *(*s + len - 1) = '\0';
93 }
94 }
95
96 static void
bin_w_word(Wire * w,void * v)97 bin_w_word (Wire *w, void *v)
98 {
99 SANE_Word val, *word = v;
100
101 sanei_w_space (w, 4);
102 if (w->status)
103 return;
104 switch (w->direction)
105 {
106 case WIRE_ENCODE:
107 val = *word;
108 /* store in bigendian byte-order: */
109 w->buffer.curr[0] = (val >> 24) & 0xff;
110 w->buffer.curr[1] = (val >> 16) & 0xff;
111 w->buffer.curr[2] = (val >> 8) & 0xff;
112 w->buffer.curr[3] = (val >> 0) & 0xff;
113 w->buffer.curr += 4;
114 break;
115
116 case WIRE_DECODE:
117 val = ( ((w->buffer.curr[0] & 0xff) << 24)
118 | ((w->buffer.curr[1] & 0xff) << 16)
119 | ((w->buffer.curr[2] & 0xff) << 8)
120 | ((w->buffer.curr[3] & 0xff) << 0));
121 *word = val;
122 w->buffer.curr += 4;
123 break;
124
125 case WIRE_FREE:
126 break;
127 }
128 }
129
130 void
sanei_codec_bin_init(Wire * w)131 sanei_codec_bin_init (Wire *w)
132 {
133 w->codec.w_byte = bin_w_byte;
134 w->codec.w_char = bin_w_byte;
135 w->codec.w_word = bin_w_word;
136 w->codec.w_string = bin_w_string;
137 }
138