text:
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
  return getPollyPluginInfo();
}


text:
extern "C"
{
#include <memory.h>
#include <strings.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <errno.h>
#ifdef Linux
#include <dlfcn.h>
typedef int (res_ninit_t)(res_state);
#endif
}

#include "arapi.h"
#include "arlib.h"

#ifdef HPUX
#ifndef IN6ADDRSZ
#define IN6ADDRSZ 16
#endif
extern "C"
{
int res_init(void);
int res_mkquery(int, const char *, int, int, const char *, int, struct rrec *, unsigned char *, int);
int dn_skipname(const unsigned char *, const unsigned char *);
int dn_expand(const unsigned char*, const unsigned char*, const unsigned char*, char*, int);
}
#endif

static char hex_digits[] = {
        '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

#ifdef Linux
static inline void res_init_tls()
{
    /*
     * XXX glibc-2.2.4 maintains a separate _res per thread, but res_init
     * only seems to work for the first thread's _res.
     */
    if (!(_res.options & RES_INIT))
    {
        static res_ninit_t *pres_ninit = (res_ninit_t *)-1;
        if (pres_ninit == (res_ninit_t *)-1)
        {
            res_ninit_t *fn = (res_ninit_t *)dlsym(NULL, "res_ninit");
            if (fn == NULL)
                fn = (res_ninit_t *)dlsym(NULL, "__res_ninit");
            pres_ninit = fn;
        }

        /* If there is a res_ninit, use it to initialize this thread's _res */
        if (pres_ninit != NULL)
            (*pres_ninit)(&_res);

        PR_ASSERT(_res.options & RES_INIT);
    }
}
#endif

// constructor for forward DNS lookup (name resolution)
DNSSession :: DNSSession(const char* name, PRUint16 family, PRIntervalTime tm,
                   char* buf, PRIntn bufsize, PRHostEnt* hentp)
{
    common(tm);
    ipfamily = family;
    hostname = name;
    ip = NULL;
    buffer = buf;
    buffersize = bufsize ;
    hostentp = hentp;
    qtype = FORWARD_LOOKUP;
}

// constructor for reverse DNS lookup (IP resolution)
DNSSession :: DNSSession(const PRNetAddr* addrp, PRIntervalTime tm,
                   char* buf, PRIntn bufsize, PRHostEnt* hentp)
{
    common(tm);
    ipfamily = 0;
    hostname = NULL;
    ip = addrp;
    buffer = buf;
    buffersize = bufsize ;
    hostentp = hentp;
    qtype = REVERSE_LOOKUP;
}

char DNSSession :: decrRetries()
{
    --re_retries;
    return re_retries;
}

void DNSSession :: setRetries(char r)
{
    re_retries = r;
}

datapacket* DNSSession :: getData()
{
    return re_data;
}

hostent* DNSSession :: getArhost()
{
    return &ar_host;
}

hent& DNSSession :: getHent()
{
    return re_he;
}

in6_addr* DNSSession :: getReaddr()
{
    return &re_addr;
}

char DNSSession :: getReaddrtype()
{
    return re_addrtype;
}

void DNSSession :: setReaddrtype(char t)
{
    re_addrtype = t;
}

void DNSSession :: incrSrch()
{
    re_srch ++;
}

char DNSSession :: getSrch() const
{
    return re_srch;
}

void DNSSession :: setType(char t)
{
    re_type = t;
}

char DNSSession :: getType() const
{
    return re_type;
}

void DNSSession :: common(PRIntervalTime tm)
{
    starttime = PR_IntervalNow();
    timeout = tm;
    timedout = PR_FALSE;
    sendfailed = PR_FALSE;
    id = 0;
    lock = PR_NewLock();
    PR_ASSERT(lock);
    cvar = PR_NewCondVar(lock);
    PR_ASSERT(cvar);
    status = DNS_INIT;
    awake = PR_FALSE;

    // query stuff

    re_retries = _res.retry;
    re_sends = 1;
    re_resend  = 1;
    re_he.h_addr_list[0] = 0;
    re_data = NULL;
    re_sent = 0;
    re_srch = 0;
    re_type = 0;
    memset(re_name, 0, sizeof(re_name));
    memset((void*)&re_he, 0, sizeof(re_he));
    memset((void*)&ar_host, 0, sizeof(ar_host));
}

void DNSSession :: setRename(const char* inname)
{
    strncpy(re_name, inname, sizeof(re_name)-1);
}

const char* DNSSession :: getRename() const
{
    return re_name;
}

DNSSession :: ~ DNSSession()
{
    //PR_ASSERT(awake);
    PR_DestroyCondVar(cvar);
    PR_DestroyLock(lock);
    hostname = "grmblll";
    free_packet_list();

    PRInt32 host_cnt = 0;
    while (re_he.h_addr_list[host_cnt])
    {
        free(re_he.h_addr_list[host_cnt++]);
    }

    if (re_he.h_name)
    {
        free(re_he.h_name);
    }

    ar_free_hostent(&ar_host, 0);
}

int DNSSession :: getID() const
{
    return id;
}

char DNSSession :: getSends() const
{
    return re_sends;
}

void DNSSession :: setSends(char c)
{
    re_sends = c;
}

void DNSSession :: incrSends()
{
    re_sends++;
}

char DNSSession :: getResends() const
{
    return re_resend;
}

void DNSSession :: setResends(char c)
{
    re_resend = c;
}

PRUint16 DNSSession :: getFamily() const
{
    return ipfamily;
}

querytype DNSSession :: getQType() const
{
    return qtype;
}

const PRNetAddr* DNSSession :: getIP() const
{
    return ip;
}

PRHostEnt* DNSSession :: getHentp() const
{
    return hostentp;
}

char* DNSSession :: getBuffer() const
{
    return buffer;
}

PRIntn DNSSession :: getBufferSize() const
{
    return buffersize;
}

const char* DNSSession :: getName() const
{
    return hostname;
}

void DNSSession :: setID(int newid)
{
    id = newid;
}

void DNSSession :: addSent(int sent)
{
    re_sent += sent;
}

void DNSSession :: update ( PRIntervalTime now )
{
    if (now - starttime > timeout)
    {
        timedout = PR_TRUE;
    }
}

PRBool DNSSession :: hasTimedOut()
{
    return timedout;
}

void DNSSession :: setSendFailed()
{
    sendfailed = PR_TRUE;
}

PRBool DNSSession :: hasSendFailed()
{
    return sendfailed;
}

void DNSSession :: wait()
{
    PR_Lock(lock);
    while ( PR_FALSE == awake )
    {
        PR_WaitCondVar(cvar, PR_INTERVAL_NO_TIMEOUT);
    }
    awake = PR_FALSE;
    PR_Unlock(lock);
}

void DNSSession :: error()
{
    // set status
    PR_Lock(lock);
    status = DNS_ERROR;
    awake = PR_TRUE;
    PR_NotifyCondVar(cvar);
    PR_Unlock(lock);
}

void DNSSession :: expire()
{
    // set status
    PR_Lock(lock);
    status = DNS_TIMED_OUT;
    awake = PR_TRUE;
    PR_NotifyCondVar(cvar);
    PR_Unlock(lock);
}

void DNSSession :: complete()
{
    // set status
    PR_Lock(lock);
    status = DNS_COMPLETED;
    awake = PR_TRUE;
    PR_NotifyCondVar(cvar);
    PR_Unlock(lock);
}

void DNSSession ::  setStatus(DNSStatus val)
{
    status = val;
}

DNSStatus DNSSession :: getStatus() const
{
    return status;
}

DNSSessionListElement :: DNSSessionListElement(DNSSessionListElement* p,
                              DNSSessionListElement* n, DNSSession* s)
{
    session = s;
    prev = p;
    next = n;
}

DNSSessionListElement :: ~DNSSessionListElement()
{
}

DNSSessionListElement* DNSSessionListElement :: getNext() const
{
    return next;
}

DNSSessionListElement* DNSSessionListElement :: getPrev() const
{
    return prev;
}

DNSSession* DNSSessionListElement :: getSession() const
{
    return session;
}

void DNSSessionListElement :: setNext(DNSSessionListElement* n)
{
    next = n;
}

void DNSSessionListElement :: setPrev(DNSSessionListElement* p)
{
    prev = p;
}

DNSSessionList :: DNSSessionList()
{
    total = 0;
    head = NULL;
}

void DNSSessionList :: add(DNSSession* s)
{
    if (0 == total)
    {
        PR_ASSERT(head == NULL);
        head = new DNSSessionListElement(NULL, NULL, s);
        total ++;
    }
    else
    {
        PR_ASSERT(head);
        DNSSessionListElement* newelement =
            new DNSSessionListElement(NULL, head, s);
        head->setPrev(newelement);
        head = newelement;
        total++;
    }
}

PRBool DNSSessionList :: remove(DNSSession* s)
{
    PR_ASSERT(s);
    PR_ASSERT(head);
    PR_ASSERT(total);
    PRBool removed = PR_FALSE;
    DNSSessionListElement* element = head;
    while (element)
    {
        if (s == element->getSession())
        {
            if (element->getPrev())
            {
                DNSSessionListElement* prev = element->getPrev();
                DNSSessionListElement* next = element->getNext();
                prev->setNext(next);
                if (next)
                {
                    next->setPrev(prev);
                }
                delete(element);
            }
            else
            {
                // first element
                PR_ASSERT(element == head);
                head = element->getNext();
                if (head)
                {
                    head->setPrev(NULL);
                }
                delete(element);
            }
            removed = PR_TRUE;
            break;
        }
        element = element->getNext();
    }
    if (removed)
    {
        total--;
        PR_ASSERT(total>=0);
        if (0 == total)
        {
            PR_ASSERT(NULL == head);
        }
    }
    return removed;
}

PRBool DNSSessionList :: remove(int id) // deletes this session from the list
{
    PRBool removed = PR_FALSE;
    DNSSession* session = lookup(id);
    if (session)
    {
        removed = remove(session);
    }
    return removed;
}

DNSSession* DNSSessionList :: lookup(int id)
{
    DNSSessionListElement* element = head;
    while (element)
    {
        DNSSession* session = element->getSession();
        if (id == session->getID())
        {
            return session;
        }
        element = element->getNext();
    }
    return NULL;
}

PRInt32 DNSSessionList :: numberOfElements() const
{
    return total;
}

DNSSessionHash :: DNSSessionHash()
{
    lock = PR_NewLock();
    total = 0;
    bucket = 0;
    nextitem = NULL;
    lastsession = NULL;

}

// check hash table consistency

void DNSSessionHash :: check()
{
    PR_Lock(lock);
    PRInt32 temptotal = 0;
    PRInt32 i;
    for (i=0;i<HASH_SIZE;i++)
    {
        temptotal += buckets[i].numberOfElements();
    }
    PR_ASSERT(total == temptotal);
    PR_Unlock(lock);
}

DNSSessionHash :: ~ DNSSessionHash()
{
    PR_DestroyLock(lock);
}

void DNSSessionHash :: add(DNSSession* session)
{
    PR_Lock(lock);
    int newbucket = session->getID() % HASH_SIZE;
    buckets[newbucket].add(session);
    total ++ ;
    PR_Unlock(lock);
}

void DNSSessionHash :: remove(DNSSession* session)
{
    PR_Lock(lock);
    int newbucket = session->getID() % HASH_SIZE;
    buckets[newbucket].remove(session);
    total --;
    PR_Unlock(lock);
}

DNSSession* DNSSessionHash :: lookup(int id)
{
    PR_Lock(lock);
    int newbucket = id  % HASH_SIZE;
    DNSSession* session = buckets[newbucket].lookup(id);
    PR_Unlock(lock);
    return session;
}

void DNSSessionHash :: start_enum()
{
    PR_Lock(lock);
    bucket = 0;
    nextitem = buckets[bucket].getHead();
    lastsession = NULL;
}

DNSSession* DNSSessionHash :: getNextSession()
{
    DNSSession* session = NULL;
    while (bucket < HASH_SIZE)
    {
        if (nextitem)
        {
            // found the session
            session = nextitem->getSession();
            // move to the next item for the next call to getNextSession
            nextitem = nextitem->getNext();
            break;
        }
        else
        {
            // try the next bucket
            bucket++;
            if (bucket <HASH_SIZE)
            {
                nextitem = buckets[bucket].getHead();
            }
            else
            {
                // done with all buckets, break out
                break;
            }
        }
    }
    lastsession = session;
    return session;
}

void DNSSessionHash :: end_enum()
{
    PR_Unlock(lock);
    bucket = 0;
    nextitem = NULL;
    lastsession = NULL;
}

void DNSSessionHash :: removeCurrent()
{
    if (lastsession)
    {
        buckets[bucket].remove(lastsession);
        total --;
        lastsession = NULL;
    }
}

DNSSessionListElement* DNSSessionList :: getHead()
{
    return head;
}

Resolver :: Resolver(PRIntervalTime timeout)
{
    curlookups = namelookups = addrlookups = 0;
    ar_vc = 0;

    ok = PR_FALSE;

    ar_default_workerthread_timeout = timeout;

    memset(&ar_reinfo, 0, sizeof(ar_reinfo));

    dnslock = PR_NewLock();

    initcvar = PR_NewCondVar(dnslock);

    awake = PR_FALSE;

    afd = NULL;

    // then create a DNS manager background thread

    PRThread* ar_worker = PR_CreateThread(PR_SYSTEM_THREAD,
        run,
        this,
        PR_PRIORITY_NORMAL,
        PR_LOCAL_THREAD,
        PR_JOINABLE_THREAD,
        0);

    if (ar_worker)
    {
        wait();

        if (!ok)
        {
            PR_JoinThread(ar_worker);
        }
    }
}

void Resolver :: wait()
{
    PR_Lock(dnslock);
    while (PR_FALSE == awake)
    {
        PR_WaitCondVar(initcvar, PR_INTERVAL_NO_TIMEOUT);
    }
    awake = PR_FALSE;
    PR_Unlock(dnslock);
}

void Resolver :: started()
{
    PR_Lock(dnslock);
    awake = PR_TRUE;
    ok = PR_TRUE; // successful start, async DNS is now enabled
    PR_NotifyCondVar(initcvar);
    PR_Unlock(dnslock);
}

void Resolver :: error()
{
    PR_Lock(dnslock);
    awake = PR_TRUE;
    ok = PR_FALSE; // couldn't initialize resolver
    PR_NotifyCondVar(initcvar);
    PR_Unlock(dnslock);
}

PRBool Resolver :: hasStarted() const
{
    return ok;
}

Resolver :: ~Resolver()
{
}

/*
 * ar_open
 *
 * Open a socket to talk to a name server with.
 * Check _res.options to see if we use a TCP or UDP socket.
 */

PRFileDesc* Resolver :: ar_open()
{
    if (NULL == afd && _res.nscount > 0)
    {
        if (_res.options & RES_USEVC)
        {
            struct    sockaddr_in *sip;
            int    i = 0;

            sip = _res.nsaddr_list;
            ar_vc = 1;
            afd = PR_NewTCPSocket();

            /*
                         * Try each name server listed in sequence until we
                         * succeed or run out.
                         */
            while (PR_Connect(afd, (PRNetAddr *)sip++,
                PR_INTERVAL_NO_TIMEOUT))
            {
                PR_Close(afd);
                afd = NULL;
                if (i++ >= _res.nscount)
                    break;
                afd = PR_NewTCPSocket();
            }
        }
        else
        {
            afd = PR_NewUDPSocket();
        }
    }
    return afd;
}

/*
 * ar_close
 *
 * Closes and flags the ARLIB socket as closed.
 */

void Resolver :: ar_close()
{
    PR_Close(afd);
    afd = NULL;
    return;
}

PRIntervalTime Resolver :: getDefaultTimeout()
{
    return ar_default_workerthread_timeout;
}

DNSSessionHash& Resolver :: getHTable()
{
    return DNShash;
}

PRLock* Resolver :: getDnsLock()
{
    return dnslock;
}

void Resolver :: run(void* arg)
{
    Resolver* ptr = (Resolver*) arg;
    ptr->DNS_manager();
}

void Resolver :: DNS_manager()
{
    // setup a connection to the DNS (UDP or TCP)

    PRFileDesc* fd = ar_init(ARES_CALLINIT|ARES_INITSOCK);

    if (!fd)
    {
        error();
        return;
    }

    started();

    PRIntervalTime delay = PR_SecondsToInterval(1);
    PRPollDesc pfd;

    pfd.fd = afd;
    pfd.in_flags = PR_POLL_READ;

    // wake up periodically to expire clients

    while(PR_TRUE)
    {
        switch (PR_Poll(&pfd, 1, delay))
        {
            case -1: // fail
            case 0:  // timeout
                break;

            default:
                // if there's data, let ar_receive figure out where to route it
                // read response from the DNS server
                if (pfd.out_flags & PR_POLL_READ)
                    ar_receive();
                break;
        }

        PRIntervalTime now = PR_IntervalNow();

        DNShash.start_enum();
        DNSSession* session;

        // enumerate session hash table
        while (session = DNShash.getNextSession())
        {
            if (PR_TRUE == session->hasSendFailed() )
            {
                DNShash.removeCurrent(); // remove this session from the hash table
                session->error(); // set status and wake up client
                continue;
            }

            session->update(now); // update remaining time for each session
            if (PR_TRUE == session->hasTimedOut() )
            {
                DNShash.removeCurrent(); // remove this session from the hash table
                session->expire(); // set status and wake up client
                continue;
            }
        }
        DNShash.end_enum();
        DNShash.check();
    }
}

int         ar_default_retry;
const char* AR_DOT = ".";
const PRInt32 DEFAULT_RETRY = 20; /* # of seconds to wait for resolver to return data */
PRFileDesc *afd = NULL;

// legacy code

/*
 * ar_init
 *
 * Initializes the various ARLIB internal variables and related DNS
 * options for res_init().
 *
 * Returns NULL or the pointer to PRFileDesc which contain a socket
 * opened for use with talking to name servers if 0 is passed or
 * ARES_INITSOCK is set.
 */

PRFileDesc* Resolver :: ar_init(int op)
{
    PRFileDesc* ret = NULL;

    if (op & ARES_CALLINIT)
    {
        PR_Lock(dnslock);
        res_init();
#ifdef Linux
        res_init_tls();
#endif
        PR_Unlock(dnslock);
        strcpy(ar_domainname, AR_DOT);
        strncat(ar_domainname, _res.defdname, sizeof(ar_domainname)-2);
    }

    if (op & ARES_INITSOCK)
        ret = afd = ar_open();

    if (op & ARES_INITDEBG)
        _res.options |= RES_DEBUG;

    if (op == 0)
        ret = afd;

    return ret;
}

resstats_t* Resolver :: GetInfo()
{
    return &ar_reinfo;
}

PRBool Resolver :: process(DNSSession* session)
{
    PR_AtomicIncrement(&curlookups);
    PRBool locstatus = PR_FALSE;

    char host[MAXDNAME];
    resinfo_t resi;
    resinfo_t* rp = &resi;
    struct hostent* hp = NULL;
    PRInt32 rv = 0;
    int querytype = 0;

    if (session)
    {
        session->setStatus(DNS_IN_PROCESS);

        if (FORWARD_LOOKUP == session->getQType ())
        {
            PR_AtomicIncrement(&namelookups);

            memset((char *)rp, 0, sizeof(resi));
            ar_reinfo.re_na_look++;
            strncpy(host, session->getName(), 64);
            host[64] = '\0';

            locstatus = PR_TRUE;
            switch(session->getFamily())
            {
                case PR_AF_INET:
                    querytype = T_A;
                    break;
                case PR_AF_INET6:
                    querytype = T_AAAA;
                    break;
                default:
                    locstatus = PR_FALSE;
            }

            // Makes and sends the query
            // the session gets automatically added to the hash table
            if (PR_TRUE == locstatus)
            {
                if (do_query_name(rp, host, session, querytype) == -1)
                {
                    PR_SetError(PR_BAD_ADDRESS_ERROR, 0);
                    locstatus = PR_FALSE;
                }
            }
        }

        else

        if (REVERSE_LOOKUP == session->getQType ())
        {
            PR_AtomicIncrement(&addrlookups);

            memset((char *)rp, 0, sizeof(resi));
            ar_reinfo.re_na_look++;
            const PRNetAddr* addrp = session->getIP();

            locstatus = PR_TRUE;
            switch(addrp->raw.family)
            {
                case PR_AF_INET:
                    rv = do_query_number(rp, (char *) &addrp->inet.ip, session, PR_AF_INET);
                    break;
                case PR_AF_INET6:
                    rv = do_query_number(rp, (char *) &addrp->ipv6.ip, session, PR_AF_INET6);
                    break;
                default:
                    locstatus = PR_FALSE;
                    PR_SetError(PR_BAD_ADDRESS_ERROR, 0);
                    break;
            }
            if (-1 == rv)
            {
                locstatus = PR_FALSE;
            }
        }
    }

    if (PR_TRUE == locstatus)
    {
        locstatus = PR_FALSE;
        int resend;

        do {
            resend = 0;

            session->wait();

            if (DNS_COMPLETED == session->getStatus() )
            {
                // Process answer and return meaningful data to caller.

                if (FORWARD_LOOKUP == session->getQType())
                {
                    if ((hp = ar_answer(session, &resend, NULL, 0)) != NULL)
                    {
                        PRInt32 rv = CopyHostent(session->getHentp(), hp,
                                                 session->getBuffer(),
                                                 session->getBufferSize());
                        if (rv != PR_AR_OK)
                            PR_SetError(rv, 0);
                        else
                            locstatus = PR_TRUE;
                    }
                }
                else
                if (REVERSE_LOOKUP == session->getQType())
                {
                    if (hp = ar_answer(session, &resend, NULL, 0))
                    {
                        PRInt32 rv = CopyHostent(session->getHentp(), hp,
                                                 session->getBuffer(),
                                                 session->getBufferSize());
                        if (rv != PR_AR_OK)
                            PR_SetError(rv, 0);
                        else
                            locstatus = PR_TRUE;
                    }
                }
            }
            else
            if (DNS_TIMED_OUT == session->getStatus())
            {
                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
            }
            else
            {
                PR_SetError(PR_BAD_ADDRESS_ERROR, 0);
            }
        } while (resend);
    }

    PR_AtomicDecrement(&curlookups);
    return locstatus;
}

/*
 * ar_send_res_msg
 *
 * When sending queries to nameservers listed in the resolv.conf file,
 * don't send a query to every one, but increase the number sent linearly
 * to match the number of resends. This increase only occurs if there are
 * multiple nameserver entries in the resolv.conf file.
 * The return value is the number of messages successfully sent to
 * nameservers or -1 if no successful sends.
 */
int Resolver :: ar_send_res_msg(char *msg, int len, int rcount)
{
    int    i;
    int    sent = 0;

#ifdef HPUX
    /* VB: _res on HP is thread specific and needs to be initialized
       on every thread */
    if (!(_res.options & RES_INIT)) {
        res_init();
    }
#endif
#ifdef Linux
    res_init_tls();
#endif

    if (!msg)
        return -1;

    if (_res.options & RES_PRIMARY)
        rcount = 1;
    rcount = (_res.nscount > rcount) ? rcount : _res.nscount;

    if (ar_vc)
    {
        ar_reinfo.re_sent++;
        sent++;
        if (PR_Send(afd, msg, len, 0, PR_INTERVAL_NO_TIMEOUT) == -1)
        {
            int errtmp = errno;
            PR_ASSERT(0);
            (void)PR_Close(afd);
            errno = errtmp;
            afd = NULL;
        }
    }
    else
        for (i = 0; i < rcount; i++)
        {
            struct sockaddr_in sin = _res.nsaddr_list[i];
            // XXX jpierre is this legal ???
            PRNetAddr *paddr = (PRNetAddr *)&sin;
#ifdef AIX
            PRNetAddr addr;
            /* For AIX struct sockaddr_in { uchar_t sin_len;
             *                      sa_family_t sin_family;
             *                      in_port_t sin_port;
             * struct in_addr sin_addr; uchar_t sin_zero[8]; };
             * struct in_addr { in_addr_t s_addr; };
             * typedef uint32_t in_addr_t;
             *
             * In NSPR PRNetAddr is :
             *  union PRNetAddr { struct { PRUint16 family;
             *                             char data[14]; } raw;
             *   struct { PRUint16 family; PRUint16 port;
             *           PRUint32 ip; char pad[8]; } inet; ... }
             */
            PR_ASSERT(sin.sin_family == AF_INET);
            /* xxx strange when request is sent to IPV6 address
             * then also address family is AF_INET
             */
            memset(&addr, 0, sizeof(addr));
            addr.raw.family = sin.sin_family;
            addr.inet.family = sin.sin_family;
            addr.inet.port  = sin.sin_port;
            addr.inet.ip = sin.sin_addr.s_addr;
            memcpy(addr.inet.pad, sin.sin_zero, 8);
            paddr = &addr;
#endif
            if (PR_SendTo(afd, msg, len, 0,
                paddr,
                PR_INTERVAL_NO_TIMEOUT) == len) {
                ar_reinfo.re_sent++;
                sent++;
            }
            else
            {
                PR_ASSERT(0);
            }
        }
    return (sent) ? sent : -1;
}

/*
 * ar_query_name
 *
 * generate a query based on class, type and name.
 */
int Resolver :: ar_query_name(const char *name, int xclass, int type, DNSSession* session)
{
    u_char buf[MAXPACKET];
    int r,s;
    HEADER *hptr;

    memset(buf, 0, sizeof(buf));
    PR_Lock(dnslock);
    r = res_mkquery(QUERY, name, xclass, type, NULL, 0, NULL,
        (unsigned char*)buf, sizeof(buf));
    PR_Unlock(dnslock);

    if (r <= 0)
    {
#ifndef XP_OS2
        h_errno = NO_RECOVERY;
#endif
        return r;
    }
    hptr = (HEADER *)buf;

    /* Add to chains */
    session->setID(ntohs(hptr->id));
    DNShash.add(session);

    session->setStatus(DNS_SENT);
    s = ar_send_res_msg((char*)buf, r, session->getSends());

    if (s == -1)
    {
#ifndef XP_OS2
        h_errno = TRY_AGAIN;
#endif
// XXXcelving race
//      DNShash.remove(session);
//      return -1;
        /*
         * After a DNSSession has been added to the DNShash, only DNS_manager
         * can safely remove it. Flag the DNSSession so that DNS_manager
         * removes it ASAP.
         */
        session->setSendFailed();
    }
    else
    {
        session->addSent(s);
    }
    return 0;
}

int Resolver :: do_query_name(resinfo_t *resi, const char *name, DNSSession* session, int querytype)
{
    char    hname[MAXDNAME];
    int    len;

    len = strlen((char *)strncpy(hname, session->getName(), sizeof(hname)-1));

    /*
     * Store the name passed as the one to lookup and generate other host
     * names to pass onto the nameserver(s) for lookups.
     */
    session->setType(querytype);
    session->setRename(name);

    return (ar_query_name(hname, C_IN, querytype, session));
}

/*
 * do_query_number
 *
 * Use this to do reverse IP# lookups.
 */
int Resolver :: do_query_number(resinfo_t *resi, char *numb, DNSSession* session, int addrtype)
{
    unsigned char* cp = NULL;
    char ipbuf[256];
    int length = 0;

    switch (addrtype)
    {
        case PR_AF_INET:
            /*
             * Generate name in the "in-addr.arpa" domain.  No addings bits to
             * this name to get more names to query!.
             */
            cp = (unsigned char *)numb;
            (void)sprintf(ipbuf,"%u.%u.%u.%u.in-addr.arpa.",
                (unsigned int)(cp[3]), (unsigned int)(cp[2]),
                (unsigned int)(cp[1]), (unsigned int)(cp[0]));
            length = INADDRSZ;
            break;
       case PR_AF_INET6:
            make_ipv6_addr(ipbuf, numb, 1);
            length = IN6ADDRSZ;
            break;
       default:
            return -1;
            break;
    }

    hent& re_he = session->getHent();

    session->setType(T_PTR);
    re_he.h_length = length;
    memcpy((char *)session->getReaddr(), numb, re_he.h_length);
    re_he.h_addr_list[0] = (char *)malloc(length);
    memcpy(re_he.h_addr_list[0], numb, length);
    re_he.h_name = NULL;
    session->setReaddrtype(addrtype);
    re_he.h_addrtype = addrtype;
    return (ar_query_name(ipbuf, C_IN, T_PTR, session));
}

/*
 * ar_resent_query
 *
 * resends a query.
 */
int Resolver :: ar_resend_query(DNSSession* session)
{
    if (!session->getResends())
        return -1;

    switch(session->getType())
    {
    case T_PTR:
        ar_reinfo.re_resends++;
        /* XXXMB - I think this should be:  (char *)&(session->re_addr) */
        return do_query_number(NULL, (char *)(session->getReaddr()), session, session->getReaddrtype());
    case T_A:
        ar_reinfo.re_resends++;
        return do_query_name(NULL, session->getName(), session, T_A);
    case T_AAAA:
        ar_reinfo.re_resends++;
        return do_query_name(NULL, session->getName(), session, T_AAAA);
    default:
        break;
    }

    return -1;
}

/*
 * ar_procanswer
 *
 * process an answer received from a nameserver.
 */

int Resolver :: ar_procanswer(DNSSession* session, HEADER* hptr, u_char* buf, u_char* eob)
{
    u_char* cp = NULL;
    char** alias = NULL;
    int xclass, type = 0, dlen, len, ans = 0, n = 0;
    char** adr = NULL;
    char ar_hostbuf[MAXDNAME];   /* Must be on stack */
    unsigned int ancount = 0 , arcount = 0 , nscount = 0, qdcount = 0;
    int host_cnt = 0;

    /*
     * convert things to be in the right order.
     */
    ancount = ntohs(hptr->ancount);
    arcount = ntohs(hptr->arcount);
    nscount = ntohs(hptr->nscount);
    qdcount = ntohs(hptr->qdcount);

    cp = buf + HFIXEDSZ;
    hent& re_he = session->getHent();
    adr = re_he.h_addr_list;

#ifdef DNS_DEBUG
    Print_query(buf, eob, 1);
#endif

    while (*adr)
    {
        adr++;
        host_cnt++;
    }

    alias = re_he.h_aliases;
    while (*alias)
    {
        alias++;
    }

    /*
     * Skip over the original question.
     */
    while (qdcount > 0)
    {
        qdcount--;
        cp += dn_skipname((const unsigned char*)cp, (const unsigned char*)eob) + QFIXEDSZ;
    }
    /*
     * process each answer sent to us. blech.
     */
    while ((ancount > 0) && (cp < eob))
    {
        ancount--;
        PR_Lock(dnslock);
        n = dn_expand((const unsigned char*)buf, (const unsigned char*)eob, (const unsigned char*)cp, (char*)ar_hostbuf, sizeof(ar_hostbuf));
        PR_Unlock(dnslock);
        cp += n;
        if (n <= 0)
            return ans;

        ans++;
        /*
         * 'skip' past the general dns crap (ttl, xclass, etc) to get
         * the pointer to the right spot.  Some of thse are actually
         * useful so its not a good idea to skip past in one big jump.
         */
        type = (int)GetShort(cp);
        cp += sizeof(short);
        xclass = (int)GetShort(cp);
        cp += sizeof(short);
        /* ttl = */ (void)(PRUint32)GetLong(cp);
        cp += INT32SZ;
        dlen =  (int)GetShort(cp);
        cp += sizeof(short);
        session->setType(type);

        switch(type)
        {
            case T_A :
            {
                re_he.h_addrtype = PR_AF_INET;
                re_he.h_length = INADDRSZ;
                re_he.h_addr_list[host_cnt] = (char *)malloc(INADDRSZ);
                memcpy(re_he.h_addr_list[host_cnt], cp, dlen);
                re_he.h_addr_list[++host_cnt] = NULL;
                cp += dlen;
                len = strlen(ar_hostbuf);
                if (!re_he.h_name)
                {
                    re_he.h_name = (char *)malloc(len+1);
                    (void)strcpy(re_he.h_name, ar_hostbuf);
                }
                break;
            }

            case T_AAAA :
            {
    	        char buf[1024];

                re_he.h_addrtype = PR_AF_INET6;
                re_he.h_length = IN6ADDRSZ;
                re_he.h_addr_list[host_cnt] = (char *)malloc(16);
                memcpy(re_he.h_addr_list[host_cnt], cp, dlen);
                re_he.h_addr_list[++host_cnt] = NULL;
                cp += dlen;
                len = strlen(ar_hostbuf);
                if (!re_he.h_name)
                {
                    re_he.h_name = (char *)malloc(len+1);
                    (void)strcpy(re_he.h_name, ar_hostbuf);
                }
                break;
            }

            case T_PTR :
            {
                PR_Lock(dnslock);
                n = dn_expand((const unsigned char*)buf, (const unsigned char*)eob, (const unsigned char*)cp, (char*)ar_hostbuf, sizeof(ar_hostbuf));
                PR_Unlock(dnslock);
                if (n < 0)
                {
                    cp += n;
                    continue;
                }
                cp += n;
                len = strlen(ar_hostbuf)+1;
                /*
                 * copy the returned hostname into the host name
                 * or alias field if there is a known hostname
                 * already.
                 */
                if (!re_he.h_name)
                {
                    re_he.h_name = (char *)malloc(len);
                    (void)strcpy(re_he.h_name, ar_hostbuf);
                }
                else
                {
                    *alias = (char*)malloc(len);
                    if (!*alias)
                        return -1;
                    (void)strcpy(*alias++, ar_hostbuf);
                    *alias = NULL;
                }
                break;
            }

            case T_CNAME :
            {
                cp += dlen;
                if (alias >= &(re_he.h_aliases[MAXALIASES-1]))
                    continue;
                n = strlen(ar_hostbuf)+1;
                *alias = (char*)malloc(n);
                if (!*alias)
                    return -1;
                (void)strcpy(*alias++, ar_hostbuf);
                *alias = NULL;
                break;
            }

            default :
            {
                break;
            }
        }
    }

    return ans;
}

/*
 * ar_receive
 *
 * Get the answer from the DNS server and  wakeup the corresponding
 * thread which made the request.
 */

void Resolver :: ar_receive()
{
    char ar_rcvbuf[HFIXEDSZ + MAXPACKET];
    HEADER* hptr = NULL;        /* Name Server query header */
    DNSSession* session = NULL;
    int rc = 0;
    datapacket* newpkt = NULL;
    unsigned int id = 0;

    if (ar_vc)
        rc = PR_Recv(afd, ar_rcvbuf, HFIXEDSZ+MAXPACKET, 0,
            PR_INTERVAL_NO_TIMEOUT);
    else
        rc = PR_Read(afd, ar_rcvbuf,  HFIXEDSZ+MAXPACKET);

    ar_reinfo.re_replies++;

    /* Create a reslist_pkt_t to queue against the reslist */
    newpkt = new datapacket();
    PR_ASSERT(newpkt);

    newpkt->next = NULL;
    if (rc >0)
    {
        // packet received

	    /*
	     * Wakeup corresponding thread.
	     */
	    hptr = (HEADER *)ar_rcvbuf;
	    /*
	     * convert things to be in the right order.
	     */
	    id = ntohs(hptr->id);
	    /*
	     * response for an id which we have already received an answer for
	     * just ignore this response.
	     */
	    session = DNShash.lookup(id);
	    if (!session)
        {
            delete(newpkt);
	        /* Request no longer in queue, so just return. */
	        return;
	    }

        newpkt->buflen = rc;
        memset(newpkt, 0, HFIXEDSZ + MAXPACKET);
        memcpy(newpkt, ar_rcvbuf, rc);
    }
    else
    {
        // no packet received
        newpkt->buflen = 0;
    }

    // Append new data packet to end of known data packets
    session->reslist_add_pkt(newpkt);

    session->setStatus(DNS_RCVD);

    // Wakeup corresponding thread.

    DNShash.remove(session); // remove this completed session from the hash table
    session->complete();
}

void ar_free_hostent (struct hostent *hp, int flag)
{
    char **s = NULL;

    if (hp->h_name)
        (void)free(hp->h_name);

    if (s = hp->h_aliases) {
        /* The individual aliases were allocated in one chunk... */
        if (*s)
        {
            (void)free(*s++);
            s++;
        }
        (void)free(hp->h_aliases);
    }
    if (s = hp->h_addr_list)
    {
        /* The individual addresses were allocated in one chunk... */
        if (*s)
        {
            (void)free(*s);
            s++;
        }
        (void)free(hp->h_addr_list);
    }

    if (flag)
        free(hp);
}

/*
 * ar_answer
 *
 * Get an answer from a DNS server and process it.  If a query is found to
 * which no answer has been given to yet, copy its 'info' structure back
 * to where "reip" points and return a pointer to the hostent structure.
 */
struct hostent* Resolver :: ar_answer(DNSSession* session, int* resend, char* reip, int size)
{
    hent& re_he = session->getHent();
    HEADER* hptr = NULL;        /* Name Server query header */
    struct hostent* hp = NULL;
    char** s = NULL;
    unsigned long* adr = NULL;
    int i = 0 , n = 0 , a = 0;
    char** cp = NULL;

    if (session->getData() == NULL)
    {
        /* If re_data is NULL, we don't have a packet; must have timed out */
        if (session->decrRetries() <= 0)
        {
            ar_reinfo.re_timeouts++;
            *resend = 0;
        }
        else
        {
            session->incrSends();
            if (!ar_resend_query(session))
                *resend = 1;
        }
        return NULL;
    }

    *resend = 0;

    hptr = (HEADER *)session->getData()->rcvbuf;
    /*
     * response for an id which we have already received an answer for
     * just ignore this response.
     */
    if ((hptr->rcode != NOERROR) || (hptr->ancount == 0)) {
#ifndef XP_OS2
        switch (hptr->rcode) {
        case NXDOMAIN:
            h_errno = HOST_NOT_FOUND;
            break;
        case SERVFAIL:
            h_errno = TRY_AGAIN;
            break;
        case NOERROR:
            h_errno = NO_DATA;
            break;
        case FORMERR:
        case NOTIMP:
        case REFUSED:
        default:
            h_errno = NO_RECOVERY;
            break;
        }
#endif
        ar_reinfo.re_errors++;
        /*
        ** If a bad error was returned, we stop here and dont send
        ** send any more (no retries granted).
        */
        if (h_errno != TRY_AGAIN)
        {
            session->setResends(0);
            session->setRetries(0);
        }
        goto getres_err;
    }

    a = ar_procanswer(session,
        hptr,
        (u_char*)session->getData()->rcvbuf,
        (u_char*)session->getData()->rcvbuf + session->getData()->buflen);
    if (a <= 0)
    {
        *resend=0;
        return NULL;
    }

    if ((session->getType() == T_PTR) && (_res.options & RES_CHECKPTR))
    {
        /*
         * For reverse lookups on IP#'s, lookup the name that is given
         * for the ip# and return with that as the official result.
         * -avalon
         */
        switch(session->getReaddrtype())
        {
            case PR_AF_INET:
                session->setType(T_A);
                break;
            case PR_AF_INET6:
                session->setType(T_AAAA);
                break;
            default:
                return NULL;
        }
        /*
         * Clean out the list of addresses already set, even though
         * there should only be one :)
         */
        adr = (unsigned long *)re_he.h_addr_list;
        while (*adr)
            *adr++ = 0L;
        /*
         * Lookup the name that we were given for the ip#
         */
        ar_reinfo.re_na_look++;
        session->setRename(re_he.h_name);
        re_he.h_name = NULL;
        re_he.h_addr_list[0] = 0;
        session->setRetries(_res.retry);
        session->setSends(1);
        session->setResends(1);
        ar_reinfo.re_na_look++;
        if (!ar_query_name(session->getName(), C_IN, session->getType(), session))
            *resend = 1;
        return NULL;
    }

    if (session && re_he.h_name == NULL)
        goto getres_err;

/*
    if (reip && session->re_rinfo.ri_ptr && size)
        memcpy(reip, session->re_rinfo.ri_ptr,
            MIN(session->re_rinfo.ri_size, size));
*/
    /*
     * Clean up structure from previous usage.
     */
    hp = session->getArhost();
    memset((char *)hp, 0, sizeof(*hp));

    /*
     * Setup and copy details for the structure we return a pointer to.
     */
    hp->h_addrtype = re_he.h_addrtype;
    hp->h_length = re_he.h_length;
    hp->h_name = (char *)malloc(strlen(re_he.h_name)+1);
    (void)strcpy(hp->h_name, re_he.h_name);
    /*
     * Count IP#'s.
     */
    n = 0;
    for (cp = re_he.h_addr_list; *cp; cp++)
        n++;

    s = hp->h_addr_list = (char **)malloc((n + 1) * sizeof(char *));
    if (n)
    {
        char buf[512];
        *s = (char *)malloc(n * re_he.h_length);
        memcpy(*s, re_he.h_addr_list[0],
            re_he.h_length);

        s++;
        for (i = 1; i < n; i++, s++) {
            *s = hp->h_addr + i * re_he.h_length;
            memcpy(*s, (char *)re_he.h_addr_list[i],
                re_he.h_length);
        }
    }
    *s = NULL;
    /*
     * Count CNAMEs
     */
    for (i = 0, n = 0; i < MAXADDRS; i++, n++)
        if (!re_he.h_aliases[i])
            break;

    s = hp->h_aliases = (char **)malloc((n + 1) * sizeof(char *));
    for (i = 0; i < n; i++)
    {
        *s++ = re_he.h_aliases[i];
        re_he.h_aliases[i] = NULL;
    }
    *s = NULL;

    return hp;

getres_err:
    if (session)
    {
/*
        if (reip && session->re_rinfo.ri_ptr && size)
            memcpy(reip, session->re_rinfo.ri_ptr,
                MIN(session->re_rinfo.ri_size, size));
*/
        if ((h_errno != TRY_AGAIN) &&
            (_res.options & (RES_DNSRCH|RES_DEFNAMES) ==
             (RES_DNSRCH|RES_DEFNAMES) )) {
            if (_res.dnsrch[session->getSrch()]) {
                session->setRetries(_res.retry);
                session->setSends(1);
                session->setResends(1);
                if (!ar_resend_query(session)) {
                    *resend = 1;
                    session->incrSrch();
                }
            }
        }
    }

    return NULL;
}

void DNSSession :: free_packet_list()
{
    datapacket* ptr = re_data;

    while (ptr)
    {
        datapacket* next = ptr->next;
        delete(ptr);
        ptr = next;
    }
}

/* Caller must hold the reslist->re_lock
 * add a pkt to the reslist
 */
void DNSSession :: reslist_add_pkt(datapacket *pkt)
{
    datapacket* ptr = re_data;

    /* must add to tail of list */

    while (ptr && ptr->next)
        ptr = ptr->next;

    pkt->next = NULL;
    if (ptr)
    {
        // add packet to the end of the list
        ptr->next = pkt;
    }
    else
    {
        // first packet
        re_data = pkt;
    }
}

/*
 * Mostly stolen from bind 9.0.1 code from byaddr.c
 */
void make_ipv6_addr(char  * textname, char * address, int nibble) {
    unsigned char *bytes;
    char * cp;
    int i;

    bytes = (unsigned char *)(address);

    if (nibble) {
            cp = textname;
            for (i = 15; i >= 0; i--) {
                    *cp++ = hex_digits[bytes[i] & 0x0f];
                    *cp++ = '.';
                    *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
                    *cp++ = '.';
            }
            strcpy(cp, "ip6.int.");
    } else {
            cp = textname;
            *cp++ = '\\';
            *cp++ = '[';
            *cp++ = 'x';
            for (i = 0; i < 16; i += 2) {
                    *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
                    *cp++ = hex_digits[bytes[i] & 0x0f];
                    *cp++ = hex_digits[(bytes[i+1] >> 4) & 0x0f];
                    *cp++ = hex_digits[bytes[i+1] & 0x0f];
            }
            strcpy(cp, "].ip6.arpa.");
    }
}

/*
** Allocate space from the buffer, aligning it to "align" before doing
** the allocation. "align" must be a power of 2.
*/
char *Alloc(PRIntn amount, char **bufp, PRIntn *buflenp, PRIntn align)
{
    char *buf = *bufp;
    PRIntn buflen = *buflenp;

    if (align && ((long)buf & (align - 1)))
    {
        PRIntn skip = align - ((ptrdiff_t)buf & (align - 1));
        if (buflen < skip) {
            return 0;
        }
        buf += skip;
        buflen -= skip;
    }
    if (buflen < amount)
    {
        return 0;
    }
    *bufp = buf + amount;
    *buflenp = buflen - amount;
    return buf;
}

/*
** Copy a hostent, and all of the memory that it refers to into
** (hopefully) stacked buffers.
*/

PRInt32 CopyHostent(PRHostEnt *to, struct hostent *from, char *buf, PRIntn bufsize)
{
    PRIntn len = 0, na = 0;
    char** ap = NULL;

    /* Do the easy stuff */
    to->h_addrtype = from->h_addrtype;
    to->h_length = from->h_length;

    /* Copy the official name */
    if (!from->h_name)
        return PR_BAD_ADDRESS_ERROR;
    len = strlen(from->h_name) + 1;
    to->h_name = Alloc(len, &buf, &bufsize, 0);
    if (!to->h_name)
        return PR_BAD_ADDRESS_ERROR;
    memcpy(to->h_name, from->h_name, len);

    /* Count the aliases, then allocate storage for the pointers */
    for (na = 1, ap = from->h_aliases; *ap != 0; na++, ap++);
    to->h_aliases = (char**) Alloc(na * sizeof(char*), &buf, &bufsize,
        sizeof(char**));
    if (!to->h_aliases)
        return PR_BAD_ADDRESS_ERROR;

    /* Copy the aliases, one at a time */
    for (na = 0, ap = from->h_aliases; *ap != 0; na++, ap++)
    {
        if (*ap)
        {
            len = strlen(*ap) + 1;
            to->h_aliases[na] = Alloc(len, &buf, &bufsize, 0);
            if (!to->h_aliases[na]) return PR_BAD_ADDRESS_ERROR;
            memcpy(to->h_aliases[na], *ap, len);
        } else
        {
            to->h_aliases[na] = 0;
        }
    }
    to->h_aliases[na] = 0;

    /* Count the addresses, then allocate storage for the pointers */
    for (na = 1, ap = from->h_addr_list; *ap != 0; na++, ap++);
    to->h_addr_list = (char**) Alloc(na * sizeof(char*), &buf, &bufsize,
        sizeof(char**));
    if (!to->h_addr_list) return PR_BAD_ADDRESS_ERROR;

    /* Copy the addresses, one at a time */
    for (na = 0, ap = from->h_addr_list; *ap != 0; na++, ap++)
    {
        if (*ap)
        {
            to->h_addr_list[na] = Alloc(to->h_length, &buf, &bufsize, 0);
            if (!to->h_addr_list[na]) return PR_BAD_ADDRESS_ERROR;
            memcpy(to->h_addr_list[na], *ap, to->h_length);
        } else
        {
            to->h_addr_list[na] = 0;
        }
    }
    to->h_addr_list[na] = 0;
    return PR_AR_OK;
}

PRInt32 Resolver :: getNameLookups() const
{
    return namelookups;
}

PRInt32 Resolver :: getAddrLookups() const
{
    return addrlookups;
}

PRInt32  Resolver :: getCurrentLookups() const
{
    return curlookups;
}

short GetShort(unsigned char* in)
{
    char myarray[2];
    myarray[0] = in[0];
    myarray[1] = in[1];
    return ntohs(*(short*)myarray);
}

long GetLong(unsigned char* in)
{
    char myarray[4];
    myarray[0] = in[0];
    myarray[1] = in[1];
    myarray[2] = in[2];
    myarray[3] = in[3];
    return ntohl(*(long*)myarray);
}

redpajama_set_name: RedPajamaGithub


text:
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
  <OperationRequest>
    <HTTPHeaders>

    </HTTPHeaders>
    <RequestId>e2cc8969-ce87-4b63-b90f-cee3a928b591</RequestId>
    <Arguments>
      <Argument Name="Operation" Value="ItemLookup"/>
      <Argument Name="Service" Value="AWSECommerceService"/>
      <Argument Name="Signature" Value="XXXXXXXXXXXXXXX"/>
      <Argument Name="AssociateTag" Value="XXXXXXXXXXXXXXX"/>
      <Argument Name="Version" Value="2011-08-01"/>
      <Argument Name="ItemId" Value="9780747532743"/>
      <Argument Name="IdType" Value="ISBN"/>
      <Argument Name="AWSAccessKeyId" Value="XXXXXXXXXXXXXXX"/>
      <Argument Name="Timestamp" Value="2011-11-09T08:25:50Z"/>
      <Argument Name="ResponseGroup" Value="???"/>
      <Argument Name="SearchIndex" Value="All"/>
    </Arguments>
    <RequestProcessingTime>0.0027530000000000</RequestProcessingTime>
  </OperationRequest>
  <Items>
    <Request>
      <IsValid>False</IsValid>
      <ItemLookupRequest>
        <IdType>ISBN</IdType>
        <ItemId>9780747532743</ItemId>
        <ResponseGroup>???</ResponseGroup>
        <SearchIndex>All</SearchIndex>
        <VariationPage>All</VariationPage>
      </ItemLookupRequest>
      <Errors>
        <Error>
          AWS.InvalidEnumeratedParameter
          <Message>The value you specified for ResponseGroup is invalid. Valid values include [
    		'Tags', 'Help', 'ListMinimum', 'VariationSummary', 'VariationMatrix',
    		'TransactionDetails', 'VariationMinimum', 'VariationImages',
    		'PartBrandBinsSummary', 'CustomerFull', 'CartNewReleases',
    		'ItemIds', 'SalesRank', 'TagsSummary', 'Fitments',
    		'Subjects', 'Medium', 'ListmaniaLists',
    		'PartBrowseNodeBinsSummary', 'TopSellers', 'Request',
    		'HasPartCompatibility', 'PromotionDetails', 'ListFull',
    		'Small', 'Seller', 'OfferFull', 'Accessories',
    		'VehicleMakes', 'MerchantItemAttributes', 'TaggedItems',
    		'VehicleParts', 'BrowseNodeInfo', 'ItemAttributes',
    		'PromotionalTag', 'VehicleOptions', 'ListItems', 'Offers',
    		'TaggedGuides', 'NewReleases', 'VehiclePartFit',
    		'OfferSummary', 'VariationOffers', 'CartSimilarities',
    		'Reviews', 'ShippingCharges', 'ShippingOptions', 'EditorialReview',
    		'CustomerInfo', 'PromotionSummary', 'BrowseNodes',
    		'PartnerTransactionDetails', 'VehicleYears', 'SearchBins',
    		'VehicleTrims', 'Similarities', 'AlternateVersions',
    		'SearchInside', 'CustomerReviews', 'SellerListing',
    		'OfferListings', 'Cart', 'TaggedListmaniaLists',
    		'VehicleModels', 'ListInfo', 'Large', 'CustomerLists',
    		'Tracks', 'CartTopSellers', 'Images', 'Variations',
    	'RelatedItems','Collections'
    	].</Message>
        </Error>
        <Error>
          AWS.InvalidResponseGroup
          <Message>Your ResponseGroup parameter is invalid. Valid response groups for ItemLookup requests include ['Request','ItemIds','Small','Medium','Large','Offers','OfferFull','OfferSummary','OfferListings','PromotionSummary','PromotionDetails','Variations','VariationImages','VariationMinimum','VariationSummary','TagsSummary','Tags','VariationMatrix','VariationOffers','ItemAttributes','MerchantItemAttributes','Tracks','Accessories','EditorialReview','SalesRank','BrowseNodes','Images','Similarities','Subjects','Reviews','ListmaniaLists','SearchInside','PromotionalTag','AlternateVersions','Collections','ShippingCharges','RelatedItems','ShippingOptions'].</Message>
        </Error>
      </Errors>
    </Request>
  </Items>
</ItemLookupResponse>


text:
var THREEx = THREEx || {}

THREEx.ArMarkerControls = function(context, object3d, parameters){
	var _this = this
	this.context = context
	// handle default parameters
	this.parameters = {
		// size of the marker in meter
		size : parameters.debug !== undefined ? parameters.debug : 1,
		// type of marker - ['pattern', 'barcode', 'unknown' ]
		type : parameters.type !== undefined ? parameters.type : 'unknown',
		// url of the pattern - IIF type='pattern'
		patternUrl : parameters.patternUrl !== undefined ? parameters.patternUrl : null,
		// value of the barcode - IIF type='barcode'
		barcodeValue : parameters.barcodeValue !== undefined ? parameters.barcodeValue : null,
		// change matrix mode - [modelViewMatrix, cameraTransformMatrix]
		changeMatrixMode : parameters.changeMatrixMode !== undefined ? parameters.changeMatrixMode : 'modelViewMatrix',
		// enable motion prediction - default to true
		motionPredictionEnabled : parameters.motionPredictionEnabled !== undefined ? parameters.motionPredictionEnabled : false,
	}

	// sanity check
	var possibleValues = ['pattern', 'barcode', 'unknown' ]
	console.assert(possibleValues.indexOf(this.parameters.type) !== -1, 'illegal value', this.parameters.type)
	var possibleValues = ['modelViewMatrix', 'cameraTransformMatrix' ]
	console.assert(possibleValues.indexOf(this.parameters.changeMatrixMode) !== -1, 'illegal value', this.parameters.changeMatrixMode)

	this.markerId = null

        // create the marker Root
	this.object3d = object3d
	this.object3d.name = 'Marker Root'
	// this.object3d.matrixAutoUpdate = false;
	this.object3d.visible = false

	// init the object which follow the marker - directly out of artoolkit
	this._markerObject3D = new THREE.Group
	this._markerObject3D.matrixAutoUpdate = false;

	// init MotionPredictionControls
	this._motionPredictionControls = new THREEx.MotionPredictionControls(this.object3d)
	this._motionPredictionControls.enabled = this.parameters.motionPredictionEnabled

	// add this marker to artoolkitsystem
	context.addMarker(this)

	// wait for arController to be initialized before going on with the init
	var delayedInitTimerId = setInterval(function(){
		// check if arController is init
		var arController = _this.context.arController
		if( arController === null )	return
		// stop looping if it is init
		clearInterval(delayedInitTimerId)
		delayedInitTimerId = null
		// launch the _postInit
		_this._postInit()
	}, 1000/50)
	return

}

THREEx.ArMarkerControls.prototype._postInit = function(){
	var _this = this
	// var markerObject3D = this.object3d;
	var markerObject3D = this._markerObject3D;
	// check if arController is init
	var arController = this.context.arController
	console.assert(arController !== null )

	// start tracking this pattern
	if( _this.parameters.type === 'pattern' ){
                arController.loadMarker(_this.parameters.patternUrl, function(markerId) {
			_this.markerId = markerId
                        arController.trackPatternMarkerId(_this.markerId, _this.parameters.size);
                });
	}else if( _this.parameters.type === 'barcode' ){
		_this.markerId = _this.parameters.barcodeValue
		arController.trackBarcodeMarkerId(_this.markerId, _this.parameters.size);
	}else if( _this.parameters.type === 'unknown' ){
		_this.markerId = null
	}else{
		console.log(false, 'invalid marker type', _this.parameters.type)
	}

	// listen to the event
	arController.addEventListener('getMarker', function(event){

		if( event.data.type === artoolkit.PATTERN_MARKER && _this.parameters.type === 'pattern' ){
			if( _this.markerId === null )	return
			if( event.data.marker.idPatt === _this.markerId ) onMarkerFound()
		}else if( event.data.type === artoolkit.BARCODE_MARKER && _this.parameters.type === 'barcode' ){
			// console.log('BARCODE_MARKER idMatrix', event.data.marker.idMatrix, _this.markerId )
			if( _this.markerId === null )	return
			if( event.data.marker.idMatrix === _this.markerId )  onMarkerFound()
		}else if( event.data.type === artoolkit.UNKNOWN_MARKER && _this.parameters.type === 'unknown'){
			onMarkerFound()
		}

		function onMarkerFound(){
			// mark object as visible
			_this.object3d.visible = true

			// data.matrix is the model view matrix
			var modelViewMatrix = new THREE.Matrix4().fromArray(event.data.matrix)

			// change markerObject3D.matrix based on parameters.changeMatrixMode
			if( _this.parameters.changeMatrixMode === 'modelViewMatrix' ){
				markerObject3D.matrix.copy(modelViewMatrix)
			}else if( _this.parameters.changeMatrixMode === 'cameraTransformMatrix' ){
				var cameraTransformMatrix = new THREE.Matrix4().getInverse( modelViewMatrix )
				markerObject3D.matrix.copy(cameraTransformMatrix)
			}else {
				console.assert(false)
			}

			// decompose the matrix into .position, .quaternion, .scale
			markerObject3D.matrix.decompose(markerObject3D.position, markerObject3D.quaternion, markerObject3D.scale)

			// set this position as a knownPosition in _motionPredictionControls
			_this._motionPredictionControls.setKnownPose(markerObject3D.position, markerObject3D.quaternion, markerObject3D.scale)

		// console.log('onMarkerFound')

			// console.log('position', _this.object3d.position)
			// console.log('quaternion', _this.object3d.quaternion)

			// _this.object3d.updateMatrix(true)
			// _this.object3d.updateMatrixWorld(true)
			// _this._markerObject3D.updateMatrix(true)
			// _this._markerObject3D.updateMatrixWorld(true)
		}
	})
}

THREEx.ArMarkerControls.dispose = function(){
	this.context.removeMarker(this)

	// TODO remove the event listener if needed
	// unloadMaker ???
}

//////////////////////////////////////////////////////////////////////////////
//		Code Separator
//////////////////////////////////////////////////////////////////////////////
THREEx.ArMarkerControls.prototype.update = function(){
	// update motion prediction controls
	this._motionPredictionControls.update()
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<link rel="stylesheet" type="text/css" href="../../css/style.css">
	<link rel="icon" href="../../img/icon.png">
	<title>QRazyBox - Help Page</title>

QRazyBox Help

				<input type="text" placeholder="Search here...">

Help › Editor & Painter › Painter Basic Usage

Painter Basic Usage

(The image of Painter tools)

Color Palettes

There are three colors to draw data bits of QR code that available in Painter: Black, White, and Grey.

		Black and white color represent binary value of data bits (black = 1 and white = 0).

		Then, grey color represents damaged or missing data of QR code.

You can switch the active color by clicking color palette you want to activated.

Drawing Methods

There are two methods that you can use to draw QR code using color palette.
First, called Toggling, where you color modules by clicking one by one. In addition, when you color on an already colored module with the same color as your current color palette, it will color alternately between that color and grey color (this only works on black and white palettes).

The second is Dragging. This method will let you color modules freely by dragging cursor to the modules you want to color.

Flood Fill

Besides using the above methods, you can also color modules quickly by using Flood fill. It use the same concept as bucket fill tool that perhaps you have seen in common Paint-like application. It will color all similarly-colored and connected modules.

Flood fill is toggleable, and will color according to the currently active color palette.

			<button class="left" onclick="window.location='index.html';">Prev : Editor & Painter</button>
			<button class="right" onclick="window.location='format-info-pattern.html';">Next : Format Info Pattern</button>


text:
using System;

using Org.BouncyCastle.Math;

namespace Org.BouncyCastle.Asn1.X509
{
	/**
	 * Class for containing a restriction object subtrees in NameConstraints. See
	 * RFC 3280.
	 *
	 *

	 *
	 *       GeneralSubtree ::= SEQUENCE
	 *       {
	 *         baseName                    GeneralName,
	 *         minimum         [0]     BaseDistance DEFAULT 0,
	 *         maximum         [1]     BaseDistance OPTIONAL
	 *       }
	 *

	 *
	 * @see org.bouncycastle.asn1.x509.NameConstraints
	 *
	 */
	public class GeneralSubtree
		: Asn1Encodable
	{
		private readonly GeneralName	baseName;
		private readonly DerInteger		minimum;
		private readonly DerInteger		maximum;

		private GeneralSubtree(
			Asn1Sequence seq)
		{
			baseName = GeneralName.GetInstance(seq[0]);

			switch (seq.Count)
			{
				case 1:
					break;
				case 2:
				{
					Asn1TaggedObject o = Asn1TaggedObject.GetInstance(seq[1]);
					switch (o.TagNo)
					{
						case 0:
							minimum = DerInteger.GetInstance(o, false);
							break;
						case 1:
							maximum = DerInteger.GetInstance(o, false);
							break;
						default:
							throw new ArgumentException("Bad tag number: " + o.TagNo);
					}
					break;
				}
				case 3:
				{
					{
						Asn1TaggedObject oMin = Asn1TaggedObject.GetInstance(seq[1]);
						if (oMin.TagNo != 0)
							throw new ArgumentException("Bad tag number for 'minimum': " + oMin.TagNo);
						minimum = DerInteger.GetInstance(oMin, false);
					}

					{
						Asn1TaggedObject oMax = Asn1TaggedObject.GetInstance(seq[2]);
						if (oMax.TagNo != 1)
							throw new ArgumentException("Bad tag number for 'maximum': " + oMax.TagNo);
						maximum = DerInteger.GetInstance(oMax, false);
					}

					break;
				}
				default:
					throw new ArgumentException("Bad sequence size: " + seq.Count);
			}
		}

		/**
		 * Constructor from a given details.
		 *
		 * According RFC 3280, the minimum and maximum fields are not used with any
		 * name forms, thus minimum MUST be zero, and maximum MUST be absent.
		 *

		 * If minimum is null, zero is assumed, if
		 * maximum is null, maximum is absent.

		 *
		 * @param baseName
		 *            A restriction.
		 * @param minimum
		 *            Minimum
		 *
		 * @param maximum
		 *            Maximum
		 */
		public GeneralSubtree(
			GeneralName	baseName,
			BigInteger	minimum,
			BigInteger	maximum)
		{
			this.baseName = baseName;
			if (minimum != null)
			{
				this.minimum = new DerInteger(minimum);
			}
			if (maximum != null)
			{
				this.maximum = new DerInteger(maximum);
			}
		}

		public GeneralSubtree(
			GeneralName baseName)
			: this(baseName, null, null)
		{
		}

		public static GeneralSubtree GetInstance(
			Asn1TaggedObject	o,
			bool				isExplicit)
		{
			return new GeneralSubtree(Asn1Sequence.GetInstance(o, isExplicit));
		}

		public static GeneralSubtree GetInstance(
			object obj)
		{
			if (obj == null)
			{
				return null;
			}

			if (obj is GeneralSubtree)
			{
				return (GeneralSubtree) obj;
			}

			return new GeneralSubtree(Asn1Sequence.GetInstance(obj));
		}

		public GeneralName Base
		{
			get { return baseName; }
		}

		public BigInteger Minimum
		{
			get { return minimum == null ? BigInteger.Zero : minimum.Value; }
		}

		public BigInteger Maximum
		{
			get { return maximum == null ? null : maximum.Value; }
		}

		/**
		 * Produce an object suitable for an Asn1OutputStream.
		 *
		 * Returns:
		 *
		 *

		 *       GeneralSubtree ::= SEQUENCE
		 *       {
		 *         baseName                    GeneralName,
		 *         minimum         [0]     BaseDistance DEFAULT 0,
		 *         maximum         [1]     BaseDistance OPTIONAL
		 *       }
		 *

		 *
		 * @return a DERObject
		 */
		public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector(baseName);

			if (minimum != null && minimum.Value.SignValue != 0)
			{
				v.Add(new DerTaggedObject(false, 0, minimum));
			}

			if (maximum != null)
			{
				v.Add(new DerTaggedObject(false, 1, maximum));
			}

			return new DerSequence(v);
		}
	}
}


text:
set(AS_RELEASE 2.29.2)
set(AS_SHASUM a983ea7e429f18672f9b6f927c98f580679ce19c)

ExternalProject_Add(lib-angelscript-${AS_RELEASE}
        URL "http://www.angelcode.com/angelscript/sdk/files/angelscript_${AS_RELEASE}.zip"
        URL_HASH SHA1=${AS_SHASUM}
        SOURCE_SUBDIR angelscript/projects/cmake
        CMAKE_ARGS "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
        UPDATE_COMMAND ""
        INSTALL_COMMAND ""
        )

ExternalProject_Get_Property(lib-angelscript-${AS_RELEASE} source_dir)
set(AS_INCLUDE_DIR "${source_dir}/angelscript/include")
set(AS_LIB_DIR "${source_dir}/angelscript/lib")

add_library(angelscript STATIC IMPORTED)
set_property(TARGET angelscript PROPERTY IMPORTED_LOCATION "${AS_LIB_DIR}/libAngelscript.a")
add_dependencies(angelscript lib-angelscript-${AS_RELEASE})


text:
/*global module:false*/

module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({

        clean: {
            release: ['css']
        },

        topcoat: {
            options: {
                browsers: ['last 2 versions']
            },
            compile: {
                files: [{
                        src: 'test/fixtures/notification.css',
                        dest: 'css/notification.css'
                    }
                ]
            }
        },

        cssmin: {
            minify: {
                expand: true,
                cwd: 'css',
                src: ['*.css', '!*.min.css'],
                dest: 'css',
                ext: '.min.css'
            },
        },

        simplemocha: {
            all: {
                src: ['test/*.test.js']
            }
        }
    });

    // These plugins provide necessary tasks.
    grunt.loadNpmTasks('grunt-topcoat');
    grunt.loadNpmTasks('grunt-simple-mocha');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-clean');

    grunt.registerTask('default', ['clean', 'build', 'test', 'release']);
    grunt.registerTask('build', ['topcoat']);
    grunt.registerTask('test', ['simplemocha']);
    grunt.registerTask('release', ['cssmin']);

};


