Back to index

lightning-sunbird  0.9+nobinonly
libc.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: libc.c,v $ $Revision: 1.3 $ $Date: 2005/01/20 02:25:45 $";
00039 #endif /* DEBUG */
00040 
00041 /*
00042  * libc.c
00043  *
00044  * This file contains our wrappers/reimplementations for "standard" 
00045  * libc functions.  Things like "memcpy."  We add to this as we need 
00046  * it.  Oh, and let's keep it in alphabetical order, should it ever 
00047  * get large.  Most string/character stuff should be in utf8.c, not 
00048  * here.  This file (and maybe utf8.c) should be the only ones in
00049  * NSS to include files with angle brackets.
00050  */
00051 
00052 #ifndef BASE_H
00053 #include "base.h"
00054 #endif /* BASE_H */
00055 
00056 #include <string.h> /* memcpy, memset */
00057 
00058 /*
00059  * nsslibc_memcpy
00060  * nsslibc_memset
00061  * nsslibc_offsetof
00062  * nsslibc_memequal
00063  */
00064 
00065 /*
00066  * nsslibc_memcpy
00067  *
00068  * Errors:
00069  *  NSS_ERROR_INVALID_POINTER
00070  *
00071  * Return value:
00072  *  NULL on error
00073  *  The destination pointer on success
00074  */
00075 
00076 NSS_IMPLEMENT void *
00077 nsslibc_memcpy
00078 (
00079   void *dest,
00080   const void *source,
00081   PRUint32 n
00082 )
00083 {
00084 #ifdef NSSDEBUG
00085   if( ((void *)NULL == dest) || ((const void *)NULL == source) ) {
00086     nss_SetError(NSS_ERROR_INVALID_POINTER);
00087     return (void *)NULL;
00088   }
00089 #endif /* NSSDEBUG */
00090 
00091   return memcpy(dest, source, (size_t)n);
00092 }
00093 
00094 /*
00095  * nsslibc_memset
00096  *
00097  * Errors:
00098  *  NSS_ERROR_INVALID_POINTER
00099  *
00100  * Return value:
00101  *  NULL on error
00102  *  The destination pointer on success
00103  */
00104 
00105 NSS_IMPLEMENT void *
00106 nsslibc_memset
00107 (
00108   void *dest,
00109   PRUint8 byte,
00110   PRUint32 n
00111 )
00112 {
00113 #ifdef NSSDEBUG
00114   if( ((void *)NULL == dest) ) {
00115     nss_SetError(NSS_ERROR_INVALID_POINTER);
00116     return (void *)NULL;
00117   }
00118 #endif /* NSSDEBUG */
00119 
00120   return memset(dest, (int)byte, (size_t)n);
00121 }
00122 
00123 /*
00124  * nsslibc_memequal
00125  *
00126  * Errors:
00127  *  NSS_ERROR_INVALID_POINTER
00128  *
00129  * Return value:
00130  *  PR_TRUE if they match
00131  *  PR_FALSE if they don't
00132  *  PR_FALSE upon error
00133  */
00134 
00135 NSS_IMPLEMENT PRBool
00136 nsslibc_memequal
00137 (
00138   const void *a,
00139   const void *b,
00140   PRUint32 len,
00141   PRStatus *statusOpt
00142 )
00143 {
00144 #ifdef NSSDEBUG
00145   if( (((void *)NULL == a) || ((void *)NULL == b)) ) {
00146     nss_SetError(NSS_ERROR_INVALID_POINTER);
00147     if( (PRStatus *)NULL != statusOpt ) {
00148       *statusOpt = PR_FAILURE;
00149     }
00150     return PR_FALSE;
00151   }
00152 #endif /* NSSDEBUG */
00153 
00154   if( (PRStatus *)NULL != statusOpt ) {
00155     *statusOpt = PR_SUCCESS;
00156   }
00157 
00158   if( 0 == memcmp(a, b, len) ) {
00159     return PR_TRUE;
00160   } else {
00161     return PR_FALSE;
00162   }
00163 }
00164 
00165 /*
00166  * nsslibc_memcmp
00167  */
00168 
00169 NSS_IMPLEMENT PRInt32
00170 nsslibc_memcmp
00171 (
00172   const void *a,
00173   const void *b,
00174   PRUint32 len,
00175   PRStatus *statusOpt
00176 )
00177 {
00178   int v;
00179 
00180 #ifdef NSSDEBUG
00181   if( (((void *)NULL == a) || ((void *)NULL == b)) ) {
00182     nss_SetError(NSS_ERROR_INVALID_POINTER);
00183     if( (PRStatus *)NULL != statusOpt ) {
00184       *statusOpt = PR_FAILURE;
00185     }
00186     return -2;
00187   }
00188 #endif /* NSSDEBUG */
00189 
00190   if( (PRStatus *)NULL != statusOpt ) {
00191     *statusOpt = PR_SUCCESS;
00192   }
00193 
00194   v = memcmp(a, b, len);
00195   return (PRInt32)v;
00196 }
00197 
00198 /*
00199  * offsetof is a preprocessor definition
00200  */