1N/A/* BEGIN CSTYLED */
1N/A/* ***** BEGIN LICENSE BLOCK *****
1N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1N/A *
1N/A * The contents of this file are subject to the Mozilla Public License Version
1N/A * 1.1 (the "License"); you may not use this file except in compliance with
1N/A * the License. You may obtain a copy of the License at
1N/A * http://www.mozilla.org/MPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS IS" basis,
1N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1N/A * for the specific language governing rights and limitations under the
1N/A * License.
1N/A *
1N/A * The Original Code is the Netscape security libraries.
1N/A *
1N/A * The Initial Developer of the Original Code is
1N/A * Netscape Communications Corporation.
1N/A * Portions created by the Initial Developer are Copyright (C) 1994-2000
1N/A * the Initial Developer. All Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A *
1N/A * Alternatively, the contents of this file may be used under the terms of
1N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
1N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1N/A * in which case the provisions of the GPL or the LGPL are applicable instead
1N/A * of those above. If you wish to allow use of your version of this file only
1N/A * under the terms of either the GPL or the LGPL, and not to allow others to
1N/A * use your version of this file under the terms of the MPL, indicate your
1N/A * decision by deleting the provisions above and replace them with the notice
1N/A * and other provisions required by the GPL or the LGPL. If you do not delete
1N/A * the provisions above, a recipient may use your version of this file under
1N/A * the terms of any one of the MPL, the GPL or the LGPL.
1N/A *
1N/A * ***** END LICENSE BLOCK ***** */
1N/A/*
1N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A *
1N/A * Sun elects to use this software under the MPL license.
1N/A */
1N/A
1N/A/*
1N/A * Support routines for SECItem data structure.
1N/A *
1N/A * $Id: secitem.c,v 1.14 2006/05/22 22:24:34 wtchang%redhat.com Exp $
1N/A */
1N/A
1N/A#include <sys/types.h>
1N/A#include <sys/systm.h>
1N/A#include <sys/param.h>
1N/A#ifdef _KERNEL
1N/A#include <sys/kmem.h>
1N/A#else
1N/A#include <string.h>
1N/A#include <strings.h>
1N/A#include <assert.h>
1N/A#endif
1N/A#include "ec.h"
1N/A#include "ecl-curve.h"
1N/A#include "ecc_impl.h"
1N/A
1N/Avoid SECITEM_FreeItem(SECItem *, PRBool);
1N/A
1N/ASECItem *
1N/ASECITEM_AllocItem(PRArenaPool *arena, SECItem *item, unsigned int len,
1N/A int kmflag)
1N/A{
1N/A SECItem *result = NULL;
1N/A void *mark = NULL;
1N/A
1N/A if (arena != NULL) {
1N/A mark = PORT_ArenaMark(arena);
1N/A }
1N/A
1N/A if (item == NULL) {
1N/A if (arena != NULL) {
1N/A result = PORT_ArenaZAlloc(arena, sizeof(SECItem), kmflag);
1N/A } else {
1N/A result = PORT_ZAlloc(sizeof(SECItem), kmflag);
1N/A }
1N/A if (result == NULL) {
1N/A goto loser;
1N/A }
1N/A } else {
1N/A PORT_Assert(item->data == NULL);
1N/A result = item;
1N/A }
1N/A
1N/A result->len = len;
1N/A if (len) {
1N/A if (arena != NULL) {
1N/A result->data = PORT_ArenaAlloc(arena, len, kmflag);
1N/A } else {
1N/A result->data = PORT_Alloc(len, kmflag);
1N/A }
1N/A if (result->data == NULL) {
1N/A goto loser;
1N/A }
1N/A } else {
1N/A result->data = NULL;
1N/A }
1N/A
1N/A if (mark) {
1N/A PORT_ArenaUnmark(arena, mark);
1N/A }
1N/A return(result);
1N/A
1N/Aloser:
1N/A if ( arena != NULL ) {
1N/A if (mark) {
1N/A PORT_ArenaRelease(arena, mark);
1N/A }
1N/A }
1N/A SECITEM_FreeItem(result, (item == NULL) ? PR_TRUE : PR_FALSE);
1N/A /*
1N/A * If item is not NULL, the above also sets item->data and
1N/A * item->len to 0.
1N/A */
1N/A return(NULL);
1N/A}
1N/A
1N/ASECStatus
1N/ASECITEM_CopyItem(PRArenaPool *arena, SECItem *to, const SECItem *from,
1N/A int kmflag)
1N/A{
1N/A to->type = from->type;
1N/A if (from->data && from->len) {
1N/A if ( arena ) {
1N/A to->data = (unsigned char*) PORT_ArenaAlloc(arena, from->len,
1N/A kmflag);
1N/A } else {
1N/A to->data = (unsigned char*) PORT_Alloc(from->len, kmflag);
1N/A }
1N/A
1N/A if (!to->data) {
1N/A return SECFailure;
1N/A }
1N/A PORT_Memcpy(to->data, from->data, from->len);
1N/A to->len = from->len;
1N/A } else {
1N/A to->data = 0;
1N/A to->len = 0;
1N/A }
1N/A return SECSuccess;
1N/A}
1N/A
1N/Avoid
1N/ASECITEM_FreeItem(SECItem *zap, PRBool freeit)
1N/A{
1N/A if (zap) {
1N/A#ifdef _KERNEL
1N/A kmem_free(zap->data, zap->len);
1N/A#else
1N/A free(zap->data);
1N/A#endif
1N/A zap->data = 0;
1N/A zap->len = 0;
1N/A if (freeit) {
1N/A#ifdef _KERNEL
1N/A kmem_free(zap, sizeof (SECItem));
1N/A#else
1N/A free(zap);
1N/A#endif
1N/A }
1N/A }
1N/A}
1N/A/* END CSTYLED */
1N/A
1N/Avoid
1N/ASECITEM_ZfreeItem(SECItem *zap, PRBool freeit)
1N/A{
1N/A if (zap) {
1N/A PORT_ZFree(zap->data, zap->len);
1N/A zap->data = 0;
1N/A zap->len = 0;
1N/A if (freeit) {
1N/A PORT_ZFree(zap, sizeof (SECItem));
1N/A }
1N/A }
1N/A}