text:
var RES;
(function (RES) {
    /**
     * SpriteSheet解析器
     */
    var SheetAnalyzer = (function (_super) {
        __extends(SheetAnalyzer, _super);
        function SheetAnalyzer() {
            _super.call(this);
            this.sheetMap = {};
            this.textureMap = {};
            this._dataFormat = egret.URLLoaderDataFormat.TEXT;
        }
        var __egretProto__ = SheetAnalyzer.prototype;
        /**
         * @inheritDoc
         */
        __egretProto__.getRes = function (name) {
            var res = this.fileDic[name];
            if (!res) {
                res = this.textureMap[name];
            }
            if (!res) {
                var prefix = RES.AnalyzerBase.getStringPrefix(name);
                res = this.fileDic[prefix];
                if (res) {
                    var tail = RES.AnalyzerBase.getStringTail(name);
                    res = res.getTexture(tail);
                }
            }
            return res;
        };
        /**
         * 一项加载结束
         */
        __egretProto__.onLoadFinish = function (event) {
            var loader = (event.target);
            var data = this.resItemDic[loader.hashCode];
            delete this.resItemDic[loader.hashCode];
            var resItem = data.item;
            var compFunc = data.func;
            resItem.loaded = (event.type == egret.Event.COMPLETE);
            if (resItem.loaded) {
                if (typeof (loader.data) == "string") {
                    resItem.loaded = false;
                    var imageUrl = this.analyzeConfig(resItem, loader.data);
                    if (imageUrl) {
                        var tempUrl = resItem.url;
                        resItem.url = imageUrl;
                        this._dataFormat = egret.URLLoaderDataFormat.TEXTURE;
                        this.loadFile(resItem, compFunc, data.thisObject);
                        this._dataFormat = egret.URLLoaderDataFormat.TEXT;
                        resItem.url = tempUrl;
                        return;
                    }
                }
                else {
                    this.analyzeBitmap(resItem, loader.data);
                }
            }
            this.recycler.push(loader);
            compFunc.call(data.thisObject, resItem);
        };
        /**
         * 解析并缓存加载成功的配置文件
         */
        __egretProto__.analyzeConfig = function (resItem, data) {
            var name = resItem.name;
            var config;
            var imageUrl = "";
            try {
                var str = data;
                config = JSON.parse(str);
            }
            catch (e) {
                egret.Logger.warningWithErrorId(1017, resItem.url, data);
            }
            if (config) {
                this.sheetMap[name] = config;
                imageUrl = this.getRelativePath(resItem.url, config["file"]);
            }
            return imageUrl;
        };
        /**
         * 解析并缓存加载成功的位图数据
         */
        __egretProto__.analyzeBitmap = function (resItem, data) {
            var name = resItem.name;
            if (this.fileDic[name] || !data) {
                return;
            }
            var config = this.sheetMap[name];
            delete this.sheetMap[name];
            var targetName = resItem.data && resItem.data.subkeys ? "" : name;
            var spriteSheet = this.parseSpriteSheet(data, config, targetName);
            this.fileDic[name] = spriteSheet;
        };
        /**
         * 获取相对位置
         */
        __egretProto__.getRelativePath = function (url, file) {
            url = url.split("\\").join("/");
            var index = url.lastIndexOf("/");
            if (index != -1) {
                url = url.substring(0, index + 1) + file;
            }
            else {
                url = file;
            }
            return url;
        };
        __egretProto__.parseSpriteSheet = function (texture, data, name) {
            var frames = data.frames;
            if (!frames) {
                return null;
            }
            var spriteSheet = new egret.SpriteSheet(texture);
            var textureMap = this.textureMap;
            for (var subkey in frames) {
                var config = frames[subkey];
                var texture = spriteSheet.createTexture(subkey, config.x, config.y, config.w, config.h, config.offX, config.offY, config.sourceW, config.sourceH);
                if (config["scale9grid"]) {
                    var str = config["scale9grid"];
                    var list = str.split(",");
                    texture["scale9Grid"] = new egret.Rectangle(parseInt(list[0]), parseInt(list[1]), parseInt(list[2]), parseInt(list[3]));
                }
                if (textureMap[subkey] == null) {
                    textureMap[subkey] = texture;
                    if (name) {
                        this.addSubkey(subkey, name);
                    }
                }
            }
            return spriteSheet;
        };
        /**
         * @inheritDoc
         */
        __egretProto__.destroyRes = function (name) {
            var sheet = this.fileDic[name];
            if (sheet) {
                delete this.fileDic[name];
                for (var subkey in sheet._textureMap) {
                    if (this.textureMap[subkey]) {
                        delete this.textureMap[subkey];
                    }
                }
                return true;
            }
            return false;
        };
        return SheetAnalyzer;
    })(RES.BinAnalyzer);
    RES.SheetAnalyzer = SheetAnalyzer;
    SheetAnalyzer.prototype.__class__ = "RES.SheetAnalyzer";
})(RES || (RES = {}));


text:
package com.haden.family.fragment.home;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.haden.family.R;
import com.haden.family.data.DataManager;
import com.haden.family.data.Family;
import com.haden.family.data.Person;
import com.haden.family.data.Role;
import com.haden.family.fragment.BaseFragment;

public class HomeFragment extends BaseFragment {
	List<Family> list;

