summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorJeremie Courreges-Anglas <jca@cvs.openbsd.org>2018-07-09 09:21:27 +0000
committerJeremie Courreges-Anglas <jca@cvs.openbsd.org>2018-07-09 09:21:27 +0000
commit39d1b4aca9a2279c71ffbe083d566aab3b969443 (patch)
tree50d3566d9748568307c839fe7637308b1144b3c3 /usr.bin
parentb545cdd311a5d0dd5f307b083a8e5bdbc7d2c14d (diff)
Simplify ber_read()
After the removal of fd-based read/writes I could have trimmed the code further. - no socket-based reads so ber_read() doesn't need to loop until it gets the desired amount of data - return either the requested amount of data or -1/ECANCELED, the caller shouldn't have to handle partial reads itself - inline ber_readbuf() into ber_read() ok rob@ claudio@ tb@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/ldap/ber.c52
1 files changed, 16 insertions, 36 deletions
diff --git a/usr.bin/ldap/ber.c b/usr.bin/ldap/ber.c
index 3683c618770..ffdafb263cb 100644
--- a/usr.bin/ldap/ber.c
+++ b/usr.bin/ldap/ber.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ber.c,v 1.11 2018/07/04 15:21:24 rob Exp $ */
+/* $OpenBSD: ber.c,v 1.12 2018/07/09 09:21:26 jca Exp $ */
/*
* Copyright (c) 2007, 2012 Reyk Floeter <reyk@openbsd.org>
@@ -31,8 +31,6 @@
#include "ber.h"
-#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
-
#define BER_TYPE_CONSTRUCTED 0x20 /* otherwise primitive */
#define BER_TYPE_SINGLE_MAX 30
#define BER_TAG_MASK 0x1f
@@ -48,7 +46,6 @@ static ssize_t get_id(struct ber *b, unsigned long *tag, int *class,
int *cstruct);
static ssize_t get_len(struct ber *b, ssize_t *len);
static ssize_t ber_read_element(struct ber *ber, struct ber_element *elm);
-static ssize_t ber_readbuf(struct ber *b, void *buf, size_t nbytes);
static ssize_t ber_getc(struct ber *b, u_char *c);
static ssize_t ber_read(struct ber *ber, void *buf, size_t len);
@@ -1208,28 +1205,6 @@ ber_read_element(struct ber *ber, struct ber_element *elm)
return totlen;
}
-static ssize_t
-ber_readbuf(struct ber *b, void *buf, size_t nbytes)
-{
- size_t sz, len;
-
- if (b->br_rbuf == NULL)
- return -1;
-
- sz = b->br_rend - b->br_rptr;
- len = MINIMUM(nbytes, sz);
- if (len == 0) {
- errno = ECANCELED;
- return (-1); /* end of buffer and parser wants more data */
- }
-
- bcopy(b->br_rptr, buf, len);
- b->br_rptr += len;
- b->br_offs += len;
-
- return (len);
-}
-
void
ber_set_readbuf(struct ber *b, void *buf, size_t len)
{
@@ -1269,23 +1244,28 @@ ber_free(struct ber *b)
static ssize_t
ber_getc(struct ber *b, u_char *c)
{
- return ber_readbuf(b, c, 1);
+ return ber_read(b, c, 1);
}
static ssize_t
ber_read(struct ber *ber, void *buf, size_t len)
{
- u_char *b = buf;
- ssize_t r, remain = len;
+ size_t sz;
- while (remain > 0) {
- r = ber_readbuf(ber, b, remain);
- if (r == -1)
- return -1;
- b += r;
- remain -= r;
+ if (ber->br_rbuf == NULL)
+ return -1;
+
+ sz = ber->br_rend - ber->br_rptr;
+ if (len > sz) {
+ errno = ECANCELED;
+ return -1; /* parser wants more data than available */
}
- return (b - (u_char *)buf);
+
+ bcopy(ber->br_rptr, buf, len);
+ ber->br_rptr += len;
+ ber->br_offs += len;
+
+ return len;
}
int