/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.hadoop.hive.ql.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.SerDeInfo;
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
import org.apache.hadoop.hive.ql.metadata.Hive;
import org.apache.hadoop.hive.ql.security.authorization.HDFSPermissionPolicyProvider;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject.HivePrivilegeObjectType;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveResourceACLs;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test cases for privilege synchronizer for storage based authorizer
 */
public class TestHDFSPermissionPolicyProvider {
  private static MiniDFSCluster mDfs;
  private static IMetaStoreClient client;
  private static Configuration conf;
  private static String defaultTbl1Loc, defaultTbl2Loc, db1Loc, db1Tbl1Loc;

  @BeforeClass
  public static void setup() throws Exception {
    mDfs = new MiniDFSCluster.Builder(new Configuration()).numDataNodes(1).format(true).build();
    conf = new Configuration();
    conf.set("fs.defaultFS", "hdfs://" + mDfs.getNameNode().getHostAndPort());
    String warehouseLocation = "hdfs://" + mDfs.getNameNode().getHostAndPort()
        + MetastoreConf.ConfVars.WAREHOUSE.getDefaultVal();
    conf.set(MetastoreConf.ConfVars.WAREHOUSE.getVarname(), warehouseLocation);
    conf.set(MetastoreConf.ConfVars.AUTO_CREATE_ALL.getVarname(), "true");
    conf.set(MetastoreConf.ConfVars.SCHEMA_VERIFICATION.getVarname(), "false");
    client = Hive.get(conf, TestHDFSPermissionPolicyProvider.class).getMSC();

    try {
      client.dropTable("default", "tbl1");
    } catch (Exception e) {
    }
    try {
      client.dropTable("default", "tbl2");
    } catch (Exception e) {
    }
    try {
      client.dropTable("db1", "tbl1");
    } catch (Exception e) {
    }
    try {
      client.dropDatabase("db1");
    } catch (Exception e) {
    }

    defaultTbl1Loc = warehouseLocation + "/tbl1";
    defaultTbl2Loc = warehouseLocation + "/tbl2";
    db1Loc = warehouseLocation + "/db1";
    db1Tbl1Loc = warehouseLocation + "/db1/tbl1";

    int now = (int)System.currentTimeMillis() / 1000;
    FieldSchema col1 = new FieldSchema("col1", "int", "no comment");
    List<FieldSchema> cols = new ArrayList<FieldSchema>();
    cols.add(col1);
    SerDeInfo serde = new SerDeInfo("serde", "seriallib", null);
    StorageDescriptor sd =
        new StorageDescriptor(cols, defaultTbl1Loc, "input", "output", false, 0, serde, null, null,
            new HashMap<String, String>());
    Table tbl =
        new Table("tbl1", "default", "foo", now, now, 0, sd, null,
            new HashMap<String, String>(), null, null, TableType.MANAGED_TABLE.toString());
    client.createTable(tbl);

    sd = new StorageDescriptor(cols, defaultTbl2Loc, "input", "output", false, 0, serde,
        null, null, new HashMap<String, String>());
    tbl = new Table("tbl2", "default", "foo", now, now, 0, sd, null,
            new HashMap<String, String>(), null, null, TableType.MANAGED_TABLE.toString());
    client.createTable(tbl);

    Database db = new Database("db1", "no description", db1Loc, new HashMap<String, String>());
    client.createDatabase(db);

    sd = new StorageDescriptor(cols, db1Tbl1Loc, "input", "output", false, 0, serde, null, null,
            new HashMap<String, String>());
    tbl = new Table("tbl1", "db1", "foo", now, now, 0, sd, null,
            new HashMap<String, String>(), null, null, TableType.MANAGED_TABLE.toString());
    client.createTable(tbl);
  }

  @Test
  public void testPolicyProvider() throws Exception {
    HDFSPermissionPolicyProvider policyProvider = new HDFSPermissionPolicyProvider(conf);
    FileSystem fs = FileSystem.get(conf);
    fs.setOwner(new Path(defaultTbl1Loc), "user1", "group1");
    fs.setOwner(new Path(defaultTbl2Loc), "user1", "group1");
    fs.setOwner(new Path(db1Loc), "user1", "group1");
    fs.setOwner(new Path(db1Tbl1Loc), "user1", "group1");
    fs.setPermission(new Path(defaultTbl1Loc), new FsPermission("444")); // r--r--r--
    HiveResourceACLs acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "default", "tbl1"));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertTrue(acls.getUserPermissions().keySet().contains("user1"));
    assertEquals(acls.getGroupPermissions().size(), 2);
    assertTrue(acls.getGroupPermissions().keySet().contains("group1"));
    assertTrue(acls.getGroupPermissions().keySet().contains("public"));

    fs.setPermission(new Path(defaultTbl1Loc), new FsPermission("440")); // r--r-----
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "default", "tbl1"));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertEquals(acls.getUserPermissions().keySet().iterator().next(), "user1");
    assertEquals(acls.getGroupPermissions().size(), 1);
    assertTrue(acls.getGroupPermissions().keySet().contains("group1"));

    fs.setPermission(new Path(defaultTbl1Loc), new FsPermission("404")); // r-----r--
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "default", "tbl1"));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertTrue(acls.getUserPermissions().keySet().contains("user1"));
    assertEquals(acls.getGroupPermissions().size(), 1);
    assertTrue(acls.getGroupPermissions().keySet().contains("public"));

    fs.setPermission(new Path(defaultTbl1Loc), new FsPermission("400")); // r--------
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "default", "tbl1"));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertTrue(acls.getUserPermissions().keySet().contains("user1"));
    assertEquals(acls.getGroupPermissions().size(), 0);

    fs.setPermission(new Path(defaultTbl1Loc), new FsPermission("004")); // ------r--
    fs.setPermission(new Path(defaultTbl2Loc), new FsPermission("777")); // rwxrwxrwx
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "default", "tbl1"));
    assertEquals(acls.getUserPermissions().size(), 0);
    assertEquals(acls.getGroupPermissions().size(), 1);
    assertTrue(acls.getGroupPermissions().keySet().contains("public"));
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "default", "tbl2"));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertTrue(acls.getUserPermissions().keySet().contains("user1"));
    assertEquals(acls.getGroupPermissions().size(), 2);
    assertTrue(acls.getGroupPermissions().keySet().contains("group1"));
    assertTrue(acls.getGroupPermissions().keySet().contains("public"));

    fs.setPermission(new Path(db1Loc), new FsPermission("400")); // ------r--
    fs.delete(new Path(db1Tbl1Loc), true);
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.DATABASE, "db1", null));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertTrue(acls.getUserPermissions().keySet().contains("user1"));
    assertEquals(acls.getGroupPermissions().size(), 0);
    acls = policyProvider.getResourceACLs(
        new HivePrivilegeObject(HivePrivilegeObjectType.TABLE_OR_VIEW, "db1", "tbl1"));
    assertEquals(acls.getUserPermissions().size(), 1);
    assertTrue(acls.getUserPermissions().keySet().contains("user1"));
    assertEquals(acls.getGroupPermissions().size(), 0);

  }
}


/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2012, assimp team
All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------
*/

/** @file  FBXDocument.h
 *  @brief FBX DOM
 */
#ifndef INCLUDED_AI_FBX_DOCUMENT_H
#define INCLUDED_AI_FBX_DOCUMENT_H

#include <vector>
#include <map>
#include <string>

#include "FBXProperties.h"

namespace Assimp {
namespace FBX {

	class Parser;
	class Object;
	struct ImportSettings;

	class PropertyTable;
	class Document;
	class Material;
	class Geometry;

	class AnimationCurve;
	class AnimationCurveNode;
	class AnimationLayer;
	class AnimationStack;

	class Skin;
	class Cluster;

/** Represents a delay-parsed FBX objects. Many objects in the scene
 *  are not needed by assimp, so it makes no sense to parse them
 *  upfront. */
class LazyObject
{
public:

	LazyObject(uint64_t id, const Element& element, const Document& doc);
	~LazyObject();

public:

	const Object* Get(bool dieOnError = false);

	template <typename T>
	const T* Get(bool dieOnError = false) {
		const Object* const ob = Get(dieOnError);
		return ob ? dynamic_cast<const T*>(ob) : NULL;
	}

	uint64_t ID() const {
		return id;
	}

	bool IsBeingConstructed() const {
		return (flags & BEING_CONSTRUCTED) != 0;
	}

	bool FailedToConstruct() const {
		return (flags & FAILED_TO_CONSTRUCT) != 0;
	}

	const Element& GetElement() const {
		return element;
	}

	const Document& GetDocument() const {
		return doc;
	}

private:

	const Document& doc;
	const Element& element;
	boost::scoped_ptr<const Object> object;

	const uint64_t id;

	enum Flags {
		BEING_CONSTRUCTED = 0x1,
		FAILED_TO_CONSTRUCT = 0x2
	};

	unsigned int flags;
};

/** Base class for in-memory (DOM) representations of FBX objects */
class Object
{
public:

	Object(uint64_t id, const Element& element, const std::string& name);
	virtual ~Object();

public:

	const Element& SourceElement() const {
		return element;
	}

	const std::string& Name() const {
		return name;
	}

	uint64_t ID() const {
		return id;
	}

protected:
	const Element& element;
	const std::string name;
	const uint64_t id;
};

/** DOM class for generic FBX NoteAttribute blocks. NoteAttribute's just hold a property table,
 *  fixed members are added by deriving classes. */
class NodeAttribute : public Object
{
public:

	NodeAttribute(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~NodeAttribute();

public:

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

private:

	boost::shared_ptr<const PropertyTable> props;
};

/** DOM base class for FBX camera settings attached to a node */
class CameraSwitcher : public NodeAttribute
{
public:

	CameraSwitcher(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~CameraSwitcher();

public:

	int CameraID() const {
		return cameraId;
	}

	const std::string& CameraName() const {
		return cameraName;
	}

	const std::string& CameraIndexName() const {
		return cameraIndexName;
	}

private:

	int cameraId;
	std::string cameraName;
	std::string cameraIndexName;
};

#define fbx_stringize(a) #a

#define fbx_simple_property(name, type, default_value) \
	type name() const { \
		return PropertyGet<type>(Props(), fbx_stringize(name), (default_value)); \
	}

// XXX improve logging
#define fbx_simple_enum_property(name, type, default_value) \
	type name() const { \
		const int ival = PropertyGet<int>(Props(), fbx_stringize(name), static_cast<int>(default_value)); \
		if (ival < 0 || ival >= AI_CONCAT(type, _MAX)) { \
			ai_assert(static_cast<int>(default_value) >= 0 && static_cast<int>(default_value) < AI_CONCAT(type, _MAX)); \
			return static_cast<type>(default_value); \
		} \
		return static_cast<type>(ival); \
}

/** DOM base class for FBX cameras attached to a node */
class Camera : public NodeAttribute
{
public:

