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