summaryrefslogtreecommitdiffstats
path: root/usr.bin/fetch/util.c
diff options
context:
space:
mode:
authorwollman <wollman@FreeBSD.org>1997-02-05 19:59:18 +0000
committerwollman <wollman@FreeBSD.org>1997-02-05 19:59:18 +0000
commit37ca85c089f28bd04237b7fd363ad07b07994aac (patch)
tree3eb28344fec90f861cb268c3467fefc2a8aee72f /usr.bin/fetch/util.c
parentf061fa28f5ac3f93d633c36e904431dd425e25ba (diff)
downloadFreeBSD-src-37ca85c089f28bd04237b7fd363ad07b07994aac.zip
FreeBSD-src-37ca85c089f28bd04237b7fd363ad07b07994aac.tar.gz
Some bug-fixes, clean-ups, and one new feature:
- Fix the bug with URIs of the form ftp://host/filename. - Fix some more string-termination bugs in util.c. - Use safe_malloc() rather than testing the return value of regular malloc() in 15 places. - Implement HTTP authentication, for both servers and proxies. Currently only ``basic'' authentication is supported; This Is A Bug (but less of one tjhan nmot supporting any authentication). I think there is only one more feature which is required for full HTTP/1.1 support, which is Transfer-Encoding: chunked; this should not be toohard, but it isn't very important, either.
Diffstat (limited to 'usr.bin/fetch/util.c')
-rw-r--r--usr.bin/fetch/util.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/usr.bin/fetch/util.c b/usr.bin/fetch/util.c
index aab9278..b7d4b21 100644
--- a/usr.bin/fetch/util.c
+++ b/usr.bin/fetch/util.c
@@ -26,7 +26,7 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: util.c,v 1.1 1997/01/30 21:43:44 wollman Exp $
+ * $Id: util.c,v 1.2 1997/02/02 09:16:37 bde Exp $
*/
#include <sys/types.h>
@@ -129,9 +129,7 @@ percent_decode(const char *uri)
{
char *rv, *s;
- rv = s = malloc(strlen(uri) + 1);
- if (rv == 0)
- err(EX_OSERR, "malloc");
+ rv = s = safe_malloc(strlen(uri) + 1);
while (*uri) {
if (*uri == '%' && uri[1]
@@ -183,6 +181,20 @@ parse_host_port(const char *s, char **hostname, int *port)
}
/*
+ * safe_malloc is like malloc, but aborts on error.
+ */
+void *
+safe_malloc(size_t len)
+{
+ void *rv;
+
+ rv = malloc(len);
+ if (rv == 0)
+ err(EX_OSERR, "malloc(%qu)", (u_quad_t)len);
+ return rv;
+}
+
+/*
* safe_strdup is like strdup, but aborts on error.
*/
char *
@@ -190,9 +202,7 @@ safe_strdup(const char *orig)
{
char *s;
- s = malloc(strlen(orig) + 1);
- if (s == 0)
- err(EX_OSERR, "malloc");
+ s = safe_malloc(strlen(orig) + 1);
strcpy(s, orig);
return s;
}
@@ -206,9 +216,7 @@ safe_strndup(const char *orig, size_t len)
{
char *s;
- s = malloc(len + 1);
- if (s == 0)
- err(EX_OSERR, "malloc");
+ s = safe_malloc(len + 1);
s[0] = '\0';
strncat(s, orig, len);
return s;
@@ -223,15 +231,14 @@ static const char base64[] =
char *
to_base64(const unsigned char *buf, size_t len)
{
- char *s = malloc((4 * (len + 1)) / 3 + 1), *rv;
+ char *s, *rv;
unsigned tmp;
- if (s == 0)
- err(EX_OSERR, "malloc");
+ s = safe_malloc((4 * (len + 1)) / 3 + 1);
rv = s;
while (len >= 3) {
- tmp = buf[0] << 16 | buf[1] << 8 || buf[2];
+ tmp = buf[0] << 16 | buf[1] << 8 | buf[2];
s[0] = base64[tmp >> 18];
s[1] = base64[(tmp >> 12) & 077];
s[2] = base64[(tmp >> 6) & 077];
@@ -249,14 +256,17 @@ to_base64(const unsigned char *buf, size_t len)
s[1] = base64[(tmp >> 12) & 077];
s[2] = base64[(tmp >> 6) & 077];
s[3] = '=';
+ s[4] = '\0';
break;
case 1:
tmp = buf[0] << 16;
s[0] = base64[(tmp >> 18) & 077];
s[1] = base64[(tmp >> 12) & 077];
s[2] = s[3] = '=';
+ s[4] = '\0';
break;
case 0:
+ s[0] = '\0';
break;
}
OpenPOWER on IntegriCloud