	View rootView;
	AdapterViewFlipper flipper;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_home, container, false);

		list = new ArrayList<Family>();
		list.add(DataManager.family);

		flipper = (AdapterViewFlipper) rootView.findViewById(R.id.flipper);
		flipper.setAdapter(new MyAdapter());

		return rootView;
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(HomeFragment.this
						.getActivity());
				convertView = inflater.inflate(R.layout.item_home, parent,
						false);
				holder = new ViewHolder();
				holder.layout0 = (LinearLayout) convertView
						.findViewById(R.id.layout0);
				holder.layout1 = (LinearLayout) convertView
						.findViewById(R.id.layout1);
				holder.layout2 = (LinearLayout) convertView
						.findViewById(R.id.layout2);
				holder.layout3 = (LinearLayout) convertView
						.findViewById(R.id.layout3);
				holder.layout4 = (LinearLayout) convertView
						.findViewById(R.id.layout4);

				holder.btn0 = (ImageButton) convertView
						.findViewById(R.id.btn_0);
				holder.btn1 = (ImageButton) convertView
						.findViewById(R.id.btn_1);
				holder.btn2 = (ImageButton) convertView
						.findViewById(R.id.btn_2);
				holder.btn3 = (ImageButton) convertView
						.findViewById(R.id.btn_3);
				holder.btn4 = (ImageButton) convertView
						.findViewById(R.id.btn_4);

				holder.btn0f = (ImageButton) convertView
						.findViewById(R.id.btn_0f);
				holder.btn1f = (ImageButton) convertView
						.findViewById(R.id.btn_1f);
				holder.btn2f = (ImageButton) convertView
						.findViewById(R.id.btn_2f);
				holder.btn3f = (ImageButton) convertView
						.findViewById(R.id.btn_3f);
				holder.btn4f = (ImageButton) convertView
						.findViewById(R.id.btn_4f);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			setItemView(holder, position);

			return convertView;
		}

		private void setItemView(ViewHolder holder, int position) {
			Family family = list.get(position);
			Map<Role, Map<String, Person>> familyList = family.getFamilyList();

			if (familyList.get(Role.Grandson) == null) {
				holder.btn0.setVisibility(View.GONE);
			} else {
				holder.btn0.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Son) == null) {
				holder.btn1.setVisibility(View.GONE);
			} else {
				holder.btn1.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Husband) == null) {
				holder.btn2.setVisibility(View.GONE);
			} else {
				holder.btn2.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Father) == null) {
				holder.btn3.setVisibility(View.GONE);
			} else {
				holder.btn3.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Grandfather) == null) {
				holder.btn4.setVisibility(View.GONE);
			} else {
				holder.btn4.setVisibility(View.VISIBLE);
			}

			if (familyList.get(Role.Granddaught) == null) {
				holder.btn0f.setVisibility(View.GONE);
			} else {
				holder.btn0f.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Daught) == null) {
				holder.btn1f.setVisibility(View.GONE);
			} else {
				holder.btn1f.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Wife) == null) {
				holder.btn2f.setVisibility(View.GONE);
			} else {
				holder.btn2f.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Mother) == null) {
				holder.btn3f.setVisibility(View.GONE);
			} else {
				holder.btn3f.setVisibility(View.VISIBLE);
			}
			if (familyList.get(Role.Grandmother) == null) {
				holder.btn4f.setVisibility(View.GONE);
			} else {
				holder.btn4f.setVisibility(View.VISIBLE);
			}

			if (holder.btn0.getVisibility() == View.GONE
					&& holder.btn0f.getVisibility() == View.GONE) {
				holder.layout0.setVisibility(View.GONE);
			} else {
				holder.layout0.setVisibility(View.VISIBLE);
			}
			if (holder.btn1.getVisibility() == View.GONE
					&& holder.btn1f.getVisibility() == View.GONE) {
				holder.layout1.setVisibility(View.GONE);
			} else {
				holder.layout1.setVisibility(View.VISIBLE);
			}
			if (holder.btn2.getVisibility() == View.GONE
					&& holder.btn2f.getVisibility() == View.GONE) {
				holder.layout2.setVisibility(View.GONE);
			} else {
				holder.layout2.setVisibility(View.VISIBLE);
			}
			if (holder.btn3.getVisibility() == View.GONE
					&& holder.btn3f.getVisibility() == View.GONE) {
				holder.layout3.setVisibility(View.GONE);
			} else {
				holder.layout3.setVisibility(View.VISIBLE);
			}
			if (holder.btn4.getVisibility() == View.GONE
					&& holder.btn4f.getVisibility() == View.GONE) {
				holder.layout4.setVisibility(View.GONE);
			} else {
				holder.layout4.setVisibility(View.VISIBLE);
			}

		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}

	class ViewHolder {
		LinearLayout layout0;
		LinearLayout layout1;
		LinearLayout layout2;
		LinearLayout layout3;
		LinearLayout layout4;

		ImageButton btn0;
		ImageButton btn1;
		ImageButton btn2;
		ImageButton btn3;
		ImageButton btn4;

		ImageButton btn0f;
		ImageButton btn1f;
		ImageButton btn2f;
		ImageButton btn3f;
		ImageButton btn4f;

	}
}

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.ibase4j.mapper.SysDicMapper">
	<select id="selectIdPage" resultType="java.lang.Long">
		select id_ from sys_dic
		<where>
			<if test="cm.type != null and cm.type != ''">
			and type_=#{cm.type}
			</if>
			<if test="cm.parentType != null and cm.parentType != ''">
			and parent_type=#{cm.parentType}
			</if>
			<if test="cm.parentCode != null and cm.parentCode != ''">
			and parent_code=#{cm.parentCode}
			</if>
			<if test="cm.keyword != null and cm.keyword != ''">
				and (type_ like #{cm.keyword}"%" OR code_ like #{cm.keyword}"%")
			</if>
		</where>
	</select>
</mapper>


text:
import * as Command from './Command';
import { GameState } from './GameState';

export interface EntityType {
  id: string
  name: string
  description: string
  char: string
  color: string
  update: (state: GameState) => Array<Command.Command>
}

export const Player: EntityType = {
  id: 'Player',
  name: 'Player',
  description: 'This is you.',
  char: '@',
  color: '#FFFFFF',
  update: (state: GameState) => undefined
}

export const HugeHollow: EntityType = {
  id: 'HugeHollow',
  name: 'Huge Hollow',
  description: 'It is a huge hollow. It is very scary.',
  char: 'H',
  color: '#FF0000',
  update: (state: GameState) => {
    return undefined;
    // return [
    //   new Command.ShowMessage("The huge hollow looks at you!")
    // ]
  }
}

let classes: { [id: string]: EntityType } = {};
classes[Player.id] = Player;
classes[HugeHollow.id] = HugeHollow;

export function getClassById(id: string) {
  return classes[id];
}


text:
from enum import Enum, IntEnum, IntFlag

from commandment.apps import ManagedAppStatus
from ..models import db

class ManagementFlag(IntFlag):
    """This enum of integer bitwise OR flags represents all the fields available as part of the ``ManagementFlag``
    option to the ``InstallApplication`` command."""
    NOTHING = 0
    REMOVE_APP_WITH_ENROLLMENT = 1
    PREVENT_APPDATA_BACKUP = 4

class PurchaseMethod(IntEnum):
    """Purchase methods, the flag should almost always be VPP_APP_ASSIGNMENT"""
    LEGACY_VPP = 0
    VPP_APP_ASSIGNMENT = 1

class ApplicationType(Enum):
    """A list of the polymorphic identities available for subclasses of Application."""
    ENTERPRISE_MAC = 'enterprise_mac'
    ENTERPRISE_IOS = 'enterprise_ios'
    APPSTORE_MAC = 'appstore_mac'
    APPSTORE_IOS = 'appstore_ios'

application_tags = db.Table(
    'application_tags',
    db.metadata,
    db.Column('application_id', db.Integer, db.ForeignKey('applications.id')),
    db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')),
)

class Application(db.Model):
    """This table holds details of each individual application that may be
     managed (either app store or enterprise application).

    :table: applications
    """
    __tablename__ = 'applications'

    id = db.Column(db.Integer, primary_key=True)
    """id (db.Integer): ID"""
    display_name = db.Column(db.String, nullable=False)
    """display_name (db.String): The name of the application displayed in the MDM."""
    description = db.Column(db.String)
    """description (db.String): Description of this application, possibly including release notes."""
    version = db.Column(db.String)
    """version (db.String): Application version."""
    itunes_store_id = db.Column(db.Integer)
    """itunes_store_id (db.Integer): The application's iTunes Store ID."""
    bundle_id = db.Column(db.String, index=True, nullable=False)
    """bundle_id (db.String): The application bundle identifier."""
    purchase_method = db.Column(db.Enum(PurchaseMethod))
    """purchase_method (db.Integer): Used in the Options key of InstallApplication to denote the purchase method."""
    manifest_url = db.Column(db.String)
    """manifest_url (db.String): The application manifest URL if iTunesStoreID is not supplied (an enterprise app)."""
    management_flags = db.Column(db.Integer)
    """management_flags (ManagementFlag): Denotes whether app is removed with MDM profile, and whether the user may back
        up application data."""
    change_management_state = db.Column(db.String, default="Managed")
    """change_management_state (db.String): Take ownership of an existing application that is unmanaged."""
    discriminator = db.Column(db.String(20))
    """discriminator (str): The type of application"""

    # iTunes Search API - Cached Result
    country = db.Column(db.String(2))
    """country (str): The two letter country code of the store country. We cache this to avoid assigning apps to devices
        that cannot even install them due to the Apple ID residing in a different locale."""

    artist_id = db.Column(db.Integer)
    """artist_id (int): The iTunes Artist ID, which is commonly the developer in the app store."""
    artist_name = db.Column(db.String)
    """artist_id (str): The iTunes Artist Name, which is commonly the developer in the app store."""
    artist_view_url = db.Column(db.String)
    artwork_url60 = db.Column(db.String)
    """artwork_url60 (str): A URL to the 60x60 icon for this result."""
    artwork_url100 = db.Column(db.String)
    """artwork_url100 (str): A URL to the 100x100 icon for this result."""
    artwork_url512 = db.Column(db.String)
    """artwork_url512 (str): A URL to the 512x512 icon for this result."""
    release_notes = db.Column(db.String)
    release_date = db.Column(db.DateTime)
    minimum_os_version = db.Column(db.String)
    file_size_bytes = db.Column(db.BigInteger)

    tags = db.relationship(
        'Tag',
        secondary=application_tags,
        backref='applications'
    )

    __mapper_args__ = {
        'polymorphic_on': discriminator,
        'polymorphic_identity': 'applications',
    }

class EnterpriseMacApplication(Application):
    """Polymorphic single table inheritance specifically for Enterprise Mac Applications.

    These applications are .pkg files which are often distributed by the MDM or from a host outside of the App Store.
    """
    __mapper_args__ = {
        'polymorphic_identity': ApplicationType.ENTERPRISE_MAC.value
    }

class EnterpriseiOSApplication(Application):
    """Polymorphic single table inheritance specifically for Enterprise iOS Applications.

    These applications are .ipa files which are often distributed by the MDM or from a host outside of the App Store.
    With or without provisioning profiles.
    """
    __mapper_args__ = {
        'polymorphic_identity': ApplicationType.ENTERPRISE_IOS.value
    }

class AppstoreMacApplication(Application):
    """Polymorphic single table inheritance specifically for MAS (App Store) Mac Applications.

    These applications are distributed by VPP using an iTunes Store ID
    """
    __mapper_args__ = {
        'polymorphic_identity': ApplicationType.APPSTORE_MAC.value
    }

class AppstoreiOSApplication(Application):
    """Polymorphic single table inheritance specifically for App Store iOS Applications.

    These applications are distributed by VPP using an iTunes Store ID
    """
    __mapper_args__ = {
        'polymorphic_identity': ApplicationType.APPSTORE_IOS.value
    }

class ApplicationManifest(db.Model):
    """An application manifest describes a non-App store installable application.

    See: `macOS Application <https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/MobileDeviceManagementProtocolRef/3-MDM_Protocol/MDM_Protocol.html#//apple_ref/doc/uid/TP40017387-CH3-SW755>`_.

    :table: application_manifests
    """
    __tablename__ = 'application_manifests'

    id = db.Column(db.Integer, primary_key=True)
    """id (db.Integer): ID"""
    bundle_id = db.Column(db.String, index=True, nullable=False)
    """bundle_id (db.String): Bundle Identifier of the top-level distribution package."""
    bundle_version = db.Column(db.String, index=True)
    """bundle_version (db.String): Bundle Version of the top-level distribution package."""
    kind = db.Column(db.String, default='software')
    """kind (db.String): Type of item to install, at the moment ignored and always set to 'software'."""
    size_in_bytes = db.Column(db.BigInteger)
    """size_in_bytes (db.BigInteger): Size of the package (in bytes)."""
    subtitle = db.Column(db.String)
    """subtitle (db.String):"""
    title = db.Column(db.String)
    """title (db.String):"""
    full_size_image_url = db.Column(db.String)
    """full_size_image_url (db.String): URL to full size image. may be null"""
    full_size_image_needs_shine = db.Column(db.Boolean, default=False)
    """full_size_image_needs_shine (db.Boolean): Whether the image needs the shine effect placed over it."""
    display_image_url = db.Column(db.String)
    """display_image_url (db.String): URL to display image. may be null"""
    display_image_needs_shine = db.Column(db.Boolean, default=False)
    """display_image_needs_shine (db.Boolean): Whether the display image needs the shine effect placed over it."""
    checksums = db.relationship('ApplicationManifestChecksum', back_populates='application_manifest')

class ApplicationManifestChecksum(db.Model):
    __tablename__ = 'application_manifest_checksums'

    id = db.Column(db.Integer, primary_key=True)
    """id (db.Integer): ID"""
    application_manifest_id = db.Column(db.Integer, db.ForeignKey('application_manifests.id'))
    """application_manifest_id (db.Integer): Foreign key reference to the parent manifest."""
    application_manifest = db.relationship(ApplicationManifest, back_populates='checksums')
    """application_manifest (db.relationship): Relationship to the parent manifest."""
    checksum_index = db.Column(db.Integer, nullable=False)
    """checksum_index (db.Integer): Index of this checksum in the sequence of checksums."""
    checksum_value = db.Column(db.String(32), nullable=False)
    """checksum_value (db.String): 32 byte MD5 checksum of this chunk. Chunk size is defined as 10485760 bytes (10mb)"""

class AppSourceType(Enum):
    S3 = 'S3'
    Munki = 'Munki'

class ApplicationSource(db.Model):
    """This table holds rows indicating sources that may referenced in ``InstallApplication`` commands.

    The MDM may require write access to create application manifests from existing items.

    :table: application_sources
    """
    __tablename__ = 'application_sources'

    id = db.Column(db.Integer, primary_key=True)
    """id (db.Integer): ID"""
    name = db.Column(db.String)
    """name (db.String): A short, descriptive name for the source. Only used in display."""
    source_type = db.Column(db.Enum(AppSourceType), default=AppSourceType.Munki)
    """source_type (AppSourceType): The application source type."""

    endpoint = db.Column(db.String)
    """endpoint (db.String): The hostname for object storage or URI for read-only munki repositories."""
    mount_uri = db.Column(db.String)
    """mount_uri (db.String): The R/W mount URI for munki repositories only."""
    use_ssl = db.Column(db.Boolean)
    """use_ssl (Boolean): Use SSL when connecting to endpoint. Used when endpoint is host only."""

    # For S3 / Minio
    access_key = db.Column(db.String)
    """access_key (db.String): The access key for S3 / Minio that uniquely identifies this client."""
    secret_key = db.Column(db.String)
    """secret_key (db.String): The secret key for S3 / Minio that authenticates this client."""
    bucket = db.Column(db.String)
    """bucket (db.String): The bucket name that holds installation packages."""

class ManagedApplication(db.Model):
    """This table holds rows for application installation statuses that are reported by the `ManagedApplicationList`
    command."""
    __tablename__ = 'managed_applications'

    id = db.Column(db.Integer, primary_key=True)
    """id (db.Integer): ID"""
    device_id = db.Column(db.ForeignKey('devices.id'))
    """(int): Device foreign key ID."""
    device = db.relationship('Device', backref='managed_applications')
    """(db.relationship): Device relationship"""
    bundle_id = db.Column(db.String)
    """(db.String): The bundle identifier of the application being installed."""
    external_version_id = db.Column(db.Integer)
    """(db.Integer): The external version identifier (which is also shown in the vpp contentMetadataUrl lookup)"""
    has_configuration = db.Column(db.Boolean)
    """(db.Boolean): Whether the app has managed app configuration or not."""
    has_feedback = db.Column(db.Boolean)
    """(db.Boolean): Whether the app has managed app feedback or not."""
    is_validated = db.Column(db.Boolean)
    """(db.Boolean): Whether the app has been validated."""
    management_flags = db.Column(db.Integer)
    """(db.Integer): Which management flags the application has been installed with."""
    status = db.Column(db.Enum(ManagedAppStatus))
    """(ManagedAppStatus): The status of the managed application."""
    application_id = db.Column(db.ForeignKey('applications.id'), nullable=True)
    """(db.ForeignKey): Foreign key reference to the application row which was assigned to the device"""
    application = db.relationship('Application', backref='managed_applications')
    """(db.relationship): relationship to the defined application object."""
    ia_command_id = db.Column(db.ForeignKey('commands.id'), nullable=True)
    """(db.ForeignKey): Foreign key reference to the last `InstallApplication` command that
        installed this app on the device."""
    ia_command = db.relationship('Command', backref='managed_application')
    """(db.relationship): Relationship to the last command that was sent in regards to this application entry"""


text:
package vecbackup

import (
	"bufio"
	"bytes"
	"crypto/sha512"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"sort"
	"sync"
	"time"
)

const (
	VC_VERSION              = 1
	VC_MAGIC                = "VBKC"
	VV_VERSION              = 1
	VV_MAGIC                = "VBKV"
	CONFIG_FILE             = "vecbackup-config"
	VERSION_DIR             = "versions"
	CHUNK_DIR               = "chunks"
	VERSION_FILENAME_PREFIX = "version-"
	LOCK_FILENAME           = "lock"
	RESTORE_TEMP_SUFFIX     = ".vbk.restore.temp"
	DEFAULT_DIR_PERM        = 0700
	DEFAULT_FILE_PERM       = 0600
	PATH_SEP                = string(os.PathSeparator)
)

var stdout = log.New(os.Stdout, "", 0)
var stderr = log.New(os.Stderr, "", 0) //log.Lshortfile)
var debug = false

func SetDebug(dbg bool) {
	debug = dbg
}

func debugP(fmt string, v ...interface{}) {
	if debug {
		log.Printf(fmt, v...)
	}
}

func readExcludeFile(fn string) ([]string, error) {
	if fn == "" {
		return nil, nil
	}
	excludePatterns := []string{}
	in, err := os.Open(fn)
	if err != nil {
		return nil, err
	}
	defer in.Close()
	scanner := bufio.NewScanner(in)
	for scanner.Scan() {
		l := scanner.Text()
		if len(l) > 0 {
			_, err := filepath.Match(l, "zzz")
			if err != nil {
				return nil, fmt.Errorf("Bad exclude pattern: %s", l)
			} else {
				excludePatterns = append(excludePatterns, l)
			}
		}
	}
	return excludePatterns, nil
}

func toExclude(excludePatterns []string, dir, fn string) bool {
	for _, p := range excludePatterns {
		var matched bool
		var err error
		if p[0] == os.PathSeparator {
			matched, err = filepath.Match(p, PATH_SEP+filepath.Join(dir, fn))
		} else {
			matched, err = filepath.Match(p, fn)
		}
		if err == nil && matched {
			return true
		}
	}
	return false
}

func isSymlink(f os.FileInfo) bool {
	return f.Mode()&os.ModeSymlink != 0
}

type fileDataMap struct {
	names []string
	files map[string]*FileData
}

func (fdm *fileDataMap) Init() {
	fdm.files = make(map[string]*FileData)
	fdm.names = nil
}

func (fdm *fileDataMap) AddItem(fd *FileData) bool {
	n := fd.Name
	if fdm.files[n] == nil {
		fdm.files[n] = fd
		fdm.names = append(fdm.names, n)
		return true
	}
	return false
}

func scanOneDir(src string, f os.FileInfo, excludes []string, fdm *fileDataMap) int {
	if f == nil {
		if fi, err := os.Lstat(src); err != nil {
			stderr.Printf("F %s: %s\n", src, err)
			return 1
		} else {
			f = fi
		}
	}
	if f.IsDir() {
		if fdm.AddItem(NewDirectory(src, f.Mode().Perm())) {
			if files, err := ioutil.ReadDir(src); err != nil {
				stderr.Printf("F %s: %s\n", src, err)
				return 1
			} else {
				errs := 0
				for _, child := range files {
					if toExclude(excludes, src, child.Name()) {
						continue
					}
					errs2 := scanOneDir(filepath.Join(src, child.Name()), child, excludes, fdm)
					errs = errs + errs2
				}
				return errs
			}
		}
	} else if f.Mode().IsRegular() {
		fdm.AddItem(NewRegularFile(src, f.Size(), f.ModTime(), f.Mode().Perm(), nil, nil, nil))
	} else if isSymlink(f) {
		if target, err := os.Readlink(src); err != nil {
			stderr.Printf("F %s: %s\n", src, err)
			return 1
		} else {
			fdm.AddItem(NewSymlink(src, target))
		}
	}
	return 0
}

func scanSrcs(excludes []string, srcs []string) (*fileDataMap, int) {
	errs := 0
	fdm := &fileDataMap{}
	fdm.Init()
	for _, src := range srcs {
		src = filepath.Clean(src)
		err2 := scanOneDir(src, nil, excludes, fdm)
		errs = errs + err2
	}
	return fdm, errs
}

func makeChunkFP(secret []byte, origFp FP) FP {
	if secret == nil {
		return origFp
	}
	s := append(append([]byte(nil), secret...), origFp[:]...)
	return sha512.Sum512_256(s)
}

func matchChunkFP(secret []byte, fp FP, b []byte) bool {
	fp2 := makeChunkFP(secret, sha512.Sum512_256(b))
	return fp == fp2
}

func statsRemoveFile(fd *FileData, stats *BackupStats) {
	if fd != nil {
		if fd.IsFile() {
			stats.FilesRemoved++
		} else if fd.IsDir() {
			stats.DirsRemoved++
		} else if fd.IsSymlink() {
			stats.SymlinksRemoved++
		}
	}
}

func backupOneNode(cm *CMgr, mem *addChunkMem, dryRun, force, checkChunks, verbose bool, old *FileData, new *FileData, secret []byte, mu *sync.Mutex, stats *BackupStats) (*FileData, error) {
	mu.Lock()
	defer mu.Unlock()
	if old != nil && new == nil {
		statsRemoveFile(old, stats)
		if verbose {
			stdout.Printf("- %s\n", old.PrettyPrint())
		}
		return nil, nil
	}
	to_add := old
	if force || old == nil && new != nil || new.Type != old.Type || (new.IsFile() && (new.Size != old.Size || !new.ModTime.Equal(old.ModTime))) || (new.IsSymlink() && new.Target != old.Target) {
		to_add = new
	} else if old.IsFile() {
		if checkChunks {
			for _, chunk := range old.Chunks {
				if !cm.FindChunk(chunk) {
					stderr.Printf("Missing chunk %s from file %s\n", chunk, old.PrettyPrint())
					to_add = new
					break
				}
			}
		}
	}
	var addSrcSize int64 = 0
	var addRepoSize int64 = 0
	if to_add == new {
		if new.IsFile() {
			if !dryRun {
				mu.Unlock()
				srcAdded, repoAdded, err := addChunks(new, cm, mem, dryRun, secret)
				mu.Lock()
				if err != nil {
					stderr.Printf("F %s: %s\n", to_add.PrettyPrint(), err)
					stats.Errors++
					return nil, err
				} else {
					addSrcSize = srcAdded
					addRepoSize = repoAdded
				}
			}
			if old == nil || !old.IsFile() {
				stats.FilesNew++
				statsRemoveFile(old, stats)
			} else {
				stats.FilesUpdated++
			}
		} else if new.IsDir() {
			if old == nil || !old.IsDir() {
				stats.DirsNew++
				statsRemoveFile(old, stats)
			} else {
				stats.DirsUpdated++
			}
		} else if new.IsSymlink() {
			if old == nil || !old.IsSymlink() {
				stats.SymlinksNew++
				statsRemoveFile(old, stats)
			} else {
				stats.SymlinksUpdated++
			}
		}
		if verbose {
			stdout.Printf("+ %v\n", to_add.PrettyPrint())
		}
	} else {
		if !old.IsSymlink() {
			old.Perm = new.Perm
		}
	}
	if to_add.IsFile() {
		stats.Files++
		stats.Size += new.Size
		stats.SrcAdded += addSrcSize
		stats.RepoAdded += addRepoSize
	} else if to_add.IsDir() {
		stats.Dirs++
	} else {
		stats.Symlinks++
	}
	return to_add, nil
}

func addChunks(fd *FileData, cm *CMgr, mem *addChunkMem, dryRun bool, secret []byte) (int64, int64, error) {
	h := sha512.New512_256()
	var chunks []FP = nil
	var sizes []int32
	file, err := os.Open(fd.Name)
	if err != nil {
		return 0, 0, err
	}
	defer file.Close()
	var n int64 = 0
	var srcAdded int64 = 0
	var repoAdded int64 = 0
	blockSize := mem.chunkSize
	for {
		mem.setSize(blockSize)
		buf := mem.buf()
		count, err := io.ReadFull(file, buf)
		if count > 0 {
			mem.setSize(count)
			buf := mem.buf()
			n += int64(count)
			h.Write(buf)
			var chunk FP = makeChunkFP(secret, sha512.Sum512_256(buf))
			dup, compressedLen, err := cm.AddChunk(chunk, mem)
			if err != nil {
				return 0, 0, err
			}
			srcAdded = srcAdded + int64(count)
			if !dup {
				repoAdded = repoAdded + int64(compressedLen)
			}
			chunks = append(chunks, chunk)
			sizes = append(sizes, int32(count))
		}
		if n > fd.Size {
			return 0, 0, fmt.Errorf("File size changed %s", fd.Name)
		}
		if err == io.EOF || count < blockSize {
			if n < fd.Size {
				return 0, 0, fmt.Errorf("File size changed %s", fd.Name)
			}
			break
		}
	}
	fd.Chunks = chunks
	fd.Sizes = sizes
	fd.FileChecksum = h.Sum(nil)
	return srcAdded, repoAdded, nil
}

func matchRestorePatterns(fp string, patterns []string) bool {
	if len(patterns) == 0 {
		return true
	}
	for _, pat := range patterns {
		if matchRestorePattern(fp, pat) {
			return true
		}
	}
	return false
}

func matchRestorePattern(p, pat string) bool {
	if p == pat {
		return true
	}
	l := len(pat)
	return l == 0 || (len(p) > l && p[:l] == pat && os.IsPathSeparator(p[l]))
}

func restoreFileToTemp(fd *FileData, cm *CMgr, mem *readChunkMem, fn string, verifyOnly bool, secret []byte) error {
	var f *os.File
	if !verifyOnly {
		d := filepath.Dir(fn)
		err := os.MkdirAll(d, DEFAULT_DIR_PERM)
		f, err = os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fd.Perm|DEFAULT_FILE_PERM)
		if err != nil {
			return err
		}
		defer f.Close()
	}
	var l int64
	h := sha512.New512_256()
	for _, chunk := range fd.Chunks {
		b, err := cm.ReadChunk(chunk, mem)
		if err == nil && !matchChunkFP(secret, chunk, b) {
			err = fmt.Errorf("Bad chunk, mismatch fingerpint %s", chunk)
		}
		if err != nil {
			if os.IsNotExist(err) {
				return fmt.Errorf("Missing chunk %s", chunk)
			} else {
				return fmt.Errorf("Bad chunk %s", chunk)
			}
		}
		h.Write(b)
		l += int64(len(b))
		if !verifyOnly {
			_, err = f.Write(b)
			if err != nil {
				return err
			}
		}
	}
	if l != fd.Size {
		return fmt.Errorf("Length mismatch: %d vs %d", l, fd.Size)
	}
	cs := h.Sum(nil)
	if bytes.Compare(fd.FileChecksum, cs) != 0 {
		fmt.Println("CHECKSUMS", fd.FileChecksum, cs)
		return errors.New("File checksum mismatch")
	}
	return nil
}

