99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * The contents of this file are subject to the Netscape Public License
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Version 1.0 (the "NPL"); you may not use this file except in
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * compliance with the NPL. You may obtain a copy of the NPL at
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Software distributed under the NPL is distributed on an "AS IS" basis,
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * for the specific language governing rights and limitations under the
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * The Initial Developer of this code under the NPL is Netscape
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Communications Corporation. Portions created by Netscape are
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Copyright (C) 1998 Netscape Communications Corporation. All Rights
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Reserved.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Copyright (c) 1990 Regents of the University of Michigan.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * All rights reserved.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Redistribution and use in source and binary forms are permitted
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * provided that this notice is preserved and that due credit is given
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * to the University of Michigan at Ann Arbor. The name of the University
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * may not be used to endorse or promote products derived from this
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * software without specific prior written permission. This software
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * is provided ``as is'' without express or implied warranty.
56664548661c43ae04de4a32bce3510ed36aeaf9Wyllys Ingersoll * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Use is subject to license terms.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Note: kmfber_read() only uses the ber_end and ber_ptr elements of ber.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Functions like kmfber_get_tag(), kmfber_skip_tag, and kmfber_peek_tag()
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * rely on that fact, so if this code is changed to use any additional
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * elements of the ber structure, those functions will need to be changed
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllyskmfber_read(BerElement *ber, char *buf, ber_len_t len)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys (void) memmove(buf, ber->ber_ptr, (size_t)actuallen);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * enlarge the ber buffer.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * return 0 on success, -1 on error.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys need = (len < EXBUFSIZ ? 1 : (len + (EXBUFSIZ - 1)) / EXBUFSIZ);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* transition to malloc'd buffer */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* copy existing data into new malloc'd buffer */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * If the stinking thing was moved, we need to go through and
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * reset all the sos and ber pointers. Offsets would've been
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * a better idea... oh well.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys ber->ber_ptr = ber->ber_buf + (ber->ber_ptr - oldbuf);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys for (s = ber->ber_sos; s != NULLSEQORSET; s = s->sos_next) {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (0);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * returns "len" on success and -1 on failure.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllyskmfber_write(BerElement *ber, char *buf, ber_len_t len, int nosos)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys (void) memmove(ber->ber_sos->sos_ptr, buf, (size_t)len);
56664548661c43ae04de4a32bce3510ed36aeaf9Wyllys Ingersoll !(ber->ber_flags & KMFBER_FLAG_NO_FREE_BUFFER))
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys/* we pre-allocate a buffer to save the extra malloc later */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys ber->ber_buf = (char *)ber + sizeof (struct berelement);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys (void) memset((char *)ber, '\0', sizeof (struct berelement));
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys sprintf(msg, "ber_dump: buf 0x%lx, ptr 0x%lx, rwptr 0x%lx, end 0x%lx\n",
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys ber->ber_buf, ber->ber_ptr, ber->ber_rwptr, ber->ber_end);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys lber_bprint(ber->ber_ptr, ber->ber_end - ber->ber_ptr);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys lber_bprint(ber->ber_buf, ber->ber_ptr - ber->ber_buf);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys sprintf(msg, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys lber_bprint(sos->sos_first, sos->sos_ptr - sos->sos_first);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys/* new dboreham code below: */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys unsigned char *p;
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * The kmfber_flatten routine allocates a struct berval whose contents
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * are a BER encoding taken from the ber argument. The bvPtr pointer
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * points to the returned berval, which must be freed using
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * kmfber_bvfree(). This routine returns 0 on success and -1 on error.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * The use of kmfber_flatten on a BerElement in which all '{' and '}'
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * format modifiers have not been properly matched can result in a
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * berval whose contents are not a valid BER encoding.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Note that the ber_ptr is not modified.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllyskmfber_flatten(BerElement *ber, struct berval **bvPtr)
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* allocate a struct berval */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys new = (struct berval *)malloc((size_t)(sizeof (struct berval)));
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * Copy everything from the BerElement's ber_buf to ber_ptr
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * into the berval structure.
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (-1);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys (void) memmove(new->bv_val, ber->ber_buf, (size_t)len);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* set bvPtr pointer to point to the returned berval */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys return (0);
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* construct BerElement */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if ((ber = kmfber_alloc_t(KMFBER_OPT_USE_DER)) != NULL) {
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* copy data from the bv argument into BerElement */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* XXXmcs: had to cast unsigned long bv_len to long */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if ((kmfber_write(ber, bv->bv_val, bv->bv_len, 0)) !=
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * reset ber_ptr back to the beginning of buffer so that this new
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * and initialized ber element can be READ
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * return a ptr to a new BerElement containing a copy of the data
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * in the bv argument or a null pointer on error
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* construct BerElement */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* copy data from the bv argument into BerElement */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys /* XXXmcs: had to cast unsigned long bv_len to long */
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys if ((kmfber_write(ber, bv->bv_val, bv->bv_len, 0)) !=
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * reset ber_ptr back to the beginning of buffer so that this new
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * and initialized ber element can be READ
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * return a ptr to a new BerElement containing a copy of the data
99ebb4ca412cb0a19d77a3899a87c055b9c30fa8wyllys * in the bv argument or a null pointer on error