PageRenderTime 37ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/libpkix/pkix/results/pkix_buildresult.c

http://github.com/zpao/v8monkey
C | 395 lines | 206 code | 79 blank | 110 comment | 4 complexity | 170716070b47f575bbd824e40bd3b500 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the PKIX-C library.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Sun Microsystems, Inc.
 18 * Portions created by the Initial Developer are
 19 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *   Sun Microsystems, Inc.
 23 *
 24 * Alternatively, the contents of this file may be used under the terms of
 25 * either the GNU General Public License Version 2 or later (the "GPL"), or
 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27 * in which case the provisions of the GPL or the LGPL are applicable instead
 28 * of those above. If you wish to allow use of your version of this file only
 29 * under the terms of either the GPL or the LGPL, and not to allow others to
 30 * use your version of this file under the terms of the MPL, indicate your
 31 * decision by deleting the provisions above and replace them with the notice
 32 * and other provisions required by the GPL or the LGPL. If you do not delete
 33 * the provisions above, a recipient may use your version of this file under
 34 * the terms of any one of the MPL, the GPL or the LGPL.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37/*
 38 * pkix_buildresult.c
 39 *
 40 * BuildResult Object Functions
 41 *
 42 */
 43
 44#include "pkix_buildresult.h"
 45
 46/* --Private-Functions-------------------------------------------- */
 47
 48/*
 49 * FUNCTION: pkix_BuildResult_Destroy
 50 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
 51 */
 52static PKIX_Error *
 53pkix_BuildResult_Destroy(
 54        PKIX_PL_Object *object,
 55        void *plContext)
 56{
 57        PKIX_BuildResult *result = NULL;
 58
 59        PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Destroy");
 60        PKIX_NULLCHECK_ONE(object);
 61
 62        /* Check that this object is a build result object */
 63        PKIX_CHECK(pkix_CheckType(object, PKIX_BUILDRESULT_TYPE, plContext),
 64                    PKIX_OBJECTNOTBUILDRESULT);
 65
 66        result = (PKIX_BuildResult *)object;
 67
 68        PKIX_DECREF(result->valResult);
 69        PKIX_DECREF(result->certChain);
 70
 71cleanup:
 72
 73        PKIX_RETURN(BUILDRESULT);
 74}
 75
 76/*
 77 * FUNCTION: pkix_BuildResult_Equals
 78 * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h)
 79 */
 80static PKIX_Error *
 81pkix_BuildResult_Equals(
 82        PKIX_PL_Object *first,
 83        PKIX_PL_Object *second,
 84        PKIX_Boolean *pResult,
 85        void *plContext)
 86{
 87        PKIX_UInt32 secondType;
 88        PKIX_Boolean cmpResult;
 89        PKIX_BuildResult *firstBuildResult = NULL;
 90        PKIX_BuildResult *secondBuildResult = NULL;
 91
 92        PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Equals");
 93        PKIX_NULLCHECK_THREE(first, second, pResult);
 94
 95        PKIX_CHECK(pkix_CheckType(first, PKIX_BUILDRESULT_TYPE, plContext),
 96                    PKIX_FIRSTOBJECTNOTBUILDRESULT);
 97
 98        PKIX_CHECK(PKIX_PL_Object_GetType(second, &secondType, plContext),
 99                    PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
100
101        *pResult = PKIX_FALSE;
102
103        if (secondType != PKIX_BUILDRESULT_TYPE) goto cleanup;
104
105        firstBuildResult = (PKIX_BuildResult *)first;
106        secondBuildResult = (PKIX_BuildResult *)second;
107
108        PKIX_CHECK(PKIX_PL_Object_Equals
109                    ((PKIX_PL_Object *)firstBuildResult->valResult,
110                    (PKIX_PL_Object *)secondBuildResult->valResult,
111                    &cmpResult,
112                    plContext),
113                    PKIX_OBJECTEQUALSFAILED);
114
115        if (!cmpResult) goto cleanup;
116
117        PKIX_CHECK(PKIX_PL_Object_Equals
118                    ((PKIX_PL_Object *)firstBuildResult->certChain,
119                    (PKIX_PL_Object *)secondBuildResult->certChain,
120                    &cmpResult,
121                    plContext),
122                    PKIX_OBJECTEQUALSFAILED);
123
124        if (!cmpResult) goto cleanup;
125
126        /*
127         * The remaining case is that both are null,
128         * which we consider equality.
129         *      cmpResult = PKIX_TRUE;
130         */
131
132        *pResult = cmpResult;
133
134cleanup:
135
136        PKIX_RETURN(BUILDRESULT);
137}
138
139/*
140 * FUNCTION: pkix_BuildResult_Hashcode
141 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
142 */
143static PKIX_Error *
144pkix_BuildResult_Hashcode(
145        PKIX_PL_Object *object,
146        PKIX_UInt32 *pHashcode,
147        void *plContext)
148{
149        PKIX_BuildResult *buildResult = NULL;
150        PKIX_UInt32 hash = 0;
151        PKIX_UInt32 valResultHash = 0;
152        PKIX_UInt32 certChainHash = 0;
153
154        PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Hashcode");
155        PKIX_NULLCHECK_TWO(object, pHashcode);
156
157        PKIX_CHECK(pkix_CheckType(object, PKIX_BUILDRESULT_TYPE, plContext),
158                    PKIX_OBJECTNOTBUILDRESULT);
159
160        buildResult = (PKIX_BuildResult*)object;
161
162        PKIX_CHECK(PKIX_PL_Object_Hashcode
163                    ((PKIX_PL_Object *)buildResult->valResult,
164                    &valResultHash,
165                    plContext),
166                    PKIX_OBJECTHASHCODEFAILED);
167
168        PKIX_CHECK(PKIX_PL_Object_Hashcode
169                    ((PKIX_PL_Object *)buildResult->certChain,
170                    &certChainHash,
171                    plContext),
172                    PKIX_OBJECTHASHCODEFAILED);
173
174        hash = 31*(31 * valResultHash + certChainHash);
175
176        *pHashcode = hash;
177
178cleanup:
179
180        PKIX_RETURN(BUILDRESULT);
181}
182
183/*
184 * FUNCTION: pkix_BuildResult_ToString
185 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
186 */
187static PKIX_Error *
188pkix_BuildResult_ToString(
189        PKIX_PL_Object *object,
190        PKIX_PL_String **pString,
191        void *plContext)
192{
193        PKIX_BuildResult *buildResult = NULL;
194        PKIX_PL_String *formatString = NULL;
195        PKIX_PL_String *buildResultString = NULL;
196
197        PKIX_ValidateResult *valResult = NULL;
198        PKIX_List *certChain = NULL;
199
200        PKIX_PL_String *valResultString = NULL;
201        PKIX_PL_String *certChainString = NULL;
202
203        char *asciiFormat =
204                "[\n"
205                "\tValidateResult: \t\t%s"
206                "\tCertChain:    \t\t%s\n"
207                "]\n";
208
209        PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_ToString");
210        PKIX_NULLCHECK_TWO(object, pString);
211
212        PKIX_CHECK(pkix_CheckType(object, PKIX_BUILDRESULT_TYPE, plContext),
213                PKIX_OBJECTNOTBUILDRESULT);
214
215        buildResult = (PKIX_BuildResult*)object;
216
217        valResult = buildResult->valResult;
218
219        PKIX_CHECK(PKIX_PL_String_Create
220                (PKIX_ESCASCII, asciiFormat, 0, &formatString, plContext),
221                PKIX_STRINGCREATEFAILED);
222
223        PKIX_CHECK(PKIX_PL_Object_ToString
224                ((PKIX_PL_Object *)valResult, &valResultString, plContext),
225                PKIX_OBJECTTOSTRINGFAILED);
226
227        certChain = buildResult->certChain;
228
229        PKIX_CHECK(PKIX_PL_Object_ToString
230                ((PKIX_PL_Object *)certChain, &certChainString, plContext),
231                PKIX_OBJECTTOSTRINGFAILED);
232
233        PKIX_CHECK(PKIX_PL_Sprintf
234                (&buildResultString,
235                plContext,
236                formatString,
237                valResultString,
238                certChainString),
239                PKIX_SPRINTFFAILED);
240
241        *pString = buildResultString;
242
243cleanup:
244
245        PKIX_DECREF(formatString);
246        PKIX_DECREF(valResultString);
247        PKIX_DECREF(certChainString);
248
249        PKIX_RETURN(BUILDRESULT);
250}
251
252/*
253 * FUNCTION: pkix_BuildResult_RegisterSelf
254 * DESCRIPTION:
255 *  Registers PKIX_BUILDRESULT_TYPE and its related functions with
256 *  systemClasses[]
257 * THREAD SAFETY:
258 *  Not Thread Safe - for performance and complexity reasons
259 *
260 *  Since this function is only called by PKIX_PL_Initialize, which should
261 *  only be called once, it is acceptable that this function is not
262 *  thread-safe.
263 */
264PKIX_Error *
265pkix_BuildResult_RegisterSelf(void *plContext)
266{
267
268        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
269        pkix_ClassTable_Entry entry;
270
271        PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_RegisterSelf");
272
273        entry.description = "BuildResult";
274        entry.objCounter = 0;
275        entry.typeObjectSize = sizeof(PKIX_BuildResult);
276        entry.destructor = pkix_BuildResult_Destroy;
277        entry.equalsFunction = pkix_BuildResult_Equals;
278        entry.hashcodeFunction = pkix_BuildResult_Hashcode;
279        entry.toStringFunction = pkix_BuildResult_ToString;
280        entry.comparator = NULL;
281        entry.duplicateFunction = pkix_duplicateImmutable;
282
283        systemClasses[PKIX_BUILDRESULT_TYPE] = entry;
284
285        PKIX_RETURN(BUILDRESULT);
286}
287
288/*
289 * FUNCTION: pkix_BuildResult_Create
290 * DESCRIPTION:
291 *
292 *  Creates a new BuildResult Object using the ValidateResult pointed to by
293 *  "valResult" and the List pointed to by "certChain", and stores it at
294 *  "pResult".
295 *
296 * PARAMETERS
297 *  "valResult"
298 *      Address of ValidateResult component. Must be non-NULL.
299 *  "certChain
300 *      Address of List component. Must be non-NULL.
301 *  "pResult"
302 *      Address where object pointer will be stored. Must be non-NULL.
303 *  "plContext"
304 *      Platform-specific context pointer.
305 * THREAD SAFETY:
306 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
307 * RETURNS:
308 *  Returns NULL if the function succeeds.
309 *  Returns a Fatal Error if the function fails in an unrecoverable way.
310 */
311PKIX_Error *
312pkix_BuildResult_Create(
313        PKIX_ValidateResult *valResult,
314        PKIX_List *certChain,
315        PKIX_BuildResult **pResult,
316        void *plContext)
317{
318        PKIX_BuildResult *result = NULL;
319
320        PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Create");
321        PKIX_NULLCHECK_THREE(valResult, certChain, pResult);
322
323        PKIX_CHECK(PKIX_PL_Object_Alloc
324                    (PKIX_BUILDRESULT_TYPE,
325                    sizeof (PKIX_BuildResult),
326                    (PKIX_PL_Object **)&result,
327                    plContext),
328                    PKIX_COULDNOTCREATEBUILDRESULTOBJECT);
329
330        /* initialize fields */
331
332        PKIX_INCREF(valResult);
333        result->valResult = valResult;
334
335        PKIX_INCREF(certChain);
336        result->certChain = certChain;
337
338        PKIX_CHECK(PKIX_List_SetImmutable(result->certChain, plContext),
339                     PKIX_LISTSETIMMUTABLEFAILED);
340
341        *pResult = result;
342        result = NULL;
343
344cleanup:
345
346        PKIX_DECREF(result);
347
348        PKIX_RETURN(BUILDRESULT);
349
350}
351
352/* --Public-Functions--------------------------------------------- */
353
354
355/*
356 * FUNCTION: PKIX_BuildResult_GetValidateResult
357 *      (see comments in pkix_result.h)
358 */
359PKIX_Error *
360PKIX_BuildResult_GetValidateResult(
361        PKIX_BuildResult *result,
362        PKIX_ValidateResult **pResult,
363        void *plContext)
364{
365        PKIX_ENTER(BUILDRESULT, "PKIX_BuildResult_GetValidateResult");
366        PKIX_NULLCHECK_TWO(result, pResult);
367
368        PKIX_INCREF(result->valResult);
369        *pResult = result->valResult;
370
371cleanup:
372        PKIX_RETURN(BUILDRESULT);
373}
374
375
376
377/*
378 * FUNCTION: PKIX_BuildResult_GetCertChain
379 *      (see comments in pkix_result.h)
380 */
381PKIX_Error *
382PKIX_BuildResult_GetCertChain(
383        PKIX_BuildResult *result,
384        PKIX_List **pChain,
385        void *plContext)
386{
387        PKIX_ENTER(BUILDRESULT, "PKIX_BuildResult_GetCertChain");
388        PKIX_NULLCHECK_TWO(result, pChain);
389
390        PKIX_INCREF(result->certChain);
391        *pChain = result->certChain;
392
393cleanup:
394        PKIX_RETURN(BUILDRESULT);
395}