func restoreDir(fd *FileData, resDir string, dryRun bool) (bool, error) {
	p := filepath.Join(resDir, fd.Name)
	fi, err := os.Lstat(p)
	if err != nil {
		if !os.IsNotExist(err) {
			return false, err
		}
		if dryRun {
			return true, nil
		}
		return true, os.MkdirAll(p, fd.Perm|DEFAULT_DIR_PERM)
	}
	if !fi.IsDir() {
		return false, errors.New("Cannot dir. File/symlink already exist at the path.")
	}
	if (fi.Mode() & 0300) != 0300 {
		return false, errors.New("Directory is not writable.")
	}
	return false, nil
}

func restoreSymlink(fd *FileData, resDir string, dryRun bool) (bool, error) {
	p := filepath.Join(resDir, fd.Name)
	fi, err := os.Lstat(p)
	if err != nil {
		if !os.IsNotExist(err) {
			return false, err
		}
		if dryRun {
			return true, nil
		}
		return true, os.Symlink(fd.Target, p)
	}
	if !isSymlink(fi) {
		return false, errors.New("Cannot restore symlink. File/dir already exist at the path.")
	}
	target, err := os.Readlink(p)
	if err != nil {
		return false, err
	}
	if target != fd.Target {
		return false, errors.New("Cannot restore symlink. Existing symlink points to wrong target.")
	}
	return false, nil
}