	Camera(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Camera();

public:

	fbx_simple_property(Position, aiVector3D, aiVector3D(0,0,0));
	fbx_simple_property(UpVector, aiVector3D, aiVector3D(0,1,0));
	fbx_simple_property(InterestPosition, aiVector3D, aiVector3D(0,0,0));

	fbx_simple_property(AspectWidth, float, 1.0f);
	fbx_simple_property(AspectHeight, float, 1.0f);
	fbx_simple_property(FilmWidth, float, 1.0f);
	fbx_simple_property(FilmHeight, float, 1.0f);

	fbx_simple_property(FilmAspectRatio, float, 1.0f);
	fbx_simple_property(ApertureMode, int, 0);

	fbx_simple_property(FieldOfView, float, 1.0f);
	fbx_simple_property(FocalLength, float, 1.0f);

private:
};

/** DOM base class for FBX null markers attached to a node */
class Null : public NodeAttribute
{
public:

	Null(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Null();
};

/** DOM base class for FBX limb node markers attached to a node */
class LimbNode : public NodeAttribute
{
public:

	LimbNode(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~LimbNode();
};

/** DOM base class for FBX lights attached to a node */
class Light : public NodeAttribute
{
public:

	Light(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Light();

public:

	enum Type
	{
		Type_Point,
		Type_Directional,
		Type_Spot,
		Type_Area,
		Type_Volume,

		Type_MAX // end-of-enum sentinel
	};

	enum Decay
	{
		Decay_None,
		Decay_Linear,
		Decay_Quadratic,
		Decay_Cubic,

		Decay_MAX // end-of-enum sentinel
	};

public:

	fbx_simple_property(Color, aiVector3D, aiVector3D(1,1,1));
	fbx_simple_enum_property(LightType, Type, 0);
	fbx_simple_property(CastLightOnObject, bool, false);
	fbx_simple_property(DrawVolumetricLight, bool, true);
	fbx_simple_property(DrawGroundProjection, bool, true);
	fbx_simple_property(DrawFrontFacingVolumetricLight, bool, false);
	fbx_simple_property(Intensity, float, 1.0f);
	fbx_simple_property(InnerAngle, float, 0.0f);
	fbx_simple_property(OuterAngle, float, 45.0f);
	fbx_simple_property(Fog, int, 50);
	fbx_simple_enum_property(DecayType, Decay, 0);
	fbx_simple_property(DecayStart, int, 0);
	fbx_simple_property(FileName, std::string, "");

	fbx_simple_property(EnableNearAttenuation, bool, false);
	fbx_simple_property(NearAttenuationStart, float, 0.0f);
	fbx_simple_property(NearAttenuationEnd, float, 0.0f);
	fbx_simple_property(EnableFarAttenuation, bool, false);
	fbx_simple_property(FarAttenuationStart, float, 0.0f);
	fbx_simple_property(FarAttenuationEnd, float, 0.0f);

	fbx_simple_property(CastShadows, bool, true);
	fbx_simple_property(ShadowColor, aiVector3D, aiVector3D(0,0,0));

	fbx_simple_property(AreaLightShape, int, 0);

	fbx_simple_property(LeftBarnDoor, float, 20.0f);
	fbx_simple_property(RightBarnDoor, float, 20.0f);
	fbx_simple_property(TopBarnDoor, float, 20.0f);
	fbx_simple_property(BottomBarnDoor, float, 20.0f);
	fbx_simple_property(EnableBarnDoor, bool, true);

private:
};

/** DOM base class for FBX models (even though its semantics are more "node" than "model" */
class Model : public Object
{
public:

	Model(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Model();

public:

	enum RotOrder
	{
		RotOrder_EulerXYZ = 0,
		RotOrder_EulerXZY,
		RotOrder_EulerYZX,
		RotOrder_EulerYXZ,
		RotOrder_EulerZXY,
		RotOrder_EulerZYX,

		RotOrder_SphericXYZ,

		RotOrder_MAX // end-of-enum sentinel
	};

	enum TransformInheritance
	{
		TransformInheritance_RrSs = 0,
		TransformInheritance_RSrs,
		TransformInheritance_Rrs,

		TransformInheritance_MAX // end-of-enum sentinel
	};

public:

	fbx_simple_property(QuaternionInterpolate, int, 0);

	fbx_simple_property(RotationOffset, aiVector3D, aiVector3D());
	fbx_simple_property(RotationPivot, aiVector3D, aiVector3D());
	fbx_simple_property(ScalingOffset, aiVector3D, aiVector3D());
	fbx_simple_property(ScalingPivot, aiVector3D, aiVector3D());
	fbx_simple_property(TranslationActive, bool, false);

	fbx_simple_property(TranslationMin, aiVector3D, aiVector3D());
	fbx_simple_property(TranslationMax, aiVector3D, aiVector3D());

	fbx_simple_property(TranslationMinX, bool, false);
	fbx_simple_property(TranslationMaxX, bool, false);
	fbx_simple_property(TranslationMinY, bool, false);
	fbx_simple_property(TranslationMaxY, bool, false);
	fbx_simple_property(TranslationMinZ, bool, false);
	fbx_simple_property(TranslationMaxZ, bool, false);

	fbx_simple_enum_property(RotationOrder, RotOrder, 0);
	fbx_simple_property(RotationSpaceForLimitOnly, bool, false);
	fbx_simple_property(RotationStiffnessX, float, 0.0f);
	fbx_simple_property(RotationStiffnessY, float, 0.0f);
	fbx_simple_property(RotationStiffnessZ, float, 0.0f);
	fbx_simple_property(AxisLen, float, 0.0f);

	fbx_simple_property(PreRotation, aiVector3D, aiVector3D());
	fbx_simple_property(PostRotation, aiVector3D, aiVector3D());
	fbx_simple_property(RotationActive, bool, false);

	fbx_simple_property(RotationMin, aiVector3D, aiVector3D());
	fbx_simple_property(RotationMax, aiVector3D, aiVector3D());

	fbx_simple_property(RotationMinX, bool, false);
	fbx_simple_property(RotationMaxX, bool, false);
	fbx_simple_property(RotationMinY, bool, false);
	fbx_simple_property(RotationMaxY, bool, false);
	fbx_simple_property(RotationMinZ, bool, false);
	fbx_simple_property(RotationMaxZ, bool, false);
	fbx_simple_enum_property(InheritType, TransformInheritance, 0);

	fbx_simple_property(ScalingActive, bool, false);
	fbx_simple_property(ScalingMin, aiVector3D, aiVector3D());
	fbx_simple_property(ScalingMax, aiVector3D, aiVector3D(1.f,1.f,1.f));
	fbx_simple_property(ScalingMinX, bool, false);
	fbx_simple_property(ScalingMaxX, bool, false);
	fbx_simple_property(ScalingMinY, bool, false);
	fbx_simple_property(ScalingMaxY, bool, false);
	fbx_simple_property(ScalingMinZ, bool, false);
	fbx_simple_property(ScalingMaxZ, bool, false);

	fbx_simple_property(GeometricTranslation, aiVector3D, aiVector3D());
	fbx_simple_property(GeometricRotation, aiVector3D, aiVector3D());
	fbx_simple_property(GeometricScaling, aiVector3D, aiVector3D(1.f, 1.f, 1.f));

	fbx_simple_property(MinDampRangeX, float, 0.0f);
	fbx_simple_property(MinDampRangeY, float, 0.0f);
	fbx_simple_property(MinDampRangeZ, float, 0.0f);
	fbx_simple_property(MaxDampRangeX, float, 0.0f);
	fbx_simple_property(MaxDampRangeY, float, 0.0f);
	fbx_simple_property(MaxDampRangeZ, float, 0.0f);

	fbx_simple_property(MinDampStrengthX, float, 0.0f);
	fbx_simple_property(MinDampStrengthY, float, 0.0f);
	fbx_simple_property(MinDampStrengthZ, float, 0.0f);
	fbx_simple_property(MaxDampStrengthX, float, 0.0f);
	fbx_simple_property(MaxDampStrengthY, float, 0.0f);
	fbx_simple_property(MaxDampStrengthZ, float, 0.0f);

	fbx_simple_property(PreferredAngleX, float, 0.0f);
	fbx_simple_property(PreferredAngleY, float, 0.0f);
	fbx_simple_property(PreferredAngleZ, float, 0.0f);

	fbx_simple_property(Show, bool, true);
	fbx_simple_property(LODBox, bool, false);
	fbx_simple_property(Freeze, bool, false);

public:

	const std::string& Shading() const {
		return shading;
	}

	const std::string& Culling() const {
		return culling;
	}

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	/** Get material links */
	const std::vector<const Material*>& GetMaterials() const {
		return materials;
	}

	/** Get geometry links */
	const std::vector<const Geometry*>& GetGeometry() const {
		return geometry;
	}

	/** Get node attachments */
	const std::vector<const NodeAttribute*>& GetAttributes() const {
		return attributes;
	}

public:

	/** convenience method to check if the node has a Null node marker */
	bool IsNull() const;

private:

	void ResolveLinks(const Element& element, const Document& doc);

private:

	std::vector<const Material*> materials;
	std::vector<const Geometry*> geometry;
	std::vector<const NodeAttribute*> attributes;

	std::string shading;
	std::string culling;
	boost::shared_ptr<const PropertyTable> props;
};

/** DOM class for generic FBX textures */
class Texture : public Object
{
public:

	Texture(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Texture();

public:

	const std::string& Type() const {
		return type;
	}

	const std::string& FileName() const {
		return fileName;
	}

	const std::string& RelativeFilename() const {
		return relativeFileName;
	}

	const std::string& AlphaSource() const {
		return alphaSource;
	}

	const aiVector2D& UVTranslation() const {
		return uvTrans;
	}

	const aiVector2D& UVScaling() const {
		return uvScaling;
	}

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	// return a 4-tuple
	const unsigned int* Crop() const {
		return crop;
	}

private:

	aiVector2D uvTrans;
	aiVector2D uvScaling;

	std::string type;
	std::string relativeFileName;
	std::string fileName;
	std::string alphaSource;
	boost::shared_ptr<const PropertyTable> props;

	unsigned int crop[4];
};

/** DOM class for layered FBX textures */
class LayeredTexture : public Object
{
public:

