1/*
2 * Copyright (c) 2006,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _XDR_DLDB_H
25#define _XDR_DLDB_H
26
27#include "sec_xdr.h"
28#include "xdr_cssm.h"
29
30#include <security_cdsa_utilities/cssmdb.h>
31
32bool_t xdr_DLDbFlatIdentifier(XDR *xdrs, DataWalkers::DLDbFlatIdentifier *objp);
33bool_t xdr_DLDbFlatIdentifierRef(XDR *xdrs, DataWalkers::DLDbFlatIdentifier **objp);
34
35class CopyIn {
36    public:
37        CopyIn(const void *data, xdrproc_t proc) : mLength(0), mData(0) {
38            if (data && !::copyin(static_cast<uint8_t*>(const_cast<void *>(data)), proc, &mData, &mLength))
39                CssmError::throwMe(CSSM_ERRCODE_MEMORY_ERROR);
40        }
41        ~CopyIn() { if (mData) free(mData);     }
42        u_int length() { return mLength; }
43        void *data() { return mData; }
44    protected:
45        u_int mLength;
46        void *mData;
47};
48
49// split out CSSM_DATA variant
50class CopyOut {
51    public:
52		// CSSM_DATA can be output only if empty, but also specify preallocated memory to use
53        CopyOut(void *copy, size_t size, xdrproc_t proc, bool dealloc = false, CSSM_DATA *in_out_data = NULL) : mLength(in_out_data?(u_int)in_out_data->Length:0), mData(NULL), mInOutData(in_out_data), mDealloc(dealloc), mSource(copy), mSourceLen(size) {
54            if (copy && size && !::copyout(copy, (u_int)size, proc, mInOutData ? reinterpret_cast<void**>(&mInOutData) : &mData, &mLength)) {
55                if (mInOutData && mInOutData->Length) // DataOut behaviour: error back to user if likely related to amount of space passed in
56                    CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
57                else
58                    CssmError::throwMe(CSSM_ERRCODE_MEMORY_ERROR);
59            }
60        }
61        ~CopyOut();
62        u_int length() { return mLength; }
63        void* data() { return mData; }
64        void* get() { void *tmp = mData; mData = NULL; return tmp; }
65    protected:
66        u_int mLength;
67        void *mData;
68		CSSM_DATA *mInOutData;
69		bool mDealloc;
70		void *mSource;
71		size_t mSourceLen;
72};
73
74#endif /* !_XDR_AUTH_H */
75