text:
#include "fcdevice.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "opc.h"
#include <math.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <stdio.h>

FCDevice::Transfer::Transfer(FCDevice *device, void *buffer, int length, PacketType type)
    : transfer(libusb_alloc_transfer(0)),
      type(type), finished(false)
{
    #if NEED_COPY_USB_TRANSFER_BUFFER
        bufferCopy = malloc(length);
        memcpy(bufferCopy, buffer, length);
        uint8_t *data = (uint8_t*) bufferCopy;
    #else
        uint8_t *data = (uint8_t*) buffer;
    #endif

    libusb_fill_bulk_transfer(transfer, device->mHandle,
        OUT_ENDPOINT, data, length, FCDevice::completeTransfer, this, 2000);
}

FCDevice::Transfer::~Transfer()
{
    libusb_free_transfer(transfer);
    #if NEED_COPY_USB_TRANSFER_BUFFER
        free(bufferCopy);
    #endif
}

FCDevice::FCDevice(libusb_device *device, bool verbose)
    : USBDevice(device, "fadecandy", verbose),
      mConfigMap(0), mNumFramesPending(0), mFrameWaitingForSubmit(false)
{
    mSerialBuffer[0] = '\0';
    mSerialString = mSerialBuffer;

    memset(&mFirmwareConfig, 0, sizeof mFirmwareConfig);
    mFirmwareConfig.control = TYPE_CONFIG;

    // Framebuffer headers
    memset(mFramebuffer, 0, sizeof mFramebuffer);
    for (unsigned i = 0; i < FRAMEBUFFER_PACKETS; ++i) {
        mFramebuffer[i].control = TYPE_FRAMEBUFFER | i;
    }
    mFramebuffer[FRAMEBUFFER_PACKETS - 1].control |= FINAL;

    // Color LUT headers
    memset(mColorLUT, 0, sizeof mColorLUT);
    for (unsigned i = 0; i < LUT_PACKETS; ++i) {
        mColorLUT[i].control = TYPE_LUT | i;
    }
    mColorLUT[LUT_PACKETS - 1].control |= FINAL;
}

FCDevice::~FCDevice()
{
    /*
     * If we have pending transfers, cancel them.
     * The Transfer objects themselves will be freed
     * once libusb completes them.
     */

    for (std::set<Transfer*>::iterator i = mPending.begin(), e = mPending.end(); i != e; ++i) {
        Transfer *fct = *i;
        libusb_cancel_transfer(fct->transfer);
    }
}

bool FCDevice::probe(libusb_device *device)
{
    libusb_device_descriptor dd;

    if (libusb_get_device_descriptor(device, &dd) < 0) {
        // Can't access descriptor?
        return false;
    }

    return dd.idVendor == 0x1d50 && dd.idProduct == 0x607a;
}

int FCDevice::open()
{
    int r = libusb_get_device_descriptor(mDevice, &mDD);
    if (r < 0) {
        return r;
    }

    r = libusb_open(mDevice, &mHandle);
    if (r < 0) {
        return r;
    }

    r = libusb_claim_interface(mHandle, 0);
    if (r < 0) {
        return r;
    }

    unsigned major = mDD.bcdDevice >> 8;
    unsigned minor = mDD.bcdDevice & 0xFF;
    snprintf(mVersionString, sizeof mVersionString, "%x.%02x", major, minor);

    return libusb_get_string_descriptor_ascii(mHandle, mDD.iSerialNumber,
        (uint8_t*)mSerialBuffer, sizeof mSerialBuffer);
}

void FCDevice::loadConfiguration(const Value &config)
{
    mConfigMap = findConfigMap(config);

    // Initial firmware configuration from our device options
    writeFirmwareConfiguration(config);
}

void FCDevice::writeFirmwareConfiguration(const Value &config)
{
    /*
     * Send a device configuration settings packet, using values based on a JSON
     * configuration.
     */

    if (!config.IsObject()) {
        std::clog << "Firmware configuration is not a JSON object\n";
        return;
    }

    const Value &led = config["led"];
    const Value &dither = config["dither"];
    const Value &interpolate = config["interpolate"];

    if (!(led.IsTrue() || led.IsFalse() || led.IsNull())) {
        std::clog << "LED configuration must be true (always on), false (always off), or null (default).\n";
    }

    mFirmwareConfig.data[0] =
        (led.IsNull() ? 0 : CFLAG_NO_ACTIVITY_LED)             |
        (led.IsTrue() ? CFLAG_LED_CONTROL : 0)                 |
        (dither.IsFalse() ? CFLAG_NO_DITHERING : 0)            |
        (interpolate.IsFalse() ? CFLAG_NO_INTERPOLATION : 0)   ;

    writeFirmwareConfiguration();
}

bool FCDevice::submitTransfer(Transfer *fct)
{
    /*
     * Submit a new USB transfer. The Transfer object is guaranteed to be freed eventually.
     * On error, it's freed right away.
     */

    int r = libusb_submit_transfer(fct->transfer);

    if (r < 0) {
        if (mVerbose && r != LIBUSB_ERROR_PIPE) {
            std::clog << "Error submitting USB transfer: " << libusb_strerror(libusb_error(r)) << "\n";
        }
        delete fct;
        return false;

    } else {
        mPending.insert(fct);
        return true;
    }
}

void FCDevice::completeTransfer(libusb_transfer *transfer)
{
    FCDevice::Transfer *fct = static_cast<FCDevice::Transfer*>(transfer->user_data);
    fct->finished = true;
}

void FCDevice::flush()
{
    // Erase any finished transfers

    std::set<Transfer*>::iterator current = mPending.begin();
    while (current != mPending.end()) {
        std::set<Transfer*>::iterator next = current;
        next++;

        Transfer *fct = *current;
        if (fct->finished) {
            switch (fct->type) {

                case FRAME:
                    mNumFramesPending--;
                    break;

                default:
                    break;
            }

            mPending.erase(current);
            delete fct;
        }

        current = next;
    }

    // Submit new frames, if we had a queued frame waiting

    if (mFrameWaitingForSubmit && mNumFramesPending < MAX_FRAMES_PENDING) {
        writeFramebuffer();
    }
}

void FCDevice::writeColorCorrection(const Value &color)
{
    /*
     * Populate the color correction table based on a JSON configuration object,
     * and send the new color LUT out over USB.
     *
     * 'color' may be 'null' to load an identity-mapped LUT, or it may be
     * a dictionary of options including 'gamma' and 'whitepoint'.
     *
     * This calculates a compound curve with a linear section and a nonlinear
     * section. The linear section, near zero, avoids creating very low output
     * values that will cause distracting flicker when dithered. This isn't a problem
     * when the LEDs are viewed indirectly such that the flicker is below the threshold
     * of perception, but in cases where the flicker is a problem this linear section can
     * eliminate it entierly at the cost of some dynamic range.
     *
     * By default, the linear section is disabled (linearCutoff is zero). To enable the
     * linear section, set linearCutoff to some nonzero value. A good starting point is
     * 1/256.0, correspnding to the lowest 8-bit PWM level.
     */

    // Default color LUT parameters
    double gamma = 1.0;                         // Power for nonlinear portion of curve
    double whitepoint[3] = {1.0, 1.0, 1.0};     // White-point RGB value (also, global brightness)
    double linearSlope = 1.0;                   // Slope (output / input) of linear section of the curve, near zero
    double linearCutoff = 0.0;                  // Y (output) coordinate of intersection of linear and nonlinear curves

    /*
     * Parse the JSON object
     */

    if (color.IsObject()) {
        const Value &vGamma = color["gamma"];
        const Value &vWhitepoint = color["whitepoint"];
        const Value &vLinearSlope = color["linearSlope"];
        const Value &vLinearCutoff = color["linearCutoff"];

        if (vGamma.IsNumber()) {
            gamma = vGamma.GetDouble();
        } else if (!vGamma.IsNull() && mVerbose) {
            std::clog << "Gamma value must be a number.\n";
        }

        if (vLinearSlope.IsNumber()) {
            linearSlope = vLinearSlope.GetDouble();
        } else if (!vLinearSlope.IsNull() && mVerbose) {
            std::clog << "Linear slope value must be a number.\n";
        }

        if (vLinearCutoff.IsNumber()) {
            linearCutoff = vLinearCutoff.GetDouble();
        } else if (!vLinearCutoff.IsNull() && mVerbose) {
            std::clog << "Linear slope value must be a number.\n";
        }

        if (vWhitepoint.IsArray() &&
            vWhitepoint.Size() == 3 &&
            vWhitepoint[0u].IsNumber() &&
            vWhitepoint[1].IsNumber() &&
            vWhitepoint[2].IsNumber()) {
            whitepoint[0] = vWhitepoint[0u].GetDouble();
            whitepoint[1] = vWhitepoint[1].GetDouble();
            whitepoint[2] = vWhitepoint[2].GetDouble();
        } else if (!vWhitepoint.IsNull() && mVerbose) {
            std::clog << "Whitepoint value must be a list of 3 numbers.\n";
        }

    } else if (!color.IsNull() && mVerbose) {
        std::clog << "Color correction value must be a JSON dictionary object.\n";
    }

    /*
     * Calculate the color LUT, stowing the result in an array of USB packets.
     */

    Packet *packet = mColorLUT;
    const unsigned firstByteOffset = 1;  // Skip padding byte
    unsigned byteOffset = firstByteOffset;

    for (unsigned channel = 0; channel < 3; channel++) {
        for (unsigned entry = 0; entry < LUT_ENTRIES; entry++) {
            double output;

            /*
             * Normalized input value corresponding to this LUT entry.
             * Ranges from 0 to slightly higher than 1. (The last LUT entry
             * can't quite be reached.)
             */
            double input = (entry << 8) / 65535.0;

            // Scale by whitepoint before anything else
            input *= whitepoint[channel];

            // Is this entry part of the linear section still?
            if (input * linearSlope <= linearCutoff) {

                // Output value is below linearCutoff. We're still in the linear portion of the curve
                output = input * linearSlope;

            } else {

                // Nonlinear portion of the curve. This starts right where the linear portion leaves
                // off. We need to avoid any discontinuity.

                double nonlinearInput = input - (linearSlope * linearCutoff);
                double scale = 1.0 - linearCutoff;
                output = linearCutoff + pow(nonlinearInput / scale, gamma) * scale;
            }

            // Round to the nearest integer, and clamp. Overflow-safe.
            int64_t longValue = (output * 0xFFFF) + 0.5;
            int intValue = std::max<int64_t>(0, std::min<int64_t>(0xFFFF, longValue));

            // Store LUT entry, little-endian order.
            packet->data[byteOffset++] = uint8_t(intValue);
            packet->data[byteOffset++] = uint8_t(intValue >> 8);
            if (byteOffset >= sizeof packet->data) {
                byteOffset = firstByteOffset;
                packet++;
            }
        }
    }

    // Start asynchronously sending the LUT.
    submitTransfer(new Transfer(this, &mColorLUT, sizeof mColorLUT));
}