text:
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "/events/index.html.erb" do
  include EventsHelper

  before(:each) do
    assigns[:events] = [
      stub_model(Event,
        :from_block => 1,
        :to_block => 1,
        :repeatFrequency => "value for repeatFrequency",
        :repeatInterval => 1,
        :lecture => 1,
        :room => 1,
        :is_blocked => false
      ),
      stub_model(Event,
        :from_block => 1,
        :to_block => 1,
        :repeatFrequency => "value for repeatFrequency",
        :repeatInterval => 1,
        :lecture => 1,
        :room => 1,
        :is_blocked => false
      )
    ]
  end

  it "renders a list of events" do
    render
    response.should have_tag("tr>td", 1.to_s, 2)
    response.should have_tag("tr>td", 1.to_s, 2)
    response.should have_tag("tr>td", "value for repeatFrequency".to_s, 2)
    response.should have_tag("tr>td", 1.to_s, 2)
    response.should have_tag("tr>td", 1.to_s, 2)
    response.should have_tag("tr>td", 1.to_s, 2)
    response.should have_tag("tr>td", false.to_s, 2)
  end
end


text:
<?php
namespace deArcane\Debugger\Strategies;

class Dev extends Strategy implements IEndpointStrategy{

	public function stop(){
		$this->clearBuffers();
		$this->renderReport();
		$this->shutDownApplication();
	}

