<commit_before>//  File: util.cc
//  Author: <NAME> <<EMAIL>>
//
//  Description: Implementation of utility methods for the Tao.
//
//  Copyright (c) 2013, Google Inc.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tao/util.h"

#include <dirent.h>
#include <fcntl.h>
#include <ftw.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/unistd.h>

#include <memory>
#include <mutex>
#include <vector>

#include <glog/logging.h>
#include <keyczar/base/base64w.h>
#include <keyczar/base/file_util.h>
#include <keyczar/base/json_reader.h>
#include <keyczar/base/json_writer.h>
#include <keyczar/base/values.h>
#include <keyczar/crypto_factory.h>
#include <keyczar/keyczar.h>
#include <keyczar/rw/keyset_encrypted_file_reader.h>
#include <keyczar/rw/keyset_encrypted_file_writer.h>
#include <keyczar/rw/keyset_file_reader.h>
#include <keyczar/rw/keyset_file_writer.h>
#include <keyczar/rw/keyset_writer.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>

#include "tao/kvm_unix_tao_child_channel.h"
#include "tao/pipe_tao_child_channel.h"
#include "tao/signature.pb.h"
#include "tao/tao_domain.h"

using std::ios;
using std::mutex;
using std::ofstream;
using std::shared_ptr;
using std::vector;

using keyczar::Crypter;
using keyczar::CryptoFactory;
using keyczar::Key;
using keyczar::KeyPurpose;
using keyczar::KeyStatus;
using keyczar::KeyType;
using keyczar::Keyczar;
using keyczar::Keyset;
using keyczar::KeysetMetadata;
using keyczar::MessageDigestImpl;
using keyczar::Signer;
using keyczar::Verifier;
using keyczar::base::Base64WDecode;
using keyczar::base::Base64WEncode;
using keyczar::base::CreateDirectory;
using keyczar::base::JSONReader;
using keyczar::base::JSONWriter;
using keyczar::base::PathExists;
using keyczar::base::ReadFileToString;
using keyczar::base::ScopedSafeString;
using keyczar::base::WriteStringToFile;
using keyczar::rw::KeysetEncryptedJSONFileReader;
using keyczar::rw::KeysetEncryptedJSONFileWriter;
using keyczar::rw::KeysetJSONFileReader;
using keyczar::rw::KeysetJSONFileWriter;
using keyczar::rw::KeysetPBEJSONFileReader;
using keyczar::rw::KeysetPBEJSONFileWriter;
using keyczar::rw::KeysetReader;
using keyczar::rw::KeysetWriter;

namespace tao {

/// 20 MB is the maximum allowed message on our channel implementations.
static size_t MaxChannelMessage = 20 * 1024 * 1024;

void fd_close(int *fd) {
  if (fd) {
    if (*fd >= 0) {
      if (close(*fd) < 0) {
        PLOG(ERROR) << "Could not close file descriptor " << *fd;
      }
    }

    delete fd;
  }

  return;
}

void file_close(FILE *file) {
  if (file) fclose(file);
}

void temp_file_cleaner(string *dir) {
  if (dir) {
    if (!keyczar::base::Delete(FilePath(*dir), true /* recursive */))
      PLOG(ERROR) << "Could not remove temp directory " << *dir;
    delete dir;
  }
}

vector<shared_ptr<mutex> > locks;

void locking_function(int mode, int n, const char *file, int line) {
  if (mode & CRYPTO_LOCK) {
    locks[n]->lock();
  } else {<commit_msg>scspell, formatting<commit_after>//  File: util.cc
//  Author: <NAME> <<EMAIL>>
//
//  Description: Implementation of utility methods for the Tao.
//
//  Copyright (c) 2013, Google Inc.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tao/util.h"

#include <dirent.h>
#include <fcntl.h>
#include <ftw.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/unistd.h>

#include <memory>
#include <mutex>
#include <vector>

#include <glog/logging.h>
#include <keyczar/base/base64w.h>
#include <keyczar/base/file_util.h>
#include <keyczar/base/json_reader.h>
#include <keyczar/base/json_writer.h>
#include <keyczar/base/values.h>
#include <keyczar/crypto_factory.h>
#include <keyczar/keyczar.h>
#include <keyczar/rw/keyset_encrypted_file_reader.h>
#include <keyczar/rw/keyset_encrypted_file_writer.h>
#include <keyczar/rw/keyset_file_reader.h>
#include <keyczar/rw/keyset_file_writer.h>
#include <keyczar/rw/keyset_writer.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>

#include "tao/kvm_unix_tao_child_channel.h"
#include "tao/pipe_tao_child_channel.h"
#include "tao/signature.pb.h"
#include "tao/tao_domain.h"

using std::ios;
using std::mutex;
using std::ofstream;
using std::shared_ptr;
using std::vector;

using keyczar::Crypter;
using keyczar::CryptoFactory;
using keyczar::Key;
using keyczar::KeyPurpose;
using keyczar::KeyStatus;
using keyczar::KeyType;
using keyczar::Keyczar;
using keyczar::Keyset;
using keyczar::KeysetMetadata;
using keyczar::MessageDigestImpl;
using keyczar::Signer;
using keyczar::Verifier;
using keyczar::base::Base64WDecode;
using keyczar::base::Base64WEncode;
using keyczar::base::CreateDirectory;
using keyczar::base::JSONReader;
using keyczar::base::JSONWriter;
using keyczar::base::PathExists;
using keyczar::base::ReadFileToString;
using keyczar::base::ScopedSafeString;
using keyczar::base::WriteStringToFile;
using keyczar::rw::KeysetEncryptedJSONFileReader;
using keyczar::rw::KeysetEncryptedJSONFileWriter;
using keyczar::rw::KeysetJSONFileReader;
using keyczar::rw::KeysetJSONFileWriter;
using keyczar::rw::KeysetPBEJSONFileReader;
using keyczar::rw::KeysetPBEJSONFileWriter;
using keyczar::rw::KeysetReader;
using keyczar::rw::KeysetWriter;

namespace tao {

/// 20 MB is the maximum allowed message on our channel implementations.
static size_t MaxChannelMessage = 20 * 1024 * 1024;

void fd_close(int *fd) {
  if (fd && *fd >= 0) {
    if (close(*fd) < 0) {
      PLOG(ERROR) << "Could not close file descriptor " << *fd;
    }
    delete fd;
  }
}

void file_close(FILE *file) {
  if (file) fclose(file);
}

void temp_file_cleaner(string *dir) {
  if (dir) {
    if (!keyczar::base::Delete(FilePath(*dir), true /* recursive */))
      PLOG(ERROR) << "Could not remove temp directory " << *dir;
    delete dir;
  }
}

vector<shared_ptr<mutex>> locks;

void locking_function(int mode, int n, const char *file, int line) {
  if (mode & CRYPTO_LOCK) {
    locks[n]->lock();
  } else {
{'fraction_non_alphanumeric': 0.14004078857919783, 'fraction_numerical': 0.009517335146159076, 'mean_word_length': 5.276450511945392, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2681950', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2622, 'n_words': 645}