	LayeredTexture(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~LayeredTexture();

	//Can only be called after construction of the layered texture object due to construction flag.
	void fillTexture(const Document& doc);

	enum BlendMode
	{
		BlendMode_Translucent,
		BlendMode_Additive,
		BlendMode_Modulate,
		BlendMode_Modulate2,
		BlendMode_Over,
		BlendMode_Normal,
		BlendMode_Dissolve,
		BlendMode_Darken,
		BlendMode_ColorBurn,
		BlendMode_LinearBurn,
		BlendMode_DarkerColor,
		BlendMode_Lighten,
		BlendMode_Screen,
		BlendMode_ColorDodge,
		BlendMode_LinearDodge,
		BlendMode_LighterColor,
		BlendMode_SoftLight,
		BlendMode_HardLight,
		BlendMode_VividLight,
		BlendMode_LinearLight,
		BlendMode_PinLight,
		BlendMode_HardMix,
		BlendMode_Difference,
		BlendMode_Exclusion,
		BlendMode_Subtract,
		BlendMode_Divide,
		BlendMode_Hue,
		BlendMode_Saturation,
		BlendMode_Color,
		BlendMode_Luminosity,
		BlendMode_Overlay,
		BlendMode_BlendModeCount
	};

	const Texture* getTexture() const
	{
		return texture;
	}
	BlendMode GetBlendMode()
	{
		return blendMode;
	}
	float Alpha()
	{
		return alpha;
	}
private:
	const Texture* texture;
	BlendMode blendMode;
	float alpha;
};

typedef std::fbx_unordered_map<std::string, const Texture*> TextureMap;
typedef std::fbx_unordered_map<std::string, const LayeredTexture*> LayeredTextureMap;

/** DOM class for generic FBX materials */
class Material : public Object
{
public:

	Material(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Material();

public:

	const std::string& GetShadingModel() const {
		return shading;
	}

	bool IsMultilayer() const {
		return multilayer;
	}

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	const TextureMap& Textures() const {
		return textures;
	}

	const LayeredTextureMap& LayeredTextures() const {
		return layeredTextures;
	}

private:

	std::string shading;
	bool multilayer;
	boost::shared_ptr<const PropertyTable> props;

	TextureMap textures;
	LayeredTextureMap layeredTextures;
};

/** DOM base class for all kinds of FBX geometry */
class Geometry : public Object
{
public:

	Geometry(uint64_t id, const Element& element, const std::string& name, const Document& doc);
	~Geometry();

public:

	/** Get the Skin attached to this geometry or NULL */
	const Skin* const DeformerSkin() const {
		return skin;
	}

private:

	const Skin* skin;
};

typedef std::vector<int> MatIndexArray;

/** DOM class for FBX geometry of type "Mesh"*/
class MeshGeometry : public Geometry
{

public:

	MeshGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc);
	~MeshGeometry();

public:

	/** Get a list of all vertex points, non-unique*/
	const std::vector<aiVector3D>& GetVertices() const {
		return vertices;
	}

	/** Get a list of all vertex normals or an empty array if
	 *  no normals are specified. */
	const std::vector<aiVector3D>& GetNormals() const {
		return normals;
	}

	/** Get a list of all vertex tangents or an empty array
	 *  if no tangents are specified */
	const std::vector<aiVector3D>& GetTangents() const {
		return tangents;
	}

	/** Get a list of all vertex binormals or an empty array
	 *  if no binormals are specified */
	const std::vector<aiVector3D>& GetBinormals() const {
		return binormals;
	}

	/** Return list of faces - each entry denotes a face and specifies
	 *  how many vertices it has. Vertices are taken from the
	 *  vertex data arrays in sequential order. */
	const std::vector<unsigned int>& GetFaceIndexCounts() const {
		return faces;
	}

	/** Get a UV coordinate slot, returns an empty array if
	 *  the requested slot does not exist. */
	const std::vector<aiVector2D>& GetTextureCoords(unsigned int index) const {
		static const std::vector<aiVector2D> empty;
		return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? empty : uvs[index];
	}

	/** Get a UV coordinate slot, returns an empty array if
	 *  the requested slot does not exist. */
	std::string GetTextureCoordChannelName(unsigned int index) const {
		return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? "" : uvNames[index];
	}

	/** Get a vertex color coordinate slot, returns an empty array if
	 *  the requested slot does not exist. */
	const std::vector<aiColor4D>& GetVertexColors(unsigned int index) const {
		static const std::vector<aiColor4D> empty;
		return index >= AI_MAX_NUMBER_OF_COLOR_SETS ? empty : colors[index];
	}

	/** Get per-face-vertex material assignments */
	const MatIndexArray& GetMaterialIndices() const {
		return materials;
	}

	/** Convert from a fbx file vertex index (for example from a #Cluster weight) or NULL
	  * if the vertex index is not valid. */
	const unsigned int* ToOutputVertexIndex(unsigned int in_index, unsigned int& count) const {
		if(in_index >= mapping_counts.size()) {
			return NULL;
		}

		ai_assert(mapping_counts.size() == mapping_offsets.size());
		count = mapping_counts[in_index];

		ai_assert(count != 0);
		ai_assert(mapping_offsets[in_index] + count <= mappings.size());

		return &mappings[mapping_offsets[in_index]];
	}

	/** Determine the face to which a particular output vertex index belongs.
	 *  This mapping is always unique. */
	unsigned int FaceForVertexIndex(unsigned int in_index) const {
		ai_assert(in_index < vertices.size());

		// in the current conversion pattern this will only be needed if
		// weights are present, so no need to always pre-compute this table
		if (facesVertexStartIndices.empty()) {
			facesVertexStartIndices.resize(faces.size() + 1, 0);

			std::partial_sum(faces.begin(), faces.end(), facesVertexStartIndices.begin() + 1);
			facesVertexStartIndices.pop_back();
		}

		ai_assert(facesVertexStartIndices.size() == faces.size());
		const std::vector<unsigned int>::iterator it = std::upper_bound(
			facesVertexStartIndices.begin(),
			facesVertexStartIndices.end(),
			in_index
		);

		return static_cast<unsigned int>(std::distance(facesVertexStartIndices.begin(), it - 1));
	}

public:

private:

	void ReadLayer(const Scope& layer);
	void ReadLayerElement(const Scope& layerElement);
	void ReadVertexData(const std::string& type, int index, const Scope& source);

	void ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
		const std::string& MappingInformationType,
		const std::string& ReferenceInformationType);

	void ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
		const std::string& MappingInformationType,
		const std::string& ReferenceInformationType);

	void ReadVertexDataColors(std::vector<aiColor4D>& colors_out, const Scope& source,
		const std::string& MappingInformationType,
		const std::string& ReferenceInformationType);

	void ReadVertexDataTangents(std::vector<aiVector3D>& tangents_out, const Scope& source,
		const std::string& MappingInformationType,
		const std::string& ReferenceInformationType);

	void ReadVertexDataBinormals(std::vector<aiVector3D>& binormals_out, const Scope& source,
		const std::string& MappingInformationType,
		const std::string& ReferenceInformationType);

	void ReadVertexDataMaterials(MatIndexArray& materials_out, const Scope& source,
		const std::string& MappingInformationType,
		const std::string& ReferenceInformationType);

private:

	// cached data arrays
	MatIndexArray materials;
	std::vector<aiVector3D> vertices;
	std::vector<unsigned int> faces;
	mutable std::vector<unsigned int> facesVertexStartIndices;
	std::vector<aiVector3D> tangents;
	std::vector<aiVector3D> binormals;
	std::vector<aiVector3D> normals;

	std::string uvNames[AI_MAX_NUMBER_OF_TEXTURECOORDS];
	std::vector<aiVector2D> uvs[AI_MAX_NUMBER_OF_TEXTURECOORDS];
	std::vector<aiColor4D> colors[AI_MAX_NUMBER_OF_COLOR_SETS];

	std::vector<unsigned int> mapping_counts;
	std::vector<unsigned int> mapping_offsets;
	std::vector<unsigned int> mappings;
};

typedef std::vector<uint64_t> KeyTimeList;
typedef std::vector<float> KeyValueList;

/** Represents a FBX animation curve (i.e. a 1-dimensional set of keyframes and values therefor) */
class AnimationCurve : public Object
{
public:

	AnimationCurve(uint64_t id, const Element& element, const std::string& name, const Document& doc);
	~AnimationCurve();

public:

	/** get list of keyframe positions (time).
	 *  Invariant: |GetKeys()| > 0 */
	const KeyTimeList& GetKeys() const {
		return keys;
	}

	/** get list of keyframe values.
	  * Invariant: |GetKeys()| == |GetValues()| && |GetKeys()| > 0*/
	const KeyValueList& GetValues() const {
		return values;
	}

	const std::vector<float>& GetAttributes() const {
		return attributes;
	}

	const std::vector<unsigned int>& GetFlags() const {
		return flags;
	}

private:

	KeyTimeList keys;
	KeyValueList values;
	std::vector<float> attributes;
	std::vector<unsigned int> flags;
};

// property-name -> animation curve
typedef std::map<std::string, const AnimationCurve*> AnimationCurveMap;

/** Represents a FBX animation curve (i.e. a mapping from single animation curves to nodes) */
class AnimationCurveNode : public Object
{
public:

	/* the optional whitelist specifies a list of property names for which the caller
	wants animations for. If the curve node does not match one of these, std::range_error
	will be thrown. */
	AnimationCurveNode(uint64_t id, const Element& element, const std::string& name, const Document& doc,
		const char* const * target_prop_whitelist = NULL, size_t whitelist_size = 0);

	~AnimationCurveNode();

public:

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	const AnimationCurveMap& Curves() const;

	/** Object the curve is assigned to, this can be NULL if the
	 *  target object has no DOM representation or could not
	 *  be read for other reasons.*/
	const Object* Target() const {
		return target;
	}

	const Model* TargetAsModel() const {
		return dynamic_cast<const Model*>(target);
	}