	public function handle(){
		$this->clearBuffers();
		$this->renderReport();
		$this->shutDownApplication();
	}
}


text:
module WeatherObject
  module Data
    class NullPressure < NullConvertableUnits
      def mb
        nil
      end

      def in
        nil
      end
    end
  end
end


text:
from .text import StixTextTransform

class StixCsvTransform(StixTextTransform):
    """Generate a CSV formatted summary of observables from a STIX package.

    This class can be used to generate a delimited text dump of the
    observable fields contained in a STIX package. Output is grouped by
    the object type contained in the observable.

    Args:
        package: the STIX package to process
        separator: a string separator used in the text output
        include_header: a boolean value that indicates whether or not header
            information should be included in the text output
        header_prefix: a string prepended to header lines in the output
        include_observable_id: a boolean value indicating whether or not
            the output should include the observable's UUID
        include_condition: a boolean value indicating whether or not the
            output should include additional fields containing the Cybox
            string matching condition (which may be empty)
    """

    OBJECT_FIELDS = {
        'Address': ['category', 'address_value'],
        'DomainName': ['value'],
        'EmailMessage': [
            'header.from_.address_value',
            'header.to.address_value',
            'header.subject',
            'attachments.object_reference',
        ],
        'File': ['file_name', 'hashes.type_', 'hashes.simple_hash_value'],
        'HTTPSession': ['http_request_response.http_client_request.' +
                        'http_request_header.parsed_header.user_agent'],
        'Mutex': ['name'],
        'SocketAddress': [
            'ip_address.category',
            'ip_address.address_value',
            'port.port_value',
            'port.layer4_protocol',
        ],
        'URI': ['value'],
        'WinRegistryKey': ['hive', 'key', 'values.name', 'values.data'],
    }

    OBJECT_HEADER_LABELS = {
        'Address': ['category', 'address'],
        'DomainName': ['domain'],
        'EmailMessage': ['fromaddr', 'toaddr', 'subject', 'attachment_ref'],
        'File': ['file_name', 'hash_type', 'hashes'],
        'HTTPSession': ['user_agent'],
        'Mutex': ['mutex'],
        'SocketAddress': [
            'category',
            'address',
            'port_value',
            'port_protocol',
        ],
        'URI': ['uri'],
        'WinRegistryKey': ['hive', 'key', 'name', 'data'],
    }

    STRING_CONDITION_FIELDS = {
        'DomainName': ['value'],
        'EmailMessage': [
            'header.from_.address_value',
            'header.to.address_value',
            'header.subject',
        ],
        'File': ['file_name'],
        'HTTPSession': ['http_request_response.http_client_request.' +
                        'http_request_header.parsed_header.user_agent'],
        'Mutex': ['name'],
        'URI': ['value'],
        'WinRegistryKey': ['hive', 'key', 'values.name', 'values.data'],
    }

    def __init__(self, package, separator='|', include_header=True,
                 header_prefix='#', include_observable_id=True,
                 include_condition=True):
        super(StixCsvTransform, self).__init__(
            package, separator, include_header, header_prefix,
        )
        self._include_observable_id = include_observable_id
        self._include_condition = include_condition

    def _include_condition_with_property(self, object_type, property_):
        if (self._include_condition and
                object_type in self.STRING_CONDITION_FIELDS and
                property_ in self.STRING_CONDITION_FIELDS[object_type]):
            return True
        else:
            return False

    def header(self):
        title = self.package_title(default=self._package.id_)
        tlp = self.package_tlp()

        if title or tlp:
            header = self._header_prefix
            if title:
                header += ' {}'.format(title)
            if tlp:
                header += ' (TLP:{})'.format(tlp)
            header += '\n\n'
        else:
            header = ''
        return header

    def header_for_object_type(self, object_type):
        header_values = ['id'] if self._include_observable_id else []
        header_values.extend(self.OBJECT_HEADER_LABELS[object_type])
        if self._include_condition:
            index = 1
            for field in self.OBJECT_FIELDS[object_type]:
                if self._include_condition_with_property(object_type, field):
                    condition_label = header_values[index] + '_condition'
                    header_values.insert(index+1, condition_label)
                    index += 1
                index += 1
        header = '{} {} observables\n'.format(self._header_prefix, object_type)
        header += '{} {}\n'.format(self._header_prefix,
                                   self.join(header_values))
        return header

    def text_for_fields(self, fields, object_type):
        field_values = []
        if self.OBJECT_FIELDS and object_type in self.OBJECT_FIELDS:
            for field in self.OBJECT_FIELDS[object_type]:
                field_value = fields.get(field, 'None')
                field_values.append(field_value)
                if self._include_condition_with_property(object_type, field):
                    c_field = self._condition_key_for_field(field)
                    condition = fields.get(c_field, 'None')
                    field_values.append(condition)
        return self.join(field_values)

    def text_for_object_type(self, object_type):
        text = ''
        if object_type in self._observables:
            for observable in self._observables[object_type]:
                id_ = observable['id']
                for field in observable['fields']:
                    if self._include_observable_id:
                        text += '{}{}'.format(id_, self._separator)
                    text += self.text_for_fields(field, object_type) + '\n'
        if text:
            text += '\n'
        return text