void FCDevice::writeFramebuffer()
{
    /*
     * Asynchronously write the current framebuffer.
     *
     * TODO: Currently if this gets ahead of what the USB device is capable of,
     *       we always drop frames. Alternatively, it would be nice to have end-to-end
     *       flow control so that the client can produce frames slower.
     */

    if (mNumFramesPending >= MAX_FRAMES_PENDING) {
        // Too many outstanding frames. Wait to submit until a previous frame completes.
        mFrameWaitingForSubmit = true;
        return;
    }

    if (submitTransfer(new Transfer(this, &mFramebuffer, sizeof mFramebuffer, FRAME))) {
        mFrameWaitingForSubmit = false;
        mNumFramesPending++;
    }
}

void FCDevice::writeMessage(Document &msg)
{
    /*
     * Dispatch a device-specific JSON command.
     *
     * This can be used to send frames or settings directly to one device,
     * bypassing the mapping we use for Open Pixel Control clients. This isn't
     * intended to be the fast path for regular applications, but it can be used
     * by configuration tools that need to operate regardless of the mapping setup.
     */

    const char *type = msg["type"].GetString();

    if (!strcmp(type, "device_options")) {
        /*
         * TODO: Eventually this should turn into the same thing as
         *       loadConfiguration() and it shouldn't be device-specific,
         *       but for now most of fcserver assumes the configuration is static.
         */
        writeFirmwareConfiguration(msg["options"]);
        return;
    }

    if (!strcmp(type, "device_pixels")) {
        // Write raw pixels, without any mapping
        writeDevicePixels(msg);
        return;
    }

    // Chain to default handler
    USBDevice::writeMessage(msg);
}

void FCDevice::writeDevicePixels(Document &msg)
{
    /*
     * Write pixels without mapping, from a JSON integer
     * array in msg["pixels"]. The pixel array is removed from
     * the reply to save network bandwidth.
     *
     * Pixel values are clamped to [0, 255], for convenience.
     */

    const Value &pixels = msg["pixels"];
    if (!pixels.IsArray()) {
        msg.AddMember("error", "Pixel array is missing", msg.GetAllocator());
    } else {

        // Truncate to the framebuffer size, and only deal in whole pixels.
        int numPixels = pixels.Size() / 3;
        if (numPixels > NUM_PIXELS)
            numPixels = NUM_PIXELS;

        for (int i = 0; i < numPixels; i++) {
            uint8_t *out = fbPixel(i);

            const Value &r = pixels[i*3 + 0];
            const Value &g = pixels[i*3 + 1];
            const Value &b = pixels[i*3 + 2];

            out[0] = std::max(0, std::min(255, r.IsInt() ? r.GetInt() : 0));
            out[1] = std::max(0, std::min(255, g.IsInt() ? g.GetInt() : 0));
            out[2] = std::max(0, std::min(255, b.IsInt() ? b.GetInt() : 0));
        }

        writeFramebuffer();
    }
}

void FCDevice::writeMessage(const OPC::Message &msg)
{
    /*
     * Dispatch an incoming OPC command
     */

    switch (msg.command) {

        case OPC::SetPixelColors:
            opcSetPixelColors(msg);
            writeFramebuffer();
            return;

        case OPC::SystemExclusive:
            opcSysEx(msg);
            return;
    }

    if (mVerbose) {
        std::clog << "Unsupported OPC command: " << unsigned(msg.command) << "\n";
    }
}

void FCDevice::opcSysEx(const OPC::Message &msg)
{
    if (msg.length() < 4) {
        if (mVerbose) {
            std::clog << "SysEx message too short!\n";
        }
        return;
    }

    unsigned id = (unsigned(msg.data[0]) << 24) |
                  (unsigned(msg.data[1]) << 16) |
                  (unsigned(msg.data[2]) << 8)  |
                   unsigned(msg.data[3])        ;

    switch (id) {

        case OPC::FCSetGlobalColorCorrection:
            return opcSetGlobalColorCorrection(msg);

        case OPC::FCSetFirmwareConfiguration:
            return opcSetFirmwareConfiguration(msg);

    }

    // Quietly ignore unhandled SysEx messages.
}

void FCDevice::opcSetPixelColors(const OPC::Message &msg)
{
    /*
     * Parse through our device's mapping, and store any relevant portions of 'msg'
     * in the framebuffer.
     */

    if (!mConfigMap) {
        // No mapping defined yet. This device is inactive.
        return;
    }

    const Value &map = *mConfigMap;
    for (unsigned i = 0, e = map.Size(); i != e; i++) {
        opcMapPixelColors(msg, map[i]);
    }
}

void FCDevice::opcMapPixelColors(const OPC::Message &msg, const Value &inst)
{
    /*
     * Parse one JSON mapping instruction, and copy any relevant parts of 'msg'
     * into our framebuffer. This looks for any mapping instructions that we
     * recognize:
     *
     *   [ OPC Channel, First OPC Pixel, First output pixel, Pixel count ]
     *   [ OPC Channel, First OPC Pixel, First output pixel, Color channels ]
     */

    unsigned msgPixelCount = msg.length() / 3;

    if (inst.IsArray() && inst.Size() == 4) {
        // Map a range from an OPC channel to our framebuffer

        const Value &vChannel = inst[0u];
        const Value &vFirstOPC = inst[1];
        const Value &vFirstOut = inst[2];
        const Value &vCount = inst[3];

        if (vChannel.IsUint() && vFirstOPC.IsUint() && vFirstOut.IsUint() && vCount.IsUint()) {
            unsigned channel = vChannel.GetUint();
            unsigned firstOPC = vFirstOPC.GetUint();
            unsigned firstOut = vFirstOut.GetUint();
            unsigned count = vCount.GetUint();

            if (channel != msg.channel) {
                return;
            }

            // Clamping, overflow-safe
            firstOPC = std::min<unsigned>(firstOPC, msgPixelCount);
            firstOut = std::min<unsigned>(firstOut, unsigned(NUM_PIXELS));
            count = std::min<unsigned>(count, msgPixelCount - firstOPC);
            count = std::min<unsigned>(count, NUM_PIXELS - firstOut);

            // Copy pixels
            const uint8_t *inPtr = msg.data + (firstOPC * 3);
            unsigned outIndex = firstOut;

            while (count--) {
                uint8_t *outPtr = fbPixel(outIndex++);
                outPtr[0] = inPtr[0];
                outPtr[1] = inPtr[1];
                outPtr[2] = inPtr[2];
                inPtr += 3;
            }

            return;
        }
    }

    if (inst.IsArray() && inst.Size() == 5) {
        // Map a range from an OPC channel to our framebuffer, with color channel swizzling

        const Value &vChannel = inst[0u];
        const Value &vFirstOPC = inst[1];
        const Value &vFirstOut = inst[2];
        const Value &vCount = inst[3];
        const Value &vColorChannels = inst[4];

        if (vChannel.IsUint() && vFirstOPC.IsUint() && vFirstOut.IsUint() && vCount.IsUint()
            && vColorChannels.IsString() && vColorChannels.GetStringLength() == 3) {

            unsigned channel = vChannel.GetUint();
            unsigned firstOPC = vFirstOPC.GetUint();
            unsigned firstOut = vFirstOut.GetUint();
            unsigned count = vCount.GetUint();
            const char *colorChannels = vColorChannels.GetString();

            if (channel != msg.channel) {
                return;
            }

            // Clamping, overflow-safe
            firstOPC = std::min<unsigned>(firstOPC, msgPixelCount);
            firstOut = std::min<unsigned>(firstOut, unsigned(NUM_PIXELS));
            count = std::min<unsigned>(count, msgPixelCount - firstOPC);
            count = std::min<unsigned>(count, NUM_PIXELS - firstOut);

            // Copy pixels
            const uint8_t *inPtr = msg.data + (firstOPC * 3);
            unsigned outIndex = firstOut;
            bool success = true;

            while (count--) {
                uint8_t *outPtr = fbPixel(outIndex++);

                for (int channel = 0; channel < 3; channel++) {
                    if (!OPC::pickColorChannel(outPtr[channel], colorChannels[channel], inPtr)) {
                        success = false;
                        break;
                    }
                }

                inPtr += 3;
            }

            if (success) {
                return;
            }
        }
    }

    // Still haven't found a match?
    if (mVerbose) {
        rapidjson::GenericStringBuffer<rapidjson::UTF8<> > buffer;
        rapidjson::Writer<rapidjson::GenericStringBuffer<rapidjson::UTF8<> > > writer(buffer);
        inst.Accept(writer);
        std::clog << "Unsupported JSON mapping instruction: " << buffer.GetString() << "\n";
    }
}

void FCDevice::opcSetGlobalColorCorrection(const OPC::Message &msg)
{
    /*
     * Parse the message as JSON text, and if successful, write new
     * color correction data to the device.
     */

    // Mutable NUL-terminated copy of the message string
    std::string text((char*)msg.data + 4, msg.length() - 4);

    // Parse it in-place
    rapidjson::Document doc;
    doc.ParseInsitu<0>(&text[0]);

    if (doc.HasParseError()) {
        if (mVerbose) {
            std::clog << "Parse error in color correction JSON at character "
                << doc.GetErrorOffset() << ": " << doc.GetParseError() << "\n";
        }
        return;
    }

    /*
     * Successfully parsed the JSON. From here, it's handled identically to
     * objects that come through the config file.
     */
    writeColorCorrection(doc);
}

void FCDevice::opcSetFirmwareConfiguration(const OPC::Message &msg)
{
    /*
     * Raw firmware configuration packet
     */

    memcpy(mFirmwareConfig.data, msg.data + 4, std::min<size_t>(sizeof mFirmwareConfig.data, msg.length() - 4));
    writeFirmwareConfiguration();
}

void FCDevice::writeFirmwareConfiguration()
{
    // Write mFirmwareConfig to the device
    submitTransfer(new Transfer(this, &mFirmwareConfig, sizeof mFirmwareConfig));
}

std::string FCDevice::getName()
{
    std::ostringstream s;
    s << "Fadecandy";
    if (mSerialString[0]) {
        s << " (Serial# " << mSerialString << ", Version " << mVersionString << ")";
    }
    return s.str();
}

void FCDevice::describe(rapidjson::Value &object, Allocator &alloc)
{
    USBDevice::describe(object, alloc);
    object.AddMember("version", mVersionString, alloc);
    object.AddMember("bcd_version", mDD.bcdDevice, alloc);
}


text:
package org.asynchttpclient.providers.netty3;

import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.async.WebDavBasicTest;