func restoreFile(fd *FileData, cm *CMgr, mem *readChunkMem, resDir string, merge, verifyOnly, dryRun bool, secret []byte) (bool, error) {
	p := filepath.Join(resDir, fd.Name)
	if merge {
		if fi, err := os.Lstat(p); err == nil && fi.Size() == fd.Size && fi.ModTime().Equal(fd.ModTime) {
			return false, nil
		}
	}
	if dryRun {
		return true, nil
	}
	tp := p + RESTORE_TEMP_SUFFIX
	err := restoreFileToTemp(fd, cm, mem, tp, verifyOnly, secret)
	if err == nil {
		if verifyOnly {
			return true, nil
		}
		err = os.Chtimes(tp, fd.ModTime, fd.ModTime)
		if err == nil {
			err = os.Chmod(tp, fd.Perm)
			if err == nil {
				err = os.Rename(tp, p)
				if err == nil {
					return true, nil
				}
			}
		}
	}
	if tp != "" {
		os.Remove(tp)
	}
	return false, err
}

func setup(repo string, pwFile string) (*VMgr, *CMgr, *Config, error) {
	sm, repo2 := GetStorageMgr(repo)
	cfg, err := GetConfig(pwFile, sm, repo2)
	if err != nil {
		return nil, nil, nil, err
	}
	vm := MakeVMgr(sm, repo2, cfg.EncryptionKey)
	cm := MakeCMgr(sm, repo2, cfg.EncryptionKey, cfg.Compress)
	return vm, cm, cfg, nil
}

//---------------------------------------------------------------------------

func InitRepo(pwFile, repo string, chunkSize int32, iterations int, compress CompressionMode) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	sm, repo2 := GetStorageMgr(repo)
	files, err := sm.LsDir(repo2)
	if !os.IsNotExist(err) && len(files) != 0 {
		return fmt.Errorf("Backup repository already exists: %s", repo)
	}
	err = sm.MkdirAll(repo2)
	if err != nil {
		return fmt.Errorf("Cannot create repo dir: %s", err)
	}
	cfg := &Config{ChunkSize: chunkSize, Compress: compress}
	err = WriteNewConfig(pwFile, sm, repo2, iterations, cfg)
	if err != nil {
		return fmt.Errorf("Cannot write encypted config file: %s", err)
	}
	return err
}

type BackupStats struct {
	Version         string
	Dirs            int
	DirsNew         int
	DirsUpdated     int
	DirsRemoved     int
	Files           int
	FilesUpdated    int
	FilesNew        int
	FilesRemoved    int
	Symlinks        int
	SymlinksNew     int
	SymlinksUpdated int
	SymlinksRemoved int
	Errors          int
	Size            int64
	SrcAdded        int64
	RepoAdded       int64
}

func Backup(pwFile, repo, excludeFrom, setVersion string, dryRun, force, checkChunks, verbose bool, lockFile string, maxDop int, srcs []string, stats *BackupStats) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	if len(srcs) == 0 {
		return errors.New("At least one backup src must be specified")
	}
	vm, cm, cfg, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	var sml StorageMgr
	var lockFile2 string
	if lockFile == "" {
		var repo2 string
		sml, repo2 = GetStorageMgr(repo)
		lockFile = sml.JoinPath(repo, LOCK_FILENAME)
		lockFile2 = sml.JoinPath(repo2, LOCK_FILENAME)
	} else {
		sml, lockFile2 = GetStorageMgr(lockFile)
	}
	excludePatterns, err := readExcludeFile(excludeFrom)
	if err != nil {
		return fmt.Errorf("Cannot read exclude-from file: %s", err)
	}
	if err = sml.WriteLockFile(lockFile2); os.IsExist(err) {
		return fmt.Errorf("Repository is locked. Lock file %s exists.", lockFile)
	} else if err != nil {
		return err
	}
	defer sml.RemoveLockFile(lockFile2)
	var new_version string
	if setVersion != "" {
		if _, ok := DecodeVersionTime(setVersion); ok {
			new_version = setVersion
		} else {
			return fmt.Errorf("Invalid version %s", setVersion)
		}
	}
	last_version, err := vm.GetLatestVersion()
	if err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("Cannot read version files: %s", err)
	}
	if new_version == "" {
		new_version = CreateNewVersion(last_version)
	}
	if verbose {
		stdout.Println("Scanning sources...")
	}
	sfdm, errs := scanSrcs(excludePatterns, srcs)
	stats.Errors += errs
	if len(sfdm.names) == 0 {
		return errors.New("Nothing to back up.")
	}
	var vfdm = &fileDataMap{}
	vfdm.Init()
	if last_version != "" {
		fds, err, errs := vm.LoadFiles(last_version)
		if err != nil {
			return fmt.Errorf("Failed reading previous version: %s", err)
		}
		stats.Errors += errs
		for _, fd := range fds {
			if !vfdm.AddItem(fd) {
				stderr.Printf("Ignoring duplicate item in version file: %s.\n", fd.Name)
			}
		}
	}
	comb := append(append([]string(nil), vfdm.names...), sfdm.names...)
	sort.Strings(comb)
	if verbose {
		if last_version == "" {
			stdout.Println("Starting inital backup...")
		} else {
			stdout.Printf("Starting backup from last version %s ...", last_version)
		}
	}
	//cm.CacheChunkInfo()
	var last string
	var fds []*FileData
	var wg sync.WaitGroup
	var mu sync.Mutex // protect fds and stats
	ch := make(chan *addChunkMem, maxDop)
	for i := 0; i < maxDop; i++ {
		ch <- makeAddChunkMem(int(cfg.ChunkSize))
	}
	for _, n := range comb {
		if n == last {
			continue
		}
		last = n
		wg.Add(1)
		go func(name string) {
			defer wg.Done()
			mem := <-ch
			defer func() { ch <- mem }()
			new_fd, err := backupOneNode(cm, mem, dryRun, force, checkChunks, verbose, vfdm.files[name], sfdm.files[name], cfg.FPSecret, &mu, stats)
			if err == nil && new_fd != nil {
				mu.Lock()
				fds = append(fds, new_fd)
				mu.Unlock()
			}
		}(n)
	}
	wg.Wait()
	for i := 0; i < maxDop; i++ {
		<-ch
	}
	ch = nil
	if !dryRun {
		if err = vm.SaveFiles(new_version, fds); err != nil {
			return err
		}
		stats.Version = new_version
	}
	return nil
}

func Restore(pwFile, repo, resDir, version string, merge, verifyOnly, dryRun, verbose bool, maxDop int, patterns []string) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	if resDir == "" && !verifyOnly {
		return errors.New("Target must be specified.")
	}
	vm, cm, cfg, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	if !merge {
		if _, err := os.Lstat(resDir); !os.IsNotExist(err) {
			return fmt.Errorf("Restore dir %s already exists", resDir)
		}
	}
	if version == "" {
		version, err = vm.GetLatestVersion()
		if err != nil {
			return fmt.Errorf("Cannot read version files: %s", err)
		}
		if version == "" {
			return errors.New("Error! Nothing to restore. Repository is empty")
		}
	}
	allFiles, err, errs := vm.LoadFiles(version)
	if err != nil {
		return fmt.Errorf("Cannot read version file: %s", err)
	}
	fdm := make(map[string]*FileData)
	var names []string
	for _, fd := range allFiles {
		if matchRestorePatterns(fd.Name, patterns) {
			fdm[fd.Name] = fd
			names = append(names, fd.Name)
		}
	}
	sort.Strings(names)
	allFiles = nil
	for _, name := range names {
		fd := fdm[name]
		if fd.IsDir() || fd.IsSymlink() {
			var acted = true
			if !verifyOnly {
				if fd.IsDir() {
					acted, err = restoreDir(fd, resDir, dryRun)
				} else {
					acted, err = restoreSymlink(fd, resDir, dryRun)
				}
			}
			if err == nil {
				if verbose && acted {
					stdout.Printf("%s\n", fd.PrettyPrint())
				}
			} else {
				stderr.Printf("F %s: %s\n", fd.PrettyPrint(), err)
				errs++
			}
		}
	}
	var wg sync.WaitGroup
	var mu sync.Mutex // protects errs
	ch := make(chan *readChunkMem, maxDop)
	for i := 0; i < maxDop; i++ {
		ch <- &readChunkMem{}
	}
	for _, name := range names {
		fd := fdm[name]
		if fd.IsFile() {
			wg.Add(1)
			go func(fd *FileData) {
				defer wg.Done()
				mem := <-ch
				defer func() { ch <- mem }()
				acted, e := restoreFile(fd, cm, mem, resDir, merge, verifyOnly, dryRun, cfg.FPSecret)
				if e == nil {
					if verbose && acted {
						stdout.Printf("%s\n", fd.PrettyPrint())
					}
				} else {
					stderr.Printf("F %s: %s\n", fd.PrettyPrint(), e)
					mu.Lock()
					errs++
					mu.Unlock()
				}
			}(fd)
		}
	}
	wg.Wait()
	for i := 0; i < maxDop; i++ {
		<-ch
	}
	if !dryRun && !verifyOnly {
		for i := len(names) - 1; i >= 0; i-- {
			fd := fdm[names[i]]
			if fd.IsDir() {
				fi, err := os.Lstat(filepath.Join(resDir, fd.Name))
				if err != nil || fi.Mode() != fd.Perm {
					err = os.Chmod(filepath.Join(resDir, fd.Name), fd.Perm)
					if err != nil {
						stderr.Printf("F %s: %s\n", fd.PrettyPrint(), err)
						errs++
					}
				}
			}
		}
	}
	if errs > 0 {
		return errors.New("Errors occured during restore. Some files were not restored.")
	}
	return nil
}

func Ls(pwFile, repo, version string) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	vm, _, _, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	if version == "" {
		version, err = vm.GetLatestVersion()
	}
	if err != nil {
		return fmt.Errorf("Cannot read version files: %s", err)
	} else if version == "" {
		return nil
	}
	fds, err, errs := vm.LoadFiles(version)
	if err != nil {
		return fmt.Errorf("Cannot read version file: %s", err)
	}
	var names []string
	fdm := make(map[string]*FileData)
	for _, fd := range fds {
		fdm[fd.Name] = fd
		names = append(names, fd.Name)
	}
	sort.Strings(names)
	for _, n := range names {
		fd := fdm[n]
		stdout.Printf("%s\n", fd.PrettyPrint())
	}
	if errs > 0 {
		return errors.New("Error! Some file info were invalid.")
	}
	return nil
}

func Versions(pwFile, repo string) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	vm, _, _, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	versions, err := vm.GetVersions()
	if err != nil {
		return fmt.Errorf("Cannot read version files: %s", err)
	}
	for _, v := range versions {
		stdout.Printf("%s\n", v)
	}
	return nil
}

func DeleteVersion(pwFile, repo, version string) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	if version == "" {
		return errors.New("Version must be specified.")
	}
	vm, _, _, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	err = vm.DeleteVersion(version)
	if err != nil {
		return fmt.Errorf("Cannot delete version %s: %s", version, err)
	}
	return nil
}

func DeleteOldVersions(pwFile, repo string, dryRun bool) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	vm, _, _, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	versions, err := vm.GetVersions()
	if err != nil {
		return fmt.Errorf("Cannot read version files: %s", err)
	}
	sort.Strings(versions)
	d := ReduceVersions(time.Now(), versions)
	for _, v := range d {
		stdout.Printf("Deleting version %s\n", v)
		if !dryRun {
			err = vm.DeleteVersion(v)
			if err != nil {
				return fmt.Errorf("Cannot delete version %s: %s", v, err)
			}
		}
	}
	return nil
}

type VerifyRepoResults struct {
	Chunks, Ok, Errors, Missing, Unused int
}