	const NodeAttribute* TargetAsNodeAttribute() const {
		return dynamic_cast<const NodeAttribute*>(target);
	}

	/** Property of Target() that is being animated*/
	const std::string& TargetProperty() const {
		return prop;
	}

private:

	const Object* target;
	boost::shared_ptr<const PropertyTable> props;
	mutable AnimationCurveMap curves;

	std::string prop;
	const Document& doc;
};

typedef std::vector<const AnimationCurveNode*> AnimationCurveNodeList;

/** Represents a FBX animation layer (i.e. a list of node animations) */
class AnimationLayer : public Object
{
public:

	AnimationLayer(uint64_t id, const Element& element, const std::string& name, const Document& doc);
	~AnimationLayer();

public:

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	/* the optional whitelist specifies a list of property names for which the caller
	wants animations for. Curves not matching this list will not be added to the
	animation layer. */
	AnimationCurveNodeList Nodes(const char* const * target_prop_whitelist = NULL, size_t whitelist_size = 0) const;

private:

	boost::shared_ptr<const PropertyTable> props;
	const Document& doc;
};

typedef std::vector<const AnimationLayer*> AnimationLayerList;

/** Represents a FBX animation stack (i.e. a list of animation layers) */
class AnimationStack : public Object
{
public:

	AnimationStack(uint64_t id, const Element& element, const std::string& name, const Document& doc);
	~AnimationStack();

public:

	fbx_simple_property(LocalStart, uint64_t, 0L);
	fbx_simple_property(LocalStop, uint64_t, 0L);
	fbx_simple_property(ReferenceStart, uint64_t, 0L);
	fbx_simple_property(ReferenceStop, uint64_t, 0L);

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	const AnimationLayerList& Layers() const {
		return layers;
	}

private:

	boost::shared_ptr<const PropertyTable> props;
	AnimationLayerList layers;
};

/** DOM class for deformers */
class Deformer : public Object
{
public:

	Deformer(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Deformer();

public:

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

private:

	boost::shared_ptr<const PropertyTable> props;
};

typedef std::vector<float> WeightArray;
typedef std::vector<unsigned int> WeightIndexArray;

/** DOM class for skin deformer clusters (aka subdeformers) */
class Cluster : public Deformer
{
public:

	Cluster(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Cluster();

public:

	/** get the list of deformer weights associated with this cluster.
	 *  Use #GetIndices() to get the associated vertices. Both arrays
	 *  have the same size (and may also be empty). */
	const WeightArray& GetWeights() const {
		return weights;
	}

	/** get indices into the vertex data of the geometry associated
	 *  with this cluster. Use #GetWeights() to get the associated weights.
	 *  Both arrays have the same size (and may also be empty). */
	const WeightIndexArray& GetIndices() const {
		return indices;
	}

	/** */
	const aiMatrix4x4& Transform() const {
		return transform;
	}

	const aiMatrix4x4& TransformLink() const {
		return transformLink;
	}

	const Model* const TargetNode() const {
		return node;
	}

private:

	WeightArray weights;
	WeightIndexArray indices;

	aiMatrix4x4 transform;
	aiMatrix4x4 transformLink;

	const Model* node;
};

/** DOM class for skin deformers */
class Skin : public Deformer
{
public:

	Skin(uint64_t id, const Element& element, const Document& doc, const std::string& name);
	~Skin();

public:

	float DeformAccuracy() const {
		return accuracy;
	}

	const std::vector<const Cluster*>& Clusters() const {
		return clusters;
	}

private:

	float accuracy;
	std::vector<const Cluster*> clusters;
};

/** Represents a link between two FBX objects. */
class Connection
{
public:

	Connection(uint64_t insertionOrder,  uint64_t src, uint64_t dest, const std::string& prop, const Document& doc);
	~Connection();

	// note: a connection ensures that the source and dest objects exist, but
	// not that they have DOM representations, so the return value of one of
	// these functions can still be NULL.
	const Object* SourceObject() const;
	const Object* DestinationObject() const;

	// these, however, are always guaranteed to be valid
	LazyObject& LazySourceObject() const;
	LazyObject& LazyDestinationObject() const;

	/** return the name of the property the connection is attached to.
	  * this is an empty string for object to object (OO) connections. */
	const std::string& PropertyName() const {
		return prop;
	}

	uint64_t InsertionOrder() const {
		return insertionOrder;
	}

	int CompareTo(const Connection* c) const {
		// note: can't subtract because this would overflow uint64_t
		if(InsertionOrder() > c->InsertionOrder()) {
			return 1;
		}
		else if(InsertionOrder() < c->InsertionOrder()) {
			return -1;
		}
		return 0;
	}

	bool Compare(const Connection* c) const {
		return InsertionOrder() < c->InsertionOrder();
	}

public:

	uint64_t insertionOrder;
	const std::string prop;

	uint64_t src, dest;
	const Document& doc;
};

	// XXX again, unique_ptr would be useful. shared_ptr is too
	// bloated since the objects have a well-defined single owner
	// during their entire lifetime (Document). FBX files have
	// up to many thousands of objects (most of which we never use),
	// so the memory overhead for them should be kept at a minimum.
	typedef std::map<uint64_t, LazyObject*> ObjectMap;
	typedef std::fbx_unordered_map<std::string, boost::shared_ptr<const PropertyTable> > PropertyTemplateMap;

	typedef std::multimap<uint64_t, const Connection*> ConnectionMap;

/** DOM class for global document settings, a single instance per document can
 *  be accessed via Document.Globals(). */
class FileGlobalSettings
{
public:

	FileGlobalSettings(const Document& doc, boost::shared_ptr<const PropertyTable> props);
	~FileGlobalSettings();

public:

	const PropertyTable& Props() const {
		ai_assert(props.get());
		return *props.get();
	}

	const Document& GetDocument() const {
		return doc;
	}

	fbx_simple_property(UpAxis, int, 1);
	fbx_simple_property(UpAxisSign, int, 1);
	fbx_simple_property(FrontAxis, int, 2);
	fbx_simple_property(FrontAxisSign, int, 1);
	fbx_simple_property(CoordAxis, int, 0);
	fbx_simple_property(CoordAxisSign, int, 1);
	fbx_simple_property(OriginalUpAxis, int, 0);
	fbx_simple_property(OriginalUpAxisSign, int, 1);
	fbx_simple_property(UnitScaleFactor, double, 1);
	fbx_simple_property(OriginalUnitScaleFactor, double, 1);
	fbx_simple_property(AmbientColor, aiVector3D, aiVector3D(0,0,0));
	fbx_simple_property(DefaultCamera, std::string, "");

	enum FrameRate {
		FrameRate_DEFAULT = 0,
		FrameRate_120 = 1,
		FrameRate_100 = 2,
		FrameRate_60 = 3,
		FrameRate_50 = 4,
		FrameRate_48 = 5,
		FrameRate_30 = 6,
		FrameRate_30_DROP = 7,
		FrameRate_NTSC_DROP_FRAME = 8,
		FrameRate_NTSC_FULL_FRAME = 9,
		FrameRate_PAL = 10,
		FrameRate_CINEMA = 11,
		FrameRate_1000 = 12,
		FrameRate_CINEMA_ND = 13,
		FrameRate_CUSTOM = 14,

		FrameRate_MAX// end-of-enum sentinel
	};

	fbx_simple_enum_property(TimeMode, FrameRate, FrameRate_DEFAULT);
	fbx_simple_property(TimeSpanStart, uint64_t, 0L);
	fbx_simple_property(TimeSpanStop, uint64_t, 0L);
	fbx_simple_property(CustomFrameRate, float, -1.0f);

private:

	boost::shared_ptr<const PropertyTable> props;
	const Document& doc;
};

/** DOM root for a FBX file */
class Document
{
public:

	Document(const Parser& parser, const ImportSettings& settings);
	~Document();

public:

	LazyObject* GetObject(uint64_t id) const;

	bool IsBinary() const {
		return parser.IsBinary();
	}

	unsigned int FBXVersion() const {
		return fbxVersion;
	}

	const std::string& Creator() const {
		return creator;
	}

	// elements (in this order): Uear, Month, Day, Hour, Second, Millisecond
	const unsigned int* CreationTimeStamp() const {
		return creationTimeStamp;
	}

	const FileGlobalSettings& GlobalSettings() const {
		ai_assert(globals.get());
		return *globals.get();
	}

	const PropertyTemplateMap& Templates() const {
		return templates;
	}

	const ObjectMap& Objects() const {
		return objects;
	}

	const ImportSettings& Settings() const {
		return settings;
	}

	const ConnectionMap& ConnectionsBySource() const {
		return src_connections;
	}

	const ConnectionMap& ConnectionsByDestination() const {
		return dest_connections;
	}

	// note: the implicit rule in all DOM classes is to always resolve
	// from destination to source (since the FBX object hierarchy is,
	// with very few exceptions, a DAG, this avoids cycles). In all
	// cases that may involve back-facing edges in the object graph,
	// use LazyObject::IsBeingConstructed() to check.

	std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source) const;
	std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest) const;

	std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source, const char* classname) const;
	std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest, const char* classname) const;

	std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source,
		const char* const* classnames, size_t count) const;
	std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest,
		const char* const* classnames,
		size_t count) const;

	const std::vector<const AnimationStack*>& AnimationStacks() const;

private:

	std::vector<const Connection*> GetConnectionsSequenced(uint64_t id, const ConnectionMap&) const;
	std::vector<const Connection*> GetConnectionsSequenced(uint64_t id, bool is_src,
		const ConnectionMap&,
		const char* const* classnames,
		size_t count) const;

private:

	void ReadHeader();
	void ReadObjects();
	void ReadPropertyTemplates();
	void ReadConnections();
	void ReadGlobalSettings();

private:

	const ImportSettings& settings;

	ObjectMap objects;
	const Parser& parser;

	PropertyTemplateMap templates;
	ConnectionMap src_connections;
	ConnectionMap dest_connections;

	unsigned int fbxVersion;
	std::string creator;
	unsigned int creationTimeStamp[7];

	std::vector<uint64_t> animationStacks;
	mutable std::vector<const AnimationStack*> animationStacksResolved;

	boost::scoped_ptr<FileGlobalSettings> globals;
};

}
}

#endif


<!DOCTYPE html>