public class NettyWebDavBasicTest extends WebDavBasicTest {
    @Override
    public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
        return NettyProviderUtil.nettyProvider(config);
    }
}

redpajama_set_name: RedPajamaGithub


text:
#ifndef TENSORFLOW_LITE_DELEGATES_GPU_METAL_KERNELS_CUSTOM_REGISTRY_H_
#define TENSORFLOW_LITE_DELEGATES_GPU_METAL_KERNELS_CUSTOM_REGISTRY_H_

#include <vector>

#include "tensorflow/lite/delegates/gpu/common/model.h"
#include "tensorflow/lite/delegates/gpu/common/status.h"
#include "tensorflow/lite/delegates/gpu/metal/compute_task_descriptor.h"
#include "tensorflow/lite/delegates/gpu/metal/runtime_options.h"

namespace tflite {
namespace gpu {
namespace metal {

// Registers custom operations.
Status RegisterCustomOps(const GraphFloat32& graph, const Node* node,
                         const std::vector<ValueId>& inputs,
                         const std::vector<ValueId>& outputs,
                         const RuntimeOptions& options,
                         std::vector<ComputeTaskDescriptorPtr>* tasks);

}  // namespace metal
}  // namespace gpu
}  // namespace tflite

#endif  // TENSORFLOW_LITE_DELEGATES_GPU_METAL_KERNELS_CUSTOM_REGISTRY_H_


text:
package com.mxit.core.protocol.command;

import com.mxit.core.protocol.type.*;
import com.mxit.core.protocol.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/** New MultiMX Command */
public class CommandNewMultiMX extends Command
{
    // Members:
    public String RoomName = null;
    public ArrayList<String> Contacts = null;
    public String GroupName = "";

    public String RoomId;

    /** Creates a new Login Command */
    public CommandNewMultiMX()
    {
        Type = CommandType.AddNewGroupChat;
    }

    @Override
    public void parseData(byte[] cmdData)
    {
        RoomId = new String(cmdData);
    }

    @Override
    public void buildData(DataOutputStream sb) throws IOException
    {
        sb.write(RoomName.getBytes());
        sb.write(FIELD_SEP);
        sb.write(String.valueOf(Contacts.size()).getBytes());
        for (String MXitId : Contacts)
        {
            sb.write(FIELD_SEP);
            sb.write(MXitId.getBytes());
        }
        sb.write(FIELD_SEP);
        sb.write(GroupName.getBytes());
    }
}


text:
title: Footprinting and Reconnaissance
localeTitle: Pegada e Reconhecimento
---
## Pegada e Reconhecimento

Pegada ou Reconhecimento é o processo de coletar informações sobre o alvo antes que um ataque seja realizado por um hacker. Em uma fase de pegada, o hacker tentará coletar o máximo de informações possíveis sobre o alvo, o que pode ser útil em um ataque. tal como:

1.  Identificando o sistema operacional
2.  Portas de varredura para detectar as abertas para exploração
3.  As consultas WHOIS costumavam conhecer as informações do registrador de um serviço específico.
4.  Rastreando a web para saber mais sobre o alvo.
5.  Coleta de números de telefone, endereços de e-mail, informações sobre funcionários e outras informações privilegiadas.

Em suma, a pegada é a primeira e mais importante fase do teste de penetração, sem a qual pode ser muito difícil realizar um hack.

Para coletar informações, um hacker pode usar várias ferramentas e técnicas listadas abaixo.

1.  Nslookup
2.  Nmap
3.  Sam Spade
4.  Traceroute
5.  NeoTrace e muitos mais.

#### Mais Informações:


text:
<?php

namespace SB\PlatformBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ImageType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file', 'file');
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'SB\PlatformBundle\Entity\Image'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'sb_platformbundle_image';
    }
}


text:
<?php

namespace Admingenerator\DoctrineOrmDemoBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Admingenerator\DoctrineOrmDemoBundle\Entity\Post;

class LoadPostData extends AbstractFixture implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $postA = $this->createPost(
            'Brasil 2014!',
            'I can\'t wait for Football World Championship in Brasil!

I really hope Ronaldo will lead Portugal into the finals. I\'m so
             excited to see his goals!
',
            '2014-02-19',
            true,
            'Football',
            array('Amazing', 'Can\'t wait')
        );

        $postB = $this->createPost(
            'George Michael\'s new album released!',
            '
Finally the long awaited "Symphonica" album is released!
             I\'ll be buying my copy first thing in the morning!
',
            '2014-03-12',
            true,
            'Pop',
            array('Amazing', 'Finally')
        );

        $postC = $this->createPost(
            'Eminem music is bad for your health',
            '
I was listening to Eninem while jogging this morning and then I got hit by a car.
             I love Eminem music, but it\'s bad for my health.
',
            '2014-05-12',
            false,
            'Hip Hop',
            array('Funny', 'Silly')
        );

        $manager->persist($postA);
        $manager->persist($postB);
        $manager->persist($postC);
        $manager->flush();
    }

    private function createPost($title, $content, $date, $published, $categoryName, $tagNames)
    {
        $post = new Post();
        $post->setTitle($title);
        $post->setCreatedAt(new \DateTime($date));
        $post->setAuthor('John Doe');
        $post->setContent($content);
        $post->setIsPublished($published);
        $post->setCategory($this->getReference(sprintf('category_%s', strtolower($categoryName))));

        foreach($tagNames as $tagName) {
            $post->addTag($this->getReference(sprintf('tag_%s', strtolower($tagName))));
        }

        return $post;
    }

    /**
     * {@inheritDoc}
     */
    public function getOrder()
    {
        return 3; // the order in which fixtures will be loaded
    }
}


text:
import $ from 'jquery';

import { A } from '@ember/array';
import { gt } from '@ember/object/computed';
import Component from '@ember/component';

export default Component.extend({
  currentPage:1,
  pageSize:5,
  nextDisabled:gt('currentPage', 5),
  tableClassNames:'table table-striped table-bordered table-hover yte-table-responsive table-condensed',
  data: A(),

  didInsertElement() {
    this.fetchData();
  },

  fetchData() {
    let url = `https://api.github.com/repos/emberjs/ember.js/events?page=${this.get('currentPage')}&per_page=${this.get('pageSize')}`;

    $.getJSON(url, (json) => {
      this.set('data', json);
    });
  },

  actions: {
    nextPageRequested() {
      this.incrementProperty('currentPage');
      this.fetchData();
    },

    prevPageRequested() {
      this.decrementProperty('currentPage');
      this.fetchData();
    },

    selectionChanged:function(selectedRows){
      this.set('selectedRows', selectedRows);
    }
  }
});
// END-SNIPPET


text:
package com.anaplan.client.transport;

import java.io.IOException;
import java.util.List;
import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import org.apache.http.impl.auth.NTLMEngine;

/**
 * Yanked from here: https://github.com/square/okhttp/issues/206
 * User: spondonsaha
 * Date: 9/20/17
 * Time: 11:41 PM
 */
public class NtlmAuthenticator implements Authenticator {
    private NTLMEngine engine;
    private final String workstation;
    private final String domain;
    private final String username;
    private final String password;
    private final String ntlmMsg1;

