Fixed some minor mismatched types found by splint.
[platform/upstream/curl.git] / lib / dict.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifndef CURL_DISABLE_DICT
27
28 /* -- WIN32 approved -- */
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #ifdef WIN32
36 #include <time.h>
37 #include <io.h>
38 #else
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #include <netinet/in.h>
43 #ifdef HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include <netdb.h>
50 #ifdef HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
52 #endif
53 #ifdef HAVE_NET_IF_H
54 #include <net/if.h>
55 #endif
56 #include <sys/ioctl.h>
57 #include <signal.h>
58
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
61 #endif
62
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
65 #endif
66
67
68 #endif
69
70 #include "urldata.h"
71 #include <curl/curl.h>
72 #include "transfer.h"
73 #include "sendf.h"
74
75 #include "progress.h"
76 #include "strequal.h"
77 #include "dict.h"
78
79 #define _MPRINTF_REPLACE /* use our functions only */
80 #include <curl/mprintf.h>
81
82 /* The last #include file should be: */
83 #include "memdebug.h"
84
85 static char *unescape_word(struct SessionHandle *data, const char *inp)
86 {
87   char *newp;
88   char *dictp;
89   char *ptr;
90   int len;
91   char byte;
92   int olen=0;
93
94   newp = curl_easy_unescape(data, inp, 0, &len);
95   if(!newp)
96     return NULL;
97
98   dictp = malloc(len*2 + 1); /* add one for terminating zero */
99   if(dictp) {
100     /* According to RFC2229 section 2.2, these letters need to be escaped with
101        \[letter] */
102     for(ptr = newp;
103         (byte = *ptr) != 0;
104         ptr++) {
105       if ((byte <= 32) || (byte == 127) ||
106           (byte == '\'') || (byte == '\"') || (byte == '\\')) {
107         dictp[olen++] = '\\';
108       }
109       dictp[olen++] = byte;
110     }
111     dictp[olen]=0;
112
113     free(newp);
114   }
115   return dictp;
116 }
117
118 CURLcode Curl_dict(struct connectdata *conn, bool *done)
119 {
120   char *word;
121   char *eword;
122   char *ppath;
123   char *database = NULL;
124   char *strategy = NULL;
125   char *nthdef = NULL; /* This is not part of the protocol, but required
126                           by RFC 2229 */
127   CURLcode result=CURLE_OK;
128   struct SessionHandle *data=conn->data;
129   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
130
131   char *path = data->reqdata.path;
132   curl_off_t *bytecount = &data->reqdata.keep.bytecount;
133
134   *done = TRUE; /* unconditionally */
135
136   if(conn->bits.user_passwd) {
137     /* AUTH is missing */
138   }
139
140   if (strnequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
141       strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
142       strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
143
144     word = strchr(path, ':');
145     if (word) {
146       word++;
147       database = strchr(word, ':');
148       if (database) {
149         *database++ = (char)0;
150         strategy = strchr(database, ':');
151         if (strategy) {
152           *strategy++ = (char)0;
153           nthdef = strchr(strategy, ':');
154           if (nthdef) {
155             *nthdef++ = (char)0;
156           }
157         }
158       }
159     }
160
161     if ((word == NULL) || (*word == (char)0)) {
162       infof(data, "lookup word is missing");
163       word=(char *)"default";
164     }
165     if ((database == NULL) || (*database == (char)0)) {
166       database = (char *)"!";
167     }
168     if ((strategy == NULL) || (*strategy == (char)0)) {
169       strategy = (char *)".";
170     }
171
172     eword = unescape_word(data, word);
173     if(!eword)
174       return CURLE_OUT_OF_MEMORY;
175
176     result = Curl_sendf(sockfd, conn,
177                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
178                         "MATCH "
179                         "%s "    /* database */
180                         "%s "    /* strategy */
181                         "%s\r\n" /* word */
182                         "QUIT\r\n",
183
184                         database,
185                         strategy,
186                         eword
187                         );
188
189     free(eword);
190
191     if(result)
192       failf(data, "Failed sending DICT request");
193     else
194       result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
195                                    -1, NULL); /* no upload */
196     if(result)
197       return result;
198   }
199   else if (strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
200            strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
201            strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
202
203     word = strchr(path, ':');
204     if (word) {
205       word++;
206       database = strchr(word, ':');
207       if (database) {
208         *database++ = (char)0;
209         nthdef = strchr(database, ':');
210         if (nthdef) {
211           *nthdef++ = (char)0;
212         }
213       }
214     }
215
216     if ((word == NULL) || (*word == (char)0)) {
217       infof(data, "lookup word is missing");
218       word=(char *)"default";
219     }
220     if ((database == NULL) || (*database == (char)0)) {
221       database = (char *)"!";
222     }
223
224     eword = unescape_word(data, word);
225     if(!eword)
226       return CURLE_OUT_OF_MEMORY;
227
228     result = Curl_sendf(sockfd, conn,
229                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
230                         "DEFINE "
231                         "%s "     /* database */
232                         "%s\r\n"  /* word */
233                         "QUIT\r\n",
234                         database,
235                         eword);
236
237     free(eword);
238
239     if(result)
240       failf(data, "Failed sending DICT request");
241     else
242       result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
243                                    -1, NULL); /* no upload */
244
245     if(result)
246       return result;
247
248   }
249   else {
250
251     ppath = strchr(path, '/');
252     if (ppath) {
253       int i;
254
255       ppath++;
256       for (i = 0; ppath[i]; i++) {
257         if (ppath[i] == ':')
258           ppath[i] = ' ';
259       }
260       result = Curl_sendf(sockfd, conn,
261                           "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
262                           "%s\r\n"
263                           "QUIT\r\n", ppath);
264       if(result)
265         failf(data, "Failed sending DICT request");
266       else
267         result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
268                                      -1, NULL);
269       if(result)
270         return result;
271     }
272   }
273
274   return CURLE_OK;
275 }
276 #endif /*CURL_DISABLE_DICT*/