  <title>sandbox</title>

查看控制台的打印结果


ADDGRID = True
ALGO = All
EDIFF = 1e-05
EDIFFG = -0.05
ENCUT = 680
IBRION = 2
ICHARG = 1
ISIF = 3
ISMEAR = 2
ISPIN = 2
ISTART = 0
KSPACING = 0.44
LAECHG = True
LASPH = True
LCHARG = True
LELF = True
LMIXTAU = True
LORBIT = 11
LREAL = False
LVTOT = True
LWAVE = True
MAGMOM = 2*0.0
METAGGA = None
NELM = 200
NSW = 0
PREC = Accurate
SIGMA = 0.2


/* Parser to read in a edwFastqFile from a ra file where tags in ra file correspond to fields in a
 * struct. This program was generated by raToStructGen. */

/* Copyright (C) 2013 The Regents of the University of California
 * See README in this or parent directory for licensing information. */

#include "common.h"
#include "linefile.h"
#include "hash.h"
#include "obscure.h"
#include "sqlNum.h"
#include "sqlList.h"
#include "ra.h"
#include "raToStruct.h"
#include "encodeDataWarehouse.h"

struct raToStructReader *edwFastqFileRaReader()
/* Make a raToStructReader for edwFastqFile */
{
static char *fields[] = {
    "sampleCount",
    "basesInSample",
    "readCount",
    "baseCount",
    "readSizeMean",
    "readSizeStd",
    "readSizeMin",
    "readSizeMax",
    "qualMean",
    "qualStd",
    "qualMin",
    "qualMax",
    "qualType",
    "qualZero",
    "atRatio",
    "aRatio",
    "cRatio",
    "gRatio",
    "tRatio",
    "nRatio",
    "qualPos",
    "aAtPos",
    "cAtPos",
    "gAtPos",
    "tAtPos",
    "nAtPos",
    };
static char *requiredFields[] = {
    "readCount",
    "baseCount",
    "readSizeMean",
    "readSizeStd",
    "readSizeMin",
    "readSizeMax",
    "qualMean",
    "qualStd",
    "qualMin",
    "qualMax",
    "qualType",
    "qualZero",
    "atRatio",
    "aRatio",
    "cRatio",
    "gRatio",
    "tRatio",
    "nRatio",
    "qualPos",
    "aAtPos",
    "cAtPos",
    "gAtPos",
    "tAtPos",
    "nAtPos",
    };
return raToStructReaderNew("edwFastqFile", ArraySize(fields), fields, ArraySize(requiredFields), requiredFields);
}

struct edwFastqFile *edwFastqFileFromNextRa(struct lineFile *lf, struct raToStructReader *reader)
/* Return next stanza put into an edwFastqFile. */
{
enum fields
    {
    sampleCountField,
    basesInSampleField,
    readCountField,
    baseCountField,
    readSizeMeanField,
    readSizeStdField,
    readSizeMinField,
    readSizeMaxField,
    qualMeanField,
    qualStdField,
    qualMinField,
    qualMaxField,
    qualTypeField,
    qualZeroField,
    atRatioField,
    aRatioField,
    cRatioField,
    gRatioField,
    tRatioField,
    nRatioField,
    qualPosField,
    aAtPosField,
    cAtPosField,
    gAtPosField,
    tAtPosField,
    nAtPosField,
    };
if (!raSkipLeadingEmptyLines(lf, NULL))
    return NULL;

struct edwFastqFile *el;
AllocVar(el);

bool *fieldsObserved = reader->fieldsObserved;
bzero(fieldsObserved, reader->fieldCount);

char *tag, *val;
while (raNextTagVal(lf, &tag, &val, NULL))
    {
    struct hashEl *hel = hashLookup(reader->fieldIds, tag);
    if (hel != NULL)
        {
	int id = ptToInt(hel->val);
	if (fieldsObserved[id])
	     errAbort("Duplicate tag %s line %d of %s\n", tag, lf->lineIx, lf->fileName);
	fieldsObserved[id] = TRUE;
	switch (id)
	    {
	    case sampleCountField:
	        {
	        el->sampleCount = sqlLongLong(val);
		break;
	        }
	    case basesInSampleField:
	        {
	        el->basesInSample = sqlLongLong(val);
		break;
	        }
	    case readCountField:
	        {
	        el->readCount = sqlLongLong(val);
		break;
	        }
	    case baseCountField:
	        {
	        el->baseCount = sqlLongLong(val);
		break;
	        }
	    case readSizeMeanField:
	        {
	        el->readSizeMean = sqlDouble(val);
		break;
	        }
	    case readSizeStdField:
	        {
	        el->readSizeStd = sqlDouble(val);
		break;
	        }
	    case readSizeMinField:
	        {
	        el->readSizeMin = sqlSigned(val);
		break;
	        }
	    case readSizeMaxField:
	        {
                int arraySize = sqlSigned(val);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "readSizeMax");
		break;
	        }
	    case qualMeanField:
	        {
	        el->qualMean = sqlDouble(val);
		break;
	        }
	    case qualStdField:
	        {
	        el->qualStd = sqlDouble(val);
		break;
	        }
	    case qualMinField:
	        {
	        el->qualMin = sqlDouble(val);
		break;
	        }
	    case qualMaxField:
	        {
	        el->qualMax = sqlDouble(val);
		break;
	        }
	    case qualTypeField:
	        {
	        el->qualType = cloneString(val);
		break;
	        }
	    case qualZeroField:
	        {
	        el->qualZero = sqlSigned(val);
		break;
	        }
	    case atRatioField:
	        {
	        el->atRatio = sqlDouble(val);
		break;
	        }
	    case aRatioField:
	        {
	        el->aRatio = sqlDouble(val);
		break;
	        }
	    case cRatioField:
	        {
	        el->cRatio = sqlDouble(val);
		break;
	        }
	    case gRatioField:
	        {
	        el->gRatio = sqlDouble(val);
		break;
	        }
	    case tRatioField:
	        {
	        el->tRatio = sqlDouble(val);
		break;
	        }
	    case nRatioField:
	        {
	        el->nRatio = sqlDouble(val);
		break;
	        }
	    case qualPosField:
	        {
                int arraySize;
		sqlDoubleDynamicArray(val, &el->qualPos, &arraySize);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "qualPos");
		break;
	        }
	    case aAtPosField:
	        {
                int arraySize;
		sqlDoubleDynamicArray(val, &el->aAtPos, &arraySize);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "aAtPos");
		break;
	        }
	    case cAtPosField:
	        {
                int arraySize;
		sqlDoubleDynamicArray(val, &el->cAtPos, &arraySize);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "cAtPos");
		break;
	        }
	    case gAtPosField:
	        {
                int arraySize;
		sqlDoubleDynamicArray(val, &el->gAtPos, &arraySize);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "gAtPos");
		break;
	        }
	    case tAtPosField:
	        {
                int arraySize;
		sqlDoubleDynamicArray(val, &el->tAtPos, &arraySize);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "tAtPos");
		break;
	        }
	    case nAtPosField:
	        {
                int arraySize;
		sqlDoubleDynamicArray(val, &el->nAtPos, &arraySize);
                raToStructArraySignedSizer(lf, arraySize, &el->readSizeMax, "nAtPos");
		break;
	        }
	    default:
	        internalErr();
		break;
	    }
	}
    }

raToStructReaderCheckRequiredFields(reader, lf);
return el;
}

struct edwFastqFile *edwFastqFileLoadRa(char *fileName)
/* Return list of all edwFastqFile in ra file. */
{
struct raToStructReader *reader = edwFastqFileRaReader();
struct lineFile *lf = lineFileOpen(fileName, TRUE);
struct edwFastqFile *el, *list = NULL;
while ((el = edwFastqFileFromNextRa(lf, reader)) != NULL)
    slAddHead(&list, el);
slReverse(&list);
lineFileClose(&lf);
raToStructReaderFree(&reader);
return list;
}

struct edwFastqFile *edwFastqFileOneFromRa(char *fileName)
/* Return edwFastqFile in file and insist there be exactly one record. */
{
struct edwFastqFile *one = edwFastqFileLoadRa(fileName);
if (one == NULL)
    errAbort("No data in %s", fileName);
if (one->next != NULL)
    errAbort("Multiple records in %s", fileName);
return one;
}


/**
 * Author......: See docs/credits.txt
 * License.....: MIT
 */

#include "common.h"
#include "types.h"
#include "modules.h"
#include "bitops.h"
#include "convert.h"
#include "shared.h"
#include "memory.h"
#include "cpu_crc32.h"
#include "keyboard_layout.h"

static const u32   ATTACK_EXEC    = ATTACK_EXEC_OUTSIDE_KERNEL;
static const u32   DGST_POS0      = 0;
static const u32   DGST_POS1      = 1;
static const u32   DGST_POS2      = 2;
static const u32   DGST_POS3      = 3;
static const u32   DGST_SIZE      = DGST_SIZE_8_8;
static const u32   HASH_CATEGORY  = HASH_CATEGORY_FDE;
static const char *HASH_NAME      = "TrueCrypt SHA512 + XTS 1536 bit";
static const u64   KERN_TYPE      = 6223;
static const u32   OPTI_TYPE      = OPTI_TYPE_ZERO_BYTE
                                  | OPTI_TYPE_SLOW_HASH_SIMD_LOOP
                                  | OPTI_TYPE_USES_BITS_64;
static const u64   OPTS_TYPE      = OPTS_TYPE_PT_GENERATE_LE
                                  | OPTS_TYPE_BINARY_HASHFILE;
static const u32   SALT_TYPE      = SALT_TYPE_EMBEDDED;
static const char *ST_PASS        = "hashcat";
static const char *ST_HASH        = "721a7f40d2b88de8e11f1a203b04ffa97a1f5671623c6783f984cc7c55e04665f95a7f3fd52f402898aaaed68d048cc4c4fabf81c26832b589687dad082f3e4e0f23c7caba28118f21a4cbb8f32b25914ff4022e7c4c8cdd45411801c7c6bde4033badbdcb82f96c77b42025d13fa71415b3278138100ea58ee4476c81ce66f78e89c59ac22cf454684ea7e8c3900374662f23c9491891b60ed7ce8231a7ac5710ee87b51a3f7bd9566a60dc6e7e701c41f3810d7977314b321e8194349909f2ca458a976851d854eaeb934c8df2b5e063d416d3d7c464e28173a0bbba88ec75cf8fe68f21067739b2473bd804fd710de1e4d3ae9451b374edcfd8e3cd613b23aeae272e0923007482dac26a7532ab09af8aad57cd7f1c451bc260cc912d5830cb0d5332f792519e009ed5450171434e5f0f2ba9e003676933a86d83c766419fac98a7ee232eeb593d1686528fab576d5f393d82f9602bcd65975153df205b6d1bc50dacad2ea5bb184696f978efd2b1c1656bf87e03a28a536c48320c430d407ff6c2fc6e7d4ae7b115e79fd0a88df08eca4743178c7c216f35035596a90b0f0fe9c173c7d0e3d76c33a8fce1f5b9b37674bd12e93fb714c9cbba6768c101b5db8f8fd137144453f00dccc7b66911a0a8d87b198807f30be6619400331c5746d481df7ad47a1f867c07f7b8cd296a0c5e03a121c1a7a60b4f768bea49799d2f";

