Back to index

lightning-sunbird  0.9+nobinonly
nsLDAPBERElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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  * Oracle Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2005
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Dan Mosedale <dan.mosedale@oracle.com> (original author)
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 "nsLDAPBERElement.h"
00041 #include "nsString.h"
00042 #include "nsCOMPtr.h"
00043 #include "nsLDAPBERValue.h"
00044 
00045 NS_IMPL_ISUPPORTS1(nsLDAPBERElement, nsILDAPBERElement)
00046 
00047 nsLDAPBERElement::nsLDAPBERElement()
00048   : mElement(0)
00049 {
00050 }
00051 
00052 nsLDAPBERElement::~nsLDAPBERElement()
00053 {
00054   if (mElement) {
00055     // anything inside here is not something that we own separately from
00056     // this object, so free it
00057     ber_free(mElement, 1);
00058   }
00059 
00060   return;
00061 }
00062 
00063 NS_IMETHODIMP
00064 nsLDAPBERElement::Init(nsILDAPBERValue *aValue)
00065 {
00066   if (aValue) {
00067     return NS_ERROR_NOT_IMPLEMENTED;
00068   } 
00069 
00070   mElement = ber_alloc_t(LBER_USE_DER);
00071   return mElement ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00072 }
00073 
00074 /* void putString (in AUTF8String aString, in unsigned long aTag); */
00075 NS_IMETHODIMP
00076 nsLDAPBERElement::PutString(const nsACString & aString, PRUint32 aTag, 
00077                             PRUint32 *aBytesWritten)
00078 {
00079   // XXX if the string translation feature of the C SDK is ever used,
00080   // this const_cast will break
00081   int i = ber_put_ostring(mElement, 
00082                           NS_CONST_CAST(char *, 
00083                                         PromiseFlatCString(aString).get()),
00084                           aString.Length(), aTag);
00085 
00086   if (i < 0) {
00087     return NS_ERROR_FAILURE;
00088   }
00089 
00090   *aBytesWritten = i;
00091   return NS_OK;
00092 }
00093 
00094 /* void startSet (); */
00095 NS_IMETHODIMP nsLDAPBERElement::StartSet(PRUint32 aTag)
00096 {
00097   int i = ber_start_set(mElement, aTag);
00098 
00099   if (i < 0) {
00100     return NS_ERROR_FAILURE;
00101   }
00102 
00103   return NS_OK;
00104 }
00105 
00106 /* void putSet (); */
00107 NS_IMETHODIMP nsLDAPBERElement::PutSet(PRUint32 *aBytesWritten)
00108 {
00109   int i = ber_put_set(mElement);
00110 
00111   if (i < 0) {
00112     return NS_ERROR_FAILURE;
00113   }
00114 
00115   *aBytesWritten = i;
00116   return NS_OK;
00117 }
00118 
00119 /* nsILDAPBERValue flatten (); */
00120 NS_IMETHODIMP nsLDAPBERElement::GetAsValue(nsILDAPBERValue **_retval)
00121 {
00122   // create the value object
00123   nsCOMPtr<nsILDAPBERValue> berValue;
00124   NS_NEWXPCOM(berValue, nsLDAPBERValue);
00125 
00126   if (!berValue) {
00127     NS_ERROR("nsLDAPBERElement::GetAsValue(): out of memory"
00128              " creating nsLDAPBERValue object");
00129     return NS_ERROR_OUT_OF_MEMORY;
00130   }
00131 
00132   struct berval *bv;
00133   if ( ber_flatten(mElement, &bv) < 0 ) {
00134     return NS_ERROR_OUT_OF_MEMORY;
00135   }
00136 
00137   nsresult rv = berValue->Set(bv->bv_len, 
00138                               NS_REINTERPRET_CAST(PRUint8 *, bv->bv_val));
00139 
00140   // whether or not we've succeeded, we're done with the ldap c sdk struct
00141   ber_bvfree(bv);
00142 
00143   // as of this writing, this error can only be NS_ERROR_OUT_OF_MEMORY
00144   if (NS_FAILED(rv)) {
00145     return rv;
00146   }
00147 
00148   // return the raw interface pointer
00149   NS_ADDREF(*_retval = berValue.get());
00150 
00151   return NS_OK;
00152 }