Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2013 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : /**
21 : * \file
22 : * \brief Handle stream of data
23 : */
24 :
25 : #include <config.h>
26 :
27 : #if HAVE_ERRNO_H
28 : #include <errno.h>
29 : #endif /* HAVE_ERRNO_H */
30 :
31 : #if HAVE_STDLIB_H
32 : #include <stdlib.h>
33 : #endif /* HAVE_STDLIB_H */
34 :
35 : #if HAVE_STRING_H
36 : #include <string.h>
37 : #endif /* HAVE_STRING_H */
38 :
39 : #if HAVE_UNISTD_H
40 : #include <unistd.h>
41 : #endif /* HAVE_UNISTD_H */
42 :
43 : #include <assert.h>
44 :
45 : #include <freetds/tds.h>
46 : #include <freetds/iconv.h>
47 : #include <freetds/stream.h>
48 :
49 : /** \cond HIDDEN_SYMBOLS */
50 : #if ENABLE_EXTRA_CHECKS
51 : # define TEMP_INIT(s) const size_t temp_size = s; char* temp = tds_new(char, temp_size)
52 : # define TEMP_FREE free(temp);
53 : # define TEMP_SIZE temp_size
54 : #else
55 : # define TEMP_INIT(s) char temp[s]
56 : # define TEMP_FREE ;
57 : # define TEMP_SIZE sizeof(temp)
58 : #endif
59 : /** \endcond */
60 :
61 : /**
62 : * Reads and writes from a stream converting characters
63 : * \tds
64 : * \param char_conv conversion structure
65 : * \param direction specify conversion to server or from server
66 : * \param istream input stream
67 : * \param ostream output stream
68 : * \return TDS_SUCCESS of TDS_FAIL
69 : */
70 : TDSRET
71 691881 : tds_convert_stream(TDSSOCKET * tds, TDSICONV * char_conv, TDS_ICONV_DIRECTION direction,
72 : TDSINSTREAM * istream, TDSOUTSTREAM *ostream)
73 : {
74 691881 : TEMP_INIT(4096);
75 : /*
76 : * temp (above) is the "preconversion" buffer, the place where the UCS-2 data
77 : * are parked before converting them to ASCII. It has to have a size,
78 : * and there's no advantage to allocating dynamically.
79 : * This also avoids any memory allocation error.
80 : */
81 : const char *ib;
82 691881 : size_t bufleft = 0;
83 691881 : TDSRET res = TDS_FAIL;
84 :
85 : /* cast away const for message suppression sub-structure */
86 691881 : TDS_ERRNO_MESSAGE_FLAGS *suppress = (TDS_ERRNO_MESSAGE_FLAGS*) &char_conv->suppress;
87 :
88 691881 : memset(suppress, 0, sizeof(char_conv->suppress));
89 1338422 : for (ib = temp; ostream->buf_len; ib = temp + bufleft) {
90 :
91 : char *ob;
92 : int len, conv_errno;
93 : size_t ol;
94 :
95 1127022 : assert(ib >= temp);
96 :
97 : /* read a chunk of data */
98 1127022 : len = istream->read(istream, (char*) ib, TEMP_SIZE - bufleft);
99 1127022 : if (len < 0)
100 : break;
101 1127022 : if (len == 0 && bufleft == 0) {
102 : res = TDS_SUCCESS;
103 : break;
104 : }
105 708910 : bufleft += len;
106 :
107 : /* Convert chunk */
108 708910 : ib = temp; /* always convert from start of buffer */
109 :
110 826982 : convert_more:
111 767946 : ob = ostream->buffer;
112 767946 : ol = ostream->buf_len;
113 : /* FIXME not for last */
114 767946 : suppress->einval = 1; /* EINVAL matters only on the last chunk. */
115 767946 : suppress->e2big = 1;
116 767946 : ol = tds_iconv(tds, char_conv, direction, (const char **) &ib, &bufleft, &ob, &ol);
117 767946 : conv_errno = errno;
118 :
119 : /* write converted chunk */
120 767946 : len = ostream->write(ostream, ob - ostream->buffer);
121 767946 : if (TDS_UNLIKELY(len < 0))
122 : break;
123 :
124 767946 : if ((size_t) -1 == ol) {
125 182550 : tdsdump_log(TDS_DBG_NETWORK, "Error: tds_convert_stream: tds_iconv returned errno %d, conv_errno %d\n", errno, conv_errno);
126 182550 : if (conv_errno == E2BIG && ostream->buf_len && bufleft && len)
127 : goto convert_more;
128 123514 : if (conv_errno != EILSEQ) {
129 117672 : tdsdump_log(TDS_DBG_NETWORK, "Error: tds_convert_stream: "
130 : "Gave up converting %u bytes due to error %d.\n",
131 : (unsigned int) bufleft, errno);
132 117672 : tdsdump_dump_buf(TDS_DBG_NETWORK, "Troublesome bytes:", ib, bufleft);
133 : }
134 :
135 123514 : if (TDS_UNLIKELY(ib == temp)) { /* tds_iconv did not convert anything, avoid infinite loop */
136 62369 : tdsdump_log(TDS_DBG_NETWORK, "No conversion possible: some bytes left.\n");
137 62369 : res = TDS_FAIL;
138 62369 : if (conv_errno == EINVAL && tds)
139 592 : tdserror(tds_get_ctx(tds), tds, TDSEICONVAVAIL, 0);
140 62369 : if (conv_errno == E2BIG && tds)
141 28824 : tdserror(tds_get_ctx(tds), tds, TDSEICONVIU, 0);
142 62369 : errno = conv_errno;
143 62369 : break;
144 : }
145 :
146 61145 : if (bufleft)
147 61145 : memmove(temp, ib, bufleft);
148 : }
149 : }
150 :
151 691881 : TEMP_FREE;
152 691881 : return res;
153 : }
154 :
155 : /**
156 : * Reads and writes from a stream to another
157 : * \tds
158 : * \param istream input stream
159 : * \param ostream output stream
160 : * \return TDS_SUCCESS or TDS_FAIL
161 : */
162 : TDSRET
163 2576 : tds_copy_stream(TDSINSTREAM * istream, TDSOUTSTREAM * ostream)
164 : {
165 8284 : while (ostream->buf_len) {
166 : /* read a chunk of data */
167 5708 : int len = istream->read(istream, ostream->buffer, ostream->buf_len);
168 5708 : if (len == 0)
169 : return TDS_SUCCESS;
170 3132 : if (TDS_UNLIKELY(len < 0))
171 : break;
172 :
173 : /* write chunk */
174 3132 : len = ostream->write(ostream, len);
175 3132 : if (TDS_UNLIKELY(len < 0))
176 : break;
177 : }
178 : return TDS_FAIL;
179 : }
180 :
181 : /**
182 : * Reads data from network for input stream
183 : */
184 : static int
185 808016 : tds_datain_stream_read(TDSINSTREAM *stream, void *ptr, size_t len)
186 : {
187 808016 : TDSDATAINSTREAM *s = (TDSDATAINSTREAM *) stream;
188 808016 : if (len > s->wire_size)
189 807260 : len = s->wire_size;
190 808016 : if (!tds_get_n(s->tds, ptr, len))
191 : return -1;
192 808016 : s->wire_size -= len;
193 808016 : return len;
194 : }
195 :
196 : /**
197 : * Initialize a data input stream.
198 : * This stream read data from network.
199 : * \param stream input stream to initialize
200 : * \tds
201 : * \param wire_size byte to read
202 : */
203 : void
204 532157 : tds_datain_stream_init(TDSDATAINSTREAM * stream, TDSSOCKET * tds, size_t wire_size)
205 : {
206 532157 : stream->stream.read = tds_datain_stream_read;
207 532157 : stream->wire_size = wire_size;
208 532157 : stream->tds = tds;
209 532157 : }
210 :
211 : /**
212 : * Writes data to network for output stream
213 : */
214 : static int
215 73969 : tds_dataout_stream_write(TDSOUTSTREAM *stream, size_t len)
216 : {
217 73969 : TDSDATAOUTSTREAM *s = (TDSDATAOUTSTREAM *) stream;
218 73969 : TDSSOCKET *tds = s->tds;
219 :
220 73969 : assert(len <= stream->buf_len);
221 73969 : assert(stream->buffer == (char *) tds->out_buf + tds->out_pos);
222 73969 : assert(stream->buf_len == tds->out_buf_max - tds->out_pos + TDS_ADDITIONAL_SPACE);
223 :
224 73969 : tds->out_pos += len;
225 : /* this must be strictly test as equal means we send a full packet
226 : * and we could be just at the end of packet so server would
227 : * wait for another packet with flag != 0
228 : */
229 73969 : if (tds->out_pos > tds->out_buf_max)
230 436 : tds_write_packet(tds, 0x0);
231 73969 : stream->buffer = (char *) tds->out_buf + tds->out_pos;
232 73969 : stream->buf_len = tds->out_buf_max - tds->out_pos + TDS_ADDITIONAL_SPACE;
233 73969 : s->written += len;
234 73969 : return len;
235 : }
236 :
237 : /**
238 : * Initialize a data output stream.
239 : * This stream writes data to network.
240 : * \param stream output stream to initialize
241 : * \tds
242 : */
243 : void
244 77036 : tds_dataout_stream_init(TDSDATAOUTSTREAM * stream, TDSSOCKET * tds)
245 : {
246 : #if TDS_ADDITIONAL_SPACE < 4
247 : #error Not supported
248 : #endif
249 : /*
250 : * we use the extra space as we want possible space for converting
251 : * a character and cause we don't want to send an exactly entire
252 : * packet with 0 flag and then nothing
253 : */
254 77036 : size_t left = tds->out_buf_max - tds->out_pos + TDS_ADDITIONAL_SPACE;
255 :
256 77036 : assert(left > 0);
257 77036 : stream->stream.write = tds_dataout_stream_write;
258 77036 : stream->stream.buffer = (char *) tds->out_buf + tds->out_pos;
259 77036 : stream->stream.buf_len = left;
260 77036 : stream->written = 0;
261 77036 : stream->tds = tds;
262 77036 : }
263 :
264 : /**
265 : * Reads data from a static allocated buffer
266 : */
267 : static int
268 320804 : tds_staticin_stream_read(TDSINSTREAM *stream, void *ptr, size_t len)
269 : {
270 320804 : TDSSTATICINSTREAM *s = (TDSSTATICINSTREAM *) stream;
271 320804 : size_t cp = (len <= s->buf_left) ? len : s->buf_left;
272 :
273 320804 : memcpy(ptr, s->buffer, cp);
274 320804 : s->buffer += cp;
275 320804 : s->buf_left -= cp;
276 320804 : return cp;
277 : }
278 :
279 : /**
280 : * Initialize an input stream for read from a static allocated buffer
281 : * \param stream stream to initialize
282 : * \param ptr buffer to read from
283 : * \param len buffer size in bytes
284 : */
285 : void
286 160474 : tds_staticin_stream_init(TDSSTATICINSTREAM * stream, const void *ptr, size_t len)
287 : {
288 160474 : stream->stream.read = tds_staticin_stream_read;
289 160474 : stream->buffer = (const char *) ptr;
290 160474 : stream->buf_left = len;
291 160474 : }
292 :
293 :
294 : /**
295 : * Writes data to a static allocated buffer
296 : */
297 : static int
298 677745 : tds_staticout_stream_write(TDSOUTSTREAM *stream, size_t len)
299 : {
300 677745 : assert(stream->buf_len >= len);
301 677745 : stream->buffer += len;
302 677745 : stream->buf_len -= len;
303 677745 : return len;
304 : }
305 :
306 : /**
307 : * Initialize an output stream for write into a static allocated buffer
308 : * \param stream stream to initialize
309 : * \param ptr buffer to write to
310 : * \param len buffer size in bytes
311 : */
312 : void
313 599795 : tds_staticout_stream_init(TDSSTATICOUTSTREAM * stream, void *ptr, size_t len)
314 : {
315 599795 : stream->stream.write = tds_staticout_stream_write;
316 599795 : stream->stream.buffer = (char *) ptr;
317 599795 : stream->stream.buf_len = len;
318 599795 : }
319 :
320 : /**
321 : * Writes data to a dynamic allocated buffer
322 : */
323 : static int
324 44945 : tds_dynamic_stream_write(TDSOUTSTREAM *stream, size_t len)
325 : {
326 44945 : TDSDYNAMICSTREAM *s = (TDSDYNAMICSTREAM *) stream;
327 : size_t wanted;
328 :
329 44945 : s->size += len;
330 : /* grow linearly till some limit then exponentially */
331 44945 : if (s->size + 256 > s->allocated) {
332 2244 : wanted = s->size + (s->size < 4096 ? 1024 : s->size / 8u);
333 2244 : if (TDS_UNLIKELY(!tds_realloc(s->buf, wanted)))
334 : return -1;
335 2244 : s->allocated = wanted;
336 : }
337 44945 : assert(s->allocated > s->size);
338 44945 : stream->buffer = (char *) *s->buf + s->size;
339 44945 : stream->buf_len = s->allocated - s->size;
340 44945 : return len;
341 : }
342 :
343 : /**
344 : * Initialize a dynamic output stream.
345 : * This stream write data into a dynamic allocated buffer.
346 : * \param stream stream to initialize
347 : * \param ptr pointer to pointer to buffer to fill. Buffer
348 : * will be extended as needed
349 : * \param allocated bytes initialially allocated for the buffer.
350 : * Useful to reuse buffers
351 : * \return TDS_SUCCESS on success, TDS_FAIL otherwise
352 : */
353 : TDSRET
354 6721 : tds_dynamic_stream_init(TDSDYNAMICSTREAM * stream, void **ptr, size_t allocated)
355 : {
356 6721 : const size_t initial_size = 1024;
357 :
358 6721 : stream->stream.write = tds_dynamic_stream_write;
359 6721 : stream->buf = ptr;
360 6721 : if (allocated < initial_size) {
361 6337 : free(*ptr);
362 6337 : *ptr = NULL;
363 6337 : allocated = initial_size;
364 : }
365 6721 : if (!*ptr) {
366 6697 : *ptr = malloc(allocated);
367 6697 : if (TDS_UNLIKELY(!*ptr))
368 : return TDS_FAIL;
369 : }
370 6721 : stream->allocated = allocated;
371 6721 : stream->size = 0;
372 6721 : stream->stream.buffer = (char *) *ptr;
373 6721 : stream->stream.buf_len = allocated;
374 6721 : return TDS_SUCCESS;
375 : }
376 :
377 :
|