u32         module_attack_exec    (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return ATTACK_EXEC;     }
u32         module_dgst_pos0      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS0;       }
u32         module_dgst_pos1      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS1;       }
u32         module_dgst_pos2      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS2;       }
u32         module_dgst_pos3      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_POS3;       }
u32         module_dgst_size      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return DGST_SIZE;       }
u32         module_hash_category  (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return HASH_CATEGORY;   }
const char *module_hash_name      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return HASH_NAME;       }
u64         module_kern_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return KERN_TYPE;       }
u32         module_opti_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return OPTI_TYPE;       }
u64         module_opts_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return OPTS_TYPE;       }
u32         module_salt_type      (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return SALT_TYPE;       }
const char *module_st_hash        (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return ST_HASH;         }
const char *module_st_pass        (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra) { return ST_PASS;         }

typedef struct tc64_tmp
{
  u64  ipad[8];
  u64  opad[8];

  u64  dgst[32];
  u64  out[32];

} tc64_tmp_t;

typedef struct tc
{
  u32 salt_buf[32];
  u32 data_buf[112];
  u32 keyfile_buf[16];
  u32 signature;

  keyboard_layout_mapping_t keyboard_layout_mapping_buf[256];
  int                       keyboard_layout_mapping_cnt;

} tc_t;

static const int   ROUNDS_TRUECRYPT_1K         = 1000;
static const float MIN_SUFFICIENT_ENTROPY_FILE = 7.0f;

bool module_unstable_warning (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra, MAYBE_UNUSED const hc_device_param_t *device_param)
{
  if (device_param->opencl_platform_vendor_id == VENDOR_ID_APPLE)
  {
    // self-test failed
    if ((device_param->opencl_device_vendor_id == VENDOR_ID_AMD) && (device_param->opencl_device_type & CL_DEVICE_TYPE_GPU))
    {
      return true;
    }
  }

  return false;
}