text:
var sudo = require( 'sudo' );
var options = {
  cachePassword: true,
  prompt: 'Enter your sudo password to continue installation of global npm modules!'
};

var npmInstall = [
  'npm', 'install', '-g'
];

var dependencies = [
  'bower', 'jshint', 'sails', 'requirejs', 'forever'
];

var children = [];

dependencies.forEach(function( dep ) {
  var child = sudo( npmInstall.concat([ dep ]), options );
  child.stdout.on( 'data', function( data ) {
    console.log( data.toString() );
  });
  children.push( child );
});


text:
package com.dangdang.ddframe.job.lite.spring.test;

import com.dangdang.ddframe.job.reg.exception.RegExceptionHandler;
import com.dangdang.ddframe.job.util.concurrent.BlockUtils;
import org.apache.curator.test.TestingServer;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;

import java.io.File;
import java.io.IOException;

public final class EmbedZookeeperTestExecutionListener extends AbstractTestExecutionListener {

    private static volatile TestingServer testingServer;

    @Override
    public void beforeTestClass(final TestContext testContext) throws Exception {
        startEmbedTestingServer();
    }

    private static void startEmbedTestingServer() {
        if (null != testingServer) {
            return;
        }
        try {
            testingServer = new TestingServer(3181, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            RegExceptionHandler.handleException(ex);
        } finally {
            Runtime.getRuntime().addShutdownHook(new Thread() {

                @Override
                public void run() {
                    try {
                        BlockUtils.sleep(2000L);
                        testingServer.close();
                    } catch (final IOException ex) {
                        RegExceptionHandler.handleException(ex);
                    }
                }
            });
        }
    }
}


text:
package apple.webkit;

import java.io.*;
import java.nio.*;
import java.util.*;
import com.google.j2objc.annotations.*;
import com.google.j2objc.runtime.*;
import com.google.j2objc.runtime.block.*;
import apple.audiotoolbox.*;
import apple.corefoundation.*;
import apple.coregraphics.*;
import apple.coreservices.*;
import apple.foundation.*;
import apple.uikit.*;

/**
 * @since Available in iOS 8.0 and later.
 */
@Library("WebKit/WebKit.h")
@Mapping("WKSelectionGranularity")
public final class WKSelectionGranularity extends ObjCEnum {

