Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2006 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 : #include "common.h"
21 : #include <freetds/tds/iconv.h>
22 : #include <freetds/tds/stream.h>
23 :
24 : #if HAVE_UNISTD_H
25 : #undef getpid
26 : #include <unistd.h>
27 : #endif /* HAVE_UNISTD_H */
28 :
29 : #if HAVE_STDLIB_H
30 : #include <stdlib.h>
31 : #endif /* HAVE_STDLIB_H */
32 :
33 : #include <assert.h>
34 :
35 : /* test tds_bcp_fread */
36 :
37 : static unsigned char buf[4096+80];
38 :
39 10 : TEST_MAIN()
40 : {
41 : static const char out_file[] = "iconv_fread.out";
42 : int i;
43 : FILE *f;
44 10 : TDSCONTEXT *ctx = tds_alloc_context(NULL);
45 10 : TDSSOCKET *tds = tds_alloc_socket(ctx, 512);
46 : TDSICONV *conv;
47 : TDSFILESTREAM stream;
48 : const tds_dir_char *tdsdump;
49 :
50 10 : tdsdump = tds_dir_getenv(TDS_DIR("TDSDUMP"));
51 10 : if (tdsdump)
52 0 : tdsdump_topen(tdsdump);
53 :
54 10 : if (!ctx || !tds) {
55 0 : fprintf(stderr, "Error creating socket!\n");
56 0 : return 1;
57 : }
58 :
59 10 : tds_iconv_open(tds->conn, "ISO-8859-1", 0);
60 :
61 10 : conv = tds_iconv_get(tds->conn, "UTF-8", "ISO-8859-1");
62 10 : if (conv == NULL) {
63 0 : fprintf(stderr, "Error creating conversion, giving up!\n");
64 0 : return 1;
65 : }
66 :
67 10 : f = fopen(out_file, "w+b");
68 10 : if (!f) {
69 0 : fprintf(stderr, "Error opening file!\n");
70 0 : return 1;
71 : }
72 :
73 10 : if (!TDS_SUCCEED(tds_file_stream_init(&stream, f))) {
74 0 : fprintf(stderr, "Error initializing input file stream.\n");
75 0 : return 1;
76 : }
77 :
78 1450 : for (i = 0; i < 4096 + 20; ++i) {
79 720 : char *out = NULL;
80 720 : size_t out_len = 0xdeadbeef;
81 : TDSRET res;
82 720 : const unsigned char x = 0x90;
83 :
84 720 : if (i == 32)
85 10 : i = 4096-20;
86 :
87 : /* write test string to file */
88 720 : if (fseek(f, 0L, SEEK_SET)) {
89 0 : fprintf(stderr, "Error seeking!\n");
90 0 : return 1;
91 : }
92 720 : memset(buf, 'a', i);
93 720 : buf[i] = (char) (0xC0 + (x >> 6));
94 720 : buf[i+1] = (char) (0x80 + (x & 0x3f));
95 720 : buf[i+2] = '!';
96 720 : buf[i+3] = '!';
97 :
98 720 : fwrite(buf, 1, i+4, f);
99 720 : if (fseek(f, 0L, SEEK_SET)) {
100 0 : fprintf(stderr, "Error seeking!\n");
101 0 : return 1;
102 : }
103 :
104 : /* convert it */
105 720 : res = tds_bcp_fread(NULL, conv, &stream, "!!", 2, &out, &out_len);
106 720 : printf("res %d out_len %u\n", (int) res, (unsigned int) out_len);
107 :
108 : /* test */
109 720 : memset(buf, 'a', i);
110 720 : buf[i] = x;
111 720 : assert(TDS_SUCCEED(res));
112 720 : if (out_len != i+1) {
113 0 : fprintf(stderr, "out %u bytes expected %d\n",
114 : (unsigned int) out_len, i+1);
115 0 : return 1;
116 : }
117 720 : assert(memcmp(out, buf, i+1) == 0);
118 720 : free(out);
119 : }
120 10 : fclose(f);
121 10 : unlink(out_file);
122 :
123 10 : tds_free_socket(tds);
124 10 : tds_free_context(ctx);
125 10 : return 0;
126 : }
|