    public NtlmAuthenticator(String username, String password, String domain, String workstation) {
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.workstation = workstation;
        String localNtlmMsg1 = null;
        try {
            engine = new JCIFSEngine();
            localNtlmMsg1 = engine.generateType1Msg(null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ntlmMsg1 = localNtlmMsg1;
    }

    @Override
    public Request authenticate(Route route, Response response) throws IOException {
        final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
        if (WWWAuthenticate.contains("NTLM")) {
            return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
        }
        String ntlmMsg3 = null;
        try {
            ntlmMsg3 = engine.generateType3Msg(username, password, domain, workstation, WWWAuthenticate.get(0).substring(5));
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to set up JCIFS NTLM proxy authentication, Please check your NTLM parameters and provide the correct value of domain, workstation, username and password");
        }
        return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
    }
}


text:
FROM debian:buster-slim

# set correct timezone + generic UTF-8 locale
ENV TZ=Europe/Berlin LC_ALL=C.UTF-8

# set workdir ("~" does not work)
WORKDIR /root

# prevent "update-alternatives: error: error creating symbolic link '/usr/share/man/man1/rmid.1.gz.dpkg-tmp': No such file or directory" (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=863199)
RUN mkdir -p /usr/share/man/man1

# install necessary packages
RUN apt-get update && apt-get install -y --no-install-recommends \
 openjdk-11-jre-headless \
 python3 \
 wget \
 unzip \
 && rm -rf /var/lib/apt/lists/*

# download/extract BaseX
RUN wget https://files.basex.org/releases/9.3.3/BaseX933.zip -O basex.zip \
 && unzip basex.zip -d tools \
 && rm basex.zip

# download/extract/move Saxon HE 9
RUN wget https://sourceforge.net/projects/saxon/files/Saxon-HE/9.9/SaxonHE9-9-1-7J.zip/download -O saxon.zip \
 && unzip saxon.zip saxon9he.jar \
 && mv saxon9he.jar tools/basex/lib/custom \
 && rm saxon.zip

# copy Webapp
COPY webapp/*.xqm webapp/scf_service_config*.xml webapp/
COPY webapp/static/error.xsl webapp/static/
COPY webapp/WEB-INF/*.xml webapp/WEB-INF/
COPY modules modules
COPY .basexhome .

# expose RESTXQ port
EXPOSE 8984

# run BaseX
CMD ["tools/basex/bin/basexhttp", "-d"]


text:
/***************************************************************************
 *  ChangeLog:
 * -------------------------------------------------------------------------
 *    Revision 1.2  2009/10/31 21:28:52  jot836
 *    This is the current version of KMA project 3.
 *    It includes:
 *    - the most up-to-date handout (F'09)
 *    - updated skeleton including
 *        file-driven test harness,
 *        trace generator script,
 *        support for evaluating efficiency of algorithm (wasted memory),
 *        gnuplot support for plotting allocation and waste,
 *        set of traces for all students to use (including a makefile and README of the settings),
 *    - different version of the testsuite for use on the submission site, including:
 *        scoreboard Python scripts, which posts the top 5 scores on the course webpage
 *
 *    Revision 1.1  2005/10/24 16:07:09  sbirrer
 *    - skeleton
 *
 *    Revision 1.2  2004/11/05 15:45:56  sbirrer
 *    - added size as a parameter to kma_free
 *
 *    Revision 1.1  2004/11/03 23:04:03  sbirrer
 *    - initial version for the kernel memory allocator project
 *
 ***************************************************************************/
#ifdef KMA_RM
#define __KMA_IMPL__

/************System include***********************************************/
#include <assert.h>
#include <stdlib.h>

/************Private include**********************************************/
#include "kma_page.h"
#include "kma.h"

/************Defines and Typedefs*****************************************/
/*  #defines and typedefs should have their names in all caps.
 *  Global variables begin with g. Global constants with k. Local
 *  variables should be in all lower case. When initializing
 *  structures and arrays, line everything up in neat columns.
 */

/************Global Variables*********************************************/

/************Function Prototypes******************************************/

/************External Declaration*****************************************/

/**************Implementation***********************************************/

void*
kma_malloc(kma_size_t size)
{
  return NULL;
}

void
kma_free(void* ptr, kma_size_t size)
{
  ;
}

#endif // KMA_RM


text:
[![CocoaPods](https://img.shields.io/cocoapods/p/libPhoneNumber-iOS.svg?style=flat)](http://cocoapods.org/?q=libPhoneNumber-iOS)
[![CocoaPods](https://img.shields.io/cocoapods/v/libPhoneNumber-iOS.svg?style=flat)](http://cocoapods.org/?q=libPhoneNumber-iOS)
[![Travis](https://travis-ci.org/iziz/libPhoneNumber-iOS.svg?branch=master)](https://travis-ci.org/iziz/libPhoneNumber-iOS)
[![Coveralls](https://coveralls.io/repos/iziz/libPhoneNumber-iOS/badge.svg?branch=master&service=github)](https://coveralls.io/github/iziz/libPhoneNumber-iOS?branch=master)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)

# **libPhoneNumber for iOS**

 - NBPhoneNumberUtil
 - NBAsYouTypeFormatter

> ARC only, or add the **"-fobjc-arc"** flag for non-ARC

## Update Log
[https://github.com/iziz/libPhoneNumber-iOS/wiki/Update-Log](https://github.com/iziz/libPhoneNumber-iOS/wiki/Update-Log)

## Install

#### Using [CocoaPods](http://cocoapods.org/?q=libPhoneNumber-iOS)
```
source 'https://github.com/CocoaPods/Specs.git'
pod 'libPhoneNumber-iOS', '~> 0.8'
```

#### Using [Carthage](https://github.com/Carthage/Carthage)

 Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

 You can install Carthage with [Homebrew](http://brew.sh/) using the following command:

```bash
$ brew update
$ brew install carthage
```

To integrate libPhoneNumber into your Xcode project using Carthage, specify it in your `Cartfile`:

```ogdl
github "iziz/libPhoneNumber-iOS"
```

#### Setting up manually
 Add source files to your projects from libPhoneNumber
    - Add "CoreTelephony.framework"

See sample test code from
> [libPhoneNumber-iOS/libPhoneNumberTests/ ... Test.m] (https://github.com/iziz/libPhoneNumber-iOS/tree/master/libPhoneNumberTests)

## Usage - **NBPhoneNumberUtil**
```obj-c
 NBPhoneNumberUtil *phoneUtil = [[NBPhoneNumberUtil alloc] init];
 NSError *anError = nil;
 NBPhoneNumber *myNumber = [phoneUtil parse:@"6766077303"
                              defaultRegion:@"AT" error:&anError];
 if (anError == nil) {
     // Should check error
     NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");

     // E164          : +436766077303
     NSLog(@"E164          : %@", [phoneUtil format:myNumber
                                       numberFormat:NBEPhoneNumberFormatE164
                                              error:&anError]);
     // INTERNATIONAL : +43 676 6077303
     NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber
                                       numberFormat:NBEPhoneNumberFormatINTERNATIONAL
                                              error:&anError]);
     // NATIONAL      : 0676 6077303
     NSLog(@"NATIONAL      : %@", [phoneUtil format:myNumber
                                       numberFormat:NBEPhoneNumberFormatNATIONAL
                                              error:&anError]);
     // RFC3966       : tel:+43-676-6077303
     NSLog(@"RFC3966       : %@", [phoneUtil format:myNumber
                                       numberFormat:NBEPhoneNumberFormatRFC3966
                                              error:&anError]);
 } else {
     NSLog(@"Error : %@", [anError localizedDescription]);
 }

 NSLog (@"extractCountryCode [%@]", [phoneUtil extractCountryCode:@"823213123123" nationalNumber:nil]);

 NSString *nationalNumber = nil;
 NSNumber *countryCode = [phoneUtil extractCountryCode:@"823213123123" nationalNumber:&nationalNumber];

 NSLog (@"extractCountryCode [%@] [%@]", countryCode, nationalNumber);
```
##### Output
```
2014-07-06 12:39:37.240 libPhoneNumberTest[1581:60b] isValidPhoneNumber ? [YES]
2014-07-06 12:39:37.242 libPhoneNumberTest[1581:60b] E164          : +436766077303
2014-07-06 12:39:37.243 libPhoneNumberTest[1581:60b] INTERNATIONAL : +43 676 6077303
2014-07-06 12:39:37.243 libPhoneNumberTest[1581:60b] NATIONAL      : 0676 6077303
2014-07-06 12:39:37.244 libPhoneNumberTest[1581:60b] RFC3966       : tel:+43-676-6077303
2014-07-06 12:39:37.244 libPhoneNumberTest[1581:60b] extractCountryCode [82]
2014-07-06 12:39:37.245 libPhoneNumberTest[1581:60b] extractCountryCode [82] [3213123123]
```

#### with Swift
##### - in Bridging-Header
```swift
// Manually added
#import "NBPhoneNumberUtil.h"
#import "NBPhoneNumber.h"

// CocoaPods (check your library path)
#import "libPhoneNumber_iOS/NBPhoneNumberUtil.h"
#import "libPhoneNumber_iOS/NBPhoneNumber.h"

// add more if you want...
```

##### - in swift class file
```swift
override func viewDidLoad() {
    super.viewDidLoad()
    let phoneUtil = NBPhoneNumberUtil()

    var errorPointer:NSError?
    var number:NBPhoneNumber = phoneUtil.parse("01041241282", defaultRegion:"KR", error:&errorPointer)

    NSLog("%@", number)
}
```

## Usage - **NBAsYouTypeFormatter**
```obj-c
    NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
    NSLog(@"%@", [f inputDigit:@"6"]); // "6"
    NSLog(@"%@", [f inputDigit:@"5"]); // "65"
    NSLog(@"%@", [f inputDigit:@"0"]); // "650"
    NSLog(@"%@", [f inputDigit:@"2"]); // "650 2"
    NSLog(@"%@", [f inputDigit:@"5"]); // "650 25"
    NSLog(@"%@", [f inputDigit:@"3"]); // "650 253"

    // Note this is how a US local number (without area code) should be formatted.
    NSLog(@"%@", [f inputDigit:@"2"]); // "650 2532"
    NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 22"
    NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 222"
    NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 2222"
    // Can remove last digit
    NSLog(@"%@", [f removeLastDigit]); // "650 253 222"

    NSLog(@"%@", [f inputString:@"16502532222"]); // 1 650 253 2222
```

##### Visit [libphonenumber](https://github.com/googlei18n/libphonenumber) for more information or mail (zen.isis@gmail.com)


text:
<?php

error_reporting(E_STRICT | E_ALL);

// You can set the include path to src directory or reference
// DfpUser.php directly via require_once.
// $path = '/path/to/dfp_api_php_lib/src';
$path = dirname(__FILE__) . '/../../../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once 'Google/Api/Ads/Dfp/Lib/DfpUser.php';
require_once dirname(__FILE__) . '/../../../Common/ExampleUtils.php';

try {
  // Get DfpUser from credentials in "../auth.ini"
  // relative to the DfpUser.php file's directory.
  $user = new DfpUser();

  // Log SOAP XML request and response.
  $user->LogDefaults();

  // Get the NetworkService.
  $networkService = $user->GetService('NetworkService', 'v201505');

  // Get the current network.
  $network = $networkService->getCurrentNetwork();

  print 'Current network has network code "' . $network->networkCode
      . '" and display name "' . $network->displayName . "\".\n";
} catch (OAuth2Exception $e) {
  ExampleUtils::CheckForOAuth2Errors($e);
} catch (ValidationException $e) {
  ExampleUtils::CheckForOAuth2Errors($e);
} catch (Exception $e) {
  print $e->getMessage() . "\n";
}


text:
layout: post
title: "Imperative Functional Reactive"
date: 2017-03-18 11:25:06 -0500
comments: true
published: false
---

FP:
  immutability and higher order functions are not the main draw
  Function pipeline/chaining and lazy evaluaton is

  - Observable different from Observer pattern is that an end-of-signal is sent by Observables
  - Errors are not thrown 'back' as in Imperative programming but is treated like a first class citizen and is processed as data
  - Sync and Async can be implemented on either server or client

  ------

I like this ascii art:

¯\_(ツ)_/¯


text:
YouTube client App
==================

Test app for some of the most recent/interesting technologies.

## How to run
1. run `npm install`
2. run `bower install`
3. `grunt`

## Used technology

* YouTube API v3
* [underscore]
* [Backbone.js]
* [RequireJS]
* [Bacon.js]
* [jQuery]
* [JSHint]
* [scss]
* oocss
* [Bootstrap]
* [grunt]
* [bower]

[underscore]:http://underscorejs.org/
[jquery]:https://jquery.com/
[SCSS]:http://sass-lang.com/
[backbone.js]:http://backbonejs.org/
[RequireJS]:http://requirejs.org/
[Bacon.js]:https://baconjs.github.io/
[JSHint]:http://jshint.com/
[Bootstrap]:http://getbootstrap.com/
[grunt]:http://gruntjs.com/
[bower]:http://bower.io/


text:
package com.mpatric.mp3agic;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

public class Mp3File extends FileWrapper {

	private static final int DEFAULT_BUFFER_LENGTH = 65536;
	private static final int MINIMUM_BUFFER_LENGTH = 40;
	private static final int XING_MARKER_OFFSET_1 = 13;
	private static final int XING_MARKER_OFFSET_2 = 21;
	private static final int XING_MARKER_OFFSET_3 = 36;

	protected int bufferLength;
	private int xingOffset = -1;
	private int startOffset = -1;
	private int endOffset = -1;
	private int frameCount = 0;
	private Map<Integer, MutableInteger> bitrates = new HashMap<Integer, MutableInteger>();
	private int xingBitrate;
	private double bitrate = 0;
	private String channelMode;
	private String emphasis;
	private String layer;
	private String modeExtension;
	private int sampleRate;
	private boolean copyright;
	private boolean original;
	private String version;
	private ID3v1 id3v1Tag;
	private ID3v2 id3v2Tag;
	private byte[] customTag;
	private boolean scanFile;

	protected Mp3File() {
	}

	public Mp3File(String filename) throws IOException, UnsupportedTagException, InvalidDataException {
		this(filename, DEFAULT_BUFFER_LENGTH, true);
	}

	public Mp3File(String filename, int bufferLength) throws IOException, UnsupportedTagException, InvalidDataException {
		this(filename, bufferLength, true);
	}

	public Mp3File(String filename, boolean scanFile) throws IOException, UnsupportedTagException, InvalidDataException {
		this(filename, DEFAULT_BUFFER_LENGTH, scanFile);
	}

	public Mp3File(String filename, int bufferLength, boolean scanFile) throws IOException, UnsupportedTagException, InvalidDataException {
		super(filename);
		init(bufferLength, scanFile);
	}

	public Mp3File(File file) throws IOException, UnsupportedTagException, InvalidDataException {
		this(file, DEFAULT_BUFFER_LENGTH, true);
	}

	public Mp3File(File file, int bufferLength) throws IOException, UnsupportedTagException, InvalidDataException {
		this(file, bufferLength, true);
	}

	public Mp3File(File file, int bufferLength, boolean scanFile) throws IOException, UnsupportedTagException, InvalidDataException {
		super(file);
		init(bufferLength, scanFile);
	}

	private void init(int bufferLength, boolean scanFile) throws IOException, UnsupportedTagException, InvalidDataException {
		if (bufferLength < MINIMUM_BUFFER_LENGTH + 1) throw new IllegalArgumentException("Buffer too small");

		this.bufferLength = bufferLength;
		this.scanFile = scanFile;

		RandomAccessFile randomAccessFile = new RandomAccessFile(file.getPath(), "r");

		try {
			initId3v1Tag(randomAccessFile);
			scanFile(randomAccessFile);
			if (startOffset < 0) {
				throw new InvalidDataException("No mpegs frames found");
			}
			initId3v2Tag(randomAccessFile);
			if (scanFile) {
				initCustomTag(randomAccessFile);
			}
		} finally {
			randomAccessFile.close();
		}
	}

	protected int preScanFile(RandomAccessFile file) {
		byte[] bytes = new byte[AbstractID3v2Tag.HEADER_LENGTH];
		try {
			file.seek(0);
			int bytesRead = file.read(bytes, 0, AbstractID3v2Tag.HEADER_LENGTH);
			if (bytesRead == AbstractID3v2Tag.HEADER_LENGTH) {
				try {
					ID3v2TagFactory.sanityCheckTag(bytes);
					return AbstractID3v2Tag.HEADER_LENGTH + BufferTools.unpackSynchsafeInteger(bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 1], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 2], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 3]);
				} catch (NoSuchTagException e) {
					// do nothing
				} catch (UnsupportedTagException e) {
					// do nothing
				}
			}
		} catch (IOException e) {
			// do nothing
		}
		return 0;
	}

	private void scanFile(RandomAccessFile file) throws IOException, InvalidDataException {
		byte[] bytes = new byte[bufferLength];
		int fileOffset = preScanFile(file);
		file.seek(fileOffset);
		boolean lastBlock = false;
		int lastOffset = fileOffset;
		while (!lastBlock) {
			int bytesRead = file.read(bytes, 0, bufferLength);
			if (bytesRead < bufferLength) lastBlock = true;
			if (bytesRead >= MINIMUM_BUFFER_LENGTH) {
				while (true) {
					try {
						int offset = 0;
						if (startOffset < 0) {
							offset = scanBlockForStart(bytes, bytesRead, fileOffset, offset);
							if (startOffset >= 0 && ! scanFile) {
								return;
							}
							lastOffset = startOffset;
						}
						offset = scanBlock(bytes, bytesRead, fileOffset, offset);
						fileOffset += offset;
						file.seek(fileOffset);
						break;
					} catch (InvalidDataException e) {
						if (frameCount < 2) {
							startOffset = -1;
							xingOffset = -1;
							frameCount = 0;
							bitrates.clear();
							lastBlock = false;
							fileOffset = lastOffset + 1;
							if (fileOffset == 0) throw new InvalidDataException("Valid start of mpeg frames not found", e);
							file.seek(fileOffset);
							break;
						}
						return;
					}
				}
			}
		}
	}

	private int scanBlockForStart(byte[] bytes, int bytesRead, int absoluteOffset, int offset) {
		while (offset < bytesRead - MINIMUM_BUFFER_LENGTH) {
			if (bytes[offset] == (byte)0xFF && (bytes[offset + 1] & (byte)0xE0) == (byte)0xE0) {
				try {
					MpegFrame frame = new MpegFrame(bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3]);
					if (xingOffset < 0 && isXingFrame(bytes, offset)) {
						xingOffset = absoluteOffset + offset;
						xingBitrate = frame.getBitrate();
						offset += frame.getLengthInBytes();
					} else {
						startOffset = absoluteOffset + offset;
						channelMode = frame.getChannelMode();
						emphasis = frame.getEmphasis();
						layer = frame.getLayer();
						modeExtension = frame.getModeExtension();
						sampleRate = frame.getSampleRate();
						version = frame.getVersion();
						copyright = frame.isCopyright();
						original = frame.isOriginal();
						frameCount++;
						addBitrate(frame.getBitrate());
						offset += frame.getLengthInBytes();
						return offset;
					}
				} catch (InvalidDataException e) {
					offset++;
				}
			} else {
				offset++;
			}
		}
		return offset;
	}

	private int scanBlock(byte[] bytes, int bytesRead, int absoluteOffset, int offset) throws InvalidDataException {
		while (offset < bytesRead - MINIMUM_BUFFER_LENGTH) {
			MpegFrame frame = new MpegFrame(bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3]);
			sanityCheckFrame(frame, absoluteOffset + offset);
			int newEndOffset = absoluteOffset + offset + frame.getLengthInBytes() - 1;
			if (newEndOffset < maxEndOffset()) {
				endOffset = absoluteOffset + offset + frame.getLengthInBytes() - 1;
				frameCount++;
				addBitrate(frame.getBitrate());
				offset += frame.getLengthInBytes();
			} else {
				break;
			}
		}
		return offset;
	}

	private int maxEndOffset() {
		int maxEndOffset = (int)getLength();
		if (hasId3v1Tag()) maxEndOffset -= ID3v1Tag.TAG_LENGTH;
		return maxEndOffset;
	}

	private boolean isXingFrame(byte[] bytes, int offset) {
		if (bytes.length >= offset + XING_MARKER_OFFSET_1 + 3) {
			if ("Xing".equals(BufferTools.byteBufferToStringIgnoringEncodingIssues(bytes, offset + XING_MARKER_OFFSET_1, 4))) return true;
			if ("Info".equals(BufferTools.byteBufferToStringIgnoringEncodingIssues(bytes, offset + XING_MARKER_OFFSET_1, 4))) return true;
			if (bytes.length >= offset + XING_MARKER_OFFSET_2 + 3) {
				if ("Xing".equals(BufferTools.byteBufferToStringIgnoringEncodingIssues(bytes, offset + XING_MARKER_OFFSET_2, 4))) return true;
				if ("Info".equals(BufferTools.byteBufferToStringIgnoringEncodingIssues(bytes, offset + XING_MARKER_OFFSET_2, 4))) return true;
				if (bytes.length >= offset + XING_MARKER_OFFSET_3 + 3) {
					if ("Xing".equals(BufferTools.byteBufferToStringIgnoringEncodingIssues(bytes, offset + XING_MARKER_OFFSET_3, 4))) return true;
					if ("Info".equals(BufferTools.byteBufferToStringIgnoringEncodingIssues(bytes, offset + XING_MARKER_OFFSET_3, 4))) return true;
				}
			}
		}
		return false;
	}

	private void sanityCheckFrame(MpegFrame frame, int offset) throws InvalidDataException {
		if (sampleRate != frame.getSampleRate()) throw new InvalidDataException("Inconsistent frame header");
		if (! layer.equals(frame.getLayer())) throw new InvalidDataException("Inconsistent frame header");
		if (! version.equals(frame.getVersion())) throw new InvalidDataException("Inconsistent frame header");
		if (offset + frame.getLengthInBytes() > getLength()) throw new InvalidDataException("Frame would extend beyond end of file");
	}

	private void addBitrate(int bitrate) {
		Integer key = new Integer(bitrate);
		MutableInteger count = bitrates.get(key);
		if (count != null) {
			count.increment();
		} else {
			bitrates.put(key, new MutableInteger(1));
		}
		this.bitrate = ((this.bitrate * (frameCount - 1)) + bitrate) / frameCount;
	}

	private void initId3v1Tag(RandomAccessFile file) throws IOException {
		byte[] bytes = new byte[ID3v1Tag.TAG_LENGTH];
		file.seek(getLength() - ID3v1Tag.TAG_LENGTH);
		int bytesRead = file.read(bytes, 0, ID3v1Tag.TAG_LENGTH);
		if (bytesRead < ID3v1Tag.TAG_LENGTH) throw new IOException("Not enough bytes read");
		try {
			id3v1Tag = new ID3v1Tag(bytes);
		} catch (NoSuchTagException e) {
			id3v1Tag = null;
		}
	}

	private void initId3v2Tag(RandomAccessFile file) throws IOException, UnsupportedTagException, InvalidDataException {
		if (xingOffset == 0 || startOffset == 0) {
			id3v2Tag = null;
		} else {
			int bufferLength;
			if (hasXingFrame()) bufferLength = xingOffset;
			else bufferLength = startOffset;
			byte[] bytes = new byte[bufferLength];
			file.seek(0);
			int bytesRead = file.read(bytes, 0, bufferLength);
			if (bytesRead < bufferLength) throw new IOException("Not enough bytes read");
			try {
				id3v2Tag = ID3v2TagFactory.createTag(bytes);
			} catch (NoSuchTagException e) {
				id3v2Tag = null;
			}
		}
	}

	private void initCustomTag(RandomAccessFile file) throws IOException {
		int bufferLength = (int)(getLength() - (endOffset + 1));
		if (hasId3v1Tag()) bufferLength -= ID3v1Tag.TAG_LENGTH;
		if (bufferLength <= 0) {
			customTag = null;
		}
		else {
			customTag = new byte[bufferLength];
			file.seek(endOffset + 1);
			int bytesRead = file.read(customTag, 0, bufferLength);
			if (bytesRead < bufferLength) throw new IOException("Not enough bytes read");
		}
	}

	public int getFrameCount() {
		return frameCount;
	}

	public int getStartOffset() {
		return startOffset;
	}

	public int getEndOffset() {
		return endOffset;
	}

	public long getLengthInMilliseconds() {
		double d = 8 * (endOffset - startOffset);
		return (long)((d / bitrate) + 0.5);
	}

	public long getLengthInSeconds() {
		return ((getLengthInMilliseconds() + 500) / 1000);
	}

	public boolean isVbr() {
		return bitrates.size() > 1;
	}

	public int getBitrate() {
		return (int)(bitrate + 0.5);
	}

	public Map<Integer, MutableInteger> getBitrates() {
		return bitrates;
	}

	public String getChannelMode() {
		return channelMode;
	}

	public boolean isCopyright() {
		return copyright;
	}

	public String getEmphasis() {
		return emphasis;
	}

	public String getLayer() {
		return layer;
	}

	public String getModeExtension() {
		return modeExtension;
	}

	public boolean isOriginal() {
		return original;
	}

	public int getSampleRate() {
		return sampleRate;
	}

	public String getVersion() {
		return version;
	}

	public boolean hasXingFrame() {
		return (xingOffset >= 0);
	}

	public int getXingOffset() {
		return xingOffset;
	}

	public int getXingBitrate() {
		return xingBitrate;
	}

	public boolean hasId3v1Tag() {
		return id3v1Tag != null;
	}

	public ID3v1 getId3v1Tag() {
		return id3v1Tag;
	}

	public void setId3v1Tag(ID3v1 id3v1Tag) {
		this.id3v1Tag = id3v1Tag;
	}

	public void removeId3v1Tag() {
		this.id3v1Tag = null;
	}

	public boolean hasId3v2Tag() {
		return id3v2Tag != null;
	}

	public ID3v2 getId3v2Tag() {
		return id3v2Tag;
	}

	public void setId3v2Tag(ID3v2 id3v2Tag) {
		this.id3v2Tag = id3v2Tag;
	}

	public void removeId3v2Tag() {
		this.id3v2Tag = null;
	}

	public boolean hasCustomTag() {
		return customTag != null;
	}

	public byte[] getCustomTag() {
		return customTag;
	}

	public void setCustomTag(byte[] customTag) {
		this.customTag = customTag;
	}

	public void removeCustomTag() {
		this.customTag = null;
	}

	public void save(String newFilename) throws IOException, NotSupportedException {
		if (file.compareTo(new File(newFilename)) == 0) {
			throw new IllegalArgumentException("Save filename same as source filename");
		}
		RandomAccessFile saveFile = new RandomAccessFile(newFilename, "rw");
		try {
			if (hasId3v2Tag()) {
				saveFile.write(id3v2Tag.toBytes());
			}
			saveMpegFrames(saveFile);
			if (hasCustomTag()) {
				saveFile.write(customTag);
			}
			if (hasId3v1Tag()) {
				saveFile.write(id3v1Tag.toBytes());
			}
		} finally {
			saveFile.close();
		}
	}

	private void saveMpegFrames(RandomAccessFile saveFile) throws IOException {
		int filePos = xingOffset;
		if (filePos < 0) filePos = startOffset;
		if (filePos < 0) return;
		if (endOffset < filePos) return;
		RandomAccessFile randomAccessFile = new RandomAccessFile(this.file.getPath(), "r");
		byte[] bytes = new byte[bufferLength];
		try {
			randomAccessFile.seek(filePos);
			while (true) {
				int bytesRead = randomAccessFile.read(bytes, 0, bufferLength);
				if (filePos + bytesRead <= endOffset) {
					saveFile.write(bytes, 0, bytesRead);
					filePos += bytesRead;
				} else {
					saveFile.write(bytes, 0, endOffset - filePos + 1);
					break;
				}
			}
		} finally {
			randomAccessFile.close();
		}
	}
}


text:
<!DOCTYPE html>

    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css"href="css/bootstrap.min.css" >


text:
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */
package org.hibernate.validator.test.constraints.boolcomposition.localconstrval;

import java.lang.annotation.Retention;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.ConstraintComposition;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.hibernate.validator.constraints.CompositionType.ALL_FALSE;

/**
 * Test mode for HV-390.
 *
 * Composed annotation with both boolean operators and local constraintValidator.
 * It checks in a very complicated way whether a string is strictly shorter than 11 chars.
 *
 * @author Federico Mancini
 * @author Dag Hovland
 */
@Constraint(validatedBy = SmallStringValidator.class)
@ConstraintComposition(ALL_FALSE)
@Size(min = 10, max = 10)
@Retention(RUNTIME)
public @interface SmallString {
	String message() default "Not a small string";

	Class<?>[] groups() default { };

	Class<? extends Payload>[] payload() default { };
}


text:
import sys
sys.path.append('../../')

import cvxpy as cvx
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
from PIL import Image
import cv2

import matlab.engine
import StringIO

############################################################

# Start the engine
eng = matlab.engine.start_matlab()

# Add test path
eng.addpath(r'../../apps/poisson', nargout=0)
eng.addpath(r'../../apps/poisson/images', nargout=0)
eng.addpath(r'../../apps/poisson/kernels', nargout=0)
eng.addpath(r'../../apps/poisson/hyperlaplacian_code', nargout=0)

# Call script function
# eng.deblurring_launch_single_poisson(nargout=0)
#result = np.array( eng.workspace['I_deconv'] )
# print(result.shape, ' ', result.dtype)

result = np.array(eng.deblurring_launch_single_poisson_test())
print(result.shape, ' ', result.dtype)

# Show result
plt.ion()
plt.figure()
imgplot = plt.imshow(result, interpolation="nearest", clim=(0.0, 1.0))
imgplot.set_cmap('gray')
plt.title('Original Image')
plt.show()

# Stop enging
eng.quit()

# Wait until done
raw_input("Press Enter to continue...")


text:
#ifndef SCENELAYERS_H_
#define SCENELAYERS_H_

#include <Assets/TerrainModel.h>
#include "ITestScene.h"

class SceneLayers : public ITestScene
{
public:
    SceneLayers(const TestSceneData & data);
    virtual bool initialise(const Ptr<Core::VirtualFileSystem> & pVFS, const Ptr<Window::Window> & pRenderWindow);
    virtual void update(double elapsed);
    virtual void render();

protected:
    void generateLayers(bool diffuseOn, bool normalOn, bool detailOn, int32 size);

    Ptr<Core::VirtualFileSystem>              _pVFS;
    Ptr<Assets::TerrainModel>                 _pModel;
    Core::List<Ptr<Renderer::IMeshInstance> > _nodes;
};

#endif /* SCENEGRASS_H_ */


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Figlotech.Core.Interfaces {

    public interface IContextProvider {
        void Set(String name, Object value);
        Object Get(String name);

        T Get<T>(string name);
    }
}


text:
package io.jenetics.engine;

import static java.util.Objects.requireNonNull;

import java.util.function.Function;

import io.jenetics.Alterer;
import io.jenetics.AltererResult;
import io.jenetics.Gene;
import io.jenetics.Genotype;
import io.jenetics.Phenotype;
import io.jenetics.util.MSeq;
import io.jenetics.util.Seq;

/**
 * @author Franz Wilhelmstötter
 */
public final class RepairingAlterer<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements Alterer<G, C>
{
	private final Alterer<G, C> _adoptee;
	private final Function<Genotype<G>, Genotype<G>> _repair;

	public RepairingAlterer(
		final Alterer<G, C> adoptee,
		final Function<Genotype<G>, Genotype<G>> repair
	) {
		_adoptee = requireNonNull(adoptee);
		_repair = requireNonNull(repair);
	}

	@Override
	public AltererResult<G, C> alter(
		final Seq<Phenotype<G, C>> population,
		final long generation
	) {
		final AltererResult<G, C> result = _adoptee.alter(population, generation);

		final MSeq<Phenotype<G, C>> pop = MSeq.of(population);
		for (int i = 0, n = pop.size(); i < n; ++i) {
			if (!pop.get(i).isValid()) {
				pop.set(i, repair(pop.get(i)));
			}
		}
		return new AltererResult<>(pop.toISeq(), result.alterations());
	}

	private Phenotype<G, C> repair(final Phenotype<G, C> pt) {
		return Phenotype.of(
			_repair.apply(pt.genotype()),
			pt.generation()
		);
	}

}


text:
class Credit < Adjustment
   before_save :inverse_amount

  def calculate_adjustment
    if adjustment_source
      case adjustment_source_type
      when "Coupon"
       calculate_coupon_credit
      else
        super
      end
    end
  end

  def inverse_amount
    x = self.amount > 0 ? -1 : 1
    self.amount = self.amount * x
  end

  private
  def calculate_coupon_credit
    return 0 if order.line_items.empty?
    amount = adjustment_source.calculator.compute(order)
    amount = order.item_total if amount > order.item_total
    amount
  end
end


text:
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <dimen name="banner_height">200dp</dimen>

    <dimen name="page_divider">10dp</dimen>
    <dimen name="page_divider_thin">1dp</dimen>
    <dimen name="web_progress_height">3dp</dimen>

    <!-- Album相关 -->
    <dimen name="albumitem_height">100dp</dimen>
    <dimen name="albumitem_content_height">80dp</dimen>
    <dimen name="albumitem_image_height">20dp</dimen>

    <!-- 图片选择界面Item相关 -->
    <dimen name="sticky_item_horizontalSpacing">2dp</dimen>
    <dimen name="sticky_item_verticalSpacing">2dp</dimen>
    <dimen name="collection_photo_toolbar_height">45dp</dimen>

    <dimen name="btn_submit_height">50dp</dimen>

    <dimen name="height_top_bar">46dp</dimen>
    <dimen name="padding_search_bar">5dp</dimen>
    <dimen name="height_row_weixin">59dp</dimen>
    <dimen name="size_avatar">48dp</dimen>
    <dimen name="margin_chat_activity">5dp</dimen>
    <dimen name="field_textsize">14sp</dimen>
    <dimen name="field_margin_right">5dp</dimen>
    <dimen name="call_button_padding_vertical">10dp</dimen>
    <dimen name="call_button_padding_left">25dp</dimen>
    <dimen name="call_button_padding_right">25dp</dimen>
    <dimen name="sidebar_text_size">10dp</dimen>
     <dimen name="image_thumbnail_size">100dp</dimen>
    <dimen name="image_thumbnail_spacing">1dp</dimen>
    <dimen name="chat_nick_margin_left">12dp</dimen>
    <dimen name="chat_nick_text_size">13sp</dimen>
</resources>


text:
package de.consol.research;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static java.time.LocalDate.now;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

@Path("dashboard")
public class Dashboard {

    private final List<Contract> contracts;

    public Dashboard() {
        contracts = TestDataGenerator.makeTestData();
    }

    // Called by the benchmark to set the number of contracts in the test data.
    public Dashboard(List<Contract> contracts) {
        this.contracts = contracts;
    }

    private Predicate<Contract> last12months = contract -> contract.getDate().isAfter(now().minusYears(1));

    @GET
    @Path("total-sales")
    @Produces(APPLICATION_JSON)
    public int totalSales() {
        return contracts
                .stream()
                .filter(contract -> contract.getDate().isAfter(now().minusYears(1)))
                .mapToInt(Contract::getPriceInCent)
                .sum();
    }

    // Called by the benchmark to compare the throughput with the totalSales() method.
    public int parallelTotalSales() {
        return contracts
                .parallelStream()
                .filter(contract -> contract.getDate().isAfter(now().minusYears(1)))
                .mapToInt(Contract::getPriceInCent)
                .sum();
    }

    @GET
    @Path("sales-by-state")
    @Produces(APPLICATION_JSON)
    public Map<State, Integer> salesByState() {
        return contracts
                .stream()
                .filter(last12months)
                .collect(Collectors.groupingBy(
                        Contract::getState,
                        Collectors.summingInt(Contract::getPriceInCent)));
    }

    @GET
    @Path("sales-per-month")
    @Produces(APPLICATION_JSON)
    public Map<String, Integer> salesByMonth() {
        return contracts
                .stream()
                .filter(last12months)
                .collect(Collectors.groupingBy(
                        c -> c.getDate().getYear() + "-" + c.getDate().getMonthValue(),
                        Collectors.summingInt(Contract::getPriceInCent)));
    }
}


text:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <title>french_xmlfile_example (Fusioncharts::Utf8ExampleController)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />

# File app/controllers/fusioncharts/utf8_example_controller.rb, line 65
  def french_xmlfile_example
    #The common layout for this view
    render(:layout => "layouts/common")
  end


text:
class CreateDonations < ActiveRecord::Migration
  def change
    create_table :donations do |t|
      t.datetime :created_at, null: false
      t.money :amount, null: false
      t.string :email
      t.string :client_ip
    end
  end
end


text:
using SMCLoan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace SMCLoanFacts
{
    public class LoanFacts
    {
        public class PayMethod
        {
            [Fact]
            public void DifferentYearPayment_HaveCorrectDays()
            {
                var loan = new Loan(1000000, 1000000, new Bank() { InterestRate = 7 }, new DateTime(2014, 12, 20));
                var p = loan.Pay(new DateTime(2015, 1, 20), 10000);
                Assert.Equal(31, p.Days);
            }

            [Fact]
            public void FirstPay()
            {
             //arrange
               Bank bank = new Bank("SMC", 7.0);
               Loan loan = new Loan(1000000, 1000000, bank, new DateTime(2015, 7, 1));

               // act
               var d = new DateTime(2015, 8, 1);
               var payment = loan.Pay(d, 10000);

               // assert
               Assert.NotNull(payment);
               Assert.Equal(31, payment.Days);
               Assert.Equal(10000, payment.PaidAmount);
               Assert.Equal(d, payment.PaidDate);

               Assert.Equal(5945.21m, payment.InterestAmount);
               Assert.Equal(4054.79m, payment.PaidPrincipalAmount);
               Assert.Equal(995945.21m, payment.Outstanding);

               Assert.Equal(d, loan.LastPayDate);
               Assert.Equal(995945.21m, loan.Outstanding);
            }

            [Fact]
            public void PaidAmountIsNotEnoughForInterest()
            {
                // arrange
                Loan loan = new Loan(
                  principal: 1000000,
                  outstanding: 1000000,
                  previousOwner: new Bank("TEST BANK", 7.0),
                  lastPayDate: new DateTime(2015, 7, 1)); // 1 Jul, 2015.

                // act
                var d1 = new DateTime(2015, 8, 1);
                var payment1 = loan.Pay(d1, 3000);

                // assert
                Assert.NotNull(d1);
                Assert.Equal(31, payment1.Days);
                Assert.Equal(3000m, payment1.PaidAmount);
                Assert.Equal(d1, payment1.PaidDate);

                Assert.Equal(3000m, payment1.InterestAmount);
                Assert.Equal(0m, payment1.PaidPrincipalAmount);
                Assert.Equal(1000000m, payment1.Outstanding);
                Assert.Equal(2945.21m, loan.Accrued);

                var d2 = new DateTime(2015, 9, 1);
                var payment2 = loan.Pay(d2, 10000);

                Assert.NotNull(payment2);
                Assert.Equal(31, payment2.Days);
                Assert.Equal(10000, payment2.PaidAmount);
                Assert.Equal(d2, payment2.PaidDate);

                Assert.Equal(8890.42m, payment2.InterestAmount);
                Assert.Equal(1109.58m, payment2.PaidPrincipalAmount);
                Assert.Equal(998890.42m, payment2.Outstanding);
                Assert.Equal(0m, loan.Accrued);

                Assert.Equal(d2, loan.LastPayDate);
                Assert.Equal(998890.42m, loan.Outstanding);

            }

            [Fact]
            public void PaidDateIsBeforeLastPayDate_ThrowsException()
            {
                // arrange
                Loan loan = new Loan(
                  principal: 1000000,
                  outstanding: 1000000,
                  previousOwner: new Bank("TEST BANK", 7.0),
                  lastPayDate: new DateTime(2015, 7, 1)); // 1 Jul, 2015.

                var ex = Assert.ThrowsAny<Exception>(() =>
                {
                    loan.Pay(new DateTime(2015, 6, 15), 10000m);
                });

                Assert.Contains("Last Pay Date is 2015-07-01",ex.Message);
            }
        }
    }
}


text:
package meetup.beeno;

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

import meetup.beeno.mapping.IndexMapping;
import meetup.beeno.util.HUtil;
import meetup.beeno.util.PBUtil;

import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

/**
 * Generates updates for secondary index tables based on an update for
 * the indexed table.  This implementation generates index keys based
 * on a primary property value (from which the index is mapped) and an
 * optional date column, which can be appended to the key as is or as
 * an inverted value for reverse chronological sorting.
 *
 * Extra columns mapped in the index will be stored in index table
 * row, along with the primary property value and the date column (if
 * present), and can be used for filtering in scanning the index.
 *
 * TODO: Currently this implementation does not handle removing
 * secondary index rows when a value is removed!!!
 *
 * @author garyh
 *
 */
public class EntityIndexer {
	static final byte[] INDEX_FAMILY = Bytes.toBytes("__idx__");
	static final byte[] INDEX_KEY_COLUMN = Bytes.toBytes("row");
	private static final byte[] ROW_KEY_SEP = Bytes.toBytes("-");

	private static Logger log = Logger.getLogger(EntityIndexer.class);

	private String indexTable;
	private HUtil.HCol primaryField;
	private HUtil.HCol dateField;
	private boolean invertDate = false;
	private List<HUtil.HCol> extraFields;
	private IndexKeyFactory keyFactory = new DefaultKeyFactory();

	public EntityIndexer(IndexMapping mapping) {
		this.indexTable = mapping.getTableName();
		this.primaryField = new HUtil.HCol(mapping.getPrimaryField().getFamily(),
										   mapping.getPrimaryField().getColumn());
		this.dateField = mapping.getDateField();
		this.invertDate = mapping.isDateInverted();
		this.extraFields = mapping.getExtraFields();

		if (mapping.getKeyFactory() != null) {
			try {
				this.keyFactory = mapping.getKeyFactory().newInstance();
			}
			catch (Exception e) {
				throw new IllegalArgumentException("Unable to instantiate key factory class", e);
			}
		}
	}

	public String getIndexTable() { return this.indexTable; }

	/**
	 * Returns a set of updates for this index table, based on the
	 * update to the underlying table.
	 *
	 * @param entityUpdate
	 * @return
	 */
	public List<Put> getIndexUpdates(Put entityUpdate) {
		List<Put> up = new ArrayList<Put>(1);

		/* TODO: handle many index records to one base record.
		 * For example, if the primary property value contains
		 * a collection type, a new index record could be generated
		 * for each value in the collection.
		 */
		Put valUpdate = getUpdateForValue(entityUpdate);
		if (valUpdate != null)
			up.add(valUpdate);

		return up;
	}

	protected Put getUpdateForValue(Put entityUpdate) {
		Put put = null;

		// generate the index row key
		Map<byte[],List<KeyValue>> familyMap = entityUpdate.getFamilyMap();
		// store all the indexed values
		byte[] primaryVal = getValue(this.primaryField.family(), this.primaryField.column(), familyMap);
		if (primaryVal != null && primaryVal.length > 0) {
			Long date = getDateValue(familyMap);
			put = new Put( createIndexKey(primaryVal, date, entityUpdate.getRow()) );

			// sync with base timestamp
			put.setTimeStamp( entityUpdate.getTimeStamp() );
			// store all specified values (when present)
			put.add(this.primaryField.family(), this.primaryField.column(), primaryVal);

			if (this.dateField != null && date != null)
				put.add(this.dateField.family(), this.dateField.column(), PBUtil.toBytes(date));

			// add any extra fields
			for (HUtil.HCol col : this.extraFields) {
				byte[] val = getValue(col.family(), col.column(), familyMap);
				if (val != null)
					put.add(col.family(), col.column(), val);
			}

			// store the orig record key
			put.add(INDEX_FAMILY, INDEX_KEY_COLUMN, entityUpdate.getRow());
		}
		else {
			// no update for primary value, skip
			log.debug("No primary value for index "+getIndexTable());
		}
		return put;
	}

	protected Long getDateValue(Map<byte[],List<KeyValue>> familyMap) {
		Long dateVal = null;

		if (this.dateField != null) {
			byte[] rawDate = getValue(this.dateField.family(),
									  this.dateField.column(), familyMap);
			HDataTypes.HField pbDate = PBUtil.readMessage(rawDate);
			// dates are assumed to be Long!
			if (pbDate != null && pbDate.getType() == HDataTypes.HField.Type.INTEGER)
				dateVal = pbDate.getInteger();
		}

		return dateVal;
	}

	protected byte[] getValue(byte[] family, byte[] col,
							  Map<byte[],List<KeyValue>> familyMap) {
		byte[] val = null;
		List<KeyValue> familyVals = familyMap.get(family);
		if (familyVals != null) {
			for (KeyValue kv : familyVals) {
				if (kv.matchingColumn(family, col)) {
					val = kv.getValue();
					break;
				}
			}
		}
		return val;
	}

	/**
	 * TODO: split this out into a separate interface for different
	 * implementations
	 */
	public byte[] createIndexKey(byte[] primaryVal, Long date, byte[] origRow) {
		if (this.dateField != null && date != null) {
			return this.keyFactory.createKey(primaryVal, origRow, date, this.invertDate);
		}
		else {
			return this.keyFactory.createKey(primaryVal, origRow, null, this.invertDate);
		}
	}

	public static class DefaultKeyFactory implements IndexKeyFactory {

		@Override
		public byte[] createKey( byte[] primaryVal, byte[] rowKey, Long date, boolean invertDate ) {
			byte[] key = new byte[0];
			HDataTypes.HField pbVal = PBUtil.readMessage(primaryVal);
			// order numeric types
			if (pbVal != null && pbVal.getType() == HDataTypes.HField.Type.INTEGER) {
				key = Bytes.add(key, HUtil.toOrderedBytes(pbVal.getInteger()));
			}
			else {
				// just use raw bytes
				key = Bytes.add(key, primaryVal);
			}

			// add on date, if specified
			if (date != null) {
				key = Bytes.add(key,
							Bytes.add(ROW_KEY_SEP, HUtil.toOrderedBytes(date, invertDate)) );
			}

			// add on the original row key to ensure uniqueness
			if (rowKey != null && rowKey.length > 0) {
				key = Bytes.add(key,
						Bytes.add(ROW_KEY_SEP, rowKey));
			}

			return key;
		}
	}

	/**
	 * Generates the same index keys as DefaultKeyFactory, but prefixed with the primary value mod 100 for
	 * better row key distribution.
	 *
	 * This is designed specifically to avoid hot regions arising from frequently used indexes based off of
	 * a sequentially incremented primary value.
	 * @author garyh
	 *
	 */
	public static class ModKeyFactory extends DefaultKeyFactory {
		private static int base = 100;

		public byte[] createKey( byte[] primaryVal, byte[] rowKey, Long date, boolean invertDate) {
			byte[] key = new byte[0];
			HDataTypes.HField pbVal = PBUtil.readMessage(primaryVal);
			// order numeric types
			if (pbVal != null && pbVal.getType() == HDataTypes.HField.Type.INTEGER) {
				long val = pbVal.getInteger();
				key = Bytes.add(Bytes.toBytes(Long.toString( val % base )), ROW_KEY_SEP);
			}
			key = Bytes.add(key, super.createKey(primaryVal, rowKey, date, invertDate));

			return key;
		}
	}
}


text:
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2012 Couchbase, Inc
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

#include "config.h"

#include <iostream>

#include "access_scanner.h"
#include "ep_engine.h"
#include "mutation_log.h"

class ItemAccessVisitor : public VBucketVisitor {
public:
    ItemAccessVisitor(EventuallyPersistentStore &_store, EPStats &_stats,
                      uint16_t sh, std::atomic<bool> &sfin, AccessScanner &aS) :
        store(_store), stats(_stats), startTime(ep_real_time()),
        taskStart(gethrtime()), shardID(sh), stateFinalizer(sfin), as(aS)
    {
        Configuration &conf = store.getEPEngine().getConfiguration();
        name = conf.getAlogPath();
        std::stringstream s;
        s << shardID;
        name = name + "." + s.str();
        prev = name + ".old";
        next = name + ".next";

        log = new MutationLog(next, conf.getAlogBlockSize());
        log->open();
        if (!log->isOpen()) {
            LOG(EXTENSION_LOG_WARNING, "Failed to open access log: '%s'",
                next.c_str());
            delete log;
            log = NULL;
        } else {
            LOG(EXTENSION_LOG_NOTICE, "Attempting to generate new access file "
                "'%s'", next.c_str());
        }
    }

    void visit(StoredValue *v) {
        if (log != NULL && v->isResident()) {
            if (v->isExpired(startTime) || v->isDeleted()) {
                LOG(EXTENSION_LOG_INFO,
                "INFO: Skipping expired/deleted item: %s",v->getKey().c_str());
            } else {
                accessed.push_back(std::make_pair(v->getBySeqno(), v->getKey()));
            }
        }
    }

    void update() {
        if (log != NULL) {
            std::list<std::pair<uint64_t, std::string> >::iterator it;
            for (it = accessed.begin(); it != accessed.end(); ++it) {
                log->newItem(currentBucket->getId(), it->second, it->first);
            }
        }
        accessed.clear();
    }

    bool visitBucket(RCPtr<VBucket> &vb) {
        update();

        if (log == NULL) {
            return false;
        }

        return VBucketVisitor::visitBucket(vb);
    }

    virtual void complete() {
        update();

        if (log != NULL) {
            size_t num_items = log->itemsLogged[ML_NEW];
            log->commit1();
            log->commit2();
            delete log;
            log = NULL;
            ++stats.alogRuns;
            stats.alogRuntime.store(ep_real_time() - startTime);
            stats.alogNumItems.store(num_items);
            stats.accessScannerHisto.add((gethrtime() - taskStart) / 1000);

            if (num_items == 0) {
                LOG(EXTENSION_LOG_NOTICE, "The new access log file is empty. "
                    "Delete it without replacing the current access log...");
                remove(next.c_str());
                updateStateFinalizer();
                return;
            }

            if (access(prev.c_str(), F_OK) == 0 && remove(prev.c_str()) == -1){
                LOG(EXTENSION_LOG_WARNING, "Failed to remove access log file "
                    "'%s': %s", prev.c_str(), strerror(errno));
                remove(next.c_str());
                updateStateFinalizer();
                return;
            }
            LOG(EXTENSION_LOG_NOTICE, "Removed old access log file: '%s'",
                prev.c_str());
            if (access(name.c_str(), F_OK) == 0 && rename(name.c_str(),
                                                          prev.c_str()) == -1){
                LOG(EXTENSION_LOG_WARNING, "Failed to rename access log file "
                    "from '%s' to '%s': %s", name.c_str(), prev.c_str(),
                    strerror(errno));
                remove(next.c_str());
                updateStateFinalizer();
                return;
            }
            LOG(EXTENSION_LOG_NOTICE, "Renamed access log file from '%s' to "
                "'%s'", name.c_str(), prev.c_str());
            if (rename(next.c_str(), name.c_str()) == -1) {
                LOG(EXTENSION_LOG_WARNING, "Failed to rename access log file "
                    "from '%s' to '%s': %s", next.c_str(), name.c_str(),
                    strerror(errno));
                remove(next.c_str());
                updateStateFinalizer();
                return;
            }
            LOG(EXTENSION_LOG_NOTICE, "New access log file '%s' created with "
                "%" PRIu64 " keys", name.c_str(),
                static_cast<uint64_t>(num_items));
        }

        updateStateFinalizer();
    }

private:
    void updateStateFinalizer() {
        if (++(as.completedCount) == store.getVBuckets().getNumShards()) {
            bool inverse = false;
            stateFinalizer.compare_exchange_strong(inverse, true);
        }
    }

    EventuallyPersistentStore &store;
    EPStats &stats;
    rel_time_t startTime;
    hrtime_t taskStart;
    std::string prev;
    std::string next;
    std::string name;
    uint16_t shardID;

    std::list<std::pair<uint64_t, std::string> > accessed;

    MutationLog *log;
    std::atomic<bool> &stateFinalizer;
    AccessScanner &as;
};

AccessScanner::AccessScanner(EventuallyPersistentStore &_store, EPStats &st,
                             const Priority &p, double sleeptime,
                             bool useStartTime, bool completeBeforeShutdown)
    : GlobalTask(&_store.getEPEngine(), p, sleeptime,
                 completeBeforeShutdown),
      completedCount(0),
      store(_store),
      stats(st),
      sleepTime(sleeptime),
      available(true) {

    Configuration &conf = store.getEPEngine().getConfiguration();
    residentRatioThreshold = conf.getAlogResidentRatioThreshold();
    alogPath = conf.getAlogPath();
    double initialSleep = sleeptime;
    if (useStartTime) {
        size_t startTime = conf.getAlogTaskTime();

        /*
         * Ensure startTime will always be within a range of (0, 23).
         * A validator is already in place in the configuration file.
         */
        startTime = startTime % 24;

        /*
         * The following logic calculates the amount of time this task
         * needs to sleep for initially so that it would wake up at the
         * designated task time, note that this logic kicks in only when
         * useStartTime argument in the constructor is set to TRUE.
         * Otherwise this task will wake up periodically in a time
         * specified by sleeptime.
         */
        time_t now = ep_abs_time(ep_current_time());
        struct tm timeNow, timeTarget;
        timeNow = *(gmtime(&now));
        timeTarget = timeNow;
        if (timeNow.tm_hour >= (int)startTime) {
            timeTarget.tm_mday += 1;
        }
        timeTarget.tm_hour = startTime;
        timeTarget.tm_min = 0;
        timeTarget.tm_sec = 0;

        initialSleep = difftime(mktime(&timeTarget), mktime(&timeNow));
        snooze(initialSleep);
    }

    updateAlogTime(initialSleep);
}

bool AccessScanner::run() {
    bool inverse = true;
    if (available.compare_exchange_strong(inverse, false)) {
        store.resetAccessScannerTasktime();
        completedCount = 0;

        bool deleteAccessLogFiles = false;
        /* Get the resident ratio */
        VBucketCountAggregator aggregator;
        VBucketCountVisitor activeCountVisitor(store.getEPEngine(),
                                               vbucket_state_active);
        aggregator.addVisitor(&activeCountVisitor);
        VBucketCountVisitor replicaCountVisitor(store.getEPEngine(),
                                                vbucket_state_replica);
        aggregator.addVisitor(&replicaCountVisitor);

        store.visit(aggregator);

        /* If the resident ratio is greater than 95% we do not want to generate
         access log and also we want to delete previously existing access log
         files*/
        if ((activeCountVisitor.getMemResidentPer() > residentRatioThreshold) &&
            (replicaCountVisitor.getMemResidentPer() > residentRatioThreshold))
        {
            deleteAccessLogFiles = true;
        }
        for (size_t i = 0; i < store.getVBuckets().getNumShards(); i++) {
            if (deleteAccessLogFiles) {
                std::string name(alogPath + "." + std::to_string(i));
                std::string prev(name + ".old");

                LOG(EXTENSION_LOG_NOTICE, "Deleting access log files '%s' and "
                    "'%s' as resident ratio is over %" PRIu8,
                    name.c_str(), prev.c_str(), residentRatioThreshold);

                /* Remove .old shard access log file */
                deleteAlogFile(prev);
                /* Remove shard access log file */
                deleteAlogFile(name);
                stats.accessScannerSkips++;
            } else {
                std::shared_ptr<ItemAccessVisitor> pv(new ItemAccessVisitor(store,
                                                 stats, i, available, *this));
                std::shared_ptr<VBucketVisitor> vbv(pv);
                ExTask task = new VBucketVisitorTask(&store, vbv, i,
                                                     "Item Access Scanner",
                                                     sleepTime, true);
                ExecutorPool::get()->schedule(task, AUXIO_TASK_IDX);
            }
        }
    }
    snooze(sleepTime);
    updateAlogTime(sleepTime);

    return true;
}

void AccessScanner::updateAlogTime(double sleepSecs) {
    struct timeval _waketime;
    gettimeofday(&_waketime, NULL);
    _waketime.tv_sec += sleepSecs;
    stats.alogTime.store(_waketime.tv_sec);
}

std::string AccessScanner::getDescription() {
    return std::string("Generating access log");
}

void AccessScanner::deleteAlogFile(const std::string& fileName) {
    if (access(fileName.c_str(), F_OK) == 0 && remove(fileName.c_str()) == -1) {
        LOG(EXTENSION_LOG_WARNING, "Failed to remove '%s': %s",
            fileName.c_str(), strerror(errno));
    }
}