    @GlobalConstant("WKSelectionGranularityDynamic")
    public static final long Dynamic = 0L;
    @GlobalConstant("WKSelectionGranularityCharacter")
    public static final long Character = 1L;

}


text: <img src="assets/img/work/black-lunch-tables/black-lunch-tables.jpg" alt="Black Lunch Tables" />


text:
<?xml version="1.0"?>

    <sectioninfo/>
    <title>Simple tables</title>

      <tgroup>

          <row>
            <entry>
              <para>Text</para>
            </entry>
            <entry>
              <para>Text</para>
            </entry>
          </row>

          <row>
            <entry>
                <para>Some word</para>
            </entry>
            <entry>
              <para>A Olórin i yáresse Mentaner i Númeherui Tírien i Rómenóri, Maiaron i oiosaila Manan elye etevanne Nórie i malanelye? Ú-reniathach i amar galen I reniad lín ne môr, nuithannen In gwidh ristennin, i fae narchannen I Lach Anor ed ardhon gwannen Calad veleg, ethuiannen. Melme nóren sina núra ala Eäro. Ilfirin nairelma ullume nucuvalme Nauva i nauva.</para>
              <para>A Olórin i yáresse Mentaner i Númeherui Tírien i Rómenóri, Maiaron i oiosaila Manan elye etevanne Nórie i malanelye? Ú-reniathach i amar galen I reniad lín ne môr, nuithannen In gwidh ristennin, i fae narchannen I Lach Anor ed ardhon gwannen Calad veleg, ethuiannen. Melme nóren sina núra ala Eäro. Ilfirin nairelma ullume nucuvalme Nauva i nauva.</para>
            </entry>
          </row>
          <row>
            <entry>
              <para>A Olórin i yáresse Mentaner i Númeherui Tírien i Rómenóri, Maiaron i oiosaila Manan elye etevanne Nórie i malanelye? Ú-reniathach i amar galen I reniad lín ne môr, nuithannen In gwidh ristennin, i fae narchannen I Lach Anor ed ardhon gwannen Calad veleg, ethuiannen. Melme nóren sina núra ala Eäro. Ilfirin nairelma ullume nucuvalme Nauva i nauva.</para>
              <para>A Olórin i yáresse Mentaner i Númeherui Tírien i Rómenóri, Maiaron i oiosaila Manan elye etevanne Nórie i malanelye? Ú-reniathach i amar galen I reniad lín ne môr, nuithannen In gwidh ristennin, i fae narchannen I Lach Anor ed ardhon gwannen Calad veleg, ethuiannen. Melme nóren sina núra ala Eäro. Ilfirin nairelma ullume nucuvalme Nauva i nauva.</para>
            </entry>
            <entry>
                <para>Some word</para>
            </entry>
          </row>