bool module_potfile_disable (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
{
  const bool potfile_disable = true;

  return potfile_disable;
}

bool module_outfile_check_disable (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
{
  const bool outfile_check_disable = true;

  return outfile_check_disable;
}

u64 module_esalt_size (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
{
  const u64 esalt_size = (const u64) sizeof (tc_t);

  return esalt_size;
}

u64 module_tmp_size (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
{
  const u64 tmp_size = (const u64) sizeof (tc64_tmp_t);

  return tmp_size;
}

u32 module_pw_max (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra)
{
  // this overrides the reductions of PW_MAX in case optimized kernel is selected
  // IOW, even in optimized kernel mode it support length 64

  const u32 pw_max = 64;

  return pw_max;
}

int module_hash_init_selftest (MAYBE_UNUSED const hashconfig_t *hashconfig, hash_t *hash)
{
  const size_t st_hash_len = strlen (hashconfig->st_hash);

  char *tmpdata = (char *) hcmalloc (st_hash_len / 2);

  for (size_t i = 0, j = 0; j < st_hash_len; i += 1, j += 2)
  {
    const u8 c = hex_to_u8 ((const u8 *) hashconfig->st_hash + j);

    tmpdata[i] = c;
  }

  const int parser_status = module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, tmpdata, st_hash_len / 2);

  hcfree (tmpdata);

  return parser_status;
}

int module_hash_binary_parse (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED const user_options_t *user_options, MAYBE_UNUSED const user_options_extra_t *user_options_extra, hashes_t *hashes)
{
  // note: if module_hash_binary_parse exists, then module_hash_decode is not called

  HCFILE fp;

  if (hc_fopen (&fp, hashes->hashfile, "rb") == false) return (PARSER_HAVE_ERRNO);

  #define TC_HEADER_SIZE 512

  char *in = (char *) hcmalloc (TC_HEADER_SIZE);

  const size_t n = hc_fread (in, 1, TC_HEADER_SIZE, &fp);

  hc_fclose (&fp);

  if (n != TC_HEADER_SIZE) return (PARSER_TC_FILE_SIZE);

  hash_t *hashes_buf = hashes->hashes_buf;

  hash_t *hash = &hashes_buf[0];

  const int parser_status = module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, in, TC_HEADER_SIZE);

  if (parser_status != PARSER_OK) return 0;

  hcfree (in);

  // keyfiles

  tc_t *tc = (tc_t *) hash->esalt;

  if (user_options->truecrypt_keyfiles)
  {
    char *keyfiles = hcstrdup (user_options->truecrypt_keyfiles);

    char *saveptr = NULL;

    char *keyfile = strtok_r (keyfiles, ",", &saveptr);

    while (keyfile)
    {
      if (hc_path_read (keyfile))
      {
        cpu_crc32 (keyfile, (u8 *) tc->keyfile_buf);
      }

      keyfile = strtok_r ((char *) NULL, ",", &saveptr);
    }

    hcfree (keyfiles);
  }

  // keyboard layout mapping

  if (user_options->keyboard_layout_mapping)
  {
    if (hc_path_read (user_options->keyboard_layout_mapping))
    {
      initialize_keyboard_layout_mapping (user_options->keyboard_layout_mapping, tc->keyboard_layout_mapping_buf, &tc->keyboard_layout_mapping_cnt);
    }
  }

  return 1;
}

int module_hash_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSED void *digest_buf, MAYBE_UNUSED salt_t *salt, MAYBE_UNUSED void *esalt_buf, MAYBE_UNUSED void *hook_salt_buf, MAYBE_UNUSED hashinfo_t *hash_info, const char *line_buf, MAYBE_UNUSED const int line_len)
{
  u32 *digest = (u32 *) digest_buf;

  tc_t *tc = (tc_t *) esalt_buf;

  const float entropy = get_entropy ((const u8 *) line_buf, line_len);

  if (entropy < MIN_SUFFICIENT_ENTROPY_FILE) return (PARSER_INSUFFICIENT_ENTROPY);

  memcpy (tc->salt_buf, line_buf, 64);

  memcpy (tc->data_buf, line_buf + 64, 512 - 64);

  salt->salt_buf[0] = tc->salt_buf[0];

  salt->salt_len = 4;

  salt->salt_iter = ROUNDS_TRUECRYPT_1K - 1;

  tc->signature = 0x45555254; // "TRUE"

  digest[0] = tc->data_buf[0];

  return (PARSER_OK);
}

void module_init (module_ctx_t *module_ctx)
{
  module_ctx->module_context_size             = MODULE_CONTEXT_SIZE_CURRENT;
  module_ctx->module_interface_version        = MODULE_INTERFACE_VERSION_CURRENT;

  module_ctx->module_attack_exec              = module_attack_exec;
  module_ctx->module_benchmark_esalt          = MODULE_DEFAULT;
  module_ctx->module_benchmark_hook_salt      = MODULE_DEFAULT;
  module_ctx->module_benchmark_mask           = MODULE_DEFAULT;
  module_ctx->module_benchmark_salt           = MODULE_DEFAULT;
  module_ctx->module_build_plain_postprocess  = MODULE_DEFAULT;
  module_ctx->module_deep_comp_kernel         = MODULE_DEFAULT;
  module_ctx->module_dgst_pos0                = module_dgst_pos0;
  module_ctx->module_dgst_pos1                = module_dgst_pos1;
  module_ctx->module_dgst_pos2                = module_dgst_pos2;
  module_ctx->module_dgst_pos3                = module_dgst_pos3;
  module_ctx->module_dgst_size                = module_dgst_size;
  module_ctx->module_dictstat_disable         = MODULE_DEFAULT;
  module_ctx->module_esalt_size               = module_esalt_size;
  module_ctx->module_extra_buffer_size        = MODULE_DEFAULT;
  module_ctx->module_extra_tmp_size           = MODULE_DEFAULT;
  module_ctx->module_forced_outfile_format    = MODULE_DEFAULT;
  module_ctx->module_hash_binary_count        = MODULE_DEFAULT;
  module_ctx->module_hash_binary_parse        = module_hash_binary_parse;
  module_ctx->module_hash_binary_save         = MODULE_DEFAULT;
  module_ctx->module_hash_decode_potfile      = MODULE_DEFAULT;
  module_ctx->module_hash_decode_zero_hash    = MODULE_DEFAULT;
  module_ctx->module_hash_decode              = module_hash_decode;
  module_ctx->module_hash_encode_status       = MODULE_DEFAULT;
  module_ctx->module_hash_encode_potfile      = MODULE_DEFAULT;
  module_ctx->module_hash_encode              = MODULE_DEFAULT;
  module_ctx->module_hash_init_selftest       = module_hash_init_selftest;
  module_ctx->module_hash_mode                = MODULE_DEFAULT;
  module_ctx->module_hash_category            = module_hash_category;
  module_ctx->module_hash_name                = module_hash_name;
  module_ctx->module_hashes_count_min         = MODULE_DEFAULT;
  module_ctx->module_hashes_count_max         = MODULE_DEFAULT;
  module_ctx->module_hlfmt_disable            = MODULE_DEFAULT;
  module_ctx->module_hook_extra_param_size    = MODULE_DEFAULT;
  module_ctx->module_hook_extra_param_init    = MODULE_DEFAULT;
  module_ctx->module_hook_extra_param_term    = MODULE_DEFAULT;
  module_ctx->module_hook12                   = MODULE_DEFAULT;
  module_ctx->module_hook23                   = MODULE_DEFAULT;
  module_ctx->module_hook_salt_size           = MODULE_DEFAULT;
  module_ctx->module_hook_size                = MODULE_DEFAULT;
  module_ctx->module_jit_build_options        = MODULE_DEFAULT;
  module_ctx->module_jit_cache_disable        = MODULE_DEFAULT;
  module_ctx->module_kernel_accel_max         = MODULE_DEFAULT;
  module_ctx->module_kernel_accel_min         = MODULE_DEFAULT;
  module_ctx->module_kernel_loops_max         = MODULE_DEFAULT;
  module_ctx->module_kernel_loops_min         = MODULE_DEFAULT;
  module_ctx->module_kernel_threads_max       = MODULE_DEFAULT;
  module_ctx->module_kernel_threads_min       = MODULE_DEFAULT;
  module_ctx->module_kern_type                = module_kern_type;
  module_ctx->module_kern_type_dynamic        = MODULE_DEFAULT;
  module_ctx->module_opti_type                = module_opti_type;
  module_ctx->module_opts_type                = module_opts_type;
  module_ctx->module_outfile_check_disable    = module_outfile_check_disable;
  module_ctx->module_outfile_check_nocomp     = MODULE_DEFAULT;
  module_ctx->module_potfile_custom_check     = MODULE_DEFAULT;
  module_ctx->module_potfile_disable          = module_potfile_disable;
  module_ctx->module_potfile_keep_all_hashes  = MODULE_DEFAULT;
  module_ctx->module_pwdump_column            = MODULE_DEFAULT;
  module_ctx->module_pw_max                   = module_pw_max;
  module_ctx->module_pw_min                   = MODULE_DEFAULT;
  module_ctx->module_salt_max                 = MODULE_DEFAULT;
  module_ctx->module_salt_min                 = MODULE_DEFAULT;
  module_ctx->module_salt_type                = module_salt_type;
  module_ctx->module_separator                = MODULE_DEFAULT;
  module_ctx->module_st_hash                  = module_st_hash;
  module_ctx->module_st_pass                  = module_st_pass;
  module_ctx->module_tmp_size                 = module_tmp_size;
  module_ctx->module_unstable_warning         = module_unstable_warning;
  module_ctx->module_warmup_disable           = MODULE_DEFAULT;
}


// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package leveldb

import (
	"time"

	"github.com/syndtr/goleveldb/leveldb/memdb"
	"github.com/syndtr/goleveldb/leveldb/opt"
	"github.com/syndtr/goleveldb/leveldb/util"
)

func (db *DB) writeJournal(b *Batch) error {
	w, err := db.journal.Next()
	if err != nil {
		return err
	}
	if _, err := w.Write(b.encode()); err != nil {
		return err
	}
	if err := db.journal.Flush(); err != nil {
		return err
	}
	if b.sync {
		return db.journalWriter.Sync()
	}
	return nil
}

func (db *DB) jWriter() {
	defer db.closeW.Done()
	for {
		select {
		case b := <-db.journalC:
			if b != nil {
				db.journalAckC <- db.writeJournal(b)
			}
		case _, _ = <-db.closeC:
			return
		}
	}
}

func (db *DB) rotateMem(n int, wait bool) (mem *memDB, err error) {
	// Wait for pending memdb compaction.
	err = db.compTriggerWait(db.mcompCmdC)
	if err != nil {
		return
	}

	// Create new memdb and journal.
	mem, err = db.newMem(n)
	if err != nil {
		return
	}

	// Schedule memdb compaction.
	if wait {
		err = db.compTriggerWait(db.mcompCmdC)
	} else {
		db.compTrigger(db.mcompCmdC)
	}
	return
}

func (db *DB) flush(n int) (mdb *memDB, mdbFree int, err error) {
	delayed := false
	flush := func() (retry bool) {
		v := db.s.version()
		defer v.release()
		mdb = db.getEffectiveMem()
		defer func() {
			if retry {
				mdb.decref()
				mdb = nil
			}
		}()
		mdbFree = mdb.Free()
		switch {
		case v.tLen(0) >= db.s.o.GetWriteL0SlowdownTrigger() && !delayed:
			delayed = true
			time.Sleep(time.Millisecond)
		case mdbFree >= n:
			return false
		case v.tLen(0) >= db.s.o.GetWriteL0PauseTrigger():
			delayed = true
			err = db.compTriggerWait(db.tcompCmdC)
			if err != nil {
				return false
			}
		default:
			// Allow memdb to grow if it has no entry.
			if mdb.Len() == 0 {
				mdbFree = n
			} else {
				mdb.decref()
				mdb, err = db.rotateMem(n, false)
				if err == nil {
					mdbFree = mdb.Free()
				} else {
					mdbFree = 0
				}
			}
			return false
		}
		return true
	}
	start := time.Now()
	for flush() {
	}
	if delayed {
		db.writeDelay += time.Since(start)
		db.writeDelayN++
	} else if db.writeDelayN > 0 {
		db.logf("db@write was delayed N·%d T·%v", db.writeDelayN, db.writeDelay)
		db.writeDelay = 0
		db.writeDelayN = 0
	}
	return
}

// Write apply the given batch to the DB. The batch will be applied
// sequentially.
//
// It is safe to modify the contents of the arguments after Write returns.
func (db *DB) Write(b *Batch, wo *opt.WriteOptions) (err error) {
	err = db.ok()
	if err != nil || b == nil || b.Len() == 0 {
		return
	}

	b.init(wo.GetSync() && !db.s.o.GetNoSync())

	if b.size() > db.s.o.GetWriteBuffer() && !db.s.o.GetDisableLargeBatchTransaction() {
		// Writes using transaction.
		tr, err1 := db.OpenTransaction()
		if err1 != nil {
			return err1
		}
		if err1 := tr.Write(b, wo); err1 != nil {
			tr.Discard()
			return err1
		}
		return tr.Commit()
	}

	// The write happen synchronously.
	select {
	case db.writeC <- b:
		if <-db.writeMergedC {
			return <-db.writeAckC
		}
		// Continue, the write lock already acquired by previous writer
		// and handed out to us.
	case db.writeLockC <- struct{}{}:
	case err = <-db.compPerErrC:
		return
	case _, _ = <-db.closeC:
		return ErrClosed
	}

	merged := 0
	danglingMerge := false
	defer func() {
		for i := 0; i < merged; i++ {
			db.writeAckC <- err
		}
		if danglingMerge {
			// Only one dangling merge at most, so this is safe.
			db.writeMergedC <- false
		} else {
			<-db.writeLockC
		}
	}()

	mdb, mdbFree, err := db.flush(b.size())
	if err != nil {
		return
	}
	defer mdb.decref()

	// Calculate maximum size of the batch.
	m := 1 << 20
	if x := b.size(); x <= 128<<10 {
		m = x + (128 << 10)
	}
	m = minInt(m, mdbFree)

	// Merge with other batch.
drain:
	for b.size() < m && !b.sync {
		select {
		case nb := <-db.writeC:
			if b.size()+nb.size() <= m {
				b.append(nb)
				db.writeMergedC <- true
				merged++
			} else {
				danglingMerge = true
				break drain
			}
		default:
			break drain
		}
	}

	// Set batch first seq number relative from last seq.
	b.seq = db.seq + 1

	// Write journal concurrently if it is large enough.
	if b.size() >= (128 << 10) {
		// Push the write batch to the journal writer
		select {
		case db.journalC <- b:
			// Write into memdb
			if berr := b.memReplay(mdb.DB); berr != nil {
				panic(berr)
			}
		case err = <-db.compPerErrC:
			return
		case _, _ = <-db.closeC:
			err = ErrClosed
			return
		}
		// Wait for journal writer
		select {
		case err = <-db.journalAckC:
			if err != nil {
				// Revert memdb if error detected
				if berr := b.revertMemReplay(mdb.DB); berr != nil {
					panic(berr)
				}
				return
			}
		case _, _ = <-db.closeC:
			err = ErrClosed
			return
		}
	} else {
		err = db.writeJournal(b)
		if err != nil {
			return
		}
		if berr := b.memReplay(mdb.DB); berr != nil {
			panic(berr)
		}
	}

	// Set last seq number.
	db.addSeq(uint64(b.Len()))

	if b.size() >= mdbFree {
		db.rotateMem(0, false)
	}
	return
}

// Put sets the value for the given key. It overwrites any previous value
// for that key; a DB is not a multi-map.
//
// It is safe to modify the contents of the arguments after Put returns.
func (db *DB) Put(key, value []byte, wo *opt.WriteOptions) error {
	b := new(Batch)
	b.Put(key, value)
	return db.Write(b, wo)
}

// Delete deletes the value for the given key.
//
// It is safe to modify the contents of the arguments after Delete returns.
func (db *DB) Delete(key []byte, wo *opt.WriteOptions) error {
	b := new(Batch)
	b.Delete(key)
	return db.Write(b, wo)
}

func isMemOverlaps(icmp *iComparer, mem *memdb.DB, min, max []byte) bool {
	iter := mem.NewIterator(nil)
	defer iter.Release()
	return (max == nil || (iter.First() && icmp.uCompare(max, internalKey(iter.Key()).ukey()) >= 0)) &&
		(min == nil || (iter.Last() && icmp.uCompare(min, internalKey(iter.Key()).ukey()) <= 0))
}

// CompactRange compacts the underlying DB for the given key range.
// In particular, deleted and overwritten versions are discarded,
// and the data is rearranged to reduce the cost of operations
// needed to access the data. This operation should typically only
// be invoked by users who understand the underlying implementation.
//
// A nil Range.Start is treated as a key before all keys in the DB.
// And a nil Range.Limit is treated as a key after all keys in the DB.
// Therefore if both is nil then it will compact entire DB.
func (db *DB) CompactRange(r util.Range) error {
	if err := db.ok(); err != nil {
		return err
	}

	// Lock writer.
	select {
	case db.writeLockC <- struct{}{}:
	case err := <-db.compPerErrC:
		return err
	case _, _ = <-db.closeC:
		return ErrClosed
	}

	// Check for overlaps in memdb.
	mdb := db.getEffectiveMem()
	defer mdb.decref()
	if isMemOverlaps(db.s.icmp, mdb.DB, r.Start, r.Limit) {
		// Memdb compaction.
		if _, err := db.rotateMem(0, false); err != nil {
			<-db.writeLockC
			return err
		}
		<-db.writeLockC
		if err := db.compTriggerWait(db.mcompCmdC); err != nil {
			return err
		}
	} else {
		<-db.writeLockC
	}

	// Table compaction.
	return db.compTriggerRange(db.tcompCmdC, -1, r.Start, r.Limit)
}

// SetReadOnly makes DB read-only. It will stay read-only until reopened.
func (db *DB) SetReadOnly() error {
	if err := db.ok(); err != nil {
		return err
	}

	// Lock writer.
	select {
	case db.writeLockC <- struct{}{}:
		db.compWriteLocking = true
	case err := <-db.compPerErrC:
		return err
	case _, _ = <-db.closeC:
		return ErrClosed
	}

	// Set compaction read-only.
	select {
	case db.compErrSetC <- ErrReadOnly:
	case perr := <-db.compPerErrC:
		return perr
	case _, _ = <-db.closeC:
		return ErrClosed
	}

	return nil
}


<?xml version="1.0" encoding="Windows-1252"?>
<Grid
	xmlns="http://schemas.microsoft.com/client/2007"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Name="DrmMessage"
	Visibility="Visible"
	Opacity="1"
	CacheMode="BitmapCache"
	>
	<Border HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="8" Padding="10" MaxWidth="458">
		<Border.Background>
			<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
				<GradientStop Color="#FF242323" Offset="0"/>
				<GradientStop Color="#FF515151" Offset="1"/>
			</LinearGradientBrush>
		</Border.Background>
		<TextBlock TextAlignment="Center" TextWrapping="Wrap" FontWeight="Normal" Foreground="#FFFFFFFF" FontSize="14" x:Name="message" Text="This Silverlight application is using DRM-protected content, which Moonlight does not support" />
	</Border>
</Grid>


<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="?attr/colorControlNormal">
  <path
      android:fillColor="@android:color/white"
      android:pathData="M19,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM19,19L5,19L5,5h14v14zM13.96,12.29l-2.75,3.54 -1.96,-2.36L6.5,17h11l-3.54,-4.71z"/>
</vector>


#----------------------------导入工具库---------------------------#
"""本程序实现最近收缩质心分类模型"""
import os
from functools import reduce
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from DropDfNan import drop_df_nan                            # 导入数据清洗函数
from DiscrimAnalysisFit import discrim_analysis_fit          # 导入模型训练函数
from DiscrimAnalysisPredict import discrim_analysis_predict  # 导入模型预测函数
from ZeroOneLossFn import zero_one_loss_fn                   # 导入0-1损失函数
from FitCv import fit_cv                                     # 导入交叉验证函数
plt.rcParams['figure.figsize'] = (10, 8)

#-------------------------------加载数据----------------------------#

data_file = 'dataset'     # 数据所在的目录
df_xtrain = pd.read_table(\
                os.path.join(data_file, 'khan.xtrain'), sep='\s+', header=None, encoding='utf-8')
df_ytrain = pd.read_table(\
                os.path.join(data_file,'khan.ytrain'), sep='\s+', header=None, encoding='utf-8')
df_xtest = pd.read_table(\
                os.path.join(data_file, 'khan.xtest'), sep='\s+', header=None, encoding='utf-8')
df_ytest = pd.read_table(\
                os.path.join(data_file, 'khan.ytest'), sep='\s+', header=None, encoding='utf-8')

#-------------------------------数据预处理----------------------------#

df_xtrain, df_ytrain = drop_df_nan(df_xtrain, df_ytrain)
df_xtest, df_ytest = drop_df_nan(df_xtest, df_ytest)

# 按照机器学习中的常规习惯，我们将数据进行转置，
# 使shape=(n_samples,dim)，并将其转换为numpy类型
X_train = np.array(df_xtrain.T)
X_test = np.array(df_xtest.T)
y_train = np.array(df_ytrain.T).flatten()    # 将所有的类标签转换为1维数组,程序只支持单标签预测
y_test = np.array(df_ytest.T).flatten()

# 查看调整后的数据的形状
print('训练集的尺寸分别为{0}{1},测试集的尺寸分别为{2}{3}'.\
      format(X_train.shape, y_train.shape, X_test.shape, y_test.shape))
print('*********数据加载，并预处理成功*********')

#----------------------------- 对模型进行训练----------------------------#

fit_fn = lambda X, y, _lambda: \
                discrim_analysis_fit(X, y, 'shrunkenCentroids', lambdas=_lambda)    # 定义模型训练函数
predict_fn = lambda model, X: discrim_analysis_predict(model, X)                    # 定义模型预测函数

# 配置超参数
n_lambdas = 20
lambdas = np.linspace(0, 8, n_lambdas)

# 初始化误分类率
error_train_rate = np.empty((n_lambdas, ))
error_test_rate = np.empty((n_lambdas, ))

for i, single_lambda in enumerate(lambdas):
    print('Epoch {0}/{1} start....'.format(i+1, n_lambdas))

    model = fit_fn(X_train, y_train, single_lambda)                    # 对模型进行训练

    y_hat_train_index, _ = predict_fn(model, X_train)                  # 得到预测标签的索引值
    y_hat_test_index, _ = predict_fn(model, X_test)

    y_hat_train = model.support[y_hat_train_index.astype('int64')]     # 得到预测的类标签
    y_hat_test = model.support[y_hat_test_index.astype('int64')]

    error_train_rate[i] = np.sum(zero_one_loss_fn(y_hat_train, y_train))/len(X_train)     # 计算误分类率
    error_test_rate[i] = np.sum(zero_one_loss_fn(y_hat_test, y_test))/len(X_test)

    if single_lambda == 0:
        print('*******使用对角LDA模型时在测试集上的误分类数为{}******'.format(error_test_rate[i]*len(X_test)))

    print('Epoch {0}/{1} finished'.format(i+1, n_lambdas))

#----------------------------- 交叉验证----------------------------#

n_folds = 10                           # 交叉验证所需要的fold数目
X_all = np.vstack((X_train, X_test))   # 根据原文中的处理方式，将所有样本进行合并
y_all = np.hstack((y_train, y_test))
print('start cross validation....')
best_model, best_param, error_cv, se = \
              fit_cv(lambdas, fit_fn, predict_fn, zero_one_loss_fn, X_all, y_all, n_folds)
print('*****通过交叉验证得到的最佳参数为{}******'.format(best_param))
print('cross validation has finished.')
#----------------------------- 图形绘制----------------------------#
new_figure = plt.figure()
plt.plot(lambdas, error_train_rate, 'bo-', markersize=10, linewidth=2, label='training')
plt.plot(lambdas, error_test_rate, 'r*-', markersize=10, linewidth=2, label='testing')
plt.plot(lambdas, error_cv, 'g+-', markersize=10, linewidth=2, label='cv')

plt.legend(loc='upper left')
plt.tick_params(labelsize=23)
plt.xlabel(r'$\lambda$')
plt.ylabel('Misclassification Error')
plt.show()

#----------------------------- 绘制质心----------------------------#
cent_shrunk = best_model.shrunken_centroids        # 收缩质心

model = fit_fn(X_train, y_train, 0)                # 对应于对角LDA模型

cent_unshrunk = model.shrunken_centroids           # 未收缩质心

n_classes, dim = cent_unshrunk.shape
cent_shrunk_boolen = (cent_shrunk != 0)
cent_shrunk_not_zero = np.sum( (np.sum(cent_shrunk_boolen, axis=0)) > 0 )   # 不全部为0的特征有多少个
print('*****在最优模型的情况下，所需要的特征共{}个*****'.format(cent_shrunk_not_zero))

x_labels = ['(a)', '(b)', '(c)', '(d)']
for g in range(n_classes):
    new_figure = plt.figure()
    plt.plot(range(dim), cent_unshrunk[g,:], color=(0.8, 0.8, 0.8))    #  (0.8,0.8,0.8)为灰色的RGB参数
    plt.plot(range(dim), cent_shrunk[g,:], 'b', linewidth=2)
    plt.title('Class{}'.format(model.support[g]))
    plt.xlabel(x_labels[g])
    plt.show()


// Copyright (C) 2004-2008 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  Authors: Nick Edmonds
//           Douglas Gregor
//           Andrew Lumsdaine
#ifndef BOOST_DISTRIBUTED_FILTERED_GRAPH_HPP
#define BOOST_DISTRIBUTED_FILTERED_GRAPH_HPP

#ifndef BOOST_GRAPH_USE_MPI
#error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
#endif

#include <boost/graph/parallel/process_group.hpp>
#include <boost/graph/filtered_graph.hpp>

namespace boost {
  namespace graph {
          namespace parallel {
      /// Retrieve the process group from a filtered graph
      template<typename Graph, typename EdgePredicate, typename VertexPredicate>
      struct process_group_type<filtered_graph<Graph, EdgePredicate, VertexPredicate> >
        : process_group_type<Graph> { };

      template<typename Graph, typename EdgePredicate, typename VertexPredicate>
      struct process_group_type<const filtered_graph<Graph, EdgePredicate, VertexPredicate> >
        : process_group_type<Graph> { };
    }

  }

  /// Retrieve the process group from a filtered graph
  template<typename Graph, typename EdgePredicate, typename VertexPredicate>
  inline typename graph::parallel::process_group_type<Graph>::type
  process_group(filtered_graph<Graph, EdgePredicate, VertexPredicate> const& g) {
    return process_group(g.m_g);
  }

  /// Forward vertex() to vertex() of the base graph
  template <typename Graph, typename EdgePredicate, typename VertexPredicate>
  typename graph_traits<Graph>::vertex_descriptor
  vertex(typename graph_traits<Graph>::vertices_size_type i,
         filtered_graph<Graph, EdgePredicate, VertexPredicate> const& g)
  { return vertex(i, g.m_g); }

}

#endif // BOOST_DISTRIBUTED_FILTERED_GRAPH_HPP


<?php
namespace Payum\Core\Tests\Request;

use Payum\Core\Model\ModelAggregateInterface;
use Payum\Core\Model\ModelAwareInterface;
use Payum\Core\Request\GetStatusInterface;
use PHPUnit\Framework\TestCase;

class GetStatusInterfaceTest extends TestCase
{
    /**
     * @test
     */
    public function shouldImplementModelAwareInterface()
    {
        $rc = new \ReflectionClass(GetStatusInterface::class);

        $this->assertTrue($rc->implementsInterface(ModelAwareInterface::class));
    }

    /**
     * @test
     */
    public function shouldImplementModelAggregateInterface()
    {
        $rc = new \ReflectionClass(GetStatusInterface::class);

        $this->assertTrue($rc->implementsInterface(ModelAggregateInterface::class));
    }
}