<commit_before> /* ***** BEGIN LICENSE BLOCK *****
 *
 * BBN Address and AS Number PKI Database/repository software
 * Version 3.0-beta
 *
 * US government users are permitted unrestricted rights as
 * defined in the FAR.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied.
 *
 * Copyright (C) Raytheon BBN Technologies Corp. 2008-2010.  All Rights Reserved.
 *
 * Contributor(s):  <NAME>
 *
 * ***** END LICENSE BLOCK ***** */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include "cryptlib.h"
#include "certificate.h"
#include <roa.h>
#include <keyfile.h>
#include <casn.h>
#include <asn.h>
#include <time.h>
#include "create_object.h"
#include "obj_err.h"
#include "roa.h"

#define IPv4 4
#define IPv6 6
#define ASNUM 8

extern char *signCMS(struct ROA *roap, char *keyfile, int bad); 
int  txt2loc(int typ, char *buf, struct iprange *iprangep);
static void make_IPAddrOrRange(struct IPAddressOrRangeA *ipAddrOrRangep,
			       struct iprange *tiprangep);

/**
 * Writes an EEcert into a ROA or Manifest
 *
 * author: <NAME>
 */
int write_EEcert(void* my_var, void* value)
{
  struct Certificate my_cert;
  struct ROA* roa = my_var;
  Certificate(&my_cert, (ushort)0);

  //read the EE certificate from file
  if( !(get_casn_file(&my_cert.self, (char*)value,0) < 0) ) 
    {
      struct SignedData *sgdp = &roa->content.signedData;
      //Clear the old one
      clear_casn(&sgdp->certificates.self);
      
      struct Certificate *sigcertp = (struct Certificate *)inject_casn(&sgdp->certificates.self, 0);
      
      //copy the new one in
      if(sigcertp != NULL)
	copy_casn(&sigcertp->self, &my_cert.self);
      else
	{
	  warn(1,"ERROR injecting EE cert");
	  return 1;
	}
    }
  else
    return 1;
  return SUCCESS;
}
/**
 * Writes an EEkey into a ROA or Manifest
 *
 * author: <NAME>
 */
int write_EEkey(void* my_var, void* value)
{
  //struct Certificate my_cert;
  struct ROA* roa = my_var;
  //Certificate(&my_cert, (ushort)0);
  char* c;

  if ((c = signCMS(roa, (char*)value, 0))){
    warn(1,"Error signing with the EE key file");
    return 1;
  }

  //read the EE certificate from file
  /* if( !(get_casn_file(&my_cert.self, (char*)value,0) < 0) )  */
/*     { */
/*       struct SignedData *sgdp = &roa->content.signedData; */
/*       //Clear the old one */
/*       clear_casn(&sgdp->certificates.self); */
      
/*       struct Certificate *sigcertp = (struct Certificate *)inject_casn(&sgdp->certificates.self, 0); */
      
/*       //copy the new one in */
/*       if(sigcertp != NULL) */
/* 	copy_casn(&sigcertp->self, &my_cert.self); */
/*       else */
/* 	{ */
/* 	  warn(1,"ERROR injecting EE cert"); */
/* 	  return 1; */
/* 	} */
/*     } */
/*   else */
/*     return 1; */
  return SUCCESS;
}

// read ascii valus from from_val and convert into hex in to_val
// not to_val buffer is allocated by the caller
int read_hex_val(char *from_val, int len, unsigned char *to_val)
{
  int i = 0, j;
  unsigned int byte;


  // if it doesn't start with '0x' then return without converting
  if (strncmp(from_val, "0x", 2) == 0)
    i = 2;
  
  j = 0;
  for (; i < len; i+=2)
    {
      sscanf(&from_val[i], "%2x",&byte);
      memcpy(&to_val[j],(unsigned char *) &byte, 1);
      j++;
    }

  return j;
}

char *stripQuotes(char *str)	  
{
  char *tmpval = str;
  char *end;

  if (strlen(tmpval) > 0)
    { 
      if (strncmp(tmpval, "\"", 1) == 0)
	tmpval++;
      end = tmpval+strlen(tmpval)-1;
      if (strncmp((char *)end,"\"", 1) == 0)
	*end = '\0';
      return tmpval;
    }

  return NULL;
}

/*
 * Alloc memory, copy string and strip white space.
 * return new string. 
 */