      </tgroup>


text:
"""An abstract storage class for alternative location tool."""
import abc

class AbstractStorage(object):
  """The storage abstract class."""

  __metaclass__ = abc.ABCMeta

  def __init__(self, config):
    """Initializes the storage class.

    Args:
      config: (config) A configuration object.
    """
    self.config = config

  @abc.abstractmethod
  def GetLocations(self):
    """Get a list of locations from storage.

    Returns:
      An array of locations.
    """
    pass

  @abc.abstractmethod
  def UpdateLocation(self):
    """Update a location to storage.

    Returns:
      A boolean of the result.
    """
    pass

  @abc.abstractmethod
  def RemoveLocation(self):
    """Remove a location from storage.

    Returns:
      A boolean of the result.
    """


text:
require 'optparse'
require 'pp'

module Spiderman

  # Command Line Interface
  class CLI

    # Class constructor
    def initialize
      @end_program = false
    end

    # Parse the command line options using OptionParser from StdLib
    def parse_options(args)

      dopts = Spiderman::Crawler::DEFAULT_OPTS
      options = Hash.new

      opt_parser = OptionParser.new do |opts|
        opts.banner = 'Usage: spiderman [options]'
        opts.separator ""
        opts.separator "Default values are given in parenthesis"
        opts.separator ""
        opts.separator "Specific options:"

