Back to index

lightning-sunbird  0.9+nobinonly
nsLDAPControl.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 "nsLDAPControl.h"
00041 #include "prmem.h"
00042 #include "plstr.h"
00043 #include "nsLDAPBERValue.h"
00044 
00045 NS_IMPL_ISUPPORTS1(nsLDAPControl, nsILDAPControl)
00046 
00047 nsLDAPControl::nsLDAPControl()
00048   : mIsCritical(PR_FALSE)
00049 {
00050 }
00051 
00052 nsLDAPControl::~nsLDAPControl()
00053 {
00054 }
00055 
00056 /* attribute ACString oid; */
00057 NS_IMETHODIMP nsLDAPControl::GetOid(nsACString & aOid)
00058 {
00059   aOid.Assign(mOid);
00060   return NS_OK;
00061 }
00062 NS_IMETHODIMP nsLDAPControl::SetOid(const nsACString & aOid)
00063 {
00064   mOid = aOid;
00065   return NS_OK;
00066 }
00067 
00068 /* attribute nsILDAPBERValue value; */
00069 NS_IMETHODIMP
00070 nsLDAPControl::GetValue(nsILDAPBERValue * *aValue)
00071 {
00072   NS_IF_ADDREF(*aValue = mValue);
00073   return NS_OK;
00074 }
00075 
00076 NS_IMETHODIMP
00077 nsLDAPControl::SetValue(nsILDAPBERValue * aValue)
00078 {
00079   mValue = aValue;
00080   return NS_OK;
00081 }
00082 
00083 /* attribute boolean isCritical; */
00084 NS_IMETHODIMP 
00085 nsLDAPControl::GetIsCritical(PRBool *aIsCritical)
00086 {
00087   *aIsCritical = mIsCritical;
00088   return NS_OK;
00089 }
00090 NS_IMETHODIMP
00091 nsLDAPControl::SetIsCritical(PRBool aIsCritical)
00092 {
00093   mIsCritical = aIsCritical;
00094   return NS_OK;
00095 }
00096 
00100 nsresult
00101 nsLDAPControl::ToLDAPControl(LDAPControl **control)
00102 {
00103   // because nsLDAPProtocolModule::Init calls prldap_install_routines we know
00104   // that the C SDK will be using the NSPR allocator under the hood, so our
00105   // callers will therefore be able to use ldap_control_free() and friends on
00106   // this control.
00107   LDAPControl *ctl = NS_STATIC_CAST(LDAPControl *, 
00108                                     PR_Calloc(1, sizeof(LDAPControl)));
00109   if (!ctl) {
00110     return NS_ERROR_OUT_OF_MEMORY;
00111   }
00112 
00113   // need to ensure that this string is also alloced by PR_Alloc
00114   ctl->ldctl_oid = PL_strdup(mOid.get());
00115   if (!ctl->ldctl_oid) {
00116     PR_Free(ctl);
00117     return NS_ERROR_OUT_OF_MEMORY;
00118   }
00119 
00120   ctl->ldctl_iscritical = mIsCritical;
00121 
00122   if (!mValue) {
00123     // no data associated with this control
00124     ctl->ldctl_value.bv_len = 0;
00125     ctl->ldctl_value.bv_val = 0;
00126   } else {
00127 
00128     // just to make the code below a bit more readable
00129     nsLDAPBERValue *nsBerVal = 
00130       NS_STATIC_CAST(nsLDAPBERValue *, NS_STATIC_CAST(nsILDAPBERValue *,
00131                                                       mValue.get()));
00132     ctl->ldctl_value.bv_len = nsBerVal->mSize;
00133 
00134     if (!nsBerVal->mSize) {
00135       // a zero-length value is associated with this control
00136       return NS_ERROR_NOT_IMPLEMENTED;
00137     } else {
00138 
00139       // same for the berval itself
00140       ctl->ldctl_value.bv_len = nsBerVal->mSize;
00141       ctl->ldctl_value.bv_val = NS_STATIC_CAST(char *,
00142                                                PR_Malloc(nsBerVal->mSize));
00143       if (!ctl->ldctl_value.bv_val) {
00144         ldap_control_free(ctl);
00145         return NS_ERROR_OUT_OF_MEMORY;
00146       }
00147   
00148       memcpy(ctl->ldctl_value.bv_val, nsBerVal->mValue,
00149              ctl->ldctl_value.bv_len);
00150     }
00151   }
00152 
00153   *control = ctl;
00154 
00155   return NS_OK;
00156 }