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