dplayx: Code to forward player creation
[wine/gsoc_dplay.git] / dlls / winhttp / net.c
blobed4d4b90e37a8de6b36526f7d4bab921a248a44a
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_SOCKET_H
28 # include <sys/socket.h>
29 #endif
30 #ifdef HAVE_SYS_IOCTL_H
31 # include <sys/ioctl.h>
32 #endif
33 #ifdef HAVE_SYS_FILIO_H
34 # include <sys/filio.h>
35 #endif
36 #ifdef HAVE_POLL_H
37 # include <poll.h>
38 #endif
39 #ifdef HAVE_OPENSSL_SSL_H
40 # include <openssl/ssl.h>
41 #undef FAR
42 #undef DSA
43 #endif
45 #include "wine/debug.h"
46 #include "wine/library.h"
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winhttp.h"
51 #include "wincrypt.h"
53 #include "winhttp_private.h"
55 /* to avoid conflicts with the Unix socket headers */
56 #define USE_WS_PREFIX
57 #include "winsock2.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
61 #ifndef HAVE_GETADDRINFO
63 /* critical section to protect non-reentrant gethostbyname() */
64 static CRITICAL_SECTION cs_gethostbyname;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
67 0, 0, &cs_gethostbyname,
68 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
71 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
73 #endif
75 #ifdef SONAME_LIBSSL
77 #include <openssl/err.h>
79 static void *libssl_handle;
80 static void *libcrypto_handle;
82 static SSL_METHOD *method;
83 static SSL_CTX *ctx;
85 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
87 MAKE_FUNCPTR( SSL_library_init );
88 MAKE_FUNCPTR( SSL_load_error_strings );
89 MAKE_FUNCPTR( SSLv23_method );
90 MAKE_FUNCPTR( SSL_CTX_new );
91 MAKE_FUNCPTR( SSL_new );
92 MAKE_FUNCPTR( SSL_free );
93 MAKE_FUNCPTR( SSL_set_fd );
94 MAKE_FUNCPTR( SSL_connect );
95 MAKE_FUNCPTR( SSL_shutdown );
96 MAKE_FUNCPTR( SSL_write );
97 MAKE_FUNCPTR( SSL_read );
98 MAKE_FUNCPTR( SSL_get_verify_result );
99 MAKE_FUNCPTR( SSL_get_peer_certificate );
100 MAKE_FUNCPTR( SSL_CTX_set_default_verify_paths );
102 MAKE_FUNCPTR( BIO_new_fp );
103 MAKE_FUNCPTR( ERR_get_error );
104 MAKE_FUNCPTR( ERR_error_string );
105 MAKE_FUNCPTR( i2d_X509 );
106 #undef MAKE_FUNCPTR
108 #endif
110 /* translate a unix error code into a winsock error code */
111 static int sock_get_error( int err )
113 #if !defined(__MINGW32__) && !defined (_MSC_VER)
114 switch (err)
116 case EINTR: return WSAEINTR;
117 case EBADF: return WSAEBADF;
118 case EPERM:
119 case EACCES: return WSAEACCES;
120 case EFAULT: return WSAEFAULT;
121 case EINVAL: return WSAEINVAL;
122 case EMFILE: return WSAEMFILE;
123 case EWOULDBLOCK: return WSAEWOULDBLOCK;
124 case EINPROGRESS: return WSAEINPROGRESS;
125 case EALREADY: return WSAEALREADY;
126 case ENOTSOCK: return WSAENOTSOCK;
127 case EDESTADDRREQ: return WSAEDESTADDRREQ;
128 case EMSGSIZE: return WSAEMSGSIZE;
129 case EPROTOTYPE: return WSAEPROTOTYPE;
130 case ENOPROTOOPT: return WSAENOPROTOOPT;
131 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
132 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
133 case EOPNOTSUPP: return WSAEOPNOTSUPP;
134 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
135 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
136 case EADDRINUSE: return WSAEADDRINUSE;
137 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
138 case ENETDOWN: return WSAENETDOWN;
139 case ENETUNREACH: return WSAENETUNREACH;
140 case ENETRESET: return WSAENETRESET;
141 case ECONNABORTED: return WSAECONNABORTED;
142 case EPIPE:
143 case ECONNRESET: return WSAECONNRESET;
144 case ENOBUFS: return WSAENOBUFS;
145 case EISCONN: return WSAEISCONN;
146 case ENOTCONN: return WSAENOTCONN;
147 case ESHUTDOWN: return WSAESHUTDOWN;
148 case ETOOMANYREFS: return WSAETOOMANYREFS;
149 case ETIMEDOUT: return WSAETIMEDOUT;
150 case ECONNREFUSED: return WSAECONNREFUSED;
151 case ELOOP: return WSAELOOP;
152 case ENAMETOOLONG: return WSAENAMETOOLONG;
153 case EHOSTDOWN: return WSAEHOSTDOWN;
154 case EHOSTUNREACH: return WSAEHOSTUNREACH;
155 case ENOTEMPTY: return WSAENOTEMPTY;
156 #ifdef EPROCLIM
157 case EPROCLIM: return WSAEPROCLIM;
158 #endif
159 #ifdef EUSERS
160 case EUSERS: return WSAEUSERS;
161 #endif
162 #ifdef EDQUOT
163 case EDQUOT: return WSAEDQUOT;
164 #endif
165 #ifdef ESTALE
166 case ESTALE: return WSAESTALE;
167 #endif
168 #ifdef EREMOTE
169 case EREMOTE: return WSAEREMOTE;
170 #endif
171 default: errno = err; perror( "sock_set_error" ); return WSAEFAULT;
173 #endif
174 return err;
177 BOOL netconn_init( netconn_t *conn, BOOL secure )
179 conn->socket = -1;
180 if (!secure) return TRUE;
182 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
183 if (libssl_handle) return TRUE;
184 if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
186 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
187 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
188 return FALSE;
190 if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
192 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
193 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
194 return FALSE;
196 #define LOAD_FUNCPTR(x) \
197 if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
199 ERR("Failed to load symbol %s\n", #x); \
200 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
201 return FALSE; \
203 LOAD_FUNCPTR( SSL_library_init );
204 LOAD_FUNCPTR( SSL_load_error_strings );
205 LOAD_FUNCPTR( SSLv23_method );
206 LOAD_FUNCPTR( SSL_CTX_new );
207 LOAD_FUNCPTR( SSL_new );
208 LOAD_FUNCPTR( SSL_free );
209 LOAD_FUNCPTR( SSL_set_fd );
210 LOAD_FUNCPTR( SSL_connect );
211 LOAD_FUNCPTR( SSL_shutdown );
212 LOAD_FUNCPTR( SSL_write );
213 LOAD_FUNCPTR( SSL_read );
214 LOAD_FUNCPTR( SSL_get_verify_result );
215 LOAD_FUNCPTR( SSL_get_peer_certificate );
216 LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
217 #undef LOAD_FUNCPTR
219 #define LOAD_FUNCPTR(x) \
220 if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
222 ERR("Failed to load symbol %s\n", #x); \
223 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
224 return FALSE; \
226 LOAD_FUNCPTR( BIO_new_fp );
227 LOAD_FUNCPTR( ERR_get_error );
228 LOAD_FUNCPTR( ERR_error_string );
229 LOAD_FUNCPTR( i2d_X509 );
230 #undef LOAD_FUNCPTR
232 pSSL_library_init();
233 pSSL_load_error_strings();
234 pBIO_new_fp( stderr, BIO_NOCLOSE );
236 method = pSSLv23_method();
237 #else
238 WARN("SSL support not compiled in.\n");
239 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
240 return FALSE;
241 #endif
242 return TRUE;
245 BOOL netconn_connected( netconn_t *conn )
247 return (conn->socket != -1);
250 BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
252 if ((conn->socket = socket( domain, type, protocol )) == -1)
254 WARN("unable to create socket (%s)\n", strerror(errno));
255 set_last_error( sock_get_error( errno ) );
256 return FALSE;
258 return TRUE;
261 BOOL netconn_close( netconn_t *conn )
263 int res;
265 #ifdef SONAME_LIBSSL
266 if (conn->secure)
268 heap_free( conn->peek_msg_mem );
269 conn->peek_msg_mem = NULL;
270 conn->peek_msg = NULL;
271 conn->peek_len = 0;
273 pSSL_shutdown( conn->ssl_conn );
274 pSSL_free( conn->ssl_conn );
276 conn->ssl_conn = NULL;
277 conn->secure = FALSE;
279 #endif
280 res = closesocket( conn->socket );
281 conn->socket = -1;
282 if (res == -1)
284 set_last_error( sock_get_error( errno ) );
285 return FALSE;
287 return TRUE;
290 BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len, int timeout )
292 BOOL ret = FALSE;
293 int res = 0, state;
295 if (timeout > 0)
297 state = 1;
298 ioctlsocket( conn->socket, FIONBIO, &state );
300 if (connect( conn->socket, sockaddr, addr_len ) < 0)
302 res = sock_get_error( errno );
303 if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
305 struct pollfd pfd;
307 pfd.fd = conn->socket;
308 pfd.events = POLLOUT;
309 if (poll( &pfd, 1, timeout ) > 0)
310 ret = TRUE;
311 else
312 res = sock_get_error( errno );
315 else
316 ret = TRUE;
317 if (timeout > 0)
319 state = 0;
320 ioctlsocket( conn->socket, FIONBIO, &state );
322 if (!ret)
324 WARN("unable to connect to host (%d)\n", res);
325 set_last_error( res );
327 return ret;
330 BOOL netconn_secure_connect( netconn_t *conn )
332 #ifdef SONAME_LIBSSL
333 X509 *cert;
334 long res;
336 ctx = pSSL_CTX_new( method );
337 if (!pSSL_CTX_set_default_verify_paths( ctx ))
339 ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
340 set_last_error( ERROR_OUTOFMEMORY );
341 return FALSE;
343 if (!(conn->ssl_conn = pSSL_new( ctx )))
345 ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
346 set_last_error( ERROR_OUTOFMEMORY );
347 goto fail;
349 if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
351 ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
352 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
353 goto fail;
355 if (pSSL_connect( conn->ssl_conn ) <= 0)
357 ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
358 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
359 goto fail;
361 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn )))
363 ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 ));
364 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
365 goto fail;
367 if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK)
369 /* FIXME: we should set an error and return, but we only print an error at the moment */
370 ERR("couldn't verify server certificate (%ld)\n", res);
372 TRACE("established SSL connection\n");
373 conn->secure = TRUE;
374 return TRUE;
376 fail:
377 if (conn->ssl_conn)
379 pSSL_shutdown( conn->ssl_conn );
380 pSSL_free( conn->ssl_conn );
381 conn->ssl_conn = NULL;
383 #endif
384 return FALSE;
387 BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
389 if (!netconn_connected( conn )) return FALSE;
390 if (conn->secure)
392 #ifdef SONAME_LIBSSL
393 if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
394 *sent = pSSL_write( conn->ssl_conn, msg, len );
395 if (*sent < 1 && len) return FALSE;
396 return TRUE;
397 #else
398 return FALSE;
399 #endif
401 if ((*sent = send( conn->socket, msg, len, flags )) == -1)
403 set_last_error( sock_get_error( errno ) );
404 return FALSE;
406 return TRUE;
409 BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
411 *recvd = 0;
412 if (!netconn_connected( conn )) return FALSE;
413 if (!len) return TRUE;
415 if (conn->secure)
417 #ifdef SONAME_LIBSSL
418 if (flags & ~(MSG_PEEK | MSG_WAITALL))
419 FIXME("SSL_read does not support the following flags: %08x\n", flags);
421 /* this ugly hack is all for MSG_PEEK */
422 if (flags & MSG_PEEK && !conn->peek_msg)
424 if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
426 else if (flags & MSG_PEEK && conn->peek_msg)
428 if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
429 *recvd = min( len, conn->peek_len );
430 memcpy( buf, conn->peek_msg, *recvd );
431 return TRUE;
433 else if (conn->peek_msg)
435 *recvd = min( len, conn->peek_len );
436 memcpy( buf, conn->peek_msg, *recvd );
437 conn->peek_len -= *recvd;
438 conn->peek_msg += *recvd;
440 if (conn->peek_len == 0)
442 heap_free( conn->peek_msg_mem );
443 conn->peek_msg_mem = NULL;
444 conn->peek_msg = NULL;
446 /* check if we have enough data from the peek buffer */
447 if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
449 *recvd += pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
450 if (flags & MSG_PEEK) /* must copy into buffer */
452 conn->peek_len = *recvd;
453 if (!*recvd)
455 heap_free( conn->peek_msg_mem );
456 conn->peek_msg_mem = NULL;
457 conn->peek_msg = NULL;
459 else memcpy( conn->peek_msg, buf, *recvd );
461 if (*recvd < 1 && len) return FALSE;
462 return TRUE;
463 #else
464 return FALSE;
465 #endif
467 if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
469 set_last_error( sock_get_error( errno ) );
470 return FALSE;
472 return TRUE;
475 BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
477 #ifdef FIONREAD
478 int ret, unread;
479 #endif
480 *available = 0;
481 if (!netconn_connected( conn )) return FALSE;
483 if (conn->secure)
485 #ifdef SONAME_LIBSSL
486 if (conn->peek_msg) *available = conn->peek_len;
487 #endif
488 return TRUE;
490 #ifdef FIONREAD
491 if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
492 #endif
493 return TRUE;
496 BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
498 struct pollfd pfd;
499 BOOL ret = FALSE;
500 DWORD recvd = 0;
502 if (!netconn_connected( conn )) return FALSE;
504 if (conn->secure)
506 #ifdef SONAME_LIBSSL
507 while (recvd < *buflen)
509 int dummy;
510 if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
512 set_last_error( ERROR_CONNECTION_ABORTED );
513 break;
515 if (buffer[recvd] == '\n')
517 ret = TRUE;
518 break;
520 if (buffer[recvd] != '\r') recvd++;
522 if (ret)
524 buffer[recvd++] = 0;
525 *buflen = recvd;
526 TRACE("received line %s\n", debugstr_a(buffer));
528 return ret;
529 #else
530 return FALSE;
531 #endif
534 pfd.fd = conn->socket;
535 pfd.events = POLLIN;
536 while (recvd < *buflen)
538 int timeout, res;
539 struct timeval tv;
540 socklen_t len = sizeof(tv);
542 if ((res = getsockopt( conn->socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &len ) != -1))
543 timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
544 else
545 timeout = -1;
546 if (poll( &pfd, 1, timeout ) > 0)
548 if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
550 if (res == -1) set_last_error( sock_get_error( errno ) );
551 break;
553 if (buffer[recvd] == '\n')
555 ret = TRUE;
556 break;
558 if (buffer[recvd] != '\r') recvd++;
560 else
562 set_last_error( ERROR_WINHTTP_TIMEOUT );
563 break;
566 if (ret)
568 buffer[recvd++] = 0;
569 *buflen = recvd;
570 TRACE("received line %s\n", debugstr_a(buffer));
572 return ret;
575 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
577 int res;
578 struct timeval tv;
580 /* value is in milliseconds, convert to struct timeval */
581 tv.tv_sec = value / 1000;
582 tv.tv_usec = (value % 1000) * 1000;
584 if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1))
586 WARN("setsockopt failed (%s)\n", strerror( errno ));
587 return sock_get_error( errno );
589 return ERROR_SUCCESS;
592 BOOL netconn_resolve( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len )
594 char *hostname;
595 #ifdef HAVE_GETADDRINFO
596 struct addrinfo *res, hints;
597 int ret;
598 #else
599 struct hostent *he;
600 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
601 #endif
603 if (!(hostname = strdupWA( hostnameW ))) return FALSE;
605 #ifdef HAVE_GETADDRINFO
606 memset( &hints, 0, sizeof(struct addrinfo) );
607 /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
608 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
610 hints.ai_family = AF_INET;
612 ret = getaddrinfo( hostname, NULL, &hints, &res );
613 if (ret != 0)
615 TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(hostnameW), gai_strerror(ret));
616 hints.ai_family = AF_INET6;
617 ret = getaddrinfo( hostname, NULL, &hints, &res );
618 if (ret != 0)
620 TRACE("failed to get address of %s (%s)\n", debugstr_w(hostnameW), gai_strerror(ret));
621 heap_free( hostname );
622 return FALSE;
625 heap_free( hostname );
626 if (*sa_len < res->ai_addrlen)
628 WARN("address too small\n");
629 freeaddrinfo( res );
630 return FALSE;
632 *sa_len = res->ai_addrlen;
633 memcpy( sa, res->ai_addr, res->ai_addrlen );
634 /* Copy port */
635 switch (res->ai_family)
637 case AF_INET:
638 ((struct sockaddr_in *)sa)->sin_port = htons( port );
639 break;
640 case AF_INET6:
641 ((struct sockaddr_in6 *)sa)->sin6_port = htons( port );
642 break;
645 freeaddrinfo( res );
646 return TRUE;
647 #else
648 EnterCriticalSection( &cs_gethostbyname );
650 he = gethostbyname( hostname );
651 heap_free( hostname );
652 if (!he)
654 TRACE("failed to get address of %s (%d)\n", debugstr_w(hostnameW), h_errno);
655 LeaveCriticalSection( &cs_gethostbyname );
656 return FALSE;
658 if (*sa_len < sizeof(struct sockaddr_in))
660 WARN("address too small\n");
661 LeaveCriticalSection( &cs_gethostbyname );
662 return FALSE;
664 *sa_len = sizeof(struct sockaddr_in);
665 memset( sa, 0, sizeof(struct sockaddr_in) );
666 memcpy( &sin->sin_addr, he->h_addr, he->h_length );
667 sin->sin_family = he->h_addrtype;
668 sin->sin_port = htons( port );
670 LeaveCriticalSection( &cs_gethostbyname );
671 return TRUE;
672 #endif
675 const void *netconn_get_certificate( netconn_t *conn )
677 #ifdef SONAME_LIBSSL
678 X509 *cert;
679 unsigned char *buffer, *p;
680 int len;
681 BOOL malloc = FALSE;
682 const CERT_CONTEXT *ret;
684 if (!conn->secure) return NULL;
686 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
687 p = NULL;
688 if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
690 * SSL 0.9.7 and above malloc the buffer if it is null.
691 * however earlier version do not and so we would need to alloc the buffer.
693 * see the i2d_X509 man page for more details.
695 if (!p)
697 if (!(buffer = heap_alloc( len ))) return NULL;
698 p = buffer;
699 len = pi2d_X509( cert, &p );
701 else
703 buffer = p;
704 malloc = TRUE;
707 ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
709 if (malloc) free( buffer );
710 else heap_free( buffer );
712 return ret;
713 #else
714 return NULL;
715 #endif