char *stripws(char *str)
{
  
  char *end;
  char *value;
  int len;

  // Trim leading space<commit_msg>Bugfix: Added defensive programming in stripQuotes(), to fix crash on bad config input.<commit_after> /* ***** BEGIN LICENSE BLOCK *****
 *
 * BBN Address and AS Number PKI Database/repository software
 * Version 3.0-beta
 *
 * US government users are permitted unrestricted rights as
 * defined in the FAR.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied.
 *
 * Copyright (C) Raytheon BBN Technologies Corp. 2008-2010.  All Rights Reserved.
 *
 * Contributor(s):  <NAME>
 *
 * ***** END LICENSE BLOCK ***** */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include "cryptlib.h"
#include "certificate.h"
#include <roa.h>
#include <keyfile.h>
#include <casn.h>
#include <asn.h>
#include <time.h>
#include "create_object.h"
#include "obj_err.h"
#include "roa.h"

#define IPv4 4
#define IPv6 6
#define ASNUM 8

extern char *signCMS(struct ROA *roap, char *keyfile, int bad); 
int  txt2loc(int typ, char *buf, struct iprange *iprangep);
static void make_IPAddrOrRange(struct IPAddressOrRangeA *ipAddrOrRangep,
			       struct iprange *tiprangep);

/**
 * Writes an EEcert into a ROA or Manifest
 *
 * author: <NAME>
 */
int write_EEcert(void* my_var, void* value)
{
  struct Certificate my_cert;
  struct ROA* roa = my_var;
  Certificate(&my_cert, (ushort)0);

  //read the EE certificate from file
  if( !(get_casn_file(&my_cert.self, (char*)value,0) < 0) ) 
    {
      struct SignedData *sgdp = &roa->content.signedData;
      //Clear the old one
      clear_casn(&sgdp->certificates.self);
      
      struct Certificate *sigcertp = (struct Certificate *)inject_casn(&sgdp->certificates.self, 0);
      
      //copy the new one in
      if(sigcertp != NULL)
	copy_casn(&sigcertp->self, &my_cert.self);
      else
	{
	  warn(1,"ERROR injecting EE cert");
	  return 1;
	}
    }
  else
    return 1;
  return SUCCESS;
}
/**
 * Writes an EEkey into a ROA or Manifest
 *
 * author: <NAME>
 */
int write_EEkey(void* my_var, void* value)
{
  //struct Certificate my_cert;
  struct ROA* roa = my_var;
  //Certificate(&my_cert, (ushort)0);
  char* c;

  if ((c = signCMS(roa, (char*)value, 0))){
    warn(1,"Error signing with the EE key file");
    return 1;
  }

  //read the EE certificate from file
  /* if( !(get_casn_file(&my_cert.self, (char*)value,0) < 0) )  */
/*     { */
/*       struct SignedData *sgdp = &roa->content.signedData; */
/*       //Clear the old one */
/*       clear_casn(&sgdp->certificates.self); */
      
/*       struct Certificate *sigcertp = (struct Certificate *)inject_casn(&sgdp->certificates.self, 0); */
      
/*       //copy the new one in */
/*       if(sigcertp != NULL) */
/* 	copy_casn(&sigcertp->self, &my_cert.self); */
/*       else */
/* 	{ */
/* 	  warn(1,"ERROR injecting EE cert"); */
/* 	  return 1; */
/* 	} */
/*     } */
/*   else */
/*     return 1; */
  return SUCCESS;
}

// read ascii valus from from_val and convert into hex in to_val
// not to_val buffer is allocated by the caller
int read_hex_val(char *from_val, int len, unsigned char *to_val)
{
  int i = 0, j;
  unsigned int byte;


  // if it doesn't start with '0x' then return without converting
  if (strncmp(from_val, "0x", 2) == 0)
    i = 2;
  
  j = 0;
  for (; i < len; i+=2)
    {
      sscanf(&from_val[i], "%2x",&byte);
      memcpy(&to_val[j],(unsigned char *) &byte, 1);
      j++;
    }

  return j;
}

char *stripQuotes(char *str)	  
{
  char *tmpval = str;
  char *end;

  if (!str)
    return NULL;

  if (strlen(tmpval) > 0)
    { 
      if (strncmp(tmpval, "\"", 1) == 0)
	tmpval++;
      end = tmpval+strlen(tmpval)-1;
      if (strncmp((char *)end,"\"", 1) == 0)
	*end = '\0';
      return tmpval;
    }

  return NULL;
}

/*
 * Alloc memory, copy string and strip white space.
 * return new string. 
 */
char *stripws(char *str)
{
  
  char *end;
  char *value;
  int len;

  // Trim leading space
{'fraction_non_alphanumeric': 0.15665236051502146, 'fraction_numerical': 0.012623074981065387, 'mean_word_length': 2.897196261682243, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4197526', 'n_tokens_mistral': 3068, 'n_tokens_neox': 2892, 'n_words': 825}