func VerifyRepo(pwFile, repo string, quick bool, maxDop int, r *VerifyRepoResults) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	vm, cm, cfg, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	versions, err := vm.GetVersions()
	if err != nil {
		return fmt.Errorf("Cannot read version files: %s", err)
	}
	allChunks := cm.GetAllChunks()
	allOk := make(map[FP]bool)
	allErrors := make(map[FP]bool)
	allMissing := make(map[FP]bool)
	fail := false
	totalFiles := 0
	totalBadFiles := 0
	totalDirs := 0
	totalSymlinks := 0
	var wg sync.WaitGroup
	var mu sync.Mutex // protects vr, allOk, allErrors, AllMissing
	ch := make(chan *readChunkMem, maxDop)
	for i := 0; i < maxDop; i++ {
		ch <- &readChunkMem{}
	}
	sort.Sort(sort.Reverse(sort.StringSlice(versions)))
	for _, v := range versions {
		fds, err, nerrs := vm.LoadFiles(v)
		if err != nil {
			stderr.Printf("Failed to read version %s : %s\n", v, err)
			fail = true
			continue
		}
		if nerrs > 0 {
			fail = true
		}
		vChunks := make(map[FP]int32)
		for _, fd := range fds {
			for i, chunk := range fd.Chunks {
				vChunks[chunk] = fd.Sizes[i]
			}
		}
		var vr VerifyRepoResults
		vr.Chunks = len(vChunks)
		var size int64
		mu.Lock()
		for chunk, chunkSize := range vChunks {
			size = size + int64(chunkSize)
			if allOk[chunk] {
				vr.Ok++
			} else if allErrors[chunk] {
				vr.Errors++
			} else if allMissing[chunk] {
				vr.Missing++
			} else if quick {
				if cm.FindChunk(chunk) {
					vr.Ok++
					allOk[chunk] = true
				} else {
					stderr.Printf("Missing chunk %s\n", chunk)
					vr.Missing++
					allMissing[chunk] = true
				}
			} else {
				wg.Add(1)
				go func(cc FP) {
					defer wg.Done()
					mem := <-ch
					defer func() { ch <- mem }()
					b, err := cm.ReadChunk(cc, mem)
					if err == nil && !matchChunkFP(cfg.FPSecret, cc, b) {
						err = fmt.Errorf("Mismatch fingerpint %s", cc)
					}
					if err != nil {
						if os.IsNotExist(err) {
							stderr.Printf("Missing chunk %s\n", cc)
							mu.Lock()
							vr.Missing++
							allMissing[cc] = true
							mu.Unlock()
						} else {
							stderr.Printf("Error chunk %s\n", cc)
							mu.Lock()
							vr.Errors++
							allErrors[cc] = true
							mu.Unlock()
						}
					} else {
						mu.Lock()
						vr.Ok++
						allOk[cc] = true
						mu.Unlock()
					}
				}(chunk)
			}
		}
		mu.Unlock()
		wg.Wait()
		badFiles := 0
		files := 0
		dirs := 0
		symlinks := 0
		for _, fd := range fds {
			if fd.IsFile() {
				files++
				for _, chunk := range fd.Chunks {
					if allErrors[chunk] || allMissing[chunk] {
						badFiles++
						stderr.Printf("F %s\n", fd.Name)
						break
					}
				}
			} else if fd.IsDir() {
				dirs++
			} else {
				symlinks++
			}
		}
		totalFiles += files
		totalBadFiles += badFiles
		totalDirs += dirs
		totalSymlinks += symlinks
		nerrsMsg := ""
		if nerrs > 0 {
			nerrsMsg = fmt.Sprintf(" %d invalid file info.\n", nerrs)
		}
		chunkMsg := ""
		if !quick {
			chunkMsg = fmt.Sprintf("%d good, %d bad, ", vr.Ok, vr.Errors)
		}
		stdout.Printf("Version %s : %d bytes, %d chunk(s), %s%d missing. %d files, %d bad. %d dirs. %d symlinks.%s", v, size, vr.Chunks, chunkMsg, vr.Missing, files, badFiles, dirs, symlinks, nerrsMsg)
	}
	for i := 0; i < maxDop; i++ {
		<-ch
	}
	r.Chunks = len(allChunks)
	r.Ok = len(allOk)
	r.Errors = len(allErrors)
	r.Missing = len(allMissing)
	for chunk, _ := range allChunks {
		if !(allOk[chunk] || allErrors[chunk] || allMissing[chunk]) {
			r.Unused++
		}
	}
	chunkMsg := ""
	if !quick {
		chunkMsg = fmt.Sprintf("%d good, %d bad, ", r.Ok, r.Errors)
	}
	stdout.Printf("Summary: %d chunk(s), %s%d missing, %d unused. %d files, %d bad. %d dirs. %d symlinks.\n", r.Chunks, chunkMsg, r.Missing, r.Unused, totalFiles, totalBadFiles, totalDirs, totalSymlinks)
	if fail || r.Errors > 0 || r.Missing > 0 {
		return errors.New("Error verifying repo.")
	}
	return nil
}

func PurgeUnused(pwFile, repo string, dryRun, verbose bool) error {
	if repo == "" {
		return errors.New("Backup repository must be specified.")
	}
	vm, cm, _, err := setup(repo, pwFile)
	if err != nil {
		return err
	}
	versions, err := vm.GetVersions()
	if err != nil {
		return fmt.Errorf("Cannot read version files: %s", err)
	}
	counts := cm.GetAllChunks()
	total_chunks := len(counts)
	for _, v := range versions {
		fds, err, errs := vm.LoadFiles(v)
		if err != nil {
			return fmt.Errorf("Cannot read version file: %s", err)
		}
		if errs > 0 {
			return fmt.Errorf("Error! Some file info were invalid in version %s", v)
		}
		for _, fd := range fds {
			for _, chunk := range fd.Chunks {
				delete(counts, chunk)
			}
		}
	}
	numDeleted := 0
	numFailed := 0
	for chunk, _ := range counts {
		if dryRun {
			if verbose {
				stdout.Printf("Delete %s\n", chunk)
			}
			numDeleted++
		} else {
			if err := cm.DeleteChunk(chunk); err != nil {
				numFailed++
				if verbose {
					stdout.Printf("Failed to delete %s: %s\n", chunk, err)
				}
			} else {
				numDeleted++
				if verbose {
					stdout.Printf("Deleted %s\n", chunk)
				}
			}
		}
	}
	if dryRun {
		stdout.Printf("Chunks to be purged (dryrun): %d out of %d.\n", numDeleted, total_chunks)
	} else {
		stdout.Printf("Chunks purged: %d out of %d.\n", numDeleted, total_chunks)
	}
	if numFailed > 0 {
		return fmt.Errorf("Failed to purge %d chunk(s).", numFailed)
	}
	return nil
}

func RemoveLock(repo, lockFile string) error {
	var sml StorageMgr
	var lockFile2 string
	if lockFile == "" {
		var repo2 string
		sml, repo2 = GetStorageMgr(repo)
		lockFile2 = sml.JoinPath(repo2, LOCK_FILENAME)
	} else {
		sml, lockFile2 = GetStorageMgr(lockFile)
	}
	err := sml.RemoveLockFile(lockFile2)
	if os.IsNotExist(err) {
		return fmt.Errorf("Repo is not locked. Lock file %s does not exist", lockFile2)
	}
	return nil
}


text:
require_relative '../test_helper'

