27567a1ac71e59c030c4fe71428e523b10c062d2
[platform/upstream/curl.git] / lib / curl_strerror.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2004 - 2012, 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  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifdef HAVE_STRERROR_R
26 #  if (!defined(HAVE_POSIX_STRERROR_R) && \
27        !defined(HAVE_GLIBC_STRERROR_R) && \
28        !defined(HAVE_VXWORKS_STRERROR_R)) || \
29       (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)) || \
30       (defined(HAVE_GLIBC_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)) || \
31       (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R))
32 #    error "strerror_r MUST be either POSIX, glibc or vxworks-style"
33 #  endif
34 #endif
35
36 #include <curl/curl.h>
37
38 #ifdef USE_LIBIDN
39 #include <idna.h>
40 #endif
41
42 #include "curl_strerror.h"
43
44 #define _MPRINTF_REPLACE /* use our functions only */
45 #include <curl/mprintf.h>
46
47 #include "curl_memory.h"
48 /* The last #include file should be: */
49 #include "curl_memdebug.h"
50
51 const char *
52 curl_easy_strerror(CURLcode error)
53 {
54 #ifndef CURL_DISABLE_VERBOSE_STRINGS
55   switch (error) {
56   case CURLE_OK:
57     return "No error";
58
59   case CURLE_UNSUPPORTED_PROTOCOL:
60     return "Unsupported protocol";
61
62   case CURLE_FAILED_INIT:
63     return "Failed initialization";
64
65   case CURLE_URL_MALFORMAT:
66     return "URL using bad/illegal format or missing URL";
67
68   case CURLE_NOT_BUILT_IN:
69     return "A requested feature, protocol or option was not found built-in in"
70       " this libcurl due to a build-time decision.";
71
72   case CURLE_COULDNT_RESOLVE_PROXY:
73     return "Couldn't resolve proxy name";
74
75   case CURLE_COULDNT_RESOLVE_HOST:
76     return "Couldn't resolve host name";
77
78   case CURLE_COULDNT_CONNECT:
79     return "Couldn't connect to server";
80
81   case CURLE_FTP_WEIRD_SERVER_REPLY:
82     return "FTP: weird server reply";
83
84   case CURLE_REMOTE_ACCESS_DENIED:
85     return "Access denied to remote resource";
86
87   case CURLE_FTP_ACCEPT_FAILED:
88     return "FTP: The server failed to connect to data port";
89
90   case CURLE_FTP_ACCEPT_TIMEOUT:
91     return "FTP: Accepting server connect has timed out";
92
93   case CURLE_FTP_PRET_FAILED:
94     return "FTP: The server did not accept the PRET command.";
95
96   case CURLE_FTP_WEIRD_PASS_REPLY:
97     return "FTP: unknown PASS reply";
98
99   case CURLE_FTP_WEIRD_PASV_REPLY:
100     return "FTP: unknown PASV reply";
101
102   case CURLE_FTP_WEIRD_227_FORMAT:
103     return "FTP: unknown 227 response format";
104
105   case CURLE_FTP_CANT_GET_HOST:
106     return "FTP: can't figure out the host in the PASV response";
107
108   case CURLE_FTP_COULDNT_SET_TYPE:
109     return "FTP: couldn't set file type";
110
111   case CURLE_PARTIAL_FILE:
112     return "Transferred a partial file";
113
114   case CURLE_FTP_COULDNT_RETR_FILE:
115     return "FTP: couldn't retrieve (RETR failed) the specified file";
116
117   case CURLE_QUOTE_ERROR:
118     return "Quote command returned error";
119
120   case CURLE_HTTP_RETURNED_ERROR:
121     return "HTTP response code said error";
122
123   case CURLE_WRITE_ERROR:
124     return "Failed writing received data to disk/application";
125
126   case CURLE_UPLOAD_FAILED:
127     return "Upload failed (at start/before it took off)";
128
129   case CURLE_READ_ERROR:
130     return "Failed to open/read local data from file/application";
131
132   case CURLE_OUT_OF_MEMORY:
133     return "Out of memory";
134
135   case CURLE_OPERATION_TIMEDOUT:
136     return "Timeout was reached";
137
138   case CURLE_FTP_PORT_FAILED:
139     return "FTP: command PORT failed";
140
141   case CURLE_FTP_COULDNT_USE_REST:
142     return "FTP: command REST failed";
143
144   case CURLE_RANGE_ERROR:
145     return "Requested range was not delivered by the server";
146
147   case CURLE_HTTP_POST_ERROR:
148     return "Internal problem setting up the POST";
149
150   case CURLE_SSL_CONNECT_ERROR:
151     return "SSL connect error";
152
153   case CURLE_BAD_DOWNLOAD_RESUME:
154     return "Couldn't resume download";
155
156   case CURLE_FILE_COULDNT_READ_FILE:
157     return "Couldn't read a file:// file";
158
159   case CURLE_LDAP_CANNOT_BIND:
160     return "LDAP: cannot bind";
161
162   case CURLE_LDAP_SEARCH_FAILED:
163     return "LDAP: search failed";
164
165   case CURLE_FUNCTION_NOT_FOUND:
166     return "A required function in the library was not found";
167
168   case CURLE_ABORTED_BY_CALLBACK:
169     return "Operation was aborted by an application callback";
170
171   case CURLE_BAD_FUNCTION_ARGUMENT:
172     return "A libcurl function was given a bad argument";
173
174   case CURLE_INTERFACE_FAILED:
175     return "Failed binding local connection end";
176
177   case CURLE_TOO_MANY_REDIRECTS :
178     return "Number of redirects hit maximum amount";
179
180   case CURLE_UNKNOWN_OPTION:
181     return "An unknown option was passed in to libcurl";
182
183   case CURLE_TELNET_OPTION_SYNTAX :
184     return "Malformed telnet option";
185
186   case CURLE_PEER_FAILED_VERIFICATION:
187     return "SSL peer certificate or SSH remote key was not OK";
188
189   case CURLE_GOT_NOTHING:
190     return "Server returned nothing (no headers, no data)";
191
192   case CURLE_SSL_ENGINE_NOTFOUND:
193     return "SSL crypto engine not found";
194
195   case CURLE_SSL_ENGINE_SETFAILED:
196     return "Can not set SSL crypto engine as default";
197
198   case CURLE_SSL_ENGINE_INITFAILED:
199     return "Failed to initialise SSL crypto engine";
200
201   case CURLE_SEND_ERROR:
202     return "Failed sending data to the peer";
203
204   case CURLE_RECV_ERROR:
205     return "Failure when receiving data from the peer";
206
207   case CURLE_SSL_CERTPROBLEM:
208     return "Problem with the local SSL certificate";
209
210   case CURLE_SSL_CIPHER:
211     return "Couldn't use specified SSL cipher";
212
213   case CURLE_SSL_CACERT:
214     return "Peer certificate cannot be authenticated with given CA "
215       "certificates";
216
217   case CURLE_SSL_CACERT_BADFILE:
218     return "Problem with the SSL CA cert (path? access rights?)";
219
220   case CURLE_BAD_CONTENT_ENCODING:
221     return "Unrecognized or bad HTTP Content or Transfer-Encoding";
222
223   case CURLE_LDAP_INVALID_URL:
224     return "Invalid LDAP URL";
225
226   case CURLE_FILESIZE_EXCEEDED:
227     return "Maximum file size exceeded";
228
229   case CURLE_USE_SSL_FAILED:
230     return "Requested SSL level failed";
231
232   case CURLE_SSL_SHUTDOWN_FAILED:
233     return "Failed to shut down the SSL connection";
234
235   case CURLE_SSL_CRL_BADFILE:
236     return "Failed to load CRL file (path? access rights?, format?)";
237
238   case CURLE_SSL_ISSUER_ERROR:
239     return "Issuer check against peer certificate failed";
240
241   case CURLE_SEND_FAIL_REWIND:
242     return "Send failed since rewinding of the data stream failed";
243
244   case CURLE_LOGIN_DENIED:
245     return "Login denied";
246
247   case CURLE_TFTP_NOTFOUND:
248     return "TFTP: File Not Found";
249
250   case CURLE_TFTP_PERM:
251     return "TFTP: Access Violation";
252
253   case CURLE_REMOTE_DISK_FULL:
254     return "Disk full or allocation exceeded";
255
256   case CURLE_TFTP_ILLEGAL:
257     return "TFTP: Illegal operation";
258
259   case CURLE_TFTP_UNKNOWNID:
260     return "TFTP: Unknown transfer ID";
261
262   case CURLE_REMOTE_FILE_EXISTS:
263     return "Remote file already exists";
264
265   case CURLE_TFTP_NOSUCHUSER:
266     return "TFTP: No such user";
267
268   case CURLE_CONV_FAILED:
269     return "Conversion failed";
270
271   case CURLE_CONV_REQD:
272     return "Caller must register CURLOPT_CONV_ callback options";
273
274   case CURLE_REMOTE_FILE_NOT_FOUND:
275     return "Remote file not found";
276
277   case CURLE_SSH:
278     return "Error in the SSH layer";
279
280   case CURLE_AGAIN:
281     return "Socket not ready for send/recv";
282
283   case CURLE_RTSP_CSEQ_ERROR:
284     return "RTSP CSeq mismatch or invalid CSeq";
285
286   case CURLE_RTSP_SESSION_ERROR:
287     return "RTSP session error";
288
289   case CURLE_FTP_BAD_FILE_LIST:
290     return "Unable to parse FTP file list";
291
292   case CURLE_CHUNK_FAILED:
293     return "Chunk callback failed";
294
295     /* error codes not used by current libcurl */
296   case CURLE_OBSOLETE16:
297   case CURLE_OBSOLETE20:
298   case CURLE_OBSOLETE24:
299   case CURLE_OBSOLETE29:
300   case CURLE_OBSOLETE32:
301   case CURLE_OBSOLETE40:
302   case CURLE_OBSOLETE44:
303   case CURLE_OBSOLETE46:
304   case CURLE_OBSOLETE50:
305   case CURLE_OBSOLETE57:
306   case CURL_LAST:
307     break;
308   }
309   /*
310    * By using a switch, gcc -Wall will complain about enum values
311    * which do not appear, helping keep this function up-to-date.
312    * By using gcc -Wall -Werror, you can't forget.
313    *
314    * A table would not have the same benefit.  Most compilers will
315    * generate code very similar to a table in any case, so there
316    * is little performance gain from a table.  And something is broken
317    * for the user's application, anyways, so does it matter how fast
318    * it _doesn't_ work?
319    *
320    * The line number for the error will be near this comment, which
321    * is why it is here, and not at the start of the switch.
322    */
323   return "Unknown error";
324 #else
325   if(error == CURLE_OK)
326     return "No error";
327   else
328     return "Error";
329 #endif
330 }
331
332 const char *
333 curl_multi_strerror(CURLMcode error)
334 {
335 #ifndef CURL_DISABLE_VERBOSE_STRINGS
336   switch (error) {
337   case CURLM_CALL_MULTI_PERFORM:
338     return "Please call curl_multi_perform() soon";
339
340   case CURLM_OK:
341     return "No error";
342
343   case CURLM_BAD_HANDLE:
344     return "Invalid multi handle";
345
346   case CURLM_BAD_EASY_HANDLE:
347     return "Invalid easy handle";
348
349   case CURLM_OUT_OF_MEMORY:
350     return "Out of memory";
351
352   case CURLM_INTERNAL_ERROR:
353     return "Internal error";
354
355   case CURLM_BAD_SOCKET:
356     return "Invalid socket argument";
357
358   case CURLM_UNKNOWN_OPTION:
359     return "Unknown option";
360
361   case CURLM_LAST:
362     break;
363   }
364
365   return "Unknown error";
366 #else
367   if(error == CURLM_OK)
368     return "No error";
369   else
370     return "Error";
371 #endif
372 }
373
374 const char *
375 curl_share_strerror(CURLSHcode error)
376 {
377 #ifndef CURL_DISABLE_VERBOSE_STRINGS
378   switch (error) {
379   case CURLSHE_OK:
380     return "No error";
381
382   case CURLSHE_BAD_OPTION:
383     return "Unknown share option";
384
385   case CURLSHE_IN_USE:
386     return "Share currently in use";
387
388   case CURLSHE_INVALID:
389     return "Invalid share handle";
390
391   case CURLSHE_NOMEM:
392     return "Out of memory";
393
394   case CURLSHE_NOT_BUILT_IN:
395     return "Feature not enabled in this library";
396
397   case CURLSHE_LAST:
398     break;
399   }
400
401   return "CURLSHcode unknown";
402 #else
403   if(error == CURLSHE_OK)
404     return "No error";
405   else
406     return "Error";
407 #endif
408 }
409
410 #ifdef USE_WINSOCK
411
412 /* This function handles most / all (?) Winsock errors cURL is able to produce.
413  */
414 static const char *
415 get_winsock_error (int err, char *buf, size_t len)
416 {
417   const char *p;
418
419 #ifndef CURL_DISABLE_VERBOSE_STRINGS
420   switch (err) {
421   case WSAEINTR:
422     p = "Call interrupted";
423     break;
424   case WSAEBADF:
425     p = "Bad file";
426     break;
427   case WSAEACCES:
428     p = "Bad access";
429     break;
430   case WSAEFAULT:
431     p = "Bad argument";
432     break;
433   case WSAEINVAL:
434     p = "Invalid arguments";
435     break;
436   case WSAEMFILE:
437     p = "Out of file descriptors";
438     break;
439   case WSAEWOULDBLOCK:
440     p = "Call would block";
441     break;
442   case WSAEINPROGRESS:
443   case WSAEALREADY:
444     p = "Blocking call in progress";
445     break;
446   case WSAENOTSOCK:
447     p = "Descriptor is not a socket";
448     break;
449   case WSAEDESTADDRREQ:
450     p = "Need destination address";
451     break;
452   case WSAEMSGSIZE:
453     p = "Bad message size";
454     break;
455   case WSAEPROTOTYPE:
456     p = "Bad protocol";
457     break;
458   case WSAENOPROTOOPT:
459     p = "Protocol option is unsupported";
460     break;
461   case WSAEPROTONOSUPPORT:
462     p = "Protocol is unsupported";
463     break;
464   case WSAESOCKTNOSUPPORT:
465     p = "Socket is unsupported";
466     break;
467   case WSAEOPNOTSUPP:
468     p = "Operation not supported";
469     break;
470   case WSAEAFNOSUPPORT:
471     p = "Address family not supported";
472     break;
473   case WSAEPFNOSUPPORT:
474     p = "Protocol family not supported";
475     break;
476   case WSAEADDRINUSE:
477     p = "Address already in use";
478     break;
479   case WSAEADDRNOTAVAIL:
480     p = "Address not available";
481     break;
482   case WSAENETDOWN:
483     p = "Network down";
484     break;
485   case WSAENETUNREACH:
486     p = "Network unreachable";
487     break;
488   case WSAENETRESET:
489     p = "Network has been reset";
490     break;
491   case WSAECONNABORTED:
492     p = "Connection was aborted";
493     break;
494   case WSAECONNRESET:
495     p = "Connection was reset";
496     break;
497   case WSAENOBUFS:
498     p = "No buffer space";
499     break;
500   case WSAEISCONN:
501     p = "Socket is already connected";
502     break;
503   case WSAENOTCONN:
504     p = "Socket is not connected";
505     break;
506   case WSAESHUTDOWN:
507     p = "Socket has been shut down";
508     break;
509   case WSAETOOMANYREFS:
510     p = "Too many references";
511     break;
512   case WSAETIMEDOUT:
513     p = "Timed out";
514     break;
515   case WSAECONNREFUSED:
516     p = "Connection refused";
517     break;
518   case WSAELOOP:
519     p = "Loop??";
520     break;
521   case WSAENAMETOOLONG:
522     p = "Name too long";
523     break;
524   case WSAEHOSTDOWN:
525     p = "Host down";
526     break;
527   case WSAEHOSTUNREACH:
528     p = "Host unreachable";
529     break;
530   case WSAENOTEMPTY:
531     p = "Not empty";
532     break;
533   case WSAEPROCLIM:
534     p = "Process limit reached";
535     break;
536   case WSAEUSERS:
537     p = "Too many users";
538     break;
539   case WSAEDQUOT:
540     p = "Bad quota";
541     break;
542   case WSAESTALE:
543     p = "Something is stale";
544     break;
545   case WSAEREMOTE:
546     p = "Remote error";
547     break;
548 #ifdef WSAEDISCON  /* missing in SalfordC! */
549   case WSAEDISCON:
550     p = "Disconnected";
551     break;
552 #endif
553     /* Extended Winsock errors */
554   case WSASYSNOTREADY:
555     p = "Winsock library is not ready";
556     break;
557   case WSANOTINITIALISED:
558     p = "Winsock library not initialised";
559     break;
560   case WSAVERNOTSUPPORTED:
561     p = "Winsock version not supported";
562     break;
563
564     /* getXbyY() errors (already handled in herrmsg):
565      * Authoritative Answer: Host not found */
566   case WSAHOST_NOT_FOUND:
567     p = "Host not found";
568     break;
569
570     /* Non-Authoritative: Host not found, or SERVERFAIL */
571   case WSATRY_AGAIN:
572     p = "Host not found, try again";
573     break;
574
575     /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
576   case WSANO_RECOVERY:
577     p = "Unrecoverable error in call to nameserver";
578     break;
579
580     /* Valid name, no data record of requested type */
581   case WSANO_DATA:
582     p = "No data record of requested type";
583     break;
584
585   default:
586     return NULL;
587   }
588 #else
589   if(err == CURLE_OK)
590     return NULL;
591   else
592     p = "error";
593 #endif
594   strncpy (buf, p, len);
595   buf [len-1] = '\0';
596   return buf;
597 }
598 #endif   /* USE_WINSOCK */
599
600 /*
601  * Our thread-safe and smart strerror() replacement.
602  *
603  * The 'err' argument passed in to this function MUST be a true errno number
604  * as reported on this system. We do no range checking on the number before
605  * we pass it to the "number-to-message" conversion function and there might
606  * be systems that don't do proper range checking in there themselves.
607  *
608  * We don't do range checking (on systems other than Windows) since there is
609  * no good reliable and portable way to do it.
610  */
611 const char *Curl_strerror(struct connectdata *conn, int err)
612 {
613   char *buf, *p;
614   size_t max;
615   int old_errno = ERRNO;
616
617   DEBUGASSERT(conn);
618   DEBUGASSERT(err >= 0);
619
620   buf = conn->syserr_buf;
621   max = sizeof(conn->syserr_buf)-1;
622   *buf = '\0';
623
624 #ifdef USE_WINSOCK
625
626 #ifdef _WIN32_WCE
627   {
628     wchar_t wbuf[256];
629     wbuf[0] = L'\0';
630
631     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
632                   LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
633     wcstombs(buf,wbuf,max);
634   }
635 #else
636   /* 'sys_nerr' is the maximum errno number, it is not widely portable */
637   if(err >= 0 && err < sys_nerr)
638     strncpy(buf, strerror(err), max);
639   else {
640     if(!get_winsock_error(err, buf, max) &&
641        !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
642                        LANG_NEUTRAL, buf, (DWORD)max, NULL))
643       snprintf(buf, max, "Unknown error %d (%#x)", err, err);
644   }
645 #endif
646
647 #else /* not USE_WINSOCK coming up */
648
649 #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
650  /*
651   * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
652   * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
653   * message string, or EINVAL if 'errnum' is not a valid error number.
654   */
655   if(0 != strerror_r(err, buf, max)) {
656     if('\0' == buf[0])
657       snprintf(buf, max, "Unknown error %d", err);
658   }
659 #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
660  /*
661   * The glibc-style strerror_r() only *might* use the buffer we pass to
662   * the function, but it always returns the error message as a pointer,
663   * so we must copy that string unconditionally (if non-NULL).
664   */
665   {
666     char buffer[256];
667     char *msg = strerror_r(err, buffer, sizeof(buffer));
668     if(msg)
669       strncpy(buf, msg, max);
670     else
671       snprintf(buf, max, "Unknown error %d", err);
672   }
673 #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
674  /*
675   * The vxworks-style strerror_r() does use the buffer we pass to the function.
676   * The buffer size should be at least MAXERRSTR_SIZE (150) defined in rtsold.h
677   */
678   {
679     char buffer[256];
680     if(OK == strerror_r(err, buffer))
681       strncpy(buf, buffer, max);
682     else
683       snprintf(buf, max, "Unknown error %d", err);
684   }
685 #else
686   {
687     char *msg = strerror(err);
688     if(msg)
689       strncpy(buf, msg, max);
690     else
691       snprintf(buf, max, "Unknown error %d", err);
692   }
693 #endif
694
695 #endif /* end of ! USE_WINSOCK */
696
697   buf[max] = '\0'; /* make sure the string is zero terminated */
698
699   /* strip trailing '\r\n' or '\n'. */
700   if((p = strrchr(buf,'\n')) != NULL && (p - buf) >= 2)
701      *p = '\0';
702   if((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1)
703      *p = '\0';
704
705   if(old_errno != ERRNO)
706     SET_ERRNO(old_errno);
707
708   return buf;
709 }
710
711 #ifdef USE_LIBIDN
712 /*
713  * Return error-string for libidn status as returned from idna_to_ascii_lz().
714  */
715 const char *Curl_idn_strerror (struct connectdata *conn, int err)
716 {
717 #ifdef HAVE_IDNA_STRERROR
718   (void)conn;
719   return idna_strerror((Idna_rc) err);
720 #else
721   const char *str;
722   char *buf;
723   size_t max;
724
725   DEBUGASSERT(conn);
726
727   buf = conn->syserr_buf;
728   max = sizeof(conn->syserr_buf)-1;
729   *buf = '\0';
730
731 #ifndef CURL_DISABLE_VERBOSE_STRINGS
732   switch ((Idna_rc)err) {
733     case IDNA_SUCCESS:
734       str = "No error";
735       break;
736     case IDNA_STRINGPREP_ERROR:
737       str = "Error in string preparation";
738       break;
739     case IDNA_PUNYCODE_ERROR:
740       str = "Error in Punycode operation";
741       break;
742     case IDNA_CONTAINS_NON_LDH:
743       str = "Illegal ASCII characters";
744       break;
745     case IDNA_CONTAINS_MINUS:
746       str = "Contains minus";
747       break;
748     case IDNA_INVALID_LENGTH:
749       str = "Invalid output length";
750       break;
751     case IDNA_NO_ACE_PREFIX:
752       str = "No ACE prefix (\"xn--\")";
753       break;
754     case IDNA_ROUNDTRIP_VERIFY_ERROR:
755       str = "Round trip verify error";
756       break;
757     case IDNA_CONTAINS_ACE_PREFIX:
758       str = "Already have ACE prefix (\"xn--\")";
759       break;
760     case IDNA_ICONV_ERROR:
761       str = "Locale conversion failed";
762       break;
763     case IDNA_MALLOC_ERROR:
764       str = "Allocation failed";
765       break;
766     case IDNA_DLOPEN_ERROR:
767       str = "dlopen() error";
768       break;
769     default:
770       snprintf(buf, max, "error %d", err);
771       str = NULL;
772       break;
773   }
774 #else
775   if((Idna_rc)err == IDNA_SUCCESS)
776     str = "No error";
777   else
778     str = "Error";
779 #endif
780   if(str)
781     strncpy(buf, str, max);
782   buf[max] = '\0';
783   return (buf);
784 #endif
785 }
786 #endif  /* USE_LIBIDN */
787
788 #ifdef USE_WINDOWS_SSPI
789 const char *Curl_sspi_strerror (struct connectdata *conn, int err)
790 {
791 #ifndef CURL_DISABLE_VERBOSE_STRINGS
792   char txtbuf[80];
793   char msgbuf[sizeof(conn->syserr_buf)];
794   char *p, *str, *msg = NULL;
795   bool msg_formatted = FALSE;
796   int old_errno;
797 #endif
798   const char *txt;
799   char *outbuf;
800   size_t outmax;
801
802   DEBUGASSERT(conn);
803
804   outbuf = conn->syserr_buf;
805   outmax = sizeof(conn->syserr_buf)-1;
806   *outbuf = '\0';
807
808 #ifndef CURL_DISABLE_VERBOSE_STRINGS
809
810   old_errno = ERRNO;
811
812   switch (err) {
813     case SEC_E_OK:
814       txt = "No error";
815       break;
816     case SEC_E_ALGORITHM_MISMATCH:
817       txt = "SEC_E_ALGORITHM_MISMATCH";
818       break;
819     case SEC_E_BAD_BINDINGS:
820       txt = "SEC_E_BAD_BINDINGS";
821       break;
822     case SEC_E_BAD_PKGID:
823       txt = "SEC_E_BAD_PKGID";
824       break;
825     case SEC_E_BUFFER_TOO_SMALL:
826       txt = "SEC_E_BUFFER_TOO_SMALL";
827       break;
828     case SEC_E_CANNOT_INSTALL:
829       txt = "SEC_E_CANNOT_INSTALL";
830       break;
831     case SEC_E_CANNOT_PACK:
832       txt = "SEC_E_CANNOT_PACK";
833       break;
834     case SEC_E_CERT_EXPIRED:
835       txt = "SEC_E_CERT_EXPIRED";
836       break;
837     case SEC_E_CERT_UNKNOWN:
838       txt = "SEC_E_CERT_UNKNOWN";
839       break;
840     case SEC_E_CERT_WRONG_USAGE:
841       txt = "SEC_E_CERT_WRONG_USAGE";
842       break;
843     case SEC_E_CONTEXT_EXPIRED:
844       txt = "SEC_E_CONTEXT_EXPIRED";
845       break;
846     case SEC_E_CROSSREALM_DELEGATION_FAILURE:
847       txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
848       break;
849     case SEC_E_CRYPTO_SYSTEM_INVALID:
850       txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
851       break;
852     case SEC_E_DECRYPT_FAILURE:
853       txt = "SEC_E_DECRYPT_FAILURE";
854       break;
855     case SEC_E_DELEGATION_POLICY:
856       txt = "SEC_E_DELEGATION_POLICY";
857       break;
858     case SEC_E_DELEGATION_REQUIRED:
859       txt = "SEC_E_DELEGATION_REQUIRED";
860       break;
861     case SEC_E_DOWNGRADE_DETECTED:
862       txt = "SEC_E_DOWNGRADE_DETECTED";
863       break;
864     case SEC_E_ENCRYPT_FAILURE:
865       txt = "SEC_E_ENCRYPT_FAILURE";
866       break;
867     case SEC_E_ILLEGAL_MESSAGE:
868       txt = "SEC_E_ILLEGAL_MESSAGE";
869       break;
870     case SEC_E_INCOMPLETE_CREDENTIALS:
871       txt = "SEC_E_INCOMPLETE_CREDENTIALS";
872       break;
873     case SEC_E_INCOMPLETE_MESSAGE:
874       txt = "SEC_E_INCOMPLETE_MESSAGE";
875       break;
876     case SEC_E_INSUFFICIENT_MEMORY:
877       txt = "SEC_E_INSUFFICIENT_MEMORY";
878       break;
879     case SEC_E_INTERNAL_ERROR:
880       txt = "SEC_E_INTERNAL_ERROR";
881       break;
882     case SEC_E_INVALID_HANDLE:
883       txt = "SEC_E_INVALID_HANDLE";
884       break;
885     case SEC_E_INVALID_PARAMETER:
886       txt = "SEC_E_INVALID_PARAMETER";
887       break;
888     case SEC_E_INVALID_TOKEN:
889       txt = "SEC_E_INVALID_TOKEN";
890       break;
891     case SEC_E_ISSUING_CA_UNTRUSTED:
892       txt = "SEC_E_ISSUING_CA_UNTRUSTED";
893       break;
894     case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
895       txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
896       break;
897     case SEC_E_KDC_CERT_EXPIRED:
898       txt = "SEC_E_KDC_CERT_EXPIRED";
899       break;
900     case SEC_E_KDC_CERT_REVOKED:
901       txt = "SEC_E_KDC_CERT_REVOKED";
902       break;
903     case SEC_E_KDC_INVALID_REQUEST:
904       txt = "SEC_E_KDC_INVALID_REQUEST";
905       break;
906     case SEC_E_KDC_UNABLE_TO_REFER:
907       txt = "SEC_E_KDC_UNABLE_TO_REFER";
908       break;
909     case SEC_E_KDC_UNKNOWN_ETYPE:
910       txt = "SEC_E_KDC_UNKNOWN_ETYPE";
911       break;
912     case SEC_E_LOGON_DENIED:
913       txt = "SEC_E_LOGON_DENIED";
914       break;
915     case SEC_E_MAX_REFERRALS_EXCEEDED:
916       txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
917       break;
918     case SEC_E_MESSAGE_ALTERED:
919       txt = "SEC_E_MESSAGE_ALTERED";
920       break;
921     case SEC_E_MULTIPLE_ACCOUNTS:
922       txt = "SEC_E_MULTIPLE_ACCOUNTS";
923       break;
924     case SEC_E_MUST_BE_KDC:
925       txt = "SEC_E_MUST_BE_KDC";
926       break;
927     case SEC_E_NOT_OWNER:
928       txt = "SEC_E_NOT_OWNER";
929       break;
930     case SEC_E_NO_AUTHENTICATING_AUTHORITY:
931       txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
932       break;
933     case SEC_E_NO_CREDENTIALS:
934       txt = "SEC_E_NO_CREDENTIALS";
935       break;
936     case SEC_E_NO_IMPERSONATION:
937       txt = "SEC_E_NO_IMPERSONATION";
938       break;
939     case SEC_E_NO_IP_ADDRESSES:
940       txt = "SEC_E_NO_IP_ADDRESSES";
941       break;
942     case SEC_E_NO_KERB_KEY:
943       txt = "SEC_E_NO_KERB_KEY";
944       break;
945     case SEC_E_NO_PA_DATA:
946       txt = "SEC_E_NO_PA_DATA";
947       break;
948     case SEC_E_NO_S4U_PROT_SUPPORT:
949       txt = "SEC_E_NO_S4U_PROT_SUPPORT";
950       break;
951     case SEC_E_NO_TGT_REPLY:
952       txt = "SEC_E_NO_TGT_REPLY";
953       break;
954     case SEC_E_OUT_OF_SEQUENCE:
955       txt = "SEC_E_OUT_OF_SEQUENCE";
956       break;
957     case SEC_E_PKINIT_CLIENT_FAILURE:
958       txt = "SEC_E_PKINIT_CLIENT_FAILURE";
959       break;
960     case SEC_E_PKINIT_NAME_MISMATCH:
961       txt = "SEC_E_PKINIT_NAME_MISMATCH";
962       break;
963     case SEC_E_POLICY_NLTM_ONLY:
964       txt = "SEC_E_POLICY_NLTM_ONLY";
965       break;
966     case SEC_E_QOP_NOT_SUPPORTED:
967       txt = "SEC_E_QOP_NOT_SUPPORTED";
968       break;
969     case SEC_E_REVOCATION_OFFLINE_C:
970       txt = "SEC_E_REVOCATION_OFFLINE_C";
971       break;
972     case SEC_E_REVOCATION_OFFLINE_KDC:
973       txt = "SEC_E_REVOCATION_OFFLINE_KDC";
974       break;
975     case SEC_E_SECPKG_NOT_FOUND:
976       txt = "SEC_E_SECPKG_NOT_FOUND";
977       break;
978     case SEC_E_SECURITY_QOS_FAILED:
979       txt = "SEC_E_SECURITY_QOS_FAILED";
980       break;
981     case SEC_E_SHUTDOWN_IN_PROGRESS:
982       txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
983       break;
984     case SEC_E_SMARTCARD_CERT_EXPIRED:
985       txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
986       break;
987     case SEC_E_SMARTCARD_CERT_REVOKED:
988       txt = "SEC_E_SMARTCARD_CERT_REVOKED";
989       break;
990     case SEC_E_SMARTCARD_LOGON_REQUIRED:
991       txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
992       break;
993     case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
994       txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
995       break;
996     case SEC_E_TARGET_UNKNOWN:
997       txt = "SEC_E_TARGET_UNKNOWN";
998       break;
999     case SEC_E_TIME_SKEW:
1000       txt = "SEC_E_TIME_SKEW";
1001       break;
1002     case SEC_E_TOO_MANY_PRINCIPALS:
1003       txt = "SEC_E_TOO_MANY_PRINCIPALS";
1004       break;
1005     case SEC_E_UNFINISHED_CONTEXT_DELETED:
1006       txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
1007       break;
1008     case SEC_E_UNKNOWN_CREDENTIALS:
1009       txt = "SEC_E_UNKNOWN_CREDENTIALS";
1010       break;
1011     case SEC_E_UNSUPPORTED_FUNCTION:
1012       txt = "SEC_E_UNSUPPORTED_FUNCTION";
1013       break;
1014     case SEC_E_UNSUPPORTED_PREAUTH:
1015       txt = "SEC_E_UNSUPPORTED_PREAUTH";
1016       break;
1017     case SEC_E_UNTRUSTED_ROOT:
1018       txt = "SEC_E_UNTRUSTED_ROOT";
1019       break;
1020     case SEC_E_WRONG_CREDENTIAL_HANDLE:
1021       txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
1022       break;
1023     case SEC_E_WRONG_PRINCIPAL:
1024       txt = "SEC_E_WRONG_PRINCIPAL";
1025       break;
1026     case SEC_I_COMPLETE_AND_CONTINUE:
1027       txt = "SEC_I_COMPLETE_AND_CONTINUE";
1028       break;
1029     case SEC_I_COMPLETE_NEEDED:
1030       txt = "SEC_I_COMPLETE_NEEDED";
1031       break;
1032     case SEC_I_CONTEXT_EXPIRED:
1033       txt = "SEC_I_CONTEXT_EXPIRED";
1034       break;
1035     case SEC_I_CONTINUE_NEEDED:
1036       txt = "SEC_I_CONTINUE_NEEDED";
1037       break;
1038     case SEC_I_INCOMPLETE_CREDENTIALS:
1039       txt = "SEC_I_INCOMPLETE_CREDENTIALS";
1040       break;
1041     case SEC_I_LOCAL_LOGON:
1042       txt = "SEC_I_LOCAL_LOGON";
1043       break;
1044     case SEC_I_NO_LSA_CONTEXT:
1045       txt = "SEC_I_NO_LSA_CONTEXT";
1046       break;
1047     case SEC_I_RENEGOTIATE:
1048       txt = "SEC_I_RENEGOTIATE";
1049       break;
1050     case SEC_I_SIGNATURE_NEEDED:
1051       txt = "SEC_I_SIGNATURE_NEEDED";
1052       break;
1053     default:
1054       txt = "Unknown error";
1055   }
1056
1057   if(err == SEC_E_OK)
1058     strncpy(outbuf, txt, outmax);
1059   else {
1060     str = txtbuf;
1061     snprintf(txtbuf, sizeof(txtbuf), "%s (0x%04X%04X)",
1062              txt, (err >> 16) & 0xffff, err & 0xffff);
1063     txtbuf[sizeof(txtbuf)-1] = '\0';
1064
1065 #ifdef _WIN32_WCE
1066     {
1067       wchar_t wbuf[256];
1068       wbuf[0] = L'\0';
1069
1070       if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
1071                        FORMAT_MESSAGE_IGNORE_INSERTS,
1072                        NULL, err, LANG_NEUTRAL,
1073                        wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
1074         wcstombs(msgbuf,wbuf,sizeof(msgbuf)-1);
1075         msg_formatted = TRUE;
1076       }
1077     }
1078 #else
1079     if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
1080                       FORMAT_MESSAGE_IGNORE_INSERTS,
1081                       NULL, err, LANG_NEUTRAL,
1082                       msgbuf, sizeof(msgbuf)-1, NULL)) {
1083       msg_formatted = TRUE;
1084     }
1085 #endif
1086     if(msg_formatted) {
1087       msgbuf[sizeof(msgbuf)-1] = '\0';
1088       /* strip trailing '\r\n' or '\n' */
1089       if((p = strrchr(msgbuf,'\n')) != NULL && (p - msgbuf) >= 2)
1090          *p = '\0';
1091       if((p = strrchr(msgbuf,'\r')) != NULL && (p - msgbuf) >= 1)
1092          *p = '\0';
1093       msg = msgbuf;
1094     }
1095     if(msg)
1096       snprintf(outbuf, outmax, "%s - %s", str, msg);
1097     else
1098       strncpy(outbuf, str, outmax);
1099   }
1100
1101   if(old_errno != ERRNO)
1102     SET_ERRNO(old_errno);
1103
1104 #else
1105
1106   if(err == SEC_E_OK)
1107     txt = "No error";
1108   else
1109     txt = "Error";
1110
1111   strncpy(outbuf, txt, outmax);
1112
1113 #endif
1114
1115   outbuf[outmax] = '\0';
1116
1117   return outbuf;
1118 }
1119 #endif /* USE_WINDOWS_SSPI */