        opts.on("--url URL", "Setting the Base URL. (#{dopts[:base_url_string]})") do |url|
          if url !~ Spiderman::Scraper::VALID_URL
            raise OptionParser::InvalidArgument, "supports only http/https URLs"
          end
          options[:base_url_string] = url
        end

        opts.on("--site", "-s", "Crawl single site only. (#{dopts[:site_only]})") do
          options[:site_only] = true
        end

        opts.on("--depth N", "-d N", Numeric,  "Maximum crawl depth, counting the Base URL. (#{dopts[:max_crawl_depth]})") do |max_crawl_depth|
          if !(max_crawl_depth > 0)
            raise OptionParser::InvalidArgument, "supports only numbers > 0"
          end
          options[:max_crawl_depth] = max_crawl_depth
        end

        opts.on("--urls N", "-u N", Numeric, "Maximum number of urls to discover (#{dopts[:max_urls]})") do |max_urls|
          if !(max_urls > 0)
            raise OptionParser::InvalidArgument, "supports only numbers > 0"
          end
          options[:max_urls] = max_urls
        end

        opts.separator ""
        opts.separator "Common options:"

        opts.on_tail('--verbose', "Verbose output. (#{dopts[:verbose]})") do
          options[:verbose] = true
        end

        opts.on_tail('--help', '-h', 'Show this message') do
          puts opts
          @end_program=true
          return
        end

