<commit_before>// Copyright 2004-present Facebook. All Rights Reserved.

//#include <algorithm>
//#include <map>
#include <string>
//#include <unordered_set>

#include <stdio.h>
#include <stdlib.h>

//#include <boost/filesystem/operations.hpp>
//#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>

#include <IOKit/IOKitLib.h>
#include <IOKit/IOKitKeys.h>
#include <CoreFoundation/CoreFoundation.h>

#include <glog/logging.h>

#include "osquery/core.h"
#include "osquery/database.h"

using namespace osquery::core;
using namespace osquery::db;

namespace osquery { namespace tables {

void genVariable(const void *key, const void *value, void *results) {  
  Row nvram_row;
  
  // Get the OF variable's name.
  CFIndex nameLen;
  char *nameBuffer = 0;

  nameLen = CFStringGetLength((CFStringRef) key) + 1;
  nameBuffer = (char*) malloc(nameLen);
  if(nameBuffer && CFStringGetCString((CFStringRef) key, nameBuffer, nameLen, 
      kCFStringEncodingUTF8) ) {
    nvram_row["name"] = boost::lexical_cast<std::string>(nameBuffer);
  } else {
    LOG(WARNING) << "Unable to convert NVRAM property name to C string";
    goto cleanup;
  }

  // Get the OF variable's type.
  CFTypeID typeID;
  CFIndex typeLen;
  char *typeBuffer;

  typeID = CFGetTypeID(value);
  typeLen = CFStringGetLength(CFCopyTypeIDDescription(typeID)) + 1;
  typeBuffer = (char*) malloc(typeLen);
  if ( typeBuffer && CFStringGetCString(CFCopyTypeIDDescription(typeID), 
      typeBuffer, typeLen, kCFStringEncodingUTF8) ) {
    nvram_row["type"] = boost::lexical_cast<std::string>(typeBuffer);
    free(typeBuffer);
  }

  // Variable type casting members.
  long          cnt, cnt2;
  const uint8_t *dataPtr;
  uint8_t       dataChar;
  char          numberBuffer[10];
  char          *dataBuffer = 0;
  CFIndex       valueLen;
  char          *valueBuffer = 0;
  const char    *valueString = 0;
  uint32_t      number, length;

  // Based on the type, get a texual representation of the variable.
  if (typeID == CFBooleanGetTypeID()) {
    valueString = (CFBooleanGetValue((CFBooleanRef) value)) ? "true" : "false";
  } else if (typeID == CFNumberGetTypeID()) {
    CFNumberGetValue((CFNumberRef) value, kCFNumberSInt32Type, &number);
    if (number == 0xFFFFFFFF) { 
      sprintf(numberBuffer, "-1"); 
    } else if (number < 1000) { 
      sprintf(numberBuffer, "%d", number); 
    } else { 
      sprintf(numberBuffer, "0x%x", number); 
    }
    valueString = numberBuffer;
  } else if (typeID == CFStringGetTypeID()) {
    valueLen = CFStringGetLength((CFStringRef) value) + 1;
    valueBuffer = (char*) malloc(valueLen + 1);
    if ( valueBuffer && CFStringGetCString((CFStringRef) value, valueBuffer, 
        valueLen, kCFStringEncodingUTF8) ) {
      valueString = valueBuffer;
    } else {
      LOG(WARNING) << "Unable to convert NVRAM value to C string";
      goto cleanup;
    }
  } else if (typeID == CFDataGetTypeID()) {
    length = CFDataGetLength((CFDataRef) value);
    if (length == 0) {
      valueString = "";
    } else {
      dataBuffer = (char*) malloc(length * 3 + 1);
      if (dataBuffer != 0) {
        dataPtr = CFDataGetBytePtr((CFDataRef) value);<commit_msg>[vtable_nvram] Various code cleanups<commit_after>// Copyright 2004-present Facebook. All Rights Reserved.

#include <string>
#include <stdio.h>
#include <stdlib.h>

#include <boost/lexical_cast.hpp>

#include <IOKit/IOKitLib.h>
#include <IOKit/IOKitKeys.h>
#include <CoreFoundation/CoreFoundation.h>

#include <glog/logging.h>

#include "osquery/core.h"
#include "osquery/database.h"

using namespace osquery::core;
using namespace osquery::db;

namespace osquery { namespace tables {

void genVariable(const void *key, const void *value, void *results) {  
  Row nvram_row;
  
  // Get the OF variable's name.
  CFIndex nameLen;
  char *nameBuffer = 0;

  nameLen = CFStringGetLength((CFStringRef) key) + 1;
  nameBuffer = (char*) malloc(nameLen);
  if(nameBuffer && CFStringGetCString((CFStringRef) key, nameBuffer, nameLen, 
      kCFStringEncodingUTF8)) {
    nvram_row["name"] = boost::lexical_cast<std::string>(nameBuffer);
  } else {
    LOG(WARNING) << "Unable to convert NVRAM property name to C string";
    goto cleanup;
  }

  // Get the OF variable's type.
  CFTypeID typeID;
  CFIndex typeLen;
  char *typeBuffer;

  typeID = CFGetTypeID(value);
  typeLen = CFStringGetLength(CFCopyTypeIDDescription(typeID)) + 1;
  typeBuffer = (char*) malloc(typeLen);
  if (typeBuffer && CFStringGetCString(CFCopyTypeIDDescription(typeID), 
      typeBuffer, typeLen, kCFStringEncodingUTF8)) {
    nvram_row["type"] = boost::lexical_cast<std::string>(typeBuffer);
    free(typeBuffer);
  }

  // Variable type casting members.
  long          cnt, cnt2;
  const uint8_t *dataPtr;
  uint8_t       dataChar;
  char          numberBuffer[10];
  char          *dataBuffer = 0;
  CFIndex       valueLen;
  char          *valueBuffer = 0;
  const char    *valueString = 0;
  uint32_t      number, length;

  // Based on the type, get a texual representation of the variable.
  if (typeID == CFBooleanGetTypeID()) {
    valueString = (CFBooleanGetValue((CFBooleanRef) value)) ? "true" : "false";
  } else if (typeID == CFNumberGetTypeID()) {
    CFNumberGetValue((CFNumberRef) value, kCFNumberSInt32Type, &number);
    if (number == 0xFFFFFFFF) { 
      sprintf(numberBuffer, "-1"); 
    } else if (number < 1000) { 
      sprintf(numberBuffer, "%d", number); 
    } else { 
      sprintf(numberBuffer, "0x%x", number); 
    }
    valueString = numberBuffer;
  } else if (typeID == CFStringGetTypeID()) {
    valueLen = CFStringGetLength((CFStringRef) value) + 1;
    valueBuffer = (char*) malloc(valueLen);
    if (valueBuffer && CFStringGetCString((CFStringRef) value, valueBuffer, 
        valueLen, kCFStringEncodingUTF8)) {
      valueString = valueBuffer;
    } else {
      LOG(WARNING) << "Unable to convert NVRAM value to C string";
      goto cleanup;
    }
  } else if (typeID == CFDataGetTypeID()) {
    length = CFDataGetLength((CFDataRef) value);
    if (length == 0) {
      valueString = "";
    } else {
      dataBuffer = (char*) malloc(length * 3 + 1);
      if (dataBuffer != 0) {
        dataPtr = CFDataGetBytePtr((CFDataRef) value);
{'fraction_non_alphanumeric': 0.12732520846696602, 'fraction_numerical': 0.011064785118665812, 'mean_word_length': 3.579295154185022, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6415234', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2027, 'n_words': 499}