Back to index

lightning-sunbird  0.9+nobinonly
nsLDAPBERValue.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * 
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the mozilla.org LDAP XPCOM SDK.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2002
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Dan Mosedale <dmose@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsLDAPBERValue.h"
00041 #include "nsMemory.h"
00042 #include "nsString.h"
00043 #include "nsReadableUtils.h"
00044 
00045 NS_IMPL_THREADSAFE_ISUPPORTS1(nsLDAPBERValue, nsILDAPBERValue)
00046 
00047 nsLDAPBERValue::nsLDAPBERValue() : mValue(0), mSize(0)
00048 {
00049 }
00050 
00051 nsLDAPBERValue::~nsLDAPBERValue()
00052 {
00053     if (mValue) {
00054         nsMemory::Free(mValue);
00055     }
00056 }
00057 
00058 // void get (out unsigned long aCount, 
00059 //           [array, size_is (aCount), retval] out octet aRetVal); */
00060 NS_IMETHODIMP 
00061 nsLDAPBERValue::Get(PRUint32 *aCount, PRUint8 **aRetVal)
00062 {
00063     // if mSize = 0, return a count of a 0 and a null pointer
00064 
00065     if (mSize) {
00066         // get a buffer to hold a copy of the data
00067         //
00068         PRUint8 *array = NS_STATIC_CAST(PRUint8 *, nsMemory::Alloc(mSize));
00069 
00070         if (!array) {
00071             return NS_ERROR_OUT_OF_MEMORY;
00072         }
00073     
00074         // copy and return
00075         //
00076         memcpy(array, mValue, mSize);
00077         *aRetVal = array;
00078     } else {
00079         *aRetVal = 0;
00080     }
00081 
00082     *aCount = mSize;
00083     return NS_OK;
00084 }
00085 
00086 // void set(in unsigned long aCount, 
00087 //          [array, size_is(aCount)] in octet aValue);
00088 NS_IMETHODIMP
00089 nsLDAPBERValue::Set(PRUint32 aCount, PRUint8 *aValue)
00090 {
00091     // get rid of any old value being held here
00092     //
00093     if (mValue) {
00094         nsMemory::Free(mValue);
00095     }
00096 
00097     // if this is a non-zero value, allocate a buffer and copy
00098     //
00099     if (aCount) { 
00100         // get a buffer to hold a copy of this data
00101         //
00102         mValue = NS_STATIC_CAST(PRUint8 *, nsMemory::Alloc(aCount));
00103         if (!mValue) {
00104             return NS_ERROR_OUT_OF_MEMORY;
00105         }
00106 
00107         // copy the data and return
00108         //
00109         memcpy(mValue, aValue, aCount);
00110     } else {
00111         // otherwise just set it to null
00112         //
00113         mValue = 0;
00114     }
00115 
00116     mSize = aCount;
00117     return NS_OK;
00118 }
00119 
00120 // void setFromUTF8(in AUTF8String aValue);
00121 //
00122 NS_IMETHODIMP
00123 nsLDAPBERValue::SetFromUTF8(const nsACString & aValue)
00124 {
00125     // get rid of any old value being held here
00126     //
00127     if (mValue) {
00128         nsMemory::Free(mValue);
00129     }
00130 
00131     // copy the data and return
00132     //
00133     // XXXdmose should really be ToNewUTF8String, once the appropriate 
00134     // signature for that exists
00135     //
00136     mSize = aValue.Length();
00137     if (mSize) {
00138         mValue = NS_REINTERPRET_CAST(PRUint8 *, ToNewCString(aValue)); 
00139     } else {
00140         mValue = 0;
00141     }
00142     return NS_OK;
00143 }