class CmsPageTest < ActiveSupport::TestCase

  def test_fixtures_validity
    Comfy::Cms::Page.all.each do |page|
      assert page.valid?, page.errors.full_messages.to_s
      assert_equal page.content_cache, page.render
    end
  end

  def test_validations
    page = Comfy::Cms::Page.new
    page.save
    assert page.invalid?
    assert_has_errors_on page, :site_id, :layout, :slug, :label
  end

  def test_validation_of_parent_presence
    page = comfy_cms_sites(:default).pages.new(new_params)
    assert !page.parent
    assert page.valid?, page.errors.full_messages.to_s
    assert_equal comfy_cms_pages(:default), page.parent
  end

  def test_validation_of_parent_relationship
    page = comfy_cms_pages(:default)
    assert !page.parent
    page.parent = page
    assert page.invalid?
    assert_has_errors_on page, :parent_id
    page.parent = comfy_cms_pages(:child)
    assert page.invalid?
    assert_has_errors_on page, :parent_id
  end

  def test_validation_of_target_page
    page = comfy_cms_pages(:child)
    page.target_page = comfy_cms_pages(:default)
    page.save!
    assert_equal comfy_cms_pages(:default), page.target_page
    page.target_page = page
    assert page.invalid?
    assert_has_errors_on page, :target_page_id
  end

  def test_validation_of_slug
    page = comfy_cms_pages(:child)
    page.slug = 'slug.with.d0ts-and_things'
    assert page.valid?

    page.slug = 'inva lid'
    assert page.invalid?

    page.slug = 'acción'
    assert page.valid?
  end

  def test_validation_of_slug_allows_unicode_accent_characters
    page = comfy_cms_pages(:child)
    thai_character_ko_kai = "\u0e01"
    thai_character_mai_tho = "\u0E49"
    page.slug = thai_character_ko_kai + thai_character_mai_tho
    assert page.valid?
  end

  def test_label_assignment
    page = comfy_cms_sites(:default).pages.new(
      :slug   => 'test',
      :parent => comfy_cms_pages(:default),
      :layout => comfy_cms_layouts(:default)
    )
    assert page.valid?
    assert_equal 'Test', page.label
  end

  def test_creation
    assert_difference ['Comfy::Cms::Page.count', 'Comfy::Cms::Block.count'] do
      page = comfy_cms_sites(:default).pages.create!(
        :label  => 'test',
        :slug   => 'test',
        :parent => comfy_cms_pages(:default),
        :layout => comfy_cms_layouts(:default),
        :blocks_attributes => [
          { :identifier => 'default_page_text',
            :content    => 'test' }
        ]
      )
      assert page.is_published?
      assert_equal 1, page.position
    end
  end

  def test_initialization_of_full_path
    page = Comfy::Cms::Page.new
    assert_equal '/', page.full_path

    page = Comfy::Cms::Page.new(new_params)
    assert page.invalid?
    assert_has_errors_on page, :site_id

    page = comfy_cms_sites(:default).pages.new(new_params(:parent => comfy_cms_pages(:default)))
    assert page.valid?
    assert_equal '/test-page', page.full_path

    page = comfy_cms_sites(:default).pages.new(new_params(:parent => comfy_cms_pages(:child)))
    assert page.valid?
    assert_equal '/child-page/test-page', page.full_path

    Comfy::Cms::Page.destroy_all
    page = comfy_cms_sites(:default).pages.new(new_params)
    assert page.valid?
    assert_equal '/', page.full_path
  end

  def test_sync_child_pages
    page = comfy_cms_pages(:child)
    page_1 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page, :slug => 'test-page-1'))
    page_2 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page, :slug => 'test-page-2'))
    page_3 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page_2, :slug => 'test-page-3'))
    page_4 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page_1, :slug => 'test-page-4'))
    assert_equal '/child-page/test-page-1', page_1.full_path
    assert_equal '/child-page/test-page-2', page_2.full_path
    assert_equal '/child-page/test-page-2/test-page-3', page_3.full_path
    assert_equal '/child-page/test-page-1/test-page-4', page_4.full_path

    page.update_attributes!(:slug => 'updated-page')
    assert_equal '/updated-page', page.full_path
    page_1.reload; page_2.reload; page_3.reload; page_4.reload
    assert_equal '/updated-page/test-page-1', page_1.full_path
    assert_equal '/updated-page/test-page-2', page_2.full_path
    assert_equal '/updated-page/test-page-2/test-page-3', page_3.full_path
    assert_equal '/updated-page/test-page-1/test-page-4', page_4.full_path

    page_2.update_attributes!(:parent => page_1)
    page_1.reload; page_2.reload; page_3.reload; page_4.reload
    assert_equal '/updated-page/test-page-1', page_1.full_path
    assert_equal '/updated-page/test-page-1/test-page-2', page_2.full_path
    assert_equal '/updated-page/test-page-1/test-page-2/test-page-3', page_3.full_path
    assert_equal '/updated-page/test-page-1/test-page-4', page_4.full_path
  end

  def test_children_count_updating
    page_1 = comfy_cms_pages(:default)
    page_2 = comfy_cms_pages(:child)
    assert_equal 1, page_1.children_count
    assert_equal 0, page_2.children_count

    page_3 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page_2))
    page_1.reload; page_2.reload
    assert_equal 1, page_1.children_count
    assert_equal 1, page_2.children_count
    assert_equal 0, page_3.children_count

    page_3.update_attributes!(:parent => page_1)
    page_1.reload; page_2.reload
    assert_equal 2, page_1.children_count
    assert_equal 0, page_2.children_count

    page_3.destroy
    page_1.reload; page_2.reload
    assert_equal 1, page_1.children_count
    assert_equal 0, page_2.children_count
  end

  def test_cascading_destroy
    assert_difference 'Comfy::Cms::Page.count', -2 do
      assert_difference 'Comfy::Cms::Block.count', -2 do
        comfy_cms_pages(:default).destroy
      end
    end
  end

  def test_options_for_select
    assert_equal ['Default Page', '. . Child Page'],
      Comfy::Cms::Page.options_for_select(comfy_cms_sites(:default)).collect{|t| t.first }
    assert_equal ['Default Page'],
      Comfy::Cms::Page.options_for_select(comfy_cms_sites(:default), comfy_cms_pages(:child)).collect{|t| t.first }
    assert_equal [],
      Comfy::Cms::Page.options_for_select(comfy_cms_sites(:default), comfy_cms_pages(:default))

    page = Comfy::Cms::Page.new(new_params(:parent => comfy_cms_pages(:default)))
    assert_equal ['Default Page', '. . Child Page'],
      Comfy::Cms::Page.options_for_select(comfy_cms_sites(:default), page).collect{|t| t.first }
  end

  def test_comfy_cms_blocks_attributes_accessor
    page = comfy_cms_pages(:default)
    assert_equal page.blocks.count, page.blocks_attributes.size
    assert_equal 'default_field_text', page.blocks_attributes.first[:identifier]
    assert_equal 'default_field_text_content', page.blocks_attributes.first[:content]
  end

  def test_content_caching
    page = comfy_cms_pages(:default)
    assert_equal page.content_cache, page.render

    page.update_columns(:content_cache => 'Old Content')
    refute_equal page.content_cache, page.render

    page.clear_content_cache!
    assert_equal page.content_cache, page.render
  end

  def test_content_cache_clear_on_save
    page = comfy_cms_pages(:default)
    old_content = 'Old Content'
    page.update_columns(:content_cache => old_content)

    page.save!
    refute_equal old_content, page.content_cache
  end

  def test_scope_published
    assert_equal 2, Comfy::Cms::Page.published.count
    comfy_cms_pages(:child).update_columns(:is_published => false)
    assert_equal 1, Comfy::Cms::Page.published.count
  end

  def test_root?
    assert comfy_cms_pages(:default).root?
    assert !comfy_cms_pages(:child).root?
  end

  def test_url
    site = comfy_cms_sites(:default)

    assert_equal '//test.host/', comfy_cms_pages(:default).url
    assert_equal '//test.host/child-page', comfy_cms_pages(:child).url

    site.update_columns(:path => '/en/site')
    comfy_cms_pages(:default).reload
    comfy_cms_pages(:child).reload

    assert_equal '//test.host/en/site/', comfy_cms_pages(:default).url
    assert_equal '//test.host/en/site/child-page', comfy_cms_pages(:child).url
  end

  def test_unicode_slug_escaping
    page = comfy_cms_pages(:child)
    page_1 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page, :slug => 'tést-ünicode-slug'))
    assert_equal CGI::escape('tést-ünicode-slug'), page_1.slug
    assert_equal CGI::escape('/child-page/tést-ünicode-slug').gsub('%2F', '/'), page_1.full_path
  end

  def test_unicode_slug_unescaping
    page = comfy_cms_pages(:child)
    page_1 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page, :slug => 'tést-ünicode-slug'))
    found_page = comfy_cms_sites(:default).pages.where(:slug => CGI::escape('tést-ünicode-slug')).first
    assert_equal 'tést-ünicode-slug', found_page.slug
    assert_equal '/child-page/tést-ünicode-slug', found_page.full_path
  end

  def test_identifier
    assert_equal 'index',       comfy_cms_pages(:default).identifier
    assert_equal 'child-page',  comfy_cms_pages(:child).identifier

    comfy_cms_pages(:default).update_column(:slug, 'index')
    assert_equal 'index', comfy_cms_pages(:default).identifier
  end

  def test_children_count_updating_on_move
    page_1 = comfy_cms_pages(:default)
    page_2 = comfy_cms_pages(:child)
    page_3 = comfy_cms_sites(:default).pages.create!(new_params(:parent => page_2))

    page_2.reload

    assert_equal 1, page_1.children_count
    assert_equal 1, page_2.children_count
    assert_equal 0, page_3.children_count

    page_3.parent_id = page_1.id
    page_3.save!

    page_1.reload; page_2.reload; page_3.reload

    assert_equal 2, page_1.children_count
    assert_equal 0, page_2.children_count
    assert_equal 0, page_3.children_count
  end

protected

  def new_params(options = {})
    {
      :label  => 'Test Page',
      :slug   => 'test-page',
      :layout => comfy_cms_layouts(:default)
    }.merge(options)
  end
end


text:
/**
 * @module lib/regexp
 */

"use strict";

function end_module() {
    Object.freeze(__RegExp__proto__.data);
    Object.freeze(__RegExp__.data);

    module.exports = Object.freeze({
        "__RegExp__proto__": __RegExp__proto__,
        "__RegExp__"       : __RegExp__,
        "__splitRE__"      : __splitRE__,
        "__replaceRE__"    : __replaceRE__,
        "__replaceREWith__": __replaceREWith__
    });
}

var core         = require("../core.js"),
    Value        = core.Value,
    DataType     = core.DataType,
    calcTailCall = core.calcTailCall,
    __unit__     = core.__unit__,
    __true__     = core.__true__,
    __false__    = core.__false__;

var errors = require("../errors.js");

var utils         = require("./utils.js"),
    assertType    = utils.assertType,
    createObject  = utils.createObject,
    readProperty  = utils.readProperty,
    writeProperty = utils.writeProperty,
    readInternalProperty  = utils.readInternalProperty,
    writeInternalProperty = utils.writeInternalProperty;

var module_object = require("./object.js");

var INTERNAL_KEY = "__regexp__";

function assertRegExp(obj) {
    if (!(obj instanceof RegExp)) {
        throw new errors.RuntimeError([], "invalid object");
    }
}

/**
 * @static
 */
var __RegExp__proto__ = createObject(module_object.__Object__proto__);
Object.defineProperties(__RegExp__proto__.data, {
    "toString": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return new Value(
                    DataType.STRING,
                    regexp.source
                );
            }
        )
    },
    "global": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return regexp.global ? __true__ : __false__;
            }
        )
    },
    "ignoreCase": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return regexp.ignoreCase ? __true__ : __false__;
            }
        )
    },
    "multiline": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return regexp.multiline ? __true__ : __false__;
            }
        )
    },
    "lastIndex": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return new Value(
                    DataType.NUMBER,
                    regexp.lastIndex
                );
            }
        )
    },
    "setLastIndex": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return new Value(
                    DataType.FUNCTION,
                    function (index) {
                        assertType(index, DataType.NUMBER);
                        regexp.lastIndex = index.data;
                        return __unit__;
                    }
                );
            }
        )
    },
    "exec": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return new Value(
                    DataType.FUNCTION,
                    function (str) {
                        assertType(str, DataType.STRING);
                        var res = regexp.exec(str.data);
                        if (res) {
                            var resObj = createObject(module_object.__Object__proto__);
                            writeProperty(resObj, "index",
                                new Value(
                                    DataType.NUMBER,
                                    res.index
                                )
                            );
                            writeProperty(resObj, "input",
                                new Value(
                                    DataType.STRING,
                                    res.input
                                )
                            );
                            writeProperty(resObj, "substrings",
                                new Value(
                                    DataType.ARRAY,
                                    res.map(function (substr) {
                                        return new Value(
                                            DataType.STRING,
                                            substr
                                        );
                                    })
                                )
                            );
                            return resObj;
                        }
                        else {
                            return __unit__;
                        }
                    }
                );
            }
        )
    },
    "test": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                var regexp = readInternalProperty(obj, INTERNAL_KEY, "regexp");
                assertRegExp(regexp);
                return new Value(
                    DataType.FUNCTION,
                    function (str) {
                        assertType(str, DataType.STRING);
                        return regexp.test(str.data) ? __true__ : __false__;
                    }
                )
            }
        )
    }
});

/**
 * @static
 */
var __RegExp__ = createObject(module_object.__Class__proto__);
Object.defineProperties(__RegExp__.data, {
    "proto": {
        "value": __RegExp__proto__
    },
    "new": {
        "value": new Value(
            DataType.FUNCTION,
            function (cls) {
                assertType(cls, DataType.OBJECT);
                return new Value(
                    DataType.FUNCTION,
                    function (pattern) {
                        assertType(pattern, DataType.STRING);
                        return new Value(
                            DataType.FUNCTION,
                            function (flags) {
                                assertType(flags, DataType.STRING);
                                var f = calcTailCall(
                                    readProperty(module_object.__Class__proto__, "new").data(cls)
                                );
                                assertType(f, DataType.FUNCTION);
                                var g = calcTailCall(f.data(pattern));
                                assertType(g, DataType.FUNCTION);
                                return g.data(flags);
                            }
                        );
                    }
                );
            }
        )
    },
    "ctor": {
        "value": new Value(
            DataType.FUNCTION,
            function (obj) {
                assertType(obj, DataType.OBJECT);
                return new Value(
                    DataType.FUNCTION,
                    function (pattern) {
                        assertType(pattern, DataType.STRING);
                        return new Value(
                            DataType.FUNCTION,
                            function (flags) {
                                assertType(flags, DataType.STRING);
                                writeInternalProperty(obj, INTERNAL_KEY, "regexp",
                                    new RegExp(pattern.data, flags.data));
                                return obj;
                            }
                        );
                    }
                );
            }
        )
    }
});

/**
 * @static
 */
var __splitRE__ =
    new Value(
        DataType.FUNCTION,
        function (str) {
            assertType(str, DataType.STRING);
            return new Value(
                DataType.FUNCTION,
                function (re) {
                    assertType(re, DataType.OBJECT);
                    var regexp = readInternalProperty(re, INTERNAL_KEY, "regexp");
                    assertRegExp(regexp);
                    return new Value(
                        DataType.ARRAY,
                        str.data.split(regexp).map(function (elem) {
                            return new Value(
                                DataType.STRING,
                                elem
                            );
                        })
                    );
                }
            );
        }
    );

/**
 * @static
 */
var __replaceRE__ =
    new Value(
        DataType.FUNCTION,
        function (str) {
            assertType(str, DataType.STRING);
            return new Value(
                DataType.FUNCTION,
                function (re) {
                    assertType(re, DataType.OBJECT);
                    var regexp = readInternalProperty(re, INTERNAL_KEY, "regexp");
                    assertRegExp(regexp);
                    return new Value(
                        DataType.FUNCTION,
                        function (newSubstr) {
                            assertType(newSubstr, DataType.STRING);
                            return new Value(
                                DataType.STRING,
                                str.data.replace(regexp, newSubstr.data)
                            );
                        }
                    );
                }
            );
        }
    );

/**
 * @static
 */