        opts.on_tail('--version', '-v', 'Show version') do
          puts "#{APPNAME}: version #{VERSION}"
          @end_program=true
          return
        end
      end

      begin
        opt_parser.parse!(args)
      rescue => ex
        puts ex
        @end_program = true
      end

      options
    end

    # Parse the options and kick off the crawler
    def run(args)
      opts = parse_options(args)
      unless @end_program
        crawler = Spiderman::Crawler.new(opts)
        crawler.crawl
      end
    end
  end

end


text:
import { toggleLocateModal } from 'js/actions/mapActions';
import Wrapper from 'js/components/modals/Wrapper';
import React, { Component } from 'react';
import appStore from 'js/appStore';

export default class LocateModal extends Component {
  displayName: 'LocateModal';

  close = () => {
    appStore.dispatch(toggleLocateModal({ visible: false }));
  };

  render () {
    let { visible } = this.props;

    return (
      <Wrapper theme='locate-modal' visible={visible} close={this.close}>

Locate Something

      </Wrapper>
    );
  }
}


text:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows;
using System.Windows.Media.Animation;
using XPFriend.JenkinsOnDesktop.Core;

namespace XPFriend.JenkinsOnDesktop.Animation
{
    [TestClass]
    public class WindowOperatorTest
    {
        [TestCleanup]
        public void Cleanup()
        {
            if (MainWindow.Current != null)
            {
                MainWindow.Current.HideNotifyIcon();
            }
        }

        [TestMethod]
        public void TestGetInstance()
        {
            // expect
            Assert.AreSame(WindowOperator.GetInstance(), WindowOperator.GetInstance());
        }

        [TestMethod]
        public void TestApplyAnimationClock()
        {
            try
            {
                // when
                WindowOperator.GetInstance().ApplyAnimationClock(null, null, HandoffBehavior.Compose);
                Assert.Fail();
            }
            catch (NotImplementedException e)
            {
                // then
                Console.WriteLine(e);
            }

            try
            {
                // when
                WindowOperator.GetInstance().ApplyAnimationClock(null, null);
                Assert.Fail();
            }
            catch (NotImplementedException e)
            {
                // then
                Console.WriteLine(e);
            }
        }

        [TestMethod]
        public void TestBeginAnimation()
        {
            try
            {
                // when
                WindowOperator.GetInstance().BeginAnimation(null, null, HandoffBehavior.Compose);
                Assert.Fail();
            }
            catch (NotImplementedException e)
            {
                // then
                Console.WriteLine(e);
            }

            try
            {
                // when
                WindowOperator.GetInstance().BeginAnimation(null, null);
                Assert.Fail();
            }
            catch (NotImplementedException e)
            {
                // then
                Console.WriteLine(e);
            }
        }

        [TestMethod]
        public void TestGetAnimationBaseValue()
        {
            try
            {
                // when
                WindowOperator.GetInstance().GetAnimationBaseValue(null);
                Assert.Fail();
            }
            catch (NotImplementedException e)
            {
                // then
                Console.WriteLine(e);
            }
        }

        [TestMethod]
        public void TestHasAnimatedProperties()
        {
            try
            {
                // when
                bool b = WindowOperator.GetInstance().HasAnimatedProperties;
                Assert.Fail();
            }
            catch (NotImplementedException e)
            {
                // then
                Console.WriteLine(e);
            }
        }

        [TestMethod]
        public void TestConvertNaNToZero()
        {
            // setup
            Workspace.Current.Configuration = new Configuration();
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(margin, WindowOperator.ConvertNaNToZero(double.NaN));
            Assert.AreEqual(margin, WindowOperator.ConvertNaNToZero(double.PositiveInfinity));
            Assert.AreEqual(margin, WindowOperator.ConvertNaNToZero(double.NegativeInfinity));
            Assert.AreEqual(1.0, WindowOperator.ConvertNaNToZero(1.0));
        }

        [TestMethod]
        public void TestGetLeft()
        {
            // setup
            Workspace.Current.Configuration = new Configuration();
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(margin, WindowOperator.GetLeft(null));
        }

        [TestMethod]
        public void TestGetTop()
        {
            // setup
            Workspace.Current.Configuration = new Configuration();
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(margin, WindowOperator.GetTop(null));
        }

        [TestMethod]
        public void TestGetRight()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(window.ScreenWidth - margin, WindowOperator.GetRight(window));
        }

        [TestMethod]
        public void TestGetBottom()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(window.ScreenHeight - margin, WindowOperator.GetBottom(window));
        }

        [TestMethod]
        public void TestGetCenter()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(window.ScreenWidth / 2, WindowOperator.GetCenter(window));
        }

        [TestMethod]
        public void TestGetMiddle()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(window.ScreenHeight / 2, WindowOperator.GetMiddle(window));
        }

        [TestMethod]
        public void TestGetLeftWithPosition()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(margin, WindowOperator.GetLeft(window, Position.LeftTop));
            Assert.AreEqual(margin, WindowOperator.GetLeft(window, Position.LeftMiddle));
            Assert.AreEqual(margin, WindowOperator.GetLeft(window, Position.LeftBottom));
            Assert.AreEqual(window.ScreenWidth / 2, WindowOperator.GetLeft(window, Position.CenterTop));
            Assert.AreEqual(window.ScreenWidth / 2, WindowOperator.GetLeft(window, Position.CenterMiddle));
            Assert.AreEqual(window.ScreenWidth / 2, WindowOperator.GetLeft(window, Position.CenterBottom));
            Assert.AreEqual(window.ScreenWidth - margin, WindowOperator.GetLeft(window, Position.RightTop));
            Assert.AreEqual(window.ScreenWidth - margin, WindowOperator.GetLeft(window, Position.RightMiddle));
            Assert.AreEqual(window.ScreenWidth - margin, WindowOperator.GetLeft(window, Position.RightBottom));
        }

        [TestMethod]
        public void TestGetTopWithPosition()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            // expect
            Assert.AreEqual(margin, WindowOperator.GetTop(window, Position.LeftTop));
            Assert.AreEqual(margin, WindowOperator.GetTop(window, Position.CenterTop));
            Assert.AreEqual(margin, WindowOperator.GetTop(window, Position.RightTop));
            Assert.AreEqual(window.ScreenHeight / 2, WindowOperator.GetTop(window, Position.LeftMiddle));
            Assert.AreEqual(window.ScreenHeight / 2, WindowOperator.GetTop(window, Position.CenterMiddle));
            Assert.AreEqual(window.ScreenHeight / 2, WindowOperator.GetTop(window, Position.RightMiddle));
            Assert.AreEqual(window.ScreenHeight - margin, WindowOperator.GetTop(window, Position.LeftBottom));
            Assert.AreEqual(window.ScreenHeight - margin, WindowOperator.GetTop(window, Position.CenterBottom));
            Assert.AreEqual(window.ScreenHeight - margin, WindowOperator.GetTop(window, Position.RightBottom));
        }

        [TestMethod]
        public void TestLeft()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;

            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.LeftProperty, 10.0);

                // then
                Assert.AreEqual(10.0, window.Left);
            }
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.LeftProperty, null);

                // then
                Assert.AreEqual(10.0, window.Left);
            }
        }

        [TestMethod]
        public void TestTop()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;

            // when
            WindowOperator.GetInstance().SetValue(WindowOperator.TopProperty, 10.0);

            // then
            Assert.AreEqual(10.0, window.Top);
        }

        [TestMethod]
        public void TestPosition()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            window.Left = 10.0;
            window.Top = 11.0;
            double margin = Workspace.Current.Configuration.DesktopMargin;

            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.PositionProperty, Position.Current);

                // then
                Assert.AreEqual(10.0, window.Left);
                Assert.AreEqual(11.0, window.Top);
            }
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.PositionProperty, Position.LeftBottom);

                // then
                Assert.AreEqual(margin, window.Left);
                Assert.AreEqual(window.ScreenHeight - margin, window.Top);
            }
        }

        [TestMethod]
        public void TestOpacity()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            window.Opacity = 1.0;

            // when
            WindowOperator.GetInstance().SetValue(WindowOperator.OpacityProperty, 0.5);

            // then
            Assert.AreEqual(0.5, window.Opacity);
        }

        [TestMethod]
        public void TestCommand()
        {
            // setup
            MainWindow window = new MainWindow();
            MainWindow.Current = window;
            window.Opacity = 1.0;
            window.WindowState = WindowState.Normal;
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.CommandProperty, Command.Hide);

                // then
                Assert.AreEqual(0.0, window.Opacity);
            }
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.CommandProperty, Command.Show);

                // then
                Assert.AreEqual(1.0, window.Opacity);
            }
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.CommandProperty, Command.Maximize);

                // then
                Assert.AreEqual(WindowState.Maximized, window.WindowState);
            }
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.CommandProperty, Command.Minimize);

                // then
                Assert.AreEqual(WindowState.Minimized, window.WindowState);
            }
            {
                // when
                WindowOperator.GetInstance().SetValue(WindowOperator.CommandProperty, Command.Normalize);

                // then
                Assert.AreEqual(WindowState.Normal, window.WindowState);
            }
        }
    }
}