]> git.sur5r.net Git - openldap/blob - libraries/libldif/fetch.c
s/SAFEMEMCPY/AC_MEMCPY/
[openldap] / libraries / libldif / fetch.c
1 /* line64.c - routines for dealing with the slapd line format */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/time.h>
17
18 #ifdef HAVE_FETCH
19 #include <fetch.h>
20 #endif
21
22 #include "ldap_log.h"
23 #include "lber_pvt.h"
24 #include "ldap_pvt.h"
25 #include "ldap_config.h"
26 #include "ldif.h"
27
28 int
29 ldif_fetch_url(
30     LDAP_CONST char     *urlstr,
31     char        **valuep,
32     ber_len_t *vlenp
33 )
34 {
35         FILE *url;
36         char buffer[1024];
37         char *p = NULL;
38         size_t total;
39         size_t bytes;
40
41         *valuep = NULL;
42         *vlenp = 0;
43
44 #ifdef HAVE_FETCH
45         url = fetchGetURL( (char*) urlstr, "" );
46
47 #else
48         if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
49                 p = strchr( &urlstr[sizeof("file://")-1], '/' );
50                 if( p == NULL ) {
51                         return -1;
52                 }
53
54                 if( *p != *LDAP_DIRSEP ) {
55                         /* skip over false root */
56                         p++;
57                 }
58
59                 p = ber_strdup( p );
60                 ldap_pvt_hex_unescape( p );
61
62                 url = fopen( p, "rb" );
63
64         } else {
65                 return -1;
66         }
67 #endif
68
69         if( url == NULL ) {
70                 return -1;
71         }
72
73         total = 0;
74
75         while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
76                 char *newp = ber_memrealloc( p, total + bytes );
77                 if( newp == NULL ) {
78                         ber_memfree( p );
79                         fclose( url );
80                         return -1;
81                 }
82                 p = newp;
83                 AC_MEMCPY( &p[total], buffer, bytes );
84                 total += bytes;
85         }
86
87         fclose( url );
88
89         *valuep = p;
90         *vlenp = total;
91
92         return 0;
93 }