var __replaceREWith__ =
    new Value(
        DataType.FUNCTION,
        function (str) {
            assertType(str, DataType.STRING);
            return new Value(
                DataType.FUNCTION,
                function (re) {
                    assertType(re, DataType.OBJECT);
                    var regexp = readInternalProperty(re, INTERNAL_KEY, "regexp");
                    assertRegExp(regexp);
                    return new Value(
                        DataType.FUNCTION,
                        function (f) {
                            assertType(f, DataType.FUNCTION);
                            return new Value(
                                DataType.STRING,
                                str.data.replace(regexp, function (rep) {
                                    var newSubstr = calcTailCall(f.data(
                                            new Value(DataType.STRING, rep)
                                        ));
                                    assertType(newSubstr, DataType.STRING);
                                    return newSubstr.data;
                                })
                            );
                        }
                    );
                }
            );
        }
    );

end_module();


text:
class ExtendedContentAuthorizer < ContentAuthorizer
  def self.creatable_by?(user)
    return false unless user.present?
    return false if ENV.key?('CONTENT_BLACKLIST') && ENV['CONTENT_BLACKLIST'].split(',').include?(user.email)

    return true if PermissionService.billing_plan_allows_extended_content?(user: user)
    return true if PermissionService.user_can_collaborate_in_universe_that_allows_extended_content?(user: user)
    return true if user.active_promo_codes.any?
  end
end


text:
<?php

/**
 * CheetahO API php library
 * @author CheetahO
 *
 */
class CheetahO
{
    protected $auth = array();

    public function __construct($key = '')
    {
        $this->auth = array(
                "key" => $key
        );
    }

    /**
     *
     * @param array $opts
     * @return multitype:boolean string
     */
    public function url($params = array())
    {
        $data = json_encode(array_merge($this->auth, $params));
        $response = self::request($data, 'http://app.cheetaho.com/api/v1/media');

        return $response;
    }

    /**
     *
     * @param unknown $data
     * @param unknown $url
     * @return multitype:boolean string
     */
    private function request($data, $url)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36");
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_FAILONERROR, 0);

        $response = json_decode(curl_exec($curl), true);

        if ($response === null) {

            $response = array('data' => array(
                "error" => array('message' => 'cURL Error: ' . curl_error($curl)))
            );
        }

        curl_close($curl);

        return $response;
    }
}


text:
#ifndef _SPEAR_COMMON_H
#define _SPEAR_COMMON_H
/*
 * Common configurations used for both spear3xx as well as spear6xx
 */

/* U-Boot Load Address */
#define CONFIG_SYS_TEXT_BASE			0x00700000

/* Ethernet driver configuration */
#define CONFIG_MII
#define CONFIG_PHY_RESET_DELAY			10000		/* in usec */
#define CONFIG_PHY_GIGE			/* Include GbE speed/duplex detection */

/* USBD driver configuration */
#if defined(CONFIG_SPEAR_USBTTY)
#define CONFIG_DW_UDC
#define CONFIG_USB_DEVICE
#define CONFIG_USBD_HS
#define CONFIG_USB_TTY

#define CONFIG_USBD_PRODUCT_NAME		"SPEAr SoC"
#define CONFIG_USBD_MANUFACTURER		"ST Microelectronics"

#endif

#define CONFIG_EXTRA_ENV_USBTTY			"usbtty=cdc_acm\0"

/* I2C driver configuration */
#define CONFIG_SYS_I2C
#if defined(CONFIG_SPEAR600)
#define CONFIG_SYS_I2C_BASE			0xD0200000
#elif defined(CONFIG_SPEAR300)
#define CONFIG_SYS_I2C_BASE			0xD0180000
#elif defined(CONFIG_SPEAR310)
#define CONFIG_SYS_I2C_BASE			0xD0180000
#elif defined(CONFIG_SPEAR320)
#define CONFIG_SYS_I2C_BASE			0xD0180000
#endif
#define CONFIG_SYS_I2C_SPEED			400000
#define CONFIG_SYS_I2C_SLAVE			0x02

#define CONFIG_I2C_CHIPADDRESS			0x50

/* Timer, HZ specific defines */

/* Flash configuration */
#if defined(CONFIG_FLASH_PNOR)
#define CONFIG_SPEAR_EMI
#else
#define CONFIG_ST_SMI
#endif

#if defined(CONFIG_ST_SMI)

#define CONFIG_SYS_MAX_FLASH_BANKS		2
#define CONFIG_SYS_FLASH_BASE			0xF8000000
#define CONFIG_SYS_CS1_FLASH_BASE		0xF9000000
#define CONFIG_SYS_FLASH_BANK_SIZE		0x01000000
#define CONFIG_SYS_FLASH_ADDR_BASE		{CONFIG_SYS_FLASH_BASE, \
						CONFIG_SYS_CS1_FLASH_BASE}
#define CONFIG_SYS_MAX_FLASH_SECT		128

#define CONFIG_SYS_FLASH_ERASE_TOUT		(3 * CONFIG_SYS_HZ)
#define CONFIG_SYS_FLASH_WRITE_TOUT		(3 * CONFIG_SYS_HZ)

#endif

/*
 * Serial Configuration (PL011)
 * CONFIG_PL01x_PORTS is defined in specific files
 */
#define CONFIG_PL011_SERIAL
#define CONFIG_PL011_CLOCK			(48 * 1000 * 1000)
#define CONFIG_CONS_INDEX			0
#define CONFIG_BAUDRATE				115200
#define CONFIG_SYS_BAUDRATE_TABLE		{ 9600, 19200, 38400, \
						57600, 115200 }

#define CONFIG_SYS_LOADS_BAUD_CHANGE

/* NAND FLASH Configuration */
#define CONFIG_SYS_NAND_SELF_INIT
#define CONFIG_MTD_DEVICE
#define CONFIG_MTD_PARTITIONS
#define CONFIG_NAND_FSMC
#define CONFIG_SYS_MAX_NAND_DEVICE		1
#define CONFIG_SYS_NAND_ONFI_DETECTION

/*
 * Command support defines
 */
#define CONFIG_CMD_NAND
#define CONFIG_CMD_ENV
#define CONFIG_CMD_SAVES

/*
 * Default Environment Varible definitions
 */
#define CONFIG_ENV_OVERWRITE

/*
 * U-Boot Environment placing definitions.
 */
#if defined(CONFIG_ENV_IS_IN_FLASH)
#ifdef CONFIG_ST_SMI
/*
 * Environment is in serial NOR flash
 */
#define CONFIG_SYS_MONITOR_LEN			0x00040000
#define CONFIG_ENV_SECT_SIZE			0x00010000
#define CONFIG_FSMTDBLK				"/dev/mtdblock3 "

#define CONFIG_BOOTCOMMAND			"bootm 0xf8050000"

#elif defined(CONFIG_SPEAR_EMI)
/*
 * Environment is in parallel NOR flash
 */
#define CONFIG_SYS_MONITOR_LEN			0x00060000
#define CONFIG_ENV_SECT_SIZE			0x00020000
#define CONFIG_FSMTDBLK				"/dev/mtdblock3 "

#define CONFIG_BOOTCOMMAND			"cp.b 0x50080000 0x1600000 " \
						"0x4C0000; bootm 0x1600000"
#endif

#define CONFIG_ENV_ADDR				(CONFIG_SYS_FLASH_BASE + \
						CONFIG_SYS_MONITOR_LEN)
#elif defined(CONFIG_ENV_IS_IN_NAND)
/*
 * Environment is in NAND
 */

#define CONFIG_ENV_OFFSET			0x60000
#define CONFIG_ENV_RANGE			0x10000
#define CONFIG_FSMTDBLK				"/dev/mtdblock7 "

#define CONFIG_BOOTCOMMAND			"nand read.jffs2 0x1600000 " \
						"0x80000 0x4C0000; " \
						"bootm 0x1600000"
#endif

#define CONFIG_BOOTARGS				"console=ttyAMA0,115200 " \
						"mem=128M " \
						"root="CONFIG_FSMTDBLK \
						"rootfstype=jffs2"

#define CONFIG_NFSBOOTCOMMAND						\
	"bootp; "							\
	"setenv bootargs root=/dev/nfs rw "				\
	"nfsroot=$(serverip):$(rootpath) "				\
	"ip=$(ipaddr):$(serverip):$(gatewayip):"			\
			"$(netmask):$(hostname):$(netdev):off "		\
			"console=ttyAMA0,115200 $(othbootargs);"	\
	"bootm; "

#define CONFIG_RAMBOOTCOMMAND						\
	"setenv bootargs root=/dev/ram rw "				\
		"console=ttyAMA0,115200 $(othbootargs);"		\
	CONFIG_BOOTCOMMAND

#define CONFIG_ENV_SIZE				0x02000
#define CONFIG_SYS_MONITOR_BASE			CONFIG_SYS_TEXT_BASE

/* Miscellaneous configurable options */
#define CONFIG_ARCH_CPU_INIT
#define CONFIG_BOARD_EARLY_INIT_F
#define CONFIG_DISPLAY_CPUINFO
#define CONFIG_BOOT_PARAMS_ADDR			0x00000100
#define CONFIG_CMDLINE_TAG
#define CONFIG_SETUP_MEMORY_TAGS
#define CONFIG_MISC_INIT_R

#define CONFIG_SYS_MEMTEST_START		0x00800000
#define CONFIG_SYS_MEMTEST_END			0x04000000
#define CONFIG_SYS_MALLOC_LEN			(1024*1024)
#define CONFIG_IDENT_STRING			"-SPEAr"
#define CONFIG_SYS_LONGHELP
#define CONFIG_CMDLINE_EDITING
#define CONFIG_SYS_CBSIZE			256
#define CONFIG_SYS_PBSIZE			(CONFIG_SYS_CBSIZE + \
						sizeof(CONFIG_SYS_PROMPT) + 16)
#define CONFIG_SYS_MAXARGS			16
#define CONFIG_SYS_BARGSIZE			CONFIG_SYS_CBSIZE
#define CONFIG_SYS_LOAD_ADDR			0x00800000
#define CONFIG_SYS_CONSOLE_INFO_QUIET

#define CONFIG_SYS_FLASH_EMPTY_INFO

/* Physical Memory Map */
#define CONFIG_NR_DRAM_BANKS			1
#define PHYS_SDRAM_1				0x00000000
#define PHYS_SDRAM_1_MAXSIZE			0x40000000

#define CONFIG_SYS_SDRAM_BASE			PHYS_SDRAM_1
#define CONFIG_SYS_INIT_RAM_ADDR		0xD2800000
#define CONFIG_SYS_INIT_RAM_SIZE		0x2000

#define CONFIG_SYS_INIT_SP_OFFSET		\
	(CONFIG_SYS_INIT_RAM_SIZE - GENERATED_GBL_DATA_SIZE)

#define CONFIG_SYS_INIT_SP_ADDR			\
	(CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_INIT_SP_OFFSET)

#endif


text:
API
===

.. module:: bizdays

:class:`Calendar`
-----------------

.. autoclass:: Calendar
   :members: bizdays, isbizday, offset, seq, getdate, getbizdays, following, preceding, modified_following, modified_preceding, load, diff
   :undoc-members:

options
-------

.. autofunction:: set_option

Options:

- `mode`: accepts `pandas` and `python` (default).
  Pandas mode enable integration with pandas,
  check out :doc:`pandas` for more information.
- `mode.datetype`: specify the date type returned by
  Calendar's methods that return dates (`seq`, `following`, `preceding`, ...).
  Accepts `date` (default), `datetime` and `iso` for ISO formated strings.
  In pandas mode this option is ignored.

.. code-block:: python

   from bizdays import set_option
   set_option('mode', 'pandas')

.. autofunction:: get_option

