Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/secitem.c
38918 views
/*1* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.2* Use is subject to license terms.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public6* License as published by the Free Software Foundation; either7* version 2.1 of the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public License15* along with this library; if not, write to the Free Software Foundation,16* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* *********************************************************************24*25* The Original Code is the Netscape security libraries.26*27* The Initial Developer of the Original Code is28* Netscape Communications Corporation.29* Portions created by the Initial Developer are Copyright (C) 1994-200030* the Initial Developer. All Rights Reserved.31*32* Contributor(s):33*34* Last Modified Date from the Original Code: March 201235*********************************************************************** */3637/*38* Support routines for SECItem data structure.39*40* $Id: secitem.c,v 1.14 2006/05/22 22:24:34 wtchang%redhat.com Exp $41*/4243#include <sys/types.h>4445#ifndef _WIN3246#if !defined(__linux__) && !defined(_ALLBSD_SOURCE)47#include <sys/systm.h>48#endif /* __linux__ || _ALLBSD_SOURCE */49#include <sys/param.h>50#endif /* _WIN32 */5152#ifdef _KERNEL53#include <sys/kmem.h>54#else55#include <string.h>5657#ifndef _WIN3258#include <strings.h>59#endif /* _WIN32 */6061#include <assert.h>62#endif63#include "ec.h"64#include "ecl-curve.h"65#include "ecc_impl.h"6667void SECITEM_FreeItem(SECItem *, PRBool);6869SECItem *70SECITEM_AllocItem(PRArenaPool *arena, SECItem *item, unsigned int len,71int kmflag)72{73SECItem *result = NULL;74void *mark = NULL;7576if (arena != NULL) {77mark = PORT_ArenaMark(arena);78}7980if (item == NULL) {81if (arena != NULL) {82result = PORT_ArenaZAlloc(arena, sizeof(SECItem), kmflag);83} else {84result = PORT_ZAlloc(sizeof(SECItem), kmflag);85}86if (result == NULL) {87goto loser;88}89} else {90PORT_Assert(item->data == NULL);91result = item;92}9394result->len = len;95if (len) {96if (arena != NULL) {97result->data = PORT_ArenaAlloc(arena, len, kmflag);98} else {99result->data = PORT_Alloc(len, kmflag);100}101if (result->data == NULL) {102goto loser;103}104} else {105result->data = NULL;106}107108if (mark) {109PORT_ArenaUnmark(arena, mark);110}111return(result);112113loser:114if ( arena != NULL ) {115if (mark) {116PORT_ArenaRelease(arena, mark);117}118if (item != NULL) {119item->data = NULL;120item->len = 0;121}122} else {123if (result != NULL) {124SECITEM_FreeItem(result, (item == NULL) ? PR_TRUE : PR_FALSE);125}126/*127* If item is not NULL, the above has set item->data and128* item->len to 0.129*/130}131return(NULL);132}133134SECStatus135SECITEM_CopyItem(PRArenaPool *arena, SECItem *to, const SECItem *from,136int kmflag)137{138to->type = from->type;139if (from->data && from->len) {140if ( arena ) {141to->data = (unsigned char*) PORT_ArenaAlloc(arena, from->len,142kmflag);143} else {144to->data = (unsigned char*) PORT_Alloc(from->len, kmflag);145}146147if (!to->data) {148return SECFailure;149}150PORT_Memcpy(to->data, from->data, from->len);151to->len = from->len;152} else {153to->data = 0;154to->len = 0;155}156return SECSuccess;157}158159void160SECITEM_FreeItem(SECItem *zap, PRBool freeit)161{162if (zap) {163#ifdef _KERNEL164kmem_free(zap->data, zap->len);165#else166free(zap->data);167#endif168zap->data = 0;169zap->len = 0;170if (freeit) {171#ifdef _KERNEL172kmem_free(zap, sizeof (SECItem));173#else174free(zap);175#endif176}177}178}179180181