.. code-block:: python

   from bizdays import get_option
   get_option('mode')


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Chime</title>
    <link rel="stylesheet" href="build/admin-bundle.min.css" />
    <link rel="icon" type="image/png" href="images/favicon.ico">

              <button class="btn btn-navbar dashboard-small dashboard-navbar-button">Log In</button>

              <button class="btn btn-navbar dashboard-small dashboard-navbar-button">Sign Up</button>

Chime is a tablet-based reception solution for the workplace.

New to Chime? Learn more below.

About Chime

Welcome All

The Chime Client provides your office with a way to ensure that every visitor is greeted and assisted.

Best Face Forward

Visitors can see all of your staff and their titles, and select the right contact for their needs.

Touch Base

Your guests can then ping their contact, add their own custom message, and get a confirmation.

Take Control

The Chime Dashboard allows you to manage your organization, its members, and their contact info.

Ready to get started?

              <form class="col-xs-10 col-xs-push-1">

                  <button type="submit" class="btn btn-default dashboard-button-medium-black dashboard-large">Sign Up</button>

              </form>


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/re"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.guoxiaoxing.cloud.music.magicasakura.widgets.TintToolbar
        android:id="@+id/activity_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/theme_color_primary"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/Theme.AppCompat"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/select_del"
        android:layout_below="@+id/activity_main_toolbar" />

    <LinearLayout
        android:id="@+id/select_del"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/selectableItemBackground"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:src="@drawable/btmlay_icn_dlt" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:src="@drawable/btmlay_icn_dlt"
            android:text="删除"
            android:textAlignment="center" />
    </LinearLayout>
</RelativeLayout>


text:
require 'open_jtalk/config'
require 'open_jtalk/version'
require 'open_jtalk/open_jtalk'
require 'open_jtalk/wave_file_writer'
require 'open_jtalk/text_cutter'

begin
  require 'forwardable'
  require 'lame'
  require 'open_jtalk/mp3_file_writer'
  require 'open_jtalk/mp3_synthesizer'
  require 'open_jtalk/mp3_streaming_synthesizer'
rescue LoadError, StandardError => e
end


text:
<?php

declare(strict_types=1);

namespace Vpn\Portal;

use Vpn\Portal\Exception\QrCodeException;

class QrCode
{
    private const QR_ENCODE_PATH = '/usr/bin/qrencode';

    public static function generate(string $qrText): string
    {
        ob_start();
        passthru(
            sprintf(
                '%s -m 0 -s 4 --inline -t SVG -o - -- %s',
                self::QR_ENCODE_PATH,
                escapeshellarg($qrText)
            ),
            $resultCode
        );

        if (0 !== $resultCode) {
            ob_end_clean();

            throw new QrCodeException('unable to generate QR code');
        }

        return ob_get_clean();
    }
}


text:
squarescale_pd
==============

A puredata external that scales an input float up or down until it's within
range.

Internally, the object halves or doubles the input until it's within range.

It can be used something like this:

```
| 220 (
  |
| squarescale 60 150 |
  |               |
| 110 )         | 0.5 )
```

The object is especially useful for determining a canonical tempo in bpm for
very large or very small time values.


text:
// ==++==
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--==
/*============================================================
**
** Class:  AddInProcess
**
** Purpose:
**
===========================================================*/
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics.Contracts;
using System.Security;

namespace System.AddIn
{
    [SuppressUnmanagedCodeSecurity]
    [SecurityCritical]
    internal static class NativeMethods
    {
        [DllImport("KERNEL32.DLL", EntryPoint="IsWow64Process", PreserveSig=true, ExactSpelling=true, SetLastError=true)]
        internal static extern bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] ref bool bIsWow);
    }
}


text:
/*	This file is part of the software similarity tester SIM.
	Written by Dick Grune, Vrije Universiteit, Amsterdam.
	$Id: newargs.h,v 2.4 2012-05-16 07:56:06 dick Exp $
*/

extern void get_new_std_input_args(int *argcp, char const **argvp[]);
extern void get_new_recursive_args(int *argcp, const char **argvp[]);


text:
[Back to index][index]

Key Activities
--------------

### Evaluation

-   Dedicate time to understanding the "lay of the land" and the key
    players, along with their objectives and motivations.
-   Spend time with the parts of the organization that you are least
    familiar with.
-   Identify the security mandate as defined by the business/management.
-   Identify how IT Operations align with the security mandate.
-   Be alert to your existing biases.

Key Questions
-------------

### Management

***Has the business defined an internal security mandate?*** Business
drivers may originate from legal or industry compliance obligations,
internal policy, or customer or partner requirements. All actions and
activities in the Application Security program should tie back to
specific business obligations. If there is no business mandate, a case
must be made to the business that an application security program is
necessary. There are a number of industry statistical reports and
benchmarks available that address what other, similar organizations have
implemented and why.

***What are the current and planned human and financial resources
dedicated to Application Security?*** If resources are currently
dedicated to ensuring the integrity of applications, examine the current
allocation of those resources against their outcomes. The goal is to
ensure that in a world of limited resources, each activity is measured
for actual efficacy.

If there are few or no resources allocated to ensuring the integrity of
applications, the case must be made by leveraging data relevant to the
organization that can be quantified in financial terms. Building a
formal business case is the most effective way to approach the problem.
The case must address the needs as they apply to existing business
objectives. This is not the time for expounding on the virtues of
security. You must translate the business benefit of reallocating
financial resources. Simply asking for X number of dollars to reduce Y
number of vulnerabilities is typically ineffective.

***What are the IT & Business priorities?*** IT is both an enabler and
supporter of the business. Security is no different in this respect;
however, it has the added challenge of needing to be transparent where
it can be, and be a part of the ordinary workflow, or be perceived as a
roadblock. Once the business priorities have been identified, the
application security goals must be aligned with those priorities. If the
business priorities are to release new functionality at a rapid pace,
then application testing must keep pace; if the priorities are to
maintain the user experience over a long period of time, a pplication
testing must be rigorous before the user is ever exposed to the
application.

### Security

 ***What is the overall security budget and
the priorities of that budget?*** ***What percentage of that budget is
allocated to Application Security?*** Most departments will feel under
budget to some degree. Examine the overall security budget (including
cyberinsurance budget if possible, as it likely falls outside of the
security budget) to understand what receives financial priority and how
the Application Security program could benefit from those activities. If
there is significant spend on security infrastructure, ensure that they
are leveraging controls to protect applications -- for example, alerting
on the lack or improper use of security headers.

***Which of your assets are most frequently attacked?*** Not all attacks
are equal, and not all attacks are opportunistic. Careful examination of
attack frequency and velocity can identify the assets requiring
additional testing to ensure resiliency. Often, low(er) priority assets
will be targeted for exploitation as they tend not be as resilient to
attacks.

***What security tools and/or services do you as a company currently
own/use?*** Query other parts of the security organization to understand
what tools and services they already have access to. Software and
services are often bundled in contracts but not used as they were not
the primary purpose for purchase. If the QA team already has access to
security tools that were bundled as part of their QA software
acquisition there are opportunities to leverage that existing financial
commitment and even allow for native integration.

### IT Operations

 ***What are your assets?*** Include
assets you own and assets hosted or owned by third parties. Unknown or
unmanaged assets cannot be protected. It is also difficult to fix issues
if the person or team responsible for those assets is unknown.
Developing an accurate and update-to-date asset inventory can be
challenging, however, for the purpose of this exercise we will be
focusing on web-based assets. The narrowed scope can be aggressively
targeted and maintained.

***How are web assets isolated and distributed throughout the
infrastructure?*** If an attacker were able to compromise a machine (say
a webserver, or a database server) what would prevent them from pivoting
and hacking into other nearby machines? Are they physically isolated,
logically isolated, or do they share similar credentials, etc?

***How frequently do you perform network and server vulnerability
scans?*** Addressing web application vulnerabilities on a server that
never patches its operating system is a waste of resources. Understand
how often infrastructure is assessed and patched – this should match or
exceed the pac e of attack frequency.

***What is your tolerance around production safety?*** Some environments
are more sensitive to production testing, as there is always some
likelihood of impact; the goal is to get the likelihood of impact as
close to zero as possible. As the likelihood of impact approaches zero,
the frequency of testing should be increased. Ultimately, production
systems are the primary targets of our adversaries – they should be
tested as often as possible; at a minimum, at least as frequently as the
application itself is changed or updated.

### Engineering Groups (including QA and Software Development)

***According to the software development group's understanding of the
current processes, whose responsibility is application security?***
Security lives in our corporate cultures and psyche. Developers are
ultimately responsible for their code; understand whether they also
believe they are responsible for the integrity of that code. Security is
not the sole responsibility of either the developer community or the
security department. Foster a healthy environment of mutual
responsibility and accountability from all stakeholders. Begin by
sharing information in a non-aggressive manner.

***Where does Security fit into the software development lifecycle?***
If a development lifecycle does not exist, the first priority is to
demonstrate the business value of having a defined process. Most
organizations will have some process in place, even if it is an immature
one. The absence of a development process could also prove to be an
opportunity to ensure security is built into a process from the start.

Roughly, the steps to building or updating software can be generalized
as:

1.  Planning / Analysis
2.  Design
3.  Implementation
4.  Testing
5.  Maintain
6.  Decommission

Let's break down each step and discuss basic security activities that
are often considered to reduce risk.

**Planning / Analysis:** Ensure business analysts and stakeholders have
considered and can detail the security needs and risk tolerance of an
application. This may reference internal data classification policies to
describe the data sensitivity. Threat modeling may also help clarify the
potential threat agents who may be motivated to attack the proposed
application.

**Design:** A security architecture review may reveal security design
flaws in key areas such as authentication, access control, or separation
of concerns, and of course may identify missing categorical security
controls.

**Implementation:** The implementation process normally consists of the
coding and development of the overall architecture design formulated in
the previous phase. Developers should be receiving continuous security
feedback to ensure all security issues are being identified and
mitigated in conformance with the organization's risk tolerance.

**Testing:** Testing should include security tests as well as functional
tests. Areas of concentration should be on vulnerabilities that would
not have been uncovered during the implementation phase, such as
business logic vulnerabilities.

**Maintenance:** Once the application is promoted to production,
continuous testing of security issues should be ongoing throughout the
life of the application. As vulnerability vectors and attacks evolve,
the application should be tested to ensure defensibility against these
new attacks.

***How are software defects documented, trended, and prioritized?*** The
key to adoption of an Application Security program is alignment with and
transparency to current workflows. Identify how application defects are
documented, trended and prioritized, and plan to insert the application
security defects into these existing documents and processes.

***Are developers encouraged to develop secure code?*** Positive
incentive programs foster ownership and accountability for output.
Corporate culture varies from each organization; tapping into that
culture to offer incentives for producing rugged code will create a
natural momentum for finding and fixing security issues. Some cultures
will value access to otherwise less accessible personnel, such as lunch
with the CTO; others will be motivated by gifts that they might not have
purchased for themselves, such as a robotics kit. Take the time to
understand the culture of your organization and tap into the inherent
desire most people have to be rewarded. Remember that money does not
motivate everyone, and can even be demotivating in some cases.

***Are abuse and misuse cases part of test scripts?*** Test scripts are
often developed for the sole purpose of ensuring the application
performs the intended functionality. Introduce test scripts that could
identify the misuse of intended functionality, such as the ability to
execute similar functionality a user should not be able to access.

'**'Is everyone in the organization expected to have general software
security knowledge or is there a team/individual tasked with being the
"Security Deputy"?**' Security deputy programs are a good approach to
disseminating application security information to non-security focused
departments; they also have the added bonus of fostering a two-way
relationship.

Implement a deputy program that:

-   is focused on the goals of the application developers and
    application security testers
-   is aimed at achieving results beyond simple security awareness
-   can address the points throughout the development cycle where
    vulnerabilities are introduced – at the time the code is written

[back to index][index]

[index]: index.md "Index"