diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..7393e46d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +*.o diff --git a/InstantDEX/InstantDEX.c b/InstantDEX/InstantDEX.c new file mode 100755 index 000000000..4828f96d4 --- /dev/null +++ b/InstantDEX/InstantDEX.c @@ -0,0 +1,582 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define _issue_curl(curl_handle,label,url) bitcoind_RPC(curl_handle,label,url,0,0,0) + +#define INSTANTDEX_MINVOL 75 +#define INSTANTDEX_MINVOLPERC ((double)INSTANTDEX_MINVOL / 100.) +#define INSTANTDEX_PRICESLIPPAGE 0.001 +#define FINISH_HEIGHT 7 + +#define INSTANTDEX_TRIGGERDEADLINE 120 +#define JUMPTRADE_SECONDS 100 +#define INSTANTDEX_ACCT "4383817337783094122" +#define INSTANTDEX_FEE ((long)(2.5 * SATOSHIDEN)) + +#include "../iguana777.h" +#include "InstantDEX_quote.h" + +#define INSTANTDEX_LOCALAPI "allorderbooks", "orderbook", "lottostats", "LSUM", "makebasket", "disable", "enable", "peggyrates", "tradesequence", "placebid", "placeask", "orderstatus", "openorders", "cancelorder", "tradehistory", "balance", "allexchanges", + + +typedef char *(*json_handler)(int32_t localaccess,int32_t valid,char *sender,cJSON **objs,int32_t numobjs,char *origargstr); + +queue_t InstantDEXQ,TelepathyQ,Pending_offersQ; +cJSON *InstantDEX_lottostats(); + +//#include "NXT_tx.h" +#include "trades.h" +#include "quotes.h" +#include "subatomic.h" + +#include "orderbooks.h" +#include "exchangeparse.h" +#include "exchange_trades.h" +#include "exchanges/poloniex.c" +#include "exchanges/bittrex.c" +#include "exchanges/btce.c" +#include "exchanges/bitfinex.c" +#include "exchanges/btc38.c" +#include "exchanges/huobi.c" +#include "exchanges/lakebtc.c" +#include "exchanges/quadriga.c" +#include "exchanges/okcoin.c" +#include "exchanges/coinbase.c" +#include "exchanges/bitstamp.c" + +// {"plugin":"InstantDEX","method":"orderbook","baseid":"8688289798928624137","rel":"USD","exchange":"active","allfields":1} + +// {"plugin":"InstantDEX","method":"orderbook","baseid":"17554243582654188572","rel":"12071612744977229797","exchange":"active","allfields":1} +// {"plugin":"InstantDEX","method":"orderbook","baseid":"6918149200730574743","rel":"XMR","exchange":"active","allfields":1} + +void idle() +{ + char *jsonstr,*str; cJSON *json; int32_t n = 0; uint32_t nonce; + /*printf("INSTANTDEX.readyflag.%d\n",INSTANTDEX.readyflag); + while ( INSTANTDEX.readyflag == 0 ) + sleep(1); + printf("INSTANTDEX.readyflag.%d\n",INSTANTDEX.readyflag);*/ + while ( 1 ) + { + if ( n == 0 ) + sleep(1); + n = 0; + if ( (jsonstr= queue_dequeue(&InstantDEXQ,1)) != 0 ) + { + printf("Dequeued InstantDEX.(%s)\n",jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + //fprintf(stderr,"dequeued\n"); + if ( (str= busdata_sync(&nonce,jsonstr,"allnodes",0)) != 0 ) + { + //fprintf(stderr,"busdata.(%s)\n",str); + free(str); + } + free_json(json); + n++; + } else printf("error parsing (%s) from InstantDEXQ\n",jsonstr); + free_queueitem(jsonstr); + } + } +} + +uint32_t _get_NXTheight(uint32_t *firsttimep) +{ + static uint32_t last,lastheight,lastNXTtime; + cJSON *json; uint32_t height = 0; char cmd[256],*jsonstr; + if ( time(NULL) > last+10 ) + { + sprintf(cmd,"requestType=getState"); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( firsttimep != 0 ) + lastNXTtime = *firsttimep = (uint32_t)get_cJSON_int(json,"time"); + height = (int32_t)get_cJSON_int(json,"numberOfBlocks"); + if ( height > 0 ) + height--; + lastheight = height; + free_json(json); + } + free(jsonstr); + } + last = (uint32_t)time(NULL); + } + else + { + height = lastheight; + if ( firsttimep != 0 ) + *firsttimep = lastNXTtime; + } + return(height); +} + +void idle2() +{ + static double lastmilli; + uint32_t NXTblock; + //while ( INSTANTDEX.readyflag == 0 ) + // sleep(1); + while ( 1 ) + { + if ( milliseconds() < (lastmilli + 5000) ) + sleep(1); + NXTblock = _get_NXTheight(0); + if ( 1 && NXTblock != prices777_NXTBLOCK ) + { + prices777_NXTBLOCK = NXTblock; + InstantDEX_update(IGUANA_NXTADDR,IGUANA_NXTACCTSECRET);//,SUPERNET.); + //fprintf(stderr,"done idle NXT\n"); + } + lastmilli = milliseconds(); + } +} + +cJSON *InstantDEX_lottostats() +{ + char cmdstr[1024],NXTaddr[64],buf[1024],*jsonstr; struct destbuf receiverstr; + cJSON *json,*array,*txobj; int32_t i,n,totaltickets = 0; uint64_t amount,senderbits; uint32_t timestamp = 0; + if ( timestamp == 0 ) + timestamp = 38785003; + sprintf(cmdstr,"requestType=getBlockchainTransactions&account=%s×tamp=%u&type=0&subtype=0",INSTANTDEX_ACCT,timestamp); + //printf("cmd.(%s)\n",cmdstr); + if ( (jsonstr= issue_NXTPOST(cmdstr)) != 0 ) + { + // printf("jsonstr.(%s)\n",jsonstr); + // mm string.({"requestProcessingTime":33,"transactions":[{"fullHash":"2a2aab3b84dadf092cf4cedcd58a8b5a436968e836338e361c45651bce0ef97e","confirmations":203,"signatureHash":"52a4a43d9055fe4861b3d13fbd03a42fecb8c9ad4ac06a54da7806a8acd9c5d1","transaction":"711527527619439146","amountNQT":"1100000000","transactionIndex":2,"ecBlockHeight":360943,"block":"6797727125503999830","recipientRS":"NXT-74VC-NKPE-RYCA-5LMPT","type":0,"feeNQT":"100000000","recipient":"4383817337783094122","version":1,"sender":"423766016895692955","timestamp":38929220,"ecBlockId":"10121077683890606382","height":360949,"subtype":0,"senderPublicKey":"4e5bbad625df3d536fa90b1e6a28c3f5a56e1fcbe34132391c8d3fd7f671cb19","deadline":1440,"blockTimestamp":38929430,"senderRS":"NXT-8E6V-YBWH-5VMR-26ESD","signature":"4318f36d9cf68ef0a8f58303beb0ed836b670914065a868053da5fe8b096bc0c268e682c0274e1614fc26f81be4564ca517d922deccf169eafa249a88de58036"}]}) + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (array= cJSON_GetObjectItem(json,"transactions")) != 0 && is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i= 2*INSTANTDEX_FEE ) + totaltickets += 2; + } + } + } + } + free_json(json); + } + free(jsonstr); + } + sprintf(buf,"{\"result\":\"lottostats\",\"totaltickets\":\"%d\"}",totaltickets); + return(cJSON_Parse(buf)); +} + +void set_best_amounts(int64_t *baseamountp,int64_t *relamountp,double price,double volume) +{ + double checkprice,checkvol,distA,distB,metric,bestmetric = (1. / SMALLVAL); + uint64_t baseamount,relamount,bestbaseamount = 0,bestrelamount = 0; + int32_t i,j; + baseamount = volume * SATOSHIDEN; + relamount = ((price * volume) * SATOSHIDEN); + //*baseamountp = baseamount, *relamountp = relamount; + //return; + for (i=-1; i<=1; i++) + for (j=-1; j<=1; j++) + { + checkprice = prices777_price_volume(&checkvol,baseamount+i,relamount+j); + distA = (checkprice - price); + distA *= distA; + distB = (checkvol - volume); + distB *= distB; + metric = sqrt(distA + distB); + if ( metric < bestmetric ) + { + bestmetric = metric; + bestbaseamount = baseamount + i; + bestrelamount = relamount + j; + //printf("i.%d j.%d metric. %f\n",i,j,metric); + } + } + *baseamountp = bestbaseamount; + *relamountp = bestrelamount; +} + +int32_t bidask_parse(int32_t localaccess,struct destbuf *exchangestr,struct destbuf *name,struct destbuf *base,struct destbuf *rel,struct destbuf *gui,struct InstantDEX_quote *iQ,cJSON *json) +{ + uint64_t basemult,relmult,baseamount,relamount; double price,volume; int32_t exchangeid,keysize,flag; char key[1024],buf[64],*methodstr; + memset(iQ,0,sizeof(*iQ)); + iQ->s.baseid = j64bits(json,"baseid"); iQ->s.relid = j64bits(json,"relid"); + iQ->s.baseamount = j64bits(json,"baseamount"), iQ->s.relamount = j64bits(json,"relamount"); + iQ->s.vol = jdouble(json,"volume"); iQ->s.price = jdouble(json,"price"); + copy_cJSON(exchangestr,jobj(json,"exchange")); + if ( exchangestr->buf[0] == 0 || find_exchange(&exchangeid,exchangestr->buf) == 0 ) + exchangeid = -1; + iQ->exchangeid = exchangeid; + copy_cJSON(base,jobj(json,"base")); + copy_cJSON(rel,jobj(json,"rel")); + copy_cJSON(name,jobj(json,"name")); + methodstr = jstr(json,"method"); + if ( methodstr != 0 && (strcmp(methodstr,"placeask") == 0 || strcmp(methodstr,"ask") == 0) ) + iQ->s.isask = 1; + if ( iQ->s.vol < 0. ) + { + iQ->s.vol = -iQ->s.vol; + iQ->s.isask ^= 1; + } + if ( methodstr != 0 && strcmp(exchangestr->buf,"wallet") == 0 && (iQ->s.baseid == NXT_ASSETID || strcmp(base->buf,"NXT") == 0) ) + { + flag = 1; + if ( strcmp(methodstr,"placeask") == 0 ) + methodstr = "placebid"; + else if ( strcmp(methodstr,"placebid") == 0 ) + methodstr = "placeask"; + else if ( strcmp(methodstr,"ask") == 0 ) + methodstr = "bid"; + else if ( strcmp(methodstr,"bid") == 0 ) + methodstr = "ask"; + else flag = 0; + if ( flag != 0 ) + { + iQ->s.baseid = iQ->s.relid, iQ->s.relid = NXT_ASSETID; + strcpy(base->buf,rel->buf), strcpy(rel->buf,"NXT"); + baseamount = iQ->s.baseamount; + iQ->s.baseamount = iQ->s.relamount, iQ->s.relamount = baseamount; + name->buf[0] = 0; + if ( iQ->s.vol > SMALLVAL && iQ->s.price > SMALLVAL ) + { + iQ->s.vol *= iQ->s.price; + iQ->s.price = 1. / iQ->s.price; + } + iQ->s.isask ^= 1; + printf("INVERT\n"); + } + } + if ( (iQ->s.timestamp= juint(json,"timestamp")) == 0 ) + iQ->s.timestamp = (uint32_t)time(NULL); + copy_cJSON(gui,jobj(json,"gui")), strncpy(iQ->gui,gui->buf,sizeof(iQ->gui)-1); + iQ->s.automatch = juint(json,"automatch"); + iQ->s.minperc = juint(json,"minperc"); + if ( (iQ->s.duration= juint(json,"duration")) == 0 || iQ->s.duration > ORDERBOOK_EXPIRATION ) + iQ->s.duration = ORDERBOOK_EXPIRATION; + InstantDEX_name(key,&keysize,exchangestr->buf,name->buf,base->buf,&iQ->s.baseid,rel->buf,&iQ->s.relid); + //printf(">>>>>>>>>>>> BASE.(%s) REL.(%s)\n",base->buf,rel->buf); + iQ->s.basebits = stringbits(base->buf); + iQ->s.relbits = stringbits(rel->buf); + safecopy(iQ->base,base->buf,sizeof(iQ->base)); + safecopy(iQ->rel,rel->buf,sizeof(iQ->rel)); + iQ->s.offerNXT = j64bits(json,"offerNXT"); + iQ->s.quoteid = j64bits(json,"quoteid"); + if ( strcmp(exchangestr->buf,"jumblr") == 0 || strcmp(exchangestr->buf,"pangea") == 0 ) + { + if ( strcmp(exchangestr->buf,"pangea") == 0 ) + { + if ( juint(json,"rakemillis") != 0 ) + iQ->s.minperc = juint(json,"rakemillis"); + if ( j64bits(json,"bigblind") != 0 ) + { + iQ->s.baseamount = j64bits(json,"bigblind"); + iQ->s.vol = ((double)iQ->s.baseamount / SATOSHIDEN); + } + if ( j64bits(json,"ante") != 0 ) + iQ->s.relamount = j64bits(json,"ante"); + iQ->s.minbuyin = juint(json,"minbuyin"); + iQ->s.maxbuyin = juint(json,"maxbuyin"); + /*if ( (iQ->s.maxrake= j64bits(json,"maxrake")) != 0 ) + { + if ( strcmp(base->buf,"BTC") == 0 && iQ->s.maxrake < SATOSHIDEN/10 ) + iQ->s.maxrake = SATOSHIDEN/10; + else if ( iQ->s.maxrake < 10*SATOSHIDEN ) + iQ->s.maxrake = 10*SATOSHIDEN; + }*/ + } + if ( iQ->s.price == 0. ) + iQ->s.price = 1.; + if ( iQ->s.vol == 0. ) + iQ->s.vol = 1.; + if ( iQ->s.baseamount == 0 ) + iQ->s.baseamount = iQ->s.vol * SATOSHIDEN; + if ( localaccess != 0 && strcmp(exchangestr->buf,"jumblr") == 0 ) + { +#ifdef later + struct coin777 *coin; int32_t maxamount; + if ( (coin= coin777_find(base->buf,0)) != 0 ) + { + if ( coin->jvin == 0 && coin->jvinaddr[0] == 0 ) + { + coin->jvin = -1; + printf("initial state for jumblr.%s detected\n",coin->name); + sleep(5); + } + if ( coin->jvin < 0 ) + { + printf("no %s unspents available for jumblr/pangea jvin.%d %.8f\n",coin->name,coin->jvin,dstr(coin->junspent)); + return(-1); + } + maxamount = coin->junspent - coin->mgw.txfee*2 - (coin->junspent>>10); + if ( iQ->s.baseamount > maxamount ) + iQ->s.baseamount = maxamount; + else if ( iQ->s.baseamount < coin->mgw.txfee ) + { + printf("jumblr/pangea amount %.8f less than txfee %.8f\n",dstr(iQ->s.baseamount),dstr(coin->mgw.txfee)); + return(-1); + } + } + else + { + printf("%s not initialized for jumblr\n",base->buf); + return(-1); + } +#endif + } + } + else + { + if ( iQ->s.baseamount == 0 || iQ->s.relamount == 0 ) + { + if ( iQ->s.price <= SMALLVAL || iQ->s.vol <= SMALLVAL ) + return(-1); + set_best_amounts(&iQ->s.baseamount,&iQ->s.relamount,iQ->s.price,iQ->s.vol); + } + } + if ( iQ->s.quoteid == 0 ) + iQ->s.quoteid = calc_quoteid(iQ); + else if ( iQ->s.quoteid != calc_quoteid(iQ) ) + { + printf("bidask_parse quoteid.%llu != calc.%llu\n",(long long)iQ->s.quoteid,(long long)calc_quoteid(iQ)); + return(-1); + } + if ( iQ->s.price > SMALLVAL && iQ->s.vol > SMALLVAL && iQ->s.baseid != 0 && iQ->s.relid != 0 ) + { + buf[0] = 0, _set_assetname(&basemult,buf,0,iQ->s.baseid); + printf("baseid.%llu -> %s mult.%llu\n",(long long)iQ->s.baseid,buf,(long long)basemult); + buf[0] = 0, _set_assetname(&relmult,buf,0,iQ->s.relid); + printf("relid.%llu -> %s mult.%llu\n",(long long)iQ->s.relid,buf,(long long)relmult); + //basemult = get_assetmult(iQ->baseid), relmult = get_assetmult(iQ->relid); + baseamount = (iQ->s.baseamount + basemult/2) / basemult, baseamount *= basemult; + relamount = (iQ->s.relamount + relmult/2) / relmult, relamount *= relmult; + if ( iQ->s.price != 0. && iQ->s.vol != 0 ) + { + price = prices777_price_volume(&volume,baseamount,relamount); + if ( fabs(iQ->s.price - price)/price > 0.001 ) + { + printf("cant create accurate price ref.(%f %f) -> (%f %f)\n",iQ->s.price,iQ->s.vol,price,volume); + return(-1); + } + } + } + return(0); +} + +char *InstantDEX(char *jsonstr,char *remoteaddr,int32_t localaccess) +{ + char *prices777_allorderbooks(); + char *InstantDEX_tradehistory(cJSON *json,int32_t firsti,int32_t endi); + char *InstantDEX_cancelorder(cJSON *json,char *activenxt,char *secret,uint64_t sequenceid,uint64_t quoteid); + struct destbuf exchangestr,method,gui,name,base,rel; double balance; + char *retstr = 0,key[512],retbuf[1024],*activenxt,*secret,*coinstr; struct InstantDEX_quote iQ; struct exchange_info *exchange; + cJSON *json; uint64_t assetbits,sequenceid; uint32_t maxdepth; int32_t invert=0,keysize,allfields; struct prices777 *prices = 0; + //printf("INSTANTDEX.(%s)\n",jsonstr); + //if ( INSTANTDEX.readyflag == 0 ) + // return(0); + if ( jsonstr != 0 && (json= cJSON_Parse(jsonstr)) != 0 ) + { + // test: asset/asset, asset/external, external/external, autofill and automatch + // peggy integration + if ( bidask_parse(localaccess,&exchangestr,&name,&base,&rel,&gui,&iQ,json) < 0 && (strcmp(exchangestr.buf,"jumblr") == 0 || strcmp(exchangestr.buf,"pangea") == 0) ) + { + //return(clonestr("{\"error\":\"invalid parameters\"}")); + } + if ( iQ.s.offerNXT == 0 ) + iQ.s.offerNXT = IGUANA_MY64BITS; +printf("isask.%d base.(%s) rel.(%s)\n",iQ.s.isask,base.buf,rel.buf); + copy_cJSON(&method,jobj(json,"method")); + if ( (sequenceid= j64bits(json,"orderid")) == 0 ) + sequenceid = j64bits(json,"sequenceid"); + allfields = juint(json,"allfields"); + if ( (maxdepth= juint(json,"maxdepth")) <= 0 ) + maxdepth = MAX_DEPTH; + if ( exchangestr.buf[0] == 0 ) + { + if ( iQ.s.baseid != 0 && iQ.s.relid != 0 ) + strcpy(exchangestr.buf,"nxtae"); + else strcpy(exchangestr.buf,"basket"); + } + assetbits = InstantDEX_name(key,&keysize,exchangestr.buf,name.buf,base.buf,&iQ.s.baseid,rel.buf,&iQ.s.relid); + //printf("2nd isask.%d base.(%s) rel.(%s)\n",iQ.s.isask,base.buf,rel.buf); + exchange = exchange_find(exchangestr.buf); + secret = jstr(json,"secret"), activenxt = jstr(json,"activenxt"); + if ( secret == 0 ) + { + secret = IGUANA_NXTACCTSECRET; + activenxt = IGUANA_NXTADDR; + } + if ( strcmp(method.buf,"exit") == 0 ) + { + printf("getchar and then exit\n"); + getchar(); + exit(0); + } + if ( strcmp(method.buf,"orderstatus") == 0 ) + retstr = InstantDEX_orderstatus(json,sequenceid,iQ.s.quoteid); + else if ( strcmp(method.buf,"cancelorder") == 0 ) + retstr = InstantDEX_cancelorder(json,jstr(json,"activenxt"),jstr(json,"secret"),sequenceid,iQ.s.quoteid); + else if ( strcmp(method.buf,"openorders") == 0 ) + retstr = InstantDEX_openorders(json,IGUANA_NXTADDR,juint(json,"allorders")); + else if ( strcmp(method.buf,"tradehistory") == 0 ) + retstr = InstantDEX_tradehistory(json,juint(json,"firsti"),juint(json,"endi")); + else if ( strcmp(method.buf,"withdraw") == 0 ) + retstr = InstantDEX_withdraw(json); + else if ( strcmp(method.buf,"balance") == 0 ) + { + if ( exchange != 0 && exchange->issue.trade != 0 ) + { + if ( exchange->issue.balances != 0 ) + { + if ( exchange->balancejson != 0 ) + free_json(exchange->balancejson), exchange->balancejson = 0; + exchange->lastbalancetime = (uint32_t)time(NULL); + if ( (exchange->balancejson= (*exchange->issue.balances)(&exchange->cHandle,exchange)) != 0 ) + { + if ( (coinstr= jstr(json,"base")) != 0 ) + retstr = (*exchange->issue.parsebalance)(exchange,&balance,coinstr); + else retstr = jprint(exchange->balancejson,0); + } else retstr = clonestr("{\"error\":\"balances null return\"}"); + } else retstr = clonestr("{\"error\":\"no balances function\"}"); + } else retstr = clonestr("{\"error\":\"cant find exchange trade or balances function\"}"); + printf("%s ptr.%p trade.%p\n",exchangestr.buf,exchange,exchange!=0?exchange->issue.trade:0); + } + else if ( strcmp(method.buf,"allorderbooks") == 0 ) + retstr = prices777_allorderbooks(); + else if ( strcmp(method.buf,"allexchanges") == 0 ) + retstr = jprint(exchanges_json(),1); + else if ( strcmp(method.buf,"lottostats") == 0 ) + retstr = jprint(InstantDEX_lottostats(),1); + /* else if ( strcmp(method.buf,"tradesequence") == 0 ) + { + //printf("call tradesequence.(%s)\n",jsonstr); + int32_t dotrade,numtrades; struct prices777_order trades[256]; struct pending_trade *pend; + dotrade = juint(json,"dotrade"); + retstr = InstantDEX_tradesequence(0,0,0,&numtrades,trades,(int32_t)(sizeof(trades)/sizeof(*trades)),dotrade,activenxt,secret,json); + if ( dotrade != 0 ) + { + pend = calloc(1,sizeof(*pend)); + pend->dir = iQ.s.isask == 0 ? 1 : -1, pend->price = iQ.s.price, pend->volume = iQ.s.vol, pend->orderid = iQ.s.quoteid; + pend->tradesjson = json; + pend->type = 'S'; + pend->timestamp = (uint32_t)time(NULL); + //InstantDEX_history(0,pend,0); + queue_enqueue("PendingQ",&Pending_offersQ.pingpong[0],&pend->DL,0); + } + }*/ + else if ( strcmp(method.buf,"makebasket") == 0 ) + { + if ( (prices= prices777_makebasket(0,json,1,"basket",0,0)) != 0 ) + retstr = clonestr("{\"result\":\"basket made\"}"); + else retstr = clonestr("{\"error\":\"couldnt make basket\"}"); + } + else if ( strcmp(method.buf,"peggyrates") == 0 ) + { + //if ( SUPERNET.peggy != 0 ) + // retstr = peggyrates(juint(json,"timestamp"),jstr(json,"name")); + //else retstr = clonestr("{\"error\":\"peggy disabled\"}"); + } + else if ( strcmp(method.buf,"LSUM") == 0 ) + { + sprintf(retbuf,"{\"result\":\"%s\",\"amount\":%d}",(rand() & 1) ? "BUY" : "SELL",(rand() % 100) * 100000); + retstr = clonestr(retbuf); + } + else if ( strcmp(method.buf,"placebid") == 0 || strcmp(method.buf,"placeask") == 0 ) + return(InstantDEX_placebidask(0,sequenceid,exchangestr.buf,name.buf,base.buf,rel.buf,&iQ,jstr(json,"extra"),secret,activenxt,json)); + else if ( strcmp(exchangestr.buf,"active") == 0 && strcmp(method.buf,"orderbook") == 0 ) + retstr = prices777_activebooks(name.buf,base.buf,rel.buf,iQ.s.baseid,iQ.s.relid,maxdepth,allfields,strcmp(exchangestr.buf,"active") == 0 || juint(json,"tradeable")); + else if ( (prices= prices777_find(&invert,iQ.s.baseid,iQ.s.relid,exchangestr.buf)) == 0 ) + { + if ( (prices= prices777_poll(exchangestr.buf,name.buf,base.buf,iQ.s.baseid,rel.buf,iQ.s.relid)) != 0 ) + { + if ( prices777_equiv(prices->baseid) == prices777_equiv(iQ.s.baseid) && prices777_equiv(prices->relid) == prices777_equiv(iQ.s.relid) ) + invert = 0; + else if ( prices777_equiv(prices->baseid) == prices777_equiv(iQ.s.relid) && prices777_equiv(prices->relid) == prices777_equiv(iQ.s.baseid) ) + invert = 1; + else invert = 0, printf("baserel not matching (%s %s) %llu %llu vs (%s %s) %llu %llu\n",prices->base,prices->rel,(long long)prices->baseid,(long long)prices->relid,base.buf,rel.buf,(long long)iQ.s.baseid,(long long)iQ.s.relid); + } + } + if ( retstr == 0 && prices != 0 ) + { + if ( strcmp(method.buf,"disablequotes") == 0 ) + { + if ( prices != 0 ) + { + if ( strcmp(prices->exchange,"unconf") == 0 ) + return(clonestr("{\"error\":\"cannot disable unconf\"}")); + prices->disabled = 1; + return(clonestr("{\"result\":\"success\"}")); + } + else return(clonestr("{\"error\":\"no prices to disable\"}")); + } + else if ( strcmp(method.buf,"enablequotes") == 0 ) + { + if ( prices != 0 ) + { + prices->disabled = 0; + return(clonestr("{\"result\":\"success\"}")); + } + else return(clonestr("{\"error\":\"no prices to enable\"}")); + } + else if ( strcmp(method.buf,"orderbook") == 0 ) + { + if ( maxdepth < MAX_DEPTH ) + return(prices777_orderbook_jsonstr(invert,IGUANA_MY64BITS,prices,&prices->O,maxdepth,allfields)); + else if ( (retstr= prices->orderbook_jsonstrs[invert][allfields]) == 0 ) + { + retstr = prices777_orderbook_jsonstr(invert,IGUANA_MY64BITS,prices,&prices->O,MAX_DEPTH,allfields); + portable_mutex_lock(&prices->mutex); + if ( prices->orderbook_jsonstrs[invert][allfields] != 0 ) + free(prices->orderbook_jsonstrs[invert][allfields]); + prices->orderbook_jsonstrs[invert][allfields] = retstr; + portable_mutex_unlock(&prices->mutex); + if ( retstr == 0 ) + retstr = clonestr("{}"); + } + if ( retstr != 0 ) + retstr = clonestr(retstr); + } + //else if ( strcmp(method.buf,"tradebot") == 0 ) + // retstr = InstantDEX_tradebot(prices,json,&iQ,invert); + } + //if ( Debuglevel > 2 ) + printf("(%s) %p exchange.(%s) base.(%s) %llu rel.(%s) %llu | name.(%s) %llu\n",retstr!=0?retstr:"",prices,exchangestr.buf,base.buf,(long long)iQ.s.baseid,rel.buf,(long long)iQ.s.relid,name.buf,(long long)assetbits); + } + return(retstr); +} + +char *bidask_func(int32_t localaccess,int32_t valid,char *sender,cJSON *json,char *origargstr) +{ + struct destbuf gui,exchangestr,name,base,rel,offerNXT; struct InstantDEX_quote iQ; + copy_cJSON(&offerNXT,jobj(json,"offerNXT")); +//printf("got (%s)\n",origargstr); + if ( strcmp(IGUANA_NXTADDR,offerNXT.buf) != 0 ) + { + if ( bidask_parse(localaccess,&exchangestr,&name,&base,&rel,&gui,&iQ,json) == 0 ) + return(InstantDEX_placebidask(sender,j64bits(json,"orderid"),exchangestr.buf,name.buf,base.buf,rel.buf,&iQ,jstr(json,"extra"),jstr(json,"secret"),jstr(json,"activenxt"),json)); + else printf("error with incoming bidask\n"); + } else fprintf(stderr,"got my bidask from network (%s)\n",origargstr); + return(clonestr("{\"result\":\"got loopback bidask\"}")); +} + diff --git a/InstantDEX/InstantDEX_quote.h b/InstantDEX/InstantDEX_quote.h new file mode 100755 index 000000000..5239dd941 --- /dev/null +++ b/InstantDEX/InstantDEX_quote.h @@ -0,0 +1,223 @@ +// +// sha256.h +// crypto777 +// +// Created by James on 4/9/15. +// Copyright (c) 2015 jl777. All rights reserved. +// + +#ifndef crypto777_InstantDEX_quote_h +#define crypto777_InstantDEX_quote_h + +#include +#include "../includes/uthash.h" + +#define NXT_ASSETID ('N' + ((uint64_t)'X'<<8) + ((uint64_t)'T'<<16)) // 5527630 +#define MAX_BUYNXT 10 +#define MIN_NQTFEE 100000000 +#define NXT_TOKEN_LEN 160 + +#define GENESISACCT "1739068987193023818" // NXT-MRCC-2YLS-8M54-3CMAJ +#define GENESISPUBKEYSTR "1259ec21d31a30898d7cd1609f80d9668b4778e3d97e941044b39f0c44d2e51b" +#define GENESISPRIVKEYSTR "1259ec21d31a30898d7cd1609f80d9668b4778e3d97e941044b39f0c44d2e51b" +#define GENESIS_SECRET "It was a bright cold day in April, and the clocks were striking thirteen." +#define GENESISBLOCK "2680262203532249785" + +#define NXT_GENESISTIME 1385294400 + +#define DEFAULT_NXT_DEADLINE 720 +#define issue_curl(cmdstr) bitcoind_RPC(0,"curl",cmdstr,0,0,0) +#define issue_NXT(cmdstr) bitcoind_RPC(0,"NXT",cmdstr,0,0,0) +#define issue_NXTPOST(cmdstr) bitcoind_RPC(0,"curl",NXTAPIURL,0,0,cmdstr) +#define fetch_URL(url) bitcoind_RPC(0,"fetch",url,0,0,0) + +#define INSTANTDEX_TRIGGERDEADLINE 120 +#define _issue_curl(curl_handle,label,url) bitcoind_RPC(curl_handle,label,url,0,0,0) + +#define ORDERBOOK_EXPIRATION 3600 +#define INSTANTDEX_MINVOL 75 +#define INSTANTDEX_MINVOLPERC ((double)INSTANTDEX_MINVOL / 100.) +#define INSTANTDEX_PRICESLIPPAGE 0.001 + +#define INSTANTDEX_TRIGGERDEADLINE 120 +#define JUMPTRADE_SECONDS 100 +#define INSTANTDEX_ACCT "4383817337783094122" +#define INSTANTDEX_FEE ((long)(2.5 * SATOSHIDEN)) + +#define INSTANTDEX_NAME "InstantDEX" +#define INSTANTDEX_NXTAENAME "nxtae" +#define INSTANTDEX_NXTAEUNCONF "unconf" +#define INSTANTDEX_BASKETNAME "basket" +#define INSTANTDEX_ACTIVENAME "active" +#define INSTANTDEX_EXCHANGEID 0 +#define INSTANTDEX_UNCONFID 1 +#define INSTANTDEX_NXTAEID 2 +#define MAX_EXCHANGES 64 +#define ORDERBOOK_EXPIRATION 3600 + + +#define NXT_ASSETID ('N' + ((uint64_t)'X'<<8) + ((uint64_t)'T'<<16)) // 5527630 +#define BTC_ASSETID ('B' + ((uint64_t)'T'<<8) + ((uint64_t)'C'<<16)) // 4412482 +#define LTC_ASSETID ('L' + ((uint64_t)'T'<<8) + ((uint64_t)'C'<<16)) +#define PPC_ASSETID ('P' + ((uint64_t)'P'<<8) + ((uint64_t)'C'<<16)) +#define NMC_ASSETID ('N' + ((uint64_t)'M'<<8) + ((uint64_t)'C'<<16)) +#define DASH_ASSETID ('D' + ((uint64_t)'A'<<8) + ((uint64_t)'S'<<16) + ((uint64_t)'H'<<24)) +#define BTCD_ASSETID ('B' + ((uint64_t)'T'<<8) + ((uint64_t)'C'<<16) + ((uint64_t)'D'<<24)) + +#define USD_ASSETID ('U' + ((uint64_t)'S'<<8) + ((uint64_t)'D'<<16)) +#define CNY_ASSETID ('C' + ((uint64_t)'N'<<8) + ((uint64_t)'Y'<<16)) +#define EUR_ASSETID ('E' + ((uint64_t)'U'<<8) + ((uint64_t)'R'<<16)) +#define RUR_ASSETID ('R' + ((uint64_t)'U'<<8) + ((uint64_t)'R'<<16)) + +struct InstantDEX_shared +{ + double price,vol; + uint64_t quoteid,offerNXT,basebits,relbits,baseid,relid; int64_t baseamount,relamount; + uint32_t timestamp; + uint16_t duration:14,wallet:1,a:1,isask:1,expired:1,closed:1,swap:1,responded:1,matched:1,feepaid:1,automatch:1,pending:1,minperc:7; + uint16_t minbuyin,maxbuyin; +}; + +struct InstantDEX_quote +{ + UT_hash_handle hh; + struct InstantDEX_shared s; // must be here + char exchangeid,gui[9],base[8],rel[8]; + char walletstr[]; +}; + +struct InstantDEX_quote *delete_iQ(uint64_t quoteid); +struct InstantDEX_quote *find_iQ(uint64_t quoteid); +struct InstantDEX_quote *create_iQ(struct InstantDEX_quote *iQ,char *walletstr); +uint64_t calc_quoteid(struct InstantDEX_quote *iQ); +cJSON *set_walletstr(cJSON *walletitem,char *walletstr,struct InstantDEX_quote *iQ); +cJSON *InstantDEX_specialorders(uint64_t *quoteidp,uint64_t nxt64bits,char *base,char *special,uint64_t baseamount,int32_t addrtype); +int32_t bidask_parse(int32_t localaccess,struct destbuf *exchangestr,struct destbuf *name,struct destbuf *base,struct destbuf *rel,struct destbuf *gui,struct InstantDEX_quote *iQ,cJSON *json); + +int32_t coin777_addrtype(uint8_t *p2shtypep,char *coinstr); + +struct prices777_order +{ + struct InstantDEX_shared s; cJSON *retitem; struct prices777 *source; struct pending_trade *pend; + uint64_t id; double wt,ratio; uint16_t slot_ba; +}; +struct prices777_basket +{ + struct prices777 *prices; double wt; + int32_t groupid,groupsize,aski,bidi; + char base[64],rel[64]; +}; +struct prices777_orderentry { struct prices777_order bid,ask; }; +#define MAX_GROUPS 8 +#define _MAX_DEPTH 100 + +struct prices777_basketinfo +{ + int32_t numbids,numasks; uint32_t timestamp; + struct prices777_orderentry book[MAX_GROUPS+1][_MAX_DEPTH]; +}; + +struct NXTtx { uint64_t txid; char fullhash[MAX_JSON_FIELD],utxbytes[MAX_JSON_FIELD],utxbytes2[MAX_JSON_FIELD],txbytes[MAX_JSON_FIELD],sighash[MAX_JSON_FIELD]; }; + +struct pending_trade +{ + struct queueitem DL; + struct NXTtx trigger; struct prices777_order order; + uint64_t triggertxid,txid,quoteid,orderid,my64bits; + struct prices777 *prices; void *cHandlep; struct exchange_info *exchange; void *bot; + char *triggertx,*txbytes,extra[128]; uint8_t nxtsecret[2048]; cJSON *tradesjson,*item; + double price,volume; uint32_t timestamp,finishtime,expiration; + int32_t dir,type,version,size,dotrade,queueflag,*curlingp; +}; + +struct prices777 +{ + char url[512],exchange[64],base[64],rel[64],lbase[64],lrel[64],key[512],oppokey[512],contract[64],origbase[64],origrel[64]; + uint64_t contractnum,ap_mult,baseid,relid,basemult,relmult; double lastupdate,decay,oppodecay,lastprice,lastbid,lastask; + uint32_t pollnxtblock,exchangeid,numquotes,updated,lasttimestamp,RTflag,disabled,dirty; int32_t keysize,oppokeysize; + portable_mutex_t mutex; + char *orderbook_jsonstrs[2][2]; + struct prices777_basketinfo O,O2; double groupwts[MAX_GROUPS + 1]; + uint8_t changed,type; uint8_t **dependents; int32_t numdependents,numgroups,basketsize; double commission; + void *tradebot; + struct prices777_basket basket[]; +}; + +struct exchange_info; +struct exchange_funcs +{ + char *exchange; + double (*update)(struct prices777 *prices,int32_t maxdepth); + int32_t (*supports)(char *base,char *rel); + uint64_t (*trade)(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume); + char *(*orderstatus)(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid); + char *(*cancelorder)(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid); + char *(*openorders)(void **cHandlep,struct exchange_info *exchange,cJSON *argjson); + char *(*tradehistory)(void **cHandlep,struct exchange_info *exchange,cJSON *argjson); + cJSON *(*balances)(void **cHandlep,struct exchange_info *exchange); + char *(*parsebalance)(struct exchange_info *exchange,double *balancep,char *coinstr); + char *(*withdraw)(void **cHandlep,struct exchange_info *exchange,cJSON *argjson); +}; +#define EXCHANGE_FUNCS(xchg,name) { name, prices777_ ## xchg, xchg ## _supports, xchg ## _trade, xchg ## _orderstatus, xchg ## _cancelorder, xchg ## _openorders, xchg ## _tradehistory, xchg ## _balances, xchg ## _parsebalance, xchg ## _withdraw } + +struct exchange_info +{ + struct exchange_funcs issue; + char name[16],apikey[MAX_JSON_FIELD],apisecret[MAX_JSON_FIELD],userid[MAX_JSON_FIELD]; + cJSON *balancejson; + uint32_t num,exchangeid,pollgap,refcount,polling,lastbalancetime; + uint64_t nxt64bits,lastnonce; double lastupdate,commission; + void *cHandle; + portable_mutex_t mutex; +}; + +#define calc_predisplinex(startweekind,clumpsize,weekind) (((weekind) - (startweekind))/(clumpsize)) +#define _extrapolate_Spline(Splines,gap) ((double)(Splines)[0] + ((gap) * ((double)(Splines)[1] + ((gap) * ((double)(Splines)[2] + ((gap) * (double)(Splines)[3])))))) +#define _extrapolate_Slope(Splines,gap) ((double)(Splines)[1] + ((gap) * ((double)(Splines)[2] + ((gap) * (double)(Splines)[3])))) + +#define PRICE_BLEND(oldval,newval,decay,oppodecay) ((oldval == 0.) ? newval : ((oldval * decay) + (oppodecay * newval))) +#define PRICE_BLEND64(oldval,newval,decay,oppodecay) ((oldval == 0) ? newval : ((oldval * decay) + (oppodecay * newval) + 0.499)) + +struct prices777 *prices777_initpair(int32_t needfunc,char *exchange,char *base,char *rel,double decay,char *name,uint64_t baseid,uint64_t relid,int32_t basketsize); +struct exchange_info *get_exchange(int32_t exchangeid); +char *exchange_str(int32_t exchangeid); +struct exchange_info *exchange_find(char *exchangestr); +void prices777_exchangeloop(void *ptr); +uint64_t InstantDEX_name(char *key,int32_t *keysizep,char *exchange,char *name,char *base,uint64_t *baseidp,char *rel,uint64_t *relidp); +struct prices777 *prices777_find(int32_t *invertedp,uint64_t baseid,uint64_t relid,char *exchange); +struct exchange_info *find_exchange(int32_t *exchangeidp,char *exchangestr); +double prices777_InstantDEX(struct prices777 *prices,int32_t maxdepth); +uint64_t prices777_equiv(uint64_t assetid); +char *prices777_trade(int32_t *curlingp,void *bot,struct pending_trade **pendp,void **cHandlep,int32_t dotrade,cJSON *item,char *activenxt,char *secret,struct prices777 *prices,int32_t dir,double price,double volume,struct InstantDEX_quote *iQ,struct prices777_order *order,uint64_t orderid,char *extra); +double prices777_price_volume(double *volumep,uint64_t baseamount,uint64_t relamount); +struct prices777 *prices777_poll(char *exchangestr,char *name,char *base,uint64_t refbaseid,char *rel,uint64_t refrelid); +void set_best_amounts(int64_t *baseamountp,int64_t *relamountp,double price,double volume); +int32_t _set_assetname(uint64_t *multp,char *buf,char *jsonstr,uint64_t assetid); +char *InstantDEX_withdraw(cJSON *argjson); +cJSON *exchanges_json(); +char *InstantDEX_tradesequence(int32_t curlings[],void *bot,void *cHandles[],int32_t *nump,struct prices777_order *trades,int32_t maxtrades,int32_t dotrade,char *activenxt,char *secret,cJSON *json); +struct prices777 *prices777_makebasket(char *basketstr,cJSON *_basketjson,int32_t addbasket,char *typestr,struct prices777 *ptrs[],int32_t num); +char *prices777_activebooks(char *name,char *_base,char *_rel,uint64_t baseid,uint64_t relid,int32_t maxdepth,int32_t allflag,int32_t tradeable); +char *prices777_orderbook_jsonstr(int32_t invert,uint64_t nxt64bits,struct prices777 *prices,struct prices777_basketinfo *OB,int32_t maxdepth,int32_t allflag); +int32_t get_assetname(char *name,uint64_t assetid); +int32_t is_mscoin(char *assetidstr); +uint32_t _get_NXTheight(uint32_t *firsttimep); +char *fill_nxtae(int32_t dotrade,uint64_t *txidp,uint64_t nxt64bits,char *secret,int32_t dir,double price,double volume,uint64_t baseid,uint64_t relid); +uint64_t get_assetmult(uint64_t assetid); +int32_t InstantDEX_verify(uint64_t destNXTaddr,uint64_t sendasset,uint64_t sendqty,cJSON *txobj,uint64_t recvasset,uint64_t recvqty); +int32_t verify_NXTtx(cJSON *json,uint64_t refasset,uint64_t qty,uint64_t destNXTbits); +uint64_t assetmult(char *assetidstr); +int64_t get_asset_quantity(int64_t *unconfirmedp,char *NXTaddr,char *assetidstr); +uint64_t calc_asset_qty(uint64_t *availp,uint64_t *priceNQTp,char *NXTaddr,int32_t checkflag,uint64_t assetid,double price,double vol); + +cJSON *InstantDEX_orderbook(struct prices777 *prices); +char *hmac_sha512_str(char dest[(512>>3)*2 + 1],char *key,unsigned int key_size,char *message); +char *hmac_sha384_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_sha1_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_sha256_str(char *dest,char *key,int32_t key_size,char *message); + +extern uint32_t MAX_DEPTH; +extern char NXTAPIURL[],IGUANA_NXTACCTSECRET[],IGUANA_NXTADDR[]; +extern int32_t FIRST_EXTERNAL,IGUANA_disableNXT,Debuglevel,prices777_NXTBLOCK; +extern uint64_t IGUANA_MY64BITS; +#endif diff --git a/InstantDEX/Makefile b/InstantDEX/Makefile new file mode 100644 index 000000000..de0608fb4 --- /dev/null +++ b/InstantDEX/Makefile @@ -0,0 +1,47 @@ +# Copyright (c) 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# GNU Makefile based on shared rules provided by the Native Client SDK. +# See README.Makefiles for more details. + +VALID_TOOLCHAINS := pnacl newlib glibc clang-newlib mac + +NACL_SDK_ROOT ?= $(abspath $(CURDIR)) + +TARGET = InstantDEX + +EXTRA= -D__PNACL + +include $(NACL_SDK_ROOT)/tools/common.mk + +CHROME_ARGS += --allow-nacl-socket-api=127.0.0.1 + +DEPS = nacl_io +LIBS = crypto777 curl ssl crypto z glibc-compat nacl_spawn ppapi nacl_io ppapi_simple # cli_main ppapi_cpp ppapi_simple + +CFLAGS = -Wall -D__PNACL -fno-strict-aliasing $(EXTRA) +LFLAGS = libs + +SOURCES = main.c InstantDEX.c + +# Build rules generated by macros from common.mk: + +$(foreach dep,$(DEPS),$(eval $(call DEPEND_RULE,$(dep)))) + +$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) + +# The PNaCl workflow uses both an unstripped and finalized/stripped binary. +# On NaCl, only produce a stripped binary for Release configs (not Debug). +ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) + +$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES) $(LOCALLIBS),$(LIBS),$(DEPS))); +$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) + +else +$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) + +endif + +$(eval $(call NMF_RULE,$(TARGET),)) + diff --git a/InstantDEX/exchange_trades.h b/InstantDEX/exchange_trades.h new file mode 100755 index 000000000..a84785b34 --- /dev/null +++ b/InstantDEX/exchange_trades.h @@ -0,0 +1,1407 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#ifndef xcode_exchanges_h +#define xcode_exchanges_h + +#define SHA512_DIGEST_SIZE (512 / 8) +void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); + +char *exchange_would_submit(char *postreq,char *hdr1,char *hdr2,char *hdr3, char *hdr4) +{ + char *data; cJSON *json; + json = cJSON_CreateObject(); + jaddstr(json,"post",postreq); + if ( hdr1[0] != 0 ) + jaddstr(json,"hdr1",hdr1); + if ( hdr2[0] != 0 ) + jaddstr(json,"hdr2",hdr2); + if ( hdr3[0] != 0 ) + jaddstr(json,"hdr3",hdr3); + if ( hdr4[0] != 0 ) + jaddstr(json,"hdr4",hdr4); + data = jprint(json,1); + json = 0; + return(data); +} + +uint64_t exchange_nonce(struct exchange_info *exchange) +{ + uint64_t nonce; + nonce = time(NULL); + if ( nonce < exchange->lastnonce ) + nonce = exchange->lastnonce + 1; + exchange->lastnonce = nonce; + return(nonce); +} + +int32_t flip_for_exchange(char *pairstr,char *fmt,char *refstr,int32_t dir,double *pricep,double *volumep,char *base,char *rel) +{ + if ( strcmp(rel,refstr) == 0 ) + sprintf(pairstr,fmt,rel,base); + else + { + if ( strcmp(base,refstr) == 0 ) + { + sprintf(pairstr,fmt,base,rel); + dir = -dir; + *volumep *= *pricep; + *pricep = (1. / *pricep); + } + else sprintf(pairstr,fmt,rel,base); + } + return(dir); +} + +int32_t flipstr_for_exchange(struct exchange_info *exchange,char *pairstr,char *fmt,int32_t dir,double *pricep,double *volumep,char *_base,char *_rel) +{ + int32_t polarity; char base[64],rel[64]; + strcpy(base,_base), strcpy(rel,_rel); + tolowercase(base), tolowercase(rel); + polarity = (*exchange->issue.supports)(base,rel); + if ( dir > 0 ) + sprintf(pairstr,fmt,base,rel); + else if ( dir < 0 ) + { + *volumep *= *pricep; + *pricep = (1. / *pricep); + sprintf(pairstr,fmt,rel,base); + } + return(dir); +} + +int32_t cny_flip(char *market,char *coinname,char *base,char *rel,int32_t dir,double *pricep,double *volumep) +{ + char pairstr[512],lbase[16],lrel[16],*refstr=0; + strcpy(lbase,base), tolowercase(lbase), strcpy(lrel,rel), tolowercase(lrel); + if ( strcmp(lbase,"cny") == 0 || strcmp(lrel,"cny") == 0 ) + { + dir = flip_for_exchange(pairstr,"%s_%s","cny",dir,pricep,volumep,lbase,lrel); + refstr = "cny"; + } + else if ( strcmp(lbase,"btc") == 0 || strcmp(lrel,"btc") == 0 ) + { + dir = flip_for_exchange(pairstr,"%s_%s","btc",dir,pricep,volumep,lbase,lrel); + refstr = "btc"; + } + if ( market != 0 && coinname != 0 && refstr != 0 ) + { + strcpy(market,refstr); + if ( strcmp(lbase,"refstr") != 0 ) + strcpy(coinname,lbase); + else strcpy(coinname,lrel); + touppercase(coinname); + } + return(dir); +} + +char *exchange_extractorderid(int32_t historyflag,char *status,uint64_t quoteid,char *quoteid_field) +{ + cJSON *array,*item,*json; int32_t i,n; uint64_t txid; + if ( status != 0 ) + { + if ( (array= cJSON_Parse(status)) != 0 && is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; iapikey,(long long)nonce); + else + { + dir = flip_for_exchange(pairstr,"%s-%s","BTC",dir,&price,&volume,base,rel); + sprintf(urlbuf,"https://bittrex.com/api/v1.1/market/%slimit?apikey=%s&nonce=%llu&market=%s&rate=%.8f&quantity=%.8f",dir>0?"buy":"sell",exchange->apikey,(long long)nonce,pairstr,price,volume); + } + if ( (sig = hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),urlbuf)) != 0 ) + sprintf(hdr,"apisign:%s",sig); + else hdr[0] = 0; + //printf("cmdbuf.(%s) h1.(%s)\n",urlbuf,hdr); + if ( (data= curl_post(&cHandle,urlbuf,0,0,hdr,0,0,0)) != 0 ) + { + //printf("cmd.(%s) [%s]\n",urlbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + // { "success" : true, "message" : "", "result" : { "uuid" : "e606d53c-8d70-11e3-94b5-425861b86ab6" } } + if ( dir == 0 ) + { + //printf("got balances.(%s)\n",data); + } + else if ( is_cJSON_True(cJSON_GetObjectItem(json,"success")) != 0 && (resultobj= cJSON_GetObjectItem(json,"result")) != 0 ) + { + copy_cJSON(&uuidstr,cJSON_GetObjectItem(resultobj,"uuid")); + for (i=j=0; uuidstr.buf[i]!=0; i++) + if ( uuidstr.buf[i] != '-' ) + uuidstr.buf[j++] = uuidstr.buf[i]; + uuidstr.buf[j] = 0; + n = (int32_t)strlen(uuidstr.buf); + printf("-> uuidstr.(%s).%d\n",uuidstr.buf,n); + decode_hex(databuf,n/2,uuidstr.buf); + if ( n >= 16 ) + for (i=0; i<8; i++) + databuf[i] ^= databuf[8 + i]; + memcpy(&txid,databuf,8); + printf("-> %llx\n",(long long)txid); + } + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t poloniex_trade(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + static CURL *cHandle; + char *sig,*data,*extra,*typestr,cmdbuf[8192],hdr1[4096],hdr2[4096],pairstr[512],dest[SHA512_DIGEST_SIZE*2 + 1]; cJSON *json; uint64_t nonce,txid = 0; + nonce = exchange_nonce(exchange); + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( dir == 0 ) + sprintf(cmdbuf,"command=returnCompleteBalances&nonce=%llu",(long long)nonce); + else + { + dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + if ( extra != 0 && strcmp(extra,"margin") == 0 ) + typestr = (dir > 0) ? "marginBuy":"marginSell"; + else typestr = (dir > 0) ? "buy":"sell"; + sprintf(cmdbuf,"command=%s&nonce=%ld¤cyPair=%s&rate=%.8f&amount=%.8f",typestr,(long)time(NULL),pairstr,price,volume); + } + if ( (sig= hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),cmdbuf)) != 0 ) + sprintf(hdr2,"Sign:%s",sig); + else hdr2[0] = 0; + sprintf(hdr1,"Key:%s",exchange->apikey); + //printf("cmdbuf.(%s) h1.(%s) h2.(%s)\n",cmdbuf,hdr2,hdr1); + if ( (data= curl_post(&cHandle,"https://poloniex.com/tradingApi",0,cmdbuf,hdr2,hdr1,0,0)) != 0 ) + { + //printf("cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = (get_API_nxt64bits(cJSON_GetObjectItem(json,"orderNumber")) << 32) | get_API_nxt64bits(cJSON_GetObjectItem(json,"tradeID")); + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +/*uint64_t bter_trade(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + static CURL *cHandle; + char *sig,*data,buf[512],cmdbuf[8192],hdr1[1024],hdr2[1024],pairstr[512],dest[SHA512_DIGEST_SIZE*2 + 1]; + cJSON *json; uint64_t txid = 0; + dir = cny_flip(0,0,base,rel,dir,&price,&volume); + sprintf(cmdbuf,"type=%s&nonce=%ld&pair=%s&rate=%.8f&amount=%.8f",dir>0?"BUY":"SELL",(long)time(NULL),pairstr,price,volume); + if ( (sig = hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),cmdbuf)) != 0 ) + sprintf(hdr2,"SIGN:%s",sig); + else hdr2[0] = 0; + sprintf(hdr1,"KEY:%s",exchange->apikey); + printf("cmdbuf.(%s) h1.(%s) h2.(%s)\n",cmdbuf,hdr2,hdr1); + if ( (data= curl_post(&cHandle,"https://bter.com/api/1/private/placeorder",0,cmdbuf,hdr2,hdr1,0)) != 0 ) + { + printf("cmd.(%s) [%s]\n",cmdbuf,data); + //{ "result":"true", "order_id":"123456", "msg":"Success" } + if ( (json= cJSON_Parse(data)) != 0 ) + { + copy_cJSON(buf,cJSON_GetObjectItem(json,"result")); + if ( strcmp(buf,"true") != 0 ) + { + copy_cJSON(buf,cJSON_GetObjectItem(json,"msg")); + if ( strcmp(buf,"Success") != 0 ) + txid = get_API_nxt64bits(cJSON_GetObjectItem(json,"order_id")); + } + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +}*/ + +uint64_t btce_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + /*Authentication is made by sending the following HTTP headers: + Key — API key. API key examples: 46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T + API keys are created in the Profile in the API keys section. + Sign — Signature. POST-parameters (?nonce=1¶m0=val0), signed with a Secret key using HMAC-SHA512*/ + static CURL *cHandle; + + char *sig,*data,base[64],rel[64],payload[8192],hdr1[4096],hdr2[4096],pairstr[512],dest[SHA512_DIGEST_SIZE*2 + 1]; cJSON *json,*resultobj; uint64_t nonce,txid = 0; + sprintf(hdr1,"Key:%s",exchange->apikey); + nonce = exchange_nonce(exchange); + if ( dir == 0 ) + sprintf(payload,"method=getInfo&nonce=%llu",(long long)nonce); + else + { + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + sprintf(payload,"method=Trade&nonce=%ld&pair=%s&type=%s&rate=%.3f&amount=%.6f",(long)time(NULL),pairstr,dir>0?"buy":"sell",price,volume); + } + if ( (sig= hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),payload)) != 0 ) + sprintf(hdr2,"Sign:%s",sig); + else hdr2[0] = 0; + //printf("cmdbuf.(%s) h1.(%s) h2.(%s)\n",payload,hdr2,hdr1); + if ( (data= curl_post(&cHandle,"https://btc-e.com/tapi",0,payload,hdr2,hdr1,0,0)) != 0 ) + { + //printf("cmd.(%s) [%s]\n",payload,data); + //{ "success":1, "return":{ "received":0.1, "remains":0, "order_id":0, "funds":{ "usd":325, "btc":2.498, } } } + if ( (json= cJSON_Parse(data)) != 0 ) + { + if ( juint(json,"success") > 0 && (resultobj= cJSON_GetObjectItem(json,"return")) != 0 ) + { + if ( (txid= get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"order_id"))) == 0 ) + { + if ( get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"remains")) == 0 ) + txid = _crc32(0,payload,strlen(payload)); + } + } + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t kraken_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + //API-Key = API key + //API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key + + static CURL *cHandle; + char *sig,*data,url[512],base[64],rel[64],buf[8192],postbuf[1024],payload[8192],sha256[65],hdr1[4096],hdr2[4096],encode64[4096],decode64[4096],dest[SHA512_DIGEST_SIZE*2 + 1]; + cJSON *json,*resultobj; uint8_t hash[32]; uint64_t nonce,txid = 0; int32_t n; + if ( _base != 0 && _rel != 0 ) + { + strcpy(base,_base), strcpy(rel,_rel); + touppercase(base), touppercase(rel); + if ( strcmp(base,"BTC") == 0 ) + strcpy(base,"XBT"); + if ( strcmp(rel,"BTC") == 0 ) + strcpy(rel,"XBT"); + if ( strcmp(base,"DOGE") == 0 ) + strcpy(base,"XDG"); + if ( strcmp(rel,"DOGE") == 0 ) + strcpy(rel,"XDG"); + } + sprintf(hdr1,"API-Key:%s",exchange->apikey); + n = nn_base64_decode((void *)exchange->apisecret,strlen(exchange->apisecret),(void *)decode64,sizeof(decode64)); + nonce = exchange_nonce(exchange); + if ( dir == 0 ) + { + sprintf(postbuf,"nonce=%llu",(long long)nonce); + sprintf(url,"https://api.kraken.com/0/private/Balance"); + } + else + { + /* + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + //dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + sprintf(payload,"method=Trade&nonce=%ld&pair=%s&type=%s&rate=%.6f&amount=%.6f",(long)time(NULL),pairstr,dir>0?"buy":"sell",price,volume);*/ + } + sprintf(buf,"%s",postbuf); + calc_sha256(sha256,hash,(uint8_t *)buf,(int32_t)strlen(buf)); + sprintf(payload,"%s%s",url,sha256); + //memset(payload,0,sizeof(payload)); + //sprintf(payload,"%s",url); + //memcpy(payload+strlen(payload),hash,sizeof(hash)); + if ( (sig= hmac_sha512_str(dest,decode64,n,payload)) != 0 ) + { + n = nn_base64_encode((void *)sig,n,(void *)encode64,sizeof(encode64)); + sprintf(hdr2,"API-Sign:%s",encode64); + } + else hdr2[0] = 0; + //printf("cmdbuf.(%s) h1.(%s) h2.(%s)\n",postbuf,hdr2,hdr1); + if ( (data= curl_post(&cHandle,url,0,postbuf,hdr1,hdr2,0,0)) != 0 ) + { + //printf("cmd.(%s) [%s]\n",payload,data); + //{ "success":1, "return":{ "received":0.1, "remains":0, "order_id":0, "funds":{ "usd":325, "btc":2.498, } } } + if ( (json= cJSON_Parse(data)) != 0 ) + { + if ( juint(json,"success") > 0 && (resultobj= cJSON_GetObjectItem(json,"return")) != 0 ) + { + if ( (txid= get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"order_id"))) == 0 ) + { + if ( get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"remains")) == 0 ) + txid = _crc32(0,payload,strlen(payload)); + } + } + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t bitfinex_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + /* POST https://api.bitfinex.com/v1/order/new + void *curl_post(CURL **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2) + With a payload of + + { + "request": "/v1/order/new", + "nonce": "1234", + "option1": ... + } + The nonce provided must be strictly increasing. + + To authenticate a request, use the following: + + payload = parameters-dictionary -> JSON encode -> base64 + signature = HMAC-SHA384(payload, api-secret) as hexadecimal + send (api-key, payload, signature) + These are encoded as HTTP headers named: + + X-BFX-APIKEY + X-BFX-PAYLOAD + X-BFX-SIGNATURE + */ + /* POST /order/new + Request + Key Type Description + symbol [string] The name of the symbol (see `/symbols`). + amount [decimal] Order size: how much to buy or sell. + price [price] Price to buy or sell at. Must be positive. Use random number for market orders. + exchange [string] "bitfinex" + side [string] Either "buy" or "sell". + type [string] Either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders) + is_hidden [bool] true if the order should be hidden. Default is false.*/ + + static CURL *cHandle; + char *sig,*data,hdr3[4096],url[512],*extra,*typestr,*method,req[4096],base[16],rel[16],payload[4096],hdr1[4096],hdr2[4096],pairstr[512],dest[1024 + 1]; + cJSON *json; uint64_t nonce,txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + memset(req,0,sizeof(req)); + nonce = exchange_nonce(exchange); + if ( dir == 0 ) + { + method = "balances"; + sprintf(req,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\"}",method,(long long)nonce); + } + else + { + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + method = "order/new"; + //Either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders) + if ( (typestr= extra) == 0 ) + typestr = "exchange limit"; + sprintf(req,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"exchange\":\"bitfinex\",\"side\":\"%s\",\"type\":\"%s\",\"price\":\"%.8f\",\"amount\":\"%.8f\",\"symbol\":\"%s\"}",method,(long long)nonce,dir>0?"buy":"sell",typestr,price,volume,pairstr); + } + nn_base64_encode((void *)req,strlen(req),payload,sizeof(payload)); + if ( (sig= hmac_sha384_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),payload)) != 0 ) + { + sprintf(hdr1,"X-BFX-APIKEY:%s",exchange->apikey); + sprintf(hdr2,"X-BFX-PAYLOAD:%s",payload); + sprintf(hdr3,"X-BFX-SIGNATURE:%s",sig); + sprintf(url,"https://api.bitfinex.com/v1/%s",method); + //printf("bitfinex req.(%s) -> (%s) [%s %s %s]\n",req,payload,hdr1,hdr2,hdr3); + if ( (data= curl_post(&cHandle,url,0,req,hdr1,hdr2,hdr3,0)) != 0 ) + { + //printf("[%s]\n",data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + if ( (txid= j64bits(json,"order_id")) == 0 ) + { + if ( dir != 0 ) + printf("bitfinex: no txid error\n"); + } + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + } + return(txid); +} + +uint64_t btc38_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + /* $ Stamp = $ date-> getTimestamp (); + type, 1 for the purchase of Entry, 2 entry order to sell, can not be empty / the type of the order + + $ Mdt = "_ public here to write here write here to write user ID_ private _" $ stamp.; + $ Mdt = md5 ($ mdt); + + $ Data = array ("key" => "here to write public", "time" => $ stamp, "md5" => $ mdt, "type" => 1, "mk_type" => "cny", + "Price" => "0.0001", "amount" => "100", "coinname" => "XRP"); + // $ Data_string = json_encode ($ data); + $ Ch = curl_init (); + curl_setopt ($ ch, CURLOPT_URL, 'http://www.btc38.com/trade/t_api/submitOrder.php'); + curl_setopt ($ ch, CURLOPT_POST, 1); + curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); + curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($ ch, CURLOPT_HEADER, 0); */ + + static CURL *cHandle; + char *data,*path,url[1024],cmdbuf[8192],buf[512],digest[33],market[16],base[64],rel[64],coinname[16],fmtstr[512],*pricefmt,*volfmt = "%.3f"; + cJSON *json,*resultobj; uint64_t nonce,txid = 0; + if ( _base != 0 && _rel != 0 ) + { + strcpy(base,_base), strcpy(rel,_rel); + touppercase(base), touppercase(rel); + if ( btc38_supports(base,rel) == 0 ) + { + *retstrp = clonestr("{\"error\":\"invalid contract pair\"}"); + return(0); + } + } + nonce = exchange_nonce(exchange); + sprintf(buf,"%s_%s_%s_%llu",exchange->apikey,exchange->userid,exchange->apisecret,(long long)nonce); + //printf("MD5.(%s)\n",buf); + calc_md5(digest,buf,(int32_t)strlen(buf)); + *retstrp = 0; + if ( dir == 0 ) + { + path = "getMyBalance.php"; + sprintf(cmdbuf,"key=%s&time=%llu&md5=%s",exchange->apikey,(long long)nonce,digest); + } + else + { + if ( (dir= cny_flip(market,coinname,base,rel,dir,&price,&volume)) == 0 ) + { + fprintf(stderr,"btc38_trade illegal base.(%s) or rel.(%s)\n",base,rel); + return(0); + } + if ( strcmp(market,"cny") == 0 ) + pricefmt = "%.5f"; + else pricefmt = "%.6f"; + sprintf(fmtstr,"key=%%s&time=%%llu&md5=%%s&type=%%s&mk_type=%%s&coinname=%%s&price=%s&amount=%s",pricefmt,volfmt); + sprintf(cmdbuf,fmtstr,exchange->apikey,(long long)nonce,digest,dir>0?"1":"2",market,coinname,price,volume); + path = "submitOrder.php"; + } + sprintf(url,"http://www.btc38.com/trade/t_api/%s",path); + if ( (data= curl_post(&cHandle,url,0,cmdbuf,0,0,0,0)) != 0 ) + { + //printf("submit cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + if ( juint(json,"success") > 0 && (resultobj= cJSON_GetObjectItem(json,"return")) != 0 ) + { + if ( (txid= get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"order_id"))) == 0 ) + { + if ( get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"remains")) == 0 ) + txid = _crc32(0,cmdbuf,strlen(cmdbuf)); + } + } + free_json(json); + } + } else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); + if ( retstrp != 0 && data != 0 ) + { + if ( (json= cJSON_Parse(data)) == 0 ) + { + json = cJSON_CreateObject(); + jaddstr(json,"result",data); + data = jprint(json,1); + } else free_json(json); + //printf("btc38 returning.(%s) in %p\n",data,data); + *retstrp = data; + } + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t huobi_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + static CURL *cHandle; + char *data,*extra,*method,pricestr[64],pairstr[64],base[64],rel[64],url[1024],cmdbuf[8192],buf[512],digest[33]; cJSON *json; uint64_t nonce,txid = 0; int32_t type; + nonce = exchange_nonce(exchange); + pricestr[0] = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( dir == 0 ) + { + method = "get_account_info"; + sprintf(buf,"access_key=%s&created=%llu&method=%s&secret_key=%s",exchange->apikey,(long long)nonce,method,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + sprintf(cmdbuf,"access_key=%s&created=%llu&method=%s&sign=%s",exchange->apikey,(long long)nonce,method,digest); + } + else + { + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + if ( extra != 0 && strcmp(extra,"market") == 0 ) + method = (dir > 0) ? "buy_market" : "sell_market"; + else method = (dir > 0) ? "buy" : "sell", sprintf(pricestr,"&price=%.2f",price); + if ( strcmp(pairstr,"btccny") == 0 ) + type = 1; + else if ( strcmp(pairstr,"ltccny") == 0 ) + type = 2; + else + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + sprintf(buf,"access_key=%s&amount=%.4f&coin_type=%d&created=%llu&method=%s%s&secret_key=%s",exchange->apikey,volume,type,(long long)nonce,method,pricestr,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + sprintf(cmdbuf,"access_key=%s&amount=%.4f&coin_type=%d&created=%llu&method=%s%s&sign=%s",exchange->apikey,volume,type,(long long)nonce,method,pricestr,digest); + } + sprintf(url,"https://api.huobi.com/apiv3"); + if ( (data= curl_post(&cHandle,url,0,cmdbuf,"Content-Type:application/x-www-form-urlencoded",0,0,0)) != 0 ) + { + //printf("submit cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + } else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t bityes_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + static CURL *cHandle; + char *data,*extra,*method,pricestr[64],pairstr[64],base[64],rel[64],url[1024],cmdbuf[8192],buf[512],digest[33]; cJSON *json; uint64_t nonce,txid = 0; int32_t type; + nonce = exchange_nonce(exchange); + pricestr[0] = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( dir == 0 ) + { + method = "get_account_info"; + sprintf(buf,"access_key=%s&created=%llu&method=%s&secret_key=%s",exchange->apikey,(long long)nonce,method,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + sprintf(cmdbuf,"access_key=%s&created=%llu&method=%s&sign=%s",exchange->apikey,(long long)nonce,method,digest); + } + else + { + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + if ( extra != 0 && strcmp(extra,"market") == 0 ) + method = (dir > 0) ? "buy_market" : "sell_market"; + else method = (dir > 0) ? "buy" : "sell", sprintf(pricestr,"&price=%.2f",price); + if ( strcmp(pairstr,"btcusd") == 0 ) + type = 1; + else if ( strcmp(pairstr,"ltcusd") == 0 ) + type = 2; + else + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + /* access_key Required Access Key + amount Required Order Amount + coin_type Required Type 1 -BTC 2 -LTC + created Required Submit 10 digits timestamp + method Required Request method: buy_market + sign Required MD5 Signature + Encryption Instance sign=md5(access_key=xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxx&amount=10&coin_type=1&created=1386844119&method=buy_market&secret_key=xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx) + trade_password Optional No sign signature, payment password is required. + */ + sprintf(buf,"access_key=%s&amount=%.4f&coin_type=%d&created=%llu&method=%s%s&secret_key=%s",exchange->apikey,volume,type,(long long)nonce,method,pricestr,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + sprintf(cmdbuf,"access_key=%s&amount=%.4f&coin_type=%d&created=%llu&method=%s%s&sign=%s",exchange->apikey,volume,type,(long long)nonce,method,pricestr,digest); + } + sprintf(url,"https://api.bityes.com/apiv2"); + if ( (data= curl_post(&cHandle,url,0,cmdbuf,"Content-Type:application/x-www-form-urlencoded",0,0,0)) != 0 ) + { + //printf("submit cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + } else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t okcoin_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + static CURL *cHandle; + char *data,*path,*typestr,*extra,pricestr[64],base[64],rel[64],pairstr[64],url[1024],cmdbuf[8192],buf[512],digest[33]; cJSON *json; uint64_t nonce,txid = 0; + nonce = exchange_nonce(exchange); + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( dir == 0 ) + { + path = "userinfo.do"; + sprintf(buf,"api_key=%s&secret_key=%s",exchange->apikey,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + touppercase(digest); + sprintf(cmdbuf,"api_key=%s&sign=%s",exchange->apikey,digest); + } + else + { + path = "trade.do"; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + if ( extra != 0 && strcmp(extra,"market") == 0 ) + typestr = (dir > 0) ? "buy_market" : "sell_market", sprintf(pricestr,"&price=%.2f",price); // docs say market orders put volume in price + else typestr = (dir > 0) ? "buy" : "sell", sprintf(pricestr,"&price=%.2f",price); + sprintf(buf,"amount=%.4f&api_key=%s%ssymbol=%s&type=%s&secret_key=%s",volume,exchange->apikey,pricestr,pairstr,typestr,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + touppercase(digest); + sprintf(cmdbuf,"amount=%.4f&api_key=%s%s&symbol=%s&type=%s&sign=%s",volume,exchange->apikey,pricestr,pairstr,typestr,digest); + } + //printf("MD5.(%s)\n",buf); + sprintf(url,"https://www.okcoin.com/api/v1/%s",path); + if ( (data= curl_post(&cHandle,url,0,cmdbuf,0,0,0,0)) != 0 ) // "{\"Content-type\":\"application/x-www-form-urlencoded\"}","{\"User-Agent\":\"OKCoin Javascript API Client\"}" + { + //printf("submit cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + } else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +uint64_t lakebtc_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + /* LakeBTC provides trading JSON-RPC API interface. HMAC (Hash-based Message Authentication Code) is employed as our authentication mechanisms. You need at 0.1 BTC in your account to retrieve your private key. + + Besides your private key, the client needs to prepare the following attributes + tonce (timestamp in microseconds, i.e., unixtime × 1000000, make sure your clock is correctly adjusted) + accesskey (your registered email address at LakeBTC) + requestmethod (post) + id (JSON-RPC request id, an integer) + method (JSON-RPC method) + params (JSON-RPC parameters) + Concatenate the above parameters with &, in that order. Parameters can be blank. For example, $signature = + tonce=1389067414466757&accesskey=foo@bar.com&requestmethod=post&id=123&method=ticker¶ms= + Create HMAC signature with your private key by using SHA1. $hash = + hash_hmac('sha1', $signature, $privatetkey) #php + Join your email and the hash signature with colon (:), and sign with Base64. $b64 = + base64_encode("foo@bar.com:") #php YXRjQHF3amlhbi5jb206ZmEzM2UzYzg5MDZjg5MzdiYzFiYw== + Set HTTP Header. Note tonce is the same as that in Step 2. + Json-Rpc-Tonce: 1389067414466757 #HTTP HEADER + Authorization: Basic YXRjQHF3amlhbi5jb206ZmEzM2UzYzg5MDZjg5MzdiYzFiYw== #HTTP HEADER + POST params data in JSON format to this url: + https://www.LakeBTC.com/api_v1 + API Methods + getAccountInfo + method=getAccountInfo + params= (i.e., blank)*/ + + static CURL *cHandle; + char *data,*method,buf64[4096],paramstr[128],jsonbuf[1024],base[64],rel[64],pairstr[64],params[1024],dest[512],url[1024],cmdbuf[8192],*sig,hdr1[4096],hdr2[4096],buf[4096]; cJSON *json; uint64_t tonce,nonce,txid = 0; + *retstrp = 0; + params[0] = 0; + nonce = exchange_nonce(exchange); + tonce = (nonce * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); + if ( dir == 0 ) + { + method = "getAccountInfo"; + sprintf(buf,"tonce=%llu&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=",(long long)tonce,exchange->userid,method); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,params); + } + else + { + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + method = (dir > 0) ? "buyOrder" : "sellOrder"; + touppercase(rel); + sprintf(paramstr,"%.2f,%.4f,%s",price,volume,rel); + sprintf(buf,"tonce=%llu&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=%s",(long long)tonce,exchange->userid,method,paramstr); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,paramstr); + } + if ( (sig= hmac_sha1_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),buf)) != 0 ) + { + sprintf(cmdbuf,"%s:%s",exchange->userid,sig); + nn_base64_encode((void *)cmdbuf,strlen(cmdbuf),buf64,sizeof(buf64)); + sprintf(url,"https://www.lakebtc.com/api_v1"); + sprintf(hdr1,"Authorization:Basic %s",buf64); + sprintf(hdr2,"Json-Rpc-Tonce: %llu",(long long)tonce); + if ( (data= curl_post(&cHandle,url,0,jsonbuf,hdr1,hdr2,0,0)) != 0 ) + { + //printf("submit cmd.(%s) [%s]\n",jsonbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + } else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + } + return(txid); +} + +uint64_t quadriga_trade(char **retstrp,struct exchange_info *exchange,char *_base,char *_rel,int32_t dir,double price,double volume) +{ + /* You need to POST 3 fields as a JSON payload to the API in order to perform authentication. + + key – The API Key as shown above + nonce – an integer that must be unique for each API call (we recommend using a UNIX timestamp) + signature – HMAC_SHA256 encrypted string + Signature + + The signature has to be created using a concatenation of the nonce, your client id, the API key and using the MD5 hash of the API Secret as key. The pseudo-algorithm is shown below and you will find code examples in the Appendix. + + HMAC_SHA256 ( nonce + client + key, MD5 ( secret ) ) + Please note the HMAC_SHA256 and MD5 strings are both lower case. + + Using the API shown in Figure 2, the JSON payload will be: + + { + key: "JJHlXeDcFM", + nonce: 1391683499, + signature: "cdbf5cc64c70e1485fcf976cdf367960c2b28cfc28080973ce677cebb6db9681" + } + The signature being calculated using: + + HMAC_SHA256 ( 1391683499 + 3 + JJHlXeDcFM , MD5 ( *9q(;5]necq[otcCTfBeiI_Ug;ErCt]Ywjgg^G;t ) ) + HMAC_SHA256 ( 13916834993JJHlXeDcFM , 230664ae53cbe5a07c6c389910540729 ) + = cdbf5cc64c70e1485fcf976cdf367960c2b28cfc28080973ce677cebb6db9681 + + curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json; charset=utf-8', + 'Content-Length: ' . strlen($data_string)) + ); + + */ + static CURL *cHandle; + char *extra,*sig,*data,*path,pairstr[64],base[64],rel[64],hdr3[4096],url[512],md5secret[128],req[4096],payload[4096],hdr1[4096],hdr2[4096],dest[1024 + 1]; + cJSON *json; uint64_t nonce,txid = 0; + memset(payload,0,sizeof(payload)); + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + nonce = exchange_nonce(exchange); + sprintf(payload,"%llu%s%s",(long long)nonce,exchange->userid,exchange->apikey); + calc_md5(md5secret,exchange->apisecret,(int32_t)strlen(exchange->apisecret)); + if ( (sig= hmac_sha256_str(dest,md5secret,(int32_t)strlen(md5secret),payload)) != 0 ) + { + if ( dir == 0 ) + { + path = "balance"; + sprintf(req,"{\"key\":\"%s\",\"nonce\":%llu,\"signature\":\"%s\"}",exchange->apikey,(long long)nonce,sig); + } + else + { + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + path = (dir > 0) ? "buy" : "sell"; + /*key - API key + signature - signature + nonce - nonce + amount - amount of major currency + price - price to buy at + book - optional, if not specified, will default to btc_cad*/ + sprintf(req,"{\"key\":\"%s\",\"amount\":%.6f,\"price\":%.3f,\"book\":\"%s_%s\",\"nonce\":%llu,\"signature\":\"%s\"}",exchange->apikey,volume,price,base,rel,(long long)nonce,sig); + + //dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + } + sprintf(hdr1,"Content-Type:application/json"), sprintf(hdr2,"charset=utf-8"), sprintf(hdr3,"Content-Length:%ld",(long)strlen(req)); + printf("quadriga req.(%s) -> (%s) [%s %s sig.%s]\n",req,payload,md5secret,payload,sig); + sprintf(url,"https://api.quadrigacx.com/v2/%s",path); + if ( (data= curl_post(&cHandle,url,0,req,hdr1,hdr2,hdr3,0)) != 0 ) + { + printf("[%s]\n",data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + } + return(txid); +} + +uint64_t bitstamp_trade(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + /*signature is a HMAC-SHA256 encoded message containing: nonce, customer ID (can be found here) and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to it's hexadecimal representation (64 uppercase characters).Example (Python): + message = nonce + customer_id + api_key + signature = hmac.new(API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest().upper() + + key - API key + signature - signature + nonce - nonce + */ + + static CURL *cHandle; + char *sig,*data,*path,url[512],req[4096],payload[2048],dest[1024 + 1]; cJSON *json; uint64_t nonce,txid = 0; + memset(payload,0,sizeof(payload)); + nonce = exchange_nonce(exchange); + sprintf(payload,"%llu%s%s",(long long)nonce,exchange->userid,exchange->apikey); + if ( dir == 0 ) + path = "balance"; + else + { + //dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + } + if ( (sig= hmac_sha256_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),payload)) != 0 ) + { + touppercase(sig); + sprintf(req,"{\"key\":\"%s\",\"signature\":\"%s\",\"nonce\":%llu}",exchange->apikey,sig,(long long)nonce); + sprintf(url,"https://www.bitstamp.net/api/%s/",path); + printf("bitstamp.(%s) ->\n",req); + if ( (data= curl_post(&cHandle,url,0,req,0,0,0,0)) != 0 ) + { + printf("[%s]\n",data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + } + return(txid); +} + +uint64_t coinbase_trade(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + /*All REST requests must contain the following headers: + + CB-ACCESS-KEY The api key as a string. + CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message). + CB-ACCESS-TIMESTAMP A timestamp for your request. + CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key. + All request bodies should have content type application/json and be valid JSON. + + Signing a Message + The CB-ACCESS-SIGN header is generated by creating a sha256 HMAC using the base64-decoded + secret key on the prehash string timestamp + method + requestPath + body (where + represents string concatenation) + and base64-encode the output. The timestamp value is the same as the CB-ACCESS-TIMESTAMP header. + + The body is the request body string or omitted if there is no request body (typically for GET requests). + + The method should be UPPER CASE + Remember to first base64-decode the alphanumeric secret string (resulting in 64 bytes) before using it as the key for HMAC. Also, base64-encode the digest output before sending in the header. + */ + static CURL *cHandle; + char *sig,*data,*path,sig64[1024],body[4096],method[64],prehash64[512],prehash[512],cmdbuf[8192],url[1024],decodedsecret[128],hdr1[4096],hdr2[4096],hdr3[4096],hdr4[4096],pairstr[512],dest[SHA512_DIGEST_SIZE*2 + 1]; cJSON *json; int32_t n; uint64_t nonce,txid = 0; + nonce = exchange_nonce(exchange); + cmdbuf[0] = 0; + body[0] = 0; + n = nn_base64_decode((void *)exchange->apisecret,strlen(exchange->apisecret),(void *)decodedsecret,sizeof(decodedsecret)); + if ( dir == 0 ) + path = "accounts", strcpy(method,"GET"); + else + { + path = "trade", strcpy(method,"POST"); + dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + sprintf(cmdbuf,"method=Trade&nonce=%ld&pair=%s&type=%s&rate=%.6f&amount=%.6f",(long)time(NULL),pairstr,dir>0?"buy":"sell",price,volume); + } + touppercase(method); + sprintf(prehash,"%llu%s/%s%s",(long long)nonce,method,path,body); + nn_base64_encode((void *)prehash,strlen(prehash),prehash64,sizeof(prehash64)); + if ( (sig= hmac_sha256_str(dest,decodedsecret,n,prehash64)) != 0 ) + { + nn_base64_encode((void *)sig,strlen(sig),sig64,sizeof(sig64)); + } + //CB-ACCESS-KEY The api key as a string. + //CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message). + //CB-ACCESS-TIMESTAMP A timestamp for your request. + //CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key. + sprintf(hdr1,"CB-ACCESS-KEY:%s",exchange->apikey); + sprintf(hdr2,"CB-ACCESS-SIGN:%s",sig64); + sprintf(hdr3,"CB-ACCESS-TIMESTAMP:%llu",(long long)nonce); + //sprintf(hdr4,"CB-ACCESS-PASSPHRASE:%s; content-type:application/json; charset=utf-8",exchange->userid); + sprintf(hdr4,"CB-ACCESS-PASSPHRASE:%s",exchange->userid); + sprintf(url,"https://api.exchange.coinbase.com/%s",path); + if ( (data= curl_post(&cHandle,url,0,cmdbuf,hdr1,hdr2,hdr3,hdr4)) != 0 ) + { + printf("cmd.(%s) prehash.(%s) n.%d [%s]\n",cmdbuf,prehash,n,data); + //{ "success":1, "return":{ "received":0.1, "remains":0, "order_id":0, "funds":{ "usd":325, "btc":2.498, } } } + if ( (json= cJSON_Parse(data)) != 0 ) + { + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} + +#ifdef enable_exmo +uint64_t exmo_trade(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + /* $req['nonce'] = $NONCE; + + // generate the POST data string + $post_data = http_build_query($req, '', '&'); + + $sign = hash_hmac('sha512', $post_data, $secret); + + // generate the extra headers + $headers = array( + 'Sign: ' . $sign, + 'Key: ' . $key, + ); + */ + static CURL *cHandle; + char *sig,*method,*data,url[512],cmdbuf[8192],hdr1[4096],hdr2[4096],pairstr[512],dest[SHA512_DIGEST_SIZE*2 + 1]; cJSON *json; uint64_t nonce,txid = 0; + nonce = exchange_nonce(exchange); + if ( dir == 0 ) + { + sprintf(cmdbuf,"nonce=%llu?method=get_info",(long long)nonce); + method = "get_info"; + } + else + { + method = "notyet"; + dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + sprintf(cmdbuf,"method=Trade&nonce=%ld&pair=%s&type=%s&rate=%.6f&amount=%.6f",(long)time(NULL),pairstr,dir>0?"buy":"sell",price,volume); + //printf("cmdbuf.(%s) h1.(%s) h2.(%s)\n",cmdbuf,hdr2,hdr1); + } + if ( (sig= hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),cmdbuf)) != 0 ) + sprintf(hdr2,"Sign:%s",sig); + else hdr2[0] = 0; + sprintf(hdr1,"Key:%s",exchange->apikey); + sprintf(url,"https://api.exmo.com/api_v2/%s",method); + sprintf(cmdbuf,"{\"method\":\"get_info\"}"); + if ( (data= curl_post(&cHandle,url,0,cmdbuf,hdr1,hdr2,0,0)) != 0 ) + { + printf("cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + free_json(json); + } + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(txid); +} +#endif +#endif + +uint64_t submit_triggered_nxtae(int32_t dotrade,char **retjsonstrp,int32_t is_MS,char *bidask,uint64_t nxt64bits,char *NXTACCTSECRET,uint64_t assetid,uint64_t qty,uint64_t NXTprice,char *triggerhash,char *comment,uint64_t otherNXT,uint32_t triggerheight) +{ + int32_t deadline = 1 + 20; uint64_t txid = 0; struct destbuf errstr; char cmd[4096],secret[8192],*jsonstr; cJSON *json; + if ( retjsonstrp != 0 ) + *retjsonstrp = 0; + if ( triggerheight != 0 ) + deadline = DEFAULT_NXT_DEADLINE; + escape_code(secret,NXTACCTSECRET); + if ( dotrade == 0 ) + strcpy(secret,""); + sprintf(cmd,"requestType=%s&secretPhrase=%s&feeNQT=%llu&deadline=%d",bidask,secret,(long long)MIN_NQTFEE,deadline); + sprintf(cmd+strlen(cmd),"&%s=%llu&%s=%llu",is_MS!=0?"units":"quantityQNT",(long long)qty,is_MS!=0?"currency":"asset",(long long)assetid); + if ( NXTprice != 0 ) + { + if ( is_MS != 0 ) + sprintf(cmd+strlen(cmd),"&rateNQT=%llu",(long long)NXTprice); + else sprintf(cmd+strlen(cmd),"&priceNQT=%llu",(long long)NXTprice); + } + if ( otherNXT != 0 ) + sprintf(cmd+strlen(cmd),"&recipient=%llu",(long long)otherNXT); + if ( triggerhash != 0 && triggerhash[0] != 0 ) + { + if ( triggerheight == 0 ) + sprintf(cmd+strlen(cmd),"&referencedTransactionFullHash=%s",triggerhash); + else sprintf(cmd+strlen(cmd),"&referencedTransactionFullHash=%s&phased=true&phasingFinishHeight=%u&phasingVotingModel=4&phasingQuorum=1&phasingLinkedFullHash=%s",triggerhash,triggerheight,triggerhash); + } + if ( comment != 0 && comment[0] != 0 ) + sprintf(cmd+strlen(cmd),"&message=%s",comment); + if ( dotrade == 0 ) + { + if ( retjsonstrp != 0 ) + { + json = cJSON_CreateObject(); + jaddstr(json,"submit",cmd); + *retjsonstrp = jprint(json,1); + } + return(0); + } + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + _stripwhite(jsonstr,' '); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + copy_cJSON(&errstr,cJSON_GetObjectItem(json,"error")); + if ( errstr.buf[0] == 0 ) + copy_cJSON(&errstr,cJSON_GetObjectItem(json,"errorDescription")); + if ( errstr.buf[0] != 0 ) + { + printf("submit_triggered_bidask.(%s) -> (%s)\n",cmd,jsonstr); + if ( retjsonstrp != 0 ) + *retjsonstrp = clonestr(errstr.buf); + } + else txid = get_API_nxt64bits(cJSON_GetObjectItem(json,"transaction")); + } + free(jsonstr); + } + return(txid); +} + +int32_t get_assettype(int32_t *numdecimalsp,char *assetidstr) +{ + cJSON *json; char name[64],*jsonstr; uint64_t assetid; int32_t ap_type = -1; //struct assethash *ap,A; + *numdecimalsp = -1; + name[0] = 0; + if ( is_native_crypto(name,calc_nxt64bits(assetidstr)) > 0 ) + { + //printf("found native crypto.(%s) name.(%s)\n",assetidstr,name); + ap_type = 0; + *numdecimalsp = 8; + return(0); + } + if ( (assetid= calc_nxt64bits(assetidstr)) == NXT_ASSETID ) + { + //printf("found NXT_ASSETID.(%s)\n",assetidstr); + ap_type = 0; + *numdecimalsp = 8; + return(0); + } + /*if ( (ap= find_asset(assetid)) != 0 ) + { + *numdecimalsp = ap->decimals; + return(ap->type); + }*/ + memset(name,0,sizeof(name)); + if ( (jsonstr= _issue_getAsset(assetidstr)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( get_cJSON_int(json,"errorCode") == 0 ) + { + //printf("assetstr.(%s)\n",jsonstr); + if ( extract_cJSON_str(name,16,json,"name") <= 0 ) + *numdecimalsp = -1; + else *numdecimalsp = (int32_t)get_cJSON_int(json,"decimals"); + ap_type = 2; + } //else printf("errorcode.%lld (%s)\n",(long long)get_cJSON_int(json,"errorCode"),jsonstr); + free_json(json); + } else printf("cant parse.(%s)\n",jsonstr); + free(jsonstr); + } else printf("couldnt getAsset.(%s)\n",assetidstr); + if ( ap_type < 0 ) + { + if ( (jsonstr= _issue_getCurrency(assetidstr)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( get_cJSON_int(json,"errorCode") == 0 ) + { + if ( extract_cJSON_str(name,16,json,"name") <= 0 ) + *numdecimalsp = -1; + else *numdecimalsp = (int32_t)get_cJSON_int(json,"decimals"); + ap_type = 5; + } + free_json(json); + } + free(jsonstr); + } + } + /*memset(&A,0,sizeof(A)); + A.assetid = assetid; + A.minvol = A.mult = calc_decimals_mult(*numdecimalsp); + A.decimals = *numdecimalsp; + A.type = ap_type; + strcpy(A.name,name); + create_asset(assetid,&A);*/ + return(ap_type); +} + +uint64_t assetmult(char *assetidstr) +{ + int32_t ap_type,decimals; uint64_t mult = 0; + ap_type = get_assettype(&decimals,assetidstr); + if ( decimals >= 0 && decimals <= 8 ) + mult = calc_decimals_mult(decimals); + return(mult); +} + +int32_t assetdecimals(char *assetidstr) +{ + int32_t ap_type,decimals = 0; + ap_type = get_assettype(&decimals,assetidstr); + if ( ap_type == 0 ) + return(8); + return(decimals); +} + +uint64_t min_asset_amount(uint64_t assetid) +{ + char assetidstr[64]; + if ( assetid == NXT_ASSETID ) + return(1); + expand_nxt64bits(assetidstr,assetid); + return(assetmult(assetidstr)); +} + +int32_t get_assetdecimals(uint64_t assetid) +{ + char assetidstr[64]; + if ( assetid == NXT_ASSETID ) + return(8); + expand_nxt64bits(assetidstr,assetid); + return(assetdecimals(assetidstr)); +} + +uint64_t get_assetmult(uint64_t assetid) +{ + char assetidstr[64]; + expand_nxt64bits(assetidstr,assetid); + return(assetmult(assetidstr)); +} + +double get_minvolume(uint64_t assetid) +{ + return(dstr(get_assetmult(assetid))); +} + +int64_t get_asset_quantity(int64_t *unconfirmedp,char *NXTaddr,char *assetidstr) +{ + char cmd[2*MAX_JSON_FIELD],*jsonstr; struct destbuf assetid; int32_t i,n,iter; cJSON *array,*item,*obj,*json; int64_t quantity,qty = 0; + uint64_t assetidbits = calc_nxt64bits(assetidstr); + quantity = *unconfirmedp = 0; + if ( assetidbits == NXT_ASSETID ) + { + sprintf(cmd,"requestType=getBalance&account=%s",NXTaddr); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + qty = get_API_nxt64bits(cJSON_GetObjectItem(json,"balanceNQT")); + *unconfirmedp = get_API_nxt64bits(cJSON_GetObjectItem(json,"unconfirmedBalanceNQT")); + printf("(%s)\n",jsonstr); + free_json(json); + } + free(jsonstr); + } + return(qty); + } + sprintf(cmd,"requestType=getAccount&account=%s",NXTaddr); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + for (iter=0; iter<2; iter++) + { + qty = 0; + array = cJSON_GetObjectItem(json,iter==0?"assetBalances":"unconfirmedAssetBalances"); + if ( is_cJSON_Array(array) != 0 ) + { + n = cJSON_GetArraySize(array); + for (i=0; i 0 ? "placeBidOrder" : "placeAskOrder",nxt64bits,secret,assetid,qty,priceNQT,0,0,0,0); + if ( errstr != 0 ) + sprintf(retbuf,"{\"error\":\"%s\"}",errstr), free(errstr); + else sprintf(retbuf,"{\"result\":\"success\",\"txid\":\"%llu\"}",(long long)txid); + if ( txidp != 0 ) + *txidp = txid; + return(clonestr(retbuf)); +} + +uint64_t submit_to_exchange(void **cHandlep,int32_t dotrade,int32_t exchangeid,char **jsonstrp,uint64_t assetid,uint64_t qty,uint64_t priceNQT,int32_t dir,uint64_t nxt64bits,char *NXTACCTSECRET,char *triggerhash,char *comment,uint64_t otherNXT,char *base,char *rel,double price,double volume,uint32_t triggerheight) +{ + uint64_t txid = 0; + char assetidstr[64],*cmd,*retstr = 0; + int32_t ap_type,decimals; + struct exchange_info *exchange; + *jsonstrp = 0; + expand_nxt64bits(assetidstr,assetid); + ap_type = get_assettype(&decimals,assetidstr); + if ( dir == 0 || priceNQT == 0 ) + cmd = (ap_type == 2 ? "transferAsset" : "transferCurrency"), priceNQT = 0; + else cmd = ((dir > 0) ? (ap_type == 2 ? "placeBidOrder" : "currencyBuy") : (ap_type == 2 ? "placeAskOrder" : "currencySell")), otherNXT = 0; + if ( exchangeid == INSTANTDEX_NXTAEID || exchangeid == INSTANTDEX_UNCONFID ) + { + if ( assetid != NXT_ASSETID && qty != 0 && (dir == 0 || priceNQT != 0) ) + { + printf("submit to exchange.%s (%s) dir.%d\n",Exchanges[exchangeid].name,comment,dir); + txid = submit_triggered_nxtae(dotrade,jsonstrp,ap_type == 5,cmd,nxt64bits,NXTACCTSECRET,assetid,qty,priceNQT,triggerhash,comment,otherNXT,triggerheight); + if ( *jsonstrp != 0 ) + txid = 0; + } + } + else if ( exchangeid < MAX_EXCHANGES && (exchange= &Exchanges[exchangeid]) != 0 && exchange->exchangeid == exchangeid && exchange->issue.trade != 0 ) + { + printf("submit_to_exchange.(%d) dir.%d price %f vol %f | inv %f %f (%s)\n",exchangeid,dir,price,volume,1./price,price*volume,comment); + if ( (txid= (*exchange->issue.trade)(cHandlep,dotrade,&retstr,exchange,base,rel,dir,price,volume)) == 0 ) + printf("error trading (%s/%s) dir.%d price %f vol %f ret.(%s)\n",base,rel,dir,price,volume,retstr!=0?retstr:""); + if ( jsonstrp != 0 ) + *jsonstrp = retstr; + } + return(txid); +} + +uint64_t InstantDEX_tradestub(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + printf("this is just a InstantDEX_tradestub\n"); + return(0); +} + +uint64_t NXT_tradestub(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + printf("this is just a NXT_tradestub\n"); + return(0); +} + +#endif diff --git a/InstantDEX/exchangeparse.h b/InstantDEX/exchangeparse.h new file mode 100755 index 000000000..1e301cdb2 --- /dev/null +++ b/InstantDEX/exchangeparse.h @@ -0,0 +1,1208 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#ifndef xcode_exchangeparse_h +#define xcode_exchangeparse_h + + +char *Supported_exchanges[] = { INSTANTDEX_NAME, INSTANTDEX_NXTAEUNCONF, INSTANTDEX_NXTAENAME, INSTANTDEX_BASKETNAME, "basketNXT", "basketUSD", "basketBTC", "basketCNY", INSTANTDEX_ACTIVENAME, "wallet", "jumblr", "pangea", "peggy", // peggy MUST be last of special exchanges + "bitfinex", "btc38", "bitstamp", "btce", "poloniex", "bittrex", "huobi", "coinbase", "okcoin", "lakebtc", "quadriga", + //"bityes", "kraken", "gatecoin", "quoine", "jubi", "hitbtc" // no trading for these exchanges yet +}; // "bter" <- orderbook is backwards and all entries are needed, later to support, "exmo" flakey apiservers + +void init_exchanges() +{ + int32_t i; + for (FIRST_EXTERNAL=0; FIRST_EXTERNAL %llu BTC -> %llu\n",(long long)stringbits("NXT"),(long long)stringbits("BTC")); + //INSTANTDEX.readyflag = 1; +} + +int32_t supported_exchange(char *exchangestr) +{ + int32_t i; + for (i=0; iname[0] == 0 ) + break; + cJSON_AddItemToObject(item,"name",cJSON_CreateString(exchange->name)); + memset(api,0,sizeof(api)); + n = 0; + if ( exchange->issue.trade != 0 ) + { + //printf("%s.(%s/%s/%s).%p\n",exchange->name,exchange->apikey,exchange->apisecret,exchange->userid,exchange); + if ( exchange->apikey[0] != 0 ) + api[n++] = 'K'; + if ( exchange->apisecret[0] != 0 ) + api[n++] = 'S'; + if ( exchange->userid[0] != 0 ) + api[n++] = 'U'; + api[n] = 0; + cJSON_AddItemToObject(item,"trade",cJSON_CreateString(api)); + } + cJSON_AddItemToArray(array,item); + } + return(array); +} + +int32_t is_exchange_nxt64bits(uint64_t nxt64bits) +{ + int32_t exchangeid; + struct exchange_info *exchange = 0; + for (exchangeid=0; exchangeidname,(long long)exchange->nxt64bits,(long long)nxt64bits); + if ( exchange->name[0] == 0 ) + return(0); + if ( exchange->nxt64bits == nxt64bits ) + return(1); + } + printf("no exchangebits match\n"); + return(0); +} + +struct exchange_info *get_exchange(int32_t exchangeid) { return(&Exchanges[exchangeid]); } +char *exchange_str(int32_t exchangeid) { return(Exchanges[exchangeid].name); } + +struct exchange_info *exchange_find(char *exchangestr) +{ + int32_t exchangeid; + struct exchange_info *exchange = 0; + for (exchangeid=0; exchangeidname) == 0 ) + return(exchange); + } + return(0); +} + +struct exchange_info *find_exchange(int32_t *exchangeidp,char *exchangestr) +{ + int32_t exchangeid; struct exchange_info *exchange = 0; + if ( supported_exchange(exchangestr) < 0 ) + { + if ( exchangeidp != 0 ) + *exchangeidp = -1; + return(0); + } + for (exchangeid=0; exchangeidname); + if ( exchange->name[0] == 0 ) + { + portable_mutex_init(&exchange->mutex); + strcpy(exchange->name,exchangestr); + exchange->exchangeid = exchangeid; + exchange->nxt64bits = stringbits(exchangestr); + printf("CREATE EXCHANGE.(%s) id.%d %llu\n",exchangestr,exchangeid,(long long)exchange->nxt64bits); + //if ( exchangestr[0] == 0 ) + // getchar(); + break; + } + if ( strcmp(exchangestr,exchange->name) == 0 ) + break; + } + if ( exchange != 0 && exchangeidp != 0 ) + *exchangeidp = exchange->exchangeid; + return(exchange); +} + +int32_t baserel_polarity(char *pairs[][2],int32_t n,char *_base,char *_rel) +{ + int32_t i; char base[16],rel[16]; + strcpy(base,_base), tolowercase(base); + strcpy(rel,_rel), tolowercase(rel); + for (i=0; i (%s)\n",url,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + hbla = prices777_json_orderbook(exchangestr,prices,maxdepth,json,field,"bids","asks",price,volume); + free_json(json); + } + free(jsonstr); + } + return(hbla); +} + +int32_t InstantDEX_supports(char *base,char *rel) { return(1); } + +int32_t NXT_supports(char *base,char *rel) +{ + if ( strcmp(rel,"NXT") == 0 ) + return(1); + else if ( strcmp(base,"NXT") == 0 ) + return(-1); + else return(0); +} + +#ifdef notnow +char *bittrex_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + int32_t i,n; char *str,*itemstr = 0; cJSON *item,*array,*obj; double total,pending; + *balancep = 0.; + if ( exchange->balancejson != 0 && (array= jarray(&n,exchange->balancejson,"result")) != 0 ) + { + for (i=0; i 5 || strlen(rel) > 5 || strcmp(rel,"CNY") == 0 || strcmp(base,"CNY") == 0 || strcmp(rel,"USD") == 0 || strcmp(base,"USD") == 0 ) + return(0); + if ( strcmp(rel,"BTC") == 0 ) + return(1); + else if ( strcmp(base,"BTC") == 0 ) + return(-1); + else return(0); +} + +double prices777_bittrex(struct prices777 *prices,int32_t maxdepth) // "BTC-BTCD" +{ + cJSON *json,*obj; char *jsonstr,market[128]; double hbla = 0.; + if ( prices->url[0] == 0 ) + { + sprintf(market,"%s-%s",prices->rel,prices->base); + sprintf(prices->url,"https://bittrex.com/api/v1.1/public/getorderbook?market=%s&type=both&depth=%d",market,maxdepth); + } + jsonstr = issue_curl(prices->url); + if ( jsonstr != 0 ) + { + if ( (json = cJSON_Parse(jsonstr)) != 0 ) + { + if ( (obj= cJSON_GetObjectItem(json,"success")) != 0 && is_cJSON_True(obj) != 0 ) + hbla = prices777_json_orderbook("bittrex",prices,maxdepth,json,"result","buy","sell","Rate","Quantity"); + free_json(json); + } + free(jsonstr); + } + return(hbla); +} + +/*int32_t bter_supports(char *base,char *rel) +{ + return(0); + if ( strcmp(rel,"BTC") == 0 || strcmp(rel,"CNY") == 0 ) + return(1); + else if ( strcmp(base,"BTC") == 0 || strcmp(base,"CNY") == 0 ) + return(-1); + else return(0); + char *bterassets[][8] = { { "UNITY", "12071612744977229797" }, { "ATOMIC", "11694807213441909013" }, { "DICE", "18184274154437352348" }, { "MRKT", "134138275353332190" }, { "MGW", "10524562908394749924" } }; + uint64_t unityid = calc_nxt64bits("12071612744977229797"); + n = add_exchange_assetids(assetids,n,BTC_ASSETID,baseid,relid,exchangeid,bterassets,(int32_t)(sizeof(bterassets)/sizeof(*bterassets))); + if ( baseid == unityid || relid == unityid ) + { + n = add_exchange_assetid(assetids,n,unityid,BTC_ASSETID,exchangeid); + n = add_exchange_assetid(assetids,n,unityid,NXT_ASSETID,exchangeid); + n = add_exchange_assetid(assetids,n,unityid,CNY_ASSETID,exchangeid); + } + return(n); +} + +double prices777_bter(struct prices777 *prices,int32_t maxdepth) +{ + cJSON *json,*obj; char resultstr[MAX_JSON_FIELD],*jsonstr; double hbla = 0.; + if ( prices->url[0] == 0 ) + sprintf(prices->url,"http://data.bter.com/api/1/depth/%s_%s",prices->base,prices->rel); + jsonstr = issue_curl(prices->url); + //printf("(%s) -> (%s)\n",ep->url,jsonstr); + //{"result":"true","asks":[["0.00008035",100],["0.00008030",2030],["0.00008024",100],["0.00008018",643.41783554],["0.00008012",100] + if ( jsonstr != 0 ) + { + //printf("BTER.(%s)\n",jsonstr); + if ( (json = cJSON_Parse(jsonstr)) != 0 ) + { + if ( (obj= cJSON_GetObjectItem(json,"result")) != 0 ) + { + copy_cJSON(resultstr,obj); + if ( strcmp(resultstr,"true") == 0 ) + { + maxdepth = MAX_DEPTH;//1000; // since bter ask is wrong order, need to scan entire list + hbla = prices777_json_orderbook("bter",prices,maxdepth,json,0,"bids","asks",0,0); + } + } + free_json(json); + } + free(jsonstr); + } + return(hbla); +}*/ + +char *poloniex_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char *itemstr = 0; cJSON *item,*obj; double onorders,btcvalue; + *balancep = 0.; + if ( exchange->balancejson != 0 && (item= jobj(exchange->balancejson,coinstr)) != 0 ) + { + itemstr = jprint(item,0); + *balancep = jdouble(item,"available"); + onorders = jdouble(item,"onOrders"); + btcvalue = jdouble(item,"btcValue"); + if ( (obj= cJSON_Parse(itemstr)) != 0 ) + { + free(itemstr); + jaddstr(obj,"base",coinstr); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"onOrders",onorders); + jaddnum(obj,"btcvalue",btcvalue); + itemstr = jprint(obj,1); + } + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t poloniex_supports(char *base,char *rel) +{ + if ( strlen(base) > 5 || strlen(rel) > 5 || strcmp(rel,"CNY") == 0 || strcmp(base,"CNY") == 0 || strcmp(rel,"USD") == 0 || strcmp(base,"USD") == 0 ) + return(0); + if ( strcmp(rel,"BTC") == 0 ) + return(1); + else if ( strcmp(base,"BTC") == 0 ) + return(-1); + else return(0); + //char *poloassets[][8] = { { "UNITY", "12071612744977229797" }, { "JLH", "6932037131189568014" }, { "XUSD", "12982485703607823902" }, { "LQD", "4630752101777892988" }, { "NXTI", "14273984620270850703" }, { "CNMT", "7474435909229872610", "6220108297598959542" } }; + //return(add_exchange_assetids(assetids,n,BTC_ASSETID,baseid,relid,exchangeid,poloassets,(int32_t)(sizeof(poloassets)/sizeof(*poloassets)))); +} + +double prices777_poloniex(struct prices777 *prices,int32_t maxdepth) +{ + char market[128]; + if ( prices->url[0] == 0 ) + { + sprintf(market,"%s_%s",prices->rel,prices->base); + sprintf(prices->url,"https://poloniex.com/public?command=returnOrderBook¤cyPair=%s&depth=%d",market,maxdepth); + } + return(prices777_standard("poloniex",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t kraken_supports(char *_base,char *_rel) +{ + char *supports[] = { "BTC", "ETH", "LTC", "NMC", "STR", "DOGE", "XVN", "XRP", "USD", "CAD", "JPY", "GBP", "KRW" }; + int32_t i,j; char base[64],rel[64]; + strcpy(base,_base), strcpy(rel,_rel); + touppercase(base), touppercase(rel); + if ( strlen(base) > 5 || strlen(rel) > 5 ) + return(0); + for (i=0; ibase), strcpy(rel,prices->rel); + touppercase(base), touppercase(rel); + if ( strcmp(base,"BTC") == 0 ) + strcpy(base,"XBT"); + if ( strcmp(rel,"BTC") == 0 ) + strcpy(rel,"XBT"); + if ( strcmp(base,"DOGE") == 0 ) + strcpy(base,"XDG"); + if ( strcmp(rel,"DOGE") == 0 ) + strcpy(rel,"XDG"); + sprintf(field,"X%sZ%s",base,rel); + if ( prices->url[0] == 0 ) + { + sprintf(market,"%s%s",base,rel); + sprintf(prices->url,"https://api.kraken.com/0/public/Depth?pair=%s&count=%d",market,maxdepth); + } + if ( (jsonstr= issue_curl(prices->url)) != 0 ) + { +//{"error":[],"result":{"XXBTZUSD":{"asks":[["230.31677","1.438",1440886427],["230.31678","7.229",1440886068],["230.77732","0.012",1440876801],["230.77833","9.642",1440885707],["231.24081","9.719",1440884428]],"bids":[["228.04086","3.052",1440886443],["228.04085","0.590",1440886446],["228.04076","9.550",1440886434],["227.58559","10.214",1440800610],["227.56018","5.000",1440881811]]}}} + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + //printf("got.(%s)\n",jsonstr); + hbla = prices777_json_orderbook("kraken",prices,maxdepth,jobj(json,"result"),field,"bids","asks",0,0); + free_json(json); + } + free(jsonstr); + } + return(hbla); +} + +char *bitfinex_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //[[{"type":"deposit","currency":"btc","amount":"0.0","available":"0.0"},{"type":"deposit","currency":"usd","amount":"0.0","available":"0.0"},{"type":"exchange","currency":"btc","amount":"0.01065851","available":"0.01065851"},{"type":"exchange","currency":"usd","amount":"23386.37278962","available":"0.00378962"},{"type":"trading","currency":"btc","amount":"0.0","available":"0.0"},{"type":"trading","currency":"usd","amount":"0.0","available":"0.0"}]] + int32_t i,n,ind; char field[64],*str,*typestr,*itemstr = 0; cJSON *item,*obj,*array; double amounts[3],avail[3],val0,val1; + *balancep = 0.; + strcpy(field,coinstr), tolowercase(field); + memset(amounts,0,sizeof(amounts)); + memset(avail,0,sizeof(avail)); + if ( exchange->balancejson != 0 && is_cJSON_Array(exchange->balancejson) != 0 && (n= cJSON_GetArraySize(exchange->balancejson)) > 0 ) + { + for (i=0; ibalancejson,i)) != 0 ) + { + if ( (str= jstr(item,"currency")) != 0 && strcmp(field,str) == 0 ) + { + val0 = jdouble(item,"amount"); + val1 = jdouble(item,"available"); + if ( (typestr= jstr(item,"type")) != 0 ) + { + if ( strcmp(typestr,"deposit") == 0 ) + ind = 0; + else if ( strcmp(typestr,"exchange") == 0 ) + ind = 1; + else if ( strcmp(typestr,"trading") == 0 ) + ind = 2; + else ind = -1; + if ( ind >= 0 ) + { + amounts[ind] = val0; + avail[ind] = val1; + } + } + } + } + } + if ( (obj= cJSON_CreateObject()) != 0 ) + { + touppercase(field); + *balancep = avail[0] + avail[1] + avail[2]; + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"total",amounts[0]+amounts[1]+amounts[2]); + array = cJSON_CreateArray(), jaddinum(array,avail[0]), jaddinum(array,amounts[0]), jadd(obj,"deposit",array); + array = cJSON_CreateArray(), jaddinum(array,avail[1]), jaddinum(array,amounts[1]), jadd(obj,"exchange",array); + array = cJSON_CreateArray(), jaddinum(array,avail[2]), jaddinum(array,amounts[2]), jadd(obj,"trading",array); + itemstr = jprint(obj,1); + } + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t bitfinex_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"ltc","usd"}, {"ltc","btc"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_bitfinex(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.bitfinex.com/v1/book/%s%s",prices->base,prices->rel); + return(prices777_standard("bitfinex",prices->url,prices,"price","amount",maxdepth,0)); +} + +char *btce_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //btce.({"success":1,"return":{"funds":{"usd":73.02571846,"btc":0,"ltc":0,"nmc":0,"rur":0,"eur":0,"nvc":0.0000322,"trc":0,"ppc":0.00000002,"ftc":0,"xpm":2.28605349,"cnh":0,"gbp":0},"rights":{"info":1,"trade":1,"withdraw":0},"transaction_count":0,"open_orders":3,"server_time":1441918649}}) + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t btce_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"btc","rur"}, {"btc","eur"}, {"ltc","btc"}, {"ltc","usd"}, {"ltc","rur"}, {"ltc","eur"}, {"nmc","btc"}, {"nmc","usd"}, {"nvc","btc"}, {"nvc","usd"}, {"eur","usd"}, {"eur","rur"}, {"ppc","btc"}, {"ppc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_btce(struct prices777 *prices,int32_t maxdepth) +{ + char field[64]; + sprintf(field,"%s_%s",prices->lbase,prices->lrel); + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://btc-e.com/api/3/depth/%s",field); + return(prices777_standard("btce",prices->url,prices,0,0,maxdepth,field)); +} + +char *bitstamp_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t bitstamp_supports(char *base,char *rel) +{ + if ( strcmp(base,"BTC") == 0 && strcmp(rel,"USD") == 0 ) + return(1); + else if ( strcmp(rel,"BTC") == 0 && strcmp(base,"USD") == 0 ) + return(-1); + else return(0); +} + +double prices777_bitstamp(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://www.bitstamp.net/api/order_book/"); + return(prices777_standard("bitstamp",prices->url,prices,0,0,maxdepth,0)); +} + +char *okcoin_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //okcoin.({"info":{"funds":{"asset":{"net":"0","total":"0"},"free":{"btc":"0","ltc":"0","usd":"0"},"freezed":{"btc":"0","ltc":"0","usd":"0"}}},"result":true}) + char field[128],*itemstr = 0; cJSON *obj,*item,*avail,*locked; double lockval = 0; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"info")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + if ( (avail= jobj(item,"free")) != 0 ) + *balancep = jdouble(avail,field); + if ( (locked= jobj(item,"freezed")) != 0 ) + lockval = jdouble(locked,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked",lockval); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t okcoin_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"ltc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_okcoin(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://www.okcoin.com/api/v1/depth.do?symbol=%s_%s",prices->lbase,prices->lrel); + if ( strcmp(prices->rel,"USD") != 0 && strcmp(prices->rel,"BTC") != 0 ) + { + fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FATAL ERROR OKCOIN.(%s) only supports USD\n",prices->url); + printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FATAL ERROR OKCOIN.(%s) only supports USD\n",prices->url); + exit(-1); + return(0); + } + return(prices777_standard("okcoin",prices->url,prices,0,0,maxdepth,0)); +} + +char *huobi_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t huobi_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","cny"}, {"ltc","cny"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_huobi(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"http://api.huobi.com/staticmarket/depth_%s_json.js ",prices->lbase); + return(prices777_standard("huobi",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t bityes_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"ltc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_bityes(struct prices777 *prices,int32_t maxdepth) +{ + //if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://market.bityes.com/%s_%s/depth.js?time=%ld",prices->lrel,prices->lbase,(long)time(NULL)); + return(prices777_standard("bityes",prices->url,prices,0,0,maxdepth,0)); +} + +char *coinbase_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t coinbase_supports(char *base,char *rel) +{ + if ( strcmp(base,"BTC") == 0 && strcmp(rel,"USD") == 0 ) + return(1); + else if ( strcmp(rel,"BTC") == 0 && strcmp(base,"USD") == 0 ) + return(-1); + else return(0); +} + +double prices777_coinbase(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.exchange.coinbase.com/products/%s-%s/book?level=2",prices->base,prices->rel); + return(prices777_standard("coinbase",prices->url,prices,0,0,maxdepth,0)); +} + +char *lakebtc_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //lakebtc.({"balance":{"BTC":0.1},"locked":{"BTC":0.0},"profile":{"email":"jameslee777@yahoo.com","id":"U137561934","btc_deposit_addres":"1RyKrNJjezeFfvYaicnJEozHfhWfYzbuh"}}) + char field[128],*str,*itemstr = 0; cJSON *obj=0,*item=0,*prof=0; double locked = 0; + *balancep = 0.; + strcpy(field,coinstr); + touppercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"balance")) != 0 && (item= jobj(exchange->balancejson,"locked")) != 0 && (prof= jobj(exchange->balancejson,"profile")) != 0 ) + { + *balancep = jdouble(obj,field); + locked = jdouble(item,field); + obj = cJSON_CreateObject(); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked",locked); + if ( (str= jstr(prof,"btc_deposit_addres")) != 0 ) + jaddstr(obj,"deposit_address",str); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t lakebtc_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"btc","cny"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_lakebtc(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + { + if ( strcmp(prices->rel,"USD") == 0 ) + sprintf(prices->url,"https://www.LakeBTC.com/api_v1/bcorderbook"); + else if ( strcmp(prices->rel,"CNY") == 0 ) + sprintf(prices->url,"https://www.LakeBTC.com/api_v1/bcorderbook_cny"); + else printf("illegal lakebtc pair.(%s/%s)\n",prices->base,prices->rel); + } + return(prices777_standard("lakebtc",prices->url,prices,0,0,maxdepth,0)); +} + +#ifdef exmo_supported +int32_t exmo_supports(char *base,char *rel) +{ + if ( strcmp(base,"BTC") == 0 && (strcmp(rel,"USD") == 0 || strcmp(rel,"EUR") == 0 || strcmp(rel,"RUR") == 0) ) + return(1); + else if ( strcmp(rel,"BTC") == 0 && (strcmp(base,"USD") == 0 || strcmp(base,"EUR") == 0 || strcmp(base,"RUR") == 0) ) + return(-1); + else return(0); +} + +double prices777_exmo(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.exmo.com/api_v2/orders_book?pair=%s_%s",prices->base,prices->rel); + return(prices777_standard("exmo",prices->url,prices,0,0,maxdepth,0)); +} +#endif + +// "gatecoin", "quoine", "jubi", "hitbtc" + +char *btc38_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*str,*itemstr = 0; cJSON *obj; double lockbalance,imma; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + strcat(field,"_balance"); + if ( exchange->balancejson != 0 && (str= jstr(exchange->balancejson,field)) != 0 ) + { + *balancep = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance_lock"); + lockbalance = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance_imma"); + imma = jdouble(exchange->balancejson,field); + obj = cJSON_CreateObject(); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked_balance",lockbalance); + jaddnum(obj,"imma_balance",imma); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t btc38_supports(char *_base,char *_rel) +{ + char *cnypairs[] = { "BTC", "LTC", "DOGE", "XRP", "BTS", "STR", "NXT", "BLK", "YBC", "BILS", "BOST", "PPC", "APC", "ZCC", "XPM", "DGC", "MEC", "WDC", "QRK", "BEC", "ANC", "UNC", "RIC", "SRC", "TAG" }; + char *btcpairs[] = { "TMC", "LTC", "DOGE", "XRP", "BTS", "STR", "NXT", "BLK", "XEM", "VPN", "BILS", "BOST", "WDC", "ANC", "XCN", "VOOT", "SYS", "NRS", "NAS", "SYNC", "MED", "EAC" }; + int32_t i; char base[64],rel[64]; + strcpy(base,_base), strcpy(rel,_rel); + touppercase(base), touppercase(rel); + if ( strlen(base) > 5 || strlen(rel) > 5 ) + return(0); + if ( strcmp(base,"BTC") == 0 && strcmp(rel,"CNY") == 0 ) + return(1); + else if ( strcmp(base,"CNY") == 0 && strcmp(rel,"BTC") == 0 ) + return(-1); + else if ( strcmp(base,"BTC") == 0 ) + { + for (i=0; iurl[0] == 0 ) + { + if ( strcmp(prices->lbase,"cny") == 0 && strcmp(prices->lrel,"btc") == 0 ) + sprintf(prices->url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s","btc","cny"); + else sprintf(prices->url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s",prices->lbase,prices->lrel); + } + return(prices777_standard("btc38",prices->url,prices,0,0,maxdepth,0)); +} + +char *quadriga_parsebalance(struct exchange_info *exchange,double *balancep,char *coinstr) +{ +//[{"btc_available":"0.00000000","btc_reserved":"0.00000000","btc_balance":"0.00000000","cad_available":"0.00","cad_reserved":"0.00","cad_balance":"0.00","usd_available":"0.00","usd_reserved":"0.00","usd_balance":"0.00","xau_available":"0.000000","xau_reserved":"0.000000","xau_balance":"0.000000","fee":"0.5000"}] + char field[128],*str,*itemstr = 0; cJSON *obj; double reserv,total; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + strcat(field,"_available"); + if ( exchange->balancejson != 0 && (str= jstr(exchange->balancejson,field)) != 0 ) + { + *balancep = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_reserved"); + reserv = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance"); + total = jdouble(exchange->balancejson,field); + obj = cJSON_CreateObject(); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked_balance",reserv); + jaddnum(obj,"total",total); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +int32_t quadriga_supports(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"btc","cad"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +double prices777_quadriga(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.quadrigacx.com/v2/order_book?book=%s_%s",prices->lbase,prices->lrel); + return(prices777_standard("quadriga",prices->url,prices,0,0,maxdepth,0)); +} + + +/*void prices777_kraken(struct prices777 *prices,int32_t maxdepth) + { + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.kraken.com/0/public/Depth"); // need POST + prices777_standard("kraken",prices->url,prices,0,0,maxdepth); + }*/ + +/*void prices777_itbit(struct prices777 *prices,int32_t maxdepth) + { + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://www.itbit.com/%s%s",prices->base,prices->rel); + prices777_standard("itbit",prices->url,prices,0,0,maxdepth); + }*/ +#endif + +uint64_t prices777_truefx(uint64_t *millistamps,double *bids,double *asks,double *opens,double *highs,double *lows,char *username,char *password,uint64_t idnum) +{ + char *truefxfmt = "http://webrates.truefx.com/rates/connect.html?f=csv&id=jl777:truefxtest:poll:1437671654417&c=EUR/USD,USD/JPY,GBP/USD,EUR/GBP,USD/CHF,AUD/NZD,CAD/CHF,CHF/JPY,EUR/AUD,EUR/CAD,EUR/JPY,EUR/CHF,USD/CAD,AUD/USD,GBP/JPY,AUD/CAD,AUD/CHF,AUD/JPY,EUR/NOK,EUR/NZD,GBP/CAD,GBP/CHF,NZD/JPY,NZD/USD,USD/NOK,USD/SEK"; + + // EUR/USD,1437569931314,1.09,034,1.09,038,1.08922,1.09673,1.09384 USD/JPY,1437569932078,123.,778,123.,781,123.569,123.903,123.860 GBP/USD,1437569929008,1.56,332,1.56,337,1.55458,1.56482,1.55538 EUR/GBP,1437569931291,0.69,742,0.69,750,0.69710,0.70383,0.70338 USD/CHF,1437569932237,0.96,142,0.96,153,0.95608,0.96234,0.95748 EUR/JPY,1437569932237,134.,960,134.,972,134.842,135.640,135.476 EUR/CHF,1437569930233,1.04,827,1.04,839,1.04698,1.04945,1.04843 USD/CAD,1437569929721,1.30,231,1.30,241,1.29367,1.30340,1.29466 AUD/USD,1437569931700,0.73,884,0.73,890,0.73721,0.74395,0.74200 GBP/JPY,1437569931924,193.,500,193.,520,192.298,193.670,192.649 + char url[1024],userpass[1024],buf[128],base[64],rel[64],*str; cJSON *array; int32_t jpyflag,i,c,n = 0; double pre,pre2,bid,ask,open,high,low; long millistamp; + //printf("truefx.(%s)(%s).%llu\n",username,password,(long long)idnum); + url[0] = 0; + if ( username[0] != 0 && password[0] != 0 ) + { + if ( idnum == 0 ) + { + sprintf(userpass,"http://webrates.truefx.com/rates/connect.html?f=csv&s=y&u=%s&p=%s&q=poll",username,password); + if ( (str= issue_curl(userpass)) != 0 ) + { + _stripwhite(str,0); + printf("(%s) -> (%s)\n",userpass,str); + sprintf(userpass,"%s:%s:poll:",username,password); + idnum = calc_nxt64bits(str + strlen(userpass)); + free(str); + printf("idnum.%llu\n",(long long)idnum); + } + } + if ( idnum != 0 ) + sprintf(url,truefxfmt,username,password,(long long)idnum); + } + if ( url[0] == 0 ) + sprintf(url,"http://webrates.truefx.com/rates/connect.html?f=csv&s=y"); + if ( (str= issue_curl(url)) != 0 ) + { + //printf("(%s) -> (%s)\n",url,str); + while ( str[n + 0] != 0 && str[n] != '\n' && str[n] != '\r' ) + { + for (i=jpyflag=0; str[n + i]!=' '&&str[n + i]!='\n'&&str[n + i]!='\r'&&str[n + i]!=0; i++) + { + if ( i > 0 && str[n+i] == ',' && str[n+i-1] == '.' ) + str[n+i-1] = ' ', jpyflag = 1; + else if ( i > 0 && str[n+i-1] == ',' && str[n+i] == '0' && str[n+i+1+2] == ',' ) + { + str[n+i] = ' '; + if ( str[n+i+1] == '0' ) + str[n+i+1] = ' ', i++; + } + } + memcpy(base,str+n,3), base[3] = 0; + memcpy(rel,str+n+4,3), rel[3] = 0; + str[n + i] = 0; + //printf("str.(%s) (%s/%s) %d n.%d i.%d\n",str+n,base,rel,str[n],n,i); + sprintf(buf,"[%s]",str+n+7+1); + n += i + 1; + if ( (array= cJSON_Parse(buf)) != 0 ) + { + if ( is_cJSON_Array(array) != 0 ) + { + millistamp = (uint64_t)get_API_float(jitem(array,0)); + pre = get_API_float(jitem(array,1)); + bid = get_API_float(jitem(array,2)); + pre2 = get_API_float(jitem(array,3)); + ask = get_API_float(jitem(array,4)); + open = get_API_float(jitem(array,5)); + high = get_API_float(jitem(array,6)); + low = get_API_float(jitem(array,7)); + if ( jpyflag != 0 ) + bid = pre + (bid / 1000.), ask = pre2 + (ask / 1000.); + else bid = pre + (bid / 100000.), ask = pre2 + (ask / 100000.); + if ( (c= prices777_contractnum(base,rel)) >= 0 ) + { + char name[64]; + strcpy(name,base), strcat(name,rel); + if ( BUNDLE.truefx[c] == 0 ) + BUNDLE.truefx[c] = prices777_initpair(0,"truefx",base,rel,0,name,stringbits(base),stringbits(rel),0); + millistamps[c] = millistamp,opens[c] = open, highs[c] = high, lows[c] = low, bids[c] = bid, asks[c] = ask; + if ( Debuglevel > 2 ) + { + if ( jpyflag != 0 ) + printf("%s%s.%-2d %llu: %.3f %.3f %.3f | %.3f %.3f\n",base,rel,c,(long long)millistamp,open,high,low,bid,ask); + else printf("%s%s.%-2d %llu: %.5f %.5f %.5f | %.5f %.5f\n",base,rel,c,(long long)millistamp,open,high,low,bid,ask); + } + } else printf("unknown basepair.(%s) (%s)\n",base,rel); + } + free_json(array); + } else printf("cant parse.(%s)\n",buf); + } + free(str); + } + return(idnum); +} + + +double prices777_fxcm(double lhlogmatrix[8][8],double logmatrix[8][8],double bids[64],double asks[64],double highs[64],double lows[64]) +{ + char name[64],*xmlstr,*str; cJSON *json,*obj; int32_t i,j,c,flag,k,n = 0; double bid,ask,high,low; struct destbuf numstr; + memset(bids,0,sizeof(*bids) * NUM_CONTRACTS), memset(asks,0,sizeof(*asks) * NUM_CONTRACTS); + if ( (xmlstr= issue_curl("http://rates.fxcm.com/RatesXML")) != 0 ) + { + _stripwhite(xmlstr,0); + //printf("(%s)\n",xmlstr); + i = 0; + if ( strncmp("'&&xmlstr[i]!=0; i++) + ; + if ( xmlstr[i] == '>' ) + i++; + for (j=0; xmlstr[i]!=0; i++) + { + if ( strncmp("",&xmlstr[i],strlen("")) == 0 ) + xmlstr[j++] = '[', i += strlen("")-1; + else if ( strncmp(" 1 ) + xmlstr[j++] = ','; + memcpy(&xmlstr[j],"{\"Symbol\":",strlen("{\"Symbol\":")), i += strlen("", "\"Bid\":" }, { "", "\"Ask\":" }, { "", "\"High\":" }, { "", "\"Low\":" }, { "", "\"Direction\":" }, { "", "\"Last\":\"" } }; + for (k=0; k", "", "", "", "", "", "", "", ">" }; + for (k=0; k",ends[k]) == 0 ) + xmlstr[j++] = '}'; + else if ( strcmp("",ends[k]) == 0 ) + xmlstr[j++] = ']'; + else if ( strcmp("",ends[k]) == 0 ) + xmlstr[j++] = '\"'; + else xmlstr[j++] = ','; + break; + } + if ( k == sizeof(ends)/sizeof(*ends) ) + xmlstr[j++] = xmlstr[i]; + } + } + } + xmlstr[j] = 0; + if ( (json= cJSON_Parse(xmlstr)) != 0 ) + { + /* + 123.763 + 123.786 + 123.956 + 123.562 + -1 + 08:49:15*/ + //printf("Parsed stupid XML! (%s)\n",xmlstr); + if ( is_cJSON_Array(json) != 0 && (n= cJSON_GetArraySize(json)) != 0 ) + { + for (i=0; i= 0 ) + { + bids[c] = bid, asks[c] = ask, highs[c] = high, lows[c] = low; + //printf("c.%d (%s) %f %f\n",c,name,bid,ask); + flag = 1; + if ( BUNDLE.fxcm[c] == 0 ) + { + //printf("max.%ld FXCM: not initialized.(%s) %d\n",sizeof(CONTRACTS)/sizeof(*CONTRACTS),name,c); + BUNDLE.fxcm[c] = prices777_initpair(0,"fxcm",name,0,0,name,peggy_basebits(name),peggy_relbits(name),0); + } + } else printf("cant find.%s\n",name);//, getchar(); + } + if ( flag == 0 ) + printf("FXCM: Error finding.(%s) c.%d (%s)\n",name,c,cJSON_Print(obj)); + } + } + free_json(json); + } else printf("couldnt parse.(%s)\n",xmlstr); + free(xmlstr); + } + calc_primary_currencies(lhlogmatrix,lows,highs); + return(calc_primary_currencies(logmatrix,bids,asks)); +} + +double prices777_instaforex(double logmatrix[8][8],uint32_t timestamps[NUM_COMBINED+1],double bids[128],double asks[128]) +{ + //{"NZDUSD":{"symbol":"NZDUSD","lasttime":1437580206,"digits":4,"change":"-0.0001","bid":"0.6590","ask":"0.6593"}, + char *jsonstr,*str; cJSON *json,*item; int32_t i,c; struct destbuf numstr; + memset(timestamps,0,sizeof(*timestamps) * (NUM_COMBINED + 1)), memset(bids,0,sizeof(*bids) * (NUM_COMBINED + 1)), memset(asks,0,sizeof(*asks) * (NUM_COMBINED + 1)); + if ( (jsonstr= issue_curl("https://quotes.instaforex.com/get_quotes.php?q=NZDUSD,NZDCHF,NZDCAD,NZDJPY,GBPNZD,EURNZD,AUDNZD,CADJPY,CADCHF,USDCAD,EURCAD,GBPCAD,AUDCAD,USDCHF,CHFJPY,EURCHF,GBPCHF,AUDCHF,EURUSD,EURAUD,EURJPY,EURGBP,GBPUSD,GBPJPY,GBPAUD,USDJPY,AUDJPY,AUDUSD,XAUUSD&m=json")) != 0 ) + { + // printf("(%s)\n",jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + for (i=0; i<=NUM_CONTRACTS; i++) + { + if ( i < NUM_CONTRACTS ) + str = CONTRACTS[i], c = i; + else str = "XAUUSD", c = prices777_contractnum(str,0); + if ( (item= jobj(json,str)) != 0 ) + { + timestamps[c] = juint(item,"lasttime"); + copy_cJSON(&numstr,jobj(item,"bid")), bids[c] = atof(numstr.buf); + copy_cJSON(&numstr,jobj(item,"ask")), asks[c] = atof(numstr.buf); + //if ( c < NUM_CONTRACTS && Contract_rel[c] == JPY ) + // bids[i] /= 100., asks[i] /= 100.; + if ( Debuglevel > 2 ) + printf("%s.(%.6f %.6f) ",str,bids[c],asks[c]); + if ( BUNDLE.instaforex[c] == 0 ) + BUNDLE.instaforex[c] = prices777_initpair(0,"instaforex",str,0,0,str,peggy_basebits(str),peggy_relbits(str),0); + } + } + free_json(json); + } + free(jsonstr); + } + return(calc_primary_currencies(logmatrix,bids,asks)); +} + +int32_t prices777_ecbparse(char *date,double *prices,char *url,int32_t basenum) +{ + char *jsonstr,*relstr,*basestr; int32_t count=0,i,relnum; cJSON *json,*ratesobj,*item; struct destbuf tmp; + if ( (jsonstr= issue_curl(url)) != 0 ) + { + if ( Debuglevel > 2 ) + printf("(%s)\n",jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + copy_cJSON(&tmp,jobj(json,"date")), safecopy(date,tmp.buf,64); + if ( (basestr= jstr(json,"base")) != 0 && strcmp(basestr,CURRENCIES[basenum]) == 0 && (ratesobj= jobj(json,"rates")) != 0 && (item= ratesobj->child) != 0 ) + { + while ( item != 0 ) + { + if ( (relstr= get_cJSON_fieldname(item)) != 0 && (relnum= prices777_basenum(relstr)) >= 0 ) + { + i = basenum*MAX_CURRENCIES + relnum; + prices[i] = item->valuedouble; + //if ( basenum == JPYNUM ) + // prices[i] *= 100.; + // else if ( relnum == JPYNUM ) + // prices[i] /= 100.; + count++; + if ( Debuglevel > 2 ) + printf("(%02d:%02d %f) ",basenum,relnum,prices[i]); + } else printf("cant find.(%s)\n",relstr);//, getchar(); + item = item->next; + } + } + free_json(json); + } + free(jsonstr); + } + return(count); +} + +int32_t prices777_ecb(char *date,double *prices,int32_t year,int32_t month,int32_t day) +{ + // http://api.fixer.io/latest?base=CNH + // http://api.fixer.io/2000-01-03?base=USD + // "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD" + char baseurl[512],tmpdate[64],url[512],checkdate[16]; int32_t basenum,count,i,iter,nonz; + checkdate[0] = 0; + if ( year == 0 ) + strcpy(baseurl,"http://api.fixer.io/latest?base="); + else + { + sprintf(checkdate,"%d-%02d-%02d",year,month,day); + sprintf(baseurl,"http://api.fixer.io/%s?base=",checkdate); + } + count = 0; + for (iter=0; iter<2; iter++) + { + for (basenum=0; basenum 2 ) + printf("%8.5f ",prices[MAX_CURRENCIES*basenum + i]); + } + if ( Debuglevel > 2 ) + printf("%s.%d %d\n",CURRENCIES[basenum],basenum,nonz); + } + } + } + return(count); +} + +#endif + diff --git a/InstantDEX/exchanges/bitfinex.c b/InstantDEX/exchanges/bitfinex.c new file mode 100755 index 000000000..7eb43cf1d --- /dev/null +++ b/InstantDEX/exchanges/bitfinex.c @@ -0,0 +1,259 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "bitfinex" +#define UPDATE prices777_ ## bitfinex +#define SUPPORTS bitfinex ## _supports +#define SIGNPOST bitfinex ## _signpost +#define TRADE bitfinex ## _trade +#define ORDERSTATUS bitfinex ## _orderstatus +#define CANCELORDER bitfinex ## _cancelorder +#define OPENORDERS bitfinex ## _openorders +#define TRADEHISTORY bitfinex ## _tradehistory +#define BALANCES bitfinex ## _balances +#define PARSEBALANCE bitfinex ## _parsebalance +#define WITHDRAW bitfinex ## _withdraw +#define CHECKBALANCE bitfinex ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.bitfinex.com/v1/book/%s%s",prices->base,prices->rel); + return(prices777_standard(EXCHANGE_NAME,prices->url,prices,"price","amount",maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"ltc","usd"}, {"ltc","btc"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //[[{"type":"deposit","currency":"btc","amount":"0.0","available":"0.0"},{"type":"deposit","currency":"usd","amount":"0.0","available":"0.0"},{"type":"exchange","currency":"btc","amount":"0.01065851","available":"0.01065851"},{"type":"exchange","currency":"usd","amount":"23386.37278962","available":"0.00378962"},{"type":"trading","currency":"btc","amount":"0.0","available":"0.0"},{"type":"trading","currency":"usd","amount":"0.0","available":"0.0"}]] + int32_t i,n,ind; char field[64],*str,*typestr,*itemstr = 0; cJSON *item,*obj,*array; double amounts[3],avail[3],val0,val1; + *balancep = 0.; + strcpy(field,coinstr), tolowercase(field); + memset(amounts,0,sizeof(amounts)); + memset(avail,0,sizeof(avail)); + if ( exchange->balancejson != 0 && is_cJSON_Array(exchange->balancejson) != 0 && (n= cJSON_GetArraySize(exchange->balancejson)) > 0 ) + { + for (i=0; ibalancejson,i)) != 0 ) + { + if ( (str= jstr(item,"currency")) != 0 && strcmp(field,str) == 0 ) + { + val0 = jdouble(item,"amount"); + val1 = jdouble(item,"available"); + if ( (typestr= jstr(item,"type")) != 0 ) + { + if ( strcmp(typestr,"deposit") == 0 ) + ind = 0; + else if ( strcmp(typestr,"exchange") == 0 ) + ind = 1; + else if ( strcmp(typestr,"trading") == 0 ) + ind = 2; + else ind = -1; + if ( ind >= 0 ) + { + amounts[ind] = val0; + avail[ind] = val1; + } + } + } + } + } + if ( (obj= cJSON_CreateObject()) != 0 ) + { + touppercase(field); + *balancep = avail[0] + avail[1] + avail[2]; + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"total",amounts[0]+amounts[1]+amounts[2]); + array = cJSON_CreateArray(), jaddinum(array,avail[0]), jaddinum(array,amounts[0]), jadd(obj,"deposit",array); + array = cJSON_CreateArray(), jaddinum(array,avail[1]), jaddinum(array,amounts[1]), jadd(obj,"exchange",array); + array = cJSON_CreateArray(), jaddinum(array,avail[2]), jaddinum(array,amounts[2]), jadd(obj,"trading",array); + itemstr = jprint(obj,1); + } + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *method) +{ + char dest[1025],url[1024],hdr1[512],hdr2[512],hdr3[512],hdr4[512],req[1024],*sig,*data = 0; cJSON *json; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + nn_base64_encode((void *)payload,strlen(payload),req,sizeof(req)); + if ( (sig= hmac_sha384_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),req)) != 0 ) + { + sprintf(hdr1,"X-BFX-APIKEY:%s",exchange->apikey); + sprintf(hdr2,"X-BFX-PAYLOAD:%s",req); + sprintf(hdr3,"X-BFX-SIGNATURE:%s",sig); + //printf("req.(%s) H0.(%s) H1.(%s) H2.(%s)\n",req,hdr1,hdr2,hdr3); + sprintf(url,"https://api.bitfinex.com/v1/%s",method); + if ( dotrade == 0 ) + data = exchange_would_submit(req,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,req,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + } + return(json); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + char payload[1024],*method; + method = "balances"; + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\"}",method,(long long)exchange_nonce(exchange)); + return(SIGNPOST(cHandlep,1,0,exchange,payload,method)); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],pairstr[512],*typestr,*method,*extra; cJSON *json; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + method = "order/new"; + //Either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders) + if ( (typestr= extra) == 0 ) + typestr = "exchange limit"; + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"exchange\":\"bitfinex\",\"side\":\"%s\",\"type\":\"%s\",\"price\":\"%.8f\",\"amount\":\"%.8f\",\"symbol\":\"%s\"}",method,(long long)exchange_nonce(exchange),dir>0?"buy":"sell",typestr,price,volume,pairstr); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,payload,method)) != 0 ) + { + if ( (txid= j64bits(json,"order_id")) == 0 ) + { + if ( dir != 0 ) + printf("bitfinex: no txid error\n"); + } + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*method,*retstr = 0; cJSON *json; + method = "order/status"; + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"order_id\":%llu}",method,(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,method)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized orderstatus +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*method,*retstr = 0; cJSON *json; + method = "order/cancel"; + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"order_id\":%llu}",method,(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,method)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*method,*retstr = 0; cJSON *json; + method = "orders"; + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\"}",method,(long long)exchange_nonce(exchange)); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,method)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],baserel[16],*method,*base,*rel,*retstr = 0; uint32_t timestamp; cJSON *json; + method = "mytrades"; + base = jstr(argjson,"base"); + rel = jstr(argjson,"rel"); + if ( base == 0 || rel == 0 ) + { + base = "BTC"; + rel = "USD"; + } + sprintf(baserel,"%s%s",base,rel); + timestamp = juint(argjson,"start"); + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"symbol\":\"%s\",\"timestamp\":%u}",method,(long long)exchange_nonce(exchange),baserel,timestamp); + //printf("TRADEHISTORY.(%s)\n",payload); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,method)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized tradehistory +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*method,*base,*destaddr,*type,*retstr = 0; cJSON *json; double amount; + amount = jdouble(argjson,"amount"); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 || amount < SMALLVAL ) + return(clonestr("{\"error\":\"no destaddr specified or amount too small\"}")); + if ( (base= jstr(argjson,"base")) == 0 ) + base = "bitcoin"; + else if ( strcmp(base,"BTC") == 0 ) + base = "bitcoin"; + else if ( strcmp(base,"LTC") == 0 ) + base = "litecoin"; + else if ( strcmp(base,"DRK") == 0 ) + base = "darkcoin"; + else return(clonestr("{\"error\":\"invalid base specified\"}")); + if ( (type= jstr(argjson,"extra")) == 0 ) + type = "exchange"; + else if ( strcmp(type,"exchange") != 0 && strcmp(type,"trading") != 0 && strcmp(type,"deposit") != 0 ) + return(clonestr("{\"error\":\"invalid wallet type specified\"}")); + method = "withdraw"; + sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"amount\":\"%.6f\",\"withdraw_type\":\"%s\",\"walletselected\":\"%s\",\"address\":\"%s\"}",method,(long long)exchange_nonce(exchange),amount,base,type,destaddr); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,method)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized withdraw +} + +struct exchange_funcs bitfinex_funcs = EXCHANGE_FUNCS(bitfinex,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef CHECKBALANCE diff --git a/InstantDEX/exchanges/bitstamp.c b/InstantDEX/exchanges/bitstamp.c new file mode 100755 index 000000000..e27700153 --- /dev/null +++ b/InstantDEX/exchanges/bitstamp.c @@ -0,0 +1,170 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "bitstamp" +#define UPDATE prices777_ ## bitstamp +#define SUPPORTS bitstamp ## _supports +#define SIGNPOST bitstamp ## _signpost +#define TRADE bitstamp ## _trade +#define ORDERSTATUS bitstamp ## _orderstatus +#define CANCELORDER bitstamp ## _cancelorder +#define OPENORDERS bitstamp ## _openorders +#define TRADEHISTORY bitstamp ## _tradehistory +#define BALANCES bitstamp ## _balances +#define PARSEBALANCE bitstamp ## _parsebalance +#define WITHDRAW bitstamp ## _withdraw +#define EXCHANGE_AUTHURL "https://www.bitstamp.net/api" +#define CHECKBALANCE bitstamp ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://www.bitstamp.net/api/order_book/"); + return(prices777_standard("bitstamp",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *method,char *payload) +{ + /*signature is a HMAC-SHA256 encoded message containing: nonce, customer ID (can be found here) and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to it's hexadecimal representation (64 uppercase characters).Example (Python): + message = nonce + customer_id + api_key + signature = hmac.new(API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest().upper() + + key - API key + signature - signature + nonce - nonce + */ + char dest[1025],url[1024],req[1024],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*sig,*data = 0; + cJSON *json; uint64_t nonce; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + nonce = exchange_nonce(exchange); + sprintf(req,"%llu%s%s",(long long)nonce,exchange->userid,exchange->apikey); + json = 0; + if ( (sig= hmac_sha256_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),req)) != 0 ) + { + //touppercase(sig); + //printf("req.(%s) sig.(%s)\n",req,sig); + //sprintf(req,"{\"key\":\"%s\",\"signature\":\"%s\",\"nonce\":%llu%s}",exchange->apikey,sig,(long long)nonce,payload); + sprintf(req,"key=%s&signature=%s&nonce=%llu%s",exchange->apikey,sig,(long long)nonce,payload); + //printf("submit.(%s)\n",req); + sprintf(url,"%s/%s/",EXCHANGE_AUTHURL,method); + if ( dotrade == 0 ) + data = exchange_would_submit(req,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,req,req,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + return(SIGNPOST(cHandlep,1,0,exchange,"balance","")); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],url[512],pairstr[512],*extra; cJSON *json; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + sprintf(url,"%s/%s/",EXCHANGE_AUTHURL,dir>0 ? "buy" : "sell"); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,url,payload)) != 0 ) + { + // parse json and set txid + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char jsonbuf[128]; + sprintf(jsonbuf,"&id=%llu",(long long)quoteid); + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"order_status",jsonbuf),1)); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char jsonbuf[128]; + sprintf(jsonbuf,"&id=%llu",(long long)quoteid); + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"cancel_order",jsonbuf),1)); +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"open_orders",""),1)); +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"user_transactions",""),1)); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(clonestr("{\"error\":\"withdraw not yet\"}")); +} + +struct exchange_funcs bitstamp_funcs = EXCHANGE_FUNCS(bitstamp,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef EXCHANGE_AUTHURL +#undef CHECKBALANCE diff --git a/InstantDEX/exchanges/bittrex.c b/InstantDEX/exchanges/bittrex.c new file mode 100755 index 000000000..807f69566 --- /dev/null +++ b/InstantDEX/exchanges/bittrex.c @@ -0,0 +1,247 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "bittrex" +#define UPDATE prices777_ ## bittrex +#define SUPPORTS bittrex ## _supports +#define SIGNPOST bittrex ## _signpost +#define TRADE bittrex ## _trade +#define ORDERSTATUS bittrex ## _orderstatus +#define CANCELORDER bittrex ## _cancelorder +#define OPENORDERS bittrex ## _openorders +#define TRADEHISTORY bittrex ## _tradehistory +#define BALANCES bittrex ## _balances +#define PARSEBALANCE bittrex ## _parsebalance +#define WITHDRAW bittrex ## _withdraw +#define CHECKBALANCE bittrex ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + cJSON *json,*obj; char *jsonstr,market[128]; double hbla = 0.; + if ( prices->url[0] == 0 ) + { + sprintf(market,"%s-%s",prices->rel,prices->base); + sprintf(prices->url,"https://bittrex.com/api/v1.1/public/getorderbook?market=%s&type=both&depth=%d",market,maxdepth); + } + jsonstr = issue_curl(prices->url); + if ( jsonstr != 0 ) + { + if ( (json = cJSON_Parse(jsonstr)) != 0 ) + { + if ( (obj= cJSON_GetObjectItem(json,"success")) != 0 && is_cJSON_True(obj) != 0 ) + hbla = prices777_json_orderbook("bittrex",prices,maxdepth,json,"result","buy","sell","Rate","Quantity"); + free_json(json); + } + free(jsonstr); + } + return(hbla); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + if ( strlen(base) > 5 || strlen(rel) > 5 || strcmp(rel,"CNY") == 0 || strcmp(base,"CNY") == 0 || strcmp(rel,"USD") == 0 || strcmp(base,"USD") == 0 ) + return(0); + if ( strcmp(rel,"BTC") == 0 ) + return(1); + else if ( strcmp(base,"BTC") == 0 ) + return(-1); + else return(0); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) +{ + char dest[SHA512_DIGEST_SIZE*2+1],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*data,*sig; cJSON *json; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + if ( (sig= hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),payload)) != 0 ) + sprintf(hdr1,"apisign:%s",sig); + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,payload,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + char payload[1024]; + sprintf(payload,"https://bittrex.com/api/v1.1/account/getbalances?apikey=%s&nonce=%llu",exchange->apikey,(long long)exchange_nonce(exchange)); + return(SIGNPOST(cHandlep,1,0,exchange,payload,payload)); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + int32_t i,n; char *str,*itemstr = 0; cJSON *item,*array,*obj; double total,pending; + *balancep = 0.; + if ( exchange->balancejson != 0 && (array= jarray(&n,exchange->balancejson,"result")) != 0 ) + { + for (i=0; i0?"buy":"sell",exchange->apikey,(long long)exchange_nonce(exchange),pairstr,price,volume); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,payload,payload)) != 0 ) + { + if ( is_cJSON_True(cJSON_GetObjectItem(json,"success")) != 0 && (resultobj= cJSON_GetObjectItem(json,"result")) != 0 ) + { + copy_cJSON(&uuidstr,cJSON_GetObjectItem(resultobj,"uuid")); + for (i=j=0; uuidstr.buf[i]!=0; i++) + if ( uuidstr.buf[i] != '-' ) + uuidstr.buf[j++] = uuidstr.buf[i]; + uuidstr.buf[j] = 0; + n = (int32_t)strlen(uuidstr.buf); + printf("-> uuidstr.(%s).%d\n",uuidstr.buf,n); + decode_hex(databuf,n/2,uuidstr.buf); + if ( n >= 16 ) + for (i=0; i<8; i++) + databuf[i] ^= databuf[8 + i]; + memcpy(&txid,databuf,8); + printf("-> %llx\n",(long long)txid); + } + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + sprintf(payload,"https://bittrex.com/api/v1.1/account/getorder?apikey=%s&nonce=%llu&uuid=%llu",exchange->apikey,(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized orderstatus +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + sprintf(payload,"https://bittrex.com/api/v1.1/market/cancel?apikey=%s&nonce=%llu&uuid=%llu",exchange->apikey,(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],market[64],*base,*rel,*retstr = 0; cJSON *json; + sprintf(payload,"https://bittrex.com/api/v1.1/market/getopenorders?apikey=%s&nonce=%llu",exchange->apikey,(long long)exchange_nonce(exchange)); + if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 ) + { + sprintf(market,"%s-%s",rel,base); + sprintf(payload + strlen(payload),"&market=%s",market); + } + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],market[64],*base,*rel,*retstr = 0; cJSON *json; + sprintf(payload,"https://bittrex.com/api/v1.1/account/getorderhistory?apikey=%s&nonce=%llu",exchange->apikey,(long long)exchange_nonce(exchange)); + if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 ) + { + sprintf(market,"%s-%s",rel,base); + sprintf(payload + strlen(payload),"&market=%s",market); + } + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized tradehistory +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*destaddr,*paymentid,*base,*retstr = 0; cJSON *json; double amount; + if ( (base= jstr(argjson,"base")) == 0 ) + return(clonestr("{\"error\":\"base not specified\"}")); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 ) + return(clonestr("{\"error\":\"destaddr not specified\"}")); + if ( (amount= jdouble(argjson,"amount")) < SMALLVAL ) + return(clonestr("{\"error\":\"amount not specified\"}")); + paymentid = jstr(argjson,"paymentid"); + sprintf(payload,"https://bittrex.com/api/v1.1/account/withdraw?apikey=%s&nonce=%llu¤cy=%s&amount=%.4f&address=%s",exchange->apikey,(long long)exchange_nonce(exchange),base,amount,destaddr); + if ( paymentid != 0 ) + sprintf(payload + strlen(payload),"&paymentid=%s",paymentid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized withdraw +} + +struct exchange_funcs bittrex_funcs = EXCHANGE_FUNCS(bittrex,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef CHECKBALANCE diff --git a/InstantDEX/exchanges/btc38.c b/InstantDEX/exchanges/btc38.c new file mode 100755 index 000000000..9dabb495a --- /dev/null +++ b/InstantDEX/exchanges/btc38.c @@ -0,0 +1,321 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "btc38" +#define UPDATE prices777_ ## btc38 +#define SUPPORTS btc38 ## _supports +#define SIGNPOST btc38 ## _signpost +#define TRADE btc38 ## _trade +#define ORDERSTATUS btc38 ## _orderstatus +#define CANCELORDER btc38 ## _cancelorder +#define OPENORDERS btc38 ## _openorders +#define TRADEHISTORY btc38 ## _tradehistory +#define BALANCES btc38 ## _balances +#define PARSEBALANCE btc38 ## _parsebalance +#define WITHDRAW btc38 ## _withdraw +#define EXCHANGE_AUTHURL "http://www.btc38.com/trade/t_api" +#define CHECKBALANCE btc38 ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + { + if ( strcmp(prices->lbase,"cny") == 0 && strcmp(prices->lrel,"btc") == 0 ) + sprintf(prices->url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s","btc","cny"); + else sprintf(prices->url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s",prices->lbase,prices->lrel); + } + return(prices777_standard("btc38",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *_base,char *_rel) +{ + char *cnypairs[] = { "BTC", "LTC", "DOGE", "XRP", "BTS", "STR", "NXT", "BLK", "BC", "VPN", "BILS", "BOST", "PPC", "APC", "ZCC", "XPM", "DGC", "MEC", "WDC", "QRK", "BEC", "ANC", "UNC", "RIC", "SRC", "TAG" }; + char *btcpairs[] = { "TMC", "LTC", "DOGE", "XRP", "BTS", "XEM", "VPN", "XCN", "VOOT", "SYS", "NRS", "NAS", "SYNC", "MED", "EAC" }; + int32_t i; char base[64],rel[64]; + strcpy(base,_base), strcpy(rel,_rel); + touppercase(base), touppercase(rel); + if ( strlen(base) > 5 || strlen(rel) > 5 ) + return(0); + if ( strcmp(base,"BTC") == 0 && strcmp(rel,"CNY") == 0 ) + return(1); + else if ( strcmp(base,"CNY") == 0 && strcmp(rel,"BTC") == 0 ) + return(-1); + else if ( strcmp(base,"BTC") == 0 ) + { + for (i=0; iapikey,exchange->userid,exchange->apisecret,(long long)nonce); + //printf("MD5.(%s)\n",buf); + calc_md5(digest,buf,(int32_t)strlen(buf)); + sprintf(cmdbuf,"key=%s&time=%llu&md5=%s%s",exchange->apikey,(long long)nonce,digest,payload); + sprintf(url,"%s/%s",EXCHANGE_AUTHURL,path); + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,cmdbuf,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +/* $ Stamp = $ date-> getTimestamp (); + type, 1 for the purchase of Entry, 2 entry order to sell, can not be empty / the type of the order + + $ Mdt = "_ public here to write here write here to write user ID_ private _" $ stamp.; + $ Mdt = md5 ($ mdt); + + $ Data = array ("key" => "here to write public", "time" => $ stamp, "md5" => $ mdt, "type" => 1, "mk_type" => "cny", + "Price" => "0.0001", "amount" => "100", "coinname" => "XRP"); + // $ Data_string = json_encode ($ data); + $ Ch = curl_init (); + curl_setopt ($ ch, CURLOPT_URL, 'http://www.btc38.com/trade/t_api/submitOrder.php'); + curl_setopt ($ ch, CURLOPT_POST, 1); + curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); + curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($ ch, CURLOPT_HEADER, 0); */ +/* +static CURL *cHandle; +char *data,*path,url[1024],cmdbuf[8192],buf[512],digest[33],market[16],base[64],rel[64],coinname[16],fmtstr[512],*pricefmt,*volfmt = "%.3f"; +cJSON *json,*resultobj; uint64_t nonce,txid = 0; +if ( _base != 0 && _rel != 0 ) +{ + strcpy(base,_base), strcpy(rel,_rel); + touppercase(base), touppercase(rel); + if ( btc38_supports(base,rel) == 0 ) + { + *retstrp = clonestr("{\"error\":\"invalid contract pair\"}"); + return(0); + } +} +nonce = exchange_nonce(exchange); +sprintf(buf,"%s_%s_%s_%llu",exchange->apikey,exchange->userid,exchange->apisecret,(long long)nonce); +//printf("MD5.(%s)\n",buf); +calc_md5(digest,buf,(int32_t)strlen(buf)); +*retstrp = 0; +if ( dir == 0 ) +{ + path = "getMyBalance.php"; + sprintf(cmdbuf,"key=%s&time=%llu&md5=%s",exchange->apikey,(long long)nonce,digest); +} +else +{ +if ( (data= curl_post(&cHandle,url,0,cmdbuf,0,0,0,0)) != 0 ) +{ + //printf("submit cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + if ( juint(json,"success") > 0 && (resultobj= cJSON_GetObjectItem(json,"return")) != 0 ) + { + if ( (txid= get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"order_id"))) == 0 ) + { + if ( get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"remains")) == 0 ) + txid = _crc32(0,cmdbuf,strlen(cmdbuf)); + } + } + free_json(json); + } +} else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); +if ( retstrp != 0 && data != 0 ) +{ + if ( (json= cJSON_Parse(data)) == 0 ) + { + json = cJSON_CreateObject(); + jaddstr(json,"result",data); + data = jprint(json,1); + } else free_json(json); + //printf("btc38 returning.(%s) in %p\n",data,data); + *retstrp = data; + } +else if ( data != 0 ) +free(data); +return(txid); +*/ + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + return(SIGNPOST(cHandlep,1,0,exchange,"","getMyBalance.php")); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*str,*itemstr = 0; cJSON *obj; double lockbalance,imma; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + strcat(field,"_balance"); + if ( exchange->balancejson != 0 && (str= jstr(exchange->balancejson,field)) != 0 ) + { + *balancep = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance_lock"); + lockbalance = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance_imma"); + imma = jdouble(exchange->balancejson,field); + obj = cJSON_CreateObject(); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked_balance",lockbalance); + jaddnum(obj,"imma_balance",imma); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],market[16],coinname[16],fmtstr[512],*pricefmt,*extra,*volfmt = "%.3f"; + cJSON *json,*resultobj; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= cny_flip(market,coinname,base,rel,dir,&price,&volume)) == 0 ) + { + fprintf(stderr,"btc38_trade illegal base.(%s) or rel.(%s)\n",base,rel); + return(0); + } + if ( strcmp(market,"cny") == 0 ) + pricefmt = "%.5f"; + else pricefmt = "%.6f"; + //sprintf(fmtstr,"key=%%s&time=%%llu&md5=%%s&type=%%s&mk_type=%%s&coinname=%%s&price=%s&amount=%s",pricefmt,volfmt); + //sprintf(payload,fmtstr,exchange->apikey,(long long)nonce,digest,dir>0?"1":"2",market,coinname,price,volume); + sprintf(fmtstr,"&type=%%s&mk_type=%%s&coinname=%%s&price=%s&amount=%s",pricefmt,volfmt); + sprintf(payload,fmtstr,dir>0?"1":"2",market,coinname,price,volume); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,payload,"submitOrder.php")) != 0 ) + { + if ( juint(json,"success") > 0 && (resultobj= jobj(json,"return")) != 0 ) + { + if ( (txid= j64bits(resultobj,"order_id")) == 0 ) + { + if ( j64bits(resultobj,"remains") == 0 ) + txid = calc_crc32(0,payload,strlen(payload)); + } + } + free_json(json); + if ( retstrp != 0 && *retstrp != 0 ) + { + if ( (json= cJSON_Parse(*retstrp)) == 0 ) + { + json = cJSON_CreateObject(); + jaddstr(json,"result",*retstrp); + free(*retstrp); + *retstrp = jprint(json,1); + } else free_json(json); + } + } + return(txid); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*rel,*retstr = 0; cJSON *json; + if ( (rel= jstr(argjson,"rel")) == 0 ) + rel = "cny"; + sprintf(payload,"&mk_type=%s&order_id=%llu",rel,(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,"cancelOrder.php")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*base,*rel,*retstr = 0; cJSON *json; + if ( (rel= jstr(argjson,"rel")) == 0 ) + rel = "cny"; + sprintf(payload,"&mk_type=%s",rel); + if ( (base= jstr(argjson,"base")) != 0 ) + sprintf(payload + strlen(payload),"&coinname=%s",base); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,"getOrderList.php")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(clonestr("{\"error\":\"btc38 doesnt seem to have trade history api!\"}")); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(clonestr("{\"error\":\"btc38 doesnt seem to have withdraw api!\"}")); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char *status,*retstr; + status = OPENORDERS(cHandlep,exchange,argjson); + if ( (retstr= exchange_extractorderid(0,status,quoteid,"order_id")) != 0 ) + { + free(status); + return(retstr); + } + free(status); + return(clonestr("{\"result\":\"order not pending\"}")); +} + +struct exchange_funcs btc38_funcs = EXCHANGE_FUNCS(btc38,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef EXCHANGE_AUTHURL +#undef CHECKBALANCE + diff --git a/InstantDEX/exchanges/btce.c b/InstantDEX/exchanges/btce.c new file mode 100755 index 000000000..6ef9adeb8 --- /dev/null +++ b/InstantDEX/exchanges/btce.c @@ -0,0 +1,215 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "btce" +#define UPDATE prices777_ ## btce +#define SUPPORTS btce ## _supports +#define SIGNPOST btce ## _signpost +#define TRADE btce ## _trade +#define ORDERSTATUS btce ## _orderstatus +#define CANCELORDER btce ## _cancelorder +#define OPENORDERS btce ## _openorders +#define TRADEHISTORY btce ## _tradehistory +#define BALANCES btce ## _balances +#define PARSEBALANCE btce ## _parsebalance +#define WITHDRAW btce ## _withdraw +#define EXCHANGE_AUTHURL "https://btc-e.com/tapi" +#define CHECKBALANCE btce ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + char field[64]; + sprintf(field,"%s_%s",prices->lbase,prices->lrel); + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://btc-e.com/api/3/depth/%s",field); + return(prices777_standard("btce",prices->url,prices,0,0,maxdepth,field)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"btc","rur"}, {"btc","eur"}, {"ltc","btc"}, {"ltc","usd"}, {"ltc","rur"}, {"ltc","eur"}, {"nmc","btc"}, {"nmc","usd"}, {"nvc","btc"}, {"nvc","usd"}, {"eur","usd"}, {"eur","rur"}, {"ppc","btc"}, {"ppc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) +{ + char dest[SHA512_DIGEST_SIZE*2+1],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*data,*sig; cJSON *json; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + if ( (sig= hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),payload)) != 0 ) + sprintf(hdr1,"Sign:%s",sig); + else hdr1[0] = 0; + sprintf(hdr2,"Key:%s",exchange->apikey); + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,payload,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + char payload[1024]; + sprintf(payload,"method=getInfo&nonce=%llu",(long long)exchange_nonce(exchange)); + return(SIGNPOST(cHandlep,1,0,exchange,EXCHANGE_AUTHURL,payload)); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //btce.({"success":1,"return":{"funds":{"usd":73.02571846,"btc":0,"ltc":0,"nmc":0,"rur":0,"eur":0,"nvc":0.0000322,"trc":0,"ppc":0.00000002,"ftc":0,"xpm":2.28605349,"cnh":0,"gbp":0},"rights":{"info":1,"trade":1,"withdraw":0},"transaction_count":0,"open_orders":3,"server_time":1441918649}}) + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],pairstr[512],*extra; cJSON *json,*resultobj; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + sprintf(payload,"method=Trade&nonce=%llu&pair=%s&type=%s&rate=%.3f&amount=%.6f",(long long)exchange_nonce(exchange),pairstr,dir>0?"buy":"sell",price,volume); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + //{ "success":1, "return":{ "received":0.1, "remains":0, "order_id":0, "funds":{ "usd":325, "btc":2.498, } } } + if ( juint(json,"success") > 0 && (resultobj= jobj(json,"return")) != 0 ) + { + if ( (txid= j64bits(resultobj,"order_id")) == 0 ) + { + if ( j64bits(resultobj,"remains") == 0 ) + txid = calc_crc32(0,payload,strlen(payload)); + } + } + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + sprintf(payload,"method=OrderInfo&nonce=%llu&order_id=%llu",(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized orderstatus +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + sprintf(payload,"method=CancelOrder&nonce=%llu&order_id=%llu",(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],market[64],*base,*rel,*retstr = 0; cJSON *json; + sprintf(payload,"method=ActiveOrders&nonce=%llu",(long long)exchange_nonce(exchange)); + if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 ) + { + sprintf(market,"%s_%s",base,rel); + tolowercase(market); + sprintf(payload + strlen(payload),"&pair=%s",market); + } + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],market[64],*base,*rel,*retstr = 0; cJSON *json; uint32_t starttime,endtime; + sprintf(payload,"method=TradeHistory&nonce=%llu",(long long)exchange_nonce(exchange)); + if ( (starttime= juint(argjson,"start")) != 0 ) + sprintf(payload + strlen(payload),"&since=%u",starttime); + if ( (endtime= juint(argjson,"end")) != 0 ) + sprintf(payload + strlen(payload),"&end=%u",endtime); + if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 ) + { + sprintf(market,"%s_%s",base,rel); + tolowercase(market); + sprintf(payload + strlen(payload),"&pair=%s",market); + } + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized tradehistory +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*base,*destaddr,*retstr = 0; cJSON *json; double amount; + if ( (base= jstr(argjson,"base")) == 0 ) + return(clonestr("{\"error\":\"base not specified\"}")); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 ) + return(clonestr("{\"error\":\"destaddr not specified\"}")); + if ( (amount= jdouble(argjson,"amount")) < SMALLVAL ) + return(clonestr("{\"error\":\"amount not specified\"}")); + sprintf(payload,"method=WithdrawCoin&nonce=%llu&coinName=%s&amount=%.6f&address=%s",(long long)exchange_nonce(exchange),base,amount,destaddr); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized withdraw +} + +struct exchange_funcs btce_funcs = EXCHANGE_FUNCS(btce,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef EXCHANGE_AUTHURL +#undef CHECKBALANCE diff --git a/InstantDEX/exchanges/checkbalance.c b/InstantDEX/exchanges/checkbalance.c new file mode 100755 index 000000000..d57ed1063 --- /dev/null +++ b/InstantDEX/exchanges/checkbalance.c @@ -0,0 +1,53 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +int32_t CHECKBALANCE(char **retstrp,int32_t skipflag,struct exchange_info *exchange,int32_t dir,char *base,char *rel,double price,double volume) +{ + cJSON *json; char *coinstr,*balancestr,*resultstr,*resultval; double balance; int32_t retval = -1; + if ( skipflag == 0 ) + { + coinstr = (dir > 0) ? rel : base; + if ( (balancestr= PARSEBALANCE(exchange,&balance,coinstr)) != 0 ) + { + json = cJSON_Parse(balancestr); + free(balancestr); + printf("%s balance.%s %f vs %f\n",exchange->name,coinstr,balance,dir > 0 ? volume : volume * price); + if ( (dir > 0 && balance < volume) || (dir < 0 && balance < (volume * price)) ) + { + resultstr = "error"; + resultval = "not enough balance"; + } + else + { + resultval = "balance"; + resultstr = "success"; + retval = 0; + } + if ( retstrp != 0 ) + { + if ( json == 0 ) + json = cJSON_CreateObject(); + jaddstr(json,"coin",coinstr); + jaddnum(json,"balance",balance); + jaddnum(json,"required",volume * (dir < 0 ? price : 1.)); + jaddstr(json,resultstr,resultval); + *retstrp = jprint(json,1); + } + else if ( json != 0 ) + free_json(json); + } + } else retval = 0; + return(retval); +} diff --git a/InstantDEX/exchanges/coinbase.c b/InstantDEX/exchanges/coinbase.c new file mode 100755 index 000000000..65d07c382 --- /dev/null +++ b/InstantDEX/exchanges/coinbase.c @@ -0,0 +1,229 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "coinbase" +#define UPDATE prices777_ ## coinbase +#define SUPPORTS coinbase ## _supports +#define SIGNPOST coinbase ## _signpost +#define TRADE coinbase ## _trade +#define ORDERSTATUS coinbase ## _orderstatus +#define CANCELORDER coinbase ## _cancelorder +#define OPENORDERS coinbase ## _openorders +#define TRADEHISTORY coinbase ## _tradehistory +#define BALANCES coinbase ## _balances +#define PARSEBALANCE coinbase ## _parsebalance +#define WITHDRAW coinbase ## _withdraw +#define EXCHANGE_AUTHURL "https://api.exchange.coinbase.com" +#define CHECKBALANCE coinbase ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.exchange.coinbase.com/products/%s-%s/book?level=2",prices->base,prices->rel); + return(prices777_standard("coinbase",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,uint64_t nonce,char *path,char *method) +{ + /*All REST requests must contain the following headers: + + CB-ACCESS-KEY The api key as a string. + CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message). + CB-ACCESS-TIMESTAMP A timestamp for your request. + CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key. + All request bodies should have content type application/json and be valid JSON. + + Signing a Message + The CB-ACCESS-SIGN header is generated by creating a sha256 HMAC using the base64-decoded + secret key on the prehash string timestamp + method + requestPath + body (where + represents string concatenation) + and base64-encode the output. The timestamp value is the same as the CB-ACCESS-TIMESTAMP header. + + The body is the request body string or omitted if there is no request body (typically for GET requests). + + The method should be UPPER CASE + Remember to first base64-decode the alphanumeric secret string (resulting in 64 bytes) before using it as the key for HMAC. Also, base64-encode the digest output before sending in the header. + */ + /* def __call__(self, request): + timestamp = str(time.time()) + message = timestamp + request.method + request.path_url + (request.body or '') + hmac_key = base64.b64decode(self.secret_key) + signature = hmac.new(hmac_key, message, hashlib.sha256) + signature_b64 = signature.digest().encode('base64').rstrip('\n') + + request.headers.update({ + 'CB-ACCESS-SIGN': signature_b64, + 'CB-ACCESS-TIMESTAMP': timestamp, + 'CB-ACCESS-KEY': self.api_key, + 'CB-ACCESS-PASSPHRASE': self.passphrase, + 'Content-Type': 'application/json' + })*/ + char url[1024],hdr1[512],hdr2[512],hdr3[512],hdr4[512],dest[1024]; cJSON *json; int32_t n; + char prehash64[512],prehash[512],decodedsecret[512],sig64[512],*sig,*data = 0; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + n = nn_base64_decode((void *)exchange->apisecret,strlen(exchange->apisecret),(void *)decodedsecret,sizeof(decodedsecret)); + sprintf(prehash,"%llu%s/%s%s",(long long)nonce,method,path,payload); + nn_base64_encode((void *)prehash,strlen(prehash),prehash64,sizeof(prehash64)); + if ( (sig= hmac_sha256_str(dest,decodedsecret,n,prehash64)) != 0 ) + { + nn_base64_encode((void *)sig,strlen(sig),sig64,sizeof(sig64)); + //CB-ACCESS-KEY The api key as a string. + //CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message). + //CB-ACCESS-TIMESTAMP A timestamp for your request. + //CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key. + sprintf(hdr1,"CB-ACCESS-KEY:%s",exchange->apikey); + sprintf(hdr2,"CB-ACCESS-SIGN:%s",sig64); + sprintf(hdr3,"CB-ACCESS-TIMESTAMP:%llu",(long long)nonce); + //sprintf(hdr4,"CB-ACCESS-PASSPHRASE:%s; content-type:application/json; charset=utf-8",exchange->userid); + sprintf(hdr4,"CB-ACCESS-PASSPHRASE:%s",exchange->userid); + sprintf(url,"%s/%s",EXCHANGE_AUTHURL,path); + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,payload,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + return(SIGNPOST(cHandlep,1,0,exchange,"",exchange_nonce(exchange),"accounts","GET")); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],pairstr[512],method[32],*path,*extra; + cJSON *json; uint64_t nonce,txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + path = "trade", strcpy(method,"POST"); + if ( (dir= flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + nonce = exchange_nonce(exchange); + sprintf(payload,"method=Trade&nonce=%llu&pair=%s&type=%s&rate=%.6f&amount=%.6f",(long long)nonce,pairstr,dir>0?"buy":"sell",price,volume); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,payload,nonce,path,method)) != 0 ) + { + // parse json and set txid + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + // generate payload + if ( (json= SIGNPOST(cHandlep,1,0,exchange,payload,exchange_nonce(exchange),"accounts","GET")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized orderstatus +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + // generate payload + if ( (json= SIGNPOST(cHandlep,1,0,exchange,payload,exchange_nonce(exchange),"accounts","GET")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*retstr = 0; cJSON *json; + // generate payload + if ( (json= SIGNPOST(cHandlep,1,0,exchange,payload,exchange_nonce(exchange),"accounts","GET")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*retstr = 0; cJSON *json; + // generate payload + if ( (json= SIGNPOST(cHandlep,1,0,exchange,payload,exchange_nonce(exchange),"accounts","GET")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized tradehistory +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*retstr = 0; cJSON *json; + // generate payload + if ( (json= SIGNPOST(cHandlep,1,0,exchange,payload,exchange_nonce(exchange),"accounts","GET")) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized withdraw +} + +struct exchange_funcs coinbase_funcs = EXCHANGE_FUNCS(coinbase,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef EXCHANGE_AUTHURL +#undef CHECKBALANCE diff --git a/InstantDEX/exchanges/huobi.c b/InstantDEX/exchanges/huobi.c new file mode 100755 index 000000000..5e8b856f2 --- /dev/null +++ b/InstantDEX/exchanges/huobi.c @@ -0,0 +1,183 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "huobi" +#define UPDATE prices777_ ## huobi +#define SUPPORTS huobi ## _supports +#define SIGNPOST huobi ## _signpost +#define TRADE huobi ## _trade +#define ORDERSTATUS huobi ## _orderstatus +#define CANCELORDER huobi ## _cancelorder +#define OPENORDERS huobi ## _openorders +#define TRADEHISTORY huobi ## _tradehistory +#define BALANCES huobi ## _balances +#define PARSEBALANCE huobi ## _parsebalance +#define WITHDRAW huobi ## _withdraw +#define CHECKBALANCE huobi ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"http://api.huobi.com/staticmarket/depth_%s_json.js ",prices->lbase); + return(prices777_standard("huobi",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","cny"}, {"ltc","cny"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload) +{ + char *data; cJSON *json; + json = 0; + //if ( (data= curl_post(&cHandle,"https://api.huobi.com/apiv3",0,payload,"Content-Type:application/x-www-form-urlencoded",0,0,0)) != 0 ) + if ( dotrade == 0 ) + data = exchange_would_submit(payload,"","","",""); + else if ( (data= curl_post(cHandlep,"https://api.huobi.com/apiv3",0,payload,"",0,0,0)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +cJSON *huobi_issue_auth(void **cHandlep,struct exchange_info *exchange,char *method,char *buf) +{ + char payload[1024],digest[33],tmp[1024]; uint64_t nonce; + nonce = exchange_nonce(exchange); + sprintf(tmp,"access_key=%s&created=%llu&method=%s%s",exchange->apikey,(long long)nonce,method,buf); + sprintf(payload,"%s&secret_key=%s",tmp,exchange->apisecret); + //printf("tmp.(%s) payload.(%s)\n",tmp,payload); + calc_md5(digest,payload,(int32_t)strlen(payload)); + sprintf(payload,"%s&sign=%s",tmp,digest); + //printf("-> (%s)\n",payload); + return(SIGNPOST(cHandlep,1,0,exchange,payload)); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char field[128],*itemstr = 0; cJSON *obj,*item; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"return")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + *balancep = jdouble(item,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + return(huobi_issue_auth(cHandlep,exchange,"get_account_info","")); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],pairstr[64],pricestr[64],*extra,*method; cJSON *json; int32_t type; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + if ( extra != 0 && strcmp(extra,"market") == 0 ) + method = (dir > 0) ? "buy_market" : "sell_market"; + else method = (dir > 0) ? "buy" : "sell", sprintf(pricestr,"&price=%.2f",price); + if ( strcmp(pairstr,"btccny") == 0 ) + type = 1; + else if ( strcmp(pairstr,"ltccny") == 0 ) + type = 2; + else + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + sprintf(payload,"&amount=%.4f&coin_type=%d%s",volume,type,pricestr); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= huobi_issue_auth(cHandlep,exchange,method,payload)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024]; + sprintf(payload,"&id=%llu&coin_type=1",(long long)quoteid); + return(jprint(huobi_issue_auth(cHandlep,exchange,"order_info",payload),1)); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024]; + sprintf(payload,"&id=%llu&coin_type=1",(long long)quoteid); + return(jprint(huobi_issue_auth(cHandlep,exchange,"cancel_order",payload),1)); +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(huobi_issue_auth(cHandlep,exchange,"get_orders","&coin_type=1"),1)); +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(clonestr("{\"error\":\"huobi doesnt seem to have trade history api!\"}")); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*base,*destaddr,*method; double amount; + if ( (base= jstr(argjson,"base")) == 0 || strcmp(base,"BTC") != 0 ) + return(clonestr("{\"error\":\"base not specified or base != BTC\"}")); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 ) + return(clonestr("{\"error\":\"destaddr not specified\"}")); + if ( (amount= jdouble(argjson,"amount")) < SMALLVAL ) + return(clonestr("{\"error\":\"amount not specified\"}")); + method = "withdraw_coin"; + sprintf(payload,"&coin_type=1&withdraw_address=%s&withdraw_amount=%.4f",destaddr,amount); + return(jprint(huobi_issue_auth(cHandlep,exchange,method,payload),1)); +} + +struct exchange_funcs huobi_funcs = EXCHANGE_FUNCS(huobi,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef CHECKBALANCE + diff --git a/InstantDEX/exchanges/lakebtc.c b/InstantDEX/exchanges/lakebtc.c new file mode 100755 index 000000000..3c45366e2 --- /dev/null +++ b/InstantDEX/exchanges/lakebtc.c @@ -0,0 +1,285 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "lakebtc" +#define UPDATE prices777_ ## lakebtc +#define SUPPORTS lakebtc ## _supports +#define SIGNPOST lakebtc ## _signpost +#define TRADE lakebtc ## _trade +#define ORDERSTATUS lakebtc ## _orderstatus +#define CANCELORDER lakebtc ## _cancelorder +#define OPENORDERS lakebtc ## _openorders +#define TRADEHISTORY lakebtc ## _tradehistory +#define BALANCES lakebtc ## _balances +#define PARSEBALANCE lakebtc ## _parsebalance +#define WITHDRAW lakebtc ## _withdraw +#define CHECKBALANCE lakebtc ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + { + if ( strcmp(prices->rel,"USD") == 0 ) + sprintf(prices->url,"https://www.LakeBTC.com/api_v1/bcorderbook"); + else if ( strcmp(prices->rel,"CNY") == 0 ) + sprintf(prices->url,"https://www.LakeBTC.com/api_v1/bcorderbook_cny"); + else printf("illegal lakebtc pair.(%s/%s)\n",prices->base,prices->rel); + } + return(prices777_standard("lakebtc",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"btc","cny"} }; + int32_t polarity; + polarity = baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel); + printf("lakebtc.(%s %s) polarity.%d\n",base,rel,polarity); + return(polarity); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *hdr1,uint64_t tonce) +{ + char hdr2[512],cmdbuf[1024],buf64[1024],hdr3[512],dest[1025],hdr4[512],*sig,*data = 0; cJSON *json; + hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + if ( (sig= hmac_sha1_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),hdr1)) != 0 ) + { + sprintf(cmdbuf,"%s:%s",exchange->userid,sig); + nn_base64_encode((void *)cmdbuf,strlen(cmdbuf),buf64,sizeof(buf64)); + sprintf(hdr1,"Authorization:Basic %s",buf64); + sprintf(hdr2,"Json-Rpc-Tonce: %llu",(long long)tonce); + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,"https://www.LakeBTC.com/api_v1",0,payload,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +/* LakeBTC provides trading JSON-RPC API interface. HMAC (Hash-based Message Authentication Code) is employed as our authentication mechanisms. You need at 0.1 BTC in your account to retrieve your private key. + + Besides your private key, the client needs to prepare the following attributes + tonce (timestamp in microseconds, i.e., unixtime × 1000000, make sure your clock is correctly adjusted) + accesskey (your registered email address at LakeBTC) + requestmethod (post) + id (JSON-RPC request id, an integer) + method (JSON-RPC method) + params (JSON-RPC parameters) + Concatenate the above parameters with &, in that order. Parameters can be blank. For example, $signature = + tonce=1389067414466757&accesskey=foo@bar.com&requestmethod=post&id=123&method=ticker¶ms= + Create HMAC signature with your private key by using SHA1. $hash = + hash_hmac('sha1', $signature, $privatetkey) #php + Join your email and the hash signature with colon (:), and sign with Base64. $b64 = + base64_encode("foo@bar.com:") #php YXRjQHF3amlhbi5jb206ZmEzM2UzYzg5MDZjg5MzdiYzFiYw== + Set HTTP Header. Note tonce is the same as that in Step 2. + Json-Rpc-Tonce: 1389067414466757 #HTTP HEADER + Authorization: Basic YXRjQHF3amlhbi5jb206ZmEzM2UzYzg5MDZjg5MzdiYzFiYw== #HTTP HEADER + POST params data in JSON format to this url: + https://www.LakeBTC.com/api_v1 + API Methods + getAccountInfo + method=getAccountInfo + params= (i.e., blank) + +static CURL *cHandle; +char *data,*method,buf64[4096],paramstr[128],jsonbuf[1024],base[64],rel[64],pairstr[64],params[1024],dest[512],url[1024],cmdbuf[8192],*sig,hdr1[4096],hdr2[4096],buf[4096]; cJSON *json; uint64_t tonce,nonce,txid = 0; +*retstrp = 0; +params[0] = 0; +nonce = exchange_nonce(exchange); +tonce = (nonce * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); +if ( dir == 0 ) +{ + method = "getAccountInfo"; + sprintf(buf,"tonce=%llu&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=",(long long)tonce,exchange->userid,method); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,params); +} +else +{ + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + method = (dir > 0) ? "buyOrder" : "sellOrder"; + touppercase(rel); + sprintf(paramstr,"%.2f,%.4f,%s",price,volume,rel); + sprintf(buf,"tonce=%llu&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=%s",(long long)tonce,exchange->userid,method,paramstr); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,paramstr); +} +if ( (sig= hmac_sha1_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),buf)) != 0 ) +{ + sprintf(cmdbuf,"%s:%s",exchange->userid,sig); + nn_base64_encode((void *)cmdbuf,strlen(cmdbuf),buf64,sizeof(buf64)); + sprintf(url,"https://www.lakebtc.com/api_v1"); + sprintf(hdr1,"Authorization:Basic %s",buf64); + sprintf(hdr2,"Json-Rpc-Tonce: %llu",(long long)tonce); + if ( (data= curl_post(&cHandle,url,0,jsonbuf,hdr1,hdr2,0,0)) != 0 ) + { + //printf("submit cmd.(%s) [%s]\n",jsonbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + } else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + } +*/ + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //lakebtc.({"balance":{"BTC":0.1},"locked":{"BTC":0.0},"profile":{"email":"jameslee777@yahoo.com","id":"U137561934","btc_deposit_addres":"1RyKrNJjezeFfvYaicnJEozHfhWfYzbuh"}}) + char field[128],*str,*itemstr = 0; cJSON *obj=0,*item=0,*prof=0; double locked = 0; + *balancep = 0.; + strcpy(field,coinstr); + touppercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"balance")) != 0 && (item= jobj(exchange->balancejson,"locked")) != 0 && (prof= jobj(exchange->balancejson,"profile")) != 0 ) + { + *balancep = jdouble(obj,field); + locked = jdouble(item,field); + obj = cJSON_CreateObject(); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked",locked); + if ( (str= jstr(prof,"btc_deposit_addres")) != 0 ) + jaddstr(obj,"deposit_address",str); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + char payload[1024],jsonbuf[1024],*method; uint64_t tonce; + method = "getAccountInfo"; + tonce = (exchange_nonce(exchange) * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); + sprintf(payload,"tonce=%llu&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=",(long long)tonce,exchange->userid,method); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,""); + return(SIGNPOST(cHandlep,1,0,exchange,jsonbuf,payload,tonce)); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],jsonbuf[1024],pairstr[64],paramstr[512],*extra,*method; + cJSON *json; uint64_t tonce,txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + tonce = (exchange_nonce(exchange) * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + method = (dir > 0) ? "buyOrder" : "sellOrder"; + touppercase(rel); + sprintf(paramstr,"%.2f,%.4f,%s",price,volume,rel); + sprintf(payload,"tonce=%llu&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=%s",(long long)tonce,exchange->userid,method,paramstr); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,paramstr); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,jsonbuf,payload,tonce)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + return(txid); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],jsonbuf[1024],*method,*retstr = 0; cJSON *json; uint64_t tonce; + method = "cancelOrder"; + tonce = (exchange_nonce(exchange) * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%llu\"],\"id\":1}",method,(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,jsonbuf,tonce)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],jsonbuf[1024],*method,*retstr = 0; cJSON *json; uint64_t tonce; + method = "getOrders"; + tonce = (exchange_nonce(exchange) * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[\"%s\"],\"id\":1}",method,""); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,jsonbuf,tonce)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],jsonbuf[1024],timestr[64],*method,*retstr = 0; + cJSON *json; uint64_t tonce; uint32_t starttime; + method = "getTrades"; + if ( (starttime= juint(argjson,"start")) != 0 ) + sprintf(timestr,"%u",starttime); + else timestr[0] = 0; + tonce = (exchange_nonce(exchange) * 1000000 + ((uint64_t)milliseconds() % 1000) * 1000); + sprintf(jsonbuf,"{\"method\":\"%s\",\"params\":[%s],\"id\":1}",method,timestr); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,jsonbuf,tonce)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized tradehistory +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char *status,*retstr; + status = OPENORDERS(cHandlep,exchange,argjson); + if ( (retstr= exchange_extractorderid(0,status,quoteid,"id")) != 0 ) + { + free(status); + return(retstr); + } + free(status); + return(clonestr("{\"error\":\"cant find quoteid\"}")); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(clonestr("{\"error\":\"lakebtc doesnt seem to have withdraw api!\"}")); +} + +struct exchange_funcs lakebtc_funcs = EXCHANGE_FUNCS(lakebtc,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef CHECKBALANCE + diff --git a/InstantDEX/exchanges/okcoin.c b/InstantDEX/exchanges/okcoin.c new file mode 100755 index 000000000..5b3376abc --- /dev/null +++ b/InstantDEX/exchanges/okcoin.c @@ -0,0 +1,239 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "okcoin" +#define UPDATE prices777_ ## okcoin +#define SUPPORTS okcoin ## _supports +#define SIGNPOST okcoin ## _signpost +#define TRADE okcoin ## _trade +#define ORDERSTATUS okcoin ## _orderstatus +#define CANCELORDER okcoin ## _cancelorder +#define OPENORDERS okcoin ## _openorders +#define TRADEHISTORY okcoin ## _tradehistory +#define BALANCES okcoin ## _balances +#define PARSEBALANCE okcoin ## _parsebalance +#define WITHDRAW okcoin ## _withdraw +#define EXCHANGE_AUTHURL "https://www.okcoin.com/api/v1" +#define CHECKBALANCE okcoin ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://www.okcoin.com/api/v1/depth.do?symbol=%s_%s",prices->lbase,prices->lrel); + if ( strcmp(prices->rel,"USD") != 0 && strcmp(prices->rel,"BTC") != 0 ) + { + fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FATAL ERROR OKCOIN.(%s) only supports USD\n",prices->url); + printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FATAL ERROR OKCOIN.(%s) only supports USD\n",prices->url); + exit(-1); + return(0); + } + return(prices777_standard("okcoin",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"ltc","usd"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) +{ + char hdr1[512],hdr2[512],hdr3[512],hdr4[512],*data; cJSON *json; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,payload,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} +/* +static CURL *cHandle; +char *data,*path,*typestr,*extra,pricestr[64],base[64],rel[64],pairstr[64],url[1024],cmdbuf[8192],buf[512],digest[33]; cJSON *json; uint64_t nonce,txid = 0; +nonce = exchange_nonce(exchange); +if ( (extra= *retstrp) != 0 ) +*retstrp = 0; +if ( dir == 0 ) +{ + path = "userinfo.do"; + sprintf(buf,"api_key=%s&secret_key=%s",exchange->apikey,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + touppercase(digest); + sprintf(cmdbuf,"api_key=%s&sign=%s",exchange->apikey,digest); +} +else +{ + path = "trade.do"; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + if ( extra != 0 && strcmp(extra,"market") == 0 ) + typestr = (dir > 0) ? "buy_market" : "sell_market", sprintf(pricestr,"&price=%.2f",price); // docs say market orders put volume in price + else typestr = (dir > 0) ? "buy" : "sell", sprintf(pricestr,"&price=%.2f",price); + sprintf(buf,"amount=%.4f&api_key=%s%ssymbol=%s&type=%s&secret_key=%s",volume,exchange->apikey,pricestr,pairstr,typestr,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + touppercase(digest); + sprintf(cmdbuf,"amount=%.4f&api_key=%s%s&symbol=%s&type=%s&sign=%s",volume,exchange->apikey,pricestr,pairstr,typestr,digest); + } +//printf("MD5.(%s)\n",buf); +sprintf(url,"https://www.okcoin.com/api/v1/%s",path); +if ( (data= curl_post(&cHandle,url,0,cmdbuf,0,0,0,0)) != 0 ) // "{\"Content-type\":\"application/x-www-form-urlencoded\"}","{\"User-Agent\":\"OKCoin Javascript API Client\"}" +{ + //printf("submit cmd.(%s) [%s]\n",cmdbuf,data); + if ( (json= cJSON_Parse(data)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } +} else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf); +*/ + +cJSON *okcoin_issue_auth(void **cHandlep,struct exchange_info *exchange,char *method,char *buf) +{ + char payload[1024],tmp[1024],digest[512],url[512]; + sprintf(tmp,"api_key=%s%s",exchange->apikey,buf); + + sprintf(payload,"%s&secret_key=%s",tmp,exchange->apisecret); + //printf("tmp.(%s) payload.(%s)\n",tmp,payload); + calc_md5(digest,payload,(int32_t)strlen(payload)); + touppercase(digest); + sprintf(payload,"%s&sign=%s",tmp,digest); + sprintf(url,"%s/%s",EXCHANGE_AUTHURL,method); + return(SIGNPOST(cHandlep,1,0,exchange,url,payload)); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //okcoin.({"info":{"funds":{"asset":{"net":"0","total":"0"},"free":{"btc":"0","ltc":"0","usd":"0"},"freezed":{"btc":"0","ltc":"0","usd":"0"}}},"result":true}) + char field[128],*itemstr = 0; cJSON *obj,*item,*avail,*locked; double lockval = 0; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + if ( exchange->balancejson != 0 && (obj= jobj(exchange->balancejson,"info")) != 0 && (item= jobj(obj,"funds")) != 0 ) + { + if ( (avail= jobj(item,"free")) != 0 ) + *balancep = jdouble(avail,field); + if ( (locked= jobj(item,"freezed")) != 0 ) + lockval = jdouble(locked,field); + obj = cJSON_CreateObject(); + touppercase(field); + jaddstr(obj,"base",field); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked",lockval); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + return(okcoin_issue_auth(cHandlep,exchange,"userinfo.do","")); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],buf[1024],url[1024],digest[512],pairstr[512],pricestr[64],*extra,*typestr; + cJSON *json; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + if ( extra != 0 && strcmp(extra,"market") == 0 ) + typestr = (dir > 0) ? "buy_market" : "sell_market", sprintf(pricestr,"&price=%.2f",price); // docs say market orders put volume in price + else typestr = (dir > 0) ? "buy" : "sell"; + sprintf(pricestr,"&price=%.2f",price); + sprintf(buf,"amount=%.4f&api_key=%s%ssymbol=%s&type=%s&secret_key=%s",volume,exchange->apikey,pricestr,pairstr,typestr,exchange->apisecret); + calc_md5(digest,buf,(int32_t)strlen(buf)); + touppercase(digest); + sprintf(payload,"amount=%.4f&api_key=%s%s&symbol=%s&type=%s&sign=%s",volume,exchange->apikey,pricestr,pairstr,typestr,digest); + sprintf(url,"%s/%s",EXCHANGE_AUTHURL,"trade.do"); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,url,payload)) != 0 ) + { + txid = j64bits(json,"order_id"); + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char buf[64]; + sprintf(buf,"&symbol=btc_usd&order_id=%llu",(long long)quoteid); + return(jprint(okcoin_issue_auth(cHandlep,exchange,"order_info.do",buf),1)); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char buf[64]; + sprintf(buf,"&symbol=btc_usd&order_id=%llu",(long long)quoteid); + return(jprint(okcoin_issue_auth(cHandlep,exchange,"cancel_order.do",buf),1)); +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(okcoin_issue_auth(cHandlep,exchange,"orders_info.do",""),1)); +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(okcoin_issue_auth(cHandlep,exchange,"orders_history.do","&status=1&symbol=btc_usd¤t_page=0&page_length=200"),1)); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*base,*destaddr,*method,*tradepassword; double amount; + if ( (base= jstr(argjson,"base")) == 0 || strcmp(base,"BTC") != 0 ) + return(clonestr("{\"error\":\"base not specified or base != BTC\"}")); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 ) + return(clonestr("{\"error\":\"destaddr not specified\"}")); + if ( (amount= jdouble(argjson,"amount")) < SMALLVAL ) + return(clonestr("{\"error\":\"amount not specified\"}")); + if ( (tradepassword= jstr(argjson,"tradepassword")) == 0 ) + return(clonestr("{\"error\":\"tradepassword not specified\"}")); + method = "withdraw_coin"; + sprintf(payload,"&symbol=btc_usd&chargefee=0.0001&withdraw_address=%s&withdraw_amount=%.4f&trade_pwd=%s",destaddr,amount,tradepassword); + return(jprint(okcoin_issue_auth(cHandlep,exchange,method,payload),1)); +} + +struct exchange_funcs okcoin_funcs = EXCHANGE_FUNCS(okcoin,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef EXCHANGE_AUTHURL +#undef CHECKBALANCE + diff --git a/InstantDEX/exchanges/poloniex.c b/InstantDEX/exchanges/poloniex.c new file mode 100755 index 000000000..da01fc290 --- /dev/null +++ b/InstantDEX/exchanges/poloniex.c @@ -0,0 +1,233 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "poloniex" +#define UPDATE prices777_ ## poloniex +#define SUPPORTS poloniex ## _supports +#define SIGNPOST poloniex ## _signpost +#define TRADE poloniex ## _trade +#define ORDERSTATUS poloniex ## _orderstatus +#define CANCELORDER poloniex ## _cancelorder +#define OPENORDERS poloniex ## _openorders +#define TRADEHISTORY poloniex ## _tradehistory +#define BALANCES poloniex ## _balances +#define PARSEBALANCE poloniex ## _parsebalance +#define WITHDRAW poloniex ## _withdraw +#define EXCHANGE_AUTHURL "https://poloniex.com/tradingApi" +#define CHECKBALANCE poloniex ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + char market[128]; + if ( prices->url[0] == 0 ) + { + sprintf(market,"%s_%s",prices->rel,prices->base); + sprintf(prices->url,"https://poloniex.com/public?command=returnOrderBook¤cyPair=%s&depth=%d",market,maxdepth); + } + return(prices777_standard(EXCHANGE_NAME,prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + //char *baserels[][2] = { {"btc","usd"} }; + //return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); + if ( strlen(base) > 5 || strlen(rel) > 5 || strcmp(rel,"CNY") == 0 || strcmp(base,"CNY") == 0 || strcmp(rel,"USD") == 0 || strcmp(base,"USD") == 0 ) + return(0); + if ( strcmp(rel,"BTC") == 0 ) + return(1); + else if ( strcmp(base,"BTC") == 0 ) + return(-1); + else return(0); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) +{ + char dest[SHA512_DIGEST_SIZE*2+1],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*data,*sig; cJSON *json; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + if ( (sig= hmac_sha512_str(dest,exchange->apisecret,(int32_t)strlen(exchange->apisecret),payload)) != 0 ) + sprintf(hdr1,"Sign:%s",sig); + else hdr1[0] = 0; + sprintf(hdr2,"Key:%s",exchange->apikey); + if ( dotrade == 0 ) + data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,payload,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + char payload[1024]; + sprintf(payload,"command=returnCompleteBalances&nonce=%llu",(long long)exchange_nonce(exchange)); + return(SIGNPOST(cHandlep,1,0,exchange,EXCHANGE_AUTHURL,payload)); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + char *itemstr = 0; cJSON *item,*obj; double onorders,btcvalue; + *balancep = 0.; + if ( exchange->balancejson != 0 && (item= jobj(exchange->balancejson,coinstr)) != 0 ) + { + itemstr = jprint(item,0); + *balancep = jdouble(item,"available"); + onorders = jdouble(item,"onOrders"); + btcvalue = jdouble(item,"btcValue"); + if ( (obj= cJSON_Parse(itemstr)) != 0 ) + { + free(itemstr); + jaddstr(obj,"base",coinstr); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"onOrders",onorders); + jaddnum(obj,"btcvalue",btcvalue); + itemstr = jprint(obj,1); + } + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],pairstr[64],*extra,*typestr; cJSON *json; uint64_t nonce,txid = 0; + nonce = exchange_nonce(exchange); + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel); + if ( extra != 0 && strcmp(extra,"margin") == 0 ) + typestr = (dir > 0) ? "marginBuy":"marginSell"; + else typestr = (dir > 0) ? "buy":"sell"; + sprintf(payload,"command=%s&nonce=%lld¤cyPair=%s&rate=%.8f&amount=%.8f",typestr,(long long)nonce,pairstr,price,volume); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + txid = (get_API_nxt64bits(cJSON_GetObjectItem(json,"orderNumber")) << 32) | get_API_nxt64bits(cJSON_GetObjectItem(json,"tradeID")); + free_json(json); + } + return(txid); +} + +void poloniex_setpair(char *pair,cJSON *argjson) +{ + char *base,*rel; + base = jstr(argjson,"base"); + rel = jstr(argjson,"rel"); + if ( base == 0 || rel == 0 ) + strcpy(pair,"all"); + else sprintf(pair,"%s_%s",rel,base); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char payload[1024],*retstr = 0; cJSON *json; + sprintf(payload,"command=cancelOrder&nonce=%llu&orderNumber=%llu",(long long)exchange_nonce(exchange),(long long)quoteid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized cancelorder +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],pair[64],*retstr = 0; cJSON *json; + poloniex_setpair(pair,argjson); + sprintf(payload,"command=returnOpenOrders&nonce=%llu¤cyPair=%s",(long long)exchange_nonce(exchange),pair); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized open orders +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],pair[64],*retstr = 0; cJSON *json; uint32_t timestamp,endstamp; + poloniex_setpair(pair,argjson); + timestamp = juint(argjson,"start"); + endstamp = juint(argjson,"end"); + sprintf(payload,"command=returnTradeHistory&nonce=%llu¤cyPair=%s",(long long)exchange_nonce(exchange),pair); + if ( timestamp != 0 ) + sprintf(payload + strlen(payload),"&start=%u",timestamp); + if ( endstamp != 0 ) + sprintf(payload + strlen(payload),"&end=%u",endstamp); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized tradehistory +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char *status,*retstr; uint32_t iter; + for (iter=0; iter<2; iter++) + { + if ( iter == 0 ) + status = OPENORDERS(cHandlep,exchange,argjson); + else status = TRADEHISTORY(cHandlep,exchange,argjson); + if ( (retstr= exchange_extractorderid(iter,status,quoteid,"orderNumber")) != 0 ) + { + free(status); + return(retstr); + } + free(status); + } + return(clonestr("{\"error\":\"cant find quoteid\"}")); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char payload[1024],*destaddr,*paymentid,*base,*retstr = 0; cJSON *json; double amount; + if ( (base= jstr(argjson,"base")) == 0 ) + return(clonestr("{\"error\":\"base not specified\"}")); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 ) + return(clonestr("{\"error\":\"destaddr not specified\"}")); + if ( (amount= jdouble(argjson,"amount")) < SMALLVAL ) + return(clonestr("{\"error\":\"amount not specified\"}")); + paymentid = jstr(argjson,"paymentid"); + sprintf(payload,"command=withdraw&nonce=%llu¤cy=%s&amount=%.6f&address=%s",(long long)exchange_nonce(exchange),base,amount,destaddr); + if ( paymentid != 0 ) + sprintf(payload + strlen(payload),"&paymentId=%s",paymentid); + if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 ) + { + free_json(json); + } + return(retstr); // return standardized withdraw +} + +struct exchange_funcs poloniex_funcs = EXCHANGE_FUNCS(poloniex,EXCHANGE_NAME); + + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef EXCHANGE_AUTHURL +#undef CHECKBALANCE diff --git a/InstantDEX/exchanges/quadriga.c b/InstantDEX/exchanges/quadriga.c new file mode 100755 index 000000000..8d5716e3d --- /dev/null +++ b/InstantDEX/exchanges/quadriga.c @@ -0,0 +1,183 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "quadriga" +#define UPDATE prices777_ ## quadriga +#define SUPPORTS quadriga ## _supports +#define SIGNPOST quadriga ## _signpost +#define TRADE quadriga ## _trade +#define ORDERSTATUS quadriga ## _orderstatus +#define CANCELORDER quadriga ## _cancelorder +#define OPENORDERS quadriga ## _openorders +#define TRADEHISTORY quadriga ## _tradehistory +#define BALANCES quadriga ## _balances +#define PARSEBALANCE quadriga ## _parsebalance +#define WITHDRAW quadriga ## _withdraw +#define CHECKBALANCE quadriga ## _checkbalance + +double UPDATE(struct prices777 *prices,int32_t maxdepth) +{ + if ( prices->url[0] == 0 ) + sprintf(prices->url,"https://api.quadrigacx.com/v2/order_book?book=%s_%s",prices->lbase,prices->lrel); + return(prices777_standard("quadriga",prices->url,prices,0,0,maxdepth,0)); +} + +int32_t SUPPORTS(char *base,char *rel) +{ + char *baserels[][2] = { {"btc","usd"}, {"btc","cad"} }; + return(baserel_polarity(baserels,(int32_t)(sizeof(baserels)/sizeof(*baserels)),base,rel)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *path) +{ + char url[1024],req[1024],md5secret[128],tmp[1024],dest[1025],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*sig,*data = 0; + cJSON *json; uint64_t nonce; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + nonce = exchange_nonce(exchange) * 1000 + ((uint64_t)milliseconds() % 1000); + sprintf(tmp,"%llu%s%s",(long long)nonce,exchange->userid,exchange->apikey); + calc_md5(md5secret,exchange->apisecret,(int32_t)strlen(exchange->apisecret)); + if ( (sig= hmac_sha256_str(dest,md5secret,(int32_t)strlen(md5secret),tmp)) != 0 ) + { + sprintf(req,"{\"key\":\"%s\",%s\"nonce\":%llu,\"signature\":\"%s\"}",exchange->apikey,payload,(long long)nonce,sig); + sprintf(hdr1,"Content-Type:application/json"); + sprintf(hdr2,"charset=utf-8"); + sprintf(hdr3,"Content-Length:%ld",(long)strlen(req)); + sprintf(url,"https://api.quadrigacx.com/v2/%s",path); + if ( dotrade == 0 ) + data = exchange_would_submit(req,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(cHandlep,url,0,req,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr) +{ + //[{"btc_available":"0.00000000","btc_reserved":"0.00000000","btc_balance":"0.00000000","cad_available":"0.00","cad_reserved":"0.00","cad_balance":"0.00","usd_available":"0.00","usd_reserved":"0.00","usd_balance":"0.00","xau_available":"0.000000","xau_reserved":"0.000000","xau_balance":"0.000000","fee":"0.5000"}] + char field[128],*str,*itemstr = 0; cJSON *obj; double reserv,total; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + strcat(field,"_available"); + if ( exchange->balancejson != 0 && (str= jstr(exchange->balancejson,field)) != 0 ) + { + *balancep = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_reserved"); + reserv = jdouble(exchange->balancejson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance"); + total = jdouble(exchange->balancejson,field); + obj = cJSON_CreateObject(); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked_balance",reserv); + jaddnum(obj,"total",total); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange) +{ + return(SIGNPOST(cHandlep,1,0,exchange,"","balance")); +} + +#include "checkbalance.c" + +uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume) +{ + char payload[1024],pairstr[64],*extra,*path; cJSON *json; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + path = (dir > 0) ? "buy" : "sell"; + //key - API key + //signature - signature + //nonce - nonce + //amount - amount of major currency + //price - price to buy at + //book - optional, if not specified, will default to btc_cad + sprintf(payload,"\"amount\":%.6f,\"price\":%.3f,\"book\":\"%s_%s\",",volume,price,base,rel); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,payload,path)) != 0 ) + { + // parse json and set txid + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char buf[64]; + sprintf(buf,"\"id\":%llu,",(long long)quoteid); + return(jprint(SIGNPOST(cHandlep,1,0,exchange,buf,"lookup_order"),1)); +} + +char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid) +{ + char buf[64]; + sprintf(buf,"\"id\":%llu,",(long long)quoteid); + return(jprint(SIGNPOST(cHandlep,1,0,exchange,buf,"cancel_order"),1)); +} + +char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"","open_orders"),1)); +} + +char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"","user_transactions"),1)); +} + +char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson) +{ + char buf[1024],*base,*destaddr; double amount; + if ( (base= jstr(argjson,"base")) == 0 || strcmp(base,"BTC") != 0 ) + return(clonestr("{\"error\":\"base not specified or base != BTC\"}")); + if ( (destaddr= jstr(argjson,"destaddr")) == 0 ) + return(clonestr("{\"error\":\"destaddr not specified\"}")); + if ( (amount= jdouble(argjson,"amount")) < SMALLVAL ) + return(clonestr("{\"error\":\"amount not specified\"}")); + sprintf(buf,"\"amount\":%.4f,\"address\":\"%s\",",amount,destaddr); + printf("submit.(%s)\n",buf); + return(jprint(SIGNPOST(cHandlep,1,0,exchange,"","bitcoin_withdrawal"),1)); +} + +struct exchange_funcs quadriga_funcs = EXCHANGE_FUNCS(quadriga,EXCHANGE_NAME); + +#undef UPDATE +#undef SUPPORTS +#undef SIGNPOST +#undef TRADE +#undef ORDERSTATUS +#undef CANCELORDER +#undef OPENORDERS +#undef TRADEHISTORY +#undef BALANCES +#undef PARSEBALANCE +#undef WITHDRAW +#undef EXCHANGE_NAME +#undef CHECKBALANCE + diff --git a/InstantDEX/main.c b/InstantDEX/main.c new file mode 100644 index 000000000..f6a4c58bd --- /dev/null +++ b/InstantDEX/main.c @@ -0,0 +1,36 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define CHROMEAPP_NAME InstantDEX +#define CHROMEAPP_STR "InstantDEX" +#define CHROMEAPP_CONF "InstantDEX.conf" +#define CHROMEAPP_MAIN InstantDEX_main +#define CHROMEAPP_JSON InstantDEX_JSON +#define CHROMEAPP_HANDLER Handler_InstantDEX + +#include "../pnacl_main.h" + +// ALL globals must be here! + +void InstantDEX_main(void *arg) +{ + while ( 1 ) + sleep(777); +} + +char *InstantDEX_JSON(char *jsonstr) +{ + return(clonestr("{\"error\":\"InstantDEX is just a stub for now\"}")); +} \ No newline at end of file diff --git a/InstantDEX/manifest.json b/InstantDEX/manifest.json new file mode 100755 index 000000000..6346528e8 --- /dev/null +++ b/InstantDEX/manifest.json @@ -0,0 +1,17 @@ +{ + "name": "InstantDEX", + "uri": "InstantDEX.pexe", + "short_name": "InstantDEX", + "version": "44.1.2", + "manifest_version": 2, + "description": "InstantDEX", + "offline_enabled": true, + "icons": { "128": "icon128.png" }, + "app": + { + "name": "InstantDEX", + "background": { "scripts": ["background.js"] }, + "sockets": { "tcp": { "connect": "" }, "tcpServer": { "listen": "127.0.0.1:*" } }, + "permissions": [ "unlimitedStorage", "filesystem", "storage", "system.storage", "system.display", "system.network","system.cpu" ] + } +} diff --git a/InstantDEX/orderbooks.h b/InstantDEX/orderbooks.h new file mode 100755 index 000000000..cfd2c51b4 --- /dev/null +++ b/InstantDEX/orderbooks.h @@ -0,0 +1,3456 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#ifndef xcode_orderbooks_h +#define xcode_orderbooks_h + +char *peggy_contracts[64] = +{ + "BTCD", "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", // major currencies + "CNY", "RUB", "MXN", "BRL", "INR", "HKD", "TRY", "ZAR", "PLN", "NOK", "SEK", "DKK", "CZK", "HUF", "ILS", "KRW", "MYR", "PHP", "RON", "SGD", "THB", "BGN", "IDR", "HRK", + "BTCUSD", "NXTBTC", "SuperNET", "ETHBTC", "LTCBTC", "XMRBTC", "BTSBTC", "XCPBTC", // BTC priced + "XAUUSD", "XAGUSD", "XPTUSD", "XPDUSD", "Copper", "NGAS", "UKOil", "USOil", // USD priced + "Bund", "NAS100", "SPX500", "US30", "EUSTX50", "UK100", "JPN225", "GER30", "SUI30", "AUS200", "HKG33", "XAUUSD", "BTCRUB", "BTCCNY", "BTCUSD" // abstract +}; + +char *MGWassets[][3] = +{ + { "12659653638116877017", "BTC", "8" }, + { "17554243582654188572", "BTC", "8" }, // assetid, name, decimals + { "4551058913252105307", "BTC", "8" }, + { "6918149200730574743", "BTCD", "4" }, + { "11060861818140490423", "BTCD", "4" }, + { "13120372057981370228", "BITS", "6" }, + { "16344939950195952527", "DOGE", "4" }, + { "2303962892272487643", "DOGE", "4" }, + { "6775076774325697454", "OPAL", "8" }, + { "7734432159113182240", "VPN", "4" }, + { "9037144112883608562", "VRC", "8" }, + { "1369181773544917037", "BBR", "8" }, + { "17353118525598940144", "DRK", "8" }, + { "2881764795164526882", "LTC", "4" }, + { "7117580438310874759", "BC", "4" }, + { "275548135983837356", "VIA", "4" }, + { "6220108297598959542", "CNMT", "0" }, + { "7474435909229872610", "CNMT", "0" }, +}; + +char *Tradedassets[][4] = +{ + { "6220108297598959542", "CNMT", "0", "poloniex" }, + { "7474435909229872610", "CNMT", "0", "poloniex" }, + { "979292558519844732", "MMNXT", "0", "poloniex" }, + { "12982485703607823902", "XUSD", "0", "poloniex" }, + { "13634675574519917918", "INDEX", "0", "poloniex" }, + { "6932037131189568014", "JLH", "0", "poloniex" }, + { "14273984620270850703", "NXTI", "0", "poloniex" }, + { "12071612744977229797", "UNITY", "4", "poloniex" }, +}; + +char *is_tradedasset(char *exchange,char *assetidstr) +{ + int32_t i; + for (i=0; i<(int32_t)(sizeof(Tradedassets)/sizeof(*Tradedassets)); i++) + if ( strcmp(Tradedassets[i][0],assetidstr) == 0 ) + { + strcpy(exchange,Tradedassets[i][3]); + return(Tradedassets[i][1]); + } + return(0); +} + +uint64_t is_MGWcoin(char *name) +{ + int32_t i; + for (i=0; i<(int32_t)(sizeof(MGWassets)/sizeof(*MGWassets)); i++) + if ( strcmp(MGWassets[i][1],name) == 0 ) + return(calc_nxt64bits(MGWassets[i][0])); + return(0); +} + +char *is_MGWasset(uint64_t assetid) +{ + int32_t i; char assetidstr[64]; + expand_nxt64bits(assetidstr,assetid); + for (i=0; i<(int32_t)(sizeof(MGWassets)/sizeof(*MGWassets)); i++) + if ( strcmp(MGWassets[i][0],assetidstr) == 0 ) + return(MGWassets[i][1]); + return(0); +} + +uint64_t prices777_equiv(uint64_t assetid) +{ + char *str; + if ( (str= is_MGWasset(assetid)) != 0 ) + return(stringbits(str)); + return(assetid); +} + +struct prices777 *prices777_find(int32_t *invertedp,uint64_t baseid,uint64_t relid,char *exchange) +{ + int32_t i; struct prices777 *prices; + *invertedp = 0; + for (i=0; iexchange,exchange) == 0 ) + { + //printf("FOUND.(%s)\n",exchange); + if ( prices777_equiv(prices->baseid) == prices777_equiv(baseid) && prices777_equiv(prices->relid) == prices777_equiv(relid) ) + return(prices); + else if ( prices777_equiv(prices->relid) == prices777_equiv(baseid) && prices777_equiv(prices->baseid) == prices777_equiv(relid) ) + { + *invertedp = 1; + return(prices); + } + //else printf("(%llu/%llu) != (%llu/%llu)\n",(long long)baseid,(long long)relid,(long long)prices->baseid,(long long)prices->relid); + } //else fprintf(stderr,"(%s).%d ",prices->exchange,i); + } + //printf("CANTFIND.(%s) %llu/%llu\n",exchange,(long long)baseid,(long long)relid); + return(0); +} + +struct prices777 *prices777_createbasket(int32_t addbasket,char *name,char *base,char *rel,uint64_t baseid,uint64_t relid,struct prices777_basket *basket,int32_t n,char *typestr) +{ + int32_t i,j,m,iter,max = 0; double firstwt,wtsum; struct prices777 *prices,*feature; + printf("createbasket.%s n.%d (%s/%s)\n",typestr,n,base,rel); + prices = prices777_initpair(1,typestr,base,rel,0.,name,baseid,relid,n); + for (iter=0; iter<2; iter++) + { + for (i=0; idependents = realloc(feature->dependents,sizeof(*feature->dependents) * (feature->numdependents + 1)); + feature->dependents[feature->numdependents++] = &prices->changed; + printf("%p i.%d/%d addlink.%s groupid.%d wt.%f (%s.%llu/%llu -> %s.%llu/%llu).%s\n",feature,i,n,feature->exchange,basket[i].groupid,basket[i].wt,feature->contract,(long long)feature->baseid,(long long)feature->relid,prices->contract,(long long)prices->baseid,(long long)prices->relid,prices->exchange); + } + if ( basket[i].groupid > max ) + max = basket[i].groupid; + if ( fabs(basket[i].wt) < SMALLVAL ) + { + printf("all basket features.%s i.%d must have nonzero wt\n",feature->contract,i); + free(prices); + return(0); + } + if ( strcmp(feature->base,feature->rel) == 0 || feature->baseid == feature->relid ) + { + printf("base rel cant be the same (%s %s) %llu %llu\n",feature->base,feature->rel,(long long)feature->baseid,(long long)feature->relid); + free(prices); + return(0); + } + } + if ( (max+1) > MAX_GROUPS ) + { + printf("baskets limited to %d, %d is too many for %s.(%s/%s)\n",MAX_GROUPS,n,name,base,rel); + return(0); + } + } + prices->numgroups = (max + 1); + for (j=0; jnumgroups; j++) + { + for (firstwt=i=m=0; igroupwts[j] = firstwt = basket[i].wt; + else if ( basket[i].wt != firstwt ) + { + printf("warning features of same group.%d different wt: %d %f != %f\n",j,i,firstwt,basket[i].wt); + // free(prices); + // return(0); + } + m++; + } + } + //printf("m.%d\n",m); + for (i=0; ibasketsize,n,j,i,m); + } + for (j=0; jnumgroups; j++) + for (i=0; ibasket[prices->basketsize++] = basket[i]; + for (i=-1; i<=1; i+=2) + { + for (wtsum=j=m=0; jnumgroups; j++) + { + if ( prices->groupwts[j]*i > 0 ) + wtsum += prices->groupwts[j], m++; + } + if ( 0 && wtsum != 0. ) + { + if ( wtsum < 0. ) + wtsum = -wtsum; + for (j=0; jnumgroups; j++) + prices->groupwts[j] /= wtsum; + } + } + if ( prices->numgroups == 1 ) + prices->groupwts[0] = 1.; + for (j=0; jnumgroups; j++) + printf("%9.6f ",prices->groupwts[j]); + printf("groupwts %s\n",typestr); + return(prices); +} + +double prices777_price_volume(double *volumep,uint64_t baseamount,uint64_t relamount) +{ + *volumep = (((double)baseamount + 0.000000009999999) / SATOSHIDEN); + if ( baseamount > 0. ) + return((double)relamount / (double)baseamount); + else return(0.); +} + +void prices777_best_amounts(uint64_t *baseamountp,uint64_t *relamountp,double price,double volume) +{ + double checkprice,checkvol,distA,distB,metric,bestmetric = (1. / SMALLVAL); + uint64_t baseamount,relamount,bestbaseamount = 0,bestrelamount = 0; + int32_t i,j; + baseamount = volume * SATOSHIDEN; + relamount = ((price * volume) * SATOSHIDEN); + //*baseamountp = baseamount, *relamountp = relamount; + //return; + for (i=-1; i<=1; i++) + for (j=-1; j<=1; j++) + { + checkprice = prices777_price_volume(&checkvol,baseamount+i,relamount+j); + distA = (checkprice - price); + distA *= distA; + distB = (checkvol - volume); + distB *= distB; + metric = sqrt(distA + distB); + if ( metric < bestmetric ) + { + bestmetric = metric; + bestbaseamount = baseamount + i; + bestrelamount = relamount + j; + //printf("i.%d j.%d metric. %f\n",i,j,metric); + } + } + *baseamountp = bestbaseamount; + *relamountp = bestrelamount; +} + +void prices777_additem(cJSON **highbidp,cJSON **lowaskp,cJSON *bids,cJSON *asks,int32_t ind,cJSON *item,int32_t bidask) +{ + if ( bidask == 0 ) + { + cJSON_AddItemToArray(bids,item); + if ( ind == 0 ) + *highbidp = item; + } + else + { + cJSON_AddItemToArray(asks,item); + if ( ind == 0 ) + *lowaskp = item; + } +} + +uint64_t calc_qty(uint64_t mult,uint64_t assetid,uint64_t amount) +{ + if ( assetid != NXT_ASSETID ) + return(amount / mult); + else return(amount); +} + +int32_t verify_NXTtx(cJSON *json,uint64_t refasset,uint64_t qty,uint64_t destNXTbits) +{ + int32_t typeval,subtypeval,n = 0; + uint64_t quantity,price,assetidbits; + cJSON *attachmentobj; + char sender[MAX_JSON_FIELD],recipient[MAX_JSON_FIELD],deadline[MAX_JSON_FIELD],feeNQT[MAX_JSON_FIELD],amountNQT[MAX_JSON_FIELD],type[MAX_JSON_FIELD],subtype[MAX_JSON_FIELD],verify[MAX_JSON_FIELD],referencedTransaction[MAX_JSON_FIELD],quantityQNT[MAX_JSON_FIELD],priceNQT[MAX_JSON_FIELD],assetidstr[MAX_JSON_FIELD],sighash[MAX_JSON_FIELD],fullhash[MAX_JSON_FIELD],timestamp[MAX_JSON_FIELD],transaction[MAX_JSON_FIELD]; + if ( json == 0 ) + { + printf("verify_NXTtx cant parse json\n"); + return(-1); + } + if ( extract_cJSON_str(sender,sizeof(sender),json,"sender") > 0 ) n++; + if ( extract_cJSON_str(recipient,sizeof(recipient),json,"recipient") > 0 ) n++; + if ( extract_cJSON_str(referencedTransaction,sizeof(referencedTransaction),json,"referencedTransactionFullHash") > 0 ) n++; + if ( extract_cJSON_str(amountNQT,sizeof(amountNQT),json,"amountNQT") > 0 ) n++; + if ( extract_cJSON_str(feeNQT,sizeof(feeNQT),json,"feeNQT") > 0 ) n++; + if ( extract_cJSON_str(deadline,sizeof(deadline),json,"deadline") > 0 ) n++; + if ( extract_cJSON_str(type,sizeof(type),json,"type") > 0 ) n++; + if ( extract_cJSON_str(subtype,sizeof(subtype),json,"subtype") > 0 ) n++; + if ( extract_cJSON_str(verify,sizeof(verify),json,"verify") > 0 ) n++; + if ( extract_cJSON_str(sighash,sizeof(sighash),json,"signatureHash") > 0 ) n++; + if ( extract_cJSON_str(fullhash,sizeof(fullhash),json,"fullHash") > 0 ) n++; + if ( extract_cJSON_str(timestamp,sizeof(timestamp),json,"timestamp") > 0 ) n++; + if ( extract_cJSON_str(transaction,sizeof(transaction),json,"transaction") > 0 ) n++; + if ( calc_nxt64bits(recipient) != destNXTbits ) + { + if ( Debuglevel > 2 ) + fprintf(stderr,"recipient.%s != %llu\n",recipient,(long long)destNXTbits); + return(-2); + } + typeval = myatoi(type,256), subtypeval = myatoi(subtype,256); + if ( refasset == NXT_ASSETID ) + { + if ( typeval != 0 || subtypeval != 0 ) + { + fprintf(stderr,"unexpected typeval.%d subtypeval.%d\n",typeval,subtypeval); + return(-3); + } + if ( qty != calc_nxt64bits(amountNQT) ) + { + fprintf(stderr,"unexpected qty.%llu vs.%s\n",(long long)qty,amountNQT); + return(-4); + } + return(0); + } + else + { + if ( typeval != 2 || subtypeval != 1 ) + { + if ( Debuglevel > 2 ) + fprintf(stderr,"refasset.%llu qty %lld\n",(long long)refasset,(long long)qty); + return(-11); + } + price = quantity = assetidbits = 0; + attachmentobj = cJSON_GetObjectItem(json,"attachment"); + if ( attachmentobj != 0 ) + { + if ( extract_cJSON_str(assetidstr,sizeof(assetidstr),attachmentobj,"asset") > 0 ) + assetidbits = calc_nxt64bits(assetidstr); + //else if ( extract_cJSON_str(assetidstr,sizeof(assetidstr),attachmentobj,"currency") > 0 ) + // assetidbits = calc_nxt64bits(assetidstr); + if ( extract_cJSON_str(quantityQNT,sizeof(quantityQNT),attachmentobj,"quantityQNT") > 0 ) + quantity = calc_nxt64bits(quantityQNT); + //else if ( extract_cJSON_str(quantityQNT,sizeof(quantityQNT),attachmentobj,"units") > 0 ) + // quantity = calc_nxt64bits(quantityQNT); + if ( extract_cJSON_str(priceNQT,sizeof(priceNQT),attachmentobj,"priceNQT") > 0 ) + price = calc_nxt64bits(priceNQT); + } + if ( assetidbits != refasset ) + { + fprintf(stderr,"assetidbits %llu != %llu refasset\n",(long long)assetidbits,(long long)refasset); + return(-12); + } + if ( qty != quantity ) + { + fprintf(stderr,"qty.%llu != %llu\n",(long long)qty,(long long)quantity); + return(-13); + } + return(0); + } + return(-1); +} + +int32_t InstantDEX_verify(uint64_t destNXTaddr,uint64_t sendasset,uint64_t sendqty,cJSON *txobj,uint64_t recvasset,uint64_t recvqty) +{ + int32_t err; + // verify recipient, amounts in txobj + if ( (err= verify_NXTtx(txobj,recvasset,recvqty,destNXTaddr)) != 0 ) + { + if ( Debuglevel > 2 ) + printf("InstantDEX_verify dest.(%llu) tx mismatch %d (%llu %lld) -> (%llu %lld)\n",(long long)destNXTaddr,err,(long long)sendasset,(long long)sendqty,(long long)recvasset,(long long)recvqty); + return(-1); + } + return(0); +} + +cJSON *wallet_swapjson(char *recv,uint64_t recvasset,char *send,uint64_t sendasset,uint64_t orderid,uint64_t quoteid) +{ + return(cJSON_Parse(clonestr("{\"error\":\"notyet\"}"))); +#ifdef notyet + int32_t iter; uint64_t assetid; struct coin777 *coin; struct InstantDEX_quote *iQ; + char account[128],walletstr[512],*addr,*str; cJSON *walletitem = 0; + printf("wallet_swapjson is not yet\n"); + if ( (iQ= find_iQ(quoteid)) != 0 && iQ->s.wallet != 0 ) + { + walletitem = cJSON_Parse(iQ->walletstr); + //printf("start with (%s)\n",iQ->walletstr); + } + for (iter=0; iter<2; iter++) + { + addr = 0; + str = (iter == 0) ? recv : send; + assetid = (iter == 0) ? recvasset : sendasset; + if ( (coin= coin777_find(str,1)) != 0 ) + { + if ( is_NXT_native(assetid) == 0 ) + { + if ( (walletitem= set_walletstr(walletitem,walletstr,iQ)) != 0 ) + { + + } + } // else printf("%s is NXT\n",coin->name); + if ( is_NXT_native(assetid) != 0 ) + addr = SUPERNET.NXTADDR; + else + { + addr = (iter == 0) ? coin->atomicrecv : coin->atomicsend; + if ( addr[0] == 0 ) + addr = get_acct_coinaddr(addr,str,coin->serverport,coin->userpass,account); + } + if ( addr != 0 ) + { + } else printf("%s no addr\n",coin->name); + } else printf("cant find coin.(%s)\n",iter == 0 ? recv : send); + } + if ( walletitem == 0 ) + walletitem = cJSON_CreateObject(), jaddstr(walletitem,"error","cant find local coin daemons"); + return(walletitem); +#endif +} + +void _prices777_item(cJSON *item,int32_t group,struct prices777 *prices,int32_t bidask,double price,double volume,uint64_t orderid,uint64_t quoteid) +{ + uint64_t baseqty,relqty; int32_t iswallet = 0; char basec,relc; struct InstantDEX_quote *iQ; + jaddnum(item,"group",group); + jaddstr(item,"exchange",prices->exchange); + jaddstr(item,"base",prices->base), jaddstr(item,"rel",prices->rel); + if ( (iQ= find_iQ(quoteid)) != 0 ) + jadd64bits(item,"offerNXT",iQ->s.offerNXT); + if ( strcmp(prices->exchange,"nxtae") == 0 || strcmp(prices->exchange,"unconf") == 0 || strcmp(prices->exchange,"InstantDEX") == 0 || strcmp(prices->exchange,"wallet") == 0 ) + { + jadd64bits(item,prices->type == 5 ? "currency" : "asset",prices->baseid); + //else if ( quoteid != 0 ) printf("cant find offerNXT.%llu\n",(long long)quoteid); + jadd64bits(item,"baseid",prices->baseid), jadd64bits(item,"relid",prices->relid); + iswallet = (strcmp(prices->exchange,"wallet") == 0); + if ( strcmp(prices->exchange,"InstantDEX") == 0 || iswallet != 0 ) + { + jaddstr(item,"trade","swap"); + baseqty = calc_qty(prices->basemult,prices->baseid,SATOSHIDEN * volume + 0.5/SATOSHIDEN); + //printf("baseid.%llu basemult.%llu -> %llu\n",(long long)prices->baseid,(long long)prices->basemult,(long long)baseqty); + relqty = calc_qty(prices->relmult,prices->relid,SATOSHIDEN * volume * price + 0.5/SATOSHIDEN); + if ( bidask != 0 ) + { + basec = '+', relc = '-'; + jadd64bits(item,"recvbase",baseqty); + jadd64bits(item,"sendrel",relqty); + if ( iswallet != 0 ) + jadd(item,"wallet",wallet_swapjson(prices->base,prices->baseid,prices->rel,prices->relid,orderid,quoteid)); + } + else + { + basec = '-', relc = '+'; + jadd64bits(item,"sendbase",baseqty); + jadd64bits(item,"recvrel",relqty); + if ( iswallet != 0 ) + jadd(item,"wallet",wallet_swapjson(prices->rel,prices->relid,prices->base,prices->baseid,orderid,quoteid)); + } + //printf("(%s %cbaseqty.%llu <-> %s %crelqty.%llu) basemult.%llu baseid.%llu vol %f amount %llu\n",prices->base,basec,(long long)baseqty,prices->rel,relc,(long long)relqty,(long long)prices->basemult,(long long)prices->baseid,volume,(long long)volume*SATOSHIDEN); + } + else + { + //printf("alternate path\n"); + jaddstr(item,"trade",bidask == 0 ? "sell" : "buy"); + } + } + else + { + //printf("alternate path\n"); + jaddstr(item,"trade",bidask == 0 ? "sell" : "buy"); + jaddstr(item,"name",prices->contract); + } + jaddnum(item,"orderprice",price); + jaddnum(item,"ordervolume",volume); + if ( orderid != 0 ) + jadd64bits(item,"orderid",orderid); + if ( quoteid != 0 ) + jadd64bits(item,"quoteid",quoteid); +} + +cJSON *prices777_item(int32_t rootbidask,struct prices777 *prices,int32_t group,int32_t bidask,double origprice,double origvolume,double rootwt,double groupwt,double wt,uint64_t orderid,uint64_t quoteid) +{ + cJSON *item; double price,volume,oppo = 1.; + item = cJSON_CreateObject(); + jaddstr(item,"basket",rootbidask == 0 ? "bid":"ask"); + //jaddnum(item,"rootwt",rootwt); + //jaddnum(item,"groupwt",groupwt); + //jaddnum(item,"wt",wt); + if ( wt*groupwt < 0. ) + oppo = -1.; + if ( wt*groupwt < 0 ) + { + volume = origprice * origvolume; + price = 1./origprice; + } else price = origprice, volume = origvolume; + jaddnum(item,"price",price); + jaddnum(item,"volume",volume); + if ( groupwt*wt < 0 ) + { + volume = origprice * origvolume; + price = 1./origprice; + } else price = origprice, volume = origvolume; + _prices777_item(item,group,prices,bidask,price,volume,orderid,quoteid); + return(item); +} + +cJSON *prices777_tradeitem(int32_t rootbidask,struct prices777 *prices,int32_t group,int32_t bidask,int32_t slot,uint32_t timestamp,double price,double volume,double rootwt,double groupwt,double wt,uint64_t orderid,uint64_t quoteid) +{ + static uint32_t match,error; + if ( prices->O.timestamp == timestamp ) + { + //printf("tradeitem.(%s %f %f)\n",prices->exchange,price,volume); + if ( bidask == 0 && prices->O.book[MAX_GROUPS][slot].bid.s.price == price && prices->O.book[MAX_GROUPS][slot].bid.s.vol == volume ) + match++; + else if ( bidask != 0 && prices->O.book[MAX_GROUPS][slot].ask.s.price == price && prices->O.book[MAX_GROUPS][slot].ask.s.vol == volume ) + match++; + } + else if ( prices->O2.timestamp == timestamp ) + { + //printf("2tradeitem.(%s %f %f)\n",prices->exchange,price,volume); + if ( bidask == 0 && prices->O2.book[MAX_GROUPS][slot].bid.s.price == price && prices->O2.book[MAX_GROUPS][slot].bid.s.vol == volume ) + match++; + else if ( bidask != 0 && prices->O2.book[MAX_GROUPS][slot].ask.s.price == price && prices->O2.book[MAX_GROUPS][slot].ask.s.vol == volume ) + match++; + } else error++, printf("mismatched tradeitem error.%d match.%d\n",error,match); + return(prices777_item(rootbidask,prices,group,bidask,price,volume,rootwt,groupwt,wt,orderid,quoteid)); +} + +cJSON *prices777_tradesequence(struct prices777 *prices,int32_t bidask,struct prices777_order *orders[],double rootwt,double groupwt,double wt,int32_t refgroup) +{ + int32_t i,j,srcslot,srcbidask,err = 0; cJSON *array; struct prices777_order *suborders[MAX_GROUPS]; + struct prices777_order *order; struct prices777 *src; + array = cJSON_CreateArray(); + for (i=0; inumgroups; i++) + { + order = orders[i]; + groupwt = prices->groupwts[i]; + memset(suborders,0,sizeof(suborders)); + srcbidask = (order->slot_ba & 1); srcslot = order->slot_ba >> 1; + if ( (src= order->source) != 0 ) + { + if ( src->basketsize == 0 ) + jaddi(array,prices777_tradeitem(bidask,src,refgroup*10+i,srcbidask,srcslot,order->s.timestamp,order->s.price,order->ratio*order->s.vol,rootwt,groupwt/groupwt,order->wt,order->id,order->s.quoteid)); + else if ( src->O.timestamp == order->s.timestamp ) + { + for (j=0; jnumgroups; j++) + suborders[j] = (srcbidask == 0) ? &src->O.book[j][srcslot].bid : &src->O.book[j][srcslot].ask; + jaddi(array,prices777_tradesequence(src,bidask,suborders,rootwt,groupwt/groupwt,order->wt,refgroup*10 + i)); + } + else if ( src->O2.timestamp == order->s.timestamp ) + { + for (j=0; jnumgroups; j++) + suborders[j] = (srcbidask == 0) ? &src->O2.book[j][srcslot].bid : &src->O2.book[j][srcslot].ask; + jaddi(array,prices777_tradesequence(src,bidask,suborders,rootwt,groupwt/groupwt,order->wt,refgroup*10 + i)); + } + else err = 1; + } + if ( src == 0 || err != 0 ) + { + //jaddi(array,prices777_item(prices,bidask,price,volume,wt,orderid)); + //printf("prices777_tradesequence warning cant match timestamp %u (%s %s/%s)\n",order->timestamp,prices->contract,prices->base,prices->rel); + } + } + return(array); +} + +void prices777_orderbook_item(struct prices777 *prices,int32_t bidask,struct prices777_order *suborders[],cJSON *array,int32_t invert,int32_t allflag,double origprice,double origvolume,uint64_t orderid,uint64_t quoteid) +{ + cJSON *item,*obj,*tarray,*walletitem; double price,volume; struct InstantDEX_quote *iQ; + item = cJSON_CreateObject(); + if ( invert != 0 ) + volume = (origvolume * origprice), price = 1./origprice; + else price = origprice, volume = origvolume; + if ( strcmp(prices->exchange,"jumblr") == 0 || strcmp(prices->exchange,"pangea") == 0 ) + { + jaddstr(item,"plugin",prices->exchange), jaddstr(item,"method","start"); + jaddnum(item,"dotrade",1), jaddnum(item,"volume",volume); + jaddnum(item,"timeout",20000); + jaddstr(item,"base",prices->base); + if ( (iQ= find_iQ(quoteid)) != 0 ) + jadd64bits(item,"offerNXT",iQ->s.offerNXT); + jadd64bits(item,"quoteid",iQ->s.quoteid); + if ( strcmp(prices->exchange,"pangea") == 0 && iQ->s.wallet != 0 && (walletitem= cJSON_Parse(iQ->walletstr)) != 0 ) + jadd(item,"wallet",walletitem); + jaddi(array,item); + return; + } + jaddstr(item,"plugin","InstantDEX"), jaddstr(item,"method","tradesequence"); + jaddnum(item,"dotrade",1), jaddnum(item,"price",price), jaddnum(item,"volume",volume); + //jaddnum(item,"invert",invert), jaddnum(item,"origprice",origprice), jaddnum(item,"origvolume",origvolume); + //jaddstr(item,"base",prices->base), jaddstr(item,"rel",prices->rel); + if ( allflag != 0 ) + { + if ( prices->basketsize == 0 ) + { + tarray = cJSON_CreateArray(); + obj = cJSON_CreateObject(); + _prices777_item(obj,0,prices,bidask,origprice,origvolume,orderid,quoteid); + jaddi(tarray,obj); + } else tarray = prices777_tradesequence(prices,bidask,suborders,invert!=0?-1:1.,1.,1.,0); + jadd(item,"trades",tarray); + } + jaddi(array,item); +} + +char *prices777_orderbook_jsonstr(int32_t invert,uint64_t nxt64bits,struct prices777 *prices,struct prices777_basketinfo *OB,int32_t maxdepth,int32_t allflag) +{ + struct prices777_orderentry *gp; struct prices777_order *suborders[MAX_GROUPS]; cJSON *json,*bids,*asks; + int32_t i,slot; char baserel[64],base[64],rel[64],assetA[64],assetB[64],NXTaddr[64]; + if ( invert == 0 ) + sprintf(baserel,"%s/%s",prices->base,prices->rel); + else sprintf(baserel,"%s/%s",prices->rel,prices->base); + if ( Debuglevel > 2 ) + printf("ORDERBOOK %s/%s iQsize.%ld numbids.%d numasks.%d maxdepth.%d (%llu %llu)\n",prices->base,prices->rel,(long)sizeof(struct InstantDEX_quote),OB->numbids,OB->numasks,maxdepth,(long long)prices->baseid,(long long)prices->relid); + json = cJSON_CreateObject(), bids = cJSON_CreateArray(), asks = cJSON_CreateArray(); + gp = &OB->book[MAX_GROUPS][0]; + memset(suborders,0,sizeof(suborders)); + for (slot=0; (slotnumbids || slotnumasks) && slotnumbids ) + { + for (i=0; inumgroups; i++) + suborders[i] = &OB->book[i][slot].bid; + prices777_orderbook_item(prices,0,suborders,(invert==0) ? bids : asks,invert,allflag,gp->bid.s.price,gp->bid.s.vol,gp->bid.id,gp->bid.s.quoteid); + } + if ( slot < OB->numasks ) + { + for (i=0; inumgroups; i++) + suborders[i] = &OB->book[i][slot].ask; + prices777_orderbook_item(prices,1,suborders,(invert==0) ? asks : bids,invert,allflag,gp->ask.s.price,gp->ask.s.vol,gp->ask.id,gp->ask.s.quoteid); + } + } + expand_nxt64bits(NXTaddr,nxt64bits); + if ( invert != 0 ) + strcpy(base,prices->rel), strcpy(rel,prices->base); + else strcpy(base,prices->base), strcpy(rel,prices->rel); + expand_nxt64bits(assetA,invert==0 ? prices->baseid : prices->relid); + expand_nxt64bits(assetB,invert!=0 ? prices->baseid : prices->relid); + cJSON_AddItemToObject(json,"exchange",cJSON_CreateString(prices->exchange)); + cJSON_AddItemToObject(json,"inverted",cJSON_CreateNumber(invert)); + cJSON_AddItemToObject(json,"contract",cJSON_CreateString(prices->contract)); + cJSON_AddItemToObject(json,"baseid",cJSON_CreateString(assetA)); + if ( assetB[0] != 0 ) + cJSON_AddItemToObject(json,"relid",cJSON_CreateString(assetB)); + cJSON_AddItemToObject(json,"base",cJSON_CreateString(base)); + if ( rel[0] != 0 ) + cJSON_AddItemToObject(json,"rel",cJSON_CreateString(rel)); + cJSON_AddItemToObject(json,"bids",bids); + cJSON_AddItemToObject(json,"asks",asks); + if ( invert == 0 ) + { + cJSON_AddItemToObject(json,"numbids",cJSON_CreateNumber(OB->numbids)); + cJSON_AddItemToObject(json,"numasks",cJSON_CreateNumber(OB->numasks)); + cJSON_AddItemToObject(json,"lastbid",cJSON_CreateNumber(prices->lastbid)); + cJSON_AddItemToObject(json,"lastask",cJSON_CreateNumber(prices->lastask)); + } + else + { + cJSON_AddItemToObject(json,"numbids",cJSON_CreateNumber(OB->numasks)); + cJSON_AddItemToObject(json,"numasks",cJSON_CreateNumber(OB->numbids)); + if ( prices->lastask != 0 ) + cJSON_AddItemToObject(json,"lastbid",cJSON_CreateNumber(1. / prices->lastask)); + if ( prices->lastbid != 0 ) + cJSON_AddItemToObject(json,"lastask",cJSON_CreateNumber(1. / prices->lastbid)); + } + cJSON_AddItemToObject(json,"NXT",cJSON_CreateString(NXTaddr)); + cJSON_AddItemToObject(json,"timestamp",cJSON_CreateNumber(time(NULL))); + cJSON_AddItemToObject(json,"maxdepth",cJSON_CreateNumber(maxdepth)); + return(jprint(json,1)); +} + +void prices777_jsonstrs(struct prices777 *prices,struct prices777_basketinfo *OB) +{ + int32_t allflag; char *strs[4]; + if ( OB->numbids == 0 && OB->numasks == 0 ) + { + printf("warning: updating null orderbook ignored for %s (%s/%s)\n",prices->contract,prices->base,prices->rel); + return; + } + for (allflag=0; allflag<4; allflag++) + { + strs[allflag] = prices777_orderbook_jsonstr(allflag/2,IGUANA_MY64BITS,prices,OB,MAX_DEPTH,allflag%2); + if ( Debuglevel > 2 ) + printf("strs[%d].(%s) prices.%p\n",allflag,strs[allflag],prices); + } + portable_mutex_lock(&prices->mutex); + for (allflag=0; allflag<4; allflag++) + { + if ( prices->orderbook_jsonstrs[allflag/2][allflag%2] != 0 ) + free(prices->orderbook_jsonstrs[allflag/2][allflag%2]); + prices->orderbook_jsonstrs[allflag/2][allflag%2] = strs[allflag]; + } + portable_mutex_unlock(&prices->mutex); +} + +char *orderbook_clonestr(struct prices777 *prices,int32_t invert,int32_t allflag) +{ + char *str,*clone = 0; + portable_mutex_lock(&prices->mutex); + if ( (str= prices->orderbook_jsonstrs[invert][allflag]) != 0 ) + clone = clonestr(str); + portable_mutex_unlock(&prices->mutex); + return(clone); +} + +void prices777_json_quotes(double *hblap,struct prices777 *prices,cJSON *bids,cJSON *asks,int32_t maxdepth,char *pricefield,char *volfield,uint32_t reftimestamp) +{ + cJSON *item; int32_t i,slot,n=0,m=0,dir,bidask,numitems; uint64_t orderid,quoteid; uint32_t timestamp; double price,volume,hbla = 0.; + struct prices777_basketinfo OB; struct prices777_orderentry *gp; struct prices777_order *order; + memset(&OB,0,sizeof(OB)); + if ( reftimestamp == 0 ) + reftimestamp = (uint32_t)time(NULL); + OB.timestamp = reftimestamp; + if ( bids != 0 ) + { + n = cJSON_GetArraySize(bids); + if ( maxdepth != 0 && n > maxdepth ) + n = maxdepth; + } + if ( asks != 0 ) + { + m = cJSON_GetArraySize(asks); + if ( maxdepth != 0 && m > maxdepth ) + m = maxdepth; + } + for (i=0; ibid.source = gp->ask.source = prices; + for (bidask=0; bidask<2; bidask++) + { + price = volume = 0.; + orderid = quoteid = 0; + dir = (bidask == 0) ? 1 : -1; + if ( bidask == 0 && i >= n ) + continue; + else if ( bidask == 1 && i >= m ) + continue; + if ( strcmp(prices->exchange,"bter") == 0 && dir < 0 ) + slot = ((bidask==0?n:m) - 1) - i; + else slot = i; + timestamp = 0; + item = jitem(bidask==0?bids:asks,slot); + if ( pricefield != 0 && volfield != 0 ) + price = jdouble(item,pricefield), volume = jdouble(item,volfield); + else if ( is_cJSON_Array(item) != 0 && (numitems= cJSON_GetArraySize(item)) != 0 ) // big assumptions about order within nested array! + { + price = jdouble(jitem(item,0),0), volume = jdouble(jitem(item,1),0); + if ( strcmp(prices->exchange,"kraken") == 0 ) + timestamp = juint(jitem(item,2),0); + else orderid = j64bits(jitem(item,2),0); + } + else continue; + if ( quoteid == 0 ) + quoteid = orderid; + if ( price > SMALLVAL && volume > SMALLVAL ) + { + if ( prices->commission != 0. ) + { + //printf("price %f fee %f -> ",price,prices->commission * price); + if ( bidask == 0 ) + price -= prices->commission * price; + else price += prices->commission * price; + //printf("%f\n",price); + } + order = (bidask == 0) ? &gp->bid : &gp->ask; + order->s.price = price, order->s.vol = volume, order->source = prices, order->s.timestamp = OB.timestamp, order->wt = 1, order->id = orderid, order->s.quoteid = quoteid; + if ( bidask == 0 ) + order->slot_ba = (OB.numbids++ << 1); + else order->slot_ba = (OB.numasks++ << 1) | 1; + if ( i == 0 ) + { + if ( bidask == 0 ) + prices->lastbid = price; + else prices->lastask = price; + if ( hbla == 0. ) + hbla = price; + else hbla = 0.5 * (hbla + price); + } + if ( Debuglevel > 2 )//|| prices->basketsize > 0 || strcmp("unconf",prices->exchange) == 0 ) + printf("%d,%d: %-8s %s %5s/%-5s %13.8f vol %13.8f | invert %13.8f vol %13.8f | timestamp.%u\n",OB.numbids,OB.numasks,prices->exchange,dir>0?"bid":"ask",prices->base,prices->rel,price,volume,1./price,volume*price,timestamp); + } + } + } + if ( hbla != 0. ) + *hblap = hbla; + prices->O2 = prices->O; + //prices->O = OB; + for (i=0; iO.book[i],prices->O.book[i+1],sizeof(prices->O.book[i])); + memcpy(prices->O.book[MAX_GROUPS],OB.book[MAX_GROUPS],sizeof(OB.book[MAX_GROUPS])); + prices->O.numbids = OB.numbids, prices->O.numasks = OB.numasks, prices->O.timestamp = OB.timestamp; +} + +double prices777_json_orderbook(char *exchangestr,struct prices777 *prices,int32_t maxdepth,cJSON *json,char *resultfield,char *bidfield,char *askfield,char *pricefield,char *volfield) +{ + cJSON *obj = 0,*bidobj=0,*askobj=0; double hbla = 0.; int32_t numasks=0,numbids=0; + if ( resultfield == 0 ) + obj = json; + if ( maxdepth == 0 ) + maxdepth = MAX_DEPTH; + if ( resultfield == 0 || (obj= jobj(json,resultfield)) != 0 ) + { + bidobj = jarray(&numbids,obj,bidfield); + askobj = jarray(&numasks,obj,askfield); + if ( bidobj != 0 || askobj != 0 ) + { + prices777_json_quotes(&hbla,prices,bidobj,askobj,maxdepth,pricefield,volfield,0); + prices777_jsonstrs(prices,&prices->O); + } + } + return(hbla); +} + +void prices777_hbla(uint64_t *bidorderid,uint64_t *askorderid,int32_t *lowaski,int32_t *highbidi,double *highbid,double *bidvol,double *lowask,double *askvol,double groupwt,int32_t i,int32_t bidask,double price,double vol,uint64_t orderid) +{ + if ( groupwt > SMALLVAL && (*lowask == 0. || price < *lowask) ) + *askorderid = orderid, *lowask = price, *askvol = vol, *lowaski = (i << 1) | bidask; + else if ( groupwt < -SMALLVAL && (*highbid == 0. || price > *highbid) ) + *bidorderid = orderid, *highbid = price, *bidvol = vol, *highbidi = (i << 1) | bidask; + //printf("hbla.(%f %f)\n",price,vol); +} + +void prices777_setorder(struct prices777_order *order,struct prices777_basket *group,int32_t coordinate,uint64_t orderid,double refprice,double refvol) +{ + int32_t bidask; struct prices777 *prices; double price=0,vol=0; + bidask = (coordinate & 1), coordinate >>= 1; + prices = group[coordinate].prices; + if ( bidask != 0 && group[coordinate].aski < prices->O.numasks ) + price = prices->O.book[MAX_GROUPS][group[coordinate].aski].ask.s.price, vol = prices->O.book[MAX_GROUPS][group[coordinate].aski].ask.s.vol, order->slot_ba = (group[coordinate].aski++ << 1) | 1; + else if ( group[coordinate].bidi < prices->O.numbids ) + price = prices->O.book[MAX_GROUPS][group[coordinate].bidi].bid.s.price, vol = prices->O.book[MAX_GROUPS][group[coordinate].bidi].bid.s.vol,order->slot_ba = (group[coordinate].bidi++ << 1); + else printf("illegal coordinate.%d bidask.%d when bidi.%d aski.%d numbids.%d numasks.%d\n",coordinate,bidask,group[coordinate].bidi,group[coordinate].aski,prices->O.numbids,prices->O.numasks); + order->source = prices; + order->wt = group[coordinate].wt; + order->s.timestamp = prices->O.timestamp; + order->id = orderid; + if ( order->wt < 0. ) + vol *= price, price = (1. / price); + if ( fabs(price - refprice) > SMALLVAL || fabs(vol - refvol) > SMALLVAL ) + { + printf("[ERROR] "); + printf("%s group.%d (%s/%s) bidask.%d coordinate.%d wt %.8f (%.8f %.8f) vs (%.8f %.8f)\n",prices->exchange,group[coordinate].groupid,prices->base,prices->rel,bidask,coordinate,order->wt,price,vol,refprice,refvol); + } +} + + +int32_t prices777_groupbidasks(struct prices777_orderentry *gp,double groupwt,double minvol,struct prices777_basket *group,int32_t groupsize) +{ + int32_t i,highbidi,lowaski; double highbid,lowask,bidvol,askvol,vol,price,polarity; uint64_t bidorderid,askorderid; + struct prices777 *feature; struct prices777_order *order; + memset(gp,0,sizeof(*gp)); + highbidi = lowaski = -1; + for (bidvol=askvol=highbid=lowask=bidorderid=askorderid=i=0; ibase,group[0].rel) == 0 && strcmp(feature->rel,group[0].base) == 0 ) + // polarity = -1.; + //else polarity = 1.; + //if ( group[i].wt * groupwt < 0 ) fixes supernet/BTC + // polarity *= -1; + polarity = group[i].wt;// * groupwt; + order = &feature->O.book[MAX_GROUPS][group[i].bidi].bid; + if ( group[i].bidi < feature->O.numbids && (vol= order->s.vol) > minvol && (price= order->s.price) > SMALLVAL ) + { + //printf("%d/%d: (%s/%s) %s bidi.%d price.%f polarity.%f groupwt.%f -> ",i,groupsize,feature->base,feature->rel,feature->exchange,group[i].bidi,price,polarity,groupwt); + if ( polarity < 0. ) + vol *= price, price = (1. / price); + prices777_hbla(&bidorderid,&askorderid,&lowaski,&highbidi,&highbid,&bidvol,&lowask,&askvol,-polarity,i,0,price,vol,order->id); + } + order = &feature->O.book[MAX_GROUPS][group[i].aski].ask; + if ( group[i].aski < feature->O.numasks && (vol= order->s.vol) > minvol && (price= order->s.price) > SMALLVAL ) + { + //printf("%d/%d: (%s/%s) %s aski.%d price.%f polarity.%f groupwt.%f -> ",i,groupsize,feature->base,feature->rel,feature->exchange,group[i].aski,price,polarity,groupwt); + if ( polarity < 0. ) + vol *= price, price = (1. / price); + prices777_hbla(&bidorderid,&askorderid,&lowaski,&highbidi,&highbid,&bidvol,&lowask,&askvol,polarity,i,1,price,vol,order->id); + } + } else printf("null feature.%p\n",feature); + } + gp->bid.s.price = highbid, gp->bid.s.vol = bidvol, gp->ask.s.price = lowask, gp->ask.s.vol = askvol; + if ( highbidi >= 0 ) + prices777_setorder(&gp->bid,group,highbidi,bidorderid,highbid,bidvol); + if ( lowaski >= 0 ) + prices777_setorder(&gp->ask,group,lowaski,askorderid,lowask,askvol); + // if ( lowaski >= 0 && highbidi >= 0 ) +//printf("groupwt %f groupsize.%d %s highbidi.%d %f %f %s lowaski.%d wts.(%f %f)\n",groupwt,groupsize,gp->bid.source->exchange,highbidi,gp->bid.s.price,gp->ask.s.price,gp->ask.source->exchange,lowaski,gp->bid.wt,gp->ask.wt); + if ( gp->bid.s.price > SMALLVAL && gp->ask.s.price > SMALLVAL ) + return(0); + return(-1); +} + +double prices777_volcalc(double *basevols,uint64_t *baseids,uint64_t baseid,double basevol) +{ + int32_t i; + //printf("(add %llu %f) ",(long long)baseid,basevol); + for (i=0; i 0. ) + { + //printf("(vol %f vs %f) ",vol,basevols[i]); + if ( vol > basevols[i] ) + return(basevols[i]/vol); + else return(1.); + } + printf("unexpected zero vol basevols.%d\n",i); + return(1.); + break; + } + } + printf("unexpected cant find baseid.%llu\n",(long long)baseid); + return(1.); +} + +double prices777_basket(struct prices777 *prices,int32_t maxdepth) +{ + int32_t i,j,groupsize,slot; uint64_t baseids[MAX_GROUPS*2]; + double basevols[MAX_GROUPS*2],relvols[MAX_GROUPS*2],baseratio,relratio,a,av,b,bv,gap,bid,ask,minvol,bidvol,askvol,hbla = 0.; + struct prices777_basketinfo OB; uint32_t timestamp; struct prices777 *feature; struct prices777_orderentry *gp; + a = av = b = bv = 0; + timestamp = (uint32_t)time(NULL); + memset(&OB,0,sizeof(OB)); + memset(baseids,0,sizeof(baseids)); + OB.timestamp = timestamp; + //printf("prices777_basket.(%s) %s (%s/%s) %llu/%llu basketsize.%d\n",prices->exchange,prices->contract,prices->base,prices->rel,(long long)prices->baseid,(long long)prices->relid,prices->basketsize); + for (i=0; ibasketsize; i++) + { + if ( 0 && strcmp(prices->exchange,"active") == 0 && prices->basket[i].prices != 0 ) + printf("%s.group.%d %10s %10s/%10s wt %3.0f %.8f %.8f\n",prices->exchange,i,prices->basket[i].prices->exchange,prices->basket[i].prices->base,prices->basket[i].rel,prices->basket[i].wt,prices->basket[i].prices->O.book[MAX_GROUPS][0].bid.s.price,prices->basket[i].prices->O.book[MAX_GROUPS][0].ask.s.price); + if ( (feature= prices->basket[i].prices) != 0 ) + { + if ( 0 && (gap= (prices->lastupdate - feature->lastupdate)) < 0 ) + { + if ( prices->lastupdate != 0 ) + printf("you can ignore this harmless warning about unexpected time traveling feature %f vs %f or laggy feature\n",prices->lastupdate,feature->lastupdate); + return(0.); + } + } + else + { + printf("unexpected null basket item %s[%d]\n",prices->contract,i); + return(0.); + } + prices->basket[i].aski = prices->basket[i].bidi = 0; + for (j=0; jbasket[i].prices == 0 || prices->basket[i].prices->baseid == baseids[j] ) + break; + if ( baseids[j] == 0 ) + { + baseids[j] = prices->basket[i].prices->baseid; + break; + } + } + for (j=0; jbasket[i].prices == 0 || prices->basket[i].prices->relid == baseids[j] ) + break; + if ( baseids[j] == 0 ) + { + baseids[j] = prices->basket[i].prices->relid; + break; + } + } + } + //printf("%s basketsize.%d numgroups.%d maxdepth.%d group0size.%d\n",prices->contract,prices->basketsize,prices->numgroups,maxdepth,prices->basket[0].groupsize); + for (slot=0; slotbasket[0].groupsize; + minvol = (1. / SATOSHIDEN); + bid = ask = 1.; bidvol = askvol = 0.; + for (j=i=0; jnumgroups; j++,i+=groupsize) + { + groupsize = prices->basket[i].groupsize; + gp = &OB.book[j][slot]; + if ( prices777_groupbidasks(gp,prices->groupwts[j],minvol,&prices->basket[i],groupsize) != 0 ) + { + //printf("prices777_groupbidasks i.%d j.%d error\n",i,j); + break; + } + //printf("%s j%d slot.%d %s numgroups.%d groupsize.%d\n",prices->exchange,j,slot,prices->contract,prices->numgroups,groupsize); + if ( bid > SMALLVAL && (b= gp->bid.s.price) > SMALLVAL && (bv= gp->bid.s.vol) > SMALLVAL ) + { + //if ( gp->bid.wt*prices->groupwts[j] < 0 ) + // bid /= b; + //else + bid *= b; + prices777_volcalc(basevols,baseids,gp->bid.source->baseid,bv); + prices777_volcalc(basevols,baseids,gp->bid.source->relid,b*bv); + //printf("bid %f b %f bv %f %s %s %f\n",bid,b,bv,gp->bid.source->base,gp->bid.source->rel,bv*b); + } else bid = 0.; + if ( ask > SMALLVAL && (a= gp->ask.s.price) > SMALLVAL && (av= gp->ask.s.vol) > SMALLVAL ) + { + //if ( gp->ask.wt*prices->groupwts[j] < 0 ) + // ask /= a; + //else + ask *= a; + prices777_volcalc(relvols,baseids,gp->ask.source->baseid,av); + prices777_volcalc(relvols,baseids,gp->ask.source->relid,a*av); + //printf("ask %f b %f bv %f %s %s %f\n",ask,a,av,gp->ask.source->base,gp->ask.source->rel,av*a); + } else ask = 0.; + if ( Debuglevel > 2 ) + printf("%10s %10s/%10s %s (%s %s) wt:%f %2.0f/%2.0f j.%d: b %.8f %12.6f a %.8f %12.6f, bid %.8f ask %.8f inv %f %f\n",prices->exchange,gp->bid.source->exchange,gp->ask.source->exchange,prices->contract,gp->bid.source->contract,gp->ask.source->contract,prices->groupwts[j],gp->bid.wt,gp->ask.wt,j,b,bv,a,av,bid,ask,1/bid,1/ask); + } + for (j=0; jnumgroups; j++) + { + gp = &OB.book[j][slot]; + if ( gp->bid.source == 0 || gp->ask.source == 0 ) + { + printf("%s: (%s/%s) null source slot.%d j.%d\n",prices->exchange,prices->base,prices->rel,slot,j); + break; + } + baseratio = prices777_volratio(basevols,baseids,gp->bid.source->baseid,gp->bid.s.vol); + relratio = prices777_volratio(basevols,baseids,gp->bid.source->relid,gp->bid.s.vol * gp->bid.s.price); + gp->bid.ratio = (baseratio < relratio) ? baseratio : relratio; + if ( j == 0 ) + bidvol = (gp->bid.ratio * gp->bid.s.vol); + //printf("(%f %f) (%f %f) bid%d bidratio %f bidvol %f ",gp->bid.s.vol,baseratio,gp->bid.s.vol * gp->bid.s.price,relratio,j,gp->bid.ratio,bidvol); + baseratio = prices777_volratio(relvols,baseids,gp->ask.source->baseid,gp->ask.s.vol); + relratio = prices777_volratio(relvols,baseids,gp->ask.source->relid,gp->ask.s.vol * gp->ask.s.price); + gp->ask.ratio = (baseratio < relratio) ? baseratio : relratio; + if ( j == 0 ) + askvol = (gp->ask.ratio * gp->ask.s.vol); + } + if ( j != prices->numgroups ) + { + //printf("%s: j.%d != numgroups.%d\n",prices->exchange,j,prices->numgroups); + break; + } + for (j=0; j SMALLVAL && bidvol > SMALLVAL ) + { + if ( slot == 0 ) + prices->lastbid = bid; + gp->bid.s.timestamp = OB.timestamp, gp->bid.s.price = bid, gp->bid.s.vol = bidvol, gp->bid.slot_ba = (OB.numbids++ << 1); + gp->bid.source = prices, gp->bid.wt = prices->groupwts[j]; + } + if ( ask > SMALLVAL && askvol > SMALLVAL ) + { + if ( slot == 0 ) + prices->lastask = ask; + gp->ask.s.timestamp = OB.timestamp, gp->ask.s.price = ask, gp->ask.s.vol = askvol, gp->ask.slot_ba = (OB.numasks++ << 1) | 1; + gp->ask.source = prices, gp->ask.wt = prices->groupwts[j]; + } + //printf("%s %s slot.%d (%.8f %.6f %.8f %.6f) (%d %d)\n",prices->exchange,prices->contract,slot,gp->bid.s.price,gp->bid.s.vol,gp->ask.s.price,gp->ask.s.vol,OB.numbids,OB.numasks); + } + //fprintf(stderr,"%s basket.%s slot.%d numbids.%d numasks.%d %f %f\n",prices->exchange,prices->contract,slot,prices->O.numbids,prices->O.numasks,prices->O.book[MAX_GROUPS][0].bid.s.price,prices->O.book[MAX_GROUPS][0].ask.s.price); + if ( slot > 0 ) + { + prices->O2 = prices->O; + prices->O = OB; + if ( prices->lastbid > SMALLVAL && prices->lastask > SMALLVAL ) + hbla = 0.5 * (prices->lastbid + prices->lastask); + } + return(hbla); +} + +struct prices777 *prices777_addbundle(int32_t *validp,int32_t loadprices,struct prices777 *prices,char *exchangestr,uint64_t baseid,uint64_t relid) +{ + int32_t j; struct prices777 *ptr; struct exchange_info *exchange; + *validp = -1; + if ( prices != 0 ) + { + exchangestr = prices->exchange; + baseid = prices->baseid, relid = prices->relid; + } + for (j=0; jbaseid == baseid && ptr->relid == relid) || (ptr->relid == baseid && ptr->baseid == relid)) && strcmp(ptr->exchange,exchangestr) == 0 ) + return(ptr); + } + if ( j == BUNDLE.num ) + { + if ( prices != 0 ) + { + exchange = &Exchanges[prices->exchangeid]; + if ( loadprices != 0 && exchange->issue.update != 0 ) + { + portable_mutex_lock(&exchange->mutex); + (exchange->issue.update)(prices,MAX_DEPTH); + portable_mutex_unlock(&exchange->mutex); + } + printf("total polling.%d added.(%s)\n",BUNDLE.num,prices->contract); + if ( exchange->polling == 0 ) + { + printf("First pair for (%s), start polling]\n",exchange_str(prices->exchangeid)); + exchange->polling = 1; + if ( strcmp(exchange->name,"wallet") != 0 )//&& strcmp(exchange->name,"jumblr") != 0 && strcmp(exchange->name,"pangea") != 0 ) + iguana_launch(iguana_coinadd("BTCD"),"exchangeloop",(void *)prices777_exchangeloop,&Exchanges[prices->exchangeid],IGUANA_EXCHANGETHREAD); + } + BUNDLE.ptrs[BUNDLE.num] = prices; + printf("prices777_addbundle.(%s) (%s/%s).%s %llu %llu\n",prices->contract,prices->base,prices->rel,prices->exchange,(long long)prices->baseid,(long long)prices->relid); + BUNDLE.num++; + } else printf("no prices\n"); + *validp = BUNDLE.num; + return(prices); + } + return(0); +} + +int32_t is_native_crypto(char *name,uint64_t bits) +{ + int32_t i,n; + if ( (n= (int32_t)strlen(name)) > 0 || (n= unstringbits(name,bits)) <= 5 ) + { + for (i=0; i= '0' && name[i] <= '9') || (name[i] >= 'A' && name[i] <= 'Z') )// || (name[i] >= '0' && name[i] <= '9') ) + continue; + printf("(%s) is not native crypto\n",name); + return(0); + } + printf("(%s) is native crypto\n",name); + return(1); + } + return(0); +} + +char *_issue_getAsset(char *assetidstr) +{ + char cmd[4096],*jsonstr; + //sprintf(cmd,"requestType=getAsset&asset=%s",assetidstr); + sprintf(cmd,"requestType=getAsset&asset=%s",assetidstr); + //printf("_cmd.(%s)\n",cmd); + jsonstr = issue_NXTPOST(cmd); + //printf("(%s) -> (%s)\n",cmd,jsonstr); + return(jsonstr); +} + +char *_issue_getCurrency(char *assetidstr) +{ + char cmd[4096]; + //sprintf(cmd,"requestType=getAsset&asset=%s",assetidstr); + sprintf(cmd,"requestType=getCurrency¤cy=%s",assetidstr); + //printf("_cmd.(%s)\n",cmd); + return(issue_NXTPOST(cmd)); +} + +int32_t is_mscoin(char *assetidstr) +{ + char *jsonstr; cJSON *json; int32_t retcode = 0; + if ( (jsonstr= _issue_getCurrency(assetidstr)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( get_cJSON_int(json,"errorCode") == 0 ) + retcode = 1; + free_json(json); + } + free(jsonstr); + } + return(retcode); +} + +int32_t get_assetname(char *name,uint64_t assetid) +{ + char assetidstr[64],*jsonstr; cJSON *json; + name[0] = 0; + if ( is_native_crypto(name,assetid) != 0 ) + return((int32_t)strlen(name)); + expand_nxt64bits(assetidstr,assetid); + name[0] = 0; + if ( (jsonstr= _issue_getAsset(assetidstr)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + extract_cJSON_str(name,15,json,"name"); + free_json(json); + } + free(jsonstr); + } + return((int32_t)strlen(assetidstr)); +} + +uint32_t get_blockutime(uint32_t blocknum) +{ + cJSON *json; + uint32_t timestamp = 0; + char cmd[4096],*jsonstr; + sprintf(cmd,"requestType=getBlock&height=%u",blocknum); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (timestamp= juint(json,"timestamp")) != 0 ) + timestamp += NXT_GENESISTIME; + free_json(json); + } + free(jsonstr); + } + return(timestamp); +} + +uint64_t calc_decimals_mult(int32_t decimals) +{ + int32_t i; uint64_t mult = 1; + for (i=7-decimals; i>=0; i--) + mult *= 10; + return(mult); +} + +int32_t _set_assetname(uint64_t *multp,char *buf,char *jsonstr,uint64_t assetid) +{ + int32_t type = 0,decimals = -1; cJSON *json=0; char assetidstr[64],*str; + *multp = 1; + buf[0] = 0; + if ( assetid != 0 ) + { + //fprintf(stderr,"assetid.%llu\n",(long long)assetid); + if ( (str= is_MGWasset(assetid)) != 0 ) + { + strcpy(buf,str); + return(0); + } + if ( is_native_crypto(buf,assetid) != 0 ) + { + unstringbits(buf,assetid); + return(0); + } + } + if ( jsonstr == 0 ) + { + if ( assetid == 0 ) + printf("_set_assetname null assetid\n"), getchar(); + expand_nxt64bits(assetidstr,assetid); + type = 2; + if ( (jsonstr= _issue_getAsset(assetidstr)) != 0 ) + { + //printf("%llu (%s) -> (%s)\n",(long long)assetid,assetidstr,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( get_cJSON_int(json,"errorCode") != 0 ) + { + free_json(json), free(jsonstr); + if ( (jsonstr= _issue_getCurrency(assetidstr)) != 0 ) + { + //printf("(%s) -> (%s)\n",assetidstr,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( get_cJSON_int(json,"errorCode") != 0 ) + { + printf("(%s) not asset and not currency (%s)\n",assetidstr,jsonstr);//, getchar(); + free_json(json), free(jsonstr); + return(-1); + } + type = 5; + } + } + } + } + free(jsonstr), jsonstr = 0; + } else return(-1); + } + if ( multp != 0 ) + *multp = 0; + if ( json == 0 ) + json = cJSON_Parse(jsonstr); + if ( json != 0 ) + { + if ( get_cJSON_int(json,"errorCode") == 0 ) + { + decimals = (int32_t)get_cJSON_int(json,"decimals"); + if ( multp != 0 && decimals >= 0 && decimals <= 8 ) + *multp = calc_decimals_mult(decimals); + if ( extract_cJSON_str(buf,16,json,"name") <= 0 ) + decimals = -1; + //printf("%s decimals.%d (%s)\n",assetidstr,decimals,buf); + } + free_json(json); + } + return(type); +} + +char *peggy_mapname(char *basebuf,char *relbuf,int32_t i) // sorry it is messy thing +{ + char *base,*rel,buf[16]; + base = rel = 0; + strcpy(buf,peggy_contracts[i]); + base = buf, rel = "BTCD"; + if ( strlen(buf) > 3 && strcmp(buf+strlen(buf)-3,"USD") == 0 ) + { + if ( strcmp(buf,"BTCUSD") == 0 ) + base = "BTC"; + buf[strlen(buf)-3] = 0; + } + else if ( strcmp(buf,"Copper") == 0 || strcmp(buf,"NGAS") == 0 || strcmp(buf,"UKOil") == 0 || strcmp(buf,"USOil") == 0 || strcmp(buf,"US30") == 0 || strcmp(buf,"SPX500") == 0 || strcmp(buf,"NAS100") == 0 ) + rel = "USD"; + else if ( strcmp(buf,"Bund") == 0 ) + rel = "yield"; + else if ( strcmp(buf,"EUSTX50") == 0 ) + rel = "EUR"; + else if ( strcmp(buf,"JPN225") == 0 ) + rel = "JPY"; + else if ( strcmp(buf,"UK100") == 0 ) + rel = "GBP"; + else if ( strcmp(buf,"GER30") == 0 ) + rel = "EUR"; + else if ( strcmp(buf,"SUI30") == 0 ) + rel = "CHF"; + else if ( strcmp(buf,"AUS200") == 0 ) + rel = "AUD"; + else if ( strcmp(buf,"HKG33") == 0 ) + rel = "HKD"; + else if ( strlen(buf) > 3 && strcmp(buf+strlen(buf)-3,"BTC") == 0 ) + base = buf, buf[strlen(buf)-3] = 0; + if ( i == sizeof(peggy_contracts)/sizeof(*peggy_contracts)-1 && strcmp(peggy_contracts[i],"BTCUSD") == 0 ) + base = "BTC", rel = "USD"; + else if ( i == sizeof(peggy_contracts)/sizeof(*peggy_contracts)-2 && strcmp(peggy_contracts[i],"BTCCNY") == 0 ) + base = "BTC", rel = "CNY"; + else if ( i == sizeof(peggy_contracts)/sizeof(*peggy_contracts)-3 && strcmp(peggy_contracts[i],"BTCRUB") == 0 ) + base = "BTC", rel = "RUB"; + else if ( i == sizeof(peggy_contracts)/sizeof(*peggy_contracts)-4 && strcmp(peggy_contracts[i],"XAUUSD") == 0 ) + base = "XAU", rel = "USD"; + else if ( i == 0 ) + base = "BTCD", rel = "maincurrency peggy, price is BTCD/BTC for info only"; + basebuf[0] = relbuf[0] = 0; + if ( rel != 0 ) + strcpy(relbuf,rel);//, printf("rel.(%s) ",rel); + if ( base != 0 ) + strcpy(basebuf,base);//, printf("base.(%s) ",base); + return(basebuf); +} + +uint64_t peggy_basebits(char *name) +{ + int32_t i; char basebuf[64],relbuf[64]; + for (i=0; i<64; i++) + { + if ( strcmp(name,peggy_contracts[i]) == 0 ) + { + peggy_mapname(basebuf,relbuf,i); + return(stringbits(basebuf)); + } + } + return(0); +} + +uint64_t peggy_relbits(char *name) +{ + int32_t i; char basebuf[64],relbuf[64]; + for (i=0; i<64; i++) + { + if ( strcmp(name,peggy_contracts[i]) == 0 ) + { + peggy_mapname(basebuf,relbuf,i); + return(stringbits(relbuf)); + } + } + return(0); +} + +int32_t prices777_key(char *key,char *exchange,char *name,char *base,uint64_t baseid,char *rel,uint64_t relid) +{ + int32_t len,keysize = 0; + memcpy(&key[keysize],&baseid,sizeof(baseid)), keysize += sizeof(baseid); + memcpy(&key[keysize],&relid,sizeof(relid)), keysize += sizeof(relid); + strcpy(&key[keysize],exchange), keysize += strlen(exchange) + 1; + strcpy(&key[keysize],name), keysize += strlen(name) + 1; + memcpy(&key[keysize],base,strlen(base)+1), keysize += strlen(base) + 1; + if ( rel != 0 && (len= (int32_t)strlen(rel)) > 0 ) + memcpy(&key[keysize],rel,len+1), keysize += len+1; + return(keysize); +} + +uint64_t InstantDEX_name(char *key,int32_t *keysizep,char *exchange,char *name,char *base,uint64_t *baseidp,char *rel,uint64_t *relidp) +{ + uint64_t baseid,relid,assetbits = 0; char *s,*str; + baseid = *baseidp, relid = *relidp; + //printf(">>>>>> name.(%s) (%s/%s) %llu/%llu\n",name,base,rel,(long long)baseid,(long long)relid); + if ( strcmp(base,"5527630") == 0 || baseid == 5527630 ) + strcpy(base,"NXT"); + if ( strcmp(rel,"5527630") == 0 || relid == 5527630 ) + strcpy(rel,"NXT"); + if ( relid == 0 && rel[0] != 0 ) + { + if ( is_decimalstr(rel) != 0 ) + relid = calc_nxt64bits(rel); + else relid = is_MGWcoin(rel); + } + else if ( (str= is_MGWasset(relid)) != 0 ) + strcpy(rel,str); + if ( baseid == 0 && base[0] != 0 ) + { + if ( is_decimalstr(base) != 0 ) + baseid = calc_nxt64bits(base); + else baseid = is_MGWcoin(base); + } + else if ( (str= is_MGWasset(baseid)) != 0 ) + strcpy(base,str); + if ( strcmp("InstantDEX",exchange) == 0 || strcmp("nxtae",exchange) == 0 || strcmp("unconf",exchange) == 0 || (baseid != 0 && relid != 0) ) + { + if ( strcmp(rel,"NXT") == 0 ) + s = "+", relid = stringbits("NXT"), strcpy(rel,"NXT"); + else if ( strcmp(base,"NXT") == 0 ) + s = "-", baseid = stringbits("NXT"), strcpy(base,"NXT"); + else s = ""; + if ( base[0] == 0 ) + { + get_assetname(base,baseid); + //printf("mapped %llu -> (%s)\n",(long long)baseid,base); + } + if ( rel[0] == 0 ) + { + get_assetname(rel,relid); + //printf("mapped %llu -> (%s)\n",(long long)relid,rel); + } + if ( name[0] == 0 ) + { + if ( relid == NXT_ASSETID ) + sprintf(name,"%llu",(long long)baseid); + else if ( baseid == NXT_ASSETID ) + sprintf(name,"-%llu",(long long)relid); + else sprintf(name,"%llu/%llu",(long long)baseid,(long long)relid); + } + } + else + { + if ( base[0] != 0 && rel[0] != 0 && baseid == 0 && relid == 0 ) + { + baseid = peggy_basebits(base), relid = peggy_basebits(rel); + if ( name[0] == 0 && baseid != 0 && relid != 0 ) + { + strcpy(name,base); // need to be smarter + strcat(name,"/"); + strcat(name,rel); + } + } + if ( name[0] == 0 || baseid == 0 || relid == 0 || base[0] == 0 || rel[0] == 0 ) + { + if ( baseid == 0 && base[0] != 0 ) + baseid = stringbits(base); + else if ( baseid != 0 && base[0] == 0 ) + sprintf(base,"%llu",(long long)baseid); + if ( relid == 0 && rel[0] != 0 ) + { + relid = stringbits(rel); + printf("set relid.%llu <- (%s)\n",(long long)relid,rel); + } + else if ( relid != 0 && rel[0] == 0 ) + sprintf(rel,"%llu",(long long)relid); + if ( name[0] == 0 ) + strcpy(name,base), strcat(name,"/"), strcat(name,rel); + } + } + *baseidp = baseid, *relidp = relid; + *keysizep = prices777_key(key,exchange,name,base,baseid,rel,relid); + //printf("<<<<<<< name.(%s) (%s/%s) %llu/%llu\n",name,base,rel,(long long)baseid,(long long)relid); + return(assetbits); +} + +int32_t create_basketitem(struct prices777_basket *basketitem,cJSON *item,char *refbase,char *refrel,int32_t basketsize) +{ + struct destbuf exchangestr,name,base,rel; char key[512]; uint64_t tmp,baseid,relid; int32_t groupid,keysize,valid; double wt; struct prices777 *prices; + copy_cJSON(&exchangestr,jobj(item,"exchange")); + if ( strcmp("jumblr",exchangestr.buf) == 0 || strcmp("pangea",exchangestr.buf) == 0 || strcmp("wallet",exchangestr.buf) == 0 || exchange_find(exchangestr.buf) == 0 ) + { + printf("create_basketitem: illegal exchange.%s\n",exchangestr.buf); + return(-1); + } + copy_cJSON(&name,jobj(item,"name")); + copy_cJSON(&base,jobj(item,"base")); + copy_cJSON(&rel,jobj(item,"rel")); + if ( (baseid= j64bits(item,"baseid")) != 0 && base.buf[0] == 0 ) + { + _set_assetname(&tmp,base.buf,0,baseid); + //printf("GOT.(%s) <- %llu\n",base.buf,(long long)baseid); + } + else if ( baseid == 0 ) + baseid = stringbits(base.buf); + if ( (relid= j64bits(item,"relid")) != 0 && rel.buf[0] == 0 ) + { + _set_assetname(&tmp,rel.buf,0,relid); + //printf("GOT.(%s) <- %llu\n",rel.buf,(long long)relid); + } + else if ( relid == 0 ) + relid = stringbits(rel.buf); + groupid = juint(item,"group"); + wt = jdouble(item,"wt"); + if ( wt == 0. ) + wt = 1.; + if ( strcmp(refbase,rel.buf) == 0 || strcmp(refrel,base.buf) == 0 ) + { + if ( wt != -1 ) + { + printf("need to flip wt %f for (%s/%s) ref.(%s/%s)\n",wt,base.buf,rel.buf,refbase,refrel); + wt = -1.; + } + } + else if ( wt != 1. ) + { + printf("need to flip wt %f for (%s/%s) ref.(%s/%s)\n",wt,base.buf,rel.buf,refbase,refrel); + wt = 1.; + } + if ( name.buf[0] == 0 ) + sprintf(name.buf,"%s/%s",base.buf,rel.buf); + if ( base.buf[0] == 0 ) + strcpy(base.buf,refbase); + if ( rel.buf[0] == 0 ) + strcpy(rel.buf,refrel); + InstantDEX_name(key,&keysize,exchangestr.buf,name.buf,base.buf,&baseid,rel.buf,&relid); + printf(">>>>>>>>>> create basketitem.(%s) name.(%s) %s (%s/%s) %llu/%llu wt %f\n",jprint(item,0),name.buf,exchangestr.buf,base.buf,rel.buf,(long long)baseid,(long long)relid,wt); + if ( (prices= prices777_initpair(1,exchangestr.buf,base.buf,rel.buf,0.,name.buf,baseid,relid,basketsize)) != 0 ) + { + prices777_addbundle(&valid,0,prices,0,0,0); + basketitem->prices = prices; + basketitem->wt = wt; + basketitem->groupid = groupid; + strcpy(basketitem->base,base.buf); + strcpy(basketitem->rel,rel.buf); + return(0); + } else printf("couldnt create basketitem\n"); + return(-1); +} + +struct prices777 *prices777_makebasket(char *basketstr,cJSON *_basketjson,int32_t addbasket,char *typestr,struct prices777 *ptrs[],int32_t num) +{ + //{"name":"NXT/BTC","base":"NXT","rel":"BTC","basket":[{"exchange":"poloniex"},{"exchange":"btc38"}]} + int32_t i,j,n,keysize,valid,basketsize,total = 0; struct destbuf refname,refbase,refrel; char key[8192]; uint64_t refbaseid=0,refrelid=0; + struct prices777_basket *basketitem,*basket = 0; cJSON *basketjson,*array,*item; struct prices777 *prices = 0; + if ( (basketjson= _basketjson) == 0 && (basketjson= cJSON_Parse(basketstr)) == 0 ) + { + printf("cant parse basketstr.(%s)\n",basketstr); + return(0); + } + copy_cJSON(&refname,jobj(basketjson,"name")); + copy_cJSON(&refbase,jobj(basketjson,"base")); + copy_cJSON(&refrel,jobj(basketjson,"rel")); + refbaseid = j64bits(basketjson,"baseid"); + refrelid = j64bits(basketjson,"relid"); + if ( (array= jarray(&n,basketjson,"basket")) != 0 ) + { + printf("MAKE/(%s) n.%d num.%d\n",jprint(basketjson,0),n,num); + basketsize = (n + num); + basket = calloc(1,sizeof(*basket) * basketsize); + for (i=0; i>>>>>>>>>>> skipped create_basketitem %d of %d of %d\n",i,n,basketsize); + else + { + printf("MAKE.%d: (%s) %p.%s\n",total,jprint(item,0),basket[total].prices,basket[total].prices->exchange); + total++; + } + } + if ( ptrs != 0 && num > 0 ) + { + for (i=0; i 0 ) + { + for (j=0; jprices = ptrs[i]; + if ( strcmp(refbase.buf,ptrs[i]->rel) == 0 || strcmp(refrel.buf,ptrs[i]->base) == 0 ) + basketitem->wt = -1; + else basketitem->wt = 1; + basketitem->groupid = 0; + strcpy(basketitem->base,ptrs[i]->base); + strcpy(basketitem->rel,ptrs[i]->rel); + total++; + printf("extrai.%d/%d total.%d wt.%f (%s/%s).%s\n",i,num,total,basketitem->wt,ptrs[i]->base,ptrs[i]->rel,ptrs[i]->exchange); + } + } + } + printf(">>>>> addbasket.%d (%s/%s).%s %llu %llu\n",addbasket,refbase.buf,refrel.buf,typestr,(long long)refbaseid,(long long)refrelid); + InstantDEX_name(key,&keysize,typestr,refname.buf,refbase.buf,&refbaseid,refrel.buf,&refrelid); + printf(">>>>> addbasket.%d (%s/%s).%s %llu %llu\n",addbasket,refbase.buf,refrel.buf,typestr,(long long)refbaseid,(long long)refrelid); + if ( addbasket != 0 ) + { + prices777_addbundle(&valid,0,0,typestr,refbaseid,refrelid); + printf("<<<<< created.%s valid.%d refname.(%s) (%s/%s).%s %llu %llu\n",typestr,valid,refname.buf,refbase.buf,refrel.buf,typestr,(long long)refbaseid,(long long)refrelid); + } else valid = 0; + if ( valid >= 0 ) + { + if ( (prices= prices777_createbasket(addbasket,refname.buf,refbase.buf,refrel.buf,refbaseid,refrelid,basket,total,typestr)) != 0 ) + { + if ( addbasket != 0 ) + BUNDLE.ptrs[BUNDLE.num] = prices; + prices->lastprice = prices777_basket(prices,MAX_DEPTH); + //printf("C.bsize.%d total polling.%d added.(%s/%s).%s updating basket lastprice %f changed.%p %d groupsize.%d numgroups.%d %p\n",total,BUNDLE.num,prices->base,prices->rel,prices->exchange,prices->lastprice,&prices->changed,prices->changed,prices->basket[0].groupsize,prices->numgroups,&prices->basket[0].groupsize); + BUNDLE.num++; + } + } else prices = 0; + if ( basketjson != _basketjson ) + free_json(basketjson); + free(basket); + } + return(prices); +} + +cJSON *inner_json(double price,double vol,uint32_t timestamp,uint64_t quoteid,uint64_t nxt64bits,uint64_t qty,uint64_t pqt,uint64_t baseamount,uint64_t relamount) +{ + cJSON *inner = cJSON_CreateArray(); + char numstr[64]; + sprintf(numstr,"%.8f",price), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + sprintf(numstr,"%.8f",vol), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + sprintf(numstr,"%llu",(long long)quoteid), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + cJSON_AddItemToArray(inner,cJSON_CreateNumber(timestamp)); + sprintf(numstr,"%llu",(long long)nxt64bits), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + sprintf(numstr,"%llu",(long long)qty), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + sprintf(numstr,"%llu",(long long)pqt), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + sprintf(numstr,"%llu",(long long)baseamount), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + sprintf(numstr,"%llu",(long long)relamount), cJSON_AddItemToArray(inner,cJSON_CreateString(numstr)); + // printf("(%s) ",jprint(inner,0)); + return(inner); +} + +double prices777_NXT(struct prices777 *prices,int32_t maxdepth) +{ + uint32_t timestamp; int32_t flip,i,n; uint64_t baseamount,relamount,qty,pqt; char url[1024],*str,*cmd,*field; + cJSON *json,*bids,*asks,*srcobj,*item,*array; double price,vol,hbla = 0.; + if ( NXT_ASSETID != stringbits("NXT") || (strcmp(prices->rel,"NXT") != 0 && strcmp(prices->rel,"5527630") != 0) ) + { + printf("NXT_ASSETID.%llu != %llu stringbits rel.%s\n",(long long)NXT_ASSETID,(long long)stringbits("NXT"),prices->rel);//, getchar(); + return(0); + } + bids = cJSON_CreateArray(), asks = cJSON_CreateArray(); + for (flip=0; flip<2; flip++) + { + /*{ + "offer": "16959774565785265980", + "expirationHeight": 1000000, + "accountRS": "NXT-QFAF-GR4F-RBSR-AXW2G", + "limit": "9000000", + "currency": "5775213290661997199", + "supply": "0", + "account": "9728792749189838093", + "height": 348856, + "rateNQT": "650" + }*/ + if ( prices->type != 5 ) + { + if ( flip == 0 ) + cmd = "getBidOrders", field = "bidOrders", array = bids; + else cmd = "getAskOrders", field = "askOrders", array = asks; + sprintf(url,"requestType=%s&asset=%llu&limit=%d",cmd,(long long)prices->baseid,maxdepth); + } + else + { + if ( flip == 0 ) + cmd = "getBuyOffers", field = "offers", array = bids; + else cmd = "getSellOffers", field = "offers", array = asks; + sprintf(url,"requestType=%s¤cy=%llu&limit=%d",cmd,(long long)prices->baseid,maxdepth); + } + if ( (str= issue_NXTPOST(url)) != 0 ) + { + //printf("{%s}\n",str); + if ( (json= cJSON_Parse(str)) != 0 ) + { + if ( (srcobj= jarray(&n,json,field)) != 0 ) + { + for (i=0; itype != 5 ) + qty = j64bits(item,"quantityQNT"), pqt = j64bits(item,"priceNQT"); + else qty = j64bits(item,"limit"), pqt = j64bits(item,"rateNQT"); + baseamount = (qty * prices->ap_mult), relamount = (qty * pqt); + price = prices777_price_volume(&vol,baseamount,relamount); + if ( i == 0 ) + { + hbla = (hbla == 0.) ? price : 0.5 * (price + hbla); + if ( flip == 0 ) + prices->lastbid = price; + else prices->lastask = price; + } + //printf("(%llu %llu) %f %f mult.%llu qty.%llu pqt.%llu baseamount.%lld relamount.%lld\n",(long long)prices->baseid,(long long)prices->relid,price,vol,(long long)prices->ap_mult,(long long)qty,(long long)pqt,(long long)baseamount,(long long)relamount); + timestamp = get_blockutime(juint(item,"height")); + item = inner_json(price,vol,timestamp,j64bits(item,prices->type != 5 ? "order" : "offer"),j64bits(item,"account"),qty,pqt,baseamount,relamount); + cJSON_AddItemToArray(array,item); + } + } + free_json(json); + } + free(str); + } else printf("cant get.(%s)\n",url); + } + json = cJSON_CreateObject(); + cJSON_AddItemToObject(json,"bids",bids); + cJSON_AddItemToObject(json,"asks",asks); + if ( Debuglevel > 2 ) + printf("NXTAE.(%s)\n",jprint(json,0)); + prices777_json_orderbook("nxtae",prices,maxdepth,json,0,"bids","asks",0,0); + free_json(json); + return(hbla); +} + +double prices777_unconfNXT(struct prices777 *prices,int32_t maxdepth) +{ + struct destbuf account,txidstr,comment,recipient; char url[1024],*str; uint32_t timestamp; int32_t type,i,subtype,n; + cJSON *json,*bids,*asks,*array,*txobj,*attachment; + double price,vol; uint64_t assetid,accountid,quoteid,baseamount,relamount,qty,priceNQT,amount; + bids = cJSON_CreateArray(), asks = cJSON_CreateArray(); + prices->lastbid = prices->lastask = 0.; + prices->O.numbids = prices->O.numasks = 0; + sprintf(url,"requestType=getUnconfirmedTransactions"); + if ( IGUANA_disableNXT == 0 && (str= issue_NXTPOST(url)) != 0 ) + { + //printf("{%s}\n",str); + if ( (json= cJSON_Parse(str)) != 0 ) + { + if ( (array= jarray(&n,json,"unconfirmedTransactions")) != 0 ) + { + for (i=0; iap_mult), relamount = (qty * priceNQT); + copy_cJSON(&comment,jobj(attachment,"message")); + if ( comment.buf[0] != 0 ) + { + int32_t match_unconfirmed(char *sender,char *hexstr,cJSON *txobj,char *txidstr,char *account,uint64_t amount,uint64_t qty,uint64_t assetid,char *recipient); + //printf("sender.%s -> recv.(%s)\n",account,recipient); + match_unconfirmed(account.buf,comment.buf,txobj,txidstr.buf,account.buf,amount,qty,assetid,recipient.buf); + } + quoteid = calc_nxt64bits(txidstr.buf); + price = prices777_price_volume(&vol,baseamount,relamount); + if ( prices->baseid == assetid ) + { + if ( Debuglevel > 2 ) + printf("unconf.%d subtype.%d %s %llu (%llu %llu) %f %f mult.%llu qty.%llu pqt.%llu baseamount.%lld relamount.%lld\n",i,subtype,txidstr.buf,(long long)prices->baseid,(long long)assetid,(long long)NXT_ASSETID,price,vol,(long long)prices->ap_mult,(long long)qty,(long long)priceNQT,(long long)baseamount,(long long)relamount); + if ( subtype == 2 ) + { + array = bids; + prices->lastbid = price; + } + else if ( subtype == 3 ) + { + array = asks; + prices->lastask = price; + } + cJSON_AddItemToArray(array,inner_json(price,vol,timestamp,quoteid,accountid,qty,priceNQT,baseamount,relamount)); + } + } + } + free_json(json); + } + free(str); + } else printf("cant get.(%s)\n",url); + } + json = cJSON_CreateObject(); + cJSON_AddItemToObject(json,"bids",bids); + cJSON_AddItemToObject(json,"asks",asks); + prices777_json_orderbook("unconf",prices,maxdepth,json,0,"bids","asks",0,0); + if ( Debuglevel > 2 )//|| prices->O.numbids != 0 || prices->O.numasks != 0 ) + printf("%s %s/%s unconf.(%s) %f %f (%d %d)\n",prices->contract,prices->base,prices->rel,jprint(json,0),prices->lastbid,prices->lastask,prices->O.numbids,prices->O.numasks); + free_json(json); + return(_pairaved(prices->lastbid,prices->lastask)); +} + +double prices777_InstantDEX(struct prices777 *prices,int32_t maxdepth) +{ + cJSON *json; double hbla = 0.; + if ( (json= InstantDEX_orderbook(prices)) != 0 ) // strcmp(prices->exchange,INSTANTDEX_NAME) == 0 && + { + if ( Debuglevel > 2 ) + printf("InstantDEX.(%s)\n",jprint(json,0)); + prices777_json_orderbook("InstantDEX",prices,maxdepth,json,0,"bids","asks",0,0); + free_json(json); + } + return(hbla); +} + +#define BASE_ISNXT 1 +#define BASE_ISASSET 2 +#define BASE_ISNAME 4 +#define BASE_ISMGW 8 +#define BASE_EXCHANGEASSET 16 + +int32_t calc_baseflags(char *exchange,char *base,uint64_t *baseidp) +{ + char assetidstr[64],tmpstr[64],*str; uint64_t tmp; int32_t flags = 0; + exchange[0] = 0; + printf("calc_baseflags.(%s/%llu) ",base,(long long)*baseidp); + if ( strcmp(base,"NXT") == 0 || *baseidp == NXT_ASSETID ) + strcpy(base,"NXT"), *baseidp = NXT_ASSETID, flags |= BASE_ISNXT; + else + { + if ( *baseidp == 0 ) + { + if ( is_decimalstr(base) != 0 ) + { + *baseidp = calc_nxt64bits(base), flags |= BASE_ISASSET; + unstringbits(tmpstr,*baseidp); + if ( (tmp= is_MGWcoin(tmpstr)) != 0 ) + *baseidp = tmp, flags |= (BASE_EXCHANGEASSET | BASE_ISMGW); + else + { + printf("set base.(%s) -> %llu\n",base,(long long)*baseidp); + if ( (str= is_MGWasset(*baseidp)) != 0 ) + strcpy(base,str), flags |= (BASE_EXCHANGEASSET | BASE_ISMGW); + } + } + else + { + *baseidp = stringbits(base), flags |= BASE_ISNAME; + printf("stringbits.(%s) -> %llu\n",base,(long long)*baseidp); + } + } + else + { + if ( (str= is_MGWasset(*baseidp)) != 0 ) + { + printf("is MGWasset.(%s)\n",str); + strcpy(base,str), flags |= (BASE_EXCHANGEASSET | BASE_ISMGW | BASE_ISASSET); + } + else + { + expand_nxt64bits(assetidstr,*baseidp); + if ( (str= is_tradedasset(exchange,assetidstr)) != 0 ) + { + strcpy(base,str), flags |= (BASE_EXCHANGEASSET | BASE_ISASSET); + printf("%s is tradedasset at (%s) %llu\n",assetidstr,str,(long long)*baseidp); + } + else + { + unstringbits(tmpstr,*baseidp); + if ( (tmp= is_MGWcoin(tmpstr)) != 0 ) + strcpy(base,tmpstr), *baseidp = tmp, flags |= (BASE_EXCHANGEASSET | BASE_ISMGW | BASE_ISASSET); + else + { + _set_assetname(&tmp,base,0,*baseidp), flags |= BASE_ISASSET; + printf("_set_assetname.(%s) from %llu\n",base,(long long)*baseidp); + } + } + } + } + if ( (flags & (BASE_ISASSET|BASE_EXCHANGEASSET|BASE_ISMGW)) == 0 ) + *baseidp = stringbits(base); + } + printf("-> flags.%d (%s %llu) %s\n",flags,base,(long long)*baseidp,exchange); + return(flags); +} + +void setitemjson(cJSON *item,char *name,char *base,uint64_t baseid,char *rel,uint64_t relid) +{ + char numstr[64]; + jaddstr(item,"name",name), jaddstr(item,"base",base), jaddstr(item,"rel",rel); + sprintf(numstr,"%llu",(long long)baseid), jaddstr(item,"baseid",numstr); + sprintf(numstr,"%llu",(long long)relid), jaddstr(item,"relid",numstr); +} + +int32_t nxt_basketjson(cJSON *array,int32_t groupid,int32_t polarity,char *base,uint64_t baseid,char *rel,uint64_t relid,char *refbase,char *refrel) +{ + cJSON *item,*item2,*item3; char name[64]; int32_t dir = 0; + item = cJSON_CreateObject(), jaddstr(item,"exchange",INSTANTDEX_NXTAENAME); + item2 = cJSON_CreateObject(), jaddstr(item2,"exchange",INSTANTDEX_NXTAEUNCONF); + item3 = cJSON_CreateObject(), jaddstr(item3,"exchange",INSTANTDEX_NAME); + if ( strcmp(base,"NXT") == 0 ) + { + sprintf(name,"%s/%s",rel,"NXT"); + setitemjson(item,name,rel,relid,"NXT",NXT_ASSETID); + setitemjson(item2,name,rel,relid,"NXT",NXT_ASSETID); + setitemjson(item3,name,rel,relid,"NXT",NXT_ASSETID); + } + else if ( strcmp(rel,"NXT") == 0 ) + { + sprintf(name,"%s/%s",base,"NXT"); + setitemjson(item,name,base,baseid,"NXT",NXT_ASSETID); + setitemjson(item2,name,base,baseid,"NXT",NXT_ASSETID); + setitemjson(item3,name,base,baseid,"NXT",NXT_ASSETID); + } + else + { + free_json(item); + free_json(item2); + free_json(item3); + return(0); + } + if ( strcmp(refbase,rel) == 0 || strcmp(refrel,base) == 0 ) + dir = -1; + else dir = 1; + jaddnum(item,"wt",dir), jaddnum(item2,"wt",dir), jaddnum(item3,"wt",dir); + jaddnum(item,"group",groupid), jaddnum(item2,"group",groupid), jaddnum(item3,"group",groupid); + printf("nxt_basketjson (%s/%s) %llu/%llu ref.(%s/%s) dir.%d polarity.%d\n",base,rel,(long long)baseid,(long long)relid,refbase,refrel,dir,polarity); + jaddi(array,item), jaddi(array,item2), jaddi(array,item3); + return(dir * polarity); +} + +void add_nxtbtc(cJSON *array,int32_t groupid,double wt) +{ + char *btcnxt_xchgs[] = { "poloniex", "bittrex", "btc38" }; + int32_t i; cJSON *item; + if ( wt != 0 ) + { + printf("add NXT/BTC\n"); + for (i=0; i 0 && centralexchange_items(0,1,0,rel,coinstr,tradeable,base,rel) > 0 ) + { + array = cJSON_CreateArray(); + printf("add central jumper.(%s) for (%s/%s)\n",coinstr,base,rel); + centralexchange_items(0,1,array,base,coinstr,tradeable,base,rel); + centralexchange_items(1,-1,array,rel,coinstr,tradeable,base,rel); + } + } + return(array); +} + +int32_t make_subactive(struct prices777 *baskets[],int32_t n,cJSON *array,char *prefix,char *base,char *rel,uint64_t baseid,uint64_t relid,int32_t maxdepth) +{ + char tmpstr[64],typestr[64]; struct prices777 *basket; cJSON *basketjson; + basketjson = cJSON_CreateObject(); + sprintf(tmpstr,"%s/%s",base,rel); + setitemjson(basketjson,tmpstr,base,baseid,rel,relid); + jadd(basketjson,"basket",array); + printf("%s BASKETMAKE.(%s)\n",prefix,jprint(basketjson,0)); + sprintf(typestr,"basket%s",prefix); + if ( (basket= prices777_makebasket(0,basketjson,1,typestr,0,0)) != 0 ) + { + prices777_basket(basket,maxdepth); + prices777_jsonstrs(basket,&basket->O); + printf("add to baskets[%d].%s (%s/%s) (%s)\n",n,basket->exchange,basket->base,basket->rel,basket->contract); + baskets[n++] = basket; + } + free_json(basketjson); + return(n); +} + +char *prices777_activebooks(char *name,char *_base,char *_rel,uint64_t baseid,uint64_t relid,int32_t maxdepth,int32_t allflag,int32_t tradeable) +{ + cJSON *array,*arrayNXT,*arrayBTC,*arrayUSD,*arrayCNY,*basketjson; struct prices777 *active,*basket,*baskets[64]; + int32_t inverted,keysize,baseflags,relflags,n = 0; char tmpstr[64],base[64],rel[64],bexchange[64],rexchange[64],key[512],*retstr = 0; + memset(baskets,0,sizeof(baskets)); + strcpy(base,_base), strcpy(rel,_rel); + baseflags = calc_baseflags(bexchange,base,&baseid); + relflags = calc_baseflags(rexchange,rel,&relid); + InstantDEX_name(key,&keysize,"active",name,base,&baseid,rel,&relid); + printf("activebooks (%s/%s) (%llu/%llu)\n",base,rel,(long long)baseid,(long long)relid); + if ( (active= prices777_find(&inverted,baseid,relid,"active")) == 0 ) + { + if ( ((baseflags & BASE_ISMGW) != 0 || (baseflags & BASE_ISASSET) == 0) && ((relflags & BASE_ISMGW) != 0 || (relflags & BASE_ISASSET) == 0) ) + { + if ( (arrayUSD= external_combo(base,rel,"USD",tradeable)) != 0 ) + n = make_subactive(baskets,n,arrayUSD,"USD",base,rel,baseid,relid,maxdepth); + if ( (arrayCNY= external_combo(base,rel,"CNY",tradeable)) != 0 ) + n = make_subactive(baskets,n,arrayCNY,"CNY",base,rel,baseid,relid,maxdepth); + } + arrayBTC = external_combo(base,rel,"BTC",tradeable); + basketjson = cJSON_CreateObject(), array = cJSON_CreateArray(); + sprintf(tmpstr,"%s/%s",base,rel); + setitemjson(basketjson,tmpstr,base,baseid,rel,relid); + //if ( baseflags != BASE_ISASSET && relflags != BASE_ISASSET ) + centralexchange_items(0,1,array,base,rel,tradeable,base,rel); + if ( (arrayNXT= make_arrayNXT(array,&arrayBTC,base,rel,baseid,relid)) != 0 ) + n = make_subactive(baskets,n,arrayNXT,"NXT",base,rel,baseid,relid,maxdepth); + if ( arrayBTC != 0 ) + n = make_subactive(baskets,n,arrayBTC,"BTC",base,rel,baseid,relid,maxdepth); + if ( (basket= prices777_find(&inverted,baseid,relid,"basket")) != 0 ) + baskets[n++] = basket; + jadd(basketjson,"basket",array); + printf(" ACTIVE MAKE.(%s)\n",jprint(basketjson,0)); + if ( (active= prices777_makebasket(0,basketjson,1,"active",baskets,n)) != 0 ) + { + prices777_basket(active,maxdepth); + prices777_jsonstrs(active,&active->O); + } + free_json(array); + } + if ( active != 0 && retstr == 0 ) + { + prices777_basket(active,maxdepth); + prices777_jsonstrs(active,&active->O); + if ( (retstr= active->orderbook_jsonstrs[inverted][allflag]) != 0 ) + retstr = clonestr(retstr); + } + if ( retstr == 0 ) + retstr = clonestr("{\"error\":\"null active orderbook\"}"); + return(retstr); +} + +cJSON *basketitem_json(struct prices777 *prices) +{ + int32_t i; char numstr[64]; cJSON *json,*array,*item; + json = cJSON_CreateObject(); + if ( prices != 0 ) + { + jaddstr(json,"exchange",prices->exchange); + jaddstr(json,"name",prices->contract); + jaddstr(json,"base",prices->base); + sprintf(numstr,"%llu",(long long)prices->baseid), jaddstr(json,"baseid",numstr); + jaddstr(json,"rel",prices->rel); + sprintf(numstr,"%llu",(long long)prices->relid), jaddstr(json,"relid",numstr); + jaddnum(json,"commission",prices->commission); + if ( prices->basketsize != 0 ) + { + array = cJSON_CreateArray(); + for (i=0; ibasketsize; i++) + { + item = basketitem_json(prices->basket[i].prices); + if ( prices->basket[i].groupsize != 0 ) + jaddnum(item,"groupsize",prices->basket[i].groupsize); + jaddnum(item,"group",prices->basket[i].groupid); + jaddnum(item,"wt",prices->basket[i].wt); + jaddi(array,item); + } + jadd(json,"basket",array); + } + } + return(json); +} + +char *prices777_allorderbooks() +{ + int32_t i; cJSON *json,*array = cJSON_CreateArray(); + for (i=0; iissue = funcs[i]; + } + } + return(0); + } + //printf("init.(%s/%s) name.(%s) %llu %llu\n",base,rel,name,(long long)baseid,(long long)relid); + if ( strcmp(exchange,"nxtae") == 0 || strcmp(exchange,"unconf") == 0 )//|| strcmp(exchange,"InstantDEX") == 0 ) + { + if ( strcmp(base,"NXT") == 0 || baseid == NXT_ASSETID ) + { + strcpy(base,rel), baseid = relid; + strcpy(rel,"NXT"), relid = NXT_ASSETID; + printf("flip.(%s/%s) %llu %llu\n",base,rel,(long long)baseid,(long long)relid); + } + } + for (i=0; iexchange,exchange) == 0 ) + { + if ( baseid != 0 && relid != 0 && BUNDLE.ptrs[i]->baseid == baseid && BUNDLE.ptrs[i]->relid == relid ) + return(BUNDLE.ptrs[i]); + if ( strcmp(BUNDLE.ptrs[i]->origbase,base) == 0 && strcmp(BUNDLE.ptrs[i]->origrel,rel) == 0 ) + return(BUNDLE.ptrs[i]); + } + } + printf("cant find (%s) (%llu) (%llu) (%s) (%s)\n",exchange,(long long)baseid,(long long)relid,base,rel); + prices = calloc(1,sizeof(*prices) + basketsize*sizeof(*prices->basket)); + // printf("new prices %ld\n",sizeof(*prices)); + strcpy(prices->exchange,exchange), strcpy(prices->contract,name), strcpy(prices->base,base), strcpy(prices->rel,rel); + prices->baseid = baseid, prices->relid = relid; + prices->contractnum = InstantDEX_name(prices->key,&prices->keysize,exchange,prices->contract,prices->base,&prices->baseid,prices->rel,&prices->relid); + portable_mutex_init(&prices->mutex); + strcpy(prices->origbase,base); + if ( rel[0] != 0 ) + strcpy(prices->origrel,rel); + allocated += sizeof(*prices); + safecopy(prices->exchange,exchange,sizeof(prices->exchange)); + if ( strcmp(exchange,"nxtae") == 0 || strcmp(exchange,"unconf") == 0 || strcmp(exchange,INSTANTDEX_NAME) == 0 ) + { + char tmp[16]; + _set_assetname(&prices->basemult,tmp,0,prices->baseid); + _set_assetname(&prices->relmult,tmp,0,prices->relid); + if ( (prices->relid != NXT_ASSETID && prices->relid < (1LL << (5*8))) || (prices->baseid != NXT_ASSETID && prices->baseid == (1LL << (5*8))) ) + { + printf("illegal baseid.%llu or relid.%llu\n",(long long)prices->baseid,(long long)prices->relid); + free(prices); + return(0); + } + //prices->nxtbooks = calloc(1,sizeof(*prices->nxtbooks)); + safecopy(prices->lbase,base,sizeof(prices->lbase)), tolowercase(prices->lbase); + safecopy(prices->lrel,rel,sizeof(prices->lrel)), tolowercase(prices->lrel); + rellen = (int32_t)(strlen(prices->rel) + 1); + tmp[0] = 0; + prices->type = _set_assetname(&prices->ap_mult,tmp,0,prices->baseid); + printf("nxtbook.(%s) -> NXT %s %llu/%llu vs (%s) mult.%llu (%llu/%llu)\n",base,prices->contract,(long long)prices->baseid,(long long)prices->relid,tmp,(long long)prices->ap_mult,(long long)prices->basemult,(long long)prices->relmult); + } + else + { + prices->basemult = prices->relmult = 1; + safecopy(prices->base,base,sizeof(prices->base)), touppercase(prices->base); + safecopy(prices->lbase,base,sizeof(prices->lbase)), tolowercase(prices->lbase); + if ( rel[0] == 0 && prices777_ispair(basebuf,relbuf,base) >= 0 ) + { + strcpy(base,basebuf), strcpy(rel,relbuf); + //printf("(%s) is a pair (%s)+(%s)\n",base,basebuf,relbuf); + } + if ( rel[0] != 0 ) + { + rellen = (int32_t)(strlen(rel) + 1); + safecopy(prices->rel,rel,sizeof(prices->rel)), touppercase(prices->rel); + safecopy(prices->lrel,rel,sizeof(prices->lrel)), tolowercase(prices->lrel); + if ( prices->contract[0] == 0 ) + { + strcpy(prices->contract,prices->base); + if ( strcmp(prices->rel,&prices->contract[strlen(prices->contract)-3]) != 0 ) + strcat(prices->contract,"/"), strcat(prices->contract,prices->rel); + } + //printf("create base.(%s) rel.(%s)\n",prices->base,prices->rel); + } + else + { + if ( prices->contract[0] == 0 ) + strcpy(prices->contract,base); + } + } + char str[65]; printf("%s init_pair.(%s) (%s)(%s).%llu -> (%s) keysize.%d crc.%u (baseid.%llu relid.%llu)\n",mbstr(str,allocated),exchange,base,rel,(long long)prices->contractnum,prices->contract,prices->keysize,calc_crc32(0,(void *)prices->key,prices->keysize),(long long)prices->baseid,(long long)prices->relid); + prices->decay = decay, prices->oppodecay = (1. - decay); + prices->RTflag = 1; + if ( (exchangeptr= find_exchange(0,exchange)) != 0 ) + { + if ( prices->commission == 0. ) + prices->commission = exchangeptr->commission; + prices->exchangeid = exchangeptr->exchangeid; + if ( exchangeptr->issue.update == 0 ) + { + for (i=0; iissue = funcs[i]; + //printf("return prices.%p\n",prices); + } + } + } + if ( exchangeptr->refcount == 0 ) + { + printf("incr refcount.%s from %d\n",exchangeptr->name,exchangeptr->refcount); + exchangeptr->refcount++; + } + return(prices); + } + //printf("initialized.(%s).%lld\n",prices->contract,(long long)prices->contractnum); + return(prices); +} + +int32_t is_pair(char *base,char *rel,char *refbase,char *refrel) +{ + if ( strcmp(base,refbase) == 0 && strcmp(rel,refrel) == 0 ) + return(1); + else if ( strcmp(rel,refbase) == 0 && strcmp(base,refrel) == 0 ) + return(-1); + return(0); +} + +struct prices777 *prices777_poll(char *_exchangestr,char *_name,char *_base,uint64_t refbaseid,char *_rel,uint64_t refrelid) +{ + char exchangestr[64],base[64],rel[64],name[64],key[1024]; uint64_t baseid,relid; + int32_t keysize,exchangeid,valid; struct exchange_info *exchange; struct prices777 *prices; + baseid = refbaseid, relid = refrelid; + strcpy(exchangestr,_exchangestr), strcpy(base,_base), strcpy(rel,_rel), strcpy(name,_name); + if ( (strcmp(exchangestr,"huobi") == 0 && is_pair(base,rel,"BTC","CNY") == 0 && is_pair(base,rel,"LTC","CNY") == 0) || + ((strcmp(exchangestr,"bityes") == 0 || strcmp(exchangestr,"okcoin") == 0) && is_pair(base,rel,"BTC","USD") == 0 && is_pair(base,rel,"LTC","USD") == 0) || + ((strcmp(exchangestr,"bitstamp") == 0 || strcmp(exchangestr,"coinbase") == 0) && is_pair(base,rel,"BTC","USD") == 0) || + (strcmp(exchangestr,"lakebtc") == 0 && is_pair(base,rel,"BTC","CNY") == 0 && is_pair(base,rel,"BTC","USD") == 0) || + (strcmp(exchangestr,"quadriga") == 0 && is_pair(base,rel,"BTC","CAD") == 0 && is_pair(base,rel,"BTC","USD") == 0) || + 0 ) + { + printf("%s (%s/%s) is not a supported trading pair\n",exchangestr,base,rel); + return(0); + } + InstantDEX_name(key,&keysize,exchangestr,name,base,&baseid,rel,&relid); + //printf("call addbundle\n"); + if ( (prices= prices777_addbundle(&valid,0,0,exchangestr,baseid,relid)) != 0 ) + { + printf("found (%s/%s).%s %llu %llu in slot-> %p\n",base,rel,exchangestr,(long long)baseid,(long long)relid,prices); + return(prices); + } + //printf("call find_exchange\n"); + if ( (exchange= find_exchange(&exchangeid,exchangestr)) == 0 ) + { + printf("cant add exchange.(%s)\n",exchangestr); + return(0); + } + if ( strcmp(exchangestr,"nxtae") == 0 || strcmp(exchangestr,"unconf") == 0 ) + { + if ( strcmp(base,"NXT") != 0 && strcmp(rel,"NXT") != 0 ) + { + printf("nxtae/unconf needs to be relative to NXT (%s/%s) %llu/%llu\n",base,rel,(long long)baseid,(long long)relid); + return(0); + } + } + if ( (prices= prices777_initpair(1,exchangestr,base,rel,0.,name,baseid,relid,0)) != 0 ) + { + //printf("call addbundle after initpair\n"); + prices777_addbundle(&valid,1,prices,0,0,0); + } + return(prices); +} + +int32_t prices777_propagate(struct prices777 *prices) +{ + int32_t i,n = 0; + for (i=0; inumdependents; i++) + { + n++; + if ( (*prices->dependents[i]) < 0xff ) + (*prices->dependents[i])++; + if ( Debuglevel > 2 ) + printf("numdependents.%d of %d %p %d\n",i,prices->numdependents,prices->dependents[i],*prices->dependents[i]); + } + return(n); +} + +int32_t prices777_updated; +void prices777_basketsloop(void *ptr) +{ + extern int32_t prices777_NXTBLOCK; + int32_t i,n; uint32_t updated; struct prices777 *prices; + while ( 1 ) + { + for (i=n=0; idisabled == 0 && prices->basketsize != 0 ) + { + if ( prices->changed != 0 ) + { + if ( Debuglevel > 2 ) + printf("%s updating basket(%s) lastprice %.8f changed.%p %d\n",prices->exchange,prices->contract,prices->lastprice,&prices->changed,prices->changed); + prices->pollnxtblock = prices777_NXTBLOCK; + n++; + prices->lastupdate = updated; + if ( (prices->lastprice= prices777_basket(prices,MAX_DEPTH)) != 0. ) + { + if ( prices->O.numbids > 0 || prices->O.numasks > 0 ) + { + prices777_jsonstrs(prices,&prices->O); + prices777_updated += prices777_propagate(prices); + } + } + prices->changed = 0; + } + } + } + if ( n == 0 ) + usleep(250000); + else usleep(10000); + } +} + +void prices777_exchangeloop(void *ptr) +{ + extern int32_t prices777_NXTBLOCK; + struct prices777 *prices; int32_t i,n,pollflag,isnxtae = 0; double updated = 0.; struct exchange_info *exchange = ptr; + if ( strcmp(exchange->name,"nxtae") == 0 || strcmp(exchange->name,"unconf") == 0 ) + isnxtae = 1; + printf("POLL.(%s)\n",exchange->name); + while ( 1 ) + { + for (i=n=0; idisabled == 0 && prices->basketsize == 0 && prices->exchangeid == exchange->exchangeid ) + { + if ( prices->exchangeid == INSTANTDEX_EXCHANGEID && prices->dirty != 0 ) + pollflag = 1; + else if ( isnxtae == 0 ) + pollflag = milliseconds() > (exchange->lastupdate + exchange->pollgap*1000) && milliseconds() > (prices->lastupdate + 1000*IGUANA_EXCHANGEIDLE); + else if ( (strcmp(exchange->name,"unconf") == 0 && milliseconds() > prices->lastupdate + 5000) || prices->pollnxtblock < prices777_NXTBLOCK || milliseconds() > prices->lastupdate + 1000*IGUANA_EXCHANGEIDLE ) + pollflag = 1; + else continue; + //printf("(%s) pollflag.%d %p\n",exchange->name,pollflag,exchange->issue.update); + if ( pollflag != 0 && exchange->issue.update != 0 ) + { + portable_mutex_lock(&exchange->mutex); + prices->lastprice = (*exchange->issue.update)(prices,MAX_DEPTH); + portable_mutex_unlock(&exchange->mutex); + updated = exchange->lastupdate = milliseconds(), prices->lastupdate = milliseconds(); + if ( prices->lastprice != 0. ) + { + if ( Debuglevel > 2 && strcmp(exchange->name,"unconf") != 0 ) + printf("%-8s %8s (%8s %8s) %llu %llu isnxtae.%d poll %u -> %u %.8f hbla %.8f %.8f\n",prices->exchange,prices->contract,prices->base,prices->rel,(long long)prices->baseid,(long long)prices->relid,isnxtae,prices->pollnxtblock,prices777_NXTBLOCK,prices->lastprice,prices->lastbid,prices->lastask); + prices777_propagate(prices); + } + prices->pollnxtblock = prices777_NXTBLOCK; + prices->dirty = 0; + n++; + } + /*if ( 0 && exchange->issue.trade != 0 && exchange->apikey[0] != 0 && exchange->exchangeid >= FIRST_EXTERNAL && time(NULL) > exchange->lastbalancetime+300 ) + { + if ( (json= (*exchange->issue.balances)(exchange)) != 0 ) + { + if ( exchange->balancejson != 0 ) + free_json(exchange->balancejson); + exchange->balancejson = json; + } + exchange->lastbalancetime = (uint32_t)time(NULL); + }*/ + } + } + if ( n == 0 ) + sleep(3); + else sleep(1); + } +} + +int32_t prices777_init(char *jsonstr,int32_t peggyflag) +{ + static int32_t didinit; + char *btcdexchanges[] = { "poloniex", "bittrex" };//, "bter" }; + char *btcusdexchanges[] = { "bityes", "bitfinex", "bitstamp", "okcoin", "coinbase", "btce", "lakebtc", "kraken" }; + cJSON *json=0,*item,*exchanges; int32_t i,n; char *exchange,*base,*rel,*contract; struct exchange_info *exchangeptr=0; struct destbuf tmp; + if ( didinit != 0 ) + return(0); + didinit = 1; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"unconf","BTC","NXT",0,"BTC/NXT",calc_nxt64bits("12659653638116877017"),NXT_ASSETID,0)) != 0 ) + BUNDLE.num++; + if ( peggyflag != 0 ) + { + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"huobi","BTC","USD",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"btc38","CNY","NXT",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"okcoin","LTC","BTC",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"poloniex","LTC","BTC",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"poloniex","XMR","BTC",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"poloniex","BTS","BTC",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + if ( (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,"poloniex","XCP","BTC",0.,0,0,0,0)) != 0 ) + BUNDLE.num++; + for (i=0; ipollgap = get_API_int(cJSON_GetObjectItem(item,"pollgap"),IGUANA_EXCHANGEIDLE); + extract_cJSON_str(exchangeptr->apikey,sizeof(exchangeptr->apikey),item,"key"); + if ( exchangeptr->apikey[0] == 0 ) + extract_cJSON_str(exchangeptr->apikey,sizeof(exchangeptr->apikey),item,"apikey"); + extract_cJSON_str(exchangeptr->userid,sizeof(exchangeptr->userid),item,"userid"); + extract_cJSON_str(exchangeptr->apisecret,sizeof(exchangeptr->apisecret),item,"secret"); + if ( exchangeptr->apisecret[0] == 0 ) + extract_cJSON_str(exchangeptr->apisecret,sizeof(exchangeptr->apisecret),item,"apisecret"); + if ( exchangeptr->commission == 0. ) + exchangeptr->commission = jdouble(item,"commission"); + printf("%p ADDEXCHANGE.(%s) [%s, %s, %s] commission %.3f%%\n",exchangeptr,exchange,exchangeptr->apikey,exchangeptr->userid,exchangeptr->apisecret,exchangeptr->commission * 100); + } else printf(" exchangeptr.%p for (%p)\n",exchangeptr,exchange); + if ( exchange != 0 && strcmp(exchange,"truefx") == 0 ) + { + copy_cJSON(&tmp,jobj(item,"truefxuser")), safecopy(BUNDLE.truefxuser,tmp.buf,sizeof(BUNDLE.truefxuser)); + copy_cJSON(&tmp,jobj(item,"truefxpass")), safecopy(BUNDLE.truefxpass,tmp.buf,sizeof(BUNDLE.truefxpass));; + printf("truefx.(%s %s)\n",BUNDLE.truefxuser,BUNDLE.truefxpass); + } + else if ( base != 0 && rel != 0 && base[0] != 0 && rel[0] != 0 && (BUNDLE.ptrs[BUNDLE.num]= prices777_initpair(1,exchange,base,rel,jdouble(item,"decay"),contract,stringbits(base),stringbits(rel),0)) != 0 ) + { + if ( exchangeptr != 0 && (BUNDLE.ptrs[BUNDLE.num]->commission= jdouble(item,"commission")) == 0. ) + BUNDLE.ptrs[BUNDLE.num]->commission = exchangeptr->commission; + printf("SET COMMISSION.%s %f for %s/%s\n",exchange,exchangeptr!=0?exchangeptr->commission:0,base,rel); + BUNDLE.num++; + } + } + } else printf("(%s) has no prices[]\n",jsonstr); + if ( json != 0 ) + free_json(json); + for (i=0; irefcount > 0 || strcmp(exchangeptr->name,"unconf") == 0) )//&& strcmp(exchangeptr->name,"pangea") != 0 && strcmp(exchangeptr->name,"jumblr") != 0 ) + iguana_launch(iguana_coinadd("BTCD"),"exchangeloop",(void *)prices777_exchangeloop,exchangeptr,IGUANA_EXCHANGETHREAD); + } + return(0); +} + +double prices777_yahoo(char *metal) +{ + // http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json + // http://finance.yahoo.com/webservice/v1/symbols/XAU=X/quote?format=json + // http://finance.yahoo.com/webservice/v1/symbols/XAG=X/quote?format=json + // http://finance.yahoo.com/webservice/v1/symbols/XPT=X/quote?format=json + // http://finance.yahoo.com/webservice/v1/symbols/XPD=X/quote?format=json + char url[1024],*jsonstr; cJSON *json,*obj,*robj,*item,*field; double price = 0.; + sprintf(url,"http://finance.yahoo.com/webservice/v1/symbols/%s=X/quote?format=json",metal); + if ( (jsonstr= issue_curl(url)) != 0 ) + { + //printf("(%s)\n",jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (obj= jobj(json,"list")) != 0 && (robj= jobj(obj,"resources")) != 0 && (item= jitem(robj,0)) != 0 ) + { + if ( (robj= jobj(item,"resource")) != 0 && (field= jobj(robj,"fields")) != 0 && (price= jdouble(field,"price")) != 0 ) + price = 1. / price; + } + free_json(json); + } + free(jsonstr); + } + if ( Debuglevel > 2 ) + printf("(%s %f) ",metal,price); + return(price); +} + +cJSON *url_json(char *url) +{ + char *jsonstr; cJSON *json = 0; + if ( (jsonstr= issue_curl(url)) != 0 ) + { + //printf("(%s) -> (%s)\n",url,jsonstr); + json = cJSON_Parse(jsonstr); + free(jsonstr); + } + return(json); +} + +cJSON *url_json2(char *url) +{ + char *jsonstr; cJSON *json = 0; + if ( (jsonstr= issue_curl(url)) != 0 ) + { + //printf("(%s) -> (%s)\n",url,jsonstr); + json = cJSON_Parse(jsonstr); + free(jsonstr); + } + return(json); +} + +void prices777_btcprices(int32_t enddatenum,int32_t numdates) +{ + int32_t i,n,year,month,day,seconds,datenum; char url[1024],date[64],*dstr,*str; uint32_t timestamp,utc32[MAX_SPLINES]; + cJSON *coindesk,*quandl,*btcdhist,*bpi,*array,*item; + double btcddaily[MAX_SPLINES],cdaily[MAX_SPLINES],qdaily[MAX_SPLINES],ask,high,low,bid,close,vol,quotevol,open,price = 0.; + coindesk = url_json("http://api.coindesk.com/v1/bpi/historical/close.json"); + sprintf(url,"https://poloniex.com/public?command=returnChartData¤cyPair=BTC_BTCD&start=%ld&end=9999999999&period=86400",(long)(time(NULL)-numdates*3600*24)); + if ( (bpi= jobj(coindesk,"bpi")) != 0 ) + { + datenum = enddatenum; + memset(utc32,0,sizeof(utc32)); + memset(cdaily,0,sizeof(cdaily)); + if ( datenum == 0 ) + { + datenum = OS_conv_unixtime(&seconds,(uint32_t)time(NULL)); + printf("got datenum.%d %d %d %d\n",datenum,seconds/3600,(seconds/60)%24,seconds%60); + } + for (i=0; i 0 && (array= jarray(&n,quandl,"data")) != 0 ) + { + printf("datenum.%d data.%d %d\n",datenum,n,cJSON_GetArraySize(array)); + memset(utc32,0,sizeof(utc32)), memset(qdaily,0,sizeof(qdaily)); + for (i=0; i 2 ) + printf("(%s) ",cJSON_Print(item)); + if ( (dstr= jstr(jitem(item,0),0)) != 0 && (datenum= conv_date(&seconds,dstr)) > 0 ) + { + price = jdouble(jitem(item,1),0), ask = jdouble(jitem(item,2),0), bid = jdouble(jitem(item,3),0); + close = jdouble(jitem(item,4),0), vol = jdouble(jitem(item,5),0); + if ( Debuglevel > 2 ) + fprintf(stderr,"%d.[%d %f %f %f %f %f].%d ",i,datenum,price,ask,bid,close,vol,n); + utc32[numdates - 1 - i] = OS_conv_datenum(datenum,12,0,0), qdaily[numdates - 1 - i] = price * .001; + } + } + prices777_genspline(&BUNDLE.splines[MAX_CURRENCIES+1],MAX_CURRENCIES+1,"quandl",utc32,qdaily,n 2 ) + printf("[%u %d %f]",timestamp,OS_conv_unixtime(&seconds,timestamp),price); + utc32[i] = timestamp - 12*3600, btcddaily[i] = price * 100.; + } + if ( Debuglevel > 2 ) + printf("poloniex.%d\n",n); + prices777_genspline(&BUNDLE.splines[MAX_CURRENCIES+2],MAX_CURRENCIES+2,"btcdhist",utc32,btcddaily,n price ) + // printf("base.%d rel.%d price2 %f vs %f\n",basenum,relnum,1/price2,price); + } + } + if ( iter == 0 ) + sum += 1., vsum += 1.; + if ( nonz != 0 ) + sum /= nonz; + if ( vnum != 0 ) + vsum /= vnum; + if ( iter == 0 ) + basevals[basenum] = (sum + 1./vsum) / 2.; + else errsum += (sum + vsum)/2, numerrs++;//, printf("(%.8f %.8f) ",sum,vsum); + //printf("date.%d (%.8f/%d %.8f/%d).%02d -> %.8f\n",i,sum,nonz,vsum,vnum,basenum,basevals[basenum]); + } + if ( iter == 0 ) + { + for (sum=relnum=0; relnum 0 ) + { + sprintf(fname,"ECB/%s",date), iguana_compatible_path(fname); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + if ( fwrite(matrix,1,sizeof(matrix[0][0])*32*32,fp) == sizeof(matrix[0][0])*32*32 ) + loaded = 1; + fclose(fp); + } + } else printf("peggy_matrix error loading %d.%d.%d\n",year,month,day); + } + if ( loaded == 0 && n == 0 ) + { + printf("peggy_matrix couldnt process loaded.%d n.%d\n",loaded,n); + return(-1); + } + //"2000-01-03" + if ( (datenum= conv_date(&seconds,date)) < 0 ) + return(-1); + printf("loaded.(%s) nonz.%d (%d %d %d) datenum.%d\n",date,n,year,month,day,datenum); + return(datenum); +} + +void price777_update(double *btcusdp,double *btcdbtcp) +{ + int32_t i,n,seconds,datenum; uint32_t timestamp; char url[1024],*dstr,*str; + double btcddaily=0.,btcusd=0.,ask,high,low,bid,close,vol,quotevol,open,price = 0.; + //cJSON *btcdtrades,*btcdtrades2,*,*bitcoincharts,; + cJSON *quandl,*btcdhist,*array,*item,*bitcoinave,*blockchaininfo,*coindesk=0; + //btcdtrades = url_json("https://poloniex.com/public?command=returnTradeHistory¤cyPair=BTC_BTCD"); + //btcdtrades2 = url_json("https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-BTCD&count=50"); + bitcoinave = url_json("https://api.bitcoinaverage.com/ticker/USD/"); + //bitcoincharts = url_json("http://api.bitcoincharts.com/v1/weighted_prices.json"); + blockchaininfo = url_json("https://blockchain.info/ticker"); + coindesk = url_json("http://api.coindesk.com/v1/bpi/historical/close.json"); + sprintf(url,"https://poloniex.com/public?command=returnChartData¤cyPair=BTC_BTCD&start=%ld&end=9999999999&period=86400",(long)(time(NULL)-2*3600*24)); + quandl = url_json("https://www.quandl.com/api/v1/datasets/BAVERAGE/USD.json?rows=1"); + if ( quandl != 0 && (str= jstr(quandl,"updated_at")) != 0 && (datenum= conv_date(&seconds,str)) > 0 && (array= jarray(&n,quandl,"data")) != 0 ) + { + //printf("datenum.%d data.%d %d\n",datenum,n,cJSON_GetArraySize(array)); + for (i=0; i<1; i++) + { + // ["Date","24h Average","Ask","Bid","Last","Total Volume"] + // ["2015-07-25",289.27,288.84,288.68,288.87,44978.61] + item = jitem(array,i); + if ( (dstr= jstr(jitem(item,0),0)) != 0 && (datenum= conv_date(&seconds,dstr)) > 0 ) + { + btcusd = price = jdouble(jitem(item,1),0), ask = jdouble(jitem(item,2),0), bid = jdouble(jitem(item,3),0); + close = jdouble(jitem(item,4),0), vol = jdouble(jitem(item,5),0); + //fprintf(stderr,"%d.[%d %f %f %f %f %f].%d ",i,datenum,price,ask,bid,close,vol,n); + } + } + } + price = 0.; + for (i=n=0; ilbase,"btcd") == 0 && strcmp(BUNDLE.ptrs[i]->lrel,"btc") == 0 && BUNDLE.ptrs[i]->lastprice != 0. ) + { + price += BUNDLE.ptrs[i]->lastprice; + n++; + } + } + if ( n != 0 ) + { + price /= n; + *btcdbtcp = price; + //printf("set BTCD price %f\n",price); + BUNDLE.btcdbtc = price; + } + else + { + btcdhist = url_json(url); + //{"date":1406160000,"high":0.01,"low":0.00125,"open":0.01,"close":0.001375,"volume":1.50179994,"quoteVolume":903.58818412,"weightedAverage":0.00166204}, + if ( btcdhist != 0 && (array= jarray(&n,btcdhist,0)) != 0 ) + { + //printf("GOT.(%s)\n",cJSON_Print(array)); + for (i=0; i<1; i++) + { + item = jitem(array,i); + timestamp = juint(item,"date"), high = jdouble(item,"high"), low = jdouble(item,"low"), open = jdouble(item,"open"); + close = jdouble(item,"close"), vol = jdouble(item,"volume"), quotevol = jdouble(item,"quoteVolume"), price = jdouble(item,"weightedAverage"); + //printf("[%u %f %f %f %f %f %f %f]",timestamp,high,low,open,close,vol,quotevol,price); + //printf("[%u %d %f]",timestamp,OS_conv_unixtime(&seconds,timestamp),price); + btcddaily = price; + if ( btcddaily != 0 ) + BUNDLE.btcdbtc = *btcdbtcp = btcddaily; + } + //printf("poloniex.%d\n",n); + } + if ( btcdhist != 0 ) + free_json(btcdhist); + } + // https://blockchain.info/ticker + /* + { + "USD" : {"15m" : 288.22, "last" : 288.22, "buy" : 288.54, "sell" : 288.57, "symbol" : "$"}, + "ISK" : {"15m" : 38765.88, "last" : 38765.88, "buy" : 38808.92, "sell" : 38812.95, "symbol" : "kr"}, + "HKD" : {"15m" : 2234, "last" : 2234, "buy" : 2236.48, "sell" : 2236.71, "symbol" : "$"}, + "TWD" : {"15m" : 9034.19, "last" : 9034.19, "buy" : 9044.22, "sell" : 9045.16, "symbol" : "NT$"}, + "CHF" : {"15m" : 276.39, "last" : 276.39, "buy" : 276.69, "sell" : 276.72, "symbol" : "CHF"}, + "EUR" : {"15m" : 262.46, "last" : 262.46, "buy" : 262.75, "sell" : 262.78, "symbol" : "€"}, + "DKK" : {"15m" : 1958.92, "last" : 1958.92, "buy" : 1961.1, "sell" : 1961.3, "symbol" : "kr"}, + "CLP" : {"15m" : 189160.6, "last" : 189160.6, "buy" : 189370.62, "sell" : 189390.31, "symbol" : "$"}, + "CAD" : {"15m" : 375.45, "last" : 375.45, "buy" : 375.87, "sell" : 375.91, "symbol" : "$"}, + "CNY" : {"15m" : 1783.67, "last" : 1783.67, "buy" : 1785.65, "sell" : 1785.83, "symbol" : "¥"}, + "THB" : {"15m" : 10046.98, "last" : 10046.98, "buy" : 10058.14, "sell" : 10059.18, "symbol" : "฿"}, + "AUD" : {"15m" : 394.77, "last" : 394.77, "buy" : 395.2, "sell" : 395.25, "symbol" : "$"}, + "SGD" : {"15m" : 395.08, "last" : 395.08, "buy" : 395.52, "sell" : 395.56, "symbol" : "$"}, + "KRW" : {"15m" : 335991.51, "last" : 335991.51, "buy" : 336364.55, "sell" : 336399.52, "symbol" : "₩"}, + "JPY" : {"15m" : 35711.99, "last" : 35711.99, "buy" : 35751.64, "sell" : 35755.35, "symbol" : "¥"}, + "PLN" : {"15m" : 1082.74, "last" : 1082.74, "buy" : 1083.94, "sell" : 1084.06, "symbol" : "zł"}, + "GBP" : {"15m" : 185.84, "last" : 185.84, "buy" : 186.04, "sell" : 186.06, "symbol" : "£"}, + "SEK" : {"15m" : 2471.02, "last" : 2471.02, "buy" : 2473.76, "sell" : 2474.02, "symbol" : "kr"}, + "NZD" : {"15m" : 436.89, "last" : 436.89, "buy" : 437.37, "sell" : 437.42, "symbol" : "$"}, + "BRL" : {"15m" : 944.91, "last" : 944.91, "buy" : 945.95, "sell" : 946.05, "symbol" : "R$"}, + "RUB" : {"15m" : 16695.05, "last" : 16695.05, "buy" : 16713.58, "sell" : 16715.32, "symbol" : "RUB"} + }*/ + /*{ + "24h_avg": 281.22, + "ask": 280.12, + "bid": 279.33, + "last": 279.58, + "timestamp": "Sun, 02 Aug 2015 09:36:34 -0000", + "total_vol": 39625.8 + }*/ + + if ( bitcoinave != 0 ) + { + if ( (price= jdouble(bitcoinave,"24h_avg")) > SMALLVAL ) + { + //printf("bitcoinave %f %f\n",btcusd,price); + dxblend(&btcusd,price,0.5); + } + free_json(bitcoinave); + } + if ( quandl != 0 ) + free_json(quandl); + if ( coindesk != 0 ) + free_json(coindesk); + if ( blockchaininfo != 0 ) + { + if ( (item= jobj(blockchaininfo,"USD")) != 0 && item != 0 && (price= jdouble(item,"15m")) > SMALLVAL ) + { + //printf("blockchaininfo %f %f\n",btcusd,price); + dxblend(&btcusd,price,0.5); + } + free_json(blockchaininfo); + } + if ( btcusd != 0 ) + BUNDLE.btcusd = *btcusdp = btcusd; + + + // https://poloniex.com/public?command=returnChartData¤cyPair=BTC_BTCD&start=1405699200&end=9999999999&period=86400 + + // https://poloniex.com/public?command=returnTradeHistory¤cyPair=BTC_BTCD + //https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-BTCD&count=50 + /*{"success":true,"message":"","result":[{"Id":8551089,"TimeStamp":"2015-07-25T16:00:41.597","Quantity":59.60917089,"Price":0.00642371,"Total":0.38291202,"FillType":"FILL","OrderType":"BUY"},{"Id":8551088,"TimeStamp":"2015-07-25T16:00:41.597","Quantity":7.00000000,"Price":0.00639680,"Total":0.04477760,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551087,"TimeStamp":"2015-07-25T16:00:41.597","Quantity":6.51000000,"Price":0.00639679,"Total":0.04164310,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551086,"TimeStamp":"2015-07-25T16:00:41.597","Quantity":6.00000000,"Price":0.00633300,"Total":0.03799800,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551085,"TimeStamp":"2015-07-25T16:00:41.593","Quantity":4.76833955,"Price":0.00623300,"Total":0.02972106,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551084,"TimeStamp":"2015-07-25T16:00:41.593","Quantity":5.00000000,"Price":0.00620860,"Total":0.03104300,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551083,"TimeStamp":"2015-07-25T16:00:41.593","Quantity":4.91803279,"Price":0.00620134,"Total":0.03049839,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551082,"TimeStamp":"2015-07-25T16:00:41.593","Quantity":4.45166432,"Price":0.00619316,"Total":0.02756986,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8551081,"TimeStamp":"2015-07-25T16:00:41.59","Quantity":2.00000000,"Price":0.00619315,"Total":0.01238630,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547525,"TimeStamp":"2015-07-25T06:20:43.69","Quantity":1.23166045,"Price":0.00623300,"Total":0.00767693,"FillType":"FILL","OrderType":"BUY"},{"Id":8547524,"TimeStamp":"2015-07-25T06:20:43.69","Quantity":5.00000000,"Price":0.00613300,"Total":0.03066500,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547523,"TimeStamp":"2015-07-25T06:20:43.687","Quantity":10.00000000,"Price":0.00609990,"Total":0.06099900,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547522,"TimeStamp":"2015-07-25T06:20:43.687","Quantity":0.12326502,"Price":0.00609989,"Total":0.00075190,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547521,"TimeStamp":"2015-07-25T06:20:43.687","Quantity":3.29000000,"Price":0.00609989,"Total":0.02006863,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547520,"TimeStamp":"2015-07-25T06:20:43.687","Quantity":5.00000000,"Price":0.00604400,"Total":0.03022000,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547519,"TimeStamp":"2015-07-25T06:20:43.683","Quantity":12.80164947,"Price":0.00603915,"Total":0.07731108,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547518,"TimeStamp":"2015-07-25T06:20:43.683","Quantity":10.00000000,"Price":0.00602715,"Total":0.06027150,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547517,"TimeStamp":"2015-07-25T06:20:43.683","Quantity":4.29037397,"Price":0.00600000,"Total":0.02574224,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547516,"TimeStamp":"2015-07-25T06:20:43.683","Quantity":77.55994092,"Price":0.00598921,"Total":0.46452277,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":8547515,"TimeStamp":"2015-07-25T06:20:43.68","Quantity":0.08645064,"Price":0.00598492,"Total":0.00051740,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]} + */ + + // https://api.bitcoinaverage.com/ticker/global/all + /* { + "AED": { + "ask": 1063.28, + "bid": 1062.1, + "last": 1062.29, + "timestamp": "Sat, 25 Jul 2015 17:13:14 -0000", + "volume_btc": 0.0, + "volume_percent": 0.0 + },*/ + + // http://api.bitcoincharts.com/v1/weighted_prices.json + // {"USD": {"7d": "279.79", "30d": "276.05", "24h": "288.55"}, "IDR": {"7d": "3750799.88", "30d": "3636926.02", "24h": "3860769.92"}, "ILS": {"7d": "1033.34", "30d": "1031.58", "24h": "1092.36"}, "GBP": {"7d": "179.51", "30d": "175.30", "24h": "185.74"}, "DKK": {"30d": "1758.61"}, "CAD": {"7d": "364.04", "30d": "351.27", "24h": "376.12"}, "MXN": {"30d": "4369.33"}, "XRP": {"7d": "35491.70", "30d": "29257.39", "24h": "36979.02"}, "SEK": {"7d": "2484.50", "30d": "2270.94"}, "SGD": {"7d": "381.93", "30d": "373.69", "24h": "393.94"}, "HKD": {"7d": "2167.99", "30d": "2115.77", "24h": "2232.12"}, "AUD": {"7d": "379.42", "30d": "365.85", "24h": "394.93"}, "CHF": {"30d": "250.61"}, "timestamp": 1437844509, "CNY": {"7d": "1724.99", "30d": "1702.32", "24h": "1779.48"}, "LTC": {"7d": "67.46", "30d": "51.97", "24h": "61.61"}, "NZD": {"7d": "425.01", "30d": "409.33", "24h": "437.86"}, "THB": {"30d": "8632.82"}, "EUR": {"7d": "257.32", "30d": "249.88", "24h": "263.42"}, "ARS": {"30d": "3271.98"}, "NOK": {"30d": "2227.54"}, "RUB": {"7d": "16032.32", "30d": "15600.38", "24h": "16443.39"}, "INR": {"30d": "16601.17"}, "JPY": {"7d": "34685.73", "30d": "33617.77", "24h": "35652.79"}, "CZK": {"30d": "6442.13"}, "BRL": {"7d": "946.76", "30d": "900.77", "24h": "964.09"}, "NMC": {"7d": "454.06", "30d": "370.39", "24h": "436.71"}, "PLN": {"7d": "1041.81", "30d": "1024.96", "24h": "1072.49"}, "ZAR": {"30d": "3805.55"}} +} + +double blend_price(double *volp,double wtA,cJSON *jsonA,double wtB,cJSON *jsonB) +{ + //A.{"ticker":{"base":"BTS","target":"CNY","price":"0.02958291","volume":"3128008.39295500","change":"0.00019513","markets":[{"market":"BTC38","price":"0.02960000","volume":3051650.682955},{"market":"Bter","price":"0.02890000","volume":76357.71}]},"timestamp":1438490881,"success":true,"error":""} + // B.{"id":"bts\/cny","price":"0.02940000","price_before_24h":"0.02990000","volume_first":"3048457.6857147217","volume_second":"90629.45859575272","volume_btc":"52.74","best_market":"btc38","latest_trade":"2015-08-02 03:57:38","coin1":"BitShares","coin2":"CNY","markets":[{"market":"btc38","price":"0.02940000","volume":"3048457.6857147217","volume_btc":"52.738317962865"},{"market":"bter","price":"0.04350000","volume":"0","volume_btc":"0"}]} + double priceA,priceB,priceB24,price,volA,volB; cJSON *obj; + priceA = priceB = priceB24= price = volA = volB = 0.; + if ( jsonA != 0 && (obj= jobj(jsonA,"ticker")) != 0 ) + { + priceA = jdouble(obj,"price"); + volA = jdouble(obj,"volume"); + } + if ( jsonB != 0 ) + { + priceB = jdouble(jsonB,"price"); + priceB24 = jdouble(jsonB,"price_before_24h"); + volB = jdouble(jsonB,"volume_first"); + } + //printf("priceA %f volA %f, priceB %f %f volB %f\n",priceA,volA,priceB,priceB24,volB); + if ( priceB > SMALLVAL && priceB24 > SMALLVAL ) + priceB = (priceB * .1) + (priceB24 * .9); + else if ( priceB < SMALLVAL ) + priceB = priceB24; + if ( priceA*volA < SMALLVAL ) + price = priceB; + else if ( priceB*volB < SMALLVAL ) + price = priceA; + else price = (wtA * priceA) + (wtB * priceB); + *volp = (volA + volB); + return(price); +} + +void _crypto_update(double cryptovols[2][8][2],struct prices777_data *dp,int32_t selector,int32_t peggyflag) +{ + char *cryptonatorA = "https://www.cryptonator.com/api/full/%s-%s"; //unity-btc + char *cryptocoinchartsB = "http://api.cryptocoincharts.info/tradingPair/%s_%s"; //bts_btc + char *cryptostrs[9] = { "btc", "nxt", "unity", "eth", "ltc", "xmr", "bts", "xcp", "etc" }; + int32_t iter,i,j; double btcusd,btcdbtc,cnyusd,prices[8][2],volumes[8][2]; + char base[16],rel[16],url[512],*str; cJSON *jsonA,*jsonB; + if ( peggyflag != 0 ) + { + cnyusd = BUNDLE.cnyusd; + btcusd = BUNDLE.btcusd; + btcdbtc = BUNDLE.btcdbtc; + //printf("update with btcusd %f btcd %f cnyusd %f cnybtc %f\n",btcusd,btcdbtc,cnyusd,cnyusd/btcusd); + if ( btcusd < SMALLVAL || btcdbtc < SMALLVAL ) + { + price777_update(&btcusd,&btcdbtc); + printf("price777_update with btcusd %f btcd %f\n",btcusd,btcdbtc); + } + memset(prices,0,sizeof(prices)); + memset(volumes,0,sizeof(volumes)); + for (j=0; j SMALLVAL ) + break; + i = 3; + } else i = j; + for (iter=0; iter<1; iter++) + { + if ( i == 0 && iter == 0 ) + strcpy(base,"btcd"), strcpy(rel,"btc"); + else strcpy(base,str), strcpy(rel,iter==0?"btc":"cny"); + //if ( selector == 0 ) + { + sprintf(url,cryptonatorA,base,rel); + jsonA = url_json(url); + } + //else + { + sprintf(url,cryptocoinchartsB,base,rel); + jsonB = url_json(url); + } + prices[i][iter] = blend_price(&volumes[i][iter],0.4,jsonA,0.6,jsonB); + if ( iter == 1 ) + { + if ( btcusd > SMALLVAL ) + { + prices[i][iter] *= cnyusd / btcusd; + volumes[i][iter] *= cnyusd / btcusd; + } else prices[i][iter] = volumes[i][iter] = 0.; + } + cryptovols[0][i][iter] = _pairaved(cryptovols[0][i][iter],prices[i][iter]); + cryptovols[1][i][iter] = _pairaved(cryptovols[1][i][iter],volumes[i][iter]); + if ( Debuglevel > 2 ) + printf("(%f %f).%d:%d ",cryptovols[0][i][iter],cryptovols[1][i][iter],i,iter); + //if ( cnyusd < SMALLVAL || btcusd < SMALLVAL ) + // break; + } + } + } +} + +void crypto_update(int32_t peggyflag) +{ + _crypto_update(BUNDLE.cryptovols,&BUNDLE.data,1,peggyflag); + while ( 1 ) + { + _crypto_update(BUNDLE.cryptovols,&BUNDLE.data,1,peggyflag); + sleep(100); + } +} + +void prices777_RTupdate(double cryptovols[2][8][2],double RTmetals[4],double *RTprices,struct prices777_data *dp) +{ + char *cryptostrs[8] = { "btc", "nxt", "unity", "eth", "ltc", "xmr", "bts", "xcp" }; + int32_t iter,i,c,baserel,basenum,relnum; double cnyusd,btcusd,btcdbtc,bid,ask,price,vol,prices[8][2],volumes[8][2]; + char base[16],rel[16]; + price777_update(&btcusd,&btcdbtc); + memset(prices,0,sizeof(prices)); + memset(volumes,0,sizeof(volumes)); + for (i=0; i SMALLVAL ) + dxblend(&btcdbtc,prices[0][0],.9); + dxblend(&dp->btcdbtc,btcdbtc,.995); + if ( BUNDLE.btcdbtc < SMALLVAL ) + BUNDLE.btcdbtc = dp->btcdbtc; + if ( (cnyusd= BUNDLE.cnyusd) > SMALLVAL ) + { + if ( prices[0][1] > SMALLVAL ) + { + //printf("cnyusd %f, btccny %f -> btcusd %f %f\n",cnyusd,prices[0][1],prices[0][1]*cnyusd,btcusd); + btcusd = prices[0][1] * cnyusd; + if ( dp->btcusd < SMALLVAL ) + dp->btcusd = btcusd; + else dxblend(&dp->btcusd,btcusd,.995); + if ( BUNDLE.btcusd < SMALLVAL ) + BUNDLE.btcusd = dp->btcusd; + if ( BUNDLE.data.btcusd < SMALLVAL ) + BUNDLE.data.btcusd = dp->btcusd; + printf("cnyusd %f, btccny %f -> btcusd %f %f -> %f %f %f\n",cnyusd,prices[0][1],prices[0][1]*cnyusd,btcusd,dp->btcusd,BUNDLE.btcusd,BUNDLE.data.btcusd); + } + } + for (i=1; i SMALLVAL ) + { + price = ((prices[i][0] * volumes[i][0]) + (prices[i][1] * volumes[i][1])) / vol; + if ( Debuglevel > 2 ) + printf("%s %f v%f + %f v%f -> %f %f\n",cryptostrs[i],prices[i][0],volumes[i][0],prices[i][1],volumes[i][1],price,dp->cryptos[i]); + dxblend(&dp->cryptos[i],price,.995); + } + } + btcusd = BUNDLE.btcusd; + btcdbtc = BUNDLE.btcdbtc; + if ( Debuglevel > 2 ) + printf(" update with btcusd %f btcd %f\n",btcusd,btcdbtc); + if ( btcusd < SMALLVAL || btcdbtc < SMALLVAL ) + { + price777_update(&btcusd,&btcdbtc); + if ( Debuglevel > 2 ) + printf(" price777_update with btcusd %f btcd %f\n",btcusd,btcdbtc); + } else BUNDLE.btcusd = btcusd, BUNDLE.btcdbtc = btcdbtc; + for (c=0; ctbids[c], ask = dp->tasks[c]; break; + case 1: bid = dp->fbids[c], ask = dp->fasks[c]; break; + case 2: bid = dp->ibids[c], ask = dp->iasks[c]; break; + } + if ( (price= _pairaved(bid,ask)) > SMALLVAL ) + { + if ( Debuglevel > 2 ) + printf("%.6f ",price); + dxblend(&RTprices[c],price,.995); + if ( 0 && (baserel= prices777_ispair(base,rel,CONTRACTS[c])) >= 0 ) + { + basenum = (baserel >> 8) & 0xff, relnum = baserel & 0xff; + if ( basenum < 32 && relnum < 32 ) + { + //printf("%s.%d %f <- %f\n",CONTRACTS[c],c,RTmatrix[basenum][relnum],RTprices[c]); + //dxblend(&RTmatrix[basenum][relnum],RTprices[c],.999); + } + } + if ( strcmp(CONTRACTS[c],"XAUUSD") == 0 ) + dxblend(&RTmetals[0],price,.995); + } + } + } + for (i=0; i SMALLVAL ) + dxblend(btcusdp,btcusd,.9); + if ( btcdbtc > SMALLVAL ) + dxblend(btcdbtcp,btcdbtc,.9); + // char *cryptostrs[8] = { "btc", "nxt", "unity", "eth", "ltc", "xmr", "bts", "xcp" }; + // "BTCUSD", "NXTBTC", "SuperNET", "ETHBTC", "LTCBTC", "XMRBTC", "BTSBTC", "XCPBTC", // BTC priced + for (i=0; i 2 ) + printf("(%s %f).%d ",CURRENCIES[i],basevals[i],i); + } + else if ( (c= prices777_contractnum(contracts[i],0)) >= 0 ) + { + RTprices[i] = BUNDLE.data.RTprices[c]; + //if ( is_decimalstr(contracts[i]+strlen(contracts[i])-2) != 0 ) + // cprices[i] *= .0001; + } + else + { + for (j=0; j 2 ) + printf("(%f %f) i.%d num.%d %s %f\n",*btcusdp,*btcdbtcp,i,num,contracts[i],RTprices[i]); + //printf("RT.(%s %f) ",contracts[i],RTprices[i]); + } + return(BUNDLE.data.ecbdatenum); +} + +int32_t prices_idle(int32_t peggyflag,int32_t idlegap) +{ + static double lastupdate,lastdayupdate; static int32_t didinit; static portable_mutex_t mutex; + int32_t i,datenum; struct prices777_data *dp = &BUNDLE.tmp; + *dp = BUNDLE.data; + if ( didinit == 0 ) + { + portable_mutex_init(&mutex); + prices777_init(BUNDLE.jsonstr,peggyflag); + didinit = 1; + if ( peggyflag != 0 ) + { + int32_t opreturns_init(uint32_t blocknum,uint32_t blocktimestamp,char *path); + opreturns_init(0,(uint32_t)time(NULL),"peggy"); + } + } + if ( peggyflag != 0 && milliseconds() > lastupdate + (1000*idlegap) ) + { + lastupdate = milliseconds(); + if ( milliseconds() > lastdayupdate + 60000*60 ) + { + lastdayupdate = milliseconds(); + if ( (datenum= ecb_matrix(dp->ecbmatrix,dp->edate)) > 0 ) + { + dp->ecbdatenum = datenum; + dp->ecbyear = dp->ecbdatenum / 10000, dp->ecbmonth = (dp->ecbdatenum / 100) % 100, dp->ecbday = (dp->ecbdatenum % 100); + expand_datenum(dp->edate,datenum); + memcpy(dp->RTmatrix,dp->ecbmatrix,sizeof(dp->RTmatrix)); + } + } + for (i=0; itmillistamps,dp->tbids,dp->tasks,dp->topens,dp->thighs,dp->tlows,BUNDLE.truefxuser,BUNDLE.truefxpass,(uint32_t)BUNDLE.truefxidnum); + prices777_fxcm(dp->flhlogmatrix,dp->flogmatrix,dp->fbids,dp->fasks,dp->fhighs,dp->flows); + prices777_instaforex(dp->ilogmatrix,dp->itimestamps,dp->ibids,dp->iasks); + double btcdbtc,btcusd; + price777_update(&btcusd,&btcdbtc); + if ( btcusd > SMALLVAL ) + dxblend(&dp->btcusd,btcusd,0.99); + if ( btcdbtc > SMALLVAL ) + dxblend(&dp->btcdbtc,btcdbtc,0.99); + if ( BUNDLE.data.btcusd == 0 ) + BUNDLE.data.btcusd = dp->btcusd; + if ( BUNDLE.data.btcdbtc == 0 ) + BUNDLE.data.btcdbtc = dp->btcdbtc; + if ( dp->ecbmatrix[USD][USD] > SMALLVAL && dp->ecbmatrix[CNY][CNY] > SMALLVAL ) + BUNDLE.cnyusd = (dp->ecbmatrix[CNY][CNY] / dp->ecbmatrix[USD][USD]); + portable_mutex_lock(&mutex); + BUNDLE.data = *dp; + portable_mutex_unlock(&mutex); + //kv777_write(BUNDLE.kv,"data",5,&BUNDLE.data,sizeof(BUNDLE.data)); + prices777_RTupdate(BUNDLE.cryptovols,BUNDLE.data.RTmetals,BUNDLE.data.RTprices,&BUNDLE.data); + //printf("update finished\n"); + void peggy(); + peggy(); + didinit = 1; + } + return(0); +} + +void prices777_sim(uint32_t now,int32_t numiters) +{ + double btca,btcb,btcd,btc,btcdusd,basevals[MAX_CURRENCIES],btcdprices[MAX_CURRENCIES+1]; + int32_t i,j,datenum,seconds; uint32_t timestamp,starttime = (uint32_t)time(NULL); + for (i=0; i USD %.8f (EURUSD %.8f %.8f) ",datenum,seconds/3600,(seconds%3600)/60,btc,btcd,btcdusd,btcdprices[EUR]/btcdprices[USD],basevals[EUR]/basevals[USD]); + for (j=0; jexchange,cJSON_CreateString(prices->contract)); + cJSON_AddItemToObject(item,"base",cJSON_CreateString(prices->base)); + if ( prices->rel[0] != 0 ) + cJSON_AddItemToObject(item,"rel",cJSON_CreateString(prices->rel)); + //printf("(%s) (%s) (%s)\n",prices->contract,prices->base,prices->rel); + cJSON_AddItemToArray(array,item); + } + } + cJSON_AddItemToObject(json,"result",cJSON_CreateString("success")); + cJSON_AddItemToObject(json,"list",array); + jsonstr = cJSON_Print(json), _stripwhite(jsonstr,' '), free_json(json); + strcpy(retbuf,jsonstr), free(jsonstr); + printf("list -> (%s)\n",retbuf); +} + + +#endif diff --git a/InstantDEX/quotes.h b/InstantDEX/quotes.h new file mode 100755 index 000000000..19a1218a7 --- /dev/null +++ b/InstantDEX/quotes.h @@ -0,0 +1,880 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#ifndef xcode_quotes_h +#define xcode_quotes_h + +#ifdef oldway +int32_t make_jumpiQ(uint64_t refbaseid,uint64_t refrelid,int32_t flip,struct InstantDEX_quote *iQ,struct InstantDEX_quote *baseiQ,struct InstantDEX_quote *reliQ,char *gui,int32_t duration) +{ + uint64_t baseamount,relamount,frombase,fromrel,tobase,torel; + double vol; + char exchange[64]; + uint32_t timestamp; + frombase = baseiQ->baseamount, fromrel = baseiQ->relamount; + tobase = reliQ->baseamount, torel = reliQ->relamount; + if ( make_jumpquote(refbaseid,refrelid,&baseamount,&relamount,&frombase,&fromrel,&tobase,&torel) == 0. ) + return(0); + if ( (timestamp= reliQ->timestamp) > baseiQ->timestamp ) + timestamp = baseiQ->timestamp; + iQ_exchangestr(exchange,iQ); + create_InstantDEX_quote(iQ,timestamp,0,calc_quoteid(baseiQ) ^ calc_quoteid(reliQ),0.,0.,refbaseid,baseamount,refrelid,relamount,exchange,0,gui,baseiQ,reliQ,duration); + if ( Debuglevel > 2 ) + printf("jump%s: %f (%llu/%llu) %llu %llu (%f %f) %llu %llu\n",flip==0?"BID":"ASK",calc_price_volume(&vol,iQ->baseamount,iQ->relamount),(long long)baseamount,(long long)relamount,(long long)frombase,(long long)fromrel,calc_price_volume(&vol,frombase,fromrel),calc_price_volume(&vol,tobase,torel),(long long)tobase,(long long)torel); + iQ->isask = flip; + iQ->minperc = baseiQ->minperc; + if ( reliQ->minperc > iQ->minperc ) + iQ->minperc = reliQ->minperc; + return(1); +} +#else + +struct InstantDEX_quote *AllQuotes; + +void clear_InstantDEX_quoteflags(struct InstantDEX_quote *iQ) +{ + //duration:14,wallet:1,a:1,isask:1,expired:1,closed:1,swap:1,responded:1,matched:1,feepaid:1,automatch:1,pending:1,minperc:7; + iQ->s.a = iQ->s.expired = iQ->s.swap = iQ->s.feepaid = 0; + iQ->s.closed = iQ->s.pending = iQ->s.responded = iQ->s.matched = 0; +} +void cancel_InstantDEX_quote(struct InstantDEX_quote *iQ) { iQ->s.closed = 1; } + +int32_t InstantDEX_uncalcsize() { struct InstantDEX_quote iQ; return(sizeof(iQ.hh) + sizeof(iQ.s.quoteid) + sizeof(iQ.s.price) + sizeof(iQ.s.vol)); } + +int32_t iQcmp(struct InstantDEX_quote *iQA,struct InstantDEX_quote *iQB) +{ + if ( iQA->s.isask == iQB->s.isask && iQA->s.baseid == iQB->s.baseid && iQA->s.relid == iQB->s.relid && iQA->s.baseamount == iQB->s.baseamount && iQA->s.relamount == iQB->s.relamount ) + return(0); + else if ( iQA->s.isask != iQB->s.isask && iQA->s.baseid == iQB->s.relid && iQA->s.relid == iQB->s.baseid && iQA->s.baseamount == iQB->s.relamount && iQA->s.relamount == iQB->s.baseamount ) + return(0); + return(-1); +} + +uint64_t calc_txid(unsigned char *buf,int32_t len) +{ + bits256 hash; + vcalc_sha256(0,hash.bytes,buf,len); + return(hash.txid); +} + +uint64_t calc_quoteid(struct InstantDEX_quote *iQ) +{ + struct InstantDEX_quote Q; + if ( iQ == 0 ) + return(0); + if ( iQ->s.duration == 0 || iQ->s.duration > ORDERBOOK_EXPIRATION ) + iQ->s.duration = ORDERBOOK_EXPIRATION; + if ( iQ->s.quoteid == 0 ) + { + Q = *iQ; + clear_InstantDEX_quoteflags(&Q); + if ( Q.s.isask != 0 ) + { + Q.s.baseid = iQ->s.relid, Q.s.baseamount = iQ->s.relamount; + Q.s.relid = iQ->s.baseid, Q.s.relamount = iQ->s.baseamount; + Q.s.isask = Q.s.minperc = 0; + } + return(calc_txid((uint8_t *)((long)&Q + InstantDEX_uncalcsize()),sizeof(Q) - InstantDEX_uncalcsize())); + } return(iQ->s.quoteid); +} + +struct InstantDEX_quote *find_iQ(uint64_t quoteid) +{ + struct InstantDEX_quote *iQ; + HASH_FIND(hh,AllQuotes,"eid,sizeof(quoteid),iQ); + return(iQ); +} + +struct InstantDEX_quote *delete_iQ(uint64_t quoteid) +{ + struct InstantDEX_quote *iQ; + if ( (iQ= find_iQ(quoteid)) != 0 ) + { + HASH_DELETE(hh,AllQuotes,iQ); + } + return(iQ); +} + +struct InstantDEX_quote *findquoteid(uint64_t quoteid,int32_t evenclosed) +{ + struct InstantDEX_quote *iQ; + if ( (iQ= find_iQ(quoteid)) != 0 ) + { + if ( evenclosed != 0 || iQ->s.closed == 0 ) + { + if ( calc_quoteid(iQ) == quoteid ) + return(iQ); + else printf("calc_quoteid %llu vs %llu\n",(long long)calc_quoteid(iQ),(long long)quoteid); + } //else printf("quoteid.%llu closed.%d\n",(long long)quoteid,iQ->closed); + } else printf("couldnt find %llu\n",(long long)quoteid); + return(0); +} + +int32_t cancelquote(char *NXTaddr,uint64_t quoteid) +{ + struct InstantDEX_quote *iQ; + if ( (iQ= findquoteid(quoteid,0)) != 0 && iQ->s.offerNXT == calc_nxt64bits(NXTaddr) && iQ->exchangeid == INSTANTDEX_EXCHANGEID ) + { + cancel_InstantDEX_quote(iQ); + return(1); + } + return(0); +} + +struct InstantDEX_quote *create_iQ(struct InstantDEX_quote *iQ,char *walletstr) +{ + struct InstantDEX_quote *newiQ,*tmp; struct prices777 *prices; int32_t inverted; long len = 0; + if ( walletstr != 0 && (len= strlen(walletstr)) > 0 ) + iQ->s.wallet = 1, len++; + calc_quoteid(iQ); + printf("createiQ %llu/%llu %f %f quoteid.%llu offerNXT.%llu wallet.%d (%s)\n",(long long)iQ->s.baseid,(long long)iQ->s.relid,iQ->s.price,iQ->s.vol,(long long)iQ->s.quoteid,(long long)iQ->s.offerNXT,iQ->s.wallet,walletstr!=0?walletstr:""); + if ( (newiQ= find_iQ(iQ->s.quoteid)) != 0 ) + return(newiQ); + newiQ = calloc(1,sizeof(*newiQ) + len); + *newiQ = *iQ; + if ( len != 0 ) + memcpy(newiQ->walletstr,walletstr,len); + HASH_ADD(hh,AllQuotes,s.quoteid,sizeof(newiQ->s.quoteid),newiQ); + if ( (prices= prices777_find(&inverted,iQ->s.baseid,iQ->s.relid,INSTANTDEX_NAME)) != 0 ) + prices->dirty++; + { + struct InstantDEX_quote *checkiQ; + if ( (checkiQ= find_iQ(iQ->s.quoteid)) == 0 || iQcmp(iQ,checkiQ) != 0 )//memcmp((uint8_t *)((long)checkiQ + sizeof(checkiQ->hh) + sizeof(checkiQ->quoteid)),(uint8_t *)((long)iQ + sizeof(iQ->hh) + sizeof(iQ->quoteid)),sizeof(*iQ) - sizeof(iQ->hh) - sizeof(iQ->quoteid)) != 0 ) + { + int32_t i; + for (i=(sizeof(iQ->hh) - sizeof(iQ->s.quoteid)); ihh) - sizeof(iQ->s.quoteid); i++) + printf("%02x ",((uint8_t *)iQ)[i]); + printf("iQ\n"); + for (i=(sizeof(checkiQ->hh) + sizeof(checkiQ->s.quoteid)); ihh) - sizeof(checkiQ->s.quoteid); i++) + printf("%02x ",((uint8_t *)checkiQ)[i]); + printf("checkiQ\n"); + printf("error finding iQ after adding %llu vs %llu\n",(long long)checkiQ->s.quoteid,(long long)iQ->s.quoteid); + } + } + HASH_ITER(hh,AllQuotes,iQ,tmp) + { + if ( iQ->s.expired != 0 ) + { + printf("quoteid.%llu expired, purging\n",(long long)iQ->s.expired); + delete_iQ(iQ->s.quoteid); + } + } + return(newiQ); +} + +#ifdef later +cJSON *pangea_walletitem(cJSON *walletitem,struct coin777 *coin,int32_t rakemillis,int64_t bigblind,int64_t ante,int32_t minbuyin,int32_t maxbuyin) +{ + char *addr; struct destbuf pubkey; + if ( walletitem == 0 ) + walletitem = cJSON_CreateObject(); + //printf("call get_acct_coinaddr.%s (%s) (%s)\n",coin->name,coin->serverport,coin->userpass); + if ( coin->pangeapubkey[0] == 0 || coin->pangeacoinaddr[0] == 0 ) + { + if ( strcmp("NXT",coin->name) == 0 ) + { + } + else if ( (addr= get_acct_coinaddr(coin->pangeacoinaddr,coin->name,coin->serverport,coin->userpass,"pangea")) != 0 ) + { + //printf("get_pubkey\n"); + get_pubkey(&pubkey,coin->name,coin->serverport,coin->userpass,coin->pangeacoinaddr); + strcpy(coin->pangeapubkey,pubkey.buf); + } + } + jaddstr(walletitem,"pubkey",coin->pangeapubkey); + jaddstr(walletitem,"coinaddr",coin->pangeacoinaddr); + jaddnum(walletitem,"rakemillis",rakemillis); + jaddnum(walletitem,"minbuyin",minbuyin); + jaddnum(walletitem,"maxbuyin",maxbuyin); + jadd64bits(walletitem,"bigblind",bigblind); + jadd64bits(walletitem,"ante",ante); + return(walletitem); +} + +cJSON *set_walletstr(cJSON *walletitem,char *walletstr,struct InstantDEX_quote *iQ) +{ + char pubkeystr[128],pkhash[128],base[64],rel[64],fieldA[64],fieldB[64],fieldpkhash[64],*pubA,*pubB,*pkhashstr,*str,*exchangestr; + struct coin777 *coin; int32_t flip = 0; + if ( walletstr != 0 && walletitem == 0 ) + walletitem = cJSON_Parse(walletstr); + if ( walletitem == 0 ) + walletitem = cJSON_CreateObject(); + unstringbits(base,iQ->s.basebits), unstringbits(rel,iQ->s.relbits); + flip = (iQ->s.offerNXT != IGUANA_MY64BITS); + if ( strcmp(base,"NXT") != 0 ) + coin = coin777_find(base,1); + else if ( strcmp(rel,"NXT") != 0 ) + coin = coin777_find(rel,1), flip ^= 1; + else coin = 0; + if ( coin != 0 ) + { + if ( (exchangestr= exchange_str(iQ->exchangeid)) != 0 && strcmp(exchangestr,"pangea") == 0 ) + pangea_walletitem(walletitem,coin,iQ->s.minperc,iQ->s.baseamount,iQ->s.relamount,iQ->s.minbuyin,iQ->s.maxbuyin); + else + { + //printf("START.(%s)\n",jprint(walletitem,0)); + if ( (iQ->s.isask ^ flip) == 0 ) + { + sprintf(fieldA,"%spubA",coin->name); + if ( (pubA= jstr(walletitem,fieldA)) != 0 ) + cJSON_DeleteItemFromObject(walletitem,fieldA); + jaddstr(walletitem,fieldA,coin->atomicsendpubkey); + //printf("replaceA\n"); + } + else + { + sprintf(fieldB,"%spubB",coin->name); + if ( (pubB= jstr(walletitem,fieldB)) != 0 ) + cJSON_DeleteItemFromObject(walletitem,fieldB); + jaddstr(walletitem,fieldB,coin->atomicrecvpubkey); + sprintf(fieldpkhash,"%spkhash",coin->name); + if ( (pkhashstr= jstr(walletitem,fieldpkhash)) != 0 ) + cJSON_DeleteItemFromObject(walletitem,fieldpkhash); + subatomic_pubkeyhash(pubkeystr,pkhash,coin,iQ->s.quoteid); + jaddstr(walletitem,fieldpkhash,pkhash); + //printf("replaceB\n"); + } + } + str = jprint(walletitem,0); + strcpy(walletstr,str); + free(str); + return(walletitem); + } + return(0); +} +#endif + +char *InstantDEX_str(char *walletstr,char *buf,int32_t extraflag,struct InstantDEX_quote *iQ) +{ + cJSON *json; char _buf[4096],base[64],rel[64],*str; + unstringbits(base,iQ->s.basebits), unstringbits(rel,iQ->s.relbits); + if ( buf == 0 ) + buf = _buf; + sprintf(buf,"{\"quoteid\":\"%llu\",\"base\":\"%s\",\"baseid\":\"%llu\",\"baseamount\":\"%llu\",\"rel\":\"%s\",\"relid\":\"%llu\",\"relamount\":\"%llu\",\"price\":%.8f,\"volume\":%.8f,\"offerNXT\":\"%llu\",\"timestamp\":\"%u\",\"isask\":\"%u\",\"exchange\":\"%s\",\"gui\":\"%s\"}",(long long)iQ->s.quoteid,base,(long long)iQ->s.baseid,(long long)iQ->s.baseamount,rel,(long long)iQ->s.relid,(long long)iQ->s.relamount,iQ->s.price,iQ->s.vol,(long long)iQ->s.offerNXT,iQ->s.timestamp,iQ->s.isask,exchange_str(iQ->exchangeid),iQ->gui); + if ( extraflag != 0 ) + { + sprintf(buf + strlen(buf) - 1,",\"plugin\":\"relay\",\"destplugin\":\"InstantDEX\",\"method\":\"busdata\",\"submethod\":\"%s\"}",(iQ->s.isask != 0) ? "ask" : "bid"); + } + //printf("InstantDEX_str.(%s)\n",buf); + if ( (json= cJSON_Parse(buf)) != 0 ) + { +#ifdef later + char _buf[4096],_walletstr[256],base[64],rel[64],*exchange,*str; cJSON *walletitem,*json; struct coin777 *coin; + if ( walletstr == 0 ) + { + walletstr = _walletstr; + walletstr[0] = 0; + } + if ( (exchange= exchange_str(iQ->exchangeid)) != 0 ) + { + coin = coin777_find(base,0); + if ( strcmp(exchange,"wallet") == 0 ) + walletitem = set_walletstr(0,walletstr,iQ); + else if ( strcmp(exchange,"pangea") == 0 && walletstr[0] == 0 && coin != 0 ) + walletitem = pangea_walletitem(0,coin,iQ->s.minperc,iQ->s.baseamount,iQ->s.relamount,iQ->s.minbuyin,iQ->s.maxbuyin); + else walletitem = 0; + if ( walletitem != 0 ) + { + jadd(json,"wallet",walletitem); + strcpy(walletstr,jprint(walletitem,0)); + } +//printf("exchange.(%s) iswallet.%d (%s) base.(%s) coin.%p (%s)\n",exchange,iQ->s.wallet,walletstr,base,coin,jprint(json,0)); + } else printf("InstantDEX_str cant find exchangeid.%d\n",iQ->exchangeid); +#endif + str = jprint(json,1); + strcpy(buf,str); + //printf("str.(%s) %p\n",buf,buf); + free(str); + } else printf("InstantDEX_str cant parse.(%s)\n",buf); + if ( buf == _buf ) + return(clonestr(buf)); + else return(buf); +} + +uint64_t _get_AEquote(char *str,uint64_t orderid) +{ + cJSON *json; + uint64_t nxt64bits = 0; + char cmd[256],*jsonstr; + sprintf(cmd,"requestType=get%sOrder&order=%llu",str,(long long)orderid); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + nxt64bits = get_API_nxt64bits(cJSON_GetObjectItem(json,"account")); + free_json(json); + } + free(jsonstr); + } + return(nxt64bits); +} + +char *cancel_NXTorderid(char *NXTaddr,char *nxtsecret,uint64_t orderid) +{ + uint64_t nxt64bits; char cmd[1025],secret[8192],*str = "Bid",*retstr = 0; + if ( (nxt64bits= _get_AEquote(str,orderid)) == 0 ) + str = "Ask", nxt64bits = _get_AEquote(str,orderid); + if ( nxt64bits == calc_nxt64bits(NXTaddr) ) + { + escape_code(secret,nxtsecret); + sprintf(cmd,"requestType=cancel%sOrder&secretPhrase=%s&feeNQT=%lld&deadline=%d&order=%llu",str,secret,(long long)MIN_NQTFEE,DEFAULT_NXT_DEADLINE,(long long)orderid); + retstr = issue_NXTPOST(cmd); + //printf("(%s) -> (%s)\n",cmd,retstr); + } + return(retstr); +} + +char *InstantDEX_cancelorder(cJSON *argjson,char *activenxt,char *secret,uint64_t orderid,uint64_t quoteid) +{ + struct InstantDEX_quote *iQ; cJSON *json,*array,*item; char numstr[64],*retstr,*exchangestr; + uint64_t quoteids[256]; int32_t i,exchangeid,n=0; struct exchange_info *exchange; + if ( (exchangestr= jstr(argjson,"exchange")) != 0 && (exchange= find_exchange(&exchangeid,exchangestr)) != 0 ) + { + if ( exchange->issue.cancelorder != 0 ) + { + if ( (retstr= (*exchange->issue.cancelorder)(&exchange->cHandle,exchange,argjson,quoteid)) == 0 ) + retstr = clonestr("{\"result\":\"nothing returned from exchange\"}"); + return(retstr); + } + else return(clonestr("{\"error\":\"no cancelorder function\"}")); + } + memset(quoteids,0,sizeof(quoteids)); + json = cJSON_CreateObject(), array = cJSON_CreateArray(); + if ( quoteid != 0 ) + quoteids[n++] = quoteid; + //n += InstantDEX_quoteids(quoteids+n,orderid); + for (i=0; is.offerNXT == calc_nxt64bits(activenxt) ) + cancel_InstantDEX_quote(iQ); + if ( (item= cJSON_Parse(retstr)) != 0 ) + jaddi(array,item); + free(retstr); + } + cancelquote(activenxt,quoteid); + } + if ( orderid != 0 ) + { + if ( cancelquote(activenxt,orderid) != 0 ) + sprintf(numstr,"%llu",(long long)orderid), jaddstr(json,"ordercanceled",numstr); + } + return(jprint(json,1)); +} + +char *InstantDEX_orderstatus(cJSON *argjson,uint64_t orderid,uint64_t quoteid) +{ + struct InstantDEX_quote *iQ = 0; char *exchangestr,*str; struct exchange_info *exchange; int32_t exchangeid; + if ( (exchangestr= jstr(argjson,"exchange")) != 0 && (exchange= find_exchange(&exchangeid,exchangestr)) != 0 ) + { + if ( exchange->issue.orderstatus != 0 ) + { + if ( (str= (*exchange->issue.orderstatus)(&exchange->cHandle,exchange,argjson,quoteid)) == 0 ) + str = clonestr("{\"result\":\"nothing returned from exchange\"}"); + return(str); + } + else return(clonestr("{\"error\":\"no orderstatus function\"}")); + } + if ( (iQ= find_iQ(orderid)) != 0 || (iQ= find_iQ(quoteid)) != 0 ) + return(InstantDEX_str(0,0,0,iQ)); + return(clonestr("{\"error\":\"couldnt find orderid\"}")); +} + +char *InstantDEX_openorders(cJSON *argjson,char *NXTaddr,int32_t allorders) +{ + struct InstantDEX_quote *iQ,*tmp; char buf[4096],*exchangestr,*jsonstr,*str; uint32_t now,duration; + cJSON *json,*array,*item; uint64_t nxt64bits; struct exchange_info *exchange; int32_t exchangeid; + if ( (exchangestr= jstr(argjson,"exchange")) != 0 && (exchange= find_exchange(&exchangeid,exchangestr)) != 0 ) + { + if ( exchange->issue.openorders != 0 ) + { + if ( (str= (*exchange->issue.openorders)(&exchange->cHandle,exchange,argjson)) == 0 ) + str = clonestr("{\"result\":\"nothing returned from exchange\"}"); + return(str); + } + else return(clonestr("{\"error\":\"no orderstatus function\"}")); + } + nxt64bits = calc_nxt64bits(NXTaddr); + now = (uint32_t)time(NULL); + json = cJSON_CreateObject(), array = cJSON_CreateArray(); + HASH_ITER(hh,AllQuotes,iQ,tmp) + { + if ( (duration= iQ->s.duration) == 0 ) + duration = ORDERBOOK_EXPIRATION; + if ( iQ->s.timestamp > (now + duration) ) + iQ->s.expired = iQ->s.closed = 1; + if ( iQ->s.offerNXT == nxt64bits && (allorders != 0 || iQ->s.closed == 0) ) + { + if ( (jsonstr= InstantDEX_str(0,buf,0,iQ)) != 0 && (item= cJSON_Parse(jsonstr)) != 0 ) + jaddi(array,item); + } + } + jadd(json,"openorders",array); + return(jprint(json,1)); +} + +cJSON *InstantDEX_specialorders(uint64_t *quoteidp,uint64_t nxt64bits,char *base,char *special,uint64_t baseamount,int32_t addrtype) +{ + struct InstantDEX_quote *iQ,*tmp; int32_t exchangeid; uint32_t i,n,now,duration,ismine = 0; + uint64_t basebits; cJSON *item=0,*array = 0; char *coinaddr=0,*pubkey,checkaddr[128]; + now = (uint32_t)time(NULL); + basebits = stringbits(base); + if ( special == 0 || find_exchange(&exchangeid,special) == 0 ) + exchangeid = 0; + n = 0; + *quoteidp = 0; + HASH_ITER(hh,AllQuotes,iQ,tmp) + { + //printf("iter Q.%llu b.%llu\n",(long long)iQ->s.quoteid,(long long)iQ->s.basebits); + if ( (duration= iQ->s.duration) == 0 ) + duration = ORDERBOOK_EXPIRATION; + if ( iQ->s.timestamp > (now + duration) ) + { + iQ->s.expired = iQ->s.closed = 1; + printf("expire order %llu\n",(long long)iQ->s.quoteid); + continue; + } + if ( iQ->s.basebits == basebits && (exchangeid == 0 || iQ->exchangeid == exchangeid) ) + { + //printf("matched basebits\n"); + if ( strcmp(special,"pangea") == 0 ) + { + checkaddr[0] = 0; + if ( iQ->s.wallet != 0 && (item= cJSON_Parse(iQ->walletstr)) != 0 && (coinaddr= jstr(item,"coinaddr")) != 0 && coinaddr[0] != 0 && (pubkey= jstr(item,"pubkey")) != 0 && pubkey[0] != 0 ) + btc_coinaddr(coinaddr,addrtype,pubkey); + if ( item != 0 ) + free_json(item); + if ( coinaddr == 0 || strcmp(coinaddr,checkaddr) != 0 ) + { + printf("mismatched pangea coinaddr (%s) vs (%s) or baseamount %.8f vs %.8f\n",coinaddr,checkaddr,dstr(baseamount),dstr(iQ->s.baseamount)); + continue; + } + } + if ( n > 0 ) + { + for (i=0; is.offerNXT == j64bits(jitem(array,i),0) ) + break; + } + //printf("found duplicate\n"); + } else i = 0; + if ( i == n ) + { + if ( iQ->s.offerNXT == nxt64bits ) + { + ismine = 1; + if ( *quoteidp == 0 ) + *quoteidp = iQ->s.quoteid; + } + if ( array == 0 ) + array = cJSON_CreateArray(); + jaddi64bits(array,iQ->s.offerNXT); + //printf("add %llu\n",(long long)iQ->s.offerNXT); + } + } //else printf("quote.%llu basebits.%llu\n",(long long)iQ->s.quoteid,(long long)iQ->s.basebits); + } + if ( ismine == 0 ) + free_json(array), array = 0; + //printf("ismine.%d n.%d array.%d\n",ismine,n,array==0?0:cJSON_GetArraySize(array)); + return(array); +} + +int _decreasing_quotes(const void *a,const void *b) +{ +#define order_a ((struct InstantDEX_quote *)a) +#define order_b ((struct InstantDEX_quote *)b) + if ( order_b->s.price > order_a->s.price ) + return(1); + else if ( order_b->s.price < order_a->s.price ) + return(-1); + return(0); +#undef order_a +#undef order_b +} + +int _increasing_quotes(const void *a,const void *b) +{ +#define order_a ((struct InstantDEX_quote *)a) +#define order_b ((struct InstantDEX_quote *)b) + if ( order_b->s.price > order_a->s.price ) + return(-1); + else if ( order_b->s.price < order_a->s.price ) + return(1); + return(0); +#undef order_a +#undef order_b +} + +cJSON *prices777_orderjson(struct InstantDEX_quote *iQ) +{ + cJSON *item = cJSON_CreateArray(); + jaddinum(item,iQ->s.price); + jaddinum(item,iQ->s.vol); + jaddi64bits(item,iQ->s.quoteid); + return(item); +} + +cJSON *InstantDEX_orderbook(struct prices777 *prices) +{ + struct InstantDEX_quote *ptr,iQ,*tmp,*askvals=0,*bidvals=0; cJSON *json,*bids,*asks; uint32_t now,duration; + int32_t i,isask,iter,n,m,numbids,numasks,invert; + json = cJSON_CreateObject(), bids = cJSON_CreateArray(), asks = cJSON_CreateArray(); + now = (uint32_t)time(NULL); + for (iter=numbids=numasks=n=m=0; iter<2; iter++) + { + HASH_ITER(hh,AllQuotes,ptr,tmp) + { + iQ = *ptr; + if ( (duration= iQ.s.duration) == 0 ) + duration = ORDERBOOK_EXPIRATION; + if ( iQ.s.timestamp > (now + duration) ) + { + iQ.s.expired = iQ.s.closed = 1; + continue; + } + if ( Debuglevel > 2 ) + printf("iterate quote.%llu\n",(long long)iQ.s.quoteid); + if ( prices777_equiv(ptr->s.baseid) == prices777_equiv(prices->baseid) && prices777_equiv(ptr->s.relid) == prices777_equiv(prices->relid) ) + invert = 0; + else if ( prices777_equiv(ptr->s.relid) == prices777_equiv(prices->baseid) && prices777_equiv(ptr->s.baseid) == prices777_equiv(prices->relid) ) + invert = 1; + else continue; + if ( ptr->s.pending != 0 ) + continue; + isask = iQ.s.isask; + if ( invert != 0 ) + isask ^= 1; + if ( invert != 0 ) + { + if ( iQ.s.price > SMALLVAL ) + iQ.s.vol *= iQ.s.price, iQ.s.price = 1. / iQ.s.price; + else iQ.s.price = prices777_price_volume(&iQ.s.vol,iQ.s.relamount,iQ.s.baseamount); + } + else if ( iQ.s.price <= SMALLVAL ) + iQ.s.price = prices777_price_volume(&iQ.s.vol,iQ.s.baseamount,iQ.s.relamount); + if ( iter == 0 ) + { + if ( isask != 0 ) + numasks++; + else numbids++; + } + else + { + if ( isask == 0 && n < numbids ) + bidvals[n++] = iQ; + else if ( isask != 0 && m < numasks ) + askvals[m++] = iQ; + } + } + if ( iter == 0 ) + { + if ( numbids > 0 ) + bidvals = calloc(numbids,sizeof(*bidvals)); + if ( numasks > 0 ) + askvals = calloc(numasks,sizeof(*askvals)); + } + } + if ( numbids > 0 ) + { + if ( n > 0 ) + { + qsort(bidvals,n,sizeof(*bidvals),_decreasing_quotes); + for (i=0; i 0 ) + { + if ( m > 0 ) + { + qsort(askvals,m,sizeof(*askvals),_increasing_quotes); + for (i=0; i (refvol * INSTANTDEX_MINVOLPERC) )//&& refvol > (vol * iQ->s.minperc * .01) ) + { + if ( vol < refvol ) + metric = (vol / refvol); + else metric = 1.; + if ( dir > 0 && price < (refprice * (1. + INSTANTDEX_PRICESLIPPAGE) + SMALLVAL) ) + metric *= (1. + (refprice - price)/refprice); + else if ( dir < 0 && price > (refprice * (1. - INSTANTDEX_PRICESLIPPAGE) - SMALLVAL) ) + metric *= (1. + (price - refprice)/refprice); + else metric = 0.; + if ( metric != 0. ) + { + printf("price %.8f vol %.8f | %.8f > %.8f? %.8f > %.8f?\n",price,vol,vol,(refvol * INSTANTDEX_MINVOLPERC),refvol,(vol * INSTANTDEX_MINVOLPERC)); + printf("price %f against %f or %f\n",price,(refprice * (1. + INSTANTDEX_PRICESLIPPAGE) + SMALLVAL),(refprice * (1. - INSTANTDEX_PRICESLIPPAGE) - SMALLVAL)); + printf("metric %f\n",metric); + } + } + return(metric); +} + +char *autofill(char *remoteaddr,struct InstantDEX_quote *refiQ,char *NXTaddr,char *NXTACCTSECRET) +{ + double price,volume,revprice,revvol,metric,bestmetric = 0.; int32_t dir,inverted; uint64_t nxt64bits; char *retstr=0; + struct InstantDEX_quote *iQ,*tmp,*bestiQ; struct prices777 *prices; uint32_t duration,now = (uint32_t)time(NULL); +return(0); + nxt64bits = calc_nxt64bits(NXTaddr); + memset(&bestiQ,0,sizeof(bestiQ)); + dir = (refiQ->s.isask != 0) ? -1 : 1; + HASH_ITER(hh,AllQuotes,iQ,tmp) + { + if ( (duration= refiQ->s.duration) == 0 ) + duration = ORDERBOOK_EXPIRATION; + if ( iQ->s.timestamp > (now + duration) ) + iQ->s.expired = iQ->s.closed = 1; + if ( iQ->s.offerNXT == nxt64bits && iQ->s.closed == 0 && iQ->s.pending == 0 ) + { + if ( iQ->s.baseid == refiQ->s.baseid && iQ->s.relid == refiQ->s.relid && iQ->s.isask != refiQ->s.isask && (metric= ordermetric(iQ->s.price,iQ->s.vol,dir,refiQ->s.price,refiQ->s.vol)) > bestmetric ) + { + bestmetric = metric; + bestiQ = iQ; + } + else if ( iQ->s.baseid == refiQ->s.relid && iQ->s.relid == refiQ->s.baseid && iQ->s.isask == refiQ->s.isask && iQ->s.price > SMALLVAL ) + { + revvol = (iQ->s.price * iQ->s.vol), revprice = (1. / iQ->s.price); + if ( (metric= ordermetric(revprice,revvol,dir,refiQ->s.price,refiQ->s.vol)) > bestmetric ) + { + bestmetric = metric; + bestiQ = iQ; + } + } + } + } + if ( bestmetric > 0. ) + { + if ( (prices= prices777_find(&inverted,bestiQ->s.baseid,bestiQ->s.relid,exchange_str(bestiQ->exchangeid))) != 0 ) + { + printf("isask.%d %f %f -> bestmetric %f inverted.%d autofill dir.%d price %f vol %f\n",bestiQ->s.isask,bestiQ->s.price,bestiQ->s.vol,bestmetric,inverted,dir,refiQ->s.price,refiQ->s.vol); + if ( bestiQ->s.isask != 0 ) + dir = -1; + else dir = 1; + if ( inverted != 0 ) + { + dir *= -1; + volume = (bestiQ->s.price * bestiQ->s.vol); + price = 1. / bestiQ->s.price; + printf("price inverted (%f %f) -> (%f %f)\n",bestiQ->s.price,bestiQ->s.vol,price,volume); + } else price = bestiQ->s.price, volume = bestiQ->s.vol; + retstr = prices777_trade(0,0,0,0,1,0,NXTaddr,NXTACCTSECRET,prices,dir,price,volume,bestiQ,0,bestiQ->s.quoteid,0); + } + } + return(retstr); +} + +char *automatch(struct prices777 *prices,int32_t dir,double refprice,double refvol,char *NXTaddr,char *NXTACCTSECRET) +{ + int32_t i,n=0; struct prices777_order order,bestorder; char *retstr = 0; double metric,bestmetric = 0.; +return(0); + memset(&bestorder,0,sizeof(bestorder)); + if ( dir > 0 ) + n = prices->O.numasks; + else if ( dir < 0 ) + n = prices->O.numbids; + if ( n > 0 ) + { + for (i=0; i 0) ? prices->O.book[MAX_GROUPS][i].ask : prices->O.book[MAX_GROUPS][i].bid; + if ( (metric= ordermetric(order.s.price,order.s.vol,dir,refprice,refvol)) > bestmetric ) + { + bestmetric = metric; + bestorder = order; + } + } + } + //printf("n.%d\n",n); + if ( bestorder.source != 0 ) + retstr = prices777_trade(0,0,0,0,1,0,NXTaddr,NXTACCTSECRET,bestorder.source,bestorder.s.isask!=0?-1:1,bestorder.s.price,bestorder.s.vol,0,&bestorder,bestorder.s.quoteid,0); + return(retstr); +} + +int offer_checkitem(struct pending_trade *pend,cJSON *item) +{ + uint64_t quoteid; struct InstantDEX_quote *iQ; + if ( (quoteid= j64bits(item,"quoteid")) != 0 && (iQ= find_iQ(quoteid)) != 0 && iQ->s.closed != 0 ) + return(0); + return(-1); +} + +void trades_update() +{ +#ifdef later + int32_t iter; struct pending_trade *pend; + for (iter=0; iter<2; iter++) + { + while ( (pend= queue_dequeue(&Pending_offersQ.pingpong[iter],0)) != 0 ) + { + if ( time(NULL) > pend->expiration ) + { + printf("now.%ld vs timestamp.%u vs expiration %u | ",(long)time(NULL),pend->timestamp,pend->expiration); + printf("offer_statemachine %llu/%llu %d %f %f\n",(long long)pend->orderid,(long long)pend->quoteid,pend->dir,pend->price,pend->volume); + //InstantDEX_history(1,pend,retstr); + if ( pend->bot == 0 ) + free_pending(pend); + else pend->finishtime = (uint32_t)time(NULL); + } + else + { + printf("InstantDEX_update requeue %llu/%llu %d %f %f\n",(long long)pend->orderid,(long long)pend->quoteid,pend->dir,pend->price,pend->volume); + queue_enqueue("requeue",&Pending_offersQ.pingpong[iter ^ 1],&pend->DL,0); + } + } + } +#endif +} + +void InstantDEX_update(char *NXTaddr,char *NXTACCTSECRET) +{ + int32_t dir; double price,volume; uint32_t now; char *retstr = 0; + int32_t inverted; struct InstantDEX_quote *iQ,*tmp; struct prices777 *prices; uint64_t nxt64bits = calc_nxt64bits(NXTaddr); + now = (uint32_t)time(NULL); + HASH_ITER(hh,AllQuotes,iQ,tmp) + { + if ( iQ->s.timestamp > (now + ORDERBOOK_EXPIRATION) ) + iQ->s.expired = iQ->s.closed = 1; + if ( iQ->s.offerNXT == nxt64bits && iQ->s.closed == 0 && iQ->s.pending == 0 ) + { + if ( (prices= prices777_find(&inverted,iQ->s.baseid,iQ->s.relid,exchange_str(iQ->exchangeid))) != 0 ) + { + if ( iQ->s.isask != 0 ) + dir = -1; + else dir = 1; + if ( inverted != 0 ) + { + dir *= -1; + volume = (iQ->s.price * iQ->s.vol); + price = 1. / iQ->s.price; + printf("price inverted (%f %f) -> (%f %f)\n",iQ->s.price,iQ->s.vol,price,volume); + } else price = iQ->s.price, volume = iQ->s.vol; + if ( (retstr= automatch(prices,dir,price,volume,NXTaddr,NXTACCTSECRET)) != 0 ) + { + printf("automatched %s isask.%d %f %f (%s)\n",prices->contract,iQ->s.isask,iQ->s.price,iQ->s.vol,retstr); + free(retstr); + } + } + } + } + trades_update(); +} + +int32_t is_specialexchange(char *exchangestr) +{ + if ( strcmp(exchangestr,"InstantDEX") == 0 || strcmp(exchangestr,"jumblr") == 0 || strcmp(exchangestr,"pangea") == 0 || strcmp(exchangestr,"peggy") == 0 || strcmp(exchangestr,"wallet") == 0 || strcmp(exchangestr,"active") == 0 || strncmp(exchangestr,"basket",strlen("basket")) == 0 ) + return(1); + return(0); +} + +char *InstantDEX_placebidask(char *remoteaddr,uint64_t orderid,char *exchangestr,char *name,char *base,char *rel,struct InstantDEX_quote *iQ,char *extra,char *secret,char *activenxt,cJSON *origjson) +{ + struct exchange_info *exchange; cJSON *obj; + char walletstr[256],*str,*retstr = 0; int32_t inverted,dir; struct prices777 *prices; double price,volume; + if ( secret == 0 || activenxt == 0 ) + { + secret = IGUANA_NXTACCTSECRET; + activenxt = IGUANA_NXTADDR; + } +//printf("placebidask.(%s)\n",jprint(origjson,0)); + if ( (obj= jobj(origjson,"wallet")) != 0 ) + { + str = jprint(obj,1); + safecopy(walletstr,str,sizeof(walletstr)); + free(str), str = 0; + } + else walletstr[0] = 0; + if ( exchangestr != 0 && (exchange= exchange_find(exchangestr)) != 0 ) + iQ->exchangeid = exchange->exchangeid; + if ( iQ->exchangeid < 0 || (exchangestr= exchange_str(iQ->exchangeid)) == 0 ) + { + printf("exchangestr.%s id.%d\n",exchangestr,iQ->exchangeid); + return(clonestr("{\"error\":\"exchange not active, check SuperNET.conf exchanges array\"}\n")); + } + //printf("walletstr.(%s)\n",walletstr); + if ( (prices= prices777_find(&inverted,iQ->s.baseid,iQ->s.relid,exchangestr)) == 0 ) + prices = prices777_poll(exchangestr,name,base,iQ->s.baseid,rel,iQ->s.relid); + if ( prices != 0 ) + { + price = iQ->s.price, volume = iQ->s.vol; + if ( price < SMALLVAL || volume < SMALLVAL ) + { + printf("price %f volume %f error\n",price,volume); + return(clonestr("{\"error\":\"prices777_trade invalid price or volume\"}\n")); + } + if ( iQ->s.isask != 0 ) + dir = -1; + else dir = 1; + if ( inverted != 0 ) + { + dir *= -1; + volume *= price; + price = 1. / price; + printf("price inverted (%f %f) -> (%f %f)\n",iQ->s.price,iQ->s.vol,price,volume); + } +//printf("dir.%d price %f vol %f isask.%d remoteaddr.%p\n",dir,price,volume,iQ->s.isask,remoteaddr); + if ( remoteaddr == 0 ) + { + if ( is_specialexchange(exchangestr) == 0 ) + return(prices777_trade(0,0,0,0,1,0,activenxt,secret,prices,dir,price,volume,iQ,0,iQ->s.quoteid,extra)); + //printf("check automatch\n"); + //if ( strcmp(exchangestr,"wallet") != 0 && strcmp(exchangestr,"jumblr") != 0 && strcmp(exchangestr,"pangea") != 0 && iQ->s.automatch != 0 && (SUPERNET.automatch & 1) != 0 && (retstr= automatch(prices,dir,volume,price,activenxt,secret)) != 0 ) + // return(retstr); + if ( strcmp(IGUANA_NXTACCTSECRET,secret) != 0 ) + return(clonestr("{\"error\":\"cant do queued requests with non-default accounts\"}")); + retstr = InstantDEX_str(walletstr,0,1,iQ); + //printf("create_iQ.(%llu) quoteid.%llu walletstr.(%s) %p\n",(long long)iQ->s.offerNXT,(long long)iQ->s.quoteid,walletstr,walletstr); + iQ = create_iQ(iQ,walletstr); + printf("local got create_iQ.(%llu) quoteid.%llu wallet.(%s) baseamount %llu iswallet.%d\n",(long long)iQ->s.offerNXT,(long long)iQ->s.quoteid,walletstr,(long long)iQ->s.baseamount,iQ->s.wallet); + prices777_InstantDEX(prices,MAX_DEPTH); + queue_enqueue("InstantDEX",&InstantDEXQ,queueitem(retstr),0); + } + else + { + iQ = create_iQ(iQ,walletstr); + if ( (retstr= autofill(remoteaddr,iQ,activenxt,secret)) == 0 ) + { + //printf("create_iQ.(%llu) quoteid.%llu\n",(long long)iQ->s.offerNXT,(long long)iQ->s.quoteid); + if ( strcmp(IGUANA_NXTACCTSECRET,secret) != 0 ) + return(clonestr("{\"error\":\"cant do queued requests with non-default accounts\"}")); + prices777_InstantDEX(prices,MAX_DEPTH); + printf("remote got create_iQ.(%llu) quoteid.%llu wallet.(%s) baseamount %llu\n",(long long)iQ->s.offerNXT,(long long)iQ->s.quoteid,walletstr,(long long)iQ->s.baseamount); + } + return(retstr); + } + } else printf("cant find prices\n"); + if ( retstr == 0 ) + retstr = clonestr("{\"error\":\"cant get prices ptr\"}"); + return(retstr); +} + + +#endif +#endif diff --git a/InstantDEX/subatomic.h b/InstantDEX/subatomic.h new file mode 100755 index 000000000..c98e30fd5 --- /dev/null +++ b/InstantDEX/subatomic.h @@ -0,0 +1,1496 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef xcode_subatomic_h +#define xcode_subatomic_h + +//https://bitcointalk.org/index.php?topic=1172153.0 + +#include + +struct bp_key { void *k; }; +typedef struct cstring { + char *str; // string data, incl. NUL + size_t len; // length of string, not including NUL + size_t alloc; // total allocated buffer length +} cstring; + +extern bool bp_key_init(struct bp_key *key); +extern void bp_key_free(struct bp_key *key); +extern bool bp_key_generate(struct bp_key *key); +extern bool bp_privkey_set(struct bp_key *key, const void *privkey, size_t pk_len); +extern bool bp_pubkey_set(struct bp_key *key, const void *pubkey, size_t pk_len); +extern bool bp_key_secret_set(struct bp_key *key, const void *privkey_, size_t pk_len); +extern bool bp_privkey_get(const struct bp_key *key, void **privkey, size_t *pk_len); +extern bool bp_pubkey_get(const struct bp_key *key, void **pubkey, size_t *pk_len); +extern bool bp_key_secret_get(void *p, size_t len, const struct bp_key *key); +extern bool bp_sign(const struct bp_key *key, const void *data, size_t data_len,void **sig_, size_t *sig_len_); +extern bool bp_verify(const struct bp_key *key, const void *data, size_t data_len,const void *sig, size_t sig_len); + +void cstr_free(cstring *s, bool free_buf); +cstring *base58_encode_check(unsigned char addrtype,bool have_addrtype,const void *data,size_t data_len); +cstring *base58_decode_check(unsigned char *addrtype, const char *s_in); +int32_t btc_setprivkey(struct bp_key *key,char *privkeystr); +int32_t btc_getpubkey(char pubkeystr[67],uint8_t pubkeybuf[33],struct bp_key *key); +void btc_freekey(void *key); + +struct btcaddr +{ + struct bp_key key; + uint8_t *pubkey; uint16_t p2sh; + char addr[36],coin[8]; + uint8_t privkey[280]; +}; + +#define SCRIPT_OP_IF 0x63 +#define SCRIPT_OP_ELSE 0x67 +#define SCRIPT_OP_DUP 0x76 +#define SCRIPT_OP_ENDIF 0x68 +#define SCRIPT_OP_TRUE 0x51 +#define SCRIPT_OP_2 0x52 +#define SCRIPT_OP_3 0x53 +#define SCRIPT_OP_EQUALVERIFY 0x88 +#define SCRIPT_OP_HASH160 0xa9 +#define SCRIPT_OP_EQUAL 0x87 +#define SCRIPT_OP_CHECKSIG 0xac +#define SCRIPT_OP_CHECKMULTISIG 0xae +#define SCRIPT_OP_CHECKMULTISIGVERIFY 0xaf + +char *create_atomictx_scripts(uint8_t addrtype,char *scriptPubKey,char *p2shaddr,char *pubkeyA,char *pubkeyB,char *hash160str) +{ + // if ( refund ) OP_HASH160 <2of2 multisig hash> OP_EQUAL // standard multisig + // else OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG // standard spend + cstring *btc_addr; char *retstr; uint8_t pubkeyAbytes[33],pubkeyBbytes[33],hash160[20],tmpbuf[24],hex[4096]; int32_t i,n = 0; + decode_hex(pubkeyAbytes,33,pubkeyA); + decode_hex(pubkeyBbytes,33,pubkeyB); + decode_hex(hash160,20,hash160str); + hex[n++] = SCRIPT_OP_IF; + hex[n++] = SCRIPT_OP_2; + hex[n++] = 33, memcpy(&hex[n],pubkeyAbytes,33), n += 33; + hex[n++] = 33, memcpy(&hex[n],pubkeyBbytes,33), n += 33; + hex[n++] = SCRIPT_OP_2; + hex[n++] = SCRIPT_OP_CHECKMULTISIG; + hex[n++] = SCRIPT_OP_ELSE; + hex[n++] = SCRIPT_OP_DUP; + hex[n++] = SCRIPT_OP_HASH160; + hex[n++] = 20; memcpy(&hex[n],hash160,20); n += 20; + hex[n++] = SCRIPT_OP_EQUALVERIFY; + hex[n++] = SCRIPT_OP_CHECKSIG; + hex[n++] = SCRIPT_OP_ENDIF; + if ( (retstr= calloc(1,n*2+16)) == 0 ) + return(0); + //printf("pubkeyA.(%s) pubkeyB.(%s) hash160.(%s) ->\n",pubkeyA,pubkeyB,hash160str); + //strcpy(retstr,"01"); + //sprintf(retstr+2,"%02x",n); + for (i=0; i>4) & 0xf); + retstr[i*2 + 1] = hexbyte(hex[i] & 0xf); + //printf("%02x",hex[i]); + } + retstr[n*2] = 0; + calc_OP_HASH160(scriptPubKey,tmpbuf+2,retstr); + tmpbuf[0] = SCRIPT_OP_HASH160; + tmpbuf[1] = 20; + tmpbuf[22] = SCRIPT_OP_EQUAL; + init_hexbytes_noT(scriptPubKey,tmpbuf,23); + if ( p2shaddr != 0 ) + { + p2shaddr[0] = 0; + if ( (btc_addr= base58_encode_check(addrtype,true,tmpbuf+2,20)) != 0 ) + { + if ( strlen(btc_addr->str) < 36 ) + strcpy(p2shaddr,btc_addr->str); + cstr_free(btc_addr,true); + } + } + return(retstr); +} + +#ifdef noiguana +int32_t create_MofN(uint8_t addrtype,char *redeemScript,char *scriptPubKey,char *p2shaddr,char *pubkeys[],int32_t M,int32_t N) +{ + cstring *btc_addr; uint8_t pubkey[33],tmpbuf[24],hex[4096]; int32_t i,n = 0; + hex[n++] = 0x50 + M; + for (i=0; i>4) & 0xf); + redeemScript[i*2 + 1] = hexbyte(hex[i] & 0xf); + //fprintf(stderr,"%02x",hex[i]); + } + //fprintf(stderr," n.%d\n",n); + redeemScript[n*2] = 0; + calc_OP_HASH160(0,tmpbuf+2,redeemScript); + //printf("op160.(%s)\n",redeemScript); + tmpbuf[0] = SCRIPT_OP_HASH160; + tmpbuf[1] = 20; + tmpbuf[22] = SCRIPT_OP_EQUAL; + init_hexbytes_noT(scriptPubKey,tmpbuf,23); + p2shaddr[0] = 0; + if ( (btc_addr= base58_encode_check(addrtype,true,tmpbuf+2,20)) != 0 ) + { + if ( strlen(btc_addr->str) < 36 ) + strcpy(p2shaddr,btc_addr->str); + cstr_free(btc_addr,true); + } + return(n); +} + +struct btcaddr *btcaddr_new(char *coinstr,char *p2sh_script) +{ + uint8_t script[8192],md160[20]; char pubkeystr[512],privkeystr[512],hashstr[41]; struct coin777 *coin; + void *privkey=0,*pubkey=0; int32_t n; size_t len,slen; cstring *btc_addr; struct btcaddr *btc; + if ( (btc= calloc(1,sizeof(*btc))) == 0 || (coin = coin777_find(coinstr,1)) == 0 ) + { + if ( btc != 0 ) + free(btc); + return(0); + } + strncpy(btc->coin,coin->name,sizeof(btc->coin)-1); + if ( p2sh_script != 0 ) + { + calc_OP_HASH160(0,md160,p2sh_script); + btc->p2sh = n = (int32_t)strlen(p2sh_script) >> 1; + decode_hex(script,n,p2sh_script); + if ( (btc_addr= base58_encode_check(coin->p2shtype,true,md160,sizeof(md160))) != 0 ) + { + if ( n > sizeof(btc->privkey)-23 ) + { + printf("script.(%s) len.%d is too big\n",p2sh_script,n); + free(btc); + return(0); + } + strcpy(btc->addr,btc_addr->str); + memcpy(btc->privkey,script,n); + btc->pubkey = &btc->privkey[sizeof(btc->privkey) - 23]; + btc->pubkey[0] = SCRIPT_OP_HASH160; + btc->pubkey[2] = 20; + memcpy(&btc->pubkey[2],md160,20); + btc->pubkey[22] = SCRIPT_OP_EQUAL; + init_hexbytes_noT(privkeystr,script,n); + printf("type.%u btcaddr.%ld addr.(%s) %ld p2sh.(%s) %d\n",coin->p2shtype,(long)sizeof(struct btcaddr),btc->addr,(long)strlen(btc->addr),privkeystr,n); + cstr_free(btc_addr,true); + } else free(btc), btc = 0; + return(btc); + } + else if ( bp_key_init(&btc->key) != 0 && bp_key_generate(&btc->key) != 0 && bp_pubkey_get(&btc->key,&pubkey,&len) != 0 && bp_privkey_get(&btc->key,&privkey,&slen) != 0 ) + { + if ( len == 33 && slen == 214 && memcmp((void *)((long)privkey + slen - 33),pubkey,33) == 0 ) + { + init_hexbytes_noT(pubkeystr,pubkey,len); + init_hexbytes_noT(privkeystr,privkey,slen); + calc_OP_HASH160(hashstr,md160,pubkeystr); + if ( (btc_addr= base58_encode_check(coin->addrtype,true,md160,sizeof(md160))) != 0 ) + { + strcpy(btc->addr,btc_addr->str); + memcpy(btc->privkey,privkey,slen); + btc->pubkey = &btc->privkey[slen - len]; + printf("type.%u btcaddr.%ld rmd160.(%s) addr.(%s) %ld pubkey.(%s) %d privkey.(%s) %d\n",coin->addrtype,(long)sizeof(struct btcaddr),hashstr,btc->addr,(long)strlen(btc->addr),pubkeystr,(int32_t)len,privkeystr,(int32_t)slen); + cstr_free(btc_addr,true); + } + else free(btc), btc = 0; + } else free(btc), btc = 0; + } + return(btc); +} + +int32_t btc_getpubkey(char pubkeystr[67],uint8_t pubkeybuf[33],struct bp_key *key) +{ + void *pubkey = 0; size_t len = 0; + bp_pubkey_get(key,&pubkey,&len); + if ( pubkey != 0 ) + { + if ( pubkeystr != 0 ) + { + if ( len < 34 ) + { + init_hexbytes_noT(pubkeystr,pubkey,(int32_t)len); + memcpy(pubkeybuf,pubkey,len); + } + else printf("btc_getpubkey error len.%d\n",(int32_t)len), len = -1; + } + //printf("btc_getpubkey len.%ld (%s).%p\n",len,pubkeystr,pubkeystr); + } else len = -1; + return((int32_t)len); +} + +int32_t btc_convrmd160(char *coinaddr,uint8_t addrtype,uint8_t md160[20]) +{ + cstring *btc_addr; + if ( (btc_addr= base58_encode_check(addrtype,true,md160,20)) != 0 ) + { + strcpy(coinaddr,btc_addr->str); + cstr_free(btc_addr,true); + return(0); + } + return(-1); +} + +int32_t btc_coinaddr(char *coinaddr,uint8_t addrtype,char *pubkeystr) +{ + uint8_t rmd160[20]; char hashstr[41]; + calc_OP_HASH160(hashstr,rmd160,pubkeystr); + return(btc_convrmd160(coinaddr,addrtype,rmd160)); +} + +int32_t btc_convaddr(char *hexaddr,char *addr58) +{ + uint8_t addrtype; cstring *cstr; + if ( (cstr= base58_decode_check(&addrtype,(const char *)addr58)) != 0 ) + { + sprintf(hexaddr,"%02x",addrtype); + init_hexbytes_noT(hexaddr+2,(void *)cstr->str,cstr->len); + cstr_free(cstr,true); + return(0); + } + return(-1); +} + +int32_t btc_priv2wip(char *wipstr,uint8_t privkey[32],uint8_t addrtype) +{ + uint8_t tmp[128]; char hexstr[67]; cstring *btc_addr; + memcpy(tmp,privkey,32); + tmp[32] = 1; + init_hexbytes_noT(hexstr,tmp,32); + if ( (btc_addr= base58_encode_check(addrtype,true,tmp,33)) != 0 ) + { + strcpy(wipstr,btc_addr->str); + cstr_free(btc_addr,true); + } + printf("-> (%s) -> wip.(%s) addrtype.%02x\n",hexstr,wipstr,addrtype); + return(0); +} + +int32_t btc_wip2priv(uint8_t privkey[32],char *wipstr) +{ + uint8_t addrtype; cstring *cstr; int32_t len = -1; + if ( (cstr= base58_decode_check(&addrtype,(const char *)wipstr)) != 0 ) + { + init_hexbytes_noT((void *)privkey,(void *)cstr->str,cstr->len); + if ( cstr->str[cstr->len-1] == 0x01 ) + cstr->len--; + memcpy(privkey,cstr->str,cstr->len); + len = (int32_t)cstr->len; + char tmp[138]; + btc_priv2wip(tmp,privkey,addrtype); + printf("addrtype.%02x wipstr.(%llx) len.%d\n",addrtype,*(long long *)privkey,len); + cstr_free(cstr,true); + } + return(len); +} + +int32_t btc_setprivkey(struct bp_key *key,char *privkeystr) +{ + uint8_t privkey[512]; int32_t len = btc_wip2priv(privkey,privkeystr); + if ( len < 0 || bp_key_init(key) == 0 || bp_key_secret_set(key,privkey,len) == 0 ) + { + printf("error setting privkey\n"); + return(-1); + } + return(0); +} + +void jumblr_freekey(void *key) +{ + bp_key_free(key); + free(key); +} + +int32_t btc_priv2pub(uint8_t pubkey[33],uint8_t privkey[32]) +{ + size_t len; void *pub = 0; int32_t retval = -1; + struct bp_key *key = calloc(1,sizeof(*key)); + if ( key != 0 && bp_key_init(key) != 0 && bp_key_secret_set(key,privkey,32) != 0 ) + { + bp_pubkey_get(key,&pub,&len); + bp_key_free(key); + if ( len == 33 ) + memcpy(pubkey,pub,33); + if ( pub != 0 ) + free(pub); + return(retval); + } + if ( key != 0 ) + bp_key_free(key); + return(retval); +} + +int32_t btc_pub2rmd(uint8_t rmd160[20],uint8_t pubkey[33]) +{ + char pubkeystr[67],hashstr[41]; + init_hexbytes_noT(pubkeystr,pubkey,33); + calc_OP_HASH160(hashstr,rmd160,pubkeystr); + return(0); +} + +void *jumblr_bpkey(char *pubP,struct coin777 *coin,char *coinaddr) +{ + uint8_t buf[2048]; char *privkey; struct bp_key *key = 0; + //printf("coin.%s (%s)\n",coin->name,coinaddr); + if ( (privkey = dumpprivkey(coin->name,coin->serverport,coin->userpass,coinaddr)) != 0 ) + { + //printf("privkey.(%s)\n",privkey); + key = calloc(1,sizeof(*key)); + if ( key != 0 && btc_setprivkey(key,privkey) == 0 && btc_getpubkey(pubP,buf,key) > 0 ) + return(key); + btc_freekey(key); + } + return(0); +} + +void set_spendscript(char *spendscript,char *coinaddr) +{ + char hexaddr[128]; + btc_convaddr(hexaddr,coinaddr); + sprintf(spendscript,"76a914%s88ac",hexaddr+2); +} + +int32_t script_coinaddr(char *coinaddr,cJSON *scriptobj) +{ + struct destbuf buf; cJSON *addresses; + coinaddr[0] = 0; + if ( scriptobj == 0 ) + return(-1); + if ( (addresses= cJSON_GetObjectItem(scriptobj,"addresses")) != 0 ) + { + copy_cJSON(&buf,jitem(addresses,0)); + strcpy(coinaddr,buf.buf); + return(0); + } + return(-1); +} + +char *pangea_signp2sh(int32_t oldtx_format,struct cointx_info *refT,int32_t redeemi,char *redeemscript,char sigs[][256],int32_t n,uint8_t privkey[32],int32_t privkeyind) +{ + char hexstr[16384]; bits256 hash2; uint8_t data[4096],sigbuf[512]; struct bp_key key; + struct cointx_info *T; int32_t i,len; void *sig = NULL; size_t siglen = 0; struct cointx_input *vin; + if ( bp_key_init(&key) != 0 && bp_key_secret_set(&key,privkey,32) != 0 ) + { + if ( (T= calloc(1,sizeof(*T))) == 0 ) + return(0); + *T = *refT; vin = &T->inputs[redeemi]; + for (i=0; inuminputs; i++) + strcpy(T->inputs[i].sigs,"00"); + strcpy(vin->sigs,redeemscript); + vin->sequence = (uint32_t)-1; + T->nlocktime = 0; + //disp_cointx(&T); + emit_cointx(&hash2,data,sizeof(data),T,oldtx_format,SIGHASH_ALL); + //printf("HASH2.(%llx)\n",(long long)hash2.txid); + if ( bp_sign(&key,hash2.bytes,sizeof(hash2),&sig,&siglen) != 0 ) + { + memcpy(sigbuf,sig,siglen); + sigbuf[siglen++] = SIGHASH_ALL; + init_hexbytes_noT(sigs[privkeyind],sigbuf,(int32_t)siglen); + strcpy(vin->sigs,"00"); + for (i=0; isigs + strlen(vin->sigs),"%02x%s",(int32_t)strlen(sigs[i])>>1,sigs[i]); + //printf("(%s).%ld ",sigs[i],strlen(sigs[i])); + } + } + len = (int32_t)(strlen(redeemscript)/2); + if ( len >= 0xfd ) + sprintf(&vin->sigs[strlen(vin->sigs)],"4d%02x%02x",len & 0xff,(len >> 8) & 0xff); + else sprintf(&vin->sigs[strlen(vin->sigs)],"4c%02x",len); + sprintf(&vin->sigs[strlen(vin->sigs)],"%s",redeemscript); + //printf("after A.(%s) othersig.(%s) siglen.%02lx -> (%s)\n",hexstr,othersig != 0 ? othersig : "",siglen,vin->sigs); + //printf("vinsigs.(%s) %ld\n",vin->sigs,strlen(vin->sigs)); + _emit_cointx(hexstr,sizeof(hexstr),T,oldtx_format); + //disp_cointx(&T); + free(T); + return(clonestr(hexstr)); + } + else printf("error signing\n"); + free(T); + } + return(0); +} + +uint64_t jumblr_getcoinaddr(char *coinaddr,struct destbuf *scriptPubKey,struct coin777 *coin,char *txid,int32_t vout) +{ + char *rawtransaction,*txidstr,*asmstr; uint64_t value = 0; int32_t n,m,len,reqSigs; cJSON *json,*scriptobj,*array,*item,*hexobj; + scriptPubKey->buf[0] = 0; + if ( (rawtransaction= _get_transaction(coin->name,coin->serverport,coin->userpass,txid)) == 0 ) + { + printf("jumblr_getprivkey: error getting (%s)\n",txid); + return(0); + } + if ( (json= cJSON_Parse(rawtransaction)) != 0 ) + { + if ( (txidstr= jstr(json,"txid")) == 0 || strcmp(txidstr,txid) != 0 ) + { + printf("jumblr_getcoinaddr no txid or mismatch\n"); + free_json(json); + free(rawtransaction); + return(0); + } + if ( (array= jarray(&n,json,"vout")) != 0 && vout < n && (item= jitem(array,vout)) != 0 ) + { + reqSigs = (int32_t)get_cJSON_int(item,"reqSigs"); + value = conv_cJSON_float(item,"value"); + scriptobj = cJSON_GetObjectItem(item,"scriptPubKey"); + printf("ITEM.(%s)\n",jprint(item,0)); + if ( scriptobj != 0 ) + { + printf("script.(%s)\n",jprint(scriptobj,0)); + script_coinaddr(coinaddr,scriptobj); + hexobj = cJSON_GetObjectItem(scriptobj,"hex"); + if ( scriptPubKey != 0 && hexobj != 0 ) + copy_cJSON(scriptPubKey,hexobj); + else + { + // OP_DUP OP_HASH160 f563e867027dedd109c9bb5f3354c3cc41dc7c7f OP_EQUALVERIFY OP_CHECKSIG + // 0318d4f6cdcbe6c822b979fc318dbe4ad58287223c8fb57b7bec0c88cd58a4b16a OP_CHECKSIG + if ( (asmstr= jstr(scriptobj,"asm")) != 0 ) + { + len = (int32_t)strlen(asmstr); + m = (int32_t)strlen(" OP_EQUALVERIFY OP_CHECKSIG"); + if ( strncmp(asmstr,"OP_DUP OP_HASH160 ",strlen("OP_DUP OP_HASH160 ")) == 0 && strcmp(&asmstr[len - m]," OP_EQUALVERIFY OP_CHECKSIG") == 0 ) + set_spendscript(scriptPubKey->buf,coinaddr); + else + { + printf("nonstandard.(%s)\n",&asmstr[len - m]); + m = (int32_t)strlen(" OP_CHECKSIG"); + if ( strcmp(&asmstr[len - m]," OP_CHECKSIG") == 0 ) + { + printf("key sig (%s)\n",asmstr); + sprintf(scriptPubKey->buf,"%02x",(len-m)/2); + memcpy(&scriptPubKey->buf[2],asmstr,(len - m)); + scriptPubKey->buf[2 + (len - m)] = 0; + strcat(scriptPubKey->buf,"ac"); + } + } + } + } + } else printf("null scriptobj.%p (%s)\n",scriptobj,coinaddr); + } + free_json(json); + } + free(rawtransaction); + return(value); +} + +char *jumblr_getprivkey(uint64_t *valuep,struct destbuf *scriptPubKey,uint32_t *locktimep,struct coin777 *coin,char *txid,int32_t vout) +{ + char *rawtransaction,*txidstr,*privkey=0,coinaddr[64]; uint64_t value = 0; int32_t n,reqSigs; cJSON *json,*scriptobj,*array,*item,*hexobj; + *locktimep = -1; + scriptPubKey->buf[0] = 0; + if ( (rawtransaction= _get_transaction(coin->name,coin->serverport,coin->userpass,txid)) == 0 ) + { + printf("jumblr_getprivkey: error getting (%s)\n",txid); + return(0); + } + if ( (json= cJSON_Parse(rawtransaction)) != 0 )//get_decoderaw_json(coin,rawtransaction)) != 0 ) + { + *locktimep = (int32_t)get_cJSON_int(json,"locktime"); + if ( (txidstr= jstr(json,"txid")) == 0 || strcmp(txidstr,txid) != 0 ) + { + printf("jumblr_getprivkey no txid or mismatch\n"); + free_json(json); + free(rawtransaction); + return(0); + } + //printf("txidstr.(%s) vout.%d\n",txidstr,vout); + if ( (array= jarray(&n,json,"vout")) != 0 && (item= jitem(array,vout)) != 0 ) + { + scriptobj = cJSON_GetObjectItem(item,"scriptPubKey"); + if ( scriptobj != 0 && script_coinaddr(coinaddr,scriptobj) == 0 ) + { + reqSigs = (int32_t)get_cJSON_int(item,"reqSigs"); + value = conv_cJSON_float(item,"value"); + hexobj = cJSON_GetObjectItem(scriptobj,"hex"); + if ( scriptPubKey != 0 && hexobj != 0 ) + copy_cJSON(scriptPubKey,hexobj); + privkey = dumpprivkey(coin->name,coin->serverport,coin->userpass,coinaddr); + } else printf("null scriptobj.%p (%s)\n",scriptobj,coinaddr); + } + free_json(json); + } + free(rawtransaction); + if ( valuep != 0 ) + *valuep = value; + return(privkey); +} + +cJSON *cointx_vins_json_params(struct coin777 *coin,char *rawbytes) +{ + int32_t i; cJSON *json,*array; char coinaddr[128]; struct destbuf scriptPubKey; struct cointx_info *cointx; + array = cJSON_CreateArray(); + printf("convert.(%s)\n",rawbytes); + if ( (cointx= _decode_rawtransaction(rawbytes,coin->mgw.oldtx_format)) != 0 ) + { + disp_cointx(cointx); + for (i=0; inuminputs; i++) + { + json = cJSON_CreateObject(); + jaddstr(json,"txid",cointx->inputs[i].tx.txidstr); + jaddnum(json,"vout",cointx->inputs[i].tx.vout); + if ( cointx->inputs[i].sigs[0] != 0 ) + jaddstr(json,"scriptPubKey",cointx->inputs[i].sigs); + else + { + jumblr_getcoinaddr(coinaddr,&scriptPubKey,coin,cointx->inputs[i].tx.txidstr,cointx->inputs[i].tx.vout); + jaddstr(json,"scriptPubKey",scriptPubKey.buf); + } + cJSON_AddItemToArray(array,json); + } + free(cointx); + } + return(array); +} + +char *jumblr_signraw_json_params(struct coin777 *coin,char *rawbytes) +{ + char *paramstr = 0; cJSON *array,*rawobj,*vinsobj;//,*keysobj;char *coinaddrs[MAX_SUBATOMIC_INPUTS+1], + if ( (rawobj= cJSON_CreateString(rawbytes)) != 0 ) + { + if ( (vinsobj= cointx_vins_json_params(coin,rawbytes)) != 0 ) + { + array = cJSON_CreateArray(); + jaddi(array,rawobj); + jaddi(array,vinsobj); + //cJSON_AddItemToArray(array,keysobj); + paramstr = jprint(array,1); + } + else free_json(rawobj); + } + return(paramstr); +} + +int32_t jumblr_signtx(char *signedtx,unsigned long destsize,struct coin777 *coin,char *signparams) +{ + cJSON *json,*compobj; char *retstr,*deststr; uint32_t completed = 0; + signedtx[0] = 0; + //printf("cp.%d vs %d: subatomic_signtx rawbytes.(%s)\n",cp->coinid,coinid,rawbytes); + if ( coin != 0 && signparams != 0 ) + { + _stripwhite(signparams,' '); + printf("got signparams.(%s)\n",signparams); + if ( (retstr= bitcoind_RPC(0,coin->name,coin->serverport,coin->userpass,"signrawtransaction",signparams)) != 0 ) + { + //printf("got retstr.(%s)\n",retstr); + if ( (json= cJSON_Parse(retstr)) != 0 ) + { + if ( (deststr= jstr(json,"hex")) != 0 ) + { + compobj = cJSON_GetObjectItem(json,"complete"); + if ( compobj != 0 ) + completed = ((compobj->type&0xff) == cJSON_True); + if ( strlen(deststr) > destsize ) + printf("sign_rawtransaction: strlen(deststr) %ld > %ld destize\n",(long)strlen(deststr),destsize); + else strcpy(signedtx,deststr); + } else printf("cant get hex from.(%s)\n",retstr); + free_json(json); + } else printf("json parse error.(%s)\n",retstr); + free(retstr); + } else printf("error signing rawtx\n"); + } else printf("error generating signparams\n"); + return(completed); +} + +char *jumblr_signvin(char *sigstr,struct coin777 *coin,char *signedtx,int32_t bufsize,void *bpkey,char *pubP,struct cointx_info *refT,int32_t redeemi,char *rawtx) +{ + // signrawtransaction [{"txid":txid,"vout":n,"scriptPubKey":hex},...] [,...] + char hexstr[4096],redeem[2048]; bits256 hash2; uint8_t *data,sigbuf[1024]; + struct cointx_info *T; int32_t i; void *sig = NULL; size_t siglen = 0; struct cointx_input *vin; + sigstr[0] = 0; + if ( 1 ) + { + char *paramstr; cJSON *vinarray,*item,*array = cJSON_CreateArray(); + vinarray = cJSON_CreateArray(); + jaddistr(array,rawtx); + for (i=0; inuminputs; i++) + { + vin = &refT->inputs[i]; + item = cJSON_CreateObject(); + jaddstr(item,"txid",vin->tx.txidstr); + jaddnum(item,"vout",vin->tx.vout); + jaddstr(item,"scriptPubKey",vin->sigs); + jaddi(vinarray,item); + } + jaddi(array,vinarray); + paramstr = jprint(array,1); + if ( jumblr_signtx(signedtx,bufsize,coin,paramstr) > 0 ) + printf("SIGS completed\n"); + if ( signedtx[0] != 0 ) + { + if ( (T= _decode_rawtransaction(signedtx,coin->mgw.oldtx_format)) != 0 ) + { + strcpy(sigstr,T->inputs[redeemi].sigs); + free(T); + return(sigstr); + } + } + return(0); + } + if ( (T = calloc(1,sizeof(*T))) == 0 ) + { + printf("unexpected out of mem in jumblr_signvin\n"); + return(0); + } + + *T = *refT; + vin = &T->inputs[redeemi]; + safecopy(redeem,vin->sigs,sizeof(redeem)); + fprintf(stderr,"redeemi.%d numinputs.%d\n",redeemi,T->numinputs); + for (i=0; inuminputs; i++) + if ( i != redeemi ) + strcpy(T->inputs[i].sigs,"00"); + vin->sequence = (uint32_t)-1; + T->nlocktime = 0; + data = malloc(65536); + disp_cointx(T); + emit_cointx(&hash2,data,sizeof(data),T,coin->mgw.oldtx_format,SIGHASH_ALL); + free(data); + if ( bp_sign(bpkey,hash2.bytes,sizeof(hash2),&sig,&siglen) != 0 && sig != 0 ) + { + memcpy(sigbuf,sig,siglen); + free(sig); + sigbuf[siglen++] = SIGHASH_ALL; + init_hexbytes_noT(hexstr,sigbuf,(int32_t)siglen); + sprintf(vin->sigs,"%02x%s%02x%s",(uint32_t)siglen,hexstr,(uint32_t)strlen(pubP)/2,pubP); + strcpy(sigstr,vin->sigs); + printf("after P.(%s) siglen.%02x -> %s pubP.(%s)\n",sigstr,(uint32_t)siglen,vin->sigs,pubP); + } + free(T); + if ( sigstr[0] != 0 ) + return(sigstr); + else return(0); +} + +int32_t script_has_coinaddr(cJSON *scriptobj,char *coinaddr) +{ + int32_t i,n; struct destbuf buf; cJSON *addresses,*addrobj; + if ( scriptobj == 0 ) + return(0); + addresses = cJSON_GetObjectItem(scriptobj,"addresses"); + if ( addresses != 0 ) + { + n = cJSON_GetArraySize(addresses); + for (i=0; iname,coin->serverport,coin->userpass,"decoderawtransaction",str)) != 0 && retstr[0] != 0 ) + { + //printf("got decodetransaction.(%s)\n",retstr); + json = cJSON_Parse(retstr); + } else printf("error decoding.(%s)\n",str); + if ( retstr != 0 ) + free(retstr); + free(str); + return(json); +} + +char *subatomic_decodetxid(int64_t *valuep,struct destbuf *scriptPubKey,uint32_t *locktimep,struct coin777 *coin,char *rawtransaction,char *mycoinaddr) +{ + char *txidstr,checkasmstr[1024],*asmstr,*txid = 0; uint64_t value = 0; int32_t i,n,nval,reqSigs; cJSON *json,*scriptobj,*array,*item,*hexobj; + *locktimep = -1; + if ( (json= get_decoderaw_json(coin,rawtransaction)) != 0 ) + { + *locktimep = (int32_t)get_cJSON_int(json,"locktime"); + if ( (txidstr= jstr(json,"txid")) == 0 ) + { + printf("subatomic_decodetxid no txid\n"); + return(0); + } + txid = clonestr(txidstr); + array = cJSON_GetObjectItem(json,"vout"); + if ( mycoinaddr != 0 && is_cJSON_Array(array) != 0 ) + { + n = cJSON_GetArraySize(array); + for (i=0; inuminputs; i++) + { + up = &rp->inputs[i]; + json = cJSON_CreateObject(); + jaddstr(json,"txid",up->txid.buf); + jaddnum(json,"vout",up->vout); + if ( up->scriptPubKey.buf[0] != 0 ) + jaddstr(json,"scriptPubKey",up->scriptPubKey.buf); + if ( up->redeemScript.buf[0] != 0 ) + jaddstr(json,"redeemScript",up->redeemScript.buf); + cJSON_AddItemToArray(array,json); + } + return(array); +} + +cJSON *subatomic_privkeys_json_params(struct coin777 *coin,char **coinaddrs,int32_t n) +{ + int32_t i; char *privkey; cJSON *array = cJSON_CreateArray(); + //sprintf(walletkey,"[\"%s\",%d]",Global_subatomic->NXTADDR,BITCOIN_WALLET_UNLOCKSECONDS); + // locking first avoids error, hacky but no time for wallet fiddling now + //bitcoind_RPC(0,coin->name,coin->serverport,coin->userpass,"walletlock",0); + //bitcoind_RPC(0,coin->name,coin->serverport,coin->userpass,"walletpassphrase",walletkey); + for (i=0; iname,coin->serverport,coin->userpass,coinaddrs[i])) != 0 ) + { + jaddistr(array,privkey); + free(privkey); + } + } + } + return(array); +} + +char *subatomic_signraw_json_params(char *skipaddr,char *coinaddr,struct coin777 *coin,struct subatomic_rawtransaction *rp,char *rawbytes) +{ + int32_t i,j,flag; char *coinaddrs[MAX_SUBATOMIC_INPUTS+1],*paramstr = 0; cJSON *array,*rawobj,*vinsobj,*keysobj; + if ( (rawobj= cJSON_CreateString(rawbytes)) != 0 ) + { + if ( (vinsobj= subatomic_vins_json_params(coin,rp)) != 0 ) + { + // printf("add %d inputs skipaddr.%s coinaddr.%s\n",rp->numinputs,skipaddr,coinaddr); + for (i=flag=j=0; inuminputs; i++) + { + if ( skipaddr == 0 || strcmp(rp->inputs[i].address.buf,skipaddr) != 0 ) + { + printf("i.%d j.%d flag.%d %s\n",i,j,flag,rp->inputs[i].address.buf); + coinaddrs[j] = rp->inputs[i].address.buf; + if ( coinaddr != 0 && strcmp(coinaddrs[j],coinaddr) == 0 ) + flag++; + j++; + } + } + //printf("i.%d j.%d flag.%d\n",i,j,flag); + //if ( coinaddr != 0 && flag == 0 ) + //coinaddrs[j++] = coinaddr; + coinaddrs[j] = 0; + keysobj = subatomic_privkeys_json_params(coin,coinaddrs,j); + if ( keysobj != 0 ) + { + array = cJSON_CreateArray(); + cJSON_AddItemToArray(array,rawobj); + cJSON_AddItemToArray(array,vinsobj); + cJSON_AddItemToArray(array,keysobj); + paramstr = cJSON_Print(array); + free_json(array); + } + else free_json(vinsobj); + } + else free_json(rawobj); + } + return(paramstr); +} + +char *subatomic_signtx(char *skipaddr,uint32_t *lockedblockp,int64_t *valuep,char *coinaddr,char *signedtx,unsigned long destsize,struct coin777 *coin,struct subatomic_rawtransaction *rp,char *rawbytes) +{ + cJSON *json,*compobj; char *retstr,*deststr,*signparams,*txid = 0; uint32_t locktime = 0; + rp->txid[0] = signedtx[0] = 0; + rp->completed = -1; + //printf("cp.%d vs %d: subatomic_signtx rawbytes.(%s)\n",cp->coinid,coinid,rawbytes); + if ( coin != 0 && (signparams= subatomic_signraw_json_params(skipaddr,coinaddr,coin,rp,rawbytes)) != 0 ) + { + _stripwhite(signparams,' '); + //printf("got signparams.(%s)\n",signparams); + if ( (retstr= bitcoind_RPC(0,coin->name,coin->serverport,coin->userpass,"signrawtransaction",signparams)) != 0 ) + { + //printf("got retstr.(%s)\n",retstr); + if ( (json= cJSON_Parse(retstr)) != 0 ) + { + if ( (deststr= jstr(json,"hex")) != 0 ) + { + compobj = cJSON_GetObjectItem(json,"complete"); + if ( compobj != 0 ) + rp->completed = ((compobj->type&0xff) == cJSON_True); + if ( strlen(deststr) > destsize ) + printf("sign_rawtransaction: strlen(deststr) %ld > %ld destize\n",(long)strlen(deststr),destsize); + else + { + strcpy(signedtx,deststr); + txid = subatomic_decodetxid(valuep,0,&locktime,coin,deststr,coinaddr); + if ( txid != 0 ) + { + safecopy(rp->txid,txid,sizeof(rp->txid)); + free(txid); + txid = rp->txid; + } + // printf("got signedtransaction -> txid.(%s) %.8f\n",rp->txid,dstr(valuep!=0?*valuep:0)); + } + } else printf("cant get hex from.(%s)\n",retstr); + free_json(json); + } else printf("json parse error.(%s)\n",retstr); + free(retstr); + } else printf("error signing rawtx\n"); + free(signparams); + } else printf("error generating signparams\n"); + if ( lockedblockp != 0 ) + *lockedblockp = locktime; + return(txid); +} + +cJSON *subatomic_vouts_json_params(struct subatomic_rawtransaction *rp) +{ + int32_t i; cJSON *json,*obj; + json = cJSON_CreateObject(); + for (i=0; inumoutputs; i++) + { + obj = cJSON_CreateNumber((double)rp->destamounts[i]/SATOSHIDEN); + cJSON_AddItemToObject(json,rp->destaddrs[i],obj); + } + // printf("numdests.%d (%s)\n",rp->numoutputs,cJSON_Print(json)); + return(json); +} + +char *subatomic_rawtxid_json(struct coin777 *coin,struct subatomic_rawtransaction *rp) +{ + char *paramstr = 0; cJSON *array,*vinsobj,*voutsobj; + if ( (vinsobj= subatomic_vins_json_params(coin,rp)) != 0 ) + { + if ( (voutsobj= subatomic_vouts_json_params(rp)) != 0 ) + { + array = cJSON_CreateArray(); + cJSON_AddItemToArray(array,vinsobj); + cJSON_AddItemToArray(array,voutsobj); + paramstr = cJSON_Print(array); + free_json(array); // this frees both vinsobj and voutsobj + } + else free_json(vinsobj); + } + // printf("subatomic_rawtxid_json.%s\n",paramstr); + return(paramstr); +} + +uint64_t subatomic_donation(struct coin777 *coin,uint64_t amount) +{ + uint64_t donation = 0; + if ( coin->donationaddress[0] != 0 ) + { + donation = amount >> 11; + if ( donation < coin->mgw.txfee ) + donation = coin->mgw.txfee; + } + return(donation); +} + +char *gather_account_addresses(struct coin777 *coin,char *account) +{ + cJSON *array,*retarray,*subarray,*item; int32_t i,j,m,n; char *acct; + //printf("call listaddressgroupings\n"); + if ( (array= _get_localaddresses(coin->name,coin->serverport,coin->userpass)) != 0 ) + { + retarray = cJSON_CreateArray(); + n = cJSON_GetArraySize(array); + for (i=0; i 0 ) + { + for (j=0; j 2 ) + { + if ( (acct= jstr(jitem(item,2),0)) != 0 && strcmp(acct,account) == 0 ) + { + //printf("gather.(%s) %s\n",jstr(jitem(item,0),0),account); + jaddistr(retarray,jstr(jitem(item,0),0)); + } + } //else printf("skip item.%p, %d %d\n",item,is_cJSON_Array(item),cJSON_GetArraySize(item)); + } + } + } + } + free_json(array); + if ( cJSON_GetArraySize(retarray) == 0 ) + { + free_json(retarray); + return(0); + } + else return(jprint(retarray,1)); + } + else return(0); +} + +struct subatomic_unspent_tx *gather_unspents(uint64_t *totalp,int32_t *nump,struct coin777 *coin,char *account) +{ + int32_t i,j,num; struct subatomic_unspent_tx *ups = 0; char *params,*addrs,*retstr; cJSON *json,*item; + /*{ + "txid" : "1ccd2a9d0f8d690ed13b6768fc6c041972362f5531922b6b152ed2c98d3fe113", + "vout" : 1, + "address" : "DK3nxu6GshBcQNDMqc66ARcwqDZ1B5TJe5", + "scriptPubKey" : "76a9149891029995222077889b36c77e2b85690878df9088ac", + "amount" : 2.00000000, + "confirmations" : 72505 + },*/ + *totalp = *nump = 0; + if ( account != 0 && account[0] != 0 ) + { + if ( (addrs= gather_account_addresses(coin,account)) != 0 ) + { + if ( (params = calloc(1,strlen(addrs) + 128)) == 0 ) + { + free(addrs); + return(0); + } + addrs[strlen(addrs)-1] = 0; + sprintf(params,"[%d, 99999999, [%s]]",coin->minconfirms,addrs+1); + free(addrs); + } else return(0); + } + else + { + if ( (params = calloc(1,128)) == 0 ) + return(0); + sprintf(params,"%d, 99999999",coin->minconfirms); + } + //printf("issue listunspent.(%s)\n",params); + if ( (retstr= bitcoind_passthru(coin->name,coin->serverport,coin->userpass,"listunspent",params)) != 0 ) + { + //printf("unspents (%s)\n",retstr); + if ( (json= cJSON_Parse(retstr)) != 0 ) + { + if ( is_cJSON_Array(json) != 0 && (num= cJSON_GetArraySize(json)) > 0 ) + { + ups = calloc(num,sizeof(struct subatomic_unspent_tx)); + for (i=j=0; i 0 ) + { + int _decreasing_signedint64(const void *a,const void *b); + if ( j > 1 ) + qsort(ups,j,sizeof(*ups),_decreasing_signedint64); + if ( coin->changeaddr[0] == 0 ) + strcpy(coin->changeaddr,ups[0].address.buf); + //for (i=0; iname,account != 0 ? account : ""); + return(ups); +} + +struct subatomic_unspent_tx *subatomic_bestfit(struct coin777 *coin,struct subatomic_unspent_tx *unspents,int32_t numunspents,uint64_t value,int32_t mode) +{ + int32_t i; uint64_t above,below,gap,atx_value; struct subatomic_unspent_tx *vin,*abovevin,*belowvin; + abovevin = belowvin = 0; + for (above=below=i=0; iamount; + //printf("(%.8f vs %.8f)\n",dstr(atx_value),dstr(value)); + if ( atx_value == value ) + return(vin); + else if ( atx_value > value ) + { + gap = (atx_value - value); + if ( above == 0 || gap < above ) + { + above = gap; + abovevin = vin; + } + } + else if ( mode == 0 ) + { + gap = (value - atx_value); + if ( below == 0 || gap < below ) + { + below = gap; + belowvin = vin; + } + } + } + if ( (vin= (abovevin != 0) ? abovevin : belowvin) == 0 && mode == 1 ) + vin = unspents; + return(vin); +} + +int64_t subatomic_calc_rawinputs(struct coin777 *coin,struct subatomic_rawtransaction *rp,uint64_t amount,struct subatomic_unspent_tx *ups,int32_t num,uint64_t donation) +{ + uint64_t sum = 0; struct subatomic_unspent_tx *up; int32_t i; + rp->inputsum = rp->numinputs = 0; + printf("unspent num %d, amount %.8f vs donation %.8f txfee %.8f\n",num,dstr(amount),dstr(donation),dstr(coin->mgw.txfee)); + if ( coin == 0 || num == 0 ) // (donation + coin->mgw.txfee) > amount || + return(0); + amount += coin->mgw.txfee + donation; + for (i=0; iinputs)/sizeof(*rp->inputs))); i++) + { + if ( (up= subatomic_bestfit(coin,ups,num,amount,0)) != 0 ) + { + sum += up->amount; + rp->inputs[rp->numinputs++] = *up; + if ( sum >= amount ) + { + rp->amount = (amount - coin->mgw.txfee - donation); + rp->change = (sum - amount); + rp->inputsum = sum; + printf("numinputs %d sum %.8f vs amount %.8f change %.8f -> txfee %.8f\n",rp->numinputs,dstr(rp->inputsum),dstr(amount),dstr(rp->change),dstr(sum - rp->change - rp->amount)); + return(rp->inputsum); + } + } + printf("error getting bestfit unspent\n"); + break; + } + printf("i.%d error numinputs %d sum %.8f\n",i,rp->numinputs,dstr(rp->inputsum)); + return(0); +} + +char *subatomic_gen_rawtransaction(char *skipaddr,struct coin777 *coin,struct subatomic_rawtransaction *rp,char *signcoinaddr,uint32_t locktime,uint32_t vin0sequenceid,char *redeem0script) +{ + char *rawparams,*retstr,*txid=0; int64_t value; long len; struct cointx_info *cointx; + if ( (rawparams= subatomic_rawtxid_json(coin,rp)) != 0 ) + { + _stripwhite(rawparams,' '); + //printf("create.(%s)\n",rawparams); + if ( (retstr= bitcoind_RPC(0,coin->name,coin->serverport,coin->userpass,"createrawtransaction",rawparams)) != 0 ) + { + if ( retstr[0] != 0 ) + { + // printf("calc_rawtransaction retstr.(%s)\n",retstr); + safecopy(rp->rawtransaction,retstr,sizeof(rp->rawtransaction)); + len = strlen(rp->rawtransaction); + if ( len < 8 ) + { + printf("funny rawtransactionlen %ld??\n",len); + free(rawparams); + return(0); + } + if ( locktime != 0 || redeem0script != 0 ) + { + if ( (cointx= _decode_rawtransaction(rp->rawtransaction,coin->mgw.oldtx_format)) != 0 ) + { + //printf("%s\n->\n",rp->rawtransaction); + cointx->nlocktime = locktime; + cointx->inputs[0].sequence = vin0sequenceid; + if ( redeem0script != 0 ) + safecopy(cointx->outputs[0].script,redeem0script,sizeof(cointx->outputs[0].script)); + _emit_cointx(rp->rawtransaction,sizeof(rp->rawtransaction),cointx,coin->mgw.oldtx_format); + _validate_decoderawtransaction(rp->rawtransaction,cointx,coin->mgw.oldtx_format); + //printf("spliced tx.(%s)\n",rp->rawtransaction); + free(cointx); + } + printf("locktime.%d sequenceid.%d signcoinaddr.(%s)\n",locktime,vin0sequenceid,signcoinaddr!=0?signcoinaddr:""); + } + if ( signcoinaddr != 0 ) + { + txid = subatomic_signtx(skipaddr,0,&value,signcoinaddr,rp->signedtransaction,sizeof(rp->signedtransaction),coin,rp,rp->rawtransaction); + printf("signedtxid.%s\n",txid); + } + } + free(retstr); + } else printf("error creating rawtransaction from.(%s)\n",rawparams); + free(rawparams); + } else printf("error creating rawparams\n"); + return(txid); +} + +char *subatomic_signp2sh(char *sigstr,struct coin777 *coin,struct cointx_info *refT,int32_t msigflag,int32_t lockblocks,int32_t redeemi,char *redeemscript,int32_t p2shflag,char *privkeystr,int32_t privkeyind,char *othersig,char *otherpubkey,char *checkprivkey) +{ + char hexstr[1024],pubP[128],*sig0,*sig1; bits256 hash2; uint8_t data[4096],sigbuf[512]; struct bp_key key,keyV; + struct cointx_info *T; int32_t i,n; void *sig = NULL; size_t siglen = 0; struct cointx_input *vin; + if ( (T= calloc(1,sizeof(*T))) == 0 ) + return(0); + if ( privkeystr != 0 ) + btc_setprivkey(&key,privkeystr); + *T = *refT; vin = &T->inputs[redeemi]; + for (i=0; inuminputs; i++) + strcpy(T->inputs[i].sigs,"00"); + strcpy(vin->sigs,redeemscript); + if ( msigflag == 0 ) + { + vin->sequence = (uint32_t)-1; + T->nlocktime = 0; + } + else + { + if ( vin->sequence == 0 ) + vin->sequence = (uint32_t)time(NULL); + if ( T->nlocktime == 0 && lockblocks != 0 ) + { + if ( lockblocks != 0 ) + { + coin->ramchain.RTblocknum = _get_RTheight(&coin->ramchain.lastgetinfo,coin->name,coin->serverport,coin->userpass,coin->ramchain.RTblocknum); + if ( coin->ramchain.RTblocknum == 0 ) + { + printf("cant get RTblocknum for %s\n",coin->name); + free(T); + return(0); + } + lockblocks += coin->ramchain.RTblocknum; + } + T->nlocktime = lockblocks; + } + } + //disp_cointx(&T); + emit_cointx(&hash2,data,sizeof(data),T,coin->mgw.oldtx_format,SIGHASH_ALL); + //printf("HASH2.(%llx)\n",(long long)hash2.txid); + if ( msigflag != 0 ) + { + if ( othersig != 0 ) + { + n = (int32_t)strlen(otherpubkey) >> 1; + decode_hex(data,n,otherpubkey); + if ( bp_key_init(&keyV) == 0 || bp_pubkey_set(&keyV,data,n) == 0 ) + { + printf("cant set pubkey\n"); + free(T); + return(0); + } + n = (int32_t)strlen(othersig) >> 1; + decode_hex(data,n,othersig); + if ( data[n-1] != SIGHASH_ALL ) + { + printf("othersig.(%s) hash type mismatch %d != %d\n",othersig,data[n-1],SIGHASH_ALL); + free(T); + return(0); + } + if ( bp_verify(&keyV,hash2.bytes,sizeof(hash2),data,n-1) == 0 ) + { + hexstr[0] = 0; + if ( checkprivkey != 0 ) + { + //printf("checkprivkey.(%s)\n",checkprivkey); + btc_setprivkey(&keyV,checkprivkey); + void *dispkey; size_t slen; + bp_privkey_get(&keyV,&dispkey,&slen); + //for (i=0; isigs,"00%02x%s%02x%s51",(int32_t)strlen(sig0)>>1,sig0,(int32_t)strlen(sig1)>>1,sig1); + //printf("after A.(%s) othersig.(%s) siglen.%02lx -> (%s)\n",hexstr,othersig != 0 ? othersig : "",siglen,vin->sigs); + } + else + { + printf("error signing\n"); + free(T); + return(0); + } + } + else vin->sigs[0] = 0; + } + else + { + if ( bp_sign(&key,hash2.bytes,sizeof(hash2),&sig,&siglen) != 0 && btc_getpubkey(pubP,data,&key) > 0 ) + { + memcpy(sigbuf,sig,siglen); + sigbuf[siglen++] = SIGHASH_ALL; + init_hexbytes_noT(hexstr,sigbuf,(int32_t)siglen); + sprintf(vin->sigs,"%02x%s%02x%s00",(int32_t)siglen,hexstr,(int32_t)strlen(pubP)/2,pubP); + //printf("after P.(%s) siglen.%02lx\n",vin->sigs,siglen); + } + } + if ( vin->sigs[0] != 0 ) + { + if ( p2shflag != 0 ) + sprintf(&vin->sigs[strlen(vin->sigs)],"4c%02x",(int32_t)strlen(redeemscript)/2); + sprintf(&vin->sigs[strlen(vin->sigs)],"%s",redeemscript); + } + //printf("scriptSig.(%s)\n",vin->sigs); + _emit_cointx(hexstr,sizeof(hexstr),T,coin->mgw.oldtx_format); + //disp_cointx(&T); + free(T); + return(clonestr(hexstr)); + //printf("T.msigredeem %d -> (%s)\n",msigflag,hexstr); +} + +char *subatomic_fundingtx(char *refredeemscript,struct subatomic_rawtransaction *funding,struct coin777 *coin,char *mypubkey,char *otherpubkey,char *pkhash,uint64_t amount,int32_t lockblocks) +{ + char scriptPubKey[128],mycoinaddr[64],p2shaddr[64],sigstr[512],*refundtx=0,*redeemscript,*txid=0; struct subatomic_unspent_tx *utx; + uint64_t total,donation; int32_t num,n=0,lockblock = 0; struct cointx_info *refT; uint8_t rmd160[20]; + memset(funding,0,sizeof(*funding)); + refredeemscript[0] = 0; + if ( (redeemscript= create_atomictx_scripts(coin->p2shtype,scriptPubKey,p2shaddr,mypubkey,otherpubkey,pkhash)) != 0 ) + { + strcpy(refredeemscript,redeemscript); + if ( btc_coinaddr(mycoinaddr,coin->addrtype,mypubkey) != 0 && (utx= gather_unspents(&total,&num,coin,0)) != 0 ) + { + donation = subatomic_donation(coin,amount); + //printf("CREATE FUNDING TX.(%s) [%s %s %s] for %.8f -> %s locktime.%u donation %.8f\n",coin->name,mypubkey,otherpubkey,pkhash,dstr(amount),p2shaddr,lockblock,dstr(donation)); + if ( subatomic_calc_rawinputs(coin,funding,amount,utx,num,donation) >= amount ) + { + if ( funding->amount == amount && funding->change == (funding->inputsum - amount - coin->mgw.txfee - donation) ) + { + safecopy(funding->destaddrs[n],p2shaddr,sizeof(funding->destaddrs[n])); + funding->destamounts[n] = amount; + n++; + } + if ( donation != 0 ) + { + if ( coin->donationaddress[0] != 0 ) + { + safecopy(funding->destaddrs[n],coin->donationaddress,sizeof(funding->destaddrs[n])); + funding->destamounts[n] = donation; + n++; + } else funding->change += donation; + } + if ( funding->change != 0 ) + { + if ( coin->changeaddr[0] == 0 ) + { + printf("no changeaddress for (%s)\n",coin->name); + return(0); + } + safecopy(funding->destaddrs[n],coin->changeaddr,sizeof(funding->destaddrs[n])); + funding->destamounts[n] = funding->change; + n++; + } + funding->numoutputs = n; + if ( (txid= subatomic_gen_rawtransaction(0,coin,funding,p2shaddr,lockblock,lockblock==0?0xffffffff:(uint32_t)time(NULL),coin->usep2sh!=0?0:redeemscript)) == 0 ) + printf("error creating tx\n"); + else + { + if ( (refT= calloc(1,sizeof(*refT))) == 0 ) + return(0); + refT->version = 1; + refT->timestamp = (uint32_t)time(NULL); + strcpy(refT->inputs[0].tx.txidstr,txid); + refT->inputs[0].tx.vout = 0; + refT->numinputs = 1; + strcpy(scriptPubKey,"76a914"); + calc_OP_HASH160(scriptPubKey+6,rmd160,mypubkey); + strcat(scriptPubKey,"88ac"); + if ( mycoinaddr[0] != 0 ) + { + strcpy(refT->outputs[0].coinaddr,mycoinaddr); + strcpy(refT->outputs[0].script,scriptPubKey); + refT->outputs[0].value = funding->destamounts[0] - coin->mgw.txfee; + refT->numoutputs = 1; + if ( lockblocks == 0 ) + lockblocks = 10; + refundtx = subatomic_signp2sh(sigstr,coin,refT,1,lockblocks,0,redeemscript,coin->usep2sh,0,0,0,0,0); + free(refT); + } else printf("cant get %s addr from (%s)\n",coin->name,mypubkey); + } + } else printf("error: probably not enough funds\n"); + } else printf("error: btc_coinaddr.(%s)\n",mycoinaddr); + free(redeemscript); + } else printf("subatomic_fundingtx: cant create redeemscript\n"); + return(refundtx); +} + +char *subatomic_spendtx(struct destbuf *spendtxid,char *vintxid,char *refundsig,struct coin777 *coin,char *otherpubkey,char *mypubkey,char *onetimepubkey,uint64_t amount,char *refundtx,char *refredeemscript) +{ + char scriptPubKey[128],p2shaddr[64],rmdstr[41],onetimecoinaddr[64],msigcoinaddr[64],sigstr[512]; cJSON *json; + char *redeemscript,*signedtx,*spendtx=0,*mprivkey,*oprivkey; uint8_t rmd160[20]; long diff=0; struct cointx_info *refundT=0; + refundsig[0] = onetimecoinaddr[0] = msigcoinaddr[0] = spendtxid->buf[0] = vintxid[0] = 0; + if ( btc_coinaddr(onetimecoinaddr,coin->addrtype,onetimepubkey) != 0 && btc_coinaddr(msigcoinaddr,coin->addrtype,mypubkey) != 0 ) + { + //printf("mypubkey.(%s) -> (%s)\n",mypubkey,msigcoinaddr); + calc_OP_HASH160(rmdstr,rmd160,onetimepubkey); + amount -= coin->mgw.txfee; + coin->ramchain.RTblocknum = _get_RTheight(&coin->ramchain.lastgetinfo,coin->name,coin->serverport,coin->userpass,coin->ramchain.RTblocknum); + if ( (refundT= _decode_rawtransaction(refundtx,coin->mgw.oldtx_format)) != 0 && refundT->inputs[0].sequence != 0xffffffff && refundT->nlocktime != 0 && (diff= ((long)refundT->nlocktime - coin->ramchain.RTblocknum)) > 1 && diff < 1000 ) + { + strcpy(vintxid,refundT->inputs[0].tx.txidstr); + if ( (redeemscript= create_atomictx_scripts(coin->p2shtype,scriptPubKey,p2shaddr,otherpubkey,mypubkey,rmdstr)) != 0 ) + { + if ( refundT->outputs[0].value == amount && strcmp(refredeemscript,redeemscript) == 0 && refundT->numinputs == 1 && refundT->numoutputs == 1 ) + { + if ( (mprivkey= dumpprivkey(coin->name,coin->serverport,coin->userpass,msigcoinaddr)) != 0 && (oprivkey= dumpprivkey(coin->name,coin->serverport,coin->userpass,onetimecoinaddr)) != 0 ) + { + //printf("mprivkey.(%s)\n",mprivkey); + if ( (signedtx= subatomic_signp2sh(refundsig,coin,refundT,1,0,0,redeemscript,coin->usep2sh,mprivkey,1,0,0,0)) != 0 ) + { + //printf("one sig.(%s)\n",signedtx); + free(signedtx); + strcpy(refundT->outputs[0].coinaddr,onetimecoinaddr); + sprintf(scriptPubKey,"76a914%s88ac",rmdstr); + strcpy(refundT->outputs[0].script,scriptPubKey); + spendtx = subatomic_signp2sh(sigstr,coin,refundT,0,0,0,redeemscript,coin->usep2sh,oprivkey,0,0,0,0); + if ( (json= get_decoderaw_json(coin,spendtx)) != 0 ) + { + copy_cJSON(spendtxid,jobj(json,"txid")); + free_json(json); + } + } else printf("Error signing\n"); + free(mprivkey); + free(oprivkey); + } + else + { + if ( mprivkey != 0 ) + free(mprivkey); + printf("error getting privkeys M.(%s) onetime.(%s)\n",msigcoinaddr,onetimecoinaddr); + } + } else printf("error (%.8f vs %.8f) comparing redeemscript.(%s) vs (%s) io.(%d %d)\n",dstr(refundT->outputs[0].value),dstr(amount),refredeemscript,redeemscript,refundT->numinputs,refundT->numoutputs); + free(redeemscript); + } else printf("error creating redeemscript\n"); + free(refundT); + } else printf("error decoding refundT.%p or diff %ld too big (%u %u)\n",refundT,diff,refundT->nlocktime,coin->ramchain.RTblocknum); + } else printf("error getting addresses (%s) (%s)\n",msigcoinaddr,onetimecoinaddr); + return(spendtx); +} + +char *subatomic_validate(struct coin777 *coin,char *pubA,char *pubB,char *pkhash,char *refundtx,char *refundsig) +{ + char scriptPubKey[512],mycoinaddr[64],p2shaddr[128],mysig[512],*redeemscript,*privkeystr,*signedrefund=0; + struct cointx_info *refundT; + if ( (refundT= _decode_rawtransaction(refundtx,coin->mgw.oldtx_format)) != 0 && btc_coinaddr(mycoinaddr,coin->addrtype,pubA) != 0 ) + { + if ( (privkeystr= dumpprivkey(coin->name,coin->serverport,coin->userpass,mycoinaddr)) != 0 ) + { + if ( (redeemscript= create_atomictx_scripts(coin->p2shtype,scriptPubKey,p2shaddr,pubA,pubB,pkhash)) != 0 ) + { + if ( (signedrefund= subatomic_signp2sh(mysig,coin,refundT,1,0,0,redeemscript,1,privkeystr,0,refundsig,pubB,0)) != 0 ) + { + //printf("SIGNEDREFUND.(%s)\n",signedrefund); + } + free(redeemscript); + } + free(privkeystr); + } + free(refundT); + } + return(signedrefund); +} + +void test_subatomic() +{ + char pkhash[8192],pubA[67],pubB[67],pubP[67]; uint8_t tmpbuf[512]; struct coin777 *coin; + struct subatomic_rawtransaction funding; char refredeemscript[4096],vintxid[128],swapacct[64],othercoinaddr[64],mycoinaddr[64],onetimeaddr[64],refundsig[512],*signedrefund,*refundtx=0,*spendtx=0; + uint64_t amount; struct destbuf pubkey; struct destbuf spendtxid; + coin = coin777_find("BTCD",1); + if ( strcmp(coin->name,"BTC") == 0 ) + coin->mgw.oldtx_format = 1; + //coin->usep2sh = 0; + strcpy(mycoinaddr,coin->atomicsend),get_pubkey(&pubkey,coin->name,coin->serverport,coin->userpass,mycoinaddr), strcpy(pubA,pubkey.buf); + strcpy(othercoinaddr,coin->atomicrecv),get_pubkey(&pubkey,coin->name,coin->serverport,coin->userpass,othercoinaddr), strcpy(pubB,pubkey.buf); + sprintf(swapacct,"%u",777); + if ( get_acct_coinaddr(onetimeaddr,coin->name,coin->serverport,coin->userpass,swapacct) != 0 ) + { + get_pubkey(&pubkey,coin->name,coin->serverport,coin->userpass,onetimeaddr); + strcpy(pubP,pubkey.buf); + printf("onetimeadddr.(%s) pubkey.(%s)\n",onetimeaddr,pubP); + } + calc_OP_HASH160(pkhash,tmpbuf,pubP); + amount = 20000; + printf("pkhash.(%s)\n",pkhash); + if ( (refundtx= subatomic_fundingtx(refredeemscript,&funding,coin,pubA,pubB,pkhash,20000,10)) != 0 ) + { + printf("FUNDING.(%s) unsignedrefund.(%s)\n",funding.signedtransaction,refundtx); + if ( (spendtx= subatomic_spendtx(&spendtxid,vintxid,refundsig,coin,pubA,pubB,pubP,amount,refundtx,refredeemscript)) != 0 ) + { + printf("vin.%s SPENDTX.(%s) %s refundsig.(%s)\n",vintxid,spendtx,spendtxid.buf,refundsig); + if ( (signedrefund= subatomic_validate(coin,pubA,pubB,pkhash,refundtx,refundsig)) != 0 ) + { + printf("SIGNEDREFUND.(%s)\n",signedrefund); + free(signedrefund); + } else printf("null signedrefund\n"); + } else printf("null spendtx\n"); + free(refundtx); + } + getchar(); +} +#endif + +#endif diff --git a/InstantDEX/tradebots.h b/InstantDEX/tradebots.h new file mode 100755 index 000000000..60d570dfb --- /dev/null +++ b/InstantDEX/tradebots.h @@ -0,0 +1,323 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#ifndef xcode_tradebots_h +#define xcode_tradebots_h + +#define TRADEBOT_DEFAULT_DURATION (600) +struct tradebot_info +{ + char buf[512],name[64],*prevobookstr,NXTADDR[64],NXTACCTSECRET[64]; + uint32_t starttime,expiration,finishtime,startedtrades,apitag; + int32_t numtrades,havetrade,numlinks; + double price,volume; + struct prices777_order trades[256]; void *cHandles[256]; int32_t curlings[256]; + struct tradebot_info *linkedbots[8]; + struct apitag_info *api; + struct tradebot_info *oppo; + struct InstantDEX_quote iQ; +}; + +// ./SNapi "{\"allfields\":1,\"agent\":\"InstantDEX\",\"method\":\"orderbook\",\"exchange\":\"active\",\"base\":\"NXT\",\"rel\":\"BTC\"}" + +// test balance verifier +// test tradeleg verifier +// test pass through quotes +// user lockin addrs +// atomic swaps using 2of3 msig +// broadcast request to all marketmakers +// pick best response and do BTC <-> NXT and NXT <-> ABC + +int32_t tradebot_havealltrades(struct tradebot_info *bot) +{ + int32_t i; + if ( bot->havetrade != 0 ) + { + if ( bot->numlinks > 0 ) + { + for (i=0; inumlinks; i++) + if ( bot->linkedbots[i] == 0 || bot->linkedbots[i]->havetrade == 0 ) + return(0); + } + return(1); + } + return(0); +} + +struct tradebot_info *tradebot_compile(cJSON *argjson,struct InstantDEX_quote *iQ,struct apitag_info *api) +{ + static uint64_t lastmonce; + uint64_t monce; char *name,*tmp,*tmp2; int32_t duration; struct tradebot_info *bot = calloc(1,sizeof(*bot)); + monce = (long long)(1000*time(NULL) + milliseconds()); + if ( monce == lastmonce ) + monce++; + lastmonce = monce; + bot->iQ = *iQ; + bot->api = api; + if ( (duration= juint(argjson,"duration")) == 0 ) + duration = TRADEBOT_DEFAULT_DURATION; + bot->expiration = (uint32_t)time(NULL) + duration; + if ( (name= jstr(argjson,"name")) != 0 ) + safecopy(bot->name,name,sizeof(bot->name)); + else sprintf(bot->name,"bot.%llu",monce); + if ( (tmp= jstr(argjson,"botnxt")) == 0 || (tmp2= jstr(argjson,"secret")) == 0 ) + { + safecopy(bot->NXTADDR,SUPERNET.NXTADDR,sizeof(bot->NXTADDR)); + safecopy(bot->NXTACCTSECRET,SUPERNET.NXTACCTSECRET,sizeof(bot->NXTACCTSECRET)); + } + else + { + safecopy(bot->NXTADDR,tmp,sizeof(bot->NXTADDR)); + safecopy(bot->NXTACCTSECRET,tmp2,sizeof(bot->NXTACCTSECRET)); + } + //bot->arbmargin = jdouble(argjson,"arbmargin"); + return(bot); +} + +int32_t tradebot_acceptable(struct tradebot_info *bot,cJSON *item) +{ + double price,volume; int32_t dir,i,n; cJSON *trades,*trade; + if ( bot->iQ.s.isask != 0 ) + dir = -1; + else dir = 1; + bot->price = price = jdouble(item,"price"); + bot->volume = volume = jdouble(item,"volume"); + if ( (trades= jarray(&n,item,"trades")) != 0 ) + { + /*{ + "plugin": "InstantDEX", + "method": "tradesequence", + "dotrade": 1, + "price": 0.00001858, + "volume": 484.39181916, + "trades": [ + { + "basket": "bid", + "price": 0.00001858, + "volume": 484.39181916, + "group": 0, + "exchange": "bittrex", + "base": "NXT", + "rel": "BTC", + "trade": "sell", + "name": "NXT/BTC", + "orderprice": 0.00001858, + "ordervolume": 484.39181916 + } + ] + }*/ + if ( n == 1 && is_cJSON_Array(jitem(trades,0)) != 0 ) + { + //printf("NESTED ARRAY DETECTED\n"); + trades = jitem(trades,0); + n = cJSON_GetArraySize(trades); + } + sprintf(bot->buf,"[%s %s%s %.8f %.4f] <- ",bot->iQ.s.isask != 0 ? "sell" : "buy ",bot->iQ.base,bot->iQ.rel,price,volume); + for (i=0; ibuf+strlen(bot->buf),"[%s %s %.8f %.4f] ",jstr(trade,"exchange"),jstr(trade,"trade"),jdouble(trade,"orderprice"),jdouble(trade,"ordervolume")); + } + sprintf(bot->buf+strlen(bot->buf),"n.%d\n",n); + if ( bot->iQ.s.isask == 0 && bot->oppo != 0 && bot->price > 0. && bot->oppo->price > 0 ) + { + //if ( bot->price < bot->oppo->price ) + { + printf("%s%s%.8f -> %.8f = gain %.3f%%\n\n",bot->buf,bot->oppo->buf,bot->price,bot->oppo->price,(bot->oppo->price/bot->price - 1)*100); + } + } + } + //printf("%s: dir.%d price %.8f vol %f vs bot price %.8f vol %f\n",bot->name,dir,price,volume,bot->iQ.s.price,bot->iQ.s.vol); + //if ( (dir > 0 && price < bot->iQ.s.price) || (dir < 0 && price >= bot->iQ.s.price) ) + return(1); + return(0); +} + +int32_t tradebot_isvalidtrade(struct tradebot_info *bot,struct prices777_order *order,cJSON *retjson) +{ + cJSON *array,*item; char *resultval; double balance,required; int32_t i,n,valid = 0; + if ( (array= jarray(&n,retjson,"traderesults")) != 0 ) + { + for (i=0; ipend) != 0 && pend->finishtime != 0 ) + return(1); + else return(0); +} + +int32_t tradebot_haspending(struct tradebot_info *bot) +{ + int32_t i,finished; + for (i=finished=0; inumtrades; i++) + { + if ( tradebot_tradedone(bot,&bot->trades[i]) > 0 ) + finished++; + } + return(finished < bot->numtrades); +} + +void tradebot_free(struct tradebot_info *bot) +{ + int32_t i; struct pending_trade *pend; + for (i=0; inumtrades; i++) + { + if ( (pend= bot->trades[i].pend) != 0 ) + free_pending(pend); + if ( bot->trades[i].retitem != 0 ) + free_json(bot->trades[i].retitem); + if ( bot->cHandles[i] != 0 ) + { + while ( bot->curlings[i] != 0 ) + { + fprintf(stderr,"%s: wait for curlrequest[%d] to finish\n",bot->name,i); + sleep(3); + } + curlhandle_free(bot->cHandles[i]); + } + } + if ( bot->prevobookstr != 0 ) + free(bot->prevobookstr); + free(bot); +} + +void Tradebot_loop(void *ptr) +{ + int32_t i,n,dotrade; char *obookstr,*retstr; cJSON *json,*array,*item,*retjson,*submit; + char jsonstr[1024]; struct tradebot_info *bot = ptr; + printf("START Tradebot.(%s)\n",bot->name); + while ( bot->finishtime == 0 && time(NULL) < bot->expiration ) + { + if ( bot->startedtrades == 0 ) + { + sprintf(jsonstr,"{\"allfields\":1,\"agent\":\"InstantDEX\",\"method\":\"orderbook\",\"exchange\":\"active\",\"base\":\"%s\",\"rel\":\"%s\"}",bot->iQ.base,bot->iQ.rel); + if ( (json= cJSON_Parse(jsonstr)) == 0 ) + { + printf("cant parse.(%s)\n",jsonstr); + exit(-1); + } + obookstr = SuperNET_SNapi(bot->api,json,0,1); + //printf("GOT.(%s)\n",obookstr); + free_json(json); + if ( bot->prevobookstr == 0 || strcmp(obookstr,bot->prevobookstr) != 0 ) + { + if ( bot->prevobookstr != 0 ) + free(bot->prevobookstr); + bot->prevobookstr = obookstr; + //printf("UPDATE.(%s)\n",obookstr); + submit = 0; + if ( (json= cJSON_Parse(obookstr)) != 0 ) + { + array = (bot->iQ.s.isask != 0) ? jarray(&n,json,"bids") : jarray(&n,json,"asks"); + if ( array != 0 && n > 0 ) + { + dotrade = 0; + for (i=0; i<1; i++) + { + item = jitem(array,i); + if ( tradebot_acceptable(bot,item) > 0 ) + { + submit = cJSON_Duplicate(item,1); + if ( jobj(submit,"dotrade") == 0 ) + jaddnum(submit,"dotrade",0); + else cJSON_ReplaceItemInObject(submit,"dotrade",cJSON_CreateNumber(0)); + retstr = SuperNET_SNapi(bot->api,submit,0,1); + free_json(submit); + //retstr = InstantDEX_tradesequence(bot->curlings,bot,bot->cHandles,&bot->numtrades,bot->trades,(int32_t)( sizeof(bot->trades)/sizeof(*bot->trades)),dotrade,bot->NXTADDR,bot->NXTACCTSECRET,item); + if ( retstr != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( tradebot_isvalidtrade(bot,&bot->trades[i],retjson) > 0 ) + bot->havetrade = 1; + free_json(retjson); + } + free(retstr); + if ( bot->havetrade == 0 ) + continue; + } + } + break; + } + if ( 0 && submit != 0 && tradebot_havealltrades(bot) != 0 ) + { + dotrade = 1; + cJSON_ReplaceItemInObject(submit,"dotrade",cJSON_CreateNumber(1)); + bot->startedtrades = (uint32_t)time(NULL); + retstr = InstantDEX_tradesequence(bot->curlings,bot,bot->cHandles,&bot->numtrades,bot->trades,(int32_t)(sizeof(bot->trades)/sizeof(*bot->trades)),dotrade,bot->NXTADDR,bot->NXTACCTSECRET,item); + printf("TRADE RESULT.(%s)\n",retstr); + break; + } + } + free_json(json); + } + } + } + else if ( bot->startedtrades != 0 ) + { + if ( tradebot_haspending(bot) > 0 && bot->finishtime == 0 ) + bot->finishtime = (uint32_t)time(NULL); + } + usleep(5000000); + } + while ( tradebot_haspending(bot) != 0 ) + sleep(60); + printf("FINISHED Tradebot.(%s) at %u finishtime.%u expiration.%u\n",bot->name,(uint32_t)time(NULL),bot->finishtime,bot->expiration); + tradebot_free(bot); +} + +char *Tradebot_parser(cJSON *argjson,struct InstantDEX_quote *iQ,struct apitag_info *api) +{ + char *submethod,*exchange; struct tradebot_info *bot,*oppobot; + printf("InstantDEX_tradebot.(%s) (%s/%s)\n",jprint(argjson,0),iQ->base,iQ->rel); + if ( (submethod= jstr(argjson,"submethod")) != 0 && (exchange= jstr(argjson,"exchange")) != 0 && strcmp(exchange,"active") == 0 && iQ != 0 ) + { + if ( strcmp(submethod,"simplebot") == 0 ) + { + if ( (bot= tradebot_compile(argjson,iQ,api)) == 0 ) + return(clonestr("{\"error\":\"tradebot compiler error\"}")); + iQ->s.isask ^= 1; + if ( (oppobot= tradebot_compile(argjson,iQ,api)) == 0 ) + return(clonestr("{\"error\":\"tradebot compiler error\"}")); + bot->oppo = oppobot; + oppobot->oppo = bot; + iguana_launch("bot",(void *)Tradebot_loop,bot); + iguana_launch("oppobot",(void *)Tradebot_loop,oppobot); + return(clonestr("{\"result\":\"tradebot started\"}")); + } else return(clonestr("{\"error\":\"unrecognized tradebot command\"}")); + return(clonestr("{\"result\":\"tradebot command processed\"}")); + } else return(clonestr("{\"error\":\"no prices777 or no tradebot submethod or not active exchange\"}")); +} + +#endif diff --git a/InstantDEX/trades.h b/InstantDEX/trades.h new file mode 100755 index 000000000..9667c7da0 --- /dev/null +++ b/InstantDEX/trades.h @@ -0,0 +1,1583 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef xcode_trades_h +#define xcode_trades_h + +struct tradehistory { uint64_t assetid,purchased,sold; }; + +struct tradehistory *_update_tradehistory(struct tradehistory *hist,uint64_t assetid,uint64_t purchased,uint64_t sold) +{ + int32_t i = 0; + if ( hist == 0 ) + hist = calloc(1,sizeof(*hist)); + if ( hist[i].assetid != 0 ) + { + for (i=0; hist[i].assetid!=0; i++) + if ( hist[i].assetid == assetid ) + break; + } + if ( hist[i].assetid == 0 ) + { + hist = realloc(hist,(i+2) * sizeof(*hist)); + memset(&hist[i],0,2 * sizeof(hist[i])); + hist[i].assetid = assetid; + } + if ( hist[i].assetid == assetid ) + { + hist[i].purchased += purchased; + hist[i].sold += sold; + printf("hist[%d] %llu +%llu -%llu -> (%llu %llu)\n",i,(long long)hist[i].assetid,(long long)purchased,(long long)sold,(long long)hist[i].purchased,(long long)hist[i].sold); + } else printf("_update_tradehistory: impossible case!\n"); + return(hist); +} + +struct tradehistory *update_tradehistory(struct tradehistory *hist,uint64_t srcasset,uint64_t srcamount,uint64_t destasset,uint64_t destamount) +{ + hist = _update_tradehistory(hist,srcasset,0,srcamount); + hist = _update_tradehistory(hist,destasset,destamount,0); + return(hist); +} + +cJSON *_tradehistory_json(struct tradehistory *asset) +{ + cJSON *json = cJSON_CreateObject(); + char numstr[64]; + sprintf(numstr,"%llu",(long long)asset->assetid), cJSON_AddItemToObject(json,"assetid",cJSON_CreateString(numstr)); + sprintf(numstr,"%.8f",dstr(asset->purchased)), cJSON_AddItemToObject(json,"purchased",cJSON_CreateString(numstr)); + sprintf(numstr,"%.8f",dstr(asset->sold)), cJSON_AddItemToObject(json,"sold",cJSON_CreateString(numstr)); + sprintf(numstr,"%.8f",dstr(asset->purchased) - dstr(asset->sold)), cJSON_AddItemToObject(json,"net",cJSON_CreateString(numstr)); + return(json); +} + +cJSON *tradehistory_json(struct tradehistory *hist,cJSON *array) +{ + int32_t i; char assetname[64],numstr[64]; cJSON *assets,*netpos,*item,*json = cJSON_CreateObject(); + cJSON_AddItemToObject(json,"rawtrades",array); + assets = cJSON_CreateArray(); + netpos = cJSON_CreateArray(); + for (i=0; hist[i].assetid!=0; i++) + { + cJSON_AddItemToArray(assets,_tradehistory_json(&hist[i])); + item = cJSON_CreateObject(); + get_assetname(assetname,hist[i].assetid); + cJSON_AddItemToObject(item,"asset",cJSON_CreateString(assetname)); + sprintf(numstr,"%.8f",dstr(hist[i].purchased) - dstr(hist[i].sold)), cJSON_AddItemToObject(item,"net",cJSON_CreateString(numstr)); + cJSON_AddItemToArray(netpos,item); + } + cJSON_AddItemToObject(json,"assets",assets); + cJSON_AddItemToObject(json,"netpositions",netpos); + return(json); +} + +cJSON *tabulate_trade_history(uint64_t mynxt64bits,cJSON *array) +{ + int32_t i,n; + cJSON *item; + long balancing; + struct tradehistory *hist = 0; + uint64_t src64bits,srcamount,srcasset,dest64bits,destamount,destasset,jump64bits,jumpamount,jumpasset; + //{"requestType":"processjumptrade","NXT":"5277534112615305538","assetA":"5527630","amountA":"6700000000","other":"1510821971811852351","assetB":"12982485703607823902","amountB":"100000000","feeA":"250000000","balancing":0,"feeAtxid":"1234468909119892020","triggerhash":"34ea5aaeeeb62111a825a94c366b4ae3d12bb73f9a3413a27d1b480f6029a73c"} + if ( array != 0 && is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i %llu) via %llu\n",(long long)mynxt64bits,(long long)src64bits,(long long)dest64bits,(long long)jump64bits); + } else printf("illegal tabulate_trade_entry %llu: %llu %llu %llu || %llu %llu %llu\n",(long long)mynxt64bits,(long long)src64bits,(long long)srcamount,(long long)srcasset,(long long)dest64bits,(long long)destamount,(long long)destasset); + } + } + if ( hist != 0 ) + { + array = tradehistory_json(hist,array); + free(hist); + } + return(array); +} + +cJSON *get_tradehistory(char *refNXTaddr,uint32_t timestamp) +{ + char cmdstr[1024],NXTaddr[64],*jsonstr; struct destbuf receiverstr,message,newtriggerhash,triggerhash; + cJSON *json,*array,*txobj,*msgobj,*attachment,*retjson = 0,*histarray = 0; int32_t i,j,n,m,duplicates = 0; uint64_t senderbits; + if ( timestamp == 0 ) + timestamp = 38785003; + sprintf(cmdstr,"requestType=getBlockchainTransactions&account=%s×tamp=%u&withMessage=true",refNXTaddr,timestamp); + if ( (jsonstr= issue_NXTPOST(cmdstr)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (array= cJSON_GetObjectItem(json,"transactions")) != 0 && is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i ",message); + unstringify(message.buf); + if ( (msgobj= cJSON_Parse(message.buf)) != 0 ) + { + //printf("(%s)\n",message); + if ( histarray == 0 ) + histarray = cJSON_CreateArray(), j = m = 0; + else + { + copy_cJSON(&newtriggerhash,cJSON_GetObjectItem(msgobj,"triggerhash")); + m = cJSON_GetArraySize(histarray); + for (j=0; jquoteid)) != 0 ) + { + iQ->s.closed = 1; + delete_iQ(pend->quoteid); + } + else printf("free_pending: cant find pending tx for %llu\n",(long long)pend->quoteid); + if ( pend->triggertx != 0 ) + free(pend->triggertx); + if ( pend->txbytes != 0 ) + free(pend->txbytes); + if ( pend->tradesjson != 0 ) + free_json(pend->tradesjson); + free(pend); +} + +/*void oldInstantDEX_history(int32_t action,struct pending_trade *pend,char *str) +{ + uint8_t txbuf[32768]; char *tmpstr; uint16_t n; long len = 0; + // struct pending_trade { struct queueitem DL; struct prices777_order order; uint64_t triggertxid,txid,quoteid,orderid; struct prices777 *prices; char *triggertx,*txbytes; cJSON *tradesjson; double price,volume; uint32_t timestamp; int32_t dir,type; }; + memcpy(&txbuf[len],&action,sizeof(action)), len += sizeof(action); + if ( action == 0 ) + { + memcpy(&txbuf[len],pend,sizeof(*pend)), len += sizeof(*pend); + if ( pend->triggertx != 0 ) + { + n = (uint16_t)strlen(pend->triggertx) + 1; + memcpy(&txbuf[len],&n,sizeof(n)), len += sizeof(n); + memcpy(&txbuf[len],pend->triggertx,n), len += n; + } + if ( pend->txbytes != 0 ) + { + n = (uint16_t)strlen(pend->txbytes) + 1; + memcpy(&txbuf[len],&n,sizeof(n)), len += sizeof(n); + memcpy(&txbuf[len],pend->txbytes,n), len += n; + } + if ( pend->tradesjson != 0 ) + { + tmpstr = jprint(pend->tradesjson,0); + n = (uint16_t)strlen(tmpstr) + 1; + memcpy(&txbuf[len],&n,sizeof(n)), len += sizeof(n); + memcpy(&txbuf[len],tmpstr,n), len += n; + free(tmpstr); + } + } + else + { + memcpy(&txbuf[len],&pend->orderid,sizeof(pend->orderid)), len += sizeof(pend->orderid); + memcpy(&txbuf[len],&pend->quoteid,sizeof(pend->quoteid)), len += sizeof(pend->quoteid); + } + if ( str != 0 ) + { + n = (uint16_t)strlen(str) + 1; + memcpy(&txbuf[len],&n,sizeof(n)), len += sizeof(n); + memcpy(&txbuf[len],str,n), len += n; + } + else + { + n = 0; + memcpy(&txbuf[len],&n,sizeof(n)), len += sizeof(n); + } + txind777_create(INSTANTDEX.history,INSTANTDEX.numhist,pend->timestamp,txbuf,len); + txinds777_flush(INSTANTDEX.history,INSTANTDEX.numhist,pend->timestamp); + INSTANTDEX.numhist++; +}*/ + +char *InstantDEX_loadhistory(struct pending_trade *pend,int32_t *actionp,uint8_t *txbuf,int32_t size) +{ + char *tmpstr,*str = 0; uint16_t n; long len = 0; + memcpy(actionp,&txbuf[len],sizeof(*actionp)), len += sizeof(*actionp); + if ( *actionp == 0 ) + { + memcpy(pend,&txbuf[len],sizeof(*pend)), len += sizeof(*pend); + //printf("pendsize.%ld trigger.%p tx.%p json.%p\n",(long)sizeof(*pend),pend->triggertx,pend->txbytes,pend->tradesjson); + if ( pend->triggertx != 0 ) + { + memcpy(&n,&txbuf[len],sizeof(n)), len += sizeof(n); + pend->triggertx = calloc(1,n); + memcpy(pend->triggertx,&txbuf[len],n), len += n; + } + if ( pend->txbytes != 0 ) + { + memcpy(&n,&txbuf[len],sizeof(n)), len += sizeof(n); + pend->txbytes = calloc(1,n); + memcpy(pend->txbytes,&txbuf[len],n), len += n; + } + if ( pend->tradesjson != 0 ) + { + memcpy(&n,&txbuf[len],sizeof(n)), len += sizeof(n); + tmpstr = calloc(1,n); + memcpy(tmpstr,&txbuf[len],n), len += n; + if ( (pend->tradesjson= cJSON_Parse(tmpstr)) == 0 ) + printf("cant parse.(%s)\n",tmpstr); + free(tmpstr); + } + } + else + { + memcpy(&pend->orderid,&txbuf[len],sizeof(pend->orderid)), len += sizeof(pend->orderid); + memcpy(&pend->quoteid,&txbuf[len],sizeof(pend->quoteid)), len += sizeof(pend->quoteid); + } + memcpy(&n,&txbuf[len],sizeof(n)), len += sizeof(n); + if ( n != 0 ) + { + str = calloc(1,n); + memcpy(str,&txbuf[len],n), len += n; + } + if ( len != size ) + printf("loadhistory warning: len.%ld != size.%d\n",len,size); + return(str); +} + +struct pending_trade *InstantDEX_historyi(int32_t *actionp,char **strp,int32_t i,uint8_t *txbuf,int32_t maxsize) +{ + struct pending_trade *pend = 0; +/* void *ptr; int32_t size; + *strp = 0; + txinds777_seek(INSTANTDEX.history,i); + if ( (ptr= txinds777_read(&size,txbuf,INSTANTDEX.history)) == 0 || size <= 0 || size > maxsize ) + { + printf("InstantDEX_inithistory: error reading entry.%d | ptr.%p size.%d\n",i,ptr,maxsize); + return(0); + } + pend = calloc(1,sizeof(*pend)); + *strp = InstantDEX_loadhistory(pend,actionp,ptr,size);*/ + return(pend); +} + +int32_t oldInstantDEX_inithistory(int32_t firsti,int32_t endi) +{ + int32_t i,action; uint8_t txbuf[32768]; char *str; struct pending_trade *pend; + printf("InstantDEX_inithistory firsti.%d endi.%d\n",firsti,endi); + for (i=firsti; itype,pend->type!=0?pend->type:'0',action,(long long)pend->orderid,(long long)pend->quoteid,str!=0?str:""); + if ( str != 0 ) + free(str); + free_pending(pend); + } + } + return(i); +} + +cJSON *InstantDEX_tradeitem(struct pending_trade *pend) +{ + // struct pending_trade { struct queueitem DL; struct prices777_order order; uint64_t triggertxid,txid,quoteid,orderid; struct prices777 *prices; char *triggertx,*txbytes; cJSON *tradesjson; double price,volume; uint32_t timestamp; int32_t dir,type; }; + struct InstantDEX_quote *iQ; char str[64]; cJSON *json = cJSON_CreateObject(); + str[0] = (pend->type == 0) ? '0' : pend->type; + str[1] = 0; + jaddstr(json,"type",str); + jaddnum(json,"timestamp",pend->timestamp); + jadd64bits(json,"orderid",pend->orderid), jadd64bits(json,"quoteid",pend->quoteid); + if ( (iQ= find_iQ(pend->quoteid)) != 0 ) + { + if ( iQ->s.baseid != 0 && iQ->s.relid != 0 ) + jadd64bits(json,"baseid",iQ->s.baseid), jadd64bits(json,"relid",iQ->s.relid); + if ( iQ->s.baseamount != 0 && iQ->s.relamount != 0 ) + jaddnum(json,"baseqty",iQ->s.baseamount), jaddnum(json,"relqty",iQ->s.relamount); + } else printf("tradeitem cant find quoteid.%llu\n",(long long)pend->quoteid); + if ( pend->dir != 0 ) + jaddnum(json,"dir",pend->dir); + if ( pend->price > SMALLVAL && pend->volume > SMALLVAL ) + jaddnum(json,"price",pend->price), jaddnum(json,"volume",pend->volume); + if ( pend->triggertxid != 0 ) + jadd64bits(json,"triggertxid",pend->triggertxid); + if ( pend->txid != 0 ) + jadd64bits(json,"txid",pend->txid); + if ( pend->triggertx != 0 ) + jaddstr(json,"triggertx",pend->triggertx); + if ( pend->txbytes != 0 ) + jaddstr(json,"txbytes",pend->txbytes); + return(json); +} + +char *InstantDEX_withdraw(cJSON *argjson) +{ + char *exchangestr,*str; struct exchange_info *exchange; int32_t exchangeid; + if ( (exchangestr= jstr(argjson,"exchange")) != 0 && (exchange= find_exchange(&exchangeid,exchangestr)) != 0 ) + { + if ( exchange->issue.withdraw != 0 ) + { + if ( (str= (*exchange->issue.withdraw)(&exchange->cHandle,exchange,argjson)) == 0 ) + str = clonestr("{\"result\":\"nothing returned from exchange\"}"); + return(str); + } + else return(clonestr("{\"error\":\"no withdraw function\"}")); + } + return(clonestr("{\"error\":\"withdraw is not yet\"}")); +} + +char *InstantDEX_tradehistory(cJSON *argjson,int32_t firsti,int32_t endi) +{ + /* + cJSON *json,*array,*item,*tmp; int32_t exchangeid,i,action; uint8_t txbuf[32768]; + char *str,*exchangestr; struct pending_trade *pend; struct exchange_info *exchange; + if ( (exchangestr= jstr(argjson,"exchange")) != 0 && (exchange= find_exchange(&exchangeid,exchangestr)) != 0 ) + { + if ( exchange->issue.tradehistory != 0 ) + { + if ( (str= (*exchange->issue.tradehistory)(&exchange->cHandle,exchange,argjson)) == 0 ) + str = clonestr("{\"result\":\"nothing returned from exchange\"}"); + return(str); + } + else return(clonestr("{\"error\":\"no tradehistory function\"}")); + } + json = cJSON_CreateObject(); + array = cJSON_CreateArray(); + if ( endi == 0 ) + endi = INSTANTDEX.numhist-1; + if ( endi < firsti ) + endi = firsti; + for (i=firsti; i<=endi; i++) + { + if ( (pend= InstantDEX_historyi(&action,&str,i,txbuf,sizeof(txbuf))) != 0 ) + { + item = cJSON_CreateObject(); + jaddnum(item,"i",i); + jaddnum(item,"action",action); + jadd(item,"trade",InstantDEX_tradeitem(pend)); + if ( pend->tradesjson != 0 ) + jadd(item,"trades",cJSON_Duplicate(pend->tradesjson,1)); + if ( str != 0 ) + { + if ( (tmp= cJSON_Parse(str)) != 0 ) + jadd(item,"str",tmp); + free(str); + } + free_pending(pend); + jaddi(array,item); + } + } + jadd(json,"tradehistory",array); + jaddnum(json,"numentries",INSTANTDEX.numhist); + return(jprint(json,1));*/ + return(0); +} + +int32_t substr128(char *dest,char *src) +{ + char zeroes[129],*match; int32_t i; + for (i=0; i<128; i++) + zeroes[i] = '0'; + zeroes[i] = 0; + strcpy(dest,src); + if ( (match= strstr(dest,zeroes)) != 0 ) + { + strcpy(match,"Z"); + for (i=0; match[128+i]!=0; i++) + match[i+1] = match[128+i]; + match[i+1] = 0; + } + //printf("substr128.(%s) -> (%s)\n",src,dest); + return(0); +} + +uint64_t gen_NXTtx(struct NXTtx *tx,uint64_t dest64bits,uint64_t assetidbits,uint64_t qty,uint64_t orderid,uint64_t quoteid,int32_t deadline,char *reftx,char *phaselink,uint32_t finishheight,char *phasesecret) +{ + char secret[8192],cmd[16384],destNXTaddr[64],assetidstr[64],hexstr[64],*retstr; uint8_t msgbuf[17]; cJSON *json; int32_t len; uint64_t phasecost = 0; + if ( deadline > 1000 ) + deadline = 1000; + expand_nxt64bits(destNXTaddr,dest64bits); + memset(tx,0,sizeof(*tx)); + if ( ((phasesecret != 0 && phasesecret[0] != 0) || (phaselink!= 0 && phaselink[0] != 0)) && finishheight <= _get_NXTheight(0) ) + { + printf("finish height.%u must be in the future.%u\n",finishheight,_get_NXTheight(0)); + return(0); + } + if ( phaselink != 0 || phasesecret != 0 ) + phasecost = MIN_NQTFEE; + cmd[0] = 0; + if ( assetidbits == NXT_ASSETID ) + sprintf(cmd,"requestType=sendMoney&amountNQT=%lld",(long long)qty); + else + { + expand_nxt64bits(assetidstr,assetidbits); + if ( is_mscoin(assetidstr) == 0 ) + sprintf(cmd,"requestType=transferAsset&asset=%s&quantityQNT=%lld",assetidstr,(long long)qty); + else sprintf(cmd,"requestType=transferCurrency¤cy=%s&units=%lld",assetidstr,(long long)qty); + } + if ( quoteid != 0 ) + { + len = 0; + printf("serialize buffer\n"); + //len = txind777_txbuf(msgbuf,len,orderid,sizeof(orderid)); + //len = txind777_txbuf(msgbuf,len,quoteid,sizeof(quoteid)); + init_hexbytes_noT(hexstr,msgbuf,len); + sprintf(cmd+strlen(cmd),"&messageIsText=true&message=%s",hexstr); + } + if ( cmd[0] != 0 ) + { + escape_code(secret,IGUANA_NXTACCTSECRET); + sprintf(cmd+strlen(cmd),"&deadline=%u&feeNQT=%lld&secretPhrase=%s&recipient=%s&broadcast=false",deadline,(long long)MIN_NQTFEE+phasecost,secret,destNXTaddr); + if ( reftx != 0 && reftx[0] != 0 ) + sprintf(cmd+strlen(cmd),"&referencedTransactionFullHash=%s",reftx); + if ( phaselink != 0 && phaselink[0] != 0 ) + sprintf(cmd+strlen(cmd),"&phased=true&phasingFinishHeight=%u&phasingVotingModel=4&phasingQuorum=1&phasingLinkedFullHash=%s",finishheight,phaselink); + else if ( phasesecret != 0 && phasesecret[0] != 0 ) + sprintf(cmd+strlen(cmd),"&phased=true&phasingFinishHeight=%u&phasingVotingModel=5&phasingHashedSecretAlgorithm=62&phasingQuorum=1&phasingHashedSecret=%s",finishheight,phasesecret); +//printf("generated cmd.(%s)\n",cmd); + if ( (retstr= issue_NXTPOST(cmd)) != 0 ) + { +//printf("(%s)\n",retstr); + if ( (json= cJSON_Parse(retstr)) != 0 ) + { + if ( extract_cJSON_str(tx->txbytes,MAX_JSON_FIELD,json,"transactionBytes") > 0 && + extract_cJSON_str(tx->utxbytes,MAX_JSON_FIELD,json,"unsignedTransactionBytes") > 0 && + extract_cJSON_str(tx->fullhash,MAX_JSON_FIELD,json,"fullHash") > 0 && + extract_cJSON_str(tx->sighash,MAX_JSON_FIELD,json,"signatureHash") > 0 ) + { + tx->txid = j64bits(json,"transaction"); + substr128(tx->utxbytes2,tx->utxbytes); + } + free_json(json); + } + free(retstr); + } + } + return(tx->txid); +} + +struct NXTtx *fee_triggerhash(char *triggerhash,uint64_t orderid,uint64_t quoteid,int32_t deadline) +{ + static struct NXTtx fee; + if ( fee.fullhash[0] == 0 ) + gen_NXTtx(&fee,calc_nxt64bits(INSTANTDEX_ACCT),NXT_ASSETID,INSTANTDEX_FEE,orderid,quoteid,deadline,0,0,0,0); + strcpy(triggerhash,fee.fullhash); + return(&fee); +} + +uint64_t InstantDEX_swapstr(char *sendphased,char *phasesecret,uint64_t *txidp,char *triggertx,char *txbytes,char *swapstr,uint64_t orderid,struct prices777_order *order,char *triggerhash,char *phaselink,int32_t finishheight) +{ + struct NXTtx fee,sendtx; uint64_t otherqty = 0,otherassetbits = 0,assetidbits = 0,qty = 0; int32_t deadline = INSTANTDEX_TRIGGERDEADLINE; + if ( finishheight != 0 ) + { + if ( finishheight > FINISH_HEIGHT ) + deadline *= (finishheight / FINISH_HEIGHT); + finishheight += _get_NXTheight(0); + } + swapstr[0] = triggertx[0] = txbytes[0] = 0; + *txidp = 0; + gen_NXTtx(&fee,calc_nxt64bits(INSTANTDEX_ACCT),NXT_ASSETID,INSTANTDEX_FEE,orderid,order->s.quoteid,deadline,triggerhash,0,0,0); + strcpy(triggertx,fee.txbytes); + if ( order->s.baseamount < 0 ) + assetidbits = order->s.baseid, qty = -order->s.baseamount, otherassetbits = order->s.relid, otherqty = order->s.relamount; + else if ( order->s.relamount < 0 ) + assetidbits = order->s.relid, qty = -order->s.relamount, otherassetbits = order->s.baseid, otherqty = order->s.baseamount; + printf("genNXTtx.(%llu/%llu) finish at %u vs %u lag %u deadline %d assetidbits.%llu sendphased.(%s)\n",(long long)orderid,(long long)order->s.quoteid,finishheight,_get_NXTheight(0),finishheight-_get_NXTheight(0),deadline,(long long)assetidbits,sendphased!=0?sendphased:""); + if ( sendphased != 0 && assetidbits != 0 && qty != 0 ) + { + if ( triggerhash == 0 || triggerhash[0] == 0 ) + triggerhash = fee.fullhash; + gen_NXTtx(&sendtx,order->s.offerNXT,assetidbits,qty,orderid,order->s.quoteid,deadline,triggerhash,phaselink,finishheight,phasesecret); + *txidp = sendtx.txid; + strcpy(txbytes,sendtx.txbytes); + sprintf(swapstr,",\"F\":\"%u\",\"T\":\"%s\",\"FH\":\"%s\",\"U\":\"%s\",\"S\":\"%s\",\"a\":\"%llu\",\"q\":\"%llu\"}",finishheight,fee.fullhash,sendtx.fullhash,sendtx.utxbytes2,sendtx.sighash,(long long)otherassetbits,(long long)otherqty); + } + else sprintf(swapstr,",\"F\":\"%u\",\"T\":\"%s\",\"a\":\"%llu\",\"q\":\"%llu\"}",finishheight,fee.fullhash,(long long)otherassetbits,(long long)otherqty); + return(fee.txid); +} + +uint64_t prices777_swapbuf(char *sendphased,char *phasesecret,uint64_t *txidp,char *triggertx,char *txbytes,char *swapbuf,char *exchangestr,char *base,char *rel,struct prices777_order *order,uint64_t orderid,int32_t finishoffset,char *triggerhash) +{ + char swapstr[4096],*str; uint64_t txid = 0; + *txidp = 0; + if ( strcmp(exchangestr,"wallet") == 0 ) + str = "swap"; + else + { + str = order->wt > 0. ? "buy" : (order->wt < 0. ? "sell" : "swap"); + //printf("not wallet!\n"); getchar(); + } + if ( finishoffset == 0 ) + finishoffset = FINISH_HEIGHT; + sprintf(swapbuf,"{\"orderid\":\"%llu\",\"quoteid\":\"%llu\",\"offerNXT\":\"%llu\",\"fillNXT\":\"%s\",\"plugin\":\"relay\",\"destplugin\":\"InstantDEX\",\"method\":\"busdata\",\"submethod\":\"%s\",\"exchange\":\"%s\",\"base\":\"%s\",\"rel\":\"%s\",\"baseid\":\"%llu\",\"relid\":\"%llu\",\"baseqty\":\"%lld\",\"relqty\":\"%lld\"}",(long long)orderid,(long long)order->s.quoteid,(long long)order->s.offerNXT,IGUANA_NXTADDR,str,exchangestr,base,rel,(long long)order->s.baseid,(long long)order->s.relid,(long long)order->s.baseamount,(long long)order->s.relamount); + if ( order->s.price > SMALLVAL ) + sprintf(swapbuf + strlen(swapbuf) - 1,",\"price\":%.8f,\"volume\":%.8f}",order->s.price,order->s.vol); + txid = InstantDEX_swapstr(sendphased,phasesecret,txidp,triggertx,txbytes,swapstr,orderid,order,triggerhash,0,finishoffset); + strcpy(swapbuf+strlen(swapbuf)-1,swapstr); + //printf("swapbuf.(%s)\n",swapbuf); + return(txid); +} + +char *prices777_finishswap(int32_t dotrade,int32_t type,struct pending_trade *pend,char *swapbuf,char *triggertx,char *txbytes) +{ + uint32_t nonce; char *str; + if ( triggertx[0] != 0 ) + pend->triggertx = clonestr(triggertx); + if ( txbytes[0] != 0 ) + pend->txbytes = clonestr(txbytes); + pend->order.s.swap = 1; + pend->tradesjson = cJSON_Parse(swapbuf); + pend->type = type; + printf("quoteid.%llu and pending.%d\n",(long long)pend->order.s.quoteid,pend->order.s.pending); + if ( dotrade != 0 ) + { + if ( (str= busdata_sync(&nonce,swapbuf,"allnodes",0)) != 0 ) + free(str); + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ,&pend->DL,0); + } + //InstantDEX_history(0,pend,swapbuf); + return(clonestr(swapbuf)); +} + +/*int32_t subatomic_pubkeyhash(char *pubkeystr,char *pkhash,struct coin777 *coin,uint64_t quoteid) +{ + printf("subatomic pubkeyhash not yet\n"); + char tmpswapaddr[128],swapacct[128]; uint8_t tmpbuf[128]; struct destbuf pubkey; + sprintf(swapacct,"atomic.%llu",(long long)quoteid); + pkhash[0] = pubkeystr[0] = 0; + if ( get_acct_coinaddr(tmpswapaddr,coin->name,coin->serverport,coin->userpass,swapacct) != 0 ) + { + get_pubkey(&pubkey,coin->name,coin->serverport,coin->userpass,tmpswapaddr); + strcpy(pubkeystr,pubkey.buf); + calc_OP_HASH160(pkhash,tmpbuf,pubkey.buf); + return(0); + } + return(-1); +}*/ + +int32_t complete_swap(struct InstantDEX_quote *iQ,uint64_t orderid,uint64_t quoteid,int32_t err) +{ + /*int32_t errcode=-1,errcode2=-2; char *txstr,*txstr2; int32_t iter; struct pending_trade *pend; + for (iter=0; iter<2; iter++) + { + while ( (pend= queue_dequeue(&Pending_offersQ.pingpong[iter],0)) != 0 ) + { + if ( pend->quoteid == quoteid ) + { + if ( err == 0 && issue_broadcastTransaction(&errcode2,&txstr2,pend->txbytes,IGUANA_NXTACCTSECRET) == pend->txid && errcode2 == 0 ) + { + if ( err == 0 && (issue_broadcastTransaction(&errcode,&txstr,pend->triggertx,IGUANA_NXTACCTSECRET) != pend->triggertxid || errcode != 0) ) + err = -13; + } + if ( err == 0 && errcode == 0 && errcode2 == 0 ) + { + iQ->s.matched = 1; + //InstantDEX_history(1,pend,0); + } //else InstantDEX_history(-1,pend,0); + printf("errs.(%d %d %d) COMPLETED %llu/%llu %d %f %f with txids %llu %llu\n",err,errcode,errcode2,(long long)pend->orderid,(long long)pend->quoteid,pend->dir,pend->price,pend->volume,(long long)pend->triggertxid,(long long)pend->txid); + pend->queueflag = 1; + pend->finishtime = (uint32_t)time(NULL); + return(1); + } + queue_enqueue("requeue",&Pending_offersQ.pingpong[iter ^ 1],&pend->DL,0); + } + }*/ + printf("complete swap is notyet\n"); + return(-1); +} + +char *prices777_tradewallet(struct pending_trade *pend) +{ + printf("tradewallet is not yet\n"); + return(0); + /* + struct coin777 *recvcoin,*sendcoin; cJSON *walletitem,*item; + char fieldA[64],fieldB[64],triggertx[4096],txbytes[4096],fieldpkhash[64],refredeemscript[2048],scriptPubKey[128],p2shaddr[64]; + char swapbuf[8192],buf[1024],*rpubA=0,*rpubB=0,*rpkhash=0,*spubA=0,*spubB=0,*spkhash=0,*recvstr=0; + char *sendstr=0,*refundtx,*redeemscript,*str; int32_t finishin,deadline; uint32_t nonce; + uint64_t sendamount,recvamount,sendasset,recvasset; struct destbuf base,rel; + if ( pend->item != 0 && (item= jitem(pend->item,0)) != 0 && (walletitem= jobj(item,"wallet")) != 0 ) + { + finishin = (pend->extra[0] == 0) ? 200 : myatoi(pend->extra,10000); + if ( finishin < FINISH_HEIGHT ) + finishin = FINISH_HEIGHT; + copy_cJSON(&base,jobj(item,"base")); + copy_cJSON(&rel,jobj(item,"rel")); + if ( (recvamount= j64bits(item,"recvbase")) != 0 && (sendamount= j64bits(item,"sendrel")) != 0 ) + recvstr = base.buf, sendstr = rel.buf, recvasset = pend->order.s.baseid, sendasset = pend->order.s.relid; + else if ( (recvamount= j64bits(item,"recvrel")) != 0 && (sendamount= j64bits(item,"sendbase")) != 0 ) + recvstr = rel.buf, sendstr = base.buf, recvasset = pend->order.s.relid, sendasset = pend->order.s.baseid; + else + { + return(clonestr("{\"error\":\"need recvbase/sendrel or recvrel/sendbase\"}\n")); + } + recvcoin = coin777_find(recvstr,1), sendcoin = coin777_find(sendstr,1); + // placeask -> recvbase/sendrel, placebid -> sendbase/recvrel, it is relative to the one that placed quote + if ( strcmp(recvstr,"NXT") != 0 ) // placeask COIN/NXT or placebid NXT/COIN + { + if ( recvamount < recvcoin->mgw.txfee ) + { + printf("recvamount %.8f < txfee %.8f\n",dstr(recvamount),dstr(recvcoin->mgw.txfee)); + return(clonestr("{\"error\":\"amount too small\"}\n")); + } + sprintf(fieldA,"%spubA",recvstr), rpubA = jstr(walletitem,fieldA); + sprintf(fieldB,"%spubB",recvstr), rpubB = jstr(walletitem,fieldB); + sprintf(fieldpkhash,"%spkhash",recvstr), rpkhash = jstr(walletitem,fieldpkhash); + if ( rpubA[0] != 0 && rpubB != 0 && rpkhash != 0 ) // Alice for recvcoin -> Bob, Bob sends NXT -> Alice + { + if ( recvcoin->funding.signedtransaction[0] == 0 && (refundtx= subatomic_fundingtx(refredeemscript,&recvcoin->funding,recvcoin,rpubA,rpubB,rpkhash,recvamount,finishin)) != 0 ) + { + deadline = 3600; + gen_NXTtx(&recvcoin->trigger,calc_nxt64bits(INSTANTDEX_ACCT),NXT_ASSETID,INSTANTDEX_FEE,pend->orderid,pend->order.s.quoteid,deadline,0,0,0,0); + sprintf(swapbuf,"{\"orderid\":\"%llu\",\"quoteid\":\"%llu\",\"offerNXT\":\"%llu\",\"fillNXT\":\"%s\",\"plugin\":\"relay\",\"destplugin\":\"InstantDEX\",\"method\":\"busdata\",\"submethod\":\"swap\",\"exchange\":\"wallet\",\"recvamount\":\"%lld\",\"rtx\":\"%s\",\"rs\":\"%s\",\"recvcoin\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"trigger\":\"%s\",\"sendasset\":\"%llu\",\"sendqty\":\"%llu\",\"base\":\"%s\",\"rel\":\"%s\"}",(long long)pend->orderid,(long long)pend->order.s.quoteid,(long long)pend->order.s.offerNXT,SUPERNET.NXTADDR,(long long)recvamount,refundtx,refredeemscript,recvstr,fieldA,rpubA,fieldB,rpubB,fieldpkhash,rpkhash,recvcoin->trigger.fullhash,(long long)sendasset,(long long)sendamount,pend->prices->base,pend->prices->rel); + recvcoin->refundtx = refundtx; + pend->order.s.swap = 1; + if ( pend->dotrade != 0 && (str= busdata_sync(&nonce,swapbuf,"allnodes",0)) != 0 ) + { + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ.pingpong[0],&pend->DL,0); + } + return(clonestr(swapbuf)); + } else return(clonestr("{\"error\":\"cant create refundtx, maybe already pending\"}\n")); + } + else + { + sprintf(buf,"{\"error\":\"sendNXT recvstr.(%s) rpubA.(%s) without %s rpubB.%p or %s rpkhash.%p\"}\n",recvstr,rpubA,fieldB,rpubB,fieldpkhash,rpkhash); + return(clonestr(buf)); + } + } + else if ( strcmp(sendstr,"NXT") != 0 ) + { + if ( sendamount < sendcoin->mgw.txfee ) + { + printf("sendamount %.8f < txfee %.8f\n",dstr(sendamount),dstr(sendcoin->mgw.txfee)); + return(clonestr("{\"error\":\"amount too small\"}\n")); + } + sprintf(fieldA,"%spubA",sendstr), spubA = jstr(walletitem,fieldA); + sprintf(fieldB,"%spubB",sendstr), spubB = jstr(walletitem,fieldB); + sprintf(fieldpkhash,"%spkhash",sendstr), spkhash = jstr(walletitem,fieldpkhash); + if ( spubA != 0 && spubB != 0 && spkhash[0] != 0 ) // Bob <- sendcoin from Alice, send NXT -> Alice + { + if ( (redeemscript= create_atomictx_scripts(sendcoin->p2shtype,scriptPubKey,p2shaddr,spubA,spubB,spkhash)) != 0 ) + { + pend->triggertxid = prices777_swapbuf("yes",spkhash,&pend->txid,triggertx,txbytes,swapbuf,"wallet",pend->prices->base,pend->prices->rel,&pend->order,pend->orderid,finishin,0); + sprintf(swapbuf+strlen(swapbuf)-1,",\"sendcoin\":\"%s\",\"sendamount\":\"%llu\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"recvasset\":\"%llu\",\"recvqty\":\"%llu\"}",sendstr,(long long)sendamount,fieldA,spubA,fieldB,spubB,fieldpkhash,spkhash,(long long)recvasset,(long long)recvamount); + free(redeemscript); + pend->order.s.swap = 1; + if ( pend->dotrade != 0 && (str= busdata_sync(&nonce,swapbuf,"allnodes",0)) != 0 ) + { + free(str); + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ.pingpong[0],&pend->DL,0); + } + return(clonestr(swapbuf)); + } + } + else + { + sprintf(buf,"{\"error\":\"recvNXT sendstr.(%s) spubA.(%s) without %s spubB.(%s) or %s spkhash.(%s)\"}\n",sendstr,spubA,fieldB,spubB,fieldpkhash,spkhash); + return(clonestr(buf)); + } + } + else if ( rpubA[0] != 0 && rpubB != 0 && rpkhash != 0 && spubA != 0 && spubB != 0 && spkhash[0] != 0 && (strcmp(sendstr,"BTC") == 0 || strcmp(recvstr,"BTC") == 0) ) + { + if ( recvcoin->funding.signedtransaction[0] == 0 && (refundtx= subatomic_fundingtx(refredeemscript,&recvcoin->funding,recvcoin,rpubA,rpubB,rpkhash,recvamount,finishin)) != 0 ) + { + if ( (redeemscript= create_atomictx_scripts(sendcoin->p2shtype,scriptPubKey,p2shaddr,spubA,spubB,spkhash)) != 0 ) + { + pend->triggertxid = prices777_swapbuf(0,0,&pend->txid,triggertx,txbytes,swapbuf,"wallet",pend->prices->base,pend->prices->rel,&pend->order,pend->orderid,finishin,0); + sprintf(swapbuf,"{\"orderid\":\"%llu\",\"quoteid\":\"%llu\",\"offerNXT\":\"%llu\",\"fillNXT\":\"%s\",\"plugin\":\"relay\",\"destplugin\":\"InstantDEX\",\"method\":\"busdata\",\"submethod\":\"swap\",\"exchange\":\"wallet\",\"sendcoin\":\"%s\",\"recvcoin\":\"%s\",\"sendamount\":\"%lld\",\"recvamount\":\"%lld\",\"base\":\"%s\",\"rel\":\"%s\"}",(long long)pend->orderid,(long long)pend->order.s.quoteid,(long long)pend->order.s.offerNXT,SUPERNET.NXTADDR,sendstr,recvstr,(long long)sendamount,(long long)recvamount,pend->prices->base,pend->prices->rel); + sprintf(swapbuf+strlen(swapbuf)-1,",\"rtx\":\"%s\",\"rs\":\"%s\",\"rpubA\":\"%s\",\"rpubB\":\"%s\",\"rpkhash\":\"%s\",\"pubA\":\"%s\",\"pubB\":\"%s\",\"pkhash\":\"%s\"}",refundtx,refredeemscript,rpubA,rpubB,rpkhash,spubA,spubB,spkhash); + free(redeemscript); + free(refundtx); + pend->order.s.swap = 1; + if ( pend->dotrade != 0 && (str= busdata_sync(&nonce,swapbuf,"allnodes",0)) != 0 ) + { + free(str); + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ.pingpong[0],&pend->DL,0); + } + return(clonestr(swapbuf)); + } + free(refundtx); + } + else return(clonestr("{\"error\":\"cant create refundtx, maybe already pending\"}\n")); + } + else return(clonestr("{\"error\":\"one of wallets must be NXT or BTC\"}\n")); + printf("wallet swap finishin.%d trigger.%llu swapbuf.(%s)\n",finishin,(long long)pend->triggertxid,swapbuf); + return(prices777_finishswap(pend->dotrade,'A',pend,swapbuf,triggertx,txbytes)); + } + else return(clonestr("{\"error\":\"need to have trades[] json item\"}\n"));*/ +} + +struct pending_trade *prices777_createpending(int32_t *curlingp,void *bot,void **cHandlep,int32_t dotrade,cJSON *item,char *activenxt,char *secret,struct prices777 *prices,int32_t dir,double price,double volume,struct InstantDEX_quote *iQ,struct prices777_order *order,uint64_t orderid,char *extra) +{ + struct InstantDEX_quote _iQ; struct exchange_info *exchange; struct pending_trade *pend; + char swapbuf[8192],triggertx[4096],txbytes[4096]; + if ( (exchange= find_exchange(0,prices->exchange)) == 0 && exchange->issue.trade != 0 ) + { + printf("prices777_trade: need to have supported exchange\n"); + return(0); + } + if ( cHandlep == 0 ) + cHandlep = &exchange->cHandle; + if ( iQ == 0 && order == 0 ) + { + printf("prices777_trade: need to have either iQ or order\n"); + return(0); + } + else if ( iQ == 0 && (iQ= find_iQ(order->s.quoteid)) == 0 ) + { + iQ = &_iQ; + memset(&_iQ,0,sizeof(_iQ)); + iQ->s = order->s; + iQ->exchangeid = prices->exchangeid; + if ( iQ->s.timestamp == 0 ) + iQ->s.timestamp = (uint32_t)time(NULL); + iQ = create_iQ(iQ,0); + } else iQ = create_iQ(iQ,0); + pend = calloc(1,sizeof(*pend)); + pend->bot = bot; + safecopy((char *)pend->nxtsecret,secret,sizeof(pend->nxtsecret)); + pend->size = (int32_t)sizeof(*pend); + pend->my64bits = calc_nxt64bits(activenxt); + triggertx[0] = txbytes[0] = swapbuf[0] = 0; + pend->prices = prices, pend->dir = dir, pend->price = price, pend->volume = volume, pend->orderid = orderid; + iQ->s.pending = 1; + pend->curlingp = curlingp; + pend->quoteid = iQ->s.quoteid; + if ( order != 0 ) + pend->order = *order; + else pend->order.s = iQ->s; + pend->timestamp = (uint32_t)time(NULL); + pend->expiration = pend->timestamp + 60; + pend->cHandlep = cHandlep; + pend->dotrade = dotrade; + pend->item = item; + pend->exchange = exchange; + safecopy(pend->extra,extra,sizeof(pend->extra)); + return(pend); +} + +char *prices777_issuepending(struct pending_trade *pend) +{ + char swapbuf[8192],triggertx[4096],txbytes[4096],*retstr; + struct prices777 *prices; struct exchange_info *exchange; + if ( (prices= pend->prices) == 0 || (exchange= pend->exchange) == 0 ) + retstr = clonestr("{\"error\":\"no prices ptr\"}"); + else if ( strcmp(prices->exchange,"wallet") == 0 ) + retstr = prices777_tradewallet(pend); + else if ( strcmp(prices->exchange,INSTANTDEX_NAME) == 0 ) + { + pend->expiration = pend->timestamp + INSTANTDEX_TRIGGERDEADLINE*60; + pend->triggertxid = prices777_swapbuf("yes",0,&pend->txid,triggertx,txbytes,swapbuf,prices->exchange,prices->base,prices->rel,&pend->order,pend->orderid,myatoi(pend->extra,10000),0); + retstr = prices777_finishswap(pend->dotrade,'T',pend,swapbuf,triggertx,txbytes); + } + else if ( strcmp(prices->exchange,"nxtae") == 0 ) + { + pend->type = 'N'; + retstr = fill_nxtae(pend->dotrade,&pend->txid,pend->my64bits,(char *)pend->nxtsecret,pend->dir,pend->price,pend->volume,prices->baseid,prices->relid); + if ( pend->dotrade != 0 ) + { + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ,&pend->DL,0); + } + } + else + { + if ( exchange->issue.trade != 0 ) + { + printf(" issue dir.%d %s/%s price %f vol %f -> %s\n",pend->dir,prices->base,prices->rel,pend->price,pend->volume,prices->exchange); + retstr = pend->extra; + if ( pend->curlingp != 0 ) + *pend->curlingp = 1; + if ( (pend->txid= (*exchange->issue.trade)(pend->cHandlep,pend->dotrade,&retstr,exchange,prices->base,prices->rel,pend->dir,pend->price,pend->volume)) != 0 ) + { + pend->queueflag = 1; + pend->finishtime = (uint32_t)time(NULL); + } + else printf("no txid from trade\n"); + if ( pend->curlingp != 0 ) + *pend->curlingp = 0; + if ( retstr != 0 ) + { + if ( pend->dotrade != 0 ) + { + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ,&pend->DL,0); + } + printf("returning.%p (%s)\n",retstr,retstr); + } + } else retstr = clonestr("{\"error\":\"no trade function for exchange\"}\n"); + } + if ( retstr == 0 ) + retstr = clonestr("{\"error\":\"no response\"}"); + return(retstr); +} + +char *prices777_trade(int32_t *curlingp,void *bot,struct pending_trade **pendp,void **cHandlep,int32_t dotrade,cJSON *item,char *activenxt,char *secret,struct prices777 *prices,int32_t dir,double price,double volume,struct InstantDEX_quote *iQ,struct prices777_order *order,uint64_t orderid,char *extra) +{ + struct pending_trade *pend; char *retstr; + if ( pendp != 0 ) + *pendp = 0; + if ( (pend= prices777_createpending(curlingp,bot,cHandlep,dotrade,item,activenxt,secret,prices,dir,price,volume,iQ,order,orderid,extra)) != 0 ) + { + if ( bot == 0 || dotrade == 0 ) + retstr = prices777_issuepending(pend); + else if ( pend->queueflag != 0 ) + retstr = clonestr("{\"result\":\"pending_trade created\"}"); + else retstr = clonestr("{\"error\":\"pending_trade couldnt be created\"}"); + if ( pend->queueflag == 0 ) + free_pending(pend), pend = 0; + else if ( pendp != 0 ) + *pendp = pend; + return(retstr); + } + else return(clonestr("{\"error\":\"couldnt createpending\"}")); +} + +char *issue_calculateFullHash(char *unsignedtxbytes,char *sighash) +{ + char cmd[4096]; + sprintf(cmd,"requestType=calculateFullHash&unsignedTransactionBytes=%s&signatureHash=%s",unsignedtxbytes,sighash); + return(issue_NXTPOST(cmd)); +} + +char *issue_parseTransaction(char *txbytes) +{ + char cmd[4096],*retstr = 0; + sprintf(cmd,"requestType=parseTransaction&transactionBytes=%s",txbytes); + retstr = issue_NXTPOST(cmd); + //printf("issue_parseTransaction.%s %s\n",txbytes,retstr); + if ( retstr != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,retstr); + //retstr = parse_NXTresults(0,"sender","",results_processor,jsonstr,strlen(jsonstr)); + //free(jsonstr); + } else printf("error getting txbytes.%s\n",txbytes); + return(retstr); +} + +uint64_t issue_broadcastTransaction(int32_t *errcodep,char **retstrp,char *txbytes,char *NXTACCTSECRET) +{ + cJSON *json,*errjson; + uint64_t txid = 0; + char cmd[4096],secret[8192],*retstr; + escape_code(secret,NXTACCTSECRET); + sprintf(cmd,"requestType=broadcastTransaction&secretPhrase=%s&transactionBytes=%s",secret,txbytes); + retstr = issue_NXTPOST(cmd); + *errcodep = -1; + if ( retstrp != 0 ) + *retstrp = retstr; + if ( retstr != 0 ) + { + //printf("(%s) -> (%s)\n",cmd,retstr); + //printf("broadcast got.(%s)\n",retstr); + if ( (json= cJSON_Parse(retstr)) != 0 ) + { + errjson = cJSON_GetObjectItem(json,"errorCode"); + if ( errjson != 0 ) + { + //printf("ERROR broadcasting.(%s)\n",retstr); + *errcodep = (int32_t)get_cJSON_int(json,"errorCode"); + } + else + { + if ( (txid = get_satoshi_obj(json,"transaction")) != 0 ) + *errcodep = 0; + } + } + if ( retstrp == 0 ) + free(retstr); + } + return(txid); +} + +char *issue_signTransaction(char *txbytes,char *NXTACCTSECRET) +{ + char cmd[4096],secret[8192]; + escape_code(secret,NXTACCTSECRET); + sprintf(cmd,"requestType=signTransaction&secretPhrase=%s&unsignedTransactionBytes=%s",secret,txbytes); + return(issue_NXTPOST(cmd)); +} + +char *issue_approveTransaction(char *fullhash,char *revealed,char *message,char *NXTACCTSECRET) +{ + char cmd[4096],secret[8192]; + escape_code(secret,NXTACCTSECRET); + sprintf(cmd,"requestType=approveTransaction&secretPhrase=%s&transactionFullHash=%s&revealedSecret=%s&messageIsText=true&feeNQT=%lld&deadline=%d&message=%s",secret,fullhash,revealed,(long long)MIN_NQTFEE,DEFAULT_NXT_DEADLINE,message); + printf("submit approve.(%s)\n",cmd); + return(issue_NXTPOST(cmd)); +} + +uint32_t issue_getTime() +{ + char cmd[4096],*jsonstr; cJSON *json; uint32_t timestamp = 0; + //sprintf(cmd,"requestType=getAsset&asset=%s",assetidstr); + sprintf(cmd,"requestType=getTime"); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + timestamp = juint(json,"time"), free_json(json); + free(jsonstr); + } + return(timestamp); +} + +int32_t swap_verifyNXT(uint32_t *finishp,uint32_t *deadlinep,cJSON *origjson,char *offerNXT,char *exchangestr,uint64_t orderid,uint64_t quoteid,struct InstantDEX_quote *iQ,char *phasedtx) +{ + char UTX[32768],*triggerhash,*utx,*sighash,*jsonstr=0,*parsed,*fullhash,*cmpstr; cJSON *json=0,*txobj,*attachment; int32_t retval = -1; + uint64_t otherbits,otherqty,recvasset; struct destbuf calchash; int64_t recvqty; uint32_t i,j,timestamp,now,finishheight; + *finishp = 0; + if ( (triggerhash= jstr(origjson,"T")) == 0 ) + triggerhash = jstr(origjson,"trigger"); + otherbits = j64bits(origjson,"a"); + otherqty = j64bits(origjson,"q"); + fullhash = jstr(origjson,"FH"); + finishheight = juint(origjson,"F"); + if ( phasedtx == 0 ) + { + utx = jstr(origjson,"U"); + if ( utx != 0 && strlen(utx) > sizeof(UTX) ) + { + printf("UTX overflow\n"); + return(-1); + } + else if ( utx != 0 ) + { + for (i=0; utx[i]!=0; i++) + if ( utx[i] == 'Z' ) + { + memcpy(UTX,utx,i); + for (j=0; j<128; j++) + UTX[i+j] = '0'; + UTX[i+j] = 0; + strcat(UTX,utx+i+1); + break; + } + } + sighash = jstr(origjson,"S"); + if ( iQ->s.isask == 0 ) + recvasset = iQ->s.baseid, recvqty = iQ->s.baseamount / get_assetmult(recvasset); + else recvasset = iQ->s.relid, recvqty = iQ->s.relamount / get_assetmult(recvasset); + printf("utx.(%s) -> UTX.(%s) sighash.(%s)\n",utx,UTX,sighash); + } + else + { + recvqty = otherqty; + recvasset = otherbits; + } + if ( phasedtx != 0 || (jsonstr= issue_calculateFullHash(UTX,sighash)) != 0 ) + { + if ( phasedtx != 0 || (json= cJSON_Parse(jsonstr)) != 0 ) + { + copy_cJSON(&calchash,jobj(json,"fullHash")); + if ( phasedtx != 0 || strcmp(calchash.buf,fullhash) == 0 ) + { + if ( (parsed= issue_parseTransaction(phasedtx != 0 ? phasedtx : UTX)) != 0 ) + { + _stripwhite(parsed,' '); + //printf("iQ (%llu/%llu) otherbits.%llu qty %llu PARSED OFFER.(%s) triggerhash.(%s) (%s) offer sender.%s\n",(long long)iQ->s.baseid,(long long)iQ->s.relid,(long long)otherbits,(long long)otherqty,parsed,fullhash,calchash,sender); + if ( (txobj= cJSON_Parse(parsed)) != 0 ) + { + *deadlinep = juint(txobj,"deadline"); + timestamp = juint(txobj,"timestamp"); + now = issue_getTime(); + if ( (attachment= jobj(txobj,"attachment")) != 0 ) + *finishp = juint(attachment,"phasingFinishHeight"); + cmpstr = jstr(txobj,"referencedTransactionFullHash"); + if ( *deadlinep >= INSTANTDEX_TRIGGERDEADLINE/2 && ((long)now - timestamp) < 60 && (cmpstr == 0 || triggerhash == 0 || (cmpstr != 0 && triggerhash != 0 && strcmp(cmpstr,triggerhash) == 0)) ) + { + // https://nxtforum.org/nrs-releases/nrs-v1-5-15/msg191715/#msg191715 + printf("GEN RESPONDTX lag.%d deadline.%d (recv.%llu %lld) recv.(%llu %lld) orderid.%llu/%llx quoteid.%llu/%llx\n",now-timestamp,*deadlinep,(long long)recvasset,(long long)recvqty,(long long)recvasset,(long long)recvqty,(long long)orderid,(long long)orderid,(long long)quoteid,(long long)quoteid); + if ( InstantDEX_verify(IGUANA_MY64BITS,recvasset,recvqty,txobj,recvasset,recvqty) == 0 ) + retval = 0; + else printf("(%s) didnt validate against quoteid.%llu\n",parsed,(long long)quoteid); + } else fprintf(stderr,"swap rejects tx deadline %d >= INSTANTDEX_TRIGGERDEADLINE/2 && (now %d - %d timestamp) %d < 60\n",*deadlinep,now,timestamp,now-timestamp); + free_json(txobj); + } else fprintf(stderr,"swap cant parse tx.(%s)\n",parsed); + free(parsed); + } else fprintf(stderr,"swap cant parse UTX.(%s)\n",UTX); + } else fprintf(stderr,"mismatch (%s) != (%s)\n",calchash.buf,fullhash); + if ( json != 0 ) + free_json(json); + } else fprintf(stderr,"swap cant parse.(%s)\n",jsonstr); + if ( jsonstr != 0 ) + free(jsonstr); + } else fprintf(stderr,"calchash.(%s)\n",jsonstr); + return(retval); +} + +struct pending_trade *pending_swap(char **strp,int32_t type,uint64_t orderid,uint64_t quoteid,char *triggerhash,char *fullhash,char *txstr,char *txstr2) +{ + struct pending_trade *pend; cJSON *retjson; + pend = calloc(1,sizeof(*pend)); + pend->orderid = orderid, pend->quoteid = quoteid; + if ( triggerhash != 0 ) + pend->triggertx = clonestr(triggerhash); + if ( fullhash != 0 ) + pend->txbytes = clonestr(fullhash); + pend->type = type; + if ( txstr != 0 && txstr2 != 0 ) + { + retjson = cJSON_CreateObject(); + jadd(retjson,"fee",cJSON_Parse(txstr)); + jadd(retjson,"responsetx",cJSON_Parse(txstr2)); + *strp = jprint(retjson,0); + pend->tradesjson = retjson; + } + pend->timestamp = (uint32_t)time(NULL); + return(pend); +} + +char *swap_responseNXT(int32_t type,char *offerNXT,uint64_t otherbits,uint64_t otherqty,uint64_t orderid,uint64_t quoteid,int32_t deadline,char *triggerhash,char *phaselink,int32_t finishheight,struct InstantDEX_quote *iQ) +{ + struct NXTtx fee,responsetx; int32_t errcode,errcode2; char *txstr,*txstr2,*str = 0; struct pending_trade *pend; + gen_NXTtx(&fee,calc_nxt64bits(INSTANTDEX_ACCT),NXT_ASSETID,INSTANTDEX_FEE,orderid,quoteid,deadline,triggerhash,0,0,0); + gen_NXTtx(&responsetx,calc_nxt64bits(offerNXT),otherbits,otherqty,orderid,quoteid,deadline,triggerhash,phaselink,finishheight,0); + if ( (fee.txid= issue_broadcastTransaction(&errcode,&txstr,fee.txbytes,IGUANA_NXTACCTSECRET)) != 0 ) + { + if ( (responsetx.txid= issue_broadcastTransaction(&errcode2,&txstr2,responsetx.txbytes,IGUANA_NXTACCTSECRET)) != 0 ) + { + if ( (pend= pending_swap(&str,type,orderid,quoteid,triggerhash,phaselink,txstr,txstr2)) != 0 && str != 0 ) + { + iQ->s.pending = iQ->s.swap = 1; + //InstantDEX_history(0,pend,str); + pend->queueflag = 1; + queue_enqueue("PendingQ",&Pending_offersQ,&pend->DL,0); + printf("BROADCAST fee.txid %llu and %llu (%s %s)\n",(long long)fee.txid,(long long)responsetx.txid,fee.fullhash,responsetx.fullhash); + } + } else printf("error.%d broadcasting responsetx.(%s) %s\n",errcode2,responsetx.txbytes,txstr2); + } else printf("error.%d broadcasting feetx.(%s) %s\n",errcode,fee.txbytes,txstr); + if ( str == 0 ) + str = clonestr("{\"error\":\"swap_responseNXT error responding\"}"); + return(str); +} + +int32_t extract_pkhash(char *pubkeystr,char *pkhash,char *script) +{ + int32_t len; uint8_t rmd160[20],data[4096],*ptr; + decode_hex(data,(int32_t)strlen(script)>>1,script); + len = data[0]; + ptr = &data[len + 1]; + len = *ptr++; + if ( len == 33 ) + { + init_hexbytes_noT(pubkeystr,ptr,33); + calc_OP_HASH160(pkhash,rmd160,pubkeystr); + printf("pkhash.(%s)\n",pkhash); + return(0); + } + return(-1); +} + +char *swap_func(int32_t localaccess,int32_t valid,char *sender,cJSON *origjson,char *origargstr) +{ + /*char script[4096],hexstr[128],*str,*base,*rel,*txstr,*phasedtx,*cointxid,*signedtx,*jsonstr; uint8_t msgbuf[512]; + struct pending_trade *pend; struct prices777_order order; struct InstantDEX_quote *iQ,_iQ; cJSON *json; + uint32_t deadline,finishheight,nonce,isask; int32_t errcode,myoffer,myfill,len; struct NXTtx sendtx,fee; struct destbuf spendtxid,reftx; + struct destbuf offerNXT,exchange; uint64_t otherbits,otherqty,quoteid,orderid,recvasset,recvqty,sendasset,sendqty,fillNXT,destbits,value; + char pubkeystr[128],pkhash[64],swapbuf[4096],refredeemscript[1024],vintxid[128],*triggerhash,*fullhash,*dest,deststr[64];*/ + struct destbuf offerNXT,exchange; uint32_t deadline,finishheight,isask; char *triggerhash,*fullhash; int32_t myoffer,myfill; struct InstantDEX_quote *iQ,_iQ; struct prices777_order order; + uint64_t otherbits,otherqty,quoteid,orderid,fillNXT; + copy_cJSON(&offerNXT,jobj(origjson,"offerNXT")); + fillNXT = j64bits(origjson,"fillNXT"); + copy_cJSON(&exchange,jobj(origjson,"exchange")); + finishheight = juint(origjson,"F"); + if ( (triggerhash= jstr(origjson,"T")) == 0 ) + triggerhash = jstr(origjson,"trigger"); + myoffer = strcmp(IGUANA_NXTADDR,offerNXT.buf) == 0; + myfill = (IGUANA_MY64BITS == fillNXT); +//printf("swap_func got (%s)\n",origargstr); + if ( myoffer+myfill != 0 ) + { + orderid = j64bits(origjson,"orderid"); + quoteid = j64bits(origjson,"quoteid"); + if ( (iQ= find_iQ(quoteid)) == 0 ) + { + fprintf(stderr,"swap_func: cant find quoteid.%llu\n",(long long)quoteid); + iQ = &_iQ, memset(iQ,0,sizeof(*iQ)); + //return(clonestr("{\"error\":\"cant find quoteid\"}")); + } + if ( iQ->s.responded != 0 ) + { + fprintf(stderr,"already responded quoteid.%llu\n",(long long)iQ->s.quoteid); + return(0); + } + isask = iQ->s.isask; + memset(&order,0,sizeof(order)); + order.s = iQ->s; +#ifdef notyet + if ( strcmp("wallet",exchange.buf) == 0 ) + { + uint64_t sendamount,recvamount; struct coin777 *recvcoin,*sendcoin; + char refundsig[512],fieldA[64],fieldB[64],fieldpkhash[64]; + char *recvstr,*sendstr,*spendtx,*refundtx,*redeemscript,*rpubA,*rpubB,*rpkhash,*spubA,*spubB,*spkhash; + recvcoin = sendcoin = 0; sendamount = recvamount = 0; + if ( (recvstr= jstr(origjson,"recvcoin")) != 0 ) + recvcoin = coin777_find(recvstr,0); + if ( (sendstr= jstr(origjson,"sendcoin")) != 0 ) + sendcoin = coin777_find(sendstr,0); + if ( iQ->s.baseid == NXT_ASSETID ) + isask ^= 1; + //printf("recvstr.%p sendstr.%p\n",recvstr,sendstr); + if ( recvstr != 0 && sendstr != 0 ) + { + if ( (sendamount= j64bits(origjson,"sendamount")) != 0 && (recvamount= j64bits(origjson,"recvamount")) != 0 && sendcoin != 0 && recvcoin != 0 && (refundtx= jstr(origjson,"rtx")) != 0 && (redeemscript= jstr(origjson,"rs")) != 0 && (rpubA= jstr(origjson,"rpubA")) != 0 && (rpubB= jstr(origjson,"rpubB")) != 0 && (rpkhash= jstr(origjson,"rpkhash")) != 0 && triggerhash != 0 && (spubA= jstr(origjson,"spubA")) != 0 && (spubB= jstr(origjson,"spubB")) != 0 && (spkhash= jstr(origjson,"spkhash")) != 0 ) + { + } + } + else if ( recvstr != 0 ) + { + //printf("INCOMINGRECV.(%s)\n",origargstr); + sprintf(fieldA,"%spubA",recvcoin->name); + sprintf(fieldB,"%spubB",recvcoin->name); + sprintf(fieldpkhash,"%spkhash",recvcoin->name); + if ( (recvamount= j64bits(origjson,"recvamount")) != 0 && recvcoin != 0 && (rpubA= jstr(origjson,fieldA)) != 0 && (rpubB= jstr(origjson,fieldB)) != 0 && (rpkhash= jstr(origjson,fieldpkhash)) != 0 ) + { + if ( ((isask != 0 && myoffer != 0) || (isask == 0 && myfill != 0)) && j64bits(origjson,"fill") != IGUANA_MY64BITS && (refundtx= jstr(origjson,"rtx")) != 0 && (redeemscript= jstr(origjson,"rs")) != 0 ) // Bob: sends NXT to Alice, recvs recvcoin + { + subatomic_pubkeyhash(pubkeystr,pkhash,recvcoin,quoteid); + //printf("CALC >>>>>>>>>> (%s) vs (%s)\n",pkhash,rpkhash); + if ( (base= jstr(origjson,"base")) != 0 && (rel= jstr(origjson,"rel")) != 0 && (sendasset= j64bits(origjson,"sendasset")) != 0 && (sendqty= j64bits(origjson,"sendqty")) != 0 ) + { + //printf("inside (%s/%s) sendasset.%llu sendqty.%llu rpkhash.(%s)\n",base,rel,(long long)sendasset,(long long)sendqty,rpkhash); + if ( (spendtx= subatomic_spendtx(&spendtxid,vintxid,refundsig,recvcoin,rpubA,rpubB,pubkeystr,recvamount,refundtx,redeemscript)) != 0 ) + { + finishheight = 60; deadline = 3600*4; + if ( (pend= pending_swap(&str,'A',orderid,quoteid,0,0,0,0)) != 0 ) + { + if ( isask == 0 ) + destbits = calc_nxt64bits(offerNXT.buf); + else destbits = fillNXT; + gen_NXTtx(&fee,calc_nxt64bits(INSTANTDEX_ACCT),NXT_ASSETID,INSTANTDEX_FEE,orderid,quoteid,deadline,triggerhash,0,0,0); + issue_broadcastTransaction(&errcode,&txstr,fee.txbytes,IGUANA_NXTACCTSECRET); + gen_NXTtx(&sendtx,destbits,sendasset,sendqty,orderid,quoteid,deadline,triggerhash,0,_get_NXTheight(0)+finishheight,rpkhash); + //issue_broadcastTransaction(&errcode,&txstr,sendtx.txbytes,IGUANA_NXTACCTSECRET); + printf(">>>>>>>>>>>> broadcast fee and phased.(%s) trigger.%s\n",sendtx.txbytes,triggerhash); + sprintf(swapbuf,"{\"orderid\":\"%llu\",\"quoteid\":\"%llu\",\"offerNXT\":\"%s\",\"fillNXT\":\"%llu\",\"plugin\":\"relay\",\"destplugin\":\"InstantDEX\",\"method\":\"busdata\",\"submethod\":\"swap\",\"exchange\":\"wallet\",\"recvcoin\":\"%s\",\"recvamount\":\"%lld\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"refundsig\":\"%s\",\"phasedtx\":\"%s\",\"spendtxid\":\"%s\",\"a\":\"%llu\",\"q\":\"%llu\",\"trigger\":\"%s\",\"fill\":\"%llu\"}",(long long)orderid,(long long)quoteid,offerNXT.buf,(long long)fillNXT,recvcoin->name,(long long)recvamount,fieldA,rpubA,fieldB,rpubB,fieldpkhash,rpkhash,refundsig,sendtx.txbytes,spendtxid.buf,(long long)sendasset,(long long)sendqty,triggerhash,(long long)IGUANA_MY64BITS); + if ( (str= busdata_sync(&nonce,swapbuf,"allnodes",0)) != 0 ) + free(str); + // poll for vin then broadcast spendtx + printf(">>>>>>>>>>>>>>>>>>>> wait for (%s) then send SPENDTX.(%s)\n",vintxid,spendtx); + if ( (value= wait_for_txid(script,recvcoin,vintxid,0,recvamount,recvcoin->minconfirms,0)) != 0 ) + { + signedtx = malloc(strlen(spendtx) + 16); + sprintf(signedtx,"[\"%s\"]",spendtx); + if ( (cointxid= bitcoind_passthru(recvcoin->name,recvcoin->serverport,recvcoin->userpass,"sendrawtransaction",signedtx)) != 0 ) + { + printf(">>>>>>>>>>>>> BROADCAST SPENDTX.(%s) (%s)\n",signedtx,cointxid); + free(cointxid); + } + free(signedtx); + } + printf("ATOMIC SWAP.%llu finished\n",(long long)quoteid); + iQ->s.closed = 1; + delete_iQ(quoteid); + } else printf("cant get pending_swap pend.%p\n",pend); + free(spendtx); + return(clonestr(swapbuf)); + } else printf("error generating spendtx\n"); + } else printf("mismatched recv (%s vs %s) or (%s)\n",recvcoin->atomicrecvpubkey,rpubB,rpkhash); + } + else if ( j64bits(origjson,"fill") != IGUANA_MY64BITS && (str= jstr(origjson,"refundsig")) != 0 && str[0] != 0 && (phasedtx= jstr(origjson,"phasedtx")) != 0 && phasedtx[0] != 0 ) // Alice to verify NXTtx and send recvcoin + { + if ( isask != 0 ) + dest = offerNXT.buf; + else + { + expand_nxt64bits(deststr,fillNXT); + dest = deststr; + } + if ( swap_verifyNXT(&finishheight,&deadline,origjson,dest,exchange.buf,orderid,quoteid,iQ,phasedtx) == 0 ) + { + if ( recvcoin->refundtx != 0 && (recvcoin->signedrefund= subatomic_validate(recvcoin,rpubA,rpubB,rpkhash,recvcoin->refundtx,str)) != 0 ) + { + free(recvcoin->refundtx), recvcoin->refundtx = 0; + issue_broadcastTransaction(&errcode,&txstr,recvcoin->trigger.txbytes,IGUANA_NXTACCTSECRET); + issue_broadcastTransaction(&errcode,&txstr,phasedtx,IGUANA_NXTACCTSECRET); + printf(">>>>>>>>>>>>>>>>>>>>>>>>>>> ISSUE TRIGGER.(%s) phased.(%s).%d | signedrefund.(%s)\n",recvcoin->trigger.txbytes,txstr!=0?txstr:"phasedsubmit error",errcode,recvcoin->signedrefund); + signedtx = malloc(strlen(recvcoin->funding.signedtransaction) + 16); + sprintf(signedtx,"[\"%s\"]",recvcoin->funding.signedtransaction); + if ( (cointxid= bitcoind_passthru(recvcoin->name,recvcoin->serverport,recvcoin->userpass,"sendrawtransaction",signedtx)) != 0 ) + { + printf(">>>>>>>>>>>>> FUNDING BROADCAST.(%s) (%s)\n",recvcoin->funding.signedtransaction,cointxid); + free(cointxid); + } else printf("error sendrawtransaction.(%s)\n",recvcoin->funding.signedtransaction); + free(signedtx); + copy_cJSON(&spendtxid,jobj(origjson,"spendtxid")); + printf("wait for spendtx.(%s)\n",spendtxid.buf); + if ( (value= wait_for_txid(script,recvcoin,spendtxid.buf,0,recvamount-recvcoin->mgw.txfee,0,0)) != 0 ) + { + iQ->s.responded = 1; + if ( extract_pkhash(pubkeystr,pkhash,script) == 0 ) + { + if ( strcmp(pkhash,rpkhash) == 0 ) + { + reftx.buf[0] = 0; + if ( (jsonstr= issue_parseTransaction(phasedtx)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + copy_cJSON(&reftx,jobj(json,"fullHash")); + free_json(json); + } + free(jsonstr); + } + len = 0; + len = txind777_txbuf(msgbuf,len,orderid,sizeof(orderid)); + len = txind777_txbuf(msgbuf,len,quoteid,sizeof(quoteid)); + init_hexbytes_noT(hexstr,msgbuf,len); + if ( (str= issue_approveTransaction(reftx.buf,pubkeystr,hexstr,IGUANA_NXTACCTSECRET)) != 0 ) + { + printf("fullhash.(%s) pubkey.(%s) pkhash.(%s) APPROVED.(%s)\n",reftx.buf,pubkeystr,pkhash,str); + free(str); + } else printf("error sending in approval\n"); + + } else printf("script.(%s) -> pkhash.(%s) vs rpkhash.(%s)\n",script,pkhash,rpkhash); + } else printf("unexpected end of script.(%s) (%s)\n",script,str); + } + printf("FINISHED ATOMIC SWAP of quoteid.%llu\n",(long long)quoteid); + iQ->s.closed = 1; + memset(&recvcoin->trigger,0,sizeof(recvcoin->trigger)); + memset(&recvcoin->funding,0,sizeof(recvcoin->funding)); + free(recvcoin->signedrefund), recvcoin->signedrefund = 0; + delete_iQ(quoteid); + } else printf("refund tx didnt verify\n"); + } else printf("NXT tx didnt verify\n"); + } //else printf("myfill.%d myoffer.%d recv mismatch isask.%d\n",myfill,myoffer,iQ->s.isask); + //printf("recv failed\n"); + return(clonestr("{\"result\":\"recv failed\"}")); + } + } + else if ( sendstr != 0 ) // Alice sendcoin -> Bob, recvs NXT + { + //printf("INCOMINGSEND.(%s)\n",origargstr); + sprintf(fieldA,"%spubA",sendcoin->name); + sprintf(fieldB,"%spubB",sendcoin->name); + sprintf(fieldpkhash,"%spkhash",sendcoin->name); + if ( ((isask == 0 && myoffer != 0) || (isask != 0 && myfill != 0)) && (sendamount= j64bits(origjson,"sendamount")) != 0 && sendcoin != 0 && triggerhash != 0 && (spubA= jstr(origjson,fieldA)) != 0 && (spubB= jstr(origjson,fieldB)) != 0 && (spkhash= jstr(origjson,fieldpkhash)) != 0 ) + { + if ( (base= jstr(origjson,"base")) != 0 && (rel= jstr(origjson,"rel")) != 0 && (recvasset= j64bits(origjson,"recvasset")) != 0 && (recvqty= j64bits(origjson,"recvqty")) != 0 ) + { + if ( sendcoin->funding.signedtransaction[0] == 0 && (refundtx= subatomic_fundingtx(refredeemscript,&sendcoin->funding,sendcoin,spubA,spubB,spkhash,sendamount,10)) != 0 ) + { + deadline = 3600; + gen_NXTtx(&sendcoin->trigger,calc_nxt64bits(INSTANTDEX_ACCT),NXT_ASSETID,INSTANTDEX_FEE,orderid,quoteid,deadline,0,0,0,0); + sprintf(swapbuf,"{\"orderid\":\"%llu\",\"quoteid\":\"%llu\",\"offerNXT\":\"%s\",\"fillNXT\":\"%llu\",\"plugin\":\"relay\",\"destplugin\":\"InstantDEX\",\"method\":\"busdata\",\"submethod\":\"swap\",\"exchange\":\"%s\",\"recvamount\":\"%lld\",\"rtx\":\"%s\",\"rs\":\"%s\",\"recvcoin\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"trigger\":\"%s\",\"sendasset\":\"%llu\",\"sendqty\":\"%llu\",\"base\":\"%s\",\"rel\":\"%s\",\"fill\":\"%llu\"}",(long long)orderid,(long long)quoteid,offerNXT.buf,(long long)fillNXT,exchange.buf,(long long)sendamount,refundtx,refredeemscript,sendstr,fieldA,spubA,fieldB,spubB,fieldpkhash,spkhash,sendcoin->trigger.fullhash,(long long)recvasset,(long long)recvqty,base,rel,(long long)IGUANA_MY64BITS); + sendcoin->refundtx = refundtx; + if ( (str= busdata_sync(&nonce,swapbuf,"allnodes",0)) != 0 ) + free(str); + return(clonestr(swapbuf)); + //printf("BUSDATA.(%s)\n",swapbuf); + } else return(clonestr("{\"error\":\"cant create refundtx, maybe already pending\"}\n")); + } //else printf("mismatched send (%s vs %s) or (%s)\n",sendcoin->atomicrecvpubkey,spubB,spkhash); + } else printf("myfill.%d myoffer.%d send mismatch isask.%d\n",myfill,myoffer,iQ->s.isask); + } + return(clonestr("{\"result\":\"processed wallet swap\"}")); + } +#endif + if ( myoffer != 0 && swap_verifyNXT(&finishheight,&deadline,origjson,offerNXT.buf,exchange.buf,orderid,quoteid,iQ,0) == 0 ) + { + otherbits = j64bits(origjson,"a"); + otherqty = j64bits(origjson,"q"); + fullhash = jstr(origjson,"FH"); + finishheight = juint(origjson,"F"); + return(swap_responseNXT('R',offerNXT.buf,otherbits,otherqty,orderid,quoteid,deadline,triggerhash,fullhash,finishheight,iQ)); + } else printf("myfill.%d myoffer.%d swap mismatch\n",myfill,myoffer); + } + return(clonestr("{\"result\":\"processed swap\"}")); +} + +int32_t match_unconfirmed(char *sender,char *hexstr,cJSON *txobj,char *txidstr,char *account,uint64_t amount,uint64_t qty,uint64_t assetid,char *recipient) +{ + // ok, the bug here is that on a delayed respondtx, the originator should refuse to release the trigger (and the money tx) + uint64_t orderid,quoteid,recvasset,sendasset; int64_t recvqty,sendqty; uint32_t bidask,deadline,timestamp,now; struct InstantDEX_quote *iQ; + decode_hex((void *)&orderid,sizeof(orderid),hexstr); + decode_hex((void *)"eid,sizeof(quoteid),hexstr+16); + //printf("match_unconfirmed.(%s) orderid.%llu %llx quoteid.%llu %llx\n",hexstr,(long long)orderid,(long long)orderid,(long long)quoteid,(long long)quoteid); + deadline = juint(txobj,"deadline"); + timestamp = juint(txobj,"timestamp"); + now = issue_getTime(); + //printf("deadline.%u now.%u timestamp.%u lag %ld\n",deadline,now,timestamp,((long)now - timestamp)); + if ( deadline < INSTANTDEX_TRIGGERDEADLINE/2 || ((long)now - timestamp) > 60*2 ) + return(0); + if ( (iQ= find_iQ(quoteid)) != 0 && iQ->s.closed == 0 && iQ->s.pending != 0 && (iQ->s.responded == 0 || iQ->s.feepaid == 0) ) + { + if ( Debuglevel > 2 ) + printf("match unconfirmed %llu/%llu %p swap.%d feepaid.%d responded.%d sender.(%s) -> recv.(%s) me.(%s) offer.(%llu)\n",(long long)orderid,(long long)quoteid,iQ,iQ->s.swap,iQ->s.feepaid,iQ->s.responded,sender,recipient,IGUANA_NXTADDR,(long long)iQ->s.offerNXT); + if ( iQ->s.swap != 0 && (strcmp(recipient,INSTANTDEX_ACCT) == 0 || strcmp(recipient,IGUANA_NXTADDR) == 0) ) + { + if ( iQ->s.feepaid == 0 ) + { + if ( verify_NXTtx(txobj,NXT_ASSETID,INSTANTDEX_FEE,calc_nxt64bits(INSTANTDEX_ACCT)) == 0 ) + { + iQ->s.feepaid = 1; + printf("FEE DETECTED\n"); + } else printf("notfee: dest.%s src.%s amount.%llu qty.%llu assetid.%llu\n",recipient,sender,(long long)amount,(long long)qty,(long long)assetid); + } + if ( iQ->s.responded == 0 ) + { + bidask = iQ->s.isask; + if ( iQ->s.offerNXT == IGUANA_MY64BITS ) + bidask ^= 1; + if ( bidask != 0 ) + { + sendasset = iQ->s.relid, sendqty = iQ->s.relamount; + recvasset = iQ->s.baseid, recvqty = iQ->s.baseamount; + } + else + { + sendasset = iQ->s.baseid, sendqty = iQ->s.baseamount; + recvasset = iQ->s.relid, recvqty = iQ->s.relamount; + } + sendqty /= get_assetmult(sendasset); + recvqty /= get_assetmult(recvasset); + if ( Debuglevel > 2 ) + printf("sendasset.%llu sendqty.%llu mult.%llu, recvasset.%llu recvqty.%llu mult.%llu\n",(long long)sendasset,(long long)sendqty,(long long)get_assetmult(sendasset),(long long)recvasset,(long long)recvqty,(long long)get_assetmult(recvasset)); + if ( InstantDEX_verify(IGUANA_MY64BITS,sendasset,sendqty,txobj,recvasset,recvqty) == 0 ) + { + iQ->s.responded = 1; + printf("iQ: %llu/%llu %lld/%lld | recv %llu %lld offerNXT.%llu\n",(long long)iQ->s.baseid,(long long)iQ->s.relid,(long long)iQ->s.baseamount,(long long)iQ->s.relamount,(long long)recvasset,(long long)recvqty,(long long)iQ->s.offerNXT); + printf("RESPONSE DETECTED\n"); + } + } + if ( iQ->s.responded != 0 && iQ->s.feepaid != 0 ) + { + printf("both detected offer.%llu my64bits.%llu\n",(long long)iQ->s.offerNXT,(long long)IGUANA_MY64BITS); + complete_swap(iQ,orderid,quoteid,iQ->s.offerNXT == IGUANA_MY64BITS); + } + } + } + return(-1); +} + +int32_t is_unfunded_order(uint64_t nxt64bits,uint64_t assetid,uint64_t amount) +{ + char assetidstr[64],NXTaddr[64],cmd[1024],*jsonstr; + int64_t ap_mult,unconfirmed,balance = 0; + cJSON *json; + expand_nxt64bits(NXTaddr,nxt64bits); + if ( assetid == NXT_ASSETID ) + { + sprintf(cmd,"requestType=getAccount&account=%s",NXTaddr); + if ( (jsonstr= issue_NXTPOST(cmd)) != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + balance = get_API_nxt64bits(cJSON_GetObjectItem(json,"balanceNQT")); + free_json(json); + } + free(jsonstr); + } + strcpy(assetidstr,"NXT"); + } + else + { + expand_nxt64bits(assetidstr,assetid); + if ( (ap_mult= assetmult(assetidstr)) != 0 ) + { + expand_nxt64bits(NXTaddr,nxt64bits); + balance = ap_mult * get_asset_quantity(&unconfirmed,NXTaddr,assetidstr); + } + } + if ( balance < amount ) + { + printf("balance %.8f < amount %.8f for asset.%s\n",dstr(balance),dstr(amount),assetidstr); + return(1); + } + return(0); +} + +cJSON *InstantDEX_tradejson(int32_t *curlingp,void *bot,struct pending_trade **pendp,void **cHandlep,cJSON *item,char *activenxt,char *secret,struct prices777_order *order,int32_t dotrade,uint64_t orderid,char *extra) +{ + char swapbuf[8192],buf[8192],triggertx[4096],txbytes[4096],*retstr,*exchange; uint64_t txid,qty,avail,priceNQT; + struct prices777 *prices; cJSON *json = 0; + if ( pendp != 0 ) + *pendp = 0; + if ( (prices= order->source) != 0 ) + { + exchange = prices->exchange; + swapbuf[0] = 0; + if ( dotrade == 0 ) + { + if ( strcmp(exchange,INSTANTDEX_NAME) != 0 && strcmp(exchange,"wallet") != 0 ) + { + sprintf(buf,"{\"orderid\":\"%llu\",\"trade\":\"%s\",\"exchange\":\"%s\",\"base\":\"%s\",\"rel\":\"%s\",\"baseid\":\"%llu\",\"relid\":\"%llu\",\"price\":%.8f,\"volume\":%.8f,\"extra\":\"%s\"}",(long long)orderid,order->wt > 0. ? "buy" : "sell",exchange,prices->base,prices->rel,(long long)prices->baseid,(long long)prices->relid,order->s.price,order->s.vol,extra!=0?extra:""); + if ( strcmp(exchange,"nxtae") == 0 ) + { + qty = calc_asset_qty(&avail,&priceNQT,activenxt,0,prices->baseid,order->s.price,order->s.vol); + sprintf(buf+strlen(buf)-1,",\"priceNQT\":\"%llu\",\"quantityQNT\":\"%llu\",\"avail\":\"%llu\"}",(long long)priceNQT,(long long)qty,(long long)avail); + if ( qty == 0 ) + sprintf(buf+strlen(buf)-1,",\"error\":\"insufficient balance\"}"); + } + return(cJSON_Parse(buf)); + } + else + { + //{"inverted":0,"contract":"MMNXT/Jay","baseid":"979292558519844732","relid":"8688289798928624137","bids":[{"plugin":"Inst + // antDEX","method":"tradesequence","dotrade":1,"price":2,"volume":2,"trades":[]}],"asks":[],"numbids":1,"numasks":0,"lastb + // id":2,"lastask":0,"NXT":"11471677413693100042","timestamp":1440587058,"maxdepth":25} + prices777_swapbuf("yes",0,&txid,triggertx,txbytes,swapbuf,prices->exchange,prices->base,prices->rel,order,orderid,extra==0?0:myatoi(extra,10000),0); + return(cJSON_Parse(swapbuf)); + } + } + retstr = prices777_trade(curlingp,bot,pendp,cHandlep,dotrade,item,activenxt,secret,prices,order->wt,order->s.price,order->s.vol,0,order,orderid,extra); + if ( retstr != 0 ) + { + json = cJSON_Parse(retstr); + free(retstr); + } + } + return(json); +} + +char *InstantDEX_dotrades(int32_t curlings[],void *bot,void *cHandles[],char *activenxt,char *secret,cJSON *json,struct prices777_order *trades,int32_t numtrades,int32_t dotrade,char *extra) +{ + struct destbuf exchangestr,gui,name,base,rel; struct InstantDEX_quote iQ; cJSON *retjson,*retarray; int32_t i; + bidask_parse(1,&exchangestr,&name,&base,&rel,&gui,&iQ,json); + retjson = cJSON_CreateObject(), retarray = cJSON_CreateArray(); + for (i=0; i maxtrades ) + return(clonestr("{\"error\":\"exceeded max trades possible in a tradesequence\"}")); + if ( n == 1 && is_cJSON_Array(jitem(array,0)) != 0 ) + { + //printf("NESTED ARRAY DETECTED\n"); + array = jitem(array,0); + n = cJSON_GetArraySize(array); + } + *nump = n; + timestamp = (uint32_t)time(NULL); + for (i=0; iid = orderid, order->s.quoteid = quoteid; + assetid = j64bits(item,"asset"), currency = j64bits(item,"currency"); + baseid = j64bits(item,"baseid"), relid = j64bits(item,"relid"); + sendbase = j64bits(item,"sendbase"), recvbase = j64bits(item,"recvbase"); + sendrel = j64bits(item,"sendrel"), recvrel = j64bits(item,"recvrel"); + order->s.baseamount = (recvbase - sendbase); + order->s.relamount = (recvrel - sendrel); + orderprice = jdouble(item,"orderprice"), ordervolume = jdouble(item,"ordervolume"); + order->s.timestamp = juint(item,"timestamp"); + order->s.duration = juint(item,"duration"); + order->s.minperc = juint(item,"minperc"); + order->s.baseid = baseid; + order->s.relid = relid; + //printf("ITEM.(%s)\n",jprint(item,0)); + if ( tradestr != 0 ) + { + if ( strcmp(tradestr,"buy") == 0 ) + dir = 1; + else if ( strcmp(tradestr,"sell") == 0 ) + dir = -1; + else if ( strcmp(tradestr,"swap") == 0 ) + dir = 0; + else return(clonestr("{\"error\":\"invalid trade direction\"}")); + if ( (prices= prices777_initpair(1,exchangestr,base.buf,rel.buf,0.,name.buf,baseid,relid,0)) != 0 ) + { + order->source = prices; + order->s.offerNXT = j64bits(item,"offerNXT"); + order->wt = dir, order->s.price = orderprice, order->s.vol = ordervolume; + printf("item[%d] dir.%d (price %.8f vol %.4f) %s/%s baseid.%llu relid.%llu sendbase.%llu recvbase.%llu sendrel.%llu recvrel.%llu | baseqty.%lld relqty.%lld\n",i,dir,order->s.price,order->s.vol,prices->base,prices->rel,(long long)order->s.baseid,(long long)order->s.relid,(long long)sendbase,(long long)recvbase,(long long)sendrel,(long long)recvrel,(long long)order->s.baseamount,(long long)order->s.relamount); + } else return(clonestr("{\"error\":\"invalid exchange or contract pair\"}")); + } + else + { + printf("item.(%s)\n",jprint(item,0)); + return(clonestr("{\"error\":\"no trade specified\"}")); + } + } + return(InstantDEX_dotrades(curlings,bot,cHandles,activenxt,secret,json,trades,n,dotrade,jstr(json,"extra"))); + } + printf("error parsing.(%s)\n",jprint(json,0)); + return(clonestr("{\"error\":\"couldnt process trades\"}")); +} + +#endif diff --git a/LEGAL/AUTHORS b/LEGAL/AUTHORS new file mode 100644 index 000000000..8b5005c4d --- /dev/null +++ b/LEGAL/AUTHORS @@ -0,0 +1,2 @@ +jl777 NXT-SQ9J-JCAN-8XVY-5XN7K + diff --git a/LEGAL/COPYING b/LEGAL/COPYING new file mode 100755 index 000000000..d159169d1 --- /dev/null +++ b/LEGAL/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/LEGAL/DEVELOPER-AGREEMENT b/LEGAL/DEVELOPER-AGREEMENT new file mode 100644 index 000000000..a5d2aabfe --- /dev/null +++ b/LEGAL/DEVELOPER-AGREEMENT @@ -0,0 +1,67 @@ +This document describes the agreement between the SuperNET developers +regarding copyright and licensing policies. + + +0. License. + +The SuperNET software is distributed under the GPL version 2, with the exception of +the code that allows SuperNET agents to be created. Agent reference code uses the MIT +license to allow fully unencumbered development of SuperNET agents. Independently created +SuperNET agents can even be closed source and be made available via the service provider +functionality within SuperNET. Also, service providers are free to decide on what +type of fees to charge for their services. + + +1. Individual copyright. + +Each core developer retains full copyright over his contributions to +the code. The aggregate "Copyright © The SuperNET Developers" notice +can still be used in some places for brevity, but the metadata +maintained by the version control software (currently Git) about the +origin and subsequent modifications of each file shall be used as a +definitive record of the specific copyright holders for that file or +modification (if original enough to be copyrightable). + + +2. Outside contributions. + +Contributions of non-committers (those without write access to the +repository) shall only be accepted if submitted under the MIT license, +or if placed in the public domain. Contributions of non-committers that +do not specify a license shall be deemed to be public domain work. + + +3. Closed source releases. + +Each copyright holder grants a non-transferable permission to the SuperNET +development team to use his code in closed source experimental +releases, provided that those are clearly labeled as experimental, for +testing purposes only, and are in a reasonable timeframe (not to exceed +six months) superseded by open source non-experimental releases with +essentially the same functionality. + + +4. Re-licensing. + +Re-licensing of the SuperNET software under a different license requires the +agreement of all copyright holders whose work is being re-licensed. To +ensure that an unreachable copyright holder cannot prevent the active +development team from making licensing decisions, each copyright holder +who leaves the development team shall provide an NXT account number in +the AUTHORS file, at which he can be contacted to discuss such +decisions. Lack of such contact info, or lack of any type of response to +a re-licensing permission request after more than 28 days, as recorded +in the NXT blockchain, shall be interpreted as an irrevocable permission +to the then active development team to perform the specific re-licensing +for which such a permission has been sought. + + +5. Pseudonymous developers. + +Developers may choose to contribute under a fictitious name. Such +developers shall provide verifiable crypto addresses in the AUTHORS file +A verified signature with such addresses shall be considered +sufficient, for making legally binding statements, or as a proof of +copyright ownership, by such pseudonymous developers. + + diff --git a/LEGAL/LICENSE b/LEGAL/LICENSE new file mode 100644 index 000000000..67f75dcd8 --- /dev/null +++ b/LEGAL/LICENSE @@ -0,0 +1,32 @@ +Copyright © 2013-2015 The SuperNET Developers. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License version 2, +as published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License version 2 for more details. + +You should have received a copy of the GNU General Public License version 2 +along with this program in the file COPYING. If not, see +. + +The SuperNET development team will consider granting exceptions to allow use of +this software under a different license on a case by case basis. Please see the +DEVELOPER-AGREEMENT file describing the developer agreement on copyright +and licensing policies, and the AUTHORS file for individual copyright holder +information. + +This software uses third party libraries, distributed under licenses described +in THIRDPARTY-LICENSES. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + diff --git a/LEGAL/THIRDPARTY-LICENSES b/LEGAL/THIRDPARTY-LICENSES new file mode 100644 index 000000000..db260ab54 --- /dev/null +++ b/LEGAL/THIRDPARTY-LICENSES @@ -0,0 +1,21 @@ +The following third party projects are incorporated into SuperNET and their respective licenses are adopted for each of these projects. Please see the files for each project for the exact details of their licensing. Most of them are in the public domain, MIT license or GPL. + +libtom: Tom St Denis, tomstdenis@gmail.com, http://libtom.org + +tweetnacl: http://tweetnacl.cr.yp.to/ + +curve25519: http://code.google.com/p/curve25519-donna/ and http://cr.yp.to/ecdh.html + +libtai: also from DJB http://tweetnacl.cr.yp.to/ + +SaM and vps: from Come-from-Beyond + +cJSON: Copyright (c) 2009 Dave Gamble http://sourceforge.net/projects/cjson/ + +uthash/utlist: Copyright (c) 2003-2014, Troy D. Hanson http://troydhanson.github.com/uthash/ + +inet.c: Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") Copyright (c) 1996-1999 by Internet Software Consortium. + +libgfshare: Copyright Daniel Silverstone + +misc: there might be some other third party files not listed above, in such cases the relevant copyright header in the top of these files govern diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..c139445ad --- /dev/null +++ b/Makefile @@ -0,0 +1,78 @@ + +CRYPTO777 = agents/libcrypto777.a +IGUANA = agents/iguana +INSTANTDEX = agents/InstantDEX +PAX = agents/PAX +PRICES = agents/prices +PANGEA = agents/pangea +TRADEBOTS = agents/tradebots +SUPERNET = agents/SuperNET + +DEPS = + +CFLAGS = -Wall -Wno-deprecated -Wno-unused-function -fno-strict-aliasing + +LIBS = ../agents/libcrypto777.a -lcurl -lssl -lcrypto -lpthread -lz -lm + +CC = gcc +OS := $(shell uname -s) +ifeq ($(OSNAME),Linux) +CFLAGS += -Wno-unused-but-set-variable +endif + +CFLAGS += -O2 + + +all: $(CRYPTO777) $(IGUANA) $(TRADEBOTS) $(SUPERNET) # $(INSTANTDEX) $(PAX) $(PRICES) $(PANGEA) + +$(CRYPTO777): crypto777/OS_nonportable.c crypto777/OS_portable.c crypto777/OS_time.c crypto777/iguana_OS.c crypto777/OS_portable.h crypto777/iguana_utils.c crypto777/bitcoind_RPC.c crypto777/cJSON.c crypto777/curve25519-donna.c crypto777/curve25519.c crypto777/hmac_sha512.c crypto777/inet.c crypto777/libgfshare.c crypto777/ramcoder.c crypto777/SaM.c crypto777/jpeg/jaricom.c crypto777/jpeg/jcapimin.c crypto777/jpeg/jcapistd.c crypto777/jpeg/jcarith.c crypto777/jpeg/jccoefct.c crypto777/jpeg/jccolor.c \ + crypto777/jpeg/jcdctmgr.c crypto777/jpeg/jchuff.c crypto777/jpeg/jcinit.c crypto777/jpeg/jcmainct.c crypto777/jpeg/jcmarker.c crypto777/jpeg/jcmaster.c \ + crypto777/jpeg/jcomapi.c crypto777/jpeg/jcparam.c crypto777/jpeg/jcprepct.c crypto777/jpeg/jcsample.c crypto777/jpeg/jctrans.c crypto777/jpeg/jdapimin.c \ + crypto777/jpeg/jdapistd.c crypto777/jpeg/jdarith.c crypto777/jpeg/jdatadst.c crypto777/jpeg/jdatasrc.c crypto777/jpeg/jdcoefct.c crypto777/jpeg/jdcolor.c \ + crypto777/jpeg/jddctmgr.c crypto777/jpeg/jdhuff.c crypto777/jpeg/jdinput.c crypto777/jpeg/jdmainct.c crypto777/jpeg/jdmarker.c crypto777/jpeg/jdmaster.c \ + crypto777/jpeg/jdmerge.c crypto777/jpeg/jdpostct.c crypto777/jpeg/jdsample.c crypto777/jpeg/jdtrans.c crypto777/jpeg/jerror.c crypto777/jpeg/jfdctflt.c \ + crypto777/jpeg/jfdctfst.c crypto777/jpeg/jfdctint.c crypto777/jpeg/jidctflt.c crypto777/jpeg/jidctfst.c crypto777/jpeg/jidctint.c crypto777/jpeg/jquant1.c \ + crypto777/jpeg/jquant2.c crypto777/jpeg/jutils.c crypto777/jpeg/jmemmgr.c crypto777/jpeg/jmemnobs.c; \ + cd crypto777; gcc -c -O2 *.c jpeg/jaricom.c jpeg/jcapimin.c jpeg/jcapistd.c jpeg/jcarith.c jpeg/jccoefct.c jpeg/jccolor.c \ + jpeg/jcdctmgr.c jpeg/jchuff.c jpeg/jcinit.c jpeg/jcmainct.c jpeg/jcmarker.c jpeg/jcmaster.c \ + jpeg/jcomapi.c jpeg/jcparam.c jpeg/jcprepct.c jpeg/jcsample.c jpeg/jctrans.c jpeg/jdapimin.c \ + jpeg/jdapistd.c jpeg/jdarith.c jpeg/jdatadst.c jpeg/jdatasrc.c jpeg/jdcoefct.c jpeg/jdcolor.c \ + jpeg/jddctmgr.c jpeg/jdhuff.c jpeg/jdinput.c jpeg/jdmainct.c jpeg/jdmarker.c jpeg/jdmaster.c \ + jpeg/jdmerge.c jpeg/jdpostct.c jpeg/jdsample.c jpeg/jdtrans.c jpeg/jerror.c jpeg/jfdctflt.c \ + jpeg/jfdctfst.c jpeg/jfdctint.c jpeg/jidctflt.c jpeg/jidctfst.c jpeg/jidctint.c jpeg/jquant1.c \ + jpeg/jquant2.c jpeg/jutils.c jpeg/jmemmgr.c jpeg/jmemnobs.c; \ + ar rcu ../agents/libcrypto777.a *.o jpeg/*.o; cd .. + +$(IGUANA): ;\ + cd iguana; $(CC) -o ../agents/iguana *.c $(LIBS); make; cd .. + +$(SUPERNET): ;\ + cd SuperNET; $(CC) -o ../agents/SuperNET *.c $(LIBS); make; cd .. + +$(INSTANTDEX): ;\ + cd InstantDEX; $(CC) -o ../agents/InstantDEX *.c $(LIBS); make; cd .. + +$(PANGEA): ;\ + cd pangea; $(CC) -o ../agents/pangea *.c $(LIBS); make; cd .. + +$(TRADEBOTS): ;\ + cd tradebots; $(CC) -o ../agents/tradebots *.c $(LIBS); make; cd .. + +$(PRICES): ;\ + cd pangea; $(CC) -o ../agents/pangea *.c $(LIBS); make; cd .. + +$(PAX): ;\ + cd peggy; $(CC) -o ../agents/PAX *.c $(LIBS); make; cd .. + +iguana: $(IGUANA) +SN: $(SuperNET) +idex: $(InstantDEX) +PAX: $(PAX) +prices: $(PRICES) +pangea: $(PANGEA) +lib: $(CRYPTO777) + +doesntexist: + +clean: doesntexist; \ + rm agents/*; cd crypto777; rm *.o jpeg/*.o; make clean; cd ..; cd iguana; make clean; cd ..; cd SuperNET; make clean; cd ..; cd InstantDEX; make clean; cd ..; cd pangea; make clean; cd ..; cd prices; make clean; cd ..; cd tradebots; make clean; cd .. diff --git a/README.md b/README.md index 138fee8bd..8a62d7408 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ -# SuperNET +iguana is easy to build. just make sure you have the dev versions of openssl and curl installed + +gcc -O2 -o iguana *.c InstantDEX/*.c -lssl -lcrypto -lpthread -lcurl -lm + +the above builds native iguana on unix/osx + +then just run it and browse to http://127.0.0.1:7778/?method +you can use the gui to find the URL you need for an operation, then add /json to get just the json and not the entire webpage. alternatively a POST (ie via curl --data) with the JSON request will just return json + +http://127.0.0.1:7778/ramchain/block/height/0 -> full webpage + +http://127.0.0.1:7778/json/ramchain/block/height/0 -> JSON only + +the superugly GUI is not stateless, there is a default coin that is used for any coin based API. diff --git a/SuperNET/Makefile b/SuperNET/Makefile new file mode 100644 index 000000000..46779ed9d --- /dev/null +++ b/SuperNET/Makefile @@ -0,0 +1,48 @@ +# Copyright (c) 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# GNU Makefile based on shared rules provided by the Native Client SDK. +# See README.Makefiles for more details. + +VALID_TOOLCHAINS := pnacl newlib glibc clang-newlib mac + +NACL_SDK_ROOT ?= $(abspath $(CURDIR)) + +TARGET = SuperNET + +EXTRA= -D__PNACL + +include $(NACL_SDK_ROOT)/tools/common.mk + +CHROME_ARGS += --allow-nacl-socket-api=127.0.0.1 + +DEPS = nacl_io +LIBS = crypto777 curl ssl crypto z glibc-compat nacl_spawn ppapi nacl_io ppapi_simple # cli_main ppapi_cpp ppapi_simple + +CFLAGS = -Wall -D__PNACL -fno-strict-aliasing $(EXTRA) +LFLAGS = libs + +SOURCES = main.c SuperNET.c + + +# Build rules generated by macros from common.mk: + +$(foreach dep,$(DEPS),$(eval $(call DEPEND_RULE,$(dep)))) + +$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) + +# The PNaCl workflow uses both an unstripped and finalized/stripped binary. +# On NaCl, only produce a stripped binary for Release configs (not Debug). +ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) + +$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES) $(LOCALLIBS),$(LIBS),$(DEPS))); +$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) + +else +$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) + +endif + +$(eval $(call NMF_RULE,$(TARGET),)) + diff --git a/SuperNET/SuperNET.c b/SuperNET/SuperNET.c new file mode 100644 index 000000000..8fe733fe9 --- /dev/null +++ b/SuperNET/SuperNET.c @@ -0,0 +1,23 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "../crypto777/OS_portable.h" + + + +char *SuperNET_JSON(char *jsonstr) +{ + return(clonestr("{\"error\":\"SuperNET is just a stub for now\"}")); +} diff --git a/SuperNET/main.c b/SuperNET/main.c new file mode 100644 index 000000000..7c7eca6fc --- /dev/null +++ b/SuperNET/main.c @@ -0,0 +1,31 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#define CHROMEAPP_NAME SuperNET +#define CHROMEAPP_STR "SuperNET" +#define CHROMEAPP_CONF "SuperNET.conf" +#define CHROMEAPP_MAIN SuperNET_main +#define CHROMEAPP_JSON SuperNET_JSON +#define CHROMEAPP_HANDLER Handler_SuperNET + +#include "../pnacl_main.h" + +// ALL globals must be here! + +void SuperNET_main(void *arg) +{ + while ( 1 ) + sleep(777); +} diff --git a/SuperNET/manifest.json b/SuperNET/manifest.json new file mode 100755 index 000000000..b0838ca29 --- /dev/null +++ b/SuperNET/manifest.json @@ -0,0 +1,17 @@ +{ + "name": "SuperNET", + "uri": "SuperNET.pexe", + "short_name": "SuperNET", + "version": "44.1.2", + "manifest_version": 2, + "description": "SuperNET", + "offline_enabled": true, + "icons": { "128": "icon128.png" }, + "app": + { + "name": "SuperNET", + "background": { "scripts": ["background.js"] }, + "sockets": { "tcp": { "connect": "" }, "tcpServer": { "listen": "127.0.0.1:*" } }, + "permissions": [ "unlimitedStorage", "filesystem", "storage", "system.storage", "system.display", "system.network","system.cpu" ] + } +} diff --git a/confs/BTCD_hdrs.txt b/confs/BTCD_hdrs.txt new file mode 100644 index 000000000..f455c5c1b --- /dev/null +++ b/confs/BTCD_hdrs.txt @@ -0,0 +1,1772 @@ +885001 +0 0000044966f40703b516c5af180582d53f783bfd319bb045e2dc3e05ea695d46 a5d211145f8e6ba0920b2893d307c5d7c207ae0800a80955299678d1706ea8ac +500 000000000680a9a697eb71155b18a5827e0889fca28afb81fcbb46469ed7877e 79f80a8f54c6762d6408347c6dd7dfd2f8b8c191077c1d7881dfc5b7ec6a408e +1000 0000000000000cf908c887020f8970b7fe952f8b81164d83a87621dfdb581d08 3356ec4296ff2f04281492b0dedbaed80edeb6dd9170b87230ff79f6b0daade7 +1500 00000000000010e39eaa987e695caed67aa0f3c33576fd2621422c7c09152ca2 e9d31ec9f5dd4dc2688791edd4cd1abc964a7f843c59b948903c3492940e459a +2000 000000000000029e318c44be8a5e1d5bc8f7823907bad160aa63fd6121dd0ba0 15e2439021c4563c7eab5b91c916a0c9c24c3fb374b49ea24a4e2aec241ab099 +2500 0000000000000b813b136f623f85155cad3069031cde918b4079e907e1a9fd58 76ef339392ae45dbfeb2c6263e4e88b4cf888264d7ce8d0361258a81061fadb6 +3000 00000000000000e5ac27d3bb729f5c8a4312f69b28c9cefbb18b8fb55ee42c87 dee1d4ef373897e52847416837485706f5c0fe29245eccb0819ea5d32f594051 +3500 000000000000123b4e50db3f3706974f7e3593c2897bb545177d5c8a5a11fd35 75c65538d4fd33e5a77d5b9e5d756587f45a7cdf55646f1cd72f2da2b7ae5772 +4000 000000000000013e3c1513a7ffd71ba89d7440fcacef6bb39a76527815a71043 6ca70d4bd86a4dabdf5312a88a99aeb874d851df717879049d8d91b464109adc +4500 00000000000006e6d70e292394dd1f4aaf1ba3ea7313ae981a0b2f38ec4b0503 e97f1c03d9e0445a00c160c648d2f4920845502e788f5c3f2a5a392544e18d0f +5000 0000000000000278ac01346559ded91fc585defc88bbaa9d7f111b199e516779 45f12a1790418c3e9000bca11394719138f398232fd62ed3c801b5eb1e9199c5 +5500 0000000000000284e3d2455f09d9613ff71c49144823f882b003ed3177c1abab 149ab9c582053c69b64d9b3c3041f04527bcd47c572d600947d0f737d675f953 +6000 0000000000000bd43190ec79e54ba203c9387abcc8c1c03c52452a44466f24b9 ab0f1f81aaf4000295bec7b21ad5c272aecf7dee60c451bcbfafb0beddabd2be +6500 000000000000029e27c331ad9865fee87dd19e5ed0e526f9974510649616cc1b 9ee7b5e21984f8e3ceaa88aba5ab2d88dda10cf28c57b826e3bc8401d0bac0cd +7000 000000000000034bfd6bd049678a41563af84a9581d1ccaeb4952c954e327918 e09213923b66de743cf8edc0389ab1b6c07c0c79a175b3e93364ed73c2efeb69 +7500 00000000000003c5a246cab977e3f339041bc95aec5cddcc3d459d3d2cd546a7 4f26360f00347667d1c2f108ad3e2b2f599a599cb77b1ba9936988664ac53d4d +8000 34d2ddf9aa94d6e02224588d3d6585028a22f05f53529a19a202f41ceb36cefc 8bc3244b74f884ae355598fe0200a7f09bfab53de1b3e122020c4e2daf08fb63 +8500 87ddea52f5073e5283ecaf0efc637da3a47986396ad30b3adec8ea2ba5f6a09b 65ae81f7094626494fe8b984141c265f689f4d6abd33a3a74eb491c31c5d02eb +9000 000000000000016845387841c3d5a584d82763471d4b84af3921ee2832715e4e 11b2851886bc2208cd41c8381ff7bd6350379f6c0224f9608450fb0da56a59d0 +9500 e64e3a10f2daca17836d2bb5868e76290da1fd3e0574da496b06182033fc70ac 5d6ac130a3a5cbbf28cf389cda08aabe9cd5bd9a4da2d0ecd19c27d9ab2320fa +10000 00000000000001634bb95749a866092348aef266f9169c3ddbcf226d4807ba09 986b59e97d259c74881bec99d68adad8e219a0fcaaff6af3fa31b0b5a89e1b33 +10500 9a98c541a5e55bcfc8eb6d887d83dc6049a33381fe0aa3f4766006c1fb066905 3dc29b2469167c07aab94fcd87c03e0bad1a99d47c3594d80b700b05190eb526 +11000 54bafbe72c3547248f553615eacb2254fefe5a4d4cdcc66a2efde3e1ad9f3354 7113b80a2fd13692c85f39cf494ba0d30285981b4829caaa92b4b748df134346 +11500 9d6f0308699be5bed63c54a2518b7a69aa2e7f5076202ec077d3d53159b291e2 32f1b2236a56fbbfac8b1a72e3e3cfe24531bdb2c38c2619828d11c1474df677 +12000 00000000000000734778db2ce455744e1a50745ff4d10ad3d787e49c9c2913ba 712f312d18d3c817c33e522dc5b7b50b9a16c0dbac13e56ce817e8b6d7f75ccd +12500 00000000000001209c8fe742d1a45c5fa9689f8622217ace049fa3864ae24321 0c4bf64e0024dde945ddad558a13bb180d931ed8d6ca79c13112a154db415794 +13000 0000000000000064fade9e75fa24d1d1974f5ca69e84e8688ef0a58b9dd2dad2 b3411ebcf6385fb575beaac292c70d4e79686d832d6138ff3b68cac8fb8e96c4 +13500 000000000000012cdd1996be2f29e14c0654b11476e254e4ba4c6829c6887ac6 054f36fdd7e83ed836fa5c6ca63ee54929fe3549b45da07a1efd1cbfca73188f +14000 00000000000000b930521b5956b17943d5dcf001b4d319ba7157bd7fee6a6fa1 80ed5287e00bf627a20e525cacd1b6ebc2077a3e2c71a400bec64eb7b80046f5 +14500 225abc3eacd079cc0949cd8d0eb8978b5bb00720da7178ef5652e70754914d88 c87e7237cadf805554ab3f259bc89eecda51149fdff0ffb29925712a218f76f8 +15000 000000000000001f08d59b246a225efd70b813c3df8e468bc9d99d7d686fedc6 2ec038bb14816230448f041cae3a9ab64f4e237a53cdb0416dff8a2e062586b5 +15500 1181e59d86d7026c523659c152db3732c69e56a04ead32a0617be36ad3f2f0ad a2511b8ee2e0158dc668e0de527e71455b7245c01f41ad66e296118715aa33a5 +16000 0000000000000015155b08a0f074b1ae3c960bb90e96549a16d0b3eb20a4353a 05409419f76edb4ecdaba62aa65195063865344c93cfa1e35bd41931201830dd +16500 91d0e76a759d19aed57d2657cea4dbe861e849b57602a4c5c69028d24f795fb5 767a41f042292ccf2acdb0b91163b18dd6b59aae25ba60a8557a01f02a5db8d9 +17000 000000000000009f949c3d1a6eb1d50cbe5a3638acc49987902526df0d4c3eee dc090785300641d04d64f572fa7d645faa11c4677d74ff356bd8d51f50c0517a +17500 00000000000000376f6a1ca86680e16d0127507a1182507e502c6cb85cb6719d 89dd74d7a8b4f5ee127e7b4aebe54f4cb2c91c79fbc3045a59e5f94227ac6b12 +18000 a02b59af01d4fc762d8a5708b22be769b7ef651afa88ecada21eec58dfd53cdd 42abadc5ef27ee4c954518ed1a1e865a3b2bff3683baad8fb35589f8953ac5e4 +18500 00000000000000dc2e6182da1a35d916b98a4778fc35c50de27a770b9f63daeb 7a44c2603b3d31b23a607a40cd63f9ce9486a7a3de507bb26f6ad7c1e0e75466 +19000 00000000000000017207a368616b26632bf68e16984c098be64d2fb064d3a2dc 190ef6e670cda631380598333765702648c55bbc0c92d68d3c2bb5f76bc546d6 +19500 00000000000000ab3d7e8810b7ca9f8b61f630ec6e596ee105cbeffe80c41d4e 5bb4af1b5c912873e90a93c0d2c998b367768f05fa8650246bce8dd4e8b5f0a8 +20000 000000000000003adb97f0d6976b906982f3272ae0d656e1c593406b05bae17c fdf85ac01a396b796ee5dfdf3c7869dd3f7eac36a61bc7a30a5c7e8a9c5dea1e +20500 dabee81d7aeb87b59d94c497ebe1552b3f5a5ee1e366ce64143e9fa426a6462b 017e99080b6064d5f8852f6cda7563f8bf299dd15370a64ac74f89f22bb42220 +21000 7b549feae0c08f58c44ece421b89b0626d15547fd1199ff172704a51476e17a3 50e176972bbd1528d333c01501e05baf97876b8a51030cc3dced40c1bfd852fe +21500 4d7df2acdfee2592ee66f6498c2b0de972ca6e0c8e2ab38eb641dfbbce8aca04 e10aafa5c02a56181cc6ed4b48ddf1a52370d95c99678bcf5fc6a106df343bd2 +22000 1f2cd2094e85ba89286a183da2b9135d94b2be2c5b3f31bafe89ab3f631c5791 b97f1ff53a6a11d6ad3b0ff270ed2bcf70500069ab5651b8466110d472a1651b +22500 e797c4b4ed2720b494a3501a32a247f7acc38f526245ff89c8f148babbbf0cdf a5b1353f344534e8a9ef6365be58040355391297cde74c723c6a5465e39f727d +23000 50b9f76effc822c9f1051b1c0ce25bba6112a31d32a055520d84ca0f1fc39b8c e12e33c6b273416adbf598ccec93ce0dd6a3fc817c6f8e4753064e9ef78e89a2 +23500 fcf644a9930ec30bb933cc37061a17d0edc29bb16a04ea4bbf8b27444d442415 d5a23580de1505a7df77130fda147aa774abfa1f16468a55b542f69bb6199019 +24000 144f4d11b3a867d17560ef3ef04362efc715c89c30082882df0692a214cdbd75 add23a1feaf6ae651d6d322142bed5998f2d3345fc767082817ca86139d2de71 +24500 8d8fa5a290cae00be37053ee99cf2ae70b37b4806b41575a09b7a948545215eb ce758bf937c83d93671078985a70493b6bc1ab33e680143c6e93c2c5aa3674b6 +25000 b5c67f326ffd5481bdd18f140c03ac34a9d467bee8116c7a559a8777c688ee18 2e618ba056f201573ae6222928d56fc3321a3b16c32681dcba8c07587ce581b5 +25500 80d41cb90ce050cb9db84205aad00db5761a465b000b8b3a74bdf57e7bc272c4 fa5e727492c4a83fe816724e80c54ef2add090d60c3fdd788c44721b67331726 +26000 de58126ac82bebb58f841f75602b4307ceafa0d9aad7271567ec69d734b3422e 3931c58b417157cbfa4e990ceb75e8f2aaef945922962b671c17fc9076be9c7c +26500 857f050c1a4a323b2f9d808bf2da8e11a9d691517cb0189ed2f7f103794bcad1 a7ea3d0c368fe526c8a1a79c9ce3b5cba52634c324271b3982c70e2ec1698800 +27000 43b14fcd1424ed78b3cf03615ba16c8fdb53c414e894a6fe555b5a0221053ca9 6719c5530a1c6579dc00a6d6bc26c59900d5437d4bef8d03fc47cc018937a52e +27500 503b22cd2997d3ff79a7857530f975798fe48a3b46bad0a38a3f83653beb8d1a e013d7f398f337d83d758e6af4eec6ecce14d080ab95f037e8c4354b1e4265b3 +28000 85e03383bd64b7cde88467bfd7973ad219d635708a69f925316e958d524c1b2a 112902560ea7fe15351297834affe49c0a8e768b587d1a8e6574ae89903d8143 +28500 57c1ce655a20e5d4639e934776edbbc5ad099c383b62c7bfc623368a2c3368a0 c3e03e5754ed76153656787b565a0143d538298277c55ac0ca79e99b0ea134ff +29000 d9963c1d22e68c2492bef7a33f34bb7bda7b326b4256104ac1ef7d6421862c5b 91dce271c5fcec2b60e9e9a7d1670f191be7f8e76d3d13e75eecbad77dce8173 +29500 43f543efd477664f84ee78bac095d5413449e552ef90204f79bb8463ecbd5e89 4e93e04161f98d745707c6c7a85bd4b223f124c6dad64aa7080dc612526329f3 +30000 23da6267be7a1511a4f9892875bd61bc1ae5d1b35063d78a7a448379db07c40d 760c525285ec0731ff71deed949476248fde392c8bb15f100af786b7d7e60642 +30500 199d170a784a96d70fd6421057653141b423cb730e577bbe490c4d9d5b31621c f836756815de2b187d68e2729bc667522ac6f00379759c5a7815c728d52fdd6b +31000 a2f6774be3decd68c4fc0d55249a61d73333358b03a5d7f48f19ff53c6eca193 a72c7c5328570231dfa7e3b7fb68ed42e4b4f9f4e05505ea4526779d279b6045 +31500 ead2b784815b474f24a12e57068086c7cd2b59051e4570eb4eff43c00079120a cdfb920e73fa2ab6278a84292a23123bc921782c7b9648c137ea3b5d936838a8 +32000 51690f90531b33b61489e34c805c7d1235708691163d0b25a9afe161b5bde918 44ba77a723c883778528e872d4cc6779c6485e4274ceb2ee7c1f219274c74297 +32500 b8f881e8f20864ef1db26eccd81bc46ce2b5dff8359ab86cc0b915bd6d1437e9 f390ac467135ddb1d88617c29db574c34ee51a4a79cf041bf7444c522229f8f5 +33000 8c8c61289e79ec3e22d294ab824743c29194740fca09ab44d460291599a33186 24bf35eb0ef662470b75fe38c332729ec8cdfa122a52be3d81a97bb9cad009d5 +33500 958755e9cf4e957d0471cfbeec0913140759fb3219aa74096c26cba2536579ae e88ae01956031b06310eb260b95a76e2e4e9b66d40df56300ecf108cb4579c4f +34000 d4effcfa7746bab24f9c3d12bf94680770ecc8405b5b0a17bfd0b33c2f4f475c 9e5f3f87097c229ec5011c8ce05cc12a81dfd166741ad2762a0f156a8d3b3928 +34500 7f971e1a0d2e94dd8cd0ccabc451fef38429ddbf2400bd45278fe000210d5f87 8b08d83e6bebcb12ae7b2b9fc1de91a0566ed33f2c40125d7810e2a97566a28a +35000 90e4d16dd67369b4b5795571fe0cfe63be8c1796ecf5870cad20798a752d92f2 619428a018aadf7a144b3981ecf018bfffa7975819a58cfc0e16f9ef0ba274e9 +35500 03aad5e354d5019e2cecb5a44c4ab79c7120543370cce9830b1d41590a59706b d94a9fa2f0ef62b409348c56bc376733e50fcab89ecd7fe711845f66c03a8409 +36000 5659c47e9c1b006ddb991b7b0163777977d04d70b4b52f4e3b87bc3aae6fba2c b1cb79822ef346bdfbcaacb59b7a289bd07b5cc02c8c6f9c9b1c5d27111c892b +36500 166c22d2278b5d4960af665a6dcb8c4718ca3c489fb12f6744a60337a6ea4a97 b55d2820c96584bb049d4f57766a2fb7b21ac2998038d1dad3c39dd553309a5d +37000 0c73b35172fd5a08dad4570ade1ae552008a801be8f2df85e52ae71957cc8bf1 66fceda7365584ca4e28a192d4716cd0bc61da166e5c6036bd2dad1ba0f034f2 +37500 e25729880903dd4f6562deb2c3e076c00d29e08e9c531518b2920b3e747ba6ce 886750b60b53c72b8d9bd1cf744d74fd3947ea62b878fd186618a46f1ae114af +38000 fd1aa8f0791fca7ab540a37b8fe5a6a23a84d0bf372ee35d6b12fe540b53f746 3bb81e40ce808ad93fd0057fe4c8c111478857f8ee143a27383a4d64f7c9cce7 +38500 73dc7035d76f11e288a04b87036996efbce8dc6233d00a1238f40ccd95a4ddc2 b8f0d99842fa481e5b0e6011ddd8abed25c9073b259e37ee7bf2f451d4ad2c92 +39000 f0e3715e1a7b82521f8d39fb490f261c5b8684008f5b52fbca7e5f7b204ddecb 2851428725cdcc924d42f907200a50ec708342cc1025ad035dbd7f84c0818340 +39500 16693e3a20a6c2d2d823b7c3020af1a20d555401ab098f94ea8a83b7a2a9af4c 400ac0ebe9428f526123f90c8bccf69a6835ad322affdfcfda314ea795e1a1b1 +40000 fbe5242823fe378d36335366cf45b3309e8fad0df3f9dd542e10eab3b7a296fb f73a533f1c917595a88ca8058bcf54b2d8e3c8cf8c46a165c84ef124fb07d15f +40500 448e82eebd5d00a7ed11123f43f8ee955790cab17cfad0fb4f1ba154b20cd1d4 f6c092a7945bdf4ce8bbb08645a349b208b054925376821f469309fcf46cb4de +41000 42e29e0fc3e8fb2c517bf4b7d24175f7467980d793bb18d4b1903fec0292d0a0 b8d88a3f3968b88b98e51232a0304506821c7c83f405515d2acf89a6a39a7048 +41500 fce90b09826a8120907462a95aef4b8dd9487efa4b0b2cb7e90a4d6d3d40e4a8 2a4319200375bf38a272bba432cb62054c4863db024e4a30c5bd20bdd569b939 +42000 f0815d389fe88bfbf5c361762efc5bd1264dd9256bf895b8dc6f39128e130154 b26d951f8be8ab9091f1e6c56a539d1d7a01b50c265ae1c025b698f3edde5b6c +42500 97171a32bd838c19f12586fb03992387b4b4c8d1342a0ddf3397e44b7c9b432a 04737e3fefb938c73100a32ccea93455aa0a564e41e608a1772ffdd2e0d10033 +43000 a1ae4df09a7e3f060a6693d9bd75af599a658f501b0a5583442aed2bd0393dff b6290ff23b70623802b15c12dfe3e19111ce081bedd2f24a0c6b4ead9d8989ba +43500 85e2d7b7fda1433ec964d4ea6cb4fa7af661d38f761877bb9b0aa0acead5e437 8dcd8aa4dd692a271a4e6efe7b8314f2fc5adc4b96bdd932e2ffe76b496f1347 +44000 7526450969b0254a66686dd28d61c10d0bffafd0bbb623d0ac71f6c45048b195 7c626106e228c853ba6d6c9d63f934e51661ebd7ecaa02d66608bc2f1da0877e +44500 313c4a7250c92bf888dbbbe977f119854876d877cd7ebad97da3e793e4167084 e1da8d161f08c6e7cbff74ccfcb80644b571747988ada991c4982625e59d1a0b +45000 9fde3334234a707dee9c6a2e3ef6069a30c0bb4293555f59ac70ec398c818e94 987db78e383fd57efd0a1b0b0b8fe38c50dbcd648da9a1eaf5d5f59d0c15235d +45500 30f5524584e0e3016ea738b1984cc7fdbac63956d6a154ac474ff28c873eaf02 2a6909db1ed2d684bae8cacedac0453a13e37ef1485760b0ef6329228a71bccd +46000 b79943e9ccbd2513710f58b3a5ac822bdcd6f390afc8258ce7fa1ea6822daf2c 1db10c590ef1fb219c3b98c707fa294e263bf95a4ad26c2c214ebb19d4236297 +46500 9680afcbae2cdf4712605c9d592ddaeb87f9e8c522c6b5f0beb219e71de389b8 75ab5e40232ac575b25c0b892af025d5405aafe1f808551e7bd33abdca9a86cc +47000 1deba95895fa7fa09760b2b6f12395e6a40dc7ed3b14e629b7d85875e6913eda e735fd2aefe57540d02327e774c7069d08a3ec5b35e6ba939b1609d98a29a245 +47500 400b71a80cf5c7a408363c4a88a6f17b9fee56830f9639bf682cca7f6a1b9ef4 179f44d13af45605ea60c9fa6e0030b5de6c274104649283e5929abda3cabd0b +48000 b62fdb21ede8c1760346b4325be6d08852ea3944fb0adea0b408742977fe122a 8ff82f9540381a20ba68681f6e5442df8852cf38b2476d0a6bc61162d6eeabf3 +48500 baf6f61655d7a53726d313806841300c1d729f7206f62a267c1b4c351b365763 fde6fd7055e1f6e03b9d327b3c502fc02bac00738ae0fb079f128e2774d0078a +49000 854ab5b99aa89fc7d03f0b2d7f8a54aa7c98b23957520ebc8f4e6305902cf2cd f39213ce4a4fb7c2de3088ffcef3712113958562638395dcfda8ff6da621a09d +49500 c964b7057db84d6b9282baff7f2f09dd72fff06dec110201904bfc3846923bdc e2c15ad8f20ec681283af4d88c56e1433c9c20ccbd11e269ea267bbc2f9ebf1d +50000 fab7500f5f0b1694a9c65a4b02e84dff228af92fa447d810200a3b14c35242cb 194fd02a69a66e9293a06d354206757e2bc23484e3e8cd80522ff71feffbdff2 +50500 ed4e556f251680672e442c00cf6a48f3a787acc589ef3e83019851808ffd05a5 b51a4252fdbf12378dae10ac1c94690365e096f786feadb8e75cbad87c8f8a99 +51000 2410005ccfce88835da97372f39ba6708e7eb10f97a4428ba2cd51e8ee87f7dc 64e4a8d5a545ffb1af7a5dc66be6ff2aa517a99c7bc5d7b2d1d1afafc1b93a84 +51500 ae13af1cc26f53deaa57482111239bd24f99814dae1e01580a1957f11aa49a63 4a0c54d391ad8bdc303e226fa1aca24cce0ba24370424396bec9917804f0bce7 +52000 163e369cecd2362ecebef4717e206124e3bd47dd40104efcbbc8610fb48dbe46 3b68edeaf66e8bf1e1b187fc0a5e0424b6cc896661e2a4f44882cb060f9815f9 +52500 c3e5334bfa7c8d5d577b1a74df2deb553a006f68b384ebd44eb7bdd7aca3a297 fa5cd9249e7b39e2aef8bb19e83e214d77a45b4c67c4504522f94baeec2241f1 +53000 3a790689d0f51fe7dd6115b7f0e5f5265a3aeb912692596d2611050a17795705 23b602a52f2e393f2cedd9c8ea0597931f6abd7b4f4f1d85f1de8d6382c03ec4 +53500 70a1da282b26b9dffe0b36a6dcdfc0c4bf218a86164adfbe7df4cc9c0b978969 f6ffbf0fc14817b41ce42831d14add2f8ec22307e5b6baaa9f07dad06c387e8c +54000 630d02d2f2a8889fe47a4c2235a60b2dfd31ca78e487860f59d24637a2032af3 901287c9cb47c082b5329f422d94e71def436bd0979e939e7def8c60f99272bd +54500 4943f865d3e75ddd060f8145558ec7f57c10d186b3bbd3d90377be4713e1fd22 26a8c618d328579faee0816d1d723736a249dda1e95647ce7775b0aae5b72854 +55000 6e96c77b0964fc2077455aaa2067110351212e2ca707fda778c963fbbddc844c 350612a0426a630a0b001ec51d875e2d1c87d20353a480539e5fddf91cb3097c +55500 cd718716c94cebd61eafac70f8562db8b48735765a8613aec79d901e0c5a05e6 234209073d425564622dfa5268e7f2af5aac71a18e2b15d23ceff1da4d876563 +56000 04002106ca4da32c5c7889c8f17369d23a35bce62a8196477c8f8e0e0dbe4efc ac25f2042536f1a588ac6747b2ff3b45de9aed063b096ea59632b3aa45441ddd +56500 6460fc580380db980f3b76e9480b2dd98f4fec5c9642b211f7258f50575522c3 12c289cc82037f732305d895742cb703eaa35a4656faf84c6f7dd9a2783cd2a9 +57000 7f1a59ac62a06df2d3d82226e03a1081fa9a8c72c5333ba485565f9215423362 5363e6896a5eac3c62543b82b6260a18b07597b5963d038e728a9508ab55b181 +57500 88bf14d568f5b9d8e88d27a92f2aa97a569f371850be3d6d1ec175fd9e1dee54 56aa31dfeccbed31839cc2399397a826f2e64a16c4fd309ba87ebb2ae7e7f695 +58000 0ab0e582e675d0ead1173cd65e71157f13841b22560251a2aace14ca86fd66f2 bb20d53b616962e78610b02a60785252e90c54d78f33a3578172cde77075a776 +58500 637d24d3f5e56f5481735d0853d74cc85c170dc95dba4eb733af11ec3ff048d4 aa8d5505be6a30af2a0292fcd19800b0744acd87634bc49ce43b0d767117a5cb +59000 2d88b4676741afa7b183502fffd15bb1580f81b4b8631844be86bacaeec0524b fe841a70acbed4f52a83b35cf30c0cd5ad38b36b21b24da1a0ead110f5e7b2fa +59500 e5b98e82ea2968293969b1da31bbb8e30dec3ab5cf10308d4a979e3264513bff 17429be3ddc868c9e3662b5aa6223340d57b7284138db428c725c8fe69e1f89d +60000 98764fcdc2a2f8a740a4a1c891ad966b44062f64195effad2ba0aa4bb9c4370b 55053a4d083f019f602ca159faef86ddf421c4c4c3bcef9f0849f45e1f8c1c44 +60500 24bdb847b0ae121363eb0d2a7ab69d672627b4d68195fee18df5ab5c076ba3ed be197241a0c377fa6982b5c24af043034024f889cd3ebe42d4a9b3fa5dd18953 +61000 54222686316d912f144e6d0bbaee1b3fa50549e9da65ffc21b04c5beb3ec6daf 6c5a388a8dd7f9d104377e1f5b2817b50f9269145da48dd0d71f48e2af8b8731 +61500 f14bee1d7d3830f8c316f448ca36fd0591371a63fb9d04596c657051ab4c2ff8 86c9232b738073c671adec011771072c92ed0616c361bcb6e9af6074d09bd6ff +62000 5903eccbe3d0442fabf30748705e88e9cd83128a0bbfbef66841f590b5011bdc 4de78ca5e16fc438337a8d5f7aefd2cbbc66790d019a1bbc5de082a79e39c303 +62500 35db59c0b059705518e07b3beab25716c9fa347a1fdf7bfa0089d011e2586419 961e208bd356ecb07f838ec1d1cba0eddd79d5e026ae350f0945cc321dec970c +63000 5fb6db58962ea9eabcc50dde4f473a0adf58eb9a409665d57cea3b2d3d2f44ea b673d848fb79ad142be92514c27dad64fbf98d26e1e4f5957376bcb2f8542288 +63500 92e31a33e15bb677aed01f2ed669a2700223a34fa7342270275561ed5b88eac7 7932d5bdbb9c2fd7f172a79a9e93f73aa3703c284afa98e7530ea4ebbb4ce7fa +64000 065c7297e545fd1739d04c6ed41da4a82a24404c7b3573bbacca0c266c4cb716 912632c93e91f5f539b2f9d6ff8b16a1817c8eca88b6d579dab56bf4d48eb31f +64500 a011434ee029b50acdd49262a91b0767a897a5dfa7f5c22dda0a32aca9ff334a 37993dc2223b292109f02e9eda7db4178f6602aae957b80eee7cf94dfdcbf020 +65000 5144e900145e481fb1e569b1b522143348a38052d35452dc7d97e2140b812e26 217530b550c589b0de7d145fc8b1059215bf5c124be7e79b5854acc200b511a0 +65500 6db49e04b2c1ed108caba28d4de98628fc786f5224b098de1636a697ddbd6e6c 5cc92190d39119982836cd5b0759902c4c61517aebaba16e6515cfca53bbcba2 +66000 559d2603be7835ec386467221e23836803606f7fcd7068f6a42ac5054e48fe88 68c9698e2684c3375d6b4d83b33155e15ff1d50aaa0fe61276a854f7c022e3c5 +66500 d724f78b706b6a58400a729586b050fcbe843a3e632376f24db6fd4cb9a44177 968a75dc8e7e9128b5e7e00b22a78fb53553102b54247bf644247546a9db85e3 +67000 8ddfa37cf395f7b4b8d6d09e29cb7f11e7d06f929d7ce7a77ced27c79011235b 9cba1d323704f1b566fe4b639737ca2f6f96ab6f9f1373d95e1d8cfef0a0a1fd +67500 2c5e2421936073eb47199fb03dc0023bf42572f507f484fd9181eb1a862e8972 4928ddef10d983dccfdbcce08743afe2a0d1238fd84bb4b6e1781ca7847d6ff7 +68000 cf49fb97e127593bcf3591390401803095f71ea86922369e573b1c6bf59ca692 accdaba238ebdebdf85fcb74a46d17e5c29a996e3f1be6147a3f6027a13c8412 +68500 eb68bd36c8212efaa20bcc2d1c17f17af6ce65b060ca7ef80dec2f44052defcf e04d8a45c474fb6ab8071cadd8fb14f5d208472089f6a22ff5c04904f7a01d55 +69000 03609399659eacd9b55c608e6f35195530a35358812bed13782f9d5df5da1ebe cf4d7449a4a7aebba820fc84f3dca2a48ada14feda094a425833d4a5aec834aa +69500 1d23b15a2484a71d270170f32918f36fb87e80cf4020f6a3e6d61907276fb3a5 2a218f285edae9995b3334352864684c66f4a919c92031ee3b24510b568f6246 +70000 5667dd2604c90cb15f49a75456b0bcc2a0098bc025592d48379f4360844b79c5 1b6966e82886048d64879d2b76663fd70689c7dee808cb42352f57af598f9bca +70500 745715d010b5ab51456342b8772a53b95ef0cd409980df39c443531f2f6c2346 a1ca05cd11e6f3d9eb2f0498b8a4743defe546c7602c3fe06642f0226cd22719 +71000 c622f3d2de9a6aa2d649f10532164082ff6ac21da796b037f2b2a210fd2910ad 3c24535ccd8328940b86980f1797d0c4e0dd0fed2ada72a0d9ce7927f4e3b040 +71500 c4350561b063c19326afb0b38b7aa70a65a928ecbe3a545f26f1dfba40d16599 7167266f09bc1b259fc6dc6ddd1c4e03514e4b4e5a985ce79cc320d80f376e9b +72000 df857691f4e2d2ad6a7de6a3fd77cd3e1ee19be220f7f92006e30c4cab62eb8a 1e962f15a4472d511bc049be123041f357376d95df56677c32ebf69640fa5459 +72500 affa0299a9d64d61176179cb14978c6dfd116b0350638b5e2633c5a73bb9e5ba b04e559b1ff6d04f2fdd2693c384bc89f05ee28f5ba8cee49cd057c575b0acf4 +73000 a50964b3523aaffcbcfd0be0a6fda32ceb87018450ad2ff81f1ba61de5fa20b7 e3301d1249deb8b1065c59ddf02c4480af9208ca0fe70f0b94f7135e3baed857 +73500 629acc1dd038725bbad7598fc49a7178771332ca4f12372248da579ee5dbbe54 8d2425646be01e20cf3554feec43cb91bdd92f0a6e8791c7f8fac4d21767b45e +74000 068954a253aa54239a0a4169f50ee7d37b0b5bdfcdfe9719ce9921a5917715eb 722bb617216427a0cbbaa037d1234bb7d6765707108aaf171cbe14e24392a8d0 +74500 e688640c22b3c55fdd29e86a159e1cb665cb0937cd6a5591730a1b5de7c04b76 4ed22bf35c9c19ccb01db47714af7f2ed0a7d2506f40a5e43b92b971edf62250 +75000 31c015b503c6830b93948a51598beefedc78d8957af82aca6ccedb2aa592de8c b75f99a073680d704089e1a77cca65d027722aee3a4fd7c1f9578b535150d5e6 +75500 3eb906d2939c3f6ad815a51421f488025326b8cbdbe28c15ef32ba410b7b8ab4 39ed4ea10fdb97d192dc03a86a397b24f6883439e7ff6e82d9e6738cb90ed5d0 +76000 d21f08b186c4d763e03ff71b1ae4e7c07a9dbd30c518242740c2c93119d8d96d 3898b748f911a413cf66daf2e319b3e72ad25ceddb77f56a7de9165484405cd1 +76500 5c8f54cbdc2f5f83621da3e57c28d8e19faca81a438cb5f4aa144a4c037a0055 979a3726654c4e3bc195da85f13e24639650c2d65c460c91ff1b377e27a3b73e +77000 e5e2e50e27fe46845384682b593999c03b4848a5912bb45d22974cccb551d125 75be98b3706894389661b6e9c20057b55cdabfa26ffd74bcaac8fc01ba273340 +77500 be8371eaee6d9fbf2c550f71e9b41d135201b8b4657b94e6660c908f239f986c 32b8f8fdb525510204119ba84666c0713d8f486328a98a130c166f36a2582523 +78000 2eff5feda9ab021f5afc380a7f931e7d52e7fe2ff3e2cf1ee7fdbde0eef82b64 65fbc737d459fa4f14f34704e11ecd9adfd51078f45626671bc698ae18d849d6 +78500 3018bc8d8b545e9ec88e936af13fee6bdf3ede83f61d2bab5f723803e53e0062 7b2380f5e4d64db1aefd218b868cc51902a54d0a0db42cefed99c7e8de87a218 +79000 b0c165f1302d8f19256dc2e278a7a5ecb99eac7c998306a50c8b4686a077356d 491242e8fb5fc40eb435f931bd11bc42428634a5b83abd1fb9f19059712b1ec5 +79500 68a1ec922af3091b3f2d59824979ab0f53a92198465ba557236cd41723deffb8 fb4a299d949b55ea78834906a69ab9022a49d8de653310369fd8ba9af477b7ce +80000 3b7489d3c34d0848f031a2325269daeacd8d00cca588b2b0155a81bf5bead1a2 9ec13567c8ceb32e9315afa4321f0a088d662f7853d492e8fea9aa0825f0c8ba +80500 a4bdbb1067263df39ae9c3fe98b7e436bfc29fb020ca4e0caedc4b92409cbabf 5d94d51295cffe3419841c9aa1afe3a281863f571789ded22dbee32e03726946 +81000 ff01efad3f1d35001246db4167727135f57b0e2603185bd8f43310d2725b4da5 41766254f9700bf8590b6bfa6993eb56e866af75377438b7b7ec4aecff1062e0 +81500 1e6d76e93daa7db4bef0b866414eb884b80193359343f7984cc4ad813f10a555 87b53a2233125cc8b28fa7fce0ed9d4030665fe83b9e63a76e545f7eaf332fd9 +82000 f77522692699eaa7adfa876193ff59d445443d55389e9110c211d4892d0e4501 a283d883a0705d2dba7c61cb45d6019a7e464aaaf297d8cb28103358c12e33db +82500 ce6f78297eeeaaae876c2528e2620ee91005af068e45c079f16523772b1ad1ab f52cb36181b62da6142382debe8325153fa672ea2a82ae0f4f1f05d3945ab944 +83000 fb926dce75fe0237ca38f35e58a77ce4ee8be04da61b16f360b029ccb889ff75 28adc102bd9b8be9013e2400e65d2d57750e995bce1b03ecdf1cee59712f359a +83500 f41912c2e881115560d9b957f91a8463c082efd4b887d3a9762fa7aa70d7dac0 aca22527fa30f5d0e0a8f2505a12fad648526bf7c7a0012b2ea02ecdf55b6fa2 +84000 04c39ca3103cde71f02315b339936f58cb04aafe9990c46326733493897f848e cacfdbf9b2ae2aeee8bfc355d807badb9cc02835889a999e93c14ea7800d54c5 +84500 5f40a8c35de9dd97ecd7244e84a895deb4fffaeadc22ed8d206d303578471ad8 e1c2dc0617f6e76b2ab84179c8bbc55c46d9732c6952beabec2d14babd7fd9c0 +85000 b181b9521ee2b12c19c5db467c39855af3e1f636a4086e1c6a728e2b45165402 8243d308e207fc44be4180ad0a63916aef3877e6278d30cb6f795d463541d26a +85500 b010687cf83e100f2d398b3927659ca342b25e443147badc9be5f13331fde93c c635b1e609f40f3bbd4aaee0df1ea6b27f18094f1d00377f69430b6507012e88 +86000 291fee34f060bf3d1c39be96f5ee8cf18c625515947c5495775924f5702abfad 47e1684b5b4942e0719228e060af33e8f13389619e91925d58f86408ea149035 +86500 bdaeabf298427c06e09a2e646e10ca3d17056bf1ba9619371327ccdb3f72c09a 4eda2aae345266db3c1d7f8e49699c30e70c8675735a83972d0dbf0db2372c24 +87000 8f8fbadad337ea15f63bc845e472d58d4c6467a516d77d08fd603ddb3b98d823 2d021df329f3c3bd4a39a5ecbae773b063f4baaf776f8ffb519b0fd4d803373b +87500 109c6f62e3aa5e0ade745ab3544f8d055d4ee1bf27d217bcaa0815f8b31118f1 1cf68acbbe94735132fe7929ae72e1fced6fc8cf569bfbc088a16c93c3e9e403 +88000 f46e00026587a254cd7a382218d9f2d61d37139d7c3c4f0ad76d35c76a2e8fd3 fc2e809bba17e3198bb92edc5999820f19a4d7135330b2f7b055252ab91f4896 +88500 a048c5a363aaf55cee60a2a4286afc8199b7bd0d57d5e039027d3db2a6cfada8 fcddf7448b220764d0452dacea67e5a91d537830349e307fdd20763143f92bc5 +89000 95e0fcbc78961eb79d44bd8e8055a26e77056036cb235779fe1ce22086a8705e 3454b0857e99663789646a938c33241ff00e4f7f853870665767bfe04ffce0bd +89500 8ed9d4f7f4c3ec8eaaa57d893a9464cafa4e2ea705756bc00c9b9719d51ee56c e836a5257dd6be6b167b7cf6c39b17a2433b07e86b2ceecc6886e57cddfa2a94 +90000 bd2cdc7f05511794992b0ce31ee8d58e428ddf12874905f102d2ca0a37adf8c8 e182353321678e537290c24a2a9c09d226ecd91044cc139038dd134ec1ef1a73 +90500 a996f4865ec304be3972056e4fa5890baaa296db692f4442c5b0b7337e75054d 98a91f31ee5cfdf960e1fcda3ff3d8d058471e2df6db7bea0e3937dd09baf06d +91000 f6cb4c4b3fe3b7365cd9e05f757498cef6da8eb0e6730f4c569bcd3700ba1d2a 91a792fa965f57abd8c8c95a37069682f68a3ad2112883779fd33fddf3cf7368 +91500 4421ef207f0a56fe208ecf3cc145cefe926784dc163500dd5018585c6bc4c0b0 2870e9bbe0c6ae16f66d6255485d6c6c5c198e16b7a721c0a027df05bcbb46d0 +92000 eb18cde79b36e5cfb7047d7ffd72374892d294d97b19965f67c264a42be981c2 77ce5943ff19145cab30c4b25f762f1e93afbb25e284388f88b4078fb934db67 +92500 2a1e0986047c363c557e0719dc464f44cb2a5530f36ead6455caf22579fa728c 3fc8941af430a2cbbe3a9e51ece9042410bce0c9eec2905109433485e8dc6eb6 +93000 759bd6b0b243739d3d6c28227f3a9eeca8afc6d341d17da49751065ff6d9027c ff40f2c094d151ad356ff66fd39eaed507a78f9d3c4ed2ccde744d600d3ee843 +93500 0c1648e1e670c38758befb41e8fc48f337f3b9c2aa8cf162c2856bfd44dfb4e6 e417032b5b7a38c5a9812822810148a15818047c2e8364ec0766a33cba25ef34 +94000 66cfc1f322a9971bc39ee18125a9464b9f82eef9c314670e51261402e50086d7 34ddac2e7b943cd760ed4863497ce0ba2a723bad5afb6f391082f1f1603c5a51 +94500 0bf23f1c069fc47b2289bf52f8462229a39ace7afa517a43a52276f5cb8a0f00 6d333049619b2fa4f612c1a6bc8bf82ab8a7db7cbedb51a258ea1bcc33b03f90 +95000 1781fded2a4d5a2ab054b04de3935c97fd5f535f0bd109496aab9abbc6564dba 18990d4e8be1e75e4e97a92e16bb4fbd805afa2d419629a5eec4d46a269438a1 +95500 ac453fe6cbfc81ed0c9b32241f121410d4caae7ab57c62ef99e08750f2d5f153 e3c4ea5da181b0fbd82e45e106a9c3dde36c28382093708d8cebbb72d001960c +96000 e464e4c5f135dcbdbe41de354730b1f633f00f458994d6ab3e7b272a5335e3b0 093f11b3784b1ef794fb06db18424f84c6a1bea0b9038666ac5eb0d36b18258d +96500 82345f602aad0d7ff796eaddddda22958f4d3c9c41fb987378947be6ee12f808 f6bb034032bc77c931558568182e10066932b5326940dfe562048d83dba4c126 +97000 eae3af1d8bfe6d1c40c118e0c78bae2bfb1800401d532b63a3c875fd3fc8a24f ea34ca2227308ab8818f085e7fefb2f140dfe9479558e6dae8c0dc049e09765d +97500 60dc3e9f055602d3673ddb11ab25d7c094057a0c71db2a327a491b3be93eb9a4 186a25fa3609da954813d575a4128ff890d571f0c003f0e6d62a6322279125a1 +98000 5124c9f250499786c2948dcf5e2be2c7c61b446be3972adbc0d65edc8ff87b0b 7f5892932cd29e78d17fda5c3d15bba0a600f1e9ec2281dbaf0fb1dc67d5e8dc +98500 6f507567f549b3b1e387f1bf80ec0fef03a9a1af73147dbbbae2134d6d1633c1 60d73750b6ce4d508dc18cc64adc9e68b6bb7ea4828f09e8fb7419b56eb652dc +99000 64999aa405a2a2e5de34a4c3547ee525b503566532d82c35839a9e74a1389ffc 20052d2448a28485dee72fe536fa8b6ffda1e8465af82491e87980c8f806c8ac +99500 3cd60a841f4913bfaf6183237ebd325b7baee3a5992f092118fa49817240a385 08f5df63fb4186638211b70120a165898891fdb195aa8a5b4e41bb0f9f0f16cc +100000 f0394d3c54a8687b6090333aa03f93f9369846e7c142653524adaa9192b37b7b a739c2cc2c59123a161c9b9864a4c834dfd5021066198f51fde3ba9348d195d5 +100500 c5ad61c2da15ed8083a3d3dfd80d226b9977dc831c11e83b75b3ed68d2af0e25 e3121bb1573ace63889a3fb9305fb95857bda6e889e9a88aa2ad4c76cc407386 +101000 27c2691b9e3e9cc2008da070c0af12d0997c895fea5c405347496c6a37570f95 d465e2fcce8e1b4bf7355b4d87445d2557336354b86db40bc924752796aca840 +101500 80c3bf7835d6a11fcaa22931aae51bbc602af4412ee8ec0e8b96c3a9c7128f16 1d7c3e519be3686c4b9f09708e225c8259c5ba6647fd78653b387539f58bdbdf +102000 52bf59cb7e10047df152f35bfb683611f084d444aac4301bd0284d3e458b6022 7c8226cd17af258fd69631ce4cfdf204dab70dcf93bfed953d6cf488d576f56e +102500 5dc7c42f2501c79ca5d610b8b69d408033bf0f7d16d036bea01e90e6c1ac9342 403edaea352b4da33ec3269ccbae23c226a39961f457e5d3f84b806e517074fe +103000 719c3b85ab62248c015de680a8fba158d0a336492668bc0b202ff9fae1b1b5e4 d90dede7a044e1b11ae429bd313e841c12e8d0303aa6626db4c953d198b0f531 +103500 fa0bc10bbd112c733022783fa67cc4ba045bf93115654e1f25e680595024ef04 e55422d2e8d2b8b6429ed85747a8ee57c549651eda0fcb2871ae5762862a86c9 +104000 e63af7f5547bf8bc2f36058a43569a30f7edd8286ce44ea561e63a3bc11ea7d4 939e2e2c30e66ae3b9077e947acda9b7dc0b9f326c55cef4e4d2e5025a0df9a0 +104500 d73f74f43bc0653041db94c60b0e68c194199041c830e4f90345c1dee78b9c23 d1b14fe1a0631af85d0da1b0376a93ed925248fb2c5bc47d4284832d40fa318b +105000 460580489a262514f5414c1fdc95ef0d8290a23bdca4af93fd226d72ca79bb11 11fedbb936fb27e227c2602d32f688f1d024a5fbc8d31823d273818ed142ae62 +105500 a461d00c1217d40b1774f631ce44e78c974be18cdefc160ec33e865b1c7805d5 24f67d2f822429d89701691d7834c58593be43e7249530a9c1dae9aa12cde4d2 +106000 70b95f002b31d55c7b523451d746df1ea1ab182f4e3d26a6a8146daa6b175f70 f59d284adbc90a549c211187c55814e85b8343c7797ade9c5f761e5b4c5855c8 +106500 5bcd8c94849d744d137dac85d734d893dea9963746e1d5adefc0852cc99c3c66 67b4fb04c0780ac951d84e6779fafa76dae5d46e84ef41cf690cd33392c6f926 +107000 aa50a7001438cb8eab4442c4635453881df972b8707150275fac0867d7254341 9e2451f91358f222b3934b24013be83c1fcc78415c12b4fab2cc5dd8a3d3c36a +107500 f83ed835545b9320da20a1b2961c285adf11dcde168619a3e7e746f93cb93cb3 e27c84ca437850f8aeb0cf3bd1a727caf91808663679f2dfca99f4c676a4cef6 +108000 d9cabd49da35be240bfaf8dc107e13fa3ead3029573f7711c51c874f09e2af83 85f41c365756daf80d25256a132c25ced262eefd77547a42a51603fb98f0fac2 +108500 e6f6bf4d75295e5dfff4f979fb12292736dfa7d4d8a7dc683f6238a4e00caf0d f294a30724f9d9ffcc244995acf528e501f6ffd0840f5ba8ecddbba1d1bbe250 +109000 154e12021e0e5198def8989a1d68326833d020409a76f980e4e41cb1662e0d47 2e486aa0080740653141a5219ef25dc79777d3976bd9fad1a7c7bddc2fae858c +109500 02a8b8fcee9f8a420ad024de30fb8f4b2db683b4f93e37d5a7936728ad1bc1a9 f9c26724d2d6e8398a66e8ac080c8aadfc5155ce4be3521b3ae5aff95fca7541 +110000 587654021f4afef0e863df85d5c48c1da57af439c890cd7c82f57112b523e567 41299bbc731c54c00252ac8506c5671a019c2f24117534a52a6dc243de19e60c +110500 1b144103a2729d255d2c79223dc4804e9ec38bbd07105eda2eda5d350933c8e0 47988c8a2b50a19d5c4cfd5c657b3bdb19848828866e762f5fe40f8ed3b0fc5d +111000 d2dfeee7017ce245109d6fd20d439a58fba318eb0853369ad740a4c7d8cb3679 89d0671f5c51cc5e682fde76759bf130bde74a8c3d0334013165cdc74df67a35 +111500 7bad9ae95d958cfdfe5f6f1ab0692c74023e9676aebf77696f0dd92f272484ee 8711ac67cfc650bcc9a9a3b170896959954dce728667035278b865e843c5f5d2 +112000 aaa6a01ee0e8be04b54950e9fc47308bea161476fd9e5f7d931a9ac7a177c6b1 cfbf8e1b380186cdd7dff5b1775e68857e0144a6cff5c699cda0df8a9b755879 +112500 856c253a4ff511cde2cd1138cc415c15fc4601a9cf20f97a40a3ed5a8e4bd28a 2418313d86bc12dd0b7ae5246d70c5c95ddc342c5f522d33a4faa38677b1cf74 +113000 d39c76ff2213efb6aaf21e1831e8dfd86e82851311214222cacd1065e5f442f5 7027dd65582113bd83834da3b8feb6f5e724156d8653689be02acecf664ed8b5 +113500 072de8c792fb777438dae0fe6605d05f84c05c478cf045595236439ed614c869 66e29b9bcaa1beaa8d901e8227510190b64b665c427147e79fbb03f4c7560df3 +114000 2727f22d9d9b83d232fb91cf7fef9923d998166a632e104da344aa5b2c33f963 afb687e99a9ef1e794a049e78417ff2b0a15bf3c0f0a01b5c04e381dfd3eced0 +114500 fda2f80a90dd787eb236d38096e1f73b7ee1b54d067c53ee62d4201004df7f96 434c6e214b4120e3df23c820dfbf7ecf3facaf2f30b55fcce4dc094d0d052ff3 +115000 f8642fb7ecc176c23e6919e2a5f9a3f69d60c1bf10329e853b94141d7eecec79 02a4953482c2887d0dc4d2980de20cf75b73ec8d81d5b3d6ca6df741a3f3fc7c +115500 96be8c04c6533cf914499dfa780622f64e3362ee67b888c3d187c7cd167c124f d160418c73e78f1f75972ff6fce579d50f936678a03b8e3e2e8eec885c8e7f97 +116000 6efc2af9311adf3ae7cefcd558b25335048a0344d0fcca48f4a8302e0c2f4e4c 5bd32d67887dbdc6482620b347a1ad8eb9c8d124ecf4e6966269a1ee537f7562 +116500 d2fca2b2bf347959eda9a08e76c269d1e00d475d90ed02ec7d5d537e542c648b 9476a9957e44fe4de208e4c707f0b6d1a2c0c0b3a32dabd912707ea86823f62f +117000 5ac65e76af1b75c9aee7eb49b22ff2fee88a1bda49287fc5dc7762b2c5b8768f d357d64072327f9db3d73ea738abdf89f5bd20ed4b56733bc9df37d200b6db39 +117500 3d7e8e1235d8f137cd5661125cc0627dcb3c18945254e9676e21f2a445ab8b9f 0f9c97626569303b76ebbd2cb960341f2297f69ae140cc84a40a83695993605f +118000 be2144efc3b4ccf0d4d3d7b66c5f6ba253d1fff069b89a69f8eb2cc4ff44fe34 9bffc6fa6a4cf35486018721652d5358be0bbeeef8b98398c9373c352cacd4be +118500 58ca03280dfb1e73dfe8ae35f67b60087586b36823e7fcdfb0509bed23c0f9d3 58ce09f4189a7f9263ee9292d7cafb880bb3676175c7553a04e25bc261ca21f8 +119000 907a0fea05eed4572602c76515bfa03d5cc79da72496d41d84afb4ce4098be07 32779d699cd1abba0d653a332f07727b9a2094a4149fb66cb11f16df7c68d34f +119500 ee7317e1516104041197ebb0bba085bb14932972d5bf4ce305a3bacb9ce17dbf 6c779c5b384b94c7649ed90936688e83762407a89643aa89f730c6c399abcade +120000 7105703f4cfd592d639e0b5bf6f2d25380a1787346f24331401eeed04f146130 2a44b6fc864aac66301c1b20def1cafb0704830fb07e2684651513a6f8a89843 +120500 1dc347ca251ff3eda0a459a85d9df212ed27b9e7521bcb8445d5a377e23da48a 3da9eaf002870bd0bc46688093150e254cbc0171abafafacb2c03f6381a532b1 +121000 7b8f755c93b69f842a1f9ab68f67e597f0fce31b94e0cfed7f3847662dd1a401 f175a7b245645b0f769638a6ceeec460e87782994586ff8be749cee06da716a4 +121500 2e5aa2e8bd0b018cc58a7d1cfc18d19903cd57882ed5ae8f2d6eedf72efab59c daa4a0635304dda991bc047ed1e075a9bfce603bd3bc1dcf93da809f82b0c5de +122000 9c662917653809b60484673f8f223f3ccc52f9288b8598b6414bef79181de9aa c8c62e402da8ff36b5833ba9327dd92a501367be811157fca1f0b80ee49a4ef2 +122500 74a15173b1a78cb7e95f220178205db14c5bb67dcc64f8a1e4feb2cb5b2a6e28 f480c746a1436fb843e7f5935f2a8eb79007857b4c110a9348d4391302537110 +123000 38b438bda7e108f97112d984725fab27d277a08b1a72affda99b79654c21e0f7 b1bfac6a48327a4005f79408df343adb7c9cb22d2b70ebf2ad79121cbbfddfde +123500 ec7a4edbed36c3d7a0eb4098e6c6bca397bf08cbc0e0b190a4cfd506df0ac656 7700c27d9c2e7a0360bfa2426fb3688bc8b3ca935de69d15b6dce9ca6b31a4fd +124000 c3d8229a44faa337d05e0b5311a5dda37b91babcf95e5a8ccefbe872af0bd2fa 793481678c778e8653890f1145e178e42effa2ea7f8e50522187ab298ec6b512 +124500 1510db798814caabdc90f2b18282456d1b1be40948ba43fdc68e2da017c2dedd ecd23539bb6699ca362d898cfd6181c49ac97b0d9031538fdc0303cc8441fe9d +125000 82460aef9d1133d30707bd416b3afc2929a388ec8d1431e92699aa625a4cc3e9 8ac7f7dd700d26de62506f88cf117282a7840236bd66aeb148cde751ee3f7dbe +125500 cf883230f6afd44caf802508a9bce39adeae83cfdaea0ca10c5eeb7e6e0f7abd 269f3d0cfa2df454579a4b7bc758970ffb853aaa7600808071ff130b994adfc8 +126000 e36d486bbf7eed518a2a1b1668b516daf85bcfb0359337042bb8c278a5826863 8ebd6563987e0969505718a915e7945f0d26213475be39a4b36ce9928d313935 +126500 41b57270dc8a7fdaef0345502f3ad6ecf8012b680714cf7723aea4c7cb03a864 e422bcde2c4b3e00c42dde121d189205de9acc58be35d92bc590a67fc11b336e +127000 34d5852f2c7ab91f6b7ba43851efe2838a874f1d47e6c41dcf413f263617c4e0 296613313aa18e97281d6dc14419528e0914d61bf8eb26f4ee08105aa358c0d2 +127500 07f471a70c1d559e6fd70d2adf4720987ac72e71d29c4bafd5f78e428aa6dc00 c32efb7a2072cf74b7124a008939f8093f7028259a69d8c9d175e0bb4910f03a +128000 063c53a0e11590d0f370d0a3c2fdb3722c1c91e0203558436fb43b16b06f572b 2cf68c71009f221feec16d73d8ec1b729b0624f854851153bf78146935b69148 +128500 32a9b0507313417fdb97dce6e15852a8a55c80690028759b577723b629ec0234 b2e0756fd80ff838386d84e8d82e2704a52c07e56c10a88fdea969948893b808 +129000 b3dc6186f79ecf8e7d4b2937ddc1ff5372684cf818665a730771a932461fa8b6 6da8856b4177dafa784a25b68c4df9c250a664cc6831c9804403cb021239d03f +129500 1f5e8b0c9c4229ece2580e76e5164e1beb86e165d959cff8d7a0a9f74660505a fc76b8f58cf2fcbf3f2821d871788cf4cfac6e52b0fb0476e96769311865d90c +130000 3052fa9be8c4f5f24f40c5cdf0a8eef8087e08f1d168dde5d1e502960183f137 8d108e91ab41a1057413efa8794611be06459dc53a8d2099457073ba932acb4c +130500 73ed2cd1c1fa06e01c3c6fa4848d31b24564e667e3871ee032a1ae04812c4383 df3d5e20f9ebc94bc84af002cdc9211efbbcabee00cd755653a0cadfe4cf77c3 +131000 f78703a391119a15b5ce6a2a2bebf2988fb164656c968f72359af65852080c1c 8fca74427f6789147cd1e233e020050d9babd0bd6f078ed53f704c6922b5ce3a +131500 e8873601a44048b85d11b25b05c225fa970d4ae14a3a30c0072bcf6459f2d828 140c8f3bdc90be5ca51e7c3179c7b8839afed4458278759a35c4a078ae8cf4e7 +132000 98752c54d7a5238387599ac3a0fe44b3ec462f88eb0c3c6414160ce79c128a14 f82ed570eb9d3c9078ccf41e675c13fcf6e73e6e5bfbc4b6b2c1e4649dbf8a0d +132500 7377043aca01beda7f7d70c6c2a9f59ecec4769cb330c231abf8439f231a6355 3a35dacdff298a95ee0fc61728b4711d99c4d1bd4806dc738c684d2632fd48fd +133000 0d70e8ae76041c02ab65bdd3b0f6a456288532d5447dab7f7e30123b28e16543 d6b3a5ad40a90d26a4b218b8b90852d49f508cde1bb2a23031f64e765d519dbe +133500 37aa235fefa6f62af6e393639361a4109369d0393d66597a3c63736fa5113a02 9c59c20c57628b250fa5757d8d2d55feec38752453f21232141f82c1be4dea63 +134000 2605e14bfe5b9a39a4d01da070c05d9bce4b6453b599a1f6f95d8a6344138edf 1b02cbaad0ab46a737dd848c3db0f111c3fdc248d472636765ede6a6c2389b74 +134500 12cb93a2b1e5ce4d9c6d2266bc6984839271c92f8ac34205dc621da8033af81b 6346ab5cc2bca05485ed517f9e494e1e35e33ab9ce011923c6713dd0bc28fe1c +135000 5d475b01fc5b6a22c775b89914c3b148431956df26fff2631111e9ee7d1ea55e 182dc60ad5a4689e655a00728594807ca659ea3e870a4c71152b220279fda836 +135500 d4b244f6df6c05a1c918ec0c80b4a230c2e40de23847a3c6a48b8706b55191a0 c0ea29271b681e460441fc515fe8af217459716e9116ebfaf9899531e55b9181 +136000 123eb5d144a44b6940cc6194be4ba041c5fd8a5ffb342b5d62d8779605d1fff9 9836476b408a143b336dab7cfcee4c07ddcbf6939fd40267f8315a7d4d576fd4 +136500 c169555d2b59eaf45046d5c3832203dd5109c6e3018504de66fd99242b41d5ad 0884992c307a5b98b026f9885b6c81082aaa18a12ddd0b302fc5e358e313c810 +137000 123d022b5482871c55b3638324b5713e225c6e36ab4f83120d883ddf3c0fbbf0 024aa453462e49f50c55c36736542e4129f56cc81956b74f7c77e90b0e6796cb +137500 3dbdb568d735e6c47c5fb8f459bc448693566ee30140da275f786fffb244901b c0e599ed4901402951a3663d225047e94861817b0a412b01e4c770c9719cfe53 +138000 9dcdb63588c3f9d43ce67bb6da88a144e98b3340eed60478d67941bc1f3e48b0 0c04f84ec720ccb4908774fb474149dc069c8420a53e89e9007dea32d90e380e +138500 918f3566304f92ee937b50939e5224bcefce1b61503b8cf5c75cddd293115037 345d16e0d5aa525aeef890c1cd118edb3d1e6bf784e9445aad720dadc3408667 +139000 5a5cd26abe8d7b66b82dc847fe582d00c1aedf51bb747cda126f35d177df3780 b11a4c12939538a057f35a15f561da8ad09ce4ec33a1b6b965e6c35d7f6da935 +139500 883ccc866e98513c10d46ce62ffc4b2f52914f9d3c77835ea88b2d772647098b ae7d18414cffeb5073a788005a70e1e80b3c04abc1726101177509d0872ef2a7 +140000 0d53fc7a730aab5b7751832647af30f6ae6de5f66a26aec228c9642132460c09 d157b0bf7301f4c28143ea0266a95c92a05a3499dac2ab929d57ab75b2be499f +140500 b67b7cad6c800acd1fa41cad0e075a544368d679d03d620f5cee45cd966b61ee eec57bbe03beb832a99ab0bc6b77817e6bc459e365367b09fbcb905f3edfa85e +141000 68756fe78e60cc7191d62c387eeccd0030ea6a67d4b1fb6667c068c5dbe20d53 113007738fdc702b03dc17993f916d40e24e88d12a95726aef18156355990d7a +141500 12aa3f916704a9120c99c22977c22b16bfb670c9e9d2807e0cadc444c9e8d02b e7dc3dcc436c770ba20dcbc3f4f762b7ab343fdb180e867b3f3db34f8b279871 +142000 629495f161c45d033130f52a4df5e06cf5f705842039b89c3d499aa658ce5a6d 865d4be2b339178a44d8d9ac4a53abc6b4f710b723583053f23b73c0dabd77b0 +142500 941a5dbf034317971407a4743fe1c5cf11ae87be184fe4e7749e2d32632deed5 d8d4c2edb8362a39b9ef8b2693e1b4ed597a9b315e60e424e26797ac5266f353 +143000 b8ac8abfe7b81372299d2a8be307e13786a5aeb4c3a671c8ae45d32f4bba9281 8d7e1ac28129b56b0ee93dc931e54293f181d3aa253e844d1807d47b31193f56 +143500 610bc499b94eb7e00d1aae8388937087aa421bfa9cfa70baf798b8f97e1b23d6 e990d00ff71509c232b43bde64c749bb1fe597e52e40116893e02937c7943b1d +144000 58ab7580eef5d40c0ec3d7ac52425ad5afcbccac834192fe71ec5a1d15cd4ed7 d9194f449db3d58a1784cdda50ca32d4212d43a12e85c6ad9f8b77687ca04a6f +144500 9a49c8d5f6b072b617ff80d4b70116bf3db461f9fdf283009cd9e9b2c2df0925 a2921c9b5ac78ceb49666c487d217ec303b1f5347d6902baca9b8e3779774c5f +145000 29f1fe68b8289b62a4eaabaec5abe811c61b6f2220dd15f04bf3d2ed3f98ba02 8d3672fa9d6c4767e8d3819f09a9bfdeb98a6f35b83f01faf1f7b85c6871118a +145500 677d2c090d3beb31d1e611e42801e75fd3e2004e10ba8ef1cbbad5e2b17eb02d 93a4cedb34458ef8b90ee740a6b19364f5ba6c0096a16142929987d2386358f6 +146000 b85efd80a2171505e8e2a83feb7f7d12e3793fa4ed2999d7b6148a1673e574e7 c76311a2cba2713e1e7706f71e22abd117ec5c45d7463bd7cf9baa7734468c79 +146500 f1dec5d905557b23d644829ceb4f9de311a05ea717740e9103d155157e4d1354 3764ee8b0d6ae8dc762a3b43d012eea45e1e5c1008e90403ba84549502c3b62d +147000 1e59d25367fe453ea1e39d7f0c870f14bc37e99ae2899a6f0e67dc8ca110804e 238df05f6e2a851137fb165117f2feb458487307ae95d0683bc85fa748cea31c +147500 4547d35c4821dd671dd3d4d99fce894d455c083985eeaff26531187f54349bd4 13f4a0105947e707604293a632917b8b93caeb00c02a28668a9559de36e5822e +148000 f50a828d973baaefd78a069efcbc42389c7a9d0e17cf4476d6bff763d3b219a7 7d057f6f26c87ead3d5a3bd63b23ad81ba5a9a84258b46921f27f07919be82cc +148500 219d8ce760106e41d5b6cb72360801884950e795cb80c5cb75a4cbee7c9884cc 66abe4202adfd1c91b092dc15dcd702b0e383c2a9a87dad84efc20f2391d6024 +149000 e13e98351891f9940b40210188016098d67d555ee60922bd710d234393583886 2751b92f77148b0b55b15c1e1939f566749d8bd28b70f9fa9445d5c66b116a4c +149500 bd0986d58541fe55bc5615de06174615007bb0a4e9b7d52a6e1de27660ff5e1c 075df9893a744f62bc60e850104f6ad60764b8098a18f89eba4418789bbb7c6b +150000 b0a52b599b482e9e081373c2d0c12adae59742c92c3feb16f2b56a536389c566 2b66e1c89afb06043357c8a702e15f37c6cebfe2c3ac2e2994b387b111852c2c +150500 348a90e69dee425ca95455f04bb8e0a9a5e8e4c9efc06e8358e90069f33cbd87 6296399a3fc3e48c09ff1bfda646b6f19277f2c2562c8cac5c850d691b2c2110 +151000 c2fc138fb4f13487c8f0ba65b1843cbcbfef598d66205495ce12eded834ed5d6 6b3d49e0f596367b951b6939dae4629d81758a560d8bbee402b4258fe3896e18 +151500 f22af8f0a616af674ee4e07e5821c6ae1ba54d431200daaf39362d203283af2e 8c445485555c915933539ad6c8ec776b65480a6b6100cc2f2355af041d37ae9c +152000 ffb05330a4e59f90c081b821c1cc26def98eaade28aa6fa36688c9c9ec2a0ea4 7c94c4bbaec72efc42d8c00f50f777d09fd0bf0c71475a31183bc391038e4fa1 +152500 b7c7e8aa6953c0ad15cc9b003dea50f9f8bc08d8c8ffcbb287918a9e570affbd 16591b434c6436280873b7b1d81157359b67969455cab02fcbf7ed9dec87e64b +153000 fef11d21d57c34ca41af06608c09e6d0a1623e6970123e654a81959089913630 0683353ede70fecca1f5f072916105b0fe941d24b2744dced890e2d4cc114356 +153500 0decdb908b6c897fd269156ef088ea6c5c76ad98cae97cdab6b9958fa458c4f1 458fa9b12274e81b31d551966d7992d6304bd3486a76d9e484af250b4d725e83 +154000 8e921a4716e55e2c03536ace904f1b76f2e1c1f44eb1efc293e819619fc64a6f 9f3faf97a3d07baee0cab898c7e2c16e1e75cb08b8593b30897f9290e7564464 +154500 b2ab747c57f1e073712e5c7c63d14e427e4c5aecb70c749d686ae6740eaccdd0 5b6d69da753ccb8fc0e7cc323bc544e272b607f0fcd7ecd3afc27fa454bb13c1 +155000 4a27f6a366c137b5b0d9a7864ad6ae3aa2aa4f92fd96afad203f9750743d1b25 5618bbb8a52a4a6523d7b79f9da38af51c38ec0b250918f9fe13df1f5ca4cded +155500 44de83681d8bcc4a775141039552a3bc94ae7150ba6cccb0eb51d12f67cfd3db 526775b79eb1e6f88bf7b11c21c21cd3de6b70c591b146f75dd6f7dfb25d8258 +156000 fd67b25d5f2f9e33e0717ef2eb6f7a8b61c0e369d1bb27d363c2fffec6934c6f 069fcfe28f96738efe2241bd5c46e5f9dc2568399dedf97a3b222e7db1f8d479 +156500 e4ccbec221374ecbbed5f7515a62ab145566ca98f91fca6e9e9e5faa5a302e1f 0138154cbb27aeafb8785898175459b8fad1a50e5526745da4f3e3fce73906ea +157000 7931d577b76d43c21d638b392ce9e89deb30440d5965bdeeff5ddca528b23f8e 2b3023999ab4fa8af216fc6fcd1b74a6d9b91805c25286b3a9b5c21d111bd929 +157500 5a785cae6668099aa3bba0aced796a2efd3f32357d5cda07964c38bbb8547f11 92fa0afcea0018aa72753e44978b29517c44b86583a7e7293bf41d0fc52adc33 +158000 215585110369813d5c23324bba75c2fba77a3cbbb8074822747d1d5f7a54459e c2bcfd99aadb46819152e678172eca6ea41d068cb9c3644effd3da1d90fdb6d9 +158500 33ea7e9a89e3e620f7622a67f945e6a84caab88f8df84416920e34611183a012 fe4a8ca54ee30957c1c4ff378addca94aa3c2faa44d6dff2c90aa740c94f322f +159000 5dac31f23e99492315f0ec0395c5913a197ecc6bfa5a1f1c38fcbbce10f5b2b1 3f33701f476adc2de539a516353507c2319ee322415cbe2d2a81d25579320df7 +159500 e49f3c6d819ba98b65a07f3e2b4cbd4ed74f8f352ddcc5b29cf715870ffc96b6 8d4baf5eb4e709a1749ffab123eae1c16c84c08c2b10c1bf35b904214b55622d +160000 4ffcbf8641b4b794fe4e304f3e33f8b252b45c99d647e26c174830811d5214f4 d9feccb9318af01dda19ac1b834e8aba8f1a31b7e3c0c874c2340e5f963099f5 +160500 940e235e254ce7e8c7884b846860faa5a7f8fe08fd7d05bedf60a178b294bbc2 4c96d0c1b2293677ff55acaca052f84941b801d35426f5f6fdd0985a772e140b +161000 49a2fe7b8f78e83709f195d11f092cd5bf6756dfbea8b61f6ad4acca38e0f433 c61985a4419ef741b3255effd2490b3959d9a37b9fa458a01e314acf2cd110c5 +161500 adf7a049b0055137358e82ff4cd0b223d4f1e65a99cd9427eb8892e9b3956546 f2ea625ab6cfbd295435c3e70fc60ca04f444e88a9951351519c27066044094c +162000 78eb9152a6249d26cfb9acccd3be7692835f61a70f9c4dbca7dbce97e8eaef29 92bb404d3deec9249fdb8e8b0c11e1e6ffccdce4f6b76e49185e0e0aa6480b30 +162500 f0016f65cfe29b53a443c3117392b631edda432fb508e67f537158d939bd24f9 7da855957173b1caef3a5be9882b88d6be2feb3860bc528822ac203df9480ef7 +163000 16310f334b323b1c422388a77287e590286b436eb1d54c99a7dce533febad9d1 50b8d182a094d5709040d1f8be2ed7aff41c50d3dfb49b6474ed04c7563a2afc +163500 54529c58d723c981aa51c3bf7202246d09e30d6bcc22b28929b2bc80b970a46f 81722de9f999145ae10c5a72cb03be616a2c631f6239f01243836ceaadb219d8 +164000 135d6c860f9cd670d82153e8e37e5202b6b1632cc35280c0210587f4cb4d8f49 c5bf964eb82fb51d1f5a4507bc2c9d658ae2f868cd517674ee1d81266971aef7 +164500 08495f35f925470863e7dfb79ec17bd61029899bad972ca0e5bdb0512a0ad908 8901ac98fdcdd833938280eb495214ec81269d8ee99e0c10886b9184b0aa9d26 +165000 5178c8e3106c700a225efb2d7453be3a94616e980fae5a532c0a9fd103aeaaab a71b7ee462788bdcf6b6945da2a2b426770bb4e31c7063db4b9c80f82ca88af7 +165500 3d1e48c8657aa62207f24888630361eb85ea3f8225e65deab240e5a242fcc463 b16e47a7ad5a208fe467efb2f7a2aba78cccc41dbcdfde571ded9b033b2c660b +166000 2a5df181ba84755d4893b8af164abc28ba9d67f0fbf70a9fe44f78202c19e78d 6382ac5c3033076f67f758d9d4a66006b6c67983e46eb4488b8482e5d020cc7f +166500 f33bbe1188c265e4f52649d90a9c853742c89348276119166d9dfb5b0a1c1d1c bc724795f0704caf13beea8188647ee2d0d8c05593cff788541d1f694f02e7df +167000 278a49c7367096b101da81aeed3a9b3763b9087446e3f3eb7d279bbb180ccd1a c90bba777861cff1db1ef3cd40e240a74bdbeaa23e2d4716dff0700608431547 +167500 de103832de75b420ee01b8eb238c7e5ab2fc2895848f1fb934bc0979af54ee3d 5263039373a72cea915df57bc9369aa15e7dd2ca6c3b6df27938d5240ef4cd8f +168000 c347ff8a9e0c4297f74db5943de2edcda14652f1e707b0d789878424b0f50555 fd732e674ea0cc59454e599365d9526a135a67ee82a1e54460f90b934957f9cf +168500 7c4bd55cdd16268ac7da91c44a5c204519aad6dc79a173d65227995d8f480ca4 66e715d9afc7e37855c6f1436bb3344b440a7db44fe5163f2674b542a3a8affe +169000 8d8295fb630e487b21d46f82e5e0bb95d667635b1c0cde080e9965ea021f0d3d 32309e6dc4e7812fe36a3e9f14079eb2f9a04dd910965a631f76fb23f29f2c5c +169500 3ff4f89571d4b7f8e2b25a761e2b03dbe82d9d2acc565c8f06f8e61b7b84a48a d71ea82c1b352917a9a60feeb3ea40f35447c58909a0832d17451458c4a0a079 +170000 e4612c319a4c8cda00c8853428e0c57c4c688d9c7ed0ebe3adb4ea410066230a 2428df1325dedaad3c6c2e18ba57184348603cb4e5d4fad961d9f66a33e8d4c5 +170500 38f6d4c55c456c85b59d965db3386f3bebc9f3ffc101703667c9ada2c6f110fd 7fec7fc3f24c1f7e635e06427d949e9244c3383ae8a1cbe5ff92c20318d1b303 +171000 fa2d2abf42e47345b9b06ab4f0dbcb6d818815b383959b5b0f69a207e755ae18 6943c6e99feadbcc5f22d2000b715e609874953a2f6b06ab18c6f6fe0d9ead08 +171500 b269e08b7b8fcfaacc4cb1cdc39bea13237265f03eeea6df488039715959d60d f978606a7a355e240373d149a0bf3b2a80d735371506f6d83a7eaf2a05f6a300 +172000 c1ff70cb404b9a3f97513e00fe8d713ffc5a1ea49b29be1480dfd3b0e4410d1b f2d46aa05b23a697e347a4260c9d14b51e08af5108bf22123b820a54c73c3fe0 +172500 6af8406f49ba05570e46ebbea295e07ff856de60f7f7a85f29ccdf0483deb414 2bfa8eaf8be3fde78ec35b8bc4ec1274cd089732617255d9b1450f676d359631 +173000 4187e88e79ee333c661955addb9fd1ff4853bccda1c1f73585fbc4b866d46ffc 2c561a6112ac2531467818ef79bda7fd294cc2d1aca8d700ffaa8ca3c7e2cabb +173500 979cd0755a559b4298c4e1af1ca1ed389c152f6800718aee777431e3b84b2975 817578adb3539145804a17166425c07a798bedb2637e8e6567e77406da8cbab2 +174000 84e476b10a846c1f76cd25ee8721aa8098a3a794f56a5a6e2fa8ea08dd7de954 7e3015ed948fc061ef3955ff4d18f5c9f2ccd86794be959d5e51e604c4ba315f +174500 daef6a8cec458daee2a97f145d74867790d12697f424e390fa5ea96c0edc5889 8edfed33a6db9e18b19b49a451c2278d0df8c17b86a5be2703336ad41ddecdb7 +175000 3f6c5c68d024c26235bb054fe0e892b4172a50c900ff7e36a061b17bb4adb44c f36837bd4b44bb99bc4a99acdc71611e6bbfef26095aad173d5b6c2847cf5cfe +175500 2a25fd32974c49ec621f4256e89f3c1569734a675bfde35fe0ec4572459a99ce 0de2de8ab8f49f684a4ed93762fad169165b9f29306fddcce37a10b3e0f324b6 +176000 ca43896b57242e50c5e1b0904b12b7008d63f30810c9d1728929a45a051b1c28 0c0a240dbb5b8d9c5303c2e401b1a9be3eb940a2ccdbd40482b18ebd9d16fdc2 +176500 01bd824255da4562af0cfe656645a2e644bedd67f5c4c2266e9d706d724f77c3 c1c8c3eefcd2a0dd893ec58a59d9b4b9fca2456b4ebfa118caf122316ec1758d +177000 1931b94a1d1731ee2fb2b959098279a73f2938815dabb1689c98ca64ca265eb3 9ecec3dcc0f626eff5310a338ac6ec36e52cde0e31dd2668510985504dec6b76 +177500 d5e641f50d45b9d3c2f81c255fab7812b79d778bfbf3acc3a905255009efabb7 780102c55e6488511505df92213d370484505fcc63e36844adb4e55e28b11891 +178000 41dc7c791cea13b0c22c556ecf0b9c43bcabfe110cda05fdffc9a213d07878bd f502dfc4f8e1e754fb19789de0195d3f76515af97df6f6a4dff9af35e9e64cc2 +178500 86fd0e05f7c975edd10fa04040fcfc06aa5988f25a24dc475468023fb5ba60d5 9a9d5b0da31bb391cfadb78b79de03e6d2f9356b6e8f2a6b6f3561718607666b +179000 9f87f2c50cac2bf6596533b242692ac06dd59688c85711dd3075556a42b5a51f 62c5b9a2f74642239aa89a9468c1959af35f4218d16ae1c0e22879ce5aea14fb +179500 2802d26be7d5a8dfa0e84d507569ab54eb2815addf1363e844143d4d1949aae3 04a66a42a874e8a94de4b35e0fe5b2415e2806089e62b0ce867206a3bbeec923 +180000 fc0f88ce08e8dfefa8774c7b9e101f82f4c7928aac246233b5d97b178ea1e8c8 d2cb3429b3bb7ca9c4bdbffed735f779979900ec989687ff9563819b3f340a40 +180500 55068efb221561b62da4c6cd3933963165b99679274f0a18b57e12a0f59c02bc f4a3a53f8756a0f1ab9f0dde456df65f68b487f452e35119a395990224589672 +181000 87c0badaa9ad1ee338a1e0bee51f0e5d95eeb69b608604b687e1b75cfe28b1fc 55c6a1f3d35e1ba2c56261370b991094f37b2e0c94c585c83c91ceb0e66b4205 +181500 0223e4ac4db7cb7aca8c5dbcccf4dd6d67c261e158579465c830d00f0b2065a4 1523b5a7b17c5f0a487b27b05911b0b023b7b70c1e93a676178193e10dee764d +182000 3bd580fcde83b72980a98de1bd2bc33f5d2e5260a45b2025f8993ff148006fc8 b5867c690edbe8ebe6938bc93774d516dc0de5a71a28b48539e4029162825354 +182500 d5ed6e8efecd96800b30644154dbaef8704783309362f9cdba26c757fcad2d70 f64f64d8652b12527197f54411334ba0ba66f2515e4c99281a075d1df597c8b3 +183000 e926c6476da9f0d3fd84aec136e029881901667ff07d80ecd1e6e6e4a1e78979 e74160bd9ff069b6511c0602a1c72fc61dddb22899f805cd9c2c0be184848033 +183500 5bde7c982564b58b9d46d8208922b3ae8cd1d50f2f68c7a4a8d71e636b66181f 00d016a3247ef22887c0909dc0a7370c62bb2601b09425bfbcd93f782e1466a1 +184000 f1cecf410b8d9f5cecd6bf19ddb1c7157cdc5c42e110a42596620d9b54216dbb ca3d938eed902785feee39efcc9a60f26ba5a53ef969f174ffc7f44dbe544bfc +184500 89d6e5e381ea4c429428a773caa457945d6e5986e77830699d64ca84b6b7cfde 30e5fef97c937baabf267183b5f8bc9de88eb72b00c204abf6db1962ad1e1f0b +185000 d55dfc02a7dc5e64f615b9821e7738cc3f4b1cea50e291834b3d950457c0befa 3976b8fa6d5486bb07e4f6a5a63f200089781bc8c347f858bcd2d4a8eb4ebc7b +185500 b74792db4244be3b16341cd6bb1af5c7a9b71df58b812691901a59e2e6889f8b 4523193666a8f6eab2f73949efa94fd98d9ac8821ffec1c8069abfb1dca83b0f +186000 673e42a71dc7428d516a2bd892f5bba9740deb4a8e7100afbb0db70de4432d4b 54bb7fcf882d25f7e4cc19b41bd13e44d70c04d31a6b4e2ddc388b6ebc0b1818 +186500 ac9d112147478ad6096daea4f3b9b76912367605cfb6ac68a5951478787d84a5 7e112c5297f3521039d52a994be89b1dc3b6387b4be5398d7673542df3db0945 +187000 b06838fe81d8bc39304fd98dc9f24ebdc0e6a05a363713e028382d77334c2afe 53c32d4af24bd19d1dda8c23186b6cbad5cfbaa6ee169666de3b028473f3d388 +187500 c3f730a1d3977c557401a7ba920f22b768e9e89924a07598af8f6dc0930a4585 a83e748dec264dcc6494d6ef7c63025e554aea410915c25db93249088c287442 +188000 4a74ac7ebe38798b80ff54f21a428c40915895bc44a528bd8e4ac3b9f378def9 847a11b8eb71e0b5dd698b277fbcbbfae9c54e55477320396a602a26b1f73d8d +188500 59891ac998632472cdca935fa4a3819fc676d23702621f0c41d6d38fb9e354a0 4393d0b0d5af19f6c123777762a186fd30666e8498fd76468c3cf928b4e6fe1a +189000 7c43d2b882946af37b0d1b1e0b3c08a8c55e83b9d93e0d3c10ecd3ac7a49c1c9 0a5758bf838434644aedcccdb7905d4bcbd576745f82e53ef74c9539b9c95cd0 +189500 4913cd6821bb3900bb012df98da308582c7f99c8907d1dcbde5caeeb04ba1abe 83dd01f4f3544a1254fb7b6c1fd8654a10b4ed99581b1058c79ff2a3a08b3ab7 +190000 825d6842d16dafb7b6d3fff172fd176916478e6a14c50efca5e2b6990f7c2299 0d7f3744d4e18d9132d3bf7e94b9325900ad8a574c6a48b04e6594f2ed07083c +190500 b7dbfde08c23efe184956485d0c19d62325d121b493560ebb0d45321b1832b38 227eab03ea5949234a281ae85657bb5fa39b271fa2218e294c4dade8273639eb +191000 e61629b5f59b130b4e6fbe4d3aa8027d13aafd890ac3a16b1ffeb1d3f590bc5e 7736329d7fc7d226fbce139fd58d1b21a41381b442fd62f1d03bc53b5580f906 +191500 06e8304e59c8f386db5f89bbbf524959e5674c1f560121ee65e04b5902833f98 5e099e60aa5f56a39831962822390e897947b435b4b84cc2f45a1e98319aeac3 +192000 7a94bca1896611bedd44c28323897ac0b065d2832d36000b51da6d8da42ba38e 640f07390bf420c0f65d0b59112d66b148df416f2a139f961bee379df0928bd7 +192500 23a1270346e2cd8b1439b2357aa6d9967285d727963656385edad740cd66922c fe551350fefafd463b3d5f47a53ab239b346bebeaca875c6c7a87d42ba976bf2 +193000 5c7ee526691d558d00b30f649c4f77ca4c595742f23e389ead78633f8ce2dd35 68a041f99854b2ea4ae5bec9b030b48ef9edb3aa89a144bad9c7e744b234d409 +193500 d03957be53c5a92ade2f6caa1e1999c3dfb567084580cfc618fc7bbc6758fc1a e218bdc4ec703cc01b2ec52b860b2bc0ad51b90087088c9cc0e58736d625de91 +194000 d50b3435b1f5fe763970370fe9fba1f0a5af9e9a9abe295e1e61d8832b43ffe0 6f3878f181e0dadfd1dafc8b2e75c73c6ae0fbf97a6bd1894b20aa93139cfd80 +194500 43f28b5c1af708ef1725ae8b0e4542dc6c6f33585455e6763038d93b37992cf5 6792e3e7d2ee64a48ec47e9fb86823aa726ac49948e3843d43ef80995a4ba7e3 +195000 27deca29e0f720fa505c64794b7904c2f15e2b2bdd539f6066bdbfa8b9cb16ae 53d423505456177cb9e6e39879e51b1a473abc363a6df7025d6c523f71fe10aa +195500 4996255d3fc243ca9b29ed6f5e7cb2799330bcf2c11e42ce9a82c017fa4dc47d 1ee2de24a628d83a4a9b796d5f3c6c65d0327202191554d0362ceb1418904a26 +196000 c3861c14add9fd9665a1751e1bee488a13e0156bbd37013e3e6a8da54da26550 4da431d08df43f875267b70903b275831281c28902749fefd4bbe544989a910a +196500 9e85e9ce7c9f969af748556cb124ec97c581e6ed8367c4c44e8b9b12d5495ea9 14f7254f9bf3d122eadff425ddb06f86c16c4a4954bcadb5e99ba6ea78f8f0f1 +197000 387b990115d39fb36240de77081a933eb7d4f8ae20cd72f0dcc162c496717a4e 69c3b4cee6ad5d2d315e2b133a716b8640b170a0f5ea53269343bf4455a886f9 +197500 0db48271823bfbbb62c8d9bb68a0ff7f46724dd251ceb8244253418b3089060e 0c2cf4682db25fe743e7e2781ecedaa243dda4d9944edd9d1aec7a9e93b831df +198000 c65104e71ac22bf228691460ed6036046fb091eed1059ce756e1056bf149148b 3c3f1f86ad2cd5cea8a979c55d01fe3dfcd5d6ec6fe485c3461d65604ad4adc0 +198500 30e8c038e2a43c5ddc9e0f3a030070380cbb09e577ed7d783b83651df9ad5015 dd0bd23d500ed9eb599249629cf07236244c001e0517c93dbe6debe262e7f4ee +199000 4b4e61aa4eec7a39dd94aae2c114994fe32185c72050d3908b7fc0860d41458e 0443985e3352c7ec9e83fc34187460671679f71a3eff263956dfdb306f4e518b +199500 09e62ab674ae8526370e0059097c80cbf382fa098e67d4a1175bada96d5d87a1 b78d27bf65267b4e1e7298efed4270582f8c9e26d53af59d73250d4862ca4968 +200000 cd9a04e20b651dcc4518ae71ff5b0c8bc2ed5c6eb3d5fd2196b1e636987c3a95 6160d970d27a8f799d74d3e01630761bd655268486a9107be28b128fbbc9dbe6 +200500 03609721a5de60ab9175d5cf5b9dc444f8c9d420b94814be90e39e4d5411f33a f1625e3d9d93f152c9e351928677f1e8cbca9c6812473c759de6216321fbfe53 +201000 f1be5c8c40f967dc2f1448f38a7e7e6a47c6d7e6ae9eae150762642394644f19 562f88889f723e4c4d2a7b0f8b0eed383db6aa34c8e094f88238526206af125a +201500 668f7b1463226ffe529002e0dd28b813c68396ed381a5725d166691660a0383c 0077ebac17ba34e5d77c492ce62473602d1010d21a99380380dcf52f1a6a3318 +202000 60ea6173bbe62b24b704cfe2b6e1d1995dc8854d618fb0e42e0c712110b70e50 aa3b08fccaff9887aed03778827f73871a658dfae6ad99b7521142cdf8e86dd0 +202500 6de298a7a2fa7ce5bba344237129f982b1071969df56c15d0ca2c0fa39f9c1c7 dedfea5340d820409423688ebf7c236cbead8e502dcb39af7686681b1f4cdea2 +203000 0e117900e8f0bc91b0279b83445b8d9cebb374dd81c84a52f0b3e6b94c880e1c 74c1495fb1f720209dadc85404e75bde143a59c7d21ecac61204734dacf63545 +203500 ee347878394b9da8a6c11dcad54882196eb1a6acd7e323154167e764d57f00b5 6ef1bef22de744dcac118ba398ca60424a713140fbb76fe0555f1e89369458b1 +204000 a687e4d0cdb3eb142d1acaed94b80625e8d9a9d472545ed1dec3c5e966db98b7 728306e941f0ebde00511bfc3a30e50cbf283e1850ad4145f2697671f14c5775 +204500 c813ad2efcaa352e786ef9dc912e133bb1f32fe408a54c114152223fa3c4e96a fefab4b9604535559292b1b31c4996dd815f25a8ac474cf8bc31d0c176aba97b +205000 b67beac20c8c3dc2e07c7d88d8bd4922b8803da3a5cbaec8c8b0a6601314df69 88197fef17e17da657552b7cc5ea2e09377fc466d37dbc22ff8a948509057d63 +205500 0937f7527f96ec479765e3be263f25130d7898056813dfdc67f3f95f4923f2c8 716b95c3c62dce9b236cba23dfd9dd3fcc68cd41cbe9d13d6f8349503fdc3827 +206000 aaa70022052f41b2195c68a4c77c11b78e1a8136f03477d310a57ffdcdf835d2 dd9991c1a086391127322eec9667bc193908aa869a012b36f0968270c8c679a0 +206500 7724e797f7f8e157fe2292c5a76a018278bd7bb95924932df289d975c4191c6d dbe2029b6398ed4f32b906956006915345255bfac82ddcd79e44848574c50d36 +207000 3d779550ba9dcc4e29106cfbd2efd9b22340432345c0df139ab31eac25a45226 cbe3c2dd884e9ed7a1dae427c3d475a8c9f3c8803d0f3507398b92a6cf5ddb96 +207500 5484677fb6f857f8009fe0e371280b61ba15d0a5fac08bad41392dec1f38c85a 649c2c9faebadeb93551828d590b757b444b6a54b40298519e5aa8505fd81009 +208000 cf31a458b286a4f77890e65d02cc506bb0454be9f373a1685f2f6b2b91a76207 22e9abf8f61d55f7fd7b85e0efa3d8a0f6579f43020b19cbe921a79d0122000d +208500 4fd3fc158e9219659c7ceea1c4f02bb89563c7524e9b8e9f158b61bc82c52881 4a0ec89610a1cbe8193dc195991742ac08583b9c86f9d24a5b63241f8da4b476 +209000 d54f4d0b5a8ce4ee97d0ef4cb455ac54b93b0092e177f1532e6513052439e9fd 59a0a1d9ca71393c4e4271d8d1ea63d8f0fa6e8388c610fe4bfe8362dc0fb231 +209500 14e2bf16398fc52492652cc0ef3fd516eaffaf2cc3b7f89e37f3bce63d1117de d9a9a12a655ef80cc830f766200a05617bb235555e7cea98818bf103a3a6c1ab +210000 4e035fb68e9d78b953e76e14a90355fa127db686af2bed4bdb301fc3223c322a f6916044d1b6a16eb726cbd05a2e900f5b9f828625c850cccb699327c22037ba +210500 e9ad24df7fe871c7a87de17e0c091b99a332b47903d8d39da2a18ccafe02dc1f f2cb48175161b22cf3369f59e67941f77675c6109590ad622a4bc476364043a4 +211000 21fc2c297b7bb7c088668fa502215ac26b2ded973796765acc4feaf2bff112a9 e21b83547b49382c15c8fdb2929711d49eb8693d08c0738813df68c359fa2442 +211500 a83b2da994898b752271b844f26d88d300646e04c4e89786f24fb4c7397ce6fd 4d8d8b7364ca603e61da4d09bc9c0b1dbc2373e65f5db2297828aec6395b5991 +212000 ab0f9dd7169c2b902f4762fce10983fe94e3c349353e189a510781244d66b77a 3ba1a0d61a8b8a750cebf79f5c00362bf3c909f11725bc9e9762f21de08a7945 +212500 b1fb4ee2f329a5664e6be5c85834dd79452b1f89551d599a70852bf49daec183 d9143da167a6553a85f452745577ee6ef19fd21565b09ed5fa6cdc00a0f01a49 +213000 dc4c87ea7c60026a65104a0d8a04e5540e3952df79a62e59deebfef6ae235e81 8da8a14d3971436306dba00351d265f0bd06b1f59005f94336aea44880498b5d +213500 2466c2e233db8e30bce3d5ba87f694c3095fd391b6c38c2edf907a4f97d498eb c3f5734faaf87bcaa9be69122a144ad1f26009d8fce1f859a17c2fd4bc0c2c0c +214000 fa84d9bd1c6575cabfcec73e1834d129821b6a94d7814d15d038239cbc4a568e bd1413d32f7719df41ee9caf406a30c8b97e2f875efe7c9ab2fc56555136b5d5 +214500 556dbda0caa051476bf896fab40e8854a166c7bc416d5f3121fe1d312369dd0c 4e5d7dafa56fbb04283e8c41e18697dac7beddc742d098bfb87d4df1fe90679a +215000 b7ccb76ed29ea49301f630ca5e9cf200ba7a9388156304636814ba6a6b417265 8392386752e5ac61b9856b95e9c833b57fbbc5717e93f63e3944676202df04e0 +215500 a56a071a789afd22ee4a9e4abcebd695a26ff824a895f5291ab639336d609bcf 6dcf6a9b561236f8f627cd567d937ca4d7e6e9df72d466fcbcb505af9bf1ab11 +216000 0e1a9b2d055f2e7b637d9fdeb778c040967a8a2fbe0a38ee57bce4e8f4bb0a51 c5532c221b5cba08f4ca281a247e98d28654b934c8595e57873232111c347ecd +216500 9890c0ab7b427bfa46ae90fda04503f1b17a2493c7b36f12fccf0dc010d3f9da 24ba34ff5619c38e4dc516e815af584f4c5ae6518a57918aedb59403a3b356de +217000 b282a7dbf2dfcfa4a46938422e557404ddcdf11509053fae49f5add5dd101d8c 645bf6456c36a43bef5c217fd00b1e457ae136d52396e504db5ed9a97e406594 +217500 9539ac7cca0ca0524060e3fb6ee956d28ba115c7ae3f54d5927492d0a43eb49d 21f680a60447675216989ea718c68c63361fab5615c683a7f047960d50ff8873 +218000 dd15ae9a3a001651303c757cdfa162b252b9aea2d1c4fcac645cfbc1d7dcbd7f 78cd6e280ca819c264e6447e6cb93c616f0bd03547259a0f23ae38234e907dbd +218500 6455bdb4441ac37e8f0d04b645fc033010698430944a873c2c097f7c0d3a02bf 466f4eda729538e0604472f6c00c4f7398a28c9e59c89602716f611ed8731edc +219000 a8944486d2a12285430d1764ccfa03bfed013dbff38ff5fada97148b883db9f3 5e8d4e1d9d6b8bcfdb3ca94ef5d0f8c75b9620b7bb5f360d8172e1becad9353e +219500 adad89636c4dbae0dc680326d95d5a2bec6ffe7baf704f26889730681328b3e8 d17aeaafa2e7a17d6aaabe48e88ffc53a8e61bca58de2e844fade3461f4931e1 +220000 69fc4cb8e3103151a20add15fcd55a9ee3c1f2cf78ed3bf9ddcd0692b78c2a71 25cbfeac28f67981d5d011357c01ad82e097c0d09e77f0746fb11a93329c6958 +220500 fb7731f8712f081c1e9f446217bc1882d62d3db40bbe2711a25654e2e74c6cb0 796240f85e38b57a751b19d648938d9f8f66cc8cd2e8611635832cec8d25179c +221000 488df413aeead90e34c182fe75cd71227be759865df577cc6a4508cda9f13213 bce1bde7cca72a43d9d80b03ea33c845799313914905c7800f7672654821a7a5 +221500 84d958c341c484ca8e85984cbdf5ea436fc1564c95baac52ec00e9f03d5dda3c c493d1eeb83f8dad5c17eec83e69277fc98e519c639d3c892faee2d6f80a5fe7 +222000 24f8be00386b9a38f5507e77e72cea34598910fb8aeb57c5d7fd5b8b082b1d16 77d77f3bc606ba0a8ac17dacf2d6d794bf655c58709a5e6affb1c2238149fe94 +222500 c9db027d5a71c17a32dfd7694775edd913f829900532a36cc673168de4b41cd1 c98ee6b634235506cd78e47c3c51ff7f20e2da5be5bc51c9269e0585a77f344a +223000 2491331dde706d72557719b2d51300a41185e0a231600a748cadf6d76cb97be2 f464bf83372078d78b37b0aab9e98a697eb07bca9eb62d65919635d9a62c29f6 +223500 757897eb0ef5db6fac3a16b4167e73ac300fdf2126d175ddbfab527fe72e5cc6 8033074e1db6b73e123b62aebd8983887d50385f70770bec207b628e9c945425 +224000 b07c519e796e7ca9ecf620e6437128ea8762b00f16ffb3f2ee2abd5973fc3b21 eb8135fbce4a5f3d73713eb67416be84d02239e85bf87dccd22763075438c693 +224500 4a873566e5cb9ef793cee37c09f7bc11668091934776eaf0cc5254cf8f565ebc e0b9c689e95ff5d675aaafc9916f010b7e5b9a13b201686d4a600c5516a8e263 +225000 b4b671d3954ba70856a978c8c173096a9db7ba168c8815730cb445ed0b921f7c b16288a5f727839ad38a64139741eba8f93f33035d294365e841e714927ce5d6 +225500 17279999d391f86406797119b105c9f5f6a02678f7c3d34d9f272c73dd77faf8 d555247b00f10b1b9825a09d2692d22129b967e1ff244d1a60a756e7b2d9cefe +226000 94d997cdf120719be0ea6a4eb198704a18baebdf13f65e28e51b49df7de1d861 d3d5f6655db1fa41b2f916399ed5f36246ad98025f6943480fe15e43b0a8a034 +226500 dd41f722e300bee85cd30450c9fd08c3d4102b1095f88617c10b100556c5da49 f43f89d4fabd9330fee3336ad9bf701601c33aad3abdd56eb6c164c21da1ff10 +227000 a0e48510962ea5a5f18c8d413b48688843e5cb36b8b74b5e61bf357b43334ec3 0fcdb763c6a77599a872c5baa6fd1ddd761889eca1c53e882b616dbe40de391b +227500 7f5337c81192ee64e2e1fd4da832f60ba2d89b0336e8e169423dd403d6f76a7d fdebb66099af9283e9c026e1e9a112a66af6e3bb0f657ef78392a9dad4768963 +228000 a8779b9ce01000a531f400c46d8930c487ad00315f0ec4ee8acfcbbd02ec9e7a e9b6fdc4d73e7910e18d95bf096bd5becac7b96a9989d60e564f1ac2480b1fcd +228500 074c0079bec868361a58eae22d107577538fcfcd531bd3b0516becef48d25083 b0bb15325aa285bc0890e24b0892efb113246e6d785554e59022c0917ee4c35f +229000 27c38d7adc92ec6c3b2e5c16ddea6cc06d25cf138ebd45a935953a357143cc68 f916d1f8fef52c7532988bb984eea0f136d7c777fdbab72ca0011b4535c91e46 +229500 1010ad9db0dbaf647e510b73feb8352b927c1170dd09dfa15fa0f5b9735852fb 7a7b2eb60a9dab15faf8e37370dfcbb6684a67faaf7ec1f50980694c26dc558f +230000 e84c9aa480b0a7900f557d49afdc6273171bfea6e6adee45c3c2c7408c7a172b 1f03b2bfbe709d7b411679979aaaf2eccfbf14b2a08aecb7139b191691acf77b +230500 1cab5352d167b21bf28c9b3b0f8cc821eda60b32ce9f2f77de5737edbf28bcda 8867625cb142e048d056cfcfe5627fb7ac7a20a83de0a9fc008bd4951ed7def7 +231000 a7e7ae0369d5839690ba5cd0f83167cf68e6dc9c5604feddeb986b17cd35ae76 4f7deab8e1ace7033b726836f01863f33677e4ecf1f8a488fb19b99630fd2176 +231500 3bf6451daa832303c2242f195ad2ee1a205ca168fe132087ff899461f29dadfd 6b87a643ce4a5cdee1f45ea3ee7fcb9b5df8452209970de76513c846c73ae7aa +232000 6e1ae43ba56524a6e2bda4d769f8ec8e99620690baed8d8d1764c3a42509af81 eff179dfdb3a490f2038514a8ca3c65a1ce73c266f808378d767c60d1a8f9b16 +232500 95589eb200430bba2e6660842109cfceef9a90889ab8eef7091685dd052037d7 2b8b93e3ab1fd3b12368ba3dcb7b087d84528932cd4fef51ae863dc40718df55 +233000 edef1536377488c20d7836daea81dc7b542347febfdb3b7f0e89ace9a9a2f980 ef9ee2dc8e6eece115a4fa188288ac21117d58bf45921924db619655394ef4d5 +233500 7cef0816722fc26a5a131003a9df2d08cfb005d752a7211cf52c6c27acb735ae dc9115037b26543076cef7cfaa03c2839378b0cfcb8b164f883f2f73cfa95b11 +234000 da363a034d807c2ec1aab013d8695631a0d24c50c34dc0db04286a1a53fd58ad ef5a49fea7936d6c151fb533277c19b7e2bb3e88e9351cec946bdce22c5ae5b6 +234500 e35fd8c71da099b9f3f1d6017c9b799b6f150e553ef827f83f84b4d62ef723cc b469f833b3d307d9f12fdfcb7a9777aff2c37a2ccf713d2239275f39ccdac37a +235000 903c98a140b5e8ce72a2571fc17c585544c4076e6d2b2efa8c58b635adcef6db 5d048227793f68e79facf5538ee5980089562490688dd1f929fbfd459edc3263 +235500 1e0c48356729ddb9196f9bc8cfff295e2205283e468534eb127eac2a8503612f 152dff3242d0914c503c8ecdb376d1cec2426abc09cc99907b1f43b31dc197b3 +236000 48c76115fb5c845fb7e9b8d9bcbe2270755b054db11cd6ad6c2ad89152607576 1420e404d7b541a053200034e94f9bdadd705dbfb57532c2a454b9b17c36cee9 +236500 76dbcbc51593c2ced3dad33d42a9f8c522f01a21383b227b0e6511c76363d782 f432ef854ed6a03b4809d3ecd8a46def940a0d251d09484ef1b6bcc3271b0f17 +237000 8c821cb30e183a3f5a3b28bff8b16d1606566e3836a2eb0d47f6c02af8d3f324 b4bd12526051dd6ba2a2d616a95212c8ab4d6adc18ac21822a57738eb8284e2f +237500 ed4f21e0bf654339e17083701448309eff81e272b89e00fdabff166cdf812bbd 9d03191f41bf9d7bfe65b5cea79e55f32c57765ed5909e11ef961fa56f3f69fa +238000 5af7f4d9b69ce00a56f5c7d89de2ffb0df0ae23520ff218ab2330ce4817ee326 509f60bae30c0cb7666f7baf33fedb510f3364abccfd0881671650caceca4cf6 +238500 e95df9d3b7bae4323a4562627b9926d2a3f4c152e93513f0b57d83ec88ad8868 9d0e160a8f87e36e9fc815538b27d8636daf94ae5c8701d323995054446ade18 +239000 17dd8f7338c85dbdbaf0d87bfa224d5fc13e58aa465cda17eed21ec1e5a13051 61604321167ce51b0c2043524749d385b40cf729814d88e0394149298e1d612b +239500 d10a85ecd99d5fa8df9df4aaae9ff7b6360bf0f6c541fa6e1c8d743ba350d1b9 085caebd6ae46fd58b66481d21579720316fca78552121f9252ff8fd5019257a +240000 dd21b880d234b770079e0478bddfc53646feba611a1858a82be2d68a5f70b5cd d0bdb4e68e287851256fe8cd8518ab06b2f8bdc30984ef27feb96cbf2625bd81 +240500 4bb3da166c854ac97920a04fff729b2da619b130b3b257fba10f5be47354faed b7081936dea5f45ae555b58ad83d42234183f80123236a1474f1df92b289dbb3 +241000 cad2acf8c8a9802e5c162170b2cb7112447b1b5bdf3184dfb6ffca5d5ca066f4 50a13b720476b0d72efe8fedb30d6c24265a0cef374e1de14df89e3488fd3930 +241500 f89869f168fee009c6b743da990e96a296823c795da2fbece16346aff95cc0d1 f1e24d47abbd5056f7d3fd322a882ef885585d877ebe23b86787fd8bfda5c03b +242000 8cd2957d89f5937f87ecdc188f5ad6b275768752b873f36348fffde02bfcc4bc 9e37495c487f19227aa0b4312458907ff749f668a3cd386d46318281e03497f2 +242500 4d84991c165bcd35c6f24e4124e5b93678214af88c683ba82419330162070a43 dbc9324659ca8a138b7f72edddffa009859cbdfe03dd397da0d897b40eb97efc +243000 99f2ae7beed98478d52ce8a3e8106262d777e9220d54159cd29173f823002468 f1af12a5aa6ba842b39e357a45bcdcf93c9f718992cc160e1849c693211642cd +243500 84b668ed1334a0f60f2b7cccd9a1bd05b758fcc1d3b6de13314b60b1ce8dd69c a02d27be36d8ff1a07ad35d80e1b2340c042bbfeb7ba7c0f4289eeb8b66ae117 +244000 58ccb25106990e5eef0f7118707b96402f853599ec51917347012350230701ce 989a5646dc6e9f91b27d9d3f3eee07de2267b5b62ca8c2e0faed6883f1d04e18 +244500 60b50cdbc1be5d94c144f682c62cada4668154d94b909100baa392bd914fabfd 15fe02c7705d1c3860305d80ae479410c94dbe8ed03f942dde09d3a3094163c5 +245000 538eb0feb39a02cb69f673a6707d23f80be40a922ef8fb43ddb1782d8bca83e3 ca4b24d390ad10642d096a14bcf8fd51d7135681c9a750a80144b107ea02ce1a +245500 8e7a40c1deab5dedd03f960399dd0a59ae92ed849f0288ed447307112a4679c4 fe223451b524113b6d13c696681af1a1004e5be0fcd14dba4edf5d43f1b52894 +246000 e5410e4186cb320449ee28882d9f32cd3eeb79104f807651b8dfa0164ebbef82 0a4203ec776383111ff8f9ff697c72bd8fe1203d55242790a9cd42086b2f6013 +246500 0c2a1c0920dffa7e8a582bb47b121ac02167b2015deacd3cc2a4151419dd8dde 1df603f77f223501d3036d16d2201004c712f86aed58cd0b3d89b32fdd1b2032 +247000 def0d291743461c8680127c4393f912663ea3e8105026bc76c315de3f7471a17 830d9dfbf53d41d49c1a28631361db55fee2284001c16395ed3046f9a9278212 +247500 41c85aad59b4ed9baa25743ed3b2205550292eb7e5e4dac974f0839053d0df75 dbeb22eb94053b3bda3520ea239bf64c51d046da08951074dabeb73b06cce009 +248000 e34f803c19b9214d128703d584947a348459dcb0191cfd0a3928447747b7e3cf e58a6b50690ab0de2e1d7e5eaac39e60495e7f15c0e494797c49ff0a83e80d6f +248500 779f30864bc7a0eff2ff65a65af9ef49ed7125f708e923cff6a7a88d661c1688 34ac76a53fca6d0daada5f7f12e339fddf5d654bc1ebe03267716822cbbd0514 +249000 46d582e130542ed696e49a52224757fbadbf61037d15182cbaee9d6895261d72 7487aaf55ca988670dec71cb5386b80d8975446ba57feecaf64b3cc9c52a18e2 +249500 87fdc18e9972ab01bd0f1c0314fe017d5b825b2a802da4da2be0c495e8a6c2fe 364509f6a5c984e616212b9a328735397515d8392c612f774a55e8013b80f868 +250000 f345e75ffd358b61924b0bf3fcf99156f97c1479a0b31c53713c219a38846b5f 156b414c9c8d5782e5ff3c541bae9a14641338757b351f8cd7340d91ffe7c8ba +250500 111446c3129d174c80978898f17b8c14c485a4c92396a8c2b15c76d7da4e4ee3 67bc454190eebae6062bcfbd41b10f18161d777d0c7f1547d9c922fbe59301e3 +251000 a1d9e94171306fd7e28559cfdfc8be43a89ce142208f0b4a4319177bd6e1821c bb9e6ac2b06ece214228cab806b96fafd12d7fab5899ad4b8f6c57764c04b65a +251500 011ed051ea80d7aa8c5631cc3f6c808582b7bae9533a47a1e9336280e244a66d 5ab6d307a8215f078406e4ef5256e86f30683732743e4e102fca988e3e9bf157 +252000 0f60353af5fa6f614467ac0b793c0cbfb723b48353e45a163384288db9039860 977d7256cf493f632575935a40240e3cbc08e8b82b9cb78666fdf4e17a80748f +252500 856aa75176f71b2613ce0655d8bbefc03739a2ea066d1a396f608d596de9214e 6edcec572883a3e3e60b10a312a59c1e4486a0c4a73f5726b7b3cd1fa5ad8a8c +253000 135cd797ea002f028f372b545ba294120d92fc0368996f3f0a4a2c3aff5e2178 f6f35b4573ebff09a1d86085a7c5ad8f5154bc67cf6f1b821780ce3f39d845c7 +253500 7e62b3ed039f164bd4b37eeacc05736f7160fbd8ab2a0ed7c8789817ba43f47d 92fe7b5cfff4d6c5fa17c317ba47417e2841e65a9a985499f3ed0ebccb05993a +254000 413a5fd20a447bae48dd74c04912c73ced49cb866c96a65aa447164084dc3723 dbdf24cc5fe71f376465115ace1ebbfdb9382c6a47ffeca175f7b1d71d077109 +254500 dae6ea70719a1798ef4cd7eea67fb84482f58695bbf68106fb1ffca714532549 aa0bf86ce61e6a6801a133340c93e099d26f06c45ea6a7d0303866d59720cd45 +255000 48686fa37b3f22077cadd219544c1d55015b613ec8c5a4c367b0e32a1d93f114 8419590a10c931fc7b06e4f0c9cd66add2783839c03872569a4d84100fd3ec7f +255500 3480ad9060e55cf444b9f63adf976cc48c8e68e9c5d9f2e173d30ee5e21f8e55 22d1784cfffac5b2f3587152e60a93c18c3ba34d5872ac90a1bc249b98aa17ab +256000 773459b90dc0d860e1caf2eeb99f85410a17adcd86b9c7a91e282d7375f69b87 dfe7baf5a19c7d41d0be344f1c0648ae6a720b5c9b50d1a1755908da38ca28bb +256500 5c5a4002c3f5675ed11fbe0d1640a0cf72150462b9f38f2b6fefa04e2d47e57f 504eb85fa2d5bd399b16262f3ed52330fd25eff4396ce7ce96604c58c835314d +257000 f957080ceb81d1ff1a57fac8d99d89f92332198d332b82ed19f116bb2e4bb232 2aa6db9a7e207b621656e249da6a29182bb6e77489137f185c4adc4b43c2063b +257500 124667d3ac4e97b204a238e688229f21aac351bceaee670bc86b048aa25a326f 0806775e04bc4c3536af5ee7c404d2d53e780775da20c269b0c52258be37dad0 +258000 281275e81b624913ab150bb6061bfc3d065c3b8e876c1c84c933d1340342804c f2a8c5a57fa4b177d510bd095fcdf765e7b4c08d8ded22f12c07e3fe85011df8 +258500 99b3f45ea656e722d919f59a89e1ef97e171f1422ef68fe6ea17e84496369d31 4b30ebda14fbb69dc80d6219066b66d8716e2235595125113444f28e2e0e3ee0 +259000 a3c8b1010cff2df85b748d06b4070653855a780fa89c1fea1df8af1b99e1f71f 611c731a6c76c22ace3e93b064a4f73f2debf5f63d270a9a3a59a7a46544d6ba +259500 6f80b9fc49fe6de69d6ca2ec1a783fb006b73c7e2fc0caac427088dc31d0b527 d9ddfb40c125b781b7780845a78f6c90c2b887b8c28a0a930ccd6ce7020bfd3a +260000 d3f3a0df265b13c30fd6d529da8a8628c98144abee10ac66feb2d98972e52f54 31ada4b62db504d84fed898117105427ae16309d45de494c4a266316f8225d25 +260500 10c9f1214c1a77cf5d4097d05a4ecc043d8db4dc9ed855892763104094e4a1f7 273762c7e60334efe73f1325072dd8829130ace31287493f4028fa9c22c98619 +261000 bed3ea15e53cd2a4b2f8d515efa665b1552ac7bcdd575048efb6bafa8b3d0f0d 3fe66c2acc1cef4355c4ae83ecb34597060c44da2aa73f0a51618f4f6956a8e1 +261500 0e9b83d06669715a10a70adbd0a89d58a56a853be8c6b4e6fbd3894fb865c224 992896ed5c5c7c262850a9d3f7b10ab729de8fc572190e0c08745ca9aa2a5433 +262000 61022120589859e23c0c4fa1f2650329f44feda0823bd30d597be34ed030c86a eec28d8aa336511816f17eb3cbff661e6fb35bdd9cc7a63e9ce830e7dc1261bf +262500 957872933b3a7c22e527930d65220a77ebf04077130f18703f4d9eb8384edac4 e267fc444b94c0c3e8312c063746af1e93887e4f9affb11d03d5728885b63cc6 +263000 e06e647a254beaeea41f7d2cf9d88531c5193df8a75d4f1d8df403e1c2a24309 5aa9aa86564cd18ec3e2dca55315b46958ed92e4ed8710e33ecf3da9b051107f +263500 152388327d812eedfbbaf6f841061eb28be5ed87dc9453402355d8d06c64011a 23552aaa4ce81d4ada113f198ba4c848defa3870f8093576be677c2e3c92c149 +264000 ce414cd76a86b1af60c778ff9e5b04c31464198d303c9f1d53126cfa930e706a 2a3acad69f31b7b30208aab2b1bd15695cfaca2d8aa20bb2751afcb5755ac0c6 +264500 d259e8637b4b48b373b6a1a7afcd89b1056e2adda59b819654f393e63ad2653b 31bbff6e4910bae42fb446c184efe8191dac8d6a86156568daa369230b5198d3 +265000 ead49b3a388fe07616d1a8e8c391d50dffa01899a7444df60d4d2c40fd8afaca 9fa60617a2f080f521628ef3d754f1706c464fb43a8282bd07ce2ed26994be5d +265500 6bacbbb516631a7922e9a988fcc5e102d6f58601085f09511601f4672a3fe316 4a5b61cce54e08360b9b2e338f39f45ae427b2cc49034fb0f1e71948a7eb92be +266000 b6993a4fddf6404fe544fddf2df238c999366b43e7a335b81b4524f9aed32999 91407232bbaf55fa658262dd6269b1ec6c620eefab326e556434fa1d0d64cd95 +266500 a22adcd41d7ee4df6deb8633f53d381e9826dfbc7709e68b7b1ad5d71e8898f6 314feaaf908e8a8046ab6eba7cbe3c97aa478b2ad93c39aee7df9f5a72955db4 +267000 d270af2ee0a98e7e407e39d50cf4cd22005faa34480d638e7a6b7c10b8d59e4f 024ad7c62e3a9ab757d4905bba29fccf5ef435e4471fbed49ef50f6f9bd1075b +267500 1fa94c5777a0fb5f2c0b89389d3b537d5d6465bfd432810fc173dfed7a666c36 b4e5805d093717d9c5d8766b1ccbf2791eb10118c9fb51898f0c49273c3ace31 +268000 ef3af0dbba05b3633b87348bfa9492b90d0c500f8427c2f83e485ddb4e326b7f ea130ea23e47338355b0336648892d7c09251bcc9df307a7f587d7a425d2a741 +268500 f7d6e8a528849da7d14e07869feea84c406697ae7a16ccac0ef890aac5fcf4eb 489c417d04b8a41f630bca15696e7a05ce36d4d216c04b1a6560db25e03e3cdd +269000 0237cc909ecc6a560183034f478c44d9f9fc96d5a0369b89d5f9510676274123 e84c666b3a8122ccb250bef7b603c2afde722fd985aa36be484e76368f13f10c +269500 d342e101fa8308700ee1a663fd05b05da014141725d0b3655a3f0fb15da83771 93daf70fc7af5e7fb37d973246c434cc834a37a1fc69fb522f9ea5b82ed2bb2d +270000 83e4f6c8831be3fa7b6b6a0aa13805ae107ebe64f496be123a4ed9711cd69065 ef10852638bcb24c4341161935a116bf5b764ed34380ee55243a35c938fef7fe +270500 557c88f51ceba0d2dcf7bffe8ee8cb308f8fb2f14998db8b6c23764d00d756eb ed1e1382a827b3dbee6c583053acc263ef365697a9325d3ab3bb60f80621b66f +271000 8cf965660be26838d4edfc4ee6c9e3f700c5074a0537edf09d19fa8b25f9297f f717dc6ecf1404858433dea8353cae36a181baec3c8ab3fb0c8eaf17e76f2f46 +271500 940439c29f8259ebd1f7a7a76f9cb7c34981795365d06cef15b272ceafddb513 e013f425e7b9e0c919d3a8a6e584fda18d798f64cd32003c79d1b9acffe92f8b +272000 6aa33a0d087f0a56f911acfa99469611ce6a9fc336e2ef2ede07c854c0ac34aa 34612581d8665965d653cd9736459b2708141168a297c9eda4c73e64ee4b3511 +272500 e165b1f323ea11ebff46a9365e5c15c736a639c87e0e7866a790c212398fab51 6e2b03ecf9ce016d375afd28e4895a3905c5e79e64a451f4b5863d6f8ad37c15 +273000 7e8e42c625a39262e7cd8ba81d6042788c5685fb7e1925556bb81d87f23fc9fc 60337941f27f0191f55253642397b4141c09a3796a2e8c972154c90847a04adc +273500 50d2fab64d92e6bd34396a65ea083c254392b73b806c9c23d10a64fa2046ef63 def5f3f2d73ebc5c90d4a3e5e8100e51e3144b63d3de0fed9a6393ba3138f62b +274000 86afbaf7b386a0a5ca9199f42849ba58e03c52c3e14e4796be5a47bca65c3e56 9fa018f6e78596557adb6a6294c3ed8c02e5b07835258d38d00714b662cb88ad +274500 f4a2f13cdd599a02991bab088d21e7d28d907caab124755cda5b5da1a0ac3514 27a84bde9f23b95957a07f3950a238602bd158eb9fc3d8b7ba88884425fa2eca +275000 a655f397a1362429e51239e663aca0a3582f4a984001c6093e48c4e0a2e05941 c3a045ee8c5239615793c006587052cce7d63cc81c2ecb2af75e67f3c53a8ab2 +275500 b3b6e311d0f5bd738f2502c2684af690e0f71c2a4c55afd5661d069e604ef3fa 0690d029ffaee874f0407d12906116c79bba67f9b8378a31334b25483daadf3f +276000 f24a85fcd91ad6a82056c8a76c2d3d5d04070ffd7d9b84a94ad0d6b8acc0c187 5c21716aa2a15d63d306717b47e86748fb203faeb1c6b19d63a5349176e9fa06 +276500 787f090210f18af487a5a4f9f284f1d3314fafddfdee64723d3237f55d7597b7 3fafdf028ccf9b4ccc043ab7159a37ec9740b5cb34847a6ef427e117ed86f206 +277000 b431470e16022c9e3e58e772bc8db6482f309cfb72a197a773b67ee7ffcdb756 894962f8fdc8091b4f15d60a3ea4de232e7921227ec9092e4674338c6d0907d0 +277500 d4f3d44e493b537d6f5a6e63e315dcf5031240fbe6b6ac65a76eb8c35f5db13d fed8c64ebc096321c5b99ff1d5d424c213c3e75d13375645f37dc5aa807eb57d +278000 4f76c78fc8d544c1e8a0d0429f7dac32c9f0857f906944503c80f58d809d25a4 eeb4b5d853e4a976e994c9e129bad662bd03d8f2c7e78fe68bdbd47c28480ba2 +278500 494b9880d486e1151d466eb73004b3f226ea90b6127a4aef1b3a90edffd4d0ca d9b63101013dfd8dac4895a0d5ece4c62702aad7cdb81a6b186e530df840996f +279000 8722865896dd4d2351ef4e4def382a2e622b1e46ad54b3940a16f5073bb1d844 2ee3816538cef7b1d029887af7e39d60cc1e336036fdbf089439f38cf13ebcef +279500 3423afaedb970d2a590a0f3e023906bef4110f1389bdf54e2c4620c36b72a3ef 3e7446b32ace025408872375b1f3111075f305cc83f615ff2c6133e49ac96d88 +280000 9cb8d7f3caee172f2a7b8b8c2bab3fd01326468f8b5932bb88921e06167a0e31 468c01acf084fd4986b21d1c0e1defef6a874a01b0ec21be66098d53fc4244c7 +280500 6cdf47d170a6a5e57f6a9cfef7f2239d4f15a61d5c37f5b44a72188aeabd541a dc58435982155aaba2de5c124a9f441a90a575c9cda3f071fc9c4a3c9db61c8c +281000 3228123dd268365939c7fd5d9ea6a151b6a95825ee1a49b9fbf3ab9f3ea45508 9320e6e8aef527e622e610ab13eddcf82598b3f2b744f6e90727ebbad920c080 +281500 a35ba852d6be87b2a614ceece176b04152feb39a95e52e641e7da7c1fcfd220a 547cc277e5a3fe410e5265d8622bf0ce0f722c39c23b3274458f7f99e68f0ef0 +282000 2533068b9c436719d1eadc4bd7e3e805ebd949d57e75cf5e0b17321d109a9a10 1ee93f862e7504974feb14b488a590aa9221ebf331beae0786bebbc013278457 +282500 47262c1b2c424270254ebc4ad32694e51ff4c09d8c8da9e944b17ca3bc734e1b f940a70f96d1369af5b0318ee86f9902bd122036ace273643a8aa0ae7878764a +283000 bbaa0da5024d6e74becb1d0d62b2aa4a8ce1c3bcfe27c587d3c89b581f22b893 b8f67f07d3e46ae0436cdbe91a4eac1ffd8a8de111cab322c4895423393be210 +283500 008c22a6e8d0b46e6c785736f6098de9ac43d5d1964b2103ab383498623a53d1 77b3fa5d6f930d2da8b1171725e9f6e7ac340b88637955083de8a55a0d129b70 +284000 6273a37ce3e5fe2c58d6b1bbc1f31822f9e1f78e434d62c9aedf2c6c18faddb7 0e5422505c2be9b93d74f39ddb43b8f2a6cebccdad41d3e631e56a5c075622ea +284500 6dd5efb7d0636ca3212a14ec60bdb9940d355e75c4c3890958c2120c4e0e92e0 8adad9dc3bad71a55635afc41505883ae38fe99f3c96f021137e049a1b829a55 +285000 7d49941cc51ef16e04919b3dc5c5ec87b62e98267f173202167ee9a359d8a5b8 d947bdd3999caf548887208f2049fe528335634f5762d6882d4075dbbc6c6b1a +285500 c06cb12bcc248f8a0c2e764e2c160d85ca356ba7235faa143eecf42f7ac2f7b8 7ef16bd0db693d3ebd90f3e9152a8cc1c420a3c57dbb6bf02a86cfb3b1d19dd9 +286000 45583c3f15ca70ada0a0c38923bf9a7c63a82d7be1a05dd2d3d098fa6040b522 d497cde577489bcf9ea8e08eec093ae16e80f5c58ea09344c27bf55b98fe3fd9 +286500 94f13e245e46c93614627914bf086bde72751bbc3f88b35877d73bc28bd4a7b7 83245d666cc2a32058839f630baf72c0d96977e7aa4fc400f58e75f2f9ecf78f +287000 eda15ec5157413f65856008baef31e5d124c511a95716b1f6b7364f5e3b5e75a 5ac1908f2739c718feb04686b434dfa4e7ddfd5213d9977137354ecfdf5eca28 +287500 9f7a944ae0a5d2caa33f72042e674d47334445b2f47ac89da38e29357301a060 b7fca600d7445e8470e57c18e2eadaf73aae7abe888acdfba5d458197ecc1e6e +288000 0853047a1ac1adb0d95427898e88464c078405fa22ec5def178eb5de7ad31e02 a3369699142afb6a7da295ca36953c883d67a31d3096d158486b578a6a585fae +288500 eab408ff9eb88ad56e302df11a2513c1dd7873311632911cfdfce5ad4f814066 4f39359784cfd3619082b590cfdef0d6978434ad01885e66ea50f2d96abb2674 +289000 1eacafb1bff01bc0489b29ebc39bf137d040e6641170ce3dd89ff6ba59abf97e c8d5fe21e49f66adbcfa0c7d71ddbd5bfd2210554315f1142656f3802fb9de86 +289500 887dd7480f6c7ac0983d66c8af20fde72fd415dc9341eb28e1c017cc0ca046fc 64cc676cdff6225d910a798088789b311dd78932e090292f289b965e35812bfa +290000 d537cc92d4d860c7d1e74c77d63543d1ee28bfccfb33a3798b8fe4ee6aad5fae b1422ca72d6adfc0c8ac92b75dd68b19615bf27ce351c07dc461e4a483a01d36 +290500 1f72a0f641f0e7c0a14ae8aa3a90a93130a302bc82616e7df2eac6cc893f8826 7cfd6bdf925165f98609e9ac990d9c91cb484a1b0c4f26181f519180b2cd0ad1 +291000 045c570b2414764749905e61329694b5feda76d2656dc07c208e0f163e8d8be6 ff11ec49560b7c1264cc610daaf95d5bb7f581edf89568fefcc83bbef13b5835 +291500 fd55aa9e9dcba1752e4c20055a1c9b6ac46a21a3a932f059ee574f49b721f507 3575b8d9556832ef3d4d929787f0ffcb147cb349499026edc50e4be282b15863 +292000 37273300cca125ee3ecca17ef0b26cc7880ad9b6caf876a4eb5f248743433519 9e2e93489d99a34477c9e7e60d7a971b7e7a14f2b1d1af1d797d95c945c26d08 +292500 c6d6356e96458404221b2fde26482fa42559dfd1caab1b600f045d8f6a2302cc 5fd0c22eecda0a4ec51190a6356fcedb96e3d4662a03d1c231575dca131b801c +293000 d3cadc149d4b135ec11e9b97cd25d6e1caac6560b16c0d53a798fdc191519682 e8fb2e323adbb30a6ac4a40239c68d2377e9c8fc2094901d694e0957b9815cb0 +293500 a0ebf3b0b17cd3dfce121b5a4466c2a60a9ce0169c79aa070efbab912bc0bdd0 c9da3ed914a818f8973e44fc8a4d450e68d775dac064b9baed30d71db09323e2 +294000 492dad282770f45e70bc4cccb1ff2d69568a4d761395515f41d53d0d703db481 7c090581b9e3e74b2ae80d821555b83a563442f01ec32994b87b8794c8a94dfe +294500 8923473c7806f6175ad2be0cc98c4066e69dd2acf8e7f965fdcefadbcdcbc930 689e66a64c3f66de762b38b5cb0593b6c0f076c84a04823cb1f927f5d6601c12 +295000 3d5870c04b03d0cfb60c9d47169b81aa5c669fcbfffc602ae51b930391d06f7a 34ef893941a5215eccb47786b0d108e5150cee56d691cd6d54280afa5ce5e85a +295500 96c1ad430e0ca606b9da9929ea3e6644ba7d42c02371abd5d1a1ae099654192b 8e03c6f39a7dc904f768bb459ba01c623791992208f124c37bc679e61a49fbe5 +296000 6407e9c420afe4be97d10102c8cfc0d7f5ebaac40f6d5e678cac8144f007fff0 d19593e2ea6bc1c13a6a3269bc9d7c6ff918f0a01dcc61daac670b37d2d80347 +296500 1f8d31ccc299f2c5b05e93383d8926862fbc09ca5b01085b3a1dd82d5d495d8b 31fe78d77df28d71d73858589dd74fd5362642fafc9c8d29ba137126bfc0fbc3 +297000 9cfe512e43f2cd52ff58f90464a04f076dfd0d12d59cdeb100818f109a9003e4 5e47ee43df09cc31576cd9b0440c619a16298d39ab9264f639b5b9c86f8af572 +297500 95a02da1b06b1593ea08b020b4bafdc67f5e176dde40de7a1feda249571e53ac 43777410c9686e5c56891eb7eb0c37f2110b339e5eefb75da0bb28b78f3072c9 +298000 0d802cbe03a4b85c23624a03bb35290b461d8d9dcffa128de936649b7007f622 b5b3a839917d03e52ba179edc51c12fe47a6dd9a5c57d678299387299219d164 +298500 7e4e639258fe47ffca25a4b7bc7b908cdff41c7e1a737abfd45fdea1ba62ec12 a8c88f6180fdb40fc4524197c5eb32ab9bfda23b88c4eda4a422994710030d1c +299000 d2980e238042ab1ee113dedd25dd97fde37f474e6ea666efc83ad169c7ac4cb6 5f89ed52f8866c340657985291ef23e18eb47e0631407dadde650801b632c010 +299500 a3f56b859e1362c88c4ff21ad8cf5c799c2ae565f46f15bcbd275e7cb0fbef1d 2a189688c70b29e449f6b3bc93fe61d4b2eef5af756039f90fd745b47bd74403 +300000 5a2452a4745eaa9cd3c1c42b7e4482193c4f7c3324ef185a4d0a403d2002c0a2 0276aa15dc1d6f0d96dde0520e357fb5ca4d8469115a894dd76e4a34d79b792d +300500 d02571cc76f7cdccf536b836770aa02a0d238cde89f23f767fde8e62e2648735 25fc046b62a57b71347be8c33dbe9d201af1efecf98e973b35294f32b745fe25 +301000 b8ef5fe2861b8750589ed344c9d8edc7ebb9630dac0ec507ce09bdd532af32b3 bdd32f8faa1bebe0c4d250a309d8e983c5444f63b8748eef76470b980c2542c1 +301500 573f412e4b6a3a7cd2506df938810bee7029b9af102e096e83861eb3b29647a4 e8fb5de4c13ef24efd099f7808c0952e287aa23d51a96d37ca919c3fe08b9685 +302000 18b4cdc5a75c82496545009007b5f488aaef67892f6886bb36ec2526f02a5501 c8898b9f003e3a651e963ce212412e35d3d825ad2170cbd1cc56bcb1f7a0c4e4 +302500 ca57a75391cd670c7e1ed0fde565c1c15d67ac4fadddf92549874fefc3a51c74 08a7221ce567158af54cd9ebb89e6cde68430c185f6ae18f1c4d7001970f0dad +303000 e6dcb8971d8b0e379173bec442b5e465d02e700b30a79598de9be17a5497cb89 41c38012157119f65a5404f0e62a7656673f033eb64e8cf4275bc81aa4d88805 +303500 e737b5b4805e4740cc8c50d60ce3769621294b1d20ea8b1bd3f3757c2a37e1b9 b5aba92d8014d56fa32993c21c908b42330c6ca382631f57162cc63aa1b842a9 +304000 9e5aa8da5821d8594bd9ed376f69dc851e5a7e0b4cdd755c6394f0eaf76d0e9d 225f9b063c89eac0ee1e3136e644cba4f65fbb475fc5a0e2c1fb66270f6f13fe +304500 45a43ebbca6f1cd31a03e301398c2e9e9d15f1bf91db745a7417b4d8f25ac394 4246e0afd88d5225b8ed8642d8337681f42952d0df01a71aa94d268790755cf5 +305000 153be3e755e4953c3810e08165407725c11fbf92665616c708bd3b824e973cfd aa39085be20fd4450a1a03bab45c09f669a0a50d84219505b7077c0bccdde28d +305500 c488fbfd55f3fc873ca49d57111f71aaf5a42c4bbdd720c0d96a5dc66eeb6736 546e53c9ec8cab56e41df87aa26122e2cab4077646b800b63134a712a1e3d543 +306000 83982570d251358e02e1b359a0d95b96b9f64f3736dd823654d47da633d48708 6a861e2b1c87c8002bf0794ea718b6be98b4d77ffa95a22a3decf18aa985bb25 +306500 96485ddc1d2d81f38a752fcf1ed3d7b734197c472e8ccfb0eb4ce3101a473394 068dc5f5f5064a475c7c84960a192433e9bf18098d581f0877066c60ff0a8861 +307000 0d209df0e3d2879d8a0f356f7d4041d6e0825ed0b33011d5c8db9bb1ec0c7d42 1f41650a79db8f7bfe188f47b7f1bb293ad4f178ad4cbea99d8f87876b68ce91 +307500 aad6c7f578667b9527223b8e4785e9a6739169528d9b0bb741119f224e0125a3 d56e00856dd907339c3959733761bf3d2ed206bdad2bbf9d49e8ce6cec4be663 +308000 1a02c273d30fd2bdfd3c0391715a78f6223c944db913c846927b8a20289ea527 232e616645960a14c845a721665ccbffe5a70408ac38dc2f9d53e306c40287e6 +308500 b5f04f2fd81500e1f7d817b94c566b97c38cddb21041bc5ad38757a1d11c3a74 5596842f2ec00ea253eb5a0a5565d21e871a511caf4a40f7e559d089ac67ae08 +309000 ec31bf5b86d611359ba7818f0926b3314c13e7fc6be4771e78aa66c65f308e1b 19be4c08d59662682dcf27d7cea164915e94d1ff761f8321d6302d7de2bafdc4 +309500 a40c3dc8a06147b339366c16bc1a21d4841ffffb9721f952276c35f6ebb0fc67 161fbd3c4ba1d443606832471979959ac0281462f897801dfc9e4a48d03df741 +310000 10eeededb4d54ad661d257fe3ee8b383d88ea175da4eca06cb0d2afd91d705aa 82535ab42a3a4494e51e508dcf04f233df0ee6869b14f12c2eeb3de7d0d9bfb0 +310500 5937e9322a0b8b1886bdd639f3fdea48392c3a1f3ce5c729f4300307b30a9858 463320d4b753e3deae3eaba1c7c0c81de49d883a7205d34efc6f933287bec340 +311000 106bcfeebd4f8d313bc06f691e30560fc6d5fd4e8eb2d3aa6e5e553d5d7d6769 efd1663f7e472207765e064537779ac8b52090326f6afcfbaccbcf0dd5c7cb49 +311500 81c0cd746d76874a8add01868bcc5465d7ff0a28ef2ba742b75c6c89986ca3cd 1e89778081b0b13e825bf16daf61ee16669826b50efc794cf871ea170399ca1f +312000 bdcec2a9a093b8c5e6a3f712c866bf78e720862b26918e7387d511de18af59e0 96fb2b8bdf01cf8b391a87158b880f38acdf15a6f761abe2cfbcbee83478566c +312500 a9716ae91f6c1577726c38cc4fddf8a39f1bac41cd1d9b5605e52184e573d6c5 4fae3413451a71a5f1e4ad71341035c582aa1aa721ded95eaabd734137ddfce8 +313000 cfa985aff09b9e7f22b17a600cbc428dc8e2dbb9c2b22420050ed8dd4597963a fc3335bb9389d77b4323758d39d7391d4896b94ad440a66b178b9366492fc79c +313500 959ad500e00153dcd9d8b6a8cf1190c799f69d3c5f6dfdff026a5d78ca5d0c5b 711b49060d7eee4f08a198fd3fbb3c611915c64cb12c8152210e9c4ffbbe9c0f +314000 ab6a7acb2629ccba949934209d7dc9cd895eacddcd7143dcd7eda19604ebe05c 3faeb9f09e3a75ed2247e4e6352c5744e091f5f44792ad20000721198a075c79 +314500 d9527af7b2e46e049c738a9d40f7720d8ac2e8e94b847cb8884d922ed004f543 03476229cf97cd0f2813169fab1ac07b9648a4dd86ea864feead69fa34dc498d +315000 626c5f923088c9093b00b846a1ee92657bae30a36370f679804743d1decf948b d51b5ca87ce3888f7299e3f2c0ab7f3e28afeef234f63337553c972f6e688be2 +315500 2446c74456b4b4b216b760d567abed176c12c2f474459d6be711490190904e07 23a8dd7d4d710ef6a23aada5e4fb960bcaff7873416c6b9904e1cf61c2abb0d9 +316000 32e55b2ff3cae917e3248bfdf7ebf79cdeb323cacb54348bfdd0b1f411c92186 ed4b4d67a3f55e5e3247f48d32ff231d24ef6970f8223e255c2870aa2ddfc92e +316500 634fd61f515e6ab677e142e73668fe41e462f25351383262b369e1a622bb9784 0f6cfcbdc1408b6bc3cf64c897fb67126affe7e82ac682707d90894d0c6768ed +317000 1a8b1bfb7cf59f0ea1d7141f2859a289b1cc0cc312836f5754121000a5a08dae 189e9b6ec139450bdf78eb6e37a63c60a13551e1b9d0ab737051cd5dc29dc445 +317500 d2d2e1c03ca763382245658d9e13d60db1c11199930c6edffa504cea704e9cb4 5ecf2ae923e1b0ad4bb52dda4d3c5b6ab31dae5a1152f40688992ac56cf7c1e7 +318000 14c481ed71e0265d9971041215ffa9d80bd9ae3d8a20bdd514b3cb15e6d820bb b83d25bc14861317c60d2a38c2cebc995004a3164e49dc4109d195ce719165a3 +318500 fb1bdbe68f6fa03555a52cc7c70086b585d1585bad7e7f36bd7849b20bd62d7f 56c93b2ec1e226511dc23b53e7c6992905a4c4875ee9146558712faf39024595 +319000 3d520676419671efd72eb59b19a912dd30419f5d50f9dcdc3faa15797e8b78ee f0cb1c494644102763b745ed186810fd2fd2443abbf4c8ef02dd42e20e344e35 +319500 e6f93385e77b36b8bd02e95a7be1daf93e43c2c15df61ad7a53cdf76581c93e7 8679904d6abde60c2f8b0628024a00f1357357852ea5e0085a8557cd55a3d5b5 +320000 edbd0041bf7c7fe4378c089724a0dc3d91bcff7efae1aa5a5e9b1d914d9f4255 83d2528f44d5537305cd76560b0fe1da24549b295e84a37ebc9f7e442f59a2d6 +320500 fe6af92a7429dc15ded665d91c0280feddee060e7a12cea78ab54f591a2a97c8 3840d83015f1c9053f2a74305ec9aaa4ab9c8fca66c62b71c898c0d8e40f94a4 +321000 09fbf5bf7f04579822b0643da9de72f9ffb4e3b086334876dd7ebd4a0b1e5456 3d01cbb877ed01843faf98fb687aedfdd63b6eef204d7a08e5177e93b6315237 +321500 79143aebef47a364850164688450127d503fb0b5fbbf6b3ff422a655973699da b65e2511ade0cf401a38d1089a5ee4a0747e003fdc2c29cd48e47b288d776801 +322000 03aa6946623b9252193d58e6dd00bf586ad4f57dc8fa04dee924b5c15d08d8ba 378b057486d0bcb43313d5206b5e08eeb06be740ceb84456f5cf8549e46157c6 +322500 cf8971a02d54a830b3729fb8f1d1c283135b1a7e30d5cfffb4462a2472467d66 233e6d4bb154d5d3fb1739a4e90a258f0910ceab33eaad183fd2108d8ed464d5 +323000 8d6c05ddfebc0476b7ea506c6edd8b1e7a1f04f46d3d44c404936fd8c8c97089 c8e2bb9126149c636285107b6b51a2ea4dfae6a9362ea82e9a01fc5b62317e13 +323500 f92d520784eb12d8ae3c11ad1803d5dd95a497c0084c88784702a5acce66ae41 81e2f522cc756b27794e1472df684a598aa925938ee87027d455dada0d2835e4 +324000 4d33cb6af695638f89500645f741981a2b96eb2b6f1361cf4915e9df14639412 0d5d58a71b6fb721127f551a4744091aaafc4740ce74e1e094783488946bf46b +324500 9cd5a93ac51080a0a3dfa6e7612adebfd307a7c2ea510d033368490c6d25b7be daf9a838f7b869ca77d3d1fbfa8631bd99aaf46967802148dc7e44fd261a530e +325000 ea1bd97d2e59f6bddba189466ffe1e7be63f5574f110d5b87ec3e0294b2bfe5e c4d8725f512f352599b2a7506b3c52e78012eded7fa5f6bfd99058046342b2a6 +325500 df8156f044118b84c3e2482e346c5639a19b2f1583944286cb9550931c3ca0f2 4310bc5bfec110e5da8fe1dc798b18d1b1453895c70554fb4d52dd7d6d7d813e +326000 d46ca9a4ec0e9126c9b570d00f7b47a005d51f25d9fcbf0c4ca71a29a5f255e6 426c81fa951ff0ceab76207fe576bad3be2279d802d5b95b45881c73827a1241 +326500 603728e060b7c7cfa603aebb548a3362364f3ebd83b07742458a412777ba9259 16fea467df6f746c0a430e00b12d568c7103829328a2bc9b57fcb55c74a66bb5 +327000 6913cd3da36dbaf5c6d407da68f46bcdabeb504269ea70c0cdc219590355796e 745d85006082600149a72c38c32e2d378917153b7c8dbade476f6ba32726c893 +327500 b6538b2705612808867fe5d1da0fb00b6257c3a4314d327b0c6403facc8c06fe bf9d4d5ad9f15c14615c36f8cc7ae29defe64971c5824f95aa535c1bba7083e9 +328000 ff013ae3b0bf04df355f954637c6ca2510479c51180977a99af805bcc8c2d63a fad93d54a8cd267fd2e63abf1da6b26f2f9bc1febac401d4731af0103b31fc6e +328500 ab64d04f26e2164824afa50c1b5592c172469531a20f35a38181993a087a5cfb 24d35a5eb7accd3545888e4c525742d90b01981447d1c2969ca744181e4d6b70 +329000 79384d9bb22811755a4bad2ca7d238243ad975b956019cca645144215ead0547 295fe151911572801c0d4e1ee4e753852edbf6078d7bd798dc5e2da10692e7a8 +329500 1839ba9cdb5db971f72defa707c006ef6ebeb93dfff2bf36ce873acd327531f9 a930915c8a7d58ab4774dffb87fa36a6f9b513b466bd73e5c96965f85ec3300c +330000 829b9c203a0c146c2794aa0ac22bc71033ddd15a68959064526afc2722febf94 e09093eed9d282fce0825778bd7d2c9415fa5b9adcfd2263e7137537d861d29b +330500 740e9f27889b06a301e355ef7dee62cf10dcda17d4023c157c55327420b308f3 a298a8255a0b30782c5d968988514e0437f4cb34a0a2f7df8475a18809007d1e +331000 e2a4cc3a53ac8a24ac52f6fc574d0e9fed0d240710de0694ed5656abfbcbec46 74e67ad885d1f874c4ae7453085f29d254c684ebc71eca3af50f3278c9e1c266 +331500 afdc42adb73a6717f48e35b3b51bb13dc6233645b8a4805723dd771062dfcbdd a3988534eac8b2c3f5d557d1277440783c624597b8b922df33b6fa38b824fc33 +332000 4812ec71190df534e5d8d4965492cabc3606e51c11e4f9181c470a779926dd7b 3d1fb3699e9f859472fd032915cab01de53232e123b1e4c93db2871f7c2aff6b +332500 50ae97dd398c25069522d5eb3b45a2332fd045f6be4d615c7d36c7ccf54c6c18 0bb46697d721417a2217fb4e632513d2ff82f34287bbdd732b9e2f893ac10df3 +333000 fdd8f58aece53dd4f24881f1007cbd0ed25d9f3c62f356a00dc92aa68a13539a e0ed76c2c22a2e784c8a689e1032cf00b65a2702f2b3f025c78e2d1e5190070c +333500 5ea2791919a4000f9e363e277b7522fd9ebc9c06ee5fd9dc15e4ca8984a2d8d2 69d3bcbe827a926dca144c589eaa175f16b6e523b487773b93fee7b45a5a60c1 +334000 cac1a3b80353133e863014d138b408202c77a5759cfc5642546416d73f66ed84 9005b02c03c93c41c6c78357f4323a3234bf12db1eaf36e81c1077a324f7a07e +334500 50861ba6d952e1f0fe4dac891ff4339ac5fb7094205767f2d6233941d2821c10 f77408c3140c620c63f8bedefb370397dab67077b4168c628d787d5b22419aa4 +335000 594b37798c188fab76d6d65c058115b58757893ec32173b45bfc618e8c857fa8 fe50dd76cb7dbe52cc6a9f6c25a3497dc5e4d141e59ce1bafed04367fb155b3f +335500 69b0bd40b74e7fd15be354b734717bf197f1800bec85120b26ca21ea5ac0255c 58cf057f1edd07227964239e7d220b4a6bae95793694841f7a06479c5e5f7420 +336000 71631092dff7d8d09d44bbf35c187c7cb5521c7a5eddb1422f790e52107b6797 e3bbb2e549a6c857855772ad5317902ce52231c464fa78a107811c8381629a49 +336500 ec3497d60fb7d85cfb349bd96712903aaf8a86e6d746f8187a54a1aa5b7b3d51 dc276fdf1858b7a7600213f9b06d4377dc53ef54e112efc864006d28c6cba3ff +337000 70b67fe1ffc39b65d7c3834439efd501fcb9e5d31202322a878be2cb78f741ff b91daa38e81b0b79bbd48c50f9d5ef92cd965e729b075e10b270e29effadcc3f +337500 c5df2f17757c5cf77fc421904662468bf003326049de85bec43003d97a5fc47d 29d4cbe540e3ac54271d12308edae379b59365521ee2f09a6585a82a046d7550 +338000 60ba652d7ad31c96015323efbe454694c5ec72dd732c8f4578ec2f5d072afbf2 2e3a1bbd3ea4e0f318830e43b20d53162ab6b6fbd2191524725965c549360550 +338500 c898aa2617812d17eaa3f8ee142c3018fe2807862ba6d06f309b3344e7d799ad 6bdee06900bdd6c83f8fd8538d75af113af8893ff99cf9a25de6842d3f0d9889 +339000 e0d97756042e78b1c14495a68291e5033c39c56f37a48adaccaf350b12f59202 913f62e783258b88638a7c42b2f403328b683f8c657520ab723ce2196af69666 +339500 1dbe406aeef28cf78b7d43eed5fcdd8c9d7d6d11363cb7e595d7bc1d1899140c 8cee4f37f98fc3da1826dbb96aa525a49fb1fa6db5b2bd64dd3bbd4fb7d6c238 +340000 99ed171ddc0aafa8a5b6f9e5890822c34f6ddf4bf63f1488afde10526c74c8b2 ffe05de29a9901370e815a782c7478c3075f8444ff473b20ff5a2ab311a63022 +340500 92da105d515c1952ffbdcb5f5b99acaa565500d8f4b6e45aa093b61ae53fb7e0 852c42d37d42f8d2c740e57b8e9e769e72445f57c8fc9d1f91e62f029d7d985d +341000 da401dfe258750911b7264ccee0de1b53bd7b90f63d73f46ad5d5914203b59f6 84e7b57cabd661c1cfb68407595a6a9607e0dcaf68f74dce2f8487b1f43a6ac4 +341500 ada69a2ac0977672d08682d195934176caaa7de0baae2f3a32b1391ebf550eb0 cc1d077aae6a1c18f60c874525182f8cc211cfd0a851381773cd0166c108a43c +342000 ff83fa02f1e15e52a1e6ab3f8690bd9c4bcc7f3adf77dda003c6871283b695ac 5983b9dee33342727befc15d5982f9ce987ccee38fef71c34c482c92e062308b +342500 cfcf80f28aefa27c6f09f71080e4af7bfb33f94d1c47e80633187f80a806c6cc 08f62f928db36fa7757f30edd40f93c20dc43c65c693ca5de4c87467d40377a1 +343000 55e1b51f72f048231b675c90224571930a59146003b4ddd3e883bcbe4c8c2685 1f6c53f636480ebaac6569f100e804de9a604dc1bca0a626004ba9852f2fb546 +343500 3d1f67b0a5d905f15f3fe5a7a07aba456ce3ce41422a3bd94f92fd19ce7960e4 4201088ace85294f87bf646209d2e9cea09ac2587654d57c8105b016ed3adac4 +344000 1e718e4e82839b50bb5409297dc94bd783264f1bd3da639f2e4b5444c6765025 c2e0bdb2e293d2940f99295b59d2fd9b57a02632605bc66e4ed69de4e26c4afa +344500 aee9558284286ee6de93a1456fd3cf6c81ff542b4c1eac2c7694b869742572e4 56b70751d01bc1925f288a0af1223f309b7bac54e1cde463ccc9667ea50c9ddf +345000 852cae52d99ea4f4f299f1b5f4a7629ab0d32f8693f503cf74a1f0a31a9a80e2 a627a26363ae216baf1774585d1159977d378f34b54119670620bb82cc9784d1 +345500 09a3e1f7d56b1662f10470c2497e662495b62065364f40dfefdf8d34f5939fce e4d96a1576f516b5303b60dcdded21cb4e3f382f4e7c4b10399ea4b6fc37670d +346000 3dc0fc157c00c40ca92c8125e818f8b5c6163761b0c6955a88eb48498b1d6a45 6f60e0950cb6f6ad48e5b56e9e94a680ac85109ea90ff94df6ccb58adc44d3a2 +346500 bd5552ed2748d686ed3540c52ec7a0f97b839254c953074357fc019667d69188 d1707671c71c75edd986ed69fa0d8462f1d7aaea3e0b5cf519972cd3ce7d73a2 +347000 68b383fb101b4ad08a4ab58777b9f3e34c69143bc3872785c281b155de8769f1 0105724a536021bd7c8fbf2ca7e433c247009c97aa48a8688dc7c9af88018688 +347500 af3af4870dfdc980d2bc5c946e773806957c2f5d98200d5e9ace72e4301d0ef9 20b564b903b75c62b5c7bdb911c753fa67a547a47d47e5a60a0a4790362adb49 +348000 e72024ff3d0ee72f4f78b7e63ade7a7ea037da43bfa9deab400f6739fde391c5 c0ed8b08b6b7d50bf86069b1e8f6c7c36435817114e27745be63db96b5f716bb +348500 1d9d9ad6a6b1a8d5392aa6d35a7472fbf80535ee334a043025cc3637d9894db9 101e9ff2c05137c25a2a270055264bfdcc81b290826d968ca94de4038f791790 +349000 bf0cadc56a8359bb703738a06f7a31b305fe7433989e7e4094f379511e27a19e 2a2cfebba2b09215327049cba461b4c56cf54cf31a0d3c6f8b21a637e6a4db5a +349500 78065cd5e2af5a3a4e838289be5d1460605048c1cf2fd1ef9ba8c88619631e91 aac94ac665bc7ba954323a07216ac1669f19bcae1541459f41534d8058dfd3b5 +350000 937db7a154c0d4c5043fe8bdaa0969670f132252a6c6bf8f7ed5858ee11c97de 8c276300614c430a75552e0cca7d83834ce7bccd7ea1d592012300ac2bc5bc66 +350500 6ebacc5b6fe9ff0e34d64a047981fd170f1b13d40139073e6e65da9a617fbd18 f181abacd6038099f96854c2c0ac8f7d19504609f36dd0aa085af150f21ef930 +351000 156a0053ebf2ee2e68a7ef337edc14044503653188c60669ec924fde43c21ea5 dd728a51d7f9e8f04e74e2116698305470591a6e7937b2c4b714193843eeb584 +351500 5cea6cb5b1b96a2641bcd705ae86c40ce7a8572ff4d6815d7531ebdbca13b958 fd4148c5b36775f35873746e962941ed43d95db28ef21734f4351f1d8a69d012 +352000 43dc3dcb74fcab2f5e90992eedd8a7719f7b7c7658b2086583ef881092f88aa1 eac1623de8acd8fd497f9da85f85614f5cd02c69aac6044f24a3685957009f58 +352500 6b895134f53c26c0b24e9909472962abb3dba5f8b8ab65693ff8e45b3b77e566 aa1abff115fc49dbd6847ba4779ee7c514dfdbe3895e7bc3105b74d0fcf1f4e4 +353000 7785e099822d5b6bac5c707ea57d5d2cabeb52e1e2d82619c8bc2051b486767c afcd99c877d144c3253dacf018ca36e61e95761e44f993e0a4cee0854a7030c4 +353500 c0790882442b7ad61aec5a64faa355a8486f8112f9a691208f38e9bc96230492 f7888632b88cc7800d381774153c34626ff63d859ce489286d9fe904403e9ec0 +354000 de0551272a32e89def017385ba1833443c4e2ec02196c33739deb8bac6cdaf25 78038315c2d8c142ae67a01dfb98009847380ad96cbf6cf700af8693f6260caa +354500 c2e4b7243560cd6e4511d175e2c3fe98df223524124f44faabfc252f57d503d7 1d0fc9a93c812ad30f81462a5ce1fb62d74f14a1672e69dee3a3b797fde53270 +355000 1a11051121727fbe8bf80391ee403566e3b0dce4ac611b37a516c7e3432aa231 f363b96bad7663c981865cd98aa36aa2cadf8b4a623a030e3c7e8a58dea81992 +355500 c877ee5b39b2a7ea9723e1c90b2e307f861e42275c826ecf5b6c84f97c120385 d7cc21f18f45fcba316afb516f70ae028723a709a1a0ad2994579078aec10ddd +356000 4226d4f7d74316c4f9baf33868b492a72f2a2deabe02b5568ef40606641862be a364b4408c45633b64ca685f42f4be377a4b730fd762f28e0015e2ae6e23b0e8 +356500 f0014d06574914b9215379d2b187dcca46081a5fa27d6a32a38efd382a9e945f 853c10ad51f992a4bf0c46c8471046641edaa972c537c8a92b32cc79ff80b445 +357000 1c78c05a5a6c50b6d0372745b8a3f0ccbd6c4878e5fe2227c81bf36cbe31fede 118150dac00bdc45c95b36d35a57c63379205cabc42f8da5d8cac3f8198df0aa +357500 44900087fce8f2c01a87a2ab15befccc1d9b9e702b17c0184cda90876a07fc86 621888deca43a1398ce69ef2cb1ea7523e563dd552e173c3af45eb3f00667df0 +358000 40d642581118288429f0b72a73930470989ff443944b7215edbddf7ad8596d31 819138d682f96c5be33a73fb51c7b698f347c76086d468a3cec421b5ded9bdef +358500 39f707f88dda6ce8bc39c64bdad9d1fe29f98b517080ed55466a8a221ee19bcc 30d012c4929eb3131b7f50c34986d7e1337b46ce6ae7065a0613ba6ad336d2dc +359000 8a04b19b8f068b14a987511d635cdc70ea66e37b63a5c3750bbf9bb2881439ce 7d053e5b5d919b21a1ca21dcb5d31126f519c76f8c0d2816555816496229c970 +359500 15293eeb7f425f8f041248d3cb15cbedf19eb921389a4425b8a14a30d9f6f08a 5b47bc2a1eaaf8b83461d28c535cb1b6b376826c9b554df7498216cbfbd5e76b +360000 37577d18f84202e74e17eb9fd38e760dd94096b4623917c8fbe4eb8c1fe727b8 985b64f248199de5acb5f6dd54aadfe6b2eec0580ac17701e1a6590b46830cab +360500 2db2b88ab382de78f62fb710454fbe0a9e07dc7c09ba76ef26bf72e4768b8cfd a1f4e7d6f52f1fcc610793c0822a6c9a40e0224deb7e56495d34ead537fe2fb5 +361000 3891670905fc1e7ca588a4b72a30c57749cb7c9c2ed47907ed3a69c7339caef6 164eecf5a84889a1ce42accb7ffcae55c4bf598b13720c34eb403fc849dc5801 +361500 ebe6b83861d092f8eff3579515524a45568c743e7df656feb775c45981251891 ba453dda58aa37da9c4babea1597815a99cc4bfc9aec16fc3f8ba1d85a031574 +362000 8524397d4612f5acd6fe921ca9de4e9338e2f74221d4d1755c3682fdbb2bdada d9de4ba457b0d1ae37a2dcb9c8d921acf206e96673d45346ff85de620fc08334 +362500 244d8c96246dd1772efa9b6dedf90ed9933f9a1fef43a5a61fb9d59e9e19645d fbd24f0a05dad2f8048794e8f0c48aef1d9419e28e125ead88eb1127bef72ed8 +363000 2d0854b709d2c4fd84c1d66a736c4148a3b1001fb083f1acb38aa34416063487 eaaf97d25a09d024b71fb1dbe1935a8cf34bf0d30736df8e9a7b6fcd4fcd63c7 +363500 eb76872dee58ba1a3285a93cf5b257a12a903f9f628828bde4dd320ce44ea11e 2145d5e2136393a6d205af57d6544782db4c701b6a5b3f14a90528f30cded69b +364000 043d7b87efe77f8f2d34833a08544e15838bd8b8e1bcd902d8b1ba9dc30285f9 71ad76919b65882a929954ac642c401dd8f298faebf7208aa01301815df4f32a +364500 10b77b07108cd3c1b5166e51eae74df84f39313508e8093af4fb5a5c8db99c8f 9738d20593ba72b8e0f80a8c960f343846b639d3b5f3ac623ab7d7d0258e8c61 +365000 a88f0052b9b490639e238ed8d2bdae3ac573c598eebc036fecfbe8d0d651cb62 806193a3ff83178e6847129340bc66f087903dc85e8d45d50a3843511625fe4d +365500 07674af4f751c8a09011204107b77df887eb0989580ab710bc28f86e1724b972 a59376d69d1fd2b3601728c2eb2f3f935177f5abec94f9b441a6734b8c9dd57a +366000 e7f0e9b3671c1fd8ac289879d42ff60cb2decb8c9fb8095152e444d183d33d55 f04e0f59196d771ea62c59ac3882474ecd053519d37899e2ce9bb2d1a779b3d1 +366500 28fe13630301f2809e429288503e7a341a536de22c078ba11f5056400837d18d a3a2aa196a595a7effa0e8a3ddf8197fa3837c1a55a392f456bbe2745442d844 +367000 066c5da3bfb95638c9b0c37cf99fe28f31cc4852082933305b1561f8b289ebe2 4d69dc379574aebfb8c507bc6791adee838a066e7de10e2e5d633faa6aaf7927 +367500 ee383e75691d5d76bd0d84fb2edb77a9f3e453fe14e3a0840524d4966eb3bfc7 addae0433c1a0e30adaa6bc21dc08c4c37c7ad6d12109458cc8b9c3deb4fa80c +368000 7718d30ae93a53584971a1aae7c71b99f17332702adfa3ba93ac53faa3131bb3 00d48572249cc7eb9929065f4d8e5a4268b69c425ab521e974066c7a15ba96eb +368500 49160ed17423768caebf15a07b978b51b9a50f88cb934cfcb2ca335db599b313 252c55080a47e986faa7b8e8321239a25b25d5109f3b60cdd66c2d7086c32bed +369000 2a35afabeb4c08f7e0fc8564b016b65792e64b6beba78e5d7c0641af63530170 08264c57b5533fcc49b365920cb2d8b698be6d412ee0693e4d78d847530eecc2 +369500 003a85d64df7bbbe1692464040b072934789d092d6f866338b0e9427e823a83e 7084540c646571d0af1e383b6753e92aec196cf20c88bd913e074ff05dd63502 +370000 434d40143a0e8d8fe2e00984effc5579bd3661fdf33dbd93cca7fd72351dfafc bef78b640aacd4fd8598056dd42dfd6557355ba44f18fa9aba104eba4bad9738 +370500 02cf8d2c1db6faf3e7755a2b3d144a4936dcd9f34214f1c8aa826465cc3918e5 24b38a6971f48836c71e37abbeeff129d21fe5975302103ec2af62a337c33afd +371000 2534f9c4ca46fe2df863db301dea97a3bfc6e5a3645f4080d221e9fc214efc8b 24faf019a5e3c38e1e9c941869b93a6c5bb2b2e3e3c754c37d83bfe009f78c20 +371500 1c3ab21bf75be98963553462f21fc369e7503fb8699d9f6e1f635a952c55db42 8dba0c68d6c580055ce406b81e3262b9c7057ba8cbb8b221130766e667c878a9 +372000 b7d862d978a007b9e4afa1f2f0650bb20c0298b8bbce1666d58b009c07014bed f070560c4db4d96f47694e25dc96746634b3b1f172ae1efc8435b29ab083697f +372500 aa2c34e6c03803e0edee9675926dba87db1bed0c89e605dc40c2213a330e53cf dbad4244855f790740309f5f7e49755213bec4d672cef6491758cba12b5571e6 +373000 c03556ea141010a523c44868eca127684b01e83f0a76090906728aa34e599931 b2d61cc71ab0b18171db2117a6d535128c25e68031c3c44c059c7c7965d05c53 +373500 b61ce82c38d812c1e72a830b062bd3854701409fed42cb8cf89d9c83a68aa4a2 3cb1ca2e543310ff8d22cba4bfa2973e264837d2a4a5052cfccde4de4a4a477b +374000 0a2b671cf0c4e82c857482fd9eae13f8212fabc508c3f6137fd3004038d2fb7f fe35de23fa234b6dae59ea82cfeac3259eef5f8d8878aece906a9e71121ed71a +374500 31b2c402de5e4f0911f5d14b927f38483fec9234b0ef67efa026a3648ffd12a0 63c1e30437b3fd68e0529230da7123339e9b8d2079576d68221897e443be0e3e +375000 1673933d5b2d1584e3c923b45bacbae604b308007ff19e25999f5a1d89afb040 cdf2c192e290686651e7f63bbadc54d0d8f7b1d2f7208a097807de32159451c2 +375500 6b35b7e04b2d788de6e2cc8909ab7fc5470b385905dc0e7b7b8524ff57d7d7d3 ce795b401b8cc3db0def999633c2835436348b032919c9123e1481800aaac88e +376000 9a2ec84dae6c1b6dd358c57205e0858d987b68bc1e029cae1b8239ee5ac181c0 d576e0d718d3f594ef61eb7be25e4083d1ead1201fe8eaabad00c63938144f85 +376500 bca044a02b4bd78ddd32eb668c603f16f14daaf04cf3541d019d3421868a14d6 9abdb75f5d7bccf402e2e02a07b5e6d5205822c4ff20ff5531d29d4a8cae5d4b +377000 1b4b732e1f4b18b744aa40196b7bc5d3e1074fee1beb4efc674e38f16deea7e9 04755ac05c88590c66be5b72ea85735df901ee644ea1f35772b82bf18c2eed13 +377500 a51b1aadb6f280e7c23ad430b8c59c8067f0b8dc10a0be61666d1da71bcfadda d8eb0789d6088438ddd828d02844a644a2102dcda7614b262daf994437be74e1 +378000 82f6ad88db9e44f44c837c7a117802a6bc4379ba9141c4faeb9e7731bec60f35 8c48f7630228d3c0322986552aa96929eba1314f8c62baeb801b95cd3cf400f1 +378500 c2fbdc981504762cd306473faab285e108b27bff83a84a1460bf513839d9ee93 37a18476810b603df1cb7efbad84e3b49a24676073397977eda35fb559e7dca7 +379000 5866ea5d40e02e5c73a1828d3c3bc5ffe23f9702292ba595560908f66b23366b ffd0dffc4b8703364b9885055b427f2bea2336714ad4c5fe567e1efbc8fb7b62 +379500 2519dfa5eccc5f59b59c89bf35ecf2aa0f5f8b43ccb098622def88dda5177b36 56df546c0e7bc87f0ca01ee62f0d0939810749cd5fe68fa4848f6f302052ccc0 +380000 7bfff4292f5f17bc71f6763e2257fa585c6d56470390648079609d8116988255 e5c4c441e3a8f0b5f55bb8f5ad18a9bf4bf04719a558e2b954109e5c47cb9896 +380500 386869e1fc8e3f7d6aa8bf28caac3d5a344330e974a42704885aea3cb459e179 279bf20f5fc0c66f97256a42cecb4597dfbee3eda52cc03c01239d2be7ce0e0f +381000 86cf8da3f13d65311d707cbdc932a563a44fc12e1c43de337583bc0d113a5a03 733b60f69120d3ba845a388871c75cec4b4e2479ffe31d83c0bfe980133460a0 +381500 3ddcf06f6b302ddea992508dc99ce57c1a7922d05a89decd38668c974c0d3d41 145aa998b1e691947f9e9c1f9d7bce235dd64fb3101664f2c310b59479a49954 +382000 5cbf96c1818addd8739c3b0a928605824a71eeae2cff1f520e644096662c6d95 0282ec673961b684b9c2c43f17dc47d7a1c4128a19b4dde559103986abf4ca50 +382500 01447f3299aee987ac9c9294682b89f9643f28bf18123ce8114b3ffdb6a03c5e 8a0e1f6c67098b89b81bb0769c520cb50a87299fff5497a87486417b43efb9f7 +383000 d5efe4b385b101b754b2b30f6f99c2bf3736e9fd0e5a06d0b5ed3064e7641852 245a1712512d17bf19156fd5288ebf706e85ba49278ed03c2d506364e7002e08 +383500 5b88888d1abb320e7d295c00a15bd74041ab35850fa768a8786d2e0cfff0948e adbbab8c600e751c331a054924b778213bef9b5a5e23f3d9ca8c9e3b877d8b30 +384000 def1f25883b2060a7b02f078b184a1fb50ebc9b9ee8190971a6315386382328a 31cf28e8a9ff6242a0e4c9b2ba30c018acdf9279b8326185516851a41a15535f +384500 a08467964a472ee61fb3ed6b622acbcec35b4903251cb99324f14f2b9bba7545 720e5881cab3613d6baeb94449d6695102a1e508f1b456ab03d2fdcc32486ebc +385000 d460b85b1ac270fad347a02c19917b9f38ce22c01d8b88e90d0baf63f011b117 b207a874a45789d7b09e0948eafcef8b76c9184aa370a508a4bafdf9d26ebe29 +385500 abe63c027a3d085071de60832600c63e46d78d244315facbee6f0f3fdbb02912 7d911e4630c29d4daf24ef749a17d8b7276c4c9c3f6194c6b0176b4b5f9271c9 +386000 656989d4c67537591081d194c31766bbd9fe449159aa2d3037afa3fab0f74a82 bb24425f9ada80398f6b1e4505b1f6eb372966389f0805b22f2e2d8607aaa38e +386500 2c055ea6793d46b622e43db2f1d25e05558f77edab26f4b1b200bd5aba514747 17499e0c656152df6a2d6096c5dbc50eadc4f19ed4919a04aa3a5c06668f3acf +387000 b25736d7898a4555b0e257ee1e4cc0a96c1275378dcdc0056234846010c29101 15a65bf7b60e7729a9c9a12883b882eeae58af6b07aa9e1e4b7eab5775de2154 +387500 623ea15901831a8ece8d44b5da072e281b237a693fc3524b523f658fc4ab621c adcaaf4a4184c1719060e828a12933ceaf54184d50afa75118d16df5a67aa578 +388000 91bbcc4c97dbbca8f88c68abad1a68e91c79f337a0bb7e14180bc2ce92eac137 21bc2be4b2c59b991ac29c2266fc1cafdfe59da48e957f3105f8d45fe2284c41 +388500 8b9552c05fef1a291df8a1b47d92229429ba2aa1c3453c274bdbf8d3c7c4fc92 f146203b047b7ecb750434bc317aaa95adc82c1fad15f35b22b879325ba38958 +389000 8e938a51863daea6ed977f9068092e5764c7dc9bb2491ac10291921725be7ef6 ccf2ce60dd83f9dfae647c30e824864a5c208ed5c8837420f30cbbc7caa3638d +389500 7505772f28c062e4751b5653080ccdf943181e5f0419069bc20ee6255c2cdd2d a4080018717b150e0384c0cae9a6500dac7779a57565cad365483cf79ea99a4a +390000 6f4b50c04bbd5aed6b57f287a0e6d90a11ea1571dd3635ca9b6ca6ef541cbdd8 b408a6d6415251b4b6ab6db5ae815ee07d0dfc8a8e9ac22c44470916d7d57ef9 +390500 b8caebe933d7dc40b9648b530bc042f1f93f4a1bf6e1530164682a1397dc1aef 192645f2fce9e3b4a7398c935db6ecbe5b79d25d8557fc5ee13d1a5c95fe790f +391000 0e6e6686e469e5fe43fe99fdfb377dc48f85d7892b9aa70e1ca46fcba1fbc940 face758288797482e69de640daa24de10d7a35e17e3b575fe147d73666fd0b75 +391500 fd1e752770b8e555108d7e32c8f6c7e1f9b8dfcb20502babe4cd541910a03b68 89601e3e196aa20964f326b166a74f7a601174815ac89bd1c385a464cee7a54d +392000 529453ab5db3938d1195a3ef84c286e8103c42b7b1b25362898674a66a48ed5e 892462e851252b9a2ee03c507d1acae80ea67d360514a4c5770d7ce0dd3e1f54 +392500 e3a3fd9781e171e256dbee0afe8373a2be01930182de5bcdf501db3a73a70952 945a626198abde6c92ee9304cec95937cb148057692f8a6c5080e0ae930be61d +393000 01db0c4add701bc81282082aacd1fa1ed032967c1ce44c88a46f22f09a00af6f 7b2b867b0ac1dc90aa91e0b88722f474b7d78a3322ba125ce36d0ac755b2c3f9 +393500 c045647644b0d7daac826a34e5f7cdb1f55a4cb4cb5500c16ba4791a8f46433b 8985e0f86f19a7a72fbe581cab68588d1882b9a4b1c236836aa1e559612692ca +394000 7336f022ef425203978336336066daf7f85850fd1ca5ce2be57119543211b7d6 6f1a7d270bd2cd756c02d24aed1e33fb78042efa052bc7e710cf37a60110bffb +394500 2791b8c33b59179365cb1f1e97a71a0ed8c4f9dcf98105ca35275dbfde1b8213 8b5c24c27b2fe79353caa4f2d7548591db269b31c1c53b5e7664a47ff1646a33 +395000 b68a97351f28549882241171a79cb58a1ae92d93a30e7524f3580dfa69bd70b1 a2d0161bea0620be17038a96a9c21a7c5ef08df89362f1e82ce8de29187735bd +395500 f674116f1af73caf4c6d464f99d55c1152f7529c15b0d28a12f35abc1553f91e 32fd584340a63818498504c5fabc21fdf3feb2a4529a6b5af5d9ad67f3679b20 +396000 ded515c3d960dfe073403b321aae9d66a1dbe4a9565caed08c9314e8f1b1d2df 134fcfe75781458a3899ad3330c143de491b74c09c98b8e95c16f79cac282828 +396500 b6745b3e2a46f458eca58083625f3eeab22ca3256087a45aa6457cdbe9999aa6 4124109f22fdfc42c1343feb20ed8a93ba55cd7f65ac34146c3f5caeee272d94 +397000 c79ef7c45f179c76254995bf3c810a62636c40d7283658c3d35f167f9b4289f7 a6fe1a5721abe2a12fce95bc5f5e12a4185596ce991d6af28ad2cb2972305637 +397500 d36dcc0090a5585951004c7f9dd645a5a2daff1d97b796f2dba60c78a89d5a14 a89cd3b2d93dcf17cdcb0f7badfe7af1077f916d6ad5329adfc38a6e1d25cb5c +398000 2b97743e109f78d910532ee0f342b1c491ae74badc58085ba2a7b9029f9fab5f 7ab0985407419f861b725834b5643d2666cbf5aaef6aa6eb94dc9e35c0f630cc +398500 4df2d83b00fd22751d5fc8b22743c9831701408cb9b788d86bf081c666e389fc 9c545b4c3cac0e1d1f9fecd92ba0f7c61ac3e8a28a8c973ff6026fdcd90c4a43 +399000 6a00c77818429d3ed4b9bd5211a3cca69cf68c9a99a89f9e1f9e5301c08afb03 44e27a5f4e4c32f6698257f5cd63ecf165cf4941a94f7473f3995b1a3a6bfd82 +399500 a6e49cf1def602b4915fbc2b208490f5a84c8a93366582f77ecf5575119e6cab 5100ff78853bcfc8606b12c5c335c772964323d7486d55af0fe315c05715875f +400000 641c6a53eaa3c9b94e924327e70830770397343d7927eb713609f14448ea6228 4252f178c0c42e28cce9ef5c0c40f08bc03e3b631a393de6eeff90e7b4d26d38 +400500 73e034517c9649420d36a641130bf16b8634fd3b2568e58198508e65def1fa37 a13f2b06f4b1b3daff6c10571f25318c3ec8e9f2d42c59f4f19369bcbff62da4 +401000 b249ae5082bc1eb852ce420a2361be80dd17228fd17c709761d0f52e81e8478f 679e41c907f1e82c438671466ce285437ba23fa2c845d8c07b43af0829692b69 +401500 ae40fda2bd776861208459ccc6f1d205b769de518dd00aec84f87811e006098c 2cb9da6c40cfc83129da322498da46fbb48f7509d66d7e5ad53b6ccf2723ab83 +402000 53a37e92887f0f1305b937538e060a4626675b361a28d8ac3cd88ce7c16adede d8bd6aadcc2b327c5d2ebe73f48ff2df81310be0b9c194cde8fa5aca004874d9 +402500 54008c3fb34d324191f2e8790f68e097dab91c9933bb83566e2039abd876818a cd1605e18479c4a48bc76ea463608f2c2475d5b3d603bf9ba7555194f6a77d73 +403000 7ccb2c59698108c6ada2be19fe72a1be124c9650abbad735cdcd1b07fb5888c8 208d179f6a09f352c3ce8c445468340beac166c28398132d642afa58cbb06fbd +403500 4307fac30841a99cdb606d9828cd6a920da0155afbea4e5ec770d2e82da4a850 b6304f7d096028bb882b39de8ab14f625d2ae63e4c597d34fddbaed7c2b9f526 +404000 b7845240f6d8ee0cc32e37d8682c587dbc52d07eef8c1b1fa38f73428b73afa4 326e6eaf313513e4a52cfbdbb0106538065a4ed8fdf3d0e7eb6e00ba46037af1 +404500 73ba4ce92e212342e14471f4d85581e85c402a55671db59282110671ede03341 b2af723149653ceffb01cae05de8860e28208128f215e3bd8bc65c7f2d03de41 +405000 3b3d4f18a6a63daf34505404d27dd936531654047b1dc9d1b1f864c33f7ed996 b6287250e883b8c416c65541fbd8cdb01e7d76f989f3fe3e2c31b762d9c3269a +405500 74fe48ee491e1f7e881a9a5918f1848dbd129b08720f0c9f2dac17dd413454f4 d57fd78c8080fb6739f260c4117e08c2214bd1afa6c4244bc16fdfd04804fa76 +406000 81c002e0fe1d6089bc83dc43cd9d616138fb16db20f8ff9dcb039a2bf54bdbd8 7e37f0088205c718348f6f13211a98b16ad8808ed51c824050c1c3793562197c +406500 20aa6245826270b507ea3aa44b5360d608e2d52b20109dc81a63181f9727dcfc 2d722a96f3605f68e844eaa0d3629408e9e20e8d5d095d33c435ea2427420a9a +407000 51c75ef8115644c138fc20ce9af7b771731a1efd11231c881096fdaf59591660 11b150fec2d433d57eee04489446c967a692ccdd64fb6c90f95b150f6d5ccaa9 +407500 6f24e1c00195775eda44acca48e654190b2a9cb22f2dfca41410ce90a067af16 8b49f54e99a2096f0ab8dcf8f22fe21db789b9bad29e43edd3566f5eb7045b5b +408000 ce3c90abbc8da7ea5c5df3c25ad6fd3a7fda496b1e0f696195bda3e6dbe7919f a5a21a4cbf89271969f43d9c808706c6cdb624daf4e5250d6690d40bfef2e78f +408500 ff9b888f601e151a141d92abe7007d40203e6cdbf107740c78a4c1c715608626 3a6e13fefffab34b8bc11418f83163112c871b7483b6cebaac9f9397b5894976 +409000 7f3914a988816ad71eeabeaa91e918ab92df56753de139ce9170256ea433c610 39c9ca62d51e871cec9c9724f12841ded9e81d1b684ba9bbbd118c4d79675e3d +409500 e3fa8435841942ec88357b1e3e0c33702412a83b07c68589319064dfdffaaf81 beda309cb0d0295f165e7ff4168eeb205c921f202c3c9524960152b7ddfa00d4 +410000 3ae6fd96e35b4a84bd063aaf72cb8ce5eda797e213dfa856008516a88039a94d 58e69a89912f506e6f4ed9e89229f9d81113cdaeb5bbd120ca273136117dc82c +410500 eb2bc9bd04d8c2c4ccfc4096116e390bd407e98f0d81b2a7a1f096ce0e86336b 3a87b4d2e3e1c6c5171edf685bfbd0694f293987197b33680dfd338e3ecb4b98 +411000 a57f32043fdbc1b8eabab9e66fdbb7f40066ea9e92d3b1055e7867faa4b73187 afaf24d67374beb37bf9b1c1f0e231ba901b91abae33f81367d55bdf78e1cf2f +411500 ea48060ae79a850a73fb9da920ff9d38245420ff20fd71ee09f5e1411c53f60d 248238d9ddd8abfa0b502311f3e2b21f3e5c5b75719915069a209beb7a1be710 +412000 aa2691852758d5a285217fe3a9bd1c2d29a6d337a0e350abd60809cecfe3207a 5a3ac34a9dfcdc2b17eb45244fccf0fb4e9a843dec6c6f76355c1a81b16f90f0 +412500 5a0807ca910611b9dc9992060c41491071f17f9b4f71b8934e8fba9cbdf95a69 a904ce16e3399f6e8eca3201ccc7fe5b15344c4bf306f1e63da4ab019e0d9582 +413000 6b93d17d702eef90b2795ba888ba49dd4d0d1545784e5d508bab8f3f3700be79 20028cf3dfdf9af6de78fd6ec4f174f46a617f2d3204ba22c3b487d3cc135be5 +413500 1bf2cd60f8c697785716c23db0c3588c42528d3acd6a8427311ad176e9bd3d71 b6e7e1e76a68c20f6909cdcab53f6e820bfbe896cf25a7e435a52ec6c2c3bb85 +414000 ca3778a71094657cab8c697bb6d36930a875d3c76fba72194abcd69fa5c17703 02c6d73c2091a860215de5aa0741c1396c9fb5334dad2786f1f483add87f1ce0 +414500 97f6a6e7e49a245ff3e2acf4c2882ec0173994a21c619f7a384adb93804bfea7 79845ca1a36820aada03e8c46de2dd19dacd64131d91ea5c102bb90df653cf6e +415000 e68357575afa23d47a2a7fd4077e14d22e51b786c8866042263c42ce2f6fdda4 4cf1d285f03ccf0ef31333c8b6ba0142b54791b826f29ed619266270c725b070 +415500 f975006a0b269a3c5e89fea165ec1821ab21dadd0e95d2f372428339a09b9476 a2e3b1b7b2cedaaf531168cf85aea230e8a27131f3643f15269cd8b7fb55387d +416000 27ac3cca7b525e4bced3bf89072f6df21ab97f85317dd242d9698a9f695b3d29 65b7175c50d753059f0955541a646bb523b47f1fa35f5d3d86ffec0077e5e6ec +416500 c3947b6b74b100c5bfd70c7cf1baf822871046f3ebddf211f20ac0ad00660851 ee1d3925d350a9da5ee86bd63c792406dc6a1c70fc0d286dda655687a0b83b57 +417000 cdd42386e134f3cbe56a5b6ff14b08dc50a1a02b70278f20a6982517868c7613 2f7eb3c08a36c3d1ffd90dafc4ce917e3b44fe62e8f545e581af01a2fec59891 +417500 758c5ed9fa53f6a940eb15078bfc3300f68a45174ad55b6d754de04344db5dc6 9d180ee51efbd082e027e34d85c9002b72233e99014924f3b56955f3dc2ba17e +418000 de38ce06e6f7c0509fb958124422fc1e0711c318d7fd1a0fbadae47352ec0eeb 128c492324c57e30449629f7a9d475ccff48d077ccb61dcc5b7f91d013d89337 +418500 52335906771b9b38787ad4381361b609d5eda9864ca2a7d2d22125faecbbf76e 192a8939e510e4b270a3cdcaf4d5ae7379dd0a20e4addae21c2115d0bcad0d18 +419000 d7718cb6a671d3e323e7f8c85b013f8f46dd89353a6f77672806cbddd9887379 ccdffea8a7310aadb5f103ae277ad075b9daa876c08f3bc4e13ca1fe85a400c2 +419500 311eb00a753d20b8c4c073eca48fbf44066e4c97dead7b0224ea65d24dae43e2 5d4d46ec84c96ba390f8d1ab5cd82fad5d412133341c4ec204f36a47462a1633 +420000 02abe9181281799d247884c1887745c6aa80f60a1ceb12693d328015d28b5a84 c5713fdc696198d00a681a1602a8703b724c13362bf059dee081747db8c31908 +420500 f24e5019cfcf1f0612785511437fb2eb94b0a44ae6207ff537b07113a6deaa5a 4c49208f8e5513b0dd1940c861175e2bf7bba04d6373e734cdac63237fc24f1a +421000 0ebae57f2319ea062c101095eb8c2cde71bb38b0df36fccd23fe6d5e40f968bc 7c50e4dcbe642218c07f3bc8df56974ea197f1107b9b523cc8d984f1903c944c +421500 3a65fa88661cc79145ba391c18d2eb928842a9ee2642c757b3731ef399323e13 2dee8bda6a09ea7ab0349f743da33dee3614c069af8977377a94bd87d072f131 +422000 030b699998f98d4b331ef746f96eda057e7a454f98765a0b695d16fd3e7aa44f 9b2c25f8a003bba7778aa66ea405ca0a7d1b4d1a44ab35c28124d0d147a731cd +422500 44dd126ceb8b88b2d3d942246ec2ff298ff07890fc75156510de8dfa22c121a6 fa987f8cf06191b45925bee07fa035ab558e651c1b94d1d518524db426449e5d +423000 93bc08b4a0ce5ef57f63ee483936125540dffca3d9d6aa1a24ca2f69df2e2100 a02f084ecd89f843bf976fcc5f5c3d7b9a5c2b5f549c12b896f4985f3fb18f82 +423500 4a09df19634b6b11e40263176de979e2aa0b3aca1a95825c3ad7eac2a84bad86 1a2acd078f11b82d5bfc43a088c8ad652ac993c2478fc2469859ee4885da2ef9 +424000 bad4fea158757021f14bfa735be04d0514bc3632f96600b1e4d632efc33c6238 ea4dcf8ad8122d652811cb25f7277c96380f8f3b1c839f63d8085d1aa69f5f89 +424500 bc5a6d616947f853ed4f4d9737fa063959d32b8acdd92b11e27dd47b435b38b7 80e414f560811279931e8906a37b79d64836df3f560e19ad9a9940a2e282ce47 +425000 5b30049913f1ad030235ecd4deb6fc86120b8206e5269d39e56e6c5f1bdef8e8 40b7b38eead88896d0993db8806af19167c6c2dcaf989e571a90cc689568e9a4 +425500 1c8c6b6e080abbbd1aae24639ad147ce9dcfb2836be1a4d5520ebfb3a9af61f1 c6e3bb5149886a4050a284990303b89355967d534894ec988b9b9be1f451edcb +426000 9a644de2cda849b362daeec3da5cc28e71652c6a2bbf37151d8ec938e728f2b3 a38d8a78768bea93a802d40ee5d92753afeae17ec5c625515512f4284d56eb1d +426500 4b3cba7009b8a7e4fca74996822c10f661db865afde3ccf0335607d549db29ea 91b61ada9596f861c6ae77dcb3f1da3ea0f39dd623b58910ffbacb800448a8ca +427000 6c5ce8cfc2031e16ddbc1da1b7854aa2b45d1e3a69f2fb73bb14d74a34c702ca ab6ed27075246d24cad1577e05b7c23f081c1be76318508cc5f08aa692ebad22 +427500 8156e4220e867b6f1f988b354b5a96ad98b7f7e83774dc658aef3cb7113d274b f8874e6dca4d1a2e74d442999c6f47bb74a49892a0d9a1894c81fb7d1c350f40 +428000 8dbc7ef6a382825ff9af8347d23569898d687ffd3743285297278cd90e3cdb13 21978c24fad677e5014f13ed54d547eb96722526cb52969673292324cb188b79 +428500 cd56d45be8fc1eef02a601ff132566ef3537c7a69f67f2d8ca6de2fbcff16bf7 f0b9826043f778014106dd4bc0e5429d0f217fce16e401a42ed252b1d2ea6551 +429000 4aec842c5e392ec18423fe3735e8f9c84203ef54f2b3dc837100e42fba29dd61 5e2a8cf8c42f6cbdcd136ecb5386e02eb993ab5314fd3410354159cb6497943c +429500 24d33bfe6e7a8694630a848958175d99c8c5a5fc092437ca95edb5e8f1aaee5c dc92e71cbf3286ffc80dd237cc7cf5ed769de81324291bb80548a79b6a12e354 +430000 64e5cb5e05477470b8068e35326c7e6779e4cbb740cdab675f60a3048e15fc25 c622476e2285d634a82c4720d0a864b11332b9e00c3379f7001f28608c1c4817 +430500 c7938907c4845919f2ec22dd7ff432e7e3e2429e65118a11ae5f2fdc1f5d9ba4 5b34e827fefc85f603439e3367c5bcbb63d94aef7f5389c54e10bf41c1149d09 +431000 a1f4642fec301e246762ea675616ba729278569d221b83e0fee38459ebb157a0 449b183170ba71bd90a2cf58cf0fa0a61ae8d86fe32075dd4b9e80c0a3d8f365 +431500 012e1de4c57ac640cce243e1577db4193427c285579d43614463815932e6f227 c5e13378fdc635b4f2d4dc61ee5d1c0ed7f60850a9de87de8307c9e7e494cbdf +432000 e530822bbe70b043e108e5101b6a684e9b1d5c47a8d0b2e5ace12e44810097f4 211321ba33cbfd213efb8e3df7e7ea774ea25fb24c82cb757e568b756472ee12 +432500 c40c2a8915c595ad827fc5097518aaeedb71832cfdc7f3336789fd6ec0fe4231 37b6a9e80f2b789d61060f97f6f7b8ce019ff788339de54fccb2101c184b810c +433000 c471b86324fb2df461084f3b3f91364c8e4e54b9c592457044805c9c0b1b1ff0 c3cac09940b2a846d2798df91b2e697dff55fcac233473d159da02f5d2a8865d +433500 7c83482c8fd9e4c57aa0dd75563c991933b314f3f18c7cc821204bc41d4c76d3 4d6ebbf09ef39066a45d9157273466fc6e1b08f246396ad00ca6a533a4d08da5 +434000 7d3607cb242e5daa146bb817da979a8874bbbb9abdffa2cfecc546bef9a2cd26 7bbd1335b9852ee4cb13b1d7285dc11f9c2867fbe267cc4b6c1b1a3dd6a15803 +434500 0534c7881cc8c3745dcac6942291cfab024520de1159d3cd1cd6fd853a52e1c2 b08a60d232a23a137cb4808f3636128335ccd3dd10fdcf766ae5c20b64d8a926 +435000 264a1cbeeed20cb538b71b99c8da4874af707bdf7591cc37e8f2780ed8fda372 f226d8a6d56c761d1f9ebb8f5b06414d1339c17889767ce48317564edbfb337b +435500 410932e8557a564a226ac672c5578f75653ed84c4669f64b6bc29fabcdd90586 a5c68267fd466a48ec4473ff1c904efa8eeed2124b7e4bab8bbf83ee876dc375 +436000 c153195fdfcf3938a5dd5ebb0d5dd0ec59692a8decf1184083679c46569b537a 5d8daaa7bff2de12b5d9b428e84e5ba25ef9001e45ab728ec74d6a22c61b269b +436500 5e3b108e26916d2507a40bc30211948a0bcff8ce4b40117ade096cb5f94d22c4 16754de8c6290ed9a3e8769b8a03375448921fdf847c7f1ed6d627e614b8f6ea +437000 06ad60451a13eb38ac6f9abd2375d50afa00d4d8c5dc1c2806bb5a05727ba523 4960cb8d1571d7c0aaf33b659c0721fb84bdda17daad04f0cc169dfe1a22ee28 +437500 2f14aaa3e58a32d5a3f17562b3ccbf8edc8bd17193a9a09af65052be99225a2b 8709492c2ef726d995e4a2092a2d062a849450a00582ccf51fd7ce3905ffd2c6 +438000 87d16a339b0ffda31c75b16f3fc8f7c45d0c67ed9de0bb27027524661b1f91a7 c21254cce7d2b9df55ce98218ab9d20d9d2afc3a450ad1f3931e9c7802de8f35 +438500 b84fb753cd672d1622e7384382069066410f1c27763902b5ee2c919ba3f20bce 075834fbe8e33fb3add9426e52ca900f7db370b85651ae10ac5079aae540153a +439000 cebe0aac7f629f59ee1b2928d8ce4340b247c0c619e3ff53b369375bfa71abaa 7e5fe6db2171c4040cf71a4bbc598983a4892689b7b8da5e3cd161ed7696a410 +439500 7a70cbc08418eb8b711aa13b0a49ca6cdf063a443a0aace6b8f78aa0c9bdc3d5 c7107988cc60c6c66c2c9fce42c97303bfbe3d3db9ebb17da8416d0b1a918e44 +440000 7649654fe753d823dc09f3ccd2157d9e9849e534962d5ec32f5a71775ba31fff 3670ea77b7e5a8750d63d8d846cc2e676e69539125ca1a3f66c1d1573e47ede3 +440500 9ad49b33c96384a916c19e0b7fceecc324d33aaa8fc45b5e48c661aec09c880e e594489948d159bafb11b9a4151a89280c37b8f5665d6d209766ccda296346a1 +441000 2a3a6ecf5d7defaaee82b14d7073402109769a5a8c940d6ee1fd35f23303bf95 e8520de1202a192e8f8bc825b528d2fd8805efb774b18c4ab2f3dd153063a0dc +441500 72fbb24c48304e9daaec1801004fcd4bb4b6c0c511932821a6d875bf4c9e6bc9 13850c1041b5b257adf6f51551051abcdcf13bf6d674e075c0ea32028011acc5 +442000 d8d9a1091bf2e48609bde3ea3140ec19cfb7a2a14735474c6f9ef6105c46c8f6 8879f9476a6a342e58cb8ba5e4e40a9ff6ed0b605f3cd9b4bd9e6cf5dd2dfff1 +442500 78782ee99be29090c13682ee644a47f85f410c5a24c1976dc047f3249dc1e0e4 b4fd6ca7a0b0a77143c457285ecc982da5b9639a5659c03ba88315432e4881dc +443000 a5d7115bce13dbdf436226400b23c30de09f68f6a5137cba50de278effa35891 cfcc7a433eb789185f88cd600fb249d26b8c945d5e5a5d7e978ae3c1c1118a10 +443500 f73cd58af88e5cc893f5a5981888e792f6a15cc06093c14e0ed91110778f0312 22b5b27df320f33c1856dd087c8537e5837c0686ccb436961189989d593b318b +444000 7c497c96ad4740eb68993e26ae3127c6c9e10b8625f9e759131574b87cd722a3 e5405706b778afbf261e0b4ec0a7fdca861baa26ec3a65f5e970769a8497ac1d +444500 cd52d7101a166f4dea73875c0fa5c494db3419bc3f63d539e57c8e853e96cc6f c874b6d14b66ae740907a7ef12eb55dc0983a4a9ad00b27885902a11c72d8009 +445000 30283438585b2c4d3409d61110cc5d400b0abc0cb8e8b65b9fd8619e54dc387a 3664d9de72ce8aebeaf5f1e844e44f5b3362d0faa80415de24c2cf5fdce153a0 +445500 a4496d3bd4aea3dbe7cf7d2d5a3f0850f5c01b04662dfd38d3dbe6736251e91f 8cfad175dbdfb208dc40cdc2f03d0ebcbd9f3b12847f6d069fac33a5c1ba5173 +446000 7cff0564cc670e8a69315b18b0ee229269682bb0286b5e4b3db25f02ce0c4d2c b5c22f647055dc1204b243da18d999adebc56060e52f1f0c4d35450a9e941c68 +446500 201cc692970eec864c92e5e7af036192a9562a62d2723204366bdbfdb0741fa4 f3abf2331a35c83fb86e4484026fbc40266f5551bc90551b90be2d26b3771e3d +447000 c06f8dfebb81f0cb3f19109f662a7dcaa4a023f1fbf735ed81fdd6bf9fa95023 0799398c8a29c71de6027c446de153b17b1bb364570328bf5aea872fa246ca98 +447500 05185f624dab270906ae111fb2c43e838b70a78e0420e84fcc94bfe4fc911dbe f73e1007bb6e28fa8a5add7dc21539c7545ca68c600ebc62b10ae00382c9a538 +448000 4fc96bd14509bf55922cb5d8ed4cbf372f7ce991646e5be3a086204fea0c368d 0fff78f76cbc925b62ffe927d50cbc02dc83fd9e5405a3a77e232b55a494812d +448500 742e9431b2ac6f0ac24e58cb8e6088ac1de875cda8f16739a78e0f1f7c05ce33 d892c34609acf3eeaeb32a384985fd2da70c4f53f59f8c459eaa5e74b3d61c40 +449000 335341c810fdea9e158ba78a542922754fac2e0fece92c27559d14c5c3ca0267 8d6340cdabfbfcddacf2991913176b17513be28b9472b744e7a64b2f2ca13896 +449500 2b97e9d073d0b8922e73a1b1d6fcb7f8dbcb0e22fea6a707d9faa2afa1cddf8f 52066346f6120422b739d9d1cfd8452a00c55505e0b2b9805351f7721cf253db +450000 a6448ff3de412e8784a4d0590120e0727c3ef1c6e98af7adec4b687e10c22b40 b12232c014cf52f4f8d0fdb4037642bc1ce2a4d709ae37b91d908c9d36830fd2 +450500 32343305ccca111d5b0396816091f1f169314acfb4974629e7beb8680523cd47 36cc9b37d7962e434782de92543635fb826a4d84fb5260ca0161680523f54272 +451000 adcb05f8f053823db604b6278de320da2097890b386fe67c0104721e6cd76954 c63c045a3117df4fef3287be2f66c5691b700b18b42d3ba60509faf162dc9720 +451500 c3f6f574abc62ac6ee2b65eee953b3386808361f721ec77e4df9bad7811b0c13 8ed2df18709fd8580694387f903d9c31fa4a7c5e3bf3845b4fa4e9a701441112 +452000 b47901f63e411758d074dd4181d87fe30acb1b1623d0e69701093d93bc505b4b 227dde0bdd2ce1659265ced176e8c5514aa1d7c9c58c5f92d18534141e157539 +452500 723793ff45924363551d433d63f58d9951d58791f6954a6aa906d8c61f80d535 1a931421c2325c0c2cdea860d128402f6ba18decc20e05279f62f0d0f5940f58 +453000 14a86fdf9716b2225cf80394126c1bcebeba3be4a4f103a3497c8eb079ab11bf c0261d2846f68bee936c5ebfb638d9d4e6bffc53519b76ce72dcefa8f66e948b +453500 4204f92e27edb642343263ee5c8265639a4e4bcfe6743c323e5915af2db0f68f 681663c10b07fae27a187f5512c63f266d4460abd46ec1f464e8744b5ddaef67 +454000 d7173ff752ac555014e60e3928c1a8ba368d9ecfacec9b302cda79a6c6162eb4 c34bd72a990e253a26ffe68a91553b0b771083a569e512c6a46a52e5c7b6a724 +454500 df134eb761013d3ba1bd9ccb38d735427a299f2e275a3a1764ae436ba07f09c2 4d9b5dfb6baf847acb9499ef9cedfabe98dd58150569156e5bd692d1f7434f3f +455000 edf2fdc49b76a90ecdfcf0e238e66d574794b7ebab6a4e7d212e1884a21588d0 c40b528799070a1964e73025da6badbda9f994fe3d83cb4ac5fae95aa2e42963 +455500 0728ecc53f1b1b7be2478f687881eff7e6ba31e9d565619ed628ea9263b67a97 700384e16447d9873213d70063f468f9e4e9b1f780104d9a118d86b09aab9594 +456000 fee4d1a2fb2a99260e941a74ab4dc87e0520c28a115ba86c6382791b2f06f1f6 e0487333ab303a6d155cbc61b33eb1b23a3a9bfc3d466c32d8e6793328b3bd8d +456500 b5f05116bdbe8b51f4c1ffaff1a0e0a445e3a0f0c6c718a72d346162820509bc c7a2de5b71c4c7b338b04adfa8fe16857ba3c926fffbfc97dac86b0b75033978 +457000 b414f29bc13f7cac4f028641c788a54786bc9412ed582f4aedd5df86039a8e00 ce69e9060126b330e010d201b073dfc0e16924a8c11acdaa61c7776f0f2d8021 +457500 c2f18fb64768083a462a48b87b993870ab96a70e325cc1b7ce676a504dbbf4a2 bcf97c78db8bc3b69a377a185a429277aa530aa853c7ea12c2ab06566e837c9d +458000 379e0643c8dab2bdefcc1cedf8118337bc0133f0c938a1553088230052f5cfcb 22e7617308101b51b4ddaebed23dc67e0d1e0b3f84c5074039ed77c466cc83a5 +458500 5b8d6a2de4952aa7aa38b9a1d403fd80e65f6cefb5e824c062d060894d490946 56ff3b66fcc71ae8550fd0630fc358edf11f7afdd551d69a2ec9d4b9fe154bae +459000 426443a695e4615554b95c1070f14391c199178d8be6860807b69cf2d686501f 40015ea68bd905903100db2ed67d0a0e25510796dbcd1bfaa0b8dffec058ec4c +459500 561074bb87ee4d3132bd22b91af78df7d2cdec1c6914b43b12a4004204094a4c 86a908568cbb19be09a1c70b4a92a0b3022e5d0008ad78d6ed536a9bdb08deec +460000 7adb9b6d14e8124ab25bb4696bc8afe6a96cdae5396ac2a5d6d859c47236990b 6ea2894408475e17a8c4476b5084cd76742a3cdb97292eba2e2ba0ad4bb17d87 +460500 5cc330e1e985af39a1a24602603f26535df43ea7e5e164c6b654f292bb359633 69c11c20de0f89ada1a65781a2b7e7a1e235f964341eb70434135888794032c8 +461000 c1c45b5a71cf6ed3c5d9a739c227b68fdd3cb41e8102b7e99c28084776c3d8c9 122728c928e7025fba2e58a3b70cf5dbf448e26389be3606b43f3b014d5f2001 +461500 096479184f5b32532940e42b35c51e50008b8224b2c6d6317dc69478d3d316ac aa693ef567b3b7c4a7091b5bb3d34161b4235e7bb3b573a22e66bab0e622f16e +462000 c78d5a646f9130aacd23a52cc3dfc5a084add7fde9dddbefdd13d5c438e024a2 3242055c63d5e14c1efdba0effc3580106e14c443a3c39c699931a6c4a36c94b +462500 b464f9105311560ac63c29a94f8a0a5f378bcf5c22cbd23d5818ebe2e62550d3 4b91a857ed6803609c0f1d514e5793f3f63ea3dd37f84b345c3c64c7248f8358 +463000 ad7d579916aabacd745a9d48122a64e420ba111b8633119f6110a93125eb6072 ab7a1e347245f8d9ca551cc5e89247e18d130aa0180d375673de0578dffdec36 +463500 9f647ead66c59424ffdba0067d38a93c33cbc5cce95c1fb19483d3138d9a2924 f9be09f0a7ec9832b7d2e238f0f3841a10e99967b378bb93e1e8ed95025791e4 +464000 8239a8c5704697db70720d8227fef8093751df0e79bc792a4583106c5229d97c 56a2bdda4613688231ea85b9d659f05000786ebd53300466da5dcc474f8386f1 +464500 b8500802e87f0a3d08b275296b32aa66211d93ba13dffce95a77f4d2ab6ff8a1 87c4e1ae3a488895890a0dda084969a43703e9abb82fa8a6b0fffda090b1fcb0 +465000 9c5f241c6d7947c078423264cd080eebeb852aca6898b71dec1f2fe0f97f40a4 eef3196b8ab3c6d1e3df1eeed6f857644e50f6a751843e308a86d72e945cc6b6 +465500 714cef624738d796efc3fb3fdae8f42478924d28a0644c225374b1e5f8707262 8d68d66be9f3952f46bcbe5df4829d93eff2f4501bf422582e8594bf3f03730e +466000 760869daabeae89da11828967600ecfc92ff38d5eb79326da9fc526ff88e3dc5 ff631213c2cd21307d635337ecefd200cc2b7767c171ba5913ecb785d6876aca +466500 9699ebe38cd6fed8bad4e96c18eeb8bbfce93fa6be86c0c364ac187c246d47e9 f18cb4eae80510bbccc360228942bbde0144f6c66f378e0b4bd0005ce35debe9 +467000 6de68cf541236c2b20da6bb789be6df085207919a94074f31975a54250c4d918 0c998a0b617d6dbe4454ad981478bc5903ace25595e5f868c6bbab61f80cab09 +467500 34aae978a356d729783acb130e2238fefadba1789ddf82d73b87c79d862d297f a90873e05bcc6a81932e4562242f7ca201a07707b505f5dddb46418cfc26c5dd +468000 96a959479e0159621c8660165d2053878cad8adccc1ea5a4020c4a1f63941b09 ad947d10fe2c2fb1f61572586b941c872bd958499342e2e44b545d4c0faaafc3 +468500 ef36c23495b3e062fd2a92e24164f80a01d74452d2d439a23a38e403e531ffb4 5790fe543b78423f088a1d53daf1692a7051169bbf1e469caaa7d3862d709b2d +469000 e32fb17dab672f159e97ac45ddb12dfe0dc0bf2a2abdc455beccdfaca5e2c69b 56f4d6e427a46b3467c26174b6a4b66e30f5d962450273ebaa598c06aa31975b +469500 3870050849e79e3243d3460ea594ef741a1d6327f5d336f8036fa5e97e0cfbb2 2e0823dd7f7a137a063f473cb6991510a112460ecacfe18aba39094d410c8930 +470000 2aeac4abdc914d4ac3fde31a5882b242aa26f1049c86f703fa5da279edad5a7f db8d47e66737aa4776e8cb857386d346d356ba91d0f6212d8aac34c9ac2e5906 +470500 fdab2f454e5102f44be86a36b7c2a54aea2f5681bcbe811942451827e8106734 cb60e3d4e6620535cbf57bf0f94c5055eabf0ca48052e9728fb28f5ec913e5c9 +471000 afd4b785fe4656f48ec1120a14136d133df7059eedf76f407dc2bddb91ab0ce4 0eca3c26dcc9bd50d78a8322c07b18d59c7609bc6ac97258b56b20ac73187f06 +471500 e633efdf19faa95b16be4cb94f7072dc1238342d624bb4d69609555a927f2522 570bc44f8e51f6dceddce363db3b87fc17d9c8ae51ab9266a3d9248b546df4cc +472000 d4aeba089eca88aca7c2b7cc89e10146158dac72fd7643e4c7dada43c8645757 e49b1d22916fa97893a9f31f31bce50f96d49f6a388ee1c8ec2216aa619d9101 +472500 a1c6bef24a340e81f3212db55e9db0bbe8135334e14f66839123c21fa393ea57 1b5318294e73812e157fd78c84b79a31d076bafd91aeda00fa49055003be7e91 +473000 c2f76403fbff7b3601c0b2bc71cc00963cf0b012e09d6be5d8fcc12a8f78c8e6 be2ae59d51ecc553798bcd9c82f4e747dfd1782ad18021dadc24e4838c2b0666 +473500 5f6ff7d98fff43942ec210bff0555d74768aff3e5fd5ab6e0b546d0bfea79921 b828dfae2a89c1199e2cad79165f815f8d858620d87aefe17630048ec05f0bbe +474000 2c191256f2a1ea617517c81c3133a47d2fa264586885b2ca6a7b782f9c78a8bf 7d0b4169707e36667345fa68284bb9eb0dbbb726b9391ec0b3018c71a343ce0b +474500 50b6c4f4c1da30ee2043c4a18b0dfa1da9aa33000be4525bb07b2462d8cabd2f 097a81a5771e9db71271cc6c0ce12a4053e89f2975e8523b6dde6556c170e5e8 +475000 2c38054fd7a02ffcac28fb8c53ad84b68d50ef30af030e597414e15b59bef269 6faee9c17b651d220534dc8c66c0345d162426661af23bd6be235d1c0eef73ab +475500 8d981411752ee6b44f12860a9975ef23df0c7dcb5b5c621368be19033de81de2 23fd87f81163c7f0580a7eaf5f74b7501a868508ba60f47836488f5b72716608 +476000 27aaae6fc04f000f25a467ff5c2fcf62a6872b3ea96a3ca445e0947bc912d050 061c4b35170cf452518590f1e96624dbc031e5eec8b9de1acc81a477bbaa18a0 +476500 84e8616d4259371a11abc9625c4f7d55ccaf949881e017b663f249d29aaabf70 6e6418118d4d34c06bf73284c95f2e3622c87c42ebd4a8e1380939e6abeca428 +477000 d459f75729583c8259ea66e8dcc7b8e1486b0a30e14864332dd014757bac1f5f 9316f02e5bffa72a4ea6c7809be57001db28415e75048320d33dbf23c3a39e86 +477500 329e7c98cde787901a05bbae8aea2a10fa244630640182eb9fcb80e83ff47cc8 04fabe2b506d1c811471d88cb7986d2a67a988e0e094936d398af0b20dafed91 +478000 8515a758d83284a93a354e91ca1389d0dae0914aabde4c7069180dac0f88ece2 0f9d14354952b22318f9909502aaf188e43c511ea6c627724bcb7f0d14550233 +478500 e0b6428a55906d115c2365630e7a748422b1309a75df0712730a018e138f8ae7 75327db1cbc6d65c203eea4cf697e0e8674ff0cf180ad7730c0f42fac2cb2259 +479000 acdd6bdcb3e05e00631aeac70db0beacb5ff7339f9efffe5d125a748e050cf0a 2f4a8780e4913da0886b73e399430b0a65b84e906eb7b47bb14ef6139699e222 +479500 173ac0f8bef2de4b544b42c3506aa887ddb348e6f719f2b4fe16beb569b6b9a9 550714098a237e5f2a2313711787f7037937081893315ca43450ca64c071c798 +480000 39a37003b75d37e0588e177d5d5566dbd2cf4f1ade343067de439e4e60d401e7 e90cf6f99b06ab2f396192723accafe62f0a7705a8795b39371238dc660881ff +480500 675b1ab9b45350f0db5814692844dedce30a416b0c5a6b04e3ec778de1c54476 0cb43e9d578692ced5b3c43a067a09dc8e582a12e46208e1a26841645ebbccf4 +481000 c49b18c02ab6cdbcbd1380535c394567b0fef261db605ae42584fcfc5657292a e45920a96e78f6ef20be3f24d9a8b1accb6ed9b0a9ace38694eba826aafe2bd5 +481500 e70588e89d0a3a15d7de9bc26e47768a94964a14edaa0355853f5629a500dffa f623feb088592f1cc01e94f8984e5bd7f5469ce71ff7645ed81648dcb25ecebe +482000 bdfa119169d8507d41720ba8fa40f5ecb75267570ad0ba58e342f4351fb74575 f28b44f2e70183e19deb6562a107d204531edc8b46d4f5aac76884d7d3a9e750 +482500 14e2be2612c4290229084f2c1edfefd0e432a91254abb83ef8ddbe8d7bf2e661 aa012dfb4ea0b0d0a565fd8f2f55692135969aeb6a9d3f34cb17b31e32ab22c6 +483000 8cc62d490274477579f93e8af507c07b3816d393200aafdbb6b68755b763c2f8 3bce0766356c52720491791bd9cc5a6584c48a39d620bb185f368eb7dd7db697 +483500 72c78ca3f93bde96a8afdc199714ae18e9c15b92526ff48845c050047343f548 f4c218fc750e8b31e2b4f56ea5bdd7f027227b58920c7d08946d193bd090a366 +484000 ad5719becb74523e16221adb2353a4786de357612d9e2ae86753be7035d1074c f040df388303fff9b7c97bfd38d55a94c1701b82ff4f2d594b1f4637fd8ba964 +484500 a143595d89e542b00901e959c20b1bee243c6b7f2a8bd930401a0a922586308e 1c50e70afe27a5026a6ae968118f5141448e69955b431f74c2b94af7bee31ffe +485000 ca0faa2c4b601116d3e03c898bb6fd6de955e9e083910fcbe93303702522fecb 98250685f5d2fc349e6088d617a7986ea28204843abf4e887ee121b3f89a2b36 +485500 1d2f98bacec2bc91b7ffcd9e760c49bc18183ddf1bfb4aac8145abcfe2f91adf c03ae99bd105f3c8154bed1630bbfc10d56a2aa4e36a85d4a9fb2e629920e285 +486000 3f9896825fb06766d9216cf1004aa6d2541642253b2980c59775fa720e1af9d4 e8ff26003f9db4ca25d4a5e8f22c615b6b376f2aba0b6e3d3ec1fc54bfd3e8c9 +486500 8387046d6d520535e933c40c8e7a9b253dea20d1d4fea49c53cca41d13f87b5e 29e01fc49acda9fd49203e03a86fd7fafff8dba947e907ca41db1b6e2802081c +487000 3ee4cfc2068bfe1472bb2345900fda4ce7d99aef724ce5c35e8dafc97472a163 84ee857e054c6f118f897926d53b546c4fbaa38576d66eee4560d88f90f2ac91 +487500 7802c03efcb4298fbff0c05c7c2a2670e2b5c5c0a912260fca95f5ee401f9fac 9c507f7adb01fdb9fe8edd09d969ea7b12474cd9f317e6e0c8d1047f720cfa01 +488000 6c0026aa2c04a1ab5ab59df72d9ac017f559889453749c95937e01230628ff22 89cd039f94be43a6d229425daa26daef3bee7688fd591dc954ee503ebe2f3573 +488500 ad7a33c68b62d181fe269bb58ad9f00b48b4b19f77cae3c68407ba81af9e29f5 e26367c609049d5cd867041787aac089a459ae2a39e420b791b778f49c4222e6 +489000 dce5e567c4502e3b3344804468bd48900df890568521360b198e5b7e48c4a7cf 497cd478db672eb3f6e975b722a162b26d5f0fffdfeabba6dc28664d4dc76bb2 +489500 326013c6ec37ad623eb9c5a35a51c154447c8e180d4efc381f02a791c8dff517 81c2d130a11e3caf98c5d4af0ae7c05bd5d79099d5c959efabe7766a211c87f6 +490000 b0ddbded4a9bad235b75d2dad597b8fd87b80cb721d2c322bde82e9bb3763d72 5abf2e1aa1273cc9de5eab070f0dcd5b082cf7459de699a96e7b12dae3311265 +490500 40f093e9a8e074fded5c67276c85a0261d15f3e418e492ab1452e340da3c0469 0d79b5359c16640713fd6b885d6327bf67971e99395eb633ec71edf9f67a0649 +491000 42ff56a78710f1d58f555a7f55001a1760c7090441dd2657f959a67b2d8a8289 7a167a1f0656c972aaaf079ba002f35ecb7d47aed4c72a7da645276ee21f9ae5 +491500 38a42670dbea9a526f90e8a824bb52398335afd259b0d89f287687810cb3a268 c4f01d0a2b59e7cedc7957ca4dd510eb692cb5ef4c257a76fce04c3e51487e20 +492000 1aedf52ef4f42d0e049cd6f2b564a1416ff737cf792eef860c4678cf4b1929f6 a6c9e1dee9d08b510553bee2acf0d1b8b2f0bcd202ae4d28110fff8dcedd85c8 +492500 d7c8c52fb1f9138293bdf0c28ac68aa8447efcb1246b8896a1777e5b20800ee5 0d6687cf95402d4df5a9115b6786b1fdd27493c88b0e1fba921bb7b722084a88 +493000 e4265d17c243d4e4dcbe12d216d7dfe7e4e3b7da161226203881b0c007f2936e a6f9daa6f9635dcf4a2cc894a4b459a8aa5dc2ed487d95d733bbfe827aebb0f2 +493500 9c3b415ad45f42001d88e2dd38748048ef9f40223e5c1c62caf5eb7912d78b42 e68eb0e1b1b95dea2fff359dc5444930588ef743a58da356b8d33d8e6a0ee1b7 +494000 e78ce6a76a928e878b6cb5a0ca25c6b9a8cd69d4e2db9a066e702635c11ebe17 ea5640b26510140f3abd04e611f124851c15338caa163bea3a5032fd0d16b160 +494500 cb62ce4407ef63b81d3afec33e7b57b31a6e7c8b3e0888b8db96f17e4d39756a 6600b14d7f4f7bbc0f680587684f9fa7d5a7acd26cf9d103ac31bbedf9d6afed +495000 0e8a7e8cc89426af75740fb8469fc9616a8e75401fc785a8e686d5ec0f4e5732 0a1ce34d16dece7d7bda04e5b9bcce126b08d7e514e0fb37c5c1b0871cff4cda +495500 ba423afca93230eb911c2f4dfbba40b893b938a7365cfc2068e1ecd3bc770b5f 5720feefb7971397896c9a270a6318f2adc667cb20f2cd218b2791778cd50680 +496000 352a1fd91dc52fe0cd78e2de4240e75a785420049e7b95e01adfa3d1ac4ad512 781da1ffa0ccbadf08f451c290221fbef9b440c52b5f7ef5bfa3dfe51562de47 +496500 d0743556e112656c069f2749baef59ef56225b6bbee44b2336ff106773fbe965 ebc3df7a34a4a4e9d8a60151f4a5bdce4f40cc8864851520f5161abf9b9e6d51 +497000 1887117917c8adb81cfbf85bd4950684145a964a12c81ca89a44fa107247c275 e48c07018555f1a8be3ad71f6ec7c31932eab7aedfb414c99bb793715ec36e4a +497500 2ffe736439e02d764f544aa4f86a125b1567ab7d24a9d944313877d2d7c39d14 a2b4732449a3ad1488478233895bd8ba26b72f14bf839a33d5fc0f2934bcdf2a +498000 0bc8b494b42efd68080fb3d3d83cc8156b56439c10fa789814d751d5c4f7d9dc ea1ad23e7f85ec30d138ecdf527a5ec5ac6d925eb888ab061b9ae24e48e60b2b +498500 5440fcfb2e8888508c4fd6920d0e6c7c1f3f1095813d45b691edc3a2a475722a 4c2c9c60a2a43f9786004a9b43275656b60a70c9643d6c676ede104b9f0cec12 +499000 d84c475656720900a0b3765c4ed57ae1f0ba17238d34ac17b1635c6c95064750 236f102f6f2350128916d138401344233a7483a0dcb1eb39730c6a5fa22fed3a +499500 8b9aabf8cb60c29c4b60a942812968ab80ec48cad4053de2624d42372a547756 4fdf0de2a22bbd66bcebdd844beeaa7d5caa353352275953936b6f7839497577 +500000 bb94f21cd879e992217888fa56f0cb57bf585f8a41f845fbb46872fd7ddd4a40 541ce9a7c0f8771a84281db66fba7cc2dc43fa636a39a840fee5bdf987a3348f +500500 d74c1f7510c4307c942906d4c66ce5fc542f2a1944659c1ad37a0eb82be11751 976e8ed703619833f8d78889bb8a3a71a06392e257b093a01f7a1b7036d22fb0 +501000 3383f0c0456774cc529b5b0633a947e0708156bc873180f56fe3c32008c8bdac 69c86425b5c47c2bc3a9fe3669b0a80aeeb72bad8179676a55dae6445b7bdd36 +501500 d076738e554cadcd99a14287f0d0af18fca6f893e6ab519fff1a9c987e3aa7c9 9ceace4059979133e529e2e33930ee2ceeb2058633e06c31efecc7d2112a2437 +502000 03b70f8f87fc995b1f25c5979cab284b112e66ebeec690dd93e6c1f238ab616a 5c4442bcce1ace2848f3bbff98abe5776b58af7da464ea4d8ce5c681ecb99261 +502500 99130d7f62752e1e0a7318ae3a933b638ae37528134af7b76590d477bee107b5 206bd50bea02399a5ad897c4e1190e96ba7c092f83cbad5e0bd65955cc253219 +503000 3eeca5fb5aa1f44a7a86e9c2756add708089ba817e4ebe983da92bd815fd51da fc3643c494c343939f26a8237b3df02c2b96235e034e92e199980834ed4a59b1 +503500 8034c879f7596a3d9846ce1ae028fa596b84c62313f9b43564ada16c3e2f547a fd766d22b34e5e391c7a3e8c1318ae469a763ba6c49b75d55b0f928c83e5f992 +504000 bbc5fdb678ff466da54e09ab29562e16a798ae2a05eaf776d97edfa35c839a85 b6dce69571860fa8d6db1cae19087b3232b06b8282f5b905a6ff462528a1fcc7 +504500 956aea829d066998cf96a8b8edb9a0323dfd018642692dd7daf59d0be1b31ef5 f90a6f0b96ed84327fe33e6edf930a439d1843046154fe1fdcd65c011aafde2b +505000 0a3445456f4807eb2f234e51acf42870aa7b6a2eb96749ac3cf764979d3aeb14 f6977bf4113ff5e28895c0f148fa0ae812a4dfed4ac0483419d08e3493c90e4d +505500 fdf15fb92600803d9d99b54b13c028ebe2a39d0681e7cb751496a155b11a9656 6fdd6b0729448d9a5dbd5b5ec1198c5f89d0a437c9019c94e0cc4b0cff9344f5 +506000 94533d7531d5ab46aa3ad4c03ad71b8a6f78574805a683be4021d3648a56b0b0 7c1f8200acab1aa87ce1ce548b727d3c47239045d9dcb6d80ba58d908af43c99 +506500 0f86a6ffdc4880c047c73dc16aa8d0bf27d1dde7700c2fd77e4cdd7e7d926cd8 e9c9a93746ec3a17feb362c2dce6e9a9c51a0d9f0d9700d21bd8aba906602e95 +507000 431e08381fac05624f905ca38d18e788204823803e0607106e147bbbe8bf688a 4ac228d8c9c41f4e3d3a3c4e320ed04c13be367680f9fdbe8f6e5a7ab2a23f36 +507500 a227a08c0b6d0818ac30280e4cc9f422bbb17cde711c371acb7fc19c8a642276 6dd10139c09a88ed1692aa137b788336a80ccd60753eb237e90687a15ec987cb +508000 ac9eed373dc35d2906cefc9e3ad2c8044253e75789f4d028b84a5acab33f8187 d4fcd4e62b88c4ef4e9609e8e5c85ba44b15b999dafa68a7cd45eea0b9fee193 +508500 82837e772818deb177c58056f9b8eb8c42b42c75053c206ce64d4cb0caa569a0 088031bb4d6c8d50690782746dae3a2894e04749cb8073703758bf84403f65e9 +509000 46d4c946d3305147e565ccd6bcf5049931048ab9fa78fe64a45fcd29df7edeee 0d2fb9af0d18a37f8bd0d90677391033552038c5d24a58db89420c355342bc33 +509500 dd8574ebf8743606a8b2692ca8fe7c406487b6ba9a0eb1194eba2631c7ce67c1 2ff727ddf8cb09ef0622821c1886cb8270d4401658d2249c402f48a6ffbbbcbe +510000 8b496633b233a6ff9deba1fc40f3e1ecff693d91d8abda762bb165b4f10933e9 3a409f0d90719857a525deb2038518acc44e2e710e3f51830c11f3a83c89ec22 +510500 1052dbabe3ecd64ab6a4b0b05f41c6c4131f5beee2028096f688af8da17151c9 0cdd930cbfb6e609de9a22d029c15c5106fbb5603222b8e470a43594d1ee1dd5 +511000 e1718d99ca8b8c0e1f84663aaa6ac9cd8c369e9118bd6ea5919856382019f5b7 8320100810a7678c08ef0003dd4f057369386278d95ac3d9b316a062b1cd119a +511500 df937a1ddd9ab676d6b958c03e97b7789fc0b486d52e1a518f4d4e46d22db830 e5b333e553b5a4bca5ee6f4a3ed9d83ba46d981a3ce30fe8fe0578788a400f42 +512000 38982a86cf02da0b76412cd5bc52aac39854cb1d000c7f24e6b66061c9552820 7b4b3c77c94a5b9de5a7ce479a1c15ae56663e655104b0f0efb5f3adb971cd4a +512500 c668fd92e5ab8abe4fa8c66022eaf0b8387408849349453ba8751194bc9ea391 aebb86a20afe467e4959eb4704849a076a8e02cbc51c2dfe07648562e42f9f5b +513000 fa2042f44dad2c0e4b243fc07a60172d057c7715b7d7f73e9cdab068df241dca 9bf87e1bf1a619c55107e28f3abdabc3bb3267a0b0225855695ccae2be2c8492 +513500 3ada907b4eb50e027c4dce815bc8464aab74eebaa02c3fff63e829fcbcc06790 1504730fea3476e4a5a65059e9e45acf9fde314062f93761273d0d47ef900b5d +514000 b690974d95cf519aa561d59afc9812bf23f8596633d4e43638aefd0a645cfd9a 53527239f3915f67eca6a1f57a5b82564ca7c4c5344668140b960adb23246528 +514500 4efdb833ed17ee35a5c830945a287adb464a23d60f16c1f1d6d3488d628df9cf f6990f581fe79ffb9d1bed01f84a338213c70bc996261e07507624df42c528a7 +515000 f902ef1df4acafde6dfad28852fa03b1961241be620aa057860c9a70a0fffafc f8f7c7f68536f87056eb0a199f0112974f80f4ca443721ec8c1d67c3cbb7fbfb +515500 df363417aed948a08fe0c6aa48dbc5764fd5cfffadbc7945f1a6ae310a9a1d50 d55f6e43fa28f97008367107abdef40ea271780e5d19a561db5f3af215e60ae3 +516000 6af0f998e7b95f5928801133f52c03133ca1d706f044f545812d0e64afa7eb56 79daefaf210fa29d3c633094944a6ea814f73589d3d477a38fb6d68f0380795c +516500 c7174fb637f35bc6822a651eddde06e842534badc1d7a4c2d61090e79510b970 ed427f81415ab5bfc57f4cfaf8461c18a23a2ca0a7ec9b48002d694c70137716 +517000 f4055907d0bc72d6e28eaa1b566ab77904495bcf286d0dd6d7bc297b8fef7a63 74104317e7c69033b643afb089ea06cafe18e13f125bb7a2f381ac88d362eb63 +517500 dfc6759e8ed2c1078dbf08a64b71ddc9ab2a04fff41fbe9a5c82a250cb3ef4a9 5a544c7180a99ca77684597ca3cfb1257ee0e3fccd468e3771c55e4fd9a1152f +518000 3f867c72bf194f1c257f1476aa4cfd18cbe3a5df52e374b1f705e496e06840c5 36fc1c6723e3e48773e1bd8ae04210fdf0a9d22cce523387250deb28c3793a8c +518500 941b4dd50e5124b756a381e83ef95106d15522461c348d5e032c1b61d838e50f ab5d00a5d8df0c54e297611a063ba4357b4686259473accba2b139ee69dc4d8e +519000 2136325d5351d77945e009193f054b04492a8d8f210eef016a51630313a17c3a ceaa807d0b581d95060026f8ff02c392db4fd0ab5007a86053f9e0b93049aae3 +519500 8740a71b16e0335054adba609b5df4dc066d157dcb0511442d8d413c2333d883 216da33354860bd3b2b44d9f217b89d2a203836e5f0c04de39381776c78d43f1 +520000 21557c530c9135898351f2625779248d5c5ad1fa473d8989e8795cd650f3b036 02327cfdef960f127b2a99811b52f651fd5f84d3064dc10b2b8c4c528a362c2b +520500 fdd0b3a9a08562ab9256162fdf1e8aebba6589b585f34ce26ff0ac20f3b35715 cb7b802cfb533b7191e892348f4076176e842e2a598e373e6653c92b62b4d5f9 +521000 4d45b8b34e350a098a5ec1575639465783982db7710d4ad07f7a299a19b860b1 587836bb3390bf2dc472a0b9e20a5e643cb270d85ed4286180f358cc8d0dfe88 +521500 8357476f03fda1bb1f346946484bfa6cc9d80a89b508255c3c4b583c71ead128 2c2d08c950c6437e2f9ff89d0c3b0d0467a7761a0f19d421e0c0846c2792bb62 +522000 a12936a6677badc6d2c8e83d2058abcbd2deb580e5147f1e742e379f0ffbee7d 322277c4d49c9c37e94b84c184e09dccd63cbf0154e61e36b497369c4c7f3fc4 +522500 f0770540ebe64a0de99c169cd1a7d9c8459eae0e67873510f1a8dcf4841131a3 25ae4d82a8a9089095bc4583c9842984d53bb4b0e6a162d369a70802b7f9476f +523000 72d57e281db8b4a79f566fb587e448883bc1d1325c9ffc2931f1100b63e5836c 00fcd8bff8a5e19824f0e98fca489d02ca2d27fa2b4fd9a73e410d935a1b93d1 +523500 97b2d54ba68ed4a14ca1fb4e4b697b2bf8f7d0332272af4531a72c94c35dc932 04d4f8ef5ff03931eb5c0eac18894a9a7ac8ac7f6bc0b34b43420ae9d7802a1b +524000 f5c96b25808ab9cbbcedecfe467e163e3bd9d771a8efbd3cc414c7adf1b7d380 c11e17455d687b0dbee864879ffb41fa6dfefffd54d339fff58dfa99790a78f9 +524500 cc5a17d877467df92ed0b3462d0031003c642f64896f4019da641d1572d693e9 471a8ebde906603f5c054eed0da8b69aee53c35133712be35b22e5b4a8a3435c +525000 1b009804fbb8489d23d5512262d7536375873ef933c154a7058ec1c934f61892 d583cc3f0f7f01c67294d2e98d28e83c81824cee30d1252816a7828c0461f350 +525500 63de360342a1abd256b2f4a3f1a74da8e5bfff409282c8d06cadfea596220f9e 124aca94373c78ba91a8f1c01d89d5bbeded60581199d3b12ac66b76c37c112f +526000 25464aef8a123b8bc5dc4b9f6b83149c84511a88da41f7b5f6ddf4cbd340dd30 ca911108aa51b78a806e58f876767baaf341c9844f31da71959ed4ea2f87d62e +526500 54cfed1a647b8513e845b57381456d983320e1f6dfc26bdcd4e97da73f31bd60 a5639a2ead728587161e02add9b047738a8534d0725a84c5532bdd054e715ee7 +527000 d601736c31be7964de7a76ee1fa2c4771d804dca1c1a268b608f3c77971893b2 1831200e768d735e9bf8b5ddddb9119ec19c43e22b966bd969346814cf454d55 +527500 dbfd9c8fe9edc773e210401cb7341166fda6e1153a1fb47f866b919dbef91d31 5f15be969fa28b64f5250f0787876b05adfcd2c64ee1c2c72eb9724403ed6dee +528000 92d74a8d4df0a8f7510fab8b5d02692442d1fb8bb9046cff981cecbef3353585 b67baa2dd73a4061188c5cbaf13ed1c063e29193fedcac0b5440e042d39a45e2 +528500 b4a034b57dac02980cec5830c0718b0ce4a89bd3ef3f5f36e8b4181c1f42ace7 de92eb4a468c264b4a9ad63eb75ca3702af7c023481c829041f328797d2e3636 +529000 a05c9409ae06d5bc48284e5c624f8d6f6b001cc6f887d459043f442367e5e1e3 dcb56fadb166de779d9d0e4e5e4d30dd558b0ef3fba4f9afc8023ae5715d30df +529500 f62867ba72013c202a9a61da49b6e4eabc05e6d247f2ffd98235b8e4d862e795 915588450e5ec1300605afdbd6dc47954df1fcb45a6a10953db21bb54bba5797 +530000 35c57a8e961f11ffe1cf6712861b43b4e7d3cc3107c28d6d8722de90506a5b54 1c060c7a921b2d8567acb70356957e7aa044d8e37abe1923ec1ccd3f0f7191b2 +530500 0aa4f3b5419e0844a7316ac4b76e00f3230781c8d320cdd62dc1dbe21875dbbd a3bf6f2508bcf7bf8a6fc19b2a75859a8c2d3a68858d6dedca2de5a5c687b94d +531000 b65e6fc5a87f8d30ac2203e13e9e9fa450ada17650d6c639fde4f71d2832062f 6c4e276f9cc20e4111c9c45b760cb529b12e6f6f9d984c6338e47ecf0d1fd5d5 +531500 4fc020cbe21507bbfef6eb1cb30c11359f8ba8ed0fabfd67181b596859a11fee 736ebd1d9d616f4132934784dc13c0ddd6382616a501f11f84071f39bb794a0c +532000 936bf25a71f4f427a32035e860b049c5e0ecf36b0dc007608b31fb9a496b058b 9c412485687232293357f94ea0cf8a76c84e8757046eae59406f5ee225651a55 +532500 51b5d1a338bf32a3273b45d2dcabcc8fde6bfb68f80624624b8a0ccecfacf751 81559753a06dfa90b7ed984962a88154310cefa89233d47edce8438601051981 +533000 d80bd2b8d148cc346bcc49974171c998a30bad85878c6e508d16c728098d5418 cd8c0e674768e8afa69fc6366c3e2aa66ee31b8a10cceba75d3148857442c30d +533500 06cddea66b25e0387bb678836918cca4be53f245e84c01fdd8194ff147377a1a cc77c89ce8bc38a702e21b00df5e4e65f2217fd7e3d330aeaea89e67b2906d7a +534000 0427f67b170428d1d33242707a2a8273c6447a2762ed42e49580526cee35bb3c 66b923b19b1dae626b62a5c76dc9710d396b9436cb8cc549b87a4878f457e561 +534500 de3c914cd38e7fee1888806ccd3d4cd5d39b81cdd22f869cc5c2d5335fa29533 77b2265b37eef1e316d0d480f28bbaeb8faaefee270991fba6bb5680a65f5298 +535000 b4c2ed2b12c9b6ee617058231e9099ceacfbb10e344c8060df631fc42cc276f8 1cef714d86e11d17fccfa377f5550acbdf781a19f272cc8917c3c9886b99414e +535500 9515324744b14c233fe0463842b81e99c1c7bb28d99234d0c2126ecea9a3250c c96091825c57b11639c7eb8ac6f6c76d48c4507af32c1fc95a93fad60e5b1a78 +536000 ba4a67d76d58f76b3cb47b9f89792153db68285c94062df446d924e59ccf7b87 6208b177efeaa145f365051fd3d5f9e82dba8c42f1d0d2b67635c4a908b7dd17 +536500 865528966307aa0504e45931d75610ae56b4c4490c763198c1570a66f75b1f1a 09c2e4d9405d693c07371d2eae527aa1c2311db979f6d2d0abfdc5467b81cb65 +537000 c3355c3c49454bb38156c96fb23c13fe9e5fb73129bfae6c61ba4e6d1e46ef00 29ce20cbec7687f75730f0e4be6c672d1fcf96baa9168eefe3030116e7f06b63 +537500 895be1cd157309627c65fc37e677a72fd82f27d0418e6b43998aba564d205369 d5e4a2bc8d014079e205050f18f390c776f5b45940c5835ed995817dbcfb9da3 +538000 99248aee8ec1681099e7018b3f27ff7d48023639c7a16f846ceba99bfb8b6794 51589b8c95d4c555e3e85904368362e86c1ca65ee73cf4b3a56348352d48187c +538500 8ed90c82d16b675f2e1616b4e6877fea7d223f4c6049c8abe24071ed0cf55cff 0952515dbe216e7f8705998796c926f16186b5d911466f31d36255852c2a1740 +539000 5eee066175978a2e04f8070ec0a1c5e215afe79e6342f882f272e3ab3e5088d3 a8fcd0b0db8bf7fa522a86757bc674077b64491860fe1e1c346d48b5056c3625 +539500 1fc7f87c388bd6e524b114ae6bd9654031d2cede075e50c5070d5f7492238b49 86203c750603c2b64073e488e635049afd299e58642bc90cfc7bd562fa1372db +540000 c7c3d0e72955da1daf3ed29a7f50e39bbc72a97a7a3b9657151e2180b297bfdf d8789c0edcb9150d9cf88b973a2fba35c0e8a13a79e9f5e6fe67eb527f100dda +540500 078244b881a7ea8079cee149a21afb1f3e12513eac743435b26fca944d1f8c0a b0d2681f5339e6747da87fbdc8eea0947f09458e9e121fe715050d97d5380b1c +541000 c1fc5936f601dbe88f86537a73e3082d817270cfc534f985e6aadf1e69017c62 c5025d93f22d3c16cf3f1be03756c8a04da4c330a8779bb156b6d6812b65db25 +541500 f06fc398ee363680986ea85bba8897ca25e7d7398defc7ae5dea8950e5e10b9a 6eb30b839ec9f6b85ba6a68be9a25f4ad0d72cddfcbcf5bd4346e61905e1ce37 +542000 f1133f6d4e5f03eee2ab13868e8a296ecb14fa9a0fb0074e5fc3cc46a1f7f2f9 b1827592124a51a20ae293f4e824c8030eee78ee82ea039c27e427fe4d2da5c6 +542500 7892e32548d849918dd9946be8adab0f8d0842afa1e9f62c066d27924a96153b 421009927f9b4183293a0e7a57882322f87b30137239b6db48168b25d1fc5ede +543000 656da82150e03b0f6920c13ae9f012c768ad1db9da51f3cb36cb08f597c944ef 3fdc935dccb715960e745f505147ea8508aa5ba7c7f2a657a14ee46eec52732e +543500 9c3e877261982dd30c2c1c7bf6604e64b22fcb0ac6f1f31d133ad853e8b2475c 058abb454a8f38b333a57e220049716ddcf596ca81ed33fa28772cbd87968bf2 +544000 1ecd239bf3296b13b04cd7aa240769324beb8a9de6e35f10c193ce60adfcd2b0 7321ab6022acecdd7ec7526d7fd560c768da79151c01e409de85f70e8d9d9bf3 +544500 ab6981fdb795d866908290b102477a670822935da8f8af9c6a32ee740d69bbe7 c1eb309bc65e67917e1c46e7d0f232900974267e048abd48a1b04e1d4a3172f8 +545000 00257b2829d7c11f95de86dee675a7edb80dff9715bd2ab64d7454182f213a0d bf13fcbf0b1247b11accd940ac8772789e081f0bf1cb70b1425d6288c69b1e76 +545500 ea64a39237ad3d242cb2db3a1faebecd3b20c682383b014227a3f230a3345dd2 09c175ccbaa02745d5f72bb6f68a9b32cac259ecc88aa1f49bb30d336d79bf59 +546000 efaebeda777c28c7e35347e84da7681aaa14c502d53698838eea8aba6e55c1a4 79194dc97f5c84f2a55ca245e93b69b5caa44c6bd5e581044e2e26580bcf561e +546500 bffd9b66e5207f5cb60bcdce5dda495ab78fa6206aed6981bfeb3ab4961ed56c 2e133bc3bb1e140288f0abd4ac9fa18fc4e30ba6fa672f75cdea8fedbcf093cf +547000 b7c08bd8cd06070b6b52aafb511d26a3e4dde18e15ef004576de6c06024f2a30 6aa7c21876ee2145c6b97d41d858abf162abee2f179e94e354799664f38be6b9 +547500 77b94303d50c7c640f68812f0dd13cde9cdc2b43c5f6e3fa4c99621be61279a9 31c6dfbadc919ec783b0bdada382c4c3e1204008b245b6455679b08aa7f2a6bc +548000 bf973366b8395a897b56bb1f9160f8144c40ba17934fb14b46d2bdbb32715fe5 a732ddef1fa97dee750cea03675a6eb9557af9ed6314a162f5ebd8ded9407698 +548500 c2f20ed6a252cc714238efd5531084bd5555ab8ec65169a86c9a0ba97b2a7eb5 d029d1bf6706c1b298d9f7ffdd53a26b17704433c42683bc02718ef33d2d70b7 +549000 7e07ebf8ad3697390d81f7381637048377f461e46030c8eb1feb17eea4c9d3dd 1f2183d7618eabf7356a08a0486675b0163aafe13e6b27bb6179fdaa59685fce +549500 7ce04ee7a21eaffbe862646c3a301460da80cecad0e9a66a157bc1d0c335a18c cc27c95b13f1ece38002d33c7f55f371076fc10dfe1b48754e3418a0222248c4 +550000 0b717011e4c14ba25388b6be779ec7a7fa2939c68a49ad5fc8cccb32a3fc96e6 d95f6b2500300308ce3f44e53c38e6f0eda793b3e1d96f698cdfd8d70888e277 +550500 b1388fa70e3a79143b985bbe2587a8a36a3ce7f90dbdf93c312bf375b04204d4 2f81d9add40226b4b3d3f2466a2330e412de80bc31cf3242e0f8e8c1f45f1a88 +551000 b0f768d89c0f0e5929e41afa3e9711a2f6dd04c71519c718ec5a4851f13ae4e4 2b1fb05c01f17b3eb7268d552a99acd306e4dc0107985d6020dfaf4443dadd73 +551500 891da4a1108be080247fbc0089c4cbc2922d25cf2285538261195b41bfb48747 e5f1700d6fa46e80db5586d1f3261b758d374d43adcfbfad70ed5112bb1bf603 +552000 8a4da9058e5aa3a483af0775ddb984048bec5a52dfefe711d620a0d179856ee3 333c9af8ca1f58b6185db26d60b4fbb955176320995ce208f18aaacd277b4f1e +552500 f290fd729e75722394720a4b82e34da44d640af62c995fe6867fdd9167ba8335 e85093bef089d344639461c61bd1ec7819d5105b4f984d423041d035335e24fc +553000 2558178ee645f229d1b39f11e8bc8e423e6665ce3e7491ca195fb72d8a13034f 0a823b23f9e2761e8f32e42f7bd569ebb56869af3a1093d7a170f792605be7dc +553500 001c1d53874f0032ac25fabae2f92deca9235bd409eaac1aebec39bd798830d2 d36b5ccec99827a8a5d775d707a82836c0a10d13bf9199e25a3c31bd17f66370 +554000 e2710db357675cd18df933733e23992517017ab0507fa218fb45bc9f5d812224 786326555bcb406ffe22d8dd499684db1b8b70df508572f1ada7dcd6eeae85d1 +554500 cd244e1a5feb9d4f3a6c51f92f1c9396b8871ac5a20cf587092abb68b4fe2eec e7a78e8e16dc73cdf9064f039df1a67996bb3eaa744f2fc0598bd07519078b24 +555000 1a6ebd86249e3a3837d8a1eff644e240333704c7f9c48515e849578016d8fc8f b5da194a23915c042e044b0d3e5e1856f7640ac8344a013d7a1c459d967d6eb0 +555500 978b357011addba581355b92d3faf71397a7c55689ac4fb0d4a6eebd8409f714 519677a9eca60cabdfcd5e888ecb570d09275bb7e2585e9226a9b664ad0d7fe4 +556000 edc28b6ce373cf5a5d13e86ffea09ac385d8d2bd251ea07617b10abade558e12 7b3f6c61707185443f2565ad2f228bcb006302a1bec5a203f143e730c681a9c2 +556500 95ae0bac1e11d4df7fcb554f665966b3bb2195010c0cddf48d07803743149578 1c9c7ca6f9c8b40134151daca7a7cd1b7cc24deac24aaf2f3a450b34b74730ba +557000 a1cbf8cd746fe3b27616f6515149cfb57408d82b8147c19510ad13806b7845b6 2287f21d56991d98d17edc545f93a85d01b7919fde25d726cc9cff6f62d97461 +557500 1cd1617ae1474529b7b4f322db9442763fcd418410a3c3e918e17f13966491d4 e71635aa05a9aee0ca857b41574d348954419959f8782b6aa3b7a3e0d3b6fb43 +558000 ecca0843b0d748735413e34ca513454ce2497271cf04a9d12455f0bf07c09f22 c3a7537c5ee0d6615a53e3e30cdcc6433a554a120756be6247f3c3b703f023c9 +558500 0bc0b2dc367328ad409219afddf852412ecedb43e048ee73449cdfa5e152159a 64f4cd8ebaf6f492c200b11b080c521f4efe87e777818f9012e969eb2dc0fb57 +559000 ac95a1595559d38fc68c9dc48d9af40b3e31d71c292a83998398af38f09b3fb8 a65b3a0e37cadbb31f367a83edbc4c70d0537633af498c9ece211c681fb9f671 +559500 06afcb286229def7721d7a5bfb22cb59f1b25c6aea819bd26456d5fe0baedf74 b871a3c694108b366de593c52d6a1b3c38af1849b0092e5474275258239e6088 +560000 538bfca7649306c7ad77eefd31a7cce9b81ee7ee9d20d97322e9f785cb555afc e08954b12673a8e87fc4db6f848c2129d39286fe856ab80f3a11480a82772963 +560500 05fe497d72e403e9753264fa5d4a7348843e07afbd260e21fdcb9898d9de63ca b538d81ebe3d0367d4820f155315debd0b078100fe17f3ec6a0c1b1d989fdf8d +561000 b80da09d4fc326223399fac5659ad647dcf37232eb63546212c7b951d429effa 014c43fc1995d966785af2772de5f8d4738b44785a2bd81145d32236931bc978 +561500 998927d54433d0ea4fe19a4ab0eb8e08ae656e8cac608e3a824bb084967a37e4 a7709319ad385d1345ee6171bb57bef2d0a648278e6cf763068496eca01d5315 +562000 a005e7c48077a108e353402f478f0f3cdbadeb95f360f6d4d056ec8aab302f7a ad678ad8a4b421f5211b5953c3080ffde18f2a395f5f77a467a6a3991e155b97 +562500 cf4a618bbd1d4eec4f6ca3993fb2b3ce901592c365c2b7526c9de2d0c98cb74f 05921bea10fcdc426b73459e8ea0ab421c1635c93b60ee8d950cfa0efa91a126 +563000 ffcd676fa825e67eb4832cc72b5767f50fa306f1dd251055ee3f623d9fbf4d6b f3fcba9c0dafd172664da1d0e692cb1f4e898f23eccce5a265104164a5ece2d4 +563500 5f96af414cb11afbbcf48a7fd1738fc1c8ae91dfe58ccfaaa7149264ce34c5d4 54756714dd75d4cbcf87b452238296c5acbece0f991ff2cf3b239d6af4b7f4ee +564000 0345b35925619ae8f774196dbc6ae52b2d065ee4ca6f9ab87aac366307e1512c 7ccabfabcd36f82cc3195d587e6b530f2e303525e253d84376f02b907e1bf611 +564500 478fd153799b8afd93360f4288321ae7e82aa7bb5474f13913274834bd4b1ba9 b73b7af475c03ffe2bd0e9b09f32a966126d650007f991cde96bc8ae208d2844 +565000 4bf82342209df889c321571400c9170621b17c9b88b60b609a7a9a31a4e6879d d33e0ac1f8f8614cfec0768dca67ba99561976ef142a62171bd596ca26265462 +565500 18ef43d2230a09c588644154757d4c3136a0dcebbd1e7169349f280be113ec99 d3cff22fd147792fef86c9d94f7b8553c555a925e7038e738bde608c83ceda53 +566000 178cd3b2773545e457ec933671f4b7e0cfa269454371789d79c4c73d879fa497 776636ace5ca4a51abb4f04c22d5a2a0c51d42368f43b8b776299b087d1bafff +566500 6e70ed03c1ade6968a6f18aed75a42130abe4674a9cb3d6715d5320474a310f0 9e51b370f5e5b6dd65ffe6fc854265dc071b7dddad6a68e25a6082d60a4573e8 +567000 6a0b1d677c5cc0ce3f890ae8495c70403ffae874414fec9e3ec8b3aa73bfbf12 44a2fd59a3f18b4d59d7bcabc842bc21681b3c82613c95038aec10315bfdc050 +567500 aaa507e4d8344e885c748389b8812c28ab49533b13fa635bb345a4495be34850 f0b2d2e0f7477c8703fb6c7ddaf4d22c17da14d6bb1be606d96b4fe23932ff61 +568000 058e126ea94bbb99030e2be4364db58bbe0703c0fb4c3155ee7c386e19efe976 e68dcd0c11aa8d9b09161d9fdb25196b0b02017743235c5e77c83314139abc6a +568500 369e444ff13a9ce379c2c98e70e2e50e18c823b4b43d51e57224f9d7bf7c2a82 e5755456750a9a92ffefb86ebd18fcb6d4f790bd51e47f1dd92d5d0db8c60b7c +569000 50685dcbbd12f30781fbb546d1a4cbc349244a2d6e14ccb1530059e4073fc0d4 5841f99c2648c1075709873b22b8bc25695c01fb146b0e82291b6a0f5cf16fbf +569500 452253574aa43c9f3ecd39f7122789bcb9c83e343843540860b950ff27d673cc 2ab70f1d2aa896e8a8495df628e675c983e45047d719a9e8fa0804f34a8f82e9 +570000 c1b6627d3100900dcf795d2b684e83d11a797d2302a3bc058dab61ba109e5d08 997761e1d03f77cee9256dcf268f308a14899d00386cf1cdcd9df39a57ade810 +570500 7cd59e7583d4c4878b1dc3171dcbab4832d2c4d61512d7f313ab88782a9d9336 0a439cac39577c5f0188a201ff1ed831ed2fbd5d819c6682d7aecb2e4b77b7d8 +571000 6aa099647512a9654c54e730e3f1d4a1fda25ac58387503932ded2817e5b446f a629a59a6420e7edf13f0a7dc05a918f86f9fb337459a1f85b65cdc71b301d92 +571500 a3d2f0b075cf3e735e586c69c3eeaf7a07cf0f391e1a6ecada1327abe8ab8c8e c97c75ac8bd884337edb7011ce4a68f31d1a06f477d821b9e473614f871e059c +572000 154d5a1e996a34d40d3aaaf2244d5df055de5902201bbefdff166b63d440090e 38fafdb36a68381be6d3a3f358ee61f1998c8647949eb6659f8794c5d84646f9 +572500 05d47c51813cee5f42bf7803a627ade017772b7f4e734fdce5a824edbf480359 ce6eeaba07d4a7d43ae137f3f66d7bd2c3ff0b55b973c0e87283011f58f94fc8 +573000 2cc24da51032f6db15842996a46a0f01e17e7c1fa178d34f4d520e57a1dec5f5 814f5511a95d2112e9d6f5ff9147e4af6d2d14c674008901888a59d77adc0127 +573500 5a412746451823e5fe7cf63b331c59b6b1808d176c1f3562631755a1aec62267 a8a55f440729934a987fa7bcc21e4bed9ae4cb4fdda5bba05f328d33c1c93dca +574000 4e71a21262bee73564eed3938de4c2a673ae763450fcb183bab52fc3739e9307 f5fd0fbd347c9146a3356914145926887833b394f0664b2e5d098660848e8b5c +574500 bee3161612969794e4cc7811c35fb9fd844623da6a5b5f2dc38f32d50728ab90 d97f6edfca11c6cdd984f126252ed5c39587feac95f4d412a947fb327ee9f4b9 +575000 0560ddebf2a1fefe36dfcbf14c0b9e2101445b34e79b4d2d2f7c329c1dc5c97a 58a5850806bdc8a70223873c23aa3a5975634c989d8ef4f4f78fc0b71c025a59 +575500 077c9db46dee33b8fed0edbe55dd09ced1c04eb22096d9a7f0a8238b764a9005 a527ce25eff18c724535306c76670e1a44b010183ad970d41e4168c7e47e8ef7 +576000 f9762a0e8b01efc84c09bf94e0eec3168852cac22bf5e0e657957ffbeb857c08 41b866640d3804e09ffd386d7f6b0cfcc5e95c65991fa0b753c875676eec7c37 +576500 fff9ba66ac3dd62ff0931e644ac522896b5e86c2a23e04bb4f2501145dd96b75 def321280e76f1a8aefd6607ded8cb73769de57f19c641b7c6356e60105d89a6 +577000 69c48c7908b7b71bde3ba3aaacc99951cccbbf4ca98fe699367cda9c2101efe1 da1cfb6020117eb45c262bdafdc11ef962f6156c0c95e68cee1bf1a2b7b9a1e5 +577500 9c563371fc767b2343d7f1cf6f23e4cc7b42aabb564e94a5cfa940cd84330cb2 33aaa085f7076d2841dbce57b42bb7c61739c76f041c130becc350efcbcb5138 +578000 e5b36705e126028107e070cd6b748d0ba1820ef3f0b7cdbd8c60a822011afbe3 b2797c8eec34eb94ad7b5de3c93144e6511d79ec389cd2b3037abf705da704c0 +578500 0bb4c3b62c096285827dd7079f91bb367f08fbf323f01cc27257c6ad1b6b09f2 2709c029d1cd3164ac618ca33d9453913f52247ca266755995eba29516fd6693 +579000 62ca69f749191230d6588250d41ff452312e141c30bdc253a4ba473d3736c8fd 9690b50bbb4fae4acf7114beebb9c8f012328638fbafd2f54c18db4983c019fd +579500 ef36ef78010479cc9664a2e7cbbeed19da7b60b990ae1d6ca1c62815b74e4ac7 dedc37c50f5ee81e9697c49243e55aa6ed5c5ba8d1d56305c5120b308600fb6c +580000 439f43597ce36aa161cc2c4cd7c25607edada72003a7fb681be2f6d8b714d14d be5636a23710b20c61b3aa181bd75a4e0992dbfc1d267be7c66a3bb459ad10cd +580500 c6b2c13db62622a7c3ac9e8072840de5d24c34f6337fc3d8f826933501245deb 25eba3116b640e04a77b843ad79a25847d31c2c5eef7da98952ebbb526440eba +581000 4dcd0d20d94245063d630bcf6d8079cbc52dfc522c0e320e39c16ca18853cd6c ee3c988f0ea99a39c2cf69e88ba22aedaa2710f49bbaccd1ea0b2c1ee65648d9 +581500 44b084102871018a5db5f990085f3177856a38a8b727a36b29837c34e4a8ce59 6d6899031aab90e68c34f4bc3c1458960c7debc0e5e88de13bc0d29d9a4f1406 +582000 4fc26f1bccf775fbc7314ca86532fffb93b9be94bc6f1bd75fbcbe688eb9dc1f 07296b68ecad02d38df4dfc0c927b430ce4c978fd42772740cfb185d1f73cf08 +582500 fcdd7ac7dbd7a3ec5bffcf498ca341ea6f45eec4f07fcf5d838805979c154e75 1fdcf21608195ac2f0076e8203f72e8468099613e8edd350440efb4fa495de35 +583000 d32c77f1ee2542c91734ccfbbba36b35dc4b76db17374b52d514ab45e03031bd d7414a5f1b9e801ffdd889f95d91b381486c2b7bfd718907df3e4f3c16f831aa +583500 bf8b25a2a0b19d293bc739d63786a6e5be7cf111d60d6cfea462dc2cc7cab22d c03d273bfa3ebeff31d43a7a1fe34b51a5c968676dab85910f61691ea66cd879 +584000 b3ec434425ed197a317fe360d7f2bdd065a7f167e5c57bc9623cd150df2aa5bf 42bd629da8bdfeaa2aff0cbd4092f1db54db6da4e4487a7460ca7081849b1d01 +584500 f09dc804f47fc9fbd5a391883d0be1d3ccf5b71a69e9c2571ce1ff55b83422df 28cec5b73ebea11f29daf62db2ac9a594a7497ede4087e0932881d39343d15d1 +585000 5874d180e9c48959e292587310dd81134eb03197f0257be1516c7c50248fc3b5 c5e42637b4e90535090b09f2d90d7ab90ce46eb3255ce6abad941ff07ab5e0cf +585500 a6dd4e58143b11d190e168b1dd20fd7da47ba2af0d089f70bdb5ab423c8f47fd 134ef498a9bf87099abd879d68ad7d67ef4dc45b63bc16438aa0f95092e34c8d +586000 5fc5f5de827b01b620304dc74ea53a0fea4f814bb51ec5fe3b6c74ef560a5917 e1d0845de83160f08be85d114882976278106adf7a3c16ce16ff6f08caeeba7b +586500 2d1270ba120a5cf1427736d7f89cb05c42af508e0a0038833c9445dfa9c66fd7 be7998a06e1fa3910cf77a7cb385cbc6933abee919664bc52a52afa5388c5b0f +587000 47ae47f30d7d51e5b874e5ae103aa38208203f2a380b624cbe1ba122d222f9e5 81558ea382a62cc93ccc002e8ba9d88058ebfe683b05be16fac0d84612ad2152 +587500 a558bd54422bc5d0a90c20310f81d9901a9d3956f5d5505a5dc50b6459ecaffe a8994820ef7c6f2fd8d9b6d710a0f83b537ff6fa1368060418cb00ddf642f357 +588000 fe5749f9e8ce47f885f4335cf2b24a6659913ed62e7d5af026ef0470004d4983 87e2b4277260305d5bfbd921b2d4592e180837e092f2acd3896621cffa5228d4 +588500 4c798283eb8cbfca17647b16a919d49b77a999d0bd319fa624ed665b30172e21 a4c43d466e6429ac399cba93e1a594703176f7d1d7f86990936973120554c72e +589000 f8c9ccb89cd751d66c9e2b053cdfa670556f6dafac1e67a12f8bf06790c9fb93 ef76c7bdbce08ea6dc8dfbefec9d64b2bbf3180dc4304ee7e39d8c098ce3602d +589500 0971e83e6a813fcc290f352ada5422a0f48b12e15628f9522d1ce40b2fd01e85 cd5ac7829f9c4de5b606b3a5406383ab2634f09661977f867c47d58f89f16081 +590000 cc8a0c71618970a1cd40ecd78572568e8522faf1c6cdf597def52feaabd15eed ede2c72e45aef2c244f56183b127ef08f9137effd46386eb8013f1435de42f08 +590500 6c876707aa719eef0d5a9534f9961ffceadfdfa16e75a4a8c7f5c7822f8d9945 44d6b98fec0842acf25460f93ac90ce132d3600c752bcfb18d3e4bea90b9e052 +591000 5c5a7be46768f7b29d8912feb8eacabb2c592fac374fd8b1c9d74124a5bde94a b725f0ce70d650d386e7fae730da1d44a308d2517c91e0f6d6fc659316647202 +591500 4df0cd912fb5772a508f9ab6fc5dc26ebcfc8e5fda9026d682cdeee3350f7f99 1a9e6cf241740c2f4f615bf2017473e6eccf52ce14ca3c244609c83411749374 +592000 5a9465ba2bf3034272ebb1a8b742abe5b66607193dd04f838bd9a2e9525081c3 c36756b7c0b4f7577f33bb7a6a2cb06d444e5bfac823242a2208a74dfa4a5264 +592500 8e712cb434a6086e1068c5a36a70ac8382f8bd2df8f3328e869b5f74c4d3e268 1f6f8f8c7b6abb35e1bcc30443a2112a1cf6711f020c6474ea2b987e122e2710 +593000 5d4b59deb55edff702d98ceafe7e74fb2c27a9e82b3b8f38a747c0013210125a 397321c39e0255bbf2f345d8c410a3a75f0cddf11f90b04e1fed22d57313f436 +593500 efab77d82b76d1ba1f130c61a5146828ef22808cabae00e8aaca746c9701756d 5ae3383c918a2d0de41ef7e68ecc81a14448ae6265fec8146377b88c14678583 +594000 2eddcbffecddf8dd19c6183606ed678b8d3c80b3d18a03d1f6c98de436465bbc 313ed4476687d320b09ed8f775c84f848b315ec7051a819b7b5db928b06b10bc +594500 17c2823062fbd036efda8d7a6cf9f02177470df9ab03a1ca6a49a4feceaa813e 967817ffd15c1eb09ae18be9c89af301d4faca27bd1435a4c1b016328efd7452 +595000 0b2d83d89d4bfcf2ba0bb0d6f4f002f83b33532a9c72d204b84c3624b82d1d12 ac1ae8086c27ae5dcb5491883fd2c00dd1936ce5ebb753bb9721795b51ac427e +595500 94e93ae2421b77bdb8eebc5a889e0dbf64bf0af3a9d7d718b92c626186186adc 58b3debd63420f3503c3f813e1fec6e2dc9de31eabbfe6a5517eda797c5a1381 +596000 e556867900ceab6ad0fb7923aaad1331b8a44fb432ed5479bc8255a9f9889db7 5608f70ec745d7dd10b0d6650661ee7db9d15ead665ce8062892cdb60c51b81e +596500 e07b8c8db8312393c02cfe1bc00a1a5ee6166083702d2db0fd24297095202f77 3df091b27bde63d70fc8d0feb4da1e97ee21915b24f6be2a60bf702051c243d7 +597000 8e07ad6645a83dc63b311fb7d39bb583980164e40032a0821d0acb50953b8ef5 96c796666a2f4c215410baa022874914fd9c04a430313d38e98f6c5b743b969d +597500 6681cd271934d16f89af3d71bdd886f5d3d50f6ef9fbe17c4d95eefb6c770974 e7909149c034c8ea84b92f77b39a9331560661cb4c158edda770306a4ed7cc9b +598000 92b359d658152c31b8c2f32c4509f3ccb1cc18b4544745608da9a230327c109e 3f75a2b9ea70de0fdc330ebbc5f83301dd507c4e0fe536200402cdf30fd28a12 +598500 be474d552a4f17ec673478af48534ad3c65eef1752223ed3012534dcc4274db4 9c304d0d24348fa013c7223f1e05fc9092a773e6da14920e628baf05669a0f08 +599000 275ca25666031af015670734cd4fa02ea0e0b099921240991498e684bc234940 83b44eaabc822a3d26f168dabcbb175357e1a37a025813fe30d5e6560418dc4a +599500 f20a2a85028e0a3c65f118e50296794ae25821fb5dc0a59f415305d826a827cb 3dbc1d013d4559da355fea928c27fb17e921124893b7052f2aced8c8ef9c9085 +600000 fde9cb0677887e2f966c3be87d1780db88353977fa2e426249d9b26b01be1db1 74fc8f58d336eaafad2f515f0b5a5eaa679c82160f2dc54f35fed44bb845c395 +600500 ed2fdfc9d3b07cd7e4d4fb46b5cde484b7ea5cbd436608698ba0be6407928d45 78eebaf2d54a3f5ef9c98b58e75325a263d7040a85d2757eaa6fcd9edefa8bfa +601000 3a7b3e53a5c38bb7d0600352a71701c8c7b961a9936d319ec1f39eacbfa317ea ae8e3de2cdfe471244ac6698534f60a82a93e59b36ff22135ad57f5ffa9eddde +601500 9ac84b004712eedc9cb82e4eb4f459e92634ae20ded19ce2a01b5039bc9be041 b1800a6430feb20276c6d1f65b1042d5da3d547a575d7aa8f8515985e793cc8c +602000 e584426ba4821ce97899bedc37ebdc5667d94f796853b5dbfc8597123f61cd93 670512bb3eb55350a01e55a614addd9a1936e220c349b5fdf83ff85d36ee4762 +602500 98c1693b6c94cebd5e17bed36779a7c1498f43d83c3fb9f833ac42eb3b1f148a 4335ab6f363d6556b820a8527702f5b224789aae810a9799c40289f088d876cb +603000 cc90f10fadd0c325018a7f5b2819ef126eeeceb29cfb1abac3aa4d918dec8ce6 c323abc97d8ddde6d935f4c4a3748fd5df141948b855062c0d557814b6c57582 +603500 88a282051002196bd7e47c2d4d05acccc49be9e0a1958c608bc8857b0752d805 4a129ae3447c19b8a4ca1812fe819cc571bae08dcee26b2f312bdb2292e7567c +604000 8423befd6cbd718339237b9fd01a9bbc7de101525a01e45777cecc593795e76d 10125b30e2a5762ca8991b3ee699bc69e552b65457759c8b0a85468f52575c2f +604500 5f07bfc38f07c663a551462ef3fffbbe9a1f01d54e4987727649b2cea7ea3716 f17db8519ba7ab7e3cd0442a008bd05dd5354ddbfc696dc9e2aeb07385929acc +605000 691b12ae780daabc6228123ba85e5d4ce2c48d1f6084a428ed5d310d644ae101 15ecf51f40220aff461a2662902a91b0ff974c6b5dfc6853a37d492ad0e7c507 +605500 52537e09d147a2eaf5a3e843ce45824f3a0db686f36cbbe72f69fca61a2b1c01 ab643490828905c7be3f1c8afe6f7755a27c4cdfaa5c8488fe2441aa6afaa70b +606000 945062759b3b417205cfff5d9b2e1c29d29fdc235efc71971a5c930ef5fac2a6 5cf40892ca34be01dfa549d94b8784051375b2949ba916931a8addf7eb3a9216 +606500 284cfde9bc92fc8a9a75636ed0bb98a68a7d26fa82acfdba09391939c2e9acdf e0d8d1c85bb390ccf48b59af60483c919c08df0f8830de5befe65a43a53a99fb +607000 fabf1c22a50594542dd8cc8e0aeb6ce78992e1fb97dd46f25b1f766392b8c466 c42f95954963685fb338b1ebf0f0de3c8fcbada7fa53dbd4c0472b7cabb75eee +607500 a2af7276cb9398592a0f0d137875baf3d9291a606f583b269540f9ac44a17ce4 b5170fbaafdd9059ecc043f1c4ca714de50133bee7054f8c119e0eed79e20ef1 +608000 6a78949783e00a8ac3d832c24e69a2c6fe028f110e8f1a7d87d652acfd5b6b63 01ef19adaaf1a1d75900879f2c7bd5366ec4a6811ee539db16e81a2b981c192b +608500 1941dfc27e96aee1848d5fc8ba94bad2507ee14f8a24235e4b35af5dc0559f05 559954098665a3b7a698445445daeee8967c092ce38be59cf1fe78ce354d221c +609000 ddb824a17fcc47e7312cbc0b80838b8f4fdc077d1f8cad5b08008aad8a25afdf 528daaece3c6a912cf0859f93018845f781575fa67f27479ccd56480f40b8c10 +609500 3f80a004163aa45d2c9d2a1c8bb238a15b9db4214c8b0ca08effb0f37697c40c e6a2eb1304a43261fa54127676cdee58792115ca1b400c4a08261f1e170f19fc +610000 0362121a825d39875de7eb528be247603e5d93922f21f738f255ad44171f5edf 3b57b352f45dde9c233dae766260ea380cf6114c7682dca95566e656e9be1511 +610500 d39319109dfd619800add37b85ab545fe8832c50036eaf1aea5489348e749f18 14c16bbd895be10be482c66ae8444f39cfd67f9c3c244f86bb3f309f3f38ea3d +611000 274d0b2789a39a2808a69e9c7d4fa6b2b34b7be5ad55d541785f6230103a6110 610d4811e3da6b704ea224ce3cef798b3fb9495609971c09971691d88b714885 +611500 c25930007a1325f971285a32257362f90f6b5da474225d3cdd19b9053a55e6b2 c052c3e6c7b866d2212ecc66d4956d8b3c0a9b97381a822ed203ea796ba58581 +612000 8dc46e21fcf1b95e59d6be6454adf1db9d141d9e78d583c0f4efa47e0b2e38a8 538c9ffcee5485b309bce281d6913a254331647ab43d492250c66318503ada14 +612500 48c70c688ae362b4ce2e19325b0bf5335301f45cd6f0f562ff263d5d57324a2c 06f90f226b98bf0fff455478d39513800ed400dc9dbf41c3a6e498730c883e42 +613000 a05ec2324bead1349845a8000c76ff7705e0ed1f958dfb46c92eaf3180f301f7 4d6d1eefb176cf04b6cc0e9554cd81bcfb00e1f737ffe5e25e752f9eacfb20bd +613500 6d0183915fa23fbe61d2cfa7dc9ba9c54c164b246d9f3ea2bf28c675203896b5 e233a4490b16a593a62853877efc01bff2e03b4e5017027fda754056171236ee +614000 dc4d72e154553d04502271aeea36609af31e967faec981fdb7aa7686b194ef72 a20198e44d09d5a45d18b1b2e20911f6d8840ae7618e39f1805dead4136ff220 +614500 401187c2be8053257741c3c78fd751d5aa80b74239ac1a5fd7c2008623b8056e b3d69a8f679aa779dcfd6b03d44da4318c10241e1a87c8de46dc2067879e7561 +615000 a7edfae28117b555c502b1b0866e70c2e45b2b53095b0b3ec0518f76a3250e96 180c90afe2385b2149b5622d702e1ae33e1908e03fc9ac457f18cc86e3453813 +615500 247cbe6b3a28a410c816bbf3e344fd707f5b98c9fc7ea74a653e55daae43aa7a 675ffec485147855367994e9f3ab4ac5ad7bedfb39a35959ced78034e9a04318 +616000 091e39d1de4719fafc22075635a0ed10d1d8b5636a21927557b2baee8bc453a3 5cd5dc9688dfccc2024c771720a75202758606c6c2b62b3317eaca215338eb9e +616500 245d68d439cb7cc9d17d41d8748be112448c1c3d757746c28c32e3531a23a66c dddbe95e870b48d8e1d3be72da7976def8a5f96e20846bee2ec8d3644de876bf +617000 9a08546acd2859279fbf0a140d792be72040b3b5deccfdfcc3a40d640c60f35c 3fa1b0094367169cfe3c3047b01a8bbe5bd1179ea5252153da7113477dda7bb6 +617500 ec4c7a99f349f9b610a1c2c94f01fd8e37bf98d8feb6a68874517a47b27a199f ada5742a01ae9d78a4aaa0fefcc2542633e90bdd178f540e802a4e5bf96368e8 +618000 30bf0bf59fc693f06b630ff38f55f5788f24b27fec1c171fd5f63b6a75729f83 a4d0bec878322f07128ea1f7318a28c492a27649b4e216332589628ddd9053ae +618500 095af87395d73d93df9f2531ad54c6a486b0c98fff5e59a75d9f468e2317b989 c98bdb9ed3f618281b60f496fad0753ed0610f95899058a5662a45bdc30f2c2c +619000 53c7aaacaaf469764119760051cbb7be6806f80d51f75099d8096831443712aa 8e52bf819476612fc0943a9b56d5027935ca897f9a5dfb686cbedb8dcdd83bc5 +619500 7e55322d59a65d0ff1be11ba083fccc9800550a273a52a77926d5a67c383f429 1cde548b5fd12717415c6f8ab191b0a73aa77acfb6c8db2ab9ef1f374bb8fccb +620000 ce46ee172a243c059752e237dab267c4c95b111745b808bb9ad1f8e99dbe84cd e14873bf1154683503d0b3a1d0180742f3eb790efe5a968845a337a4877f9cc8 +620500 31fca5e075086e3ce312c78e4e67a24a245c48db04b7d07688a46be3efd12560 d5c066fadd66b4053f35e728308e8e1e9594693f9afe8f930e342f89014e1435 +621000 c27deb337c86fa6c1a9c967c1c0e967d66ca91c3a605ee42ca9ecaa3c7801715 4bf8d5ac8c9c275f434ce687e9aa2e5bb0e1c1a8634ad7dc0380ad74e9f2a471 +621500 6d6de5c0c183af66e09203c1f851e005b11ffc3e65821a0b0c53ddcf0577a86b f8e09ce62f6793636184630998b1cdd6e513174a268fedfa3c23d1beedd6f127 +622000 3bb4c331c1c410066bfc9c84fa96f35f008d7ebbb8630f35616b07d3a8f6bf09 2625b34fe7f2c205449f2ff67a4bfe036ef5f479b520c16f0fc16ffc43e5e713 +622500 b69c590df86c64c53eec4355ff7e4525ab0ede7c40da8cb6304556c465708619 ea025a51dbfa831f98ebb1597cb43dc29f34d18f5522b57ac06d7e19bca1ed39 +623000 248e12a541844945c6b91e45f24d7f63db3dfb41016952e628c647bb82562c91 d2dcaa98eed5db5e34581c8bc52808819c5cf98bd564661252e24fb7f98218d4 +623500 0f6332963afce6ea94d654d103c3604c724a5a6bcdf6e32bdf9a708d10de519f 80f5b0685f010693bff7453273d0cbef3d878e9753b19e05a5d2fdb4984f8a0a +624000 3a250bcfa462ff8b750d0a725962879d47832f0c77b46dfcd27e3eb4bf352257 f75082779f75cba11e26f60eb9556caf48daa15221abc2b4a83a1cdfff9368f0 +624500 6a0ceb40f1277b4289334b6cbd593aa5bf593160ea960b4887e40afd2b06d767 54386a3f312db13d8151cdb5ff628ac2e9e13d897287b553d581a6c74cdad388 +625000 c28164fd18e1087787d2e3c7d67b8349aafcadfbf815fd7547fd7ae8da9e7937 319cabe08b33898fc3421e1cfe329711de03a92bdf21828a59594e813e71393f +625500 00c9e51e1237a00f30562f40f8d7696876a62523a7888f3019103f97d27c2afb cc7d1fd4e2d9a95e3a9a7e23f44a96f6840036265a29cac5b4a364ae1ee1b806 +626000 452cd22905dce67c27114fdc0b6ca64f4127abcbaff999b7aed68be214fc9313 c969541e23ade41ed8ab864eb6fba11ab9ba5c6f29374e666af4912c2c076de9 +626500 7a3f9fe36cb72b3529747754309d2fec46a8155a6f297bf5d9998336ca26ed2f 9184cefba7d61de4482fb57cafe1420eaaf58e3f2fe370e9c22f2dc235c26763 +627000 d8f9b2f906265aa50c5eefed906e89c06856dd2c25bfa2df52f402fa029b6c6a 1c5dfbc7bb572903d6eb6a400fa50df3c18c87cedade5b4ac63be7037f050a7a +627500 711651d2f7440f20927ed66e680d833755ffecaa905c0712ed31e1e06a81a4f1 6de06425cf412c7d272841f1b3de2652b15d3300902c0da6b7c1b08bf4e14112 +628000 1f04acc62ae096325174e8ae11579d88e65ab4f435e3bb8c41896a1cdee6e231 c87dd969f7dc7a3a356d7935a69e4c8a4487c72e7c623f49cf0f60cbc438aded +628500 f55649153110900f0fa6b7d2f9dd6c293bc44ed7911a6e4d52ebe5563eb3a4e5 de1bf3695afd590e9ede9881a431abee861f4cac9ab8b42cfdb8c37f0e02e19c +629000 8e7208c5c406f95dae4ffd540aeb30a111696ed10374f03cdc186db66295f054 5a8b55bcbfe3fed8453499ed4c4a51233f65626132aa23ff6acae30dd5cb3800 +629500 30b5177f1554053efca360e8acc003ef0cc4ae26820331715fdfeeb2003daea6 f7e5d1c5ed411c3afd3a51d63abc4f29fed49ee8bc2f810481491c138cf09183 +630000 9bbdcad95ee5cacc45ff51ca8d627ec7a971b1040368ac62a4616f873623cc79 306b7845856f157aa25e14e34165e75ee5abdef7337cfbfb9611f628287b83b2 +630500 cf2b412c6c992847944b1fb396cacf61bcd03aaa460cd810e3f42a2866e8d729 8d2c8d7be61fe16d9f5c0f5ea155eaae7f926fd8011209de27e41cc67055f5a9 +631000 ea5e4b4acfeba1f4a69c63647cbb45147ba1dd101b9fe58843dbc6fb09f7c12a 2ef37567bfb65be1417d9b5914570c55bd88da0bc07f7ef7389e6324cb0b528d +631500 539334450cd6eca49473d63e4917725250afbb6a546a025b57b40e4ced8ce63d 865fdec23689d881dac49ab8fe9a20c5984f42b4be7cdf3e94118a4baf4fdec1 +632000 dfdc58aa41d2ebe7751fe2b3ff90f0496e602cfaef1fa0f1c25181a3be4cebf5 8e3f86b2b753e8f342046b61d84ed4d2e9ce107d7f30a47361e983aac874f448 +632500 54f71b9330b758d881d325e8f3ac9e0f042d8ea1222e52b580a0337a3b4f3fde cc0ff7cff02212ef316bb6fc2a131b018fef8b6dbfac01b9e2eb275c9008c613 +633000 67de40386baf1179b74ea2a750bd20d6de8f6ec3cffe5100aa3307ec81bf3a4c 7d69bc05bf967c291b2156c18c0851caa853110a8ee527bb9872001cd8179b34 +633500 4414265aed7c038b6f7ce24cc86619b0596162ced270a87c70a59ee3089165a5 09970b09ad4958924d24703ec56fcf49a7526a0916cc7f0f841ed9796bff86f0 +634000 a55d577e338fdde481a3cadfea26ee6c1777ddd6e35e66040a4d99455626d9c7 5da3e590cd7190a473f7d61027754e5bc50545fceb8867db96006d3773a2b746 +634500 571d3172a3095d0a34747c18c2e2cbaa4173a09ae3a351736b2b4802752cd9b7 6799dc3c2b9fe39f72a22e6fe27ebf46308c887e31245a5cbcc2a55f1060ab44 +635000 6786a43fee0f379b103bd594943f26eee0f0ab3af9dadd36d6afee547d7ff8b1 60794659a5de639280735ce3ece1864a66e5754bb1608b2aaedb9874a6c34d51 +635500 bc5896373cd6ab14f28a690a7d59366ee57a625cd6291c502d64a3a9a5969a96 cf6d7eb9c0306f24bd7a47fa0660804b633f29fbfb647e79d03a545518d5ba17 +636000 1aec8a1d1f6f5abb68d49597795c24102bfb767822705205486829ba50940cb7 348625a773758822cae41638d27628bed89a118fdff13dbb773ba28471ec914e +636500 cd844ea294ba265e14ea515b603d35c2dd13c9c402a4a1a13628925634e81f23 2c881b78d84e98a3a6dffa6d4e55c347947053f184c22f2a75bf62cd779febe6 +637000 7f0393f05b3ebfd7838740f3030dd915d2125e9546e39f503e288a20b14d29d0 ffcda35e368f4784c75aa0e6a98485a2b84fa1ccba37f7fb5a6b4d4e187a5033 +637500 7453182caa6345472aebc820d839d70e916b8e01527e69d2451e68df9f5fff94 df9418647903ff269234df3583ba76f054c7fefb3237b47206f7e25d9ac118ac +638000 2917816bff7d73f4b4f03a31fdbef162ab7b2bc38e2539952648f7e9b66dfdff c7367a49d181f9e332613c76cba3371c16cff94c10a6bacf504c8ac404358b74 +638500 e10674d6c53842c520040ea608584860418908244a9203ea9e726544ef6dcc6e b67a32293808e06725a391d616889dc4a438c8681893c17646896d19411e1cba +639000 a4629de92adb7ee055af0b7a55a0690efde637a84e9c19fd423aa74389c7ca4c 95ccb5ef5e2720ec5ce9635d8eaec7d000b16b3abe24936fb8daea2def31b681 +639500 dffba976bf5db95ba5845ee2863a8f60f855391a80d3a59cff38762f0992b38a 589744adb3b63b274b63d7c83913c65d121c13dbd8399aa7e0f317d4b37b5155 +640000 d5c3942c19dfae03595dbc0683dc712467c00e5307bfd96609c4a728eff4f586 33aee46df3a2c807e3fb8961929402532d1f1061a5e1df2f28335ab40002096c +640500 2efd0a03b8098ebfb96870e4235206b8bb428b5aa568aab56db36e3124c98c04 e67d6fe1642bd591df2f838f07190f8477622358706dce1a631819c828dc4980 +641000 91beaca7dba7afc17cc193cabc2fa7c34be665d2d367c9f0d17a8385903c5299 b90b505393575a5869ad32b498b2fe7ae026db5e3be2ae066ee4c8a4e50ccd0d +641500 52a2ed25b8afbf07e9bcbee8bab225af6bb0834b500ec30114697924951b9db4 0d30d77943438dfd2b1d85adbfe26de36a3f821303c50717d3a7e55abc59cfd5 +642000 24cc44ed0510958b23b614206b702fab53cb7681905d1aeb12fb532fa850a415 05e56dc42c34c44ae0c67c45985004c753588fe4588a4060444532194840e5a7 +642500 2cacb3c0be5a18667d5972d99182b87eaf485c724ce81cb59f3417691a920977 1d5689874267933eb1b72a5696935202699416f5f064b689e7221d6dd4a2c3c5 +643000 94ce4d9dcf18b532dd9f8a490fe270dab5697c14f49b7326efb3e3f82ddbe7b9 f8466520f91a0a9b5136c8ba589bf7fa7a5ddb292516ea1dcf7820d3ee7c0cbd +643500 e3c751b8fb071c5ef4fd1e7ebc9e902d365b88c3dd5a90ca5386d7cc42813b39 fb799e659716a37ba6e9982fd09e8848d637ef1d85555c61af74dc996e1c6908 +644000 2873489104d5b817e24659b1c91e0b4ed350c388563670846658b452e279a44b bc4bee8a1c8a2b98d32ec490a31632852f7935737bcad951d741e789ef99d15b +644500 2996cff77ae12636ed2da94137e2dbda45f5db09c968e13d9002d9d5e8d1bc8f 1482ea5ca89d761a9b99c8e0a5791d430478b289ecd506b3b1ee803d90be7f94 +645000 db1d293ab843d88849874e33883a8236a8bc4bff1d93b7d9922cf1f7dd3d0803 b13760cc8ccad388e7a35b3fa743b2c07b1e40cd5604c3d4162695d02bf601dc +645500 0c98834ca2fbd0b1f4b7e708c5f9bc9e820370fdc2b1126aa50eca16a46813cf 95b1ca36aee7f85aa247e91eed0bfd17f8ea675c5d9db342d9087017117d73c3 +646000 dea1a9883f1c3af496fd37570580c5c9a1e1ff7e8822d70188737f7aa45df488 ebab9f3327181450b58a388214d86b56c395f2e90be9f7d595ad7ec863e6c1c0 +646500 5cf92252a010e2d62030e5d74d6371e873f7fe6353a010791c21307d8236a3a3 392318738e04bc641e4375f47820bb59664b62c88a0f62a3f1bcc75be023b3d9 +647000 b0a00219234dbf1942eeceb7a31565ed117dd8a8152f1d343277433fb3a19233 8cd4d156025aee385a9cfd46d38594d1c4825f52555a703031c99c005adb1513 +647500 8cff85fb1e3b4a7218a58b6fb2e7d0168fb5a4ce4171bf3f0b7f1e7a5f214d0d 50dd61b46e224a437159c8faa7d18cd9c100d1e3ff75eccd770d8debd7723410 +648000 133d2255f56a3c2737eb94f337c9fac97214a7e1f54e8ca9b4bbfc6e056443cf d10ca77595aabc294483ef2fd07975ec43cd2e0764d0df38786d3ecb5cdb810d +648500 37691423db6098cce3ac61dd2e0ad5202cd08d37e2a810a4b7e259742b50f561 4fc55c6790346c316db4b0f1a35d067052bbaed3932cb8a369f8263791b9e749 +649000 e8f43ecdb6979574c66b478a2d3ad028145349c43ac18f67e089f6948a48812d 7609d55e10c1b40d62adbb39792b58b5c075f1c34c6f8676cbbe39e91a7e504e +649500 18f631309a2acc38dbd4d08322998f0d4fac526eadbb1aa2ef1ccf75173adbdd 83794a76b25f0ee49153d50f9bf6d196d1b78bac25dca55b9d3eece5c8c1c3dd +650000 fffaaa151d0634fdd830e104f884bc05d30160640eefd126e0e01b43bd5db5ef 7ef1c36f637320886b505c0ad5847bf0a345c6abbf4e92432d863b0ff517bb91 +650500 bae09e49248acd64fdbc94cefd831ecb2a957c0d5aed72644a3607a198c77740 f1ca4b8b2e74774fc3d86ff3f2b5e6ea8d59e40a683b993fa638c6770aad547a +651000 5d9f45be4e7d558b3133ab3a7d01a96db73f6a16604f4e348fe04aedab48fc1d 38c0484fb001d8039ac07895bf04765ad79babecb93040f8b45e18629c1fe566 +651500 7e12af704c44d001d842767b49709eba23ffc53c8a20bf96b0cbda7955168521 3e563eb1f016f0b9a83caf023067785be15032588fb1ed9d6dc2e7d47d7df8d9 +652000 6577afda5b1a2535cf30f91faff821b56dc9f969e590d406f644518ae4dff6ab a6aa3534f3f962041d6c88a5c413edb0860aad04ae8068ee31070716e552c03d +652500 f60863237c82f26f35682a510882c8c8b3ddf302e479fd201a5532fe02993d3a 623ee7add095d1a51ccbeacebe34fde662c727a14ec2ce8c856d2da57ebd1537 +653000 6d2f869d3cc7314a06868edf43473c3eb2a4ffc902b6866b2b6d9ed8254aca15 ddd54a91243a02e778e23135881e1a17a5829d6a698410cbc91c9c70047f5463 +653500 32d04fcd783380025ac2f7776d0868e44808c446df33e7790b9eb06edf1a13c2 9dce2c2f0200948be45127d74dfcce50bfe2787f01a1f62cb304cc7644754999 +654000 ad3da7c147ea7020a8854d4768937b46b2e9ddca13d786a2dbcda5fd691aa264 15baaddcb66534aaa738d7370796b82234bd7d1a183081ebd7f452a33254bb3c +654500 b38fc7f77f763e12c55c816230cb83453e513d7ffbc2f71cb18a4b970a30b6dc 8b8acdaea52986089352c0b17b8479def4e4e01d8f797609fce1c737e36f4314 +655000 58642d4240fc44829a1c5fb427e034890ca40c5097aaff77eadcce464be04701 11f24474492b2851fa79a42ff8d8685162e2b4dd1099722f3ce9ec4d5686cbfe +655500 d71e2ed94bdc78741559fb4c58bb944dde0adedc05e7558d7de6ab254c332fbe 7bbb488685c2ca7c84d98149d2383b157d88304c5b34aa7679ac24ad5c85b006 +656000 c1d7426dcfe8733c72b8fae49fbbd127a6e257ccbe4120600f8454287ad26105 2f8f7af5f2ec8a3e0333cfafc069bdfca90b0ad6b99ca970b0f458bafb0dbc3b +656500 c4f911e6a72381235e92a8c58ebf6fc654e35c3f5f4b2851f7f8a9ed3949e047 3d157f3d2c8158bf886bf994e549a6da2db2c092266c7050b831c8888d62b205 +657000 da224ea959041cf5d98fa040a10ddd7a1904725a649ec84f2ca403a8117c7f5b 9b9b9bbc0221a24b8642fadd26c1157ce903d7be532aff91519f431e886f9ebf +657500 61cdb241ff295b25fa6a478eaa78f111e8d7ada328853b0af54bb1b20a2a427a 482b2e9a549491f90026f8e50ca10016dcc78e4c3d2064e9f730b23ca9520a2f +658000 1c4498c323201e26bfb8a8955f29cdeb66cd43d6a2b2befb1da4a6c0647d1c53 931ef1f4868a3c5b54ef4354bfa791c3ffe15c02b3c704a6a281154e2f5be98f +658500 33dce694605303583aeadcaa80db0d8569ff712b588875d48edb395520a43981 6247bcba2c8883551a85e364e30b4104edb0318eee718e0317dc42f2a26c7451 +659000 339aa922137edb48aa37b841e956adee6c9222ae9bec106ed49ce5843b179681 ded7991d424229bf67cbf220dc2a5a5b223b571374f52d14bc9e83df72ea4753 +659500 38dddb9b6e5dad4ec1f95be3a83a156be12c5dd9d5e74a20bedee990b3f511fa 7815a077c5c8a265fd122b282139e3a13ef5951482f6d550a566ad74ec2db369 +660000 5562a936684fa4976fade44cd5eaf6531bcc844027c9ca01e33516c0c87be6e6 0af9d5b94d513c095d2883a381746ff79e47d5179cc95165708ad37dd0615764 +660500 de362a2665571bb51834a5e8cf65cc0b436097d1a640ccb9666fb41ee1694713 b7b39581644d06d7672965e189c645b4396f4d50b223eed99080a122b22c2f1b +661000 7df5c95f34efbe842ac26e4252440f4f315b342bd66e74a3d1bc3c48ca0f3844 0a4f81f5017d079725470dd800ffe7e955bd82df18b6e8abc325165537ead1f7 +661500 dfa40eda9296e76deebe46ab09b9d581445d20814c44c42090766c0015162e53 88b577c05b1a5f27b2aec225732da967a44e30dbb463455bc7ed223bfdc76025 +662000 7cd8f5daa828803f06d2ad8e7899d4993999d0ec37e8a37ec52555d1c64acd8b d0a6e4b8cc44620fbb80e061a949a8fcc371fd1f2a0b05138978d234a99fbc26 +662500 057c9dcdeb7375f51cdedc28740c881bf6ae644ff17c0aeb77ed796e89db8672 c749272ba9509a09bdb7697c6197f132c1de0c58de5130e8c3257288be94da8e +663000 d9ceb13692d03610ff6e97eeb643407840e27d32c389404cd3cdd14733756dcd 05b1659e97dd91ccffdf180d8da3a35f6b2fceb9676091bd997ad363210e2f5b +663500 cfe8d9435ad14423479546bd4d6d1e8dbdcd62a8214ab922402a86838a8fb3f0 cf68cef0e01bc5856468753c4df42bd16f3d33dd68783e012c1b012a3cfddc48 +664000 e18e31c640bf36f6cce981521fd55f42761fda24538dac34285fb02e69c427f2 f237c6c072f16f524d90aae138ec3a9176c2fa8f3049ac9ec009ecbb678e4f77 +664500 2cc31198ec6c3fe6e286f01d98f097e19a5c23d3f478916414e83f201e724e19 1805da29e7f7e8dbb2b5aff1e712b3a447106db48671120732111884e0783b5c +665000 4b332fdeb19f5d2fc8108c233679e861bd40df746262d5412b8a6800f50ec073 b2119ba9b25aa2a5921c7aeba1759395795989824a5d6d204592d0ab6ee8ca12 +665500 3f0705d552dc4294997339d7015a4897e55790353236540c775e330c78cbb34a 9595abcff6a155b8b995057dc725cffc3b283086ca65a6213368073f7f5d2b34 +666000 df535e0c312494f9f1a288cc54b72779ec7ba745bac09f7b952191c5a904c56a a545eb072527dc053c0630dbd820c51a2c3a22510f31a389abf75294dfb34911 +666500 ce6966c65ae25a88bac2a35c95d1943c482febdf7f9c28efb57eacf2dfb9544e 4d6fbf4b0b9c493ac96d63f97dee48afd60c2c847f9aba07110791514b0f4e21 +667000 e7a271b35d3fb008b6938cb618fc6f57032c92aa1e09fc50363cb181ca53e2cd 0f496a3a120f9644e6e3c5d371e6e01de4afc4a85f010306919bbef0aa323942 +667500 783bafc58044919edecb6e3938f0ac69dcc75bfc220bfd8717d419091bd680d2 b22cef79cae030286f2fd4b62c409c6d9a801f65e377d66d455fd814628d5ac0 +668000 beaea7bcbb6c4c3a18e0c9bcf6cee525f9036796157c8b4ce5748819c13c2967 a81cfb88c97c1489d9193801faacea720e98d21981856d121ffd5e75f7ab393b +668500 393c647b49312eece8839452c0874a2e82aff7169a3a7e1e5326b1993a5d1af8 212bf73b67feff4a971dde4a00c12b32fe82859167687a3cfeeb03a93ccdc14e +669000 f24c2d105f11f385c356455d460f8b74d466885118dc0e1ece8bddf624cd5246 d91bbe1649788ea71e0574f15865868289b28651ab7e28455d823a515492ce0a +669500 3dcf0b942af949fb37bdc61645d89efd6f72b13c02fac02a96ccf1375cf3cc21 81fdc3a3d66e72026b4bc01320881275584a05be5b0a5f8edbb2a6f334126024 +670000 e6d5cc2dd441d4b9f08b05a582c291a01927bd1a945f1a43557e3bc70cac59be c82d3be4f90e23836110348e66a9224cfe135ca4408363489f3296dcb8c33f2b +670500 c420986426267474054b56073135491ad4381376c64dbba0c57deab84ef61846 b0c138c7e5b8bf286b3f5b6ad0a83fafa7ba68d499fea78553241e741a05d183 +671000 d21e66d96e08adbb61bbdb75a7e8971ca9bd45a4906f3ce23dbd84c2e5e8d161 ba60c1936b7c35369e31e9fb6be2739cf60fc4685d210a209c4fbcf341d5c9f8 +671500 f7bb538d49e99ee06136105edfbd282e2f370d3f4f4c6d2f20b75938aca43040 9c1b787f1413f483e72003c0ca97831ef1e616b86fc3165423cc84e878d487b0 +672000 8c85db8531cb0e6bde769d488b6c6a639f4839a819abe23ccf88f3756caa6089 598752609771c022cecd036a0b8aab8caf8fa3e64724dc20815a827b750daaa1 +672500 efd3aacdf98aa1fc4fea20a2f4cdd9ea3eba6dbe03a63a149610d842793bc85e 93df0fed7bd2dfd8ca6293601d799ea9a54af7b0d45997c821e4a8a7ab7d1f9d +673000 3109a32670b3c1a796f9c6cdf5f3744848e8ac3ef0ee2ea891fac0eb69223ae2 baa8971d22821acd1fee4649f702edc70cf1b072517f4b69d6bf4060cefa34cc +673500 8064bdf0c36a535d5d7788f901ac0713b627e2f8b80c4b20d26cee0daf02ee6c 5548c9d616e98ed4fcccb300d3e35a78d66e61aa95c6465fc66a98c1e40405c2 +674000 b409ec86b27b1a828acc7f056dcece326a7892314cc5b0d6682d21c06d4e4cc5 a25e0ee6404f9db371913c15348e3aa8a9ad4b3cd99cca3fbf1e8f95b6e152dd +674500 0c8142e02a8e0cc1667a81f4c4d08ab7577102481f73d31b9d8c67589874e970 e615eda6aec462572a43acadcbde8bccd88e07fd0029bd953e04d7022dfeb355 +675000 f200a64f03ed78fdfb3a35be9133cc144cb7621be76f0a470a6cc451d31de7d6 2dec2d8e6cbe5d122786b9c571751d54ba1d597e23dd4aa03ebc50212c68cf8b +675500 44927533602536ec6857a62ddd2e7ca31a05c733cd0699a078c5106620da51f3 989564e602ccbd682fcf993e4ca7691f049bb6adcb099860f9577a83cbe436bf +676000 1349301af96fd291f3f6cac4d6a6a94779a2015ee9d63989d79c813e7bf3ec81 c196991fb4a511513fcdd4300dad73d556daf8eaaea1411a5c4b0d0382e96d45 +676500 7a7071dae50a8ee6f25b289f3e9d15d53661469e5379b56278f391381f2b5ce3 918295a1444f694e137f7f9653f87f1863884241462386fffd0037927c3779c1 +677000 a816938aabe21e8efc9c41ccbe0e7de2188e0bdb3635541e05b9cc8e61ed3dec d59c6134dc9539b3cc6aaff9870b30ba12676fadafe95167bf90bda3f074bdde +677500 d06ed31f2f1a7dee027e76fd419c93a2c80824f31e5b0bf09e85e10362cdfa8e 30baa2fa7e5834e442e8445c81d124018eec1ecaaeea695d4278263ae60ef134 +678000 356bc1b553016f0a17399fc531e9d5a86bb273ef944843e8aca16d16480f1e1c 74c26fdb3397f25f794bb15595b8807b7d4a4500ddc941e7461bb106a69daa62 +678500 e7f83fbb9fef902caa5ad499746100c3422842f793424bb2b17f9431b0a3b6ce 5177927957f3b258bec01be88d817c262ee5a64cee44e04b6d78b5f227e44442 +679000 1ced7af5a996a7028c63fe45238dda8c540295b48d082cc278614f68dae0e249 cbc8a6d5c9ec62bd0d50ddb90e7e1efc4b62dfef8d7aede6279ad95186497f7c +679500 a00fef7924aae620536c387899d8628d4eac6fb45bb1954447e2de6a4fdb2ab9 69b47a27d041a54b0be2406df7be07f3fbecfafc0d58407a7946bbfb0f65cec9 +680000 76831117aae716df313e1e7af8c4c8076e2b7d37bd7239c827962cf903afdb55 0ba660526d75f5b1765967a87119f6bb67d33a496e23b13e58335e960610b38e +680500 17835aef913aa41dfe7a49e91fa7bdd85411356233f5cdda375771706b5a4e5a b88eeb3ef96d4ce4adf49b87f88a5c835bdf4ff88aa3a45bc46d2970a14533fd +681000 86ca3bca8de152e4ddd4ac3171ed463466dc040afc5e2f38eafb7fd0ce49dbdf 55a9a5b5ed90ef00ce658b5fe52373ec23b37e7ae1c477decf0e8a41326cad7f +681500 5b31112aa2d9b190844d109c44f8a4845bcb0f75e567055bc1368bfbae9bd427 9b695ac5b41310d05f09d0e5265f53b4ff475b10d2b0e27928d81543857a1f0b +682000 0f036f94ee9dccdbb0ccfd4dc10663890af3c2b7da67321baf4919f8da0ef388 a9d70e8df288e875c0361d7273ff846b54dbdae0d6d6459831e71ea4a41397fa +682500 18658069c688414684b22ffe62a7e4d9b84b3f27c28351b57c8c5763aee40ac8 c4ec9845b905ddf71c374d400faeaa649ba8b08751f0a5c6b9780d4c7d497fee +683000 ab2a4002ccfc546d80417f9c1c66dca34d8de7df714fbcd4eee134659c6b4261 5bccf1dbe0d677d06768a02a5bb6fadf5deb0075582a97e4524e2128a4fa4363 +683500 5b302ac27ba940456860fa752bdeb27a3c8494c1fec08578c6d204588ae18420 ae3de94a6481c28aca678f115b73c830ecbd163fd2e11a9d85b2c3132911c3cd +684000 15fc8d38546017ea2ae0e0b4387d74bf702a396b52cccfa879bec7c415a164b0 9a6e35b63f814b6837bf67d897103f320a25e5cb15dee8e5524d9961f53368d8 +684500 e40472171e3915e9518f1646a3ca09a46154b226443848605a4d8c78803c90d3 2c7e33f27973f6345dac95862d63006b4b1506d5979f60d8c52fec12b74a1b78 +685000 742e161c4d772ed9ab9b2aa95ab7dd4646e7fa71a7b71d0062120791fff3d538 7854ab499956e6893c2679a3393fb30e450f2cda76cfbf08bb3e77b586b10599 +685500 697224d799e123de9da2b93aaf6a3f00ddd507080eca2357c7cde1ed1ae8f189 1b02e1bbb85f1e71e1a1bd1e58e4eca588997cddc1f545ee293b17267fe5fa50 +686000 7a7fffd9aa90d0d9e7309ccc0e450c7544dde058085bf4bfc9efde0cf8fc7b3c fd10b3c2c0539bc586cda1dbdc454a1597f4acbfc1d9e7e7b472e2d3547c76a3 +686500 3dc10029047449d4411fbefe15a7f25864007511102972f14697dadfb914d5d0 8064a285a9ebb460f864525434c5bfd94ea7c437f67c9eb6268c3f3e4737b798 +687000 2e2c774dfd40cd18616a82d3e5769d4f447a2eb5214bd8783a5084cb8ab38e20 52379664d91e53a60d1b1a4d64ae2005e6549972c45af11ef2ecf2f2faf82ba3 +687500 295e12eaca70c7eaafb7cd25efb6b461f5aca68665832ab9eb652e6fe476d809 43763991b8beebed72f3b7215ee72a2f074daa83a27c8db1decca52bbfca7b15 +688000 88efc791202d8e4a0266dc8114487847252ff2497c022ff4ac237aabc6602406 560346aa44e4afaf2fa9a4e1506534bce8c0e113efe4b19c6aa26804b405bb11 +688500 8bde3d0d7f23d76ffb10e59c43a9a2cd4121c3f5033204c4689fe77b860d616e fbc4d97609351dc9fcc8f4e08b5a0c79624c9e81989b684e219245878ec2fed1 +689000 01a706323f362664abca40cfecb4c6efa65aece3270fb1240e238f1e93ea3ca1 e4672adb018af124ae848405442ca5e255ff9af3909d258bba12d29f3aecfae8 +689500 3d25b5834362be1296b92448e3d2ece79f8017708479f56d7e28d602817c82f8 643b9f9b0a754b768ff504c9941c627627b49aecbe170a72590179536840b843 +690000 0e622f16da74d548cc9e49c20d072a9a8a6bf45dfd793830fd25369adca29bc9 fcc1d77406d58f3e1e5630871011b389449597e41f1df035518c1e25affcdc9c +690500 d96ee19654c250c20011f56dff3850e61b9b01a8c3be009b93c0f53c79d6ad5e d3b6058792062983ed59720fa58ca7cafc890e475e2411a01331a4e3e8ddc580 +691000 0e02a80970060ff139ed4ce0a83bf5221895d80de69da89bccfd720da52793a0 20279d4dcc8aaf7a83a59d70cbfdc4c51e8d034467a956cf012349dc01d875ea +691500 6bbc975ee40102521ac8b041e481797c83174c5f2b5fceaf3090895a6301f42a 4ba322db9723202338d5c0e46427fdcf24c8631f53f3db590489cf5cd74311bf +692000 a36c0ce45bdbfbcd02fe25ec7c199be6e01f6edd8e5095dd745b136e1a1956ed 65fb3e0023b45c838637825b0e95f461b02f90134e875d85ca8f084bd30d3eda +692500 2f8577c57b15cf8c176873a546e1451b379f4e09967643452a37b037b538a0e6 b15ca73e362e6394166b6e104ac474d1f73d4fe671117a77ad3f9786ee35488d +693000 634e96cae2b7253783b3ba6dc65bbf79ad87782a46f07a11ca8c3b2325e2d2aa 08db37defef7bf06dd75f2d8e4f8d1be0f2004d24e816a71ffa4fee43fb5153d +693500 c472b7da4a1d6e9a9728e58dbabfdea31af58c18a17e79e3dcade6d96efdb689 021ef6717375d52d4e02c36bd8757c6c73b77c97bf3bb4d6292cec23afc3c8ec +694000 8d5000ca461da1bf1687fc3ee92e3a1b414a92737d82e301895d6cf41e0e4cf9 e2e6ad6d2a58ee8538e7dd8136587a612c39ac281fc3e6bf609b1c497d9e331e +694500 daf014e18bd15e7a19ac859acd668aa28bae8fac71079e0b8285c25fd28a46d4 9a671a25ba774c600b2f967a1632561ab4ebbfc4b4016979aa9288af8be93065 +695000 f0d64ffc15600b2a53e5b9f09f1a0f981273e1cff39f01bb69a3c27fbe679c71 d5adc8baacd5fc0635fe0ac739a16893fe0c4215655ba111f49e3c709f0d1faa +695500 d784ac30db59c0e5e8906f2211b6405356158cf57af758b047445926e8dc140d 7d0c9248ce3387940358e82c01b7accebe7d3445fe97f6569b0a99a1a4822b61 +696000 16e6034f4089c38e9eb2a11669e4d5b896b4e36e767626bb4c2cb852031099ae 5f54a918561ebc4c21c27b793cee698c347e8dbfa49ce72f4580ad7d3e464a91 +696500 bde4528da57c22025db1123e7fdf3e58f020015b1b500be5d2ea13be8f4f50fd 954600e88d55d507e82333a0b9b36f5bfd5da128e5dfe721ea486a117f196e51 +697000 eea7b9418887e6bfe3a460b03f30f800672d8df94217fab7d1e7a74f437ebd50 a210a2e4efac6617e3df43fd4219eee8e4a9dbf05e4cf103cdc9f39e162d61dd +697500 af96502823fad0e9693d94bc707b04d59d3f42fb27a12134f1aa0a863ca616cd 8cc57e73164f446606d37223429ba5a37d92d1ab52265fef43c81d8f5d9756ff +698000 8a0878bcd828268633df687a5405b2b86051640a122f616231d8d6146d4c2ab2 27a9f143cdcb85bc01f31868f92a964337c451cd1a546f93c29361f245aff07c +698500 afbb603de0da6476915e14cf182c6c785ec6514b17d9ba7bfa74e5a228f6d8e6 f730e6ed2ef1334071ce63f9ac851de557383c98b40a72f8bdfb29c49bb7d571 +699000 4e3bcab244d1ec2528890ed24340027a9add680ccac038c42e9a5b4880368a57 1b7062e806e70df560b7dd19713440125dbf814412ff713c5bafc8776c443a60 +699500 69c03ffa655f439caaa38a36031f45c27f6afa345fd8d380064ac4106a47b9d6 9835287f527b8d7075cb42cd361e6048b9defe40cd855bf186641ac16a40cfa2 +700000 19c87f86c5d859f1d4ba642097f626a5c6cc8420f1b438ef0ddfc52d77b5a7ce e7cef1ed642d40f49563c66d9428c699a66241a86d05aba5cb6fc6d6218317a6 +700500 c22be0aa764d1e0d90432e726ecb95a769d691a7de81ea480c1bb9e74f43bd8b b0c38cfcda8312393e4906e1068c7a323defc33624e99c8c8f918771010a417f +701000 b2505b0ce33e22fc06044bc95bcf1bcea363d91c541451ba7ebbb07079c9b8e6 36066185fdc8a919096d36245786b921b24ea4b9ad0949d3685c7d54fd3faeed +701500 11c69ac149f239f122c309f5c7c8c08ae9ed95e563e6e5ebc0db4d49628972d2 52282de896ddb2f1cc56c22ec74637395d1c2ff49508e903cf4ef1f5c01a1da1 +702000 73e7769b7982c2e9be15c6d3b0156680f7705c802452205d64a5d66187de5008 623290f47a7ec8b202b95f45d5c6408072766e49439c1aed29f023bd79a8e1d0 +702500 cd7f69f08cba82026873f72e8709cffc89a4bfe3c5300231a180fe4e7335e87a 4f1bd226f6b1bc4313e7618f03e4522da41509937f596a3728da2310deecf49c +703000 6253ae1e6bcfdba8257a18760dbd8f42bdfedce3257b9cff76660667e29e5e20 05134bea7f58be79fbc3942bff929fef977a42b5bce28e787bdf2bbc1bc70764 +703500 affc08445fc42e60d11152790408c66118b2a815647640a3eabc3006e0f58f8f 34c74888ee86d1759127612373b099f56c53bdba1b0fbabb0a7ec61eb0f24ef7 +704000 5298b2ba05735ba586e9f6aeaad350fe88b762d00c4551d6b02f6c0b706b0f34 9a36354d1266eb50f84338359401e91aad336d97cbfc39d574a83fbb4aa11d67 +704500 f674bfadd8d412cffe1c8d6fedeb91e9c8914c3280414896f97e1c08e05b53b5 6e593d4f49588dc6a68b5adbf870674cd17037cf7431f34253eb6cde06364421 +705000 d288536328fe3b3301de1ba97c0237dcd740f225f4df07e923bb570f68716604 b918a47fe346fbb9958ebff0e557b9febde26c435ff6320858bdeaa09ed0eb60 +705500 bffd8b226418632ef3a9f71f473093e5592f446776db14fec222f85de068c840 e889bcd01a761d5208221642a51bb1d0afb1f324f74914590242204164287740 +706000 2547d907a333fdebec0140593814d90a1d806ee539598cf0672cc30a1d5c31d6 77323ead475140b2224eb35de6da439d3832a091f1ca17e53a885d0b4d469105 +706500 5625e44c5204f1d9d3cb4b5b55aaa18f13be66ec9f76988159a172713f141fa2 31a50d4821509dd711e9a541a3c54e28aad4bd6c6314526ba82e64857f52f4aa +707000 e15531acfd422b5932479ecfe5dbec51af4e824298c73658bd43c143958a70b6 e9af02a4dbfa950c0a45fe96162bf991f65df101072db521b4631ba7e64ad248 +707500 904129b3e99e06b3a75bb2b903b65b1176d82601355b13c92dfd2551edeea6e3 4e5bdefe9ac9916fa5ba321a6056ec1bc8fa32ce3370587f13bed12c32976b71 +708000 23cd48d83d0bc7e5b80c8a337389801ef9bd5584097667310342d92beafbabe7 e916d2f42c77692bf97058ded798c541ce883f69786596ad94a22108adc17694 +708500 2f58264be757741e695394b348381a9ae8fd583341791a84d68d338cd811ae39 dafed9faf74c750eedfc2e65491b75d4bae7d13b9e082e6fa91376c2d6034da9 +709000 e55588bd39fc8e754f2b272f80ac86b5515bad37eeeac6fc3dc0d1179fd53423 69815269b65b334aa89cfca70ebf71dce7de046cc84b68f0f9c462da718cc469 +709500 e30d4db49a196e4ae4502c992118f37ddd6da696f296413500ce20b4b9838497 4cdf69b6c9515176894db3bb5ead92d5ea1772d53717224656242e8007552228 +710000 6f141473cc79d5def23f888471ff143ab546b5b1d1d5fd1fe51c996674e3018d 5af180a3caf0ca42e0ba20e959495120693f273201de65aec15993cca37cbd2b +710500 10d11c0d1dc7316a7bc8a99b685c0b12c9e2bbec941d3c33781059c7048ab022 541f760b8b3ed77398d5b5193220799c84f36e5cf342256921661f6d6fee0316 +711000 dd068ba9611f623d9e761f5e6a10b0298130c8b25317f047b6a4e79a3360909e 920987e93062ef5eebcfeb0a98ff5b108649f1a092f76cad1c7d09b276469f80 +711500 96778f4ce3094a1c3323e3afd658b20cff90b2aa7f23d886a5f5629704f5726d 9f4d8c3657fbcdf2375c92f6f7e700f8f108a1a5df22468685782c897b15865a +712000 f4414c743e2b4f89177f92e0e11d61e9f3b1669d3ddd6148b3915862dd2b87b0 df705fe65a056478cc8c914ead67c0a9b6023fcecb68dc7d9ec56a8c6ea1b865 +712500 fba950fc02bb67da1c4de9b6805b7bcfc0cf7e2bfff478568be99a70596267ac dc357d74f8f524808f3f41d6604d74a8a96c6bb87f695a15ad835abcc6669be0 +713000 ff4dd851417240d00e594ee5fb5408372e4fd5585bb5ebf3d73a97dce1f4c41f b54673e8a2bd6d75e2218fecd8d20c0bff1b231b80fedc53b3f1deefc7fe4747 +713500 92e49d6caa2cdfde8ec7d8c37ae12d995b9552c76f7c7ba0abacb77b02bdb5c8 4c7b0f7d4f28c7c4d35a29c0a74310b38789decd628308c72a5a270a66ae55ba +714000 dc8dfa5b910afe73d3558be9d077fa57f1b6ac42e058227cd73b1b1cc0c70231 9ccbffeff96630cd9248b3001d7ec7ccc83c9daa5351bafbdefd8a88c03f3224 +714500 e172274b228f5e4f49167f7d36076f71db89acf666b9e519da3a33c548515826 14e35bb2ffc296d0d1f30be89e8a66f0bc7ade63250bd68043acf98dc625c91e +715000 a9b83db90d65b2e1db885b5b708f1d7203019a7073ca3e5310ed4dfe19bb9d61 1e2fea7d862a4b98f558f07e35d23b1d229dcbe2047a58f7bcec840147dae863 +715500 969fa24302ef22b2bea25b3f981fbb756f3de84f841d6f73ad9d6a6bbf463fc0 31872cd93e38c92acd7b1af2ca83b7ddbc9319dd8606725fe0aa2b50026d98aa +716000 e33babb3b3269852ab03a5044a595312e1828dc11169020fa2a05f4ab329ac15 129a208db82ccf3418d94c00f39f3eb81bb7ebbe901f2a698362be4fa1f6e7cc +716500 9c0cd626f57ca90c771b00015658c387466c2f3d462d36d9fd3622abec903b1b 044c3a4459440fa9078eb26f15d114c6ef9d63bc5b8640f84289a1f376b4b85c +717000 77a31d517bfcc769f64d0de88617d62d4d433c1d1a3136962eb07baa5f227a86 e1f060643884a9ede876b52b38690ccca9c9d94140470024d2f825f4307dc918 +717500 546d0c517b9b0c5910d4d7337c366d76c7d99be4b875f673cf3876ca5458cddb 814a3c25d78419214f5645a68ca361b0199b58ba3fd3109817b585cde74503d1 +718000 7e44714aea5af5ac9195904d29c9d53d03fca15e00e0f2c1a071611b6fd03011 38d5d155e5a9992a6e9c1423f0ee621f19173c533e6954a46258e02982b89a02 +718500 dabb2702316c8e8043539190a1581ecb881dfdb7502a517fa183815a012eb344 268c57e30e191d41a2793ad6b70796829bfe2726aacb3864283e82e384eea0df +719000 9db785b29f04f274688856f0858c7c63961a6aec5788a94bc1aeaf483dcbbfa7 8addd23f1cf6b051fc6bcd5558893366fa7a54ed89deba74d79f7a307c5224bc +719500 4615a7726a6b3b00fe5a9cc53fc916af6402871508fdc9b9f6e5799083edf0db 3d3702be96cdabe826e24dd724ecfc426024a693c161889d0b41275cd6130d19 +720000 6b70419e1023a8dd272910d32c30aa0617bef67234063aa6e7447aae5356fe34 55f7d559d3733e4d3d6b5c3c68265e088eeb6d2892671e88e9567f238268925c +720500 473da49304fdf70d4b5adadf6fcab3f0c332ab5ffe82972dffac6b14aa20f23c 3de3c3ceb92f8a7f45fc19ccf72f79f2fb8d3bc61654c534f81df978e905a3a5 +721000 c293a009462b561ea37ba8614559acaa63cb6291673c68082a6e4691ab0eea86 ea34bdfab3e2a7087605df85d858866b54b4ab1acd86619e99f6283a99f7ad67 +721500 a15ef2603cf7a7b0ebec81fe4abbc6035183755a08fa11604e40c95477d61812 6aeba98a3c5083b997eca56cd003e7f0285963bcb8502f73f2994dc04492c35d +722000 39271600011dc5e938c1308fc8f467fa44145f8bbc5335f24719d125dc92f664 bb0387e362d72388584708287f86c92ae50f7ba4330c9857b308837d150906fa +722500 d7f846f9e439702e48044fe02c689990aa0a56613fced844d989816e0317be80 f9308f9c0bb63c44b443473a8a25777e34d9a0e903edcd33598b270f0d7e9036 +723000 6fafd8c865b3d0bb05f18cac70fb93007cff223789fb46958b55a205ffbbc959 cdc9d13b0b9a5fa827d2572e9c3232f498e8b06ed8627199c61db9525469fcdf +723500 6ed55084a271a39809b6f3455a08d082fb7c56ad97427307ec207135c7c16ef3 cd4a3d7e1dc9e293cecbcf14b969480c0c2f277819e9eb2286d1761df7320694 +724000 c1b649d3101d61bfe8b949f443e1035a3d5944310f83e94785592c4cc91552c7 452655726058f019fcf400a89e464688ba4bd0fcf267493e54748b146c7dbe1a +724500 21f5f4bc2fbfbfff183a3eaa110c1e3f826eae2f58d32242919026a8d1ea6206 d15908ddef7626667600c5eedca9119d82e0da5f4cf56bf6616edce895b38d9f +725000 5db006a94bfe731764372f332c2b23f9863f7741c0aacc4d92769f353cb4a4d3 db57165deffffe98c3cfe7b95e119d0253a6e984d11fc2b1894ba4bb3010301b +725500 f313538888a7b2cbf72adfff7c6aac1583c44b477c27810920835e1f36104ec8 c811a4d09b2afb0e9e70bc9416e58aa2c3dfa4955ec4ecf03b3db9b65450e858 +726000 f9f289b08641492431e4ff87acb944092bb1fd4ae405569e0f3f14e433aecc79 5c10e47957ac47afc2bd4fc6daa0c7e30f127f0ebdeb1b52149756d9f996eb76 +726500 769de3f5c47b72bb76aabcd88f8c84595015f6f3f8d78b4d4a0df2be171faf31 12f1f3f363f1cfe762d972b988ba7e0943520065bcafadfeda1cd73aad8c2aec +727000 2d757e0b21cd7f64dc962c5ef6a7268d0e0223814f74e6fff9c81f849e496ded 3ef2c4f48504f065f4efc06814674b635ff6d31e9b667f9b1ad864819804f446 +727500 5b2875cc1b107110464a44d7d9a1835e8c19246cc4d646402fd3bfacd53b7201 48695a64b57a8523e285989dc5bca5360df44847a66d926c154c6929e92608a3 +728000 35fb3c9e4fb0a40ebced7b7d2e9bae928790bac78a8460c311b99e70cb109c9b 758fc72831974d7f37e9307b34f80de04f3ceebebfc97c79eae5e1dabcf2374d +728500 156a06f3bbea42cebe61c148aa7ac4cdfebf95edde5d021c03a90ddaabffd20d 6b170063e91dfa3d05c9d6edf71577d85a9e3f0ef734b7376f757d1bd5a42705 +729000 e8771ec45982e5e9928539d0f4f398b3a35f02b92f44ff6937e6a6a50fdf4100 bba9a535cc36db2ca8af73b8707635e3c12b3125b320df91257a944ea3a6af8d +729500 e17e17e50ead400f5dcb3f3ffec5a1ff95da211a7f6d30f87399cb034b2fed00 0263cf2e76899d4424125c726396ca7c8c3f1830eef290b084bc67aa3dff7ea3 +730000 06ee01124cc2d7e6ef4f764638700ae11ca26a457317564df6e5e7f0d881ba4c f20e5e74a5a07e05fbea7bdbc73ad393bc83a144a7e15d03a096f052d7065eff +730500 2692d795f114318940b89b8aba8a910b6360170048ac824590c5d814f8b01973 9aad48ed4e603ebd7c79c8535c24ec7d2ddf7db6eed92b9d1e83846e9e139a1e +731000 e167080479d38ecc4cbcd16e4fae05c1f87e09d33e4947b6a9813ca63e147d5d ae82cde68df2aab3ef79112d4080a285b806bed3e457aa4215c6ee35a6d73e33 +731500 d8c7e6903152f5f559f108449eead76f647a5c9991f2815eef0d0058c25b6bb0 f786151a72adafd6930fe59a04cad481a49770c191a3f5444eb9b4d49b000779 +732000 7371a17e1393bde477a0beb136f98e89dd30f49ebb28cf59a265dea0f82deb70 67d98bd80facaa69bcac4ddff46661d1dced445c622bc90bbdbe1e7de9a8f768 +732500 921701199569749e2d13f7940e6347ded7a3f1dfee26b7f7bb744d5d3db586dc 41fd3e2f31c4d732326d13c5f31cd850e864c43f1f179d8631ec1aa69c506d88 +733000 d452ca29f9949b714f175c7ca45841a6519857541c44df4e52caa73b4c9b347a 616e3b11378287c338d03dc5e2395c5500915fff6340d6fb48c0123e33f92d51 +733500 4ecb0e1b2273ed4df5bb7db47988c3271e81cb9933d7cf50120e9730db47c82a 6dbe9a6615825d26d58bc5a2c068e9068dc5b4a1402dda75c43ad3810cc55a80 +734000 6ef7723b332f6d1a4791f14a1cf2811870cbb08c2484ece5cededa3f69fb920e de8e532d9596a92249adbea9833ff2022f96bdd3f82b7c734fc3580b4b3cc946 +734500 01d19d29a83d79e38c274b76943e9d5769932a6a7812e3f297620a40a0bc922c ff43eec3f821cef199133f7c5282c9892c1b1546a77f6fac3e9021037481fdb0 +735000 544a5f6ce7a71e13db7335cd2a4e02fb7b5ff98253016f9d7f26956dec908470 662695548e437e31e7687cd4aa3a463d2dbd1b0579df23c6f5daa7e9b3788e66 +735500 997d9b45e87e6f1f1b76c7c212527bef213f348c51d806200bf6541da07b76d1 68e5ed2790eb489379491eeb583227ffaa09afd5b62b9073117e540505058b8d +736000 b371ea1a60567133d86757ae1b12faa8832166726b9c2eb67a643e6878b0f64c f5308b0f2fe00d7b1e1bde602e6028ba5f2ce8849da22ecf2cc07edb069a540a +736500 ce6bfc2ae42b6e0893e4abf6edeab04ee36cae259415004f71811cac250e2abf cf5273f67e35b1bea345758b059f5b4b05ad705469c64e85cd90f392f03db171 +737000 79e359fb34759ed805e4ba17b22b3f64f4e05bff59b0929d5a996038e3e2684a 06fd8eed2bedf16f590dbe5210c4657f8f14142fbd12b823b4972a4062c46476 +737500 3d69d33e4b40ef18a677394e025c74f1ff03807f1cc1a9924301da4af97d40b9 8d5cdd08a8a4db368da7ab6be4a68085fc9b2a156fc5e89b69ed661267bd9d05 +738000 de67c044daf5e82856c70c57c05e0963d2767f721c9a2a3095052d271ff77a21 3f6cace03de90c0a7093e549aaa5862b4d0d4304d20e548c1b77a7022a39722d +738500 9857746dacb79d61819a9a7bed47d4f3f8a8ef2f0a4241e4a26e84eecc388178 b324b1e1ec98f23aad98bd643e12396cb0e6db5b6679f5f197b8c82a74912869 +739000 e760e2ba3fbf67764d9ebb7a9b5684b71d079174bbabdf7de315b7814e6a6d74 74cb6e148688d11254c235898692018c699071730431d7e9f7438c6859e1b140 +739500 f066444b8192b7bb857f79443308595802f67e277d85efca22432c5dd095ca02 91fdef383bf939a8e230f32a00ad0c58452c3bb0847a257a136297dcfd476a6e +740000 8d11e7b07aad1f84d0976806645f5d508acb6aa563e1905bdbfd49f88a83f561 d1fa7be0afdec09bdc631fb8d5a35bdf26f47b479aeee287cee3557ddcc412e2 +740500 54b44786a4b53f25a1ff7dcbe85ba288f6108cafb0abaf193c6fa9fd1bbec690 511fae7de79f3e6aed0645375673168bba00a2d39563ea2596e09e3a0e609840 +741000 6e98421219a255828fe8067f68097ce9d08c9f33886b8a29a22d2ebe6ba1f3e0 ad33286ebbf08385412b7a732c551354dead015e466163e74e7fb5f94f6b1cda +741500 81eb9f3b0f352e0ed1c5935647d526ac71e0b775d8715732e262f50ab6a55963 fa8c84206d369ea634fb6668aff771d4c9d3ca7e687a6f2aabf1b2c80563b955 +742000 540e29303471cd64e05ae80fbd19847c61476ca202848595583e1432e45b6a12 01c47d0edde8e38dbef982a21b7b95e6049dfd3ecdb75763b3ade1cb8b804ac5 +742500 6bdd578f687b5b04d2492b7bebe989d618b7d55802b977c9f1362c022d37b5e0 abaccce8aa24b8424fe18df7490c67e36999e1f44dbcc68ad557c59717da560f +743000 1a56cced54e09d3e03f267054909c5800b30e571a81e21cb8e1960c32fb615ce fceaa34349bdb34a4bd0125a9d5a0d73ac8eabd0da333a665f71613075268854 +743500 2b102f1ce482314a2ffc09c26320f241ed197ef0ad1b0b1b05884d95e98b4832 0d514c31a22d865f9480fb408373139aeb312d750df0441dbba91f6dd74b7c34 +744000 9eb6d3d3bb45887dccff04f8555b7b6ee5c16af80cfc8fd132f2f5d64ef32d85 18c336ab05bde704336b82417e0f8a33d6d70df4bcf3fd3277dbdbac7d285313 +744500 2c894cc985ba39b9d41d6cdffd9225d1137285bb2a9faa355fc04e1c3a503783 fafb7464d1745969dd5edbe08aa6400acd05dcfaaf6d09a135b206eadb2d7a5b +745000 0d3d86df05886717a850b573d540f01159dc4aeb9715260e19da107d8a5768d3 273a426515f6fbbc4d1f505ac48c2be61e47dfddeef2e0725d1d287da3706876 +745500 b614d96445616a551d2ef219bec80a8b104035ae75230819c08c1bb5d40d3eb3 e8a90e0d452a4db19b7703651fd04988b45763240ec6e769e681cff12a9713c4 +746000 32fd2569cffbaa373077a1d958628f5bb59edb4f1123ea22d827e59b58e6128d f0023c390d625e1286ba9aecfc80f28d96109289f7c438379a75c7b08c915f24 +746500 51f9b04af9044618552cc030e88269c4c8b6f6285653003ea31fa0596b3be8ca 21e1a38a259322e17a4fcfc9d45cbfb75c345d2475f7081373a9e8b791e3d906 +747000 ea7dec43ccd90255b703203200c0b71d3aa7332a2bb43549c5b713bb03b77f9c 9cf126abdc8e883dd428ae846e7fa5d694e87f40d1de07f772f916de45db3de2 +747500 1b651b29875be11fbb4fa8ed4c857b030122b7facdc75ec31ddaed88e3623566 66c676aae65eae30d876db4035a19e8e00bdf9a9331361e72f57af17a3da4dcc +748000 55f61b3e6a5434428b1a062ee86523b68f5459e7615c29d83f01eee310ec66bb 4927588d5f0600d0a418298312581aab6d41144ee3bb87a6875d7b11c676704d +748500 cc910d9ef1a087c0c9aadfb77da9051aed5124b7d072b976f4d34dc74f842f14 f1b10cb29bf15f5280b2506a480c07a610de4dd63212ac5fb1a33894c2fc0353 +749000 70c48f09a23f35a244e22e7d43f245a20d1d171bd3958ffe261363536f92a087 5cf7c04ef74572576b5debed4c20f3446db017a0360142b325a330909c904b09 +749500 33d54681aff8149655455f3331792f7a4582c0f0a436b9d3ec0fced7a525ecb6 382f51b24f05dcc78ee7b0e02e1389d1247a30489fbc317e6588238d78cbe478 +750000 8e0b562cbf9f5d9e0539f9964745265261cce019e73e22cd0db86f0dc7049110 03a1b08260d16527973210066d867267be1f3d4692f3bc680afadd2b1653f995 +750500 a85d11029aa82c4dfcad63d3e6e32c4525f9d192fe8ed223ab9f05a3fc4a5d11 1e1aaa3b0c05e04b991026c5a9b6a56228b007a6f936902f7a80f1b8f48eacf7 +751000 bc48dc5090db1a9a8016b89fcef45999f9d6c87c8982dc4ed52e4b583ac2f7bf 94ffd27371d8ad63511d87f728731b0513ca8d8b948d8b777d852ea9aab4804e +751500 c45fd98831ad0acd126e9fc658da6851a2a14a1cb037ffa596da1f2da2fc47cb 82be3962eb66aa5c0fbbababddc65c99ac5c70b372b32aff94aa47392ce6b802 +752000 373e8654d6a04c16dc7ef938fa6db6abc8672283a0f027cc34eecb1ec872ab7d bc163b7e20ee438640c02e74d48ecd1f65a4bd4b16d21248663a48e63eb02616 +752500 5f217d14da39d80fabcaa06ee56ddb41ee0dfef892576c2e100793f03818145d 91a33cc31ca938ce0b222ee141bb4edc500263a0689d0aed50660f8e4581a960 +753000 3e4aa8b1ce5cd84238df7ad4558f411a41a674ebee9fbe087bb41e880936a7c5 7b8530bc78bb832d51460962af3cca2f534cfdc28760d7f5363534e68a72ec9b +753500 23a5236d81db31cd3e174f58fc2f9c1263365c35dcde4a20f315cdb01fc29d65 042dfcbd5f30011ff7476e398e4c4b90f890a9fa4eac0da6b809410c3a9cbba2 +754000 5b4879ebfac928244d38117df31bb10c9d7c9e8e8d2a35c5c2f8a230b4593434 c57c3095308f688759285e35312da7e6da208cba0f9db64180d551affaf107ab +754500 96cc5bb1028f5ad1cd8a33800d9832d85700d037d5746d6cb4e58c1eadc8b119 df2a0abff85d391ea42eda8fb0a1d7a750eefe9d5fe25efa10a9f46fb6110f8b +755000 e7129e48fecdac55aa3944a4bbd19eb16f0d8a73cfadd44a79d641aaa46dfbde 385cb4dea3040a9b4d475877dd9d3cb61e23091f492fc5ad741a5cad2268c6a8 +755500 93174ce679911cf73dee7fb12bc26b02889deb145dbc92d69637a85407812272 5f000b1a76187f1c309f9495dbdabd5001f69bee3f9498316d0277383d861388 +756000 d2cad51c4c7f604a86bd42e5fa0855488394561fd8b8e403c968676dcecf2f9a d24b9c50230b5c1aac5e5937e488ef65f19cc11fd4cb6b97497cea46061d7e44 +756500 2e24a5dff5e90a2bddd7343297d9e1cc5f1e8555a19606314e46c09962ead770 dcd2285a17c2b37a17fa6cd27899af70be173d7a3d5c28c7f3ba6cf4d835c716 +757000 d11e6ed1c335b9cbb3db5e79e9ac728fe2377ec42de299a318dcf31ffc545ae5 748036b1271dd158b39d7de65e91a95fc4e51fc0e9eb3445cf7306f15c0974e7 +757500 0b5f20e382095632dec99c72f66fa419e1ce638a80671b8ba91b9a6d24f51e29 d6181cc28a4fd461f6de97bb298ef188941d1765a5bf650e95d418fa8b7fbd4c +758000 5a2a205c4c51fd035a4f57e79f2d92ca5e3c9af3c8c2beba21a442fb02b2c705 e019ca24e6b67025cf564537c25fc2e6e241b44730988c316eb32aa8afbb6804 +758500 e33c965b4ebb8b924a1fb73c65017199c806b762d4441854b79661f4de03b427 313706df2d3cde7074b2f6849c1f134ede50bb46a297da3884d21a1828ce8072 +759000 31928e69c1bdd6c727eda5662bce3f72e36496d538d9fc1cff1424bfcc24d0b8 98b76d0e9682ac933b75419da77ebdf7ba10f016d35d26638915a5ba5be4db94 +759500 2d322b637e8d92f51372101bf714ba726396d413b0a8e9ba573018ee1023b0c1 caea6763d02b49152bce73655908d1bf489b2918b8514982a2fbfcb1099011e9 +760000 1cd9bc900f95c2e15a802f7eceb33ff51dd12a71718cb4220e466bfaa0f5ffaa 0243b603adce50f296920262a13469d2ac1d0c42c2ecd633feaabea80a4c6100 +760500 4de4cae653ec7611cf7079c4deecd470afc39ac6a083a7b5ebfacd9d789c4394 d3a390c8ac0a632c7565327db3e9a4fcc30aacc43e2ba57377a1477235124baf +761000 6aee8513d50a19fdaf21e0ad49ffd92cea289ed0fae844cb3afa16ad3e60b934 045c6ba4c62d4ae995dced0633d85b8e120542be2533abd9fd1fc8c651f47563 +761500 b1917f5b3a3f1acd43c09e2d975abdf63f4a2c6d0b8ab429b47f467020e5b31f c7f69fc7d9f169a97f9ce25956c25668cb780205eb59dbee76f592718d74503c +762000 5f11eb707b887cdd88dfd5f98203a949824d1fe960c4c85525995a377a39a79d 6a3963332b853b978afeaf71fdf868b0d36ab203773c958a5fe5a427013ad02d +762500 8c3d3c496b6ab6a1e56cd5fceb131d8a9dd8c4fdc938d15e6ed090dab25889c2 e6eb9f858bb451d57926036c80c3c7ac4617b7897b19f67e2cb1952ecab6a24b +763000 6fcef4afba243507e7d54aeeeba6cc7de011b43df9d90f364794e3c59621b980 47461602514b2ae53177fa064e5abfc46da728227988a9388120901fb2dc9a94 +763500 41ed18026f39928e570601d40419b18758312017a7c9568d73d12d4b828a121f c04c199a5b9d12441ed489dcdff90453791925fa29ab6eb6c62b0c4d648e28b8 +764000 67e928458125fe2e3c6970092328b48497a4d26c6b04b01e53cac4de70fea64b 9270bc1fbd0aeda1572f2c2ef56dc700086652533d70be2fc6d13e5bde70f8e3 +764500 015f8999e9b59d173970ac6f7b5b6b460879a56fb254c55fc64c79c2f1ceb2af 0fb0da763fc466bcda98bdb2d28195935e5ed9c8d35fdc877160aacf553a4a3e +765000 9e706cdbb0b6dd01e5fed36ab646fc0a80a850e08f2088de2136388d4db8a219 f307dd0ce42f0f743054ed8236b6e8c44e4370634d9382ba439ef593210f14ca +765500 76b996f89bf12e2d725e1df6b6e1f9fb71a0e4741add4657f8233fa6b1a9cc0d 1c1d390e6e339f40129dd63bf89b9dd34574b6c0e16739449ed1936b023f73a3 +766000 46b8674f0faf71bdc7bf0a656e0e02660d90388ec7c3df894b0b3562d8541ba3 ed993dfee8eda538ad156e46f49e73c96e6abeb40c4e2ce1b34751bcdf4e7bbd +766500 719246e48b8d681ea5869953004219e8e9ad1ae095d9f7005b39316010542a79 7d760e07389497315c4c3b10c43d939fa4e1d43dc29fd4e898911ab78a6943c4 +767000 281b1c63ced6e6a9e548f01039e79b341b4c75980548acc70b0c52d71feb8e6d fc8bd623900af0aecc3fdec5de0a55fc7889c6b3005c4aa8fc3973a221702eaa +767500 d9a20cb9dfca54abe3534dbcf69a04b8f6b43afbe3703e7aebb2bee8cb81f73d 899b241bdd3264be250d3f665e9930a6b2f302e1eb87ef65845be021d856c35f +768000 8360e09883922bda97c55167146f583896798f25e12baf10cbdb39254ab11ba8 fa86336cef4c01ff1afee67169e0f4a3810546750ff4c38cec86e37c27444bb8 +768500 ffb03996ae3de8c5bf73eeb51ca0527f6ebee4b20dd059828efad08e7e6601c2 df22eef90381eb583a9e93e90e0722e0a6210c72da28deed40c27499c7a5ef21 +769000 2103acd9679638073a79876db195d494213d9d6e749faaeaddd6c672cc7e177a cc3a63870c9ff71fe67008397b61d975a8147d03a0d8d35a3565538ed514b4c9 +769500 9336bf1ee4de809e164e98a29bddd7a448333f2e25de891b4ac528d1bd7af785 605f77b3340dd29717f7ba780e9f4ce15fd0fa43f2ed916b9e54cf23aab2863f +770000 6182eaec959caf801de20f3000efc3b9431aa1af594a05d704bf2c53d810fd83 1ff43d1f6b27ee4063f01fd16f49110f0db50d58ee4937c28fef346f0777335b +770500 5405e7303c38724438e124b5e518c9af26a0076596be02cc57348e91a89abb6e a64f6256dfe425b8fb3b98b45dae59bac34213b85fd777b91d08d15a62a25623 +771000 07874de328fb41f73e1aadae9e57705d7679e8e186b8b6a2ac50ed5d1455d1a0 38084368706c2b4c84de621b821faec1466db4cd8334bbb13e4eb6960dda4b5b +771500 caf1cde2dfee0a11872272140e8043b92a33ab604f0b8a8c34d7a7006a11381f eb96afea84eba379a82e30b8366611d133f219dbbc728f99d55419e5494c5b6c +772000 3f8fb7f8de518b519eb102a29f372ffb4627dca2bbe06dc035bf7ebd4e6b733a b6de44e3b4a2a16494b4d6d7e5423e1ca66faf4ec0916a8ac97e4a3a656fde81 +772500 2fe560018de02c9be5e9cb02c0d2abcf8d6efd94390f10d8eb84d9dd3964d53c 631496990ae92715067442cef87711925361593696c1ac62ed053618951f9d76 +773000 53c1e8e472ee4d6bad6411fdc74d9fe4fe2564d0baf1bc5107b9dc4ec6262afe 8d48f77bfb9b49f20dfdb2168f0abb988b8ddcd8e9e62ac35ab0a2968b912986 +773500 2836b1c2239c528ef1ca59823417c6ae51c93040e1a5e3ca11742dd10350d3e6 16f14274772f96d6d2072a33cafd5c8f9cf4bbe37111b3f620e04bfc42392829 +774000 69a397d817d7155b644d650da51d5413ed2c133a3cbe2b54bbab7ae7e5a82914 c50ff677b8441b9a940984cf5ddb46f4b690ffbc9703ced2b163d89b929830b4 +774500 e7569a38cfb85227d2d2189a89c59518b64da01e2fdc89bca8e4759269fad47d 3bc601776ed5afb50fc51058a988d114287afa8fc4ce04c8d09215ad8ee3e9a4 +775000 d62bff072790320b532267e13d16eaa2b6124b12e9dc9704ea8b3472c33a7584 2ccb3c701d5d6730cb7fea0a703c1228f75c58b95dd167e135689d020e686cdc +775500 69206115fd6be6c73b95c85875329472d8550ea98bbcdad27b1fff5f13f1a383 1044c0b51ee23082e8268ea9f57e614ead985939009b2d6595f866bfd407c543 +776000 acae9ada07a40fbcd1abe244a88eea4497d892f56bc6c47c9a3256960c48a674 fa6f6390e5912cac9e116a1dc8ce23ea3acd3a45ab8c372f265c2a813a1d43c6 +776500 c7800bf474c58aeae4052c7de8a9a5be9ec371403659d6ce87f478083814cc61 5801468504c422c927e64b555cbde135e90690036800af540e0a515cde84a6ec +777000 60999146dcce1a4d7c57777fd14c438aa74620575ee304da40f3139145be094e 761c802120daf7dc3d71945a83698a8ec2813efe961ad3ddd5f0e8b05d88824e +777500 55464b5c24a72ca78e577947e5e31095625d4a5453d4ae8e1a628586173696cb 7198361fa24cc1510d0eed076fab9402f8d312924dad53b2cffe4a2634450753 +778000 a96e3ae7c78171f4839888370b8ab6a75527c35181a76170fb0c0048a340efa9 40f745faad08e9ec7fb07610cee6fb0bfdc4b221bfe568e692928cbd556fd0d1 +778500 dd54a9dab8dfd4362d396a50863007b96b150c454397a2b4cc486f8b4436acc4 45f3cb013e25a9a12e1998baf79504649e32d0405bda1b8d6cecb7f51597dac4 +779000 04bc1cac32dcf0121905e3b0f8bf15938de5a5470e130dad3d5e9478e5c04ded dfbf8b41e5cb2498ab9f6c772c18ae3af36f693115fd0b7ca12fc18fe8dc9d6e +779500 acdc4b3a573253149261bf7b8483f07e26febfa36a79b0f85663d0418bf89a87 c4e9858c10b84a43f0d48b59f2aabbb978411667e1e740d26cd90e824e60bbcc +780000 70e69b1c46b39329196e880591e35915ed2ff68cb83f2899e1c8a09885462ddd eddb55e0d58a9e963cc6bd8e74cdea45af4a42783f9b070eac50fd00b10933e8 +780500 44f6b73dd235490b827c83c949f8116d3e794d139ff88688db18a6b45e1473b1 011d86e2b32e548d2143b273ff895d85f132b7ad91a6ada40d29517155de88e7 +781000 afa04315bad0a73e082c417351c846b882f070fb89fbb62f94bf6627355eaba9 83f5ee23a34675c501aaeafbd8589c2173bed116beef5a6c05da3652da34eb4a +781500 daab9fd8e2a04d026d3dc748d775734b92ba9d42af448b88c4e54313458f8e55 edfa9dc118b24ca0b01976fa94551d0e78fb276ab38af92581b336d08ad979eb +782000 71ac38f6202666bef6bd02261cdf9590ddc79b15b1f0fd74a0991f32d7076cd6 d02102ec38f004cd966d4e8d53db021475903f0630404bac02af92fc1ba0f055 +782500 7065d66fe20e800ec4e007a0cd3301a98985fa7f755f0fd904b2a999c45f16fd c68d77bdcd1b1080ab776a210493e4371a1d30e3c73d7fe871e04b9345a0661a +783000 d494fe23c434284aa32d3043e1cc6443fd7bf9b36fe12410f7d2e35f5360b5e5 4ef3f9328e1d2d96c19ca549d1147c2063430601ec62870fc66c5b6e68aab6bc +783500 568a8df7d5669899b02e916c28755a8d1fc4a111f3af3dfd4195298ab257a847 7f752afdb474b41347fb14da6be44561bbd5dc034ad25035718d57ba0e326165 +784000 98aa97184148b247fa71460e2ac43389d0d04b6265546ce0eba7c2be328b6bbe cbb9d6529647174626503265092201bc8ed564798e1244839a9a2f022c5774ad +784500 db38f59e0843aca6f105b25581ed990384b8b6fa6c340cd21be37799ac892e8c bdf27aceb99b7e0386f937d7c1b8a5a8cc92ceb95cb3191e5502b41245ba3465 +785000 ddb1f819bd3e7a89588081208e3dc87ebca46dd381593d402bc80aeede0cd724 814c34edf844fbebab1739243a0a664de608eab49ef2d91c57ef7195bbee80f6 +785500 92f3c145e77f1f518c1decb6e99b826ce6d778c24634b3d6d3340897cb670c63 6d607478b75d072ff0a14c4c23454a04b9a08d3f9c196ebc0b817fc7b9efbd30 +786000 07a5df141fe4feb999505fef56d930e19c684e5e452ca352f0fc49c2d9eea76f 192297c49f950aee2578f0ee3249bdebdbf6955714b4a4e05edb3e8f24aae8c6 +786500 d9b454e9c5a5a6f9d16d22acadb61cf1ce231ca4dae12878d868598425eae775 d47d551d3fde80fb8fc6886f235fb6164cd99133fe9dedc2bdfeaeb10bd45308 +787000 1d5ad94333c42f9cd396bd5a2772973ec2440f1d9f3cedade83af2e4951cbb32 0cddc7f86a6714a3e3d2d2b004d1d3869d5226992f0bdf0f0d3bba94177b6a49 +787500 b454323565de62c5e27b717877037c918e5c046dea15404d0817ed066267fa1a e428f7829407e31e1079ddcac80dab9b56743be59912ddbc0e0691178991b568 +788000 28c337da150a017aca9d5619b15d605cfe5c41838893650f9a8108c4844dc5b8 0d7ed84426c912c0a3d3320009e66302ea92039ebfa4eeed9bce380b2b2df8fd +788500 5f6ec263add108aa8bae692f8381bf9e71a615ff5d4dbbe3785167a10465dfdd 3171b907ce8071d79c40b61ab802f51d8f1ecff22c4cfcf618065e8bdefa3910 +789000 1a1a8318353d2997eb996843165ce0a2879e058d6cc2498ac43fc495f3010874 0832a6b246a8221f36b8cfc3c15c743878842545a3ef3b5ffc0f1cde8246b72b +789500 88387f881fec667c0a2943deb34f9e508a994383c3ac8884d24b1dfc400344e2 866305720d60de682a074333f7b1512e45c63ab0f9090d8791f315e199222519 +790000 1659d12734baca5dc87174dd4ea3020c907031df843af6bdf22cc2ec8dea611e 63e7e3f379c2e361085770ef24c82f250cb47056045bae37c92cd369aff4984c +790500 a01132497c3c4a8622f3c0039f319e7bee99942268a0ef323cda3fddfa11f1f8 aebda06ea6afc6b95cd31185250686bdd815e89025a1b067f47db36dec1fb8c2 +791000 9209e4bff9f2c45a6bf3d2dd4f8960b050fef2a600c1dbafd65dda57bcd909b8 e005838fe0d4d7128702bd61e7be6be96340f3d4538aac8ec8adf6955bfefa94 +791500 facd9b684bce4af3ed32b93fde842ee8d30775ca97b1b3a691ac0f267301b280 df48a7e529861e94604eaff33a4ecdd7e35be502a78ce16aaebc413f4f4ba59e +792000 8439d367ef216677cf50d64e35abd6a55d555260d8a4f53af86560221aa48b07 6b7959d457444a0e8c085d2f366d54460a8e8ea5691d8d0f198275ef79b72ed4 +792500 2200f9ce62b2fbb1b25ba5e05a09301f576ca7606c2eebeab05a7188ff5d714c 2b7504213f6fa99dda613f8e7bca2fc6310930eb40c405a06df793406d146b8d +793000 3c520432d9a47e6e272dcdd99e656b04a1bcabdaafeca135cdb61b105b31d413 6f6b8a462b2d883580b1e2acacb9ce051016c06a12a87219097e5327c39d84b4 +793500 a9ede0aed7bc7639d97fbf8c81203349b1aed926ab534ffd055199c047c427aa 6b6e849eb0227f85c0faf658c06e5b4b75e15431263bc587a0417b801910ed72 +794000 8173afb6ef69b30acaed892e2cb93eb9186a55ec63a8730cf5f23909c13cfa9a 4f340c5f5665cf08ec366e0ffe561908c19c646b9567ab3078ddc99ce216fd32 +794500 debb2e81b7e22fe9f49c42c5d747837344567a5d301f900731c7ec427bc671b7 79a735251474e25bff8fd23b469f0d79eae7b941b45b181f475d7bf8ac2fb842 +795000 a83507c3c3bd71dec9a8b3f471cddb67361e6401767c8c8ad348926ce4830d89 e626138e04de566aa065b5a7106daa670c614de2ab4d326523bfe5d1a4ad4899 +795500 0558ab8c1e1ddaa709688f0fbdbc1413bd8f80362f6e053f20479e9aeeedb4a3 13a40efafdbd6358d9ef9f1b9307a88b458e999100a93b8f4611542996eaebf4 +796000 a141fbfbd754e10fd7e956c10d459834261d3571affa49b8fd90c0c804d0fed0 00fc2ab3f76f3b577f97c348313e9256331ea81a5cfb7d8466fdf144c8e9fbcc +796500 7351e53a275444b95005213efb4a5a1397a7a01d58b750323bd37ac47326c704 57ea2db92ddb3e36ad171b3bacb48f883fb7e8ef74466fb769f7d5553be66c0a +797000 80fcb6e51160ba191312939b815fab48de5a56f4121a3d2cd7b6925918a07af9 96242babb7769751d636828f5fdc2d1a44ca57e399fbe5b0683d09fd66b83da2 +797500 57bf7c33a40e1813c1947efd7412a3977b0d74ebfb6e846097aa45d03eb1fe47 e1250b6f44746881c59360dd6144bf4d2e86f8e31e68b0b820732b2a8bd69d5b +798000 f3370335ede3598caea04a87041fa7c1b51e5c9e1c5e0d50bf2bb136a64cd167 e6e1d946fc72e48f0399f980e5a4f7e544b4eb2997739223c355e3b15af48fed +798500 8f2f93e0248a8d8ded2c5106fe5034916127161f1a554e2ce307c5b6afc3e00c 2db41c24fb6ddfebf0b3bb5d74fbc1cbff4185082e7c22ef66c336467c652180 +799000 5c376ff3e907391741b0ce9601a65e616d6183cff6bf744bdd6597c455b306eb 9b86110172b3879f17995b766972b505bf547f40e1113cdb52533357eecc4ef7 +799500 bd143703c66bf324a5cbf67f9da778755b4f7a598450c328fbb011d1eba4d96c a4c63a77ffa25dcbb59ef8e829106f61ad3875ad2b9485ee6653a83691bc9ef2 +800000 e951054363c21bdb8e2c65154dc8453a7ed6908ecb10ed72fab814899d3cd345 9a0b2888559918507acbb704a78888f3293e34ec18cbc9961ac7842ca297984c +800500 956b9c5b3c055d3ff4ca81de618186166629076e317b2bc429faab8ed5a9bea2 9a80628c0558276ab328fbbff05e76501ccc20593000dc13d57eb3ac4b618838 +801000 3abcc1a5f4b6411fad8b518e4e0266bebf4d74ec36400e0b242e299aab5d8d43 6e72ab4aef323f82c48dcd697cff16bb1f0ecb039a1a892a74c57abaed498d92 +801500 c49350d66b895f226052f92ec5d5f35b70596d37a64fc2f36e386c21cdc224a2 1e1eab3a1d14c473a254babba1a8c9f342cf97b0ef0f60c2946149c72cae6423 +802000 f739f9b985ba0ee82823c7c03d7ef8788d23c01ffa5ab040ab23682d5e1f27f7 45786b2936278a79a8d856f96f53f0d4b09a04056b96a10d5bf71f5ff849cb58 +802500 1bc5ec3d7f952e5f27e9de2f21a30fe9333fb4a53b9627d78567d789cd578098 11b12b1784bf7f779aa53e67753bb6eb9d1fadae56dffeb1b9efb5f1b1ddbebf +803000 06fe520cf2eb81478bc9548d5802a7ca815743a5e541ad64b995af5b177ed245 f2401149fd3855cb680fdbacdb22b368edb2a34f73c59af55099c2d44e79adf4 +803500 2bc1768fa981d73fe4eef0174018988fcc0aaf5316c8597cc1c1eae8618eb055 b1a09ed639e05848794994217faf96d9b66a9e5ea926a08efa7bdfcef312b107 +804000 9d0ce3d35c935bd67f2970d8d3d5b098f99f936e880f9ec87cc5e0b1f837a0d1 4eb8648f841497a4bf8388158844361e138eebcde30aca295e530b9d97bbed6b +804500 87f582951ccb498931e71f194f2bacc2aa593bfd236519e4abd2336136333283 9ba93a57335f44c4bfb962c33fc0698f6846755d94ab393b23a81fa9acf72603 +805000 efa84f401e0f8a68ac402f09bc3988192fd6c85f22fdf239e5b76ce100e3a2df a4a5b65c94cf7ce1d1cfe3c087f63037ba522ee4916dc20051d5953f6d42e067 +805500 c0aa476e7e6452f41dbf4cb5bca05893a93782716c3299f44cf31670079f3190 349733cacd77e399f52831dd1f33884319cabf7934c3705e798fd8add0d90247 +806000 8459d195b63e53f3495da811539f2c883361bbcdd4b0097d0ba214fdc55a8219 9fd38c1f19472a144ec9b8cf57d8d9a85966dbc99cfb05628f5683dbe75501eb +806500 8ca60c34843a452a19d05b38dd13aaaddf0cf602601ab66769801c5443380904 f1a31e6d1b4cf5387c7def82294d179a656a10f438585a643e49dd6372982193 +807000 ca98242c7ff34cee593fe6dad4393698bf5875e9a980928363fb0f2bfc01e0cf 040fc4bf74790e2218abe48b6e53fbc6103ccaded8f389a0957e9008f96c9e2d +807500 39be5b7e38795922ba3653d1310f1579b152e493ea3c8bd6630e0a561465fd7e a88ca5c9790cb9542ef0a64dee1b5c001d56bee7636c09105a95abc859ca5335 +808000 8e9c44d713b7bb2ad9d8cbb9ba3d9aed199b4a70ea1f7288795ca86c966e323d 2c9dbc0b472b7ae26418b1def60df7a4869a35cf9bbf74b9a323d08035aa13aa +808500 dfd89eee02bd3dd16482828c743927801121f67cab61a4335d4eead749876a21 549d7b6c5b66bd74331c6cb6f937c8cbc605dee884796067f42fa1bd04bc6a86 +809000 676d2d9df28b1d4bcc68bbb1606033349d3e3a4c11790eef43f689d5e8d3ba7a 18d4addc8cc3174723f9ce263cf632f3817d015245ef3fb6ecae87aed807b0bd +809500 001261eaa38a49c58facc31b6c7245af90f5ccae8d120be1aa3b23f80e68513e 1489cbc34c82aa22a7de85e615f2039703a23da1494aaf7b9d1c425e4839849a +810000 051b563ee0050d2539bb0f57e3a5b5edc759af06e4dca9ecc9b651122354d828 84359c9f7e1bd44948c4df3a1affb6a4ee05c2761f3b22a192faf696c1c8cfe2 +810500 9ca81889bc6223d0195bb98eaae9b2b81771b202153dbcd4aedeba781063e4e0 9ee6f32bb1bcd2ac2787662019ca595b8a2569bb90a38be9dbdc77fd30e0f541 +811000 e24027de80e7e3cc2e2fb95439a4bc88541b10316eb1b111d107119a154ce444 373b8d3021db012bcd4b8558235d625a2d8eebec48f5c9c8784ce80be7ea459d +811500 2188003a3ac1f51c3147082a9737895235c62f579500ab7207e04c3db272300e 5d5c778748335e1c6a4d7b72e799a84b1128150bc5b782238c222d234b6e916f +812000 8edc7d32936aead671b293ca3c35a4f2cd8da3da8d0a911eab810aae919c671a 67921067e57ccb2692bfb932c9fadfd695208ae63e41c58cbaa2d1ead989b572 +812500 f93429e089b8304c72beec28ab2589d33557ec36f12f1e5c21acf96a1eccbbca bceb1b621ade4000a2e9855c387f4575dc2599df96fff5fbb43a4e934612010d +813000 c50b28d6a65438104f28a3518afc71932e5acfc9cbd3dbb0870dbdf8e175e159 b43d73d2203a216818e42d28b5108016335c606dceac6f6706641e240b065929 +813500 9ea262207c1743a9666200b8f174328d53875437ef4238ac0f7f9f1e841d52cd e915810eb721a46d939812114e3017db4a2fcee9c0d3b6701f070d61260706a7 +814000 7fc202882e511a8dc8b76ba9fd0e5d24578c5c45f51998f83d22e5c54eb72c5b 69feb5f7c9e1eae4dcb884df1bed34f628846110480539f3ad02db9e49c87f59 +814500 946dd535ac1806a838ff7af300006a3b019926e3caef42f04e77cc50453fb597 91645835f2dfa4f7bcd0818c145f349fc912335d0c28bb21f5a6c88833531ef2 +815000 32f06e0d2112aa022953d25a663a77c34fb1f0089091be64755920e082588a50 09503cf3aa4c5c2dcdfed49412a77bea1120ed6741f024f25384f9a2f6865f4d +815500 ac758032ea15073a6e7063dc49bac109b04e84245383dfe4a57d8a2d68e74c5c e8796fee578d7b8c3a1282cdd6106eb5be84df03f16f290b1042788e3cad77a0 +816000 4b0a8e17626d583d00af2d1e0d3d5aaa733b2637c0e7daecdc9ca7d32ef78b16 5513bb0e272bc3c35af92c733ac8e3f54a4f9280dd6305eb3f521c0a798864d8 +816500 7883f443a30cbe6777c45da4c70e650bdbdf7f2296d0eef9580b412d304dd1d0 83b8b3ed3caa544c6827b267919896ad0df9b50b416b4092a81c4a16cac4a2e1 +817000 d13df124f351e01764c49f28bd7bd9d94ec697abb36970435ede31462e245b78 3668760c6bac61282ad6173be88f0ac74108aab7e6b2d5b89bb05be5f7450139 +817500 cd9bec7288443f2289c3e350aa1e2f133d13377ef45051c8ef68f588f0ebfb9b a6dcda37fb4542b47d6358e6f4990494b3889031b7d57270d4581c3c5f5c834a +818000 db2426a8e3f0260a1385c6514a424c1db30e077cd18f25affc301b857899672c 1e887aa787c16fd0a14425d7699a20ce4a184468a4e8ddda982637cd60dd684e +818500 475ceb331fef75bf43194e6ad78493ee5a3132b1366764166bea3f98214c5e02 67e95130d865ef6b96c3c3594993525b0ed740d94de1322bdbdee1ca44c52be8 +819000 8bb061140f8217d2d20fc59542169fd7afa823a911f74040d58b3c86dd366481 226b79c0b56a187130199fa71bb43f0c0c17986b38be4bc953aae9035b3b137f +819500 9fa10df35922528bf712983310840e630f4293f91e9e81ea9a6f416107b4b385 23400d8d0d4f2eca02b0a320b012d9c3874250e3703defd32a742ff3a14161f2 +820000 0b83d657248250c7e9d96015875066e2e5d59b7a0696af84d83b246abf9355ae c7575028b8e2df600f1ba0acc5a101952b47e9660a8a912940a2b548172a05f4 +820500 e0176c1631f2909dcccd2e56971085cc4a435398a3a967945666e2d7b3232d1b 86cbb6afeb7fa1e6ac902b3d28f2aff25971d24b6cff228da7e4c8046b397ad6 +821000 be0584aaa7571f33c631f75d7881d80e2a19f6eaddb91f311547687ab8b6127f f8edec8d65127f3eaefc274cfce29f29c4cd339739f2903ec60759367f283f67 +821500 fd2e812d81cc2da106beaca8ac8e7ec4ef854416f30d7428c83dc72944e01ed4 9bccbcaf94c587dafd152fa579fe7bc4e87734d1840e0f939f494d1d60dcd499 +822000 9054e0ed1b7621397499863a4dfee073b379cbab6d211118e0daba86ec09b239 0f78238858465e4308062dd44ccca1a81e3b1aa830378d63ba37cf340272f04c +822500 c50dafdd9aa14cb0fb01b2d54da426cb862dffeb6369c1ff1f27b86bec6ea28e d7d020758a8d3fd93ce7a603dc710b085d16c3c232f389f2518b96b88d903a68 +823000 6631367fed6a7ca98d2ca58e0ccb8060e9e0309df1bbb5d76ea337c386f3066d 4ef109d1b5fc3ec978f34d66b8d93cd3ba0b278d7ba9c6ddf4fc3d56039e0fec +823500 b924862a40ef19454769b776643bafafdf407500c16dd73a70a61ff7d033519c dc8d2c04b3dc1785a7a76698674830e8aa8371dcb815083d849851ecb47f1805 +824000 d9ff8b7426f57da2db3d9f855572b493eba90e61b0ff3b40beec0b46578679cb 95e502ccf5b34258f239d7a5aa86f16c643e189873979210e0cee9c5acda128d +824500 b668014cbc4e38dbe8870d15c7ece604e8a698dead63136af5954cc403a13921 b634009a41de61587a78dc3ab8aeea26b6d107f5e59df2e927321f31c2751347 +825000 ab26157df7834e458aacbb394f8f6f34fd489cce3c7a1e6251289401acc59062 73352b8b59b8fdbe1d83e82c2da9ed6282c5788802e2eaf3db7e7647e04d83c6 +825500 4447fc4bfa77499510c1f033998de510174732f103eccea27ffe544577472f5b f15ba38703c021fa9419af02b8210ad9a9658b8866f70cfd734e282daf996169 +826000 8b30104ee7551d9d564a9142e2334aade0c93f1fe7846f2d2d21380096a5a1c0 dafdfa617b0bb3f7980b4df7dd4d0159144b7973976db5e60a28308c0e9a2216 +826500 0e2e09768e2c0cf1f39eaab0b5182b1616b40b9a6d931946bfae09c246bff35e 417a26d05c6fe49e0800213efe648fc7fa29040f0f16b4e0f3814ac4087fba79 +827000 9539816f7acbfb9151d30dbd91509b104a83bc0564050ab5f3c3a180d1659bbd b8d3ddccffd24ee4f56c161acf6a739668f48a10fc703233bae30108e8b23f04 +827500 735c35ab86f85b3d7c430b126cda4a4c8e55479a414be9b344ef0749bb2381dd 526c10694250f563eb1f7e7be275d89c14893a95118bb5caa0fa3757d2f1b08c +828000 846213c0f91cc5f47286bda5e74a48dc86d91bed3d5549f2130211dff2210540 195e0a72d1b5934d7b37d8678c6ee6da4713fcfad761e397921573f0c727dcae +828500 033da5a6c02d67fd91df3e6d357de891ba0a8352546be1e4c3c47d6d7a0b8916 3d55e3405532f51149ebf67cc1367c225ffcbe2832a37814c37dbbb324fe8c6d +829000 b64d6b04a7eeb8513608c60f52b3a54a2758808beecc359cf23e5a1c9991e1eb ccd618e28a3217ca7d14e462c4d2df34473438c0e89e68a498b9d236507003bd +829500 31cc80619db75aad85974e5c52755375dba0dc8ce67a17e6e6e4cb29f60dad24 41145f9a5566c0d919937386229a00f302067d3d0dfdc369d866f164c4edce40 +830000 ba6ae9853c6f5efc64e12f9982df9f199587f43ac41c49c56c52a217c42d31fa db3584bd7127b287f20a097fefea21aafe602b6d36287248e5b878d8f59a55c0 +830500 881ba4b6afba166f55baadba4ff7b98076398db48b53f05058e4fcef5931803b f2d61ffdc8eb96d572eeb829ebe227489c2748aabb4e7ae8dd2d4ff04db6135a +831000 a49b5343b0e4fa384c6cdd8a9b79713ddefccf1cf1b9a9b8340a55ec56330684 d5d6f8ddaa5631cc30932ced1cd5151825bedbff4f7c454fcbccc0dedd8822c4 +831500 2325e3f54a404e7bc60ccd0acd9ea77d90a31b18f8368ba2797d9a2f7388b482 09565fb72aad572d7b2fbbf1b0662da7687c7d98db22f55063d753d0e59a9663 +832000 79353ddf61d1fd1c8930b19c8e2abf92382f86b7ec5d3de61a766952b465144d 2cfcfc5a17e8042417128b3e60f6c2f4acb4f980508c3d172e0530ef1322f55e +832500 15c4c20b6d16377134e683d2eb7d561d4276a32d61f2179b1ec4f6461ec4954b 8d2364c10cb39a2c74360fda5585ead678c0325656ee7e2803383cf5c9e94862 +833000 771fb7f8e60a4065f3977ce3397d58a58102e11111312bb8d61399be0774809a bb06d1ca6e8e92d34fa9f31c9e3332ee8b3b31e0a4bc3a1deacc6a9e0a24079b +833500 b9d939f1ccd4ee8a2b769a4b014dd932f2d51d765eb4a4cb45844e726ba26f7e 77a9c4bdc03d8413b3600eece8b0593804b58afc41204063c3ab92b4c70d2478 +834000 c7bf6a274ef1f46d5faec1bb73cea0d5873fd15340b0ed103b0551213bd0b168 89deddbe651ce60767e84fcaf8419a7146cd8128f439436cd9c5edeea0c0262b +834500 614d3beaadffc9dd3765a68108e9f2e01c33a9c440438163970be6883f71b68f b8cc0eca95d7f827ab45bcba1eb5640f883f803c6635d09eefae1ab30558872a +835000 9a9f3c4367e50c24b3210a0650dbcab4ca0762018f41110fd7d45e70a0270e7e d827e1af0280be63e64f467a81e39404d3798234593ed89f03619354cd4dd7a5 +835500 cbbc70202da7cdb4cde6059eda7529609c9781e534661c44a1f61e5fab4de31f 9640b254c6bf22f95f94b10ee1263d475d76ec7ffc8d8813c2a7f73a7ca77a28 +836000 63d55fcaa4b84b7278dc98564a5955756f3ecf915b231cd6f2399ec836537a34 be4ab8603457a5c080f6d4f451702426b19f87ade3869565c41aa9aa1cdb6b16 +836500 a0a1ad507cecde4069df4dcd3ff6b0e44b917e732cded5fb86d50acbae0009eb 09c9ddd16cdd8ee68faa85a59b8547b9d07b19ef08ad42fc9d98f9d85ae7f54a +837000 4236b2466078279f38a8df0df56bbdf71da7a184f750d439aae1b7b9b6a0e217 1b7b95d0f01cc0dda255ea389572d8d66be622054e9913e6471347d83070da7f +837500 975034c8d678099a6afeff9e8e37d1ee524f64a851ce07def65676c0ee189b4b 2dbd86bbab192ae7e44609388bd9036e20d6496962b2ced1b23eb764d4eeb3b4 +838000 05228288f4e87f9283efa2e6547316fb61f8a6c9a1c4b3dd1b919d02c4beff88 8bb8421d901f05fcb5b476b8d5aa66f4c380fb7b00f65833f6b0bdd6ca93f62e +838500 f165d6b92e8b15b8a276e7aa3682e61c44e6729a39111e1883cc8bcc78cf9b30 55e669d31736fed4be07280a142594c8f283e7a427f0630f9ee7f0b06411b948 +839000 e7b73701d281fab70e7279cc3406b961db9c7d89643139882ccc269515bfb8ce 44e7b9087a0f031995a963ba2c3d4d59a2856ddf3e822d0944fa472768990ddf +839500 a0e9976acd9fc6b93cedeaac38938156409b74d9192f1eb4d614d4af4072d67d 774aeb086be488987a9ff3d9d80fe11d7815c1061754431fe3ec2ff387a4e2bd +840000 85f4bef924671af404c377269d655812da734608007a1e43c7e2c974ff1a7f26 a7c27c2f0b37677d61314ca1a510aef72e2a7149c0dfb3e314c47ff151a7589f +840500 9a3f8a90c257352934919ba5678d710e02abeb3f8c9b609f00827147d2f7cf81 c9234118875f5ff26dd4c37d0f498911d1caba96dee4d33a50c7ce378a9a0749 +841000 51b45385204a2bec9cfb32ccd8677bb9fce49c7c5fcb6a53c452e9b36cdb0bde 07ed169f164ef448a9a828d17a6c3455f3a11dd6c2bda1eb38a5100b8817e04c +841500 92f53759b018613ada40999270e66657ae1026e4997862ed0d5e5560b917d6d2 37b8239afbb1d59bb35fd5696404860f47dd31163981e62738867af4d1d13c85 +842000 a1202ffaaa1bef1c9c95e11f9b2f1e1b03e69f535628130df12635ed9a3fc297 ff3835e7e4cb8e4514adddc8fdcce0466170f3062e57d9abd9599da0fd0ae1dc +842500 fcaf836bba0923b8d887f619a0ecd298a94f04a1127f82773a531d8951326582 7fd60c91ddf7611c5d0200f3f5adffda13a651c5bc71929b0724a3ef0c178463 +843000 37827f08056c92edc0a941127d96f9f4f481d70cf2cb6c8c0212bbca042ea04d 863b61ceff5d6d3314fb420c17bb16c00b220833a634f53d24f7fb6c9ec7f7eb +843500 5dae3f790ecdfda5bd46aa1005cdf1170b25daa976dfe3f83f07cddd47747f86 3d352565915c9634066580e57621f6682cc147cc92fa1e7f0112af699a2a09fe +844000 b3b067a2be410c5b04cbc3088bcff2678204f31c734440fa5ce28a43e61f6e20 dd379efdd80ed38f855dc2fed11db6947ffecca2c6d78c3b13d9e42de618eb95 +844500 d8ec90a7f602aa58fe31112ed0917f4597352ffbfb186524253bd4a88a283efe b8684b9b13ead86b6620d8c6cb1b4e3ae9891ad53c74a5ed93bb547b05d1a8df +845000 66ca29da4d604a893f7a56ebd96f365b3853ac2de6b38b71db898ed7226fb49c 2a671d1a790cf390618793e3a9e2995de070c431014e9b1c88c9c587cc12708a +845500 b735fe850f31101901fe04a4207e15ccae72039895ae0f149593dee3e6cf8a47 fde7269beb8e5c3ea60f2b30d1d43ed52319787b1d9363aa67c53e6beb187d6d +846000 b526a9a73f37f20c6ae046ec9b131f6354e90a4596cec60cce1414285c838df4 fd42d265ac2d194a94732500fcdaa7f4f2e629793fb220a871459880c0cd000f +846500 761bbfd7f554b81cd153149a73641b567685b60914402dba0ee171d8dd0237e1 2d9ea683992b346008f860b330b15ba6ea707338daba431103b2a444542f9870 +847000 375dd44f3bb80150df686ae8f3813999d60507c6e9ac4750686b5d182adcec3d e4ad131095150cdc0a23d0d7948bf3c870e01ed0b4a6f9950457ecfbb254b342 +847500 0539f76c06c311da93887c10f65904de31c72a00a2c0ee1bc66a46c50433c0b7 2ce4000e6e36145f7c0711f53a8b20144a69599490a7da79726938926685c5dd +848000 c6e8ae1f92209bde14bb652bc8478d5592d0f6741c3d2f2f232c037494d81f70 b7c0aedb71b645f0501319a9842e1fac4873184f50a0c5b758fd9f2cc2547a1e +848500 d302c73e4d2ce3e318afb5cc773b089956501f00f64de08073bde92b2f3a99f3 0a8901df9a3d00b66ed1bd0b117645237b61fd34c63fc45a0604daf2ac932bea +849000 0ad8844c1a45d6846464c00e9c6853719f80056b6f0fb70821536a133432ba1b 6478dd9c136dd28bbc4a49eacb7909c179abfef94618dc6da67adb4ffe69c6dd +849500 8ddd724ab7ab24e5ce2f5355c2c4bea780fe7a2b4a01fd04d6a0be5bbcf6059f a8ac1fba940445cb8dc5f6eec18d1546f36bc441db187b088b1054ce8236d17b +850000 b5e0c35c2d33ac37932b6d187a8839f1bf846817dbf5f4c58b73418e9a93941e e9f2c9230ca20ec14c17a35fa24d9eecf9119c5a38809c420ef099d9cd4ac61c +850500 7c1ea7bb106ee4a55d64930589f44b2e9fe63ab9846eb3caa545fd3b5597ac0b 9b2b304003bc1746f27de0d9a7cadc9dca7d6c87b6cd5c88da09f45282298480 +851000 78c3131d573b6a70a648b111fa904b99707c622bc56f842297953968e50bb804 9dcd68f3d2c985c324ae53f03582ffce484a70724e2c13602f84099737641cd8 +851500 e470491ea5d0631653c6200effed322f6696614bd98109f9f1c90c2cfc72f211 5dfdf81aae36a8b8aada229a8ddfba138552fb9e101f4faa56bc76d3860e7738 +852000 528590bfc01e4b7f567aa786f0de29c2b7aeb8dc3042114b087f761a5e6b161c a96175b5107c4129f6512d54adc38ebe2f236ed6c3f362f885cd4a96c6651e0d +852500 a286a106373a8e32cbc0b3df7ac65b0720c04cae34a07cf070bbf652474d8c56 205a7977e413da822705095027abd2edbe7c5e34c9afb7225721dd18b700454c +853000 a2d7a0b916cf5aff1d6cd5150b034448c58dbd104c5f88e9b18a692d4a8e98e3 d91fce318de2b273cbff6891c8ab29c769d7dd2f09b302937493a38488ce1446 +853500 794d93de5f3d5c39ad05ee77658de33f094880b037da8d2c124f4257f7360164 3f94598f08e53b436df3517458172624b710f4f9c108c463a39b2b816e511a0b +854000 886b8b0cae2d1c914ab6748d636d5553dde505c655102ccb384a5c9a2b864516 308dae1dc6e58335096c15c90fc276e9cfc6639289c25a9a748ebe226f37604d +854500 78d0ac66bbe4ed7830eb9025fb934514035d9440e2dfa75d4046410185235755 e2976153d1e9c4253528b84741fbf9d8bc73e85089d63a59b29d5c847e336c02 +855000 be6a06e20a33be8f7e0c96318ffc3ad202215dcc9124c06ec678a6a775c8f67c 4ce675eafc85e0f8d54f8281fecef780e98677534b3dbc3b1e9e7f7a529aa3dc +855500 ac409e08616a1e06ddc3e14a2b1f6bd947840932fc6e8b007246de280a6b66bc ebb67251598e38254f9327ff1dc2336da5269cfc209a5476260da523cdfee0ab +856000 62a624ad49bb1908d25d7190f363019b1cb5f1a1dfb849bb50226561b3c60218 ba4fef7924ee2e26f271a49319726894894497685b1c81ea84e0bb62246c24c5 +856500 f02dd187e61db50768a596177bf62878130d15910ced1f4bed2d5aaebcf4960e 8e9e38e2a88d1a24151ea08cd82c2debc7034d5fa21e75f926d9a6c0df18d6ab +857000 b2ea7f0e6a51ea74155d6964d732dcc4fc8a0b71c4e6837d1f17030d026da89c 8ceccd983eb05888198163e6a0ea44261e44876289a9effdcf3d377ccf62ef67 +857500 f99e644cfa949f806ea938b97d5232d9430ca11a51ecb0ed30ded90e6d4f120d fd26d9ef2f463304bb2839e8bdb3b5b0f48592e2cdfd043ad809292e22391623 +858000 9dfed199b5f19ad984111f21fdaad2d5d1779a317ba5982a93e14e37e21a2eb0 3da6db23d0fbcdc02c440eb755828b4a5c02fe77c75b153055eb25362399216d +858500 daf47ce2ce33b9db10011f6bd963bca3abe9b8b27e6101394d5516555e8e7bc2 4fecbb4ea28d764bd3dfdcb6571a7c6400c76c1e7f21a1c1407d057fbc6feb62 +859000 f6cbc9c86ebcd2832f2d0db57e47b611d187189a37f33f7588c7c24e2f2174fe e85af2f0bf8bae60e417cc65de17bcdac486348b40eb55da41a51e98b5c148e9 +859500 11102fd852576a361c4c9a3050eb8d605f317f82ee78200d2a2820d8a3ff76ae e70ce2ca1c2e9b818d276254f46f0d6156003a8421ffa99fa8499a09a982e1dc +860000 b0689ff799ad43c8123c38f815e8a5dfcb4fa2b2cc1d59dd08f6d6e6f260ccbe 60815a438a372d41ec3439f8b21cd8cbea71b090758260b1920dfdba4558f235 +860500 6298a42b99734a2702bcc358dcf100321fca31a1cc547ef0c168fa83a50eb132 2881b992eca478a15c1d505d3b021e2f589d536832e98cbb90831b4d0daff767 +861000 9f8f6446a6df4ac6674a0639bc616fbf8955fbd7c5a6ae8743ae009340acc6cd b9c2b8a675ec2a9f1323f5391a81d5a2bcb83ff02fbfb94e1960eebb50033bfc +861500 c53640e61534d7de4001cc4f6547133d225ebaf607937c6ea99b3a92e0f1b445 7befef633b3eb72ac1897e3628b20540cb0cb51bc447fd69877d178705b165b7 +862000 1bb40c878bbe264e2fad6a6e523345390091bef9bf689f6860bd30fbd27ccece 66bf4703cd800c13edc3e67d0889df0c4694e2f6545c5f71bb0d43d80adaaaf3 +862500 464bf61e9afcf7f9418661d069ebb015bed143a3c53449e9ca98013c624c0d01 b858f8d0afefa1f199f3434b656587228c659429a9b29876139cbcde4a66d36a +863000 0dee780a99a58bfd60c05f95db7ecf49c8ca8380b7a5b8ccb4c7c26229966441 4555335383dd8dc416b36cfdd110d6da87faee63fd0461dbacdb6cf9dc61fd1e +863500 98a57cc44810adfffccb2f42f93455e1124e3297a8dd4e176b6367bcf864fa6c b16bc478ec16263bc8995541eeb0cfc6640074c65853989f7619f1094b48f17e +864000 516c8212c055b610d7295dd1dd14e797a2b4ac025f1fd2aa7b3d26488f260a60 e221eacebb459d180c743666d691c11997995f6f4d75dd21613b75eed2be2425 +864500 2e8ffca67b4cf71272e42d524e1427b794a8e663b437c156cce8f9ad39d28b58 7237d3868b25d2fb9801de887b7c2071cf24952719c6170a6e550773a61a2170 +865000 995bc313381ad5d7799cccbcfbc18876582f5a82e25dc4a6bfd299facb83adba 3ea758c944b2f0be32fa7fb541e18f683734df6197ba711285bedad4f17d1c8f +865500 dc2fb63083a52596b814426dc2577b6eca27f5e80b08b83776c93adb0b913324 007ae746a4a5b901f91e51014eefab59c84dbec3a3e784711d1a16df0aa04ac5 +866000 a1a4a61f9e30b4ac585c2777dcb066e543a3ff03ddc20b67aadb9fe8842080b8 953ff29441259817927e36e3a89db69fafe11d253509c36a2bd00805563ec4a5 +866500 44fff17033fa5e2f48afd1313484e62ecb1bfaeaa0b19b1973514abe18381d2a 1bee0d01001a576776d0e6113954449de228bd3f69feea67a11769f2626176cb +867000 77d6f737b4c7b493657ef08c7f28b5dc43e3a7194b2e3193a37d1a931daca9a9 a9f847fa150f5881ffade66584ad6b5a741f8dcf9310eb808181c9c3e4724a8b +867500 98da2413dbd1bf3e2d3d6254f849e3a971ff9579e78a4bb1ba192f61a1ca1bf7 55a4bc913aa643d798a0d9d29b5c31bd38db0051ccedef30bf4bfb2ae6210159 +868000 1e33271126faa909e8b77bfe4431587ac68d50716e3ddee70f18f4ba56509c98 6b3ceb03adfc3ed05acb7ca6a2c9822e0b183c1359826f2714d26fedcc8b779a +868500 b4bc5ab2d0f52588e8e154a831e92f9bb249bb30c4b6a4e379d529d9c916508f e1810b871f581727fa81164f7ba37242a0376aa7870f897547fa032efef248a1 +869000 9db0fa91a26eb2c05272e50cfff46c7c9faa4d72938f14f03736c7084c4b2d20 e80a050a8dfd6c5d2ecf765f43222b8aec1a85232641c32583487753ab3107ae +869500 ca4ed5b1069fe42bcf42af8fb065aaa98db119b2e98c23c8ee9dc823c46a0bc1 1f45046c69978e3a13a013d995a469a2a6583b18cabe3e48c5735a1bbe88f1f0 +870000 02da00fc2fa6bfb0027e8c85a1b92e919495b84e763dd57afe758df11635956c 8ff1ef73ea14756b421192b3f936ed06cc5a8f016c33a4bb8c3eb94a0a4afb6b +870500 04cc97d0004ba00a8211986f0fa13bad1cb0e7cd17e302242128ba5bc923bbeb d4dce041cd8b2c51cfb7a50864de5d72a627a93ea5f5cc4d837004e9fac80691 +871000 8c46f91d61fa06dc47ef2715dd66a510f1eb909b275c85c0f8f914e8ef0e2ce8 a04657d974398377996f4584c23bf29a55085fd914b88fcc85bcfdee46c1f4cf +871500 285a13a25bf27fd974c9bd32ffda2796ba46e57044b4c282331cdf94ad55ce0a c701e8a240d4dce38b98c91b01c0b306ea6c8e72c345a2fcec704f4af6f880a9 +872000 0f474790eec84e0216b27a41df528a1037767792657a3aa79122dc31caf1e1bb 0774199988a33ae42d91de34cf2c1450aabd65b62e4792acdd67ee92c9d617fe +872500 4192751ee0a2c7b7bebe5e6a7c60dfb98f1527eb6dab97071b5ea89fb851215c 9e36875819fc969d8b5f971bce6d1610be7a012a520b317ce574c1e24f26c0dc +873000 34692e36b62fdc785dd9468d43a0a637aae66ea75c592d5c92e3b79245efe449 e97152bec752a6cda91c4153cf191e6a04135a2d824a6ef8ce6d64f640a97fab +873500 af2ef369df933f912140c7a4ba01d3e33ae803b7b2f4939f8a98405fa4127b90 3e30270cf39cf279ffcf99ad4b0cd55c85872bbf82654d03f6a9a40867ceaead +874000 7f1cd90d997b5a1da4daac3691082770bc7bad48acca998746980f0b57ccf21c d7cba24bf85848378fbc0f655a1795b9baea22b97a29b3bb1b74fcbdc25baec1 +874500 c15fe484c8c031d1327d24f1b563bae0c6ceb1f81b8c0b37c1a3a6d3ea9c12a4 532b3b617b128a1da69a7390fa37d292771bfbe039436bc7f34cc60d51ba1289 +875000 6d606825c8c4902999704cd9839f60f072743c33ef3cc8fe5b075e15ad6f521d 0f98e7eb858a741ea9b7f9260142cd07d98f3ef2fd0f5897f0dd1196506f28af +875500 8102b25248d5cc9fc5e8780fa7782a5f37113d5797ec544bcb6164d922d91ddc 5331a1c3f2f1e95e9acc0acb83ef4cbe04785dc7a319ce069d252cccaaa748b8 +876000 2a6d640eb47cbc93033640e4eece162640b36cf256a1cafb406e2c22f0c10fb0 34f34e19ede327aa5be14240436f18ead4abe6f226c1571429937380247402d4 +876500 61e5fae25c4f18e92e7adf64242dcbf6507a7ce4f1b0de6f2e614b8f1f5b33c9 3f421f95de7e2129353ef06a9f9a206639e0f5be018937dd02e4c39907b75702 +877000 46d077012ece5a31c29ba761425e6ef9d42501740db45016c8c5b7da1d216c7d d06c5c6aa3c05475f8a8649b9b6134e0c92301d47186f2c379e7b159bab2388b +877500 610a50c20c1661286355fad8c8d067e794b5ed61c14087d04976cf0f378e37fe 18141f4f7bed0308bddc68a7b897a035293fa9fdb074b6af3b271c2365a3d1a1 +878000 1742b73fe7c2d014484b21a1604b0a70dc7f12540d52b21ccfdc9f9ea72dc53a 90831e43dd3912ceefc00d3d544c3f26dbf358ff3b5d24041ba1f322be13d9f2 +878500 749fec5fbed9d5446533f22f0aa36c20b63b35116e6005ca519f7fe1ddc76b93 a13ee98f76ebaece600b0723a6ae7f7ebcabbb86e6beb5c4b9e5cf3e8170dd76 +879000 8c578dac00d2b6db2e1972886194a4be70dddbceeb457150cf1b74ff19022532 627f0854e663c40920d22d252b41ecb60a4bff1de055d1789727ff33eee14455 +879500 b31016513fc56e8df0eaa71197462492482734a9dacbb0d85838bf4253b70b30 854e8e0aca0625eda1261e56697d87244c1e386be8d6aa78f15de8de50a76169 +880000 e0764e8169753f7f532973ad4706e5f6a07e64b7ec10f64240c91f0d319f485c 4e1ebede695c7ee95e4442814a5d98539df2ba46757b95d8ba4f0e0e437bb535 +880500 b72623a454e91149046f522d60935871a1e5f098389d9cad4086119732b5e3f6 580082c91f04822d21265dcbbf8c3d3a0866171acf5564e982c8386ae5a3a40d +881000 b94687f79df272c2c5e85e21bff01136c5e99576571bbb8a635353aa9b331c81 8f383eea1f9a3014c27cc3c0a6196b0764c269a1f2f3effd31f4f6b76d2a33a7 +881500 5ed1eceeaeebe16a0c746f796507f6b0dde6da8c53f4e3502cf5a49f4ecdff18 901bbb467df17a499475e767cf7886763681deebd4bbb537a4bfc7f3dfb89774 +882000 bf12ffa015864c59f04c80d0141643c9de99f79b79c2b26ddf9bc398b9d8bf32 2b7b77da53ad2a19f61e3aa54ab5833bef79cb88700e51189ce7b1ecf001a2bc +882500 c8e8deff1d81c65f2b607cde0312982b03523aa76c6562c666c1dddf223e7432 ee0cfaf5a0a284a4e324c4725bc6ed5499cec94d170baff0b955ad6b2a2eb487 +883000 b12011d25c4c08da00b5acef266ab9e451d06c2fe9c65e4f149f7f0bc32b9c44 9b06de94a84c8e550c49947303d058d4a93d898970d50b8be9cea965ca656575 +883500 77b6c306c2410ce6e03c825d016aafeeaea3daa7172111085ebcc5806dcd3bd9 01d7719a498afa8051f872a3e8fc32cda3eba0e4151fc16722c0944d8715a6ee +884000 1fe3f4911ede2f37d7587640a80408092c044904f44900ce1bc41df2e0f887fa 8e9b706cdcdc53e77915c281b532f0589c649a80fb563bcedfa8fea8493d4a67 +884500 22d008aa6022144cf5b7070e91a056b437f105ee9ee3d343c2e1eb485afc55e5 90404898817c93e64a98b4919df62ae8a21e4dcc9bff13fb563a33d544b9a09b +885000 0d39372bb9bdadcd80ce022e52792bd5097a8235cc9a571cffa92805b0d875b5 92306cc62db985c495b43e84e2c4ee4c6310035952d4d52e4a13cbd1af0311b0 diff --git a/confs/BTCD_peers.txt b/confs/BTCD_peers.txt new file mode 100644 index 000000000..a36595633 --- /dev/null +++ b/confs/BTCD_peers.txt @@ -0,0 +1,49 @@ +78.47.58.62 +67.212.70.88 +94.102.50.69 +50.179.58.158 +194.135.94.30 +109.236.85.42 +104.236.127.154 +68.45.147.145 +37.59.14.7 +78.47.115.250 +188.40.138.8 +62.75.143.120 +82.241.71.230 +217.23.6.2 +73.28.172.128 +45.55.149.34 +192.0.242.54 +81.181.155.53 +91.66.185.97 +85.25.217.233 +144.76.239.66 +95.80.9.112 +80.162.193.118 +173.65.129.85 +2.26.173.58 +78.14.250.69 +188.226.253.77 +58.107.67.39 +124.191.37.212 +176.226.137.238 +69.145.25.85 +24.168.14.28 +73.201.180.47 +76.188.171.53 +63.247.147.166 +121.108.241.247 +36.74.36.125 +106.186.119.171 +188.166.91.37 +223.134.228.208 +89.248.160.244 +178.33.209.212 +71.53.156.38 +88.198.10.165 +24.117.221.0 +74.14.104.57 +158.69.27.82 +110.174.129.213 +75.130.163.51 diff --git a/confs/BTC_hdrs.txt b/confs/BTC_hdrs.txt new file mode 100644 index 000000000..374f793ec --- /dev/null +++ b/confs/BTC_hdrs.txt @@ -0,0 +1,195 @@ +386001 +0 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f +2000 00000000dfd5d65c9d8561b4b8f60a63018fe3933ecb131fb37f905f87da951a +4000 00000000922e2aa9e84a474350a3555f49f06061fd49df50a9352f156692a842 +6000 00000000dbbb79792303bdd1c6c4d7ab9c21bba0667213c2eca955e11230c5a5 +8000 0000000094fbacdffec05aea9847000522a258c269ae37a74a818afb96fc27d9 +10000 0000000099c744455f58e6c6e98b671e1bf7f37346bfd4cf5d0274ad8ee660cb +12000 0000000011d1d9f1af3e1d038cebba251f933102dbe181d46a7966191b3299ee +14000 000000002d9050318ec8112057423e30b9570b39998aacd00ca648216525fce3 +16000 00000000679a1ab3af6da03f13a0bc96d7215e65458b2d2edfa030b5b431e8b3 +18000 00000000f914f0d0692e56bd06565ac4de668251b6a29fe0535d1e0031cfd0de +20000 00000000770ebe897270ca5f6d539d8afb4ea4f4e757761a34ca82e17207d886 +22000 000000004625a14242beccb38c63a1f770a76ee5788764e6c0abd4129bbc1b9d +24000 00000000f04fccc81f37002707e9501a3f7bdcf25f65531f386a2da8af20122e +26000 000000006d6c151db6d4d67356d590a897a11cd7d8111ee989de6f2f548410bf +28000 00000000172c5ed49d7dfc29bf9a18a53fa2d050fa37aa210d6d4080fd0c7e67 +30000 00000000de1250dc2df5cf4d877e055f338d6ed1ab504d5b71c097cdccd00e13 +32000 00000000049172ba3ec1b673cf13e3d0049c1c07bb103ed3fa300e3833480055 +34000 00000000495968d19210d3be15bd24fdc19805a0ef15026b0bb4482b04a9da3c +36000 0000000080c3deea35dc3df90a5fbe5f27db52f5e01018ae7d62f8b454c71335 +38000 000000002dfebce284d1e08b6cf04452530891579b7377669865889498de8f3f +40000 00000000504d5fa0ad2cb90af16052a4eb2aea70fa1cba653b90a4583c5193e4 +42000 000000000f80c09687893406279f62da437a6a0b95b8dc096b30c10ce088fc64 +44000 000000000122898b31073a770a97cf599c00672fc8d6ae15652235862f8b76d8 +46000 000000001dd39771dbe4f9fc6da07327f13f894dd2c1a46cdfcedf930fbbc52b +48000 000000000f3d40ea2bfa8d779010e52cff4720c072ec4b12ed576cf5cf93c947 +50000 000000001aeae195809d120b5d66a39c83eb48792e068f8ea1fea19d84a4278a +52000 00000000082bc4398c4aa5bd8d9fc452d60d533ef68baabf594c9e7d6649049f +54000 00000000144197f54afa21ae7db2bc93eee604432101fc0ebe7966a52bb27e61 +56000 000000000dfa452ea45e0426dd8914c35e24dfd4399037c5e6deb9f18f58d6d3 +58000 0000000013e3791d288d9db814c52fbdf240b2206eb8e19d7dc80013c60c0c00 +60000 000000000b554c46f8eb7264d7d5e334382c6fc3098dabf734de37962ccd7495 +62000 0000000006dd4bc72daabef992f860e703820de119af3e24a1ea6f6c81521011 +64000 0000000003d7055b51d7b9ab693de84c03201fe0396af61dbb30bf31445d3f55 +66000 00000000071d7e8a0f4895e60c1073df9311d65a85244be1ee6369c9506281af +68000 0000000000d991791fdfdbccbbc2a73d2f86ccf78e2d0a7ce7675f40b5986b3e +70000 00000000002b8cd0faa58444df3ba2a22af2b5838c7e4a5b687444f913a575c2 +72000 0000000000eb357d4c6fef6ad9a6fade126985ad36042a99cf215a4454545977 +74000 0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20 +76000 0000000000571138ff757a28ddf9b56f28c4a461e170660bb5ae79a556069bb6 +78000 00000000001f3fe62641b473673c9ababbe207046a109f0861af95c905a918fa +80000 000000000043a8c0fd1d6f726790caa2a406010d19efd2780db27bdbbd93baf6 +82000 00000000000c9d1c4acc114afb58d55db5ec44a963263cf6247220b7a3f85c5c +84000 00000000001385326e30864192ba84ed2f9cbfadf0698655b1c25f93c92f22ad +86000 000000000000ff4e1adb14f07774dad6b34968a5e19d1a2fe1fc9157e7c2b85d +88000 00000000000ae9e98b82b39a912cdc0ebed97c26376780ac996c84d9ec3264a4 +90000 0000000000071694daf735a6b5da101d77a04c7e6008c680e461f0025ba7b7af +92000 0000000000001df90b0c523a4d7e4731336b00cf4ba9d8e02d111523df80998c +94000 000000000002a4c42580d51f0ddfd867eaaa790781c484c633a69167d17b48ec +96000 000000000002c86b568cdd2d0f4b0430cccf42bcde3361f63a32e23b5d839e99 +98000 000000000002272a6dfb695d9db936d813bf0055ae92e920c2791d4c5f7290f1 +100000 000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506 +102000 00000000000335c47dd6ae953912d172a4d9839355f2083165043bb6f43c2f58 +104000 000000000000a9887c91956b638bb3c0651321fdb24715354c3fc6633f5a16a3 +106000 00000000000058d919f52d255f394ed0aa3a344432676fd30f1aab4e10c22fad +108000 00000000000167cea0b43ff7ce22f330d3e302832187eb31c61b15bb1511e118 +110000 000000000001bbda3f22ef8e476b470a2d3ae16821c23a6d22db77318d0799a9 +112000 0000000000001d69b3899a49f37799c375a7471829953d5470f468f48ff70432 +114000 0000000000003195a1e6dc48a540264d37e9ef79b552bd78ea4b93a3b6e7e449 +116000 00000000000007ff257fb2edd3fdbd7b00c127a66dae1288fc5e26c402d13bf7 +118000 000000000000774a7f8a7a12dc906ddb9e17e75d684f15e00f8767f9e8f36553 +120000 0000000000000e07595fca57b37fea8522e95e0f6891779cfd34d7e537524471 +122000 0000000000002fe5f29af38282ac1c8f4ea2bf8a0855946150130419491b6c05 +124000 00000000000023e9a0523cfac29afe07a07acf81e273cd892c51ff8318846620 +126000 000000000000166b7d480aada35af1e6f9a2835d68f9c2fbd272073dc6c9d5fb +128000 00000000000003b8ddd8692769e1965554a8bb030863e0566a28bc0dc952864e +130000 00000000000011906b491883ab0f16f0e690b133ca860b199b775c3cf6581c21 +132000 00000000000000a7a0483857f0d951983ff2834a47c38fdcc22563ac0f8f707b +134000 00000000000007e3e442ce1423496a064a7c34342ba98be164ac0c9f9b872213 +136000 00000000000004da0d6d69fd474fa08fe2ff3111ff1e9e01f72899dcd9d897f0 +138000 0000000000000044c7b6a5511c0b2ae64ec545abccac8053f31cf7bba23bb886 +140000 000000000000086e28cf4717a80066def0ec26c53d660582bd997221fef297db +142000 00000000000006379826f5f10cd23739b9c29f87ca10f199f9f4b72006311f85 +144000 0000000000000681a73f1bb50454cee419048d24e1091bcddadded89df53fd07 +146000 0000000000000188cbeebda87456f040370995dc11eb3a1e76b1577b6e0b588d +148000 00000000000008be94b219a94752bde6a6a1c5b9d72abf2aaab53df7d93c5fa6 +150000 0000000000000a3290f20e75860d505ce0e948a1d1d846bec7e39015d242884b +152000 0000000000000aca2b3a267dab498adc48afd15b60cbf21fa58dc26c86a6dc13 +154000 0000000000000a7446d1a63b8229670aa02d1d9fdfd729b89107fe5d88dacd8e +156000 00000000000002adfcffbd5f09744ae3b930597dd0ea684cd37b816783ba3762 +158000 00000000000000e50d56f13c7ce64183386abcac63462ca745b711be27568f52 +160000 000000000000066c6e629b2fb49c7fcc52b82fe9833f328e0c3943856facf231 +162000 00000000000001a83f5b20cd132f38f792fc02a17eb14d494c780ea9d1c82acc +164000 00000000000005a38f162cf308edea0a0a5d000bdb2073cba2386ebb1df7a2cf +166000 00000000000003b3402f35327d144a465f3768d6e6cb06cd8a2d8fc1328b2477 +168000 000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763 +170000 000000000000051f68f43e9d455e72d9c4e4ce52e8a00c5e24c07340632405cb +172000 0000000000000837e82c3a4ebe35a1d1d943e056234dba7c629922c6d4052d4c +174000 0000000000000504d3e701deb624eee4370f50c3d688fd1c27be5bbef07d76dd +176000 00000000000004659b5b8602b2132b62973994079a1c828df6ef8d6427e4686b +178000 00000000000009eae2697a7aaf57e730b707b9f4530449c16d924d534d41f297 +180000 00000000000004ff83b6c10460b239ef4a6aa320e5fffd6c7bcedefa8c78593c +182000 000000000000068dce12903c1447e4c5b60311b61e443a25d5fc82c77f4f9a8f +184000 000000000000060405a235c6b968ccb18fd6b3800ae9742c2524e28863367359 +186000 000000000000072ede9629fd1fd1af3cc2baa0e637f1959f34884be0e160dd1c +188000 000000000000004cf0c72d6dedfde88ca4c3dae129563210072ee68acded0ab1 +190000 0000000000000708bf3b261ffc963b6a768d915f9cfc9ec0a6c2a09969efad1a +192000 00000000000000af130d565291ba49208c546685c69b48a293aaf06387fc22ef +194000 000000000000046242d4984ecf2217e9afa113f2835bffbff118f2df4d80b216 +196000 00000000000006ae59396d4a289e83fe1b9967630752a5799f064620af7836a9 +198000 000000000000000f2ad431ff18ab1673d911395c8fa1f6801e054c5dcb54f8fb +200000 000000000000034a7dedef4a161fa058a2d67a173a90155f3a2fe6fc132e0ebf +202000 00000000000003282fe1d5533e4275fd9f51e6ba0352ec01f32914e9fbaeaf55 +204000 0000000000000423eb625dc140272ab97fea3ba6baf1dc56de77deabcc492872 +206000 0000000000000130b815d40fd6d8851438cd21ac9e428615ba03a1285ef1374c +208000 000000000000001db5a1515a5f8534c941b1628f60466e6b709b3b320254afff +210000 000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e +212000 00000000000003d906e4131c39f7655b72df40146d2967f5d75113a09610de61 +214000 00000000000003e6427f9fafa8b0e1af0859f15cea90d911f64445d296a2781a +216000 00000000000001f79a2db15d0ec6d951729e044749372caf504679bba5b1e65e +218000 0000000000000569070e338293af66258adba29dcdd5f33212314dff752ff458 +220000 000000000000002fdd2c741ed50bc3975a640ca419081711f30f553939641303 +222000 00000000000002c752a481ce0c45450ab046e640d38d6532178721e7700d8148 +224000 0000000000000107ee276d037218bf1780dbf6d4256bd7e05c66ca133bbc9ac5 +226000 000000000000012c614cf477c3b155d339f29d565c0258f9846c2f4dd402ff9b +228000 00000000000000efc4311c93fafbccedb6fdc682b566cba9519f1736b9788a67 +230000 000000000000012cfb19f5662707816e122ad60dd9b1cd646c6c9899be2c9667 +232000 000000000000018f47636e1c3a946db77624880ae484ffb0233f5aac6316b3bb +234000 00000000000000597f9263ea97bed4d3b10fbd55733a73bd1027f1a9b6c1451a +236000 00000000000000f2f5e55e89dde082cecc9b4a46a10bbb4197f5e35b16612db5 +238000 000000000000010014007d4b51ab60063684665401e448c6b0b1971a7398a442 +240000 000000000000000e7ad69c72afc00dc4e05fc15ae3061c47d3591d07c09f2928 +242000 00000000000000c95233d37a8c78dff10afecb14060347151b7eb7a04a2a5a3c +244000 000000000000006ded1526017d5b87ca22e1bd0da3921872cc99e9ec77ee5166 +246000 000000000000004c318a3ad2ebac28d140fada215b11f5b7d8e9151ff0b000af +248000 000000000000004d945017c14b75a3a58a2aa6772cacbfcaf907b3bee6d7f344 +250000 000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214 +252000 00000000000000200e99940b296ded4ce16462bba1950453b29abf313ba7cc47 +254000 000000000000004753be91559a2c74c6cb8a5d2be6db1df2ca0b2385697e53ff +256000 00000000000000252b217c0ce5c4d96b825b90dacbe8e4dcf5f6a8ba6749f3c2 +258000 00000000000000159f682a983465761f471dd24300746efe8db5642411f1b631 +260000 000000000000001fb91fbcebaaba0e2d926f04908d798a8b598c3bd962951080 +262000 000000000000001002ac67e026c523c3779b1ff2e3b9e2b7bfa6022ee1afae2a +264000 000000000000000d05c31485c532503939ca0b88d7e322dff79900ee6cdd5ed4 +266000 0000000000000009d755c65d58c7c1fde9167ba632a85574de3bb11c8a862e35 +268000 000000000000000048974ba0669938f7f8463650cd5c48c027aafd88c00a46af +270000 0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5 +272000 00000000000000050a89e2ffb28757d35e14615f23b981eca68906c8c71f65f2 +274000 0000000000000003fe2d3425e9f9b906f02f40b3db90d908ba0fbd1e44cf43f8 +276000 0000000000000004b8ed801f8a09ba8c1248a5b1dd1533a35124a80438573f59 +278000 0000000000000001bcadd1e4b4d01063a17347dfca126c63893d2aa37d82eb7f +280000 0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03 +282000 0000000000000002337ad25e6a9767420766309cfea79f13dd9c910bcf5ca063 +284000 0000000000000000eac86582f121e5431734e2ea36bf73347022c99c1adae37f +286000 00000000000000004388ae444347bde423f2f3aa6ef335b50909f5bc27d31ea3 +288000 00000000000000003c395f08779c3ac1301488b8a18c0999c008129a55610785 +290000 0000000000000000fa0b2badd05db0178623ebf8dd081fe7eb874c26e27d0b3b +292000 0000000000000000620671231acb6a68134a0396235dcb0e53f4fc82bbaa1184 +294000 0000000000000000cb2540b3f00ce422887904c75b24bf75b8a73817302a4138 +296000 00000000000000009570102278e59ecf045c16ec8c8a5ea85bf823d0ec72e3d0 +298000 000000000000000047d2f2eb7278e3f4aded9acaf502f5ec27bab5018b5871f2 +300000 000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254 +302000 0000000000000000072268c9bb18603566ed5012378c29bb4d37e34cead7448d +304000 00000000000000003558a1ceec3f5338c0e887b4171410195a7fa0a81bcaa628 +306000 00000000000000002bb3265a8bf67ec2aa436c297ac7e56fcedd4dbaecccacc0 +308000 00000000000000001d55aa114bddd81938d09e2dccd432dec59a4078ca0bc0f4 +310000 0000000000000000125a28cc9e9209ddb75718f599a8039f6c9e7d9f1fb021e0 +312000 00000000000000002bd1fa27964e31fe9861b40940e7ece2cfa359765b219a49 +314000 000000000000000008ae6cb20997f3c4aacc50ee2f0d08a0c3691907fe7357a3 +316000 00000000000000000d77a89ab1069e47d1213ae509de95ee0d9ab095a725f7d7 +318000 00000000000000002583a647dc5f084a312e12bb90a70c3fe1eb1e4d419f35b6 +320000 000000000000000015aab005b28a326ade60f07515c33517ea5cb598f28fb7ea +322000 0000000000000000177da809382f93ca1c4336811e4a910050689d317d62264e +324000 00000000000000000b9880c40075d763b2a5f04fc01444a6278c5d2d442cda0b +326000 00000000000000001e95e7216072cd53353b964054b592f7ce84d3743aab125a +328000 000000000000000009cb9a303d105e7b96b36546a3196f6f79ece4b43712cbb2 +330000 00000000000000000faabab19f17c0178c754dbed023e6c871dcaf74159c5f02 +332000 00000000000000001799255bc0c35f91f7d4fddfbf7e84dedf94fc59cde9b7f2 +334000 000000000000000008d1ff7b7673837e9d7e1324dc7ab8498405ea583f43f53d +336000 0000000000000000125e3e6f327edcd7163b486efc16e79b8a996270127b54df +338000 00000000000000001983dc4a87df627b63cdce28e5321cb867fbbb74c0e87e8e +340000 00000000000000000d9b2508615d569e18f00c034d71474fc44a43af8d4a5003 +342000 0000000000000000007220892af98a563a1c891c756e94be3f14edddcf637c3e +344000 000000000000000005f0a16f5a9f95eeb95c5eed0eb221e8f5dc5a9943a03aee +346000 0000000000000000068d33fd865621cb7eedbf05c6b235191fa1cb8ee2c797d7 +348000 00000000000000001598a651988bb3a45237c4f801cf8049be20f74aed8e827a +350000 0000000000000000053cf64f0400bb38e0c4b3872c38795ddde27acb40a112bb +352000 00000000000000001635a4b5f27d2ec458f7bca550d71f490b93e98e7a07cbca +354000 00000000000000000cf8af9be2709e9d7adf2c33b3789aeeff517987f4be22e8 +356000 0000000000000000138ce5493b612b0e90b66e2a76714088d6b3e6a4770215af +358000 0000000000000000073aceefab8c381c3c4edb4f87a6d5d2ae32184278218429 +360000 00000000000000000ca6e07cf681390ff888b7f96790286a440da0f2b87c8ea6 +362000 00000000000000000bfcad6c331dd152cfc713e9e0790978a10e0bfda3e030d5 +364000 00000000000000000e20bcf213a0bbd6be88d5fede6b060c737f7f8b7f1df504 +366000 0000000000000000138e108e780fdb71eb4cad533b46445ab6befbf9687f561f +368000 00000000000000000d39970aac12754eb89c2dcfda539b65562e5c3fec102c24 +370000 000000000000000002cad3026f68357229dd6eaa6bcef6fe5166e1e53b039b8c +372000 0000000000000000028093cc8035a6bc4e0d1b40932c2f8b50312a3fc86bf3da +374000 00000000000000001016aa3783721673bebbcd1efa49946b52cceb09a81465a6 +376000 0000000000000000106e9e99cf4fce4e8a4abc97f3e883956e26d76b3a1133ce +378000 00000000000000000516cd5b5f4b7e528d6e61c643595cc818f1d02f53da4281 +380000 00000000000000000b06cee3cee10d2617e2024a996f5c613f7d786b15a571ff +382000 000000000000000003cf98590769bde40ffcd6800733ab47dd406d8203e65a89 +384000 000000000000000005dc7ea53e2f6eeb09798cc9d2214f09d249661c36c288b3 +386000 00000000000000000d94c8c0b0ddec874d2a597e988154733d9ea614292c08bb diff --git a/confs/BTC_peers.txt b/confs/BTC_peers.txt new file mode 100644 index 000000000..abb52b839 --- /dev/null +++ b/confs/BTC_peers.txt @@ -0,0 +1,137 @@ +108.58.252.82 +74.207.233.193 +130.211.146.81 +71.193.19.234 +173.66.1.180 +104.158.113.201 +108.207.245.69 +107.4.134.66 +96.231.100.124 +209.6.208.31 +69.141.89.74 +82.20.129.167 +5.9.222.226 +149.210.234.41 +168.235.85.242 +52.91.247.30 +191.237.64.28 +173.236.101.34 +73.189.2.240 +106.186.113.184 +173.64.13.6 +73.166.27.56 +70.106.255.189 +168.62.188.213 +71.234.225.255 +24.41.10.204 +72.175.146.90 +184.107.155.82 +162.220.47.150 +12.23.127.150 +169.228.66.43 +192.227.137.5 +71.205.232.181 +207.182.151.130 +46.4.22.45 +198.50.238.171 +174.59.182.120 +66.172.10.4 +75.73.82.209 +91.121.108.61 +24.6.74.4 +37.187.78.27 +96.32.46.235 +107.170.13.184 +148.251.151.48 +178.62.70.16 +144.76.185.151 +144.76.92.199 +91.121.210.159 +76.105.242.7 +54.84.231.113 +89.187.134.220 +64.15.77.36 +212.51.147.153 +216.15.33.203 +74.100.90.30 +185.18.6.3 +104.131.65.197 +89.248.174.54 +128.8.124.7 +54.232.245.146 +67.205.101.120 +72.207.119.149 +192.95.27.144 +69.61.93.240 +209.91.190.202 +50.35.82.152 +66.175.220.212 +23.239.22.219 +73.229.104.201 +207.244.73.8 +78.129.251.170 +139.162.211.181 +172.245.5.156 +98.144.123.251 +177.238.90.180 +198.71.92.236 +73.254.38.48 +75.189.201.141 +52.24.104.64 +71.231.209.66 +87.224.35.189 +73.162.143.196 +82.204.103.94 +91.148.210.17 +94.242.229.158 +188.121.252.243 +70.39.8.97 +95.97.112.190 +109.228.152.9 +91.209.77.101 +217.76.121.251 +79.136.29.43 +178.212.136.92 +178.255.41.123 +84.212.200.24 +91.145.49.56 +162.220.246.101 +95.167.109.125 +153.163.32.61 +78.67.29.111 +92.247.229.163 +210.195.201.103 +95.84.162.95 +106.38.234.67 +186.88.0.18 +121.208.106.80 +120.55.193.136 +124.171.128.201 +54.94.163.92 +98.217.125.225 +104.156.97.121 +162.255.117.230 +129.13.252.36 +79.120.12.63 +108.5.176.30 +69.144.244.229 +76.22.18.34 +115.29.186.22 +78.46.193.75 +213.91.211.17 +164.177.179.162 +134.249.141.40 +69.140.88.12 +178.212.136.108 +109.120.250.3 +154.127.61.55 +123.120.167.101 +73.210.74.120 +82.136.95.220 +124.122.212.150 +81.191.80.160 +96.58.196.51 +77.23.111.25 +14.175.245.51 +76.164.234.12 +92.156.214.192 diff --git a/crypto777/Makefile b/crypto777/Makefile new file mode 100644 index 000000000..fe1aa2da0 --- /dev/null +++ b/crypto777/Makefile @@ -0,0 +1,40 @@ +# Copyright (c) 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# GNU Makefile based on shared rules provided by the Native Client SDK. +# See README.Makefiles for more details. + +VALID_TOOLCHAINS := pnacl newlib glibc clang-newlib mac + +NACL_SDK_ROOT ?= $(abspath $(CURDIR)) + +TARGET = crypto777 + +EXTRA= -D__PNACL + +include $(NACL_SDK_ROOT)/tools/common.mk + +CHROME_ARGS += --allow-nacl-socket-api=127.0.0.1 + +DEPS = nacl_io +LIBS = + +CFLAGS = -Wall -D__PNACL -fno-strict-aliasing $(EXTRA) +LFLAGS = libs + +SOURCES = cJSON.c ramcoder.c iguana_OS.c OS_portable.c OS_time.c OS_nonportable.c hmac_sha512.c SaM.c bitcoind_RPC.c inet.c iguana_utils.c curve25519.c curve25519-donna.c jpeg/jaricom.c jpeg/jcapimin.c jpeg/jcapistd.c jpeg/jcarith.c jpeg/jccoefct.c jpeg/jccolor.c \ + jpeg/jcdctmgr.c jpeg/jchuff.c jpeg/jcinit.c jpeg/jcmainct.c jpeg/jcmarker.c jpeg/jcmaster.c \ + jpeg/jcomapi.c jpeg/jcparam.c jpeg/jcprepct.c jpeg/jcsample.c jpeg/jctrans.c jpeg/jdapimin.c \ + jpeg/jdapistd.c jpeg/jdarith.c jpeg/jdatadst.c jpeg/jdatasrc.c jpeg/jdcoefct.c jpeg/jdcolor.c \ + jpeg/jddctmgr.c jpeg/jdhuff.c jpeg/jdinput.c jpeg/jdmainct.c jpeg/jdmarker.c jpeg/jdmaster.c \ + jpeg/jdmerge.c jpeg/jdpostct.c jpeg/jdsample.c jpeg/jdtrans.c jpeg/jerror.c jpeg/jfdctflt.c \ + jpeg/jfdctfst.c jpeg/jfdctint.c jpeg/jidctflt.c jpeg/jidctfst.c jpeg/jidctint.c jpeg/jquant1.c \ + jpeg/jquant2.c jpeg/jutils.c jpeg/jmemmgr.c jpeg/jmemnobs.c + +# Build rules generated by macros from common.mk: + +$(foreach dep,$(DEPS),$(eval $(call DEPEND_RULE,$(dep)))) +$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) + +$(eval $(call LIB_RULE,$(TARGET),$(SOURCES))) diff --git a/crypto777/OS_nonportable.c b/crypto777/OS_nonportable.c new file mode 100755 index 000000000..18697d7c2 --- /dev/null +++ b/crypto777/OS_nonportable.c @@ -0,0 +1,133 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#include "OS_portable.h" + +#ifdef __PNACL +int32_t OS_nonportable_syncmap(struct OS_mappedptr *mp,long len) +{ + printf("no way to sync mapped mem in pnacl\n"); + return(-1); +} + +void *OS_nonportable_tmpalloc(char *dirname,char *name,struct OS_memspace *mem,long origsize) +{ + printf("no way to do tmpallocs in pnacl\n"); + return(0); +} + +#elif _WIN32 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* _O_BINARY */ +#include +#include +#include +#include +#include +#include + +char *OS_nonportable_path(char *str) +{ + int32_t i; + for (i=0; str[i]!=0; i++) + if ( str[i] == '/' ) + str[i] = '\\'; + return(str); +} + +void *OS_nonportable_mapfile(char *fname,uint64_t *filesizep,int32_t enablewrite) +{ + int32_t fd,rwflags,flags = MAP_FILE|MAP_SHARED; + uint64_t filesize; + void *ptr = 0; + *filesizep = 0; + if ( enablewrite != 0 ) + fd = _sopen(fname, _O_RDWR | _O_BINARY, _SH_DENYNO); + else fd = _sopen(fname, _O_RDONLY | _O_BINARY, _SH_DENYNO); + if ( fd < 0 ) + { + //printf("map_file: error opening enablewrite.%d %s\n",enablewrite,fname); + return(0); + } + if ( *filesizep == 0 ) + filesize = (uint64_t)lseek(fd,0,SEEK_END); + else filesize = *filesizep; + rwflags = PROT_READ; + if ( enablewrite != 0 ) + rwflags |= PROT_WRITE; + ptr = mmap(0,filesize,rwflags,flags,fd,0); + _close(fd); + if ( ptr == 0 || ptr == MAP_FAILED ) + { + printf("map_file.write%d: mapping %s failed? mp %p\n",enablewrite,fname,ptr); + return(0); + } + *filesizep = filesize; + return(ptr); +} + +int32_t OS_nonportable_renamefile(char *fname,char *newfname) +{ + char cmdstr[1024],tmp[512]; + strcpt(tmp,fname); + OS_nonportable_path(tmp); + sprintf(cmdstr,"del %s",tmp); + if ( system() != 0 ) + printf("error deleting file.(%s)\n",cmdstr); + else return(1); +} + +int32_t OS_nonportable_launch(char *args[]) +{ + int32_t pid; + pid = _spawnl( _P_NOWAIT, args[0], args[0], NULL, NULL ); + return pid; +} + +void OS_nonportable_randombytes(unsigned char *x,long xlen) +{ + HCRYPTPROV prov = 0; + CryptAcquireContextW(&prov, NULL, NULL,PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT); + CryptGenRandom(prov, xlen, x); + CryptReleaseContext(prov, 0); +} + +int32_t OS_nonportable_init() +{ + // Initialize Windows Sockets + WSADATA wsadata; + int ret = WSAStartup(MAKEWORD(2,2), &wsadata); + if (ret != NO_ERROR) + { + printf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)\n", ret); + //printf("%s\n", strError.c_str()); + return -1; + } + return(0); +} + +#endif + diff --git a/crypto777/OS_portable.c b/crypto777/OS_portable.c new file mode 100755 index 000000000..fe1b63fe0 --- /dev/null +++ b/crypto777/OS_portable.c @@ -0,0 +1,207 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "OS_portable.h" +#include +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif + +void OS_portable_init() +{ +#ifdef _WIN32 + OS_nonportable_init(); +#endif +} + +// from tweetnacl +void OS_portable_randombytes(unsigned char *x,long xlen) +{ +#ifdef _WIN32 + return(OS_nonportable_randombytes(x,xlen)); +#else + static int fd = -1; + int32_t i; + if (fd == -1) { + for (;;) { + fd = open("/dev/urandom",O_RDONLY); + if (fd != -1) break; + sleep(1); + } + } + while (xlen > 0) { + if (xlen < 1048576) i = (int32_t)xlen; else i = 1048576; + i = (int32_t)read(fd,x,i); + if (i < 1) { + sleep(1); + continue; + } + if ( 0 ) + { + int32_t j; + for (j=0; j %p\n",x); + } + x += i; + xlen -= i; + } +#endif +} + +int32_t OS_portable_truncate(char *fname,long filesize) +{ + return(truncate(fname,filesize)); +} + +char *OS_portable_path(char *str) +{ +#ifdef _WIN32 + return(OS_nonportable_path(str)); +#else + return(str); +#endif +} + +int32_t OS_portable_renamefile(char *fname,char *newfname) +{ +#ifdef _WIN32 + char cmdstr[1024],tmp[512]; + strcpt(tmp,fname); + iguana_compatible_path(tmp); + sprintf(cmdstr,"del %s",tmp); + if ( system() != 0 ) + printf("error deleting file.(%s)\n",cmdstr); + else return(1); +#else + return(rename(fname,newfname)); +#endif +} + +int32_t OS_portable_removefile(char *fname) +{ +#ifdef _WIN32 + char cmdstr[1024],tmp[512]; + strcpt(tmp,fname); + iguana_compatible_path(tmp); + sprintf(cmdstr,"del %s",tmp); + if ( system() != 0 ) + printf("error deleting file.(%s)\n",cmdstr); + else return(1); +#else + return(remove(fname)); +#endif +} + +void *OS_portable_mapfile(char *fname,long *filesizep,int32_t enablewrite) +{ +#ifdef _WIN32 + return(OS_nonportable_mapfile(fname,filesizep,enablewrite); +#else + int32_t fd,rwflags,flags = MAP_FILE|MAP_SHARED; + uint64_t filesize; + void *ptr = 0; + *filesizep = 0; + if ( enablewrite != 0 ) + fd = open(fname,O_RDWR); + else fd = open(fname,O_RDONLY); + if ( fd < 0 ) + { + //printf("map_file: error opening enablewrite.%d %s\n",enablewrite,fname); + return(0); + } + if ( *filesizep == 0 ) + filesize = (uint64_t)lseek(fd,0,SEEK_END); + else filesize = *filesizep; + rwflags = PROT_READ; + if ( enablewrite != 0 ) + rwflags |= PROT_WRITE; +//#if __i386__ || _WIN32 || __PNACL + ptr = mmap(0,filesize,rwflags,flags,fd,0); +//#else +// void *mmap64(void *addr,size_t len,int32_t prot,int32_t flags,int32_t fildes,off_t off); +// ptr = mmap64(0,filesize,rwflags,flags,fd,0); +//#endif + close(fd); + if ( ptr == 0 || ptr == MAP_FAILED ) + { + printf("map_file.write%d: mapping %s failed? mp %p\n",enablewrite,fname,ptr); + return(0); + } + *filesizep = filesize; + //printf("mapped %ld -> %p\n",(long)filesize,ptr); + return(ptr); +#endif +} + +int32_t OS_portable_syncmap(struct OS_mappedptr *mp,long len) +{ +#ifndef __PNACL + int32_t err = -1; + if ( mp->actually_allocated != 0 ) + return(0); + if ( mp->fileptr != 0 && mp->dirty != 0 ) + { + if ( len == 0 ) + len = mp->allocsize; + err = msync(mp->fileptr,len,MS_SYNC); + if ( err != 0 ) + printf("sync (%s) len %llu, err %d errno.%d\n",mp->fname,(long long)len,err,errno); + //Sync_total += len; + mp->dirty = 0; + } + return(err); +#else + return(OS_nonportable_syncmap(mp,len)); +#endif +} + +void *OS_portable_tmpalloc(char *dirname,char *name,struct OS_memspace *mem,long origsize) +{ +#ifdef __PNACL + return(OS_nonportable_tmpalloc(dirname,name,mem,origsize)); +#else + char fname[1024]; void *ptr; long size; + if ( mem->threadsafe != 0 ) + portable_mutex_lock(&mem->mutex); + if ( origsize != 0 && (mem->M.fileptr == 0 || (mem->used + origsize) > mem->totalsize) ) + { + //coin->TMPallocated += origsize; + memset(&mem->M,0,sizeof(mem->M)); + sprintf(fname,"tmp/%s/%s.%d",dirname,name,mem->counter), OS_compatible_path(fname); + mem->counter++; + if ( mem->totalsize == 0 ) + { + mem->totalsize = (1024 * 1024 * 16); + } + //if ( coin->R.RSPACE.size == 0 ) + // coin->R.RSPACE.size = mem->size; + if ( mem->totalsize > origsize ) + size = mem->totalsize; + else size = origsize; + fprintf(stderr,"filealloc.(%s) -> ",fname); + if ( OS_filealloc(&mem->M,fname,mem,size) == 0 ) + { + printf("couldnt map tmpfile %s\n",fname); + return(0); + } + fprintf(stderr,"created\n"); + } + ptr = iguana_memalloc(mem,origsize,1); + if ( mem->threadsafe != 0 ) + portable_mutex_unlock(&mem->mutex); + return(ptr); +#endif +} diff --git a/crypto777/OS_portable.h b/crypto777/OS_portable.h new file mode 100755 index 000000000..a768e8588 --- /dev/null +++ b/crypto777/OS_portable.h @@ -0,0 +1,259 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ +#ifndef OS_PORTABLEH +#define OS_PORTABLEH + +// iguana_OS has functions that invoke system calls. Whenever possible stdio and similar functions are use and most functions are fully portable and in this file. For things that require OS specific, the call is routed to iguana_OS_portable_* Usually, all but one OS can be handled with the same code, so iguana_OS_portable.c has most of this shared logic and an #ifdef iguana_OS_nonportable.c + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../includes/libgfshare.h" +#include "../includes/utlist.h" +#include "../includes/uthash.h" + +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif + +#define SATOSHIDEN ((uint64_t)100000000L) +#define dstr(x) ((double)(x) / SATOSHIDEN) + +#define SMALLVAL 0.000000000000001 + +#define SETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] |= (1 << ((bitoffset) & 7))) +#define GETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] & (1 << ((bitoffset) & 7))) +#define CLEARBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] &= ~(1 << ((bitoffset) & 7))) + +#define portable_mutex_t pthread_mutex_t +#define portable_mutex_init(ptr) pthread_mutex_init(ptr,NULL) +#define portable_mutex_lock pthread_mutex_lock +#define portable_mutex_unlock pthread_mutex_unlock +#define OS_thread_create pthread_create + +struct allocitem { uint32_t allocsize,type; } __attribute__((packed)); +struct queueitem { struct queueitem *next,*prev; uint32_t allocsize,type; } __attribute__((packed)); +typedef struct queue +{ + struct queueitem *list; + portable_mutex_t mutex; + char name[64],initflag; +} queue_t; + +struct OS_mappedptr +{ + char fname[512]; + void *fileptr,*pending; + long allocsize,changedsize; + int32_t rwflag,dirty,actually_allocated; + uint32_t closetime,opentime; +}; + +struct OS_memspace +{ + portable_mutex_t mutex; long used,totalsize; struct OS_mappedptr M; char name[64]; void *ptr; + int32_t alignflag,counter,maxheight,openfiles,lastcounter,threadsafe,allocated:1,mapped:1,external:1; +#ifdef IGUANA_PEERALLOC + int32_t outofptrs,numptrs,availptrs; + void *ptrs[4096]; int32_t allocsizes[4096],maxsizes[4096]; +#endif +}; + +struct tai { uint64_t x; double millis; }; +struct taidate { int32_t year,month,day; }; +struct taitime { struct taidate date; int32_t hour,minute,second; uint32_t offset; double millis; }; +int32_t leapsecs_sub(struct tai *); + +struct tai tai_now(); +uint32_t tai2utc(struct tai t); +struct taidate taidate_frommjd(int32_t day,int32_t *pwday,int32_t *pyday); +struct taitime tai2time(struct tai t,int32_t *pwday,int32_t *pyday); +struct taidate tai2date(struct tai t); +int32_t taidate_str(char *s,struct taidate cd); +char *taitime_str(char *s,struct taitime ct); +int32_t taidate_mjd(struct taidate cd); +uint32_t tai2utime(struct tai t); +struct tai taitime2tai(struct taitime ct); +char *tai_str(char *str,struct tai t); +char *utc_str(char *str,struct tai t); + +int32_t msync(void *addr,size_t len,int32_t flags); + +#ifdef __PNACL +int32_t OS_nonportable_syncmap(struct OS_mappedptr *mp,long len); +void *OS_nonportable_tmpalloc(char *dirname,char *name,struct OS_memspace *mem,long origsize); + +#elif _WIN32 +char *OS_portable_path(char *str); +int32_t OS_nonportable_renamefile(char *fname,char *newfname); +int32_t OS_nonportable_launch(char *args[]); +void OS_nonportable_randombytes(unsigned char *x,long xlen); +int32_t OS_nonportable_init(); +#endif + +void OS_portable_init(); +void OS_init(); + +double OS_portable_milliseconds(); +void OS_portable_randombytes(unsigned char *x,long xlen); +int32_t OS_portable_truncate(char *fname,long filesize); +char *OS_portable_path(char *str); +int32_t OS_portable_renamefile(char *fname,char *newfname); +int32_t OS_portable_removefile(char *fname); +void *OS_portable_mapfile(char *fname,long *filesizep,int32_t enablewrite); +int32_t OS_portable_syncmap(struct OS_mappedptr *mp,long len); +void *OS_portable_tmpalloc(char *dirname,char *name,struct OS_memspace *mem,long origsize); + + +int32_t is_DST(int32_t datenum); +int32_t extract_datenum(int32_t *yearp,int32_t *monthp,int32_t *dayp,int32_t datenum); +int32_t expand_datenum(char *date,int32_t datenum); +int32_t calc_datenum(int32_t year,int32_t month,int32_t day); +int32_t ecb_decrdate(int32_t *yearp,int32_t *monthp,int32_t *dayp,char *date,int32_t datenum); +int32_t conv_date(int32_t *secondsp,char *buf); +uint32_t OS_conv_datenum(int32_t datenum,int32_t hour,int32_t minute,int32_t second); +int32_t OS_conv_unixtime(int32_t *secondsp,time_t timestamp); +double OS_milliseconds(); + +void OS_randombytes(unsigned char *x,long xlen); + +int32_t OS_truncate(char *fname,long filesize); +char *OS_compatible_path(char *str); +int32_t OS_renamefile(char *fname,char *newfname); +int32_t OS_removefile(char *fname,int32_t scrubflag); +void OS_ensure_directory(char *dirname); +uint64_t OS_filesize(char *fname); +int32_t OS_compare_files(char *fname,char *fname2); +int64_t OS_copyfile(char *src,char *dest,int32_t cmpflag); +int32_t OS_releasemap(void *ptr,uint64_t filesize); +void _OS_closemap(struct OS_mappedptr *mp); +void OS_closemap(struct OS_mappedptr *mp); +long OS_ensurefilesize(char *fname,long filesize,int32_t truncateflag); +int32_t OS_openmap(struct OS_mappedptr *mp); +void *OS_mappedptr(void **ptrp,struct OS_mappedptr *mp,uint64_t allocsize,int32_t rwflag,char *fname); +void *OS_filealloc(struct OS_mappedptr *M,char *fname,struct OS_memspace *mem,long size); +void *OS_mapfile(char *fname,long *filesizep,int32_t enablewrite); +void *OS_loadfile(char *fname,char **bufp,int64_t *lenp,int64_t *allocsizep); +void *OS_filestr(int64_t *allocsizep,char *fname); + +int32_t OS_syncmap(struct OS_mappedptr *mp,long len); +void *OS_tmpalloc(char *dirname,char *name,struct OS_memspace *mem,long origsize); + +long myallocated(uint8_t type,long change); +void *mycalloc(uint8_t type,int32_t n,long itemsize); +void myfree(void *_ptr,long allocsize); +void free_queueitem(void *itemdata); +void *myrealloc(uint8_t type,void *oldptr,long oldsize,long newsize); +void *myaligned_alloc(uint64_t allocsize); +int32_t myaligned_free(void *ptr,long size); + +void *queueitem(char *str); +void queue_enqueue(char *name,queue_t *queue,struct queueitem *origitem,int32_t offsetflag); +void *queue_dequeue(queue_t *queue,int32_t offsetflag); +void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize,int32_t freeitem); +void *queue_free(queue_t *queue); +void *queue_clone(queue_t *clone,queue_t *queue,int32_t size); +int32_t queue_size(queue_t *queue); + +void iguana_memreset(struct OS_memspace *mem); +void iguana_mempurge(struct OS_memspace *mem); +void *iguana_meminit(struct OS_memspace *mem,char *name,void *ptr,int64_t totalsize,int32_t threadsafe); +void *iguana_memalloc(struct OS_memspace *mem,long size,int32_t clearflag); +int64_t iguana_memfree(struct OS_memspace *mem,void *ptr,int32_t size); + + +// generic functions +int32_t unhex(char c); +void touppercase(char *str); +uint32_t is_ipaddr(char *str); +void iguana_bitmap(char *space,int32_t max,char *name); +double _pairaved(double valA,double valB); +int32_t unstringbits(char *buf,uint64_t bits); +uint64_t stringbits(char *str); +int32_t is_decimalstr(char *str); +void tolowercase(char *str); +char *clonestr(char *str); +long _stripwhite(char *buf,int accept); +int32_t is_DST(int32_t datenum); +int32_t extract_datenum(int32_t *yearp,int32_t *monthp,int32_t *dayp,int32_t datenum); +int32_t expand_datenum(char *date,int32_t datenum); +int32_t calc_datenum(int32_t year,int32_t month,int32_t day); +int32_t ecb_decrdate(int32_t *yearp,int32_t *monthp,int32_t *dayp,char *date,int32_t datenum); +int32_t conv_date(int32_t *secondsp,char *buf); +uint32_t OS_conv_datenum(int32_t datenum,int32_t hour,int32_t minute,int32_t second); +int32_t OS_conv_unixtime(int32_t *secondsp,time_t timestamp); +int32_t btc_coinaddr(char *coinaddr,uint8_t addrtype,char *pubkeystr); +void reverse_hexstr(char *str); +int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); + +uint64_t RS_decode(char *rs); +int32_t RS_encode(char *rsaddr,uint64_t id); + +void calc_sha1(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_md2(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_md4(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_md5str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_sha224(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_sha384(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_sha512(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_sha224(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_rmd160(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_rmd128(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_rmd256(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_rmd320(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_tiger(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_whirlpool(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); + +char *hmac_sha1_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_md2_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_md4_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_md5_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_sha224_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_sha256_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_sha384_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_sha512_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_rmd128_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_rmd160_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_rmd256_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_rmd320_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_tiger_str(char *dest,char *key,int32_t key_size,char *message); +char *hmac_whirlpool_str(char *dest,char *key,int32_t key_size,char *message); +int nn_base64_encode(const uint8_t *in,size_t in_len,char *out,size_t out_len); +int nn_base64_decode(const char *in,size_t in_len,uint8_t *out,size_t out_len); + +void sha256_sha256(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void rmd160ofsha256(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_md5str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_crc32str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_NXTaddr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_curve25519_str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_base64_encodestr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); +void calc_base64_decodestr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); + +#endif + diff --git a/crypto777/OS_time.c b/crypto777/OS_time.c new file mode 100755 index 000000000..208321f4e --- /dev/null +++ b/crypto777/OS_time.c @@ -0,0 +1,529 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +// DJB's libtai was modified for the tai code + +#include "OS_portable.h" + +#define TAI_PACK 8 +//#define UTC_ADJUST -36 +#define tai_approx(t) ((double) ((t)->x)) +#define tai_less(t,u) ((t)->x < (u)->x) + +int32_t leapsecs_sub(struct tai *); +static struct tai First_TAI; +uint32_t First_utc; +int32_t UTC_ADJUST; + +struct taidate taidate_frommjd(int32_t day,int32_t *pwday,int32_t *pyday) +{ + int32_t year,month,yday; struct taidate cd; + year = day / 146097L; + day %= 146097L; + day += 678881L; + while (day >= 146097L) { day -= 146097L; ++year; } + // year * 146097 + day - 678881 is MJD; 0 <= day < 146097 + // 2000-03-01, MJD 51604, is year 5, day 0 + if ( pwday != 0 ) + *pwday = (day + 3) % 7; + year *= 4; + if (day == 146096L) { year += 3; day = 36524L; } + else { year += day / 36524L; day %= 36524L; } + year *= 25; + year += day / 1461; + day %= 1461; + year *= 4; + yday = (day < 306); + if (day == 1460) { year += 3; day = 365; } + else { year += day / 365; day %= 365; } + yday += day; + day *= 10; + month = (day + 5) / 306; + day = (day + 5) % 306; + day /= 10; + if (month >= 10) { yday -= 306; ++year; month -= 10; } + else { yday += 59; month += 2; } + cd.year = year; + cd.month = month + 1; + cd.day = day + 1; + if ( pyday != 0 ) + *pyday = yday; + return(cd); +} + +struct taitime tai2time(struct tai t,int32_t *pwday,int32_t *pyday) +{ + uint64_t u,tmp; int32_t leap,s; double diff; struct taitime ct; + leap = leapsecs_sub(&t); + u = t.x; + u += (58486 + 60); // was off by a minute + s = u % 86400ULL; + memset(&ct,0,sizeof(ct)); + ct.second = (s % 60) + leap; s /= 60; + ct.minute = s % 60; s /= 60; + ct.hour = s; + u /= 86400ULL; + ct.date = taidate_frommjd((int32_t)(u - 53375995543064ULL),pwday,pyday); + ct.offset = 0; + if ( First_TAI.x != 0 && t.x > First_TAI.x ) + { + tmp = (t.x - First_TAI.x); + diff = (t.millis - First_TAI.millis); + if ( diff < tmp*1000 ) + tmp = 0, printf("TAI diff %f vs tmp.%lld\n",diff,(long long)tmp); + else tmp = diff * 1000000000.; + //printf("tmp.%llu \n",(long long)tmp); + tmp %= (uint64_t)1000000000000; + ct.millis = ((double)tmp / 1000000000.); + } + //printf("TAI millis: %lld -1st.%lld %f - %f -> %f | %f\n",(long long)t.x,(long long)First_TAI.x,t.millis,First_TAI.millis,t.millis-First_TAI.millis,ct.millis); + return(ct); +} + +struct taidate tai2date(struct tai t) +{ + struct taitime ct = tai2time(t,0,0); + return(ct.date); +} + +/*int32_t taitime_scan(char *s,struct taitime *ct) + { + int32_t z,c,sign; char *t = s; + t += taidate_scan(t,&ct->date); + while ((*t == ' ') || (*t == '\t') || (*t == 'T')) ++t; + z = 0; while ((c = (uint8_t) (*t - '0')) <= 9) { z = z * 10 + c; ++t; } + ct->hour = z; + if (*t++ != ':') return 0; + z = 0; while ((c = (uint8_t) (*t - '0')) <= 9) { z = z * 10 + c; ++t; } + ct->minute = z; + if (*t != ':') + ct->second = 0; + else + { + ++t; + z = 0; while ((c = (uint8_t) (*t - '0')) <= 9) { z = z * 10 + c; ++t; } + ct->second = z; + } + while ((*t == ' ') || (*t == '\t')) ++t; + if (*t == '+') sign = 1; else if (*t == '-') sign = -1; else return 0; + ++t; + c = (uint8_t) (*t++ - '0'); if (c > 9) return 0; z = c; + c = (uint8_t) (*t++ - '0'); if (c > 9) return 0; z = z * 10 + c; + c = (uint8_t) (*t++ - '0'); if (c > 9) return 0; z = z * 6 + c; + c = (uint8_t) (*t++ - '0'); if (c > 9) return 0; z = z * 10 + c; + ct->offset = z * sign; + printf("t.%p s.%p\n",t,s); + return((int32_t)((long)t - (long)s)); + }*/ + +int32_t taidate_str(char *s,struct taidate cd) +{ + int32_t x,len,i = 0; + x = cd.year; if (x < 0) x = -x; do { ++i; x /= 10; } while(x); + len = (cd.year < 0) + i + 6; + if ( s != 0 ) + { + x = cd.year; + if (x < 0) { x = -x; *s++ = '-'; } + s += i; do { *--s = '0' + (x % 10); x /= 10; } while(x); s += i; + x = cd.month; + s[0] = '-'; s[2] = '0' + (x % 10); x /= 10; s[1] = '0' + (x % 10); + x = cd.day; + s[3] = '-'; s[5] = '0' + (x % 10); x /= 10; s[4] = '0' + (x % 10); + s[len] = 0; + } + return(len); +} + +char *taitime_str(char *s,struct taitime ct) +{ + int32_t result,x,len; + result = taidate_str(s,ct.date); + len = result + 15; + if ( s != 0 ) + { + s += result; + x = ct.hour; + s[0] = ' '; + s[2] = '0' + (x % 10); x /= 10; + s[1] = '0' + (x % 10); + s += 3; + x = ct.minute; + s[0] = ':'; + s[2] = '0' + (x % 10); x /= 10; + s[1] = '0' + (x % 10); + s += 3; + x = ct.second; + s[0] = ':'; + s[2] = '0' + (x % 10); x /= 10; + s[1] = '0' + (x % 10); + s += 3; + s[0] = ' '; + x = ct.offset; + if (x < 0) { s[1] = '-'; x = -x; } else s[1] = '+'; + s[5] = '0' + (x % 10); x /= 10; + s[4] = '0' + (x % 6); x /= 6; + s[3] = '0' + (x % 10); x /= 10; + s[2] = '0' + (x % 10); + s[len] = 0; + } + return(s); +} + +void tai_pack(char *s,struct tai *t) +{ + uint64_t x; + x = t->x; + s[7] = x & 255; x >>= 8; + s[6] = x & 255; x >>= 8; + s[5] = x & 255; x >>= 8; + s[4] = x & 255; x >>= 8; + s[3] = x & 255; x >>= 8; + s[2] = x & 255; x >>= 8; + s[1] = x & 255; x >>= 8; + s[0] = x; +} + +void tai_unpack(char *s,struct tai *t) +{ + uint64_t x; + x = (uint8_t) s[0]; + x <<= 8; x += (uint8_t) s[1]; + x <<= 8; x += (uint8_t) s[2]; + x <<= 8; x += (uint8_t) s[3]; + x <<= 8; x += (uint8_t) s[4]; + x <<= 8; x += (uint8_t) s[5]; + x <<= 8; x += (uint8_t) s[6]; + x <<= 8; x += (uint8_t) s[7]; + t->x = x; +} + +void tai_add(struct tai *t,struct tai *u,struct tai *v) { t->x = u->x + v->x; } + +void tai_sub(struct tai *t,struct tai *u,struct tai *v) { t->x = u->x - v->x; } + +// {"leapseconds":["+1972-06-30", "+1972-12-31", "+1973-12-31", "+1974-12-31", "+1975-12-31", "+1976-12-31", "+1977-12-31", "+1982-06-30", "+1983-06-30", "+1985-06-30", "+1987-12-31", "+1989-12-31", "+1990-12-31", "+1992-06-30", "+1993-06-30", "+1994-06-30", "+1995-12-31", "+1997-06-30", "+1998-12-31", "+2005-12-31", "+2008-12-31", "+2012-06-30", "+2015-06-30"]} +char *leapseconds[] = { "+1972-06-30", "+1972-12-31", "+1973-12-31", "+1974-12-31", "+1975-12-31", "+1976-12-31", "+1977-12-31", "+1982-06-30", "+1983-06-30", "+1985-06-30", "+1987-12-31", "+1989-12-31", "+1990-12-31", "+1992-06-30", "+1993-06-30", "+1994-06-30", "+1995-12-31", "+1997-06-30", "+1998-12-31", "+2005-12-31", "+2008-12-31", "+2012-06-30", "+2015-06-30" }; +struct tai leaptais[sizeof(leapseconds)/sizeof(*leapseconds)]; + +char *dayname[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } ; + +static int32_t times365[4] = { 0, 365, 730, 1095 } ; +static int32_t times36524[4] = { 0, 36524, 73048, 109572 } ; +static int32_t montab[12] = { 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337 } ; +// month length after february is (306 * m + 5) / 10 + +int32_t taidate_mjd(struct taidate cd) +{ + int32_t y,m,d; + d = cd.day - 678882L; + m = cd.month - 1; + y = cd.year; + d += 146097L * (y / 400); + y %= 400; + if (m >= 2) m -= 2; else { m += 10; --y; } + y += (m / 12); + m %= 12; + if (m < 0) { m += 12; --y; } + d += montab[m]; + d += 146097L * (y / 400); + y %= 400; + if (y < 0) { y += 400; d -= 146097L; } + d += times365[y & 3]; + y >>= 2; + d += 1461L * (y % 25); + y /= 25; + d += times36524[y & 3]; + return d; +} + +uint32_t tai2utc(struct tai t) { t.x -= 4611686018427387914ULL; return((uint32_t)t.x); } + +uint32_t tai2utime(struct tai t) +{ + uint64_t mjd; struct taitime ct = tai2time(t,0,0); + mjd = taidate_mjd(ct.date); + return((uint32_t)(mjd * 24*3600 + ct.hour*3600 + ct.minute*60 + ct.second)); +} + +struct tai tai_now() +{ + struct tai t; + t.x = 4611686018427387914ULL + (uint64_t)time(NULL); + t.millis = OS_milliseconds(); + if ( First_TAI.x == 0 ) + { + First_TAI = t, First_utc = (uint32_t)time(NULL); + UTC_ADJUST = -36; + printf("TAINOW.%llu %03.3f UTC.%u vs %u [diff %d]\n",(long long)t.x,t.millis,First_utc,tai2utc(t),UTC_ADJUST); + } + return(t); +} + +struct tai leapsecs_add(struct tai t,int32_t hit) +{ + int32_t i; uint64_t u; + u = t.x; + if ( t.x > leaptais[sizeof(leaptais)/sizeof(*leaptais)-1].x ) + u += (sizeof(leaptais)/sizeof(*leaptais) - 1); + else + { + for (i=0; i leaptais[i].x) ) ++u; + } + } + t.x = u; + return(t); +} + +struct tai taitime2tai(struct taitime ct) +{ + int32_t day,s; struct tai t; + day = taidate_mjd(ct.date); + s = ct.hour * 60 + ct.minute; + s = (s - ct.offset) * 60 + ct.second; + t.x = day * 86400ULL + 4611686014920671114ULL + (uint64_t)s; + t.millis = ct.millis; + return(leapsecs_add(t,ct.second == 60)); +} + +struct tai taidate_scan(char *s,int32_t numleaps) +{ + int32_t z,c,sign = 1; char *t = s; struct taidate cd; struct tai st; + st.x = 0; + if (*t == '-') { ++t; sign = -1; } + else if ( *t == '+' ) + t++; + z = 0; while ((c = (uint8_t) (*t - '0')) <= 9) { z = z * 10 + c; ++t; } + cd.year = z * sign; + if (*t++ != '-') return(st); + z = 0; while ((c = (uint8_t) (*t - '0')) <= 9) { z = z * 10 + c; ++t; } + cd.month = z; + if (*t++ != '-') return(st); + z = 0; while ((c = (uint8_t) (*t - '0')) <= 9) { z = z * 10 + c; ++t; } + cd.day = z; + //printf("year.%d month.%d day.%d numleaps.%d\n",cd.year,cd.month,cd.day,numleaps); + st.x = (taidate_mjd(cd) + 1) * 86400ULL + 4611686014920671114ULL + numleaps; + return(st); +} + +int32_t leapsecs_sub(struct tai *lt) +{ + char out[101],x[TAI_PACK]; double packerr; + int32_t weekday,yearday,i,j,s; uint64_t u; struct tai t,t2; struct taitime ct2; + if ( leaptais[0].x == 0 ) + { + for (i=0; ix; + if ( u > leaptais[sizeof(leaptais)/sizeof(*leaptais)-1].x ) + lt->x -= (sizeof(leaptais)/sizeof(*leaptais) - 1); + else + { + s = 0; + for (i=0; ix = u - s; + return(1); + } + } + lt->x = u - s; + } + return(0); +} + +char *tai_str(char *str,struct tai t) +{ + struct taitime ct; + ct = tai2time(t,0,0); + sprintf(str,"%d-%02d-%02d %02d:%02d:%02d %03.3f",ct.date.year,ct.date.month,ct.date.day,ct.hour,ct.minute,ct.second,ct.millis); + return(str); +} + +char *utc_str(char *str,struct tai t) +{ + t.x += UTC_ADJUST; + return(tai_str(str,t)); +} + +#ifdef ENABLE_TEST +#include +int main(int argc, const char * argv[]) +{ + int i; char str[111],str2[111],str3[111],str4[111]; struct taitime ct; + struct tai t,start = tai_now(); + for (i=0; i<100; i++) + { + sleep(1); + t = tai_now(); + taidate_str(str2,tai2date(t)); + printf("(%s) time.%s date.%s %ld start.%ld %s %u %u\n",tai_str(str3,t),taitime_str(str,ct),str2,(long)tai2utime(t),(long)tai2utime(start),utime_str(str4,t),tai2utc(t),(uint32_t)time(NULL)); + } + // insert code here... + printf("Hello, World!\n"); + return 0; +} +#endif + + +int32_t conv_date(int32_t *secondsp,char *buf); + +double OS_portable_milliseconds() +{ + struct timeval tv; double millis; + gettimeofday(&tv,NULL); + millis = ((double)tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.); + //printf("tv_sec.%ld usec.%d %f\n",tv.tv_sec,tv.tv_usec,millis); + return(millis); +} + +uint32_t OS_conv_datenum(int32_t datenum,int32_t hour,int32_t minute,int32_t second) // datenum+H:M:S -> unix time +{ +#ifdef __PNACL + return(0); +#else + struct tm t; + memset(&t,0,sizeof(t)); + t.tm_year = (datenum / 10000) - 1900, t.tm_mon = ((datenum / 100) % 100) - 1, t.tm_mday = (datenum % 100); + t.tm_hour = hour, t.tm_min = minute, t.tm_sec = second; + return((uint32_t)timegm(&t)); +#endif +} + +double OS_milliseconds() +{ + return(OS_portable_milliseconds()); +} + +int32_t OS_conv_unixtime(int32_t *secondsp,time_t timestamp) // gmtime -> datenum + number of seconds +{ + struct tm t; int32_t datenum; uint32_t checktime; char buf[64]; + t = *gmtime(×tamp); + strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ",&t); //printf("%s\n",buf); + datenum = conv_date(secondsp,buf); + if ( (checktime= OS_conv_datenum(datenum,*secondsp/3600,(*secondsp%3600)/60,*secondsp%60)) != timestamp ) + { + printf("error: timestamp.%u -> (%d + %d) -> %u\n",(uint32_t)timestamp,datenum,*secondsp,checktime); + return(-1); + } + return(datenum); +} + +int32_t is_DST(int32_t datenum) +{ + int32_t year,month,day; + year = datenum / 10000, month = (datenum / 100) % 100, day = (datenum % 100); + if ( month >= 4 && month <= 9 ) + return(1); + else if ( month == 3 && day >= 29 ) + return(1); + else if ( month == 10 && day < 25 ) + return(1); + return(0); +} + +int32_t conv_date(int32_t *secondsp,char *date) +{ + char origdate[64],tmpdate[64]; int32_t year,month,day,hour,min,sec,len; + strcpy(origdate,date), strcpy(tmpdate,date), tmpdate[8 + 2] = 0; + year = atoi(tmpdate), month = atoi(tmpdate+5), day = atoi(tmpdate+8); + *secondsp = 0; + if ( (len= (int32_t)strlen(date)) <= 10 ) + hour = min = sec = 0; + if ( len >= 18 ) + { + tmpdate[11 + 2] = 0, tmpdate[14 + 2] = 0, tmpdate[17 + 2] = 0; + hour = atoi(tmpdate+11), min = atoi(tmpdate + 14), sec = atoi(tmpdate+17); + if ( hour >= 0 && hour < 24 && min >= 0 && min < 60 && sec >= 0 && sec < 60 ) + *secondsp = (3600*hour + 60*min + sec); + else printf("ERROR: seconds.%d %d %d %d, len.%d\n",*secondsp,hour,min,sec,len); + } + sprintf(origdate,"%d-%02d-%02d",year,month,day); //2015-07-25T22:34:31Z + if ( strcmp(tmpdate,origdate) != 0 ) + { + printf("conv_date date conversion error (%s) -> (%s)\n",origdate,date); + return(-1); + } + return((year * 10000) + (month * 100) + day); +} + +int32_t extract_datenum(int32_t *yearp,int32_t *monthp,int32_t *dayp,int32_t datenum) +{ + *yearp = datenum / 10000, *monthp = (datenum / 100) % 100, *dayp = (datenum % 100); + if ( *yearp >= 2000 && *yearp <= 2038 && *monthp >= 1 && *monthp <= 12 && *dayp >= 1 && *dayp <= 31 ) + return(datenum); + else return(-1); +} + +int32_t expand_datenum(char *date,int32_t datenum) { int32_t year,month,day; date[0] = 0; if ( extract_datenum(&year,&month,&day,datenum) != datenum) return(-1); sprintf(date,"%d-%02d-%02d",year,month,day); return(0); } + +int32_t calc_datenum(int32_t year,int32_t month,int32_t day) { return((year * 10000) + (month * 100) + day); } + + +int32_t ecb_decrdate(int32_t *yearp,int32_t *monthp,int32_t *dayp,char *date,int32_t datenum) +{ + static int lastday[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + int32_t year,month,day; + year = datenum / 10000, month = (datenum / 100) % 100, day = (datenum % 100); + //printf("%d -> %d %d %d\n",datenum,year,month,day); + if ( --day <= 0 ) + { + if ( --month <= 0 ) + { + if ( --year < 2000 ) + { + printf("reached epoch start\n"); + return(-1); + } + month = 12; + } + day = lastday[month]; + if ( month == 2 && (year % 4) == 0 ) + day++; + } + sprintf(date,"%d-%02d-%02d",year,month,day); + //printf("%d -> %d %d %d (%s)\n",datenum,year,month,day,date); + *yearp = year, *monthp = month, *dayp = day; + return((year * 10000) + (month * 100) + day); +} diff --git a/crypto777/SaM.c b/crypto777/SaM.c new file mode 100755 index 000000000..9f1f0d359 --- /dev/null +++ b/crypto777/SaM.c @@ -0,0 +1,951 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +// based on SaM code by Come-from-Beyond + +#ifdef DEFINES_ONLY +#ifndef crypto777_SaM_h +#define crypto777_SaM_h +#include +#include +#include + +#define TRIT signed char + +#define TRIT_FALSE 1 +#define TRIT_UNKNOWN 0 +#define TRIT_TRUE -1 + +#define SAM_HASH_SIZE 243 +#define SAM_STATE_SIZE (SAM_HASH_SIZE * 3) +#define SAM_NUMBER_OF_ROUNDS 9 +#define SAM_DELTA 254 + +#define SAMHIT_LIMIT ((uint64_t)1594323 * 4782969) //7625597484987LL // 3 ** 27 +#define MAX_CRYPTO777_HIT (((uint64_t)1 << 62) / 1000) + +//#include "bits777.c" +//#include "utils777.c" +#include +#include "../includes/curve25519.h" +#define MAX_INPUT_SIZE ((int32_t)(65536 - sizeof(bits256) - 2*sizeof(uint32_t))) + +struct SaM_info { bits384 bits; TRIT trits[SAM_STATE_SIZE],hash[SAM_HASH_SIZE]; }; +struct SaMhdr { bits384 sig; uint32_t timestamp,nonce; uint8_t numrounds,leverage; }; + +void SaM_Initialize(struct SaM_info *state); +int32_t SaM_Absorb(struct SaM_info *state,const uint8_t *input,const uint32_t inputSize,const uint8_t *input2,const uint32_t inputSize2); +bits384 SaM_emit(struct SaM_info *state); +bits384 SaM_encrypt(uint8_t *dest,uint8_t *src,int32_t len,bits384 password,uint32_t timestamp); +uint64_t SaM_threshold(int32_t leverage); +uint64_t SaM(bits384 *sigp,uint8_t *input,int32_t inputSize,uint8_t *input2,int32_t inputSize2); +uint32_t SaM_nonce(void *data,int32_t datalen,int32_t leverage,int32_t maxmillis,uint32_t nonce); +//uint64_t SaMnonce(bits384 *sigp,uint32_t *noncep,uint8_t *buf,int32_t len,uint64_t threshold,uint32_t rseed,int32_t maxmillis); +#endif +#else +#ifndef crypto777_SaM_c +#define crypto777_SaM_c + +#ifndef crypto777_SaM_h +#define DEFINES_ONLY +#include "SaM.c" +#undef DEFINES_ONLY +#endif + +static int32_t SAM_INDICES[SAM_STATE_SIZE]; + +void SaM_PrepareIndices() +{ + int32_t i,nextIndex,currentIndex = 0; + for (i=0; itrits[currentIndex],state->trits[nextIndex]); + //rightPart.trits[i] = SaM_Bias(state->trits[nextIndex],state->trits[currentIndex]); + leftPart.trits[i] = SAMBIAS[state->trits[currentIndex]+1][1+state->trits[nextIndex]]; + rightPart.trits[i] = SAMBIAS[state->trits[nextIndex]+1][1+state->trits[currentIndex]]; + currentIndex = nextIndex; + } + for (i=0; itrits[i] = SaM_Sum(leftPart.trits[currentIndex],rightPart.trits[nextIndex]); + state->trits[i] = SAMSUM[leftPart.trits[currentIndex]+1][1+rightPart.trits[nextIndex]]; + currentIndex = nextIndex; + } + } +} + +void SaM_Initialize(struct SaM_info *state) +{ + int32_t i; + for (i=SAM_HASH_SIZE; itrits[i] = (i & 1) ? TRIT_FALSE : TRIT_TRUE; +} + +void SaM_Squeeze(struct SaM_info *state,TRIT *output) +{ + memcpy(output,state->trits,SAM_HASH_SIZE * sizeof(TRIT)); + SaM_SplitAndMerge(state); +} + +void _SaM_Absorb(struct SaM_info *state,const TRIT *input,const int32_t inputSize) +{ + int32_t size,i,remainder = inputSize; + do + { + size = remainder >= SAM_HASH_SIZE ? SAM_HASH_SIZE : remainder; + memcpy(state->trits,&input[inputSize - remainder],size); + remainder -= SAM_HASH_SIZE; + if ( size < SAM_HASH_SIZE ) + for (i=size; itrits[i] = (i & 1) ? TRIT_FALSE : TRIT_TRUE; + SaM_SplitAndMerge(state); + } while ( remainder > 0 ); +} + +int32_t SaM_Absorb(struct SaM_info *state,const uint8_t *input,uint32_t inputSize,const uint8_t *input2,uint32_t inputSize2) +{ + //TRIT output[(MAX_INPUT_SIZE + sizeof(struct SaMhdr)) << 3]; + TRIT *trits,tritbuf[4096]; + int32_t i,size,n = 0; + /*if ( inputSize + inputSize2 > sizeof(output) ) + { + printf("SaM overflow (%d + %d) > %ld\n",inputSize,inputSize2,sizeof(output)); + if ( inputSize > MAX_INPUT_SIZE ) + inputSize = MAX_INPUT_SIZE; + inputSize2 = 0; + }*/ + size = (inputSize + inputSize2) << 3; + trits = (size < sizeof(tritbuf)) ? tritbuf : malloc(size); + if ( input != 0 && inputSize != 0 ) + { + for (i=0; i<(inputSize << 3); i++) + trits[n++] = ((input[i >> 3] & (1 << (i & 7))) != 0); + } + if ( input2 != 0 && inputSize2 != 0 ) + { + for (i=0; i<(inputSize2 << 3); i++) + trits[n++] = ((input2[i >> 3] & (1 << (i & 7))) != 0); + } + _SaM_Absorb(state,trits,n); + if ( trits != tritbuf ) + free(trits); + return(n); +} + +static TRIT InputA[] = { 0 }; // zero len +static TRIT OutputA[] = { 1, -1, 1, 1, -1, -1, 0, -1, 0, 0, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0, 0, 0, 1, 1, -1, -1, 0, 0, 1, -1, -1, 0, 0, -1, 1, -1, 0, 0, -1, -1, -1, -1, 0, 0, 0, -1, 1, 0, 1, 0, -1, -1, -1, -1, 0, 1, -1, 1, -1, 0, 1, 1, 0, 0, -1, 0, 1, 1, -1, 1, 0, 0, 0, 1, 0, -1, 1, 1, 0, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 0, 1, -1, 1, -1, 0, 0, 1, 1, 1, 1, -1, 1, 1, -1, 0, 0, 1, 1, 0, 0, -1, 1, 1, -1, 0, 0, -1, 0, 0, 1, 0, 0, 0, -1, 1, -1, 0, 1, -1, 0, -1, 1, 1, 1, -1, 0, 1, 1, -1, -1, 0, 0, 1, -1, -1, -1, 0, -1, -1, 1, 1, 0, 1, 0, 1, -1, 1, -1, -1, 0, 0, -1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, -1, 1, -1, 0, 0, 1, 0, -1, -1, -1, 1, -1, 1, -1, -1, 1, 0, 1, -1, 1, -1, 1, -1, 1, 0, 1, 0, 1, -1, -1, -1, -1, 1, 0, 0, -1, -1, 1, 0, 1, 1, -1, 1, -1, -1, -1, 0, 0, -1, 0, 1, 1, 1, 0, 1, 1, -1, 1, 1, 0, 1, 1, 1, 0, -1, 0, 0, -1, -1, -1 }; + +static TRIT InputB[] = { 0 }; +static TRIT OutputB[] = { -1, -1, -1, 1, 0, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1, 0, 0, 0, 0, 1, 1, 0, -1, 1, 0, 1, 0, 1, -1, 0, -1, 0, 0, -1, 1, -1, -1, 0, 0, 1, -1, -1, 0, 0, -1, 1, 1, 0, 1, 0, 0, 1, -1, 1, 0, -1, -1, 1, -1, 0, -1, 1, -1, 0, 0, 0, 1, -1, 0, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 0, 1, -1, -1, -1, 0, 1, 0, 0, -1, 1, 1, 0, 0, -1, 1, 1, 0, -1, -1, 0, 0, 0, -1, 1, 0, -1, 0, -1, 0, -1, 0, -1, 0, 1, 0, 1, 0, -1, 1, 0, -1, 1, 1, -1, 1, 0, 1, -1, -1, 1, 1, 0, -1, 0, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 0, 0, 0, 0, -1, -1, 1, 1, 1, -1, 1, 0, -1, 1, 0, 1, 0, 0, -1, -1, 1, 1, 0, 0, 1, 0, 0, 0, 0, -1, 1, 0, 0, 1, 1, 0, -1, 1, -1, 1, 0, -1, 0, 0, 1, -1, -1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, -1, 1, -1, 1, 1, 1, -1, 0, 1, 0, -1, 1, 0, 1, 1, 0, -1, 1, 1, -1, 0, -1, 1, 1, 0, -1, -1, -1, -1, 1, 0, 0, -1, -1, -1, 0, 1 }; + +static TRIT InputC[] = { 1 }; +static TRIT OutputC[] = { 1, -1, 1, 1, -1, -1, 0, -1, 0, 0, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0, 0, 0, 1, 1, -1, -1, 0, 0, 1, -1, -1, 0, 0, -1, 1, -1, 0, 0, -1, -1, -1, -1, 0, 0, 0, -1, 1, 0, 1, 0, -1, -1, -1, -1, 0, 1, -1, 1, -1, 0, 1, 1, 0, 0, -1, 0, 1, 1, -1, 1, 0, 0, 0, 1, 0, -1, 1, 1, 0, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 0, 1, -1, 1, -1, 0, 0, 1, 1, 1, 1, -1, 1, 1, -1, 0, 0, 1, 1, 0, 0, -1, 1, 1, -1, 0, 0, -1, 0, 0, 1, 0, 0, 0, -1, 1, -1, 0, 1, -1, 0, -1, 1, 1, 1, -1, 0, 1, 1, -1, -1, 0, 0, 1, -1, -1, -1, 0, -1, -1, 1, 1, 0, 1, 0, 1, -1, 1, -1, -1, 0, 0, -1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, -1, 1, -1, 0, 0, 1, 0, -1, -1, -1, 1, -1, 1, -1, -1, 1, 0, 1, -1, 1, -1, 1, -1, 1, 0, 1, 0, 1, -1, -1, -1, -1, 1, 0, 0, -1, -1, 1, 0, 1, 1, -1, 1, -1, -1, -1, 0, 0, -1, 0, 1, 1, 1, 0, 1, 1, -1, 1, 1, 0, 1, 1, 1, 0, -1, 0, 0, -1, -1, -1 }; + +static TRIT InputD[] = { -1 }; +static TRIT OutputD[] = { -1, 0, 0, 1, 1, 0, -1, 1, 1, 0, 1, 0, -1, 1, -1, 0, 0, 1, 0, -1, 0, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 0, 0, 0, -1, -1, 1, 1, -1, 1, -1, 0, -1, 1, -1, 0, 0, -1, 0, 0, 0, -1, -1, 0, -1, 1, -1, 1, 1, 0, -1, 1, -1, 0, 0, 1, -1, 1, -1, 0, 0, 1, 1, -1, -1, -1, -1, 1, 0, 0, -1, 0, 0, -1, 0, 0, 1, -1, -1, -1, -1, 1, 1, 0, 0, -1, 1, -1, 1, 0, 0, -1, 1, -1, 0, 1, 1, -1, 1, -1, 0, -1, -1, 0, 0, 0, -1, 0, 0, -1, 1, -1, 0, -1, 1, -1, 1, 1, -1, -1, 0, 0, 0, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 0, -1, 0, 1, 0, 0, -1, 1, -1, 0, 1, 0, 1, 1, -1, 0, 1, 1, 0, 0, -1, -1, -1, -1, 0, 1, 0, -1, -1, 0, 0, 1, 1, 1, 0, 0, -1, 1, -1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, -1, -1, 0, -1, -1, 0, -1, -1, 1, 0, 0, -1, -1, 1, 0, 0, 0, 1, 1, 0, -1, 1, -1, -1, 1, -1, -1, 1, 1, 0, 1, 0, 0, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0, 0, 0, -1, -1, 1, 1 }; + +static TRIT InputE[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +static TRIT OutputE[] = { 0, 1, 0, 1, -1, -1, 1, -1, -1, 0, 0, 1, 1, -1, -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, -1, 0, -1, -1, -1, -1, -1, 1, -1, -1, -1, 0, 0, 1, 1, 0, 1, -1, -1, 0, -1, -1, 1, 1, 1, -1, 1, 1, 0, -1, 0, 1, -1, 1, -1, 1, 1, -1, 1, 0, -1, -1, -1, 0, 0, 1, 1, 0, -1, 0, 0, -1, 0, 0, 1, 1, -1, 0, 1, -1, -1, 1, -1, 1, -1, 0, 1, -1, 1, 0, 1, -1, -1, -1, 0, 1, -1, 0, 1, -1, 1, 0, -1, 1, -1, 1, 0, -1, -1, 1, 0, 1, 0, 0, 1, 1, 1, -1, 1, -1, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, 0, 0, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 1, 0, 0, 1, 0, -1, -1, 0, 0, -1, -1, 1, -1, 0, -1, 1, -1, 0, 1, -1, 0, 1, 1, -1, 1, -1, 1, -1, 0, 0, 0, -1, 0, -1, 1, -1, 1, 1, 1, 1, 1, 0, -1, 0, -1, -1, 0, 0, -1, -1, 1, -1, -1, -1, 1, 0, 0, 0, 1, 0, 1, 0, 1, -1, 0, -1, -1, 1, -1, -1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, -1, -1, -1, -1, 1, 0, -1, 0, 0 }; + +bits384 SaM_emit(struct SaM_info *state) +{ + // i.12 531441 81bf1 0.68% numbits.19 mask.7ffff -> bias -0.0005870312 + TRIT *ptr; + uint64_t bits64; + uint32_t i,j,rawbits,bits19[20],mask = 0x7ffff; + SaM_Squeeze(state,state->hash); + ptr = state->hash; + for (i=0; i 27 + rawbits = (rawbits * 3 + *ptr++ + 1); + rawbits = (((rawbits<<4)/27) & 0xf); + //printf("%x -> Sam_emit\n",rawbits); + for (bits64=i=0; i<20; i++) + { + memcpy(&state->bits.bytes[i*sizeof(uint16_t)],&bits19[i],sizeof(uint16_t)); + bits64 = (bits64 << 3) | ((bits19[i] >> 16) & 7); + } + bits64 = (bits64 << 4) | (rawbits & 0xf); + memcpy(&state->bits.bytes[40],&bits64,sizeof(uint64_t)); + return(state->bits); +} + +int32_t _SaM_test(char *msg,TRIT *testvector,int32_t n,TRIT *checkvals) +{ + struct SaM_info state; int32_t i,errs; + SaM_Initialize(&state); + _SaM_Absorb(&state,testvector,n); + SaM_emit(&state); + for (i=errs=0; i<243; i++) + { + if ( state.hash[i] != checkvals[i] ) + errs++; + } + if ( errs != 0 ) + { + for (i=0; i<243; i++) + printf("%2d, ",state.hash[i]); + printf("\nSaM_test.%s errs.%d vs output\n",msg,errs); + } + return(errs); +} + +int32_t bitweight(uint64_t x) +{ + int i,wt = 0; + for (i=0; i<64; i++) + if ( (1LL << i) & x ) + wt++; + return(wt); +} + +#define SETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] |= (1 << ((bitoffset) & 7))) +#define GETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] & (1 << ((bitoffset) & 7))) +#define CLEARBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] &= ~(1 << ((bitoffset) & 7))) + + +int32_t SaM_test() +{ + int32_t i,j,wt,iter,totalset,totalclr,setcount[48*8],clrcount[48*8],histo[16]; bits256 seed; + struct SaM_info state; + uint8_t buf[4096*2],bits[2][10][48]; + double startmilli = time(NULL) * 1000; + for (i=0; i<1000; i++) + { + _SaM_test("A",InputA,0,OutputA); + _SaM_test("B",InputB,sizeof(InputB),OutputB); + _SaM_test("C",InputC,sizeof(InputC),OutputC); + _SaM_test("D",InputD,sizeof(InputD),OutputD); + _SaM_test("E",InputE,sizeof(InputE),OutputE); + } + printf("per SaM %.3f\n",((time(NULL) * 1000) - startmilli) / (5 * i)); + memset(seed.bytes,0,sizeof(seed)); + memcpy(seed.bytes,(uint8_t *)"12345678901",11); + for (i=0; i<243*2; i++) + buf[i] = 0; + OS_randombytes(buf,sizeof(buf)); + for (iter=0; iter<2; iter++) + { + memset(&state,0,sizeof(state)); + SaM_Initialize(&state); + SaM_Absorb(&state,buf,243*2,0,0); + memset(setcount,0,sizeof(setcount)); + memset(clrcount,0,sizeof(clrcount)); + memset(histo,0,sizeof(histo)); + for (i=0; i<5; i++) + { + if ( 0 && (i % 100) == 99 ) + { + for (j=0; j<32; j++) + seed.bytes[j] = rand() >> 8; + SaM_Absorb(&state,seed.bytes,sizeof(seed),0,0); + } + memset(bits[iter][i],0,sizeof(bits[iter][i])); + SaM_emit(&state); + memcpy(bits[iter][i],state.bits.bytes,sizeof(bits[iter][i])); + for (j=0; j<48; j++) + { + histo[bits[iter][i][j] & 0xf]++; + histo[(bits[iter][i][j]>>4) & 0xf]++; + printf("%02x ",bits[iter][i][j]); + } + printf("\n"); + for (j=0; j<48*8; j++) + { + if ( GETBIT(bits[iter][i],j) != 0 ) + setcount[j]++; + else clrcount[j]++; + } + } + for (i=0; i<16; i++) + printf("%8d ",histo[i]); + printf("hex histogram\n"); + seed.bytes[0] ^= 1; + buf[0] ^= 1; + } + for (i=0; i<5; i++) + { + for (j=wt=0; j<48; j++) + { + wt += bitweight(bits[0][i][j] ^ bits[1][i][j]); + printf("%02x",bits[0][i][j] ^ bits[1][i][j]); + } + printf(" i.%d diff.%d\n",i,wt); + } + //set.19090245 clr.19309755 -0.0057 + //total set.19200072 clr.19199928 0.0000037500 + // total set.19191713 clr.19208287 -0.0004316146 + for (totalset=totalclr=j=0; j<48*8; j++) + { + totalset += setcount[j]; + totalclr += clrcount[j]; + printf("%.2f ",(double)(setcount[j]-clrcount[j])/i); + } + printf("total set.%d clr.%d %.10f\n",totalset,totalclr,(double)(totalset-totalclr)/(totalset+totalclr)); + return(0); +} + +bits384 SaM_encrypt(uint8_t *dest,uint8_t *src,int32_t len,bits384 password,uint32_t timestamp) +{ + bits384 xorpad; int32_t i; struct SaM_info XORpad; + SaM_Initialize(&XORpad), SaM_Absorb(&XORpad,password.bytes,sizeof(password),(void *)×tamp,sizeof(timestamp)); + while ( len >= 0 ) + { + SaM_emit(&XORpad); + for (i=0; i=0; i++,len--) + { + xorpad.bytes[i] = (XORpad.bits.bytes[i] ^ *src++); + if ( dest != 0 ) + *dest++ = xorpad.bytes[i]; + } + } + return(xorpad); +} + +uint64_t SaM_hit(struct SaM_info *state) +{ + int32_t i; uint64_t hit = 0; + for (i=0; i<27; i++) + hit = (hit * 3 + state->hash[i] + 1); + return(hit); +} + +uint64_t SaM(bits384 *sigp,uint8_t *input,int32_t inputSize,uint8_t *input2,int32_t inputSize2) +{ + int32_t verify_SaM(TRIT *newhash,uint8_t *buf,const int n); + struct SaM_info state; + SaM_Initialize(&state); + SaM_Absorb(&state,input,inputSize,input2,inputSize2); + //printf("len.%d: ",inputSize+inputSize2); + *sigp = SaM_emit(&state); + //if ( 0 && input2 == 0 && numrounds == SAM_MAGIC_NUMBER ) + // verify_SaM(state.hash,(uint8_t *)input,inputSize); + return(SaM_hit(&state)); +} + +uint64_t SaM_threshold(int32_t leverage) +{ + int32_t i; + uint64_t threshold,divisor = 1; + if ( leverage > 26 ) + leverage = 26; + for (i=0; i + +uint32_t SaM_nonce(void *data,int32_t datalen,int32_t leverage,int32_t maxmillis,uint32_t nonce) +{ + double milliseconds(); + uint64_t hit,threshold; bits384 sig; double endmilli; + if ( leverage != 0 ) + { + threshold = SaM_threshold(leverage); + if ( maxmillis == 0 ) + { + if ( (hit= SaM(&sig,data,datalen,(void *)&nonce,sizeof(nonce))) >= threshold ) + { + printf("nonce failure hit.%llu >= threshold.%llu | leverage.%d nonce.%u\n",(long long)hit,(long long)threshold,leverage,nonce); + if ( (threshold - hit) > ((uint64_t)1L << 32) ) + return(0xffffffff); + else return((uint32_t)(threshold - hit)); + } + } + else + { + endmilli = (milliseconds() + maxmillis); + while ( milliseconds() < endmilli ) + { + OS_randombytes((void *)&nonce,sizeof(nonce)); + if ( (hit= SaM(&sig,data,datalen,(void *)&nonce,sizeof(nonce))) < threshold ) + { + printf("-> nonce.%u leverage.%d | hit.%llu < threshold.%llu\n",nonce,leverage,(long long)hit,(long long)threshold); + SaM_nonce(data,datalen,leverage,0,nonce); + return(nonce); + } + } + } + } + return(0); +} + +/*uint64_t SaMnonce(bits384 *sigp,uint32_t *noncep,uint8_t *buf,int32_t len,uint64_t threshold,uint32_t rseed,int32_t maxmillis) +{ + uint64_t hit = SAMHIT_LIMIT; + double startmilli = 0; + if ( maxmillis == 0 ) + { + hit = calc_SaM(sigp,buf,len,0,0); + if ( hit >= threshold ) + { + printf("nonce failure hit.%llu >= threshold.%llu\n",(long long)hit,(long long)threshold); + return(threshold - hit); + } + else return(0); + } + else startmilli = milliseconds(); + while ( hit >= threshold ) + { + if ( rseed == 0 ) + randombytes((uint8_t *)noncep,sizeof(*noncep)); + else _randombytes((uint8_t *)noncep,sizeof(*noncep),rseed); + hit = calc_SaM(sigp,buf,len,0,0); + //printf("%llu %.2f%% (%s) len.%d numrounds.%lld threshold.%llu seed.%u\n",(long long)hit,100.*(double)hit/threshold,(char *)buf,len,(long long)numrounds,(long long)threshold,rseed); + if ( maxmillis != 0 && milliseconds() > (startmilli + maxmillis) ) + return(0); + if ( rseed != 0 ) + rseed = (uint32_t)(sigp->txid ^ hit); + } + //printf("%5.1f %14llu %7.2f%% numrounds.%lld threshold.%llu seed.%u\n",milliseconds()-startmilli,(long long)hit,100.*(double)hit/threshold,(long long)numrounds,(long long)threshold,rseed); + return(hit); +}*/ + +#ifdef include_vps +// from Come-from-Beyond +#define HASH_SIZE 32 + +#define DAILY 0 +#define WEEKLY 1 +#define MONTHLY 2 +#define YEARLY 3 + +#define MAX_NUMBER_OF_POOLS 1000 +#define MAX_NUMBER_OF_TOKENS 1000 +#define MAX_NUMBER_OF_UNITS 1000000 +#define MAX_NUMBER_OF_SUPERVISORS 1000000 + +#define MAX_TOKEN_LIFESPAN 36500 + +unsigned int numberOfPools = 0; +struct Pool { + + signed long reserve; + unsigned long quorum, decisionThreshold; + +} pools[MAX_NUMBER_OF_POOLS]; + +unsigned int numberOfTokens = 0; +struct Token { + + BOOL enabled; + unsigned int pool; + unsigned long curSupply, maxSupply; // Defines max %% of total coin supply that can be locked + signed int fadeRate; // Per day in 1/1000th (zero - to keep value const; negative - for deflation; positive - for inflation) + unsigned int decreaseLimits[YEARLY + 1], increaseLimits[YEARLY + 1]; // In 1/1000th + unsigned long unitSize; // Locked amount + unsigned short minLockPeriod, maxLockPeriod; // In days + unsigned char minExtraLockPeriod, maxExtraLockPeriod; // In days + unsigned char redemptionGap; // In days + unsigned long day0Offset; // UNIX time + unsigned long prices[MAX_TOKEN_LIFESPAN]; // In main currency units + +} tokens[MAX_NUMBER_OF_TOKENS]; + +unsigned int numberOfUnits = 0; +struct Unit { + + unsigned long id; + unsigned int token; + unsigned long account; + signed int fadeRate; + unsigned long size; + unsigned long timestamp; + unsigned char lockPeriodHash[HASH_SIZE]; + unsigned short minLockPeriod, maxLockPeriod; + unsigned char extraLockPeriod; + unsigned char redemptionGap; + +} units[MAX_NUMBER_OF_UNITS]; + +unsigned int numberOfSupervisors = 0; +struct Supervisor { + + unsigned long id; + signed long rating; + unsigned int activity; + +} supervisors[MAX_NUMBER_OF_SUPERVISORS]; + +struct Vote { + + unsigned long supervisorId; + unsigned long price; + unsigned long tolerance; + unsigned long weight; + unsigned long bet; +}; + +unsigned char random() { + + return 42; // TODO: Replace with a better RNG +} + +void hash(unsigned char* data, unsigned int dataSize, unsigned char* hash) { + + // TODO: Invoke SHA-256 +} + +unsigned int addPool(unsigned long quorum, unsigned long decisionThreshold) { + // Returns the index of the new pool + + if (numberOfPools >= MAX_NUMBER_OF_POOLS) { + + // TODO: Throw exception + } + + pools[numberOfPools].reserve = 0; + pools[numberOfPools].quorum = quorum; + pools[numberOfPools].decisionThreshold = decisionThreshold; + + return numberOfPools++; +} + +unsigned int addToken(unsigned int pool, + unsigned long maxSupply, + signed int fadeRate, + unsigned int* decreaseLimits, unsigned int* increaseLimits, + unsigned long unitSize, + unsigned short minLockPeriod, unsigned short maxLockPeriod, + unsigned char minExtraLockPeriod, unsigned char maxExtraLockPeriod, + unsigned char redemptionGap, + unsigned long day0Offset, + unsigned long initialPrice) { + // Returns the index of the new token + + if (numberOfTokens >= MAX_NUMBER_OF_TOKENS) { + + // TODO: Throw exception + } + + if (pool >= numberOfPools) { + + // TODO: Throw exception + } + + if (minLockPeriod > maxLockPeriod || minExtraLockPeriod > maxExtraLockPeriod) { + + // TODO: Throw exception + } + + tokens[numberOfTokens].enabled = TRUE; + tokens[numberOfTokens].pool = pool; + tokens[numberOfTokens].curSupply = 0; + tokens[numberOfTokens].maxSupply = maxSupply; + tokens[numberOfTokens].fadeRate = fadeRate; + memcpy(tokens[numberOfTokens].decreaseLimits, decreaseLimits, sizeof(tokens[numberOfTokens].decreaseLimits)); + memcpy(tokens[numberOfTokens].increaseLimits, increaseLimits, sizeof(tokens[numberOfTokens].increaseLimits)); + tokens[numberOfTokens].unitSize = unitSize; + tokens[numberOfTokens].minLockPeriod = minLockPeriod; + tokens[numberOfTokens].maxLockPeriod = maxLockPeriod; + tokens[numberOfTokens].minExtraLockPeriod = minExtraLockPeriod; + tokens[numberOfTokens].maxExtraLockPeriod = maxExtraLockPeriod; + tokens[numberOfTokens].redemptionGap = redemptionGap; + tokens[numberOfTokens].day0Offset = day0Offset; + + memset(tokens[numberOfTokens].prices, 0, sizeof(tokens[numberOfTokens].prices)); + tokens[numberOfTokens].prices[0] = initialPrice; + + return numberOfTokens++; +} + +void enableToken(unsigned int token) { + + tokens[token].enabled = TRUE; +} + +void disableToken(unsigned int token) { + + tokens[token].enabled = FALSE; +} + +void changeFadeRate(unsigned int token, signed int newFadeRate) { + + tokens[token].fadeRate = newFadeRate; +} + +void changeUnitSize(unsigned int token, unsigned long newUnitSize) { + + tokens[token].unitSize = newUnitSize; +} + +void changeLockPeriods(unsigned int token, unsigned short newMinLockPeriod, unsigned short newMaxLockPeriod) { + + tokens[token].minLockPeriod = newMinLockPeriod; + tokens[token].maxLockPeriod = newMaxLockPeriod; +} + +void changeExtraLockPeriods(unsigned int token, unsigned char newMinExtraLockPeriod, unsigned char newMaxExtraLockPeriod) { + + tokens[token].minExtraLockPeriod = newMinExtraLockPeriod; + tokens[token].maxExtraLockPeriod = newMaxExtraLockPeriod; +} + +void changeRedemptionGap(unsigned int token, unsigned char newRedemptionGap) { + + tokens[token].redemptionGap = newRedemptionGap; +} + +void getLockPeriodHashAndPrefix(unsigned short lockPeriod, unsigned char* lockPeriodHash, unsigned char* lockPeriodPrefix) { + + unsigned char buffer[HASH_SIZE]; + int i; + for (i = 0; i < HASH_SIZE - sizeof(lockPeriod); i++) { + + buffer[i] = random(); + } + *((unsigned short*)&buffer[i]) = lockPeriod; // WARNING: Depends on endianness! + hash(buffer, sizeof(buffer), lockPeriodHash); + memcpy(lockPeriodPrefix, buffer, i); +} + +unsigned long getLastPrice(unsigned int token, unsigned long time) { + + for (int i = (time - tokens[token].day0Offset) / (24 * 60 * 60 * 1000) + 1; i-- > 0;) { + + if (tokens[token].prices[i] > 0) { + + return tokens[token].prices[i]; + } + } +} + +unsigned int addUnit(unsigned long id, + unsigned int token, + unsigned long account, + unsigned long time, + unsigned char* lockPeriodHash, + unsigned short minLockPeriod, unsigned short maxLockPeriod, + unsigned long seed, + unsigned long mainCurrencyUnitSize) { + // Returns the index of the new unit + + if (numberOfUnits >= MAX_NUMBER_OF_UNITS) { + + // TODO: Throw exception + } + + if (token >= numberOfTokens) { + + // TODO: Throw exception + } + + if (tokens[token].enabled == FALSE) { + + // TODO: Throw exception + } + + units[numberOfUnits].id = id; + units[numberOfUnits].token = token; + units[numberOfUnits].account = account; + units[numberOfUnits].fadeRate = tokens[token].fadeRate; + units[numberOfUnits].size = tokens[token].unitSize; + units[numberOfUnits].timestamp = time; + memcpy(units[numberOfUnits].lockPeriodHash, lockPeriodHash, HASH_SIZE); + units[numberOfUnits].minLockPeriod = minLockPeriod; + units[numberOfUnits].maxLockPeriod = maxLockPeriod; + units[numberOfUnits].extraLockPeriod = seed % (tokens[token].maxExtraLockPeriod - tokens[token].minExtraLockPeriod + 1) + tokens[token].minExtraLockPeriod; + units[numberOfUnits].redemptionGap = tokens[token].redemptionGap; + + pools[tokens[token].pool].reserve += units[numberOfUnits].size * getLastPrice(token, time) / mainCurrencyUnitSize; // WARNING: May overflow! + + return numberOfUnits++; +} + +unsigned long redeemUnit(unsigned long id, unsigned long account, unsigned short lockPeriod, unsigned char* lockPeriodPrefix, unsigned long time, unsigned long mainCurrencyUnitSize) { + // Returns amount to add to the account balance + + for (int i = 0; i < numberOfUnits; i++) { + + if (units[i].id == id) { + + if (units[i].account == account) { + + unsigned char buffer[HASH_SIZE]; + memcpy(buffer, lockPeriodPrefix, HASH_SIZE - sizeof(lockPeriod)); + *((unsigned short*)&buffer[HASH_SIZE - sizeof(lockPeriod)]) = lockPeriod; // WARNING: Depends on endianness! + unsigned char lockPeriodHash[HASH_SIZE]; + hash(buffer, sizeof(buffer), lockPeriodHash); + for (int j = 0; j < HASH_SIZE; j++) { + + if (lockPeriodHash[j] != units[i].lockPeriodHash[j]) { + + return 0; + } + } + + if (lockPeriod < units[i].minLockPeriod || lockPeriod > units[i].maxLockPeriod) { + + return 0; + } + + unsigned int delta = (time - units[i].timestamp) / (24 * 60 * 60 * 1000); + if (delta < lockPeriod + units[i].extraLockPeriod || delta > lockPeriod + units[i].extraLockPeriod + units[i].redemptionGap) { + + return 0; + } + + unsigned long amount = units[i].size * getLastPrice(units[i].token, units[i].timestamp + (lockPeriod + units[i].extraLockPeriod) * 24 * 60 * 60 * 1000) / mainCurrencyUnitSize; // WARNING: May overflow! + for (int j = lockPeriod + units[i].extraLockPeriod; j-- > 0; ) { + + amount = amount * (1000 - units[i].fadeRate) / 1000; // WARNING: Do not use floating-point math! + } + if (pools[tokens[units[i].token].pool].reserve < amount) { + + amount = pools[tokens[units[i].token].pool].reserve; + } + pools[tokens[units[i].token].pool].reserve -= amount; + + memcpy(&units[i], &units[--numberOfUnits], sizeof(Unit)); + + return amount; + } + + break; + } + } + + return 0; +} + +void salvageExpiredUnits(unsigned long time) { + + for (int i = numberOfUnits; i-- > 0; ) { + + if ((time - units[i].timestamp) / (24 * 60 * 60 * 1000) > units[i].maxLockPeriod + units[i].extraLockPeriod + units[i].redemptionGap) { + + memcpy(&units[i], &units[--numberOfUnits], sizeof(Unit)); + } + } +} + +unsigned int addSupervisor(unsigned long id) { + // Returns the index of the new supervisor + + if (numberOfSupervisors >= MAX_NUMBER_OF_SUPERVISORS) { + + // TODO: Throw exception + } + + supervisors[numberOfSupervisors].id = id; + supervisors[numberOfSupervisors].rating = 0; + supervisors[numberOfSupervisors].activity = 0; + + return numberOfSupervisors++; +} + +Supervisor* getSupervisor(unsigned long id) { + + for (int i = 0; i < numberOfSupervisors; i++) { + + if (supervisors[i].id == id) { + + return &supervisors[i]; + } + } + + return NULL; +} + +BOOL castSupervisorVotes(unsigned int token, unsigned long time, Vote* votes, unsigned int numberOfVotes, unsigned long* prizes) { + // Returns if a new price has been set + + unsigned long totalWeight = 0; + unsigned long totalBet = 0; + for (int i = 0; i < numberOfVotes; i++) { + + totalWeight += votes[i].weight; + getSupervisor(votes[i].supervisorId)->activity++; + totalBet += votes[i].bet; + } + + if (totalWeight < pools[tokens[token].pool].quorum) { + + return FALSE; + } + + unsigned long prices[MAX_NUMBER_OF_SUPERVISORS]; + unsigned long weights[MAX_NUMBER_OF_SUPERVISORS]; + for (int i = 0; i < numberOfVotes; i++) { + + int j; + for (j = 0; j < i; j++) { + + if (prices[j] > votes[i].price) { + + break; + } + memmove(&prices[j + 1], &prices[j], (i - j) * sizeof(Vote)); + memmove(&weights[j + 1], &weights[j], (i - j) * sizeof(Vote)); + prices[j] = votes[i].price; + weights[j] = votes[i].weight; + } + } + + unsigned long newPrice = 0; + for (int i = 0; i < numberOfVotes; i++) { + + unsigned long weight = 0; + unsigned long bet = 0; + for (int j = 0; j < numberOfVotes; j++) { + + signed long delta = votes[i].price - votes[j].price; + if (delta < 0) { + + delta = -delta; + } + + if (delta <= votes[j].tolerance) { + + weight += votes[j].weight; + bet += votes[j].bet; + } + } + if (weight > totalWeight / 2) { + + newPrice = votes[i].price; + + unsigned long totalPrize = 0; + for (int j = 0; j < numberOfVotes; j++) { + + signed long delta = votes[i].price - votes[j].price; + if (delta < 0) { + + delta = -delta; + } + + if (delta <= votes[j].tolerance) { + + getSupervisor(votes[j].supervisorId)->rating++; + if (prizes != NULL) { + + prizes[j] = votes[j].bet + (votes[j].bet * (totalBet - bet) / bet); + totalPrize += prizes[j]; + } + + } else { + + if (prizes != NULL) { + + prizes[j] = 0; + } + } + } + + if (prizes != NULL) { + + pools[tokens[token].pool].reserve += totalBet - totalPrize; + } + + break; + } + } + + if (newPrice == 0) { + + return FALSE; + + } else { + + unsigned long lastPrice = getLastPrice(token, time); + if (newPrice < lastPrice) { + + if ((lastPrice - newPrice) * 1000 / lastPrice > tokens[token].decreaseLimits[DAILY]) { + + newPrice = lastPrice - tokens[token].decreaseLimits[DAILY] * lastPrice / 1000; + } + + lastPrice = getLastPrice(token, time - 7L * 24 * 60 * 60 * 1000); + if ((lastPrice - newPrice) * 1000 / lastPrice > tokens[token].decreaseLimits[WEEKLY]) { + + newPrice = lastPrice - tokens[token].decreaseLimits[WEEKLY] * lastPrice / 1000; + } + + lastPrice = getLastPrice(token, time - 30L * 24 * 60 * 60 * 1000); + if ((lastPrice - newPrice) * 1000 / lastPrice > tokens[token].decreaseLimits[MONTHLY]) { + + newPrice = lastPrice - tokens[token].decreaseLimits[MONTHLY] * lastPrice / 1000; + } + + lastPrice = getLastPrice(token, time - 365L * 24 * 60 * 60 * 1000); + if ((lastPrice - newPrice) * 1000 / lastPrice > tokens[token].decreaseLimits[YEARLY]) { + + newPrice = lastPrice - tokens[token].decreaseLimits[YEARLY] * lastPrice / 1000; + } + + } else { + + if ((newPrice - lastPrice) * 1000 / lastPrice > tokens[token].increaseLimits[DAILY]) { + + newPrice = lastPrice + tokens[token].increaseLimits[DAILY] * lastPrice / 1000; + } + + lastPrice = getLastPrice(token, time - 7L * 24 * 60 * 60 * 1000); + if ((newPrice - lastPrice) * 1000 / lastPrice > tokens[token].increaseLimits[WEEKLY]) { + + newPrice = lastPrice + tokens[token].increaseLimits[WEEKLY] * lastPrice / 1000; + } + + lastPrice = getLastPrice(token, time - 30L * 24 * 60 * 60 * 1000); + if ((newPrice - lastPrice) * 1000 / lastPrice > tokens[token].increaseLimits[MONTHLY]) { + + newPrice = lastPrice + tokens[token].increaseLimits[MONTHLY] * lastPrice / 1000; + } + + lastPrice = getLastPrice(token, time - 365L * 24 * 60 * 60 * 1000); + if ((newPrice - lastPrice) * 1000 / lastPrice > tokens[token].increaseLimits[YEARLY]) { + + newPrice = lastPrice + tokens[token].increaseLimits[YEARLY] * lastPrice / 1000; + } + } + + tokens[token].prices[(time - tokens[token].day0Offset) / (24 * 60 * 60 * 1000)] = newPrice; + + return TRUE; + } +} +#endif + +#endif +#endif diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c new file mode 100755 index 000000000..f8875ea8c --- /dev/null +++ b/crypto777/bitcoind_RPC.c @@ -0,0 +1,338 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "OS_portable.h" +#include "../includes/cJSON.h" + +#include +#include + +// return data from the server +struct return_string { + char *ptr; + size_t len; +}; + +size_t accumulate(void *ptr, size_t size, size_t nmemb, struct return_string *s); +void init_string(struct return_string *s); + + +/************************************************************************ + * + * return the current system time in milliseconds + * + ************************************************************************/ + +#define EXTRACT_BITCOIND_RESULT // if defined, ensures error is null and returns the "result" field +#ifdef EXTRACT_BITCOIND_RESULT + +/************************************************************************ + * + * perform post processing of the results + * + ************************************************************************/ + +char *post_process_bitcoind_RPC(char *debugstr,char *command,char *rpcstr,char *params) +{ + long i,j,len; + char *retstr = 0; + cJSON *json,*result,*error; + printf("<<<<<<<<<<< bitcoind_RPC: %s post_process_bitcoind_RPC.%s.[%s]\n",debugstr,command,rpcstr); + if ( command == 0 || rpcstr == 0 || rpcstr[0] == 0 ) + { + printf("<<<<<<<<<<< bitcoind_RPC: %s post_process_bitcoind_RPC.%s.[%s]\n",debugstr,command,rpcstr); + return(rpcstr); + } + json = cJSON_Parse(rpcstr); + if ( json == 0 ) + { + printf("<<<<<<<<<<< bitcoind_RPC: %s post_process_bitcoind_RPC.%s can't parse.(%s) params.(%s)\n",debugstr,command,rpcstr,params); + free(rpcstr); + return(0); + } + result = cJSON_GetObjectItem(json,"result"); + error = cJSON_GetObjectItem(json,"error"); + if ( error != 0 && result != 0 ) + { + if ( (error->type&0xff) == cJSON_NULL && (result->type&0xff) != cJSON_NULL ) + { + retstr = cJSON_Print(result); + len = strlen(retstr); + if ( retstr[0] == '"' && retstr[len-1] == '"' ) + { + for (i=1,j=0; itype&0xff) != cJSON_NULL || (result->type&0xff) != cJSON_NULL ) + printf("<<<<<<<<<<< bitcoind_RPC: %s post_process_bitcoind_RPC (%s) error.%s\n",debugstr,command,rpcstr); + free(rpcstr); + } else retstr = rpcstr; + free_json(json); + //fprintf(stderr,"<<<<<<<<<<< bitcoind_RPC: postprocess returns.(%s)\n",retstr); + return(retstr); +} +#endif + +/************************************************************************ + * + * perform the query + * + ************************************************************************/ + +char *Jay_NXTrequest(char *command,char *params) +{ + char *retstr = 0; + // issue JS Jay request + // wait till it is done + return(retstr); +} + +char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params) +{ + static int count,count2; static double elapsedsum,elapsedsum2; extern int32_t USE_JAY; + struct curl_slist *headers = NULL; struct return_string s; CURLcode res; CURL *curl_handle; + char *bracket0,*bracket1,*databuf = 0; long len; int32_t specialcase,numretries; double starttime; + if ( USE_JAY != 0 && (strncmp(url,"http://127.0.0.1:7876/nxt",strlen("http://127.0.0.1:7876/nxt")) == 0 || strncmp(url,"https://127.0.0.1:7876/nxt",strlen("https://127.0.0.1:7876/nxt")) == 0) ) + { + if ( (databuf= Jay_NXTrequest(command,params)) != 0 ) + return(databuf); + } + numretries = 0; + if ( debugstr != 0 && strcmp(debugstr,"BTCD") == 0 && command != 0 && strcmp(command,"SuperNET") == 0 ) + specialcase = 1; + else specialcase = 0; + if ( url[0] == 0 ) + strcpy(url,"http://127.0.0.1:7876/nxt"); + if ( specialcase != 0 && 0 ) + printf("<<<<<<<<<<< bitcoind_RPC: debug.(%s) url.(%s) command.(%s) params.(%s)\n",debugstr,url,command,params); +try_again: + if ( retstrp != 0 ) + *retstrp = 0; + starttime = OS_milliseconds(); + curl_handle = curl_easy_init(); + init_string(&s); + headers = curl_slist_append(0,"Expect:"); + + curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )"); + curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl_handle,CURLOPT_URL, url); + curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION, (void *)accumulate); // send all data to this function + curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA, &s); // we pass our 's' struct to the callback + curl_easy_setopt(curl_handle,CURLOPT_NOSIGNAL, 1L); // supposed to fix "Alarm clock" and long jump crash + curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS, 1L); // no progress callback + if ( strncmp(url,"https",5) == 0 ) + { + curl_easy_setopt(curl_handle,CURLOPT_SSL_VERIFYPEER,0); + curl_easy_setopt(curl_handle,CURLOPT_SSL_VERIFYHOST,0); + } + if ( userpass != 0 ) + curl_easy_setopt(curl_handle,CURLOPT_USERPWD, userpass); + databuf = 0; + if ( params != 0 ) + { + if ( command != 0 && specialcase == 0 ) + { + len = strlen(params); + if ( len > 0 && params[0] == '[' && params[len-1] == ']' ) { + bracket0 = bracket1 = (char *)""; + } + else + { + bracket0 = (char *)"["; + bracket1 = (char *)"]"; + } + + databuf = (char *)malloc(256 + strlen(command) + strlen(params)); + sprintf(databuf,"{\"id\":\"jl777\",\"method\":\"%s\",\"params\":%s%s%s}",command,bracket0,params,bracket1); + //printf("url.(%s) userpass.(%s) databuf.(%s)\n",url,userpass,databuf); + // + } //else if ( specialcase != 0 ) fprintf(stderr,"databuf.(%s)\n",params); + curl_easy_setopt(curl_handle,CURLOPT_POST,1L); + if ( databuf != 0 ) + curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDS,databuf); + else curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDS,params); + } + //laststart = milliseconds(); + res = curl_easy_perform(curl_handle); + curl_slist_free_all(headers); + curl_easy_cleanup(curl_handle); + if ( databuf != 0 ) // clean up temporary buffer + { + free(databuf); + databuf = 0; + } + if ( res != CURLE_OK ) + { + numretries++; + if ( specialcase != 0 ) + { + printf("<<<<<<<<<<< bitcoind_RPC.(%s): BTCD.%s timeout params.(%s) s.ptr.(%s) err.%d\n",url,command,params,s.ptr,res); + free(s.ptr); + return(0); + } + else if ( numretries >= 2 ) + { + printf("Maximum number of retries exceeded!\n"); + free(s.ptr); + return(0); + } + printf( "curl_easy_perform() failed: %s %s.(%s %s), retries: %d\n",curl_easy_strerror(res),debugstr,url,command,numretries); + free(s.ptr); + sleep((1< (%s)\n",params,s.ptr); + count2++; + elapsedsum2 += (OS_milliseconds() - starttime); + if ( (count2 % 10000) == 0) + printf("%d: ave %9.6f | elapsed %.3f millis | NXT calls.(%s) cmd.(%s)\n",count2,elapsedsum2/count2,(double)(OS_milliseconds() - starttime),url,command); + return(s.ptr); + } + } + printf("bitcoind_RPC: impossible case\n"); + free(s.ptr); + return(0); +} + + + +/************************************************************************ + * + * Initialize the string handler so that it is thread safe + * + ************************************************************************/ + +void init_string(struct return_string *s) +{ + s->len = 0; + s->ptr = (char *)calloc(1,s->len+1); + if ( s->ptr == NULL ) + { + fprintf(stderr,"init_string malloc() failed\n"); + exit(-1); + } + s->ptr[0] = '\0'; +} + +/************************************************************************ + * + * Use the "writer" to accumulate text until done + * + ************************************************************************/ + +size_t accumulate(void *ptr,size_t size,size_t nmemb,struct return_string *s) +{ + size_t new_len = s->len + size*nmemb; + s->ptr = (char *)realloc(s->ptr,new_len+1); + if ( s->ptr == NULL ) + { + fprintf(stderr, "accumulate realloc() failed\n"); + exit(-1); + } + memcpy(s->ptr+s->len,ptr,size*nmemb); + s->ptr[new_len] = '\0'; + s->len = new_len; + return(size * nmemb); +} + +struct MemoryStruct { char *memory; size_t size; }; + +static size_t WriteMemoryCallback(void *ptr,size_t size,size_t nmemb,void *data) +{ + size_t realsize = (size * nmemb); + struct MemoryStruct *mem = (struct MemoryStruct *)data; + mem->memory = (ptr != 0) ? realloc(mem->memory,mem->size + realsize + 1) : malloc(mem->size + realsize + 1); + if ( mem->memory != 0 ) + { + if ( ptr != 0 ) + memcpy(&(mem->memory[mem->size]),ptr,realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + } + return(realsize); +} + +void *curl_post(CURL **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3) +{ + struct MemoryStruct chunk; CURL *cHandle; long code; struct curl_slist *headers = 0; + if ( (cHandle= *cHandlep) == NULL ) + *cHandlep = cHandle = curl_easy_init(); + else curl_easy_reset(cHandle); + //#ifdef DEBUG + //curl_easy_setopt(cHandle,CURLOPT_VERBOSE, 1); + //#endif + curl_easy_setopt(cHandle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )"); + curl_easy_setopt(cHandle,CURLOPT_SSL_VERIFYPEER,0); + //curl_easy_setopt(cHandle,CURLOPT_SSLVERSION,1); + curl_easy_setopt(cHandle,CURLOPT_URL,url); + curl_easy_setopt(cHandle,CURLOPT_CONNECTTIMEOUT,10); + if ( userpass != 0 && userpass[0] != 0 ) + curl_easy_setopt(cHandle,CURLOPT_USERPWD,userpass); + if ( postfields != 0 && postfields[0] != 0 ) + { + curl_easy_setopt(cHandle,CURLOPT_POST,1); + curl_easy_setopt(cHandle,CURLOPT_POSTFIELDS,postfields); + } + if ( hdr0 != NULL && hdr0[0] != 0 ) + { + //printf("HDR0.(%s) HDR1.(%s) HDR2.(%s) HDR3.(%s)\n",hdr0!=0?hdr0:"",hdr1!=0?hdr1:"",hdr2!=0?hdr2:"",hdr3!=0?hdr3:""); + headers = curl_slist_append(headers,hdr0); + if ( hdr1 != 0 && hdr1[0] != 0 ) + headers = curl_slist_append(headers,hdr1); + if ( hdr2 != 0 && hdr2[0] != 0 ) + headers = curl_slist_append(headers,hdr2); + if ( hdr3 != 0 && hdr3[0] != 0 ) + headers = curl_slist_append(headers,hdr3); + } //headers = curl_slist_append(0,"Expect:"); + if ( headers != 0 ) + curl_easy_setopt(cHandle,CURLOPT_HTTPHEADER,headers); + //res = curl_easy_perform(cHandle); + memset(&chunk,0,sizeof(chunk)); + curl_easy_setopt(cHandle,CURLOPT_WRITEFUNCTION,WriteMemoryCallback); + curl_easy_setopt(cHandle,CURLOPT_WRITEDATA,(void *)&chunk); + curl_easy_perform(cHandle); + curl_easy_getinfo(cHandle,CURLINFO_RESPONSE_CODE,&code); + if ( code != 200 ) + printf("error: (%s) server responded with code %ld\n",url,code); + if ( headers != 0 ) + curl_slist_free_all(headers); + return(chunk.memory); +} + +void curlhandle_free(void *curlhandle) +{ + curl_easy_cleanup(curlhandle); +} diff --git a/crypto777/cJSON.c b/crypto777/cJSON.c new file mode 100755 index 000000000..7e059633f --- /dev/null +++ b/crypto777/cJSON.c @@ -0,0 +1,1054 @@ + +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +/* cJSON */ +/* JSON parser in C. */ +#include + +#include "../includes/cJSON.h" +//#define DEFINES_ONLY +//#include "../common/system777.c" +//#undef DEFINES_ONLY + +static const char *ep; + +long stripquotes(char *str) +{ + long len,offset; + if ( str == 0 ) + return(0); + len = strlen(str); + if ( str[0] == '"' && str[len-1] == '"' ) + str[len-1] = 0, offset = 1; + else offset = 0; + return(offset); +} + +const char *cJSON_GetErrorPtr(void) {return ep;} + +static int32_t cJSON_strcasecmp(const char *s1,const char *s2) +{ + if (!s1) return (s1==s2)?0:1;if (!s2) return 1; + for(; tolower((int32_t)(*s1)) == tolower((int32_t)(*s2)); ++s1, ++s2) if(*s1 == 0) return 0; + return tolower((int32_t)(*(const unsigned char *)s1)) - tolower((int32_t)(*(const unsigned char *)s2)); +} + +static void *(*cJSON_malloc)(size_t sz) = malloc; +static void (*cJSON_free)(void *ptr) = free; + +static char* cJSON_strdup(const char* str) +{ + size_t len; + char* copy; + + len = strlen(str) + 1; + if (!(copy = (char*)cJSON_malloc(len))) return 0; + memcpy(copy,str,len); + return copy; +} + +void cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (!hooks) { /* Reset hooks */ + cJSON_malloc = malloc; + cJSON_free = free; + return; + } + + cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc; + cJSON_free = (hooks->free_fn)?hooks->free_fn:free; +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(void) +{ + cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON)); + if (node) memset(node,0,sizeof(cJSON)); + return node; +} + +/* Delete a cJSON structure. */ +void cJSON_Delete(cJSON *c) +{ + cJSON *next; + while (c) + { + next=c->next; + if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child); + if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring); + if (c->string) cJSON_free(c->string); + cJSON_free(c); + c=next; + } +} + +/* Parse the input text to generate a number, and populate the result into item. */ +static const char *parse_number(cJSON *item,const char *num) +{ + double n=0,sign=1,scale=0;int32_t subscale=0,signsubscale=1; + + if (*num=='-') sign=-1,num++; /* Has sign? */ + if (*num=='0') num++; /* is zero */ + if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */ + if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */ + if (*num=='e' || *num=='E') /* Exponent? */ + { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */ + while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */ + } + + n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */ + + item->valuedouble=n; + item->valueint=(int64_t)n; + item->type=cJSON_Number; + return num; +} + +/* Render the number nicely from the given item into a string. */ +static char *print_number(cJSON *item) +{ + char *str; + double d = item->valuedouble; + if ( fabs(((double)item->valueint) - d) <= DBL_EPSILON && d >= (1. - DBL_EPSILON) && d < (1LL << 62) )//d <= INT_MAX && d >= INT_MIN ) + { + str = (char *)cJSON_malloc(24); /* 2^64+1 can be represented in 21 chars + sign. */ + if ( str != 0 ) + sprintf(str,"%lld",(long long)item->valueint); + } + else + { + str = (char *)cJSON_malloc(64); /* This is a nice tradeoff. */ + if ( str != 0 ) + { + if ( fabs(floor(d) - d) <= DBL_EPSILON && fabs(d) < 1.0e60 ) + sprintf(str,"%.0f",d); + //else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d); + else + sprintf(str,"%.8f",d); + } + } + return str; +} + +static unsigned parse_hex4(const char *str) +{ + unsigned h=0; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + return h; +} + +/* Parse the input text into an unescaped cstring, and populate item. */ +static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; +static const char *parse_string(cJSON *item,const char *str) +{ + const char *ptr=str+1;char *ptr2;char *out;int32_t len=0;unsigned uc,uc2; + if (*str!='\"') {ep=str;return 0;} /* not a string! */ + + while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ + + out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */ + if (!out) return 0; + + ptr=str+1;ptr2=out; + while (*ptr!='\"' && *ptr) + { + if (*ptr!='\\') *ptr2++=*ptr++; + else + { + ptr++; + switch (*ptr) + { + case 'b': *ptr2++='\b'; break; + case 'f': *ptr2++='\f'; break; + case 'n': *ptr2++='\n'; break; + case 'r': *ptr2++='\r'; break; + case 't': *ptr2++='\t'; break; + case 'u': /* transcode utf16 to utf8. */ + uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */ + + if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */ + + if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */ + { + if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */ + uc2=parse_hex4(ptr+3);ptr+=6; + if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */ + uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF)); + } + + len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len; + + switch (len) { + case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 1: *--ptr2 =(uc | firstByteMark[len]); + } + ptr2+=len; + break; + default: *ptr2++=*ptr; break; + } + ptr++; + } + } + *ptr2=0; + if (*ptr=='\"') ptr++; + item->valuestring=out; + item->type=cJSON_String; + return ptr; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static char *print_string_ptr(const char *str) +{ + const char *ptr;char *ptr2,*out;int32_t len=0;unsigned char token; + + if (!str) return cJSON_strdup(""); + ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;} + + out=(char*)cJSON_malloc(len+3); + if (!out) return 0; + + ptr2=out;ptr=str; + *ptr2++='\"'; + while (*ptr) + { + if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++; + else + { + *ptr2++='\\'; + switch (token=*ptr++) + { + case '\\': *ptr2++='\\'; break; + case '\"': *ptr2++='\"'; break; + case '\b': *ptr2++='b'; break; + case '\f': *ptr2++='f'; break; + case '\n': *ptr2++='n'; break; + case '\r': *ptr2++='r'; break; + case '\t': *ptr2++='t'; break; + default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */ + } + } + } + *ptr2++='\"';*ptr2++=0; + return out; +} +/* Invote print_string_ptr (which is useful) on an item. */ +static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);} + +/* Predeclare these prototypes. */ +static const char *parse_value(cJSON *item,const char *value); +static char *print_value(cJSON *item,int32_t depth,int32_t fmt); +static const char *parse_array(cJSON *item,const char *value); +static char *print_array(cJSON *item,int32_t depth,int32_t fmt); +static const char *parse_object(cJSON *item,const char *value); +static char *print_object(cJSON *item,int32_t depth,int32_t fmt); + +/* Utility to jump whitespace and cr/lf */ +static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} + +/* Parse an object - create a new root, and populate. */ +cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int32_t require_null_terminated) +{ + const char *end=0; + cJSON *c=cJSON_New_Item(); + ep=0; + if (!c) return 0; /* memory fail */ + + end=parse_value(c,skip(value)); + if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */ + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}} + if (return_parse_end) *return_parse_end=end; + return c; +} +/* Default options for cJSON_Parse */ +cJSON *cJSON_Parse(const char *value) +{ + return(cJSON_ParseWithOpts(value,0,0)); +} + +/* Render a cJSON item/entity/structure to text. */ +char *cJSON_Print(cJSON *item) +{ + return(print_value(item,0,1)); +} +char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);} + +/* Parser core - when encountering text, process appropriately. */ +static const char *parse_value(cJSON *item,const char *value) +{ + if (!value) return 0; /* Fail on null. */ + if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; } + if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; } + if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; } + if (*value=='\"') { return parse_string(item,value); } + if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); } + if (*value=='[') { return parse_array(item,value); } + if (*value=='{') { return parse_object(item,value); } + + ep=value;return 0; /* failure. */ +} + +/* Render a value to text. */ +static char *print_value(cJSON *item,int32_t depth,int32_t fmt) +{ + char *out=0; + if (!item) return 0; + switch ((item->type)&255) + { + case cJSON_NULL: out=cJSON_strdup("null"); break; + case cJSON_False: out=cJSON_strdup("false");break; + case cJSON_True: out=cJSON_strdup("true"); break; + case cJSON_Number: out=print_number(item);break; + case cJSON_String: out=print_string(item);break; + case cJSON_Array: out=print_array(item,depth,fmt);break; + case cJSON_Object: out=print_object(item,depth,fmt);break; + } + return out; +} + +/* Build an array from input text. */ +static const char *parse_array(cJSON *item,const char *value) +{ + cJSON *child; + if (*value!='[') {ep=value;return 0;} /* not an array! */ + + item->type=cJSON_Array; + value=skip(value+1); + if (*value==']') return value+1; /* empty array. */ + + item->child=child=cJSON_New_Item(); + if (!item->child) return 0; /* memory fail */ + value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */ + if (!value) return 0; + + while (*value==',') + { + cJSON *new_item; + if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ + child->next=new_item;new_item->prev=child;child=new_item; + value=skip(parse_value(child,skip(value+1))); + if (!value) return 0; /* memory fail */ + } + + if (*value==']') return value+1; /* end of array */ + ep=value;return 0; /* malformed. */ +} + +/* Render an array to text */ +static char *print_array(cJSON *item,int32_t depth,int32_t fmt) +{ + char **entries; + char *out=0,*ptr,*ret;int32_t len=5; + cJSON *child=item->child; + int32_t numentries=0,i=0,fail=0; + + /* How many entries in the array? */ + while (child) numentries++,child=child->next; + /* Explicitly handle numentries==0 */ + if (!numentries) + { + out=(char*)cJSON_malloc(3); + if (out) strcpy(out,"[]"); + return out; + } + /* Allocate an array to hold the values for each */ + entries=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!entries) return 0; + memset(entries,0,numentries*sizeof(char*)); + /* Retrieve all the results: */ + child=item->child; + while (child && !fail) + { + ret=print_value(child,depth+1,fmt); + entries[i++]=ret; + if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; + child=child->next; + } + + /* If we didn't fail, try to malloc the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + /* If that fails, we fail. */ + if (!out) fail=1; + + /* Handle failure. */ + if (fail) + { + for (i=0;itype=cJSON_Object; + value=skip(value+1); + if (*value=='}') return value+1; /* empty array. */ + + item->child=child=cJSON_New_Item(); + if (!item->child) return 0; + value=skip(parse_string(child,skip(value))); + if (!value) return 0; + child->string=child->valuestring;child->valuestring=0; + if (*value!=':') {ep=value;return 0;} /* fail! */ + value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ + if (!value) return 0; + + while (*value==',') + { + cJSON *new_item; + if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ + child->next=new_item;new_item->prev=child;child=new_item; + value=skip(parse_string(child,skip(value+1))); + if (!value) return 0; + child->string=child->valuestring;child->valuestring=0; + if (*value!=':') {ep=value;return 0;} /* fail! */ + value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ + if (!value) return 0; + } + + if (*value=='}') return value+1; /* end of array */ + ep=value;return 0; /* malformed. */ +} + +/* Render an object to text. */ +static char *print_object(cJSON *item,int32_t depth,int32_t fmt) +{ + char **entries=0,**names=0; + char *out=0,*ptr,*ret,*str;int32_t len=7,i=0,j; + cJSON *child=item->child; + int32_t numentries=0,fail=0; + /* Count the number of entries. */ + while (child) numentries++,child=child->next; + /* Explicitly handle empty object case */ + if (!numentries) + { + out=(char*)cJSON_malloc(fmt?depth+4:3); + if (!out) return 0; + ptr=out;*ptr++='{'; + if (fmt) {*ptr++='\n';for (i=0;ichild;depth++;if (fmt) len+=depth; + while (child) + { + names[i]=str=print_string_ptr(child->string); + entries[i++]=ret=print_value(child,depth,fmt); + if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1; + child=child->next; + } + + /* Try to allocate the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + if (!out) fail=1; + + /* Handle failure */ + if (fail) + { + for (i=0;ichild;int32_t i=0;while(c)i++,c=c->next;return i;} +cJSON *cJSON_GetArrayItem(cJSON *array,int32_t item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;} +cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;} +/* Utility for handling references. */ +static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;} + +/* Add item to array/object. */ +void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}} +void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);} +void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));} +void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));} + +cJSON *cJSON_DetachItemFromArray(cJSON *array,int32_t which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0; + if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;} +void cJSON_DeleteItemFromArray(cJSON *array,int32_t which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));} +cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int32_t i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;} +void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));} + +/* Replace array/object items with new ones. */ +void cJSON_ReplaceItemInArray(cJSON *array,int32_t which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return; + newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem; + if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);} +void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int32_t i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}} + +/* Create basic types: */ +cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;} +cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;} +cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;} +cJSON *cJSON_CreateBool(int32_t b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;} +cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int64_t)num;}return item;} +cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;} +cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;} +cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;} + +/* Create Arrays: */ +cJSON *cJSON_CreateIntArray(int64_t *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateFloatArray(float *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateDoubleArray(double *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateStringArray(char **strings,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} + +/* Duplication */ +cJSON *cJSON_Duplicate(cJSON *item,int32_t recurse) +{ + cJSON *newitem,*cptr,*nptr=0,*newchild; + /* Bail on bad ptr */ + if (!item) return 0; + /* Create new item */ + newitem=cJSON_New_Item(); + if (!newitem) return 0; + /* Copy over all vars */ + newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble; + if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}} + if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}} + /* If non-recursive, then we're done! */ + if (!recurse) return newitem; + /* Walk the ->next chain for the child. */ + cptr=item->child; + while (cptr) + { + newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) {cJSON_Delete(newitem);return 0;} + if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */ + cptr=cptr->next; + } + return newitem; +} + +void cJSON_Minify(char *json) +{ + char *into=json; + while (*json) + { + if (*json==' ') json++; + else if (*json=='\t') json++; // Whitespace characters. + else if (*json=='\r') json++; + else if (*json=='\n') json++; + else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line. + else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments. + else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are \" sensitive. + else *into++=*json++; // All other characters. + } + *into=0; // and null-terminate. +} + +// the following written by jl777 +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +void copy_cJSON(struct destbuf *dest,cJSON *obj) +{ + char *str; + int i; + long offset; + dest->buf[0] = 0; + if ( obj != 0 ) + { + str = cJSON_Print(obj); + if ( str != 0 ) + { + offset = stripquotes(str); + //strcpy(dest,str+offset); + for (i=0; ibuf[i]= str[offset+i]) == 0 ) + break; + dest->buf[i] = 0; + free(str); + } + } +} + +void copy_cJSON2(char *dest,int32_t maxlen,cJSON *obj) +{ + struct destbuf tmp; + maxlen--; + dest[0] = 0; + if ( maxlen > sizeof(tmp.buf) ) + maxlen = sizeof(tmp.buf); + copy_cJSON(&tmp,obj); + if ( strlen(tmp.buf) < maxlen ) + strcpy(dest,tmp.buf); + else dest[0] = 0; +} + +int64_t _get_cJSON_int(cJSON *json) +{ + struct destbuf tmp; + if ( json != 0 ) + { + copy_cJSON(&tmp,json); + if ( tmp.buf[0] != 0 ) + return(calc_nxt64bits(tmp.buf)); + } + return(0); +} + +int64_t get_cJSON_int(cJSON *json,char *field) +{ + cJSON *numjson; + if ( json != 0 ) + { + numjson = cJSON_GetObjectItem(json,field); + if ( numjson != 0 ) + return(_get_cJSON_int(numjson)); + } + return(0); +} + +int64_t _conv_cJSON_float(cJSON *json) +{ + int64_t conv_floatstr(char *); + struct destbuf tmp; + if ( json != 0 ) + { + copy_cJSON(&tmp,json); + return(conv_floatstr(tmp.buf)); + } + return(0); +} + +int64_t conv_cJSON_float(cJSON *json,char *field) +{ + if ( json != 0 ) + return(_conv_cJSON_float(cJSON_GetObjectItem(json,field))); + return(0); +} + +int32_t extract_cJSON_str(char *dest,int32_t max,cJSON *json,char *field) +{ + int32_t safecopy(char *dest,char *src,long len); + char *str; + cJSON *obj; + int32_t len; + long offset; + dest[0] = 0; + obj = cJSON_GetObjectItem(json,field); + if ( obj != 0 ) + { + str = cJSON_Print(obj); + offset = stripquotes(str); + len = safecopy(dest,str+offset,max); + free(str); + return(len); + } + return(0); +} + +cJSON *gen_list_json(char **list) +{ + cJSON *array,*item; + array = cJSON_CreateArray(); + while ( list != 0 && *list != 0 && *list[0] != 0 ) + { + item = cJSON_CreateString(*list++); + cJSON_AddItemToArray(array,item); + } + return(array); +} + +uint64_t get_API_nxt64bits(cJSON *obj) +{ + uint64_t nxt64bits = 0; + struct destbuf tmp; + if ( obj != 0 ) + { + if ( is_cJSON_Number(obj) != 0 ) + return((uint64_t)obj->valuedouble); + copy_cJSON(&tmp,obj); + nxt64bits = calc_nxt64bits(tmp.buf); + } + return(nxt64bits); +} +uint64_t j64bits(cJSON *json,char *field) { if ( field == 0 ) return(get_API_nxt64bits(json)); return(get_API_nxt64bits(cJSON_GetObjectItem(json,field))); } +uint64_t j64bitsi(cJSON *json,int32_t i) { return(get_API_nxt64bits(cJSON_GetArrayItem(json,i))); } + +uint64_t get_satoshi_obj(cJSON *json,char *field) +{ + int32_t i,n; + uint64_t prev,satoshis,mult = 1; + struct destbuf numstr,checkstr; + cJSON *numjson; + numjson = cJSON_GetObjectItem(json,field); + copy_cJSON(&numstr,numjson); + satoshis = prev = 0; mult = 1; n = (int32_t)strlen(numstr.buf); + for (i=n-1; i>=0; i--,mult*=10) + { + satoshis += (mult * (numstr.buf[i] - '0')); + if ( satoshis < prev ) + printf("get_satoshi_obj numstr.(%s) i.%d prev.%llu vs satoshis.%llu\n",numstr.buf,i,(unsigned long long)prev,(unsigned long long)satoshis); + prev = satoshis; + } + sprintf(checkstr.buf,"%llu",(long long)satoshis); + if ( strcmp(checkstr.buf,numstr.buf) != 0 ) + { + printf("SATOSHI GREMLIN?? numstr.(%s) -> %.8f -> (%s)\n",numstr.buf,dstr(satoshis),checkstr.buf); + } + return(satoshis); +} + +void add_satoshis_json(cJSON *json,char *field,uint64_t satoshis) +{ + cJSON *obj; + char numstr[64]; + sprintf(numstr,"%lld",(long long)satoshis); + obj = cJSON_CreateString(numstr); + cJSON_AddItemToObject(json,field,obj); + if ( satoshis != get_satoshi_obj(json,field) ) + printf("error adding satoshi obj %ld -> %ld\n",(unsigned long)satoshis,(unsigned long)get_satoshi_obj(json,field)); +} + +char *cJSON_str(cJSON *json) +{ + if ( json != 0 && is_cJSON_String(json) != 0 ) + return(json->valuestring); + return(0); +} + +void jadd(cJSON *json,char *field,cJSON *item) { cJSON_AddItemToObject(json,field,item); } +void jaddstr(cJSON *json,char *field,char *str) { cJSON_AddItemToObject(json,field,cJSON_CreateString(str)); } +void jaddnum(cJSON *json,char *field,double num) { cJSON_AddItemToObject(json,field,cJSON_CreateNumber(num)); } +void jadd64bits(cJSON *json,char *field,uint64_t nxt64bits) { char numstr[64]; sprintf(numstr,"%llu",(long long)nxt64bits), jaddstr(json,field,numstr); } +void jaddi(cJSON *json,cJSON *item) { cJSON_AddItemToArray(json,item); } +void jaddistr(cJSON *json,char *str) { cJSON_AddItemToArray(json,cJSON_CreateString(str)); } +void jaddinum(cJSON *json,double num) { cJSON_AddItemToArray(json,cJSON_CreateNumber(num)); } +void jaddi64bits(cJSON *json,uint64_t nxt64bits) { char numstr[64]; sprintf(numstr,"%llu",(long long)nxt64bits), jaddistr(json,numstr); } +char *jstr(cJSON *json,char *field) { if ( field == 0 ) return(cJSON_str(json)); return(cJSON_str(cJSON_GetObjectItem(json,field))); } + +char *jstri(cJSON *json,int32_t i) { return(cJSON_str(cJSON_GetArrayItem(json,i))); } +char *jprint(cJSON *json,int32_t freeflag) { char *str; if ( json == 0 ) return(clonestr("{}")); str = cJSON_Print(json), _stripwhite(str,' '); if ( freeflag != 0 ) free_json(json); return(str); } + +char *get_cJSON_fieldname(cJSON *obj) +{ + if ( obj != 0 ) + { + if ( obj->child != 0 && obj->child->string != 0 ) + return(obj->child->string); + else if ( obj->string != 0 ) + return(obj->string); + } + return((char *)""); +} + +void ensure_jsonitem(cJSON *json,char *field,char *value) +{ + cJSON *obj = cJSON_GetObjectItem(json,field); + if ( obj == 0 ) + cJSON_AddItemToObject(json,field,cJSON_CreateString(value)); + else cJSON_ReplaceItemInObject(json,field,cJSON_CreateString(value)); +} + +int32_t in_jsonarray(cJSON *array,char *value) +{ + int32_t i,n; + struct destbuf remote; + if ( array != 0 && is_cJSON_Array(array) != 0 ) + { + n = cJSON_GetArraySize(array); + for (i=0; i= range ) + x = (range - 1); + return((int32_t)x); +} + +int32_t get_API_int(cJSON *obj,int32_t val) +{ + struct destbuf buf; + if ( obj != 0 ) + { + if ( is_cJSON_Number(obj) != 0 ) + return((int32_t)obj->valuedouble); + copy_cJSON(&buf,obj); + val = myatoi(buf.buf,0); + if ( val < 0 ) + val = 0; + } + return(val); +} +int32_t jint(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(get_API_int(json,0)); return(get_API_int(cJSON_GetObjectItem(json,field),0)); } +int32_t jinti(cJSON *json,int32_t i) { if ( json == 0 ) return(0); return(get_API_int(cJSON_GetArrayItem(json,i),0)); } + +uint32_t get_API_uint(cJSON *obj,uint32_t val) +{ + struct destbuf buf; + if ( obj != 0 ) + { + if ( is_cJSON_Number(obj) != 0 ) + return((uint32_t)obj->valuedouble); + copy_cJSON(&buf,obj); + val = myatoi(buf.buf,0); + } + return(val); +} +uint32_t juint(cJSON *json,char *field) { if ( json == 0 ) return(0); if ( field == 0 ) return(get_API_uint(json,0)); return(get_API_uint(cJSON_GetObjectItem(json,field),0)); } +uint32_t juinti(cJSON *json,int32_t i) { if ( json == 0 ) return(0); return(get_API_uint(cJSON_GetArrayItem(json,i),0)); } + +double get_API_float(cJSON *obj) +{ + double val = 0.; + struct destbuf buf; + if ( obj != 0 ) + { + if ( is_cJSON_Number(obj) != 0 ) + return(obj->valuedouble); + copy_cJSON(&buf,obj); + val = atof(buf.buf); + } + return(val); +} +double jdouble(cJSON *json,char *field) +{ + if ( json != 0 ) + { + if ( field == 0 ) + return(get_API_float(json)); + else return(get_API_float(cJSON_GetObjectItem(json,field))); + } else return(0.); +} + +double jdoublei(cJSON *json,int32_t i) +{ + if ( json != 0 ) + return(get_API_float(cJSON_GetArrayItem(json,i))); + else return(0.); +} + +cJSON *jobj(cJSON *json,char *field) { if ( json != 0 ) return(cJSON_GetObjectItem(json,field)); return(0); } + +void jdelete(cJSON *json,char *field) +{ + if ( jobj(json,field) != 0 ) + cJSON_DeleteItemFromObject(json,field); +} + +cJSON *jduplicate(cJSON *json) { return(cJSON_Duplicate(json,1)); } + +cJSON *jitem(cJSON *array,int32_t i) { if ( array != 0 && is_cJSON_Array(array) != 0 && cJSON_GetArraySize(array) > i ) return(cJSON_GetArrayItem(array,i)); return(0); } +cJSON *jarray(int32_t *nump,cJSON *json,char *field) +{ + cJSON *array; + if ( json != 0 ) + { + if ( field == 0 ) + array = json; + else array = cJSON_GetObjectItem(json,field); + if ( array != 0 && is_cJSON_Array(array) != 0 && (*nump= cJSON_GetArraySize(array)) > 0 ) + return(array); + } + *nump = 0; + return(0); +} + +int32_t expand_nxt64bits(char *NXTaddr,uint64_t nxt64bits) +{ + int32_t i,n; + uint64_t modval; + char rev[64]; + for (i=0; nxt64bits!=0; i++) + { + modval = nxt64bits % 10; + rev[i] = (char)(modval + '0'); + nxt64bits /= 10; + } + n = i; + for (i=0; i= 22 ) + { + printf("calc_nxt64bits: illegal NXTaddr.(%s) too long\n",NXTaddr); + return(0); + } + else if ( strcmp(NXTaddr,"0") == 0 || strcmp(NXTaddr,"false") == 0 ) + { + // printf("zero address?\n"); getchar(); + return(0); + } + if ( NXTaddr[0] == '-' ) + polarity = -1, NXTaddr++, n--; + mult = 1; + lastval = 0; + for (i=n-1; i>=0; i--,mult*=10) + { + c = NXTaddr[i]; + if ( c < '0' || c > '9' ) + { + printf("calc_nxt64bits: illegal char.(%c %d) in (%s).%d\n",c,c,NXTaddr,(int32_t)i); +#ifdef __APPLE__ + while ( 1 ) + { + sleep(60); + printf("calc_nxt64bits: illegal char.(%c %d) in (%s).%d\n",c,c,NXTaddr,(int32_t)i); + } +#endif + return(0); + } + nxt64bits += mult * (c - '0'); + if ( nxt64bits < lastval ) + printf("calc_nxt64bits: warning: 64bit overflow %llx < %llx\n",(long long)nxt64bits,(long long)lastval); + lastval = nxt64bits; + } + if ( cmp_nxt64bits(NXTaddr,nxt64bits) != 0 ) + printf("error calculating nxt64bits: %s -> %llx -> %s\n",NXTaddr,(long long)nxt64bits,nxt64str(nxt64bits)); + if ( polarity < 0 ) + return(-(int64_t)nxt64bits); + return(nxt64bits); +} + +cJSON *addrs_jsonarray(uint64_t *addrs,int32_t num) +{ + int32_t j; cJSON *array; + array = cJSON_CreateArray(); + for (j=0; j + * + * Derived from public domain C code by Daniel J. Bernstein + * + * More information about curve25519 can be found here + * http://cr.yp.to/ecdh.html + * + * djb's sample implementation of curve25519 is written in a special assembly + * language called qhasm and uses the floating point registers. + * + * This is, almost, a clean room reimplementation from the curve25519 paper. It + * uses many of the tricks described therein. Only the crecip function is taken + * from the sample implementation. */ + +#include +#include +#include "../includes/curve25519.h" + +#ifdef _MSC_VER +#define inline __inline +#endif + +typedef uint8_t u8; +typedef int32_t s32; +typedef int64_t limb; + +/* Field element representation: + * + * Field elements are written as an array of signed, 64-bit limbs, least + * significant first. The value of the field element is: + * x[0] + 2^26·x[1] + x^51·x[2] + 2^102·x[3] + ... + * + * i.e. the limbs are 26, 25, 26, 25, ... bits wide. */ + +/* Sum two numbers: output += in */ +static void fsum(limb *output, const limb *in) { + unsigned i; + for (i = 0; i < 10; i += 2) { + output[0+i] = output[0+i] + in[0+i]; + output[1+i] = output[1+i] + in[1+i]; + } +} + +/* Find the difference of two numbers: output = in - output + * (note the order of the arguments!). */ +void fdifference_backwards(limb *output, const limb *in) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] - output[i]; + } +} + +/* Multiply a number by a scalar: output = in * scalar */ +static void fscalar_product(limb *output, const limb *in, const limb scalar) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] * scalar; + } +} + +/* Multiply two numbers: output = in2 * in + * + * output must be distinct to both inputs. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fproduct(limb *output, const limb *in2, const limb *in) { + output[0] = ((limb) ((s32) in2[0])) * ((s32) in[0]); + output[1] = ((limb) ((s32) in2[0])) * ((s32) in[1]) + + ((limb) ((s32) in2[1])) * ((s32) in[0]); + output[2] = 2 * ((limb) ((s32) in2[1])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[0]); + output[3] = ((limb) ((s32) in2[1])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[0]); + output[4] = ((limb) ((s32) in2[2])) * ((s32) in[2]) + + 2 * (((limb) ((s32) in2[1])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[1])) + + ((limb) ((s32) in2[0])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[0]); + output[5] = ((limb) ((s32) in2[2])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[0]); + output[6] = 2 * (((limb) ((s32) in2[3])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[0]); + output[7] = ((limb) ((s32) in2[3])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[0]); + output[8] = ((limb) ((s32) in2[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in2[3])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[0]); + output[9] = ((limb) ((s32) in2[4])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[0]); + output[10] = 2 * (((limb) ((s32) in2[5])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[1])) + + ((limb) ((s32) in2[4])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[4]) + + ((limb) ((s32) in2[2])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[2]); + output[11] = ((limb) ((s32) in2[5])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[2]); + output[12] = ((limb) ((s32) in2[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in2[5])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[3])) + + ((limb) ((s32) in2[4])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[4]); + output[13] = ((limb) ((s32) in2[6])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[6]) + + ((limb) ((s32) in2[5])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[4]); + output[14] = 2 * (((limb) ((s32) in2[7])) * ((s32) in[7]) + + ((limb) ((s32) in2[5])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[5])) + + ((limb) ((s32) in2[6])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[6]); + output[15] = ((limb) ((s32) in2[7])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[7]) + + ((limb) ((s32) in2[6])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[6]); + output[16] = ((limb) ((s32) in2[8])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in2[7])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[7])); + output[17] = ((limb) ((s32) in2[8])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[8]); + output[18] = 2 * ((limb) ((s32) in2[9])) * ((s32) in[9]); +} + +/* Reduce a long form to a short form by taking the input mod 2^255 - 19. + * + * On entry: |output[i]| < 14*2^54 + * On exit: |output[0..8]| < 280*2^54 */ +static void freduce_degree(limb *output) { + /* Each of these shifts and adds ends up multiplying the value by 19. + * + * For output[0..8], the absolute entry value is < 14*2^54 and we add, at + * most, 19*14*2^54 thus, on exit, |output[0..8]| < 280*2^54. */ + output[8] += output[18] << 4; + output[8] += output[18] << 1; + output[8] += output[18]; + output[7] += output[17] << 4; + output[7] += output[17] << 1; + output[7] += output[17]; + output[6] += output[16] << 4; + output[6] += output[16] << 1; + output[6] += output[16]; + output[5] += output[15] << 4; + output[5] += output[15] << 1; + output[5] += output[15]; + output[4] += output[14] << 4; + output[4] += output[14] << 1; + output[4] += output[14]; + output[3] += output[13] << 4; + output[3] += output[13] << 1; + output[3] += output[13]; + output[2] += output[12] << 4; + output[2] += output[12] << 1; + output[2] += output[12]; + output[1] += output[11] << 4; + output[1] += output[11] << 1; + output[1] += output[11]; + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; +} + +#if (-1 & 3) != 3 +#error "This code only works on a two's complement system" +#endif + +/* return v / 2^26, using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_26(const limb v) +{ + /* High word of v; no shift needed. */ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x3ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 6; + /* Should return v / (1<<26) */ + return (v + roundoff) >> 26; +} + +/* return v / (2^25), using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_25(const limb v) +{ + /* High word of v; no shift needed*/ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x1ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 7; + /* Should return v / (1<<25) */ + return (v + roundoff) >> 25; +} + +/* Reduce all coefficients of the short form input so that |x| < 2^26. + * + * On entry: |output[i]| < 280*2^54 */ +static void freduce_coefficients(limb *output) { + unsigned i; + + output[10] = 0; + + for (i = 0; i < 10; i += 2) { + limb over = div_by_2_26(output[i]); + /* The entry condition (that |output[i]| < 280*2^54) means that over is, at + * most, 280*2^28 in the first iteration of this loop. This is added to the + * next limb and we can approximate the resulting bound of that limb by + * 281*2^54. */ + output[i] -= over << 26; + output[i+1] += over; + + /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| < + * 281*2^29. When this is added to the next limb, the resulting bound can + * be approximated as 281*2^54. + * + * For subsequent iterations of the loop, 281*2^54 remains a conservative + * bound and no overflow occurs. */ + over = div_by_2_25(output[i+1]); + output[i+1] -= over << 25; + output[i+2] += over; + } + /* Now |output[10]| < 281*2^29 and all other coefficients are reduced. */ + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; + + output[10] = 0; + + /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29 + * So |over| will be no more than 2^16. */ + { + limb over = div_by_2_26(output[0]); + output[0] -= over << 26; + output[1] += over; + } + + /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The + * bound on |output[1]| is sufficient to meet our needs. */ +} + +/* A helpful wrapper around fproduct: output = in * in2. + * + * On entry: |in[i]| < 2^27 and |in2[i]| < 2^27. + * + * output must be distinct to both inputs. The output is reduced degree + * (indeed, one need only provide storage for 10 limbs) and |output[i]| < 2^26. */ +static void fmul32(limb *output, const limb *in, const limb *in2) +{ + limb t[19]; + fproduct(t, in, in2); + /* |t[i]| < 14*2^54 */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +/* Square a number: output = in**2 + * + * output must be distinct from the input. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fsquare_inner(limb *output, const limb *in) { + output[0] = ((limb) ((s32) in[0])) * ((s32) in[0]); + output[1] = 2 * ((limb) ((s32) in[0])) * ((s32) in[1]); + output[2] = 2 * (((limb) ((s32) in[1])) * ((s32) in[1]) + + ((limb) ((s32) in[0])) * ((s32) in[2])); + output[3] = 2 * (((limb) ((s32) in[1])) * ((s32) in[2]) + + ((limb) ((s32) in[0])) * ((s32) in[3])); + output[4] = ((limb) ((s32) in[2])) * ((s32) in[2]) + + 4 * ((limb) ((s32) in[1])) * ((s32) in[3]) + + 2 * ((limb) ((s32) in[0])) * ((s32) in[4]); + output[5] = 2 * (((limb) ((s32) in[2])) * ((s32) in[3]) + + ((limb) ((s32) in[1])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[5])); + output[6] = 2 * (((limb) ((s32) in[3])) * ((s32) in[3]) + + ((limb) ((s32) in[2])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[6]) + + 2 * ((limb) ((s32) in[1])) * ((s32) in[5])); + output[7] = 2 * (((limb) ((s32) in[3])) * ((s32) in[4]) + + ((limb) ((s32) in[2])) * ((s32) in[5]) + + ((limb) ((s32) in[1])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[7])); + output[8] = ((limb) ((s32) in[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in[2])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[1])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[5]))); + output[9] = 2 * (((limb) ((s32) in[4])) * ((s32) in[5]) + + ((limb) ((s32) in[3])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[8]) + + ((limb) ((s32) in[0])) * ((s32) in[9])); + output[10] = 2 * (((limb) ((s32) in[5])) * ((s32) in[5]) + + ((limb) ((s32) in[4])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[3])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[9]))); + output[11] = 2 * (((limb) ((s32) in[5])) * ((s32) in[6]) + + ((limb) ((s32) in[4])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[8]) + + ((limb) ((s32) in[2])) * ((s32) in[9])); + output[12] = ((limb) ((s32) in[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in[4])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[5])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[9]))); + output[13] = 2 * (((limb) ((s32) in[6])) * ((s32) in[7]) + + ((limb) ((s32) in[5])) * ((s32) in[8]) + + ((limb) ((s32) in[4])) * ((s32) in[9])); + output[14] = 2 * (((limb) ((s32) in[7])) * ((s32) in[7]) + + ((limb) ((s32) in[6])) * ((s32) in[8]) + + 2 * ((limb) ((s32) in[5])) * ((s32) in[9])); + output[15] = 2 * (((limb) ((s32) in[7])) * ((s32) in[8]) + + ((limb) ((s32) in[6])) * ((s32) in[9])); + output[16] = ((limb) ((s32) in[8])) * ((s32) in[8]) + + 4 * ((limb) ((s32) in[7])) * ((s32) in[9]); + output[17] = 2 * ((limb) ((s32) in[8])) * ((s32) in[9]); + output[18] = 2 * ((limb) ((s32) in[9])) * ((s32) in[9]); +} + +/* fsquare sets output = in^2. + * + * On entry: The |in| argument is in reduced coefficients form and |in[i]| < + * 2^27. + * + * On exit: The |output| argument is in reduced coefficients form (indeed, one + * need only provide storage for 10 limbs) and |out[i]| < 2^26. */ +static void +fsquare(limb *output, const limb *in) { + limb t[19]; + fsquare_inner(t, in); + /* |t[i]| < 14*2^54 because the largest product of two limbs will be < + * 2^(27+27) and fsquare_inner adds together, at most, 14 of those + * products. */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +/* Take a little-endian, 32-byte number and expand it into polynomial form */ +static void fexpand32(limb *output, const u8 *input) +{ +#define F(n,start,shift,mask) \ + output[n] = ((((limb) input[start + 0]) | \ + ((limb) input[start + 1]) << 8 | \ + ((limb) input[start + 2]) << 16 | \ + ((limb) input[start + 3]) << 24) >> shift) & mask; + F(0, 0, 0, 0x3ffffff); + F(1, 3, 2, 0x1ffffff); + F(2, 6, 3, 0x3ffffff); + F(3, 9, 5, 0x1ffffff); + F(4, 12, 6, 0x3ffffff); + F(5, 16, 0, 0x1ffffff); + F(6, 19, 1, 0x3ffffff); + F(7, 22, 3, 0x1ffffff); + F(8, 25, 4, 0x3ffffff); + F(9, 28, 6, 0x1ffffff); +#undef F +} + +#if (-32 >> 1) != -16 +#error "This code only works when >> does sign-extension on negative numbers" +#endif + +/* s32_eq returns 0xffffffff iff a == b and zero otherwise. */ +static s32 s32_eq(s32 a, s32 b) { + a = ~(a ^ b); + a &= a << 16; + a &= a << 8; + a &= a << 4; + a &= a << 2; + a &= a << 1; + return a >> 31; +} + +/* s32_gte returns 0xffffffff if a >= b and zero otherwise, where a and b are + * both non-negative. */ +static s32 s32_gte(s32 a, s32 b) { + a -= b; + /* a >= 0 iff a >= b. */ + return ~(a >> 31); +} + +/* Take a fully reduced polynomial form number and contract it into a + * little-endian, 32-byte array. + * + * On entry: |input_limbs[i]| < 2^26 */ +static void fcontract32(u8 *output, limb *input_limbs) +{ + int i; + int j; + s32 input[10]; + s32 mask,carry; + + /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */ + for (i = 0; i < 10; i++) { + input[i] = (s32)input_limbs[i]; + } + + for (j = 0; j < 2; ++j) { + for (i = 0; i < 9; ++i) { + if ((i & 1) == 1) { + /* This calculation is a time-invariant way to make input[i] + * non-negative by borrowing from the next-larger limb. */ + mask = input[i] >> 31; + carry = -((input[i] & mask) >> 25); + input[i] = input[i] + (carry << 25); + input[i+1] = input[i+1] - carry; + } else { + mask = input[i] >> 31; + carry = -((input[i] & mask) >> 26); + input[i] = input[i] + (carry << 26); + input[i+1] = input[i+1] - carry; + } + } + + /* There's no greater limb for input[9] to borrow from, but we can multiply + * by 19 and borrow from input[0], which is valid mod 2^255-19. */ + { + mask = input[9] >> 31; + carry = -((input[9] & mask) >> 25); + input[9] = input[9] + (carry << 25); + input[0] = input[0] - (carry * 19); + } + + /* After the first iteration, input[1..9] are non-negative and fit within + * 25 or 26 bits, depending on position. However, input[0] may be + * negative. */ + } + + /* The first borrow-propagation pass above ended with every limb + except (possibly) input[0] non-negative. + + If input[0] was negative after the first pass, then it was because of a + carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most, + one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19. + + In the second pass, each limb is decreased by at most one. Thus the second + borrow-propagation pass could only have wrapped around to decrease + input[0] again if the first pass left input[0] negative *and* input[1] + through input[9] were all zero. In that case, input[1] is now 2^25 - 1, + and this last borrow-propagation step will leave input[1] non-negative. */ + { + mask = input[0] >> 31; + carry = -((input[0] & mask) >> 26); + input[0] = input[0] + (carry << 26); + input[1] = input[1] - carry; + } + + /* All input[i] are now non-negative. However, there might be values between + * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */ + for (j = 0; j < 2; j++) { + for (i = 0; i < 9; i++) { + if ((i & 1) == 1) { + carry = input[i] >> 25; + input[i] &= 0x1ffffff; + input[i+1] += carry; + } else { + carry = input[i] >> 26; + input[i] &= 0x3ffffff; + input[i+1] += carry; + } + } + + { + carry = input[9] >> 25; + input[9] &= 0x1ffffff; + input[0] += 19*carry; + } + } + + /* If the first carry-chain pass, just above, ended up with a carry from + * input[9], and that caused input[0] to be out-of-bounds, then input[0] was + * < 2^26 + 2*19, because the carry was, at most, two. + * + * If the second pass carried from input[9] again then input[0] is < 2*19 and + * the input[9] -> input[0] carry didn't push input[0] out of bounds. */ + + /* It still remains the case that input might be between 2^255-19 and 2^255. + * In this case, input[1..9] must take their maximum value and input[0] must + * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */ + mask = s32_gte(input[0], 0x3ffffed); + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + mask &= s32_eq(input[i], 0x1ffffff); + } else { + mask &= s32_eq(input[i], 0x3ffffff); + } + } + + /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus + * this conditionally subtracts 2^255-19. */ + input[0] -= mask & 0x3ffffed; + + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + input[i] -= mask & 0x1ffffff; + } else { + input[i] -= mask & 0x3ffffff; + } + } + + input[1] <<= 2; + input[2] <<= 3; + input[3] <<= 5; + input[4] <<= 6; + input[6] <<= 1; + input[7] <<= 3; + input[8] <<= 4; + input[9] <<= 6; +#define F(i, s) \ + output[s+0] |= input[i] & 0xff; \ + output[s+1] = (input[i] >> 8) & 0xff; \ + output[s+2] = (input[i] >> 16) & 0xff; \ + output[s+3] = (input[i] >> 24) & 0xff; + output[0] = 0; + output[16] = 0; + F(0,0); + F(1,3); + F(2,6); + F(3,9); + F(4,12); + F(5,16); + F(6,19); + F(7,22); + F(8,25); + F(9,28); +#undef F +} + +/* Input: Q, Q', Q-Q' + * Output: 2Q, Q+Q' + * + * x2 z3: long form + * x3 z3: long form + * x z: short form, destroyed + * xprime zprime: short form, destroyed + * qmqp: short form, preserved + * + * On entry and exit, the absolute value of the limbs of all inputs and outputs + * are < 2^26. */ +static void fmonty(limb *x2, limb *z2, /* output 2Q */ + limb *x3, limb *z3, /* output Q + Q' */ + limb *x, limb *z, /* input Q */ + limb *xprime, limb *zprime, /* input Q' */ + const limb *qmqp /* input Q - Q' */) { + limb origx[10], origxprime[10], zzz[19], xx[19], zz[19], xxprime[19], + zzprime[19], zzzprime[19], xxxprime[19]; + + memcpy(origx, x, 10 * sizeof(limb)); + fsum(x, z); + /* |x[i]| < 2^27 */ + fdifference_backwards(z, origx); /* does x - z */ + /* |z[i]| < 2^27 */ + + memcpy(origxprime, xprime, sizeof(limb) * 10); + fsum(xprime, zprime); + /* |xprime[i]| < 2^27 */ + fdifference_backwards(zprime, origxprime); + /* |zprime[i]| < 2^27 */ + fproduct(xxprime, xprime, z); + /* |xxprime[i]| < 14*2^54: the largest product of two limbs will be < + * 2^(27+27) and fproduct adds together, at most, 14 of those products. + * (Approximating that to 2^58 doesn't work out.) */ + fproduct(zzprime, x, zprime); + /* |zzprime[i]| < 14*2^54 */ + freduce_degree(xxprime); + freduce_coefficients(xxprime); + /* |xxprime[i]| < 2^26 */ + freduce_degree(zzprime); + freduce_coefficients(zzprime); + /* |zzprime[i]| < 2^26 */ + memcpy(origxprime, xxprime, sizeof(limb) * 10); + fsum(xxprime, zzprime); + /* |xxprime[i]| < 2^27 */ + fdifference_backwards(zzprime, origxprime); + /* |zzprime[i]| < 2^27 */ + fsquare(xxxprime, xxprime); + /* |xxxprime[i]| < 2^26 */ + fsquare(zzzprime, zzprime); + /* |zzzprime[i]| < 2^26 */ + fproduct(zzprime, zzzprime, qmqp); + /* |zzprime[i]| < 14*2^52 */ + freduce_degree(zzprime); + freduce_coefficients(zzprime); + /* |zzprime[i]| < 2^26 */ + memcpy(x3, xxxprime, sizeof(limb) * 10); + memcpy(z3, zzprime, sizeof(limb) * 10); + + fsquare(xx, x); + /* |xx[i]| < 2^26 */ + fsquare(zz, z); + /* |zz[i]| < 2^26 */ + fproduct(x2, xx, zz); + /* |x2[i]| < 14*2^52 */ + freduce_degree(x2); + freduce_coefficients(x2); + /* |x2[i]| < 2^26 */ + fdifference_backwards(zz, xx); // does zz = xx - zz + /* |zz[i]| < 2^27 */ + memset(zzz + 10, 0, sizeof(limb) * 9); + fscalar_product(zzz, zz, 121665); + /* |zzz[i]| < 2^(27+17) */ + /* No need to call freduce_degree here: + fscalar_product doesn't increase the degree of its input. */ + freduce_coefficients(zzz); + /* |zzz[i]| < 2^26 */ + fsum(zzz, xx); + /* |zzz[i]| < 2^27 */ + fproduct(z2, zz, zzz); + /* |z2[i]| < 14*2^(26+27) */ + freduce_degree(z2); + freduce_coefficients(z2); + /* |z2|i| < 2^26 */ +} + +/* Conditionally swap two reduced-form limb arrays if 'iswap' is 1, but leave + * them unchanged if 'iswap' is 0. Runs in data-invariant time to avoid + * side-channel attacks. + * + * NOTE that this function requires that 'iswap' be 1 or 0; other values give + * wrong results. Also, the two limb arrays must be in reduced-coefficient, + * reduced-degree form: the values in a[10..19] or b[10..19] aren't swapped, + * and all all values in a[0..9],b[0..9] must have magnitude less than + * INT32_MAX. */ +static void +swap_conditional(limb a[19], limb b[19], limb iswap) { + unsigned i; + const s32 swap = (s32) -iswap; + + for (i = 0; i < 10; ++i) { + const s32 x = swap & ( ((s32)a[i]) ^ ((s32)b[i]) ); + a[i] = ((s32)a[i]) ^ x; + b[i] = ((s32)b[i]) ^ x; + } +} + +/* Calculates nQ where Q is the x-coordinate of a point on the curve + * + * resultx/resultz: the x coordinate of the resulting curve point (short form) + * n: a little endian, 32-byte number + * q: a point of the curve (short form) */ +static void +cmult32(limb *resultx, limb *resultz, const u8 *n, const limb *q) { + limb a[19] = {0}, b[19] = {1}, c[19] = {1}, d[19] = {0}; + limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t; + limb e[19] = {0}, f[19] = {1}, g[19] = {0}, h[19] = {1}; + limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h; + + unsigned i, j; + + memcpy(nqpqx, q, sizeof(limb) * 10); + + for (i = 0; i < 32; ++i) { + u8 byte = n[31 - i]; + for (j = 0; j < 8; ++j) { + const limb bit = byte >> 7; + + swap_conditional(nqx, nqpqx, bit); + swap_conditional(nqz, nqpqz, bit); + fmonty(nqx2, nqz2, + nqpqx2, nqpqz2, + nqx, nqz, + nqpqx, nqpqz, + q); + swap_conditional(nqx2, nqpqx2, bit); + swap_conditional(nqz2, nqpqz2, bit); + + t = nqx; + nqx = nqx2; + nqx2 = t; + t = nqz; + nqz = nqz2; + nqz2 = t; + t = nqpqx; + nqpqx = nqpqx2; + nqpqx2 = t; + t = nqpqz; + nqpqz = nqpqz2; + nqpqz2 = t; + + byte <<= 1; + } + } + + memcpy(resultx, nqx, sizeof(limb) * 10); + memcpy(resultz, nqz, sizeof(limb) * 10); +} + +// ----------------------------------------------------------------------------- +// Shamelessly copied from djb's code +// ----------------------------------------------------------------------------- +static void +crecip32(limb *out, const limb *z) { + limb z2[10]; + limb z9[10]; + limb z11[10]; + limb z2_5_0[10]; + limb z2_10_0[10]; + limb z2_20_0[10]; + limb z2_50_0[10]; + limb z2_100_0[10]; + limb t0[10]; + limb t1[10]; + int i; + + /* 2 */ fsquare(z2,z); + /* 4 */ fsquare(t1,z2); + /* 8 */ fsquare(t0,t1); + /* 9 */ fmul32(z9,t0,z); + /* 11 */ fmul32(z11,z9,z2); + /* 22 */ fsquare(t0,z11); + /* 2^5 - 2^0 = 31 */ fmul32(z2_5_0,t0,z9); + + /* 2^6 - 2^1 */ fsquare(t0,z2_5_0); + /* 2^7 - 2^2 */ fsquare(t1,t0); + /* 2^8 - 2^3 */ fsquare(t0,t1); + /* 2^9 - 2^4 */ fsquare(t1,t0); + /* 2^10 - 2^5 */ fsquare(t0,t1); + /* 2^10 - 2^0 */ fmul32(z2_10_0,t0,z2_5_0); + + /* 2^11 - 2^1 */ fsquare(t0,z2_10_0); + /* 2^12 - 2^2 */ fsquare(t1,t0); + /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^20 - 2^0 */ fmul32(z2_20_0,t1,z2_10_0); + + /* 2^21 - 2^1 */ fsquare(t0,z2_20_0); + /* 2^22 - 2^2 */ fsquare(t1,t0); + /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^40 - 2^0 */ fmul32(t0,t1,z2_20_0); + + /* 2^41 - 2^1 */ fsquare(t1,t0); + /* 2^42 - 2^2 */ fsquare(t0,t1); + /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t1,t0); fsquare(t0,t1); } + /* 2^50 - 2^0 */ fmul32(z2_50_0,t0,z2_10_0); + + /* 2^51 - 2^1 */ fsquare(t0,z2_50_0); + /* 2^52 - 2^2 */ fsquare(t1,t0); + /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^100 - 2^0 */ fmul32(z2_100_0,t1,z2_50_0); + + /* 2^101 - 2^1 */ fsquare(t1,z2_100_0); + /* 2^102 - 2^2 */ fsquare(t0,t1); + /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { fsquare(t1,t0); fsquare(t0,t1); } + /* 2^200 - 2^0 */ fmul32(t1,t0,z2_100_0); + + /* 2^201 - 2^1 */ fsquare(t0,t1); + /* 2^202 - 2^2 */ fsquare(t1,t0); + /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^250 - 2^0 */ fmul32(t0,t1,z2_50_0); + + /* 2^251 - 2^1 */ fsquare(t1,t0); + /* 2^252 - 2^2 */ fsquare(t0,t1); + /* 2^253 - 2^3 */ fsquare(t1,t0); + /* 2^254 - 2^4 */ fsquare(t0,t1); + /* 2^255 - 2^5 */ fsquare(t1,t0); + /* 2^255 - 21 */ fmul32(out,t1,z11); +} + +int +curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) { + limb bp[10], x[10], z[11], zmone[10]; + uint8_t e[32]; + int i; + + for (i = 0; i < 32; ++i) e[i] = secret[i]; + e[0] &= 248; + e[31] &= 127; + e[31] |= 64; + + fexpand32(bp, basepoint); + cmult32(x, z, e, bp); + crecip32(zmone, z); + fmul32(z, x, zmone); + fcontract32(mypublic, z); + return 0; +} + +bits256 xoverz_donna(bits256 a) +{ + limb x[10],zmone[10],z[10],bp[10],out[11]; bits256 result,basepoint; + memset(basepoint.bytes,0,sizeof(basepoint)), basepoint.bytes[0] = 9; + fexpand32(bp,basepoint.bytes); + cmult32(x,z,a.bytes,bp); + crecip32(zmone,z); + fmul32(out,x,zmone); + fcontract32(result.bytes,out); + return(result); +} + +bits256 fmul_donna(bits256 a,bits256 b) +{ + limb avals[10],bvals[10],z[11]; bits256 result; + fexpand32(avals,a.bytes); + fexpand32(bvals,b.bytes); + fmul32(z,avals,bvals); + fcontract32(result.bytes,z); + return(result); +} + +bits256 crecip_donna(bits256 a) +{ + limb avals[10],zmone[10]; bits256 result; + fexpand32(avals,a.bytes); + crecip32(zmone,avals); + fcontract32(result.bytes,zmone); + return(result); +} diff --git a/crypto777/curve25519.c b/crypto777/curve25519.c new file mode 100755 index 000000000..d28b1eaf3 --- /dev/null +++ b/crypto777/curve25519.c @@ -0,0 +1,1695 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ +// derived from curve25519_donna + +#include "../includes/curve25519.h" + +#undef force_inline +#define force_inline __attribute__((always_inline)) + +// Sum two numbers: output += in +static inline bits320 force_inline fsum(bits320 output,bits320 in) +{ + int32_t i; + for (i=0; i<5; i++) + output.ulongs[i] += in.ulongs[i]; + return(output); +} + +static inline void force_inline fdifference_backwards(uint64_t *out,const uint64_t *in) +{ + static const uint64_t two54m152 = (((uint64_t)1) << 54) - 152; // 152 is 19 << 3 + static const uint64_t two54m8 = (((uint64_t)1) << 54) - 8; + int32_t i; + out[0] = in[0] + two54m152 - out[0]; + for (i=1; i<5; i++) + out[i] = in[i] + two54m8 - out[i]; +} + +inline void force_inline store_limb(uint8_t *out,uint64_t in) +{ + int32_t i; + for (i=0; i<8; i++,in>>=8) + out[i] = (in & 0xff); +} + +static inline uint64_t force_inline load_limb(uint8_t *in) +{ + return + ((uint64_t)in[0]) | + (((uint64_t)in[1]) << 8) | + (((uint64_t)in[2]) << 16) | + (((uint64_t)in[3]) << 24) | + (((uint64_t)in[4]) << 32) | + (((uint64_t)in[5]) << 40) | + (((uint64_t)in[6]) << 48) | + (((uint64_t)in[7]) << 56); +} + +// Take a little-endian, 32-byte number and expand it into polynomial form +bits320 fexpand(bits256 basepoint) +{ + bits320 out; + out.ulongs[0] = load_limb(basepoint.bytes) & 0x7ffffffffffffLL; + out.ulongs[1] = (load_limb(basepoint.bytes+6) >> 3) & 0x7ffffffffffffLL; + out.ulongs[2] = (load_limb(basepoint.bytes+12) >> 6) & 0x7ffffffffffffLL; + out.ulongs[3] = (load_limb(basepoint.bytes+19) >> 1) & 0x7ffffffffffffLL; + out.ulongs[4] = (load_limb(basepoint.bytes+24) >> 12) & 0x7ffffffffffffLL; + return(out); +} + +#if __amd64__ +// donna: special gcc mode for 128-bit integers. It's implemented on 64-bit platforms only as far as I know. +typedef unsigned uint128_t __attribute__((mode(TI))); + +// Multiply a number by a scalar: output = in * scalar +static inline bits320 force_inline fscalar_product(const bits320 in,const uint64_t scalar) +{ + int32_t i; uint128_t a = 0; bits320 output; + a = ((uint128_t)in.ulongs[0]) * scalar; + output.ulongs[0] = ((uint64_t)a) & 0x7ffffffffffffLL; + for (i=1; i<5; i++) + { + a = ((uint128_t)in.ulongs[i]) * scalar + ((uint64_t) (a >> 51)); + output.ulongs[i] = ((uint64_t)a) & 0x7ffffffffffffLL; + } + output.ulongs[0] += (a >> 51) * 19; + return(output); +} + +// Multiply two numbers: output = in2 * in +// output must be distinct to both inputs. The inputs are reduced coefficient form, the output is not. +// Assumes that in[i] < 2**55 and likewise for in2. On return, output[i] < 2**52 +bits320 fmul(const bits320 in2,const bits320 in) +{ + uint128_t t[5]; uint64_t r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c; bits320 out; + r0 = in.ulongs[0], r1 = in.ulongs[1], r2 = in.ulongs[2], r3 = in.ulongs[3], r4 = in.ulongs[4]; + s0 = in2.ulongs[0], s1 = in2.ulongs[1], s2 = in2.ulongs[2], s3 = in2.ulongs[3], s4 = in2.ulongs[4]; + t[0] = ((uint128_t) r0) * s0; + t[1] = ((uint128_t) r0) * s1 + ((uint128_t) r1) * s0; + t[2] = ((uint128_t) r0) * s2 + ((uint128_t) r2) * s0 + ((uint128_t) r1) * s1; + t[3] = ((uint128_t) r0) * s3 + ((uint128_t) r3) * s0 + ((uint128_t) r1) * s2 + ((uint128_t) r2) * s1; + t[4] = ((uint128_t) r0) * s4 + ((uint128_t) r4) * s0 + ((uint128_t) r3) * s1 + ((uint128_t) r1) * s3 + ((uint128_t) r2) * s2; + r4 *= 19, r1 *= 19, r2 *= 19, r3 *= 19; + t[0] += ((uint128_t) r4) * s1 + ((uint128_t) r1) * s4 + ((uint128_t) r2) * s3 + ((uint128_t) r3) * s2; + t[1] += ((uint128_t) r4) * s2 + ((uint128_t) r2) * s4 + ((uint128_t) r3) * s3; + t[2] += ((uint128_t) r4) * s3 + ((uint128_t) r3) * s4; + t[3] += ((uint128_t) r4) * s4; + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); + t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); + t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); + t[3] += c; r3 = (uint64_t)t[3] & 0x7ffffffffffffLL; c = (uint64_t)(t[3] >> 51); + t[4] += c; r4 = (uint64_t)t[4] & 0x7ffffffffffffLL; c = (uint64_t)(t[4] >> 51); + r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffffLL; + r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffffLL; + r2 += c; + out.ulongs[0] = r0, out.ulongs[1] = r1, out.ulongs[2] = r2, out.ulongs[3] = r3, out.ulongs[4] = r4; + return(out); +} + +inline bits320 force_inline fsquare_times(const bits320 in,uint64_t count) +{ + uint128_t t[5]; uint64_t r0,r1,r2,r3,r4,c,d0,d1,d2,d4,d419; bits320 out; + r0 = in.ulongs[0], r1 = in.ulongs[1], r2 = in.ulongs[2], r3 = in.ulongs[3], r4 = in.ulongs[4]; + do + { + d0 = r0 * 2; + d1 = r1 * 2; + d2 = r2 * 2 * 19; + d419 = r4 * 19; + d4 = d419 * 2; + t[0] = ((uint128_t) r0) * r0 + ((uint128_t) d4) * r1 + (((uint128_t) d2) * (r3 )); + t[1] = ((uint128_t) d0) * r1 + ((uint128_t) d4) * r2 + (((uint128_t) r3) * (r3 * 19)); + t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 )); + t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 )); + t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 )); + + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); + t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); + t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); + t[3] += c; r3 = (uint64_t)t[3] & 0x7ffffffffffffL; c = (uint64_t)(t[3] >> 51); + t[4] += c; r4 = (uint64_t)t[4] & 0x7ffffffffffffLL; c = (uint64_t)(t[4] >> 51); + r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffffLL; + r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffffLL; + r2 += c; + } while( --count ); + out.ulongs[0] = r0, out.ulongs[1] = r1, out.ulongs[2] = r2, out.ulongs[3] = r3, out.ulongs[4] = r4; + return(out); +} + +static inline void force_inline fcontract_iter(uint128_t t[5],int32_t flag) +{ + int32_t i; uint64_t mask = 0x7ffffffffffffLL; + for (i=0; i<4; i++) + t[i+1] += t[i] >> 51, t[i] &= mask; + if ( flag != 0 ) + t[0] += 19 * (t[4] >> 51); t[4] &= mask; +} + +// donna: Take a fully reduced polynomial form number and contract it into a little-endian, 32-byte array +bits256 fcontract(const bits320 input) +{ + uint128_t t[5]; int32_t i; bits256 out; + for (i=0; i<5; i++) + t[i] = input.ulongs[i]; + fcontract_iter(t,1), fcontract_iter(t,1); + // donna: now t is between 0 and 2^255-1, properly carried. + // donna: case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. + t[0] += 19, fcontract_iter(t,1); + // now between 19 and 2^255-1 in both cases, and offset by 19. + t[0] += 0x8000000000000 - 19; + for (i=1; i<5; i++) + t[i] += 0x8000000000000 - 1; + // now between 2^255 and 2^256-20, and offset by 2^255. + fcontract_iter(t,0); + store_limb(out.bytes,t[0] | (t[1] << 51)); + store_limb(out.bytes+8,(t[1] >> 13) | (t[2] << 38)); + store_limb(out.bytes+16,(t[2] >> 26) | (t[3] << 25)); + store_limb(out.bytes+24,(t[3] >> 39) | (t[4] << 12)); + return(out); +} + +bits256 curve25519(bits256 mysecret,bits256 basepoint) +{ + bits320 bp,x,z; + bp = fexpand(basepoint); + cmult(&x,&z,mysecret,bp); + return(fcontract(fmul(x,crecip(z)))); +} + +#else +// from curve25519-donna.c +typedef uint8_t u8; +typedef int32_t s32; +typedef int64_t limb; + +/* Multiply a number by a scalar: output = in * scalar */ +static void fscalar_product32(limb *output, const limb *in, const limb scalar) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] * scalar; + } +} + +/* Multiply two numbers: output = in2 * in + * + * output must be distinct to both inputs. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. +static void fproduct(limb *output, const limb *in2, const limb *in) { + output[0] = ((limb) ((s32) in2[0])) * ((s32) in[0]); + output[1] = ((limb) ((s32) in2[0])) * ((s32) in[1]) + + ((limb) ((s32) in2[1])) * ((s32) in[0]); + output[2] = 2 * ((limb) ((s32) in2[1])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[0]); + output[3] = ((limb) ((s32) in2[1])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[0]); + output[4] = ((limb) ((s32) in2[2])) * ((s32) in[2]) + + 2 * (((limb) ((s32) in2[1])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[1])) + + ((limb) ((s32) in2[0])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[0]); + output[5] = ((limb) ((s32) in2[2])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[0]); + output[6] = 2 * (((limb) ((s32) in2[3])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[0]); + output[7] = ((limb) ((s32) in2[3])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[0]); + output[8] = ((limb) ((s32) in2[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in2[3])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[0]); + output[9] = ((limb) ((s32) in2[4])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[0]); + output[10] = 2 * (((limb) ((s32) in2[5])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[1])) + + ((limb) ((s32) in2[4])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[4]) + + ((limb) ((s32) in2[2])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[2]); + output[11] = ((limb) ((s32) in2[5])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[2]); + output[12] = ((limb) ((s32) in2[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in2[5])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[3])) + + ((limb) ((s32) in2[4])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[4]); + output[13] = ((limb) ((s32) in2[6])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[6]) + + ((limb) ((s32) in2[5])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[4]); + output[14] = 2 * (((limb) ((s32) in2[7])) * ((s32) in[7]) + + ((limb) ((s32) in2[5])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[5])) + + ((limb) ((s32) in2[6])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[6]); + output[15] = ((limb) ((s32) in2[7])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[7]) + + ((limb) ((s32) in2[6])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[6]); + output[16] = ((limb) ((s32) in2[8])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in2[7])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[7])); + output[17] = ((limb) ((s32) in2[8])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[8]); + output[18] = 2 * ((limb) ((s32) in2[9])) * ((s32) in[9]); +}*/ + +/* Reduce a long form to a short form by taking the input mod 2^255 - 19. + * + * On entry: |output[i]| < 14*2^54 + * On exit: |output[0..8]| < 280*2^54 */ +static void freduce_degree(limb *output) { + /* Each of these shifts and adds ends up multiplying the value by 19. + * + * For output[0..8], the absolute entry value is < 14*2^54 and we add, at + * most, 19*14*2^54 thus, on exit, |output[0..8]| < 280*2^54. */ + output[8] += output[18] << 4; + output[8] += output[18] << 1; + output[8] += output[18]; + output[7] += output[17] << 4; + output[7] += output[17] << 1; + output[7] += output[17]; + output[6] += output[16] << 4; + output[6] += output[16] << 1; + output[6] += output[16]; + output[5] += output[15] << 4; + output[5] += output[15] << 1; + output[5] += output[15]; + output[4] += output[14] << 4; + output[4] += output[14] << 1; + output[4] += output[14]; + output[3] += output[13] << 4; + output[3] += output[13] << 1; + output[3] += output[13]; + output[2] += output[12] << 4; + output[2] += output[12] << 1; + output[2] += output[12]; + output[1] += output[11] << 4; + output[1] += output[11] << 1; + output[1] += output[11]; + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; +} + +#if (-1 & 3) != 3 +#error "This code only works on a two's complement system" +#endif + +/* return v / 2^26, using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_26(const limb v) +{ + /* High word of v; no shift needed. */ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x3ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 6; + /* Should return v / (1<<26) */ + return (v + roundoff) >> 26; +} + +/* return v / (2^25), using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_25(const limb v) +{ + /* High word of v; no shift needed*/ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x1ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 7; + /* Should return v / (1<<25) */ + return (v + roundoff) >> 25; +} + +/* Reduce all coefficients of the short form input so that |x| < 2^26. + * + * On entry: |output[i]| < 280*2^54 */ +static void freduce_coefficients(limb *output) { + unsigned i; + + output[10] = 0; + + for (i = 0; i < 10; i += 2) { + limb over = div_by_2_26(output[i]); + /* The entry condition (that |output[i]| < 280*2^54) means that over is, at + * most, 280*2^28 in the first iteration of this loop. This is added to the + * next limb and we can approximate the resulting bound of that limb by + * 281*2^54. */ + output[i] -= over << 26; + output[i+1] += over; + + /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| < + * 281*2^29. When this is added to the next limb, the resulting bound can + * be approximated as 281*2^54. + * + * For subsequent iterations of the loop, 281*2^54 remains a conservative + * bound and no overflow occurs. */ + over = div_by_2_25(output[i+1]); + output[i+1] -= over << 25; + output[i+2] += over; + } + /* Now |output[10]| < 281*2^29 and all other coefficients are reduced. */ + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; + + output[10] = 0; + + /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29 + * So |over| will be no more than 2^16. */ + { + limb over = div_by_2_26(output[0]); + output[0] -= over << 26; + output[1] += over; + } + + /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The + * bound on |output[1]| is sufficient to meet our needs. */ +} + +/* A helpful wrapper around fproduct: output = in * in2. + * + * On entry: |in[i]| < 2^27 and |in2[i]| < 2^27. + * + * output must be distinct to both inputs. The output is reduced degree + * (indeed, one need only provide storage for 10 limbs) and |output[i]| < 2^26. +static void fmul32(limb *output, const limb *in, const limb *in2) +{ + limb t[19]; + fproduct(t, in, in2); + //|t[i]| < 14*2^54 + freduce_degree(t); + freduce_coefficients(t); + // |t[i]| < 2^26 + memcpy(output, t, sizeof(limb) * 10); +}*/ + +/* Square a number: output = in**2 + * + * output must be distinct from the input. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fsquare_inner(limb *output, const limb *in) { + output[0] = ((limb) ((s32) in[0])) * ((s32) in[0]); + output[1] = 2 * ((limb) ((s32) in[0])) * ((s32) in[1]); + output[2] = 2 * (((limb) ((s32) in[1])) * ((s32) in[1]) + + ((limb) ((s32) in[0])) * ((s32) in[2])); + output[3] = 2 * (((limb) ((s32) in[1])) * ((s32) in[2]) + + ((limb) ((s32) in[0])) * ((s32) in[3])); + output[4] = ((limb) ((s32) in[2])) * ((s32) in[2]) + + 4 * ((limb) ((s32) in[1])) * ((s32) in[3]) + + 2 * ((limb) ((s32) in[0])) * ((s32) in[4]); + output[5] = 2 * (((limb) ((s32) in[2])) * ((s32) in[3]) + + ((limb) ((s32) in[1])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[5])); + output[6] = 2 * (((limb) ((s32) in[3])) * ((s32) in[3]) + + ((limb) ((s32) in[2])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[6]) + + 2 * ((limb) ((s32) in[1])) * ((s32) in[5])); + output[7] = 2 * (((limb) ((s32) in[3])) * ((s32) in[4]) + + ((limb) ((s32) in[2])) * ((s32) in[5]) + + ((limb) ((s32) in[1])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[7])); + output[8] = ((limb) ((s32) in[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in[2])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[1])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[5]))); + output[9] = 2 * (((limb) ((s32) in[4])) * ((s32) in[5]) + + ((limb) ((s32) in[3])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[8]) + + ((limb) ((s32) in[0])) * ((s32) in[9])); + output[10] = 2 * (((limb) ((s32) in[5])) * ((s32) in[5]) + + ((limb) ((s32) in[4])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[3])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[9]))); + output[11] = 2 * (((limb) ((s32) in[5])) * ((s32) in[6]) + + ((limb) ((s32) in[4])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[8]) + + ((limb) ((s32) in[2])) * ((s32) in[9])); + output[12] = ((limb) ((s32) in[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in[4])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[5])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[9]))); + output[13] = 2 * (((limb) ((s32) in[6])) * ((s32) in[7]) + + ((limb) ((s32) in[5])) * ((s32) in[8]) + + ((limb) ((s32) in[4])) * ((s32) in[9])); + output[14] = 2 * (((limb) ((s32) in[7])) * ((s32) in[7]) + + ((limb) ((s32) in[6])) * ((s32) in[8]) + + 2 * ((limb) ((s32) in[5])) * ((s32) in[9])); + output[15] = 2 * (((limb) ((s32) in[7])) * ((s32) in[8]) + + ((limb) ((s32) in[6])) * ((s32) in[9])); + output[16] = ((limb) ((s32) in[8])) * ((s32) in[8]) + + 4 * ((limb) ((s32) in[7])) * ((s32) in[9]); + output[17] = 2 * ((limb) ((s32) in[8])) * ((s32) in[9]); + output[18] = 2 * ((limb) ((s32) in[9])) * ((s32) in[9]); +} + +/* fsquare sets output = in^2. + * + * On entry: The |in| argument is in reduced coefficients form and |in[i]| < + * 2^27. + * + * On exit: The |output| argument is in reduced coefficients form (indeed, one + * need only provide storage for 10 limbs) and |out[i]| < 2^26. */ +static void +fsquare32(limb *output, const limb *in) { + limb t[19]; + fsquare_inner(t, in); + /* |t[i]| < 14*2^54 because the largest product of two limbs will be < + * 2^(27+27) and fsquare_inner adds together, at most, 14 of those + * products. */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +#if (-32 >> 1) != -16 +#error "This code only works when >> does sign-extension on negative numbers" +#endif + +/* s32_eq returns 0xffffffff iff a == b and zero otherwise. */ +static s32 s32_eq(s32 a, s32 b) { + a = ~(a ^ b); + a &= a << 16; + a &= a << 8; + a &= a << 4; + a &= a << 2; + a &= a << 1; + return a >> 31; +} + +/* s32_gte returns 0xffffffff if a >= b and zero otherwise, where a and b are + * both non-negative. */ +static s32 s32_gte(s32 a, s32 b) { + a -= b; + /* a >= 0 iff a >= b. */ + return ~(a >> 31); +} + +/* Take a fully reduced polynomial form number and contract it into a + * little-endian, 32-byte array. + * + * On entry: |input_limbs[i]| < 2^26 */ +static void fcontract32(u8 *output, limb *input_limbs) +{ + int i; + int j; + s32 input[10]; + s32 mask; + + /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */ + for (i = 0; i < 10; i++) + input[i] = (s32)input_limbs[i]; + + for (j = 0; j < 2; ++j) { + for (i = 0; i < 9; ++i) { + if ((i & 1) == 1) { + /* This calculation is a time-invariant way to make input[i] + * non-negative by borrowing from the next-larger limb. */ + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 25); + input[i] = input[i] + (carry << 25); + input[i+1] = input[i+1] - carry; + } else { + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 26); + input[i] = input[i] + (carry << 26); + input[i+1] = input[i+1] - carry; + } + } + + /* There's no greater limb for input[9] to borrow from, but we can multiply + * by 19 and borrow from input[0], which is valid mod 2^255-19. */ + { + const s32 mask = input[9] >> 31; + const s32 carry = -((input[9] & mask) >> 25); + input[9] = input[9] + (carry << 25); + input[0] = input[0] - (carry * 19); + } + + /* After the first iteration, input[1..9] are non-negative and fit within + * 25 or 26 bits, depending on position. However, input[0] may be + * negative. */ + } + + /* The first borrow-propagation pass above ended with every limb + except (possibly) input[0] non-negative. + + If input[0] was negative after the first pass, then it was because of a + carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most, + one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19. + + In the second pass, each limb is decreased by at most one. Thus the second + borrow-propagation pass could only have wrapped around to decrease + input[0] again if the first pass left input[0] negative *and* input[1] + through input[9] were all zero. In that case, input[1] is now 2^25 - 1, + and this last borrow-propagation step will leave input[1] non-negative. */ + { + const s32 mask = input[0] >> 31; + const s32 carry = -((input[0] & mask) >> 26); + input[0] = input[0] + (carry << 26); + input[1] = input[1] - carry; + } + + /* All input[i] are now non-negative. However, there might be values between + * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */ + for (j = 0; j < 2; j++) { + for (i = 0; i < 9; i++) { + if ((i & 1) == 1) { + const s32 carry = input[i] >> 25; + input[i] &= 0x1ffffff; + input[i+1] += carry; + } else { + const s32 carry = input[i] >> 26; + input[i] &= 0x3ffffff; + input[i+1] += carry; + } + } + + { + const s32 carry = input[9] >> 25; + input[9] &= 0x1ffffff; + input[0] += 19*carry; + } + } + + /* If the first carry-chain pass, just above, ended up with a carry from + * input[9], and that caused input[0] to be out-of-bounds, then input[0] was + * < 2^26 + 2*19, because the carry was, at most, two. + * + * If the second pass carried from input[9] again then input[0] is < 2*19 and + * the input[9] -> input[0] carry didn't push input[0] out of bounds. */ + + /* It still remains the case that input might be between 2^255-19 and 2^255. + * In this case, input[1..9] must take their maximum value and input[0] must + * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */ + mask = s32_gte(input[0], 0x3ffffed); + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + mask &= s32_eq(input[i], 0x1ffffff); + } else { + mask &= s32_eq(input[i], 0x3ffffff); + } + } + + /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus + * this conditionally subtracts 2^255-19. */ + input[0] -= mask & 0x3ffffed; + + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + input[i] -= mask & 0x1ffffff; + } else { + input[i] -= mask & 0x3ffffff; + } + } + + input[1] <<= 2; + input[2] <<= 3; + input[3] <<= 5; + input[4] <<= 6; + input[6] <<= 1; + input[7] <<= 3; + input[8] <<= 4; + input[9] <<= 6; +#define F(i, s) \ +output[s+0] |= input[i] & 0xff; \ +output[s+1] = (input[i] >> 8) & 0xff; \ +output[s+2] = (input[i] >> 16) & 0xff; \ +output[s+3] = (input[i] >> 24) & 0xff; + output[0] = 0; + output[16] = 0; + F(0,0); + F(1,3); + F(2,6); + F(3,9); + F(4,12); + F(5,16); + F(6,19); + F(7,22); + F(8,25); + F(9,28); +#undef F +} + +bits320 bits320_limbs(limb limbs[10]) +{ + bits320 output; int32_t i; + for (i=0; i<10; i++) + output.uints[i] = limbs[i]; + return(output); +} + +static inline bits320 force_inline fscalar_product(const bits320 in,const uint64_t scalar) +{ + limb output[10],input[10]; int32_t i; + for (i=0; i<10; i++) + input[i] = in.uints[i]; + fscalar_product32(output,input,scalar); + return(bits320_limbs(output)); +} + +static inline bits320 force_inline fsquare_times(const bits320 in,uint64_t count) +{ + limb output[10],input[10]; int32_t i; + for (i=0; i<10; i++) + input[i] = in.uints[i]; + for (i=0; i Output: 2Q, Q+Q' +// x2 z2: long form && x3 z3: long form +// x z: short form, destroyed && xprime zprime: short form, destroyed +// qmqp: short form, preserved +static inline void force_inline +fmonty(bits320 *x2, bits320 *z2, // output 2Q + bits320 *x3, bits320 *z3, // output Q + Q' + bits320 *x, bits320 *z, // input Q + bits320 *xprime, bits320 *zprime, // input Q' + const bits320 qmqp) // input Q - Q' +{ + bits320 origx,origxprime,zzz,xx,zz,xxprime,zzprime; + origx = *x; + *x = fsum(*x,*z), fdifference_backwards(z->ulongs,origx.ulongs); // does x - z + origxprime = *xprime; + *xprime = fsum(*xprime,*zprime), fdifference_backwards(zprime->ulongs,origxprime.ulongs); + xxprime = fmul(*xprime,*z), zzprime = fmul(*x,*zprime); + origxprime = xxprime; + xxprime = fsum(xxprime,zzprime), fdifference_backwards(zzprime.ulongs,origxprime.ulongs); + *x3 = fsquare_times(xxprime,1), *z3 = fmul(fsquare_times(zzprime,1),qmqp); + xx = fsquare_times(*x,1), zz = fsquare_times(*z,1); + *x2 = fmul(xx,zz); + fdifference_backwards(zz.ulongs,xx.ulongs); // does zz = xx - zz + zzz = fscalar_product(zz,121665); + *z2 = fmul(zz,fsum(zzz,xx)); +} + +// ----------------------------------------------------------------------------- +// Maybe swap the contents of two limb arrays (@a and @b), each @len elements +// long. Perform the swap iff @swap is non-zero. +// This function performs the swap without leaking any side-channel information. +// ----------------------------------------------------------------------------- +static inline void force_inline swap_conditional(bits320 *a,bits320 *b,uint64_t iswap) +{ + int32_t i; const uint64_t swap = -iswap; + for (i=0; i<5; ++i) + { + const uint64_t x = swap & (a->ulongs[i] ^ b->ulongs[i]); + a->ulongs[i] ^= x, b->ulongs[i] ^= x; + } +} + +// Calculates nQ where Q is the x-coordinate of a point on the curve +// resultx/resultz: the x coordinate of the resulting curve point (short form) +// n: a little endian, 32-byte number +// q: a point of the curve (short form) +void cmult(bits320 *resultx,bits320 *resultz,bits256 secret,const bits320 q) +{ + int32_t i,j; bits320 a,b,c,d,e,f,g,h,*t; + bits320 Zero320bits,One320bits, *nqpqx = &a,*nqpqz = &b,*nqx = &c,*nqz = &d,*nqpqx2 = &e,*nqpqz2 = &f,*nqx2 = &g,*nqz2 = &h; + memset(&Zero320bits,0,sizeof(Zero320bits)); + memset(&One320bits,0,sizeof(One320bits)), One320bits.ulongs[0] = 1; + a = d = e = g = Zero320bits, b = c = f = h = One320bits; + *nqpqx = q; + for (i=0; i<32; i++) + { + uint8_t byte = secret.bytes[31 - i]; + for (j=0; j<8; j++) + { + const uint64_t bit = byte >> 7; + swap_conditional(nqx,nqpqx,bit), swap_conditional(nqz,nqpqz,bit); + fmonty(nqx2,nqz2,nqpqx2,nqpqz2,nqx,nqz,nqpqx,nqpqz,q); + swap_conditional(nqx2,nqpqx2,bit), swap_conditional(nqz2,nqpqz2,bit); + t = nqx, nqx = nqx2, nqx2 = t; + t = nqz, nqz = nqz2, nqz2 = t; + t = nqpqx, nqpqx = nqpqx2, nqpqx2 = t; + t = nqpqz, nqpqz = nqpqz2, nqpqz2 = t; + byte <<= 1; + } + } + *resultx = *nqx, *resultz = *nqz; +} + +// Shamelessly copied from donna's code that copied djb's code, changed a little +inline bits320 force_inline crecip(const bits320 z) +{ + bits320 a,t0,b,c; + /* 2 */ a = fsquare_times(z, 1); // a = 2 + /* 8 */ t0 = fsquare_times(a, 2); + /* 9 */ b = fmul(t0, z); // b = 9 + /* 11 */ a = fmul(b, a); // a = 11 + /* 22 */ t0 = fsquare_times(a, 1); + /* 2^5 - 2^0 = 31 */ b = fmul(t0, b); + /* 2^10 - 2^5 */ t0 = fsquare_times(b, 5); + /* 2^10 - 2^0 */ b = fmul(t0, b); + /* 2^20 - 2^10 */ t0 = fsquare_times(b, 10); + /* 2^20 - 2^0 */ c = fmul(t0, b); + /* 2^40 - 2^20 */ t0 = fsquare_times(c, 20); + /* 2^40 - 2^0 */ t0 = fmul(t0, c); + /* 2^50 - 2^10 */ t0 = fsquare_times(t0, 10); + /* 2^50 - 2^0 */ b = fmul(t0, b); + /* 2^100 - 2^50 */ t0 = fsquare_times(b, 50); + /* 2^100 - 2^0 */ c = fmul(t0, b); + /* 2^200 - 2^100 */ t0 = fsquare_times(c, 100); + /* 2^200 - 2^0 */ t0 = fmul(t0, c); + /* 2^250 - 2^50 */ t0 = fsquare_times(t0, 50); + /* 2^250 - 2^0 */ t0 = fmul(t0, b); + /* 2^255 - 2^5 */ t0 = fsquare_times(t0, 5); + /* 2^255 - 21 */ return(fmul(t0, a)); +} + +void randombytes(unsigned char *x,long xlen); + +bits256 rand256(int32_t privkeyflag) +{ + bits256 randval; + OS_randombytes(randval.bytes,sizeof(randval)); + if ( privkeyflag != 0 ) + randval.bytes[0] &= 0xf8, randval.bytes[31] &= 0x7f, randval.bytes[31] |= 0x40; + return(randval); +} + +bits256 curve25519_basepoint9() +{ + bits256 basepoint; + memset(&basepoint,0,sizeof(basepoint)); + basepoint.bytes[0] = 9; + return(basepoint); +} + +bits256 curve25519_keypair(bits256 *pubkeyp) +{ + bits256 privkey; + privkey = rand256(1); + *pubkeyp = curve25519(privkey,curve25519_basepoint9()); + //printf("[%llx %llx] ",privkey.txid,(*pubkeyp).txid); + return(privkey); +} + +// following is ported from libtom + +#define STORE32L(x, y) \ +{ (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \ +(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); } + +#define LOAD32L(x, y) \ +{ x = (uint32_t)(((uint64_t)((y)[3] & 255)<<24) | \ +((uint32_t)((y)[2] & 255)<<16) | \ +((uint32_t)((y)[1] & 255)<<8) | \ +((uint32_t)((y)[0] & 255))); } + +#define STORE64L(x, y) \ +{ (y)[7] = (uint8_t)(((x)>>56)&255); (y)[6] = (uint8_t)(((x)>>48)&255); \ +(y)[5] = (uint8_t)(((x)>>40)&255); (y)[4] = (uint8_t)(((x)>>32)&255); \ +(y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \ +(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); } + +#define LOAD64L(x, y) \ +{ x = (((uint64_t)((y)[7] & 255))<<56)|(((uint64_t)((y)[6] & 255))<<48)| \ +(((uint64_t)((y)[5] & 255))<<40)|(((uint64_t)((y)[4] & 255))<<32)| \ +(((uint64_t)((y)[3] & 255))<<24)|(((uint64_t)((y)[2] & 255))<<16)| \ +(((uint64_t)((y)[1] & 255))<<8)|(((uint64_t)((y)[0] & 255))); } + +#define STORE32H(x, y) \ +{ (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255); \ +(y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); } + +#define LOAD32H(x, y) \ +{ x = (uint32_t)(((uint64_t)((y)[0] & 255)<<24) | \ +((uint32_t)((y)[1] & 255)<<16) | \ +((uint32_t)((y)[2] & 255)<<8) | \ +((uint32_t)((y)[3] & 255))); } + +#define STORE64H(x, y) \ +{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \ +(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \ +(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \ +(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); } + +#define LOAD64H(x, y) \ +{ x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \ +(((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \ +(((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \ +(((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); } + +// Various logical functions +#define RORc(x, y) ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define Ch(x,y,z) (z ^ (x & (y ^ z))) +#define Maj(x,y,z) (((x | y) & z) | (x & y)) +#define S(x, n) RORc((x),(n)) +#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) +#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) +#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) +#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) +#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) +#define MIN(x, y) ( ((x)<(y))?(x):(y) ) + +static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf) +{ + uint32_t S[8],W[64],t0,t1,i; + for (i=0; i<8; i++) // copy state into S + S[i] = md->state[i]; + for (i=0; i<16; i++) // copy the state into 512-bits into W[0..15] + LOAD32H(W[i],buf + (4*i)); + for (i=16; i<64; i++) // fill W[16..63] + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + +#define RND(a,b,c,d,e,f,g,h,i,ki) \ +t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ +t1 = Sigma0(a) + Maj(a, b, c); \ +d += t0; \ +h = t0 + t1; + + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); +#undef RND + for (i=0; i<8; i++) // feedback + md->state[i] = md->state[i] + S[i]; + return(0); +} + +#undef RORc +#undef Ch +#undef Maj +#undef S +#undef R +#undef Sigma0 +#undef Sigma1 +#undef Gamma0 +#undef Gamma1 + +static inline void sha256_vinit(struct sha256_vstate * md) +{ + md->curlen = 0; + md->length = 0; + md->state[0] = 0x6A09E667UL; + md->state[1] = 0xBB67AE85UL; + md->state[2] = 0x3C6EF372UL; + md->state[3] = 0xA54FF53AUL; + md->state[4] = 0x510E527FUL; + md->state[5] = 0x9B05688CUL; + md->state[6] = 0x1F83D9ABUL; + md->state[7] = 0x5BE0CD19UL; +} + +static inline int32_t sha256_vprocess(struct sha256_vstate *md,const uint8_t *in,uint64_t inlen) +{ + uint64_t n; int32_t err; + if ( md->curlen > sizeof(md->buf) ) + return(-1); + while ( inlen > 0 ) + { + if ( md->curlen == 0 && inlen >= 64 ) + { + if ( (err= sha256_vcompress(md,(uint8_t *)in)) != 0 ) + return(err); + md->length += 64 * 8, in += 64, inlen -= 64; + } + else + { + n = MIN(inlen,64 - md->curlen); + memcpy(md->buf + md->curlen,in,(size_t)n); + md->curlen += n, in += n, inlen -= n; + if ( md->curlen == 64 ) + { + if ( (err= sha256_vcompress(md,md->buf)) != 0 ) + return(err); + md->length += 8*64; + md->curlen = 0; + } + } + } + return(0); +} + +static inline int32_t sha256_vdone(struct sha256_vstate *md,uint8_t *out) +{ + int32_t i; + if ( md->curlen >= sizeof(md->buf) ) + return(-1); + md->length += md->curlen * 8; // increase the length of the message + md->buf[md->curlen++] = (uint8_t)0x80; // append the '1' bit + // if len > 56 bytes we append zeros then compress. Then we can fall back to padding zeros and length encoding like normal. + if ( md->curlen > 56 ) + { + while ( md->curlen < 64 ) + md->buf[md->curlen++] = (uint8_t)0; + sha256_vcompress(md,md->buf); + md->curlen = 0; + } + while ( md->curlen < 56 ) // pad upto 56 bytes of zeroes + md->buf[md->curlen++] = (uint8_t)0; + STORE64H(md->length,md->buf+56); // store length + sha256_vcompress(md,md->buf); + for (i=0; i<8; i++) // copy output + STORE32H(md->state[i],out+(4*i)); + return(0); +} + +int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); + +void vcalc_sha256(char hashstr[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len) +{ + struct sha256_vstate md; + sha256_vinit(&md); + sha256_vprocess(&md,src,len); + sha256_vdone(&md,hash); + if ( hashstr != 0 ) + init_hexbytes_noT(hashstr,hash,256 >> 3); +} + +void vcalc_sha256cat(uint8_t hash[256 >> 3],uint8_t *src,int32_t len,uint8_t *src2,int32_t len2) +{ + struct sha256_vstate md; + sha256_vinit(&md); + sha256_vprocess(&md,src,len); + if ( src2 != 0 ) + sha256_vprocess(&md,src2,len2); + sha256_vdone(&md,hash); +} + +void vupdate_sha256(uint8_t hash[256 >> 3],struct sha256_vstate *state,uint8_t *src,int32_t len) +{ + struct sha256_vstate md; + memset(&md,0,sizeof(md)); + if ( src == 0 ) + sha256_vinit(&md); + else + { + md = *state; + sha256_vprocess(&md,src,len); + } + *state = md; + sha256_vdone(&md,hash); +} + + +// rmd160: the five basic functions F(), G() and H() +#define F(x, y, z) ((x) ^ (y) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define H(x, y, z) (((x) | ~(y)) ^ (z)) +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define J(x, y, z) ((x) ^ ((y) | ~(z))) +#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) + +/* the ten basic operations FF() through III() */ +#define FF(a, b, c, d, e, x, s) \ +(a) += F((b), (c), (d)) + (x);\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define GG(a, b, c, d, e, x, s) \ +(a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define HH(a, b, c, d, e, x, s) \ +(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define II(a, b, c, d, e, x, s) \ +(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define JJ(a, b, c, d, e, x, s) \ +(a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define FFF(a, b, c, d, e, x, s) \ +(a) += F((b), (c), (d)) + (x);\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define GGG(a, b, c, d, e, x, s) \ +(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define HHH(a, b, c, d, e, x, s) \ +(a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define III(a, b, c, d, e, x, s) \ +(a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +#define JJJ(a, b, c, d, e, x, s) \ +(a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ +(a) = ROLc((a), (s)) + (e);\ +(c) = ROLc((c), 10); + +static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) +{ + uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16]; + int i; + + /* load words X */ + for (i = 0; i < 16; i++){ + LOAD32L(X[i], buf + (4 * i)); + } + + /* load state */ + aa = aaa = md->state[0]; + bb = bbb = md->state[1]; + cc = ccc = md->state[2]; + dd = ddd = md->state[3]; + ee = eee = md->state[4]; + + /* round 1 */ + FF(aa, bb, cc, dd, ee, X[ 0], 11); + FF(ee, aa, bb, cc, dd, X[ 1], 14); + FF(dd, ee, aa, bb, cc, X[ 2], 15); + FF(cc, dd, ee, aa, bb, X[ 3], 12); + FF(bb, cc, dd, ee, aa, X[ 4], 5); + FF(aa, bb, cc, dd, ee, X[ 5], 8); + FF(ee, aa, bb, cc, dd, X[ 6], 7); + FF(dd, ee, aa, bb, cc, X[ 7], 9); + FF(cc, dd, ee, aa, bb, X[ 8], 11); + FF(bb, cc, dd, ee, aa, X[ 9], 13); + FF(aa, bb, cc, dd, ee, X[10], 14); + FF(ee, aa, bb, cc, dd, X[11], 15); + FF(dd, ee, aa, bb, cc, X[12], 6); + FF(cc, dd, ee, aa, bb, X[13], 7); + FF(bb, cc, dd, ee, aa, X[14], 9); + FF(aa, bb, cc, dd, ee, X[15], 8); + + /* round 2 */ + GG(ee, aa, bb, cc, dd, X[ 7], 7); + GG(dd, ee, aa, bb, cc, X[ 4], 6); + GG(cc, dd, ee, aa, bb, X[13], 8); + GG(bb, cc, dd, ee, aa, X[ 1], 13); + GG(aa, bb, cc, dd, ee, X[10], 11); + GG(ee, aa, bb, cc, dd, X[ 6], 9); + GG(dd, ee, aa, bb, cc, X[15], 7); + GG(cc, dd, ee, aa, bb, X[ 3], 15); + GG(bb, cc, dd, ee, aa, X[12], 7); + GG(aa, bb, cc, dd, ee, X[ 0], 12); + GG(ee, aa, bb, cc, dd, X[ 9], 15); + GG(dd, ee, aa, bb, cc, X[ 5], 9); + GG(cc, dd, ee, aa, bb, X[ 2], 11); + GG(bb, cc, dd, ee, aa, X[14], 7); + GG(aa, bb, cc, dd, ee, X[11], 13); + GG(ee, aa, bb, cc, dd, X[ 8], 12); + + /* round 3 */ + HH(dd, ee, aa, bb, cc, X[ 3], 11); + HH(cc, dd, ee, aa, bb, X[10], 13); + HH(bb, cc, dd, ee, aa, X[14], 6); + HH(aa, bb, cc, dd, ee, X[ 4], 7); + HH(ee, aa, bb, cc, dd, X[ 9], 14); + HH(dd, ee, aa, bb, cc, X[15], 9); + HH(cc, dd, ee, aa, bb, X[ 8], 13); + HH(bb, cc, dd, ee, aa, X[ 1], 15); + HH(aa, bb, cc, dd, ee, X[ 2], 14); + HH(ee, aa, bb, cc, dd, X[ 7], 8); + HH(dd, ee, aa, bb, cc, X[ 0], 13); + HH(cc, dd, ee, aa, bb, X[ 6], 6); + HH(bb, cc, dd, ee, aa, X[13], 5); + HH(aa, bb, cc, dd, ee, X[11], 12); + HH(ee, aa, bb, cc, dd, X[ 5], 7); + HH(dd, ee, aa, bb, cc, X[12], 5); + + /* round 4 */ + II(cc, dd, ee, aa, bb, X[ 1], 11); + II(bb, cc, dd, ee, aa, X[ 9], 12); + II(aa, bb, cc, dd, ee, X[11], 14); + II(ee, aa, bb, cc, dd, X[10], 15); + II(dd, ee, aa, bb, cc, X[ 0], 14); + II(cc, dd, ee, aa, bb, X[ 8], 15); + II(bb, cc, dd, ee, aa, X[12], 9); + II(aa, bb, cc, dd, ee, X[ 4], 8); + II(ee, aa, bb, cc, dd, X[13], 9); + II(dd, ee, aa, bb, cc, X[ 3], 14); + II(cc, dd, ee, aa, bb, X[ 7], 5); + II(bb, cc, dd, ee, aa, X[15], 6); + II(aa, bb, cc, dd, ee, X[14], 8); + II(ee, aa, bb, cc, dd, X[ 5], 6); + II(dd, ee, aa, bb, cc, X[ 6], 5); + II(cc, dd, ee, aa, bb, X[ 2], 12); + + /* round 5 */ + JJ(bb, cc, dd, ee, aa, X[ 4], 9); + JJ(aa, bb, cc, dd, ee, X[ 0], 15); + JJ(ee, aa, bb, cc, dd, X[ 5], 5); + JJ(dd, ee, aa, bb, cc, X[ 9], 11); + JJ(cc, dd, ee, aa, bb, X[ 7], 6); + JJ(bb, cc, dd, ee, aa, X[12], 8); + JJ(aa, bb, cc, dd, ee, X[ 2], 13); + JJ(ee, aa, bb, cc, dd, X[10], 12); + JJ(dd, ee, aa, bb, cc, X[14], 5); + JJ(cc, dd, ee, aa, bb, X[ 1], 12); + JJ(bb, cc, dd, ee, aa, X[ 3], 13); + JJ(aa, bb, cc, dd, ee, X[ 8], 14); + JJ(ee, aa, bb, cc, dd, X[11], 11); + JJ(dd, ee, aa, bb, cc, X[ 6], 8); + JJ(cc, dd, ee, aa, bb, X[15], 5); + JJ(bb, cc, dd, ee, aa, X[13], 6); + + /* parallel round 1 */ + JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); + JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9); + JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13); + JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15); + JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5); + JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7); + JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); + JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14); + JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); + JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); + + /* parallel round 2 */ + III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); + III(ddd, eee, aaa, bbb, ccc, X[11], 13); + III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); + III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); + III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); + III(eee, aaa, bbb, ccc, ddd, X[13], 8); + III(ddd, eee, aaa, bbb, ccc, X[ 5], 9); + III(ccc, ddd, eee, aaa, bbb, X[10], 11); + III(bbb, ccc, ddd, eee, aaa, X[14], 7); + III(aaa, bbb, ccc, ddd, eee, X[15], 7); + III(eee, aaa, bbb, ccc, ddd, X[ 8], 12); + III(ddd, eee, aaa, bbb, ccc, X[12], 7); + III(ccc, ddd, eee, aaa, bbb, X[ 4], 6); + III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); + III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); + III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); + + /* parallel round 3 */ + HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); + HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); + HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15); + HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11); + HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8); + HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); + HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6); + HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14); + HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); + HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13); + HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); + HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14); + HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); + HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); + HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); + HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); + + /* parallel round 4 */ + GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); + GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); + GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); + GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); + GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); + GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); + GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); + GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14); + GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6); + GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); + GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12); + GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); + GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12); + GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); + GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); + GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); + + /* parallel round 5 */ + FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); + FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); + FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12); + FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9); + FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12); + FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5); + FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14); + FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6); + FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8); + FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13); + FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6); + FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5); + FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15); + FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); + FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); + FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); + + /* combine results */ + ddd += cc + md->state[1]; /* final result for md->state[0] */ + md->state[1] = md->state[2] + dd + eee; + md->state[2] = md->state[3] + ee + aaa; + md->state[3] = md->state[4] + aa + bbb; + md->state[4] = md->state[0] + bb + ccc; + md->state[0] = ddd; + + return 0; +} + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return 0 if successful + */ +int rmd160_vinit(struct rmd160_vstate * md) +{ + md->state[0] = 0x67452301UL; + md->state[1] = 0xefcdab89UL; + md->state[2] = 0x98badcfeUL; + md->state[3] = 0x10325476UL; + md->state[4] = 0xc3d2e1f0UL; + md->curlen = 0; + md->length = 0; + return 0; +} +#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ +int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen) \ +{ \ +unsigned long n; \ +int err; \ +if (md->curlen > sizeof(md->buf)) { \ +return -1; \ +} \ +while (inlen > 0) { \ +if (md->curlen == 0 && inlen >= block_size) { \ +if ((err = compress_name (md, (unsigned char *)in)) != 0) { \ +return err; \ +} \ +md->length += block_size * 8; \ +in += block_size; \ +inlen -= block_size; \ +} else { \ +n = MIN(inlen, (block_size - md->curlen)); \ +memcpy(md->buf + md->curlen, in, (size_t)n); \ +md->curlen += n; \ +in += n; \ +inlen -= n; \ +if (md->curlen == block_size) { \ +if ((err = compress_name (md, md->buf)) != 0) { \ +return err; \ +} \ +md->length += 8*block_size; \ +md->curlen = 0; \ +} \ +} \ +} \ +return 0; \ +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return 0 if successful + */ +HASH_PROCESS(rmd160_vprocess, rmd160_vcompress, rmd160, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (20 bytes) + @return 0 if successful + */ +int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out) +{ + int i; + if (md->curlen >= sizeof(md->buf)) { + return -1; + } + /* increase the length of the message */ + md->length += md->curlen * 8; + + /* append the '1' bit */ + md->buf[md->curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->curlen > 56) { + while (md->curlen < 64) { + md->buf[md->curlen++] = (unsigned char)0; + } + rmd160_vcompress(md, md->buf); + md->curlen = 0; + } + /* pad upto 56 bytes of zeroes */ + while (md->curlen < 56) { + md->buf[md->curlen++] = (unsigned char)0; + } + /* store length */ + STORE64L(md->length, md->buf+56); + rmd160_vcompress(md, md->buf); + /* copy output */ + for (i = 0; i < 5; i++) { + STORE32L(md->state[i], out+(4*i)); + } + return 0; +} + +void calc_rmd160(char hexstr[41],uint8_t buf[20],uint8_t *msg,int32_t len) +{ + struct rmd160_vstate md; + rmd160_vinit(&md); + rmd160_vprocess(&md,msg,len); + rmd160_vdone(&md, buf); + if ( hexstr != 0 ) + init_hexbytes_noT(hexstr,buf,20); +} + +#ifdef ENABLE_RMDTEST +int rmd160_test(void) +{ + static const struct { + char *msg; + unsigned char md[20]; + } tests[] = { + { "", + { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, + 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 } + }, + { "a", + { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae, + 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe } + }, + { "abc", + { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04, + 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc } + }, + { "message digest", + { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8, + 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 } + }, + { "abcdefghijklmnopqrstuvwxyz", + { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb, + 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, + 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b } + } + }; + int x; + unsigned char buf[20]; char hexstr[41]; + + for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + calc_rmd160(hexstr,buf,(unsigned char *)tests[x].msg,(int32_t) strlen(tests[x].msg)); + if (memcmp(buf, tests[x].md, 20) != 0) { + printf("Failed test %d\n", x); + return -1; + } + else printf("rmd160(%s) -> (%s)\n",tests[x].msg,hexstr); + } + return 0; +} +#endif + +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef F +#undef G +#undef H +#undef I +#undef J +#undef ROLc + +static const uint32_t crc32_tab[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size) +{ + const uint8_t *p; + + p = (const uint8_t *)buf; + crc = crc ^ ~0U; + + while (size--) + crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); + + return crc ^ ~0U; +} + +bits256 curve25519_shared(bits256 privkey,bits256 otherpub) +{ + bits256 shared,hash; + shared = curve25519(privkey,otherpub); + vcalc_sha256(0,hash.bytes,shared.bytes,sizeof(shared)); + //printf("priv.%llx pub.%llx shared.%llx -> hash.%llx\n",privkey.txid,pubkey.txid,shared.txid,hash.txid); + //hash.bytes[0] &= 0xf8, hash.bytes[31] &= 0x7f, hash.bytes[31] |= 64; + return(hash); +} + +int32_t curve25519_donna(uint8_t *mypublic,const uint8_t *secret,const uint8_t *basepoint); +/*{ + bits256 val,p,bp; + memcpy(p.bytes,secret,sizeof(p)); + memcpy(bp.bytes,basepoint,sizeof(bp)); + val = curve25519(p,bp); + memcpy(mypublic,val.bytes,sizeof(val)); + return(0); +}*/ + +uint64_t conv_NXTpassword(unsigned char *mysecret,unsigned char *mypublic,uint8_t *pass,int32_t passlen) +{ + static uint8_t basepoint[32] = {9}; + uint64_t addr; uint8_t hash[32]; + if ( pass != 0 && passlen != 0 ) + vcalc_sha256(0,mysecret,pass,passlen); + mysecret[0] &= 248, mysecret[31] &= 127, mysecret[31] |= 64; + curve25519_donna(mypublic,mysecret,basepoint); + vcalc_sha256(0,hash,mypublic,32); + memcpy(&addr,hash,sizeof(addr)); + return(addr); +} +#undef force_inline diff --git a/crypto777/hmac/crypt_argchk.c b/crypto777/hmac/crypt_argchk.c new file mode 100755 index 000000000..3906b3ff9 --- /dev/null +++ b/crypto777/hmac/crypt_argchk.c @@ -0,0 +1,30 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" +#include + +/** + @file crypt_argchk.c + Perform argument checking, Tom St Denis +*/ + +#if (ARGTYPE == 0) +void crypt_argchk(char *v, char *s, int d) +{ + fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n", + v, d, s); + (void)raise(SIGABRT); +} +#endif + +/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_argchk.c,v $ */ +/* $Revision: 1.5 $ */ +/* $Date: 2006/12/28 01:27:24 $ */ diff --git a/crypto777/hmac/hash_memory.c b/crypto777/hmac/hash_memory.c new file mode 100755 index 000000000..e6154caab --- /dev/null +++ b/crypto777/hmac/hash_memory.c @@ -0,0 +1,65 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hash_memory.c + Hash memory helper, Tom St Denis +*/ + +/** + Hash a block of memory and store the digest. + @param hash The index of the hash you wish to use + @param in The data you wish to hash + @param inlen The length of the data to hash (octets) + @param out [out] Where to store the digest + @param outlen [in/out] Max size and resulting size of the digest + @return CRYPT_OK if successful +*/ +int hash_memory(const struct ltc_hash_descriptor *hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) +{ + hash_state *md; + int err; + + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if (*outlen < hash->hashsize) { + *outlen = hash->hashsize; + return CRYPT_BUFFER_OVERFLOW; + } + + md = XMALLOC(sizeof(hash_state)); + if (md == NULL) { + return CRYPT_MEM; + } + + if ((err = hash->init(md)) != CRYPT_OK) { + goto LBL_ERR; + } + if ((err = hash->process(md, in, inlen)) != CRYPT_OK) { + goto LBL_ERR; + } + err = hash->done(md, out); + *outlen = hash->hashsize; +LBL_ERR: +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + XFREE(md); + + return err; +} + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/helper/hash_memory.c,v $ */ +/* $Revision: 1.6 $ */ +/* $Date: 2006/12/28 01:27:23 $ */ diff --git a/crypto777/hmac/hmac_done.c b/crypto777/hmac/hmac_done.c new file mode 100755 index 000000000..caefc35af --- /dev/null +++ b/crypto777/hmac/hmac_done.c @@ -0,0 +1,103 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hmac_done.c + LTC_HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +#define LTC_HMAC_BLOCKSIZE hash->blocksize + +/** + Terminate an LTC_HMAC session + @param hmac The LTC_HMAC state + @param out [out] The destination of the LTC_HMAC authentication tag + @param outlen [in/out] The max size and resulting size of the LTC_HMAC authentication tag + @return CRYPT_OK if successful +*/ +int hmac_done(hmac_state *hmac,const struct ltc_hash_descriptor *hash,unsigned char *out,unsigned long *outlen) +{ + unsigned char *buf, *isha; + unsigned long hashsize, i; + int err; + + LTC_ARGCHK(hmac != NULL); + LTC_ARGCHK(out != NULL); + + /* get the hash message digest size */ + hashsize = hash->hashsize; + + /* allocate buffers */ + buf = XMALLOC(LTC_HMAC_BLOCKSIZE); + isha = XMALLOC(hashsize); + if (buf == NULL || isha == NULL) { + if (buf != NULL) { + XFREE(buf); + } + if (isha != NULL) { + XFREE(isha); + } + return CRYPT_MEM; + } + + /* Get the hash of the first LTC_HMAC vector plus the data */ + if ((err = hash->done(&hmac->md, isha)) != CRYPT_OK) { + goto LBL_ERR; + } + + /* Create the second LTC_HMAC vector vector for step (3) */ + for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) { + buf[i] = hmac->key[i] ^ 0x5C; + } + + /* Now calculate the "outer" hash for step (5), (6), and (7) */ + if ((err = hash->init(&hmac->md)) != CRYPT_OK) { + goto LBL_ERR; + } + if ((err = hash->process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) { + goto LBL_ERR; + } + if ((err = hash->process(&hmac->md, isha, hashsize)) != CRYPT_OK) { + goto LBL_ERR; + } + if ((err = hash->done(&hmac->md, buf)) != CRYPT_OK) { + goto LBL_ERR; + } + + /* copy to output */ + for (i = 0; i < hashsize && i < *outlen; i++) { + out[i] = buf[i]; + } + *outlen = i; + + err = CRYPT_OK; +LBL_ERR: + XFREE(hmac->key); +#ifdef LTC_CLEAN_STACK + zeromem(isha, hashsize); + zeromem(buf, hashsize); + zeromem(hmac, sizeof(*hmac)); +#endif + + XFREE(isha); + XFREE(buf); + + return err; +} + +#endif + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_done.c,v $ */ +/* $Revision: 1.7 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/hmac_file.c b/crypto777/hmac/hmac_file.c new file mode 100755 index 000000000..5c55c964d --- /dev/null +++ b/crypto777/hmac/hmac_file.c @@ -0,0 +1,87 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hmac_file.c + LTC_HMAC support, process a file, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +/** + LTC_HMAC a file + @param hash The index of the hash you wish to use + @param fname The name of the file you wish to LTC_HMAC + @param key The secret key + @param keylen The length of the secret key + @param out [out] The LTC_HMAC authentication tag + @param outlen [in/out] The max size and resulting size of the authentication tag + @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled +*/ +int hmac_file(const struct ltc_hash_descriptor *hash, const char *fname, const unsigned char *key, unsigned long keylen,unsigned char *out, unsigned long *outlen) +{ +#ifdef LTC_NO_FILE + return CRYPT_NOP; +#else + hmac_state hmac; + FILE *in; + unsigned char buf[512]; + size_t x; + int err; + + LTC_ARGCHK(fname != NULL); + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { + return err; + } + + in = fopen(fname, "rb"); + if (in == NULL) { + return CRYPT_FILE_NOTFOUND; + } + + /* process the file contents */ + do { + x = fread(buf, 1, sizeof(buf), in); + if ((err = hmac_process(&hmac, hash, buf, (unsigned long)x)) != CRYPT_OK) { + /* we don't trap this error since we're already returning an error! */ + fclose(in); + return err; + } + } while (x == sizeof(buf)); + + if (fclose(in) != 0) { + return CRYPT_ERROR; + } + + /* get final hmac */ + if ((err = hmac_done(&hmac, hash, out, outlen)) != CRYPT_OK) { + return err; + } + +#ifdef LTC_CLEAN_STACK + /* clear memory */ + zeromem(buf, sizeof(buf)); +#endif + return CRYPT_OK; +#endif +} + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_file.c,v $ */ +/* $Revision: 1.7 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/hmac_init.c b/crypto777/hmac/hmac_init.c new file mode 100755 index 000000000..dcf7a7df5 --- /dev/null +++ b/crypto777/hmac/hmac_init.c @@ -0,0 +1,108 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hmac_init.c + LTC_HMAC support, initialize state, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +#define LTC_HMAC_BLOCKSIZE hash->blocksize + +/** + Initialize an LTC_HMAC context. + @param hmac The LTC_HMAC state + @param hash The index of the hash you want to use + @param key The secret key + @param keylen The length of the secret key (octets) + @return CRYPT_OK if successful +*/ +int hmac_init(hmac_state *hmac,const struct ltc_hash_descriptor *hash, const unsigned char *key, unsigned long keylen) +{ + unsigned char *buf; + unsigned long hashsize; + unsigned long i, z; + int err; + + LTC_ARGCHK(hmac != NULL); + LTC_ARGCHK(key != NULL); + + //hmac->hash = hash; + hashsize = hash->hashsize; + + /* valid key length? */ + if (keylen == 0) { + return CRYPT_INVALID_KEYSIZE; + } + + /* allocate ram for buf */ + buf = XMALLOC(LTC_HMAC_BLOCKSIZE); + if (buf == NULL) { + return CRYPT_MEM; + } + + /* allocate memory for key */ + hmac->key = XMALLOC(LTC_HMAC_BLOCKSIZE); + if (hmac->key == NULL) { + XFREE(buf); + return CRYPT_MEM; + } + + /* (1) make sure we have a large enough key */ + if(keylen > LTC_HMAC_BLOCKSIZE) { + z = LTC_HMAC_BLOCKSIZE; + if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { + goto LBL_ERR; + } + if(hashsize < LTC_HMAC_BLOCKSIZE) { + zeromem((hmac->key) + hashsize, (size_t)(LTC_HMAC_BLOCKSIZE - hashsize)); + } + keylen = hashsize; + } else { + XMEMCPY(hmac->key, key, (size_t)keylen); + if(keylen < LTC_HMAC_BLOCKSIZE) { + zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen)); + } + } + + /* Create the initial vector for step (3) */ + for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) { + buf[i] = hmac->key[i] ^ 0x36; + } + + /* Pre-pend that to the hash data */ + if ((err = hash->init(&hmac->md)) != CRYPT_OK) { + goto LBL_ERR; + } + + if ((err = hash->process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) { + goto LBL_ERR; + } + goto done; +LBL_ERR: + /* free the key since we failed */ + XFREE(hmac->key); +done: +#ifdef LTC_CLEAN_STACK + zeromem(buf, LTC_HMAC_BLOCKSIZE); +#endif + + XFREE(buf); + return err; +} + +#endif + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_init.c,v $ */ +/* $Revision: 1.7 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/hmac_memory.c b/crypto777/hmac/hmac_memory.c new file mode 100755 index 000000000..952e41ba0 --- /dev/null +++ b/crypto777/hmac/hmac_memory.c @@ -0,0 +1,83 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hmac_memory.c + LTC_HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +/** + LTC_HMAC a block of memory to produce the authentication tag + @param hash The index of the hash to use + @param key The secret key + @param keylen The length of the secret key (octets) + @param in The data to LTC_HMAC + @param inlen The length of the data to LTC_HMAC (octets) + @param out [out] Destination of the authentication tag + @param outlen [in/out] Max size and resulting size of authentication tag + @return CRYPT_OK if successful +*/ +int hmac_memory(const struct ltc_hash_descriptor *hash, + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen) +{ + hmac_state *hmac; + int err; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + /* is there a descriptor? */ + if (hash->hmac_block != NULL) { + return hash->hmac_block(key, keylen, in, inlen, out, outlen); + } + + /* nope, so call the hmac functions */ + /* allocate ram for hmac state */ + hmac = XMALLOC(sizeof(hmac_state)); + if (hmac == NULL) { + return CRYPT_MEM; + } + + if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) { + goto LBL_ERR; + } + + if ((err = hmac_process(hmac, hash, in, inlen)) != CRYPT_OK) { + goto LBL_ERR; + } + + if ((err = hmac_done(hmac, hash, out, outlen)) != CRYPT_OK) { + goto LBL_ERR; + } + + err = CRYPT_OK; +LBL_ERR: +#ifdef LTC_CLEAN_STACK + zeromem(hmac, sizeof(hmac_state)); +#endif + + XFREE(hmac); + return err; +} + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_memory.c,v $ */ +/* $Revision: 1.8 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/hmac_memory_multi.c b/crypto777/hmac/hmac_memory_multi.c new file mode 100755 index 000000000..c2bc24620 --- /dev/null +++ b/crypto777/hmac/hmac_memory_multi.c @@ -0,0 +1,92 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" +#include + +/** + @file hmac_memory_multi.c + LTC_HMAC support, process multiple blocks of memory, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +/** + LTC_HMAC multiple blocks of memory to produce the authentication tag + @param hash The index of the hash to use + @param key The secret key + @param keylen The length of the secret key (octets) + @param out [out] Destination of the authentication tag + @param outlen [in/out] Max size and resulting size of authentication tag + @param in The data to LTC_HMAC + @param inlen The length of the data to LTC_HMAC (octets) + @param ... tuples of (data,len) pairs to LTC_HMAC, terminated with a (NULL,x) (x=don't care) + @return CRYPT_OK if successful +*/ +int hmac_memory_multi(struct ltc_hash_descriptor *hash, + const unsigned char *key, unsigned long keylen, + unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...) + +{ + hmac_state *hmac; + int err; + va_list args; + const unsigned char *curptr; + unsigned long curlen; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + /* allocate ram for hmac state */ + hmac = XMALLOC(sizeof(hmac_state)); + if (hmac == NULL) { + return CRYPT_MEM; + } + + if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) { + goto LBL_ERR; + } + + va_start(args, inlen); + curptr = in; + curlen = inlen; + for (;;) { + /* process buf */ + if ((err = hmac_process(hmac, hash, curptr, curlen)) != CRYPT_OK) { + goto LBL_ERR; + } + /* step to next */ + curptr = va_arg(args, const unsigned char*); + if (curptr == NULL) { + break; + } + curlen = va_arg(args, unsigned long); + } + if ((err = hmac_done(hmac, hash, out, outlen)) != CRYPT_OK) { + goto LBL_ERR; + } +LBL_ERR: +#ifdef LTC_CLEAN_STACK + zeromem(hmac, sizeof(hmac_state)); +#endif + XFREE(hmac); + va_end(args); + return err; +} + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_memory_multi.c,v $ */ +/* $Revision: 1.7 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/hmac_process.c b/crypto777/hmac/hmac_process.c new file mode 100755 index 000000000..351cb9d7b --- /dev/null +++ b/crypto777/hmac/hmac_process.c @@ -0,0 +1,39 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hmac_process.c + LTC_HMAC support, process data, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +/** + Process data through LTC_HMAC + @param hmac The hmac state + @param in The data to send through LTC_HMAC + @param inlen The length of the data to LTC_HMAC (octets) + @return CRYPT_OK if successful +*/ +int hmac_process(hmac_state *hmac,const struct ltc_hash_descriptor *hash,const unsigned char *in, unsigned long inlen) +{ + LTC_ARGCHK(hmac != NULL); + LTC_ARGCHK(in != NULL); + return hash->process(&hmac->md, in, inlen); +} + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_process.c,v $ */ +/* $Revision: 1.7 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/hmac_test.c b/crypto777/hmac/hmac_test.c new file mode 100755 index 000000000..c96128e56 --- /dev/null +++ b/crypto777/hmac/hmac_test.c @@ -0,0 +1,318 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file hmac_test.c + LTC_HMAC support, self-test, Tom St Denis/Dobes Vandermeer +*/ + +#ifdef LTC_HMAC + +#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize + +/* + TEST CASES SOURCE: + +Network Working Group P. Cheng +Request for Comments: 2202 IBM +Category: Informational R. Glenn + NIST + September 1997 + Test Cases for LTC_HMAC-LTC_MD5 and LTC_HMAC-LTC_SHA-1 +*/ + +/** + LTC_HMAC self-test + @return CRYPT_OK if successful, CRYPT_NOP if tests have been disabled. +*/ +#ifdef notow +int hmac_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + unsigned char digest[MAXBLOCKSIZE]; + int i; + + static const struct hmac_test_case { + int num; + char *algo; + unsigned char key[128]; + unsigned long keylen; + unsigned char data[128]; + unsigned long datalen; + unsigned char digest[MAXBLOCKSIZE]; + } cases[] = { + /* + 3. Test Cases for LTC_HMAC-LTC_SHA-1 + + test_case = 1 + key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c + key_len = 20 + data = "Hi Ther 20 + digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 + digest-96 = 0x4c1a03424b55e07fe7f27be1 + */ + { 5, "sha1", + {0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c}, 20, + "Test With Truncation", 20, + {0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f, 0xe7, 0xf2, + 0x7b, 0xe1, 0xd5, 0x8b, 0xb9, 0x32, 0x4a, 0x9a, 0x5a, 0x04} }, + + /* + test_case = 6 + key = 0xaa repeated 80 times + key_len = 80 + data = "Test Using Larger Than Block-Size Key - Hash Key First" + data_len = 54 + digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112 + */ + { 6, "sha1", + {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, 80, + "Test Using Larger Than Block-Size Key - Hash Key First", 54, + {0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0, 0x0e, + 0x95, 0x70, 0x56, 0x37, 0xce, 0x8a, 0x3b, 0x55, + 0xed, 0x40, 0x21, 0x12} }, + + /* + test_case = 7 + key = 0xaa repeated 80 times + key_len = 80 + data = "Test Using Larger Than Block-Size Key and Larger + Than One Block-Size Data" + data_len = 73 + digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91 + */ + { 7, "sha1", + {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, 80, + "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73, + {0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d, 0x78, 0x6d, + 0x6b, 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x08, 0xbb, 0xff, 0x1a, 0x91} }, + + /* + 2. Test Cases for LTC_HMAC-LTC_MD5 + + test_case = 1 + key = 0x0b 0b 0b 0b + 0b 0b 0b 0b + 0b 0b 0b 0b + 0b 0b 0b 0b + key_len = 16 + data = "Hi There" + data_len = 8 + digest = 0x92 94 72 7a + 36 38 bb 1c + 13 f4 8e f8 + 15 8b fc 9d + */ + { 1, "md5", + {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}, 16, + "Hi There", 8, + {0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, + 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d} }, + /* + test_case = 2 + key = "Jefe" + key_len = 4 + data = "what do ya want for nothing?" + data_len = 28 + digest = 0x750c783e6ab0b503eaa86e310a5db738 + */ + { 2, "md5", + "Jefe", 4, + "what do ya want for nothing?", 28, + {0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, + 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38} }, + + /* + test_case = 3 + key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + key_len 16 + data = 0xdd repeated 50 times + data_len = 50 + digest = 0x56be34521d144c88dbb8c733f0e8b3f6 + */ + { 3, "md5", + {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, 16, + {0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd}, 50, + {0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, + 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6} }, + /* + + test_case = 4 + key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819 + key_len 25 + data = 0xcd repeated 50 times + data_len = 50 + digest = 0x697eaf0aca3a3aea3a75164746ffaa79 + */ + { 4, "md5", + {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, + 0x15, 0x16, 0x17, 0x18, 0x19}, 25, + {0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd}, 50, + {0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, + 0x3a, 0x75, 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79} }, + + + /* + + test_case = 5 + key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c + key_len = 16 + data = "Test With Truncation" + data_len = 20 + digest = 0x56461ef2342edc00f9bab995690efd4c + digest-96 0x56461ef2342edc00f9bab995 + */ + { 5, "md5", + {0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c}, 16, + "Test With Truncation", 20, + {0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, + 0xf9, 0xba, 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c} }, + + /* + + test_case = 6 + key = 0xaa repeated 80 times + key_len = 80 + data = "Test Using Larger Than Block-Size Key - Hash +Key First" + data_len = 54 + digest = 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd + */ + { 6, "md5", + {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, 80, + "Test Using Larger Than Block-Size Key - Hash Key First", 54, + {0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, + 0x0b, 0x62, 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd} }, + + /* + + test_case = 7 + key = 0xaa repeated 80 times + key_len = 80 + data = "Test Using Larger Than Block-Size Key and Larger + Than One Block-Size Data" + data_len = 73 + digest = 0x6f630fad67cda0ee1fb1f562db3aa53e + */ + { 7, "md5", + {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, 80, + "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73, + {0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, + 0x1f, 0xb1, 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e} } + }; + + unsigned long outlen; + int err; + int tested=0,failed=0; + for(i=0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) { + int hash = find_hash(cases[i].algo); + if (hash == -1) continue; + ++tested; + outlen = sizeof(digest); + if((err = hmac_memory(hash, cases[i].key, cases[i].keylen, cases[i].data, cases[i].datalen, digest, &outlen)) != CRYPT_OK) { +#if 0 + printf("LTC_HMAC-%s test #%d, %s\n", cases[i].algo, cases[i].num, error_to_string(err)); +#endif + return err; + } + + if(XMEMCMP(digest, cases[i].digest, (size_t)hash_descriptor[hash].hashsize) != 0) { + failed++; +#if 0 + unsigned int j; + printf("\nLTC_HMAC-%s test #%d:\n", cases[i].algo, cases[i].num); + printf( "Result: 0x"); + for(j=0; j < hash_descriptor[hash].hashsize; j++) { + printf("%2x ", digest[j]); + } + printf("\nCorrect: 0x"); + for(j=0; j < hash_descriptor[hash].hashsize; j++) { + printf("%2x ", cases[i].digest[j]); + } + printf("\n"); + return CRYPT_ERROR; +#endif + } else { + /* printf("LTC_HMAC-%s test #%d: Passed\n", cases[i].algo, cases[i].num); */ + } + } + + if (failed != 0) { + return CRYPT_FAIL_TESTVECTOR; + } else if (tested == 0) { + return CRYPT_NOP; + } else { + return CRYPT_OK; + } + #endif +} +#endif + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_test.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/md2.c b/crypto777/hmac/md2.c new file mode 100755 index 000000000..6b8974150 --- /dev/null +++ b/crypto777/hmac/md2.c @@ -0,0 +1,262 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @param md2.c + LTC_MD2 (RFC 1319) hash function implementation by Tom St Denis +*/ + +#ifdef LTC_MD2 + +const struct ltc_hash_descriptor md2_desc = +{ + "md2", + 7, + 16, + 16, + + /* OID */ + { 1, 2, 840, 113549, 2, 2, }, + 6, + + &md2_init, + &md2_process, + &md2_done, + &md2_test, + NULL +}; + +static const unsigned char PI_SUBST[256] = { + 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, + 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, + 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, + 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, + 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, + 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, + 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, + 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, + 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, + 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, + 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, + 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, + 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, + 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, + 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, + 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, + 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, + 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 +}; + +/* adds 16 bytes to the checksum */ +static void md2_update_chksum(hash_state *md) +{ + int j; + unsigned char L; + L = md->md2.chksum[15]; + for (j = 0; j < 16; j++) { + +/* caution, the RFC says its "C[j] = S[M[i*16+j] xor L]" but the reference source code [and test vectors] say + otherwise. +*/ + L = (md->md2.chksum[j] ^= PI_SUBST[(int)(md->md2.buf[j] ^ L)] & 255); + } +} + +static void md2_compress(hash_state *md) +{ + int j, k; + unsigned char t; + + /* copy block */ + for (j = 0; j < 16; j++) { + md->md2.X[16+j] = md->md2.buf[j]; + md->md2.X[32+j] = md->md2.X[j] ^ md->md2.X[16+j]; + } + + t = (unsigned char)0; + + /* do 18 rounds */ + for (j = 0; j < 18; j++) { + for (k = 0; k < 48; k++) { + t = (md->md2.X[k] ^= PI_SUBST[(int)(t & 255)]); + } + t = (t + (unsigned char)j) & 255; + } +} + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int md2_init(hash_state *md) +{ + LTC_ARGCHK(md != NULL); + + /* LTC_MD2 uses a zero'ed state... */ + zeromem(md->md2.X, sizeof(md->md2.X)); + zeromem(md->md2.chksum, sizeof(md->md2.chksum)); + zeromem(md->md2.buf, sizeof(md->md2.buf)); + md->md2.curlen = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +int md2_process(hash_state *md, const unsigned char *in, unsigned long inlen) +{ + unsigned long n; + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(in != NULL); + if (md-> md2 .curlen > sizeof(md-> md2 .buf)) { + return CRYPT_INVALID_ARG; + } + while (inlen > 0) { + n = MIN(inlen, (16 - md->md2.curlen)); + XMEMCPY(md->md2.buf + md->md2.curlen, in, (size_t)n); + md->md2.curlen += n; + in += n; + inlen -= n; + + /* is 16 bytes full? */ + if (md->md2.curlen == 16) { + md2_compress(md); + md2_update_chksum(md); + md->md2.curlen = 0; + } + } + return CRYPT_OK; +} + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (16 bytes) + @return CRYPT_OK if successful +*/ +int md2_done(hash_state * md, unsigned char *out) +{ + unsigned long i, k; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->md2.curlen >= sizeof(md->md2.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* pad the message */ + k = 16 - md->md2.curlen; + for (i = md->md2.curlen; i < 16; i++) { + md->md2.buf[i] = (unsigned char)k; + } + + /* hash and update */ + md2_compress(md); + md2_update_chksum(md); + + /* hash checksum */ + XMEMCPY(md->md2.buf, md->md2.chksum, 16); + md2_compress(md); + + /* output is lower 16 bytes of X */ + XMEMCPY(out, md->md2.X, 16); + +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +void calc_md2(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + md2_init(&md); + md2_process(&md,message,len); + md2_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,16); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int md2_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char md[16]; + } tests[] = { + { "", + {0x83,0x50,0xe5,0xa3,0xe2,0x4c,0x15,0x3d, + 0xf2,0x27,0x5c,0x9f,0x80,0x69,0x27,0x73 + } + }, + { "a", + {0x32,0xec,0x01,0xec,0x4a,0x6d,0xac,0x72, + 0xc0,0xab,0x96,0xfb,0x34,0xc0,0xb5,0xd1 + } + }, + { "message digest", + {0xab,0x4f,0x49,0x6b,0xfb,0x2a,0x53,0x0b, + 0x21,0x9f,0xf3,0x30,0x31,0xfe,0x06,0xb0 + } + }, + { "abcdefghijklmnopqrstuvwxyz", + {0x4e,0x8d,0xdf,0xf3,0x65,0x02,0x92,0xab, + 0x5a,0x41,0x08,0xc3,0xaa,0x47,0x94,0x0b + } + }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + {0xda,0x33,0xde,0xf2,0xa4,0x2d,0xf1,0x39, + 0x75,0x35,0x28,0x46,0xc3,0x03,0x38,0xcd + } + }, + { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", + {0xd5,0x97,0x6f,0x79,0xd8,0x3d,0x3a,0x0d, + 0xc9,0x80,0x6c,0x3c,0x66,0xf3,0xef,0xd8 + } + } + }; + int i; + hash_state md; + unsigned char buf[16]; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + md2_init(&md); + md2_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + md2_done(&md, buf); + if (XMEMCMP(buf, tests[i].md, 16) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/md2.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/md4.c b/crypto777/hmac/md4.c new file mode 100755 index 000000000..b2593e090 --- /dev/null +++ b/crypto777/hmac/md4.c @@ -0,0 +1,325 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @param md4.c + Submitted by Dobes Vandermeer (dobes@smartt.com) +*/ + +#ifdef LTC_MD4 + +const struct ltc_hash_descriptor md4_desc = +{ + "md4", + 6, + 16, + 64, + + /* OID */ + { 1, 2, 840, 113549, 2, 4, }, + 6, + + &md4_init, + &md4_process, + &md4_done, + &md4_test, + NULL +}; + +#define S11 3 +#define S12 7 +#define S13 11 +#define S14 19 +#define S21 3 +#define S22 5 +#define S23 9 +#define S24 13 +#define S31 3 +#define S32 9 +#define S33 11 +#define S34 15 + +/* F, G and H are basic LTC_MD4 functions. */ +#define F(x, y, z) (z ^ (x & (y ^ z))) +#define G(x, y, z) ((x & y) | (z & (x | y))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + +/* ROTATE_LEFT rotates x left n bits. */ +#define ROTATE_LEFT(x, n) ROLc(x, n) + +/* FF, GG and HH are transformations for rounds 1, 2 and 3 */ +/* Rotation is separate from addition to prevent recomputation */ + +#define FF(a, b, c, d, x, s) { \ + (a) += F ((b), (c), (d)) + (x); \ + (a) = ROTATE_LEFT ((a), (s)); \ + } +#define GG(a, b, c, d, x, s) { \ + (a) += G ((b), (c), (d)) + (x) + 0x5a827999UL; \ + (a) = ROTATE_LEFT ((a), (s)); \ + } +#define HH(a, b, c, d, x, s) { \ + (a) += H ((b), (c), (d)) + (x) + 0x6ed9eba1UL; \ + (a) = ROTATE_LEFT ((a), (s)); \ + } + + +#ifdef LTC_CLEAN_STACK +static int _md4_compress(hash_state *md, unsigned char *buf) +#else +static int md4_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 x[16], a, b, c, d; + int i; + + /* copy state */ + a = md->md4.state[0]; + b = md->md4.state[1]; + c = md->md4.state[2]; + d = md->md4.state[3]; + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32L(x[i], buf + (4*i)); + } + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11); /* 1 */ + FF (d, a, b, c, x[ 1], S12); /* 2 */ + FF (c, d, a, b, x[ 2], S13); /* 3 */ + FF (b, c, d, a, x[ 3], S14); /* 4 */ + FF (a, b, c, d, x[ 4], S11); /* 5 */ + FF (d, a, b, c, x[ 5], S12); /* 6 */ + FF (c, d, a, b, x[ 6], S13); /* 7 */ + FF (b, c, d, a, x[ 7], S14); /* 8 */ + FF (a, b, c, d, x[ 8], S11); /* 9 */ + FF (d, a, b, c, x[ 9], S12); /* 10 */ + FF (c, d, a, b, x[10], S13); /* 11 */ + FF (b, c, d, a, x[11], S14); /* 12 */ + FF (a, b, c, d, x[12], S11); /* 13 */ + FF (d, a, b, c, x[13], S12); /* 14 */ + FF (c, d, a, b, x[14], S13); /* 15 */ + FF (b, c, d, a, x[15], S14); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 0], S21); /* 17 */ + GG (d, a, b, c, x[ 4], S22); /* 18 */ + GG (c, d, a, b, x[ 8], S23); /* 19 */ + GG (b, c, d, a, x[12], S24); /* 20 */ + GG (a, b, c, d, x[ 1], S21); /* 21 */ + GG (d, a, b, c, x[ 5], S22); /* 22 */ + GG (c, d, a, b, x[ 9], S23); /* 23 */ + GG (b, c, d, a, x[13], S24); /* 24 */ + GG (a, b, c, d, x[ 2], S21); /* 25 */ + GG (d, a, b, c, x[ 6], S22); /* 26 */ + GG (c, d, a, b, x[10], S23); /* 27 */ + GG (b, c, d, a, x[14], S24); /* 28 */ + GG (a, b, c, d, x[ 3], S21); /* 29 */ + GG (d, a, b, c, x[ 7], S22); /* 30 */ + GG (c, d, a, b, x[11], S23); /* 31 */ + GG (b, c, d, a, x[15], S24); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 0], S31); /* 33 */ + HH (d, a, b, c, x[ 8], S32); /* 34 */ + HH (c, d, a, b, x[ 4], S33); /* 35 */ + HH (b, c, d, a, x[12], S34); /* 36 */ + HH (a, b, c, d, x[ 2], S31); /* 37 */ + HH (d, a, b, c, x[10], S32); /* 38 */ + HH (c, d, a, b, x[ 6], S33); /* 39 */ + HH (b, c, d, a, x[14], S34); /* 40 */ + HH (a, b, c, d, x[ 1], S31); /* 41 */ + HH (d, a, b, c, x[ 9], S32); /* 42 */ + HH (c, d, a, b, x[ 5], S33); /* 43 */ + HH (b, c, d, a, x[13], S34); /* 44 */ + HH (a, b, c, d, x[ 3], S31); /* 45 */ + HH (d, a, b, c, x[11], S32); /* 46 */ + HH (c, d, a, b, x[ 7], S33); /* 47 */ + HH (b, c, d, a, x[15], S34); /* 48 */ + + + /* Update our state */ + md->md4.state[0] = md->md4.state[0] + a; + md->md4.state[1] = md->md4.state[1] + b; + md->md4.state[2] = md->md4.state[2] + c; + md->md4.state[3] = md->md4.state[3] + d; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int md4_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _md4_compress(md, buf); + burn_stack(sizeof(ulong32) * 20 + sizeof(int)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int md4_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->md4.state[0] = 0x67452301UL; + md->md4.state[1] = 0xefcdab89UL; + md->md4.state[2] = 0x98badcfeUL; + md->md4.state[3] = 0x10325476UL; + md->md4.length = 0; + md->md4.curlen = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(md4_process, md4_compress, md4, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (16 bytes) + @return CRYPT_OK if successful +*/ +int md4_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->md4.curlen >= sizeof(md->md4.buf)) { + return CRYPT_INVALID_ARG; + } + + /* increase the length of the message */ + md->md4.length += md->md4.curlen * 8; + + /* append the '1' bit */ + md->md4.buf[md->md4.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->md4.curlen > 56) { + while (md->md4.curlen < 64) { + md->md4.buf[md->md4.curlen++] = (unsigned char)0; + } + md4_compress(md, md->md4.buf); + md->md4.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->md4.curlen < 56) { + md->md4.buf[md->md4.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->md4.length, md->md4.buf+56); + md4_compress(md, md->md4.buf); + + /* copy output */ + for (i = 0; i < 4; i++) { + STORE32L(md->md4.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + + +void calc_md4(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + md4_init(&md); + md4_process(&md,message,len); + md4_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,16); +} +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int md4_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct md4_test_case { + char *input; + unsigned char digest[16]; + } cases[] = { + { "", + {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, + 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} }, + { "a", + {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, + 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} }, + { "abc", + {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, + 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} }, + { "message digest", + {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, + 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} }, + { "abcdefghijklmnopqrstuvwxyz", + {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, + 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, + 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} }, + { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", + {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, + 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} }, + }; + int i; + hash_state md; + unsigned char digest[16]; + + for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) { + md4_init(&md); + md4_process(&md, (unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input)); + md4_done(&md, digest); + if (XMEMCMP(digest, cases[i].digest, 16) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + + } + return CRYPT_OK; + #endif +} +#undef F +#undef G +#undef H +#undef FF +#undef GG +#undef HH + +#endif + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/md4.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/md5.c b/crypto777/hmac/md5.c new file mode 100755 index 000000000..96fac8d3b --- /dev/null +++ b/crypto777/hmac/md5.c @@ -0,0 +1,380 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file md5.c + LTC_MD5 hash function by Tom St Denis +*/ + +#ifdef LTC_MD5 + +const struct ltc_hash_descriptor md5_desc = +{ + "md5", + 3, + 16, + 64, + + /* OID */ + { 1, 2, 840, 113549, 2, 5, }, + 6, + + &md5_init, + &md5_process, + &md5_done, + &md5_test, + NULL +}; + +#define F(x,y,z) (z ^ (x & (y ^ z))) +#define G(x,y,z) (y ^ (z & (y ^ x))) +#define H(x,y,z) (x^y^z) +#define I(x,y,z) (y^(x|(~z))) + +#ifdef LTC_SMALL_CODE + +#define FF(a,b,c,d,M,s,t) \ + a = (a + F(b,c,d) + M + t); a = ROL(a, s) + b; + +#define GG(a,b,c,d,M,s,t) \ + a = (a + G(b,c,d) + M + t); a = ROL(a, s) + b; + +#define HH(a,b,c,d,M,s,t) \ + a = (a + H(b,c,d) + M + t); a = ROL(a, s) + b; + +#define II(a,b,c,d,M,s,t) \ + a = (a + I(b,c,d) + M + t); a = ROL(a, s) + b; + +static const unsigned char Worder[64] = { + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12, + 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2, + 0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 +}; + +static const unsigned char Rorder[64] = { + 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22, + 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20, + 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23, + 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21 +}; + +static const ulong32 Korder[64] = { +0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL, 0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL, +0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL, 0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL, +0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL, 0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL, +0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL, 0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL, +0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL, 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL, +0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL, 0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL, +0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL, 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL, +0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL, 0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL +}; + +#else + +#define FF(a,b,c,d,M,s,t) \ + a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b; + +#define GG(a,b,c,d,M,s,t) \ + a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b; + +#define HH(a,b,c,d,M,s,t) \ + a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b; + +#define II(a,b,c,d,M,s,t) \ + a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b; + + +#endif + +#ifdef LTC_CLEAN_STACK +static int _md5_compress(hash_state *md, unsigned char *buf) +#else +static int md5_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 i, W[16], a, b, c, d; +#ifdef LTC_SMALL_CODE + ulong32 t; +#endif + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32L(W[i], buf + (4*i)); + } + + /* copy state */ + a = md->md5.state[0]; + b = md->md5.state[1]; + c = md->md5.state[2]; + d = md->md5.state[3]; + +#ifdef LTC_SMALL_CODE + for (i = 0; i < 16; ++i) { + FF(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); + t = d; d = c; c = b; b = a; a = t; + } + + for (; i < 32; ++i) { + GG(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); + t = d; d = c; c = b; b = a; a = t; + } + + for (; i < 48; ++i) { + HH(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); + t = d; d = c; c = b; b = a; a = t; + } + + for (; i < 64; ++i) { + II(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); + t = d; d = c; c = b; b = a; a = t; + } + +#else + FF(a,b,c,d,W[0],7,0xd76aa478UL) + FF(d,a,b,c,W[1],12,0xe8c7b756UL) + FF(c,d,a,b,W[2],17,0x242070dbUL) + FF(b,c,d,a,W[3],22,0xc1bdceeeUL) + FF(a,b,c,d,W[4],7,0xf57c0fafUL) + FF(d,a,b,c,W[5],12,0x4787c62aUL) + FF(c,d,a,b,W[6],17,0xa8304613UL) + FF(b,c,d,a,W[7],22,0xfd469501UL) + FF(a,b,c,d,W[8],7,0x698098d8UL) + FF(d,a,b,c,W[9],12,0x8b44f7afUL) + FF(c,d,a,b,W[10],17,0xffff5bb1UL) + FF(b,c,d,a,W[11],22,0x895cd7beUL) + FF(a,b,c,d,W[12],7,0x6b901122UL) + FF(d,a,b,c,W[13],12,0xfd987193UL) + FF(c,d,a,b,W[14],17,0xa679438eUL) + FF(b,c,d,a,W[15],22,0x49b40821UL) + GG(a,b,c,d,W[1],5,0xf61e2562UL) + GG(d,a,b,c,W[6],9,0xc040b340UL) + GG(c,d,a,b,W[11],14,0x265e5a51UL) + GG(b,c,d,a,W[0],20,0xe9b6c7aaUL) + GG(a,b,c,d,W[5],5,0xd62f105dUL) + GG(d,a,b,c,W[10],9,0x02441453UL) + GG(c,d,a,b,W[15],14,0xd8a1e681UL) + GG(b,c,d,a,W[4],20,0xe7d3fbc8UL) + GG(a,b,c,d,W[9],5,0x21e1cde6UL) + GG(d,a,b,c,W[14],9,0xc33707d6UL) + GG(c,d,a,b,W[3],14,0xf4d50d87UL) + GG(b,c,d,a,W[8],20,0x455a14edUL) + GG(a,b,c,d,W[13],5,0xa9e3e905UL) + GG(d,a,b,c,W[2],9,0xfcefa3f8UL) + GG(c,d,a,b,W[7],14,0x676f02d9UL) + GG(b,c,d,a,W[12],20,0x8d2a4c8aUL) + HH(a,b,c,d,W[5],4,0xfffa3942UL) + HH(d,a,b,c,W[8],11,0x8771f681UL) + HH(c,d,a,b,W[11],16,0x6d9d6122UL) + HH(b,c,d,a,W[14],23,0xfde5380cUL) + HH(a,b,c,d,W[1],4,0xa4beea44UL) + HH(d,a,b,c,W[4],11,0x4bdecfa9UL) + HH(c,d,a,b,W[7],16,0xf6bb4b60UL) + HH(b,c,d,a,W[10],23,0xbebfbc70UL) + HH(a,b,c,d,W[13],4,0x289b7ec6UL) + HH(d,a,b,c,W[0],11,0xeaa127faUL) + HH(c,d,a,b,W[3],16,0xd4ef3085UL) + HH(b,c,d,a,W[6],23,0x04881d05UL) + HH(a,b,c,d,W[9],4,0xd9d4d039UL) + HH(d,a,b,c,W[12],11,0xe6db99e5UL) + HH(c,d,a,b,W[15],16,0x1fa27cf8UL) + HH(b,c,d,a,W[2],23,0xc4ac5665UL) + II(a,b,c,d,W[0],6,0xf4292244UL) + II(d,a,b,c,W[7],10,0x432aff97UL) + II(c,d,a,b,W[14],15,0xab9423a7UL) + II(b,c,d,a,W[5],21,0xfc93a039UL) + II(a,b,c,d,W[12],6,0x655b59c3UL) + II(d,a,b,c,W[3],10,0x8f0ccc92UL) + II(c,d,a,b,W[10],15,0xffeff47dUL) + II(b,c,d,a,W[1],21,0x85845dd1UL) + II(a,b,c,d,W[8],6,0x6fa87e4fUL) + II(d,a,b,c,W[15],10,0xfe2ce6e0UL) + II(c,d,a,b,W[6],15,0xa3014314UL) + II(b,c,d,a,W[13],21,0x4e0811a1UL) + II(a,b,c,d,W[4],6,0xf7537e82UL) + II(d,a,b,c,W[11],10,0xbd3af235UL) + II(c,d,a,b,W[2],15,0x2ad7d2bbUL) + II(b,c,d,a,W[9],21,0xeb86d391UL) +#endif + + md->md5.state[0] = md->md5.state[0] + a; + md->md5.state[1] = md->md5.state[1] + b; + md->md5.state[2] = md->md5.state[2] + c; + md->md5.state[3] = md->md5.state[3] + d; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int md5_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _md5_compress(md, buf); + burn_stack(sizeof(ulong32) * 21); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int md5_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->md5.state[0] = 0x67452301UL; + md->md5.state[1] = 0xefcdab89UL; + md->md5.state[2] = 0x98badcfeUL; + md->md5.state[3] = 0x10325476UL; + md->md5.curlen = 0; + md->md5.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(md5_process, md5_compress, md5, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (16 bytes) + @return CRYPT_OK if successful +*/ +int md5_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->md5.curlen >= sizeof(md->md5.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* increase the length of the message */ + md->md5.length += md->md5.curlen * 8; + + /* append the '1' bit */ + md->md5.buf[md->md5.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->md5.curlen > 56) { + while (md->md5.curlen < 64) { + md->md5.buf[md->md5.curlen++] = (unsigned char)0; + } + md5_compress(md, md->md5.buf); + md->md5.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->md5.curlen < 56) { + md->md5.buf[md->md5.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->md5.length, md->md5.buf+56); + md5_compress(md, md->md5.buf); + + /* copy output */ + for (i = 0; i < 4; i++) { + STORE32L(md->md5.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +#include "../OS_portable.h" +#include "../../includes/curve25519.h" +bits128 calc_md5(char digeststr[33],void *buf,int32_t len) +{ + int32_t init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; bits128 digest; + md5_init(&md); + md5_process(&md,buf,len); + md5_done(&md,digest.bytes); + if ( digeststr != 0 ) + init_hexbytes_noT(digeststr,digest.bytes,sizeof(digest)); + return(digest); +} +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int md5_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[16]; + } tests[] = { + { "", + { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, + 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } }, + { "a", + {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, + 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } }, + { "abc", + { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, + 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } }, + { "message digest", + { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, + 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } }, + { "abcdefghijklmnopqrstuvwxyz", + { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, + 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, + 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } }, + { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", + { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, + 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } }, + { NULL, { 0 } } + }; + + int i; + unsigned char tmp[16]; + hash_state md; + + for (i = 0; tests[i].msg != NULL; i++) { + md5_init(&md); + md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + md5_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 16) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + +#endif + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/rmd128.c b/crypto777/hmac/rmd128.c new file mode 100755 index 000000000..a1957d3df --- /dev/null +++ b/crypto777/hmac/rmd128.c @@ -0,0 +1,434 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @param rmd128.c + RMD128 Hash function +*/ + +/* Implementation of LTC_RIPEMD-128 based on the source by Antoon Bosselaers, ESAT-COSIC + * + * This source has been radically overhauled to be portable and work within + * the LibTomCrypt API by Tom St Denis + */ + +#ifdef LTC_RIPEMD128 + +const struct ltc_hash_descriptor rmd128_desc = +{ + "rmd128", + 8, + 16, + 64, + + /* OID */ + { 1, 0, 10118, 3, 0, 50 }, + 6, + + &rmd128_init, + &rmd128_process, + &rmd128_done, + &rmd128_test, + NULL +}; + +/* the four basic functions F(), G() and H() */ +#define F(x, y, z) ((x) ^ (y) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define H(x, y, z) (((x) | ~(y)) ^ (z)) +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) + +/* the eight basic operations FF() through III() */ +#define FF(a, b, c, d, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)); + +#define GG(a, b, c, d, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ + (a) = ROLc((a), (s)); + +#define HH(a, b, c, d, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ + (a) = ROLc((a), (s)); + +#define II(a, b, c, d, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ + (a) = ROLc((a), (s)); + +#define FFF(a, b, c, d, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)); + +#define GGG(a, b, c, d, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x6d703ef3UL;\ + (a) = ROLc((a), (s)); + +#define HHH(a, b, c, d, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x5c4dd124UL;\ + (a) = ROLc((a), (s)); + +#define III(a, b, c, d, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x50a28be6UL;\ + (a) = ROLc((a), (s)); + +#ifdef LTC_CLEAN_STACK +static int _rmd128_compress(hash_state *md, unsigned char *buf) +#else +static int rmd128_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 aa,bb,cc,dd,aaa,bbb,ccc,ddd,X[16]; + int i; + + /* load words X */ + for (i = 0; i < 16; i++){ + LOAD32L(X[i], buf + (4 * i)); + } + + /* load state */ + aa = aaa = md->rmd128.state[0]; + bb = bbb = md->rmd128.state[1]; + cc = ccc = md->rmd128.state[2]; + dd = ddd = md->rmd128.state[3]; + + /* round 1 */ + FF(aa, bb, cc, dd, X[ 0], 11); + FF(dd, aa, bb, cc, X[ 1], 14); + FF(cc, dd, aa, bb, X[ 2], 15); + FF(bb, cc, dd, aa, X[ 3], 12); + FF(aa, bb, cc, dd, X[ 4], 5); + FF(dd, aa, bb, cc, X[ 5], 8); + FF(cc, dd, aa, bb, X[ 6], 7); + FF(bb, cc, dd, aa, X[ 7], 9); + FF(aa, bb, cc, dd, X[ 8], 11); + FF(dd, aa, bb, cc, X[ 9], 13); + FF(cc, dd, aa, bb, X[10], 14); + FF(bb, cc, dd, aa, X[11], 15); + FF(aa, bb, cc, dd, X[12], 6); + FF(dd, aa, bb, cc, X[13], 7); + FF(cc, dd, aa, bb, X[14], 9); + FF(bb, cc, dd, aa, X[15], 8); + + /* round 2 */ + GG(aa, bb, cc, dd, X[ 7], 7); + GG(dd, aa, bb, cc, X[ 4], 6); + GG(cc, dd, aa, bb, X[13], 8); + GG(bb, cc, dd, aa, X[ 1], 13); + GG(aa, bb, cc, dd, X[10], 11); + GG(dd, aa, bb, cc, X[ 6], 9); + GG(cc, dd, aa, bb, X[15], 7); + GG(bb, cc, dd, aa, X[ 3], 15); + GG(aa, bb, cc, dd, X[12], 7); + GG(dd, aa, bb, cc, X[ 0], 12); + GG(cc, dd, aa, bb, X[ 9], 15); + GG(bb, cc, dd, aa, X[ 5], 9); + GG(aa, bb, cc, dd, X[ 2], 11); + GG(dd, aa, bb, cc, X[14], 7); + GG(cc, dd, aa, bb, X[11], 13); + GG(bb, cc, dd, aa, X[ 8], 12); + + /* round 3 */ + HH(aa, bb, cc, dd, X[ 3], 11); + HH(dd, aa, bb, cc, X[10], 13); + HH(cc, dd, aa, bb, X[14], 6); + HH(bb, cc, dd, aa, X[ 4], 7); + HH(aa, bb, cc, dd, X[ 9], 14); + HH(dd, aa, bb, cc, X[15], 9); + HH(cc, dd, aa, bb, X[ 8], 13); + HH(bb, cc, dd, aa, X[ 1], 15); + HH(aa, bb, cc, dd, X[ 2], 14); + HH(dd, aa, bb, cc, X[ 7], 8); + HH(cc, dd, aa, bb, X[ 0], 13); + HH(bb, cc, dd, aa, X[ 6], 6); + HH(aa, bb, cc, dd, X[13], 5); + HH(dd, aa, bb, cc, X[11], 12); + HH(cc, dd, aa, bb, X[ 5], 7); + HH(bb, cc, dd, aa, X[12], 5); + + /* round 4 */ + II(aa, bb, cc, dd, X[ 1], 11); + II(dd, aa, bb, cc, X[ 9], 12); + II(cc, dd, aa, bb, X[11], 14); + II(bb, cc, dd, aa, X[10], 15); + II(aa, bb, cc, dd, X[ 0], 14); + II(dd, aa, bb, cc, X[ 8], 15); + II(cc, dd, aa, bb, X[12], 9); + II(bb, cc, dd, aa, X[ 4], 8); + II(aa, bb, cc, dd, X[13], 9); + II(dd, aa, bb, cc, X[ 3], 14); + II(cc, dd, aa, bb, X[ 7], 5); + II(bb, cc, dd, aa, X[15], 6); + II(aa, bb, cc, dd, X[14], 8); + II(dd, aa, bb, cc, X[ 5], 6); + II(cc, dd, aa, bb, X[ 6], 5); + II(bb, cc, dd, aa, X[ 2], 12); + + /* parallel round 1 */ + III(aaa, bbb, ccc, ddd, X[ 5], 8); + III(ddd, aaa, bbb, ccc, X[14], 9); + III(ccc, ddd, aaa, bbb, X[ 7], 9); + III(bbb, ccc, ddd, aaa, X[ 0], 11); + III(aaa, bbb, ccc, ddd, X[ 9], 13); + III(ddd, aaa, bbb, ccc, X[ 2], 15); + III(ccc, ddd, aaa, bbb, X[11], 15); + III(bbb, ccc, ddd, aaa, X[ 4], 5); + III(aaa, bbb, ccc, ddd, X[13], 7); + III(ddd, aaa, bbb, ccc, X[ 6], 7); + III(ccc, ddd, aaa, bbb, X[15], 8); + III(bbb, ccc, ddd, aaa, X[ 8], 11); + III(aaa, bbb, ccc, ddd, X[ 1], 14); + III(ddd, aaa, bbb, ccc, X[10], 14); + III(ccc, ddd, aaa, bbb, X[ 3], 12); + III(bbb, ccc, ddd, aaa, X[12], 6); + + /* parallel round 2 */ + HHH(aaa, bbb, ccc, ddd, X[ 6], 9); + HHH(ddd, aaa, bbb, ccc, X[11], 13); + HHH(ccc, ddd, aaa, bbb, X[ 3], 15); + HHH(bbb, ccc, ddd, aaa, X[ 7], 7); + HHH(aaa, bbb, ccc, ddd, X[ 0], 12); + HHH(ddd, aaa, bbb, ccc, X[13], 8); + HHH(ccc, ddd, aaa, bbb, X[ 5], 9); + HHH(bbb, ccc, ddd, aaa, X[10], 11); + HHH(aaa, bbb, ccc, ddd, X[14], 7); + HHH(ddd, aaa, bbb, ccc, X[15], 7); + HHH(ccc, ddd, aaa, bbb, X[ 8], 12); + HHH(bbb, ccc, ddd, aaa, X[12], 7); + HHH(aaa, bbb, ccc, ddd, X[ 4], 6); + HHH(ddd, aaa, bbb, ccc, X[ 9], 15); + HHH(ccc, ddd, aaa, bbb, X[ 1], 13); + HHH(bbb, ccc, ddd, aaa, X[ 2], 11); + + /* parallel round 3 */ + GGG(aaa, bbb, ccc, ddd, X[15], 9); + GGG(ddd, aaa, bbb, ccc, X[ 5], 7); + GGG(ccc, ddd, aaa, bbb, X[ 1], 15); + GGG(bbb, ccc, ddd, aaa, X[ 3], 11); + GGG(aaa, bbb, ccc, ddd, X[ 7], 8); + GGG(ddd, aaa, bbb, ccc, X[14], 6); + GGG(ccc, ddd, aaa, bbb, X[ 6], 6); + GGG(bbb, ccc, ddd, aaa, X[ 9], 14); + GGG(aaa, bbb, ccc, ddd, X[11], 12); + GGG(ddd, aaa, bbb, ccc, X[ 8], 13); + GGG(ccc, ddd, aaa, bbb, X[12], 5); + GGG(bbb, ccc, ddd, aaa, X[ 2], 14); + GGG(aaa, bbb, ccc, ddd, X[10], 13); + GGG(ddd, aaa, bbb, ccc, X[ 0], 13); + GGG(ccc, ddd, aaa, bbb, X[ 4], 7); + GGG(bbb, ccc, ddd, aaa, X[13], 5); + + /* parallel round 4 */ + FFF(aaa, bbb, ccc, ddd, X[ 8], 15); + FFF(ddd, aaa, bbb, ccc, X[ 6], 5); + FFF(ccc, ddd, aaa, bbb, X[ 4], 8); + FFF(bbb, ccc, ddd, aaa, X[ 1], 11); + FFF(aaa, bbb, ccc, ddd, X[ 3], 14); + FFF(ddd, aaa, bbb, ccc, X[11], 14); + FFF(ccc, ddd, aaa, bbb, X[15], 6); + FFF(bbb, ccc, ddd, aaa, X[ 0], 14); + FFF(aaa, bbb, ccc, ddd, X[ 5], 6); + FFF(ddd, aaa, bbb, ccc, X[12], 9); + FFF(ccc, ddd, aaa, bbb, X[ 2], 12); + FFF(bbb, ccc, ddd, aaa, X[13], 9); + FFF(aaa, bbb, ccc, ddd, X[ 9], 12); + FFF(ddd, aaa, bbb, ccc, X[ 7], 5); + FFF(ccc, ddd, aaa, bbb, X[10], 15); + FFF(bbb, ccc, ddd, aaa, X[14], 8); + + /* combine results */ + ddd += cc + md->rmd128.state[1]; /* final result for MDbuf[0] */ + md->rmd128.state[1] = md->rmd128.state[2] + dd + aaa; + md->rmd128.state[2] = md->rmd128.state[3] + aa + bbb; + md->rmd128.state[3] = md->rmd128.state[0] + bb + ccc; + md->rmd128.state[0] = ddd; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int rmd128_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _rmd128_compress(md, buf); + burn_stack(sizeof(ulong32) * 24 + sizeof(int)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int rmd128_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->rmd128.state[0] = 0x67452301UL; + md->rmd128.state[1] = 0xefcdab89UL; + md->rmd128.state[2] = 0x98badcfeUL; + md->rmd128.state[3] = 0x10325476UL; + md->rmd128.curlen = 0; + md->rmd128.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(rmd128_process, rmd128_compress, rmd128, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (16 bytes) + @return CRYPT_OK if successful +*/ +int rmd128_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->rmd128.curlen >= sizeof(md->rmd128.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* increase the length of the message */ + md->rmd128.length += md->rmd128.curlen * 8; + + /* append the '1' bit */ + md->rmd128.buf[md->rmd128.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->rmd128.curlen > 56) { + while (md->rmd128.curlen < 64) { + md->rmd128.buf[md->rmd128.curlen++] = (unsigned char)0; + } + rmd128_compress(md, md->rmd128.buf); + md->rmd128.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->rmd128.curlen < 56) { + md->rmd128.buf[md->rmd128.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->rmd128.length, md->rmd128.buf+56); + rmd128_compress(md, md->rmd128.buf); + + /* copy output */ + for (i = 0; i < 4; i++) { + STORE32L(md->rmd128.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +void calc_rmd128(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + rmd128_init(&md); + rmd128_process(&md,message,len); + rmd128_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,16); +} +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int rmd128_test(void) +{ +#ifndef LTC_TEST + return CRYPT_NOP; +#else + static const struct { + char *msg; + unsigned char md[16]; + } tests[] = { + { "", + { 0xcd, 0xf2, 0x62, 0x13, 0xa1, 0x50, 0xdc, 0x3e, + 0xcb, 0x61, 0x0f, 0x18, 0xf6, 0xb3, 0x8b, 0x46 } + }, + { "a", + { 0x86, 0xbe, 0x7a, 0xfa, 0x33, 0x9d, 0x0f, 0xc7, + 0xcf, 0xc7, 0x85, 0xe7, 0x2f, 0x57, 0x8d, 0x33 } + }, + { "abc", + { 0xc1, 0x4a, 0x12, 0x19, 0x9c, 0x66, 0xe4, 0xba, + 0x84, 0x63, 0x6b, 0x0f, 0x69, 0x14, 0x4c, 0x77 } + }, + { "message digest", + { 0x9e, 0x32, 0x7b, 0x3d, 0x6e, 0x52, 0x30, 0x62, + 0xaf, 0xc1, 0x13, 0x2d, 0x7d, 0xf9, 0xd1, 0xb8 } + }, + { "abcdefghijklmnopqrstuvwxyz", + { 0xfd, 0x2a, 0xa6, 0x07, 0xf7, 0x1d, 0xc8, 0xf5, + 0x10, 0x71, 0x49, 0x22, 0xb3, 0x71, 0x83, 0x4e } + }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + { 0xd1, 0xe9, 0x59, 0xeb, 0x17, 0x9c, 0x91, 0x1f, + 0xae, 0xa4, 0x62, 0x4c, 0x60, 0xc5, 0xc7, 0x02 } + } + }; + int x; + unsigned char buf[16]; + hash_state md; + + for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + rmd128_init(&md); + rmd128_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); + rmd128_done(&md, buf); + if (XMEMCMP(buf, tests[x].md, 16) != 0) { + #if 0 + printf("Failed test %d\n", x); + #endif + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; +#endif +} + +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef F +#undef G +#undef H +#undef I +#undef J + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/rmd128.c,v $ */ +/* $Revision: 1.11 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/rmd160.c b/crypto777/hmac/rmd160.c new file mode 100755 index 000000000..271232281 --- /dev/null +++ b/crypto777/hmac/rmd160.c @@ -0,0 +1,483 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" +#include + +/** + @file rmd160.c + RMD160 hash function +*/ + +/* Implementation of LTC_RIPEMD-160 based on the source by Antoon Bosselaers, ESAT-COSIC + * + * This source has been radically overhauled to be portable and work within + * the LibTomCrypt API by Tom St Denis + */ + +//#ifdef LTC_RIPEMD160 + +const struct ltc_hash_descriptor rmd160_desc = +{ + "rmd160", + 9, + 20, + 64, + + /* OID */ + { 1, 3, 36, 3, 2, 1, }, + 6, + + &rmd160_init, + &rmd160_process, + &rmd160_done, + &rmd160_test, + NULL +}; + +/* the five basic functions F(), G() and H() */ +#define F(x, y, z) ((x) ^ (y) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define H(x, y, z) (((x) | ~(y)) ^ (z)) +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define J(x, y, z) ((x) ^ ((y) | ~(z))) + +/* the ten basic operations FF() through III() */ +#define FF(a, b, c, d, e, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define GG(a, b, c, d, e, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define HH(a, b, c, d, e, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define II(a, b, c, d, e, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define JJ(a, b, c, d, e, x, s) \ + (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define FFF(a, b, c, d, e, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define GGG(a, b, c, d, e, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define HHH(a, b, c, d, e, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define III(a, b, c, d, e, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define JJJ(a, b, c, d, e, x, s) \ + (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + + +#ifdef LTC_CLEAN_STACK +static int _rmd160_compress(hash_state *md, unsigned char *buf) +#else +static int rmd160_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16]; + int i; + + /* load words X */ + for (i = 0; i < 16; i++){ + LOAD32L(X[i], buf + (4 * i)); + } + + /* load state */ + aa = aaa = md->rmd160.state[0]; + bb = bbb = md->rmd160.state[1]; + cc = ccc = md->rmd160.state[2]; + dd = ddd = md->rmd160.state[3]; + ee = eee = md->rmd160.state[4]; + + /* round 1 */ + FF(aa, bb, cc, dd, ee, X[ 0], 11); + FF(ee, aa, bb, cc, dd, X[ 1], 14); + FF(dd, ee, aa, bb, cc, X[ 2], 15); + FF(cc, dd, ee, aa, bb, X[ 3], 12); + FF(bb, cc, dd, ee, aa, X[ 4], 5); + FF(aa, bb, cc, dd, ee, X[ 5], 8); + FF(ee, aa, bb, cc, dd, X[ 6], 7); + FF(dd, ee, aa, bb, cc, X[ 7], 9); + FF(cc, dd, ee, aa, bb, X[ 8], 11); + FF(bb, cc, dd, ee, aa, X[ 9], 13); + FF(aa, bb, cc, dd, ee, X[10], 14); + FF(ee, aa, bb, cc, dd, X[11], 15); + FF(dd, ee, aa, bb, cc, X[12], 6); + FF(cc, dd, ee, aa, bb, X[13], 7); + FF(bb, cc, dd, ee, aa, X[14], 9); + FF(aa, bb, cc, dd, ee, X[15], 8); + + /* round 2 */ + GG(ee, aa, bb, cc, dd, X[ 7], 7); + GG(dd, ee, aa, bb, cc, X[ 4], 6); + GG(cc, dd, ee, aa, bb, X[13], 8); + GG(bb, cc, dd, ee, aa, X[ 1], 13); + GG(aa, bb, cc, dd, ee, X[10], 11); + GG(ee, aa, bb, cc, dd, X[ 6], 9); + GG(dd, ee, aa, bb, cc, X[15], 7); + GG(cc, dd, ee, aa, bb, X[ 3], 15); + GG(bb, cc, dd, ee, aa, X[12], 7); + GG(aa, bb, cc, dd, ee, X[ 0], 12); + GG(ee, aa, bb, cc, dd, X[ 9], 15); + GG(dd, ee, aa, bb, cc, X[ 5], 9); + GG(cc, dd, ee, aa, bb, X[ 2], 11); + GG(bb, cc, dd, ee, aa, X[14], 7); + GG(aa, bb, cc, dd, ee, X[11], 13); + GG(ee, aa, bb, cc, dd, X[ 8], 12); + + /* round 3 */ + HH(dd, ee, aa, bb, cc, X[ 3], 11); + HH(cc, dd, ee, aa, bb, X[10], 13); + HH(bb, cc, dd, ee, aa, X[14], 6); + HH(aa, bb, cc, dd, ee, X[ 4], 7); + HH(ee, aa, bb, cc, dd, X[ 9], 14); + HH(dd, ee, aa, bb, cc, X[15], 9); + HH(cc, dd, ee, aa, bb, X[ 8], 13); + HH(bb, cc, dd, ee, aa, X[ 1], 15); + HH(aa, bb, cc, dd, ee, X[ 2], 14); + HH(ee, aa, bb, cc, dd, X[ 7], 8); + HH(dd, ee, aa, bb, cc, X[ 0], 13); + HH(cc, dd, ee, aa, bb, X[ 6], 6); + HH(bb, cc, dd, ee, aa, X[13], 5); + HH(aa, bb, cc, dd, ee, X[11], 12); + HH(ee, aa, bb, cc, dd, X[ 5], 7); + HH(dd, ee, aa, bb, cc, X[12], 5); + + /* round 4 */ + II(cc, dd, ee, aa, bb, X[ 1], 11); + II(bb, cc, dd, ee, aa, X[ 9], 12); + II(aa, bb, cc, dd, ee, X[11], 14); + II(ee, aa, bb, cc, dd, X[10], 15); + II(dd, ee, aa, bb, cc, X[ 0], 14); + II(cc, dd, ee, aa, bb, X[ 8], 15); + II(bb, cc, dd, ee, aa, X[12], 9); + II(aa, bb, cc, dd, ee, X[ 4], 8); + II(ee, aa, bb, cc, dd, X[13], 9); + II(dd, ee, aa, bb, cc, X[ 3], 14); + II(cc, dd, ee, aa, bb, X[ 7], 5); + II(bb, cc, dd, ee, aa, X[15], 6); + II(aa, bb, cc, dd, ee, X[14], 8); + II(ee, aa, bb, cc, dd, X[ 5], 6); + II(dd, ee, aa, bb, cc, X[ 6], 5); + II(cc, dd, ee, aa, bb, X[ 2], 12); + + /* round 5 */ + JJ(bb, cc, dd, ee, aa, X[ 4], 9); + JJ(aa, bb, cc, dd, ee, X[ 0], 15); + JJ(ee, aa, bb, cc, dd, X[ 5], 5); + JJ(dd, ee, aa, bb, cc, X[ 9], 11); + JJ(cc, dd, ee, aa, bb, X[ 7], 6); + JJ(bb, cc, dd, ee, aa, X[12], 8); + JJ(aa, bb, cc, dd, ee, X[ 2], 13); + JJ(ee, aa, bb, cc, dd, X[10], 12); + JJ(dd, ee, aa, bb, cc, X[14], 5); + JJ(cc, dd, ee, aa, bb, X[ 1], 12); + JJ(bb, cc, dd, ee, aa, X[ 3], 13); + JJ(aa, bb, cc, dd, ee, X[ 8], 14); + JJ(ee, aa, bb, cc, dd, X[11], 11); + JJ(dd, ee, aa, bb, cc, X[ 6], 8); + JJ(cc, dd, ee, aa, bb, X[15], 5); + JJ(bb, cc, dd, ee, aa, X[13], 6); + + /* parallel round 1 */ + JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); + JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9); + JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13); + JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15); + JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5); + JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7); + JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); + JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14); + JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); + JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); + + /* parallel round 2 */ + III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); + III(ddd, eee, aaa, bbb, ccc, X[11], 13); + III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); + III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); + III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); + III(eee, aaa, bbb, ccc, ddd, X[13], 8); + III(ddd, eee, aaa, bbb, ccc, X[ 5], 9); + III(ccc, ddd, eee, aaa, bbb, X[10], 11); + III(bbb, ccc, ddd, eee, aaa, X[14], 7); + III(aaa, bbb, ccc, ddd, eee, X[15], 7); + III(eee, aaa, bbb, ccc, ddd, X[ 8], 12); + III(ddd, eee, aaa, bbb, ccc, X[12], 7); + III(ccc, ddd, eee, aaa, bbb, X[ 4], 6); + III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); + III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); + III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); + + /* parallel round 3 */ + HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); + HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); + HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15); + HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11); + HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8); + HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); + HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6); + HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14); + HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); + HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13); + HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); + HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14); + HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); + HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); + HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); + HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); + + /* parallel round 4 */ + GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); + GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); + GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); + GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); + GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); + GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); + GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); + GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14); + GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6); + GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); + GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12); + GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); + GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12); + GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); + GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); + GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); + + /* parallel round 5 */ + FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); + FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); + FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12); + FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9); + FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12); + FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5); + FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14); + FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6); + FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8); + FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13); + FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6); + FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5); + FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15); + FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); + FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); + FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); + + /* combine results */ + ddd += cc + md->rmd160.state[1]; /* final result for md->rmd160.state[0] */ + md->rmd160.state[1] = md->rmd160.state[2] + dd + eee; + md->rmd160.state[2] = md->rmd160.state[3] + ee + aaa; + md->rmd160.state[3] = md->rmd160.state[4] + aa + bbb; + md->rmd160.state[4] = md->rmd160.state[0] + bb + ccc; + md->rmd160.state[0] = ddd; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int rmd160_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _rmd160_compress(md, buf); + burn_stack(sizeof(ulong32) * 26 + sizeof(int)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int rmd160_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->rmd160.state[0] = 0x67452301UL; + md->rmd160.state[1] = 0xefcdab89UL; + md->rmd160.state[2] = 0x98badcfeUL; + md->rmd160.state[3] = 0x10325476UL; + md->rmd160.state[4] = 0xc3d2e1f0UL; + md->rmd160.curlen = 0; + md->rmd160.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(rmd160_process, rmd160_compress, rmd160, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (20 bytes) + @return CRYPT_OK if successful +*/ +int rmd160_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->rmd160.curlen >= sizeof(md->rmd160.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* increase the length of the message */ + md->rmd160.length += md->rmd160.curlen * 8; + + /* append the '1' bit */ + md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->rmd160.curlen > 56) { + while (md->rmd160.curlen < 64) { + md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0; + } + rmd160_compress(md, md->rmd160.buf); + md->rmd160.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->rmd160.curlen < 56) { + md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->rmd160.length, md->rmd160.buf+56); + rmd160_compress(md, md->rmd160.buf); + + /* copy output */ + for (i = 0; i < 5; i++) { + STORE32L(md->rmd160.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int rmd160_test(void) +{ +#ifndef LTC_TEST + return CRYPT_NOP; +#else + static const struct { + char *msg; + unsigned char md[20]; + } tests[] = { + { "", + { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, + 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 } + }, + { "a", + { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae, + 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe } + }, + { "abc", + { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04, + 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc } + }, + { "message digest", + { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8, + 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 } + }, + { "abcdefghijklmnopqrstuvwxyz", + { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb, + 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, + 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b } + } + }; + int x; + unsigned char buf[20]; + hash_state md; + + for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + rmd160_init(&md); + rmd160_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); + rmd160_done(&md, buf); + if (XMEMCMP(buf, tests[x].md, 20) != 0) { +#if 0 + printf("Failed test %d\n", x); +#endif + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; +#endif +} +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef F +#undef G +#undef H +#undef I +#undef J + +//#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/rmd160.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/rmd256.c b/crypto777/hmac/rmd256.c new file mode 100755 index 000000000..af32daa40 --- /dev/null +++ b/crypto777/hmac/rmd256.c @@ -0,0 +1,454 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @param rmd256.c + RLTC_MD256 Hash function +*/ + +#ifdef LTC_RIPEMD256 + +const struct ltc_hash_descriptor rmd256_desc = +{ + "rmd256", + 8, + 32, + 64, + + /* OID */ + { 1, 3, 36, 3, 2, 3 }, + 6, + + &rmd256_init, + &rmd256_process, + &rmd256_done, + &rmd256_test, + NULL +}; + +/* the four basic functions F(), G() and H() */ +#define F(x, y, z) ((x) ^ (y) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define H(x, y, z) (((x) | ~(y)) ^ (z)) +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) + +/* the eight basic operations FF() through III() */ +#define FF(a, b, c, d, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)); + +#define GG(a, b, c, d, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ + (a) = ROLc((a), (s)); + +#define HH(a, b, c, d, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ + (a) = ROLc((a), (s)); + +#define II(a, b, c, d, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ + (a) = ROLc((a), (s)); + +#define FFF(a, b, c, d, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)); + +#define GGG(a, b, c, d, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x6d703ef3UL;\ + (a) = ROLc((a), (s)); + +#define HHH(a, b, c, d, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x5c4dd124UL;\ + (a) = ROLc((a), (s)); + +#define III(a, b, c, d, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x50a28be6UL;\ + (a) = ROLc((a), (s)); + +#ifdef LTC_CLEAN_STACK +static int _rmd256_compress(hash_state *md, unsigned char *buf) +#else +static int rmd256_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 aa,bb,cc,dd,aaa,bbb,ccc,ddd,tmp,X[16]; + int i; + + /* load words X */ + for (i = 0; i < 16; i++){ + LOAD32L(X[i], buf + (4 * i)); + } + + /* load state */ + aa = md->rmd256.state[0]; + bb = md->rmd256.state[1]; + cc = md->rmd256.state[2]; + dd = md->rmd256.state[3]; + aaa = md->rmd256.state[4]; + bbb = md->rmd256.state[5]; + ccc = md->rmd256.state[6]; + ddd = md->rmd256.state[7]; + + /* round 1 */ + FF(aa, bb, cc, dd, X[ 0], 11); + FF(dd, aa, bb, cc, X[ 1], 14); + FF(cc, dd, aa, bb, X[ 2], 15); + FF(bb, cc, dd, aa, X[ 3], 12); + FF(aa, bb, cc, dd, X[ 4], 5); + FF(dd, aa, bb, cc, X[ 5], 8); + FF(cc, dd, aa, bb, X[ 6], 7); + FF(bb, cc, dd, aa, X[ 7], 9); + FF(aa, bb, cc, dd, X[ 8], 11); + FF(dd, aa, bb, cc, X[ 9], 13); + FF(cc, dd, aa, bb, X[10], 14); + FF(bb, cc, dd, aa, X[11], 15); + FF(aa, bb, cc, dd, X[12], 6); + FF(dd, aa, bb, cc, X[13], 7); + FF(cc, dd, aa, bb, X[14], 9); + FF(bb, cc, dd, aa, X[15], 8); + + /* parallel round 1 */ + III(aaa, bbb, ccc, ddd, X[ 5], 8); + III(ddd, aaa, bbb, ccc, X[14], 9); + III(ccc, ddd, aaa, bbb, X[ 7], 9); + III(bbb, ccc, ddd, aaa, X[ 0], 11); + III(aaa, bbb, ccc, ddd, X[ 9], 13); + III(ddd, aaa, bbb, ccc, X[ 2], 15); + III(ccc, ddd, aaa, bbb, X[11], 15); + III(bbb, ccc, ddd, aaa, X[ 4], 5); + III(aaa, bbb, ccc, ddd, X[13], 7); + III(ddd, aaa, bbb, ccc, X[ 6], 7); + III(ccc, ddd, aaa, bbb, X[15], 8); + III(bbb, ccc, ddd, aaa, X[ 8], 11); + III(aaa, bbb, ccc, ddd, X[ 1], 14); + III(ddd, aaa, bbb, ccc, X[10], 14); + III(ccc, ddd, aaa, bbb, X[ 3], 12); + III(bbb, ccc, ddd, aaa, X[12], 6); + + tmp = aa; aa = aaa; aaa = tmp; + + /* round 2 */ + GG(aa, bb, cc, dd, X[ 7], 7); + GG(dd, aa, bb, cc, X[ 4], 6); + GG(cc, dd, aa, bb, X[13], 8); + GG(bb, cc, dd, aa, X[ 1], 13); + GG(aa, bb, cc, dd, X[10], 11); + GG(dd, aa, bb, cc, X[ 6], 9); + GG(cc, dd, aa, bb, X[15], 7); + GG(bb, cc, dd, aa, X[ 3], 15); + GG(aa, bb, cc, dd, X[12], 7); + GG(dd, aa, bb, cc, X[ 0], 12); + GG(cc, dd, aa, bb, X[ 9], 15); + GG(bb, cc, dd, aa, X[ 5], 9); + GG(aa, bb, cc, dd, X[ 2], 11); + GG(dd, aa, bb, cc, X[14], 7); + GG(cc, dd, aa, bb, X[11], 13); + GG(bb, cc, dd, aa, X[ 8], 12); + + /* parallel round 2 */ + HHH(aaa, bbb, ccc, ddd, X[ 6], 9); + HHH(ddd, aaa, bbb, ccc, X[11], 13); + HHH(ccc, ddd, aaa, bbb, X[ 3], 15); + HHH(bbb, ccc, ddd, aaa, X[ 7], 7); + HHH(aaa, bbb, ccc, ddd, X[ 0], 12); + HHH(ddd, aaa, bbb, ccc, X[13], 8); + HHH(ccc, ddd, aaa, bbb, X[ 5], 9); + HHH(bbb, ccc, ddd, aaa, X[10], 11); + HHH(aaa, bbb, ccc, ddd, X[14], 7); + HHH(ddd, aaa, bbb, ccc, X[15], 7); + HHH(ccc, ddd, aaa, bbb, X[ 8], 12); + HHH(bbb, ccc, ddd, aaa, X[12], 7); + HHH(aaa, bbb, ccc, ddd, X[ 4], 6); + HHH(ddd, aaa, bbb, ccc, X[ 9], 15); + HHH(ccc, ddd, aaa, bbb, X[ 1], 13); + HHH(bbb, ccc, ddd, aaa, X[ 2], 11); + + tmp = bb; bb = bbb; bbb = tmp; + + /* round 3 */ + HH(aa, bb, cc, dd, X[ 3], 11); + HH(dd, aa, bb, cc, X[10], 13); + HH(cc, dd, aa, bb, X[14], 6); + HH(bb, cc, dd, aa, X[ 4], 7); + HH(aa, bb, cc, dd, X[ 9], 14); + HH(dd, aa, bb, cc, X[15], 9); + HH(cc, dd, aa, bb, X[ 8], 13); + HH(bb, cc, dd, aa, X[ 1], 15); + HH(aa, bb, cc, dd, X[ 2], 14); + HH(dd, aa, bb, cc, X[ 7], 8); + HH(cc, dd, aa, bb, X[ 0], 13); + HH(bb, cc, dd, aa, X[ 6], 6); + HH(aa, bb, cc, dd, X[13], 5); + HH(dd, aa, bb, cc, X[11], 12); + HH(cc, dd, aa, bb, X[ 5], 7); + HH(bb, cc, dd, aa, X[12], 5); + + /* parallel round 3 */ + GGG(aaa, bbb, ccc, ddd, X[15], 9); + GGG(ddd, aaa, bbb, ccc, X[ 5], 7); + GGG(ccc, ddd, aaa, bbb, X[ 1], 15); + GGG(bbb, ccc, ddd, aaa, X[ 3], 11); + GGG(aaa, bbb, ccc, ddd, X[ 7], 8); + GGG(ddd, aaa, bbb, ccc, X[14], 6); + GGG(ccc, ddd, aaa, bbb, X[ 6], 6); + GGG(bbb, ccc, ddd, aaa, X[ 9], 14); + GGG(aaa, bbb, ccc, ddd, X[11], 12); + GGG(ddd, aaa, bbb, ccc, X[ 8], 13); + GGG(ccc, ddd, aaa, bbb, X[12], 5); + GGG(bbb, ccc, ddd, aaa, X[ 2], 14); + GGG(aaa, bbb, ccc, ddd, X[10], 13); + GGG(ddd, aaa, bbb, ccc, X[ 0], 13); + GGG(ccc, ddd, aaa, bbb, X[ 4], 7); + GGG(bbb, ccc, ddd, aaa, X[13], 5); + + tmp = cc; cc = ccc; ccc = tmp; + + /* round 4 */ + II(aa, bb, cc, dd, X[ 1], 11); + II(dd, aa, bb, cc, X[ 9], 12); + II(cc, dd, aa, bb, X[11], 14); + II(bb, cc, dd, aa, X[10], 15); + II(aa, bb, cc, dd, X[ 0], 14); + II(dd, aa, bb, cc, X[ 8], 15); + II(cc, dd, aa, bb, X[12], 9); + II(bb, cc, dd, aa, X[ 4], 8); + II(aa, bb, cc, dd, X[13], 9); + II(dd, aa, bb, cc, X[ 3], 14); + II(cc, dd, aa, bb, X[ 7], 5); + II(bb, cc, dd, aa, X[15], 6); + II(aa, bb, cc, dd, X[14], 8); + II(dd, aa, bb, cc, X[ 5], 6); + II(cc, dd, aa, bb, X[ 6], 5); + II(bb, cc, dd, aa, X[ 2], 12); + + /* parallel round 4 */ + FFF(aaa, bbb, ccc, ddd, X[ 8], 15); + FFF(ddd, aaa, bbb, ccc, X[ 6], 5); + FFF(ccc, ddd, aaa, bbb, X[ 4], 8); + FFF(bbb, ccc, ddd, aaa, X[ 1], 11); + FFF(aaa, bbb, ccc, ddd, X[ 3], 14); + FFF(ddd, aaa, bbb, ccc, X[11], 14); + FFF(ccc, ddd, aaa, bbb, X[15], 6); + FFF(bbb, ccc, ddd, aaa, X[ 0], 14); + FFF(aaa, bbb, ccc, ddd, X[ 5], 6); + FFF(ddd, aaa, bbb, ccc, X[12], 9); + FFF(ccc, ddd, aaa, bbb, X[ 2], 12); + FFF(bbb, ccc, ddd, aaa, X[13], 9); + FFF(aaa, bbb, ccc, ddd, X[ 9], 12); + FFF(ddd, aaa, bbb, ccc, X[ 7], 5); + FFF(ccc, ddd, aaa, bbb, X[10], 15); + FFF(bbb, ccc, ddd, aaa, X[14], 8); + + tmp = dd; dd = ddd; ddd = tmp; + + /* combine results */ + md->rmd256.state[0] += aa; + md->rmd256.state[1] += bb; + md->rmd256.state[2] += cc; + md->rmd256.state[3] += dd; + md->rmd256.state[4] += aaa; + md->rmd256.state[5] += bbb; + md->rmd256.state[6] += ccc; + md->rmd256.state[7] += ddd; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int rmd256_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _rmd256_compress(md, buf); + burn_stack(sizeof(ulong32) * 25 + sizeof(int)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int rmd256_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->rmd256.state[0] = 0x67452301UL; + md->rmd256.state[1] = 0xefcdab89UL; + md->rmd256.state[2] = 0x98badcfeUL; + md->rmd256.state[3] = 0x10325476UL; + md->rmd256.state[4] = 0x76543210UL; + md->rmd256.state[5] = 0xfedcba98UL; + md->rmd256.state[6] = 0x89abcdefUL; + md->rmd256.state[7] = 0x01234567UL; + md->rmd256.curlen = 0; + md->rmd256.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(rmd256_process, rmd256_compress, rmd256, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (16 bytes) + @return CRYPT_OK if successful +*/ +int rmd256_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->rmd256.curlen >= sizeof(md->rmd256.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* increase the length of the message */ + md->rmd256.length += md->rmd256.curlen * 8; + + /* append the '1' bit */ + md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->rmd256.curlen > 56) { + while (md->rmd256.curlen < 64) { + md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0; + } + rmd256_compress(md, md->rmd256.buf); + md->rmd256.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->rmd256.curlen < 56) { + md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->rmd256.length, md->rmd256.buf+56); + rmd256_compress(md, md->rmd256.buf); + + /* copy output */ + for (i = 0; i < 8; i++) { + STORE32L(md->rmd256.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +void calc_rmd256(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + rmd256_init(&md); + rmd256_process(&md,message,len); + rmd256_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,32); +} +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int rmd256_test(void) +{ +#ifndef LTC_TEST + return CRYPT_NOP; +#else + static const struct { + char *msg; + unsigned char md[32]; + } tests[] = { + { "", + { 0x02, 0xba, 0x4c, 0x4e, 0x5f, 0x8e, 0xcd, 0x18, + 0x77, 0xfc, 0x52, 0xd6, 0x4d, 0x30, 0xe3, 0x7a, + 0x2d, 0x97, 0x74, 0xfb, 0x1e, 0x5d, 0x02, 0x63, + 0x80, 0xae, 0x01, 0x68, 0xe3, 0xc5, 0x52, 0x2d } + }, + { "a", + { 0xf9, 0x33, 0x3e, 0x45, 0xd8, 0x57, 0xf5, 0xd9, + 0x0a, 0x91, 0xba, 0xb7, 0x0a, 0x1e, 0xba, 0x0c, + 0xfb, 0x1b, 0xe4, 0xb0, 0x78, 0x3c, 0x9a, 0xcf, + 0xcd, 0x88, 0x3a, 0x91, 0x34, 0x69, 0x29, 0x25 } + }, + { "abc", + { 0xaf, 0xbd, 0x6e, 0x22, 0x8b, 0x9d, 0x8c, 0xbb, + 0xce, 0xf5, 0xca, 0x2d, 0x03, 0xe6, 0xdb, 0xa1, + 0x0a, 0xc0, 0xbc, 0x7d, 0xcb, 0xe4, 0x68, 0x0e, + 0x1e, 0x42, 0xd2, 0xe9, 0x75, 0x45, 0x9b, 0x65 } + }, + { "message digest", + { 0x87, 0xe9, 0x71, 0x75, 0x9a, 0x1c, 0xe4, 0x7a, + 0x51, 0x4d, 0x5c, 0x91, 0x4c, 0x39, 0x2c, 0x90, + 0x18, 0xc7, 0xc4, 0x6b, 0xc1, 0x44, 0x65, 0x55, + 0x4a, 0xfc, 0xdf, 0x54, 0xa5, 0x07, 0x0c, 0x0e } + }, + { "abcdefghijklmnopqrstuvwxyz", + { 0x64, 0x9d, 0x30, 0x34, 0x75, 0x1e, 0xa2, 0x16, + 0x77, 0x6b, 0xf9, 0xa1, 0x8a, 0xcc, 0x81, 0xbc, + 0x78, 0x96, 0x11, 0x8a, 0x51, 0x97, 0x96, 0x87, + 0x82, 0xdd, 0x1f, 0xd9, 0x7d, 0x8d, 0x51, 0x33 } + }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + { 0x57, 0x40, 0xa4, 0x08, 0xac, 0x16, 0xb7, 0x20, + 0xb8, 0x44, 0x24, 0xae, 0x93, 0x1c, 0xbb, 0x1f, + 0xe3, 0x63, 0xd1, 0xd0, 0xbf, 0x40, 0x17, 0xf1, + 0xa8, 0x9f, 0x7e, 0xa6, 0xde, 0x77, 0xa0, 0xb8 } + } + }; + int x; + unsigned char buf[32]; + hash_state md; + + for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + rmd256_init(&md); + rmd256_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); + rmd256_done(&md, buf); + if (XMEMCMP(buf, tests[x].md, 32) != 0) { + #if 0 + printf("Failed test %d\n", x); + #endif + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; +#endif +} +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef F +#undef G +#undef H +#undef I +#undef J + +#endif + diff --git a/crypto777/hmac/rmd320.c b/crypto777/hmac/rmd320.c new file mode 100755 index 000000000..7d69d9062 --- /dev/null +++ b/crypto777/hmac/rmd320.c @@ -0,0 +1,519 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file rmd320.c + RMD320 hash function +*/ + +#ifdef LTC_RIPEMD320 + +const struct ltc_hash_descriptor rmd320_desc = +{ + "rmd320", + 9, + 40, + 64, + + /* OID */ + { 0 }, + 0, + + &rmd320_init, + &rmd320_process, + &rmd320_done, + &rmd320_test, + NULL +}; + +/* the five basic functions F(), G() and H() */ +#define F(x, y, z) ((x) ^ (y) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define H(x, y, z) (((x) | ~(y)) ^ (z)) +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define J(x, y, z) ((x) ^ ((y) | ~(z))) + +/* the ten basic operations FF() through III() */ +#define FF(a, b, c, d, e, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define GG(a, b, c, d, e, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define HH(a, b, c, d, e, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define II(a, b, c, d, e, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define JJ(a, b, c, d, e, x, s) \ + (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define FFF(a, b, c, d, e, x, s) \ + (a) += F((b), (c), (d)) + (x);\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define GGG(a, b, c, d, e, x, s) \ + (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define HHH(a, b, c, d, e, x, s) \ + (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define III(a, b, c, d, e, x, s) \ + (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + +#define JJJ(a, b, c, d, e, x, s) \ + (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ + (a) = ROLc((a), (s)) + (e);\ + (c) = ROLc((c), 10); + + +#ifdef LTC_CLEAN_STACK +static int _rmd320_compress(hash_state *md, unsigned char *buf) +#else +static int rmd320_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,tmp,X[16]; + int i; + + /* load words X */ + for (i = 0; i < 16; i++){ + LOAD32L(X[i], buf + (4 * i)); + } + + /* load state */ + aa = md->rmd320.state[0]; + bb = md->rmd320.state[1]; + cc = md->rmd320.state[2]; + dd = md->rmd320.state[3]; + ee = md->rmd320.state[4]; + aaa = md->rmd320.state[5]; + bbb = md->rmd320.state[6]; + ccc = md->rmd320.state[7]; + ddd = md->rmd320.state[8]; + eee = md->rmd320.state[9]; + + /* round 1 */ + FF(aa, bb, cc, dd, ee, X[ 0], 11); + FF(ee, aa, bb, cc, dd, X[ 1], 14); + FF(dd, ee, aa, bb, cc, X[ 2], 15); + FF(cc, dd, ee, aa, bb, X[ 3], 12); + FF(bb, cc, dd, ee, aa, X[ 4], 5); + FF(aa, bb, cc, dd, ee, X[ 5], 8); + FF(ee, aa, bb, cc, dd, X[ 6], 7); + FF(dd, ee, aa, bb, cc, X[ 7], 9); + FF(cc, dd, ee, aa, bb, X[ 8], 11); + FF(bb, cc, dd, ee, aa, X[ 9], 13); + FF(aa, bb, cc, dd, ee, X[10], 14); + FF(ee, aa, bb, cc, dd, X[11], 15); + FF(dd, ee, aa, bb, cc, X[12], 6); + FF(cc, dd, ee, aa, bb, X[13], 7); + FF(bb, cc, dd, ee, aa, X[14], 9); + FF(aa, bb, cc, dd, ee, X[15], 8); + + /* parallel round 1 */ + JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); + JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9); + JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13); + JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15); + JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5); + JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7); + JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); + JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11); + JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14); + JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); + JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); + JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); + + tmp = aa; aa = aaa; aaa = tmp; + + /* round 2 */ + GG(ee, aa, bb, cc, dd, X[ 7], 7); + GG(dd, ee, aa, bb, cc, X[ 4], 6); + GG(cc, dd, ee, aa, bb, X[13], 8); + GG(bb, cc, dd, ee, aa, X[ 1], 13); + GG(aa, bb, cc, dd, ee, X[10], 11); + GG(ee, aa, bb, cc, dd, X[ 6], 9); + GG(dd, ee, aa, bb, cc, X[15], 7); + GG(cc, dd, ee, aa, bb, X[ 3], 15); + GG(bb, cc, dd, ee, aa, X[12], 7); + GG(aa, bb, cc, dd, ee, X[ 0], 12); + GG(ee, aa, bb, cc, dd, X[ 9], 15); + GG(dd, ee, aa, bb, cc, X[ 5], 9); + GG(cc, dd, ee, aa, bb, X[ 2], 11); + GG(bb, cc, dd, ee, aa, X[14], 7); + GG(aa, bb, cc, dd, ee, X[11], 13); + GG(ee, aa, bb, cc, dd, X[ 8], 12); + + /* parallel round 2 */ + III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); + III(ddd, eee, aaa, bbb, ccc, X[11], 13); + III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); + III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); + III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); + III(eee, aaa, bbb, ccc, ddd, X[13], 8); + III(ddd, eee, aaa, bbb, ccc, X[ 5], 9); + III(ccc, ddd, eee, aaa, bbb, X[10], 11); + III(bbb, ccc, ddd, eee, aaa, X[14], 7); + III(aaa, bbb, ccc, ddd, eee, X[15], 7); + III(eee, aaa, bbb, ccc, ddd, X[ 8], 12); + III(ddd, eee, aaa, bbb, ccc, X[12], 7); + III(ccc, ddd, eee, aaa, bbb, X[ 4], 6); + III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); + III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); + III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); + + tmp = bb; bb = bbb; bbb = tmp; + + /* round 3 */ + HH(dd, ee, aa, bb, cc, X[ 3], 11); + HH(cc, dd, ee, aa, bb, X[10], 13); + HH(bb, cc, dd, ee, aa, X[14], 6); + HH(aa, bb, cc, dd, ee, X[ 4], 7); + HH(ee, aa, bb, cc, dd, X[ 9], 14); + HH(dd, ee, aa, bb, cc, X[15], 9); + HH(cc, dd, ee, aa, bb, X[ 8], 13); + HH(bb, cc, dd, ee, aa, X[ 1], 15); + HH(aa, bb, cc, dd, ee, X[ 2], 14); + HH(ee, aa, bb, cc, dd, X[ 7], 8); + HH(dd, ee, aa, bb, cc, X[ 0], 13); + HH(cc, dd, ee, aa, bb, X[ 6], 6); + HH(bb, cc, dd, ee, aa, X[13], 5); + HH(aa, bb, cc, dd, ee, X[11], 12); + HH(ee, aa, bb, cc, dd, X[ 5], 7); + HH(dd, ee, aa, bb, cc, X[12], 5); + + /* parallel round 3 */ + HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); + HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); + HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15); + HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11); + HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8); + HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); + HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6); + HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14); + HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); + HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13); + HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); + HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14); + HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); + HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); + HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); + HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); + + tmp = cc; cc = ccc; ccc = tmp; + + /* round 4 */ + II(cc, dd, ee, aa, bb, X[ 1], 11); + II(bb, cc, dd, ee, aa, X[ 9], 12); + II(aa, bb, cc, dd, ee, X[11], 14); + II(ee, aa, bb, cc, dd, X[10], 15); + II(dd, ee, aa, bb, cc, X[ 0], 14); + II(cc, dd, ee, aa, bb, X[ 8], 15); + II(bb, cc, dd, ee, aa, X[12], 9); + II(aa, bb, cc, dd, ee, X[ 4], 8); + II(ee, aa, bb, cc, dd, X[13], 9); + II(dd, ee, aa, bb, cc, X[ 3], 14); + II(cc, dd, ee, aa, bb, X[ 7], 5); + II(bb, cc, dd, ee, aa, X[15], 6); + II(aa, bb, cc, dd, ee, X[14], 8); + II(ee, aa, bb, cc, dd, X[ 5], 6); + II(dd, ee, aa, bb, cc, X[ 6], 5); + II(cc, dd, ee, aa, bb, X[ 2], 12); + + /* parallel round 4 */ + GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); + GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); + GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); + GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); + GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); + GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); + GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); + GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14); + GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6); + GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); + GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12); + GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); + GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12); + GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); + GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); + GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); + + tmp = dd; dd = ddd; ddd = tmp; + + /* round 5 */ + JJ(bb, cc, dd, ee, aa, X[ 4], 9); + JJ(aa, bb, cc, dd, ee, X[ 0], 15); + JJ(ee, aa, bb, cc, dd, X[ 5], 5); + JJ(dd, ee, aa, bb, cc, X[ 9], 11); + JJ(cc, dd, ee, aa, bb, X[ 7], 6); + JJ(bb, cc, dd, ee, aa, X[12], 8); + JJ(aa, bb, cc, dd, ee, X[ 2], 13); + JJ(ee, aa, bb, cc, dd, X[10], 12); + JJ(dd, ee, aa, bb, cc, X[14], 5); + JJ(cc, dd, ee, aa, bb, X[ 1], 12); + JJ(bb, cc, dd, ee, aa, X[ 3], 13); + JJ(aa, bb, cc, dd, ee, X[ 8], 14); + JJ(ee, aa, bb, cc, dd, X[11], 11); + JJ(dd, ee, aa, bb, cc, X[ 6], 8); + JJ(cc, dd, ee, aa, bb, X[15], 5); + JJ(bb, cc, dd, ee, aa, X[13], 6); + + /* parallel round 5 */ + FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); + FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); + FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12); + FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9); + FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12); + FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5); + FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14); + FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6); + FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8); + FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13); + FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6); + FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5); + FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15); + FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); + FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); + FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); + + tmp = ee; ee = eee; eee = tmp; + + /* combine results */ + md->rmd320.state[0] += aa; + md->rmd320.state[1] += bb; + md->rmd320.state[2] += cc; + md->rmd320.state[3] += dd; + md->rmd320.state[4] += ee; + md->rmd320.state[5] += aaa; + md->rmd320.state[6] += bbb; + md->rmd320.state[7] += ccc; + md->rmd320.state[8] += ddd; + md->rmd320.state[9] += eee; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int rmd320_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _rmd320_compress(md, buf); + burn_stack(sizeof(ulong32) * 27 + sizeof(int)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int rmd320_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->rmd320.state[0] = 0x67452301UL; + md->rmd320.state[1] = 0xefcdab89UL; + md->rmd320.state[2] = 0x98badcfeUL; + md->rmd320.state[3] = 0x10325476UL; + md->rmd320.state[4] = 0xc3d2e1f0UL; + md->rmd320.state[5] = 0x76543210UL; + md->rmd320.state[6] = 0xfedcba98UL; + md->rmd320.state[7] = 0x89abcdefUL; + md->rmd320.state[8] = 0x01234567UL; + md->rmd320.state[9] = 0x3c2d1e0fUL; + md->rmd320.curlen = 0; + md->rmd320.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(rmd320_process, rmd320_compress, rmd320, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (20 bytes) + @return CRYPT_OK if successful +*/ +int rmd320_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->rmd320.curlen >= sizeof(md->rmd320.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* increase the length of the message */ + md->rmd320.length += md->rmd320.curlen * 8; + + /* append the '1' bit */ + md->rmd320.buf[md->rmd320.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->rmd320.curlen > 56) { + while (md->rmd320.curlen < 64) { + md->rmd320.buf[md->rmd320.curlen++] = (unsigned char)0; + } + rmd320_compress(md, md->rmd320.buf); + md->rmd320.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->rmd320.curlen < 56) { + md->rmd320.buf[md->rmd320.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->rmd320.length, md->rmd320.buf+56); + rmd320_compress(md, md->rmd320.buf); + + /* copy output */ + for (i = 0; i < 10; i++) { + STORE32L(md->rmd320.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +void calc_rmd320(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + rmd320_init(&md); + rmd320_process(&md,message,len); + rmd320_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,40); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int rmd320_test(void) +{ +#ifndef LTC_TEST + return CRYPT_NOP; +#else + static const struct { + char *msg; + unsigned char md[40]; + } tests[] = { + { "", + { 0x22, 0xd6, 0x5d, 0x56, 0x61, 0x53, 0x6c, 0xdc, 0x75, 0xc1, + 0xfd, 0xf5, 0xc6, 0xde, 0x7b, 0x41, 0xb9, 0xf2, 0x73, 0x25, + 0xeb, 0xc6, 0x1e, 0x85, 0x57, 0x17, 0x7d, 0x70, 0x5a, 0x0e, + 0xc8, 0x80, 0x15, 0x1c, 0x3a, 0x32, 0xa0, 0x08, 0x99, 0xb8 } + }, + { "a", + { 0xce, 0x78, 0x85, 0x06, 0x38, 0xf9, 0x26, 0x58, 0xa5, 0xa5, + 0x85, 0x09, 0x75, 0x79, 0x92, 0x6d, 0xda, 0x66, 0x7a, 0x57, + 0x16, 0x56, 0x2c, 0xfc, 0xf6, 0xfb, 0xe7, 0x7f, 0x63, 0x54, + 0x2f, 0x99, 0xb0, 0x47, 0x05, 0xd6, 0x97, 0x0d, 0xff, 0x5d } + }, + { "abc", + { 0xde, 0x4c, 0x01, 0xb3, 0x05, 0x4f, 0x89, 0x30, 0xa7, 0x9d, + 0x09, 0xae, 0x73, 0x8e, 0x92, 0x30, 0x1e, 0x5a, 0x17, 0x08, + 0x5b, 0xef, 0xfd, 0xc1, 0xb8, 0xd1, 0x16, 0x71, 0x3e, 0x74, + 0xf8, 0x2f, 0xa9, 0x42, 0xd6, 0x4c, 0xdb, 0xc4, 0x68, 0x2d } + }, + { "message digest", + { 0x3a, 0x8e, 0x28, 0x50, 0x2e, 0xd4, 0x5d, 0x42, 0x2f, 0x68, + 0x84, 0x4f, 0x9d, 0xd3, 0x16, 0xe7, 0xb9, 0x85, 0x33, 0xfa, + 0x3f, 0x2a, 0x91, 0xd2, 0x9f, 0x84, 0xd4, 0x25, 0xc8, 0x8d, + 0x6b, 0x4e, 0xff, 0x72, 0x7d, 0xf6, 0x6a, 0x7c, 0x01, 0x97 } + }, + { "abcdefghijklmnopqrstuvwxyz", + { 0xca, 0xbd, 0xb1, 0x81, 0x0b, 0x92, 0x47, 0x0a, 0x20, 0x93, + 0xaa, 0x6b, 0xce, 0x05, 0x95, 0x2c, 0x28, 0x34, 0x8c, 0xf4, + 0x3f, 0xf6, 0x08, 0x41, 0x97, 0x51, 0x66, 0xbb, 0x40, 0xed, + 0x23, 0x40, 0x04, 0xb8, 0x82, 0x44, 0x63, 0xe6, 0xb0, 0x09 } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0xd0, 0x34, 0xa7, 0x95, 0x0c, 0xf7, 0x22, 0x02, 0x1b, 0xa4, + 0xb8, 0x4d, 0xf7, 0x69, 0xa5, 0xde, 0x20, 0x60, 0xe2, 0x59, + 0xdf, 0x4c, 0x9b, 0xb4, 0xa4, 0x26, 0x8c, 0x0e, 0x93, 0x5b, + 0xbc, 0x74, 0x70, 0xa9, 0x69, 0xc9, 0xd0, 0x72, 0xa1, 0xac } + } + }; + int x; + unsigned char buf[40]; + hash_state md; + + for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + rmd320_init(&md); + rmd320_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); + rmd320_done(&md, buf); + if (XMEMCMP(buf, tests[x].md, 40) != 0) { +#if 0 + printf("Failed test %d\n", x); +#endif + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; +#endif +} +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef F +#undef G +#undef H +#undef I +#undef J + +#endif + diff --git a/crypto777/hmac/sha1.c b/crypto777/hmac/sha1.c new file mode 100755 index 000000000..c7cbeab01 --- /dev/null +++ b/crypto777/hmac/sha1.c @@ -0,0 +1,302 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +#define LTC_SHA1 + +#include "tomcrypt.h" + +/** + @file sha1.c + LTC_SHA1 code by Tom St Denis +*/ + + +#ifdef LTC_SHA1 + +const struct ltc_hash_descriptor sha1_desc = +{ + "sha1", + 2, + 20, + 64, + + /* OID */ + { 1, 3, 14, 3, 2, 26, }, + 6, + + &sha1_init, + &sha1_process, + &sha1_done, + &sha1_test, + NULL +}; + +#define F0(x,y,z) (z ^ (x & (y ^ z))) +#define F1(x,y,z) (x ^ y ^ z) +#define F2(x,y,z) ((x & y) | (z & (x | y))) +#define F3(x,y,z) (x ^ y ^ z) + +#ifdef LTC_CLEAN_STACK +static int _sha1_compress(hash_state *md, unsigned char *buf) +#else +static int sha1_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong32 a,b,c,d,e,W[80],i; +#ifdef LTC_SMALL_CODE + ulong32 t; +#endif + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32H(W[i], buf + (4*i)); + } + + /* copy state */ + a = md->sha1.state[0]; + b = md->sha1.state[1]; + c = md->sha1.state[2]; + d = md->sha1.state[3]; + e = md->sha1.state[4]; + + /* expand it */ + for (i = 16; i < 80; i++) { + W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); + } + + /* compress */ + /* round one */ + #define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30); + #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30); + #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30); + #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30); + +#ifdef LTC_SMALL_CODE + + for (i = 0; i < 20; ) { + FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; + } + + for (; i < 40; ) { + FF1(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; + } + + for (; i < 60; ) { + FF2(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; + } + + for (; i < 80; ) { + FF3(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; + } + +#else + + for (i = 0; i < 20; ) { + FF0(a,b,c,d,e,i++); + FF0(e,a,b,c,d,i++); + FF0(d,e,a,b,c,i++); + FF0(c,d,e,a,b,i++); + FF0(b,c,d,e,a,i++); + } + + /* round two */ + for (; i < 40; ) { + FF1(a,b,c,d,e,i++); + FF1(e,a,b,c,d,i++); + FF1(d,e,a,b,c,i++); + FF1(c,d,e,a,b,i++); + FF1(b,c,d,e,a,i++); + } + + /* round three */ + for (; i < 60; ) { + FF2(a,b,c,d,e,i++); + FF2(e,a,b,c,d,i++); + FF2(d,e,a,b,c,i++); + FF2(c,d,e,a,b,i++); + FF2(b,c,d,e,a,i++); + } + + /* round four */ + for (; i < 80; ) { + FF3(a,b,c,d,e,i++); + FF3(e,a,b,c,d,i++); + FF3(d,e,a,b,c,i++); + FF3(c,d,e,a,b,i++); + FF3(b,c,d,e,a,i++); + } +#endif + + #undef FF0 + #undef FF1 + #undef FF2 + #undef FF3 + + /* store */ + md->sha1.state[0] = md->sha1.state[0] + a; + md->sha1.state[1] = md->sha1.state[1] + b; + md->sha1.state[2] = md->sha1.state[2] + c; + md->sha1.state[3] = md->sha1.state[3] + d; + md->sha1.state[4] = md->sha1.state[4] + e; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int sha1_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _sha1_compress(md, buf); + burn_stack(sizeof(ulong32) * 87); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int sha1_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->sha1.state[0] = 0x67452301UL; + md->sha1.state[1] = 0xefcdab89UL; + md->sha1.state[2] = 0x98badcfeUL; + md->sha1.state[3] = 0x10325476UL; + md->sha1.state[4] = 0xc3d2e1f0UL; + md->sha1.curlen = 0; + md->sha1.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(sha1_process, sha1_compress, sha1, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (20 bytes) + @return CRYPT_OK if successful +*/ +int sha1_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->sha1.curlen >= sizeof(md->sha1.buf)) { + return CRYPT_INVALID_ARG; + } + + /* increase the length of the message */ + md->sha1.length += md->sha1.curlen * 8; + + /* append the '1' bit */ + md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->sha1.curlen > 56) { + while (md->sha1.curlen < 64) { + md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; + } + sha1_compress(md, md->sha1.buf); + md->sha1.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->sha1.curlen < 56) { + md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64H(md->sha1.length, md->sha1.buf+56); + sha1_compress(md, md->sha1.buf); + + /* copy output */ + for (i = 0; i < 5; i++) { + STORE32H(md->sha1.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +void calc_sha1(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + sha1_init(&md); + sha1_process(&md,message,len); + sha1_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,20); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int sha1_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[20]; + } tests[] = { + { "abc", + { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, + 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, + 0x9c, 0xd0, 0xd8, 0x9d } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, + 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, + 0xE5, 0x46, 0x70, 0xF1 } + } + }; + + int i; + unsigned char tmp[20]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + sha1_init(&md); + sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + sha1_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 20) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + +#endif + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/sha224.c b/crypto777/hmac/sha224.c new file mode 100755 index 000000000..78a0bfb84 --- /dev/null +++ b/crypto777/hmac/sha224.c @@ -0,0 +1,137 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +/** + @param sha224.c + LTC_SHA-224 new NIST standard based off of LTC_SHA-256 truncated to 224 bits (Tom St Denis) +*/ +#include "tomcrypt.h" + +const struct ltc_hash_descriptor sha224_desc = +{ + "sha224", + 10, + 28, + 64, + + /* OID */ + { 2, 16, 840, 1, 101, 3, 4, 2, 4, }, + 9, + + &sha224_init, + &sha256_process, + &sha224_done, + &sha224_test, + NULL +}; + +/* init the sha256 er... sha224 state ;-) */ +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int sha224_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + + md->sha256.curlen = 0; + md->sha256.length = 0; + md->sha256.state[0] = 0xc1059ed8UL; + md->sha256.state[1] = 0x367cd507UL; + md->sha256.state[2] = 0x3070dd17UL; + md->sha256.state[3] = 0xf70e5939UL; + md->sha256.state[4] = 0xffc00b31UL; + md->sha256.state[5] = 0x68581511UL; + md->sha256.state[6] = 0x64f98fa7UL; + md->sha256.state[7] = 0xbefa4fa4UL; + return CRYPT_OK; +} + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (28 bytes) + @return CRYPT_OK if successful +*/ +int sha224_done(hash_state * md, unsigned char *out) +{ + unsigned char buf[32]; + int err; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + err = sha256_done(md, buf); + XMEMCPY(out, buf, 28); +#ifdef LTC_CLEAN_STACK + zeromem(buf, sizeof(buf)); +#endif + return err; +} + +void calc_sha224(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + sha224_init(&md); + sha224_process(&md,message,len); + sha224_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,28); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int sha224_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[28]; + } tests[] = { + { "abc", + { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, + 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, + 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd, + 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, + 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, + 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4, + 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 } + }, + }; + + int i; + unsigned char tmp[28]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + sha224_init(&md); + sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + sha224_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 28) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha224.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/sha256.c b/crypto777/hmac/sha256.c new file mode 100755 index 000000000..29dceaa2e --- /dev/null +++ b/crypto777/hmac/sha256.c @@ -0,0 +1,442 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file sha256.c + LTC_SHA256 by Tom St Denis +*/ + +//#ifdef LTC_SHA256 + +const struct ltc_hash_descriptor sha256_desc = +{ + "sha256", + 0, + 32, + 64, + + /* OID */ + { 2, 16, 840, 1, 101, 3, 4, 2, 1, }, + 9, + + &sha256_init, + &sha256_process, + &sha256_done, + &sha256_test, + NULL +}; + +#ifdef LTC_SMALL_CODE +/* the K array */ +static const ulong32 K[64] = { + 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, + 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, + 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, + 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, + 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, + 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, + 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, + 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, + 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, + 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, + 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, + 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, + 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL +}; +#endif + +/* Various logical functions */ +#define Ch(x,y,z) (z ^ (x & (y ^ z))) +#define Maj(x,y,z) (((x | y) & z) | (x & y)) +#define S(x, n) RORc((x),(n)) +#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) +#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) +#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) +#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) +#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) + +/* compress 512-bits */ +#ifdef LTC_CLEAN_STACK +static int _sha256_compress(hash_state * md, unsigned char *buf) +#else +static int sha256_compress(hash_state * md, unsigned char *buf) +#endif +{ + ulong32 S[8], W[64], t0, t1; +#ifdef LTC_SMALL_CODE + ulong32 t; +#endif + int i; + + /* copy state into S */ + for (i = 0; i < 8; i++) { + S[i] = md->sha256.state[i]; + } + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32H(W[i], buf + (4*i)); + } + + /* fill W[16..63] */ + for (i = 16; i < 64; i++) { + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + } + + /* Compress */ +#ifdef LTC_SMALL_CODE +#define RND(a,b,c,d,e,f,g,h,i) \ + t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ + t1 = Sigma0(a) + Maj(a, b, c); \ + d += t0; \ + h = t0 + t1; + + for (i = 0; i < 64; ++i) { + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i); + t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4]; + S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t; + } +#else +#define RND(a,b,c,d,e,f,g,h,i,ki) \ + t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ + t1 = Sigma0(a) + Maj(a, b, c); \ + d += t0; \ + h = t0 + t1; + + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); + +#undef RND + +#endif + + /* feedback */ + for (i = 0; i < 8; i++) { + md->sha256.state[i] = md->sha256.state[i] + S[i]; + } + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int sha256_compress(hash_state * md, unsigned char *buf) +{ + int err; + err = _sha256_compress(md, buf); + burn_stack(sizeof(ulong32) * 74); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int sha256_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + + md->sha256.curlen = 0; + md->sha256.length = 0; + md->sha256.state[0] = 0x6A09E667UL; + md->sha256.state[1] = 0xBB67AE85UL; + md->sha256.state[2] = 0x3C6EF372UL; + md->sha256.state[3] = 0xA54FF53AUL; + md->sha256.state[4] = 0x510E527FUL; + md->sha256.state[5] = 0x9B05688CUL; + md->sha256.state[6] = 0x1F83D9ABUL; + md->sha256.state[7] = 0x5BE0CD19UL; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(sha256_process, sha256_compress, sha256, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (32 bytes) + @return CRYPT_OK if successful +*/ +int sha256_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->sha256.curlen >= sizeof(md->sha256.buf)) { + return CRYPT_INVALID_ARG; + } + + + /* increase the length of the message */ + md->sha256.length += md->sha256.curlen * 8; + + /* append the '1' bit */ + md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->sha256.curlen > 56) { + while (md->sha256.curlen < 64) { + md->sha256.buf[md->sha256.curlen++] = (unsigned char)0; + } + sha256_compress(md, md->sha256.buf); + md->sha256.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->sha256.curlen < 56) { + md->sha256.buf[md->sha256.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64H(md->sha256.length, md->sha256.buf+56); + sha256_compress(md, md->sha256.buf); + + /* copy output */ + for (i = 0; i < 8; i++) { + STORE32H(md->sha256.state[i], out+(4*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +void calc_sha256(char hashstr[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len) +{ + hash_state md; + sha256_init(&md); + sha256_process(&md,src,len); + sha256_done(&md,hash); + if ( hashstr != 0 ) + { + int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); + init_hexbytes_noT(hashstr,hash,256 >> 3); + } +} + +void calc_sha256cat(uint8_t hash[256 >> 3],uint8_t *src,int32_t len,uint8_t *src2,int32_t len2) +{ + hash_state md; + sha256_init(&md); + sha256_process(&md,src,len); + if ( src2 != 0 ) + sha256_process(&md,src2,len2); + sha256_done(&md,hash); +} + +void update_sha256(uint8_t hash[256 >> 3],struct sha256_state *state,uint8_t *src,int32_t len) +{ + hash_state md; + memset(&md,0,sizeof(md)); + if ( src == 0 ) + sha256_init(&md); + else + { + md.sha256 = *state; + sha256_process(&md,src,len); + } + *state = md.sha256; + sha256_done(&md,hash); +} + +/*void calc_OP_HASH160(char hexstr[41],uint8_t hash160[20],char *pubkey) +{ + int32_t decode_hex(unsigned char *bytes,int32_t n,char *hex); + int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); + uint8_t sha256[32],buf[4096]; int32_t len; hash_state md; + len = (int32_t)strlen(pubkey)/2; + if ( len > sizeof(buf) ) + { + printf("calc_OP_HASH160 overflow len.%d vs %d\n",len,(int32_t)sizeof(buf)); + return; + } + decode_hex(buf,len,pubkey); + sha256_init(&md); + sha256_process(&md,buf,len); + sha256_done(&md,sha256); + + rmd160_init(&md); + rmd160_process(&md,sha256,256 >> 3); + rmd160_done(&md,hash160); + if ( 0 ) + { + int i; + for (i=0; i<20; i++) + printf("%02x",hash160[i]); + printf("<- (%s)\n",pubkey); + } + if ( hexstr != 0 ) + init_hexbytes_noT(hexstr,hash160,20); +}*/ + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ + +int sha256_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[32]; + } tests[] = { + { "abc", + { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, + 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, + 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, + 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, + 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, + 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, + 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } + }, + { "helloworld", { 0x00 } }, + }; + void reverse_hexstr(char *str); + int32_t decode_hex(unsigned char *bytes,int32_t n,char *hex); + + int i,j; + unsigned char tmp[32],buf[512]; + hash_state md; + char *str; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + sha256_init(&md); + sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + sha256_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 32) != 0) { + for (j=0; j<32; j++) + printf("%02x",tmp[j]); + printf(" <- sha256(%s)\n",tests[i].msg); + str = malloc(strlen(tests[i].msg) + 16); + strcpy(str,(char*)tests[i].msg); + reverse_hexstr(str); + printf("reversed.(%s)\n",str); + sha256_init(&md); + sha256_process(&md, (unsigned char*)str, (unsigned long)strlen(str)); + sha256_done(&md, tmp); + for (j=0; j<32; j++) + printf("%02x",tmp[j]); + printf(" <- sha256(%s)\n",str); + decode_hex(buf,(int)strlen(tests[i].msg),tests[i].msg); + sha256_init(&md); + sha256_process(&md, (unsigned char*)buf, (unsigned long)strlen(tests[i].msg)/2); + sha256_done(&md, tmp); + for (j=0; j<32; j++) + printf("%02x",tmp[j]); + printf(" <- sha256(binary %s)\n",tests[i].msg); + return CRYPT_FAIL_TESTVECTOR; + } + } + printf("tests completed\n"); + return CRYPT_OK; + #endif +} + +//#ifdef LTC_SHA224 +//#include "sha224.c" +//#endif + +//#endif +#undef S +#undef R +#undef Sigma0 +#undef Sigma1 +#undef Gamma0 +#undef Gamma1 +#undef Ch +#undef Maj + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha256.c,v $ */ +/* $Revision: 1.11 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/sha384.c b/crypto777/hmac/sha384.c new file mode 100755 index 000000000..8adae5c9d --- /dev/null +++ b/crypto777/hmac/sha384.c @@ -0,0 +1,148 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +/** + @param sha384.c + LTC_SHA384 hash included in sha512.c, Tom St Denis +*/ +#include "tomcrypt.h" + + +const struct ltc_hash_descriptor sha384_desc = +{ + "sha384", + 4, + 48, + 128, + + /* OID */ + { 2, 16, 840, 1, 101, 3, 4, 2, 2, }, + 9, + + &sha384_init, + &sha512_process, + &sha384_done, + &sha384_test, + NULL +}; + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int sha384_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + + md->sha512.curlen = 0; + md->sha512.length = 0; + md->sha512.state[0] = CONST64(0xcbbb9d5dc1059ed8); + md->sha512.state[1] = CONST64(0x629a292a367cd507); + md->sha512.state[2] = CONST64(0x9159015a3070dd17); + md->sha512.state[3] = CONST64(0x152fecd8f70e5939); + md->sha512.state[4] = CONST64(0x67332667ffc00b31); + md->sha512.state[5] = CONST64(0x8eb44a8768581511); + md->sha512.state[6] = CONST64(0xdb0c2e0d64f98fa7); + md->sha512.state[7] = CONST64(0x47b5481dbefa4fa4); + return CRYPT_OK; +} + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (48 bytes) + @return CRYPT_OK if successful +*/ +int sha384_done(hash_state * md, unsigned char *out) +{ + unsigned char buf[64]; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->sha512.curlen >= sizeof(md->sha512.buf)) { + return CRYPT_INVALID_ARG; + } + + sha512_done(md, buf); + XMEMCPY(out, buf, 48); +#ifdef LTC_CLEAN_STACK + zeromem(buf, sizeof(buf)); +#endif + return CRYPT_OK; +} + +void calc_sha384(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + sha384_init(&md); + sha384_process(&md,message,len); + sha384_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,48); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int sha384_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[48]; + } tests[] = { + { "abc", + { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, + 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07, + 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, + 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed, + 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, + 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 } + }, + { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + { 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, + 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47, + 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, + 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, + 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9, + 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 } + }, + }; + + int i; + unsigned char tmp[48]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + sha384_init(&md); + sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + sha384_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 48) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + + + + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha384.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/sha512.c b/crypto777/hmac/sha512.c new file mode 100755 index 000000000..8b09925a5 --- /dev/null +++ b/crypto777/hmac/sha512.c @@ -0,0 +1,319 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @param sha512.c + LTC_SHA512 by Tom St Denis +*/ + +#ifdef LTC_SHA512 + +const struct ltc_hash_descriptor sha512_desc = +{ + "sha512", + 5, + 64, + 128, + + /* OID */ + { 2, 16, 840, 1, 101, 3, 4, 2, 3, }, + 9, + + &sha512_init, + &sha512_process, + &sha512_done, + &sha512_test, + NULL +}; + +/* the K array */ +static const ulong64 K[80] = { +CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd), +CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc), +CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019), +CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118), +CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe), +CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2), +CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1), +CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694), +CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3), +CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65), +CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483), +CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5), +CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210), +CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4), +CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725), +CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70), +CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926), +CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df), +CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8), +CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b), +CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001), +CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30), +CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910), +CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8), +CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53), +CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8), +CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb), +CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3), +CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60), +CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec), +CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9), +CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b), +CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207), +CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178), +CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6), +CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b), +CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493), +CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c), +CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a), +CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817) +}; + +/* Various logical functions */ +#define Ch(x,y,z) (z ^ (x & (y ^ z))) +#define Maj(x,y,z) (((x | y) & z) | (x & y)) +#define S(x, n) ROR64c(x, n) +#define R(x, n) (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)n)) +#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39)) +#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41)) +#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7)) +#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6)) + +/* compress 1024-bits */ +#ifdef LTC_CLEAN_STACK +static int _sha512_compress(hash_state * md, unsigned char *buf) +#else +static int sha512_compress(hash_state * md, unsigned char *buf) +#endif +{ + ulong64 S[8], W[80], t0, t1; + int i; + + /* copy state into S */ + for (i = 0; i < 8; i++) { + S[i] = md->sha512.state[i]; + } + + /* copy the state into 1024-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD64H(W[i], buf + (8*i)); + } + + /* fill W[16..79] */ + for (i = 16; i < 80; i++) { + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + } + + /* Compress */ +#ifdef LTC_SMALL_CODE + for (i = 0; i < 80; i++) { + t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i]; + t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]); + S[7] = S[6]; + S[6] = S[5]; + S[5] = S[4]; + S[4] = S[3] + t0; + S[3] = S[2]; + S[2] = S[1]; + S[1] = S[0]; + S[0] = t0 + t1; + } +#else +#define RND(a,b,c,d,e,f,g,h,i) \ + t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ + t1 = Sigma0(a) + Maj(a, b, c); \ + d += t0; \ + h = t0 + t1; + + for (i = 0; i < 80; i += 8) { + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7); + } +#endif + + + /* feedback */ + for (i = 0; i < 8; i++) { + md->sha512.state[i] = md->sha512.state[i] + S[i]; + } + + return CRYPT_OK; +} + +/* compress 1024-bits */ +#ifdef LTC_CLEAN_STACK +static int sha512_compress(hash_state * md, unsigned char *buf) +{ + int err; + err = _sha512_compress(md, buf); + burn_stack(sizeof(ulong64) * 90 + sizeof(int)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int sha512_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + md->sha512.curlen = 0; + md->sha512.length = 0; + md->sha512.state[0] = CONST64(0x6a09e667f3bcc908); + md->sha512.state[1] = CONST64(0xbb67ae8584caa73b); + md->sha512.state[2] = CONST64(0x3c6ef372fe94f82b); + md->sha512.state[3] = CONST64(0xa54ff53a5f1d36f1); + md->sha512.state[4] = CONST64(0x510e527fade682d1); + md->sha512.state[5] = CONST64(0x9b05688c2b3e6c1f); + md->sha512.state[6] = CONST64(0x1f83d9abfb41bd6b); + md->sha512.state[7] = CONST64(0x5be0cd19137e2179); + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(sha512_process, sha512_compress, sha512, 128) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (64 bytes) + @return CRYPT_OK if successful +*/ +int sha512_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->sha512.curlen >= sizeof(md->sha512.buf)) { + return CRYPT_INVALID_ARG; + } + + /* increase the length of the message */ + md->sha512.length += md->sha512.curlen * CONST64(8); + + /* append the '1' bit */ + md->sha512.buf[md->sha512.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 112 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->sha512.curlen > 112) { + while (md->sha512.curlen < 128) { + md->sha512.buf[md->sha512.curlen++] = (unsigned char)0; + } + sha512_compress(md, md->sha512.buf); + md->sha512.curlen = 0; + } + + /* pad upto 120 bytes of zeroes + * note: that from 112 to 120 is the 64 MSB of the length. We assume that you won't hash + * > 2^64 bits of data... :-) + */ + while (md->sha512.curlen < 120) { + md->sha512.buf[md->sha512.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64H(md->sha512.length, md->sha512.buf+120); + sha512_compress(md, md->sha512.buf); + + /* copy output */ + for (i = 0; i < 8; i++) { + STORE64H(md->sha512.state[i], out+(8*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + return CRYPT_OK; +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int sha512_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[64]; + } tests[] = { + { "abc", + { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, + 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, + 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, + 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, + 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, + 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, + 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, + 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f } + }, + { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + { 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, + 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, + 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, + 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, + 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, + 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, + 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, + 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 } + }, + }; + + int i; + unsigned char tmp[64]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + sha512_init(&md); + sha512_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + sha512_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 64) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + +//#ifdef LTC_SHA384 +// #include "sha384.c" +//#endif + +#endif + + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha512.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/tiger.c b/crypto777/hmac/tiger.c new file mode 100755 index 000000000..6958cf54d --- /dev/null +++ b/crypto777/hmac/tiger.c @@ -0,0 +1,825 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +#include "tomcrypt.h" + +/** + @file tiger.c + Tiger hash function, Tom St Denis +*/ + +#ifdef LTC_TIGER + +const struct ltc_hash_descriptor tiger_desc = +{ + "tiger", + 1, + 24, + 64, + + /* OID */ + { 1, 3, 6, 1, 4, 1, 11591, 12, 2, }, + 9, + + &tiger_init, + &tiger_process, + &tiger_done, + &tiger_test, + NULL +}; + +#define t1 (table) +#define t2 (table+256) +#define t3 (table+256*2) +#define t4 (table+256*3) + +static const ulong64 table[4*256] = { + CONST64(0x02AAB17CF7E90C5E) /* 0 */, CONST64(0xAC424B03E243A8EC) /* 1 */, + CONST64(0x72CD5BE30DD5FCD3) /* 2 */, CONST64(0x6D019B93F6F97F3A) /* 3 */, + CONST64(0xCD9978FFD21F9193) /* 4 */, CONST64(0x7573A1C9708029E2) /* 5 */, + CONST64(0xB164326B922A83C3) /* 6 */, CONST64(0x46883EEE04915870) /* 7 */, + CONST64(0xEAACE3057103ECE6) /* 8 */, CONST64(0xC54169B808A3535C) /* 9 */, + CONST64(0x4CE754918DDEC47C) /* 10 */, CONST64(0x0AA2F4DFDC0DF40C) /* 11 */, + CONST64(0x10B76F18A74DBEFA) /* 12 */, CONST64(0xC6CCB6235AD1AB6A) /* 13 */, + CONST64(0x13726121572FE2FF) /* 14 */, CONST64(0x1A488C6F199D921E) /* 15 */, + CONST64(0x4BC9F9F4DA0007CA) /* 16 */, CONST64(0x26F5E6F6E85241C7) /* 17 */, + CONST64(0x859079DBEA5947B6) /* 18 */, CONST64(0x4F1885C5C99E8C92) /* 19 */, + CONST64(0xD78E761EA96F864B) /* 20 */, CONST64(0x8E36428C52B5C17D) /* 21 */, + CONST64(0x69CF6827373063C1) /* 22 */, CONST64(0xB607C93D9BB4C56E) /* 23 */, + CONST64(0x7D820E760E76B5EA) /* 24 */, CONST64(0x645C9CC6F07FDC42) /* 25 */, + CONST64(0xBF38A078243342E0) /* 26 */, CONST64(0x5F6B343C9D2E7D04) /* 27 */, + CONST64(0xF2C28AEB600B0EC6) /* 28 */, CONST64(0x6C0ED85F7254BCAC) /* 29 */, + CONST64(0x71592281A4DB4FE5) /* 30 */, CONST64(0x1967FA69CE0FED9F) /* 31 */, + CONST64(0xFD5293F8B96545DB) /* 32 */, CONST64(0xC879E9D7F2A7600B) /* 33 */, + CONST64(0x860248920193194E) /* 34 */, CONST64(0xA4F9533B2D9CC0B3) /* 35 */, + CONST64(0x9053836C15957613) /* 36 */, CONST64(0xDB6DCF8AFC357BF1) /* 37 */, + CONST64(0x18BEEA7A7A370F57) /* 38 */, CONST64(0x037117CA50B99066) /* 39 */, + CONST64(0x6AB30A9774424A35) /* 40 */, CONST64(0xF4E92F02E325249B) /* 41 */, + CONST64(0x7739DB07061CCAE1) /* 42 */, CONST64(0xD8F3B49CECA42A05) /* 43 */, + CONST64(0xBD56BE3F51382F73) /* 44 */, CONST64(0x45FAED5843B0BB28) /* 45 */, + CONST64(0x1C813D5C11BF1F83) /* 46 */, CONST64(0x8AF0E4B6D75FA169) /* 47 */, + CONST64(0x33EE18A487AD9999) /* 48 */, CONST64(0x3C26E8EAB1C94410) /* 49 */, + CONST64(0xB510102BC0A822F9) /* 50 */, CONST64(0x141EEF310CE6123B) /* 51 */, + CONST64(0xFC65B90059DDB154) /* 52 */, CONST64(0xE0158640C5E0E607) /* 53 */, + CONST64(0x884E079826C3A3CF) /* 54 */, CONST64(0x930D0D9523C535FD) /* 55 */, + CONST64(0x35638D754E9A2B00) /* 56 */, CONST64(0x4085FCCF40469DD5) /* 57 */, + CONST64(0xC4B17AD28BE23A4C) /* 58 */, CONST64(0xCAB2F0FC6A3E6A2E) /* 59 */, + CONST64(0x2860971A6B943FCD) /* 60 */, CONST64(0x3DDE6EE212E30446) /* 61 */, + CONST64(0x6222F32AE01765AE) /* 62 */, CONST64(0x5D550BB5478308FE) /* 63 */, + CONST64(0xA9EFA98DA0EDA22A) /* 64 */, CONST64(0xC351A71686C40DA7) /* 65 */, + CONST64(0x1105586D9C867C84) /* 66 */, CONST64(0xDCFFEE85FDA22853) /* 67 */, + CONST64(0xCCFBD0262C5EEF76) /* 68 */, CONST64(0xBAF294CB8990D201) /* 69 */, + CONST64(0xE69464F52AFAD975) /* 70 */, CONST64(0x94B013AFDF133E14) /* 71 */, + CONST64(0x06A7D1A32823C958) /* 72 */, CONST64(0x6F95FE5130F61119) /* 73 */, + CONST64(0xD92AB34E462C06C0) /* 74 */, CONST64(0xED7BDE33887C71D2) /* 75 */, + CONST64(0x79746D6E6518393E) /* 76 */, CONST64(0x5BA419385D713329) /* 77 */, + CONST64(0x7C1BA6B948A97564) /* 78 */, CONST64(0x31987C197BFDAC67) /* 79 */, + CONST64(0xDE6C23C44B053D02) /* 80 */, CONST64(0x581C49FED002D64D) /* 81 */, + CONST64(0xDD474D6338261571) /* 82 */, CONST64(0xAA4546C3E473D062) /* 83 */, + CONST64(0x928FCE349455F860) /* 84 */, CONST64(0x48161BBACAAB94D9) /* 85 */, + CONST64(0x63912430770E6F68) /* 86 */, CONST64(0x6EC8A5E602C6641C) /* 87 */, + CONST64(0x87282515337DDD2B) /* 88 */, CONST64(0x2CDA6B42034B701B) /* 89 */, + CONST64(0xB03D37C181CB096D) /* 90 */, CONST64(0xE108438266C71C6F) /* 91 */, + CONST64(0x2B3180C7EB51B255) /* 92 */, CONST64(0xDF92B82F96C08BBC) /* 93 */, + CONST64(0x5C68C8C0A632F3BA) /* 94 */, CONST64(0x5504CC861C3D0556) /* 95 */, + CONST64(0xABBFA4E55FB26B8F) /* 96 */, CONST64(0x41848B0AB3BACEB4) /* 97 */, + CONST64(0xB334A273AA445D32) /* 98 */, CONST64(0xBCA696F0A85AD881) /* 99 */, + CONST64(0x24F6EC65B528D56C) /* 100 */, CONST64(0x0CE1512E90F4524A) /* 101 */, + CONST64(0x4E9DD79D5506D35A) /* 102 */, CONST64(0x258905FAC6CE9779) /* 103 */, + CONST64(0x2019295B3E109B33) /* 104 */, CONST64(0xF8A9478B73A054CC) /* 105 */, + CONST64(0x2924F2F934417EB0) /* 106 */, CONST64(0x3993357D536D1BC4) /* 107 */, + CONST64(0x38A81AC21DB6FF8B) /* 108 */, CONST64(0x47C4FBF17D6016BF) /* 109 */, + CONST64(0x1E0FAADD7667E3F5) /* 110 */, CONST64(0x7ABCFF62938BEB96) /* 111 */, + CONST64(0xA78DAD948FC179C9) /* 112 */, CONST64(0x8F1F98B72911E50D) /* 113 */, + CONST64(0x61E48EAE27121A91) /* 114 */, CONST64(0x4D62F7AD31859808) /* 115 */, + CONST64(0xECEBA345EF5CEAEB) /* 116 */, CONST64(0xF5CEB25EBC9684CE) /* 117 */, + CONST64(0xF633E20CB7F76221) /* 118 */, CONST64(0xA32CDF06AB8293E4) /* 119 */, + CONST64(0x985A202CA5EE2CA4) /* 120 */, CONST64(0xCF0B8447CC8A8FB1) /* 121 */, + CONST64(0x9F765244979859A3) /* 122 */, CONST64(0xA8D516B1A1240017) /* 123 */, + CONST64(0x0BD7BA3EBB5DC726) /* 124 */, CONST64(0xE54BCA55B86ADB39) /* 125 */, + CONST64(0x1D7A3AFD6C478063) /* 126 */, CONST64(0x519EC608E7669EDD) /* 127 */, + CONST64(0x0E5715A2D149AA23) /* 128 */, CONST64(0x177D4571848FF194) /* 129 */, + CONST64(0xEEB55F3241014C22) /* 130 */, CONST64(0x0F5E5CA13A6E2EC2) /* 131 */, + CONST64(0x8029927B75F5C361) /* 132 */, CONST64(0xAD139FABC3D6E436) /* 133 */, + CONST64(0x0D5DF1A94CCF402F) /* 134 */, CONST64(0x3E8BD948BEA5DFC8) /* 135 */, + CONST64(0xA5A0D357BD3FF77E) /* 136 */, CONST64(0xA2D12E251F74F645) /* 137 */, + CONST64(0x66FD9E525E81A082) /* 138 */, CONST64(0x2E0C90CE7F687A49) /* 139 */, + CONST64(0xC2E8BCBEBA973BC5) /* 140 */, CONST64(0x000001BCE509745F) /* 141 */, + CONST64(0x423777BBE6DAB3D6) /* 142 */, CONST64(0xD1661C7EAEF06EB5) /* 143 */, + CONST64(0xA1781F354DAACFD8) /* 144 */, CONST64(0x2D11284A2B16AFFC) /* 145 */, + CONST64(0xF1FC4F67FA891D1F) /* 146 */, CONST64(0x73ECC25DCB920ADA) /* 147 */, + CONST64(0xAE610C22C2A12651) /* 148 */, CONST64(0x96E0A810D356B78A) /* 149 */, + CONST64(0x5A9A381F2FE7870F) /* 150 */, CONST64(0xD5AD62EDE94E5530) /* 151 */, + CONST64(0xD225E5E8368D1427) /* 152 */, CONST64(0x65977B70C7AF4631) /* 153 */, + CONST64(0x99F889B2DE39D74F) /* 154 */, CONST64(0x233F30BF54E1D143) /* 155 */, + CONST64(0x9A9675D3D9A63C97) /* 156 */, CONST64(0x5470554FF334F9A8) /* 157 */, + CONST64(0x166ACB744A4F5688) /* 158 */, CONST64(0x70C74CAAB2E4AEAD) /* 159 */, + CONST64(0xF0D091646F294D12) /* 160 */, CONST64(0x57B82A89684031D1) /* 161 */, + CONST64(0xEFD95A5A61BE0B6B) /* 162 */, CONST64(0x2FBD12E969F2F29A) /* 163 */, + CONST64(0x9BD37013FEFF9FE8) /* 164 */, CONST64(0x3F9B0404D6085A06) /* 165 */, + CONST64(0x4940C1F3166CFE15) /* 166 */, CONST64(0x09542C4DCDF3DEFB) /* 167 */, + CONST64(0xB4C5218385CD5CE3) /* 168 */, CONST64(0xC935B7DC4462A641) /* 169 */, + CONST64(0x3417F8A68ED3B63F) /* 170 */, CONST64(0xB80959295B215B40) /* 171 */, + CONST64(0xF99CDAEF3B8C8572) /* 172 */, CONST64(0x018C0614F8FCB95D) /* 173 */, + CONST64(0x1B14ACCD1A3ACDF3) /* 174 */, CONST64(0x84D471F200BB732D) /* 175 */, + CONST64(0xC1A3110E95E8DA16) /* 176 */, CONST64(0x430A7220BF1A82B8) /* 177 */, + CONST64(0xB77E090D39DF210E) /* 178 */, CONST64(0x5EF4BD9F3CD05E9D) /* 179 */, + CONST64(0x9D4FF6DA7E57A444) /* 180 */, CONST64(0xDA1D60E183D4A5F8) /* 181 */, + CONST64(0xB287C38417998E47) /* 182 */, CONST64(0xFE3EDC121BB31886) /* 183 */, + CONST64(0xC7FE3CCC980CCBEF) /* 184 */, CONST64(0xE46FB590189BFD03) /* 185 */, + CONST64(0x3732FD469A4C57DC) /* 186 */, CONST64(0x7EF700A07CF1AD65) /* 187 */, + CONST64(0x59C64468A31D8859) /* 188 */, CONST64(0x762FB0B4D45B61F6) /* 189 */, + CONST64(0x155BAED099047718) /* 190 */, CONST64(0x68755E4C3D50BAA6) /* 191 */, + CONST64(0xE9214E7F22D8B4DF) /* 192 */, CONST64(0x2ADDBF532EAC95F4) /* 193 */, + CONST64(0x32AE3909B4BD0109) /* 194 */, CONST64(0x834DF537B08E3450) /* 195 */, + CONST64(0xFA209DA84220728D) /* 196 */, CONST64(0x9E691D9B9EFE23F7) /* 197 */, + CONST64(0x0446D288C4AE8D7F) /* 198 */, CONST64(0x7B4CC524E169785B) /* 199 */, + CONST64(0x21D87F0135CA1385) /* 200 */, CONST64(0xCEBB400F137B8AA5) /* 201 */, + CONST64(0x272E2B66580796BE) /* 202 */, CONST64(0x3612264125C2B0DE) /* 203 */, + CONST64(0x057702BDAD1EFBB2) /* 204 */, CONST64(0xD4BABB8EACF84BE9) /* 205 */, + CONST64(0x91583139641BC67B) /* 206 */, CONST64(0x8BDC2DE08036E024) /* 207 */, + CONST64(0x603C8156F49F68ED) /* 208 */, CONST64(0xF7D236F7DBEF5111) /* 209 */, + CONST64(0x9727C4598AD21E80) /* 210 */, CONST64(0xA08A0896670A5FD7) /* 211 */, + CONST64(0xCB4A8F4309EBA9CB) /* 212 */, CONST64(0x81AF564B0F7036A1) /* 213 */, + CONST64(0xC0B99AA778199ABD) /* 214 */, CONST64(0x959F1EC83FC8E952) /* 215 */, + CONST64(0x8C505077794A81B9) /* 216 */, CONST64(0x3ACAAF8F056338F0) /* 217 */, + CONST64(0x07B43F50627A6778) /* 218 */, CONST64(0x4A44AB49F5ECCC77) /* 219 */, + CONST64(0x3BC3D6E4B679EE98) /* 220 */, CONST64(0x9CC0D4D1CF14108C) /* 221 */, + CONST64(0x4406C00B206BC8A0) /* 222 */, CONST64(0x82A18854C8D72D89) /* 223 */, + CONST64(0x67E366B35C3C432C) /* 224 */, CONST64(0xB923DD61102B37F2) /* 225 */, + CONST64(0x56AB2779D884271D) /* 226 */, CONST64(0xBE83E1B0FF1525AF) /* 227 */, + CONST64(0xFB7C65D4217E49A9) /* 228 */, CONST64(0x6BDBE0E76D48E7D4) /* 229 */, + CONST64(0x08DF828745D9179E) /* 230 */, CONST64(0x22EA6A9ADD53BD34) /* 231 */, + CONST64(0xE36E141C5622200A) /* 232 */, CONST64(0x7F805D1B8CB750EE) /* 233 */, + CONST64(0xAFE5C7A59F58E837) /* 234 */, CONST64(0xE27F996A4FB1C23C) /* 235 */, + CONST64(0xD3867DFB0775F0D0) /* 236 */, CONST64(0xD0E673DE6E88891A) /* 237 */, + CONST64(0x123AEB9EAFB86C25) /* 238 */, CONST64(0x30F1D5D5C145B895) /* 239 */, + CONST64(0xBB434A2DEE7269E7) /* 240 */, CONST64(0x78CB67ECF931FA38) /* 241 */, + CONST64(0xF33B0372323BBF9C) /* 242 */, CONST64(0x52D66336FB279C74) /* 243 */, + CONST64(0x505F33AC0AFB4EAA) /* 244 */, CONST64(0xE8A5CD99A2CCE187) /* 245 */, + CONST64(0x534974801E2D30BB) /* 246 */, CONST64(0x8D2D5711D5876D90) /* 247 */, + CONST64(0x1F1A412891BC038E) /* 248 */, CONST64(0xD6E2E71D82E56648) /* 249 */, + CONST64(0x74036C3A497732B7) /* 250 */, CONST64(0x89B67ED96361F5AB) /* 251 */, + CONST64(0xFFED95D8F1EA02A2) /* 252 */, CONST64(0xE72B3BD61464D43D) /* 253 */, + CONST64(0xA6300F170BDC4820) /* 254 */, CONST64(0xEBC18760ED78A77A) /* 255 */, + CONST64(0xE6A6BE5A05A12138) /* 256 */, CONST64(0xB5A122A5B4F87C98) /* 257 */, + CONST64(0x563C6089140B6990) /* 258 */, CONST64(0x4C46CB2E391F5DD5) /* 259 */, + CONST64(0xD932ADDBC9B79434) /* 260 */, CONST64(0x08EA70E42015AFF5) /* 261 */, + CONST64(0xD765A6673E478CF1) /* 262 */, CONST64(0xC4FB757EAB278D99) /* 263 */, + CONST64(0xDF11C6862D6E0692) /* 264 */, CONST64(0xDDEB84F10D7F3B16) /* 265 */, + CONST64(0x6F2EF604A665EA04) /* 266 */, CONST64(0x4A8E0F0FF0E0DFB3) /* 267 */, + CONST64(0xA5EDEEF83DBCBA51) /* 268 */, CONST64(0xFC4F0A2A0EA4371E) /* 269 */, + CONST64(0xE83E1DA85CB38429) /* 270 */, CONST64(0xDC8FF882BA1B1CE2) /* 271 */, + CONST64(0xCD45505E8353E80D) /* 272 */, CONST64(0x18D19A00D4DB0717) /* 273 */, + CONST64(0x34A0CFEDA5F38101) /* 274 */, CONST64(0x0BE77E518887CAF2) /* 275 */, + CONST64(0x1E341438B3C45136) /* 276 */, CONST64(0xE05797F49089CCF9) /* 277 */, + CONST64(0xFFD23F9DF2591D14) /* 278 */, CONST64(0x543DDA228595C5CD) /* 279 */, + CONST64(0x661F81FD99052A33) /* 280 */, CONST64(0x8736E641DB0F7B76) /* 281 */, + CONST64(0x15227725418E5307) /* 282 */, CONST64(0xE25F7F46162EB2FA) /* 283 */, + CONST64(0x48A8B2126C13D9FE) /* 284 */, CONST64(0xAFDC541792E76EEA) /* 285 */, + CONST64(0x03D912BFC6D1898F) /* 286 */, CONST64(0x31B1AAFA1B83F51B) /* 287 */, + CONST64(0xF1AC2796E42AB7D9) /* 288 */, CONST64(0x40A3A7D7FCD2EBAC) /* 289 */, + CONST64(0x1056136D0AFBBCC5) /* 290 */, CONST64(0x7889E1DD9A6D0C85) /* 291 */, + CONST64(0xD33525782A7974AA) /* 292 */, CONST64(0xA7E25D09078AC09B) /* 293 */, + CONST64(0xBD4138B3EAC6EDD0) /* 294 */, CONST64(0x920ABFBE71EB9E70) /* 295 */, + CONST64(0xA2A5D0F54FC2625C) /* 296 */, CONST64(0xC054E36B0B1290A3) /* 297 */, + CONST64(0xF6DD59FF62FE932B) /* 298 */, CONST64(0x3537354511A8AC7D) /* 299 */, + CONST64(0xCA845E9172FADCD4) /* 300 */, CONST64(0x84F82B60329D20DC) /* 301 */, + CONST64(0x79C62CE1CD672F18) /* 302 */, CONST64(0x8B09A2ADD124642C) /* 303 */, + CONST64(0xD0C1E96A19D9E726) /* 304 */, CONST64(0x5A786A9B4BA9500C) /* 305 */, + CONST64(0x0E020336634C43F3) /* 306 */, CONST64(0xC17B474AEB66D822) /* 307 */, + CONST64(0x6A731AE3EC9BAAC2) /* 308 */, CONST64(0x8226667AE0840258) /* 309 */, + CONST64(0x67D4567691CAECA5) /* 310 */, CONST64(0x1D94155C4875ADB5) /* 311 */, + CONST64(0x6D00FD985B813FDF) /* 312 */, CONST64(0x51286EFCB774CD06) /* 313 */, + CONST64(0x5E8834471FA744AF) /* 314 */, CONST64(0xF72CA0AEE761AE2E) /* 315 */, + CONST64(0xBE40E4CDAEE8E09A) /* 316 */, CONST64(0xE9970BBB5118F665) /* 317 */, + CONST64(0x726E4BEB33DF1964) /* 318 */, CONST64(0x703B000729199762) /* 319 */, + CONST64(0x4631D816F5EF30A7) /* 320 */, CONST64(0xB880B5B51504A6BE) /* 321 */, + CONST64(0x641793C37ED84B6C) /* 322 */, CONST64(0x7B21ED77F6E97D96) /* 323 */, + CONST64(0x776306312EF96B73) /* 324 */, CONST64(0xAE528948E86FF3F4) /* 325 */, + CONST64(0x53DBD7F286A3F8F8) /* 326 */, CONST64(0x16CADCE74CFC1063) /* 327 */, + CONST64(0x005C19BDFA52C6DD) /* 328 */, CONST64(0x68868F5D64D46AD3) /* 329 */, + CONST64(0x3A9D512CCF1E186A) /* 330 */, CONST64(0x367E62C2385660AE) /* 331 */, + CONST64(0xE359E7EA77DCB1D7) /* 332 */, CONST64(0x526C0773749ABE6E) /* 333 */, + CONST64(0x735AE5F9D09F734B) /* 334 */, CONST64(0x493FC7CC8A558BA8) /* 335 */, + CONST64(0xB0B9C1533041AB45) /* 336 */, CONST64(0x321958BA470A59BD) /* 337 */, + CONST64(0x852DB00B5F46C393) /* 338 */, CONST64(0x91209B2BD336B0E5) /* 339 */, + CONST64(0x6E604F7D659EF19F) /* 340 */, CONST64(0xB99A8AE2782CCB24) /* 341 */, + CONST64(0xCCF52AB6C814C4C7) /* 342 */, CONST64(0x4727D9AFBE11727B) /* 343 */, + CONST64(0x7E950D0C0121B34D) /* 344 */, CONST64(0x756F435670AD471F) /* 345 */, + CONST64(0xF5ADD442615A6849) /* 346 */, CONST64(0x4E87E09980B9957A) /* 347 */, + CONST64(0x2ACFA1DF50AEE355) /* 348 */, CONST64(0xD898263AFD2FD556) /* 349 */, + CONST64(0xC8F4924DD80C8FD6) /* 350 */, CONST64(0xCF99CA3D754A173A) /* 351 */, + CONST64(0xFE477BACAF91BF3C) /* 352 */, CONST64(0xED5371F6D690C12D) /* 353 */, + CONST64(0x831A5C285E687094) /* 354 */, CONST64(0xC5D3C90A3708A0A4) /* 355 */, + CONST64(0x0F7F903717D06580) /* 356 */, CONST64(0x19F9BB13B8FDF27F) /* 357 */, + CONST64(0xB1BD6F1B4D502843) /* 358 */, CONST64(0x1C761BA38FFF4012) /* 359 */, + CONST64(0x0D1530C4E2E21F3B) /* 360 */, CONST64(0x8943CE69A7372C8A) /* 361 */, + CONST64(0xE5184E11FEB5CE66) /* 362 */, CONST64(0x618BDB80BD736621) /* 363 */, + CONST64(0x7D29BAD68B574D0B) /* 364 */, CONST64(0x81BB613E25E6FE5B) /* 365 */, + CONST64(0x071C9C10BC07913F) /* 366 */, CONST64(0xC7BEEB7909AC2D97) /* 367 */, + CONST64(0xC3E58D353BC5D757) /* 368 */, CONST64(0xEB017892F38F61E8) /* 369 */, + CONST64(0xD4EFFB9C9B1CC21A) /* 370 */, CONST64(0x99727D26F494F7AB) /* 371 */, + CONST64(0xA3E063A2956B3E03) /* 372 */, CONST64(0x9D4A8B9A4AA09C30) /* 373 */, + CONST64(0x3F6AB7D500090FB4) /* 374 */, CONST64(0x9CC0F2A057268AC0) /* 375 */, + CONST64(0x3DEE9D2DEDBF42D1) /* 376 */, CONST64(0x330F49C87960A972) /* 377 */, + CONST64(0xC6B2720287421B41) /* 378 */, CONST64(0x0AC59EC07C00369C) /* 379 */, + CONST64(0xEF4EAC49CB353425) /* 380 */, CONST64(0xF450244EEF0129D8) /* 381 */, + CONST64(0x8ACC46E5CAF4DEB6) /* 382 */, CONST64(0x2FFEAB63989263F7) /* 383 */, + CONST64(0x8F7CB9FE5D7A4578) /* 384 */, CONST64(0x5BD8F7644E634635) /* 385 */, + CONST64(0x427A7315BF2DC900) /* 386 */, CONST64(0x17D0C4AA2125261C) /* 387 */, + CONST64(0x3992486C93518E50) /* 388 */, CONST64(0xB4CBFEE0A2D7D4C3) /* 389 */, + CONST64(0x7C75D6202C5DDD8D) /* 390 */, CONST64(0xDBC295D8E35B6C61) /* 391 */, + CONST64(0x60B369D302032B19) /* 392 */, CONST64(0xCE42685FDCE44132) /* 393 */, + CONST64(0x06F3DDB9DDF65610) /* 394 */, CONST64(0x8EA4D21DB5E148F0) /* 395 */, + CONST64(0x20B0FCE62FCD496F) /* 396 */, CONST64(0x2C1B912358B0EE31) /* 397 */, + CONST64(0xB28317B818F5A308) /* 398 */, CONST64(0xA89C1E189CA6D2CF) /* 399 */, + CONST64(0x0C6B18576AAADBC8) /* 400 */, CONST64(0xB65DEAA91299FAE3) /* 401 */, + CONST64(0xFB2B794B7F1027E7) /* 402 */, CONST64(0x04E4317F443B5BEB) /* 403 */, + CONST64(0x4B852D325939D0A6) /* 404 */, CONST64(0xD5AE6BEEFB207FFC) /* 405 */, + CONST64(0x309682B281C7D374) /* 406 */, CONST64(0xBAE309A194C3B475) /* 407 */, + CONST64(0x8CC3F97B13B49F05) /* 408 */, CONST64(0x98A9422FF8293967) /* 409 */, + CONST64(0x244B16B01076FF7C) /* 410 */, CONST64(0xF8BF571C663D67EE) /* 411 */, + CONST64(0x1F0D6758EEE30DA1) /* 412 */, CONST64(0xC9B611D97ADEB9B7) /* 413 */, + CONST64(0xB7AFD5887B6C57A2) /* 414 */, CONST64(0x6290AE846B984FE1) /* 415 */, + CONST64(0x94DF4CDEACC1A5FD) /* 416 */, CONST64(0x058A5BD1C5483AFF) /* 417 */, + CONST64(0x63166CC142BA3C37) /* 418 */, CONST64(0x8DB8526EB2F76F40) /* 419 */, + CONST64(0xE10880036F0D6D4E) /* 420 */, CONST64(0x9E0523C9971D311D) /* 421 */, + CONST64(0x45EC2824CC7CD691) /* 422 */, CONST64(0x575B8359E62382C9) /* 423 */, + CONST64(0xFA9E400DC4889995) /* 424 */, CONST64(0xD1823ECB45721568) /* 425 */, + CONST64(0xDAFD983B8206082F) /* 426 */, CONST64(0xAA7D29082386A8CB) /* 427 */, + CONST64(0x269FCD4403B87588) /* 428 */, CONST64(0x1B91F5F728BDD1E0) /* 429 */, + CONST64(0xE4669F39040201F6) /* 430 */, CONST64(0x7A1D7C218CF04ADE) /* 431 */, + CONST64(0x65623C29D79CE5CE) /* 432 */, CONST64(0x2368449096C00BB1) /* 433 */, + CONST64(0xAB9BF1879DA503BA) /* 434 */, CONST64(0xBC23ECB1A458058E) /* 435 */, + CONST64(0x9A58DF01BB401ECC) /* 436 */, CONST64(0xA070E868A85F143D) /* 437 */, + CONST64(0x4FF188307DF2239E) /* 438 */, CONST64(0x14D565B41A641183) /* 439 */, + CONST64(0xEE13337452701602) /* 440 */, CONST64(0x950E3DCF3F285E09) /* 441 */, + CONST64(0x59930254B9C80953) /* 442 */, CONST64(0x3BF299408930DA6D) /* 443 */, + CONST64(0xA955943F53691387) /* 444 */, CONST64(0xA15EDECAA9CB8784) /* 445 */, + CONST64(0x29142127352BE9A0) /* 446 */, CONST64(0x76F0371FFF4E7AFB) /* 447 */, + CONST64(0x0239F450274F2228) /* 448 */, CONST64(0xBB073AF01D5E868B) /* 449 */, + CONST64(0xBFC80571C10E96C1) /* 450 */, CONST64(0xD267088568222E23) /* 451 */, + CONST64(0x9671A3D48E80B5B0) /* 452 */, CONST64(0x55B5D38AE193BB81) /* 453 */, + CONST64(0x693AE2D0A18B04B8) /* 454 */, CONST64(0x5C48B4ECADD5335F) /* 455 */, + CONST64(0xFD743B194916A1CA) /* 456 */, CONST64(0x2577018134BE98C4) /* 457 */, + CONST64(0xE77987E83C54A4AD) /* 458 */, CONST64(0x28E11014DA33E1B9) /* 459 */, + CONST64(0x270CC59E226AA213) /* 460 */, CONST64(0x71495F756D1A5F60) /* 461 */, + CONST64(0x9BE853FB60AFEF77) /* 462 */, CONST64(0xADC786A7F7443DBF) /* 463 */, + CONST64(0x0904456173B29A82) /* 464 */, CONST64(0x58BC7A66C232BD5E) /* 465 */, + CONST64(0xF306558C673AC8B2) /* 466 */, CONST64(0x41F639C6B6C9772A) /* 467 */, + CONST64(0x216DEFE99FDA35DA) /* 468 */, CONST64(0x11640CC71C7BE615) /* 469 */, + CONST64(0x93C43694565C5527) /* 470 */, CONST64(0xEA038E6246777839) /* 471 */, + CONST64(0xF9ABF3CE5A3E2469) /* 472 */, CONST64(0x741E768D0FD312D2) /* 473 */, + CONST64(0x0144B883CED652C6) /* 474 */, CONST64(0xC20B5A5BA33F8552) /* 475 */, + CONST64(0x1AE69633C3435A9D) /* 476 */, CONST64(0x97A28CA4088CFDEC) /* 477 */, + CONST64(0x8824A43C1E96F420) /* 478 */, CONST64(0x37612FA66EEEA746) /* 479 */, + CONST64(0x6B4CB165F9CF0E5A) /* 480 */, CONST64(0x43AA1C06A0ABFB4A) /* 481 */, + CONST64(0x7F4DC26FF162796B) /* 482 */, CONST64(0x6CBACC8E54ED9B0F) /* 483 */, + CONST64(0xA6B7FFEFD2BB253E) /* 484 */, CONST64(0x2E25BC95B0A29D4F) /* 485 */, + CONST64(0x86D6A58BDEF1388C) /* 486 */, CONST64(0xDED74AC576B6F054) /* 487 */, + CONST64(0x8030BDBC2B45805D) /* 488 */, CONST64(0x3C81AF70E94D9289) /* 489 */, + CONST64(0x3EFF6DDA9E3100DB) /* 490 */, CONST64(0xB38DC39FDFCC8847) /* 491 */, + CONST64(0x123885528D17B87E) /* 492 */, CONST64(0xF2DA0ED240B1B642) /* 493 */, + CONST64(0x44CEFADCD54BF9A9) /* 494 */, CONST64(0x1312200E433C7EE6) /* 495 */, + CONST64(0x9FFCC84F3A78C748) /* 496 */, CONST64(0xF0CD1F72248576BB) /* 497 */, + CONST64(0xEC6974053638CFE4) /* 498 */, CONST64(0x2BA7B67C0CEC4E4C) /* 499 */, + CONST64(0xAC2F4DF3E5CE32ED) /* 500 */, CONST64(0xCB33D14326EA4C11) /* 501 */, + CONST64(0xA4E9044CC77E58BC) /* 502 */, CONST64(0x5F513293D934FCEF) /* 503 */, + CONST64(0x5DC9645506E55444) /* 504 */, CONST64(0x50DE418F317DE40A) /* 505 */, + CONST64(0x388CB31A69DDE259) /* 506 */, CONST64(0x2DB4A83455820A86) /* 507 */, + CONST64(0x9010A91E84711AE9) /* 508 */, CONST64(0x4DF7F0B7B1498371) /* 509 */, + CONST64(0xD62A2EABC0977179) /* 510 */, CONST64(0x22FAC097AA8D5C0E) /* 511 */, + CONST64(0xF49FCC2FF1DAF39B) /* 512 */, CONST64(0x487FD5C66FF29281) /* 513 */, + CONST64(0xE8A30667FCDCA83F) /* 514 */, CONST64(0x2C9B4BE3D2FCCE63) /* 515 */, + CONST64(0xDA3FF74B93FBBBC2) /* 516 */, CONST64(0x2FA165D2FE70BA66) /* 517 */, + CONST64(0xA103E279970E93D4) /* 518 */, CONST64(0xBECDEC77B0E45E71) /* 519 */, + CONST64(0xCFB41E723985E497) /* 520 */, CONST64(0xB70AAA025EF75017) /* 521 */, + CONST64(0xD42309F03840B8E0) /* 522 */, CONST64(0x8EFC1AD035898579) /* 523 */, + CONST64(0x96C6920BE2B2ABC5) /* 524 */, CONST64(0x66AF4163375A9172) /* 525 */, + CONST64(0x2174ABDCCA7127FB) /* 526 */, CONST64(0xB33CCEA64A72FF41) /* 527 */, + CONST64(0xF04A4933083066A5) /* 528 */, CONST64(0x8D970ACDD7289AF5) /* 529 */, + CONST64(0x8F96E8E031C8C25E) /* 530 */, CONST64(0xF3FEC02276875D47) /* 531 */, + CONST64(0xEC7BF310056190DD) /* 532 */, CONST64(0xF5ADB0AEBB0F1491) /* 533 */, + CONST64(0x9B50F8850FD58892) /* 534 */, CONST64(0x4975488358B74DE8) /* 535 */, + CONST64(0xA3354FF691531C61) /* 536 */, CONST64(0x0702BBE481D2C6EE) /* 537 */, + CONST64(0x89FB24057DEDED98) /* 538 */, CONST64(0xAC3075138596E902) /* 539 */, + CONST64(0x1D2D3580172772ED) /* 540 */, CONST64(0xEB738FC28E6BC30D) /* 541 */, + CONST64(0x5854EF8F63044326) /* 542 */, CONST64(0x9E5C52325ADD3BBE) /* 543 */, + CONST64(0x90AA53CF325C4623) /* 544 */, CONST64(0xC1D24D51349DD067) /* 545 */, + CONST64(0x2051CFEEA69EA624) /* 546 */, CONST64(0x13220F0A862E7E4F) /* 547 */, + CONST64(0xCE39399404E04864) /* 548 */, CONST64(0xD9C42CA47086FCB7) /* 549 */, + CONST64(0x685AD2238A03E7CC) /* 550 */, CONST64(0x066484B2AB2FF1DB) /* 551 */, + CONST64(0xFE9D5D70EFBF79EC) /* 552 */, CONST64(0x5B13B9DD9C481854) /* 553 */, + CONST64(0x15F0D475ED1509AD) /* 554 */, CONST64(0x0BEBCD060EC79851) /* 555 */, + CONST64(0xD58C6791183AB7F8) /* 556 */, CONST64(0xD1187C5052F3EEE4) /* 557 */, + CONST64(0xC95D1192E54E82FF) /* 558 */, CONST64(0x86EEA14CB9AC6CA2) /* 559 */, + CONST64(0x3485BEB153677D5D) /* 560 */, CONST64(0xDD191D781F8C492A) /* 561 */, + CONST64(0xF60866BAA784EBF9) /* 562 */, CONST64(0x518F643BA2D08C74) /* 563 */, + CONST64(0x8852E956E1087C22) /* 564 */, CONST64(0xA768CB8DC410AE8D) /* 565 */, + CONST64(0x38047726BFEC8E1A) /* 566 */, CONST64(0xA67738B4CD3B45AA) /* 567 */, + CONST64(0xAD16691CEC0DDE19) /* 568 */, CONST64(0xC6D4319380462E07) /* 569 */, + CONST64(0xC5A5876D0BA61938) /* 570 */, CONST64(0x16B9FA1FA58FD840) /* 571 */, + CONST64(0x188AB1173CA74F18) /* 572 */, CONST64(0xABDA2F98C99C021F) /* 573 */, + CONST64(0x3E0580AB134AE816) /* 574 */, CONST64(0x5F3B05B773645ABB) /* 575 */, + CONST64(0x2501A2BE5575F2F6) /* 576 */, CONST64(0x1B2F74004E7E8BA9) /* 577 */, + CONST64(0x1CD7580371E8D953) /* 578 */, CONST64(0x7F6ED89562764E30) /* 579 */, + CONST64(0xB15926FF596F003D) /* 580 */, CONST64(0x9F65293DA8C5D6B9) /* 581 */, + CONST64(0x6ECEF04DD690F84C) /* 582 */, CONST64(0x4782275FFF33AF88) /* 583 */, + CONST64(0xE41433083F820801) /* 584 */, CONST64(0xFD0DFE409A1AF9B5) /* 585 */, + CONST64(0x4325A3342CDB396B) /* 586 */, CONST64(0x8AE77E62B301B252) /* 587 */, + CONST64(0xC36F9E9F6655615A) /* 588 */, CONST64(0x85455A2D92D32C09) /* 589 */, + CONST64(0xF2C7DEA949477485) /* 590 */, CONST64(0x63CFB4C133A39EBA) /* 591 */, + CONST64(0x83B040CC6EBC5462) /* 592 */, CONST64(0x3B9454C8FDB326B0) /* 593 */, + CONST64(0x56F56A9E87FFD78C) /* 594 */, CONST64(0x2DC2940D99F42BC6) /* 595 */, + CONST64(0x98F7DF096B096E2D) /* 596 */, CONST64(0x19A6E01E3AD852BF) /* 597 */, + CONST64(0x42A99CCBDBD4B40B) /* 598 */, CONST64(0xA59998AF45E9C559) /* 599 */, + CONST64(0x366295E807D93186) /* 600 */, CONST64(0x6B48181BFAA1F773) /* 601 */, + CONST64(0x1FEC57E2157A0A1D) /* 602 */, CONST64(0x4667446AF6201AD5) /* 603 */, + CONST64(0xE615EBCACFB0F075) /* 604 */, CONST64(0xB8F31F4F68290778) /* 605 */, + CONST64(0x22713ED6CE22D11E) /* 606 */, CONST64(0x3057C1A72EC3C93B) /* 607 */, + CONST64(0xCB46ACC37C3F1F2F) /* 608 */, CONST64(0xDBB893FD02AAF50E) /* 609 */, + CONST64(0x331FD92E600B9FCF) /* 610 */, CONST64(0xA498F96148EA3AD6) /* 611 */, + CONST64(0xA8D8426E8B6A83EA) /* 612 */, CONST64(0xA089B274B7735CDC) /* 613 */, + CONST64(0x87F6B3731E524A11) /* 614 */, CONST64(0x118808E5CBC96749) /* 615 */, + CONST64(0x9906E4C7B19BD394) /* 616 */, CONST64(0xAFED7F7E9B24A20C) /* 617 */, + CONST64(0x6509EADEEB3644A7) /* 618 */, CONST64(0x6C1EF1D3E8EF0EDE) /* 619 */, + CONST64(0xB9C97D43E9798FB4) /* 620 */, CONST64(0xA2F2D784740C28A3) /* 621 */, + CONST64(0x7B8496476197566F) /* 622 */, CONST64(0x7A5BE3E6B65F069D) /* 623 */, + CONST64(0xF96330ED78BE6F10) /* 624 */, CONST64(0xEEE60DE77A076A15) /* 625 */, + CONST64(0x2B4BEE4AA08B9BD0) /* 626 */, CONST64(0x6A56A63EC7B8894E) /* 627 */, + CONST64(0x02121359BA34FEF4) /* 628 */, CONST64(0x4CBF99F8283703FC) /* 629 */, + CONST64(0x398071350CAF30C8) /* 630 */, CONST64(0xD0A77A89F017687A) /* 631 */, + CONST64(0xF1C1A9EB9E423569) /* 632 */, CONST64(0x8C7976282DEE8199) /* 633 */, + CONST64(0x5D1737A5DD1F7ABD) /* 634 */, CONST64(0x4F53433C09A9FA80) /* 635 */, + CONST64(0xFA8B0C53DF7CA1D9) /* 636 */, CONST64(0x3FD9DCBC886CCB77) /* 637 */, + CONST64(0xC040917CA91B4720) /* 638 */, CONST64(0x7DD00142F9D1DCDF) /* 639 */, + CONST64(0x8476FC1D4F387B58) /* 640 */, CONST64(0x23F8E7C5F3316503) /* 641 */, + CONST64(0x032A2244E7E37339) /* 642 */, CONST64(0x5C87A5D750F5A74B) /* 643 */, + CONST64(0x082B4CC43698992E) /* 644 */, CONST64(0xDF917BECB858F63C) /* 645 */, + CONST64(0x3270B8FC5BF86DDA) /* 646 */, CONST64(0x10AE72BB29B5DD76) /* 647 */, + CONST64(0x576AC94E7700362B) /* 648 */, CONST64(0x1AD112DAC61EFB8F) /* 649 */, + CONST64(0x691BC30EC5FAA427) /* 650 */, CONST64(0xFF246311CC327143) /* 651 */, + CONST64(0x3142368E30E53206) /* 652 */, CONST64(0x71380E31E02CA396) /* 653 */, + CONST64(0x958D5C960AAD76F1) /* 654 */, CONST64(0xF8D6F430C16DA536) /* 655 */, + CONST64(0xC8FFD13F1BE7E1D2) /* 656 */, CONST64(0x7578AE66004DDBE1) /* 657 */, + CONST64(0x05833F01067BE646) /* 658 */, CONST64(0xBB34B5AD3BFE586D) /* 659 */, + CONST64(0x095F34C9A12B97F0) /* 660 */, CONST64(0x247AB64525D60CA8) /* 661 */, + CONST64(0xDCDBC6F3017477D1) /* 662 */, CONST64(0x4A2E14D4DECAD24D) /* 663 */, + CONST64(0xBDB5E6D9BE0A1EEB) /* 664 */, CONST64(0x2A7E70F7794301AB) /* 665 */, + CONST64(0xDEF42D8A270540FD) /* 666 */, CONST64(0x01078EC0A34C22C1) /* 667 */, + CONST64(0xE5DE511AF4C16387) /* 668 */, CONST64(0x7EBB3A52BD9A330A) /* 669 */, + CONST64(0x77697857AA7D6435) /* 670 */, CONST64(0x004E831603AE4C32) /* 671 */, + CONST64(0xE7A21020AD78E312) /* 672 */, CONST64(0x9D41A70C6AB420F2) /* 673 */, + CONST64(0x28E06C18EA1141E6) /* 674 */, CONST64(0xD2B28CBD984F6B28) /* 675 */, + CONST64(0x26B75F6C446E9D83) /* 676 */, CONST64(0xBA47568C4D418D7F) /* 677 */, + CONST64(0xD80BADBFE6183D8E) /* 678 */, CONST64(0x0E206D7F5F166044) /* 679 */, + CONST64(0xE258A43911CBCA3E) /* 680 */, CONST64(0x723A1746B21DC0BC) /* 681 */, + CONST64(0xC7CAA854F5D7CDD3) /* 682 */, CONST64(0x7CAC32883D261D9C) /* 683 */, + CONST64(0x7690C26423BA942C) /* 684 */, CONST64(0x17E55524478042B8) /* 685 */, + CONST64(0xE0BE477656A2389F) /* 686 */, CONST64(0x4D289B5E67AB2DA0) /* 687 */, + CONST64(0x44862B9C8FBBFD31) /* 688 */, CONST64(0xB47CC8049D141365) /* 689 */, + CONST64(0x822C1B362B91C793) /* 690 */, CONST64(0x4EB14655FB13DFD8) /* 691 */, + CONST64(0x1ECBBA0714E2A97B) /* 692 */, CONST64(0x6143459D5CDE5F14) /* 693 */, + CONST64(0x53A8FBF1D5F0AC89) /* 694 */, CONST64(0x97EA04D81C5E5B00) /* 695 */, + CONST64(0x622181A8D4FDB3F3) /* 696 */, CONST64(0xE9BCD341572A1208) /* 697 */, + CONST64(0x1411258643CCE58A) /* 698 */, CONST64(0x9144C5FEA4C6E0A4) /* 699 */, + CONST64(0x0D33D06565CF620F) /* 700 */, CONST64(0x54A48D489F219CA1) /* 701 */, + CONST64(0xC43E5EAC6D63C821) /* 702 */, CONST64(0xA9728B3A72770DAF) /* 703 */, + CONST64(0xD7934E7B20DF87EF) /* 704 */, CONST64(0xE35503B61A3E86E5) /* 705 */, + CONST64(0xCAE321FBC819D504) /* 706 */, CONST64(0x129A50B3AC60BFA6) /* 707 */, + CONST64(0xCD5E68EA7E9FB6C3) /* 708 */, CONST64(0xB01C90199483B1C7) /* 709 */, + CONST64(0x3DE93CD5C295376C) /* 710 */, CONST64(0xAED52EDF2AB9AD13) /* 711 */, + CONST64(0x2E60F512C0A07884) /* 712 */, CONST64(0xBC3D86A3E36210C9) /* 713 */, + CONST64(0x35269D9B163951CE) /* 714 */, CONST64(0x0C7D6E2AD0CDB5FA) /* 715 */, + CONST64(0x59E86297D87F5733) /* 716 */, CONST64(0x298EF221898DB0E7) /* 717 */, + CONST64(0x55000029D1A5AA7E) /* 718 */, CONST64(0x8BC08AE1B5061B45) /* 719 */, + CONST64(0xC2C31C2B6C92703A) /* 720 */, CONST64(0x94CC596BAF25EF42) /* 721 */, + CONST64(0x0A1D73DB22540456) /* 722 */, CONST64(0x04B6A0F9D9C4179A) /* 723 */, + CONST64(0xEFFDAFA2AE3D3C60) /* 724 */, CONST64(0xF7C8075BB49496C4) /* 725 */, + CONST64(0x9CC5C7141D1CD4E3) /* 726 */, CONST64(0x78BD1638218E5534) /* 727 */, + CONST64(0xB2F11568F850246A) /* 728 */, CONST64(0xEDFABCFA9502BC29) /* 729 */, + CONST64(0x796CE5F2DA23051B) /* 730 */, CONST64(0xAAE128B0DC93537C) /* 731 */, + CONST64(0x3A493DA0EE4B29AE) /* 732 */, CONST64(0xB5DF6B2C416895D7) /* 733 */, + CONST64(0xFCABBD25122D7F37) /* 734 */, CONST64(0x70810B58105DC4B1) /* 735 */, + CONST64(0xE10FDD37F7882A90) /* 736 */, CONST64(0x524DCAB5518A3F5C) /* 737 */, + CONST64(0x3C9E85878451255B) /* 738 */, CONST64(0x4029828119BD34E2) /* 739 */, + CONST64(0x74A05B6F5D3CECCB) /* 740 */, CONST64(0xB610021542E13ECA) /* 741 */, + CONST64(0x0FF979D12F59E2AC) /* 742 */, CONST64(0x6037DA27E4F9CC50) /* 743 */, + CONST64(0x5E92975A0DF1847D) /* 744 */, CONST64(0xD66DE190D3E623FE) /* 745 */, + CONST64(0x5032D6B87B568048) /* 746 */, CONST64(0x9A36B7CE8235216E) /* 747 */, + CONST64(0x80272A7A24F64B4A) /* 748 */, CONST64(0x93EFED8B8C6916F7) /* 749 */, + CONST64(0x37DDBFF44CCE1555) /* 750 */, CONST64(0x4B95DB5D4B99BD25) /* 751 */, + CONST64(0x92D3FDA169812FC0) /* 752 */, CONST64(0xFB1A4A9A90660BB6) /* 753 */, + CONST64(0x730C196946A4B9B2) /* 754 */, CONST64(0x81E289AA7F49DA68) /* 755 */, + CONST64(0x64669A0F83B1A05F) /* 756 */, CONST64(0x27B3FF7D9644F48B) /* 757 */, + CONST64(0xCC6B615C8DB675B3) /* 758 */, CONST64(0x674F20B9BCEBBE95) /* 759 */, + CONST64(0x6F31238275655982) /* 760 */, CONST64(0x5AE488713E45CF05) /* 761 */, + CONST64(0xBF619F9954C21157) /* 762 */, CONST64(0xEABAC46040A8EAE9) /* 763 */, + CONST64(0x454C6FE9F2C0C1CD) /* 764 */, CONST64(0x419CF6496412691C) /* 765 */, + CONST64(0xD3DC3BEF265B0F70) /* 766 */, CONST64(0x6D0E60F5C3578A9E) /* 767 */, + CONST64(0x5B0E608526323C55) /* 768 */, CONST64(0x1A46C1A9FA1B59F5) /* 769 */, + CONST64(0xA9E245A17C4C8FFA) /* 770 */, CONST64(0x65CA5159DB2955D7) /* 771 */, + CONST64(0x05DB0A76CE35AFC2) /* 772 */, CONST64(0x81EAC77EA9113D45) /* 773 */, + CONST64(0x528EF88AB6AC0A0D) /* 774 */, CONST64(0xA09EA253597BE3FF) /* 775 */, + CONST64(0x430DDFB3AC48CD56) /* 776 */, CONST64(0xC4B3A67AF45CE46F) /* 777 */, + CONST64(0x4ECECFD8FBE2D05E) /* 778 */, CONST64(0x3EF56F10B39935F0) /* 779 */, + CONST64(0x0B22D6829CD619C6) /* 780 */, CONST64(0x17FD460A74DF2069) /* 781 */, + CONST64(0x6CF8CC8E8510ED40) /* 782 */, CONST64(0xD6C824BF3A6ECAA7) /* 783 */, + CONST64(0x61243D581A817049) /* 784 */, CONST64(0x048BACB6BBC163A2) /* 785 */, + CONST64(0xD9A38AC27D44CC32) /* 786 */, CONST64(0x7FDDFF5BAAF410AB) /* 787 */, + CONST64(0xAD6D495AA804824B) /* 788 */, CONST64(0xE1A6A74F2D8C9F94) /* 789 */, + CONST64(0xD4F7851235DEE8E3) /* 790 */, CONST64(0xFD4B7F886540D893) /* 791 */, + CONST64(0x247C20042AA4BFDA) /* 792 */, CONST64(0x096EA1C517D1327C) /* 793 */, + CONST64(0xD56966B4361A6685) /* 794 */, CONST64(0x277DA5C31221057D) /* 795 */, + CONST64(0x94D59893A43ACFF7) /* 796 */, CONST64(0x64F0C51CCDC02281) /* 797 */, + CONST64(0x3D33BCC4FF6189DB) /* 798 */, CONST64(0xE005CB184CE66AF1) /* 799 */, + CONST64(0xFF5CCD1D1DB99BEA) /* 800 */, CONST64(0xB0B854A7FE42980F) /* 801 */, + CONST64(0x7BD46A6A718D4B9F) /* 802 */, CONST64(0xD10FA8CC22A5FD8C) /* 803 */, + CONST64(0xD31484952BE4BD31) /* 804 */, CONST64(0xC7FA975FCB243847) /* 805 */, + CONST64(0x4886ED1E5846C407) /* 806 */, CONST64(0x28CDDB791EB70B04) /* 807 */, + CONST64(0xC2B00BE2F573417F) /* 808 */, CONST64(0x5C9590452180F877) /* 809 */, + CONST64(0x7A6BDDFFF370EB00) /* 810 */, CONST64(0xCE509E38D6D9D6A4) /* 811 */, + CONST64(0xEBEB0F00647FA702) /* 812 */, CONST64(0x1DCC06CF76606F06) /* 813 */, + CONST64(0xE4D9F28BA286FF0A) /* 814 */, CONST64(0xD85A305DC918C262) /* 815 */, + CONST64(0x475B1D8732225F54) /* 816 */, CONST64(0x2D4FB51668CCB5FE) /* 817 */, + CONST64(0xA679B9D9D72BBA20) /* 818 */, CONST64(0x53841C0D912D43A5) /* 819 */, + CONST64(0x3B7EAA48BF12A4E8) /* 820 */, CONST64(0x781E0E47F22F1DDF) /* 821 */, + CONST64(0xEFF20CE60AB50973) /* 822 */, CONST64(0x20D261D19DFFB742) /* 823 */, + CONST64(0x16A12B03062A2E39) /* 824 */, CONST64(0x1960EB2239650495) /* 825 */, + CONST64(0x251C16FED50EB8B8) /* 826 */, CONST64(0x9AC0C330F826016E) /* 827 */, + CONST64(0xED152665953E7671) /* 828 */, CONST64(0x02D63194A6369570) /* 829 */, + CONST64(0x5074F08394B1C987) /* 830 */, CONST64(0x70BA598C90B25CE1) /* 831 */, + CONST64(0x794A15810B9742F6) /* 832 */, CONST64(0x0D5925E9FCAF8C6C) /* 833 */, + CONST64(0x3067716CD868744E) /* 834 */, CONST64(0x910AB077E8D7731B) /* 835 */, + CONST64(0x6A61BBDB5AC42F61) /* 836 */, CONST64(0x93513EFBF0851567) /* 837 */, + CONST64(0xF494724B9E83E9D5) /* 838 */, CONST64(0xE887E1985C09648D) /* 839 */, + CONST64(0x34B1D3C675370CFD) /* 840 */, CONST64(0xDC35E433BC0D255D) /* 841 */, + CONST64(0xD0AAB84234131BE0) /* 842 */, CONST64(0x08042A50B48B7EAF) /* 843 */, + CONST64(0x9997C4EE44A3AB35) /* 844 */, CONST64(0x829A7B49201799D0) /* 845 */, + CONST64(0x263B8307B7C54441) /* 846 */, CONST64(0x752F95F4FD6A6CA6) /* 847 */, + CONST64(0x927217402C08C6E5) /* 848 */, CONST64(0x2A8AB754A795D9EE) /* 849 */, + CONST64(0xA442F7552F72943D) /* 850 */, CONST64(0x2C31334E19781208) /* 851 */, + CONST64(0x4FA98D7CEAEE6291) /* 852 */, CONST64(0x55C3862F665DB309) /* 853 */, + CONST64(0xBD0610175D53B1F3) /* 854 */, CONST64(0x46FE6CB840413F27) /* 855 */, + CONST64(0x3FE03792DF0CFA59) /* 856 */, CONST64(0xCFE700372EB85E8F) /* 857 */, + CONST64(0xA7BE29E7ADBCE118) /* 858 */, CONST64(0xE544EE5CDE8431DD) /* 859 */, + CONST64(0x8A781B1B41F1873E) /* 860 */, CONST64(0xA5C94C78A0D2F0E7) /* 861 */, + CONST64(0x39412E2877B60728) /* 862 */, CONST64(0xA1265EF3AFC9A62C) /* 863 */, + CONST64(0xBCC2770C6A2506C5) /* 864 */, CONST64(0x3AB66DD5DCE1CE12) /* 865 */, + CONST64(0xE65499D04A675B37) /* 866 */, CONST64(0x7D8F523481BFD216) /* 867 */, + CONST64(0x0F6F64FCEC15F389) /* 868 */, CONST64(0x74EFBE618B5B13C8) /* 869 */, + CONST64(0xACDC82B714273E1D) /* 870 */, CONST64(0xDD40BFE003199D17) /* 871 */, + CONST64(0x37E99257E7E061F8) /* 872 */, CONST64(0xFA52626904775AAA) /* 873 */, + CONST64(0x8BBBF63A463D56F9) /* 874 */, CONST64(0xF0013F1543A26E64) /* 875 */, + CONST64(0xA8307E9F879EC898) /* 876 */, CONST64(0xCC4C27A4150177CC) /* 877 */, + CONST64(0x1B432F2CCA1D3348) /* 878 */, CONST64(0xDE1D1F8F9F6FA013) /* 879 */, + CONST64(0x606602A047A7DDD6) /* 880 */, CONST64(0xD237AB64CC1CB2C7) /* 881 */, + CONST64(0x9B938E7225FCD1D3) /* 882 */, CONST64(0xEC4E03708E0FF476) /* 883 */, + CONST64(0xFEB2FBDA3D03C12D) /* 884 */, CONST64(0xAE0BCED2EE43889A) /* 885 */, + CONST64(0x22CB8923EBFB4F43) /* 886 */, CONST64(0x69360D013CF7396D) /* 887 */, + CONST64(0x855E3602D2D4E022) /* 888 */, CONST64(0x073805BAD01F784C) /* 889 */, + CONST64(0x33E17A133852F546) /* 890 */, CONST64(0xDF4874058AC7B638) /* 891 */, + CONST64(0xBA92B29C678AA14A) /* 892 */, CONST64(0x0CE89FC76CFAADCD) /* 893 */, + CONST64(0x5F9D4E0908339E34) /* 894 */, CONST64(0xF1AFE9291F5923B9) /* 895 */, + CONST64(0x6E3480F60F4A265F) /* 896 */, CONST64(0xEEBF3A2AB29B841C) /* 897 */, + CONST64(0xE21938A88F91B4AD) /* 898 */, CONST64(0x57DFEFF845C6D3C3) /* 899 */, + CONST64(0x2F006B0BF62CAAF2) /* 900 */, CONST64(0x62F479EF6F75EE78) /* 901 */, + CONST64(0x11A55AD41C8916A9) /* 902 */, CONST64(0xF229D29084FED453) /* 903 */, + CONST64(0x42F1C27B16B000E6) /* 904 */, CONST64(0x2B1F76749823C074) /* 905 */, + CONST64(0x4B76ECA3C2745360) /* 906 */, CONST64(0x8C98F463B91691BD) /* 907 */, + CONST64(0x14BCC93CF1ADE66A) /* 908 */, CONST64(0x8885213E6D458397) /* 909 */, + CONST64(0x8E177DF0274D4711) /* 910 */, CONST64(0xB49B73B5503F2951) /* 911 */, + CONST64(0x10168168C3F96B6B) /* 912 */, CONST64(0x0E3D963B63CAB0AE) /* 913 */, + CONST64(0x8DFC4B5655A1DB14) /* 914 */, CONST64(0xF789F1356E14DE5C) /* 915 */, + CONST64(0x683E68AF4E51DAC1) /* 916 */, CONST64(0xC9A84F9D8D4B0FD9) /* 917 */, + CONST64(0x3691E03F52A0F9D1) /* 918 */, CONST64(0x5ED86E46E1878E80) /* 919 */, + CONST64(0x3C711A0E99D07150) /* 920 */, CONST64(0x5A0865B20C4E9310) /* 921 */, + CONST64(0x56FBFC1FE4F0682E) /* 922 */, CONST64(0xEA8D5DE3105EDF9B) /* 923 */, + CONST64(0x71ABFDB12379187A) /* 924 */, CONST64(0x2EB99DE1BEE77B9C) /* 925 */, + CONST64(0x21ECC0EA33CF4523) /* 926 */, CONST64(0x59A4D7521805C7A1) /* 927 */, + CONST64(0x3896F5EB56AE7C72) /* 928 */, CONST64(0xAA638F3DB18F75DC) /* 929 */, + CONST64(0x9F39358DABE9808E) /* 930 */, CONST64(0xB7DEFA91C00B72AC) /* 931 */, + CONST64(0x6B5541FD62492D92) /* 932 */, CONST64(0x6DC6DEE8F92E4D5B) /* 933 */, + CONST64(0x353F57ABC4BEEA7E) /* 934 */, CONST64(0x735769D6DA5690CE) /* 935 */, + CONST64(0x0A234AA642391484) /* 936 */, CONST64(0xF6F9508028F80D9D) /* 937 */, + CONST64(0xB8E319A27AB3F215) /* 938 */, CONST64(0x31AD9C1151341A4D) /* 939 */, + CONST64(0x773C22A57BEF5805) /* 940 */, CONST64(0x45C7561A07968633) /* 941 */, + CONST64(0xF913DA9E249DBE36) /* 942 */, CONST64(0xDA652D9B78A64C68) /* 943 */, + CONST64(0x4C27A97F3BC334EF) /* 944 */, CONST64(0x76621220E66B17F4) /* 945 */, + CONST64(0x967743899ACD7D0B) /* 946 */, CONST64(0xF3EE5BCAE0ED6782) /* 947 */, + CONST64(0x409F753600C879FC) /* 948 */, CONST64(0x06D09A39B5926DB6) /* 949 */, + CONST64(0x6F83AEB0317AC588) /* 950 */, CONST64(0x01E6CA4A86381F21) /* 951 */, + CONST64(0x66FF3462D19F3025) /* 952 */, CONST64(0x72207C24DDFD3BFB) /* 953 */, + CONST64(0x4AF6B6D3E2ECE2EB) /* 954 */, CONST64(0x9C994DBEC7EA08DE) /* 955 */, + CONST64(0x49ACE597B09A8BC4) /* 956 */, CONST64(0xB38C4766CF0797BA) /* 957 */, + CONST64(0x131B9373C57C2A75) /* 958 */, CONST64(0xB1822CCE61931E58) /* 959 */, + CONST64(0x9D7555B909BA1C0C) /* 960 */, CONST64(0x127FAFDD937D11D2) /* 961 */, + CONST64(0x29DA3BADC66D92E4) /* 962 */, CONST64(0xA2C1D57154C2ECBC) /* 963 */, + CONST64(0x58C5134D82F6FE24) /* 964 */, CONST64(0x1C3AE3515B62274F) /* 965 */, + CONST64(0xE907C82E01CB8126) /* 966 */, CONST64(0xF8ED091913E37FCB) /* 967 */, + CONST64(0x3249D8F9C80046C9) /* 968 */, CONST64(0x80CF9BEDE388FB63) /* 969 */, + CONST64(0x1881539A116CF19E) /* 970 */, CONST64(0x5103F3F76BD52457) /* 971 */, + CONST64(0x15B7E6F5AE47F7A8) /* 972 */, CONST64(0xDBD7C6DED47E9CCF) /* 973 */, + CONST64(0x44E55C410228BB1A) /* 974 */, CONST64(0xB647D4255EDB4E99) /* 975 */, + CONST64(0x5D11882BB8AAFC30) /* 976 */, CONST64(0xF5098BBB29D3212A) /* 977 */, + CONST64(0x8FB5EA14E90296B3) /* 978 */, CONST64(0x677B942157DD025A) /* 979 */, + CONST64(0xFB58E7C0A390ACB5) /* 980 */, CONST64(0x89D3674C83BD4A01) /* 981 */, + CONST64(0x9E2DA4DF4BF3B93B) /* 982 */, CONST64(0xFCC41E328CAB4829) /* 983 */, + CONST64(0x03F38C96BA582C52) /* 984 */, CONST64(0xCAD1BDBD7FD85DB2) /* 985 */, + CONST64(0xBBB442C16082AE83) /* 986 */, CONST64(0xB95FE86BA5DA9AB0) /* 987 */, + CONST64(0xB22E04673771A93F) /* 988 */, CONST64(0x845358C9493152D8) /* 989 */, + CONST64(0xBE2A488697B4541E) /* 990 */, CONST64(0x95A2DC2DD38E6966) /* 991 */, + CONST64(0xC02C11AC923C852B) /* 992 */, CONST64(0x2388B1990DF2A87B) /* 993 */, + CONST64(0x7C8008FA1B4F37BE) /* 994 */, CONST64(0x1F70D0C84D54E503) /* 995 */, + CONST64(0x5490ADEC7ECE57D4) /* 996 */, CONST64(0x002B3C27D9063A3A) /* 997 */, + CONST64(0x7EAEA3848030A2BF) /* 998 */, CONST64(0xC602326DED2003C0) /* 999 */, + CONST64(0x83A7287D69A94086) /* 1000 */, CONST64(0xC57A5FCB30F57A8A) /* 1001 */, + CONST64(0xB56844E479EBE779) /* 1002 */, CONST64(0xA373B40F05DCBCE9) /* 1003 */, + CONST64(0xD71A786E88570EE2) /* 1004 */, CONST64(0x879CBACDBDE8F6A0) /* 1005 */, + CONST64(0x976AD1BCC164A32F) /* 1006 */, CONST64(0xAB21E25E9666D78B) /* 1007 */, + CONST64(0x901063AAE5E5C33C) /* 1008 */, CONST64(0x9818B34448698D90) /* 1009 */, + CONST64(0xE36487AE3E1E8ABB) /* 1010 */, CONST64(0xAFBDF931893BDCB4) /* 1011 */, + CONST64(0x6345A0DC5FBBD519) /* 1012 */, CONST64(0x8628FE269B9465CA) /* 1013 */, + CONST64(0x1E5D01603F9C51EC) /* 1014 */, CONST64(0x4DE44006A15049B7) /* 1015 */, + CONST64(0xBF6C70E5F776CBB1) /* 1016 */, CONST64(0x411218F2EF552BED) /* 1017 */, + CONST64(0xCB0C0708705A36A3) /* 1018 */, CONST64(0xE74D14754F986044) /* 1019 */, + CONST64(0xCD56D9430EA8280E) /* 1020 */, CONST64(0xC12591D7535F5065) /* 1021 */, + CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */}; + +#ifdef _MSC_VER + #define INLINE __inline +#else + #define INLINE +#endif + +/* one round of the hash function */ +INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul) +{ + ulong64 tmp; + tmp = (*c ^= x); + *a -= t1[byte(tmp, 0)] ^ t2[byte(tmp, 2)] ^ t3[byte(tmp, 4)] ^ t4[byte(tmp, 6)]; + tmp = (*b += t4[byte(tmp, 1)] ^ t3[byte(tmp, 3)] ^ t2[byte(tmp,5)] ^ t1[byte(tmp,7)]); + switch (mul) { + case 5: *b = (tmp << 2) + tmp; break; + case 7: *b = (tmp << 3) - tmp; break; + case 9: *b = (tmp << 3) + tmp; break; + } +} + +/* one complete pass */ +static void pass(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 *x, int mul) +{ + tiger_round(a,b,c,x[0],mul); + tiger_round(b,c,a,x[1],mul); + tiger_round(c,a,b,x[2],mul); + tiger_round(a,b,c,x[3],mul); + tiger_round(b,c,a,x[4],mul); + tiger_round(c,a,b,x[5],mul); + tiger_round(a,b,c,x[6],mul); + tiger_round(b,c,a,x[7],mul); +} + +/* The key mixing schedule */ +static void key_schedule(ulong64 *x) +{ + x[0] -= x[7] ^ CONST64(0xA5A5A5A5A5A5A5A5); + x[1] ^= x[0]; + x[2] += x[1]; + x[3] -= x[2] ^ ((~x[1])<<19); + x[4] ^= x[3]; + x[5] += x[4]; + x[6] -= x[5] ^ ((~x[4])>>23); + x[7] ^= x[6]; + x[0] += x[7]; + x[1] -= x[0] ^ ((~x[7])<<19); + x[2] ^= x[1]; + x[3] += x[2]; + x[4] -= x[3] ^ ((~x[2])>>23); + x[5] ^= x[4]; + x[6] += x[5]; + x[7] -= x[6] ^ CONST64(0x0123456789ABCDEF); +} + +#ifdef LTC_CLEAN_STACK +static int _tiger_compress(hash_state *md, unsigned char *buf) +#else +static int tiger_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong64 a, b, c, x[8]; + unsigned long i; + + /* load words */ + for (i = 0; i < 8; i++) { + LOAD64L(x[i],&buf[8*i]); + } + a = md->tiger.state[0]; + b = md->tiger.state[1]; + c = md->tiger.state[2]; + + pass(&a,&b,&c,x,5); + key_schedule(x); + pass(&c,&a,&b,x,7); + key_schedule(x); + pass(&b,&c,&a,x,9); + + /* store state */ + md->tiger.state[0] = a ^ md->tiger.state[0]; + md->tiger.state[1] = b - md->tiger.state[1]; + md->tiger.state[2] = c + md->tiger.state[2]; + + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int tiger_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _tiger_compress(md, buf); + burn_stack(sizeof(ulong64) * 11 + sizeof(unsigned long)); + return err; +} +#endif + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int tiger_init(hash_state *md) +{ + LTC_ARGCHK(md != NULL); + md->tiger.state[0] = CONST64(0x0123456789ABCDEF); + md->tiger.state[1] = CONST64(0xFEDCBA9876543210); + md->tiger.state[2] = CONST64(0xF096A5B4C3B2E187); + md->tiger.curlen = 0; + md->tiger.length = 0; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(tiger_process, tiger_compress, tiger, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (24 bytes) + @return CRYPT_OK if successful +*/ +int tiger_done(hash_state * md, unsigned char *out) +{ + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->tiger.curlen >= sizeof(md->tiger.buf)) { + return CRYPT_INVALID_ARG; + } + + /* increase the length of the message */ + md->tiger.length += md->tiger.curlen * 8; + + /* append the '1' bit */ + md->tiger.buf[md->tiger.curlen++] = (unsigned char)0x01; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. */ + if (md->tiger.curlen > 56) { + while (md->tiger.curlen < 64) { + md->tiger.buf[md->tiger.curlen++] = (unsigned char)0; + } + tiger_compress(md, md->tiger.buf); + md->tiger.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->tiger.curlen < 56) { + md->tiger.buf[md->tiger.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md->tiger.length, md->tiger.buf+56); + tiger_compress(md, md->tiger.buf); + + /* copy output */ + STORE64L(md->tiger.state[0], &out[0]); + STORE64L(md->tiger.state[1], &out[8]); + STORE64L(md->tiger.state[2], &out[16]); +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(hash_state)); +#endif + + return CRYPT_OK; +} + +void calc_tiger(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + tiger_init(&md); + tiger_process(&md,message,len); + tiger_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,24); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int tiger_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + char *msg; + unsigned char hash[24]; + } tests[] = { + { "", + { 0x32, 0x93, 0xac, 0x63, 0x0c, 0x13, 0xf0, 0x24, + 0x5f, 0x92, 0xbb, 0xb1, 0x76, 0x6e, 0x16, 0x16, + 0x7a, 0x4e, 0x58, 0x49, 0x2d, 0xde, 0x73, 0xf3 } + }, + { "abc", + { 0x2a, 0xab, 0x14, 0x84, 0xe8, 0xc1, 0x58, 0xf2, + 0xbf, 0xb8, 0xc5, 0xff, 0x41, 0xb5, 0x7a, 0x52, + 0x51, 0x29, 0x13, 0x1c, 0x95, 0x7b, 0x5f, 0x93 } + }, + { "Tiger", + { 0xdd, 0x00, 0x23, 0x07, 0x99, 0xf5, 0x00, 0x9f, + 0xec, 0x6d, 0xeb, 0xc8, 0x38, 0xbb, 0x6a, 0x27, + 0xdf, 0x2b, 0x9d, 0x6f, 0x11, 0x0c, 0x79, 0x37 } + }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-", + { 0xf7, 0x1c, 0x85, 0x83, 0x90, 0x2a, 0xfb, 0x87, + 0x9e, 0xdf, 0xe6, 0x10, 0xf8, 0x2c, 0x0d, 0x47, + 0x86, 0xa3, 0xa5, 0x34, 0x50, 0x44, 0x86, 0xb5 } + }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-", + { 0xc5, 0x40, 0x34, 0xe5, 0xb4, 0x3e, 0xb8, 0x00, + 0x58, 0x48, 0xa7, 0xe0, 0xae, 0x6a, 0xac, 0x76, + 0xe4, 0xff, 0x59, 0x0a, 0xe7, 0x15, 0xfd, 0x25 } + }, + }; + + int i; + unsigned char tmp[24]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + tiger_init(&md); + tiger_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + tiger_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 24) != 0) { + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + +#endif + +/* +Hash of "": + 24F0130C63AC9332 16166E76B1BB925F F373DE2D49584E7A +Hash of "abc": + F258C1E88414AB2A 527AB541FFC5B8BF 935F7B951C132951 +Hash of "Tiger": + 9F00F599072300DD 276ABB38C8EB6DEC 37790C116F9D2BDF +Hash of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-": + 87FB2A9083851CF7 470D2CF810E6DF9E B586445034A5A386 +Hash of "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789": + 467DB80863EBCE48 8DF1CD1261655DE9 57896565975F9197 +Hash of "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham": + 0C410A042968868A 1671DA5A3FD29A72 5EC1E457D3CDB303 +Hash of "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge.": + EBF591D5AFA655CE 7F22894FF87F54AC 89C811B6B0DA3193 +Hash of "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.": + 3D9AEB03D1BD1A63 57B2774DFD6D5B24 DD68151D503974FC +Hash of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-": + 00B83EB4E53440C5 76AC6AAEE0A74858 25FD15E70A59FFE4 +*/ + + + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/tiger.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:25:28 $ */ diff --git a/crypto777/hmac/tomcrypt.h b/crypto777/hmac/tomcrypt.h new file mode 100755 index 000000000..a8018bf85 --- /dev/null +++ b/crypto777/hmac/tomcrypt.h @@ -0,0 +1,95 @@ +#ifndef TOMCRYPT_H_ +#define TOMCRYPT_H_ +//#include +//#include +//#include +//#include +//#include +//#include +//#include + +/* use configuration data */ +#include +#include +#include +#include +#include +#define ulong32 uint32_t +#define zeromem(buf,len) memset(buf,0,len) + +#include "tomcrypt_custom.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* version */ +#define CRYPT 0x0117 +#define SCRYPT "1.17" + +/* max size of either a cipher/hash block or symmetric key [largest of the two] */ +#define MAXBLOCKSIZE 128 + +/* descriptor table size */ +#define TAB_SIZE 32 + +/* error codes [will be expanded in future releases] */ +enum { + CRYPT_OK=0, /* Result OK */ + CRYPT_ERROR, /* Generic Error */ + CRYPT_NOP, /* Not a failure but no operation was performed */ + + CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ + CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ + CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ + + CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ + CRYPT_INVALID_PACKET, /* Invalid input packet given */ + + CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ + CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ + + CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ + CRYPT_INVALID_HASH, /* Invalid hash specified */ + CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ + + CRYPT_MEM, /* Out of memory */ + + CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ + CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ + + CRYPT_INVALID_ARG, /* Generic invalid argument */ + CRYPT_FILE_NOTFOUND, /* File Not Found */ + + CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ + CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ + CRYPT_PK_DUP, /* Duplicate key already in key ring */ + CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ + CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ + + CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */ + CRYPT_PK_INVALID_PADDING /* Invalid padding on input */ +}; + +#include "tomcrypt_cfg.h" +#include "tomcrypt_macros.h" +#include "tomcrypt_cipher.h" +#include "tomcrypt_hash.h" +#include "tomcrypt_mac.h" +#include "tomcrypt_prng.h" +#include "tomcrypt_pk.h" +#include "tomcrypt_math.h" +#include "tomcrypt_misc.h" +#include "tomcrypt_argchk.h" +#include "tomcrypt_pkcs.h" + +#ifdef __cplusplus + } +#endif + +#endif /* TOMCRYPT_H_ */ + + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt.h,v $ */ +/* $Revision: 1.21 $ */ +/* $Date: 2006/12/16 19:34:05 $ */ diff --git a/crypto777/hmac/tomcrypt_argchk.h b/crypto777/hmac/tomcrypt_argchk.h new file mode 100755 index 000000000..7efbba0b4 --- /dev/null +++ b/crypto777/hmac/tomcrypt_argchk.h @@ -0,0 +1,38 @@ +/* Defines the LTC_ARGCHK macro used within the library */ +/* ARGTYPE is defined in mycrypt_cfg.h */ +#if ARGTYPE == 0 + +//#include + +/* this is the default LibTomCrypt macro */ +void crypt_argchk(char *v, char *s, int d); +#define LTC_ARGCHK(x) if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } +#define LTC_ARGCHKVD(x) LTC_ARGCHK(x) + +#elif ARGTYPE == 1 + +/* fatal type of error */ +#define LTC_ARGCHK(x) assert((x)) +#define LTC_ARGCHKVD(x) LTC_ARGCHK(x) + +#elif ARGTYPE == 2 + +#define LTC_ARGCHK(x) if (!(x)) { fprintf(stderr, "\nwarning: ARGCHK failed at %s:%d\n", __FILE__, __LINE__); } +#define LTC_ARGCHKVD(x) LTC_ARGCHK(x) + +#elif ARGTYPE == 3 + +#define LTC_ARGCHK(x) +#define LTC_ARGCHKVD(x) LTC_ARGCHK(x) + +#elif ARGTYPE == 4 + +#define LTC_ARGCHK(x) if (!(x)) return CRYPT_INVALID_ARG; +#define LTC_ARGCHKVD(x) if (!(x)) return; + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_argchk.h,v $ */ +/* $Revision: 1.5 $ */ +/* $Date: 2006/08/27 20:50:21 $ */ diff --git a/crypto777/hmac/tomcrypt_cfg.h b/crypto777/hmac/tomcrypt_cfg.h new file mode 100755 index 000000000..f1e6bc9e3 --- /dev/null +++ b/crypto777/hmac/tomcrypt_cfg.h @@ -0,0 +1,140 @@ +/* This is the build config file. + * + * With this you can setup what to inlcude/exclude automatically during any build. Just comment + * out the line that #define's the word for the thing you want to remove. phew! + */ + +#ifndef TOMCRYPT_CFG_H +#define TOMCRYPT_CFG_H + +#if defined(_WIN32) || defined(_MSC_VER) +#define LTC_CALL __cdecl +#else +#ifndef LTC_CALL + #define LTC_CALL +#endif +#endif + +#ifndef LTC_EXPORT +#define LTC_EXPORT +#endif + +/* certain platforms use macros for these, making the prototypes broken */ +#ifndef LTC_NO_PROTOTYPES + +/* you can change how memory allocation works ... */ +LTC_EXPORT void * LTC_CALL XMALLOC(size_t n); +LTC_EXPORT void * LTC_CALL XREALLOC(void *p, size_t n); +LTC_EXPORT void * LTC_CALL XCALLOC(size_t n, size_t s); +LTC_EXPORT void LTC_CALL XFREE(void *p); + +LTC_EXPORT void LTC_CALL XQSORT(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); + + +/* change the clock function too */ +#ifndef _WIN32 +LTC_EXPORT clock_t LTC_CALL XCLOCK(void); +#endif + +/* various other functions */ +LTC_EXPORT void * LTC_CALL XMEMCPY(void *dest, const void *src, size_t n); +LTC_EXPORT int LTC_CALL XMEMCMP(const void *s1, const void *s2, size_t n); +LTC_EXPORT void * LTC_CALL XMEMSET(void *s, int c, size_t n); + +LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2); + +#endif + +/* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */ +#ifndef ARGTYPE + #define ARGTYPE 0 +#endif + +/* Controls endianess and size of registers. Leave uncommented to get platform neutral [slower] code + * + * Note: in order to use the optimized macros your platform must support unaligned 32 and 64 bit read/writes. + * The x86 platforms allow this but some others [ARM for instance] do not. On those platforms you **MUST** + * use the portable [slower] macros. + */ + +/* detect x86-32 machines somewhat */ +#if !defined(__STRICT_ANSI__) && (defined(INTEL_CC) || (defined(_MSC_VER) && defined(WIN32)) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__)))) + #define ENDIAN_LITTLE + #define ENDIAN_32BITWORD + #define LTC_FAST + #define LTC_FAST_TYPE unsigned long +#endif + +/* detects MIPS R5900 processors (PS2) */ +#if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips)) + #define ENDIAN_LITTLE + #define ENDIAN_64BITWORD +#endif + +/* detect amd64 */ +#if !defined(__STRICT_ANSI__) && defined(__x86_64__) + #define ENDIAN_LITTLE + #define ENDIAN_64BITWORD + #define LTC_FAST + #define LTC_FAST_TYPE unsigned long +#endif + +/* detect PPC32 */ +#if !defined(__STRICT_ANSI__) && defined(LTC_PPC32) + #define ENDIAN_BIG + #define ENDIAN_32BITWORD + #define LTC_FAST + #define LTC_FAST_TYPE unsigned long +#endif + +/* detect sparc and sparc64 */ +#if defined(__sparc__) + #define ENDIAN_BIG + #if defined(__arch64__) + #define ENDIAN_64BITWORD + #else + #define ENDIAN_32BITWORD + #endif +#endif + + +#ifdef LTC_NO_FAST + #ifdef LTC_FAST + #undef LTC_FAST + #endif +#endif + +#define LTC_NO_ASM + +/* No asm is a quick way to disable anything "not portable" */ +#ifdef LTC_NO_ASM + #undef ENDIAN_LITTLE + #undef ENDIAN_BIG + #undef ENDIAN_32BITWORD + #undef ENDIAN_64BITWORD + #undef LTC_FAST + #undef LTC_FAST_TYPE + #define LTC_NO_ROLC + #define LTC_NO_BSWAP +#endif + +/* #define ENDIAN_LITTLE */ +/* #define ENDIAN_BIG */ + +/* #define ENDIAN_32BITWORD */ +/* #define ENDIAN_64BITWORD */ + +#if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD)) + #error You must specify a word size as well as endianess in tomcrypt_cfg.h +#endif + +#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) + #define ENDIAN_NEUTRAL +#endif + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_cfg.h,v $ */ +/* $Revision: 1.19 $ */ +/* $Date: 2006/12/04 02:19:48 $ */ diff --git a/crypto777/hmac/tomcrypt_cipher.h b/crypto777/hmac/tomcrypt_cipher.h new file mode 100755 index 000000000..bd740bf4a --- /dev/null +++ b/crypto777/hmac/tomcrypt_cipher.h @@ -0,0 +1,891 @@ +/* ---- SYMMETRIC KEY STUFF ----- + * + * We put each of the ciphers scheduled keys in their own structs then we put all of + * the key formats in one union. This makes the function prototypes easier to use. + */ +#ifdef LTC_BLOWFISH +struct blowfish_key { + ulong32 S[4][256]; + ulong32 K[18]; +}; +#endif + +#ifdef LTC_RC5 +struct rc5_key { + int rounds; + ulong32 K[50]; +}; +#endif + +#ifdef LTC_RC6 +struct rc6_key { + ulong32 K[44]; +}; +#endif + +#ifdef LTC_SAFERP +struct saferp_key { + unsigned char K[33][16]; + long rounds; +}; +#endif + +#ifdef LTC_RIJNDAEL +struct rijndael_key { + ulong32 eK[60], dK[60]; + int Nr; +}; +#endif + +#ifdef LTC_KSEED +struct kseed_key { + ulong32 K[32], dK[32]; +}; +#endif + +#ifdef LTC_KASUMI +struct kasumi_key { + ulong32 KLi1[8], KLi2[8], + KOi1[8], KOi2[8], KOi3[8], + KIi1[8], KIi2[8], KIi3[8]; +}; +#endif + +#ifdef LTC_XTEA +struct xtea_key { + unsigned long A[32], B[32]; +}; +#endif + +#ifdef LTC_TWOFISH +#ifndef LTC_TWOFISH_SMALL + struct twofish_key { + ulong32 S[4][256], K[40]; + }; +#else + struct twofish_key { + ulong32 K[40]; + unsigned char S[32], start; + }; +#endif +#endif + +#ifdef LTC_SAFER +#define LTC_SAFER_K64_DEFAULT_NOF_ROUNDS 6 +#define LTC_SAFER_K128_DEFAULT_NOF_ROUNDS 10 +#define LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS 8 +#define LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS 10 +#define LTC_SAFER_MAX_NOF_ROUNDS 13 +#define LTC_SAFER_BLOCK_LEN 8 +#define LTC_SAFER_KEY_LEN (1 + LTC_SAFER_BLOCK_LEN * (1 + 2 * LTC_SAFER_MAX_NOF_ROUNDS)) +typedef unsigned char safer_block_t[LTC_SAFER_BLOCK_LEN]; +typedef unsigned char safer_key_t[LTC_SAFER_KEY_LEN]; +struct safer_key { safer_key_t key; }; +#endif + +#ifdef LTC_RC2 +struct rc2_key { unsigned xkey[64]; }; +#endif + +#ifdef LTC_DES +struct des_key { + ulong32 ek[32], dk[32]; +}; + +struct des3_key { + ulong32 ek[3][32], dk[3][32]; +}; +#endif + +#ifdef LTC_CAST5 +struct cast5_key { + ulong32 K[32], keylen; +}; +#endif + +#ifdef LTC_NOEKEON +struct noekeon_key { + ulong32 K[4], dK[4]; +}; +#endif + +#ifdef LTC_SKIPJACK +struct skipjack_key { + unsigned char key[10]; +}; +#endif + +#ifdef LTC_KHAZAD +struct khazad_key { + ulong64 roundKeyEnc[8 + 1]; + ulong64 roundKeyDec[8 + 1]; +}; +#endif + +#ifdef LTC_ANUBIS +struct anubis_key { + int keyBits; + int R; + ulong32 roundKeyEnc[18 + 1][4]; + ulong32 roundKeyDec[18 + 1][4]; +}; +#endif + +#ifdef LTC_MULTI2 +struct multi2_key { + int N; + ulong32 uk[8]; +}; +#endif + +typedef union Symmetric_key { +#ifdef LTC_DES + struct des_key des; + struct des3_key des3; +#endif +#ifdef LTC_RC2 + struct rc2_key rc2; +#endif +#ifdef LTC_SAFER + struct safer_key safer; +#endif +#ifdef LTC_TWOFISH + struct twofish_key twofish; +#endif +#ifdef LTC_BLOWFISH + struct blowfish_key blowfish; +#endif +#ifdef LTC_RC5 + struct rc5_key rc5; +#endif +#ifdef LTC_RC6 + struct rc6_key rc6; +#endif +#ifdef LTC_SAFERP + struct saferp_key saferp; +#endif +#ifdef LTC_RIJNDAEL + struct rijndael_key rijndael; +#endif +#ifdef LTC_XTEA + struct xtea_key xtea; +#endif +#ifdef LTC_CAST5 + struct cast5_key cast5; +#endif +#ifdef LTC_NOEKEON + struct noekeon_key noekeon; +#endif +#ifdef LTC_SKIPJACK + struct skipjack_key skipjack; +#endif +#ifdef LTC_KHAZAD + struct khazad_key khazad; +#endif +#ifdef LTC_ANUBIS + struct anubis_key anubis; +#endif +#ifdef LTC_KSEED + struct kseed_key kseed; +#endif +#ifdef LTC_KASUMI + struct kasumi_key kasumi; +#endif +#ifdef LTC_MULTI2 + struct multi2_key multi2; +#endif + void *data; +} symmetric_key; + +#ifdef LTC_ECB_MODE +/** A block cipher ECB structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen; + /** The scheduled key */ + symmetric_key key; +} symmetric_ECB; +#endif + +#ifdef LTC_CFB_MODE +/** A block cipher CFB structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen, + /** The padding offset */ + padlen; + /** The current IV */ + unsigned char IV[MAXBLOCKSIZE], + /** The pad used to encrypt/decrypt */ + pad[MAXBLOCKSIZE]; + /** The scheduled key */ + symmetric_key key; +} symmetric_CFB; +#endif + +#ifdef LTC_OFB_MODE +/** A block cipher OFB structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen, + /** The padding offset */ + padlen; + /** The current IV */ + unsigned char IV[MAXBLOCKSIZE]; + /** The scheduled key */ + symmetric_key key; +} symmetric_OFB; +#endif + +#ifdef LTC_CBC_MODE +/** A block cipher CBC structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen; + /** The current IV */ + unsigned char IV[MAXBLOCKSIZE]; + /** The scheduled key */ + symmetric_key key; +} symmetric_CBC; +#endif + + +#ifdef LTC_CTR_MODE +/** A block cipher CTR structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen, + /** The padding offset */ + padlen, + /** The mode (endianess) of the CTR, 0==little, 1==big */ + mode, + /** counter width */ + ctrlen; + + /** The counter */ + unsigned char ctr[MAXBLOCKSIZE], + /** The pad used to encrypt/decrypt */ + pad[MAXBLOCKSIZE]; + /** The scheduled key */ + symmetric_key key; +} symmetric_CTR; +#endif + + +#ifdef LTC_LRW_MODE +/** A LRW structure */ +typedef struct { + /** The index of the cipher chosen (must be a 128-bit block cipher) */ + int cipher; + + /** The current IV */ + unsigned char IV[16], + + /** the tweak key */ + tweak[16], + + /** The current pad, it's the product of the first 15 bytes against the tweak key */ + pad[16]; + + /** The scheduled symmetric key */ + symmetric_key key; + +#ifdef LRW_TABLES + /** The pre-computed multiplication table */ + unsigned char PC[16][256][16]; +#endif +} symmetric_LRW; +#endif + +#ifdef LTC_F8_MODE +/** A block cipher F8 structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen, + /** The padding offset */ + padlen; + /** The current IV */ + unsigned char IV[MAXBLOCKSIZE], + MIV[MAXBLOCKSIZE]; + /** Current block count */ + ulong32 blockcnt; + /** The scheduled key */ + symmetric_key key; +} symmetric_F8; +#endif + + +/** cipher descriptor table, last entry has "name == NULL" to mark the end of table */ +extern struct ltc_cipher_descriptor { + /** name of cipher */ + char *name; + /** internal ID */ + unsigned char ID; + /** min keysize (octets) */ + int min_key_length, + /** max keysize (octets) */ + max_key_length, + /** block size (octets) */ + block_length, + /** default number of rounds */ + default_rounds; + /** Setup the cipher + @param key The input symmetric key + @param keylen The length of the input key (octets) + @param num_rounds The requested number of rounds (0==default) + @param skey [out] The destination of the scheduled key + @return CRYPT_OK if successful + */ + int (*setup)(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); + /** Encrypt a block + @param pt The plaintext + @param ct [out] The ciphertext + @param skey The scheduled key + @return CRYPT_OK if successful + */ + int (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); + /** Decrypt a block + @param ct The ciphertext + @param pt [out] The plaintext + @param skey The scheduled key + @return CRYPT_OK if successful + */ + int (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); + /** Test the block cipher + @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled + */ + int (*test)(void); + + /** Terminate the context + @param skey The scheduled key + */ + void (*done)(symmetric_key *skey); + + /** Determine a key size + @param keysize [in/out] The size of the key desired and the suggested size + @return CRYPT_OK if successful + */ + int (*keysize)(int *keysize); + +/** Accelerators **/ + /** Accelerated ECB encryption + @param pt Plaintext + @param ct Ciphertext + @param blocks The number of complete blocks to process + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey); + + /** Accelerated ECB decryption + @param pt Plaintext + @param ct Ciphertext + @param blocks The number of complete blocks to process + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey); + + /** Accelerated CBC encryption + @param pt Plaintext + @param ct Ciphertext + @param blocks The number of complete blocks to process + @param IV The initial value (input/output) + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey); + + /** Accelerated CBC decryption + @param pt Plaintext + @param ct Ciphertext + @param blocks The number of complete blocks to process + @param IV The initial value (input/output) + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey); + + /** Accelerated CTR encryption + @param pt Plaintext + @param ct Ciphertext + @param blocks The number of complete blocks to process + @param IV The initial value (input/output) + @param mode little or big endian counter (mode=0 or mode=1) + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey); + + /** Accelerated LRW + @param pt Plaintext + @param ct Ciphertext + @param blocks The number of complete blocks to process + @param IV The initial value (input/output) + @param tweak The LRW tweak + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey); + + /** Accelerated LRW + @param ct Ciphertext + @param pt Plaintext + @param blocks The number of complete blocks to process + @param IV The initial value (input/output) + @param tweak The LRW tweak + @param skey The scheduled key context + @return CRYPT_OK if successful + */ + int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey); + + /** Accelerated CCM packet (one-shot) + @param key The secret key to use + @param keylen The length of the secret key (octets) + @param uskey A previously scheduled key [optional can be NULL] + @param nonce The session nonce [use once] + @param noncelen The length of the nonce + @param header The header for the session + @param headerlen The length of the header (octets) + @param pt [out] The plaintext + @param ptlen The length of the plaintext (octets) + @param ct [out] The ciphertext + @param tag [out] The destination tag + @param taglen [in/out] The max size and resulting size of the authentication tag + @param direction Encrypt or Decrypt direction (0 or 1) + @return CRYPT_OK if successful + */ + int (*accel_ccm_memory)( + const unsigned char *key, unsigned long keylen, + symmetric_key *uskey, + const unsigned char *nonce, unsigned long noncelen, + const unsigned char *header, unsigned long headerlen, + unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen, + int direction); + + /** Accelerated GCM packet (one shot) + @param key The secret key + @param keylen The length of the secret key + @param IV The initial vector + @param IVlen The length of the initial vector + @param adata The additional authentication data (header) + @param adatalen The length of the adata + @param pt The plaintext + @param ptlen The length of the plaintext (ciphertext length is the same) + @param ct The ciphertext + @param tag [out] The MAC tag + @param taglen [in/out] The MAC tag length + @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT) + @return CRYPT_OK on success + */ + int (*accel_gcm_memory)( + const unsigned char *key, unsigned long keylen, + const unsigned char *IV, unsigned long IVlen, + const unsigned char *adata, unsigned long adatalen, + unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen, + int direction); + + /** Accelerated one shot LTC_OMAC + @param key The secret key + @param keylen The key length (octets) + @param in The message + @param inlen Length of message (octets) + @param out [out] Destination for tag + @param outlen [in/out] Initial and final size of out + @return CRYPT_OK on success + */ + int (*omac_memory)( + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + + /** Accelerated one shot XCBC + @param key The secret key + @param keylen The key length (octets) + @param in The message + @param inlen Length of message (octets) + @param out [out] Destination for tag + @param outlen [in/out] Initial and final size of out + @return CRYPT_OK on success + */ + int (*xcbc_memory)( + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + + /** Accelerated one shot F9 + @param key The secret key + @param keylen The key length (octets) + @param in The message + @param inlen Length of message (octets) + @param out [out] Destination for tag + @param outlen [in/out] Initial and final size of out + @return CRYPT_OK on success + @remark Requires manual padding + */ + int (*f9_memory)( + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +} cipher_descriptor[]; + +#ifdef LTC_BLOWFISH +int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int blowfish_test(void); +void blowfish_done(symmetric_key *skey); +int blowfish_keysize(int *keysize); +extern const struct ltc_cipher_descriptor blowfish_desc; +#endif + +#ifdef LTC_RC5 +int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int rc5_test(void); +void rc5_done(symmetric_key *skey); +int rc5_keysize(int *keysize); +extern const struct ltc_cipher_descriptor rc5_desc; +#endif + +#ifdef LTC_RC6 +int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int rc6_test(void); +void rc6_done(symmetric_key *skey); +int rc6_keysize(int *keysize); +extern const struct ltc_cipher_descriptor rc6_desc; +#endif + +#ifdef LTC_RC2 +int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int rc2_test(void); +void rc2_done(symmetric_key *skey); +int rc2_keysize(int *keysize); +extern const struct ltc_cipher_descriptor rc2_desc; +#endif + +#ifdef LTC_SAFERP +int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int saferp_test(void); +void saferp_done(symmetric_key *skey); +int saferp_keysize(int *keysize); +extern const struct ltc_cipher_descriptor saferp_desc; +#endif + +#ifdef LTC_SAFER +int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); +int safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); +int safer_k64_test(void); +int safer_sk64_test(void); +int safer_sk128_test(void); +void safer_done(symmetric_key *skey); +int safer_64_keysize(int *keysize); +int safer_128_keysize(int *keysize); +extern const struct ltc_cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc; +#endif + +#ifdef LTC_RIJNDAEL + +/* make aes an alias */ +#define aes_setup rijndael_setup +#define aes_ecb_encrypt rijndael_ecb_encrypt +#define aes_ecb_decrypt rijndael_ecb_decrypt +#define aes_test rijndael_test +#define aes_done rijndael_done +#define aes_keysize rijndael_keysize + +#define aes_enc_setup rijndael_enc_setup +#define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt +#define aes_enc_keysize rijndael_enc_keysize + +int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int rijndael_test(void); +void rijndael_done(symmetric_key *skey); +int rijndael_keysize(int *keysize); +int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +void rijndael_enc_done(symmetric_key *skey); +int rijndael_enc_keysize(int *keysize); +extern const struct ltc_cipher_descriptor rijndael_desc, aes_desc; +extern const struct ltc_cipher_descriptor rijndael_enc_desc, aes_enc_desc; +#endif + +#ifdef LTC_XTEA +int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int xtea_test(void); +void xtea_done(symmetric_key *skey); +int xtea_keysize(int *keysize); +extern const struct ltc_cipher_descriptor xtea_desc; +#endif + +#ifdef LTC_TWOFISH +int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int twofish_test(void); +void twofish_done(symmetric_key *skey); +int twofish_keysize(int *keysize); +extern const struct ltc_cipher_descriptor twofish_desc; +#endif + +#ifdef LTC_DES +int des_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int des_test(void); +void des_done(symmetric_key *skey); +int des_keysize(int *keysize); +int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int des3_test(void); +void des3_done(symmetric_key *skey); +int des3_keysize(int *keysize); +extern const struct ltc_cipher_descriptor des_desc, des3_desc; +#endif + +#ifdef LTC_CAST5 +int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int cast5_test(void); +void cast5_done(symmetric_key *skey); +int cast5_keysize(int *keysize); +extern const struct ltc_cipher_descriptor cast5_desc; +#endif + +#ifdef LTC_NOEKEON +int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int noekeon_test(void); +void noekeon_done(symmetric_key *skey); +int noekeon_keysize(int *keysize); +extern const struct ltc_cipher_descriptor noekeon_desc; +#endif + +#ifdef LTC_SKIPJACK +int skipjack_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int skipjack_test(void); +void skipjack_done(symmetric_key *skey); +int skipjack_keysize(int *keysize); +extern const struct ltc_cipher_descriptor skipjack_desc; +#endif + +#ifdef LTC_KHAZAD +int khazad_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int khazad_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int khazad_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int khazad_test(void); +void khazad_done(symmetric_key *skey); +int khazad_keysize(int *keysize); +extern const struct ltc_cipher_descriptor khazad_desc; +#endif + +#ifdef LTC_ANUBIS +int anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int anubis_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int anubis_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int anubis_test(void); +void anubis_done(symmetric_key *skey); +int anubis_keysize(int *keysize); +extern const struct ltc_cipher_descriptor anubis_desc; +#endif + +#ifdef LTC_KSEED +int kseed_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int kseed_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int kseed_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int kseed_test(void); +void kseed_done(symmetric_key *skey); +int kseed_keysize(int *keysize); +extern const struct ltc_cipher_descriptor kseed_desc; +#endif + +#ifdef LTC_KASUMI +int kasumi_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int kasumi_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int kasumi_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int kasumi_test(void); +void kasumi_done(symmetric_key *skey); +int kasumi_keysize(int *keysize); +extern const struct ltc_cipher_descriptor kasumi_desc; +#endif + + +#ifdef LTC_MULTI2 +int multi2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); +int multi2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); +int multi2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); +int multi2_test(void); +void multi2_done(symmetric_key *skey); +int multi2_keysize(int *keysize); +extern const struct ltc_cipher_descriptor multi2_desc; +#endif + +#ifdef LTC_ECB_MODE +int ecb_start(int cipher, const unsigned char *key, + int keylen, int num_rounds, symmetric_ECB *ecb); +int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb); +int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb); +int ecb_done(symmetric_ECB *ecb); +#endif + +#ifdef LTC_CFB_MODE +int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key, + int keylen, int num_rounds, symmetric_CFB *cfb); +int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb); +int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb); +int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb); +int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb); +int cfb_done(symmetric_CFB *cfb); +#endif + +#ifdef LTC_OFB_MODE +int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, + int keylen, int num_rounds, symmetric_OFB *ofb); +int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb); +int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb); +int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb); +int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb); +int ofb_done(symmetric_OFB *ofb); +#endif + +#ifdef LTC_CBC_MODE +int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key, + int keylen, int num_rounds, symmetric_CBC *cbc); +int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc); +int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc); +int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc); +int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc); +int cbc_done(symmetric_CBC *cbc); +#endif + +#ifdef LTC_CTR_MODE + +#define CTR_COUNTER_LITTLE_ENDIAN 0x0000 +#define CTR_COUNTER_BIG_ENDIAN 0x1000 +#define LTC_CTR_RFC3686 0x2000 + +int ctr_start( int cipher, + const unsigned char *IV, + const unsigned char *key, int keylen, + int num_rounds, int ctr_mode, + symmetric_CTR *ctr); +int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr); +int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr); +int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr); +int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr); +int ctr_done(symmetric_CTR *ctr); +int ctr_test(void); +#endif + +#ifdef LTC_LRW_MODE + +#define LRW_ENCRYPT 0 +#define LRW_DECRYPT 1 + +int lrw_start( int cipher, + const unsigned char *IV, + const unsigned char *key, int keylen, + const unsigned char *tweak, + int num_rounds, + symmetric_LRW *lrw); +int lrw_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_LRW *lrw); +int lrw_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_LRW *lrw); +int lrw_getiv(unsigned char *IV, unsigned long *len, symmetric_LRW *lrw); +int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw); +int lrw_done(symmetric_LRW *lrw); +int lrw_test(void); + +/* don't call */ +int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw); +#endif + +#ifdef LTC_F8_MODE +int f8_start( int cipher, const unsigned char *IV, + const unsigned char *key, int keylen, + const unsigned char *salt_key, int skeylen, + int num_rounds, symmetric_F8 *f8); +int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_F8 *f8); +int f8_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_F8 *f8); +int f8_getiv(unsigned char *IV, unsigned long *len, symmetric_F8 *f8); +int f8_setiv(const unsigned char *IV, unsigned long len, symmetric_F8 *f8); +int f8_done(symmetric_F8 *f8); +int f8_test_mode(void); +#endif + +#ifdef LTC_XTS_MODE +typedef struct { + symmetric_key key1, key2; + int cipher; +} symmetric_xts; + +int xts_start( int cipher, + const unsigned char *key1, + const unsigned char *key2, + unsigned long keylen, + int num_rounds, + symmetric_xts *xts); + +int xts_encrypt( + const unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + const unsigned char *tweak, + symmetric_xts *xts); +int xts_decrypt( + const unsigned char *ct, unsigned long ptlen, + unsigned char *pt, + const unsigned char *tweak, + symmetric_xts *xts); + +void xts_done(symmetric_xts *xts); +int xts_test(void); +void xts_mult_x(unsigned char *I); +#endif + +int find_cipher(const char *name); +int find_cipher_any(const char *name, int blocklen, int keylen); +int find_cipher_id(unsigned char ID); +int register_cipher(const struct ltc_cipher_descriptor *cipher); +int unregister_cipher(const struct ltc_cipher_descriptor *cipher); +int cipher_is_valid(int idx); + +LTC_MUTEX_PROTO(ltc_cipher_mutex) + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_cipher.h,v $ */ +/* $Revision: 1.54 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/tomcrypt_custom.h b/crypto777/hmac/tomcrypt_custom.h new file mode 100755 index 000000000..7a8644cd2 --- /dev/null +++ b/crypto777/hmac/tomcrypt_custom.h @@ -0,0 +1,407 @@ +#ifndef TOMCRYPT_CUSTOM_H_ +#define TOMCRYPT_CUSTOM_H_ + + +/* macros for various libc functions you can change for embedded targets */ +#ifndef XMALLOC + #ifdef malloc + #define LTC_NO_PROTOTYPES + #endif +#define XMALLOC malloc +#endif +#ifndef XREALLOC + #ifdef realloc + #define LTC_NO_PROTOTYPES + #endif +#define XREALLOC realloc +#endif +#ifndef XCALLOC + #ifdef calloc + #define LTC_NO_PROTOTYPES + #endif +#define XCALLOC calloc +#endif +#ifndef XFREE + #ifdef free + #define LTC_NO_PROTOTYPES + #endif +#define XFREE free +#endif + +#ifndef XMEMSET + #ifdef memset + #define LTC_NO_PROTOTYPES + #endif +#define XMEMSET memset +#endif +#ifndef XMEMCPY + #ifdef memcpy + #define LTC_NO_PROTOTYPES + #endif +#define XMEMCPY memcpy +#endif +#ifndef XMEMCMP + #ifdef memcmp + #define LTC_NO_PROTOTYPES + #endif +#define XMEMCMP memcmp +#endif +#ifndef XSTRCMP + #ifdef strcmp + #define LTC_NO_PROTOTYPES + #endif +#define XSTRCMP strcmp +#endif + +#ifndef XCLOCK +#define XCLOCK clock +#endif +#ifndef XCLOCKS_PER_SEC +#define XCLOCKS_PER_SEC CLOCKS_PER_SEC +#endif + +#ifndef XQSORT + #ifdef qsort + #define LTC_NO_PROTOTYPES + #endif +#define XQSORT qsort +#endif + +/* Easy button? */ +#ifdef LTC_EASY + #define LTC_NO_CIPHERS + #define LTC_RIJNDAEL + #define LTC_BLOWFISH + #define LTC_DES + #define LTC_CAST5 + + #define LTC_NO_MODES + #define LTC_ECB_MODE + #define LTC_CBC_MODE + #define LTC_CTR_MODE + + #define LTC_NO_HASHES + #define LTC_SHA1 + #define LTC_SHA512 + #define LTC_SHA384 + #define LTC_SHA256 + #define LTC_SHA224 + + #define LTC_NO_MACS + #define LTC_HMAC + #define LTC_OMAC + #define LTC_CCM_MODE + + #define LTC_NO_PRNGS + #define LTC_SPRNG + #define LTC_YARROW + #define LTC_DEVRANDOM + #define TRY_URANDOM_FIRST + + #define LTC_NO_PK + #define LTC_MRSA + #define LTC_MECC +#endif + +/* Use small code where possible */ +/* #define LTC_SMALL_CODE */ + +/* Enable self-test test vector checking */ +#ifndef LTC_NO_TEST + #define LTC_TEST +#endif + +/* clean the stack of functions which put private information on stack */ +/* #define LTC_CLEAN_STACK */ + +/* disable all file related functions */ +/* #define LTC_NO_FILE */ + +/* disable all forms of ASM */ +/* #define LTC_NO_ASM */ + +/* disable FAST mode */ +/* #define LTC_NO_FAST */ + +/* disable BSWAP on x86 */ +/* #define LTC_NO_BSWAP */ + +/* ---> Symmetric Block Ciphers <--- */ +#ifndef LTC_NO_CIPHERS + +#define LTC_BLOWFISH +#define LTC_RC2 +#define LTC_RC5 +#define LTC_RC6 +#define LTC_SAFERP +#define LTC_RIJNDAEL +#define LTC_XTEA +/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format + * (saves 4KB of ram), _ALL_TABLES enables all tables during setup */ +#define LTC_TWOFISH +#ifndef LTC_NO_TABLES + #define LTC_TWOFISH_TABLES + /* #define LTC_TWOFISH_ALL_TABLES */ +#else + #define LTC_TWOFISH_SMALL +#endif +/* #define LTC_TWOFISH_SMALL */ +/* LTC_DES includes EDE triple-LTC_DES */ +#define LTC_DES +#define LTC_CAST5 +#define LTC_NOEKEON +#define LTC_SKIPJACK +#define LTC_SAFER +#define LTC_KHAZAD +#define LTC_ANUBIS +#define LTC_ANUBIS_TWEAK +#define LTC_KSEED +#define LTC_KASUMI + +#endif /* LTC_NO_CIPHERS */ + + +/* ---> Block Cipher Modes of Operation <--- */ +#ifndef LTC_NO_MODES + +#define LTC_CFB_MODE +#define LTC_OFB_MODE +#define LTC_ECB_MODE +#define LTC_CBC_MODE +#define LTC_CTR_MODE + +/* F8 chaining mode */ +#define LTC_F8_MODE + +/* LRW mode */ +#define LTC_LRW_MODE +#ifndef LTC_NO_TABLES + /* like GCM mode this will enable 16 8x128 tables [64KB] that make + * seeking very fast. + */ + #define LRW_TABLES +#endif + +/* XTS mode */ +#define LTC_XTS_MODE + +#endif /* LTC_NO_MODES */ + +/* ---> One-Way Hash Functions <--- */ +#ifndef LTC_NO_HASHES + +#define LTC_CHC_HASH +#define LTC_WHIRLPOOL +#define LTC_SHA512 +#define LTC_SHA384 +#define LTC_SHA256 +#define LTC_SHA224 +#define LTC_TIGER +#define LTC_SHA1 +#define LTC_MD5 +#define LTC_MD4 +#define LTC_MD2 +#define LTC_RIPEMD128 +#define LTC_RIPEMD160 +#define LTC_RIPEMD256 +#define LTC_RIPEMD320 + +#endif /* LTC_NO_HASHES */ + +/* ---> MAC functions <--- */ +#ifndef LTC_NO_MACS + +#define LTC_HMAC +#define LTC_OMAC +#define LTC_PMAC +#define LTC_XCBC +#define LTC_F9_MODE +#define LTC_PELICAN + +#if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL) + #error Pelican-MAC requires LTC_RIJNDAEL +#endif + +/* ---> Encrypt + Authenticate Modes <--- */ + +#define LTC_EAX_MODE +#if defined(LTC_EAX_MODE) && !(defined(LTC_CTR_MODE) && defined(LTC_OMAC)) + #error LTC_EAX_MODE requires CTR and LTC_OMAC mode +#endif + +#define LTC_OCB_MODE +#define LTC_CCM_MODE +#define LTC_GCM_MODE + +/* Use 64KiB tables */ +#ifndef LTC_NO_TABLES + #define LTC_GCM_TABLES +#endif + +/* USE SSE2? requires GCC works on x86_32 and x86_64*/ +#ifdef LTC_GCM_TABLES +/* #define LTC_GCM_TABLES_SSE2 */ +#endif + +#endif /* LTC_NO_MACS */ + +/* Various tidbits of modern neatoness */ +#define LTC_BASE64 + +/* --> Pseudo Random Number Generators <--- */ +#ifndef LTC_NO_PRNGS + +/* Yarrow */ +#define LTC_YARROW +/* which descriptor of AES to use? */ +/* 0 = rijndael_enc 1 = aes_enc, 2 = rijndael [full], 3 = aes [full] */ +#define LTC_YARROW_AES 0 + +#if defined(LTC_YARROW) && !defined(LTC_CTR_MODE) + #error LTC_YARROW requires LTC_CTR_MODE chaining mode to be defined! +#endif + +/* a PRNG that simply reads from an available system source */ +#define LTC_SPRNG + +/* The LTC_RC4 stream cipher */ +#define LTC_RC4 + +/* Fortuna PRNG */ +#define LTC_FORTUNA +/* reseed every N calls to the read function */ +#define LTC_FORTUNA_WD 10 +/* number of pools (4..32) can save a bit of ram by lowering the count */ +#define LTC_FORTUNA_POOLS 32 + +/* Greg's LTC_SOBER128 PRNG ;-0 */ +#define LTC_SOBER128 + +/* the *nix style /dev/random device */ +#define LTC_DEVRANDOM +/* try /dev/urandom before trying /dev/random */ +#define TRY_URANDOM_FIRST + +#endif /* LTC_NO_PRNGS */ + +/* ---> math provider? <--- */ +#ifndef LTC_NO_MATH + +/* LibTomMath */ +/* #define LTM_LTC_DESC */ + +/* TomsFastMath */ +/* #define TFM_LTC_DESC */ + +#endif /* LTC_NO_MATH */ + +/* ---> Public Key Crypto <--- */ +#ifndef LTC_NO_PK + +/* Include RSA support */ +#define LTC_MRSA + +/* Include Katja (a Rabin variant like RSA) */ +/* #define MKAT */ + +/* Digital Signature Algorithm */ +#define LTC_MDSA + +/* ECC */ +#define LTC_MECC + +/* use Shamir's trick for point mul (speeds up signature verification) */ +#define LTC_ECC_SHAMIR + +#if defined(TFM_LTC_DESC) && defined(LTC_MECC) + #define LTC_MECC_ACCEL +#endif + +/* do we want fixed point ECC */ +/* #define LTC_MECC_FP */ + +/* Timing Resistant? */ +/* #define LTC_ECC_TIMING_RESISTANT */ + +#endif /* LTC_NO_PK */ + +/* LTC_PKCS #1 (RSA) and #5 (Password Handling) stuff */ +#ifndef LTC_NO_PKCS + +#define LTC_PKCS_1 +#define LTC_PKCS_5 + +/* Include ASN.1 DER (required by DSA/RSA) */ +#define LTC_DER + +#endif /* LTC_NO_PKCS */ + +/* cleanup */ + +#ifdef LTC_MECC +/* Supported ECC Key Sizes */ +#ifndef LTC_NO_CURVES + #define ECC112 + #define ECC128 + #define ECC160 + #define ECC192 + #define ECC224 + #define ECC256 + #define ECC384 + #define ECC521 +#endif +#endif + +#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(MKATJA) + /* Include the MPI functionality? (required by the PK algorithms) */ + #define MPI +#endif + +#ifdef LTC_MRSA + #define LTC_PKCS_1 +#endif + +#if defined(LTC_DER) && !defined(MPI) + #error ASN.1 DER requires MPI functionality +#endif + +#if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(MKATJA)) && !defined(LTC_DER) + #error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled +#endif + +/* THREAD management */ +#ifdef LTC_PTHREAD + +//#include + +#define LTC_MUTEX_GLOBAL(x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER; +#define LTC_MUTEX_PROTO(x) extern pthread_mutex_t x; +#define LTC_MUTEX_TYPE(x) pthread_mutex_t x; +#define LTC_MUTEX_INIT(x) pthread_mutex_init(x, NULL); +#define LTC_MUTEX_LOCK(x) pthread_mutex_lock(x); +#define LTC_MUTEX_UNLOCK(x) pthread_mutex_unlock(x); + +#else + +/* default no functions */ +#define LTC_MUTEX_GLOBAL(x) +#define LTC_MUTEX_PROTO(x) +#define LTC_MUTEX_TYPE(x) +#define LTC_MUTEX_INIT(x) +#define LTC_MUTEX_LOCK(x) +#define LTC_MUTEX_UNLOCK(x) + +#endif + +/* Debuggers */ + +/* define this if you use Valgrind, note: it CHANGES the way SOBER-128 and LTC_RC4 work (see the code) */ +/* #define LTC_VALGRIND */ + +#endif + + + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_custom.h,v $ */ +/* $Revision: 1.73 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/tomcrypt_hash.h b/crypto777/hmac/tomcrypt_hash.h new file mode 100755 index 000000000..b596c6a37 --- /dev/null +++ b/crypto777/hmac/tomcrypt_hash.h @@ -0,0 +1,379 @@ +/* ---- HASH FUNCTIONS ---- */ +#ifdef LTC_SHA512 +struct sha512_state { + ulong64 length, state[8]; + unsigned long curlen; + unsigned char buf[128]; +}; +#endif + +#ifdef LTC_SHA256 + +#endif + +#ifdef LTC_SHA1 +struct sha1_state { + ulong64 length; + ulong32 state[5], curlen; + unsigned char buf[64]; +}; +#endif + +#ifdef LTC_MD5 +struct md5_state { + ulong64 length; + ulong32 state[4], curlen; + unsigned char buf[64]; +}; +#endif + +#ifdef LTC_MD4 +struct md4_state { + ulong64 length; + ulong32 state[4], curlen; + unsigned char buf[64]; +}; +#endif + +#ifdef LTC_TIGER +struct tiger_state { + ulong64 state[3], length; + unsigned long curlen; + unsigned char buf[64]; +}; +#endif + +#ifdef LTC_MD2 +struct md2_state { + unsigned char chksum[16], X[48], buf[16]; + unsigned long curlen; +}; +#endif + +#ifdef LTC_RIPEMD128 +struct rmd128_state { + ulong64 length; + unsigned char buf[64]; + ulong32 curlen, state[4]; +}; +#endif + +#ifdef LTC_RIPEMD160 +struct rmd160_state { + ulong64 length; + unsigned char buf[64]; + ulong32 curlen, state[5]; +}; +#endif + +#ifdef LTC_RIPEMD256 +struct rmd256_state { + ulong64 length; + unsigned char buf[64]; + ulong32 curlen, state[8]; +}; +#endif + +#ifdef LTC_RIPEMD320 +struct rmd320_state { + ulong64 length; + unsigned char buf[64]; + ulong32 curlen, state[10]; +}; +#endif + +#ifdef LTC_WHIRLPOOL +struct whirlpool_state { + ulong64 length, state[8]; + unsigned char buf[64]; + ulong32 curlen; +}; +#endif + +#ifdef LTC_CHC_HASH +struct chc_state { + ulong64 length; + unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE]; + ulong32 curlen; +}; +#endif + +typedef union Hash_state { + char dummy[1]; +#ifdef LTC_CHC_HASH + struct chc_state chc; +#endif +#ifdef LTC_WHIRLPOOL + struct whirlpool_state whirlpool; +#endif +#ifdef LTC_SHA512 + struct sha512_state sha512; +#endif +#ifdef LTC_SHA256 +//#include "../sha256.h" +struct sha256_state { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; } sha256; + + + //struct sha256_state sha256; +#endif +#ifdef LTC_SHA1 + struct sha1_state sha1; +#endif +#ifdef LTC_MD5 + struct md5_state md5; +#endif +#ifdef LTC_MD4 + struct md4_state md4; +#endif +#ifdef LTC_MD2 + struct md2_state md2; +#endif +#ifdef LTC_TIGER + struct tiger_state tiger; +#endif +#ifdef LTC_RIPEMD128 + struct rmd128_state rmd128; +#endif +#ifdef LTC_RIPEMD160 + struct rmd160_state rmd160; +#endif +#ifdef LTC_RIPEMD256 + struct rmd256_state rmd256; +#endif +#ifdef LTC_RIPEMD320 + struct rmd320_state rmd320; +#endif + void *data; +} hash_state; + +/** hash descriptor */ +extern struct ltc_hash_descriptor { + /** name of hash */ + char *name; + /** internal ID */ + unsigned char ID; + /** Size of digest in octets */ + unsigned long hashsize; + /** Input block size in octets */ + unsigned long blocksize; + /** ASN.1 OID */ + unsigned long OID[16]; + /** Length of DER encoding */ + unsigned long OIDlen; + + /** Init a hash state + @param hash The hash to initialize + @return CRYPT_OK if successful + */ + int (*init)(hash_state *hash); + /** Process a block of data + @param hash The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful + */ + int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen); + /** Produce the digest and store it + @param hash The hash state + @param out [out] The destination of the digest + @return CRYPT_OK if successful + */ + int (*done)(hash_state *hash, unsigned char *out); + /** Self-test + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled + */ + int (*test)(void); + + /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */ + int (*hmac_block)(const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + +} hash_descriptor[]; + +#ifdef LTC_CHC_HASH +int chc_register(int cipher); +int chc_init(hash_state * md); +int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int chc_done(hash_state * md, unsigned char *hash); +int chc_test(void); +extern const struct ltc_hash_descriptor chc_desc; +#endif + +#ifdef LTC_WHIRLPOOL +int whirlpool_init(hash_state * md); +int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int whirlpool_done(hash_state * md, unsigned char *hash); +int whirlpool_test(void); +extern const struct ltc_hash_descriptor whirlpool_desc; +#endif + +#ifdef LTC_SHA512 +int sha512_init(hash_state * md); +int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int sha512_done(hash_state * md, unsigned char *hash); +int sha512_test(void); +extern const struct ltc_hash_descriptor sha512_desc; +#endif + +#ifdef LTC_SHA384 +#ifndef LTC_SHA512 + #error LTC_SHA512 is required for LTC_SHA384 +#endif +int sha384_init(hash_state * md); +#define sha384_process sha512_process +int sha384_done(hash_state * md, unsigned char *hash); +int sha384_test(void); +extern const struct ltc_hash_descriptor sha384_desc; +#endif + +#ifdef LTC_SHA256 +int sha256_init(hash_state * md); +int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int sha256_done(hash_state * md, unsigned char *hash); +int sha256_test(void); +extern const struct ltc_hash_descriptor sha256_desc; + +#ifdef LTC_SHA224 +#ifndef LTC_SHA256 + #error LTC_SHA256 is required for LTC_SHA224 +#endif +int sha224_init(hash_state * md); +#define sha224_process sha256_process +int sha224_done(hash_state * md, unsigned char *hash); +int sha224_test(void); +extern const struct ltc_hash_descriptor sha224_desc; +#endif +#endif + +#ifdef LTC_SHA1 +int sha1_init(hash_state * md); +int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int sha1_done(hash_state * md, unsigned char *hash); +int sha1_test(void); +extern const struct ltc_hash_descriptor sha1_desc; +#endif + +#ifdef LTC_MD5 +int md5_init(hash_state * md); +int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int md5_done(hash_state * md, unsigned char *hash); +int md5_test(void); +extern const struct ltc_hash_descriptor md5_desc; +#endif + +#ifdef LTC_MD4 +int md4_init(hash_state * md); +int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int md4_done(hash_state * md, unsigned char *hash); +int md4_test(void); +extern const struct ltc_hash_descriptor md4_desc; +#endif + +#ifdef LTC_MD2 +int md2_init(hash_state * md); +int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int md2_done(hash_state * md, unsigned char *hash); +int md2_test(void); +extern const struct ltc_hash_descriptor md2_desc; +#endif + +#ifdef LTC_TIGER +int tiger_init(hash_state * md); +int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int tiger_done(hash_state * md, unsigned char *hash); +int tiger_test(void); +extern const struct ltc_hash_descriptor tiger_desc; +#endif + +#ifdef LTC_RIPEMD128 +int rmd128_init(hash_state * md); +int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int rmd128_done(hash_state * md, unsigned char *hash); +int rmd128_test(void); +extern const struct ltc_hash_descriptor rmd128_desc; +#endif + +#ifdef LTC_RIPEMD160 +int rmd160_init(hash_state * md); +int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int rmd160_done(hash_state * md, unsigned char *hash); +int rmd160_test(void); +extern const struct ltc_hash_descriptor rmd160_desc; +#endif + +#ifdef LTC_RIPEMD256 +int rmd256_init(hash_state * md); +int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int rmd256_done(hash_state * md, unsigned char *hash); +int rmd256_test(void); +extern const struct ltc_hash_descriptor rmd256_desc; +#endif + +#ifdef LTC_RIPEMD320 +int rmd320_init(hash_state * md); +int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen); +int rmd320_done(hash_state * md, unsigned char *hash); +int rmd320_test(void); +extern const struct ltc_hash_descriptor rmd320_desc; +#endif + + +int find_hash(const char *name); +int find_hash_id(unsigned char ID); +int find_hash_oid(const unsigned long *ID, unsigned long IDlen); +int find_hash_any(const char *name, int digestlen); +int register_hash(const struct ltc_hash_descriptor *hash); +int unregister_hash(const struct ltc_hash_descriptor *hash); +int hash_is_valid(int idx); + +LTC_MUTEX_PROTO(ltc_hash_mutex) + +int hash_memory(const struct ltc_hash_descriptor *hash, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int hash_memory_multi(struct ltc_hash_descriptor *hash, unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); +int hash_filehandle(struct ltc_hash_descriptor *hash, FILE *in, unsigned char *out, unsigned long *outlen); +int hash_file(struct ltc_hash_descriptor *hash, const char *fname, unsigned char *out, unsigned long *outlen); + +/* a simple macro for making hash "process" functions */ +#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ +int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \ +{ \ + unsigned long n; \ + int err; \ + LTC_ARGCHK(md != NULL); \ + LTC_ARGCHK(in != NULL); \ + if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \ + return CRYPT_INVALID_ARG; \ + } \ + while (inlen > 0) { \ + if (md-> state_var .curlen == 0 && inlen >= block_size) { \ + if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \ + return err; \ + } \ + md-> state_var .length += block_size * 8; \ + in += block_size; \ + inlen -= block_size; \ + } else { \ + n = MIN(inlen, (block_size - md-> state_var .curlen)); \ + memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \ + md-> state_var .curlen += n; \ + in += n; \ + inlen -= n; \ + if (md-> state_var .curlen == block_size) { \ + if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \ + return err; \ + } \ + md-> state_var .length += 8*block_size; \ + md-> state_var .curlen = 0; \ + } \ + } \ + } \ + return CRYPT_OK; \ +} + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_hash.h,v $ */ +/* $Revision: 1.22 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ diff --git a/crypto777/hmac/tomcrypt_mac.h b/crypto777/hmac/tomcrypt_mac.h new file mode 100755 index 000000000..6be8324d8 --- /dev/null +++ b/crypto777/hmac/tomcrypt_mac.h @@ -0,0 +1,382 @@ +#ifdef LTC_HMAC +typedef struct Hmac_state { + hash_state md; + struct ltc_hash_descriptor *hash; + hash_state hashstate; + unsigned char *key; +} hmac_state; + +int hmac_init(hmac_state *hmac, const struct ltc_hash_descriptor *hash, const unsigned char *key, unsigned long keylen); +int hmac_process(hmac_state *hmac, const struct ltc_hash_descriptor *hash,const unsigned char *in, unsigned long inlen); +int hmac_done(hmac_state *hmac,const struct ltc_hash_descriptor *hash, unsigned char *out, unsigned long *outlen); +int hmac_test(void); +int hmac_memory(const struct ltc_hash_descriptor *hash, + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int hmac_memory_multi(struct ltc_hash_descriptor *hash, + const unsigned char *key, unsigned long keylen, + unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); +int hmac_file(const struct ltc_hash_descriptor *hash, const char *fname, const unsigned char *key, unsigned long keylen,unsigned char *out, unsigned long *outlen); +#endif + +#ifdef LTC_OMAC + +typedef struct { + int cipher_idx, + buflen, + blklen; + unsigned char block[MAXBLOCKSIZE], + prev[MAXBLOCKSIZE], + Lu[2][MAXBLOCKSIZE]; + symmetric_key key; +} omac_state; + +int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen); +int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen); +int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen); +int omac_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int omac_memory_multi(int cipher, + const unsigned char *key, unsigned long keylen, + unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); +int omac_file(int cipher, + const unsigned char *key, unsigned long keylen, + const char *filename, + unsigned char *out, unsigned long *outlen); +int omac_test(void); +#endif /* LTC_OMAC */ + +#ifdef LTC_PMAC + +typedef struct { + unsigned char Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */ + Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */ + Lr[MAXBLOCKSIZE], /* L * x^-1 */ + block[MAXBLOCKSIZE], /* currently accumulated block */ + checksum[MAXBLOCKSIZE]; /* current checksum */ + + symmetric_key key; /* scheduled key for cipher */ + unsigned long block_index; /* index # for current block */ + int cipher_idx, /* cipher idx */ + block_len, /* length of block */ + buflen; /* number of bytes in the buffer */ +} pmac_state; + +int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen); +int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen); +int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen); + +int pmac_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *msg, unsigned long msglen, + unsigned char *out, unsigned long *outlen); + +int pmac_memory_multi(int cipher, + const unsigned char *key, unsigned long keylen, + unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); + +int pmac_file(int cipher, + const unsigned char *key, unsigned long keylen, + const char *filename, + unsigned char *out, unsigned long *outlen); + +int pmac_test(void); + +/* internal functions */ +int pmac_ntz(unsigned long x); +void pmac_shift_xor(pmac_state *pmac); + +#endif /* PMAC */ + +#ifdef LTC_EAX_MODE + +#if !(defined(LTC_OMAC) && defined(LTC_CTR_MODE)) + #error LTC_EAX_MODE requires LTC_OMAC and CTR +#endif + +typedef struct { + unsigned char N[MAXBLOCKSIZE]; + symmetric_CTR ctr; + omac_state headeromac, ctomac; +} eax_state; + +int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen, + const unsigned char *nonce, unsigned long noncelen, + const unsigned char *header, unsigned long headerlen); + +int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length); +int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length); +int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length); +int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen); + +int eax_encrypt_authenticate_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *nonce, unsigned long noncelen, + const unsigned char *header, unsigned long headerlen, + const unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen); + +int eax_decrypt_verify_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *nonce, unsigned long noncelen, + const unsigned char *header, unsigned long headerlen, + const unsigned char *ct, unsigned long ctlen, + unsigned char *pt, + unsigned char *tag, unsigned long taglen, + int *stat); + + int eax_test(void); +#endif /* EAX MODE */ + +#ifdef LTC_OCB_MODE +typedef struct { + unsigned char L[MAXBLOCKSIZE], /* L value */ + Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */ + Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */ + Lr[MAXBLOCKSIZE], /* L * x^-1 */ + R[MAXBLOCKSIZE], /* R value */ + checksum[MAXBLOCKSIZE]; /* current checksum */ + + symmetric_key key; /* scheduled key for cipher */ + unsigned long block_index; /* index # for current block */ + int cipher, /* cipher idx */ + block_len; /* length of block */ +} ocb_state; + +int ocb_init(ocb_state *ocb, int cipher, + const unsigned char *key, unsigned long keylen, const unsigned char *nonce); + +int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct); +int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt); + +int ocb_done_encrypt(ocb_state *ocb, + const unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen); + +int ocb_done_decrypt(ocb_state *ocb, + const unsigned char *ct, unsigned long ctlen, + unsigned char *pt, + const unsigned char *tag, unsigned long taglen, int *stat); + +int ocb_encrypt_authenticate_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *nonce, + const unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen); + +int ocb_decrypt_verify_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *nonce, + const unsigned char *ct, unsigned long ctlen, + unsigned char *pt, + const unsigned char *tag, unsigned long taglen, + int *stat); + +int ocb_test(void); + +/* internal functions */ +void ocb_shift_xor(ocb_state *ocb, unsigned char *Z); +int ocb_ntz(unsigned long x); +int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, + unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode); + +#endif /* LTC_OCB_MODE */ + +#ifdef LTC_CCM_MODE + +#define CCM_ENCRYPT 0 +#define CCM_DECRYPT 1 + +int ccm_memory(int cipher, + const unsigned char *key, unsigned long keylen, + symmetric_key *uskey, + const unsigned char *nonce, unsigned long noncelen, + const unsigned char *header, unsigned long headerlen, + unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen, + int direction); + +int ccm_test(void); + +#endif /* LTC_CCM_MODE */ + +#if defined(LRW_MODE) || defined(LTC_GCM_MODE) +void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c); +#endif + + +/* table shared between GCM and LRW */ +#if defined(LTC_GCM_TABLES) || defined(LRW_TABLES) || ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST)) +extern const unsigned char gcm_shift_table[]; +#endif + +#ifdef LTC_GCM_MODE + +#define GCM_ENCRYPT 0 +#define GCM_DECRYPT 1 + +#define LTC_GCM_MODE_IV 0 +#define LTC_GCM_MODE_AAD 1 +#define LTC_GCM_MODE_TEXT 2 + +typedef struct { + symmetric_key K; + unsigned char H[16], /* multiplier */ + X[16], /* accumulator */ + Y[16], /* counter */ + Y_0[16], /* initial counter */ + buf[16]; /* buffer for stuff */ + + int cipher, /* which cipher */ + ivmode, /* Which mode is the IV in? */ + mode, /* mode the GCM code is in */ + buflen; /* length of data in buf */ + + ulong64 totlen, /* 64-bit counter used for IV and AAD */ + pttotlen; /* 64-bit counter for the PT */ + +#ifdef LTC_GCM_TABLES + unsigned char PC[16][256][16] /* 16 tables of 8x128 */ +#ifdef LTC_GCM_TABLES_SSE2 +__attribute__ ((aligned (16))) +#endif +; +#endif +} gcm_state; + +void gcm_mult_h(gcm_state *gcm, unsigned char *I); + +int gcm_init(gcm_state *gcm, int cipher, + const unsigned char *key, int keylen); + +int gcm_reset(gcm_state *gcm); + +int gcm_add_iv(gcm_state *gcm, + const unsigned char *IV, unsigned long IVlen); + +int gcm_add_aad(gcm_state *gcm, + const unsigned char *adata, unsigned long adatalen); + +int gcm_process(gcm_state *gcm, + unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + int direction); + +int gcm_done(gcm_state *gcm, + unsigned char *tag, unsigned long *taglen); + +int gcm_memory( int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *IV, unsigned long IVlen, + const unsigned char *adata, unsigned long adatalen, + unsigned char *pt, unsigned long ptlen, + unsigned char *ct, + unsigned char *tag, unsigned long *taglen, + int direction); +int gcm_test(void); + +#endif /* LTC_GCM_MODE */ + +#ifdef LTC_PELICAN + +typedef struct pelican_state +{ + symmetric_key K; + unsigned char state[16]; + int buflen; +} pelican_state; + +int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen); +int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen); +int pelican_done(pelican_state *pelmac, unsigned char *out); +int pelican_test(void); + +int pelican_memory(const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out); + +#endif + +#ifdef LTC_XCBC + +/* add this to "keylen" to xcbc_init to use a pure three-key XCBC MAC */ +#define LTC_XCBC_PURE 0x8000UL + +typedef struct { + unsigned char K[3][MAXBLOCKSIZE], + IV[MAXBLOCKSIZE]; + + symmetric_key key; + + int cipher, + buflen, + blocksize; +} xcbc_state; + +int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen); +int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen); +int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen); +int xcbc_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int xcbc_memory_multi(int cipher, + const unsigned char *key, unsigned long keylen, + unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); +int xcbc_file(int cipher, + const unsigned char *key, unsigned long keylen, + const char *filename, + unsigned char *out, unsigned long *outlen); +int xcbc_test(void); + +#endif + +#ifdef LTC_F9_MODE + +typedef struct { + unsigned char akey[MAXBLOCKSIZE], + ACC[MAXBLOCKSIZE], + IV[MAXBLOCKSIZE]; + + symmetric_key key; + + int cipher, + buflen, + keylen, + blocksize; +} f9_state; + +int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long keylen); +int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen); +int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen); +int f9_memory(int cipher, + const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int f9_memory_multi(int cipher, + const unsigned char *key, unsigned long keylen, + unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); +int f9_file(int cipher, + const unsigned char *key, unsigned long keylen, + const char *filename, + unsigned char *out, unsigned long *outlen); +int f9_test(void); + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_mac.h,v $ */ +/* $Revision: 1.23 $ */ +/* $Date: 2007/05/12 14:37:41 $ */ diff --git a/crypto777/hmac/tomcrypt_macros.h b/crypto777/hmac/tomcrypt_macros.h new file mode 100755 index 000000000..f73f6e563 --- /dev/null +++ b/crypto777/hmac/tomcrypt_macros.h @@ -0,0 +1,426 @@ +/* fix for MSVC ...evil! */ +#ifdef _MSC_VER + #define CONST64(n) n ## ui64 + typedef unsigned __int64 ulong64; +#else + #define CONST64(n) n ## ULL +#define ulong64 uint64_t + //typedef unsigned long long ulong64; +#endif + +/* this is the "32-bit at least" data type + * Re-define it to suit your platform but it must be at least 32-bits + +#if defined(__x86_64__) || (defined(__sparc__) && defined(__arch64__)) + typedef unsigned ulong32; +#else +#define ulong32 uint32_t +//typedef unsigned long ulong32; +#endif*/ + +/* ---- HELPER MACROS ---- */ +#ifdef ENDIAN_NEUTRAL + +#define STORE32L(x, y) \ + { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#define LOAD32L(x, y) \ + { x = (ulong32)(((unsigned long)((y)[3] & 255)<<24) | \ + ((unsigned long)((y)[2] & 255)<<16) | \ + ((unsigned long)((y)[1] & 255)<<8) | \ + ((unsigned long)((y)[0] & 255))); } + +#define STORE64L(x, y) \ + { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ + (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ + (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#define LOAD64L(x, y) \ + { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ + (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ + (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ + (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } + +#define STORE32H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ + (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } + +#define LOAD32H(x, y) \ + { x = (ulong32)(((unsigned long)((y)[0] & 255)<<24) | \ + ((unsigned long)((y)[1] & 255)<<16) | \ + ((unsigned long)((y)[2] & 255)<<8) | \ + ((unsigned long)((y)[3] & 255))); } + +#define STORE64H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ + (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ + (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ + (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } + +#define LOAD64H(x, y) \ + { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ + (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ + (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ + (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } + +#endif /* ENDIAN_NEUTRAL */ + +#ifdef ENDIAN_LITTLE + +#if !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__)))) + +#define STORE32H(x, y) \ +asm __volatile__ ( \ + "bswapl %0 \n\t" \ + "movl %0,(%1)\n\t" \ + "bswapl %0 \n\t" \ + ::"r"(x), "r"(y)); + +#define LOAD32H(x, y) \ +asm __volatile__ ( \ + "movl (%1),%0\n\t" \ + "bswapl %0\n\t" \ + :"=r"(x): "r"(y)); + +#else + +#define STORE32H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ + (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } + +#define LOAD32H(x, y) \ + { x = ((unsigned long)((y)[0] & 255)<<24) | \ + ((unsigned long)((y)[1] & 255)<<16) | \ + ((unsigned long)((y)[2] & 255)<<8) | \ + ((unsigned long)((y)[3] & 255)); } + +#endif + + +/* x86_64 processor */ +#if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__)) + +#define STORE64H(x, y) \ +asm __volatile__ ( \ + "bswapq %0 \n\t" \ + "movq %0,(%1)\n\t" \ + "bswapq %0 \n\t" \ + ::"r"(x), "r"(y)); + +#define LOAD64H(x, y) \ +asm __volatile__ ( \ + "movq (%1),%0\n\t" \ + "bswapq %0\n\t" \ + :"=r"(x): "r"(y)); + +#else + +#define STORE64H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ + (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ + (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ + (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } + +#define LOAD64H(x, y) \ + { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ + (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ + (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ + (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } + +#endif + +#ifdef ENDIAN_32BITWORD + +#define STORE32L(x, y) \ + { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } + +#define LOAD32L(x, y) \ + XMEMCPY(&(x), y, 4); + +#define STORE64L(x, y) \ + { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ + (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ + (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#define LOAD64L(x, y) \ + { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ + (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ + (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ + (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } + +#else /* 64-bit words then */ + +#define STORE32L(x, y) \ + { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } + +#define LOAD32L(x, y) \ + { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } + +#define STORE64L(x, y) \ + { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } + +#define LOAD64L(x, y) \ + { XMEMCPY(&(x), y, 8); } + +#endif /* ENDIAN_64BITWORD */ + +#endif /* ENDIAN_LITTLE */ + +#ifdef ENDIAN_BIG +#define STORE32L(x, y) \ + { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#define LOAD32L(x, y) \ + { x = ((unsigned long)((y)[3] & 255)<<24) | \ + ((unsigned long)((y)[2] & 255)<<16) | \ + ((unsigned long)((y)[1] & 255)<<8) | \ + ((unsigned long)((y)[0] & 255)); } + +#define STORE64L(x, y) \ + { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ + (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ + (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#define LOAD64L(x, y) \ + { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \ + (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \ + (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \ + (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } + +#ifdef ENDIAN_32BITWORD + +#define STORE32H(x, y) \ + { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } + +#define LOAD32H(x, y) \ + XMEMCPY(&(x), y, 4); + +#define STORE64H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ + (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ + (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ + (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } + +#define LOAD64H(x, y) \ + { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \ + (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \ + (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \ + (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } + +#else /* 64-bit words then */ + +#define STORE32H(x, y) \ + { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } + +#define LOAD32H(x, y) \ + { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } + +#define STORE64H(x, y) \ + { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } + +#define LOAD64H(x, y) \ + { XMEMCPY(&(x), y, 8); } + +#endif /* ENDIAN_64BITWORD */ +#endif /* ENDIAN_BIG */ + +#define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \ + ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) ) + + +/* 32-bit Rotates */ +#if defined(_MSC_VER) + +/* instrinsic rotate */ +#include +#pragma intrinsic(_lrotr,_lrotl) +#define ROR(x,n) _lrotr(x,n) +#define ROL(x,n) _lrotl(x,n) +#define RORc(x,n) _lrotr(x,n) +#define ROLc(x,n) _lrotl(x,n) + +#elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM) + +static inline unsigned ROL(unsigned word, int i) +{ + asm ("roll %%cl,%0" + :"=r" (word) + :"0" (word),"c" (i)); + return word; +} + +static inline unsigned ROR(unsigned word, int i) +{ + asm ("rorl %%cl,%0" + :"=r" (word) + :"0" (word),"c" (i)); + return word; +} + +#ifndef LTC_NO_ROLC + +static inline unsigned ROLc(unsigned word, const int i) +{ + asm ("roll %2,%0" + :"=r" (word) + :"0" (word),"I" (i)); + return word; +} + +static inline unsigned RORc(unsigned word, const int i) +{ + asm ("rorl %2,%0" + :"=r" (word) + :"0" (word),"I" (i)); + return word; +} + +#else + +#define ROLc ROL +#define RORc ROR + +#endif + +#elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32) + +static inline unsigned ROL(unsigned word, int i) +{ + asm ("rotlw %0,%0,%2" + :"=r" (word) + :"0" (word),"r" (i)); + return word; +} + +static inline unsigned ROR(unsigned word, int i) +{ + asm ("rotlw %0,%0,%2" + :"=r" (word) + :"0" (word),"r" (32-i)); + return word; +} + +#ifndef LTC_NO_ROLC + +static inline unsigned ROLc(unsigned word, const int i) +{ + asm ("rotlwi %0,%0,%2" + :"=r" (word) + :"0" (word),"I" (i)); + return word; +} + +static inline unsigned RORc(unsigned word, const int i) +{ + asm ("rotrwi %0,%0,%2" + :"=r" (word) + :"0" (word),"I" (i)); + return word; +} + +#else + +#define ROLc ROL +#define RORc ROR + +#endif + + +#else + +/* rotates the hard way */ +#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) + +#endif + + +/* 64-bit Rotates */ +#if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(LTC_NO_ASM) + +static inline unsigned long ROL64(unsigned long word, int i) +{ + asm("rolq %%cl,%0" + :"=r" (word) + :"0" (word),"c" (i)); + return word; +} + +static inline unsigned long ROR64(unsigned long word, int i) +{ + asm("rorq %%cl,%0" + :"=r" (word) + :"0" (word),"c" (i)); + return word; +} + +#ifndef LTC_NO_ROLC + +static inline unsigned long ROL64c(unsigned long word, const int i) +{ + asm("rolq %2,%0" + :"=r" (word) + :"0" (word),"J" (i)); + return word; +} + +static inline unsigned long ROR64c(unsigned long word, const int i) +{ + asm("rorq %2,%0" + :"=r" (word) + :"0" (word),"J" (i)); + return word; +} + +#else /* LTC_NO_ROLC */ + +#define ROL64c ROL64 +#define ROR64c ROR64 + +#endif + +#else /* Not x86_64 */ + +#define ROL64(x, y) \ + ( (((x)<<((ulong64)(y)&63)) | \ + (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF)) + +#define ROR64(x, y) \ + ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ + ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) + +#define ROL64c(x, y) \ + ( (((x)<<((ulong64)(y)&63)) | \ + (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF)) + +#define ROR64c(x, y) \ + ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ + ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) + +#endif + +#ifndef MAX + #define MAX(x, y) ( ((x)>(y))?(x):(y) ) +#endif + +#ifndef MIN + #define MIN(x, y) ( ((x)<(y))?(x):(y) ) +#endif + +/* extract a byte portably */ +#ifdef _MSC_VER + #define byte(x, n) ((unsigned char)((x) >> (8 * (n)))) +#else + #define byte(x, n) (((x) >> (8 * (n))) & 255) +#endif + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_macros.h,v $ */ +/* $Revision: 1.15 $ */ +/* $Date: 2006/11/29 23:43:57 $ */ diff --git a/crypto777/hmac/tomcrypt_math.h b/crypto777/hmac/tomcrypt_math.h new file mode 100755 index 000000000..a05d7fff9 --- /dev/null +++ b/crypto777/hmac/tomcrypt_math.h @@ -0,0 +1,500 @@ +/** math functions **/ + +#define LTC_MP_LT -1 +#define LTC_MP_EQ 0 +#define LTC_MP_GT 1 + +#define LTC_MP_NO 0 +#define LTC_MP_YES 1 + +#ifndef LTC_MECC + typedef void ecc_point; +#endif + +#ifndef LTC_MRSA + typedef void rsa_key; +#endif + +/** math descriptor */ +typedef struct { + /** Name of the math provider */ + char *name; + + /** Bits per digit, amount of bits must fit in an unsigned long */ + int bits_per_digit; + +/* ---- init/deinit functions ---- */ + + /** initialize a bignum + @param a The number to initialize + @return CRYPT_OK on success + */ + int (*init)(void **a); + + /** init copy + @param dst The number to initialize and write to + @param src The number to copy from + @return CRYPT_OK on success + */ + int (*init_copy)(void **dst, void *src); + + /** deinit + @param a The number to free + @return CRYPT_OK on success + */ + void (*deinit)(void *a); + +/* ---- data movement ---- */ + + /** negate + @param src The number to negate + @param dst The destination + @return CRYPT_OK on success + */ + int (*neg)(void *src, void *dst); + + /** copy + @param src The number to copy from + @param dst The number to write to + @return CRYPT_OK on success + */ + int (*copy)(void *src, void *dst); + +/* ---- trivial low level functions ---- */ + + /** set small constant + @param a Number to write to + @param n Source upto bits_per_digit (actually meant for very small constants) + @return CRYPT_OK on succcess + */ + int (*set_int)(void *a, unsigned long n); + + /** get small constant + @param a Number to read, only fetches upto bits_per_digit from the number + @return The lower bits_per_digit of the integer (unsigned) + */ + unsigned long (*get_int)(void *a); + + /** get digit n + @param a The number to read from + @param n The number of the digit to fetch + @return The bits_per_digit sized n'th digit of a + */ + unsigned long (*get_digit)(void *a, int n); + + /** Get the number of digits that represent the number + @param a The number to count + @return The number of digits used to represent the number + */ + int (*get_digit_count)(void *a); + + /** compare two integers + @param a The left side integer + @param b The right side integer + @return LTC_MP_LT if a < b, LTC_MP_GT if a > b and LTC_MP_EQ otherwise. (signed comparison) + */ + int (*compare)(void *a, void *b); + + /** compare against int + @param a The left side integer + @param b The right side integer (upto bits_per_digit) + @return LTC_MP_LT if a < b, LTC_MP_GT if a > b and LTC_MP_EQ otherwise. (signed comparison) + */ + int (*compare_d)(void *a, unsigned long n); + + /** Count the number of bits used to represent the integer + @param a The integer to count + @return The number of bits required to represent the integer + */ + int (*count_bits)(void * a); + + /** Count the number of LSB bits which are zero + @param a The integer to count + @return The number of contiguous zero LSB bits + */ + int (*count_lsb_bits)(void *a); + + /** Compute a power of two + @param a The integer to store the power in + @param n The power of two you want to store (a = 2^n) + @return CRYPT_OK on success + */ + int (*twoexpt)(void *a , int n); + +/* ---- radix conversions ---- */ + + /** read ascii string + @param a The integer to store into + @param str The string to read + @param radix The radix the integer has been represented in (2-64) + @return CRYPT_OK on success + */ + int (*read_radix)(void *a, const char *str, int radix); + + /** write number to string + @param a The integer to store + @param str The destination for the string + @param radix The radix the integer is to be represented in (2-64) + @return CRYPT_OK on success + */ + int (*write_radix)(void *a, char *str, int radix); + + /** get size as unsigned char string + @param a The integer to get the size (when stored in array of octets) + @return The length of the integer + */ + unsigned long (*unsigned_size)(void *a); + + /** store an integer as an array of octets + @param src The integer to store + @param dst The buffer to store the integer in + @return CRYPT_OK on success + */ + int (*unsigned_write)(void *src, unsigned char *dst); + + /** read an array of octets and store as integer + @param dst The integer to load + @param src The array of octets + @param len The number of octets + @return CRYPT_OK on success + */ + int (*unsigned_read)(void *dst, unsigned char *src, unsigned long len); + +/* ---- basic math ---- */ + + /** add two integers + @param a The first source integer + @param b The second source integer + @param c The destination of "a + b" + @return CRYPT_OK on success + */ + int (*add)(void *a, void *b, void *c); + + + /** add two integers + @param a The first source integer + @param b The second source integer (single digit of upto bits_per_digit in length) + @param c The destination of "a + b" + @return CRYPT_OK on success + */ + int (*addi)(void *a, unsigned long b, void *c); + + /** subtract two integers + @param a The first source integer + @param b The second source integer + @param c The destination of "a - b" + @return CRYPT_OK on success + */ + int (*sub)(void *a, void *b, void *c); + + /** subtract two integers + @param a The first source integer + @param b The second source integer (single digit of upto bits_per_digit in length) + @param c The destination of "a - b" + @return CRYPT_OK on success + */ + int (*subi)(void *a, unsigned long b, void *c); + + /** multiply two integers + @param a The first source integer + @param b The second source integer (single digit of upto bits_per_digit in length) + @param c The destination of "a * b" + @return CRYPT_OK on success + */ + int (*mul)(void *a, void *b, void *c); + + /** multiply two integers + @param a The first source integer + @param b The second source integer (single digit of upto bits_per_digit in length) + @param c The destination of "a * b" + @return CRYPT_OK on success + */ + int (*muli)(void *a, unsigned long b, void *c); + + /** Square an integer + @param a The integer to square + @param b The destination + @return CRYPT_OK on success + */ + int (*sqr)(void *a, void *b); + + /** Divide an integer + @param a The dividend + @param b The divisor + @param c The quotient (can be NULL to signify don't care) + @param d The remainder (can be NULL to signify don't care) + @return CRYPT_OK on success + */ + int (*mpdiv)(void *a, void *b, void *c, void *d); + + /** divide by two + @param a The integer to divide (shift right) + @param b The destination + @return CRYPT_OK on success + */ + int (*div_2)(void *a, void *b); + + /** Get remainder (small value) + @param a The integer to reduce + @param b The modulus (upto bits_per_digit in length) + @param c The destination for the residue + @return CRYPT_OK on success + */ + int (*modi)(void *a, unsigned long b, unsigned long *c); + + /** gcd + @param a The first integer + @param b The second integer + @param c The destination for (a, b) + @return CRYPT_OK on success + */ + int (*gcd)(void *a, void *b, void *c); + + /** lcm + @param a The first integer + @param b The second integer + @param c The destination for [a, b] + @return CRYPT_OK on success + */ + int (*lcm)(void *a, void *b, void *c); + + /** Modular multiplication + @param a The first source + @param b The second source + @param c The modulus + @param d The destination (a*b mod c) + @return CRYPT_OK on success + */ + int (*mulmod)(void *a, void *b, void *c, void *d); + + /** Modular squaring + @param a The first source + @param b The modulus + @param c The destination (a*a mod b) + @return CRYPT_OK on success + */ + int (*sqrmod)(void *a, void *b, void *c); + + /** Modular inversion + @param a The value to invert + @param b The modulus + @param c The destination (1/a mod b) + @return CRYPT_OK on success + */ + int (*invmod)(void *, void *, void *); + +/* ---- reduction ---- */ + + /** setup montgomery + @param a The modulus + @param b The destination for the reduction digit + @return CRYPT_OK on success + */ + int (*montgomery_setup)(void *a, void **b); + + /** get normalization value + @param a The destination for the normalization value + @param b The modulus + @return CRYPT_OK on success + */ + int (*montgomery_normalization)(void *a, void *b); + + /** reduce a number + @param a The number [and dest] to reduce + @param b The modulus + @param c The value "b" from montgomery_setup() + @return CRYPT_OK on success + */ + int (*montgomery_reduce)(void *a, void *b, void *c); + + /** clean up (frees memory) + @param a The value "b" from montgomery_setup() + @return CRYPT_OK on success + */ + void (*montgomery_deinit)(void *a); + +/* ---- exponentiation ---- */ + + /** Modular exponentiation + @param a The base integer + @param b The power (can be negative) integer + @param c The modulus integer + @param d The destination + @return CRYPT_OK on success + */ + int (*exptmod)(void *a, void *b, void *c, void *d); + + /** Primality testing + @param a The integer to test + @param b The destination of the result (FP_YES if prime) + @return CRYPT_OK on success + */ + int (*isprime)(void *a, int *b); + +/* ---- (optional) ecc point math ---- */ + + /** ECC GF(p) point multiplication (from the NIST curves) + @param k The integer to multiply the point by + @param G The point to multiply + @param R The destination for kG + @param modulus The modulus for the field + @param map Boolean indicated whether to map back to affine or not (can be ignored if you work in affine only) + @return CRYPT_OK on success + */ + int (*ecc_ptmul)(void *k, ecc_point *G, ecc_point *R, void *modulus, int map); + + /** ECC GF(p) point addition + @param P The first point + @param Q The second point + @param R The destination of P + Q + @param modulus The modulus + @param mp The "b" value from montgomery_setup() + @return CRYPT_OK on success + */ + int (*ecc_ptadd)(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp); + + /** ECC GF(p) point double + @param P The first point + @param R The destination of 2P + @param modulus The modulus + @param mp The "b" value from montgomery_setup() + @return CRYPT_OK on success + */ + int (*ecc_ptdbl)(ecc_point *P, ecc_point *R, void *modulus, void *mp); + + /** ECC mapping from projective to affine, currently uses (x,y,z) => (x/z^2, y/z^3, 1) + @param P The point to map + @param modulus The modulus + @param mp The "b" value from montgomery_setup() + @return CRYPT_OK on success + @remark The mapping can be different but keep in mind a ecc_point only has three + integers (x,y,z) so if you use a different mapping you have to make it fit. + */ + int (*ecc_map)(ecc_point *P, void *modulus, void *mp); + + /** Computes kA*A + kB*B = C using Shamir's Trick + @param A First point to multiply + @param kA What to multiple A by + @param B Second point to multiply + @param kB What to multiple B by + @param C [out] Destination point (can overlap with A or B + @param modulus Modulus for curve + @return CRYPT_OK on success + */ + int (*ecc_mul2add)(ecc_point *A, void *kA, + ecc_point *B, void *kB, + ecc_point *C, + void *modulus); + +/* ---- (optional) rsa optimized math (for internal CRT) ---- */ + + /** RSA Key Generation + @param prng An active PRNG state + @param wprng The index of the PRNG desired + @param size The size of the modulus (key size) desired (octets) + @param e The "e" value (public key). e==65537 is a good choice + @param key [out] Destination of a newly created private key pair + @return CRYPT_OK if successful, upon error all allocated ram is freed + */ + int (*rsa_keygen)(prng_state *prng, int wprng, int size, long e, rsa_key *key); + + + /** RSA exponentiation + @param in The octet array representing the base + @param inlen The length of the input + @param out The destination (to be stored in an octet array format) + @param outlen The length of the output buffer and the resulting size (zero padded to the size of the modulus) + @param which PK_PUBLIC for public RSA and PK_PRIVATE for private RSA + @param key The RSA key to use + @return CRYPT_OK on success + */ + int (*rsa_me)(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, int which, + rsa_key *key); +} ltc_math_descriptor; + +extern ltc_math_descriptor ltc_mp; + +int ltc_init_multi(void **a, ...); +void ltc_deinit_multi(void *a, ...); + +#ifdef LTM_DESC +extern const ltc_math_descriptor ltm_desc; +#endif + +#ifdef TFM_DESC +extern const ltc_math_descriptor tfm_desc; +#endif + +#ifdef GMP_DESC +extern const ltc_math_descriptor gmp_desc; +#endif + +#if !defined(DESC_DEF_ONLY) && defined(LTC_SOURCE) + +#define MP_DIGIT_BIT ltc_mp.bits_per_digit + +/* some handy macros */ +#define mp_init(a) ltc_mp.init(a) +#define mp_init_multi ltc_init_multi +#define mp_clear(a) ltc_mp.deinit(a) +#define mp_clear_multi ltc_deinit_multi +#define mp_init_copy(a, b) ltc_mp.init_copy(a, b) + +#define mp_neg(a, b) ltc_mp.neg(a, b) +#define mp_copy(a, b) ltc_mp.copy(a, b) + +#define mp_set(a, b) ltc_mp.set_int(a, b) +#define mp_set_int(a, b) ltc_mp.set_int(a, b) +#define mp_get_int(a) ltc_mp.get_int(a) +#define mp_get_digit(a, n) ltc_mp.get_digit(a, n) +#define mp_get_digit_count(a) ltc_mp.get_digit_count(a) +#define mp_cmp(a, b) ltc_mp.compare(a, b) +#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b) +#define mp_count_bits(a) ltc_mp.count_bits(a) +#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a) +#define mp_2expt(a, b) ltc_mp.twoexpt(a, b) + +#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c) +#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c) +#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a) +#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b) +#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c) + +#define mp_add(a, b, c) ltc_mp.add(a, b, c) +#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c) +#define mp_sub(a, b, c) ltc_mp.sub(a, b, c) +#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c) +#define mp_mul(a, b, c) ltc_mp.mul(a, b, c) +#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c) +#define mp_sqr(a, b) ltc_mp.sqr(a, b) +#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d) +#define mp_div_2(a, b) ltc_mp.div_2(a, b) +#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c) +#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c) +#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c) +#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c) + +#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d) +#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c) +#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c) + +#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b) +#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b) +#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c) +#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a) + +#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d) +#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, c) + +#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO) +#define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO) +#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0); + +#define mp_tohex(a, b) mp_toradix(a, b, 16) + +#endif + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_math.h,v $ */ +/* $Revision: 1.44 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ diff --git a/crypto777/hmac/tomcrypt_misc.h b/crypto777/hmac/tomcrypt_misc.h new file mode 100755 index 000000000..f118e22f1 --- /dev/null +++ b/crypto777/hmac/tomcrypt_misc.h @@ -0,0 +1,23 @@ +/* ---- LTC_BASE64 Routines ---- */ +#ifdef LTC_BASE64 +int base64_encode(const unsigned char *in, unsigned long len, + unsigned char *out, unsigned long *outlen); + +int base64_decode(const unsigned char *in, unsigned long len, + unsigned char *out, unsigned long *outlen); +#endif + +/* ---- MEM routines ---- */ +//void zeromem(void *dst, size_t len); +void burn_stack(unsigned long len); + +const char *error_to_string(int err); + +extern const char *crypt_build_settings; + +/* ---- HMM ---- */ +int crypt_fsa(void *mp, ...); + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_misc.h,v $ */ +/* $Revision: 1.5 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ diff --git a/crypto777/hmac/tomcrypt_pk.h b/crypto777/hmac/tomcrypt_pk.h new file mode 100755 index 000000000..800fab3a2 --- /dev/null +++ b/crypto777/hmac/tomcrypt_pk.h @@ -0,0 +1,552 @@ +/* ---- NUMBER THEORY ---- */ + +enum { + PK_PUBLIC=0, + PK_PRIVATE=1 +}; + +int rand_prime(void *N, long len, prng_state *prng, int wprng); + +/* ---- RSA ---- */ +#ifdef LTC_MRSA + +/* Min and Max RSA key sizes (in bits) */ +#define MIN_RSA_SIZE 1024 +#define MAX_RSA_SIZE 4096 + +/** RSA LTC_PKCS style key */ +typedef struct Rsa_key { + /** Type of key, PK_PRIVATE or PK_PUBLIC */ + int type; + /** The public exponent */ + void *e; + /** The private exponent */ + void *d; + /** The modulus */ + void *N; + /** The p factor of N */ + void *p; + /** The q factor of N */ + void *q; + /** The 1/q mod p CRT param */ + void *qP; + /** The d mod (p - 1) CRT param */ + void *dP; + /** The d mod (q - 1) CRT param */ + void *dQ; +} rsa_key; + +int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); + +int rsa_exptmod(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, int which, + rsa_key *key); + +void rsa_free(rsa_key *key); + +/* These use LTC_PKCS #1 v2.0 padding */ +#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \ + rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_LTC_PKCS_1_OAEP, _key) + +#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \ + rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_LTC_PKCS_1_OAEP, _stat, _key) + +#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \ + rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key) + +#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \ + rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key) + +/* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */ +int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + const unsigned char *lparam, unsigned long lparamlen, + prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key); + +int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + const unsigned char *lparam, unsigned long lparamlen, + int hash_idx, int padding, + int *stat, rsa_key *key); + +int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + int padding, + prng_state *prng, int prng_idx, + int hash_idx, unsigned long saltlen, + rsa_key *key); + +int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, + const unsigned char *hash, unsigned long hashlen, + int padding, + int hash_idx, unsigned long saltlen, + int *stat, rsa_key *key); + +/* LTC_PKCS #1 import/export */ +int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); +int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); + +#endif + +/* ---- Katja ---- */ +#ifdef MKAT + +/* Min and Max KAT key sizes (in bits) */ +#define MIN_KAT_SIZE 1024 +#define MAX_KAT_SIZE 4096 + +/** Katja LTC_PKCS style key */ +typedef struct KAT_key { + /** Type of key, PK_PRIVATE or PK_PUBLIC */ + int type; + /** The private exponent */ + void *d; + /** The modulus */ + void *N; + /** The p factor of N */ + void *p; + /** The q factor of N */ + void *q; + /** The 1/q mod p CRT param */ + void *qP; + /** The d mod (p - 1) CRT param */ + void *dP; + /** The d mod (q - 1) CRT param */ + void *dQ; + /** The pq param */ + void *pq; +} katja_key; + +int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key); + +int katja_exptmod(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, int which, + katja_key *key); + +void katja_free(katja_key *key); + +/* These use LTC_PKCS #1 v2.0 padding */ +int katja_encrypt_key(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + const unsigned char *lparam, unsigned long lparamlen, + prng_state *prng, int prng_idx, int hash_idx, katja_key *key); + +int katja_decrypt_key(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + const unsigned char *lparam, unsigned long lparamlen, + int hash_idx, int *stat, + katja_key *key); + +/* LTC_PKCS #1 import/export */ +int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key); +int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key); + +#endif + +/* ---- ECC Routines ---- */ +#ifdef LTC_MECC + +/* size of our temp buffers for exported keys */ +#define ECC_BUF_SIZE 256 + +/* max private key size */ +#define ECC_MAXSIZE 66 + +/** Structure defines a NIST GF(p) curve */ +typedef struct { + /** The size of the curve in octets */ + int size; + + /** name of curve */ + char *name; + + /** The prime that defines the field the curve is in (encoded in hex) */ + char *prime; + + /** The fields B param (hex) */ + char *B; + + /** The order of the curve (hex) */ + char *order; + + /** The x co-ordinate of the base point on the curve (hex) */ + char *Gx; + + /** The y co-ordinate of the base point on the curve (hex) */ + char *Gy; +} ltc_ecc_set_type; + +/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */ +typedef struct { + /** The x co-ordinate */ + void *x; + + /** The y co-ordinate */ + void *y; + + /** The z co-ordinate */ + void *z; +} ecc_point; + +/** An ECC key */ +typedef struct { + /** Type of key, PK_PRIVATE or PK_PUBLIC */ + int type; + + /** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */ + int idx; + + /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */ + const ltc_ecc_set_type *dp; + + /** The public key */ + ecc_point pubkey; + + /** The private key */ + void *k; +} ecc_key; + +/** the ECC params provided */ +extern const ltc_ecc_set_type ltc_ecc_sets[]; + +int ecc_test(void); +void ecc_sizes(int *low, int *high); +int ecc_get_size(ecc_key *key); + +int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); +int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp); +void ecc_free(ecc_key *key); + +int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); +int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); +int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp); + +int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen); +int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key); +int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp); + +int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, + unsigned char *out, unsigned long *outlen); + +int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + prng_state *prng, int wprng, int hash, + ecc_key *key); + +int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + ecc_key *key); + +int ecc_sign_hash(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + prng_state *prng, int wprng, ecc_key *key); + +int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, + const unsigned char *hash, unsigned long hashlen, + int *stat, ecc_key *key); + +/* low level functions */ +ecc_point *ltc_ecc_new_point(void); +void ltc_ecc_del_point(ecc_point *p); +int ltc_ecc_is_valid_idx(int n); + +/* point ops (mp == montgomery digit) */ +#if !defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC) || defined(GMP_LTC_DESC) +/* R = 2P */ +int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp); + +/* R = P + Q */ +int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp); +#endif + +#if defined(LTC_MECC_FP) +/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */ +int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map); + +/* functions for saving/loading/freeing/adding to fixed point cache */ +int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen); +int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen); +void ltc_ecc_fp_free(void); +int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock); + +/* lock/unlock all points currently in fixed point cache */ +void ltc_ecc_fp_tablelock(int lock); +#endif + +/* R = kG */ +int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map); + +#ifdef LTC_ECC_SHAMIR +/* kA*A + kB*B = C */ +int ltc_ecc_mul2add(ecc_point *A, void *kA, + ecc_point *B, void *kB, + ecc_point *C, + void *modulus); + +#ifdef LTC_MECC_FP +/* Shamir's trick with optimized point multiplication using fixed point cache */ +int ltc_ecc_fp_mul2add(ecc_point *A, void *kA, + ecc_point *B, void *kB, + ecc_point *C, void *modulus); +#endif + +#endif + + +/* map P to affine from projective */ +int ltc_ecc_map(ecc_point *P, void *modulus, void *mp); + +#endif + +#ifdef LTC_MDSA + +/* Max diff between group and modulus size in bytes */ +#define LTC_MDSA_DELTA 512 + +/* Max DSA group size in bytes (default allows 4k-bit groups) */ +#define LTC_MDSA_MAX_GROUP 512 + +/** DSA key structure */ +typedef struct { + /** The key type, PK_PRIVATE or PK_PUBLIC */ + int type; + + /** The order of the sub-group used in octets */ + int qord; + + /** The generator */ + void *g; + + /** The prime used to generate the sub-group */ + void *q; + + /** The large prime that generats the field the contains the sub-group */ + void *p; + + /** The private key */ + void *x; + + /** The public key */ + void *y; +} dsa_key; + +int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); +void dsa_free(dsa_key *key); + +int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen, + void *r, void *s, + prng_state *prng, int wprng, dsa_key *key); + +int dsa_sign_hash(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + prng_state *prng, int wprng, dsa_key *key); + +int dsa_verify_hash_raw( void *r, void *s, + const unsigned char *hash, unsigned long hashlen, + int *stat, dsa_key *key); + +int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, + const unsigned char *hash, unsigned long hashlen, + int *stat, dsa_key *key); + +int dsa_encrypt_key(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + prng_state *prng, int wprng, int hash, + dsa_key *key); + +int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen, + dsa_key *key); + +int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); +int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); +int dsa_verify_key(dsa_key *key, int *stat); + +int dsa_shared_secret(void *private_key, void *base, + dsa_key *public_key, + unsigned char *out, unsigned long *outlen); +#endif + +#ifdef LTC_DER +/* DER handling */ + +enum { + LTC_ASN1_EOL, + LTC_ASN1_BOOLEAN, + LTC_ASN1_INTEGER, + LTC_ASN1_SHORT_INTEGER, + LTC_ASN1_BIT_STRING, + LTC_ASN1_OCTET_STRING, + LTC_ASN1_NULL, + LTC_ASN1_OBJECT_IDENTIFIER, + LTC_ASN1_IA5_STRING, + LTC_ASN1_PRINTABLE_STRING, + LTC_ASN1_UTF8_STRING, + LTC_ASN1_UTCTIME, + LTC_ASN1_CHOICE, + LTC_ASN1_SEQUENCE, + LTC_ASN1_SET, + LTC_ASN1_SETOF +}; + +/** A LTC ASN.1 list type */ +typedef struct ltc_asn1_list_ { + /** The LTC ASN.1 enumerated type identifier */ + int type; + /** The data to encode or place for decoding */ + void *data; + /** The size of the input or resulting output */ + unsigned long size; + /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */ + int used; + /** prev/next entry in the list */ + struct ltc_asn1_list_ *prev, *next, *child, *parent; +} ltc_asn1_list; + +#define LTC_SET_ASN1(list, index, Type, Data, Size) \ + do { \ + int LTC_MACRO_temp = (index); \ + ltc_asn1_list *LTC_MACRO_list = (list); \ + LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \ + LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \ + LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \ + LTC_MACRO_list[LTC_MACRO_temp].used = 0; \ + } while (0); + +/* SEQUENCE */ +int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, + unsigned char *out, unsigned long *outlen, int type_of); + +#define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE) + +int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen, + ltc_asn1_list *list, unsigned long outlen, int ordered); + +#define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1) + +int der_length_sequence(ltc_asn1_list *list, unsigned long inlen, + unsigned long *outlen); + +/* SET */ +#define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0) +#define der_length_set der_length_sequence +int der_encode_set(ltc_asn1_list *list, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + +int der_encode_setof(ltc_asn1_list *list, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + +/* VA list handy helpers with triplets of */ +int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...); +int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...); + +/* FLEXI DECODER handle unknown list decoder */ +int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out); +void der_free_sequence_flexi(ltc_asn1_list *list); +void der_sequence_free(ltc_asn1_list *in); + +/* BOOLEAN */ +int der_length_boolean(unsigned long *outlen); +int der_encode_boolean(int in, + unsigned char *out, unsigned long *outlen); +int der_decode_boolean(const unsigned char *in, unsigned long inlen, + int *out); +/* INTEGER */ +int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen); +int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num); +int der_length_integer(void *num, unsigned long *len); + +/* INTEGER -- handy for 0..2^32-1 values */ +int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num); +int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen); +int der_length_short_integer(unsigned long num, unsigned long *outlen); + +/* BIT STRING */ +int der_encode_bit_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_decode_bit_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_length_bit_string(unsigned long nbits, unsigned long *outlen); + +/* OCTET STRING */ +int der_encode_octet_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_decode_octet_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_length_octet_string(unsigned long noctets, unsigned long *outlen); + +/* OBJECT IDENTIFIER */ +int der_encode_object_identifier(unsigned long *words, unsigned long nwords, + unsigned char *out, unsigned long *outlen); +int der_decode_object_identifier(const unsigned char *in, unsigned long inlen, + unsigned long *words, unsigned long *outlen); +int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen); +unsigned long der_object_identifier_bits(unsigned long x); + +/* IA5 STRING */ +int der_encode_ia5_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_decode_ia5_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); + +int der_ia5_char_encode(int c); +int der_ia5_value_decode(int v); + +/* Printable STRING */ +int der_encode_printable_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_decode_printable_string(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); + +int der_printable_char_encode(int c); +int der_printable_value_decode(int v); + +/* UTF-8 */ +#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR) +#include +#else +typedef ulong32 wchar_t; +#endif + +int der_encode_utf8_string(const wchar_t *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + +int der_decode_utf8_string(const unsigned char *in, unsigned long inlen, + wchar_t *out, unsigned long *outlen); +unsigned long der_utf8_charsize(const wchar_t c); +int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen); + + +/* CHOICE */ +int der_decode_choice(const unsigned char *in, unsigned long *inlen, + ltc_asn1_list *list, unsigned long outlen); + +/* UTCTime */ +typedef struct { + unsigned YY, /* year */ + MM, /* month */ + DD, /* day */ + hh, /* hour */ + mm, /* minute */ + ss, /* second */ + off_dir, /* timezone offset direction 0 == +, 1 == - */ + off_hh, /* timezone offset hours */ + off_mm; /* timezone offset minutes */ +} ltc_utctime; + +int der_encode_utctime(ltc_utctime *utctime, + unsigned char *out, unsigned long *outlen); + +int der_decode_utctime(const unsigned char *in, unsigned long *inlen, + ltc_utctime *out); + +int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen); + + +#endif + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_pk.h,v $ */ +/* $Revision: 1.81 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ diff --git a/crypto777/hmac/tomcrypt_pkcs.h b/crypto777/hmac/tomcrypt_pkcs.h new file mode 100755 index 000000000..84fb82a62 --- /dev/null +++ b/crypto777/hmac/tomcrypt_pkcs.h @@ -0,0 +1,89 @@ +/* LTC_PKCS Header Info */ + +/* ===> LTC_PKCS #1 -- RSA Cryptography <=== */ +#ifdef LTC_PKCS_1 + +enum ltc_pkcs_1_v1_5_blocks +{ + LTC_LTC_PKCS_1_EMSA = 1, /* Block type 1 (LTC_PKCS #1 v1.5 signature padding) */ + LTC_LTC_PKCS_1_EME = 2 /* Block type 2 (LTC_PKCS #1 v1.5 encryption padding) */ +}; + +enum ltc_pkcs_1_paddings +{ + LTC_LTC_PKCS_1_V1_5 = 1, /* LTC_PKCS #1 v1.5 padding (\sa ltc_pkcs_1_v1_5_blocks) */ + LTC_LTC_PKCS_1_OAEP = 2, /* LTC_PKCS #1 v2.0 encryption padding */ + LTC_LTC_PKCS_1_PSS = 3 /* LTC_PKCS #1 v2.1 signature padding */ +}; + +int pkcs_1_mgf1( int hash_idx, + const unsigned char *seed, unsigned long seedlen, + unsigned char *mask, unsigned long masklen); + +int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out); +int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen); + +/* *** v1.5 padding */ +int pkcs_1_v1_5_encode(const unsigned char *msg, + unsigned long msglen, + int block_type, + unsigned long modulus_bitlen, + prng_state *prng, + int prng_idx, + unsigned char *out, + unsigned long *outlen); + +int pkcs_1_v1_5_decode(const unsigned char *msg, + unsigned long msglen, + int block_type, + unsigned long modulus_bitlen, + unsigned char *out, + unsigned long *outlen, + int *is_valid); + +/* *** v2.1 padding */ +int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, + const unsigned char *lparam, unsigned long lparamlen, + unsigned long modulus_bitlen, prng_state *prng, + int prng_idx, int hash_idx, + unsigned char *out, unsigned long *outlen); + +int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, + const unsigned char *lparam, unsigned long lparamlen, + unsigned long modulus_bitlen, int hash_idx, + unsigned char *out, unsigned long *outlen, + int *res); + +int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, + unsigned long saltlen, prng_state *prng, + int prng_idx, int hash_idx, + unsigned long modulus_bitlen, + unsigned char *out, unsigned long *outlen); + +int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, + const unsigned char *sig, unsigned long siglen, + unsigned long saltlen, int hash_idx, + unsigned long modulus_bitlen, int *res); + +#endif /* LTC_PKCS_1 */ + +/* ===> LTC_PKCS #5 -- Password Based Cryptography <=== */ +#ifdef LTC_PKCS_5 + +/* Algorithm #1 (old) */ +int pkcs_5_alg1(const unsigned char *password, unsigned long password_len, + const unsigned char *salt, + int iteration_count, int hash_idx, + unsigned char *out, unsigned long *outlen); + +/* Algorithm #2 (new) */ +int pkcs_5_alg2(const unsigned char *password, unsigned long password_len, + const unsigned char *salt, unsigned long salt_len, + int iteration_count, int hash_idx, + unsigned char *out, unsigned long *outlen); + +#endif /* LTC_PKCS_5 */ + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_pkcs.h,v $ */ +/* $Revision: 1.8 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ diff --git a/crypto777/hmac/tomcrypt_prng.h b/crypto777/hmac/tomcrypt_prng.h new file mode 100755 index 000000000..f3e3e550e --- /dev/null +++ b/crypto777/hmac/tomcrypt_prng.h @@ -0,0 +1,199 @@ +/* ---- PRNG Stuff ---- */ +#ifdef LTC_YARROW +struct yarrow_prng { + int cipher, hash; + unsigned char pool[MAXBLOCKSIZE]; + symmetric_CTR ctr; + LTC_MUTEX_TYPE(prng_lock) +}; +#endif + +#ifdef LTC_RC4 +struct rc4_prng { + int x, y; + unsigned char buf[256]; +}; +#endif + +#ifdef LTC_FORTUNA +struct fortuna_prng { + hash_state pool[LTC_FORTUNA_POOLS]; /* the pools */ + + symmetric_key skey; + + unsigned char K[32], /* the current key */ + IV[16]; /* IV for CTR mode */ + + unsigned long pool_idx, /* current pool we will add to */ + pool0_len, /* length of 0'th pool */ + wd; + + ulong64 reset_cnt; /* number of times we have reset */ + LTC_MUTEX_TYPE(prng_lock) +}; +#endif + +#ifdef LTC_SOBER128 +struct sober128_prng { + ulong32 R[17], /* Working storage for the shift register */ + initR[17], /* saved register contents */ + konst, /* key dependent constant */ + sbuf; /* partial word encryption buffer */ + + int nbuf, /* number of part-word stream bits buffered */ + flag, /* first add_entropy call or not? */ + set; /* did we call add_entropy to set key? */ + +}; +#endif + +typedef union Prng_state { + char dummy[1]; +#ifdef LTC_YARROW + struct yarrow_prng yarrow; +#endif +#ifdef LTC_RC4 + struct rc4_prng rc4; +#endif +#ifdef LTC_FORTUNA + struct fortuna_prng fortuna; +#endif +#ifdef LTC_SOBER128 + struct sober128_prng sober128; +#endif +} prng_state; + +/** PRNG descriptor */ +extern struct ltc_prng_descriptor { + /** Name of the PRNG */ + char *name; + /** size in bytes of exported state */ + int export_size; + /** Start a PRNG state + @param prng [out] The state to initialize + @return CRYPT_OK if successful + */ + int (*start)(prng_state *prng); + /** Add entropy to the PRNG + @param in The entropy + @param inlen Length of the entropy (octets)\ + @param prng The PRNG state + @return CRYPT_OK if successful + */ + int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng); + /** Ready a PRNG state to read from + @param prng The PRNG state to ready + @return CRYPT_OK if successful + */ + int (*ready)(prng_state *prng); + /** Read from the PRNG + @param out [out] Where to store the data + @param outlen Length of data desired (octets) + @param prng The PRNG state to read from + @return Number of octets read + */ + unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng); + /** Terminate a PRNG state + @param prng The PRNG state to terminate + @return CRYPT_OK if successful + */ + int (*done)(prng_state *prng); + /** Export a PRNG state + @param out [out] The destination for the state + @param outlen [in/out] The max size and resulting size of the PRNG state + @param prng The PRNG to export + @return CRYPT_OK if successful + */ + int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng); + /** Import a PRNG state + @param in The data to import + @param inlen The length of the data to import (octets) + @param prng The PRNG to initialize/import + @return CRYPT_OK if successful + */ + int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng); + /** Self-test the PRNG + @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled + */ + int (*test)(void); +} prng_descriptor[]; + +#ifdef LTC_YARROW +int yarrow_start(prng_state *prng); +int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng); +int yarrow_ready(prng_state *prng); +unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng); +int yarrow_done(prng_state *prng); +int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng); +int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng); +int yarrow_test(void); +extern const struct ltc_prng_descriptor yarrow_desc; +#endif + +#ifdef LTC_FORTUNA +int fortuna_start(prng_state *prng); +int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng); +int fortuna_ready(prng_state *prng); +unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng); +int fortuna_done(prng_state *prng); +int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng); +int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng); +int fortuna_test(void); +extern const struct ltc_prng_descriptor fortuna_desc; +#endif + +#ifdef LTC_RC4 +int rc4_start(prng_state *prng); +int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng); +int rc4_ready(prng_state *prng); +unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng); +int rc4_done(prng_state *prng); +int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng); +int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng); +int rc4_test(void); +extern const struct ltc_prng_descriptor rc4_desc; +#endif + +#ifdef LTC_SPRNG +int sprng_start(prng_state *prng); +int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng); +int sprng_ready(prng_state *prng); +unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng); +int sprng_done(prng_state *prng); +int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng); +int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng); +int sprng_test(void); +extern const struct ltc_prng_descriptor sprng_desc; +#endif + +#ifdef LTC_SOBER128 +int sober128_start(prng_state *prng); +int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng); +int sober128_ready(prng_state *prng); +unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng); +int sober128_done(prng_state *prng); +int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng); +int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng); +int sober128_test(void); +extern const struct ltc_prng_descriptor sober128_desc; +#endif + +int find_prng(const char *name); +int register_prng(const struct ltc_prng_descriptor *prng); +int unregister_prng(const struct ltc_prng_descriptor *prng); +int prng_is_valid(int idx); +LTC_MUTEX_PROTO(ltc_prng_mutex) + +/* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this + * might not work on all platforms as planned + */ +unsigned long rng_get_bytes(unsigned char *out, + unsigned long outlen, + void (*callback)(void)); + +int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void)); + + +/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_prng.h,v $ */ +/* $Revision: 1.9 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ diff --git a/crypto777/hmac/whirl.c b/crypto777/hmac/whirl.c new file mode 100755 index 000000000..a025657ac --- /dev/null +++ b/crypto777/hmac/whirl.c @@ -0,0 +1,325 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +/** + @file whirl.c + LTC_WHIRLPOOL (using their new sbox) hash function by Tom St Denis +*/ + +#include "tomcrypt.h" + +#ifdef LTC_WHIRLPOOL + +const struct ltc_hash_descriptor whirlpool_desc = +{ + "whirlpool", + 11, + 64, + 64, + + /* OID */ + { 1, 0, 10118, 3, 0, 55 }, + 6, + + &whirlpool_init, + &whirlpool_process, + &whirlpool_done, + &whirlpool_test, + NULL +}; + +/* the sboxes */ +#include "whirltab.c" + +/* get a_{i,j} */ +#define GB(a,i,j) ((a[(i) & 7] >> (8 * (j))) & 255) + +/* shortcut macro to perform three functions at once */ +#define theta_pi_gamma(a, i) \ + SB0(GB(a, i-0, 7)) ^ \ + SB1(GB(a, i-1, 6)) ^ \ + SB2(GB(a, i-2, 5)) ^ \ + SB3(GB(a, i-3, 4)) ^ \ + SB4(GB(a, i-4, 3)) ^ \ + SB5(GB(a, i-5, 2)) ^ \ + SB6(GB(a, i-6, 1)) ^ \ + SB7(GB(a, i-7, 0)) + +#ifdef LTC_CLEAN_STACK +static int _whirlpool_compress(hash_state *md, unsigned char *buf) +#else +static int whirlpool_compress(hash_state *md, unsigned char *buf) +#endif +{ + ulong64 K[2][8], T[3][8]; + int x, y; + + /* load the block/state */ + for (x = 0; x < 8; x++) { + K[0][x] = md->whirlpool.state[x]; + + LOAD64H(T[0][x], buf + (8 * x)); + T[2][x] = T[0][x]; + T[0][x] ^= K[0][x]; + } + + /* do rounds 1..10 */ + for (x = 0; x < 10; x += 2) { + /* odd round */ + /* apply main transform to K[0] into K[1] */ + for (y = 0; y < 8; y++) { + K[1][y] = theta_pi_gamma(K[0], y); + } + /* xor the constant */ + K[1][0] ^= cont[x]; + + /* apply main transform to T[0] into T[1] */ + for (y = 0; y < 8; y++) { + T[1][y] = theta_pi_gamma(T[0], y) ^ K[1][y]; + } + + /* even round */ + /* apply main transform to K[1] into K[0] */ + for (y = 0; y < 8; y++) { + K[0][y] = theta_pi_gamma(K[1], y); + } + /* xor the constant */ + K[0][0] ^= cont[x+1]; + + /* apply main transform to T[1] into T[0] */ + for (y = 0; y < 8; y++) { + T[0][y] = theta_pi_gamma(T[1], y) ^ K[0][y]; + } + } + + /* store state */ + for (x = 0; x < 8; x++) { + md->whirlpool.state[x] ^= T[0][x] ^ T[2][x]; + } + + return CRYPT_OK; +} + + +#ifdef LTC_CLEAN_STACK +static int whirlpool_compress(hash_state *md, unsigned char *buf) +{ + int err; + err = _whirlpool_compress(md, buf); + burn_stack((5 * 8 * sizeof(ulong64)) + (2 * sizeof(int))); + return err; +} +#endif + + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int whirlpool_init(hash_state * md) +{ + LTC_ARGCHK(md != NULL); + zeromem(&md->whirlpool, sizeof(md->whirlpool)); + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +HASH_PROCESS(whirlpool_process, whirlpool_compress, whirlpool, 64) + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (64 bytes) + @return CRYPT_OK if successful +*/ +int whirlpool_done(hash_state * md, unsigned char *out) +{ + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->whirlpool.curlen >= sizeof(md->whirlpool.buf)) { + return CRYPT_INVALID_ARG; + } + + /* increase the length of the message */ + md->whirlpool.length += md->whirlpool.curlen * 8; + + /* append the '1' bit */ + md->whirlpool.buf[md->whirlpool.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 32 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->whirlpool.curlen > 32) { + while (md->whirlpool.curlen < 64) { + md->whirlpool.buf[md->whirlpool.curlen++] = (unsigned char)0; + } + whirlpool_compress(md, md->whirlpool.buf); + md->whirlpool.curlen = 0; + } + + /* pad upto 56 bytes of zeroes (should be 32 but we only support 64-bit lengths) */ + while (md->whirlpool.curlen < 56) { + md->whirlpool.buf[md->whirlpool.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64H(md->whirlpool.length, md->whirlpool.buf+56); + whirlpool_compress(md, md->whirlpool.buf); + + /* copy output */ + for (i = 0; i < 8; i++) { + STORE64H(md->whirlpool.state[i], out+(8*i)); + } +#ifdef LTC_CLEAN_STACK + zeromem(md, sizeof(*md)); +#endif + return CRYPT_OK; +} + +void calc_whirlpool(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); + hash_state md; + whirlpool_init(&md); + whirlpool_process(&md,message,len); + whirlpool_done(&md,digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,64); +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int whirlpool_test(void) +{ + #ifndef LTC_TEST + return CRYPT_NOP; + #else + static const struct { + int len; + unsigned char msg[128], hash[64]; + } tests[] = { + + /* NULL Message */ +{ + 0, + { 0x00 }, + { 0x19, 0xFA, 0x61, 0xD7, 0x55, 0x22, 0xA4, 0x66, 0x9B, 0x44, 0xE3, 0x9C, 0x1D, 0x2E, 0x17, 0x26, + 0xC5, 0x30, 0x23, 0x21, 0x30, 0xD4, 0x07, 0xF8, 0x9A, 0xFE, 0xE0, 0x96, 0x49, 0x97, 0xF7, 0xA7, + 0x3E, 0x83, 0xBE, 0x69, 0x8B, 0x28, 0x8F, 0xEB, 0xCF, 0x88, 0xE3, 0xE0, 0x3C, 0x4F, 0x07, 0x57, + 0xEA, 0x89, 0x64, 0xE5, 0x9B, 0x63, 0xD9, 0x37, 0x08, 0xB1, 0x38, 0xCC, 0x42, 0xA6, 0x6E, 0xB3 } +}, + + + /* 448-bits of 0 bits */ +{ + + 56, + { 0x00 }, + { 0x0B, 0x3F, 0x53, 0x78, 0xEB, 0xED, 0x2B, 0xF4, 0xD7, 0xBE, 0x3C, 0xFD, 0x81, 0x8C, 0x1B, 0x03, + 0xB6, 0xBB, 0x03, 0xD3, 0x46, 0x94, 0x8B, 0x04, 0xF4, 0xF4, 0x0C, 0x72, 0x6F, 0x07, 0x58, 0x70, + 0x2A, 0x0F, 0x1E, 0x22, 0x58, 0x80, 0xE3, 0x8D, 0xD5, 0xF6, 0xED, 0x6D, 0xE9, 0xB1, 0xE9, 0x61, + 0xE4, 0x9F, 0xC1, 0x31, 0x8D, 0x7C, 0xB7, 0x48, 0x22, 0xF3, 0xD0, 0xE2, 0xE9, 0xA7, 0xE7, 0xB0 } +}, + + /* 520-bits of 0 bits */ +{ + 65, + { 0x00 }, + { 0x85, 0xE1, 0x24, 0xC4, 0x41, 0x5B, 0xCF, 0x43, 0x19, 0x54, 0x3E, 0x3A, 0x63, 0xFF, 0x57, 0x1D, + 0x09, 0x35, 0x4C, 0xEE, 0xBE, 0xE1, 0xE3, 0x25, 0x30, 0x8C, 0x90, 0x69, 0xF4, 0x3E, 0x2A, 0xE4, + 0xD0, 0xE5, 0x1D, 0x4E, 0xB1, 0xE8, 0x64, 0x28, 0x70, 0x19, 0x4E, 0x95, 0x30, 0xD8, 0xD8, 0xAF, + 0x65, 0x89, 0xD1, 0xBF, 0x69, 0x49, 0xDD, 0xF9, 0x0A, 0x7F, 0x12, 0x08, 0x62, 0x37, 0x95, 0xB9 } +}, + + /* 512-bits, leading set */ +{ + 64, + { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x10, 0x3E, 0x00, 0x55, 0xA9, 0xB0, 0x90, 0xE1, 0x1C, 0x8F, 0xDD, 0xEB, 0xBA, 0x06, 0xC0, 0x5A, + 0xCE, 0x8B, 0x64, 0xB8, 0x96, 0x12, 0x8F, 0x6E, 0xED, 0x30, 0x71, 0xFC, 0xF3, 0xDC, 0x16, 0x94, + 0x67, 0x78, 0xE0, 0x72, 0x23, 0x23, 0x3F, 0xD1, 0x80, 0xFC, 0x40, 0xCC, 0xDB, 0x84, 0x30, 0xA6, + 0x40, 0xE3, 0x76, 0x34, 0x27, 0x1E, 0x65, 0x5C, 0xA1, 0x67, 0x4E, 0xBF, 0xF5, 0x07, 0xF8, 0xCB } +}, + + /* 512-bits, leading set of second byte */ +{ + 64, + { 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x35, 0x7B, 0x42, 0xEA, 0x79, 0xBC, 0x97, 0x86, 0x97, 0x5A, 0x3C, 0x44, 0x70, 0xAA, 0xB2, 0x3E, + 0x62, 0x29, 0x79, 0x7B, 0xAD, 0xBD, 0x54, 0x36, 0x5B, 0x54, 0x96, 0xE5, 0x5D, 0x9D, 0xD7, 0x9F, + 0xE9, 0x62, 0x4F, 0xB4, 0x22, 0x66, 0x93, 0x0A, 0x62, 0x8E, 0xD4, 0xDB, 0x08, 0xF9, 0xDD, 0x35, + 0xEF, 0x1B, 0xE1, 0x04, 0x53, 0xFC, 0x18, 0xF4, 0x2C, 0x7F, 0x5E, 0x1F, 0x9B, 0xAE, 0x55, 0xE0 } +}, + + /* 512-bits, leading set of last byte */ +{ + 64, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, + { 0x8B, 0x39, 0x04, 0xDD, 0x19, 0x81, 0x41, 0x26, 0xFD, 0x02, 0x74, 0xAB, 0x49, 0xC5, 0x97, 0xF6, + 0xD7, 0x75, 0x33, 0x52, 0xA2, 0xDD, 0x91, 0xFD, 0x8F, 0x9F, 0x54, 0x05, 0x4C, 0x54, 0xBF, 0x0F, + 0x06, 0xDB, 0x4F, 0xF7, 0x08, 0xA3, 0xA2, 0x8B, 0xC3, 0x7A, 0x92, 0x1E, 0xEE, 0x11, 0xED, 0x7B, + 0x6A, 0x53, 0x79, 0x32, 0xCC, 0x5E, 0x94, 0xEE, 0x1E, 0xA6, 0x57, 0x60, 0x7E, 0x36, 0xC9, 0xF7 } +}, + +}; + + int i; + unsigned char tmp[64]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + whirlpool_init(&md); + whirlpool_process(&md, (unsigned char *)tests[i].msg, tests[i].len); + whirlpool_done(&md, tmp); + if (XMEMCMP(tmp, tests[i].hash, 64) != 0) { +#if 0 + printf("\nFailed test %d\n", i); + for (i = 0; i < 64; ) { + printf("%02x ", tmp[i]); + if (!(++i & 15)) printf("\n"); + } +#endif + return CRYPT_FAIL_TESTVECTOR; + } + } + return CRYPT_OK; + #endif +} + + +#endif + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/whirl/whirl.c,v $ */ +/* $Revision: 1.10 $ */ +/* $Date: 2007/05/12 14:21:44 $ */ diff --git a/crypto777/hmac/whirltab.c b/crypto777/hmac/whirltab.c new file mode 100755 index 000000000..5f2b701dc --- /dev/null +++ b/crypto777/hmac/whirltab.c @@ -0,0 +1,586 @@ +/** + @file whirltab.c + LTC_WHIRLPOOL tables, Tom St Denis +*/ +#include "tomcrypt_macros.h" +#include + +static const uint64_t sbox0[] = { +CONST64(0x18186018c07830d8), CONST64(0x23238c2305af4626), CONST64(0xc6c63fc67ef991b8), CONST64(0xe8e887e8136fcdfb), +CONST64(0x878726874ca113cb), CONST64(0xb8b8dab8a9626d11), CONST64(0x0101040108050209), CONST64(0x4f4f214f426e9e0d), +CONST64(0x3636d836adee6c9b), CONST64(0xa6a6a2a6590451ff), CONST64(0xd2d26fd2debdb90c), CONST64(0xf5f5f3f5fb06f70e), +CONST64(0x7979f979ef80f296), CONST64(0x6f6fa16f5fcede30), CONST64(0x91917e91fcef3f6d), CONST64(0x52525552aa07a4f8), +CONST64(0x60609d6027fdc047), CONST64(0xbcbccabc89766535), CONST64(0x9b9b569baccd2b37), CONST64(0x8e8e028e048c018a), +CONST64(0xa3a3b6a371155bd2), CONST64(0x0c0c300c603c186c), CONST64(0x7b7bf17bff8af684), CONST64(0x3535d435b5e16a80), +CONST64(0x1d1d741de8693af5), CONST64(0xe0e0a7e05347ddb3), CONST64(0xd7d77bd7f6acb321), CONST64(0xc2c22fc25eed999c), +CONST64(0x2e2eb82e6d965c43), CONST64(0x4b4b314b627a9629), CONST64(0xfefedffea321e15d), CONST64(0x575741578216aed5), +CONST64(0x15155415a8412abd), CONST64(0x7777c1779fb6eee8), CONST64(0x3737dc37a5eb6e92), CONST64(0xe5e5b3e57b56d79e), +CONST64(0x9f9f469f8cd92313), CONST64(0xf0f0e7f0d317fd23), CONST64(0x4a4a354a6a7f9420), CONST64(0xdada4fda9e95a944), +CONST64(0x58587d58fa25b0a2), CONST64(0xc9c903c906ca8fcf), CONST64(0x2929a429558d527c), CONST64(0x0a0a280a5022145a), +CONST64(0xb1b1feb1e14f7f50), CONST64(0xa0a0baa0691a5dc9), CONST64(0x6b6bb16b7fdad614), CONST64(0x85852e855cab17d9), +CONST64(0xbdbdcebd8173673c), CONST64(0x5d5d695dd234ba8f), CONST64(0x1010401080502090), CONST64(0xf4f4f7f4f303f507), +CONST64(0xcbcb0bcb16c08bdd), CONST64(0x3e3ef83eedc67cd3), CONST64(0x0505140528110a2d), CONST64(0x676781671fe6ce78), +CONST64(0xe4e4b7e47353d597), CONST64(0x27279c2725bb4e02), CONST64(0x4141194132588273), CONST64(0x8b8b168b2c9d0ba7), +CONST64(0xa7a7a6a7510153f6), CONST64(0x7d7de97dcf94fab2), CONST64(0x95956e95dcfb3749), CONST64(0xd8d847d88e9fad56), +CONST64(0xfbfbcbfb8b30eb70), CONST64(0xeeee9fee2371c1cd), CONST64(0x7c7ced7cc791f8bb), CONST64(0x6666856617e3cc71), +CONST64(0xdddd53dda68ea77b), CONST64(0x17175c17b84b2eaf), CONST64(0x4747014702468e45), CONST64(0x9e9e429e84dc211a), +CONST64(0xcaca0fca1ec589d4), CONST64(0x2d2db42d75995a58), CONST64(0xbfbfc6bf9179632e), CONST64(0x07071c07381b0e3f), +CONST64(0xadad8ead012347ac), CONST64(0x5a5a755aea2fb4b0), CONST64(0x838336836cb51bef), CONST64(0x3333cc3385ff66b6), +CONST64(0x636391633ff2c65c), CONST64(0x02020802100a0412), CONST64(0xaaaa92aa39384993), CONST64(0x7171d971afa8e2de), +CONST64(0xc8c807c80ecf8dc6), CONST64(0x19196419c87d32d1), CONST64(0x494939497270923b), CONST64(0xd9d943d9869aaf5f), +CONST64(0xf2f2eff2c31df931), CONST64(0xe3e3abe34b48dba8), CONST64(0x5b5b715be22ab6b9), CONST64(0x88881a8834920dbc), +CONST64(0x9a9a529aa4c8293e), CONST64(0x262698262dbe4c0b), CONST64(0x3232c8328dfa64bf), CONST64(0xb0b0fab0e94a7d59), +CONST64(0xe9e983e91b6acff2), CONST64(0x0f0f3c0f78331e77), CONST64(0xd5d573d5e6a6b733), CONST64(0x80803a8074ba1df4), +CONST64(0xbebec2be997c6127), CONST64(0xcdcd13cd26de87eb), CONST64(0x3434d034bde46889), CONST64(0x48483d487a759032), +CONST64(0xffffdbffab24e354), CONST64(0x7a7af57af78ff48d), CONST64(0x90907a90f4ea3d64), CONST64(0x5f5f615fc23ebe9d), +CONST64(0x202080201da0403d), CONST64(0x6868bd6867d5d00f), CONST64(0x1a1a681ad07234ca), CONST64(0xaeae82ae192c41b7), +CONST64(0xb4b4eab4c95e757d), CONST64(0x54544d549a19a8ce), CONST64(0x93937693ece53b7f), CONST64(0x222288220daa442f), +CONST64(0x64648d6407e9c863), CONST64(0xf1f1e3f1db12ff2a), CONST64(0x7373d173bfa2e6cc), CONST64(0x12124812905a2482), +CONST64(0x40401d403a5d807a), CONST64(0x0808200840281048), CONST64(0xc3c32bc356e89b95), CONST64(0xecec97ec337bc5df), +CONST64(0xdbdb4bdb9690ab4d), CONST64(0xa1a1bea1611f5fc0), CONST64(0x8d8d0e8d1c830791), CONST64(0x3d3df43df5c97ac8), +CONST64(0x97976697ccf1335b), CONST64(0x0000000000000000), CONST64(0xcfcf1bcf36d483f9), CONST64(0x2b2bac2b4587566e), +CONST64(0x7676c57697b3ece1), CONST64(0x8282328264b019e6), CONST64(0xd6d67fd6fea9b128), CONST64(0x1b1b6c1bd87736c3), +CONST64(0xb5b5eeb5c15b7774), CONST64(0xafaf86af112943be), CONST64(0x6a6ab56a77dfd41d), CONST64(0x50505d50ba0da0ea), +CONST64(0x45450945124c8a57), CONST64(0xf3f3ebf3cb18fb38), CONST64(0x3030c0309df060ad), CONST64(0xefef9bef2b74c3c4), +CONST64(0x3f3ffc3fe5c37eda), CONST64(0x55554955921caac7), CONST64(0xa2a2b2a2791059db), CONST64(0xeaea8fea0365c9e9), +CONST64(0x656589650fecca6a), CONST64(0xbabad2bab9686903), CONST64(0x2f2fbc2f65935e4a), CONST64(0xc0c027c04ee79d8e), +CONST64(0xdede5fdebe81a160), CONST64(0x1c1c701ce06c38fc), CONST64(0xfdfdd3fdbb2ee746), CONST64(0x4d4d294d52649a1f), +CONST64(0x92927292e4e03976), CONST64(0x7575c9758fbceafa), CONST64(0x06061806301e0c36), CONST64(0x8a8a128a249809ae), +CONST64(0xb2b2f2b2f940794b), CONST64(0xe6e6bfe66359d185), CONST64(0x0e0e380e70361c7e), CONST64(0x1f1f7c1ff8633ee7), +CONST64(0x6262956237f7c455), CONST64(0xd4d477d4eea3b53a), CONST64(0xa8a89aa829324d81), CONST64(0x96966296c4f43152), +CONST64(0xf9f9c3f99b3aef62), CONST64(0xc5c533c566f697a3), CONST64(0x2525942535b14a10), CONST64(0x59597959f220b2ab), +CONST64(0x84842a8454ae15d0), CONST64(0x7272d572b7a7e4c5), CONST64(0x3939e439d5dd72ec), CONST64(0x4c4c2d4c5a619816), +CONST64(0x5e5e655eca3bbc94), CONST64(0x7878fd78e785f09f), CONST64(0x3838e038ddd870e5), CONST64(0x8c8c0a8c14860598), +CONST64(0xd1d163d1c6b2bf17), CONST64(0xa5a5aea5410b57e4), CONST64(0xe2e2afe2434dd9a1), CONST64(0x616199612ff8c24e), +CONST64(0xb3b3f6b3f1457b42), CONST64(0x2121842115a54234), CONST64(0x9c9c4a9c94d62508), CONST64(0x1e1e781ef0663cee), +CONST64(0x4343114322528661), CONST64(0xc7c73bc776fc93b1), CONST64(0xfcfcd7fcb32be54f), CONST64(0x0404100420140824), +CONST64(0x51515951b208a2e3), CONST64(0x99995e99bcc72f25), CONST64(0x6d6da96d4fc4da22), CONST64(0x0d0d340d68391a65), +CONST64(0xfafacffa8335e979), CONST64(0xdfdf5bdfb684a369), CONST64(0x7e7ee57ed79bfca9), CONST64(0x242490243db44819), +CONST64(0x3b3bec3bc5d776fe), CONST64(0xabab96ab313d4b9a), CONST64(0xcece1fce3ed181f0), CONST64(0x1111441188552299), +CONST64(0x8f8f068f0c890383), CONST64(0x4e4e254e4a6b9c04), CONST64(0xb7b7e6b7d1517366), CONST64(0xebeb8beb0b60cbe0), +CONST64(0x3c3cf03cfdcc78c1), CONST64(0x81813e817cbf1ffd), CONST64(0x94946a94d4fe3540), CONST64(0xf7f7fbf7eb0cf31c), +CONST64(0xb9b9deb9a1676f18), CONST64(0x13134c13985f268b), CONST64(0x2c2cb02c7d9c5851), CONST64(0xd3d36bd3d6b8bb05), +CONST64(0xe7e7bbe76b5cd38c), CONST64(0x6e6ea56e57cbdc39), CONST64(0xc4c437c46ef395aa), CONST64(0x03030c03180f061b), +CONST64(0x565645568a13acdc), CONST64(0x44440d441a49885e), CONST64(0x7f7fe17fdf9efea0), CONST64(0xa9a99ea921374f88), +CONST64(0x2a2aa82a4d825467), CONST64(0xbbbbd6bbb16d6b0a), CONST64(0xc1c123c146e29f87), CONST64(0x53535153a202a6f1), +CONST64(0xdcdc57dcae8ba572), CONST64(0x0b0b2c0b58271653), CONST64(0x9d9d4e9d9cd32701), CONST64(0x6c6cad6c47c1d82b), +CONST64(0x3131c43195f562a4), CONST64(0x7474cd7487b9e8f3), CONST64(0xf6f6fff6e309f115), CONST64(0x464605460a438c4c), +CONST64(0xacac8aac092645a5), CONST64(0x89891e893c970fb5), CONST64(0x14145014a04428b4), CONST64(0xe1e1a3e15b42dfba), +CONST64(0x16165816b04e2ca6), CONST64(0x3a3ae83acdd274f7), CONST64(0x6969b9696fd0d206), CONST64(0x09092409482d1241), +CONST64(0x7070dd70a7ade0d7), CONST64(0xb6b6e2b6d954716f), CONST64(0xd0d067d0ceb7bd1e), CONST64(0xeded93ed3b7ec7d6), +CONST64(0xcccc17cc2edb85e2), CONST64(0x424215422a578468), CONST64(0x98985a98b4c22d2c), CONST64(0xa4a4aaa4490e55ed), +CONST64(0x2828a0285d885075), CONST64(0x5c5c6d5cda31b886), CONST64(0xf8f8c7f8933fed6b), CONST64(0x8686228644a411c2) +}; + +#ifdef LTC_SMALL_CODE + +#define SB0(x) sbox0[x] +#define SB1(x) ROR64c(sbox0[x], 8) +#define SB2(x) ROR64c(sbox0[x], 16) +#define SB3(x) ROR64c(sbox0[x], 24) +#define SB4(x) ROR64c(sbox0[x], 32) +#define SB5(x) ROR64c(sbox0[x], 40) +#define SB6(x) ROR64c(sbox0[x], 48) +#define SB7(x) ROR64c(sbox0[x], 56) + +#else + +#define SB0(x) sbox0[x] +#define SB1(x) sbox1[x] +#define SB2(x) sbox2[x] +#define SB3(x) sbox3[x] +#define SB4(x) sbox4[x] +#define SB5(x) sbox5[x] +#define SB6(x) sbox6[x] +#define SB7(x) sbox7[x] + + +static const ulong64 sbox1[] = { +CONST64(0xd818186018c07830), CONST64(0x2623238c2305af46), CONST64(0xb8c6c63fc67ef991), CONST64(0xfbe8e887e8136fcd), +CONST64(0xcb878726874ca113), CONST64(0x11b8b8dab8a9626d), CONST64(0x0901010401080502), CONST64(0x0d4f4f214f426e9e), +CONST64(0x9b3636d836adee6c), CONST64(0xffa6a6a2a6590451), CONST64(0x0cd2d26fd2debdb9), CONST64(0x0ef5f5f3f5fb06f7), +CONST64(0x967979f979ef80f2), CONST64(0x306f6fa16f5fcede), CONST64(0x6d91917e91fcef3f), CONST64(0xf852525552aa07a4), +CONST64(0x4760609d6027fdc0), CONST64(0x35bcbccabc897665), CONST64(0x379b9b569baccd2b), CONST64(0x8a8e8e028e048c01), +CONST64(0xd2a3a3b6a371155b), CONST64(0x6c0c0c300c603c18), CONST64(0x847b7bf17bff8af6), CONST64(0x803535d435b5e16a), +CONST64(0xf51d1d741de8693a), CONST64(0xb3e0e0a7e05347dd), CONST64(0x21d7d77bd7f6acb3), CONST64(0x9cc2c22fc25eed99), +CONST64(0x432e2eb82e6d965c), CONST64(0x294b4b314b627a96), CONST64(0x5dfefedffea321e1), CONST64(0xd5575741578216ae), +CONST64(0xbd15155415a8412a), CONST64(0xe87777c1779fb6ee), CONST64(0x923737dc37a5eb6e), CONST64(0x9ee5e5b3e57b56d7), +CONST64(0x139f9f469f8cd923), CONST64(0x23f0f0e7f0d317fd), CONST64(0x204a4a354a6a7f94), CONST64(0x44dada4fda9e95a9), +CONST64(0xa258587d58fa25b0), CONST64(0xcfc9c903c906ca8f), CONST64(0x7c2929a429558d52), CONST64(0x5a0a0a280a502214), +CONST64(0x50b1b1feb1e14f7f), CONST64(0xc9a0a0baa0691a5d), CONST64(0x146b6bb16b7fdad6), CONST64(0xd985852e855cab17), +CONST64(0x3cbdbdcebd817367), CONST64(0x8f5d5d695dd234ba), CONST64(0x9010104010805020), CONST64(0x07f4f4f7f4f303f5), +CONST64(0xddcbcb0bcb16c08b), CONST64(0xd33e3ef83eedc67c), CONST64(0x2d0505140528110a), CONST64(0x78676781671fe6ce), +CONST64(0x97e4e4b7e47353d5), CONST64(0x0227279c2725bb4e), CONST64(0x7341411941325882), CONST64(0xa78b8b168b2c9d0b), +CONST64(0xf6a7a7a6a7510153), CONST64(0xb27d7de97dcf94fa), CONST64(0x4995956e95dcfb37), CONST64(0x56d8d847d88e9fad), +CONST64(0x70fbfbcbfb8b30eb), CONST64(0xcdeeee9fee2371c1), CONST64(0xbb7c7ced7cc791f8), CONST64(0x716666856617e3cc), +CONST64(0x7bdddd53dda68ea7), CONST64(0xaf17175c17b84b2e), CONST64(0x454747014702468e), CONST64(0x1a9e9e429e84dc21), +CONST64(0xd4caca0fca1ec589), CONST64(0x582d2db42d75995a), CONST64(0x2ebfbfc6bf917963), CONST64(0x3f07071c07381b0e), +CONST64(0xacadad8ead012347), CONST64(0xb05a5a755aea2fb4), CONST64(0xef838336836cb51b), CONST64(0xb63333cc3385ff66), +CONST64(0x5c636391633ff2c6), CONST64(0x1202020802100a04), CONST64(0x93aaaa92aa393849), CONST64(0xde7171d971afa8e2), +CONST64(0xc6c8c807c80ecf8d), CONST64(0xd119196419c87d32), CONST64(0x3b49493949727092), CONST64(0x5fd9d943d9869aaf), +CONST64(0x31f2f2eff2c31df9), CONST64(0xa8e3e3abe34b48db), CONST64(0xb95b5b715be22ab6), CONST64(0xbc88881a8834920d), +CONST64(0x3e9a9a529aa4c829), CONST64(0x0b262698262dbe4c), CONST64(0xbf3232c8328dfa64), CONST64(0x59b0b0fab0e94a7d), +CONST64(0xf2e9e983e91b6acf), CONST64(0x770f0f3c0f78331e), CONST64(0x33d5d573d5e6a6b7), CONST64(0xf480803a8074ba1d), +CONST64(0x27bebec2be997c61), CONST64(0xebcdcd13cd26de87), CONST64(0x893434d034bde468), CONST64(0x3248483d487a7590), +CONST64(0x54ffffdbffab24e3), CONST64(0x8d7a7af57af78ff4), CONST64(0x6490907a90f4ea3d), CONST64(0x9d5f5f615fc23ebe), +CONST64(0x3d202080201da040), CONST64(0x0f6868bd6867d5d0), CONST64(0xca1a1a681ad07234), CONST64(0xb7aeae82ae192c41), +CONST64(0x7db4b4eab4c95e75), CONST64(0xce54544d549a19a8), CONST64(0x7f93937693ece53b), CONST64(0x2f222288220daa44), +CONST64(0x6364648d6407e9c8), CONST64(0x2af1f1e3f1db12ff), CONST64(0xcc7373d173bfa2e6), CONST64(0x8212124812905a24), +CONST64(0x7a40401d403a5d80), CONST64(0x4808082008402810), CONST64(0x95c3c32bc356e89b), CONST64(0xdfecec97ec337bc5), +CONST64(0x4ddbdb4bdb9690ab), CONST64(0xc0a1a1bea1611f5f), CONST64(0x918d8d0e8d1c8307), CONST64(0xc83d3df43df5c97a), +CONST64(0x5b97976697ccf133), CONST64(0x0000000000000000), CONST64(0xf9cfcf1bcf36d483), CONST64(0x6e2b2bac2b458756), +CONST64(0xe17676c57697b3ec), CONST64(0xe68282328264b019), CONST64(0x28d6d67fd6fea9b1), CONST64(0xc31b1b6c1bd87736), +CONST64(0x74b5b5eeb5c15b77), CONST64(0xbeafaf86af112943), CONST64(0x1d6a6ab56a77dfd4), CONST64(0xea50505d50ba0da0), +CONST64(0x5745450945124c8a), CONST64(0x38f3f3ebf3cb18fb), CONST64(0xad3030c0309df060), CONST64(0xc4efef9bef2b74c3), +CONST64(0xda3f3ffc3fe5c37e), CONST64(0xc755554955921caa), CONST64(0xdba2a2b2a2791059), CONST64(0xe9eaea8fea0365c9), +CONST64(0x6a656589650fecca), CONST64(0x03babad2bab96869), CONST64(0x4a2f2fbc2f65935e), CONST64(0x8ec0c027c04ee79d), +CONST64(0x60dede5fdebe81a1), CONST64(0xfc1c1c701ce06c38), CONST64(0x46fdfdd3fdbb2ee7), CONST64(0x1f4d4d294d52649a), +CONST64(0x7692927292e4e039), CONST64(0xfa7575c9758fbcea), CONST64(0x3606061806301e0c), CONST64(0xae8a8a128a249809), +CONST64(0x4bb2b2f2b2f94079), CONST64(0x85e6e6bfe66359d1), CONST64(0x7e0e0e380e70361c), CONST64(0xe71f1f7c1ff8633e), +CONST64(0x556262956237f7c4), CONST64(0x3ad4d477d4eea3b5), CONST64(0x81a8a89aa829324d), CONST64(0x5296966296c4f431), +CONST64(0x62f9f9c3f99b3aef), CONST64(0xa3c5c533c566f697), CONST64(0x102525942535b14a), CONST64(0xab59597959f220b2), +CONST64(0xd084842a8454ae15), CONST64(0xc57272d572b7a7e4), CONST64(0xec3939e439d5dd72), CONST64(0x164c4c2d4c5a6198), +CONST64(0x945e5e655eca3bbc), CONST64(0x9f7878fd78e785f0), CONST64(0xe53838e038ddd870), CONST64(0x988c8c0a8c148605), +CONST64(0x17d1d163d1c6b2bf), CONST64(0xe4a5a5aea5410b57), CONST64(0xa1e2e2afe2434dd9), CONST64(0x4e616199612ff8c2), +CONST64(0x42b3b3f6b3f1457b), CONST64(0x342121842115a542), CONST64(0x089c9c4a9c94d625), CONST64(0xee1e1e781ef0663c), +CONST64(0x6143431143225286), CONST64(0xb1c7c73bc776fc93), CONST64(0x4ffcfcd7fcb32be5), CONST64(0x2404041004201408), +CONST64(0xe351515951b208a2), CONST64(0x2599995e99bcc72f), CONST64(0x226d6da96d4fc4da), CONST64(0x650d0d340d68391a), +CONST64(0x79fafacffa8335e9), CONST64(0x69dfdf5bdfb684a3), CONST64(0xa97e7ee57ed79bfc), CONST64(0x19242490243db448), +CONST64(0xfe3b3bec3bc5d776), CONST64(0x9aabab96ab313d4b), CONST64(0xf0cece1fce3ed181), CONST64(0x9911114411885522), +CONST64(0x838f8f068f0c8903), CONST64(0x044e4e254e4a6b9c), CONST64(0x66b7b7e6b7d15173), CONST64(0xe0ebeb8beb0b60cb), +CONST64(0xc13c3cf03cfdcc78), CONST64(0xfd81813e817cbf1f), CONST64(0x4094946a94d4fe35), CONST64(0x1cf7f7fbf7eb0cf3), +CONST64(0x18b9b9deb9a1676f), CONST64(0x8b13134c13985f26), CONST64(0x512c2cb02c7d9c58), CONST64(0x05d3d36bd3d6b8bb), +CONST64(0x8ce7e7bbe76b5cd3), CONST64(0x396e6ea56e57cbdc), CONST64(0xaac4c437c46ef395), CONST64(0x1b03030c03180f06), +CONST64(0xdc565645568a13ac), CONST64(0x5e44440d441a4988), CONST64(0xa07f7fe17fdf9efe), CONST64(0x88a9a99ea921374f), +CONST64(0x672a2aa82a4d8254), CONST64(0x0abbbbd6bbb16d6b), CONST64(0x87c1c123c146e29f), CONST64(0xf153535153a202a6), +CONST64(0x72dcdc57dcae8ba5), CONST64(0x530b0b2c0b582716), CONST64(0x019d9d4e9d9cd327), CONST64(0x2b6c6cad6c47c1d8), +CONST64(0xa43131c43195f562), CONST64(0xf37474cd7487b9e8), CONST64(0x15f6f6fff6e309f1), CONST64(0x4c464605460a438c), +CONST64(0xa5acac8aac092645), CONST64(0xb589891e893c970f), CONST64(0xb414145014a04428), CONST64(0xbae1e1a3e15b42df), +CONST64(0xa616165816b04e2c), CONST64(0xf73a3ae83acdd274), CONST64(0x066969b9696fd0d2), CONST64(0x4109092409482d12), +CONST64(0xd77070dd70a7ade0), CONST64(0x6fb6b6e2b6d95471), CONST64(0x1ed0d067d0ceb7bd), CONST64(0xd6eded93ed3b7ec7), +CONST64(0xe2cccc17cc2edb85), CONST64(0x68424215422a5784), CONST64(0x2c98985a98b4c22d), CONST64(0xeda4a4aaa4490e55), +CONST64(0x752828a0285d8850), CONST64(0x865c5c6d5cda31b8), CONST64(0x6bf8f8c7f8933fed), CONST64(0xc28686228644a411) +}; + +static const ulong64 sbox2[] = { +CONST64(0x30d818186018c078), CONST64(0x462623238c2305af), CONST64(0x91b8c6c63fc67ef9), CONST64(0xcdfbe8e887e8136f), +CONST64(0x13cb878726874ca1), CONST64(0x6d11b8b8dab8a962), CONST64(0x0209010104010805), CONST64(0x9e0d4f4f214f426e), +CONST64(0x6c9b3636d836adee), CONST64(0x51ffa6a6a2a65904), CONST64(0xb90cd2d26fd2debd), CONST64(0xf70ef5f5f3f5fb06), +CONST64(0xf2967979f979ef80), CONST64(0xde306f6fa16f5fce), CONST64(0x3f6d91917e91fcef), CONST64(0xa4f852525552aa07), +CONST64(0xc04760609d6027fd), CONST64(0x6535bcbccabc8976), CONST64(0x2b379b9b569baccd), CONST64(0x018a8e8e028e048c), +CONST64(0x5bd2a3a3b6a37115), CONST64(0x186c0c0c300c603c), CONST64(0xf6847b7bf17bff8a), CONST64(0x6a803535d435b5e1), +CONST64(0x3af51d1d741de869), CONST64(0xddb3e0e0a7e05347), CONST64(0xb321d7d77bd7f6ac), CONST64(0x999cc2c22fc25eed), +CONST64(0x5c432e2eb82e6d96), CONST64(0x96294b4b314b627a), CONST64(0xe15dfefedffea321), CONST64(0xaed5575741578216), +CONST64(0x2abd15155415a841), CONST64(0xeee87777c1779fb6), CONST64(0x6e923737dc37a5eb), CONST64(0xd79ee5e5b3e57b56), +CONST64(0x23139f9f469f8cd9), CONST64(0xfd23f0f0e7f0d317), CONST64(0x94204a4a354a6a7f), CONST64(0xa944dada4fda9e95), +CONST64(0xb0a258587d58fa25), CONST64(0x8fcfc9c903c906ca), CONST64(0x527c2929a429558d), CONST64(0x145a0a0a280a5022), +CONST64(0x7f50b1b1feb1e14f), CONST64(0x5dc9a0a0baa0691a), CONST64(0xd6146b6bb16b7fda), CONST64(0x17d985852e855cab), +CONST64(0x673cbdbdcebd8173), CONST64(0xba8f5d5d695dd234), CONST64(0x2090101040108050), CONST64(0xf507f4f4f7f4f303), +CONST64(0x8bddcbcb0bcb16c0), CONST64(0x7cd33e3ef83eedc6), CONST64(0x0a2d050514052811), CONST64(0xce78676781671fe6), +CONST64(0xd597e4e4b7e47353), CONST64(0x4e0227279c2725bb), CONST64(0x8273414119413258), CONST64(0x0ba78b8b168b2c9d), +CONST64(0x53f6a7a7a6a75101), CONST64(0xfab27d7de97dcf94), CONST64(0x374995956e95dcfb), CONST64(0xad56d8d847d88e9f), +CONST64(0xeb70fbfbcbfb8b30), CONST64(0xc1cdeeee9fee2371), CONST64(0xf8bb7c7ced7cc791), CONST64(0xcc716666856617e3), +CONST64(0xa77bdddd53dda68e), CONST64(0x2eaf17175c17b84b), CONST64(0x8e45474701470246), CONST64(0x211a9e9e429e84dc), +CONST64(0x89d4caca0fca1ec5), CONST64(0x5a582d2db42d7599), CONST64(0x632ebfbfc6bf9179), CONST64(0x0e3f07071c07381b), +CONST64(0x47acadad8ead0123), CONST64(0xb4b05a5a755aea2f), CONST64(0x1bef838336836cb5), CONST64(0x66b63333cc3385ff), +CONST64(0xc65c636391633ff2), CONST64(0x041202020802100a), CONST64(0x4993aaaa92aa3938), CONST64(0xe2de7171d971afa8), +CONST64(0x8dc6c8c807c80ecf), CONST64(0x32d119196419c87d), CONST64(0x923b494939497270), CONST64(0xaf5fd9d943d9869a), +CONST64(0xf931f2f2eff2c31d), CONST64(0xdba8e3e3abe34b48), CONST64(0xb6b95b5b715be22a), CONST64(0x0dbc88881a883492), +CONST64(0x293e9a9a529aa4c8), CONST64(0x4c0b262698262dbe), CONST64(0x64bf3232c8328dfa), CONST64(0x7d59b0b0fab0e94a), +CONST64(0xcff2e9e983e91b6a), CONST64(0x1e770f0f3c0f7833), CONST64(0xb733d5d573d5e6a6), CONST64(0x1df480803a8074ba), +CONST64(0x6127bebec2be997c), CONST64(0x87ebcdcd13cd26de), CONST64(0x68893434d034bde4), CONST64(0x903248483d487a75), +CONST64(0xe354ffffdbffab24), CONST64(0xf48d7a7af57af78f), CONST64(0x3d6490907a90f4ea), CONST64(0xbe9d5f5f615fc23e), +CONST64(0x403d202080201da0), CONST64(0xd00f6868bd6867d5), CONST64(0x34ca1a1a681ad072), CONST64(0x41b7aeae82ae192c), +CONST64(0x757db4b4eab4c95e), CONST64(0xa8ce54544d549a19), CONST64(0x3b7f93937693ece5), CONST64(0x442f222288220daa), +CONST64(0xc86364648d6407e9), CONST64(0xff2af1f1e3f1db12), CONST64(0xe6cc7373d173bfa2), CONST64(0x248212124812905a), +CONST64(0x807a40401d403a5d), CONST64(0x1048080820084028), CONST64(0x9b95c3c32bc356e8), CONST64(0xc5dfecec97ec337b), +CONST64(0xab4ddbdb4bdb9690), CONST64(0x5fc0a1a1bea1611f), CONST64(0x07918d8d0e8d1c83), CONST64(0x7ac83d3df43df5c9), +CONST64(0x335b97976697ccf1), CONST64(0x0000000000000000), CONST64(0x83f9cfcf1bcf36d4), CONST64(0x566e2b2bac2b4587), +CONST64(0xece17676c57697b3), CONST64(0x19e68282328264b0), CONST64(0xb128d6d67fd6fea9), CONST64(0x36c31b1b6c1bd877), +CONST64(0x7774b5b5eeb5c15b), CONST64(0x43beafaf86af1129), CONST64(0xd41d6a6ab56a77df), CONST64(0xa0ea50505d50ba0d), +CONST64(0x8a5745450945124c), CONST64(0xfb38f3f3ebf3cb18), CONST64(0x60ad3030c0309df0), CONST64(0xc3c4efef9bef2b74), +CONST64(0x7eda3f3ffc3fe5c3), CONST64(0xaac755554955921c), CONST64(0x59dba2a2b2a27910), CONST64(0xc9e9eaea8fea0365), +CONST64(0xca6a656589650fec), CONST64(0x6903babad2bab968), CONST64(0x5e4a2f2fbc2f6593), CONST64(0x9d8ec0c027c04ee7), +CONST64(0xa160dede5fdebe81), CONST64(0x38fc1c1c701ce06c), CONST64(0xe746fdfdd3fdbb2e), CONST64(0x9a1f4d4d294d5264), +CONST64(0x397692927292e4e0), CONST64(0xeafa7575c9758fbc), CONST64(0x0c3606061806301e), CONST64(0x09ae8a8a128a2498), +CONST64(0x794bb2b2f2b2f940), CONST64(0xd185e6e6bfe66359), CONST64(0x1c7e0e0e380e7036), CONST64(0x3ee71f1f7c1ff863), +CONST64(0xc4556262956237f7), CONST64(0xb53ad4d477d4eea3), CONST64(0x4d81a8a89aa82932), CONST64(0x315296966296c4f4), +CONST64(0xef62f9f9c3f99b3a), CONST64(0x97a3c5c533c566f6), CONST64(0x4a102525942535b1), CONST64(0xb2ab59597959f220), +CONST64(0x15d084842a8454ae), CONST64(0xe4c57272d572b7a7), CONST64(0x72ec3939e439d5dd), CONST64(0x98164c4c2d4c5a61), +CONST64(0xbc945e5e655eca3b), CONST64(0xf09f7878fd78e785), CONST64(0x70e53838e038ddd8), CONST64(0x05988c8c0a8c1486), +CONST64(0xbf17d1d163d1c6b2), CONST64(0x57e4a5a5aea5410b), CONST64(0xd9a1e2e2afe2434d), CONST64(0xc24e616199612ff8), +CONST64(0x7b42b3b3f6b3f145), CONST64(0x42342121842115a5), CONST64(0x25089c9c4a9c94d6), CONST64(0x3cee1e1e781ef066), +CONST64(0x8661434311432252), CONST64(0x93b1c7c73bc776fc), CONST64(0xe54ffcfcd7fcb32b), CONST64(0x0824040410042014), +CONST64(0xa2e351515951b208), CONST64(0x2f2599995e99bcc7), CONST64(0xda226d6da96d4fc4), CONST64(0x1a650d0d340d6839), +CONST64(0xe979fafacffa8335), CONST64(0xa369dfdf5bdfb684), CONST64(0xfca97e7ee57ed79b), CONST64(0x4819242490243db4), +CONST64(0x76fe3b3bec3bc5d7), CONST64(0x4b9aabab96ab313d), CONST64(0x81f0cece1fce3ed1), CONST64(0x2299111144118855), +CONST64(0x03838f8f068f0c89), CONST64(0x9c044e4e254e4a6b), CONST64(0x7366b7b7e6b7d151), CONST64(0xcbe0ebeb8beb0b60), +CONST64(0x78c13c3cf03cfdcc), CONST64(0x1ffd81813e817cbf), CONST64(0x354094946a94d4fe), CONST64(0xf31cf7f7fbf7eb0c), +CONST64(0x6f18b9b9deb9a167), CONST64(0x268b13134c13985f), CONST64(0x58512c2cb02c7d9c), CONST64(0xbb05d3d36bd3d6b8), +CONST64(0xd38ce7e7bbe76b5c), CONST64(0xdc396e6ea56e57cb), CONST64(0x95aac4c437c46ef3), CONST64(0x061b03030c03180f), +CONST64(0xacdc565645568a13), CONST64(0x885e44440d441a49), CONST64(0xfea07f7fe17fdf9e), CONST64(0x4f88a9a99ea92137), +CONST64(0x54672a2aa82a4d82), CONST64(0x6b0abbbbd6bbb16d), CONST64(0x9f87c1c123c146e2), CONST64(0xa6f153535153a202), +CONST64(0xa572dcdc57dcae8b), CONST64(0x16530b0b2c0b5827), CONST64(0x27019d9d4e9d9cd3), CONST64(0xd82b6c6cad6c47c1), +CONST64(0x62a43131c43195f5), CONST64(0xe8f37474cd7487b9), CONST64(0xf115f6f6fff6e309), CONST64(0x8c4c464605460a43), +CONST64(0x45a5acac8aac0926), CONST64(0x0fb589891e893c97), CONST64(0x28b414145014a044), CONST64(0xdfbae1e1a3e15b42), +CONST64(0x2ca616165816b04e), CONST64(0x74f73a3ae83acdd2), CONST64(0xd2066969b9696fd0), CONST64(0x124109092409482d), +CONST64(0xe0d77070dd70a7ad), CONST64(0x716fb6b6e2b6d954), CONST64(0xbd1ed0d067d0ceb7), CONST64(0xc7d6eded93ed3b7e), +CONST64(0x85e2cccc17cc2edb), CONST64(0x8468424215422a57), CONST64(0x2d2c98985a98b4c2), CONST64(0x55eda4a4aaa4490e), +CONST64(0x50752828a0285d88), CONST64(0xb8865c5c6d5cda31), CONST64(0xed6bf8f8c7f8933f), CONST64(0x11c28686228644a4) +}; + +static const ulong64 sbox3[] = { +CONST64(0x7830d818186018c0), CONST64(0xaf462623238c2305), CONST64(0xf991b8c6c63fc67e), CONST64(0x6fcdfbe8e887e813), +CONST64(0xa113cb878726874c), CONST64(0x626d11b8b8dab8a9), CONST64(0x0502090101040108), CONST64(0x6e9e0d4f4f214f42), +CONST64(0xee6c9b3636d836ad), CONST64(0x0451ffa6a6a2a659), CONST64(0xbdb90cd2d26fd2de), CONST64(0x06f70ef5f5f3f5fb), +CONST64(0x80f2967979f979ef), CONST64(0xcede306f6fa16f5f), CONST64(0xef3f6d91917e91fc), CONST64(0x07a4f852525552aa), +CONST64(0xfdc04760609d6027), CONST64(0x766535bcbccabc89), CONST64(0xcd2b379b9b569bac), CONST64(0x8c018a8e8e028e04), +CONST64(0x155bd2a3a3b6a371), CONST64(0x3c186c0c0c300c60), CONST64(0x8af6847b7bf17bff), CONST64(0xe16a803535d435b5), +CONST64(0x693af51d1d741de8), CONST64(0x47ddb3e0e0a7e053), CONST64(0xacb321d7d77bd7f6), CONST64(0xed999cc2c22fc25e), +CONST64(0x965c432e2eb82e6d), CONST64(0x7a96294b4b314b62), CONST64(0x21e15dfefedffea3), CONST64(0x16aed55757415782), +CONST64(0x412abd15155415a8), CONST64(0xb6eee87777c1779f), CONST64(0xeb6e923737dc37a5), CONST64(0x56d79ee5e5b3e57b), +CONST64(0xd923139f9f469f8c), CONST64(0x17fd23f0f0e7f0d3), CONST64(0x7f94204a4a354a6a), CONST64(0x95a944dada4fda9e), +CONST64(0x25b0a258587d58fa), CONST64(0xca8fcfc9c903c906), CONST64(0x8d527c2929a42955), CONST64(0x22145a0a0a280a50), +CONST64(0x4f7f50b1b1feb1e1), CONST64(0x1a5dc9a0a0baa069), CONST64(0xdad6146b6bb16b7f), CONST64(0xab17d985852e855c), +CONST64(0x73673cbdbdcebd81), CONST64(0x34ba8f5d5d695dd2), CONST64(0x5020901010401080), CONST64(0x03f507f4f4f7f4f3), +CONST64(0xc08bddcbcb0bcb16), CONST64(0xc67cd33e3ef83eed), CONST64(0x110a2d0505140528), CONST64(0xe6ce78676781671f), +CONST64(0x53d597e4e4b7e473), CONST64(0xbb4e0227279c2725), CONST64(0x5882734141194132), CONST64(0x9d0ba78b8b168b2c), +CONST64(0x0153f6a7a7a6a751), CONST64(0x94fab27d7de97dcf), CONST64(0xfb374995956e95dc), CONST64(0x9fad56d8d847d88e), +CONST64(0x30eb70fbfbcbfb8b), CONST64(0x71c1cdeeee9fee23), CONST64(0x91f8bb7c7ced7cc7), CONST64(0xe3cc716666856617), +CONST64(0x8ea77bdddd53dda6), CONST64(0x4b2eaf17175c17b8), CONST64(0x468e454747014702), CONST64(0xdc211a9e9e429e84), +CONST64(0xc589d4caca0fca1e), CONST64(0x995a582d2db42d75), CONST64(0x79632ebfbfc6bf91), CONST64(0x1b0e3f07071c0738), +CONST64(0x2347acadad8ead01), CONST64(0x2fb4b05a5a755aea), CONST64(0xb51bef838336836c), CONST64(0xff66b63333cc3385), +CONST64(0xf2c65c636391633f), CONST64(0x0a04120202080210), CONST64(0x384993aaaa92aa39), CONST64(0xa8e2de7171d971af), +CONST64(0xcf8dc6c8c807c80e), CONST64(0x7d32d119196419c8), CONST64(0x70923b4949394972), CONST64(0x9aaf5fd9d943d986), +CONST64(0x1df931f2f2eff2c3), CONST64(0x48dba8e3e3abe34b), CONST64(0x2ab6b95b5b715be2), CONST64(0x920dbc88881a8834), +CONST64(0xc8293e9a9a529aa4), CONST64(0xbe4c0b262698262d), CONST64(0xfa64bf3232c8328d), CONST64(0x4a7d59b0b0fab0e9), +CONST64(0x6acff2e9e983e91b), CONST64(0x331e770f0f3c0f78), CONST64(0xa6b733d5d573d5e6), CONST64(0xba1df480803a8074), +CONST64(0x7c6127bebec2be99), CONST64(0xde87ebcdcd13cd26), CONST64(0xe468893434d034bd), CONST64(0x75903248483d487a), +CONST64(0x24e354ffffdbffab), CONST64(0x8ff48d7a7af57af7), CONST64(0xea3d6490907a90f4), CONST64(0x3ebe9d5f5f615fc2), +CONST64(0xa0403d202080201d), CONST64(0xd5d00f6868bd6867), CONST64(0x7234ca1a1a681ad0), CONST64(0x2c41b7aeae82ae19), +CONST64(0x5e757db4b4eab4c9), CONST64(0x19a8ce54544d549a), CONST64(0xe53b7f93937693ec), CONST64(0xaa442f222288220d), +CONST64(0xe9c86364648d6407), CONST64(0x12ff2af1f1e3f1db), CONST64(0xa2e6cc7373d173bf), CONST64(0x5a24821212481290), +CONST64(0x5d807a40401d403a), CONST64(0x2810480808200840), CONST64(0xe89b95c3c32bc356), CONST64(0x7bc5dfecec97ec33), +CONST64(0x90ab4ddbdb4bdb96), CONST64(0x1f5fc0a1a1bea161), CONST64(0x8307918d8d0e8d1c), CONST64(0xc97ac83d3df43df5), +CONST64(0xf1335b97976697cc), CONST64(0x0000000000000000), CONST64(0xd483f9cfcf1bcf36), CONST64(0x87566e2b2bac2b45), +CONST64(0xb3ece17676c57697), CONST64(0xb019e68282328264), CONST64(0xa9b128d6d67fd6fe), CONST64(0x7736c31b1b6c1bd8), +CONST64(0x5b7774b5b5eeb5c1), CONST64(0x2943beafaf86af11), CONST64(0xdfd41d6a6ab56a77), CONST64(0x0da0ea50505d50ba), +CONST64(0x4c8a574545094512), CONST64(0x18fb38f3f3ebf3cb), CONST64(0xf060ad3030c0309d), CONST64(0x74c3c4efef9bef2b), +CONST64(0xc37eda3f3ffc3fe5), CONST64(0x1caac75555495592), CONST64(0x1059dba2a2b2a279), CONST64(0x65c9e9eaea8fea03), +CONST64(0xecca6a656589650f), CONST64(0x686903babad2bab9), CONST64(0x935e4a2f2fbc2f65), CONST64(0xe79d8ec0c027c04e), +CONST64(0x81a160dede5fdebe), CONST64(0x6c38fc1c1c701ce0), CONST64(0x2ee746fdfdd3fdbb), CONST64(0x649a1f4d4d294d52), +CONST64(0xe0397692927292e4), CONST64(0xbceafa7575c9758f), CONST64(0x1e0c360606180630), CONST64(0x9809ae8a8a128a24), +CONST64(0x40794bb2b2f2b2f9), CONST64(0x59d185e6e6bfe663), CONST64(0x361c7e0e0e380e70), CONST64(0x633ee71f1f7c1ff8), +CONST64(0xf7c4556262956237), CONST64(0xa3b53ad4d477d4ee), CONST64(0x324d81a8a89aa829), CONST64(0xf4315296966296c4), +CONST64(0x3aef62f9f9c3f99b), CONST64(0xf697a3c5c533c566), CONST64(0xb14a102525942535), CONST64(0x20b2ab59597959f2), +CONST64(0xae15d084842a8454), CONST64(0xa7e4c57272d572b7), CONST64(0xdd72ec3939e439d5), CONST64(0x6198164c4c2d4c5a), +CONST64(0x3bbc945e5e655eca), CONST64(0x85f09f7878fd78e7), CONST64(0xd870e53838e038dd), CONST64(0x8605988c8c0a8c14), +CONST64(0xb2bf17d1d163d1c6), CONST64(0x0b57e4a5a5aea541), CONST64(0x4dd9a1e2e2afe243), CONST64(0xf8c24e616199612f), +CONST64(0x457b42b3b3f6b3f1), CONST64(0xa542342121842115), CONST64(0xd625089c9c4a9c94), CONST64(0x663cee1e1e781ef0), +CONST64(0x5286614343114322), CONST64(0xfc93b1c7c73bc776), CONST64(0x2be54ffcfcd7fcb3), CONST64(0x1408240404100420), +CONST64(0x08a2e351515951b2), CONST64(0xc72f2599995e99bc), CONST64(0xc4da226d6da96d4f), CONST64(0x391a650d0d340d68), +CONST64(0x35e979fafacffa83), CONST64(0x84a369dfdf5bdfb6), CONST64(0x9bfca97e7ee57ed7), CONST64(0xb44819242490243d), +CONST64(0xd776fe3b3bec3bc5), CONST64(0x3d4b9aabab96ab31), CONST64(0xd181f0cece1fce3e), CONST64(0x5522991111441188), +CONST64(0x8903838f8f068f0c), CONST64(0x6b9c044e4e254e4a), CONST64(0x517366b7b7e6b7d1), CONST64(0x60cbe0ebeb8beb0b), +CONST64(0xcc78c13c3cf03cfd), CONST64(0xbf1ffd81813e817c), CONST64(0xfe354094946a94d4), CONST64(0x0cf31cf7f7fbf7eb), +CONST64(0x676f18b9b9deb9a1), CONST64(0x5f268b13134c1398), CONST64(0x9c58512c2cb02c7d), CONST64(0xb8bb05d3d36bd3d6), +CONST64(0x5cd38ce7e7bbe76b), CONST64(0xcbdc396e6ea56e57), CONST64(0xf395aac4c437c46e), CONST64(0x0f061b03030c0318), +CONST64(0x13acdc565645568a), CONST64(0x49885e44440d441a), CONST64(0x9efea07f7fe17fdf), CONST64(0x374f88a9a99ea921), +CONST64(0x8254672a2aa82a4d), CONST64(0x6d6b0abbbbd6bbb1), CONST64(0xe29f87c1c123c146), CONST64(0x02a6f153535153a2), +CONST64(0x8ba572dcdc57dcae), CONST64(0x2716530b0b2c0b58), CONST64(0xd327019d9d4e9d9c), CONST64(0xc1d82b6c6cad6c47), +CONST64(0xf562a43131c43195), CONST64(0xb9e8f37474cd7487), CONST64(0x09f115f6f6fff6e3), CONST64(0x438c4c464605460a), +CONST64(0x2645a5acac8aac09), CONST64(0x970fb589891e893c), CONST64(0x4428b414145014a0), CONST64(0x42dfbae1e1a3e15b), +CONST64(0x4e2ca616165816b0), CONST64(0xd274f73a3ae83acd), CONST64(0xd0d2066969b9696f), CONST64(0x2d12410909240948), +CONST64(0xade0d77070dd70a7), CONST64(0x54716fb6b6e2b6d9), CONST64(0xb7bd1ed0d067d0ce), CONST64(0x7ec7d6eded93ed3b), +CONST64(0xdb85e2cccc17cc2e), CONST64(0x578468424215422a), CONST64(0xc22d2c98985a98b4), CONST64(0x0e55eda4a4aaa449), +CONST64(0x8850752828a0285d), CONST64(0x31b8865c5c6d5cda), CONST64(0x3fed6bf8f8c7f893), CONST64(0xa411c28686228644) +}; + +static const ulong64 sbox4[] = { +CONST64(0xc07830d818186018), CONST64(0x05af462623238c23), CONST64(0x7ef991b8c6c63fc6), CONST64(0x136fcdfbe8e887e8), +CONST64(0x4ca113cb87872687), CONST64(0xa9626d11b8b8dab8), CONST64(0x0805020901010401), CONST64(0x426e9e0d4f4f214f), +CONST64(0xadee6c9b3636d836), CONST64(0x590451ffa6a6a2a6), CONST64(0xdebdb90cd2d26fd2), CONST64(0xfb06f70ef5f5f3f5), +CONST64(0xef80f2967979f979), CONST64(0x5fcede306f6fa16f), CONST64(0xfcef3f6d91917e91), CONST64(0xaa07a4f852525552), +CONST64(0x27fdc04760609d60), CONST64(0x89766535bcbccabc), CONST64(0xaccd2b379b9b569b), CONST64(0x048c018a8e8e028e), +CONST64(0x71155bd2a3a3b6a3), CONST64(0x603c186c0c0c300c), CONST64(0xff8af6847b7bf17b), CONST64(0xb5e16a803535d435), +CONST64(0xe8693af51d1d741d), CONST64(0x5347ddb3e0e0a7e0), CONST64(0xf6acb321d7d77bd7), CONST64(0x5eed999cc2c22fc2), +CONST64(0x6d965c432e2eb82e), CONST64(0x627a96294b4b314b), CONST64(0xa321e15dfefedffe), CONST64(0x8216aed557574157), +CONST64(0xa8412abd15155415), CONST64(0x9fb6eee87777c177), CONST64(0xa5eb6e923737dc37), CONST64(0x7b56d79ee5e5b3e5), +CONST64(0x8cd923139f9f469f), CONST64(0xd317fd23f0f0e7f0), CONST64(0x6a7f94204a4a354a), CONST64(0x9e95a944dada4fda), +CONST64(0xfa25b0a258587d58), CONST64(0x06ca8fcfc9c903c9), CONST64(0x558d527c2929a429), CONST64(0x5022145a0a0a280a), +CONST64(0xe14f7f50b1b1feb1), CONST64(0x691a5dc9a0a0baa0), CONST64(0x7fdad6146b6bb16b), CONST64(0x5cab17d985852e85), +CONST64(0x8173673cbdbdcebd), CONST64(0xd234ba8f5d5d695d), CONST64(0x8050209010104010), CONST64(0xf303f507f4f4f7f4), +CONST64(0x16c08bddcbcb0bcb), CONST64(0xedc67cd33e3ef83e), CONST64(0x28110a2d05051405), CONST64(0x1fe6ce7867678167), +CONST64(0x7353d597e4e4b7e4), CONST64(0x25bb4e0227279c27), CONST64(0x3258827341411941), CONST64(0x2c9d0ba78b8b168b), +CONST64(0x510153f6a7a7a6a7), CONST64(0xcf94fab27d7de97d), CONST64(0xdcfb374995956e95), CONST64(0x8e9fad56d8d847d8), +CONST64(0x8b30eb70fbfbcbfb), CONST64(0x2371c1cdeeee9fee), CONST64(0xc791f8bb7c7ced7c), CONST64(0x17e3cc7166668566), +CONST64(0xa68ea77bdddd53dd), CONST64(0xb84b2eaf17175c17), CONST64(0x02468e4547470147), CONST64(0x84dc211a9e9e429e), +CONST64(0x1ec589d4caca0fca), CONST64(0x75995a582d2db42d), CONST64(0x9179632ebfbfc6bf), CONST64(0x381b0e3f07071c07), +CONST64(0x012347acadad8ead), CONST64(0xea2fb4b05a5a755a), CONST64(0x6cb51bef83833683), CONST64(0x85ff66b63333cc33), +CONST64(0x3ff2c65c63639163), CONST64(0x100a041202020802), CONST64(0x39384993aaaa92aa), CONST64(0xafa8e2de7171d971), +CONST64(0x0ecf8dc6c8c807c8), CONST64(0xc87d32d119196419), CONST64(0x7270923b49493949), CONST64(0x869aaf5fd9d943d9), +CONST64(0xc31df931f2f2eff2), CONST64(0x4b48dba8e3e3abe3), CONST64(0xe22ab6b95b5b715b), CONST64(0x34920dbc88881a88), +CONST64(0xa4c8293e9a9a529a), CONST64(0x2dbe4c0b26269826), CONST64(0x8dfa64bf3232c832), CONST64(0xe94a7d59b0b0fab0), +CONST64(0x1b6acff2e9e983e9), CONST64(0x78331e770f0f3c0f), CONST64(0xe6a6b733d5d573d5), CONST64(0x74ba1df480803a80), +CONST64(0x997c6127bebec2be), CONST64(0x26de87ebcdcd13cd), CONST64(0xbde468893434d034), CONST64(0x7a75903248483d48), +CONST64(0xab24e354ffffdbff), CONST64(0xf78ff48d7a7af57a), CONST64(0xf4ea3d6490907a90), CONST64(0xc23ebe9d5f5f615f), +CONST64(0x1da0403d20208020), CONST64(0x67d5d00f6868bd68), CONST64(0xd07234ca1a1a681a), CONST64(0x192c41b7aeae82ae), +CONST64(0xc95e757db4b4eab4), CONST64(0x9a19a8ce54544d54), CONST64(0xece53b7f93937693), CONST64(0x0daa442f22228822), +CONST64(0x07e9c86364648d64), CONST64(0xdb12ff2af1f1e3f1), CONST64(0xbfa2e6cc7373d173), CONST64(0x905a248212124812), +CONST64(0x3a5d807a40401d40), CONST64(0x4028104808082008), CONST64(0x56e89b95c3c32bc3), CONST64(0x337bc5dfecec97ec), +CONST64(0x9690ab4ddbdb4bdb), CONST64(0x611f5fc0a1a1bea1), CONST64(0x1c8307918d8d0e8d), CONST64(0xf5c97ac83d3df43d), +CONST64(0xccf1335b97976697), CONST64(0x0000000000000000), CONST64(0x36d483f9cfcf1bcf), CONST64(0x4587566e2b2bac2b), +CONST64(0x97b3ece17676c576), CONST64(0x64b019e682823282), CONST64(0xfea9b128d6d67fd6), CONST64(0xd87736c31b1b6c1b), +CONST64(0xc15b7774b5b5eeb5), CONST64(0x112943beafaf86af), CONST64(0x77dfd41d6a6ab56a), CONST64(0xba0da0ea50505d50), +CONST64(0x124c8a5745450945), CONST64(0xcb18fb38f3f3ebf3), CONST64(0x9df060ad3030c030), CONST64(0x2b74c3c4efef9bef), +CONST64(0xe5c37eda3f3ffc3f), CONST64(0x921caac755554955), CONST64(0x791059dba2a2b2a2), CONST64(0x0365c9e9eaea8fea), +CONST64(0x0fecca6a65658965), CONST64(0xb9686903babad2ba), CONST64(0x65935e4a2f2fbc2f), CONST64(0x4ee79d8ec0c027c0), +CONST64(0xbe81a160dede5fde), CONST64(0xe06c38fc1c1c701c), CONST64(0xbb2ee746fdfdd3fd), CONST64(0x52649a1f4d4d294d), +CONST64(0xe4e0397692927292), CONST64(0x8fbceafa7575c975), CONST64(0x301e0c3606061806), CONST64(0x249809ae8a8a128a), +CONST64(0xf940794bb2b2f2b2), CONST64(0x6359d185e6e6bfe6), CONST64(0x70361c7e0e0e380e), CONST64(0xf8633ee71f1f7c1f), +CONST64(0x37f7c45562629562), CONST64(0xeea3b53ad4d477d4), CONST64(0x29324d81a8a89aa8), CONST64(0xc4f4315296966296), +CONST64(0x9b3aef62f9f9c3f9), CONST64(0x66f697a3c5c533c5), CONST64(0x35b14a1025259425), CONST64(0xf220b2ab59597959), +CONST64(0x54ae15d084842a84), CONST64(0xb7a7e4c57272d572), CONST64(0xd5dd72ec3939e439), CONST64(0x5a6198164c4c2d4c), +CONST64(0xca3bbc945e5e655e), CONST64(0xe785f09f7878fd78), CONST64(0xddd870e53838e038), CONST64(0x148605988c8c0a8c), +CONST64(0xc6b2bf17d1d163d1), CONST64(0x410b57e4a5a5aea5), CONST64(0x434dd9a1e2e2afe2), CONST64(0x2ff8c24e61619961), +CONST64(0xf1457b42b3b3f6b3), CONST64(0x15a5423421218421), CONST64(0x94d625089c9c4a9c), CONST64(0xf0663cee1e1e781e), +CONST64(0x2252866143431143), CONST64(0x76fc93b1c7c73bc7), CONST64(0xb32be54ffcfcd7fc), CONST64(0x2014082404041004), +CONST64(0xb208a2e351515951), CONST64(0xbcc72f2599995e99), CONST64(0x4fc4da226d6da96d), CONST64(0x68391a650d0d340d), +CONST64(0x8335e979fafacffa), CONST64(0xb684a369dfdf5bdf), CONST64(0xd79bfca97e7ee57e), CONST64(0x3db4481924249024), +CONST64(0xc5d776fe3b3bec3b), CONST64(0x313d4b9aabab96ab), CONST64(0x3ed181f0cece1fce), CONST64(0x8855229911114411), +CONST64(0x0c8903838f8f068f), CONST64(0x4a6b9c044e4e254e), CONST64(0xd1517366b7b7e6b7), CONST64(0x0b60cbe0ebeb8beb), +CONST64(0xfdcc78c13c3cf03c), CONST64(0x7cbf1ffd81813e81), CONST64(0xd4fe354094946a94), CONST64(0xeb0cf31cf7f7fbf7), +CONST64(0xa1676f18b9b9deb9), CONST64(0x985f268b13134c13), CONST64(0x7d9c58512c2cb02c), CONST64(0xd6b8bb05d3d36bd3), +CONST64(0x6b5cd38ce7e7bbe7), CONST64(0x57cbdc396e6ea56e), CONST64(0x6ef395aac4c437c4), CONST64(0x180f061b03030c03), +CONST64(0x8a13acdc56564556), CONST64(0x1a49885e44440d44), CONST64(0xdf9efea07f7fe17f), CONST64(0x21374f88a9a99ea9), +CONST64(0x4d8254672a2aa82a), CONST64(0xb16d6b0abbbbd6bb), CONST64(0x46e29f87c1c123c1), CONST64(0xa202a6f153535153), +CONST64(0xae8ba572dcdc57dc), CONST64(0x582716530b0b2c0b), CONST64(0x9cd327019d9d4e9d), CONST64(0x47c1d82b6c6cad6c), +CONST64(0x95f562a43131c431), CONST64(0x87b9e8f37474cd74), CONST64(0xe309f115f6f6fff6), CONST64(0x0a438c4c46460546), +CONST64(0x092645a5acac8aac), CONST64(0x3c970fb589891e89), CONST64(0xa04428b414145014), CONST64(0x5b42dfbae1e1a3e1), +CONST64(0xb04e2ca616165816), CONST64(0xcdd274f73a3ae83a), CONST64(0x6fd0d2066969b969), CONST64(0x482d124109092409), +CONST64(0xa7ade0d77070dd70), CONST64(0xd954716fb6b6e2b6), CONST64(0xceb7bd1ed0d067d0), CONST64(0x3b7ec7d6eded93ed), +CONST64(0x2edb85e2cccc17cc), CONST64(0x2a57846842421542), CONST64(0xb4c22d2c98985a98), CONST64(0x490e55eda4a4aaa4), +CONST64(0x5d8850752828a028), CONST64(0xda31b8865c5c6d5c), CONST64(0x933fed6bf8f8c7f8), CONST64(0x44a411c286862286) +}; + +static const ulong64 sbox5[] = { +CONST64(0x18c07830d8181860), CONST64(0x2305af462623238c), CONST64(0xc67ef991b8c6c63f), CONST64(0xe8136fcdfbe8e887), +CONST64(0x874ca113cb878726), CONST64(0xb8a9626d11b8b8da), CONST64(0x0108050209010104), CONST64(0x4f426e9e0d4f4f21), +CONST64(0x36adee6c9b3636d8), CONST64(0xa6590451ffa6a6a2), CONST64(0xd2debdb90cd2d26f), CONST64(0xf5fb06f70ef5f5f3), +CONST64(0x79ef80f2967979f9), CONST64(0x6f5fcede306f6fa1), CONST64(0x91fcef3f6d91917e), CONST64(0x52aa07a4f8525255), +CONST64(0x6027fdc04760609d), CONST64(0xbc89766535bcbcca), CONST64(0x9baccd2b379b9b56), CONST64(0x8e048c018a8e8e02), +CONST64(0xa371155bd2a3a3b6), CONST64(0x0c603c186c0c0c30), CONST64(0x7bff8af6847b7bf1), CONST64(0x35b5e16a803535d4), +CONST64(0x1de8693af51d1d74), CONST64(0xe05347ddb3e0e0a7), CONST64(0xd7f6acb321d7d77b), CONST64(0xc25eed999cc2c22f), +CONST64(0x2e6d965c432e2eb8), CONST64(0x4b627a96294b4b31), CONST64(0xfea321e15dfefedf), CONST64(0x578216aed5575741), +CONST64(0x15a8412abd151554), CONST64(0x779fb6eee87777c1), CONST64(0x37a5eb6e923737dc), CONST64(0xe57b56d79ee5e5b3), +CONST64(0x9f8cd923139f9f46), CONST64(0xf0d317fd23f0f0e7), CONST64(0x4a6a7f94204a4a35), CONST64(0xda9e95a944dada4f), +CONST64(0x58fa25b0a258587d), CONST64(0xc906ca8fcfc9c903), CONST64(0x29558d527c2929a4), CONST64(0x0a5022145a0a0a28), +CONST64(0xb1e14f7f50b1b1fe), CONST64(0xa0691a5dc9a0a0ba), CONST64(0x6b7fdad6146b6bb1), CONST64(0x855cab17d985852e), +CONST64(0xbd8173673cbdbdce), CONST64(0x5dd234ba8f5d5d69), CONST64(0x1080502090101040), CONST64(0xf4f303f507f4f4f7), +CONST64(0xcb16c08bddcbcb0b), CONST64(0x3eedc67cd33e3ef8), CONST64(0x0528110a2d050514), CONST64(0x671fe6ce78676781), +CONST64(0xe47353d597e4e4b7), CONST64(0x2725bb4e0227279c), CONST64(0x4132588273414119), CONST64(0x8b2c9d0ba78b8b16), +CONST64(0xa7510153f6a7a7a6), CONST64(0x7dcf94fab27d7de9), CONST64(0x95dcfb374995956e), CONST64(0xd88e9fad56d8d847), +CONST64(0xfb8b30eb70fbfbcb), CONST64(0xee2371c1cdeeee9f), CONST64(0x7cc791f8bb7c7ced), CONST64(0x6617e3cc71666685), +CONST64(0xdda68ea77bdddd53), CONST64(0x17b84b2eaf17175c), CONST64(0x4702468e45474701), CONST64(0x9e84dc211a9e9e42), +CONST64(0xca1ec589d4caca0f), CONST64(0x2d75995a582d2db4), CONST64(0xbf9179632ebfbfc6), CONST64(0x07381b0e3f07071c), +CONST64(0xad012347acadad8e), CONST64(0x5aea2fb4b05a5a75), CONST64(0x836cb51bef838336), CONST64(0x3385ff66b63333cc), +CONST64(0x633ff2c65c636391), CONST64(0x02100a0412020208), CONST64(0xaa39384993aaaa92), CONST64(0x71afa8e2de7171d9), +CONST64(0xc80ecf8dc6c8c807), CONST64(0x19c87d32d1191964), CONST64(0x497270923b494939), CONST64(0xd9869aaf5fd9d943), +CONST64(0xf2c31df931f2f2ef), CONST64(0xe34b48dba8e3e3ab), CONST64(0x5be22ab6b95b5b71), CONST64(0x8834920dbc88881a), +CONST64(0x9aa4c8293e9a9a52), CONST64(0x262dbe4c0b262698), CONST64(0x328dfa64bf3232c8), CONST64(0xb0e94a7d59b0b0fa), +CONST64(0xe91b6acff2e9e983), CONST64(0x0f78331e770f0f3c), CONST64(0xd5e6a6b733d5d573), CONST64(0x8074ba1df480803a), +CONST64(0xbe997c6127bebec2), CONST64(0xcd26de87ebcdcd13), CONST64(0x34bde468893434d0), CONST64(0x487a75903248483d), +CONST64(0xffab24e354ffffdb), CONST64(0x7af78ff48d7a7af5), CONST64(0x90f4ea3d6490907a), CONST64(0x5fc23ebe9d5f5f61), +CONST64(0x201da0403d202080), CONST64(0x6867d5d00f6868bd), CONST64(0x1ad07234ca1a1a68), CONST64(0xae192c41b7aeae82), +CONST64(0xb4c95e757db4b4ea), CONST64(0x549a19a8ce54544d), CONST64(0x93ece53b7f939376), CONST64(0x220daa442f222288), +CONST64(0x6407e9c86364648d), CONST64(0xf1db12ff2af1f1e3), CONST64(0x73bfa2e6cc7373d1), CONST64(0x12905a2482121248), +CONST64(0x403a5d807a40401d), CONST64(0x0840281048080820), CONST64(0xc356e89b95c3c32b), CONST64(0xec337bc5dfecec97), +CONST64(0xdb9690ab4ddbdb4b), CONST64(0xa1611f5fc0a1a1be), CONST64(0x8d1c8307918d8d0e), CONST64(0x3df5c97ac83d3df4), +CONST64(0x97ccf1335b979766), CONST64(0x0000000000000000), CONST64(0xcf36d483f9cfcf1b), CONST64(0x2b4587566e2b2bac), +CONST64(0x7697b3ece17676c5), CONST64(0x8264b019e6828232), CONST64(0xd6fea9b128d6d67f), CONST64(0x1bd87736c31b1b6c), +CONST64(0xb5c15b7774b5b5ee), CONST64(0xaf112943beafaf86), CONST64(0x6a77dfd41d6a6ab5), CONST64(0x50ba0da0ea50505d), +CONST64(0x45124c8a57454509), CONST64(0xf3cb18fb38f3f3eb), CONST64(0x309df060ad3030c0), CONST64(0xef2b74c3c4efef9b), +CONST64(0x3fe5c37eda3f3ffc), CONST64(0x55921caac7555549), CONST64(0xa2791059dba2a2b2), CONST64(0xea0365c9e9eaea8f), +CONST64(0x650fecca6a656589), CONST64(0xbab9686903babad2), CONST64(0x2f65935e4a2f2fbc), CONST64(0xc04ee79d8ec0c027), +CONST64(0xdebe81a160dede5f), CONST64(0x1ce06c38fc1c1c70), CONST64(0xfdbb2ee746fdfdd3), CONST64(0x4d52649a1f4d4d29), +CONST64(0x92e4e03976929272), CONST64(0x758fbceafa7575c9), CONST64(0x06301e0c36060618), CONST64(0x8a249809ae8a8a12), +CONST64(0xb2f940794bb2b2f2), CONST64(0xe66359d185e6e6bf), CONST64(0x0e70361c7e0e0e38), CONST64(0x1ff8633ee71f1f7c), +CONST64(0x6237f7c455626295), CONST64(0xd4eea3b53ad4d477), CONST64(0xa829324d81a8a89a), CONST64(0x96c4f43152969662), +CONST64(0xf99b3aef62f9f9c3), CONST64(0xc566f697a3c5c533), CONST64(0x2535b14a10252594), CONST64(0x59f220b2ab595979), +CONST64(0x8454ae15d084842a), CONST64(0x72b7a7e4c57272d5), CONST64(0x39d5dd72ec3939e4), CONST64(0x4c5a6198164c4c2d), +CONST64(0x5eca3bbc945e5e65), CONST64(0x78e785f09f7878fd), CONST64(0x38ddd870e53838e0), CONST64(0x8c148605988c8c0a), +CONST64(0xd1c6b2bf17d1d163), CONST64(0xa5410b57e4a5a5ae), CONST64(0xe2434dd9a1e2e2af), CONST64(0x612ff8c24e616199), +CONST64(0xb3f1457b42b3b3f6), CONST64(0x2115a54234212184), CONST64(0x9c94d625089c9c4a), CONST64(0x1ef0663cee1e1e78), +CONST64(0x4322528661434311), CONST64(0xc776fc93b1c7c73b), CONST64(0xfcb32be54ffcfcd7), CONST64(0x0420140824040410), +CONST64(0x51b208a2e3515159), CONST64(0x99bcc72f2599995e), CONST64(0x6d4fc4da226d6da9), CONST64(0x0d68391a650d0d34), +CONST64(0xfa8335e979fafacf), CONST64(0xdfb684a369dfdf5b), CONST64(0x7ed79bfca97e7ee5), CONST64(0x243db44819242490), +CONST64(0x3bc5d776fe3b3bec), CONST64(0xab313d4b9aabab96), CONST64(0xce3ed181f0cece1f), CONST64(0x1188552299111144), +CONST64(0x8f0c8903838f8f06), CONST64(0x4e4a6b9c044e4e25), CONST64(0xb7d1517366b7b7e6), CONST64(0xeb0b60cbe0ebeb8b), +CONST64(0x3cfdcc78c13c3cf0), CONST64(0x817cbf1ffd81813e), CONST64(0x94d4fe354094946a), CONST64(0xf7eb0cf31cf7f7fb), +CONST64(0xb9a1676f18b9b9de), CONST64(0x13985f268b13134c), CONST64(0x2c7d9c58512c2cb0), CONST64(0xd3d6b8bb05d3d36b), +CONST64(0xe76b5cd38ce7e7bb), CONST64(0x6e57cbdc396e6ea5), CONST64(0xc46ef395aac4c437), CONST64(0x03180f061b03030c), +CONST64(0x568a13acdc565645), CONST64(0x441a49885e44440d), CONST64(0x7fdf9efea07f7fe1), CONST64(0xa921374f88a9a99e), +CONST64(0x2a4d8254672a2aa8), CONST64(0xbbb16d6b0abbbbd6), CONST64(0xc146e29f87c1c123), CONST64(0x53a202a6f1535351), +CONST64(0xdcae8ba572dcdc57), CONST64(0x0b582716530b0b2c), CONST64(0x9d9cd327019d9d4e), CONST64(0x6c47c1d82b6c6cad), +CONST64(0x3195f562a43131c4), CONST64(0x7487b9e8f37474cd), CONST64(0xf6e309f115f6f6ff), CONST64(0x460a438c4c464605), +CONST64(0xac092645a5acac8a), CONST64(0x893c970fb589891e), CONST64(0x14a04428b4141450), CONST64(0xe15b42dfbae1e1a3), +CONST64(0x16b04e2ca6161658), CONST64(0x3acdd274f73a3ae8), CONST64(0x696fd0d2066969b9), CONST64(0x09482d1241090924), +CONST64(0x70a7ade0d77070dd), CONST64(0xb6d954716fb6b6e2), CONST64(0xd0ceb7bd1ed0d067), CONST64(0xed3b7ec7d6eded93), +CONST64(0xcc2edb85e2cccc17), CONST64(0x422a578468424215), CONST64(0x98b4c22d2c98985a), CONST64(0xa4490e55eda4a4aa), +CONST64(0x285d8850752828a0), CONST64(0x5cda31b8865c5c6d), CONST64(0xf8933fed6bf8f8c7), CONST64(0x8644a411c2868622) +}; + +static const ulong64 sbox6[] = { +CONST64(0x6018c07830d81818), CONST64(0x8c2305af46262323), CONST64(0x3fc67ef991b8c6c6), CONST64(0x87e8136fcdfbe8e8), +CONST64(0x26874ca113cb8787), CONST64(0xdab8a9626d11b8b8), CONST64(0x0401080502090101), CONST64(0x214f426e9e0d4f4f), +CONST64(0xd836adee6c9b3636), CONST64(0xa2a6590451ffa6a6), CONST64(0x6fd2debdb90cd2d2), CONST64(0xf3f5fb06f70ef5f5), +CONST64(0xf979ef80f2967979), CONST64(0xa16f5fcede306f6f), CONST64(0x7e91fcef3f6d9191), CONST64(0x5552aa07a4f85252), +CONST64(0x9d6027fdc0476060), CONST64(0xcabc89766535bcbc), CONST64(0x569baccd2b379b9b), CONST64(0x028e048c018a8e8e), +CONST64(0xb6a371155bd2a3a3), CONST64(0x300c603c186c0c0c), CONST64(0xf17bff8af6847b7b), CONST64(0xd435b5e16a803535), +CONST64(0x741de8693af51d1d), CONST64(0xa7e05347ddb3e0e0), CONST64(0x7bd7f6acb321d7d7), CONST64(0x2fc25eed999cc2c2), +CONST64(0xb82e6d965c432e2e), CONST64(0x314b627a96294b4b), CONST64(0xdffea321e15dfefe), CONST64(0x41578216aed55757), +CONST64(0x5415a8412abd1515), CONST64(0xc1779fb6eee87777), CONST64(0xdc37a5eb6e923737), CONST64(0xb3e57b56d79ee5e5), +CONST64(0x469f8cd923139f9f), CONST64(0xe7f0d317fd23f0f0), CONST64(0x354a6a7f94204a4a), CONST64(0x4fda9e95a944dada), +CONST64(0x7d58fa25b0a25858), CONST64(0x03c906ca8fcfc9c9), CONST64(0xa429558d527c2929), CONST64(0x280a5022145a0a0a), +CONST64(0xfeb1e14f7f50b1b1), CONST64(0xbaa0691a5dc9a0a0), CONST64(0xb16b7fdad6146b6b), CONST64(0x2e855cab17d98585), +CONST64(0xcebd8173673cbdbd), CONST64(0x695dd234ba8f5d5d), CONST64(0x4010805020901010), CONST64(0xf7f4f303f507f4f4), +CONST64(0x0bcb16c08bddcbcb), CONST64(0xf83eedc67cd33e3e), CONST64(0x140528110a2d0505), CONST64(0x81671fe6ce786767), +CONST64(0xb7e47353d597e4e4), CONST64(0x9c2725bb4e022727), CONST64(0x1941325882734141), CONST64(0x168b2c9d0ba78b8b), +CONST64(0xa6a7510153f6a7a7), CONST64(0xe97dcf94fab27d7d), CONST64(0x6e95dcfb37499595), CONST64(0x47d88e9fad56d8d8), +CONST64(0xcbfb8b30eb70fbfb), CONST64(0x9fee2371c1cdeeee), CONST64(0xed7cc791f8bb7c7c), CONST64(0x856617e3cc716666), +CONST64(0x53dda68ea77bdddd), CONST64(0x5c17b84b2eaf1717), CONST64(0x014702468e454747), CONST64(0x429e84dc211a9e9e), +CONST64(0x0fca1ec589d4caca), CONST64(0xb42d75995a582d2d), CONST64(0xc6bf9179632ebfbf), CONST64(0x1c07381b0e3f0707), +CONST64(0x8ead012347acadad), CONST64(0x755aea2fb4b05a5a), CONST64(0x36836cb51bef8383), CONST64(0xcc3385ff66b63333), +CONST64(0x91633ff2c65c6363), CONST64(0x0802100a04120202), CONST64(0x92aa39384993aaaa), CONST64(0xd971afa8e2de7171), +CONST64(0x07c80ecf8dc6c8c8), CONST64(0x6419c87d32d11919), CONST64(0x39497270923b4949), CONST64(0x43d9869aaf5fd9d9), +CONST64(0xeff2c31df931f2f2), CONST64(0xabe34b48dba8e3e3), CONST64(0x715be22ab6b95b5b), CONST64(0x1a8834920dbc8888), +CONST64(0x529aa4c8293e9a9a), CONST64(0x98262dbe4c0b2626), CONST64(0xc8328dfa64bf3232), CONST64(0xfab0e94a7d59b0b0), +CONST64(0x83e91b6acff2e9e9), CONST64(0x3c0f78331e770f0f), CONST64(0x73d5e6a6b733d5d5), CONST64(0x3a8074ba1df48080), +CONST64(0xc2be997c6127bebe), CONST64(0x13cd26de87ebcdcd), CONST64(0xd034bde468893434), CONST64(0x3d487a7590324848), +CONST64(0xdbffab24e354ffff), CONST64(0xf57af78ff48d7a7a), CONST64(0x7a90f4ea3d649090), CONST64(0x615fc23ebe9d5f5f), +CONST64(0x80201da0403d2020), CONST64(0xbd6867d5d00f6868), CONST64(0x681ad07234ca1a1a), CONST64(0x82ae192c41b7aeae), +CONST64(0xeab4c95e757db4b4), CONST64(0x4d549a19a8ce5454), CONST64(0x7693ece53b7f9393), CONST64(0x88220daa442f2222), +CONST64(0x8d6407e9c8636464), CONST64(0xe3f1db12ff2af1f1), CONST64(0xd173bfa2e6cc7373), CONST64(0x4812905a24821212), +CONST64(0x1d403a5d807a4040), CONST64(0x2008402810480808), CONST64(0x2bc356e89b95c3c3), CONST64(0x97ec337bc5dfecec), +CONST64(0x4bdb9690ab4ddbdb), CONST64(0xbea1611f5fc0a1a1), CONST64(0x0e8d1c8307918d8d), CONST64(0xf43df5c97ac83d3d), +CONST64(0x6697ccf1335b9797), CONST64(0x0000000000000000), CONST64(0x1bcf36d483f9cfcf), CONST64(0xac2b4587566e2b2b), +CONST64(0xc57697b3ece17676), CONST64(0x328264b019e68282), CONST64(0x7fd6fea9b128d6d6), CONST64(0x6c1bd87736c31b1b), +CONST64(0xeeb5c15b7774b5b5), CONST64(0x86af112943beafaf), CONST64(0xb56a77dfd41d6a6a), CONST64(0x5d50ba0da0ea5050), +CONST64(0x0945124c8a574545), CONST64(0xebf3cb18fb38f3f3), CONST64(0xc0309df060ad3030), CONST64(0x9bef2b74c3c4efef), +CONST64(0xfc3fe5c37eda3f3f), CONST64(0x4955921caac75555), CONST64(0xb2a2791059dba2a2), CONST64(0x8fea0365c9e9eaea), +CONST64(0x89650fecca6a6565), CONST64(0xd2bab9686903baba), CONST64(0xbc2f65935e4a2f2f), CONST64(0x27c04ee79d8ec0c0), +CONST64(0x5fdebe81a160dede), CONST64(0x701ce06c38fc1c1c), CONST64(0xd3fdbb2ee746fdfd), CONST64(0x294d52649a1f4d4d), +CONST64(0x7292e4e039769292), CONST64(0xc9758fbceafa7575), CONST64(0x1806301e0c360606), CONST64(0x128a249809ae8a8a), +CONST64(0xf2b2f940794bb2b2), CONST64(0xbfe66359d185e6e6), CONST64(0x380e70361c7e0e0e), CONST64(0x7c1ff8633ee71f1f), +CONST64(0x956237f7c4556262), CONST64(0x77d4eea3b53ad4d4), CONST64(0x9aa829324d81a8a8), CONST64(0x6296c4f431529696), +CONST64(0xc3f99b3aef62f9f9), CONST64(0x33c566f697a3c5c5), CONST64(0x942535b14a102525), CONST64(0x7959f220b2ab5959), +CONST64(0x2a8454ae15d08484), CONST64(0xd572b7a7e4c57272), CONST64(0xe439d5dd72ec3939), CONST64(0x2d4c5a6198164c4c), +CONST64(0x655eca3bbc945e5e), CONST64(0xfd78e785f09f7878), CONST64(0xe038ddd870e53838), CONST64(0x0a8c148605988c8c), +CONST64(0x63d1c6b2bf17d1d1), CONST64(0xaea5410b57e4a5a5), CONST64(0xafe2434dd9a1e2e2), CONST64(0x99612ff8c24e6161), +CONST64(0xf6b3f1457b42b3b3), CONST64(0x842115a542342121), CONST64(0x4a9c94d625089c9c), CONST64(0x781ef0663cee1e1e), +CONST64(0x1143225286614343), CONST64(0x3bc776fc93b1c7c7), CONST64(0xd7fcb32be54ffcfc), CONST64(0x1004201408240404), +CONST64(0x5951b208a2e35151), CONST64(0x5e99bcc72f259999), CONST64(0xa96d4fc4da226d6d), CONST64(0x340d68391a650d0d), +CONST64(0xcffa8335e979fafa), CONST64(0x5bdfb684a369dfdf), CONST64(0xe57ed79bfca97e7e), CONST64(0x90243db448192424), +CONST64(0xec3bc5d776fe3b3b), CONST64(0x96ab313d4b9aabab), CONST64(0x1fce3ed181f0cece), CONST64(0x4411885522991111), +CONST64(0x068f0c8903838f8f), CONST64(0x254e4a6b9c044e4e), CONST64(0xe6b7d1517366b7b7), CONST64(0x8beb0b60cbe0ebeb), +CONST64(0xf03cfdcc78c13c3c), CONST64(0x3e817cbf1ffd8181), CONST64(0x6a94d4fe35409494), CONST64(0xfbf7eb0cf31cf7f7), +CONST64(0xdeb9a1676f18b9b9), CONST64(0x4c13985f268b1313), CONST64(0xb02c7d9c58512c2c), CONST64(0x6bd3d6b8bb05d3d3), +CONST64(0xbbe76b5cd38ce7e7), CONST64(0xa56e57cbdc396e6e), CONST64(0x37c46ef395aac4c4), CONST64(0x0c03180f061b0303), +CONST64(0x45568a13acdc5656), CONST64(0x0d441a49885e4444), CONST64(0xe17fdf9efea07f7f), CONST64(0x9ea921374f88a9a9), +CONST64(0xa82a4d8254672a2a), CONST64(0xd6bbb16d6b0abbbb), CONST64(0x23c146e29f87c1c1), CONST64(0x5153a202a6f15353), +CONST64(0x57dcae8ba572dcdc), CONST64(0x2c0b582716530b0b), CONST64(0x4e9d9cd327019d9d), CONST64(0xad6c47c1d82b6c6c), +CONST64(0xc43195f562a43131), CONST64(0xcd7487b9e8f37474), CONST64(0xfff6e309f115f6f6), CONST64(0x05460a438c4c4646), +CONST64(0x8aac092645a5acac), CONST64(0x1e893c970fb58989), CONST64(0x5014a04428b41414), CONST64(0xa3e15b42dfbae1e1), +CONST64(0x5816b04e2ca61616), CONST64(0xe83acdd274f73a3a), CONST64(0xb9696fd0d2066969), CONST64(0x2409482d12410909), +CONST64(0xdd70a7ade0d77070), CONST64(0xe2b6d954716fb6b6), CONST64(0x67d0ceb7bd1ed0d0), CONST64(0x93ed3b7ec7d6eded), +CONST64(0x17cc2edb85e2cccc), CONST64(0x15422a5784684242), CONST64(0x5a98b4c22d2c9898), CONST64(0xaaa4490e55eda4a4), +CONST64(0xa0285d8850752828), CONST64(0x6d5cda31b8865c5c), CONST64(0xc7f8933fed6bf8f8), CONST64(0x228644a411c28686) +}; + +static const ulong64 sbox7[] = { +CONST64(0x186018c07830d818), CONST64(0x238c2305af462623), CONST64(0xc63fc67ef991b8c6), CONST64(0xe887e8136fcdfbe8), +CONST64(0x8726874ca113cb87), CONST64(0xb8dab8a9626d11b8), CONST64(0x0104010805020901), CONST64(0x4f214f426e9e0d4f), +CONST64(0x36d836adee6c9b36), CONST64(0xa6a2a6590451ffa6), CONST64(0xd26fd2debdb90cd2), CONST64(0xf5f3f5fb06f70ef5), +CONST64(0x79f979ef80f29679), CONST64(0x6fa16f5fcede306f), CONST64(0x917e91fcef3f6d91), CONST64(0x525552aa07a4f852), +CONST64(0x609d6027fdc04760), CONST64(0xbccabc89766535bc), CONST64(0x9b569baccd2b379b), CONST64(0x8e028e048c018a8e), +CONST64(0xa3b6a371155bd2a3), CONST64(0x0c300c603c186c0c), CONST64(0x7bf17bff8af6847b), CONST64(0x35d435b5e16a8035), +CONST64(0x1d741de8693af51d), CONST64(0xe0a7e05347ddb3e0), CONST64(0xd77bd7f6acb321d7), CONST64(0xc22fc25eed999cc2), +CONST64(0x2eb82e6d965c432e), CONST64(0x4b314b627a96294b), CONST64(0xfedffea321e15dfe), CONST64(0x5741578216aed557), +CONST64(0x155415a8412abd15), CONST64(0x77c1779fb6eee877), CONST64(0x37dc37a5eb6e9237), CONST64(0xe5b3e57b56d79ee5), +CONST64(0x9f469f8cd923139f), CONST64(0xf0e7f0d317fd23f0), CONST64(0x4a354a6a7f94204a), CONST64(0xda4fda9e95a944da), +CONST64(0x587d58fa25b0a258), CONST64(0xc903c906ca8fcfc9), CONST64(0x29a429558d527c29), CONST64(0x0a280a5022145a0a), +CONST64(0xb1feb1e14f7f50b1), CONST64(0xa0baa0691a5dc9a0), CONST64(0x6bb16b7fdad6146b), CONST64(0x852e855cab17d985), +CONST64(0xbdcebd8173673cbd), CONST64(0x5d695dd234ba8f5d), CONST64(0x1040108050209010), CONST64(0xf4f7f4f303f507f4), +CONST64(0xcb0bcb16c08bddcb), CONST64(0x3ef83eedc67cd33e), CONST64(0x05140528110a2d05), CONST64(0x6781671fe6ce7867), +CONST64(0xe4b7e47353d597e4), CONST64(0x279c2725bb4e0227), CONST64(0x4119413258827341), CONST64(0x8b168b2c9d0ba78b), +CONST64(0xa7a6a7510153f6a7), CONST64(0x7de97dcf94fab27d), CONST64(0x956e95dcfb374995), CONST64(0xd847d88e9fad56d8), +CONST64(0xfbcbfb8b30eb70fb), CONST64(0xee9fee2371c1cdee), CONST64(0x7ced7cc791f8bb7c), CONST64(0x66856617e3cc7166), +CONST64(0xdd53dda68ea77bdd), CONST64(0x175c17b84b2eaf17), CONST64(0x47014702468e4547), CONST64(0x9e429e84dc211a9e), +CONST64(0xca0fca1ec589d4ca), CONST64(0x2db42d75995a582d), CONST64(0xbfc6bf9179632ebf), CONST64(0x071c07381b0e3f07), +CONST64(0xad8ead012347acad), CONST64(0x5a755aea2fb4b05a), CONST64(0x8336836cb51bef83), CONST64(0x33cc3385ff66b633), +CONST64(0x6391633ff2c65c63), CONST64(0x020802100a041202), CONST64(0xaa92aa39384993aa), CONST64(0x71d971afa8e2de71), +CONST64(0xc807c80ecf8dc6c8), CONST64(0x196419c87d32d119), CONST64(0x4939497270923b49), CONST64(0xd943d9869aaf5fd9), +CONST64(0xf2eff2c31df931f2), CONST64(0xe3abe34b48dba8e3), CONST64(0x5b715be22ab6b95b), CONST64(0x881a8834920dbc88), +CONST64(0x9a529aa4c8293e9a), CONST64(0x2698262dbe4c0b26), CONST64(0x32c8328dfa64bf32), CONST64(0xb0fab0e94a7d59b0), +CONST64(0xe983e91b6acff2e9), CONST64(0x0f3c0f78331e770f), CONST64(0xd573d5e6a6b733d5), CONST64(0x803a8074ba1df480), +CONST64(0xbec2be997c6127be), CONST64(0xcd13cd26de87ebcd), CONST64(0x34d034bde4688934), CONST64(0x483d487a75903248), +CONST64(0xffdbffab24e354ff), CONST64(0x7af57af78ff48d7a), CONST64(0x907a90f4ea3d6490), CONST64(0x5f615fc23ebe9d5f), +CONST64(0x2080201da0403d20), CONST64(0x68bd6867d5d00f68), CONST64(0x1a681ad07234ca1a), CONST64(0xae82ae192c41b7ae), +CONST64(0xb4eab4c95e757db4), CONST64(0x544d549a19a8ce54), CONST64(0x937693ece53b7f93), CONST64(0x2288220daa442f22), +CONST64(0x648d6407e9c86364), CONST64(0xf1e3f1db12ff2af1), CONST64(0x73d173bfa2e6cc73), CONST64(0x124812905a248212), +CONST64(0x401d403a5d807a40), CONST64(0x0820084028104808), CONST64(0xc32bc356e89b95c3), CONST64(0xec97ec337bc5dfec), +CONST64(0xdb4bdb9690ab4ddb), CONST64(0xa1bea1611f5fc0a1), CONST64(0x8d0e8d1c8307918d), CONST64(0x3df43df5c97ac83d), +CONST64(0x976697ccf1335b97), CONST64(0x0000000000000000), CONST64(0xcf1bcf36d483f9cf), CONST64(0x2bac2b4587566e2b), +CONST64(0x76c57697b3ece176), CONST64(0x82328264b019e682), CONST64(0xd67fd6fea9b128d6), CONST64(0x1b6c1bd87736c31b), +CONST64(0xb5eeb5c15b7774b5), CONST64(0xaf86af112943beaf), CONST64(0x6ab56a77dfd41d6a), CONST64(0x505d50ba0da0ea50), +CONST64(0x450945124c8a5745), CONST64(0xf3ebf3cb18fb38f3), CONST64(0x30c0309df060ad30), CONST64(0xef9bef2b74c3c4ef), +CONST64(0x3ffc3fe5c37eda3f), CONST64(0x554955921caac755), CONST64(0xa2b2a2791059dba2), CONST64(0xea8fea0365c9e9ea), +CONST64(0x6589650fecca6a65), CONST64(0xbad2bab9686903ba), CONST64(0x2fbc2f65935e4a2f), CONST64(0xc027c04ee79d8ec0), +CONST64(0xde5fdebe81a160de), CONST64(0x1c701ce06c38fc1c), CONST64(0xfdd3fdbb2ee746fd), CONST64(0x4d294d52649a1f4d), +CONST64(0x927292e4e0397692), CONST64(0x75c9758fbceafa75), CONST64(0x061806301e0c3606), CONST64(0x8a128a249809ae8a), +CONST64(0xb2f2b2f940794bb2), CONST64(0xe6bfe66359d185e6), CONST64(0x0e380e70361c7e0e), CONST64(0x1f7c1ff8633ee71f), +CONST64(0x62956237f7c45562), CONST64(0xd477d4eea3b53ad4), CONST64(0xa89aa829324d81a8), CONST64(0x966296c4f4315296), +CONST64(0xf9c3f99b3aef62f9), CONST64(0xc533c566f697a3c5), CONST64(0x25942535b14a1025), CONST64(0x597959f220b2ab59), +CONST64(0x842a8454ae15d084), CONST64(0x72d572b7a7e4c572), CONST64(0x39e439d5dd72ec39), CONST64(0x4c2d4c5a6198164c), +CONST64(0x5e655eca3bbc945e), CONST64(0x78fd78e785f09f78), CONST64(0x38e038ddd870e538), CONST64(0x8c0a8c148605988c), +CONST64(0xd163d1c6b2bf17d1), CONST64(0xa5aea5410b57e4a5), CONST64(0xe2afe2434dd9a1e2), CONST64(0x6199612ff8c24e61), +CONST64(0xb3f6b3f1457b42b3), CONST64(0x21842115a5423421), CONST64(0x9c4a9c94d625089c), CONST64(0x1e781ef0663cee1e), +CONST64(0x4311432252866143), CONST64(0xc73bc776fc93b1c7), CONST64(0xfcd7fcb32be54ffc), CONST64(0x0410042014082404), +CONST64(0x515951b208a2e351), CONST64(0x995e99bcc72f2599), CONST64(0x6da96d4fc4da226d), CONST64(0x0d340d68391a650d), +CONST64(0xfacffa8335e979fa), CONST64(0xdf5bdfb684a369df), CONST64(0x7ee57ed79bfca97e), CONST64(0x2490243db4481924), +CONST64(0x3bec3bc5d776fe3b), CONST64(0xab96ab313d4b9aab), CONST64(0xce1fce3ed181f0ce), CONST64(0x1144118855229911), +CONST64(0x8f068f0c8903838f), CONST64(0x4e254e4a6b9c044e), CONST64(0xb7e6b7d1517366b7), CONST64(0xeb8beb0b60cbe0eb), +CONST64(0x3cf03cfdcc78c13c), CONST64(0x813e817cbf1ffd81), CONST64(0x946a94d4fe354094), CONST64(0xf7fbf7eb0cf31cf7), +CONST64(0xb9deb9a1676f18b9), CONST64(0x134c13985f268b13), CONST64(0x2cb02c7d9c58512c), CONST64(0xd36bd3d6b8bb05d3), +CONST64(0xe7bbe76b5cd38ce7), CONST64(0x6ea56e57cbdc396e), CONST64(0xc437c46ef395aac4), CONST64(0x030c03180f061b03), +CONST64(0x5645568a13acdc56), CONST64(0x440d441a49885e44), CONST64(0x7fe17fdf9efea07f), CONST64(0xa99ea921374f88a9), +CONST64(0x2aa82a4d8254672a), CONST64(0xbbd6bbb16d6b0abb), CONST64(0xc123c146e29f87c1), CONST64(0x535153a202a6f153), +CONST64(0xdc57dcae8ba572dc), CONST64(0x0b2c0b582716530b), CONST64(0x9d4e9d9cd327019d), CONST64(0x6cad6c47c1d82b6c), +CONST64(0x31c43195f562a431), CONST64(0x74cd7487b9e8f374), CONST64(0xf6fff6e309f115f6), CONST64(0x4605460a438c4c46), +CONST64(0xac8aac092645a5ac), CONST64(0x891e893c970fb589), CONST64(0x145014a04428b414), CONST64(0xe1a3e15b42dfbae1), +CONST64(0x165816b04e2ca616), CONST64(0x3ae83acdd274f73a), CONST64(0x69b9696fd0d20669), CONST64(0x092409482d124109), +CONST64(0x70dd70a7ade0d770), CONST64(0xb6e2b6d954716fb6), CONST64(0xd067d0ceb7bd1ed0), CONST64(0xed93ed3b7ec7d6ed), +CONST64(0xcc17cc2edb85e2cc), CONST64(0x4215422a57846842), CONST64(0x985a98b4c22d2c98), CONST64(0xa4aaa4490e55eda4), +CONST64(0x28a0285d88507528), CONST64(0x5c6d5cda31b8865c), CONST64(0xf8c7f8933fed6bf8), CONST64(0x86228644a411c286) +}; + +#endif + +static const ulong64 cont[] = { +CONST64(0x1823c6e887b8014f), +CONST64(0x36a6d2f5796f9152), +CONST64(0x60bc9b8ea30c7b35), +CONST64(0x1de0d7c22e4bfe57), +CONST64(0x157737e59ff04ada), +CONST64(0x58c9290ab1a06b85), +CONST64(0xbd5d10f4cb3e0567), +CONST64(0xe427418ba77d95d8), +CONST64(0xfbee7c66dd17479e), +CONST64(0xca2dbf07ad5a8333), +CONST64(0x6302aa71c81949d9), +}; + + +/* $Source: /cvs/libtom/libtomcrypt/src/hashes/whirl/whirltab.c,v $ */ +/* $Revision: 1.3 $ */ +/* $Date: 2007/05/12 14:21:44 $ */ diff --git a/crypto777/hmac_sha512.c b/crypto777/hmac_sha512.c new file mode 100755 index 000000000..82adff1ed --- /dev/null +++ b/crypto777/hmac_sha512.c @@ -0,0 +1,618 @@ +/* + * FIPS 180-2 SHA-224/256/384/512 implementation + * Last update: 02/02/2007 + * Issue date: 04/30/2005 + * + * Copyright (C) 2005, 2007 Olivier Gay + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +#define SHA512_DIGEST_SIZE (512 / 8) +#include +#include + +#define SHA512_BLOCK_SIZE (1024 / 8) + +typedef struct { + unsigned int tot_len; + unsigned int len; + unsigned char block[2 * SHA512_BLOCK_SIZE]; + unsigned long long h[8]; +} sha512_ctx; + +typedef struct { + sha512_ctx ctx_inside; + sha512_ctx ctx_outside; + + /* for hmac_reinit */ + sha512_ctx ctx_inside_reinit; + sha512_ctx ctx_outside_reinit; + + unsigned char block_ipad[SHA512_BLOCK_SIZE]; + unsigned char block_opad[SHA512_BLOCK_SIZE]; +} hmac_sha512_ctx; + +#define SHFR(x, n) (x >> n) +#define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) +#define CH(x, y, z) ((x & y) ^ (~x & z)) +#define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) + +#define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39)) +#define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41)) +#define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7)) +#define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6)) + +#define UNPACK32(x, str) \ +{ \ + *((str) + 3) = (unsigned char) ((x) ); \ + *((str) + 2) = (unsigned char) ((x) >> 8); \ + *((str) + 1) = (unsigned char) ((x) >> 16); \ + *((str) + 0) = (unsigned char) ((x) >> 24); \ +} + +#define UNPACK64(x, str) \ +{ \ + *((str) + 7) = (unsigned char) ((x) ); \ + *((str) + 6) = (unsigned char) ((x) >> 8); \ + *((str) + 5) = (unsigned char) ((x) >> 16); \ + *((str) + 4) = (unsigned char) ((x) >> 24); \ + *((str) + 3) = (unsigned char) ((x) >> 32); \ + *((str) + 2) = (unsigned char) ((x) >> 40); \ + *((str) + 1) = (unsigned char) ((x) >> 48); \ + *((str) + 0) = (unsigned char) ((x) >> 56); \ +} + +#define PACK64(str, x) \ +{ \ + *(x) = ((unsigned long long) *((str) + 7) ) \ + | ((unsigned long long) *((str) + 6) << 8) \ + | ((unsigned long long) *((str) + 5) << 16) \ + | ((unsigned long long) *((str) + 4) << 24) \ + | ((unsigned long long) *((str) + 3) << 32) \ + | ((unsigned long long) *((str) + 2) << 40) \ + | ((unsigned long long) *((str) + 1) << 48) \ + | ((unsigned long long) *((str) + 0) << 56); \ +} + +/* Macros used for loops unrolling */ + +#define SHA512_SCR(i) \ +{ \ + w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \ + + SHA512_F3(w[i - 15]) + w[i - 16]; \ +} + +#define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \ +{ \ + t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \ + + sha512_k[j] + w[j]; \ + t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ + wv[d] += t1; \ + wv[h] = t1 + t2; \ +} + +static unsigned long long sha512_h0[8] = { + 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL +}; + +static unsigned long long sha512_k[80] = { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL +}; + +static void sha512_transf(sha512_ctx * ctx, const unsigned char *message, + unsigned int block_nb) +{ + unsigned long long w[80]; + unsigned long long wv[8]; + unsigned long long t1, t2; + const unsigned char *sub_block; + int i, j; + + for (i = 0; i < (int)block_nb; i++) { + sub_block = message + (i << 7); + + PACK64(&sub_block[0], &w[0]); + PACK64(&sub_block[8], &w[1]); + PACK64(&sub_block[16], &w[2]); + PACK64(&sub_block[24], &w[3]); + PACK64(&sub_block[32], &w[4]); + PACK64(&sub_block[40], &w[5]); + PACK64(&sub_block[48], &w[6]); + PACK64(&sub_block[56], &w[7]); + PACK64(&sub_block[64], &w[8]); + PACK64(&sub_block[72], &w[9]); + PACK64(&sub_block[80], &w[10]); + PACK64(&sub_block[88], &w[11]); + PACK64(&sub_block[96], &w[12]); + PACK64(&sub_block[104], &w[13]); + PACK64(&sub_block[112], &w[14]); + PACK64(&sub_block[120], &w[15]); + + SHA512_SCR(16); + SHA512_SCR(17); + SHA512_SCR(18); + SHA512_SCR(19); + SHA512_SCR(20); + SHA512_SCR(21); + SHA512_SCR(22); + SHA512_SCR(23); + SHA512_SCR(24); + SHA512_SCR(25); + SHA512_SCR(26); + SHA512_SCR(27); + SHA512_SCR(28); + SHA512_SCR(29); + SHA512_SCR(30); + SHA512_SCR(31); + SHA512_SCR(32); + SHA512_SCR(33); + SHA512_SCR(34); + SHA512_SCR(35); + SHA512_SCR(36); + SHA512_SCR(37); + SHA512_SCR(38); + SHA512_SCR(39); + SHA512_SCR(40); + SHA512_SCR(41); + SHA512_SCR(42); + SHA512_SCR(43); + SHA512_SCR(44); + SHA512_SCR(45); + SHA512_SCR(46); + SHA512_SCR(47); + SHA512_SCR(48); + SHA512_SCR(49); + SHA512_SCR(50); + SHA512_SCR(51); + SHA512_SCR(52); + SHA512_SCR(53); + SHA512_SCR(54); + SHA512_SCR(55); + SHA512_SCR(56); + SHA512_SCR(57); + SHA512_SCR(58); + SHA512_SCR(59); + SHA512_SCR(60); + SHA512_SCR(61); + SHA512_SCR(62); + SHA512_SCR(63); + SHA512_SCR(64); + SHA512_SCR(65); + SHA512_SCR(66); + SHA512_SCR(67); + SHA512_SCR(68); + SHA512_SCR(69); + SHA512_SCR(70); + SHA512_SCR(71); + SHA512_SCR(72); + SHA512_SCR(73); + SHA512_SCR(74); + SHA512_SCR(75); + SHA512_SCR(76); + SHA512_SCR(77); + SHA512_SCR(78); + SHA512_SCR(79); + + wv[0] = ctx->h[0]; + wv[1] = ctx->h[1]; + wv[2] = ctx->h[2]; + wv[3] = ctx->h[3]; + wv[4] = ctx->h[4]; + wv[5] = ctx->h[5]; + wv[6] = ctx->h[6]; + wv[7] = ctx->h[7]; + + j = 0; + + do { + SHA512_EXP(0, 1, 2, 3, 4, 5, 6, 7, j); + j++; + SHA512_EXP(7, 0, 1, 2, 3, 4, 5, 6, j); + j++; + SHA512_EXP(6, 7, 0, 1, 2, 3, 4, 5, j); + j++; + SHA512_EXP(5, 6, 7, 0, 1, 2, 3, 4, j); + j++; + SHA512_EXP(4, 5, 6, 7, 0, 1, 2, 3, j); + j++; + SHA512_EXP(3, 4, 5, 6, 7, 0, 1, 2, j); + j++; + SHA512_EXP(2, 3, 4, 5, 6, 7, 0, 1, j); + j++; + SHA512_EXP(1, 2, 3, 4, 5, 6, 7, 0, j); + j++; + } while (j < 80); + + ctx->h[0] += wv[0]; + ctx->h[1] += wv[1]; + ctx->h[2] += wv[2]; + ctx->h[3] += wv[3]; + ctx->h[4] += wv[4]; + ctx->h[5] += wv[5]; + ctx->h[6] += wv[6]; + ctx->h[7] += wv[7]; + } +} + + +static void _sha512_init(sha512_ctx * ctx) +{ + ctx->h[0] = sha512_h0[0]; + ctx->h[1] = sha512_h0[1]; + ctx->h[2] = sha512_h0[2]; + ctx->h[3] = sha512_h0[3]; + ctx->h[4] = sha512_h0[4]; + ctx->h[5] = sha512_h0[5]; + ctx->h[6] = sha512_h0[6]; + ctx->h[7] = sha512_h0[7]; + + ctx->len = 0; + ctx->tot_len = 0; +} + +static void sha512_update(sha512_ctx * ctx, const unsigned char *message, + unsigned int len) +{ + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char *shifted_message; + + tmp_len = SHA512_BLOCK_SIZE - ctx->len; + rem_len = len < tmp_len ? len : tmp_len; + + memcpy(&ctx->block[ctx->len], message, rem_len); + + if (ctx->len + len < SHA512_BLOCK_SIZE) { + ctx->len += len; + return; + } + + new_len = len - rem_len; + block_nb = new_len / SHA512_BLOCK_SIZE; + + shifted_message = message + rem_len; + + sha512_transf(ctx, ctx->block, 1); + sha512_transf(ctx, shifted_message, block_nb); + + rem_len = new_len % SHA512_BLOCK_SIZE; + + memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); + + ctx->len = rem_len; + ctx->tot_len += (block_nb + 1) << 7; +} + +static void sha512_final(sha512_ctx * ctx, unsigned char *digest) +{ + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; + + block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) + < (ctx->len % SHA512_BLOCK_SIZE)); + + len_b = (ctx->tot_len + ctx->len) << 3; + pm_len = block_nb << 7; + + memset(ctx->block + ctx->len, 0, pm_len - ctx->len); + ctx->block[ctx->len] = 0x80; + UNPACK32(len_b, ctx->block + pm_len - 4); + + sha512_transf(ctx, ctx->block, block_nb); + + UNPACK64(ctx->h[0], &digest[0]); + UNPACK64(ctx->h[1], &digest[8]); + UNPACK64(ctx->h[2], &digest[16]); + UNPACK64(ctx->h[3], &digest[24]); + UNPACK64(ctx->h[4], &digest[32]); + UNPACK64(ctx->h[5], &digest[40]); + UNPACK64(ctx->h[6], &digest[48]); + UNPACK64(ctx->h[7], &digest[56]); +} + +void sha512(const unsigned char *message, unsigned int len,unsigned char *digest) +{ + sha512_ctx ctx; + _sha512_init(&ctx); + sha512_update(&ctx, message, len); + sha512_final(&ctx, digest); +} + +int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); +void calc_sha512(char *str,uint8_t *digest,uint8_t *message,int32_t len) +{ + sha512_ctx ctx; + _sha512_init(&ctx); + sha512_update(&ctx, message, len); + sha512_final(&ctx, digest); + if ( str != 0 ) + init_hexbytes_noT(str,digest,512>>3); +} + +static void hmac_sha512_init(hmac_sha512_ctx * ctx, const unsigned char *key,unsigned int key_size) +{ + unsigned int fill; + unsigned int num; + + const unsigned char *key_used; + unsigned char key_temp[SHA512_DIGEST_SIZE]; + int i; + + if (key_size == SHA512_BLOCK_SIZE) { + key_used = key; + num = SHA512_BLOCK_SIZE; + } else { + if (key_size > SHA512_BLOCK_SIZE) { + num = SHA512_DIGEST_SIZE; + sha512(key, key_size, key_temp); + key_used = key_temp; + } else { /* key_size > SHA512_BLOCK_SIZE */ + key_used = key; + num = key_size; + } + fill = SHA512_BLOCK_SIZE - num; + + memset(ctx->block_ipad + num, 0x36, fill); + memset(ctx->block_opad + num, 0x5c, fill); + } + + for (i = 0; i < (int)num; i++) { + ctx->block_ipad[i] = key_used[i] ^ 0x36; + ctx->block_opad[i] = key_used[i] ^ 0x5c; + } + + _sha512_init(&ctx->ctx_inside); + sha512_update(&ctx->ctx_inside, ctx->block_ipad, SHA512_BLOCK_SIZE); + + _sha512_init(&ctx->ctx_outside); + sha512_update(&ctx->ctx_outside, ctx->block_opad, SHA512_BLOCK_SIZE); + + /* for hmac_reinit */ + memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, sizeof(sha512_ctx)); + memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, sizeof(sha512_ctx)); +} + +static void hmac_sha512_update(hmac_sha512_ctx * ctx, const unsigned char *message, + unsigned int message_len) +{ + sha512_update(&ctx->ctx_inside, message, message_len); +} + +static void hmac_sha512_final(hmac_sha512_ctx * ctx, unsigned char *mac, + unsigned int mac_size) +{ + unsigned char digest_inside[SHA512_DIGEST_SIZE]; + unsigned char mac_temp[SHA512_DIGEST_SIZE]; + + sha512_final(&ctx->ctx_inside, digest_inside); + sha512_update(&ctx->ctx_outside, digest_inside, SHA512_DIGEST_SIZE); + sha512_final(&ctx->ctx_outside, mac_temp); + memcpy(mac, mac_temp, mac_size); +} + +void hmac_sha512(const unsigned char *key, unsigned int key_size, + const unsigned char *message, unsigned int message_len, + unsigned char *mac, unsigned mac_size) +{ + hmac_sha512_ctx ctx; + + hmac_sha512_init(&ctx, key, key_size); + hmac_sha512_update(&ctx, message, message_len); + hmac_sha512_final(&ctx, mac, mac_size); +} + +int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); +#ifndef libtom_hmac +#define libtom_hmac +#include "hmac/crypt_argchk.c" +#include "hmac/hash_memory.c" +#include "hmac/hmac_init.c" +#include "hmac/hmac_process.c" +#include "hmac/hmac_done.c" +#include "hmac/hmac_file.c" +#include "hmac/hmac_memory.c" +#include "hmac/rmd128.c" +#include "hmac/rmd160.c" +#include "hmac/rmd256.c" +#include "hmac/rmd320.c" +#include "hmac/tiger.c" +#include "hmac/md2.c" +#include "hmac/md4.c" +#include "hmac/md5.c" +#include "hmac/sha1.c" +#include "hmac/whirl.c" +#include "hmac/sha224.c" +#include "hmac/sha256.c" +#include "hmac/sha384.c" +#include "hmac/sha512.c" +#endif + +char *hmac_sha512_str(char *dest,char *key,int32_t key_size,char *message) +{ + unsigned char mac[SHA512_DIGEST_SIZE],checkbuf[SHA512_DIGEST_SIZE*2 + 1]; char dest2[SHA512_DIGEST_SIZE*2 + 1]; unsigned long size = sizeof(checkbuf); + //int i; + hmac_sha512((const unsigned char *)key,key_size,(const unsigned char *)message,(int)strlen(message),mac,SHA512_DIGEST_SIZE); + //for (i=0; i +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif + +void OS_randombytes(unsigned char *x,long xlen) +{ + OS_portable_randombytes(x,xlen); +} + +static double _kb(double n) { return(n / 1024.); } +static double _mb(double n) { return(n / (1024.*1024.)); } +static double _gb(double n) { return(n / (1024.*1024.*1024.)); } + +char *mbstr(char *str,double n) +{ + if ( n < 1024*1024*10 ) + sprintf(str,"%.3fkb",_kb(n)); + else if ( n < 1024*1024*1024 ) + sprintf(str,"%.1fMB",_mb(n)); + else + sprintf(str,"%.2fGB",_gb(n)); + return(str); +} + +long myallocated(uint8_t type,long change) +{ + static int64_t Total_allocated,HWM_allocated,Type_allocated[256]; + int32_t i; int64_t total = 0; char buf[2049],str[65]; + buf[0] = 0; + if ( type == 0 && change <= 0 ) + { + for (i=0; i<256; i++) + { + if ( Type_allocated[i] != 0 ) + { + total += Type_allocated[i]; + if ( change == 0 ) + sprintf(buf+strlen(buf),"(%c %s) ",i,mbstr(str,Type_allocated[i])); + } + } + if ( change == 0 ) + { + sprintf(buf + strlen(buf),"-> total %lld %s",(long long)total,mbstr(str,total)); + printf("%s\n",buf); + } + } + else + { + Type_allocated[type] += change; + Total_allocated += change; + if ( Total_allocated > HWM_allocated ) + { + printf("HWM allocated %ld %s\n",(long)Total_allocated,mbstr(str,Total_allocated)); + HWM_allocated = Total_allocated * 1.5; + } + } + return(total); +} + +void *mycalloc(uint8_t type,int32_t n,long itemsize) +{ + //static portable_mutex_t MEMmutex; + struct allocitem *item; int64_t allocsize = ((uint64_t)n * itemsize); + if ( type == 0 && n == 0 && itemsize == 0 ) + { + //portable_mutex_init(&MEMmutex); + myfree(mycalloc('t',1024,1024 * 32),1024*1024*32); + return(0); + } + //portable_mutex_lock(&MEMmutex); + myallocated(type,allocsize); + while ( (item= calloc(1,sizeof(struct allocitem) + allocsize + 16)) == 0 ) + { + char str[65]; + printf("mycalloc: need to wait for memory.(%d,%ld) %s to be available\n",n,itemsize,mbstr(str,allocsize)); + sleep(1); + } + //printf("calloc origptr.%p retptr.%p size.%ld\n",item,(void *)(long)item + sizeof(*item),allocsize); + item->allocsize = (uint32_t)allocsize; + item->type = type; + //portable_mutex_unlock(&MEMmutex); + return((void *)(long)item + sizeof(*item)); +} + +void *queueitem(char *str) +{ + struct queueitem *item; int32_t n,allocsize; char *data; uint8_t type = 'y'; + //portable_mutex_lock(&MEMmutex); + n = (uint32_t)strlen(str) + 1; + allocsize = (uint32_t)(sizeof(struct queueitem) + n); + myallocated(type,allocsize); + while ( (item= calloc(1,allocsize)) == 0 ) + { + char str[65]; + printf("queueitem: need to wait for memory.(%d,%ld) %s to be available\n",n,(long)sizeof(*item),mbstr(str,allocsize)); + sleep(1); + } + item->allocsize = (uint32_t)allocsize; + item->type = type; + data = (void *)((uint64_t)item + sizeof(*item)); + memcpy(data,str,n); + //printf("(%c) queueitem.%p itemdata.%p n.%d allocsize.%d\n",type,item,data,n,allocsize); + //portable_mutex_unlock(&MEMmutex); + return(data); +} + +void _myfree(uint8_t type,int32_t origallocsize,void *origptr,int32_t allocsize) +{ + //portable_mutex_lock(&MEMmutex); + if ( allocsize == origallocsize ) + { + myallocated(type,-allocsize); + // Type_allocated[type & 0xff] -= allocsize; + // Total_allocated -= allocsize; + //printf("myfree.%p size.%d %d type %x\n",origptr,allocsize,origallocsize,type); + free(origptr); + } + else + { + printf("myfree size error %d vs %d at %p\n",allocsize,origallocsize,origptr); + getchar(); + } + //portable_mutex_unlock(&MEMmutex); +} + +void myfree(void *_ptr,long allocsize) +{ + struct allocitem *item = (void *)((long)_ptr - sizeof(struct allocitem)); + _myfree(item->type,item->allocsize,item,(uint32_t)allocsize); +} + +void free_queueitem(void *itemdata) +{ + struct queueitem *item = (void *)((long)itemdata - sizeof(struct queueitem)); + //printf("freeq item.%p itemdata.%p size.%d\n",item,itemdata,item->allocsize); + _myfree(item->type,item->allocsize,item,item->allocsize); +} + +void *myrealloc(uint8_t type,void *oldptr,long oldsize,long newsize) +{ + void *newptr; + newptr = mycalloc(type,1,newsize); + //printf("newptr.%p type.%c oldsize.%ld newsize.%ld\n",newptr,type,oldsize,newsize); + if ( oldptr != 0 ) + { + memcpy(newptr,oldptr,oldsize < newsize ? oldsize : newsize); + myfree(oldptr,oldsize); + } + return(newptr); +} + +static uint64_t _align16(uint64_t ptrval) { if ( (ptrval & 15) != 0 ) ptrval += 16 - (ptrval & 15); return(ptrval); } + +void *myaligned_alloc(uint64_t allocsize) +{ + void *ptr,*realptr; uint64_t tmp; + realptr = mycalloc('A',1,(long)(allocsize + 16 + sizeof(realptr))); + tmp = _align16((long)realptr + sizeof(ptr)); + memcpy(&ptr,&tmp,sizeof(ptr)); + memcpy((void *)((long)ptr - sizeof(realptr)),&realptr,sizeof(realptr)); + //printf("aligned_alloc(%llu) realptr.%p -> ptr.%p, diff.%ld\n",(long long)allocsize,realptr,ptr,((long)ptr - (long)realptr)); + return(ptr); +} + +int32_t myaligned_free(void *ptr,long size) +{ + void *realptr; + long diff; + if ( ((long)ptr & 0xf) != 0 ) + { + printf("misaligned ptr.%p being aligned_free\n",ptr); + return(-1); + } + memcpy(&realptr,(void *)((long)ptr - sizeof(realptr)),sizeof(realptr)); + diff = ((long)ptr - (long)realptr); + if ( diff < (long)sizeof(ptr) || diff > 32 ) + { + printf("ptr %p and realptr %p too far apart %ld\n",ptr,realptr,diff); + return(-2); + } + //printf("aligned_free: ptr %p -> realptr %p %ld\n",ptr,realptr,diff); + myfree(realptr,size + 16 + sizeof(realptr)); + return(0); +} + +void lock_queue(queue_t *queue) +{ + if ( queue->initflag == 0 ) + { + portable_mutex_init(&queue->mutex); + queue->initflag = 1; + } + portable_mutex_lock(&queue->mutex); +} + +void queue_enqueue(char *name,queue_t *queue,struct queueitem *origitem,int32_t offsetflag) +{ + struct queueitem *item; + if ( queue->name[0] == 0 && name != 0 && name[0] != 0 ) + strcpy(queue->name,name);//,sizeof(queue->name)); + if ( origitem == 0 ) + { + printf("FATAL type error: queueing empty value\n");//, getchar(); + return; + } + lock_queue(queue); + item = (struct queueitem *)((long)origitem - offsetflag*sizeof(struct queueitem)); + DL_APPEND(queue->list,item); + portable_mutex_unlock(&queue->mutex); + //printf("queue_enqueue name.(%s) origitem.%p append.%p list.%p\n",name,origitem,item,queue->list); +} + +void *queue_dequeue(queue_t *queue,int32_t offsetflag) +{ + struct queueitem *item = 0; + lock_queue(queue); + if ( queue->list != 0 ) + { + item = queue->list; + DL_DELETE(queue->list,item); + //printf("queue_dequeue name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list); + } + portable_mutex_unlock(&queue->mutex); + if ( item != 0 && offsetflag != 0 ) + return((void *)((long)item + sizeof(struct queueitem))); + else return(item); +} + +void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize,int32_t freeitem) +{ + struct queueitem *item = 0; + lock_queue(queue); + if ( queue->list != 0 ) + { + DL_FOREACH(queue->list,item) + { + if ( item == copy || memcmp((void *)((long)item + sizeof(struct queueitem)),(void *)((long)item + sizeof(struct queueitem)),copysize) == 0 ) + { + DL_DELETE(queue->list,item); + portable_mutex_unlock(&queue->mutex); + printf("name.(%s) deleted item.%p list.%p\n",queue->name,item,queue->list); + if ( freeitem != 0 ) + myfree(item,copysize); + return(item); + } + } + } + portable_mutex_unlock(&queue->mutex); + return(0); +} + +void *queue_free(queue_t *queue) +{ + struct queueitem *item = 0; + lock_queue(queue); + if ( queue->list != 0 ) + { + DL_FOREACH(queue->list,item) + { + DL_DELETE(queue->list,item); + myfree(item,sizeof(struct queueitem)); + } + //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list); + } + portable_mutex_unlock(&queue->mutex); + return(0); +} + +void *queue_clone(queue_t *clone,queue_t *queue,int32_t size) +{ + struct queueitem *ptr,*item = 0; + lock_queue(queue); + if ( queue->list != 0 ) + { + DL_FOREACH(queue->list,item) + { + ptr = mycalloc('c',1,sizeof(*ptr)); + memcpy(ptr,item,size); + queue_enqueue(queue->name,clone,ptr,0); + } + //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list); + } + portable_mutex_unlock(&queue->mutex); + return(0); +} + +int32_t queue_size(queue_t *queue) +{ + int32_t count = 0; + struct queueitem *tmp; + lock_queue(queue); + DL_COUNT(queue->list,tmp,count); + portable_mutex_unlock(&queue->mutex); + return count; +} + +void iguana_memreset(struct OS_memspace *mem) +{ + mem->used = 0; +#ifdef IGUANA_PEERALLOC + mem->availptrs = mem->outofptrs = mem->numptrs = 0; + memset(mem->ptrs,0,sizeof(mem->ptrs)); + memset(mem->maxsizes,0,sizeof(mem->maxsizes)); + memset(mem->allocsizes,0,sizeof(mem->allocsizes)); +#endif + if ( mem->threadsafe != 0 ) + portable_mutex_init(&mem->mutex); +} + +void iguana_mempurge(struct OS_memspace *mem) +{ + if ( mem->allocated != 0 && mem->ptr != 0 && mem->totalsize > 0 ) + myfree(mem->ptr,mem->totalsize), mem->ptr = 0; + iguana_memreset(mem); + mem->totalsize = 0; +} + +void *iguana_meminit(struct OS_memspace *mem,char *name,void *ptr,int64_t totalsize,int32_t threadsafe) +{ + strcpy(mem->name,name); + if ( ptr == 0 ) + { + if ( mem->ptr != 0 && mem->totalsize < totalsize ) + { + iguana_mempurge(mem); + mem->totalsize = 0; + //printf("memptr.%p totalsize.%ld vs totalsize.%ld\n",mem->ptr,(long)mem->totalsize,(long)totalsize); + } //else printf("mem->ptr.%p mem->totalsize %ld\n",mem->ptr,(long)mem->totalsize); + if ( mem->ptr == 0 ) + { + //static long alloc; + //alloc += totalsize; + //char str[65]; printf("iguana_meminit alloc %s\n",mbstr(str,alloc)); + if ( (mem->ptr= mycalloc('d',1,totalsize)) == 0 ) + { + printf("iguana_meminit: cant get %d bytes\n",(int32_t)totalsize); + exit(-1); + return(0); + } + mem->totalsize = totalsize; + } //else printf("memptr.%p\n",mem->ptr); + //printf("meminit.(%s) %d vs %ld\n",mem->name,(int32_t)totalsize,(long)mem->totalsize); + mem->allocated = 1; + } + else + { + iguana_mempurge(mem); + mem->ptr = ptr; + mem->totalsize = totalsize; + } + mem->threadsafe = threadsafe; + iguana_memreset(mem); + if ( mem->totalsize == 0 ) + printf("meminit.%s ILLEGAL STATE null size\n",mem->name), getchar(); + return(mem->ptr); +} + +int64_t iguana_memallocated(struct OS_memspace *mem) +{ + int64_t avail = (mem->totalsize - mem->used); +#ifdef IGUANA_PEERALLOC + int32_t i; + for (i=0; inumptrs; i++) + if ( mem->allocsizes[i] == 0 ) + avail += mem->maxsizes[i]; +#endif + return(avail); +} + +void *iguana_memalloc(struct OS_memspace *mem,long size,int32_t clearflag) +{ + int32_t modval; void *ptr = 0; + //printf("iguana_memalloc.%s size.%ld used.%llu of %llu, numptrs.%d avail.%d %lld\n",mem->name,size,(long long)mem->used,(long long)mem->totalsize,mem->numptrs,mem->availptrs,(long long)iguana_memallocated(mem)); + //if ( mem->threadsafe != 0 ) + // portable_mutex_lock(&mem->mutex); +#ifdef IGUANA_PEERALLOC + if ( mem->availptrs == mem->numptrs && mem->used > (mem->totalsize >> 1) ) + iguana_memreset(mem); +#endif + if ( (mem->used + size) <= mem->totalsize ) + { + ptr = (void *)((uint64_t)mem->ptr + (uint64_t)mem->used); + mem->used += size; + if ( size*clearflag != 0 ) + memset(ptr,0,size); + if ( mem->alignflag != 0 ) + { + if ( (modval= (mem->used % mem->alignflag)) != 0 ) + mem->used += mem->alignflag - modval; + } +#ifdef IGUANA_PEERALLOC + if ( mem->numptrs < sizeof(mem->ptrs)/sizeof(*mem->ptrs) ) + { + mem->allocsizes[mem->numptrs] = mem->maxsizes[mem->numptrs] = (int32_t)size; + mem->ptrs[mem->numptrs++] = ptr; + } + else + { + mem->outofptrs++; + printf("iguana_memalloc: numptrs.%d outofptrs.%d\n",mem->numptrs,mem->outofptrs); + } +#endif + //printf(">>>>>>>>> USED.%s alloc %ld used %ld alloc.%ld -> %s %p\n",mem->name,size,(long)mem->used,(long)mem->totalsize,mem->name,ptr); + } else printf("error memalloc mem.%p %s alloc %ld used %ld totalsize.%ld -> %s %p\n",mem,mem->name,size,(long)mem->used,(long)mem->totalsize,mem->name,ptr), getchar();//exit(-1); + //if ( mem->threadsafe != 0 ) + // portable_mutex_unlock(&mem->mutex); + return(ptr); +} + +int64_t iguana_memfree(struct OS_memspace *mem,void *ptr,int32_t size) +{ +#ifdef IGUANA_PEERALLOC + int32_t i; int64_t avail = -1; + if ( mem->threadsafe != 0 ) + portable_mutex_lock(&mem->mutex); + for (i=0; inumptrs; i++) + { + if ( ptr == mem->ptrs[i] ) + { + if ( mem->allocsizes[i] == size ) + { + mem->availptrs++; + mem->allocsizes[i] = 0; + avail = (mem->totalsize - mem->used); + //printf("avail %llu\n",(long long)avail); + } else printf("iguana_memfree.%s: mismatched size %d for ptr.%p %d\n",mem->name,size,ptr,mem->allocsizes[i]); + if ( mem->threadsafe != 0 ) + portable_mutex_unlock(&mem->mutex); + return(avail); + } + } + if ( mem->threadsafe != 0 ) + portable_mutex_unlock(&mem->mutex); + printf("iguana_memfree: cant find ptr.%p %d\n",ptr,size); + return(avail); +#else + printf("iguana_free not supported without #define IGUANA_PEERALLOC\n"); + return(0); +#endif +} + +int32_t OS_truncate(char *fname,long filesize) +{ + return(OS_portable_truncate(fname,filesize)); +} + +char *OS_compatible_path(char *str) +{ + return(OS_portable_path(str)); +} + +int32_t OS_renamefile(char *fname,char *newfname) +{ + return(OS_portable_renamefile(fname,newfname)); +} + +int32_t OS_removefile(char *fname,int32_t scrubflag) +{ + FILE *fp; long i,fpos; char tmp[512]; + strcpy(tmp,fname); + OS_compatible_path(tmp); + if ( (fp= fopen(tmp,"rb+")) != 0 ) + { + //printf("delete(%s)\n",fname); + if ( scrubflag != 0 ) + { + fseek(fp,0,SEEK_END); + fpos = ftell(fp); + rewind(fp); + for (i=0; i 0 && (len2= fread(buf2,1,sizeof(buf2),fp2)) == len ) + if ( (offset= memcmp(buf,buf2,len)) != 0 ) + printf("compare error at offset.%d: (%s) src.%ld vs. (%s) dest.%ld\n",offset,fname,ftell(fp),fname2,ftell(fp2)), errs++; + fclose(fp2); + } + fclose(fp); + } + return(errs); +} + +int64_t OS_copyfile(char *src,char *dest,int32_t cmpflag) +{ + int64_t allocsize,len = -1; + char *buf; + FILE *srcfp,*destfp; + if ( (srcfp= fopen(OS_compatible_path(src),"rb")) != 0 ) + { + if ( (destfp= fopen(OS_compatible_path(dest),"wb")) != 0 ) + { + allocsize = 1024 * 1024 * 128L; + buf = mycalloc('F',1,allocsize); + while ( (len= fread(buf,1,allocsize,srcfp)) > 0 ) + if ( (long)fwrite(buf,1,len,destfp) != len ) + printf("write error at (%s) src.%ld vs. (%s) dest.%ld\n",src,ftell(srcfp),dest,ftell(destfp)); + len = ftell(destfp); + fclose(destfp); + myfree(buf,allocsize); + } + fclose(srcfp); + } + if ( len < 0 || (cmpflag != 0 && OS_compare_files(src,dest) != 0) ) + printf("Error copying files (%s) -> (%s)\n",src,dest), len = -1; + return(len); +} + +int32_t OS_releasemap(void *ptr,uint64_t filesize) +{ + int32_t retval; + if ( ptr == 0 ) + { + printf("release_map_file: null ptr\n"); + return(-1); + } + retval = munmap(ptr,filesize); + if ( retval != 0 ) + printf("release_map_file: munmap error %p %llu: err %d\n",ptr,(long long)filesize,retval); + return(retval); +} + +void _OS_closemap(struct OS_mappedptr *mp) +{ + if ( mp->actually_allocated != 0 && mp->fileptr != 0 ) + myaligned_free(mp->fileptr,mp->allocsize); + else if ( mp->fileptr != 0 ) + OS_releasemap(mp->fileptr,mp->allocsize); + mp->fileptr = 0; + mp->closetime = (uint32_t)time(NULL); + mp->opentime = 0; +} + +void OS_closemap(struct OS_mappedptr *mp) +{ + struct OS_mappedptr tmp; + tmp = *mp; + _OS_closemap(mp); + memset(mp,0,sizeof(*mp)); + mp->actually_allocated = tmp.actually_allocated; + mp->allocsize = tmp.allocsize; + mp->rwflag = tmp.rwflag; + strcpy(mp->fname,tmp.fname); +} + +long OS_ensurefilesize(char *fname,long filesize,int32_t truncateflag) +{ + FILE *fp; + char *zeroes; + long i,n,allocsize = 0; + //printf("ensure_filesize.(%s) %ld %s | ",fname,filesize,mbstr(filesize)); + if ( (fp= fopen(OS_compatible_path(fname),"rb")) != 0 ) + { + fseek(fp,0,SEEK_END); + allocsize = ftell(fp); + fclose(fp); + //printf("(%s) exists size.%ld\n",fname,allocsize); + } + else + { + //printf("try to create.(%s)\n",fname); + if ( (fp= fopen(OS_compatible_path(fname),"wb")) != 0 ) + fclose(fp); + } + if ( allocsize < filesize ) + { + //printf("filesize.%ld is less than %ld\n",filesize,allocsize); + if ( (fp= fopen(OS_compatible_path(fname),"ab")) != 0 ) + { + zeroes = myaligned_alloc(16L*1024*1024); + memset(zeroes,0,16*1024*1024); + n = filesize - allocsize; + while ( n > 16*1024*1024 ) + { + fwrite(zeroes,1,16*1024*1024,fp); + n -= 16*1024*1024; + fprintf(stderr,"+"); + } + for (i=0; i filesize ) + { + OS_truncate(fname,filesize); + return(filesize); + } + else return(allocsize); +} + +int32_t OS_openmap(struct OS_mappedptr *mp) +{ + uint64_t allocsize = mp->allocsize; + if ( mp->actually_allocated != 0 ) + { + if ( mp->fileptr == 0 ) + mp->fileptr = myaligned_alloc(mp->allocsize); + else memset(mp->fileptr,0,mp->allocsize); + return(0); + } + else + { + if ( mp->fileptr != 0 ) + { + //printf("opening already open mappedptr, pending %p\n",mp->pending); + OS_closemap(mp); + } + mp->allocsize = allocsize; + // printf("calling map_file with expected %ld\n",mp->allocsize); + mp->fileptr = OS_mapfile(mp->fname,&mp->allocsize,mp->rwflag); + if ( mp->fileptr == 0 || mp->allocsize != allocsize ) + { + //printf("error mapping(%s) ptr %p mapped %ld vs allocsize %ld\n",mp->fname,mp->fileptr,mp->allocsize,allocsize); + return(-1); + } + mp->closetime = 0; + mp->opentime = (uint32_t)time(NULL); + } + return(0); +} + +void *OS_mappedptr(void **ptrp,struct OS_mappedptr *mp,uint64_t allocsize,int32_t rwflag,char *fname) +{ + uint64_t filesize; + mp->actually_allocated = 0;//!os_supports_mappedfiles(); + if ( fname != 0 ) + { + if ( strcmp(mp->fname,fname) == 0 ) + { + if ( mp->fileptr != 0 ) + { + OS_releasemap(mp->fileptr,mp->allocsize); + mp->fileptr = 0; + } + OS_openmap(mp); + if ( ptrp != 0 ) + (*ptrp) = mp->fileptr; + return(mp->fileptr); + } + strcpy(mp->fname,fname); + } + else mp->actually_allocated = 1; + mp->rwflag = rwflag; + mp->allocsize = allocsize; + if ( rwflag != 0 && mp->actually_allocated == 0 && allocsize != 0 ) + allocsize = OS_ensurefilesize(fname,allocsize,0); + if ( OS_openmap(mp) != 0 ) + { + char str[65]; + //printf("init_mappedptr %s.rwflag.%d | ",fname,rwflag); + if ( allocsize != 0 ) + printf("error mapping(%s) rwflag.%d ptr %p mapped %llu vs allocsize %llu %s\n",fname,rwflag,mp->fileptr,(long long)mp->allocsize,(long long)allocsize,mbstr(str,allocsize)); + else allocsize = mp->allocsize; + if ( rwflag != 0 && allocsize != mp->allocsize ) + { + filesize = mp->allocsize; + if ( mp->fileptr != 0 ) + OS_releasemap(mp->fileptr,mp->allocsize); + mp->allocsize = allocsize; + mp->changedsize = (allocsize - filesize); + OS_openmap(mp); + if ( mp->fileptr == 0 || mp->allocsize != allocsize ) + { + printf("SECOND error mapping(%s) ptr %p mapped %llu vs allocsize %llu\n",fname,mp->fileptr,(long long)mp->allocsize,(long long)allocsize); + exit(-1); + } + } + } + if ( ptrp != 0 ) + (*ptrp) = mp->fileptr; + return(mp->fileptr); +} + +void *OS_filealloc(struct OS_mappedptr *M,char *fname,struct OS_memspace *mem,long size) +{ + //printf("mem->used %ld size.%ld | size.%ld\n",mem->used,size,mem->size); + //printf("filemalloc.(%s) new space.%ld %s\n",fname,mem->size,mbstr(size)); + memset(M,0,sizeof(*M)); + mem->totalsize = size; + if ( OS_mappedptr(0,M,mem->totalsize,1,fname) == 0 ) + { + printf("couldnt create mapped file.(%s)\n",fname); + exit(-1); + } + mem->ptr = M->fileptr; + mem->used = 0; + return(M->fileptr); +} + +void *OS_loadfile(char *fname,char **bufp,int64_t *lenp,int64_t *allocsizep) +{ + FILE *fp; + int64_t filesize,buflen = *allocsizep; + char *buf = *bufp; + *lenp = 0; + if ( (fp= fopen(OS_compatible_path(fname),"rb")) != 0 ) + { + fseek(fp,0,SEEK_END); + filesize = ftell(fp); + if ( filesize == 0 ) + { + fclose(fp); + *lenp = 0; + return(0); + } + if ( filesize > buflen-1 ) + { + *allocsizep = filesize+1; + *bufp = buf = realloc(buf,(long)*allocsizep); + } + rewind(fp); + if ( buf == 0 ) + printf("Null buf ???\n"); + else + { + if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize ) + printf("error reading filesize.%ld\n",(long)filesize); + buf[filesize] = 0; + } + fclose(fp); + *lenp = filesize; + } + return(buf); +} + +void *OS_filestr(int64_t *allocsizep,char *fname) +{ + int64_t filesize = 0; char *buf = 0; + *allocsizep = 0; + return(OS_loadfile(fname,&buf,&filesize,allocsizep)); +} + +// following functions cant be fully implemented in one or more OS +void *OS_mapfile(char *fname,long *filesizep,int32_t enablewrite) // win and pnacl dont have mmap64 +{ + return(OS_portable_mapfile(fname,filesizep,enablewrite)); +} + +int32_t OS_syncmap(struct OS_mappedptr *mp,long len) // pnacl doesnt implement sync +{ + return(OS_portable_syncmap(mp,len)); +} + +void *OS_tmpalloc(char *dirname,char *name,struct OS_memspace *mem,long origsize) // no syncmap no tmpalloc +{ + return(OS_portable_tmpalloc(dirname,name,mem,origsize)); +} + +void OS_init() +{ + return(OS_portable_init()); +} \ No newline at end of file diff --git a/crypto777/iguana_utils.c b/crypto777/iguana_utils.c new file mode 100755 index 000000000..2b6dd9427 --- /dev/null +++ b/crypto777/iguana_utils.c @@ -0,0 +1,819 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "../iguana/iguana777.h" + +bits256 bits256_doublesha256(char *hashstr,uint8_t *data,int32_t datalen) +{ + bits256 hash,hash2; int32_t i; + vcalc_sha256(0,hash.bytes,data,datalen); + vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash)); + for (i=0; i b.ulongs[i] ) + return(1); + else if ( a.ulongs[i] < b.ulongs[i] ) + return(-1); + } + return(0); +} + +bits256 bits256_lshift(bits256 x) +{ + int32_t i,carry,prevcarry = 0; uint64_t mask = (1LL << 63); + for (i=0; i<4; i++) + { + carry = ((mask & x.ulongs[i]) != 0); + x.ulongs[i] = (x.ulongs[i] << 1) | prevcarry; + prevcarry = carry; + } + return(x); +} + +bits256 bits256_from_compact(uint32_t c) +{ + uint32_t nbytes,nbits,i; bits256 x; + memset(x.bytes,0,sizeof(x)); + nbytes = (c >> 24) & 0xFF; + nbits = (8 * (nbytes - 3)); + x.ulongs[0] = c & 0xFFFFFF; + for (i=0; i sizeof(buf) ) + { + printf("calc_OP_HASH160 overflow len.%d vs %d\n",len,(int32_t)sizeof(buf)); + return; + } + decode_hex(buf,len,pubkey); + vcalc_sha256(0,sha256,buf,len); + calc_rmd160(0,hash160,sha256,sizeof(sha256)); + if ( 0 ) + { + int i; + for (i=0; i<20; i++) + printf("%02x",hash160[i]); + printf("<- (%s)\n",pubkey); + } + if ( hexstr != 0 ) + init_hexbytes_noT(hexstr,hash160,20); +} + +double _dxblend(double *destp,double val,double decay) +{ + double oldval; + if ( (oldval = *destp) != 0. ) + return((oldval * decay) + ((1. - decay) * val)); + else return(val); +} + +double dxblend(double *destp,double val,double decay) +{ + double newval,slope; + if ( isnan(*destp) != 0 ) + *destp = 0.; + if ( isnan(val) != 0 ) + return(0.); + if ( *destp == 0 ) + { + *destp = val; + return(0); + } + newval = _dxblend(destp,val,decay); + if ( newval < SMALLVAL && newval > -SMALLVAL ) + { + // non-zero marker for actual values close to or even equal to zero + if ( newval < 0. ) + newval = -SMALLVAL; + else newval = SMALLVAL; + } + if ( *destp != 0. && newval != 0. ) + slope = (newval - *destp); + else slope = 0.; + *destp = newval; + return(slope); +} + +/*queue_t TerminateQ; int32_t TerminateQ_queued; +void iguana_terminator(void *arg) +{ + struct iguana_thread *t; uint32_t lastdisp = 0; int32_t terminated = 0; + printf("iguana_terminator\n"); + while ( 1 ) + { + if ( (t= queue_dequeue(&TerminateQ,0)) != 0 ) + { + printf("terminate.%p\n",t); + iguana_terminate(t); + terminated++; + continue; + } + sleep(1); + if ( time(NULL) > lastdisp+60 ) + { + lastdisp = (uint32_t)time(NULL); + printf("TerminateQ %d terminated of %d queued\n",terminated,TerminateQ_queued); + } + } +}*/ + + +int32_t iguana_numthreads(struct iguana_info *coin,int32_t mask) +{ + int32_t i,sum = 0; + for (i=0; i<8; i++) + if ( ((1 << i) & mask) != 0 ) + sum += (coin->Launched[i] - coin->Terminated[i]); + return(sum); +} + +void iguana_launcher(void *ptr) +{ + struct iguana_thread *t = ptr; struct iguana_info *coin; + coin = t->coin; + t->funcp(t->arg); + coin->Terminated[t->type % (sizeof(coin->Terminated)/sizeof(*coin->Terminated))]++; + queue_enqueue("TerminateQ",&coin->TerminateQ,&t->DL,0); +} + +void iguana_terminate(struct iguana_info *coin,struct iguana_thread *t) +{ + int32_t retval; + retval = pthread_join(t->handle,NULL); + if ( retval != 0 ) + printf("error.%d terminating t.%p thread.%s\n",retval,t,t->name); + myfree(t,sizeof(*t)); +} + +struct iguana_thread *iguana_launch(struct iguana_info *coin,char *name,iguana_func funcp,void *arg,uint8_t type) +{ + int32_t retval; struct iguana_thread *t; + t = mycalloc('Z',1,sizeof(*t)); + strcpy(t->name,name); + t->coin = coin; + t->funcp = funcp; + t->arg = arg; + t->type = (type % (sizeof(coin->Terminated)/sizeof(*coin->Terminated))); + coin->Launched[t->type]++; + retval = OS_thread_create(&t->handle,NULL,(void *)iguana_launcher,(void *)t); + if ( retval != 0 ) + printf("error launching %s\n",t->name); + while ( (t= queue_dequeue(&coin->TerminateQ,0)) != 0 ) + { + if ( (rand() % 100000) == 0 ) + printf("terminated.%d launched.%d terminate.%p\n",coin->Terminated[t->type],coin->Launched[t->type],t); + iguana_terminate(coin,t); + } + return(t); +} + +char hexbyte(int32_t c) +{ + c &= 0xf; + if ( c < 10 ) + return('0'+c); + else if ( c < 16 ) + return('a'+c-10); + else return(0); +} + +int32_t _unhex(char c) +{ + if ( c >= '0' && c <= '9' ) + return(c - '0'); + else if ( c >= 'a' && c <= 'f' ) + return(c - 'a' + 10); + else if ( c >= 'A' && c <= 'F' ) + return(c - 'A' + 10); + return(-1); +} + +int32_t is_hexstr(char *str,int32_t n) +{ + int32_t i; + if ( str == 0 || str[0] == 0 ) + return(0); + for (i=0; str[i]!=0&&(i %d hex.(%s) [n*2+1: %d] [n*2: %d %c] len.%ld\n",n,hex[0],bytes[0],hex,hex[n*2+1],hex[n*2],hex[n*2],(long)strlen(hex)); +#ifdef __APPLE__ + getchar(); +#endif + bytes++; + hex++; + adjust = 1; + } else adjust = 0; + if ( n > 0 ) + { + for (i=0; i>4) & 0xf); + hexbytes[i*2 + 1] = hexbyte(message[i] & 0xf); + //printf("i.%d (%02x) [%c%c]\n",i,message[i],hexbytes[i*2],hexbytes[i*2+1]); + } + hexbytes[len*2] = 0; + //printf("len.%ld\n",len*2+1); + return((int32_t)len*2+1); +} + +void touppercase(char *str) +{ + int32_t i; + if ( str == 0 || str[0] == 0 ) + return; + for (i=0; str[i]!=0; i++) + str[i] = toupper(((int32_t)str[i])); +} + +long _stripwhite(char *buf,int accept) +{ + int32_t i,j,c; + if ( buf == 0 || buf[0] == 0 ) + return(0); + for (i=j=0; buf[i]!=0; i++) + { + buf[j] = c = buf[i]; + if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') ) + j++; + } + buf[j] = 0; + return(j); +} + +char *clonestr(char *str) +{ + char *clone; + if ( str == 0 || str[0] == 0 ) + { + printf("warning cloning nullstr.%p\n",str); +#ifdef __APPLE__ + while ( 1 ) sleep(1); +#endif + str = (char *)""; + } + clone = (char *)malloc(strlen(str)+16); + strcpy(clone,str); + return(clone); +} + + +int32_t safecopy(char *dest,char *src,long len) +{ + int32_t i = -1; + if ( dest != 0 ) + memset(dest,0,len); + if ( src != 0 && dest != 0 ) + { + for (i=0; i= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ) + escaped[j++] = c; + else + { + sprintf(esc,"%%%02X",c); + //sprintf(esc,"\\\\%c",c); + strcpy(escaped + j,esc); + j += strlen(esc); + } + } + escaped[j] = 0; + //printf("escape_code: (%s) -> (%s)\n",str,escaped); +} + +int32_t is_zeroes(char *str) +{ + int32_t i; + if ( str == 0 || str[0] == 0 ) + return(1); + for (i=0; str[i]!=0; i++) + if ( str[i] != '0' ) + return(0); + return(1); +} + +int64_t conv_floatstr(char *numstr) +{ + double val,corr; + val = atof(numstr); + corr = (val < 0.) ? -0.50000000001 : 0.50000000001; + return((int64_t)(val * SATOSHIDEN + corr)); +} + +int32_t has_backslash(char *str) +{ + int32_t i; + if ( str == 0 || str[0] == 0 ) + return(0); + for (i=0; str[i]!=0; i++) + if ( str[i] == '\\' ) + return(1); + return(0); +} + +/*int32_t iguana_sortbignum(void *buf,int32_t size,uint32_t num,int32_t structsize,int32_t dir) +{ + int32_t retval = 0; + if ( dir > 0 ) + { + if ( size == 32 ) + qsort(buf,num,structsize,_increasing_bits256); + else if ( size == 20 ) + qsort(buf,num,structsize,_increasing_rmd160); + else retval = -1; + } + else + { + if ( size == 32 ) + qsort(buf,num,structsize,_decreasing_bits256); + else if ( size == 20 ) + qsort(buf,num,structsize,_decreasing_rmd160); + else retval = -1; + } + if ( retval < 0 ) + printf("iguana_sortbignum only does bits256 and rmd160 for now\n"); + return(retval); +} +*/ + +void tolowercase(char *str) +{ + int32_t i; + if ( str == 0 || str[0] == 0 ) + return; + for (i=0; str[i]!=0; i++) + str[i] = tolower(((int32_t)str[i])); +} + +int32_t is_decimalstr(char *str) +{ + int32_t i; + if ( str == 0 || str[0] == 0 ) + return(0); + for (i=0; str[i]!=0; i++) + if ( str[i] < '0' || str[i] > '9' ) + return(0); + return(i); +} + +int32_t unstringbits(char *buf,uint64_t bits) +{ + int32_t i; + for (i=0; i<8; i++,bits>>=8) + if ( (buf[i]= (char)(bits & 0xff)) == 0 ) + break; + buf[i] = 0; + return(i); +} + +uint64_t stringbits(char *str) +{ + uint64_t bits = 0; + if ( str == 0 ) + return(0); + int32_t i,n = (int32_t)strlen(str); + if ( n > 8 ) + n = 8; + for (i=n-1; i>=0; i--) + bits = (bits << 8) | (str[i] & 0xff); + //printf("(%s) -> %llx %llu\n",str,(long long)bits,(long long)bits); + return(bits); +} + +char *unstringify(char *str) +{ + int32_t i,j,n; + if ( str == 0 ) + return(0); + else if ( str[0] == 0 ) + return(str); + n = (int32_t)strlen(str); + if ( str[0] == '"' && str[n-1] == '"' ) + str[n-1] = 0, i = 1; + else i = 0; + for (j=0; str[i]!=0; i++) + { + if ( str[i] == '\\' && (str[i+1] == 't' || str[i+1] == 'n' || str[i+1] == 'b' || str[i+1] == 'r') ) + i++; + else if ( str[i] == '\\' && str[i+1] == '"' ) + str[j++] = '"', i++; + else str[j++] = str[i]; + } + str[j] = 0; + return(str); +} + +void reverse_hexstr(char *str) +{ + int i,n; + char *rev; + n = (int32_t)strlen(str); + rev = (char *)malloc(n + 1); + for (i=0; i= out_len) + return -ENOBUFS; + + out [io] = '\0'; + + return io; +} +/* + NXT address converter, + Ported from original javascript (nxtchg) + To C by Jones + */ + +int32_t gexp[] = {1, 2, 4, 8, 16, 5, 10, 20, 13, 26, 17, 7, 14, 28, 29, 31, 27, 19, 3, 6, 12, 24, 21, 15, 30, 25, 23, 11, 22, 9, 18, 1}; +int32_t glog[] = {0, 0, 1, 18, 2, 5, 19, 11, 3, 29, 6, 27, 20, 8, 12, 23, 4, 10, 30, 17, 7, 22, 28, 26, 21, 25, 9, 16, 13, 14, 24, 15}; +int32_t cwmap[] = {3, 2, 1, 0, 7, 6, 5, 4, 13, 14, 15, 16, 12, 8, 9, 10, 11}; +char alphabet[] = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; + +int32_t gmult(int32_t a,int32_t b) +{ + if ( a == 0 || b == 0 ) + return 0; + int32_t idx = (glog[a] + glog[b]) % 31; + return gexp[idx]; +} + +int32_t letterval(char letter) +{ + int32_t ret = 0; + if ( letter < '9' ) + ret = letter - '2'; + else + { + ret = letter - 'A' + 8; + if ( letter > 'I' ) + ret--; + if ( letter > 'O' ) + ret--; + } + return ret; +} + +uint64_t RS_decode(char *rs) +{ + int32_t code[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + int32_t i,p = 4; + if ( strncmp("NXT-",rs,4) != 0 ) + return(0); + for (i=0; i<17; i++) + { + code[cwmap[i]] = letterval(rs[p]); + p++; + if ( rs[p] == '-' ) + p++; + } + uint64_t out = 0; + for (i=12; i>=0; i--) + out = out * 32 + code[i]; + return out; +} + +int32_t RS_encode(char *rsaddr,uint64_t id) +{ + int32_t a,code[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + int32_t inp[32],out[32],i,j,fb,pos = 0,len = 0; + char acc[64]; + rsaddr[0] = 0; + memset(inp,0,sizeof(inp)); + memset(out,0,sizeof(out)); + memset(acc,0,sizeof(acc)); + expand_nxt64bits(acc,id); + //sprintf(acc,"%llu",(long long)id); + for (a=0; *(acc+a) != '\0'; a++) + len++; + if ( len == 20 && *acc != '1' ) + { + printf("error (%s) doesnt start with 1",acc); + return(-1); + } + for (i=0; i= 32) + { + inp[newlen++] = divide >> 5; + divide &= 31; + } + else if ( newlen > 0 ) + inp[newlen++] = 0; + } + len = newlen; + out[pos++] = divide; + } while ( newlen != 0 ); + for (i=0; i<13; i++) // copy to code in reverse, pad with 0's + code[i] = (--pos >= 0 ? out[i] : 0); + int32_t p[] = {0, 0, 0, 0}; + for (i=12; i>=0; i--) + { + fb = code[i] ^ p[3]; + p[3] = p[2] ^ gmult(30, fb); + p[2] = p[1] ^ gmult(6, fb); + p[1] = p[0] ^ gmult(9, fb); + p[0] = gmult(17, fb); + } + code[13] = p[0]; + code[14] = p[1]; + code[15] = p[2]; + code[16] = p[3]; + strcpy(rsaddr,"NXT-"); + j=4; + for (i=0; i<17; i++) + { + rsaddr[j++] = alphabet[code[cwmap[i]]]; + if ( (j % 5) == 3 && j < 20 ) + rsaddr[j++] = '-'; + } + rsaddr[j] = 0; + return(0); +} + +void calc_base64_encodestr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + nn_base64_encode(msg,len,hexstr,64); +} + +void calc_base64_decodestr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + nn_base64_decode((void *)msg,len,(void *)hexstr,1024); +} + +void sha256_sha256(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + bits256_doublesha256(hexstr,msg,len); +} + +void rmd160ofsha256(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + uint8_t sha256[32]; + vcalc_sha256(0,sha256,(void *)msg,len); + calc_rmd160(hexstr,buf,sha256,sizeof(sha256)); +} + +void calc_md5str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + bits128 x; + calc_md5(hexstr,msg,len); + decode_hex(buf,sizeof(x),hexstr); + memcpy(buf,x.bytes,sizeof(x)); +} + +void calc_crc32str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + uint32_t crc = calc_crc32(0,msg,len); + init_hexbytes_noT(hexstr,(uint8_t *)&crc,sizeof(crc)); +} + +void calc_NXTaddr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + uint8_t mysecret[32]; uint64_t nxt64bits; + nxt64bits = conv_NXTpassword(mysecret,buf,msg,len); + RS_encode(hexstr,nxt64bits); +} + +void calc_curve25519_str(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len) +{ + bits256 x = curve25519(*(bits256 *)msg,curve25519_basepoint9()); + init_hexbytes_noT(hexstr,x.bytes,sizeof(x)); +} diff --git a/crypto777/inet.c b/crypto777/inet.c new file mode 100755 index 000000000..f5c0f1777 --- /dev/null +++ b/crypto777/inet.c @@ -0,0 +1,605 @@ +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + + +#ifdef DEFINES_ONLY +#ifndef crypto777_inet_h +#define crypto777_inet_h +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#else +#include "../includes/nonportable.h" +#endif +#include +#include +#include + +#endif +#else + +#ifndef crypto777_system777_c +#define crypto777_system777_c + +#ifndef crypto777_system777_h +#define DEFINES_ONLY +#include "inet.c" +#undef DEFINES_ONLY +#endif + +static int inet_ntop4(unsigned char *src, char *dst, size_t size); +static int inet_ntop6(unsigned char *src, char *dst, size_t size); +static int inet_pton4(char *src, unsigned char *dst); +static int inet_pton6(char *src, unsigned char *dst); + +int32_t portable_ntop(int af, void* src, char* dst, size_t size) +{ + switch (af) { + case AF_INET: + return (inet_ntop4(src, dst, size)); + case AF_INET6: + return (inet_ntop6(src, dst, size)); + default: + return -1; + } + /* NOTREACHED */ +} + + +static int inet_ntop4(unsigned char *src, char *dst, size_t size) { + static const char fmt[] = "%u.%u.%u.%u"; + char tmp[sizeof "255.255.255.255"]; + int l; + +#ifndef _WIN32 + l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); +#else + l = _snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); +#endif + if (l <= 0 || (size_t) l >= size) { + return -1; + } + strncpy(dst, tmp, size); + dst[size - 1] = '\0'; + return 0; +} + + +static int inet_ntop6(unsigned char *src, char *dst, size_t size) { + /* + * Note that int32_t and int16_t need only be "at least" large enough + * to contain a value of the specified size. On some systems, like + * Crays, there is no such thing as an integer variable with 16 bits. + * Keep this in mind if you think this function should have been coded + * to use pointer overlays. All the world's not a VAX. + */ + char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; + struct { int base, len; } best, cur; + unsigned int words[sizeof(struct in6_addr) / sizeof(uint16_t)]; + int i; + + /* + * Preprocess: + * Copy the input (bytewise) array into a wordwise array. + * Find the longest run of 0x00's in src[] for :: shorthanding. + */ + memset(words, '\0', sizeof words); + for (i = 0; i < (int) sizeof(struct in6_addr); i++) + words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); + best.base = -1; + best.len = 0; + cur.base = -1; + cur.len = 0; + for (i = 0; i < (int)(sizeof(struct in6_addr) / sizeof(uint16_t)); i++) { + if (words[i] == 0) { + if (cur.base == -1) + cur.base = i, cur.len = 1; + else + cur.len++; + } else { + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + cur.base = -1; + } + } + } + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + } + if (best.base != -1 && best.len < 2) + best.base = -1; + + /* + * Format the result. + */ + tp = tmp; + for (i = 0; i < (int)(sizeof(struct in6_addr) / sizeof(uint16_t)); i++) { + /* Are we inside the best run of 0x00's? */ + if (best.base != -1 && i >= best.base && + i < (best.base + best.len)) { + if (i == best.base) + *tp++ = ':'; + continue; + } + /* Are we following an initial run of 0x00s or any real hex? */ + if (i != 0) + *tp++ = ':'; + /* Is this address an encapsulated IPv4? */ + if (i == 6 && best.base == 0 && (best.len == 6 || + (best.len == 7 && words[7] != 0x0001) || + (best.len == 5 && words[5] == 0xffff))) { + int err = inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)); + if (err) + return err; + tp += strlen(tp); + break; + } + tp += sprintf(tp, "%x", words[i]); + } + /* Was it a trailing run of 0x00's? */ + if (best.base != -1 && (best.base + best.len) == (sizeof(struct in6_addr) / sizeof(uint16_t))) + *tp++ = ':'; + *tp++ = '\0'; + + /* + * Check for overflow, copy, and we're done. + */ + if ((size_t)(tp - tmp) > size) { + return ENOSPC; + } + strcpy(dst, tmp); + return 0; +} + + +int portable_pton(int af, char* src, void* dst) +{ + switch (af) { + case AF_INET: + return (inet_pton4(src, dst)); + case AF_INET6: + return (inet_pton6(src, dst)); + default: + return EAFNOSUPPORT; + } + /* NOTREACHED */ +} + + +static int inet_pton4(char *src, unsigned char *dst) { + static const char digits[] = "0123456789"; + int saw_digit, octets, ch; + unsigned char tmp[sizeof(struct in_addr)], *tp; + char savestr[64]; + strcpy(savestr,src); + + //printf("inet_pton4(%s)\n",src); + saw_digit = 0; + octets = 0; + *(tp = tmp) = 0; + while ((ch = (uint8_t)*src++) != '\0') + { + char *pch; + if ( (pch = strchr(digits, ch)) != NULL ) + { + unsigned int nw = (unsigned int)(*tp * 10 + (pch - digits)); + if (saw_digit && *tp == 0) + { + printf("inet_pton4 0\n"); + return EINVAL; + } + if ( nw > 255 ) + { + printf("inet_pton4 1\n"); + return EINVAL; + } + *tp = nw; + if (!saw_digit) { + if (++octets > 4) + { + printf("inet_pton4 2\n"); + return EINVAL; + } + saw_digit = 1; + } + } else if (ch == '.' && saw_digit) { + if (octets == 4) + { + printf("inet_pton4 3\n"); + return EINVAL; + } + *++tp = 0; + saw_digit = 0; + } else + { + printf("inet_pton4 4 error.(%s)\n",savestr); getchar(); + return EINVAL; + } + } + if (octets < 4) + { + printf("inet_pton4 5 error.(%s)\n",savestr); getchar(); + return EINVAL; + } + memcpy(dst, tmp, sizeof(struct in_addr)); + //printf("not errors %08x\n",*(int32_t *)dst); + return 0; +} + + +static int inet_pton6(char *src, unsigned char *dst) { + static char xdigits_l[] = "0123456789abcdef", + xdigits_u[] = "0123456789ABCDEF"; + unsigned char tmp[sizeof(struct in6_addr)], *tp, *endp, *colonp; + char *xdigits, *curtok; + int ch, seen_xdigits; + unsigned int val; + + memset((tp = tmp), '\0', sizeof tmp); + endp = tp + sizeof tmp; + colonp = NULL; + /* Leading :: requires some special handling. */ + if (*src == ':') + if (*++src != ':') + return EINVAL; + curtok = src; + seen_xdigits = 0; + val = 0; + while ((ch = *src++) != '\0' && ch != '%') { + char *pch; + + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + pch = strchr((xdigits = xdigits_u), ch); + if (pch != NULL) { + val <<= 4; + val |= (pch - xdigits); + if (++seen_xdigits > 4) + return EINVAL; + continue; + } + if (ch == ':') { + curtok = src; + if (!seen_xdigits) { + if (colonp) + return EINVAL; + colonp = tp; + continue; + } else if (*src == '\0') { + return EINVAL; + } + if (tp + sizeof(uint16_t) > endp) + return EINVAL; + *tp++ = (unsigned char) (val >> 8) & 0xff; + *tp++ = (unsigned char) val & 0xff; + seen_xdigits = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) { + int err; + + /* Scope id present, parse ipv4 addr without it */ + pch = strchr(curtok, '%'); + if (pch != NULL) { + char tmp2[sizeof "255.255.255.255"]; + + memcpy(tmp2, curtok, pch - curtok); + curtok = tmp2; + src = pch; + } + + err = inet_pton4(curtok, tp); + if (err == 0) { + tp += sizeof(struct in_addr); + seen_xdigits = 0; + break; /*%< '\\0' was seen by inet_pton4(). */ + } + } + return EINVAL; + } + if (seen_xdigits) { + if (tp + sizeof(uint16_t) > endp) + return EINVAL; + *tp++ = (unsigned char) (val >> 8) & 0xff; + *tp++ = (unsigned char) val & 0xff; + } + if (colonp != NULL) { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + int n = (int)(tp - colonp); + int i; + + if (tp == endp) + return EINVAL; + for (i = 1; i <= n; i++) { + endp[- i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + if (tp != endp) + return EINVAL; + memcpy(dst, tmp, sizeof tmp); + return 0; +} + +int32_t parse_ipaddr(char *ipaddr,char *ip_port) +{ + int32_t j,port = 0; + if ( ip_port != 0 && ip_port[0] != 0 ) + { + strcpy(ipaddr,ip_port); + for (j=0; ipaddr[j]!=0&&j<60; j++) + if ( ipaddr[j] == ':' ) + { + port = atoi(ipaddr+j+1); + break; + } + ipaddr[j] = 0; + //printf("%p.(%s) -> (%s:%d)\n",ip_port,ip_port,ipaddr,port); + } else strcpy(ipaddr,"127.0.0.1"); + return(port); +} + +uint64_t _calc_ipbits(char *ip_port) +{ + int32_t port; + char ipaddr[64]; + struct sockaddr_in addr; + port = parse_ipaddr(ipaddr,ip_port); + memset(&addr,0,sizeof(addr)); + portable_pton(ip_port[0] == '[' ? AF_INET6 : AF_INET,ipaddr,&addr); + if ( 0 ) + { + int i; + for (i=0; i<16; i++) + printf("%02x ",((uint8_t *)&addr)[i]); + printf("<- %s %x\n",ip_port,*(uint32_t *)&addr); + } + return(*(uint32_t *)&addr | ((uint64_t)port << 32)); +} + +void expand_ipbits(char *ipaddr,uint64_t ipbits) +{ + uint16_t port; + struct sockaddr_in addr; + memset(&addr,0,sizeof(addr)); + *(uint32_t *)&addr = (uint32_t)ipbits; + portable_ntop(AF_INET,&addr,ipaddr,64); + if ( (port= (uint16_t)(ipbits>>32)) != 0 ) + sprintf(ipaddr + strlen(ipaddr),":%d",port); + //sprintf(ipaddr,"%d.%d.%d.%d",(ipbits>>24)&0xff,(ipbits>>16)&0xff,(ipbits>>8)&0xff,(ipbits&0xff)); +} + +uint64_t calc_ipbits(char *ip_port) +{ + uint64_t ipbits; char ipaddr[64]; + ipbits = _calc_ipbits(ip_port); + expand_ipbits(ipaddr,ipbits); + if ( ipbits != 0 && strcmp(ipaddr,ip_port) != 0 ) + printf("calc_ipbits error: (%s) -> %llx -> (%s)\n",ip_port,(long long)ipbits,ipaddr); + return(ipbits); +} + +char *ipbits_str(char ipaddr[64],uint64_t ipbits) +{ + expand_ipbits(ipaddr,ipbits); + return(ipaddr); +} + +uint32_t is_ipaddr(char *str) +{ + uint64_t ipbits; char ipaddr[64]; + if ( str != 0 && str[0] != 0 && (ipbits= calc_ipbits(str)) != 0 ) + { + expand_ipbits(ipaddr,(uint32_t)ipbits); + if ( strncmp(ipaddr,str,strlen(ipaddr)) == 0 ) + return((uint32_t)ipbits); + } + // printf("(%s) is not ipaddr\n",str); + return(0); +} + +/*int32_t conv_domain(struct sockaddr_storage *ss,const char *addr,int32_t ipv4only) +{ + //struct nn_dns dns; struct nn_dns_result dns_result; + size_t addrlen,sslen; + const char *semicolon,*hostname,*colon,*end; + addrlen = strlen(addr); + semicolon = strchr(addr,';'); + hostname = semicolon ? semicolon + 1 : addr; + colon = strrchr(addr,':'); + end = addr + addrlen; + if ( nn_slow(!colon) ) // Parse the port + return -EINVAL; + if ( nn_slow(nn_port_resolve (colon + 1, end - colon - 1) < 0) ) + return -EINVAL; + // Check whether the host portion of the address is either a literal or a valid hostname. + if ( nn_dns_check_hostname(hostname,colon - hostname) < 0 && nn_literal_resolve(hostname,colon - hostname,ipv4only,ss,&sslen) < 0 ) + return -EINVAL; + if ( semicolon != 0 && nn_iface_resolve(addr,semicolon - addr,ipv4only,ss,&sslen) < 0 ) // If local address is specified, check whether it is valid + return -ENODEV; + //memset(&dns_result,0,sizeof(dns_result)); + // nn_dns_start(&dns,addr,addrlen,ipv4only,&dns_result); + // while ( *(uint32_t *)&dns_result.addr == 0 ) + // usleep(10000); + return(0); +}*/ + +uint32_t conv_domainname(char *ipaddr,char *domain) +{ + int32_t conv_domain(struct sockaddr_storage *ss,const char *addr,int32_t ipv4only); + int32_t ipv4only = 1; + uint32_t ipbits; + struct sockaddr_in ss; + if ( 0 && conv_domain((struct sockaddr_storage *)&ss,(const char *)domain,ipv4only) == 0 ) + { + ipbits = *(uint32_t *)&ss.sin_addr; + expand_ipbits(ipaddr,ipbits); + if ( (uint32_t)calc_ipbits(ipaddr) == ipbits ) + return(ipbits); + //printf("conv_domainname (%s) -> (%s)\n",domain,ipaddr); + } //else printf("error conv_domain.(%s)\n",domain); + return(0); +} + +int32_t notlocalip(char *ipaddr) +{ + if ( ipaddr == 0 || ipaddr[0] == 0 || strcmp("127.0.0.1",ipaddr) == 0 || strncmp("192.168",ipaddr,7) == 0 ) + return(0); + else return(1); +} + +int32_t is_remote_access(char *previpaddr) +{ + if ( notlocalip(previpaddr) != 0 ) + return(1); + else return(0); +} +/*struct sockaddr_in conv_ipbits(uint64_t ipbits) + { + char ipaddr[64]; + uint16_t port; + struct hostent *host; + struct sockaddr_in server_addr; + port = (uint16_t)(ipbits>>32); + ipbits = (uint32_t)ipbits; + expand_ipbits(ipaddr,ipbits); + host = (struct hostent *)gethostbyname(ipaddr); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + server_addr.sin_addr = *((struct in_addr *)host->h_addr); + memset(&(server_addr.sin_zero),0,8); + return(server_addr); + }*/ + +char *conv_ipv6(char *ipv6addr) +{ + unsigned char IPV4CHECK[10]; // 80 ZERO BITS for testing + char ipv4str[4096]; + struct sockaddr_in6 ipv6sa; + in_addr_t *ipv4bin; + unsigned char *bytes; + int32_t isok; + memset(IPV4CHECK,0,sizeof(IPV4CHECK)); + strcpy(ipv4str,ipv6addr); + //isok = !uv_inet_pton(AF_INET,(const char*)ipv6addr,&ipv6sa.sin6_addr); + //printf("isok.%d\n",isok); + isok = portable_pton(AF_INET6,ipv6addr,&ipv6sa.sin6_addr); + if ( isok == 0 ) + { + bytes = ((struct sockaddr_in6 *)&ipv6sa)->sin6_addr.s6_addr; + if ( memcmp(bytes,IPV4CHECK,sizeof(IPV4CHECK)) != 0 ) // check its IPV4 really + { + bytes += 12; + ipv4bin = (in_addr_t *)bytes; +#ifndef _WIN32 + if ( portable_ntop(AF_INET,ipv4bin,ipv4str,sizeof(ipv4str)) == 0 ) +#endif + isok = 0; + } else isok = 0; + } + if ( isok != 0 ) + strcpy(ipv6addr,ipv4str); + return(ipv6addr); // it is ipv4 now +} + +uint16_t parse_endpoint(int32_t *ip6flagp,char *transport,char *ipbuf,char *retbuf,char *endpoint,uint16_t default_port) +{ + //int32_t myatoi(char *str,int32_t range); + char *valids[] = { "tcp", "ws", "ipc", "inproc", "tcpmux" }; + char tmp[128],*inet = 0,*ipaddr = 0; uint64_t ipbits; int32_t i,j,n,port = 0; + ipbuf[0] = retbuf[0] = 0; + *ip6flagp = 0; + if ( endpoint != 0 && strlen(endpoint) > 6 ) + { + for (i=0; i0; j--) + { + if ( ipaddr[j] == ':' ) + { + if ( (port= atoi(ipaddr + j + 1)) < 0 || port >= (1 << 16) ) + { + if ( ipaddr[j-1] == ']' ) + ipaddr[j] = 0; + else ipaddr = 0; + break; + } + } + else if ( ipaddr[j] == ']' ) + { + if ( j == n-1 ) + port = default_port; + break; + } + } + } + else + { + inet = "ip4"; + for (j=n-1; j>0; j--) + { + if ( ipaddr[j] == ':' ) + { + if ( (port= atoi(ipaddr + j + 1)) < 0 || port >= (1 << 16) ) + ipaddr = 0; + break; + } + } + } + if ( ipaddr != 0 ) + { + ipbits = calc_ipbits(ipaddr); + expand_ipbits(tmp,ipbits); + if ( strcmp(tmp,ipaddr) != 0 ) + ipaddr = 0, sprintf(retbuf,"{\"result\":\"illegal ipaddr\",\"endpoint\":\"%s\",\"ipaddr\":\"%s\",\"checkaddr\":\"%s\"}",endpoint,ipaddr,tmp); + } + if ( inet != 0 && ipaddr != 0 && port != 0 ) + { + sprintf(retbuf,"{\"result\":\"ip6 endpoint\",\"endpoint\":\"%s\",\"transport\":\"%s\",\"ipaddr\":\"%s\",\"port\":%d}",endpoint,valids[i],ipaddr,port); + if ( transport[0] == 0 ) + strcpy(transport,valids[i]); + strcpy(ipbuf,ipaddr); + return(port); + } + } + sprintf(retbuf,"{\"result\":\"illegal endpoint\",\"endpoint\":\"%s\"}",endpoint); + } else sprintf(retbuf,"{\"error\":\"no mode specified\"}"); + *ip6flagp = 0; + return(0); +} + +#endif +#endif + diff --git a/crypto777/jpeg/Makefile b/crypto777/jpeg/Makefile new file mode 100644 index 000000000..f0817a9e8 --- /dev/null +++ b/crypto777/jpeg/Makefile @@ -0,0 +1,1094 @@ +# Makefile.in generated by automake 1.11.2 from Makefile.am. +# Makefile. Generated from Makefile.in by configure. + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software +# Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + + +# +# Automake Makefile for the JPEG library +# +# This file is written by Bob Friesenhahn, Guido Vollbeding +# + + + + +pkgdatadir = $(datadir)/libjpeg +pkgincludedir = $(includedir)/libjpeg +pkglibdir = $(libdir)/libjpeg +pkglibexecdir = $(libexecdir)/libjpeg +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = x86_64-apple-darwin13.2.0 +host_triplet = x86_64-apple-darwin13.2.0 +target_triplet = x86_64-apple-darwin13.2.0 +ANSI2KNR = +#am__append_1 = -Wl,--version-script=$(srcdir)/libjpeg.map +bin_PROGRAMS = cjpeg$(EXEEXT) djpeg$(EXEEXT) jpegtran$(EXEEXT) \ + rdjpgcom$(EXEEXT) wrjpgcom$(EXEEXT) +subdir = . +DIST_COMMON = README $(srcdir)/Makefile.in $(srcdir)/Makefile.am \ + $(top_srcdir)/configure $(am__configure_deps) \ + $(srcdir)/jconfig.cfg ansi2knr.c ansi2knr.1 depcomp \ + $(include_HEADERS) $(noinst_HEADERS) +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \ + configure.lineno config.status.lineno +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = jconfig.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`; +am__install_max = 40 +am__nobase_strip_setup = \ + srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'` +am__nobase_strip = \ + for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||" +am__nobase_list = $(am__nobase_strip_setup); \ + for p in $$list; do echo "$$p $$p"; done | \ + sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \ + $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \ + if (++n[$$2] == $(am__install_max)) \ + { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \ + END { for (dir in files) print dir, files[dir] }' +am__base_list = \ + sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \ + sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g' +am__uninstall_files_from_dir = { \ + test -z "$$files" \ + || { test ! -d "$$dir" && test ! -f "$$dir" && test ! -r "$$dir"; } \ + || { echo " ( cd '$$dir' && rm -f" $$files ")"; \ + $(am__cd) "$$dir" && rm -f $$files; }; \ + } +am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" \ + "$(DESTDIR)$(man1dir)" "$(DESTDIR)$(includedir)" +LTLIBRARIES = $(lib_LTLIBRARIES) +libjpeg_la_LIBADD = +am__objects_1 = jaricom$U.lo jcapimin$U.lo jcapistd$U.lo jcarith$U.lo \ + jccoefct$U.lo jccolor$U.lo jcdctmgr$U.lo jchuff$U.lo \ + jcinit$U.lo jcmainct$U.lo jcmarker$U.lo jcmaster$U.lo \ + jcomapi$U.lo jcparam$U.lo jcprepct$U.lo jcsample$U.lo \ + jctrans$U.lo jdapimin$U.lo jdapistd$U.lo jdarith$U.lo \ + jdatadst$U.lo jdatasrc$U.lo jdcoefct$U.lo jdcolor$U.lo \ + jddctmgr$U.lo jdhuff$U.lo jdinput$U.lo jdmainct$U.lo \ + jdmarker$U.lo jdmaster$U.lo jdmerge$U.lo jdpostct$U.lo \ + jdsample$U.lo jdtrans$U.lo jerror$U.lo jfdctflt$U.lo \ + jfdctfst$U.lo jfdctint$U.lo jidctflt$U.lo jidctfst$U.lo \ + jidctint$U.lo jquant1$U.lo jquant2$U.lo jutils$U.lo \ + jmemmgr$U.lo jmemnobs$U.lo +am_libjpeg_la_OBJECTS = $(am__objects_1) +libjpeg_la_OBJECTS = $(am_libjpeg_la_OBJECTS) +AM_V_lt = $(am__v_lt_$(V)) +am__v_lt_ = $(am__v_lt_$(AM_DEFAULT_VERBOSITY)) +am__v_lt_0 = --silent +libjpeg_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(libjpeg_la_LDFLAGS) $(LDFLAGS) -o $@ +PROGRAMS = $(bin_PROGRAMS) +am_cjpeg_OBJECTS = cjpeg$U.$(OBJEXT) rdppm$U.$(OBJEXT) \ + rdgif$U.$(OBJEXT) rdtarga$U.$(OBJEXT) rdrle$U.$(OBJEXT) \ + rdbmp$U.$(OBJEXT) rdswitch$U.$(OBJEXT) cdjpeg$U.$(OBJEXT) +cjpeg_OBJECTS = $(am_cjpeg_OBJECTS) +cjpeg_DEPENDENCIES = libjpeg.la +am_djpeg_OBJECTS = djpeg$U.$(OBJEXT) wrppm$U.$(OBJEXT) \ + wrgif$U.$(OBJEXT) wrtarga$U.$(OBJEXT) wrrle$U.$(OBJEXT) \ + wrbmp$U.$(OBJEXT) rdcolmap$U.$(OBJEXT) cdjpeg$U.$(OBJEXT) +djpeg_OBJECTS = $(am_djpeg_OBJECTS) +djpeg_DEPENDENCIES = libjpeg.la +am_jpegtran_OBJECTS = jpegtran$U.$(OBJEXT) rdswitch$U.$(OBJEXT) \ + cdjpeg$U.$(OBJEXT) transupp$U.$(OBJEXT) +jpegtran_OBJECTS = $(am_jpegtran_OBJECTS) +jpegtran_DEPENDENCIES = libjpeg.la +am_rdjpgcom_OBJECTS = rdjpgcom$U.$(OBJEXT) +rdjpgcom_OBJECTS = $(am_rdjpgcom_OBJECTS) +rdjpgcom_LDADD = $(LDADD) +am_wrjpgcom_OBJECTS = wrjpgcom$U.$(OBJEXT) +wrjpgcom_OBJECTS = $(am_wrjpgcom_OBJECTS) +wrjpgcom_LDADD = $(LDADD) +DEFAULT_INCLUDES = -I. +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_$(V)) +am__v_CC_ = $(am__v_CC_$(AM_DEFAULT_VERBOSITY)) +am__v_CC_0 = @echo " CC " $@; +AM_V_at = $(am__v_at_$(V)) +am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY)) +am__v_at_0 = @ +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_$(V)) +am__v_CCLD_ = $(am__v_CCLD_$(AM_DEFAULT_VERBOSITY)) +am__v_CCLD_0 = @echo " CCLD " $@; +AM_V_GEN = $(am__v_GEN_$(V)) +am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY)) +am__v_GEN_0 = @echo " GEN " $@; +SOURCES = $(libjpeg_la_SOURCES) $(cjpeg_SOURCES) $(djpeg_SOURCES) \ + $(jpegtran_SOURCES) $(rdjpgcom_SOURCES) $(wrjpgcom_SOURCES) +man1dir = $(mandir)/man1 +NROFF = nroff +MANS = $(man_MANS) +HEADERS = $(include_HEADERS) $(noinst_HEADERS) +ETAGS = etags +CTAGS = ctags +ACLOCAL = ${SHELL} /Users/jimbolaptop/tmp/libjpeg/missing --run aclocal-1.11 +AMTAR = ${SHELL} /Users/jimbolaptop/tmp/libjpeg/missing --run tar +AM_DEFAULT_VERBOSITY = 0 +AR = ar +AS = as +AUTOCONF = ${SHELL} /Users/jimbolaptop/tmp/libjpeg/missing --run autoconf +AUTOHEADER = echo autoheader ignored +AUTOMAKE = ${SHELL} /Users/jimbolaptop/tmp/libjpeg/missing --run automake-1.11 +AWK = awk +CC = gcc +CCDEPMODE = depmode=gcc3 +CFLAGS = -g -O2 +CPP = gcc -E +CPPFLAGS = +CYGPATH_W = echo +DEFS = -DHAVE_CONFIG_H +DEPDIR = .deps +DLLTOOL = dlltool +DSYMUTIL = dsymutil +DUMPBIN = +ECHO_C = \c +ECHO_N = +ECHO_T = +EGREP = /usr/bin/grep -E +EXEEXT = +FGREP = /usr/bin/grep -F +GREP = /usr/bin/grep +INSTALL = /usr/bin/install -c +INSTALL_DATA = ${INSTALL} -m 644 +INSTALL_PROGRAM = ${INSTALL} +INSTALL_SCRIPT = ${INSTALL} +INSTALL_STRIP_PROGRAM = $(install_sh) -c -s +JPEG_LIB_VERSION = 12:0:4 +LD = /Applications/Xcode5-DP3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld +LDFLAGS = +LIBOBJS = +LIBS = +LIBTOOL = $(SHELL) $(top_builddir)/libtool +LIPO = lipo +LN_S = ln -s +LTLIBOBJS = +MAINT = # +MAKEINFO = ${SHELL} /Users/jimbolaptop/tmp/libjpeg/missing --run makeinfo +MANIFEST_TOOL = : +MEMORYMGR = jmemnobs +MKDIR_P = ./install-sh -c -d +NM = /usr/bin/nm +NMEDIT = nmedit +OBJDUMP = objdump +OBJEXT = o +OTOOL = otool +OTOOL64 = : +PACKAGE = libjpeg +PACKAGE_BUGREPORT = +PACKAGE_NAME = libjpeg +PACKAGE_STRING = libjpeg 8.4.0 +PACKAGE_TARNAME = libjpeg +PACKAGE_URL = +PACKAGE_VERSION = 8.4.0 +PATH_SEPARATOR = : +RANLIB = ranlib +SED = /usr/bin/sed +SET_MAKE = +SHELL = /bin/sh +STRIP = strip +U = +VERSION = 8.4.0 +abs_builddir = /Users/jimbolaptop/tmp/libjpeg +abs_srcdir = /Users/jimbolaptop/tmp/libjpeg +abs_top_builddir = /Users/jimbolaptop/tmp/libjpeg +abs_top_srcdir = /Users/jimbolaptop/tmp/libjpeg +ac_ct_AR = ar +ac_ct_CC = gcc +ac_ct_DUMPBIN = +am__include = include +am__leading_dot = . +am__quote = +am__tar = ${AMTAR} chof - "$$tardir" +am__untar = ${AMTAR} xf - +bindir = ${exec_prefix}/bin +build = x86_64-apple-darwin13.2.0 +build_alias = +build_cpu = x86_64 +build_os = darwin13.2.0 +build_vendor = apple +builddir = . +datadir = ${datarootdir} +datarootdir = ${prefix}/share +docdir = ${datarootdir}/doc/${PACKAGE_TARNAME} +dvidir = ${docdir} +exec_prefix = ${prefix} +host = x86_64-apple-darwin13.2.0 +host_alias = +host_cpu = x86_64 +host_os = darwin13.2.0 +host_vendor = apple +htmldir = ${docdir} +includedir = ${prefix}/include +infodir = ${datarootdir}/info +install_sh = ${SHELL} /Users/jimbolaptop/tmp/libjpeg/install-sh +libdir = ${exec_prefix}/lib +libexecdir = ${exec_prefix}/libexec +localedir = ${datarootdir}/locale +localstatedir = ${prefix}/var +mandir = ${datarootdir}/man +mkdir_p = $(top_builddir)/./install-sh -c -d +oldincludedir = /usr/include +pdfdir = ${docdir} +prefix = /usr/local +program_transform_name = s,x,x, +psdir = ${docdir} +sbindir = ${exec_prefix}/sbin +sharedstatedir = ${prefix}/com +srcdir = . +sysconfdir = ${prefix}/etc +target = x86_64-apple-darwin13.2.0 +target_alias = +target_cpu = x86_64 +target_os = darwin13.2.0 +target_vendor = apple +top_build_prefix = +top_builddir = . +top_srcdir = . + +# Sources to build library +LIBSOURCES = jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c \ + jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c \ + jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c \ + jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c \ + jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c \ + jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c \ + jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jquant1.c \ + jquant2.c jutils.c jmemmgr.c jmemnobs.c + + +# System dependent sources +SYSDEPSOURCES = jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c + +# Headers which are installed to support the library +INSTINCLUDES = jerror.h jmorecfg.h jpeglib.h + +# Headers which are not installed +OTHERINCLUDES = cderror.h cdjpeg.h jdct.h jinclude.h jmemsys.h jpegint.h \ + jversion.h transupp.h + + +# Manual pages (Automake uses 'MANS' for itself) +DISTMANS = cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 wrjpgcom.1 + +# Other documentation files +DOCS = README install.txt usage.txt wizard.txt example.c libjpeg.txt \ + structure.txt coderules.txt filelist.txt change.log + + +# Makefiles for various systems +MKFILES = configure Makefile.in makefile.ansi makefile.unix makefile.bcc \ + makefile.mc6 makefile.dj makefile.wat makefile.vc makejdsw.vc6 \ + makeadsw.vc6 makejdep.vc6 makejdsp.vc6 makejmak.vc6 makecdep.vc6 \ + makecdsp.vc6 makecmak.vc6 makeddep.vc6 makeddsp.vc6 makedmak.vc6 \ + maketdep.vc6 maketdsp.vc6 maketmak.vc6 makerdep.vc6 makerdsp.vc6 \ + makermak.vc6 makewdep.vc6 makewdsp.vc6 makewmak.vc6 makejsln.v10 \ + makeasln.v10 makejvcx.v10 makejfil.v10 makecvcx.v10 makecfil.v10 \ + makedvcx.v10 makedfil.v10 maketvcx.v10 maketfil.v10 makervcx.v10 \ + makerfil.v10 makewvcx.v10 makewfil.v10 makeproj.mac makcjpeg.st \ + makdjpeg.st makljpeg.st maktjpeg.st makefile.manx makefile.sas \ + makefile.mms makefile.vms makvms.opt + + +# Configuration files +CONFIGFILES = jconfig.cfg jconfig.bcc jconfig.mc6 jconfig.dj jconfig.wat \ + jconfig.vc jconfig.mac jconfig.st jconfig.manx jconfig.sas \ + jconfig.vms + + +# Support scripts for configure +CONFIGUREFILES = config.guess config.sub install-sh ltmain.sh depcomp missing + +# Miscellaneous support files +OTHERFILES = jconfig.txt ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm \ + libjpeg.map + + +# Test support files +TESTFILES = testorig.jpg testimg.ppm testimg.bmp testimg.jpg testprog.jpg \ + testimgp.jpg + + +# libtool libraries to build +lib_LTLIBRARIES = libjpeg.la + +# Library sources for libjpeg.la +libjpeg_la_SOURCES = $(LIBSOURCES) + +# LDFLAGS for libjpeg.la +libjpeg_la_LDFLAGS = -no-undefined -version-info $(JPEG_LIB_VERSION) \ + $(am__append_1) + +# Executable sources & libs +cjpeg_SOURCES = cjpeg.c rdppm.c rdgif.c rdtarga.c rdrle.c rdbmp.c \ + rdswitch.c cdjpeg.c + +cjpeg_LDADD = libjpeg.la +djpeg_SOURCES = djpeg.c wrppm.c wrgif.c wrtarga.c wrrle.c wrbmp.c \ + rdcolmap.c cdjpeg.c + +djpeg_LDADD = libjpeg.la +jpegtran_SOURCES = jpegtran.c rdswitch.c cdjpeg.c transupp.c +jpegtran_LDADD = libjpeg.la +rdjpgcom_SOURCES = rdjpgcom.c +wrjpgcom_SOURCES = wrjpgcom.c + +# Manual pages to install +man_MANS = $(DISTMANS) + +# Headers to install +include_HEADERS = $(INSTINCLUDES) + +# Other distributed headers +noinst_HEADERS = $(OTHERINCLUDES) + +# Other distributed files +EXTRA_DIST = $(DOCS) $(DISTMANS) $(MKFILES) $(CONFIGFILES) $(SYSDEPSOURCES) \ + $(OTHERFILES) $(TESTFILES) + + +# Files to be cleaned +CLEANFILES = testout.ppm testout.bmp testout.jpg testoutp.ppm testoutp.jpg \ + testoutt.jpg + +all: jconfig.h + $(MAKE) $(AM_MAKEFLAGS) all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +am--refresh: Makefile + @: +$(srcdir)/Makefile.in: # $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + echo ' cd $(srcdir) && $(AUTOMAKE) --foreign'; \ + $(am__cd) $(srcdir) && $(AUTOMAKE) --foreign \ + && exit 0; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + echo ' $(SHELL) ./config.status'; \ + $(SHELL) ./config.status;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + $(SHELL) ./config.status --recheck + +$(top_srcdir)/configure: # $(am__configure_deps) + $(am__cd) $(srcdir) && $(AUTOCONF) +$(ACLOCAL_M4): # $(am__aclocal_m4_deps) + $(am__cd) $(srcdir) && $(ACLOCAL) $(ACLOCAL_AMFLAGS) +$(am__aclocal_m4_deps): + +jconfig.h: stamp-h1 + @if test ! -f $@; then rm -f stamp-h1; else :; fi + @if test ! -f $@; then $(MAKE) $(AM_MAKEFLAGS) stamp-h1; else :; fi + +stamp-h1: $(srcdir)/jconfig.cfg $(top_builddir)/config.status + @rm -f stamp-h1 + cd $(top_builddir) && $(SHELL) ./config.status jconfig.h +$(srcdir)/jconfig.cfg: # $(am__configure_deps) + ($(am__cd) $(top_srcdir) && $(AUTOHEADER)) + rm -f stamp-h1 + touch $@ + +distclean-hdr: + -rm -f jconfig.h stamp-h1 +install-libLTLIBRARIES: $(lib_LTLIBRARIES) + @$(NORMAL_INSTALL) + test -z "$(libdir)" || $(MKDIR_P) "$(DESTDIR)$(libdir)" + @list='$(lib_LTLIBRARIES)'; test -n "$(libdir)" || list=; \ + list2=; for p in $$list; do \ + if test -f $$p; then \ + list2="$$list2 $$p"; \ + else :; fi; \ + done; \ + test -z "$$list2" || { \ + echo " $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL) $(INSTALL_STRIP_FLAG) $$list2 '$(DESTDIR)$(libdir)'"; \ + $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL) $(INSTALL_STRIP_FLAG) $$list2 "$(DESTDIR)$(libdir)"; \ + } + +uninstall-libLTLIBRARIES: + @$(NORMAL_UNINSTALL) + @list='$(lib_LTLIBRARIES)'; test -n "$(libdir)" || list=; \ + for p in $$list; do \ + $(am__strip_dir) \ + echo " $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=uninstall rm -f '$(DESTDIR)$(libdir)/$$f'"; \ + $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=uninstall rm -f "$(DESTDIR)$(libdir)/$$f"; \ + done + +clean-libLTLIBRARIES: + -test -z "$(lib_LTLIBRARIES)" || rm -f $(lib_LTLIBRARIES) + @list='$(lib_LTLIBRARIES)'; for p in $$list; do \ + dir="`echo $$p | sed -e 's|/[^/]*$$||'`"; \ + test "$$dir" != "$$p" || dir=.; \ + echo "rm -f \"$${dir}/so_locations\""; \ + rm -f "$${dir}/so_locations"; \ + done +libjpeg.la: $(libjpeg_la_OBJECTS) $(libjpeg_la_DEPENDENCIES) + $(AM_V_CCLD)$(libjpeg_la_LINK) -rpath $(libdir) $(libjpeg_la_OBJECTS) $(libjpeg_la_LIBADD) $(LIBS) +install-binPROGRAMS: $(bin_PROGRAMS) + @$(NORMAL_INSTALL) + test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)" + @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ + for p in $$list; do echo "$$p $$p"; done | \ + sed 's/$(EXEEXT)$$//' | \ + while read p p1; do if test -f $$p || test -f $$p1; \ + then echo "$$p"; echo "$$p"; else :; fi; \ + done | \ + sed -e 'p;s,.*/,,;n;h' -e 's|.*|.|' \ + -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \ + sed 'N;N;N;s,\n, ,g' | \ + $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \ + { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \ + if ($$2 == $$4) files[d] = files[d] " " $$1; \ + else { print "f", $$3 "/" $$4, $$1; } } \ + END { for (d in files) print "f", d, files[d] }' | \ + while read type dir files; do \ + if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \ + test -z "$$files" || { \ + echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(bindir)$$dir'"; \ + $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(bindir)$$dir" || exit $$?; \ + } \ + ; done + +uninstall-binPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ + files=`for p in $$list; do echo "$$p"; done | \ + sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \ + -e 's/$$/$(EXEEXT)/' `; \ + test -n "$$list" || exit 0; \ + echo " ( cd '$(DESTDIR)$(bindir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(bindir)" && rm -f $$files + +clean-binPROGRAMS: + @list='$(bin_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list +cjpeg$(EXEEXT): $(cjpeg_OBJECTS) $(cjpeg_DEPENDENCIES) + @rm -f cjpeg$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(cjpeg_OBJECTS) $(cjpeg_LDADD) $(LIBS) +djpeg$(EXEEXT): $(djpeg_OBJECTS) $(djpeg_DEPENDENCIES) + @rm -f djpeg$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(djpeg_OBJECTS) $(djpeg_LDADD) $(LIBS) +jpegtran$(EXEEXT): $(jpegtran_OBJECTS) $(jpegtran_DEPENDENCIES) + @rm -f jpegtran$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(jpegtran_OBJECTS) $(jpegtran_LDADD) $(LIBS) +rdjpgcom$(EXEEXT): $(rdjpgcom_OBJECTS) $(rdjpgcom_DEPENDENCIES) + @rm -f rdjpgcom$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(rdjpgcom_OBJECTS) $(rdjpgcom_LDADD) $(LIBS) +wrjpgcom$(EXEEXT): $(wrjpgcom_OBJECTS) $(wrjpgcom_DEPENDENCIES) + @rm -f wrjpgcom$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(wrjpgcom_OBJECTS) $(wrjpgcom_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c +./ansi2knr: ansi2knr.$(OBJEXT) + $(LINK) ansi2knr.$(OBJEXT) $(LIBS) +ansi2knr.$(OBJEXT): $(CONFIG_HEADER) + +clean-krextra: + -rm -f ansi2knr + +mostlyclean-kr: + -test "$U" = "" || rm -f *_.c + +include ./$(DEPDIR)/jmemnobs$U.Plo +include ./$(DEPDIR)/cdjpeg$U.Po +include ./$(DEPDIR)/cjpeg$U.Po +include ./$(DEPDIR)/djpeg$U.Po +include ./$(DEPDIR)/jaricom$U.Plo +include ./$(DEPDIR)/jcapimin$U.Plo +include ./$(DEPDIR)/jcapistd$U.Plo +include ./$(DEPDIR)/jcarith$U.Plo +include ./$(DEPDIR)/jccoefct$U.Plo +include ./$(DEPDIR)/jccolor$U.Plo +include ./$(DEPDIR)/jcdctmgr$U.Plo +include ./$(DEPDIR)/jchuff$U.Plo +include ./$(DEPDIR)/jcinit$U.Plo +include ./$(DEPDIR)/jcmainct$U.Plo +include ./$(DEPDIR)/jcmarker$U.Plo +include ./$(DEPDIR)/jcmaster$U.Plo +include ./$(DEPDIR)/jcomapi$U.Plo +include ./$(DEPDIR)/jcparam$U.Plo +include ./$(DEPDIR)/jcprepct$U.Plo +include ./$(DEPDIR)/jcsample$U.Plo +include ./$(DEPDIR)/jctrans$U.Plo +include ./$(DEPDIR)/jdapimin$U.Plo +include ./$(DEPDIR)/jdapistd$U.Plo +include ./$(DEPDIR)/jdarith$U.Plo +include ./$(DEPDIR)/jdatadst$U.Plo +include ./$(DEPDIR)/jdatasrc$U.Plo +include ./$(DEPDIR)/jdcoefct$U.Plo +include ./$(DEPDIR)/jdcolor$U.Plo +include ./$(DEPDIR)/jddctmgr$U.Plo +include ./$(DEPDIR)/jdhuff$U.Plo +include ./$(DEPDIR)/jdinput$U.Plo +include ./$(DEPDIR)/jdmainct$U.Plo +include ./$(DEPDIR)/jdmarker$U.Plo +include ./$(DEPDIR)/jdmaster$U.Plo +include ./$(DEPDIR)/jdmerge$U.Plo +include ./$(DEPDIR)/jdpostct$U.Plo +include ./$(DEPDIR)/jdsample$U.Plo +include ./$(DEPDIR)/jdtrans$U.Plo +include ./$(DEPDIR)/jerror$U.Plo +include ./$(DEPDIR)/jfdctflt$U.Plo +include ./$(DEPDIR)/jfdctfst$U.Plo +include ./$(DEPDIR)/jfdctint$U.Plo +include ./$(DEPDIR)/jidctflt$U.Plo +include ./$(DEPDIR)/jidctfst$U.Plo +include ./$(DEPDIR)/jidctint$U.Plo +include ./$(DEPDIR)/jmemmgr$U.Plo +include ./$(DEPDIR)/jpegtran$U.Po +include ./$(DEPDIR)/jquant1$U.Plo +include ./$(DEPDIR)/jquant2$U.Plo +include ./$(DEPDIR)/jutils$U.Plo +include ./$(DEPDIR)/rdbmp$U.Po +include ./$(DEPDIR)/rdcolmap$U.Po +include ./$(DEPDIR)/rdgif$U.Po +include ./$(DEPDIR)/rdjpgcom$U.Po +include ./$(DEPDIR)/rdppm$U.Po +include ./$(DEPDIR)/rdrle$U.Po +include ./$(DEPDIR)/rdswitch$U.Po +include ./$(DEPDIR)/rdtarga$U.Po +include ./$(DEPDIR)/transupp$U.Po +include ./$(DEPDIR)/wrbmp$U.Po +include ./$(DEPDIR)/wrgif$U.Po +include ./$(DEPDIR)/wrjpgcom$U.Po +include ./$(DEPDIR)/wrppm$U.Po +include ./$(DEPDIR)/wrrle$U.Po +include ./$(DEPDIR)/wrtarga$U.Po + +.c.o: + $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< + $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +# $(AM_V_CC)source='$<' object='$@' libtool=no \ +# DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \ +# $(AM_V_CC_no)$(COMPILE) -c $< + +.c.obj: + $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` + $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +# $(AM_V_CC)source='$<' object='$@' libtool=no \ +# DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \ +# $(AM_V_CC_no)$(COMPILE) -c `$(CYGPATH_W) '$<'` + +.c.lo: + $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< + $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +# $(AM_V_CC)source='$<' object='$@' libtool=yes \ +# DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \ +# $(AM_V_CC_no)$(LTCOMPILE) -c -o $@ $< +jmemnobs_.c: jmemnobs.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jmemnobs.c; then echo $(srcdir)/jmemnobs.c; else echo jmemnobs.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +cdjpeg_.c: cdjpeg.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/cdjpeg.c; then echo $(srcdir)/cdjpeg.c; else echo cdjpeg.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +cjpeg_.c: cjpeg.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/cjpeg.c; then echo $(srcdir)/cjpeg.c; else echo cjpeg.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +djpeg_.c: djpeg.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/djpeg.c; then echo $(srcdir)/djpeg.c; else echo djpeg.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jaricom_.c: jaricom.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jaricom.c; then echo $(srcdir)/jaricom.c; else echo jaricom.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcapimin_.c: jcapimin.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcapimin.c; then echo $(srcdir)/jcapimin.c; else echo jcapimin.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcapistd_.c: jcapistd.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcapistd.c; then echo $(srcdir)/jcapistd.c; else echo jcapistd.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcarith_.c: jcarith.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcarith.c; then echo $(srcdir)/jcarith.c; else echo jcarith.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jccoefct_.c: jccoefct.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jccoefct.c; then echo $(srcdir)/jccoefct.c; else echo jccoefct.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jccolor_.c: jccolor.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jccolor.c; then echo $(srcdir)/jccolor.c; else echo jccolor.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcdctmgr_.c: jcdctmgr.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcdctmgr.c; then echo $(srcdir)/jcdctmgr.c; else echo jcdctmgr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jchuff_.c: jchuff.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jchuff.c; then echo $(srcdir)/jchuff.c; else echo jchuff.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcinit_.c: jcinit.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcinit.c; then echo $(srcdir)/jcinit.c; else echo jcinit.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcmainct_.c: jcmainct.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcmainct.c; then echo $(srcdir)/jcmainct.c; else echo jcmainct.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcmarker_.c: jcmarker.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcmarker.c; then echo $(srcdir)/jcmarker.c; else echo jcmarker.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcmaster_.c: jcmaster.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcmaster.c; then echo $(srcdir)/jcmaster.c; else echo jcmaster.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcomapi_.c: jcomapi.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcomapi.c; then echo $(srcdir)/jcomapi.c; else echo jcomapi.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcparam_.c: jcparam.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcparam.c; then echo $(srcdir)/jcparam.c; else echo jcparam.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcprepct_.c: jcprepct.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcprepct.c; then echo $(srcdir)/jcprepct.c; else echo jcprepct.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jcsample_.c: jcsample.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jcsample.c; then echo $(srcdir)/jcsample.c; else echo jcsample.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jctrans_.c: jctrans.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jctrans.c; then echo $(srcdir)/jctrans.c; else echo jctrans.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdapimin_.c: jdapimin.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdapimin.c; then echo $(srcdir)/jdapimin.c; else echo jdapimin.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdapistd_.c: jdapistd.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdapistd.c; then echo $(srcdir)/jdapistd.c; else echo jdapistd.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdarith_.c: jdarith.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdarith.c; then echo $(srcdir)/jdarith.c; else echo jdarith.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdatadst_.c: jdatadst.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdatadst.c; then echo $(srcdir)/jdatadst.c; else echo jdatadst.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdatasrc_.c: jdatasrc.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdatasrc.c; then echo $(srcdir)/jdatasrc.c; else echo jdatasrc.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdcoefct_.c: jdcoefct.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdcoefct.c; then echo $(srcdir)/jdcoefct.c; else echo jdcoefct.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdcolor_.c: jdcolor.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdcolor.c; then echo $(srcdir)/jdcolor.c; else echo jdcolor.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jddctmgr_.c: jddctmgr.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jddctmgr.c; then echo $(srcdir)/jddctmgr.c; else echo jddctmgr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdhuff_.c: jdhuff.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdhuff.c; then echo $(srcdir)/jdhuff.c; else echo jdhuff.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdinput_.c: jdinput.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdinput.c; then echo $(srcdir)/jdinput.c; else echo jdinput.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdmainct_.c: jdmainct.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdmainct.c; then echo $(srcdir)/jdmainct.c; else echo jdmainct.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdmarker_.c: jdmarker.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdmarker.c; then echo $(srcdir)/jdmarker.c; else echo jdmarker.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdmaster_.c: jdmaster.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdmaster.c; then echo $(srcdir)/jdmaster.c; else echo jdmaster.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdmerge_.c: jdmerge.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdmerge.c; then echo $(srcdir)/jdmerge.c; else echo jdmerge.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdpostct_.c: jdpostct.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdpostct.c; then echo $(srcdir)/jdpostct.c; else echo jdpostct.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdsample_.c: jdsample.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdsample.c; then echo $(srcdir)/jdsample.c; else echo jdsample.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jdtrans_.c: jdtrans.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jdtrans.c; then echo $(srcdir)/jdtrans.c; else echo jdtrans.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jerror_.c: jerror.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jerror.c; then echo $(srcdir)/jerror.c; else echo jerror.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jfdctflt_.c: jfdctflt.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jfdctflt.c; then echo $(srcdir)/jfdctflt.c; else echo jfdctflt.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jfdctfst_.c: jfdctfst.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jfdctfst.c; then echo $(srcdir)/jfdctfst.c; else echo jfdctfst.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jfdctint_.c: jfdctint.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jfdctint.c; then echo $(srcdir)/jfdctint.c; else echo jfdctint.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jidctflt_.c: jidctflt.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jidctflt.c; then echo $(srcdir)/jidctflt.c; else echo jidctflt.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jidctfst_.c: jidctfst.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jidctfst.c; then echo $(srcdir)/jidctfst.c; else echo jidctfst.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jidctint_.c: jidctint.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jidctint.c; then echo $(srcdir)/jidctint.c; else echo jidctint.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jmemmgr_.c: jmemmgr.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jmemmgr.c; then echo $(srcdir)/jmemmgr.c; else echo jmemmgr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jpegtran_.c: jpegtran.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jpegtran.c; then echo $(srcdir)/jpegtran.c; else echo jpegtran.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jquant1_.c: jquant1.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jquant1.c; then echo $(srcdir)/jquant1.c; else echo jquant1.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jquant2_.c: jquant2.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jquant2.c; then echo $(srcdir)/jquant2.c; else echo jquant2.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jutils_.c: jutils.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/jutils.c; then echo $(srcdir)/jutils.c; else echo jutils.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdbmp_.c: rdbmp.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdbmp.c; then echo $(srcdir)/rdbmp.c; else echo rdbmp.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdcolmap_.c: rdcolmap.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdcolmap.c; then echo $(srcdir)/rdcolmap.c; else echo rdcolmap.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdgif_.c: rdgif.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdgif.c; then echo $(srcdir)/rdgif.c; else echo rdgif.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdjpgcom_.c: rdjpgcom.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdjpgcom.c; then echo $(srcdir)/rdjpgcom.c; else echo rdjpgcom.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdppm_.c: rdppm.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdppm.c; then echo $(srcdir)/rdppm.c; else echo rdppm.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdrle_.c: rdrle.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdrle.c; then echo $(srcdir)/rdrle.c; else echo rdrle.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdswitch_.c: rdswitch.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdswitch.c; then echo $(srcdir)/rdswitch.c; else echo rdswitch.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +rdtarga_.c: rdtarga.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/rdtarga.c; then echo $(srcdir)/rdtarga.c; else echo rdtarga.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +transupp_.c: transupp.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/transupp.c; then echo $(srcdir)/transupp.c; else echo transupp.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +wrbmp_.c: wrbmp.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/wrbmp.c; then echo $(srcdir)/wrbmp.c; else echo wrbmp.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +wrgif_.c: wrgif.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/wrgif.c; then echo $(srcdir)/wrgif.c; else echo wrgif.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +wrjpgcom_.c: wrjpgcom.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/wrjpgcom.c; then echo $(srcdir)/wrjpgcom.c; else echo wrjpgcom.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +wrppm_.c: wrppm.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/wrppm.c; then echo $(srcdir)/wrppm.c; else echo wrppm.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +wrrle_.c: wrrle.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/wrrle.c; then echo $(srcdir)/wrrle.c; else echo wrrle.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +wrtarga_.c: wrtarga.c $(ANSI2KNR) + $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/wrtarga.c; then echo $(srcdir)/wrtarga.c; else echo wrtarga.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ +jmemnobs_.$(OBJEXT) jmemnobs_.lo cdjpeg_.$(OBJEXT) cdjpeg_.lo \ +cjpeg_.$(OBJEXT) cjpeg_.lo djpeg_.$(OBJEXT) djpeg_.lo \ +jaricom_.$(OBJEXT) jaricom_.lo jcapimin_.$(OBJEXT) jcapimin_.lo \ +jcapistd_.$(OBJEXT) jcapistd_.lo jcarith_.$(OBJEXT) jcarith_.lo \ +jccoefct_.$(OBJEXT) jccoefct_.lo jccolor_.$(OBJEXT) jccolor_.lo \ +jcdctmgr_.$(OBJEXT) jcdctmgr_.lo jchuff_.$(OBJEXT) jchuff_.lo \ +jcinit_.$(OBJEXT) jcinit_.lo jcmainct_.$(OBJEXT) jcmainct_.lo \ +jcmarker_.$(OBJEXT) jcmarker_.lo jcmaster_.$(OBJEXT) jcmaster_.lo \ +jcomapi_.$(OBJEXT) jcomapi_.lo jcparam_.$(OBJEXT) jcparam_.lo \ +jcprepct_.$(OBJEXT) jcprepct_.lo jcsample_.$(OBJEXT) jcsample_.lo \ +jctrans_.$(OBJEXT) jctrans_.lo jdapimin_.$(OBJEXT) jdapimin_.lo \ +jdapistd_.$(OBJEXT) jdapistd_.lo jdarith_.$(OBJEXT) jdarith_.lo \ +jdatadst_.$(OBJEXT) jdatadst_.lo jdatasrc_.$(OBJEXT) jdatasrc_.lo \ +jdcoefct_.$(OBJEXT) jdcoefct_.lo jdcolor_.$(OBJEXT) jdcolor_.lo \ +jddctmgr_.$(OBJEXT) jddctmgr_.lo jdhuff_.$(OBJEXT) jdhuff_.lo \ +jdinput_.$(OBJEXT) jdinput_.lo jdmainct_.$(OBJEXT) jdmainct_.lo \ +jdmarker_.$(OBJEXT) jdmarker_.lo jdmaster_.$(OBJEXT) jdmaster_.lo \ +jdmerge_.$(OBJEXT) jdmerge_.lo jdpostct_.$(OBJEXT) jdpostct_.lo \ +jdsample_.$(OBJEXT) jdsample_.lo jdtrans_.$(OBJEXT) jdtrans_.lo \ +jerror_.$(OBJEXT) jerror_.lo jfdctflt_.$(OBJEXT) jfdctflt_.lo \ +jfdctfst_.$(OBJEXT) jfdctfst_.lo jfdctint_.$(OBJEXT) jfdctint_.lo \ +jidctflt_.$(OBJEXT) jidctflt_.lo jidctfst_.$(OBJEXT) jidctfst_.lo \ +jidctint_.$(OBJEXT) jidctint_.lo jmemmgr_.$(OBJEXT) jmemmgr_.lo \ +jpegtran_.$(OBJEXT) jpegtran_.lo jquant1_.$(OBJEXT) jquant1_.lo \ +jquant2_.$(OBJEXT) jquant2_.lo jutils_.$(OBJEXT) jutils_.lo \ +rdbmp_.$(OBJEXT) rdbmp_.lo rdcolmap_.$(OBJEXT) rdcolmap_.lo \ +rdgif_.$(OBJEXT) rdgif_.lo rdjpgcom_.$(OBJEXT) rdjpgcom_.lo \ +rdppm_.$(OBJEXT) rdppm_.lo rdrle_.$(OBJEXT) rdrle_.lo \ +rdswitch_.$(OBJEXT) rdswitch_.lo rdtarga_.$(OBJEXT) rdtarga_.lo \ +transupp_.$(OBJEXT) transupp_.lo wrbmp_.$(OBJEXT) wrbmp_.lo \ +wrgif_.$(OBJEXT) wrgif_.lo wrjpgcom_.$(OBJEXT) wrjpgcom_.lo \ +wrppm_.$(OBJEXT) wrppm_.lo wrrle_.$(OBJEXT) wrrle_.lo \ +wrtarga_.$(OBJEXT) wrtarga_.lo : $(ANSI2KNR) + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +distclean-libtool: + -rm -f libtool config.lt +install-man1: $(man_MANS) + @$(NORMAL_INSTALL) + test -z "$(man1dir)" || $(MKDIR_P) "$(DESTDIR)$(man1dir)" + @list=''; test -n "$(man1dir)" || exit 0; \ + { for i in $$list; do echo "$$i"; done; \ + l2='$(man_MANS)'; for i in $$l2; do echo "$$i"; done | \ + sed -n '/\.1[a-z]*$$/p'; \ + } | while read p; do \ + if test -f $$p; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; echo "$$p"; \ + done | \ + sed -e 'n;s,.*/,,;p;h;s,.*\.,,;s,^[^1][0-9a-z]*$$,1,;x' \ + -e 's,\.[0-9a-z]*$$,,;$(transform);G;s,\n,.,' | \ + sed 'N;N;s,\n, ,g' | { \ + list=; while read file base inst; do \ + if test "$$base" = "$$inst"; then list="$$list $$file"; else \ + echo " $(INSTALL_DATA) '$$file' '$(DESTDIR)$(man1dir)/$$inst'"; \ + $(INSTALL_DATA) "$$file" "$(DESTDIR)$(man1dir)/$$inst" || exit $$?; \ + fi; \ + done; \ + for i in $$list; do echo "$$i"; done | $(am__base_list) | \ + while read files; do \ + test -z "$$files" || { \ + echo " $(INSTALL_DATA) $$files '$(DESTDIR)$(man1dir)'"; \ + $(INSTALL_DATA) $$files "$(DESTDIR)$(man1dir)" || exit $$?; }; \ + done; } + +uninstall-man1: + @$(NORMAL_UNINSTALL) + @list=''; test -n "$(man1dir)" || exit 0; \ + files=`{ for i in $$list; do echo "$$i"; done; \ + l2='$(man_MANS)'; for i in $$l2; do echo "$$i"; done | \ + sed -n '/\.1[a-z]*$$/p'; \ + } | sed -e 's,.*/,,;h;s,.*\.,,;s,^[^1][0-9a-z]*$$,1,;x' \ + -e 's,\.[0-9a-z]*$$,,;$(transform);G;s,\n,.,'`; \ + dir='$(DESTDIR)$(man1dir)'; $(am__uninstall_files_from_dir) +install-includeHEADERS: $(include_HEADERS) + @$(NORMAL_INSTALL) + test -z "$(includedir)" || $(MKDIR_P) "$(DESTDIR)$(includedir)" + @list='$(include_HEADERS)'; test -n "$(includedir)" || list=; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(includedir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(includedir)" || exit $$?; \ + done + +uninstall-includeHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(include_HEADERS)'; test -n "$(includedir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + dir='$(DESTDIR)$(includedir)'; $(am__uninstall_files_from_dir) + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) jconfig.cfg $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) jconfig.cfg $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) jconfig.cfg $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) jconfig.cfg $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) check-local +check: check-am +all-am: Makefile $(ANSI2KNR) $(LTLIBRARIES) $(PROGRAMS) $(MANS) \ + $(HEADERS) jconfig.h +install-binPROGRAMS: install-libLTLIBRARIES + +installdirs: + for dir in "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" "$(DESTDIR)$(includedir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-binPROGRAMS clean-generic clean-krextra \ + clean-libLTLIBRARIES clean-libtool mostlyclean-am + +distclean: distclean-am + -rm -f $(am__CONFIG_DISTCLEAN_FILES) + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-hdr distclean-libtool distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: install-data-local install-includeHEADERS install-man + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: install-binPROGRAMS install-libLTLIBRARIES + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: install-man1 + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f $(am__CONFIG_DISTCLEAN_FILES) + -rm -rf $(top_srcdir)/autom4te.cache + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic mostlyclean-kr \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-binPROGRAMS uninstall-includeHEADERS \ + uninstall-libLTLIBRARIES uninstall-local uninstall-man + +uninstall-man: uninstall-man1 + +.MAKE: all check-am install-am install-strip + +.PHONY: CTAGS GTAGS all all-am am--refresh check check-am check-local \ + clean clean-binPROGRAMS clean-generic clean-krextra \ + clean-libLTLIBRARIES clean-libtool ctags distclean \ + distclean-compile distclean-generic distclean-hdr \ + distclean-libtool distclean-tags dvi dvi-am html html-am info \ + info-am install install-am install-binPROGRAMS install-data \ + install-data-am install-data-local install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-includeHEADERS install-info install-info-am \ + install-libLTLIBRARIES install-man install-man1 install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-kr mostlyclean-libtool pdf \ + pdf-am ps ps-am tags uninstall uninstall-am \ + uninstall-binPROGRAMS uninstall-includeHEADERS \ + uninstall-libLTLIBRARIES uninstall-local uninstall-man \ + uninstall-man1 + + +# Install jconfig.h +install-data-local: + $(mkinstalldirs) $(DESTDIR)$(includedir) + $(INSTALL_HEADER) jconfig.h $(DESTDIR)$(includedir)/jconfig.h + +# Uninstall jconfig.h +uninstall-local: + rm -f $(DESTDIR)$(includedir)/jconfig.h + +# Run tests +test: check-local +check-local: + rm -f testout* + ./djpeg -dct int -ppm -outfile testout.ppm $(srcdir)/testorig.jpg + ./djpeg -dct int -bmp -colors 256 -outfile testout.bmp $(srcdir)/testorig.jpg + ./cjpeg -dct int -outfile testout.jpg $(srcdir)/testimg.ppm + ./djpeg -dct int -ppm -outfile testoutp.ppm $(srcdir)/testprog.jpg + ./cjpeg -dct int -progressive -opt -outfile testoutp.jpg $(srcdir)/testimg.ppm + ./jpegtran -outfile testoutt.jpg $(srcdir)/testprog.jpg + cmp $(srcdir)/testimg.ppm testout.ppm + cmp $(srcdir)/testimg.bmp testout.bmp + cmp $(srcdir)/testimg.jpg testout.jpg + cmp $(srcdir)/testimg.ppm testoutp.ppm + cmp $(srcdir)/testimgp.jpg testoutp.jpg + cmp $(srcdir)/testorig.jpg testoutt.jpg + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/crypto777/jpeg/ansi2knr.c b/crypto777/jpeg/ansi2knr.c new file mode 100644 index 000000000..e84c210b6 --- /dev/null +++ b/crypto777/jpeg/ansi2knr.c @@ -0,0 +1,739 @@ +/* Copyright (C) 1989, 2000 Aladdin Enterprises. All rights reserved. */ + +/*$Id: ansi2knr.c,v 1.14 2003/09/06 05:36:56 eggert Exp $*/ +/* Convert ANSI C function definitions to K&R ("traditional C") syntax */ + +/* +ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY. No author or distributor accepts responsibility to anyone for the +consequences of using it or for whether it serves any particular purpose or +works at all, unless he says so in writing. Refer to the GNU General Public +License (the "GPL") for full details. + +Everyone is granted permission to copy, modify and redistribute ansi2knr, +but only under the conditions described in the GPL. A copy of this license +is supposed to have been given to you along with ansi2knr so you can know +your rights and responsibilities. It should be in a file named COPYLEFT, +or, if there is no file named COPYLEFT, a file named COPYING. Among other +things, the copyright notice and this notice must be preserved on all +copies. + +We explicitly state here what we believe is already implied by the GPL: if +the ansi2knr program is distributed as a separate set of sources and a +separate executable file which are aggregated on a storage medium together +with another program, this in itself does not bring the other program under +the GPL, nor does the mere fact that such a program or the procedures for +constructing it invoke the ansi2knr executable bring any other part of the +program under the GPL. +*/ + +/* + * Usage: + ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]] + * --filename provides the file name for the #line directive in the output, + * overriding input_file (if present). + * If no input_file is supplied, input is read from stdin. + * If no output_file is supplied, output goes to stdout. + * There are no error messages. + * + * ansi2knr recognizes function definitions by seeing a non-keyword + * identifier at the left margin, followed by a left parenthesis, with a + * right parenthesis as the last character on the line, and with a left + * brace as the first token on the following line (ignoring possible + * intervening comments and/or preprocessor directives), except that a line + * consisting of only + * identifier1(identifier2) + * will not be considered a function definition unless identifier2 is + * the word "void", and a line consisting of + * identifier1(identifier2, <>) + * will not be considered a function definition. + * ansi2knr will recognize a multi-line header provided that no intervening + * line ends with a left or right brace or a semicolon. These algorithms + * ignore whitespace, comments, and preprocessor directives, except that + * the function name must be the first thing on the line. The following + * constructs will confuse it: + * - Any other construct that starts at the left margin and + * follows the above syntax (such as a macro or function call). + * - Some macros that tinker with the syntax of function headers. + */ + +/* + * The original and principal author of ansi2knr is L. Peter Deutsch + * . Other authors are noted in the change history + * that follows (in reverse chronological order): + + lpd 2000-04-12 backs out Eggert's changes because of bugs: + - concatlits didn't declare the type of its bufend argument; + - concatlits didn't recognize when it was inside a comment; + - scanstring could scan backward past the beginning of the string; when + - the check for \ + newline in scanstring was unnecessary. + + 2000-03-05 Paul Eggert + + Add support for concatenated string literals. + * ansi2knr.c (concatlits): New decl. + (main): Invoke concatlits to concatenate string literals. + (scanstring): Handle backslash-newline correctly. Work with + character constants. Fix bug when scanning backwards through + backslash-quote. Check for unterminated strings. + (convert1): Parse character constants, too. + (appendline, concatlits): New functions. + * ansi2knr.1: Document this. + + lpd 1999-08-17 added code to allow preprocessor directives + wherever comments are allowed + lpd 1999-04-12 added minor fixes from Pavel Roskin + for clean compilation with + gcc -W -Wall + lpd 1999-03-22 added hack to recognize lines consisting of + identifier1(identifier2, xxx) as *not* being procedures + lpd 1999-02-03 made indentation of preprocessor commands consistent + lpd 1999-01-28 fixed two bugs: a '/' in an argument list caused an + endless loop; quoted strings within an argument list + confused the parser + lpd 1999-01-24 added a check for write errors on the output, + suggested by Jim Meyering + lpd 1998-11-09 added further hack to recognize identifier(void) + as being a procedure + lpd 1998-10-23 added hack to recognize lines consisting of + identifier1(identifier2) as *not* being procedures + lpd 1997-12-08 made input_file optional; only closes input and/or + output file if not stdin or stdout respectively; prints + usage message on stderr rather than stdout; adds + --filename switch (changes suggested by + ) + lpd 1996-01-21 added code to cope with not HAVE_CONFIG_H and with + compilers that don't understand void, as suggested by + Tom Lane + lpd 1996-01-15 changed to require that the first non-comment token + on the line following a function header be a left brace, + to reduce sensitivity to macros, as suggested by Tom Lane + + lpd 1995-06-22 removed #ifndefs whose sole purpose was to define + undefined preprocessor symbols as 0; changed all #ifdefs + for configuration symbols to #ifs + lpd 1995-04-05 changed copyright notice to make it clear that + including ansi2knr in a program does not bring the entire + program under the GPL + lpd 1994-12-18 added conditionals for systems where ctype macros + don't handle 8-bit characters properly, suggested by + Francois Pinard ; + removed --varargs switch (this is now the default) + lpd 1994-10-10 removed CONFIG_BROKETS conditional + lpd 1994-07-16 added some conditionals to help GNU `configure', + suggested by Francois Pinard ; + properly erase prototype args in function parameters, + contributed by Jim Avera ; + correct error in writeblanks (it shouldn't erase EOLs) + lpd 1989-xx-xx original version + */ + +/* Most of the conditionals here are to make ansi2knr work with */ +/* or without the GNU configure machinery. */ + +#if HAVE_CONFIG_H +# include +#endif + +#include +#include + +#if HAVE_CONFIG_H + +/* + For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h). + This will define HAVE_CONFIG_H and so, activate the following lines. + */ + +# if STDC_HEADERS || HAVE_STRING_H +# include +# else +# include +# endif + +#else /* not HAVE_CONFIG_H */ + +/* Otherwise do it the hard way */ + +# ifdef BSD +# include +# else +# ifdef VMS + extern int strlen(), strncmp(); +# else +# include +# endif +# endif + +#endif /* not HAVE_CONFIG_H */ + +#if STDC_HEADERS +# include +#else +/* + malloc and free should be declared in stdlib.h, + but if you've got a K&R compiler, they probably aren't. + */ +# ifdef MSDOS +# include +# else +# ifdef VMS + extern char *malloc(); + extern void free(); +# else + extern char *malloc(); + extern int free(); +# endif +# endif + +#endif + +/* Define NULL (for *very* old compilers). */ +#ifndef NULL +# define NULL (0) +#endif + +/* + * The ctype macros don't always handle 8-bit characters correctly. + * Compensate for this here. + */ +#ifdef isascii +# undef HAVE_ISASCII /* just in case */ +# define HAVE_ISASCII 1 +#else +#endif +#if STDC_HEADERS || !HAVE_ISASCII +# define is_ascii(c) 1 +#else +# define is_ascii(c) isascii(c) +#endif + +#define is_space(c) (is_ascii(c) && isspace(c)) +#define is_alpha(c) (is_ascii(c) && isalpha(c)) +#define is_alnum(c) (is_ascii(c) && isalnum(c)) + +/* Scanning macros */ +#define isidchar(ch) (is_alnum(ch) || (ch) == '_') +#define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_') + +/* Forward references */ +char *ppdirforward(); +char *ppdirbackward(); +char *skipspace(); +char *scanstring(); +int writeblanks(); +int test1(); +int convert1(); + +/* The main program */ +int +main(argc, argv) + int argc; + char *argv[]; +{ FILE *in = stdin; + FILE *out = stdout; + char *filename = 0; + char *program_name = argv[0]; + char *output_name = 0; +#define bufsize 5000 /* arbitrary size */ + char *buf; + char *line; + char *more; + char *usage = + "Usage: ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]\n"; + /* + * In previous versions, ansi2knr recognized a --varargs switch. + * If this switch was supplied, ansi2knr would attempt to convert + * a ... argument to va_alist and va_dcl; if this switch was not + * supplied, ansi2knr would simply drop any such arguments. + * Now, ansi2knr always does this conversion, and we only + * check for this switch for backward compatibility. + */ + int convert_varargs = 1; + int output_error; + + while ( argc > 1 && argv[1][0] == '-' ) { + if ( !strcmp(argv[1], "--varargs") ) { + convert_varargs = 1; + argc--; + argv++; + continue; + } + if ( !strcmp(argv[1], "--filename") && argc > 2 ) { + filename = argv[2]; + argc -= 2; + argv += 2; + continue; + } + fprintf(stderr, "%s: Unrecognized switch: %s\n", program_name, + argv[1]); + fprintf(stderr, usage); + exit(1); + } + switch ( argc ) + { + default: + fprintf(stderr, usage); + exit(0); + case 3: + output_name = argv[2]; + out = fopen(output_name, "w"); + if ( out == NULL ) { + fprintf(stderr, "%s: Cannot open output file %s\n", + program_name, output_name); + exit(1); + } + /* falls through */ + case 2: + in = fopen(argv[1], "r"); + if ( in == NULL ) { + fprintf(stderr, "%s: Cannot open input file %s\n", + program_name, argv[1]); + exit(1); + } + if ( filename == 0 ) + filename = argv[1]; + /* falls through */ + case 1: + break; + } + if ( filename ) + fprintf(out, "#line 1 \"%s\"\n", filename); + buf = malloc(bufsize); + if ( buf == NULL ) + { + fprintf(stderr, "Unable to allocate read buffer!\n"); + exit(1); + } + line = buf; + while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL ) + { +test: line += strlen(line); + switch ( test1(buf) ) + { + case 2: /* a function header */ + convert1(buf, out, 1, convert_varargs); + break; + case 1: /* a function */ + /* Check for a { at the start of the next line. */ + more = ++line; +f: if ( line >= buf + (bufsize - 1) ) /* overflow check */ + goto wl; + if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL ) + goto wl; + switch ( *skipspace(ppdirforward(more), 1) ) + { + case '{': + /* Definitely a function header. */ + convert1(buf, out, 0, convert_varargs); + fputs(more, out); + break; + case 0: + /* The next line was blank or a comment: */ + /* keep scanning for a non-comment. */ + line += strlen(line); + goto f; + default: + /* buf isn't a function header, but */ + /* more might be. */ + fputs(buf, out); + strcpy(buf, more); + line = buf; + goto test; + } + break; + case -1: /* maybe the start of a function */ + if ( line != buf + (bufsize - 1) ) /* overflow check */ + continue; + /* falls through */ + default: /* not a function */ +wl: fputs(buf, out); + break; + } + line = buf; + } + if ( line != buf ) + fputs(buf, out); + free(buf); + if ( output_name ) { + output_error = ferror(out); + output_error |= fclose(out); + } else { /* out == stdout */ + fflush(out); + output_error = ferror(out); + } + if ( output_error ) { + fprintf(stderr, "%s: error writing to %s\n", program_name, + (output_name ? output_name : "stdout")); + exit(1); + } + if ( in != stdin ) + fclose(in); + return 0; +} + +/* + * Skip forward or backward over one or more preprocessor directives. + */ +char * +ppdirforward(p) + char *p; +{ + for (; *p == '#'; ++p) { + for (; *p != '\r' && *p != '\n'; ++p) + if (*p == 0) + return p; + if (*p == '\r' && p[1] == '\n') + ++p; + } + return p; +} +char * +ppdirbackward(p, limit) + char *p; + char *limit; +{ + char *np = p; + + for (;; p = --np) { + if (*np == '\n' && np[-1] == '\r') + --np; + for (; np > limit && np[-1] != '\r' && np[-1] != '\n'; --np) + if (np[-1] == 0) + return np; + if (*np != '#') + return p; + } +} + +/* + * Skip over whitespace, comments, and preprocessor directives, + * in either direction. + */ +char * +skipspace(p, dir) + char *p; + int dir; /* 1 for forward, -1 for backward */ +{ + for ( ; ; ) { + while ( is_space(*p) ) + p += dir; + if ( !(*p == '/' && p[dir] == '*') ) + break; + p += dir; p += dir; + while ( !(*p == '*' && p[dir] == '/') ) { + if ( *p == 0 ) + return p; /* multi-line comment?? */ + p += dir; + } + p += dir; p += dir; + } + return p; +} + +/* Scan over a quoted string, in either direction. */ +char * +scanstring(p, dir) + char *p; + int dir; +{ + for (p += dir; ; p += dir) + if (*p == '"' && p[-dir] != '\\') + return p + dir; +} + +/* + * Write blanks over part of a string. + * Don't overwrite end-of-line characters. + */ +int +writeblanks(start, end) + char *start; + char *end; +{ char *p; + for ( p = start; p < end; p++ ) + if ( *p != '\r' && *p != '\n' ) + *p = ' '; + return 0; +} + +/* + * Test whether the string in buf is a function definition. + * The string may contain and/or end with a newline. + * Return as follows: + * 0 - definitely not a function definition; + * 1 - definitely a function definition; + * 2 - definitely a function prototype (NOT USED); + * -1 - may be the beginning of a function definition, + * append another line and look again. + * The reason we don't attempt to convert function prototypes is that + * Ghostscript's declaration-generating macros look too much like + * prototypes, and confuse the algorithms. + */ +int +test1(buf) + char *buf; +{ char *p = buf; + char *bend; + char *endfn; + int contin; + + if ( !isidfirstchar(*p) ) + return 0; /* no name at left margin */ + bend = skipspace(ppdirbackward(buf + strlen(buf) - 1, buf), -1); + switch ( *bend ) + { + case ';': contin = 0 /*2*/; break; + case ')': contin = 1; break; + case '{': return 0; /* not a function */ + case '}': return 0; /* not a function */ + default: contin = -1; + } + while ( isidchar(*p) ) + p++; + endfn = p; + p = skipspace(p, 1); + if ( *p++ != '(' ) + return 0; /* not a function */ + p = skipspace(p, 1); + if ( *p == ')' ) + return 0; /* no parameters */ + /* Check that the apparent function name isn't a keyword. */ + /* We only need to check for keywords that could be followed */ + /* by a left parenthesis (which, unfortunately, is most of them). */ + { static char *words[] = + { "asm", "auto", "case", "char", "const", "double", + "extern", "float", "for", "if", "int", "long", + "register", "return", "short", "signed", "sizeof", + "static", "switch", "typedef", "unsigned", + "void", "volatile", "while", 0 + }; + char **key = words; + char *kp; + unsigned len = endfn - buf; + + while ( (kp = *key) != 0 ) + { if ( strlen(kp) == len && !strncmp(kp, buf, len) ) + return 0; /* name is a keyword */ + key++; + } + } + { + char *id = p; + int len; + /* + * Check for identifier1(identifier2) and not + * identifier1(void), or identifier1(identifier2, xxxx). + */ + + while ( isidchar(*p) ) + p++; + len = p - id; + p = skipspace(p, 1); + if (*p == ',' || + (*p == ')' && (len != 4 || strncmp(id, "void", 4))) + ) + return 0; /* not a function */ + } + /* + * If the last significant character was a ), we need to count + * parentheses, because it might be part of a formal parameter + * that is a procedure. + */ + if (contin > 0) { + int level = 0; + + for (p = skipspace(buf, 1); *p; p = skipspace(p + 1, 1)) + level += (*p == '(' ? 1 : *p == ')' ? -1 : 0); + if (level > 0) + contin = -1; + } + return contin; +} + +/* Convert a recognized function definition or header to K&R syntax. */ +int +convert1(buf, out, header, convert_varargs) + char *buf; + FILE *out; + int header; /* Boolean */ + int convert_varargs; /* Boolean */ +{ char *endfn; + char *p; + /* + * The breaks table contains pointers to the beginning and end + * of each argument. + */ + char **breaks; + unsigned num_breaks = 2; /* for testing */ + char **btop; + char **bp; + char **ap; + char *vararg = 0; + + /* Pre-ANSI implementations don't agree on whether strchr */ + /* is called strchr or index, so we open-code it here. */ + for ( endfn = buf; *(endfn++) != '('; ) + ; +top: p = endfn; + breaks = (char **)malloc(sizeof(char *) * num_breaks * 2); + if ( breaks == NULL ) + { /* Couldn't allocate break table, give up */ + fprintf(stderr, "Unable to allocate break table!\n"); + fputs(buf, out); + return -1; + } + btop = breaks + num_breaks * 2 - 2; + bp = breaks; + /* Parse the argument list */ + do + { int level = 0; + char *lp = NULL; + char *rp = NULL; + char *end = NULL; + + if ( bp >= btop ) + { /* Filled up break table. */ + /* Allocate a bigger one and start over. */ + free((char *)breaks); + num_breaks <<= 1; + goto top; + } + *bp++ = p; + /* Find the end of the argument */ + for ( ; end == NULL; p++ ) + { switch(*p) + { + case ',': + if ( !level ) end = p; + break; + case '(': + if ( !level ) lp = p; + level++; + break; + case ')': + if ( --level < 0 ) end = p; + else rp = p; + break; + case '/': + if (p[1] == '*') + p = skipspace(p, 1) - 1; + break; + case '"': + p = scanstring(p, 1) - 1; + break; + default: + ; + } + } + /* Erase any embedded prototype parameters. */ + if ( lp && rp ) + writeblanks(lp + 1, rp); + p--; /* back up over terminator */ + /* Find the name being declared. */ + /* This is complicated because of procedure and */ + /* array modifiers. */ + for ( ; ; ) + { p = skipspace(p - 1, -1); + switch ( *p ) + { + case ']': /* skip array dimension(s) */ + case ')': /* skip procedure args OR name */ + { int level = 1; + while ( level ) + switch ( *--p ) + { + case ']': case ')': + level++; + break; + case '[': case '(': + level--; + break; + case '/': + if (p > buf && p[-1] == '*') + p = skipspace(p, -1) + 1; + break; + case '"': + p = scanstring(p, -1) + 1; + break; + default: ; + } + } + if ( *p == '(' && *skipspace(p + 1, 1) == '*' ) + { /* We found the name being declared */ + while ( !isidfirstchar(*p) ) + p = skipspace(p, 1) + 1; + goto found; + } + break; + default: + goto found; + } + } +found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' ) + { if ( convert_varargs ) + { *bp++ = "va_alist"; + vararg = p-2; + } + else + { p++; + if ( bp == breaks + 1 ) /* sole argument */ + writeblanks(breaks[0], p); + else + writeblanks(bp[-1] - 1, p); + bp--; + } + } + else + { while ( isidchar(*p) ) p--; + *bp++ = p+1; + } + p = end; + } + while ( *p++ == ',' ); + *bp = p; + /* Make a special check for 'void' arglist */ + if ( bp == breaks+2 ) + { p = skipspace(breaks[0], 1); + if ( !strncmp(p, "void", 4) ) + { p = skipspace(p+4, 1); + if ( p == breaks[2] - 1 ) + { bp = breaks; /* yup, pretend arglist is empty */ + writeblanks(breaks[0], p + 1); + } + } + } + /* Put out the function name and left parenthesis. */ + p = buf; + while ( p != endfn ) putc(*p, out), p++; + /* Put out the declaration. */ + if ( header ) + { fputs(");", out); + for ( p = breaks[0]; *p; p++ ) + if ( *p == '\r' || *p == '\n' ) + putc(*p, out); + } + else + { for ( ap = breaks+1; ap < bp; ap += 2 ) + { p = *ap; + while ( isidchar(*p) ) + putc(*p, out), p++; + if ( ap < bp - 1 ) + fputs(", ", out); + } + fputs(") ", out); + /* Put out the argument declarations */ + for ( ap = breaks+2; ap <= bp; ap += 2 ) + (*ap)[-1] = ';'; + if ( vararg != 0 ) + { *vararg = 0; + fputs(breaks[0], out); /* any prior args */ + fputs("va_dcl", out); /* the final arg */ + fputs(bp[0], out); + } + else + fputs(breaks[0], out); + } + free((char *)breaks); + return 0; +} diff --git a/crypto777/jpeg/cderror.h b/crypto777/jpeg/cderror.h new file mode 100644 index 000000000..e19c475c5 --- /dev/null +++ b/crypto777/jpeg/cderror.h @@ -0,0 +1,134 @@ +/* + * cderror.h + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * Modified 2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file defines the error and message codes for the cjpeg/djpeg + * applications. These strings are not needed as part of the JPEG library + * proper. + * Edit this file to add new codes, or to translate the message strings to + * some other language. + */ + +/* + * To define the enum list of message codes, include this file without + * defining macro JMESSAGE. To create a message string table, include it + * again with a suitable JMESSAGE definition (see jerror.c for an example). + */ +#ifndef JMESSAGE +#ifndef CDERROR_H +#define CDERROR_H +/* First time through, define the enum list */ +#define JMAKE_ENUM_LIST +#else +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */ +#define JMESSAGE(code,string) +#endif /* CDERROR_H */ +#endif /* JMESSAGE */ + +#ifdef JMAKE_ENUM_LIST + +typedef enum { + +#define JMESSAGE(code,string) code , + +#endif /* JMAKE_ENUM_LIST */ + +JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */ + +#ifdef BMP_SUPPORTED +JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format") +JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported") +JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length") +JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1") +JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB") +JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported") +JMESSAGE(JERR_BMP_EMPTY, "Empty BMP image") +JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM") +JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image") +JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image") +JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image") +JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image") +#endif /* BMP_SUPPORTED */ + +#ifdef GIF_SUPPORTED +JMESSAGE(JERR_GIF_BUG, "GIF output got confused") +JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d") +JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB") +JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file") +JMESSAGE(JERR_GIF_NOT, "Not a GIF file") +JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image") +JMESSAGE(JTRC_GIF_BADVERSION, + "Warning: unexpected GIF version number '%c%c%c'") +JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x") +JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input") +JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file") +JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring") +JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image") +JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits") +#endif /* GIF_SUPPORTED */ + +#ifdef PPM_SUPPORTED +JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB") +JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file") +JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file") +JMESSAGE(JTRC_PGM, "%ux%u PGM image") +JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image") +JMESSAGE(JTRC_PPM, "%ux%u PPM image") +JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image") +#endif /* PPM_SUPPORTED */ + +#ifdef RLE_SUPPORTED +JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library") +JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB") +JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE") +JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file") +JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header") +JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header") +JMESSAGE(JERR_RLE_NOT, "Not an RLE file") +JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE") +JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup") +JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file") +JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d") +JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file") +JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d") +JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d") +#endif /* RLE_SUPPORTED */ + +#ifdef TARGA_SUPPORTED +JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format") +JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file") +JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB") +JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image") +JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image") +JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image") +#else +JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled") +#endif /* TARGA_SUPPORTED */ + +JMESSAGE(JERR_BAD_CMAP_FILE, + "Color map file is invalid or of unsupported format") +JMESSAGE(JERR_TOO_MANY_COLORS, + "Output file format cannot handle %d colormap entries") +JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed") +#ifdef TARGA_SUPPORTED +JMESSAGE(JERR_UNKNOWN_FORMAT, + "Unrecognized input file format --- perhaps you need -targa") +#else +JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format") +#endif +JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format") + +#ifdef JMAKE_ENUM_LIST + + JMSG_LASTADDONCODE +} ADDON_MESSAGE_CODE; + +#undef JMAKE_ENUM_LIST +#endif /* JMAKE_ENUM_LIST */ + +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */ +#undef JMESSAGE diff --git a/crypto777/jpeg/cdjpeg.c b/crypto777/jpeg/cdjpeg.c new file mode 100644 index 000000000..b6250ff97 --- /dev/null +++ b/crypto777/jpeg/cdjpeg.c @@ -0,0 +1,181 @@ +/* + * cdjpeg.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains common support routines used by the IJG application + * programs (cjpeg, djpeg, jpegtran). + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ +#include /* to declare isupper(), tolower() */ +#ifdef NEED_SIGNAL_CATCHER +#include /* to declare signal() */ +#endif +#ifdef USE_SETMODE +#include /* to declare setmode()'s parameter macros */ +/* If you have setmode() but not , just delete this line: */ +#include /* to declare setmode() */ +#endif + + +/* + * Signal catcher to ensure that temporary files are removed before aborting. + * NB: for Amiga Manx C this is actually a global routine named _abort(); + * we put "#define signal_catcher _abort" in jconfig.h. Talk about bogus... + */ + +#ifdef NEED_SIGNAL_CATCHER + +static j_common_ptr sig_cinfo; + +void /* must be global for Manx C */ +signal_catcher (int signum) +{ + if (sig_cinfo != NULL) { + if (sig_cinfo->err != NULL) /* turn off trace output */ + sig_cinfo->err->trace_level = 0; + jpeg_destroy(sig_cinfo); /* clean up memory allocation & temp files */ + } + exit(EXIT_FAILURE); +} + + +GLOBAL(void) +enable_signal_catcher (j_common_ptr cinfo) +{ + sig_cinfo = cinfo; +#ifdef SIGINT /* not all systems have SIGINT */ + signal(SIGINT, signal_catcher); +#endif +#ifdef SIGTERM /* not all systems have SIGTERM */ + signal(SIGTERM, signal_catcher); +#endif +} + +#endif + + +/* + * Optional progress monitor: display a percent-done figure on stderr. + */ + +#ifdef PROGRESS_REPORT + +METHODDEF(void) +progress_monitor (j_common_ptr cinfo) +{ + cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress; + int total_passes = prog->pub.total_passes + prog->total_extra_passes; + int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit); + + if (percent_done != prog->percent_done) { + prog->percent_done = percent_done; + if (total_passes > 1) { + fprintf(stderr, "\rPass %d/%d: %3d%% ", + prog->pub.completed_passes + prog->completed_extra_passes + 1, + total_passes, percent_done); + } else { + fprintf(stderr, "\r %3d%% ", percent_done); + } + fflush(stderr); + } +} + + +GLOBAL(void) +start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress) +{ + /* Enable progress display, unless trace output is on */ + if (cinfo->err->trace_level == 0) { + progress->pub.progress_monitor = progress_monitor; + progress->completed_extra_passes = 0; + progress->total_extra_passes = 0; + progress->percent_done = -1; + cinfo->progress = &progress->pub; + } +} + + +GLOBAL(void) +end_progress_monitor (j_common_ptr cinfo) +{ + /* Clear away progress display */ + if (cinfo->err->trace_level == 0) { + fprintf(stderr, "\r \r"); + fflush(stderr); + } +} + +#endif + + +/* + * Case-insensitive matching of possibly-abbreviated keyword switches. + * keyword is the constant keyword (must be lower case already), + * minchars is length of minimum legal abbreviation. + */ + +GLOBAL(boolean) +keymatch (char * arg, const char * keyword, int minchars) +{ + register int ca, ck; + register int nmatched = 0; + + while ((ca = *arg++) != '\0') { + if ((ck = *keyword++) == '\0') + return FALSE; /* arg longer than keyword, no good */ + if (isupper(ca)) /* force arg to lcase (assume ck is already) */ + ca = tolower(ca); + if (ca != ck) + return FALSE; /* no good */ + nmatched++; /* count matched characters */ + } + /* reached end of argument; fail if it's too short for unique abbrev */ + if (nmatched < minchars) + return FALSE; + return TRUE; /* A-OK */ +} + + +/* + * Routines to establish binary I/O mode for stdin and stdout. + * Non-Unix systems often require some hacking to get out of text mode. + */ + +GLOBAL(FILE *) +read_stdin (void) +{ + FILE * input_file = stdin; + +#ifdef USE_SETMODE /* need to hack file mode? */ + setmode(fileno(stdin), O_BINARY); +#endif +#ifdef USE_FDOPEN /* need to re-open in binary mode? */ + if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) { + fprintf(stderr, "Cannot reopen stdin\n"); + exit(EXIT_FAILURE); + } +#endif + return input_file; +} + + +GLOBAL(FILE *) +write_stdout (void) +{ + FILE * output_file = stdout; + +#ifdef USE_SETMODE /* need to hack file mode? */ + setmode(fileno(stdout), O_BINARY); +#endif +#ifdef USE_FDOPEN /* need to re-open in binary mode? */ + if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) { + fprintf(stderr, "Cannot reopen stdout\n"); + exit(EXIT_FAILURE); + } +#endif + return output_file; +} diff --git a/crypto777/jpeg/cdjpeg.h b/crypto777/jpeg/cdjpeg.h new file mode 100644 index 000000000..ed024ac3a --- /dev/null +++ b/crypto777/jpeg/cdjpeg.h @@ -0,0 +1,187 @@ +/* + * cdjpeg.h + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains common declarations for the sample applications + * cjpeg and djpeg. It is NOT used by the core JPEG library. + */ + +#define JPEG_CJPEG_DJPEG /* define proper options in jconfig.h */ +#define JPEG_INTERNAL_OPTIONS /* cjpeg.c,djpeg.c need to see xxx_SUPPORTED */ +#include "jinclude.h" +#include "jpeglib.h" +#include "jerror.h" /* get library error codes too */ +#include "cderror.h" /* get application-specific error codes */ + + +/* + * Object interface for cjpeg's source file decoding modules + */ + +typedef struct cjpeg_source_struct * cjpeg_source_ptr; + +struct cjpeg_source_struct { + JMETHOD(void, start_input, (j_compress_ptr cinfo, + cjpeg_source_ptr sinfo)); + JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo, + cjpeg_source_ptr sinfo)); + JMETHOD(void, finish_input, (j_compress_ptr cinfo, + cjpeg_source_ptr sinfo)); + + FILE *input_file; + + JSAMPARRAY buffer; + JDIMENSION buffer_height; +}; + + +/* + * Object interface for djpeg's output file encoding modules + */ + +typedef struct djpeg_dest_struct * djpeg_dest_ptr; + +struct djpeg_dest_struct { + /* start_output is called after jpeg_start_decompress finishes. + * The color map will be ready at this time, if one is needed. + */ + JMETHOD(void, start_output, (j_decompress_ptr cinfo, + djpeg_dest_ptr dinfo)); + /* Emit the specified number of pixel rows from the buffer. */ + JMETHOD(void, put_pixel_rows, (j_decompress_ptr cinfo, + djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied)); + /* Finish up at the end of the image. */ + JMETHOD(void, finish_output, (j_decompress_ptr cinfo, + djpeg_dest_ptr dinfo)); + + /* Target file spec; filled in by djpeg.c after object is created. */ + FILE * output_file; + + /* Output pixel-row buffer. Created by module init or start_output. + * Width is cinfo->output_width * cinfo->output_components; + * height is buffer_height. + */ + JSAMPARRAY buffer; + JDIMENSION buffer_height; +}; + + +/* + * cjpeg/djpeg may need to perform extra passes to convert to or from + * the source/destination file format. The JPEG library does not know + * about these passes, but we'd like them to be counted by the progress + * monitor. We use an expanded progress monitor object to hold the + * additional pass count. + */ + +struct cdjpeg_progress_mgr { + struct jpeg_progress_mgr pub; /* fields known to JPEG library */ + int completed_extra_passes; /* extra passes completed */ + int total_extra_passes; /* total extra */ + /* last printed percentage stored here to avoid multiple printouts */ + int percent_done; +}; + +typedef struct cdjpeg_progress_mgr * cd_progress_ptr; + + +/* Short forms of external names for systems with brain-damaged linkers. */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jinit_read_bmp jIRdBMP +#define jinit_write_bmp jIWrBMP +#define jinit_read_gif jIRdGIF +#define jinit_write_gif jIWrGIF +#define jinit_read_ppm jIRdPPM +#define jinit_write_ppm jIWrPPM +#define jinit_read_rle jIRdRLE +#define jinit_write_rle jIWrRLE +#define jinit_read_targa jIRdTarga +#define jinit_write_targa jIWrTarga +#define read_quant_tables RdQTables +#define read_scan_script RdScnScript +#define set_quality_ratings SetQRates +#define set_quant_slots SetQSlots +#define set_sample_factors SetSFacts +#define read_color_map RdCMap +#define enable_signal_catcher EnSigCatcher +#define start_progress_monitor StProgMon +#define end_progress_monitor EnProgMon +#define read_stdin RdStdin +#define write_stdout WrStdout +#endif /* NEED_SHORT_EXTERNAL_NAMES */ + +/* Module selection routines for I/O modules. */ + +EXTERN(cjpeg_source_ptr) jinit_read_bmp JPP((j_compress_ptr cinfo)); +EXTERN(djpeg_dest_ptr) jinit_write_bmp JPP((j_decompress_ptr cinfo, + boolean is_os2)); +EXTERN(cjpeg_source_ptr) jinit_read_gif JPP((j_compress_ptr cinfo)); +EXTERN(djpeg_dest_ptr) jinit_write_gif JPP((j_decompress_ptr cinfo)); +EXTERN(cjpeg_source_ptr) jinit_read_ppm JPP((j_compress_ptr cinfo)); +EXTERN(djpeg_dest_ptr) jinit_write_ppm JPP((j_decompress_ptr cinfo)); +EXTERN(cjpeg_source_ptr) jinit_read_rle JPP((j_compress_ptr cinfo)); +EXTERN(djpeg_dest_ptr) jinit_write_rle JPP((j_decompress_ptr cinfo)); +EXTERN(cjpeg_source_ptr) jinit_read_targa JPP((j_compress_ptr cinfo)); +EXTERN(djpeg_dest_ptr) jinit_write_targa JPP((j_decompress_ptr cinfo)); + +/* cjpeg support routines (in rdswitch.c) */ + +EXTERN(boolean) read_quant_tables JPP((j_compress_ptr cinfo, char * filename, + boolean force_baseline)); +EXTERN(boolean) read_scan_script JPP((j_compress_ptr cinfo, char * filename)); +EXTERN(boolean) set_quality_ratings JPP((j_compress_ptr cinfo, char *arg, + boolean force_baseline)); +EXTERN(boolean) set_quant_slots JPP((j_compress_ptr cinfo, char *arg)); +EXTERN(boolean) set_sample_factors JPP((j_compress_ptr cinfo, char *arg)); + +/* djpeg support routines (in rdcolmap.c) */ + +EXTERN(void) read_color_map JPP((j_decompress_ptr cinfo, FILE * infile)); + +/* common support routines (in cdjpeg.c) */ + +EXTERN(void) enable_signal_catcher JPP((j_common_ptr cinfo)); +EXTERN(void) start_progress_monitor JPP((j_common_ptr cinfo, + cd_progress_ptr progress)); +EXTERN(void) end_progress_monitor JPP((j_common_ptr cinfo)); +EXTERN(boolean) keymatch JPP((char * arg, const char * keyword, int minchars)); +EXTERN(FILE *) read_stdin JPP((void)); +EXTERN(FILE *) write_stdout JPP((void)); + +/* miscellaneous useful macros */ + +#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */ +#define READ_BINARY "r" +#define WRITE_BINARY "w" +#else +#ifdef VMS /* VMS is very nonstandard */ +#define READ_BINARY "rb", "ctx=stm" +#define WRITE_BINARY "wb", "ctx=stm" +#else /* standard ANSI-compliant case */ +#define READ_BINARY "rb" +#define WRITE_BINARY "wb" +#endif +#endif + +#ifndef EXIT_FAILURE /* define exit() codes if not provided */ +#define EXIT_FAILURE 1 +#endif +#ifndef EXIT_SUCCESS +#ifdef VMS +#define EXIT_SUCCESS 1 /* VMS is very nonstandard */ +#else +#define EXIT_SUCCESS 0 +#endif +#endif +#ifndef EXIT_WARNING +#ifdef VMS +#define EXIT_WARNING 1 /* VMS is very nonstandard */ +#else +#define EXIT_WARNING 2 +#endif +#endif diff --git a/crypto777/jpeg/cjpeg.c b/crypto777/jpeg/cjpeg.c new file mode 100644 index 000000000..9a9a09a72 --- /dev/null +++ b/crypto777/jpeg/cjpeg.c @@ -0,0 +1,643 @@ +/* + * cjpeg.c + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * Modified 2003-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a command-line user interface for the JPEG compressor. + * It should work on any system with Unix- or MS-DOS-style command lines. + * + * Two different command line styles are permitted, depending on the + * compile-time switch TWO_FILE_COMMANDLINE: + * cjpeg [options] inputfile outputfile + * cjpeg [options] [inputfile] + * In the second style, output is always to standard output, which you'd + * normally redirect to a file or pipe to some other program. Input is + * either from a named file or from standard input (typically redirected). + * The second style is convenient on Unix but is unhelpful on systems that + * don't support pipes. Also, you MUST use the first style if your system + * doesn't do binary I/O to stdin/stdout. + * To simplify script writing, the "-outfile" switch is provided. The syntax + * cjpeg [options] -outfile outputfile inputfile + * works regardless of which command line style is used. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ +#include "jversion.h" /* for version message */ + +#ifdef USE_CCOMMAND /* command-line reader for Macintosh */ +#ifdef __MWERKS__ +#include /* Metrowerks needs this */ +#include /* ... and this */ +#endif +#ifdef THINK_C +#include /* Think declares it here */ +#endif +#endif + + +/* Create the add-on message string table. */ + +#define JMESSAGE(code,string) string , + +static const char * const cdjpeg_message_table[] = { +#include "cderror.h" + NULL +}; + + +/* + * This routine determines what format the input file is, + * and selects the appropriate input-reading module. + * + * To determine which family of input formats the file belongs to, + * we may look only at the first byte of the file, since C does not + * guarantee that more than one character can be pushed back with ungetc. + * Looking at additional bytes would require one of these approaches: + * 1) assume we can fseek() the input file (fails for piped input); + * 2) assume we can push back more than one character (works in + * some C implementations, but unportable); + * 3) provide our own buffering (breaks input readers that want to use + * stdio directly, such as the RLE library); + * or 4) don't put back the data, and modify the input_init methods to assume + * they start reading after the start of file (also breaks RLE library). + * #1 is attractive for MS-DOS but is untenable on Unix. + * + * The most portable solution for file types that can't be identified by their + * first byte is to make the user tell us what they are. This is also the + * only approach for "raw" file types that contain only arbitrary values. + * We presently apply this method for Targa files. Most of the time Targa + * files start with 0x00, so we recognize that case. Potentially, however, + * a Targa file could start with any byte value (byte 0 is the length of the + * seldom-used ID field), so we provide a switch to force Targa input mode. + */ + +static boolean is_targa; /* records user -targa switch */ + + +LOCAL(cjpeg_source_ptr) +select_file_type (j_compress_ptr cinfo, FILE * infile) +{ + int c; + + if (is_targa) { +#ifdef TARGA_SUPPORTED + return jinit_read_targa(cinfo); +#else + ERREXIT(cinfo, JERR_TGA_NOTCOMP); +#endif + } + + if ((c = getc(infile)) == EOF) + ERREXIT(cinfo, JERR_INPUT_EMPTY); + if (ungetc(c, infile) == EOF) + ERREXIT(cinfo, JERR_UNGETC_FAILED); + + switch (c) { +#ifdef BMP_SUPPORTED + case 'B': + return jinit_read_bmp(cinfo); +#endif +#ifdef GIF_SUPPORTED + case 'G': + return jinit_read_gif(cinfo); +#endif +#ifdef PPM_SUPPORTED + case 'P': + return jinit_read_ppm(cinfo); +#endif +#ifdef RLE_SUPPORTED + case 'R': + return jinit_read_rle(cinfo); +#endif +#ifdef TARGA_SUPPORTED + case 0x00: + return jinit_read_targa(cinfo); +#endif + default: + ERREXIT(cinfo, JERR_UNKNOWN_FORMAT); + break; + } + + return NULL; /* suppress compiler warnings */ +} + + +/* + * Argument-parsing code. + * The switch parser is designed to be useful with DOS-style command line + * syntax, ie, intermixed switches and file names, where only the switches + * to the left of a given file name affect processing of that file. + * The main program in this file doesn't actually use this capability... + */ + + +static const char * progname; /* program name for error messages */ +static char * outfilename; /* for -outfile switch */ + + +LOCAL(void) +usage (void) +/* complain about bad command line */ +{ + fprintf(stderr, "usage: %s [switches] ", progname); +#ifdef TWO_FILE_COMMANDLINE + fprintf(stderr, "inputfile outputfile\n"); +#else + fprintf(stderr, "[inputfile]\n"); +#endif + + fprintf(stderr, "Switches (names may be abbreviated):\n"); + fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is useful range)\n"); + fprintf(stderr, " -grayscale Create monochrome JPEG file\n"); + fprintf(stderr, " -rgb Create RGB JPEG file\n"); +#ifdef ENTROPY_OPT_SUPPORTED + fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n"); +#endif +#ifdef C_PROGRESSIVE_SUPPORTED + fprintf(stderr, " -progressive Create progressive JPEG file\n"); +#endif +#ifdef DCT_SCALING_SUPPORTED + fprintf(stderr, " -scale M/N Scale image by fraction M/N, eg, 1/2\n"); +#endif +#ifdef TARGA_SUPPORTED + fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n"); +#endif + fprintf(stderr, "Switches for advanced users:\n"); +#ifdef C_ARITH_CODING_SUPPORTED + fprintf(stderr, " -arithmetic Use arithmetic coding\n"); +#endif +#ifdef DCT_SCALING_SUPPORTED + fprintf(stderr, " -block N DCT block size (1..16; default is 8)\n"); +#endif +#ifdef DCT_ISLOW_SUPPORTED + fprintf(stderr, " -dct int Use integer DCT method%s\n", + (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); +#endif +#ifdef DCT_IFAST_SUPPORTED + fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n", + (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); +#endif +#ifdef DCT_FLOAT_SUPPORTED + fprintf(stderr, " -dct float Use floating-point DCT method%s\n", + (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); +#endif + fprintf(stderr, " -nosmooth Don't use high-quality downsampling\n"); + fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); +#ifdef INPUT_SMOOTHING_SUPPORTED + fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n"); +#endif + fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); + fprintf(stderr, " -outfile name Specify name for output file\n"); + fprintf(stderr, " -verbose or -debug Emit debug output\n"); + fprintf(stderr, "Switches for wizards:\n"); + fprintf(stderr, " -baseline Force baseline quantization tables\n"); + fprintf(stderr, " -qtables file Use quantization tables given in file\n"); + fprintf(stderr, " -qslots N[,...] Set component quantization tables\n"); + fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n"); +#ifdef C_MULTISCAN_FILES_SUPPORTED + fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n"); +#endif + exit(EXIT_FAILURE); +} + + +LOCAL(int) +parse_switches (j_compress_ptr cinfo, int argc, char **argv, + int last_file_arg_seen, boolean for_real) +/* Parse optional switches. + * Returns argv[] index of first file-name argument (== argc if none). + * Any file names with indexes <= last_file_arg_seen are ignored; + * they have presumably been processed in a previous iteration. + * (Pass 0 for last_file_arg_seen on the first or only iteration.) + * for_real is FALSE on the first (dummy) pass; we may skip any expensive + * processing. + */ +{ + int argn; + char * arg; + boolean force_baseline; + boolean simple_progressive; + char * qualityarg = NULL; /* saves -quality parm if any */ + char * qtablefile = NULL; /* saves -qtables filename if any */ + char * qslotsarg = NULL; /* saves -qslots parm if any */ + char * samplearg = NULL; /* saves -sample parm if any */ + char * scansarg = NULL; /* saves -scans parm if any */ + + /* Set up default JPEG parameters. */ + + force_baseline = FALSE; /* by default, allow 16-bit quantizers */ + simple_progressive = FALSE; + is_targa = FALSE; + outfilename = NULL; + cinfo->err->trace_level = 0; + + /* Scan command line options, adjust parameters */ + + for (argn = 1; argn < argc; argn++) { + arg = argv[argn]; + if (*arg != '-') { + /* Not a switch, must be a file name argument */ + if (argn <= last_file_arg_seen) { + outfilename = NULL; /* -outfile applies to just one input file */ + continue; /* ignore this name if previously processed */ + } + break; /* else done parsing switches */ + } + arg++; /* advance past switch marker character */ + + if (keymatch(arg, "arithmetic", 1)) { + /* Use arithmetic coding. */ +#ifdef C_ARITH_CODING_SUPPORTED + cinfo->arith_code = TRUE; +#else + fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "baseline", 2)) { + /* Force baseline-compatible output (8-bit quantizer values). */ + force_baseline = TRUE; + + } else if (keymatch(arg, "block", 2)) { + /* Set DCT block size. */ +#if defined DCT_SCALING_SUPPORTED && JPEG_LIB_VERSION_MAJOR >= 8 && \ + (JPEG_LIB_VERSION_MAJOR > 8 || JPEG_LIB_VERSION_MINOR >= 3) + int val; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%d", &val) != 1) + usage(); + if (val < 1 || val > 16) + usage(); + cinfo->block_size = val; +#else + fprintf(stderr, "%s: sorry, block size setting not supported\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "dct", 2)) { + /* Select DCT algorithm. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (keymatch(argv[argn], "int", 1)) { + cinfo->dct_method = JDCT_ISLOW; + } else if (keymatch(argv[argn], "fast", 2)) { + cinfo->dct_method = JDCT_IFAST; + } else if (keymatch(argv[argn], "float", 2)) { + cinfo->dct_method = JDCT_FLOAT; + } else + usage(); + + } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { + /* Enable debug printouts. */ + /* On first -d, print version identification */ + static boolean printed_version = FALSE; + + if (! printed_version) { + fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n", + JVERSION, JCOPYRIGHT); + printed_version = TRUE; + } + cinfo->err->trace_level++; + + } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) { + /* Force a monochrome JPEG file to be generated. */ + jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); + + } else if (keymatch(arg, "rgb", 3)) { + /* Force an RGB JPEG file to be generated. */ + jpeg_set_colorspace(cinfo, JCS_RGB); + + } else if (keymatch(arg, "maxmemory", 3)) { + /* Maximum memory in Kb (or Mb with 'm'). */ + long lval; + char ch = 'x'; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) + usage(); + if (ch == 'm' || ch == 'M') + lval *= 1000L; + cinfo->mem->max_memory_to_use = lval * 1000L; + + } else if (keymatch(arg, "nosmooth", 3)) { + /* Suppress fancy downsampling */ + cinfo->do_fancy_downsampling = FALSE; + + } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { + /* Enable entropy parm optimization. */ +#ifdef ENTROPY_OPT_SUPPORTED + cinfo->optimize_coding = TRUE; +#else + fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "outfile", 4)) { + /* Set output file name. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + outfilename = argv[argn]; /* save it away for later use */ + + } else if (keymatch(arg, "progressive", 1)) { + /* Select simple progressive mode. */ +#ifdef C_PROGRESSIVE_SUPPORTED + simple_progressive = TRUE; + /* We must postpone execution until num_components is known. */ +#else + fprintf(stderr, "%s: sorry, progressive output was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "quality", 1)) { + /* Quality ratings (quantization table scaling factors). */ + if (++argn >= argc) /* advance to next argument */ + usage(); + qualityarg = argv[argn]; + + } else if (keymatch(arg, "qslots", 2)) { + /* Quantization table slot numbers. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + qslotsarg = argv[argn]; + /* Must delay setting qslots until after we have processed any + * colorspace-determining switches, since jpeg_set_colorspace sets + * default quant table numbers. + */ + + } else if (keymatch(arg, "qtables", 2)) { + /* Quantization tables fetched from file. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + qtablefile = argv[argn]; + /* We postpone actually reading the file in case -quality comes later. */ + + } else if (keymatch(arg, "restart", 1)) { + /* Restart interval in MCU rows (or in MCUs with 'b'). */ + long lval; + char ch = 'x'; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) + usage(); + if (lval < 0 || lval > 65535L) + usage(); + if (ch == 'b' || ch == 'B') { + cinfo->restart_interval = (unsigned int) lval; + cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ + } else { + cinfo->restart_in_rows = (int) lval; + /* restart_interval will be computed during startup */ + } + + } else if (keymatch(arg, "sample", 2)) { + /* Set sampling factors. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + samplearg = argv[argn]; + /* Must delay setting sample factors until after we have processed any + * colorspace-determining switches, since jpeg_set_colorspace sets + * default sampling factors. + */ + + } else if (keymatch(arg, "scale", 4)) { + /* Scale the image by a fraction M/N. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%d/%d", + &cinfo->scale_num, &cinfo->scale_denom) != 2) + usage(); + + } else if (keymatch(arg, "scans", 4)) { + /* Set scan script. */ +#ifdef C_MULTISCAN_FILES_SUPPORTED + if (++argn >= argc) /* advance to next argument */ + usage(); + scansarg = argv[argn]; + /* We must postpone reading the file in case -progressive appears. */ +#else + fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "smooth", 2)) { + /* Set input smoothing factor. */ + int val; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%d", &val) != 1) + usage(); + if (val < 0 || val > 100) + usage(); + cinfo->smoothing_factor = val; + + } else if (keymatch(arg, "targa", 1)) { + /* Input file is Targa format. */ + is_targa = TRUE; + + } else { + usage(); /* bogus switch */ + } + } + + /* Post-switch-scanning cleanup */ + + if (for_real) { + + /* Set quantization tables for selected quality. */ + /* Some or all may be overridden if -qtables is present. */ + if (qualityarg != NULL) /* process -quality if it was present */ + if (! set_quality_ratings(cinfo, qualityarg, force_baseline)) + usage(); + + if (qtablefile != NULL) /* process -qtables if it was present */ + if (! read_quant_tables(cinfo, qtablefile, force_baseline)) + usage(); + + if (qslotsarg != NULL) /* process -qslots if it was present */ + if (! set_quant_slots(cinfo, qslotsarg)) + usage(); + + if (samplearg != NULL) /* process -sample if it was present */ + if (! set_sample_factors(cinfo, samplearg)) + usage(); + +#ifdef C_PROGRESSIVE_SUPPORTED + if (simple_progressive) /* process -progressive; -scans can override */ + jpeg_simple_progression(cinfo); +#endif + +#ifdef C_MULTISCAN_FILES_SUPPORTED + if (scansarg != NULL) /* process -scans if it was present */ + if (! read_scan_script(cinfo, scansarg)) + usage(); +#endif + } + + return argn; /* return index of next arg (file name) */ +} + + +/* + * The main program. + */ + +int +main (int argc, char **argv) +{ + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; +#ifdef PROGRESS_REPORT + struct cdjpeg_progress_mgr progress; +#endif + int file_index; + cjpeg_source_ptr src_mgr; + FILE * input_file; + FILE * output_file; + JDIMENSION num_scanlines; + + /* On Mac, fetch a command line. */ +#ifdef USE_CCOMMAND + argc = ccommand(&argv); +#endif + + progname = argv[0]; + if (progname == NULL || progname[0] == 0) + progname = "cjpeg"; /* in case C library doesn't provide it */ + + /* Initialize the JPEG compression object with default error handling. */ + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + /* Add some application-specific error messages (from cderror.h) */ + jerr.addon_message_table = cdjpeg_message_table; + jerr.first_addon_message = JMSG_FIRSTADDONCODE; + jerr.last_addon_message = JMSG_LASTADDONCODE; + + /* Now safe to enable signal catcher. */ +#ifdef NEED_SIGNAL_CATCHER + enable_signal_catcher((j_common_ptr) &cinfo); +#endif + + /* Initialize JPEG parameters. + * Much of this may be overridden later. + * In particular, we don't yet know the input file's color space, + * but we need to provide some value for jpeg_set_defaults() to work. + */ + + cinfo.in_color_space = JCS_RGB; /* arbitrary guess */ + jpeg_set_defaults(&cinfo); + + /* Scan command line to find file names. + * It is convenient to use just one switch-parsing routine, but the switch + * values read here are ignored; we will rescan the switches after opening + * the input file. + */ + + file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); + +#ifdef TWO_FILE_COMMANDLINE + /* Must have either -outfile switch or explicit output file name */ + if (outfilename == NULL) { + if (file_index != argc-2) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + outfilename = argv[file_index+1]; + } else { + if (file_index != argc-1) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + } +#else + /* Unix style: expect zero or one file name */ + if (file_index < argc-1) { + fprintf(stderr, "%s: only one input file\n", progname); + usage(); + } +#endif /* TWO_FILE_COMMANDLINE */ + + /* Open the input file. */ + if (file_index < argc) { + if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); + exit(EXIT_FAILURE); + } + } else { + /* default input file is stdin */ + input_file = read_stdin(); + } + + /* Open the output file. */ + if (outfilename != NULL) { + if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, outfilename); + exit(EXIT_FAILURE); + } + } else { + /* default output file is stdout */ + output_file = write_stdout(); + } + +#ifdef PROGRESS_REPORT + start_progress_monitor((j_common_ptr) &cinfo, &progress); +#endif + + /* Figure out the input file format, and set up to read it. */ + src_mgr = select_file_type(&cinfo, input_file); + src_mgr->input_file = input_file; + + /* Read the input file header to obtain file size & colorspace. */ + (*src_mgr->start_input) (&cinfo, src_mgr); + + /* Now that we know input colorspace, fix colorspace-dependent defaults */ + jpeg_default_colorspace(&cinfo); + + /* Adjust default compression parameters by re-parsing the options */ + file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); + + /* Specify data destination for compression */ + jpeg_stdio_dest(&cinfo, output_file); + + /* Start compressor */ + jpeg_start_compress(&cinfo, TRUE); + + /* Process data */ + while (cinfo.next_scanline < cinfo.image_height) { + num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); + (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); + } + + /* Finish compression and release memory */ + (*src_mgr->finish_input) (&cinfo, src_mgr); + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); + + /* Close files, if we opened them */ + if (input_file != stdin) + fclose(input_file); + if (output_file != stdout) + fclose(output_file); + +#ifdef PROGRESS_REPORT + end_progress_monitor((j_common_ptr) &cinfo); +#endif + + /* All done. */ + exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); + return 0; /* suppress no-return-value warnings */ +} diff --git a/crypto777/jpeg/ckconfig.c b/crypto777/jpeg/ckconfig.c new file mode 100644 index 000000000..e658623fa --- /dev/null +++ b/crypto777/jpeg/ckconfig.c @@ -0,0 +1,402 @@ +/* + * ckconfig.c + * + * Copyright (C) 1991-1994, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + */ + +/* + * This program is intended to help you determine how to configure the JPEG + * software for installation on a particular system. The idea is to try to + * compile and execute this program. If your compiler fails to compile the + * program, make changes as indicated in the comments below. Once you can + * compile the program, run it, and it will produce a "jconfig.h" file for + * your system. + * + * As a general rule, each time you try to compile this program, + * pay attention only to the *first* error message you get from the compiler. + * Many C compilers will issue lots of spurious error messages once they + * have gotten confused. Go to the line indicated in the first error message, + * and read the comments preceding that line to see what to change. + * + * Almost all of the edits you may need to make to this program consist of + * changing a line that reads "#define SOME_SYMBOL" to "#undef SOME_SYMBOL", + * or vice versa. This is called defining or undefining that symbol. + */ + + +/* First we must see if your system has the include files we need. + * We start out with the assumption that your system has all the ANSI-standard + * include files. If you get any error trying to include one of these files, + * undefine the corresponding HAVE_xxx symbol. + */ + +#define HAVE_STDDEF_H /* replace 'define' by 'undef' if error here */ +#ifdef HAVE_STDDEF_H /* next line will be skipped if you undef... */ +#include +#endif + +#define HAVE_STDLIB_H /* same thing for stdlib.h */ +#ifdef HAVE_STDLIB_H +#include +#endif + +#include /* If you ain't got this, you ain't got C. */ + +/* We have to see if your string functions are defined by + * strings.h (old BSD convention) or string.h (everybody else). + * We try the non-BSD convention first; define NEED_BSD_STRINGS + * if the compiler says it can't find string.h. + */ + +#undef NEED_BSD_STRINGS + +#ifdef NEED_BSD_STRINGS +#include +#else +#include +#endif + +/* On some systems (especially older Unix machines), type size_t is + * defined only in the include file . If you get a failure + * on the size_t test below, try defining NEED_SYS_TYPES_H. + */ + +#undef NEED_SYS_TYPES_H /* start by assuming we don't need it */ +#ifdef NEED_SYS_TYPES_H +#include +#endif + + +/* Usually type size_t is defined in one of the include files we've included + * above. If not, you'll get an error on the "typedef size_t my_size_t;" line. + * In that case, first try defining NEED_SYS_TYPES_H just above. + * If that doesn't work, you'll have to search through your system library + * to figure out which include file defines "size_t". Look for a line that + * says "typedef something-or-other size_t;". Then, change the line below + * that says "#include " to instead include the file + * you found size_t in, and define NEED_SPECIAL_INCLUDE. If you can't find + * type size_t anywhere, try replacing "#include " with + * "typedef unsigned int size_t;". + */ + +#undef NEED_SPECIAL_INCLUDE /* assume we DON'T need it, for starters */ + +#ifdef NEED_SPECIAL_INCLUDE +#include +#endif + +typedef size_t my_size_t; /* The payoff: do we have size_t now? */ + + +/* The next question is whether your compiler supports ANSI-style function + * prototypes. You need to know this in order to choose between using + * makefile.ansi and using makefile.unix. + * The #define line below is set to assume you have ANSI function prototypes. + * If you get an error in this group of lines, undefine HAVE_PROTOTYPES. + */ + +#define HAVE_PROTOTYPES + +#ifdef HAVE_PROTOTYPES +int testfunction (int arg1, int * arg2); /* check prototypes */ + +struct methods_struct { /* check method-pointer declarations */ + int (*error_exit) (char *msgtext); + int (*trace_message) (char *msgtext); + int (*another_method) (void); +}; + +int testfunction (int arg1, int * arg2) /* check definitions */ +{ + return arg2[arg1]; +} + +int test2function (void) /* check void arg list */ +{ + return 0; +} +#endif + + +/* Now we want to find out if your compiler knows what "unsigned char" means. + * If you get an error on the "unsigned char un_char;" line, + * then undefine HAVE_UNSIGNED_CHAR. + */ + +#define HAVE_UNSIGNED_CHAR + +#ifdef HAVE_UNSIGNED_CHAR +unsigned char un_char; +#endif + + +/* Now we want to find out if your compiler knows what "unsigned short" means. + * If you get an error on the "unsigned short un_short;" line, + * then undefine HAVE_UNSIGNED_SHORT. + */ + +#define HAVE_UNSIGNED_SHORT + +#ifdef HAVE_UNSIGNED_SHORT +unsigned short un_short; +#endif + + +/* Now we want to find out if your compiler understands type "void". + * If you get an error anywhere in here, undefine HAVE_VOID. + */ + +#define HAVE_VOID + +#ifdef HAVE_VOID +/* Caution: a C++ compiler will insist on complete prototypes */ +typedef void * void_ptr; /* check void * */ +#ifdef HAVE_PROTOTYPES /* check ptr to function returning void */ +typedef void (*void_func) (int a, int b); +#else +typedef void (*void_func) (); +#endif + +#ifdef HAVE_PROTOTYPES /* check void function result */ +void test3function (void_ptr arg1, void_func arg2) +#else +void test3function (arg1, arg2) + void_ptr arg1; + void_func arg2; +#endif +{ + char * locptr = (char *) arg1; /* check casting to and from void * */ + arg1 = (void *) locptr; + (*arg2) (1, 2); /* check call of fcn returning void */ +} +#endif + + +/* Now we want to find out if your compiler knows what "const" means. + * If you get an error here, undefine HAVE_CONST. + */ + +#define HAVE_CONST + +#ifdef HAVE_CONST +static const int carray[3] = {1, 2, 3}; + +#ifdef HAVE_PROTOTYPES +int test4function (const int arg1) +#else +int test4function (arg1) + const int arg1; +#endif +{ + return carray[arg1]; +} +#endif + + +/* If you get an error or warning about this structure definition, + * define INCOMPLETE_TYPES_BROKEN. + */ + +#undef INCOMPLETE_TYPES_BROKEN + +#ifndef INCOMPLETE_TYPES_BROKEN +typedef struct undefined_structure * undef_struct_ptr; +#endif + + +/* If you get an error about duplicate names, + * define NEED_SHORT_EXTERNAL_NAMES. + */ + +#undef NEED_SHORT_EXTERNAL_NAMES + +#ifndef NEED_SHORT_EXTERNAL_NAMES + +int possibly_duplicate_function () +{ + return 0; +} + +int possibly_dupli_function () +{ + return 1; +} + +#endif + + + +/************************************************************************ + * OK, that's it. You should not have to change anything beyond this + * point in order to compile and execute this program. (You might get + * some warnings, but you can ignore them.) + * When you run the program, it will make a couple more tests that it + * can do automatically, and then it will create jconfig.h and print out + * any additional suggestions it has. + ************************************************************************ + */ + + +#ifdef HAVE_PROTOTYPES +int is_char_signed (int arg) +#else +int is_char_signed (arg) + int arg; +#endif +{ + if (arg == 189) { /* expected result for unsigned char */ + return 0; /* type char is unsigned */ + } + else if (arg != -67) { /* expected result for signed char */ + printf("Hmm, it seems 'char' is not eight bits wide on your machine.\n"); + printf("I fear the JPEG software will not work at all.\n\n"); + } + return 1; /* assume char is signed otherwise */ +} + + +#ifdef HAVE_PROTOTYPES +int is_shifting_signed (long arg) +#else +int is_shifting_signed (arg) + long arg; +#endif +/* See whether right-shift on a long is signed or not. */ +{ + long res = arg >> 4; + + if (res == -0x7F7E80CL) { /* expected result for signed shift */ + return 1; /* right shift is signed */ + } + /* see if unsigned-shift hack will fix it. */ + /* we can't just test exact value since it depends on width of long... */ + res |= (~0L) << (32-4); + if (res == -0x7F7E80CL) { /* expected result now? */ + return 0; /* right shift is unsigned */ + } + printf("Right shift isn't acting as I expect it to.\n"); + printf("I fear the JPEG software will not work at all.\n\n"); + return 0; /* try it with unsigned anyway */ +} + + +#ifdef HAVE_PROTOTYPES +int main (int argc, char ** argv) +#else +int main (argc, argv) + int argc; + char ** argv; +#endif +{ + char signed_char_check = (char) (-67); + FILE *outfile; + + /* Attempt to write jconfig.h */ + if ((outfile = fopen("jconfig.h", "w")) == NULL) { + printf("Failed to write jconfig.h\n"); + return 1; + } + + /* Write out all the info */ + fprintf(outfile, "/* jconfig.h --- generated by ckconfig.c */\n"); + fprintf(outfile, "/* see jconfig.txt for explanations */\n\n"); +#ifdef HAVE_PROTOTYPES + fprintf(outfile, "#define HAVE_PROTOTYPES\n"); +#else + fprintf(outfile, "#undef HAVE_PROTOTYPES\n"); +#endif +#ifdef HAVE_UNSIGNED_CHAR + fprintf(outfile, "#define HAVE_UNSIGNED_CHAR\n"); +#else + fprintf(outfile, "#undef HAVE_UNSIGNED_CHAR\n"); +#endif +#ifdef HAVE_UNSIGNED_SHORT + fprintf(outfile, "#define HAVE_UNSIGNED_SHORT\n"); +#else + fprintf(outfile, "#undef HAVE_UNSIGNED_SHORT\n"); +#endif +#ifdef HAVE_VOID + fprintf(outfile, "/* #define void char */\n"); +#else + fprintf(outfile, "#define void char\n"); +#endif +#ifdef HAVE_CONST + fprintf(outfile, "/* #define const */\n"); +#else + fprintf(outfile, "#define const\n"); +#endif + if (is_char_signed((int) signed_char_check)) + fprintf(outfile, "#undef CHAR_IS_UNSIGNED\n"); + else + fprintf(outfile, "#define CHAR_IS_UNSIGNED\n"); +#ifdef HAVE_STDDEF_H + fprintf(outfile, "#define HAVE_STDDEF_H\n"); +#else + fprintf(outfile, "#undef HAVE_STDDEF_H\n"); +#endif +#ifdef HAVE_STDLIB_H + fprintf(outfile, "#define HAVE_STDLIB_H\n"); +#else + fprintf(outfile, "#undef HAVE_STDLIB_H\n"); +#endif +#ifdef NEED_BSD_STRINGS + fprintf(outfile, "#define NEED_BSD_STRINGS\n"); +#else + fprintf(outfile, "#undef NEED_BSD_STRINGS\n"); +#endif +#ifdef NEED_SYS_TYPES_H + fprintf(outfile, "#define NEED_SYS_TYPES_H\n"); +#else + fprintf(outfile, "#undef NEED_SYS_TYPES_H\n"); +#endif + fprintf(outfile, "#undef NEED_FAR_POINTERS\n"); +#ifdef NEED_SHORT_EXTERNAL_NAMES + fprintf(outfile, "#define NEED_SHORT_EXTERNAL_NAMES\n"); +#else + fprintf(outfile, "#undef NEED_SHORT_EXTERNAL_NAMES\n"); +#endif +#ifdef INCOMPLETE_TYPES_BROKEN + fprintf(outfile, "#define INCOMPLETE_TYPES_BROKEN\n"); +#else + fprintf(outfile, "#undef INCOMPLETE_TYPES_BROKEN\n"); +#endif + fprintf(outfile, "\n#ifdef JPEG_INTERNALS\n\n"); + if (is_shifting_signed(-0x7F7E80B1L)) + fprintf(outfile, "#undef RIGHT_SHIFT_IS_UNSIGNED\n"); + else + fprintf(outfile, "#define RIGHT_SHIFT_IS_UNSIGNED\n"); + fprintf(outfile, "\n#endif /* JPEG_INTERNALS */\n"); + fprintf(outfile, "\n#ifdef JPEG_CJPEG_DJPEG\n\n"); + fprintf(outfile, "#define BMP_SUPPORTED /* BMP image file format */\n"); + fprintf(outfile, "#define GIF_SUPPORTED /* GIF image file format */\n"); + fprintf(outfile, "#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */\n"); + fprintf(outfile, "#undef RLE_SUPPORTED /* Utah RLE image file format */\n"); + fprintf(outfile, "#define TARGA_SUPPORTED /* Targa image file format */\n\n"); + fprintf(outfile, "#undef TWO_FILE_COMMANDLINE /* You may need this on non-Unix systems */\n"); + fprintf(outfile, "#undef NEED_SIGNAL_CATCHER /* Define this if you use jmemname.c */\n"); + fprintf(outfile, "#undef DONT_USE_B_MODE\n"); + fprintf(outfile, "/* #define PROGRESS_REPORT */ /* optional */\n"); + fprintf(outfile, "\n#endif /* JPEG_CJPEG_DJPEG */\n"); + + /* Close the jconfig.h file */ + fclose(outfile); + + /* User report */ + printf("Configuration check for Independent JPEG Group's software done.\n"); + printf("\nI have written the jconfig.h file for you.\n\n"); +#ifdef HAVE_PROTOTYPES + printf("You should use makefile.ansi as the starting point for your Makefile.\n"); +#else + printf("You should use makefile.unix as the starting point for your Makefile.\n"); +#endif + +#ifdef NEED_SPECIAL_INCLUDE + printf("\nYou'll need to change jconfig.h to include the system include file\n"); + printf("that you found type size_t in, or add a direct definition of type\n"); + printf("size_t if that's what you used. Just add it to the end.\n"); +#endif + + return 0; +} diff --git a/crypto777/jpeg/djpeg.c b/crypto777/jpeg/djpeg.c new file mode 100644 index 000000000..bc544dc10 --- /dev/null +++ b/crypto777/jpeg/djpeg.c @@ -0,0 +1,617 @@ +/* + * djpeg.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a command-line user interface for the JPEG decompressor. + * It should work on any system with Unix- or MS-DOS-style command lines. + * + * Two different command line styles are permitted, depending on the + * compile-time switch TWO_FILE_COMMANDLINE: + * djpeg [options] inputfile outputfile + * djpeg [options] [inputfile] + * In the second style, output is always to standard output, which you'd + * normally redirect to a file or pipe to some other program. Input is + * either from a named file or from standard input (typically redirected). + * The second style is convenient on Unix but is unhelpful on systems that + * don't support pipes. Also, you MUST use the first style if your system + * doesn't do binary I/O to stdin/stdout. + * To simplify script writing, the "-outfile" switch is provided. The syntax + * djpeg [options] -outfile outputfile inputfile + * works regardless of which command line style is used. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ +#include "jversion.h" /* for version message */ + +#include /* to declare isprint() */ + +#ifdef USE_CCOMMAND /* command-line reader for Macintosh */ +#ifdef __MWERKS__ +#include /* Metrowerks needs this */ +#include /* ... and this */ +#endif +#ifdef THINK_C +#include /* Think declares it here */ +#endif +#endif + + +/* Create the add-on message string table. */ + +#define JMESSAGE(code,string) string , + +static const char * const cdjpeg_message_table[] = { +#include "cderror.h" + NULL +}; + + +/* + * This list defines the known output image formats + * (not all of which need be supported by a given version). + * You can change the default output format by defining DEFAULT_FMT; + * indeed, you had better do so if you undefine PPM_SUPPORTED. + */ + +typedef enum { + FMT_BMP, /* BMP format (Windows flavor) */ + FMT_GIF, /* GIF format */ + FMT_OS2, /* BMP format (OS/2 flavor) */ + FMT_PPM, /* PPM/PGM (PBMPLUS formats) */ + FMT_RLE, /* RLE format */ + FMT_TARGA, /* Targa format */ + FMT_TIFF /* TIFF format */ +} IMAGE_FORMATS; + +#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */ +#define DEFAULT_FMT FMT_PPM +#endif + +static IMAGE_FORMATS requested_fmt; + + +/* + * Argument-parsing code. + * The switch parser is designed to be useful with DOS-style command line + * syntax, ie, intermixed switches and file names, where only the switches + * to the left of a given file name affect processing of that file. + * The main program in this file doesn't actually use this capability... + */ + + +static const char * progname; /* program name for error messages */ +static char * outfilename; /* for -outfile switch */ + + +LOCAL(void) +usage (void) +/* complain about bad command line */ +{ + fprintf(stderr, "usage: %s [switches] ", progname); +#ifdef TWO_FILE_COMMANDLINE + fprintf(stderr, "inputfile outputfile\n"); +#else + fprintf(stderr, "[inputfile]\n"); +#endif + + fprintf(stderr, "Switches (names may be abbreviated):\n"); + fprintf(stderr, " -colors N Reduce image to no more than N colors\n"); + fprintf(stderr, " -fast Fast, low-quality processing\n"); + fprintf(stderr, " -grayscale Force grayscale output\n"); +#ifdef IDCT_SCALING_SUPPORTED + fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n"); +#endif +#ifdef BMP_SUPPORTED + fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n", + (DEFAULT_FMT == FMT_BMP ? " (default)" : "")); +#endif +#ifdef GIF_SUPPORTED + fprintf(stderr, " -gif Select GIF output format%s\n", + (DEFAULT_FMT == FMT_GIF ? " (default)" : "")); +#endif +#ifdef BMP_SUPPORTED + fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n", + (DEFAULT_FMT == FMT_OS2 ? " (default)" : "")); +#endif +#ifdef PPM_SUPPORTED + fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n", + (DEFAULT_FMT == FMT_PPM ? " (default)" : "")); +#endif +#ifdef RLE_SUPPORTED + fprintf(stderr, " -rle Select Utah RLE output format%s\n", + (DEFAULT_FMT == FMT_RLE ? " (default)" : "")); +#endif +#ifdef TARGA_SUPPORTED + fprintf(stderr, " -targa Select Targa output format%s\n", + (DEFAULT_FMT == FMT_TARGA ? " (default)" : "")); +#endif + fprintf(stderr, "Switches for advanced users:\n"); +#ifdef DCT_ISLOW_SUPPORTED + fprintf(stderr, " -dct int Use integer DCT method%s\n", + (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); +#endif +#ifdef DCT_IFAST_SUPPORTED + fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n", + (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); +#endif +#ifdef DCT_FLOAT_SUPPORTED + fprintf(stderr, " -dct float Use floating-point DCT method%s\n", + (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); +#endif + fprintf(stderr, " -dither fs Use F-S dithering (default)\n"); + fprintf(stderr, " -dither none Don't use dithering in quantization\n"); + fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n"); +#ifdef QUANT_2PASS_SUPPORTED + fprintf(stderr, " -map FILE Map to colors used in named image file\n"); +#endif + fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n"); +#ifdef QUANT_1PASS_SUPPORTED + fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n"); +#endif + fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); + fprintf(stderr, " -outfile name Specify name for output file\n"); + fprintf(stderr, " -verbose or -debug Emit debug output\n"); + exit(EXIT_FAILURE); +} + + +LOCAL(int) +parse_switches (j_decompress_ptr cinfo, int argc, char **argv, + int last_file_arg_seen, boolean for_real) +/* Parse optional switches. + * Returns argv[] index of first file-name argument (== argc if none). + * Any file names with indexes <= last_file_arg_seen are ignored; + * they have presumably been processed in a previous iteration. + * (Pass 0 for last_file_arg_seen on the first or only iteration.) + * for_real is FALSE on the first (dummy) pass; we may skip any expensive + * processing. + */ +{ + int argn; + char * arg; + + /* Set up default JPEG parameters. */ + requested_fmt = DEFAULT_FMT; /* set default output file format */ + outfilename = NULL; + cinfo->err->trace_level = 0; + + /* Scan command line options, adjust parameters */ + + for (argn = 1; argn < argc; argn++) { + arg = argv[argn]; + if (*arg != '-') { + /* Not a switch, must be a file name argument */ + if (argn <= last_file_arg_seen) { + outfilename = NULL; /* -outfile applies to just one input file */ + continue; /* ignore this name if previously processed */ + } + break; /* else done parsing switches */ + } + arg++; /* advance past switch marker character */ + + if (keymatch(arg, "bmp", 1)) { + /* BMP output format. */ + requested_fmt = FMT_BMP; + + } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) || + keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) { + /* Do color quantization. */ + int val; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%d", &val) != 1) + usage(); + cinfo->desired_number_of_colors = val; + cinfo->quantize_colors = TRUE; + + } else if (keymatch(arg, "dct", 2)) { + /* Select IDCT algorithm. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (keymatch(argv[argn], "int", 1)) { + cinfo->dct_method = JDCT_ISLOW; + } else if (keymatch(argv[argn], "fast", 2)) { + cinfo->dct_method = JDCT_IFAST; + } else if (keymatch(argv[argn], "float", 2)) { + cinfo->dct_method = JDCT_FLOAT; + } else + usage(); + + } else if (keymatch(arg, "dither", 2)) { + /* Select dithering algorithm. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (keymatch(argv[argn], "fs", 2)) { + cinfo->dither_mode = JDITHER_FS; + } else if (keymatch(argv[argn], "none", 2)) { + cinfo->dither_mode = JDITHER_NONE; + } else if (keymatch(argv[argn], "ordered", 2)) { + cinfo->dither_mode = JDITHER_ORDERED; + } else + usage(); + + } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { + /* Enable debug printouts. */ + /* On first -d, print version identification */ + static boolean printed_version = FALSE; + + if (! printed_version) { + fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n", + JVERSION, JCOPYRIGHT); + printed_version = TRUE; + } + cinfo->err->trace_level++; + + } else if (keymatch(arg, "fast", 1)) { + /* Select recommended processing options for quick-and-dirty output. */ + cinfo->two_pass_quantize = FALSE; + cinfo->dither_mode = JDITHER_ORDERED; + if (! cinfo->quantize_colors) /* don't override an earlier -colors */ + cinfo->desired_number_of_colors = 216; + cinfo->dct_method = JDCT_FASTEST; + cinfo->do_fancy_upsampling = FALSE; + + } else if (keymatch(arg, "gif", 1)) { + /* GIF output format. */ + requested_fmt = FMT_GIF; + + } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) { + /* Force monochrome output. */ + cinfo->out_color_space = JCS_GRAYSCALE; + + } else if (keymatch(arg, "map", 3)) { + /* Quantize to a color map taken from an input file. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (for_real) { /* too expensive to do twice! */ +#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */ + FILE * mapfile; + + if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); + exit(EXIT_FAILURE); + } + read_color_map(cinfo, mapfile); + fclose(mapfile); + cinfo->quantize_colors = TRUE; +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } + + } else if (keymatch(arg, "maxmemory", 3)) { + /* Maximum memory in Kb (or Mb with 'm'). */ + long lval; + char ch = 'x'; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) + usage(); + if (ch == 'm' || ch == 'M') + lval *= 1000L; + cinfo->mem->max_memory_to_use = lval * 1000L; + + } else if (keymatch(arg, "nosmooth", 3)) { + /* Suppress fancy upsampling */ + cinfo->do_fancy_upsampling = FALSE; + + } else if (keymatch(arg, "onepass", 3)) { + /* Use fast one-pass quantization. */ + cinfo->two_pass_quantize = FALSE; + + } else if (keymatch(arg, "os2", 3)) { + /* BMP output format (OS/2 flavor). */ + requested_fmt = FMT_OS2; + + } else if (keymatch(arg, "outfile", 4)) { + /* Set output file name. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + outfilename = argv[argn]; /* save it away for later use */ + + } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) { + /* PPM/PGM output format. */ + requested_fmt = FMT_PPM; + + } else if (keymatch(arg, "rle", 1)) { + /* RLE output format. */ + requested_fmt = FMT_RLE; + + } else if (keymatch(arg, "scale", 1)) { + /* Scale the output image by a fraction M/N. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%d/%d", + &cinfo->scale_num, &cinfo->scale_denom) < 1) + usage(); + + } else if (keymatch(arg, "targa", 1)) { + /* Targa output format. */ + requested_fmt = FMT_TARGA; + + } else { + usage(); /* bogus switch */ + } + } + + return argn; /* return index of next arg (file name) */ +} + + +/* + * Marker processor for COM and interesting APPn markers. + * This replaces the library's built-in processor, which just skips the marker. + * We want to print out the marker as text, to the extent possible. + * Note this code relies on a non-suspending data source. + */ + +LOCAL(unsigned int) +jpeg_getc (j_decompress_ptr cinfo) +/* Read next byte */ +{ + struct jpeg_source_mgr * datasrc = cinfo->src; + + if (datasrc->bytes_in_buffer == 0) { + if (! (*datasrc->fill_input_buffer) (cinfo)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); + } + datasrc->bytes_in_buffer--; + return GETJOCTET(*datasrc->next_input_byte++); +} + + +METHODDEF(boolean) +print_text_marker (j_decompress_ptr cinfo) +{ + boolean traceit = (cinfo->err->trace_level >= 1); + INT32 length; + unsigned int ch; + unsigned int lastch = 0; + + length = jpeg_getc(cinfo) << 8; + length += jpeg_getc(cinfo); + length -= 2; /* discount the length word itself */ + + if (traceit) { + if (cinfo->unread_marker == JPEG_COM) + fprintf(stderr, "Comment, length %ld:\n", (long) length); + else /* assume it is an APPn otherwise */ + fprintf(stderr, "APP%d, length %ld:\n", + cinfo->unread_marker - JPEG_APP0, (long) length); + } + + while (--length >= 0) { + ch = jpeg_getc(cinfo); + if (traceit) { + /* Emit the character in a readable form. + * Nonprintables are converted to \nnn form, + * while \ is converted to \\. + * Newlines in CR, CR/LF, or LF form will be printed as one newline. + */ + if (ch == '\r') { + fprintf(stderr, "\n"); + } else if (ch == '\n') { + if (lastch != '\r') + fprintf(stderr, "\n"); + } else if (ch == '\\') { + fprintf(stderr, "\\\\"); + } else if (isprint(ch)) { + putc(ch, stderr); + } else { + fprintf(stderr, "\\%03o", ch); + } + lastch = ch; + } + } + + if (traceit) + fprintf(stderr, "\n"); + + return TRUE; +} + + +/* + * The main program. + */ + +int +main (int argc, char **argv) +{ + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; +#ifdef PROGRESS_REPORT + struct cdjpeg_progress_mgr progress; +#endif + int file_index; + djpeg_dest_ptr dest_mgr = NULL; + FILE * input_file; + FILE * output_file; + JDIMENSION num_scanlines; + + /* On Mac, fetch a command line. */ +#ifdef USE_CCOMMAND + argc = ccommand(&argv); +#endif + + progname = argv[0]; + if (progname == NULL || progname[0] == 0) + progname = "djpeg"; /* in case C library doesn't provide it */ + + /* Initialize the JPEG decompression object with default error handling. */ + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_decompress(&cinfo); + /* Add some application-specific error messages (from cderror.h) */ + jerr.addon_message_table = cdjpeg_message_table; + jerr.first_addon_message = JMSG_FIRSTADDONCODE; + jerr.last_addon_message = JMSG_LASTADDONCODE; + + /* Insert custom marker processor for COM and APP12. + * APP12 is used by some digital camera makers for textual info, + * so we provide the ability to display it as text. + * If you like, additional APPn marker types can be selected for display, + * but don't try to override APP0 or APP14 this way (see libjpeg.doc). + */ + jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker); + jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker); + + /* Now safe to enable signal catcher. */ +#ifdef NEED_SIGNAL_CATCHER + enable_signal_catcher((j_common_ptr) &cinfo); +#endif + + /* Scan command line to find file names. */ + /* It is convenient to use just one switch-parsing routine, but the switch + * values read here are ignored; we will rescan the switches after opening + * the input file. + * (Exception: tracing level set here controls verbosity for COM markers + * found during jpeg_read_header...) + */ + + file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); + +#ifdef TWO_FILE_COMMANDLINE + /* Must have either -outfile switch or explicit output file name */ + if (outfilename == NULL) { + if (file_index != argc-2) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + outfilename = argv[file_index+1]; + } else { + if (file_index != argc-1) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + } +#else + /* Unix style: expect zero or one file name */ + if (file_index < argc-1) { + fprintf(stderr, "%s: only one input file\n", progname); + usage(); + } +#endif /* TWO_FILE_COMMANDLINE */ + + /* Open the input file. */ + if (file_index < argc) { + if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); + exit(EXIT_FAILURE); + } + } else { + /* default input file is stdin */ + input_file = read_stdin(); + } + + /* Open the output file. */ + if (outfilename != NULL) { + if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, outfilename); + exit(EXIT_FAILURE); + } + } else { + /* default output file is stdout */ + output_file = write_stdout(); + } + +#ifdef PROGRESS_REPORT + start_progress_monitor((j_common_ptr) &cinfo, &progress); +#endif + + /* Specify data source for decompression */ + jpeg_stdio_src(&cinfo, input_file); + + /* Read file header, set default decompression parameters */ + (void) jpeg_read_header(&cinfo, TRUE); + + /* Adjust default decompression parameters by re-parsing the options */ + file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); + + /* Initialize the output module now to let it override any crucial + * option settings (for instance, GIF wants to force color quantization). + */ + switch (requested_fmt) { +#ifdef BMP_SUPPORTED + case FMT_BMP: + dest_mgr = jinit_write_bmp(&cinfo, FALSE); + break; + case FMT_OS2: + dest_mgr = jinit_write_bmp(&cinfo, TRUE); + break; +#endif +#ifdef GIF_SUPPORTED + case FMT_GIF: + dest_mgr = jinit_write_gif(&cinfo); + break; +#endif +#ifdef PPM_SUPPORTED + case FMT_PPM: + dest_mgr = jinit_write_ppm(&cinfo); + break; +#endif +#ifdef RLE_SUPPORTED + case FMT_RLE: + dest_mgr = jinit_write_rle(&cinfo); + break; +#endif +#ifdef TARGA_SUPPORTED + case FMT_TARGA: + dest_mgr = jinit_write_targa(&cinfo); + break; +#endif + default: + ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT); + break; + } + dest_mgr->output_file = output_file; + + /* Start decompressor */ + (void) jpeg_start_decompress(&cinfo); + + /* Write output file header */ + (*dest_mgr->start_output) (&cinfo, dest_mgr); + + /* Process data */ + while (cinfo.output_scanline < cinfo.output_height) { + num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer, + dest_mgr->buffer_height); + (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); + } + +#ifdef PROGRESS_REPORT + /* Hack: count final pass as done in case finish_output does an extra pass. + * The library won't have updated completed_passes. + */ + progress.pub.completed_passes = progress.pub.total_passes; +#endif + + /* Finish decompression and release memory. + * I must do it in this order because output module has allocated memory + * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory. + */ + (*dest_mgr->finish_output) (&cinfo, dest_mgr); + (void) jpeg_finish_decompress(&cinfo); + jpeg_destroy_decompress(&cinfo); + + /* Close files, if we opened them */ + if (input_file != stdin) + fclose(input_file); + if (output_file != stdout) + fclose(output_file); + +#ifdef PROGRESS_REPORT + end_progress_monitor((j_common_ptr) &cinfo); +#endif + + /* All done. */ + exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); + return 0; /* suppress no-return-value warnings */ +} diff --git a/crypto777/jpeg/example.c b/crypto777/jpeg/example.c new file mode 100644 index 000000000..1d6f6cc30 --- /dev/null +++ b/crypto777/jpeg/example.c @@ -0,0 +1,433 @@ +/* + * example.c + * + * This file illustrates how to use the IJG code as a subroutine library + * to read or write JPEG image files. You should look at this code in + * conjunction with the documentation file libjpeg.txt. + * + * This code will not do anything useful as-is, but it may be helpful as a + * skeleton for constructing routines that call the JPEG library. + * + * We present these routines in the same coding style used in the JPEG code + * (ANSI function definitions, etc); but you are of course free to code your + * routines in a different style if you prefer. + */ + +#include + +/* + * Include file for users of JPEG library. + * You will need to have included system headers that define at least + * the typedefs FILE and size_t before you can include jpeglib.h. + * (stdio.h is sufficient on ANSI-conforming systems.) + * You may also wish to include "jerror.h". + */ + +#include "jpeglib.h" + +/* + * is used for the optional error recovery mechanism shown in + * the second part of the example. + */ + +#include + + + +/******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/ + +/* This half of the example shows how to feed data into the JPEG compressor. + * We present a minimal version that does not worry about refinements such + * as error recovery (the JPEG code will just exit() if it gets an error). + */ + + +/* + * IMAGE DATA FORMATS: + * + * The standard input image format is a rectangular array of pixels, with + * each pixel having the same number of "component" values (color channels). + * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars). + * If you are working with color data, then the color values for each pixel + * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit + * RGB color. + * + * For this example, we'll assume that this data structure matches the way + * our application has stored the image in memory, so we can just pass a + * pointer to our image buffer. In particular, let's say that the image is + * RGB color and is described by: + */ + +extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */ +extern int image_height; /* Number of rows in image */ +extern int image_width; /* Number of columns in image */ + + +/* + * Sample routine for JPEG compression. We assume that the target file name + * and a compression quality factor are passed in. + */ + +GLOBAL(void) +write_JPEG_file (char * filename, int quality) +{ + /* This struct contains the JPEG compression parameters and pointers to + * working space (which is allocated as needed by the JPEG library). + * It is possible to have several such structures, representing multiple + * compression/decompression processes, in existence at once. We refer + * to any one struct (and its associated working data) as a "JPEG object". + */ + struct jpeg_compress_struct cinfo; + /* This struct represents a JPEG error handler. It is declared separately + * because applications often want to supply a specialized error handler + * (see the second half of this file for an example). But here we just + * take the easy way out and use the standard error handler, which will + * print a message on stderr and call exit() if compression fails. + * Note that this struct must live as long as the main JPEG parameter + * struct, to avoid dangling-pointer problems. + */ + struct jpeg_error_mgr jerr; + /* More stuff */ + FILE * outfile; /* target file */ + JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ + int row_stride; /* physical row width in image buffer */ + + /* Step 1: allocate and initialize JPEG compression object */ + + /* We have to set up the error handler first, in case the initialization + * step fails. (Unlikely, but it could happen if you are out of memory.) + * This routine fills in the contents of struct jerr, and returns jerr's + * address which we place into the link field in cinfo. + */ + cinfo.err = jpeg_std_error(&jerr); + /* Now we can initialize the JPEG compression object. */ + jpeg_create_compress(&cinfo); + + /* Step 2: specify data destination (eg, a file) */ + /* Note: steps 2 and 3 can be done in either order. */ + + /* Here we use the library-supplied code to send compressed data to a + * stdio stream. You can also write your own code to do something else. + * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that + * requires it in order to write binary files. + */ + if ((outfile = fopen(filename, "wb")) == NULL) { + fprintf(stderr, "can't open %s\n", filename); + exit(1); + } + jpeg_stdio_dest(&cinfo, outfile); + + /* Step 3: set parameters for compression */ + + /* First we supply a description of the input image. + * Four fields of the cinfo struct must be filled in: + */ + cinfo.image_width = image_width; /* image width and height, in pixels */ + cinfo.image_height = image_height; + cinfo.input_components = 3; /* # of color components per pixel */ + cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ + /* Now use the library's routine to set default compression parameters. + * (You must set at least cinfo.in_color_space before calling this, + * since the defaults depend on the source color space.) + */ + jpeg_set_defaults(&cinfo); + /* Now you can set any non-default parameters you wish to. + * Here we just illustrate the use of quality (quantization table) scaling: + */ + jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); + + /* Step 4: Start compressor */ + + /* TRUE ensures that we will write a complete interchange-JPEG file. + * Pass TRUE unless you are very sure of what you're doing. + */ + jpeg_start_compress(&cinfo, TRUE); + + /* Step 5: while (scan lines remain to be written) */ + /* jpeg_write_scanlines(...); */ + + /* Here we use the library's state variable cinfo.next_scanline as the + * loop counter, so that we don't have to keep track ourselves. + * To keep things simple, we pass one scanline per call; you can pass + * more if you wish, though. + */ + row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */ + + while (cinfo.next_scanline < cinfo.image_height) { + /* jpeg_write_scanlines expects an array of pointers to scanlines. + * Here the array is only one element long, but you could pass + * more than one scanline at a time if that's more convenient. + */ + row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; + (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + + /* Step 6: Finish compression */ + + jpeg_finish_compress(&cinfo); + /* After finish_compress, we can close the output file. */ + fclose(outfile); + + /* Step 7: release JPEG compression object */ + + /* This is an important step since it will release a good deal of memory. */ + jpeg_destroy_compress(&cinfo); + + /* And we're done! */ +} + + +/* + * SOME FINE POINTS: + * + * In the above loop, we ignored the return value of jpeg_write_scanlines, + * which is the number of scanlines actually written. We could get away + * with this because we were only relying on the value of cinfo.next_scanline, + * which will be incremented correctly. If you maintain additional loop + * variables then you should be careful to increment them properly. + * Actually, for output to a stdio stream you needn't worry, because + * then jpeg_write_scanlines will write all the lines passed (or else exit + * with a fatal error). Partial writes can only occur if you use a data + * destination module that can demand suspension of the compressor. + * (If you don't know what that's for, you don't need it.) + * + * If the compressor requires full-image buffers (for entropy-coding + * optimization or a multi-scan JPEG file), it will create temporary + * files for anything that doesn't fit within the maximum-memory setting. + * (Note that temp files are NOT needed if you use the default parameters.) + * On some systems you may need to set up a signal handler to ensure that + * temporary files are deleted if the program is interrupted. See libjpeg.txt. + * + * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG + * files to be compatible with everyone else's. If you cannot readily read + * your data in that order, you'll need an intermediate array to hold the + * image. See rdtarga.c or rdbmp.c for examples of handling bottom-to-top + * source data using the JPEG code's internal virtual-array mechanisms. + */ + + + +/******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/ + +/* This half of the example shows how to read data from the JPEG decompressor. + * It's a bit more refined than the above, in that we show: + * (a) how to modify the JPEG library's standard error-reporting behavior; + * (b) how to allocate workspace using the library's memory manager. + * + * Just to make this example a little different from the first one, we'll + * assume that we do not intend to put the whole image into an in-memory + * buffer, but to send it line-by-line someplace else. We need a one- + * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG + * memory manager allocate it for us. This approach is actually quite useful + * because we don't need to remember to deallocate the buffer separately: it + * will go away automatically when the JPEG object is cleaned up. + */ + + +/* + * ERROR HANDLING: + * + * The JPEG library's standard error handler (jerror.c) is divided into + * several "methods" which you can override individually. This lets you + * adjust the behavior without duplicating a lot of code, which you might + * have to update with each future release. + * + * Our example here shows how to override the "error_exit" method so that + * control is returned to the library's caller when a fatal error occurs, + * rather than calling exit() as the standard error_exit method does. + * + * We use C's setjmp/longjmp facility to return control. This means that the + * routine which calls the JPEG library must first execute a setjmp() call to + * establish the return point. We want the replacement error_exit to do a + * longjmp(). But we need to make the setjmp buffer accessible to the + * error_exit routine. To do this, we make a private extension of the + * standard JPEG error handler object. (If we were using C++, we'd say we + * were making a subclass of the regular error handler.) + * + * Here's the extended error handler struct: + */ + +struct my_error_mgr { + struct jpeg_error_mgr pub; /* "public" fields */ + + jmp_buf setjmp_buffer; /* for return to caller */ +}; + +typedef struct my_error_mgr * my_error_ptr; + +/* + * Here's the routine that will replace the standard error_exit method: + */ + +METHODDEF(void) +my_error_exit (j_common_ptr cinfo) +{ + /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ + my_error_ptr myerr = (my_error_ptr) cinfo->err; + + /* Always display the message. */ + /* We could postpone this until after returning, if we chose. */ + (*cinfo->err->output_message) (cinfo); + + /* Return control to the setjmp point */ + longjmp(myerr->setjmp_buffer, 1); +} + + +/* + * Sample routine for JPEG decompression. We assume that the source file name + * is passed in. We want to return 1 on success, 0 on error. + */ + + +GLOBAL(int) +read_JPEG_file (char * filename) +{ + /* This struct contains the JPEG decompression parameters and pointers to + * working space (which is allocated as needed by the JPEG library). + */ + struct jpeg_decompress_struct cinfo; + /* We use our private extension JPEG error handler. + * Note that this struct must live as long as the main JPEG parameter + * struct, to avoid dangling-pointer problems. + */ + struct my_error_mgr jerr; + /* More stuff */ + FILE * infile; /* source file */ + JSAMPARRAY buffer; /* Output row buffer */ + int row_stride; /* physical row width in output buffer */ + + /* In this example we want to open the input file before doing anything else, + * so that the setjmp() error recovery below can assume the file is open. + * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that + * requires it in order to read binary files. + */ + + if ((infile = fopen(filename, "rb")) == NULL) { + fprintf(stderr, "can't open %s\n", filename); + return 0; + } + + /* Step 1: allocate and initialize JPEG decompression object */ + + /* We set up the normal JPEG error routines, then override error_exit. */ + cinfo.err = jpeg_std_error(&jerr.pub); + jerr.pub.error_exit = my_error_exit; + /* Establish the setjmp return context for my_error_exit to use. */ + if (setjmp(jerr.setjmp_buffer)) { + /* If we get here, the JPEG code has signaled an error. + * We need to clean up the JPEG object, close the input file, and return. + */ + jpeg_destroy_decompress(&cinfo); + fclose(infile); + return 0; + } + /* Now we can initialize the JPEG decompression object. */ + jpeg_create_decompress(&cinfo); + + /* Step 2: specify data source (eg, a file) */ + + jpeg_stdio_src(&cinfo, infile); + + /* Step 3: read file parameters with jpeg_read_header() */ + + (void) jpeg_read_header(&cinfo, TRUE); + /* We can ignore the return value from jpeg_read_header since + * (a) suspension is not possible with the stdio data source, and + * (b) we passed TRUE to reject a tables-only JPEG file as an error. + * See libjpeg.txt for more info. + */ + + /* Step 4: set parameters for decompression */ + + /* In this example, we don't need to change any of the defaults set by + * jpeg_read_header(), so we do nothing here. + */ + + /* Step 5: Start decompressor */ + + (void) jpeg_start_decompress(&cinfo); + /* We can ignore the return value since suspension is not possible + * with the stdio data source. + */ + + /* We may need to do some setup of our own at this point before reading + * the data. After jpeg_start_decompress() we have the correct scaled + * output image dimensions available, as well as the output colormap + * if we asked for color quantization. + * In this example, we need to make an output work buffer of the right size. + */ + /* JSAMPLEs per row in output buffer */ + row_stride = cinfo.output_width * cinfo.output_components; + /* Make a one-row-high sample array that will go away when done with image */ + buffer = (*cinfo.mem->alloc_sarray) + ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); + + /* Step 6: while (scan lines remain to be read) */ + /* jpeg_read_scanlines(...); */ + + /* Here we use the library's state variable cinfo.output_scanline as the + * loop counter, so that we don't have to keep track ourselves. + */ + while (cinfo.output_scanline < cinfo.output_height) { + /* jpeg_read_scanlines expects an array of pointers to scanlines. + * Here the array is only one element long, but you could ask for + * more than one scanline at a time if that's more convenient. + */ + (void) jpeg_read_scanlines(&cinfo, buffer, 1); + /* Assume put_scanline_someplace wants a pointer and sample count. */ + put_scanline_someplace(buffer[0], row_stride); + } + + /* Step 7: Finish decompression */ + + (void) jpeg_finish_decompress(&cinfo); + /* We can ignore the return value since suspension is not possible + * with the stdio data source. + */ + + /* Step 8: Release JPEG decompression object */ + + /* This is an important step since it will release a good deal of memory. */ + jpeg_destroy_decompress(&cinfo); + + /* After finish_decompress, we can close the input file. + * Here we postpone it until after no more JPEG errors are possible, + * so as to simplify the setjmp error logic above. (Actually, I don't + * think that jpeg_destroy can do an error exit, but why assume anything...) + */ + fclose(infile); + + /* At this point you may want to check to see whether any corrupt-data + * warnings occurred (test whether jerr.pub.num_warnings is nonzero). + */ + + /* And we're done! */ + return 1; +} + + +/* + * SOME FINE POINTS: + * + * In the above code, we ignored the return value of jpeg_read_scanlines, + * which is the number of scanlines actually read. We could get away with + * this because we asked for only one line at a time and we weren't using + * a suspending data source. See libjpeg.txt for more info. + * + * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress(); + * we should have done it beforehand to ensure that the space would be + * counted against the JPEG max_memory setting. In some systems the above + * code would risk an out-of-memory error. However, in general we don't + * know the output image dimensions before jpeg_start_decompress(), unless we + * call jpeg_calc_output_dimensions(). See libjpeg.txt for more about this. + * + * Scanlines are returned in the same order as they appear in the JPEG file, + * which is standardly top-to-bottom. If you must emit data bottom-to-top, + * you can use one of the virtual arrays provided by the JPEG memory manager + * to invert the data. See wrbmp.c for an example. + * + * As with compression, some operating modes may require temporary files. + * On some systems you may need to set up a signal handler to ensure that + * temporary files are deleted if the program is interrupted. See libjpeg.txt. + */ diff --git a/crypto777/jpeg/jaricom.c b/crypto777/jpeg/jaricom.c new file mode 100644 index 000000000..690068861 --- /dev/null +++ b/crypto777/jpeg/jaricom.c @@ -0,0 +1,153 @@ +/* + * jaricom.c + * + * Developed 1997-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains probability estimation tables for common use in + * arithmetic entropy encoding and decoding routines. + * + * This data represents Table D.3 in the JPEG spec (D.2 in the draft), + * ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81, and Table 24 + * in the JBIG spec, ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + +/* The following #define specifies the packing of the four components + * into the compact INT32 representation. + * Note that this formula must match the actual arithmetic encoder + * and decoder implementation. The implementation has to be changed + * if this formula is changed. + * The current organization is leaned on Markus Kuhn's JBIG + * implementation (jbig_tab.c). + */ + +#define V(i,a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b) + +const INT32 jpeg_aritab[113+1] = { +/* + * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS + */ + V( 0, 0x5a1d, 1, 1, 1 ), + V( 1, 0x2586, 14, 2, 0 ), + V( 2, 0x1114, 16, 3, 0 ), + V( 3, 0x080b, 18, 4, 0 ), + V( 4, 0x03d8, 20, 5, 0 ), + V( 5, 0x01da, 23, 6, 0 ), + V( 6, 0x00e5, 25, 7, 0 ), + V( 7, 0x006f, 28, 8, 0 ), + V( 8, 0x0036, 30, 9, 0 ), + V( 9, 0x001a, 33, 10, 0 ), + V( 10, 0x000d, 35, 11, 0 ), + V( 11, 0x0006, 9, 12, 0 ), + V( 12, 0x0003, 10, 13, 0 ), + V( 13, 0x0001, 12, 13, 0 ), + V( 14, 0x5a7f, 15, 15, 1 ), + V( 15, 0x3f25, 36, 16, 0 ), + V( 16, 0x2cf2, 38, 17, 0 ), + V( 17, 0x207c, 39, 18, 0 ), + V( 18, 0x17b9, 40, 19, 0 ), + V( 19, 0x1182, 42, 20, 0 ), + V( 20, 0x0cef, 43, 21, 0 ), + V( 21, 0x09a1, 45, 22, 0 ), + V( 22, 0x072f, 46, 23, 0 ), + V( 23, 0x055c, 48, 24, 0 ), + V( 24, 0x0406, 49, 25, 0 ), + V( 25, 0x0303, 51, 26, 0 ), + V( 26, 0x0240, 52, 27, 0 ), + V( 27, 0x01b1, 54, 28, 0 ), + V( 28, 0x0144, 56, 29, 0 ), + V( 29, 0x00f5, 57, 30, 0 ), + V( 30, 0x00b7, 59, 31, 0 ), + V( 31, 0x008a, 60, 32, 0 ), + V( 32, 0x0068, 62, 33, 0 ), + V( 33, 0x004e, 63, 34, 0 ), + V( 34, 0x003b, 32, 35, 0 ), + V( 35, 0x002c, 33, 9, 0 ), + V( 36, 0x5ae1, 37, 37, 1 ), + V( 37, 0x484c, 64, 38, 0 ), + V( 38, 0x3a0d, 65, 39, 0 ), + V( 39, 0x2ef1, 67, 40, 0 ), + V( 40, 0x261f, 68, 41, 0 ), + V( 41, 0x1f33, 69, 42, 0 ), + V( 42, 0x19a8, 70, 43, 0 ), + V( 43, 0x1518, 72, 44, 0 ), + V( 44, 0x1177, 73, 45, 0 ), + V( 45, 0x0e74, 74, 46, 0 ), + V( 46, 0x0bfb, 75, 47, 0 ), + V( 47, 0x09f8, 77, 48, 0 ), + V( 48, 0x0861, 78, 49, 0 ), + V( 49, 0x0706, 79, 50, 0 ), + V( 50, 0x05cd, 48, 51, 0 ), + V( 51, 0x04de, 50, 52, 0 ), + V( 52, 0x040f, 50, 53, 0 ), + V( 53, 0x0363, 51, 54, 0 ), + V( 54, 0x02d4, 52, 55, 0 ), + V( 55, 0x025c, 53, 56, 0 ), + V( 56, 0x01f8, 54, 57, 0 ), + V( 57, 0x01a4, 55, 58, 0 ), + V( 58, 0x0160, 56, 59, 0 ), + V( 59, 0x0125, 57, 60, 0 ), + V( 60, 0x00f6, 58, 61, 0 ), + V( 61, 0x00cb, 59, 62, 0 ), + V( 62, 0x00ab, 61, 63, 0 ), + V( 63, 0x008f, 61, 32, 0 ), + V( 64, 0x5b12, 65, 65, 1 ), + V( 65, 0x4d04, 80, 66, 0 ), + V( 66, 0x412c, 81, 67, 0 ), + V( 67, 0x37d8, 82, 68, 0 ), + V( 68, 0x2fe8, 83, 69, 0 ), + V( 69, 0x293c, 84, 70, 0 ), + V( 70, 0x2379, 86, 71, 0 ), + V( 71, 0x1edf, 87, 72, 0 ), + V( 72, 0x1aa9, 87, 73, 0 ), + V( 73, 0x174e, 72, 74, 0 ), + V( 74, 0x1424, 72, 75, 0 ), + V( 75, 0x119c, 74, 76, 0 ), + V( 76, 0x0f6b, 74, 77, 0 ), + V( 77, 0x0d51, 75, 78, 0 ), + V( 78, 0x0bb6, 77, 79, 0 ), + V( 79, 0x0a40, 77, 48, 0 ), + V( 80, 0x5832, 80, 81, 1 ), + V( 81, 0x4d1c, 88, 82, 0 ), + V( 82, 0x438e, 89, 83, 0 ), + V( 83, 0x3bdd, 90, 84, 0 ), + V( 84, 0x34ee, 91, 85, 0 ), + V( 85, 0x2eae, 92, 86, 0 ), + V( 86, 0x299a, 93, 87, 0 ), + V( 87, 0x2516, 86, 71, 0 ), + V( 88, 0x5570, 88, 89, 1 ), + V( 89, 0x4ca9, 95, 90, 0 ), + V( 90, 0x44d9, 96, 91, 0 ), + V( 91, 0x3e22, 97, 92, 0 ), + V( 92, 0x3824, 99, 93, 0 ), + V( 93, 0x32b4, 99, 94, 0 ), + V( 94, 0x2e17, 93, 86, 0 ), + V( 95, 0x56a8, 95, 96, 1 ), + V( 96, 0x4f46, 101, 97, 0 ), + V( 97, 0x47e5, 102, 98, 0 ), + V( 98, 0x41cf, 103, 99, 0 ), + V( 99, 0x3c3d, 104, 100, 0 ), + V( 100, 0x375e, 99, 93, 0 ), + V( 101, 0x5231, 105, 102, 0 ), + V( 102, 0x4c0f, 106, 103, 0 ), + V( 103, 0x4639, 107, 104, 0 ), + V( 104, 0x415e, 103, 99, 0 ), + V( 105, 0x5627, 105, 106, 1 ), + V( 106, 0x50e7, 108, 107, 0 ), + V( 107, 0x4b85, 109, 103, 0 ), + V( 108, 0x5597, 110, 109, 0 ), + V( 109, 0x504f, 111, 107, 0 ), + V( 110, 0x5a10, 110, 111, 1 ), + V( 111, 0x5522, 112, 109, 0 ), + V( 112, 0x59eb, 112, 111, 1 ), +/* + * This last entry is used for fixed probability estimate of 0.5 + * as suggested in Section 10.3 Table 5 of ITU-T Rec. T.851. + */ + V( 113, 0x5a1d, 113, 113, 0 ) +}; diff --git a/crypto777/jpeg/jcapimin.c b/crypto777/jpeg/jcapimin.c new file mode 100644 index 000000000..639ce86f4 --- /dev/null +++ b/crypto777/jpeg/jcapimin.c @@ -0,0 +1,288 @@ +/* + * jcapimin.c + * + * Copyright (C) 1994-1998, Thomas G. Lane. + * Modified 2003-2010 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains application interface code for the compression half + * of the JPEG library. These are the "minimum" API routines that may be + * needed in either the normal full-compression case or the transcoding-only + * case. + * + * Most of the routines intended to be called directly by an application + * are in this file or in jcapistd.c. But also see jcparam.c for + * parameter-setup helper routines, jcomapi.c for routines shared by + * compression and decompression, and jctrans.c for the transcoding case. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * Initialization of a JPEG compression object. + * The error manager must already be set up (in case memory manager fails). + */ + +GLOBAL(void) +jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize) +{ + int i; + + /* Guard against version mismatches between library and caller. */ + cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ + if (version != JPEG_LIB_VERSION) + ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); + if (structsize != SIZEOF(struct jpeg_compress_struct)) + ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, + (int) SIZEOF(struct jpeg_compress_struct), (int) structsize); + + /* For debugging purposes, we zero the whole master structure. + * But the application has already set the err pointer, and may have set + * client_data, so we have to save and restore those fields. + * Note: if application hasn't set client_data, tools like Purify may + * complain here. + */ + { + struct jpeg_error_mgr * err = cinfo->err; + void * client_data = cinfo->client_data; /* ignore Purify complaint here */ + MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct)); + cinfo->err = err; + cinfo->client_data = client_data; + } + cinfo->is_decompressor = FALSE; + + /* Initialize a memory manager instance for this object */ + jinit_memory_mgr((j_common_ptr) cinfo); + + /* Zero out pointers to permanent structures. */ + cinfo->progress = NULL; + cinfo->dest = NULL; + + cinfo->comp_info = NULL; + + for (i = 0; i < NUM_QUANT_TBLS; i++) { + cinfo->quant_tbl_ptrs[i] = NULL; + cinfo->q_scale_factor[i] = 100; + } + + for (i = 0; i < NUM_HUFF_TBLS; i++) { + cinfo->dc_huff_tbl_ptrs[i] = NULL; + cinfo->ac_huff_tbl_ptrs[i] = NULL; + } + + /* Must do it here for emit_dqt in case jpeg_write_tables is used */ + cinfo->block_size = DCTSIZE; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + + cinfo->script_space = NULL; + + cinfo->input_gamma = 1.0; /* in case application forgets */ + + /* OK, I'm ready */ + cinfo->global_state = CSTATE_START; +} + + +/* + * Destruction of a JPEG compression object + */ + +GLOBAL(void) +jpeg_destroy_compress (j_compress_ptr cinfo) +{ + jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ +} + + +/* + * Abort processing of a JPEG compression operation, + * but don't destroy the object itself. + */ + +GLOBAL(void) +jpeg_abort_compress (j_compress_ptr cinfo) +{ + jpeg_abort((j_common_ptr) cinfo); /* use common routine */ +} + + +/* + * Forcibly suppress or un-suppress all quantization and Huffman tables. + * Marks all currently defined tables as already written (if suppress) + * or not written (if !suppress). This will control whether they get emitted + * by a subsequent jpeg_start_compress call. + * + * This routine is exported for use by applications that want to produce + * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but + * since it is called by jpeg_start_compress, we put it here --- otherwise + * jcparam.o would be linked whether the application used it or not. + */ + +GLOBAL(void) +jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress) +{ + int i; + JQUANT_TBL * qtbl; + JHUFF_TBL * htbl; + + for (i = 0; i < NUM_QUANT_TBLS; i++) { + if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL) + qtbl->sent_table = suppress; + } + + for (i = 0; i < NUM_HUFF_TBLS; i++) { + if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL) + htbl->sent_table = suppress; + if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL) + htbl->sent_table = suppress; + } +} + + +/* + * Finish JPEG compression. + * + * If a multipass operating mode was selected, this may do a great deal of + * work including most of the actual output. + */ + +GLOBAL(void) +jpeg_finish_compress (j_compress_ptr cinfo) +{ + JDIMENSION iMCU_row; + + if (cinfo->global_state == CSTATE_SCANNING || + cinfo->global_state == CSTATE_RAW_OK) { + /* Terminate first pass */ + if (cinfo->next_scanline < cinfo->image_height) + ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); + (*cinfo->master->finish_pass) (cinfo); + } else if (cinfo->global_state != CSTATE_WRCOEFS) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + /* Perform any remaining passes */ + while (! cinfo->master->is_last_pass) { + (*cinfo->master->prepare_for_pass) (cinfo); + for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) { + if (cinfo->progress != NULL) { + cinfo->progress->pass_counter = (long) iMCU_row; + cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows; + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + } + /* We bypass the main controller and invoke coef controller directly; + * all work is being done from the coefficient buffer. + */ + if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); + } + (*cinfo->master->finish_pass) (cinfo); + } + /* Write EOI, do final cleanup */ + (*cinfo->marker->write_file_trailer) (cinfo); + (*cinfo->dest->term_destination) (cinfo); + /* We can use jpeg_abort to release memory and reset global_state */ + jpeg_abort((j_common_ptr) cinfo); +} + + +/* + * Write a special marker. + * This is only recommended for writing COM or APPn markers. + * Must be called after jpeg_start_compress() and before + * first call to jpeg_write_scanlines() or jpeg_write_raw_data(). + */ + +GLOBAL(void) +jpeg_write_marker (j_compress_ptr cinfo, int marker, + const JOCTET *dataptr, unsigned int datalen) +{ + JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val)); + + if (cinfo->next_scanline != 0 || + (cinfo->global_state != CSTATE_SCANNING && + cinfo->global_state != CSTATE_RAW_OK && + cinfo->global_state != CSTATE_WRCOEFS)) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); + write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */ + while (datalen--) { + (*write_marker_byte) (cinfo, *dataptr); + dataptr++; + } +} + +/* Same, but piecemeal. */ + +GLOBAL(void) +jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen) +{ + if (cinfo->next_scanline != 0 || + (cinfo->global_state != CSTATE_SCANNING && + cinfo->global_state != CSTATE_RAW_OK && + cinfo->global_state != CSTATE_WRCOEFS)) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); +} + +GLOBAL(void) +jpeg_write_m_byte (j_compress_ptr cinfo, int val) +{ + (*cinfo->marker->write_marker_byte) (cinfo, val); +} + + +/* + * Alternate compression function: just write an abbreviated table file. + * Before calling this, all parameters and a data destination must be set up. + * + * To produce a pair of files containing abbreviated tables and abbreviated + * image data, one would proceed as follows: + * + * initialize JPEG object + * set JPEG parameters + * set destination to table file + * jpeg_write_tables(cinfo); + * set destination to image file + * jpeg_start_compress(cinfo, FALSE); + * write data... + * jpeg_finish_compress(cinfo); + * + * jpeg_write_tables has the side effect of marking all tables written + * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress + * will not re-emit the tables unless it is passed write_all_tables=TRUE. + */ + +GLOBAL(void) +jpeg_write_tables (j_compress_ptr cinfo) +{ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + /* (Re)initialize error mgr and destination modules */ + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); + (*cinfo->dest->init_destination) (cinfo); + /* Initialize the marker writer ... bit of a crock to do it here. */ + jinit_marker_writer(cinfo); + /* Write them tables! */ + (*cinfo->marker->write_tables_only) (cinfo); + /* And clean up. */ + (*cinfo->dest->term_destination) (cinfo); + /* + * In library releases up through v6a, we called jpeg_abort() here to free + * any working memory allocated by the destination manager and marker + * writer. Some applications had a problem with that: they allocated space + * of their own from the library memory manager, and didn't want it to go + * away during write_tables. So now we do nothing. This will cause a + * memory leak if an app calls write_tables repeatedly without doing a full + * compression cycle or otherwise resetting the JPEG object. However, that + * seems less bad than unexpectedly freeing memory in the normal case. + * An app that prefers the old behavior can call jpeg_abort for itself after + * each call to jpeg_write_tables(). + */ +} diff --git a/crypto777/jpeg/jcapistd.c b/crypto777/jpeg/jcapistd.c new file mode 100644 index 000000000..c0320b1b1 --- /dev/null +++ b/crypto777/jpeg/jcapistd.c @@ -0,0 +1,161 @@ +/* + * jcapistd.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains application interface code for the compression half + * of the JPEG library. These are the "standard" API routines that are + * used in the normal full-compression case. They are not used by a + * transcoding-only application. Note that if an application links in + * jpeg_start_compress, it will end up linking in the entire compressor. + * We thus must separate this file from jcapimin.c to avoid linking the + * whole compression library into a transcoder. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * Compression initialization. + * Before calling this, all parameters and a data destination must be set up. + * + * We require a write_all_tables parameter as a failsafe check when writing + * multiple datastreams from the same compression object. Since prior runs + * will have left all the tables marked sent_table=TRUE, a subsequent run + * would emit an abbreviated stream (no tables) by default. This may be what + * is wanted, but for safety's sake it should not be the default behavior: + * programmers should have to make a deliberate choice to emit abbreviated + * images. Therefore the documentation and examples should encourage people + * to pass write_all_tables=TRUE; then it will take active thought to do the + * wrong thing. + */ + +GLOBAL(void) +jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables) +{ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + if (write_all_tables) + jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */ + + /* (Re)initialize error mgr and destination modules */ + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); + (*cinfo->dest->init_destination) (cinfo); + /* Perform master selection of active modules */ + jinit_compress_master(cinfo); + /* Set up for the first pass */ + (*cinfo->master->prepare_for_pass) (cinfo); + /* Ready for application to drive first pass through jpeg_write_scanlines + * or jpeg_write_raw_data. + */ + cinfo->next_scanline = 0; + cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING); +} + + +/* + * Write some scanlines of data to the JPEG compressor. + * + * The return value will be the number of lines actually written. + * This should be less than the supplied num_lines only in case that + * the data destination module has requested suspension of the compressor, + * or if more than image_height scanlines are passed in. + * + * Note: we warn about excess calls to jpeg_write_scanlines() since + * this likely signals an application programmer error. However, + * excess scanlines passed in the last valid call are *silently* ignored, + * so that the application need not adjust num_lines for end-of-image + * when using a multiple-scanline buffer. + */ + +GLOBAL(JDIMENSION) +jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines, + JDIMENSION num_lines) +{ + JDIMENSION row_ctr, rows_left; + + if (cinfo->global_state != CSTATE_SCANNING) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + if (cinfo->next_scanline >= cinfo->image_height) + WARNMS(cinfo, JWRN_TOO_MUCH_DATA); + + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) { + cinfo->progress->pass_counter = (long) cinfo->next_scanline; + cinfo->progress->pass_limit = (long) cinfo->image_height; + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + } + + /* Give master control module another chance if this is first call to + * jpeg_write_scanlines. This lets output of the frame/scan headers be + * delayed so that application can write COM, etc, markers between + * jpeg_start_compress and jpeg_write_scanlines. + */ + if (cinfo->master->call_pass_startup) + (*cinfo->master->pass_startup) (cinfo); + + /* Ignore any extra scanlines at bottom of image. */ + rows_left = cinfo->image_height - cinfo->next_scanline; + if (num_lines > rows_left) + num_lines = rows_left; + + row_ctr = 0; + (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines); + cinfo->next_scanline += row_ctr; + return row_ctr; +} + + +/* + * Alternate entry point to write raw data. + * Processes exactly one iMCU row per call, unless suspended. + */ + +GLOBAL(JDIMENSION) +jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data, + JDIMENSION num_lines) +{ + JDIMENSION lines_per_iMCU_row; + + if (cinfo->global_state != CSTATE_RAW_OK) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + if (cinfo->next_scanline >= cinfo->image_height) { + WARNMS(cinfo, JWRN_TOO_MUCH_DATA); + return 0; + } + + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) { + cinfo->progress->pass_counter = (long) cinfo->next_scanline; + cinfo->progress->pass_limit = (long) cinfo->image_height; + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + } + + /* Give master control module another chance if this is first call to + * jpeg_write_raw_data. This lets output of the frame/scan headers be + * delayed so that application can write COM, etc, markers between + * jpeg_start_compress and jpeg_write_raw_data. + */ + if (cinfo->master->call_pass_startup) + (*cinfo->master->pass_startup) (cinfo); + + /* Verify that at least one iMCU row has been passed. */ + lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE; + if (num_lines < lines_per_iMCU_row) + ERREXIT(cinfo, JERR_BUFFER_SIZE); + + /* Directly compress the row. */ + if (! (*cinfo->coef->compress_data) (cinfo, data)) { + /* If compressor did not consume the whole row, suspend processing. */ + return 0; + } + + /* OK, we processed one iMCU row. */ + cinfo->next_scanline += lines_per_iMCU_row; + return lines_per_iMCU_row; +} diff --git a/crypto777/jpeg/jcarith.c b/crypto777/jpeg/jcarith.c new file mode 100644 index 000000000..033f67069 --- /dev/null +++ b/crypto777/jpeg/jcarith.c @@ -0,0 +1,937 @@ +/* + * jcarith.c + * + * Developed 1997-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains portable arithmetic entropy encoding routines for JPEG + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). + * + * Both sequential and progressive modes are supported in this single module. + * + * Suspension is not currently supported in this module. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Expanded entropy encoder object for arithmetic encoding. */ + +typedef struct { + struct jpeg_entropy_encoder pub; /* public fields */ + + INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */ + INT32 a; /* A register, normalized size of coding interval */ + INT32 sc; /* counter for stacked 0xFF values which might overflow */ + INT32 zc; /* counter for pending 0x00 output values which might * + * be discarded at the end ("Pacman" termination) */ + int ct; /* bit shift counter, determines when next byte will be written */ + int buffer; /* buffer for most recent output byte != 0xFF */ + + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ + int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ + + unsigned int restarts_to_go; /* MCUs left in this restart interval */ + int next_restart_num; /* next restart number to write (0-7) */ + + /* Pointers to statistics areas (these workspaces have image lifespan) */ + unsigned char * dc_stats[NUM_ARITH_TBLS]; + unsigned char * ac_stats[NUM_ARITH_TBLS]; + + /* Statistics bin for coding with fixed probability 0.5 */ + unsigned char fixed_bin[4]; +} arith_entropy_encoder; + +typedef arith_entropy_encoder * arith_entropy_ptr; + +/* The following two definitions specify the allocation chunk size + * for the statistics area. + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least + * 49 statistics bins for DC, and 245 statistics bins for AC coding. + * + * We use a compact representation with 1 byte per statistics bin, + * thus the numbers directly represent byte sizes. + * This 1 byte per statistics bin contains the meaning of the MPS + * (more probable symbol) in the highest bit (mask 0x80), and the + * index into the probability estimation state machine table + * in the lower bits (mask 0x7F). + */ + +#define DC_STAT_BINS 64 +#define AC_STAT_BINS 256 + +/* NOTE: Uncomment the following #define if you want to use the + * given formula for calculating the AC conditioning parameter Kx + * for spectral selection progressive coding in section G.1.3.2 + * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4). + * Although the spec and P&M authors claim that this "has proven + * to give good results for 8 bit precision samples", I'm not + * convinced yet that this is really beneficial. + * Early tests gave only very marginal compression enhancements + * (a few - around 5 or so - bytes even for very large files), + * which would turn out rather negative if we'd suppress the + * DAC (Define Arithmetic Conditioning) marker segments for + * the default parameters in the future. + * Note that currently the marker writing module emits 12-byte + * DAC segments for a full-component scan in a color image. + * This is not worth worrying about IMHO. However, since the + * spec defines the default values to be used if the tables + * are omitted (unlike Huffman tables, which are required + * anyway), one might optimize this behaviour in the future, + * and then it would be disadvantageous to use custom tables if + * they don't provide sufficient gain to exceed the DAC size. + * + * On the other hand, I'd consider it as a reasonable result + * that the conditioning has no significant influence on the + * compression performance. This means that the basic + * statistical model is already rather stable. + * + * Thus, at the moment, we use the default conditioning values + * anyway, and do not use the custom formula. + * +#define CALCULATE_SPECTRAL_CONDITIONING + */ + +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. + * We assume that int right shift is unsigned if INT32 right shift is, + * which should be safe. + */ + +#ifdef RIGHT_SHIFT_IS_UNSIGNED +#define ISHIFT_TEMPS int ishift_temp; +#define IRIGHT_SHIFT(x,shft) \ + ((ishift_temp = (x)) < 0 ? \ + (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ + (ishift_temp >> (shft))) +#else +#define ISHIFT_TEMPS +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) +#endif + + +LOCAL(void) +emit_byte (int val, j_compress_ptr cinfo) +/* Write next output byte; we do not support suspension in this module. */ +{ + struct jpeg_destination_mgr * dest = cinfo->dest; + + *dest->next_output_byte++ = (JOCTET) val; + if (--dest->free_in_buffer == 0) + if (! (*dest->empty_output_buffer) (cinfo)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); +} + + +/* + * Finish up at the end of an arithmetic-compressed scan. + */ + +METHODDEF(void) +finish_pass (j_compress_ptr cinfo) +{ + arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; + INT32 temp; + + /* Section D.1.8: Termination of encoding */ + + /* Find the e->c in the coding interval with the largest + * number of trailing zero bits */ + if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c) + e->c = temp + 0x8000L; + else + e->c = temp; + /* Send remaining bytes to output */ + e->c <<= e->ct; + if (e->c & 0xF8000000L) { + /* One final overflow has to be handled */ + if (e->buffer >= 0) { + if (e->zc) + do emit_byte(0x00, cinfo); + while (--e->zc); + emit_byte(e->buffer + 1, cinfo); + if (e->buffer + 1 == 0xFF) + emit_byte(0x00, cinfo); + } + e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ + e->sc = 0; + } else { + if (e->buffer == 0) + ++e->zc; + else if (e->buffer >= 0) { + if (e->zc) + do emit_byte(0x00, cinfo); + while (--e->zc); + emit_byte(e->buffer, cinfo); + } + if (e->sc) { + if (e->zc) + do emit_byte(0x00, cinfo); + while (--e->zc); + do { + emit_byte(0xFF, cinfo); + emit_byte(0x00, cinfo); + } while (--e->sc); + } + } + /* Output final bytes only if they are not 0x00 */ + if (e->c & 0x7FFF800L) { + if (e->zc) /* output final pending zero bytes */ + do emit_byte(0x00, cinfo); + while (--e->zc); + emit_byte((e->c >> 19) & 0xFF, cinfo); + if (((e->c >> 19) & 0xFF) == 0xFF) + emit_byte(0x00, cinfo); + if (e->c & 0x7F800L) { + emit_byte((e->c >> 11) & 0xFF, cinfo); + if (((e->c >> 11) & 0xFF) == 0xFF) + emit_byte(0x00, cinfo); + } + } +} + + +/* + * The core arithmetic encoding routine (common in JPEG and JBIG). + * This needs to go as fast as possible. + * Machine-dependent optimization facilities + * are not utilized in this portable implementation. + * However, this code should be fairly efficient and + * may be a good base for further optimizations anyway. + * + * Parameter 'val' to be encoded may be 0 or 1 (binary decision). + * + * Note: I've added full "Pacman" termination support to the + * byte output routines, which is equivalent to the optional + * Discard_final_zeros procedure (Figure D.15) in the spec. + * Thus, we always produce the shortest possible output + * stream compliant to the spec (no trailing zero bytes, + * except for FF stuffing). + * + * I've also introduced a new scheme for accessing + * the probability estimation state machine table, + * derived from Markus Kuhn's JBIG implementation. + */ + +LOCAL(void) +arith_encode (j_compress_ptr cinfo, unsigned char *st, int val) +{ + register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; + register unsigned char nl, nm; + register INT32 qe, temp; + register int sv; + + /* Fetch values from our compact representation of Table D.3(D.2): + * Qe values and probability estimation state machine + */ + sv = *st; + qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ + nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ + nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ + + /* Encode & estimation procedures per sections D.1.4 & D.1.5 */ + e->a -= qe; + if (val != (sv >> 7)) { + /* Encode the less probable symbol */ + if (e->a >= qe) { + /* If the interval size (qe) for the less probable symbol (LPS) + * is larger than the interval size for the MPS, then exchange + * the two symbols for coding efficiency, otherwise code the LPS + * as usual: */ + e->c += e->a; + e->a = qe; + } + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ + } else { + /* Encode the more probable symbol */ + if (e->a >= 0x8000L) + return; /* A >= 0x8000 -> ready, no renormalization required */ + if (e->a < qe) { + /* If the interval size (qe) for the less probable symbol (LPS) + * is larger than the interval size for the MPS, then exchange + * the two symbols for coding efficiency: */ + e->c += e->a; + e->a = qe; + } + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ + } + + /* Renormalization & data output per section D.1.6 */ + do { + e->a <<= 1; + e->c <<= 1; + if (--e->ct == 0) { + /* Another byte is ready for output */ + temp = e->c >> 19; + if (temp > 0xFF) { + /* Handle overflow over all stacked 0xFF bytes */ + if (e->buffer >= 0) { + if (e->zc) + do emit_byte(0x00, cinfo); + while (--e->zc); + emit_byte(e->buffer + 1, cinfo); + if (e->buffer + 1 == 0xFF) + emit_byte(0x00, cinfo); + } + e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ + e->sc = 0; + /* Note: The 3 spacer bits in the C register guarantee + * that the new buffer byte can't be 0xFF here + * (see page 160 in the P&M JPEG book). */ + e->buffer = temp & 0xFF; /* new output byte, might overflow later */ + } else if (temp == 0xFF) { + ++e->sc; /* stack 0xFF byte (which might overflow later) */ + } else { + /* Output all stacked 0xFF bytes, they will not overflow any more */ + if (e->buffer == 0) + ++e->zc; + else if (e->buffer >= 0) { + if (e->zc) + do emit_byte(0x00, cinfo); + while (--e->zc); + emit_byte(e->buffer, cinfo); + } + if (e->sc) { + if (e->zc) + do emit_byte(0x00, cinfo); + while (--e->zc); + do { + emit_byte(0xFF, cinfo); + emit_byte(0x00, cinfo); + } while (--e->sc); + } + e->buffer = temp & 0xFF; /* new output byte (can still overflow) */ + } + e->c &= 0x7FFFFL; + e->ct += 8; + } + } while (e->a < 0x8000L); +} + + +/* + * Emit a restart marker & resynchronize predictions. + */ + +LOCAL(void) +emit_restart (j_compress_ptr cinfo, int restart_num) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + int ci; + jpeg_component_info * compptr; + + finish_pass(cinfo); + + emit_byte(0xFF, cinfo); + emit_byte(JPEG_RST0 + restart_num, cinfo); + + /* Re-initialize statistics areas */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* DC needs no table for refinement scan */ + if (cinfo->Ss == 0 && cinfo->Ah == 0) { + MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); + /* Reset DC predictions to 0 */ + entropy->last_dc_val[ci] = 0; + entropy->dc_context[ci] = 0; + } + /* AC needs no table when not present */ + if (cinfo->Se) { + MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); + } + } + + /* Reset arithmetic encoding variables */ + entropy->c = 0; + entropy->a = 0x10000L; + entropy->sc = 0; + entropy->zc = 0; + entropy->ct = 11; + entropy->buffer = -1; /* empty */ +} + + +/* + * MCU encoding for DC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + JBLOCKROW block; + unsigned char *st; + int blkn, ci, tbl; + int v, v2, m; + ISHIFT_TEMPS + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + emit_restart(cinfo, entropy->next_restart_num); + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + /* Encode the MCU data blocks */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + ci = cinfo->MCU_membership[blkn]; + tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; + + /* Compute the DC value after the required point transform by Al. + * This is simply an arithmetic right shift. + */ + m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al); + + /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ + + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + + /* Figure F.4: Encode_DC_DIFF */ + if ((v = m - entropy->last_dc_val[ci]) == 0) { + arith_encode(cinfo, st, 0); + entropy->dc_context[ci] = 0; /* zero diff category */ + } else { + entropy->last_dc_val[ci] = m; + arith_encode(cinfo, st, 1); + /* Figure F.6: Encoding nonzero value v */ + /* Figure F.7: Encoding the sign of v */ + if (v > 0) { + arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ + st += 2; /* Table F.4: SP = S0 + 2 */ + entropy->dc_context[ci] = 4; /* small positive diff category */ + } else { + v = -v; + arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ + st += 3; /* Table F.4: SN = S0 + 3 */ + entropy->dc_context[ci] = 8; /* small negative diff category */ + } + /* Figure F.8: Encoding the magnitude category of v */ + m = 0; + if (v -= 1) { + arith_encode(cinfo, st, 1); + m = 1; + v2 = v; + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ + while (v2 >>= 1) { + arith_encode(cinfo, st, 1); + m <<= 1; + st += 1; + } + } + arith_encode(cinfo, st, 0); + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ + if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) + entropy->dc_context[ci] = 0; /* zero diff category */ + else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) + entropy->dc_context[ci] += 8; /* large diff category */ + /* Figure F.9: Encoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + arith_encode(cinfo, st, (m & v) ? 1 : 0); + } + } + + return TRUE; +} + + +/* + * MCU encoding for AC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + JBLOCKROW block; + unsigned char *st; + int tbl, k, ke; + int v, v2, m; + const int * natural_order; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + emit_restart(cinfo, entropy->next_restart_num); + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + natural_order = cinfo->natural_order; + + /* Encode the MCU data block */ + block = MCU_data[0]; + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + + /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ + + /* Establish EOB (end-of-block) index */ + for (ke = cinfo->Se; ke > 0; ke--) + /* We must apply the point transform by Al. For AC coefficients this + * is an integer division with rounding towards 0. To do this portably + * in C, we shift after obtaining the absolute value. + */ + if ((v = (*block)[natural_order[ke]]) >= 0) { + if (v >>= cinfo->Al) break; + } else { + v = -v; + if (v >>= cinfo->Al) break; + } + + /* Figure F.5: Encode_AC_Coefficients */ + for (k = cinfo->Ss; k <= ke; k++) { + st = entropy->ac_stats[tbl] + 3 * (k - 1); + arith_encode(cinfo, st, 0); /* EOB decision */ + for (;;) { + if ((v = (*block)[natural_order[k]]) >= 0) { + if (v >>= cinfo->Al) { + arith_encode(cinfo, st + 1, 1); + arith_encode(cinfo, entropy->fixed_bin, 0); + break; + } + } else { + v = -v; + if (v >>= cinfo->Al) { + arith_encode(cinfo, st + 1, 1); + arith_encode(cinfo, entropy->fixed_bin, 1); + break; + } + } + arith_encode(cinfo, st + 1, 0); st += 3; k++; + } + st += 2; + /* Figure F.8: Encoding the magnitude category of v */ + m = 0; + if (v -= 1) { + arith_encode(cinfo, st, 1); + m = 1; + v2 = v; + if (v2 >>= 1) { + arith_encode(cinfo, st, 1); + m <<= 1; + st = entropy->ac_stats[tbl] + + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); + while (v2 >>= 1) { + arith_encode(cinfo, st, 1); + m <<= 1; + st += 1; + } + } + } + arith_encode(cinfo, st, 0); + /* Figure F.9: Encoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + arith_encode(cinfo, st, (m & v) ? 1 : 0); + } + /* Encode EOB decision only if k <= cinfo->Se */ + if (k <= cinfo->Se) { + st = entropy->ac_stats[tbl] + 3 * (k - 1); + arith_encode(cinfo, st, 1); + } + + return TRUE; +} + + +/* + * MCU encoding for DC successive approximation refinement scan. + */ + +METHODDEF(boolean) +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + unsigned char *st; + int Al, blkn; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + emit_restart(cinfo, entropy->next_restart_num); + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + st = entropy->fixed_bin; /* use fixed probability estimation */ + Al = cinfo->Al; + + /* Encode the MCU data blocks */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + /* We simply emit the Al'th bit of the DC coefficient value. */ + arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1); + } + + return TRUE; +} + + +/* + * MCU encoding for AC successive approximation refinement scan. + */ + +METHODDEF(boolean) +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + JBLOCKROW block; + unsigned char *st; + int tbl, k, ke, kex; + int v; + const int * natural_order; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + emit_restart(cinfo, entropy->next_restart_num); + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + natural_order = cinfo->natural_order; + + /* Encode the MCU data block */ + block = MCU_data[0]; + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + + /* Section G.1.3.3: Encoding of AC coefficients */ + + /* Establish EOB (end-of-block) index */ + for (ke = cinfo->Se; ke > 0; ke--) + /* We must apply the point transform by Al. For AC coefficients this + * is an integer division with rounding towards 0. To do this portably + * in C, we shift after obtaining the absolute value. + */ + if ((v = (*block)[natural_order[ke]]) >= 0) { + if (v >>= cinfo->Al) break; + } else { + v = -v; + if (v >>= cinfo->Al) break; + } + + /* Establish EOBx (previous stage end-of-block) index */ + for (kex = ke; kex > 0; kex--) + if ((v = (*block)[natural_order[kex]]) >= 0) { + if (v >>= cinfo->Ah) break; + } else { + v = -v; + if (v >>= cinfo->Ah) break; + } + + /* Figure G.10: Encode_AC_Coefficients_SA */ + for (k = cinfo->Ss; k <= ke; k++) { + st = entropy->ac_stats[tbl] + 3 * (k - 1); + if (k > kex) + arith_encode(cinfo, st, 0); /* EOB decision */ + for (;;) { + if ((v = (*block)[natural_order[k]]) >= 0) { + if (v >>= cinfo->Al) { + if (v >> 1) /* previously nonzero coef */ + arith_encode(cinfo, st + 2, (v & 1)); + else { /* newly nonzero coef */ + arith_encode(cinfo, st + 1, 1); + arith_encode(cinfo, entropy->fixed_bin, 0); + } + break; + } + } else { + v = -v; + if (v >>= cinfo->Al) { + if (v >> 1) /* previously nonzero coef */ + arith_encode(cinfo, st + 2, (v & 1)); + else { /* newly nonzero coef */ + arith_encode(cinfo, st + 1, 1); + arith_encode(cinfo, entropy->fixed_bin, 1); + } + break; + } + } + arith_encode(cinfo, st + 1, 0); st += 3; k++; + } + } + /* Encode EOB decision only if k <= cinfo->Se */ + if (k <= cinfo->Se) { + st = entropy->ac_stats[tbl] + 3 * (k - 1); + arith_encode(cinfo, st, 1); + } + + return TRUE; +} + + +/* + * Encode and output one MCU's worth of arithmetic-compressed coefficients. + */ + +METHODDEF(boolean) +encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + jpeg_component_info * compptr; + JBLOCKROW block; + unsigned char *st; + int blkn, ci, tbl, k, ke; + int v, v2, m; + const int * natural_order; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + emit_restart(cinfo, entropy->next_restart_num); + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + natural_order = cinfo->natural_order; + + /* Encode the MCU data blocks */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + + /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ + + tbl = compptr->dc_tbl_no; + + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + + /* Figure F.4: Encode_DC_DIFF */ + if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) { + arith_encode(cinfo, st, 0); + entropy->dc_context[ci] = 0; /* zero diff category */ + } else { + entropy->last_dc_val[ci] = (*block)[0]; + arith_encode(cinfo, st, 1); + /* Figure F.6: Encoding nonzero value v */ + /* Figure F.7: Encoding the sign of v */ + if (v > 0) { + arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ + st += 2; /* Table F.4: SP = S0 + 2 */ + entropy->dc_context[ci] = 4; /* small positive diff category */ + } else { + v = -v; + arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ + st += 3; /* Table F.4: SN = S0 + 3 */ + entropy->dc_context[ci] = 8; /* small negative diff category */ + } + /* Figure F.8: Encoding the magnitude category of v */ + m = 0; + if (v -= 1) { + arith_encode(cinfo, st, 1); + m = 1; + v2 = v; + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ + while (v2 >>= 1) { + arith_encode(cinfo, st, 1); + m <<= 1; + st += 1; + } + } + arith_encode(cinfo, st, 0); + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ + if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) + entropy->dc_context[ci] = 0; /* zero diff category */ + else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) + entropy->dc_context[ci] += 8; /* large diff category */ + /* Figure F.9: Encoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + arith_encode(cinfo, st, (m & v) ? 1 : 0); + } + + /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ + + if ((ke = cinfo->lim_Se) == 0) continue; + tbl = compptr->ac_tbl_no; + + /* Establish EOB (end-of-block) index */ + do { + if ((*block)[natural_order[ke]]) break; + } while (--ke); + + /* Figure F.5: Encode_AC_Coefficients */ + for (k = 0; k < ke;) { + st = entropy->ac_stats[tbl] + 3 * k; + arith_encode(cinfo, st, 0); /* EOB decision */ + while ((v = (*block)[natural_order[++k]]) == 0) { + arith_encode(cinfo, st + 1, 0); + st += 3; + } + arith_encode(cinfo, st + 1, 1); + /* Figure F.6: Encoding nonzero value v */ + /* Figure F.7: Encoding the sign of v */ + if (v > 0) { + arith_encode(cinfo, entropy->fixed_bin, 0); + } else { + v = -v; + arith_encode(cinfo, entropy->fixed_bin, 1); + } + st += 2; + /* Figure F.8: Encoding the magnitude category of v */ + m = 0; + if (v -= 1) { + arith_encode(cinfo, st, 1); + m = 1; + v2 = v; + if (v2 >>= 1) { + arith_encode(cinfo, st, 1); + m <<= 1; + st = entropy->ac_stats[tbl] + + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); + while (v2 >>= 1) { + arith_encode(cinfo, st, 1); + m <<= 1; + st += 1; + } + } + } + arith_encode(cinfo, st, 0); + /* Figure F.9: Encoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + arith_encode(cinfo, st, (m & v) ? 1 : 0); + } + /* Encode EOB decision only if k < cinfo->lim_Se */ + if (k < cinfo->lim_Se) { + st = entropy->ac_stats[tbl] + 3 * k; + arith_encode(cinfo, st, 1); + } + } + + return TRUE; +} + + +/* + * Initialize for an arithmetic-compressed scan. + */ + +METHODDEF(void) +start_pass (j_compress_ptr cinfo, boolean gather_statistics) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + int ci, tbl; + jpeg_component_info * compptr; + + if (gather_statistics) + /* Make sure to avoid that in the master control logic! + * We are fully adaptive here and need no extra + * statistics gathering pass! + */ + ERREXIT(cinfo, JERR_NOT_COMPILED); + + /* We assume jcmaster.c already validated the progressive scan parameters. */ + + /* Select execution routines */ + if (cinfo->progressive_mode) { + if (cinfo->Ah == 0) { + if (cinfo->Ss == 0) + entropy->pub.encode_mcu = encode_mcu_DC_first; + else + entropy->pub.encode_mcu = encode_mcu_AC_first; + } else { + if (cinfo->Ss == 0) + entropy->pub.encode_mcu = encode_mcu_DC_refine; + else + entropy->pub.encode_mcu = encode_mcu_AC_refine; + } + } else + entropy->pub.encode_mcu = encode_mcu; + + /* Allocate & initialize requested statistics areas */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* DC needs no table for refinement scan */ + if (cinfo->Ss == 0 && cinfo->Ah == 0) { + tbl = compptr->dc_tbl_no; + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); + if (entropy->dc_stats[tbl] == NULL) + entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); + MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); + /* Initialize DC predictions to 0 */ + entropy->last_dc_val[ci] = 0; + entropy->dc_context[ci] = 0; + } + /* AC needs no table when not present */ + if (cinfo->Se) { + tbl = compptr->ac_tbl_no; + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); + if (entropy->ac_stats[tbl] == NULL) + entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); + MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); +#ifdef CALCULATE_SPECTRAL_CONDITIONING + if (cinfo->progressive_mode) + /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */ + cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4); +#endif + } + } + + /* Initialize arithmetic encoding variables */ + entropy->c = 0; + entropy->a = 0x10000L; + entropy->sc = 0; + entropy->zc = 0; + entropy->ct = 11; + entropy->buffer = -1; /* empty */ + + /* Initialize restart stuff */ + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num = 0; +} + + +/* + * Module initialization routine for arithmetic entropy encoding. + */ + +GLOBAL(void) +jinit_arith_encoder (j_compress_ptr cinfo) +{ + arith_entropy_ptr entropy; + int i; + + entropy = (arith_entropy_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(arith_entropy_encoder)); + cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; + entropy->pub.start_pass = start_pass; + entropy->pub.finish_pass = finish_pass; + + /* Mark tables unallocated */ + for (i = 0; i < NUM_ARITH_TBLS; i++) { + entropy->dc_stats[i] = NULL; + entropy->ac_stats[i] = NULL; + } + + /* Initialize index for fixed probability estimation */ + entropy->fixed_bin[0] = 113; +} diff --git a/crypto777/jpeg/jccoefct.c b/crypto777/jpeg/jccoefct.c new file mode 100644 index 000000000..924a703dd --- /dev/null +++ b/crypto777/jpeg/jccoefct.c @@ -0,0 +1,454 @@ +/* + * jccoefct.c + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * Modified 2003-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the coefficient buffer controller for compression. + * This controller is the top level of the JPEG compressor proper. + * The coefficient buffer lies between forward-DCT and entropy encoding steps. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* We use a full-image coefficient buffer when doing Huffman optimization, + * and also for writing multiple-scan JPEG files. In all cases, the DCT + * step is run during the first pass, and subsequent passes need only read + * the buffered coefficients. + */ +#ifdef ENTROPY_OPT_SUPPORTED +#define FULL_COEF_BUFFER_SUPPORTED +#else +#ifdef C_MULTISCAN_FILES_SUPPORTED +#define FULL_COEF_BUFFER_SUPPORTED +#endif +#endif + + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_c_coef_controller pub; /* public fields */ + + JDIMENSION iMCU_row_num; /* iMCU row # within image */ + JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ + int MCU_vert_offset; /* counts MCU rows within iMCU row */ + int MCU_rows_per_iMCU_row; /* number of such rows needed */ + + /* For single-pass compression, it's sufficient to buffer just one MCU + * (although this may prove a bit slow in practice). We allocate a + * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each + * MCU constructed and sent. (On 80x86, the workspace is FAR even though + * it's not really very big; this is to keep the module interfaces unchanged + * when a large coefficient buffer is necessary.) + * In multi-pass modes, this array points to the current MCU's blocks + * within the virtual arrays. + */ + JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; + + /* In multi-pass modes, we need a virtual block array for each component. */ + jvirt_barray_ptr whole_image[MAX_COMPONENTS]; +} my_coef_controller; + +typedef my_coef_controller * my_coef_ptr; + + +/* Forward declarations */ +METHODDEF(boolean) compress_data + JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf)); +#ifdef FULL_COEF_BUFFER_SUPPORTED +METHODDEF(boolean) compress_first_pass + JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf)); +METHODDEF(boolean) compress_output + JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf)); +#endif + + +LOCAL(void) +start_iMCU_row (j_compress_ptr cinfo) +/* Reset within-iMCU-row counters for a new row */ +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + + /* In an interleaved scan, an MCU row is the same as an iMCU row. + * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. + * But at the bottom of the image, process only what's left. + */ + if (cinfo->comps_in_scan > 1) { + coef->MCU_rows_per_iMCU_row = 1; + } else { + if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1)) + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; + else + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; + } + + coef->mcu_ctr = 0; + coef->MCU_vert_offset = 0; +} + + +/* + * Initialize for a processing pass. + */ + +METHODDEF(void) +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + + coef->iMCU_row_num = 0; + start_iMCU_row(cinfo); + + switch (pass_mode) { + case JBUF_PASS_THRU: + if (coef->whole_image[0] != NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + coef->pub.compress_data = compress_data; + break; +#ifdef FULL_COEF_BUFFER_SUPPORTED + case JBUF_SAVE_AND_PASS: + if (coef->whole_image[0] == NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + coef->pub.compress_data = compress_first_pass; + break; + case JBUF_CRANK_DEST: + if (coef->whole_image[0] == NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + coef->pub.compress_data = compress_output; + break; +#endif + default: + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + break; + } +} + + +/* + * Process some data in the single-pass case. + * We process the equivalent of one fully interleaved MCU row ("iMCU" row) + * per call, ie, v_samp_factor block rows for each component in the image. + * Returns TRUE if the iMCU row is completed, FALSE if suspended. + * + * NB: input_buf contains a plane for each component in image, + * which we index according to the component's SOF position. + */ + +METHODDEF(boolean) +compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION MCU_col_num; /* index of current MCU within row */ + JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; + int blkn, bi, ci, yindex, yoffset, blockcnt; + JDIMENSION ypos, xpos; + jpeg_component_info *compptr; + forward_DCT_ptr forward_DCT; + + /* Loop to write as much as one whole iMCU row */ + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; + yoffset++) { + for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col; + MCU_col_num++) { + /* Determine where data comes from in input_buf and do the DCT thing. + * Each call on forward_DCT processes a horizontal row of DCT blocks + * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks + * sequentially. Dummy blocks at the right or bottom edge are filled in + * specially. The data in them does not matter for image reconstruction, + * so we fill them with values that will encode to the smallest amount of + * data, viz: all zeroes in the AC entries, DC entries equal to previous + * block's DC value. (Thanks to Thomas Kinsman for this idea.) + */ + blkn = 0; + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index]; + blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width + : compptr->last_col_width; + xpos = MCU_col_num * compptr->MCU_sample_width; + ypos = yoffset * compptr->DCT_v_scaled_size; + /* ypos == (yoffset+yindex) * DCTSIZE */ + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { + if (coef->iMCU_row_num < last_iMCU_row || + yoffset+yindex < compptr->last_row_height) { + (*forward_DCT) (cinfo, compptr, + input_buf[compptr->component_index], + coef->MCU_buffer[blkn], + ypos, xpos, (JDIMENSION) blockcnt); + if (blockcnt < compptr->MCU_width) { + /* Create some dummy blocks at the right edge of the image. */ + FMEMZERO((void FAR *) coef->MCU_buffer[blkn + blockcnt], + (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK)); + for (bi = blockcnt; bi < compptr->MCU_width; bi++) { + coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0]; + } + } + } else { + /* Create a row of dummy blocks at the bottom of the image. */ + FMEMZERO((void FAR *) coef->MCU_buffer[blkn], + compptr->MCU_width * SIZEOF(JBLOCK)); + for (bi = 0; bi < compptr->MCU_width; bi++) { + coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0]; + } + } + blkn += compptr->MCU_width; + ypos += compptr->DCT_v_scaled_size; + } + } + /* Try to write the MCU. In event of a suspension failure, we will + * re-DCT the MCU on restart (a bit inefficient, could be fixed...) + */ + if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) { + /* Suspension forced; update state counters and exit */ + coef->MCU_vert_offset = yoffset; + coef->mcu_ctr = MCU_col_num; + return FALSE; + } + } + /* Completed an MCU row, but perhaps not an iMCU row */ + coef->mcu_ctr = 0; + } + /* Completed the iMCU row, advance counters for next one */ + coef->iMCU_row_num++; + start_iMCU_row(cinfo); + return TRUE; +} + + +#ifdef FULL_COEF_BUFFER_SUPPORTED + +/* + * Process some data in the first pass of a multi-pass case. + * We process the equivalent of one fully interleaved MCU row ("iMCU" row) + * per call, ie, v_samp_factor block rows for each component in the image. + * This amount of data is read from the source buffer, DCT'd and quantized, + * and saved into the virtual arrays. We also generate suitable dummy blocks + * as needed at the right and lower edges. (The dummy blocks are constructed + * in the virtual arrays, which have been padded appropriately.) This makes + * it possible for subsequent passes not to worry about real vs. dummy blocks. + * + * We must also emit the data to the entropy encoder. This is conveniently + * done by calling compress_output() after we've loaded the current strip + * of the virtual arrays. + * + * NB: input_buf contains a plane for each component in image. All + * components are DCT'd and loaded into the virtual arrays in this pass. + * However, it may be that only a subset of the components are emitted to + * the entropy encoder during this first pass; be careful about looking + * at the scan-dependent variables (MCU dimensions, etc). + */ + +METHODDEF(boolean) +compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; + JDIMENSION blocks_across, MCUs_across, MCUindex; + int bi, ci, h_samp_factor, block_row, block_rows, ndummy; + JCOEF lastDC; + jpeg_component_info *compptr; + JBLOCKARRAY buffer; + JBLOCKROW thisblockrow, lastblockrow; + forward_DCT_ptr forward_DCT; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Align the virtual buffer for this component. */ + buffer = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[ci], + coef->iMCU_row_num * compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, TRUE); + /* Count non-dummy DCT block rows in this iMCU row. */ + if (coef->iMCU_row_num < last_iMCU_row) + block_rows = compptr->v_samp_factor; + else { + /* NB: can't use last_row_height here, since may not be set! */ + block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); + if (block_rows == 0) block_rows = compptr->v_samp_factor; + } + blocks_across = compptr->width_in_blocks; + h_samp_factor = compptr->h_samp_factor; + /* Count number of dummy blocks to be added at the right margin. */ + ndummy = (int) (blocks_across % h_samp_factor); + if (ndummy > 0) + ndummy = h_samp_factor - ndummy; + forward_DCT = cinfo->fdct->forward_DCT[ci]; + /* Perform DCT for all non-dummy blocks in this iMCU row. Each call + * on forward_DCT processes a complete horizontal row of DCT blocks. + */ + for (block_row = 0; block_row < block_rows; block_row++) { + thisblockrow = buffer[block_row]; + (*forward_DCT) (cinfo, compptr, input_buf[ci], thisblockrow, + (JDIMENSION) (block_row * compptr->DCT_v_scaled_size), + (JDIMENSION) 0, blocks_across); + if (ndummy > 0) { + /* Create dummy blocks at the right edge of the image. */ + thisblockrow += blocks_across; /* => first dummy block */ + FMEMZERO((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK)); + lastDC = thisblockrow[-1][0]; + for (bi = 0; bi < ndummy; bi++) { + thisblockrow[bi][0] = lastDC; + } + } + } + /* If at end of image, create dummy block rows as needed. + * The tricky part here is that within each MCU, we want the DC values + * of the dummy blocks to match the last real block's DC value. + * This squeezes a few more bytes out of the resulting file... + */ + if (coef->iMCU_row_num == last_iMCU_row) { + blocks_across += ndummy; /* include lower right corner */ + MCUs_across = blocks_across / h_samp_factor; + for (block_row = block_rows; block_row < compptr->v_samp_factor; + block_row++) { + thisblockrow = buffer[block_row]; + lastblockrow = buffer[block_row-1]; + FMEMZERO((void FAR *) thisblockrow, + (size_t) (blocks_across * SIZEOF(JBLOCK))); + for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) { + lastDC = lastblockrow[h_samp_factor-1][0]; + for (bi = 0; bi < h_samp_factor; bi++) { + thisblockrow[bi][0] = lastDC; + } + thisblockrow += h_samp_factor; /* advance to next MCU in row */ + lastblockrow += h_samp_factor; + } + } + } + } + /* NB: compress_output will increment iMCU_row_num if successful. + * A suspension return will result in redoing all the work above next time. + */ + + /* Emit data to the entropy encoder, sharing code with subsequent passes */ + return compress_output(cinfo, input_buf); +} + + +/* + * Process some data in subsequent passes of a multi-pass case. + * We process the equivalent of one fully interleaved MCU row ("iMCU" row) + * per call, ie, v_samp_factor block rows for each component in the scan. + * The data is obtained from the virtual arrays and fed to the entropy coder. + * Returns TRUE if the iMCU row is completed, FALSE if suspended. + * + * NB: input_buf is ignored; it is likely to be a NULL pointer. + */ + +METHODDEF(boolean) +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION MCU_col_num; /* index of current MCU within row */ + int blkn, ci, xindex, yindex, yoffset; + JDIMENSION start_col; + JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; + JBLOCKROW buffer_ptr; + jpeg_component_info *compptr; + + /* Align the virtual buffers for the components used in this scan. + * NB: during first pass, this is safe only because the buffers will + * already be aligned properly, so jmemmgr.c won't need to do any I/O. + */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + buffer[ci] = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], + coef->iMCU_row_num * compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, FALSE); + } + + /* Loop to process one whole iMCU row */ + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; + yoffset++) { + for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; + MCU_col_num++) { + /* Construct list of pointers to DCT blocks belonging to this MCU */ + blkn = 0; /* index of current DCT block within MCU */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + start_col = MCU_col_num * compptr->MCU_width; + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { + buffer_ptr = buffer[ci][yindex+yoffset] + start_col; + for (xindex = 0; xindex < compptr->MCU_width; xindex++) { + coef->MCU_buffer[blkn++] = buffer_ptr++; + } + } + } + /* Try to write the MCU. */ + if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) { + /* Suspension forced; update state counters and exit */ + coef->MCU_vert_offset = yoffset; + coef->mcu_ctr = MCU_col_num; + return FALSE; + } + } + /* Completed an MCU row, but perhaps not an iMCU row */ + coef->mcu_ctr = 0; + } + /* Completed the iMCU row, advance counters for next one */ + coef->iMCU_row_num++; + start_iMCU_row(cinfo); + return TRUE; +} + +#endif /* FULL_COEF_BUFFER_SUPPORTED */ + + +/* + * Initialize coefficient buffer controller. + */ + +GLOBAL(void) +jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer) +{ + my_coef_ptr coef; + + coef = (my_coef_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_coef_controller)); + cinfo->coef = (struct jpeg_c_coef_controller *) coef; + coef->pub.start_pass = start_pass_coef; + + /* Create the coefficient buffer. */ + if (need_full_buffer) { +#ifdef FULL_COEF_BUFFER_SUPPORTED + /* Allocate a full-image virtual array for each component, */ + /* padded to a multiple of samp_factor DCT blocks in each direction. */ + int ci; + jpeg_component_info *compptr; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + (JDIMENSION) jround_up((long) compptr->width_in_blocks, + (long) compptr->h_samp_factor), + (JDIMENSION) jround_up((long) compptr->height_in_blocks, + (long) compptr->v_samp_factor), + (JDIMENSION) compptr->v_samp_factor); + } +#else + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); +#endif + } else { + /* We only need a single-MCU buffer. */ + JBLOCKROW buffer; + int i; + + buffer = (JBLOCKROW) + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, + C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); + for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { + coef->MCU_buffer[i] = buffer + i; + } + coef->whole_image[0] = NULL; /* flag for no virtual arrays */ + } +} diff --git a/crypto777/jpeg/jccolor.c b/crypto777/jpeg/jccolor.c new file mode 100644 index 000000000..3e2d0e927 --- /dev/null +++ b/crypto777/jpeg/jccolor.c @@ -0,0 +1,490 @@ +/* + * jccolor.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modified 2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains input colorspace conversion routines. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Private subobject */ + +typedef struct { + struct jpeg_color_converter pub; /* public fields */ + + /* Private state for RGB->YCC conversion */ + INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ +} my_color_converter; + +typedef my_color_converter * my_cconvert_ptr; + + +/**************** RGB -> YCbCr conversion: most common case **************/ + +/* + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. + * The conversion equations to be implemented are therefore + * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B + * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE + * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) + * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, + * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and + * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) + * were not represented exactly. Now we sacrifice exact representation of + * maximum red and maximum blue in order to get exact grayscales. + * + * To avoid floating-point arithmetic, we represent the fractional constants + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide + * the products by 2^16, with appropriate rounding, to get the correct answer. + * + * For even more speed, we avoid doing any multiplications in the inner loop + * by precalculating the constants times R,G,B for all possible values. + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); + * for 12-bit samples it is still acceptable. It's not very reasonable for + * 16-bit samples, but if you want lossless storage you shouldn't be changing + * colorspace anyway. + * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included + * in the tables to save adding them separately in the inner loop. + */ + +#define SCALEBITS 16 /* speediest right-shift on some machines */ +#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) +#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) +#define FIX(x) ((INT32) ((x) * (1L< Y section */ +#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ +#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ +#define R_CB_OFF (3*(MAXJSAMPLE+1)) +#define G_CB_OFF (4*(MAXJSAMPLE+1)) +#define B_CB_OFF (5*(MAXJSAMPLE+1)) +#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ +#define G_CR_OFF (6*(MAXJSAMPLE+1)) +#define B_CR_OFF (7*(MAXJSAMPLE+1)) +#define TABLE_SIZE (8*(MAXJSAMPLE+1)) + + +/* + * Initialize for RGB->YCC colorspace conversion. + */ + +METHODDEF(void) +rgb_ycc_start (j_compress_ptr cinfo) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + INT32 * rgb_ycc_tab; + INT32 i; + + /* Allocate and fill in the conversion tables. */ + cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (TABLE_SIZE * SIZEOF(INT32))); + + for (i = 0; i <= MAXJSAMPLE; i++) { + rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; + rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; + rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; + rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; + rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; + /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. + * This ensures that the maximum output will round to MAXJSAMPLE + * not MAXJSAMPLE+1, and thus that we don't have to range-limit. + */ + rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; +/* B=>Cb and R=>Cr tables are the same + rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; +*/ + rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; + rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; + } +} + + +/* + * Convert some rows of samples to the JPEG colorspace. + * + * Note that we change from the application's interleaved-pixel format + * to our internal noninterleaved, one-plane-per-component format. + * The input buffer is therefore three times as wide as the output buffer. + * + * A starting row offset is provided only for the output buffer. The caller + * can easily adjust the passed input_buf value to accommodate any row + * offset required on that side. + */ + +METHODDEF(void) +rgb_ycc_convert (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + register int r, g, b; + register INT32 * ctab = cconvert->rgb_ycc_tab; + register JSAMPROW inptr; + register JSAMPROW outptr0, outptr1, outptr2; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->image_width; + + while (--num_rows >= 0) { + inptr = *input_buf++; + outptr0 = output_buf[0][output_row]; + outptr1 = output_buf[1][output_row]; + outptr2 = output_buf[2][output_row]; + output_row++; + for (col = 0; col < num_cols; col++) { + r = GETJSAMPLE(inptr[RGB_RED]); + g = GETJSAMPLE(inptr[RGB_GREEN]); + b = GETJSAMPLE(inptr[RGB_BLUE]); + inptr += RGB_PIXELSIZE; + /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations + * must be too; we do not need an explicit range-limiting operation. + * Hence the value being shifted is never negative, and we don't + * need the general RIGHT_SHIFT macro. + */ + /* Y */ + outptr0[col] = (JSAMPLE) + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) + >> SCALEBITS); + /* Cb */ + outptr1[col] = (JSAMPLE) + ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) + >> SCALEBITS); + /* Cr */ + outptr2[col] = (JSAMPLE) + ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) + >> SCALEBITS); + } + } +} + + +/**************** Cases other than RGB -> YCbCr **************/ + + +/* + * Convert some rows of samples to the JPEG colorspace. + * This version handles RGB->grayscale conversion, which is the same + * as the RGB->Y portion of RGB->YCbCr. + * We assume rgb_ycc_start has been called (we only use the Y tables). + */ + +METHODDEF(void) +rgb_gray_convert (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + register int r, g, b; + register INT32 * ctab = cconvert->rgb_ycc_tab; + register JSAMPROW inptr; + register JSAMPROW outptr; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->image_width; + + while (--num_rows >= 0) { + inptr = *input_buf++; + outptr = output_buf[0][output_row]; + output_row++; + for (col = 0; col < num_cols; col++) { + r = GETJSAMPLE(inptr[RGB_RED]); + g = GETJSAMPLE(inptr[RGB_GREEN]); + b = GETJSAMPLE(inptr[RGB_BLUE]); + inptr += RGB_PIXELSIZE; + /* Y */ + outptr[col] = (JSAMPLE) + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) + >> SCALEBITS); + } + } +} + + +/* + * Convert some rows of samples to the JPEG colorspace. + * This version handles Adobe-style CMYK->YCCK conversion, + * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same + * conversion as above, while passing K (black) unchanged. + * We assume rgb_ycc_start has been called. + */ + +METHODDEF(void) +cmyk_ycck_convert (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + register int r, g, b; + register INT32 * ctab = cconvert->rgb_ycc_tab; + register JSAMPROW inptr; + register JSAMPROW outptr0, outptr1, outptr2, outptr3; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->image_width; + + while (--num_rows >= 0) { + inptr = *input_buf++; + outptr0 = output_buf[0][output_row]; + outptr1 = output_buf[1][output_row]; + outptr2 = output_buf[2][output_row]; + outptr3 = output_buf[3][output_row]; + output_row++; + for (col = 0; col < num_cols; col++) { + r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); + g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); + b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); + /* K passes through as-is */ + outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ + inptr += 4; + /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations + * must be too; we do not need an explicit range-limiting operation. + * Hence the value being shifted is never negative, and we don't + * need the general RIGHT_SHIFT macro. + */ + /* Y */ + outptr0[col] = (JSAMPLE) + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) + >> SCALEBITS); + /* Cb */ + outptr1[col] = (JSAMPLE) + ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) + >> SCALEBITS); + /* Cr */ + outptr2[col] = (JSAMPLE) + ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) + >> SCALEBITS); + } + } +} + + +/* + * Convert some rows of samples to the JPEG colorspace. + * This version handles grayscale output with no conversion. + * The source can be either plain grayscale or YCbCr (since Y == gray). + */ + +METHODDEF(void) +grayscale_convert (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows) +{ + register JSAMPROW inptr; + register JSAMPROW outptr; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->image_width; + int instride = cinfo->input_components; + + while (--num_rows >= 0) { + inptr = *input_buf++; + outptr = output_buf[0][output_row]; + output_row++; + for (col = 0; col < num_cols; col++) { + outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ + inptr += instride; + } + } +} + + +/* + * Convert some rows of samples to the JPEG colorspace. + * No colorspace conversion, but change from interleaved + * to separate-planes representation. + */ + +METHODDEF(void) +rgb_convert (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows) +{ + register JSAMPROW inptr; + register JSAMPROW outptr0, outptr1, outptr2; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->image_width; + + while (--num_rows >= 0) { + inptr = *input_buf++; + outptr0 = output_buf[0][output_row]; + outptr1 = output_buf[1][output_row]; + outptr2 = output_buf[2][output_row]; + output_row++; + for (col = 0; col < num_cols; col++) { + /* We can dispense with GETJSAMPLE() here */ + outptr0[col] = inptr[RGB_RED]; + outptr1[col] = inptr[RGB_GREEN]; + outptr2[col] = inptr[RGB_BLUE]; + inptr += RGB_PIXELSIZE; + } + } +} + + +/* + * Convert some rows of samples to the JPEG colorspace. + * This version handles multi-component colorspaces without conversion. + * We assume input_components == num_components. + */ + +METHODDEF(void) +null_convert (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows) +{ + register JSAMPROW inptr; + register JSAMPROW outptr; + register JDIMENSION col; + register int ci; + int nc = cinfo->num_components; + JDIMENSION num_cols = cinfo->image_width; + + while (--num_rows >= 0) { + /* It seems fastest to make a separate pass for each component. */ + for (ci = 0; ci < nc; ci++) { + inptr = *input_buf; + outptr = output_buf[ci][output_row]; + for (col = 0; col < num_cols; col++) { + outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ + inptr += nc; + } + } + input_buf++; + output_row++; + } +} + + +/* + * Empty method for start_pass. + */ + +METHODDEF(void) +null_method (j_compress_ptr cinfo) +{ + /* no work needed */ +} + + +/* + * Module initialization routine for input colorspace conversion. + */ + +GLOBAL(void) +jinit_color_converter (j_compress_ptr cinfo) +{ + my_cconvert_ptr cconvert; + + cconvert = (my_cconvert_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_color_converter)); + cinfo->cconvert = (struct jpeg_color_converter *) cconvert; + /* set start_pass to null method until we find out differently */ + cconvert->pub.start_pass = null_method; + + /* Make sure input_components agrees with in_color_space */ + switch (cinfo->in_color_space) { + case JCS_GRAYSCALE: + if (cinfo->input_components != 1) + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); + break; + + case JCS_RGB: + if (cinfo->input_components != RGB_PIXELSIZE) + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); + break; + + case JCS_YCbCr: + if (cinfo->input_components != 3) + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); + break; + + case JCS_CMYK: + case JCS_YCCK: + if (cinfo->input_components != 4) + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); + break; + + default: /* JCS_UNKNOWN can be anything */ + if (cinfo->input_components < 1) + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); + break; + } + + /* Check num_components, set conversion method based on requested space */ + switch (cinfo->jpeg_color_space) { + case JCS_GRAYSCALE: + if (cinfo->num_components != 1) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + if (cinfo->in_color_space == JCS_GRAYSCALE || + cinfo->in_color_space == JCS_YCbCr) + cconvert->pub.color_convert = grayscale_convert; + else if (cinfo->in_color_space == JCS_RGB) { + cconvert->pub.start_pass = rgb_ycc_start; + cconvert->pub.color_convert = rgb_gray_convert; + } else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + case JCS_RGB: + if (cinfo->num_components != 3) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + if (cinfo->in_color_space == JCS_RGB) + cconvert->pub.color_convert = rgb_convert; + else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + case JCS_YCbCr: + if (cinfo->num_components != 3) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + if (cinfo->in_color_space == JCS_RGB) { + cconvert->pub.start_pass = rgb_ycc_start; + cconvert->pub.color_convert = rgb_ycc_convert; + } else if (cinfo->in_color_space == JCS_YCbCr) + cconvert->pub.color_convert = null_convert; + else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + case JCS_CMYK: + if (cinfo->num_components != 4) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + if (cinfo->in_color_space == JCS_CMYK) + cconvert->pub.color_convert = null_convert; + else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + case JCS_YCCK: + if (cinfo->num_components != 4) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + if (cinfo->in_color_space == JCS_CMYK) { + cconvert->pub.start_pass = rgb_ycc_start; + cconvert->pub.color_convert = cmyk_ycck_convert; + } else if (cinfo->in_color_space == JCS_YCCK) + cconvert->pub.color_convert = null_convert; + else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + default: /* allow null conversion of JCS_UNKNOWN */ + if (cinfo->jpeg_color_space != cinfo->in_color_space || + cinfo->num_components != cinfo->input_components) + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + cconvert->pub.color_convert = null_convert; + break; + } +} diff --git a/crypto777/jpeg/jcdctmgr.c b/crypto777/jpeg/jcdctmgr.c new file mode 100644 index 000000000..0bbdbb685 --- /dev/null +++ b/crypto777/jpeg/jcdctmgr.c @@ -0,0 +1,482 @@ +/* + * jcdctmgr.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the forward-DCT management logic. + * This code selects a particular DCT implementation to be used, + * and it performs related housekeeping chores including coefficient + * quantization. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + + +/* Private subobject for this module */ + +typedef struct { + struct jpeg_forward_dct pub; /* public fields */ + + /* Pointer to the DCT routine actually in use */ + forward_DCT_method_ptr do_dct[MAX_COMPONENTS]; + + /* The actual post-DCT divisors --- not identical to the quant table + * entries, because of scaling (especially for an unnormalized DCT). + * Each table is given in normal array order. + */ + DCTELEM * divisors[NUM_QUANT_TBLS]; + +#ifdef DCT_FLOAT_SUPPORTED + /* Same as above for the floating-point case. */ + float_DCT_method_ptr do_float_dct[MAX_COMPONENTS]; + FAST_FLOAT * float_divisors[NUM_QUANT_TBLS]; +#endif +} my_fdct_controller; + +typedef my_fdct_controller * my_fdct_ptr; + + +/* The current scaled-DCT routines require ISLOW-style divisor tables, + * so be sure to compile that code if either ISLOW or SCALING is requested. + */ +#ifdef DCT_ISLOW_SUPPORTED +#define PROVIDE_ISLOW_TABLES +#else +#ifdef DCT_SCALING_SUPPORTED +#define PROVIDE_ISLOW_TABLES +#endif +#endif + + +/* + * Perform forward DCT on one or more blocks of a component. + * + * The input samples are taken from the sample_data[] array starting at + * position start_row/start_col, and moving to the right for any additional + * blocks. The quantized coefficients are returned in coef_blocks[]. + */ + +METHODDEF(void) +forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY sample_data, JBLOCKROW coef_blocks, + JDIMENSION start_row, JDIMENSION start_col, + JDIMENSION num_blocks) +/* This version is used for integer DCT implementations. */ +{ + /* This routine is heavily used, so it's worth coding it tightly. */ + my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; + forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index]; + DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no]; + DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */ + JDIMENSION bi; + + sample_data += start_row; /* fold in the vertical offset once */ + + for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) { + /* Perform the DCT */ + (*do_dct) (workspace, sample_data, start_col); + + /* Quantize/descale the coefficients, and store into coef_blocks[] */ + { register DCTELEM temp, qval; + register int i; + register JCOEFPTR output_ptr = coef_blocks[bi]; + + for (i = 0; i < DCTSIZE2; i++) { + qval = divisors[i]; + temp = workspace[i]; + /* Divide the coefficient value by qval, ensuring proper rounding. + * Since C does not specify the direction of rounding for negative + * quotients, we have to force the dividend positive for portability. + * + * In most files, at least half of the output values will be zero + * (at default quantization settings, more like three-quarters...) + * so we should ensure that this case is fast. On many machines, + * a comparison is enough cheaper than a divide to make a special test + * a win. Since both inputs will be nonnegative, we need only test + * for a < b to discover whether a/b is 0. + * If your machine's division is fast enough, define FAST_DIVIDE. + */ +#ifdef FAST_DIVIDE +#define DIVIDE_BY(a,b) a /= b +#else +#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0 +#endif + if (temp < 0) { + temp = -temp; + temp += qval>>1; /* for rounding */ + DIVIDE_BY(temp, qval); + temp = -temp; + } else { + temp += qval>>1; /* for rounding */ + DIVIDE_BY(temp, qval); + } + output_ptr[i] = (JCOEF) temp; + } + } + } +} + + +#ifdef DCT_FLOAT_SUPPORTED + +METHODDEF(void) +forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY sample_data, JBLOCKROW coef_blocks, + JDIMENSION start_row, JDIMENSION start_col, + JDIMENSION num_blocks) +/* This version is used for floating-point DCT implementations. */ +{ + /* This routine is heavily used, so it's worth coding it tightly. */ + my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; + float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index]; + FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no]; + FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */ + JDIMENSION bi; + + sample_data += start_row; /* fold in the vertical offset once */ + + for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) { + /* Perform the DCT */ + (*do_dct) (workspace, sample_data, start_col); + + /* Quantize/descale the coefficients, and store into coef_blocks[] */ + { register FAST_FLOAT temp; + register int i; + register JCOEFPTR output_ptr = coef_blocks[bi]; + + for (i = 0; i < DCTSIZE2; i++) { + /* Apply the quantization and scaling factor */ + temp = workspace[i] * divisors[i]; + /* Round to nearest integer. + * Since C does not specify the direction of rounding for negative + * quotients, we have to force the dividend positive for portability. + * The maximum coefficient size is +-16K (for 12-bit data), so this + * code should work for either 16-bit or 32-bit ints. + */ + output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384); + } + } + } +} + +#endif /* DCT_FLOAT_SUPPORTED */ + + +/* + * Initialize for a processing pass. + * Verify that all referenced Q-tables are present, and set up + * the divisor table for each one. + * In the current implementation, DCT of all components is done during + * the first pass, even if only some components will be output in the + * first scan. Hence all components should be examined here. + */ + +METHODDEF(void) +start_pass_fdctmgr (j_compress_ptr cinfo) +{ + my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; + int ci, qtblno, i; + jpeg_component_info *compptr; + int method = 0; + JQUANT_TBL * qtbl; + DCTELEM * dtbl; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Select the proper DCT routine for this component's scaling */ + switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) { +#ifdef DCT_SCALING_SUPPORTED + case ((1 << 8) + 1): + fdct->do_dct[ci] = jpeg_fdct_1x1; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((2 << 8) + 2): + fdct->do_dct[ci] = jpeg_fdct_2x2; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((3 << 8) + 3): + fdct->do_dct[ci] = jpeg_fdct_3x3; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((4 << 8) + 4): + fdct->do_dct[ci] = jpeg_fdct_4x4; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((5 << 8) + 5): + fdct->do_dct[ci] = jpeg_fdct_5x5; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((6 << 8) + 6): + fdct->do_dct[ci] = jpeg_fdct_6x6; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((7 << 8) + 7): + fdct->do_dct[ci] = jpeg_fdct_7x7; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((9 << 8) + 9): + fdct->do_dct[ci] = jpeg_fdct_9x9; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((10 << 8) + 10): + fdct->do_dct[ci] = jpeg_fdct_10x10; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((11 << 8) + 11): + fdct->do_dct[ci] = jpeg_fdct_11x11; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((12 << 8) + 12): + fdct->do_dct[ci] = jpeg_fdct_12x12; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((13 << 8) + 13): + fdct->do_dct[ci] = jpeg_fdct_13x13; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((14 << 8) + 14): + fdct->do_dct[ci] = jpeg_fdct_14x14; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((15 << 8) + 15): + fdct->do_dct[ci] = jpeg_fdct_15x15; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((16 << 8) + 16): + fdct->do_dct[ci] = jpeg_fdct_16x16; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((16 << 8) + 8): + fdct->do_dct[ci] = jpeg_fdct_16x8; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((14 << 8) + 7): + fdct->do_dct[ci] = jpeg_fdct_14x7; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((12 << 8) + 6): + fdct->do_dct[ci] = jpeg_fdct_12x6; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((10 << 8) + 5): + fdct->do_dct[ci] = jpeg_fdct_10x5; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((8 << 8) + 4): + fdct->do_dct[ci] = jpeg_fdct_8x4; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((6 << 8) + 3): + fdct->do_dct[ci] = jpeg_fdct_6x3; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((4 << 8) + 2): + fdct->do_dct[ci] = jpeg_fdct_4x2; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((2 << 8) + 1): + fdct->do_dct[ci] = jpeg_fdct_2x1; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((8 << 8) + 16): + fdct->do_dct[ci] = jpeg_fdct_8x16; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((7 << 8) + 14): + fdct->do_dct[ci] = jpeg_fdct_7x14; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((6 << 8) + 12): + fdct->do_dct[ci] = jpeg_fdct_6x12; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((5 << 8) + 10): + fdct->do_dct[ci] = jpeg_fdct_5x10; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((4 << 8) + 8): + fdct->do_dct[ci] = jpeg_fdct_4x8; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((3 << 8) + 6): + fdct->do_dct[ci] = jpeg_fdct_3x6; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((2 << 8) + 4): + fdct->do_dct[ci] = jpeg_fdct_2x4; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; + case ((1 << 8) + 2): + fdct->do_dct[ci] = jpeg_fdct_1x2; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ + break; +#endif + case ((DCTSIZE << 8) + DCTSIZE): + switch (cinfo->dct_method) { +#ifdef DCT_ISLOW_SUPPORTED + case JDCT_ISLOW: + fdct->do_dct[ci] = jpeg_fdct_islow; + method = JDCT_ISLOW; + break; +#endif +#ifdef DCT_IFAST_SUPPORTED + case JDCT_IFAST: + fdct->do_dct[ci] = jpeg_fdct_ifast; + method = JDCT_IFAST; + break; +#endif +#ifdef DCT_FLOAT_SUPPORTED + case JDCT_FLOAT: + fdct->do_float_dct[ci] = jpeg_fdct_float; + method = JDCT_FLOAT; + break; +#endif + default: + ERREXIT(cinfo, JERR_NOT_COMPILED); + break; + } + break; + default: + ERREXIT2(cinfo, JERR_BAD_DCTSIZE, + compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size); + break; + } + qtblno = compptr->quant_tbl_no; + /* Make sure specified quantization table is present */ + if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || + cinfo->quant_tbl_ptrs[qtblno] == NULL) + ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); + qtbl = cinfo->quant_tbl_ptrs[qtblno]; + /* Compute divisors for this quant table */ + /* We may do this more than once for same table, but it's not a big deal */ + switch (method) { +#ifdef PROVIDE_ISLOW_TABLES + case JDCT_ISLOW: + /* For LL&M IDCT method, divisors are equal to raw quantization + * coefficients multiplied by 8 (to counteract scaling). + */ + if (fdct->divisors[qtblno] == NULL) { + fdct->divisors[qtblno] = (DCTELEM *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + DCTSIZE2 * SIZEOF(DCTELEM)); + } + dtbl = fdct->divisors[qtblno]; + for (i = 0; i < DCTSIZE2; i++) { + dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3; + } + fdct->pub.forward_DCT[ci] = forward_DCT; + break; +#endif +#ifdef DCT_IFAST_SUPPORTED + case JDCT_IFAST: + { + /* For AA&N IDCT method, divisors are equal to quantization + * coefficients scaled by scalefactor[row]*scalefactor[col], where + * scalefactor[0] = 1 + * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 + * We apply a further scale factor of 8. + */ +#define CONST_BITS 14 + static const INT16 aanscales[DCTSIZE2] = { + /* precomputed values scaled up by 14 bits */ + 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, + 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, + 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, + 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, + 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, + 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, + 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, + 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 + }; + SHIFT_TEMPS + + if (fdct->divisors[qtblno] == NULL) { + fdct->divisors[qtblno] = (DCTELEM *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + DCTSIZE2 * SIZEOF(DCTELEM)); + } + dtbl = fdct->divisors[qtblno]; + for (i = 0; i < DCTSIZE2; i++) { + dtbl[i] = (DCTELEM) + DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], + (INT32) aanscales[i]), + CONST_BITS-3); + } + } + fdct->pub.forward_DCT[ci] = forward_DCT; + break; +#endif +#ifdef DCT_FLOAT_SUPPORTED + case JDCT_FLOAT: + { + /* For float AA&N IDCT method, divisors are equal to quantization + * coefficients scaled by scalefactor[row]*scalefactor[col], where + * scalefactor[0] = 1 + * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 + * We apply a further scale factor of 8. + * What's actually stored is 1/divisor so that the inner loop can + * use a multiplication rather than a division. + */ + FAST_FLOAT * fdtbl; + int row, col; + static const double aanscalefactor[DCTSIZE] = { + 1.0, 1.387039845, 1.306562965, 1.175875602, + 1.0, 0.785694958, 0.541196100, 0.275899379 + }; + + if (fdct->float_divisors[qtblno] == NULL) { + fdct->float_divisors[qtblno] = (FAST_FLOAT *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + DCTSIZE2 * SIZEOF(FAST_FLOAT)); + } + fdtbl = fdct->float_divisors[qtblno]; + i = 0; + for (row = 0; row < DCTSIZE; row++) { + for (col = 0; col < DCTSIZE; col++) { + fdtbl[i] = (FAST_FLOAT) + (1.0 / (((double) qtbl->quantval[i] * + aanscalefactor[row] * aanscalefactor[col] * 8.0))); + i++; + } + } + } + fdct->pub.forward_DCT[ci] = forward_DCT_float; + break; +#endif + default: + ERREXIT(cinfo, JERR_NOT_COMPILED); + break; + } + } +} + + +/* + * Initialize FDCT manager. + */ + +GLOBAL(void) +jinit_forward_dct (j_compress_ptr cinfo) +{ + my_fdct_ptr fdct; + int i; + + fdct = (my_fdct_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_fdct_controller)); + cinfo->fdct = (struct jpeg_forward_dct *) fdct; + fdct->pub.start_pass = start_pass_fdctmgr; + + /* Mark divisor tables unallocated */ + for (i = 0; i < NUM_QUANT_TBLS; i++) { + fdct->divisors[i] = NULL; +#ifdef DCT_FLOAT_SUPPORTED + fdct->float_divisors[i] = NULL; +#endif + } +} diff --git a/crypto777/jpeg/jchuff.c b/crypto777/jpeg/jchuff.c new file mode 100644 index 000000000..257d7aa1f --- /dev/null +++ b/crypto777/jpeg/jchuff.c @@ -0,0 +1,1576 @@ +/* + * jchuff.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2006-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains Huffman entropy encoding routines. + * Both sequential and progressive modes are supported in this single module. + * + * Much of the complexity here has to do with supporting output suspension. + * If the data destination module demands suspension, we want to be able to + * back up to the start of the current MCU. To do this, we copy state + * variables into local working storage, and update them back to the + * permanent JPEG objects only upon successful completion of an MCU. + * + * We do not support output suspension for the progressive JPEG mode, since + * the library currently does not allow multiple-scan files to be written + * with output suspension. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* The legal range of a DCT coefficient is + * -1024 .. +1023 for 8-bit data; + * -16384 .. +16383 for 12-bit data. + * Hence the magnitude should always fit in 10 or 14 bits respectively. + */ + +#if BITS_IN_JSAMPLE == 8 +#define MAX_COEF_BITS 10 +#else +#define MAX_COEF_BITS 14 +#endif + +/* Derived data constructed for each Huffman table */ + +typedef struct { + unsigned int ehufco[256]; /* code for each symbol */ + char ehufsi[256]; /* length of code for each symbol */ + /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */ +} c_derived_tbl; + + +/* Expanded entropy encoder object for Huffman encoding. + * + * The savable_state subrecord contains fields that change within an MCU, + * but must not be updated permanently until we complete the MCU. + */ + +typedef struct { + INT32 put_buffer; /* current bit-accumulation buffer */ + int put_bits; /* # of bits now in it */ + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ +} savable_state; + +/* This macro is to work around compilers with missing or broken + * structure assignment. You'll need to fix this code if you have + * such a compiler and you change MAX_COMPS_IN_SCAN. + */ + +#ifndef NO_STRUCT_ASSIGN +#define ASSIGN_STATE(dest,src) ((dest) = (src)) +#else +#if MAX_COMPS_IN_SCAN == 4 +#define ASSIGN_STATE(dest,src) \ + ((dest).put_buffer = (src).put_buffer, \ + (dest).put_bits = (src).put_bits, \ + (dest).last_dc_val[0] = (src).last_dc_val[0], \ + (dest).last_dc_val[1] = (src).last_dc_val[1], \ + (dest).last_dc_val[2] = (src).last_dc_val[2], \ + (dest).last_dc_val[3] = (src).last_dc_val[3]) +#endif +#endif + + +typedef struct { + struct jpeg_entropy_encoder pub; /* public fields */ + + savable_state saved; /* Bit buffer & DC state at start of MCU */ + + /* These fields are NOT loaded into local working state. */ + unsigned int restarts_to_go; /* MCUs left in this restart interval */ + int next_restart_num; /* next restart number to write (0-7) */ + + /* Pointers to derived tables (these workspaces have image lifespan) */ + c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; + c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; + + /* Statistics tables for optimization */ + long * dc_count_ptrs[NUM_HUFF_TBLS]; + long * ac_count_ptrs[NUM_HUFF_TBLS]; + + /* Following fields used only in progressive mode */ + + /* Mode flag: TRUE for optimization, FALSE for actual data output */ + boolean gather_statistics; + + /* next_output_byte/free_in_buffer are local copies of cinfo->dest fields. + */ + JOCTET * next_output_byte; /* => next byte to write in buffer */ + size_t free_in_buffer; /* # of byte spaces remaining in buffer */ + j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ + + /* Coding status for AC components */ + int ac_tbl_no; /* the table number of the single component */ + unsigned int EOBRUN; /* run length of EOBs */ + unsigned int BE; /* # of buffered correction bits before MCU */ + char * bit_buffer; /* buffer for correction bits (1 per char) */ + /* packing correction bits tightly would save some space but cost time... */ +} huff_entropy_encoder; + +typedef huff_entropy_encoder * huff_entropy_ptr; + +/* Working state while writing an MCU (sequential mode). + * This struct contains all the fields that are needed by subroutines. + */ + +typedef struct { + JOCTET * next_output_byte; /* => next byte to write in buffer */ + size_t free_in_buffer; /* # of byte spaces remaining in buffer */ + savable_state cur; /* Current bit buffer & DC state */ + j_compress_ptr cinfo; /* dump_buffer needs access to this */ +} working_state; + +/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit + * buffer can hold. Larger sizes may slightly improve compression, but + * 1000 is already well into the realm of overkill. + * The minimum safe size is 64 bits. + */ + +#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ + +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. + * We assume that int right shift is unsigned if INT32 right shift is, + * which should be safe. + */ + +#ifdef RIGHT_SHIFT_IS_UNSIGNED +#define ISHIFT_TEMPS int ishift_temp; +#define IRIGHT_SHIFT(x,shft) \ + ((ishift_temp = (x)) < 0 ? \ + (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ + (ishift_temp >> (shft))) +#else +#define ISHIFT_TEMPS +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) +#endif + + +/* + * Compute the derived values for a Huffman table. + * This routine also performs some validation checks on the table. + */ + +LOCAL(void) +jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno, + c_derived_tbl ** pdtbl) +{ + JHUFF_TBL *htbl; + c_derived_tbl *dtbl; + int p, i, l, lastp, si, maxsymbol; + char huffsize[257]; + unsigned int huffcode[257]; + unsigned int code; + + /* Note that huffsize[] and huffcode[] are filled in code-length order, + * paralleling the order of the symbols themselves in htbl->huffval[]. + */ + + /* Find the input Huffman table */ + if (tblno < 0 || tblno >= NUM_HUFF_TBLS) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); + htbl = + isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; + if (htbl == NULL) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); + + /* Allocate a workspace if we haven't already done so. */ + if (*pdtbl == NULL) + *pdtbl = (c_derived_tbl *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(c_derived_tbl)); + dtbl = *pdtbl; + + /* Figure C.1: make table of Huffman code length for each symbol */ + + p = 0; + for (l = 1; l <= 16; l++) { + i = (int) htbl->bits[l]; + if (i < 0 || p + i > 256) /* protect against table overrun */ + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + while (i--) + huffsize[p++] = (char) l; + } + huffsize[p] = 0; + lastp = p; + + /* Figure C.2: generate the codes themselves */ + /* We also validate that the counts represent a legal Huffman code tree. */ + + code = 0; + si = huffsize[0]; + p = 0; + while (huffsize[p]) { + while (((int) huffsize[p]) == si) { + huffcode[p++] = code; + code++; + } + /* code is now 1 more than the last code used for codelength si; but + * it must still fit in si bits, since no code is allowed to be all ones. + */ + if (((INT32) code) >= (((INT32) 1) << si)) + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + code <<= 1; + si++; + } + + /* Figure C.3: generate encoding tables */ + /* These are code and size indexed by symbol value */ + + /* Set all codeless symbols to have code length 0; + * this lets us detect duplicate VAL entries here, and later + * allows emit_bits to detect any attempt to emit such symbols. + */ + MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi)); + + /* This is also a convenient place to check for out-of-range + * and duplicated VAL entries. We allow 0..255 for AC symbols + * but only 0..15 for DC. (We could constrain them further + * based on data depth and mode, but this seems enough.) + */ + maxsymbol = isDC ? 15 : 255; + + for (p = 0; p < lastp; p++) { + i = htbl->huffval[p]; + if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + dtbl->ehufco[i] = huffcode[p]; + dtbl->ehufsi[i] = huffsize[p]; + } +} + + +/* Outputting bytes to the file. + * NB: these must be called only when actually outputting, + * that is, entropy->gather_statistics == FALSE. + */ + +/* Emit a byte, taking 'action' if must suspend. */ +#define emit_byte_s(state,val,action) \ + { *(state)->next_output_byte++ = (JOCTET) (val); \ + if (--(state)->free_in_buffer == 0) \ + if (! dump_buffer_s(state)) \ + { action; } } + +/* Emit a byte */ +#define emit_byte_e(entropy,val) \ + { *(entropy)->next_output_byte++ = (JOCTET) (val); \ + if (--(entropy)->free_in_buffer == 0) \ + dump_buffer_e(entropy); } + + +LOCAL(boolean) +dump_buffer_s (working_state * state) +/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */ +{ + struct jpeg_destination_mgr * dest = state->cinfo->dest; + + if (! (*dest->empty_output_buffer) (state->cinfo)) + return FALSE; + /* After a successful buffer dump, must reset buffer pointers */ + state->next_output_byte = dest->next_output_byte; + state->free_in_buffer = dest->free_in_buffer; + return TRUE; +} + + +LOCAL(void) +dump_buffer_e (huff_entropy_ptr entropy) +/* Empty the output buffer; we do not support suspension in this case. */ +{ + struct jpeg_destination_mgr * dest = entropy->cinfo->dest; + + if (! (*dest->empty_output_buffer) (entropy->cinfo)) + ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); + /* After a successful buffer dump, must reset buffer pointers */ + entropy->next_output_byte = dest->next_output_byte; + entropy->free_in_buffer = dest->free_in_buffer; +} + + +/* Outputting bits to the file */ + +/* Only the right 24 bits of put_buffer are used; the valid bits are + * left-justified in this part. At most 16 bits can be passed to emit_bits + * in one call, and we never retain more than 7 bits in put_buffer + * between calls, so 24 bits are sufficient. + */ + +INLINE +LOCAL(boolean) +emit_bits_s (working_state * state, unsigned int code, int size) +/* Emit some bits; return TRUE if successful, FALSE if must suspend */ +{ + /* This routine is heavily used, so it's worth coding tightly. */ + register INT32 put_buffer = (INT32) code; + register int put_bits = state->cur.put_bits; + + /* if size is 0, caller used an invalid Huffman table entry */ + if (size == 0) + ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE); + + put_buffer &= (((INT32) 1)<cur.put_buffer; /* and merge with old buffer contents */ + + while (put_bits >= 8) { + int c = (int) ((put_buffer >> 16) & 0xFF); + + emit_byte_s(state, c, return FALSE); + if (c == 0xFF) { /* need to stuff a zero byte? */ + emit_byte_s(state, 0, return FALSE); + } + put_buffer <<= 8; + put_bits -= 8; + } + + state->cur.put_buffer = put_buffer; /* update state variables */ + state->cur.put_bits = put_bits; + + return TRUE; +} + + +INLINE +LOCAL(void) +emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size) +/* Emit some bits, unless we are in gather mode */ +{ + /* This routine is heavily used, so it's worth coding tightly. */ + register INT32 put_buffer = (INT32) code; + register int put_bits = entropy->saved.put_bits; + + /* if size is 0, caller used an invalid Huffman table entry */ + if (size == 0) + ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); + + if (entropy->gather_statistics) + return; /* do nothing if we're only getting stats */ + + put_buffer &= (((INT32) 1)<saved.put_buffer; + + while (put_bits >= 8) { + int c = (int) ((put_buffer >> 16) & 0xFF); + + emit_byte_e(entropy, c); + if (c == 0xFF) { /* need to stuff a zero byte? */ + emit_byte_e(entropy, 0); + } + put_buffer <<= 8; + put_bits -= 8; + } + + entropy->saved.put_buffer = put_buffer; /* update variables */ + entropy->saved.put_bits = put_bits; +} + + +LOCAL(boolean) +flush_bits_s (working_state * state) +{ + if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */ + return FALSE; + state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ + state->cur.put_bits = 0; + return TRUE; +} + + +LOCAL(void) +flush_bits_e (huff_entropy_ptr entropy) +{ + emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */ + entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */ + entropy->saved.put_bits = 0; +} + + +/* + * Emit (or just count) a Huffman symbol. + */ + +INLINE +LOCAL(void) +emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol) +{ + if (entropy->gather_statistics) + entropy->dc_count_ptrs[tbl_no][symbol]++; + else { + c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no]; + emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); + } +} + + +INLINE +LOCAL(void) +emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol) +{ + if (entropy->gather_statistics) + entropy->ac_count_ptrs[tbl_no][symbol]++; + else { + c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no]; + emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); + } +} + + +/* + * Emit bits from a correction bit buffer. + */ + +LOCAL(void) +emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart, + unsigned int nbits) +{ + if (entropy->gather_statistics) + return; /* no real work */ + + while (nbits > 0) { + emit_bits_e(entropy, (unsigned int) (*bufstart), 1); + bufstart++; + nbits--; + } +} + + +/* + * Emit any pending EOBRUN symbol. + */ + +LOCAL(void) +emit_eobrun (huff_entropy_ptr entropy) +{ + register int temp, nbits; + + if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ + temp = entropy->EOBRUN; + nbits = 0; + while ((temp >>= 1)) + nbits++; + /* safety check: shouldn't happen given limited correction-bit buffer */ + if (nbits > 14) + ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); + + emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4); + if (nbits) + emit_bits_e(entropy, entropy->EOBRUN, nbits); + + entropy->EOBRUN = 0; + + /* Emit any buffered correction bits */ + emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); + entropy->BE = 0; + } +} + + +/* + * Emit a restart marker & resynchronize predictions. + */ + +LOCAL(boolean) +emit_restart_s (working_state * state, int restart_num) +{ + int ci; + + if (! flush_bits_s(state)) + return FALSE; + + emit_byte_s(state, 0xFF, return FALSE); + emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE); + + /* Re-initialize DC predictions to 0 */ + for (ci = 0; ci < state->cinfo->comps_in_scan; ci++) + state->cur.last_dc_val[ci] = 0; + + /* The restart counter is not updated until we successfully write the MCU. */ + + return TRUE; +} + + +LOCAL(void) +emit_restart_e (huff_entropy_ptr entropy, int restart_num) +{ + int ci; + + emit_eobrun(entropy); + + if (! entropy->gather_statistics) { + flush_bits_e(entropy); + emit_byte_e(entropy, 0xFF); + emit_byte_e(entropy, JPEG_RST0 + restart_num); + } + + if (entropy->cinfo->Ss == 0) { + /* Re-initialize DC predictions to 0 */ + for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) + entropy->saved.last_dc_val[ci] = 0; + } else { + /* Re-initialize all AC-related fields to 0 */ + entropy->EOBRUN = 0; + entropy->BE = 0; + } +} + + +/* + * MCU encoding for DC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + register int temp, temp2; + register int nbits; + int blkn, ci; + int Al = cinfo->Al; + JBLOCKROW block; + jpeg_component_info * compptr; + ISHIFT_TEMPS + + entropy->next_output_byte = cinfo->dest->next_output_byte; + entropy->free_in_buffer = cinfo->dest->free_in_buffer; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) + if (entropy->restarts_to_go == 0) + emit_restart_e(entropy, entropy->next_restart_num); + + /* Encode the MCU data blocks */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + + /* Compute the DC value after the required point transform by Al. + * This is simply an arithmetic right shift. + */ + temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); + + /* DC differences are figured on the point-transformed values. */ + temp = temp2 - entropy->saved.last_dc_val[ci]; + entropy->saved.last_dc_val[ci] = temp2; + + /* Encode the DC coefficient difference per section G.1.2.1 */ + temp2 = temp; + if (temp < 0) { + temp = -temp; /* temp is abs value of input */ + /* For a negative input, want temp2 = bitwise complement of abs(input) */ + /* This code assumes we are on a two's complement machine */ + temp2--; + } + + /* Find the number of bits needed for the magnitude of the coefficient */ + nbits = 0; + while (temp) { + nbits++; + temp >>= 1; + } + /* Check for out-of-range coefficient values. + * Since we're encoding a difference, the range limit is twice as much. + */ + if (nbits > MAX_COEF_BITS+1) + ERREXIT(cinfo, JERR_BAD_DCT_COEF); + + /* Count/emit the Huffman-coded symbol for the number of bits */ + emit_dc_symbol(entropy, compptr->dc_tbl_no, nbits); + + /* Emit that number of bits of the value, if positive, */ + /* or the complement of its magnitude, if negative. */ + if (nbits) /* emit_bits rejects calls with size 0 */ + emit_bits_e(entropy, (unsigned int) temp2, nbits); + } + + cinfo->dest->next_output_byte = entropy->next_output_byte; + cinfo->dest->free_in_buffer = entropy->free_in_buffer; + + /* Update restart-interval state too */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + return TRUE; +} + + +/* + * MCU encoding for AC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + register int temp, temp2; + register int nbits; + register int r, k; + int Se, Al; + const int * natural_order; + JBLOCKROW block; + + entropy->next_output_byte = cinfo->dest->next_output_byte; + entropy->free_in_buffer = cinfo->dest->free_in_buffer; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) + if (entropy->restarts_to_go == 0) + emit_restart_e(entropy, entropy->next_restart_num); + + Se = cinfo->Se; + Al = cinfo->Al; + natural_order = cinfo->natural_order; + + /* Encode the MCU data block */ + block = MCU_data[0]; + + /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ + + r = 0; /* r = run length of zeros */ + + for (k = cinfo->Ss; k <= Se; k++) { + if ((temp = (*block)[natural_order[k]]) == 0) { + r++; + continue; + } + /* We must apply the point transform by Al. For AC coefficients this + * is an integer division with rounding towards 0. To do this portably + * in C, we shift after obtaining the absolute value; so the code is + * interwoven with finding the abs value (temp) and output bits (temp2). + */ + if (temp < 0) { + temp = -temp; /* temp is abs value of input */ + temp >>= Al; /* apply the point transform */ + /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ + temp2 = ~temp; + } else { + temp >>= Al; /* apply the point transform */ + temp2 = temp; + } + /* Watch out for case that nonzero coef is zero after point transform */ + if (temp == 0) { + r++; + continue; + } + + /* Emit any pending EOBRUN */ + if (entropy->EOBRUN > 0) + emit_eobrun(entropy); + /* if run length > 15, must emit special run-length-16 codes (0xF0) */ + while (r > 15) { + emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0); + r -= 16; + } + + /* Find the number of bits needed for the magnitude of the coefficient */ + nbits = 1; /* there must be at least one 1 bit */ + while ((temp >>= 1)) + nbits++; + /* Check for out-of-range coefficient values */ + if (nbits > MAX_COEF_BITS) + ERREXIT(cinfo, JERR_BAD_DCT_COEF); + + /* Count/emit Huffman symbol for run length / number of bits */ + emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); + + /* Emit that number of bits of the value, if positive, */ + /* or the complement of its magnitude, if negative. */ + emit_bits_e(entropy, (unsigned int) temp2, nbits); + + r = 0; /* reset zero run length */ + } + + if (r > 0) { /* If there are trailing zeroes, */ + entropy->EOBRUN++; /* count an EOB */ + if (entropy->EOBRUN == 0x7FFF) + emit_eobrun(entropy); /* force it out to avoid overflow */ + } + + cinfo->dest->next_output_byte = entropy->next_output_byte; + cinfo->dest->free_in_buffer = entropy->free_in_buffer; + + /* Update restart-interval state too */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + return TRUE; +} + + +/* + * MCU encoding for DC successive approximation refinement scan. + * Note: we assume such scans can be multi-component, although the spec + * is not very clear on the point. + */ + +METHODDEF(boolean) +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + register int temp; + int blkn; + int Al = cinfo->Al; + JBLOCKROW block; + + entropy->next_output_byte = cinfo->dest->next_output_byte; + entropy->free_in_buffer = cinfo->dest->free_in_buffer; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) + if (entropy->restarts_to_go == 0) + emit_restart_e(entropy, entropy->next_restart_num); + + /* Encode the MCU data blocks */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + + /* We simply emit the Al'th bit of the DC coefficient value. */ + temp = (*block)[0]; + emit_bits_e(entropy, (unsigned int) (temp >> Al), 1); + } + + cinfo->dest->next_output_byte = entropy->next_output_byte; + cinfo->dest->free_in_buffer = entropy->free_in_buffer; + + /* Update restart-interval state too */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + return TRUE; +} + + +/* + * MCU encoding for AC successive approximation refinement scan. + */ + +METHODDEF(boolean) +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + register int temp; + register int r, k; + int EOB; + char *BR_buffer; + unsigned int BR; + int Se, Al; + const int * natural_order; + JBLOCKROW block; + int absvalues[DCTSIZE2]; + + entropy->next_output_byte = cinfo->dest->next_output_byte; + entropy->free_in_buffer = cinfo->dest->free_in_buffer; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) + if (entropy->restarts_to_go == 0) + emit_restart_e(entropy, entropy->next_restart_num); + + Se = cinfo->Se; + Al = cinfo->Al; + natural_order = cinfo->natural_order; + + /* Encode the MCU data block */ + block = MCU_data[0]; + + /* It is convenient to make a pre-pass to determine the transformed + * coefficients' absolute values and the EOB position. + */ + EOB = 0; + for (k = cinfo->Ss; k <= Se; k++) { + temp = (*block)[natural_order[k]]; + /* We must apply the point transform by Al. For AC coefficients this + * is an integer division with rounding towards 0. To do this portably + * in C, we shift after obtaining the absolute value. + */ + if (temp < 0) + temp = -temp; /* temp is abs value of input */ + temp >>= Al; /* apply the point transform */ + absvalues[k] = temp; /* save abs value for main pass */ + if (temp == 1) + EOB = k; /* EOB = index of last newly-nonzero coef */ + } + + /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ + + r = 0; /* r = run length of zeros */ + BR = 0; /* BR = count of buffered bits added now */ + BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ + + for (k = cinfo->Ss; k <= Se; k++) { + if ((temp = absvalues[k]) == 0) { + r++; + continue; + } + + /* Emit any required ZRLs, but not if they can be folded into EOB */ + while (r > 15 && k <= EOB) { + /* emit any pending EOBRUN and the BE correction bits */ + emit_eobrun(entropy); + /* Emit ZRL */ + emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0); + r -= 16; + /* Emit buffered correction bits that must be associated with ZRL */ + emit_buffered_bits(entropy, BR_buffer, BR); + BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ + BR = 0; + } + + /* If the coef was previously nonzero, it only needs a correction bit. + * NOTE: a straight translation of the spec's figure G.7 would suggest + * that we also need to test r > 15. But if r > 15, we can only get here + * if k > EOB, which implies that this coefficient is not 1. + */ + if (temp > 1) { + /* The correction bit is the next bit of the absolute value. */ + BR_buffer[BR++] = (char) (temp & 1); + continue; + } + + /* Emit any pending EOBRUN and the BE correction bits */ + emit_eobrun(entropy); + + /* Count/emit Huffman symbol for run length / number of bits */ + emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); + + /* Emit output bit for newly-nonzero coef */ + temp = ((*block)[natural_order[k]] < 0) ? 0 : 1; + emit_bits_e(entropy, (unsigned int) temp, 1); + + /* Emit buffered correction bits that must be associated with this code */ + emit_buffered_bits(entropy, BR_buffer, BR); + BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ + BR = 0; + r = 0; /* reset zero run length */ + } + + if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ + entropy->EOBRUN++; /* count an EOB */ + entropy->BE += BR; /* concat my correction bits to older ones */ + /* We force out the EOB if we risk either: + * 1. overflow of the EOB counter; + * 2. overflow of the correction bit buffer during the next MCU. + */ + if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) + emit_eobrun(entropy); + } + + cinfo->dest->next_output_byte = entropy->next_output_byte; + cinfo->dest->free_in_buffer = entropy->free_in_buffer; + + /* Update restart-interval state too */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + return TRUE; +} + + +/* Encode a single block's worth of coefficients */ + +LOCAL(boolean) +encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val, + c_derived_tbl *dctbl, c_derived_tbl *actbl) +{ + register int temp, temp2; + register int nbits; + register int k, r, i; + int Se = state->cinfo->lim_Se; + const int * natural_order = state->cinfo->natural_order; + + /* Encode the DC coefficient difference per section F.1.2.1 */ + + temp = temp2 = block[0] - last_dc_val; + + if (temp < 0) { + temp = -temp; /* temp is abs value of input */ + /* For a negative input, want temp2 = bitwise complement of abs(input) */ + /* This code assumes we are on a two's complement machine */ + temp2--; + } + + /* Find the number of bits needed for the magnitude of the coefficient */ + nbits = 0; + while (temp) { + nbits++; + temp >>= 1; + } + /* Check for out-of-range coefficient values. + * Since we're encoding a difference, the range limit is twice as much. + */ + if (nbits > MAX_COEF_BITS+1) + ERREXIT(state->cinfo, JERR_BAD_DCT_COEF); + + /* Emit the Huffman-coded symbol for the number of bits */ + if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits])) + return FALSE; + + /* Emit that number of bits of the value, if positive, */ + /* or the complement of its magnitude, if negative. */ + if (nbits) /* emit_bits rejects calls with size 0 */ + if (! emit_bits_s(state, (unsigned int) temp2, nbits)) + return FALSE; + + /* Encode the AC coefficients per section F.1.2.2 */ + + r = 0; /* r = run length of zeros */ + + for (k = 1; k <= Se; k++) { + if ((temp = block[natural_order[k]]) == 0) { + r++; + } else { + /* if run length > 15, must emit special run-length-16 codes (0xF0) */ + while (r > 15) { + if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0])) + return FALSE; + r -= 16; + } + + temp2 = temp; + if (temp < 0) { + temp = -temp; /* temp is abs value of input */ + /* This code assumes we are on a two's complement machine */ + temp2--; + } + + /* Find the number of bits needed for the magnitude of the coefficient */ + nbits = 1; /* there must be at least one 1 bit */ + while ((temp >>= 1)) + nbits++; + /* Check for out-of-range coefficient values */ + if (nbits > MAX_COEF_BITS) + ERREXIT(state->cinfo, JERR_BAD_DCT_COEF); + + /* Emit Huffman symbol for run length / number of bits */ + i = (r << 4) + nbits; + if (! emit_bits_s(state, actbl->ehufco[i], actbl->ehufsi[i])) + return FALSE; + + /* Emit that number of bits of the value, if positive, */ + /* or the complement of its magnitude, if negative. */ + if (! emit_bits_s(state, (unsigned int) temp2, nbits)) + return FALSE; + + r = 0; + } + } + + /* If the last coef(s) were zero, emit an end-of-block code */ + if (r > 0) + if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0])) + return FALSE; + + return TRUE; +} + + +/* + * Encode and output one MCU's worth of Huffman-compressed coefficients. + */ + +METHODDEF(boolean) +encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + working_state state; + int blkn, ci; + jpeg_component_info * compptr; + + /* Load up working state */ + state.next_output_byte = cinfo->dest->next_output_byte; + state.free_in_buffer = cinfo->dest->free_in_buffer; + ASSIGN_STATE(state.cur, entropy->saved); + state.cinfo = cinfo; + + /* Emit restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! emit_restart_s(&state, entropy->next_restart_num)) + return FALSE; + } + + /* Encode the MCU data blocks */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + if (! encode_one_block(&state, + MCU_data[blkn][0], state.cur.last_dc_val[ci], + entropy->dc_derived_tbls[compptr->dc_tbl_no], + entropy->ac_derived_tbls[compptr->ac_tbl_no])) + return FALSE; + /* Update last_dc_val */ + state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; + } + + /* Completed MCU, so update state */ + cinfo->dest->next_output_byte = state.next_output_byte; + cinfo->dest->free_in_buffer = state.free_in_buffer; + ASSIGN_STATE(entropy->saved, state.cur); + + /* Update restart-interval state too */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num++; + entropy->next_restart_num &= 7; + } + entropy->restarts_to_go--; + } + + return TRUE; +} + + +/* + * Finish up at the end of a Huffman-compressed scan. + */ + +METHODDEF(void) +finish_pass_huff (j_compress_ptr cinfo) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + working_state state; + + if (cinfo->progressive_mode) { + entropy->next_output_byte = cinfo->dest->next_output_byte; + entropy->free_in_buffer = cinfo->dest->free_in_buffer; + + /* Flush out any buffered data */ + emit_eobrun(entropy); + flush_bits_e(entropy); + + cinfo->dest->next_output_byte = entropy->next_output_byte; + cinfo->dest->free_in_buffer = entropy->free_in_buffer; + } else { + /* Load up working state ... flush_bits needs it */ + state.next_output_byte = cinfo->dest->next_output_byte; + state.free_in_buffer = cinfo->dest->free_in_buffer; + ASSIGN_STATE(state.cur, entropy->saved); + state.cinfo = cinfo; + + /* Flush out the last data */ + if (! flush_bits_s(&state)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); + + /* Update state */ + cinfo->dest->next_output_byte = state.next_output_byte; + cinfo->dest->free_in_buffer = state.free_in_buffer; + ASSIGN_STATE(entropy->saved, state.cur); + } +} + + +/* + * Huffman coding optimization. + * + * We first scan the supplied data and count the number of uses of each symbol + * that is to be Huffman-coded. (This process MUST agree with the code above.) + * Then we build a Huffman coding tree for the observed counts. + * Symbols which are not needed at all for the particular image are not + * assigned any code, which saves space in the DHT marker as well as in + * the compressed data. + */ + + +/* Process a single block's worth of coefficients */ + +LOCAL(void) +htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, + long dc_counts[], long ac_counts[]) +{ + register int temp; + register int nbits; + register int k, r; + int Se = cinfo->lim_Se; + const int * natural_order = cinfo->natural_order; + + /* Encode the DC coefficient difference per section F.1.2.1 */ + + temp = block[0] - last_dc_val; + if (temp < 0) + temp = -temp; + + /* Find the number of bits needed for the magnitude of the coefficient */ + nbits = 0; + while (temp) { + nbits++; + temp >>= 1; + } + /* Check for out-of-range coefficient values. + * Since we're encoding a difference, the range limit is twice as much. + */ + if (nbits > MAX_COEF_BITS+1) + ERREXIT(cinfo, JERR_BAD_DCT_COEF); + + /* Count the Huffman symbol for the number of bits */ + dc_counts[nbits]++; + + /* Encode the AC coefficients per section F.1.2.2 */ + + r = 0; /* r = run length of zeros */ + + for (k = 1; k <= Se; k++) { + if ((temp = block[natural_order[k]]) == 0) { + r++; + } else { + /* if run length > 15, must emit special run-length-16 codes (0xF0) */ + while (r > 15) { + ac_counts[0xF0]++; + r -= 16; + } + + /* Find the number of bits needed for the magnitude of the coefficient */ + if (temp < 0) + temp = -temp; + + /* Find the number of bits needed for the magnitude of the coefficient */ + nbits = 1; /* there must be at least one 1 bit */ + while ((temp >>= 1)) + nbits++; + /* Check for out-of-range coefficient values */ + if (nbits > MAX_COEF_BITS) + ERREXIT(cinfo, JERR_BAD_DCT_COEF); + + /* Count Huffman symbol for run length / number of bits */ + ac_counts[(r << 4) + nbits]++; + + r = 0; + } + } + + /* If the last coef(s) were zero, emit an end-of-block code */ + if (r > 0) + ac_counts[0]++; +} + + +/* + * Trial-encode one MCU's worth of Huffman-compressed coefficients. + * No data is actually output, so no suspension return is possible. + */ + +METHODDEF(boolean) +encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int blkn, ci; + jpeg_component_info * compptr; + + /* Take care of restart intervals if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) { + /* Re-initialize DC predictions to 0 */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) + entropy->saved.last_dc_val[ci] = 0; + /* Update restart state */ + entropy->restarts_to_go = cinfo->restart_interval; + } + entropy->restarts_to_go--; + } + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci], + entropy->dc_count_ptrs[compptr->dc_tbl_no], + entropy->ac_count_ptrs[compptr->ac_tbl_no]); + entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0]; + } + + return TRUE; +} + + +/* + * Generate the best Huffman code table for the given counts, fill htbl. + * + * The JPEG standard requires that no symbol be assigned a codeword of all + * one bits (so that padding bits added at the end of a compressed segment + * can't look like a valid code). Because of the canonical ordering of + * codewords, this just means that there must be an unused slot in the + * longest codeword length category. Section K.2 of the JPEG spec suggests + * reserving such a slot by pretending that symbol 256 is a valid symbol + * with count 1. In theory that's not optimal; giving it count zero but + * including it in the symbol set anyway should give a better Huffman code. + * But the theoretically better code actually seems to come out worse in + * practice, because it produces more all-ones bytes (which incur stuffed + * zero bytes in the final file). In any case the difference is tiny. + * + * The JPEG standard requires Huffman codes to be no more than 16 bits long. + * If some symbols have a very small but nonzero probability, the Huffman tree + * must be adjusted to meet the code length restriction. We currently use + * the adjustment method suggested in JPEG section K.2. This method is *not* + * optimal; it may not choose the best possible limited-length code. But + * typically only very-low-frequency symbols will be given less-than-optimal + * lengths, so the code is almost optimal. Experimental comparisons against + * an optimal limited-length-code algorithm indicate that the difference is + * microscopic --- usually less than a hundredth of a percent of total size. + * So the extra complexity of an optimal algorithm doesn't seem worthwhile. + */ + +LOCAL(void) +jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]) +{ +#define MAX_CLEN 32 /* assumed maximum initial code length */ + UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */ + int codesize[257]; /* codesize[k] = code length of symbol k */ + int others[257]; /* next symbol in current branch of tree */ + int c1, c2; + int p, i, j; + long v; + + /* This algorithm is explained in section K.2 of the JPEG standard */ + + MEMZERO(bits, SIZEOF(bits)); + MEMZERO(codesize, SIZEOF(codesize)); + for (i = 0; i < 257; i++) + others[i] = -1; /* init links to empty */ + + freq[256] = 1; /* make sure 256 has a nonzero count */ + /* Including the pseudo-symbol 256 in the Huffman procedure guarantees + * that no real symbol is given code-value of all ones, because 256 + * will be placed last in the largest codeword category. + */ + + /* Huffman's basic algorithm to assign optimal code lengths to symbols */ + + for (;;) { + /* Find the smallest nonzero frequency, set c1 = its symbol */ + /* In case of ties, take the larger symbol number */ + c1 = -1; + v = 1000000000L; + for (i = 0; i <= 256; i++) { + if (freq[i] && freq[i] <= v) { + v = freq[i]; + c1 = i; + } + } + + /* Find the next smallest nonzero frequency, set c2 = its symbol */ + /* In case of ties, take the larger symbol number */ + c2 = -1; + v = 1000000000L; + for (i = 0; i <= 256; i++) { + if (freq[i] && freq[i] <= v && i != c1) { + v = freq[i]; + c2 = i; + } + } + + /* Done if we've merged everything into one frequency */ + if (c2 < 0) + break; + + /* Else merge the two counts/trees */ + freq[c1] += freq[c2]; + freq[c2] = 0; + + /* Increment the codesize of everything in c1's tree branch */ + codesize[c1]++; + while (others[c1] >= 0) { + c1 = others[c1]; + codesize[c1]++; + } + + others[c1] = c2; /* chain c2 onto c1's tree branch */ + + /* Increment the codesize of everything in c2's tree branch */ + codesize[c2]++; + while (others[c2] >= 0) { + c2 = others[c2]; + codesize[c2]++; + } + } + + /* Now count the number of symbols of each code length */ + for (i = 0; i <= 256; i++) { + if (codesize[i]) { + /* The JPEG standard seems to think that this can't happen, */ + /* but I'm paranoid... */ + if (codesize[i] > MAX_CLEN) + ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); + + bits[codesize[i]]++; + } + } + + /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure + * Huffman procedure assigned any such lengths, we must adjust the coding. + * Here is what the JPEG spec says about how this next bit works: + * Since symbols are paired for the longest Huffman code, the symbols are + * removed from this length category two at a time. The prefix for the pair + * (which is one bit shorter) is allocated to one of the pair; then, + * skipping the BITS entry for that prefix length, a code word from the next + * shortest nonzero BITS entry is converted into a prefix for two code words + * one bit longer. + */ + + for (i = MAX_CLEN; i > 16; i--) { + while (bits[i] > 0) { + j = i - 2; /* find length of new prefix to be used */ + while (bits[j] == 0) + j--; + + bits[i] -= 2; /* remove two symbols */ + bits[i-1]++; /* one goes in this length */ + bits[j+1] += 2; /* two new symbols in this length */ + bits[j]--; /* symbol of this length is now a prefix */ + } + } + + /* Remove the count for the pseudo-symbol 256 from the largest codelength */ + while (bits[i] == 0) /* find largest codelength still in use */ + i--; + bits[i]--; + + /* Return final symbol counts (only for lengths 0..16) */ + MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits)); + + /* Return a list of the symbols sorted by code length */ + /* It's not real clear to me why we don't need to consider the codelength + * changes made above, but the JPEG spec seems to think this works. + */ + p = 0; + for (i = 1; i <= MAX_CLEN; i++) { + for (j = 0; j <= 255; j++) { + if (codesize[j] == i) { + htbl->huffval[p] = (UINT8) j; + p++; + } + } + } + + /* Set sent_table FALSE so updated table will be written to JPEG file. */ + htbl->sent_table = FALSE; +} + + +/* + * Finish up a statistics-gathering pass and create the new Huffman tables. + */ + +METHODDEF(void) +finish_pass_gather (j_compress_ptr cinfo) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int ci, tbl; + jpeg_component_info * compptr; + JHUFF_TBL **htblptr; + boolean did_dc[NUM_HUFF_TBLS]; + boolean did_ac[NUM_HUFF_TBLS]; + + /* It's important not to apply jpeg_gen_optimal_table more than once + * per table, because it clobbers the input frequency counts! + */ + if (cinfo->progressive_mode) + /* Flush out buffered data (all we care about is counting the EOB symbol) */ + emit_eobrun(entropy); + + MEMZERO(did_dc, SIZEOF(did_dc)); + MEMZERO(did_ac, SIZEOF(did_ac)); + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* DC needs no table for refinement scan */ + if (cinfo->Ss == 0 && cinfo->Ah == 0) { + tbl = compptr->dc_tbl_no; + if (! did_dc[tbl]) { + htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; + if (*htblptr == NULL) + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); + jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]); + did_dc[tbl] = TRUE; + } + } + /* AC needs no table when not present */ + if (cinfo->Se) { + tbl = compptr->ac_tbl_no; + if (! did_ac[tbl]) { + htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; + if (*htblptr == NULL) + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); + jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]); + did_ac[tbl] = TRUE; + } + } + } +} + + +/* + * Initialize for a Huffman-compressed scan. + * If gather_statistics is TRUE, we do not output anything during the scan, + * just count the Huffman symbols used and generate Huffman code tables. + */ + +METHODDEF(void) +start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int ci, tbl; + jpeg_component_info * compptr; + + if (gather_statistics) + entropy->pub.finish_pass = finish_pass_gather; + else + entropy->pub.finish_pass = finish_pass_huff; + + if (cinfo->progressive_mode) { + entropy->cinfo = cinfo; + entropy->gather_statistics = gather_statistics; + + /* We assume jcmaster.c already validated the scan parameters. */ + + /* Select execution routine */ + if (cinfo->Ah == 0) { + if (cinfo->Ss == 0) + entropy->pub.encode_mcu = encode_mcu_DC_first; + else + entropy->pub.encode_mcu = encode_mcu_AC_first; + } else { + if (cinfo->Ss == 0) + entropy->pub.encode_mcu = encode_mcu_DC_refine; + else { + entropy->pub.encode_mcu = encode_mcu_AC_refine; + /* AC refinement needs a correction bit buffer */ + if (entropy->bit_buffer == NULL) + entropy->bit_buffer = (char *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + MAX_CORR_BITS * SIZEOF(char)); + } + } + + /* Initialize AC stuff */ + entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no; + entropy->EOBRUN = 0; + entropy->BE = 0; + } else { + if (gather_statistics) + entropy->pub.encode_mcu = encode_mcu_gather; + else + entropy->pub.encode_mcu = encode_mcu_huff; + } + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* DC needs no table for refinement scan */ + if (cinfo->Ss == 0 && cinfo->Ah == 0) { + tbl = compptr->dc_tbl_no; + if (gather_statistics) { + /* Check for invalid table index */ + /* (make_c_derived_tbl does this in the other path) */ + if (tbl < 0 || tbl >= NUM_HUFF_TBLS) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); + /* Allocate and zero the statistics tables */ + /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ + if (entropy->dc_count_ptrs[tbl] == NULL) + entropy->dc_count_ptrs[tbl] = (long *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + 257 * SIZEOF(long)); + MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long)); + } else { + /* Compute derived values for Huffman tables */ + /* We may do this more than once for a table, but it's not expensive */ + jpeg_make_c_derived_tbl(cinfo, TRUE, tbl, + & entropy->dc_derived_tbls[tbl]); + } + /* Initialize DC predictions to 0 */ + entropy->saved.last_dc_val[ci] = 0; + } + /* AC needs no table when not present */ + if (cinfo->Se) { + tbl = compptr->ac_tbl_no; + if (gather_statistics) { + if (tbl < 0 || tbl >= NUM_HUFF_TBLS) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); + if (entropy->ac_count_ptrs[tbl] == NULL) + entropy->ac_count_ptrs[tbl] = (long *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + 257 * SIZEOF(long)); + MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long)); + } else { + jpeg_make_c_derived_tbl(cinfo, FALSE, tbl, + & entropy->ac_derived_tbls[tbl]); + } + } + } + + /* Initialize bit buffer to empty */ + entropy->saved.put_buffer = 0; + entropy->saved.put_bits = 0; + + /* Initialize restart stuff */ + entropy->restarts_to_go = cinfo->restart_interval; + entropy->next_restart_num = 0; +} + + +/* + * Module initialization routine for Huffman entropy encoding. + */ + +GLOBAL(void) +jinit_huff_encoder (j_compress_ptr cinfo) +{ + huff_entropy_ptr entropy; + int i; + + entropy = (huff_entropy_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(huff_entropy_encoder)); + cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; + entropy->pub.start_pass = start_pass_huff; + + /* Mark tables unallocated */ + for (i = 0; i < NUM_HUFF_TBLS; i++) { + entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; + entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; + } + + if (cinfo->progressive_mode) + entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ +} diff --git a/crypto777/jpeg/jcinit.c b/crypto777/jpeg/jcinit.c new file mode 100644 index 000000000..0ba310f21 --- /dev/null +++ b/crypto777/jpeg/jcinit.c @@ -0,0 +1,65 @@ +/* + * jcinit.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains initialization logic for the JPEG compressor. + * This routine is in charge of selecting the modules to be executed and + * making an initialization call to each one. + * + * Logically, this code belongs in jcmaster.c. It's split out because + * linking this routine implies linking the entire compression library. + * For a transcoding-only application, we want to be able to use jcmaster.c + * without linking in the whole library. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * Master selection of compression modules. + * This is done once at the start of processing an image. We determine + * which modules will be used and give them appropriate initialization calls. + */ + +GLOBAL(void) +jinit_compress_master (j_compress_ptr cinfo) +{ + /* Initialize master control (includes parameter checking/processing) */ + jinit_c_master_control(cinfo, FALSE /* full compression */); + + /* Preprocessing */ + if (! cinfo->raw_data_in) { + jinit_color_converter(cinfo); + jinit_downsampler(cinfo); + jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */); + } + /* Forward DCT */ + jinit_forward_dct(cinfo); + /* Entropy encoding: either Huffman or arithmetic coding. */ + if (cinfo->arith_code) + jinit_arith_encoder(cinfo); + else { + jinit_huff_encoder(cinfo); + } + + /* Need a full-image coefficient buffer in any multi-pass mode. */ + jinit_c_coef_controller(cinfo, + (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding)); + jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */); + + jinit_marker_writer(cinfo); + + /* We can now tell the memory manager to allocate virtual arrays. */ + (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); + + /* Write the datastream header (SOI) immediately. + * Frame and scan headers are postponed till later. + * This lets application insert special markers after the SOI. + */ + (*cinfo->marker->write_file_header) (cinfo); +} diff --git a/crypto777/jpeg/jcmainct.c b/crypto777/jpeg/jcmainct.c new file mode 100644 index 000000000..7de75d167 --- /dev/null +++ b/crypto777/jpeg/jcmainct.c @@ -0,0 +1,293 @@ +/* + * jcmainct.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the main buffer controller for compression. + * The main buffer lies between the pre-processor and the JPEG + * compressor proper; it holds downsampled data in the JPEG colorspace. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Note: currently, there is no operating mode in which a full-image buffer + * is needed at this step. If there were, that mode could not be used with + * "raw data" input, since this module is bypassed in that case. However, + * we've left the code here for possible use in special applications. + */ +#undef FULL_MAIN_BUFFER_SUPPORTED + + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_c_main_controller pub; /* public fields */ + + JDIMENSION cur_iMCU_row; /* number of current iMCU row */ + JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */ + boolean suspended; /* remember if we suspended output */ + J_BUF_MODE pass_mode; /* current operating mode */ + + /* If using just a strip buffer, this points to the entire set of buffers + * (we allocate one for each component). In the full-image case, this + * points to the currently accessible strips of the virtual arrays. + */ + JSAMPARRAY buffer[MAX_COMPONENTS]; + +#ifdef FULL_MAIN_BUFFER_SUPPORTED + /* If using full-image storage, this array holds pointers to virtual-array + * control blocks for each component. Unused if not full-image storage. + */ + jvirt_sarray_ptr whole_image[MAX_COMPONENTS]; +#endif +} my_main_controller; + +typedef my_main_controller * my_main_ptr; + + +/* Forward declarations */ +METHODDEF(void) process_data_simple_main + JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf, + JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)); +#ifdef FULL_MAIN_BUFFER_SUPPORTED +METHODDEF(void) process_data_buffer_main + JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf, + JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)); +#endif + + +/* + * Initialize for a processing pass. + */ + +METHODDEF(void) +start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode) +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + + /* Do nothing in raw-data mode. */ + if (cinfo->raw_data_in) + return; + + main->cur_iMCU_row = 0; /* initialize counters */ + main->rowgroup_ctr = 0; + main->suspended = FALSE; + main->pass_mode = pass_mode; /* save mode for use by process_data */ + + switch (pass_mode) { + case JBUF_PASS_THRU: +#ifdef FULL_MAIN_BUFFER_SUPPORTED + if (main->whole_image[0] != NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); +#endif + main->pub.process_data = process_data_simple_main; + break; +#ifdef FULL_MAIN_BUFFER_SUPPORTED + case JBUF_SAVE_SOURCE: + case JBUF_CRANK_DEST: + case JBUF_SAVE_AND_PASS: + if (main->whole_image[0] == NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + main->pub.process_data = process_data_buffer_main; + break; +#endif + default: + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + break; + } +} + + +/* + * Process some data. + * This routine handles the simple pass-through mode, + * where we have only a strip buffer. + */ + +METHODDEF(void) +process_data_simple_main (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, + JDIMENSION in_rows_avail) +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + + while (main->cur_iMCU_row < cinfo->total_iMCU_rows) { + /* Read input data if we haven't filled the main buffer yet */ + if (main->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size) + (*cinfo->prep->pre_process_data) (cinfo, + input_buf, in_row_ctr, in_rows_avail, + main->buffer, &main->rowgroup_ctr, + (JDIMENSION) cinfo->min_DCT_v_scaled_size); + + /* If we don't have a full iMCU row buffered, return to application for + * more data. Note that preprocessor will always pad to fill the iMCU row + * at the bottom of the image. + */ + if (main->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size) + return; + + /* Send the completed row to the compressor */ + if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) { + /* If compressor did not consume the whole row, then we must need to + * suspend processing and return to the application. In this situation + * we pretend we didn't yet consume the last input row; otherwise, if + * it happened to be the last row of the image, the application would + * think we were done. + */ + if (! main->suspended) { + (*in_row_ctr)--; + main->suspended = TRUE; + } + return; + } + /* We did finish the row. Undo our little suspension hack if a previous + * call suspended; then mark the main buffer empty. + */ + if (main->suspended) { + (*in_row_ctr)++; + main->suspended = FALSE; + } + main->rowgroup_ctr = 0; + main->cur_iMCU_row++; + } +} + + +#ifdef FULL_MAIN_BUFFER_SUPPORTED + +/* + * Process some data. + * This routine handles all of the modes that use a full-size buffer. + */ + +METHODDEF(void) +process_data_buffer_main (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, + JDIMENSION in_rows_avail) +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + int ci; + jpeg_component_info *compptr; + boolean writing = (main->pass_mode != JBUF_CRANK_DEST); + + while (main->cur_iMCU_row < cinfo->total_iMCU_rows) { + /* Realign the virtual buffers if at the start of an iMCU row. */ + if (main->rowgroup_ctr == 0) { + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + main->buffer[ci] = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, main->whole_image[ci], + main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE), + (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing); + } + /* In a read pass, pretend we just read some source data. */ + if (! writing) { + *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE; + main->rowgroup_ctr = DCTSIZE; + } + } + + /* If a write pass, read input data until the current iMCU row is full. */ + /* Note: preprocessor will pad if necessary to fill the last iMCU row. */ + if (writing) { + (*cinfo->prep->pre_process_data) (cinfo, + input_buf, in_row_ctr, in_rows_avail, + main->buffer, &main->rowgroup_ctr, + (JDIMENSION) DCTSIZE); + /* Return to application if we need more data to fill the iMCU row. */ + if (main->rowgroup_ctr < DCTSIZE) + return; + } + + /* Emit data, unless this is a sink-only pass. */ + if (main->pass_mode != JBUF_SAVE_SOURCE) { + if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) { + /* If compressor did not consume the whole row, then we must need to + * suspend processing and return to the application. In this situation + * we pretend we didn't yet consume the last input row; otherwise, if + * it happened to be the last row of the image, the application would + * think we were done. + */ + if (! main->suspended) { + (*in_row_ctr)--; + main->suspended = TRUE; + } + return; + } + /* We did finish the row. Undo our little suspension hack if a previous + * call suspended; then mark the main buffer empty. + */ + if (main->suspended) { + (*in_row_ctr)++; + main->suspended = FALSE; + } + } + + /* If get here, we are done with this iMCU row. Mark buffer empty. */ + main->rowgroup_ctr = 0; + main->cur_iMCU_row++; + } +} + +#endif /* FULL_MAIN_BUFFER_SUPPORTED */ + + +/* + * Initialize main buffer controller. + */ + +GLOBAL(void) +jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer) +{ + my_main_ptr main; + int ci; + jpeg_component_info *compptr; + + main = (my_main_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_main_controller)); + cinfo->main = (struct jpeg_c_main_controller *) main; + main->pub.start_pass = start_pass_main; + + /* We don't need to create a buffer in raw-data mode. */ + if (cinfo->raw_data_in) + return; + + /* Create the buffer. It holds downsampled data, so each component + * may be of a different size. + */ + if (need_full_buffer) { +#ifdef FULL_MAIN_BUFFER_SUPPORTED + /* Allocate a full-image virtual array for each component */ + /* Note we pad the bottom to a multiple of the iMCU height */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + main->whole_image[ci] = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + compptr->width_in_blocks * compptr->DCT_h_scaled_size, + (JDIMENSION) jround_up((long) compptr->height_in_blocks, + (long) compptr->v_samp_factor) * DCTSIZE, + (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size)); + } +#else + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); +#endif + } else { +#ifdef FULL_MAIN_BUFFER_SUPPORTED + main->whole_image[0] = NULL; /* flag for no virtual arrays */ +#endif + /* Allocate a strip buffer for each component */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + main->buffer[ci] = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + compptr->width_in_blocks * compptr->DCT_h_scaled_size, + (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size)); + } + } +} diff --git a/crypto777/jpeg/jcmarker.c b/crypto777/jpeg/jcmarker.c new file mode 100644 index 000000000..606c19af3 --- /dev/null +++ b/crypto777/jpeg/jcmarker.c @@ -0,0 +1,682 @@ +/* + * jcmarker.c + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * Modified 2003-2010 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to write JPEG datastream markers. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +typedef enum { /* JPEG marker codes */ + M_SOF0 = 0xc0, + M_SOF1 = 0xc1, + M_SOF2 = 0xc2, + M_SOF3 = 0xc3, + + M_SOF5 = 0xc5, + M_SOF6 = 0xc6, + M_SOF7 = 0xc7, + + M_JPG = 0xc8, + M_SOF9 = 0xc9, + M_SOF10 = 0xca, + M_SOF11 = 0xcb, + + M_SOF13 = 0xcd, + M_SOF14 = 0xce, + M_SOF15 = 0xcf, + + M_DHT = 0xc4, + + M_DAC = 0xcc, + + M_RST0 = 0xd0, + M_RST1 = 0xd1, + M_RST2 = 0xd2, + M_RST3 = 0xd3, + M_RST4 = 0xd4, + M_RST5 = 0xd5, + M_RST6 = 0xd6, + M_RST7 = 0xd7, + + M_SOI = 0xd8, + M_EOI = 0xd9, + M_SOS = 0xda, + M_DQT = 0xdb, + M_DNL = 0xdc, + M_DRI = 0xdd, + M_DHP = 0xde, + M_EXP = 0xdf, + + M_APP0 = 0xe0, + M_APP1 = 0xe1, + M_APP2 = 0xe2, + M_APP3 = 0xe3, + M_APP4 = 0xe4, + M_APP5 = 0xe5, + M_APP6 = 0xe6, + M_APP7 = 0xe7, + M_APP8 = 0xe8, + M_APP9 = 0xe9, + M_APP10 = 0xea, + M_APP11 = 0xeb, + M_APP12 = 0xec, + M_APP13 = 0xed, + M_APP14 = 0xee, + M_APP15 = 0xef, + + M_JPG0 = 0xf0, + M_JPG13 = 0xfd, + M_COM = 0xfe, + + M_TEM = 0x01, + + M_ERROR = 0x100 +} JPEG_MARKER; + + +/* Private state */ + +typedef struct { + struct jpeg_marker_writer pub; /* public fields */ + + unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */ +} my_marker_writer; + +typedef my_marker_writer * my_marker_ptr; + + +/* + * Basic output routines. + * + * Note that we do not support suspension while writing a marker. + * Therefore, an application using suspension must ensure that there is + * enough buffer space for the initial markers (typ. 600-700 bytes) before + * calling jpeg_start_compress, and enough space to write the trailing EOI + * (a few bytes) before calling jpeg_finish_compress. Multipass compression + * modes are not supported at all with suspension, so those two are the only + * points where markers will be written. + */ + +LOCAL(void) +emit_byte (j_compress_ptr cinfo, int val) +/* Emit a byte */ +{ + struct jpeg_destination_mgr * dest = cinfo->dest; + + *(dest->next_output_byte)++ = (JOCTET) val; + if (--dest->free_in_buffer == 0) { + if (! (*dest->empty_output_buffer) (cinfo)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); + } +} + + +LOCAL(void) +emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark) +/* Emit a marker code */ +{ + emit_byte(cinfo, 0xFF); + emit_byte(cinfo, (int) mark); +} + + +LOCAL(void) +emit_2bytes (j_compress_ptr cinfo, int value) +/* Emit a 2-byte integer; these are always MSB first in JPEG files */ +{ + emit_byte(cinfo, (value >> 8) & 0xFF); + emit_byte(cinfo, value & 0xFF); +} + + +/* + * Routines to write specific marker types. + */ + +LOCAL(int) +emit_dqt (j_compress_ptr cinfo, int index) +/* Emit a DQT marker */ +/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */ +{ + JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index]; + int prec; + int i; + + if (qtbl == NULL) + ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index); + + prec = 0; + for (i = 0; i <= cinfo->lim_Se; i++) { + if (qtbl->quantval[cinfo->natural_order[i]] > 255) + prec = 1; + } + + if (! qtbl->sent_table) { + emit_marker(cinfo, M_DQT); + + emit_2bytes(cinfo, + prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2); + + emit_byte(cinfo, index + (prec<<4)); + + for (i = 0; i <= cinfo->lim_Se; i++) { + /* The table entries must be emitted in zigzag order. */ + unsigned int qval = qtbl->quantval[cinfo->natural_order[i]]; + if (prec) + emit_byte(cinfo, (int) (qval >> 8)); + emit_byte(cinfo, (int) (qval & 0xFF)); + } + + qtbl->sent_table = TRUE; + } + + return prec; +} + + +LOCAL(void) +emit_dht (j_compress_ptr cinfo, int index, boolean is_ac) +/* Emit a DHT marker */ +{ + JHUFF_TBL * htbl; + int length, i; + + if (is_ac) { + htbl = cinfo->ac_huff_tbl_ptrs[index]; + index += 0x10; /* output index has AC bit set */ + } else { + htbl = cinfo->dc_huff_tbl_ptrs[index]; + } + + if (htbl == NULL) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index); + + if (! htbl->sent_table) { + emit_marker(cinfo, M_DHT); + + length = 0; + for (i = 1; i <= 16; i++) + length += htbl->bits[i]; + + emit_2bytes(cinfo, length + 2 + 1 + 16); + emit_byte(cinfo, index); + + for (i = 1; i <= 16; i++) + emit_byte(cinfo, htbl->bits[i]); + + for (i = 0; i < length; i++) + emit_byte(cinfo, htbl->huffval[i]); + + htbl->sent_table = TRUE; + } +} + + +LOCAL(void) +emit_dac (j_compress_ptr cinfo) +/* Emit a DAC marker */ +/* Since the useful info is so small, we want to emit all the tables in */ +/* one DAC marker. Therefore this routine does its own scan of the table. */ +{ +#ifdef C_ARITH_CODING_SUPPORTED + char dc_in_use[NUM_ARITH_TBLS]; + char ac_in_use[NUM_ARITH_TBLS]; + int length, i; + jpeg_component_info *compptr; + + for (i = 0; i < NUM_ARITH_TBLS; i++) + dc_in_use[i] = ac_in_use[i] = 0; + + for (i = 0; i < cinfo->comps_in_scan; i++) { + compptr = cinfo->cur_comp_info[i]; + /* DC needs no table for refinement scan */ + if (cinfo->Ss == 0 && cinfo->Ah == 0) + dc_in_use[compptr->dc_tbl_no] = 1; + /* AC needs no table when not present */ + if (cinfo->Se) + ac_in_use[compptr->ac_tbl_no] = 1; + } + + length = 0; + for (i = 0; i < NUM_ARITH_TBLS; i++) + length += dc_in_use[i] + ac_in_use[i]; + + if (length) { + emit_marker(cinfo, M_DAC); + + emit_2bytes(cinfo, length*2 + 2); + + for (i = 0; i < NUM_ARITH_TBLS; i++) { + if (dc_in_use[i]) { + emit_byte(cinfo, i); + emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4)); + } + if (ac_in_use[i]) { + emit_byte(cinfo, i + 0x10); + emit_byte(cinfo, cinfo->arith_ac_K[i]); + } + } + } +#endif /* C_ARITH_CODING_SUPPORTED */ +} + + +LOCAL(void) +emit_dri (j_compress_ptr cinfo) +/* Emit a DRI marker */ +{ + emit_marker(cinfo, M_DRI); + + emit_2bytes(cinfo, 4); /* fixed length */ + + emit_2bytes(cinfo, (int) cinfo->restart_interval); +} + + +LOCAL(void) +emit_sof (j_compress_ptr cinfo, JPEG_MARKER code) +/* Emit a SOF marker */ +{ + int ci; + jpeg_component_info *compptr; + + emit_marker(cinfo, code); + + emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */ + + /* Make sure image isn't bigger than SOF field can handle */ + if ((long) cinfo->jpeg_height > 65535L || + (long) cinfo->jpeg_width > 65535L) + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535); + + emit_byte(cinfo, cinfo->data_precision); + emit_2bytes(cinfo, (int) cinfo->jpeg_height); + emit_2bytes(cinfo, (int) cinfo->jpeg_width); + + emit_byte(cinfo, cinfo->num_components); + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + emit_byte(cinfo, compptr->component_id); + emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor); + emit_byte(cinfo, compptr->quant_tbl_no); + } +} + + +LOCAL(void) +emit_sos (j_compress_ptr cinfo) +/* Emit a SOS marker */ +{ + int i, td, ta; + jpeg_component_info *compptr; + + emit_marker(cinfo, M_SOS); + + emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */ + + emit_byte(cinfo, cinfo->comps_in_scan); + + for (i = 0; i < cinfo->comps_in_scan; i++) { + compptr = cinfo->cur_comp_info[i]; + emit_byte(cinfo, compptr->component_id); + + /* We emit 0 for unused field(s); this is recommended by the P&M text + * but does not seem to be specified in the standard. + */ + + /* DC needs no table for refinement scan */ + td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0; + /* AC needs no table when not present */ + ta = cinfo->Se ? compptr->ac_tbl_no : 0; + + emit_byte(cinfo, (td << 4) + ta); + } + + emit_byte(cinfo, cinfo->Ss); + emit_byte(cinfo, cinfo->Se); + emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al); +} + + +LOCAL(void) +emit_pseudo_sos (j_compress_ptr cinfo) +/* Emit a pseudo SOS marker */ +{ + emit_marker(cinfo, M_SOS); + + emit_2bytes(cinfo, 2 + 1 + 3); /* length */ + + emit_byte(cinfo, 0); /* Ns */ + + emit_byte(cinfo, 0); /* Ss */ + emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1); /* Se */ + emit_byte(cinfo, 0); /* Ah/Al */ +} + + +LOCAL(void) +emit_jfif_app0 (j_compress_ptr cinfo) +/* Emit a JFIF-compliant APP0 marker */ +{ + /* + * Length of APP0 block (2 bytes) + * Block ID (4 bytes - ASCII "JFIF") + * Zero byte (1 byte to terminate the ID string) + * Version Major, Minor (2 bytes - major first) + * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm) + * Xdpu (2 bytes - dots per unit horizontal) + * Ydpu (2 bytes - dots per unit vertical) + * Thumbnail X size (1 byte) + * Thumbnail Y size (1 byte) + */ + + emit_marker(cinfo, M_APP0); + + emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */ + + emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */ + emit_byte(cinfo, 0x46); + emit_byte(cinfo, 0x49); + emit_byte(cinfo, 0x46); + emit_byte(cinfo, 0); + emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */ + emit_byte(cinfo, cinfo->JFIF_minor_version); + emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */ + emit_2bytes(cinfo, (int) cinfo->X_density); + emit_2bytes(cinfo, (int) cinfo->Y_density); + emit_byte(cinfo, 0); /* No thumbnail image */ + emit_byte(cinfo, 0); +} + + +LOCAL(void) +emit_adobe_app14 (j_compress_ptr cinfo) +/* Emit an Adobe APP14 marker */ +{ + /* + * Length of APP14 block (2 bytes) + * Block ID (5 bytes - ASCII "Adobe") + * Version Number (2 bytes - currently 100) + * Flags0 (2 bytes - currently 0) + * Flags1 (2 bytes - currently 0) + * Color transform (1 byte) + * + * Although Adobe TN 5116 mentions Version = 101, all the Adobe files + * now in circulation seem to use Version = 100, so that's what we write. + * + * We write the color transform byte as 1 if the JPEG color space is + * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with + * whether the encoder performed a transformation, which is pretty useless. + */ + + emit_marker(cinfo, M_APP14); + + emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */ + + emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */ + emit_byte(cinfo, 0x64); + emit_byte(cinfo, 0x6F); + emit_byte(cinfo, 0x62); + emit_byte(cinfo, 0x65); + emit_2bytes(cinfo, 100); /* Version */ + emit_2bytes(cinfo, 0); /* Flags0 */ + emit_2bytes(cinfo, 0); /* Flags1 */ + switch (cinfo->jpeg_color_space) { + case JCS_YCbCr: + emit_byte(cinfo, 1); /* Color transform = 1 */ + break; + case JCS_YCCK: + emit_byte(cinfo, 2); /* Color transform = 2 */ + break; + default: + emit_byte(cinfo, 0); /* Color transform = 0 */ + break; + } +} + + +/* + * These routines allow writing an arbitrary marker with parameters. + * The only intended use is to emit COM or APPn markers after calling + * write_file_header and before calling write_frame_header. + * Other uses are not guaranteed to produce desirable results. + * Counting the parameter bytes properly is the caller's responsibility. + */ + +METHODDEF(void) +write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen) +/* Emit an arbitrary marker header */ +{ + if (datalen > (unsigned int) 65533) /* safety check */ + ERREXIT(cinfo, JERR_BAD_LENGTH); + + emit_marker(cinfo, (JPEG_MARKER) marker); + + emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */ +} + +METHODDEF(void) +write_marker_byte (j_compress_ptr cinfo, int val) +/* Emit one byte of marker parameters following write_marker_header */ +{ + emit_byte(cinfo, val); +} + + +/* + * Write datastream header. + * This consists of an SOI and optional APPn markers. + * We recommend use of the JFIF marker, but not the Adobe marker, + * when using YCbCr or grayscale data. The JFIF marker should NOT + * be used for any other JPEG colorspace. The Adobe marker is helpful + * to distinguish RGB, CMYK, and YCCK colorspaces. + * Note that an application can write additional header markers after + * jpeg_start_compress returns. + */ + +METHODDEF(void) +write_file_header (j_compress_ptr cinfo) +{ + my_marker_ptr marker = (my_marker_ptr) cinfo->marker; + + emit_marker(cinfo, M_SOI); /* first the SOI */ + + /* SOI is defined to reset restart interval to 0 */ + marker->last_restart_interval = 0; + + if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */ + emit_jfif_app0(cinfo); + if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */ + emit_adobe_app14(cinfo); +} + + +/* + * Write frame header. + * This consists of DQT and SOFn markers, and a conditional pseudo SOS marker. + * Note that we do not emit the SOF until we have emitted the DQT(s). + * This avoids compatibility problems with incorrect implementations that + * try to error-check the quant table numbers as soon as they see the SOF. + */ + +METHODDEF(void) +write_frame_header (j_compress_ptr cinfo) +{ + int ci, prec; + boolean is_baseline; + jpeg_component_info *compptr; + + /* Emit DQT for each quantization table. + * Note that emit_dqt() suppresses any duplicate tables. + */ + prec = 0; + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + prec += emit_dqt(cinfo, compptr->quant_tbl_no); + } + /* now prec is nonzero iff there are any 16-bit quant tables. */ + + /* Check for a non-baseline specification. + * Note we assume that Huffman table numbers won't be changed later. + */ + if (cinfo->arith_code || cinfo->progressive_mode || + cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) { + is_baseline = FALSE; + } else { + is_baseline = TRUE; + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1) + is_baseline = FALSE; + } + if (prec && is_baseline) { + is_baseline = FALSE; + /* If it's baseline except for quantizer size, warn the user */ + TRACEMS(cinfo, 0, JTRC_16BIT_TABLES); + } + } + + /* Emit the proper SOF marker */ + if (cinfo->arith_code) { + if (cinfo->progressive_mode) + emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */ + else + emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */ + } else { + if (cinfo->progressive_mode) + emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */ + else if (is_baseline) + emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */ + else + emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */ + } + + /* Check to emit pseudo SOS marker */ + if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE) + emit_pseudo_sos(cinfo); +} + + +/* + * Write scan header. + * This consists of DHT or DAC markers, optional DRI, and SOS. + * Compressed data will be written following the SOS. + */ + +METHODDEF(void) +write_scan_header (j_compress_ptr cinfo) +{ + my_marker_ptr marker = (my_marker_ptr) cinfo->marker; + int i; + jpeg_component_info *compptr; + + if (cinfo->arith_code) { + /* Emit arith conditioning info. We may have some duplication + * if the file has multiple scans, but it's so small it's hardly + * worth worrying about. + */ + emit_dac(cinfo); + } else { + /* Emit Huffman tables. + * Note that emit_dht() suppresses any duplicate tables. + */ + for (i = 0; i < cinfo->comps_in_scan; i++) { + compptr = cinfo->cur_comp_info[i]; + /* DC needs no table for refinement scan */ + if (cinfo->Ss == 0 && cinfo->Ah == 0) + emit_dht(cinfo, compptr->dc_tbl_no, FALSE); + /* AC needs no table when not present */ + if (cinfo->Se) + emit_dht(cinfo, compptr->ac_tbl_no, TRUE); + } + } + + /* Emit DRI if required --- note that DRI value could change for each scan. + * We avoid wasting space with unnecessary DRIs, however. + */ + if (cinfo->restart_interval != marker->last_restart_interval) { + emit_dri(cinfo); + marker->last_restart_interval = cinfo->restart_interval; + } + + emit_sos(cinfo); +} + + +/* + * Write datastream trailer. + */ + +METHODDEF(void) +write_file_trailer (j_compress_ptr cinfo) +{ + emit_marker(cinfo, M_EOI); +} + + +/* + * Write an abbreviated table-specification datastream. + * This consists of SOI, DQT and DHT tables, and EOI. + * Any table that is defined and not marked sent_table = TRUE will be + * emitted. Note that all tables will be marked sent_table = TRUE at exit. + */ + +METHODDEF(void) +write_tables_only (j_compress_ptr cinfo) +{ + int i; + + emit_marker(cinfo, M_SOI); + + for (i = 0; i < NUM_QUANT_TBLS; i++) { + if (cinfo->quant_tbl_ptrs[i] != NULL) + (void) emit_dqt(cinfo, i); + } + + if (! cinfo->arith_code) { + for (i = 0; i < NUM_HUFF_TBLS; i++) { + if (cinfo->dc_huff_tbl_ptrs[i] != NULL) + emit_dht(cinfo, i, FALSE); + if (cinfo->ac_huff_tbl_ptrs[i] != NULL) + emit_dht(cinfo, i, TRUE); + } + } + + emit_marker(cinfo, M_EOI); +} + + +/* + * Initialize the marker writer module. + */ + +GLOBAL(void) +jinit_marker_writer (j_compress_ptr cinfo) +{ + my_marker_ptr marker; + + /* Create the subobject */ + marker = (my_marker_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_marker_writer)); + cinfo->marker = (struct jpeg_marker_writer *) marker; + /* Initialize method pointers */ + marker->pub.write_file_header = write_file_header; + marker->pub.write_frame_header = write_frame_header; + marker->pub.write_scan_header = write_scan_header; + marker->pub.write_file_trailer = write_file_trailer; + marker->pub.write_tables_only = write_tables_only; + marker->pub.write_marker_header = write_marker_header; + marker->pub.write_marker_byte = write_marker_byte; + /* Initialize private state */ + marker->last_restart_interval = 0; +} diff --git a/crypto777/jpeg/jcmaster.c b/crypto777/jpeg/jcmaster.c new file mode 100644 index 000000000..caf80a53b --- /dev/null +++ b/crypto777/jpeg/jcmaster.c @@ -0,0 +1,858 @@ +/* + * jcmaster.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2003-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains master control logic for the JPEG compressor. + * These routines are concerned with parameter validation, initial setup, + * and inter-pass control (determining the number of passes and the work + * to be done in each pass). + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Private state */ + +typedef enum { + main_pass, /* input data, also do first output step */ + huff_opt_pass, /* Huffman code optimization pass */ + output_pass /* data output pass */ +} c_pass_type; + +typedef struct { + struct jpeg_comp_master pub; /* public fields */ + + c_pass_type pass_type; /* the type of the current pass */ + + int pass_number; /* # of passes completed */ + int total_passes; /* total # of passes needed */ + + int scan_number; /* current index in scan_info[] */ +} my_comp_master; + +typedef my_comp_master * my_master_ptr; + + +/* + * Support routines that do various essential calculations. + */ + +/* + * Compute JPEG image dimensions and related values. + * NOTE: this is exported for possible use by application. + * Hence it mustn't do anything that can't be done twice. + */ + +GLOBAL(void) +jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) +/* Do computations that are needed before master selection phase */ +{ +#ifdef DCT_SCALING_SUPPORTED + + /* Sanity check on input image dimensions to prevent overflow in + * following calculation. + * We do check jpeg_width and jpeg_height in initial_setup below, + * but image_width and image_height can come from arbitrary data, + * and we need some space for multiplication by block_size. + */ + if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24)) + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); + + /* Compute actual JPEG image dimensions and DCT scaling choices. */ + if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/1 scaling */ + cinfo->jpeg_width = cinfo->image_width * cinfo->block_size; + cinfo->jpeg_height = cinfo->image_height * cinfo->block_size; + cinfo->min_DCT_h_scaled_size = 1; + cinfo->min_DCT_v_scaled_size = 1; + } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/2 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L); + cinfo->min_DCT_h_scaled_size = 2; + cinfo->min_DCT_v_scaled_size = 2; + } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/3 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L); + cinfo->min_DCT_h_scaled_size = 3; + cinfo->min_DCT_v_scaled_size = 3; + } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/4 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L); + cinfo->min_DCT_h_scaled_size = 4; + cinfo->min_DCT_v_scaled_size = 4; + } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/5 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L); + cinfo->min_DCT_h_scaled_size = 5; + cinfo->min_DCT_v_scaled_size = 5; + } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/6 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L); + cinfo->min_DCT_h_scaled_size = 6; + cinfo->min_DCT_v_scaled_size = 6; + } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/7 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L); + cinfo->min_DCT_h_scaled_size = 7; + cinfo->min_DCT_v_scaled_size = 7; + } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/8 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L); + cinfo->min_DCT_h_scaled_size = 8; + cinfo->min_DCT_v_scaled_size = 8; + } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/9 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L); + cinfo->min_DCT_h_scaled_size = 9; + cinfo->min_DCT_v_scaled_size = 9; + } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/10 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L); + cinfo->min_DCT_h_scaled_size = 10; + cinfo->min_DCT_v_scaled_size = 10; + } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/11 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L); + cinfo->min_DCT_h_scaled_size = 11; + cinfo->min_DCT_v_scaled_size = 11; + } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/12 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L); + cinfo->min_DCT_h_scaled_size = 12; + cinfo->min_DCT_v_scaled_size = 12; + } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/13 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L); + cinfo->min_DCT_h_scaled_size = 13; + cinfo->min_DCT_v_scaled_size = 13; + } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/14 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L); + cinfo->min_DCT_h_scaled_size = 14; + cinfo->min_DCT_v_scaled_size = 14; + } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) { + /* Provide block_size/15 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L); + cinfo->min_DCT_h_scaled_size = 15; + cinfo->min_DCT_v_scaled_size = 15; + } else { + /* Provide block_size/16 scaling */ + cinfo->jpeg_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L); + cinfo->jpeg_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L); + cinfo->min_DCT_h_scaled_size = 16; + cinfo->min_DCT_v_scaled_size = 16; + } + +#else /* !DCT_SCALING_SUPPORTED */ + + /* Hardwire it to "no scaling" */ + cinfo->jpeg_width = cinfo->image_width; + cinfo->jpeg_height = cinfo->image_height; + cinfo->min_DCT_h_scaled_size = DCTSIZE; + cinfo->min_DCT_v_scaled_size = DCTSIZE; + +#endif /* DCT_SCALING_SUPPORTED */ +} + + +LOCAL(void) +jpeg_calc_trans_dimensions (j_compress_ptr cinfo) +{ + if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size) + ERREXIT2(cinfo, JERR_BAD_DCTSIZE, + cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size); + + cinfo->block_size = cinfo->min_DCT_h_scaled_size; +} + + +LOCAL(void) +initial_setup (j_compress_ptr cinfo, boolean transcode_only) +/* Do computations that are needed before master selection phase */ +{ + int ci, ssize; + jpeg_component_info *compptr; + long samplesperrow; + JDIMENSION jd_samplesperrow; + + if (transcode_only) + jpeg_calc_trans_dimensions(cinfo); + else + jpeg_calc_jpeg_dimensions(cinfo); + + /* Sanity check on block_size */ + if (cinfo->block_size < 1 || cinfo->block_size > 16) + ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size); + + /* Derive natural_order from block_size */ + switch (cinfo->block_size) { + case 2: cinfo->natural_order = jpeg_natural_order2; break; + case 3: cinfo->natural_order = jpeg_natural_order3; break; + case 4: cinfo->natural_order = jpeg_natural_order4; break; + case 5: cinfo->natural_order = jpeg_natural_order5; break; + case 6: cinfo->natural_order = jpeg_natural_order6; break; + case 7: cinfo->natural_order = jpeg_natural_order7; break; + default: cinfo->natural_order = jpeg_natural_order; break; + } + + /* Derive lim_Se from block_size */ + cinfo->lim_Se = cinfo->block_size < DCTSIZE ? + cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1; + + /* Sanity check on image dimensions */ + if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || + cinfo->num_components <= 0 || cinfo->input_components <= 0) + ERREXIT(cinfo, JERR_EMPTY_IMAGE); + + /* Make sure image isn't bigger than I can handle */ + if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || + (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); + + /* Width of an input scanline must be representable as JDIMENSION. */ + samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; + jd_samplesperrow = (JDIMENSION) samplesperrow; + if ((long) jd_samplesperrow != samplesperrow) + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); + + /* For now, precision must match compiled-in value... */ + if (cinfo->data_precision != BITS_IN_JSAMPLE) + ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); + + /* Check that number of components won't exceed internal array sizes */ + if (cinfo->num_components > MAX_COMPONENTS) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, + MAX_COMPONENTS); + + /* Compute maximum sampling factors; check factor validity */ + cinfo->max_h_samp_factor = 1; + cinfo->max_v_samp_factor = 1; + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || + compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) + ERREXIT(cinfo, JERR_BAD_SAMPLING); + cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, + compptr->h_samp_factor); + cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, + compptr->v_samp_factor); + } + + /* Compute dimensions of components */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Fill in the correct component_index value; don't rely on application */ + compptr->component_index = ci; + /* In selecting the actual DCT scaling for each component, we try to + * scale down the chroma components via DCT scaling rather than downsampling. + * This saves time if the downsampler gets to use 1:1 scaling. + * Note this code adapts subsampling ratios which are powers of 2. + */ + ssize = 1; +#ifdef DCT_SCALING_SUPPORTED + while (cinfo->min_DCT_h_scaled_size * ssize <= + (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && + (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { + ssize = ssize * 2; + } +#endif + compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; + ssize = 1; +#ifdef DCT_SCALING_SUPPORTED + while (cinfo->min_DCT_v_scaled_size * ssize <= + (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && + (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { + ssize = ssize * 2; + } +#endif + compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; + + /* We don't support DCT ratios larger than 2. */ + if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) + compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; + else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) + compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; + + /* Size in DCT blocks */ + compptr->width_in_blocks = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, + (long) (cinfo->max_h_samp_factor * cinfo->block_size)); + compptr->height_in_blocks = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + /* Size in samples */ + compptr->downsampled_width = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_width * + (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), + (long) (cinfo->max_h_samp_factor * cinfo->block_size)); + compptr->downsampled_height = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_height * + (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + /* Mark component needed (this flag isn't actually used for compression) */ + compptr->component_needed = TRUE; + } + + /* Compute number of fully interleaved MCU rows (number of times that + * main controller will call coefficient controller). + */ + cinfo->total_iMCU_rows = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_height, + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); +} + + +#ifdef C_MULTISCAN_FILES_SUPPORTED + +LOCAL(void) +validate_script (j_compress_ptr cinfo) +/* Verify that the scan script in cinfo->scan_info[] is valid; also + * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. + */ +{ + const jpeg_scan_info * scanptr; + int scanno, ncomps, ci, coefi, thisi; + int Ss, Se, Ah, Al; + boolean component_sent[MAX_COMPONENTS]; +#ifdef C_PROGRESSIVE_SUPPORTED + int * last_bitpos_ptr; + int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; + /* -1 until that coefficient has been seen; then last Al for it */ +#endif + + if (cinfo->num_scans <= 0) + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); + + /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; + * for progressive JPEG, no scan can have this. + */ + scanptr = cinfo->scan_info; + if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { +#ifdef C_PROGRESSIVE_SUPPORTED + cinfo->progressive_mode = TRUE; + last_bitpos_ptr = & last_bitpos[0][0]; + for (ci = 0; ci < cinfo->num_components; ci++) + for (coefi = 0; coefi < DCTSIZE2; coefi++) + *last_bitpos_ptr++ = -1; +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } else { + cinfo->progressive_mode = FALSE; + for (ci = 0; ci < cinfo->num_components; ci++) + component_sent[ci] = FALSE; + } + + for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { + /* Validate component indexes */ + ncomps = scanptr->comps_in_scan; + if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); + for (ci = 0; ci < ncomps; ci++) { + thisi = scanptr->component_index[ci]; + if (thisi < 0 || thisi >= cinfo->num_components) + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); + /* Components must appear in SOF order within each scan */ + if (ci > 0 && thisi <= scanptr->component_index[ci-1]) + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); + } + /* Validate progression parameters */ + Ss = scanptr->Ss; + Se = scanptr->Se; + Ah = scanptr->Ah; + Al = scanptr->Al; + if (cinfo->progressive_mode) { +#ifdef C_PROGRESSIVE_SUPPORTED + /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that + * seems wrong: the upper bound ought to depend on data precision. + * Perhaps they really meant 0..N+1 for N-bit precision. + * Here we allow 0..10 for 8-bit data; Al larger than 10 results in + * out-of-range reconstructed DC values during the first DC scan, + * which might cause problems for some decoders. + */ +#if BITS_IN_JSAMPLE == 8 +#define MAX_AH_AL 10 +#else +#define MAX_AH_AL 13 +#endif + if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || + Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + if (Ss == 0) { + if (Se != 0) /* DC and AC together not OK */ + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + } else { + if (ncomps != 1) /* AC scans must be for only one component */ + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + } + for (ci = 0; ci < ncomps; ci++) { + last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; + if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + for (coefi = Ss; coefi <= Se; coefi++) { + if (last_bitpos_ptr[coefi] < 0) { + /* first scan of this coefficient */ + if (Ah != 0) + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + } else { + /* not first scan */ + if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + } + last_bitpos_ptr[coefi] = Al; + } + } +#endif + } else { + /* For sequential JPEG, all progression parameters must be these: */ + if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) + ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); + /* Make sure components are not sent twice */ + for (ci = 0; ci < ncomps; ci++) { + thisi = scanptr->component_index[ci]; + if (component_sent[thisi]) + ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); + component_sent[thisi] = TRUE; + } + } + } + + /* Now verify that everything got sent. */ + if (cinfo->progressive_mode) { +#ifdef C_PROGRESSIVE_SUPPORTED + /* For progressive mode, we only check that at least some DC data + * got sent for each component; the spec does not require that all bits + * of all coefficients be transmitted. Would it be wiser to enforce + * transmission of all coefficient bits?? + */ + for (ci = 0; ci < cinfo->num_components; ci++) { + if (last_bitpos[ci][0] < 0) + ERREXIT(cinfo, JERR_MISSING_DATA); + } +#endif + } else { + for (ci = 0; ci < cinfo->num_components; ci++) { + if (! component_sent[ci]) + ERREXIT(cinfo, JERR_MISSING_DATA); + } + } +} + + +LOCAL(void) +reduce_script (j_compress_ptr cinfo) +/* Adapt scan script for use with reduced block size; + * assume that script has been validated before. + */ +{ + jpeg_scan_info * scanptr; + int idxout, idxin; + + /* Circumvent const declaration for this function */ + scanptr = (jpeg_scan_info *) cinfo->scan_info; + idxout = 0; + + for (idxin = 0; idxin < cinfo->num_scans; idxin++) { + /* After skipping, idxout becomes smaller than idxin */ + if (idxin != idxout) + /* Copy rest of data; + * note we stay in given chunk of allocated memory. + */ + scanptr[idxout] = scanptr[idxin]; + if (scanptr[idxout].Ss > cinfo->lim_Se) + /* Entire scan out of range - skip this entry */ + continue; + if (scanptr[idxout].Se > cinfo->lim_Se) + /* Limit scan to end of block */ + scanptr[idxout].Se = cinfo->lim_Se; + idxout++; + } + + cinfo->num_scans = idxout; +} + +#endif /* C_MULTISCAN_FILES_SUPPORTED */ + + +LOCAL(void) +select_scan_parameters (j_compress_ptr cinfo) +/* Set up the scan parameters for the current scan */ +{ + int ci; + +#ifdef C_MULTISCAN_FILES_SUPPORTED + if (cinfo->scan_info != NULL) { + /* Prepare for current scan --- the script is already validated */ + my_master_ptr master = (my_master_ptr) cinfo->master; + const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; + + cinfo->comps_in_scan = scanptr->comps_in_scan; + for (ci = 0; ci < scanptr->comps_in_scan; ci++) { + cinfo->cur_comp_info[ci] = + &cinfo->comp_info[scanptr->component_index[ci]]; + } + if (cinfo->progressive_mode) { + cinfo->Ss = scanptr->Ss; + cinfo->Se = scanptr->Se; + cinfo->Ah = scanptr->Ah; + cinfo->Al = scanptr->Al; + return; + } + } + else +#endif + { + /* Prepare for single sequential-JPEG scan containing all components */ + if (cinfo->num_components > MAX_COMPS_IN_SCAN) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, + MAX_COMPS_IN_SCAN); + cinfo->comps_in_scan = cinfo->num_components; + for (ci = 0; ci < cinfo->num_components; ci++) { + cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; + } + } + cinfo->Ss = 0; + cinfo->Se = cinfo->block_size * cinfo->block_size - 1; + cinfo->Ah = 0; + cinfo->Al = 0; +} + + +LOCAL(void) +per_scan_setup (j_compress_ptr cinfo) +/* Do computations that are needed before processing a JPEG scan */ +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ +{ + int ci, mcublks, tmp; + jpeg_component_info *compptr; + + if (cinfo->comps_in_scan == 1) { + + /* Noninterleaved (single-component) scan */ + compptr = cinfo->cur_comp_info[0]; + + /* Overall image size in MCUs */ + cinfo->MCUs_per_row = compptr->width_in_blocks; + cinfo->MCU_rows_in_scan = compptr->height_in_blocks; + + /* For noninterleaved scan, always one block per MCU */ + compptr->MCU_width = 1; + compptr->MCU_height = 1; + compptr->MCU_blocks = 1; + compptr->MCU_sample_width = compptr->DCT_h_scaled_size; + compptr->last_col_width = 1; + /* For noninterleaved scans, it is convenient to define last_row_height + * as the number of block rows present in the last iMCU row. + */ + tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); + if (tmp == 0) tmp = compptr->v_samp_factor; + compptr->last_row_height = tmp; + + /* Prepare array describing MCU composition */ + cinfo->blocks_in_MCU = 1; + cinfo->MCU_membership[0] = 0; + + } else { + + /* Interleaved (multi-component) scan */ + if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, + MAX_COMPS_IN_SCAN); + + /* Overall image size in MCUs */ + cinfo->MCUs_per_row = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_width, + (long) (cinfo->max_h_samp_factor * cinfo->block_size)); + cinfo->MCU_rows_in_scan = (JDIMENSION) + jdiv_round_up((long) cinfo->jpeg_height, + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + + cinfo->blocks_in_MCU = 0; + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* Sampling factors give # of blocks of component in each MCU */ + compptr->MCU_width = compptr->h_samp_factor; + compptr->MCU_height = compptr->v_samp_factor; + compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; + compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; + /* Figure number of non-dummy blocks in last MCU column & row */ + tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); + if (tmp == 0) tmp = compptr->MCU_width; + compptr->last_col_width = tmp; + tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); + if (tmp == 0) tmp = compptr->MCU_height; + compptr->last_row_height = tmp; + /* Prepare array describing MCU composition */ + mcublks = compptr->MCU_blocks; + if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) + ERREXIT(cinfo, JERR_BAD_MCU_SIZE); + while (mcublks-- > 0) { + cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; + } + } + + } + + /* Convert restart specified in rows to actual MCU count. */ + /* Note that count must fit in 16 bits, so we provide limiting. */ + if (cinfo->restart_in_rows > 0) { + long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; + cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); + } +} + + +/* + * Per-pass setup. + * This is called at the beginning of each pass. We determine which modules + * will be active during this pass and give them appropriate start_pass calls. + * We also set is_last_pass to indicate whether any more passes will be + * required. + */ + +METHODDEF(void) +prepare_for_pass (j_compress_ptr cinfo) +{ + my_master_ptr master = (my_master_ptr) cinfo->master; + + switch (master->pass_type) { + case main_pass: + /* Initial pass: will collect input data, and do either Huffman + * optimization or data output for the first scan. + */ + select_scan_parameters(cinfo); + per_scan_setup(cinfo); + if (! cinfo->raw_data_in) { + (*cinfo->cconvert->start_pass) (cinfo); + (*cinfo->downsample->start_pass) (cinfo); + (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); + } + (*cinfo->fdct->start_pass) (cinfo); + (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); + (*cinfo->coef->start_pass) (cinfo, + (master->total_passes > 1 ? + JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); + (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); + if (cinfo->optimize_coding) { + /* No immediate data output; postpone writing frame/scan headers */ + master->pub.call_pass_startup = FALSE; + } else { + /* Will write frame/scan headers at first jpeg_write_scanlines call */ + master->pub.call_pass_startup = TRUE; + } + break; +#ifdef ENTROPY_OPT_SUPPORTED + case huff_opt_pass: + /* Do Huffman optimization for a scan after the first one. */ + select_scan_parameters(cinfo); + per_scan_setup(cinfo); + if (cinfo->Ss != 0 || cinfo->Ah == 0) { + (*cinfo->entropy->start_pass) (cinfo, TRUE); + (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); + master->pub.call_pass_startup = FALSE; + break; + } + /* Special case: Huffman DC refinement scans need no Huffman table + * and therefore we can skip the optimization pass for them. + */ + master->pass_type = output_pass; + master->pass_number++; + /*FALLTHROUGH*/ +#endif + case output_pass: + /* Do a data-output pass. */ + /* We need not repeat per-scan setup if prior optimization pass did it. */ + if (! cinfo->optimize_coding) { + select_scan_parameters(cinfo); + per_scan_setup(cinfo); + } + (*cinfo->entropy->start_pass) (cinfo, FALSE); + (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); + /* We emit frame/scan headers now */ + if (master->scan_number == 0) + (*cinfo->marker->write_frame_header) (cinfo); + (*cinfo->marker->write_scan_header) (cinfo); + master->pub.call_pass_startup = FALSE; + break; + default: + ERREXIT(cinfo, JERR_NOT_COMPILED); + } + + master->pub.is_last_pass = (master->pass_number == master->total_passes-1); + + /* Set up progress monitor's pass info if present */ + if (cinfo->progress != NULL) { + cinfo->progress->completed_passes = master->pass_number; + cinfo->progress->total_passes = master->total_passes; + } +} + + +/* + * Special start-of-pass hook. + * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. + * In single-pass processing, we need this hook because we don't want to + * write frame/scan headers during jpeg_start_compress; we want to let the + * application write COM markers etc. between jpeg_start_compress and the + * jpeg_write_scanlines loop. + * In multi-pass processing, this routine is not used. + */ + +METHODDEF(void) +pass_startup (j_compress_ptr cinfo) +{ + cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ + + (*cinfo->marker->write_frame_header) (cinfo); + (*cinfo->marker->write_scan_header) (cinfo); +} + + +/* + * Finish up at end of pass. + */ + +METHODDEF(void) +finish_pass_master (j_compress_ptr cinfo) +{ + my_master_ptr master = (my_master_ptr) cinfo->master; + + /* The entropy coder always needs an end-of-pass call, + * either to analyze statistics or to flush its output buffer. + */ + (*cinfo->entropy->finish_pass) (cinfo); + + /* Update state for next pass */ + switch (master->pass_type) { + case main_pass: + /* next pass is either output of scan 0 (after optimization) + * or output of scan 1 (if no optimization). + */ + master->pass_type = output_pass; + if (! cinfo->optimize_coding) + master->scan_number++; + break; + case huff_opt_pass: + /* next pass is always output of current scan */ + master->pass_type = output_pass; + break; + case output_pass: + /* next pass is either optimization or output of next scan */ + if (cinfo->optimize_coding) + master->pass_type = huff_opt_pass; + master->scan_number++; + break; + } + + master->pass_number++; +} + + +/* + * Initialize master compression control. + */ + +GLOBAL(void) +jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) +{ + my_master_ptr master; + + master = (my_master_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_comp_master)); + cinfo->master = (struct jpeg_comp_master *) master; + master->pub.prepare_for_pass = prepare_for_pass; + master->pub.pass_startup = pass_startup; + master->pub.finish_pass = finish_pass_master; + master->pub.is_last_pass = FALSE; + + /* Validate parameters, determine derived values */ + initial_setup(cinfo, transcode_only); + + if (cinfo->scan_info != NULL) { +#ifdef C_MULTISCAN_FILES_SUPPORTED + validate_script(cinfo); + if (cinfo->block_size < DCTSIZE) + reduce_script(cinfo); +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } else { + cinfo->progressive_mode = FALSE; + cinfo->num_scans = 1; + } + + if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) && + !cinfo->arith_code) /* TEMPORARY HACK ??? */ + /* assume default tables no good for progressive or downscale mode */ + cinfo->optimize_coding = TRUE; + + /* Initialize my private state */ + if (transcode_only) { + /* no main pass in transcoding */ + if (cinfo->optimize_coding) + master->pass_type = huff_opt_pass; + else + master->pass_type = output_pass; + } else { + /* for normal compression, first pass is always this type: */ + master->pass_type = main_pass; + } + master->scan_number = 0; + master->pass_number = 0; + if (cinfo->optimize_coding) + master->total_passes = cinfo->num_scans * 2; + else + master->total_passes = cinfo->num_scans; +} diff --git a/crypto777/jpeg/jcomapi.c b/crypto777/jpeg/jcomapi.c new file mode 100644 index 000000000..9b1fa7568 --- /dev/null +++ b/crypto777/jpeg/jcomapi.c @@ -0,0 +1,106 @@ +/* + * jcomapi.c + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains application interface routines that are used for both + * compression and decompression. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * Abort processing of a JPEG compression or decompression operation, + * but don't destroy the object itself. + * + * For this, we merely clean up all the nonpermanent memory pools. + * Note that temp files (virtual arrays) are not allowed to belong to + * the permanent pool, so we will be able to close all temp files here. + * Closing a data source or destination, if necessary, is the application's + * responsibility. + */ + +GLOBAL(void) +jpeg_abort (j_common_ptr cinfo) +{ + int pool; + + /* Do nothing if called on a not-initialized or destroyed JPEG object. */ + if (cinfo->mem == NULL) + return; + + /* Releasing pools in reverse order might help avoid fragmentation + * with some (brain-damaged) malloc libraries. + */ + for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) { + (*cinfo->mem->free_pool) (cinfo, pool); + } + + /* Reset overall state for possible reuse of object */ + if (cinfo->is_decompressor) { + cinfo->global_state = DSTATE_START; + /* Try to keep application from accessing now-deleted marker list. + * A bit kludgy to do it here, but this is the most central place. + */ + ((j_decompress_ptr) cinfo)->marker_list = NULL; + } else { + cinfo->global_state = CSTATE_START; + } +} + + +/* + * Destruction of a JPEG object. + * + * Everything gets deallocated except the master jpeg_compress_struct itself + * and the error manager struct. Both of these are supplied by the application + * and must be freed, if necessary, by the application. (Often they are on + * the stack and so don't need to be freed anyway.) + * Closing a data source or destination, if necessary, is the application's + * responsibility. + */ + +GLOBAL(void) +jpeg_destroy (j_common_ptr cinfo) +{ + /* We need only tell the memory manager to release everything. */ + /* NB: mem pointer is NULL if memory mgr failed to initialize. */ + if (cinfo->mem != NULL) + (*cinfo->mem->self_destruct) (cinfo); + cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */ + cinfo->global_state = 0; /* mark it destroyed */ +} + + +/* + * Convenience routines for allocating quantization and Huffman tables. + * (Would jutils.c be a more reasonable place to put these?) + */ + +GLOBAL(JQUANT_TBL *) +jpeg_alloc_quant_table (j_common_ptr cinfo) +{ + JQUANT_TBL *tbl; + + tbl = (JQUANT_TBL *) + (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL)); + tbl->sent_table = FALSE; /* make sure this is false in any new table */ + return tbl; +} + + +GLOBAL(JHUFF_TBL *) +jpeg_alloc_huff_table (j_common_ptr cinfo) +{ + JHUFF_TBL *tbl; + + tbl = (JHUFF_TBL *) + (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL)); + tbl->sent_table = FALSE; /* make sure this is false in any new table */ + return tbl; +} diff --git a/crypto777/jpeg/jconfig.h b/crypto777/jpeg/jconfig.h new file mode 100644 index 000000000..966b1d514 --- /dev/null +++ b/crypto777/jpeg/jconfig.h @@ -0,0 +1,54 @@ +/* jconfig.h. Generated from jconfig.cfg by configure. */ +/* jconfig.cfg --- source file edited by configure script */ +/* see jconfig.txt for explanations */ + +#define HAVE_PROTOTYPES 1 +#define HAVE_UNSIGNED_CHAR 1 +#define HAVE_UNSIGNED_SHORT 1 +/* #undef void */ +/* #undef const */ +/* #undef CHAR_IS_UNSIGNED */ +#define HAVE_STDDEF_H 1 +#define HAVE_STDLIB_H 1 +#define HAVE_LOCALE_H 1 +/* #undef NEED_BSD_STRINGS */ +/* #undef NEED_SYS_TYPES_H */ +/* #undef NEED_FAR_POINTERS */ +/* #undef NEED_SHORT_EXTERNAL_NAMES */ +/* Define this if you get warnings about undefined structures. */ +/* #undef INCOMPLETE_TYPES_BROKEN */ + +/* Define "boolean" as unsigned char, not int, on Windows systems. */ +#ifdef _WIN32 +#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */ +typedef unsigned char boolean; +#endif +#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */ +#endif + +#ifdef JPEG_INTERNALS + +/* #undef RIGHT_SHIFT_IS_UNSIGNED */ +#define INLINE __inline__ +/* These are for configuring the JPEG memory manager. */ +/* #undef DEFAULT_MAX_MEM */ +/* #undef NO_MKTEMP */ + +#endif /* JPEG_INTERNALS */ + +#ifdef JPEG_CJPEG_DJPEG + +#define BMP_SUPPORTED /* BMP image file format */ +#define GIF_SUPPORTED /* GIF image file format */ +#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */ +/* #undef RLE_SUPPORTED */ +#define TARGA_SUPPORTED /* Targa image file format */ + +/* #undef TWO_FILE_COMMANDLINE */ +/* #undef NEED_SIGNAL_CATCHER */ +/* #undef DONT_USE_B_MODE */ + +/* Define this if you want percent-done progress reports from cjpeg/djpeg. */ +/* #undef PROGRESS_REPORT */ + +#endif /* JPEG_CJPEG_DJPEG */ diff --git a/crypto777/jpeg/jcparam.c b/crypto777/jpeg/jcparam.c new file mode 100644 index 000000000..c5e85dda5 --- /dev/null +++ b/crypto777/jpeg/jcparam.c @@ -0,0 +1,632 @@ +/* + * jcparam.c + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * Modified 2003-2008 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains optional default-setting code for the JPEG compressor. + * Applications do not have to use this file, but those that don't use it + * must know a lot more about the innards of the JPEG code. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * Quantization table setup routines + */ + +GLOBAL(void) +jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, + const unsigned int *basic_table, + int scale_factor, boolean force_baseline) +/* Define a quantization table equal to the basic_table times + * a scale factor (given as a percentage). + * If force_baseline is TRUE, the computed quantization table entries + * are limited to 1..255 for JPEG baseline compatibility. + */ +{ + JQUANT_TBL ** qtblptr; + int i; + long temp; + + /* Safety check to ensure start_compress not called yet. */ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS) + ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl); + + qtblptr = & cinfo->quant_tbl_ptrs[which_tbl]; + + if (*qtblptr == NULL) + *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo); + + for (i = 0; i < DCTSIZE2; i++) { + temp = ((long) basic_table[i] * scale_factor + 50L) / 100L; + /* limit the values to the valid range */ + if (temp <= 0L) temp = 1L; + if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */ + if (force_baseline && temp > 255L) + temp = 255L; /* limit to baseline range if requested */ + (*qtblptr)->quantval[i] = (UINT16) temp; + } + + /* Initialize sent_table FALSE so table will be written to JPEG file. */ + (*qtblptr)->sent_table = FALSE; +} + + +/* These are the sample quantization tables given in JPEG spec section K.1. + * The spec says that the values given produce "good" quality, and + * when divided by 2, "very good" quality. + */ +static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = { + 16, 11, 10, 16, 24, 40, 51, 61, + 12, 12, 14, 19, 26, 58, 60, 55, + 14, 13, 16, 24, 40, 57, 69, 56, + 14, 17, 22, 29, 51, 87, 80, 62, + 18, 22, 37, 56, 68, 109, 103, 77, + 24, 35, 55, 64, 81, 104, 113, 92, + 49, 64, 78, 87, 103, 121, 120, 101, + 72, 92, 95, 98, 112, 100, 103, 99 +}; +static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = { + 17, 18, 24, 47, 99, 99, 99, 99, + 18, 21, 26, 66, 99, 99, 99, 99, + 24, 26, 56, 99, 99, 99, 99, 99, + 47, 66, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99 +}; + + +GLOBAL(void) +jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline) +/* Set or change the 'quality' (quantization) setting, using default tables + * and straight percentage-scaling quality scales. + * This entry point allows different scalings for luminance and chrominance. + */ +{ + /* Set up two quantization tables using the specified scaling */ + jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, + cinfo->q_scale_factor[0], force_baseline); + jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, + cinfo->q_scale_factor[1], force_baseline); +} + + +GLOBAL(void) +jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor, + boolean force_baseline) +/* Set or change the 'quality' (quantization) setting, using default tables + * and a straight percentage-scaling quality scale. In most cases it's better + * to use jpeg_set_quality (below); this entry point is provided for + * applications that insist on a linear percentage scaling. + */ +{ + /* Set up two quantization tables using the specified scaling */ + jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, + scale_factor, force_baseline); + jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, + scale_factor, force_baseline); +} + + +GLOBAL(int) +jpeg_quality_scaling (int quality) +/* Convert a user-specified quality rating to a percentage scaling factor + * for an underlying quantization table, using our recommended scaling curve. + * The input 'quality' factor should be 0 (terrible) to 100 (very good). + */ +{ + /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */ + if (quality <= 0) quality = 1; + if (quality > 100) quality = 100; + + /* The basic table is used as-is (scaling 100) for a quality of 50. + * Qualities 50..100 are converted to scaling percentage 200 - 2*Q; + * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table + * to make all the table entries 1 (hence, minimum quantization loss). + * Qualities 1..50 are converted to scaling percentage 5000/Q. + */ + if (quality < 50) + quality = 5000 / quality; + else + quality = 200 - quality*2; + + return quality; +} + + +GLOBAL(void) +jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline) +/* Set or change the 'quality' (quantization) setting, using default tables. + * This is the standard quality-adjusting entry point for typical user + * interfaces; only those who want detailed control over quantization tables + * would use the preceding three routines directly. + */ +{ + /* Convert user 0-100 rating to percentage scaling */ + quality = jpeg_quality_scaling(quality); + + /* Set up standard quality tables */ + jpeg_set_linear_quality(cinfo, quality, force_baseline); +} + + +/* + * Huffman table setup routines + */ + +LOCAL(void) +add_huff_table (j_compress_ptr cinfo, + JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val) +/* Define a Huffman table */ +{ + int nsymbols, len; + + if (*htblptr == NULL) + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); + + /* Copy the number-of-symbols-of-each-code-length counts */ + MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); + + /* Validate the counts. We do this here mainly so we can copy the right + * number of symbols from the val[] array, without risking marching off + * the end of memory. jchuff.c will do a more thorough test later. + */ + nsymbols = 0; + for (len = 1; len <= 16; len++) + nsymbols += bits[len]; + if (nsymbols < 1 || nsymbols > 256) + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + + MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8)); + + /* Initialize sent_table FALSE so table will be written to JPEG file. */ + (*htblptr)->sent_table = FALSE; +} + + +LOCAL(void) +std_huff_tables (j_compress_ptr cinfo) +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ +/* IMPORTANT: these are only valid for 8-bit data precision! */ +{ + static const UINT8 bits_dc_luminance[17] = + { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; + static const UINT8 val_dc_luminance[] = + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + + static const UINT8 bits_dc_chrominance[17] = + { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; + static const UINT8 val_dc_chrominance[] = + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + + static const UINT8 bits_ac_luminance[17] = + { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; + static const UINT8 val_ac_luminance[] = + { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, + 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, + 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, + 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, + 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, + 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, + 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, + 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, + 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, + 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, + 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, + 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, + 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, + 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, + 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, + 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa }; + + static const UINT8 bits_ac_chrominance[17] = + { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; + static const UINT8 val_ac_chrominance[] = + { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, + 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, + 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, + 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, + 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, + 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, + 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, + 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, + 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, + 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, + 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, + 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, + 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, + 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa }; + + add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0], + bits_dc_luminance, val_dc_luminance); + add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0], + bits_ac_luminance, val_ac_luminance); + add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1], + bits_dc_chrominance, val_dc_chrominance); + add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1], + bits_ac_chrominance, val_ac_chrominance); +} + + +/* + * Default parameter setup for compression. + * + * Applications that don't choose to use this routine must do their + * own setup of all these parameters. Alternately, you can call this + * to establish defaults and then alter parameters selectively. This + * is the recommended approach since, if we add any new parameters, + * your code will still work (they'll be set to reasonable defaults). + */ + +GLOBAL(void) +jpeg_set_defaults (j_compress_ptr cinfo) +{ + int i; + + /* Safety check to ensure start_compress not called yet. */ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + /* Allocate comp_info array large enough for maximum component count. + * Array is made permanent in case application wants to compress + * multiple images at same param settings. + */ + if (cinfo->comp_info == NULL) + cinfo->comp_info = (jpeg_component_info *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + MAX_COMPONENTS * SIZEOF(jpeg_component_info)); + + /* Initialize everything not dependent on the color space */ + + cinfo->scale_num = 1; /* 1:1 scaling */ + cinfo->scale_denom = 1; + cinfo->data_precision = BITS_IN_JSAMPLE; + /* Set up two quantization tables using default quality of 75 */ + jpeg_set_quality(cinfo, 75, TRUE); + /* Set up two Huffman tables */ + std_huff_tables(cinfo); + + /* Initialize default arithmetic coding conditioning */ + for (i = 0; i < NUM_ARITH_TBLS; i++) { + cinfo->arith_dc_L[i] = 0; + cinfo->arith_dc_U[i] = 1; + cinfo->arith_ac_K[i] = 5; + } + + /* Default is no multiple-scan output */ + cinfo->scan_info = NULL; + cinfo->num_scans = 0; + + /* Expect normal source image, not raw downsampled data */ + cinfo->raw_data_in = FALSE; + + /* Use Huffman coding, not arithmetic coding, by default */ + cinfo->arith_code = FALSE; + + /* By default, don't do extra passes to optimize entropy coding */ + cinfo->optimize_coding = FALSE; + /* The standard Huffman tables are only valid for 8-bit data precision. + * If the precision is higher, force optimization on so that usable + * tables will be computed. This test can be removed if default tables + * are supplied that are valid for the desired precision. + */ + if (cinfo->data_precision > 8) + cinfo->optimize_coding = TRUE; + + /* By default, use the simpler non-cosited sampling alignment */ + cinfo->CCIR601_sampling = FALSE; + + /* By default, apply fancy downsampling */ + cinfo->do_fancy_downsampling = TRUE; + + /* No input smoothing */ + cinfo->smoothing_factor = 0; + + /* DCT algorithm preference */ + cinfo->dct_method = JDCT_DEFAULT; + + /* No restart markers */ + cinfo->restart_interval = 0; + cinfo->restart_in_rows = 0; + + /* Fill in default JFIF marker parameters. Note that whether the marker + * will actually be written is determined by jpeg_set_colorspace. + * + * By default, the library emits JFIF version code 1.01. + * An application that wants to emit JFIF 1.02 extension markers should set + * JFIF_minor_version to 2. We could probably get away with just defaulting + * to 1.02, but there may still be some decoders in use that will complain + * about that; saying 1.01 should minimize compatibility problems. + */ + cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */ + cinfo->JFIF_minor_version = 1; + cinfo->density_unit = 0; /* Pixel size is unknown by default */ + cinfo->X_density = 1; /* Pixel aspect ratio is square by default */ + cinfo->Y_density = 1; + + /* Choose JPEG colorspace based on input space, set defaults accordingly */ + + jpeg_default_colorspace(cinfo); +} + + +/* + * Select an appropriate JPEG colorspace for in_color_space. + */ + +GLOBAL(void) +jpeg_default_colorspace (j_compress_ptr cinfo) +{ + switch (cinfo->in_color_space) { + case JCS_GRAYSCALE: + jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); + break; + case JCS_RGB: + jpeg_set_colorspace(cinfo, JCS_YCbCr); + break; + case JCS_YCbCr: + jpeg_set_colorspace(cinfo, JCS_YCbCr); + break; + case JCS_CMYK: + jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */ + break; + case JCS_YCCK: + jpeg_set_colorspace(cinfo, JCS_YCCK); + break; + case JCS_UNKNOWN: + jpeg_set_colorspace(cinfo, JCS_UNKNOWN); + break; + default: + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); + } +} + + +/* + * Set the JPEG colorspace, and choose colorspace-dependent default values. + */ + +GLOBAL(void) +jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace) +{ + jpeg_component_info * compptr; + int ci; + +#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \ + (compptr = &cinfo->comp_info[index], \ + compptr->component_id = (id), \ + compptr->h_samp_factor = (hsamp), \ + compptr->v_samp_factor = (vsamp), \ + compptr->quant_tbl_no = (quant), \ + compptr->dc_tbl_no = (dctbl), \ + compptr->ac_tbl_no = (actbl) ) + + /* Safety check to ensure start_compress not called yet. */ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + /* For all colorspaces, we use Q and Huff tables 0 for luminance components, + * tables 1 for chrominance components. + */ + + cinfo->jpeg_color_space = colorspace; + + cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */ + cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */ + + switch (colorspace) { + case JCS_GRAYSCALE: + cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ + cinfo->num_components = 1; + /* JFIF specifies component ID 1 */ + SET_COMP(0, 1, 1,1, 0, 0,0); + break; + case JCS_RGB: + cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */ + cinfo->num_components = 3; + SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0); + SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0); + SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0); + break; + case JCS_YCbCr: + cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ + cinfo->num_components = 3; + /* JFIF specifies component IDs 1,2,3 */ + /* We default to 2x2 subsamples of chrominance */ + SET_COMP(0, 1, 2,2, 0, 0,0); + SET_COMP(1, 2, 1,1, 1, 1,1); + SET_COMP(2, 3, 1,1, 1, 1,1); + break; + case JCS_CMYK: + cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */ + cinfo->num_components = 4; + SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0); + SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0); + SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0); + SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0); + break; + case JCS_YCCK: + cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */ + cinfo->num_components = 4; + SET_COMP(0, 1, 2,2, 0, 0,0); + SET_COMP(1, 2, 1,1, 1, 1,1); + SET_COMP(2, 3, 1,1, 1, 1,1); + SET_COMP(3, 4, 2,2, 0, 0,0); + break; + case JCS_UNKNOWN: + cinfo->num_components = cinfo->input_components; + if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, + MAX_COMPONENTS); + for (ci = 0; ci < cinfo->num_components; ci++) { + SET_COMP(ci, ci, 1,1, 0, 0,0); + } + break; + default: + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + } +} + + +#ifdef C_PROGRESSIVE_SUPPORTED + +LOCAL(jpeg_scan_info *) +fill_a_scan (jpeg_scan_info * scanptr, int ci, + int Ss, int Se, int Ah, int Al) +/* Support routine: generate one scan for specified component */ +{ + scanptr->comps_in_scan = 1; + scanptr->component_index[0] = ci; + scanptr->Ss = Ss; + scanptr->Se = Se; + scanptr->Ah = Ah; + scanptr->Al = Al; + scanptr++; + return scanptr; +} + +LOCAL(jpeg_scan_info *) +fill_scans (jpeg_scan_info * scanptr, int ncomps, + int Ss, int Se, int Ah, int Al) +/* Support routine: generate one scan for each component */ +{ + int ci; + + for (ci = 0; ci < ncomps; ci++) { + scanptr->comps_in_scan = 1; + scanptr->component_index[0] = ci; + scanptr->Ss = Ss; + scanptr->Se = Se; + scanptr->Ah = Ah; + scanptr->Al = Al; + scanptr++; + } + return scanptr; +} + +LOCAL(jpeg_scan_info *) +fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al) +/* Support routine: generate interleaved DC scan if possible, else N scans */ +{ + int ci; + + if (ncomps <= MAX_COMPS_IN_SCAN) { + /* Single interleaved DC scan */ + scanptr->comps_in_scan = ncomps; + for (ci = 0; ci < ncomps; ci++) + scanptr->component_index[ci] = ci; + scanptr->Ss = scanptr->Se = 0; + scanptr->Ah = Ah; + scanptr->Al = Al; + scanptr++; + } else { + /* Noninterleaved DC scan for each component */ + scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al); + } + return scanptr; +} + + +/* + * Create a recommended progressive-JPEG script. + * cinfo->num_components and cinfo->jpeg_color_space must be correct. + */ + +GLOBAL(void) +jpeg_simple_progression (j_compress_ptr cinfo) +{ + int ncomps = cinfo->num_components; + int nscans; + jpeg_scan_info * scanptr; + + /* Safety check to ensure start_compress not called yet. */ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + /* Figure space needed for script. Calculation must match code below! */ + if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { + /* Custom script for YCbCr color images. */ + nscans = 10; + } else { + /* All-purpose script for other color spaces. */ + if (ncomps > MAX_COMPS_IN_SCAN) + nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */ + else + nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */ + } + + /* Allocate space for script. + * We need to put it in the permanent pool in case the application performs + * multiple compressions without changing the settings. To avoid a memory + * leak if jpeg_simple_progression is called repeatedly for the same JPEG + * object, we try to re-use previously allocated space, and we allocate + * enough space to handle YCbCr even if initially asked for grayscale. + */ + if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) { + cinfo->script_space_size = MAX(nscans, 10); + cinfo->script_space = (jpeg_scan_info *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + cinfo->script_space_size * SIZEOF(jpeg_scan_info)); + } + scanptr = cinfo->script_space; + cinfo->scan_info = scanptr; + cinfo->num_scans = nscans; + + if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { + /* Custom script for YCbCr color images. */ + /* Initial DC scan */ + scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); + /* Initial AC scan: get some luma data out in a hurry */ + scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2); + /* Chroma data is too small to be worth expending many scans on */ + scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1); + scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1); + /* Complete spectral selection for luma AC */ + scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2); + /* Refine next bit of luma AC */ + scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1); + /* Finish DC successive approximation */ + scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); + /* Finish AC successive approximation */ + scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0); + scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0); + /* Luma bottom bit comes last since it's usually largest scan */ + scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0); + } else { + /* All-purpose script for other color spaces. */ + /* Successive approximation first pass */ + scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); + scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2); + scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2); + /* Successive approximation second pass */ + scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1); + /* Successive approximation final pass */ + scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); + scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0); + } +} + +#endif /* C_PROGRESSIVE_SUPPORTED */ diff --git a/crypto777/jpeg/jcprepct.c b/crypto777/jpeg/jcprepct.c new file mode 100644 index 000000000..be44cc4b4 --- /dev/null +++ b/crypto777/jpeg/jcprepct.c @@ -0,0 +1,358 @@ +/* + * jcprepct.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the compression preprocessing controller. + * This controller manages the color conversion, downsampling, + * and edge expansion steps. + * + * Most of the complexity here is associated with buffering input rows + * as required by the downsampler. See the comments at the head of + * jcsample.c for the downsampler's needs. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* At present, jcsample.c can request context rows only for smoothing. + * In the future, we might also need context rows for CCIR601 sampling + * or other more-complex downsampling procedures. The code to support + * context rows should be compiled only if needed. + */ +#ifdef INPUT_SMOOTHING_SUPPORTED +#define CONTEXT_ROWS_SUPPORTED +#endif + + +/* + * For the simple (no-context-row) case, we just need to buffer one + * row group's worth of pixels for the downsampling step. At the bottom of + * the image, we pad to a full row group by replicating the last pixel row. + * The downsampler's last output row is then replicated if needed to pad + * out to a full iMCU row. + * + * When providing context rows, we must buffer three row groups' worth of + * pixels. Three row groups are physically allocated, but the row pointer + * arrays are made five row groups high, with the extra pointers above and + * below "wrapping around" to point to the last and first real row groups. + * This allows the downsampler to access the proper context rows. + * At the top and bottom of the image, we create dummy context rows by + * copying the first or last real pixel row. This copying could be avoided + * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the + * trouble on the compression side. + */ + + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_c_prep_controller pub; /* public fields */ + + /* Downsampling input buffer. This buffer holds color-converted data + * until we have enough to do a downsample step. + */ + JSAMPARRAY color_buf[MAX_COMPONENTS]; + + JDIMENSION rows_to_go; /* counts rows remaining in source image */ + int next_buf_row; /* index of next row to store in color_buf */ + +#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */ + int this_row_group; /* starting row index of group to process */ + int next_buf_stop; /* downsample when we reach this index */ +#endif +} my_prep_controller; + +typedef my_prep_controller * my_prep_ptr; + + +/* + * Initialize for a processing pass. + */ + +METHODDEF(void) +start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode) +{ + my_prep_ptr prep = (my_prep_ptr) cinfo->prep; + + if (pass_mode != JBUF_PASS_THRU) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + + /* Initialize total-height counter for detecting bottom of image */ + prep->rows_to_go = cinfo->image_height; + /* Mark the conversion buffer empty */ + prep->next_buf_row = 0; +#ifdef CONTEXT_ROWS_SUPPORTED + /* Preset additional state variables for context mode. + * These aren't used in non-context mode, so we needn't test which mode. + */ + prep->this_row_group = 0; + /* Set next_buf_stop to stop after two row groups have been read in. */ + prep->next_buf_stop = 2 * cinfo->max_v_samp_factor; +#endif +} + + +/* + * Expand an image vertically from height input_rows to height output_rows, + * by duplicating the bottom row. + */ + +LOCAL(void) +expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols, + int input_rows, int output_rows) +{ + register int row; + + for (row = input_rows; row < output_rows; row++) { + jcopy_sample_rows(image_data, input_rows-1, image_data, row, + 1, num_cols); + } +} + + +/* + * Process some data in the simple no-context case. + * + * Preprocessor output data is counted in "row groups". A row group + * is defined to be v_samp_factor sample rows of each component. + * Downsampling will produce this much data from each max_v_samp_factor + * input rows. + */ + +METHODDEF(void) +pre_process_data (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, + JDIMENSION in_rows_avail, + JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, + JDIMENSION out_row_groups_avail) +{ + my_prep_ptr prep = (my_prep_ptr) cinfo->prep; + int numrows, ci; + JDIMENSION inrows; + jpeg_component_info * compptr; + + while (*in_row_ctr < in_rows_avail && + *out_row_group_ctr < out_row_groups_avail) { + /* Do color conversion to fill the conversion buffer. */ + inrows = in_rows_avail - *in_row_ctr; + numrows = cinfo->max_v_samp_factor - prep->next_buf_row; + numrows = (int) MIN((JDIMENSION) numrows, inrows); + (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr, + prep->color_buf, + (JDIMENSION) prep->next_buf_row, + numrows); + *in_row_ctr += numrows; + prep->next_buf_row += numrows; + prep->rows_to_go -= numrows; + /* If at bottom of image, pad to fill the conversion buffer. */ + if (prep->rows_to_go == 0 && + prep->next_buf_row < cinfo->max_v_samp_factor) { + for (ci = 0; ci < cinfo->num_components; ci++) { + expand_bottom_edge(prep->color_buf[ci], cinfo->image_width, + prep->next_buf_row, cinfo->max_v_samp_factor); + } + prep->next_buf_row = cinfo->max_v_samp_factor; + } + /* If we've filled the conversion buffer, empty it. */ + if (prep->next_buf_row == cinfo->max_v_samp_factor) { + (*cinfo->downsample->downsample) (cinfo, + prep->color_buf, (JDIMENSION) 0, + output_buf, *out_row_group_ctr); + prep->next_buf_row = 0; + (*out_row_group_ctr)++; + } + /* If at bottom of image, pad the output to a full iMCU height. + * Note we assume the caller is providing a one-iMCU-height output buffer! + */ + if (prep->rows_to_go == 0 && + *out_row_group_ctr < out_row_groups_avail) { + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + numrows = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; + expand_bottom_edge(output_buf[ci], + compptr->width_in_blocks * compptr->DCT_h_scaled_size, + (int) (*out_row_group_ctr * numrows), + (int) (out_row_groups_avail * numrows)); + } + *out_row_group_ctr = out_row_groups_avail; + break; /* can exit outer loop without test */ + } + } +} + + +#ifdef CONTEXT_ROWS_SUPPORTED + +/* + * Process some data in the context case. + */ + +METHODDEF(void) +pre_process_context (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, + JDIMENSION in_rows_avail, + JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, + JDIMENSION out_row_groups_avail) +{ + my_prep_ptr prep = (my_prep_ptr) cinfo->prep; + int numrows, ci; + int buf_height = cinfo->max_v_samp_factor * 3; + JDIMENSION inrows; + + while (*out_row_group_ctr < out_row_groups_avail) { + if (*in_row_ctr < in_rows_avail) { + /* Do color conversion to fill the conversion buffer. */ + inrows = in_rows_avail - *in_row_ctr; + numrows = prep->next_buf_stop - prep->next_buf_row; + numrows = (int) MIN((JDIMENSION) numrows, inrows); + (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr, + prep->color_buf, + (JDIMENSION) prep->next_buf_row, + numrows); + /* Pad at top of image, if first time through */ + if (prep->rows_to_go == cinfo->image_height) { + for (ci = 0; ci < cinfo->num_components; ci++) { + int row; + for (row = 1; row <= cinfo->max_v_samp_factor; row++) { + jcopy_sample_rows(prep->color_buf[ci], 0, + prep->color_buf[ci], -row, + 1, cinfo->image_width); + } + } + } + *in_row_ctr += numrows; + prep->next_buf_row += numrows; + prep->rows_to_go -= numrows; + } else { + /* Return for more data, unless we are at the bottom of the image. */ + if (prep->rows_to_go != 0) + break; + /* When at bottom of image, pad to fill the conversion buffer. */ + if (prep->next_buf_row < prep->next_buf_stop) { + for (ci = 0; ci < cinfo->num_components; ci++) { + expand_bottom_edge(prep->color_buf[ci], cinfo->image_width, + prep->next_buf_row, prep->next_buf_stop); + } + prep->next_buf_row = prep->next_buf_stop; + } + } + /* If we've gotten enough data, downsample a row group. */ + if (prep->next_buf_row == prep->next_buf_stop) { + (*cinfo->downsample->downsample) (cinfo, + prep->color_buf, + (JDIMENSION) prep->this_row_group, + output_buf, *out_row_group_ctr); + (*out_row_group_ctr)++; + /* Advance pointers with wraparound as necessary. */ + prep->this_row_group += cinfo->max_v_samp_factor; + if (prep->this_row_group >= buf_height) + prep->this_row_group = 0; + if (prep->next_buf_row >= buf_height) + prep->next_buf_row = 0; + prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor; + } + } +} + + +/* + * Create the wrapped-around downsampling input buffer needed for context mode. + */ + +LOCAL(void) +create_context_buffer (j_compress_ptr cinfo) +{ + my_prep_ptr prep = (my_prep_ptr) cinfo->prep; + int rgroup_height = cinfo->max_v_samp_factor; + int ci, i; + jpeg_component_info * compptr; + JSAMPARRAY true_buffer, fake_buffer; + + /* Grab enough space for fake row pointers for all the components; + * we need five row groups' worth of pointers for each component. + */ + fake_buffer = (JSAMPARRAY) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (cinfo->num_components * 5 * rgroup_height) * + SIZEOF(JSAMPROW)); + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Allocate the actual buffer space (3 row groups) for this component. + * We make the buffer wide enough to allow the downsampler to edge-expand + * horizontally within the buffer, if it so chooses. + */ + true_buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) (((long) compptr->width_in_blocks * + cinfo->min_DCT_h_scaled_size * + cinfo->max_h_samp_factor) / compptr->h_samp_factor), + (JDIMENSION) (3 * rgroup_height)); + /* Copy true buffer row pointers into the middle of the fake row array */ + MEMCOPY(fake_buffer + rgroup_height, true_buffer, + 3 * rgroup_height * SIZEOF(JSAMPROW)); + /* Fill in the above and below wraparound pointers */ + for (i = 0; i < rgroup_height; i++) { + fake_buffer[i] = true_buffer[2 * rgroup_height + i]; + fake_buffer[4 * rgroup_height + i] = true_buffer[i]; + } + prep->color_buf[ci] = fake_buffer + rgroup_height; + fake_buffer += 5 * rgroup_height; /* point to space for next component */ + } +} + +#endif /* CONTEXT_ROWS_SUPPORTED */ + + +/* + * Initialize preprocessing controller. + */ + +GLOBAL(void) +jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer) +{ + my_prep_ptr prep; + int ci; + jpeg_component_info * compptr; + + if (need_full_buffer) /* safety check */ + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + + prep = (my_prep_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_prep_controller)); + cinfo->prep = (struct jpeg_c_prep_controller *) prep; + prep->pub.start_pass = start_pass_prep; + + /* Allocate the color conversion buffer. + * We make the buffer wide enough to allow the downsampler to edge-expand + * horizontally within the buffer, if it so chooses. + */ + if (cinfo->downsample->need_context_rows) { + /* Set up to provide context rows */ +#ifdef CONTEXT_ROWS_SUPPORTED + prep->pub.pre_process_data = pre_process_context; + create_context_buffer(cinfo); +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } else { + /* No context, just make it tall enough for one row group */ + prep->pub.pre_process_data = pre_process_data; + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + prep->color_buf[ci] = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) (((long) compptr->width_in_blocks * + cinfo->min_DCT_h_scaled_size * + cinfo->max_h_samp_factor) / compptr->h_samp_factor), + (JDIMENSION) cinfo->max_v_samp_factor); + } + } +} diff --git a/crypto777/jpeg/jcsample.c b/crypto777/jpeg/jcsample.c new file mode 100644 index 000000000..4d36f85f3 --- /dev/null +++ b/crypto777/jpeg/jcsample.c @@ -0,0 +1,545 @@ +/* + * jcsample.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains downsampling routines. + * + * Downsampling input data is counted in "row groups". A row group + * is defined to be max_v_samp_factor pixel rows of each component, + * from which the downsampler produces v_samp_factor sample rows. + * A single row group is processed in each call to the downsampler module. + * + * The downsampler is responsible for edge-expansion of its output data + * to fill an integral number of DCT blocks horizontally. The source buffer + * may be modified if it is helpful for this purpose (the source buffer is + * allocated wide enough to correspond to the desired output width). + * The caller (the prep controller) is responsible for vertical padding. + * + * The downsampler may request "context rows" by setting need_context_rows + * during startup. In this case, the input arrays will contain at least + * one row group's worth of pixels above and below the passed-in data; + * the caller will create dummy rows at image top and bottom by replicating + * the first or last real pixel row. + * + * An excellent reference for image resampling is + * Digital Image Warping, George Wolberg, 1990. + * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. + * + * The downsampling algorithm used here is a simple average of the source + * pixels covered by the output pixel. The hi-falutin sampling literature + * refers to this as a "box filter". In general the characteristics of a box + * filter are not very good, but for the specific cases we normally use (1:1 + * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not + * nearly so bad. If you intend to use other sampling ratios, you'd be well + * advised to improve this code. + * + * A simple input-smoothing capability is provided. This is mainly intended + * for cleaning up color-dithered GIF input files (if you find it inadequate, + * we suggest using an external filtering program such as pnmconvol). When + * enabled, each input pixel P is replaced by a weighted sum of itself and its + * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF, + * where SF = (smoothing_factor / 1024). + * Currently, smoothing is only supported for 2h2v sampling factors. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Pointer to routine to downsample a single component */ +typedef JMETHOD(void, downsample1_ptr, + (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data)); + +/* Private subobject */ + +typedef struct { + struct jpeg_downsampler pub; /* public fields */ + + /* Downsampling method pointers, one per component */ + downsample1_ptr methods[MAX_COMPONENTS]; + + /* Height of an output row group for each component. */ + int rowgroup_height[MAX_COMPONENTS]; + + /* These arrays save pixel expansion factors so that int_downsample need not + * recompute them each time. They are unused for other downsampling methods. + */ + UINT8 h_expand[MAX_COMPONENTS]; + UINT8 v_expand[MAX_COMPONENTS]; +} my_downsampler; + +typedef my_downsampler * my_downsample_ptr; + + +/* + * Initialize for a downsampling pass. + */ + +METHODDEF(void) +start_pass_downsample (j_compress_ptr cinfo) +{ + /* no work for now */ +} + + +/* + * Expand a component horizontally from width input_cols to width output_cols, + * by duplicating the rightmost samples. + */ + +LOCAL(void) +expand_right_edge (JSAMPARRAY image_data, int num_rows, + JDIMENSION input_cols, JDIMENSION output_cols) +{ + register JSAMPROW ptr; + register JSAMPLE pixval; + register int count; + int row; + int numcols = (int) (output_cols - input_cols); + + if (numcols > 0) { + for (row = 0; row < num_rows; row++) { + ptr = image_data[row] + input_cols; + pixval = ptr[-1]; /* don't need GETJSAMPLE() here */ + for (count = numcols; count > 0; count--) + *ptr++ = pixval; + } + } +} + + +/* + * Do downsampling for a whole row group (all components). + * + * In this version we simply downsample each component independently. + */ + +METHODDEF(void) +sep_downsample (j_compress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION in_row_index, + JSAMPIMAGE output_buf, JDIMENSION out_row_group_index) +{ + my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample; + int ci; + jpeg_component_info * compptr; + JSAMPARRAY in_ptr, out_ptr; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + in_ptr = input_buf[ci] + in_row_index; + out_ptr = output_buf[ci] + + (out_row_group_index * downsample->rowgroup_height[ci]); + (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr); + } +} + + +/* + * Downsample pixel values of a single component. + * One row group is processed per call. + * This version handles arbitrary integral sampling ratios, without smoothing. + * Note that this version is not actually used for customary sampling ratios. + */ + +METHODDEF(void) +int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data) +{ + my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample; + int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v; + JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */ + JDIMENSION output_cols = compptr->width_in_blocks * compptr->DCT_h_scaled_size; + JSAMPROW inptr, outptr; + INT32 outvalue; + + h_expand = downsample->h_expand[compptr->component_index]; + v_expand = downsample->v_expand[compptr->component_index]; + numpix = h_expand * v_expand; + numpix2 = numpix/2; + + /* Expand input data enough to let all the output samples be generated + * by the standard loop. Special-casing padded output would be more + * efficient. + */ + expand_right_edge(input_data, cinfo->max_v_samp_factor, + cinfo->image_width, output_cols * h_expand); + + inrow = outrow = 0; + while (inrow < cinfo->max_v_samp_factor) { + outptr = output_data[outrow]; + for (outcol = 0, outcol_h = 0; outcol < output_cols; + outcol++, outcol_h += h_expand) { + outvalue = 0; + for (v = 0; v < v_expand; v++) { + inptr = input_data[inrow+v] + outcol_h; + for (h = 0; h < h_expand; h++) { + outvalue += (INT32) GETJSAMPLE(*inptr++); + } + } + *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix); + } + inrow += v_expand; + outrow++; + } +} + + +/* + * Downsample pixel values of a single component. + * This version handles the special case of a full-size component, + * without smoothing. + */ + +METHODDEF(void) +fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data) +{ + /* Copy the data */ + jcopy_sample_rows(input_data, 0, output_data, 0, + cinfo->max_v_samp_factor, cinfo->image_width); + /* Edge-expand */ + expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width, + compptr->width_in_blocks * compptr->DCT_h_scaled_size); +} + + +/* + * Downsample pixel values of a single component. + * This version handles the common case of 2:1 horizontal and 1:1 vertical, + * without smoothing. + * + * A note about the "bias" calculations: when rounding fractional values to + * integer, we do not want to always round 0.5 up to the next integer. + * If we did that, we'd introduce a noticeable bias towards larger values. + * Instead, this code is arranged so that 0.5 will be rounded up or down at + * alternate pixel locations (a simple ordered dither pattern). + */ + +METHODDEF(void) +h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data) +{ + int inrow; + JDIMENSION outcol; + JDIMENSION output_cols = compptr->width_in_blocks * compptr->DCT_h_scaled_size; + register JSAMPROW inptr, outptr; + register int bias; + + /* Expand input data enough to let all the output samples be generated + * by the standard loop. Special-casing padded output would be more + * efficient. + */ + expand_right_edge(input_data, cinfo->max_v_samp_factor, + cinfo->image_width, output_cols * 2); + + for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { + outptr = output_data[inrow]; + inptr = input_data[inrow]; + bias = 0; /* bias = 0,1,0,1,... for successive samples */ + for (outcol = 0; outcol < output_cols; outcol++) { + *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1]) + + bias) >> 1); + bias ^= 1; /* 0=>1, 1=>0 */ + inptr += 2; + } + } +} + + +/* + * Downsample pixel values of a single component. + * This version handles the standard case of 2:1 horizontal and 2:1 vertical, + * without smoothing. + */ + +METHODDEF(void) +h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data) +{ + int inrow, outrow; + JDIMENSION outcol; + JDIMENSION output_cols = compptr->width_in_blocks * compptr->DCT_h_scaled_size; + register JSAMPROW inptr0, inptr1, outptr; + register int bias; + + /* Expand input data enough to let all the output samples be generated + * by the standard loop. Special-casing padded output would be more + * efficient. + */ + expand_right_edge(input_data, cinfo->max_v_samp_factor, + cinfo->image_width, output_cols * 2); + + inrow = outrow = 0; + while (inrow < cinfo->max_v_samp_factor) { + outptr = output_data[outrow]; + inptr0 = input_data[inrow]; + inptr1 = input_data[inrow+1]; + bias = 1; /* bias = 1,2,1,2,... for successive samples */ + for (outcol = 0; outcol < output_cols; outcol++) { + *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]) + + bias) >> 2); + bias ^= 3; /* 1=>2, 2=>1 */ + inptr0 += 2; inptr1 += 2; + } + inrow += 2; + outrow++; + } +} + + +#ifdef INPUT_SMOOTHING_SUPPORTED + +/* + * Downsample pixel values of a single component. + * This version handles the standard case of 2:1 horizontal and 2:1 vertical, + * with smoothing. One row of context is required. + */ + +METHODDEF(void) +h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data) +{ + int inrow, outrow; + JDIMENSION colctr; + JDIMENSION output_cols = compptr->width_in_blocks * compptr->DCT_h_scaled_size; + register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr; + INT32 membersum, neighsum, memberscale, neighscale; + + /* Expand input data enough to let all the output samples be generated + * by the standard loop. Special-casing padded output would be more + * efficient. + */ + expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, + cinfo->image_width, output_cols * 2); + + /* We don't bother to form the individual "smoothed" input pixel values; + * we can directly compute the output which is the average of the four + * smoothed values. Each of the four member pixels contributes a fraction + * (1-8*SF) to its own smoothed image and a fraction SF to each of the three + * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final + * output. The four corner-adjacent neighbor pixels contribute a fraction + * SF to just one smoothed pixel, or SF/4 to the final output; while the + * eight edge-adjacent neighbors contribute SF to each of two smoothed + * pixels, or SF/2 overall. In order to use integer arithmetic, these + * factors are scaled by 2^16 = 65536. + * Also recall that SF = smoothing_factor / 1024. + */ + + memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */ + neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */ + + inrow = outrow = 0; + while (inrow < cinfo->max_v_samp_factor) { + outptr = output_data[outrow]; + inptr0 = input_data[inrow]; + inptr1 = input_data[inrow+1]; + above_ptr = input_data[inrow-1]; + below_ptr = input_data[inrow+2]; + + /* Special case for first column: pretend column -1 is same as column 0 */ + membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); + neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + + GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) + + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]); + neighsum += neighsum; + neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) + + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]); + membersum = membersum * memberscale + neighsum * neighscale; + *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); + inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; + + for (colctr = output_cols - 2; colctr > 0; colctr--) { + /* sum of pixels directly mapped to this output element */ + membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); + /* sum of edge-neighbor pixels */ + neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + + GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) + + GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]); + /* The edge-neighbors count twice as much as corner-neighbors */ + neighsum += neighsum; + /* Add in the corner-neighbors */ + neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) + + GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]); + /* form final output scaled up by 2^16 */ + membersum = membersum * memberscale + neighsum * neighscale; + /* round, descale and output it */ + *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); + inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; + } + + /* Special case for last column */ + membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); + neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + + GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) + + GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]); + neighsum += neighsum; + neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) + + GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]); + membersum = membersum * memberscale + neighsum * neighscale; + *outptr = (JSAMPLE) ((membersum + 32768) >> 16); + + inrow += 2; + outrow++; + } +} + + +/* + * Downsample pixel values of a single component. + * This version handles the special case of a full-size component, + * with smoothing. One row of context is required. + */ + +METHODDEF(void) +fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, + JSAMPARRAY input_data, JSAMPARRAY output_data) +{ + int inrow; + JDIMENSION colctr; + JDIMENSION output_cols = compptr->width_in_blocks * compptr->DCT_h_scaled_size; + register JSAMPROW inptr, above_ptr, below_ptr, outptr; + INT32 membersum, neighsum, memberscale, neighscale; + int colsum, lastcolsum, nextcolsum; + + /* Expand input data enough to let all the output samples be generated + * by the standard loop. Special-casing padded output would be more + * efficient. + */ + expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, + cinfo->image_width, output_cols); + + /* Each of the eight neighbor pixels contributes a fraction SF to the + * smoothed pixel, while the main pixel contributes (1-8*SF). In order + * to use integer arithmetic, these factors are multiplied by 2^16 = 65536. + * Also recall that SF = smoothing_factor / 1024. + */ + + memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */ + neighscale = cinfo->smoothing_factor * 64; /* scaled SF */ + + for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { + outptr = output_data[inrow]; + inptr = input_data[inrow]; + above_ptr = input_data[inrow-1]; + below_ptr = input_data[inrow+1]; + + /* Special case for first column */ + colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) + + GETJSAMPLE(*inptr); + membersum = GETJSAMPLE(*inptr++); + nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + + GETJSAMPLE(*inptr); + neighsum = colsum + (colsum - membersum) + nextcolsum; + membersum = membersum * memberscale + neighsum * neighscale; + *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); + lastcolsum = colsum; colsum = nextcolsum; + + for (colctr = output_cols - 2; colctr > 0; colctr--) { + membersum = GETJSAMPLE(*inptr++); + above_ptr++; below_ptr++; + nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + + GETJSAMPLE(*inptr); + neighsum = lastcolsum + (colsum - membersum) + nextcolsum; + membersum = membersum * memberscale + neighsum * neighscale; + *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); + lastcolsum = colsum; colsum = nextcolsum; + } + + /* Special case for last column */ + membersum = GETJSAMPLE(*inptr); + neighsum = lastcolsum + (colsum - membersum) + colsum; + membersum = membersum * memberscale + neighsum * neighscale; + *outptr = (JSAMPLE) ((membersum + 32768) >> 16); + + } +} + +#endif /* INPUT_SMOOTHING_SUPPORTED */ + + +/* + * Module initialization routine for downsampling. + * Note that we must select a routine for each component. + */ + +GLOBAL(void) +jinit_downsampler (j_compress_ptr cinfo) +{ + my_downsample_ptr downsample; + int ci; + jpeg_component_info * compptr; + boolean smoothok = TRUE; + int h_in_group, v_in_group, h_out_group, v_out_group; + + downsample = (my_downsample_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_downsampler)); + cinfo->downsample = (struct jpeg_downsampler *) downsample; + downsample->pub.start_pass = start_pass_downsample; + downsample->pub.downsample = sep_downsample; + downsample->pub.need_context_rows = FALSE; + + if (cinfo->CCIR601_sampling) + ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); + + /* Verify we can handle the sampling factors, and set up method pointers */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Compute size of an "output group" for DCT scaling. This many samples + * are to be converted from max_h_samp_factor * max_v_samp_factor pixels. + */ + h_out_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) / + cinfo->min_DCT_h_scaled_size; + v_out_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; + h_in_group = cinfo->max_h_samp_factor; + v_in_group = cinfo->max_v_samp_factor; + downsample->rowgroup_height[ci] = v_out_group; /* save for use later */ + if (h_in_group == h_out_group && v_in_group == v_out_group) { +#ifdef INPUT_SMOOTHING_SUPPORTED + if (cinfo->smoothing_factor) { + downsample->methods[ci] = fullsize_smooth_downsample; + downsample->pub.need_context_rows = TRUE; + } else +#endif + downsample->methods[ci] = fullsize_downsample; + } else if (h_in_group == h_out_group * 2 && + v_in_group == v_out_group) { + smoothok = FALSE; + downsample->methods[ci] = h2v1_downsample; + } else if (h_in_group == h_out_group * 2 && + v_in_group == v_out_group * 2) { +#ifdef INPUT_SMOOTHING_SUPPORTED + if (cinfo->smoothing_factor) { + downsample->methods[ci] = h2v2_smooth_downsample; + downsample->pub.need_context_rows = TRUE; + } else +#endif + downsample->methods[ci] = h2v2_downsample; + } else if ((h_in_group % h_out_group) == 0 && + (v_in_group % v_out_group) == 0) { + smoothok = FALSE; + downsample->methods[ci] = int_downsample; + downsample->h_expand[ci] = (UINT8) (h_in_group / h_out_group); + downsample->v_expand[ci] = (UINT8) (v_in_group / v_out_group); + } else + ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); + } + +#ifdef INPUT_SMOOTHING_SUPPORTED + if (cinfo->smoothing_factor && !smoothok) + TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL); +#endif +} diff --git a/crypto777/jpeg/jctrans.c b/crypto777/jpeg/jctrans.c new file mode 100644 index 000000000..f7d7b8149 --- /dev/null +++ b/crypto777/jpeg/jctrans.c @@ -0,0 +1,382 @@ +/* + * jctrans.c + * + * Copyright (C) 1995-1998, Thomas G. Lane. + * Modified 2000-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains library routines for transcoding compression, + * that is, writing raw DCT coefficient arrays to an output JPEG file. + * The routines in jcapimin.c will also be needed by a transcoder. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Forward declarations */ +LOCAL(void) transencode_master_selection + JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); +LOCAL(void) transencode_coef_controller + JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); + + +/* + * Compression initialization for writing raw-coefficient data. + * Before calling this, all parameters and a data destination must be set up. + * Call jpeg_finish_compress() to actually write the data. + * + * The number of passed virtual arrays must match cinfo->num_components. + * Note that the virtual arrays need not be filled or even realized at + * the time write_coefficients is called; indeed, if the virtual arrays + * were requested from this compression object's memory manager, they + * typically will be realized during this routine and filled afterwards. + */ + +GLOBAL(void) +jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays) +{ + if (cinfo->global_state != CSTATE_START) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + /* Mark all tables to be written */ + jpeg_suppress_tables(cinfo, FALSE); + /* (Re)initialize error mgr and destination modules */ + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); + (*cinfo->dest->init_destination) (cinfo); + /* Perform master selection of active modules */ + transencode_master_selection(cinfo, coef_arrays); + /* Wait for jpeg_finish_compress() call */ + cinfo->next_scanline = 0; /* so jpeg_write_marker works */ + cinfo->global_state = CSTATE_WRCOEFS; +} + + +/* + * Initialize the compression object with default parameters, + * then copy from the source object all parameters needed for lossless + * transcoding. Parameters that can be varied without loss (such as + * scan script and Huffman optimization) are left in their default states. + */ + +GLOBAL(void) +jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, + j_compress_ptr dstinfo) +{ + JQUANT_TBL ** qtblptr; + jpeg_component_info *incomp, *outcomp; + JQUANT_TBL *c_quant, *slot_quant; + int tblno, ci, coefi; + + /* Safety check to ensure start_compress not called yet. */ + if (dstinfo->global_state != CSTATE_START) + ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state); + /* Copy fundamental image dimensions */ + dstinfo->image_width = srcinfo->image_width; + dstinfo->image_height = srcinfo->image_height; + dstinfo->input_components = srcinfo->num_components; + dstinfo->in_color_space = srcinfo->jpeg_color_space; + dstinfo->jpeg_width = srcinfo->output_width; + dstinfo->jpeg_height = srcinfo->output_height; + dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size; + dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size; + /* Initialize all parameters to default values */ + jpeg_set_defaults(dstinfo); + /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. + * Fix it to get the right header markers for the image colorspace. + */ + jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); + dstinfo->data_precision = srcinfo->data_precision; + dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling; + /* Copy the source's quantization tables. */ + for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { + if (srcinfo->quant_tbl_ptrs[tblno] != NULL) { + qtblptr = & dstinfo->quant_tbl_ptrs[tblno]; + if (*qtblptr == NULL) + *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo); + MEMCOPY((*qtblptr)->quantval, + srcinfo->quant_tbl_ptrs[tblno]->quantval, + SIZEOF((*qtblptr)->quantval)); + (*qtblptr)->sent_table = FALSE; + } + } + /* Copy the source's per-component info. + * Note we assume jpeg_set_defaults has allocated the dest comp_info array. + */ + dstinfo->num_components = srcinfo->num_components; + if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS) + ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components, + MAX_COMPONENTS); + for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info; + ci < dstinfo->num_components; ci++, incomp++, outcomp++) { + outcomp->component_id = incomp->component_id; + outcomp->h_samp_factor = incomp->h_samp_factor; + outcomp->v_samp_factor = incomp->v_samp_factor; + outcomp->quant_tbl_no = incomp->quant_tbl_no; + /* Make sure saved quantization table for component matches the qtable + * slot. If not, the input file re-used this qtable slot. + * IJG encoder currently cannot duplicate this. + */ + tblno = outcomp->quant_tbl_no; + if (tblno < 0 || tblno >= NUM_QUANT_TBLS || + srcinfo->quant_tbl_ptrs[tblno] == NULL) + ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno); + slot_quant = srcinfo->quant_tbl_ptrs[tblno]; + c_quant = incomp->quant_table; + if (c_quant != NULL) { + for (coefi = 0; coefi < DCTSIZE2; coefi++) { + if (c_quant->quantval[coefi] != slot_quant->quantval[coefi]) + ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno); + } + } + /* Note: we do not copy the source's Huffman table assignments; + * instead we rely on jpeg_set_colorspace to have made a suitable choice. + */ + } + /* Also copy JFIF version and resolution information, if available. + * Strictly speaking this isn't "critical" info, but it's nearly + * always appropriate to copy it if available. In particular, + * if the application chooses to copy JFIF 1.02 extension markers from + * the source file, we need to copy the version to make sure we don't + * emit a file that has 1.02 extensions but a claimed version of 1.01. + * We will *not*, however, copy version info from mislabeled "2.01" files. + */ + if (srcinfo->saw_JFIF_marker) { + if (srcinfo->JFIF_major_version == 1) { + dstinfo->JFIF_major_version = srcinfo->JFIF_major_version; + dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version; + } + dstinfo->density_unit = srcinfo->density_unit; + dstinfo->X_density = srcinfo->X_density; + dstinfo->Y_density = srcinfo->Y_density; + } +} + + +/* + * Master selection of compression modules for transcoding. + * This substitutes for jcinit.c's initialization of the full compressor. + */ + +LOCAL(void) +transencode_master_selection (j_compress_ptr cinfo, + jvirt_barray_ptr * coef_arrays) +{ + /* Initialize master control (includes parameter checking/processing) */ + jinit_c_master_control(cinfo, TRUE /* transcode only */); + + /* Entropy encoding: either Huffman or arithmetic coding. */ + if (cinfo->arith_code) + jinit_arith_encoder(cinfo); + else { + jinit_huff_encoder(cinfo); + } + + /* We need a special coefficient buffer controller. */ + transencode_coef_controller(cinfo, coef_arrays); + + jinit_marker_writer(cinfo); + + /* We can now tell the memory manager to allocate virtual arrays. */ + (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); + + /* Write the datastream header (SOI, JFIF) immediately. + * Frame and scan headers are postponed till later. + * This lets application insert special markers after the SOI. + */ + (*cinfo->marker->write_file_header) (cinfo); +} + + +/* + * The rest of this file is a special implementation of the coefficient + * buffer controller. This is similar to jccoefct.c, but it handles only + * output from presupplied virtual arrays. Furthermore, we generate any + * dummy padding blocks on-the-fly rather than expecting them to be present + * in the arrays. + */ + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_c_coef_controller pub; /* public fields */ + + JDIMENSION iMCU_row_num; /* iMCU row # within image */ + JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ + int MCU_vert_offset; /* counts MCU rows within iMCU row */ + int MCU_rows_per_iMCU_row; /* number of such rows needed */ + + /* Virtual block array for each component. */ + jvirt_barray_ptr * whole_image; + + /* Workspace for constructing dummy blocks at right/bottom edges. */ + JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU]; +} my_coef_controller; + +typedef my_coef_controller * my_coef_ptr; + + +LOCAL(void) +start_iMCU_row (j_compress_ptr cinfo) +/* Reset within-iMCU-row counters for a new row */ +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + + /* In an interleaved scan, an MCU row is the same as an iMCU row. + * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. + * But at the bottom of the image, process only what's left. + */ + if (cinfo->comps_in_scan > 1) { + coef->MCU_rows_per_iMCU_row = 1; + } else { + if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1)) + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; + else + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; + } + + coef->mcu_ctr = 0; + coef->MCU_vert_offset = 0; +} + + +/* + * Initialize for a processing pass. + */ + +METHODDEF(void) +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + + if (pass_mode != JBUF_CRANK_DEST) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + + coef->iMCU_row_num = 0; + start_iMCU_row(cinfo); +} + + +/* + * Process some data. + * We process the equivalent of one fully interleaved MCU row ("iMCU" row) + * per call, ie, v_samp_factor block rows for each component in the scan. + * The data is obtained from the virtual arrays and fed to the entropy coder. + * Returns TRUE if the iMCU row is completed, FALSE if suspended. + * + * NB: input_buf is ignored; it is likely to be a NULL pointer. + */ + +METHODDEF(boolean) +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION MCU_col_num; /* index of current MCU within row */ + JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; + int blkn, ci, xindex, yindex, yoffset, blockcnt; + JDIMENSION start_col; + JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; + JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; + JBLOCKROW buffer_ptr; + jpeg_component_info *compptr; + + /* Align the virtual buffers for the components used in this scan. */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + buffer[ci] = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], + coef->iMCU_row_num * compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, FALSE); + } + + /* Loop to process one whole iMCU row */ + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; + yoffset++) { + for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; + MCU_col_num++) { + /* Construct list of pointers to DCT blocks belonging to this MCU */ + blkn = 0; /* index of current DCT block within MCU */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + start_col = MCU_col_num * compptr->MCU_width; + blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width + : compptr->last_col_width; + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { + if (coef->iMCU_row_num < last_iMCU_row || + yindex+yoffset < compptr->last_row_height) { + /* Fill in pointers to real blocks in this row */ + buffer_ptr = buffer[ci][yindex+yoffset] + start_col; + for (xindex = 0; xindex < blockcnt; xindex++) + MCU_buffer[blkn++] = buffer_ptr++; + } else { + /* At bottom of image, need a whole row of dummy blocks */ + xindex = 0; + } + /* Fill in any dummy blocks needed in this row. + * Dummy blocks are filled in the same way as in jccoefct.c: + * all zeroes in the AC entries, DC entries equal to previous + * block's DC value. The init routine has already zeroed the + * AC entries, so we need only set the DC entries correctly. + */ + for (; xindex < compptr->MCU_width; xindex++) { + MCU_buffer[blkn] = coef->dummy_buffer[blkn]; + MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0]; + blkn++; + } + } + } + /* Try to write the MCU. */ + if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) { + /* Suspension forced; update state counters and exit */ + coef->MCU_vert_offset = yoffset; + coef->mcu_ctr = MCU_col_num; + return FALSE; + } + } + /* Completed an MCU row, but perhaps not an iMCU row */ + coef->mcu_ctr = 0; + } + /* Completed the iMCU row, advance counters for next one */ + coef->iMCU_row_num++; + start_iMCU_row(cinfo); + return TRUE; +} + + +/* + * Initialize coefficient buffer controller. + * + * Each passed coefficient array must be the right size for that + * coefficient: width_in_blocks wide and height_in_blocks high, + * with unitheight at least v_samp_factor. + */ + +LOCAL(void) +transencode_coef_controller (j_compress_ptr cinfo, + jvirt_barray_ptr * coef_arrays) +{ + my_coef_ptr coef; + JBLOCKROW buffer; + int i; + + coef = (my_coef_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_coef_controller)); + cinfo->coef = (struct jpeg_c_coef_controller *) coef; + coef->pub.start_pass = start_pass_coef; + coef->pub.compress_data = compress_output; + + /* Save pointer to virtual arrays */ + coef->whole_image = coef_arrays; + + /* Allocate and pre-zero space for dummy DCT blocks. */ + buffer = (JBLOCKROW) + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, + C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); + FMEMZERO((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); + for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { + coef->dummy_buffer[i] = buffer + i; + } +} diff --git a/crypto777/jpeg/jdapimin.c b/crypto777/jpeg/jdapimin.c new file mode 100644 index 000000000..7f1ce4c05 --- /dev/null +++ b/crypto777/jpeg/jdapimin.c @@ -0,0 +1,396 @@ +/* + * jdapimin.c + * + * Copyright (C) 1994-1998, Thomas G. Lane. + * Modified 2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains application interface code for the decompression half + * of the JPEG library. These are the "minimum" API routines that may be + * needed in either the normal full-decompression case or the + * transcoding-only case. + * + * Most of the routines intended to be called directly by an application + * are in this file or in jdapistd.c. But also see jcomapi.c for routines + * shared by compression and decompression, and jdtrans.c for the transcoding + * case. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * Initialization of a JPEG decompression object. + * The error manager must already be set up (in case memory manager fails). + */ + +GLOBAL(void) +jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize) +{ + int i; + + /* Guard against version mismatches between library and caller. */ + cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ + if (version != JPEG_LIB_VERSION) + ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); + if (structsize != SIZEOF(struct jpeg_decompress_struct)) + ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, + (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize); + + /* For debugging purposes, we zero the whole master structure. + * But the application has already set the err pointer, and may have set + * client_data, so we have to save and restore those fields. + * Note: if application hasn't set client_data, tools like Purify may + * complain here. + */ + { + struct jpeg_error_mgr * err = cinfo->err; + void * client_data = cinfo->client_data; /* ignore Purify complaint here */ + MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct)); + cinfo->err = err; + cinfo->client_data = client_data; + } + cinfo->is_decompressor = TRUE; + + /* Initialize a memory manager instance for this object */ + jinit_memory_mgr((j_common_ptr) cinfo); + + /* Zero out pointers to permanent structures. */ + cinfo->progress = NULL; + cinfo->src = NULL; + + for (i = 0; i < NUM_QUANT_TBLS; i++) + cinfo->quant_tbl_ptrs[i] = NULL; + + for (i = 0; i < NUM_HUFF_TBLS; i++) { + cinfo->dc_huff_tbl_ptrs[i] = NULL; + cinfo->ac_huff_tbl_ptrs[i] = NULL; + } + + /* Initialize marker processor so application can override methods + * for COM, APPn markers before calling jpeg_read_header. + */ + cinfo->marker_list = NULL; + jinit_marker_reader(cinfo); + + /* And initialize the overall input controller. */ + jinit_input_controller(cinfo); + + /* OK, I'm ready */ + cinfo->global_state = DSTATE_START; +} + + +/* + * Destruction of a JPEG decompression object + */ + +GLOBAL(void) +jpeg_destroy_decompress (j_decompress_ptr cinfo) +{ + jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ +} + + +/* + * Abort processing of a JPEG decompression operation, + * but don't destroy the object itself. + */ + +GLOBAL(void) +jpeg_abort_decompress (j_decompress_ptr cinfo) +{ + jpeg_abort((j_common_ptr) cinfo); /* use common routine */ +} + + +/* + * Set default decompression parameters. + */ + +LOCAL(void) +default_decompress_parms (j_decompress_ptr cinfo) +{ + /* Guess the input colorspace, and set output colorspace accordingly. */ + /* (Wish JPEG committee had provided a real way to specify this...) */ + /* Note application may override our guesses. */ + switch (cinfo->num_components) { + case 1: + cinfo->jpeg_color_space = JCS_GRAYSCALE; + cinfo->out_color_space = JCS_GRAYSCALE; + break; + + case 3: + if (cinfo->saw_JFIF_marker) { + cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */ + } else if (cinfo->saw_Adobe_marker) { + switch (cinfo->Adobe_transform) { + case 0: + cinfo->jpeg_color_space = JCS_RGB; + break; + case 1: + cinfo->jpeg_color_space = JCS_YCbCr; + break; + default: + WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); + cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ + break; + } + } else { + /* Saw no special markers, try to guess from the component IDs */ + int cid0 = cinfo->comp_info[0].component_id; + int cid1 = cinfo->comp_info[1].component_id; + int cid2 = cinfo->comp_info[2].component_id; + + if (cid0 == 1 && cid1 == 2 && cid2 == 3) + cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */ + else if (cid0 == 82 && cid1 == 71 && cid2 == 66) + cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */ + else { + TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2); + cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ + } + } + /* Always guess RGB is proper output colorspace. */ + cinfo->out_color_space = JCS_RGB; + break; + + case 4: + if (cinfo->saw_Adobe_marker) { + switch (cinfo->Adobe_transform) { + case 0: + cinfo->jpeg_color_space = JCS_CMYK; + break; + case 2: + cinfo->jpeg_color_space = JCS_YCCK; + break; + default: + WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); + cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */ + break; + } + } else { + /* No special markers, assume straight CMYK. */ + cinfo->jpeg_color_space = JCS_CMYK; + } + cinfo->out_color_space = JCS_CMYK; + break; + + default: + cinfo->jpeg_color_space = JCS_UNKNOWN; + cinfo->out_color_space = JCS_UNKNOWN; + break; + } + + /* Set defaults for other decompression parameters. */ + cinfo->scale_num = cinfo->block_size; /* 1:1 scaling */ + cinfo->scale_denom = cinfo->block_size; + cinfo->output_gamma = 1.0; + cinfo->buffered_image = FALSE; + cinfo->raw_data_out = FALSE; + cinfo->dct_method = JDCT_DEFAULT; + cinfo->do_fancy_upsampling = TRUE; + cinfo->do_block_smoothing = TRUE; + cinfo->quantize_colors = FALSE; + /* We set these in case application only sets quantize_colors. */ + cinfo->dither_mode = JDITHER_FS; +#ifdef QUANT_2PASS_SUPPORTED + cinfo->two_pass_quantize = TRUE; +#else + cinfo->two_pass_quantize = FALSE; +#endif + cinfo->desired_number_of_colors = 256; + cinfo->colormap = NULL; + /* Initialize for no mode change in buffered-image mode. */ + cinfo->enable_1pass_quant = FALSE; + cinfo->enable_external_quant = FALSE; + cinfo->enable_2pass_quant = FALSE; +} + + +/* + * Decompression startup: read start of JPEG datastream to see what's there. + * Need only initialize JPEG object and supply a data source before calling. + * + * This routine will read as far as the first SOS marker (ie, actual start of + * compressed data), and will save all tables and parameters in the JPEG + * object. It will also initialize the decompression parameters to default + * values, and finally return JPEG_HEADER_OK. On return, the application may + * adjust the decompression parameters and then call jpeg_start_decompress. + * (Or, if the application only wanted to determine the image parameters, + * the data need not be decompressed. In that case, call jpeg_abort or + * jpeg_destroy to release any temporary space.) + * If an abbreviated (tables only) datastream is presented, the routine will + * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then + * re-use the JPEG object to read the abbreviated image datastream(s). + * It is unnecessary (but OK) to call jpeg_abort in this case. + * The JPEG_SUSPENDED return code only occurs if the data source module + * requests suspension of the decompressor. In this case the application + * should load more source data and then re-call jpeg_read_header to resume + * processing. + * If a non-suspending data source is used and require_image is TRUE, then the + * return code need not be inspected since only JPEG_HEADER_OK is possible. + * + * This routine is now just a front end to jpeg_consume_input, with some + * extra error checking. + */ + +GLOBAL(int) +jpeg_read_header (j_decompress_ptr cinfo, boolean require_image) +{ + int retcode; + + if (cinfo->global_state != DSTATE_START && + cinfo->global_state != DSTATE_INHEADER) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + retcode = jpeg_consume_input(cinfo); + + switch (retcode) { + case JPEG_REACHED_SOS: + retcode = JPEG_HEADER_OK; + break; + case JPEG_REACHED_EOI: + if (require_image) /* Complain if application wanted an image */ + ERREXIT(cinfo, JERR_NO_IMAGE); + /* Reset to start state; it would be safer to require the application to + * call jpeg_abort, but we can't change it now for compatibility reasons. + * A side effect is to free any temporary memory (there shouldn't be any). + */ + jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */ + retcode = JPEG_HEADER_TABLES_ONLY; + break; + case JPEG_SUSPENDED: + /* no work */ + break; + } + + return retcode; +} + + +/* + * Consume data in advance of what the decompressor requires. + * This can be called at any time once the decompressor object has + * been created and a data source has been set up. + * + * This routine is essentially a state machine that handles a couple + * of critical state-transition actions, namely initial setup and + * transition from header scanning to ready-for-start_decompress. + * All the actual input is done via the input controller's consume_input + * method. + */ + +GLOBAL(int) +jpeg_consume_input (j_decompress_ptr cinfo) +{ + int retcode = JPEG_SUSPENDED; + + /* NB: every possible DSTATE value should be listed in this switch */ + switch (cinfo->global_state) { + case DSTATE_START: + /* Start-of-datastream actions: reset appropriate modules */ + (*cinfo->inputctl->reset_input_controller) (cinfo); + /* Initialize application's data source module */ + (*cinfo->src->init_source) (cinfo); + cinfo->global_state = DSTATE_INHEADER; + /*FALLTHROUGH*/ + case DSTATE_INHEADER: + retcode = (*cinfo->inputctl->consume_input) (cinfo); + if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */ + /* Set up default parameters based on header data */ + default_decompress_parms(cinfo); + /* Set global state: ready for start_decompress */ + cinfo->global_state = DSTATE_READY; + } + break; + case DSTATE_READY: + /* Can't advance past first SOS until start_decompress is called */ + retcode = JPEG_REACHED_SOS; + break; + case DSTATE_PRELOAD: + case DSTATE_PRESCAN: + case DSTATE_SCANNING: + case DSTATE_RAW_OK: + case DSTATE_BUFIMAGE: + case DSTATE_BUFPOST: + case DSTATE_STOPPING: + retcode = (*cinfo->inputctl->consume_input) (cinfo); + break; + default: + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } + return retcode; +} + + +/* + * Have we finished reading the input file? + */ + +GLOBAL(boolean) +jpeg_input_complete (j_decompress_ptr cinfo) +{ + /* Check for valid jpeg object */ + if (cinfo->global_state < DSTATE_START || + cinfo->global_state > DSTATE_STOPPING) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + return cinfo->inputctl->eoi_reached; +} + + +/* + * Is there more than one scan? + */ + +GLOBAL(boolean) +jpeg_has_multiple_scans (j_decompress_ptr cinfo) +{ + /* Only valid after jpeg_read_header completes */ + if (cinfo->global_state < DSTATE_READY || + cinfo->global_state > DSTATE_STOPPING) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + return cinfo->inputctl->has_multiple_scans; +} + + +/* + * Finish JPEG decompression. + * + * This will normally just verify the file trailer and release temp storage. + * + * Returns FALSE if suspended. The return value need be inspected only if + * a suspending data source is used. + */ + +GLOBAL(boolean) +jpeg_finish_decompress (j_decompress_ptr cinfo) +{ + if ((cinfo->global_state == DSTATE_SCANNING || + cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) { + /* Terminate final pass of non-buffered mode */ + if (cinfo->output_scanline < cinfo->output_height) + ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); + (*cinfo->master->finish_output_pass) (cinfo); + cinfo->global_state = DSTATE_STOPPING; + } else if (cinfo->global_state == DSTATE_BUFIMAGE) { + /* Finishing after a buffered-image operation */ + cinfo->global_state = DSTATE_STOPPING; + } else if (cinfo->global_state != DSTATE_STOPPING) { + /* STOPPING = repeat call after a suspension, anything else is error */ + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } + /* Read until EOI */ + while (! cinfo->inputctl->eoi_reached) { + if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) + return FALSE; /* Suspend, come back later */ + } + /* Do final cleanup */ + (*cinfo->src->term_source) (cinfo); + /* We can use jpeg_abort to release memory and reset global_state */ + jpeg_abort((j_common_ptr) cinfo); + return TRUE; +} diff --git a/crypto777/jpeg/jdapistd.c b/crypto777/jpeg/jdapistd.c new file mode 100644 index 000000000..9d7453777 --- /dev/null +++ b/crypto777/jpeg/jdapistd.c @@ -0,0 +1,275 @@ +/* + * jdapistd.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains application interface code for the decompression half + * of the JPEG library. These are the "standard" API routines that are + * used in the normal full-decompression case. They are not used by a + * transcoding-only application. Note that if an application links in + * jpeg_start_decompress, it will end up linking in the entire decompressor. + * We thus must separate this file from jdapimin.c to avoid linking the + * whole decompression library into a transcoder. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Forward declarations */ +LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo)); + + +/* + * Decompression initialization. + * jpeg_read_header must be completed before calling this. + * + * If a multipass operating mode was selected, this will do all but the + * last pass, and thus may take a great deal of time. + * + * Returns FALSE if suspended. The return value need be inspected only if + * a suspending data source is used. + */ + +GLOBAL(boolean) +jpeg_start_decompress (j_decompress_ptr cinfo) +{ + if (cinfo->global_state == DSTATE_READY) { + /* First call: initialize master control, select active modules */ + jinit_master_decompress(cinfo); + if (cinfo->buffered_image) { + /* No more work here; expecting jpeg_start_output next */ + cinfo->global_state = DSTATE_BUFIMAGE; + return TRUE; + } + cinfo->global_state = DSTATE_PRELOAD; + } + if (cinfo->global_state == DSTATE_PRELOAD) { + /* If file has multiple scans, absorb them all into the coef buffer */ + if (cinfo->inputctl->has_multiple_scans) { +#ifdef D_MULTISCAN_FILES_SUPPORTED + for (;;) { + int retcode; + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + /* Absorb some more input */ + retcode = (*cinfo->inputctl->consume_input) (cinfo); + if (retcode == JPEG_SUSPENDED) + return FALSE; + if (retcode == JPEG_REACHED_EOI) + break; + /* Advance progress counter if appropriate */ + if (cinfo->progress != NULL && + (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { + if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { + /* jdmaster underestimated number of scans; ratchet up one scan */ + cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; + } + } + } +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif /* D_MULTISCAN_FILES_SUPPORTED */ + } + cinfo->output_scan_number = cinfo->input_scan_number; + } else if (cinfo->global_state != DSTATE_PRESCAN) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + /* Perform any dummy output passes, and set up for the final pass */ + return output_pass_setup(cinfo); +} + + +/* + * Set up for an output pass, and perform any dummy pass(es) needed. + * Common subroutine for jpeg_start_decompress and jpeg_start_output. + * Entry: global_state = DSTATE_PRESCAN only if previously suspended. + * Exit: If done, returns TRUE and sets global_state for proper output mode. + * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN. + */ + +LOCAL(boolean) +output_pass_setup (j_decompress_ptr cinfo) +{ + if (cinfo->global_state != DSTATE_PRESCAN) { + /* First call: do pass setup */ + (*cinfo->master->prepare_for_output_pass) (cinfo); + cinfo->output_scanline = 0; + cinfo->global_state = DSTATE_PRESCAN; + } + /* Loop over any required dummy passes */ + while (cinfo->master->is_dummy_pass) { +#ifdef QUANT_2PASS_SUPPORTED + /* Crank through the dummy pass */ + while (cinfo->output_scanline < cinfo->output_height) { + JDIMENSION last_scanline; + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) { + cinfo->progress->pass_counter = (long) cinfo->output_scanline; + cinfo->progress->pass_limit = (long) cinfo->output_height; + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + } + /* Process some data */ + last_scanline = cinfo->output_scanline; + (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL, + &cinfo->output_scanline, (JDIMENSION) 0); + if (cinfo->output_scanline == last_scanline) + return FALSE; /* No progress made, must suspend */ + } + /* Finish up dummy pass, and set up for another one */ + (*cinfo->master->finish_output_pass) (cinfo); + (*cinfo->master->prepare_for_output_pass) (cinfo); + cinfo->output_scanline = 0; +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif /* QUANT_2PASS_SUPPORTED */ + } + /* Ready for application to drive output pass through + * jpeg_read_scanlines or jpeg_read_raw_data. + */ + cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING; + return TRUE; +} + + +/* + * Read some scanlines of data from the JPEG decompressor. + * + * The return value will be the number of lines actually read. + * This may be less than the number requested in several cases, + * including bottom of image, data source suspension, and operating + * modes that emit multiple scanlines at a time. + * + * Note: we warn about excess calls to jpeg_read_scanlines() since + * this likely signals an application programmer error. However, + * an oversize buffer (max_lines > scanlines remaining) is not an error. + */ + +GLOBAL(JDIMENSION) +jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, + JDIMENSION max_lines) +{ + JDIMENSION row_ctr; + + if (cinfo->global_state != DSTATE_SCANNING) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + if (cinfo->output_scanline >= cinfo->output_height) { + WARNMS(cinfo, JWRN_TOO_MUCH_DATA); + return 0; + } + + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) { + cinfo->progress->pass_counter = (long) cinfo->output_scanline; + cinfo->progress->pass_limit = (long) cinfo->output_height; + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + } + + /* Process some data */ + row_ctr = 0; + (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines); + cinfo->output_scanline += row_ctr; + return row_ctr; +} + + +/* + * Alternate entry point to read raw data. + * Processes exactly one iMCU row per call, unless suspended. + */ + +GLOBAL(JDIMENSION) +jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, + JDIMENSION max_lines) +{ + JDIMENSION lines_per_iMCU_row; + + if (cinfo->global_state != DSTATE_RAW_OK) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + if (cinfo->output_scanline >= cinfo->output_height) { + WARNMS(cinfo, JWRN_TOO_MUCH_DATA); + return 0; + } + + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) { + cinfo->progress->pass_counter = (long) cinfo->output_scanline; + cinfo->progress->pass_limit = (long) cinfo->output_height; + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + } + + /* Verify that at least one iMCU row can be returned. */ + lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size; + if (max_lines < lines_per_iMCU_row) + ERREXIT(cinfo, JERR_BUFFER_SIZE); + + /* Decompress directly into user's buffer. */ + if (! (*cinfo->coef->decompress_data) (cinfo, data)) + return 0; /* suspension forced, can do nothing more */ + + /* OK, we processed one iMCU row. */ + cinfo->output_scanline += lines_per_iMCU_row; + return lines_per_iMCU_row; +} + + +/* Additional entry points for buffered-image mode. */ + +#ifdef D_MULTISCAN_FILES_SUPPORTED + +/* + * Initialize for an output pass in buffered-image mode. + */ + +GLOBAL(boolean) +jpeg_start_output (j_decompress_ptr cinfo, int scan_number) +{ + if (cinfo->global_state != DSTATE_BUFIMAGE && + cinfo->global_state != DSTATE_PRESCAN) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + /* Limit scan number to valid range */ + if (scan_number <= 0) + scan_number = 1; + if (cinfo->inputctl->eoi_reached && + scan_number > cinfo->input_scan_number) + scan_number = cinfo->input_scan_number; + cinfo->output_scan_number = scan_number; + /* Perform any dummy output passes, and set up for the real pass */ + return output_pass_setup(cinfo); +} + + +/* + * Finish up after an output pass in buffered-image mode. + * + * Returns FALSE if suspended. The return value need be inspected only if + * a suspending data source is used. + */ + +GLOBAL(boolean) +jpeg_finish_output (j_decompress_ptr cinfo) +{ + if ((cinfo->global_state == DSTATE_SCANNING || + cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) { + /* Terminate this pass. */ + /* We do not require the whole pass to have been completed. */ + (*cinfo->master->finish_output_pass) (cinfo); + cinfo->global_state = DSTATE_BUFPOST; + } else if (cinfo->global_state != DSTATE_BUFPOST) { + /* BUFPOST = repeat call after a suspension, anything else is error */ + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } + /* Read markers looking for SOS or EOI */ + while (cinfo->input_scan_number <= cinfo->output_scan_number && + ! cinfo->inputctl->eoi_reached) { + if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) + return FALSE; /* Suspend, come back later */ + } + cinfo->global_state = DSTATE_BUFIMAGE; + return TRUE; +} + +#endif /* D_MULTISCAN_FILES_SUPPORTED */ diff --git a/crypto777/jpeg/jdarith.c b/crypto777/jpeg/jdarith.c new file mode 100644 index 000000000..092f8af5f --- /dev/null +++ b/crypto777/jpeg/jdarith.c @@ -0,0 +1,776 @@ +/* + * jdarith.c + * + * Developed 1997-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains portable arithmetic entropy decoding routines for JPEG + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). + * + * Both sequential and progressive modes are supported in this single module. + * + * Suspension is not currently supported in this module. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Expanded entropy decoder object for arithmetic decoding. */ + +typedef struct { + struct jpeg_entropy_decoder pub; /* public fields */ + + INT32 c; /* C register, base of coding interval + input bit buffer */ + INT32 a; /* A register, normalized size of coding interval */ + int ct; /* bit shift counter, # of bits left in bit buffer part of C */ + /* init: ct = -16 */ + /* run: ct = 0..7 */ + /* error: ct = -1 */ + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ + int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ + + unsigned int restarts_to_go; /* MCUs left in this restart interval */ + + /* Pointers to statistics areas (these workspaces have image lifespan) */ + unsigned char * dc_stats[NUM_ARITH_TBLS]; + unsigned char * ac_stats[NUM_ARITH_TBLS]; + + /* Statistics bin for coding with fixed probability 0.5 */ + unsigned char fixed_bin[4]; +} arith_entropy_decoder; + +typedef arith_entropy_decoder * arith_entropy_ptr; + +/* The following two definitions specify the allocation chunk size + * for the statistics area. + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least + * 49 statistics bins for DC, and 245 statistics bins for AC coding. + * + * We use a compact representation with 1 byte per statistics bin, + * thus the numbers directly represent byte sizes. + * This 1 byte per statistics bin contains the meaning of the MPS + * (more probable symbol) in the highest bit (mask 0x80), and the + * index into the probability estimation state machine table + * in the lower bits (mask 0x7F). + */ + +#define DC_STAT_BINS 64 +#define AC_STAT_BINS 256 + + +LOCAL(int) +get_byte (j_decompress_ptr cinfo) +/* Read next input byte; we do not support suspension in this module. */ +{ + struct jpeg_source_mgr * src = cinfo->src; + + if (src->bytes_in_buffer == 0) + if (! (*src->fill_input_buffer) (cinfo)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); + src->bytes_in_buffer--; + return GETJOCTET(*src->next_input_byte++); +} + + +/* + * The core arithmetic decoding routine (common in JPEG and JBIG). + * This needs to go as fast as possible. + * Machine-dependent optimization facilities + * are not utilized in this portable implementation. + * However, this code should be fairly efficient and + * may be a good base for further optimizations anyway. + * + * Return value is 0 or 1 (binary decision). + * + * Note: I've changed the handling of the code base & bit + * buffer register C compared to other implementations + * based on the standards layout & procedures. + * While it also contains both the actual base of the + * coding interval (16 bits) and the next-bits buffer, + * the cut-point between these two parts is floating + * (instead of fixed) with the bit shift counter CT. + * Thus, we also need only one (variable instead of + * fixed size) shift for the LPS/MPS decision, and + * we can get away with any renormalization update + * of C (except for new data insertion, of course). + * + * I've also introduced a new scheme for accessing + * the probability estimation state machine table, + * derived from Markus Kuhn's JBIG implementation. + */ + +LOCAL(int) +arith_decode (j_decompress_ptr cinfo, unsigned char *st) +{ + register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; + register unsigned char nl, nm; + register INT32 qe, temp; + register int sv, data; + + /* Renormalization & data input per section D.2.6 */ + while (e->a < 0x8000L) { + if (--e->ct < 0) { + /* Need to fetch next data byte */ + if (cinfo->unread_marker) + data = 0; /* stuff zero data */ + else { + data = get_byte(cinfo); /* read next input byte */ + if (data == 0xFF) { /* zero stuff or marker code */ + do data = get_byte(cinfo); + while (data == 0xFF); /* swallow extra 0xFF bytes */ + if (data == 0) + data = 0xFF; /* discard stuffed zero byte */ + else { + /* Note: Different from the Huffman decoder, hitting + * a marker while processing the compressed data + * segment is legal in arithmetic coding. + * The convention is to supply zero data + * then until decoding is complete. + */ + cinfo->unread_marker = data; + data = 0; + } + } + } + e->c = (e->c << 8) | data; /* insert data into C register */ + if ((e->ct += 8) < 0) /* update bit shift counter */ + /* Need more initial bytes */ + if (++e->ct == 0) + /* Got 2 initial bytes -> re-init A and exit loop */ + e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ + } + e->a <<= 1; + } + + /* Fetch values from our compact representation of Table D.3(D.2): + * Qe values and probability estimation state machine + */ + sv = *st; + qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ + nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ + nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ + + /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ + temp = e->a - qe; + e->a = temp; + temp <<= e->ct; + if (e->c >= temp) { + e->c -= temp; + /* Conditional LPS (less probable symbol) exchange */ + if (e->a < qe) { + e->a = qe; + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ + } else { + e->a = qe; + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ + sv ^= 0x80; /* Exchange LPS/MPS */ + } + } else if (e->a < 0x8000L) { + /* Conditional MPS (more probable symbol) exchange */ + if (e->a < qe) { + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ + sv ^= 0x80; /* Exchange LPS/MPS */ + } else { + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ + } + } + + return sv >> 7; +} + + +/* + * Check for a restart marker & resynchronize decoder. + */ + +LOCAL(void) +process_restart (j_decompress_ptr cinfo) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + int ci; + jpeg_component_info * compptr; + + /* Advance past the RSTn marker */ + if (! (*cinfo->marker->read_restart_marker) (cinfo)) + ERREXIT(cinfo, JERR_CANT_SUSPEND); + + /* Re-initialize statistics areas */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { + MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); + /* Reset DC predictions to 0 */ + entropy->last_dc_val[ci] = 0; + entropy->dc_context[ci] = 0; + } + if ((! cinfo->progressive_mode && cinfo->lim_Se) || + (cinfo->progressive_mode && cinfo->Ss)) { + MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); + } + } + + /* Reset arithmetic decoding variables */ + entropy->c = 0; + entropy->a = 0; + entropy->ct = -16; /* force reading 2 initial bytes to fill C */ + + /* Reset restart counter */ + entropy->restarts_to_go = cinfo->restart_interval; +} + + +/* + * Arithmetic MCU decoding. + * Each of these routines decodes and returns one MCU's worth of + * arithmetic-compressed coefficients. + * The coefficients are reordered from zigzag order into natural array order, + * but are not dequantized. + * + * The i'th block of the MCU is stored into the block pointed to by + * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. + */ + +/* + * MCU decoding for DC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + JBLOCKROW block; + unsigned char *st; + int blkn, ci, tbl, sign; + int v, m; + + /* Process restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + process_restart(cinfo); + entropy->restarts_to_go--; + } + + if (entropy->ct == -1) return TRUE; /* if error do nothing */ + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + ci = cinfo->MCU_membership[blkn]; + tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; + + /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ + + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + + /* Figure F.19: Decode_DC_DIFF */ + if (arith_decode(cinfo, st) == 0) + entropy->dc_context[ci] = 0; + else { + /* Figure F.21: Decoding nonzero value v */ + /* Figure F.22: Decoding the sign of v */ + sign = arith_decode(cinfo, st + 1); + st += 2; st += sign; + /* Figure F.23: Decoding the magnitude category of v */ + if ((m = arith_decode(cinfo, st)) != 0) { + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ + while (arith_decode(cinfo, st)) { + if ((m <<= 1) == 0x8000) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* magnitude overflow */ + return TRUE; + } + st += 1; + } + } + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ + if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) + entropy->dc_context[ci] = 0; /* zero diff category */ + else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) + entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ + else + entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ + v = m; + /* Figure F.24: Decoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + if (arith_decode(cinfo, st)) v |= m; + v += 1; if (sign) v = -v; + entropy->last_dc_val[ci] += v; + } + + /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ + (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); + } + + return TRUE; +} + + +/* + * MCU decoding for AC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + JBLOCKROW block; + unsigned char *st; + int tbl, sign, k; + int v, m; + const int * natural_order; + + /* Process restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + process_restart(cinfo); + entropy->restarts_to_go--; + } + + if (entropy->ct == -1) return TRUE; /* if error do nothing */ + + natural_order = cinfo->natural_order; + + /* There is always only one block per MCU */ + block = MCU_data[0]; + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + + /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ + + /* Figure F.20: Decode_AC_coefficients */ + for (k = cinfo->Ss; k <= cinfo->Se; k++) { + st = entropy->ac_stats[tbl] + 3 * (k - 1); + if (arith_decode(cinfo, st)) break; /* EOB flag */ + while (arith_decode(cinfo, st + 1) == 0) { + st += 3; k++; + if (k > cinfo->Se) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* spectral overflow */ + return TRUE; + } + } + /* Figure F.21: Decoding nonzero value v */ + /* Figure F.22: Decoding the sign of v */ + sign = arith_decode(cinfo, entropy->fixed_bin); + st += 2; + /* Figure F.23: Decoding the magnitude category of v */ + if ((m = arith_decode(cinfo, st)) != 0) { + if (arith_decode(cinfo, st)) { + m <<= 1; + st = entropy->ac_stats[tbl] + + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); + while (arith_decode(cinfo, st)) { + if ((m <<= 1) == 0x8000) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* magnitude overflow */ + return TRUE; + } + st += 1; + } + } + } + v = m; + /* Figure F.24: Decoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + if (arith_decode(cinfo, st)) v |= m; + v += 1; if (sign) v = -v; + /* Scale and output coefficient in natural (dezigzagged) order */ + (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al); + } + + return TRUE; +} + + +/* + * MCU decoding for DC successive approximation refinement scan. + */ + +METHODDEF(boolean) +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + unsigned char *st; + int p1, blkn; + + /* Process restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + process_restart(cinfo); + entropy->restarts_to_go--; + } + + st = entropy->fixed_bin; /* use fixed probability estimation */ + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + /* Encoded data is simply the next bit of the two's-complement DC value */ + if (arith_decode(cinfo, st)) + MCU_data[blkn][0][0] |= p1; + } + + return TRUE; +} + + +/* + * MCU decoding for AC successive approximation refinement scan. + */ + +METHODDEF(boolean) +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + JBLOCKROW block; + JCOEFPTR thiscoef; + unsigned char *st; + int tbl, k, kex; + int p1, m1; + const int * natural_order; + + /* Process restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + process_restart(cinfo); + entropy->restarts_to_go--; + } + + if (entropy->ct == -1) return TRUE; /* if error do nothing */ + + natural_order = cinfo->natural_order; + + /* There is always only one block per MCU */ + block = MCU_data[0]; + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ + m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ + + /* Establish EOBx (previous stage end-of-block) index */ + for (kex = cinfo->Se; kex > 0; kex--) + if ((*block)[natural_order[kex]]) break; + + for (k = cinfo->Ss; k <= cinfo->Se; k++) { + st = entropy->ac_stats[tbl] + 3 * (k - 1); + if (k > kex) + if (arith_decode(cinfo, st)) break; /* EOB flag */ + for (;;) { + thiscoef = *block + natural_order[k]; + if (*thiscoef) { /* previously nonzero coef */ + if (arith_decode(cinfo, st + 2)) { + if (*thiscoef < 0) + *thiscoef += m1; + else + *thiscoef += p1; + } + break; + } + if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ + if (arith_decode(cinfo, entropy->fixed_bin)) + *thiscoef = m1; + else + *thiscoef = p1; + break; + } + st += 3; k++; + if (k > cinfo->Se) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* spectral overflow */ + return TRUE; + } + } + } + + return TRUE; +} + + +/* + * Decode one MCU's worth of arithmetic-compressed coefficients. + */ + +METHODDEF(boolean) +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + jpeg_component_info * compptr; + JBLOCKROW block; + unsigned char *st; + int blkn, ci, tbl, sign, k; + int v, m; + const int * natural_order; + + /* Process restart marker if needed */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + process_restart(cinfo); + entropy->restarts_to_go--; + } + + if (entropy->ct == -1) return TRUE; /* if error do nothing */ + + natural_order = cinfo->natural_order; + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + + /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ + + tbl = compptr->dc_tbl_no; + + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + + /* Figure F.19: Decode_DC_DIFF */ + if (arith_decode(cinfo, st) == 0) + entropy->dc_context[ci] = 0; + else { + /* Figure F.21: Decoding nonzero value v */ + /* Figure F.22: Decoding the sign of v */ + sign = arith_decode(cinfo, st + 1); + st += 2; st += sign; + /* Figure F.23: Decoding the magnitude category of v */ + if ((m = arith_decode(cinfo, st)) != 0) { + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ + while (arith_decode(cinfo, st)) { + if ((m <<= 1) == 0x8000) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* magnitude overflow */ + return TRUE; + } + st += 1; + } + } + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ + if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) + entropy->dc_context[ci] = 0; /* zero diff category */ + else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) + entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ + else + entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ + v = m; + /* Figure F.24: Decoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + if (arith_decode(cinfo, st)) v |= m; + v += 1; if (sign) v = -v; + entropy->last_dc_val[ci] += v; + } + + (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; + + /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ + + if (cinfo->lim_Se == 0) continue; + tbl = compptr->ac_tbl_no; + k = 0; + + /* Figure F.20: Decode_AC_coefficients */ + do { + st = entropy->ac_stats[tbl] + 3 * k; + if (arith_decode(cinfo, st)) break; /* EOB flag */ + for (;;) { + k++; + if (arith_decode(cinfo, st + 1)) break; + st += 3; + if (k >= cinfo->lim_Se) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* spectral overflow */ + return TRUE; + } + } + /* Figure F.21: Decoding nonzero value v */ + /* Figure F.22: Decoding the sign of v */ + sign = arith_decode(cinfo, entropy->fixed_bin); + st += 2; + /* Figure F.23: Decoding the magnitude category of v */ + if ((m = arith_decode(cinfo, st)) != 0) { + if (arith_decode(cinfo, st)) { + m <<= 1; + st = entropy->ac_stats[tbl] + + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); + while (arith_decode(cinfo, st)) { + if ((m <<= 1) == 0x8000) { + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); + entropy->ct = -1; /* magnitude overflow */ + return TRUE; + } + st += 1; + } + } + } + v = m; + /* Figure F.24: Decoding the magnitude bit pattern of v */ + st += 14; + while (m >>= 1) + if (arith_decode(cinfo, st)) v |= m; + v += 1; if (sign) v = -v; + (*block)[natural_order[k]] = (JCOEF) v; + } while (k < cinfo->lim_Se); + } + + return TRUE; +} + + +/* + * Initialize for an arithmetic-compressed scan. + */ + +METHODDEF(void) +start_pass (j_decompress_ptr cinfo) +{ + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; + int ci, tbl; + jpeg_component_info * compptr; + + if (cinfo->progressive_mode) { + /* Validate progressive scan parameters */ + if (cinfo->Ss == 0) { + if (cinfo->Se != 0) + goto bad; + } else { + /* need not check Ss/Se < 0 since they came from unsigned bytes */ + if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se) + goto bad; + /* AC scans may have only one component */ + if (cinfo->comps_in_scan != 1) + goto bad; + } + if (cinfo->Ah != 0) { + /* Successive approximation refinement scan: must have Al = Ah-1. */ + if (cinfo->Ah-1 != cinfo->Al) + goto bad; + } + if (cinfo->Al > 13) { /* need not check for < 0 */ + bad: + ERREXIT4(cinfo, JERR_BAD_PROGRESSION, + cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); + } + /* Update progression status, and verify that scan order is legal. + * Note that inter-scan inconsistencies are treated as warnings + * not fatal errors ... not clear if this is right way to behave. + */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; + int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; + if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); + for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { + int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; + if (cinfo->Ah != expected) + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); + coef_bit_ptr[coefi] = cinfo->Al; + } + } + /* Select MCU decoding routine */ + if (cinfo->Ah == 0) { + if (cinfo->Ss == 0) + entropy->pub.decode_mcu = decode_mcu_DC_first; + else + entropy->pub.decode_mcu = decode_mcu_AC_first; + } else { + if (cinfo->Ss == 0) + entropy->pub.decode_mcu = decode_mcu_DC_refine; + else + entropy->pub.decode_mcu = decode_mcu_AC_refine; + } + } else { + /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. + * This ought to be an error condition, but we make it a warning. + */ + if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || + (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se)) + WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); + /* Select MCU decoding routine */ + entropy->pub.decode_mcu = decode_mcu; + } + + /* Allocate & initialize requested statistics areas */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { + tbl = compptr->dc_tbl_no; + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); + if (entropy->dc_stats[tbl] == NULL) + entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); + MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); + /* Initialize DC predictions to 0 */ + entropy->last_dc_val[ci] = 0; + entropy->dc_context[ci] = 0; + } + if ((! cinfo->progressive_mode && cinfo->lim_Se) || + (cinfo->progressive_mode && cinfo->Ss)) { + tbl = compptr->ac_tbl_no; + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); + if (entropy->ac_stats[tbl] == NULL) + entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); + MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); + } + } + + /* Initialize arithmetic decoding variables */ + entropy->c = 0; + entropy->a = 0; + entropy->ct = -16; /* force reading 2 initial bytes to fill C */ + + /* Initialize restart counter */ + entropy->restarts_to_go = cinfo->restart_interval; +} + + +/* + * Module initialization routine for arithmetic entropy decoding. + */ + +GLOBAL(void) +jinit_arith_decoder (j_decompress_ptr cinfo) +{ + arith_entropy_ptr entropy; + int i; + + entropy = (arith_entropy_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(arith_entropy_decoder)); + cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; + entropy->pub.start_pass = start_pass; + + /* Mark tables unallocated */ + for (i = 0; i < NUM_ARITH_TBLS; i++) { + entropy->dc_stats[i] = NULL; + entropy->ac_stats[i] = NULL; + } + + /* Initialize index for fixed probability estimation */ + entropy->fixed_bin[0] = 113; + + if (cinfo->progressive_mode) { + /* Create progression status table */ + int *coef_bit_ptr, ci; + cinfo->coef_bits = (int (*)[DCTSIZE2]) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->num_components*DCTSIZE2*SIZEOF(int)); + coef_bit_ptr = & cinfo->coef_bits[0][0]; + for (ci = 0; ci < cinfo->num_components; ci++) + for (i = 0; i < DCTSIZE2; i++) + *coef_bit_ptr++ = -1; + } +} diff --git a/crypto777/jpeg/jdatadst.c b/crypto777/jpeg/jdatadst.c new file mode 100644 index 000000000..6981fb73d --- /dev/null +++ b/crypto777/jpeg/jdatadst.c @@ -0,0 +1,267 @@ +/* + * jdatadst.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * Modified 2009-2012 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains compression data destination routines for the case of + * emitting JPEG data to memory or to a file (or any stdio stream). + * While these routines are sufficient for most applications, + * some will want to use a different destination manager. + * IMPORTANT: we assume that fwrite() will correctly transcribe an array of + * JOCTETs into 8-bit-wide elements on external storage. If char is wider + * than 8 bits on your machine, you may need to do some tweaking. + */ + +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */ +#include "jinclude.h" +#include "jpeglib.h" +#include "jerror.h" + +#ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ +extern void * malloc JPP((size_t size)); +extern void free JPP((void *ptr)); +#endif + + +/* Expanded data destination object for stdio output */ + +typedef struct { + struct jpeg_destination_mgr pub; /* public fields */ + + FILE * outfile; /* target stream */ + JOCTET * buffer; /* start of buffer */ +} my_destination_mgr; + +typedef my_destination_mgr * my_dest_ptr; + +#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ + + +/* Expanded data destination object for memory output */ + +typedef struct { + struct jpeg_destination_mgr pub; /* public fields */ + + unsigned char ** outbuffer; /* target buffer */ + unsigned long * outsize; + unsigned char * newbuffer; /* newly allocated buffer */ + JOCTET * buffer; /* start of buffer */ + size_t bufsize; +} my_mem_destination_mgr; + +typedef my_mem_destination_mgr * my_mem_dest_ptr; + + +/* + * Initialize destination --- called by jpeg_start_compress + * before any data is actually written. + */ + +METHODDEF(void) +init_destination (j_compress_ptr cinfo) +{ + my_dest_ptr dest = (my_dest_ptr) cinfo->dest; + + /* Allocate the output buffer --- it will be released when done with image */ + dest->buffer = (JOCTET *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + OUTPUT_BUF_SIZE * SIZEOF(JOCTET)); + + dest->pub.next_output_byte = dest->buffer; + dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; +} + +METHODDEF(void) +init_mem_destination (j_compress_ptr cinfo) +{ + /* no work necessary here */ +} + + +/* + * Empty the output buffer --- called whenever buffer fills up. + * + * In typical applications, this should write the entire output buffer + * (ignoring the current state of next_output_byte & free_in_buffer), + * reset the pointer & count to the start of the buffer, and return TRUE + * indicating that the buffer has been dumped. + * + * In applications that need to be able to suspend compression due to output + * overrun, a FALSE return indicates that the buffer cannot be emptied now. + * In this situation, the compressor will return to its caller (possibly with + * an indication that it has not accepted all the supplied scanlines). The + * application should resume compression after it has made more room in the + * output buffer. Note that there are substantial restrictions on the use of + * suspension --- see the documentation. + * + * When suspending, the compressor will back up to a convenient restart point + * (typically the start of the current MCU). next_output_byte & free_in_buffer + * indicate where the restart point will be if the current call returns FALSE. + * Data beyond this point will be regenerated after resumption, so do not + * write it out when emptying the buffer externally. + */ + +METHODDEF(boolean) +empty_output_buffer (j_compress_ptr cinfo) +{ + my_dest_ptr dest = (my_dest_ptr) cinfo->dest; + + if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != + (size_t) OUTPUT_BUF_SIZE) + ERREXIT(cinfo, JERR_FILE_WRITE); + + dest->pub.next_output_byte = dest->buffer; + dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; + + return TRUE; +} + +METHODDEF(boolean) +empty_mem_output_buffer (j_compress_ptr cinfo) +{ + size_t nextsize; + JOCTET * nextbuffer; + my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; + + /* Try to allocate new buffer with double size */ + nextsize = dest->bufsize * 2; + nextbuffer = (JOCTET *) malloc(nextsize); + + if (nextbuffer == NULL) + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); + + MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); + + if (dest->newbuffer != NULL) + free(dest->newbuffer); + + dest->newbuffer = nextbuffer; + + dest->pub.next_output_byte = nextbuffer + dest->bufsize; + dest->pub.free_in_buffer = dest->bufsize; + + dest->buffer = nextbuffer; + dest->bufsize = nextsize; + + return TRUE; +} + + +/* + * Terminate destination --- called by jpeg_finish_compress + * after all data has been written. Usually needs to flush buffer. + * + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding + * application must deal with any cleanup that should happen even + * for error exit. + */ + +METHODDEF(void) +term_destination (j_compress_ptr cinfo) +{ + my_dest_ptr dest = (my_dest_ptr) cinfo->dest; + size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; + + /* Write any data remaining in the buffer */ + if (datacount > 0) { + if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount) + ERREXIT(cinfo, JERR_FILE_WRITE); + } + fflush(dest->outfile); + /* Make sure we wrote the output file OK */ + if (ferror(dest->outfile)) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + +METHODDEF(void) +term_mem_destination (j_compress_ptr cinfo) +{ + my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; + + *dest->outbuffer = dest->buffer; + *dest->outsize = dest->bufsize - dest->pub.free_in_buffer; +} + + +/* + * Prepare for output to a stdio stream. + * The caller must have already opened the stream, and is responsible + * for closing it after finishing compression. + */ + +GLOBAL(void) +jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) +{ + my_dest_ptr dest; + + /* The destination object is made permanent so that multiple JPEG images + * can be written to the same file without re-executing jpeg_stdio_dest. + * This makes it dangerous to use this manager and a different destination + * manager serially with the same JPEG object, because their private object + * sizes may be different. Caveat programmer. + */ + if (cinfo->dest == NULL) { /* first time for this JPEG object? */ + cinfo->dest = (struct jpeg_destination_mgr *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + SIZEOF(my_destination_mgr)); + } + + dest = (my_dest_ptr) cinfo->dest; + dest->pub.init_destination = init_destination; + dest->pub.empty_output_buffer = empty_output_buffer; + dest->pub.term_destination = term_destination; + dest->outfile = outfile; +} + + +/* + * Prepare for output to a memory buffer. + * The caller may supply an own initial buffer with appropriate size. + * Otherwise, or when the actual data output exceeds the given size, + * the library adapts the buffer size as necessary. + * The standard library functions malloc/free are used for allocating + * larger memory, so the buffer is available to the application after + * finishing compression, and then the application is responsible for + * freeing the requested memory. + */ + +GLOBAL(void) +jpeg_mem_dest (j_compress_ptr cinfo, + unsigned char ** outbuffer, unsigned long * outsize) +{ + my_mem_dest_ptr dest; + + if (outbuffer == NULL || outsize == NULL) /* sanity check */ + ERREXIT(cinfo, JERR_BUFFER_SIZE); + + /* The destination object is made permanent so that multiple JPEG images + * can be written to the same buffer without re-executing jpeg_mem_dest. + */ + if (cinfo->dest == NULL) { /* first time for this JPEG object? */ + cinfo->dest = (struct jpeg_destination_mgr *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + SIZEOF(my_mem_destination_mgr)); + } + + dest = (my_mem_dest_ptr) cinfo->dest; + dest->pub.init_destination = init_mem_destination; + dest->pub.empty_output_buffer = empty_mem_output_buffer; + dest->pub.term_destination = term_mem_destination; + dest->outbuffer = outbuffer; + dest->outsize = outsize; + dest->newbuffer = NULL; + + if (*outbuffer == NULL || *outsize == 0) { + /* Allocate initial buffer */ + dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE); + if (dest->newbuffer == NULL) + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); + *outsize = OUTPUT_BUF_SIZE; + } + + dest->pub.next_output_byte = dest->buffer = *outbuffer; + dest->pub.free_in_buffer = dest->bufsize = *outsize; +} diff --git a/crypto777/jpeg/jdatasrc.c b/crypto777/jpeg/jdatasrc.c new file mode 100644 index 000000000..7be59a88a --- /dev/null +++ b/crypto777/jpeg/jdatasrc.c @@ -0,0 +1,275 @@ +/* + * jdatasrc.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * Modified 2009-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains decompression data source routines for the case of + * reading JPEG data from memory or from a file (or any stdio stream). + * While these routines are sufficient for most applications, + * some will want to use a different source manager. + * IMPORTANT: we assume that fread() will correctly transcribe an array of + * JOCTETs from 8-bit-wide elements on external storage. If char is wider + * than 8 bits on your machine, you may need to do some tweaking. + */ + +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */ +#include "jinclude.h" +#include "jpeglib.h" +#include "jerror.h" + + +/* Expanded data source object for stdio input */ + +typedef struct { + struct jpeg_source_mgr pub; /* public fields */ + + FILE * infile; /* source stream */ + JOCTET * buffer; /* start of buffer */ + boolean start_of_file; /* have we gotten any data yet? */ +} my_source_mgr; + +typedef my_source_mgr * my_src_ptr; + +#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ + + +/* + * Initialize source --- called by jpeg_read_header + * before any data is actually read. + */ + +METHODDEF(void) +init_source (j_decompress_ptr cinfo) +{ + my_src_ptr src = (my_src_ptr) cinfo->src; + + /* We reset the empty-input-file flag for each image, + * but we don't clear the input buffer. + * This is correct behavior for reading a series of images from one source. + */ + src->start_of_file = TRUE; +} + +METHODDEF(void) +init_mem_source (j_decompress_ptr cinfo) +{ + /* no work necessary here */ +} + + +/* + * Fill the input buffer --- called whenever buffer is emptied. + * + * In typical applications, this should read fresh data into the buffer + * (ignoring the current state of next_input_byte & bytes_in_buffer), + * reset the pointer & count to the start of the buffer, and return TRUE + * indicating that the buffer has been reloaded. It is not necessary to + * fill the buffer entirely, only to obtain at least one more byte. + * + * There is no such thing as an EOF return. If the end of the file has been + * reached, the routine has a choice of ERREXIT() or inserting fake data into + * the buffer. In most cases, generating a warning message and inserting a + * fake EOI marker is the best course of action --- this will allow the + * decompressor to output however much of the image is there. However, + * the resulting error message is misleading if the real problem is an empty + * input file, so we handle that case specially. + * + * In applications that need to be able to suspend compression due to input + * not being available yet, a FALSE return indicates that no more data can be + * obtained right now, but more may be forthcoming later. In this situation, + * the decompressor will return to its caller (with an indication of the + * number of scanlines it has read, if any). The application should resume + * decompression after it has loaded more data into the input buffer. Note + * that there are substantial restrictions on the use of suspension --- see + * the documentation. + * + * When suspending, the decompressor will back up to a convenient restart point + * (typically the start of the current MCU). next_input_byte & bytes_in_buffer + * indicate where the restart point will be if the current call returns FALSE. + * Data beyond this point must be rescanned after resumption, so move it to + * the front of the buffer rather than discarding it. + */ + +METHODDEF(boolean) +fill_input_buffer (j_decompress_ptr cinfo) +{ + my_src_ptr src = (my_src_ptr) cinfo->src; + size_t nbytes; + + nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE); + + if (nbytes <= 0) { + if (src->start_of_file) /* Treat empty input file as fatal error */ + ERREXIT(cinfo, JERR_INPUT_EMPTY); + WARNMS(cinfo, JWRN_JPEG_EOF); + /* Insert a fake EOI marker */ + src->buffer[0] = (JOCTET) 0xFF; + src->buffer[1] = (JOCTET) JPEG_EOI; + nbytes = 2; + } + + src->pub.next_input_byte = src->buffer; + src->pub.bytes_in_buffer = nbytes; + src->start_of_file = FALSE; + + return TRUE; +} + +METHODDEF(boolean) +fill_mem_input_buffer (j_decompress_ptr cinfo) +{ + static const JOCTET mybuffer[4] = { + (JOCTET) 0xFF, (JOCTET) JPEG_EOI, 0, 0 + }; + + /* The whole JPEG data is expected to reside in the supplied memory + * buffer, so any request for more data beyond the given buffer size + * is treated as an error. + */ + WARNMS(cinfo, JWRN_JPEG_EOF); + + /* Insert a fake EOI marker */ + + cinfo->src->next_input_byte = mybuffer; + cinfo->src->bytes_in_buffer = 2; + + return TRUE; +} + + +/* + * Skip data --- used to skip over a potentially large amount of + * uninteresting data (such as an APPn marker). + * + * Writers of suspendable-input applications must note that skip_input_data + * is not granted the right to give a suspension return. If the skip extends + * beyond the data currently in the buffer, the buffer can be marked empty so + * that the next read will cause a fill_input_buffer call that can suspend. + * Arranging for additional bytes to be discarded before reloading the input + * buffer is the application writer's problem. + */ + +METHODDEF(void) +skip_input_data (j_decompress_ptr cinfo, long num_bytes) +{ + struct jpeg_source_mgr * src = cinfo->src; + + /* Just a dumb implementation for now. Could use fseek() except + * it doesn't work on pipes. Not clear that being smart is worth + * any trouble anyway --- large skips are infrequent. + */ + if (num_bytes > 0) { + while (num_bytes > (long) src->bytes_in_buffer) { + num_bytes -= (long) src->bytes_in_buffer; + (void) (*src->fill_input_buffer) (cinfo); + /* note we assume that fill_input_buffer will never return FALSE, + * so suspension need not be handled. + */ + } + src->next_input_byte += (size_t) num_bytes; + src->bytes_in_buffer -= (size_t) num_bytes; + } +} + + +/* + * An additional method that can be provided by data source modules is the + * resync_to_restart method for error recovery in the presence of RST markers. + * For the moment, this source module just uses the default resync method + * provided by the JPEG library. That method assumes that no backtracking + * is possible. + */ + + +/* + * Terminate source --- called by jpeg_finish_decompress + * after all data has been read. Often a no-op. + * + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding + * application must deal with any cleanup that should happen even + * for error exit. + */ + +METHODDEF(void) +term_source (j_decompress_ptr cinfo) +{ + /* no work necessary here */ +} + + +/* + * Prepare for input from a stdio stream. + * The caller must have already opened the stream, and is responsible + * for closing it after finishing decompression. + */ + +GLOBAL(void) +jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile) +{ + my_src_ptr src; + + /* The source object and input buffer are made permanent so that a series + * of JPEG images can be read from the same file by calling jpeg_stdio_src + * only before the first one. (If we discarded the buffer at the end of + * one image, we'd likely lose the start of the next one.) + * This makes it unsafe to use this manager and a different source + * manager serially with the same JPEG object. Caveat programmer. + */ + if (cinfo->src == NULL) { /* first time for this JPEG object? */ + cinfo->src = (struct jpeg_source_mgr *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + SIZEOF(my_source_mgr)); + src = (my_src_ptr) cinfo->src; + src->buffer = (JOCTET *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + INPUT_BUF_SIZE * SIZEOF(JOCTET)); + } + + src = (my_src_ptr) cinfo->src; + src->pub.init_source = init_source; + src->pub.fill_input_buffer = fill_input_buffer; + src->pub.skip_input_data = skip_input_data; + src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ + src->pub.term_source = term_source; + src->infile = infile; + src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ + src->pub.next_input_byte = NULL; /* until buffer loaded */ +} + + +/* + * Prepare for input from a supplied memory buffer. + * The buffer must contain the whole JPEG data. + */ + +GLOBAL(void) +jpeg_mem_src (j_decompress_ptr cinfo, + unsigned char * inbuffer, unsigned long insize) +{ + struct jpeg_source_mgr * src; + + if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */ + ERREXIT(cinfo, JERR_INPUT_EMPTY); + + /* The source object is made permanent so that a series of JPEG images + * can be read from the same buffer by calling jpeg_mem_src only before + * the first one. + */ + if (cinfo->src == NULL) { /* first time for this JPEG object? */ + cinfo->src = (struct jpeg_source_mgr *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + SIZEOF(struct jpeg_source_mgr)); + } + + src = cinfo->src; + src->init_source = init_mem_source; + src->fill_input_buffer = fill_mem_input_buffer; + src->skip_input_data = skip_input_data; + src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ + src->term_source = term_source; + src->bytes_in_buffer = (size_t) insize; + src->next_input_byte = (JOCTET *) inbuffer; +} diff --git a/crypto777/jpeg/jdcoefct.c b/crypto777/jpeg/jdcoefct.c new file mode 100644 index 000000000..ed02fc378 --- /dev/null +++ b/crypto777/jpeg/jdcoefct.c @@ -0,0 +1,741 @@ +/* + * jdcoefct.c + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * Modified 2002-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the coefficient buffer controller for decompression. + * This controller is the top level of the JPEG decompressor proper. + * The coefficient buffer lies between entropy decoding and inverse-DCT steps. + * + * In buffered-image mode, this controller is the interface between + * input-oriented processing and output-oriented processing. + * Also, the input side (only) is used when reading a file for transcoding. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + +/* Block smoothing is only applicable for progressive JPEG, so: */ +#ifndef D_PROGRESSIVE_SUPPORTED +#undef BLOCK_SMOOTHING_SUPPORTED +#endif + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_d_coef_controller pub; /* public fields */ + + /* These variables keep track of the current location of the input side. */ + /* cinfo->input_iMCU_row is also used for this. */ + JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ + int MCU_vert_offset; /* counts MCU rows within iMCU row */ + int MCU_rows_per_iMCU_row; /* number of such rows needed */ + + /* The output side's location is represented by cinfo->output_iMCU_row. */ + + /* In single-pass modes, it's sufficient to buffer just one MCU. + * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, + * and let the entropy decoder write into that workspace each time. + * (On 80x86, the workspace is FAR even though it's not really very big; + * this is to keep the module interfaces unchanged when a large coefficient + * buffer is necessary.) + * In multi-pass modes, this array points to the current MCU's blocks + * within the virtual arrays; it is used only by the input side. + */ + JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; + +#ifdef D_MULTISCAN_FILES_SUPPORTED + /* In multi-pass modes, we need a virtual block array for each component. */ + jvirt_barray_ptr whole_image[MAX_COMPONENTS]; +#endif + +#ifdef BLOCK_SMOOTHING_SUPPORTED + /* When doing block smoothing, we latch coefficient Al values here */ + int * coef_bits_latch; +#define SAVED_COEFS 6 /* we save coef_bits[0..5] */ +#endif +} my_coef_controller; + +typedef my_coef_controller * my_coef_ptr; + +/* Forward declarations */ +METHODDEF(int) decompress_onepass + JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); +#ifdef D_MULTISCAN_FILES_SUPPORTED +METHODDEF(int) decompress_data + JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); +#endif +#ifdef BLOCK_SMOOTHING_SUPPORTED +LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); +METHODDEF(int) decompress_smooth_data + JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); +#endif + + +LOCAL(void) +start_iMCU_row (j_decompress_ptr cinfo) +/* Reset within-iMCU-row counters for a new row (input side) */ +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + + /* In an interleaved scan, an MCU row is the same as an iMCU row. + * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. + * But at the bottom of the image, process only what's left. + */ + if (cinfo->comps_in_scan > 1) { + coef->MCU_rows_per_iMCU_row = 1; + } else { + if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; + else + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; + } + + coef->MCU_ctr = 0; + coef->MCU_vert_offset = 0; +} + + +/* + * Initialize for an input processing pass. + */ + +METHODDEF(void) +start_input_pass (j_decompress_ptr cinfo) +{ + cinfo->input_iMCU_row = 0; + start_iMCU_row(cinfo); +} + + +/* + * Initialize for an output processing pass. + */ + +METHODDEF(void) +start_output_pass (j_decompress_ptr cinfo) +{ +#ifdef BLOCK_SMOOTHING_SUPPORTED + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + + /* If multipass, check to see whether to use block smoothing on this pass */ + if (coef->pub.coef_arrays != NULL) { + if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) + coef->pub.decompress_data = decompress_smooth_data; + else + coef->pub.decompress_data = decompress_data; + } +#endif + cinfo->output_iMCU_row = 0; +} + + +/* + * Decompress and return some data in the single-pass case. + * Always attempts to emit one fully interleaved MCU row ("iMCU" row). + * Input and output must run in lockstep since we have only a one-MCU buffer. + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. + * + * NB: output_buf contains a plane for each component in image, + * which we index according to the component's SOF position. + */ + +METHODDEF(int) +decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION MCU_col_num; /* index of current MCU within row */ + JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; + int blkn, ci, xindex, yindex, yoffset, useful_width; + JSAMPARRAY output_ptr; + JDIMENSION start_col, output_col; + jpeg_component_info *compptr; + inverse_DCT_method_ptr inverse_DCT; + + /* Loop to process as much as one whole iMCU row */ + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; + yoffset++) { + for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; + MCU_col_num++) { + /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ + if (cinfo->lim_Se) /* can bypass in DC only case */ + FMEMZERO((void FAR *) coef->MCU_buffer[0], + (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); + if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { + /* Suspension forced; update state counters and exit */ + coef->MCU_vert_offset = yoffset; + coef->MCU_ctr = MCU_col_num; + return JPEG_SUSPENDED; + } + /* Determine where data should go in output_buf and do the IDCT thing. + * We skip dummy blocks at the right and bottom edges (but blkn gets + * incremented past them!). Note the inner loop relies on having + * allocated the MCU_buffer[] blocks sequentially. + */ + blkn = 0; /* index of current DCT block within MCU */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* Don't bother to IDCT an uninteresting component. */ + if (! compptr->component_needed) { + blkn += compptr->MCU_blocks; + continue; + } + inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; + useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width + : compptr->last_col_width; + output_ptr = output_buf[compptr->component_index] + + yoffset * compptr->DCT_v_scaled_size; + start_col = MCU_col_num * compptr->MCU_sample_width; + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { + if (cinfo->input_iMCU_row < last_iMCU_row || + yoffset+yindex < compptr->last_row_height) { + output_col = start_col; + for (xindex = 0; xindex < useful_width; xindex++) { + (*inverse_DCT) (cinfo, compptr, + (JCOEFPTR) coef->MCU_buffer[blkn+xindex], + output_ptr, output_col); + output_col += compptr->DCT_h_scaled_size; + } + } + blkn += compptr->MCU_width; + output_ptr += compptr->DCT_v_scaled_size; + } + } + } + /* Completed an MCU row, but perhaps not an iMCU row */ + coef->MCU_ctr = 0; + } + /* Completed the iMCU row, advance counters for next one */ + cinfo->output_iMCU_row++; + if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { + start_iMCU_row(cinfo); + return JPEG_ROW_COMPLETED; + } + /* Completed the scan */ + (*cinfo->inputctl->finish_input_pass) (cinfo); + return JPEG_SCAN_COMPLETED; +} + + +/* + * Dummy consume-input routine for single-pass operation. + */ + +METHODDEF(int) +dummy_consume_data (j_decompress_ptr cinfo) +{ + return JPEG_SUSPENDED; /* Always indicate nothing was done */ +} + + +#ifdef D_MULTISCAN_FILES_SUPPORTED + +/* + * Consume input data and store it in the full-image coefficient buffer. + * We read as much as one fully interleaved MCU row ("iMCU" row) per call, + * ie, v_samp_factor block rows for each component in the scan. + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. + */ + +METHODDEF(int) +consume_data (j_decompress_ptr cinfo) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION MCU_col_num; /* index of current MCU within row */ + int blkn, ci, xindex, yindex, yoffset; + JDIMENSION start_col; + JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; + JBLOCKROW buffer_ptr; + jpeg_component_info *compptr; + + /* Align the virtual buffers for the components used in this scan. */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + buffer[ci] = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], + cinfo->input_iMCU_row * compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, TRUE); + /* Note: entropy decoder expects buffer to be zeroed, + * but this is handled automatically by the memory manager + * because we requested a pre-zeroed array. + */ + } + + /* Loop to process one whole iMCU row */ + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; + yoffset++) { + for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; + MCU_col_num++) { + /* Construct list of pointers to DCT blocks belonging to this MCU */ + blkn = 0; /* index of current DCT block within MCU */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + start_col = MCU_col_num * compptr->MCU_width; + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { + buffer_ptr = buffer[ci][yindex+yoffset] + start_col; + for (xindex = 0; xindex < compptr->MCU_width; xindex++) { + coef->MCU_buffer[blkn++] = buffer_ptr++; + } + } + } + /* Try to fetch the MCU. */ + if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { + /* Suspension forced; update state counters and exit */ + coef->MCU_vert_offset = yoffset; + coef->MCU_ctr = MCU_col_num; + return JPEG_SUSPENDED; + } + } + /* Completed an MCU row, but perhaps not an iMCU row */ + coef->MCU_ctr = 0; + } + /* Completed the iMCU row, advance counters for next one */ + if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { + start_iMCU_row(cinfo); + return JPEG_ROW_COMPLETED; + } + /* Completed the scan */ + (*cinfo->inputctl->finish_input_pass) (cinfo); + return JPEG_SCAN_COMPLETED; +} + + +/* + * Decompress and return some data in the multi-pass case. + * Always attempts to emit one fully interleaved MCU row ("iMCU" row). + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. + * + * NB: output_buf contains a plane for each component in image. + */ + +METHODDEF(int) +decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; + JDIMENSION block_num; + int ci, block_row, block_rows; + JBLOCKARRAY buffer; + JBLOCKROW buffer_ptr; + JSAMPARRAY output_ptr; + JDIMENSION output_col; + jpeg_component_info *compptr; + inverse_DCT_method_ptr inverse_DCT; + + /* Force some input to be done if we are getting ahead of the input. */ + while (cinfo->input_scan_number < cinfo->output_scan_number || + (cinfo->input_scan_number == cinfo->output_scan_number && + cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { + if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) + return JPEG_SUSPENDED; + } + + /* OK, output from the virtual arrays. */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Don't bother to IDCT an uninteresting component. */ + if (! compptr->component_needed) + continue; + /* Align the virtual buffer for this component. */ + buffer = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[ci], + cinfo->output_iMCU_row * compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, FALSE); + /* Count non-dummy DCT block rows in this iMCU row. */ + if (cinfo->output_iMCU_row < last_iMCU_row) + block_rows = compptr->v_samp_factor; + else { + /* NB: can't use last_row_height here; it is input-side-dependent! */ + block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); + if (block_rows == 0) block_rows = compptr->v_samp_factor; + } + inverse_DCT = cinfo->idct->inverse_DCT[ci]; + output_ptr = output_buf[ci]; + /* Loop over all DCT blocks to be processed. */ + for (block_row = 0; block_row < block_rows; block_row++) { + buffer_ptr = buffer[block_row]; + output_col = 0; + for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { + (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, + output_ptr, output_col); + buffer_ptr++; + output_col += compptr->DCT_h_scaled_size; + } + output_ptr += compptr->DCT_v_scaled_size; + } + } + + if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) + return JPEG_ROW_COMPLETED; + return JPEG_SCAN_COMPLETED; +} + +#endif /* D_MULTISCAN_FILES_SUPPORTED */ + + +#ifdef BLOCK_SMOOTHING_SUPPORTED + +/* + * This code applies interblock smoothing as described by section K.8 + * of the JPEG standard: the first 5 AC coefficients are estimated from + * the DC values of a DCT block and its 8 neighboring blocks. + * We apply smoothing only for progressive JPEG decoding, and only if + * the coefficients it can estimate are not yet known to full precision. + */ + +/* Natural-order array positions of the first 5 zigzag-order coefficients */ +#define Q01_POS 1 +#define Q10_POS 8 +#define Q20_POS 16 +#define Q11_POS 9 +#define Q02_POS 2 + +/* + * Determine whether block smoothing is applicable and safe. + * We also latch the current states of the coef_bits[] entries for the + * AC coefficients; otherwise, if the input side of the decompressor + * advances into a new scan, we might think the coefficients are known + * more accurately than they really are. + */ + +LOCAL(boolean) +smoothing_ok (j_decompress_ptr cinfo) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + boolean smoothing_useful = FALSE; + int ci, coefi; + jpeg_component_info *compptr; + JQUANT_TBL * qtable; + int * coef_bits; + int * coef_bits_latch; + + if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) + return FALSE; + + /* Allocate latch area if not already done */ + if (coef->coef_bits_latch == NULL) + coef->coef_bits_latch = (int *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->num_components * + (SAVED_COEFS * SIZEOF(int))); + coef_bits_latch = coef->coef_bits_latch; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* All components' quantization values must already be latched. */ + if ((qtable = compptr->quant_table) == NULL) + return FALSE; + /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ + if (qtable->quantval[0] == 0 || + qtable->quantval[Q01_POS] == 0 || + qtable->quantval[Q10_POS] == 0 || + qtable->quantval[Q20_POS] == 0 || + qtable->quantval[Q11_POS] == 0 || + qtable->quantval[Q02_POS] == 0) + return FALSE; + /* DC values must be at least partly known for all components. */ + coef_bits = cinfo->coef_bits[ci]; + if (coef_bits[0] < 0) + return FALSE; + /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ + for (coefi = 1; coefi <= 5; coefi++) { + coef_bits_latch[coefi] = coef_bits[coefi]; + if (coef_bits[coefi] != 0) + smoothing_useful = TRUE; + } + coef_bits_latch += SAVED_COEFS; + } + + return smoothing_useful; +} + + +/* + * Variant of decompress_data for use when doing block smoothing. + */ + +METHODDEF(int) +decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) +{ + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; + JDIMENSION block_num, last_block_column; + int ci, block_row, block_rows, access_rows; + JBLOCKARRAY buffer; + JBLOCKROW buffer_ptr, prev_block_row, next_block_row; + JSAMPARRAY output_ptr; + JDIMENSION output_col; + jpeg_component_info *compptr; + inverse_DCT_method_ptr inverse_DCT; + boolean first_row, last_row; + JBLOCK workspace; + int *coef_bits; + JQUANT_TBL *quanttbl; + INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; + int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; + int Al, pred; + + /* Force some input to be done if we are getting ahead of the input. */ + while (cinfo->input_scan_number <= cinfo->output_scan_number && + ! cinfo->inputctl->eoi_reached) { + if (cinfo->input_scan_number == cinfo->output_scan_number) { + /* If input is working on current scan, we ordinarily want it to + * have completed the current row. But if input scan is DC, + * we want it to keep one row ahead so that next block row's DC + * values are up to date. + */ + JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; + if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) + break; + } + if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) + return JPEG_SUSPENDED; + } + + /* OK, output from the virtual arrays. */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Don't bother to IDCT an uninteresting component. */ + if (! compptr->component_needed) + continue; + /* Count non-dummy DCT block rows in this iMCU row. */ + if (cinfo->output_iMCU_row < last_iMCU_row) { + block_rows = compptr->v_samp_factor; + access_rows = block_rows * 2; /* this and next iMCU row */ + last_row = FALSE; + } else { + /* NB: can't use last_row_height here; it is input-side-dependent! */ + block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); + if (block_rows == 0) block_rows = compptr->v_samp_factor; + access_rows = block_rows; /* this iMCU row only */ + last_row = TRUE; + } + /* Align the virtual buffer for this component. */ + if (cinfo->output_iMCU_row > 0) { + access_rows += compptr->v_samp_factor; /* prior iMCU row too */ + buffer = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[ci], + (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, + (JDIMENSION) access_rows, FALSE); + buffer += compptr->v_samp_factor; /* point to current iMCU row */ + first_row = FALSE; + } else { + buffer = (*cinfo->mem->access_virt_barray) + ((j_common_ptr) cinfo, coef->whole_image[ci], + (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); + first_row = TRUE; + } + /* Fetch component-dependent info */ + coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); + quanttbl = compptr->quant_table; + Q00 = quanttbl->quantval[0]; + Q01 = quanttbl->quantval[Q01_POS]; + Q10 = quanttbl->quantval[Q10_POS]; + Q20 = quanttbl->quantval[Q20_POS]; + Q11 = quanttbl->quantval[Q11_POS]; + Q02 = quanttbl->quantval[Q02_POS]; + inverse_DCT = cinfo->idct->inverse_DCT[ci]; + output_ptr = output_buf[ci]; + /* Loop over all DCT blocks to be processed. */ + for (block_row = 0; block_row < block_rows; block_row++) { + buffer_ptr = buffer[block_row]; + if (first_row && block_row == 0) + prev_block_row = buffer_ptr; + else + prev_block_row = buffer[block_row-1]; + if (last_row && block_row == block_rows-1) + next_block_row = buffer_ptr; + else + next_block_row = buffer[block_row+1]; + /* We fetch the surrounding DC values using a sliding-register approach. + * Initialize all nine here so as to do the right thing on narrow pics. + */ + DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; + DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; + DC7 = DC8 = DC9 = (int) next_block_row[0][0]; + output_col = 0; + last_block_column = compptr->width_in_blocks - 1; + for (block_num = 0; block_num <= last_block_column; block_num++) { + /* Fetch current DCT block into workspace so we can modify it. */ + jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); + /* Update DC values */ + if (block_num < last_block_column) { + DC3 = (int) prev_block_row[1][0]; + DC6 = (int) buffer_ptr[1][0]; + DC9 = (int) next_block_row[1][0]; + } + /* Compute coefficient estimates per K.8. + * An estimate is applied only if coefficient is still zero, + * and is not known to be fully accurate. + */ + /* AC01 */ + if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { + num = 36 * Q00 * (DC4 - DC6); + if (num >= 0) { + pred = (int) (((Q01<<7) + num) / (Q01<<8)); + if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { + pred = (int) (((Q10<<7) + num) / (Q10<<8)); + if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { + pred = (int) (((Q20<<7) + num) / (Q20<<8)); + if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { + pred = (int) (((Q11<<7) + num) / (Q11<<8)); + if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { + pred = (int) (((Q02<<7) + num) / (Q02<<8)); + if (Al > 0 && pred >= (1< 0 && pred >= (1<DCT_h_scaled_size; + } + output_ptr += compptr->DCT_v_scaled_size; + } + } + + if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) + return JPEG_ROW_COMPLETED; + return JPEG_SCAN_COMPLETED; +} + +#endif /* BLOCK_SMOOTHING_SUPPORTED */ + + +/* + * Initialize coefficient buffer controller. + */ + +GLOBAL(void) +jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) +{ + my_coef_ptr coef; + + coef = (my_coef_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_coef_controller)); + cinfo->coef = (struct jpeg_d_coef_controller *) coef; + coef->pub.start_input_pass = start_input_pass; + coef->pub.start_output_pass = start_output_pass; +#ifdef BLOCK_SMOOTHING_SUPPORTED + coef->coef_bits_latch = NULL; +#endif + + /* Create the coefficient buffer. */ + if (need_full_buffer) { +#ifdef D_MULTISCAN_FILES_SUPPORTED + /* Allocate a full-image virtual array for each component, */ + /* padded to a multiple of samp_factor DCT blocks in each direction. */ + /* Note we ask for a pre-zeroed array. */ + int ci, access_rows; + jpeg_component_info *compptr; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + access_rows = compptr->v_samp_factor; +#ifdef BLOCK_SMOOTHING_SUPPORTED + /* If block smoothing could be used, need a bigger window */ + if (cinfo->progressive_mode) + access_rows *= 3; +#endif + coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, + (JDIMENSION) jround_up((long) compptr->width_in_blocks, + (long) compptr->h_samp_factor), + (JDIMENSION) jround_up((long) compptr->height_in_blocks, + (long) compptr->v_samp_factor), + (JDIMENSION) access_rows); + } + coef->pub.consume_data = consume_data; + coef->pub.decompress_data = decompress_data; + coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } else { + /* We only need a single-MCU buffer. */ + JBLOCKROW buffer; + int i; + + buffer = (JBLOCKROW) + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, + D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); + for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { + coef->MCU_buffer[i] = buffer + i; + } + if (cinfo->lim_Se == 0) /* DC only case: want to bypass later */ + FMEMZERO((void FAR *) buffer, + (size_t) (D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK))); + coef->pub.consume_data = dummy_consume_data; + coef->pub.decompress_data = decompress_onepass; + coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ + } +} diff --git a/crypto777/jpeg/jdcolor.c b/crypto777/jpeg/jdcolor.c new file mode 100644 index 000000000..83e4d069a --- /dev/null +++ b/crypto777/jpeg/jdcolor.c @@ -0,0 +1,512 @@ +/* + * jdcolor.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains output colorspace conversion routines. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Private subobject */ + +typedef struct { + struct jpeg_color_deconverter pub; /* public fields */ + + /* Private state for YCC->RGB conversion */ + int * Cr_r_tab; /* => table for Cr to R conversion */ + int * Cb_b_tab; /* => table for Cb to B conversion */ + INT32 * Cr_g_tab; /* => table for Cr to G conversion */ + INT32 * Cb_g_tab; /* => table for Cb to G conversion */ + + /* Private state for RGB->Y conversion */ + INT32 * rgb_y_tab; /* => table for RGB to Y conversion */ +} my_color_deconverter; + +typedef my_color_deconverter * my_cconvert_ptr; + + +/**************** YCbCr -> RGB conversion: most common case **************/ +/**************** RGB -> Y conversion: less common case **************/ + +/* + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. + * The conversion equations to be implemented are therefore + * + * R = Y + 1.40200 * Cr + * G = Y - 0.34414 * Cb - 0.71414 * Cr + * B = Y + 1.77200 * Cb + * + * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B + * + * where Cb and Cr represent the incoming values less CENTERJSAMPLE. + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) + * + * To avoid floating-point arithmetic, we represent the fractional constants + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide + * the products by 2^16, with appropriate rounding, to get the correct answer. + * Notice that Y, being an integral input, does not contribute any fraction + * so it need not participate in the rounding. + * + * For even more speed, we avoid doing any multiplications in the inner loop + * by precalculating the constants times Cb and Cr for all possible values. + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); + * for 12-bit samples it is still acceptable. It's not very reasonable for + * 16-bit samples, but if you want lossless storage you shouldn't be changing + * colorspace anyway. + * The Cr=>R and Cb=>B values can be rounded to integers in advance; the + * values for the G calculation are left scaled up, since we must add them + * together before rounding. + */ + +#define SCALEBITS 16 /* speediest right-shift on some machines */ +#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) +#define FIX(x) ((INT32) ((x) * (1L<Y conversion and divide it up into + * three parts, instead of doing three alloc_small requests. This lets us + * use a single table base address, which can be held in a register in the + * inner loops on many machines (more than can hold all three addresses, + * anyway). + */ + +#define R_Y_OFF 0 /* offset to R => Y section */ +#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ +#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ +#define TABLE_SIZE (3*(MAXJSAMPLE+1)) + + +/* + * Initialize tables for YCC->RGB colorspace conversion. + */ + +LOCAL(void) +build_ycc_rgb_table (j_decompress_ptr cinfo) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + int i; + INT32 x; + SHIFT_TEMPS + + cconvert->Cr_r_tab = (int *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(int)); + cconvert->Cb_b_tab = (int *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(int)); + cconvert->Cr_g_tab = (INT32 *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(INT32)); + cconvert->Cb_g_tab = (INT32 *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(INT32)); + + for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { + /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ + /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ + /* Cr=>R value is nearest int to 1.40200 * x */ + cconvert->Cr_r_tab[i] = (int) + RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); + /* Cb=>B value is nearest int to 1.77200 * x */ + cconvert->Cb_b_tab[i] = (int) + RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); + /* Cr=>G value is scaled-up -0.71414 * x */ + cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; + /* Cb=>G value is scaled-up -0.34414 * x */ + /* We also add in ONE_HALF so that need not do it in inner loop */ + cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; + } +} + + +/* + * Convert some rows of samples to the output colorspace. + * + * Note that we change from noninterleaved, one-plane-per-component format + * to interleaved-pixel format. The output buffer is therefore three times + * as wide as the input buffer. + * A starting row offset is provided only for the input buffer. The caller + * can easily adjust the passed output_buf value to accommodate any row + * offset required on that side. + */ + +METHODDEF(void) +ycc_rgb_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + register int y, cb, cr; + register JSAMPROW outptr; + register JSAMPROW inptr0, inptr1, inptr2; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->output_width; + /* copy these pointers into registers if possible */ + register JSAMPLE * range_limit = cinfo->sample_range_limit; + register int * Crrtab = cconvert->Cr_r_tab; + register int * Cbbtab = cconvert->Cb_b_tab; + register INT32 * Crgtab = cconvert->Cr_g_tab; + register INT32 * Cbgtab = cconvert->Cb_g_tab; + SHIFT_TEMPS + + while (--num_rows >= 0) { + inptr0 = input_buf[0][input_row]; + inptr1 = input_buf[1][input_row]; + inptr2 = input_buf[2][input_row]; + input_row++; + outptr = *output_buf++; + for (col = 0; col < num_cols; col++) { + y = GETJSAMPLE(inptr0[col]); + cb = GETJSAMPLE(inptr1[col]); + cr = GETJSAMPLE(inptr2[col]); + /* Range-limiting is essential due to noise introduced by DCT losses. */ + outptr[RGB_RED] = range_limit[y + Crrtab[cr]]; + outptr[RGB_GREEN] = range_limit[y + + ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], + SCALEBITS))]; + outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]]; + outptr += RGB_PIXELSIZE; + } + } +} + + +/**************** Cases other than YCbCr -> RGB **************/ + + +/* + * Initialize for RGB->grayscale colorspace conversion. + */ + +LOCAL(void) +build_rgb_y_table (j_decompress_ptr cinfo) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + INT32 * rgb_y_tab; + INT32 i; + + /* Allocate and fill in the conversion tables. */ + cconvert->rgb_y_tab = rgb_y_tab = (INT32 *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (TABLE_SIZE * SIZEOF(INT32))); + + for (i = 0; i <= MAXJSAMPLE; i++) { + rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i; + rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i; + rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; + } +} + + +/* + * Convert RGB to grayscale. + */ + +METHODDEF(void) +rgb_gray_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + register int r, g, b; + register INT32 * ctab = cconvert->rgb_y_tab; + register JSAMPROW outptr; + register JSAMPROW inptr0, inptr1, inptr2; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->output_width; + + while (--num_rows >= 0) { + inptr0 = input_buf[0][input_row]; + inptr1 = input_buf[1][input_row]; + inptr2 = input_buf[2][input_row]; + input_row++; + outptr = *output_buf++; + for (col = 0; col < num_cols; col++) { + r = GETJSAMPLE(inptr0[col]); + g = GETJSAMPLE(inptr1[col]); + b = GETJSAMPLE(inptr2[col]); + /* Y */ + outptr[col] = (JSAMPLE) + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) + >> SCALEBITS); + } + } +} + + +/* + * No colorspace change, but conversion from separate-planes + * to interleaved representation. + */ + +METHODDEF(void) +rgb_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + register JSAMPROW outptr; + register JSAMPROW inptr0, inptr1, inptr2; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->output_width; + + while (--num_rows >= 0) { + inptr0 = input_buf[0][input_row]; + inptr1 = input_buf[1][input_row]; + inptr2 = input_buf[2][input_row]; + input_row++; + outptr = *output_buf++; + for (col = 0; col < num_cols; col++) { + /* We can dispense with GETJSAMPLE() here */ + outptr[RGB_RED] = inptr0[col]; + outptr[RGB_GREEN] = inptr1[col]; + outptr[RGB_BLUE] = inptr2[col]; + outptr += RGB_PIXELSIZE; + } + } +} + + +/* + * Color conversion for no colorspace change: just copy the data, + * converting from separate-planes to interleaved representation. + */ + +METHODDEF(void) +null_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + register JSAMPROW inptr, outptr; + register JDIMENSION count; + register int num_components = cinfo->num_components; + JDIMENSION num_cols = cinfo->output_width; + int ci; + + while (--num_rows >= 0) { + for (ci = 0; ci < num_components; ci++) { + inptr = input_buf[ci][input_row]; + outptr = output_buf[0] + ci; + for (count = num_cols; count > 0; count--) { + *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */ + outptr += num_components; + } + } + input_row++; + output_buf++; + } +} + + +/* + * Color conversion for grayscale: just copy the data. + * This also works for YCbCr -> grayscale conversion, in which + * we just copy the Y (luminance) component and ignore chrominance. + */ + +METHODDEF(void) +grayscale_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, + num_rows, cinfo->output_width); +} + + +/* + * Convert grayscale to RGB: just duplicate the graylevel three times. + * This is provided to support applications that don't want to cope + * with grayscale as a separate case. + */ + +METHODDEF(void) +gray_rgb_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + register JSAMPROW inptr, outptr; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->output_width; + + while (--num_rows >= 0) { + inptr = input_buf[0][input_row++]; + outptr = *output_buf++; + for (col = 0; col < num_cols; col++) { + /* We can dispense with GETJSAMPLE() here */ + outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col]; + outptr += RGB_PIXELSIZE; + } + } +} + + +/* + * Adobe-style YCCK->CMYK conversion. + * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same + * conversion as above, while passing K (black) unchanged. + * We assume build_ycc_rgb_table has been called. + */ + +METHODDEF(void) +ycck_cmyk_convert (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows) +{ + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; + register int y, cb, cr; + register JSAMPROW outptr; + register JSAMPROW inptr0, inptr1, inptr2, inptr3; + register JDIMENSION col; + JDIMENSION num_cols = cinfo->output_width; + /* copy these pointers into registers if possible */ + register JSAMPLE * range_limit = cinfo->sample_range_limit; + register int * Crrtab = cconvert->Cr_r_tab; + register int * Cbbtab = cconvert->Cb_b_tab; + register INT32 * Crgtab = cconvert->Cr_g_tab; + register INT32 * Cbgtab = cconvert->Cb_g_tab; + SHIFT_TEMPS + + while (--num_rows >= 0) { + inptr0 = input_buf[0][input_row]; + inptr1 = input_buf[1][input_row]; + inptr2 = input_buf[2][input_row]; + inptr3 = input_buf[3][input_row]; + input_row++; + outptr = *output_buf++; + for (col = 0; col < num_cols; col++) { + y = GETJSAMPLE(inptr0[col]); + cb = GETJSAMPLE(inptr1[col]); + cr = GETJSAMPLE(inptr2[col]); + /* Range-limiting is essential due to noise introduced by DCT losses. */ + outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */ + outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */ + ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], + SCALEBITS)))]; + outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */ + /* K passes through unchanged */ + outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */ + outptr += 4; + } + } +} + + +/* + * Empty method for start_pass. + */ + +METHODDEF(void) +start_pass_dcolor (j_decompress_ptr cinfo) +{ + /* no work needed */ +} + + +/* + * Module initialization routine for output colorspace conversion. + */ + +GLOBAL(void) +jinit_color_deconverter (j_decompress_ptr cinfo) +{ + my_cconvert_ptr cconvert; + int ci; + + cconvert = (my_cconvert_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_color_deconverter)); + cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert; + cconvert->pub.start_pass = start_pass_dcolor; + + /* Make sure num_components agrees with jpeg_color_space */ + switch (cinfo->jpeg_color_space) { + case JCS_GRAYSCALE: + if (cinfo->num_components != 1) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + break; + + case JCS_RGB: + case JCS_YCbCr: + if (cinfo->num_components != 3) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + break; + + case JCS_CMYK: + case JCS_YCCK: + if (cinfo->num_components != 4) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + break; + + default: /* JCS_UNKNOWN can be anything */ + if (cinfo->num_components < 1) + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); + break; + } + + /* Set out_color_components and conversion method based on requested space. + * Also clear the component_needed flags for any unused components, + * so that earlier pipeline stages can avoid useless computation. + */ + + switch (cinfo->out_color_space) { + case JCS_GRAYSCALE: + cinfo->out_color_components = 1; + if (cinfo->jpeg_color_space == JCS_GRAYSCALE || + cinfo->jpeg_color_space == JCS_YCbCr) { + cconvert->pub.color_convert = grayscale_convert; + /* For color->grayscale conversion, only the Y (0) component is needed */ + for (ci = 1; ci < cinfo->num_components; ci++) + cinfo->comp_info[ci].component_needed = FALSE; + } else if (cinfo->jpeg_color_space == JCS_RGB) { + cconvert->pub.color_convert = rgb_gray_convert; + build_rgb_y_table(cinfo); + } else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + case JCS_RGB: + cinfo->out_color_components = RGB_PIXELSIZE; + if (cinfo->jpeg_color_space == JCS_YCbCr) { + cconvert->pub.color_convert = ycc_rgb_convert; + build_ycc_rgb_table(cinfo); + } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { + cconvert->pub.color_convert = gray_rgb_convert; + } else if (cinfo->jpeg_color_space == JCS_RGB) { + cconvert->pub.color_convert = rgb_convert; + } else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + case JCS_CMYK: + cinfo->out_color_components = 4; + if (cinfo->jpeg_color_space == JCS_YCCK) { + cconvert->pub.color_convert = ycck_cmyk_convert; + build_ycc_rgb_table(cinfo); + } else if (cinfo->jpeg_color_space == JCS_CMYK) { + cconvert->pub.color_convert = null_convert; + } else + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + + default: + /* Permit null conversion to same output space */ + if (cinfo->out_color_space == cinfo->jpeg_color_space) { + cinfo->out_color_components = cinfo->num_components; + cconvert->pub.color_convert = null_convert; + } else /* unsupported non-null conversion */ + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); + break; + } + + if (cinfo->quantize_colors) + cinfo->output_components = 1; /* single colormapped output component */ + else + cinfo->output_components = cinfo->out_color_components; +} diff --git a/crypto777/jpeg/jdct.h b/crypto777/jpeg/jdct.h new file mode 100644 index 000000000..360dec80c --- /dev/null +++ b/crypto777/jpeg/jdct.h @@ -0,0 +1,393 @@ +/* + * jdct.h + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This include file contains common declarations for the forward and + * inverse DCT modules. These declarations are private to the DCT managers + * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms. + * The individual DCT algorithms are kept in separate files to ease + * machine-dependent tuning (e.g., assembly coding). + */ + + +/* + * A forward DCT routine is given a pointer to an input sample array and + * a pointer to a work area of type DCTELEM[]; the DCT is to be performed + * in-place in that buffer. Type DCTELEM is int for 8-bit samples, INT32 + * for 12-bit samples. (NOTE: Floating-point DCT implementations use an + * array of type FAST_FLOAT, instead.) + * The input data is to be fetched from the sample array starting at a + * specified column. (Any row offset needed will be applied to the array + * pointer before it is passed to the FDCT code.) + * Note that the number of samples fetched by the FDCT routine is + * DCT_h_scaled_size * DCT_v_scaled_size. + * The DCT outputs are returned scaled up by a factor of 8; they therefore + * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This + * convention improves accuracy in integer implementations and saves some + * work in floating-point ones. + * Quantization of the output coefficients is done by jcdctmgr.c. + */ + +#if BITS_IN_JSAMPLE == 8 +typedef int DCTELEM; /* 16 or 32 bits is fine */ +#else +typedef INT32 DCTELEM; /* must have 32 bits */ +#endif + +typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data, + JSAMPARRAY sample_data, + JDIMENSION start_col)); +typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data, + JSAMPARRAY sample_data, + JDIMENSION start_col)); + + +/* + * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer + * to an output sample array. The routine must dequantize the input data as + * well as perform the IDCT; for dequantization, it uses the multiplier table + * pointed to by compptr->dct_table. The output data is to be placed into the + * sample array starting at a specified column. (Any row offset needed will + * be applied to the array pointer before it is passed to the IDCT code.) + * Note that the number of samples emitted by the IDCT routine is + * DCT_h_scaled_size * DCT_v_scaled_size. + */ + +/* typedef inverse_DCT_method_ptr is declared in jpegint.h */ + +/* + * Each IDCT routine has its own ideas about the best dct_table element type. + */ + +typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ +#if BITS_IN_JSAMPLE == 8 +typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ +#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ +#else +typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ +#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ +#endif +typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */ + + +/* + * Each IDCT routine is responsible for range-limiting its results and + * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could + * be quite far out of range if the input data is corrupt, so a bulletproof + * range-limiting step is required. We use a mask-and-table-lookup method + * to do the combined operations quickly. See the comments with + * prepare_range_limit_table (in jdmaster.c) for more info. + */ + +#define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE) + +#define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */ + + +/* Short forms of external names for systems with brain-damaged linkers. */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jpeg_fdct_islow jFDislow +#define jpeg_fdct_ifast jFDifast +#define jpeg_fdct_float jFDfloat +#define jpeg_fdct_7x7 jFD7x7 +#define jpeg_fdct_6x6 jFD6x6 +#define jpeg_fdct_5x5 jFD5x5 +#define jpeg_fdct_4x4 jFD4x4 +#define jpeg_fdct_3x3 jFD3x3 +#define jpeg_fdct_2x2 jFD2x2 +#define jpeg_fdct_1x1 jFD1x1 +#define jpeg_fdct_9x9 jFD9x9 +#define jpeg_fdct_10x10 jFD10x10 +#define jpeg_fdct_11x11 jFD11x11 +#define jpeg_fdct_12x12 jFD12x12 +#define jpeg_fdct_13x13 jFD13x13 +#define jpeg_fdct_14x14 jFD14x14 +#define jpeg_fdct_15x15 jFD15x15 +#define jpeg_fdct_16x16 jFD16x16 +#define jpeg_fdct_16x8 jFD16x8 +#define jpeg_fdct_14x7 jFD14x7 +#define jpeg_fdct_12x6 jFD12x6 +#define jpeg_fdct_10x5 jFD10x5 +#define jpeg_fdct_8x4 jFD8x4 +#define jpeg_fdct_6x3 jFD6x3 +#define jpeg_fdct_4x2 jFD4x2 +#define jpeg_fdct_2x1 jFD2x1 +#define jpeg_fdct_8x16 jFD8x16 +#define jpeg_fdct_7x14 jFD7x14 +#define jpeg_fdct_6x12 jFD6x12 +#define jpeg_fdct_5x10 jFD5x10 +#define jpeg_fdct_4x8 jFD4x8 +#define jpeg_fdct_3x6 jFD3x6 +#define jpeg_fdct_2x4 jFD2x4 +#define jpeg_fdct_1x2 jFD1x2 +#define jpeg_idct_islow jRDislow +#define jpeg_idct_ifast jRDifast +#define jpeg_idct_float jRDfloat +#define jpeg_idct_7x7 jRD7x7 +#define jpeg_idct_6x6 jRD6x6 +#define jpeg_idct_5x5 jRD5x5 +#define jpeg_idct_4x4 jRD4x4 +#define jpeg_idct_3x3 jRD3x3 +#define jpeg_idct_2x2 jRD2x2 +#define jpeg_idct_1x1 jRD1x1 +#define jpeg_idct_9x9 jRD9x9 +#define jpeg_idct_10x10 jRD10x10 +#define jpeg_idct_11x11 jRD11x11 +#define jpeg_idct_12x12 jRD12x12 +#define jpeg_idct_13x13 jRD13x13 +#define jpeg_idct_14x14 jRD14x14 +#define jpeg_idct_15x15 jRD15x15 +#define jpeg_idct_16x16 jRD16x16 +#define jpeg_idct_16x8 jRD16x8 +#define jpeg_idct_14x7 jRD14x7 +#define jpeg_idct_12x6 jRD12x6 +#define jpeg_idct_10x5 jRD10x5 +#define jpeg_idct_8x4 jRD8x4 +#define jpeg_idct_6x3 jRD6x3 +#define jpeg_idct_4x2 jRD4x2 +#define jpeg_idct_2x1 jRD2x1 +#define jpeg_idct_8x16 jRD8x16 +#define jpeg_idct_7x14 jRD7x14 +#define jpeg_idct_6x12 jRD6x12 +#define jpeg_idct_5x10 jRD5x10 +#define jpeg_idct_4x8 jRD4x8 +#define jpeg_idct_3x6 jRD3x8 +#define jpeg_idct_2x4 jRD2x4 +#define jpeg_idct_1x2 jRD1x2 +#endif /* NEED_SHORT_EXTERNAL_NAMES */ + +/* Extern declarations for the forward and inverse DCT routines. */ + +EXTERN(void) jpeg_fdct_islow + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_ifast + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_float + JPP((FAST_FLOAT * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_7x7 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_6x6 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_5x5 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_4x4 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_3x3 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_2x2 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_1x1 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_9x9 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_10x10 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_11x11 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_12x12 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_13x13 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_14x14 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_15x15 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_16x16 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_16x8 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_14x7 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_12x6 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_10x5 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_8x4 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_6x3 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_4x2 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_2x1 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_8x16 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_7x14 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_6x12 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_5x10 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_4x8 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_3x6 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_2x4 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); +EXTERN(void) jpeg_fdct_1x2 + JPP((DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)); + +EXTERN(void) jpeg_idct_islow + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_ifast + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_float + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_7x7 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_6x6 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_5x5 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_4x4 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_3x3 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_2x2 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_1x1 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_9x9 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_10x10 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_11x11 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_12x12 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_13x13 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_14x14 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_15x15 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_16x16 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_16x8 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_14x7 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_12x6 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_10x5 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_8x4 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_6x3 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_4x2 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_2x1 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_8x16 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_7x14 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_6x12 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_5x10 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_4x8 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_3x6 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_2x4 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); +EXTERN(void) jpeg_idct_1x2 + JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); + + +/* + * Macros for handling fixed-point arithmetic; these are used by many + * but not all of the DCT/IDCT modules. + * + * All values are expected to be of type INT32. + * Fractional constants are scaled left by CONST_BITS bits. + * CONST_BITS is defined within each module using these macros, + * and may differ from one module to the next. + */ + +#define ONE ((INT32) 1) +#define CONST_SCALE (ONE << CONST_BITS) + +/* Convert a positive real constant to an integer scaled by CONST_SCALE. + * Caution: some C compilers fail to reduce "FIX(constant)" at compile time, + * thus causing a lot of useless floating-point operations at run time. + */ + +#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5)) + +/* Descale and correctly round an INT32 value that's scaled by N bits. + * We assume RIGHT_SHIFT rounds towards minus infinity, so adding + * the fudge factor is correct for either sign of X. + */ + +#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n) + +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. + * This macro is used only when the two inputs will actually be no more than + * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a + * full 32x32 multiply. This provides a useful speedup on many machines. + * Unfortunately there is no way to specify a 16x16->32 multiply portably + * in C, but some C compilers will do the right thing if you provide the + * correct combination of casts. + */ + +#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ +#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const))) +#endif +#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ +#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const))) +#endif + +#ifndef MULTIPLY16C16 /* default definition */ +#define MULTIPLY16C16(var,const) ((var) * (const)) +#endif + +/* Same except both inputs are variables. */ + +#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ +#define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2))) +#endif + +#ifndef MULTIPLY16V16 /* default definition */ +#define MULTIPLY16V16(var1,var2) ((var1) * (var2)) +#endif diff --git a/crypto777/jpeg/jddctmgr.c b/crypto777/jpeg/jddctmgr.c new file mode 100644 index 000000000..0ded9d574 --- /dev/null +++ b/crypto777/jpeg/jddctmgr.c @@ -0,0 +1,384 @@ +/* + * jddctmgr.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * Modified 2002-2010 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the inverse-DCT management logic. + * This code selects a particular IDCT implementation to be used, + * and it performs related housekeeping chores. No code in this file + * is executed per IDCT step, only during output pass setup. + * + * Note that the IDCT routines are responsible for performing coefficient + * dequantization as well as the IDCT proper. This module sets up the + * dequantization multiplier table needed by the IDCT routine. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + + +/* + * The decompressor input side (jdinput.c) saves away the appropriate + * quantization table for each component at the start of the first scan + * involving that component. (This is necessary in order to correctly + * decode files that reuse Q-table slots.) + * When we are ready to make an output pass, the saved Q-table is converted + * to a multiplier table that will actually be used by the IDCT routine. + * The multiplier table contents are IDCT-method-dependent. To support + * application changes in IDCT method between scans, we can remake the + * multiplier tables if necessary. + * In buffered-image mode, the first output pass may occur before any data + * has been seen for some components, and thus before their Q-tables have + * been saved away. To handle this case, multiplier tables are preset + * to zeroes; the result of the IDCT will be a neutral gray level. + */ + + +/* Private subobject for this module */ + +typedef struct { + struct jpeg_inverse_dct pub; /* public fields */ + + /* This array contains the IDCT method code that each multiplier table + * is currently set up for, or -1 if it's not yet set up. + * The actual multiplier tables are pointed to by dct_table in the + * per-component comp_info structures. + */ + int cur_method[MAX_COMPONENTS]; +} my_idct_controller; + +typedef my_idct_controller * my_idct_ptr; + + +/* Allocated multiplier tables: big enough for any supported variant */ + +typedef union { + ISLOW_MULT_TYPE islow_array[DCTSIZE2]; +#ifdef DCT_IFAST_SUPPORTED + IFAST_MULT_TYPE ifast_array[DCTSIZE2]; +#endif +#ifdef DCT_FLOAT_SUPPORTED + FLOAT_MULT_TYPE float_array[DCTSIZE2]; +#endif +} multiplier_table; + + +/* The current scaled-IDCT routines require ISLOW-style multiplier tables, + * so be sure to compile that code if either ISLOW or SCALING is requested. + */ +#ifdef DCT_ISLOW_SUPPORTED +#define PROVIDE_ISLOW_TABLES +#else +#ifdef IDCT_SCALING_SUPPORTED +#define PROVIDE_ISLOW_TABLES +#endif +#endif + + +/* + * Prepare for an output pass. + * Here we select the proper IDCT routine for each component and build + * a matching multiplier table. + */ + +METHODDEF(void) +start_pass (j_decompress_ptr cinfo) +{ + my_idct_ptr idct = (my_idct_ptr) cinfo->idct; + int ci, i; + jpeg_component_info *compptr; + int method = 0; + inverse_DCT_method_ptr method_ptr = NULL; + JQUANT_TBL * qtbl; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Select the proper IDCT routine for this component's scaling */ + switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) { +#ifdef IDCT_SCALING_SUPPORTED + case ((1 << 8) + 1): + method_ptr = jpeg_idct_1x1; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((2 << 8) + 2): + method_ptr = jpeg_idct_2x2; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((3 << 8) + 3): + method_ptr = jpeg_idct_3x3; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((4 << 8) + 4): + method_ptr = jpeg_idct_4x4; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((5 << 8) + 5): + method_ptr = jpeg_idct_5x5; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((6 << 8) + 6): + method_ptr = jpeg_idct_6x6; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((7 << 8) + 7): + method_ptr = jpeg_idct_7x7; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((9 << 8) + 9): + method_ptr = jpeg_idct_9x9; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((10 << 8) + 10): + method_ptr = jpeg_idct_10x10; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((11 << 8) + 11): + method_ptr = jpeg_idct_11x11; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((12 << 8) + 12): + method_ptr = jpeg_idct_12x12; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((13 << 8) + 13): + method_ptr = jpeg_idct_13x13; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((14 << 8) + 14): + method_ptr = jpeg_idct_14x14; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((15 << 8) + 15): + method_ptr = jpeg_idct_15x15; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((16 << 8) + 16): + method_ptr = jpeg_idct_16x16; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((16 << 8) + 8): + method_ptr = jpeg_idct_16x8; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((14 << 8) + 7): + method_ptr = jpeg_idct_14x7; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((12 << 8) + 6): + method_ptr = jpeg_idct_12x6; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((10 << 8) + 5): + method_ptr = jpeg_idct_10x5; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((8 << 8) + 4): + method_ptr = jpeg_idct_8x4; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((6 << 8) + 3): + method_ptr = jpeg_idct_6x3; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((4 << 8) + 2): + method_ptr = jpeg_idct_4x2; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((2 << 8) + 1): + method_ptr = jpeg_idct_2x1; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((8 << 8) + 16): + method_ptr = jpeg_idct_8x16; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((7 << 8) + 14): + method_ptr = jpeg_idct_7x14; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((6 << 8) + 12): + method_ptr = jpeg_idct_6x12; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((5 << 8) + 10): + method_ptr = jpeg_idct_5x10; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((4 << 8) + 8): + method_ptr = jpeg_idct_4x8; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((3 << 8) + 6): + method_ptr = jpeg_idct_3x6; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((2 << 8) + 4): + method_ptr = jpeg_idct_2x4; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; + case ((1 << 8) + 2): + method_ptr = jpeg_idct_1x2; + method = JDCT_ISLOW; /* jidctint uses islow-style table */ + break; +#endif + case ((DCTSIZE << 8) + DCTSIZE): + switch (cinfo->dct_method) { +#ifdef DCT_ISLOW_SUPPORTED + case JDCT_ISLOW: + method_ptr = jpeg_idct_islow; + method = JDCT_ISLOW; + break; +#endif +#ifdef DCT_IFAST_SUPPORTED + case JDCT_IFAST: + method_ptr = jpeg_idct_ifast; + method = JDCT_IFAST; + break; +#endif +#ifdef DCT_FLOAT_SUPPORTED + case JDCT_FLOAT: + method_ptr = jpeg_idct_float; + method = JDCT_FLOAT; + break; +#endif + default: + ERREXIT(cinfo, JERR_NOT_COMPILED); + break; + } + break; + default: + ERREXIT2(cinfo, JERR_BAD_DCTSIZE, + compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size); + break; + } + idct->pub.inverse_DCT[ci] = method_ptr; + /* Create multiplier table from quant table. + * However, we can skip this if the component is uninteresting + * or if we already built the table. Also, if no quant table + * has yet been saved for the component, we leave the + * multiplier table all-zero; we'll be reading zeroes from the + * coefficient controller's buffer anyway. + */ + if (! compptr->component_needed || idct->cur_method[ci] == method) + continue; + qtbl = compptr->quant_table; + if (qtbl == NULL) /* happens if no data yet for component */ + continue; + idct->cur_method[ci] = method; + switch (method) { +#ifdef PROVIDE_ISLOW_TABLES + case JDCT_ISLOW: + { + /* For LL&M IDCT method, multipliers are equal to raw quantization + * coefficients, but are stored as ints to ensure access efficiency. + */ + ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table; + for (i = 0; i < DCTSIZE2; i++) { + ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i]; + } + } + break; +#endif +#ifdef DCT_IFAST_SUPPORTED + case JDCT_IFAST: + { + /* For AA&N IDCT method, multipliers are equal to quantization + * coefficients scaled by scalefactor[row]*scalefactor[col], where + * scalefactor[0] = 1 + * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 + * For integer operation, the multiplier table is to be scaled by + * IFAST_SCALE_BITS. + */ + IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table; +#define CONST_BITS 14 + static const INT16 aanscales[DCTSIZE2] = { + /* precomputed values scaled up by 14 bits */ + 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, + 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, + 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, + 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, + 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, + 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, + 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, + 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 + }; + SHIFT_TEMPS + + for (i = 0; i < DCTSIZE2; i++) { + ifmtbl[i] = (IFAST_MULT_TYPE) + DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], + (INT32) aanscales[i]), + CONST_BITS-IFAST_SCALE_BITS); + } + } + break; +#endif +#ifdef DCT_FLOAT_SUPPORTED + case JDCT_FLOAT: + { + /* For float AA&N IDCT method, multipliers are equal to quantization + * coefficients scaled by scalefactor[row]*scalefactor[col], where + * scalefactor[0] = 1 + * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 + * We apply a further scale factor of 1/8. + */ + FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table; + int row, col; + static const double aanscalefactor[DCTSIZE] = { + 1.0, 1.387039845, 1.306562965, 1.175875602, + 1.0, 0.785694958, 0.541196100, 0.275899379 + }; + + i = 0; + for (row = 0; row < DCTSIZE; row++) { + for (col = 0; col < DCTSIZE; col++) { + fmtbl[i] = (FLOAT_MULT_TYPE) + ((double) qtbl->quantval[i] * + aanscalefactor[row] * aanscalefactor[col] * 0.125); + i++; + } + } + } + break; +#endif + default: + ERREXIT(cinfo, JERR_NOT_COMPILED); + break; + } + } +} + + +/* + * Initialize IDCT manager. + */ + +GLOBAL(void) +jinit_inverse_dct (j_decompress_ptr cinfo) +{ + my_idct_ptr idct; + int ci; + jpeg_component_info *compptr; + + idct = (my_idct_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_idct_controller)); + cinfo->idct = (struct jpeg_inverse_dct *) idct; + idct->pub.start_pass = start_pass; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Allocate and pre-zero a multiplier table for each component */ + compptr->dct_table = + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(multiplier_table)); + MEMZERO(compptr->dct_table, SIZEOF(multiplier_table)); + /* Mark multiplier table not yet set up for any method */ + idct->cur_method[ci] = -1; + } +} diff --git a/crypto777/jpeg/jdhuff.c b/crypto777/jpeg/jdhuff.c new file mode 100644 index 000000000..06f92fe47 --- /dev/null +++ b/crypto777/jpeg/jdhuff.c @@ -0,0 +1,1541 @@ +/* + * jdhuff.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2006-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains Huffman entropy decoding routines. + * Both sequential and progressive modes are supported in this single module. + * + * Much of the complexity here has to do with supporting input suspension. + * If the data source module demands suspension, we want to be able to back + * up to the start of the current MCU. To do this, we copy state variables + * into local working storage, and update them back to the permanent + * storage only upon successful completion of an MCU. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Derived data constructed for each Huffman table */ + +#define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */ + +typedef struct { + /* Basic tables: (element [0] of each array is unused) */ + INT32 maxcode[18]; /* largest code of length k (-1 if none) */ + /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ + INT32 valoffset[17]; /* huffval[] offset for codes of length k */ + /* valoffset[k] = huffval[] index of 1st symbol of code length k, less + * the smallest code of length k; so given a code of length k, the + * corresponding symbol is huffval[code + valoffset[k]] + */ + + /* Link to public Huffman table (needed only in jpeg_huff_decode) */ + JHUFF_TBL *pub; + + /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of + * the input data stream. If the next Huffman code is no more + * than HUFF_LOOKAHEAD bits long, we can obtain its length and + * the corresponding symbol directly from these tables. + */ + int look_nbits[1< 32 bits on your machine, and shifting/masking longs is + * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE + * appropriately should be a win. Unfortunately we can't define the size + * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) + * because not all machines measure sizeof in 8-bit bytes. + */ + +typedef struct { /* Bitreading state saved across MCUs */ + bit_buf_type get_buffer; /* current bit-extraction buffer */ + int bits_left; /* # of unused bits in it */ +} bitread_perm_state; + +typedef struct { /* Bitreading working state within an MCU */ + /* Current data source location */ + /* We need a copy, rather than munging the original, in case of suspension */ + const JOCTET * next_input_byte; /* => next byte to read from source */ + size_t bytes_in_buffer; /* # of bytes remaining in source buffer */ + /* Bit input buffer --- note these values are kept in register variables, + * not in this struct, inside the inner loops. + */ + bit_buf_type get_buffer; /* current bit-extraction buffer */ + int bits_left; /* # of unused bits in it */ + /* Pointer needed by jpeg_fill_bit_buffer. */ + j_decompress_ptr cinfo; /* back link to decompress master record */ +} bitread_working_state; + +/* Macros to declare and load/save bitread local variables. */ +#define BITREAD_STATE_VARS \ + register bit_buf_type get_buffer; \ + register int bits_left; \ + bitread_working_state br_state + +#define BITREAD_LOAD_STATE(cinfop,permstate) \ + br_state.cinfo = cinfop; \ + br_state.next_input_byte = cinfop->src->next_input_byte; \ + br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \ + get_buffer = permstate.get_buffer; \ + bits_left = permstate.bits_left; + +#define BITREAD_SAVE_STATE(cinfop,permstate) \ + cinfop->src->next_input_byte = br_state.next_input_byte; \ + cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \ + permstate.get_buffer = get_buffer; \ + permstate.bits_left = bits_left + +/* + * These macros provide the in-line portion of bit fetching. + * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer + * before using GET_BITS, PEEK_BITS, or DROP_BITS. + * The variables get_buffer and bits_left are assumed to be locals, + * but the state struct might not be (jpeg_huff_decode needs this). + * CHECK_BIT_BUFFER(state,n,action); + * Ensure there are N bits in get_buffer; if suspend, take action. + * val = GET_BITS(n); + * Fetch next N bits. + * val = PEEK_BITS(n); + * Fetch next N bits without removing them from the buffer. + * DROP_BITS(n); + * Discard next N bits. + * The value N should be a simple variable, not an expression, because it + * is evaluated multiple times. + */ + +#define CHECK_BIT_BUFFER(state,nbits,action) \ + { if (bits_left < (nbits)) { \ + if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \ + { action; } \ + get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } + +#define GET_BITS(nbits) \ + (((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits)) + +#define PEEK_BITS(nbits) \ + (((int) (get_buffer >> (bits_left - (nbits)))) & BIT_MASK(nbits)) + +#define DROP_BITS(nbits) \ + (bits_left -= (nbits)) + + +/* + * Code for extracting next Huffman-coded symbol from input bit stream. + * Again, this is time-critical and we make the main paths be macros. + * + * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits + * without looping. Usually, more than 95% of the Huffman codes will be 8 + * or fewer bits long. The few overlength codes are handled with a loop, + * which need not be inline code. + * + * Notes about the HUFF_DECODE macro: + * 1. Near the end of the data segment, we may fail to get enough bits + * for a lookahead. In that case, we do it the hard way. + * 2. If the lookahead table contains no entry, the next code must be + * more than HUFF_LOOKAHEAD bits long. + * 3. jpeg_huff_decode returns -1 if forced to suspend. + */ + +#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ +{ register int nb, look; \ + if (bits_left < HUFF_LOOKAHEAD) { \ + if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ + get_buffer = state.get_buffer; bits_left = state.bits_left; \ + if (bits_left < HUFF_LOOKAHEAD) { \ + nb = 1; goto slowlabel; \ + } \ + } \ + look = PEEK_BITS(HUFF_LOOKAHEAD); \ + if ((nb = htbl->look_nbits[look]) != 0) { \ + DROP_BITS(nb); \ + result = htbl->look_sym[look]; \ + } else { \ + nb = HUFF_LOOKAHEAD+1; \ +slowlabel: \ + if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \ + { failaction; } \ + get_buffer = state.get_buffer; bits_left = state.bits_left; \ + } \ +} + + +/* + * Expanded entropy decoder object for Huffman decoding. + * + * The savable_state subrecord contains fields that change within an MCU, + * but must not be updated permanently until we complete the MCU. + */ + +typedef struct { + unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ +} savable_state; + +/* This macro is to work around compilers with missing or broken + * structure assignment. You'll need to fix this code if you have + * such a compiler and you change MAX_COMPS_IN_SCAN. + */ + +#ifndef NO_STRUCT_ASSIGN +#define ASSIGN_STATE(dest,src) ((dest) = (src)) +#else +#if MAX_COMPS_IN_SCAN == 4 +#define ASSIGN_STATE(dest,src) \ + ((dest).EOBRUN = (src).EOBRUN, \ + (dest).last_dc_val[0] = (src).last_dc_val[0], \ + (dest).last_dc_val[1] = (src).last_dc_val[1], \ + (dest).last_dc_val[2] = (src).last_dc_val[2], \ + (dest).last_dc_val[3] = (src).last_dc_val[3]) +#endif +#endif + + +typedef struct { + struct jpeg_entropy_decoder pub; /* public fields */ + + /* These fields are loaded into local variables at start of each MCU. + * In case of suspension, we exit WITHOUT updating them. + */ + bitread_perm_state bitstate; /* Bit buffer at start of MCU */ + savable_state saved; /* Other state at start of MCU */ + + /* These fields are NOT loaded into local working state. */ + boolean insufficient_data; /* set TRUE after emitting warning */ + unsigned int restarts_to_go; /* MCUs left in this restart interval */ + + /* Following two fields used only in progressive mode */ + + /* Pointers to derived tables (these workspaces have image lifespan) */ + d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; + + d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ + + /* Following fields used only in sequential mode */ + + /* Pointers to derived tables (these workspaces have image lifespan) */ + d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; + d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; + + /* Precalculated info set up by start_pass for use in decode_mcu: */ + + /* Pointers to derived tables to be used for each block within an MCU */ + d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; + d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; + /* Whether we care about the DC and AC coefficient values for each block */ + int coef_limit[D_MAX_BLOCKS_IN_MCU]; +} huff_entropy_decoder; + +typedef huff_entropy_decoder * huff_entropy_ptr; + + +static const int jpeg_zigzag_order[8][8] = { + { 0, 1, 5, 6, 14, 15, 27, 28 }, + { 2, 4, 7, 13, 16, 26, 29, 42 }, + { 3, 8, 12, 17, 25, 30, 41, 43 }, + { 9, 11, 18, 24, 31, 40, 44, 53 }, + { 10, 19, 23, 32, 39, 45, 52, 54 }, + { 20, 22, 33, 38, 46, 51, 55, 60 }, + { 21, 34, 37, 47, 50, 56, 59, 61 }, + { 35, 36, 48, 49, 57, 58, 62, 63 } +}; + +static const int jpeg_zigzag_order7[7][7] = { + { 0, 1, 5, 6, 14, 15, 27 }, + { 2, 4, 7, 13, 16, 26, 28 }, + { 3, 8, 12, 17, 25, 29, 38 }, + { 9, 11, 18, 24, 30, 37, 39 }, + { 10, 19, 23, 31, 36, 40, 45 }, + { 20, 22, 32, 35, 41, 44, 46 }, + { 21, 33, 34, 42, 43, 47, 48 } +}; + +static const int jpeg_zigzag_order6[6][6] = { + { 0, 1, 5, 6, 14, 15 }, + { 2, 4, 7, 13, 16, 25 }, + { 3, 8, 12, 17, 24, 26 }, + { 9, 11, 18, 23, 27, 32 }, + { 10, 19, 22, 28, 31, 33 }, + { 20, 21, 29, 30, 34, 35 } +}; + +static const int jpeg_zigzag_order5[5][5] = { + { 0, 1, 5, 6, 14 }, + { 2, 4, 7, 13, 15 }, + { 3, 8, 12, 16, 21 }, + { 9, 11, 17, 20, 22 }, + { 10, 18, 19, 23, 24 } +}; + +static const int jpeg_zigzag_order4[4][4] = { + { 0, 1, 5, 6 }, + { 2, 4, 7, 12 }, + { 3, 8, 11, 13 }, + { 9, 10, 14, 15 } +}; + +static const int jpeg_zigzag_order3[3][3] = { + { 0, 1, 5 }, + { 2, 4, 6 }, + { 3, 7, 8 } +}; + +static const int jpeg_zigzag_order2[2][2] = { + { 0, 1 }, + { 2, 3 } +}; + + +/* + * Compute the derived values for a Huffman table. + * This routine also performs some validation checks on the table. + */ + +LOCAL(void) +jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, + d_derived_tbl ** pdtbl) +{ + JHUFF_TBL *htbl; + d_derived_tbl *dtbl; + int p, i, l, si, numsymbols; + int lookbits, ctr; + char huffsize[257]; + unsigned int huffcode[257]; + unsigned int code; + + /* Note that huffsize[] and huffcode[] are filled in code-length order, + * paralleling the order of the symbols themselves in htbl->huffval[]. + */ + + /* Find the input Huffman table */ + if (tblno < 0 || tblno >= NUM_HUFF_TBLS) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); + htbl = + isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; + if (htbl == NULL) + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); + + /* Allocate a workspace if we haven't already done so. */ + if (*pdtbl == NULL) + *pdtbl = (d_derived_tbl *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(d_derived_tbl)); + dtbl = *pdtbl; + dtbl->pub = htbl; /* fill in back link */ + + /* Figure C.1: make table of Huffman code length for each symbol */ + + p = 0; + for (l = 1; l <= 16; l++) { + i = (int) htbl->bits[l]; + if (i < 0 || p + i > 256) /* protect against table overrun */ + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + while (i--) + huffsize[p++] = (char) l; + } + huffsize[p] = 0; + numsymbols = p; + + /* Figure C.2: generate the codes themselves */ + /* We also validate that the counts represent a legal Huffman code tree. */ + + code = 0; + si = huffsize[0]; + p = 0; + while (huffsize[p]) { + while (((int) huffsize[p]) == si) { + huffcode[p++] = code; + code++; + } + /* code is now 1 more than the last code used for codelength si; but + * it must still fit in si bits, since no code is allowed to be all ones. + */ + if (((INT32) code) >= (((INT32) 1) << si)) + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + code <<= 1; + si++; + } + + /* Figure F.15: generate decoding tables for bit-sequential decoding */ + + p = 0; + for (l = 1; l <= 16; l++) { + if (htbl->bits[l]) { + /* valoffset[l] = huffval[] index of 1st symbol of code length l, + * minus the minimum code of length l + */ + dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; + p += htbl->bits[l]; + dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ + } else { + dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ + } + } + dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ + + /* Compute lookahead tables to speed up decoding. + * First we set all the table entries to 0, indicating "too long"; + * then we iterate through the Huffman codes that are short enough and + * fill in all the entries that correspond to bit sequences starting + * with that code. + */ + + MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits)); + + p = 0; + for (l = 1; l <= HUFF_LOOKAHEAD; l++) { + for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { + /* l = current code's length, p = its index in huffcode[] & huffval[]. */ + /* Generate left-justified code followed by all possible bit sequences */ + lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); + for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { + dtbl->look_nbits[lookbits] = l; + dtbl->look_sym[lookbits] = htbl->huffval[p]; + lookbits++; + } + } + } + + /* Validate symbols as being reasonable. + * For AC tables, we make no check, but accept all byte values 0..255. + * For DC tables, we require the symbols to be in range 0..15. + * (Tighter bounds could be applied depending on the data depth and mode, + * but this is sufficient to ensure safe decoding.) + */ + if (isDC) { + for (i = 0; i < numsymbols; i++) { + int sym = htbl->huffval[i]; + if (sym < 0 || sym > 15) + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + } + } +} + + +/* + * Out-of-line code for bit fetching. + * Note: current values of get_buffer and bits_left are passed as parameters, + * but are returned in the corresponding fields of the state struct. + * + * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width + * of get_buffer to be used. (On machines with wider words, an even larger + * buffer could be used.) However, on some machines 32-bit shifts are + * quite slow and take time proportional to the number of places shifted. + * (This is true with most PC compilers, for instance.) In this case it may + * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the + * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. + */ + +#ifdef SLOW_SHIFT_32 +#define MIN_GET_BITS 15 /* minimum allowable value */ +#else +#define MIN_GET_BITS (BIT_BUF_SIZE-7) +#endif + + +LOCAL(boolean) +jpeg_fill_bit_buffer (bitread_working_state * state, + register bit_buf_type get_buffer, register int bits_left, + int nbits) +/* Load up the bit buffer to a depth of at least nbits */ +{ + /* Copy heavily used state fields into locals (hopefully registers) */ + register const JOCTET * next_input_byte = state->next_input_byte; + register size_t bytes_in_buffer = state->bytes_in_buffer; + j_decompress_ptr cinfo = state->cinfo; + + /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ + /* (It is assumed that no request will be for more than that many bits.) */ + /* We fail to do so only if we hit a marker or are forced to suspend. */ + + if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ + while (bits_left < MIN_GET_BITS) { + register int c; + + /* Attempt to read a byte */ + if (bytes_in_buffer == 0) { + if (! (*cinfo->src->fill_input_buffer) (cinfo)) + return FALSE; + next_input_byte = cinfo->src->next_input_byte; + bytes_in_buffer = cinfo->src->bytes_in_buffer; + } + bytes_in_buffer--; + c = GETJOCTET(*next_input_byte++); + + /* If it's 0xFF, check and discard stuffed zero byte */ + if (c == 0xFF) { + /* Loop here to discard any padding FF's on terminating marker, + * so that we can save a valid unread_marker value. NOTE: we will + * accept multiple FF's followed by a 0 as meaning a single FF data + * byte. This data pattern is not valid according to the standard. + */ + do { + if (bytes_in_buffer == 0) { + if (! (*cinfo->src->fill_input_buffer) (cinfo)) + return FALSE; + next_input_byte = cinfo->src->next_input_byte; + bytes_in_buffer = cinfo->src->bytes_in_buffer; + } + bytes_in_buffer--; + c = GETJOCTET(*next_input_byte++); + } while (c == 0xFF); + + if (c == 0) { + /* Found FF/00, which represents an FF data byte */ + c = 0xFF; + } else { + /* Oops, it's actually a marker indicating end of compressed data. + * Save the marker code for later use. + * Fine point: it might appear that we should save the marker into + * bitread working state, not straight into permanent state. But + * once we have hit a marker, we cannot need to suspend within the + * current MCU, because we will read no more bytes from the data + * source. So it is OK to update permanent state right away. + */ + cinfo->unread_marker = c; + /* See if we need to insert some fake zero bits. */ + goto no_more_bytes; + } + } + + /* OK, load c into get_buffer */ + get_buffer = (get_buffer << 8) | c; + bits_left += 8; + } /* end while */ + } else { + no_more_bytes: + /* We get here if we've read the marker that terminates the compressed + * data segment. There should be enough bits in the buffer register + * to satisfy the request; if so, no problem. + */ + if (nbits > bits_left) { + /* Uh-oh. Report corrupted data to user and stuff zeroes into + * the data stream, so that we can produce some kind of image. + * We use a nonvolatile flag to ensure that only one warning message + * appears per data segment. + */ + if (! ((huff_entropy_ptr) cinfo->entropy)->insufficient_data) { + WARNMS(cinfo, JWRN_HIT_MARKER); + ((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE; + } + /* Fill the buffer with zero bits */ + get_buffer <<= MIN_GET_BITS - bits_left; + bits_left = MIN_GET_BITS; + } + } + + /* Unload the local registers */ + state->next_input_byte = next_input_byte; + state->bytes_in_buffer = bytes_in_buffer; + state->get_buffer = get_buffer; + state->bits_left = bits_left; + + return TRUE; +} + + +/* + * Figure F.12: extend sign bit. + * On some machines, a shift and sub will be faster than a table lookup. + */ + +#ifdef AVOID_TABLES + +#define BIT_MASK(nbits) ((1<<(nbits))-1) +#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) - ((1<<(s))-1) : (x)) + +#else + +#define BIT_MASK(nbits) bmask[nbits] +#define HUFF_EXTEND(x,s) ((x) <= bmask[(s) - 1] ? (x) - bmask[s] : (x)) + +static const int bmask[16] = /* bmask[n] is mask for n rightmost bits */ + { 0, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, + 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF }; + +#endif /* AVOID_TABLES */ + + +/* + * Out-of-line code for Huffman code decoding. + */ + +LOCAL(int) +jpeg_huff_decode (bitread_working_state * state, + register bit_buf_type get_buffer, register int bits_left, + d_derived_tbl * htbl, int min_bits) +{ + register int l = min_bits; + register INT32 code; + + /* HUFF_DECODE has determined that the code is at least min_bits */ + /* bits long, so fetch that many bits in one swoop. */ + + CHECK_BIT_BUFFER(*state, l, return -1); + code = GET_BITS(l); + + /* Collect the rest of the Huffman code one bit at a time. */ + /* This is per Figure F.16 in the JPEG spec. */ + + while (code > htbl->maxcode[l]) { + code <<= 1; + CHECK_BIT_BUFFER(*state, 1, return -1); + code |= GET_BITS(1); + l++; + } + + /* Unload the local registers */ + state->get_buffer = get_buffer; + state->bits_left = bits_left; + + /* With garbage input we may reach the sentinel value l = 17. */ + + if (l > 16) { + WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); + return 0; /* fake a zero as the safest result */ + } + + return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; +} + + +/* + * Check for a restart marker & resynchronize decoder. + * Returns FALSE if must suspend. + */ + +LOCAL(boolean) +process_restart (j_decompress_ptr cinfo) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int ci; + + /* Throw away any unused bits remaining in bit buffer; */ + /* include any full bytes in next_marker's count of discarded bytes */ + cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; + entropy->bitstate.bits_left = 0; + + /* Advance past the RSTn marker */ + if (! (*cinfo->marker->read_restart_marker) (cinfo)) + return FALSE; + + /* Re-initialize DC predictions to 0 */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) + entropy->saved.last_dc_val[ci] = 0; + /* Re-init EOB run count, too */ + entropy->saved.EOBRUN = 0; + + /* Reset restart counter */ + entropy->restarts_to_go = cinfo->restart_interval; + + /* Reset out-of-data flag, unless read_restart_marker left us smack up + * against a marker. In that case we will end up treating the next data + * segment as empty, and we can avoid producing bogus output pixels by + * leaving the flag set. + */ + if (cinfo->unread_marker == 0) + entropy->insufficient_data = FALSE; + + return TRUE; +} + + +/* + * Huffman MCU decoding. + * Each of these routines decodes and returns one MCU's worth of + * Huffman-compressed coefficients. + * The coefficients are reordered from zigzag order into natural array order, + * but are not dequantized. + * + * The i'th block of the MCU is stored into the block pointed to by + * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. + * (Wholesale zeroing is usually a little faster than retail...) + * + * We return FALSE if data source requested suspension. In that case no + * changes have been made to permanent state. (Exception: some output + * coefficients may already have been assigned. This is harmless for + * spectral selection, since we'll just re-assign them on the next call. + * Successive approximation AC refinement has to be more careful, however.) + */ + +/* + * MCU decoding for DC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int Al = cinfo->Al; + register int s, r; + int blkn, ci; + JBLOCKROW block; + BITREAD_STATE_VARS; + savable_state state; + d_derived_tbl * tbl; + jpeg_component_info * compptr; + + /* Process restart marker if needed; may have to suspend */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! process_restart(cinfo)) + return FALSE; + } + + /* If we've run out of data, just leave the MCU set to zeroes. + * This way, we return uniform gray for the remainder of the segment. + */ + if (! entropy->insufficient_data) { + + /* Load up working state */ + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + ASSIGN_STATE(state, entropy->saved); + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + tbl = entropy->derived_tbls[compptr->dc_tbl_no]; + + /* Decode a single block's worth of coefficients */ + + /* Section F.2.2.1: decode the DC coefficient difference */ + HUFF_DECODE(s, br_state, tbl, return FALSE, label1); + if (s) { + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + } + + /* Convert DC difference to actual value, update last_dc_val */ + s += state.last_dc_val[ci]; + state.last_dc_val[ci] = s; + /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ + (*block)[0] = (JCOEF) (s << Al); + } + + /* Completed MCU, so update state */ + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + ASSIGN_STATE(entropy->saved, state); + } + + /* Account for restart interval (no-op if not using restarts) */ + entropy->restarts_to_go--; + + return TRUE; +} + + +/* + * MCU decoding for AC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + register int s, k, r; + unsigned int EOBRUN; + int Se, Al; + const int * natural_order; + JBLOCKROW block; + BITREAD_STATE_VARS; + d_derived_tbl * tbl; + + /* Process restart marker if needed; may have to suspend */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! process_restart(cinfo)) + return FALSE; + } + + /* If we've run out of data, just leave the MCU set to zeroes. + * This way, we return uniform gray for the remainder of the segment. + */ + if (! entropy->insufficient_data) { + + Se = cinfo->Se; + Al = cinfo->Al; + natural_order = cinfo->natural_order; + + /* Load up working state. + * We can avoid loading/saving bitread state if in an EOB run. + */ + EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ + + /* There is always only one block per MCU */ + + if (EOBRUN > 0) /* if it's a band of zeroes... */ + EOBRUN--; /* ...process it now (we do nothing) */ + else { + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + block = MCU_data[0]; + tbl = entropy->ac_derived_tbl; + + for (k = cinfo->Ss; k <= Se; k++) { + HUFF_DECODE(s, br_state, tbl, return FALSE, label2); + r = s >> 4; + s &= 15; + if (s) { + k += r; + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + /* Scale and output coefficient in natural (dezigzagged) order */ + (*block)[natural_order[k]] = (JCOEF) (s << Al); + } else { + if (r == 15) { /* ZRL */ + k += 15; /* skip 15 zeroes in band */ + } else { /* EOBr, run length is 2^r + appended bits */ + EOBRUN = 1 << r; + if (r) { /* EOBr, r > 0 */ + CHECK_BIT_BUFFER(br_state, r, return FALSE); + r = GET_BITS(r); + EOBRUN += r; + } + EOBRUN--; /* this band is processed at this moment */ + break; /* force end-of-band */ + } + } + } + + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + } + + /* Completed MCU, so update state */ + entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ + } + + /* Account for restart interval (no-op if not using restarts) */ + entropy->restarts_to_go--; + + return TRUE; +} + + +/* + * MCU decoding for DC successive approximation refinement scan. + * Note: we assume such scans can be multi-component, although the spec + * is not very clear on the point. + */ + +METHODDEF(boolean) +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ + int blkn; + JBLOCKROW block; + BITREAD_STATE_VARS; + + /* Process restart marker if needed; may have to suspend */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! process_restart(cinfo)) + return FALSE; + } + + /* Not worth the cycles to check insufficient_data here, + * since we will not change the data anyway if we read zeroes. + */ + + /* Load up working state */ + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + block = MCU_data[blkn]; + + /* Encoded data is simply the next bit of the two's-complement DC value */ + CHECK_BIT_BUFFER(br_state, 1, return FALSE); + if (GET_BITS(1)) + (*block)[0] |= p1; + /* Note: since we use |=, repeating the assignment later is safe */ + } + + /* Completed MCU, so update state */ + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + + /* Account for restart interval (no-op if not using restarts) */ + entropy->restarts_to_go--; + + return TRUE; +} + + +/* + * MCU decoding for AC successive approximation refinement scan. + */ + +METHODDEF(boolean) +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + register int s, k, r; + unsigned int EOBRUN; + int Se, p1, m1; + const int * natural_order; + JBLOCKROW block; + JCOEFPTR thiscoef; + BITREAD_STATE_VARS; + d_derived_tbl * tbl; + int num_newnz; + int newnz_pos[DCTSIZE2]; + + /* Process restart marker if needed; may have to suspend */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! process_restart(cinfo)) + return FALSE; + } + + /* If we've run out of data, don't modify the MCU. + */ + if (! entropy->insufficient_data) { + + Se = cinfo->Se; + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ + m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ + natural_order = cinfo->natural_order; + + /* Load up working state */ + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ + + /* There is always only one block per MCU */ + block = MCU_data[0]; + tbl = entropy->ac_derived_tbl; + + /* If we are forced to suspend, we must undo the assignments to any newly + * nonzero coefficients in the block, because otherwise we'd get confused + * next time about which coefficients were already nonzero. + * But we need not undo addition of bits to already-nonzero coefficients; + * instead, we can test the current bit to see if we already did it. + */ + num_newnz = 0; + + /* initialize coefficient loop counter to start of band */ + k = cinfo->Ss; + + if (EOBRUN == 0) { + for (; k <= Se; k++) { + HUFF_DECODE(s, br_state, tbl, goto undoit, label3); + r = s >> 4; + s &= 15; + if (s) { + if (s != 1) /* size of new coef should always be 1 */ + WARNMS(cinfo, JWRN_HUFF_BAD_CODE); + CHECK_BIT_BUFFER(br_state, 1, goto undoit); + if (GET_BITS(1)) + s = p1; /* newly nonzero coef is positive */ + else + s = m1; /* newly nonzero coef is negative */ + } else { + if (r != 15) { + EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ + if (r) { + CHECK_BIT_BUFFER(br_state, r, goto undoit); + r = GET_BITS(r); + EOBRUN += r; + } + break; /* rest of block is handled by EOB logic */ + } + /* note s = 0 for processing ZRL */ + } + /* Advance over already-nonzero coefs and r still-zero coefs, + * appending correction bits to the nonzeroes. A correction bit is 1 + * if the absolute value of the coefficient must be increased. + */ + do { + thiscoef = *block + natural_order[k]; + if (*thiscoef != 0) { + CHECK_BIT_BUFFER(br_state, 1, goto undoit); + if (GET_BITS(1)) { + if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ + if (*thiscoef >= 0) + *thiscoef += p1; + else + *thiscoef += m1; + } + } + } else { + if (--r < 0) + break; /* reached target zero coefficient */ + } + k++; + } while (k <= Se); + if (s) { + int pos = natural_order[k]; + /* Output newly nonzero coefficient */ + (*block)[pos] = (JCOEF) s; + /* Remember its position in case we have to suspend */ + newnz_pos[num_newnz++] = pos; + } + } + } + + if (EOBRUN > 0) { + /* Scan any remaining coefficient positions after the end-of-band + * (the last newly nonzero coefficient, if any). Append a correction + * bit to each already-nonzero coefficient. A correction bit is 1 + * if the absolute value of the coefficient must be increased. + */ + for (; k <= Se; k++) { + thiscoef = *block + natural_order[k]; + if (*thiscoef != 0) { + CHECK_BIT_BUFFER(br_state, 1, goto undoit); + if (GET_BITS(1)) { + if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ + if (*thiscoef >= 0) + *thiscoef += p1; + else + *thiscoef += m1; + } + } + } + } + /* Count one block completed in EOB run */ + EOBRUN--; + } + + /* Completed MCU, so update state */ + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ + } + + /* Account for restart interval (no-op if not using restarts) */ + entropy->restarts_to_go--; + + return TRUE; + +undoit: + /* Re-zero any output coefficients that we made newly nonzero */ + while (num_newnz > 0) + (*block)[newnz_pos[--num_newnz]] = 0; + + return FALSE; +} + + +/* + * Decode one MCU's worth of Huffman-compressed coefficients, + * partial blocks. + */ + +METHODDEF(boolean) +decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + const int * natural_order; + int Se, blkn; + BITREAD_STATE_VARS; + savable_state state; + + /* Process restart marker if needed; may have to suspend */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! process_restart(cinfo)) + return FALSE; + } + + /* If we've run out of data, just leave the MCU set to zeroes. + * This way, we return uniform gray for the remainder of the segment. + */ + if (! entropy->insufficient_data) { + + natural_order = cinfo->natural_order; + Se = cinfo->lim_Se; + + /* Load up working state */ + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + ASSIGN_STATE(state, entropy->saved); + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + JBLOCKROW block = MCU_data[blkn]; + d_derived_tbl * htbl; + register int s, k, r; + int coef_limit, ci; + + /* Decode a single block's worth of coefficients */ + + /* Section F.2.2.1: decode the DC coefficient difference */ + htbl = entropy->dc_cur_tbls[blkn]; + HUFF_DECODE(s, br_state, htbl, return FALSE, label1); + + htbl = entropy->ac_cur_tbls[blkn]; + k = 1; + coef_limit = entropy->coef_limit[blkn]; + if (coef_limit) { + /* Convert DC difference to actual value, update last_dc_val */ + if (s) { + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + } + ci = cinfo->MCU_membership[blkn]; + s += state.last_dc_val[ci]; + state.last_dc_val[ci] = s; + /* Output the DC coefficient */ + (*block)[0] = (JCOEF) s; + + /* Section F.2.2.2: decode the AC coefficients */ + /* Since zeroes are skipped, output area must be cleared beforehand */ + for (; k < coef_limit; k++) { + HUFF_DECODE(s, br_state, htbl, return FALSE, label2); + + r = s >> 4; + s &= 15; + + if (s) { + k += r; + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + /* Output coefficient in natural (dezigzagged) order. + * Note: the extra entries in natural_order[] will save us + * if k > Se, which could happen if the data is corrupted. + */ + (*block)[natural_order[k]] = (JCOEF) s; + } else { + if (r != 15) + goto EndOfBlock; + k += 15; + } + } + } else { + if (s) { + CHECK_BIT_BUFFER(br_state, s, return FALSE); + DROP_BITS(s); + } + } + + /* Section F.2.2.2: decode the AC coefficients */ + /* In this path we just discard the values */ + for (; k <= Se; k++) { + HUFF_DECODE(s, br_state, htbl, return FALSE, label3); + + r = s >> 4; + s &= 15; + + if (s) { + k += r; + CHECK_BIT_BUFFER(br_state, s, return FALSE); + DROP_BITS(s); + } else { + if (r != 15) + break; + k += 15; + } + } + + EndOfBlock: ; + } + + /* Completed MCU, so update state */ + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + ASSIGN_STATE(entropy->saved, state); + } + + /* Account for restart interval (no-op if not using restarts) */ + entropy->restarts_to_go--; + + return TRUE; +} + + +/* + * Decode one MCU's worth of Huffman-compressed coefficients, + * full-size blocks. + */ + +METHODDEF(boolean) +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int blkn; + BITREAD_STATE_VARS; + savable_state state; + + /* Process restart marker if needed; may have to suspend */ + if (cinfo->restart_interval) { + if (entropy->restarts_to_go == 0) + if (! process_restart(cinfo)) + return FALSE; + } + + /* If we've run out of data, just leave the MCU set to zeroes. + * This way, we return uniform gray for the remainder of the segment. + */ + if (! entropy->insufficient_data) { + + /* Load up working state */ + BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + ASSIGN_STATE(state, entropy->saved); + + /* Outer loop handles each block in the MCU */ + + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + JBLOCKROW block = MCU_data[blkn]; + d_derived_tbl * htbl; + register int s, k, r; + int coef_limit, ci; + + /* Decode a single block's worth of coefficients */ + + /* Section F.2.2.1: decode the DC coefficient difference */ + htbl = entropy->dc_cur_tbls[blkn]; + HUFF_DECODE(s, br_state, htbl, return FALSE, label1); + + htbl = entropy->ac_cur_tbls[blkn]; + k = 1; + coef_limit = entropy->coef_limit[blkn]; + if (coef_limit) { + /* Convert DC difference to actual value, update last_dc_val */ + if (s) { + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + } + ci = cinfo->MCU_membership[blkn]; + s += state.last_dc_val[ci]; + state.last_dc_val[ci] = s; + /* Output the DC coefficient */ + (*block)[0] = (JCOEF) s; + + /* Section F.2.2.2: decode the AC coefficients */ + /* Since zeroes are skipped, output area must be cleared beforehand */ + for (; k < coef_limit; k++) { + HUFF_DECODE(s, br_state, htbl, return FALSE, label2); + + r = s >> 4; + s &= 15; + + if (s) { + k += r; + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + /* Output coefficient in natural (dezigzagged) order. + * Note: the extra entries in jpeg_natural_order[] will save us + * if k >= DCTSIZE2, which could happen if the data is corrupted. + */ + (*block)[jpeg_natural_order[k]] = (JCOEF) s; + } else { + if (r != 15) + goto EndOfBlock; + k += 15; + } + } + } else { + if (s) { + CHECK_BIT_BUFFER(br_state, s, return FALSE); + DROP_BITS(s); + } + } + + /* Section F.2.2.2: decode the AC coefficients */ + /* In this path we just discard the values */ + for (; k < DCTSIZE2; k++) { + HUFF_DECODE(s, br_state, htbl, return FALSE, label3); + + r = s >> 4; + s &= 15; + + if (s) { + k += r; + CHECK_BIT_BUFFER(br_state, s, return FALSE); + DROP_BITS(s); + } else { + if (r != 15) + break; + k += 15; + } + } + + EndOfBlock: ; + } + + /* Completed MCU, so update state */ + BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + ASSIGN_STATE(entropy->saved, state); + } + + /* Account for restart interval (no-op if not using restarts) */ + entropy->restarts_to_go--; + + return TRUE; +} + + +/* + * Initialize for a Huffman-compressed scan. + */ + +METHODDEF(void) +start_pass_huff_decoder (j_decompress_ptr cinfo) +{ + huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; + int ci, blkn, tbl, i; + jpeg_component_info * compptr; + + if (cinfo->progressive_mode) { + /* Validate progressive scan parameters */ + if (cinfo->Ss == 0) { + if (cinfo->Se != 0) + goto bad; + } else { + /* need not check Ss/Se < 0 since they came from unsigned bytes */ + if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se) + goto bad; + /* AC scans may have only one component */ + if (cinfo->comps_in_scan != 1) + goto bad; + } + if (cinfo->Ah != 0) { + /* Successive approximation refinement scan: must have Al = Ah-1. */ + if (cinfo->Ah-1 != cinfo->Al) + goto bad; + } + if (cinfo->Al > 13) { /* need not check for < 0 */ + /* Arguably the maximum Al value should be less than 13 for 8-bit precision, + * but the spec doesn't say so, and we try to be liberal about what we + * accept. Note: large Al values could result in out-of-range DC + * coefficients during early scans, leading to bizarre displays due to + * overflows in the IDCT math. But we won't crash. + */ + bad: + ERREXIT4(cinfo, JERR_BAD_PROGRESSION, + cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); + } + /* Update progression status, and verify that scan order is legal. + * Note that inter-scan inconsistencies are treated as warnings + * not fatal errors ... not clear if this is right way to behave. + */ + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; + int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; + if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); + for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { + int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; + if (cinfo->Ah != expected) + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); + coef_bit_ptr[coefi] = cinfo->Al; + } + } + + /* Select MCU decoding routine */ + if (cinfo->Ah == 0) { + if (cinfo->Ss == 0) + entropy->pub.decode_mcu = decode_mcu_DC_first; + else + entropy->pub.decode_mcu = decode_mcu_AC_first; + } else { + if (cinfo->Ss == 0) + entropy->pub.decode_mcu = decode_mcu_DC_refine; + else + entropy->pub.decode_mcu = decode_mcu_AC_refine; + } + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* Make sure requested tables are present, and compute derived tables. + * We may build same derived table more than once, but it's not expensive. + */ + if (cinfo->Ss == 0) { + if (cinfo->Ah == 0) { /* DC refinement needs no table */ + tbl = compptr->dc_tbl_no; + jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, + & entropy->derived_tbls[tbl]); + } + } else { + tbl = compptr->ac_tbl_no; + jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, + & entropy->derived_tbls[tbl]); + /* remember the single active table */ + entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; + } + /* Initialize DC predictions to 0 */ + entropy->saved.last_dc_val[ci] = 0; + } + + /* Initialize private state variables */ + entropy->saved.EOBRUN = 0; + } else { + /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. + * This ought to be an error condition, but we make it a warning because + * there are some baseline files out there with all zeroes in these bytes. + */ + if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || + ((cinfo->is_baseline || cinfo->Se < DCTSIZE2) && + cinfo->Se != cinfo->lim_Se)) + WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); + + /* Select MCU decoding routine */ + /* We retain the hard-coded case for full-size blocks. + * This is not necessary, but it appears that this version is slightly + * more performant in the given implementation. + * With an improved implementation we would prefer a single optimized + * function. + */ + if (cinfo->lim_Se != DCTSIZE2-1) + entropy->pub.decode_mcu = decode_mcu_sub; + else + entropy->pub.decode_mcu = decode_mcu; + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* Compute derived values for Huffman tables */ + /* We may do this more than once for a table, but it's not expensive */ + tbl = compptr->dc_tbl_no; + jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, + & entropy->dc_derived_tbls[tbl]); + if (cinfo->lim_Se) { /* AC needs no table when not present */ + tbl = compptr->ac_tbl_no; + jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, + & entropy->ac_derived_tbls[tbl]); + } + /* Initialize DC predictions to 0 */ + entropy->saved.last_dc_val[ci] = 0; + } + + /* Precalculate decoding info for each block in an MCU of this scan */ + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + ci = cinfo->MCU_membership[blkn]; + compptr = cinfo->cur_comp_info[ci]; + /* Precalculate which table to use for each block */ + entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; + entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; + /* Decide whether we really care about the coefficient values */ + if (compptr->component_needed) { + ci = compptr->DCT_v_scaled_size; + i = compptr->DCT_h_scaled_size; + switch (cinfo->lim_Se) { + case (1*1-1): + entropy->coef_limit[blkn] = 1; + break; + case (2*2-1): + if (ci <= 0 || ci > 2) ci = 2; + if (i <= 0 || i > 2) i = 2; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1]; + break; + case (3*3-1): + if (ci <= 0 || ci > 3) ci = 3; + if (i <= 0 || i > 3) i = 3; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1]; + break; + case (4*4-1): + if (ci <= 0 || ci > 4) ci = 4; + if (i <= 0 || i > 4) i = 4; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1]; + break; + case (5*5-1): + if (ci <= 0 || ci > 5) ci = 5; + if (i <= 0 || i > 5) i = 5; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1]; + break; + case (6*6-1): + if (ci <= 0 || ci > 6) ci = 6; + if (i <= 0 || i > 6) i = 6; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1]; + break; + case (7*7-1): + if (ci <= 0 || ci > 7) ci = 7; + if (i <= 0 || i > 7) i = 7; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1]; + break; + default: + if (ci <= 0 || ci > 8) ci = 8; + if (i <= 0 || i > 8) i = 8; + entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1]; + break; + } + } else { + entropy->coef_limit[blkn] = 0; + } + } + } + + /* Initialize bitread state variables */ + entropy->bitstate.bits_left = 0; + entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ + entropy->insufficient_data = FALSE; + + /* Initialize restart counter */ + entropy->restarts_to_go = cinfo->restart_interval; +} + + +/* + * Module initialization routine for Huffman entropy decoding. + */ + +GLOBAL(void) +jinit_huff_decoder (j_decompress_ptr cinfo) +{ + huff_entropy_ptr entropy; + int i; + + entropy = (huff_entropy_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(huff_entropy_decoder)); + cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; + entropy->pub.start_pass = start_pass_huff_decoder; + + if (cinfo->progressive_mode) { + /* Create progression status table */ + int *coef_bit_ptr, ci; + cinfo->coef_bits = (int (*)[DCTSIZE2]) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->num_components*DCTSIZE2*SIZEOF(int)); + coef_bit_ptr = & cinfo->coef_bits[0][0]; + for (ci = 0; ci < cinfo->num_components; ci++) + for (i = 0; i < DCTSIZE2; i++) + *coef_bit_ptr++ = -1; + + /* Mark derived tables unallocated */ + for (i = 0; i < NUM_HUFF_TBLS; i++) { + entropy->derived_tbls[i] = NULL; + } + } else { + /* Mark tables unallocated */ + for (i = 0; i < NUM_HUFF_TBLS; i++) { + entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; + } + } +} diff --git a/crypto777/jpeg/jdinput.c b/crypto777/jpeg/jdinput.c new file mode 100644 index 000000000..2c5c717b9 --- /dev/null +++ b/crypto777/jpeg/jdinput.c @@ -0,0 +1,661 @@ +/* + * jdinput.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2002-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains input control logic for the JPEG decompressor. + * These routines are concerned with controlling the decompressor's input + * processing (marker reading and coefficient decoding). The actual input + * reading is done in jdmarker.c, jdhuff.c, and jdarith.c. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Private state */ + +typedef struct { + struct jpeg_input_controller pub; /* public fields */ + + int inheaders; /* Nonzero until first SOS is reached */ +} my_input_controller; + +typedef my_input_controller * my_inputctl_ptr; + + +/* Forward declarations */ +METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo)); + + +/* + * Routines to calculate various quantities related to the size of the image. + */ + + +/* + * Compute output image dimensions and related values. + * NOTE: this is exported for possible use by application. + * Hence it mustn't do anything that can't be done twice. + */ + +GLOBAL(void) +jpeg_core_output_dimensions (j_decompress_ptr cinfo) +/* Do computations that are needed before master selection phase. + * This function is used for transcoding and full decompression. + */ +{ +#ifdef IDCT_SCALING_SUPPORTED + int ci; + jpeg_component_info *compptr; + + /* Compute actual output image dimensions and DCT scaling choices. */ + if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) { + /* Provide 1/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 1; + cinfo->min_DCT_v_scaled_size = 1; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) { + /* Provide 2/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 2; + cinfo->min_DCT_v_scaled_size = 2; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) { + /* Provide 3/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 3; + cinfo->min_DCT_v_scaled_size = 3; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) { + /* Provide 4/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 4; + cinfo->min_DCT_v_scaled_size = 4; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) { + /* Provide 5/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 5; + cinfo->min_DCT_v_scaled_size = 5; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) { + /* Provide 6/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 6; + cinfo->min_DCT_v_scaled_size = 6; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) { + /* Provide 7/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 7; + cinfo->min_DCT_v_scaled_size = 7; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) { + /* Provide 8/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 8; + cinfo->min_DCT_v_scaled_size = 8; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) { + /* Provide 9/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 9; + cinfo->min_DCT_v_scaled_size = 9; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) { + /* Provide 10/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 10; + cinfo->min_DCT_v_scaled_size = 10; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) { + /* Provide 11/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 11; + cinfo->min_DCT_v_scaled_size = 11; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) { + /* Provide 12/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 12; + cinfo->min_DCT_v_scaled_size = 12; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) { + /* Provide 13/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 13; + cinfo->min_DCT_v_scaled_size = 13; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) { + /* Provide 14/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 14; + cinfo->min_DCT_v_scaled_size = 14; + } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) { + /* Provide 15/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 15; + cinfo->min_DCT_v_scaled_size = 15; + } else { + /* Provide 16/block_size scaling */ + cinfo->output_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size); + cinfo->output_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size); + cinfo->min_DCT_h_scaled_size = 16; + cinfo->min_DCT_v_scaled_size = 16; + } + + /* Recompute dimensions of components */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size; + compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size; + } + +#else /* !IDCT_SCALING_SUPPORTED */ + + /* Hardwire it to "no scaling" */ + cinfo->output_width = cinfo->image_width; + cinfo->output_height = cinfo->image_height; + /* jdinput.c has already initialized DCT_scaled_size, + * and has computed unscaled downsampled_width and downsampled_height. + */ + +#endif /* IDCT_SCALING_SUPPORTED */ +} + + +LOCAL(void) +initial_setup (j_decompress_ptr cinfo) +/* Called once, when first SOS marker is reached */ +{ + int ci; + jpeg_component_info *compptr; + + /* Make sure image isn't bigger than I can handle */ + if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || + (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); + + /* For now, precision must match compiled-in value... */ + if (cinfo->data_precision != BITS_IN_JSAMPLE) + ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); + + /* Check that number of components won't exceed internal array sizes */ + if (cinfo->num_components > MAX_COMPONENTS) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, + MAX_COMPONENTS); + + /* Compute maximum sampling factors; check factor validity */ + cinfo->max_h_samp_factor = 1; + cinfo->max_v_samp_factor = 1; + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || + compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) + ERREXIT(cinfo, JERR_BAD_SAMPLING); + cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, + compptr->h_samp_factor); + cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, + compptr->v_samp_factor); + } + + /* Derive block_size, natural_order, and lim_Se */ + if (cinfo->is_baseline || (cinfo->progressive_mode && + cinfo->comps_in_scan)) { /* no pseudo SOS marker */ + cinfo->block_size = DCTSIZE; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + } else + switch (cinfo->Se) { + case (1*1-1): + cinfo->block_size = 1; + cinfo->natural_order = jpeg_natural_order; /* not needed */ + cinfo->lim_Se = cinfo->Se; + break; + case (2*2-1): + cinfo->block_size = 2; + cinfo->natural_order = jpeg_natural_order2; + cinfo->lim_Se = cinfo->Se; + break; + case (3*3-1): + cinfo->block_size = 3; + cinfo->natural_order = jpeg_natural_order3; + cinfo->lim_Se = cinfo->Se; + break; + case (4*4-1): + cinfo->block_size = 4; + cinfo->natural_order = jpeg_natural_order4; + cinfo->lim_Se = cinfo->Se; + break; + case (5*5-1): + cinfo->block_size = 5; + cinfo->natural_order = jpeg_natural_order5; + cinfo->lim_Se = cinfo->Se; + break; + case (6*6-1): + cinfo->block_size = 6; + cinfo->natural_order = jpeg_natural_order6; + cinfo->lim_Se = cinfo->Se; + break; + case (7*7-1): + cinfo->block_size = 7; + cinfo->natural_order = jpeg_natural_order7; + cinfo->lim_Se = cinfo->Se; + break; + case (8*8-1): + cinfo->block_size = 8; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (9*9-1): + cinfo->block_size = 9; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (10*10-1): + cinfo->block_size = 10; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (11*11-1): + cinfo->block_size = 11; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (12*12-1): + cinfo->block_size = 12; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (13*13-1): + cinfo->block_size = 13; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (14*14-1): + cinfo->block_size = 14; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (15*15-1): + cinfo->block_size = 15; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + case (16*16-1): + cinfo->block_size = 16; + cinfo->natural_order = jpeg_natural_order; + cinfo->lim_Se = DCTSIZE2-1; + break; + default: + ERREXIT4(cinfo, JERR_BAD_PROGRESSION, + cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); + break; + } + + /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size. + * In the full decompressor, + * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c; + * but in the transcoder, + * jpeg_calc_output_dimensions is not used, so we must do it here. + */ + cinfo->min_DCT_h_scaled_size = cinfo->block_size; + cinfo->min_DCT_v_scaled_size = cinfo->block_size; + + /* Compute dimensions of components */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + compptr->DCT_h_scaled_size = cinfo->block_size; + compptr->DCT_v_scaled_size = cinfo->block_size; + /* Size in DCT blocks */ + compptr->width_in_blocks = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, + (long) (cinfo->max_h_samp_factor * cinfo->block_size)); + compptr->height_in_blocks = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + /* downsampled_width and downsampled_height will also be overridden by + * jdmaster.c if we are doing full decompression. The transcoder library + * doesn't use these values, but the calling application might. + */ + /* Size in samples */ + compptr->downsampled_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, + (long) cinfo->max_h_samp_factor); + compptr->downsampled_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, + (long) cinfo->max_v_samp_factor); + /* Mark component needed, until color conversion says otherwise */ + compptr->component_needed = TRUE; + /* Mark no quantization table yet saved for component */ + compptr->quant_table = NULL; + } + + /* Compute number of fully interleaved MCU rows. */ + cinfo->total_iMCU_rows = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height, + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + + /* Decide whether file contains multiple scans */ + if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) + cinfo->inputctl->has_multiple_scans = TRUE; + else + cinfo->inputctl->has_multiple_scans = FALSE; +} + + +LOCAL(void) +per_scan_setup (j_decompress_ptr cinfo) +/* Do computations that are needed before processing a JPEG scan */ +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ +{ + int ci, mcublks, tmp; + jpeg_component_info *compptr; + + if (cinfo->comps_in_scan == 1) { + + /* Noninterleaved (single-component) scan */ + compptr = cinfo->cur_comp_info[0]; + + /* Overall image size in MCUs */ + cinfo->MCUs_per_row = compptr->width_in_blocks; + cinfo->MCU_rows_in_scan = compptr->height_in_blocks; + + /* For noninterleaved scan, always one block per MCU */ + compptr->MCU_width = 1; + compptr->MCU_height = 1; + compptr->MCU_blocks = 1; + compptr->MCU_sample_width = compptr->DCT_h_scaled_size; + compptr->last_col_width = 1; + /* For noninterleaved scans, it is convenient to define last_row_height + * as the number of block rows present in the last iMCU row. + */ + tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); + if (tmp == 0) tmp = compptr->v_samp_factor; + compptr->last_row_height = tmp; + + /* Prepare array describing MCU composition */ + cinfo->blocks_in_MCU = 1; + cinfo->MCU_membership[0] = 0; + + } else { + + /* Interleaved (multi-component) scan */ + if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, + MAX_COMPS_IN_SCAN); + + /* Overall image size in MCUs */ + cinfo->MCUs_per_row = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width, + (long) (cinfo->max_h_samp_factor * cinfo->block_size)); + cinfo->MCU_rows_in_scan = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height, + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + + cinfo->blocks_in_MCU = 0; + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* Sampling factors give # of blocks of component in each MCU */ + compptr->MCU_width = compptr->h_samp_factor; + compptr->MCU_height = compptr->v_samp_factor; + compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; + compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; + /* Figure number of non-dummy blocks in last MCU column & row */ + tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); + if (tmp == 0) tmp = compptr->MCU_width; + compptr->last_col_width = tmp; + tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); + if (tmp == 0) tmp = compptr->MCU_height; + compptr->last_row_height = tmp; + /* Prepare array describing MCU composition */ + mcublks = compptr->MCU_blocks; + if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) + ERREXIT(cinfo, JERR_BAD_MCU_SIZE); + while (mcublks-- > 0) { + cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; + } + } + + } +} + + +/* + * Save away a copy of the Q-table referenced by each component present + * in the current scan, unless already saved during a prior scan. + * + * In a multiple-scan JPEG file, the encoder could assign different components + * the same Q-table slot number, but change table definitions between scans + * so that each component uses a different Q-table. (The IJG encoder is not + * currently capable of doing this, but other encoders might.) Since we want + * to be able to dequantize all the components at the end of the file, this + * means that we have to save away the table actually used for each component. + * We do this by copying the table at the start of the first scan containing + * the component. + * The JPEG spec prohibits the encoder from changing the contents of a Q-table + * slot between scans of a component using that slot. If the encoder does so + * anyway, this decoder will simply use the Q-table values that were current + * at the start of the first scan for the component. + * + * The decompressor output side looks only at the saved quant tables, + * not at the current Q-table slots. + */ + +LOCAL(void) +latch_quant_tables (j_decompress_ptr cinfo) +{ + int ci, qtblno; + jpeg_component_info *compptr; + JQUANT_TBL * qtbl; + + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + compptr = cinfo->cur_comp_info[ci]; + /* No work if we already saved Q-table for this component */ + if (compptr->quant_table != NULL) + continue; + /* Make sure specified quantization table is present */ + qtblno = compptr->quant_tbl_no; + if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || + cinfo->quant_tbl_ptrs[qtblno] == NULL) + ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); + /* OK, save away the quantization table */ + qtbl = (JQUANT_TBL *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(JQUANT_TBL)); + MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL)); + compptr->quant_table = qtbl; + } +} + + +/* + * Initialize the input modules to read a scan of compressed data. + * The first call to this is done by jdmaster.c after initializing + * the entire decompressor (during jpeg_start_decompress). + * Subsequent calls come from consume_markers, below. + */ + +METHODDEF(void) +start_input_pass (j_decompress_ptr cinfo) +{ + per_scan_setup(cinfo); + latch_quant_tables(cinfo); + (*cinfo->entropy->start_pass) (cinfo); + (*cinfo->coef->start_input_pass) (cinfo); + cinfo->inputctl->consume_input = cinfo->coef->consume_data; +} + + +/* + * Finish up after inputting a compressed-data scan. + * This is called by the coefficient controller after it's read all + * the expected data of the scan. + */ + +METHODDEF(void) +finish_input_pass (j_decompress_ptr cinfo) +{ + cinfo->inputctl->consume_input = consume_markers; +} + + +/* + * Read JPEG markers before, between, or after compressed-data scans. + * Change state as necessary when a new scan is reached. + * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. + * + * The consume_input method pointer points either here or to the + * coefficient controller's consume_data routine, depending on whether + * we are reading a compressed data segment or inter-segment markers. + * + * Note: This function should NOT return a pseudo SOS marker (with zero + * component number) to the caller. A pseudo marker received by + * read_markers is processed and then skipped for other markers. + */ + +METHODDEF(int) +consume_markers (j_decompress_ptr cinfo) +{ + my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; + int val; + + if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */ + return JPEG_REACHED_EOI; + + for (;;) { /* Loop to pass pseudo SOS marker */ + val = (*cinfo->marker->read_markers) (cinfo); + + switch (val) { + case JPEG_REACHED_SOS: /* Found SOS */ + if (inputctl->inheaders) { /* 1st SOS */ + if (inputctl->inheaders == 1) + initial_setup(cinfo); + if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */ + inputctl->inheaders = 2; + break; + } + inputctl->inheaders = 0; + /* Note: start_input_pass must be called by jdmaster.c + * before any more input can be consumed. jdapimin.c is + * responsible for enforcing this sequencing. + */ + } else { /* 2nd or later SOS marker */ + if (! inputctl->pub.has_multiple_scans) + ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */ + if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */ + break; + start_input_pass(cinfo); + } + return val; + case JPEG_REACHED_EOI: /* Found EOI */ + inputctl->pub.eoi_reached = TRUE; + if (inputctl->inheaders) { /* Tables-only datastream, apparently */ + if (cinfo->marker->saw_SOF) + ERREXIT(cinfo, JERR_SOF_NO_SOS); + } else { + /* Prevent infinite loop in coef ctlr's decompress_data routine + * if user set output_scan_number larger than number of scans. + */ + if (cinfo->output_scan_number > cinfo->input_scan_number) + cinfo->output_scan_number = cinfo->input_scan_number; + } + return val; + case JPEG_SUSPENDED: + return val; + default: + return val; + } + } +} + + +/* + * Reset state to begin a fresh datastream. + */ + +METHODDEF(void) +reset_input_controller (j_decompress_ptr cinfo) +{ + my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; + + inputctl->pub.consume_input = consume_markers; + inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ + inputctl->pub.eoi_reached = FALSE; + inputctl->inheaders = 1; + /* Reset other modules */ + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); + (*cinfo->marker->reset_marker_reader) (cinfo); + /* Reset progression state -- would be cleaner if entropy decoder did this */ + cinfo->coef_bits = NULL; +} + + +/* + * Initialize the input controller module. + * This is called only once, when the decompression object is created. + */ + +GLOBAL(void) +jinit_input_controller (j_decompress_ptr cinfo) +{ + my_inputctl_ptr inputctl; + + /* Create subobject in permanent pool */ + inputctl = (my_inputctl_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + SIZEOF(my_input_controller)); + cinfo->inputctl = (struct jpeg_input_controller *) inputctl; + /* Initialize method pointers */ + inputctl->pub.consume_input = consume_markers; + inputctl->pub.reset_input_controller = reset_input_controller; + inputctl->pub.start_input_pass = start_input_pass; + inputctl->pub.finish_input_pass = finish_input_pass; + /* Initialize state: can't use reset_input_controller since we don't + * want to try to reset other modules yet. + */ + inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ + inputctl->pub.eoi_reached = FALSE; + inputctl->inheaders = 1; +} diff --git a/crypto777/jpeg/jdmainct.c b/crypto777/jpeg/jdmainct.c new file mode 100644 index 000000000..02723ca73 --- /dev/null +++ b/crypto777/jpeg/jdmainct.c @@ -0,0 +1,512 @@ +/* + * jdmainct.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the main buffer controller for decompression. + * The main buffer lies between the JPEG decompressor proper and the + * post-processor; it holds downsampled data in the JPEG colorspace. + * + * Note that this code is bypassed in raw-data mode, since the application + * supplies the equivalent of the main buffer in that case. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * In the current system design, the main buffer need never be a full-image + * buffer; any full-height buffers will be found inside the coefficient or + * postprocessing controllers. Nonetheless, the main controller is not + * trivial. Its responsibility is to provide context rows for upsampling/ + * rescaling, and doing this in an efficient fashion is a bit tricky. + * + * Postprocessor input data is counted in "row groups". A row group + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) + * sample rows of each component. (We require DCT_scaled_size values to be + * chosen such that these numbers are integers. In practice DCT_scaled_size + * values will likely be powers of two, so we actually have the stronger + * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.) + * Upsampling will typically produce max_v_samp_factor pixel rows from each + * row group (times any additional scale factor that the upsampler is + * applying). + * + * The coefficient controller will deliver data to us one iMCU row at a time; + * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or + * exactly min_DCT_scaled_size row groups. (This amount of data corresponds + * to one row of MCUs when the image is fully interleaved.) Note that the + * number of sample rows varies across components, but the number of row + * groups does not. Some garbage sample rows may be included in the last iMCU + * row at the bottom of the image. + * + * Depending on the vertical scaling algorithm used, the upsampler may need + * access to the sample row(s) above and below its current input row group. + * The upsampler is required to set need_context_rows TRUE at global selection + * time if so. When need_context_rows is FALSE, this controller can simply + * obtain one iMCU row at a time from the coefficient controller and dole it + * out as row groups to the postprocessor. + * + * When need_context_rows is TRUE, this controller guarantees that the buffer + * passed to postprocessing contains at least one row group's worth of samples + * above and below the row group(s) being processed. Note that the context + * rows "above" the first passed row group appear at negative row offsets in + * the passed buffer. At the top and bottom of the image, the required + * context rows are manufactured by duplicating the first or last real sample + * row; this avoids having special cases in the upsampling inner loops. + * + * The amount of context is fixed at one row group just because that's a + * convenient number for this controller to work with. The existing + * upsamplers really only need one sample row of context. An upsampler + * supporting arbitrary output rescaling might wish for more than one row + * group of context when shrinking the image; tough, we don't handle that. + * (This is justified by the assumption that downsizing will be handled mostly + * by adjusting the DCT_scaled_size values, so that the actual scale factor at + * the upsample step needn't be much less than one.) + * + * To provide the desired context, we have to retain the last two row groups + * of one iMCU row while reading in the next iMCU row. (The last row group + * can't be processed until we have another row group for its below-context, + * and so we have to save the next-to-last group too for its above-context.) + * We could do this most simply by copying data around in our buffer, but + * that'd be very slow. We can avoid copying any data by creating a rather + * strange pointer structure. Here's how it works. We allocate a workspace + * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number + * of row groups per iMCU row). We create two sets of redundant pointers to + * the workspace. Labeling the physical row groups 0 to M+1, the synthesized + * pointer lists look like this: + * M+1 M-1 + * master pointer --> 0 master pointer --> 0 + * 1 1 + * ... ... + * M-3 M-3 + * M-2 M + * M-1 M+1 + * M M-2 + * M+1 M-1 + * 0 0 + * We read alternate iMCU rows using each master pointer; thus the last two + * row groups of the previous iMCU row remain un-overwritten in the workspace. + * The pointer lists are set up so that the required context rows appear to + * be adjacent to the proper places when we pass the pointer lists to the + * upsampler. + * + * The above pictures describe the normal state of the pointer lists. + * At top and bottom of the image, we diddle the pointer lists to duplicate + * the first or last sample row as necessary (this is cheaper than copying + * sample rows around). + * + * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that + * situation each iMCU row provides only one row group so the buffering logic + * must be different (eg, we must read two iMCU rows before we can emit the + * first row group). For now, we simply do not support providing context + * rows when min_DCT_scaled_size is 1. That combination seems unlikely to + * be worth providing --- if someone wants a 1/8th-size preview, they probably + * want it quick and dirty, so a context-free upsampler is sufficient. + */ + + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_d_main_controller pub; /* public fields */ + + /* Pointer to allocated workspace (M or M+2 row groups). */ + JSAMPARRAY buffer[MAX_COMPONENTS]; + + boolean buffer_full; /* Have we gotten an iMCU row from decoder? */ + JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */ + + /* Remaining fields are only used in the context case. */ + + /* These are the master pointers to the funny-order pointer lists. */ + JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */ + + int whichptr; /* indicates which pointer set is now in use */ + int context_state; /* process_data state machine status */ + JDIMENSION rowgroups_avail; /* row groups available to postprocessor */ + JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */ +} my_main_controller; + +typedef my_main_controller * my_main_ptr; + +/* context_state values: */ +#define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */ +#define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */ +#define CTX_POSTPONED_ROW 2 /* feeding postponed row group */ + + +/* Forward declarations */ +METHODDEF(void) process_data_simple_main + JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, + JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); +METHODDEF(void) process_data_context_main + JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, + JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); +#ifdef QUANT_2PASS_SUPPORTED +METHODDEF(void) process_data_crank_post + JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, + JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); +#endif + + +LOCAL(void) +alloc_funny_pointers (j_decompress_ptr cinfo) +/* Allocate space for the funny pointer lists. + * This is done only once, not once per pass. + */ +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + int ci, rgroup; + int M = cinfo->min_DCT_v_scaled_size; + jpeg_component_info *compptr; + JSAMPARRAY xbuf; + + /* Get top-level space for component array pointers. + * We alloc both arrays with one call to save a few cycles. + */ + main->xbuffer[0] = (JSAMPIMAGE) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->num_components * 2 * SIZEOF(JSAMPARRAY)); + main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; /* height of a row group of component */ + /* Get space for pointer lists --- M+4 row groups in each list. + * We alloc both pointer lists with one call to save a few cycles. + */ + xbuf = (JSAMPARRAY) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW)); + xbuf += rgroup; /* want one row group at negative offsets */ + main->xbuffer[0][ci] = xbuf; + xbuf += rgroup * (M + 4); + main->xbuffer[1][ci] = xbuf; + } +} + + +LOCAL(void) +make_funny_pointers (j_decompress_ptr cinfo) +/* Create the funny pointer lists discussed in the comments above. + * The actual workspace is already allocated (in main->buffer), + * and the space for the pointer lists is allocated too. + * This routine just fills in the curiously ordered lists. + * This will be repeated at the beginning of each pass. + */ +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + int ci, i, rgroup; + int M = cinfo->min_DCT_v_scaled_size; + jpeg_component_info *compptr; + JSAMPARRAY buf, xbuf0, xbuf1; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; /* height of a row group of component */ + xbuf0 = main->xbuffer[0][ci]; + xbuf1 = main->xbuffer[1][ci]; + /* First copy the workspace pointers as-is */ + buf = main->buffer[ci]; + for (i = 0; i < rgroup * (M + 2); i++) { + xbuf0[i] = xbuf1[i] = buf[i]; + } + /* In the second list, put the last four row groups in swapped order */ + for (i = 0; i < rgroup * 2; i++) { + xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i]; + xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i]; + } + /* The wraparound pointers at top and bottom will be filled later + * (see set_wraparound_pointers, below). Initially we want the "above" + * pointers to duplicate the first actual data line. This only needs + * to happen in xbuffer[0]. + */ + for (i = 0; i < rgroup; i++) { + xbuf0[i - rgroup] = xbuf0[0]; + } + } +} + + +LOCAL(void) +set_wraparound_pointers (j_decompress_ptr cinfo) +/* Set up the "wraparound" pointers at top and bottom of the pointer lists. + * This changes the pointer list state from top-of-image to the normal state. + */ +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + int ci, i, rgroup; + int M = cinfo->min_DCT_v_scaled_size; + jpeg_component_info *compptr; + JSAMPARRAY xbuf0, xbuf1; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; /* height of a row group of component */ + xbuf0 = main->xbuffer[0][ci]; + xbuf1 = main->xbuffer[1][ci]; + for (i = 0; i < rgroup; i++) { + xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i]; + xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i]; + xbuf0[rgroup*(M+2) + i] = xbuf0[i]; + xbuf1[rgroup*(M+2) + i] = xbuf1[i]; + } + } +} + + +LOCAL(void) +set_bottom_pointers (j_decompress_ptr cinfo) +/* Change the pointer lists to duplicate the last sample row at the bottom + * of the image. whichptr indicates which xbuffer holds the final iMCU row. + * Also sets rowgroups_avail to indicate number of nondummy row groups in row. + */ +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + int ci, i, rgroup, iMCUheight, rows_left; + jpeg_component_info *compptr; + JSAMPARRAY xbuf; + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Count sample rows in one iMCU row and in one row group */ + iMCUheight = compptr->v_samp_factor * compptr->DCT_v_scaled_size; + rgroup = iMCUheight / cinfo->min_DCT_v_scaled_size; + /* Count nondummy sample rows remaining for this component */ + rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight); + if (rows_left == 0) rows_left = iMCUheight; + /* Count nondummy row groups. Should get same answer for each component, + * so we need only do it once. + */ + if (ci == 0) { + main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1); + } + /* Duplicate the last real sample row rgroup*2 times; this pads out the + * last partial rowgroup and ensures at least one full rowgroup of context. + */ + xbuf = main->xbuffer[main->whichptr][ci]; + for (i = 0; i < rgroup * 2; i++) { + xbuf[rows_left + i] = xbuf[rows_left-1]; + } + } +} + + +/* + * Initialize for a processing pass. + */ + +METHODDEF(void) +start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + + switch (pass_mode) { + case JBUF_PASS_THRU: + if (cinfo->upsample->need_context_rows) { + main->pub.process_data = process_data_context_main; + make_funny_pointers(cinfo); /* Create the xbuffer[] lists */ + main->whichptr = 0; /* Read first iMCU row into xbuffer[0] */ + main->context_state = CTX_PREPARE_FOR_IMCU; + main->iMCU_row_ctr = 0; + } else { + /* Simple case with no context needed */ + main->pub.process_data = process_data_simple_main; + } + main->buffer_full = FALSE; /* Mark buffer empty */ + main->rowgroup_ctr = 0; + break; +#ifdef QUANT_2PASS_SUPPORTED + case JBUF_CRANK_DEST: + /* For last pass of 2-pass quantization, just crank the postprocessor */ + main->pub.process_data = process_data_crank_post; + break; +#endif + default: + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + break; + } +} + + +/* + * Process some data. + * This handles the simple case where no context is required. + */ + +METHODDEF(void) +process_data_simple_main (j_decompress_ptr cinfo, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + JDIMENSION rowgroups_avail; + + /* Read input data if we haven't filled the main buffer yet */ + if (! main->buffer_full) { + if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer)) + return; /* suspension forced, can do nothing more */ + main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ + } + + /* There are always min_DCT_scaled_size row groups in an iMCU row. */ + rowgroups_avail = (JDIMENSION) cinfo->min_DCT_v_scaled_size; + /* Note: at the bottom of the image, we may pass extra garbage row groups + * to the postprocessor. The postprocessor has to check for bottom + * of image anyway (at row resolution), so no point in us doing it too. + */ + + /* Feed the postprocessor */ + (*cinfo->post->post_process_data) (cinfo, main->buffer, + &main->rowgroup_ctr, rowgroups_avail, + output_buf, out_row_ctr, out_rows_avail); + + /* Has postprocessor consumed all the data yet? If so, mark buffer empty */ + if (main->rowgroup_ctr >= rowgroups_avail) { + main->buffer_full = FALSE; + main->rowgroup_ctr = 0; + } +} + + +/* + * Process some data. + * This handles the case where context rows must be provided. + */ + +METHODDEF(void) +process_data_context_main (j_decompress_ptr cinfo, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + my_main_ptr main = (my_main_ptr) cinfo->main; + + /* Read input data if we haven't filled the main buffer yet */ + if (! main->buffer_full) { + if (! (*cinfo->coef->decompress_data) (cinfo, + main->xbuffer[main->whichptr])) + return; /* suspension forced, can do nothing more */ + main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ + main->iMCU_row_ctr++; /* count rows received */ + } + + /* Postprocessor typically will not swallow all the input data it is handed + * in one call (due to filling the output buffer first). Must be prepared + * to exit and restart. This switch lets us keep track of how far we got. + * Note that each case falls through to the next on successful completion. + */ + switch (main->context_state) { + case CTX_POSTPONED_ROW: + /* Call postprocessor using previously set pointers for postponed row */ + (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr], + &main->rowgroup_ctr, main->rowgroups_avail, + output_buf, out_row_ctr, out_rows_avail); + if (main->rowgroup_ctr < main->rowgroups_avail) + return; /* Need to suspend */ + main->context_state = CTX_PREPARE_FOR_IMCU; + if (*out_row_ctr >= out_rows_avail) + return; /* Postprocessor exactly filled output buf */ + /*FALLTHROUGH*/ + case CTX_PREPARE_FOR_IMCU: + /* Prepare to process first M-1 row groups of this iMCU row */ + main->rowgroup_ctr = 0; + main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_v_scaled_size - 1); + /* Check for bottom of image: if so, tweak pointers to "duplicate" + * the last sample row, and adjust rowgroups_avail to ignore padding rows. + */ + if (main->iMCU_row_ctr == cinfo->total_iMCU_rows) + set_bottom_pointers(cinfo); + main->context_state = CTX_PROCESS_IMCU; + /*FALLTHROUGH*/ + case CTX_PROCESS_IMCU: + /* Call postprocessor using previously set pointers */ + (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr], + &main->rowgroup_ctr, main->rowgroups_avail, + output_buf, out_row_ctr, out_rows_avail); + if (main->rowgroup_ctr < main->rowgroups_avail) + return; /* Need to suspend */ + /* After the first iMCU, change wraparound pointers to normal state */ + if (main->iMCU_row_ctr == 1) + set_wraparound_pointers(cinfo); + /* Prepare to load new iMCU row using other xbuffer list */ + main->whichptr ^= 1; /* 0=>1 or 1=>0 */ + main->buffer_full = FALSE; + /* Still need to process last row group of this iMCU row, */ + /* which is saved at index M+1 of the other xbuffer */ + main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_v_scaled_size + 1); + main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_v_scaled_size + 2); + main->context_state = CTX_POSTPONED_ROW; + } +} + + +/* + * Process some data. + * Final pass of two-pass quantization: just call the postprocessor. + * Source data will be the postprocessor controller's internal buffer. + */ + +#ifdef QUANT_2PASS_SUPPORTED + +METHODDEF(void) +process_data_crank_post (j_decompress_ptr cinfo, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL, + (JDIMENSION *) NULL, (JDIMENSION) 0, + output_buf, out_row_ctr, out_rows_avail); +} + +#endif /* QUANT_2PASS_SUPPORTED */ + + +/* + * Initialize main buffer controller. + */ + +GLOBAL(void) +jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer) +{ + my_main_ptr main; + int ci, rgroup, ngroups; + jpeg_component_info *compptr; + + main = (my_main_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_main_controller)); + cinfo->main = (struct jpeg_d_main_controller *) main; + main->pub.start_pass = start_pass_main; + + if (need_full_buffer) /* shouldn't happen */ + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + + /* Allocate the workspace. + * ngroups is the number of row groups we need. + */ + if (cinfo->upsample->need_context_rows) { + if (cinfo->min_DCT_v_scaled_size < 2) /* unsupported, see comments above */ + ERREXIT(cinfo, JERR_NOTIMPL); + alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */ + ngroups = cinfo->min_DCT_v_scaled_size + 2; + } else { + ngroups = cinfo->min_DCT_v_scaled_size; + } + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; /* height of a row group of component */ + main->buffer[ci] = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + compptr->width_in_blocks * compptr->DCT_h_scaled_size, + (JDIMENSION) (rgroup * ngroups)); + } +} diff --git a/crypto777/jpeg/jdmarker.c b/crypto777/jpeg/jdmarker.c new file mode 100644 index 000000000..f2a9cc429 --- /dev/null +++ b/crypto777/jpeg/jdmarker.c @@ -0,0 +1,1406 @@ +/* + * jdmarker.c + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * Modified 2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to decode JPEG datastream markers. + * Most of the complexity arises from our desire to support input + * suspension: if not all of the data for a marker is available, + * we must exit back to the application. On resumption, we reprocess + * the marker. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +typedef enum { /* JPEG marker codes */ + M_SOF0 = 0xc0, + M_SOF1 = 0xc1, + M_SOF2 = 0xc2, + M_SOF3 = 0xc3, + + M_SOF5 = 0xc5, + M_SOF6 = 0xc6, + M_SOF7 = 0xc7, + + M_JPG = 0xc8, + M_SOF9 = 0xc9, + M_SOF10 = 0xca, + M_SOF11 = 0xcb, + + M_SOF13 = 0xcd, + M_SOF14 = 0xce, + M_SOF15 = 0xcf, + + M_DHT = 0xc4, + + M_DAC = 0xcc, + + M_RST0 = 0xd0, + M_RST1 = 0xd1, + M_RST2 = 0xd2, + M_RST3 = 0xd3, + M_RST4 = 0xd4, + M_RST5 = 0xd5, + M_RST6 = 0xd6, + M_RST7 = 0xd7, + + M_SOI = 0xd8, + M_EOI = 0xd9, + M_SOS = 0xda, + M_DQT = 0xdb, + M_DNL = 0xdc, + M_DRI = 0xdd, + M_DHP = 0xde, + M_EXP = 0xdf, + + M_APP0 = 0xe0, + M_APP1 = 0xe1, + M_APP2 = 0xe2, + M_APP3 = 0xe3, + M_APP4 = 0xe4, + M_APP5 = 0xe5, + M_APP6 = 0xe6, + M_APP7 = 0xe7, + M_APP8 = 0xe8, + M_APP9 = 0xe9, + M_APP10 = 0xea, + M_APP11 = 0xeb, + M_APP12 = 0xec, + M_APP13 = 0xed, + M_APP14 = 0xee, + M_APP15 = 0xef, + + M_JPG0 = 0xf0, + M_JPG13 = 0xfd, + M_COM = 0xfe, + + M_TEM = 0x01, + + M_ERROR = 0x100 +} JPEG_MARKER; + + +/* Private state */ + +typedef struct { + struct jpeg_marker_reader pub; /* public fields */ + + /* Application-overridable marker processing methods */ + jpeg_marker_parser_method process_COM; + jpeg_marker_parser_method process_APPn[16]; + + /* Limit on marker data length to save for each marker type */ + unsigned int length_limit_COM; + unsigned int length_limit_APPn[16]; + + /* Status of COM/APPn marker saving */ + jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */ + unsigned int bytes_read; /* data bytes read so far in marker */ + /* Note: cur_marker is not linked into marker_list until it's all read. */ +} my_marker_reader; + +typedef my_marker_reader * my_marker_ptr; + + +/* + * Macros for fetching data from the data source module. + * + * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect + * the current restart point; we update them only when we have reached a + * suitable place to restart if a suspension occurs. + */ + +/* Declare and initialize local copies of input pointer/count */ +#define INPUT_VARS(cinfo) \ + struct jpeg_source_mgr * datasrc = (cinfo)->src; \ + const JOCTET * next_input_byte = datasrc->next_input_byte; \ + size_t bytes_in_buffer = datasrc->bytes_in_buffer + +/* Unload the local copies --- do this only at a restart boundary */ +#define INPUT_SYNC(cinfo) \ + ( datasrc->next_input_byte = next_input_byte, \ + datasrc->bytes_in_buffer = bytes_in_buffer ) + +/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */ +#define INPUT_RELOAD(cinfo) \ + ( next_input_byte = datasrc->next_input_byte, \ + bytes_in_buffer = datasrc->bytes_in_buffer ) + +/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available. + * Note we do *not* do INPUT_SYNC before calling fill_input_buffer, + * but we must reload the local copies after a successful fill. + */ +#define MAKE_BYTE_AVAIL(cinfo,action) \ + if (bytes_in_buffer == 0) { \ + if (! (*datasrc->fill_input_buffer) (cinfo)) \ + { action; } \ + INPUT_RELOAD(cinfo); \ + } + +/* Read a byte into variable V. + * If must suspend, take the specified action (typically "return FALSE"). + */ +#define INPUT_BYTE(cinfo,V,action) \ + MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ + bytes_in_buffer--; \ + V = GETJOCTET(*next_input_byte++); ) + +/* As above, but read two bytes interpreted as an unsigned 16-bit integer. + * V should be declared unsigned int or perhaps INT32. + */ +#define INPUT_2BYTES(cinfo,V,action) \ + MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ + bytes_in_buffer--; \ + V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \ + MAKE_BYTE_AVAIL(cinfo,action); \ + bytes_in_buffer--; \ + V += GETJOCTET(*next_input_byte++); ) + + +/* + * Routines to process JPEG markers. + * + * Entry condition: JPEG marker itself has been read and its code saved + * in cinfo->unread_marker; input restart point is just after the marker. + * + * Exit: if return TRUE, have read and processed any parameters, and have + * updated the restart point to point after the parameters. + * If return FALSE, was forced to suspend before reaching end of + * marker parameters; restart point has not been moved. Same routine + * will be called again after application supplies more input data. + * + * This approach to suspension assumes that all of a marker's parameters + * can fit into a single input bufferload. This should hold for "normal" + * markers. Some COM/APPn markers might have large parameter segments + * that might not fit. If we are simply dropping such a marker, we use + * skip_input_data to get past it, and thereby put the problem on the + * source manager's shoulders. If we are saving the marker's contents + * into memory, we use a slightly different convention: when forced to + * suspend, the marker processor updates the restart point to the end of + * what it's consumed (ie, the end of the buffer) before returning FALSE. + * On resumption, cinfo->unread_marker still contains the marker code, + * but the data source will point to the next chunk of marker data. + * The marker processor must retain internal state to deal with this. + * + * Note that we don't bother to avoid duplicate trace messages if a + * suspension occurs within marker parameters. Other side effects + * require more care. + */ + + +LOCAL(boolean) +get_soi (j_decompress_ptr cinfo) +/* Process an SOI marker */ +{ + int i; + + TRACEMS(cinfo, 1, JTRC_SOI); + + if (cinfo->marker->saw_SOI) + ERREXIT(cinfo, JERR_SOI_DUPLICATE); + + /* Reset all parameters that are defined to be reset by SOI */ + + for (i = 0; i < NUM_ARITH_TBLS; i++) { + cinfo->arith_dc_L[i] = 0; + cinfo->arith_dc_U[i] = 1; + cinfo->arith_ac_K[i] = 5; + } + cinfo->restart_interval = 0; + + /* Set initial assumptions for colorspace etc */ + + cinfo->jpeg_color_space = JCS_UNKNOWN; + cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */ + + cinfo->saw_JFIF_marker = FALSE; + cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */ + cinfo->JFIF_minor_version = 1; + cinfo->density_unit = 0; + cinfo->X_density = 1; + cinfo->Y_density = 1; + cinfo->saw_Adobe_marker = FALSE; + cinfo->Adobe_transform = 0; + + cinfo->marker->saw_SOI = TRUE; + + return TRUE; +} + + +LOCAL(boolean) +get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog, + boolean is_arith) +/* Process a SOFn marker */ +{ + INT32 length; + int c, ci; + jpeg_component_info * compptr; + INPUT_VARS(cinfo); + + cinfo->is_baseline = is_baseline; + cinfo->progressive_mode = is_prog; + cinfo->arith_code = is_arith; + + INPUT_2BYTES(cinfo, length, return FALSE); + + INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE); + INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE); + INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE); + INPUT_BYTE(cinfo, cinfo->num_components, return FALSE); + + length -= 8; + + TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker, + (int) cinfo->image_width, (int) cinfo->image_height, + cinfo->num_components); + + if (cinfo->marker->saw_SOF) + ERREXIT(cinfo, JERR_SOF_DUPLICATE); + + /* We don't support files in which the image height is initially specified */ + /* as 0 and is later redefined by DNL. As long as we have to check that, */ + /* might as well have a general sanity check. */ + if (cinfo->image_height <= 0 || cinfo->image_width <= 0 + || cinfo->num_components <= 0) + ERREXIT(cinfo, JERR_EMPTY_IMAGE); + + if (length != (cinfo->num_components * 3)) + ERREXIT(cinfo, JERR_BAD_LENGTH); + + if (cinfo->comp_info == NULL) /* do only once, even if suspend */ + cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->num_components * SIZEOF(jpeg_component_info)); + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + compptr->component_index = ci; + INPUT_BYTE(cinfo, compptr->component_id, return FALSE); + INPUT_BYTE(cinfo, c, return FALSE); + compptr->h_samp_factor = (c >> 4) & 15; + compptr->v_samp_factor = (c ) & 15; + INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE); + + TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT, + compptr->component_id, compptr->h_samp_factor, + compptr->v_samp_factor, compptr->quant_tbl_no); + } + + cinfo->marker->saw_SOF = TRUE; + + INPUT_SYNC(cinfo); + return TRUE; +} + + +LOCAL(boolean) +get_sos (j_decompress_ptr cinfo) +/* Process a SOS marker */ +{ + INT32 length; + int i, ci, n, c, cc; + jpeg_component_info * compptr; + INPUT_VARS(cinfo); + + if (! cinfo->marker->saw_SOF) + ERREXIT(cinfo, JERR_SOS_NO_SOF); + + INPUT_2BYTES(cinfo, length, return FALSE); + + INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */ + + TRACEMS1(cinfo, 1, JTRC_SOS, n); + + if (length != (n * 2 + 6) || n > MAX_COMPS_IN_SCAN || + (n == 0 && !cinfo->progressive_mode)) + /* pseudo SOS marker only allowed in progressive mode */ + ERREXIT(cinfo, JERR_BAD_LENGTH); + + cinfo->comps_in_scan = n; + + /* Collect the component-spec parameters */ + + for (i = 0; i < n; i++) { + INPUT_BYTE(cinfo, cc, return FALSE); + INPUT_BYTE(cinfo, c, return FALSE); + + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + if (cc == compptr->component_id) + goto id_found; + } + + ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc); + + id_found: + + cinfo->cur_comp_info[i] = compptr; + compptr->dc_tbl_no = (c >> 4) & 15; + compptr->ac_tbl_no = (c ) & 15; + + TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc, + compptr->dc_tbl_no, compptr->ac_tbl_no); + } + + /* Collect the additional scan parameters Ss, Se, Ah/Al. */ + INPUT_BYTE(cinfo, c, return FALSE); + cinfo->Ss = c; + INPUT_BYTE(cinfo, c, return FALSE); + cinfo->Se = c; + INPUT_BYTE(cinfo, c, return FALSE); + cinfo->Ah = (c >> 4) & 15; + cinfo->Al = (c ) & 15; + + TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se, + cinfo->Ah, cinfo->Al); + + /* Prepare to scan data & restart markers */ + cinfo->marker->next_restart_num = 0; + + /* Count another (non-pseudo) SOS marker */ + if (n) cinfo->input_scan_number++; + + INPUT_SYNC(cinfo); + return TRUE; +} + + +#ifdef D_ARITH_CODING_SUPPORTED + +LOCAL(boolean) +get_dac (j_decompress_ptr cinfo) +/* Process a DAC marker */ +{ + INT32 length; + int index, val; + INPUT_VARS(cinfo); + + INPUT_2BYTES(cinfo, length, return FALSE); + length -= 2; + + while (length > 0) { + INPUT_BYTE(cinfo, index, return FALSE); + INPUT_BYTE(cinfo, val, return FALSE); + + length -= 2; + + TRACEMS2(cinfo, 1, JTRC_DAC, index, val); + + if (index < 0 || index >= (2*NUM_ARITH_TBLS)) + ERREXIT1(cinfo, JERR_DAC_INDEX, index); + + if (index >= NUM_ARITH_TBLS) { /* define AC table */ + cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val; + } else { /* define DC table */ + cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F); + cinfo->arith_dc_U[index] = (UINT8) (val >> 4); + if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index]) + ERREXIT1(cinfo, JERR_DAC_VALUE, val); + } + } + + if (length != 0) + ERREXIT(cinfo, JERR_BAD_LENGTH); + + INPUT_SYNC(cinfo); + return TRUE; +} + +#else /* ! D_ARITH_CODING_SUPPORTED */ + +#define get_dac(cinfo) skip_variable(cinfo) + +#endif /* D_ARITH_CODING_SUPPORTED */ + + +LOCAL(boolean) +get_dht (j_decompress_ptr cinfo) +/* Process a DHT marker */ +{ + INT32 length; + UINT8 bits[17]; + UINT8 huffval[256]; + int i, index, count; + JHUFF_TBL **htblptr; + INPUT_VARS(cinfo); + + INPUT_2BYTES(cinfo, length, return FALSE); + length -= 2; + + while (length > 16) { + INPUT_BYTE(cinfo, index, return FALSE); + + TRACEMS1(cinfo, 1, JTRC_DHT, index); + + bits[0] = 0; + count = 0; + for (i = 1; i <= 16; i++) { + INPUT_BYTE(cinfo, bits[i], return FALSE); + count += bits[i]; + } + + length -= 1 + 16; + + TRACEMS8(cinfo, 2, JTRC_HUFFBITS, + bits[1], bits[2], bits[3], bits[4], + bits[5], bits[6], bits[7], bits[8]); + TRACEMS8(cinfo, 2, JTRC_HUFFBITS, + bits[9], bits[10], bits[11], bits[12], + bits[13], bits[14], bits[15], bits[16]); + + /* Here we just do minimal validation of the counts to avoid walking + * off the end of our table space. jdhuff.c will check more carefully. + */ + if (count > 256 || ((INT32) count) > length) + ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); + + for (i = 0; i < count; i++) + INPUT_BYTE(cinfo, huffval[i], return FALSE); + + length -= count; + + if (index & 0x10) { /* AC table definition */ + index -= 0x10; + htblptr = &cinfo->ac_huff_tbl_ptrs[index]; + } else { /* DC table definition */ + htblptr = &cinfo->dc_huff_tbl_ptrs[index]; + } + + if (index < 0 || index >= NUM_HUFF_TBLS) + ERREXIT1(cinfo, JERR_DHT_INDEX, index); + + if (*htblptr == NULL) + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); + + MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); + MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval)); + } + + if (length != 0) + ERREXIT(cinfo, JERR_BAD_LENGTH); + + INPUT_SYNC(cinfo); + return TRUE; +} + + +LOCAL(boolean) +get_dqt (j_decompress_ptr cinfo) +/* Process a DQT marker */ +{ + INT32 length, count, i; + int n, prec; + unsigned int tmp; + JQUANT_TBL *quant_ptr; + const int *natural_order; + INPUT_VARS(cinfo); + + INPUT_2BYTES(cinfo, length, return FALSE); + length -= 2; + + while (length > 0) { + length--; + INPUT_BYTE(cinfo, n, return FALSE); + prec = n >> 4; + n &= 0x0F; + + TRACEMS2(cinfo, 1, JTRC_DQT, n, prec); + + if (n >= NUM_QUANT_TBLS) + ERREXIT1(cinfo, JERR_DQT_INDEX, n); + + if (cinfo->quant_tbl_ptrs[n] == NULL) + cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo); + quant_ptr = cinfo->quant_tbl_ptrs[n]; + + if (prec) { + if (length < DCTSIZE2 * 2) { + /* Initialize full table for safety. */ + for (i = 0; i < DCTSIZE2; i++) { + quant_ptr->quantval[i] = 1; + } + count = length >> 1; + } else + count = DCTSIZE2; + } else { + if (length < DCTSIZE2) { + /* Initialize full table for safety. */ + for (i = 0; i < DCTSIZE2; i++) { + quant_ptr->quantval[i] = 1; + } + count = length; + } else + count = DCTSIZE2; + } + + switch (count) { + case (2*2): natural_order = jpeg_natural_order2; break; + case (3*3): natural_order = jpeg_natural_order3; break; + case (4*4): natural_order = jpeg_natural_order4; break; + case (5*5): natural_order = jpeg_natural_order5; break; + case (6*6): natural_order = jpeg_natural_order6; break; + case (7*7): natural_order = jpeg_natural_order7; break; + default: natural_order = jpeg_natural_order; break; + } + + for (i = 0; i < count; i++) { + if (prec) + INPUT_2BYTES(cinfo, tmp, return FALSE); + else + INPUT_BYTE(cinfo, tmp, return FALSE); + /* We convert the zigzag-order table to natural array order. */ + quant_ptr->quantval[natural_order[i]] = (UINT16) tmp; + } + + if (cinfo->err->trace_level >= 2) { + for (i = 0; i < DCTSIZE2; i += 8) { + TRACEMS8(cinfo, 2, JTRC_QUANTVALS, + quant_ptr->quantval[i], quant_ptr->quantval[i+1], + quant_ptr->quantval[i+2], quant_ptr->quantval[i+3], + quant_ptr->quantval[i+4], quant_ptr->quantval[i+5], + quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]); + } + } + + length -= count; + if (prec) length -= count; + } + + if (length != 0) + ERREXIT(cinfo, JERR_BAD_LENGTH); + + INPUT_SYNC(cinfo); + return TRUE; +} + + +LOCAL(boolean) +get_dri (j_decompress_ptr cinfo) +/* Process a DRI marker */ +{ + INT32 length; + unsigned int tmp; + INPUT_VARS(cinfo); + + INPUT_2BYTES(cinfo, length, return FALSE); + + if (length != 4) + ERREXIT(cinfo, JERR_BAD_LENGTH); + + INPUT_2BYTES(cinfo, tmp, return FALSE); + + TRACEMS1(cinfo, 1, JTRC_DRI, tmp); + + cinfo->restart_interval = tmp; + + INPUT_SYNC(cinfo); + return TRUE; +} + + +/* + * Routines for processing APPn and COM markers. + * These are either saved in memory or discarded, per application request. + * APP0 and APP14 are specially checked to see if they are + * JFIF and Adobe markers, respectively. + */ + +#define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */ +#define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */ +#define APPN_DATA_LEN 14 /* Must be the largest of the above!! */ + + +LOCAL(void) +examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data, + unsigned int datalen, INT32 remaining) +/* Examine first few bytes from an APP0. + * Take appropriate action if it is a JFIF marker. + * datalen is # of bytes at data[], remaining is length of rest of marker data. + */ +{ + INT32 totallen = (INT32) datalen + remaining; + + if (datalen >= APP0_DATA_LEN && + GETJOCTET(data[0]) == 0x4A && + GETJOCTET(data[1]) == 0x46 && + GETJOCTET(data[2]) == 0x49 && + GETJOCTET(data[3]) == 0x46 && + GETJOCTET(data[4]) == 0) { + /* Found JFIF APP0 marker: save info */ + cinfo->saw_JFIF_marker = TRUE; + cinfo->JFIF_major_version = GETJOCTET(data[5]); + cinfo->JFIF_minor_version = GETJOCTET(data[6]); + cinfo->density_unit = GETJOCTET(data[7]); + cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]); + cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]); + /* Check version. + * Major version must be 1, anything else signals an incompatible change. + * (We used to treat this as an error, but now it's a nonfatal warning, + * because some bozo at Hijaak couldn't read the spec.) + * Minor version should be 0..2, but process anyway if newer. + */ + if (cinfo->JFIF_major_version != 1) + WARNMS2(cinfo, JWRN_JFIF_MAJOR, + cinfo->JFIF_major_version, cinfo->JFIF_minor_version); + /* Generate trace messages */ + TRACEMS5(cinfo, 1, JTRC_JFIF, + cinfo->JFIF_major_version, cinfo->JFIF_minor_version, + cinfo->X_density, cinfo->Y_density, cinfo->density_unit); + /* Validate thumbnail dimensions and issue appropriate messages */ + if (GETJOCTET(data[12]) | GETJOCTET(data[13])) + TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, + GETJOCTET(data[12]), GETJOCTET(data[13])); + totallen -= APP0_DATA_LEN; + if (totallen != + ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3)) + TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen); + } else if (datalen >= 6 && + GETJOCTET(data[0]) == 0x4A && + GETJOCTET(data[1]) == 0x46 && + GETJOCTET(data[2]) == 0x58 && + GETJOCTET(data[3]) == 0x58 && + GETJOCTET(data[4]) == 0) { + /* Found JFIF "JFXX" extension APP0 marker */ + /* The library doesn't actually do anything with these, + * but we try to produce a helpful trace message. + */ + switch (GETJOCTET(data[5])) { + case 0x10: + TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen); + break; + case 0x11: + TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen); + break; + case 0x13: + TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen); + break; + default: + TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION, + GETJOCTET(data[5]), (int) totallen); + break; + } + } else { + /* Start of APP0 does not match "JFIF" or "JFXX", or too short */ + TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen); + } +} + + +LOCAL(void) +examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data, + unsigned int datalen, INT32 remaining) +/* Examine first few bytes from an APP14. + * Take appropriate action if it is an Adobe marker. + * datalen is # of bytes at data[], remaining is length of rest of marker data. + */ +{ + unsigned int version, flags0, flags1, transform; + + if (datalen >= APP14_DATA_LEN && + GETJOCTET(data[0]) == 0x41 && + GETJOCTET(data[1]) == 0x64 && + GETJOCTET(data[2]) == 0x6F && + GETJOCTET(data[3]) == 0x62 && + GETJOCTET(data[4]) == 0x65) { + /* Found Adobe APP14 marker */ + version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]); + flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]); + flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]); + transform = GETJOCTET(data[11]); + TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform); + cinfo->saw_Adobe_marker = TRUE; + cinfo->Adobe_transform = (UINT8) transform; + } else { + /* Start of APP14 does not match "Adobe", or too short */ + TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining)); + } +} + + +METHODDEF(boolean) +get_interesting_appn (j_decompress_ptr cinfo) +/* Process an APP0 or APP14 marker without saving it */ +{ + INT32 length; + JOCTET b[APPN_DATA_LEN]; + unsigned int i, numtoread; + INPUT_VARS(cinfo); + + INPUT_2BYTES(cinfo, length, return FALSE); + length -= 2; + + /* get the interesting part of the marker data */ + if (length >= APPN_DATA_LEN) + numtoread = APPN_DATA_LEN; + else if (length > 0) + numtoread = (unsigned int) length; + else + numtoread = 0; + for (i = 0; i < numtoread; i++) + INPUT_BYTE(cinfo, b[i], return FALSE); + length -= numtoread; + + /* process it */ + switch (cinfo->unread_marker) { + case M_APP0: + examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length); + break; + case M_APP14: + examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length); + break; + default: + /* can't get here unless jpeg_save_markers chooses wrong processor */ + ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); + break; + } + + /* skip any remaining data -- could be lots */ + INPUT_SYNC(cinfo); + if (length > 0) + (*cinfo->src->skip_input_data) (cinfo, (long) length); + + return TRUE; +} + + +#ifdef SAVE_MARKERS_SUPPORTED + +METHODDEF(boolean) +save_marker (j_decompress_ptr cinfo) +/* Save an APPn or COM marker into the marker list */ +{ + my_marker_ptr marker = (my_marker_ptr) cinfo->marker; + jpeg_saved_marker_ptr cur_marker = marker->cur_marker; + unsigned int bytes_read, data_length; + JOCTET FAR * data; + INT32 length = 0; + INPUT_VARS(cinfo); + + if (cur_marker == NULL) { + /* begin reading a marker */ + INPUT_2BYTES(cinfo, length, return FALSE); + length -= 2; + if (length >= 0) { /* watch out for bogus length word */ + /* figure out how much we want to save */ + unsigned int limit; + if (cinfo->unread_marker == (int) M_COM) + limit = marker->length_limit_COM; + else + limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0]; + if ((unsigned int) length < limit) + limit = (unsigned int) length; + /* allocate and initialize the marker item */ + cur_marker = (jpeg_saved_marker_ptr) + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(struct jpeg_marker_struct) + limit); + cur_marker->next = NULL; + cur_marker->marker = (UINT8) cinfo->unread_marker; + cur_marker->original_length = (unsigned int) length; + cur_marker->data_length = limit; + /* data area is just beyond the jpeg_marker_struct */ + data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1); + marker->cur_marker = cur_marker; + marker->bytes_read = 0; + bytes_read = 0; + data_length = limit; + } else { + /* deal with bogus length word */ + bytes_read = data_length = 0; + data = NULL; + } + } else { + /* resume reading a marker */ + bytes_read = marker->bytes_read; + data_length = cur_marker->data_length; + data = cur_marker->data + bytes_read; + } + + while (bytes_read < data_length) { + INPUT_SYNC(cinfo); /* move the restart point to here */ + marker->bytes_read = bytes_read; + /* If there's not at least one byte in buffer, suspend */ + MAKE_BYTE_AVAIL(cinfo, return FALSE); + /* Copy bytes with reasonable rapidity */ + while (bytes_read < data_length && bytes_in_buffer > 0) { + *data++ = *next_input_byte++; + bytes_in_buffer--; + bytes_read++; + } + } + + /* Done reading what we want to read */ + if (cur_marker != NULL) { /* will be NULL if bogus length word */ + /* Add new marker to end of list */ + if (cinfo->marker_list == NULL) { + cinfo->marker_list = cur_marker; + } else { + jpeg_saved_marker_ptr prev = cinfo->marker_list; + while (prev->next != NULL) + prev = prev->next; + prev->next = cur_marker; + } + /* Reset pointer & calc remaining data length */ + data = cur_marker->data; + length = cur_marker->original_length - data_length; + } + /* Reset to initial state for next marker */ + marker->cur_marker = NULL; + + /* Process the marker if interesting; else just make a generic trace msg */ + switch (cinfo->unread_marker) { + case M_APP0: + examine_app0(cinfo, data, data_length, length); + break; + case M_APP14: + examine_app14(cinfo, data, data_length, length); + break; + default: + TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, + (int) (data_length + length)); + break; + } + + /* skip any remaining data -- could be lots */ + INPUT_SYNC(cinfo); /* do before skip_input_data */ + if (length > 0) + (*cinfo->src->skip_input_data) (cinfo, (long) length); + + return TRUE; +} + +#endif /* SAVE_MARKERS_SUPPORTED */ + + +METHODDEF(boolean) +skip_variable (j_decompress_ptr cinfo) +/* Skip over an unknown or uninteresting variable-length marker */ +{ + INT32 length; + INPUT_VARS(cinfo); + + INPUT_2BYTES(cinfo, length, return FALSE); + length -= 2; + + TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length); + + INPUT_SYNC(cinfo); /* do before skip_input_data */ + if (length > 0) + (*cinfo->src->skip_input_data) (cinfo, (long) length); + + return TRUE; +} + + +/* + * Find the next JPEG marker, save it in cinfo->unread_marker. + * Returns FALSE if had to suspend before reaching a marker; + * in that case cinfo->unread_marker is unchanged. + * + * Note that the result might not be a valid marker code, + * but it will never be 0 or FF. + */ + +LOCAL(boolean) +next_marker (j_decompress_ptr cinfo) +{ + int c; + INPUT_VARS(cinfo); + + for (;;) { + INPUT_BYTE(cinfo, c, return FALSE); + /* Skip any non-FF bytes. + * This may look a bit inefficient, but it will not occur in a valid file. + * We sync after each discarded byte so that a suspending data source + * can discard the byte from its buffer. + */ + while (c != 0xFF) { + cinfo->marker->discarded_bytes++; + INPUT_SYNC(cinfo); + INPUT_BYTE(cinfo, c, return FALSE); + } + /* This loop swallows any duplicate FF bytes. Extra FFs are legal as + * pad bytes, so don't count them in discarded_bytes. We assume there + * will not be so many consecutive FF bytes as to overflow a suspending + * data source's input buffer. + */ + do { + INPUT_BYTE(cinfo, c, return FALSE); + } while (c == 0xFF); + if (c != 0) + break; /* found a valid marker, exit loop */ + /* Reach here if we found a stuffed-zero data sequence (FF/00). + * Discard it and loop back to try again. + */ + cinfo->marker->discarded_bytes += 2; + INPUT_SYNC(cinfo); + } + + if (cinfo->marker->discarded_bytes != 0) { + WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c); + cinfo->marker->discarded_bytes = 0; + } + + cinfo->unread_marker = c; + + INPUT_SYNC(cinfo); + return TRUE; +} + + +LOCAL(boolean) +first_marker (j_decompress_ptr cinfo) +/* Like next_marker, but used to obtain the initial SOI marker. */ +/* For this marker, we do not allow preceding garbage or fill; otherwise, + * we might well scan an entire input file before realizing it ain't JPEG. + * If an application wants to process non-JFIF files, it must seek to the + * SOI before calling the JPEG library. + */ +{ + int c, c2; + INPUT_VARS(cinfo); + + INPUT_BYTE(cinfo, c, return FALSE); + INPUT_BYTE(cinfo, c2, return FALSE); + if (c != 0xFF || c2 != (int) M_SOI) + ERREXIT2(cinfo, JERR_NO_SOI, c, c2); + + cinfo->unread_marker = c2; + + INPUT_SYNC(cinfo); + return TRUE; +} + + +/* + * Read markers until SOS or EOI. + * + * Returns same codes as are defined for jpeg_consume_input: + * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. + * + * Note: This function may return a pseudo SOS marker (with zero + * component number) for treat by input controller's consume_input. + * consume_input itself should filter out (skip) the pseudo marker + * after processing for the caller. + */ + +METHODDEF(int) +read_markers (j_decompress_ptr cinfo) +{ + /* Outer loop repeats once for each marker. */ + for (;;) { + /* Collect the marker proper, unless we already did. */ + /* NB: first_marker() enforces the requirement that SOI appear first. */ + if (cinfo->unread_marker == 0) { + if (! cinfo->marker->saw_SOI) { + if (! first_marker(cinfo)) + return JPEG_SUSPENDED; + } else { + if (! next_marker(cinfo)) + return JPEG_SUSPENDED; + } + } + /* At this point cinfo->unread_marker contains the marker code and the + * input point is just past the marker proper, but before any parameters. + * A suspension will cause us to return with this state still true. + */ + switch (cinfo->unread_marker) { + case M_SOI: + if (! get_soi(cinfo)) + return JPEG_SUSPENDED; + break; + + case M_SOF0: /* Baseline */ + if (! get_sof(cinfo, TRUE, FALSE, FALSE)) + return JPEG_SUSPENDED; + break; + + case M_SOF1: /* Extended sequential, Huffman */ + if (! get_sof(cinfo, FALSE, FALSE, FALSE)) + return JPEG_SUSPENDED; + break; + + case M_SOF2: /* Progressive, Huffman */ + if (! get_sof(cinfo, FALSE, TRUE, FALSE)) + return JPEG_SUSPENDED; + break; + + case M_SOF9: /* Extended sequential, arithmetic */ + if (! get_sof(cinfo, FALSE, FALSE, TRUE)) + return JPEG_SUSPENDED; + break; + + case M_SOF10: /* Progressive, arithmetic */ + if (! get_sof(cinfo, FALSE, TRUE, TRUE)) + return JPEG_SUSPENDED; + break; + + /* Currently unsupported SOFn types */ + case M_SOF3: /* Lossless, Huffman */ + case M_SOF5: /* Differential sequential, Huffman */ + case M_SOF6: /* Differential progressive, Huffman */ + case M_SOF7: /* Differential lossless, Huffman */ + case M_JPG: /* Reserved for JPEG extensions */ + case M_SOF11: /* Lossless, arithmetic */ + case M_SOF13: /* Differential sequential, arithmetic */ + case M_SOF14: /* Differential progressive, arithmetic */ + case M_SOF15: /* Differential lossless, arithmetic */ + ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker); + break; + + case M_SOS: + if (! get_sos(cinfo)) + return JPEG_SUSPENDED; + cinfo->unread_marker = 0; /* processed the marker */ + return JPEG_REACHED_SOS; + + case M_EOI: + TRACEMS(cinfo, 1, JTRC_EOI); + cinfo->unread_marker = 0; /* processed the marker */ + return JPEG_REACHED_EOI; + + case M_DAC: + if (! get_dac(cinfo)) + return JPEG_SUSPENDED; + break; + + case M_DHT: + if (! get_dht(cinfo)) + return JPEG_SUSPENDED; + break; + + case M_DQT: + if (! get_dqt(cinfo)) + return JPEG_SUSPENDED; + break; + + case M_DRI: + if (! get_dri(cinfo)) + return JPEG_SUSPENDED; + break; + + case M_APP0: + case M_APP1: + case M_APP2: + case M_APP3: + case M_APP4: + case M_APP5: + case M_APP6: + case M_APP7: + case M_APP8: + case M_APP9: + case M_APP10: + case M_APP11: + case M_APP12: + case M_APP13: + case M_APP14: + case M_APP15: + if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[ + cinfo->unread_marker - (int) M_APP0]) (cinfo)) + return JPEG_SUSPENDED; + break; + + case M_COM: + if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo)) + return JPEG_SUSPENDED; + break; + + case M_RST0: /* these are all parameterless */ + case M_RST1: + case M_RST2: + case M_RST3: + case M_RST4: + case M_RST5: + case M_RST6: + case M_RST7: + case M_TEM: + TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker); + break; + + case M_DNL: /* Ignore DNL ... perhaps the wrong thing */ + if (! skip_variable(cinfo)) + return JPEG_SUSPENDED; + break; + + default: /* must be DHP, EXP, JPGn, or RESn */ + /* For now, we treat the reserved markers as fatal errors since they are + * likely to be used to signal incompatible JPEG Part 3 extensions. + * Once the JPEG 3 version-number marker is well defined, this code + * ought to change! + */ + ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); + break; + } + /* Successfully processed marker, so reset state variable */ + cinfo->unread_marker = 0; + } /* end loop */ +} + + +/* + * Read a restart marker, which is expected to appear next in the datastream; + * if the marker is not there, take appropriate recovery action. + * Returns FALSE if suspension is required. + * + * This is called by the entropy decoder after it has read an appropriate + * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder + * has already read a marker from the data source. Under normal conditions + * cinfo->unread_marker will be reset to 0 before returning; if not reset, + * it holds a marker which the decoder will be unable to read past. + */ + +METHODDEF(boolean) +read_restart_marker (j_decompress_ptr cinfo) +{ + /* Obtain a marker unless we already did. */ + /* Note that next_marker will complain if it skips any data. */ + if (cinfo->unread_marker == 0) { + if (! next_marker(cinfo)) + return FALSE; + } + + if (cinfo->unread_marker == + ((int) M_RST0 + cinfo->marker->next_restart_num)) { + /* Normal case --- swallow the marker and let entropy decoder continue */ + TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num); + cinfo->unread_marker = 0; + } else { + /* Uh-oh, the restart markers have been messed up. */ + /* Let the data source manager determine how to resync. */ + if (! (*cinfo->src->resync_to_restart) (cinfo, + cinfo->marker->next_restart_num)) + return FALSE; + } + + /* Update next-restart state */ + cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7; + + return TRUE; +} + + +/* + * This is the default resync_to_restart method for data source managers + * to use if they don't have any better approach. Some data source managers + * may be able to back up, or may have additional knowledge about the data + * which permits a more intelligent recovery strategy; such managers would + * presumably supply their own resync method. + * + * read_restart_marker calls resync_to_restart if it finds a marker other than + * the restart marker it was expecting. (This code is *not* used unless + * a nonzero restart interval has been declared.) cinfo->unread_marker is + * the marker code actually found (might be anything, except 0 or FF). + * The desired restart marker number (0..7) is passed as a parameter. + * This routine is supposed to apply whatever error recovery strategy seems + * appropriate in order to position the input stream to the next data segment. + * Note that cinfo->unread_marker is treated as a marker appearing before + * the current data-source input point; usually it should be reset to zero + * before returning. + * Returns FALSE if suspension is required. + * + * This implementation is substantially constrained by wanting to treat the + * input as a data stream; this means we can't back up. Therefore, we have + * only the following actions to work with: + * 1. Simply discard the marker and let the entropy decoder resume at next + * byte of file. + * 2. Read forward until we find another marker, discarding intervening + * data. (In theory we could look ahead within the current bufferload, + * without having to discard data if we don't find the desired marker. + * This idea is not implemented here, in part because it makes behavior + * dependent on buffer size and chance buffer-boundary positions.) + * 3. Leave the marker unread (by failing to zero cinfo->unread_marker). + * This will cause the entropy decoder to process an empty data segment, + * inserting dummy zeroes, and then we will reprocess the marker. + * + * #2 is appropriate if we think the desired marker lies ahead, while #3 is + * appropriate if the found marker is a future restart marker (indicating + * that we have missed the desired restart marker, probably because it got + * corrupted). + * We apply #2 or #3 if the found marker is a restart marker no more than + * two counts behind or ahead of the expected one. We also apply #2 if the + * found marker is not a legal JPEG marker code (it's certainly bogus data). + * If the found marker is a restart marker more than 2 counts away, we do #1 + * (too much risk that the marker is erroneous; with luck we will be able to + * resync at some future point). + * For any valid non-restart JPEG marker, we apply #3. This keeps us from + * overrunning the end of a scan. An implementation limited to single-scan + * files might find it better to apply #2 for markers other than EOI, since + * any other marker would have to be bogus data in that case. + */ + +GLOBAL(boolean) +jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired) +{ + int marker = cinfo->unread_marker; + int action = 1; + + /* Always put up a warning. */ + WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired); + + /* Outer loop handles repeated decision after scanning forward. */ + for (;;) { + if (marker < (int) M_SOF0) + action = 2; /* invalid marker */ + else if (marker < (int) M_RST0 || marker > (int) M_RST7) + action = 3; /* valid non-restart marker */ + else { + if (marker == ((int) M_RST0 + ((desired+1) & 7)) || + marker == ((int) M_RST0 + ((desired+2) & 7))) + action = 3; /* one of the next two expected restarts */ + else if (marker == ((int) M_RST0 + ((desired-1) & 7)) || + marker == ((int) M_RST0 + ((desired-2) & 7))) + action = 2; /* a prior restart, so advance */ + else + action = 1; /* desired restart or too far away */ + } + TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action); + switch (action) { + case 1: + /* Discard marker and let entropy decoder resume processing. */ + cinfo->unread_marker = 0; + return TRUE; + case 2: + /* Scan to the next marker, and repeat the decision loop. */ + if (! next_marker(cinfo)) + return FALSE; + marker = cinfo->unread_marker; + break; + case 3: + /* Return without advancing past this marker. */ + /* Entropy decoder will be forced to process an empty segment. */ + return TRUE; + } + } /* end loop */ +} + + +/* + * Reset marker processing state to begin a fresh datastream. + */ + +METHODDEF(void) +reset_marker_reader (j_decompress_ptr cinfo) +{ + my_marker_ptr marker = (my_marker_ptr) cinfo->marker; + + cinfo->comp_info = NULL; /* until allocated by get_sof */ + cinfo->input_scan_number = 0; /* no SOS seen yet */ + cinfo->unread_marker = 0; /* no pending marker */ + marker->pub.saw_SOI = FALSE; /* set internal state too */ + marker->pub.saw_SOF = FALSE; + marker->pub.discarded_bytes = 0; + marker->cur_marker = NULL; +} + + +/* + * Initialize the marker reader module. + * This is called only once, when the decompression object is created. + */ + +GLOBAL(void) +jinit_marker_reader (j_decompress_ptr cinfo) +{ + my_marker_ptr marker; + int i; + + /* Create subobject in permanent pool */ + marker = (my_marker_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, + SIZEOF(my_marker_reader)); + cinfo->marker = (struct jpeg_marker_reader *) marker; + /* Initialize public method pointers */ + marker->pub.reset_marker_reader = reset_marker_reader; + marker->pub.read_markers = read_markers; + marker->pub.read_restart_marker = read_restart_marker; + /* Initialize COM/APPn processing. + * By default, we examine and then discard APP0 and APP14, + * but simply discard COM and all other APPn. + */ + marker->process_COM = skip_variable; + marker->length_limit_COM = 0; + for (i = 0; i < 16; i++) { + marker->process_APPn[i] = skip_variable; + marker->length_limit_APPn[i] = 0; + } + marker->process_APPn[0] = get_interesting_appn; + marker->process_APPn[14] = get_interesting_appn; + /* Reset marker processing state */ + reset_marker_reader(cinfo); +} + + +/* + * Control saving of COM and APPn markers into marker_list. + */ + +#ifdef SAVE_MARKERS_SUPPORTED + +GLOBAL(void) +jpeg_save_markers (j_decompress_ptr cinfo, int marker_code, + unsigned int length_limit) +{ + my_marker_ptr marker = (my_marker_ptr) cinfo->marker; + long maxlength; + jpeg_marker_parser_method processor; + + /* Length limit mustn't be larger than what we can allocate + * (should only be a concern in a 16-bit environment). + */ + maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct); + if (((long) length_limit) > maxlength) + length_limit = (unsigned int) maxlength; + + /* Choose processor routine to use. + * APP0/APP14 have special requirements. + */ + if (length_limit) { + processor = save_marker; + /* If saving APP0/APP14, save at least enough for our internal use. */ + if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN) + length_limit = APP0_DATA_LEN; + else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN) + length_limit = APP14_DATA_LEN; + } else { + processor = skip_variable; + /* If discarding APP0/APP14, use our regular on-the-fly processor. */ + if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14) + processor = get_interesting_appn; + } + + if (marker_code == (int) M_COM) { + marker->process_COM = processor; + marker->length_limit_COM = length_limit; + } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) { + marker->process_APPn[marker_code - (int) M_APP0] = processor; + marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit; + } else + ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code); +} + +#endif /* SAVE_MARKERS_SUPPORTED */ + + +/* + * Install a special processing method for COM or APPn markers. + */ + +GLOBAL(void) +jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code, + jpeg_marker_parser_method routine) +{ + my_marker_ptr marker = (my_marker_ptr) cinfo->marker; + + if (marker_code == (int) M_COM) + marker->process_COM = routine; + else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) + marker->process_APPn[marker_code - (int) M_APP0] = routine; + else + ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code); +} diff --git a/crypto777/jpeg/jdmaster.c b/crypto777/jpeg/jdmaster.c new file mode 100644 index 000000000..fef72a21b --- /dev/null +++ b/crypto777/jpeg/jdmaster.c @@ -0,0 +1,531 @@ +/* + * jdmaster.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2002-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains master control logic for the JPEG decompressor. + * These routines are concerned with selecting the modules to be executed + * and with determining the number of passes and the work to be done in each + * pass. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Private state */ + +typedef struct { + struct jpeg_decomp_master pub; /* public fields */ + + int pass_number; /* # of passes completed */ + + boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */ + + /* Saved references to initialized quantizer modules, + * in case we need to switch modes. + */ + struct jpeg_color_quantizer * quantizer_1pass; + struct jpeg_color_quantizer * quantizer_2pass; +} my_decomp_master; + +typedef my_decomp_master * my_master_ptr; + + +/* + * Determine whether merged upsample/color conversion should be used. + * CRUCIAL: this must match the actual capabilities of jdmerge.c! + */ + +LOCAL(boolean) +use_merged_upsample (j_decompress_ptr cinfo) +{ +#ifdef UPSAMPLE_MERGING_SUPPORTED + /* Merging is the equivalent of plain box-filter upsampling */ + if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) + return FALSE; + /* jdmerge.c only supports YCC=>RGB color conversion */ + if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || + cinfo->out_color_space != JCS_RGB || + cinfo->out_color_components != RGB_PIXELSIZE) + return FALSE; + /* and it only handles 2h1v or 2h2v sampling ratios */ + if (cinfo->comp_info[0].h_samp_factor != 2 || + cinfo->comp_info[1].h_samp_factor != 1 || + cinfo->comp_info[2].h_samp_factor != 1 || + cinfo->comp_info[0].v_samp_factor > 2 || + cinfo->comp_info[1].v_samp_factor != 1 || + cinfo->comp_info[2].v_samp_factor != 1) + return FALSE; + /* furthermore, it doesn't work if we've scaled the IDCTs differently */ + if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size || + cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size || + cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size || + cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size || + cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size || + cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size) + return FALSE; + /* ??? also need to test for upsample-time rescaling, when & if supported */ + return TRUE; /* by golly, it'll work... */ +#else + return FALSE; +#endif +} + + +/* + * Compute output image dimensions and related values. + * NOTE: this is exported for possible use by application. + * Hence it mustn't do anything that can't be done twice. + * Also note that it may be called before the master module is initialized! + */ + +GLOBAL(void) +jpeg_calc_output_dimensions (j_decompress_ptr cinfo) +/* Do computations that are needed before master selection phase. + * This function is used for full decompression. + */ +{ +#ifdef IDCT_SCALING_SUPPORTED + int ci; + jpeg_component_info *compptr; +#endif + + /* Prevent application from calling me at wrong times */ + if (cinfo->global_state != DSTATE_READY) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + /* Compute core output image dimensions and DCT scaling choices. */ + jpeg_core_output_dimensions(cinfo); + +#ifdef IDCT_SCALING_SUPPORTED + + /* In selecting the actual DCT scaling for each component, we try to + * scale up the chroma components via IDCT scaling rather than upsampling. + * This saves time if the upsampler gets to use 1:1 scaling. + * Note this code adapts subsampling ratios which are powers of 2. + */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + int ssize = 1; + while (cinfo->min_DCT_h_scaled_size * ssize <= + (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) && + (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { + ssize = ssize * 2; + } + compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; + ssize = 1; + while (cinfo->min_DCT_v_scaled_size * ssize <= + (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) && + (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { + ssize = ssize * 2; + } + compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; + + /* We don't support IDCT ratios larger than 2. */ + if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) + compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; + else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) + compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; + } + + /* Recompute downsampled dimensions of components; + * application needs to know these if using raw downsampled data. + */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Size in samples, after IDCT scaling */ + compptr->downsampled_width = (JDIMENSION) + jdiv_round_up((long) cinfo->image_width * + (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), + (long) (cinfo->max_h_samp_factor * cinfo->block_size)); + compptr->downsampled_height = (JDIMENSION) + jdiv_round_up((long) cinfo->image_height * + (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), + (long) (cinfo->max_v_samp_factor * cinfo->block_size)); + } + +#endif /* IDCT_SCALING_SUPPORTED */ + + /* Report number of components in selected colorspace. */ + /* Probably this should be in the color conversion module... */ + switch (cinfo->out_color_space) { + case JCS_GRAYSCALE: + cinfo->out_color_components = 1; + break; + case JCS_RGB: + cinfo->out_color_components = RGB_PIXELSIZE; + break; + case JCS_YCbCr: + cinfo->out_color_components = 3; + break; + case JCS_CMYK: + case JCS_YCCK: + cinfo->out_color_components = 4; + break; + default: /* else must be same colorspace as in file */ + cinfo->out_color_components = cinfo->num_components; + break; + } + cinfo->output_components = (cinfo->quantize_colors ? 1 : + cinfo->out_color_components); + + /* See if upsampler will want to emit more than one row at a time */ + if (use_merged_upsample(cinfo)) + cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; + else + cinfo->rec_outbuf_height = 1; +} + + +/* + * Several decompression processes need to range-limit values to the range + * 0..MAXJSAMPLE; the input value may fall somewhat outside this range + * due to noise introduced by quantization, roundoff error, etc. These + * processes are inner loops and need to be as fast as possible. On most + * machines, particularly CPUs with pipelines or instruction prefetch, + * a (subscript-check-less) C table lookup + * x = sample_range_limit[x]; + * is faster than explicit tests + * if (x < 0) x = 0; + * else if (x > MAXJSAMPLE) x = MAXJSAMPLE; + * These processes all use a common table prepared by the routine below. + * + * For most steps we can mathematically guarantee that the initial value + * of x is within MAXJSAMPLE+1 of the legal range, so a table running from + * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial + * limiting step (just after the IDCT), a wildly out-of-range value is + * possible if the input data is corrupt. To avoid any chance of indexing + * off the end of memory and getting a bad-pointer trap, we perform the + * post-IDCT limiting thus: + * x = range_limit[x & MASK]; + * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit + * samples. Under normal circumstances this is more than enough range and + * a correct output will be generated; with bogus input data the mask will + * cause wraparound, and we will safely generate a bogus-but-in-range output. + * For the post-IDCT step, we want to convert the data from signed to unsigned + * representation by adding CENTERJSAMPLE at the same time that we limit it. + * So the post-IDCT limiting table ends up looking like this: + * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE, + * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), + * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), + * 0,1,...,CENTERJSAMPLE-1 + * Negative inputs select values from the upper half of the table after + * masking. + * + * We can save some space by overlapping the start of the post-IDCT table + * with the simpler range limiting table. The post-IDCT table begins at + * sample_range_limit + CENTERJSAMPLE. + * + * Note that the table is allocated in near data space on PCs; it's small + * enough and used often enough to justify this. + */ + +LOCAL(void) +prepare_range_limit_table (j_decompress_ptr cinfo) +/* Allocate and fill in the sample_range_limit table */ +{ + JSAMPLE * table; + int i; + + table = (JSAMPLE *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)); + table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */ + cinfo->sample_range_limit = table; + /* First segment of "simple" table: limit[x] = 0 for x < 0 */ + MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE)); + /* Main part of "simple" table: limit[x] = x */ + for (i = 0; i <= MAXJSAMPLE; i++) + table[i] = (JSAMPLE) i; + table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */ + /* End of simple table, rest of first half of post-IDCT table */ + for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++) + table[i] = MAXJSAMPLE; + /* Second half of post-IDCT table */ + MEMZERO(table + (2 * (MAXJSAMPLE+1)), + (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE)); + MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE), + cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE)); +} + + +/* + * Master selection of decompression modules. + * This is done once at jpeg_start_decompress time. We determine + * which modules will be used and give them appropriate initialization calls. + * We also initialize the decompressor input side to begin consuming data. + * + * Since jpeg_read_header has finished, we know what is in the SOF + * and (first) SOS markers. We also have all the application parameter + * settings. + */ + +LOCAL(void) +master_selection (j_decompress_ptr cinfo) +{ + my_master_ptr master = (my_master_ptr) cinfo->master; + boolean use_c_buffer; + long samplesperrow; + JDIMENSION jd_samplesperrow; + + /* Initialize dimensions and other stuff */ + jpeg_calc_output_dimensions(cinfo); + prepare_range_limit_table(cinfo); + + /* Width of an output scanline must be representable as JDIMENSION. */ + samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components; + jd_samplesperrow = (JDIMENSION) samplesperrow; + if ((long) jd_samplesperrow != samplesperrow) + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); + + /* Initialize my private state */ + master->pass_number = 0; + master->using_merged_upsample = use_merged_upsample(cinfo); + + /* Color quantizer selection */ + master->quantizer_1pass = NULL; + master->quantizer_2pass = NULL; + /* No mode changes if not using buffered-image mode. */ + if (! cinfo->quantize_colors || ! cinfo->buffered_image) { + cinfo->enable_1pass_quant = FALSE; + cinfo->enable_external_quant = FALSE; + cinfo->enable_2pass_quant = FALSE; + } + if (cinfo->quantize_colors) { + if (cinfo->raw_data_out) + ERREXIT(cinfo, JERR_NOTIMPL); + /* 2-pass quantizer only works in 3-component color space. */ + if (cinfo->out_color_components != 3) { + cinfo->enable_1pass_quant = TRUE; + cinfo->enable_external_quant = FALSE; + cinfo->enable_2pass_quant = FALSE; + cinfo->colormap = NULL; + } else if (cinfo->colormap != NULL) { + cinfo->enable_external_quant = TRUE; + } else if (cinfo->two_pass_quantize) { + cinfo->enable_2pass_quant = TRUE; + } else { + cinfo->enable_1pass_quant = TRUE; + } + + if (cinfo->enable_1pass_quant) { +#ifdef QUANT_1PASS_SUPPORTED + jinit_1pass_quantizer(cinfo); + master->quantizer_1pass = cinfo->cquantize; +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } + + /* We use the 2-pass code to map to external colormaps. */ + if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { +#ifdef QUANT_2PASS_SUPPORTED + jinit_2pass_quantizer(cinfo); + master->quantizer_2pass = cinfo->cquantize; +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } + /* If both quantizers are initialized, the 2-pass one is left active; + * this is necessary for starting with quantization to an external map. + */ + } + + /* Post-processing: in particular, color conversion first */ + if (! cinfo->raw_data_out) { + if (master->using_merged_upsample) { +#ifdef UPSAMPLE_MERGING_SUPPORTED + jinit_merged_upsampler(cinfo); /* does color conversion too */ +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif + } else { + jinit_color_deconverter(cinfo); + jinit_upsampler(cinfo); + } + jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant); + } + /* Inverse DCT */ + jinit_inverse_dct(cinfo); + /* Entropy decoding: either Huffman or arithmetic coding. */ + if (cinfo->arith_code) + jinit_arith_decoder(cinfo); + else { + jinit_huff_decoder(cinfo); + } + + /* Initialize principal buffer controllers. */ + use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image; + jinit_d_coef_controller(cinfo, use_c_buffer); + + if (! cinfo->raw_data_out) + jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */); + + /* We can now tell the memory manager to allocate virtual arrays. */ + (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); + + /* Initialize input side of decompressor to consume first scan. */ + (*cinfo->inputctl->start_input_pass) (cinfo); + +#ifdef D_MULTISCAN_FILES_SUPPORTED + /* If jpeg_start_decompress will read the whole file, initialize + * progress monitoring appropriately. The input step is counted + * as one pass. + */ + if (cinfo->progress != NULL && ! cinfo->buffered_image && + cinfo->inputctl->has_multiple_scans) { + int nscans; + /* Estimate number of scans to set pass_limit. */ + if (cinfo->progressive_mode) { + /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ + nscans = 2 + 3 * cinfo->num_components; + } else { + /* For a nonprogressive multiscan file, estimate 1 scan per component. */ + nscans = cinfo->num_components; + } + cinfo->progress->pass_counter = 0L; + cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; + cinfo->progress->completed_passes = 0; + cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2); + /* Count the input pass as done */ + master->pass_number++; + } +#endif /* D_MULTISCAN_FILES_SUPPORTED */ +} + + +/* + * Per-pass setup. + * This is called at the beginning of each output pass. We determine which + * modules will be active during this pass and give them appropriate + * start_pass calls. We also set is_dummy_pass to indicate whether this + * is a "real" output pass or a dummy pass for color quantization. + * (In the latter case, jdapistd.c will crank the pass to completion.) + */ + +METHODDEF(void) +prepare_for_output_pass (j_decompress_ptr cinfo) +{ + my_master_ptr master = (my_master_ptr) cinfo->master; + + if (master->pub.is_dummy_pass) { +#ifdef QUANT_2PASS_SUPPORTED + /* Final pass of 2-pass quantization */ + master->pub.is_dummy_pass = FALSE; + (*cinfo->cquantize->start_pass) (cinfo, FALSE); + (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST); + (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST); +#else + ERREXIT(cinfo, JERR_NOT_COMPILED); +#endif /* QUANT_2PASS_SUPPORTED */ + } else { + if (cinfo->quantize_colors && cinfo->colormap == NULL) { + /* Select new quantization method */ + if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { + cinfo->cquantize = master->quantizer_2pass; + master->pub.is_dummy_pass = TRUE; + } else if (cinfo->enable_1pass_quant) { + cinfo->cquantize = master->quantizer_1pass; + } else { + ERREXIT(cinfo, JERR_MODE_CHANGE); + } + } + (*cinfo->idct->start_pass) (cinfo); + (*cinfo->coef->start_output_pass) (cinfo); + if (! cinfo->raw_data_out) { + if (! master->using_merged_upsample) + (*cinfo->cconvert->start_pass) (cinfo); + (*cinfo->upsample->start_pass) (cinfo); + if (cinfo->quantize_colors) + (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass); + (*cinfo->post->start_pass) (cinfo, + (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); + (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); + } + } + + /* Set up progress monitor's pass info if present */ + if (cinfo->progress != NULL) { + cinfo->progress->completed_passes = master->pass_number; + cinfo->progress->total_passes = master->pass_number + + (master->pub.is_dummy_pass ? 2 : 1); + /* In buffered-image mode, we assume one more output pass if EOI not + * yet reached, but no more passes if EOI has been reached. + */ + if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) { + cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1); + } + } +} + + +/* + * Finish up at end of an output pass. + */ + +METHODDEF(void) +finish_output_pass (j_decompress_ptr cinfo) +{ + my_master_ptr master = (my_master_ptr) cinfo->master; + + if (cinfo->quantize_colors) + (*cinfo->cquantize->finish_pass) (cinfo); + master->pass_number++; +} + + +#ifdef D_MULTISCAN_FILES_SUPPORTED + +/* + * Switch to a new external colormap between output passes. + */ + +GLOBAL(void) +jpeg_new_colormap (j_decompress_ptr cinfo) +{ + my_master_ptr master = (my_master_ptr) cinfo->master; + + /* Prevent application from calling me at wrong times */ + if (cinfo->global_state != DSTATE_BUFIMAGE) + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + + if (cinfo->quantize_colors && cinfo->enable_external_quant && + cinfo->colormap != NULL) { + /* Select 2-pass quantizer for external colormap use */ + cinfo->cquantize = master->quantizer_2pass; + /* Notify quantizer of colormap change */ + (*cinfo->cquantize->new_color_map) (cinfo); + master->pub.is_dummy_pass = FALSE; /* just in case */ + } else + ERREXIT(cinfo, JERR_MODE_CHANGE); +} + +#endif /* D_MULTISCAN_FILES_SUPPORTED */ + + +/* + * Initialize master decompression control and select active modules. + * This is performed at the start of jpeg_start_decompress. + */ + +GLOBAL(void) +jinit_master_decompress (j_decompress_ptr cinfo) +{ + my_master_ptr master; + + master = (my_master_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_decomp_master)); + cinfo->master = (struct jpeg_decomp_master *) master; + master->pub.prepare_for_output_pass = prepare_for_output_pass; + master->pub.finish_output_pass = finish_output_pass; + + master->pub.is_dummy_pass = FALSE; + + master_selection(cinfo); +} diff --git a/crypto777/jpeg/jdmerge.c b/crypto777/jpeg/jdmerge.c new file mode 100644 index 000000000..37444468c --- /dev/null +++ b/crypto777/jpeg/jdmerge.c @@ -0,0 +1,400 @@ +/* + * jdmerge.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains code for merged upsampling/color conversion. + * + * This file combines functions from jdsample.c and jdcolor.c; + * read those files first to understand what's going on. + * + * When the chroma components are to be upsampled by simple replication + * (ie, box filtering), we can save some work in color conversion by + * calculating all the output pixels corresponding to a pair of chroma + * samples at one time. In the conversion equations + * R = Y + K1 * Cr + * G = Y + K2 * Cb + K3 * Cr + * B = Y + K4 * Cb + * only the Y term varies among the group of pixels corresponding to a pair + * of chroma samples, so the rest of the terms can be calculated just once. + * At typical sampling ratios, this eliminates half or three-quarters of the + * multiplications needed for color conversion. + * + * This file currently provides implementations for the following cases: + * YCbCr => RGB color conversion only. + * Sampling ratios of 2h1v or 2h2v. + * No scaling needed at upsample time. + * Corner-aligned (non-CCIR601) sampling alignment. + * Other special cases could be added, but in most applications these are + * the only common cases. (For uncommon cases we fall back on the more + * general code in jdsample.c and jdcolor.c.) + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + +#ifdef UPSAMPLE_MERGING_SUPPORTED + + +/* Private subobject */ + +typedef struct { + struct jpeg_upsampler pub; /* public fields */ + + /* Pointer to routine to do actual upsampling/conversion of one row group */ + JMETHOD(void, upmethod, (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, + JSAMPARRAY output_buf)); + + /* Private state for YCC->RGB conversion */ + int * Cr_r_tab; /* => table for Cr to R conversion */ + int * Cb_b_tab; /* => table for Cb to B conversion */ + INT32 * Cr_g_tab; /* => table for Cr to G conversion */ + INT32 * Cb_g_tab; /* => table for Cb to G conversion */ + + /* For 2:1 vertical sampling, we produce two output rows at a time. + * We need a "spare" row buffer to hold the second output row if the + * application provides just a one-row buffer; we also use the spare + * to discard the dummy last row if the image height is odd. + */ + JSAMPROW spare_row; + boolean spare_full; /* T if spare buffer is occupied */ + + JDIMENSION out_row_width; /* samples per output row */ + JDIMENSION rows_to_go; /* counts rows remaining in image */ +} my_upsampler; + +typedef my_upsampler * my_upsample_ptr; + +#define SCALEBITS 16 /* speediest right-shift on some machines */ +#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) +#define FIX(x) ((INT32) ((x) * (1L<RGB colorspace conversion. + * This is taken directly from jdcolor.c; see that file for more info. + */ + +LOCAL(void) +build_ycc_rgb_table (j_decompress_ptr cinfo) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + int i; + INT32 x; + SHIFT_TEMPS + + upsample->Cr_r_tab = (int *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(int)); + upsample->Cb_b_tab = (int *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(int)); + upsample->Cr_g_tab = (INT32 *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(INT32)); + upsample->Cb_g_tab = (INT32 *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (MAXJSAMPLE+1) * SIZEOF(INT32)); + + for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { + /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ + /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ + /* Cr=>R value is nearest int to 1.40200 * x */ + upsample->Cr_r_tab[i] = (int) + RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); + /* Cb=>B value is nearest int to 1.77200 * x */ + upsample->Cb_b_tab[i] = (int) + RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); + /* Cr=>G value is scaled-up -0.71414 * x */ + upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; + /* Cb=>G value is scaled-up -0.34414 * x */ + /* We also add in ONE_HALF so that need not do it in inner loop */ + upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; + } +} + + +/* + * Initialize for an upsampling pass. + */ + +METHODDEF(void) +start_pass_merged_upsample (j_decompress_ptr cinfo) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + + /* Mark the spare buffer empty */ + upsample->spare_full = FALSE; + /* Initialize total-height counter for detecting bottom of image */ + upsample->rows_to_go = cinfo->output_height; +} + + +/* + * Control routine to do upsampling (and color conversion). + * + * The control routine just handles the row buffering considerations. + */ + +METHODDEF(void) +merged_2v_upsample (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +/* 2:1 vertical sampling case: may need a spare row. */ +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + JSAMPROW work_ptrs[2]; + JDIMENSION num_rows; /* number of rows returned to caller */ + + if (upsample->spare_full) { + /* If we have a spare row saved from a previous cycle, just return it. */ + jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, + 1, upsample->out_row_width); + num_rows = 1; + upsample->spare_full = FALSE; + } else { + /* Figure number of rows to return to caller. */ + num_rows = 2; + /* Not more than the distance to the end of the image. */ + if (num_rows > upsample->rows_to_go) + num_rows = upsample->rows_to_go; + /* And not more than what the client can accept: */ + out_rows_avail -= *out_row_ctr; + if (num_rows > out_rows_avail) + num_rows = out_rows_avail; + /* Create output pointer array for upsampler. */ + work_ptrs[0] = output_buf[*out_row_ctr]; + if (num_rows > 1) { + work_ptrs[1] = output_buf[*out_row_ctr + 1]; + } else { + work_ptrs[1] = upsample->spare_row; + upsample->spare_full = TRUE; + } + /* Now do the upsampling. */ + (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); + } + + /* Adjust counts */ + *out_row_ctr += num_rows; + upsample->rows_to_go -= num_rows; + /* When the buffer is emptied, declare this input row group consumed */ + if (! upsample->spare_full) + (*in_row_group_ctr)++; +} + + +METHODDEF(void) +merged_1v_upsample (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +/* 1:1 vertical sampling case: much easier, never need a spare row. */ +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + + /* Just do the upsampling. */ + (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, + output_buf + *out_row_ctr); + /* Adjust counts */ + (*out_row_ctr)++; + (*in_row_group_ctr)++; +} + + +/* + * These are the routines invoked by the control routines to do + * the actual upsampling/conversion. One row group is processed per call. + * + * Note: since we may be writing directly into application-supplied buffers, + * we have to be honest about the output width; we can't assume the buffer + * has been rounded up to an even width. + */ + + +/* + * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. + */ + +METHODDEF(void) +h2v1_merged_upsample (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, + JSAMPARRAY output_buf) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + register int y, cred, cgreen, cblue; + int cb, cr; + register JSAMPROW outptr; + JSAMPROW inptr0, inptr1, inptr2; + JDIMENSION col; + /* copy these pointers into registers if possible */ + register JSAMPLE * range_limit = cinfo->sample_range_limit; + int * Crrtab = upsample->Cr_r_tab; + int * Cbbtab = upsample->Cb_b_tab; + INT32 * Crgtab = upsample->Cr_g_tab; + INT32 * Cbgtab = upsample->Cb_g_tab; + SHIFT_TEMPS + + inptr0 = input_buf[0][in_row_group_ctr]; + inptr1 = input_buf[1][in_row_group_ctr]; + inptr2 = input_buf[2][in_row_group_ctr]; + outptr = output_buf[0]; + /* Loop for each pair of output pixels */ + for (col = cinfo->output_width >> 1; col > 0; col--) { + /* Do the chroma part of the calculation */ + cb = GETJSAMPLE(*inptr1++); + cr = GETJSAMPLE(*inptr2++); + cred = Crrtab[cr]; + cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); + cblue = Cbbtab[cb]; + /* Fetch 2 Y values and emit 2 pixels */ + y = GETJSAMPLE(*inptr0++); + outptr[RGB_RED] = range_limit[y + cred]; + outptr[RGB_GREEN] = range_limit[y + cgreen]; + outptr[RGB_BLUE] = range_limit[y + cblue]; + outptr += RGB_PIXELSIZE; + y = GETJSAMPLE(*inptr0++); + outptr[RGB_RED] = range_limit[y + cred]; + outptr[RGB_GREEN] = range_limit[y + cgreen]; + outptr[RGB_BLUE] = range_limit[y + cblue]; + outptr += RGB_PIXELSIZE; + } + /* If image width is odd, do the last output column separately */ + if (cinfo->output_width & 1) { + cb = GETJSAMPLE(*inptr1); + cr = GETJSAMPLE(*inptr2); + cred = Crrtab[cr]; + cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); + cblue = Cbbtab[cb]; + y = GETJSAMPLE(*inptr0); + outptr[RGB_RED] = range_limit[y + cred]; + outptr[RGB_GREEN] = range_limit[y + cgreen]; + outptr[RGB_BLUE] = range_limit[y + cblue]; + } +} + + +/* + * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. + */ + +METHODDEF(void) +h2v2_merged_upsample (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, + JSAMPARRAY output_buf) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + register int y, cred, cgreen, cblue; + int cb, cr; + register JSAMPROW outptr0, outptr1; + JSAMPROW inptr00, inptr01, inptr1, inptr2; + JDIMENSION col; + /* copy these pointers into registers if possible */ + register JSAMPLE * range_limit = cinfo->sample_range_limit; + int * Crrtab = upsample->Cr_r_tab; + int * Cbbtab = upsample->Cb_b_tab; + INT32 * Crgtab = upsample->Cr_g_tab; + INT32 * Cbgtab = upsample->Cb_g_tab; + SHIFT_TEMPS + + inptr00 = input_buf[0][in_row_group_ctr*2]; + inptr01 = input_buf[0][in_row_group_ctr*2 + 1]; + inptr1 = input_buf[1][in_row_group_ctr]; + inptr2 = input_buf[2][in_row_group_ctr]; + outptr0 = output_buf[0]; + outptr1 = output_buf[1]; + /* Loop for each group of output pixels */ + for (col = cinfo->output_width >> 1; col > 0; col--) { + /* Do the chroma part of the calculation */ + cb = GETJSAMPLE(*inptr1++); + cr = GETJSAMPLE(*inptr2++); + cred = Crrtab[cr]; + cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); + cblue = Cbbtab[cb]; + /* Fetch 4 Y values and emit 4 pixels */ + y = GETJSAMPLE(*inptr00++); + outptr0[RGB_RED] = range_limit[y + cred]; + outptr0[RGB_GREEN] = range_limit[y + cgreen]; + outptr0[RGB_BLUE] = range_limit[y + cblue]; + outptr0 += RGB_PIXELSIZE; + y = GETJSAMPLE(*inptr00++); + outptr0[RGB_RED] = range_limit[y + cred]; + outptr0[RGB_GREEN] = range_limit[y + cgreen]; + outptr0[RGB_BLUE] = range_limit[y + cblue]; + outptr0 += RGB_PIXELSIZE; + y = GETJSAMPLE(*inptr01++); + outptr1[RGB_RED] = range_limit[y + cred]; + outptr1[RGB_GREEN] = range_limit[y + cgreen]; + outptr1[RGB_BLUE] = range_limit[y + cblue]; + outptr1 += RGB_PIXELSIZE; + y = GETJSAMPLE(*inptr01++); + outptr1[RGB_RED] = range_limit[y + cred]; + outptr1[RGB_GREEN] = range_limit[y + cgreen]; + outptr1[RGB_BLUE] = range_limit[y + cblue]; + outptr1 += RGB_PIXELSIZE; + } + /* If image width is odd, do the last output column separately */ + if (cinfo->output_width & 1) { + cb = GETJSAMPLE(*inptr1); + cr = GETJSAMPLE(*inptr2); + cred = Crrtab[cr]; + cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); + cblue = Cbbtab[cb]; + y = GETJSAMPLE(*inptr00); + outptr0[RGB_RED] = range_limit[y + cred]; + outptr0[RGB_GREEN] = range_limit[y + cgreen]; + outptr0[RGB_BLUE] = range_limit[y + cblue]; + y = GETJSAMPLE(*inptr01); + outptr1[RGB_RED] = range_limit[y + cred]; + outptr1[RGB_GREEN] = range_limit[y + cgreen]; + outptr1[RGB_BLUE] = range_limit[y + cblue]; + } +} + + +/* + * Module initialization routine for merged upsampling/color conversion. + * + * NB: this is called under the conditions determined by use_merged_upsample() + * in jdmaster.c. That routine MUST correspond to the actual capabilities + * of this module; no safety checks are made here. + */ + +GLOBAL(void) +jinit_merged_upsampler (j_decompress_ptr cinfo) +{ + my_upsample_ptr upsample; + + upsample = (my_upsample_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_upsampler)); + cinfo->upsample = (struct jpeg_upsampler *) upsample; + upsample->pub.start_pass = start_pass_merged_upsample; + upsample->pub.need_context_rows = FALSE; + + upsample->out_row_width = cinfo->output_width * cinfo->out_color_components; + + if (cinfo->max_v_samp_factor == 2) { + upsample->pub.upsample = merged_2v_upsample; + upsample->upmethod = h2v2_merged_upsample; + /* Allocate a spare row buffer */ + upsample->spare_row = (JSAMPROW) + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); + } else { + upsample->pub.upsample = merged_1v_upsample; + upsample->upmethod = h2v1_merged_upsample; + /* No spare row needed */ + upsample->spare_row = NULL; + } + + build_ycc_rgb_table(cinfo); +} + +#endif /* UPSAMPLE_MERGING_SUPPORTED */ diff --git a/crypto777/jpeg/jdpostct.c b/crypto777/jpeg/jdpostct.c new file mode 100644 index 000000000..571563d72 --- /dev/null +++ b/crypto777/jpeg/jdpostct.c @@ -0,0 +1,290 @@ +/* + * jdpostct.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the decompression postprocessing controller. + * This controller manages the upsampling, color conversion, and color + * quantization/reduction steps; specifically, it controls the buffering + * between upsample/color conversion and color quantization/reduction. + * + * If no color quantization/reduction is required, then this module has no + * work to do, and it just hands off to the upsample/color conversion code. + * An integrated upsample/convert/quantize process would replace this module + * entirely. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Private buffer controller object */ + +typedef struct { + struct jpeg_d_post_controller pub; /* public fields */ + + /* Color quantization source buffer: this holds output data from + * the upsample/color conversion step to be passed to the quantizer. + * For two-pass color quantization, we need a full-image buffer; + * for one-pass operation, a strip buffer is sufficient. + */ + jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */ + JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */ + JDIMENSION strip_height; /* buffer size in rows */ + /* for two-pass mode only: */ + JDIMENSION starting_row; /* row # of first row in current strip */ + JDIMENSION next_row; /* index of next row to fill/empty in strip */ +} my_post_controller; + +typedef my_post_controller * my_post_ptr; + + +/* Forward declarations */ +METHODDEF(void) post_process_1pass + JPP((j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail)); +#ifdef QUANT_2PASS_SUPPORTED +METHODDEF(void) post_process_prepass + JPP((j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail)); +METHODDEF(void) post_process_2pass + JPP((j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail)); +#endif + + +/* + * Initialize for a processing pass. + */ + +METHODDEF(void) +start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) +{ + my_post_ptr post = (my_post_ptr) cinfo->post; + + switch (pass_mode) { + case JBUF_PASS_THRU: + if (cinfo->quantize_colors) { + /* Single-pass processing with color quantization. */ + post->pub.post_process_data = post_process_1pass; + /* We could be doing buffered-image output before starting a 2-pass + * color quantization; in that case, jinit_d_post_controller did not + * allocate a strip buffer. Use the virtual-array buffer as workspace. + */ + if (post->buffer == NULL) { + post->buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, post->whole_image, + (JDIMENSION) 0, post->strip_height, TRUE); + } + } else { + /* For single-pass processing without color quantization, + * I have no work to do; just call the upsampler directly. + */ + post->pub.post_process_data = cinfo->upsample->upsample; + } + break; +#ifdef QUANT_2PASS_SUPPORTED + case JBUF_SAVE_AND_PASS: + /* First pass of 2-pass quantization */ + if (post->whole_image == NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + post->pub.post_process_data = post_process_prepass; + break; + case JBUF_CRANK_DEST: + /* Second pass of 2-pass quantization */ + if (post->whole_image == NULL) + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + post->pub.post_process_data = post_process_2pass; + break; +#endif /* QUANT_2PASS_SUPPORTED */ + default: + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); + break; + } + post->starting_row = post->next_row = 0; +} + + +/* + * Process some data in the one-pass (strip buffer) case. + * This is used for color precision reduction as well as one-pass quantization. + */ + +METHODDEF(void) +post_process_1pass (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + my_post_ptr post = (my_post_ptr) cinfo->post; + JDIMENSION num_rows, max_rows; + + /* Fill the buffer, but not more than what we can dump out in one go. */ + /* Note we rely on the upsampler to detect bottom of image. */ + max_rows = out_rows_avail - *out_row_ctr; + if (max_rows > post->strip_height) + max_rows = post->strip_height; + num_rows = 0; + (*cinfo->upsample->upsample) (cinfo, + input_buf, in_row_group_ctr, in_row_groups_avail, + post->buffer, &num_rows, max_rows); + /* Quantize and emit data. */ + (*cinfo->cquantize->color_quantize) (cinfo, + post->buffer, output_buf + *out_row_ctr, (int) num_rows); + *out_row_ctr += num_rows; +} + + +#ifdef QUANT_2PASS_SUPPORTED + +/* + * Process some data in the first pass of 2-pass quantization. + */ + +METHODDEF(void) +post_process_prepass (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + my_post_ptr post = (my_post_ptr) cinfo->post; + JDIMENSION old_next_row, num_rows; + + /* Reposition virtual buffer if at start of strip. */ + if (post->next_row == 0) { + post->buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, post->whole_image, + post->starting_row, post->strip_height, TRUE); + } + + /* Upsample some data (up to a strip height's worth). */ + old_next_row = post->next_row; + (*cinfo->upsample->upsample) (cinfo, + input_buf, in_row_group_ctr, in_row_groups_avail, + post->buffer, &post->next_row, post->strip_height); + + /* Allow quantizer to scan new data. No data is emitted, */ + /* but we advance out_row_ctr so outer loop can tell when we're done. */ + if (post->next_row > old_next_row) { + num_rows = post->next_row - old_next_row; + (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row, + (JSAMPARRAY) NULL, (int) num_rows); + *out_row_ctr += num_rows; + } + + /* Advance if we filled the strip. */ + if (post->next_row >= post->strip_height) { + post->starting_row += post->strip_height; + post->next_row = 0; + } +} + + +/* + * Process some data in the second pass of 2-pass quantization. + */ + +METHODDEF(void) +post_process_2pass (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + my_post_ptr post = (my_post_ptr) cinfo->post; + JDIMENSION num_rows, max_rows; + + /* Reposition virtual buffer if at start of strip. */ + if (post->next_row == 0) { + post->buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, post->whole_image, + post->starting_row, post->strip_height, FALSE); + } + + /* Determine number of rows to emit. */ + num_rows = post->strip_height - post->next_row; /* available in strip */ + max_rows = out_rows_avail - *out_row_ctr; /* available in output area */ + if (num_rows > max_rows) + num_rows = max_rows; + /* We have to check bottom of image here, can't depend on upsampler. */ + max_rows = cinfo->output_height - post->starting_row; + if (num_rows > max_rows) + num_rows = max_rows; + + /* Quantize and emit data. */ + (*cinfo->cquantize->color_quantize) (cinfo, + post->buffer + post->next_row, output_buf + *out_row_ctr, + (int) num_rows); + *out_row_ctr += num_rows; + + /* Advance if we filled the strip. */ + post->next_row += num_rows; + if (post->next_row >= post->strip_height) { + post->starting_row += post->strip_height; + post->next_row = 0; + } +} + +#endif /* QUANT_2PASS_SUPPORTED */ + + +/* + * Initialize postprocessing controller. + */ + +GLOBAL(void) +jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer) +{ + my_post_ptr post; + + post = (my_post_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_post_controller)); + cinfo->post = (struct jpeg_d_post_controller *) post; + post->pub.start_pass = start_pass_dpost; + post->whole_image = NULL; /* flag for no virtual arrays */ + post->buffer = NULL; /* flag for no strip buffer */ + + /* Create the quantization buffer, if needed */ + if (cinfo->quantize_colors) { + /* The buffer strip height is max_v_samp_factor, which is typically + * an efficient number of rows for upsampling to return. + * (In the presence of output rescaling, we might want to be smarter?) + */ + post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor; + if (need_full_buffer) { + /* Two-pass color quantization: need full-image storage. */ + /* We round up the number of rows to a multiple of the strip height. */ +#ifdef QUANT_2PASS_SUPPORTED + post->whole_image = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + cinfo->output_width * cinfo->out_color_components, + (JDIMENSION) jround_up((long) cinfo->output_height, + (long) post->strip_height), + post->strip_height); +#else + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); +#endif /* QUANT_2PASS_SUPPORTED */ + } else { + /* One-pass color quantization: just make a strip buffer. */ + post->buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->output_width * cinfo->out_color_components, + post->strip_height); + } + } +} diff --git a/crypto777/jpeg/jdsample.c b/crypto777/jpeg/jdsample.c new file mode 100644 index 000000000..7bc8885b0 --- /dev/null +++ b/crypto777/jpeg/jdsample.c @@ -0,0 +1,361 @@ +/* + * jdsample.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modified 2002-2008 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains upsampling routines. + * + * Upsampling input data is counted in "row groups". A row group + * is defined to be (v_samp_factor * DCT_v_scaled_size / min_DCT_v_scaled_size) + * sample rows of each component. Upsampling will normally produce + * max_v_samp_factor pixel rows from each row group (but this could vary + * if the upsampler is applying a scale factor of its own). + * + * An excellent reference for image resampling is + * Digital Image Warping, George Wolberg, 1990. + * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Pointer to routine to upsample a single component */ +typedef JMETHOD(void, upsample1_ptr, + (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); + +/* Private subobject */ + +typedef struct { + struct jpeg_upsampler pub; /* public fields */ + + /* Color conversion buffer. When using separate upsampling and color + * conversion steps, this buffer holds one upsampled row group until it + * has been color converted and output. + * Note: we do not allocate any storage for component(s) which are full-size, + * ie do not need rescaling. The corresponding entry of color_buf[] is + * simply set to point to the input data array, thereby avoiding copying. + */ + JSAMPARRAY color_buf[MAX_COMPONENTS]; + + /* Per-component upsampling method pointers */ + upsample1_ptr methods[MAX_COMPONENTS]; + + int next_row_out; /* counts rows emitted from color_buf */ + JDIMENSION rows_to_go; /* counts rows remaining in image */ + + /* Height of an input row group for each component. */ + int rowgroup_height[MAX_COMPONENTS]; + + /* These arrays save pixel expansion factors so that int_expand need not + * recompute them each time. They are unused for other upsampling methods. + */ + UINT8 h_expand[MAX_COMPONENTS]; + UINT8 v_expand[MAX_COMPONENTS]; +} my_upsampler; + +typedef my_upsampler * my_upsample_ptr; + + +/* + * Initialize for an upsampling pass. + */ + +METHODDEF(void) +start_pass_upsample (j_decompress_ptr cinfo) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + + /* Mark the conversion buffer empty */ + upsample->next_row_out = cinfo->max_v_samp_factor; + /* Initialize total-height counter for detecting bottom of image */ + upsample->rows_to_go = cinfo->output_height; +} + + +/* + * Control routine to do upsampling (and color conversion). + * + * In this version we upsample each component independently. + * We upsample one row group into the conversion buffer, then apply + * color conversion a row at a time. + */ + +METHODDEF(void) +sep_upsample (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + int ci; + jpeg_component_info * compptr; + JDIMENSION num_rows; + + /* Fill the conversion buffer, if it's empty */ + if (upsample->next_row_out >= cinfo->max_v_samp_factor) { + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Invoke per-component upsample method. Notice we pass a POINTER + * to color_buf[ci], so that fullsize_upsample can change it. + */ + (*upsample->methods[ci]) (cinfo, compptr, + input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]), + upsample->color_buf + ci); + } + upsample->next_row_out = 0; + } + + /* Color-convert and emit rows */ + + /* How many we have in the buffer: */ + num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out); + /* Not more than the distance to the end of the image. Need this test + * in case the image height is not a multiple of max_v_samp_factor: + */ + if (num_rows > upsample->rows_to_go) + num_rows = upsample->rows_to_go; + /* And not more than what the client can accept: */ + out_rows_avail -= *out_row_ctr; + if (num_rows > out_rows_avail) + num_rows = out_rows_avail; + + (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf, + (JDIMENSION) upsample->next_row_out, + output_buf + *out_row_ctr, + (int) num_rows); + + /* Adjust counts */ + *out_row_ctr += num_rows; + upsample->rows_to_go -= num_rows; + upsample->next_row_out += num_rows; + /* When the buffer is emptied, declare this input row group consumed */ + if (upsample->next_row_out >= cinfo->max_v_samp_factor) + (*in_row_group_ctr)++; +} + + +/* + * These are the routines invoked by sep_upsample to upsample pixel values + * of a single component. One row group is processed per call. + */ + + +/* + * For full-size components, we just make color_buf[ci] point at the + * input buffer, and thus avoid copying any data. Note that this is + * safe only because sep_upsample doesn't declare the input row group + * "consumed" until we are done color converting and emitting it. + */ + +METHODDEF(void) +fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) +{ + *output_data_ptr = input_data; +} + + +/* + * This is a no-op version used for "uninteresting" components. + * These components will not be referenced by color conversion. + */ + +METHODDEF(void) +noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) +{ + *output_data_ptr = NULL; /* safety check */ +} + + +/* + * This version handles any integral sampling ratios. + * This is not used for typical JPEG files, so it need not be fast. + * Nor, for that matter, is it particularly accurate: the algorithm is + * simple replication of the input pixel onto the corresponding output + * pixels. The hi-falutin sampling literature refers to this as a + * "box filter". A box filter tends to introduce visible artifacts, + * so if you are actually going to use 3:1 or 4:1 sampling ratios + * you would be well advised to improve this code. + */ + +METHODDEF(void) +int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) +{ + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; + JSAMPARRAY output_data = *output_data_ptr; + register JSAMPROW inptr, outptr; + register JSAMPLE invalue; + register int h; + JSAMPROW outend; + int h_expand, v_expand; + int inrow, outrow; + + h_expand = upsample->h_expand[compptr->component_index]; + v_expand = upsample->v_expand[compptr->component_index]; + + inrow = outrow = 0; + while (outrow < cinfo->max_v_samp_factor) { + /* Generate one output row with proper horizontal expansion */ + inptr = input_data[inrow]; + outptr = output_data[outrow]; + outend = outptr + cinfo->output_width; + while (outptr < outend) { + invalue = *inptr++; /* don't need GETJSAMPLE() here */ + for (h = h_expand; h > 0; h--) { + *outptr++ = invalue; + } + } + /* Generate any additional output rows by duplicating the first one */ + if (v_expand > 1) { + jcopy_sample_rows(output_data, outrow, output_data, outrow+1, + v_expand-1, cinfo->output_width); + } + inrow++; + outrow += v_expand; + } +} + + +/* + * Fast processing for the common case of 2:1 horizontal and 1:1 vertical. + * It's still a box filter. + */ + +METHODDEF(void) +h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) +{ + JSAMPARRAY output_data = *output_data_ptr; + register JSAMPROW inptr, outptr; + register JSAMPLE invalue; + JSAMPROW outend; + int outrow; + + for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) { + inptr = input_data[outrow]; + outptr = output_data[outrow]; + outend = outptr + cinfo->output_width; + while (outptr < outend) { + invalue = *inptr++; /* don't need GETJSAMPLE() here */ + *outptr++ = invalue; + *outptr++ = invalue; + } + } +} + + +/* + * Fast processing for the common case of 2:1 horizontal and 2:1 vertical. + * It's still a box filter. + */ + +METHODDEF(void) +h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) +{ + JSAMPARRAY output_data = *output_data_ptr; + register JSAMPROW inptr, outptr; + register JSAMPLE invalue; + JSAMPROW outend; + int inrow, outrow; + + inrow = outrow = 0; + while (outrow < cinfo->max_v_samp_factor) { + inptr = input_data[inrow]; + outptr = output_data[outrow]; + outend = outptr + cinfo->output_width; + while (outptr < outend) { + invalue = *inptr++; /* don't need GETJSAMPLE() here */ + *outptr++ = invalue; + *outptr++ = invalue; + } + jcopy_sample_rows(output_data, outrow, output_data, outrow+1, + 1, cinfo->output_width); + inrow++; + outrow += 2; + } +} + + +/* + * Module initialization routine for upsampling. + */ + +GLOBAL(void) +jinit_upsampler (j_decompress_ptr cinfo) +{ + my_upsample_ptr upsample; + int ci; + jpeg_component_info * compptr; + boolean need_buffer; + int h_in_group, v_in_group, h_out_group, v_out_group; + + upsample = (my_upsample_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_upsampler)); + cinfo->upsample = (struct jpeg_upsampler *) upsample; + upsample->pub.start_pass = start_pass_upsample; + upsample->pub.upsample = sep_upsample; + upsample->pub.need_context_rows = FALSE; /* until we find out differently */ + + if (cinfo->CCIR601_sampling) /* this isn't supported */ + ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); + + /* Verify we can handle the sampling factors, select per-component methods, + * and create storage as needed. + */ + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) { + /* Compute size of an "input group" after IDCT scaling. This many samples + * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. + */ + h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) / + cinfo->min_DCT_h_scaled_size; + v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / + cinfo->min_DCT_v_scaled_size; + h_out_group = cinfo->max_h_samp_factor; + v_out_group = cinfo->max_v_samp_factor; + upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ + need_buffer = TRUE; + if (! compptr->component_needed) { + /* Don't bother to upsample an uninteresting component. */ + upsample->methods[ci] = noop_upsample; + need_buffer = FALSE; + } else if (h_in_group == h_out_group && v_in_group == v_out_group) { + /* Fullsize components can be processed without any work. */ + upsample->methods[ci] = fullsize_upsample; + need_buffer = FALSE; + } else if (h_in_group * 2 == h_out_group && + v_in_group == v_out_group) { + /* Special case for 2h1v upsampling */ + upsample->methods[ci] = h2v1_upsample; + } else if (h_in_group * 2 == h_out_group && + v_in_group * 2 == v_out_group) { + /* Special case for 2h2v upsampling */ + upsample->methods[ci] = h2v2_upsample; + } else if ((h_out_group % h_in_group) == 0 && + (v_out_group % v_in_group) == 0) { + /* Generic integral-factors upsampling method */ + upsample->methods[ci] = int_upsample; + upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group); + upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group); + } else + ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); + if (need_buffer) { + upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) jround_up((long) cinfo->output_width, + (long) cinfo->max_h_samp_factor), + (JDIMENSION) cinfo->max_v_samp_factor); + } + } +} diff --git a/crypto777/jpeg/jdtrans.c b/crypto777/jpeg/jdtrans.c new file mode 100644 index 000000000..22dd47fb5 --- /dev/null +++ b/crypto777/jpeg/jdtrans.c @@ -0,0 +1,140 @@ +/* + * jdtrans.c + * + * Copyright (C) 1995-1997, Thomas G. Lane. + * Modified 2000-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains library routines for transcoding decompression, + * that is, reading raw DCT coefficient arrays from an input JPEG file. + * The routines in jdapimin.c will also be needed by a transcoder. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Forward declarations */ +LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo)); + + +/* + * Read the coefficient arrays from a JPEG file. + * jpeg_read_header must be completed before calling this. + * + * The entire image is read into a set of virtual coefficient-block arrays, + * one per component. The return value is a pointer to the array of + * virtual-array descriptors. These can be manipulated directly via the + * JPEG memory manager, or handed off to jpeg_write_coefficients(). + * To release the memory occupied by the virtual arrays, call + * jpeg_finish_decompress() when done with the data. + * + * An alternative usage is to simply obtain access to the coefficient arrays + * during a buffered-image-mode decompression operation. This is allowed + * after any jpeg_finish_output() call. The arrays can be accessed until + * jpeg_finish_decompress() is called. (Note that any call to the library + * may reposition the arrays, so don't rely on access_virt_barray() results + * to stay valid across library calls.) + * + * Returns NULL if suspended. This case need be checked only if + * a suspending data source is used. + */ + +GLOBAL(jvirt_barray_ptr *) +jpeg_read_coefficients (j_decompress_ptr cinfo) +{ + if (cinfo->global_state == DSTATE_READY) { + /* First call: initialize active modules */ + transdecode_master_selection(cinfo); + cinfo->global_state = DSTATE_RDCOEFS; + } + if (cinfo->global_state == DSTATE_RDCOEFS) { + /* Absorb whole file into the coef buffer */ + for (;;) { + int retcode; + /* Call progress monitor hook if present */ + if (cinfo->progress != NULL) + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + /* Absorb some more input */ + retcode = (*cinfo->inputctl->consume_input) (cinfo); + if (retcode == JPEG_SUSPENDED) + return NULL; + if (retcode == JPEG_REACHED_EOI) + break; + /* Advance progress counter if appropriate */ + if (cinfo->progress != NULL && + (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { + if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { + /* startup underestimated number of scans; ratchet up one scan */ + cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; + } + } + } + /* Set state so that jpeg_finish_decompress does the right thing */ + cinfo->global_state = DSTATE_STOPPING; + } + /* At this point we should be in state DSTATE_STOPPING if being used + * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access + * to the coefficients during a full buffered-image-mode decompression. + */ + if ((cinfo->global_state == DSTATE_STOPPING || + cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { + return cinfo->coef->coef_arrays; + } + /* Oops, improper usage */ + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + return NULL; /* keep compiler happy */ +} + + +/* + * Master selection of decompression modules for transcoding. + * This substitutes for jdmaster.c's initialization of the full decompressor. + */ + +LOCAL(void) +transdecode_master_selection (j_decompress_ptr cinfo) +{ + /* This is effectively a buffered-image operation. */ + cinfo->buffered_image = TRUE; + + /* Compute output image dimensions and related values. */ + jpeg_core_output_dimensions(cinfo); + + /* Entropy decoding: either Huffman or arithmetic coding. */ + if (cinfo->arith_code) + jinit_arith_decoder(cinfo); + else { + jinit_huff_decoder(cinfo); + } + + /* Always get a full-image coefficient buffer. */ + jinit_d_coef_controller(cinfo, TRUE); + + /* We can now tell the memory manager to allocate virtual arrays. */ + (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); + + /* Initialize input side of decompressor to consume first scan. */ + (*cinfo->inputctl->start_input_pass) (cinfo); + + /* Initialize progress monitoring. */ + if (cinfo->progress != NULL) { + int nscans; + /* Estimate number of scans to set pass_limit. */ + if (cinfo->progressive_mode) { + /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ + nscans = 2 + 3 * cinfo->num_components; + } else if (cinfo->inputctl->has_multiple_scans) { + /* For a nonprogressive multiscan file, estimate 1 scan per component. */ + nscans = cinfo->num_components; + } else { + nscans = 1; + } + cinfo->progress->pass_counter = 0L; + cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; + cinfo->progress->completed_passes = 0; + cinfo->progress->total_passes = 1; + } +} diff --git a/crypto777/jpeg/jerror.c b/crypto777/jpeg/jerror.c new file mode 100644 index 000000000..3da7be86a --- /dev/null +++ b/crypto777/jpeg/jerror.c @@ -0,0 +1,252 @@ +/* + * jerror.c + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains simple error-reporting and trace-message routines. + * These are suitable for Unix-like systems and others where writing to + * stderr is the right thing to do. Many applications will want to replace + * some or all of these routines. + * + * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile, + * you get a Windows-specific hack to display error messages in a dialog box. + * It ain't much, but it beats dropping error messages into the bit bucket, + * which is what happens to output to stderr under most Windows C compilers. + * + * These routines are used by both the compression and decompression code. + */ + +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */ +#include "jinclude.h" +#include "jpeglib.h" +#include "jversion.h" +#include "jerror.h" + +#ifdef USE_WINDOWS_MESSAGEBOX +#include +#endif + +#ifndef EXIT_FAILURE /* define exit() codes if not provided */ +#define EXIT_FAILURE 1 +#endif + + +/* + * Create the message string table. + * We do this from the master message list in jerror.h by re-reading + * jerror.h with a suitable definition for macro JMESSAGE. + * The message table is made an external symbol just in case any applications + * want to refer to it directly. + */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jpeg_std_message_table jMsgTable +#endif + +#define JMESSAGE(code,string) string , + +const char * const jpeg_std_message_table[] = { +#include "jerror.h" + NULL +}; + + +/* + * Error exit handler: must not return to caller. + * + * Applications may override this if they want to get control back after + * an error. Typically one would longjmp somewhere instead of exiting. + * The setjmp buffer can be made a private field within an expanded error + * handler object. Note that the info needed to generate an error message + * is stored in the error object, so you can generate the message now or + * later, at your convenience. + * You should make sure that the JPEG object is cleaned up (with jpeg_abort + * or jpeg_destroy) at some point. + */ + +METHODDEF(void) +error_exit (j_common_ptr cinfo) +{ + /* Always display the message */ + (*cinfo->err->output_message) (cinfo); + + /* Let the memory manager delete any temp files before we die */ + jpeg_destroy(cinfo); + + exit(EXIT_FAILURE); +} + + +/* + * Actual output of an error or trace message. + * Applications may override this method to send JPEG messages somewhere + * other than stderr. + * + * On Windows, printing to stderr is generally completely useless, + * so we provide optional code to produce an error-dialog popup. + * Most Windows applications will still prefer to override this routine, + * but if they don't, it'll do something at least marginally useful. + * + * NOTE: to use the library in an environment that doesn't support the + * C stdio library, you may have to delete the call to fprintf() entirely, + * not just not use this routine. + */ + +METHODDEF(void) +output_message (j_common_ptr cinfo) +{ + char buffer[JMSG_LENGTH_MAX]; + + /* Create the message */ + (*cinfo->err->format_message) (cinfo, buffer); + +#ifdef USE_WINDOWS_MESSAGEBOX + /* Display it in a message dialog box */ + MessageBox(GetActiveWindow(), buffer, "JPEG Library Error", + MB_OK | MB_ICONERROR); +#else + /* Send it to stderr, adding a newline */ + fprintf(stderr, "%s\n", buffer); +#endif +} + + +/* + * Decide whether to emit a trace or warning message. + * msg_level is one of: + * -1: recoverable corrupt-data warning, may want to abort. + * 0: important advisory messages (always display to user). + * 1: first level of tracing detail. + * 2,3,...: successively more detailed tracing messages. + * An application might override this method if it wanted to abort on warnings + * or change the policy about which messages to display. + */ + +METHODDEF(void) +emit_message (j_common_ptr cinfo, int msg_level) +{ + struct jpeg_error_mgr * err = cinfo->err; + + if (msg_level < 0) { + /* It's a warning message. Since corrupt files may generate many warnings, + * the policy implemented here is to show only the first warning, + * unless trace_level >= 3. + */ + if (err->num_warnings == 0 || err->trace_level >= 3) + (*err->output_message) (cinfo); + /* Always count warnings in num_warnings. */ + err->num_warnings++; + } else { + /* It's a trace message. Show it if trace_level >= msg_level. */ + if (err->trace_level >= msg_level) + (*err->output_message) (cinfo); + } +} + + +/* + * Format a message string for the most recent JPEG error or message. + * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX + * characters. Note that no '\n' character is added to the string. + * Few applications should need to override this method. + */ + +METHODDEF(void) +format_message (j_common_ptr cinfo, char * buffer) +{ + struct jpeg_error_mgr * err = cinfo->err; + int msg_code = err->msg_code; + const char * msgtext = NULL; + const char * msgptr; + char ch; + boolean isstring; + + /* Look up message string in proper table */ + if (msg_code > 0 && msg_code <= err->last_jpeg_message) { + msgtext = err->jpeg_message_table[msg_code]; + } else if (err->addon_message_table != NULL && + msg_code >= err->first_addon_message && + msg_code <= err->last_addon_message) { + msgtext = err->addon_message_table[msg_code - err->first_addon_message]; + } + + /* Defend against bogus message number */ + if (msgtext == NULL) { + err->msg_parm.i[0] = msg_code; + msgtext = err->jpeg_message_table[0]; + } + + /* Check for string parameter, as indicated by %s in the message text */ + isstring = FALSE; + msgptr = msgtext; + while ((ch = *msgptr++) != '\0') { + if (ch == '%') { + if (*msgptr == 's') isstring = TRUE; + break; + } + } + + /* Format the message into the passed buffer */ + if (isstring) + sprintf(buffer, msgtext, err->msg_parm.s); + else + sprintf(buffer, msgtext, + err->msg_parm.i[0], err->msg_parm.i[1], + err->msg_parm.i[2], err->msg_parm.i[3], + err->msg_parm.i[4], err->msg_parm.i[5], + err->msg_parm.i[6], err->msg_parm.i[7]); +} + + +/* + * Reset error state variables at start of a new image. + * This is called during compression startup to reset trace/error + * processing to default state, without losing any application-specific + * method pointers. An application might possibly want to override + * this method if it has additional error processing state. + */ + +METHODDEF(void) +reset_error_mgr (j_common_ptr cinfo) +{ + cinfo->err->num_warnings = 0; + /* trace_level is not reset since it is an application-supplied parameter */ + cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */ +} + + +/* + * Fill in the standard error-handling methods in a jpeg_error_mgr object. + * Typical call is: + * struct jpeg_compress_struct cinfo; + * struct jpeg_error_mgr err; + * + * cinfo.err = jpeg_std_error(&err); + * after which the application may override some of the methods. + */ + +GLOBAL(struct jpeg_error_mgr *) +jpeg_std_error (struct jpeg_error_mgr * err) +{ + err->error_exit = error_exit; + err->emit_message = emit_message; + err->output_message = output_message; + err->format_message = format_message; + err->reset_error_mgr = reset_error_mgr; + + err->trace_level = 0; /* default = no tracing */ + err->num_warnings = 0; /* no warnings emitted yet */ + err->msg_code = 0; /* may be useful as a flag for "no error" */ + + /* Initialize message table pointers */ + err->jpeg_message_table = jpeg_std_message_table; + err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1; + + err->addon_message_table = NULL; + err->first_addon_message = 0; /* for safety */ + err->last_addon_message = 0; + + return err; +} diff --git a/crypto777/jpeg/jerror.h b/crypto777/jpeg/jerror.h new file mode 100644 index 000000000..1cfb2b19d --- /dev/null +++ b/crypto777/jpeg/jerror.h @@ -0,0 +1,304 @@ +/* + * jerror.h + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * Modified 1997-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file defines the error and message codes for the JPEG library. + * Edit this file to add new codes, or to translate the message strings to + * some other language. + * A set of error-reporting macros are defined too. Some applications using + * the JPEG library may wish to include this file to get the error codes + * and/or the macros. + */ + +/* + * To define the enum list of message codes, include this file without + * defining macro JMESSAGE. To create a message string table, include it + * again with a suitable JMESSAGE definition (see jerror.c for an example). + */ +#ifndef JMESSAGE +#ifndef JERROR_H +/* First time through, define the enum list */ +#define JMAKE_ENUM_LIST +#else +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */ +#define JMESSAGE(code,string) +#endif /* JERROR_H */ +#endif /* JMESSAGE */ + +#ifdef JMAKE_ENUM_LIST + +typedef enum { + +#define JMESSAGE(code,string) code , + +#endif /* JMAKE_ENUM_LIST */ + +JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */ + +/* For maintenance convenience, list is alphabetical by message code name */ +JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix") +JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix") +JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode") +JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS") +JMESSAGE(JERR_BAD_CROP_SPEC, "Invalid crop request") +JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range") +JMESSAGE(JERR_BAD_DCTSIZE, "DCT scaled block size %dx%d not supported") +JMESSAGE(JERR_BAD_DROP_SAMPLING, + "Component index %d: mismatching sampling ratio %d:%d, %d:%d, %c") +JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition") +JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace") +JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace") +JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length") +JMESSAGE(JERR_BAD_LIB_VERSION, + "Wrong JPEG library version: library is %d, caller expects %d") +JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan") +JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d") +JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d") +JMESSAGE(JERR_BAD_PROGRESSION, + "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d") +JMESSAGE(JERR_BAD_PROG_SCRIPT, + "Invalid progressive parameters at scan script entry %d") +JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors") +JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d") +JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d") +JMESSAGE(JERR_BAD_STRUCT_SIZE, + "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u") +JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access") +JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small") +JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here") +JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet") +JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d") +JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request") +JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d") +JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x") +JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d") +JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d") +JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)") +JMESSAGE(JERR_EMS_READ, "Read from EMS failed") +JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed") +JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan") +JMESSAGE(JERR_FILE_READ, "Input file read error") +JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?") +JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet") +JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow") +JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry") +JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels") +JMESSAGE(JERR_INPUT_EMPTY, "Empty input file") +JMESSAGE(JERR_INPUT_EOF, "Premature end of input file") +JMESSAGE(JERR_MISMATCHED_QUANT_TABLE, + "Cannot transcode due to multiple use of quantization table %d") +JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data") +JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change") +JMESSAGE(JERR_NOTIMPL, "Not implemented yet") +JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time") +JMESSAGE(JERR_NO_ARITH_TABLE, "Arithmetic table 0x%02x was not defined") +JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported") +JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined") +JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image") +JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined") +JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x") +JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)") +JMESSAGE(JERR_QUANT_COMPONENTS, + "Cannot quantize more than %d color components") +JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors") +JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors") +JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers") +JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker") +JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x") +JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers") +JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF") +JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s") +JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file") +JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file") +JMESSAGE(JERR_TFILE_WRITE, + "Write failed on temporary file --- out of disk space?") +JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines") +JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x") +JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up") +JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation") +JMESSAGE(JERR_XMS_READ, "Read from XMS failed") +JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed") +JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT) +JMESSAGE(JMSG_VERSION, JVERSION) +JMESSAGE(JTRC_16BIT_TABLES, + "Caution: quantization tables are too coarse for baseline JPEG") +JMESSAGE(JTRC_ADOBE, + "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d") +JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u") +JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u") +JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x") +JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x") +JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d") +JMESSAGE(JTRC_DRI, "Define Restart Interval %u") +JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u") +JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u") +JMESSAGE(JTRC_EOI, "End Of Image") +JMESSAGE(JTRC_HUFFBITS, " %3d %3d %3d %3d %3d %3d %3d %3d") +JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d %d") +JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE, + "Warning: thumbnail image size does not match data length %u") +JMESSAGE(JTRC_JFIF_EXTENSION, + "JFIF extension marker: type 0x%02x, length %u") +JMESSAGE(JTRC_JFIF_THUMBNAIL, " with %d x %d thumbnail image") +JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u") +JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x") +JMESSAGE(JTRC_QUANTVALS, " %4u %4u %4u %4u %4u %4u %4u %4u") +JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors") +JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors") +JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization") +JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d") +JMESSAGE(JTRC_RST, "RST%d") +JMESSAGE(JTRC_SMOOTH_NOTIMPL, + "Smoothing not supported with nonstandard sampling ratios") +JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d") +JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d") +JMESSAGE(JTRC_SOI, "Start of Image") +JMESSAGE(JTRC_SOS, "Start Of Scan: %d components") +JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d") +JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d") +JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s") +JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s") +JMESSAGE(JTRC_THUMB_JPEG, + "JFIF extension marker: JPEG-compressed thumbnail image, length %u") +JMESSAGE(JTRC_THUMB_PALETTE, + "JFIF extension marker: palette thumbnail image, length %u") +JMESSAGE(JTRC_THUMB_RGB, + "JFIF extension marker: RGB thumbnail image, length %u") +JMESSAGE(JTRC_UNKNOWN_IDS, + "Unrecognized component IDs %d %d %d, assuming YCbCr") +JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u") +JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u") +JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d") +JMESSAGE(JWRN_ARITH_BAD_CODE, "Corrupt JPEG data: bad arithmetic code") +JMESSAGE(JWRN_BOGUS_PROGRESSION, + "Inconsistent progression sequence for component %d coefficient %d") +JMESSAGE(JWRN_EXTRANEOUS_DATA, + "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x") +JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment") +JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code") +JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d") +JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file") +JMESSAGE(JWRN_MUST_RESYNC, + "Corrupt JPEG data: found marker 0x%02x instead of RST%d") +JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG") +JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines") + +#ifdef JMAKE_ENUM_LIST + + JMSG_LASTMSGCODE +} J_MESSAGE_CODE; + +#undef JMAKE_ENUM_LIST +#endif /* JMAKE_ENUM_LIST */ + +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */ +#undef JMESSAGE + + +#ifndef JERROR_H +#define JERROR_H + +/* Macros to simplify using the error and trace message stuff */ +/* The first parameter is either type of cinfo pointer */ + +/* Fatal errors (print message and exit) */ +#define ERREXIT(cinfo,code) \ + ((cinfo)->err->msg_code = (code), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) +#define ERREXIT1(cinfo,code,p1) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) +#define ERREXIT2(cinfo,code,p1,p2) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (cinfo)->err->msg_parm.i[1] = (p2), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) +#define ERREXIT3(cinfo,code,p1,p2,p3) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (cinfo)->err->msg_parm.i[1] = (p2), \ + (cinfo)->err->msg_parm.i[2] = (p3), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) +#define ERREXIT4(cinfo,code,p1,p2,p3,p4) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (cinfo)->err->msg_parm.i[1] = (p2), \ + (cinfo)->err->msg_parm.i[2] = (p3), \ + (cinfo)->err->msg_parm.i[3] = (p4), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) +#define ERREXIT6(cinfo,code,p1,p2,p3,p4,p5,p6) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (cinfo)->err->msg_parm.i[1] = (p2), \ + (cinfo)->err->msg_parm.i[2] = (p3), \ + (cinfo)->err->msg_parm.i[3] = (p4), \ + (cinfo)->err->msg_parm.i[4] = (p5), \ + (cinfo)->err->msg_parm.i[5] = (p6), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) +#define ERREXITS(cinfo,code,str) \ + ((cinfo)->err->msg_code = (code), \ + strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \ + (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) + +#define MAKESTMT(stuff) do { stuff } while (0) + +/* Nonfatal errors (we can keep going, but the data is probably corrupt) */ +#define WARNMS(cinfo,code) \ + ((cinfo)->err->msg_code = (code), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1)) +#define WARNMS1(cinfo,code,p1) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1)) +#define WARNMS2(cinfo,code,p1,p2) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (cinfo)->err->msg_parm.i[1] = (p2), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1)) + +/* Informational/debugging messages */ +#define TRACEMS(cinfo,lvl,code) \ + ((cinfo)->err->msg_code = (code), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) +#define TRACEMS1(cinfo,lvl,code,p1) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) +#define TRACEMS2(cinfo,lvl,code,p1,p2) \ + ((cinfo)->err->msg_code = (code), \ + (cinfo)->err->msg_parm.i[0] = (p1), \ + (cinfo)->err->msg_parm.i[1] = (p2), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) +#define TRACEMS3(cinfo,lvl,code,p1,p2,p3) \ + MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ + _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \ + (cinfo)->err->msg_code = (code); \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) +#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4) \ + MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ + _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \ + (cinfo)->err->msg_code = (code); \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) +#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5) \ + MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ + _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \ + _mp[4] = (p5); \ + (cinfo)->err->msg_code = (code); \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) +#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8) \ + MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ + _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \ + _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \ + (cinfo)->err->msg_code = (code); \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) +#define TRACEMSS(cinfo,lvl,code,str) \ + ((cinfo)->err->msg_code = (code), \ + strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \ + (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) + +#endif /* JERROR_H */ diff --git a/crypto777/jpeg/jfdctflt.c b/crypto777/jpeg/jfdctflt.c new file mode 100644 index 000000000..74d0d862d --- /dev/null +++ b/crypto777/jpeg/jfdctflt.c @@ -0,0 +1,174 @@ +/* + * jfdctflt.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * Modified 2003-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a floating-point implementation of the + * forward DCT (Discrete Cosine Transform). + * + * This implementation should be more accurate than either of the integer + * DCT implementations. However, it may not give the same results on all + * machines because of differences in roundoff behavior. Speed will depend + * on the hardware's floating point capacity. + * + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT + * on each column. Direct algorithms are also available, but they are + * much more complex and seem not to be any faster when reduced to code. + * + * This implementation is based on Arai, Agui, and Nakajima's algorithm for + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in + * Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following code + * is based directly on figure 4-8 in P&M. + * While an 8-point DCT cannot be done in less than 11 multiplies, it is + * possible to arrange the computation so that many of the multiplies are + * simple scalings of the final outputs. These multiplies can then be + * folded into the multiplications or divisions by the JPEG quantization + * table entries. The AA&N method leaves only 5 multiplies and 29 adds + * to be done in the DCT itself. + * The primary disadvantage of this method is that with a fixed-point + * implementation, accuracy is lost due to imprecise representation of the + * scaled quantization values. However, that problem does not arise if + * we use floating point arithmetic. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + +#ifdef DCT_FLOAT_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ +#endif + + +/* + * Perform the forward DCT on one block of samples. + */ + +GLOBAL(void) +jpeg_fdct_float (FAST_FLOAT * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + FAST_FLOAT tmp10, tmp11, tmp12, tmp13; + FAST_FLOAT z1, z2, z3, z4, z5, z11, z13; + FAST_FLOAT *dataptr; + JSAMPROW elemptr; + int ctr; + + /* Pass 1: process rows. */ + + dataptr = data; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Load data into workspace */ + tmp0 = (FAST_FLOAT) (GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7])); + tmp7 = (FAST_FLOAT) (GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[7])); + tmp1 = (FAST_FLOAT) (GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[6])); + tmp6 = (FAST_FLOAT) (GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[6])); + tmp2 = (FAST_FLOAT) (GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[5])); + tmp5 = (FAST_FLOAT) (GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5])); + tmp3 = (FAST_FLOAT) (GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[4])); + tmp4 = (FAST_FLOAT) (GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4])); + + /* Even part */ + + tmp10 = tmp0 + tmp3; /* phase 2 */ + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + /* Apply unsigned->signed conversion */ + dataptr[0] = tmp10 + tmp11 - 8 * CENTERJSAMPLE; /* phase 3 */ + dataptr[4] = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */ + dataptr[2] = tmp13 + z1; /* phase 5 */ + dataptr[6] = tmp13 - z1; + + /* Odd part */ + + tmp10 = tmp4 + tmp5; /* phase 2 */ + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + /* The rotator is modified from fig 4-8 to avoid extra negations. */ + z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */ + z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */ + z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */ + z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */ + + z11 = tmp7 + z3; /* phase 5 */ + z13 = tmp7 - z3; + + dataptr[5] = z13 + z2; /* phase 6 */ + dataptr[3] = z13 - z2; + dataptr[1] = z11 + z4; + dataptr[7] = z11 - z4; + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; + tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; + tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; + tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; + + /* Even part */ + + tmp10 = tmp0 + tmp3; /* phase 2 */ + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */ + dataptr[DCTSIZE*4] = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */ + dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */ + dataptr[DCTSIZE*6] = tmp13 - z1; + + /* Odd part */ + + tmp10 = tmp4 + tmp5; /* phase 2 */ + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + /* The rotator is modified from fig 4-8 to avoid extra negations. */ + z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */ + z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */ + z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */ + z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */ + + z11 = tmp7 + z3; /* phase 5 */ + z13 = tmp7 - z3; + + dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */ + dataptr[DCTSIZE*3] = z13 - z2; + dataptr[DCTSIZE*1] = z11 + z4; + dataptr[DCTSIZE*7] = z11 - z4; + + dataptr++; /* advance pointer to next column */ + } +} + +#endif /* DCT_FLOAT_SUPPORTED */ diff --git a/crypto777/jpeg/jfdctfst.c b/crypto777/jpeg/jfdctfst.c new file mode 100644 index 000000000..8cad5f229 --- /dev/null +++ b/crypto777/jpeg/jfdctfst.c @@ -0,0 +1,230 @@ +/* + * jfdctfst.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * Modified 2003-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a fast, not so accurate integer implementation of the + * forward DCT (Discrete Cosine Transform). + * + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT + * on each column. Direct algorithms are also available, but they are + * much more complex and seem not to be any faster when reduced to code. + * + * This implementation is based on Arai, Agui, and Nakajima's algorithm for + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in + * Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following code + * is based directly on figure 4-8 in P&M. + * While an 8-point DCT cannot be done in less than 11 multiplies, it is + * possible to arrange the computation so that many of the multiplies are + * simple scalings of the final outputs. These multiplies can then be + * folded into the multiplications or divisions by the JPEG quantization + * table entries. The AA&N method leaves only 5 multiplies and 29 adds + * to be done in the DCT itself. + * The primary disadvantage of this method is that with fixed-point math, + * accuracy is lost due to imprecise representation of the scaled + * quantization values. The smaller the quantization table entry, the less + * precise the scaled value, so this implementation does worse with high- + * quality-setting files than with low-quality ones. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + +#ifdef DCT_IFAST_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ +#endif + + +/* Scaling decisions are generally the same as in the LL&M algorithm; + * see jfdctint.c for more details. However, we choose to descale + * (right shift) multiplication products as soon as they are formed, + * rather than carrying additional fractional bits into subsequent additions. + * This compromises accuracy slightly, but it lets us save a few shifts. + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) + * everywhere except in the multiplications proper; this saves a good deal + * of work on 16-bit-int machines. + * + * Again to save a few shifts, the intermediate results between pass 1 and + * pass 2 are not upscaled, but are represented only to integral precision. + * + * A final compromise is to represent the multiplicative constants to only + * 8 fractional bits, rather than 13. This saves some shifting work on some + * machines, and may also reduce the cost of multiplication (since there + * are fewer one-bits in the constants). + */ + +#define CONST_BITS 8 + + +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus + * causing a lot of useless floating-point operations at run time. + * To get around this we use the following pre-calculated constants. + * If you change CONST_BITS you may want to add appropriate values. + * (With a reasonable C compiler, you can just rely on the FIX() macro...) + */ + +#if CONST_BITS == 8 +#define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */ +#define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */ +#define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */ +#define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */ +#else +#define FIX_0_382683433 FIX(0.382683433) +#define FIX_0_541196100 FIX(0.541196100) +#define FIX_0_707106781 FIX(0.707106781) +#define FIX_1_306562965 FIX(1.306562965) +#endif + + +/* We can gain a little more speed, with a further compromise in accuracy, + * by omitting the addition in a descaling shift. This yields an incorrectly + * rounded result half the time... + */ + +#ifndef USE_ACCURATE_ROUNDING +#undef DESCALE +#define DESCALE(x,n) RIGHT_SHIFT(x, n) +#endif + + +/* Multiply a DCTELEM variable by an INT32 constant, and immediately + * descale to yield a DCTELEM result. + */ + +#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) + + +/* + * Perform the forward DCT on one block of samples. + */ + +GLOBAL(void) +jpeg_fdct_ifast (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + DCTELEM tmp10, tmp11, tmp12, tmp13; + DCTELEM z1, z2, z3, z4, z5, z11, z13; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + + dataptr = data; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Load data into workspace */ + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]); + tmp7 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[6]); + tmp6 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[5]); + tmp5 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[4]); + tmp4 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]); + + /* Even part */ + + tmp10 = tmp0 + tmp3; /* phase 2 */ + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + /* Apply unsigned->signed conversion */ + dataptr[0] = tmp10 + tmp11 - 8 * CENTERJSAMPLE; /* phase 3 */ + dataptr[4] = tmp10 - tmp11; + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */ + dataptr[2] = tmp13 + z1; /* phase 5 */ + dataptr[6] = tmp13 - z1; + + /* Odd part */ + + tmp10 = tmp4 + tmp5; /* phase 2 */ + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + /* The rotator is modified from fig 4-8 to avoid extra negations. */ + z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */ + z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */ + z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */ + z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */ + + z11 = tmp7 + z3; /* phase 5 */ + z13 = tmp7 - z3; + + dataptr[5] = z13 + z2; /* phase 6 */ + dataptr[3] = z13 - z2; + dataptr[1] = z11 + z4; + dataptr[7] = z11 - z4; + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; + tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; + tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; + tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; + + /* Even part */ + + tmp10 = tmp0 + tmp3; /* phase 2 */ + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */ + dataptr[DCTSIZE*4] = tmp10 - tmp11; + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */ + dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */ + dataptr[DCTSIZE*6] = tmp13 - z1; + + /* Odd part */ + + tmp10 = tmp4 + tmp5; /* phase 2 */ + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + /* The rotator is modified from fig 4-8 to avoid extra negations. */ + z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */ + z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */ + z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */ + z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */ + + z11 = tmp7 + z3; /* phase 5 */ + z13 = tmp7 - z3; + + dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */ + dataptr[DCTSIZE*3] = z13 - z2; + dataptr[DCTSIZE*1] = z11 + z4; + dataptr[DCTSIZE*7] = z11 - z4; + + dataptr++; /* advance pointer to next column */ + } +} + +#endif /* DCT_IFAST_SUPPORTED */ diff --git a/crypto777/jpeg/jfdctint.c b/crypto777/jpeg/jfdctint.c new file mode 100644 index 000000000..1dde58c49 --- /dev/null +++ b/crypto777/jpeg/jfdctint.c @@ -0,0 +1,4348 @@ +/* + * jfdctint.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modification developed 2003-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a slow-but-accurate integer implementation of the + * forward DCT (Discrete Cosine Transform). + * + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT + * on each column. Direct algorithms are also available, but they are + * much more complex and seem not to be any faster when reduced to code. + * + * This implementation is based on an algorithm described in + * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT + * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, + * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. + * The primary algorithm described there uses 11 multiplies and 29 adds. + * We use their alternate method with 12 multiplies and 32 adds. + * The advantage of this method is that no data path contains more than one + * multiplication; this allows a very simple and accurate implementation in + * scaled fixed-point arithmetic, with a minimal number of shifts. + * + * We also provide FDCT routines with various input sample block sizes for + * direct resolution reduction or enlargement and for direct resolving the + * common 2x1 and 1x2 subsampling cases without additional resampling: NxN + * (N=1...16), 2NxN, and Nx2N (N=1...8) pixels for one 8x8 output DCT block. + * + * For N<8 we fill the remaining block coefficients with zero. + * For N>8 we apply a partial N-point FDCT on the input samples, computing + * just the lower 8 frequency coefficients and discarding the rest. + * + * We must scale the output coefficients of the N-point FDCT appropriately + * to the standard 8-point FDCT level by 8/N per 1-D pass. This scaling + * is folded into the constant multipliers (pass 2) and/or final/initial + * shifting. + * + * CAUTION: We rely on the FIX() macro except for the N=1,2,4,8 cases + * since there would be too many additional constants to pre-calculate. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + +#ifdef DCT_ISLOW_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCT blocks. /* deliberate syntax err */ +#endif + + +/* + * The poop on this scaling stuff is as follows: + * + * Each 1-D DCT step produces outputs which are a factor of sqrt(N) + * larger than the true DCT outputs. The final outputs are therefore + * a factor of N larger than desired; since N=8 this can be cured by + * a simple right shift at the end of the algorithm. The advantage of + * this arrangement is that we save two multiplications per 1-D DCT, + * because the y0 and y4 outputs need not be divided by sqrt(N). + * In the IJG code, this factor of 8 is removed by the quantization step + * (in jcdctmgr.c), NOT in this module. + * + * We have to do addition and subtraction of the integer inputs, which + * is no problem, and multiplication by fractional constants, which is + * a problem to do in integer arithmetic. We multiply all the constants + * by CONST_SCALE and convert them to integer constants (thus retaining + * CONST_BITS bits of precision in the constants). After doing a + * multiplication we have to divide the product by CONST_SCALE, with proper + * rounding, to produce the correct output. This division can be done + * cheaply as a right shift of CONST_BITS bits. We postpone shifting + * as long as possible so that partial sums can be added together with + * full fractional precision. + * + * The outputs of the first pass are scaled up by PASS1_BITS bits so that + * they are represented to better-than-integral precision. These outputs + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word + * with the recommended scaling. (For 12-bit sample data, the intermediate + * array is INT32 anyway.) + * + * To avoid overflow of the 32-bit intermediate results in pass 2, we must + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis + * shows that the values given below are the most effective. + */ + +#if BITS_IN_JSAMPLE == 8 +#define CONST_BITS 13 +#define PASS1_BITS 2 +#else +#define CONST_BITS 13 +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#endif + +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus + * causing a lot of useless floating-point operations at run time. + * To get around this we use the following pre-calculated constants. + * If you change CONST_BITS you may want to add appropriate values. + * (With a reasonable C compiler, you can just rely on the FIX() macro...) + */ + +#if CONST_BITS == 13 +#define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */ +#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */ +#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */ +#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ +#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ +#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */ +#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */ +#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ +#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */ +#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */ +#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ +#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */ +#else +#define FIX_0_298631336 FIX(0.298631336) +#define FIX_0_390180644 FIX(0.390180644) +#define FIX_0_541196100 FIX(0.541196100) +#define FIX_0_765366865 FIX(0.765366865) +#define FIX_0_899976223 FIX(0.899976223) +#define FIX_1_175875602 FIX(1.175875602) +#define FIX_1_501321110 FIX(1.501321110) +#define FIX_1_847759065 FIX(1.847759065) +#define FIX_1_961570560 FIX(1.961570560) +#define FIX_2_053119869 FIX(2.053119869) +#define FIX_2_562915447 FIX(2.562915447) +#define FIX_3_072711026 FIX(3.072711026) +#endif + + +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. + * For 8-bit samples with the recommended scaling, all the variable + * and constant values involved are no more than 16 bits wide, so a + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. + * For 12-bit samples, a full 32-bit multiplication will be needed. + */ + +#if BITS_IN_JSAMPLE == 8 +#define MULTIPLY(var,const) MULTIPLY16C16(var,const) +#else +#define MULTIPLY(var,const) ((var) * (const)) +#endif + + +/* + * Perform the forward DCT on one block of samples. + */ + +GLOBAL(void) +jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + dataptr = data; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[4]); + + tmp10 = tmp0 + tmp3; + tmp12 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp13 = tmp1 - tmp2; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + dataptr[2] = (DCTELEM) RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), + CONST_BITS-PASS1_BITS); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * cK represents sqrt(2) * cos(K*pi/16). + * i0..i3 in the paper are tmp0..tmp3 here. + */ + + tmp10 = tmp0 + tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp0 + tmp2; + tmp13 = tmp1 + tmp3; + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + + tmp0 = MULTIPLY(tmp0, FIX_1_501321110); /* c1+c3-c5-c7 */ + tmp1 = MULTIPLY(tmp1, FIX_3_072711026); /* c1+c3+c5-c7 */ + tmp2 = MULTIPLY(tmp2, FIX_2_053119869); /* c1+c3-c5+c7 */ + tmp3 = MULTIPLY(tmp3, FIX_0_298631336); /* -c1+c3+c5-c7 */ + tmp10 = MULTIPLY(tmp10, - FIX_0_899976223); /* c7-c3 */ + tmp11 = MULTIPLY(tmp11, - FIX_2_562915447); /* -c1-c3 */ + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* c5-c3 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + + tmp12 += z1; + tmp13 += z1; + + dataptr[1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) + RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) + RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS-PASS1_BITS); + dataptr[7] = (DCTELEM) + RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; + + /* Add fudge factor here for final descale. */ + tmp10 = tmp0 + tmp3 + (ONE << (PASS1_BITS-1)); + tmp12 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp13 = tmp1 - tmp2; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; + + dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS1_BITS); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS+PASS1_BITS-1); + dataptr[DCTSIZE*2] = (DCTELEM) + RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) + RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), CONST_BITS+PASS1_BITS); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * cK represents sqrt(2) * cos(K*pi/16). + * i0..i3 in the paper are tmp0..tmp3 here. + */ + + tmp10 = tmp0 + tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp0 + tmp2; + tmp13 = tmp1 + tmp3; + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS+PASS1_BITS-1); + + tmp0 = MULTIPLY(tmp0, FIX_1_501321110); /* c1+c3-c5-c7 */ + tmp1 = MULTIPLY(tmp1, FIX_3_072711026); /* c1+c3+c5-c7 */ + tmp2 = MULTIPLY(tmp2, FIX_2_053119869); /* c1+c3-c5+c7 */ + tmp3 = MULTIPLY(tmp3, FIX_0_298631336); /* -c1+c3+c5-c7 */ + tmp10 = MULTIPLY(tmp10, - FIX_0_899976223); /* c7-c3 */ + tmp11 = MULTIPLY(tmp11, - FIX_2_562915447); /* -c1-c3 */ + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* c5-c3 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + + tmp12 += z1; + tmp13 += z1; + + dataptr[DCTSIZE*1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + +#ifdef DCT_SCALING_SUPPORTED + + +/* + * Perform the forward DCT on a 7x7 sample block. + */ + +GLOBAL(void) +jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12; + INT32 z1, z2, z3; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* cK represents sqrt(2) * cos(K*pi/14). */ + + dataptr = data; + for (ctr = 0; ctr < 7; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[6]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[5]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[4]); + tmp3 = GETJSAMPLE(elemptr[3]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[6]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[5]); + tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[4]); + + z1 = tmp0 + tmp2; + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE) << PASS1_BITS); + tmp3 += tmp3; + z1 -= tmp3; + z1 -= tmp3; + z1 = MULTIPLY(z1, FIX(0.353553391)); /* (c2+c6-c4)/2 */ + z2 = MULTIPLY(tmp0 - tmp2, FIX(0.920609002)); /* (c2+c4-c6)/2 */ + z3 = MULTIPLY(tmp1 - tmp2, FIX(0.314692123)); /* c6 */ + dataptr[2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS-PASS1_BITS); + z1 -= z2; + z2 = MULTIPLY(tmp0 - tmp1, FIX(0.881747734)); /* c4 */ + dataptr[4] = (DCTELEM) + DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.707106781)), /* c2+c6-c4 */ + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(0.935414347)); /* (c3+c1-c5)/2 */ + tmp2 = MULTIPLY(tmp10 - tmp11, FIX(0.170262339)); /* (c3+c5-c1)/2 */ + tmp0 = tmp1 - tmp2; + tmp1 += tmp2; + tmp2 = MULTIPLY(tmp11 + tmp12, - FIX(1.378756276)); /* -c1 */ + tmp1 += tmp2; + tmp3 = MULTIPLY(tmp10 + tmp12, FIX(0.613604268)); /* c5 */ + tmp0 += tmp3; + tmp2 += tmp3 + MULTIPLY(tmp12, FIX(1.870828693)); /* c3+c1-c5 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/7)**2 = 64/49, which we fold + * into the constant multipliers: + * cK now represents sqrt(2) * cos(K*pi/14) * 64/49. + */ + + dataptr = data; + for (ctr = 0; ctr < 7; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*6]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*5]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*4]; + tmp3 = dataptr[DCTSIZE*3]; + + tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*6]; + tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*5]; + tmp12 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*4]; + + z1 = tmp0 + tmp2; + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(z1 + tmp1 + tmp3, FIX(1.306122449)), /* 64/49 */ + CONST_BITS+PASS1_BITS); + tmp3 += tmp3; + z1 -= tmp3; + z1 -= tmp3; + z1 = MULTIPLY(z1, FIX(0.461784020)); /* (c2+c6-c4)/2 */ + z2 = MULTIPLY(tmp0 - tmp2, FIX(1.202428084)); /* (c2+c4-c6)/2 */ + z3 = MULTIPLY(tmp1 - tmp2, FIX(0.411026446)); /* c6 */ + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS+PASS1_BITS); + z1 -= z2; + z2 = MULTIPLY(tmp0 - tmp1, FIX(1.151670509)); /* c4 */ + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.923568041)), /* c2+c6-c4 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.221765677)); /* (c3+c1-c5)/2 */ + tmp2 = MULTIPLY(tmp10 - tmp11, FIX(0.222383464)); /* (c3+c5-c1)/2 */ + tmp0 = tmp1 - tmp2; + tmp1 += tmp2; + tmp2 = MULTIPLY(tmp11 + tmp12, - FIX(1.800824523)); /* -c1 */ + tmp1 += tmp2; + tmp3 = MULTIPLY(tmp10 + tmp12, FIX(0.801442310)); /* c5 */ + tmp0 += tmp3; + tmp2 += tmp3 + MULTIPLY(tmp12, FIX(2.443531355)); /* c3+c1-c5 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 6x6 sample block. + */ + +GLOBAL(void) +jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2; + INT32 tmp10, tmp11, tmp12; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* cK represents sqrt(2) * cos(K*pi/12). */ + + dataptr = data; + for (ctr = 0; ctr < 6; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[5]); + tmp11 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[4]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[3]); + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[5]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[4]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp12, FIX(1.224744871)), /* c2 */ + CONST_BITS-PASS1_BITS); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(0.707106781)), /* c4 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)), /* c5 */ + CONST_BITS-PASS1_BITS); + + dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << PASS1_BITS)); + dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << PASS1_BITS); + dataptr[5] = (DCTELEM) (tmp10 + ((tmp2 - tmp1) << PASS1_BITS)); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/6)**2 = 16/9, which we fold + * into the constant multipliers: + * cK now represents sqrt(2) * cos(K*pi/12) * 16/9. + */ + + dataptr = data; + for (ctr = 0; ctr < 6; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*5]; + tmp11 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*3]; + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*3]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp12, FIX(2.177324216)), /* c2 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp2, FIX(0.650711829)); /* c5 */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0 + tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 5x5 sample block. + */ + +GLOBAL(void) +jpeg_fdct_5x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2; + INT32 tmp10, tmp11; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We scale the results further by 2 as part of output adaption */ + /* scaling for different DCT size. */ + /* cK represents sqrt(2) * cos(K*pi/10). */ + + dataptr = data; + for (ctr = 0; ctr < 5; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[4]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[3]); + tmp2 = GETJSAMPLE(elemptr[2]); + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[4]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[3]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp2 - 5 * CENTERJSAMPLE) << (PASS1_BITS+1)); + tmp11 = MULTIPLY(tmp11, FIX(0.790569415)); /* (c2+c4)/2 */ + tmp10 -= tmp2 << 2; + tmp10 = MULTIPLY(tmp10, FIX(0.353553391)); /* (c2-c4)/2 */ + dataptr[2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS-PASS1_BITS-1); + dataptr[4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS-PASS1_BITS-1); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp1, FIX(0.831253876)); /* c3 */ + + dataptr[1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.513743148)), /* c1-c3 */ + CONST_BITS-PASS1_BITS-1); + dataptr[3] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.176250899)), /* c1+c3 */ + CONST_BITS-PASS1_BITS-1); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/5)**2 = 64/25, which we partially + * fold into the constant multipliers (other part was done in pass 1): + * cK now represents sqrt(2) * cos(K*pi/10) * 32/25. + */ + + dataptr = data; + for (ctr = 0; ctr < 5; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*4]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*3]; + tmp2 = dataptr[DCTSIZE*2]; + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*4]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*3]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp2, FIX(1.28)), /* 32/25 */ + CONST_BITS+PASS1_BITS); + tmp11 = MULTIPLY(tmp11, FIX(1.011928851)); /* (c2+c4)/2 */ + tmp10 -= tmp2 << 2; + tmp10 = MULTIPLY(tmp10, FIX(0.452548340)); /* (c2-c4)/2 */ + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp1, FIX(1.064004961)); /* c3 */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.657591230)), /* c1-c3 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.785601151)), /* c1+c3 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 4x4 sample block. + */ + +GLOBAL(void) +jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1; + INT32 tmp10, tmp11; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We must also scale the output by (8/4)**2 = 2**2, which we add here. */ + /* cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. */ + + dataptr = data; + for (ctr = 0; ctr < 4; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+2)); + dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+2)); + + /* Odd part */ + + tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-3); + + dataptr[1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ + CONST_BITS-PASS1_BITS-2); + dataptr[3] = (DCTELEM) + RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ + CONST_BITS-PASS1_BITS-2); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + */ + + dataptr = data; + for (ctr = 0; ctr < 4; ctr++) { + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + (ONE << (PASS1_BITS-1)); + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*2]; + + tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; + tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; + + dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp0 + tmp1, PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) RIGHT_SHIFT(tmp0 - tmp1, PASS1_BITS); + + /* Odd part */ + + tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS+PASS1_BITS-1); + + dataptr[DCTSIZE*1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 3x3 sample block. + */ + +GLOBAL(void) +jpeg_fdct_3x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We scale the results further by 2**2 as part of output adaption */ + /* scaling for different DCT size. */ + /* cK represents sqrt(2) * cos(K*pi/6). */ + + dataptr = data; + for (ctr = 0; ctr < 3; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[2]); + tmp1 = GETJSAMPLE(elemptr[1]); + + tmp2 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[2]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp0 + tmp1 - 3 * CENTERJSAMPLE) << (PASS1_BITS+2)); + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(0.707106781)), /* c2 */ + CONST_BITS-PASS1_BITS-2); + + /* Odd part */ + + dataptr[1] = (DCTELEM) + DESCALE(MULTIPLY(tmp2, FIX(1.224744871)), /* c1 */ + CONST_BITS-PASS1_BITS-2); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/3)**2 = 64/9, which we partially + * fold into the constant multipliers (other part was done in pass 1): + * cK now represents sqrt(2) * cos(K*pi/6) * 16/9. + */ + + dataptr = data; + for (ctr = 0; ctr < 3; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*2]; + tmp1 = dataptr[DCTSIZE*1]; + + tmp2 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*2]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 + tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(1.257078722)), /* c2 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(MULTIPLY(tmp2, FIX(2.177324216)), /* c1 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 2x2 sample block. + */ + +GLOBAL(void) +jpeg_fdct_2x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + JSAMPROW elemptr; + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT. */ + + /* Row 0 */ + elemptr = sample_data[0] + start_col; + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[1]); + tmp1 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[1]); + + /* Row 1 */ + elemptr = sample_data[1] + start_col; + + tmp2 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[1]); + tmp3 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[1]); + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/2)**2 = 2**4. + */ + + /* Column 0 */ + /* Apply unsigned->signed conversion */ + data[DCTSIZE*0] = (DCTELEM) ((tmp0 + tmp2 - 4 * CENTERJSAMPLE) << 4); + data[DCTSIZE*1] = (DCTELEM) ((tmp0 - tmp2) << 4); + + /* Column 1 */ + data[DCTSIZE*0+1] = (DCTELEM) ((tmp1 + tmp3) << 4); + data[DCTSIZE*1+1] = (DCTELEM) ((tmp1 - tmp3) << 4); +} + + +/* + * Perform the forward DCT on a 1x1 sample block. + */ + +GLOBAL(void) +jpeg_fdct_1x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* We leave the result scaled up by an overall factor of 8. */ + /* We must also scale the output by (8/1)**2 = 2**6. */ + /* Apply unsigned->signed conversion */ + data[0] = (DCTELEM) + ((GETJSAMPLE(sample_data[0][start_col]) - CENTERJSAMPLE) << 6); +} + + +/* + * Perform the forward DCT on a 9x9 sample block. + */ + +GLOBAL(void) +jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1, z2; + DCTELEM workspace[8]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* we scale the results further by 2 as part of output adaption */ + /* scaling for different DCT size. */ + /* cK represents sqrt(2) * cos(K*pi/18). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[8]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[7]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[6]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[5]); + tmp4 = GETJSAMPLE(elemptr[4]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[8]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[7]); + tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[6]); + tmp13 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[5]); + + z1 = tmp0 + tmp2 + tmp3; + z2 = tmp1 + tmp4; + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) ((z1 + z2 - 9 * CENTERJSAMPLE) << 1); + dataptr[6] = (DCTELEM) + DESCALE(MULTIPLY(z1 - z2 - z2, FIX(0.707106781)), /* c6 */ + CONST_BITS-1); + z1 = MULTIPLY(tmp0 - tmp2, FIX(1.328926049)); /* c2 */ + z2 = MULTIPLY(tmp1 - tmp4 - tmp4, FIX(0.707106781)); /* c6 */ + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp2 - tmp3, FIX(1.083350441)) /* c4 */ + + z1 + z2, CONST_BITS-1); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp3 - tmp0, FIX(0.245575608)) /* c8 */ + + z1 - z2, CONST_BITS-1); + + /* Odd part */ + + dataptr[3] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12 - tmp13, FIX(1.224744871)), /* c3 */ + CONST_BITS-1); + + tmp11 = MULTIPLY(tmp11, FIX(1.224744871)); /* c3 */ + tmp0 = MULTIPLY(tmp10 + tmp12, FIX(0.909038955)); /* c5 */ + tmp1 = MULTIPLY(tmp10 + tmp13, FIX(0.483689525)); /* c7 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp11 + tmp0 + tmp1, CONST_BITS-1); + + tmp2 = MULTIPLY(tmp12 - tmp13, FIX(1.392728481)); /* c1 */ + + dataptr[5] = (DCTELEM) DESCALE(tmp0 - tmp11 - tmp2, CONST_BITS-1); + dataptr[7] = (DCTELEM) DESCALE(tmp1 - tmp11 + tmp2, CONST_BITS-1); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 9) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/9)**2 = 64/81, which we partially + * fold into the constant multipliers and final/initial shifting: + * cK now represents sqrt(2) * cos(K*pi/18) * 128/81. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*0]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*7]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*6]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*5]; + tmp4 = dataptr[DCTSIZE*4]; + + tmp10 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*0]; + tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*7]; + tmp12 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*6]; + tmp13 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*5]; + + z1 = tmp0 + tmp2 + tmp3; + z2 = tmp1 + tmp4; + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(z1 + z2, FIX(1.580246914)), /* 128/81 */ + CONST_BITS+2); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(MULTIPLY(z1 - z2 - z2, FIX(1.117403309)), /* c6 */ + CONST_BITS+2); + z1 = MULTIPLY(tmp0 - tmp2, FIX(2.100031287)); /* c2 */ + z2 = MULTIPLY(tmp1 - tmp4 - tmp4, FIX(1.117403309)); /* c6 */ + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp2 - tmp3, FIX(1.711961190)) /* c4 */ + + z1 + z2, CONST_BITS+2); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp3 - tmp0, FIX(0.388070096)) /* c8 */ + + z1 - z2, CONST_BITS+2); + + /* Odd part */ + + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12 - tmp13, FIX(1.935399303)), /* c3 */ + CONST_BITS+2); + + tmp11 = MULTIPLY(tmp11, FIX(1.935399303)); /* c3 */ + tmp0 = MULTIPLY(tmp10 + tmp12, FIX(1.436506004)); /* c5 */ + tmp1 = MULTIPLY(tmp10 + tmp13, FIX(0.764348879)); /* c7 */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp11 + tmp0 + tmp1, CONST_BITS+2); + + tmp2 = MULTIPLY(tmp12 - tmp13, FIX(2.200854883)); /* c1 */ + + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp0 - tmp11 - tmp2, CONST_BITS+2); + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(tmp1 - tmp11 + tmp2, CONST_BITS+2); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 10x10 sample block. + */ + +GLOBAL(void) +jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + DCTELEM workspace[8*2]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* we scale the results further by 2 as part of output adaption */ + /* scaling for different DCT size. */ + /* cK represents sqrt(2) * cos(K*pi/20). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[9]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[8]); + tmp12 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[7]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[6]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[5]); + + tmp10 = tmp0 + tmp4; + tmp13 = tmp0 - tmp4; + tmp11 = tmp1 + tmp3; + tmp14 = tmp1 - tmp3; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[9]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[8]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[7]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[6]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[5]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE) << 1); + tmp12 += tmp12; + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.144122806)) - /* c4 */ + MULTIPLY(tmp11 - tmp12, FIX(0.437016024)), /* c8 */ + CONST_BITS-1); + tmp10 = MULTIPLY(tmp13 + tmp14, FIX(0.831253876)); /* c6 */ + dataptr[2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.513743148)), /* c2-c6 */ + CONST_BITS-1); + dataptr[6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.176250899)), /* c2+c6 */ + CONST_BITS-1); + + /* Odd part */ + + tmp10 = tmp0 + tmp4; + tmp11 = tmp1 - tmp3; + dataptr[5] = (DCTELEM) ((tmp10 - tmp11 - tmp2) << 1); + tmp2 <<= CONST_BITS; + dataptr[1] = (DCTELEM) + DESCALE(MULTIPLY(tmp0, FIX(1.396802247)) + /* c1 */ + MULTIPLY(tmp1, FIX(1.260073511)) + tmp2 + /* c3 */ + MULTIPLY(tmp3, FIX(0.642039522)) + /* c7 */ + MULTIPLY(tmp4, FIX(0.221231742)), /* c9 */ + CONST_BITS-1); + tmp12 = MULTIPLY(tmp0 - tmp4, FIX(0.951056516)) - /* (c3+c7)/2 */ + MULTIPLY(tmp1 + tmp3, FIX(0.587785252)); /* (c1-c9)/2 */ + tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.309016994)) + /* (c3-c7)/2 */ + (tmp11 << (CONST_BITS - 1)) - tmp2; + dataptr[3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS-1); + dataptr[7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS-1); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 10) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/10)**2 = 16/25, which we partially + * fold into the constant multipliers and final/initial shifting: + * cK now represents sqrt(2) * cos(K*pi/20) * 32/25. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*1]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*0]; + tmp12 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*7]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*6]; + tmp4 = dataptr[DCTSIZE*4] + dataptr[DCTSIZE*5]; + + tmp10 = tmp0 + tmp4; + tmp13 = tmp0 - tmp4; + tmp11 = tmp1 + tmp3; + tmp14 = tmp1 - tmp3; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*1]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*0]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*7]; + tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*6]; + tmp4 = dataptr[DCTSIZE*4] - dataptr[DCTSIZE*5]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(1.28)), /* 32/25 */ + CONST_BITS+2); + tmp12 += tmp12; + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.464477191)) - /* c4 */ + MULTIPLY(tmp11 - tmp12, FIX(0.559380511)), /* c8 */ + CONST_BITS+2); + tmp10 = MULTIPLY(tmp13 + tmp14, FIX(1.064004961)); /* c6 */ + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.657591230)), /* c2-c6 */ + CONST_BITS+2); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.785601151)), /* c2+c6 */ + CONST_BITS+2); + + /* Odd part */ + + tmp10 = tmp0 + tmp4; + tmp11 = tmp1 - tmp3; + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp2, FIX(1.28)), /* 32/25 */ + CONST_BITS+2); + tmp2 = MULTIPLY(tmp2, FIX(1.28)); /* 32/25 */ + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(MULTIPLY(tmp0, FIX(1.787906876)) + /* c1 */ + MULTIPLY(tmp1, FIX(1.612894094)) + tmp2 + /* c3 */ + MULTIPLY(tmp3, FIX(0.821810588)) + /* c7 */ + MULTIPLY(tmp4, FIX(0.283176630)), /* c9 */ + CONST_BITS+2); + tmp12 = MULTIPLY(tmp0 - tmp4, FIX(1.217352341)) - /* (c3+c7)/2 */ + MULTIPLY(tmp1 + tmp3, FIX(0.752365123)); /* (c1-c9)/2 */ + tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.395541753)) + /* (c3-c7)/2 */ + MULTIPLY(tmp11, FIX(0.64)) - tmp2; /* 16/25 */ + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+2); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+2); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on an 11x11 sample block. + */ + +GLOBAL(void) +jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + INT32 z1, z2, z3; + DCTELEM workspace[8*3]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* we scale the results further by 2 as part of output adaption */ + /* scaling for different DCT size. */ + /* cK represents sqrt(2) * cos(K*pi/22). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[10]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[9]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[8]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[7]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[6]); + tmp5 = GETJSAMPLE(elemptr[5]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[10]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[9]); + tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[8]); + tmp13 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[7]); + tmp14 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[6]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 - 11 * CENTERJSAMPLE) << 1); + tmp5 += tmp5; + tmp0 -= tmp5; + tmp1 -= tmp5; + tmp2 -= tmp5; + tmp3 -= tmp5; + tmp4 -= tmp5; + z1 = MULTIPLY(tmp0 + tmp3, FIX(1.356927976)) + /* c2 */ + MULTIPLY(tmp2 + tmp4, FIX(0.201263574)); /* c10 */ + z2 = MULTIPLY(tmp1 - tmp3, FIX(0.926112931)); /* c6 */ + z3 = MULTIPLY(tmp0 - tmp1, FIX(1.189712156)); /* c4 */ + dataptr[2] = (DCTELEM) + DESCALE(z1 + z2 - MULTIPLY(tmp3, FIX(1.018300590)) /* c2+c8-c6 */ + - MULTIPLY(tmp4, FIX(1.390975730)), /* c4+c10 */ + CONST_BITS-1); + dataptr[4] = (DCTELEM) + DESCALE(z2 + z3 + MULTIPLY(tmp1, FIX(0.062335650)) /* c4-c6-c10 */ + - MULTIPLY(tmp2, FIX(1.356927976)) /* c2 */ + + MULTIPLY(tmp4, FIX(0.587485545)), /* c8 */ + CONST_BITS-1); + dataptr[6] = (DCTELEM) + DESCALE(z1 + z3 - MULTIPLY(tmp0, FIX(1.620527200)) /* c2+c4-c6 */ + - MULTIPLY(tmp2, FIX(0.788749120)), /* c8+c10 */ + CONST_BITS-1); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.286413905)); /* c3 */ + tmp2 = MULTIPLY(tmp10 + tmp12, FIX(1.068791298)); /* c5 */ + tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.764581576)); /* c7 */ + tmp0 = tmp1 + tmp2 + tmp3 - MULTIPLY(tmp10, FIX(1.719967871)) /* c7+c5+c3-c1 */ + + MULTIPLY(tmp14, FIX(0.398430003)); /* c9 */ + tmp4 = MULTIPLY(tmp11 + tmp12, - FIX(0.764581576)); /* -c7 */ + tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(1.399818907)); /* -c1 */ + tmp1 += tmp4 + tmp5 + MULTIPLY(tmp11, FIX(1.276416582)) /* c9+c7+c1-c3 */ + - MULTIPLY(tmp14, FIX(1.068791298)); /* c5 */ + tmp10 = MULTIPLY(tmp12 + tmp13, FIX(0.398430003)); /* c9 */ + tmp2 += tmp4 + tmp10 - MULTIPLY(tmp12, FIX(1.989053629)) /* c9+c5+c3-c7 */ + + MULTIPLY(tmp14, FIX(1.399818907)); /* c1 */ + tmp3 += tmp5 + tmp10 + MULTIPLY(tmp13, FIX(1.305598626)) /* c1+c5-c9-c7 */ + - MULTIPLY(tmp14, FIX(1.286413905)); /* c3 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-1); + dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-1); + dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS-1); + dataptr[7] = (DCTELEM) DESCALE(tmp3, CONST_BITS-1); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 11) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/11)**2 = 64/121, which we partially + * fold into the constant multipliers and final/initial shifting: + * cK now represents sqrt(2) * cos(K*pi/22) * 128/121. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*2]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*1]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*0]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*7]; + tmp4 = dataptr[DCTSIZE*4] + dataptr[DCTSIZE*6]; + tmp5 = dataptr[DCTSIZE*5]; + + tmp10 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*2]; + tmp11 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*1]; + tmp12 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*0]; + tmp13 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*7]; + tmp14 = dataptr[DCTSIZE*4] - dataptr[DCTSIZE*6]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5, + FIX(1.057851240)), /* 128/121 */ + CONST_BITS+2); + tmp5 += tmp5; + tmp0 -= tmp5; + tmp1 -= tmp5; + tmp2 -= tmp5; + tmp3 -= tmp5; + tmp4 -= tmp5; + z1 = MULTIPLY(tmp0 + tmp3, FIX(1.435427942)) + /* c2 */ + MULTIPLY(tmp2 + tmp4, FIX(0.212906922)); /* c10 */ + z2 = MULTIPLY(tmp1 - tmp3, FIX(0.979689713)); /* c6 */ + z3 = MULTIPLY(tmp0 - tmp1, FIX(1.258538479)); /* c4 */ + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(z1 + z2 - MULTIPLY(tmp3, FIX(1.077210542)) /* c2+c8-c6 */ + - MULTIPLY(tmp4, FIX(1.471445400)), /* c4+c10 */ + CONST_BITS+2); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(z2 + z3 + MULTIPLY(tmp1, FIX(0.065941844)) /* c4-c6-c10 */ + - MULTIPLY(tmp2, FIX(1.435427942)) /* c2 */ + + MULTIPLY(tmp4, FIX(0.621472312)), /* c8 */ + CONST_BITS+2); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(z1 + z3 - MULTIPLY(tmp0, FIX(1.714276708)) /* c2+c4-c6 */ + - MULTIPLY(tmp2, FIX(0.834379234)), /* c8+c10 */ + CONST_BITS+2); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.360834544)); /* c3 */ + tmp2 = MULTIPLY(tmp10 + tmp12, FIX(1.130622199)); /* c5 */ + tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.808813568)); /* c7 */ + tmp0 = tmp1 + tmp2 + tmp3 - MULTIPLY(tmp10, FIX(1.819470145)) /* c7+c5+c3-c1 */ + + MULTIPLY(tmp14, FIX(0.421479672)); /* c9 */ + tmp4 = MULTIPLY(tmp11 + tmp12, - FIX(0.808813568)); /* -c7 */ + tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(1.480800167)); /* -c1 */ + tmp1 += tmp4 + tmp5 + MULTIPLY(tmp11, FIX(1.350258864)) /* c9+c7+c1-c3 */ + - MULTIPLY(tmp14, FIX(1.130622199)); /* c5 */ + tmp10 = MULTIPLY(tmp12 + tmp13, FIX(0.421479672)); /* c9 */ + tmp2 += tmp4 + tmp10 - MULTIPLY(tmp12, FIX(2.104122847)) /* c9+c5+c3-c7 */ + + MULTIPLY(tmp14, FIX(1.480800167)); /* c1 */ + tmp3 += tmp5 + tmp10 + MULTIPLY(tmp13, FIX(1.381129125)) /* c1+c5-c9-c7 */ + - MULTIPLY(tmp14, FIX(1.360834544)); /* c3 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+2); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+2); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+2); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+2); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 12x12 sample block. + */ + +GLOBAL(void) +jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + DCTELEM workspace[8*4]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT. */ + /* cK represents sqrt(2) * cos(K*pi/24). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[11]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[10]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[9]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[8]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[7]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[6]); + + tmp10 = tmp0 + tmp5; + tmp13 = tmp0 - tmp5; + tmp11 = tmp1 + tmp4; + tmp14 = tmp1 - tmp4; + tmp12 = tmp2 + tmp3; + tmp15 = tmp2 - tmp3; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[11]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[10]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[9]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[8]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[7]); + tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[6]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) (tmp10 + tmp11 + tmp12 - 12 * CENTERJSAMPLE); + dataptr[6] = (DCTELEM) (tmp13 - tmp14 - tmp15); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.224744871)), /* c4 */ + CONST_BITS); + dataptr[2] = (DCTELEM) + DESCALE(tmp14 - tmp15 + MULTIPLY(tmp13 + tmp15, FIX(1.366025404)), /* c2 */ + CONST_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp1 + tmp4, FIX_0_541196100); /* c9 */ + tmp14 = tmp10 + MULTIPLY(tmp1, FIX_0_765366865); /* c3-c9 */ + tmp15 = tmp10 - MULTIPLY(tmp4, FIX_1_847759065); /* c3+c9 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.121971054)); /* c5 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.860918669)); /* c7 */ + tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.580774953)) /* c5+c7-c1 */ + + MULTIPLY(tmp5, FIX(0.184591911)); /* c11 */ + tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.184591911)); /* -c11 */ + tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.339493912)) /* c1+c5-c11 */ + + MULTIPLY(tmp5, FIX(0.860918669)); /* c7 */ + tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.725788011)) /* c1+c11-c7 */ + - MULTIPLY(tmp5, FIX(1.121971054)); /* c5 */ + tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.306562965)) /* c3 */ + - MULTIPLY(tmp2 + tmp5, FIX_0_541196100); /* c9 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp12, CONST_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp13, CONST_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 12) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/12)**2 = 4/9, which we partially + * fold into the constant multipliers and final shifting: + * cK now represents sqrt(2) * cos(K*pi/24) * 8/9. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*3]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*2]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*1]; + tmp3 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*0]; + tmp4 = dataptr[DCTSIZE*4] + dataptr[DCTSIZE*7]; + tmp5 = dataptr[DCTSIZE*5] + dataptr[DCTSIZE*6]; + + tmp10 = tmp0 + tmp5; + tmp13 = tmp0 - tmp5; + tmp11 = tmp1 + tmp4; + tmp14 = tmp1 - tmp4; + tmp12 = tmp2 + tmp3; + tmp15 = tmp2 - tmp3; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*3]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*2]; + tmp2 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*1]; + tmp3 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*0]; + tmp4 = dataptr[DCTSIZE*4] - dataptr[DCTSIZE*7]; + tmp5 = dataptr[DCTSIZE*5] - dataptr[DCTSIZE*6]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(0.888888889)), /* 8/9 */ + CONST_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(MULTIPLY(tmp13 - tmp14 - tmp15, FIX(0.888888889)), /* 8/9 */ + CONST_BITS+1); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.088662108)), /* c4 */ + CONST_BITS+1); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp14 - tmp15, FIX(0.888888889)) + /* 8/9 */ + MULTIPLY(tmp13 + tmp15, FIX(1.214244803)), /* c2 */ + CONST_BITS+1); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp1 + tmp4, FIX(0.481063200)); /* c9 */ + tmp14 = tmp10 + MULTIPLY(tmp1, FIX(0.680326102)); /* c3-c9 */ + tmp15 = tmp10 - MULTIPLY(tmp4, FIX(1.642452502)); /* c3+c9 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(0.997307603)); /* c5 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.765261039)); /* c7 */ + tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.516244403)) /* c5+c7-c1 */ + + MULTIPLY(tmp5, FIX(0.164081699)); /* c11 */ + tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.164081699)); /* -c11 */ + tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.079550144)) /* c1+c5-c11 */ + + MULTIPLY(tmp5, FIX(0.765261039)); /* c7 */ + tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.645144899)) /* c1+c11-c7 */ + - MULTIPLY(tmp5, FIX(0.997307603)); /* c5 */ + tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.161389302)) /* c3 */ + - MULTIPLY(tmp2 + tmp5, FIX(0.481063200)); /* c9 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+1); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+1); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 13x13 sample block. + */ + +GLOBAL(void) +jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + INT32 z1, z2; + DCTELEM workspace[8*5]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT. */ + /* cK represents sqrt(2) * cos(K*pi/26). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[12]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[11]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[10]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[9]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[8]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[7]); + tmp6 = GETJSAMPLE(elemptr[6]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[12]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[11]); + tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[10]); + tmp13 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[9]); + tmp14 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[8]); + tmp15 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[7]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + (tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 + tmp6 - 13 * CENTERJSAMPLE); + tmp6 += tmp6; + tmp0 -= tmp6; + tmp1 -= tmp6; + tmp2 -= tmp6; + tmp3 -= tmp6; + tmp4 -= tmp6; + tmp5 -= tmp6; + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp0, FIX(1.373119086)) + /* c2 */ + MULTIPLY(tmp1, FIX(1.058554052)) + /* c6 */ + MULTIPLY(tmp2, FIX(0.501487041)) - /* c10 */ + MULTIPLY(tmp3, FIX(0.170464608)) - /* c12 */ + MULTIPLY(tmp4, FIX(0.803364869)) - /* c8 */ + MULTIPLY(tmp5, FIX(1.252223920)), /* c4 */ + CONST_BITS); + z1 = MULTIPLY(tmp0 - tmp2, FIX(1.155388986)) - /* (c4+c6)/2 */ + MULTIPLY(tmp3 - tmp4, FIX(0.435816023)) - /* (c2-c10)/2 */ + MULTIPLY(tmp1 - tmp5, FIX(0.316450131)); /* (c8-c12)/2 */ + z2 = MULTIPLY(tmp0 + tmp2, FIX(0.096834934)) - /* (c4-c6)/2 */ + MULTIPLY(tmp3 + tmp4, FIX(0.937303064)) + /* (c2+c10)/2 */ + MULTIPLY(tmp1 + tmp5, FIX(0.486914739)); /* (c8+c12)/2 */ + + dataptr[4] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS); + dataptr[6] = (DCTELEM) DESCALE(z1 - z2, CONST_BITS); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.322312651)); /* c3 */ + tmp2 = MULTIPLY(tmp10 + tmp12, FIX(1.163874945)); /* c5 */ + tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.937797057)) + /* c7 */ + MULTIPLY(tmp14 + tmp15, FIX(0.338443458)); /* c11 */ + tmp0 = tmp1 + tmp2 + tmp3 - + MULTIPLY(tmp10, FIX(2.020082300)) + /* c3+c5+c7-c1 */ + MULTIPLY(tmp14, FIX(0.318774355)); /* c9-c11 */ + tmp4 = MULTIPLY(tmp14 - tmp15, FIX(0.937797057)) - /* c7 */ + MULTIPLY(tmp11 + tmp12, FIX(0.338443458)); /* c11 */ + tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(1.163874945)); /* -c5 */ + tmp1 += tmp4 + tmp5 + + MULTIPLY(tmp11, FIX(0.837223564)) - /* c5+c9+c11-c3 */ + MULTIPLY(tmp14, FIX(2.341699410)); /* c1+c7 */ + tmp6 = MULTIPLY(tmp12 + tmp13, - FIX(0.657217813)); /* -c9 */ + tmp2 += tmp4 + tmp6 - + MULTIPLY(tmp12, FIX(1.572116027)) + /* c1+c5-c9-c11 */ + MULTIPLY(tmp15, FIX(2.260109708)); /* c3+c7 */ + tmp3 += tmp5 + tmp6 + + MULTIPLY(tmp13, FIX(2.205608352)) - /* c3+c5+c9-c7 */ + MULTIPLY(tmp15, FIX(1.742345811)); /* c1+c11 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp3, CONST_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 13) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/13)**2 = 64/169, which we partially + * fold into the constant multipliers and final shifting: + * cK now represents sqrt(2) * cos(K*pi/26) * 128/169. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*4]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*3]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*2]; + tmp3 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*1]; + tmp4 = dataptr[DCTSIZE*4] + wsptr[DCTSIZE*0]; + tmp5 = dataptr[DCTSIZE*5] + dataptr[DCTSIZE*7]; + tmp6 = dataptr[DCTSIZE*6]; + + tmp10 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*4]; + tmp11 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*3]; + tmp12 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*2]; + tmp13 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*1]; + tmp14 = dataptr[DCTSIZE*4] - wsptr[DCTSIZE*0]; + tmp15 = dataptr[DCTSIZE*5] - dataptr[DCTSIZE*7]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 + tmp6, + FIX(0.757396450)), /* 128/169 */ + CONST_BITS+1); + tmp6 += tmp6; + tmp0 -= tmp6; + tmp1 -= tmp6; + tmp2 -= tmp6; + tmp3 -= tmp6; + tmp4 -= tmp6; + tmp5 -= tmp6; + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp0, FIX(1.039995521)) + /* c2 */ + MULTIPLY(tmp1, FIX(0.801745081)) + /* c6 */ + MULTIPLY(tmp2, FIX(0.379824504)) - /* c10 */ + MULTIPLY(tmp3, FIX(0.129109289)) - /* c12 */ + MULTIPLY(tmp4, FIX(0.608465700)) - /* c8 */ + MULTIPLY(tmp5, FIX(0.948429952)), /* c4 */ + CONST_BITS+1); + z1 = MULTIPLY(tmp0 - tmp2, FIX(0.875087516)) - /* (c4+c6)/2 */ + MULTIPLY(tmp3 - tmp4, FIX(0.330085509)) - /* (c2-c10)/2 */ + MULTIPLY(tmp1 - tmp5, FIX(0.239678205)); /* (c8-c12)/2 */ + z2 = MULTIPLY(tmp0 + tmp2, FIX(0.073342435)) - /* (c4-c6)/2 */ + MULTIPLY(tmp3 + tmp4, FIX(0.709910013)) + /* (c2+c10)/2 */ + MULTIPLY(tmp1 + tmp5, FIX(0.368787494)); /* (c8+c12)/2 */ + + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 - z2, CONST_BITS+1); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.001514908)); /* c3 */ + tmp2 = MULTIPLY(tmp10 + tmp12, FIX(0.881514751)); /* c5 */ + tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.710284161)) + /* c7 */ + MULTIPLY(tmp14 + tmp15, FIX(0.256335874)); /* c11 */ + tmp0 = tmp1 + tmp2 + tmp3 - + MULTIPLY(tmp10, FIX(1.530003162)) + /* c3+c5+c7-c1 */ + MULTIPLY(tmp14, FIX(0.241438564)); /* c9-c11 */ + tmp4 = MULTIPLY(tmp14 - tmp15, FIX(0.710284161)) - /* c7 */ + MULTIPLY(tmp11 + tmp12, FIX(0.256335874)); /* c11 */ + tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(0.881514751)); /* -c5 */ + tmp1 += tmp4 + tmp5 + + MULTIPLY(tmp11, FIX(0.634110155)) - /* c5+c9+c11-c3 */ + MULTIPLY(tmp14, FIX(1.773594819)); /* c1+c7 */ + tmp6 = MULTIPLY(tmp12 + tmp13, - FIX(0.497774438)); /* -c9 */ + tmp2 += tmp4 + tmp6 - + MULTIPLY(tmp12, FIX(1.190715098)) + /* c1+c5-c9-c11 */ + MULTIPLY(tmp15, FIX(1.711799069)); /* c3+c7 */ + tmp3 += tmp5 + tmp6 + + MULTIPLY(tmp13, FIX(1.670519935)) - /* c3+c5+c9-c7 */ + MULTIPLY(tmp15, FIX(1.319646532)); /* c1+c11 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+1); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+1); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 14x14 sample block. + */ + +GLOBAL(void) +jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + DCTELEM workspace[8*6]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT. */ + /* cK represents sqrt(2) * cos(K*pi/28). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[13]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[12]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[11]); + tmp13 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[10]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[9]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[8]); + tmp6 = GETJSAMPLE(elemptr[6]) + GETJSAMPLE(elemptr[7]); + + tmp10 = tmp0 + tmp6; + tmp14 = tmp0 - tmp6; + tmp11 = tmp1 + tmp5; + tmp15 = tmp1 - tmp5; + tmp12 = tmp2 + tmp4; + tmp16 = tmp2 - tmp4; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[13]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[12]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[11]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[10]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[9]); + tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[8]); + tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[7]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + (tmp10 + tmp11 + tmp12 + tmp13 - 14 * CENTERJSAMPLE); + tmp13 += tmp13; + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.274162392)) + /* c4 */ + MULTIPLY(tmp11 - tmp13, FIX(0.314692123)) - /* c12 */ + MULTIPLY(tmp12 - tmp13, FIX(0.881747734)), /* c8 */ + CONST_BITS); + + tmp10 = MULTIPLY(tmp14 + tmp15, FIX(1.105676686)); /* c6 */ + + dataptr[2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.273079590)) /* c2-c6 */ + + MULTIPLY(tmp16, FIX(0.613604268)), /* c10 */ + CONST_BITS); + dataptr[6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.719280954)) /* c6+c10 */ + - MULTIPLY(tmp16, FIX(1.378756276)), /* c2 */ + CONST_BITS); + + /* Odd part */ + + tmp10 = tmp1 + tmp2; + tmp11 = tmp5 - tmp4; + dataptr[7] = (DCTELEM) (tmp0 - tmp10 + tmp3 - tmp11 - tmp6); + tmp3 <<= CONST_BITS; + tmp10 = MULTIPLY(tmp10, - FIX(0.158341681)); /* -c13 */ + tmp11 = MULTIPLY(tmp11, FIX(1.405321284)); /* c1 */ + tmp10 += tmp11 - tmp3; + tmp11 = MULTIPLY(tmp0 + tmp2, FIX(1.197448846)) + /* c5 */ + MULTIPLY(tmp4 + tmp6, FIX(0.752406978)); /* c9 */ + dataptr[5] = (DCTELEM) + DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(2.373959773)) /* c3+c5-c13 */ + + MULTIPLY(tmp4, FIX(1.119999435)), /* c1+c11-c9 */ + CONST_BITS); + tmp12 = MULTIPLY(tmp0 + tmp1, FIX(1.334852607)) + /* c3 */ + MULTIPLY(tmp5 - tmp6, FIX(0.467085129)); /* c11 */ + dataptr[3] = (DCTELEM) + DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.424103948)) /* c3-c9-c13 */ + - MULTIPLY(tmp5, FIX(3.069855259)), /* c1+c5+c11 */ + CONST_BITS); + dataptr[1] = (DCTELEM) + DESCALE(tmp11 + tmp12 + tmp3 + tmp6 - + MULTIPLY(tmp0 + tmp6, FIX(1.126980169)), /* c3+c5-c1 */ + CONST_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 14) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/14)**2 = 16/49, which we partially + * fold into the constant multipliers and final shifting: + * cK now represents sqrt(2) * cos(K*pi/28) * 32/49. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*3]; + tmp13 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*2]; + tmp4 = dataptr[DCTSIZE*4] + wsptr[DCTSIZE*1]; + tmp5 = dataptr[DCTSIZE*5] + wsptr[DCTSIZE*0]; + tmp6 = dataptr[DCTSIZE*6] + dataptr[DCTSIZE*7]; + + tmp10 = tmp0 + tmp6; + tmp14 = tmp0 - tmp6; + tmp11 = tmp1 + tmp5; + tmp15 = tmp1 - tmp5; + tmp12 = tmp2 + tmp4; + tmp16 = tmp2 - tmp4; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*3]; + tmp3 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*2]; + tmp4 = dataptr[DCTSIZE*4] - wsptr[DCTSIZE*1]; + tmp5 = dataptr[DCTSIZE*5] - wsptr[DCTSIZE*0]; + tmp6 = dataptr[DCTSIZE*6] - dataptr[DCTSIZE*7]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12 + tmp13, + FIX(0.653061224)), /* 32/49 */ + CONST_BITS+1); + tmp13 += tmp13; + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(0.832106052)) + /* c4 */ + MULTIPLY(tmp11 - tmp13, FIX(0.205513223)) - /* c12 */ + MULTIPLY(tmp12 - tmp13, FIX(0.575835255)), /* c8 */ + CONST_BITS+1); + + tmp10 = MULTIPLY(tmp14 + tmp15, FIX(0.722074570)); /* c6 */ + + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.178337691)) /* c2-c6 */ + + MULTIPLY(tmp16, FIX(0.400721155)), /* c10 */ + CONST_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.122795725)) /* c6+c10 */ + - MULTIPLY(tmp16, FIX(0.900412262)), /* c2 */ + CONST_BITS+1); + + /* Odd part */ + + tmp10 = tmp1 + tmp2; + tmp11 = tmp5 - tmp4; + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp10 + tmp3 - tmp11 - tmp6, + FIX(0.653061224)), /* 32/49 */ + CONST_BITS+1); + tmp3 = MULTIPLY(tmp3 , FIX(0.653061224)); /* 32/49 */ + tmp10 = MULTIPLY(tmp10, - FIX(0.103406812)); /* -c13 */ + tmp11 = MULTIPLY(tmp11, FIX(0.917760839)); /* c1 */ + tmp10 += tmp11 - tmp3; + tmp11 = MULTIPLY(tmp0 + tmp2, FIX(0.782007410)) + /* c5 */ + MULTIPLY(tmp4 + tmp6, FIX(0.491367823)); /* c9 */ + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(1.550341076)) /* c3+c5-c13 */ + + MULTIPLY(tmp4, FIX(0.731428202)), /* c1+c11-c9 */ + CONST_BITS+1); + tmp12 = MULTIPLY(tmp0 + tmp1, FIX(0.871740478)) + /* c3 */ + MULTIPLY(tmp5 - tmp6, FIX(0.305035186)); /* c11 */ + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.276965844)) /* c3-c9-c13 */ + - MULTIPLY(tmp5, FIX(2.004803435)), /* c1+c5+c11 */ + CONST_BITS+1); + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp11 + tmp12 + tmp3 + - MULTIPLY(tmp0, FIX(0.735987049)) /* c3+c5-c1 */ + - MULTIPLY(tmp6, FIX(0.082925825)), /* c9-c11-c13 */ + CONST_BITS+1); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 15x15 sample block. + */ + +GLOBAL(void) +jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 z1, z2, z3; + DCTELEM workspace[8*7]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT. */ + /* cK represents sqrt(2) * cos(K*pi/30). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[14]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[13]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[12]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[11]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[10]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[9]); + tmp6 = GETJSAMPLE(elemptr[6]) + GETJSAMPLE(elemptr[8]); + tmp7 = GETJSAMPLE(elemptr[7]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[14]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[13]); + tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[12]); + tmp13 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[11]); + tmp14 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[10]); + tmp15 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[9]); + tmp16 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[8]); + + z1 = tmp0 + tmp4 + tmp5; + z2 = tmp1 + tmp3 + tmp6; + z3 = tmp2 + tmp7; + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) (z1 + z2 + z3 - 15 * CENTERJSAMPLE); + z3 += z3; + dataptr[6] = (DCTELEM) + DESCALE(MULTIPLY(z1 - z3, FIX(1.144122806)) - /* c6 */ + MULTIPLY(z2 - z3, FIX(0.437016024)), /* c12 */ + CONST_BITS); + tmp2 += ((tmp1 + tmp4) >> 1) - tmp7 - tmp7; + z1 = MULTIPLY(tmp3 - tmp2, FIX(1.531135173)) - /* c2+c14 */ + MULTIPLY(tmp6 - tmp2, FIX(2.238241955)); /* c4+c8 */ + z2 = MULTIPLY(tmp5 - tmp2, FIX(0.798468008)) - /* c8-c14 */ + MULTIPLY(tmp0 - tmp2, FIX(0.091361227)); /* c2-c4 */ + z3 = MULTIPLY(tmp0 - tmp3, FIX(1.383309603)) + /* c2 */ + MULTIPLY(tmp6 - tmp5, FIX(0.946293579)) + /* c8 */ + MULTIPLY(tmp1 - tmp4, FIX(0.790569415)); /* (c6+c12)/2 */ + + dataptr[2] = (DCTELEM) DESCALE(z1 + z3, CONST_BITS); + dataptr[4] = (DCTELEM) DESCALE(z2 + z3, CONST_BITS); + + /* Odd part */ + + tmp2 = MULTIPLY(tmp10 - tmp12 - tmp13 + tmp15 + tmp16, + FIX(1.224744871)); /* c5 */ + tmp1 = MULTIPLY(tmp10 - tmp14 - tmp15, FIX(1.344997024)) + /* c3 */ + MULTIPLY(tmp11 - tmp13 - tmp16, FIX(0.831253876)); /* c9 */ + tmp12 = MULTIPLY(tmp12, FIX(1.224744871)); /* c5 */ + tmp4 = MULTIPLY(tmp10 - tmp16, FIX(1.406466353)) + /* c1 */ + MULTIPLY(tmp11 + tmp14, FIX(1.344997024)) + /* c3 */ + MULTIPLY(tmp13 + tmp15, FIX(0.575212477)); /* c11 */ + tmp0 = MULTIPLY(tmp13, FIX(0.475753014)) - /* c7-c11 */ + MULTIPLY(tmp14, FIX(0.513743148)) + /* c3-c9 */ + MULTIPLY(tmp16, FIX(1.700497885)) + tmp4 + tmp12; /* c1+c13 */ + tmp3 = MULTIPLY(tmp10, - FIX(0.355500862)) - /* -(c1-c7) */ + MULTIPLY(tmp11, FIX(2.176250899)) - /* c3+c9 */ + MULTIPLY(tmp15, FIX(0.869244010)) + tmp4 - tmp12; /* c11+c13 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp3, CONST_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 15) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/15)**2 = 64/225, which we partially + * fold into the constant multipliers and final shifting: + * cK now represents sqrt(2) * cos(K*pi/30) * 256/225. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*6]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*5]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*4]; + tmp3 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*3]; + tmp4 = dataptr[DCTSIZE*4] + wsptr[DCTSIZE*2]; + tmp5 = dataptr[DCTSIZE*5] + wsptr[DCTSIZE*1]; + tmp6 = dataptr[DCTSIZE*6] + wsptr[DCTSIZE*0]; + tmp7 = dataptr[DCTSIZE*7]; + + tmp10 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*6]; + tmp11 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*5]; + tmp12 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*4]; + tmp13 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*3]; + tmp14 = dataptr[DCTSIZE*4] - wsptr[DCTSIZE*2]; + tmp15 = dataptr[DCTSIZE*5] - wsptr[DCTSIZE*1]; + tmp16 = dataptr[DCTSIZE*6] - wsptr[DCTSIZE*0]; + + z1 = tmp0 + tmp4 + tmp5; + z2 = tmp1 + tmp3 + tmp6; + z3 = tmp2 + tmp7; + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(z1 + z2 + z3, FIX(1.137777778)), /* 256/225 */ + CONST_BITS+2); + z3 += z3; + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(MULTIPLY(z1 - z3, FIX(1.301757503)) - /* c6 */ + MULTIPLY(z2 - z3, FIX(0.497227121)), /* c12 */ + CONST_BITS+2); + tmp2 += ((tmp1 + tmp4) >> 1) - tmp7 - tmp7; + z1 = MULTIPLY(tmp3 - tmp2, FIX(1.742091575)) - /* c2+c14 */ + MULTIPLY(tmp6 - tmp2, FIX(2.546621957)); /* c4+c8 */ + z2 = MULTIPLY(tmp5 - tmp2, FIX(0.908479156)) - /* c8-c14 */ + MULTIPLY(tmp0 - tmp2, FIX(0.103948774)); /* c2-c4 */ + z3 = MULTIPLY(tmp0 - tmp3, FIX(1.573898926)) + /* c2 */ + MULTIPLY(tmp6 - tmp5, FIX(1.076671805)) + /* c8 */ + MULTIPLY(tmp1 - tmp4, FIX(0.899492312)); /* (c6+c12)/2 */ + + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z3, CONST_BITS+2); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(z2 + z3, CONST_BITS+2); + + /* Odd part */ + + tmp2 = MULTIPLY(tmp10 - tmp12 - tmp13 + tmp15 + tmp16, + FIX(1.393487498)); /* c5 */ + tmp1 = MULTIPLY(tmp10 - tmp14 - tmp15, FIX(1.530307725)) + /* c3 */ + MULTIPLY(tmp11 - tmp13 - tmp16, FIX(0.945782187)); /* c9 */ + tmp12 = MULTIPLY(tmp12, FIX(1.393487498)); /* c5 */ + tmp4 = MULTIPLY(tmp10 - tmp16, FIX(1.600246161)) + /* c1 */ + MULTIPLY(tmp11 + tmp14, FIX(1.530307725)) + /* c3 */ + MULTIPLY(tmp13 + tmp15, FIX(0.654463974)); /* c11 */ + tmp0 = MULTIPLY(tmp13, FIX(0.541301207)) - /* c7-c11 */ + MULTIPLY(tmp14, FIX(0.584525538)) + /* c3-c9 */ + MULTIPLY(tmp16, FIX(1.934788705)) + tmp4 + tmp12; /* c1+c13 */ + tmp3 = MULTIPLY(tmp10, - FIX(0.404480980)) - /* -(c1-c7) */ + MULTIPLY(tmp11, FIX(2.476089912)) - /* c3+c9 */ + MULTIPLY(tmp15, FIX(0.989006518)) + tmp4 - tmp12; /* c11+c13 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+2); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+2); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+2); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+2); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 16x16 sample block. + */ + +GLOBAL(void) +jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17; + DCTELEM workspace[DCTSIZE2]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* cK represents sqrt(2) * cos(K*pi/32). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[15]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[14]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[13]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[12]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[11]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[10]); + tmp6 = GETJSAMPLE(elemptr[6]) + GETJSAMPLE(elemptr[9]); + tmp7 = GETJSAMPLE(elemptr[7]) + GETJSAMPLE(elemptr[8]); + + tmp10 = tmp0 + tmp7; + tmp14 = tmp0 - tmp7; + tmp11 = tmp1 + tmp6; + tmp15 = tmp1 - tmp6; + tmp12 = tmp2 + tmp5; + tmp16 = tmp2 - tmp5; + tmp13 = tmp3 + tmp4; + tmp17 = tmp3 - tmp4; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[15]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[14]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[13]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[12]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[11]); + tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[10]); + tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[9]); + tmp7 = GETJSAMPLE(elemptr[7]) - GETJSAMPLE(elemptr[8]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */ + MULTIPLY(tmp11 - tmp12, FIX_0_541196100), /* c12[16] = c6[8] */ + CONST_BITS-PASS1_BITS); + + tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) + /* c14[16] = c7[8] */ + MULTIPLY(tmp14 - tmp16, FIX(1.387039845)); /* c2[16] = c1[8] */ + + dataptr[2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982)) /* c6+c14 */ + + MULTIPLY(tmp16, FIX(2.172734804)), /* c2+c10 */ + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243)) /* c2-c6 */ + - MULTIPLY(tmp17, FIX(1.061594338)), /* c10+c14 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) + /* c3 */ + MULTIPLY(tmp6 - tmp7, FIX(0.410524528)); /* c13 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) + /* c5 */ + MULTIPLY(tmp5 + tmp7, FIX(0.666655658)); /* c11 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) + /* c7 */ + MULTIPLY(tmp4 - tmp7, FIX(0.897167586)); /* c9 */ + tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) + /* c15 */ + MULTIPLY(tmp6 - tmp5, FIX(1.407403738)); /* c1 */ + tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) + /* -c11 */ + MULTIPLY(tmp4 + tmp6, - FIX(1.247225013)); /* -c5 */ + tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) + /* -c3 */ + MULTIPLY(tmp5 - tmp4, FIX(0.410524528)); /* c13 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(tmp0, FIX(2.286341144)) + /* c7+c5+c3-c1 */ + MULTIPLY(tmp7, FIX(0.779653625)); /* c15+c13-c11+c9 */ + tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */ + - MULTIPLY(tmp6, FIX(1.663905119)); /* c7+c13+c1-c5 */ + tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */ + + MULTIPLY(tmp5, FIX(1.227391138)); /* c9-c11+c1-c13 */ + tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */ + + MULTIPLY(tmp4, FIX(2.167985692)); /* c1+c13+c5-c9 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp12, CONST_BITS-PASS1_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp13, CONST_BITS-PASS1_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == DCTSIZE * 2) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/16)**2 = 1/2**2. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*4] + wsptr[DCTSIZE*3]; + tmp5 = dataptr[DCTSIZE*5] + wsptr[DCTSIZE*2]; + tmp6 = dataptr[DCTSIZE*6] + wsptr[DCTSIZE*1]; + tmp7 = dataptr[DCTSIZE*7] + wsptr[DCTSIZE*0]; + + tmp10 = tmp0 + tmp7; + tmp14 = tmp0 - tmp7; + tmp11 = tmp1 + tmp6; + tmp15 = tmp1 - tmp6; + tmp12 = tmp2 + tmp5; + tmp16 = tmp2 - tmp5; + tmp13 = tmp3 + tmp4; + tmp17 = tmp3 - tmp4; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*4] - wsptr[DCTSIZE*3]; + tmp5 = dataptr[DCTSIZE*5] - wsptr[DCTSIZE*2]; + tmp6 = dataptr[DCTSIZE*6] - wsptr[DCTSIZE*1]; + tmp7 = dataptr[DCTSIZE*7] - wsptr[DCTSIZE*0]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS1_BITS+2); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */ + MULTIPLY(tmp11 - tmp12, FIX_0_541196100), /* c12[16] = c6[8] */ + CONST_BITS+PASS1_BITS+2); + + tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) + /* c14[16] = c7[8] */ + MULTIPLY(tmp14 - tmp16, FIX(1.387039845)); /* c2[16] = c1[8] */ + + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982)) /* c6+c14 */ + + MULTIPLY(tmp16, FIX(2.172734804)), /* c2+10 */ + CONST_BITS+PASS1_BITS+2); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243)) /* c2-c6 */ + - MULTIPLY(tmp17, FIX(1.061594338)), /* c10+c14 */ + CONST_BITS+PASS1_BITS+2); + + /* Odd part */ + + tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) + /* c3 */ + MULTIPLY(tmp6 - tmp7, FIX(0.410524528)); /* c13 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) + /* c5 */ + MULTIPLY(tmp5 + tmp7, FIX(0.666655658)); /* c11 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) + /* c7 */ + MULTIPLY(tmp4 - tmp7, FIX(0.897167586)); /* c9 */ + tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) + /* c15 */ + MULTIPLY(tmp6 - tmp5, FIX(1.407403738)); /* c1 */ + tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) + /* -c11 */ + MULTIPLY(tmp4 + tmp6, - FIX(1.247225013)); /* -c5 */ + tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) + /* -c3 */ + MULTIPLY(tmp5 - tmp4, FIX(0.410524528)); /* c13 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(tmp0, FIX(2.286341144)) + /* c7+c5+c3-c1 */ + MULTIPLY(tmp7, FIX(0.779653625)); /* c15+c13-c11+c9 */ + tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */ + - MULTIPLY(tmp6, FIX(1.663905119)); /* c7+c13+c1-c5 */ + tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */ + + MULTIPLY(tmp5, FIX(1.227391138)); /* c9-c11+c1-c13 */ + tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */ + + MULTIPLY(tmp4, FIX(2.167985692)); /* c1+c13+c5-c9 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS1_BITS+2); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS1_BITS+2); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS1_BITS+2); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS1_BITS+2); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 16x8 sample block. + * + * 16-point FDCT in pass 1 (rows), 8-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17; + INT32 z1; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 16-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/32). */ + + dataptr = data; + ctr = 0; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[15]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[14]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[13]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[12]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[11]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[10]); + tmp6 = GETJSAMPLE(elemptr[6]) + GETJSAMPLE(elemptr[9]); + tmp7 = GETJSAMPLE(elemptr[7]) + GETJSAMPLE(elemptr[8]); + + tmp10 = tmp0 + tmp7; + tmp14 = tmp0 - tmp7; + tmp11 = tmp1 + tmp6; + tmp15 = tmp1 - tmp6; + tmp12 = tmp2 + tmp5; + tmp16 = tmp2 - tmp5; + tmp13 = tmp3 + tmp4; + tmp17 = tmp3 - tmp4; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[15]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[14]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[13]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[12]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[11]); + tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[10]); + tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[9]); + tmp7 = GETJSAMPLE(elemptr[7]) - GETJSAMPLE(elemptr[8]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */ + MULTIPLY(tmp11 - tmp12, FIX_0_541196100), /* c12[16] = c6[8] */ + CONST_BITS-PASS1_BITS); + + tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) + /* c14[16] = c7[8] */ + MULTIPLY(tmp14 - tmp16, FIX(1.387039845)); /* c2[16] = c1[8] */ + + dataptr[2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982)) /* c6+c14 */ + + MULTIPLY(tmp16, FIX(2.172734804)), /* c2+c10 */ + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243)) /* c2-c6 */ + - MULTIPLY(tmp17, FIX(1.061594338)), /* c10+c14 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) + /* c3 */ + MULTIPLY(tmp6 - tmp7, FIX(0.410524528)); /* c13 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) + /* c5 */ + MULTIPLY(tmp5 + tmp7, FIX(0.666655658)); /* c11 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) + /* c7 */ + MULTIPLY(tmp4 - tmp7, FIX(0.897167586)); /* c9 */ + tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) + /* c15 */ + MULTIPLY(tmp6 - tmp5, FIX(1.407403738)); /* c1 */ + tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) + /* -c11 */ + MULTIPLY(tmp4 + tmp6, - FIX(1.247225013)); /* -c5 */ + tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) + /* -c3 */ + MULTIPLY(tmp5 - tmp4, FIX(0.410524528)); /* c13 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(tmp0, FIX(2.286341144)) + /* c7+c5+c3-c1 */ + MULTIPLY(tmp7, FIX(0.779653625)); /* c15+c13-c11+c9 */ + tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */ + - MULTIPLY(tmp6, FIX(1.663905119)); /* c7+c13+c1-c5 */ + tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */ + + MULTIPLY(tmp5, FIX(1.227391138)); /* c9-c11+c1-c13 */ + tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */ + + MULTIPLY(tmp4, FIX(2.167985692)); /* c1+c13+c5-c9 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp12, CONST_BITS-PASS1_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp13, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by 8/16 = 1/2. + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; + + tmp10 = tmp0 + tmp3; + tmp12 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp13 = tmp1 - tmp2; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; + + dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS+1); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS+1); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865), + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065), + CONST_BITS+PASS1_BITS+1); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + * i0..i3 in the paper are tmp0..tmp3 here. + */ + + tmp10 = tmp0 + tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp0 + tmp2; + tmp13 = tmp1 + tmp3; + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + + tmp0 = MULTIPLY(tmp0, FIX_1_501321110); /* c1+c3-c5-c7 */ + tmp1 = MULTIPLY(tmp1, FIX_3_072711026); /* c1+c3+c5-c7 */ + tmp2 = MULTIPLY(tmp2, FIX_2_053119869); /* c1+c3-c5+c7 */ + tmp3 = MULTIPLY(tmp3, FIX_0_298631336); /* -c1+c3+c5-c7 */ + tmp10 = MULTIPLY(tmp10, - FIX_0_899976223); /* c7-c3 */ + tmp11 = MULTIPLY(tmp11, - FIX_2_562915447); /* -c1-c3 */ + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* c5-c3 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + + tmp12 += z1; + tmp13 += z1; + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0 + tmp10 + tmp12, + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1 + tmp11 + tmp13, + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2 + tmp11 + tmp12, + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3 + tmp10 + tmp13, + CONST_BITS+PASS1_BITS+1); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 14x7 sample block. + * + * 14-point FDCT in pass 1 (rows), 7-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 z1, z2, z3; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Zero bottom row of output coefficient block. */ + MEMZERO(&data[DCTSIZE*7], SIZEOF(DCTELEM) * DCTSIZE); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 14-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/28). */ + + dataptr = data; + for (ctr = 0; ctr < 7; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[13]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[12]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[11]); + tmp13 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[10]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[9]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[8]); + tmp6 = GETJSAMPLE(elemptr[6]) + GETJSAMPLE(elemptr[7]); + + tmp10 = tmp0 + tmp6; + tmp14 = tmp0 - tmp6; + tmp11 = tmp1 + tmp5; + tmp15 = tmp1 - tmp5; + tmp12 = tmp2 + tmp4; + tmp16 = tmp2 - tmp4; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[13]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[12]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[11]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[10]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[9]); + tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[8]); + tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[7]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 + tmp12 + tmp13 - 14 * CENTERJSAMPLE) << PASS1_BITS); + tmp13 += tmp13; + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.274162392)) + /* c4 */ + MULTIPLY(tmp11 - tmp13, FIX(0.314692123)) - /* c12 */ + MULTIPLY(tmp12 - tmp13, FIX(0.881747734)), /* c8 */ + CONST_BITS-PASS1_BITS); + + tmp10 = MULTIPLY(tmp14 + tmp15, FIX(1.105676686)); /* c6 */ + + dataptr[2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.273079590)) /* c2-c6 */ + + MULTIPLY(tmp16, FIX(0.613604268)), /* c10 */ + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.719280954)) /* c6+c10 */ + - MULTIPLY(tmp16, FIX(1.378756276)), /* c2 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp10 = tmp1 + tmp2; + tmp11 = tmp5 - tmp4; + dataptr[7] = (DCTELEM) ((tmp0 - tmp10 + tmp3 - tmp11 - tmp6) << PASS1_BITS); + tmp3 <<= CONST_BITS; + tmp10 = MULTIPLY(tmp10, - FIX(0.158341681)); /* -c13 */ + tmp11 = MULTIPLY(tmp11, FIX(1.405321284)); /* c1 */ + tmp10 += tmp11 - tmp3; + tmp11 = MULTIPLY(tmp0 + tmp2, FIX(1.197448846)) + /* c5 */ + MULTIPLY(tmp4 + tmp6, FIX(0.752406978)); /* c9 */ + dataptr[5] = (DCTELEM) + DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(2.373959773)) /* c3+c5-c13 */ + + MULTIPLY(tmp4, FIX(1.119999435)), /* c1+c11-c9 */ + CONST_BITS-PASS1_BITS); + tmp12 = MULTIPLY(tmp0 + tmp1, FIX(1.334852607)) + /* c3 */ + MULTIPLY(tmp5 - tmp6, FIX(0.467085129)); /* c11 */ + dataptr[3] = (DCTELEM) + DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.424103948)) /* c3-c9-c13 */ + - MULTIPLY(tmp5, FIX(3.069855259)), /* c1+c5+c11 */ + CONST_BITS-PASS1_BITS); + dataptr[1] = (DCTELEM) + DESCALE(tmp11 + tmp12 + tmp3 + tmp6 - + MULTIPLY(tmp0 + tmp6, FIX(1.126980169)), /* c3+c5-c1 */ + CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/14)*(8/7) = 32/49, which we + * partially fold into the constant multipliers and final shifting: + * 7-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/14) * 64/49. + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*6]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*5]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*4]; + tmp3 = dataptr[DCTSIZE*3]; + + tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*6]; + tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*5]; + tmp12 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*4]; + + z1 = tmp0 + tmp2; + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(z1 + tmp1 + tmp3, FIX(1.306122449)), /* 64/49 */ + CONST_BITS+PASS1_BITS+1); + tmp3 += tmp3; + z1 -= tmp3; + z1 -= tmp3; + z1 = MULTIPLY(z1, FIX(0.461784020)); /* (c2+c6-c4)/2 */ + z2 = MULTIPLY(tmp0 - tmp2, FIX(1.202428084)); /* (c2+c4-c6)/2 */ + z3 = MULTIPLY(tmp1 - tmp2, FIX(0.411026446)); /* c6 */ + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS+PASS1_BITS+1); + z1 -= z2; + z2 = MULTIPLY(tmp0 - tmp1, FIX(1.151670509)); /* c4 */ + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.923568041)), /* c2+c6-c4 */ + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS1_BITS+1); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.221765677)); /* (c3+c1-c5)/2 */ + tmp2 = MULTIPLY(tmp10 - tmp11, FIX(0.222383464)); /* (c3+c5-c1)/2 */ + tmp0 = tmp1 - tmp2; + tmp1 += tmp2; + tmp2 = MULTIPLY(tmp11 + tmp12, - FIX(1.800824523)); /* -c1 */ + tmp1 += tmp2; + tmp3 = MULTIPLY(tmp10 + tmp12, FIX(0.801442310)); /* c5 */ + tmp0 += tmp3; + tmp2 += tmp3 + MULTIPLY(tmp12, FIX(2.443531355)); /* c3+c1-c5 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+PASS1_BITS+1); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 12x6 sample block. + * + * 12-point FDCT in pass 1 (rows), 6-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Zero 2 bottom rows of output coefficient block. */ + MEMZERO(&data[DCTSIZE*6], SIZEOF(DCTELEM) * DCTSIZE * 2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 12-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/24). */ + + dataptr = data; + for (ctr = 0; ctr < 6; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[11]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[10]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[9]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[8]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[7]); + tmp5 = GETJSAMPLE(elemptr[5]) + GETJSAMPLE(elemptr[6]); + + tmp10 = tmp0 + tmp5; + tmp13 = tmp0 - tmp5; + tmp11 = tmp1 + tmp4; + tmp14 = tmp1 - tmp4; + tmp12 = tmp2 + tmp3; + tmp15 = tmp2 - tmp3; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[11]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[10]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[9]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[8]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[7]); + tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[6]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 + tmp12 - 12 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[6] = (DCTELEM) ((tmp13 - tmp14 - tmp15) << PASS1_BITS); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.224744871)), /* c4 */ + CONST_BITS-PASS1_BITS); + dataptr[2] = (DCTELEM) + DESCALE(tmp14 - tmp15 + MULTIPLY(tmp13 + tmp15, FIX(1.366025404)), /* c2 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp1 + tmp4, FIX_0_541196100); /* c9 */ + tmp14 = tmp10 + MULTIPLY(tmp1, FIX_0_765366865); /* c3-c9 */ + tmp15 = tmp10 - MULTIPLY(tmp4, FIX_1_847759065); /* c3+c9 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.121971054)); /* c5 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.860918669)); /* c7 */ + tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.580774953)) /* c5+c7-c1 */ + + MULTIPLY(tmp5, FIX(0.184591911)); /* c11 */ + tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.184591911)); /* -c11 */ + tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.339493912)) /* c1+c5-c11 */ + + MULTIPLY(tmp5, FIX(0.860918669)); /* c7 */ + tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.725788011)) /* c1+c11-c7 */ + - MULTIPLY(tmp5, FIX(1.121971054)); /* c5 */ + tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.306562965)) /* c3 */ + - MULTIPLY(tmp2 + tmp5, FIX_0_541196100); /* c9 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp12, CONST_BITS-PASS1_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp13, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/12)*(8/6) = 8/9, which we + * partially fold into the constant multipliers and final shifting: + * 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12) * 16/9. + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*5]; + tmp11 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*3]; + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*3]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp12, FIX(2.177324216)), /* c2 */ + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */ + CONST_BITS+PASS1_BITS+1); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp2, FIX(0.650711829)); /* c5 */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0 + tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS+1); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 10x5 sample block. + * + * 10-point FDCT in pass 1 (rows), 5-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Zero 3 bottom rows of output coefficient block. */ + MEMZERO(&data[DCTSIZE*5], SIZEOF(DCTELEM) * DCTSIZE * 3); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 10-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/20). */ + + dataptr = data; + for (ctr = 0; ctr < 5; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[9]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[8]); + tmp12 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[7]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[6]); + tmp4 = GETJSAMPLE(elemptr[4]) + GETJSAMPLE(elemptr[5]); + + tmp10 = tmp0 + tmp4; + tmp13 = tmp0 - tmp4; + tmp11 = tmp1 + tmp3; + tmp14 = tmp1 - tmp3; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[9]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[8]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[7]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[6]); + tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[5]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE) << PASS1_BITS); + tmp12 += tmp12; + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.144122806)) - /* c4 */ + MULTIPLY(tmp11 - tmp12, FIX(0.437016024)), /* c8 */ + CONST_BITS-PASS1_BITS); + tmp10 = MULTIPLY(tmp13 + tmp14, FIX(0.831253876)); /* c6 */ + dataptr[2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.513743148)), /* c2-c6 */ + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.176250899)), /* c2+c6 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp10 = tmp0 + tmp4; + tmp11 = tmp1 - tmp3; + dataptr[5] = (DCTELEM) ((tmp10 - tmp11 - tmp2) << PASS1_BITS); + tmp2 <<= CONST_BITS; + dataptr[1] = (DCTELEM) + DESCALE(MULTIPLY(tmp0, FIX(1.396802247)) + /* c1 */ + MULTIPLY(tmp1, FIX(1.260073511)) + tmp2 + /* c3 */ + MULTIPLY(tmp3, FIX(0.642039522)) + /* c7 */ + MULTIPLY(tmp4, FIX(0.221231742)), /* c9 */ + CONST_BITS-PASS1_BITS); + tmp12 = MULTIPLY(tmp0 - tmp4, FIX(0.951056516)) - /* (c3+c7)/2 */ + MULTIPLY(tmp1 + tmp3, FIX(0.587785252)); /* (c1-c9)/2 */ + tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.309016994)) + /* (c3-c7)/2 */ + (tmp11 << (CONST_BITS - 1)) - tmp2; + dataptr[3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS-PASS1_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/10)*(8/5) = 32/25, which we + * fold into the constant multipliers: + * 5-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/10) * 32/25. + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*4]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*3]; + tmp2 = dataptr[DCTSIZE*2]; + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*4]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*3]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp2, FIX(1.28)), /* 32/25 */ + CONST_BITS+PASS1_BITS); + tmp11 = MULTIPLY(tmp11, FIX(1.011928851)); /* (c2+c4)/2 */ + tmp10 -= tmp2 << 2; + tmp10 = MULTIPLY(tmp10, FIX(0.452548340)); /* (c2-c4)/2 */ + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp1, FIX(1.064004961)); /* c3 */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.657591230)), /* c1-c3 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.785601151)), /* c1+c3 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on an 8x4 sample block. + * + * 8-point FDCT in pass 1 (rows), 4-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_8x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Zero 4 bottom rows of output coefficient block. */ + MEMZERO(&data[DCTSIZE*4], SIZEOF(DCTELEM) * DCTSIZE * 4); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We must also scale the output by 8/4 = 2, which we add here. */ + + dataptr = data; + for (ctr = 0; ctr < 4; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[4]); + + tmp10 = tmp0 + tmp3; + tmp12 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp13 = tmp1 - tmp2; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << (PASS1_BITS+1)); + dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << (PASS1_BITS+1)); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-2); + dataptr[2] = (DCTELEM) RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), + CONST_BITS-PASS1_BITS-1); + dataptr[6] = (DCTELEM) RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), + CONST_BITS-PASS1_BITS-1); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + * i0..i3 in the paper are tmp0..tmp3 here. + */ + + tmp10 = tmp0 + tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp0 + tmp2; + tmp13 = tmp1 + tmp3; + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-2); + + tmp0 = MULTIPLY(tmp0, FIX_1_501321110); /* c1+c3-c5-c7 */ + tmp1 = MULTIPLY(tmp1, FIX_3_072711026); /* c1+c3+c5-c7 */ + tmp2 = MULTIPLY(tmp2, FIX_2_053119869); /* c1+c3-c5+c7 */ + tmp3 = MULTIPLY(tmp3, FIX_0_298631336); /* -c1+c3+c5-c7 */ + tmp10 = MULTIPLY(tmp10, - FIX_0_899976223); /* c7-c3 */ + tmp11 = MULTIPLY(tmp11, - FIX_2_562915447); /* -c1-c3 */ + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* c5-c3 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + + tmp12 += z1; + tmp13 += z1; + + dataptr[1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS-PASS1_BITS-1); + dataptr[3] = (DCTELEM) + RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS-PASS1_BITS-1); + dataptr[5] = (DCTELEM) + RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS-PASS1_BITS-1); + dataptr[7] = (DCTELEM) + RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS-PASS1_BITS-1); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * 4-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + */ + + dataptr = data; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + (ONE << (PASS1_BITS-1)); + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*2]; + + tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; + tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; + + dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp0 + tmp1, PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) RIGHT_SHIFT(tmp0 - tmp1, PASS1_BITS); + + /* Odd part */ + + tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS+PASS1_BITS-1); + + dataptr[DCTSIZE*1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 6x3 sample block. + * + * 6-point FDCT in pass 1 (rows), 3-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_6x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2; + INT32 tmp10, tmp11, tmp12; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We scale the results further by 2 as part of output adaption */ + /* scaling for different DCT size. */ + /* 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12). */ + + dataptr = data; + for (ctr = 0; ctr < 3; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[5]); + tmp11 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[4]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[3]); + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[5]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[4]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << (PASS1_BITS+1)); + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp12, FIX(1.224744871)), /* c2 */ + CONST_BITS-PASS1_BITS-1); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(0.707106781)), /* c4 */ + CONST_BITS-PASS1_BITS-1); + + /* Odd part */ + + tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)), /* c5 */ + CONST_BITS-PASS1_BITS-1); + + dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << (PASS1_BITS+1))); + dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << (PASS1_BITS+1)); + dataptr[5] = (DCTELEM) (tmp10 + ((tmp2 - tmp1) << (PASS1_BITS+1))); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/6)*(8/3) = 32/9, which we partially + * fold into the constant multipliers (other part was done in pass 1): + * 3-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/6) * 16/9. + */ + + dataptr = data; + for (ctr = 0; ctr < 6; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*2]; + tmp1 = dataptr[DCTSIZE*1]; + + tmp2 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*2]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 + tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(1.257078722)), /* c2 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(MULTIPLY(tmp2, FIX(2.177324216)), /* c1 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 4x2 sample block. + * + * 4-point FDCT in pass 1 (rows), 2-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1; + INT32 tmp10, tmp11; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We must also scale the output by (8/4)*(8/2) = 2**3, which we add here. */ + /* 4-point FDCT kernel, */ + /* cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. */ + + dataptr = data; + for (ctr = 0; ctr < 2; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+3)); + dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+3)); + + /* Odd part */ + + tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-4); + + dataptr[1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ + CONST_BITS-PASS1_BITS-3); + dataptr[3] = (DCTELEM) + RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ + CONST_BITS-PASS1_BITS-3); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + */ + + dataptr = data; + for (ctr = 0; ctr < 4; ctr++) { + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = dataptr[DCTSIZE*0] + (ONE << (PASS1_BITS-1)); + tmp1 = dataptr[DCTSIZE*1]; + + dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp0 + tmp1, PASS1_BITS); + + /* Odd part */ + + dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0 - tmp1, PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 2x1 sample block. + * + * 2-point FDCT in pass 1 (rows), 1-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_2x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1; + JSAMPROW elemptr; + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + elemptr = sample_data[0] + start_col; + + tmp0 = GETJSAMPLE(elemptr[0]); + tmp1 = GETJSAMPLE(elemptr[1]); + + /* We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/2)*(8/1) = 2**5. + */ + + /* Even part */ + /* Apply unsigned->signed conversion */ + data[0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5); + + /* Odd part */ + data[1] = (DCTELEM) ((tmp0 - tmp1) << 5); +} + + +/* + * Perform the forward DCT on an 8x16 sample block. + * + * 8-point FDCT in pass 1 (rows), 16-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17; + INT32 z1; + DCTELEM workspace[DCTSIZE2]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) + GETJSAMPLE(elemptr[4]); + + tmp10 = tmp0 + tmp3; + tmp12 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp13 = tmp1 - tmp2; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[7]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[6]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]); + tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865), + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065), + CONST_BITS-PASS1_BITS); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + * i0..i3 in the paper are tmp0..tmp3 here. + */ + + tmp10 = tmp0 + tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp0 + tmp2; + tmp13 = tmp1 + tmp3; + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + + tmp0 = MULTIPLY(tmp0, FIX_1_501321110); /* c1+c3-c5-c7 */ + tmp1 = MULTIPLY(tmp1, FIX_3_072711026); /* c1+c3+c5-c7 */ + tmp2 = MULTIPLY(tmp2, FIX_2_053119869); /* c1+c3-c5+c7 */ + tmp3 = MULTIPLY(tmp3, FIX_0_298631336); /* -c1+c3+c5-c7 */ + tmp10 = MULTIPLY(tmp10, - FIX_0_899976223); /* c7-c3 */ + tmp11 = MULTIPLY(tmp11, - FIX_2_562915447); /* -c1-c3 */ + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* c5-c3 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + + tmp12 += z1; + tmp13 += z1; + + dataptr[1] = (DCTELEM) DESCALE(tmp0 + tmp10 + tmp12, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp1 + tmp11 + tmp13, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp2 + tmp11 + tmp12, CONST_BITS-PASS1_BITS); + dataptr[7] = (DCTELEM) DESCALE(tmp3 + tmp10 + tmp13, CONST_BITS-PASS1_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == DCTSIZE * 2) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by 8/16 = 1/2. + * 16-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/32). + */ + + dataptr = data; + wsptr = workspace; + for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*4] + wsptr[DCTSIZE*3]; + tmp5 = dataptr[DCTSIZE*5] + wsptr[DCTSIZE*2]; + tmp6 = dataptr[DCTSIZE*6] + wsptr[DCTSIZE*1]; + tmp7 = dataptr[DCTSIZE*7] + wsptr[DCTSIZE*0]; + + tmp10 = tmp0 + tmp7; + tmp14 = tmp0 - tmp7; + tmp11 = tmp1 + tmp6; + tmp15 = tmp1 - tmp6; + tmp12 = tmp2 + tmp5; + tmp16 = tmp2 - tmp5; + tmp13 = tmp3 + tmp4; + tmp17 = tmp3 - tmp4; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*4]; + tmp4 = dataptr[DCTSIZE*4] - wsptr[DCTSIZE*3]; + tmp5 = dataptr[DCTSIZE*5] - wsptr[DCTSIZE*2]; + tmp6 = dataptr[DCTSIZE*6] - wsptr[DCTSIZE*1]; + tmp7 = dataptr[DCTSIZE*7] - wsptr[DCTSIZE*0]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS1_BITS+1); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */ + MULTIPLY(tmp11 - tmp12, FIX_0_541196100), /* c12[16] = c6[8] */ + CONST_BITS+PASS1_BITS+1); + + tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) + /* c14[16] = c7[8] */ + MULTIPLY(tmp14 - tmp16, FIX(1.387039845)); /* c2[16] = c1[8] */ + + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982)) /* c6+c14 */ + + MULTIPLY(tmp16, FIX(2.172734804)), /* c2+c10 */ + CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243)) /* c2-c6 */ + - MULTIPLY(tmp17, FIX(1.061594338)), /* c10+c14 */ + CONST_BITS+PASS1_BITS+1); + + /* Odd part */ + + tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) + /* c3 */ + MULTIPLY(tmp6 - tmp7, FIX(0.410524528)); /* c13 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) + /* c5 */ + MULTIPLY(tmp5 + tmp7, FIX(0.666655658)); /* c11 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) + /* c7 */ + MULTIPLY(tmp4 - tmp7, FIX(0.897167586)); /* c9 */ + tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) + /* c15 */ + MULTIPLY(tmp6 - tmp5, FIX(1.407403738)); /* c1 */ + tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) + /* -c11 */ + MULTIPLY(tmp4 + tmp6, - FIX(1.247225013)); /* -c5 */ + tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) + /* -c3 */ + MULTIPLY(tmp5 - tmp4, FIX(0.410524528)); /* c13 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(tmp0, FIX(2.286341144)) + /* c7+c5+c3-c1 */ + MULTIPLY(tmp7, FIX(0.779653625)); /* c15+c13-c11+c9 */ + tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */ + - MULTIPLY(tmp6, FIX(1.663905119)); /* c7+c13+c1-c5 */ + tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */ + + MULTIPLY(tmp5, FIX(1.227391138)); /* c9-c11+c1-c13 */ + tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */ + + MULTIPLY(tmp4, FIX(2.167985692)); /* c1+c13+c5-c9 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS1_BITS+1); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 7x14 sample block. + * + * 7-point FDCT in pass 1 (rows), 14-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 z1, z2, z3; + DCTELEM workspace[8*6]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 7-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/14). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[6]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[5]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[4]); + tmp3 = GETJSAMPLE(elemptr[3]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[6]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[5]); + tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[4]); + + z1 = tmp0 + tmp2; + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE) << PASS1_BITS); + tmp3 += tmp3; + z1 -= tmp3; + z1 -= tmp3; + z1 = MULTIPLY(z1, FIX(0.353553391)); /* (c2+c6-c4)/2 */ + z2 = MULTIPLY(tmp0 - tmp2, FIX(0.920609002)); /* (c2+c4-c6)/2 */ + z3 = MULTIPLY(tmp1 - tmp2, FIX(0.314692123)); /* c6 */ + dataptr[2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS-PASS1_BITS); + z1 -= z2; + z2 = MULTIPLY(tmp0 - tmp1, FIX(0.881747734)); /* c4 */ + dataptr[4] = (DCTELEM) + DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.707106781)), /* c2+c6-c4 */ + CONST_BITS-PASS1_BITS); + dataptr[6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp1 = MULTIPLY(tmp10 + tmp11, FIX(0.935414347)); /* (c3+c1-c5)/2 */ + tmp2 = MULTIPLY(tmp10 - tmp11, FIX(0.170262339)); /* (c3+c5-c1)/2 */ + tmp0 = tmp1 - tmp2; + tmp1 += tmp2; + tmp2 = MULTIPLY(tmp11 + tmp12, - FIX(1.378756276)); /* -c1 */ + tmp1 += tmp2; + tmp3 = MULTIPLY(tmp10 + tmp12, FIX(0.613604268)); /* c5 */ + tmp0 += tmp3; + tmp2 += tmp3 + MULTIPLY(tmp12, FIX(1.870828693)); /* c3+c1-c5 */ + + dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-PASS1_BITS); + dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS-PASS1_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 14) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/7)*(8/14) = 32/49, which we + * fold into the constant multipliers: + * 14-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/28) * 32/49. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = 0; ctr < 7; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*3]; + tmp13 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*2]; + tmp4 = dataptr[DCTSIZE*4] + wsptr[DCTSIZE*1]; + tmp5 = dataptr[DCTSIZE*5] + wsptr[DCTSIZE*0]; + tmp6 = dataptr[DCTSIZE*6] + dataptr[DCTSIZE*7]; + + tmp10 = tmp0 + tmp6; + tmp14 = tmp0 - tmp6; + tmp11 = tmp1 + tmp5; + tmp15 = tmp1 - tmp5; + tmp12 = tmp2 + tmp4; + tmp16 = tmp2 - tmp4; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*3]; + tmp3 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*2]; + tmp4 = dataptr[DCTSIZE*4] - wsptr[DCTSIZE*1]; + tmp5 = dataptr[DCTSIZE*5] - wsptr[DCTSIZE*0]; + tmp6 = dataptr[DCTSIZE*6] - dataptr[DCTSIZE*7]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12 + tmp13, + FIX(0.653061224)), /* 32/49 */ + CONST_BITS+PASS1_BITS); + tmp13 += tmp13; + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp13, FIX(0.832106052)) + /* c4 */ + MULTIPLY(tmp11 - tmp13, FIX(0.205513223)) - /* c12 */ + MULTIPLY(tmp12 - tmp13, FIX(0.575835255)), /* c8 */ + CONST_BITS+PASS1_BITS); + + tmp10 = MULTIPLY(tmp14 + tmp15, FIX(0.722074570)); /* c6 */ + + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.178337691)) /* c2-c6 */ + + MULTIPLY(tmp16, FIX(0.400721155)), /* c10 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.122795725)) /* c6+c10 */ + - MULTIPLY(tmp16, FIX(0.900412262)), /* c2 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = tmp1 + tmp2; + tmp11 = tmp5 - tmp4; + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp10 + tmp3 - tmp11 - tmp6, + FIX(0.653061224)), /* 32/49 */ + CONST_BITS+PASS1_BITS); + tmp3 = MULTIPLY(tmp3 , FIX(0.653061224)); /* 32/49 */ + tmp10 = MULTIPLY(tmp10, - FIX(0.103406812)); /* -c13 */ + tmp11 = MULTIPLY(tmp11, FIX(0.917760839)); /* c1 */ + tmp10 += tmp11 - tmp3; + tmp11 = MULTIPLY(tmp0 + tmp2, FIX(0.782007410)) + /* c5 */ + MULTIPLY(tmp4 + tmp6, FIX(0.491367823)); /* c9 */ + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(1.550341076)) /* c3+c5-c13 */ + + MULTIPLY(tmp4, FIX(0.731428202)), /* c1+c11-c9 */ + CONST_BITS+PASS1_BITS); + tmp12 = MULTIPLY(tmp0 + tmp1, FIX(0.871740478)) + /* c3 */ + MULTIPLY(tmp5 - tmp6, FIX(0.305035186)); /* c11 */ + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.276965844)) /* c3-c9-c13 */ + - MULTIPLY(tmp5, FIX(2.004803435)), /* c1+c5+c11 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp11 + tmp12 + tmp3 + - MULTIPLY(tmp0, FIX(0.735987049)) /* c3+c5-c1 */ + - MULTIPLY(tmp6, FIX(0.082925825)), /* c9-c11-c13 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 6x12 sample block. + * + * 6-point FDCT in pass 1 (rows), 12-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + DCTELEM workspace[8*4]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[5]); + tmp11 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[4]); + tmp2 = GETJSAMPLE(elemptr[2]) + GETJSAMPLE(elemptr[3]); + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[5]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[4]); + tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp12, FIX(1.224744871)), /* c2 */ + CONST_BITS-PASS1_BITS); + dataptr[4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(0.707106781)), /* c4 */ + CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)), /* c5 */ + CONST_BITS-PASS1_BITS); + + dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << PASS1_BITS)); + dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << PASS1_BITS); + dataptr[5] = (DCTELEM) (tmp10 + ((tmp2 - tmp1) << PASS1_BITS)); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 12) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/6)*(8/12) = 8/9, which we + * fold into the constant multipliers: + * 12-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/24) * 8/9. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*3]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*2]; + tmp2 = dataptr[DCTSIZE*2] + wsptr[DCTSIZE*1]; + tmp3 = dataptr[DCTSIZE*3] + wsptr[DCTSIZE*0]; + tmp4 = dataptr[DCTSIZE*4] + dataptr[DCTSIZE*7]; + tmp5 = dataptr[DCTSIZE*5] + dataptr[DCTSIZE*6]; + + tmp10 = tmp0 + tmp5; + tmp13 = tmp0 - tmp5; + tmp11 = tmp1 + tmp4; + tmp14 = tmp1 - tmp4; + tmp12 = tmp2 + tmp3; + tmp15 = tmp2 - tmp3; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*3]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*2]; + tmp2 = dataptr[DCTSIZE*2] - wsptr[DCTSIZE*1]; + tmp3 = dataptr[DCTSIZE*3] - wsptr[DCTSIZE*0]; + tmp4 = dataptr[DCTSIZE*4] - dataptr[DCTSIZE*7]; + tmp5 = dataptr[DCTSIZE*5] - dataptr[DCTSIZE*6]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(0.888888889)), /* 8/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(MULTIPLY(tmp13 - tmp14 - tmp15, FIX(0.888888889)), /* 8/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.088662108)), /* c4 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp14 - tmp15, FIX(0.888888889)) + /* 8/9 */ + MULTIPLY(tmp13 + tmp15, FIX(1.214244803)), /* c2 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp1 + tmp4, FIX(0.481063200)); /* c9 */ + tmp14 = tmp10 + MULTIPLY(tmp1, FIX(0.680326102)); /* c3-c9 */ + tmp15 = tmp10 - MULTIPLY(tmp4, FIX(1.642452502)); /* c3+c9 */ + tmp12 = MULTIPLY(tmp0 + tmp2, FIX(0.997307603)); /* c5 */ + tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.765261039)); /* c7 */ + tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.516244403)) /* c5+c7-c1 */ + + MULTIPLY(tmp5, FIX(0.164081699)); /* c11 */ + tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.164081699)); /* -c11 */ + tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.079550144)) /* c1+c5-c11 */ + + MULTIPLY(tmp5, FIX(0.765261039)); /* c7 */ + tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.645144899)) /* c1+c11-c7 */ + - MULTIPLY(tmp5, FIX(0.997307603)); /* c5 */ + tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.161389302)) /* c3 */ + - MULTIPLY(tmp2 + tmp5, FIX(0.481063200)); /* c9 */ + + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 5x10 sample block. + * + * 5-point FDCT in pass 1 (rows), 10-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4; + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + DCTELEM workspace[8*2]; + DCTELEM *dataptr; + DCTELEM *wsptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* 5-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/10). */ + + dataptr = data; + ctr = 0; + for (;;) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[4]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[3]); + tmp2 = GETJSAMPLE(elemptr[2]); + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + + tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[4]); + tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[3]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp10 + tmp2 - 5 * CENTERJSAMPLE) << PASS1_BITS); + tmp11 = MULTIPLY(tmp11, FIX(0.790569415)); /* (c2+c4)/2 */ + tmp10 -= tmp2 << 2; + tmp10 = MULTIPLY(tmp10, FIX(0.353553391)); /* (c2-c4)/2 */ + dataptr[2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS-PASS1_BITS); + dataptr[4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS-PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp1, FIX(0.831253876)); /* c3 */ + + dataptr[1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.513743148)), /* c1-c3 */ + CONST_BITS-PASS1_BITS); + dataptr[3] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.176250899)), /* c1+c3 */ + CONST_BITS-PASS1_BITS); + + ctr++; + + if (ctr != DCTSIZE) { + if (ctr == 10) + break; /* Done. */ + dataptr += DCTSIZE; /* advance pointer to next row */ + } else + dataptr = workspace; /* switch pointer to extended workspace */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/5)*(8/10) = 32/25, which we + * fold into the constant multipliers: + * 10-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/20) * 32/25. + */ + + dataptr = data; + wsptr = workspace; + for (ctr = 0; ctr < 5; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + wsptr[DCTSIZE*1]; + tmp1 = dataptr[DCTSIZE*1] + wsptr[DCTSIZE*0]; + tmp12 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*7]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*6]; + tmp4 = dataptr[DCTSIZE*4] + dataptr[DCTSIZE*5]; + + tmp10 = tmp0 + tmp4; + tmp13 = tmp0 - tmp4; + tmp11 = tmp1 + tmp3; + tmp14 = tmp1 - tmp3; + + tmp0 = dataptr[DCTSIZE*0] - wsptr[DCTSIZE*1]; + tmp1 = dataptr[DCTSIZE*1] - wsptr[DCTSIZE*0]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*7]; + tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*6]; + tmp4 = dataptr[DCTSIZE*4] - dataptr[DCTSIZE*5]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(1.28)), /* 32/25 */ + CONST_BITS+PASS1_BITS); + tmp12 += tmp12; + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.464477191)) - /* c4 */ + MULTIPLY(tmp11 - tmp12, FIX(0.559380511)), /* c8 */ + CONST_BITS+PASS1_BITS); + tmp10 = MULTIPLY(tmp13 + tmp14, FIX(1.064004961)); /* c6 */ + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.657591230)), /* c2-c6 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.785601151)), /* c2+c6 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = tmp0 + tmp4; + tmp11 = tmp1 - tmp3; + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp2, FIX(1.28)), /* 32/25 */ + CONST_BITS+PASS1_BITS); + tmp2 = MULTIPLY(tmp2, FIX(1.28)); /* 32/25 */ + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(MULTIPLY(tmp0, FIX(1.787906876)) + /* c1 */ + MULTIPLY(tmp1, FIX(1.612894094)) + tmp2 + /* c3 */ + MULTIPLY(tmp3, FIX(0.821810588)) + /* c7 */ + MULTIPLY(tmp4, FIX(0.283176630)), /* c9 */ + CONST_BITS+PASS1_BITS); + tmp12 = MULTIPLY(tmp0 - tmp4, FIX(1.217352341)) - /* (c3+c7)/2 */ + MULTIPLY(tmp1 + tmp3, FIX(0.752365123)); /* (c1-c9)/2 */ + tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.395541753)) + /* (c3-c7)/2 */ + MULTIPLY(tmp11, FIX(0.64)) - tmp2; /* 16/25 */ + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + wsptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 4x8 sample block. + * + * 4-point FDCT in pass 1 (rows), 8-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We must also scale the output by 8/4 = 2, which we add here. */ + /* 4-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). */ + + dataptr = data; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]); + tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]); + + tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]); + tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+1)); + dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+1)); + + /* Odd part */ + + tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-2); + + dataptr[1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ + CONST_BITS-PASS1_BITS-1); + dataptr[3] = (DCTELEM) + RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ + CONST_BITS-PASS1_BITS-1); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + */ + + dataptr = data; + for (ctr = 0; ctr < 4; ctr++) { + /* Even part per LL&M figure 1 --- note that published figure is faulty; + * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". + */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; + + /* Add fudge factor here for final descale. */ + tmp10 = tmp0 + tmp3 + (ONE << (PASS1_BITS-1)); + tmp12 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp13 = tmp1 - tmp2; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; + tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; + + dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS1_BITS); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS+PASS1_BITS-1); + dataptr[DCTSIZE*2] = (DCTELEM) + RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) + RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), CONST_BITS+PASS1_BITS); + + /* Odd part per figure 8 --- note paper omits factor of sqrt(2). + * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + * i0..i3 in the paper are tmp0..tmp3 here. + */ + + tmp10 = tmp0 + tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp0 + tmp2; + tmp13 = tmp1 + tmp3; + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS+PASS1_BITS-1); + + tmp0 = MULTIPLY(tmp0, FIX_1_501321110); /* c1+c3-c5-c7 */ + tmp1 = MULTIPLY(tmp1, FIX_3_072711026); /* c1+c3+c5-c7 */ + tmp2 = MULTIPLY(tmp2, FIX_2_053119869); /* c1+c3-c5+c7 */ + tmp3 = MULTIPLY(tmp3, FIX_0_298631336); /* -c1+c3+c5-c7 */ + tmp10 = MULTIPLY(tmp10, - FIX_0_899976223); /* c7-c3 */ + tmp11 = MULTIPLY(tmp11, - FIX_2_562915447); /* -c1-c3 */ + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* c5-c3 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + + tmp12 += z1; + tmp13 += z1; + + dataptr[DCTSIZE*1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 3x6 sample block. + * + * 3-point FDCT in pass 1 (rows), 6-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_3x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1, tmp2; + INT32 tmp10, tmp11, tmp12; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + /* We scale the results further by 2 as part of output adaption */ + /* scaling for different DCT size. */ + /* 3-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/6). */ + + dataptr = data; + for (ctr = 0; ctr < 6; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[2]); + tmp1 = GETJSAMPLE(elemptr[1]); + + tmp2 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[2]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) + ((tmp0 + tmp1 - 3 * CENTERJSAMPLE) << (PASS1_BITS+1)); + dataptr[2] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(0.707106781)), /* c2 */ + CONST_BITS-PASS1_BITS-1); + + /* Odd part */ + + dataptr[1] = (DCTELEM) + DESCALE(MULTIPLY(tmp2, FIX(1.224744871)), /* c1 */ + CONST_BITS-PASS1_BITS-1); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We remove the PASS1_BITS scaling, but leave the results scaled up + * by an overall factor of 8. + * We must also scale the output by (8/6)*(8/3) = 32/9, which we partially + * fold into the constant multipliers (other part was done in pass 1): + * 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12) * 16/9. + */ + + dataptr = data; + for (ctr = 0; ctr < 3; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*5]; + tmp11 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*3]; + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; + + tmp0 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*5]; + tmp1 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*4]; + tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*3]; + + dataptr[DCTSIZE*0] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 + tmp11, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(MULTIPLY(tmp12, FIX(2.177324216)), /* c2 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */ + CONST_BITS+PASS1_BITS); + + /* Odd part */ + + tmp10 = MULTIPLY(tmp0 + tmp2, FIX(0.650711829)); /* c5 */ + + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp0 + tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)), /* 16/9 */ + CONST_BITS+PASS1_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 2x4 sample block. + * + * 2-point FDCT in pass 1 (rows), 4-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1; + INT32 tmp10, tmp11; + DCTELEM *dataptr; + JSAMPROW elemptr; + int ctr; + SHIFT_TEMPS + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true DCT. */ + /* We must also scale the output by (8/2)*(8/4) = 2**3, which we add here. */ + + dataptr = data; + for (ctr = 0; ctr < 4; ctr++) { + elemptr = sample_data[ctr] + start_col; + + /* Even part */ + + tmp0 = GETJSAMPLE(elemptr[0]); + tmp1 = GETJSAMPLE(elemptr[1]); + + /* Apply unsigned->signed conversion */ + dataptr[0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 3); + + /* Odd part */ + + dataptr[1] = (DCTELEM) ((tmp0 - tmp1) << 3); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. + * We leave the results scaled up by an overall factor of 8. + * 4-point FDCT kernel, + * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. + */ + + dataptr = data; + for (ctr = 0; ctr < 2; ctr++) { + /* Even part */ + + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3]; + tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*2]; + + tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; + tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; + + dataptr[DCTSIZE*0] = (DCTELEM) (tmp0 + tmp1); + dataptr[DCTSIZE*2] = (DCTELEM) (tmp0 - tmp1); + + /* Odd part */ + + tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-1); + + dataptr[DCTSIZE*1] = (DCTELEM) + RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ + CONST_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ + CONST_BITS); + + dataptr++; /* advance pointer to next column */ + } +} + + +/* + * Perform the forward DCT on a 1x2 sample block. + * + * 1-point FDCT in pass 1 (rows), 2-point in pass 2 (columns). + */ + +GLOBAL(void) +jpeg_fdct_1x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) +{ + INT32 tmp0, tmp1; + + /* Pre-zero output coefficient block. */ + MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); + + tmp0 = GETJSAMPLE(sample_data[0][start_col]); + tmp1 = GETJSAMPLE(sample_data[1][start_col]); + + /* We leave the results scaled up by an overall factor of 8. + * We must also scale the output by (8/1)*(8/2) = 2**5. + */ + + /* Even part */ + /* Apply unsigned->signed conversion */ + data[DCTSIZE*0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5); + + /* Odd part */ + data[DCTSIZE*1] = (DCTELEM) ((tmp0 - tmp1) << 5); +} + +#endif /* DCT_SCALING_SUPPORTED */ +#endif /* DCT_ISLOW_SUPPORTED */ diff --git a/crypto777/jpeg/jidctflt.c b/crypto777/jpeg/jidctflt.c new file mode 100644 index 000000000..23ae9d333 --- /dev/null +++ b/crypto777/jpeg/jidctflt.c @@ -0,0 +1,235 @@ +/* + * jidctflt.c + * + * Copyright (C) 1994-1998, Thomas G. Lane. + * Modified 2010 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a floating-point implementation of the + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine + * must also perform dequantization of the input coefficients. + * + * This implementation should be more accurate than either of the integer + * IDCT implementations. However, it may not give the same results on all + * machines because of differences in roundoff behavior. Speed will depend + * on the hardware's floating point capacity. + * + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT + * on each row (or vice versa, but it's more convenient to emit a row at + * a time). Direct algorithms are also available, but they are much more + * complex and seem not to be any faster when reduced to code. + * + * This implementation is based on Arai, Agui, and Nakajima's algorithm for + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in + * Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following code + * is based directly on figure 4-8 in P&M. + * While an 8-point DCT cannot be done in less than 11 multiplies, it is + * possible to arrange the computation so that many of the multiplies are + * simple scalings of the final outputs. These multiplies can then be + * folded into the multiplications or divisions by the JPEG quantization + * table entries. The AA&N method leaves only 5 multiplies and 29 adds + * to be done in the DCT itself. + * The primary disadvantage of this method is that with a fixed-point + * implementation, accuracy is lost due to imprecise representation of the + * scaled quantization values. However, that problem does not arise if + * we use floating point arithmetic. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + +#ifdef DCT_FLOAT_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ +#endif + + +/* Dequantize a coefficient by multiplying it by the multiplier-table + * entry; produce a float result. + */ + +#define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval)) + + +/* + * Perform dequantization and inverse DCT on one block of coefficients. + */ + +GLOBAL(void) +jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + FAST_FLOAT tmp10, tmp11, tmp12, tmp13; + FAST_FLOAT z5, z10, z11, z12, z13; + JCOEFPTR inptr; + FLOAT_MULT_TYPE * quantptr; + FAST_FLOAT * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = cinfo->sample_range_limit; + int ctr; + FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */ + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = DCTSIZE; ctr > 0; ctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any column in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * column DCT calculations can be simplified this way. + */ + + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && + inptr[DCTSIZE*7] == 0) { + /* AC terms all zero */ + FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + + wsptr[DCTSIZE*0] = dcval; + wsptr[DCTSIZE*1] = dcval; + wsptr[DCTSIZE*2] = dcval; + wsptr[DCTSIZE*3] = dcval; + wsptr[DCTSIZE*4] = dcval; + wsptr[DCTSIZE*5] = dcval; + wsptr[DCTSIZE*6] = dcval; + wsptr[DCTSIZE*7] = dcval; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + continue; + } + + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp10 = tmp0 + tmp2; /* phase 3 */ + tmp11 = tmp0 - tmp2; + + tmp13 = tmp1 + tmp3; /* phases 5-3 */ + tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */ + + tmp0 = tmp10 + tmp13; /* phase 2 */ + tmp3 = tmp10 - tmp13; + tmp1 = tmp11 + tmp12; + tmp2 = tmp11 - tmp12; + + /* Odd part */ + + tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + z13 = tmp6 + tmp5; /* phase 6 */ + z10 = tmp6 - tmp5; + z11 = tmp4 + tmp7; + z12 = tmp4 - tmp7; + + tmp7 = z11 + z13; /* phase 5 */ + tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */ + + z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ + tmp10 = z5 - z12 * ((FAST_FLOAT) 1.082392200); /* 2*(c2-c6) */ + tmp12 = z5 - z10 * ((FAST_FLOAT) 2.613125930); /* 2*(c2+c6) */ + + tmp6 = tmp12 - tmp7; /* phase 2 */ + tmp5 = tmp11 - tmp6; + tmp4 = tmp10 - tmp5; + + wsptr[DCTSIZE*0] = tmp0 + tmp7; + wsptr[DCTSIZE*7] = tmp0 - tmp7; + wsptr[DCTSIZE*1] = tmp1 + tmp6; + wsptr[DCTSIZE*6] = tmp1 - tmp6; + wsptr[DCTSIZE*2] = tmp2 + tmp5; + wsptr[DCTSIZE*5] = tmp2 - tmp5; + wsptr[DCTSIZE*3] = tmp3 + tmp4; + wsptr[DCTSIZE*4] = tmp3 - tmp4; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + } + + /* Pass 2: process rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + outptr = output_buf[ctr] + output_col; + /* Rows of zeroes can be exploited in the same way as we did with columns. + * However, the column calculation has created many nonzero AC terms, so + * the simplification applies less often (typically 5% to 10% of the time). + * And testing floats for zero is relatively expensive, so we don't bother. + */ + + /* Even part */ + + /* Apply signed->unsigned and prepare float->int conversion */ + z5 = wsptr[0] + ((FAST_FLOAT) CENTERJSAMPLE + (FAST_FLOAT) 0.5); + tmp10 = z5 + wsptr[4]; + tmp11 = z5 - wsptr[4]; + + tmp13 = wsptr[2] + wsptr[6]; + tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13; + + tmp0 = tmp10 + tmp13; + tmp3 = tmp10 - tmp13; + tmp1 = tmp11 + tmp12; + tmp2 = tmp11 - tmp12; + + /* Odd part */ + + z13 = wsptr[5] + wsptr[3]; + z10 = wsptr[5] - wsptr[3]; + z11 = wsptr[1] + wsptr[7]; + z12 = wsptr[1] - wsptr[7]; + + tmp7 = z11 + z13; + tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); + + z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ + tmp10 = z5 - z12 * ((FAST_FLOAT) 1.082392200); /* 2*(c2-c6) */ + tmp12 = z5 - z10 * ((FAST_FLOAT) 2.613125930); /* 2*(c2+c6) */ + + tmp6 = tmp12 - tmp7; + tmp5 = tmp11 - tmp6; + tmp4 = tmp10 - tmp5; + + /* Final output stage: float->int conversion and range-limit */ + + outptr[0] = range_limit[((int) (tmp0 + tmp7)) & RANGE_MASK]; + outptr[7] = range_limit[((int) (tmp0 - tmp7)) & RANGE_MASK]; + outptr[1] = range_limit[((int) (tmp1 + tmp6)) & RANGE_MASK]; + outptr[6] = range_limit[((int) (tmp1 - tmp6)) & RANGE_MASK]; + outptr[2] = range_limit[((int) (tmp2 + tmp5)) & RANGE_MASK]; + outptr[5] = range_limit[((int) (tmp2 - tmp5)) & RANGE_MASK]; + outptr[3] = range_limit[((int) (tmp3 + tmp4)) & RANGE_MASK]; + outptr[4] = range_limit[((int) (tmp3 - tmp4)) & RANGE_MASK]; + + wsptr += DCTSIZE; /* advance pointer to next row */ + } +} + +#endif /* DCT_FLOAT_SUPPORTED */ diff --git a/crypto777/jpeg/jidctfst.c b/crypto777/jpeg/jidctfst.c new file mode 100644 index 000000000..dba4216fb --- /dev/null +++ b/crypto777/jpeg/jidctfst.c @@ -0,0 +1,368 @@ +/* + * jidctfst.c + * + * Copyright (C) 1994-1998, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a fast, not so accurate integer implementation of the + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine + * must also perform dequantization of the input coefficients. + * + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT + * on each row (or vice versa, but it's more convenient to emit a row at + * a time). Direct algorithms are also available, but they are much more + * complex and seem not to be any faster when reduced to code. + * + * This implementation is based on Arai, Agui, and Nakajima's algorithm for + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in + * Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following code + * is based directly on figure 4-8 in P&M. + * While an 8-point DCT cannot be done in less than 11 multiplies, it is + * possible to arrange the computation so that many of the multiplies are + * simple scalings of the final outputs. These multiplies can then be + * folded into the multiplications or divisions by the JPEG quantization + * table entries. The AA&N method leaves only 5 multiplies and 29 adds + * to be done in the DCT itself. + * The primary disadvantage of this method is that with fixed-point math, + * accuracy is lost due to imprecise representation of the scaled + * quantization values. The smaller the quantization table entry, the less + * precise the scaled value, so this implementation does worse with high- + * quality-setting files than with low-quality ones. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + +#ifdef DCT_IFAST_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ +#endif + + +/* Scaling decisions are generally the same as in the LL&M algorithm; + * see jidctint.c for more details. However, we choose to descale + * (right shift) multiplication products as soon as they are formed, + * rather than carrying additional fractional bits into subsequent additions. + * This compromises accuracy slightly, but it lets us save a few shifts. + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) + * everywhere except in the multiplications proper; this saves a good deal + * of work on 16-bit-int machines. + * + * The dequantized coefficients are not integers because the AA&N scaling + * factors have been incorporated. We represent them scaled up by PASS1_BITS, + * so that the first and second IDCT rounds have the same input scaling. + * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to + * avoid a descaling shift; this compromises accuracy rather drastically + * for small quantization table entries, but it saves a lot of shifts. + * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway, + * so we use a much larger scaling factor to preserve accuracy. + * + * A final compromise is to represent the multiplicative constants to only + * 8 fractional bits, rather than 13. This saves some shifting work on some + * machines, and may also reduce the cost of multiplication (since there + * are fewer one-bits in the constants). + */ + +#if BITS_IN_JSAMPLE == 8 +#define CONST_BITS 8 +#define PASS1_BITS 2 +#else +#define CONST_BITS 8 +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#endif + +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus + * causing a lot of useless floating-point operations at run time. + * To get around this we use the following pre-calculated constants. + * If you change CONST_BITS you may want to add appropriate values. + * (With a reasonable C compiler, you can just rely on the FIX() macro...) + */ + +#if CONST_BITS == 8 +#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */ +#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */ +#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */ +#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */ +#else +#define FIX_1_082392200 FIX(1.082392200) +#define FIX_1_414213562 FIX(1.414213562) +#define FIX_1_847759065 FIX(1.847759065) +#define FIX_2_613125930 FIX(2.613125930) +#endif + + +/* We can gain a little more speed, with a further compromise in accuracy, + * by omitting the addition in a descaling shift. This yields an incorrectly + * rounded result half the time... + */ + +#ifndef USE_ACCURATE_ROUNDING +#undef DESCALE +#define DESCALE(x,n) RIGHT_SHIFT(x, n) +#endif + + +/* Multiply a DCTELEM variable by an INT32 constant, and immediately + * descale to yield a DCTELEM result. + */ + +#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) + + +/* Dequantize a coefficient by multiplying it by the multiplier-table + * entry; produce a DCTELEM result. For 8-bit data a 16x16->16 + * multiplication will do. For 12-bit data, the multiplier table is + * declared INT32, so a 32-bit multiply will be used. + */ + +#if BITS_IN_JSAMPLE == 8 +#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval)) +#else +#define DEQUANTIZE(coef,quantval) \ + DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS) +#endif + + +/* Like DESCALE, but applies to a DCTELEM and produces an int. + * We assume that int right shift is unsigned if INT32 right shift is. + */ + +#ifdef RIGHT_SHIFT_IS_UNSIGNED +#define ISHIFT_TEMPS DCTELEM ishift_temp; +#if BITS_IN_JSAMPLE == 8 +#define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */ +#else +#define DCTELEMBITS 32 /* DCTELEM must be 32 bits */ +#endif +#define IRIGHT_SHIFT(x,shft) \ + ((ishift_temp = (x)) < 0 ? \ + (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \ + (ishift_temp >> (shft))) +#else +#define ISHIFT_TEMPS +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) +#endif + +#ifdef USE_ACCURATE_ROUNDING +#define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n)) +#else +#define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n)) +#endif + + +/* + * Perform dequantization and inverse DCT on one block of coefficients. + */ + +GLOBAL(void) +jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + DCTELEM tmp10, tmp11, tmp12, tmp13; + DCTELEM z5, z10, z11, z12, z13; + JCOEFPTR inptr; + IFAST_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[DCTSIZE2]; /* buffers data between passes */ + SHIFT_TEMPS /* for DESCALE */ + ISHIFT_TEMPS /* for IDESCALE */ + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (IFAST_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = DCTSIZE; ctr > 0; ctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any column in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * column DCT calculations can be simplified this way. + */ + + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && + inptr[DCTSIZE*7] == 0) { + /* AC terms all zero */ + int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + + wsptr[DCTSIZE*0] = dcval; + wsptr[DCTSIZE*1] = dcval; + wsptr[DCTSIZE*2] = dcval; + wsptr[DCTSIZE*3] = dcval; + wsptr[DCTSIZE*4] = dcval; + wsptr[DCTSIZE*5] = dcval; + wsptr[DCTSIZE*6] = dcval; + wsptr[DCTSIZE*7] = dcval; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + continue; + } + + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp10 = tmp0 + tmp2; /* phase 3 */ + tmp11 = tmp0 - tmp2; + + tmp13 = tmp1 + tmp3; /* phases 5-3 */ + tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */ + + tmp0 = tmp10 + tmp13; /* phase 2 */ + tmp3 = tmp10 - tmp13; + tmp1 = tmp11 + tmp12; + tmp2 = tmp11 - tmp12; + + /* Odd part */ + + tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + z13 = tmp6 + tmp5; /* phase 6 */ + z10 = tmp6 - tmp5; + z11 = tmp4 + tmp7; + z12 = tmp4 - tmp7; + + tmp7 = z11 + z13; /* phase 5 */ + tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ + + z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ + tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ + tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ + + tmp6 = tmp12 - tmp7; /* phase 2 */ + tmp5 = tmp11 - tmp6; + tmp4 = tmp10 + tmp5; + + wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7); + wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7); + wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6); + wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6); + wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5); + wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5); + wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4); + wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4); + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + } + + /* Pass 2: process rows from work array, store into output array. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + wsptr = workspace; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + outptr = output_buf[ctr] + output_col; + /* Rows of zeroes can be exploited in the same way as we did with columns. + * However, the column calculation has created many nonzero AC terms, so + * the simplification applies less often (typically 5% to 10% of the time). + * On machines with very fast multiplication, it's possible that the + * test takes more time than it's worth. In that case this section + * may be commented out. + */ + +#ifndef NO_ZERO_ROW_TEST + if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && + wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { + /* AC terms all zero */ + JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3) + & RANGE_MASK]; + + outptr[0] = dcval; + outptr[1] = dcval; + outptr[2] = dcval; + outptr[3] = dcval; + outptr[4] = dcval; + outptr[5] = dcval; + outptr[6] = dcval; + outptr[7] = dcval; + + wsptr += DCTSIZE; /* advance pointer to next row */ + continue; + } +#endif + + /* Even part */ + + tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]); + tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]); + + tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]); + tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562) + - tmp13; + + tmp0 = tmp10 + tmp13; + tmp3 = tmp10 - tmp13; + tmp1 = tmp11 + tmp12; + tmp2 = tmp11 - tmp12; + + /* Odd part */ + + z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3]; + z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3]; + z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7]; + z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7]; + + tmp7 = z11 + z13; /* phase 5 */ + tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ + + z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ + tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ + tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ + + tmp6 = tmp12 - tmp7; /* phase 2 */ + tmp5 = tmp11 - tmp6; + tmp4 = tmp10 + tmp5; + + /* Final output stage: scale down by a factor of 8 and range-limit */ + + outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += DCTSIZE; /* advance pointer to next row */ + } +} + +#endif /* DCT_IFAST_SUPPORTED */ diff --git a/crypto777/jpeg/jidctint.c b/crypto777/jpeg/jidctint.c new file mode 100644 index 000000000..dcdf7ce45 --- /dev/null +++ b/crypto777/jpeg/jidctint.c @@ -0,0 +1,5137 @@ +/* + * jidctint.c + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * Modification developed 2002-2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a slow-but-accurate integer implementation of the + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine + * must also perform dequantization of the input coefficients. + * + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT + * on each row (or vice versa, but it's more convenient to emit a row at + * a time). Direct algorithms are also available, but they are much more + * complex and seem not to be any faster when reduced to code. + * + * This implementation is based on an algorithm described in + * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT + * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, + * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. + * The primary algorithm described there uses 11 multiplies and 29 adds. + * We use their alternate method with 12 multiplies and 32 adds. + * The advantage of this method is that no data path contains more than one + * multiplication; this allows a very simple and accurate implementation in + * scaled fixed-point arithmetic, with a minimal number of shifts. + * + * We also provide IDCT routines with various output sample block sizes for + * direct resolution reduction or enlargement and for direct resolving the + * common 2x1 and 1x2 subsampling cases without additional resampling: NxN + * (N=1...16), 2NxN, and Nx2N (N=1...8) pixels for one 8x8 input DCT block. + * + * For N<8 we simply take the corresponding low-frequency coefficients of + * the 8x8 input DCT block and apply an NxN point IDCT on the sub-block + * to yield the downscaled outputs. + * This can be seen as direct low-pass downsampling from the DCT domain + * point of view rather than the usual spatial domain point of view, + * yielding significant computational savings and results at least + * as good as common bilinear (averaging) spatial downsampling. + * + * For N>8 we apply a partial NxN IDCT on the 8 input coefficients as + * lower frequencies and higher frequencies assumed to be zero. + * It turns out that the computational effort is similar to the 8x8 IDCT + * regarding the output size. + * Furthermore, the scaling and descaling is the same for all IDCT sizes. + * + * CAUTION: We rely on the FIX() macro except for the N=1,2,4,8 cases + * since there would be too many additional constants to pre-calculate. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jdct.h" /* Private declarations for DCT subsystem */ + +#ifdef DCT_ISLOW_SUPPORTED + + +/* + * This module is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCT blocks. /* deliberate syntax err */ +#endif + + +/* + * The poop on this scaling stuff is as follows: + * + * Each 1-D IDCT step produces outputs which are a factor of sqrt(N) + * larger than the true IDCT outputs. The final outputs are therefore + * a factor of N larger than desired; since N=8 this can be cured by + * a simple right shift at the end of the algorithm. The advantage of + * this arrangement is that we save two multiplications per 1-D IDCT, + * because the y0 and y4 inputs need not be divided by sqrt(N). + * + * We have to do addition and subtraction of the integer inputs, which + * is no problem, and multiplication by fractional constants, which is + * a problem to do in integer arithmetic. We multiply all the constants + * by CONST_SCALE and convert them to integer constants (thus retaining + * CONST_BITS bits of precision in the constants). After doing a + * multiplication we have to divide the product by CONST_SCALE, with proper + * rounding, to produce the correct output. This division can be done + * cheaply as a right shift of CONST_BITS bits. We postpone shifting + * as long as possible so that partial sums can be added together with + * full fractional precision. + * + * The outputs of the first pass are scaled up by PASS1_BITS bits so that + * they are represented to better-than-integral precision. These outputs + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word + * with the recommended scaling. (To scale up 12-bit sample data further, an + * intermediate INT32 array would be needed.) + * + * To avoid overflow of the 32-bit intermediate results in pass 2, we must + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis + * shows that the values given below are the most effective. + */ + +#if BITS_IN_JSAMPLE == 8 +#define CONST_BITS 13 +#define PASS1_BITS 2 +#else +#define CONST_BITS 13 +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#endif + +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus + * causing a lot of useless floating-point operations at run time. + * To get around this we use the following pre-calculated constants. + * If you change CONST_BITS you may want to add appropriate values. + * (With a reasonable C compiler, you can just rely on the FIX() macro...) + */ + +#if CONST_BITS == 13 +#define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */ +#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */ +#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */ +#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ +#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ +#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */ +#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */ +#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ +#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */ +#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */ +#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ +#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */ +#else +#define FIX_0_298631336 FIX(0.298631336) +#define FIX_0_390180644 FIX(0.390180644) +#define FIX_0_541196100 FIX(0.541196100) +#define FIX_0_765366865 FIX(0.765366865) +#define FIX_0_899976223 FIX(0.899976223) +#define FIX_1_175875602 FIX(1.175875602) +#define FIX_1_501321110 FIX(1.501321110) +#define FIX_1_847759065 FIX(1.847759065) +#define FIX_1_961570560 FIX(1.961570560) +#define FIX_2_053119869 FIX(2.053119869) +#define FIX_2_562915447 FIX(2.562915447) +#define FIX_3_072711026 FIX(3.072711026) +#endif + + +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. + * For 8-bit samples with the recommended scaling, all the variable + * and constant values involved are no more than 16 bits wide, so a + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. + * For 12-bit samples, a full 32-bit multiplication will be needed. + */ + +#if BITS_IN_JSAMPLE == 8 +#define MULTIPLY(var,const) MULTIPLY16C16(var,const) +#else +#define MULTIPLY(var,const) ((var) * (const)) +#endif + + +/* Dequantize a coefficient by multiplying it by the multiplier-table + * entry; produce an int result. In this module, both inputs and result + * are 16 bits or less, so either int or short multiply will work. + */ + +#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval)) + + +/* + * Perform dequantization and inverse DCT on one block of coefficients. + */ + +GLOBAL(void) +jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[DCTSIZE2]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = DCTSIZE; ctr > 0; ctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any column in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * column DCT calculations can be simplified this way. + */ + + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && + inptr[DCTSIZE*7] == 0) { + /* AC terms all zero */ + int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; + + wsptr[DCTSIZE*0] = dcval; + wsptr[DCTSIZE*1] = dcval; + wsptr[DCTSIZE*2] = dcval; + wsptr[DCTSIZE*3] = dcval; + wsptr[DCTSIZE*4] = dcval; + wsptr[DCTSIZE*5] = dcval; + wsptr[DCTSIZE*6] = dcval; + wsptr[DCTSIZE*7] = dcval; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + continue; + } + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865); + tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); + + z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z2 <<= CONST_BITS; + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z2 += ONE << (CONST_BITS-PASS1_BITS-1); + + tmp0 = z2 + z3; + tmp1 = z2 - z3; + + tmp10 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + tmp11 = tmp1 + tmp3; + tmp12 = tmp1 - tmp3; + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + + z2 = tmp0 + tmp2; + z3 = tmp1 + tmp3; + + z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */ + z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + z2 += z1; + z3 += z1; + + z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + tmp0 += z1 + z2; + tmp3 += z1 + z3; + + z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp1 += z1 + z3; + tmp2 += z1 + z2; + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + wsptr[DCTSIZE*0] = (int) RIGHT_SHIFT(tmp10 + tmp3, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*7] = (int) RIGHT_SHIFT(tmp10 - tmp3, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*1] = (int) RIGHT_SHIFT(tmp11 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*6] = (int) RIGHT_SHIFT(tmp11 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*2] = (int) RIGHT_SHIFT(tmp12 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*5] = (int) RIGHT_SHIFT(tmp12 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*3] = (int) RIGHT_SHIFT(tmp13 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*4] = (int) RIGHT_SHIFT(tmp13 - tmp0, CONST_BITS-PASS1_BITS); + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + } + + /* Pass 2: process rows from work array, store into output array. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + wsptr = workspace; + for (ctr = 0; ctr < DCTSIZE; ctr++) { + outptr = output_buf[ctr] + output_col; + /* Rows of zeroes can be exploited in the same way as we did with columns. + * However, the column calculation has created many nonzero AC terms, so + * the simplification applies less often (typically 5% to 10% of the time). + * On machines with very fast multiplication, it's possible that the + * test takes more time than it's worth. In that case this section + * may be commented out. + */ + +#ifndef NO_ZERO_ROW_TEST + if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && + wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { + /* AC terms all zero */ + JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) + & RANGE_MASK]; + + outptr[0] = dcval; + outptr[1] = dcval; + outptr[2] = dcval; + outptr[3] = dcval; + outptr[4] = dcval; + outptr[5] = dcval; + outptr[6] = dcval; + outptr[7] = dcval; + + wsptr += DCTSIZE; /* advance pointer to next row */ + continue; + } +#endif + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[6]; + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865); + tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); + + /* Add fudge factor here for final descale. */ + z2 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 = (INT32) wsptr[4]; + + tmp0 = (z2 + z3) << CONST_BITS; + tmp1 = (z2 - z3) << CONST_BITS; + + tmp10 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + tmp11 = tmp1 + tmp3; + tmp12 = tmp1 - tmp3; + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + tmp0 = (INT32) wsptr[7]; + tmp1 = (INT32) wsptr[5]; + tmp2 = (INT32) wsptr[3]; + tmp3 = (INT32) wsptr[1]; + + z2 = tmp0 + tmp2; + z3 = tmp1 + tmp3; + + z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */ + z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + z2 += z1; + z3 += z1; + + z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + tmp0 += z1 + z2; + tmp3 += z1 + z3; + + z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp1 += z1 + z3; + tmp2 += z1 + z2; + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += DCTSIZE; /* advance pointer to next row */ + } +} + +#ifdef IDCT_SCALING_SUPPORTED + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 7x7 output block. + * + * Optimized algorithm with 12 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/14). + */ + +GLOBAL(void) +jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12, tmp13; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[7*7]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 7; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp13 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp13 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp13 += ONE << (CONST_BITS-PASS1_BITS-1); + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp10 = MULTIPLY(z2 - z3, FIX(0.881747734)); /* c4 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.314692123)); /* c6 */ + tmp11 = tmp10 + tmp12 + tmp13 - MULTIPLY(z2, FIX(1.841218003)); /* c2+c4-c6 */ + tmp0 = z1 + z3; + z2 -= tmp0; + tmp0 = MULTIPLY(tmp0, FIX(1.274162392)) + tmp13; /* c2 */ + tmp10 += tmp0 - MULTIPLY(z3, FIX(0.077722536)); /* c2-c4-c6 */ + tmp12 += tmp0 - MULTIPLY(z1, FIX(2.470602249)); /* c2+c4+c6 */ + tmp13 += MULTIPLY(z2, FIX(1.414213562)); /* c0 */ + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + + tmp1 = MULTIPLY(z1 + z2, FIX(0.935414347)); /* (c3+c1-c5)/2 */ + tmp2 = MULTIPLY(z1 - z2, FIX(0.170262339)); /* (c3+c5-c1)/2 */ + tmp0 = tmp1 - tmp2; + tmp1 += tmp2; + tmp2 = MULTIPLY(z2 + z3, - FIX(1.378756276)); /* -c1 */ + tmp1 += tmp2; + z2 = MULTIPLY(z1 + z3, FIX(0.613604268)); /* c5 */ + tmp0 += z2; + tmp2 += z2 + MULTIPLY(z3, FIX(1.870828693)); /* c3+c1-c5 */ + + /* Final output stage */ + + wsptr[7*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[7*6] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[7*1] = (int) RIGHT_SHIFT(tmp11 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[7*5] = (int) RIGHT_SHIFT(tmp11 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[7*2] = (int) RIGHT_SHIFT(tmp12 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[7*4] = (int) RIGHT_SHIFT(tmp12 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[7*3] = (int) RIGHT_SHIFT(tmp13, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 7 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 7; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp13 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp13 <<= CONST_BITS; + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[4]; + z3 = (INT32) wsptr[6]; + + tmp10 = MULTIPLY(z2 - z3, FIX(0.881747734)); /* c4 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.314692123)); /* c6 */ + tmp11 = tmp10 + tmp12 + tmp13 - MULTIPLY(z2, FIX(1.841218003)); /* c2+c4-c6 */ + tmp0 = z1 + z3; + z2 -= tmp0; + tmp0 = MULTIPLY(tmp0, FIX(1.274162392)) + tmp13; /* c2 */ + tmp10 += tmp0 - MULTIPLY(z3, FIX(0.077722536)); /* c2-c4-c6 */ + tmp12 += tmp0 - MULTIPLY(z1, FIX(2.470602249)); /* c2+c4+c6 */ + tmp13 += MULTIPLY(z2, FIX(1.414213562)); /* c0 */ + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + + tmp1 = MULTIPLY(z1 + z2, FIX(0.935414347)); /* (c3+c1-c5)/2 */ + tmp2 = MULTIPLY(z1 - z2, FIX(0.170262339)); /* (c3+c5-c1)/2 */ + tmp0 = tmp1 - tmp2; + tmp1 += tmp2; + tmp2 = MULTIPLY(z2 + z3, - FIX(1.378756276)); /* -c1 */ + tmp1 += tmp2; + z2 = MULTIPLY(z1 + z3, FIX(0.613604268)); /* c5 */ + tmp0 += z2; + tmp2 += z2 + MULTIPLY(z3, FIX(1.870828693)); /* c3+c1-c5 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 7; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 6x6 output block. + * + * Optimized algorithm with 3 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/12). + */ + +GLOBAL(void) +jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[6*6]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */ + tmp1 = tmp0 + tmp10; + tmp11 = RIGHT_SHIFT(tmp0 - tmp10 - tmp10, CONST_BITS-PASS1_BITS); + tmp10 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp0 = MULTIPLY(tmp10, FIX(1.224744871)); /* c2 */ + tmp10 = tmp1 + tmp0; + tmp12 = tmp1 - tmp0; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ + tmp0 = tmp1 + ((z1 + z2) << CONST_BITS); + tmp2 = tmp1 + ((z3 - z2) << CONST_BITS); + tmp1 = (z1 - z2 - z3) << PASS1_BITS; + + /* Final output stage */ + + wsptr[6*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[6*5] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[6*1] = (int) (tmp11 + tmp1); + wsptr[6*4] = (int) (tmp11 - tmp1); + wsptr[6*2] = (int) RIGHT_SHIFT(tmp12 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[6*3] = (int) RIGHT_SHIFT(tmp12 - tmp2, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 6 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + tmp2 = (INT32) wsptr[4]; + tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */ + tmp1 = tmp0 + tmp10; + tmp11 = tmp0 - tmp10 - tmp10; + tmp10 = (INT32) wsptr[2]; + tmp0 = MULTIPLY(tmp10, FIX(1.224744871)); /* c2 */ + tmp10 = tmp1 + tmp0; + tmp12 = tmp1 - tmp0; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ + tmp0 = tmp1 + ((z1 + z2) << CONST_BITS); + tmp2 = tmp1 + ((z3 - z2) << CONST_BITS); + tmp1 = (z1 - z2 - z3) << CONST_BITS; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 6; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 5x5 output block. + * + * Optimized algorithm with 5 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/10). + */ + +GLOBAL(void) +jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp10, tmp11, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[5*5]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 5; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp12 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp12 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp12 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp0 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z1 = MULTIPLY(tmp0 + tmp1, FIX(0.790569415)); /* (c2+c4)/2 */ + z2 = MULTIPLY(tmp0 - tmp1, FIX(0.353553391)); /* (c2-c4)/2 */ + z3 = tmp12 + z2; + tmp10 = z3 + z1; + tmp11 = z3 - z1; + tmp12 -= z2 << 2; + + /* Odd part */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c3 */ + tmp0 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c1-c3 */ + tmp1 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c1+c3 */ + + /* Final output stage */ + + wsptr[5*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[5*4] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[5*1] = (int) RIGHT_SHIFT(tmp11 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[5*3] = (int) RIGHT_SHIFT(tmp11 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[5*2] = (int) RIGHT_SHIFT(tmp12, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 5 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 5; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp12 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp12 <<= CONST_BITS; + tmp0 = (INT32) wsptr[2]; + tmp1 = (INT32) wsptr[4]; + z1 = MULTIPLY(tmp0 + tmp1, FIX(0.790569415)); /* (c2+c4)/2 */ + z2 = MULTIPLY(tmp0 - tmp1, FIX(0.353553391)); /* (c2-c4)/2 */ + z3 = tmp12 + z2; + tmp10 = z3 + z1; + tmp11 = z3 - z1; + tmp12 -= z2 << 2; + + /* Odd part */ + + z2 = (INT32) wsptr[1]; + z3 = (INT32) wsptr[3]; + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c3 */ + tmp0 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c1-c3 */ + tmp1 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c1+c3 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 5; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 4x4 output block. + * + * Optimized algorithm with 3 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT]. + */ + +GLOBAL(void) +jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp2, tmp10, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[4*4]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 4; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + + tmp10 = (tmp0 + tmp2) << PASS1_BITS; + tmp12 = (tmp0 - tmp2) << PASS1_BITS; + + /* Odd part */ + /* Same rotation as in the even part of the 8x8 LL&M IDCT */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp0 = RIGHT_SHIFT(z1 + MULTIPLY(z2, FIX_0_765366865), /* c2-c6 */ + CONST_BITS-PASS1_BITS); + tmp2 = RIGHT_SHIFT(z1 - MULTIPLY(z3, FIX_1_847759065), /* c2+c6 */ + CONST_BITS-PASS1_BITS); + + /* Final output stage */ + + wsptr[4*0] = (int) (tmp10 + tmp0); + wsptr[4*3] = (int) (tmp10 - tmp0); + wsptr[4*1] = (int) (tmp12 + tmp2); + wsptr[4*2] = (int) (tmp12 - tmp2); + } + + /* Pass 2: process 4 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 4; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp2 = (INT32) wsptr[2]; + + tmp10 = (tmp0 + tmp2) << CONST_BITS; + tmp12 = (tmp0 - tmp2) << CONST_BITS; + + /* Odd part */ + /* Same rotation as in the even part of the 8x8 LL&M IDCT */ + + z2 = (INT32) wsptr[1]; + z3 = (INT32) wsptr[3]; + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ + tmp0 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */ + tmp2 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 4; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 3x3 output block. + * + * Optimized algorithm with 2 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/6). + */ + +GLOBAL(void) +jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp2, tmp10, tmp12; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[3*3]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 3; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */ + tmp10 = tmp0 + tmp12; + tmp2 = tmp0 - tmp12 - tmp12; + + /* Odd part */ + + tmp12 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + tmp0 = MULTIPLY(tmp12, FIX(1.224744871)); /* c1 */ + + /* Final output stage */ + + wsptr[3*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[3*2] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[3*1] = (int) RIGHT_SHIFT(tmp2, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 3 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 3; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + tmp2 = (INT32) wsptr[2]; + tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */ + tmp10 = tmp0 + tmp12; + tmp2 = tmp0 - tmp12 - tmp12; + + /* Odd part */ + + tmp12 = (INT32) wsptr[1]; + tmp0 = MULTIPLY(tmp12, FIX(1.224744871)); /* c1 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 3; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 2x2 output block. + * + * Multiplication-less algorithm. + */ + +GLOBAL(void) +jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5; + ISLOW_MULT_TYPE * quantptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + SHIFT_TEMPS + + /* Pass 1: process columns from input. */ + + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + + /* Column 0 */ + tmp4 = DEQUANTIZE(coef_block[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp5 = DEQUANTIZE(coef_block[DCTSIZE*1], quantptr[DCTSIZE*1]); + /* Add fudge factor here for final descale. */ + tmp4 += ONE << 2; + + tmp0 = tmp4 + tmp5; + tmp2 = tmp4 - tmp5; + + /* Column 1 */ + tmp4 = DEQUANTIZE(coef_block[DCTSIZE*0+1], quantptr[DCTSIZE*0+1]); + tmp5 = DEQUANTIZE(coef_block[DCTSIZE*1+1], quantptr[DCTSIZE*1+1]); + + tmp1 = tmp4 + tmp5; + tmp3 = tmp4 - tmp5; + + /* Pass 2: process 2 rows, store into output array. */ + + /* Row 0 */ + outptr = output_buf[0] + output_col; + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK]; + + /* Row 1 */ + outptr = output_buf[1] + output_col; + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp2 + tmp3, 3) & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2 - tmp3, 3) & RANGE_MASK]; +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 1x1 output block. + * + * We hardly need an inverse DCT routine for this: just take the + * average pixel value, which is one-eighth of the DC coefficient. + */ + +GLOBAL(void) +jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + int dcval; + ISLOW_MULT_TYPE * quantptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + SHIFT_TEMPS + + /* 1x1 is trivial: just take the DC coefficient divided by 8. */ + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + dcval = DEQUANTIZE(coef_block[0], quantptr[0]); + dcval = (int) DESCALE((INT32) dcval, 3); + + output_buf[0][output_col] = range_limit[dcval & RANGE_MASK]; +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 9x9 output block. + * + * Optimized algorithm with 10 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/18). + */ + +GLOBAL(void) +jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13, tmp14; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*9]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-1); + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp3 = MULTIPLY(z3, FIX(0.707106781)); /* c6 */ + tmp1 = tmp0 + tmp3; + tmp2 = tmp0 - tmp3 - tmp3; + + tmp0 = MULTIPLY(z1 - z2, FIX(0.707106781)); /* c6 */ + tmp11 = tmp2 + tmp0; + tmp14 = tmp2 - tmp0 - tmp0; + + tmp0 = MULTIPLY(z1 + z2, FIX(1.328926049)); /* c2 */ + tmp2 = MULTIPLY(z1, FIX(1.083350441)); /* c4 */ + tmp3 = MULTIPLY(z2, FIX(0.245575608)); /* c8 */ + + tmp10 = tmp1 + tmp0 - tmp3; + tmp12 = tmp1 - tmp0 + tmp2; + tmp13 = tmp1 - tmp2 + tmp3; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + z2 = MULTIPLY(z2, - FIX(1.224744871)); /* -c3 */ + + tmp2 = MULTIPLY(z1 + z3, FIX(0.909038955)); /* c5 */ + tmp3 = MULTIPLY(z1 + z4, FIX(0.483689525)); /* c7 */ + tmp0 = tmp2 + tmp3 - z2; + tmp1 = MULTIPLY(z3 - z4, FIX(1.392728481)); /* c1 */ + tmp2 += z2 - tmp1; + tmp3 += z2 + tmp1; + tmp1 = MULTIPLY(z1 - z3 - z4, FIX(1.224744871)); /* c3 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp11 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp11 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp12 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp12 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp13 + tmp3, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp13 - tmp3, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp14, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 9 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 9; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[4]; + z3 = (INT32) wsptr[6]; + + tmp3 = MULTIPLY(z3, FIX(0.707106781)); /* c6 */ + tmp1 = tmp0 + tmp3; + tmp2 = tmp0 - tmp3 - tmp3; + + tmp0 = MULTIPLY(z1 - z2, FIX(0.707106781)); /* c6 */ + tmp11 = tmp2 + tmp0; + tmp14 = tmp2 - tmp0 - tmp0; + + tmp0 = MULTIPLY(z1 + z2, FIX(1.328926049)); /* c2 */ + tmp2 = MULTIPLY(z1, FIX(1.083350441)); /* c4 */ + tmp3 = MULTIPLY(z2, FIX(0.245575608)); /* c8 */ + + tmp10 = tmp1 + tmp0 - tmp3; + tmp12 = tmp1 - tmp0 + tmp2; + tmp13 = tmp1 - tmp2 + tmp3; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + z2 = MULTIPLY(z2, - FIX(1.224744871)); /* -c3 */ + + tmp2 = MULTIPLY(z1 + z3, FIX(0.909038955)); /* c5 */ + tmp3 = MULTIPLY(z1 + z4, FIX(0.483689525)); /* c7 */ + tmp0 = tmp2 + tmp3 - z2; + tmp1 = MULTIPLY(z3 - z4, FIX(1.392728481)); /* c1 */ + tmp2 += z2 - tmp1; + tmp3 += z2 + tmp1; + tmp1 = MULTIPLY(z1 - z3 - z4, FIX(1.224744871)); /* c3 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 10x10 output block. + * + * Optimized algorithm with 12 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/20). + */ + +GLOBAL(void) +jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24; + INT32 z1, z2, z3, z4, z5; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*10]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z3 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z3 += ONE << (CONST_BITS-PASS1_BITS-1); + z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */ + z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */ + tmp10 = z3 + z1; + tmp11 = z3 - z2; + + tmp22 = RIGHT_SHIFT(z3 - ((z1 - z2) << 1), /* c0 = (c4-c8)*2 */ + CONST_BITS-PASS1_BITS); + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c6 */ + tmp12 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c2-c6 */ + tmp13 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c2+c6 */ + + tmp20 = tmp10 + tmp12; + tmp24 = tmp10 - tmp12; + tmp21 = tmp11 + tmp13; + tmp23 = tmp11 - tmp13; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = z2 + z4; + tmp13 = z2 - z4; + + tmp12 = MULTIPLY(tmp13, FIX(0.309016994)); /* (c3-c7)/2 */ + z5 = z3 << CONST_BITS; + + z2 = MULTIPLY(tmp11, FIX(0.951056516)); /* (c3+c7)/2 */ + z4 = z5 + tmp12; + + tmp10 = MULTIPLY(z1, FIX(1.396802247)) + z2 + z4; /* c1 */ + tmp14 = MULTIPLY(z1, FIX(0.221231742)) - z2 + z4; /* c9 */ + + z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */ + z4 = z5 - tmp12 - (tmp13 << (CONST_BITS - 1)); + + tmp12 = (z1 - tmp13 - z3) << PASS1_BITS; + + tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */ + tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) (tmp22 + tmp12); + wsptr[8*7] = (int) (tmp22 - tmp12); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 10 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 10; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 <<= CONST_BITS; + z4 = (INT32) wsptr[4]; + z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */ + z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */ + tmp10 = z3 + z1; + tmp11 = z3 - z2; + + tmp22 = z3 - ((z1 - z2) << 1); /* c0 = (c4-c8)*2 */ + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[6]; + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c6 */ + tmp12 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c2-c6 */ + tmp13 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c2+c6 */ + + tmp20 = tmp10 + tmp12; + tmp24 = tmp10 - tmp12; + tmp21 = tmp11 + tmp13; + tmp23 = tmp11 - tmp13; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z3 <<= CONST_BITS; + z4 = (INT32) wsptr[7]; + + tmp11 = z2 + z4; + tmp13 = z2 - z4; + + tmp12 = MULTIPLY(tmp13, FIX(0.309016994)); /* (c3-c7)/2 */ + + z2 = MULTIPLY(tmp11, FIX(0.951056516)); /* (c3+c7)/2 */ + z4 = z3 + tmp12; + + tmp10 = MULTIPLY(z1, FIX(1.396802247)) + z2 + z4; /* c1 */ + tmp14 = MULTIPLY(z1, FIX(0.221231742)) - z2 + z4; /* c9 */ + + z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */ + z4 = z3 - tmp12 - (tmp13 << (CONST_BITS - 1)); + + tmp12 = ((z1 - tmp13) << CONST_BITS) - z3; + + tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */ + tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 11x11 output block. + * + * Optimized algorithm with 24 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/22). + */ + +GLOBAL(void) +jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*11]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp10 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp10 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp10 += ONE << (CONST_BITS-PASS1_BITS-1); + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp20 = MULTIPLY(z2 - z3, FIX(2.546640132)); /* c2+c4 */ + tmp23 = MULTIPLY(z2 - z1, FIX(0.430815045)); /* c2-c6 */ + z4 = z1 + z3; + tmp24 = MULTIPLY(z4, - FIX(1.155664402)); /* -(c2-c10) */ + z4 -= z2; + tmp25 = tmp10 + MULTIPLY(z4, FIX(1.356927976)); /* c2 */ + tmp21 = tmp20 + tmp23 + tmp25 - + MULTIPLY(z2, FIX(1.821790775)); /* c2+c4+c10-c6 */ + tmp20 += tmp25 + MULTIPLY(z3, FIX(2.115825087)); /* c4+c6 */ + tmp23 += tmp25 - MULTIPLY(z1, FIX(1.513598477)); /* c6+c8 */ + tmp24 += tmp25; + tmp22 = tmp24 - MULTIPLY(z3, FIX(0.788749120)); /* c8+c10 */ + tmp24 += MULTIPLY(z2, FIX(1.944413522)) - /* c2+c8 */ + MULTIPLY(z1, FIX(1.390975730)); /* c4+c10 */ + tmp25 = tmp10 - MULTIPLY(z4, FIX(1.414213562)); /* c0 */ + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = z1 + z2; + tmp14 = MULTIPLY(tmp11 + z3 + z4, FIX(0.398430003)); /* c9 */ + tmp11 = MULTIPLY(tmp11, FIX(0.887983902)); /* c3-c9 */ + tmp12 = MULTIPLY(z1 + z3, FIX(0.670361295)); /* c5-c9 */ + tmp13 = tmp14 + MULTIPLY(z1 + z4, FIX(0.366151574)); /* c7-c9 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(z1, FIX(0.923107866)); /* c7+c5+c3-c1-2*c9 */ + z1 = tmp14 - MULTIPLY(z2 + z3, FIX(1.163011579)); /* c7+c9 */ + tmp11 += z1 + MULTIPLY(z2, FIX(2.073276588)); /* c1+c7+3*c9-c3 */ + tmp12 += z1 - MULTIPLY(z3, FIX(1.192193623)); /* c3+c5-c7-c9 */ + z1 = MULTIPLY(z2 + z4, - FIX(1.798248910)); /* -(c1+c9) */ + tmp11 += z1; + tmp13 += z1 + MULTIPLY(z4, FIX(2.102458632)); /* c1+c5+c9-c7 */ + tmp14 += MULTIPLY(z2, - FIX(1.467221301)) + /* -(c5+c9) */ + MULTIPLY(z3, FIX(1.001388905)) - /* c1-c9 */ + MULTIPLY(z4, FIX(1.684843907)); /* c3+c9 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*10] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 11 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 11; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp10 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp10 <<= CONST_BITS; + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[4]; + z3 = (INT32) wsptr[6]; + + tmp20 = MULTIPLY(z2 - z3, FIX(2.546640132)); /* c2+c4 */ + tmp23 = MULTIPLY(z2 - z1, FIX(0.430815045)); /* c2-c6 */ + z4 = z1 + z3; + tmp24 = MULTIPLY(z4, - FIX(1.155664402)); /* -(c2-c10) */ + z4 -= z2; + tmp25 = tmp10 + MULTIPLY(z4, FIX(1.356927976)); /* c2 */ + tmp21 = tmp20 + tmp23 + tmp25 - + MULTIPLY(z2, FIX(1.821790775)); /* c2+c4+c10-c6 */ + tmp20 += tmp25 + MULTIPLY(z3, FIX(2.115825087)); /* c4+c6 */ + tmp23 += tmp25 - MULTIPLY(z1, FIX(1.513598477)); /* c6+c8 */ + tmp24 += tmp25; + tmp22 = tmp24 - MULTIPLY(z3, FIX(0.788749120)); /* c8+c10 */ + tmp24 += MULTIPLY(z2, FIX(1.944413522)) - /* c2+c8 */ + MULTIPLY(z1, FIX(1.390975730)); /* c4+c10 */ + tmp25 = tmp10 - MULTIPLY(z4, FIX(1.414213562)); /* c0 */ + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + tmp11 = z1 + z2; + tmp14 = MULTIPLY(tmp11 + z3 + z4, FIX(0.398430003)); /* c9 */ + tmp11 = MULTIPLY(tmp11, FIX(0.887983902)); /* c3-c9 */ + tmp12 = MULTIPLY(z1 + z3, FIX(0.670361295)); /* c5-c9 */ + tmp13 = tmp14 + MULTIPLY(z1 + z4, FIX(0.366151574)); /* c7-c9 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(z1, FIX(0.923107866)); /* c7+c5+c3-c1-2*c9 */ + z1 = tmp14 - MULTIPLY(z2 + z3, FIX(1.163011579)); /* c7+c9 */ + tmp11 += z1 + MULTIPLY(z2, FIX(2.073276588)); /* c1+c7+3*c9-c3 */ + tmp12 += z1 - MULTIPLY(z3, FIX(1.192193623)); /* c3+c5-c7-c9 */ + z1 = MULTIPLY(z2 + z4, - FIX(1.798248910)); /* -(c1+c9) */ + tmp11 += z1; + tmp13 += z1 + MULTIPLY(z4, FIX(2.102458632)); /* c1+c5+c9-c7 */ + tmp14 += MULTIPLY(z2, - FIX(1.467221301)) + /* -(c5+c9) */ + MULTIPLY(z3, FIX(1.001388905)) - /* c1-c9 */ + MULTIPLY(z4, FIX(1.684843907)); /* c3+c9 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 12x12 output block. + * + * Optimized algorithm with 15 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/24). + */ + +GLOBAL(void) +jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*12]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z3 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z3 += ONE << (CONST_BITS-PASS1_BITS-1); + + z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */ + + tmp10 = z3 + z4; + tmp11 = z3 - z4; + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z4 = MULTIPLY(z1, FIX(1.366025404)); /* c2 */ + z1 <<= CONST_BITS; + z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + z2 <<= CONST_BITS; + + tmp12 = z1 - z2; + + tmp21 = z3 + tmp12; + tmp24 = z3 - tmp12; + + tmp12 = z4 + z2; + + tmp20 = tmp10 + tmp12; + tmp25 = tmp10 - tmp12; + + tmp12 = z4 - z1 - z2; + + tmp22 = tmp11 + tmp12; + tmp23 = tmp11 - tmp12; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = MULTIPLY(z2, FIX(1.306562965)); /* c3 */ + tmp14 = MULTIPLY(z2, - FIX_0_541196100); /* -c9 */ + + tmp10 = z1 + z3; + tmp15 = MULTIPLY(tmp10 + z4, FIX(0.860918669)); /* c7 */ + tmp12 = tmp15 + MULTIPLY(tmp10, FIX(0.261052384)); /* c5-c7 */ + tmp10 = tmp12 + tmp11 + MULTIPLY(z1, FIX(0.280143716)); /* c1-c5 */ + tmp13 = MULTIPLY(z3 + z4, - FIX(1.045510580)); /* -(c7+c11) */ + tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */ + tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */ + tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) - /* c7-c11 */ + MULTIPLY(z4, FIX(1.982889723)); /* c5+c7 */ + + z1 -= z4; + z2 -= z3; + z3 = MULTIPLY(z1 + z2, FIX_0_541196100); /* c9 */ + tmp11 = z3 + MULTIPLY(z1, FIX_0_765366865); /* c3-c9 */ + tmp14 = z3 - MULTIPLY(z2, FIX_1_847759065); /* c3+c9 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*11] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*10] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25 + tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp25 - tmp15, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 12 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 12; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 <<= CONST_BITS; + + z4 = (INT32) wsptr[4]; + z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */ + + tmp10 = z3 + z4; + tmp11 = z3 - z4; + + z1 = (INT32) wsptr[2]; + z4 = MULTIPLY(z1, FIX(1.366025404)); /* c2 */ + z1 <<= CONST_BITS; + z2 = (INT32) wsptr[6]; + z2 <<= CONST_BITS; + + tmp12 = z1 - z2; + + tmp21 = z3 + tmp12; + tmp24 = z3 - tmp12; + + tmp12 = z4 + z2; + + tmp20 = tmp10 + tmp12; + tmp25 = tmp10 - tmp12; + + tmp12 = z4 - z1 - z2; + + tmp22 = tmp11 + tmp12; + tmp23 = tmp11 - tmp12; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + tmp11 = MULTIPLY(z2, FIX(1.306562965)); /* c3 */ + tmp14 = MULTIPLY(z2, - FIX_0_541196100); /* -c9 */ + + tmp10 = z1 + z3; + tmp15 = MULTIPLY(tmp10 + z4, FIX(0.860918669)); /* c7 */ + tmp12 = tmp15 + MULTIPLY(tmp10, FIX(0.261052384)); /* c5-c7 */ + tmp10 = tmp12 + tmp11 + MULTIPLY(z1, FIX(0.280143716)); /* c1-c5 */ + tmp13 = MULTIPLY(z3 + z4, - FIX(1.045510580)); /* -(c7+c11) */ + tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */ + tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */ + tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) - /* c7-c11 */ + MULTIPLY(z4, FIX(1.982889723)); /* c5+c7 */ + + z1 -= z4; + z2 -= z3; + z3 = MULTIPLY(z1 + z2, FIX_0_541196100); /* c9 */ + tmp11 = z3 + MULTIPLY(z1, FIX_0_765366865); /* c3-c9 */ + tmp14 = z3 - MULTIPLY(z2, FIX_1_847759065); /* c3+c9 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 13x13 output block. + * + * Optimized algorithm with 29 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/26). + */ + +GLOBAL(void) +jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*13]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z1 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z4 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp10 = z3 + z4; + tmp11 = z3 - z4; + + tmp12 = MULTIPLY(tmp10, FIX(1.155388986)); /* (c4+c6)/2 */ + tmp13 = MULTIPLY(tmp11, FIX(0.096834934)) + z1; /* (c4-c6)/2 */ + + tmp20 = MULTIPLY(z2, FIX(1.373119086)) + tmp12 + tmp13; /* c2 */ + tmp22 = MULTIPLY(z2, FIX(0.501487041)) - tmp12 + tmp13; /* c10 */ + + tmp12 = MULTIPLY(tmp10, FIX(0.316450131)); /* (c8-c12)/2 */ + tmp13 = MULTIPLY(tmp11, FIX(0.486914739)) + z1; /* (c8+c12)/2 */ + + tmp21 = MULTIPLY(z2, FIX(1.058554052)) - tmp12 + tmp13; /* c6 */ + tmp25 = MULTIPLY(z2, - FIX(1.252223920)) + tmp12 + tmp13; /* c4 */ + + tmp12 = MULTIPLY(tmp10, FIX(0.435816023)); /* (c2-c10)/2 */ + tmp13 = MULTIPLY(tmp11, FIX(0.937303064)) - z1; /* (c2+c10)/2 */ + + tmp23 = MULTIPLY(z2, - FIX(0.170464608)) - tmp12 - tmp13; /* c12 */ + tmp24 = MULTIPLY(z2, - FIX(0.803364869)) + tmp12 - tmp13; /* c8 */ + + tmp26 = MULTIPLY(tmp11 - z2, FIX(1.414213562)) + z1; /* c0 */ + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = MULTIPLY(z1 + z2, FIX(1.322312651)); /* c3 */ + tmp12 = MULTIPLY(z1 + z3, FIX(1.163874945)); /* c5 */ + tmp15 = z1 + z4; + tmp13 = MULTIPLY(tmp15, FIX(0.937797057)); /* c7 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(z1, FIX(2.020082300)); /* c7+c5+c3-c1 */ + tmp14 = MULTIPLY(z2 + z3, - FIX(0.338443458)); /* -c11 */ + tmp11 += tmp14 + MULTIPLY(z2, FIX(0.837223564)); /* c5+c9+c11-c3 */ + tmp12 += tmp14 - MULTIPLY(z3, FIX(1.572116027)); /* c1+c5-c9-c11 */ + tmp14 = MULTIPLY(z2 + z4, - FIX(1.163874945)); /* -c5 */ + tmp11 += tmp14; + tmp13 += tmp14 + MULTIPLY(z4, FIX(2.205608352)); /* c3+c5+c9-c7 */ + tmp14 = MULTIPLY(z3 + z4, - FIX(0.657217813)); /* -c9 */ + tmp12 += tmp14; + tmp13 += tmp14; + tmp15 = MULTIPLY(tmp15, FIX(0.338443458)); /* c11 */ + tmp14 = tmp15 + MULTIPLY(z1, FIX(0.318774355)) - /* c9-c11 */ + MULTIPLY(z2, FIX(0.466105296)); /* c1-c7 */ + z1 = MULTIPLY(z3 - z2, FIX(0.937797057)); /* c7 */ + tmp14 += z1; + tmp15 += z1 + MULTIPLY(z3, FIX(0.384515595)) - /* c3-c7 */ + MULTIPLY(z4, FIX(1.742345811)); /* c1+c11 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*12] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*11] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*10] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25 + tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp25 - tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp26, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 13 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 13; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z1 <<= CONST_BITS; + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[4]; + z4 = (INT32) wsptr[6]; + + tmp10 = z3 + z4; + tmp11 = z3 - z4; + + tmp12 = MULTIPLY(tmp10, FIX(1.155388986)); /* (c4+c6)/2 */ + tmp13 = MULTIPLY(tmp11, FIX(0.096834934)) + z1; /* (c4-c6)/2 */ + + tmp20 = MULTIPLY(z2, FIX(1.373119086)) + tmp12 + tmp13; /* c2 */ + tmp22 = MULTIPLY(z2, FIX(0.501487041)) - tmp12 + tmp13; /* c10 */ + + tmp12 = MULTIPLY(tmp10, FIX(0.316450131)); /* (c8-c12)/2 */ + tmp13 = MULTIPLY(tmp11, FIX(0.486914739)) + z1; /* (c8+c12)/2 */ + + tmp21 = MULTIPLY(z2, FIX(1.058554052)) - tmp12 + tmp13; /* c6 */ + tmp25 = MULTIPLY(z2, - FIX(1.252223920)) + tmp12 + tmp13; /* c4 */ + + tmp12 = MULTIPLY(tmp10, FIX(0.435816023)); /* (c2-c10)/2 */ + tmp13 = MULTIPLY(tmp11, FIX(0.937303064)) - z1; /* (c2+c10)/2 */ + + tmp23 = MULTIPLY(z2, - FIX(0.170464608)) - tmp12 - tmp13; /* c12 */ + tmp24 = MULTIPLY(z2, - FIX(0.803364869)) + tmp12 - tmp13; /* c8 */ + + tmp26 = MULTIPLY(tmp11 - z2, FIX(1.414213562)) + z1; /* c0 */ + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + tmp11 = MULTIPLY(z1 + z2, FIX(1.322312651)); /* c3 */ + tmp12 = MULTIPLY(z1 + z3, FIX(1.163874945)); /* c5 */ + tmp15 = z1 + z4; + tmp13 = MULTIPLY(tmp15, FIX(0.937797057)); /* c7 */ + tmp10 = tmp11 + tmp12 + tmp13 - + MULTIPLY(z1, FIX(2.020082300)); /* c7+c5+c3-c1 */ + tmp14 = MULTIPLY(z2 + z3, - FIX(0.338443458)); /* -c11 */ + tmp11 += tmp14 + MULTIPLY(z2, FIX(0.837223564)); /* c5+c9+c11-c3 */ + tmp12 += tmp14 - MULTIPLY(z3, FIX(1.572116027)); /* c1+c5-c9-c11 */ + tmp14 = MULTIPLY(z2 + z4, - FIX(1.163874945)); /* -c5 */ + tmp11 += tmp14; + tmp13 += tmp14 + MULTIPLY(z4, FIX(2.205608352)); /* c3+c5+c9-c7 */ + tmp14 = MULTIPLY(z3 + z4, - FIX(0.657217813)); /* -c9 */ + tmp12 += tmp14; + tmp13 += tmp14; + tmp15 = MULTIPLY(tmp15, FIX(0.338443458)); /* c11 */ + tmp14 = tmp15 + MULTIPLY(z1, FIX(0.318774355)) - /* c9-c11 */ + MULTIPLY(z2, FIX(0.466105296)); /* c1-c7 */ + z1 = MULTIPLY(z3 - z2, FIX(0.937797057)); /* c7 */ + tmp14 += z1; + tmp15 += z1 + MULTIPLY(z3, FIX(0.384515595)) - /* c3-c7 */ + MULTIPLY(z4, FIX(1.742345811)); /* c1+c11 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 14x14 output block. + * + * Optimized algorithm with 20 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/28). + */ + +GLOBAL(void) +jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*14]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z1 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */ + z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */ + z4 = MULTIPLY(z4, FIX(0.881747734)); /* c8 */ + + tmp10 = z1 + z2; + tmp11 = z1 + z3; + tmp12 = z1 - z4; + + tmp23 = RIGHT_SHIFT(z1 - ((z2 + z3 - z4) << 1), /* c0 = (c4+c12-c8)*2 */ + CONST_BITS-PASS1_BITS); + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z3 = MULTIPLY(z1 + z2, FIX(1.105676686)); /* c6 */ + + tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */ + tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */ + tmp15 = MULTIPLY(z1, FIX(0.613604268)) - /* c10 */ + MULTIPLY(z2, FIX(1.378756276)); /* c2 */ + + tmp20 = tmp10 + tmp13; + tmp26 = tmp10 - tmp13; + tmp21 = tmp11 + tmp14; + tmp25 = tmp11 - tmp14; + tmp22 = tmp12 + tmp15; + tmp24 = tmp12 - tmp15; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + tmp13 = z4 << CONST_BITS; + + tmp14 = z1 + z3; + tmp11 = MULTIPLY(z1 + z2, FIX(1.334852607)); /* c3 */ + tmp12 = MULTIPLY(tmp14, FIX(1.197448846)); /* c5 */ + tmp10 = tmp11 + tmp12 + tmp13 - MULTIPLY(z1, FIX(1.126980169)); /* c3+c5-c1 */ + tmp14 = MULTIPLY(tmp14, FIX(0.752406978)); /* c9 */ + tmp16 = tmp14 - MULTIPLY(z1, FIX(1.061150426)); /* c9+c11-c13 */ + z1 -= z2; + tmp15 = MULTIPLY(z1, FIX(0.467085129)) - tmp13; /* c11 */ + tmp16 += tmp15; + z1 += z4; + z4 = MULTIPLY(z2 + z3, - FIX(0.158341681)) - tmp13; /* -c13 */ + tmp11 += z4 - MULTIPLY(z2, FIX(0.424103948)); /* c3-c9-c13 */ + tmp12 += z4 - MULTIPLY(z3, FIX(2.373959773)); /* c3+c5-c13 */ + z4 = MULTIPLY(z3 - z2, FIX(1.405321284)); /* c1 */ + tmp14 += z4 + tmp13 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */ + tmp15 += z4 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */ + + tmp13 = (z1 - z3) << PASS1_BITS; + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*13] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*12] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*11] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) (tmp23 + tmp13); + wsptr[8*10] = (int) (tmp23 - tmp13); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25 + tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp25 - tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp26 + tmp16, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp26 - tmp16, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 14 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 14; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z1 <<= CONST_BITS; + z4 = (INT32) wsptr[4]; + z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */ + z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */ + z4 = MULTIPLY(z4, FIX(0.881747734)); /* c8 */ + + tmp10 = z1 + z2; + tmp11 = z1 + z3; + tmp12 = z1 - z4; + + tmp23 = z1 - ((z2 + z3 - z4) << 1); /* c0 = (c4+c12-c8)*2 */ + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[6]; + + z3 = MULTIPLY(z1 + z2, FIX(1.105676686)); /* c6 */ + + tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */ + tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */ + tmp15 = MULTIPLY(z1, FIX(0.613604268)) - /* c10 */ + MULTIPLY(z2, FIX(1.378756276)); /* c2 */ + + tmp20 = tmp10 + tmp13; + tmp26 = tmp10 - tmp13; + tmp21 = tmp11 + tmp14; + tmp25 = tmp11 - tmp14; + tmp22 = tmp12 + tmp15; + tmp24 = tmp12 - tmp15; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + z4 <<= CONST_BITS; + + tmp14 = z1 + z3; + tmp11 = MULTIPLY(z1 + z2, FIX(1.334852607)); /* c3 */ + tmp12 = MULTIPLY(tmp14, FIX(1.197448846)); /* c5 */ + tmp10 = tmp11 + tmp12 + z4 - MULTIPLY(z1, FIX(1.126980169)); /* c3+c5-c1 */ + tmp14 = MULTIPLY(tmp14, FIX(0.752406978)); /* c9 */ + tmp16 = tmp14 - MULTIPLY(z1, FIX(1.061150426)); /* c9+c11-c13 */ + z1 -= z2; + tmp15 = MULTIPLY(z1, FIX(0.467085129)) - z4; /* c11 */ + tmp16 += tmp15; + tmp13 = MULTIPLY(z2 + z3, - FIX(0.158341681)) - z4; /* -c13 */ + tmp11 += tmp13 - MULTIPLY(z2, FIX(0.424103948)); /* c3-c9-c13 */ + tmp12 += tmp13 - MULTIPLY(z3, FIX(2.373959773)); /* c3+c5-c13 */ + tmp13 = MULTIPLY(z3 - z2, FIX(1.405321284)); /* c1 */ + tmp14 += tmp13 + z4 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */ + tmp15 += tmp13 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */ + + tmp13 = ((z1 - z3) << CONST_BITS) + z4; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 15x15 output block. + * + * Optimized algorithm with 22 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/30). + */ + +GLOBAL(void) +jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*15]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z1 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z4 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp10 = MULTIPLY(z4, FIX(0.437016024)); /* c12 */ + tmp11 = MULTIPLY(z4, FIX(1.144122806)); /* c6 */ + + tmp12 = z1 - tmp10; + tmp13 = z1 + tmp11; + z1 -= (tmp11 - tmp10) << 1; /* c0 = (c6-c12)*2 */ + + z4 = z2 - z3; + z3 += z2; + tmp10 = MULTIPLY(z3, FIX(1.337628990)); /* (c2+c4)/2 */ + tmp11 = MULTIPLY(z4, FIX(0.045680613)); /* (c2-c4)/2 */ + z2 = MULTIPLY(z2, FIX(1.439773946)); /* c4+c14 */ + + tmp20 = tmp13 + tmp10 + tmp11; + tmp23 = tmp12 - tmp10 + tmp11 + z2; + + tmp10 = MULTIPLY(z3, FIX(0.547059574)); /* (c8+c14)/2 */ + tmp11 = MULTIPLY(z4, FIX(0.399234004)); /* (c8-c14)/2 */ + + tmp25 = tmp13 - tmp10 - tmp11; + tmp26 = tmp12 + tmp10 - tmp11 - z2; + + tmp10 = MULTIPLY(z3, FIX(0.790569415)); /* (c6+c12)/2 */ + tmp11 = MULTIPLY(z4, FIX(0.353553391)); /* (c6-c12)/2 */ + + tmp21 = tmp12 + tmp10 + tmp11; + tmp24 = tmp13 - tmp10 + tmp11; + tmp11 += tmp11; + tmp22 = z1 + tmp11; /* c10 = c6-c12 */ + tmp27 = z1 - tmp11 - tmp11; /* c0 = (c6-c12)*2 */ + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z4 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z3 = MULTIPLY(z4, FIX(1.224744871)); /* c5 */ + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp13 = z2 - z4; + tmp15 = MULTIPLY(z1 + tmp13, FIX(0.831253876)); /* c9 */ + tmp11 = tmp15 + MULTIPLY(z1, FIX(0.513743148)); /* c3-c9 */ + tmp14 = tmp15 - MULTIPLY(tmp13, FIX(2.176250899)); /* c3+c9 */ + + tmp13 = MULTIPLY(z2, - FIX(0.831253876)); /* -c9 */ + tmp15 = MULTIPLY(z2, - FIX(1.344997024)); /* -c3 */ + z2 = z1 - z4; + tmp12 = z3 + MULTIPLY(z2, FIX(1.406466353)); /* c1 */ + + tmp10 = tmp12 + MULTIPLY(z4, FIX(2.457431844)) - tmp15; /* c1+c7 */ + tmp16 = tmp12 - MULTIPLY(z1, FIX(1.112434820)) + tmp13; /* c1-c13 */ + tmp12 = MULTIPLY(z2, FIX(1.224744871)) - z3; /* c5 */ + z2 = MULTIPLY(z1 + z4, FIX(0.575212477)); /* c11 */ + tmp13 += z2 + MULTIPLY(z1, FIX(0.475753014)) - z3; /* c7-c11 */ + tmp15 += z2 - MULTIPLY(z4, FIX(0.869244010)) + z3; /* c11+c13 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*14] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*13] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*12] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*11] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*10] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25 + tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp25 - tmp15, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp26 + tmp16, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp26 - tmp16, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp27, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 15 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 15; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z1 <<= CONST_BITS; + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[4]; + z4 = (INT32) wsptr[6]; + + tmp10 = MULTIPLY(z4, FIX(0.437016024)); /* c12 */ + tmp11 = MULTIPLY(z4, FIX(1.144122806)); /* c6 */ + + tmp12 = z1 - tmp10; + tmp13 = z1 + tmp11; + z1 -= (tmp11 - tmp10) << 1; /* c0 = (c6-c12)*2 */ + + z4 = z2 - z3; + z3 += z2; + tmp10 = MULTIPLY(z3, FIX(1.337628990)); /* (c2+c4)/2 */ + tmp11 = MULTIPLY(z4, FIX(0.045680613)); /* (c2-c4)/2 */ + z2 = MULTIPLY(z2, FIX(1.439773946)); /* c4+c14 */ + + tmp20 = tmp13 + tmp10 + tmp11; + tmp23 = tmp12 - tmp10 + tmp11 + z2; + + tmp10 = MULTIPLY(z3, FIX(0.547059574)); /* (c8+c14)/2 */ + tmp11 = MULTIPLY(z4, FIX(0.399234004)); /* (c8-c14)/2 */ + + tmp25 = tmp13 - tmp10 - tmp11; + tmp26 = tmp12 + tmp10 - tmp11 - z2; + + tmp10 = MULTIPLY(z3, FIX(0.790569415)); /* (c6+c12)/2 */ + tmp11 = MULTIPLY(z4, FIX(0.353553391)); /* (c6-c12)/2 */ + + tmp21 = tmp12 + tmp10 + tmp11; + tmp24 = tmp13 - tmp10 + tmp11; + tmp11 += tmp11; + tmp22 = z1 + tmp11; /* c10 = c6-c12 */ + tmp27 = z1 - tmp11 - tmp11; /* c0 = (c6-c12)*2 */ + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z4 = (INT32) wsptr[5]; + z3 = MULTIPLY(z4, FIX(1.224744871)); /* c5 */ + z4 = (INT32) wsptr[7]; + + tmp13 = z2 - z4; + tmp15 = MULTIPLY(z1 + tmp13, FIX(0.831253876)); /* c9 */ + tmp11 = tmp15 + MULTIPLY(z1, FIX(0.513743148)); /* c3-c9 */ + tmp14 = tmp15 - MULTIPLY(tmp13, FIX(2.176250899)); /* c3+c9 */ + + tmp13 = MULTIPLY(z2, - FIX(0.831253876)); /* -c9 */ + tmp15 = MULTIPLY(z2, - FIX(1.344997024)); /* -c3 */ + z2 = z1 - z4; + tmp12 = z3 + MULTIPLY(z2, FIX(1.406466353)); /* c1 */ + + tmp10 = tmp12 + MULTIPLY(z4, FIX(2.457431844)) - tmp15; /* c1+c7 */ + tmp16 = tmp12 - MULTIPLY(z1, FIX(1.112434820)) + tmp13; /* c1-c13 */ + tmp12 = MULTIPLY(z2, FIX(1.224744871)) - z3; /* c5 */ + z2 = MULTIPLY(z1 + z4, FIX(0.575212477)); /* c11 */ + tmp13 += z2 + MULTIPLY(z1, FIX(0.475753014)) - z3; /* c7-c11 */ + tmp15 += z2 - MULTIPLY(z4, FIX(0.869244010)) + z3; /* c11+c13 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp27, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 16x16 output block. + * + * Optimized algorithm with 28 multiplications in the 1-D kernel. + * cK represents sqrt(2) * cos(K*pi/32). + */ + +GLOBAL(void) +jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*16]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += 1 << (CONST_BITS-PASS1_BITS-1); + + z1 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ + tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */ + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + tmp12 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + z3 = z1 - z2; + z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */ + z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */ + + tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447); /* (c6+c2)[16] = (c3+c1)[8] */ + tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223); /* (c6-c14)[16] = (c3-c7)[8] */ + tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887)); /* (c2-c10)[16] = (c1-c5)[8] */ + tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579)); /* (c10-c14)[16] = (c5-c7)[8] */ + + tmp20 = tmp10 + tmp0; + tmp27 = tmp10 - tmp0; + tmp21 = tmp12 + tmp1; + tmp26 = tmp12 - tmp1; + tmp22 = tmp13 + tmp2; + tmp25 = tmp13 - tmp2; + tmp23 = tmp11 + tmp3; + tmp24 = tmp11 - tmp3; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = z1 + z3; + + tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */ + tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */ + tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */ + tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */ + tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */ + tmp0 = tmp1 + tmp2 + tmp3 - + MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */ + tmp13 = tmp10 + tmp11 + tmp12 - + MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */ + z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */ + tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */ + tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */ + z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */ + tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */ + tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */ + z2 += z4; + z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */ + tmp1 += z1; + tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */ + z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */ + tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */ + tmp12 += z2; + z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */ + tmp2 += z2; + tmp3 += z2; + z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */ + tmp10 += z2; + tmp11 += z2; + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[8*15] = (int) RIGHT_SHIFT(tmp20 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[8*14] = (int) RIGHT_SHIFT(tmp21 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[8*13] = (int) RIGHT_SHIFT(tmp22 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp3, CONST_BITS-PASS1_BITS); + wsptr[8*12] = (int) RIGHT_SHIFT(tmp23 - tmp3, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*11] = (int) RIGHT_SHIFT(tmp24 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*10] = (int) RIGHT_SHIFT(tmp25 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp26 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp26 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp27 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp27 - tmp13, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 16 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 16; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + + z1 = (INT32) wsptr[4]; + tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ + tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */ + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + tmp12 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[6]; + z3 = z1 - z2; + z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */ + z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */ + + tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447); /* (c6+c2)[16] = (c3+c1)[8] */ + tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223); /* (c6-c14)[16] = (c3-c7)[8] */ + tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887)); /* (c2-c10)[16] = (c1-c5)[8] */ + tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579)); /* (c10-c14)[16] = (c5-c7)[8] */ + + tmp20 = tmp10 + tmp0; + tmp27 = tmp10 - tmp0; + tmp21 = tmp12 + tmp1; + tmp26 = tmp12 - tmp1; + tmp22 = tmp13 + tmp2; + tmp25 = tmp13 - tmp2; + tmp23 = tmp11 + tmp3; + tmp24 = tmp11 - tmp3; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + tmp11 = z1 + z3; + + tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */ + tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */ + tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */ + tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */ + tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */ + tmp0 = tmp1 + tmp2 + tmp3 - + MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */ + tmp13 = tmp10 + tmp11 + tmp12 - + MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */ + z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */ + tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */ + tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */ + z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */ + tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */ + tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */ + z2 += z4; + z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */ + tmp1 += z1; + tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */ + z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */ + tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */ + tmp12 += z2; + z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */ + tmp2 += z2; + tmp3 += z2; + z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */ + tmp10 += z2; + tmp11 += z2; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[15] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp27 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp27 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 16x8 output block. + * + * 8-point IDCT in pass 1 (columns), 16-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*8]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = DCTSIZE; ctr > 0; ctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any column in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * column DCT calculations can be simplified this way. + */ + + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && + inptr[DCTSIZE*7] == 0) { + /* AC terms all zero */ + int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; + + wsptr[DCTSIZE*0] = dcval; + wsptr[DCTSIZE*1] = dcval; + wsptr[DCTSIZE*2] = dcval; + wsptr[DCTSIZE*3] = dcval; + wsptr[DCTSIZE*4] = dcval; + wsptr[DCTSIZE*5] = dcval; + wsptr[DCTSIZE*6] = dcval; + wsptr[DCTSIZE*7] = dcval; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + continue; + } + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865); + tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); + + z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z2 <<= CONST_BITS; + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z2 += ONE << (CONST_BITS-PASS1_BITS-1); + + tmp0 = z2 + z3; + tmp1 = z2 - z3; + + tmp10 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + tmp11 = tmp1 + tmp3; + tmp12 = tmp1 - tmp3; + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + + z2 = tmp0 + tmp2; + z3 = tmp1 + tmp3; + + z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */ + z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + z2 += z1; + z3 += z1; + + z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + tmp0 += z1 + z2; + tmp3 += z1 + z3; + + z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp1 += z1 + z3; + tmp2 += z1 + z2; + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + wsptr[DCTSIZE*0] = (int) RIGHT_SHIFT(tmp10 + tmp3, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*7] = (int) RIGHT_SHIFT(tmp10 - tmp3, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*1] = (int) RIGHT_SHIFT(tmp11 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*6] = (int) RIGHT_SHIFT(tmp11 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*2] = (int) RIGHT_SHIFT(tmp12 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*5] = (int) RIGHT_SHIFT(tmp12 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*3] = (int) RIGHT_SHIFT(tmp13 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[DCTSIZE*4] = (int) RIGHT_SHIFT(tmp13 - tmp0, CONST_BITS-PASS1_BITS); + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + } + + /* Pass 2: process 8 rows from work array, store into output array. + * 16-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/32). + */ + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + + z1 = (INT32) wsptr[4]; + tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ + tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */ + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + tmp12 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[6]; + z3 = z1 - z2; + z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */ + z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */ + + tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447); /* (c6+c2)[16] = (c3+c1)[8] */ + tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223); /* (c6-c14)[16] = (c3-c7)[8] */ + tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887)); /* (c2-c10)[16] = (c1-c5)[8] */ + tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579)); /* (c10-c14)[16] = (c5-c7)[8] */ + + tmp20 = tmp10 + tmp0; + tmp27 = tmp10 - tmp0; + tmp21 = tmp12 + tmp1; + tmp26 = tmp12 - tmp1; + tmp22 = tmp13 + tmp2; + tmp25 = tmp13 - tmp2; + tmp23 = tmp11 + tmp3; + tmp24 = tmp11 - tmp3; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + tmp11 = z1 + z3; + + tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */ + tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */ + tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */ + tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */ + tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */ + tmp0 = tmp1 + tmp2 + tmp3 - + MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */ + tmp13 = tmp10 + tmp11 + tmp12 - + MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */ + z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */ + tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */ + tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */ + z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */ + tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */ + tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */ + z2 += z4; + z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */ + tmp1 += z1; + tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */ + z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */ + tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */ + tmp12 += z2; + z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */ + tmp2 += z2; + tmp3 += z2; + z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */ + tmp10 += z2; + tmp11 += z2; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[15] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp27 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp27 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 14x7 output block. + * + * 7-point IDCT in pass 1 (columns), 14-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*7]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 7-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/14). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp23 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp23 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp23 += ONE << (CONST_BITS-PASS1_BITS-1); + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + tmp20 = MULTIPLY(z2 - z3, FIX(0.881747734)); /* c4 */ + tmp22 = MULTIPLY(z1 - z2, FIX(0.314692123)); /* c6 */ + tmp21 = tmp20 + tmp22 + tmp23 - MULTIPLY(z2, FIX(1.841218003)); /* c2+c4-c6 */ + tmp10 = z1 + z3; + z2 -= tmp10; + tmp10 = MULTIPLY(tmp10, FIX(1.274162392)) + tmp23; /* c2 */ + tmp20 += tmp10 - MULTIPLY(z3, FIX(0.077722536)); /* c2-c4-c6 */ + tmp22 += tmp10 - MULTIPLY(z1, FIX(2.470602249)); /* c2+c4+c6 */ + tmp23 += MULTIPLY(z2, FIX(1.414213562)); /* c0 */ + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + + tmp11 = MULTIPLY(z1 + z2, FIX(0.935414347)); /* (c3+c1-c5)/2 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.170262339)); /* (c3+c5-c1)/2 */ + tmp10 = tmp11 - tmp12; + tmp11 += tmp12; + tmp12 = MULTIPLY(z2 + z3, - FIX(1.378756276)); /* -c1 */ + tmp11 += tmp12; + z2 = MULTIPLY(z1 + z3, FIX(0.613604268)); /* c5 */ + tmp10 += z2; + tmp12 += z2 + MULTIPLY(z3, FIX(1.870828693)); /* c3+c1-c5 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 7 rows from work array, store into output array. + * 14-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/28). + */ + wsptr = workspace; + for (ctr = 0; ctr < 7; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z1 <<= CONST_BITS; + z4 = (INT32) wsptr[4]; + z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */ + z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */ + z4 = MULTIPLY(z4, FIX(0.881747734)); /* c8 */ + + tmp10 = z1 + z2; + tmp11 = z1 + z3; + tmp12 = z1 - z4; + + tmp23 = z1 - ((z2 + z3 - z4) << 1); /* c0 = (c4+c12-c8)*2 */ + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[6]; + + z3 = MULTIPLY(z1 + z2, FIX(1.105676686)); /* c6 */ + + tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */ + tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */ + tmp15 = MULTIPLY(z1, FIX(0.613604268)) - /* c10 */ + MULTIPLY(z2, FIX(1.378756276)); /* c2 */ + + tmp20 = tmp10 + tmp13; + tmp26 = tmp10 - tmp13; + tmp21 = tmp11 + tmp14; + tmp25 = tmp11 - tmp14; + tmp22 = tmp12 + tmp15; + tmp24 = tmp12 - tmp15; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + z4 <<= CONST_BITS; + + tmp14 = z1 + z3; + tmp11 = MULTIPLY(z1 + z2, FIX(1.334852607)); /* c3 */ + tmp12 = MULTIPLY(tmp14, FIX(1.197448846)); /* c5 */ + tmp10 = tmp11 + tmp12 + z4 - MULTIPLY(z1, FIX(1.126980169)); /* c3+c5-c1 */ + tmp14 = MULTIPLY(tmp14, FIX(0.752406978)); /* c9 */ + tmp16 = tmp14 - MULTIPLY(z1, FIX(1.061150426)); /* c9+c11-c13 */ + z1 -= z2; + tmp15 = MULTIPLY(z1, FIX(0.467085129)) - z4; /* c11 */ + tmp16 += tmp15; + tmp13 = MULTIPLY(z2 + z3, - FIX(0.158341681)) - z4; /* -c13 */ + tmp11 += tmp13 - MULTIPLY(z2, FIX(0.424103948)); /* c3-c9-c13 */ + tmp12 += tmp13 - MULTIPLY(z3, FIX(2.373959773)); /* c3+c5-c13 */ + tmp13 = MULTIPLY(z3 - z2, FIX(1.405321284)); /* c1 */ + tmp14 += tmp13 + z4 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */ + tmp15 += tmp13 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */ + + tmp13 = ((z1 - z3) << CONST_BITS) + z4; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 12x6 output block. + * + * 6-point IDCT in pass 1 (columns), 12-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*6]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp10 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp10 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp10 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp12 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp20 = MULTIPLY(tmp12, FIX(0.707106781)); /* c4 */ + tmp11 = tmp10 + tmp20; + tmp21 = RIGHT_SHIFT(tmp10 - tmp20 - tmp20, CONST_BITS-PASS1_BITS); + tmp20 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp10 = MULTIPLY(tmp20, FIX(1.224744871)); /* c2 */ + tmp20 = tmp11 + tmp10; + tmp22 = tmp11 - tmp10; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp11 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ + tmp10 = tmp11 + ((z1 + z2) << CONST_BITS); + tmp12 = tmp11 + ((z3 - z2) << CONST_BITS); + tmp11 = (z1 - z2 - z3) << PASS1_BITS; + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) (tmp21 + tmp11); + wsptr[8*4] = (int) (tmp21 - tmp11); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 6 rows from work array, store into output array. + * 12-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/24). + */ + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 <<= CONST_BITS; + + z4 = (INT32) wsptr[4]; + z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */ + + tmp10 = z3 + z4; + tmp11 = z3 - z4; + + z1 = (INT32) wsptr[2]; + z4 = MULTIPLY(z1, FIX(1.366025404)); /* c2 */ + z1 <<= CONST_BITS; + z2 = (INT32) wsptr[6]; + z2 <<= CONST_BITS; + + tmp12 = z1 - z2; + + tmp21 = z3 + tmp12; + tmp24 = z3 - tmp12; + + tmp12 = z4 + z2; + + tmp20 = tmp10 + tmp12; + tmp25 = tmp10 - tmp12; + + tmp12 = z4 - z1 - z2; + + tmp22 = tmp11 + tmp12; + tmp23 = tmp11 - tmp12; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z4 = (INT32) wsptr[7]; + + tmp11 = MULTIPLY(z2, FIX(1.306562965)); /* c3 */ + tmp14 = MULTIPLY(z2, - FIX_0_541196100); /* -c9 */ + + tmp10 = z1 + z3; + tmp15 = MULTIPLY(tmp10 + z4, FIX(0.860918669)); /* c7 */ + tmp12 = tmp15 + MULTIPLY(tmp10, FIX(0.261052384)); /* c5-c7 */ + tmp10 = tmp12 + tmp11 + MULTIPLY(z1, FIX(0.280143716)); /* c1-c5 */ + tmp13 = MULTIPLY(z3 + z4, - FIX(1.045510580)); /* -(c7+c11) */ + tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */ + tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */ + tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) - /* c7-c11 */ + MULTIPLY(z4, FIX(1.982889723)); /* c5+c7 */ + + z1 -= z4; + z2 -= z3; + z3 = MULTIPLY(z1 + z2, FIX_0_541196100); /* c9 */ + tmp11 = z3 + MULTIPLY(z1, FIX_0_765366865); /* c3-c9 */ + tmp14 = z3 - MULTIPLY(z2, FIX_1_847759065); /* c3+c9 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 10x5 output block. + * + * 5-point IDCT in pass 1 (columns), 10-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*5]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 5-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/10). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp12 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp12 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp12 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp13 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp14 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z1 = MULTIPLY(tmp13 + tmp14, FIX(0.790569415)); /* (c2+c4)/2 */ + z2 = MULTIPLY(tmp13 - tmp14, FIX(0.353553391)); /* (c2-c4)/2 */ + z3 = tmp12 + z2; + tmp10 = z3 + z1; + tmp11 = z3 - z1; + tmp12 -= z2 << 2; + + /* Odd part */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c3 */ + tmp13 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c1-c3 */ + tmp14 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c1+c3 */ + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp10 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp10 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp11 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp11 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp12, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 5 rows from work array, store into output array. + * 10-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/20). + */ + wsptr = workspace; + for (ctr = 0; ctr < 5; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 <<= CONST_BITS; + z4 = (INT32) wsptr[4]; + z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */ + z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */ + tmp10 = z3 + z1; + tmp11 = z3 - z2; + + tmp22 = z3 - ((z1 - z2) << 1); /* c0 = (c4-c8)*2 */ + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[6]; + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c6 */ + tmp12 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c2-c6 */ + tmp13 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c2+c6 */ + + tmp20 = tmp10 + tmp12; + tmp24 = tmp10 - tmp12; + tmp21 = tmp11 + tmp13; + tmp23 = tmp11 - tmp13; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + z3 <<= CONST_BITS; + z4 = (INT32) wsptr[7]; + + tmp11 = z2 + z4; + tmp13 = z2 - z4; + + tmp12 = MULTIPLY(tmp13, FIX(0.309016994)); /* (c3-c7)/2 */ + + z2 = MULTIPLY(tmp11, FIX(0.951056516)); /* (c3+c7)/2 */ + z4 = z3 + tmp12; + + tmp10 = MULTIPLY(z1, FIX(1.396802247)) + z2 + z4; /* c1 */ + tmp14 = MULTIPLY(z1, FIX(0.221231742)) - z2 + z4; /* c9 */ + + z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */ + z4 = z3 - tmp12 - (tmp13 << (CONST_BITS - 1)); + + tmp12 = ((z1 - tmp13) << CONST_BITS) - z3; + + tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */ + tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 8; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 8x4 output block. + * + * 4-point IDCT in pass 1 (columns), 8-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*4]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 4-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + + tmp10 = (tmp0 + tmp2) << PASS1_BITS; + tmp12 = (tmp0 - tmp2) << PASS1_BITS; + + /* Odd part */ + /* Same rotation as in the even part of the 8x8 LL&M IDCT */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp0 = RIGHT_SHIFT(z1 + MULTIPLY(z2, FIX_0_765366865), /* c2-c6 */ + CONST_BITS-PASS1_BITS); + tmp2 = RIGHT_SHIFT(z1 - MULTIPLY(z3, FIX_1_847759065), /* c2+c6 */ + CONST_BITS-PASS1_BITS); + + /* Final output stage */ + + wsptr[8*0] = (int) (tmp10 + tmp0); + wsptr[8*3] = (int) (tmp10 - tmp0); + wsptr[8*1] = (int) (tmp12 + tmp2); + wsptr[8*2] = (int) (tmp12 - tmp2); + } + + /* Pass 2: process rows from work array, store into output array. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + wsptr = workspace; + for (ctr = 0; ctr < 4; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[6]; + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865); + tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); + + /* Add fudge factor here for final descale. */ + z2 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 = (INT32) wsptr[4]; + + tmp0 = (z2 + z3) << CONST_BITS; + tmp1 = (z2 - z3) << CONST_BITS; + + tmp10 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + tmp11 = tmp1 + tmp3; + tmp12 = tmp1 - tmp3; + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + tmp0 = (INT32) wsptr[7]; + tmp1 = (INT32) wsptr[5]; + tmp2 = (INT32) wsptr[3]; + tmp3 = (INT32) wsptr[1]; + + z2 = tmp0 + tmp2; + z3 = tmp1 + tmp3; + + z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */ + z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + z2 += z1; + z3 += z1; + + z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + tmp0 += z1 + z2; + tmp3 += z1 + z3; + + z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp1 += z1 + z3; + tmp2 += z1 + z2; + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += DCTSIZE; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 6x3 output block. + * + * 3-point IDCT in pass 1 (columns), 6-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[6*3]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 3-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/6). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */ + tmp10 = tmp0 + tmp12; + tmp2 = tmp0 - tmp12 - tmp12; + + /* Odd part */ + + tmp12 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + tmp0 = MULTIPLY(tmp12, FIX(1.224744871)); /* c1 */ + + /* Final output stage */ + + wsptr[6*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[6*2] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[6*1] = (int) RIGHT_SHIFT(tmp2, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 3 rows from work array, store into output array. + * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12). + */ + wsptr = workspace; + for (ctr = 0; ctr < 3; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + tmp2 = (INT32) wsptr[4]; + tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */ + tmp1 = tmp0 + tmp10; + tmp11 = tmp0 - tmp10 - tmp10; + tmp10 = (INT32) wsptr[2]; + tmp0 = MULTIPLY(tmp10, FIX(1.224744871)); /* c2 */ + tmp10 = tmp1 + tmp0; + tmp12 = tmp1 - tmp0; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ + tmp0 = tmp1 + ((z1 + z2) << CONST_BITS); + tmp2 = tmp1 + ((z3 - z2) << CONST_BITS); + tmp1 = (z1 - z2 - z3) << CONST_BITS; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 6; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 4x2 output block. + * + * 2-point IDCT in pass 1 (columns), 4-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_4x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp2, tmp10, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + INT32 * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + INT32 workspace[4*2]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 4; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp10 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + + /* Odd part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + + /* Final output stage */ + + wsptr[4*0] = tmp10 + tmp0; + wsptr[4*1] = tmp10 - tmp0; + } + + /* Pass 2: process 2 rows from work array, store into output array. + * 4-point IDCT kernel, + * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT]. + */ + wsptr = workspace; + for (ctr = 0; ctr < 2; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = wsptr[0] + (ONE << 2); + tmp2 = wsptr[2]; + + tmp10 = (tmp0 + tmp2) << CONST_BITS; + tmp12 = (tmp0 - tmp2) << CONST_BITS; + + /* Odd part */ + /* Same rotation as in the even part of the 8x8 LL&M IDCT */ + + z2 = wsptr[1]; + z3 = wsptr[3]; + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ + tmp0 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */ + tmp2 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+3) + & RANGE_MASK]; + + wsptr += 4; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 2x1 output block. + * + * 1-point IDCT in pass 1 (columns), 2-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp10; + ISLOW_MULT_TYPE * quantptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + SHIFT_TEMPS + + /* Pass 1: empty. */ + + /* Pass 2: process 1 row from input, store into output array. */ + + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + outptr = output_buf[0] + output_col; + + /* Even part */ + + tmp10 = DEQUANTIZE(coef_block[0], quantptr[0]); + /* Add fudge factor here for final descale. */ + tmp10 += ONE << 2; + + /* Odd part */ + + tmp0 = DEQUANTIZE(coef_block[1], quantptr[1]); + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, 3) & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, 3) & RANGE_MASK]; +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 8x16 output block. + * + * 16-point IDCT in pass 1 (columns), 8-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[8*16]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 16-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/32). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-1); + + z1 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ + tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */ + + tmp10 = tmp0 + tmp1; + tmp11 = tmp0 - tmp1; + tmp12 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + z3 = z1 - z2; + z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */ + z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */ + + tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447); /* (c6+c2)[16] = (c3+c1)[8] */ + tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223); /* (c6-c14)[16] = (c3-c7)[8] */ + tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887)); /* (c2-c10)[16] = (c1-c5)[8] */ + tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579)); /* (c10-c14)[16] = (c5-c7)[8] */ + + tmp20 = tmp10 + tmp0; + tmp27 = tmp10 - tmp0; + tmp21 = tmp12 + tmp1; + tmp26 = tmp12 - tmp1; + tmp22 = tmp13 + tmp2; + tmp25 = tmp13 - tmp2; + tmp23 = tmp11 + tmp3; + tmp24 = tmp11 - tmp3; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = z1 + z3; + + tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */ + tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */ + tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */ + tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */ + tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */ + tmp0 = tmp1 + tmp2 + tmp3 - + MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */ + tmp13 = tmp10 + tmp11 + tmp12 - + MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */ + z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */ + tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */ + tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */ + z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */ + tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */ + tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */ + z2 += z4; + z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */ + tmp1 += z1; + tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */ + z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */ + tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */ + tmp12 += z2; + z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */ + tmp2 += z2; + tmp3 += z2; + z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */ + tmp10 += z2; + tmp11 += z2; + + /* Final output stage */ + + wsptr[8*0] = (int) RIGHT_SHIFT(tmp20 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[8*15] = (int) RIGHT_SHIFT(tmp20 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[8*1] = (int) RIGHT_SHIFT(tmp21 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[8*14] = (int) RIGHT_SHIFT(tmp21 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[8*2] = (int) RIGHT_SHIFT(tmp22 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[8*13] = (int) RIGHT_SHIFT(tmp22 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[8*3] = (int) RIGHT_SHIFT(tmp23 + tmp3, CONST_BITS-PASS1_BITS); + wsptr[8*12] = (int) RIGHT_SHIFT(tmp23 - tmp3, CONST_BITS-PASS1_BITS); + wsptr[8*4] = (int) RIGHT_SHIFT(tmp24 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*11] = (int) RIGHT_SHIFT(tmp24 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[8*5] = (int) RIGHT_SHIFT(tmp25 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*10] = (int) RIGHT_SHIFT(tmp25 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[8*6] = (int) RIGHT_SHIFT(tmp26 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*9] = (int) RIGHT_SHIFT(tmp26 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[8*7] = (int) RIGHT_SHIFT(tmp27 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[8*8] = (int) RIGHT_SHIFT(tmp27 - tmp13, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process rows from work array, store into output array. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + wsptr = workspace; + for (ctr = 0; ctr < 16; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + + z2 = (INT32) wsptr[2]; + z3 = (INT32) wsptr[6]; + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865); + tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); + + /* Add fudge factor here for final descale. */ + z2 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + z3 = (INT32) wsptr[4]; + + tmp0 = (z2 + z3) << CONST_BITS; + tmp1 = (z2 - z3) << CONST_BITS; + + tmp10 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + tmp11 = tmp1 + tmp3; + tmp12 = tmp1 - tmp3; + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + tmp0 = (INT32) wsptr[7]; + tmp1 = (INT32) wsptr[5]; + tmp2 = (INT32) wsptr[3]; + tmp3 = (INT32) wsptr[1]; + + z2 = tmp0 + tmp2; + z3 = tmp1 + tmp3; + + z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */ + z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + z2 += z1; + z3 += z1; + + z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + tmp0 += z1 + z2; + tmp3 += z1 + z3; + + z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp1 += z1 + z3; + tmp2 += z1 + z2; + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += DCTSIZE; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 7x14 output block. + * + * 14-point IDCT in pass 1 (columns), 7-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[7*14]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 14-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/28). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 7; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z1 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */ + z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */ + z4 = MULTIPLY(z4, FIX(0.881747734)); /* c8 */ + + tmp10 = z1 + z2; + tmp11 = z1 + z3; + tmp12 = z1 - z4; + + tmp23 = RIGHT_SHIFT(z1 - ((z2 + z3 - z4) << 1), /* c0 = (c4+c12-c8)*2 */ + CONST_BITS-PASS1_BITS); + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z3 = MULTIPLY(z1 + z2, FIX(1.105676686)); /* c6 */ + + tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */ + tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */ + tmp15 = MULTIPLY(z1, FIX(0.613604268)) - /* c10 */ + MULTIPLY(z2, FIX(1.378756276)); /* c2 */ + + tmp20 = tmp10 + tmp13; + tmp26 = tmp10 - tmp13; + tmp21 = tmp11 + tmp14; + tmp25 = tmp11 - tmp14; + tmp22 = tmp12 + tmp15; + tmp24 = tmp12 - tmp15; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + tmp13 = z4 << CONST_BITS; + + tmp14 = z1 + z3; + tmp11 = MULTIPLY(z1 + z2, FIX(1.334852607)); /* c3 */ + tmp12 = MULTIPLY(tmp14, FIX(1.197448846)); /* c5 */ + tmp10 = tmp11 + tmp12 + tmp13 - MULTIPLY(z1, FIX(1.126980169)); /* c3+c5-c1 */ + tmp14 = MULTIPLY(tmp14, FIX(0.752406978)); /* c9 */ + tmp16 = tmp14 - MULTIPLY(z1, FIX(1.061150426)); /* c9+c11-c13 */ + z1 -= z2; + tmp15 = MULTIPLY(z1, FIX(0.467085129)) - tmp13; /* c11 */ + tmp16 += tmp15; + z1 += z4; + z4 = MULTIPLY(z2 + z3, - FIX(0.158341681)) - tmp13; /* -c13 */ + tmp11 += z4 - MULTIPLY(z2, FIX(0.424103948)); /* c3-c9-c13 */ + tmp12 += z4 - MULTIPLY(z3, FIX(2.373959773)); /* c3+c5-c13 */ + z4 = MULTIPLY(z3 - z2, FIX(1.405321284)); /* c1 */ + tmp14 += z4 + tmp13 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */ + tmp15 += z4 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */ + + tmp13 = (z1 - z3) << PASS1_BITS; + + /* Final output stage */ + + wsptr[7*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[7*13] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[7*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[7*12] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[7*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[7*11] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[7*3] = (int) (tmp23 + tmp13); + wsptr[7*10] = (int) (tmp23 - tmp13); + wsptr[7*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[7*9] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[7*5] = (int) RIGHT_SHIFT(tmp25 + tmp15, CONST_BITS-PASS1_BITS); + wsptr[7*8] = (int) RIGHT_SHIFT(tmp25 - tmp15, CONST_BITS-PASS1_BITS); + wsptr[7*6] = (int) RIGHT_SHIFT(tmp26 + tmp16, CONST_BITS-PASS1_BITS); + wsptr[7*7] = (int) RIGHT_SHIFT(tmp26 - tmp16, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 14 rows from work array, store into output array. + * 7-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/14). + */ + wsptr = workspace; + for (ctr = 0; ctr < 14; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp23 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp23 <<= CONST_BITS; + + z1 = (INT32) wsptr[2]; + z2 = (INT32) wsptr[4]; + z3 = (INT32) wsptr[6]; + + tmp20 = MULTIPLY(z2 - z3, FIX(0.881747734)); /* c4 */ + tmp22 = MULTIPLY(z1 - z2, FIX(0.314692123)); /* c6 */ + tmp21 = tmp20 + tmp22 + tmp23 - MULTIPLY(z2, FIX(1.841218003)); /* c2+c4-c6 */ + tmp10 = z1 + z3; + z2 -= tmp10; + tmp10 = MULTIPLY(tmp10, FIX(1.274162392)) + tmp23; /* c2 */ + tmp20 += tmp10 - MULTIPLY(z3, FIX(0.077722536)); /* c2-c4-c6 */ + tmp22 += tmp10 - MULTIPLY(z1, FIX(2.470602249)); /* c2+c4+c6 */ + tmp23 += MULTIPLY(z2, FIX(1.414213562)); /* c0 */ + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + + tmp11 = MULTIPLY(z1 + z2, FIX(0.935414347)); /* (c3+c1-c5)/2 */ + tmp12 = MULTIPLY(z1 - z2, FIX(0.170262339)); /* (c3+c5-c1)/2 */ + tmp10 = tmp11 - tmp12; + tmp11 += tmp12; + tmp12 = MULTIPLY(z2 + z3, - FIX(1.378756276)); /* -c1 */ + tmp11 += tmp12; + z2 = MULTIPLY(z1 + z3, FIX(0.613604268)); /* c5 */ + tmp10 += z2; + tmp12 += z2 + MULTIPLY(z3, FIX(1.870828693)); /* c3+c1-c5 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 7; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 6x12 output block. + * + * 12-point IDCT in pass 1 (columns), 6-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25; + INT32 z1, z2, z3, z4; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[6*12]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 12-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/24). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z3 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z3 += ONE << (CONST_BITS-PASS1_BITS-1); + + z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */ + + tmp10 = z3 + z4; + tmp11 = z3 - z4; + + z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z4 = MULTIPLY(z1, FIX(1.366025404)); /* c2 */ + z1 <<= CONST_BITS; + z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + z2 <<= CONST_BITS; + + tmp12 = z1 - z2; + + tmp21 = z3 + tmp12; + tmp24 = z3 - tmp12; + + tmp12 = z4 + z2; + + tmp20 = tmp10 + tmp12; + tmp25 = tmp10 - tmp12; + + tmp12 = z4 - z1 - z2; + + tmp22 = tmp11 + tmp12; + tmp23 = tmp11 - tmp12; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = MULTIPLY(z2, FIX(1.306562965)); /* c3 */ + tmp14 = MULTIPLY(z2, - FIX_0_541196100); /* -c9 */ + + tmp10 = z1 + z3; + tmp15 = MULTIPLY(tmp10 + z4, FIX(0.860918669)); /* c7 */ + tmp12 = tmp15 + MULTIPLY(tmp10, FIX(0.261052384)); /* c5-c7 */ + tmp10 = tmp12 + tmp11 + MULTIPLY(z1, FIX(0.280143716)); /* c1-c5 */ + tmp13 = MULTIPLY(z3 + z4, - FIX(1.045510580)); /* -(c7+c11) */ + tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */ + tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */ + tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) - /* c7-c11 */ + MULTIPLY(z4, FIX(1.982889723)); /* c5+c7 */ + + z1 -= z4; + z2 -= z3; + z3 = MULTIPLY(z1 + z2, FIX_0_541196100); /* c9 */ + tmp11 = z3 + MULTIPLY(z1, FIX_0_765366865); /* c3-c9 */ + tmp14 = z3 - MULTIPLY(z2, FIX_1_847759065); /* c3+c9 */ + + /* Final output stage */ + + wsptr[6*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[6*11] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[6*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[6*10] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[6*2] = (int) RIGHT_SHIFT(tmp22 + tmp12, CONST_BITS-PASS1_BITS); + wsptr[6*9] = (int) RIGHT_SHIFT(tmp22 - tmp12, CONST_BITS-PASS1_BITS); + wsptr[6*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[6*8] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[6*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[6*7] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + wsptr[6*5] = (int) RIGHT_SHIFT(tmp25 + tmp15, CONST_BITS-PASS1_BITS); + wsptr[6*6] = (int) RIGHT_SHIFT(tmp25 - tmp15, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 12 rows from work array, store into output array. + * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12). + */ + wsptr = workspace; + for (ctr = 0; ctr < 12; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp10 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp10 <<= CONST_BITS; + tmp12 = (INT32) wsptr[4]; + tmp20 = MULTIPLY(tmp12, FIX(0.707106781)); /* c4 */ + tmp11 = tmp10 + tmp20; + tmp21 = tmp10 - tmp20 - tmp20; + tmp20 = (INT32) wsptr[2]; + tmp10 = MULTIPLY(tmp20, FIX(1.224744871)); /* c2 */ + tmp20 = tmp11 + tmp10; + tmp22 = tmp11 - tmp10; + + /* Odd part */ + + z1 = (INT32) wsptr[1]; + z2 = (INT32) wsptr[3]; + z3 = (INT32) wsptr[5]; + tmp11 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ + tmp10 = tmp11 + ((z1 + z2) << CONST_BITS); + tmp12 = tmp11 + ((z3 - z2) << CONST_BITS); + tmp11 = (z1 - z2 - z3) << CONST_BITS; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 6; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 5x10 output block. + * + * 10-point IDCT in pass 1 (columns), 5-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp10, tmp11, tmp12, tmp13, tmp14; + INT32 tmp20, tmp21, tmp22, tmp23, tmp24; + INT32 z1, z2, z3, z4, z5; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[5*10]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 10-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/20). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 5; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + z3 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z3 += ONE << (CONST_BITS-PASS1_BITS-1); + z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */ + z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */ + tmp10 = z3 + z1; + tmp11 = z3 - z2; + + tmp22 = RIGHT_SHIFT(z3 - ((z1 - z2) << 1), /* c0 = (c4-c8)*2 */ + CONST_BITS-PASS1_BITS); + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c6 */ + tmp12 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c2-c6 */ + tmp13 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c2+c6 */ + + tmp20 = tmp10 + tmp12; + tmp24 = tmp10 - tmp12; + tmp21 = tmp11 + tmp13; + tmp23 = tmp11 - tmp13; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + + tmp11 = z2 + z4; + tmp13 = z2 - z4; + + tmp12 = MULTIPLY(tmp13, FIX(0.309016994)); /* (c3-c7)/2 */ + z5 = z3 << CONST_BITS; + + z2 = MULTIPLY(tmp11, FIX(0.951056516)); /* (c3+c7)/2 */ + z4 = z5 + tmp12; + + tmp10 = MULTIPLY(z1, FIX(1.396802247)) + z2 + z4; /* c1 */ + tmp14 = MULTIPLY(z1, FIX(0.221231742)) - z2 + z4; /* c9 */ + + z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */ + z4 = z5 - tmp12 - (tmp13 << (CONST_BITS - 1)); + + tmp12 = (z1 - tmp13 - z3) << PASS1_BITS; + + tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */ + tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */ + + /* Final output stage */ + + wsptr[5*0] = (int) RIGHT_SHIFT(tmp20 + tmp10, CONST_BITS-PASS1_BITS); + wsptr[5*9] = (int) RIGHT_SHIFT(tmp20 - tmp10, CONST_BITS-PASS1_BITS); + wsptr[5*1] = (int) RIGHT_SHIFT(tmp21 + tmp11, CONST_BITS-PASS1_BITS); + wsptr[5*8] = (int) RIGHT_SHIFT(tmp21 - tmp11, CONST_BITS-PASS1_BITS); + wsptr[5*2] = (int) (tmp22 + tmp12); + wsptr[5*7] = (int) (tmp22 - tmp12); + wsptr[5*3] = (int) RIGHT_SHIFT(tmp23 + tmp13, CONST_BITS-PASS1_BITS); + wsptr[5*6] = (int) RIGHT_SHIFT(tmp23 - tmp13, CONST_BITS-PASS1_BITS); + wsptr[5*4] = (int) RIGHT_SHIFT(tmp24 + tmp14, CONST_BITS-PASS1_BITS); + wsptr[5*5] = (int) RIGHT_SHIFT(tmp24 - tmp14, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 10 rows from work array, store into output array. + * 5-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/10). + */ + wsptr = workspace; + for (ctr = 0; ctr < 10; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp12 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp12 <<= CONST_BITS; + tmp13 = (INT32) wsptr[2]; + tmp14 = (INT32) wsptr[4]; + z1 = MULTIPLY(tmp13 + tmp14, FIX(0.790569415)); /* (c2+c4)/2 */ + z2 = MULTIPLY(tmp13 - tmp14, FIX(0.353553391)); /* (c2-c4)/2 */ + z3 = tmp12 + z2; + tmp10 = z3 + z1; + tmp11 = z3 - z1; + tmp12 -= z2 << 2; + + /* Odd part */ + + z2 = (INT32) wsptr[1]; + z3 = (INT32) wsptr[3]; + + z1 = MULTIPLY(z2 + z3, FIX(0.831253876)); /* c3 */ + tmp13 = z1 + MULTIPLY(z2, FIX(0.513743148)); /* c1-c3 */ + tmp14 = z1 - MULTIPLY(z3, FIX(2.176250899)); /* c1+c3 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp13, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp14, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 5; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 4x8 output block. + * + * 8-point IDCT in pass 1 (columns), 4-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp3; + INT32 tmp10, tmp11, tmp12, tmp13; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[4*8]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. */ + /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 4; ctr > 0; ctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any column in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * column DCT calculations can be simplified this way. + */ + + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && + inptr[DCTSIZE*7] == 0) { + /* AC terms all zero */ + int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; + + wsptr[4*0] = dcval; + wsptr[4*1] = dcval; + wsptr[4*2] = dcval; + wsptr[4*3] = dcval; + wsptr[4*4] = dcval; + wsptr[4*5] = dcval; + wsptr[4*6] = dcval; + wsptr[4*7] = dcval; + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + continue; + } + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865); + tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); + + z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + z2 <<= CONST_BITS; + z3 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + z2 += ONE << (CONST_BITS-PASS1_BITS-1); + + tmp0 = z2 + z3; + tmp1 = z2 - z3; + + tmp10 = tmp0 + tmp2; + tmp13 = tmp0 - tmp2; + tmp11 = tmp1 + tmp3; + tmp12 = tmp1 - tmp3; + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); + tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + + z2 = tmp0 + tmp2; + z3 = tmp1 + tmp3; + + z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */ + z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ + z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ + z2 += z1; + z3 += z1; + + z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ + tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ + tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ + tmp0 += z1 + z2; + tmp3 += z1 + z3; + + z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ + tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ + tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ + tmp1 += z1 + z3; + tmp2 += z1 + z2; + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + wsptr[4*0] = (int) RIGHT_SHIFT(tmp10 + tmp3, CONST_BITS-PASS1_BITS); + wsptr[4*7] = (int) RIGHT_SHIFT(tmp10 - tmp3, CONST_BITS-PASS1_BITS); + wsptr[4*1] = (int) RIGHT_SHIFT(tmp11 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[4*6] = (int) RIGHT_SHIFT(tmp11 - tmp2, CONST_BITS-PASS1_BITS); + wsptr[4*2] = (int) RIGHT_SHIFT(tmp12 + tmp1, CONST_BITS-PASS1_BITS); + wsptr[4*5] = (int) RIGHT_SHIFT(tmp12 - tmp1, CONST_BITS-PASS1_BITS); + wsptr[4*3] = (int) RIGHT_SHIFT(tmp13 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[4*4] = (int) RIGHT_SHIFT(tmp13 - tmp0, CONST_BITS-PASS1_BITS); + + inptr++; /* advance pointers to next column */ + quantptr++; + wsptr++; + } + + /* Pass 2: process 8 rows from work array, store into output array. + * 4-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16). + */ + wsptr = workspace; + for (ctr = 0; ctr < 8; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp2 = (INT32) wsptr[2]; + + tmp10 = (tmp0 + tmp2) << CONST_BITS; + tmp12 = (tmp0 - tmp2) << CONST_BITS; + + /* Odd part */ + /* Same rotation as in the even part of the 8x8 LL&M IDCT */ + + z2 = (INT32) wsptr[1]; + z3 = (INT32) wsptr[3]; + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ + tmp0 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */ + tmp2 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 4; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a reduced-size 3x6 output block. + * + * 6-point IDCT in pass 1 (columns), 3-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + int * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + int workspace[3*6]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12). + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 3; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp0 <<= CONST_BITS; + /* Add fudge factor here for final descale. */ + tmp0 += ONE << (CONST_BITS-PASS1_BITS-1); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); + tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */ + tmp1 = tmp0 + tmp10; + tmp11 = RIGHT_SHIFT(tmp0 - tmp10 - tmp10, CONST_BITS-PASS1_BITS); + tmp10 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + tmp0 = MULTIPLY(tmp10, FIX(1.224744871)); /* c2 */ + tmp10 = tmp1 + tmp0; + tmp12 = tmp1 - tmp0; + + /* Odd part */ + + z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); + tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ + tmp0 = tmp1 + ((z1 + z2) << CONST_BITS); + tmp2 = tmp1 + ((z3 - z2) << CONST_BITS); + tmp1 = (z1 - z2 - z3) << PASS1_BITS; + + /* Final output stage */ + + wsptr[3*0] = (int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS-PASS1_BITS); + wsptr[3*5] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS); + wsptr[3*1] = (int) (tmp11 + tmp1); + wsptr[3*4] = (int) (tmp11 - tmp1); + wsptr[3*2] = (int) RIGHT_SHIFT(tmp12 + tmp2, CONST_BITS-PASS1_BITS); + wsptr[3*3] = (int) RIGHT_SHIFT(tmp12 - tmp2, CONST_BITS-PASS1_BITS); + } + + /* Pass 2: process 6 rows from work array, store into output array. + * 3-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/6). + */ + wsptr = workspace; + for (ctr = 0; ctr < 6; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2)); + tmp0 <<= CONST_BITS; + tmp2 = (INT32) wsptr[2]; + tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */ + tmp10 = tmp0 + tmp12; + tmp2 = tmp0 - tmp12 - tmp12; + + /* Odd part */ + + tmp12 = (INT32) wsptr[1]; + tmp0 = MULTIPLY(tmp12, FIX(1.224744871)); /* c1 */ + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2, + CONST_BITS+PASS1_BITS+3) + & RANGE_MASK]; + + wsptr += 3; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 2x4 output block. + * + * 4-point IDCT in pass 1 (columns), 2-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp2, tmp10, tmp12; + INT32 z1, z2, z3; + JCOEFPTR inptr; + ISLOW_MULT_TYPE * quantptr; + INT32 * wsptr; + JSAMPROW outptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + int ctr; + INT32 workspace[2*4]; /* buffers data between passes */ + SHIFT_TEMPS + + /* Pass 1: process columns from input, store into work array. + * 4-point IDCT kernel, + * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT]. + */ + inptr = coef_block; + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + wsptr = workspace; + for (ctr = 0; ctr < 2; ctr++, inptr++, quantptr++, wsptr++) { + /* Even part */ + + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); + tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); + + tmp10 = (tmp0 + tmp2) << CONST_BITS; + tmp12 = (tmp0 - tmp2) << CONST_BITS; + + /* Odd part */ + /* Same rotation as in the even part of the 8x8 LL&M IDCT */ + + z2 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); + z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); + + z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ + tmp0 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */ + tmp2 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */ + + /* Final output stage */ + + wsptr[2*0] = tmp10 + tmp0; + wsptr[2*3] = tmp10 - tmp0; + wsptr[2*1] = tmp12 + tmp2; + wsptr[2*2] = tmp12 - tmp2; + } + + /* Pass 2: process 4 rows from work array, store into output array. */ + + wsptr = workspace; + for (ctr = 0; ctr < 4; ctr++) { + outptr = output_buf[ctr] + output_col; + + /* Even part */ + + /* Add fudge factor here for final descale. */ + tmp10 = wsptr[0] + (ONE << (CONST_BITS+2)); + + /* Odd part */ + + tmp0 = wsptr[1]; + + /* Final output stage */ + + outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS+3) + & RANGE_MASK]; + outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS+3) + & RANGE_MASK]; + + wsptr += 2; /* advance pointer to next row */ + } +} + + +/* + * Perform dequantization and inverse DCT on one block of coefficients, + * producing a 1x2 output block. + * + * 2-point IDCT in pass 1 (columns), 1-point in pass 2 (rows). + */ + +GLOBAL(void) +jpeg_idct_1x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col) +{ + INT32 tmp0, tmp10; + ISLOW_MULT_TYPE * quantptr; + JSAMPLE *range_limit = IDCT_range_limit(cinfo); + SHIFT_TEMPS + + /* Process 1 column from input, store into output array. */ + + quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; + + /* Even part */ + + tmp10 = DEQUANTIZE(coef_block[DCTSIZE*0], quantptr[DCTSIZE*0]); + /* Add fudge factor here for final descale. */ + tmp10 += ONE << 2; + + /* Odd part */ + + tmp0 = DEQUANTIZE(coef_block[DCTSIZE*1], quantptr[DCTSIZE*1]); + + /* Final output stage */ + + output_buf[0][output_col] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, 3) + & RANGE_MASK]; + output_buf[1][output_col] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, 3) + & RANGE_MASK]; +} + +#endif /* IDCT_SCALING_SUPPORTED */ +#endif /* DCT_ISLOW_SUPPORTED */ diff --git a/crypto777/jpeg/jinclude.h b/crypto777/jpeg/jinclude.h new file mode 100644 index 000000000..0a4f15146 --- /dev/null +++ b/crypto777/jpeg/jinclude.h @@ -0,0 +1,91 @@ +/* + * jinclude.h + * + * Copyright (C) 1991-1994, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file exists to provide a single place to fix any problems with + * including the wrong system include files. (Common problems are taken + * care of by the standard jconfig symbols, but on really weird systems + * you may have to edit this file.) + * + * NOTE: this file is NOT intended to be included by applications using the + * JPEG library. Most applications need only include jpeglib.h. + */ + + +/* Include auto-config file to find out which system include files we need. */ + +#include "jconfig.h" /* auto configuration options */ +#define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */ + +/* + * We need the NULL macro and size_t typedef. + * On an ANSI-conforming system it is sufficient to include . + * Otherwise, we get them from or ; we may have to + * pull in as well. + * Note that the core JPEG library does not require ; + * only the default error handler and data source/destination modules do. + * But we must pull it in because of the references to FILE in jpeglib.h. + * You can remove those references if you want to compile without . + */ + +#ifdef HAVE_STDDEF_H +#include +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif + +#ifdef NEED_SYS_TYPES_H +#include +#endif + +#include + +/* + * We need memory copying and zeroing functions, plus strncpy(). + * ANSI and System V implementations declare these in . + * BSD doesn't have the mem() functions, but it does have bcopy()/bzero(). + * Some systems may declare memset and memcpy in . + * + * NOTE: we assume the size parameters to these functions are of type size_t. + * Change the casts in these macros if not! + */ + +#ifdef NEED_BSD_STRINGS + +#include +#define MEMZERO(target,size) bzero((void *)(target), (size_t)(size)) +#define MEMCOPY(dest,src,size) bcopy((const void *)(src), (void *)(dest), (size_t)(size)) + +#else /* not BSD, assume ANSI/SysV string lib */ + +#include +#define MEMZERO(target,size) memset((void *)(target), 0, (size_t)(size)) +#define MEMCOPY(dest,src,size) memcpy((void *)(dest), (const void *)(src), (size_t)(size)) + +#endif + +/* + * In ANSI C, and indeed any rational implementation, size_t is also the + * type returned by sizeof(). However, it seems there are some irrational + * implementations out there, in which sizeof() returns an int even though + * size_t is defined as long or unsigned long. To ensure consistent results + * we always use this SIZEOF() macro in place of using sizeof() directly. + */ + +#define SIZEOF(object) ((size_t) sizeof(object)) + +/* + * The modules that use fread() and fwrite() always invoke them through + * these macros. On some systems you may need to twiddle the argument casts. + * CAUTION: argument order is different from underlying functions! + */ + +#define JFREAD(file,buf,sizeofbuf) \ + ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) +#define JFWRITE(file,buf,sizeofbuf) \ + ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) diff --git a/crypto777/jpeg/jmemansi.c b/crypto777/jpeg/jmemansi.c new file mode 100644 index 000000000..2d93e4962 --- /dev/null +++ b/crypto777/jpeg/jmemansi.c @@ -0,0 +1,167 @@ +/* + * jmemansi.c + * + * Copyright (C) 1992-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file provides a simple generic implementation of the system- + * dependent portion of the JPEG memory manager. This implementation + * assumes that you have the ANSI-standard library routine tmpfile(). + * Also, the problem of determining the amount of memory available + * is shoved onto the user. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jmemsys.h" /* import the system-dependent declarations */ + +#ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ +extern void * malloc JPP((size_t size)); +extern void free JPP((void *ptr)); +#endif + +#ifndef SEEK_SET /* pre-ANSI systems may not define this; */ +#define SEEK_SET 0 /* if not, assume 0 is correct */ +#endif + + +/* + * Memory allocation and freeing are controlled by the regular library + * routines malloc() and free(). + */ + +GLOBAL(void *) +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * "Large" objects are treated the same as "small" ones. + * NB: although we include FAR keywords in the routine declarations, + * this file won't actually work in 80x86 small/medium model; at least, + * you probably won't be able to process useful-size images in only 64KB. + */ + +GLOBAL(void FAR *) +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void FAR *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * This routine computes the total memory space available for allocation. + * It's impossible to do this in a portable way; our current solution is + * to make the user tell us (with a default value set at compile time). + * If you can actually get the available space, it's a good idea to subtract + * a slop factor of 5% or so. + */ + +#ifndef DEFAULT_MAX_MEM /* so can override from makefile */ +#define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */ +#endif + +GLOBAL(long) +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, + long max_bytes_needed, long already_allocated) +{ + return cinfo->mem->max_memory_to_use - already_allocated; +} + + +/* + * Backing store (temporary file) management. + * Backing store objects are only used when the value returned by + * jpeg_mem_available is less than the total space needed. You can dispense + * with these routines if you have plenty of virtual memory; see jmemnobs.c. + */ + + +METHODDEF(void) +read_backing_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + if (fseek(info->temp_file, file_offset, SEEK_SET)) + ERREXIT(cinfo, JERR_TFILE_SEEK); + if (JFREAD(info->temp_file, buffer_address, byte_count) + != (size_t) byte_count) + ERREXIT(cinfo, JERR_TFILE_READ); +} + + +METHODDEF(void) +write_backing_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + if (fseek(info->temp_file, file_offset, SEEK_SET)) + ERREXIT(cinfo, JERR_TFILE_SEEK); + if (JFWRITE(info->temp_file, buffer_address, byte_count) + != (size_t) byte_count) + ERREXIT(cinfo, JERR_TFILE_WRITE); +} + + +METHODDEF(void) +close_backing_store (j_common_ptr cinfo, backing_store_ptr info) +{ + fclose(info->temp_file); + /* Since this implementation uses tmpfile() to create the file, + * no explicit file deletion is needed. + */ +} + + +/* + * Initial opening of a backing-store object. + * + * This version uses tmpfile(), which constructs a suitable file name + * behind the scenes. We don't have to use info->temp_name[] at all; + * indeed, we can't even find out the actual name of the temp file. + */ + +GLOBAL(void) +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + if ((info->temp_file = tmpfile()) == NULL) + ERREXITS(cinfo, JERR_TFILE_CREATE, ""); + info->read_backing_store = read_backing_store; + info->write_backing_store = write_backing_store; + info->close_backing_store = close_backing_store; +} + + +/* + * These routines take care of any system-dependent initialization and + * cleanup required. + */ + +GLOBAL(long) +jpeg_mem_init (j_common_ptr cinfo) +{ + return DEFAULT_MAX_MEM; /* default for max_memory_to_use */ +} + +GLOBAL(void) +jpeg_mem_term (j_common_ptr cinfo) +{ + /* no work */ +} diff --git a/crypto777/jpeg/jmemdos.c b/crypto777/jpeg/jmemdos.c new file mode 100644 index 000000000..60b45c693 --- /dev/null +++ b/crypto777/jpeg/jmemdos.c @@ -0,0 +1,638 @@ +/* + * jmemdos.c + * + * Copyright (C) 1992-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file provides an MS-DOS-compatible implementation of the system- + * dependent portion of the JPEG memory manager. Temporary data can be + * stored in extended or expanded memory as well as in regular DOS files. + * + * If you use this file, you must be sure that NEED_FAR_POINTERS is defined + * if you compile in a small-data memory model; it should NOT be defined if + * you use a large-data memory model. This file is not recommended if you + * are using a flat-memory-space 386 environment such as DJGCC or Watcom C. + * Also, this code will NOT work if struct fields are aligned on greater than + * 2-byte boundaries. + * + * Based on code contributed by Ge' Weijers. + */ + +/* + * If you have both extended and expanded memory, you may want to change the + * order in which they are tried in jopen_backing_store. On a 286 machine + * expanded memory is usually faster, since extended memory access involves + * an expensive protected-mode-and-back switch. On 386 and better, extended + * memory is usually faster. As distributed, the code tries extended memory + * first (what? not everyone has a 386? :-). + * + * You can disable use of extended/expanded memory entirely by altering these + * definitions or overriding them from the Makefile (eg, -DEMS_SUPPORTED=0). + */ + +#ifndef XMS_SUPPORTED +#define XMS_SUPPORTED 1 +#endif +#ifndef EMS_SUPPORTED +#define EMS_SUPPORTED 1 +#endif + + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jmemsys.h" /* import the system-dependent declarations */ + +#ifndef HAVE_STDLIB_H /* should declare these */ +extern void * malloc JPP((size_t size)); +extern void free JPP((void *ptr)); +extern char * getenv JPP((const char * name)); +#endif + +#ifdef NEED_FAR_POINTERS + +#ifdef __TURBOC__ +/* These definitions work for Borland C (Turbo C) */ +#include /* need farmalloc(), farfree() */ +#define far_malloc(x) farmalloc(x) +#define far_free(x) farfree(x) +#else +/* These definitions work for Microsoft C and compatible compilers */ +#include /* need _fmalloc(), _ffree() */ +#define far_malloc(x) _fmalloc(x) +#define far_free(x) _ffree(x) +#endif + +#else /* not NEED_FAR_POINTERS */ + +#define far_malloc(x) malloc(x) +#define far_free(x) free(x) + +#endif /* NEED_FAR_POINTERS */ + +#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */ +#define READ_BINARY "r" +#else +#define READ_BINARY "rb" +#endif + +#ifndef USE_MSDOS_MEMMGR /* make sure user got configuration right */ + You forgot to define USE_MSDOS_MEMMGR in jconfig.h. /* deliberate syntax error */ +#endif + +#if MAX_ALLOC_CHUNK >= 65535L /* make sure jconfig.h got this right */ + MAX_ALLOC_CHUNK should be less than 64K. /* deliberate syntax error */ +#endif + + +/* + * Declarations for assembly-language support routines (see jmemdosa.asm). + * + * The functions are declared "far" as are all their pointer arguments; + * this ensures the assembly source code will work regardless of the + * compiler memory model. We assume "short" is 16 bits, "long" is 32. + */ + +typedef void far * XMSDRIVER; /* actually a pointer to code */ +typedef struct { /* registers for calling XMS driver */ + unsigned short ax, dx, bx; + void far * ds_si; + } XMScontext; +typedef struct { /* registers for calling EMS driver */ + unsigned short ax, dx, bx; + void far * ds_si; + } EMScontext; + +extern short far jdos_open JPP((short far * handle, char far * filename)); +extern short far jdos_close JPP((short handle)); +extern short far jdos_seek JPP((short handle, long offset)); +extern short far jdos_read JPP((short handle, void far * buffer, + unsigned short count)); +extern short far jdos_write JPP((short handle, void far * buffer, + unsigned short count)); +extern void far jxms_getdriver JPP((XMSDRIVER far *)); +extern void far jxms_calldriver JPP((XMSDRIVER, XMScontext far *)); +extern short far jems_available JPP((void)); +extern void far jems_calldriver JPP((EMScontext far *)); + + +/* + * Selection of a file name for a temporary file. + * This is highly system-dependent, and you may want to customize it. + */ + +static int next_file_num; /* to distinguish among several temp files */ + +LOCAL(void) +select_file_name (char * fname) +{ + const char * env; + char * ptr; + FILE * tfile; + + /* Keep generating file names till we find one that's not in use */ + for (;;) { + /* Get temp directory name from environment TMP or TEMP variable; + * if none, use "." + */ + if ((env = (const char *) getenv("TMP")) == NULL) + if ((env = (const char *) getenv("TEMP")) == NULL) + env = "."; + if (*env == '\0') /* null string means "." */ + env = "."; + ptr = fname; /* copy name to fname */ + while (*env != '\0') + *ptr++ = *env++; + if (ptr[-1] != '\\' && ptr[-1] != '/') + *ptr++ = '\\'; /* append backslash if not in env variable */ + /* Append a suitable file name */ + next_file_num++; /* advance counter */ + sprintf(ptr, "JPG%03d.TMP", next_file_num); + /* Probe to see if file name is already in use */ + if ((tfile = fopen(fname, READ_BINARY)) == NULL) + break; + fclose(tfile); /* oops, it's there; close tfile & try again */ + } +} + + +/* + * Near-memory allocation and freeing are controlled by the regular library + * routines malloc() and free(). + */ + +GLOBAL(void *) +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * "Large" objects are allocated in far memory, if possible + */ + +GLOBAL(void FAR *) +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void FAR *) far_malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) +{ + far_free(object); +} + + +/* + * This routine computes the total memory space available for allocation. + * It's impossible to do this in a portable way; our current solution is + * to make the user tell us (with a default value set at compile time). + * If you can actually get the available space, it's a good idea to subtract + * a slop factor of 5% or so. + */ + +#ifndef DEFAULT_MAX_MEM /* so can override from makefile */ +#define DEFAULT_MAX_MEM 300000L /* for total usage about 450K */ +#endif + +GLOBAL(long) +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, + long max_bytes_needed, long already_allocated) +{ + return cinfo->mem->max_memory_to_use - already_allocated; +} + + +/* + * Backing store (temporary file) management. + * Backing store objects are only used when the value returned by + * jpeg_mem_available is less than the total space needed. You can dispense + * with these routines if you have plenty of virtual memory; see jmemnobs.c. + */ + +/* + * For MS-DOS we support three types of backing storage: + * 1. Conventional DOS files. We access these by direct DOS calls rather + * than via the stdio package. This provides a bit better performance, + * but the real reason is that the buffers to be read or written are FAR. + * The stdio library for small-data memory models can't cope with that. + * 2. Extended memory, accessed per the XMS V2.0 specification. + * 3. Expanded memory, accessed per the LIM/EMS 4.0 specification. + * You'll need copies of those specs to make sense of the related code. + * The specs are available by Internet FTP from the SIMTEL archives + * (oak.oakland.edu and its various mirror sites). See files + * pub/msdos/microsoft/xms20.arc and pub/msdos/info/limems41.zip. + */ + + +/* + * Access methods for a DOS file. + */ + + +METHODDEF(void) +read_file_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + if (jdos_seek(info->handle.file_handle, file_offset)) + ERREXIT(cinfo, JERR_TFILE_SEEK); + /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */ + if (byte_count > 65535L) /* safety check */ + ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK); + if (jdos_read(info->handle.file_handle, buffer_address, + (unsigned short) byte_count)) + ERREXIT(cinfo, JERR_TFILE_READ); +} + + +METHODDEF(void) +write_file_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + if (jdos_seek(info->handle.file_handle, file_offset)) + ERREXIT(cinfo, JERR_TFILE_SEEK); + /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */ + if (byte_count > 65535L) /* safety check */ + ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK); + if (jdos_write(info->handle.file_handle, buffer_address, + (unsigned short) byte_count)) + ERREXIT(cinfo, JERR_TFILE_WRITE); +} + + +METHODDEF(void) +close_file_store (j_common_ptr cinfo, backing_store_ptr info) +{ + jdos_close(info->handle.file_handle); /* close the file */ + remove(info->temp_name); /* delete the file */ +/* If your system doesn't have remove(), try unlink() instead. + * remove() is the ANSI-standard name for this function, but + * unlink() was more common in pre-ANSI systems. + */ + TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name); +} + + +LOCAL(boolean) +open_file_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + short handle; + + select_file_name(info->temp_name); + if (jdos_open((short far *) & handle, (char far *) info->temp_name)) { + /* might as well exit since jpeg_open_backing_store will fail anyway */ + ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name); + return FALSE; + } + info->handle.file_handle = handle; + info->read_backing_store = read_file_store; + info->write_backing_store = write_file_store; + info->close_backing_store = close_file_store; + TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name); + return TRUE; /* succeeded */ +} + + +/* + * Access methods for extended memory. + */ + +#if XMS_SUPPORTED + +static XMSDRIVER xms_driver; /* saved address of XMS driver */ + +typedef union { /* either long offset or real-mode pointer */ + long offset; + void far * ptr; + } XMSPTR; + +typedef struct { /* XMS move specification structure */ + long length; + XMSH src_handle; + XMSPTR src; + XMSH dst_handle; + XMSPTR dst; + } XMSspec; + +#define ODD(X) (((X) & 1L) != 0) + + +METHODDEF(void) +read_xms_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + XMScontext ctx; + XMSspec spec; + char endbuffer[2]; + + /* The XMS driver can't cope with an odd length, so handle the last byte + * specially if byte_count is odd. We don't expect this to be common. + */ + + spec.length = byte_count & (~ 1L); + spec.src_handle = info->handle.xms_handle; + spec.src.offset = file_offset; + spec.dst_handle = 0; + spec.dst.ptr = buffer_address; + + ctx.ds_si = (void far *) & spec; + ctx.ax = 0x0b00; /* EMB move */ + jxms_calldriver(xms_driver, (XMScontext far *) & ctx); + if (ctx.ax != 1) + ERREXIT(cinfo, JERR_XMS_READ); + + if (ODD(byte_count)) { + read_xms_store(cinfo, info, (void FAR *) endbuffer, + file_offset + byte_count - 1L, 2L); + ((char FAR *) buffer_address)[byte_count - 1L] = endbuffer[0]; + } +} + + +METHODDEF(void) +write_xms_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + XMScontext ctx; + XMSspec spec; + char endbuffer[2]; + + /* The XMS driver can't cope with an odd length, so handle the last byte + * specially if byte_count is odd. We don't expect this to be common. + */ + + spec.length = byte_count & (~ 1L); + spec.src_handle = 0; + spec.src.ptr = buffer_address; + spec.dst_handle = info->handle.xms_handle; + spec.dst.offset = file_offset; + + ctx.ds_si = (void far *) & spec; + ctx.ax = 0x0b00; /* EMB move */ + jxms_calldriver(xms_driver, (XMScontext far *) & ctx); + if (ctx.ax != 1) + ERREXIT(cinfo, JERR_XMS_WRITE); + + if (ODD(byte_count)) { + read_xms_store(cinfo, info, (void FAR *) endbuffer, + file_offset + byte_count - 1L, 2L); + endbuffer[0] = ((char FAR *) buffer_address)[byte_count - 1L]; + write_xms_store(cinfo, info, (void FAR *) endbuffer, + file_offset + byte_count - 1L, 2L); + } +} + + +METHODDEF(void) +close_xms_store (j_common_ptr cinfo, backing_store_ptr info) +{ + XMScontext ctx; + + ctx.dx = info->handle.xms_handle; + ctx.ax = 0x0a00; + jxms_calldriver(xms_driver, (XMScontext far *) & ctx); + TRACEMS1(cinfo, 1, JTRC_XMS_CLOSE, info->handle.xms_handle); + /* we ignore any error return from the driver */ +} + + +LOCAL(boolean) +open_xms_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + XMScontext ctx; + + /* Get address of XMS driver */ + jxms_getdriver((XMSDRIVER far *) & xms_driver); + if (xms_driver == NULL) + return FALSE; /* no driver to be had */ + + /* Get version number, must be >= 2.00 */ + ctx.ax = 0x0000; + jxms_calldriver(xms_driver, (XMScontext far *) & ctx); + if (ctx.ax < (unsigned short) 0x0200) + return FALSE; + + /* Try to get space (expressed in kilobytes) */ + ctx.dx = (unsigned short) ((total_bytes_needed + 1023L) >> 10); + ctx.ax = 0x0900; + jxms_calldriver(xms_driver, (XMScontext far *) & ctx); + if (ctx.ax != 1) + return FALSE; + + /* Succeeded, save the handle and away we go */ + info->handle.xms_handle = ctx.dx; + info->read_backing_store = read_xms_store; + info->write_backing_store = write_xms_store; + info->close_backing_store = close_xms_store; + TRACEMS1(cinfo, 1, JTRC_XMS_OPEN, ctx.dx); + return TRUE; /* succeeded */ +} + +#endif /* XMS_SUPPORTED */ + + +/* + * Access methods for expanded memory. + */ + +#if EMS_SUPPORTED + +/* The EMS move specification structure requires word and long fields aligned + * at odd byte boundaries. Some compilers will align struct fields at even + * byte boundaries. While it's usually possible to force byte alignment, + * that causes an overall performance penalty and may pose problems in merging + * JPEG into a larger application. Instead we accept some rather dirty code + * here. Note this code would fail if the hardware did not allow odd-byte + * word & long accesses, but all 80x86 CPUs do. + */ + +typedef void far * EMSPTR; + +typedef union { /* EMS move specification structure */ + long length; /* It's easy to access first 4 bytes */ + char bytes[18]; /* Misaligned fields in here! */ + } EMSspec; + +/* Macros for accessing misaligned fields */ +#define FIELD_AT(spec,offset,type) (*((type *) &(spec.bytes[offset]))) +#define SRC_TYPE(spec) FIELD_AT(spec,4,char) +#define SRC_HANDLE(spec) FIELD_AT(spec,5,EMSH) +#define SRC_OFFSET(spec) FIELD_AT(spec,7,unsigned short) +#define SRC_PAGE(spec) FIELD_AT(spec,9,unsigned short) +#define SRC_PTR(spec) FIELD_AT(spec,7,EMSPTR) +#define DST_TYPE(spec) FIELD_AT(spec,11,char) +#define DST_HANDLE(spec) FIELD_AT(spec,12,EMSH) +#define DST_OFFSET(spec) FIELD_AT(spec,14,unsigned short) +#define DST_PAGE(spec) FIELD_AT(spec,16,unsigned short) +#define DST_PTR(spec) FIELD_AT(spec,14,EMSPTR) + +#define EMSPAGESIZE 16384L /* gospel, see the EMS specs */ + +#define HIBYTE(W) (((W) >> 8) & 0xFF) +#define LOBYTE(W) ((W) & 0xFF) + + +METHODDEF(void) +read_ems_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + EMScontext ctx; + EMSspec spec; + + spec.length = byte_count; + SRC_TYPE(spec) = 1; + SRC_HANDLE(spec) = info->handle.ems_handle; + SRC_PAGE(spec) = (unsigned short) (file_offset / EMSPAGESIZE); + SRC_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE); + DST_TYPE(spec) = 0; + DST_HANDLE(spec) = 0; + DST_PTR(spec) = buffer_address; + + ctx.ds_si = (void far *) & spec; + ctx.ax = 0x5700; /* move memory region */ + jems_calldriver((EMScontext far *) & ctx); + if (HIBYTE(ctx.ax) != 0) + ERREXIT(cinfo, JERR_EMS_READ); +} + + +METHODDEF(void) +write_ems_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + EMScontext ctx; + EMSspec spec; + + spec.length = byte_count; + SRC_TYPE(spec) = 0; + SRC_HANDLE(spec) = 0; + SRC_PTR(spec) = buffer_address; + DST_TYPE(spec) = 1; + DST_HANDLE(spec) = info->handle.ems_handle; + DST_PAGE(spec) = (unsigned short) (file_offset / EMSPAGESIZE); + DST_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE); + + ctx.ds_si = (void far *) & spec; + ctx.ax = 0x5700; /* move memory region */ + jems_calldriver((EMScontext far *) & ctx); + if (HIBYTE(ctx.ax) != 0) + ERREXIT(cinfo, JERR_EMS_WRITE); +} + + +METHODDEF(void) +close_ems_store (j_common_ptr cinfo, backing_store_ptr info) +{ + EMScontext ctx; + + ctx.ax = 0x4500; + ctx.dx = info->handle.ems_handle; + jems_calldriver((EMScontext far *) & ctx); + TRACEMS1(cinfo, 1, JTRC_EMS_CLOSE, info->handle.ems_handle); + /* we ignore any error return from the driver */ +} + + +LOCAL(boolean) +open_ems_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + EMScontext ctx; + + /* Is EMS driver there? */ + if (! jems_available()) + return FALSE; + + /* Get status, make sure EMS is OK */ + ctx.ax = 0x4000; + jems_calldriver((EMScontext far *) & ctx); + if (HIBYTE(ctx.ax) != 0) + return FALSE; + + /* Get version, must be >= 4.0 */ + ctx.ax = 0x4600; + jems_calldriver((EMScontext far *) & ctx); + if (HIBYTE(ctx.ax) != 0 || LOBYTE(ctx.ax) < 0x40) + return FALSE; + + /* Try to allocate requested space */ + ctx.ax = 0x4300; + ctx.bx = (unsigned short) ((total_bytes_needed + EMSPAGESIZE-1L) / EMSPAGESIZE); + jems_calldriver((EMScontext far *) & ctx); + if (HIBYTE(ctx.ax) != 0) + return FALSE; + + /* Succeeded, save the handle and away we go */ + info->handle.ems_handle = ctx.dx; + info->read_backing_store = read_ems_store; + info->write_backing_store = write_ems_store; + info->close_backing_store = close_ems_store; + TRACEMS1(cinfo, 1, JTRC_EMS_OPEN, ctx.dx); + return TRUE; /* succeeded */ +} + +#endif /* EMS_SUPPORTED */ + + +/* + * Initial opening of a backing-store object. + */ + +GLOBAL(void) +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + /* Try extended memory, then expanded memory, then regular file. */ +#if XMS_SUPPORTED + if (open_xms_store(cinfo, info, total_bytes_needed)) + return; +#endif +#if EMS_SUPPORTED + if (open_ems_store(cinfo, info, total_bytes_needed)) + return; +#endif + if (open_file_store(cinfo, info, total_bytes_needed)) + return; + ERREXITS(cinfo, JERR_TFILE_CREATE, ""); +} + + +/* + * These routines take care of any system-dependent initialization and + * cleanup required. + */ + +GLOBAL(long) +jpeg_mem_init (j_common_ptr cinfo) +{ + next_file_num = 0; /* initialize temp file name generator */ + return DEFAULT_MAX_MEM; /* default for max_memory_to_use */ +} + +GLOBAL(void) +jpeg_mem_term (j_common_ptr cinfo) +{ + /* Microsoft C, at least in v6.00A, will not successfully reclaim freed + * blocks of size > 32Kbytes unless we give it a kick in the rear, like so: + */ +#ifdef NEED_FHEAPMIN + _fheapmin(); +#endif +} diff --git a/crypto777/jpeg/jmemmac.c b/crypto777/jpeg/jmemmac.c new file mode 100644 index 000000000..106f9bea0 --- /dev/null +++ b/crypto777/jpeg/jmemmac.c @@ -0,0 +1,289 @@ +/* + * jmemmac.c + * + * Copyright (C) 1992-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * jmemmac.c provides an Apple Macintosh implementation of the system- + * dependent portion of the JPEG memory manager. + * + * If you use jmemmac.c, then you must define USE_MAC_MEMMGR in the + * JPEG_INTERNALS part of jconfig.h. + * + * jmemmac.c uses the Macintosh toolbox routines NewPtr and DisposePtr + * instead of malloc and free. It accurately determines the amount of + * memory available by using CompactMem. Notice that if left to its + * own devices, this code can chew up all available space in the + * application's zone, with the exception of the rather small "slop" + * factor computed in jpeg_mem_available(). The application can ensure + * that more space is left over by reducing max_memory_to_use. + * + * Large images are swapped to disk using temporary files and System 7.0+'s + * temporary folder functionality. + * + * Note that jmemmac.c depends on two features of MacOS that were first + * introduced in System 7: FindFolder and the FSSpec-based calls. + * If your application uses jmemmac.c and is run under System 6 or earlier, + * and the jpeg library decides it needs a temporary file, it will abort, + * printing error messages about requiring System 7. (If no temporary files + * are created, it will run fine.) + * + * If you want to use jmemmac.c in an application that might be used with + * System 6 or earlier, then you should remove dependencies on FindFolder + * and the FSSpec calls. You will need to replace FindFolder with some + * other mechanism for finding a place to put temporary files, and you + * should replace the FSSpec calls with their HFS equivalents: + * + * FSpDelete -> HDelete + * FSpGetFInfo -> HGetFInfo + * FSpCreate -> HCreate + * FSpOpenDF -> HOpen *** Note: not HOpenDF *** + * FSMakeFSSpec -> (fill in spec by hand.) + * + * (Use HOpen instead of HOpenDF. HOpen is just a glue-interface to PBHOpen, + * which is on all HFS macs. HOpenDF is a System 7 addition which avoids the + * ages-old problem of names starting with a period.) + * + * Contributed by Sam Bushell (jsam@iagu.on.net) and + * Dan Gildor (gyld@in-touch.com). + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jmemsys.h" /* import the system-dependent declarations */ + +#ifndef USE_MAC_MEMMGR /* make sure user got configuration right */ + You forgot to define USE_MAC_MEMMGR in jconfig.h. /* deliberate syntax error */ +#endif + +#include /* we use the MacOS memory manager */ +#include /* we use the MacOS File stuff */ +#include /* we use the MacOS HFS stuff */ +#include /* for smSystemScript */ +#include /* we use Gestalt to test for specific functionality */ + +#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */ +#define TEMP_FILE_NAME "JPG%03d.TMP" +#endif + +static int next_file_num; /* to distinguish among several temp files */ + + +/* + * Memory allocation and freeing are controlled by the MacOS library + * routines NewPtr() and DisposePtr(), which allocate fixed-address + * storage. Unfortunately, the IJG library isn't smart enough to cope + * with relocatable storage. + */ + +GLOBAL(void *) +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void *) NewPtr(sizeofobject); +} + +GLOBAL(void) +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) +{ + DisposePtr((Ptr) object); +} + + +/* + * "Large" objects are treated the same as "small" ones. + * NB: we include FAR keywords in the routine declarations simply for + * consistency with the rest of the IJG code; FAR should expand to empty + * on rational architectures like the Mac. + */ + +GLOBAL(void FAR *) +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void FAR *) NewPtr(sizeofobject); +} + +GLOBAL(void) +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) +{ + DisposePtr((Ptr) object); +} + + +/* + * This routine computes the total memory space available for allocation. + */ + +GLOBAL(long) +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, + long max_bytes_needed, long already_allocated) +{ + long limit = cinfo->mem->max_memory_to_use - already_allocated; + long slop, mem; + + /* Don't ask for more than what application has told us we may use */ + if (max_bytes_needed > limit && limit > 0) + max_bytes_needed = limit; + /* Find whether there's a big enough free block in the heap. + * CompactMem tries to create a contiguous block of the requested size, + * and then returns the size of the largest free block (which could be + * much more or much less than we asked for). + * We add some slop to ensure we don't use up all available memory. + */ + slop = max_bytes_needed / 16 + 32768L; + mem = CompactMem(max_bytes_needed + slop) - slop; + if (mem < 0) + mem = 0; /* sigh, couldn't even get the slop */ + /* Don't take more than the application says we can have */ + if (mem > limit && limit > 0) + mem = limit; + return mem; +} + + +/* + * Backing store (temporary file) management. + * Backing store objects are only used when the value returned by + * jpeg_mem_available is less than the total space needed. You can dispense + * with these routines if you have plenty of virtual memory; see jmemnobs.c. + */ + + +METHODDEF(void) +read_backing_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + long bytes = byte_count; + long retVal; + + if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr ) + ERREXIT(cinfo, JERR_TFILE_SEEK); + + retVal = FSRead ( info->temp_file, &bytes, + (unsigned char *) buffer_address ); + if ( retVal != noErr || bytes != byte_count ) + ERREXIT(cinfo, JERR_TFILE_READ); +} + + +METHODDEF(void) +write_backing_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + long bytes = byte_count; + long retVal; + + if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr ) + ERREXIT(cinfo, JERR_TFILE_SEEK); + + retVal = FSWrite ( info->temp_file, &bytes, + (unsigned char *) buffer_address ); + if ( retVal != noErr || bytes != byte_count ) + ERREXIT(cinfo, JERR_TFILE_WRITE); +} + + +METHODDEF(void) +close_backing_store (j_common_ptr cinfo, backing_store_ptr info) +{ + FSClose ( info->temp_file ); + FSpDelete ( &(info->tempSpec) ); +} + + +/* + * Initial opening of a backing-store object. + * + * This version uses FindFolder to find the Temporary Items folder, + * and puts the temporary file in there. + */ + +GLOBAL(void) +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + short tmpRef, vRefNum; + long dirID; + FInfo finderInfo; + FSSpec theSpec; + Str255 fName; + OSErr osErr; + long gestaltResponse = 0; + + /* Check that FSSpec calls are available. */ + osErr = Gestalt( gestaltFSAttr, &gestaltResponse ); + if ( ( osErr != noErr ) + || !( gestaltResponse & (1<temp_name, TEMP_FILE_NAME, next_file_num); + strcpy ( (Ptr)fName+1, info->temp_name ); + *fName = strlen (info->temp_name); + osErr = FSMakeFSSpec ( vRefNum, dirID, fName, &theSpec ); + + if ( (osErr = FSpGetFInfo ( &theSpec, &finderInfo ) ) != noErr ) + break; + } + + osErr = FSpCreate ( &theSpec, '????', '????', smSystemScript ); + if ( osErr != noErr ) + ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name); + + osErr = FSpOpenDF ( &theSpec, fsRdWrPerm, &(info->temp_file) ); + if ( osErr != noErr ) + ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name); + + info->tempSpec = theSpec; + + info->read_backing_store = read_backing_store; + info->write_backing_store = write_backing_store; + info->close_backing_store = close_backing_store; + TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name); +} + + +/* + * These routines take care of any system-dependent initialization and + * cleanup required. + */ + +GLOBAL(long) +jpeg_mem_init (j_common_ptr cinfo) +{ + next_file_num = 0; + + /* max_memory_to_use will be initialized to FreeMem()'s result; + * the calling application might later reduce it, for example + * to leave room to invoke multiple JPEG objects. + * Note that FreeMem returns the total number of free bytes; + * it may not be possible to allocate a single block of this size. + */ + return FreeMem(); +} + +GLOBAL(void) +jpeg_mem_term (j_common_ptr cinfo) +{ + /* no work */ +} diff --git a/crypto777/jpeg/jmemmgr.c b/crypto777/jpeg/jmemmgr.c new file mode 100644 index 000000000..f0e83fb95 --- /dev/null +++ b/crypto777/jpeg/jmemmgr.c @@ -0,0 +1,1119 @@ +/* + * jmemmgr.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains the JPEG system-independent memory management + * routines. This code is usable across a wide variety of machines; most + * of the system dependencies have been isolated in a separate file. + * The major functions provided here are: + * * pool-based allocation and freeing of memory; + * * policy decisions about how to divide available memory among the + * virtual arrays; + * * control logic for swapping virtual arrays between main memory and + * backing storage. + * The separate system-dependent file provides the actual backing-storage + * access code, and it contains the policy decision about how much total + * main memory to use. + * This file is system-dependent in the sense that some of its functions + * are unnecessary in some systems. For example, if there is enough virtual + * memory so that backing storage will never be used, much of the virtual + * array control logic could be removed. (Of course, if you have that much + * memory then you shouldn't care about a little bit of unused code...) + */ + +#define JPEG_INTERNALS +#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */ +#include "jinclude.h" +#include "jpeglib.h" +#include "jmemsys.h" /* import the system-dependent declarations */ + +#ifndef NO_GETENV +#ifndef HAVE_STDLIB_H /* should declare getenv() */ +extern char * getenv JPP((const char * name)); +#endif +#endif + + +/* + * Some important notes: + * The allocation routines provided here must never return NULL. + * They should exit to error_exit if unsuccessful. + * + * It's not a good idea to try to merge the sarray and barray routines, + * even though they are textually almost the same, because samples are + * usually stored as bytes while coefficients are shorts or ints. Thus, + * in machines where byte pointers have a different representation from + * word pointers, the resulting machine code could not be the same. + */ + + +/* + * Many machines require storage alignment: longs must start on 4-byte + * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc() + * always returns pointers that are multiples of the worst-case alignment + * requirement, and we had better do so too. + * There isn't any really portable way to determine the worst-case alignment + * requirement. This module assumes that the alignment requirement is + * multiples of sizeof(ALIGN_TYPE). + * By default, we define ALIGN_TYPE as double. This is necessary on some + * workstations (where doubles really do need 8-byte alignment) and will work + * fine on nearly everything. If your machine has lesser alignment needs, + * you can save a few bytes by making ALIGN_TYPE smaller. + * The only place I know of where this will NOT work is certain Macintosh + * 680x0 compilers that define double as a 10-byte IEEE extended float. + * Doing 10-byte alignment is counterproductive because longwords won't be + * aligned well. Put "#define ALIGN_TYPE long" in jconfig.h if you have + * such a compiler. + */ + +#ifndef ALIGN_TYPE /* so can override from jconfig.h */ +#define ALIGN_TYPE double +#endif + + +/* + * We allocate objects from "pools", where each pool is gotten with a single + * request to jpeg_get_small() or jpeg_get_large(). There is no per-object + * overhead within a pool, except for alignment padding. Each pool has a + * header with a link to the next pool of the same class. + * Small and large pool headers are identical except that the latter's + * link pointer must be FAR on 80x86 machines. + * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE + * field. This forces the compiler to make SIZEOF(small_pool_hdr) a multiple + * of the alignment requirement of ALIGN_TYPE. + */ + +typedef union small_pool_struct * small_pool_ptr; + +typedef union small_pool_struct { + struct { + small_pool_ptr next; /* next in list of pools */ + size_t bytes_used; /* how many bytes already used within pool */ + size_t bytes_left; /* bytes still available in this pool */ + } hdr; + ALIGN_TYPE dummy; /* included in union to ensure alignment */ +} small_pool_hdr; + +typedef union large_pool_struct FAR * large_pool_ptr; + +typedef union large_pool_struct { + struct { + large_pool_ptr next; /* next in list of pools */ + size_t bytes_used; /* how many bytes already used within pool */ + size_t bytes_left; /* bytes still available in this pool */ + } hdr; + ALIGN_TYPE dummy; /* included in union to ensure alignment */ +} large_pool_hdr; + + +/* + * Here is the full definition of a memory manager object. + */ + +typedef struct { + struct jpeg_memory_mgr pub; /* public fields */ + + /* Each pool identifier (lifetime class) names a linked list of pools. */ + small_pool_ptr small_list[JPOOL_NUMPOOLS]; + large_pool_ptr large_list[JPOOL_NUMPOOLS]; + + /* Since we only have one lifetime class of virtual arrays, only one + * linked list is necessary (for each datatype). Note that the virtual + * array control blocks being linked together are actually stored somewhere + * in the small-pool list. + */ + jvirt_sarray_ptr virt_sarray_list; + jvirt_barray_ptr virt_barray_list; + + /* This counts total space obtained from jpeg_get_small/large */ + long total_space_allocated; + + /* alloc_sarray and alloc_barray set this value for use by virtual + * array routines. + */ + JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */ +} my_memory_mgr; + +typedef my_memory_mgr * my_mem_ptr; + + +/* + * The control blocks for virtual arrays. + * Note that these blocks are allocated in the "small" pool area. + * System-dependent info for the associated backing store (if any) is hidden + * inside the backing_store_info struct. + */ + +struct jvirt_sarray_control { + JSAMPARRAY mem_buffer; /* => the in-memory buffer */ + JDIMENSION rows_in_array; /* total virtual array height */ + JDIMENSION samplesperrow; /* width of array (and of memory buffer) */ + JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */ + JDIMENSION rows_in_mem; /* height of memory buffer */ + JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */ + JDIMENSION cur_start_row; /* first logical row # in the buffer */ + JDIMENSION first_undef_row; /* row # of first uninitialized row */ + boolean pre_zero; /* pre-zero mode requested? */ + boolean dirty; /* do current buffer contents need written? */ + boolean b_s_open; /* is backing-store data valid? */ + jvirt_sarray_ptr next; /* link to next virtual sarray control block */ + backing_store_info b_s_info; /* System-dependent control info */ +}; + +struct jvirt_barray_control { + JBLOCKARRAY mem_buffer; /* => the in-memory buffer */ + JDIMENSION rows_in_array; /* total virtual array height */ + JDIMENSION blocksperrow; /* width of array (and of memory buffer) */ + JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */ + JDIMENSION rows_in_mem; /* height of memory buffer */ + JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */ + JDIMENSION cur_start_row; /* first logical row # in the buffer */ + JDIMENSION first_undef_row; /* row # of first uninitialized row */ + boolean pre_zero; /* pre-zero mode requested? */ + boolean dirty; /* do current buffer contents need written? */ + boolean b_s_open; /* is backing-store data valid? */ + jvirt_barray_ptr next; /* link to next virtual barray control block */ + backing_store_info b_s_info; /* System-dependent control info */ +}; + + +#ifdef MEM_STATS /* optional extra stuff for statistics */ + +LOCAL(void) +print_mem_stats (j_common_ptr cinfo, int pool_id) +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + small_pool_ptr shdr_ptr; + large_pool_ptr lhdr_ptr; + + /* Since this is only a debugging stub, we can cheat a little by using + * fprintf directly rather than going through the trace message code. + * This is helpful because message parm array can't handle longs. + */ + fprintf(stderr, "Freeing pool %d, total space = %ld\n", + pool_id, mem->total_space_allocated); + + for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL; + lhdr_ptr = lhdr_ptr->hdr.next) { + fprintf(stderr, " Large chunk used %ld\n", + (long) lhdr_ptr->hdr.bytes_used); + } + + for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL; + shdr_ptr = shdr_ptr->hdr.next) { + fprintf(stderr, " Small chunk used %ld free %ld\n", + (long) shdr_ptr->hdr.bytes_used, + (long) shdr_ptr->hdr.bytes_left); + } +} + +#endif /* MEM_STATS */ + + +LOCAL(void) +out_of_memory (j_common_ptr cinfo, int which) +/* Report an out-of-memory error and stop execution */ +/* If we compiled MEM_STATS support, report alloc requests before dying */ +{ +#ifdef MEM_STATS + cinfo->err->trace_level = 2; /* force self_destruct to report stats */ +#endif + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which); +} + + +/* + * Allocation of "small" objects. + * + * For these, we use pooled storage. When a new pool must be created, + * we try to get enough space for the current request plus a "slop" factor, + * where the slop will be the amount of leftover space in the new pool. + * The speed vs. space tradeoff is largely determined by the slop values. + * A different slop value is provided for each pool class (lifetime), + * and we also distinguish the first pool of a class from later ones. + * NOTE: the values given work fairly well on both 16- and 32-bit-int + * machines, but may be too small if longs are 64 bits or more. + */ + +static const size_t first_pool_slop[JPOOL_NUMPOOLS] = +{ + 1600, /* first PERMANENT pool */ + 16000 /* first IMAGE pool */ +}; + +static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = +{ + 0, /* additional PERMANENT pools */ + 5000 /* additional IMAGE pools */ +}; + +#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */ + + +METHODDEF(void *) +alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject) +/* Allocate a "small" object */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + small_pool_ptr hdr_ptr, prev_hdr_ptr; + char * data_ptr; + size_t odd_bytes, min_request, slop; + + /* Check for unsatisfiable request (do now to ensure no overflow below) */ + if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr))) + out_of_memory(cinfo, 1); /* request exceeds malloc's ability */ + + /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */ + odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE); + if (odd_bytes > 0) + sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes; + + /* See if space is available in any existing pool */ + if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ + prev_hdr_ptr = NULL; + hdr_ptr = mem->small_list[pool_id]; + while (hdr_ptr != NULL) { + if (hdr_ptr->hdr.bytes_left >= sizeofobject) + break; /* found pool with enough space */ + prev_hdr_ptr = hdr_ptr; + hdr_ptr = hdr_ptr->hdr.next; + } + + /* Time to make a new pool? */ + if (hdr_ptr == NULL) { + /* min_request is what we need now, slop is what will be leftover */ + min_request = sizeofobject + SIZEOF(small_pool_hdr); + if (prev_hdr_ptr == NULL) /* first pool in class? */ + slop = first_pool_slop[pool_id]; + else + slop = extra_pool_slop[pool_id]; + /* Don't ask for more than MAX_ALLOC_CHUNK */ + if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request)) + slop = (size_t) (MAX_ALLOC_CHUNK-min_request); + /* Try to get space, if fail reduce slop and try again */ + for (;;) { + hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop); + if (hdr_ptr != NULL) + break; + slop /= 2; + if (slop < MIN_SLOP) /* give up when it gets real small */ + out_of_memory(cinfo, 2); /* jpeg_get_small failed */ + } + mem->total_space_allocated += min_request + slop; + /* Success, initialize the new pool header and add to end of list */ + hdr_ptr->hdr.next = NULL; + hdr_ptr->hdr.bytes_used = 0; + hdr_ptr->hdr.bytes_left = sizeofobject + slop; + if (prev_hdr_ptr == NULL) /* first pool in class? */ + mem->small_list[pool_id] = hdr_ptr; + else + prev_hdr_ptr->hdr.next = hdr_ptr; + } + + /* OK, allocate the object from the current pool */ + data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */ + data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */ + hdr_ptr->hdr.bytes_used += sizeofobject; + hdr_ptr->hdr.bytes_left -= sizeofobject; + + return (void *) data_ptr; +} + + +/* + * Allocation of "large" objects. + * + * The external semantics of these are the same as "small" objects, + * except that FAR pointers are used on 80x86. However the pool + * management heuristics are quite different. We assume that each + * request is large enough that it may as well be passed directly to + * jpeg_get_large; the pool management just links everything together + * so that we can free it all on demand. + * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY + * structures. The routines that create these structures (see below) + * deliberately bunch rows together to ensure a large request size. + */ + +METHODDEF(void FAR *) +alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject) +/* Allocate a "large" object */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + large_pool_ptr hdr_ptr; + size_t odd_bytes; + + /* Check for unsatisfiable request (do now to ensure no overflow below) */ + if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr))) + out_of_memory(cinfo, 3); /* request exceeds malloc's ability */ + + /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */ + odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE); + if (odd_bytes > 0) + sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes; + + /* Always make a new pool */ + if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ + + hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject + + SIZEOF(large_pool_hdr)); + if (hdr_ptr == NULL) + out_of_memory(cinfo, 4); /* jpeg_get_large failed */ + mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr); + + /* Success, initialize the new pool header and add to list */ + hdr_ptr->hdr.next = mem->large_list[pool_id]; + /* We maintain space counts in each pool header for statistical purposes, + * even though they are not needed for allocation. + */ + hdr_ptr->hdr.bytes_used = sizeofobject; + hdr_ptr->hdr.bytes_left = 0; + mem->large_list[pool_id] = hdr_ptr; + + return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */ +} + + +/* + * Creation of 2-D sample arrays. + * The pointers are in near heap, the samples themselves in FAR heap. + * + * To minimize allocation overhead and to allow I/O of large contiguous + * blocks, we allocate the sample rows in groups of as many rows as possible + * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request. + * NB: the virtual array control routines, later in this file, know about + * this chunking of rows. The rowsperchunk value is left in the mem manager + * object so that it can be saved away if this sarray is the workspace for + * a virtual array. + */ + +METHODDEF(JSAMPARRAY) +alloc_sarray (j_common_ptr cinfo, int pool_id, + JDIMENSION samplesperrow, JDIMENSION numrows) +/* Allocate a 2-D sample array */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + JSAMPARRAY result; + JSAMPROW workspace; + JDIMENSION rowsperchunk, currow, i; + long ltemp; + + /* Calculate max # of rows allowed in one allocation chunk */ + ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / + ((long) samplesperrow * SIZEOF(JSAMPLE)); + if (ltemp <= 0) + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); + if (ltemp < (long) numrows) + rowsperchunk = (JDIMENSION) ltemp; + else + rowsperchunk = numrows; + mem->last_rowsperchunk = rowsperchunk; + + /* Get space for row pointers (small object) */ + result = (JSAMPARRAY) alloc_small(cinfo, pool_id, + (size_t) (numrows * SIZEOF(JSAMPROW))); + + /* Get the rows themselves (large objects) */ + currow = 0; + while (currow < numrows) { + rowsperchunk = MIN(rowsperchunk, numrows - currow); + workspace = (JSAMPROW) alloc_large(cinfo, pool_id, + (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow + * SIZEOF(JSAMPLE))); + for (i = rowsperchunk; i > 0; i--) { + result[currow++] = workspace; + workspace += samplesperrow; + } + } + + return result; +} + + +/* + * Creation of 2-D coefficient-block arrays. + * This is essentially the same as the code for sample arrays, above. + */ + +METHODDEF(JBLOCKARRAY) +alloc_barray (j_common_ptr cinfo, int pool_id, + JDIMENSION blocksperrow, JDIMENSION numrows) +/* Allocate a 2-D coefficient-block array */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + JBLOCKARRAY result; + JBLOCKROW workspace; + JDIMENSION rowsperchunk, currow, i; + long ltemp; + + /* Calculate max # of rows allowed in one allocation chunk */ + ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / + ((long) blocksperrow * SIZEOF(JBLOCK)); + if (ltemp <= 0) + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); + if (ltemp < (long) numrows) + rowsperchunk = (JDIMENSION) ltemp; + else + rowsperchunk = numrows; + mem->last_rowsperchunk = rowsperchunk; + + /* Get space for row pointers (small object) */ + result = (JBLOCKARRAY) alloc_small(cinfo, pool_id, + (size_t) (numrows * SIZEOF(JBLOCKROW))); + + /* Get the rows themselves (large objects) */ + currow = 0; + while (currow < numrows) { + rowsperchunk = MIN(rowsperchunk, numrows - currow); + workspace = (JBLOCKROW) alloc_large(cinfo, pool_id, + (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow + * SIZEOF(JBLOCK))); + for (i = rowsperchunk; i > 0; i--) { + result[currow++] = workspace; + workspace += blocksperrow; + } + } + + return result; +} + + +/* + * About virtual array management: + * + * The above "normal" array routines are only used to allocate strip buffers + * (as wide as the image, but just a few rows high). Full-image-sized buffers + * are handled as "virtual" arrays. The array is still accessed a strip at a + * time, but the memory manager must save the whole array for repeated + * accesses. The intended implementation is that there is a strip buffer in + * memory (as high as is possible given the desired memory limit), plus a + * backing file that holds the rest of the array. + * + * The request_virt_array routines are told the total size of the image and + * the maximum number of rows that will be accessed at once. The in-memory + * buffer must be at least as large as the maxaccess value. + * + * The request routines create control blocks but not the in-memory buffers. + * That is postponed until realize_virt_arrays is called. At that time the + * total amount of space needed is known (approximately, anyway), so free + * memory can be divided up fairly. + * + * The access_virt_array routines are responsible for making a specific strip + * area accessible (after reading or writing the backing file, if necessary). + * Note that the access routines are told whether the caller intends to modify + * the accessed strip; during a read-only pass this saves having to rewrite + * data to disk. The access routines are also responsible for pre-zeroing + * any newly accessed rows, if pre-zeroing was requested. + * + * In current usage, the access requests are usually for nonoverlapping + * strips; that is, successive access start_row numbers differ by exactly + * num_rows = maxaccess. This means we can get good performance with simple + * buffer dump/reload logic, by making the in-memory buffer be a multiple + * of the access height; then there will never be accesses across bufferload + * boundaries. The code will still work with overlapping access requests, + * but it doesn't handle bufferload overlaps very efficiently. + */ + + +METHODDEF(jvirt_sarray_ptr) +request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero, + JDIMENSION samplesperrow, JDIMENSION numrows, + JDIMENSION maxaccess) +/* Request a virtual 2-D sample array */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + jvirt_sarray_ptr result; + + /* Only IMAGE-lifetime virtual arrays are currently supported */ + if (pool_id != JPOOL_IMAGE) + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ + + /* get control block */ + result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id, + SIZEOF(struct jvirt_sarray_control)); + + result->mem_buffer = NULL; /* marks array not yet realized */ + result->rows_in_array = numrows; + result->samplesperrow = samplesperrow; + result->maxaccess = maxaccess; + result->pre_zero = pre_zero; + result->b_s_open = FALSE; /* no associated backing-store object */ + result->next = mem->virt_sarray_list; /* add to list of virtual arrays */ + mem->virt_sarray_list = result; + + return result; +} + + +METHODDEF(jvirt_barray_ptr) +request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero, + JDIMENSION blocksperrow, JDIMENSION numrows, + JDIMENSION maxaccess) +/* Request a virtual 2-D coefficient-block array */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + jvirt_barray_ptr result; + + /* Only IMAGE-lifetime virtual arrays are currently supported */ + if (pool_id != JPOOL_IMAGE) + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ + + /* get control block */ + result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id, + SIZEOF(struct jvirt_barray_control)); + + result->mem_buffer = NULL; /* marks array not yet realized */ + result->rows_in_array = numrows; + result->blocksperrow = blocksperrow; + result->maxaccess = maxaccess; + result->pre_zero = pre_zero; + result->b_s_open = FALSE; /* no associated backing-store object */ + result->next = mem->virt_barray_list; /* add to list of virtual arrays */ + mem->virt_barray_list = result; + + return result; +} + + +METHODDEF(void) +realize_virt_arrays (j_common_ptr cinfo) +/* Allocate the in-memory buffers for any unrealized virtual arrays */ +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + long space_per_minheight, maximum_space, avail_mem; + long minheights, max_minheights; + jvirt_sarray_ptr sptr; + jvirt_barray_ptr bptr; + + /* Compute the minimum space needed (maxaccess rows in each buffer) + * and the maximum space needed (full image height in each buffer). + * These may be of use to the system-dependent jpeg_mem_available routine. + */ + space_per_minheight = 0; + maximum_space = 0; + for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { + if (sptr->mem_buffer == NULL) { /* if not realized yet */ + space_per_minheight += (long) sptr->maxaccess * + (long) sptr->samplesperrow * SIZEOF(JSAMPLE); + maximum_space += (long) sptr->rows_in_array * + (long) sptr->samplesperrow * SIZEOF(JSAMPLE); + } + } + for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { + if (bptr->mem_buffer == NULL) { /* if not realized yet */ + space_per_minheight += (long) bptr->maxaccess * + (long) bptr->blocksperrow * SIZEOF(JBLOCK); + maximum_space += (long) bptr->rows_in_array * + (long) bptr->blocksperrow * SIZEOF(JBLOCK); + } + } + + if (space_per_minheight <= 0) + return; /* no unrealized arrays, no work */ + + /* Determine amount of memory to actually use; this is system-dependent. */ + avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space, + mem->total_space_allocated); + + /* If the maximum space needed is available, make all the buffers full + * height; otherwise parcel it out with the same number of minheights + * in each buffer. + */ + if (avail_mem >= maximum_space) + max_minheights = 1000000000L; + else { + max_minheights = avail_mem / space_per_minheight; + /* If there doesn't seem to be enough space, try to get the minimum + * anyway. This allows a "stub" implementation of jpeg_mem_available(). + */ + if (max_minheights <= 0) + max_minheights = 1; + } + + /* Allocate the in-memory buffers and initialize backing store as needed. */ + + for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { + if (sptr->mem_buffer == NULL) { /* if not realized yet */ + minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L; + if (minheights <= max_minheights) { + /* This buffer fits in memory */ + sptr->rows_in_mem = sptr->rows_in_array; + } else { + /* It doesn't fit in memory, create backing store. */ + sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess); + jpeg_open_backing_store(cinfo, & sptr->b_s_info, + (long) sptr->rows_in_array * + (long) sptr->samplesperrow * + (long) SIZEOF(JSAMPLE)); + sptr->b_s_open = TRUE; + } + sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE, + sptr->samplesperrow, sptr->rows_in_mem); + sptr->rowsperchunk = mem->last_rowsperchunk; + sptr->cur_start_row = 0; + sptr->first_undef_row = 0; + sptr->dirty = FALSE; + } + } + + for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { + if (bptr->mem_buffer == NULL) { /* if not realized yet */ + minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L; + if (minheights <= max_minheights) { + /* This buffer fits in memory */ + bptr->rows_in_mem = bptr->rows_in_array; + } else { + /* It doesn't fit in memory, create backing store. */ + bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess); + jpeg_open_backing_store(cinfo, & bptr->b_s_info, + (long) bptr->rows_in_array * + (long) bptr->blocksperrow * + (long) SIZEOF(JBLOCK)); + bptr->b_s_open = TRUE; + } + bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE, + bptr->blocksperrow, bptr->rows_in_mem); + bptr->rowsperchunk = mem->last_rowsperchunk; + bptr->cur_start_row = 0; + bptr->first_undef_row = 0; + bptr->dirty = FALSE; + } + } +} + + +LOCAL(void) +do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing) +/* Do backing store read or write of a virtual sample array */ +{ + long bytesperrow, file_offset, byte_count, rows, thisrow, i; + + bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE); + file_offset = ptr->cur_start_row * bytesperrow; + /* Loop to read or write each allocation chunk in mem_buffer */ + for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { + /* One chunk, but check for short chunk at end of buffer */ + rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i); + /* Transfer no more than is currently defined */ + thisrow = (long) ptr->cur_start_row + i; + rows = MIN(rows, (long) ptr->first_undef_row - thisrow); + /* Transfer no more than fits in file */ + rows = MIN(rows, (long) ptr->rows_in_array - thisrow); + if (rows <= 0) /* this chunk might be past end of file! */ + break; + byte_count = rows * bytesperrow; + if (writing) + (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info, + (void FAR *) ptr->mem_buffer[i], + file_offset, byte_count); + else + (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info, + (void FAR *) ptr->mem_buffer[i], + file_offset, byte_count); + file_offset += byte_count; + } +} + + +LOCAL(void) +do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing) +/* Do backing store read or write of a virtual coefficient-block array */ +{ + long bytesperrow, file_offset, byte_count, rows, thisrow, i; + + bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK); + file_offset = ptr->cur_start_row * bytesperrow; + /* Loop to read or write each allocation chunk in mem_buffer */ + for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { + /* One chunk, but check for short chunk at end of buffer */ + rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i); + /* Transfer no more than is currently defined */ + thisrow = (long) ptr->cur_start_row + i; + rows = MIN(rows, (long) ptr->first_undef_row - thisrow); + /* Transfer no more than fits in file */ + rows = MIN(rows, (long) ptr->rows_in_array - thisrow); + if (rows <= 0) /* this chunk might be past end of file! */ + break; + byte_count = rows * bytesperrow; + if (writing) + (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info, + (void FAR *) ptr->mem_buffer[i], + file_offset, byte_count); + else + (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info, + (void FAR *) ptr->mem_buffer[i], + file_offset, byte_count); + file_offset += byte_count; + } +} + + +METHODDEF(JSAMPARRAY) +access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr, + JDIMENSION start_row, JDIMENSION num_rows, + boolean writable) +/* Access the part of a virtual sample array starting at start_row */ +/* and extending for num_rows rows. writable is true if */ +/* caller intends to modify the accessed area. */ +{ + JDIMENSION end_row = start_row + num_rows; + JDIMENSION undef_row; + + /* debugging check */ + if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess || + ptr->mem_buffer == NULL) + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); + + /* Make the desired part of the virtual array accessible */ + if (start_row < ptr->cur_start_row || + end_row > ptr->cur_start_row+ptr->rows_in_mem) { + if (! ptr->b_s_open) + ERREXIT(cinfo, JERR_VIRTUAL_BUG); + /* Flush old buffer contents if necessary */ + if (ptr->dirty) { + do_sarray_io(cinfo, ptr, TRUE); + ptr->dirty = FALSE; + } + /* Decide what part of virtual array to access. + * Algorithm: if target address > current window, assume forward scan, + * load starting at target address. If target address < current window, + * assume backward scan, load so that target area is top of window. + * Note that when switching from forward write to forward read, will have + * start_row = 0, so the limiting case applies and we load from 0 anyway. + */ + if (start_row > ptr->cur_start_row) { + ptr->cur_start_row = start_row; + } else { + /* use long arithmetic here to avoid overflow & unsigned problems */ + long ltemp; + + ltemp = (long) end_row - (long) ptr->rows_in_mem; + if (ltemp < 0) + ltemp = 0; /* don't fall off front end of file */ + ptr->cur_start_row = (JDIMENSION) ltemp; + } + /* Read in the selected part of the array. + * During the initial write pass, we will do no actual read + * because the selected part is all undefined. + */ + do_sarray_io(cinfo, ptr, FALSE); + } + /* Ensure the accessed part of the array is defined; prezero if needed. + * To improve locality of access, we only prezero the part of the array + * that the caller is about to access, not the entire in-memory array. + */ + if (ptr->first_undef_row < end_row) { + if (ptr->first_undef_row < start_row) { + if (writable) /* writer skipped over a section of array */ + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); + undef_row = start_row; /* but reader is allowed to read ahead */ + } else { + undef_row = ptr->first_undef_row; + } + if (writable) + ptr->first_undef_row = end_row; + if (ptr->pre_zero) { + size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE); + undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */ + end_row -= ptr->cur_start_row; + while (undef_row < end_row) { + FMEMZERO((void FAR *) ptr->mem_buffer[undef_row], bytesperrow); + undef_row++; + } + } else { + if (! writable) /* reader looking at undefined data */ + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); + } + } + /* Flag the buffer dirty if caller will write in it */ + if (writable) + ptr->dirty = TRUE; + /* Return address of proper part of the buffer */ + return ptr->mem_buffer + (start_row - ptr->cur_start_row); +} + + +METHODDEF(JBLOCKARRAY) +access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr, + JDIMENSION start_row, JDIMENSION num_rows, + boolean writable) +/* Access the part of a virtual block array starting at start_row */ +/* and extending for num_rows rows. writable is true if */ +/* caller intends to modify the accessed area. */ +{ + JDIMENSION end_row = start_row + num_rows; + JDIMENSION undef_row; + + /* debugging check */ + if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess || + ptr->mem_buffer == NULL) + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); + + /* Make the desired part of the virtual array accessible */ + if (start_row < ptr->cur_start_row || + end_row > ptr->cur_start_row+ptr->rows_in_mem) { + if (! ptr->b_s_open) + ERREXIT(cinfo, JERR_VIRTUAL_BUG); + /* Flush old buffer contents if necessary */ + if (ptr->dirty) { + do_barray_io(cinfo, ptr, TRUE); + ptr->dirty = FALSE; + } + /* Decide what part of virtual array to access. + * Algorithm: if target address > current window, assume forward scan, + * load starting at target address. If target address < current window, + * assume backward scan, load so that target area is top of window. + * Note that when switching from forward write to forward read, will have + * start_row = 0, so the limiting case applies and we load from 0 anyway. + */ + if (start_row > ptr->cur_start_row) { + ptr->cur_start_row = start_row; + } else { + /* use long arithmetic here to avoid overflow & unsigned problems */ + long ltemp; + + ltemp = (long) end_row - (long) ptr->rows_in_mem; + if (ltemp < 0) + ltemp = 0; /* don't fall off front end of file */ + ptr->cur_start_row = (JDIMENSION) ltemp; + } + /* Read in the selected part of the array. + * During the initial write pass, we will do no actual read + * because the selected part is all undefined. + */ + do_barray_io(cinfo, ptr, FALSE); + } + /* Ensure the accessed part of the array is defined; prezero if needed. + * To improve locality of access, we only prezero the part of the array + * that the caller is about to access, not the entire in-memory array. + */ + if (ptr->first_undef_row < end_row) { + if (ptr->first_undef_row < start_row) { + if (writable) /* writer skipped over a section of array */ + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); + undef_row = start_row; /* but reader is allowed to read ahead */ + } else { + undef_row = ptr->first_undef_row; + } + if (writable) + ptr->first_undef_row = end_row; + if (ptr->pre_zero) { + size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK); + undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */ + end_row -= ptr->cur_start_row; + while (undef_row < end_row) { + FMEMZERO((void FAR *) ptr->mem_buffer[undef_row], bytesperrow); + undef_row++; + } + } else { + if (! writable) /* reader looking at undefined data */ + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); + } + } + /* Flag the buffer dirty if caller will write in it */ + if (writable) + ptr->dirty = TRUE; + /* Return address of proper part of the buffer */ + return ptr->mem_buffer + (start_row - ptr->cur_start_row); +} + + +/* + * Release all objects belonging to a specified pool. + */ + +METHODDEF(void) +free_pool (j_common_ptr cinfo, int pool_id) +{ + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; + small_pool_ptr shdr_ptr; + large_pool_ptr lhdr_ptr; + size_t space_freed; + + if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ + +#ifdef MEM_STATS + if (cinfo->err->trace_level > 1) + print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */ +#endif + + /* If freeing IMAGE pool, close any virtual arrays first */ + if (pool_id == JPOOL_IMAGE) { + jvirt_sarray_ptr sptr; + jvirt_barray_ptr bptr; + + for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { + if (sptr->b_s_open) { /* there may be no backing store */ + sptr->b_s_open = FALSE; /* prevent recursive close if error */ + (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info); + } + } + mem->virt_sarray_list = NULL; + for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { + if (bptr->b_s_open) { /* there may be no backing store */ + bptr->b_s_open = FALSE; /* prevent recursive close if error */ + (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info); + } + } + mem->virt_barray_list = NULL; + } + + /* Release large objects */ + lhdr_ptr = mem->large_list[pool_id]; + mem->large_list[pool_id] = NULL; + + while (lhdr_ptr != NULL) { + large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next; + space_freed = lhdr_ptr->hdr.bytes_used + + lhdr_ptr->hdr.bytes_left + + SIZEOF(large_pool_hdr); + jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed); + mem->total_space_allocated -= space_freed; + lhdr_ptr = next_lhdr_ptr; + } + + /* Release small objects */ + shdr_ptr = mem->small_list[pool_id]; + mem->small_list[pool_id] = NULL; + + while (shdr_ptr != NULL) { + small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next; + space_freed = shdr_ptr->hdr.bytes_used + + shdr_ptr->hdr.bytes_left + + SIZEOF(small_pool_hdr); + jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed); + mem->total_space_allocated -= space_freed; + shdr_ptr = next_shdr_ptr; + } +} + + +/* + * Close up shop entirely. + * Note that this cannot be called unless cinfo->mem is non-NULL. + */ + +METHODDEF(void) +self_destruct (j_common_ptr cinfo) +{ + int pool; + + /* Close all backing store, release all memory. + * Releasing pools in reverse order might help avoid fragmentation + * with some (brain-damaged) malloc libraries. + */ + for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { + free_pool(cinfo, pool); + } + + /* Release the memory manager control block too. */ + jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr)); + cinfo->mem = NULL; /* ensures I will be called only once */ + + jpeg_mem_term(cinfo); /* system-dependent cleanup */ +} + + +/* + * Memory manager initialization. + * When this is called, only the error manager pointer is valid in cinfo! + */ + +GLOBAL(void) +jinit_memory_mgr (j_common_ptr cinfo) +{ + my_mem_ptr mem; + long max_to_use; + int pool; + size_t test_mac; + + cinfo->mem = NULL; /* for safety if init fails */ + + /* Check for configuration errors. + * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably + * doesn't reflect any real hardware alignment requirement. + * The test is a little tricky: for X>0, X and X-1 have no one-bits + * in common if and only if X is a power of 2, ie has only one one-bit. + * Some compilers may give an "unreachable code" warning here; ignore it. + */ + if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0) + ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE); + /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be + * a multiple of SIZEOF(ALIGN_TYPE). + * Again, an "unreachable code" warning may be ignored here. + * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK. + */ + test_mac = (size_t) MAX_ALLOC_CHUNK; + if ((long) test_mac != MAX_ALLOC_CHUNK || + (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0) + ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK); + + max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */ + + /* Attempt to allocate memory manager's control block */ + mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr)); + + if (mem == NULL) { + jpeg_mem_term(cinfo); /* system-dependent cleanup */ + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0); + } + + /* OK, fill in the method pointers */ + mem->pub.alloc_small = alloc_small; + mem->pub.alloc_large = alloc_large; + mem->pub.alloc_sarray = alloc_sarray; + mem->pub.alloc_barray = alloc_barray; + mem->pub.request_virt_sarray = request_virt_sarray; + mem->pub.request_virt_barray = request_virt_barray; + mem->pub.realize_virt_arrays = realize_virt_arrays; + mem->pub.access_virt_sarray = access_virt_sarray; + mem->pub.access_virt_barray = access_virt_barray; + mem->pub.free_pool = free_pool; + mem->pub.self_destruct = self_destruct; + + /* Make MAX_ALLOC_CHUNK accessible to other modules */ + mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK; + + /* Initialize working state */ + mem->pub.max_memory_to_use = max_to_use; + + for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { + mem->small_list[pool] = NULL; + mem->large_list[pool] = NULL; + } + mem->virt_sarray_list = NULL; + mem->virt_barray_list = NULL; + + mem->total_space_allocated = SIZEOF(my_memory_mgr); + + /* Declare ourselves open for business */ + cinfo->mem = & mem->pub; + + /* Check for an environment variable JPEGMEM; if found, override the + * default max_memory setting from jpeg_mem_init. Note that the + * surrounding application may again override this value. + * If your system doesn't support getenv(), define NO_GETENV to disable + * this feature. + */ +#ifndef NO_GETENV + { char * memenv; + + if ((memenv = getenv("JPEGMEM")) != NULL) { + char ch = 'x'; + + if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { + if (ch == 'm' || ch == 'M') + max_to_use *= 1000L; + mem->pub.max_memory_to_use = max_to_use * 1000L; + } + } + } +#endif + +} diff --git a/crypto777/jpeg/jmemname.c b/crypto777/jpeg/jmemname.c new file mode 100644 index 000000000..ed96dee1b --- /dev/null +++ b/crypto777/jpeg/jmemname.c @@ -0,0 +1,276 @@ +/* + * jmemname.c + * + * Copyright (C) 1992-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file provides a generic implementation of the system-dependent + * portion of the JPEG memory manager. This implementation assumes that + * you must explicitly construct a name for each temp file. + * Also, the problem of determining the amount of memory available + * is shoved onto the user. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jmemsys.h" /* import the system-dependent declarations */ + +#ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ +extern void * malloc JPP((size_t size)); +extern void free JPP((void *ptr)); +#endif + +#ifndef SEEK_SET /* pre-ANSI systems may not define this; */ +#define SEEK_SET 0 /* if not, assume 0 is correct */ +#endif + +#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */ +#define READ_BINARY "r" +#define RW_BINARY "w+" +#else +#ifdef VMS /* VMS is very nonstandard */ +#define READ_BINARY "rb", "ctx=stm" +#define RW_BINARY "w+b", "ctx=stm" +#else /* standard ANSI-compliant case */ +#define READ_BINARY "rb" +#define RW_BINARY "w+b" +#endif +#endif + + +/* + * Selection of a file name for a temporary file. + * This is system-dependent! + * + * The code as given is suitable for most Unix systems, and it is easily + * modified for most non-Unix systems. Some notes: + * 1. The temp file is created in the directory named by TEMP_DIRECTORY. + * The default value is /usr/tmp, which is the conventional place for + * creating large temp files on Unix. On other systems you'll probably + * want to change the file location. You can do this by editing the + * #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h. + * + * 2. If you need to change the file name as well as its location, + * you can override the TEMP_FILE_NAME macro. (Note that this is + * actually a printf format string; it must contain %s and %d.) + * Few people should need to do this. + * + * 3. mktemp() is used to ensure that multiple processes running + * simultaneously won't select the same file names. If your system + * doesn't have mktemp(), define NO_MKTEMP to do it the hard way. + * (If you don't have , also define NO_ERRNO_H.) + * + * 4. You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c + * will cause the temp files to be removed if you stop the program early. + */ + +#ifndef TEMP_DIRECTORY /* can override from jconfig.h or Makefile */ +#define TEMP_DIRECTORY "/usr/tmp/" /* recommended setting for Unix */ +#endif + +static int next_file_num; /* to distinguish among several temp files */ + +#ifdef NO_MKTEMP + +#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */ +#define TEMP_FILE_NAME "%sJPG%03d.TMP" +#endif + +#ifndef NO_ERRNO_H +#include /* to define ENOENT */ +#endif + +/* ANSI C specifies that errno is a macro, but on older systems it's more + * likely to be a plain int variable. And not all versions of errno.h + * bother to declare it, so we have to in order to be most portable. Thus: + */ +#ifndef errno +extern int errno; +#endif + + +LOCAL(void) +select_file_name (char * fname) +{ + FILE * tfile; + + /* Keep generating file names till we find one that's not in use */ + for (;;) { + next_file_num++; /* advance counter */ + sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num); + if ((tfile = fopen(fname, READ_BINARY)) == NULL) { + /* fopen could have failed for a reason other than the file not + * being there; for example, file there but unreadable. + * If isn't available, then we cannot test the cause. + */ +#ifdef ENOENT + if (errno != ENOENT) + continue; +#endif + break; + } + fclose(tfile); /* oops, it's there; close tfile & try again */ + } +} + +#else /* ! NO_MKTEMP */ + +/* Note that mktemp() requires the initial filename to end in six X's */ +#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */ +#define TEMP_FILE_NAME "%sJPG%dXXXXXX" +#endif + +LOCAL(void) +select_file_name (char * fname) +{ + next_file_num++; /* advance counter */ + sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num); + mktemp(fname); /* make sure file name is unique */ + /* mktemp replaces the trailing XXXXXX with a unique string of characters */ +} + +#endif /* NO_MKTEMP */ + + +/* + * Memory allocation and freeing are controlled by the regular library + * routines malloc() and free(). + */ + +GLOBAL(void *) +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * "Large" objects are treated the same as "small" ones. + * NB: although we include FAR keywords in the routine declarations, + * this file won't actually work in 80x86 small/medium model; at least, + * you probably won't be able to process useful-size images in only 64KB. + */ + +GLOBAL(void FAR *) +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void FAR *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * This routine computes the total memory space available for allocation. + * It's impossible to do this in a portable way; our current solution is + * to make the user tell us (with a default value set at compile time). + * If you can actually get the available space, it's a good idea to subtract + * a slop factor of 5% or so. + */ + +#ifndef DEFAULT_MAX_MEM /* so can override from makefile */ +#define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */ +#endif + +GLOBAL(long) +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, + long max_bytes_needed, long already_allocated) +{ + return cinfo->mem->max_memory_to_use - already_allocated; +} + + +/* + * Backing store (temporary file) management. + * Backing store objects are only used when the value returned by + * jpeg_mem_available is less than the total space needed. You can dispense + * with these routines if you have plenty of virtual memory; see jmemnobs.c. + */ + + +METHODDEF(void) +read_backing_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + if (fseek(info->temp_file, file_offset, SEEK_SET)) + ERREXIT(cinfo, JERR_TFILE_SEEK); + if (JFREAD(info->temp_file, buffer_address, byte_count) + != (size_t) byte_count) + ERREXIT(cinfo, JERR_TFILE_READ); +} + + +METHODDEF(void) +write_backing_store (j_common_ptr cinfo, backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count) +{ + if (fseek(info->temp_file, file_offset, SEEK_SET)) + ERREXIT(cinfo, JERR_TFILE_SEEK); + if (JFWRITE(info->temp_file, buffer_address, byte_count) + != (size_t) byte_count) + ERREXIT(cinfo, JERR_TFILE_WRITE); +} + + +METHODDEF(void) +close_backing_store (j_common_ptr cinfo, backing_store_ptr info) +{ + fclose(info->temp_file); /* close the file */ + unlink(info->temp_name); /* delete the file */ +/* If your system doesn't have unlink(), use remove() instead. + * remove() is the ANSI-standard name for this function, but if + * your system was ANSI you'd be using jmemansi.c, right? + */ + TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name); +} + + +/* + * Initial opening of a backing-store object. + */ + +GLOBAL(void) +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + select_file_name(info->temp_name); + if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL) + ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name); + info->read_backing_store = read_backing_store; + info->write_backing_store = write_backing_store; + info->close_backing_store = close_backing_store; + TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name); +} + + +/* + * These routines take care of any system-dependent initialization and + * cleanup required. + */ + +GLOBAL(long) +jpeg_mem_init (j_common_ptr cinfo) +{ + next_file_num = 0; /* initialize temp file name generator */ + return DEFAULT_MAX_MEM; /* default for max_memory_to_use */ +} + +GLOBAL(void) +jpeg_mem_term (j_common_ptr cinfo) +{ + /* no work */ +} diff --git a/crypto777/jpeg/jmemnobs.c b/crypto777/jpeg/jmemnobs.c new file mode 100644 index 000000000..eb8c33772 --- /dev/null +++ b/crypto777/jpeg/jmemnobs.c @@ -0,0 +1,109 @@ +/* + * jmemnobs.c + * + * Copyright (C) 1992-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file provides a really simple implementation of the system- + * dependent portion of the JPEG memory manager. This implementation + * assumes that no backing-store files are needed: all required space + * can be obtained from malloc(). + * This is very portable in the sense that it'll compile on almost anything, + * but you'd better have lots of main memory (or virtual memory) if you want + * to process big images. + * Note that the max_memory_to_use option is ignored by this implementation. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" +#include "jmemsys.h" /* import the system-dependent declarations */ + +#ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ +extern void * malloc JPP((size_t size)); +extern void free JPP((void *ptr)); +#endif + + +/* + * Memory allocation and freeing are controlled by the regular library + * routines malloc() and free(). + */ + +GLOBAL(void *) +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * "Large" objects are treated the same as "small" ones. + * NB: although we include FAR keywords in the routine declarations, + * this file won't actually work in 80x86 small/medium model; at least, + * you probably won't be able to process useful-size images in only 64KB. + */ + +GLOBAL(void FAR *) +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) +{ + return (void FAR *) malloc(sizeofobject); +} + +GLOBAL(void) +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) +{ + free(object); +} + + +/* + * This routine computes the total memory space available for allocation. + * Here we always say, "we got all you want bud!" + */ + +GLOBAL(long) +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, + long max_bytes_needed, long already_allocated) +{ + return max_bytes_needed; +} + + +/* + * Backing store (temporary file) management. + * Since jpeg_mem_available always promised the moon, + * this should never be called and we can just error out. + */ + +GLOBAL(void) +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, + long total_bytes_needed) +{ + ERREXIT(cinfo, JERR_NO_BACKING_STORE); +} + + +/* + * These routines take care of any system-dependent initialization and + * cleanup required. Here, there isn't any. + */ + +GLOBAL(long) +jpeg_mem_init (j_common_ptr cinfo) +{ + return 0; /* just set max_memory_to_use to 0 */ +} + +GLOBAL(void) +jpeg_mem_term (j_common_ptr cinfo) +{ + /* no work */ +} diff --git a/crypto777/jpeg/jmemsys.h b/crypto777/jpeg/jmemsys.h new file mode 100644 index 000000000..6c3c6d348 --- /dev/null +++ b/crypto777/jpeg/jmemsys.h @@ -0,0 +1,198 @@ +/* + * jmemsys.h + * + * Copyright (C) 1992-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This include file defines the interface between the system-independent + * and system-dependent portions of the JPEG memory manager. No other + * modules need include it. (The system-independent portion is jmemmgr.c; + * there are several different versions of the system-dependent portion.) + * + * This file works as-is for the system-dependent memory managers supplied + * in the IJG distribution. You may need to modify it if you write a + * custom memory manager. If system-dependent changes are needed in + * this file, the best method is to #ifdef them based on a configuration + * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR + * and USE_MAC_MEMMGR. + */ + + +/* Short forms of external names for systems with brain-damaged linkers. */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jpeg_get_small jGetSmall +#define jpeg_free_small jFreeSmall +#define jpeg_get_large jGetLarge +#define jpeg_free_large jFreeLarge +#define jpeg_mem_available jMemAvail +#define jpeg_open_backing_store jOpenBackStore +#define jpeg_mem_init jMemInit +#define jpeg_mem_term jMemTerm +#endif /* NEED_SHORT_EXTERNAL_NAMES */ + + +/* + * These two functions are used to allocate and release small chunks of + * memory. (Typically the total amount requested through jpeg_get_small is + * no more than 20K or so; this will be requested in chunks of a few K each.) + * Behavior should be the same as for the standard library functions malloc + * and free; in particular, jpeg_get_small must return NULL on failure. + * On most systems, these ARE malloc and free. jpeg_free_small is passed the + * size of the object being freed, just in case it's needed. + * On an 80x86 machine using small-data memory model, these manage near heap. + */ + +EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject)); +EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object, + size_t sizeofobject)); + +/* + * These two functions are used to allocate and release large chunks of + * memory (up to the total free space designated by jpeg_mem_available). + * The interface is the same as above, except that on an 80x86 machine, + * far pointers are used. On most other machines these are identical to + * the jpeg_get/free_small routines; but we keep them separate anyway, + * in case a different allocation strategy is desirable for large chunks. + */ + +EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo, + size_t sizeofobject)); +EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object, + size_t sizeofobject)); + +/* + * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may + * be requested in a single call to jpeg_get_large (and jpeg_get_small for that + * matter, but that case should never come into play). This macro is needed + * to model the 64Kb-segment-size limit of far addressing on 80x86 machines. + * On those machines, we expect that jconfig.h will provide a proper value. + * On machines with 32-bit flat address spaces, any large constant may be used. + * + * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type + * size_t and will be a multiple of sizeof(align_type). + */ + +#ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */ +#define MAX_ALLOC_CHUNK 1000000000L +#endif + +/* + * This routine computes the total space still available for allocation by + * jpeg_get_large. If more space than this is needed, backing store will be + * used. NOTE: any memory already allocated must not be counted. + * + * There is a minimum space requirement, corresponding to the minimum + * feasible buffer sizes; jmemmgr.c will request that much space even if + * jpeg_mem_available returns zero. The maximum space needed, enough to hold + * all working storage in memory, is also passed in case it is useful. + * Finally, the total space already allocated is passed. If no better + * method is available, cinfo->mem->max_memory_to_use - already_allocated + * is often a suitable calculation. + * + * It is OK for jpeg_mem_available to underestimate the space available + * (that'll just lead to more backing-store access than is really necessary). + * However, an overestimate will lead to failure. Hence it's wise to subtract + * a slop factor from the true available space. 5% should be enough. + * + * On machines with lots of virtual memory, any large constant may be returned. + * Conversely, zero may be returned to always use the minimum amount of memory. + */ + +EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo, + long min_bytes_needed, + long max_bytes_needed, + long already_allocated)); + + +/* + * This structure holds whatever state is needed to access a single + * backing-store object. The read/write/close method pointers are called + * by jmemmgr.c to manipulate the backing-store object; all other fields + * are private to the system-dependent backing store routines. + */ + +#define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */ + + +#ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */ + +typedef unsigned short XMSH; /* type of extended-memory handles */ +typedef unsigned short EMSH; /* type of expanded-memory handles */ + +typedef union { + short file_handle; /* DOS file handle if it's a temp file */ + XMSH xms_handle; /* handle if it's a chunk of XMS */ + EMSH ems_handle; /* handle if it's a chunk of EMS */ +} handle_union; + +#endif /* USE_MSDOS_MEMMGR */ + +#ifdef USE_MAC_MEMMGR /* Mac-specific junk */ +#include +#endif /* USE_MAC_MEMMGR */ + + +typedef struct backing_store_struct * backing_store_ptr; + +typedef struct backing_store_struct { + /* Methods for reading/writing/closing this backing-store object */ + JMETHOD(void, read_backing_store, (j_common_ptr cinfo, + backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count)); + JMETHOD(void, write_backing_store, (j_common_ptr cinfo, + backing_store_ptr info, + void FAR * buffer_address, + long file_offset, long byte_count)); + JMETHOD(void, close_backing_store, (j_common_ptr cinfo, + backing_store_ptr info)); + + /* Private fields for system-dependent backing-store management */ +#ifdef USE_MSDOS_MEMMGR + /* For the MS-DOS manager (jmemdos.c), we need: */ + handle_union handle; /* reference to backing-store storage object */ + char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ +#else +#ifdef USE_MAC_MEMMGR + /* For the Mac manager (jmemmac.c), we need: */ + short temp_file; /* file reference number to temp file */ + FSSpec tempSpec; /* the FSSpec for the temp file */ + char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ +#else + /* For a typical implementation with temp files, we need: */ + FILE * temp_file; /* stdio reference to temp file */ + char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */ +#endif +#endif +} backing_store_info; + + +/* + * Initial opening of a backing-store object. This must fill in the + * read/write/close pointers in the object. The read/write routines + * may take an error exit if the specified maximum file size is exceeded. + * (If jpeg_mem_available always returns a large value, this routine can + * just take an error exit.) + */ + +EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo, + backing_store_ptr info, + long total_bytes_needed)); + + +/* + * These routines take care of any system-dependent initialization and + * cleanup required. jpeg_mem_init will be called before anything is + * allocated (and, therefore, nothing in cinfo is of use except the error + * manager pointer). It should return a suitable default value for + * max_memory_to_use; this may subsequently be overridden by the surrounding + * application. (Note that max_memory_to_use is only important if + * jpeg_mem_available chooses to consult it ... no one else will.) + * jpeg_mem_term may assume that all requested memory has been freed and that + * all opened backing-store objects have been closed. + */ + +EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo)); +EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo)); diff --git a/crypto777/jpeg/jmorecfg.h b/crypto777/jpeg/jmorecfg.h new file mode 100644 index 000000000..6c085c36a --- /dev/null +++ b/crypto777/jpeg/jmorecfg.h @@ -0,0 +1,369 @@ +/* + * jmorecfg.h + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 1997-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains additional configuration options that customize the + * JPEG software for special applications or support machine-dependent + * optimizations. Most users will not need to touch this file. + */ + + +/* + * Define BITS_IN_JSAMPLE as either + * 8 for 8-bit sample values (the usual setting) + * 12 for 12-bit sample values + * Only 8 and 12 are legal data precisions for lossy JPEG according to the + * JPEG standard, and the IJG code does not support anything else! + * We do not support run-time selection of data precision, sorry. + */ + +#define BITS_IN_JSAMPLE 8 /* use 8 or 12 */ + + +/* + * Maximum number of components (color channels) allowed in JPEG image. + * To meet the letter of the JPEG spec, set this to 255. However, darn + * few applications need more than 4 channels (maybe 5 for CMYK + alpha + * mask). We recommend 10 as a reasonable compromise; use 4 if you are + * really short on memory. (Each allowed component costs a hundred or so + * bytes of storage, whether actually used in an image or not.) + */ + +#define MAX_COMPONENTS 10 /* maximum number of image components */ + + +/* + * Basic data types. + * You may need to change these if you have a machine with unusual data + * type sizes; for example, "char" not 8 bits, "short" not 16 bits, + * or "long" not 32 bits. We don't care whether "int" is 16 or 32 bits, + * but it had better be at least 16. + */ + +/* Representation of a single sample (pixel element value). + * We frequently allocate large arrays of these, so it's important to keep + * them small. But if you have memory to burn and access to char or short + * arrays is very slow on your hardware, you might want to change these. + */ + +#if BITS_IN_JSAMPLE == 8 +/* JSAMPLE should be the smallest type that will hold the values 0..255. + * You can use a signed char by having GETJSAMPLE mask it with 0xFF. + */ + +#ifdef HAVE_UNSIGNED_CHAR + +typedef unsigned char JSAMPLE; +#define GETJSAMPLE(value) ((int) (value)) + +#else /* not HAVE_UNSIGNED_CHAR */ + +typedef char JSAMPLE; +#ifdef CHAR_IS_UNSIGNED +#define GETJSAMPLE(value) ((int) (value)) +#else +#define GETJSAMPLE(value) ((int) (value) & 0xFF) +#endif /* CHAR_IS_UNSIGNED */ + +#endif /* HAVE_UNSIGNED_CHAR */ + +#define MAXJSAMPLE 255 +#define CENTERJSAMPLE 128 + +#endif /* BITS_IN_JSAMPLE == 8 */ + + +#if BITS_IN_JSAMPLE == 12 +/* JSAMPLE should be the smallest type that will hold the values 0..4095. + * On nearly all machines "short" will do nicely. + */ + +typedef short JSAMPLE; +#define GETJSAMPLE(value) ((int) (value)) + +#define MAXJSAMPLE 4095 +#define CENTERJSAMPLE 2048 + +#endif /* BITS_IN_JSAMPLE == 12 */ + + +/* Representation of a DCT frequency coefficient. + * This should be a signed value of at least 16 bits; "short" is usually OK. + * Again, we allocate large arrays of these, but you can change to int + * if you have memory to burn and "short" is really slow. + */ + +typedef short JCOEF; + + +/* Compressed datastreams are represented as arrays of JOCTET. + * These must be EXACTLY 8 bits wide, at least once they are written to + * external storage. Note that when using the stdio data source/destination + * managers, this is also the data type passed to fread/fwrite. + */ + +#ifdef HAVE_UNSIGNED_CHAR + +typedef unsigned char JOCTET; +#define GETJOCTET(value) (value) + +#else /* not HAVE_UNSIGNED_CHAR */ + +typedef char JOCTET; +#ifdef CHAR_IS_UNSIGNED +#define GETJOCTET(value) (value) +#else +#define GETJOCTET(value) ((value) & 0xFF) +#endif /* CHAR_IS_UNSIGNED */ + +#endif /* HAVE_UNSIGNED_CHAR */ + + +/* These typedefs are used for various table entries and so forth. + * They must be at least as wide as specified; but making them too big + * won't cost a huge amount of memory, so we don't provide special + * extraction code like we did for JSAMPLE. (In other words, these + * typedefs live at a different point on the speed/space tradeoff curve.) + */ + +/* UINT8 must hold at least the values 0..255. */ + +#ifdef HAVE_UNSIGNED_CHAR +typedef unsigned char UINT8; +#else /* not HAVE_UNSIGNED_CHAR */ +#ifdef CHAR_IS_UNSIGNED +typedef char UINT8; +#else /* not CHAR_IS_UNSIGNED */ +typedef short UINT8; +#endif /* CHAR_IS_UNSIGNED */ +#endif /* HAVE_UNSIGNED_CHAR */ + +/* UINT16 must hold at least the values 0..65535. */ + +#ifdef HAVE_UNSIGNED_SHORT +typedef unsigned short UINT16; +#else /* not HAVE_UNSIGNED_SHORT */ +typedef unsigned int UINT16; +#endif /* HAVE_UNSIGNED_SHORT */ + +/* INT16 must hold at least the values -32768..32767. */ + +#ifndef XMD_H /* X11/xmd.h correctly defines INT16 */ +typedef short INT16; +#endif + +/* INT32 must hold at least signed 32-bit values. */ + +#ifndef XMD_H /* X11/xmd.h correctly defines INT32 */ +#ifndef _BASETSD_H_ /* Microsoft defines it in basetsd.h */ +#ifndef _BASETSD_H /* MinGW is slightly different */ +#ifndef QGLOBAL_H /* Qt defines it in qglobal.h */ +typedef long INT32; +#endif +#endif +#endif +#endif + +/* Datatype used for image dimensions. The JPEG standard only supports + * images up to 64K*64K due to 16-bit fields in SOF markers. Therefore + * "unsigned int" is sufficient on all machines. However, if you need to + * handle larger images and you don't mind deviating from the spec, you + * can change this datatype. + */ + +typedef unsigned int JDIMENSION; + +#define JPEG_MAX_DIMENSION 65500L /* a tad under 64K to prevent overflows */ + + +/* These macros are used in all function definitions and extern declarations. + * You could modify them if you need to change function linkage conventions; + * in particular, you'll need to do that to make the library a Windows DLL. + * Another application is to make all functions global for use with debuggers + * or code profilers that require it. + */ + +/* a function called through method pointers: */ +#define METHODDEF(type) static type +/* a function used only in its module: */ +#define LOCAL(type) static type +/* a function referenced thru EXTERNs: */ +#define GLOBAL(type) type +/* a reference to a GLOBAL function: */ +#define EXTERN(type) extern type + + +/* This macro is used to declare a "method", that is, a function pointer. + * We want to supply prototype parameters if the compiler can cope. + * Note that the arglist parameter must be parenthesized! + * Again, you can customize this if you need special linkage keywords. + */ + +#ifdef HAVE_PROTOTYPES +#define JMETHOD(type,methodname,arglist) type (*methodname) arglist +#else +#define JMETHOD(type,methodname,arglist) type (*methodname) () +#endif + + +/* Here is the pseudo-keyword for declaring pointers that must be "far" + * on 80x86 machines. Most of the specialized coding for 80x86 is handled + * by just saying "FAR *" where such a pointer is needed. In a few places + * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol. + */ + +#ifndef FAR +#ifdef NEED_FAR_POINTERS +#define FAR far +#else +#define FAR +#endif +#endif + + +/* + * On a few systems, type boolean and/or its values FALSE, TRUE may appear + * in standard header files. Or you may have conflicts with application- + * specific header files that you want to include together with these files. + * Defining HAVE_BOOLEAN before including jpeglib.h should make it work. + */ + +#ifndef HAVE_BOOLEAN +typedef int boolean; +#endif +#ifndef FALSE /* in case these macros already exist */ +#define FALSE 0 /* values of boolean */ +#endif +#ifndef TRUE +#define TRUE 1 +#endif + + +/* + * The remaining options affect code selection within the JPEG library, + * but they don't need to be visible to most applications using the library. + * To minimize application namespace pollution, the symbols won't be + * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined. + */ + +#ifdef JPEG_INTERNALS +#define JPEG_INTERNAL_OPTIONS +#endif + +#ifdef JPEG_INTERNAL_OPTIONS + + +/* + * These defines indicate whether to include various optional functions. + * Undefining some of these symbols will produce a smaller but less capable + * library. Note that you can leave certain source files out of the + * compilation/linking process if you've #undef'd the corresponding symbols. + * (You may HAVE to do that if your compiler doesn't like null source files.) + */ + +/* Capability options common to encoder and decoder: */ + +#define DCT_ISLOW_SUPPORTED /* slow but accurate integer algorithm */ +#define DCT_IFAST_SUPPORTED /* faster, less accurate integer method */ +#define DCT_FLOAT_SUPPORTED /* floating-point: accurate, fast on fast HW */ + +/* Encoder capability options: */ + +#define C_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */ +#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */ +#define C_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/ +#define DCT_SCALING_SUPPORTED /* Input rescaling via DCT? (Requires DCT_ISLOW)*/ +#define ENTROPY_OPT_SUPPORTED /* Optimization of entropy coding parms? */ +/* Note: if you selected 12-bit data precision, it is dangerous to turn off + * ENTROPY_OPT_SUPPORTED. The standard Huffman tables are only good for 8-bit + * precision, so jchuff.c normally uses entropy optimization to compute + * usable tables for higher precision. If you don't want to do optimization, + * you'll have to supply different default Huffman tables. + * The exact same statements apply for progressive JPEG: the default tables + * don't work for progressive mode. (This may get fixed, however.) + */ +#define INPUT_SMOOTHING_SUPPORTED /* Input image smoothing option? */ + +/* Decoder capability options: */ + +#define D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */ +#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */ +#define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/ +#define IDCT_SCALING_SUPPORTED /* Output rescaling via IDCT? */ +#define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */ +#define BLOCK_SMOOTHING_SUPPORTED /* Block smoothing? (Progressive only) */ +#undef UPSAMPLE_SCALING_SUPPORTED /* Output rescaling at upsample stage? */ +#define UPSAMPLE_MERGING_SUPPORTED /* Fast path for sloppy upsampling? */ +#define QUANT_1PASS_SUPPORTED /* 1-pass color quantization? */ +#define QUANT_2PASS_SUPPORTED /* 2-pass color quantization? */ + +/* more capability options later, no doubt */ + + +/* + * Ordering of RGB data in scanlines passed to or from the application. + * If your application wants to deal with data in the order B,G,R, just + * change these macros. You can also deal with formats such as R,G,B,X + * (one extra byte per pixel) by changing RGB_PIXELSIZE. Note that changing + * the offsets will also change the order in which colormap data is organized. + * RESTRICTIONS: + * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats. + * 2. The color quantizer modules will not behave desirably if RGB_PIXELSIZE + * is not 3 (they don't understand about dummy color components!). So you + * can't use color quantization if you change that value. + */ + +#define RGB_RED 0 /* Offset of Red in an RGB scanline element */ +#define RGB_GREEN 1 /* Offset of Green */ +#define RGB_BLUE 2 /* Offset of Blue */ +#define RGB_PIXELSIZE 3 /* JSAMPLEs per RGB scanline element */ + + +/* Definitions for speed-related optimizations. */ + + +/* If your compiler supports inline functions, define INLINE + * as the inline keyword; otherwise define it as empty. + */ + +#ifndef INLINE +#ifdef __GNUC__ /* for instance, GNU C knows about inline */ +#define INLINE __inline__ +#endif +#ifndef INLINE +#define INLINE /* default is to define it as empty */ +#endif +#endif + + +/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying + * two 16-bit shorts is faster than multiplying two ints. Define MULTIPLIER + * as short on such a machine. MULTIPLIER must be at least 16 bits wide. + */ + +#ifndef MULTIPLIER +#define MULTIPLIER int /* type for fastest integer multiply */ +#endif + + +/* FAST_FLOAT should be either float or double, whichever is done faster + * by your compiler. (Note that this type is only used in the floating point + * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.) + * Typically, float is faster in ANSI C compilers, while double is faster in + * pre-ANSI compilers (because they insist on converting to double anyway). + * The code below therefore chooses float if we have ANSI-style prototypes. + */ + +#ifndef FAST_FLOAT +#ifdef HAVE_PROTOTYPES +#define FAST_FLOAT float +#else +#define FAST_FLOAT double +#endif +#endif + +#endif /* JPEG_INTERNAL_OPTIONS */ diff --git a/crypto777/jpeg/jpegint.h b/crypto777/jpeg/jpegint.h new file mode 100644 index 000000000..c0d5c1420 --- /dev/null +++ b/crypto777/jpeg/jpegint.h @@ -0,0 +1,426 @@ +/* + * jpegint.h + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 1997-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file provides common declarations for the various JPEG modules. + * These declarations are considered internal to the JPEG library; most + * applications using the library shouldn't need to include this file. + */ + + +/* Declarations for both compression & decompression */ + +typedef enum { /* Operating modes for buffer controllers */ + JBUF_PASS_THRU, /* Plain stripwise operation */ + /* Remaining modes require a full-image buffer to have been created */ + JBUF_SAVE_SOURCE, /* Run source subobject only, save output */ + JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */ + JBUF_SAVE_AND_PASS /* Run both subobjects, save output */ +} J_BUF_MODE; + +/* Values of global_state field (jdapi.c has some dependencies on ordering!) */ +#define CSTATE_START 100 /* after create_compress */ +#define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */ +#define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */ +#define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */ +#define DSTATE_START 200 /* after create_decompress */ +#define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */ +#define DSTATE_READY 202 /* found SOS, ready for start_decompress */ +#define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/ +#define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */ +#define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */ +#define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */ +#define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */ +#define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */ +#define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */ +#define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */ + + +/* Declarations for compression modules */ + +/* Master control module */ +struct jpeg_comp_master { + JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo)); + JMETHOD(void, pass_startup, (j_compress_ptr cinfo)); + JMETHOD(void, finish_pass, (j_compress_ptr cinfo)); + + /* State variables made visible to other modules */ + boolean call_pass_startup; /* True if pass_startup must be called */ + boolean is_last_pass; /* True during last pass */ +}; + +/* Main buffer control (downsampled-data buffer) */ +struct jpeg_c_main_controller { + JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode)); + JMETHOD(void, process_data, (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, + JDIMENSION in_rows_avail)); +}; + +/* Compression preprocessing (downsampling input buffer control) */ +struct jpeg_c_prep_controller { + JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode)); + JMETHOD(void, pre_process_data, (j_compress_ptr cinfo, + JSAMPARRAY input_buf, + JDIMENSION *in_row_ctr, + JDIMENSION in_rows_avail, + JSAMPIMAGE output_buf, + JDIMENSION *out_row_group_ctr, + JDIMENSION out_row_groups_avail)); +}; + +/* Coefficient buffer control */ +struct jpeg_c_coef_controller { + JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode)); + JMETHOD(boolean, compress_data, (j_compress_ptr cinfo, + JSAMPIMAGE input_buf)); +}; + +/* Colorspace conversion */ +struct jpeg_color_converter { + JMETHOD(void, start_pass, (j_compress_ptr cinfo)); + JMETHOD(void, color_convert, (j_compress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, + JDIMENSION output_row, int num_rows)); +}; + +/* Downsampling */ +struct jpeg_downsampler { + JMETHOD(void, start_pass, (j_compress_ptr cinfo)); + JMETHOD(void, downsample, (j_compress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION in_row_index, + JSAMPIMAGE output_buf, + JDIMENSION out_row_group_index)); + + boolean need_context_rows; /* TRUE if need rows above & below */ +}; + +/* Forward DCT (also controls coefficient quantization) */ +typedef JMETHOD(void, forward_DCT_ptr, + (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY sample_data, JBLOCKROW coef_blocks, + JDIMENSION start_row, JDIMENSION start_col, + JDIMENSION num_blocks)); + +struct jpeg_forward_dct { + JMETHOD(void, start_pass, (j_compress_ptr cinfo)); + /* It is useful to allow each component to have a separate FDCT method. */ + forward_DCT_ptr forward_DCT[MAX_COMPONENTS]; +}; + +/* Entropy encoding */ +struct jpeg_entropy_encoder { + JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics)); + JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data)); + JMETHOD(void, finish_pass, (j_compress_ptr cinfo)); +}; + +/* Marker writing */ +struct jpeg_marker_writer { + JMETHOD(void, write_file_header, (j_compress_ptr cinfo)); + JMETHOD(void, write_frame_header, (j_compress_ptr cinfo)); + JMETHOD(void, write_scan_header, (j_compress_ptr cinfo)); + JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo)); + JMETHOD(void, write_tables_only, (j_compress_ptr cinfo)); + /* These routines are exported to allow insertion of extra markers */ + /* Probably only COM and APPn markers should be written this way */ + JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker, + unsigned int datalen)); + JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val)); +}; + + +/* Declarations for decompression modules */ + +/* Master control module */ +struct jpeg_decomp_master { + JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo)); + JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo)); + + /* State variables made visible to other modules */ + boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */ +}; + +/* Input control module */ +struct jpeg_input_controller { + JMETHOD(int, consume_input, (j_decompress_ptr cinfo)); + JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo)); + JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo)); + JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo)); + + /* State variables made visible to other modules */ + boolean has_multiple_scans; /* True if file has multiple scans */ + boolean eoi_reached; /* True when EOI has been consumed */ +}; + +/* Main buffer control (downsampled-data buffer) */ +struct jpeg_d_main_controller { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)); + JMETHOD(void, process_data, (j_decompress_ptr cinfo, + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail)); +}; + +/* Coefficient buffer control */ +struct jpeg_d_coef_controller { + JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo)); + JMETHOD(int, consume_data, (j_decompress_ptr cinfo)); + JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo)); + JMETHOD(int, decompress_data, (j_decompress_ptr cinfo, + JSAMPIMAGE output_buf)); + /* Pointer to array of coefficient virtual arrays, or NULL if none */ + jvirt_barray_ptr *coef_arrays; +}; + +/* Decompression postprocessing (color quantization buffer control) */ +struct jpeg_d_post_controller { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)); + JMETHOD(void, post_process_data, (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, + JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, + JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail)); +}; + +/* Marker reading & parsing */ +struct jpeg_marker_reader { + JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo)); + /* Read markers until SOS or EOI. + * Returns same codes as are defined for jpeg_consume_input: + * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. + */ + JMETHOD(int, read_markers, (j_decompress_ptr cinfo)); + /* Read a restart marker --- exported for use by entropy decoder only */ + jpeg_marker_parser_method read_restart_marker; + + /* State of marker reader --- nominally internal, but applications + * supplying COM or APPn handlers might like to know the state. + */ + boolean saw_SOI; /* found SOI? */ + boolean saw_SOF; /* found SOF? */ + int next_restart_num; /* next restart number expected (0-7) */ + unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */ +}; + +/* Entropy decoding */ +struct jpeg_entropy_decoder { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); + JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo, + JBLOCKROW *MCU_data)); +}; + +/* Inverse DCT (also performs dequantization) */ +typedef JMETHOD(void, inverse_DCT_method_ptr, + (j_decompress_ptr cinfo, jpeg_component_info * compptr, + JCOEFPTR coef_block, + JSAMPARRAY output_buf, JDIMENSION output_col)); + +struct jpeg_inverse_dct { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); + /* It is useful to allow each component to have a separate IDCT method. */ + inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS]; +}; + +/* Upsampling (note that upsampler must also call color converter) */ +struct jpeg_upsampler { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); + JMETHOD(void, upsample, (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, + JDIMENSION *in_row_group_ctr, + JDIMENSION in_row_groups_avail, + JSAMPARRAY output_buf, + JDIMENSION *out_row_ctr, + JDIMENSION out_rows_avail)); + + boolean need_context_rows; /* TRUE if need rows above & below */ +}; + +/* Colorspace conversion */ +struct jpeg_color_deconverter { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); + JMETHOD(void, color_convert, (j_decompress_ptr cinfo, + JSAMPIMAGE input_buf, JDIMENSION input_row, + JSAMPARRAY output_buf, int num_rows)); +}; + +/* Color quantization or color precision reduction */ +struct jpeg_color_quantizer { + JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan)); + JMETHOD(void, color_quantize, (j_decompress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPARRAY output_buf, + int num_rows)); + JMETHOD(void, finish_pass, (j_decompress_ptr cinfo)); + JMETHOD(void, new_color_map, (j_decompress_ptr cinfo)); +}; + + +/* Miscellaneous useful macros */ + +#undef MAX +#define MAX(a,b) ((a) > (b) ? (a) : (b)) +#undef MIN +#define MIN(a,b) ((a) < (b) ? (a) : (b)) + + +/* We assume that right shift corresponds to signed division by 2 with + * rounding towards minus infinity. This is correct for typical "arithmetic + * shift" instructions that shift in copies of the sign bit. But some + * C compilers implement >> with an unsigned shift. For these machines you + * must define RIGHT_SHIFT_IS_UNSIGNED. + * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity. + * It is only applied with constant shift counts. SHIFT_TEMPS must be + * included in the variables of any routine using RIGHT_SHIFT. + */ + +#ifdef RIGHT_SHIFT_IS_UNSIGNED +#define SHIFT_TEMPS INT32 shift_temp; +#define RIGHT_SHIFT(x,shft) \ + ((shift_temp = (x)) < 0 ? \ + (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \ + (shift_temp >> (shft))) +#else +#define SHIFT_TEMPS +#define RIGHT_SHIFT(x,shft) ((x) >> (shft)) +#endif + + +/* Short forms of external names for systems with brain-damaged linkers. */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jinit_compress_master jICompress +#define jinit_c_master_control jICMaster +#define jinit_c_main_controller jICMainC +#define jinit_c_prep_controller jICPrepC +#define jinit_c_coef_controller jICCoefC +#define jinit_color_converter jICColor +#define jinit_downsampler jIDownsampler +#define jinit_forward_dct jIFDCT +#define jinit_huff_encoder jIHEncoder +#define jinit_arith_encoder jIAEncoder +#define jinit_marker_writer jIMWriter +#define jinit_master_decompress jIDMaster +#define jinit_d_main_controller jIDMainC +#define jinit_d_coef_controller jIDCoefC +#define jinit_d_post_controller jIDPostC +#define jinit_input_controller jIInCtlr +#define jinit_marker_reader jIMReader +#define jinit_huff_decoder jIHDecoder +#define jinit_arith_decoder jIADecoder +#define jinit_inverse_dct jIIDCT +#define jinit_upsampler jIUpsampler +#define jinit_color_deconverter jIDColor +#define jinit_1pass_quantizer jI1Quant +#define jinit_2pass_quantizer jI2Quant +#define jinit_merged_upsampler jIMUpsampler +#define jinit_memory_mgr jIMemMgr +#define jdiv_round_up jDivRound +#define jround_up jRound +#define jzero_far jZeroFar +#define jcopy_sample_rows jCopySamples +#define jcopy_block_row jCopyBlocks +#define jpeg_zigzag_order jZIGTable +#define jpeg_natural_order jZAGTable +#define jpeg_natural_order7 jZAG7Table +#define jpeg_natural_order6 jZAG6Table +#define jpeg_natural_order5 jZAG5Table +#define jpeg_natural_order4 jZAG4Table +#define jpeg_natural_order3 jZAG3Table +#define jpeg_natural_order2 jZAG2Table +#define jpeg_aritab jAriTab +#endif /* NEED_SHORT_EXTERNAL_NAMES */ + + +/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays + * and coefficient-block arrays. This won't work on 80x86 because the arrays + * are FAR and we're assuming a small-pointer memory model. However, some + * DOS compilers provide far-pointer versions of memcpy() and memset() even + * in the small-model libraries. These will be used if USE_FMEM is defined. + * Otherwise, the routines in jutils.c do it the hard way. + */ + +#ifndef NEED_FAR_POINTERS /* normal case, same as regular macro */ +#define FMEMZERO(target,size) MEMZERO(target,size) +#else /* 80x86 case */ +#ifdef USE_FMEM +#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size)) +#else +EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero)); +#define FMEMZERO(target,size) jzero_far(target, size) +#endif +#endif + + +/* Compression module initialization routines */ +EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo)); +EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo, + boolean transcode_only)); +EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo, + boolean need_full_buffer)); +EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo, + boolean need_full_buffer)); +EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo, + boolean need_full_buffer)); +EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo)); +EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo)); +EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo)); +EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo)); +EXTERN(void) jinit_arith_encoder JPP((j_compress_ptr cinfo)); +EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo)); +/* Decompression module initialization routines */ +EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo, + boolean need_full_buffer)); +EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo, + boolean need_full_buffer)); +EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo, + boolean need_full_buffer)); +EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_arith_decoder JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo)); +EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo)); +/* Memory manager initialization */ +EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo)); + +/* Utility routines in jutils.c */ +EXTERN(long) jdiv_round_up JPP((long a, long b)); +EXTERN(long) jround_up JPP((long a, long b)); +EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row, + JSAMPARRAY output_array, int dest_row, + int num_rows, JDIMENSION num_cols)); +EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row, + JDIMENSION num_blocks)); +/* Constant tables in jutils.c */ +#if 0 /* This table is not actually needed in v6a */ +extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */ +#endif +extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */ +extern const int jpeg_natural_order7[]; /* zz to natural order for 7x7 block */ +extern const int jpeg_natural_order6[]; /* zz to natural order for 6x6 block */ +extern const int jpeg_natural_order5[]; /* zz to natural order for 5x5 block */ +extern const int jpeg_natural_order4[]; /* zz to natural order for 4x4 block */ +extern const int jpeg_natural_order3[]; /* zz to natural order for 3x3 block */ +extern const int jpeg_natural_order2[]; /* zz to natural order for 2x2 block */ + +/* Arithmetic coding probability estimation tables in jaricom.c */ +extern const INT32 jpeg_aritab[]; + +/* Suppress undefined-structure complaints if necessary. */ + +#ifdef INCOMPLETE_TYPES_BROKEN +#ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */ +struct jvirt_sarray_control { long dummy; }; +struct jvirt_barray_control { long dummy; }; +#endif +#endif /* INCOMPLETE_TYPES_BROKEN */ diff --git a/crypto777/jpeg/jpeglib.h b/crypto777/jpeg/jpeglib.h new file mode 100644 index 000000000..1327cffa9 --- /dev/null +++ b/crypto777/jpeg/jpeglib.h @@ -0,0 +1,1160 @@ +/* + * jpeglib.h + * + * Copyright (C) 1991-1998, Thomas G. Lane. + * Modified 2002-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file defines the application interface for the JPEG library. + * Most applications using the library need only include this file, + * and perhaps jerror.h if they want to know the exact error codes. + */ + +#ifndef JPEGLIB_H +#define JPEGLIB_H + +/* + * First we include the configuration files that record how this + * installation of the JPEG library is set up. jconfig.h can be + * generated automatically for many systems. jmorecfg.h contains + * manual configuration options that most people need not worry about. + */ + +#ifndef JCONFIG_INCLUDED /* in case jinclude.h already did */ +#include "jconfig.h" /* widely used configuration options */ +#endif +#include "jmorecfg.h" /* seldom changed options */ + + +#ifdef __cplusplus +#ifndef DONT_USE_EXTERN_C +extern "C" { +#endif +#endif + +/* Version IDs for the JPEG library. + * Might be useful for tests like "#if JPEG_LIB_VERSION >= 80". + */ + +#define JPEG_LIB_VERSION 80 /* Compatibility version 8.0 */ +#define JPEG_LIB_VERSION_MAJOR 8 +#define JPEG_LIB_VERSION_MINOR 4 + + +/* Various constants determining the sizes of things. + * All of these are specified by the JPEG standard, so don't change them + * if you want to be compatible. + */ + +#define DCTSIZE 8 /* The basic DCT block is 8x8 coefficients */ +#define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */ +#define NUM_QUANT_TBLS 4 /* Quantization tables are numbered 0..3 */ +#define NUM_HUFF_TBLS 4 /* Huffman tables are numbered 0..3 */ +#define NUM_ARITH_TBLS 16 /* Arith-coding tables are numbered 0..15 */ +#define MAX_COMPS_IN_SCAN 4 /* JPEG limit on # of components in one scan */ +#define MAX_SAMP_FACTOR 4 /* JPEG limit on sampling factors */ +/* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard; + * the PostScript DCT filter can emit files with many more than 10 blocks/MCU. + * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU + * to handle it. We even let you do this from the jconfig.h file. However, + * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe + * sometimes emits noncompliant files doesn't mean you should too. + */ +#define C_MAX_BLOCKS_IN_MCU 10 /* compressor's limit on blocks per MCU */ +#ifndef D_MAX_BLOCKS_IN_MCU +#define D_MAX_BLOCKS_IN_MCU 10 /* decompressor's limit on blocks per MCU */ +#endif + + +/* Data structures for images (arrays of samples and of DCT coefficients). + * On 80x86 machines, the image arrays are too big for near pointers, + * but the pointer arrays can fit in near memory. + */ + +typedef JSAMPLE FAR *JSAMPROW; /* ptr to one image row of pixel samples. */ +typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */ +typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */ + +typedef JCOEF JBLOCK[DCTSIZE2]; /* one block of coefficients */ +typedef JBLOCK FAR *JBLOCKROW; /* pointer to one row of coefficient blocks */ +typedef JBLOCKROW *JBLOCKARRAY; /* a 2-D array of coefficient blocks */ +typedef JBLOCKARRAY *JBLOCKIMAGE; /* a 3-D array of coefficient blocks */ + +typedef JCOEF FAR *JCOEFPTR; /* useful in a couple of places */ + + +/* Types for JPEG compression parameters and working tables. */ + + +/* DCT coefficient quantization tables. */ + +typedef struct { + /* This array gives the coefficient quantizers in natural array order + * (not the zigzag order in which they are stored in a JPEG DQT marker). + * CAUTION: IJG versions prior to v6a kept this array in zigzag order. + */ + UINT16 quantval[DCTSIZE2]; /* quantization step for each coefficient */ + /* This field is used only during compression. It's initialized FALSE when + * the table is created, and set TRUE when it's been output to the file. + * You could suppress output of a table by setting this to TRUE. + * (See jpeg_suppress_tables for an example.) + */ + boolean sent_table; /* TRUE when table has been output */ +} JQUANT_TBL; + + +/* Huffman coding tables. */ + +typedef struct { + /* These two fields directly represent the contents of a JPEG DHT marker */ + UINT8 bits[17]; /* bits[k] = # of symbols with codes of */ + /* length k bits; bits[0] is unused */ + UINT8 huffval[256]; /* The symbols, in order of incr code length */ + /* This field is used only during compression. It's initialized FALSE when + * the table is created, and set TRUE when it's been output to the file. + * You could suppress output of a table by setting this to TRUE. + * (See jpeg_suppress_tables for an example.) + */ + boolean sent_table; /* TRUE when table has been output */ +} JHUFF_TBL; + + +/* Basic info about one component (color channel). */ + +typedef struct { + /* These values are fixed over the whole image. */ + /* For compression, they must be supplied by parameter setup; */ + /* for decompression, they are read from the SOF marker. */ + int component_id; /* identifier for this component (0..255) */ + int component_index; /* its index in SOF or cinfo->comp_info[] */ + int h_samp_factor; /* horizontal sampling factor (1..4) */ + int v_samp_factor; /* vertical sampling factor (1..4) */ + int quant_tbl_no; /* quantization table selector (0..3) */ + /* These values may vary between scans. */ + /* For compression, they must be supplied by parameter setup; */ + /* for decompression, they are read from the SOS marker. */ + /* The decompressor output side may not use these variables. */ + int dc_tbl_no; /* DC entropy table selector (0..3) */ + int ac_tbl_no; /* AC entropy table selector (0..3) */ + + /* Remaining fields should be treated as private by applications. */ + + /* These values are computed during compression or decompression startup: */ + /* Component's size in DCT blocks. + * Any dummy blocks added to complete an MCU are not counted; therefore + * these values do not depend on whether a scan is interleaved or not. + */ + JDIMENSION width_in_blocks; + JDIMENSION height_in_blocks; + /* Size of a DCT block in samples, + * reflecting any scaling we choose to apply during the DCT step. + * Values from 1 to 16 are supported. + * Note that different components may receive different DCT scalings. + */ + int DCT_h_scaled_size; + int DCT_v_scaled_size; + /* The downsampled dimensions are the component's actual, unpadded number + * of samples at the main buffer (preprocessing/compression interface); + * DCT scaling is included, so + * downsampled_width = ceil(image_width * Hi/Hmax * DCT_h_scaled_size/DCTSIZE) + * and similarly for height. + */ + JDIMENSION downsampled_width; /* actual width in samples */ + JDIMENSION downsampled_height; /* actual height in samples */ + /* This flag is used only for decompression. In cases where some of the + * components will be ignored (eg grayscale output from YCbCr image), + * we can skip most computations for the unused components. + */ + boolean component_needed; /* do we need the value of this component? */ + + /* These values are computed before starting a scan of the component. */ + /* The decompressor output side may not use these variables. */ + int MCU_width; /* number of blocks per MCU, horizontally */ + int MCU_height; /* number of blocks per MCU, vertically */ + int MCU_blocks; /* MCU_width * MCU_height */ + int MCU_sample_width; /* MCU width in samples: MCU_width * DCT_h_scaled_size */ + int last_col_width; /* # of non-dummy blocks across in last MCU */ + int last_row_height; /* # of non-dummy blocks down in last MCU */ + + /* Saved quantization table for component; NULL if none yet saved. + * See jdinput.c comments about the need for this information. + * This field is currently used only for decompression. + */ + JQUANT_TBL * quant_table; + + /* Private per-component storage for DCT or IDCT subsystem. */ + void * dct_table; +} jpeg_component_info; + + +/* The script for encoding a multiple-scan file is an array of these: */ + +typedef struct { + int comps_in_scan; /* number of components encoded in this scan */ + int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */ + int Ss, Se; /* progressive JPEG spectral selection parms */ + int Ah, Al; /* progressive JPEG successive approx. parms */ +} jpeg_scan_info; + +/* The decompressor can save APPn and COM markers in a list of these: */ + +typedef struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr; + +struct jpeg_marker_struct { + jpeg_saved_marker_ptr next; /* next in list, or NULL */ + UINT8 marker; /* marker code: JPEG_COM, or JPEG_APP0+n */ + unsigned int original_length; /* # bytes of data in the file */ + unsigned int data_length; /* # bytes of data saved at data[] */ + JOCTET FAR * data; /* the data contained in the marker */ + /* the marker length word is not counted in data_length or original_length */ +}; + +/* Known color spaces. */ + +typedef enum { + JCS_UNKNOWN, /* error/unspecified */ + JCS_GRAYSCALE, /* monochrome */ + JCS_RGB, /* red/green/blue */ + JCS_YCbCr, /* Y/Cb/Cr (also known as YUV) */ + JCS_CMYK, /* C/M/Y/K */ + JCS_YCCK /* Y/Cb/Cr/K */ +} J_COLOR_SPACE; + +/* DCT/IDCT algorithm options. */ + +typedef enum { + JDCT_ISLOW, /* slow but accurate integer algorithm */ + JDCT_IFAST, /* faster, less accurate integer method */ + JDCT_FLOAT /* floating-point: accurate, fast on fast HW */ +} J_DCT_METHOD; + +#ifndef JDCT_DEFAULT /* may be overridden in jconfig.h */ +#define JDCT_DEFAULT JDCT_ISLOW +#endif +#ifndef JDCT_FASTEST /* may be overridden in jconfig.h */ +#define JDCT_FASTEST JDCT_IFAST +#endif + +/* Dithering options for decompression. */ + +typedef enum { + JDITHER_NONE, /* no dithering */ + JDITHER_ORDERED, /* simple ordered dither */ + JDITHER_FS /* Floyd-Steinberg error diffusion dither */ +} J_DITHER_MODE; + + +/* Common fields between JPEG compression and decompression master structs. */ + +#define jpeg_common_fields \ + struct jpeg_error_mgr * err; /* Error handler module */\ + struct jpeg_memory_mgr * mem; /* Memory manager module */\ + struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\ + void * client_data; /* Available for use by application */\ + boolean is_decompressor; /* So common code can tell which is which */\ + int global_state /* For checking call sequence validity */ + +/* Routines that are to be used by both halves of the library are declared + * to receive a pointer to this structure. There are no actual instances of + * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct. + */ +struct jpeg_common_struct { + jpeg_common_fields; /* Fields common to both master struct types */ + /* Additional fields follow in an actual jpeg_compress_struct or + * jpeg_decompress_struct. All three structs must agree on these + * initial fields! (This would be a lot cleaner in C++.) + */ +}; + +typedef struct jpeg_common_struct * j_common_ptr; +typedef struct jpeg_compress_struct * j_compress_ptr; +typedef struct jpeg_decompress_struct * j_decompress_ptr; + + +/* Master record for a compression instance */ + +struct jpeg_compress_struct { + jpeg_common_fields; /* Fields shared with jpeg_decompress_struct */ + + /* Destination for compressed data */ + struct jpeg_destination_mgr * dest; + + /* Description of source image --- these fields must be filled in by + * outer application before starting compression. in_color_space must + * be correct before you can even call jpeg_set_defaults(). + */ + + JDIMENSION image_width; /* input image width */ + JDIMENSION image_height; /* input image height */ + int input_components; /* # of color components in input image */ + J_COLOR_SPACE in_color_space; /* colorspace of input image */ + + double input_gamma; /* image gamma of input image */ + + /* Compression parameters --- these fields must be set before calling + * jpeg_start_compress(). We recommend calling jpeg_set_defaults() to + * initialize everything to reasonable defaults, then changing anything + * the application specifically wants to change. That way you won't get + * burnt when new parameters are added. Also note that there are several + * helper routines to simplify changing parameters. + */ + + unsigned int scale_num, scale_denom; /* fraction by which to scale image */ + + JDIMENSION jpeg_width; /* scaled JPEG image width */ + JDIMENSION jpeg_height; /* scaled JPEG image height */ + /* Dimensions of actual JPEG image that will be written to file, + * derived from input dimensions by scaling factors above. + * These fields are computed by jpeg_start_compress(). + * You can also use jpeg_calc_jpeg_dimensions() to determine these values + * in advance of calling jpeg_start_compress(). + */ + + int data_precision; /* bits of precision in image data */ + + int num_components; /* # of color components in JPEG image */ + J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */ + + jpeg_component_info * comp_info; + /* comp_info[i] describes component that appears i'th in SOF */ + + JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]; + int q_scale_factor[NUM_QUANT_TBLS]; + /* ptrs to coefficient quantization tables, or NULL if not defined, + * and corresponding scale factors (percentage, initialized 100). + */ + + JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]; + JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]; + /* ptrs to Huffman coding tables, or NULL if not defined */ + + UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */ + UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */ + UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */ + + int num_scans; /* # of entries in scan_info array */ + const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */ + /* The default value of scan_info is NULL, which causes a single-scan + * sequential JPEG file to be emitted. To create a multi-scan file, + * set num_scans and scan_info to point to an array of scan definitions. + */ + + boolean raw_data_in; /* TRUE=caller supplies downsampled data */ + boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */ + boolean optimize_coding; /* TRUE=optimize entropy encoding parms */ + boolean CCIR601_sampling; /* TRUE=first samples are cosited */ + boolean do_fancy_downsampling; /* TRUE=apply fancy downsampling */ + int smoothing_factor; /* 1..100, or 0 for no input smoothing */ + J_DCT_METHOD dct_method; /* DCT algorithm selector */ + + /* The restart interval can be specified in absolute MCUs by setting + * restart_interval, or in MCU rows by setting restart_in_rows + * (in which case the correct restart_interval will be figured + * for each scan). + */ + unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */ + int restart_in_rows; /* if > 0, MCU rows per restart interval */ + + /* Parameters controlling emission of special markers. */ + + boolean write_JFIF_header; /* should a JFIF marker be written? */ + UINT8 JFIF_major_version; /* What to write for the JFIF version number */ + UINT8 JFIF_minor_version; + /* These three values are not used by the JPEG code, merely copied */ + /* into the JFIF APP0 marker. density_unit can be 0 for unknown, */ + /* 1 for dots/inch, or 2 for dots/cm. Note that the pixel aspect */ + /* ratio is defined by X_density/Y_density even when density_unit=0. */ + UINT8 density_unit; /* JFIF code for pixel size units */ + UINT16 X_density; /* Horizontal pixel density */ + UINT16 Y_density; /* Vertical pixel density */ + boolean write_Adobe_marker; /* should an Adobe marker be written? */ + + /* State variable: index of next scanline to be written to + * jpeg_write_scanlines(). Application may use this to control its + * processing loop, e.g., "while (next_scanline < image_height)". + */ + + JDIMENSION next_scanline; /* 0 .. image_height-1 */ + + /* Remaining fields are known throughout compressor, but generally + * should not be touched by a surrounding application. + */ + + /* + * These fields are computed during compression startup + */ + boolean progressive_mode; /* TRUE if scan script uses progressive mode */ + int max_h_samp_factor; /* largest h_samp_factor */ + int max_v_samp_factor; /* largest v_samp_factor */ + + int min_DCT_h_scaled_size; /* smallest DCT_h_scaled_size of any component */ + int min_DCT_v_scaled_size; /* smallest DCT_v_scaled_size of any component */ + + JDIMENSION total_iMCU_rows; /* # of iMCU rows to be input to coef ctlr */ + /* The coefficient controller receives data in units of MCU rows as defined + * for fully interleaved scans (whether the JPEG file is interleaved or not). + * There are v_samp_factor * DCTSIZE sample rows of each component in an + * "iMCU" (interleaved MCU) row. + */ + + /* + * These fields are valid during any one scan. + * They describe the components and MCUs actually appearing in the scan. + */ + int comps_in_scan; /* # of JPEG components in this scan */ + jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]; + /* *cur_comp_info[i] describes component that appears i'th in SOS */ + + JDIMENSION MCUs_per_row; /* # of MCUs across the image */ + JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */ + + int blocks_in_MCU; /* # of DCT blocks per MCU */ + int MCU_membership[C_MAX_BLOCKS_IN_MCU]; + /* MCU_membership[i] is index in cur_comp_info of component owning */ + /* i'th block in an MCU */ + + int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */ + + int block_size; /* the basic DCT block size: 1..16 */ + const int * natural_order; /* natural-order position array */ + int lim_Se; /* min( Se, DCTSIZE2-1 ) */ + + /* + * Links to compression subobjects (methods and private variables of modules) + */ + struct jpeg_comp_master * master; + struct jpeg_c_main_controller * main; + struct jpeg_c_prep_controller * prep; + struct jpeg_c_coef_controller * coef; + struct jpeg_marker_writer * marker; + struct jpeg_color_converter * cconvert; + struct jpeg_downsampler * downsample; + struct jpeg_forward_dct * fdct; + struct jpeg_entropy_encoder * entropy; + jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */ + int script_space_size; +}; + + +/* Master record for a decompression instance */ + +struct jpeg_decompress_struct { + jpeg_common_fields; /* Fields shared with jpeg_compress_struct */ + + /* Source of compressed data */ + struct jpeg_source_mgr * src; + + /* Basic description of image --- filled in by jpeg_read_header(). */ + /* Application may inspect these values to decide how to process image. */ + + JDIMENSION image_width; /* nominal image width (from SOF marker) */ + JDIMENSION image_height; /* nominal image height */ + int num_components; /* # of color components in JPEG image */ + J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */ + + /* Decompression processing parameters --- these fields must be set before + * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes + * them to default values. + */ + + J_COLOR_SPACE out_color_space; /* colorspace for output */ + + unsigned int scale_num, scale_denom; /* fraction by which to scale image */ + + double output_gamma; /* image gamma wanted in output */ + + boolean buffered_image; /* TRUE=multiple output passes */ + boolean raw_data_out; /* TRUE=downsampled data wanted */ + + J_DCT_METHOD dct_method; /* IDCT algorithm selector */ + boolean do_fancy_upsampling; /* TRUE=apply fancy upsampling */ + boolean do_block_smoothing; /* TRUE=apply interblock smoothing */ + + boolean quantize_colors; /* TRUE=colormapped output wanted */ + /* the following are ignored if not quantize_colors: */ + J_DITHER_MODE dither_mode; /* type of color dithering to use */ + boolean two_pass_quantize; /* TRUE=use two-pass color quantization */ + int desired_number_of_colors; /* max # colors to use in created colormap */ + /* these are significant only in buffered-image mode: */ + boolean enable_1pass_quant; /* enable future use of 1-pass quantizer */ + boolean enable_external_quant;/* enable future use of external colormap */ + boolean enable_2pass_quant; /* enable future use of 2-pass quantizer */ + + /* Description of actual output image that will be returned to application. + * These fields are computed by jpeg_start_decompress(). + * You can also use jpeg_calc_output_dimensions() to determine these values + * in advance of calling jpeg_start_decompress(). + */ + + JDIMENSION output_width; /* scaled image width */ + JDIMENSION output_height; /* scaled image height */ + int out_color_components; /* # of color components in out_color_space */ + int output_components; /* # of color components returned */ + /* output_components is 1 (a colormap index) when quantizing colors; + * otherwise it equals out_color_components. + */ + int rec_outbuf_height; /* min recommended height of scanline buffer */ + /* If the buffer passed to jpeg_read_scanlines() is less than this many rows + * high, space and time will be wasted due to unnecessary data copying. + * Usually rec_outbuf_height will be 1 or 2, at most 4. + */ + + /* When quantizing colors, the output colormap is described by these fields. + * The application can supply a colormap by setting colormap non-NULL before + * calling jpeg_start_decompress; otherwise a colormap is created during + * jpeg_start_decompress or jpeg_start_output. + * The map has out_color_components rows and actual_number_of_colors columns. + */ + int actual_number_of_colors; /* number of entries in use */ + JSAMPARRAY colormap; /* The color map as a 2-D pixel array */ + + /* State variables: these variables indicate the progress of decompression. + * The application may examine these but must not modify them. + */ + + /* Row index of next scanline to be read from jpeg_read_scanlines(). + * Application may use this to control its processing loop, e.g., + * "while (output_scanline < output_height)". + */ + JDIMENSION output_scanline; /* 0 .. output_height-1 */ + + /* Current input scan number and number of iMCU rows completed in scan. + * These indicate the progress of the decompressor input side. + */ + int input_scan_number; /* Number of SOS markers seen so far */ + JDIMENSION input_iMCU_row; /* Number of iMCU rows completed */ + + /* The "output scan number" is the notional scan being displayed by the + * output side. The decompressor will not allow output scan/row number + * to get ahead of input scan/row, but it can fall arbitrarily far behind. + */ + int output_scan_number; /* Nominal scan number being displayed */ + JDIMENSION output_iMCU_row; /* Number of iMCU rows read */ + + /* Current progression status. coef_bits[c][i] indicates the precision + * with which component c's DCT coefficient i (in zigzag order) is known. + * It is -1 when no data has yet been received, otherwise it is the point + * transform (shift) value for the most recent scan of the coefficient + * (thus, 0 at completion of the progression). + * This pointer is NULL when reading a non-progressive file. + */ + int (*coef_bits)[DCTSIZE2]; /* -1 or current Al value for each coef */ + + /* Internal JPEG parameters --- the application usually need not look at + * these fields. Note that the decompressor output side may not use + * any parameters that can change between scans. + */ + + /* Quantization and Huffman tables are carried forward across input + * datastreams when processing abbreviated JPEG datastreams. + */ + + JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]; + /* ptrs to coefficient quantization tables, or NULL if not defined */ + + JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]; + JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]; + /* ptrs to Huffman coding tables, or NULL if not defined */ + + /* These parameters are never carried across datastreams, since they + * are given in SOF/SOS markers or defined to be reset by SOI. + */ + + int data_precision; /* bits of precision in image data */ + + jpeg_component_info * comp_info; + /* comp_info[i] describes component that appears i'th in SOF */ + + boolean is_baseline; /* TRUE if Baseline SOF0 encountered */ + boolean progressive_mode; /* TRUE if SOFn specifies progressive mode */ + boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */ + + UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */ + UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */ + UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */ + + unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */ + + /* These fields record data obtained from optional markers recognized by + * the JPEG library. + */ + boolean saw_JFIF_marker; /* TRUE iff a JFIF APP0 marker was found */ + /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */ + UINT8 JFIF_major_version; /* JFIF version number */ + UINT8 JFIF_minor_version; + UINT8 density_unit; /* JFIF code for pixel size units */ + UINT16 X_density; /* Horizontal pixel density */ + UINT16 Y_density; /* Vertical pixel density */ + boolean saw_Adobe_marker; /* TRUE iff an Adobe APP14 marker was found */ + UINT8 Adobe_transform; /* Color transform code from Adobe marker */ + + boolean CCIR601_sampling; /* TRUE=first samples are cosited */ + + /* Aside from the specific data retained from APPn markers known to the + * library, the uninterpreted contents of any or all APPn and COM markers + * can be saved in a list for examination by the application. + */ + jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */ + + /* Remaining fields are known throughout decompressor, but generally + * should not be touched by a surrounding application. + */ + + /* + * These fields are computed during decompression startup + */ + int max_h_samp_factor; /* largest h_samp_factor */ + int max_v_samp_factor; /* largest v_samp_factor */ + + int min_DCT_h_scaled_size; /* smallest DCT_h_scaled_size of any component */ + int min_DCT_v_scaled_size; /* smallest DCT_v_scaled_size of any component */ + + JDIMENSION total_iMCU_rows; /* # of iMCU rows in image */ + /* The coefficient controller's input and output progress is measured in + * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows + * in fully interleaved JPEG scans, but are used whether the scan is + * interleaved or not. We define an iMCU row as v_samp_factor DCT block + * rows of each component. Therefore, the IDCT output contains + * v_samp_factor*DCT_v_scaled_size sample rows of a component per iMCU row. + */ + + JSAMPLE * sample_range_limit; /* table for fast range-limiting */ + + /* + * These fields are valid during any one scan. + * They describe the components and MCUs actually appearing in the scan. + * Note that the decompressor output side must not use these fields. + */ + int comps_in_scan; /* # of JPEG components in this scan */ + jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]; + /* *cur_comp_info[i] describes component that appears i'th in SOS */ + + JDIMENSION MCUs_per_row; /* # of MCUs across the image */ + JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */ + + int blocks_in_MCU; /* # of DCT blocks per MCU */ + int MCU_membership[D_MAX_BLOCKS_IN_MCU]; + /* MCU_membership[i] is index in cur_comp_info of component owning */ + /* i'th block in an MCU */ + + int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */ + + /* These fields are derived from Se of first SOS marker. + */ + int block_size; /* the basic DCT block size: 1..16 */ + const int * natural_order; /* natural-order position array for entropy decode */ + int lim_Se; /* min( Se, DCTSIZE2-1 ) for entropy decode */ + + /* This field is shared between entropy decoder and marker parser. + * It is either zero or the code of a JPEG marker that has been + * read from the data source, but has not yet been processed. + */ + int unread_marker; + + /* + * Links to decompression subobjects (methods, private variables of modules) + */ + struct jpeg_decomp_master * master; + struct jpeg_d_main_controller * main; + struct jpeg_d_coef_controller * coef; + struct jpeg_d_post_controller * post; + struct jpeg_input_controller * inputctl; + struct jpeg_marker_reader * marker; + struct jpeg_entropy_decoder * entropy; + struct jpeg_inverse_dct * idct; + struct jpeg_upsampler * upsample; + struct jpeg_color_deconverter * cconvert; + struct jpeg_color_quantizer * cquantize; +}; + + +/* "Object" declarations for JPEG modules that may be supplied or called + * directly by the surrounding application. + * As with all objects in the JPEG library, these structs only define the + * publicly visible methods and state variables of a module. Additional + * private fields may exist after the public ones. + */ + + +/* Error handler object */ + +struct jpeg_error_mgr { + /* Error exit handler: does not return to caller */ + JMETHOD(void, error_exit, (j_common_ptr cinfo)); + /* Conditionally emit a trace or warning message */ + JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level)); + /* Routine that actually outputs a trace or error message */ + JMETHOD(void, output_message, (j_common_ptr cinfo)); + /* Format a message string for the most recent JPEG error or message */ + JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer)); +#define JMSG_LENGTH_MAX 200 /* recommended size of format_message buffer */ + /* Reset error state variables at start of a new image */ + JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo)); + + /* The message ID code and any parameters are saved here. + * A message can have one string parameter or up to 8 int parameters. + */ + int msg_code; +#define JMSG_STR_PARM_MAX 80 + union { + int i[8]; + char s[JMSG_STR_PARM_MAX]; + } msg_parm; + + /* Standard state variables for error facility */ + + int trace_level; /* max msg_level that will be displayed */ + + /* For recoverable corrupt-data errors, we emit a warning message, + * but keep going unless emit_message chooses to abort. emit_message + * should count warnings in num_warnings. The surrounding application + * can check for bad data by seeing if num_warnings is nonzero at the + * end of processing. + */ + long num_warnings; /* number of corrupt-data warnings */ + + /* These fields point to the table(s) of error message strings. + * An application can change the table pointer to switch to a different + * message list (typically, to change the language in which errors are + * reported). Some applications may wish to add additional error codes + * that will be handled by the JPEG library error mechanism; the second + * table pointer is used for this purpose. + * + * First table includes all errors generated by JPEG library itself. + * Error code 0 is reserved for a "no such error string" message. + */ + const char * const * jpeg_message_table; /* Library errors */ + int last_jpeg_message; /* Table contains strings 0..last_jpeg_message */ + /* Second table can be added by application (see cjpeg/djpeg for example). + * It contains strings numbered first_addon_message..last_addon_message. + */ + const char * const * addon_message_table; /* Non-library errors */ + int first_addon_message; /* code for first string in addon table */ + int last_addon_message; /* code for last string in addon table */ +}; + + +/* Progress monitor object */ + +struct jpeg_progress_mgr { + JMETHOD(void, progress_monitor, (j_common_ptr cinfo)); + + long pass_counter; /* work units completed in this pass */ + long pass_limit; /* total number of work units in this pass */ + int completed_passes; /* passes completed so far */ + int total_passes; /* total number of passes expected */ +}; + + +/* Data destination object for compression */ + +struct jpeg_destination_mgr { + JOCTET * next_output_byte; /* => next byte to write in buffer */ + size_t free_in_buffer; /* # of byte spaces remaining in buffer */ + + JMETHOD(void, init_destination, (j_compress_ptr cinfo)); + JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo)); + JMETHOD(void, term_destination, (j_compress_ptr cinfo)); +}; + + +/* Data source object for decompression */ + +struct jpeg_source_mgr { + const JOCTET * next_input_byte; /* => next byte to read from buffer */ + size_t bytes_in_buffer; /* # of bytes remaining in buffer */ + + JMETHOD(void, init_source, (j_decompress_ptr cinfo)); + JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo)); + JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes)); + JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired)); + JMETHOD(void, term_source, (j_decompress_ptr cinfo)); +}; + + +/* Memory manager object. + * Allocates "small" objects (a few K total), "large" objects (tens of K), + * and "really big" objects (virtual arrays with backing store if needed). + * The memory manager does not allow individual objects to be freed; rather, + * each created object is assigned to a pool, and whole pools can be freed + * at once. This is faster and more convenient than remembering exactly what + * to free, especially where malloc()/free() are not too speedy. + * NB: alloc routines never return NULL. They exit to error_exit if not + * successful. + */ + +#define JPOOL_PERMANENT 0 /* lasts until master record is destroyed */ +#define JPOOL_IMAGE 1 /* lasts until done with image/datastream */ +#define JPOOL_NUMPOOLS 2 + +typedef struct jvirt_sarray_control * jvirt_sarray_ptr; +typedef struct jvirt_barray_control * jvirt_barray_ptr; + + +struct jpeg_memory_mgr { + /* Method pointers */ + JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id, + size_t sizeofobject)); + JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id, + size_t sizeofobject)); + JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id, + JDIMENSION samplesperrow, + JDIMENSION numrows)); + JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id, + JDIMENSION blocksperrow, + JDIMENSION numrows)); + JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo, + int pool_id, + boolean pre_zero, + JDIMENSION samplesperrow, + JDIMENSION numrows, + JDIMENSION maxaccess)); + JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo, + int pool_id, + boolean pre_zero, + JDIMENSION blocksperrow, + JDIMENSION numrows, + JDIMENSION maxaccess)); + JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo)); + JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo, + jvirt_sarray_ptr ptr, + JDIMENSION start_row, + JDIMENSION num_rows, + boolean writable)); + JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo, + jvirt_barray_ptr ptr, + JDIMENSION start_row, + JDIMENSION num_rows, + boolean writable)); + JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id)); + JMETHOD(void, self_destruct, (j_common_ptr cinfo)); + + /* Limit on memory allocation for this JPEG object. (Note that this is + * merely advisory, not a guaranteed maximum; it only affects the space + * used for virtual-array buffers.) May be changed by outer application + * after creating the JPEG object. + */ + long max_memory_to_use; + + /* Maximum allocation request accepted by alloc_large. */ + long max_alloc_chunk; +}; + + +/* Routine signature for application-supplied marker processing methods. + * Need not pass marker code since it is stored in cinfo->unread_marker. + */ +typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo)); + + +/* Declarations for routines called by application. + * The JPP macro hides prototype parameters from compilers that can't cope. + * Note JPP requires double parentheses. + */ + +#ifdef HAVE_PROTOTYPES +#define JPP(arglist) arglist +#else +#define JPP(arglist) () +#endif + + +/* Short forms of external names for systems with brain-damaged linkers. + * We shorten external names to be unique in the first six letters, which + * is good enough for all known systems. + * (If your compiler itself needs names to be unique in less than 15 + * characters, you are out of luck. Get a better compiler.) + */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jpeg_std_error jStdError +#define jpeg_CreateCompress jCreaCompress +#define jpeg_CreateDecompress jCreaDecompress +#define jpeg_destroy_compress jDestCompress +#define jpeg_destroy_decompress jDestDecompress +#define jpeg_stdio_dest jStdDest +#define jpeg_stdio_src jStdSrc +#define jpeg_mem_dest jMemDest +#define jpeg_mem_src jMemSrc +#define jpeg_set_defaults jSetDefaults +#define jpeg_set_colorspace jSetColorspace +#define jpeg_default_colorspace jDefColorspace +#define jpeg_set_quality jSetQuality +#define jpeg_set_linear_quality jSetLQuality +#define jpeg_default_qtables jDefQTables +#define jpeg_add_quant_table jAddQuantTable +#define jpeg_quality_scaling jQualityScaling +#define jpeg_simple_progression jSimProgress +#define jpeg_suppress_tables jSuppressTables +#define jpeg_alloc_quant_table jAlcQTable +#define jpeg_alloc_huff_table jAlcHTable +#define jpeg_start_compress jStrtCompress +#define jpeg_write_scanlines jWrtScanlines +#define jpeg_finish_compress jFinCompress +#define jpeg_calc_jpeg_dimensions jCjpegDimensions +#define jpeg_write_raw_data jWrtRawData +#define jpeg_write_marker jWrtMarker +#define jpeg_write_m_header jWrtMHeader +#define jpeg_write_m_byte jWrtMByte +#define jpeg_write_tables jWrtTables +#define jpeg_read_header jReadHeader +#define jpeg_start_decompress jStrtDecompress +#define jpeg_read_scanlines jReadScanlines +#define jpeg_finish_decompress jFinDecompress +#define jpeg_read_raw_data jReadRawData +#define jpeg_has_multiple_scans jHasMultScn +#define jpeg_start_output jStrtOutput +#define jpeg_finish_output jFinOutput +#define jpeg_input_complete jInComplete +#define jpeg_new_colormap jNewCMap +#define jpeg_consume_input jConsumeInput +#define jpeg_core_output_dimensions jCoreDimensions +#define jpeg_calc_output_dimensions jCalcDimensions +#define jpeg_save_markers jSaveMarkers +#define jpeg_set_marker_processor jSetMarker +#define jpeg_read_coefficients jReadCoefs +#define jpeg_write_coefficients jWrtCoefs +#define jpeg_copy_critical_parameters jCopyCrit +#define jpeg_abort_compress jAbrtCompress +#define jpeg_abort_decompress jAbrtDecompress +#define jpeg_abort jAbort +#define jpeg_destroy jDestroy +#define jpeg_resync_to_restart jResyncRestart +#endif /* NEED_SHORT_EXTERNAL_NAMES */ + + +/* Default error-management setup */ +EXTERN(struct jpeg_error_mgr *) jpeg_std_error + JPP((struct jpeg_error_mgr * err)); + +/* Initialization of JPEG compression objects. + * jpeg_create_compress() and jpeg_create_decompress() are the exported + * names that applications should call. These expand to calls on + * jpeg_CreateCompress and jpeg_CreateDecompress with additional information + * passed for version mismatch checking. + * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx. + */ +#define jpeg_create_compress(cinfo) \ + jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \ + (size_t) sizeof(struct jpeg_compress_struct)) +#define jpeg_create_decompress(cinfo) \ + jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \ + (size_t) sizeof(struct jpeg_decompress_struct)) +EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo, + int version, size_t structsize)); +EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo, + int version, size_t structsize)); +/* Destruction of JPEG compression objects */ +EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo)); +EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo)); + +/* Standard data source and destination managers: stdio streams. */ +/* Caller is responsible for opening the file before and closing after. */ +EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile)); +EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile)); + +/* Data source and destination managers: memory buffers. */ +EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo, + unsigned char ** outbuffer, + unsigned long * outsize)); +EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo, + unsigned char * inbuffer, + unsigned long insize)); + +/* Default parameter setup for compression */ +EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo)); +/* Compression parameter setup aids */ +EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo, + J_COLOR_SPACE colorspace)); +EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo)); +EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality, + boolean force_baseline)); +EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo, + int scale_factor, + boolean force_baseline)); +EXTERN(void) jpeg_default_qtables JPP((j_compress_ptr cinfo, + boolean force_baseline)); +EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl, + const unsigned int *basic_table, + int scale_factor, + boolean force_baseline)); +EXTERN(int) jpeg_quality_scaling JPP((int quality)); +EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo)); +EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo, + boolean suppress)); +EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo)); +EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo)); + +/* Main entry points for compression */ +EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo, + boolean write_all_tables)); +EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo, + JSAMPARRAY scanlines, + JDIMENSION num_lines)); +EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo)); + +/* Precalculate JPEG dimensions for current compression parameters. */ +EXTERN(void) jpeg_calc_jpeg_dimensions JPP((j_compress_ptr cinfo)); + +/* Replaces jpeg_write_scanlines when writing raw downsampled data. */ +EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo, + JSAMPIMAGE data, + JDIMENSION num_lines)); + +/* Write a special marker. See libjpeg.txt concerning safe usage. */ +EXTERN(void) jpeg_write_marker + JPP((j_compress_ptr cinfo, int marker, + const JOCTET * dataptr, unsigned int datalen)); +/* Same, but piecemeal. */ +EXTERN(void) jpeg_write_m_header + JPP((j_compress_ptr cinfo, int marker, unsigned int datalen)); +EXTERN(void) jpeg_write_m_byte + JPP((j_compress_ptr cinfo, int val)); + +/* Alternate compression function: just write an abbreviated table file */ +EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo)); + +/* Decompression startup: read start of JPEG datastream to see what's there */ +EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo, + boolean require_image)); +/* Return value is one of: */ +#define JPEG_SUSPENDED 0 /* Suspended due to lack of input data */ +#define JPEG_HEADER_OK 1 /* Found valid image datastream */ +#define JPEG_HEADER_TABLES_ONLY 2 /* Found valid table-specs-only datastream */ +/* If you pass require_image = TRUE (normal case), you need not check for + * a TABLES_ONLY return code; an abbreviated file will cause an error exit. + * JPEG_SUSPENDED is only possible if you use a data source module that can + * give a suspension return (the stdio source module doesn't). + */ + +/* Main entry points for decompression */ +EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo)); +EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo, + JSAMPARRAY scanlines, + JDIMENSION max_lines)); +EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo)); + +/* Replaces jpeg_read_scanlines when reading raw downsampled data. */ +EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo, + JSAMPIMAGE data, + JDIMENSION max_lines)); + +/* Additional entry points for buffered-image mode. */ +EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo)); +EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo, + int scan_number)); +EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo)); +EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo)); +EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo)); +EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo)); +/* Return value is one of: */ +/* #define JPEG_SUSPENDED 0 Suspended due to lack of input data */ +#define JPEG_REACHED_SOS 1 /* Reached start of new scan */ +#define JPEG_REACHED_EOI 2 /* Reached end of image */ +#define JPEG_ROW_COMPLETED 3 /* Completed one iMCU row */ +#define JPEG_SCAN_COMPLETED 4 /* Completed last iMCU row of a scan */ + +/* Precalculate output dimensions for current decompression parameters. */ +EXTERN(void) jpeg_core_output_dimensions JPP((j_decompress_ptr cinfo)); +EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo)); + +/* Control saving of COM and APPn markers into marker_list. */ +EXTERN(void) jpeg_save_markers + JPP((j_decompress_ptr cinfo, int marker_code, + unsigned int length_limit)); + +/* Install a special processing method for COM or APPn markers. */ +EXTERN(void) jpeg_set_marker_processor + JPP((j_decompress_ptr cinfo, int marker_code, + jpeg_marker_parser_method routine)); + +/* Read or write raw DCT coefficients --- useful for lossless transcoding. */ +EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo)); +EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo, + jvirt_barray_ptr * coef_arrays)); +EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo, + j_compress_ptr dstinfo)); + +/* If you choose to abort compression or decompression before completing + * jpeg_finish_(de)compress, then you need to clean up to release memory, + * temporary files, etc. You can just call jpeg_destroy_(de)compress + * if you're done with the JPEG object, but if you want to clean it up and + * reuse it, call this: + */ +EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo)); +EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo)); + +/* Generic versions of jpeg_abort and jpeg_destroy that work on either + * flavor of JPEG object. These may be more convenient in some places. + */ +EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo)); +EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo)); + +/* Default restart-marker-resync procedure for use by data source modules */ +EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo, + int desired)); + + +/* These marker codes are exported since applications and data source modules + * are likely to want to use them. + */ + +#define JPEG_RST0 0xD0 /* RST0 marker code */ +#define JPEG_EOI 0xD9 /* EOI marker code */ +#define JPEG_APP0 0xE0 /* APP0 marker code */ +#define JPEG_COM 0xFE /* COM marker code */ + + +/* If we have a brain-damaged compiler that emits warnings (or worse, errors) + * for structure definitions that are never filled in, keep it quiet by + * supplying dummy definitions for the various substructures. + */ + +#ifdef INCOMPLETE_TYPES_BROKEN +#ifndef JPEG_INTERNALS /* will be defined in jpegint.h */ +struct jvirt_sarray_control { long dummy; }; +struct jvirt_barray_control { long dummy; }; +struct jpeg_comp_master { long dummy; }; +struct jpeg_c_main_controller { long dummy; }; +struct jpeg_c_prep_controller { long dummy; }; +struct jpeg_c_coef_controller { long dummy; }; +struct jpeg_marker_writer { long dummy; }; +struct jpeg_color_converter { long dummy; }; +struct jpeg_downsampler { long dummy; }; +struct jpeg_forward_dct { long dummy; }; +struct jpeg_entropy_encoder { long dummy; }; +struct jpeg_decomp_master { long dummy; }; +struct jpeg_d_main_controller { long dummy; }; +struct jpeg_d_coef_controller { long dummy; }; +struct jpeg_d_post_controller { long dummy; }; +struct jpeg_input_controller { long dummy; }; +struct jpeg_marker_reader { long dummy; }; +struct jpeg_entropy_decoder { long dummy; }; +struct jpeg_inverse_dct { long dummy; }; +struct jpeg_upsampler { long dummy; }; +struct jpeg_color_deconverter { long dummy; }; +struct jpeg_color_quantizer { long dummy; }; +#endif /* JPEG_INTERNALS */ +#endif /* INCOMPLETE_TYPES_BROKEN */ + + +/* + * The JPEG library modules define JPEG_INTERNALS before including this file. + * The internal structure declarations are read only when that is true. + * Applications using the library should not include jpegint.h, but may wish + * to include jerror.h. + */ + +#ifdef JPEG_INTERNALS +#include "jpegint.h" /* fetch private declarations */ +#include "jerror.h" /* fetch error codes too */ +#endif + +#ifdef __cplusplus +#ifndef DONT_USE_EXTERN_C +} +#endif +#endif + +#endif /* JPEGLIB_H */ diff --git a/crypto777/jpeg/jpegtran.c b/crypto777/jpeg/jpegtran.c new file mode 100644 index 000000000..e539e91b8 --- /dev/null +++ b/crypto777/jpeg/jpegtran.c @@ -0,0 +1,560 @@ +/* + * jpegtran.c + * + * Copyright (C) 1995-2011, Thomas G. Lane, Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a command-line user interface for JPEG transcoding. + * It is very similar to cjpeg.c, and partly to djpeg.c, but provides + * lossless transcoding between different JPEG file formats. It also + * provides some lossless and sort-of-lossless transformations of JPEG data. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ +#include "transupp.h" /* Support routines for jpegtran */ +#include "jversion.h" /* for version message */ + +#ifdef USE_CCOMMAND /* command-line reader for Macintosh */ +#ifdef __MWERKS__ +#include /* Metrowerks needs this */ +#include /* ... and this */ +#endif +#ifdef THINK_C +#include /* Think declares it here */ +#endif +#endif + + +/* + * Argument-parsing code. + * The switch parser is designed to be useful with DOS-style command line + * syntax, ie, intermixed switches and file names, where only the switches + * to the left of a given file name affect processing of that file. + * The main program in this file doesn't actually use this capability... + */ + + +static const char * progname; /* program name for error messages */ +static char * outfilename; /* for -outfile switch */ +static char * scaleoption; /* -scale switch */ +static JCOPY_OPTION copyoption; /* -copy switch */ +static jpeg_transform_info transformoption; /* image transformation options */ + + +LOCAL(void) +usage (void) +/* complain about bad command line */ +{ + fprintf(stderr, "usage: %s [switches] ", progname); +#ifdef TWO_FILE_COMMANDLINE + fprintf(stderr, "inputfile outputfile\n"); +#else + fprintf(stderr, "[inputfile]\n"); +#endif + + fprintf(stderr, "Switches (names may be abbreviated):\n"); + fprintf(stderr, " -copy none Copy no extra markers from source file\n"); + fprintf(stderr, " -copy comments Copy only comment markers (default)\n"); + fprintf(stderr, " -copy all Copy all extra markers\n"); +#ifdef ENTROPY_OPT_SUPPORTED + fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n"); +#endif +#ifdef C_PROGRESSIVE_SUPPORTED + fprintf(stderr, " -progressive Create progressive JPEG file\n"); +#endif + fprintf(stderr, "Switches for modifying the image:\n"); +#if TRANSFORMS_SUPPORTED + fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n"); + fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n"); + fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n"); + fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n"); + fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n"); +#endif + fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n"); +#if TRANSFORMS_SUPPORTED + fprintf(stderr, " -transpose Transpose image\n"); + fprintf(stderr, " -transverse Transverse transpose image\n"); + fprintf(stderr, " -trim Drop non-transformable edge blocks\n"); +#endif + fprintf(stderr, "Switches for advanced users:\n"); +#ifdef C_ARITH_CODING_SUPPORTED + fprintf(stderr, " -arithmetic Use arithmetic coding\n"); +#endif + fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); + fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); + fprintf(stderr, " -outfile name Specify name for output file\n"); + fprintf(stderr, " -verbose or -debug Emit debug output\n"); + fprintf(stderr, "Switches for wizards:\n"); +#ifdef C_MULTISCAN_FILES_SUPPORTED + fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n"); +#endif + exit(EXIT_FAILURE); +} + + +LOCAL(void) +select_transform (JXFORM_CODE transform) +/* Silly little routine to detect multiple transform options, + * which we can't handle. + */ +{ +#if TRANSFORMS_SUPPORTED + if (transformoption.transform == JXFORM_NONE || + transformoption.transform == transform) { + transformoption.transform = transform; + } else { + fprintf(stderr, "%s: can only do one image transformation at a time\n", + progname); + usage(); + } +#else + fprintf(stderr, "%s: sorry, image transformation was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif +} + + +LOCAL(int) +parse_switches (j_compress_ptr cinfo, int argc, char **argv, + int last_file_arg_seen, boolean for_real) +/* Parse optional switches. + * Returns argv[] index of first file-name argument (== argc if none). + * Any file names with indexes <= last_file_arg_seen are ignored; + * they have presumably been processed in a previous iteration. + * (Pass 0 for last_file_arg_seen on the first or only iteration.) + * for_real is FALSE on the first (dummy) pass; we may skip any expensive + * processing. + */ +{ + int argn; + char * arg; + boolean simple_progressive; + char * scansarg = NULL; /* saves -scans parm if any */ + + /* Set up default JPEG parameters. */ + simple_progressive = FALSE; + outfilename = NULL; + scaleoption = NULL; + copyoption = JCOPYOPT_DEFAULT; + transformoption.transform = JXFORM_NONE; + transformoption.perfect = FALSE; + transformoption.trim = FALSE; + transformoption.force_grayscale = FALSE; + transformoption.crop = FALSE; + cinfo->err->trace_level = 0; + + /* Scan command line options, adjust parameters */ + + for (argn = 1; argn < argc; argn++) { + arg = argv[argn]; + if (*arg != '-') { + /* Not a switch, must be a file name argument */ + if (argn <= last_file_arg_seen) { + outfilename = NULL; /* -outfile applies to just one input file */ + continue; /* ignore this name if previously processed */ + } + break; /* else done parsing switches */ + } + arg++; /* advance past switch marker character */ + + if (keymatch(arg, "arithmetic", 1)) { + /* Use arithmetic coding. */ +#ifdef C_ARITH_CODING_SUPPORTED + cinfo->arith_code = TRUE; +#else + fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "copy", 2)) { + /* Select which extra markers to copy. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (keymatch(argv[argn], "none", 1)) { + copyoption = JCOPYOPT_NONE; + } else if (keymatch(argv[argn], "comments", 1)) { + copyoption = JCOPYOPT_COMMENTS; + } else if (keymatch(argv[argn], "all", 1)) { + copyoption = JCOPYOPT_ALL; + } else + usage(); + + } else if (keymatch(arg, "crop", 2)) { + /* Perform lossless cropping. */ +#if TRANSFORMS_SUPPORTED + if (++argn >= argc) /* advance to next argument */ + usage(); + if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) { + fprintf(stderr, "%s: bogus -crop argument '%s'\n", + progname, argv[argn]); + exit(EXIT_FAILURE); + } +#else + select_transform(JXFORM_NONE); /* force an error */ +#endif + + } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { + /* Enable debug printouts. */ + /* On first -d, print version identification */ + static boolean printed_version = FALSE; + + if (! printed_version) { + fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n", + JVERSION, JCOPYRIGHT); + printed_version = TRUE; + } + cinfo->err->trace_level++; + + } else if (keymatch(arg, "flip", 1)) { + /* Mirror left-right or top-bottom. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (keymatch(argv[argn], "horizontal", 1)) + select_transform(JXFORM_FLIP_H); + else if (keymatch(argv[argn], "vertical", 1)) + select_transform(JXFORM_FLIP_V); + else + usage(); + + } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) { + /* Force to grayscale. */ +#if TRANSFORMS_SUPPORTED + transformoption.force_grayscale = TRUE; +#else + select_transform(JXFORM_NONE); /* force an error */ +#endif + + } else if (keymatch(arg, "maxmemory", 3)) { + /* Maximum memory in Kb (or Mb with 'm'). */ + long lval; + char ch = 'x'; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) + usage(); + if (ch == 'm' || ch == 'M') + lval *= 1000L; + cinfo->mem->max_memory_to_use = lval * 1000L; + + } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { + /* Enable entropy parm optimization. */ +#ifdef ENTROPY_OPT_SUPPORTED + cinfo->optimize_coding = TRUE; +#else + fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "outfile", 4)) { + /* Set output file name. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + outfilename = argv[argn]; /* save it away for later use */ + + } else if (keymatch(arg, "perfect", 2)) { + /* Fail if there is any partial edge MCUs that the transform can't + * handle. */ + transformoption.perfect = TRUE; + + } else if (keymatch(arg, "progressive", 2)) { + /* Select simple progressive mode. */ +#ifdef C_PROGRESSIVE_SUPPORTED + simple_progressive = TRUE; + /* We must postpone execution until num_components is known. */ +#else + fprintf(stderr, "%s: sorry, progressive output was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "restart", 1)) { + /* Restart interval in MCU rows (or in MCUs with 'b'). */ + long lval; + char ch = 'x'; + + if (++argn >= argc) /* advance to next argument */ + usage(); + if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) + usage(); + if (lval < 0 || lval > 65535L) + usage(); + if (ch == 'b' || ch == 'B') { + cinfo->restart_interval = (unsigned int) lval; + cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ + } else { + cinfo->restart_in_rows = (int) lval; + /* restart_interval will be computed during startup */ + } + + } else if (keymatch(arg, "rotate", 2)) { + /* Rotate 90, 180, or 270 degrees (measured clockwise). */ + if (++argn >= argc) /* advance to next argument */ + usage(); + if (keymatch(argv[argn], "90", 2)) + select_transform(JXFORM_ROT_90); + else if (keymatch(argv[argn], "180", 3)) + select_transform(JXFORM_ROT_180); + else if (keymatch(argv[argn], "270", 3)) + select_transform(JXFORM_ROT_270); + else + usage(); + + } else if (keymatch(arg, "scale", 4)) { + /* Scale the output image by a fraction M/N. */ + if (++argn >= argc) /* advance to next argument */ + usage(); + scaleoption = argv[argn]; + /* We must postpone processing until decompression startup. */ + + } else if (keymatch(arg, "scans", 1)) { + /* Set scan script. */ +#ifdef C_MULTISCAN_FILES_SUPPORTED + if (++argn >= argc) /* advance to next argument */ + usage(); + scansarg = argv[argn]; + /* We must postpone reading the file in case -progressive appears. */ +#else + fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n", + progname); + exit(EXIT_FAILURE); +#endif + + } else if (keymatch(arg, "transpose", 1)) { + /* Transpose (across UL-to-LR axis). */ + select_transform(JXFORM_TRANSPOSE); + + } else if (keymatch(arg, "transverse", 6)) { + /* Transverse transpose (across UR-to-LL axis). */ + select_transform(JXFORM_TRANSVERSE); + + } else if (keymatch(arg, "trim", 3)) { + /* Trim off any partial edge MCUs that the transform can't handle. */ + transformoption.trim = TRUE; + + } else { + usage(); /* bogus switch */ + } + } + + /* Post-switch-scanning cleanup */ + + if (for_real) { + +#ifdef C_PROGRESSIVE_SUPPORTED + if (simple_progressive) /* process -progressive; -scans can override */ + jpeg_simple_progression(cinfo); +#endif + +#ifdef C_MULTISCAN_FILES_SUPPORTED + if (scansarg != NULL) /* process -scans if it was present */ + if (! read_scan_script(cinfo, scansarg)) + usage(); +#endif + } + + return argn; /* return index of next arg (file name) */ +} + + +/* + * The main program. + */ + +int +main (int argc, char **argv) +{ + struct jpeg_decompress_struct srcinfo; + struct jpeg_compress_struct dstinfo; + struct jpeg_error_mgr jsrcerr, jdsterr; +#ifdef PROGRESS_REPORT + struct cdjpeg_progress_mgr progress; +#endif + jvirt_barray_ptr * src_coef_arrays; + jvirt_barray_ptr * dst_coef_arrays; + int file_index; + /* We assume all-in-memory processing and can therefore use only a + * single file pointer for sequential input and output operation. + */ + FILE * fp; + + /* On Mac, fetch a command line. */ +#ifdef USE_CCOMMAND + argc = ccommand(&argv); +#endif + + progname = argv[0]; + if (progname == NULL || progname[0] == 0) + progname = "jpegtran"; /* in case C library doesn't provide it */ + + /* Initialize the JPEG decompression object with default error handling. */ + srcinfo.err = jpeg_std_error(&jsrcerr); + jpeg_create_decompress(&srcinfo); + /* Initialize the JPEG compression object with default error handling. */ + dstinfo.err = jpeg_std_error(&jdsterr); + jpeg_create_compress(&dstinfo); + + /* Now safe to enable signal catcher. + * Note: we assume only the decompression object will have virtual arrays. + */ +#ifdef NEED_SIGNAL_CATCHER + enable_signal_catcher((j_common_ptr) &srcinfo); +#endif + + /* Scan command line to find file names. + * It is convenient to use just one switch-parsing routine, but the switch + * values read here are mostly ignored; we will rescan the switches after + * opening the input file. Also note that most of the switches affect the + * destination JPEG object, so we parse into that and then copy over what + * needs to affects the source too. + */ + + file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE); + jsrcerr.trace_level = jdsterr.trace_level; + srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use; + +#ifdef TWO_FILE_COMMANDLINE + /* Must have either -outfile switch or explicit output file name */ + if (outfilename == NULL) { + if (file_index != argc-2) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + outfilename = argv[file_index+1]; + } else { + if (file_index != argc-1) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + } +#else + /* Unix style: expect zero or one file name */ + if (file_index < argc-1) { + fprintf(stderr, "%s: only one input file\n", progname); + usage(); + } +#endif /* TWO_FILE_COMMANDLINE */ + + /* Open the input file. */ + if (file_index < argc) { + if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]); + exit(EXIT_FAILURE); + } + } else { + /* default input file is stdin */ + fp = read_stdin(); + } + +#ifdef PROGRESS_REPORT + start_progress_monitor((j_common_ptr) &dstinfo, &progress); +#endif + + /* Specify data source for decompression */ + jpeg_stdio_src(&srcinfo, fp); + + /* Enable saving of extra markers that we want to copy */ + jcopy_markers_setup(&srcinfo, copyoption); + + /* Read file header */ + (void) jpeg_read_header(&srcinfo, TRUE); + + /* Adjust default decompression parameters */ + if (scaleoption != NULL) + if (sscanf(scaleoption, "%d/%d", + &srcinfo.scale_num, &srcinfo.scale_denom) < 1) + usage(); + + /* Any space needed by a transform option must be requested before + * jpeg_read_coefficients so that memory allocation will be done right. + */ +#if TRANSFORMS_SUPPORTED + /* Fail right away if -perfect is given and transformation is not perfect. + */ + if (!jtransform_request_workspace(&srcinfo, &transformoption)) { + fprintf(stderr, "%s: transformation is not perfect\n", progname); + exit(EXIT_FAILURE); + } +#endif + + /* Read source file as DCT coefficients */ + src_coef_arrays = jpeg_read_coefficients(&srcinfo); + + /* Initialize destination compression parameters from source values */ + jpeg_copy_critical_parameters(&srcinfo, &dstinfo); + + /* Adjust destination parameters if required by transform options; + * also find out which set of coefficient arrays will hold the output. + */ +#if TRANSFORMS_SUPPORTED + dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo, + src_coef_arrays, + &transformoption); +#else + dst_coef_arrays = src_coef_arrays; +#endif + + /* Close input file, if we opened it. + * Note: we assume that jpeg_read_coefficients consumed all input + * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will + * only consume more while (! cinfo->inputctl->eoi_reached). + * We cannot call jpeg_finish_decompress here since we still need the + * virtual arrays allocated from the source object for processing. + */ + if (fp != stdin) + fclose(fp); + + /* Open the output file. */ + if (outfilename != NULL) { + if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename); + exit(EXIT_FAILURE); + } + } else { + /* default output file is stdout */ + fp = write_stdout(); + } + + /* Adjust default compression parameters by re-parsing the options */ + file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE); + + /* Specify data destination for compression */ + jpeg_stdio_dest(&dstinfo, fp); + + /* Start compressor (note no image data is actually written here) */ + jpeg_write_coefficients(&dstinfo, dst_coef_arrays); + + /* Copy to the output file any extra markers that we want to preserve */ + jcopy_markers_execute(&srcinfo, &dstinfo, copyoption); + + /* Execute image transformation, if any */ +#if TRANSFORMS_SUPPORTED + jtransform_execute_transformation(&srcinfo, &dstinfo, + src_coef_arrays, + &transformoption); +#endif + + /* Finish compression and release memory */ + jpeg_finish_compress(&dstinfo); + jpeg_destroy_compress(&dstinfo); + (void) jpeg_finish_decompress(&srcinfo); + jpeg_destroy_decompress(&srcinfo); + + /* Close output file, if we opened it */ + if (fp != stdout) + fclose(fp); + +#ifdef PROGRESS_REPORT + end_progress_monitor((j_common_ptr) &dstinfo); +#endif + + /* All done. */ + exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS); + return 0; /* suppress no-return-value warnings */ +} diff --git a/crypto777/jpeg/jquant1.c b/crypto777/jpeg/jquant1.c new file mode 100644 index 000000000..9d11f7066 --- /dev/null +++ b/crypto777/jpeg/jquant1.c @@ -0,0 +1,857 @@ +/* + * jquant1.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modified 2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains 1-pass color quantization (color mapping) routines. + * These routines provide mapping to a fixed color map using equally spaced + * color values. Optional Floyd-Steinberg or ordered dithering is available. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + +#ifdef QUANT_1PASS_SUPPORTED + + +/* + * The main purpose of 1-pass quantization is to provide a fast, if not very + * high quality, colormapped output capability. A 2-pass quantizer usually + * gives better visual quality; however, for quantized grayscale output this + * quantizer is perfectly adequate. Dithering is highly recommended with this + * quantizer, though you can turn it off if you really want to. + * + * In 1-pass quantization the colormap must be chosen in advance of seeing the + * image. We use a map consisting of all combinations of Ncolors[i] color + * values for the i'th component. The Ncolors[] values are chosen so that + * their product, the total number of colors, is no more than that requested. + * (In most cases, the product will be somewhat less.) + * + * Since the colormap is orthogonal, the representative value for each color + * component can be determined without considering the other components; + * then these indexes can be combined into a colormap index by a standard + * N-dimensional-array-subscript calculation. Most of the arithmetic involved + * can be precalculated and stored in the lookup table colorindex[]. + * colorindex[i][j] maps pixel value j in component i to the nearest + * representative value (grid plane) for that component; this index is + * multiplied by the array stride for component i, so that the + * index of the colormap entry closest to a given pixel value is just + * sum( colorindex[component-number][pixel-component-value] ) + * Aside from being fast, this scheme allows for variable spacing between + * representative values with no additional lookup cost. + * + * If gamma correction has been applied in color conversion, it might be wise + * to adjust the color grid spacing so that the representative colors are + * equidistant in linear space. At this writing, gamma correction is not + * implemented by jdcolor, so nothing is done here. + */ + + +/* Declarations for ordered dithering. + * + * We use a standard 16x16 ordered dither array. The basic concept of ordered + * dithering is described in many references, for instance Dale Schumacher's + * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991). + * In place of Schumacher's comparisons against a "threshold" value, we add a + * "dither" value to the input pixel and then round the result to the nearest + * output value. The dither value is equivalent to (0.5 - threshold) times + * the distance between output values. For ordered dithering, we assume that + * the output colors are equally spaced; if not, results will probably be + * worse, since the dither may be too much or too little at a given point. + * + * The normal calculation would be to form pixel value + dither, range-limit + * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual. + * We can skip the separate range-limiting step by extending the colorindex + * table in both directions. + */ + +#define ODITHER_SIZE 16 /* dimension of dither matrix */ +/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */ +#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */ +#define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */ + +typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE]; +typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE]; + +static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = { + /* Bayer's order-4 dither array. Generated by the code given in + * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I. + * The values in this array must range from 0 to ODITHER_CELLS-1. + */ + { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 }, + { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 }, + { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 }, + { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 }, + { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 }, + { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 }, + { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 }, + { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 }, + { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 }, + { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 }, + { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 }, + { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 }, + { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 }, + { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 }, + { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 }, + { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 } +}; + + +/* Declarations for Floyd-Steinberg dithering. + * + * Errors are accumulated into the array fserrors[], at a resolution of + * 1/16th of a pixel count. The error at a given pixel is propagated + * to its not-yet-processed neighbors using the standard F-S fractions, + * ... (here) 7/16 + * 3/16 5/16 1/16 + * We work left-to-right on even rows, right-to-left on odd rows. + * + * We can get away with a single array (holding one row's worth of errors) + * by using it to store the current row's errors at pixel columns not yet + * processed, but the next row's errors at columns already processed. We + * need only a few extra variables to hold the errors immediately around the + * current column. (If we are lucky, those variables are in registers, but + * even if not, they're probably cheaper to access than array elements are.) + * + * The fserrors[] array is indexed [component#][position]. + * We provide (#columns + 2) entries per component; the extra entry at each + * end saves us from special-casing the first and last pixels. + * + * Note: on a wide image, we might not have enough room in a PC's near data + * segment to hold the error array; so it is allocated with alloc_large. + */ + +#if BITS_IN_JSAMPLE == 8 +typedef INT16 FSERROR; /* 16 bits should be enough */ +typedef int LOCFSERROR; /* use 'int' for calculation temps */ +#else +typedef INT32 FSERROR; /* may need more than 16 bits */ +typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */ +#endif + +typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */ + + +/* Private subobject */ + +#define MAX_Q_COMPS 4 /* max components I can handle */ + +typedef struct { + struct jpeg_color_quantizer pub; /* public fields */ + + /* Initially allocated colormap is saved here */ + JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */ + int sv_actual; /* number of entries in use */ + + JSAMPARRAY colorindex; /* Precomputed mapping for speed */ + /* colorindex[i][j] = index of color closest to pixel value j in component i, + * premultiplied as described above. Since colormap indexes must fit into + * JSAMPLEs, the entries of this array will too. + */ + boolean is_padded; /* is the colorindex padded for odither? */ + + int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */ + + /* Variables for ordered dithering */ + int row_index; /* cur row's vertical index in dither matrix */ + ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */ + + /* Variables for Floyd-Steinberg dithering */ + FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */ + boolean on_odd_row; /* flag to remember which row we are on */ +} my_cquantizer; + +typedef my_cquantizer * my_cquantize_ptr; + + +/* + * Policy-making subroutines for create_colormap and create_colorindex. + * These routines determine the colormap to be used. The rest of the module + * only assumes that the colormap is orthogonal. + * + * * select_ncolors decides how to divvy up the available colors + * among the components. + * * output_value defines the set of representative values for a component. + * * largest_input_value defines the mapping from input values to + * representative values for a component. + * Note that the latter two routines may impose different policies for + * different components, though this is not currently done. + */ + + +LOCAL(int) +select_ncolors (j_decompress_ptr cinfo, int Ncolors[]) +/* Determine allocation of desired colors to components, */ +/* and fill in Ncolors[] array to indicate choice. */ +/* Return value is total number of colors (product of Ncolors[] values). */ +{ + int nc = cinfo->out_color_components; /* number of color components */ + int max_colors = cinfo->desired_number_of_colors; + int total_colors, iroot, i, j; + boolean changed; + long temp; + static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE }; + + /* We can allocate at least the nc'th root of max_colors per component. */ + /* Compute floor(nc'th root of max_colors). */ + iroot = 1; + do { + iroot++; + temp = iroot; /* set temp = iroot ** nc */ + for (i = 1; i < nc; i++) + temp *= iroot; + } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */ + iroot--; /* now iroot = floor(root) */ + + /* Must have at least 2 color values per component */ + if (iroot < 2) + ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp); + + /* Initialize to iroot color values for each component */ + total_colors = 1; + for (i = 0; i < nc; i++) { + Ncolors[i] = iroot; + total_colors *= iroot; + } + /* We may be able to increment the count for one or more components without + * exceeding max_colors, though we know not all can be incremented. + * Sometimes, the first component can be incremented more than once! + * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.) + * In RGB colorspace, try to increment G first, then R, then B. + */ + do { + changed = FALSE; + for (i = 0; i < nc; i++) { + j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i); + /* calculate new total_colors if Ncolors[j] is incremented */ + temp = total_colors / Ncolors[j]; + temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */ + if (temp > (long) max_colors) + break; /* won't fit, done with this pass */ + Ncolors[j]++; /* OK, apply the increment */ + total_colors = (int) temp; + changed = TRUE; + } + } while (changed); + + return total_colors; +} + + +LOCAL(int) +output_value (j_decompress_ptr cinfo, int ci, int j, int maxj) +/* Return j'th output value, where j will range from 0 to maxj */ +/* The output values must fall in 0..MAXJSAMPLE in increasing order */ +{ + /* We always provide values 0 and MAXJSAMPLE for each component; + * any additional values are equally spaced between these limits. + * (Forcing the upper and lower values to the limits ensures that + * dithering can't produce a color outside the selected gamut.) + */ + return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj); +} + + +LOCAL(int) +largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj) +/* Return largest input value that should map to j'th output value */ +/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */ +{ + /* Breakpoints are halfway between values returned by output_value */ + return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj)); +} + + +/* + * Create the colormap. + */ + +LOCAL(void) +create_colormap (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + JSAMPARRAY colormap; /* Created colormap */ + int total_colors; /* Number of distinct output colors */ + int i,j,k, nci, blksize, blkdist, ptr, val; + + /* Select number of colors for each component */ + total_colors = select_ncolors(cinfo, cquantize->Ncolors); + + /* Report selected color counts */ + if (cinfo->out_color_components == 3) + TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS, + total_colors, cquantize->Ncolors[0], + cquantize->Ncolors[1], cquantize->Ncolors[2]); + else + TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors); + + /* Allocate and fill in the colormap. */ + /* The colors are ordered in the map in standard row-major order, */ + /* i.e. rightmost (highest-indexed) color changes most rapidly. */ + + colormap = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components); + + /* blksize is number of adjacent repeated entries for a component */ + /* blkdist is distance between groups of identical entries for a component */ + blkdist = total_colors; + + for (i = 0; i < cinfo->out_color_components; i++) { + /* fill in colormap entries for i'th color component */ + nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ + blksize = blkdist / nci; + for (j = 0; j < nci; j++) { + /* Compute j'th output value (out of nci) for component */ + val = output_value(cinfo, i, j, nci-1); + /* Fill in all colormap entries that have this value of this component */ + for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { + /* fill in blksize entries beginning at ptr */ + for (k = 0; k < blksize; k++) + colormap[i][ptr+k] = (JSAMPLE) val; + } + } + blkdist = blksize; /* blksize of this color is blkdist of next */ + } + + /* Save the colormap in private storage, + * where it will survive color quantization mode changes. + */ + cquantize->sv_colormap = colormap; + cquantize->sv_actual = total_colors; +} + + +/* + * Create the color index table. + */ + +LOCAL(void) +create_colorindex (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + JSAMPROW indexptr; + int i,j,k, nci, blksize, val, pad; + + /* For ordered dither, we pad the color index tables by MAXJSAMPLE in + * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE). + * This is not necessary in the other dithering modes. However, we + * flag whether it was done in case user changes dithering mode. + */ + if (cinfo->dither_mode == JDITHER_ORDERED) { + pad = MAXJSAMPLE*2; + cquantize->is_padded = TRUE; + } else { + pad = 0; + cquantize->is_padded = FALSE; + } + + cquantize->colorindex = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) (MAXJSAMPLE+1 + pad), + (JDIMENSION) cinfo->out_color_components); + + /* blksize is number of adjacent repeated entries for a component */ + blksize = cquantize->sv_actual; + + for (i = 0; i < cinfo->out_color_components; i++) { + /* fill in colorindex entries for i'th color component */ + nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ + blksize = blksize / nci; + + /* adjust colorindex pointers to provide padding at negative indexes. */ + if (pad) + cquantize->colorindex[i] += MAXJSAMPLE; + + /* in loop, val = index of current output value, */ + /* and k = largest j that maps to current val */ + indexptr = cquantize->colorindex[i]; + val = 0; + k = largest_input_value(cinfo, i, 0, nci-1); + for (j = 0; j <= MAXJSAMPLE; j++) { + while (j > k) /* advance val if past boundary */ + k = largest_input_value(cinfo, i, ++val, nci-1); + /* premultiply so that no multiplication needed in main processing */ + indexptr[j] = (JSAMPLE) (val * blksize); + } + /* Pad at both ends if necessary */ + if (pad) + for (j = 1; j <= MAXJSAMPLE; j++) { + indexptr[-j] = indexptr[0]; + indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE]; + } + } +} + + +/* + * Create an ordered-dither array for a component having ncolors + * distinct output values. + */ + +LOCAL(ODITHER_MATRIX_PTR) +make_odither_array (j_decompress_ptr cinfo, int ncolors) +{ + ODITHER_MATRIX_PTR odither; + int j,k; + INT32 num,den; + + odither = (ODITHER_MATRIX_PTR) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(ODITHER_MATRIX)); + /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1). + * Hence the dither value for the matrix cell with fill order f + * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1). + * On 16-bit-int machine, be careful to avoid overflow. + */ + den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1)); + for (j = 0; j < ODITHER_SIZE; j++) { + for (k = 0; k < ODITHER_SIZE; k++) { + num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k]))) + * MAXJSAMPLE; + /* Ensure round towards zero despite C's lack of consistency + * about rounding negative values in integer division... + */ + odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den); + } + } + return odither; +} + + +/* + * Create the ordered-dither tables. + * Components having the same number of representative colors may + * share a dither table. + */ + +LOCAL(void) +create_odither_tables (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + ODITHER_MATRIX_PTR odither; + int i, j, nci; + + for (i = 0; i < cinfo->out_color_components; i++) { + nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ + odither = NULL; /* search for matching prior component */ + for (j = 0; j < i; j++) { + if (nci == cquantize->Ncolors[j]) { + odither = cquantize->odither[j]; + break; + } + } + if (odither == NULL) /* need a new table? */ + odither = make_odither_array(cinfo, nci); + cquantize->odither[i] = odither; + } +} + + +/* + * Map some rows of pixels to the output colormapped representation. + */ + +METHODDEF(void) +color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf, + JSAMPARRAY output_buf, int num_rows) +/* General case, no dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + JSAMPARRAY colorindex = cquantize->colorindex; + register int pixcode, ci; + register JSAMPROW ptrin, ptrout; + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + register int nc = cinfo->out_color_components; + + for (row = 0; row < num_rows; row++) { + ptrin = input_buf[row]; + ptrout = output_buf[row]; + for (col = width; col > 0; col--) { + pixcode = 0; + for (ci = 0; ci < nc; ci++) { + pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]); + } + *ptrout++ = (JSAMPLE) pixcode; + } + } +} + + +METHODDEF(void) +color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf, + JSAMPARRAY output_buf, int num_rows) +/* Fast path for out_color_components==3, no dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + register int pixcode; + register JSAMPROW ptrin, ptrout; + JSAMPROW colorindex0 = cquantize->colorindex[0]; + JSAMPROW colorindex1 = cquantize->colorindex[1]; + JSAMPROW colorindex2 = cquantize->colorindex[2]; + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + + for (row = 0; row < num_rows; row++) { + ptrin = input_buf[row]; + ptrout = output_buf[row]; + for (col = width; col > 0; col--) { + pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]); + pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]); + pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]); + *ptrout++ = (JSAMPLE) pixcode; + } + } +} + + +METHODDEF(void) +quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, + JSAMPARRAY output_buf, int num_rows) +/* General case, with ordered dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + register JSAMPROW input_ptr; + register JSAMPROW output_ptr; + JSAMPROW colorindex_ci; + int * dither; /* points to active row of dither matrix */ + int row_index, col_index; /* current indexes into dither matrix */ + int nc = cinfo->out_color_components; + int ci; + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + + for (row = 0; row < num_rows; row++) { + /* Initialize output values to 0 so can process components separately */ + FMEMZERO((void FAR *) output_buf[row], + (size_t) (width * SIZEOF(JSAMPLE))); + row_index = cquantize->row_index; + for (ci = 0; ci < nc; ci++) { + input_ptr = input_buf[row] + ci; + output_ptr = output_buf[row]; + colorindex_ci = cquantize->colorindex[ci]; + dither = cquantize->odither[ci][row_index]; + col_index = 0; + + for (col = width; col > 0; col--) { + /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE, + * select output value, accumulate into output code for this pixel. + * Range-limiting need not be done explicitly, as we have extended + * the colorindex table to produce the right answers for out-of-range + * inputs. The maximum dither is +- MAXJSAMPLE; this sets the + * required amount of padding. + */ + *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]]; + input_ptr += nc; + output_ptr++; + col_index = (col_index + 1) & ODITHER_MASK; + } + } + /* Advance row index for next row */ + row_index = (row_index + 1) & ODITHER_MASK; + cquantize->row_index = row_index; + } +} + + +METHODDEF(void) +quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, + JSAMPARRAY output_buf, int num_rows) +/* Fast path for out_color_components==3, with ordered dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + register int pixcode; + register JSAMPROW input_ptr; + register JSAMPROW output_ptr; + JSAMPROW colorindex0 = cquantize->colorindex[0]; + JSAMPROW colorindex1 = cquantize->colorindex[1]; + JSAMPROW colorindex2 = cquantize->colorindex[2]; + int * dither0; /* points to active row of dither matrix */ + int * dither1; + int * dither2; + int row_index, col_index; /* current indexes into dither matrix */ + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + + for (row = 0; row < num_rows; row++) { + row_index = cquantize->row_index; + input_ptr = input_buf[row]; + output_ptr = output_buf[row]; + dither0 = cquantize->odither[0][row_index]; + dither1 = cquantize->odither[1][row_index]; + dither2 = cquantize->odither[2][row_index]; + col_index = 0; + + for (col = width; col > 0; col--) { + pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) + + dither0[col_index]]); + pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) + + dither1[col_index]]); + pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) + + dither2[col_index]]); + *output_ptr++ = (JSAMPLE) pixcode; + col_index = (col_index + 1) & ODITHER_MASK; + } + row_index = (row_index + 1) & ODITHER_MASK; + cquantize->row_index = row_index; + } +} + + +METHODDEF(void) +quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, + JSAMPARRAY output_buf, int num_rows) +/* General case, with Floyd-Steinberg dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + register LOCFSERROR cur; /* current error or pixel value */ + LOCFSERROR belowerr; /* error for pixel below cur */ + LOCFSERROR bpreverr; /* error for below/prev col */ + LOCFSERROR bnexterr; /* error for below/next col */ + LOCFSERROR delta; + register FSERRPTR errorptr; /* => fserrors[] at column before current */ + register JSAMPROW input_ptr; + register JSAMPROW output_ptr; + JSAMPROW colorindex_ci; + JSAMPROW colormap_ci; + int pixcode; + int nc = cinfo->out_color_components; + int dir; /* 1 for left-to-right, -1 for right-to-left */ + int dirnc; /* dir * nc */ + int ci; + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + JSAMPLE *range_limit = cinfo->sample_range_limit; + SHIFT_TEMPS + + for (row = 0; row < num_rows; row++) { + /* Initialize output values to 0 so can process components separately */ + FMEMZERO((void FAR *) output_buf[row], + (size_t) (width * SIZEOF(JSAMPLE))); + for (ci = 0; ci < nc; ci++) { + input_ptr = input_buf[row] + ci; + output_ptr = output_buf[row]; + if (cquantize->on_odd_row) { + /* work right to left in this row */ + input_ptr += (width-1) * nc; /* so point to rightmost pixel */ + output_ptr += width-1; + dir = -1; + dirnc = -nc; + errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */ + } else { + /* work left to right in this row */ + dir = 1; + dirnc = nc; + errorptr = cquantize->fserrors[ci]; /* => entry before first column */ + } + colorindex_ci = cquantize->colorindex[ci]; + colormap_ci = cquantize->sv_colormap[ci]; + /* Preset error values: no error propagated to first pixel from left */ + cur = 0; + /* and no error propagated to row below yet */ + belowerr = bpreverr = 0; + + for (col = width; col > 0; col--) { + /* cur holds the error propagated from the previous pixel on the + * current line. Add the error propagated from the previous line + * to form the complete error correction term for this pixel, and + * round the error term (which is expressed * 16) to an integer. + * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct + * for either sign of the error value. + * Note: errorptr points to *previous* column's array entry. + */ + cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4); + /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. + * The maximum error is +- MAXJSAMPLE; this sets the required size + * of the range_limit array. + */ + cur += GETJSAMPLE(*input_ptr); + cur = GETJSAMPLE(range_limit[cur]); + /* Select output value, accumulate into output code for this pixel */ + pixcode = GETJSAMPLE(colorindex_ci[cur]); + *output_ptr += (JSAMPLE) pixcode; + /* Compute actual representation error at this pixel */ + /* Note: we can do this even though we don't have the final */ + /* pixel code, because the colormap is orthogonal. */ + cur -= GETJSAMPLE(colormap_ci[pixcode]); + /* Compute error fractions to be propagated to adjacent pixels. + * Add these into the running sums, and simultaneously shift the + * next-line error sums left by 1 column. + */ + bnexterr = cur; + delta = cur * 2; + cur += delta; /* form error * 3 */ + errorptr[0] = (FSERROR) (bpreverr + cur); + cur += delta; /* form error * 5 */ + bpreverr = belowerr + cur; + belowerr = bnexterr; + cur += delta; /* form error * 7 */ + /* At this point cur contains the 7/16 error value to be propagated + * to the next pixel on the current line, and all the errors for the + * next line have been shifted over. We are therefore ready to move on. + */ + input_ptr += dirnc; /* advance input ptr to next column */ + output_ptr += dir; /* advance output ptr to next column */ + errorptr += dir; /* advance errorptr to current column */ + } + /* Post-loop cleanup: we must unload the final error value into the + * final fserrors[] entry. Note we need not unload belowerr because + * it is for the dummy column before or after the actual array. + */ + errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */ + } + cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE); + } +} + + +/* + * Allocate workspace for Floyd-Steinberg errors. + */ + +LOCAL(void) +alloc_fs_workspace (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + size_t arraysize; + int i; + + arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); + for (i = 0; i < cinfo->out_color_components; i++) { + cquantize->fserrors[i] = (FSERRPTR) + (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); + } +} + + +/* + * Initialize for one-pass color quantization. + */ + +METHODDEF(void) +start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + size_t arraysize; + int i; + + /* Install my colormap. */ + cinfo->colormap = cquantize->sv_colormap; + cinfo->actual_number_of_colors = cquantize->sv_actual; + + /* Initialize for desired dithering mode. */ + switch (cinfo->dither_mode) { + case JDITHER_NONE: + if (cinfo->out_color_components == 3) + cquantize->pub.color_quantize = color_quantize3; + else + cquantize->pub.color_quantize = color_quantize; + break; + case JDITHER_ORDERED: + if (cinfo->out_color_components == 3) + cquantize->pub.color_quantize = quantize3_ord_dither; + else + cquantize->pub.color_quantize = quantize_ord_dither; + cquantize->row_index = 0; /* initialize state for ordered dither */ + /* If user changed to ordered dither from another mode, + * we must recreate the color index table with padding. + * This will cost extra space, but probably isn't very likely. + */ + if (! cquantize->is_padded) + create_colorindex(cinfo); + /* Create ordered-dither tables if we didn't already. */ + if (cquantize->odither[0] == NULL) + create_odither_tables(cinfo); + break; + case JDITHER_FS: + cquantize->pub.color_quantize = quantize_fs_dither; + cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */ + /* Allocate Floyd-Steinberg workspace if didn't already. */ + if (cquantize->fserrors[0] == NULL) + alloc_fs_workspace(cinfo); + /* Initialize the propagated errors to zero. */ + arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); + for (i = 0; i < cinfo->out_color_components; i++) + FMEMZERO((void FAR *) cquantize->fserrors[i], arraysize); + break; + default: + ERREXIT(cinfo, JERR_NOT_COMPILED); + break; + } +} + + +/* + * Finish up at the end of the pass. + */ + +METHODDEF(void) +finish_pass_1_quant (j_decompress_ptr cinfo) +{ + /* no work in 1-pass case */ +} + + +/* + * Switch to a new external colormap between output passes. + * Shouldn't get to this module! + */ + +METHODDEF(void) +new_color_map_1_quant (j_decompress_ptr cinfo) +{ + ERREXIT(cinfo, JERR_MODE_CHANGE); +} + + +/* + * Module initialization routine for 1-pass color quantization. + */ + +GLOBAL(void) +jinit_1pass_quantizer (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize; + + cquantize = (my_cquantize_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_cquantizer)); + cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; + cquantize->pub.start_pass = start_pass_1_quant; + cquantize->pub.finish_pass = finish_pass_1_quant; + cquantize->pub.new_color_map = new_color_map_1_quant; + cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */ + cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */ + + /* Make sure my internal arrays won't overflow */ + if (cinfo->out_color_components > MAX_Q_COMPS) + ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS); + /* Make sure colormap indexes can be represented by JSAMPLEs */ + if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1)) + ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1); + + /* Create the colormap and color index table. */ + create_colormap(cinfo); + create_colorindex(cinfo); + + /* Allocate Floyd-Steinberg workspace now if requested. + * We do this now since it is FAR storage and may affect the memory + * manager's space calculations. If the user changes to FS dither + * mode in a later pass, we will allocate the space then, and will + * possibly overrun the max_memory_to_use setting. + */ + if (cinfo->dither_mode == JDITHER_FS) + alloc_fs_workspace(cinfo); +} + +#endif /* QUANT_1PASS_SUPPORTED */ diff --git a/crypto777/jpeg/jquant2.c b/crypto777/jpeg/jquant2.c new file mode 100644 index 000000000..38fc2af7a --- /dev/null +++ b/crypto777/jpeg/jquant2.c @@ -0,0 +1,1311 @@ +/* + * jquant2.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modified 2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains 2-pass color quantization (color mapping) routines. + * These routines provide selection of a custom color map for an image, + * followed by mapping of the image to that color map, with optional + * Floyd-Steinberg dithering. + * It is also possible to use just the second pass to map to an arbitrary + * externally-given color map. + * + * Note: ordered dithering is not supported, since there isn't any fast + * way to compute intercolor distances; it's unclear that ordered dither's + * fundamental assumptions even hold with an irregularly spaced color map. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + +#ifdef QUANT_2PASS_SUPPORTED + + +/* + * This module implements the well-known Heckbert paradigm for color + * quantization. Most of the ideas used here can be traced back to + * Heckbert's seminal paper + * Heckbert, Paul. "Color Image Quantization for Frame Buffer Display", + * Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304. + * + * In the first pass over the image, we accumulate a histogram showing the + * usage count of each possible color. To keep the histogram to a reasonable + * size, we reduce the precision of the input; typical practice is to retain + * 5 or 6 bits per color, so that 8 or 4 different input values are counted + * in the same histogram cell. + * + * Next, the color-selection step begins with a box representing the whole + * color space, and repeatedly splits the "largest" remaining box until we + * have as many boxes as desired colors. Then the mean color in each + * remaining box becomes one of the possible output colors. + * + * The second pass over the image maps each input pixel to the closest output + * color (optionally after applying a Floyd-Steinberg dithering correction). + * This mapping is logically trivial, but making it go fast enough requires + * considerable care. + * + * Heckbert-style quantizers vary a good deal in their policies for choosing + * the "largest" box and deciding where to cut it. The particular policies + * used here have proved out well in experimental comparisons, but better ones + * may yet be found. + * + * In earlier versions of the IJG code, this module quantized in YCbCr color + * space, processing the raw upsampled data without a color conversion step. + * This allowed the color conversion math to be done only once per colormap + * entry, not once per pixel. However, that optimization precluded other + * useful optimizations (such as merging color conversion with upsampling) + * and it also interfered with desired capabilities such as quantizing to an + * externally-supplied colormap. We have therefore abandoned that approach. + * The present code works in the post-conversion color space, typically RGB. + * + * To improve the visual quality of the results, we actually work in scaled + * RGB space, giving G distances more weight than R, and R in turn more than + * B. To do everything in integer math, we must use integer scale factors. + * The 2/3/1 scale factors used here correspond loosely to the relative + * weights of the colors in the NTSC grayscale equation. + * If you want to use this code to quantize a non-RGB color space, you'll + * probably need to change these scale factors. + */ + +#define R_SCALE 2 /* scale R distances by this much */ +#define G_SCALE 3 /* scale G distances by this much */ +#define B_SCALE 1 /* and B by this much */ + +/* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined + * in jmorecfg.h. As the code stands, it will do the right thing for R,G,B + * and B,G,R orders. If you define some other weird order in jmorecfg.h, + * you'll get compile errors until you extend this logic. In that case + * you'll probably want to tweak the histogram sizes too. + */ + +#if RGB_RED == 0 +#define C0_SCALE R_SCALE +#endif +#if RGB_BLUE == 0 +#define C0_SCALE B_SCALE +#endif +#if RGB_GREEN == 1 +#define C1_SCALE G_SCALE +#endif +#if RGB_RED == 2 +#define C2_SCALE R_SCALE +#endif +#if RGB_BLUE == 2 +#define C2_SCALE B_SCALE +#endif + + +/* + * First we have the histogram data structure and routines for creating it. + * + * The number of bits of precision can be adjusted by changing these symbols. + * We recommend keeping 6 bits for G and 5 each for R and B. + * If you have plenty of memory and cycles, 6 bits all around gives marginally + * better results; if you are short of memory, 5 bits all around will save + * some space but degrade the results. + * To maintain a fully accurate histogram, we'd need to allocate a "long" + * (preferably unsigned long) for each cell. In practice this is overkill; + * we can get by with 16 bits per cell. Few of the cell counts will overflow, + * and clamping those that do overflow to the maximum value will give close- + * enough results. This reduces the recommended histogram size from 256Kb + * to 128Kb, which is a useful savings on PC-class machines. + * (In the second pass the histogram space is re-used for pixel mapping data; + * in that capacity, each cell must be able to store zero to the number of + * desired colors. 16 bits/cell is plenty for that too.) + * Since the JPEG code is intended to run in small memory model on 80x86 + * machines, we can't just allocate the histogram in one chunk. Instead + * of a true 3-D array, we use a row of pointers to 2-D arrays. Each + * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and + * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that + * on 80x86 machines, the pointer row is in near memory but the actual + * arrays are in far memory (same arrangement as we use for image arrays). + */ + +#define MAXNUMCOLORS (MAXJSAMPLE+1) /* maximum size of colormap */ + +/* These will do the right thing for either R,G,B or B,G,R color order, + * but you may not like the results for other color orders. + */ +#define HIST_C0_BITS 5 /* bits of precision in R/B histogram */ +#define HIST_C1_BITS 6 /* bits of precision in G histogram */ +#define HIST_C2_BITS 5 /* bits of precision in B/R histogram */ + +/* Number of elements along histogram axes. */ +#define HIST_C0_ELEMS (1<cquantize; + register JSAMPROW ptr; + register histptr histp; + register hist3d histogram = cquantize->histogram; + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + + for (row = 0; row < num_rows; row++) { + ptr = input_buf[row]; + for (col = width; col > 0; col--) { + /* get pixel value and index into the histogram */ + histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT] + [GETJSAMPLE(ptr[1]) >> C1_SHIFT] + [GETJSAMPLE(ptr[2]) >> C2_SHIFT]; + /* increment, check for overflow and undo increment if so. */ + if (++(*histp) <= 0) + (*histp)--; + ptr += 3; + } + } +} + + +/* + * Next we have the really interesting routines: selection of a colormap + * given the completed histogram. + * These routines work with a list of "boxes", each representing a rectangular + * subset of the input color space (to histogram precision). + */ + +typedef struct { + /* The bounds of the box (inclusive); expressed as histogram indexes */ + int c0min, c0max; + int c1min, c1max; + int c2min, c2max; + /* The volume (actually 2-norm) of the box */ + INT32 volume; + /* The number of nonzero histogram cells within this box */ + long colorcount; +} box; + +typedef box * boxptr; + + +LOCAL(boxptr) +find_biggest_color_pop (boxptr boxlist, int numboxes) +/* Find the splittable box with the largest color population */ +/* Returns NULL if no splittable boxes remain */ +{ + register boxptr boxp; + register int i; + register long maxc = 0; + boxptr which = NULL; + + for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) { + if (boxp->colorcount > maxc && boxp->volume > 0) { + which = boxp; + maxc = boxp->colorcount; + } + } + return which; +} + + +LOCAL(boxptr) +find_biggest_volume (boxptr boxlist, int numboxes) +/* Find the splittable box with the largest (scaled) volume */ +/* Returns NULL if no splittable boxes remain */ +{ + register boxptr boxp; + register int i; + register INT32 maxv = 0; + boxptr which = NULL; + + for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) { + if (boxp->volume > maxv) { + which = boxp; + maxv = boxp->volume; + } + } + return which; +} + + +LOCAL(void) +update_box (j_decompress_ptr cinfo, boxptr boxp) +/* Shrink the min/max bounds of a box to enclose only nonzero elements, */ +/* and recompute its volume and population */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + hist3d histogram = cquantize->histogram; + histptr histp; + int c0,c1,c2; + int c0min,c0max,c1min,c1max,c2min,c2max; + INT32 dist0,dist1,dist2; + long ccount; + + c0min = boxp->c0min; c0max = boxp->c0max; + c1min = boxp->c1min; c1max = boxp->c1max; + c2min = boxp->c2min; c2max = boxp->c2max; + + if (c0max > c0min) + for (c0 = c0min; c0 <= c0max; c0++) + for (c1 = c1min; c1 <= c1max; c1++) { + histp = & histogram[c0][c1][c2min]; + for (c2 = c2min; c2 <= c2max; c2++) + if (*histp++ != 0) { + boxp->c0min = c0min = c0; + goto have_c0min; + } + } + have_c0min: + if (c0max > c0min) + for (c0 = c0max; c0 >= c0min; c0--) + for (c1 = c1min; c1 <= c1max; c1++) { + histp = & histogram[c0][c1][c2min]; + for (c2 = c2min; c2 <= c2max; c2++) + if (*histp++ != 0) { + boxp->c0max = c0max = c0; + goto have_c0max; + } + } + have_c0max: + if (c1max > c1min) + for (c1 = c1min; c1 <= c1max; c1++) + for (c0 = c0min; c0 <= c0max; c0++) { + histp = & histogram[c0][c1][c2min]; + for (c2 = c2min; c2 <= c2max; c2++) + if (*histp++ != 0) { + boxp->c1min = c1min = c1; + goto have_c1min; + } + } + have_c1min: + if (c1max > c1min) + for (c1 = c1max; c1 >= c1min; c1--) + for (c0 = c0min; c0 <= c0max; c0++) { + histp = & histogram[c0][c1][c2min]; + for (c2 = c2min; c2 <= c2max; c2++) + if (*histp++ != 0) { + boxp->c1max = c1max = c1; + goto have_c1max; + } + } + have_c1max: + if (c2max > c2min) + for (c2 = c2min; c2 <= c2max; c2++) + for (c0 = c0min; c0 <= c0max; c0++) { + histp = & histogram[c0][c1min][c2]; + for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS) + if (*histp != 0) { + boxp->c2min = c2min = c2; + goto have_c2min; + } + } + have_c2min: + if (c2max > c2min) + for (c2 = c2max; c2 >= c2min; c2--) + for (c0 = c0min; c0 <= c0max; c0++) { + histp = & histogram[c0][c1min][c2]; + for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS) + if (*histp != 0) { + boxp->c2max = c2max = c2; + goto have_c2max; + } + } + have_c2max: + + /* Update box volume. + * We use 2-norm rather than real volume here; this biases the method + * against making long narrow boxes, and it has the side benefit that + * a box is splittable iff norm > 0. + * Since the differences are expressed in histogram-cell units, + * we have to shift back to JSAMPLE units to get consistent distances; + * after which, we scale according to the selected distance scale factors. + */ + dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE; + dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE; + dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE; + boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2; + + /* Now scan remaining volume of box and compute population */ + ccount = 0; + for (c0 = c0min; c0 <= c0max; c0++) + for (c1 = c1min; c1 <= c1max; c1++) { + histp = & histogram[c0][c1][c2min]; + for (c2 = c2min; c2 <= c2max; c2++, histp++) + if (*histp != 0) { + ccount++; + } + } + boxp->colorcount = ccount; +} + + +LOCAL(int) +median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes, + int desired_colors) +/* Repeatedly select and split the largest box until we have enough boxes */ +{ + int n,lb; + int c0,c1,c2,cmax; + register boxptr b1,b2; + + while (numboxes < desired_colors) { + /* Select box to split. + * Current algorithm: by population for first half, then by volume. + */ + if (numboxes*2 <= desired_colors) { + b1 = find_biggest_color_pop(boxlist, numboxes); + } else { + b1 = find_biggest_volume(boxlist, numboxes); + } + if (b1 == NULL) /* no splittable boxes left! */ + break; + b2 = &boxlist[numboxes]; /* where new box will go */ + /* Copy the color bounds to the new box. */ + b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max; + b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min; + /* Choose which axis to split the box on. + * Current algorithm: longest scaled axis. + * See notes in update_box about scaling distances. + */ + c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE; + c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE; + c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE; + /* We want to break any ties in favor of green, then red, blue last. + * This code does the right thing for R,G,B or B,G,R color orders only. + */ +#if RGB_RED == 0 + cmax = c1; n = 1; + if (c0 > cmax) { cmax = c0; n = 0; } + if (c2 > cmax) { n = 2; } +#else + cmax = c1; n = 1; + if (c2 > cmax) { cmax = c2; n = 2; } + if (c0 > cmax) { n = 0; } +#endif + /* Choose split point along selected axis, and update box bounds. + * Current algorithm: split at halfway point. + * (Since the box has been shrunk to minimum volume, + * any split will produce two nonempty subboxes.) + * Note that lb value is max for lower box, so must be < old max. + */ + switch (n) { + case 0: + lb = (b1->c0max + b1->c0min) / 2; + b1->c0max = lb; + b2->c0min = lb+1; + break; + case 1: + lb = (b1->c1max + b1->c1min) / 2; + b1->c1max = lb; + b2->c1min = lb+1; + break; + case 2: + lb = (b1->c2max + b1->c2min) / 2; + b1->c2max = lb; + b2->c2min = lb+1; + break; + } + /* Update stats for boxes */ + update_box(cinfo, b1); + update_box(cinfo, b2); + numboxes++; + } + return numboxes; +} + + +LOCAL(void) +compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor) +/* Compute representative color for a box, put it in colormap[icolor] */ +{ + /* Current algorithm: mean weighted by pixels (not colors) */ + /* Note it is important to get the rounding correct! */ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + hist3d histogram = cquantize->histogram; + histptr histp; + int c0,c1,c2; + int c0min,c0max,c1min,c1max,c2min,c2max; + long count; + long total = 0; + long c0total = 0; + long c1total = 0; + long c2total = 0; + + c0min = boxp->c0min; c0max = boxp->c0max; + c1min = boxp->c1min; c1max = boxp->c1max; + c2min = boxp->c2min; c2max = boxp->c2max; + + for (c0 = c0min; c0 <= c0max; c0++) + for (c1 = c1min; c1 <= c1max; c1++) { + histp = & histogram[c0][c1][c2min]; + for (c2 = c2min; c2 <= c2max; c2++) { + if ((count = *histp++) != 0) { + total += count; + c0total += ((c0 << C0_SHIFT) + ((1<>1)) * count; + c1total += ((c1 << C1_SHIFT) + ((1<>1)) * count; + c2total += ((c2 << C2_SHIFT) + ((1<>1)) * count; + } + } + } + + cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total); + cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total); + cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total); +} + + +LOCAL(void) +select_colors (j_decompress_ptr cinfo, int desired_colors) +/* Master routine for color selection */ +{ + boxptr boxlist; + int numboxes; + int i; + + /* Allocate workspace for box list */ + boxlist = (boxptr) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box)); + /* Initialize one box containing whole space */ + numboxes = 1; + boxlist[0].c0min = 0; + boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT; + boxlist[0].c1min = 0; + boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT; + boxlist[0].c2min = 0; + boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT; + /* Shrink it to actually-used volume and set its statistics */ + update_box(cinfo, & boxlist[0]); + /* Perform median-cut to produce final box list */ + numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors); + /* Compute the representative color for each box, fill colormap */ + for (i = 0; i < numboxes; i++) + compute_color(cinfo, & boxlist[i], i); + cinfo->actual_number_of_colors = numboxes; + TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes); +} + + +/* + * These routines are concerned with the time-critical task of mapping input + * colors to the nearest color in the selected colormap. + * + * We re-use the histogram space as an "inverse color map", essentially a + * cache for the results of nearest-color searches. All colors within a + * histogram cell will be mapped to the same colormap entry, namely the one + * closest to the cell's center. This may not be quite the closest entry to + * the actual input color, but it's almost as good. A zero in the cache + * indicates we haven't found the nearest color for that cell yet; the array + * is cleared to zeroes before starting the mapping pass. When we find the + * nearest color for a cell, its colormap index plus one is recorded in the + * cache for future use. The pass2 scanning routines call fill_inverse_cmap + * when they need to use an unfilled entry in the cache. + * + * Our method of efficiently finding nearest colors is based on the "locally + * sorted search" idea described by Heckbert and on the incremental distance + * calculation described by Spencer W. Thomas in chapter III.1 of Graphics + * Gems II (James Arvo, ed. Academic Press, 1991). Thomas points out that + * the distances from a given colormap entry to each cell of the histogram can + * be computed quickly using an incremental method: the differences between + * distances to adjacent cells themselves differ by a constant. This allows a + * fairly fast implementation of the "brute force" approach of computing the + * distance from every colormap entry to every histogram cell. Unfortunately, + * it needs a work array to hold the best-distance-so-far for each histogram + * cell (because the inner loop has to be over cells, not colormap entries). + * The work array elements have to be INT32s, so the work array would need + * 256Kb at our recommended precision. This is not feasible in DOS machines. + * + * To get around these problems, we apply Thomas' method to compute the + * nearest colors for only the cells within a small subbox of the histogram. + * The work array need be only as big as the subbox, so the memory usage + * problem is solved. Furthermore, we need not fill subboxes that are never + * referenced in pass2; many images use only part of the color gamut, so a + * fair amount of work is saved. An additional advantage of this + * approach is that we can apply Heckbert's locality criterion to quickly + * eliminate colormap entries that are far away from the subbox; typically + * three-fourths of the colormap entries are rejected by Heckbert's criterion, + * and we need not compute their distances to individual cells in the subbox. + * The speed of this approach is heavily influenced by the subbox size: too + * small means too much overhead, too big loses because Heckbert's criterion + * can't eliminate as many colormap entries. Empirically the best subbox + * size seems to be about 1/512th of the histogram (1/8th in each direction). + * + * Thomas' article also describes a refined method which is asymptotically + * faster than the brute-force method, but it is also far more complex and + * cannot efficiently be applied to small subboxes. It is therefore not + * useful for programs intended to be portable to DOS machines. On machines + * with plenty of memory, filling the whole histogram in one shot with Thomas' + * refined method might be faster than the present code --- but then again, + * it might not be any faster, and it's certainly more complicated. + */ + + +/* log2(histogram cells in update box) for each axis; this can be adjusted */ +#define BOX_C0_LOG (HIST_C0_BITS-3) +#define BOX_C1_LOG (HIST_C1_BITS-3) +#define BOX_C2_LOG (HIST_C2_BITS-3) + +#define BOX_C0_ELEMS (1<actual_number_of_colors; + int maxc0, maxc1, maxc2; + int centerc0, centerc1, centerc2; + int i, x, ncolors; + INT32 minmaxdist, min_dist, max_dist, tdist; + INT32 mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */ + + /* Compute true coordinates of update box's upper corner and center. + * Actually we compute the coordinates of the center of the upper-corner + * histogram cell, which are the upper bounds of the volume we care about. + * Note that since ">>" rounds down, the "center" values may be closer to + * min than to max; hence comparisons to them must be "<=", not "<". + */ + maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT)); + centerc0 = (minc0 + maxc0) >> 1; + maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT)); + centerc1 = (minc1 + maxc1) >> 1; + maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT)); + centerc2 = (minc2 + maxc2) >> 1; + + /* For each color in colormap, find: + * 1. its minimum squared-distance to any point in the update box + * (zero if color is within update box); + * 2. its maximum squared-distance to any point in the update box. + * Both of these can be found by considering only the corners of the box. + * We save the minimum distance for each color in mindist[]; + * only the smallest maximum distance is of interest. + */ + minmaxdist = 0x7FFFFFFFL; + + for (i = 0; i < numcolors; i++) { + /* We compute the squared-c0-distance term, then add in the other two. */ + x = GETJSAMPLE(cinfo->colormap[0][i]); + if (x < minc0) { + tdist = (x - minc0) * C0_SCALE; + min_dist = tdist*tdist; + tdist = (x - maxc0) * C0_SCALE; + max_dist = tdist*tdist; + } else if (x > maxc0) { + tdist = (x - maxc0) * C0_SCALE; + min_dist = tdist*tdist; + tdist = (x - minc0) * C0_SCALE; + max_dist = tdist*tdist; + } else { + /* within cell range so no contribution to min_dist */ + min_dist = 0; + if (x <= centerc0) { + tdist = (x - maxc0) * C0_SCALE; + max_dist = tdist*tdist; + } else { + tdist = (x - minc0) * C0_SCALE; + max_dist = tdist*tdist; + } + } + + x = GETJSAMPLE(cinfo->colormap[1][i]); + if (x < minc1) { + tdist = (x - minc1) * C1_SCALE; + min_dist += tdist*tdist; + tdist = (x - maxc1) * C1_SCALE; + max_dist += tdist*tdist; + } else if (x > maxc1) { + tdist = (x - maxc1) * C1_SCALE; + min_dist += tdist*tdist; + tdist = (x - minc1) * C1_SCALE; + max_dist += tdist*tdist; + } else { + /* within cell range so no contribution to min_dist */ + if (x <= centerc1) { + tdist = (x - maxc1) * C1_SCALE; + max_dist += tdist*tdist; + } else { + tdist = (x - minc1) * C1_SCALE; + max_dist += tdist*tdist; + } + } + + x = GETJSAMPLE(cinfo->colormap[2][i]); + if (x < minc2) { + tdist = (x - minc2) * C2_SCALE; + min_dist += tdist*tdist; + tdist = (x - maxc2) * C2_SCALE; + max_dist += tdist*tdist; + } else if (x > maxc2) { + tdist = (x - maxc2) * C2_SCALE; + min_dist += tdist*tdist; + tdist = (x - minc2) * C2_SCALE; + max_dist += tdist*tdist; + } else { + /* within cell range so no contribution to min_dist */ + if (x <= centerc2) { + tdist = (x - maxc2) * C2_SCALE; + max_dist += tdist*tdist; + } else { + tdist = (x - minc2) * C2_SCALE; + max_dist += tdist*tdist; + } + } + + mindist[i] = min_dist; /* save away the results */ + if (max_dist < minmaxdist) + minmaxdist = max_dist; + } + + /* Now we know that no cell in the update box is more than minmaxdist + * away from some colormap entry. Therefore, only colors that are + * within minmaxdist of some part of the box need be considered. + */ + ncolors = 0; + for (i = 0; i < numcolors; i++) { + if (mindist[i] <= minmaxdist) + colorlist[ncolors++] = (JSAMPLE) i; + } + return ncolors; +} + + +LOCAL(void) +find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2, + int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[]) +/* Find the closest colormap entry for each cell in the update box, + * given the list of candidate colors prepared by find_nearby_colors. + * Return the indexes of the closest entries in the bestcolor[] array. + * This routine uses Thomas' incremental distance calculation method to + * find the distance from a colormap entry to successive cells in the box. + */ +{ + int ic0, ic1, ic2; + int i, icolor; + register INT32 * bptr; /* pointer into bestdist[] array */ + JSAMPLE * cptr; /* pointer into bestcolor[] array */ + INT32 dist0, dist1; /* initial distance values */ + register INT32 dist2; /* current distance in inner loop */ + INT32 xx0, xx1; /* distance increments */ + register INT32 xx2; + INT32 inc0, inc1, inc2; /* initial values for increments */ + /* This array holds the distance to the nearest-so-far color for each cell */ + INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS]; + + /* Initialize best-distance for each cell of the update box */ + bptr = bestdist; + for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--) + *bptr++ = 0x7FFFFFFFL; + + /* For each color selected by find_nearby_colors, + * compute its distance to the center of each cell in the box. + * If that's less than best-so-far, update best distance and color number. + */ + + /* Nominal steps between cell centers ("x" in Thomas article) */ +#define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE) +#define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE) +#define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE) + + for (i = 0; i < numcolors; i++) { + icolor = GETJSAMPLE(colorlist[i]); + /* Compute (square of) distance from minc0/c1/c2 to this color */ + inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE; + dist0 = inc0*inc0; + inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE; + dist0 += inc1*inc1; + inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE; + dist0 += inc2*inc2; + /* Form the initial difference increments */ + inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0; + inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1; + inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2; + /* Now loop over all cells in box, updating distance per Thomas method */ + bptr = bestdist; + cptr = bestcolor; + xx0 = inc0; + for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) { + dist1 = dist0; + xx1 = inc1; + for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) { + dist2 = dist1; + xx2 = inc2; + for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) { + if (dist2 < *bptr) { + *bptr = dist2; + *cptr = (JSAMPLE) icolor; + } + dist2 += xx2; + xx2 += 2 * STEP_C2 * STEP_C2; + bptr++; + cptr++; + } + dist1 += xx1; + xx1 += 2 * STEP_C1 * STEP_C1; + } + dist0 += xx0; + xx0 += 2 * STEP_C0 * STEP_C0; + } + } +} + + +LOCAL(void) +fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2) +/* Fill the inverse-colormap entries in the update box that contains */ +/* histogram cell c0/c1/c2. (Only that one cell MUST be filled, but */ +/* we can fill as many others as we wish.) */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + hist3d histogram = cquantize->histogram; + int minc0, minc1, minc2; /* lower left corner of update box */ + int ic0, ic1, ic2; + register JSAMPLE * cptr; /* pointer into bestcolor[] array */ + register histptr cachep; /* pointer into main cache array */ + /* This array lists the candidate colormap indexes. */ + JSAMPLE colorlist[MAXNUMCOLORS]; + int numcolors; /* number of candidate colors */ + /* This array holds the actually closest colormap index for each cell. */ + JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS]; + + /* Convert cell coordinates to update box ID */ + c0 >>= BOX_C0_LOG; + c1 >>= BOX_C1_LOG; + c2 >>= BOX_C2_LOG; + + /* Compute true coordinates of update box's origin corner. + * Actually we compute the coordinates of the center of the corner + * histogram cell, which are the lower bounds of the volume we care about. + */ + minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1); + minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1); + minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1); + + /* Determine which colormap entries are close enough to be candidates + * for the nearest entry to some cell in the update box. + */ + numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist); + + /* Determine the actually nearest colors. */ + find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist, + bestcolor); + + /* Save the best color numbers (plus 1) in the main cache array */ + c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */ + c1 <<= BOX_C1_LOG; + c2 <<= BOX_C2_LOG; + cptr = bestcolor; + for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) { + for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) { + cachep = & histogram[c0+ic0][c1+ic1][c2]; + for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) { + *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1); + } + } + } +} + + +/* + * Map some rows of pixels to the output colormapped representation. + */ + +METHODDEF(void) +pass2_no_dither (j_decompress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) +/* This version performs no dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + hist3d histogram = cquantize->histogram; + register JSAMPROW inptr, outptr; + register histptr cachep; + register int c0, c1, c2; + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + + for (row = 0; row < num_rows; row++) { + inptr = input_buf[row]; + outptr = output_buf[row]; + for (col = width; col > 0; col--) { + /* get pixel value and index into the cache */ + c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT; + c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT; + c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT; + cachep = & histogram[c0][c1][c2]; + /* If we have not seen this color before, find nearest colormap entry */ + /* and update the cache */ + if (*cachep == 0) + fill_inverse_cmap(cinfo, c0,c1,c2); + /* Now emit the colormap index for this cell */ + *outptr++ = (JSAMPLE) (*cachep - 1); + } + } +} + + +METHODDEF(void) +pass2_fs_dither (j_decompress_ptr cinfo, + JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) +/* This version performs Floyd-Steinberg dithering */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + hist3d histogram = cquantize->histogram; + register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */ + LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */ + LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */ + register FSERRPTR errorptr; /* => fserrors[] at column before current */ + JSAMPROW inptr; /* => current input pixel */ + JSAMPROW outptr; /* => current output pixel */ + histptr cachep; + int dir; /* +1 or -1 depending on direction */ + int dir3; /* 3*dir, for advancing inptr & errorptr */ + int row; + JDIMENSION col; + JDIMENSION width = cinfo->output_width; + JSAMPLE *range_limit = cinfo->sample_range_limit; + int *error_limit = cquantize->error_limiter; + JSAMPROW colormap0 = cinfo->colormap[0]; + JSAMPROW colormap1 = cinfo->colormap[1]; + JSAMPROW colormap2 = cinfo->colormap[2]; + SHIFT_TEMPS + + for (row = 0; row < num_rows; row++) { + inptr = input_buf[row]; + outptr = output_buf[row]; + if (cquantize->on_odd_row) { + /* work right to left in this row */ + inptr += (width-1) * 3; /* so point to rightmost pixel */ + outptr += width-1; + dir = -1; + dir3 = -3; + errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */ + cquantize->on_odd_row = FALSE; /* flip for next time */ + } else { + /* work left to right in this row */ + dir = 1; + dir3 = 3; + errorptr = cquantize->fserrors; /* => entry before first real column */ + cquantize->on_odd_row = TRUE; /* flip for next time */ + } + /* Preset error values: no error propagated to first pixel from left */ + cur0 = cur1 = cur2 = 0; + /* and no error propagated to row below yet */ + belowerr0 = belowerr1 = belowerr2 = 0; + bpreverr0 = bpreverr1 = bpreverr2 = 0; + + for (col = width; col > 0; col--) { + /* curN holds the error propagated from the previous pixel on the + * current line. Add the error propagated from the previous line + * to form the complete error correction term for this pixel, and + * round the error term (which is expressed * 16) to an integer. + * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct + * for either sign of the error value. + * Note: errorptr points to *previous* column's array entry. + */ + cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4); + cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4); + cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4); + /* Limit the error using transfer function set by init_error_limit. + * See comments with init_error_limit for rationale. + */ + cur0 = error_limit[cur0]; + cur1 = error_limit[cur1]; + cur2 = error_limit[cur2]; + /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. + * The maximum error is +- MAXJSAMPLE (or less with error limiting); + * this sets the required size of the range_limit array. + */ + cur0 += GETJSAMPLE(inptr[0]); + cur1 += GETJSAMPLE(inptr[1]); + cur2 += GETJSAMPLE(inptr[2]); + cur0 = GETJSAMPLE(range_limit[cur0]); + cur1 = GETJSAMPLE(range_limit[cur1]); + cur2 = GETJSAMPLE(range_limit[cur2]); + /* Index into the cache with adjusted pixel value */ + cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT]; + /* If we have not seen this color before, find nearest colormap */ + /* entry and update the cache */ + if (*cachep == 0) + fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT); + /* Now emit the colormap index for this cell */ + { register int pixcode = *cachep - 1; + *outptr = (JSAMPLE) pixcode; + /* Compute representation error for this pixel */ + cur0 -= GETJSAMPLE(colormap0[pixcode]); + cur1 -= GETJSAMPLE(colormap1[pixcode]); + cur2 -= GETJSAMPLE(colormap2[pixcode]); + } + /* Compute error fractions to be propagated to adjacent pixels. + * Add these into the running sums, and simultaneously shift the + * next-line error sums left by 1 column. + */ + { register LOCFSERROR bnexterr, delta; + + bnexterr = cur0; /* Process component 0 */ + delta = cur0 * 2; + cur0 += delta; /* form error * 3 */ + errorptr[0] = (FSERROR) (bpreverr0 + cur0); + cur0 += delta; /* form error * 5 */ + bpreverr0 = belowerr0 + cur0; + belowerr0 = bnexterr; + cur0 += delta; /* form error * 7 */ + bnexterr = cur1; /* Process component 1 */ + delta = cur1 * 2; + cur1 += delta; /* form error * 3 */ + errorptr[1] = (FSERROR) (bpreverr1 + cur1); + cur1 += delta; /* form error * 5 */ + bpreverr1 = belowerr1 + cur1; + belowerr1 = bnexterr; + cur1 += delta; /* form error * 7 */ + bnexterr = cur2; /* Process component 2 */ + delta = cur2 * 2; + cur2 += delta; /* form error * 3 */ + errorptr[2] = (FSERROR) (bpreverr2 + cur2); + cur2 += delta; /* form error * 5 */ + bpreverr2 = belowerr2 + cur2; + belowerr2 = bnexterr; + cur2 += delta; /* form error * 7 */ + } + /* At this point curN contains the 7/16 error value to be propagated + * to the next pixel on the current line, and all the errors for the + * next line have been shifted over. We are therefore ready to move on. + */ + inptr += dir3; /* Advance pixel pointers to next column */ + outptr += dir; + errorptr += dir3; /* advance errorptr to current column */ + } + /* Post-loop cleanup: we must unload the final error values into the + * final fserrors[] entry. Note we need not unload belowerrN because + * it is for the dummy column before or after the actual array. + */ + errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */ + errorptr[1] = (FSERROR) bpreverr1; + errorptr[2] = (FSERROR) bpreverr2; + } +} + + +/* + * Initialize the error-limiting transfer function (lookup table). + * The raw F-S error computation can potentially compute error values of up to + * +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be + * much less, otherwise obviously wrong pixels will be created. (Typical + * effects include weird fringes at color-area boundaries, isolated bright + * pixels in a dark area, etc.) The standard advice for avoiding this problem + * is to ensure that the "corners" of the color cube are allocated as output + * colors; then repeated errors in the same direction cannot cause cascading + * error buildup. However, that only prevents the error from getting + * completely out of hand; Aaron Giles reports that error limiting improves + * the results even with corner colors allocated. + * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty + * well, but the smoother transfer function used below is even better. Thanks + * to Aaron Giles for this idea. + */ + +LOCAL(void) +init_error_limit (j_decompress_ptr cinfo) +/* Allocate and fill in the error_limiter table */ +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + int * table; + int in, out; + + table = (int *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int)); + table += MAXJSAMPLE; /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */ + cquantize->error_limiter = table; + +#define STEPSIZE ((MAXJSAMPLE+1)/16) + /* Map errors 1:1 up to +- MAXJSAMPLE/16 */ + out = 0; + for (in = 0; in < STEPSIZE; in++, out++) { + table[in] = out; table[-in] = -out; + } + /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */ + for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) { + table[in] = out; table[-in] = -out; + } + /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */ + for (; in <= MAXJSAMPLE; in++) { + table[in] = out; table[-in] = -out; + } +#undef STEPSIZE +} + + +/* + * Finish up at the end of each pass. + */ + +METHODDEF(void) +finish_pass1 (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + + /* Select the representative colors and fill in cinfo->colormap */ + cinfo->colormap = cquantize->sv_colormap; + select_colors(cinfo, cquantize->desired); + /* Force next pass to zero the color index table */ + cquantize->needs_zeroed = TRUE; +} + + +METHODDEF(void) +finish_pass2 (j_decompress_ptr cinfo) +{ + /* no work */ +} + + +/* + * Initialize for each processing pass. + */ + +METHODDEF(void) +start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + hist3d histogram = cquantize->histogram; + int i; + + /* Only F-S dithering or no dithering is supported. */ + /* If user asks for ordered dither, give him F-S. */ + if (cinfo->dither_mode != JDITHER_NONE) + cinfo->dither_mode = JDITHER_FS; + + if (is_pre_scan) { + /* Set up method pointers */ + cquantize->pub.color_quantize = prescan_quantize; + cquantize->pub.finish_pass = finish_pass1; + cquantize->needs_zeroed = TRUE; /* Always zero histogram */ + } else { + /* Set up method pointers */ + if (cinfo->dither_mode == JDITHER_FS) + cquantize->pub.color_quantize = pass2_fs_dither; + else + cquantize->pub.color_quantize = pass2_no_dither; + cquantize->pub.finish_pass = finish_pass2; + + /* Make sure color count is acceptable */ + i = cinfo->actual_number_of_colors; + if (i < 1) + ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1); + if (i > MAXNUMCOLORS) + ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS); + + if (cinfo->dither_mode == JDITHER_FS) { + size_t arraysize = (size_t) ((cinfo->output_width + 2) * + (3 * SIZEOF(FSERROR))); + /* Allocate Floyd-Steinberg workspace if we didn't already. */ + if (cquantize->fserrors == NULL) + cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large) + ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); + /* Initialize the propagated errors to zero. */ + FMEMZERO((void FAR *) cquantize->fserrors, arraysize); + /* Make the error-limit table if we didn't already. */ + if (cquantize->error_limiter == NULL) + init_error_limit(cinfo); + cquantize->on_odd_row = FALSE; + } + + } + /* Zero the histogram or inverse color map, if necessary */ + if (cquantize->needs_zeroed) { + for (i = 0; i < HIST_C0_ELEMS; i++) { + FMEMZERO((void FAR *) histogram[i], + HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell)); + } + cquantize->needs_zeroed = FALSE; + } +} + + +/* + * Switch to a new external colormap between output passes. + */ + +METHODDEF(void) +new_color_map_2_quant (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; + + /* Reset the inverse color map */ + cquantize->needs_zeroed = TRUE; +} + + +/* + * Module initialization routine for 2-pass color quantization. + */ + +GLOBAL(void) +jinit_2pass_quantizer (j_decompress_ptr cinfo) +{ + my_cquantize_ptr cquantize; + int i; + + cquantize = (my_cquantize_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_cquantizer)); + cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; + cquantize->pub.start_pass = start_pass_2_quant; + cquantize->pub.new_color_map = new_color_map_2_quant; + cquantize->fserrors = NULL; /* flag optional arrays not allocated */ + cquantize->error_limiter = NULL; + + /* Make sure jdmaster didn't give me a case I can't handle */ + if (cinfo->out_color_components != 3) + ERREXIT(cinfo, JERR_NOTIMPL); + + /* Allocate the histogram/inverse colormap storage */ + cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d)); + for (i = 0; i < HIST_C0_ELEMS; i++) { + cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell)); + } + cquantize->needs_zeroed = TRUE; /* histogram is garbage now */ + + /* Allocate storage for the completed colormap, if required. + * We do this now since it is FAR storage and may affect + * the memory manager's space calculations. + */ + if (cinfo->enable_2pass_quant) { + /* Make sure color count is acceptable */ + int desired = cinfo->desired_number_of_colors; + /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */ + if (desired < 8) + ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8); + /* Make sure colormap indexes can be represented by JSAMPLEs */ + if (desired > MAXNUMCOLORS) + ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS); + cquantize->sv_colormap = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3); + cquantize->desired = desired; + } else + cquantize->sv_colormap = NULL; + + /* Only F-S dithering or no dithering is supported. */ + /* If user asks for ordered dither, give him F-S. */ + if (cinfo->dither_mode != JDITHER_NONE) + cinfo->dither_mode = JDITHER_FS; + + /* Allocate Floyd-Steinberg workspace if necessary. + * This isn't really needed until pass 2, but again it is FAR storage. + * Although we will cope with a later change in dither_mode, + * we do not promise to honor max_memory_to_use if dither_mode changes. + */ + if (cinfo->dither_mode == JDITHER_FS) { + cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR)))); + /* Might as well create the error-limiting table too. */ + init_error_limit(cinfo); + } +} + +#endif /* QUANT_2PASS_SUPPORTED */ diff --git a/crypto777/jpeg/jutils.c b/crypto777/jpeg/jutils.c new file mode 100644 index 000000000..5b16b6d03 --- /dev/null +++ b/crypto777/jpeg/jutils.c @@ -0,0 +1,227 @@ +/* + * jutils.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modified 2009-2011 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains tables and miscellaneous utility routines needed + * for both compression and decompression. + * Note we prefix all global names with "j" to minimize conflicts with + * a surrounding application. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* + * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element + * of a DCT block read in natural order (left to right, top to bottom). + */ + +#if 0 /* This table is not actually needed in v6a */ + +const int jpeg_zigzag_order[DCTSIZE2] = { + 0, 1, 5, 6, 14, 15, 27, 28, + 2, 4, 7, 13, 16, 26, 29, 42, + 3, 8, 12, 17, 25, 30, 41, 43, + 9, 11, 18, 24, 31, 40, 44, 53, + 10, 19, 23, 32, 39, 45, 52, 54, + 20, 22, 33, 38, 46, 51, 55, 60, + 21, 34, 37, 47, 50, 56, 59, 61, + 35, 36, 48, 49, 57, 58, 62, 63 +}; + +#endif + +/* + * jpeg_natural_order[i] is the natural-order position of the i'th element + * of zigzag order. + * + * When reading corrupted data, the Huffman decoders could attempt + * to reference an entry beyond the end of this array (if the decoded + * zero run length reaches past the end of the block). To prevent + * wild stores without adding an inner-loop test, we put some extra + * "63"s after the real entries. This will cause the extra coefficient + * to be stored in location 63 of the block, not somewhere random. + * The worst case would be a run-length of 15, which means we need 16 + * fake entries. + */ + +const int jpeg_natural_order[DCTSIZE2+16] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, + 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + +const int jpeg_natural_order7[7*7+16] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 14, 21, 28, 35, + 42, 49, 50, 43, 36, 29, 22, 30, + 37, 44, 51, 52, 45, 38, 46, 53, + 54, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + +const int jpeg_natural_order6[6*6+16] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 41, 34, 27, + 20, 13, 21, 28, 35, 42, 43, 36, + 29, 37, 44, 45, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + +const int jpeg_natural_order5[5*5+16] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 12, + 19, 26, 33, 34, 27, 20, 28, 35, + 36, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + +const int jpeg_natural_order4[4*4+16] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 25, 18, 11, 19, 26, 27, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + +const int jpeg_natural_order3[3*3+16] = { + 0, 1, 8, 16, 9, 2, 10, 17, + 18, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + +const int jpeg_natural_order2[2*2+16] = { + 0, 1, 8, 9, + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ + 63, 63, 63, 63, 63, 63, 63, 63 +}; + + +/* + * Arithmetic utilities + */ + +GLOBAL(long) +jdiv_round_up (long a, long b) +/* Compute a/b rounded up to next integer, ie, ceil(a/b) */ +/* Assumes a >= 0, b > 0 */ +{ + return (a + b - 1L) / b; +} + + +GLOBAL(long) +jround_up (long a, long b) +/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */ +/* Assumes a >= 0, b > 0 */ +{ + a += b - 1L; + return a - (a % b); +} + + +/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays + * and coefficient-block arrays. This won't work on 80x86 because the arrays + * are FAR and we're assuming a small-pointer memory model. However, some + * DOS compilers provide far-pointer versions of memcpy() and memset() even + * in the small-model libraries. These will be used if USE_FMEM is defined. + * Otherwise, the routines below do it the hard way. (The performance cost + * is not all that great, because these routines aren't very heavily used.) + */ + +#ifndef NEED_FAR_POINTERS /* normal case, same as regular macro */ +#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size) +#else /* 80x86 case, define if we can */ +#ifdef USE_FMEM +#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size)) +#else +/* This function is for use by the FMEMZERO macro defined in jpegint.h. + * Do not call this function directly, use the FMEMZERO macro instead. + */ +GLOBAL(void) +jzero_far (void FAR * target, size_t bytestozero) +/* Zero out a chunk of FAR memory. */ +/* This might be sample-array data, block-array data, or alloc_large data. */ +{ + register char FAR * ptr = (char FAR *) target; + register size_t count; + + for (count = bytestozero; count > 0; count--) { + *ptr++ = 0; + } +} +#endif +#endif + + +GLOBAL(void) +jcopy_sample_rows (JSAMPARRAY input_array, int source_row, + JSAMPARRAY output_array, int dest_row, + int num_rows, JDIMENSION num_cols) +/* Copy some rows of samples from one place to another. + * num_rows rows are copied from input_array[source_row++] + * to output_array[dest_row++]; these areas may overlap for duplication. + * The source and destination arrays must be at least as wide as num_cols. + */ +{ + register JSAMPROW inptr, outptr; +#ifdef FMEMCOPY + register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE)); +#else + register JDIMENSION count; +#endif + register int row; + + input_array += source_row; + output_array += dest_row; + + for (row = num_rows; row > 0; row--) { + inptr = *input_array++; + outptr = *output_array++; +#ifdef FMEMCOPY + FMEMCOPY(outptr, inptr, count); +#else + for (count = num_cols; count > 0; count--) + *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */ +#endif + } +} + + +GLOBAL(void) +jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row, + JDIMENSION num_blocks) +/* Copy a row of coefficient blocks from one place to another. */ +{ +#ifdef FMEMCOPY + FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF))); +#else + register JCOEFPTR inptr, outptr; + register long count; + + inptr = (JCOEFPTR) input_row; + outptr = (JCOEFPTR) output_row; + for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) { + *outptr++ = *inptr++; + } +#endif +} diff --git a/crypto777/jpeg/jversion.h b/crypto777/jpeg/jversion.h new file mode 100644 index 000000000..5d4915103 --- /dev/null +++ b/crypto777/jpeg/jversion.h @@ -0,0 +1,14 @@ +/* + * jversion.h + * + * Copyright (C) 1991-2012, Thomas G. Lane, Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains software version identification. + */ + + +#define JVERSION "8d 15-Jan-2012" + +#define JCOPYRIGHT "Copyright (C) 2012, Thomas G. Lane, Guido Vollbeding" diff --git a/crypto777/jpeg/rdbmp.c b/crypto777/jpeg/rdbmp.c new file mode 100644 index 000000000..fd773d4bb --- /dev/null +++ b/crypto777/jpeg/rdbmp.c @@ -0,0 +1,480 @@ +/* + * rdbmp.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * Modified 2009-2010 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to read input images in Microsoft "BMP" + * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors). + * Currently, only 8-bit and 24-bit images are supported, not 1-bit or + * 4-bit (feeding such low-depth images into JPEG would be silly anyway). + * Also, we don't support RLE-compressed files. + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume input from + * an ordinary stdio stream. They further assume that reading begins + * at the start of the file; start_input may need work if the + * user interface has already read some data (e.g., to determine that + * the file is indeed BMP format). + * + * This code contributed by James Arthur Boucher. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef BMP_SUPPORTED + + +/* Macros to deal with unsigned chars as efficiently as compiler allows */ + +#ifdef HAVE_UNSIGNED_CHAR +typedef unsigned char U_CHAR; +#define UCH(x) ((int) (x)) +#else /* !HAVE_UNSIGNED_CHAR */ +#ifdef CHAR_IS_UNSIGNED +typedef char U_CHAR; +#define UCH(x) ((int) (x)) +#else +typedef char U_CHAR; +#define UCH(x) ((int) (x) & 0xFF) +#endif +#endif /* HAVE_UNSIGNED_CHAR */ + + +#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) + + +/* Private version of data source object */ + +typedef struct _bmp_source_struct * bmp_source_ptr; + +typedef struct _bmp_source_struct { + struct cjpeg_source_struct pub; /* public fields */ + + j_compress_ptr cinfo; /* back link saves passing separate parm */ + + JSAMPARRAY colormap; /* BMP colormap (converted to my format) */ + + jvirt_sarray_ptr whole_image; /* Needed to reverse row order */ + JDIMENSION source_row; /* Current source row number */ + JDIMENSION row_width; /* Physical width of scanlines in file */ + + int bits_per_pixel; /* remembers 8- or 24-bit format */ +} bmp_source_struct; + + +LOCAL(int) +read_byte (bmp_source_ptr sinfo) +/* Read next byte from BMP file */ +{ + register FILE *infile = sinfo->pub.input_file; + register int c; + + if ((c = getc(infile)) == EOF) + ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); + return c; +} + + +LOCAL(void) +read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize) +/* Read the colormap from a BMP file */ +{ + int i; + + switch (mapentrysize) { + case 3: + /* BGR format (occurs in OS/2 files) */ + for (i = 0; i < cmaplen; i++) { + sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo); + sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo); + sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo); + } + break; + case 4: + /* BGR0 format (occurs in MS Windows files) */ + for (i = 0; i < cmaplen; i++) { + sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo); + sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo); + sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo); + (void) read_byte(sinfo); + } + break; + default: + ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP); + break; + } +} + + +/* + * Read one row of pixels. + * The image has been read into the whole_image array, but is otherwise + * unprocessed. We must read it out in top-to-bottom row order, and if + * it is an 8-bit image, we must expand colormapped pixels to 24bit format. + */ + +METHODDEF(JDIMENSION) +get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 8-bit colormap indexes */ +{ + bmp_source_ptr source = (bmp_source_ptr) sinfo; + register JSAMPARRAY colormap = source->colormap; + JSAMPARRAY image_ptr; + register int t; + register JSAMPROW inptr, outptr; + register JDIMENSION col; + + /* Fetch next row from virtual array */ + source->source_row--; + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->whole_image, + source->source_row, (JDIMENSION) 1, FALSE); + + /* Expand the colormap indexes to real data */ + inptr = image_ptr[0]; + outptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + t = GETJSAMPLE(*inptr++); + *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */ + *outptr++ = colormap[1][t]; + *outptr++ = colormap[2][t]; + } + + return 1; +} + + +METHODDEF(JDIMENSION) +get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 24-bit pixels */ +{ + bmp_source_ptr source = (bmp_source_ptr) sinfo; + JSAMPARRAY image_ptr; + register JSAMPROW inptr, outptr; + register JDIMENSION col; + + /* Fetch next row from virtual array */ + source->source_row--; + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->whole_image, + source->source_row, (JDIMENSION) 1, FALSE); + + /* Transfer data. Note source values are in BGR order + * (even though Microsoft's own documents say the opposite). + */ + inptr = image_ptr[0]; + outptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ + outptr[1] = *inptr++; + outptr[0] = *inptr++; + outptr += 3; + } + + return 1; +} + + +METHODDEF(JDIMENSION) +get_32bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 32-bit pixels */ +{ + bmp_source_ptr source = (bmp_source_ptr) sinfo; + JSAMPARRAY image_ptr; + register JSAMPROW inptr, outptr; + register JDIMENSION col; + + /* Fetch next row from virtual array */ + source->source_row--; + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->whole_image, + source->source_row, (JDIMENSION) 1, FALSE); + /* Transfer data. Note source values are in BGR order + * (even though Microsoft's own documents say the opposite). + */ + inptr = image_ptr[0]; + outptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ + outptr[1] = *inptr++; + outptr[0] = *inptr++; + inptr++; /* skip the 4th byte (Alpha channel) */ + outptr += 3; + } + + return 1; +} + + +/* + * This method loads the image into whole_image during the first call on + * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call + * get_8bit_row, get_24bit_row, or get_32bit_row on subsequent calls. + */ + +METHODDEF(JDIMENSION) +preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + bmp_source_ptr source = (bmp_source_ptr) sinfo; + register FILE *infile = source->pub.input_file; + register int c; + register JSAMPROW out_ptr; + JSAMPARRAY image_ptr; + JDIMENSION row, col; + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; + + /* Read the data into a virtual array in input-file row order. */ + for (row = 0; row < cinfo->image_height; row++) { + if (progress != NULL) { + progress->pub.pass_counter = (long) row; + progress->pub.pass_limit = (long) cinfo->image_height; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->whole_image, + row, (JDIMENSION) 1, TRUE); + out_ptr = image_ptr[0]; + for (col = source->row_width; col > 0; col--) { + /* inline copy of read_byte() for speed */ + if ((c = getc(infile)) == EOF) + ERREXIT(cinfo, JERR_INPUT_EOF); + *out_ptr++ = (JSAMPLE) c; + } + } + if (progress != NULL) + progress->completed_extra_passes++; + + /* Set up to read from the virtual array in top-to-bottom order */ + switch (source->bits_per_pixel) { + case 8: + source->pub.get_pixel_rows = get_8bit_row; + break; + case 24: + source->pub.get_pixel_rows = get_24bit_row; + break; + case 32: + source->pub.get_pixel_rows = get_32bit_row; + break; + default: + ERREXIT(cinfo, JERR_BMP_BADDEPTH); + } + source->source_row = cinfo->image_height; + + /* And read the first row */ + return (*source->pub.get_pixel_rows) (cinfo, sinfo); +} + + +/* + * Read the file header; return image size and component count. + */ + +METHODDEF(void) +start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + bmp_source_ptr source = (bmp_source_ptr) sinfo; + U_CHAR bmpfileheader[14]; + U_CHAR bmpinfoheader[64]; +#define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \ + (((unsigned int) UCH(array[offset+1])) << 8)) +#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \ + (((INT32) UCH(array[offset+1])) << 8) + \ + (((INT32) UCH(array[offset+2])) << 16) + \ + (((INT32) UCH(array[offset+3])) << 24)) + INT32 bfOffBits; + INT32 headerSize; + INT32 biWidth; + INT32 biHeight; + unsigned int biPlanes; + INT32 biCompression; + INT32 biXPelsPerMeter,biYPelsPerMeter; + INT32 biClrUsed = 0; + int mapentrysize = 0; /* 0 indicates no colormap */ + INT32 bPad; + JDIMENSION row_width; + + /* Read and verify the bitmap file header */ + if (! ReadOK(source->pub.input_file, bmpfileheader, 14)) + ERREXIT(cinfo, JERR_INPUT_EOF); + if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */ + ERREXIT(cinfo, JERR_BMP_NOT); + bfOffBits = (INT32) GET_4B(bmpfileheader,10); + /* We ignore the remaining fileheader fields */ + + /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows), + * or 64 bytes (OS/2 2.x). Check the first 4 bytes to find out which. + */ + if (! ReadOK(source->pub.input_file, bmpinfoheader, 4)) + ERREXIT(cinfo, JERR_INPUT_EOF); + headerSize = (INT32) GET_4B(bmpinfoheader,0); + if (headerSize < 12 || headerSize > 64) + ERREXIT(cinfo, JERR_BMP_BADHEADER); + if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4)) + ERREXIT(cinfo, JERR_INPUT_EOF); + + switch ((int) headerSize) { + case 12: + /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */ + biWidth = (INT32) GET_2B(bmpinfoheader,4); + biHeight = (INT32) GET_2B(bmpinfoheader,6); + biPlanes = GET_2B(bmpinfoheader,8); + source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10); + + switch (source->bits_per_pixel) { + case 8: /* colormapped image */ + mapentrysize = 3; /* OS/2 uses RGBTRIPLE colormap */ + TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight); + break; + case 24: /* RGB image */ + TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight); + break; + default: + ERREXIT(cinfo, JERR_BMP_BADDEPTH); + break; + } + break; + case 40: + case 64: + /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */ + /* or OS/2 2.x header, which has additional fields that we ignore */ + biWidth = GET_4B(bmpinfoheader,4); + biHeight = GET_4B(bmpinfoheader,8); + biPlanes = GET_2B(bmpinfoheader,12); + source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14); + biCompression = GET_4B(bmpinfoheader,16); + biXPelsPerMeter = GET_4B(bmpinfoheader,24); + biYPelsPerMeter = GET_4B(bmpinfoheader,28); + biClrUsed = GET_4B(bmpinfoheader,32); + /* biSizeImage, biClrImportant fields are ignored */ + + switch (source->bits_per_pixel) { + case 8: /* colormapped image */ + mapentrysize = 4; /* Windows uses RGBQUAD colormap */ + TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight); + break; + case 24: /* RGB image */ + TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight); + break; + case 32: /* RGB image + Alpha channel */ + TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight); + break; + default: + ERREXIT(cinfo, JERR_BMP_BADDEPTH); + break; + } + if (biCompression != 0) + ERREXIT(cinfo, JERR_BMP_COMPRESSED); + + if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) { + /* Set JFIF density parameters from the BMP data */ + cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */ + cinfo->Y_density = (UINT16) (biYPelsPerMeter/100); + cinfo->density_unit = 2; /* dots/cm */ + } + break; + default: + ERREXIT(cinfo, JERR_BMP_BADHEADER); + return; + } + + if (biWidth <= 0 || biHeight <= 0) + ERREXIT(cinfo, JERR_BMP_EMPTY); + if (biPlanes != 1) + ERREXIT(cinfo, JERR_BMP_BADPLANES); + + /* Compute distance to bitmap data --- will adjust for colormap below */ + bPad = bfOffBits - (headerSize + 14); + + /* Read the colormap, if any */ + if (mapentrysize > 0) { + if (biClrUsed <= 0) + biClrUsed = 256; /* assume it's 256 */ + else if (biClrUsed > 256) + ERREXIT(cinfo, JERR_BMP_BADCMAP); + /* Allocate space to store the colormap */ + source->colormap = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) biClrUsed, (JDIMENSION) 3); + /* and read it from the file */ + read_colormap(source, (int) biClrUsed, mapentrysize); + /* account for size of colormap */ + bPad -= biClrUsed * mapentrysize; + } + + /* Skip any remaining pad bytes */ + if (bPad < 0) /* incorrect bfOffBits value? */ + ERREXIT(cinfo, JERR_BMP_BADHEADER); + while (--bPad >= 0) { + (void) read_byte(source); + } + + /* Compute row width in file, including padding to 4-byte boundary */ + if (source->bits_per_pixel == 24) + row_width = (JDIMENSION) (biWidth * 3); + else if (source->bits_per_pixel == 32) + row_width = (JDIMENSION) (biWidth * 4); + else + row_width = (JDIMENSION) biWidth; + while ((row_width & 3) != 0) row_width++; + source->row_width = row_width; + + /* Allocate space for inversion array, prepare for preload pass */ + source->whole_image = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + row_width, (JDIMENSION) biHeight, (JDIMENSION) 1); + source->pub.get_pixel_rows = preload_image; + if (cinfo->progress != NULL) { + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; + progress->total_extra_passes++; /* count file input as separate pass */ + } + + /* Allocate one-row buffer for returned data */ + source->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) (biWidth * 3), (JDIMENSION) 1); + source->pub.buffer_height = 1; + + cinfo->in_color_space = JCS_RGB; + cinfo->input_components = 3; + cinfo->data_precision = 8; + cinfo->image_width = (JDIMENSION) biWidth; + cinfo->image_height = (JDIMENSION) biHeight; +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + /* no work */ +} + + +/* + * The module selection routine for BMP format input. + */ + +GLOBAL(cjpeg_source_ptr) +jinit_read_bmp (j_compress_ptr cinfo) +{ + bmp_source_ptr source; + + /* Create module interface object */ + source = (bmp_source_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(bmp_source_struct)); + source->cinfo = cinfo; /* make back link for subroutines */ + /* Fill in method ptrs, except get_pixel_rows which start_input sets */ + source->pub.start_input = start_input_bmp; + source->pub.finish_input = finish_input_bmp; + + return (cjpeg_source_ptr) source; +} + +#endif /* BMP_SUPPORTED */ diff --git a/crypto777/jpeg/rdcolmap.c b/crypto777/jpeg/rdcolmap.c new file mode 100644 index 000000000..42b343763 --- /dev/null +++ b/crypto777/jpeg/rdcolmap.c @@ -0,0 +1,253 @@ +/* + * rdcolmap.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file implements djpeg's "-map file" switch. It reads a source image + * and constructs a colormap to be supplied to the JPEG decompressor. + * + * Currently, these file formats are supported for the map file: + * GIF: the contents of the GIF's global colormap are used. + * PPM (either text or raw flavor): the entire file is read and + * each unique pixel value is entered in the map. + * Note that reading a large PPM file will be horrendously slow. + * Typically, a PPM-format map file should contain just one pixel + * of each desired color. Such a file can be extracted from an + * ordinary image PPM file with ppmtomap(1). + * + * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not + * currently implemented. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */ + +/* Portions of this code are based on the PBMPLUS library, which is: +** +** Copyright (C) 1988 by Jef Poskanzer. +** +** Permission to use, copy, modify, and distribute this software and its +** documentation for any purpose and without fee is hereby granted, provided +** that the above copyright notice appear in all copies and that both that +** copyright notice and this permission notice appear in supporting +** documentation. This software is provided "as is" without express or +** implied warranty. +*/ + + +/* + * Add a (potentially) new color to the color map. + */ + +LOCAL(void) +add_map_entry (j_decompress_ptr cinfo, int R, int G, int B) +{ + JSAMPROW colormap0 = cinfo->colormap[0]; + JSAMPROW colormap1 = cinfo->colormap[1]; + JSAMPROW colormap2 = cinfo->colormap[2]; + int ncolors = cinfo->actual_number_of_colors; + int index; + + /* Check for duplicate color. */ + for (index = 0; index < ncolors; index++) { + if (GETJSAMPLE(colormap0[index]) == R && + GETJSAMPLE(colormap1[index]) == G && + GETJSAMPLE(colormap2[index]) == B) + return; /* color is already in map */ + } + + /* Check for map overflow. */ + if (ncolors >= (MAXJSAMPLE+1)) + ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1)); + + /* OK, add color to map. */ + colormap0[ncolors] = (JSAMPLE) R; + colormap1[ncolors] = (JSAMPLE) G; + colormap2[ncolors] = (JSAMPLE) B; + cinfo->actual_number_of_colors++; +} + + +/* + * Extract color map from a GIF file. + */ + +LOCAL(void) +read_gif_map (j_decompress_ptr cinfo, FILE * infile) +{ + int header[13]; + int i, colormaplen; + int R, G, B; + + /* Initial 'G' has already been read by read_color_map */ + /* Read the rest of the GIF header and logical screen descriptor */ + for (i = 1; i < 13; i++) { + if ((header[i] = getc(infile)) == EOF) + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + } + + /* Verify GIF Header */ + if (header[1] != 'I' || header[2] != 'F') + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + + /* There must be a global color map. */ + if ((header[10] & 0x80) == 0) + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + + /* OK, fetch it. */ + colormaplen = 2 << (header[10] & 0x07); + + for (i = 0; i < colormaplen; i++) { + R = getc(infile); + G = getc(infile); + B = getc(infile); + if (R == EOF || G == EOF || B == EOF) + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + add_map_entry(cinfo, + R << (BITS_IN_JSAMPLE-8), + G << (BITS_IN_JSAMPLE-8), + B << (BITS_IN_JSAMPLE-8)); + } +} + + +/* Support routines for reading PPM */ + + +LOCAL(int) +pbm_getc (FILE * infile) +/* Read next char, skipping over any comments */ +/* A comment/newline sequence is returned as a newline */ +{ + register int ch; + + ch = getc(infile); + if (ch == '#') { + do { + ch = getc(infile); + } while (ch != '\n' && ch != EOF); + } + return ch; +} + + +LOCAL(unsigned int) +read_pbm_integer (j_decompress_ptr cinfo, FILE * infile) +/* Read an unsigned decimal integer from the PPM file */ +/* Swallows one trailing character after the integer */ +/* Note that on a 16-bit-int machine, only values up to 64k can be read. */ +/* This should not be a problem in practice. */ +{ + register int ch; + register unsigned int val; + + /* Skip any leading whitespace */ + do { + ch = pbm_getc(infile); + if (ch == EOF) + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); + + if (ch < '0' || ch > '9') + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + + val = ch - '0'; + while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { + val *= 10; + val += ch - '0'; + } + return val; +} + + +/* + * Extract color map from a PPM file. + */ + +LOCAL(void) +read_ppm_map (j_decompress_ptr cinfo, FILE * infile) +{ + int c; + unsigned int w, h, maxval, row, col; + int R, G, B; + + /* Initial 'P' has already been read by read_color_map */ + c = getc(infile); /* save format discriminator for a sec */ + + /* while we fetch the remaining header info */ + w = read_pbm_integer(cinfo, infile); + h = read_pbm_integer(cinfo, infile); + maxval = read_pbm_integer(cinfo, infile); + + if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + + /* For now, we don't support rescaling from an unusual maxval. */ + if (maxval != (unsigned int) MAXJSAMPLE) + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + + switch (c) { + case '3': /* it's a text-format PPM file */ + for (row = 0; row < h; row++) { + for (col = 0; col < w; col++) { + R = read_pbm_integer(cinfo, infile); + G = read_pbm_integer(cinfo, infile); + B = read_pbm_integer(cinfo, infile); + add_map_entry(cinfo, R, G, B); + } + } + break; + + case '6': /* it's a raw-format PPM file */ + for (row = 0; row < h; row++) { + for (col = 0; col < w; col++) { + R = getc(infile); + G = getc(infile); + B = getc(infile); + if (R == EOF || G == EOF || B == EOF) + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + add_map_entry(cinfo, R, G, B); + } + } + break; + + default: + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + break; + } +} + + +/* + * Main entry point from djpeg.c. + * Input: opened input file (from file name argument on command line). + * Output: colormap and actual_number_of_colors fields are set in cinfo. + */ + +GLOBAL(void) +read_color_map (j_decompress_ptr cinfo, FILE * infile) +{ + /* Allocate space for a color map of maximum supported size. */ + cinfo->colormap = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) (MAXJSAMPLE+1), (JDIMENSION) 3); + cinfo->actual_number_of_colors = 0; /* initialize map to empty */ + + /* Read first byte to determine file format */ + switch (getc(infile)) { + case 'G': + read_gif_map(cinfo, infile); + break; + case 'P': + read_ppm_map(cinfo, infile); + break; + default: + ERREXIT(cinfo, JERR_BAD_CMAP_FILE); + break; + } +} + +#endif /* QUANT_2PASS_SUPPORTED */ diff --git a/crypto777/jpeg/rdgif.c b/crypto777/jpeg/rdgif.c new file mode 100644 index 000000000..b27c1675d --- /dev/null +++ b/crypto777/jpeg/rdgif.c @@ -0,0 +1,38 @@ +/* + * rdgif.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to read input images in GIF format. + * + ***************************************************************************** + * NOTE: to avoid entanglements with Unisys' patent on LZW compression, * + * the ability to read GIF files has been removed from the IJG distribution. * + * Sorry about that. * + ***************************************************************************** + * + * We are required to state that + * "The Graphics Interchange Format(c) is the Copyright property of + * CompuServe Incorporated. GIF(sm) is a Service Mark property of + * CompuServe Incorporated." + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef GIF_SUPPORTED + +/* + * The module selection routine for GIF format input. + */ + +GLOBAL(cjpeg_source_ptr) +jinit_read_gif (j_compress_ptr cinfo) +{ + fprintf(stderr, "GIF input is unsupported for legal reasons. Sorry.\n"); + exit(EXIT_FAILURE); + return NULL; /* keep compiler happy */ +} + +#endif /* GIF_SUPPORTED */ diff --git a/crypto777/jpeg/rdjpgcom.c b/crypto777/jpeg/rdjpgcom.c new file mode 100644 index 000000000..371915474 --- /dev/null +++ b/crypto777/jpeg/rdjpgcom.c @@ -0,0 +1,515 @@ +/* + * rdjpgcom.c + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * Modified 2009 by Bill Allombert, Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a very simple stand-alone application that displays + * the text in COM (comment) markers in a JFIF file. + * This may be useful as an example of the minimum logic needed to parse + * JPEG markers. + */ + +#define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */ +#include "jinclude.h" /* get auto-config symbols, */ + +#ifdef HAVE_LOCALE_H +#include /* Bill Allombert: use locale for isprint */ +#endif +#include /* to declare isupper(), tolower() */ +#ifdef USE_SETMODE +#include /* to declare setmode()'s parameter macros */ +/* If you have setmode() but not , just delete this line: */ +#include /* to declare setmode() */ +#endif + +#ifdef USE_CCOMMAND /* command-line reader for Macintosh */ +#ifdef __MWERKS__ +#include /* Metrowerks needs this */ +#include /* ... and this */ +#endif +#ifdef THINK_C +#include /* Think declares it here */ +#endif +#endif + +#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */ +#define READ_BINARY "r" +#else +#ifdef VMS /* VMS is very nonstandard */ +#define READ_BINARY "rb", "ctx=stm" +#else /* standard ANSI-compliant case */ +#define READ_BINARY "rb" +#endif +#endif + +#ifndef EXIT_FAILURE /* define exit() codes if not provided */ +#define EXIT_FAILURE 1 +#endif +#ifndef EXIT_SUCCESS +#ifdef VMS +#define EXIT_SUCCESS 1 /* VMS is very nonstandard */ +#else +#define EXIT_SUCCESS 0 +#endif +#endif + + +/* + * These macros are used to read the input file. + * To reuse this code in another application, you might need to change these. + */ + +static FILE * infile; /* input JPEG file */ + +/* Return next input byte, or EOF if no more */ +#define NEXTBYTE() getc(infile) + + +/* Error exit handler */ +#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE)) + + +/* Read one byte, testing for EOF */ +static int +read_1_byte (void) +{ + int c; + + c = NEXTBYTE(); + if (c == EOF) + ERREXIT("Premature EOF in JPEG file"); + return c; +} + +/* Read 2 bytes, convert to unsigned int */ +/* All 2-byte quantities in JPEG markers are MSB first */ +static unsigned int +read_2_bytes (void) +{ + int c1, c2; + + c1 = NEXTBYTE(); + if (c1 == EOF) + ERREXIT("Premature EOF in JPEG file"); + c2 = NEXTBYTE(); + if (c2 == EOF) + ERREXIT("Premature EOF in JPEG file"); + return (((unsigned int) c1) << 8) + ((unsigned int) c2); +} + + +/* + * JPEG markers consist of one or more 0xFF bytes, followed by a marker + * code byte (which is not an FF). Here are the marker codes of interest + * in this program. (See jdmarker.c for a more complete list.) + */ + +#define M_SOF0 0xC0 /* Start Of Frame N */ +#define M_SOF1 0xC1 /* N indicates which compression process */ +#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */ +#define M_SOF3 0xC3 +#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */ +#define M_SOF6 0xC6 +#define M_SOF7 0xC7 +#define M_SOF9 0xC9 +#define M_SOF10 0xCA +#define M_SOF11 0xCB +#define M_SOF13 0xCD +#define M_SOF14 0xCE +#define M_SOF15 0xCF +#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */ +#define M_EOI 0xD9 /* End Of Image (end of datastream) */ +#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */ +#define M_APP0 0xE0 /* Application-specific marker, type N */ +#define M_APP12 0xEC /* (we don't bother to list all 16 APPn's) */ +#define M_COM 0xFE /* COMment */ + + +/* + * Find the next JPEG marker and return its marker code. + * We expect at least one FF byte, possibly more if the compressor used FFs + * to pad the file. + * There could also be non-FF garbage between markers. The treatment of such + * garbage is unspecified; we choose to skip over it but emit a warning msg. + * NB: this routine must not be used after seeing SOS marker, since it will + * not deal correctly with FF/00 sequences in the compressed image data... + */ + +static int +next_marker (void) +{ + int c; + int discarded_bytes = 0; + + /* Find 0xFF byte; count and skip any non-FFs. */ + c = read_1_byte(); + while (c != 0xFF) { + discarded_bytes++; + c = read_1_byte(); + } + /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs + * are legal as pad bytes, so don't count them in discarded_bytes. + */ + do { + c = read_1_byte(); + } while (c == 0xFF); + + if (discarded_bytes != 0) { + fprintf(stderr, "Warning: garbage data found in JPEG file\n"); + } + + return c; +} + + +/* + * Read the initial marker, which should be SOI. + * For a JFIF file, the first two bytes of the file should be literally + * 0xFF M_SOI. To be more general, we could use next_marker, but if the + * input file weren't actually JPEG at all, next_marker might read the whole + * file and then return a misleading error message... + */ + +static int +first_marker (void) +{ + int c1, c2; + + c1 = NEXTBYTE(); + c2 = NEXTBYTE(); + if (c1 != 0xFF || c2 != M_SOI) + ERREXIT("Not a JPEG file"); + return c2; +} + + +/* + * Most types of marker are followed by a variable-length parameter segment. + * This routine skips over the parameters for any marker we don't otherwise + * want to process. + * Note that we MUST skip the parameter segment explicitly in order not to + * be fooled by 0xFF bytes that might appear within the parameter segment; + * such bytes do NOT introduce new markers. + */ + +static void +skip_variable (void) +/* Skip over an unknown or uninteresting variable-length marker */ +{ + unsigned int length; + + /* Get the marker parameter length count */ + length = read_2_bytes(); + /* Length includes itself, so must be at least 2 */ + if (length < 2) + ERREXIT("Erroneous JPEG marker length"); + length -= 2; + /* Skip over the remaining bytes */ + while (length > 0) { + (void) read_1_byte(); + length--; + } +} + + +/* + * Process a COM marker. + * We want to print out the marker contents as legible text; + * we must guard against non-text junk and varying newline representations. + */ + +static void +process_COM (int raw) +{ + unsigned int length; + int ch; + int lastch = 0; + + /* Bill Allombert: set locale properly for isprint */ +#ifdef HAVE_LOCALE_H + setlocale(LC_CTYPE, ""); +#endif + + /* Get the marker parameter length count */ + length = read_2_bytes(); + /* Length includes itself, so must be at least 2 */ + if (length < 2) + ERREXIT("Erroneous JPEG marker length"); + length -= 2; + + while (length > 0) { + ch = read_1_byte(); + if (raw) { + putc(ch, stdout); + /* Emit the character in a readable form. + * Nonprintables are converted to \nnn form, + * while \ is converted to \\. + * Newlines in CR, CR/LF, or LF form will be printed as one newline. + */ + } else if (ch == '\r') { + printf("\n"); + } else if (ch == '\n') { + if (lastch != '\r') + printf("\n"); + } else if (ch == '\\') { + printf("\\\\"); + } else if (isprint(ch)) { + putc(ch, stdout); + } else { + printf("\\%03o", ch); + } + lastch = ch; + length--; + } + printf("\n"); + + /* Bill Allombert: revert to C locale */ +#ifdef HAVE_LOCALE_H + setlocale(LC_CTYPE, "C"); +#endif +} + + +/* + * Process a SOFn marker. + * This code is only needed if you want to know the image dimensions... + */ + +static void +process_SOFn (int marker) +{ + unsigned int length; + unsigned int image_height, image_width; + int data_precision, num_components; + const char * process; + int ci; + + length = read_2_bytes(); /* usual parameter length count */ + + data_precision = read_1_byte(); + image_height = read_2_bytes(); + image_width = read_2_bytes(); + num_components = read_1_byte(); + + switch (marker) { + case M_SOF0: process = "Baseline"; break; + case M_SOF1: process = "Extended sequential"; break; + case M_SOF2: process = "Progressive"; break; + case M_SOF3: process = "Lossless"; break; + case M_SOF5: process = "Differential sequential"; break; + case M_SOF6: process = "Differential progressive"; break; + case M_SOF7: process = "Differential lossless"; break; + case M_SOF9: process = "Extended sequential, arithmetic coding"; break; + case M_SOF10: process = "Progressive, arithmetic coding"; break; + case M_SOF11: process = "Lossless, arithmetic coding"; break; + case M_SOF13: process = "Differential sequential, arithmetic coding"; break; + case M_SOF14: process = "Differential progressive, arithmetic coding"; break; + case M_SOF15: process = "Differential lossless, arithmetic coding"; break; + default: process = "Unknown"; break; + } + + printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n", + image_width, image_height, num_components, data_precision); + printf("JPEG process: %s\n", process); + + if (length != (unsigned int) (8 + num_components * 3)) + ERREXIT("Bogus SOF marker length"); + + for (ci = 0; ci < num_components; ci++) { + (void) read_1_byte(); /* Component ID code */ + (void) read_1_byte(); /* H, V sampling factors */ + (void) read_1_byte(); /* Quantization table number */ + } +} + + +/* + * Parse the marker stream until SOS or EOI is seen; + * display any COM markers. + * While the companion program wrjpgcom will always insert COM markers before + * SOFn, other implementations might not, so we scan to SOS before stopping. + * If we were only interested in the image dimensions, we would stop at SOFn. + * (Conversely, if we only cared about COM markers, there would be no need + * for special code to handle SOFn; we could treat it like other markers.) + */ + +static int +scan_JPEG_header (int verbose, int raw) +{ + int marker; + + /* Expect SOI at start of file */ + if (first_marker() != M_SOI) + ERREXIT("Expected SOI marker first"); + + /* Scan miscellaneous markers until we reach SOS. */ + for (;;) { + marker = next_marker(); + switch (marker) { + /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be, + * treated as SOFn. C4 in particular is actually DHT. + */ + case M_SOF0: /* Baseline */ + case M_SOF1: /* Extended sequential, Huffman */ + case M_SOF2: /* Progressive, Huffman */ + case M_SOF3: /* Lossless, Huffman */ + case M_SOF5: /* Differential sequential, Huffman */ + case M_SOF6: /* Differential progressive, Huffman */ + case M_SOF7: /* Differential lossless, Huffman */ + case M_SOF9: /* Extended sequential, arithmetic */ + case M_SOF10: /* Progressive, arithmetic */ + case M_SOF11: /* Lossless, arithmetic */ + case M_SOF13: /* Differential sequential, arithmetic */ + case M_SOF14: /* Differential progressive, arithmetic */ + case M_SOF15: /* Differential lossless, arithmetic */ + if (verbose) + process_SOFn(marker); + else + skip_variable(); + break; + + case M_SOS: /* stop before hitting compressed data */ + return marker; + + case M_EOI: /* in case it's a tables-only JPEG stream */ + return marker; + + case M_COM: + process_COM(raw); + break; + + case M_APP12: + /* Some digital camera makers put useful textual information into + * APP12 markers, so we print those out too when in -verbose mode. + */ + if (verbose) { + printf("APP12 contains:\n"); + process_COM(raw); + } else + skip_variable(); + break; + + default: /* Anything else just gets skipped */ + skip_variable(); /* we assume it has a parameter count... */ + break; + } + } /* end loop */ +} + + +/* Command line parsing code */ + +static const char * progname; /* program name for error messages */ + + +static void +usage (void) +/* complain about bad command line */ +{ + fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n"); + + fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname); + + fprintf(stderr, "Switches (names may be abbreviated):\n"); + fprintf(stderr, " -raw Display non-printable characters in comments (unsafe)\n"); + fprintf(stderr, " -verbose Also display dimensions of JPEG image\n"); + + exit(EXIT_FAILURE); +} + + +static int +keymatch (char * arg, const char * keyword, int minchars) +/* Case-insensitive matching of (possibly abbreviated) keyword switches. */ +/* keyword is the constant keyword (must be lower case already), */ +/* minchars is length of minimum legal abbreviation. */ +{ + register int ca, ck; + register int nmatched = 0; + + while ((ca = *arg++) != '\0') { + if ((ck = *keyword++) == '\0') + return 0; /* arg longer than keyword, no good */ + if (isupper(ca)) /* force arg to lcase (assume ck is already) */ + ca = tolower(ca); + if (ca != ck) + return 0; /* no good */ + nmatched++; /* count matched characters */ + } + /* reached end of argument; fail if it's too short for unique abbrev */ + if (nmatched < minchars) + return 0; + return 1; /* A-OK */ +} + + +/* + * The main program. + */ + +int +main (int argc, char **argv) +{ + int argn; + char * arg; + int verbose = 0, raw = 0; + + /* On Mac, fetch a command line. */ +#ifdef USE_CCOMMAND + argc = ccommand(&argv); +#endif + + progname = argv[0]; + if (progname == NULL || progname[0] == 0) + progname = "rdjpgcom"; /* in case C library doesn't provide it */ + + /* Parse switches, if any */ + for (argn = 1; argn < argc; argn++) { + arg = argv[argn]; + if (arg[0] != '-') + break; /* not switch, must be file name */ + arg++; /* advance over '-' */ + if (keymatch(arg, "verbose", 1)) { + verbose++; + } else if (keymatch(arg, "raw", 1)) { + raw = 1; + } else + usage(); + } + + /* Open the input file. */ + /* Unix style: expect zero or one file name */ + if (argn < argc-1) { + fprintf(stderr, "%s: only one input file\n", progname); + usage(); + } + if (argn < argc) { + if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); + exit(EXIT_FAILURE); + } + } else { + /* default input file is stdin */ +#ifdef USE_SETMODE /* need to hack file mode? */ + setmode(fileno(stdin), O_BINARY); +#endif +#ifdef USE_FDOPEN /* need to re-open in binary mode? */ + if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open stdin\n", progname); + exit(EXIT_FAILURE); + } +#else + infile = stdin; +#endif + } + + /* Scan the JPEG headers. */ + (void) scan_JPEG_header(verbose, raw); + + /* All done. */ + exit(EXIT_SUCCESS); + return 0; /* suppress no-return-value warnings */ +} diff --git a/crypto777/jpeg/rdppm.c b/crypto777/jpeg/rdppm.c new file mode 100644 index 000000000..a7570227c --- /dev/null +++ b/crypto777/jpeg/rdppm.c @@ -0,0 +1,459 @@ +/* + * rdppm.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * Modified 2009 by Bill Allombert, Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to read input images in PPM/PGM format. + * The extended 2-byte-per-sample raw PPM/PGM formats are supported. + * The PBMPLUS library is NOT required to compile this software + * (but it is highly useful as a set of PPM image manipulation programs). + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume input from + * an ordinary stdio stream. They further assume that reading begins + * at the start of the file; start_input may need work if the + * user interface has already read some data (e.g., to determine that + * the file is indeed PPM format). + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef PPM_SUPPORTED + + +/* Portions of this code are based on the PBMPLUS library, which is: +** +** Copyright (C) 1988 by Jef Poskanzer. +** +** Permission to use, copy, modify, and distribute this software and its +** documentation for any purpose and without fee is hereby granted, provided +** that the above copyright notice appear in all copies and that both that +** copyright notice and this permission notice appear in supporting +** documentation. This software is provided "as is" without express or +** implied warranty. +*/ + + +/* Macros to deal with unsigned chars as efficiently as compiler allows */ + +#ifdef HAVE_UNSIGNED_CHAR +typedef unsigned char U_CHAR; +#define UCH(x) ((int) (x)) +#else /* !HAVE_UNSIGNED_CHAR */ +#ifdef CHAR_IS_UNSIGNED +typedef char U_CHAR; +#define UCH(x) ((int) (x)) +#else +typedef char U_CHAR; +#define UCH(x) ((int) (x) & 0xFF) +#endif +#endif /* HAVE_UNSIGNED_CHAR */ + + +#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) + + +/* + * On most systems, reading individual bytes with getc() is drastically less + * efficient than buffering a row at a time with fread(). On PCs, we must + * allocate the buffer in near data space, because we are assuming small-data + * memory model, wherein fread() can't reach far memory. If you need to + * process very wide images on a PC, you might have to compile in large-memory + * model, or else replace fread() with a getc() loop --- which will be much + * slower. + */ + + +/* Private version of data source object */ + +typedef struct { + struct cjpeg_source_struct pub; /* public fields */ + + U_CHAR *iobuffer; /* non-FAR pointer to I/O buffer */ + JSAMPROW pixrow; /* FAR pointer to same */ + size_t buffer_width; /* width of I/O buffer */ + JSAMPLE *rescale; /* => maxval-remapping array, or NULL */ +} ppm_source_struct; + +typedef ppm_source_struct * ppm_source_ptr; + + +LOCAL(int) +pbm_getc (FILE * infile) +/* Read next char, skipping over any comments */ +/* A comment/newline sequence is returned as a newline */ +{ + register int ch; + + ch = getc(infile); + if (ch == '#') { + do { + ch = getc(infile); + } while (ch != '\n' && ch != EOF); + } + return ch; +} + + +LOCAL(unsigned int) +read_pbm_integer (j_compress_ptr cinfo, FILE * infile) +/* Read an unsigned decimal integer from the PPM file */ +/* Swallows one trailing character after the integer */ +/* Note that on a 16-bit-int machine, only values up to 64k can be read. */ +/* This should not be a problem in practice. */ +{ + register int ch; + register unsigned int val; + + /* Skip any leading whitespace */ + do { + ch = pbm_getc(infile); + if (ch == EOF) + ERREXIT(cinfo, JERR_INPUT_EOF); + } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); + + if (ch < '0' || ch > '9') + ERREXIT(cinfo, JERR_PPM_NONNUMERIC); + + val = ch - '0'; + while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { + val *= 10; + val += ch - '0'; + } + return val; +} + + +/* + * Read one row of pixels. + * + * We provide several different versions depending on input file format. + * In all cases, input is scaled to the size of JSAMPLE. + * + * A really fast path is provided for reading byte/sample raw files with + * maxval = MAXJSAMPLE, which is the normal case for 8-bit data. + */ + + +METHODDEF(JDIMENSION) +get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading text-format PGM files with any maxval */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + FILE * infile = source->pub.input_file; + register JSAMPROW ptr; + register JSAMPLE *rescale = source->rescale; + JDIMENSION col; + + ptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + *ptr++ = rescale[read_pbm_integer(cinfo, infile)]; + } + return 1; +} + + +METHODDEF(JDIMENSION) +get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading text-format PPM files with any maxval */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + FILE * infile = source->pub.input_file; + register JSAMPROW ptr; + register JSAMPLE *rescale = source->rescale; + JDIMENSION col; + + ptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + *ptr++ = rescale[read_pbm_integer(cinfo, infile)]; + *ptr++ = rescale[read_pbm_integer(cinfo, infile)]; + *ptr++ = rescale[read_pbm_integer(cinfo, infile)]; + } + return 1; +} + + +METHODDEF(JDIMENSION) +get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading raw-byte-format PGM files with any maxval */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + register JSAMPROW ptr; + register U_CHAR * bufferptr; + register JSAMPLE *rescale = source->rescale; + JDIMENSION col; + + if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) + ERREXIT(cinfo, JERR_INPUT_EOF); + ptr = source->pub.buffer[0]; + bufferptr = source->iobuffer; + for (col = cinfo->image_width; col > 0; col--) { + *ptr++ = rescale[UCH(*bufferptr++)]; + } + return 1; +} + + +METHODDEF(JDIMENSION) +get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading raw-byte-format PPM files with any maxval */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + register JSAMPROW ptr; + register U_CHAR * bufferptr; + register JSAMPLE *rescale = source->rescale; + JDIMENSION col; + + if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) + ERREXIT(cinfo, JERR_INPUT_EOF); + ptr = source->pub.buffer[0]; + bufferptr = source->iobuffer; + for (col = cinfo->image_width; col > 0; col--) { + *ptr++ = rescale[UCH(*bufferptr++)]; + *ptr++ = rescale[UCH(*bufferptr++)]; + *ptr++ = rescale[UCH(*bufferptr++)]; + } + return 1; +} + + +METHODDEF(JDIMENSION) +get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE. + * In this case we just read right into the JSAMPLE buffer! + * Note that same code works for PPM and PGM files. + */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + + if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) + ERREXIT(cinfo, JERR_INPUT_EOF); + return 1; +} + + +METHODDEF(JDIMENSION) +get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading raw-word-format PGM files with any maxval */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + register JSAMPROW ptr; + register U_CHAR * bufferptr; + register JSAMPLE *rescale = source->rescale; + JDIMENSION col; + + if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) + ERREXIT(cinfo, JERR_INPUT_EOF); + ptr = source->pub.buffer[0]; + bufferptr = source->iobuffer; + for (col = cinfo->image_width; col > 0; col--) { + register int temp; + temp = UCH(*bufferptr++) << 8; + temp |= UCH(*bufferptr++); + *ptr++ = rescale[temp]; + } + return 1; +} + + +METHODDEF(JDIMENSION) +get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading raw-word-format PPM files with any maxval */ +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + register JSAMPROW ptr; + register U_CHAR * bufferptr; + register JSAMPLE *rescale = source->rescale; + JDIMENSION col; + + if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) + ERREXIT(cinfo, JERR_INPUT_EOF); + ptr = source->pub.buffer[0]; + bufferptr = source->iobuffer; + for (col = cinfo->image_width; col > 0; col--) { + register int temp; + temp = UCH(*bufferptr++) << 8; + temp |= UCH(*bufferptr++); + *ptr++ = rescale[temp]; + temp = UCH(*bufferptr++) << 8; + temp |= UCH(*bufferptr++); + *ptr++ = rescale[temp]; + temp = UCH(*bufferptr++) << 8; + temp |= UCH(*bufferptr++); + *ptr++ = rescale[temp]; + } + return 1; +} + + +/* + * Read the file header; return image size and component count. + */ + +METHODDEF(void) +start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + ppm_source_ptr source = (ppm_source_ptr) sinfo; + int c; + unsigned int w, h, maxval; + boolean need_iobuffer, use_raw_buffer, need_rescale; + + if (getc(source->pub.input_file) != 'P') + ERREXIT(cinfo, JERR_PPM_NOT); + + c = getc(source->pub.input_file); /* subformat discriminator character */ + + /* detect unsupported variants (ie, PBM) before trying to read header */ + switch (c) { + case '2': /* it's a text-format PGM file */ + case '3': /* it's a text-format PPM file */ + case '5': /* it's a raw-format PGM file */ + case '6': /* it's a raw-format PPM file */ + break; + default: + ERREXIT(cinfo, JERR_PPM_NOT); + break; + } + + /* fetch the remaining header info */ + w = read_pbm_integer(cinfo, source->pub.input_file); + h = read_pbm_integer(cinfo, source->pub.input_file); + maxval = read_pbm_integer(cinfo, source->pub.input_file); + + if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ + ERREXIT(cinfo, JERR_PPM_NOT); + + cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ + cinfo->image_width = (JDIMENSION) w; + cinfo->image_height = (JDIMENSION) h; + + /* initialize flags to most common settings */ + need_iobuffer = TRUE; /* do we need an I/O buffer? */ + use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ + need_rescale = TRUE; /* do we need a rescale array? */ + + switch (c) { + case '2': /* it's a text-format PGM file */ + cinfo->input_components = 1; + cinfo->in_color_space = JCS_GRAYSCALE; + TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h); + source->pub.get_pixel_rows = get_text_gray_row; + need_iobuffer = FALSE; + break; + + case '3': /* it's a text-format PPM file */ + cinfo->input_components = 3; + cinfo->in_color_space = JCS_RGB; + TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h); + source->pub.get_pixel_rows = get_text_rgb_row; + need_iobuffer = FALSE; + break; + + case '5': /* it's a raw-format PGM file */ + cinfo->input_components = 1; + cinfo->in_color_space = JCS_GRAYSCALE; + TRACEMS2(cinfo, 1, JTRC_PGM, w, h); + if (maxval > 255) { + source->pub.get_pixel_rows = get_word_gray_row; + } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) { + source->pub.get_pixel_rows = get_raw_row; + use_raw_buffer = TRUE; + need_rescale = FALSE; + } else { + source->pub.get_pixel_rows = get_scaled_gray_row; + } + break; + + case '6': /* it's a raw-format PPM file */ + cinfo->input_components = 3; + cinfo->in_color_space = JCS_RGB; + TRACEMS2(cinfo, 1, JTRC_PPM, w, h); + if (maxval > 255) { + source->pub.get_pixel_rows = get_word_rgb_row; + } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) { + source->pub.get_pixel_rows = get_raw_row; + use_raw_buffer = TRUE; + need_rescale = FALSE; + } else { + source->pub.get_pixel_rows = get_scaled_rgb_row; + } + break; + } + + /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ + if (need_iobuffer) { + source->buffer_width = (size_t) w * cinfo->input_components * + ((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR))); + source->iobuffer = (U_CHAR *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + source->buffer_width); + } + + /* Create compressor input buffer. */ + if (use_raw_buffer) { + /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ + /* Synthesize a JSAMPARRAY pointer structure */ + /* Cast here implies near->far pointer conversion on PCs */ + source->pixrow = (JSAMPROW) source->iobuffer; + source->pub.buffer = & source->pixrow; + source->pub.buffer_height = 1; + } else { + /* Need to translate anyway, so make a separate sample buffer. */ + source->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1); + source->pub.buffer_height = 1; + } + + /* Compute the rescaling array if required. */ + if (need_rescale) { + INT32 val, half_maxval; + + /* On 16-bit-int machines we have to be careful of maxval = 65535 */ + source->rescale = (JSAMPLE *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE))); + half_maxval = maxval / 2; + for (val = 0; val <= (INT32) maxval; val++) { + /* The multiplication here must be done in 32 bits to avoid overflow */ + source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval); + } + } +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + /* no work */ +} + + +/* + * The module selection routine for PPM format input. + */ + +GLOBAL(cjpeg_source_ptr) +jinit_read_ppm (j_compress_ptr cinfo) +{ + ppm_source_ptr source; + + /* Create module interface object */ + source = (ppm_source_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(ppm_source_struct)); + /* Fill in method ptrs, except get_pixel_rows which start_input sets */ + source->pub.start_input = start_input_ppm; + source->pub.finish_input = finish_input_ppm; + + return (cjpeg_source_ptr) source; +} + +#endif /* PPM_SUPPORTED */ diff --git a/crypto777/jpeg/rdrle.c b/crypto777/jpeg/rdrle.c new file mode 100644 index 000000000..542bc3749 --- /dev/null +++ b/crypto777/jpeg/rdrle.c @@ -0,0 +1,387 @@ +/* + * rdrle.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to read input images in Utah RLE format. + * The Utah Raster Toolkit library is required (version 3.1 or later). + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume input from + * an ordinary stdio stream. They further assume that reading begins + * at the start of the file; start_input may need work if the + * user interface has already read some data (e.g., to determine that + * the file is indeed RLE format). + * + * Based on code contributed by Mike Lijewski, + * with updates from Robert Hutchinson. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef RLE_SUPPORTED + +/* rle.h is provided by the Utah Raster Toolkit. */ + +#include + +/* + * We assume that JSAMPLE has the same representation as rle_pixel, + * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples. + */ + +#if BITS_IN_JSAMPLE != 8 + Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */ +#endif + +/* + * We support the following types of RLE files: + * + * GRAYSCALE - 8 bits, no colormap + * MAPPEDGRAY - 8 bits, 1 channel colomap + * PSEUDOCOLOR - 8 bits, 3 channel colormap + * TRUECOLOR - 24 bits, 3 channel colormap + * DIRECTCOLOR - 24 bits, no colormap + * + * For now, we ignore any alpha channel in the image. + */ + +typedef enum + { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind; + + +/* + * Since RLE stores scanlines bottom-to-top, we have to invert the image + * to conform to JPEG's top-to-bottom order. To do this, we read the + * incoming image into a virtual array on the first get_pixel_rows call, + * then fetch the required row from the virtual array on subsequent calls. + */ + +typedef struct _rle_source_struct * rle_source_ptr; + +typedef struct _rle_source_struct { + struct cjpeg_source_struct pub; /* public fields */ + + rle_kind visual; /* actual type of input file */ + jvirt_sarray_ptr image; /* virtual array to hold the image */ + JDIMENSION row; /* current row # in the virtual array */ + rle_hdr header; /* Input file information */ + rle_pixel** rle_row; /* holds a row returned by rle_getrow() */ + +} rle_source_struct; + + +/* + * Read the file header; return image size and component count. + */ + +METHODDEF(void) +start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + rle_source_ptr source = (rle_source_ptr) sinfo; + JDIMENSION width, height; +#ifdef PROGRESS_REPORT + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; +#endif + + /* Use RLE library routine to get the header info */ + source->header = *rle_hdr_init(NULL); + source->header.rle_file = source->pub.input_file; + switch (rle_get_setup(&(source->header))) { + case RLE_SUCCESS: + /* A-OK */ + break; + case RLE_NOT_RLE: + ERREXIT(cinfo, JERR_RLE_NOT); + break; + case RLE_NO_SPACE: + ERREXIT(cinfo, JERR_RLE_MEM); + break; + case RLE_EMPTY: + ERREXIT(cinfo, JERR_RLE_EMPTY); + break; + case RLE_EOF: + ERREXIT(cinfo, JERR_RLE_EOF); + break; + default: + ERREXIT(cinfo, JERR_RLE_BADERROR); + break; + } + + /* Figure out what we have, set private vars and return values accordingly */ + + width = source->header.xmax - source->header.xmin + 1; + height = source->header.ymax - source->header.ymin + 1; + source->header.xmin = 0; /* realign horizontally */ + source->header.xmax = width-1; + + cinfo->image_width = width; + cinfo->image_height = height; + cinfo->data_precision = 8; /* we can only handle 8 bit data */ + + if (source->header.ncolors == 1 && source->header.ncmap == 0) { + source->visual = GRAYSCALE; + TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height); + } else if (source->header.ncolors == 1 && source->header.ncmap == 1) { + source->visual = MAPPEDGRAY; + TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height, + 1 << source->header.cmaplen); + } else if (source->header.ncolors == 1 && source->header.ncmap == 3) { + source->visual = PSEUDOCOLOR; + TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height, + 1 << source->header.cmaplen); + } else if (source->header.ncolors == 3 && source->header.ncmap == 3) { + source->visual = TRUECOLOR; + TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height, + 1 << source->header.cmaplen); + } else if (source->header.ncolors == 3 && source->header.ncmap == 0) { + source->visual = DIRECTCOLOR; + TRACEMS2(cinfo, 1, JTRC_RLE, width, height); + } else + ERREXIT(cinfo, JERR_RLE_UNSUPPORTED); + + if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) { + cinfo->in_color_space = JCS_GRAYSCALE; + cinfo->input_components = 1; + } else { + cinfo->in_color_space = JCS_RGB; + cinfo->input_components = 3; + } + + /* + * A place to hold each scanline while it's converted. + * (GRAYSCALE scanlines don't need converting) + */ + if (source->visual != GRAYSCALE) { + source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) width, (JDIMENSION) cinfo->input_components); + } + + /* request a virtual array to hold the image */ + source->image = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + (JDIMENSION) (width * source->header.ncolors), + (JDIMENSION) height, (JDIMENSION) 1); + +#ifdef PROGRESS_REPORT + if (progress != NULL) { + /* count file input as separate pass */ + progress->total_extra_passes++; + } +#endif + + source->pub.buffer_height = 1; +} + + +/* + * Read one row of pixels. + * Called only after load_image has read the image into the virtual array. + * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images. + */ + +METHODDEF(JDIMENSION) +get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + rle_source_ptr source = (rle_source_ptr) sinfo; + + source->row--; + source->pub.buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE); + + return 1; +} + +/* + * Read one row of pixels. + * Called only after load_image has read the image into the virtual array. + * Used for PSEUDOCOLOR images. + */ + +METHODDEF(JDIMENSION) +get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + rle_source_ptr source = (rle_source_ptr) sinfo; + JSAMPROW src_row, dest_row; + JDIMENSION col; + rle_map *colormap; + int val; + + colormap = source->header.cmap; + dest_row = source->pub.buffer[0]; + source->row--; + src_row = * (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE); + + for (col = cinfo->image_width; col > 0; col--) { + val = GETJSAMPLE(*src_row++); + *dest_row++ = (JSAMPLE) (colormap[val ] >> 8); + *dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8); + *dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8); + } + + return 1; +} + + +/* + * Load the image into a virtual array. We have to do this because RLE + * files start at the lower left while the JPEG standard has them starting + * in the upper left. This is called the first time we want to get a row + * of input. What we do is load the RLE data into the array and then call + * the appropriate routine to read one row from the array. Before returning, + * we set source->pub.get_pixel_rows so that subsequent calls go straight to + * the appropriate row-reading routine. + */ + +METHODDEF(JDIMENSION) +load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + rle_source_ptr source = (rle_source_ptr) sinfo; + JDIMENSION row, col; + JSAMPROW scanline, red_ptr, green_ptr, blue_ptr; + rle_pixel **rle_row; + rle_map *colormap; + char channel; +#ifdef PROGRESS_REPORT + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; +#endif + + colormap = source->header.cmap; + rle_row = source->rle_row; + + /* Read the RLE data into our virtual array. + * We assume here that (a) rle_pixel is represented the same as JSAMPLE, + * and (b) we are not on a machine where FAR pointers differ from regular. + */ + RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */ + +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_limit = cinfo->image_height; + progress->pub.pass_counter = 0; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + + switch (source->visual) { + + case GRAYSCALE: + case PSEUDOCOLOR: + for (row = 0; row < cinfo->image_height; row++) { + rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE); + rle_getrow(&source->header, rle_row); +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_counter++; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + } + break; + + case MAPPEDGRAY: + case TRUECOLOR: + for (row = 0; row < cinfo->image_height; row++) { + scanline = * (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE); + rle_row = source->rle_row; + rle_getrow(&source->header, rle_row); + + for (col = 0; col < cinfo->image_width; col++) { + for (channel = 0; channel < source->header.ncolors; channel++) { + *scanline++ = (JSAMPLE) + (colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8); + } + } + +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_counter++; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + } + break; + + case DIRECTCOLOR: + for (row = 0; row < cinfo->image_height; row++) { + scanline = * (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE); + rle_getrow(&source->header, rle_row); + + red_ptr = rle_row[0]; + green_ptr = rle_row[1]; + blue_ptr = rle_row[2]; + + for (col = cinfo->image_width; col > 0; col--) { + *scanline++ = *red_ptr++; + *scanline++ = *green_ptr++; + *scanline++ = *blue_ptr++; + } + +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_counter++; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + } + } + +#ifdef PROGRESS_REPORT + if (progress != NULL) + progress->completed_extra_passes++; +#endif + + /* Set up to call proper row-extraction routine in future */ + if (source->visual == PSEUDOCOLOR) { + source->pub.buffer = source->rle_row; + source->pub.get_pixel_rows = get_pseudocolor_row; + } else { + source->pub.get_pixel_rows = get_rle_row; + } + source->row = cinfo->image_height; + + /* And fetch the topmost (bottommost) row */ + return (*source->pub.get_pixel_rows) (cinfo, sinfo); +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + /* no work */ +} + + +/* + * The module selection routine for RLE format input. + */ + +GLOBAL(cjpeg_source_ptr) +jinit_read_rle (j_compress_ptr cinfo) +{ + rle_source_ptr source; + + /* Create module interface object */ + source = (rle_source_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(rle_source_struct)); + /* Fill in method ptrs */ + source->pub.start_input = start_input_rle; + source->pub.finish_input = finish_input_rle; + source->pub.get_pixel_rows = load_image; + + return (cjpeg_source_ptr) source; +} + +#endif /* RLE_SUPPORTED */ diff --git a/crypto777/jpeg/rdswitch.c b/crypto777/jpeg/rdswitch.c new file mode 100644 index 000000000..7a839af7a --- /dev/null +++ b/crypto777/jpeg/rdswitch.c @@ -0,0 +1,365 @@ +/* + * rdswitch.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to process some of cjpeg's more complicated + * command-line switches. Switches processed here are: + * -qtables file Read quantization tables from text file + * -scans file Read scan script from text file + * -quality N[,N,...] Set quality ratings + * -qslots N[,N,...] Set component quantization table selectors + * -sample HxV[,HxV,...] Set component sampling factors + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ +#include /* to declare isdigit(), isspace() */ + + +LOCAL(int) +text_getc (FILE * file) +/* Read next char, skipping over any comments (# to end of line) */ +/* A comment/newline sequence is returned as a newline */ +{ + register int ch; + + ch = getc(file); + if (ch == '#') { + do { + ch = getc(file); + } while (ch != '\n' && ch != EOF); + } + return ch; +} + + +LOCAL(boolean) +read_text_integer (FILE * file, long * result, int * termchar) +/* Read an unsigned decimal integer from a file, store it in result */ +/* Reads one trailing character after the integer; returns it in termchar */ +{ + register int ch; + register long val; + + /* Skip any leading whitespace, detect EOF */ + do { + ch = text_getc(file); + if (ch == EOF) { + *termchar = ch; + return FALSE; + } + } while (isspace(ch)); + + if (! isdigit(ch)) { + *termchar = ch; + return FALSE; + } + + val = ch - '0'; + while ((ch = text_getc(file)) != EOF) { + if (! isdigit(ch)) + break; + val *= 10; + val += ch - '0'; + } + *result = val; + *termchar = ch; + return TRUE; +} + + +GLOBAL(boolean) +read_quant_tables (j_compress_ptr cinfo, char * filename, boolean force_baseline) +/* Read a set of quantization tables from the specified file. + * The file is plain ASCII text: decimal numbers with whitespace between. + * Comments preceded by '#' may be included in the file. + * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values. + * The tables are implicitly numbered 0,1,etc. + * NOTE: does not affect the qslots mapping, which will default to selecting + * table 0 for luminance (or primary) components, 1 for chrominance components. + * You must use -qslots if you want a different component->table mapping. + */ +{ + FILE * fp; + int tblno, i, termchar; + long val; + unsigned int table[DCTSIZE2]; + + if ((fp = fopen(filename, "r")) == NULL) { + fprintf(stderr, "Can't open table file %s\n", filename); + return FALSE; + } + tblno = 0; + + while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */ + if (tblno >= NUM_QUANT_TBLS) { + fprintf(stderr, "Too many tables in file %s\n", filename); + fclose(fp); + return FALSE; + } + table[0] = (unsigned int) val; + for (i = 1; i < DCTSIZE2; i++) { + if (! read_text_integer(fp, &val, &termchar)) { + fprintf(stderr, "Invalid table data in file %s\n", filename); + fclose(fp); + return FALSE; + } + table[i] = (unsigned int) val; + } + jpeg_add_quant_table(cinfo, tblno, table, cinfo->q_scale_factor[tblno], + force_baseline); + tblno++; + } + + if (termchar != EOF) { + fprintf(stderr, "Non-numeric data in file %s\n", filename); + fclose(fp); + return FALSE; + } + + fclose(fp); + return TRUE; +} + + +#ifdef C_MULTISCAN_FILES_SUPPORTED + +LOCAL(boolean) +read_scan_integer (FILE * file, long * result, int * termchar) +/* Variant of read_text_integer that always looks for a non-space termchar; + * this simplifies parsing of punctuation in scan scripts. + */ +{ + register int ch; + + if (! read_text_integer(file, result, termchar)) + return FALSE; + ch = *termchar; + while (ch != EOF && isspace(ch)) + ch = text_getc(file); + if (isdigit(ch)) { /* oops, put it back */ + if (ungetc(ch, file) == EOF) + return FALSE; + ch = ' '; + } else { + /* Any separators other than ';' and ':' are ignored; + * this allows user to insert commas, etc, if desired. + */ + if (ch != EOF && ch != ';' && ch != ':') + ch = ' '; + } + *termchar = ch; + return TRUE; +} + + +GLOBAL(boolean) +read_scan_script (j_compress_ptr cinfo, char * filename) +/* Read a scan script from the specified text file. + * Each entry in the file defines one scan to be emitted. + * Entries are separated by semicolons ';'. + * An entry contains one to four component indexes, + * optionally followed by a colon ':' and four progressive-JPEG parameters. + * The component indexes denote which component(s) are to be transmitted + * in the current scan. The first component has index 0. + * Sequential JPEG is used if the progressive-JPEG parameters are omitted. + * The file is free format text: any whitespace may appear between numbers + * and the ':' and ';' punctuation marks. Also, other punctuation (such + * as commas or dashes) can be placed between numbers if desired. + * Comments preceded by '#' may be included in the file. + * Note: we do very little validity checking here; + * jcmaster.c will validate the script parameters. + */ +{ + FILE * fp; + int scanno, ncomps, termchar; + long val; + jpeg_scan_info * scanptr; +#define MAX_SCANS 100 /* quite arbitrary limit */ + jpeg_scan_info scans[MAX_SCANS]; + + if ((fp = fopen(filename, "r")) == NULL) { + fprintf(stderr, "Can't open scan definition file %s\n", filename); + return FALSE; + } + scanptr = scans; + scanno = 0; + + while (read_scan_integer(fp, &val, &termchar)) { + if (scanno >= MAX_SCANS) { + fprintf(stderr, "Too many scans defined in file %s\n", filename); + fclose(fp); + return FALSE; + } + scanptr->component_index[0] = (int) val; + ncomps = 1; + while (termchar == ' ') { + if (ncomps >= MAX_COMPS_IN_SCAN) { + fprintf(stderr, "Too many components in one scan in file %s\n", + filename); + fclose(fp); + return FALSE; + } + if (! read_scan_integer(fp, &val, &termchar)) + goto bogus; + scanptr->component_index[ncomps] = (int) val; + ncomps++; + } + scanptr->comps_in_scan = ncomps; + if (termchar == ':') { + if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') + goto bogus; + scanptr->Ss = (int) val; + if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') + goto bogus; + scanptr->Se = (int) val; + if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') + goto bogus; + scanptr->Ah = (int) val; + if (! read_scan_integer(fp, &val, &termchar)) + goto bogus; + scanptr->Al = (int) val; + } else { + /* set non-progressive parameters */ + scanptr->Ss = 0; + scanptr->Se = DCTSIZE2-1; + scanptr->Ah = 0; + scanptr->Al = 0; + } + if (termchar != ';' && termchar != EOF) { +bogus: + fprintf(stderr, "Invalid scan entry format in file %s\n", filename); + fclose(fp); + return FALSE; + } + scanptr++, scanno++; + } + + if (termchar != EOF) { + fprintf(stderr, "Non-numeric data in file %s\n", filename); + fclose(fp); + return FALSE; + } + + if (scanno > 0) { + /* Stash completed scan list in cinfo structure. + * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data, + * but if you want to compress multiple images you'd want JPOOL_PERMANENT. + */ + scanptr = (jpeg_scan_info *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + scanno * SIZEOF(jpeg_scan_info)); + MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info)); + cinfo->scan_info = scanptr; + cinfo->num_scans = scanno; + } + + fclose(fp); + return TRUE; +} + +#endif /* C_MULTISCAN_FILES_SUPPORTED */ + + +GLOBAL(boolean) +set_quality_ratings (j_compress_ptr cinfo, char *arg, boolean force_baseline) +/* Process a quality-ratings parameter string, of the form + * N[,N,...] + * If there are more q-table slots than parameters, the last value is replicated. + */ +{ + int val = 75; /* default value */ + int tblno; + char ch; + + for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { + if (*arg) { + ch = ','; /* if not set by sscanf, will be ',' */ + if (sscanf(arg, "%d%c", &val, &ch) < 1) + return FALSE; + if (ch != ',') /* syntax check */ + return FALSE; + /* Convert user 0-100 rating to percentage scaling */ + cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val); + while (*arg && *arg++ != ',') /* advance to next segment of arg string */ + ; + } else { + /* reached end of parameter, set remaining factors to last value */ + cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val); + } + } + jpeg_default_qtables(cinfo, force_baseline); + return TRUE; +} + + +GLOBAL(boolean) +set_quant_slots (j_compress_ptr cinfo, char *arg) +/* Process a quantization-table-selectors parameter string, of the form + * N[,N,...] + * If there are more components than parameters, the last value is replicated. + */ +{ + int val = 0; /* default table # */ + int ci; + char ch; + + for (ci = 0; ci < MAX_COMPONENTS; ci++) { + if (*arg) { + ch = ','; /* if not set by sscanf, will be ',' */ + if (sscanf(arg, "%d%c", &val, &ch) < 1) + return FALSE; + if (ch != ',') /* syntax check */ + return FALSE; + if (val < 0 || val >= NUM_QUANT_TBLS) { + fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n", + NUM_QUANT_TBLS-1); + return FALSE; + } + cinfo->comp_info[ci].quant_tbl_no = val; + while (*arg && *arg++ != ',') /* advance to next segment of arg string */ + ; + } else { + /* reached end of parameter, set remaining components to last table */ + cinfo->comp_info[ci].quant_tbl_no = val; + } + } + return TRUE; +} + + +GLOBAL(boolean) +set_sample_factors (j_compress_ptr cinfo, char *arg) +/* Process a sample-factors parameter string, of the form + * HxV[,HxV,...] + * If there are more components than parameters, "1x1" is assumed for the rest. + */ +{ + int ci, val1, val2; + char ch1, ch2; + + for (ci = 0; ci < MAX_COMPONENTS; ci++) { + if (*arg) { + ch2 = ','; /* if not set by sscanf, will be ',' */ + if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3) + return FALSE; + if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */ + return FALSE; + if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) { + fprintf(stderr, "JPEG sampling factors must be 1..4\n"); + return FALSE; + } + cinfo->comp_info[ci].h_samp_factor = val1; + cinfo->comp_info[ci].v_samp_factor = val2; + while (*arg && *arg++ != ',') /* advance to next segment of arg string */ + ; + } else { + /* reached end of parameter, set remaining components to 1x1 sampling */ + cinfo->comp_info[ci].h_samp_factor = 1; + cinfo->comp_info[ci].v_samp_factor = 1; + } + } + return TRUE; +} diff --git a/crypto777/jpeg/rdtarga.c b/crypto777/jpeg/rdtarga.c new file mode 100644 index 000000000..4c2cd2673 --- /dev/null +++ b/crypto777/jpeg/rdtarga.c @@ -0,0 +1,500 @@ +/* + * rdtarga.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to read input images in Targa format. + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume input from + * an ordinary stdio stream. They further assume that reading begins + * at the start of the file; start_input may need work if the + * user interface has already read some data (e.g., to determine that + * the file is indeed Targa format). + * + * Based on code contributed by Lee Daniel Crocker. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef TARGA_SUPPORTED + + +/* Macros to deal with unsigned chars as efficiently as compiler allows */ + +#ifdef HAVE_UNSIGNED_CHAR +typedef unsigned char U_CHAR; +#define UCH(x) ((int) (x)) +#else /* !HAVE_UNSIGNED_CHAR */ +#ifdef CHAR_IS_UNSIGNED +typedef char U_CHAR; +#define UCH(x) ((int) (x)) +#else +typedef char U_CHAR; +#define UCH(x) ((int) (x) & 0xFF) +#endif +#endif /* HAVE_UNSIGNED_CHAR */ + + +#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) + + +/* Private version of data source object */ + +typedef struct _tga_source_struct * tga_source_ptr; + +typedef struct _tga_source_struct { + struct cjpeg_source_struct pub; /* public fields */ + + j_compress_ptr cinfo; /* back link saves passing separate parm */ + + JSAMPARRAY colormap; /* Targa colormap (converted to my format) */ + + jvirt_sarray_ptr whole_image; /* Needed if funny input row order */ + JDIMENSION current_row; /* Current logical row number to read */ + + /* Pointer to routine to extract next Targa pixel from input file */ + JMETHOD(void, read_pixel, (tga_source_ptr sinfo)); + + /* Result of read_pixel is delivered here: */ + U_CHAR tga_pixel[4]; + + int pixel_size; /* Bytes per Targa pixel (1 to 4) */ + + /* State info for reading RLE-coded pixels; both counts must be init to 0 */ + int block_count; /* # of pixels remaining in RLE block */ + int dup_pixel_count; /* # of times to duplicate previous pixel */ + + /* This saves the correct pixel-row-expansion method for preload_image */ + JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo, + cjpeg_source_ptr sinfo)); +} tga_source_struct; + + +/* For expanding 5-bit pixel values to 8-bit with best rounding */ + +static const UINT8 c5to8bits[32] = { + 0, 8, 16, 25, 33, 41, 49, 58, + 66, 74, 82, 90, 99, 107, 115, 123, + 132, 140, 148, 156, 165, 173, 181, 189, + 197, 206, 214, 222, 230, 239, 247, 255 +}; + + + +LOCAL(int) +read_byte (tga_source_ptr sinfo) +/* Read next byte from Targa file */ +{ + register FILE *infile = sinfo->pub.input_file; + register int c; + + if ((c = getc(infile)) == EOF) + ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); + return c; +} + + +LOCAL(void) +read_colormap (tga_source_ptr sinfo, int cmaplen, int mapentrysize) +/* Read the colormap from a Targa file */ +{ + int i; + + /* Presently only handles 24-bit BGR format */ + if (mapentrysize != 24) + ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP); + + for (i = 0; i < cmaplen; i++) { + sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo); + sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo); + sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo); + } +} + + +/* + * read_pixel methods: get a single pixel from Targa file into tga_pixel[] + */ + +METHODDEF(void) +read_non_rle_pixel (tga_source_ptr sinfo) +/* Read one Targa pixel from the input file; no RLE expansion */ +{ + register FILE *infile = sinfo->pub.input_file; + register int i; + + for (i = 0; i < sinfo->pixel_size; i++) { + sinfo->tga_pixel[i] = (U_CHAR) getc(infile); + } +} + + +METHODDEF(void) +read_rle_pixel (tga_source_ptr sinfo) +/* Read one Targa pixel from the input file, expanding RLE data as needed */ +{ + register FILE *infile = sinfo->pub.input_file; + register int i; + + /* Duplicate previously read pixel? */ + if (sinfo->dup_pixel_count > 0) { + sinfo->dup_pixel_count--; + return; + } + + /* Time to read RLE block header? */ + if (--sinfo->block_count < 0) { /* decrement pixels remaining in block */ + i = read_byte(sinfo); + if (i & 0x80) { /* Start of duplicate-pixel block? */ + sinfo->dup_pixel_count = i & 0x7F; /* number of dups after this one */ + sinfo->block_count = 0; /* then read new block header */ + } else { + sinfo->block_count = i & 0x7F; /* number of pixels after this one */ + } + } + + /* Read next pixel */ + for (i = 0; i < sinfo->pixel_size; i++) { + sinfo->tga_pixel[i] = (U_CHAR) getc(infile); + } +} + + +/* + * Read one row of pixels. + * + * We provide several different versions depending on input file format. + */ + + +METHODDEF(JDIMENSION) +get_8bit_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 8-bit grayscale pixels */ +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + register JSAMPROW ptr; + register JDIMENSION col; + + ptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + (*source->read_pixel) (source); /* Load next pixel into tga_pixel */ + *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]); + } + return 1; +} + +METHODDEF(JDIMENSION) +get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 8-bit colormap indexes */ +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + register int t; + register JSAMPROW ptr; + register JDIMENSION col; + register JSAMPARRAY colormap = source->colormap; + + ptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + (*source->read_pixel) (source); /* Load next pixel into tga_pixel */ + t = UCH(source->tga_pixel[0]); + *ptr++ = colormap[0][t]; + *ptr++ = colormap[1][t]; + *ptr++ = colormap[2][t]; + } + return 1; +} + +METHODDEF(JDIMENSION) +get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 16-bit pixels */ +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + register int t; + register JSAMPROW ptr; + register JDIMENSION col; + + ptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + (*source->read_pixel) (source); /* Load next pixel into tga_pixel */ + t = UCH(source->tga_pixel[0]); + t += UCH(source->tga_pixel[1]) << 8; + /* We expand 5 bit data to 8 bit sample width. + * The format of the 16-bit (LSB first) input word is + * xRRRRRGGGGGBBBBB + */ + ptr[2] = (JSAMPLE) c5to8bits[t & 0x1F]; + t >>= 5; + ptr[1] = (JSAMPLE) c5to8bits[t & 0x1F]; + t >>= 5; + ptr[0] = (JSAMPLE) c5to8bits[t & 0x1F]; + ptr += 3; + } + return 1; +} + +METHODDEF(JDIMENSION) +get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +/* This version is for reading 24-bit pixels */ +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + register JSAMPROW ptr; + register JDIMENSION col; + + ptr = source->pub.buffer[0]; + for (col = cinfo->image_width; col > 0; col--) { + (*source->read_pixel) (source); /* Load next pixel into tga_pixel */ + *ptr++ = (JSAMPLE) UCH(source->tga_pixel[2]); /* change BGR to RGB order */ + *ptr++ = (JSAMPLE) UCH(source->tga_pixel[1]); + *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]); + } + return 1; +} + +/* + * Targa also defines a 32-bit pixel format with order B,G,R,A. + * We presently ignore the attribute byte, so the code for reading + * these pixels is identical to the 24-bit routine above. + * This works because the actual pixel length is only known to read_pixel. + */ + +#define get_32bit_row get_24bit_row + + +/* + * This method is for re-reading the input data in standard top-down + * row order. The entire image has already been read into whole_image + * with proper conversion of pixel format, but it's in a funny row order. + */ + +METHODDEF(JDIMENSION) +get_memory_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + JDIMENSION source_row; + + /* Compute row of source that maps to current_row of normal order */ + /* For now, assume image is bottom-up and not interlaced. */ + /* NEEDS WORK to support interlaced images! */ + source_row = cinfo->image_height - source->current_row - 1; + + /* Fetch that row from virtual array */ + source->pub.buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->whole_image, + source_row, (JDIMENSION) 1, FALSE); + + source->current_row++; + return 1; +} + + +/* + * This method loads the image into whole_image during the first call on + * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call + * get_memory_row on subsequent calls. + */ + +METHODDEF(JDIMENSION) +preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + JDIMENSION row; + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; + + /* Read the data into a virtual array in input-file row order. */ + for (row = 0; row < cinfo->image_height; row++) { + if (progress != NULL) { + progress->pub.pass_counter = (long) row; + progress->pub.pass_limit = (long) cinfo->image_height; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } + source->pub.buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, source->whole_image, row, (JDIMENSION) 1, TRUE); + (*source->get_pixel_rows) (cinfo, sinfo); + } + if (progress != NULL) + progress->completed_extra_passes++; + + /* Set up to read from the virtual array in unscrambled order */ + source->pub.get_pixel_rows = get_memory_row; + source->current_row = 0; + /* And read the first row */ + return get_memory_row(cinfo, sinfo); +} + + +/* + * Read the file header; return image size and component count. + */ + +METHODDEF(void) +start_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + tga_source_ptr source = (tga_source_ptr) sinfo; + U_CHAR targaheader[18]; + int idlen, cmaptype, subtype, flags, interlace_type, components; + unsigned int width, height, maplen; + boolean is_bottom_up; + +#define GET_2B(offset) ((unsigned int) UCH(targaheader[offset]) + \ + (((unsigned int) UCH(targaheader[offset+1])) << 8)) + + if (! ReadOK(source->pub.input_file, targaheader, 18)) + ERREXIT(cinfo, JERR_INPUT_EOF); + + /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */ + if (targaheader[16] == 15) + targaheader[16] = 16; + + idlen = UCH(targaheader[0]); + cmaptype = UCH(targaheader[1]); + subtype = UCH(targaheader[2]); + maplen = GET_2B(5); + width = GET_2B(12); + height = GET_2B(14); + source->pixel_size = UCH(targaheader[16]) >> 3; + flags = UCH(targaheader[17]); /* Image Descriptor byte */ + + is_bottom_up = ((flags & 0x20) == 0); /* bit 5 set => top-down */ + interlace_type = flags >> 6; /* bits 6/7 are interlace code */ + + if (cmaptype > 1 || /* cmaptype must be 0 or 1 */ + source->pixel_size < 1 || source->pixel_size > 4 || + (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */ + interlace_type != 0) /* currently don't allow interlaced image */ + ERREXIT(cinfo, JERR_TGA_BADPARMS); + + if (subtype > 8) { + /* It's an RLE-coded file */ + source->read_pixel = read_rle_pixel; + source->block_count = source->dup_pixel_count = 0; + subtype -= 8; + } else { + /* Non-RLE file */ + source->read_pixel = read_non_rle_pixel; + } + + /* Now should have subtype 1, 2, or 3 */ + components = 3; /* until proven different */ + cinfo->in_color_space = JCS_RGB; + + switch (subtype) { + case 1: /* Colormapped image */ + if (source->pixel_size == 1 && cmaptype == 1) + source->get_pixel_rows = get_8bit_row; + else + ERREXIT(cinfo, JERR_TGA_BADPARMS); + TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height); + break; + case 2: /* RGB image */ + switch (source->pixel_size) { + case 2: + source->get_pixel_rows = get_16bit_row; + break; + case 3: + source->get_pixel_rows = get_24bit_row; + break; + case 4: + source->get_pixel_rows = get_32bit_row; + break; + default: + ERREXIT(cinfo, JERR_TGA_BADPARMS); + break; + } + TRACEMS2(cinfo, 1, JTRC_TGA, width, height); + break; + case 3: /* Grayscale image */ + components = 1; + cinfo->in_color_space = JCS_GRAYSCALE; + if (source->pixel_size == 1) + source->get_pixel_rows = get_8bit_gray_row; + else + ERREXIT(cinfo, JERR_TGA_BADPARMS); + TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height); + break; + default: + ERREXIT(cinfo, JERR_TGA_BADPARMS); + break; + } + + if (is_bottom_up) { + /* Create a virtual array to buffer the upside-down image. */ + source->whole_image = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + (JDIMENSION) width * components, (JDIMENSION) height, (JDIMENSION) 1); + if (cinfo->progress != NULL) { + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; + progress->total_extra_passes++; /* count file input as separate pass */ + } + /* source->pub.buffer will point to the virtual array. */ + source->pub.buffer_height = 1; /* in case anyone looks at it */ + source->pub.get_pixel_rows = preload_image; + } else { + /* Don't need a virtual array, but do need a one-row input buffer. */ + source->whole_image = NULL; + source->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + (JDIMENSION) width * components, (JDIMENSION) 1); + source->pub.buffer_height = 1; + source->pub.get_pixel_rows = source->get_pixel_rows; + } + + while (idlen--) /* Throw away ID field */ + (void) read_byte(source); + + if (maplen > 0) { + if (maplen > 256 || GET_2B(3) != 0) + ERREXIT(cinfo, JERR_TGA_BADCMAP); + /* Allocate space to store the colormap */ + source->colormap = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) maplen, (JDIMENSION) 3); + /* and read it from the file */ + read_colormap(source, (int) maplen, UCH(targaheader[7])); + } else { + if (cmaptype) /* but you promised a cmap! */ + ERREXIT(cinfo, JERR_TGA_BADPARMS); + source->colormap = NULL; + } + + cinfo->input_components = components; + cinfo->data_precision = 8; + cinfo->image_width = width; + cinfo->image_height = height; +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) +{ + /* no work */ +} + + +/* + * The module selection routine for Targa format input. + */ + +GLOBAL(cjpeg_source_ptr) +jinit_read_targa (j_compress_ptr cinfo) +{ + tga_source_ptr source; + + /* Create module interface object */ + source = (tga_source_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(tga_source_struct)); + source->cinfo = cinfo; /* make back link for subroutines */ + /* Fill in method ptrs, except get_pixel_rows which start_input sets */ + source->pub.start_input = start_input_tga; + source->pub.finish_input = finish_input_tga; + + return (cjpeg_source_ptr) source; +} + +#endif /* TARGA_SUPPORTED */ diff --git a/crypto777/jpeg/test.c b/crypto777/jpeg/test.c new file mode 100644 index 000000000..89182a396 --- /dev/null +++ b/crypto777/jpeg/test.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include "jpeglib.h" + +void write_JPEG_file(char * filename, int quality,void *image,int width,int height) +{ + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + FILE * outfile; /* target file */ + JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ + int row_stride; /* physical row width in image buffer */ + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + if ((outfile = fopen(filename, "wb")) == NULL) + { + fprintf(stderr, "can't open %s\n", filename); + exit(1); + } + jpeg_stdio_dest(&cinfo, outfile); + cinfo.image_width = width; /* image width and height, in pixels */ + cinfo.image_height = height; + cinfo.input_components = 3; /* # of color components per pixel */ + cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); + jpeg_start_compress(&cinfo, TRUE); + row_stride = width * 3; /* JSAMPLEs per row in image_buffer */ + while (cinfo.next_scanline < cinfo.image_height) + { + row_pointer[0] = &image[cinfo.next_scanline * row_stride]; + (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + jpeg_finish_compress(&cinfo); + fclose(outfile); + jpeg_destroy_compress(&cinfo); +} + + +int main() +{ + int i; unsigned char image[100][300][3]; + memset(image,0,sizeof(image)); + for (i=0; i<300; i++) + image[50][i][0] = 0x55, image[50][i][1] = 0xaa, image[50][i][2] = 0xff; + write_JPEG_file("test.jpg",50,image,300,100); + return(0); +} + diff --git a/crypto777/jpeg/transupp.c b/crypto777/jpeg/transupp.c new file mode 100644 index 000000000..016f383d4 --- /dev/null +++ b/crypto777/jpeg/transupp.c @@ -0,0 +1,1597 @@ +/* + * transupp.c + * + * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains image transformation routines and other utility code + * used by the jpegtran sample application. These are NOT part of the core + * JPEG library. But we keep these routines separate from jpegtran.c to + * ease the task of maintaining jpegtran-like programs that have other user + * interfaces. + */ + +/* Although this file really shouldn't have access to the library internals, + * it's helpful to let it call jround_up() and jcopy_block_row(). + */ +#define JPEG_INTERNALS + +#include "jinclude.h" +#include "jpeglib.h" +#include "transupp.h" /* My own external interface */ +#include /* to declare isdigit() */ + + +#if TRANSFORMS_SUPPORTED + +/* + * Lossless image transformation routines. These routines work on DCT + * coefficient arrays and thus do not require any lossy decompression + * or recompression of the image. + * Thanks to Guido Vollbeding for the initial design and code of this feature, + * and to Ben Jackson for introducing the cropping feature. + * + * Horizontal flipping is done in-place, using a single top-to-bottom + * pass through the virtual source array. It will thus be much the + * fastest option for images larger than main memory. + * + * The other routines require a set of destination virtual arrays, so they + * need twice as much memory as jpegtran normally does. The destination + * arrays are always written in normal scan order (top to bottom) because + * the virtual array manager expects this. The source arrays will be scanned + * in the corresponding order, which means multiple passes through the source + * arrays for most of the transforms. That could result in much thrashing + * if the image is larger than main memory. + * + * If cropping or trimming is involved, the destination arrays may be smaller + * than the source arrays. Note it is not possible to do horizontal flip + * in-place when a nonzero Y crop offset is specified, since we'd have to move + * data from one block row to another but the virtual array manager doesn't + * guarantee we can touch more than one row at a time. So in that case, + * we have to use a separate destination array. + * + * Some notes about the operating environment of the individual transform + * routines: + * 1. Both the source and destination virtual arrays are allocated from the + * source JPEG object, and therefore should be manipulated by calling the + * source's memory manager. + * 2. The destination's component count should be used. It may be smaller + * than the source's when forcing to grayscale. + * 3. Likewise the destination's sampling factors should be used. When + * forcing to grayscale the destination's sampling factors will be all 1, + * and we may as well take that as the effective iMCU size. + * 4. When "trim" is in effect, the destination's dimensions will be the + * trimmed values but the source's will be untrimmed. + * 5. When "crop" is in effect, the destination's dimensions will be the + * cropped values but the source's will be uncropped. Each transform + * routine is responsible for picking up source data starting at the + * correct X and Y offset for the crop region. (The X and Y offsets + * passed to the transform routines are measured in iMCU blocks of the + * destination.) + * 6. All the routines assume that the source and destination buffers are + * padded out to a full iMCU boundary. This is true, although for the + * source buffer it is an undocumented property of jdcoefct.c. + */ + + +LOCAL(void) +do_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* Crop. This is only used when no rotate/flip is requested with the crop. */ +{ + JDIMENSION dst_blk_y, x_crop_blocks, y_crop_blocks; + int ci, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + jpeg_component_info *compptr; + + /* We simply have to copy the right amount of data (the destination's + * image size) starting at the given X and Y offsets in the source. + */ + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_y + y_crop_blocks, + (JDIMENSION) compptr->v_samp_factor, FALSE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + jcopy_block_row(src_buffer[offset_y] + x_crop_blocks, + dst_buffer[offset_y], + compptr->width_in_blocks); + } + } + } +} + + +LOCAL(void) +do_flip_h_no_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, + jvirt_barray_ptr *src_coef_arrays) +/* Horizontal flip; done in-place, so no separate dest array is required. + * NB: this only works when y_crop_offset is zero. + */ +{ + JDIMENSION MCU_cols, comp_width, blk_x, blk_y, x_crop_blocks; + int ci, k, offset_y; + JBLOCKARRAY buffer; + JCOEFPTR ptr1, ptr2; + JCOEF temp1, temp2; + jpeg_component_info *compptr; + + /* Horizontal mirroring of DCT blocks is accomplished by swapping + * pairs of blocks in-place. Within a DCT block, we perform horizontal + * mirroring by changing the signs of odd-numbered columns. + * Partial iMCUs at the right edge are left untouched. + */ + MCU_cols = srcinfo->output_width / + (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_width = MCU_cols * compptr->h_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + for (blk_y = 0; blk_y < compptr->height_in_blocks; + blk_y += compptr->v_samp_factor) { + buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + /* Do the mirroring */ + for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) { + ptr1 = buffer[offset_y][blk_x]; + ptr2 = buffer[offset_y][comp_width - blk_x - 1]; + /* this unrolled loop doesn't need to know which row it's on... */ + for (k = 0; k < DCTSIZE2; k += 2) { + temp1 = *ptr1; /* swap even column */ + temp2 = *ptr2; + *ptr1++ = temp2; + *ptr2++ = temp1; + temp1 = *ptr1; /* swap odd column with sign change */ + temp2 = *ptr2; + *ptr1++ = -temp2; + *ptr2++ = -temp1; + } + } + if (x_crop_blocks > 0) { + /* Now left-justify the portion of the data to be kept. + * We can't use a single jcopy_block_row() call because that routine + * depends on memcpy(), whose behavior is unspecified for overlapping + * source and destination areas. Sigh. + */ + for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) { + jcopy_block_row(buffer[offset_y] + blk_x + x_crop_blocks, + buffer[offset_y] + blk_x, + (JDIMENSION) 1); + } + } + } + } + } +} + + +LOCAL(void) +do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* Horizontal flip in general cropping case */ +{ + JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y; + JDIMENSION x_crop_blocks, y_crop_blocks; + int ci, k, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JBLOCKROW src_row_ptr, dst_row_ptr; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + /* Here we must output into a separate array because we can't touch + * different rows of a single virtual array simultaneously. Otherwise, + * this is essentially the same as the routine above. + */ + MCU_cols = srcinfo->output_width / + (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_width = MCU_cols * compptr->h_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_y + y_crop_blocks, + (JDIMENSION) compptr->v_samp_factor, FALSE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + dst_row_ptr = dst_buffer[offset_y]; + src_row_ptr = src_buffer[offset_y]; + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) { + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Do the mirrorable blocks */ + dst_ptr = dst_row_ptr[dst_blk_x]; + src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1]; + /* this unrolled loop doesn't need to know which row it's on... */ + for (k = 0; k < DCTSIZE2; k += 2) { + *dst_ptr++ = *src_ptr++; /* copy even column */ + *dst_ptr++ = - *src_ptr++; /* copy odd column with sign change */ + } + } else { + /* Copy last partial block(s) verbatim */ + jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks, + dst_row_ptr + dst_blk_x, + (JDIMENSION) 1); + } + } + } + } + } +} + + +LOCAL(void) +do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* Vertical flip */ +{ + JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y; + JDIMENSION x_crop_blocks, y_crop_blocks; + int ci, i, j, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JBLOCKROW src_row_ptr, dst_row_ptr; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + /* We output into a separate array because we can't touch different + * rows of the source virtual array simultaneously. Otherwise, this + * is a pretty straightforward analog of horizontal flip. + * Within a DCT block, vertical mirroring is done by changing the signs + * of odd-numbered rows. + * Partial iMCUs at the bottom edge are copied verbatim. + */ + MCU_rows = srcinfo->output_height / + (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_height = MCU_rows * compptr->v_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + if (y_crop_blocks + dst_blk_y < comp_height) { + /* Row is within the mirrorable area. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + comp_height - y_crop_blocks - dst_blk_y - + (JDIMENSION) compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, FALSE); + } else { + /* Bottom-edge blocks will be copied verbatim. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_y + y_crop_blocks, + (JDIMENSION) compptr->v_samp_factor, FALSE); + } + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + if (y_crop_blocks + dst_blk_y < comp_height) { + /* Row is within the mirrorable area. */ + dst_row_ptr = dst_buffer[offset_y]; + src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1]; + src_row_ptr += x_crop_blocks; + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; + dst_blk_x++) { + dst_ptr = dst_row_ptr[dst_blk_x]; + src_ptr = src_row_ptr[dst_blk_x]; + for (i = 0; i < DCTSIZE; i += 2) { + /* copy even row */ + for (j = 0; j < DCTSIZE; j++) + *dst_ptr++ = *src_ptr++; + /* copy odd row with sign change */ + for (j = 0; j < DCTSIZE; j++) + *dst_ptr++ = - *src_ptr++; + } + } + } else { + /* Just copy row verbatim. */ + jcopy_block_row(src_buffer[offset_y] + x_crop_blocks, + dst_buffer[offset_y], + compptr->width_in_blocks); + } + } + } + } +} + + +LOCAL(void) +do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* Transpose source into destination */ +{ + JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks; + int ci, i, j, offset_x, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + /* Transposing pixels within a block just requires transposing the + * DCT coefficients. + * Partial iMCUs at the edges require no special treatment; we simply + * process all the available DCT blocks for every component. + */ + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; + dst_blk_x += compptr->h_samp_factor) { + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_x + x_crop_blocks, + (JDIMENSION) compptr->h_samp_factor, FALSE); + for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { + dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; + src_ptr = src_buffer[offset_x][dst_blk_y + offset_y + y_crop_blocks]; + for (i = 0; i < DCTSIZE; i++) + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + } + } + } + } + } +} + + +LOCAL(void) +do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* 90 degree rotation is equivalent to + * 1. Transposing the image; + * 2. Horizontal mirroring. + * These two steps are merged into a single processing routine. + */ +{ + JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y; + JDIMENSION x_crop_blocks, y_crop_blocks; + int ci, i, j, offset_x, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + /* Because of the horizontal mirror step, we can't process partial iMCUs + * at the (output) right edge properly. They just get transposed and + * not mirrored. + */ + MCU_cols = srcinfo->output_height / + (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_width = MCU_cols * compptr->h_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; + dst_blk_x += compptr->h_samp_factor) { + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Block is within the mirrorable area. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + comp_width - x_crop_blocks - dst_blk_x - + (JDIMENSION) compptr->h_samp_factor, + (JDIMENSION) compptr->h_samp_factor, FALSE); + } else { + /* Edge blocks are transposed but not mirrored. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_x + x_crop_blocks, + (JDIMENSION) compptr->h_samp_factor, FALSE); + } + for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { + dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Block is within the mirrorable area. */ + src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1] + [dst_blk_y + offset_y + y_crop_blocks]; + for (i = 0; i < DCTSIZE; i++) { + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + i++; + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; + } + } else { + /* Edge blocks are transposed but not mirrored. */ + src_ptr = src_buffer[offset_x] + [dst_blk_y + offset_y + y_crop_blocks]; + for (i = 0; i < DCTSIZE; i++) + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + } + } + } + } + } + } +} + + +LOCAL(void) +do_rot_270 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* 270 degree rotation is equivalent to + * 1. Horizontal mirroring; + * 2. Transposing the image. + * These two steps are merged into a single processing routine. + */ +{ + JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y; + JDIMENSION x_crop_blocks, y_crop_blocks; + int ci, i, j, offset_x, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + /* Because of the horizontal mirror step, we can't process partial iMCUs + * at the (output) bottom edge properly. They just get transposed and + * not mirrored. + */ + MCU_rows = srcinfo->output_width / + (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_height = MCU_rows * compptr->v_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; + dst_blk_x += compptr->h_samp_factor) { + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_x + x_crop_blocks, + (JDIMENSION) compptr->h_samp_factor, FALSE); + for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { + dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; + if (y_crop_blocks + dst_blk_y < comp_height) { + /* Block is within the mirrorable area. */ + src_ptr = src_buffer[offset_x] + [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1]; + for (i = 0; i < DCTSIZE; i++) { + for (j = 0; j < DCTSIZE; j++) { + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + j++; + dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; + } + } + } else { + /* Edge blocks are transposed but not mirrored. */ + src_ptr = src_buffer[offset_x] + [dst_blk_y + offset_y + y_crop_blocks]; + for (i = 0; i < DCTSIZE; i++) + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + } + } + } + } + } + } +} + + +LOCAL(void) +do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* 180 degree rotation is equivalent to + * 1. Vertical mirroring; + * 2. Horizontal mirroring. + * These two steps are merged into a single processing routine. + */ +{ + JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y; + JDIMENSION x_crop_blocks, y_crop_blocks; + int ci, i, j, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JBLOCKROW src_row_ptr, dst_row_ptr; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + MCU_cols = srcinfo->output_width / + (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); + MCU_rows = srcinfo->output_height / + (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_width = MCU_cols * compptr->h_samp_factor; + comp_height = MCU_rows * compptr->v_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + if (y_crop_blocks + dst_blk_y < comp_height) { + /* Row is within the vertically mirrorable area. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + comp_height - y_crop_blocks - dst_blk_y - + (JDIMENSION) compptr->v_samp_factor, + (JDIMENSION) compptr->v_samp_factor, FALSE); + } else { + /* Bottom-edge rows are only mirrored horizontally. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_y + y_crop_blocks, + (JDIMENSION) compptr->v_samp_factor, FALSE); + } + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + dst_row_ptr = dst_buffer[offset_y]; + if (y_crop_blocks + dst_blk_y < comp_height) { + /* Row is within the mirrorable area. */ + src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1]; + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) { + dst_ptr = dst_row_ptr[dst_blk_x]; + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Process the blocks that can be mirrored both ways. */ + src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1]; + for (i = 0; i < DCTSIZE; i += 2) { + /* For even row, negate every odd column. */ + for (j = 0; j < DCTSIZE; j += 2) { + *dst_ptr++ = *src_ptr++; + *dst_ptr++ = - *src_ptr++; + } + /* For odd row, negate every even column. */ + for (j = 0; j < DCTSIZE; j += 2) { + *dst_ptr++ = - *src_ptr++; + *dst_ptr++ = *src_ptr++; + } + } + } else { + /* Any remaining right-edge blocks are only mirrored vertically. */ + src_ptr = src_row_ptr[x_crop_blocks + dst_blk_x]; + for (i = 0; i < DCTSIZE; i += 2) { + for (j = 0; j < DCTSIZE; j++) + *dst_ptr++ = *src_ptr++; + for (j = 0; j < DCTSIZE; j++) + *dst_ptr++ = - *src_ptr++; + } + } + } + } else { + /* Remaining rows are just mirrored horizontally. */ + src_row_ptr = src_buffer[offset_y]; + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) { + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Process the blocks that can be mirrored. */ + dst_ptr = dst_row_ptr[dst_blk_x]; + src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1]; + for (i = 0; i < DCTSIZE2; i += 2) { + *dst_ptr++ = *src_ptr++; + *dst_ptr++ = - *src_ptr++; + } + } else { + /* Any remaining right-edge blocks are only copied. */ + jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks, + dst_row_ptr + dst_blk_x, + (JDIMENSION) 1); + } + } + } + } + } + } +} + + +LOCAL(void) +do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, + jvirt_barray_ptr *src_coef_arrays, + jvirt_barray_ptr *dst_coef_arrays) +/* Transverse transpose is equivalent to + * 1. 180 degree rotation; + * 2. Transposition; + * or + * 1. Horizontal mirroring; + * 2. Transposition; + * 3. Horizontal mirroring. + * These steps are merged into a single processing routine. + */ +{ + JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y; + JDIMENSION x_crop_blocks, y_crop_blocks; + int ci, i, j, offset_x, offset_y; + JBLOCKARRAY src_buffer, dst_buffer; + JCOEFPTR src_ptr, dst_ptr; + jpeg_component_info *compptr; + + MCU_cols = srcinfo->output_height / + (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); + MCU_rows = srcinfo->output_width / + (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); + + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + comp_width = MCU_cols * compptr->h_samp_factor; + comp_height = MCU_rows * compptr->v_samp_factor; + x_crop_blocks = x_crop_offset * compptr->h_samp_factor; + y_crop_blocks = y_crop_offset * compptr->v_samp_factor; + for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; + dst_blk_y += compptr->v_samp_factor) { + dst_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, + (JDIMENSION) compptr->v_samp_factor, TRUE); + for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { + for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; + dst_blk_x += compptr->h_samp_factor) { + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Block is within the mirrorable area. */ + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + comp_width - x_crop_blocks - dst_blk_x - + (JDIMENSION) compptr->h_samp_factor, + (JDIMENSION) compptr->h_samp_factor, FALSE); + } else { + src_buffer = (*srcinfo->mem->access_virt_barray) + ((j_common_ptr) srcinfo, src_coef_arrays[ci], + dst_blk_x + x_crop_blocks, + (JDIMENSION) compptr->h_samp_factor, FALSE); + } + for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { + dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; + if (y_crop_blocks + dst_blk_y < comp_height) { + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Block is within the mirrorable area. */ + src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1] + [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1]; + for (i = 0; i < DCTSIZE; i++) { + for (j = 0; j < DCTSIZE; j++) { + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + j++; + dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; + } + i++; + for (j = 0; j < DCTSIZE; j++) { + dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; + j++; + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + } + } + } else { + /* Right-edge blocks are mirrored in y only */ + src_ptr = src_buffer[offset_x] + [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1]; + for (i = 0; i < DCTSIZE; i++) { + for (j = 0; j < DCTSIZE; j++) { + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + j++; + dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; + } + } + } + } else { + if (x_crop_blocks + dst_blk_x < comp_width) { + /* Bottom-edge blocks are mirrored in x only */ + src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1] + [dst_blk_y + offset_y + y_crop_blocks]; + for (i = 0; i < DCTSIZE; i++) { + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + i++; + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; + } + } else { + /* At lower right corner, just transpose, no mirroring */ + src_ptr = src_buffer[offset_x] + [dst_blk_y + offset_y + y_crop_blocks]; + for (i = 0; i < DCTSIZE; i++) + for (j = 0; j < DCTSIZE; j++) + dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; + } + } + } + } + } + } + } +} + + +/* Parse an unsigned integer: subroutine for jtransform_parse_crop_spec. + * Returns TRUE if valid integer found, FALSE if not. + * *strptr is advanced over the digit string, and *result is set to its value. + */ + +LOCAL(boolean) +jt_read_integer (const char ** strptr, JDIMENSION * result) +{ + const char * ptr = *strptr; + JDIMENSION val = 0; + + for (; isdigit(*ptr); ptr++) { + val = val * 10 + (JDIMENSION) (*ptr - '0'); + } + *result = val; + if (ptr == *strptr) + return FALSE; /* oops, no digits */ + *strptr = ptr; + return TRUE; +} + + +/* Parse a crop specification (written in X11 geometry style). + * The routine returns TRUE if the spec string is valid, FALSE if not. + * + * The crop spec string should have the format + * [f]x[f]{+-}{+-} + * where width, height, xoffset, and yoffset are unsigned integers. + * Each of the elements can be omitted to indicate a default value. + * (A weakness of this style is that it is not possible to omit xoffset + * while specifying yoffset, since they look alike.) + * + * This code is loosely based on XParseGeometry from the X11 distribution. + */ + +GLOBAL(boolean) +jtransform_parse_crop_spec (jpeg_transform_info *info, const char *spec) +{ + info->crop = FALSE; + info->crop_width_set = JCROP_UNSET; + info->crop_height_set = JCROP_UNSET; + info->crop_xoffset_set = JCROP_UNSET; + info->crop_yoffset_set = JCROP_UNSET; + + if (isdigit(*spec)) { + /* fetch width */ + if (! jt_read_integer(&spec, &info->crop_width)) + return FALSE; + if (*spec == 'f' || *spec == 'F') { + spec++; + info->crop_width_set = JCROP_FORCE; + } else + info->crop_width_set = JCROP_POS; + } + if (*spec == 'x' || *spec == 'X') { + /* fetch height */ + spec++; + if (! jt_read_integer(&spec, &info->crop_height)) + return FALSE; + if (*spec == 'f' || *spec == 'F') { + spec++; + info->crop_height_set = JCROP_FORCE; + } else + info->crop_height_set = JCROP_POS; + } + if (*spec == '+' || *spec == '-') { + /* fetch xoffset */ + info->crop_xoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS; + spec++; + if (! jt_read_integer(&spec, &info->crop_xoffset)) + return FALSE; + } + if (*spec == '+' || *spec == '-') { + /* fetch yoffset */ + info->crop_yoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS; + spec++; + if (! jt_read_integer(&spec, &info->crop_yoffset)) + return FALSE; + } + /* We had better have gotten to the end of the string. */ + if (*spec != '\0') + return FALSE; + info->crop = TRUE; + return TRUE; +} + + +/* Trim off any partial iMCUs on the indicated destination edge */ + +LOCAL(void) +trim_right_edge (jpeg_transform_info *info, JDIMENSION full_width) +{ + JDIMENSION MCU_cols; + + MCU_cols = info->output_width / info->iMCU_sample_width; + if (MCU_cols > 0 && info->x_crop_offset + MCU_cols == + full_width / info->iMCU_sample_width) + info->output_width = MCU_cols * info->iMCU_sample_width; +} + +LOCAL(void) +trim_bottom_edge (jpeg_transform_info *info, JDIMENSION full_height) +{ + JDIMENSION MCU_rows; + + MCU_rows = info->output_height / info->iMCU_sample_height; + if (MCU_rows > 0 && info->y_crop_offset + MCU_rows == + full_height / info->iMCU_sample_height) + info->output_height = MCU_rows * info->iMCU_sample_height; +} + + +/* Request any required workspace. + * + * This routine figures out the size that the output image will be + * (which implies that all the transform parameters must be set before + * it is called). + * + * We allocate the workspace virtual arrays from the source decompression + * object, so that all the arrays (both the original data and the workspace) + * will be taken into account while making memory management decisions. + * Hence, this routine must be called after jpeg_read_header (which reads + * the image dimensions) and before jpeg_read_coefficients (which realizes + * the source's virtual arrays). + * + * This function returns FALSE right away if -perfect is given + * and transformation is not perfect. Otherwise returns TRUE. + */ + +GLOBAL(boolean) +jtransform_request_workspace (j_decompress_ptr srcinfo, + jpeg_transform_info *info) +{ + jvirt_barray_ptr *coef_arrays; + boolean need_workspace, transpose_it; + jpeg_component_info *compptr; + JDIMENSION xoffset, yoffset; + JDIMENSION width_in_iMCUs, height_in_iMCUs; + JDIMENSION width_in_blocks, height_in_blocks; + int ci, h_samp_factor, v_samp_factor; + + /* Determine number of components in output image */ + if (info->force_grayscale && + srcinfo->jpeg_color_space == JCS_YCbCr && + srcinfo->num_components == 3) + /* We'll only process the first component */ + info->num_components = 1; + else + /* Process all the components */ + info->num_components = srcinfo->num_components; + + /* Compute output image dimensions and related values. */ + jpeg_core_output_dimensions(srcinfo); + + /* Return right away if -perfect is given and transformation is not perfect. + */ + if (info->perfect) { + if (info->num_components == 1) { + if (!jtransform_perfect_transform(srcinfo->output_width, + srcinfo->output_height, + srcinfo->min_DCT_h_scaled_size, + srcinfo->min_DCT_v_scaled_size, + info->transform)) + return FALSE; + } else { + if (!jtransform_perfect_transform(srcinfo->output_width, + srcinfo->output_height, + srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size, + srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size, + info->transform)) + return FALSE; + } + } + + /* If there is only one output component, force the iMCU size to be 1; + * else use the source iMCU size. (This allows us to do the right thing + * when reducing color to grayscale, and also provides a handy way of + * cleaning up "funny" grayscale images whose sampling factors are not 1x1.) + */ + switch (info->transform) { + case JXFORM_TRANSPOSE: + case JXFORM_TRANSVERSE: + case JXFORM_ROT_90: + case JXFORM_ROT_270: + info->output_width = srcinfo->output_height; + info->output_height = srcinfo->output_width; + if (info->num_components == 1) { + info->iMCU_sample_width = srcinfo->min_DCT_v_scaled_size; + info->iMCU_sample_height = srcinfo->min_DCT_h_scaled_size; + } else { + info->iMCU_sample_width = + srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size; + info->iMCU_sample_height = + srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size; + } + break; + default: + info->output_width = srcinfo->output_width; + info->output_height = srcinfo->output_height; + if (info->num_components == 1) { + info->iMCU_sample_width = srcinfo->min_DCT_h_scaled_size; + info->iMCU_sample_height = srcinfo->min_DCT_v_scaled_size; + } else { + info->iMCU_sample_width = + srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size; + info->iMCU_sample_height = + srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size; + } + break; + } + + /* If cropping has been requested, compute the crop area's position and + * dimensions, ensuring that its upper left corner falls at an iMCU boundary. + */ + if (info->crop) { + /* Insert default values for unset crop parameters */ + if (info->crop_xoffset_set == JCROP_UNSET) + info->crop_xoffset = 0; /* default to +0 */ + if (info->crop_yoffset_set == JCROP_UNSET) + info->crop_yoffset = 0; /* default to +0 */ + if (info->crop_xoffset >= info->output_width || + info->crop_yoffset >= info->output_height) + ERREXIT(srcinfo, JERR_BAD_CROP_SPEC); + if (info->crop_width_set == JCROP_UNSET) + info->crop_width = info->output_width - info->crop_xoffset; + if (info->crop_height_set == JCROP_UNSET) + info->crop_height = info->output_height - info->crop_yoffset; + /* Ensure parameters are valid */ + if (info->crop_width <= 0 || info->crop_width > info->output_width || + info->crop_height <= 0 || info->crop_height > info->output_height || + info->crop_xoffset > info->output_width - info->crop_width || + info->crop_yoffset > info->output_height - info->crop_height) + ERREXIT(srcinfo, JERR_BAD_CROP_SPEC); + /* Convert negative crop offsets into regular offsets */ + if (info->crop_xoffset_set == JCROP_NEG) + xoffset = info->output_width - info->crop_width - info->crop_xoffset; + else + xoffset = info->crop_xoffset; + if (info->crop_yoffset_set == JCROP_NEG) + yoffset = info->output_height - info->crop_height - info->crop_yoffset; + else + yoffset = info->crop_yoffset; + /* Now adjust so that upper left corner falls at an iMCU boundary */ + if (info->crop_width_set == JCROP_FORCE) + info->output_width = info->crop_width; + else + info->output_width = + info->crop_width + (xoffset % info->iMCU_sample_width); + if (info->crop_height_set == JCROP_FORCE) + info->output_height = info->crop_height; + else + info->output_height = + info->crop_height + (yoffset % info->iMCU_sample_height); + /* Save x/y offsets measured in iMCUs */ + info->x_crop_offset = xoffset / info->iMCU_sample_width; + info->y_crop_offset = yoffset / info->iMCU_sample_height; + } else { + info->x_crop_offset = 0; + info->y_crop_offset = 0; + } + + /* Figure out whether we need workspace arrays, + * and if so whether they are transposed relative to the source. + */ + need_workspace = FALSE; + transpose_it = FALSE; + switch (info->transform) { + case JXFORM_NONE: + if (info->x_crop_offset != 0 || info->y_crop_offset != 0) + need_workspace = TRUE; + /* No workspace needed if neither cropping nor transforming */ + break; + case JXFORM_FLIP_H: + if (info->trim) + trim_right_edge(info, srcinfo->output_width); + if (info->y_crop_offset != 0) + need_workspace = TRUE; + /* do_flip_h_no_crop doesn't need a workspace array */ + break; + case JXFORM_FLIP_V: + if (info->trim) + trim_bottom_edge(info, srcinfo->output_height); + /* Need workspace arrays having same dimensions as source image. */ + need_workspace = TRUE; + break; + case JXFORM_TRANSPOSE: + /* transpose does NOT have to trim anything */ + /* Need workspace arrays having transposed dimensions. */ + need_workspace = TRUE; + transpose_it = TRUE; + break; + case JXFORM_TRANSVERSE: + if (info->trim) { + trim_right_edge(info, srcinfo->output_height); + trim_bottom_edge(info, srcinfo->output_width); + } + /* Need workspace arrays having transposed dimensions. */ + need_workspace = TRUE; + transpose_it = TRUE; + break; + case JXFORM_ROT_90: + if (info->trim) + trim_right_edge(info, srcinfo->output_height); + /* Need workspace arrays having transposed dimensions. */ + need_workspace = TRUE; + transpose_it = TRUE; + break; + case JXFORM_ROT_180: + if (info->trim) { + trim_right_edge(info, srcinfo->output_width); + trim_bottom_edge(info, srcinfo->output_height); + } + /* Need workspace arrays having same dimensions as source image. */ + need_workspace = TRUE; + break; + case JXFORM_ROT_270: + if (info->trim) + trim_bottom_edge(info, srcinfo->output_width); + /* Need workspace arrays having transposed dimensions. */ + need_workspace = TRUE; + transpose_it = TRUE; + break; + } + + /* Allocate workspace if needed. + * Note that we allocate arrays padded out to the next iMCU boundary, + * so that transform routines need not worry about missing edge blocks. + */ + if (need_workspace) { + coef_arrays = (jvirt_barray_ptr *) + (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE, + SIZEOF(jvirt_barray_ptr) * info->num_components); + width_in_iMCUs = (JDIMENSION) + jdiv_round_up((long) info->output_width, + (long) info->iMCU_sample_width); + height_in_iMCUs = (JDIMENSION) + jdiv_round_up((long) info->output_height, + (long) info->iMCU_sample_height); + for (ci = 0; ci < info->num_components; ci++) { + compptr = srcinfo->comp_info + ci; + if (info->num_components == 1) { + /* we're going to force samp factors to 1x1 in this case */ + h_samp_factor = v_samp_factor = 1; + } else if (transpose_it) { + h_samp_factor = compptr->v_samp_factor; + v_samp_factor = compptr->h_samp_factor; + } else { + h_samp_factor = compptr->h_samp_factor; + v_samp_factor = compptr->v_samp_factor; + } + width_in_blocks = width_in_iMCUs * h_samp_factor; + height_in_blocks = height_in_iMCUs * v_samp_factor; + coef_arrays[ci] = (*srcinfo->mem->request_virt_barray) + ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE, + width_in_blocks, height_in_blocks, (JDIMENSION) v_samp_factor); + } + info->workspace_coef_arrays = coef_arrays; + } else + info->workspace_coef_arrays = NULL; + + return TRUE; +} + + +/* Transpose destination image parameters */ + +LOCAL(void) +transpose_critical_parameters (j_compress_ptr dstinfo) +{ + int tblno, i, j, ci, itemp; + jpeg_component_info *compptr; + JQUANT_TBL *qtblptr; + JDIMENSION jtemp; + UINT16 qtemp; + + /* Transpose image dimensions */ + jtemp = dstinfo->image_width; + dstinfo->image_width = dstinfo->image_height; + dstinfo->image_height = jtemp; + itemp = dstinfo->min_DCT_h_scaled_size; + dstinfo->min_DCT_h_scaled_size = dstinfo->min_DCT_v_scaled_size; + dstinfo->min_DCT_v_scaled_size = itemp; + + /* Transpose sampling factors */ + for (ci = 0; ci < dstinfo->num_components; ci++) { + compptr = dstinfo->comp_info + ci; + itemp = compptr->h_samp_factor; + compptr->h_samp_factor = compptr->v_samp_factor; + compptr->v_samp_factor = itemp; + } + + /* Transpose quantization tables */ + for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { + qtblptr = dstinfo->quant_tbl_ptrs[tblno]; + if (qtblptr != NULL) { + for (i = 0; i < DCTSIZE; i++) { + for (j = 0; j < i; j++) { + qtemp = qtblptr->quantval[i*DCTSIZE+j]; + qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i]; + qtblptr->quantval[j*DCTSIZE+i] = qtemp; + } + } + } + } +} + + +/* Adjust Exif image parameters. + * + * We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible. + */ + +LOCAL(void) +adjust_exif_parameters (JOCTET FAR * data, unsigned int length, + JDIMENSION new_width, JDIMENSION new_height) +{ + boolean is_motorola; /* Flag for byte order */ + unsigned int number_of_tags, tagnum; + unsigned int firstoffset, offset; + JDIMENSION new_value; + + if (length < 12) return; /* Length of an IFD entry */ + + /* Discover byte order */ + if (GETJOCTET(data[0]) == 0x49 && GETJOCTET(data[1]) == 0x49) + is_motorola = FALSE; + else if (GETJOCTET(data[0]) == 0x4D && GETJOCTET(data[1]) == 0x4D) + is_motorola = TRUE; + else + return; + + /* Check Tag Mark */ + if (is_motorola) { + if (GETJOCTET(data[2]) != 0) return; + if (GETJOCTET(data[3]) != 0x2A) return; + } else { + if (GETJOCTET(data[3]) != 0) return; + if (GETJOCTET(data[2]) != 0x2A) return; + } + + /* Get first IFD offset (offset to IFD0) */ + if (is_motorola) { + if (GETJOCTET(data[4]) != 0) return; + if (GETJOCTET(data[5]) != 0) return; + firstoffset = GETJOCTET(data[6]); + firstoffset <<= 8; + firstoffset += GETJOCTET(data[7]); + } else { + if (GETJOCTET(data[7]) != 0) return; + if (GETJOCTET(data[6]) != 0) return; + firstoffset = GETJOCTET(data[5]); + firstoffset <<= 8; + firstoffset += GETJOCTET(data[4]); + } + if (firstoffset > length - 2) return; /* check end of data segment */ + + /* Get the number of directory entries contained in this IFD */ + if (is_motorola) { + number_of_tags = GETJOCTET(data[firstoffset]); + number_of_tags <<= 8; + number_of_tags += GETJOCTET(data[firstoffset+1]); + } else { + number_of_tags = GETJOCTET(data[firstoffset+1]); + number_of_tags <<= 8; + number_of_tags += GETJOCTET(data[firstoffset]); + } + if (number_of_tags == 0) return; + firstoffset += 2; + + /* Search for ExifSubIFD offset Tag in IFD0 */ + for (;;) { + if (firstoffset > length - 12) return; /* check end of data segment */ + /* Get Tag number */ + if (is_motorola) { + tagnum = GETJOCTET(data[firstoffset]); + tagnum <<= 8; + tagnum += GETJOCTET(data[firstoffset+1]); + } else { + tagnum = GETJOCTET(data[firstoffset+1]); + tagnum <<= 8; + tagnum += GETJOCTET(data[firstoffset]); + } + if (tagnum == 0x8769) break; /* found ExifSubIFD offset Tag */ + if (--number_of_tags == 0) return; + firstoffset += 12; + } + + /* Get the ExifSubIFD offset */ + if (is_motorola) { + if (GETJOCTET(data[firstoffset+8]) != 0) return; + if (GETJOCTET(data[firstoffset+9]) != 0) return; + offset = GETJOCTET(data[firstoffset+10]); + offset <<= 8; + offset += GETJOCTET(data[firstoffset+11]); + } else { + if (GETJOCTET(data[firstoffset+11]) != 0) return; + if (GETJOCTET(data[firstoffset+10]) != 0) return; + offset = GETJOCTET(data[firstoffset+9]); + offset <<= 8; + offset += GETJOCTET(data[firstoffset+8]); + } + if (offset > length - 2) return; /* check end of data segment */ + + /* Get the number of directory entries contained in this SubIFD */ + if (is_motorola) { + number_of_tags = GETJOCTET(data[offset]); + number_of_tags <<= 8; + number_of_tags += GETJOCTET(data[offset+1]); + } else { + number_of_tags = GETJOCTET(data[offset+1]); + number_of_tags <<= 8; + number_of_tags += GETJOCTET(data[offset]); + } + if (number_of_tags < 2) return; + offset += 2; + + /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */ + do { + if (offset > length - 12) return; /* check end of data segment */ + /* Get Tag number */ + if (is_motorola) { + tagnum = GETJOCTET(data[offset]); + tagnum <<= 8; + tagnum += GETJOCTET(data[offset+1]); + } else { + tagnum = GETJOCTET(data[offset+1]); + tagnum <<= 8; + tagnum += GETJOCTET(data[offset]); + } + if (tagnum == 0xA002 || tagnum == 0xA003) { + if (tagnum == 0xA002) + new_value = new_width; /* ExifImageWidth Tag */ + else + new_value = new_height; /* ExifImageHeight Tag */ + if (is_motorola) { + data[offset+2] = 0; /* Format = unsigned long (4 octets) */ + data[offset+3] = 4; + data[offset+4] = 0; /* Number Of Components = 1 */ + data[offset+5] = 0; + data[offset+6] = 0; + data[offset+7] = 1; + data[offset+8] = 0; + data[offset+9] = 0; + data[offset+10] = (JOCTET)((new_value >> 8) & 0xFF); + data[offset+11] = (JOCTET)(new_value & 0xFF); + } else { + data[offset+2] = 4; /* Format = unsigned long (4 octets) */ + data[offset+3] = 0; + data[offset+4] = 1; /* Number Of Components = 1 */ + data[offset+5] = 0; + data[offset+6] = 0; + data[offset+7] = 0; + data[offset+8] = (JOCTET)(new_value & 0xFF); + data[offset+9] = (JOCTET)((new_value >> 8) & 0xFF); + data[offset+10] = 0; + data[offset+11] = 0; + } + } + offset += 12; + } while (--number_of_tags); +} + + +/* Adjust output image parameters as needed. + * + * This must be called after jpeg_copy_critical_parameters() + * and before jpeg_write_coefficients(). + * + * The return value is the set of virtual coefficient arrays to be written + * (either the ones allocated by jtransform_request_workspace, or the + * original source data arrays). The caller will need to pass this value + * to jpeg_write_coefficients(). + */ + +GLOBAL(jvirt_barray_ptr *) +jtransform_adjust_parameters (j_decompress_ptr srcinfo, + j_compress_ptr dstinfo, + jvirt_barray_ptr *src_coef_arrays, + jpeg_transform_info *info) +{ + /* If force-to-grayscale is requested, adjust destination parameters */ + if (info->force_grayscale) { + /* First, ensure we have YCbCr or grayscale data, and that the source's + * Y channel is full resolution. (No reasonable person would make Y + * be less than full resolution, so actually coping with that case + * isn't worth extra code space. But we check it to avoid crashing.) + */ + if (((dstinfo->jpeg_color_space == JCS_YCbCr && + dstinfo->num_components == 3) || + (dstinfo->jpeg_color_space == JCS_GRAYSCALE && + dstinfo->num_components == 1)) && + srcinfo->comp_info[0].h_samp_factor == srcinfo->max_h_samp_factor && + srcinfo->comp_info[0].v_samp_factor == srcinfo->max_v_samp_factor) { + /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed + * properly. Among other things, it sets the target h_samp_factor & + * v_samp_factor to 1, which typically won't match the source. + * We have to preserve the source's quantization table number, however. + */ + int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no; + jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE); + dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no; + } else { + /* Sorry, can't do it */ + ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL); + } + } else if (info->num_components == 1) { + /* For a single-component source, we force the destination sampling factors + * to 1x1, with or without force_grayscale. This is useful because some + * decoders choke on grayscale images with other sampling factors. + */ + dstinfo->comp_info[0].h_samp_factor = 1; + dstinfo->comp_info[0].v_samp_factor = 1; + } + + /* Correct the destination's image dimensions as necessary + * for rotate/flip, resize, and crop operations. + */ + dstinfo->jpeg_width = info->output_width; + dstinfo->jpeg_height = info->output_height; + + /* Transpose destination image parameters */ + switch (info->transform) { + case JXFORM_TRANSPOSE: + case JXFORM_TRANSVERSE: + case JXFORM_ROT_90: + case JXFORM_ROT_270: + transpose_critical_parameters(dstinfo); + break; + default: + break; + } + + /* Adjust Exif properties */ + if (srcinfo->marker_list != NULL && + srcinfo->marker_list->marker == JPEG_APP0+1 && + srcinfo->marker_list->data_length >= 6 && + GETJOCTET(srcinfo->marker_list->data[0]) == 0x45 && + GETJOCTET(srcinfo->marker_list->data[1]) == 0x78 && + GETJOCTET(srcinfo->marker_list->data[2]) == 0x69 && + GETJOCTET(srcinfo->marker_list->data[3]) == 0x66 && + GETJOCTET(srcinfo->marker_list->data[4]) == 0 && + GETJOCTET(srcinfo->marker_list->data[5]) == 0) { + /* Suppress output of JFIF marker */ + dstinfo->write_JFIF_header = FALSE; + /* Adjust Exif image parameters */ + if (dstinfo->jpeg_width != srcinfo->image_width || + dstinfo->jpeg_height != srcinfo->image_height) + /* Align data segment to start of TIFF structure for parsing */ + adjust_exif_parameters(srcinfo->marker_list->data + 6, + srcinfo->marker_list->data_length - 6, + dstinfo->jpeg_width, dstinfo->jpeg_height); + } + + /* Return the appropriate output data set */ + if (info->workspace_coef_arrays != NULL) + return info->workspace_coef_arrays; + return src_coef_arrays; +} + + +/* Execute the actual transformation, if any. + * + * This must be called *after* jpeg_write_coefficients, because it depends + * on jpeg_write_coefficients to have computed subsidiary values such as + * the per-component width and height fields in the destination object. + * + * Note that some transformations will modify the source data arrays! + */ + +GLOBAL(void) +jtransform_execute_transform (j_decompress_ptr srcinfo, + j_compress_ptr dstinfo, + jvirt_barray_ptr *src_coef_arrays, + jpeg_transform_info *info) +{ + jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays; + + /* Note: conditions tested here should match those in switch statement + * in jtransform_request_workspace() + */ + switch (info->transform) { + case JXFORM_NONE: + if (info->x_crop_offset != 0 || info->y_crop_offset != 0) + do_crop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + case JXFORM_FLIP_H: + if (info->y_crop_offset != 0) + do_flip_h(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + else + do_flip_h_no_crop(srcinfo, dstinfo, info->x_crop_offset, + src_coef_arrays); + break; + case JXFORM_FLIP_V: + do_flip_v(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + case JXFORM_TRANSPOSE: + do_transpose(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + case JXFORM_TRANSVERSE: + do_transverse(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + case JXFORM_ROT_90: + do_rot_90(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + case JXFORM_ROT_180: + do_rot_180(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + case JXFORM_ROT_270: + do_rot_270(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, + src_coef_arrays, dst_coef_arrays); + break; + } +} + +/* jtransform_perfect_transform + * + * Determine whether lossless transformation is perfectly + * possible for a specified image and transformation. + * + * Inputs: + * image_width, image_height: source image dimensions. + * MCU_width, MCU_height: pixel dimensions of MCU. + * transform: transformation identifier. + * Parameter sources from initialized jpeg_struct + * (after reading source header): + * image_width = cinfo.image_width + * image_height = cinfo.image_height + * MCU_width = cinfo.max_h_samp_factor * cinfo.block_size + * MCU_height = cinfo.max_v_samp_factor * cinfo.block_size + * Result: + * TRUE = perfect transformation possible + * FALSE = perfect transformation not possible + * (may use custom action then) + */ + +GLOBAL(boolean) +jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height, + int MCU_width, int MCU_height, + JXFORM_CODE transform) +{ + boolean result = TRUE; /* initialize TRUE */ + + switch (transform) { + case JXFORM_FLIP_H: + case JXFORM_ROT_270: + if (image_width % (JDIMENSION) MCU_width) + result = FALSE; + break; + case JXFORM_FLIP_V: + case JXFORM_ROT_90: + if (image_height % (JDIMENSION) MCU_height) + result = FALSE; + break; + case JXFORM_TRANSVERSE: + case JXFORM_ROT_180: + if (image_width % (JDIMENSION) MCU_width) + result = FALSE; + if (image_height % (JDIMENSION) MCU_height) + result = FALSE; + break; + default: + break; + } + + return result; +} + +#endif /* TRANSFORMS_SUPPORTED */ + + +/* Setup decompression object to save desired markers in memory. + * This must be called before jpeg_read_header() to have the desired effect. + */ + +GLOBAL(void) +jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option) +{ +#ifdef SAVE_MARKERS_SUPPORTED + int m; + + /* Save comments except under NONE option */ + if (option != JCOPYOPT_NONE) { + jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF); + } + /* Save all types of APPn markers iff ALL option */ + if (option == JCOPYOPT_ALL) { + for (m = 0; m < 16; m++) + jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF); + } +#endif /* SAVE_MARKERS_SUPPORTED */ +} + +/* Copy markers saved in the given source object to the destination object. + * This should be called just after jpeg_start_compress() or + * jpeg_write_coefficients(). + * Note that those routines will have written the SOI, and also the + * JFIF APP0 or Adobe APP14 markers if selected. + */ + +GLOBAL(void) +jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JCOPY_OPTION option) +{ + jpeg_saved_marker_ptr marker; + + /* In the current implementation, we don't actually need to examine the + * option flag here; we just copy everything that got saved. + * But to avoid confusion, we do not output JFIF and Adobe APP14 markers + * if the encoder library already wrote one. + */ + for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) { + if (dstinfo->write_JFIF_header && + marker->marker == JPEG_APP0 && + marker->data_length >= 5 && + GETJOCTET(marker->data[0]) == 0x4A && + GETJOCTET(marker->data[1]) == 0x46 && + GETJOCTET(marker->data[2]) == 0x49 && + GETJOCTET(marker->data[3]) == 0x46 && + GETJOCTET(marker->data[4]) == 0) + continue; /* reject duplicate JFIF */ + if (dstinfo->write_Adobe_marker && + marker->marker == JPEG_APP0+14 && + marker->data_length >= 5 && + GETJOCTET(marker->data[0]) == 0x41 && + GETJOCTET(marker->data[1]) == 0x64 && + GETJOCTET(marker->data[2]) == 0x6F && + GETJOCTET(marker->data[3]) == 0x62 && + GETJOCTET(marker->data[4]) == 0x65) + continue; /* reject duplicate Adobe */ +#ifdef NEED_FAR_POINTERS + /* We could use jpeg_write_marker if the data weren't FAR... */ + { + unsigned int i; + jpeg_write_m_header(dstinfo, marker->marker, marker->data_length); + for (i = 0; i < marker->data_length; i++) + jpeg_write_m_byte(dstinfo, marker->data[i]); + } +#else + jpeg_write_marker(dstinfo, marker->marker, + marker->data, marker->data_length); +#endif + } +} diff --git a/crypto777/jpeg/transupp.h b/crypto777/jpeg/transupp.h new file mode 100644 index 000000000..9aa0af385 --- /dev/null +++ b/crypto777/jpeg/transupp.h @@ -0,0 +1,213 @@ +/* + * transupp.h + * + * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains declarations for image transformation routines and + * other utility code used by the jpegtran sample application. These are + * NOT part of the core JPEG library. But we keep these routines separate + * from jpegtran.c to ease the task of maintaining jpegtran-like programs + * that have other user interfaces. + * + * NOTE: all the routines declared here have very specific requirements + * about when they are to be executed during the reading and writing of the + * source and destination files. See the comments in transupp.c, or see + * jpegtran.c for an example of correct usage. + */ + +/* If you happen not to want the image transform support, disable it here */ +#ifndef TRANSFORMS_SUPPORTED +#define TRANSFORMS_SUPPORTED 1 /* 0 disables transform code */ +#endif + +/* + * Although rotating and flipping data expressed as DCT coefficients is not + * hard, there is an asymmetry in the JPEG format specification for images + * whose dimensions aren't multiples of the iMCU size. The right and bottom + * image edges are padded out to the next iMCU boundary with junk data; but + * no padding is possible at the top and left edges. If we were to flip + * the whole image including the pad data, then pad garbage would become + * visible at the top and/or left, and real pixels would disappear into the + * pad margins --- perhaps permanently, since encoders & decoders may not + * bother to preserve DCT blocks that appear to be completely outside the + * nominal image area. So, we have to exclude any partial iMCUs from the + * basic transformation. + * + * Transpose is the only transformation that can handle partial iMCUs at the + * right and bottom edges completely cleanly. flip_h can flip partial iMCUs + * at the bottom, but leaves any partial iMCUs at the right edge untouched. + * Similarly flip_v leaves any partial iMCUs at the bottom edge untouched. + * The other transforms are defined as combinations of these basic transforms + * and process edge blocks in a way that preserves the equivalence. + * + * The "trim" option causes untransformable partial iMCUs to be dropped; + * this is not strictly lossless, but it usually gives the best-looking + * result for odd-size images. Note that when this option is active, + * the expected mathematical equivalences between the transforms may not hold. + * (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim + * followed by -rot 180 -trim trims both edges.) + * + * We also offer a lossless-crop option, which discards data outside a given + * image region but losslessly preserves what is inside. Like the rotate and + * flip transforms, lossless crop is restricted by the JPEG format: the upper + * left corner of the selected region must fall on an iMCU boundary. If this + * does not hold for the given crop parameters, we silently move the upper left + * corner up and/or left to make it so, simultaneously increasing the region + * dimensions to keep the lower right crop corner unchanged. (Thus, the + * output image covers at least the requested region, but may cover more.) + * The adjustment of the region dimensions may be optionally disabled. + * + * We also provide a lossless-resize option, which is kind of a lossless-crop + * operation in the DCT coefficient block domain - it discards higher-order + * coefficients and losslessly preserves lower-order coefficients of a + * sub-block. + * + * Rotate/flip transform, resize, and crop can be requested together in a + * single invocation. The crop is applied last --- that is, the crop region + * is specified in terms of the destination image after transform/resize. + * + * We also offer a "force to grayscale" option, which simply discards the + * chrominance channels of a YCbCr image. This is lossless in the sense that + * the luminance channel is preserved exactly. It's not the same kind of + * thing as the rotate/flip transformations, but it's convenient to handle it + * as part of this package, mainly because the transformation routines have to + * be aware of the option to know how many components to work on. + */ + + +/* Short forms of external names for systems with brain-damaged linkers. */ + +#ifdef NEED_SHORT_EXTERNAL_NAMES +#define jtransform_parse_crop_spec jTrParCrop +#define jtransform_request_workspace jTrRequest +#define jtransform_adjust_parameters jTrAdjust +#define jtransform_execute_transform jTrExec +#define jtransform_perfect_transform jTrPerfect +#define jcopy_markers_setup jCMrkSetup +#define jcopy_markers_execute jCMrkExec +#endif /* NEED_SHORT_EXTERNAL_NAMES */ + + +/* + * Codes for supported types of image transformations. + */ + +typedef enum { + JXFORM_NONE, /* no transformation */ + JXFORM_FLIP_H, /* horizontal flip */ + JXFORM_FLIP_V, /* vertical flip */ + JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */ + JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */ + JXFORM_ROT_90, /* 90-degree clockwise rotation */ + JXFORM_ROT_180, /* 180-degree rotation */ + JXFORM_ROT_270 /* 270-degree clockwise (or 90 ccw) */ +} JXFORM_CODE; + +/* + * Codes for crop parameters, which can individually be unspecified, + * positive or negative for xoffset or yoffset, + * positive or forced for width or height. + */ + +typedef enum { + JCROP_UNSET, + JCROP_POS, + JCROP_NEG, + JCROP_FORCE +} JCROP_CODE; + +/* + * Transform parameters struct. + * NB: application must not change any elements of this struct after + * calling jtransform_request_workspace. + */ + +typedef struct { + /* Options: set by caller */ + JXFORM_CODE transform; /* image transform operator */ + boolean perfect; /* if TRUE, fail if partial MCUs are requested */ + boolean trim; /* if TRUE, trim partial MCUs as needed */ + boolean force_grayscale; /* if TRUE, convert color image to grayscale */ + boolean crop; /* if TRUE, crop source image */ + + /* Crop parameters: application need not set these unless crop is TRUE. + * These can be filled in by jtransform_parse_crop_spec(). + */ + JDIMENSION crop_width; /* Width of selected region */ + JCROP_CODE crop_width_set; /* (forced disables adjustment) */ + JDIMENSION crop_height; /* Height of selected region */ + JCROP_CODE crop_height_set; /* (forced disables adjustment) */ + JDIMENSION crop_xoffset; /* X offset of selected region */ + JCROP_CODE crop_xoffset_set; /* (negative measures from right edge) */ + JDIMENSION crop_yoffset; /* Y offset of selected region */ + JCROP_CODE crop_yoffset_set; /* (negative measures from bottom edge) */ + + /* Internal workspace: caller should not touch these */ + int num_components; /* # of components in workspace */ + jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */ + JDIMENSION output_width; /* cropped destination dimensions */ + JDIMENSION output_height; + JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */ + JDIMENSION y_crop_offset; + int iMCU_sample_width; /* destination iMCU size */ + int iMCU_sample_height; +} jpeg_transform_info; + + +#if TRANSFORMS_SUPPORTED + +/* Parse a crop specification (written in X11 geometry style) */ +EXTERN(boolean) jtransform_parse_crop_spec + JPP((jpeg_transform_info *info, const char *spec)); +/* Request any required workspace */ +EXTERN(boolean) jtransform_request_workspace + JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info)); +/* Adjust output image parameters */ +EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters + JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + jvirt_barray_ptr *src_coef_arrays, + jpeg_transform_info *info)); +/* Execute the actual transformation, if any */ +EXTERN(void) jtransform_execute_transform + JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + jvirt_barray_ptr *src_coef_arrays, + jpeg_transform_info *info)); +/* Determine whether lossless transformation is perfectly + * possible for a specified image and transformation. + */ +EXTERN(boolean) jtransform_perfect_transform + JPP((JDIMENSION image_width, JDIMENSION image_height, + int MCU_width, int MCU_height, + JXFORM_CODE transform)); + +/* jtransform_execute_transform used to be called + * jtransform_execute_transformation, but some compilers complain about + * routine names that long. This macro is here to avoid breaking any + * old source code that uses the original name... + */ +#define jtransform_execute_transformation jtransform_execute_transform + +#endif /* TRANSFORMS_SUPPORTED */ + + +/* + * Support for copying optional markers from source to destination file. + */ + +typedef enum { + JCOPYOPT_NONE, /* copy no optional markers */ + JCOPYOPT_COMMENTS, /* copy only comment (COM) markers */ + JCOPYOPT_ALL /* copy all optional markers */ +} JCOPY_OPTION; + +#define JCOPYOPT_DEFAULT JCOPYOPT_COMMENTS /* recommended default */ + +/* Setup decompression object to save desired markers in memory */ +EXTERN(void) jcopy_markers_setup + JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option)); +/* Copy markers saved in the given source object to the destination object */ +EXTERN(void) jcopy_markers_execute + JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo, + JCOPY_OPTION option)); diff --git a/crypto777/jpeg/wrbmp.c b/crypto777/jpeg/wrbmp.c new file mode 100644 index 000000000..3283b0f15 --- /dev/null +++ b/crypto777/jpeg/wrbmp.c @@ -0,0 +1,442 @@ +/* + * wrbmp.c + * + * Copyright (C) 1994-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to write output images in Microsoft "BMP" + * format (MS Windows 3.x and OS/2 1.x flavors). + * Either 8-bit colormapped or 24-bit full-color format can be written. + * No compression is supported. + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume output to + * an ordinary stdio stream. + * + * This code contributed by James Arthur Boucher. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef BMP_SUPPORTED + + +/* + * To support 12-bit JPEG data, we'd have to scale output down to 8 bits. + * This is not yet implemented. + */ + +#if BITS_IN_JSAMPLE != 8 + Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */ +#endif + +/* + * Since BMP stores scanlines bottom-to-top, we have to invert the image + * from JPEG's top-to-bottom order. To do this, we save the outgoing data + * in a virtual array during put_pixel_row calls, then actually emit the + * BMP file during finish_output. The virtual array contains one JSAMPLE per + * pixel if the output is grayscale or colormapped, three if it is full color. + */ + +/* Private version of data destination object */ + +typedef struct { + struct djpeg_dest_struct pub; /* public fields */ + + boolean is_os2; /* saves the OS2 format request flag */ + + jvirt_sarray_ptr whole_image; /* needed to reverse row order */ + JDIMENSION data_width; /* JSAMPLEs per row */ + JDIMENSION row_width; /* physical width of one row in the BMP file */ + int pad_bytes; /* number of padding bytes needed per row */ + JDIMENSION cur_output_row; /* next row# to write to virtual array */ +} bmp_dest_struct; + +typedef bmp_dest_struct * bmp_dest_ptr; + + +/* Forward declarations */ +LOCAL(void) write_colormap + JPP((j_decompress_ptr cinfo, bmp_dest_ptr dest, + int map_colors, int map_entry_size)); + + +/* + * Write some pixel data. + * In this module rows_supplied will always be 1. + */ + +METHODDEF(void) +put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +/* This version is for writing 24-bit pixels */ +{ + bmp_dest_ptr dest = (bmp_dest_ptr) dinfo; + JSAMPARRAY image_ptr; + register JSAMPROW inptr, outptr; + register JDIMENSION col; + int pad; + + /* Access next row in virtual array */ + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->whole_image, + dest->cur_output_row, (JDIMENSION) 1, TRUE); + dest->cur_output_row++; + + /* Transfer data. Note destination values must be in BGR order + * (even though Microsoft's own documents say the opposite). + */ + inptr = dest->pub.buffer[0]; + outptr = image_ptr[0]; + for (col = cinfo->output_width; col > 0; col--) { + outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ + outptr[1] = *inptr++; + outptr[0] = *inptr++; + outptr += 3; + } + + /* Zero out the pad bytes. */ + pad = dest->pad_bytes; + while (--pad >= 0) + *outptr++ = 0; +} + +METHODDEF(void) +put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +/* This version is for grayscale OR quantized color output */ +{ + bmp_dest_ptr dest = (bmp_dest_ptr) dinfo; + JSAMPARRAY image_ptr; + register JSAMPROW inptr, outptr; + register JDIMENSION col; + int pad; + + /* Access next row in virtual array */ + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->whole_image, + dest->cur_output_row, (JDIMENSION) 1, TRUE); + dest->cur_output_row++; + + /* Transfer data. */ + inptr = dest->pub.buffer[0]; + outptr = image_ptr[0]; + for (col = cinfo->output_width; col > 0; col--) { + *outptr++ = *inptr++; /* can omit GETJSAMPLE() safely */ + } + + /* Zero out the pad bytes. */ + pad = dest->pad_bytes; + while (--pad >= 0) + *outptr++ = 0; +} + + +/* + * Startup: normally writes the file header. + * In this module we may as well postpone everything until finish_output. + */ + +METHODDEF(void) +start_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + /* no work here */ +} + + +/* + * Finish up at the end of the file. + * + * Here is where we really output the BMP file. + * + * First, routines to write the Windows and OS/2 variants of the file header. + */ + +LOCAL(void) +write_bmp_header (j_decompress_ptr cinfo, bmp_dest_ptr dest) +/* Write a Windows-style BMP file header, including colormap if needed */ +{ + char bmpfileheader[14]; + char bmpinfoheader[40]; +#define PUT_2B(array,offset,value) \ + (array[offset] = (char) ((value) & 0xFF), \ + array[offset+1] = (char) (((value) >> 8) & 0xFF)) +#define PUT_4B(array,offset,value) \ + (array[offset] = (char) ((value) & 0xFF), \ + array[offset+1] = (char) (((value) >> 8) & 0xFF), \ + array[offset+2] = (char) (((value) >> 16) & 0xFF), \ + array[offset+3] = (char) (((value) >> 24) & 0xFF)) + INT32 headersize, bfSize; + int bits_per_pixel, cmap_entries; + + /* Compute colormap size and total file size */ + if (cinfo->out_color_space == JCS_RGB) { + if (cinfo->quantize_colors) { + /* Colormapped RGB */ + bits_per_pixel = 8; + cmap_entries = 256; + } else { + /* Unquantized, full color RGB */ + bits_per_pixel = 24; + cmap_entries = 0; + } + } else { + /* Grayscale output. We need to fake a 256-entry colormap. */ + bits_per_pixel = 8; + cmap_entries = 256; + } + /* File size */ + headersize = 14 + 40 + cmap_entries * 4; /* Header and colormap */ + bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height; + + /* Set unused fields of header to 0 */ + MEMZERO(bmpfileheader, SIZEOF(bmpfileheader)); + MEMZERO(bmpinfoheader, SIZEOF(bmpinfoheader)); + + /* Fill the file header */ + bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */ + bmpfileheader[1] = 0x4D; + PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */ + /* we leave bfReserved1 & bfReserved2 = 0 */ + PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */ + + /* Fill the info header (Microsoft calls this a BITMAPINFOHEADER) */ + PUT_2B(bmpinfoheader, 0, 40); /* biSize */ + PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */ + PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */ + PUT_2B(bmpinfoheader, 12, 1); /* biPlanes - must be 1 */ + PUT_2B(bmpinfoheader, 14, bits_per_pixel); /* biBitCount */ + /* we leave biCompression = 0, for none */ + /* we leave biSizeImage = 0; this is correct for uncompressed data */ + if (cinfo->density_unit == 2) { /* if have density in dots/cm, then */ + PUT_4B(bmpinfoheader, 24, (INT32) (cinfo->X_density*100)); /* XPels/M */ + PUT_4B(bmpinfoheader, 28, (INT32) (cinfo->Y_density*100)); /* XPels/M */ + } + PUT_2B(bmpinfoheader, 32, cmap_entries); /* biClrUsed */ + /* we leave biClrImportant = 0 */ + + if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14) + ERREXIT(cinfo, JERR_FILE_WRITE); + if (JFWRITE(dest->pub.output_file, bmpinfoheader, 40) != (size_t) 40) + ERREXIT(cinfo, JERR_FILE_WRITE); + + if (cmap_entries > 0) + write_colormap(cinfo, dest, cmap_entries, 4); +} + + +LOCAL(void) +write_os2_header (j_decompress_ptr cinfo, bmp_dest_ptr dest) +/* Write an OS2-style BMP file header, including colormap if needed */ +{ + char bmpfileheader[14]; + char bmpcoreheader[12]; + INT32 headersize, bfSize; + int bits_per_pixel, cmap_entries; + + /* Compute colormap size and total file size */ + if (cinfo->out_color_space == JCS_RGB) { + if (cinfo->quantize_colors) { + /* Colormapped RGB */ + bits_per_pixel = 8; + cmap_entries = 256; + } else { + /* Unquantized, full color RGB */ + bits_per_pixel = 24; + cmap_entries = 0; + } + } else { + /* Grayscale output. We need to fake a 256-entry colormap. */ + bits_per_pixel = 8; + cmap_entries = 256; + } + /* File size */ + headersize = 14 + 12 + cmap_entries * 3; /* Header and colormap */ + bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height; + + /* Set unused fields of header to 0 */ + MEMZERO(bmpfileheader, SIZEOF(bmpfileheader)); + MEMZERO(bmpcoreheader, SIZEOF(bmpcoreheader)); + + /* Fill the file header */ + bmpfileheader[0] = 0x42; /* first 2 bytes are ASCII 'B', 'M' */ + bmpfileheader[1] = 0x4D; + PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */ + /* we leave bfReserved1 & bfReserved2 = 0 */ + PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */ + + /* Fill the info header (Microsoft calls this a BITMAPCOREHEADER) */ + PUT_2B(bmpcoreheader, 0, 12); /* bcSize */ + PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */ + PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */ + PUT_2B(bmpcoreheader, 8, 1); /* bcPlanes - must be 1 */ + PUT_2B(bmpcoreheader, 10, bits_per_pixel); /* bcBitCount */ + + if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14) + ERREXIT(cinfo, JERR_FILE_WRITE); + if (JFWRITE(dest->pub.output_file, bmpcoreheader, 12) != (size_t) 12) + ERREXIT(cinfo, JERR_FILE_WRITE); + + if (cmap_entries > 0) + write_colormap(cinfo, dest, cmap_entries, 3); +} + + +/* + * Write the colormap. + * Windows uses BGR0 map entries; OS/2 uses BGR entries. + */ + +LOCAL(void) +write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest, + int map_colors, int map_entry_size) +{ + JSAMPARRAY colormap = cinfo->colormap; + int num_colors = cinfo->actual_number_of_colors; + FILE * outfile = dest->pub.output_file; + int i; + + if (colormap != NULL) { + if (cinfo->out_color_components == 3) { + /* Normal case with RGB colormap */ + for (i = 0; i < num_colors; i++) { + putc(GETJSAMPLE(colormap[2][i]), outfile); + putc(GETJSAMPLE(colormap[1][i]), outfile); + putc(GETJSAMPLE(colormap[0][i]), outfile); + if (map_entry_size == 4) + putc(0, outfile); + } + } else { + /* Grayscale colormap (only happens with grayscale quantization) */ + for (i = 0; i < num_colors; i++) { + putc(GETJSAMPLE(colormap[0][i]), outfile); + putc(GETJSAMPLE(colormap[0][i]), outfile); + putc(GETJSAMPLE(colormap[0][i]), outfile); + if (map_entry_size == 4) + putc(0, outfile); + } + } + } else { + /* If no colormap, must be grayscale data. Generate a linear "map". */ + for (i = 0; i < 256; i++) { + putc(i, outfile); + putc(i, outfile); + putc(i, outfile); + if (map_entry_size == 4) + putc(0, outfile); + } + } + /* Pad colormap with zeros to ensure specified number of colormap entries */ + if (i > map_colors) + ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i); + for (; i < map_colors; i++) { + putc(0, outfile); + putc(0, outfile); + putc(0, outfile); + if (map_entry_size == 4) + putc(0, outfile); + } +} + + +METHODDEF(void) +finish_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + bmp_dest_ptr dest = (bmp_dest_ptr) dinfo; + register FILE * outfile = dest->pub.output_file; + JSAMPARRAY image_ptr; + register JSAMPROW data_ptr; + JDIMENSION row; + register JDIMENSION col; + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; + + /* Write the header and colormap */ + if (dest->is_os2) + write_os2_header(cinfo, dest); + else + write_bmp_header(cinfo, dest); + + /* Write the file body from our virtual array */ + for (row = cinfo->output_height; row > 0; row--) { + if (progress != NULL) { + progress->pub.pass_counter = (long) (cinfo->output_height - row); + progress->pub.pass_limit = (long) cinfo->output_height; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } + image_ptr = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->whole_image, row-1, (JDIMENSION) 1, FALSE); + data_ptr = image_ptr[0]; + for (col = dest->row_width; col > 0; col--) { + putc(GETJSAMPLE(*data_ptr), outfile); + data_ptr++; + } + } + if (progress != NULL) + progress->completed_extra_passes++; + + /* Make sure we wrote the output file OK */ + fflush(outfile); + if (ferror(outfile)) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + + +/* + * The module selection routine for BMP format output. + */ + +GLOBAL(djpeg_dest_ptr) +jinit_write_bmp (j_decompress_ptr cinfo, boolean is_os2) +{ + bmp_dest_ptr dest; + JDIMENSION row_width; + + /* Create module interface object, fill in method pointers */ + dest = (bmp_dest_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(bmp_dest_struct)); + dest->pub.start_output = start_output_bmp; + dest->pub.finish_output = finish_output_bmp; + dest->is_os2 = is_os2; + + if (cinfo->out_color_space == JCS_GRAYSCALE) { + dest->pub.put_pixel_rows = put_gray_rows; + } else if (cinfo->out_color_space == JCS_RGB) { + if (cinfo->quantize_colors) + dest->pub.put_pixel_rows = put_gray_rows; + else + dest->pub.put_pixel_rows = put_pixel_rows; + } else { + ERREXIT(cinfo, JERR_BMP_COLORSPACE); + } + + /* Calculate output image dimensions so we can allocate space */ + jpeg_calc_output_dimensions(cinfo); + + /* Determine width of rows in the BMP file (padded to 4-byte boundary). */ + row_width = cinfo->output_width * cinfo->output_components; + dest->data_width = row_width; + while ((row_width & 3) != 0) row_width++; + dest->row_width = row_width; + dest->pad_bytes = (int) (row_width - dest->data_width); + + /* Allocate space for inversion array, prepare for write pass */ + dest->whole_image = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + row_width, cinfo->output_height, (JDIMENSION) 1); + dest->cur_output_row = 0; + if (cinfo->progress != NULL) { + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; + progress->total_extra_passes++; /* count file input as separate pass */ + } + + /* Create decompressor output buffer. */ + dest->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, row_width, (JDIMENSION) 1); + dest->pub.buffer_height = 1; + + return (djpeg_dest_ptr) dest; +} + +#endif /* BMP_SUPPORTED */ diff --git a/crypto777/jpeg/wrgif.c b/crypto777/jpeg/wrgif.c new file mode 100644 index 000000000..5fe832839 --- /dev/null +++ b/crypto777/jpeg/wrgif.c @@ -0,0 +1,399 @@ +/* + * wrgif.c + * + * Copyright (C) 1991-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to write output images in GIF format. + * + ************************************************************************** + * NOTE: to avoid entanglements with Unisys' patent on LZW compression, * + * this code has been modified to output "uncompressed GIF" files. * + * There is no trace of the LZW algorithm in this file. * + ************************************************************************** + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume output to + * an ordinary stdio stream. + */ + +/* + * This code is loosely based on ppmtogif from the PBMPLUS distribution + * of Feb. 1991. That file contains the following copyright notice: + * Based on GIFENCODE by David Rowley . + * Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al. + * Copyright (C) 1989 by Jef Poskanzer. + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation. This software is provided "as is" without express or + * implied warranty. + * + * We are also required to state that + * "The Graphics Interchange Format(c) is the Copyright property of + * CompuServe Incorporated. GIF(sm) is a Service Mark property of + * CompuServe Incorporated." + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef GIF_SUPPORTED + + +/* Private version of data destination object */ + +typedef struct { + struct djpeg_dest_struct pub; /* public fields */ + + j_decompress_ptr cinfo; /* back link saves passing separate parm */ + + /* State for packing variable-width codes into a bitstream */ + int n_bits; /* current number of bits/code */ + int maxcode; /* maximum code, given n_bits */ + INT32 cur_accum; /* holds bits not yet output */ + int cur_bits; /* # of bits in cur_accum */ + + /* State for GIF code assignment */ + int ClearCode; /* clear code (doesn't change) */ + int EOFCode; /* EOF code (ditto) */ + int code_counter; /* counts output symbols */ + + /* GIF data packet construction buffer */ + int bytesinpkt; /* # of bytes in current packet */ + char packetbuf[256]; /* workspace for accumulating packet */ + +} gif_dest_struct; + +typedef gif_dest_struct * gif_dest_ptr; + +/* Largest value that will fit in N bits */ +#define MAXCODE(n_bits) ((1 << (n_bits)) - 1) + + +/* + * Routines to package finished data bytes into GIF data blocks. + * A data block consists of a count byte (1..255) and that many data bytes. + */ + +LOCAL(void) +flush_packet (gif_dest_ptr dinfo) +/* flush any accumulated data */ +{ + if (dinfo->bytesinpkt > 0) { /* never write zero-length packet */ + dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++; + if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt) + != (size_t) dinfo->bytesinpkt) + ERREXIT(dinfo->cinfo, JERR_FILE_WRITE); + dinfo->bytesinpkt = 0; + } +} + + +/* Add a character to current packet; flush to disk if necessary */ +#define CHAR_OUT(dinfo,c) \ + { (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \ + if ((dinfo)->bytesinpkt >= 255) \ + flush_packet(dinfo); \ + } + + +/* Routine to convert variable-width codes into a byte stream */ + +LOCAL(void) +output (gif_dest_ptr dinfo, int code) +/* Emit a code of n_bits bits */ +/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */ +{ + dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits; + dinfo->cur_bits += dinfo->n_bits; + + while (dinfo->cur_bits >= 8) { + CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); + dinfo->cur_accum >>= 8; + dinfo->cur_bits -= 8; + } +} + + +/* The pseudo-compression algorithm. + * + * In this module we simply output each pixel value as a separate symbol; + * thus, no compression occurs. In fact, there is expansion of one bit per + * pixel, because we use a symbol width one bit wider than the pixel width. + * + * GIF ordinarily uses variable-width symbols, and the decoder will expect + * to ratchet up the symbol width after a fixed number of symbols. + * To simplify the logic and keep the expansion penalty down, we emit a + * GIF Clear code to reset the decoder just before the width would ratchet up. + * Thus, all the symbols in the output file will have the same bit width. + * Note that emitting the Clear codes at the right times is a mere matter of + * counting output symbols and is in no way dependent on the LZW patent. + * + * With a small basic pixel width (low color count), Clear codes will be + * needed very frequently, causing the file to expand even more. So this + * simplistic approach wouldn't work too well on bilevel images, for example. + * But for output of JPEG conversions the pixel width will usually be 8 bits + * (129 to 256 colors), so the overhead added by Clear symbols is only about + * one symbol in every 256. + */ + +LOCAL(void) +compress_init (gif_dest_ptr dinfo, int i_bits) +/* Initialize pseudo-compressor */ +{ + /* init all the state variables */ + dinfo->n_bits = i_bits; + dinfo->maxcode = MAXCODE(dinfo->n_bits); + dinfo->ClearCode = (1 << (i_bits - 1)); + dinfo->EOFCode = dinfo->ClearCode + 1; + dinfo->code_counter = dinfo->ClearCode + 2; + /* init output buffering vars */ + dinfo->bytesinpkt = 0; + dinfo->cur_accum = 0; + dinfo->cur_bits = 0; + /* GIF specifies an initial Clear code */ + output(dinfo, dinfo->ClearCode); +} + + +LOCAL(void) +compress_pixel (gif_dest_ptr dinfo, int c) +/* Accept and "compress" one pixel value. + * The given value must be less than n_bits wide. + */ +{ + /* Output the given pixel value as a symbol. */ + output(dinfo, c); + /* Issue Clear codes often enough to keep the reader from ratcheting up + * its symbol size. + */ + if (dinfo->code_counter < dinfo->maxcode) { + dinfo->code_counter++; + } else { + output(dinfo, dinfo->ClearCode); + dinfo->code_counter = dinfo->ClearCode + 2; /* reset the counter */ + } +} + + +LOCAL(void) +compress_term (gif_dest_ptr dinfo) +/* Clean up at end */ +{ + /* Send an EOF code */ + output(dinfo, dinfo->EOFCode); + /* Flush the bit-packing buffer */ + if (dinfo->cur_bits > 0) { + CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); + } + /* Flush the packet buffer */ + flush_packet(dinfo); +} + + +/* GIF header construction */ + + +LOCAL(void) +put_word (gif_dest_ptr dinfo, unsigned int w) +/* Emit a 16-bit word, LSB first */ +{ + putc(w & 0xFF, dinfo->pub.output_file); + putc((w >> 8) & 0xFF, dinfo->pub.output_file); +} + + +LOCAL(void) +put_3bytes (gif_dest_ptr dinfo, int val) +/* Emit 3 copies of same byte value --- handy subr for colormap construction */ +{ + putc(val, dinfo->pub.output_file); + putc(val, dinfo->pub.output_file); + putc(val, dinfo->pub.output_file); +} + + +LOCAL(void) +emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap) +/* Output the GIF file header, including color map */ +/* If colormap==NULL, synthesize a gray-scale colormap */ +{ + int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte; + int cshift = dinfo->cinfo->data_precision - 8; + int i; + + if (num_colors > 256) + ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors); + /* Compute bits/pixel and related values */ + BitsPerPixel = 1; + while (num_colors > (1 << BitsPerPixel)) + BitsPerPixel++; + ColorMapSize = 1 << BitsPerPixel; + if (BitsPerPixel <= 1) + InitCodeSize = 2; + else + InitCodeSize = BitsPerPixel; + /* + * Write the GIF header. + * Note that we generate a plain GIF87 header for maximum compatibility. + */ + putc('G', dinfo->pub.output_file); + putc('I', dinfo->pub.output_file); + putc('F', dinfo->pub.output_file); + putc('8', dinfo->pub.output_file); + putc('7', dinfo->pub.output_file); + putc('a', dinfo->pub.output_file); + /* Write the Logical Screen Descriptor */ + put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); + put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); + FlagByte = 0x80; /* Yes, there is a global color table */ + FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */ + FlagByte |= (BitsPerPixel-1); /* size of global color table */ + putc(FlagByte, dinfo->pub.output_file); + putc(0, dinfo->pub.output_file); /* Background color index */ + putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */ + /* Write the Global Color Map */ + /* If the color map is more than 8 bits precision, */ + /* we reduce it to 8 bits by shifting */ + for (i=0; i < ColorMapSize; i++) { + if (i < num_colors) { + if (colormap != NULL) { + if (dinfo->cinfo->out_color_space == JCS_RGB) { + /* Normal case: RGB color map */ + putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file); + putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file); + putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file); + } else { + /* Grayscale "color map": possible if quantizing grayscale image */ + put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift); + } + } else { + /* Create a gray-scale map of num_colors values, range 0..255 */ + put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1)); + } + } else { + /* fill out the map to a power of 2 */ + put_3bytes(dinfo, 0); + } + } + /* Write image separator and Image Descriptor */ + putc(',', dinfo->pub.output_file); /* separator */ + put_word(dinfo, 0); /* left/top offset */ + put_word(dinfo, 0); + put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */ + put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); + /* flag byte: not interlaced, no local color map */ + putc(0x00, dinfo->pub.output_file); + /* Write Initial Code Size byte */ + putc(InitCodeSize, dinfo->pub.output_file); + + /* Initialize for "compression" of image data */ + compress_init(dinfo, InitCodeSize+1); +} + + +/* + * Startup: write the file header. + */ + +METHODDEF(void) +start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + gif_dest_ptr dest = (gif_dest_ptr) dinfo; + + if (cinfo->quantize_colors) + emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap); + else + emit_header(dest, 256, (JSAMPARRAY) NULL); +} + + +/* + * Write some pixel data. + * In this module rows_supplied will always be 1. + */ + +METHODDEF(void) +put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + gif_dest_ptr dest = (gif_dest_ptr) dinfo; + register JSAMPROW ptr; + register JDIMENSION col; + + ptr = dest->pub.buffer[0]; + for (col = cinfo->output_width; col > 0; col--) { + compress_pixel(dest, GETJSAMPLE(*ptr++)); + } +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + gif_dest_ptr dest = (gif_dest_ptr) dinfo; + + /* Flush "compression" mechanism */ + compress_term(dest); + /* Write a zero-length data block to end the series */ + putc(0, dest->pub.output_file); + /* Write the GIF terminator mark */ + putc(';', dest->pub.output_file); + /* Make sure we wrote the output file OK */ + fflush(dest->pub.output_file); + if (ferror(dest->pub.output_file)) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + + +/* + * The module selection routine for GIF format output. + */ + +GLOBAL(djpeg_dest_ptr) +jinit_write_gif (j_decompress_ptr cinfo) +{ + gif_dest_ptr dest; + + /* Create module interface object, fill in method pointers */ + dest = (gif_dest_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(gif_dest_struct)); + dest->cinfo = cinfo; /* make back link for subroutines */ + dest->pub.start_output = start_output_gif; + dest->pub.put_pixel_rows = put_pixel_rows; + dest->pub.finish_output = finish_output_gif; + + if (cinfo->out_color_space != JCS_GRAYSCALE && + cinfo->out_color_space != JCS_RGB) + ERREXIT(cinfo, JERR_GIF_COLORSPACE); + + /* Force quantization if color or if > 8 bits input */ + if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) { + /* Force quantization to at most 256 colors */ + cinfo->quantize_colors = TRUE; + if (cinfo->desired_number_of_colors > 256) + cinfo->desired_number_of_colors = 256; + } + + /* Calculate output image dimensions so we can allocate space */ + jpeg_calc_output_dimensions(cinfo); + + if (cinfo->output_components != 1) /* safety check: just one component? */ + ERREXIT(cinfo, JERR_GIF_BUG); + + /* Create decompressor output buffer. */ + dest->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1); + dest->pub.buffer_height = 1; + + return (djpeg_dest_ptr) dest; +} + +#endif /* GIF_SUPPORTED */ diff --git a/crypto777/jpeg/wrjpgcom.c b/crypto777/jpeg/wrjpgcom.c new file mode 100644 index 000000000..8c04b0551 --- /dev/null +++ b/crypto777/jpeg/wrjpgcom.c @@ -0,0 +1,583 @@ +/* + * wrjpgcom.c + * + * Copyright (C) 1994-1997, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains a very simple stand-alone application that inserts + * user-supplied text as a COM (comment) marker in a JFIF file. + * This may be useful as an example of the minimum logic needed to parse + * JPEG markers. + */ + +#define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */ +#include "jinclude.h" /* get auto-config symbols, */ + +#ifndef HAVE_STDLIB_H /* should declare malloc() */ +extern void * malloc (); +#endif +#include /* to declare isupper(), tolower() */ +#ifdef USE_SETMODE +#include /* to declare setmode()'s parameter macros */ +/* If you have setmode() but not , just delete this line: */ +#include /* to declare setmode() */ +#endif + +#ifdef USE_CCOMMAND /* command-line reader for Macintosh */ +#ifdef __MWERKS__ +#include /* Metrowerks needs this */ +#include /* ... and this */ +#endif +#ifdef THINK_C +#include /* Think declares it here */ +#endif +#endif + +#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */ +#define READ_BINARY "r" +#define WRITE_BINARY "w" +#else +#ifdef VMS /* VMS is very nonstandard */ +#define READ_BINARY "rb", "ctx=stm" +#define WRITE_BINARY "wb", "ctx=stm" +#else /* standard ANSI-compliant case */ +#define READ_BINARY "rb" +#define WRITE_BINARY "wb" +#endif +#endif + +#ifndef EXIT_FAILURE /* define exit() codes if not provided */ +#define EXIT_FAILURE 1 +#endif +#ifndef EXIT_SUCCESS +#ifdef VMS +#define EXIT_SUCCESS 1 /* VMS is very nonstandard */ +#else +#define EXIT_SUCCESS 0 +#endif +#endif + +/* Reduce this value if your malloc() can't allocate blocks up to 64K. + * On DOS, compiling in large model is usually a better solution. + */ + +#ifndef MAX_COM_LENGTH +#define MAX_COM_LENGTH 65000L /* must be <= 65533 in any case */ +#endif + + +/* + * These macros are used to read the input file and write the output file. + * To reuse this code in another application, you might need to change these. + */ + +static FILE * infile; /* input JPEG file */ + +/* Return next input byte, or EOF if no more */ +#define NEXTBYTE() getc(infile) + +static FILE * outfile; /* output JPEG file */ + +/* Emit an output byte */ +#define PUTBYTE(x) putc((x), outfile) + + +/* Error exit handler */ +#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE)) + + +/* Read one byte, testing for EOF */ +static int +read_1_byte (void) +{ + int c; + + c = NEXTBYTE(); + if (c == EOF) + ERREXIT("Premature EOF in JPEG file"); + return c; +} + +/* Read 2 bytes, convert to unsigned int */ +/* All 2-byte quantities in JPEG markers are MSB first */ +static unsigned int +read_2_bytes (void) +{ + int c1, c2; + + c1 = NEXTBYTE(); + if (c1 == EOF) + ERREXIT("Premature EOF in JPEG file"); + c2 = NEXTBYTE(); + if (c2 == EOF) + ERREXIT("Premature EOF in JPEG file"); + return (((unsigned int) c1) << 8) + ((unsigned int) c2); +} + + +/* Routines to write data to output file */ + +static void +write_1_byte (int c) +{ + PUTBYTE(c); +} + +static void +write_2_bytes (unsigned int val) +{ + PUTBYTE((val >> 8) & 0xFF); + PUTBYTE(val & 0xFF); +} + +static void +write_marker (int marker) +{ + PUTBYTE(0xFF); + PUTBYTE(marker); +} + +static void +copy_rest_of_file (void) +{ + int c; + + while ((c = NEXTBYTE()) != EOF) + PUTBYTE(c); +} + + +/* + * JPEG markers consist of one or more 0xFF bytes, followed by a marker + * code byte (which is not an FF). Here are the marker codes of interest + * in this program. (See jdmarker.c for a more complete list.) + */ + +#define M_SOF0 0xC0 /* Start Of Frame N */ +#define M_SOF1 0xC1 /* N indicates which compression process */ +#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */ +#define M_SOF3 0xC3 +#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */ +#define M_SOF6 0xC6 +#define M_SOF7 0xC7 +#define M_SOF9 0xC9 +#define M_SOF10 0xCA +#define M_SOF11 0xCB +#define M_SOF13 0xCD +#define M_SOF14 0xCE +#define M_SOF15 0xCF +#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */ +#define M_EOI 0xD9 /* End Of Image (end of datastream) */ +#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */ +#define M_COM 0xFE /* COMment */ + + +/* + * Find the next JPEG marker and return its marker code. + * We expect at least one FF byte, possibly more if the compressor used FFs + * to pad the file. (Padding FFs will NOT be replicated in the output file.) + * There could also be non-FF garbage between markers. The treatment of such + * garbage is unspecified; we choose to skip over it but emit a warning msg. + * NB: this routine must not be used after seeing SOS marker, since it will + * not deal correctly with FF/00 sequences in the compressed image data... + */ + +static int +next_marker (void) +{ + int c; + int discarded_bytes = 0; + + /* Find 0xFF byte; count and skip any non-FFs. */ + c = read_1_byte(); + while (c != 0xFF) { + discarded_bytes++; + c = read_1_byte(); + } + /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs + * are legal as pad bytes, so don't count them in discarded_bytes. + */ + do { + c = read_1_byte(); + } while (c == 0xFF); + + if (discarded_bytes != 0) { + fprintf(stderr, "Warning: garbage data found in JPEG file\n"); + } + + return c; +} + + +/* + * Read the initial marker, which should be SOI. + * For a JFIF file, the first two bytes of the file should be literally + * 0xFF M_SOI. To be more general, we could use next_marker, but if the + * input file weren't actually JPEG at all, next_marker might read the whole + * file and then return a misleading error message... + */ + +static int +first_marker (void) +{ + int c1, c2; + + c1 = NEXTBYTE(); + c2 = NEXTBYTE(); + if (c1 != 0xFF || c2 != M_SOI) + ERREXIT("Not a JPEG file"); + return c2; +} + + +/* + * Most types of marker are followed by a variable-length parameter segment. + * This routine skips over the parameters for any marker we don't otherwise + * want to process. + * Note that we MUST skip the parameter segment explicitly in order not to + * be fooled by 0xFF bytes that might appear within the parameter segment; + * such bytes do NOT introduce new markers. + */ + +static void +copy_variable (void) +/* Copy an unknown or uninteresting variable-length marker */ +{ + unsigned int length; + + /* Get the marker parameter length count */ + length = read_2_bytes(); + write_2_bytes(length); + /* Length includes itself, so must be at least 2 */ + if (length < 2) + ERREXIT("Erroneous JPEG marker length"); + length -= 2; + /* Skip over the remaining bytes */ + while (length > 0) { + write_1_byte(read_1_byte()); + length--; + } +} + +static void +skip_variable (void) +/* Skip over an unknown or uninteresting variable-length marker */ +{ + unsigned int length; + + /* Get the marker parameter length count */ + length = read_2_bytes(); + /* Length includes itself, so must be at least 2 */ + if (length < 2) + ERREXIT("Erroneous JPEG marker length"); + length -= 2; + /* Skip over the remaining bytes */ + while (length > 0) { + (void) read_1_byte(); + length--; + } +} + + +/* + * Parse the marker stream until SOFn or EOI is seen; + * copy data to output, but discard COM markers unless keep_COM is true. + */ + +static int +scan_JPEG_header (int keep_COM) +{ + int marker; + + /* Expect SOI at start of file */ + if (first_marker() != M_SOI) + ERREXIT("Expected SOI marker first"); + write_marker(M_SOI); + + /* Scan miscellaneous markers until we reach SOFn. */ + for (;;) { + marker = next_marker(); + switch (marker) { + /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be, + * treated as SOFn. C4 in particular is actually DHT. + */ + case M_SOF0: /* Baseline */ + case M_SOF1: /* Extended sequential, Huffman */ + case M_SOF2: /* Progressive, Huffman */ + case M_SOF3: /* Lossless, Huffman */ + case M_SOF5: /* Differential sequential, Huffman */ + case M_SOF6: /* Differential progressive, Huffman */ + case M_SOF7: /* Differential lossless, Huffman */ + case M_SOF9: /* Extended sequential, arithmetic */ + case M_SOF10: /* Progressive, arithmetic */ + case M_SOF11: /* Lossless, arithmetic */ + case M_SOF13: /* Differential sequential, arithmetic */ + case M_SOF14: /* Differential progressive, arithmetic */ + case M_SOF15: /* Differential lossless, arithmetic */ + return marker; + + case M_SOS: /* should not see compressed data before SOF */ + ERREXIT("SOS without prior SOFn"); + break; + + case M_EOI: /* in case it's a tables-only JPEG stream */ + return marker; + + case M_COM: /* Existing COM: conditionally discard */ + if (keep_COM) { + write_marker(marker); + copy_variable(); + } else { + skip_variable(); + } + break; + + default: /* Anything else just gets copied */ + write_marker(marker); + copy_variable(); /* we assume it has a parameter count... */ + break; + } + } /* end loop */ +} + + +/* Command line parsing code */ + +static const char * progname; /* program name for error messages */ + + +static void +usage (void) +/* complain about bad command line */ +{ + fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n"); + fprintf(stderr, "You can add to or replace any existing comment(s).\n"); + + fprintf(stderr, "Usage: %s [switches] ", progname); +#ifdef TWO_FILE_COMMANDLINE + fprintf(stderr, "inputfile outputfile\n"); +#else + fprintf(stderr, "[inputfile]\n"); +#endif + + fprintf(stderr, "Switches (names may be abbreviated):\n"); + fprintf(stderr, " -replace Delete any existing comments\n"); + fprintf(stderr, " -comment \"text\" Insert comment with given text\n"); + fprintf(stderr, " -cfile name Read comment from named file\n"); + fprintf(stderr, "Notice that you must put quotes around the comment text\n"); + fprintf(stderr, "when you use -comment.\n"); + fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n"); + fprintf(stderr, "then the comment text is read from standard input.\n"); + fprintf(stderr, "It can be multiple lines, up to %u characters total.\n", + (unsigned int) MAX_COM_LENGTH); +#ifndef TWO_FILE_COMMANDLINE + fprintf(stderr, "You must specify an input JPEG file name when supplying\n"); + fprintf(stderr, "comment text from standard input.\n"); +#endif + + exit(EXIT_FAILURE); +} + + +static int +keymatch (char * arg, const char * keyword, int minchars) +/* Case-insensitive matching of (possibly abbreviated) keyword switches. */ +/* keyword is the constant keyword (must be lower case already), */ +/* minchars is length of minimum legal abbreviation. */ +{ + register int ca, ck; + register int nmatched = 0; + + while ((ca = *arg++) != '\0') { + if ((ck = *keyword++) == '\0') + return 0; /* arg longer than keyword, no good */ + if (isupper(ca)) /* force arg to lcase (assume ck is already) */ + ca = tolower(ca); + if (ca != ck) + return 0; /* no good */ + nmatched++; /* count matched characters */ + } + /* reached end of argument; fail if it's too short for unique abbrev */ + if (nmatched < minchars) + return 0; + return 1; /* A-OK */ +} + + +/* + * The main program. + */ + +int +main (int argc, char **argv) +{ + int argn; + char * arg; + int keep_COM = 1; + char * comment_arg = NULL; + FILE * comment_file = NULL; + unsigned int comment_length = 0; + int marker; + + /* On Mac, fetch a command line. */ +#ifdef USE_CCOMMAND + argc = ccommand(&argv); +#endif + + progname = argv[0]; + if (progname == NULL || progname[0] == 0) + progname = "wrjpgcom"; /* in case C library doesn't provide it */ + + /* Parse switches, if any */ + for (argn = 1; argn < argc; argn++) { + arg = argv[argn]; + if (arg[0] != '-') + break; /* not switch, must be file name */ + arg++; /* advance over '-' */ + if (keymatch(arg, "replace", 1)) { + keep_COM = 0; + } else if (keymatch(arg, "cfile", 2)) { + if (++argn >= argc) usage(); + if ((comment_file = fopen(argv[argn], "r")) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); + exit(EXIT_FAILURE); + } + } else if (keymatch(arg, "comment", 1)) { + if (++argn >= argc) usage(); + comment_arg = argv[argn]; + /* If the comment text starts with '"', then we are probably running + * under MS-DOG and must parse out the quoted string ourselves. Sigh. + */ + if (comment_arg[0] == '"') { + comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH); + if (comment_arg == NULL) + ERREXIT("Insufficient memory"); + strcpy(comment_arg, argv[argn]+1); + for (;;) { + comment_length = (unsigned int) strlen(comment_arg); + if (comment_length > 0 && comment_arg[comment_length-1] == '"') { + comment_arg[comment_length-1] = '\0'; /* zap terminating quote */ + break; + } + if (++argn >= argc) + ERREXIT("Missing ending quote mark"); + strcat(comment_arg, " "); + strcat(comment_arg, argv[argn]); + } + } + comment_length = (unsigned int) strlen(comment_arg); + } else + usage(); + } + + /* Cannot use both -comment and -cfile. */ + if (comment_arg != NULL && comment_file != NULL) + usage(); + /* If there is neither -comment nor -cfile, we will read the comment text + * from stdin; in this case there MUST be an input JPEG file name. + */ + if (comment_arg == NULL && comment_file == NULL && argn >= argc) + usage(); + + /* Open the input file. */ + if (argn < argc) { + if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); + exit(EXIT_FAILURE); + } + } else { + /* default input file is stdin */ +#ifdef USE_SETMODE /* need to hack file mode? */ + setmode(fileno(stdin), O_BINARY); +#endif +#ifdef USE_FDOPEN /* need to re-open in binary mode? */ + if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open stdin\n", progname); + exit(EXIT_FAILURE); + } +#else + infile = stdin; +#endif + } + + /* Open the output file. */ +#ifdef TWO_FILE_COMMANDLINE + /* Must have explicit output file name */ + if (argn != argc-2) { + fprintf(stderr, "%s: must name one input and one output file\n", + progname); + usage(); + } + if ((outfile = fopen(argv[argn+1], WRITE_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open %s\n", progname, argv[argn+1]); + exit(EXIT_FAILURE); + } +#else + /* Unix style: expect zero or one file name */ + if (argn < argc-1) { + fprintf(stderr, "%s: only one input file\n", progname); + usage(); + } + /* default output file is stdout */ +#ifdef USE_SETMODE /* need to hack file mode? */ + setmode(fileno(stdout), O_BINARY); +#endif +#ifdef USE_FDOPEN /* need to re-open in binary mode? */ + if ((outfile = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) { + fprintf(stderr, "%s: can't open stdout\n", progname); + exit(EXIT_FAILURE); + } +#else + outfile = stdout; +#endif +#endif /* TWO_FILE_COMMANDLINE */ + + /* Collect comment text from comment_file or stdin, if necessary */ + if (comment_arg == NULL) { + FILE * src_file; + int c; + + comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH); + if (comment_arg == NULL) + ERREXIT("Insufficient memory"); + comment_length = 0; + src_file = (comment_file != NULL ? comment_file : stdin); + while ((c = getc(src_file)) != EOF) { + if (comment_length >= (unsigned int) MAX_COM_LENGTH) { + fprintf(stderr, "Comment text may not exceed %u bytes\n", + (unsigned int) MAX_COM_LENGTH); + exit(EXIT_FAILURE); + } + comment_arg[comment_length++] = (char) c; + } + if (comment_file != NULL) + fclose(comment_file); + } + + /* Copy JPEG headers until SOFn marker; + * we will insert the new comment marker just before SOFn. + * This (a) causes the new comment to appear after, rather than before, + * existing comments; and (b) ensures that comments come after any JFIF + * or JFXX markers, as required by the JFIF specification. + */ + marker = scan_JPEG_header(keep_COM); + /* Insert the new COM marker, but only if nonempty text has been supplied */ + if (comment_length > 0) { + write_marker(M_COM); + write_2_bytes(comment_length + 2); + while (comment_length > 0) { + write_1_byte(*comment_arg++); + comment_length--; + } + } + /* Duplicate the remainder of the source file. + * Note that any COM markers occuring after SOF will not be touched. + */ + write_marker(marker); + copy_rest_of_file(); + + /* All done. */ + exit(EXIT_SUCCESS); + return 0; /* suppress no-return-value warnings */ +} diff --git a/crypto777/jpeg/wrppm.c b/crypto777/jpeg/wrppm.c new file mode 100644 index 000000000..68e0c85c3 --- /dev/null +++ b/crypto777/jpeg/wrppm.c @@ -0,0 +1,269 @@ +/* + * wrppm.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * Modified 2009 by Guido Vollbeding. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to write output images in PPM/PGM format. + * The extended 2-byte-per-sample raw PPM/PGM formats are supported. + * The PBMPLUS library is NOT required to compile this software + * (but it is highly useful as a set of PPM image manipulation programs). + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume output to + * an ordinary stdio stream. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef PPM_SUPPORTED + + +/* + * For 12-bit JPEG data, we either downscale the values to 8 bits + * (to write standard byte-per-sample PPM/PGM files), or output + * nonstandard word-per-sample PPM/PGM files. Downscaling is done + * if PPM_NORAWWORD is defined (this can be done in the Makefile + * or in jconfig.h). + * (When the core library supports data precision reduction, a cleaner + * implementation will be to ask for that instead.) + */ + +#if BITS_IN_JSAMPLE == 8 +#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v) +#define BYTESPERSAMPLE 1 +#define PPM_MAXVAL 255 +#else +#ifdef PPM_NORAWWORD +#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8)) +#define BYTESPERSAMPLE 1 +#define PPM_MAXVAL 255 +#else +/* The word-per-sample format always puts the MSB first. */ +#define PUTPPMSAMPLE(ptr,v) \ + { register int val_ = v; \ + *ptr++ = (char) ((val_ >> 8) & 0xFF); \ + *ptr++ = (char) (val_ & 0xFF); \ + } +#define BYTESPERSAMPLE 2 +#define PPM_MAXVAL ((1<pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * This code is used when we have to copy the data and apply a pixel + * format translation. Typically this only happens in 12-bit mode. + */ + +METHODDEF(void) +copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + register char * bufferptr; + register JSAMPROW ptr; + register JDIMENSION col; + + ptr = dest->pub.buffer[0]; + bufferptr = dest->iobuffer; + for (col = dest->samples_per_row; col > 0; col--) { + PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++)); + } + (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * Write some pixel data when color quantization is in effect. + * We have to demap the color index values to straight data. + */ + +METHODDEF(void) +put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + register char * bufferptr; + register int pixval; + register JSAMPROW ptr; + register JSAMPROW color_map0 = cinfo->colormap[0]; + register JSAMPROW color_map1 = cinfo->colormap[1]; + register JSAMPROW color_map2 = cinfo->colormap[2]; + register JDIMENSION col; + + ptr = dest->pub.buffer[0]; + bufferptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + pixval = GETJSAMPLE(*ptr++); + PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval])); + PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval])); + PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval])); + } + (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +METHODDEF(void) +put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + register char * bufferptr; + register JSAMPROW ptr; + register JSAMPROW color_map = cinfo->colormap[0]; + register JDIMENSION col; + + ptr = dest->pub.buffer[0]; + bufferptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)])); + } + (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * Startup: write the file header. + */ + +METHODDEF(void) +start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + + /* Emit file header */ + switch (cinfo->out_color_space) { + case JCS_GRAYSCALE: + /* emit header for raw PGM format */ + fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n", + (long) cinfo->output_width, (long) cinfo->output_height, + PPM_MAXVAL); + break; + case JCS_RGB: + /* emit header for raw PPM format */ + fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n", + (long) cinfo->output_width, (long) cinfo->output_height, + PPM_MAXVAL); + break; + default: + ERREXIT(cinfo, JERR_PPM_COLORSPACE); + } +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + /* Make sure we wrote the output file OK */ + fflush(dinfo->output_file); + if (ferror(dinfo->output_file)) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + + +/* + * The module selection routine for PPM format output. + */ + +GLOBAL(djpeg_dest_ptr) +jinit_write_ppm (j_decompress_ptr cinfo) +{ + ppm_dest_ptr dest; + + /* Create module interface object, fill in method pointers */ + dest = (ppm_dest_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(ppm_dest_struct)); + dest->pub.start_output = start_output_ppm; + dest->pub.finish_output = finish_output_ppm; + + /* Calculate output image dimensions so we can allocate space */ + jpeg_calc_output_dimensions(cinfo); + + /* Create physical I/O buffer. Note we make this near on a PC. */ + dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; + dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char)); + dest->iobuffer = (char *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width); + + if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 || + SIZEOF(JSAMPLE) != SIZEOF(char)) { + /* When quantizing, we need an output buffer for colormap indexes + * that's separate from the physical I/O buffer. We also need a + * separate buffer if pixel format translation must take place. + */ + dest->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->output_width * cinfo->output_components, (JDIMENSION) 1); + dest->pub.buffer_height = 1; + if (! cinfo->quantize_colors) + dest->pub.put_pixel_rows = copy_pixel_rows; + else if (cinfo->out_color_space == JCS_GRAYSCALE) + dest->pub.put_pixel_rows = put_demapped_gray; + else + dest->pub.put_pixel_rows = put_demapped_rgb; + } else { + /* We will fwrite() directly from decompressor output buffer. */ + /* Synthesize a JSAMPARRAY pointer structure */ + /* Cast here implies near->far pointer conversion on PCs */ + dest->pixrow = (JSAMPROW) dest->iobuffer; + dest->pub.buffer = & dest->pixrow; + dest->pub.buffer_height = 1; + dest->pub.put_pixel_rows = put_pixel_rows; + } + + return (djpeg_dest_ptr) dest; +} + +#endif /* PPM_SUPPORTED */ diff --git a/crypto777/jpeg/wrrle.c b/crypto777/jpeg/wrrle.c new file mode 100644 index 000000000..a4e73372d --- /dev/null +++ b/crypto777/jpeg/wrrle.c @@ -0,0 +1,305 @@ +/* + * wrrle.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to write output images in RLE format. + * The Utah Raster Toolkit library is required (version 3.1 or later). + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume output to + * an ordinary stdio stream. + * + * Based on code contributed by Mike Lijewski, + * with updates from Robert Hutchinson. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef RLE_SUPPORTED + +/* rle.h is provided by the Utah Raster Toolkit. */ + +#include + +/* + * We assume that JSAMPLE has the same representation as rle_pixel, + * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples. + */ + +#if BITS_IN_JSAMPLE != 8 + Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */ +#endif + + +/* + * Since RLE stores scanlines bottom-to-top, we have to invert the image + * from JPEG's top-to-bottom order. To do this, we save the outgoing data + * in a virtual array during put_pixel_row calls, then actually emit the + * RLE file during finish_output. + */ + + +/* + * For now, if we emit an RLE color map then it is always 256 entries long, + * though not all of the entries need be used. + */ + +#define CMAPBITS 8 +#define CMAPLENGTH (1<<(CMAPBITS)) + +typedef struct { + struct djpeg_dest_struct pub; /* public fields */ + + jvirt_sarray_ptr image; /* virtual array to store the output image */ + rle_map *colormap; /* RLE-style color map, or NULL if none */ + rle_pixel **rle_row; /* To pass rows to rle_putrow() */ + +} rle_dest_struct; + +typedef rle_dest_struct * rle_dest_ptr; + +/* Forward declarations */ +METHODDEF(void) rle_put_pixel_rows + JPP((j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied)); + + +/* + * Write the file header. + * + * In this module it's easier to wait till finish_output to write anything. + */ + +METHODDEF(void) +start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + rle_dest_ptr dest = (rle_dest_ptr) dinfo; + size_t cmapsize; + int i, ci; +#ifdef PROGRESS_REPORT + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; +#endif + + /* + * Make sure the image can be stored in RLE format. + * + * - RLE stores image dimensions as *signed* 16 bit integers. JPEG + * uses unsigned, so we have to check the width. + * + * - Colorspace is expected to be grayscale or RGB. + * + * - The number of channels (components) is expected to be 1 (grayscale/ + * pseudocolor) or 3 (truecolor/directcolor). + * (could be 2 or 4 if using an alpha channel, but we aren't) + */ + + if (cinfo->output_width > 32767 || cinfo->output_height > 32767) + ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width, + cinfo->output_height); + + if (cinfo->out_color_space != JCS_GRAYSCALE && + cinfo->out_color_space != JCS_RGB) + ERREXIT(cinfo, JERR_RLE_COLORSPACE); + + if (cinfo->output_components != 1 && cinfo->output_components != 3) + ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components); + + /* Convert colormap, if any, to RLE format. */ + + dest->colormap = NULL; + + if (cinfo->quantize_colors) { + /* Allocate storage for RLE-style cmap, zero any extra entries */ + cmapsize = cinfo->out_color_components * CMAPLENGTH * SIZEOF(rle_map); + dest->colormap = (rle_map *) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize); + MEMZERO(dest->colormap, cmapsize); + + /* Save away data in RLE format --- note 8-bit left shift! */ + /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */ + for (ci = 0; ci < cinfo->out_color_components; ci++) { + for (i = 0; i < cinfo->actual_number_of_colors; i++) { + dest->colormap[ci * CMAPLENGTH + i] = + GETJSAMPLE(cinfo->colormap[ci][i]) << 8; + } + } + } + + /* Set the output buffer to the first row */ + dest->pub.buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE); + dest->pub.buffer_height = 1; + + dest->pub.put_pixel_rows = rle_put_pixel_rows; + +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->total_extra_passes++; /* count file writing as separate pass */ + } +#endif +} + + +/* + * Write some pixel data. + * + * This routine just saves the data away in a virtual array. + */ + +METHODDEF(void) +rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + rle_dest_ptr dest = (rle_dest_ptr) dinfo; + + if (cinfo->output_scanline < cinfo->output_height) { + dest->pub.buffer = (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->image, + cinfo->output_scanline, (JDIMENSION) 1, TRUE); + } +} + +/* + * Finish up at the end of the file. + * + * Here is where we really output the RLE file. + */ + +METHODDEF(void) +finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + rle_dest_ptr dest = (rle_dest_ptr) dinfo; + rle_hdr header; /* Output file information */ + rle_pixel **rle_row, *red, *green, *blue; + JSAMPROW output_row; + char cmapcomment[80]; + int row, col; + int ci; +#ifdef PROGRESS_REPORT + cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; +#endif + + /* Initialize the header info */ + header = *rle_hdr_init(NULL); + header.rle_file = dest->pub.output_file; + header.xmin = 0; + header.xmax = cinfo->output_width - 1; + header.ymin = 0; + header.ymax = cinfo->output_height - 1; + header.alpha = 0; + header.ncolors = cinfo->output_components; + for (ci = 0; ci < cinfo->output_components; ci++) { + RLE_SET_BIT(header, ci); + } + if (cinfo->quantize_colors) { + header.ncmap = cinfo->out_color_components; + header.cmaplen = CMAPBITS; + header.cmap = dest->colormap; + /* Add a comment to the output image with the true colormap length. */ + sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors); + rle_putcom(cmapcomment, &header); + } + + /* Emit the RLE header and color map (if any) */ + rle_put_setup(&header); + + /* Now output the RLE data from our virtual array. + * We assume here that (a) rle_pixel is represented the same as JSAMPLE, + * and (b) we are not on a machine where FAR pointers differ from regular. + */ + +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_limit = cinfo->output_height; + progress->pub.pass_counter = 0; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + + if (cinfo->output_components == 1) { + for (row = cinfo->output_height-1; row >= 0; row--) { + rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->image, + (JDIMENSION) row, (JDIMENSION) 1, FALSE); + rle_putrow(rle_row, (int) cinfo->output_width, &header); +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_counter++; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + } + } else { + for (row = cinfo->output_height-1; row >= 0; row--) { + rle_row = (rle_pixel **) dest->rle_row; + output_row = * (*cinfo->mem->access_virt_sarray) + ((j_common_ptr) cinfo, dest->image, + (JDIMENSION) row, (JDIMENSION) 1, FALSE); + red = rle_row[0]; + green = rle_row[1]; + blue = rle_row[2]; + for (col = cinfo->output_width; col > 0; col--) { + *red++ = GETJSAMPLE(*output_row++); + *green++ = GETJSAMPLE(*output_row++); + *blue++ = GETJSAMPLE(*output_row++); + } + rle_putrow(rle_row, (int) cinfo->output_width, &header); +#ifdef PROGRESS_REPORT + if (progress != NULL) { + progress->pub.pass_counter++; + (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); + } +#endif + } + } + +#ifdef PROGRESS_REPORT + if (progress != NULL) + progress->completed_extra_passes++; +#endif + + /* Emit file trailer */ + rle_puteof(&header); + fflush(dest->pub.output_file); + if (ferror(dest->pub.output_file)) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + + +/* + * The module selection routine for RLE format output. + */ + +GLOBAL(djpeg_dest_ptr) +jinit_write_rle (j_decompress_ptr cinfo) +{ + rle_dest_ptr dest; + + /* Create module interface object, fill in method pointers */ + dest = (rle_dest_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(rle_dest_struct)); + dest->pub.start_output = start_output_rle; + dest->pub.finish_output = finish_output_rle; + + /* Calculate output image dimensions so we can allocate space */ + jpeg_calc_output_dimensions(cinfo); + + /* Allocate a work array for output to the RLE library. */ + dest->rle_row = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->output_width, (JDIMENSION) cinfo->output_components); + + /* Allocate a virtual array to hold the image. */ + dest->image = (*cinfo->mem->request_virt_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, + (JDIMENSION) (cinfo->output_width * cinfo->output_components), + cinfo->output_height, (JDIMENSION) 1); + + return (djpeg_dest_ptr) dest; +} + +#endif /* RLE_SUPPORTED */ diff --git a/crypto777/jpeg/wrtarga.c b/crypto777/jpeg/wrtarga.c new file mode 100644 index 000000000..cf104d2de --- /dev/null +++ b/crypto777/jpeg/wrtarga.c @@ -0,0 +1,253 @@ +/* + * wrtarga.c + * + * Copyright (C) 1991-1996, Thomas G. Lane. + * This file is part of the Independent JPEG Group's software. + * For conditions of distribution and use, see the accompanying README file. + * + * This file contains routines to write output images in Targa format. + * + * These routines may need modification for non-Unix environments or + * specialized applications. As they stand, they assume output to + * an ordinary stdio stream. + * + * Based on code contributed by Lee Daniel Crocker. + */ + +#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ + +#ifdef TARGA_SUPPORTED + + +/* + * To support 12-bit JPEG data, we'd have to scale output down to 8 bits. + * This is not yet implemented. + */ + +#if BITS_IN_JSAMPLE != 8 + Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */ +#endif + +/* + * The output buffer needs to be writable by fwrite(). On PCs, we must + * allocate the buffer in near data space, because we are assuming small-data + * memory model, wherein fwrite() can't reach far memory. If you need to + * process very wide images on a PC, you might have to compile in large-memory + * model, or else replace fwrite() with a putc() loop --- which will be much + * slower. + */ + + +/* Private version of data destination object */ + +typedef struct { + struct djpeg_dest_struct pub; /* public fields */ + + char *iobuffer; /* physical I/O buffer */ + JDIMENSION buffer_width; /* width of one row */ +} tga_dest_struct; + +typedef tga_dest_struct * tga_dest_ptr; + + +LOCAL(void) +write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors) +/* Create and write a Targa header */ +{ + char targaheader[18]; + + /* Set unused fields of header to 0 */ + MEMZERO(targaheader, SIZEOF(targaheader)); + + if (num_colors > 0) { + targaheader[1] = 1; /* color map type 1 */ + targaheader[5] = (char) (num_colors & 0xFF); + targaheader[6] = (char) (num_colors >> 8); + targaheader[7] = 24; /* 24 bits per cmap entry */ + } + + targaheader[12] = (char) (cinfo->output_width & 0xFF); + targaheader[13] = (char) (cinfo->output_width >> 8); + targaheader[14] = (char) (cinfo->output_height & 0xFF); + targaheader[15] = (char) (cinfo->output_height >> 8); + targaheader[17] = 0x20; /* Top-down, non-interlaced */ + + if (cinfo->out_color_space == JCS_GRAYSCALE) { + targaheader[2] = 3; /* image type = uncompressed gray-scale */ + targaheader[16] = 8; /* bits per pixel */ + } else { /* must be RGB */ + if (num_colors > 0) { + targaheader[2] = 1; /* image type = colormapped RGB */ + targaheader[16] = 8; + } else { + targaheader[2] = 2; /* image type = uncompressed RGB */ + targaheader[16] = 24; + } + } + + if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + + +/* + * Write some pixel data. + * In this module rows_supplied will always be 1. + */ + +METHODDEF(void) +put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +/* used for unquantized full-color output */ +{ + tga_dest_ptr dest = (tga_dest_ptr) dinfo; + register JSAMPROW inptr; + register char * outptr; + register JDIMENSION col; + + inptr = dest->pub.buffer[0]; + outptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */ + outptr[1] = (char) GETJSAMPLE(inptr[1]); + outptr[2] = (char) GETJSAMPLE(inptr[0]); + inptr += 3, outptr += 3; + } + (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + +METHODDEF(void) +put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +/* used for grayscale OR quantized color output */ +{ + tga_dest_ptr dest = (tga_dest_ptr) dinfo; + register JSAMPROW inptr; + register char * outptr; + register JDIMENSION col; + + inptr = dest->pub.buffer[0]; + outptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + *outptr++ = (char) GETJSAMPLE(*inptr++); + } + (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * Write some demapped pixel data when color quantization is in effect. + * For Targa, this is only applied to grayscale data. + */ + +METHODDEF(void) +put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + tga_dest_ptr dest = (tga_dest_ptr) dinfo; + register JSAMPROW inptr; + register char * outptr; + register JSAMPROW color_map0 = cinfo->colormap[0]; + register JDIMENSION col; + + inptr = dest->pub.buffer[0]; + outptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]); + } + (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * Startup: write the file header. + */ + +METHODDEF(void) +start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + tga_dest_ptr dest = (tga_dest_ptr) dinfo; + int num_colors, i; + FILE *outfile; + + if (cinfo->out_color_space == JCS_GRAYSCALE) { + /* Targa doesn't have a mapped grayscale format, so we will */ + /* demap quantized gray output. Never emit a colormap. */ + write_header(cinfo, dinfo, 0); + if (cinfo->quantize_colors) + dest->pub.put_pixel_rows = put_demapped_gray; + else + dest->pub.put_pixel_rows = put_gray_rows; + } else if (cinfo->out_color_space == JCS_RGB) { + if (cinfo->quantize_colors) { + /* We only support 8-bit colormap indexes, so only 256 colors */ + num_colors = cinfo->actual_number_of_colors; + if (num_colors > 256) + ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors); + write_header(cinfo, dinfo, num_colors); + /* Write the colormap. Note Targa uses BGR byte order */ + outfile = dest->pub.output_file; + for (i = 0; i < num_colors; i++) { + putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile); + putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile); + putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile); + } + dest->pub.put_pixel_rows = put_gray_rows; + } else { + write_header(cinfo, dinfo, 0); + dest->pub.put_pixel_rows = put_pixel_rows; + } + } else { + ERREXIT(cinfo, JERR_TGA_COLORSPACE); + } +} + + +/* + * Finish up at the end of the file. + */ + +METHODDEF(void) +finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + /* Make sure we wrote the output file OK */ + fflush(dinfo->output_file); + if (ferror(dinfo->output_file)) + ERREXIT(cinfo, JERR_FILE_WRITE); +} + + +/* + * The module selection routine for Targa format output. + */ + +GLOBAL(djpeg_dest_ptr) +jinit_write_targa (j_decompress_ptr cinfo) +{ + tga_dest_ptr dest; + + /* Create module interface object, fill in method pointers */ + dest = (tga_dest_ptr) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(tga_dest_struct)); + dest->pub.start_output = start_output_tga; + dest->pub.finish_output = finish_output_tga; + + /* Calculate output image dimensions so we can allocate space */ + jpeg_calc_output_dimensions(cinfo); + + /* Create I/O buffer. Note we make this near on a PC. */ + dest->buffer_width = cinfo->output_width * cinfo->output_components; + dest->iobuffer = (char *) + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (size_t) (dest->buffer_width * SIZEOF(char))); + + /* Create decompressor output buffer. */ + dest->pub.buffer = (*cinfo->mem->alloc_sarray) + ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1); + dest->pub.buffer_height = 1; + + return (djpeg_dest_ptr) dest; +} + +#endif /* TARGA_SUPPORTED */ diff --git a/crypto777/libgfshare.c b/crypto777/libgfshare.c new file mode 100755 index 000000000..4f2ea6df3 --- /dev/null +++ b/crypto777/libgfshare.c @@ -0,0 +1,511 @@ +/* + * This file is Copyright Daniel Silverstone 2006 + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +#include + +//#include "config.h" +#include "OS_portable.h" +#include "../includes/libgfshare.h" +//#include "../includes/libgfshare_tables.h" + +#include +#include +#include + +#define XMALLOC malloc +#define XFREE free + +struct _gfshare_ctx +{ + uint32_t sharecount,threshold,size,buffersize; + unsigned char sharenrs[255],buffer[]; +}; + +unsigned char ctx_logs[256] = { + 0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, + 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, + 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, + 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71, + 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, + 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, + 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, + 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6, + 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, + 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88, + 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, + 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40, + 0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e, + 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d, + 0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b, + 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57, + 0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, + 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18, + 0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c, + 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e, + 0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, + 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61, + 0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e, + 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2, + 0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76, + 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6, + 0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, + 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a, + 0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51, + 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7, + 0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, + 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf }; + +unsigned char ctx_exps[510] = { + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, + 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26, + 0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, + 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, + 0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, + 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23, + 0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, + 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1, + 0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, + 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, + 0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, + 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2, + 0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, + 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce, + 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, + 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, + 0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, + 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, + 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, + 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, + 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, + 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, + 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, + 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, + 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, + 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, + 0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, + 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09, + 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, + 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16, + 0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, + 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01, + 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, + 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26, 0x4c, + 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, + 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x9d, + 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, + 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23, 0x46, + 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, + 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1, 0x5f, + 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, + 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, 0xfd, + 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, + 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2, 0xd9, + 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0x0d, + 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce, 0x81, + 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, + 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, 0x85, + 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, + 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, 0xa8, + 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, + 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, 0xe6, + 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, + 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 0xe3, + 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, + 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, 0x82, + 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, + 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, 0x51, + 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, + 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09, 0x12, + 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, + 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16, 0x2c, + 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, + 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e }; + +/*void _gfshare_fill_rand_using_random(unsigned char *buffer,unsigned long long count) +{ + uint32_t i; + for (i=0; i> 8; // apparently the bottom 8 aren't very random but the middles ones are +}*/ +//void randombytes(unsigned char *x,long xlen); + +//gfshare_rand_func_t gfshare_fill_rand = _gfshare_fill_rand_using_random; +gfshare_rand_func_t gfshare_fill_rand = OS_randombytes; + +// ------------------------------------------------------[ Preparation ]---- + +gfshare_ctx *_gfshare_ctx_init_core(unsigned char *sharenrs,uint32_t sharecount,unsigned char threshold,uint32_t size) +{ + gfshare_ctx *ctx; + ctx = XMALLOC(sizeof(struct _gfshare_ctx) + threshold * size); + if ( ctx == NULL ) + return(NULL); // errno should still be set from XMALLOC() + ctx->sharecount = sharecount; + ctx->threshold = threshold; + ctx->size = size; + memcpy(ctx->sharenrs,sharenrs,sharecount); + ctx->buffersize = threshold * size; + return(ctx); +} + +// Initialise a gfshare context for producing shares +gfshare_ctx *gfshare_ctx_init_enc(unsigned char *sharenrs,uint32_t sharecount,unsigned char threshold,uint32_t size) +{ + uint32_t i; + // can't have x[i] = 0 - that would just be a copy of the secret, in theory + // in fact, due to the way we use exp/log for multiplication and treat log(0) as 0, it ends up as a copy of x[i] = 1 + for (i=0; ithreshold = 0; + return(ctx); +} + +// Free a share context's memory. +void gfshare_ctx_free(gfshare_ctx *ctx) +{ + long len = sizeof(struct _gfshare_ctx) + ctx->buffersize; + gfshare_fill_rand((unsigned char*)ctx,len); + XFREE(ctx); +} + +// --------------------------------------------------------[ Splitting ]---- + +// Provide a secret to the encoder. (this re-scrambles the coefficients) +void gfshare_ctx_enc_setsecret(gfshare_ctx *ctx,unsigned char *secret) +{ + memcpy(ctx->buffer + ((ctx->threshold-1) * ctx->size),secret,ctx->size); + gfshare_fill_rand(ctx->buffer,(ctx->threshold-1) * ctx->size); +} + +// Extract a share from the context. 'share' must be preallocated and at least 'size' bytes long. +// 'sharenr' is the index into the 'sharenrs' array of the share you want. +void calc_share(unsigned char *buffer,int32_t size,int32_t M,uint32_t ilog,unsigned char *share) +{ + uint32_t pos,coefficient;//,ilog = ctx_logs[ctx->sharenrs[sharenr]]; + //unsigned char *coefficient_ptr = buffer; + unsigned char *share_ptr,share_byte; + for (pos=0; posbuffer,ctx->size,ctx->threshold,ctx_logs[ctx->sharenrs[sharenr]],share); +} + +void calc_shares(unsigned char *shares,unsigned char *secret,int32_t size,int32_t width,int32_t M,int32_t N,unsigned char *sharenrs) +{ + int32_t i; + unsigned char *buffer = calloc(M,width); + memset(shares,0,N*width); + memcpy(buffer + ((M - 1) * size),secret,size); + gfshare_fill_rand(buffer,(M - 1) * size); + for (i=0; isharenrs,sharenrs,ctx->sharecount); +} + +// Provide a share context with one of the shares. The 'sharenr' is the index into the 'sharenrs' array +void gfshare_ctx_dec_giveshare(gfshare_ctx *ctx,unsigned char sharenr,unsigned char *share) +{ + memcpy(ctx->buffer + (sharenr * ctx->size),share,ctx->size); +} + +// Extract the secret by interpolation of the shares. secretbuf must be allocated and at least 'size' bytes long +void gfshare_extract(unsigned char *secretbuf,uint8_t *sharenrs,int32_t N,uint8_t *buffer,int32_t size,int32_t width) +{ + uint32_t i,j,Li_top,Li_bottom; + unsigned char *secret_ptr,*share_ptr,sharei,sharej; + memset(secretbuf,0,width); + for (i=0; i= 0xff ) + Li_top -= 0xff; + Li_bottom += ctx_logs[sharei ^ sharej]; + if ( Li_bottom >= 0xff ) + Li_bottom -= 0xff; + } + if ( Li_bottom > Li_top ) + Li_top += 0xff; + Li_top -= Li_bottom; // Li_top is now log(L(i)) + secret_ptr = secretbuf; + share_ptr = buffer + (width * i); + for (j=0; jsharenrs,ctx->sharecount,ctx->buffer,ctx->size,ctx->size); +} + +int32_t init_sharenrs(unsigned char sharenrs[255],unsigned char *orig,int32_t m,int32_t n) +{ + unsigned char *randvals,valid[255]; + int32_t i,j,r,remains,orign; + if ( m > n || n >= 0xff ) // reserve 255 for illegal sharei + { + printf("illegal M.%d of N.%d\n",m,n); + return(-1); + } + randvals = calloc(1,65536); + gfshare_fill_rand(randvals,65536); + memset(sharenrs,0,n); + if ( orig == 0 && n == m ) + { + for (i=0; i<255; i++) + valid[i] = (i + 1); + remains = orign = 255; + for (i=0; i> 8) % remains; + sharenrs[i] = valid[r]; + valid[r] = valid[--remains]; + } + /*while ( i < m ) + { + if ( j >= 65536 ) + { + gfshare_fill_rand(randvals,65536); + printf("refill j.%d\n",j); + j = 0; + } + r = (randvals[j++] % n); + if ( valid[r] != 0 ) + { + remains--; + i++; + sharenrs[r] = valid[r]; + //printf("%d ",sharenrs[i]); + valid[r] = 0; + } + }*/ + for (i=0; i> 8) % n) + 1; + } + err = 0; + printf("err.%d\n",err); +#ifdef hardcoded_m_of_n_test + // Stage 3, attempt a recombination with shares 1 and 2 + sharenrs[2] = 0; + gfshare_ctx_dec_newshares(G,sharenrs); + gfshare_ctx_dec_extract( G, recomb ); + for( i = 0; i < 512; ++i ) + if( secret[i] != recomb[i] ) + ok = 0; + printf("shares 1 + 2: ok.%d\n",ok); + err += (ok == 0), ok = 1; + // Stage 4, attempt a recombination with shares 1 and 3 + sharenrs[2] = '2'; + sharenrs[1] = 0; + gfshare_ctx_dec_newshares( G, sharenrs ); + gfshare_ctx_dec_extract( G, recomb ); + for( i = 0; i < 512; ++i ) + if( secret[i] != recomb[i] ) + ok = 0; + printf("shares 1 + 3: ok.%d\n",ok); + err += (ok == 0), ok = 1; + // Stage 5, attempt a recombination with shares 2 and 3 + sharenrs[0] = 0; + sharenrs[1] = '1'; + gfshare_ctx_dec_newshares( G, sharenrs ); + gfshare_ctx_dec_extract( G, recomb ); + for( i = 0; i < 512; ++i ) + if( secret[i] != recomb[i] ) + ok = 0; + printf("shares 2 + 3: ok.%d\n",ok); + err += (ok == 0), ok = 1; + // Stage 6, attempt a recombination with shares 1, 2 and 3 + sharenrs[0] = '0'; + gfshare_ctx_dec_newshares( G, sharenrs ); + gfshare_ctx_dec_extract( G, recomb ); + for( i = 0; i < 512; ++i ) + if( secret[i] != recomb[i] ) + ok = 0; + printf("shares 1 + 2 + 3: ok.%d\n",ok); + err += (ok == 0), ok = 1; + gfshare_ctx_free( G ); + printf("total error test_m_of_n %d\n",err); +#endif +cleanup: + for (i=0; i +#include "../includes/curve25519.h" + +struct huffstream { uint8_t *ptr,*buf; uint32_t bitoffset,maski,endpos; uint32_t allocsize:31,allocated:1; }; +typedef struct huffstream HUFF; +#define hrewind(hp) hseek(hp,0,SEEK_SET) + +#define RAMMASK_BIT(x) ((uint16_t)(1 << ((8 * sizeof(uint16_t)) - (1 + (x))))) +#define RAMCODER_FINALIZE 1 +#define RAMCODER_PUTBITS 2 +#define RAMCODER_MAXSYMBOLS 0x100 + +#define SETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] |= (1 << ((bitoffset) & 7))) +#define GETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] & (1 << ((bitoffset) & 7))) + +struct ramcoder +{ + uint32_t cumulativeProb; + uint16_t lower,upper,code,underflowBits,lastsymbol,upper_lastsymbol,counter; + uint64_t *histo; + uint16_t ranges[]; +}; +int32_t ramcoder_decode(struct ramcoder *coder,int32_t updateprobs,HUFF *hp); +int32_t ramcoder_decoder(struct ramcoder *coder,int32_t updateprobs,uint8_t *buf,int32_t maxlen,HUFF *hp,bits256 *seed); +#define ramcoder_encode(val,coder,hp) ramcoder_update(val,coder,1,RAMCODER_PUTBITS,hp) +int32_t ramcoder_encoder(struct ramcoder *coder,int32_t updateprobs,uint8_t *buf,int32_t len,HUFF *hp,uint64_t *histo,bits256 *seed); +int32_t ramcoder_update(int symbol,struct ramcoder *coder,int32_t updateprobs,int32_t putflags,HUFF *hp); +int32_t init_ramcoder(struct ramcoder *coder,HUFF *hp,bits256 *seed); +int32_t ramcoder_emit(HUFF *hp,struct ramcoder *coder,int32_t updateprobs,uint8_t *buf,int32_t len); + +int32_t ramcoder_decompress(uint8_t *data,int32_t maxlen,uint8_t *bits,uint32_t numbits,bits256 seed); +int32_t ramcoder_compress(uint8_t *bits,int32_t maxlen,uint8_t *data,int32_t datalen,uint64_t *histo,bits256 seed); +uint64_t hconv_bitlen(uint64_t bitlen); +void _init_HUFF(HUFF *hp,int32_t allocsize,void *buf); + +#endif +#else +#ifndef crypto777_ramcoder_c +#define crypto777_ramcoder_c + +#ifndef crypto777_ramcoder_h +#define DEFINES_ONLY +#include "ramcoder.c" +#undef DEFINES_ONLY +#endif +static const uint8_t huffmasks[8] = { (1<<0), (1<<1), (1<<2), (1<<3), (1<<4), (1<<5), (1<<6), (1<<7) }; +static const uint8_t huffoppomasks[8] = { ~(1<<0), ~(1<<1), ~(1<<2), ~(1<<3), ~(1<<4), ~(1<<5), ~(1<<6), (uint8_t)~(1<<7) }; + +void _init_HUFF(HUFF *hp,int32_t allocsize,void *buf) { hp->buf = hp->ptr = buf, hp->allocsize = allocsize, hp->bitoffset = 0; } + +uint64_t hconv_bitlen(uint64_t bitlen) +{ + uint64_t len; + len = (bitlen >> 3); + if ( (bitlen & 7) != 0 ) + len++; + return(len); +} + +int32_t hupdate_internals(HUFF *hp) +{ + int32_t retval = 0; + if ( (hp->bitoffset >> 3) > hp->allocsize ) + { + printf("hupdate_internals: ERROR: bitoffset.%d -> %d >= allocsize.%d\n",hp->bitoffset,hp->bitoffset>>3,hp->allocsize); + //getchar(); + hp->bitoffset = (hp->allocsize << 3) - 1; + retval = -1; + } + if ( hp->bitoffset > hp->endpos ) + hp->endpos = hp->bitoffset; + hp->ptr = &hp->buf[hp->bitoffset >> 3]; + hp->maski = (hp->bitoffset & 7); + return(retval); +} + +int32_t hseek(HUFF *hp,int32_t offset,int32_t mode) +{ + if ( mode == SEEK_END ) + hp->bitoffset = (offset + hp->endpos); + else if ( mode == SEEK_SET ) + hp->bitoffset = offset; + else hp->bitoffset += offset; + if ( hupdate_internals(hp) < 0 ) + { + printf("hseek.%d: illegal offset.%d %d >= allocsize.%d\n",mode,offset,offset>>3,hp->allocsize); + return(-1); + } + return(0); +} + +void hclear(HUFF *hp,int32_t clearbuf) +{ + hp->bitoffset = 0; + hupdate_internals(hp); + hp->endpos = 0; + if ( clearbuf != 0 ) + memset(hp->buf,0,hp->allocsize); +} + +int32_t hgetbit(HUFF *hp) +{ + int32_t bit = 0; + //printf("hp.%p ptr.%ld buf.%ld maski.%d\n",hp,(long)hp->ptr-(long)hp->buf,(long)hp->buf-(long)hp,hp->maski); + if ( hp->bitoffset < hp->endpos ) + { + if ( (*hp->ptr & huffmasks[hp->maski++]) != 0 ) + bit = 1; + hp->bitoffset++; + if ( hp->maski == 8 ) + hp->maski = 0, hp->ptr++; + //fprintf(stderr,"<-%d ",bit); + return(bit); + } + printf("hgetbit past EOF: %d >= %d\n",hp->bitoffset,hp->endpos), getchar(); + return(-1); +} + +int32_t hputbit(HUFF *hp,int32_t bit) +{ + //fprintf(stderr,"->%d ",bit); + if ( bit != 0 ) + *hp->ptr |= huffmasks[hp->maski]; + else *hp->ptr &= huffoppomasks[hp->maski]; + if ( ++hp->maski >= 8 ) + hp->maski = 0, hp->ptr++; + if ( ++hp->bitoffset > hp->endpos ) + hp->endpos = hp->bitoffset; + if ( (hp->bitoffset>>3) >= hp->allocsize ) + { + printf("hwrite: bitoffset.%d >= allocsize.%d\n",hp->bitoffset,hp->allocsize); + hp->bitoffset--; + hupdate_internals(hp); + return(-1); + } + return(0); +} + +int32_t hwrite(uint64_t codebits,int32_t numbits,HUFF *hp) +{ + int32_t i; + for (i=0; i>=1) + if ( hputbit(hp,codebits & 1) < 0 ) + return(-1); + return(numbits); +} + +uint64_t hread(int32_t *numbitsp,int32_t numbits,HUFF *hp) +{ + int32_t i,bit; uint64_t codebits = 0; + for (i=0; ibitoffset & 7) != 0 || ((hp->bitoffset>>3) + datalen) > hp->allocsize ) + { + printf("misaligned hmemcpy bitoffset.%d or overflow allocsize %d vs %d\n",hp->bitoffset,hp->allocsize,((hp->bitoffset>>3) + datalen)); + getchar(); + return(-1); + } + if ( dest != 0 && src == 0 ) + memcpy(dest,hp->ptr,datalen); + else if ( dest == 0 && src != 0 ) + memcpy(hp->ptr,src,datalen); + else + { + printf("invalid hmemcpy with both dest.%p and src.%p\n",dest,src); + return(-1); + } + hp->ptr += datalen; + hp->bitoffset += (datalen << 3); + if ( hp->bitoffset > hp->endpos ) + hp->endpos = hp->bitoffset; + return(datalen); +} + +int32_t hcalc_bitsize(uint64_t x) +{ + uint64_t mask = ((uint64_t)1 << 63); + int32_t i; + if ( x == 0 ) + return(1); + for (i=63; i>=0; i--,mask>>=1) + { + if ( (mask & x) != 0 ) + return(i+1); + } + return(-1); +} + +int32_t init_ramcoder(struct ramcoder *coder,HUFF *hp,bits256 *seed) +{ + int32_t i,precision,numbits = 0; + if ( coder->lastsymbol == 0 ) + coder->lastsymbol = RAMCODER_MAXSYMBOLS, coder->upper_lastsymbol = (coder->lastsymbol + 1); + coder->cumulativeProb = coder->lower = coder->code = coder->underflowBits = coder->ranges[0] = 0; + for (i=1; i<=coder->upper_lastsymbol; i++) + { + coder->ranges[i] = coder->ranges[i - 1] + 1 + 256*((i <= sizeof(seed)*8) ? (GETBIT(seed->bytes,i-1) != 0) : 0); + //printf("%d ",coder->ranges[i]); + } + for (i=1; i<=coder->upper_lastsymbol; i++) + coder->cumulativeProb += (coder->ranges[i] - coder->ranges[i - 1]); + precision = (8 * sizeof(uint16_t)); + coder->upper = (1LL << precision) - 1; + if ( hp != 0 ) + { + for (i=0; icode = (coder->code << 1) | hgetbit(hp); + //coder->code = hread(&numbits,precision,hp), coder->code <<= (precision - numbits); + //printf("set code %x\n",coder->code); + } + //printf("cumulative.%d code.%x numbits.%d\n",coder->cumulativeProb,coder->code,numbits); + return(numbits); +} + +int32_t ramcoder_state(struct ramcoder *coder) +{ + if ( (coder->upper & RAMMASK_BIT(0)) == (coder->lower & RAMMASK_BIT(0)) ) + return(0); + else if ( (coder->lower & RAMMASK_BIT(1)) && (coder->upper & RAMMASK_BIT(1)) == 0 ) + return(1); + else return(-1); +} + +void ramcoder_normalize(struct ramcoder *coder) { coder->lower &= ~(RAMMASK_BIT(0) | RAMMASK_BIT(1)), coder->upper |= RAMMASK_BIT(1); } + +void ramcoder_shiftbits(struct ramcoder *coder) { coder->lower <<= 1, coder->upper <<= 1, coder->upper |= 1; } + +int32_t ramcoder_putbits(HUFF *hp,struct ramcoder *coder,int32_t flushflag) +{ + int32_t numbits = 0; + while ( 1 ) + { + switch ( ramcoder_state(coder) ) + { + case 1: coder->underflowBits++, ramcoder_normalize(coder); break; + case 0: + if ( hputbit(hp,(coder->upper & RAMMASK_BIT(0)) != 0) < 0 ) + return(-1); + numbits++; + //printf("%d> ",(coder->upper & RAMMASK_BIT(0)) != 0); + while ( coder->underflowBits > 0 ) + { + if ( hputbit(hp,(coder->upper & RAMMASK_BIT(0)) == 0) < 0 ) + return(-1); + numbits++; + //printf("%d> ",(coder->upper & RAMMASK_BIT(0)) == 0); + coder->underflowBits--; + } + break; + default: + if ( flushflag != 0 ) + { + if ( hputbit(hp,(coder->lower & RAMMASK_BIT(1)) != 0) < 0 ) + return(-1); + numbits++; + for (coder->underflowBits++; coder->underflowBits>0; coder->underflowBits--) + { + if ( hputbit(hp,(coder->lower & RAMMASK_BIT(1)) == 0) < 0 ) + return(-1); + numbits++; + } + } + return(numbits); + break; + } + ramcoder_shiftbits(coder); + } +} + +int32_t ramcoder_getbits(HUFF *hp,struct ramcoder *coder) +{ + int32_t nextBit,numbits = 0; + while ( 1 ) + { + switch ( ramcoder_state(coder) ) + { + case 0: break; // MSBs match, allow them to be shifted out + case 1: ramcoder_normalize(coder), coder->code ^= RAMMASK_BIT(1); break; + default: return(numbits); break; + } + ramcoder_shiftbits(coder); + coder->code <<= 1; + if ( (nextBit= hgetbit(hp)) >= 0 ) + coder->code |= nextBit;//, printf("<%c",'0'+nextBit); + else return(-1); + numbits++; + } +} + +int32_t ramdecoder_bsearch(uint16_t probability,struct ramcoder *coder) +{ + int32_t last,middle,first = 0; + last = coder->upper_lastsymbol; + while ( last >= first ) + { + middle = first + ((last - first) / 2); + //printf("[%d %d] ",coder->ranges[middle],coder->ranges[middle+1]); + if ( probability < coder->ranges[middle] ) + last = middle - 1; + else if ( probability >= coder->ranges[middle + 1] ) + first = middle + 1; + else return(middle); + } + printf("Unknown Symbol: %llu (max: %llu)\n",(long long)probability,(long long)coder->ranges[coder->upper_lastsymbol]); + return(-1); +} + +int32_t ramcoder_update(int symbol,struct ramcoder *coder,int32_t updateprobs,int32_t putflags,HUFF *hp) +{ + uint32_t range; uint16_t i,original,delta; +//printf("putflags.%d %p: upper %llu lower %llu code.%x cumulative.%d | symbol.%d\n",putflags,coder,(long long)coder->upper,(long long)coder->lower,coder->code,coder->cumulativeProb,symbol); + range = (uint32_t)(coder->upper - coder->lower) + 1; + coder->upper = coder->lower + (uint16_t)(((uint32_t)coder->ranges[symbol + 1] * range)/ coder->cumulativeProb) - 1; + coder->lower = coder->lower + (uint16_t)(((uint32_t)coder->ranges[symbol] * range) / coder->cumulativeProb); + if ( updateprobs != 0 ) + { + coder->cumulativeProb++; + for (i=(symbol+1); i<=coder->upper_lastsymbol; i++) + coder->ranges[i]++; + if ( coder->cumulativeProb >= (1 << ((8 * sizeof(uint16_t)) - 2)) ) + { + original = coder->cumulativeProb = 0; + for (i=1; i<=coder->upper_lastsymbol; i++) + { + delta = coder->ranges[i] - original, original = coder->ranges[i]; + if ( delta <= 2 ) + coder->ranges[i] = coder->ranges[i - 1] + 1; + else coder->ranges[i] = coder->ranges[i - 1] + (delta / 2); + coder->cumulativeProb += (coder->ranges[i] - coder->ranges[i - 1]); + } + } + coder->counter++; + } else printf("unexpected non-update ramcoder\n"); + if ( coder->lower > coder->upper ) + printf("ramcoderupdate: coder->lower %llu > %llu coder->upper\n",(long long)coder->lower,(long long)coder->upper); + return((putflags != 0) ? ramcoder_putbits(hp,coder,putflags & RAMCODER_FINALIZE) : ramcoder_getbits(hp,coder)); +} + +int32_t ramcoder_emit(HUFF *hp,struct ramcoder *coder,int32_t updateprobs,uint8_t *buf,int32_t len) +{ + int32_t i,n,numbits = 0; + for (i=0; ihisto != 0 ) + coder->histo[buf[i]]++; + if ( (n= ramcoder_update(buf[i],coder,updateprobs,RAMCODER_PUTBITS,hp)) < 0 ) + return(-1); + numbits += n; + } + return(numbits); +} + +int32_t ramcoder_encoder(struct ramcoder *coder,int32_t updateprobs,uint8_t *buf,int32_t len,HUFF *hp,uint64_t *histo,bits256 *seed) +{ + int32_t i,threshold; uint8_t _coder[sizeof(*coder) + (RAMCODER_MAXSYMBOLS+2)*sizeof(coder->ranges[0])]; + if ( coder == 0 ) + { + memset(_coder,0,sizeof(_coder)); + hrewind(hp); + coder = (struct ramcoder *)_coder; + coder->histo = histo; + init_ramcoder(coder,0,seed); + if ( ramcoder_emit(hp,coder,updateprobs,buf,len) < 0 ) + return(-1); + if ( ramcoder_update(coder->lastsymbol,coder,updateprobs,RAMCODER_PUTBITS,hp) < 0 ) + return(-1); + if ( ramcoder_update(coder->lastsymbol,coder,updateprobs,RAMCODER_PUTBITS|RAMCODER_FINALIZE,hp) < 0 ) + return(-1); + } + else if ( ramcoder_emit(hp,coder,updateprobs,buf,len) < 0 ) + return(-1); + memset(seed,0,sizeof(*seed)); + threshold = coder->cumulativeProb / coder->upper_lastsymbol; + for (i=1; i<=coder->upper_lastsymbol; i++) + if ( (coder->ranges[i] - coder->ranges[i - 1]) > threshold ) + SETBIT(seed->bytes,i-1); + return(hp->bitoffset); +} + +int32_t ramcoder_decode(struct ramcoder *coder,int32_t updateprobs,HUFF *hp) +{ + int32_t ind; +#define RAMDECODER_UNSCALED(coder) ((((uint32_t)coder->code - coder->lower) + 1) * (uint32_t)coder->cumulativeProb - 1) / (((uint32_t)coder->upper - coder->lower) + 1) + if ( (ind= ramdecoder_bsearch(RAMDECODER_UNSCALED(coder),coder)) < 0 || ind == coder->lastsymbol ) + return(-1); + if ( ramcoder_update(ind,coder,updateprobs,0,hp) < 0 ) + return(-1); + return(ind); +} + +int32_t ramcoder_decoder(struct ramcoder *coder,int32_t updateprobs,uint8_t *buf,int32_t maxlen,HUFF *hp,bits256 *seed) +{ + uint8_t _coder[sizeof(*coder) + (RAMCODER_MAXSYMBOLS+2)*sizeof(coder->ranges[0])]; + int32_t val,n = 0,numbits = 0; + if ( coder == 0 ) + { + memset(_coder,0,sizeof(_coder)); + coder = (struct ramcoder *)_coder; + hrewind(hp); + numbits = init_ramcoder(coder,hp,seed); + } + while ( n < maxlen ) + { + if ( (val= ramcoder_decode(coder,updateprobs,hp)) < 0 ) + return(n); + buf[n++] = val; + } + return(n); +} + +int32_t ramcoder_compress(uint8_t *bits,int32_t maxlen,uint8_t *data,int32_t datalen,uint64_t *histo,bits256 seed) +{ + int32_t numbits; HUFF H,*hp = &H; + _init_HUFF(hp,maxlen,bits); + if ( ramcoder_encoder(0,1,data,datalen,hp,histo,&seed) < 0 ) + return(-1); + numbits = hp->bitoffset; + if ( 0 ) + { + void *malloc(size_t); void free(void *); + int32_t i,checklen; uint8_t *checkbuf; + checkbuf = malloc(datalen*2); + memset(seed.bytes,0,sizeof(seed)); + hrewind(hp); + checklen = ramcoder_decoder(0,1,checkbuf,datalen*2,hp,&seed); + if ( checklen != datalen || memcmp(checkbuf,data,datalen) != 0 ) + { + for (i=0; i numbits %d %d\n",datalen,numbits,numbits/8); + free(checkbuf); + } + return(numbits); +} + +int32_t ramcoder_decompress(uint8_t *data,int32_t maxlen,uint8_t *bits,uint32_t numbits,bits256 seed) +{ + HUFF H,*hp = &H; + _init_HUFF(hp,(uint32_t)hconv_bitlen(numbits),bits); + hp->endpos = numbits; + hrewind(hp); + return(ramcoder_decoder(0,1,data,maxlen,hp,&seed)); +} + +#endif +#endif diff --git a/css/bootstrap.css b/css/bootstrap.css new file mode 100755 index 000000000..cd1c616ad --- /dev/null +++ b/css/bootstrap.css @@ -0,0 +1,5 @@ +/*! + * Bootstrap v3.3.4 (http://getbootstrap.com) + * Copyright 2011-2015 Twitter, Inc. + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + *//*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{margin:.67em 0;font-size:2em}mark{color:#000;background:#ff0}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{height:0;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{margin:0;font:inherit;color:inherit}button{overflow:visible}button,select{text-transform:none}button,html input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}input{line-height:normal}input[type=checkbox],input[type=radio]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:0}input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{height:auto}input[type=search]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}fieldset{padding:.35em .625em .75em;margin:0 2px;border:1px solid silver}legend{padding:0;border:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-spacing:0;border-collapse:collapse}td,th{padding:0}/*! Source: https://github.com/h5bp/html5-boilerplate/blob/master/src/css/main.css */@media print{*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}a,a:visited{text-decoration:underline}a[href]:after{content:" (" attr(href) ")"}abbr[title]:after{content:" (" attr(title) ")"}a[href^="javascript:"]:after,a[href^="#"]:after{content:""}blockquote,pre{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}img,tr{page-break-inside:avoid}img{max-width:100%!important}h2,h3,p{orphans:3;widows:3}h2,h3{page-break-after:avoid}select{background:#fff!important}.navbar{display:none}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table{border-collapse:collapse!important}.table td,.table th{background-color:#fff!important}.table-bordered td,.table-bordered th{border:1px solid #ddd!important}}@font-face{font-family:'Glyphicons Halflings';src:url(../fonts/glyphicons-halflings-regular.eot);src:url(../fonts/glyphicons-halflings-regular.eot?#iefix) format('embedded-opentype'),url(../fonts/glyphicons-halflings-regular.woff2) format('woff2'),url(../fonts/glyphicons-halflings-regular.woff) format('woff'),url(../fonts/glyphicons-halflings-regular.ttf) format('truetype'),url(../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular) format('svg')}.glyphicon{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:400;line-height:1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.glyphicon-asterisk:before{content:"\2a"}.glyphicon-plus:before{content:"\2b"}.glyphicon-eur:before,.glyphicon-euro:before{content:"\20ac"}.glyphicon-minus:before{content:"\2212"}.glyphicon-cloud:before{content:"\2601"}.glyphicon-envelope:before{content:"\2709"}.glyphicon-pencil:before{content:"\270f"}.glyphicon-glass:before{content:"\e001"}.glyphicon-music:before{content:"\e002"}.glyphicon-search:before{content:"\e003"}.glyphicon-heart:before{content:"\e005"}.glyphicon-star:before{content:"\e006"}.glyphicon-star-empty:before{content:"\e007"}.glyphicon-user:before{content:"\e008"}.glyphicon-film:before{content:"\e009"}.glyphicon-th-large:before{content:"\e010"}.glyphicon-th:before{content:"\e011"}.glyphicon-th-list:before{content:"\e012"}.glyphicon-ok:before{content:"\e013"}.glyphicon-remove:before{content:"\e014"}.glyphicon-zoom-in:before{content:"\e015"}.glyphicon-zoom-out:before{content:"\e016"}.glyphicon-off:before{content:"\e017"}.glyphicon-signal:before{content:"\e018"}.glyphicon-cog:before{content:"\e019"}.glyphicon-trash:before{content:"\e020"}.glyphicon-home:before{content:"\e021"}.glyphicon-file:before{content:"\e022"}.glyphicon-time:before{content:"\e023"}.glyphicon-road:before{content:"\e024"}.glyphicon-download-alt:before{content:"\e025"}.glyphicon-download:before{content:"\e026"}.glyphicon-upload:before{content:"\e027"}.glyphicon-inbox:before{content:"\e028"}.glyphicon-play-circle:before{content:"\e029"}.glyphicon-repeat:before{content:"\e030"}.glyphicon-refresh:before{content:"\e031"}.glyphicon-list-alt:before{content:"\e032"}.glyphicon-lock:before{content:"\e033"}.glyphicon-flag:before{content:"\e034"}.glyphicon-headphones:before{content:"\e035"}.glyphicon-volume-off:before{content:"\e036"}.glyphicon-volume-down:before{content:"\e037"}.glyphicon-volume-up:before{content:"\e038"}.glyphicon-qrcode:before{content:"\e039"}.glyphicon-barcode:before{content:"\e040"}.glyphicon-tag:before{content:"\e041"}.glyphicon-tags:before{content:"\e042"}.glyphicon-book:before{content:"\e043"}.glyphicon-bookmark:before{content:"\e044"}.glyphicon-print:before{content:"\e045"}.glyphicon-camera:before{content:"\e046"}.glyphicon-font:before{content:"\e047"}.glyphicon-bold:before{content:"\e048"}.glyphicon-italic:before{content:"\e049"}.glyphicon-text-height:before{content:"\e050"}.glyphicon-text-width:before{content:"\e051"}.glyphicon-align-left:before{content:"\e052"}.glyphicon-align-center:before{content:"\e053"}.glyphicon-align-right:before{content:"\e054"}.glyphicon-align-justify:before{content:"\e055"}.glyphicon-list:before{content:"\e056"}.glyphicon-indent-left:before{content:"\e057"}.glyphicon-indent-right:before{content:"\e058"}.glyphicon-facetime-video:before{content:"\e059"}.glyphicon-picture:before{content:"\e060"}.glyphicon-map-marker:before{content:"\e062"}.glyphicon-adjust:before{content:"\e063"}.glyphicon-tint:before{content:"\e064"}.glyphicon-edit:before{content:"\e065"}.glyphicon-share:before{content:"\e066"}.glyphicon-check:before{content:"\e067"}.glyphicon-move:before{content:"\e068"}.glyphicon-step-backward:before{content:"\e069"}.glyphicon-fast-backward:before{content:"\e070"}.glyphicon-backward:before{content:"\e071"}.glyphicon-play:before{content:"\e072"}.glyphicon-pause:before{content:"\e073"}.glyphicon-stop:before{content:"\e074"}.glyphicon-forward:before{content:"\e075"}.glyphicon-fast-forward:before{content:"\e076"}.glyphicon-step-forward:before{content:"\e077"}.glyphicon-eject:before{content:"\e078"}.glyphicon-chevron-left:before{content:"\e079"}.glyphicon-chevron-right:before{content:"\e080"}.glyphicon-plus-sign:before{content:"\e081"}.glyphicon-minus-sign:before{content:"\e082"}.glyphicon-remove-sign:before{content:"\e083"}.glyphicon-ok-sign:before{content:"\e084"}.glyphicon-question-sign:before{content:"\e085"}.glyphicon-info-sign:before{content:"\e086"}.glyphicon-screenshot:before{content:"\e087"}.glyphicon-remove-circle:before{content:"\e088"}.glyphicon-ok-circle:before{content:"\e089"}.glyphicon-ban-circle:before{content:"\e090"}.glyphicon-arrow-left:before{content:"\e091"}.glyphicon-arrow-right:before{content:"\e092"}.glyphicon-arrow-up:before{content:"\e093"}.glyphicon-arrow-down:before{content:"\e094"}.glyphicon-share-alt:before{content:"\e095"}.glyphicon-resize-full:before{content:"\e096"}.glyphicon-resize-small:before{content:"\e097"}.glyphicon-exclamation-sign:before{content:"\e101"}.glyphicon-gift:before{content:"\e102"}.glyphicon-leaf:before{content:"\e103"}.glyphicon-fire:before{content:"\e104"}.glyphicon-eye-open:before{content:"\e105"}.glyphicon-eye-close:before{content:"\e106"}.glyphicon-warning-sign:before{content:"\e107"}.glyphicon-plane:before{content:"\e108"}.glyphicon-calendar:before{content:"\e109"}.glyphicon-random:before{content:"\e110"}.glyphicon-comment:before{content:"\e111"}.glyphicon-magnet:before{content:"\e112"}.glyphicon-chevron-up:before{content:"\e113"}.glyphicon-chevron-down:before{content:"\e114"}.glyphicon-retweet:before{content:"\e115"}.glyphicon-shopping-cart:before{content:"\e116"}.glyphicon-folder-close:before{content:"\e117"}.glyphicon-folder-open:before{content:"\e118"}.glyphicon-resize-vertical:before{content:"\e119"}.glyphicon-resize-horizontal:before{content:"\e120"}.glyphicon-hdd:before{content:"\e121"}.glyphicon-bullhorn:before{content:"\e122"}.glyphicon-bell:before{content:"\e123"}.glyphicon-certificate:before{content:"\e124"}.glyphicon-thumbs-up:before{content:"\e125"}.glyphicon-thumbs-down:before{content:"\e126"}.glyphicon-hand-right:before{content:"\e127"}.glyphicon-hand-left:before{content:"\e128"}.glyphicon-hand-up:before{content:"\e129"}.glyphicon-hand-down:before{content:"\e130"}.glyphicon-circle-arrow-right:before{content:"\e131"}.glyphicon-circle-arrow-left:before{content:"\e132"}.glyphicon-circle-arrow-up:before{content:"\e133"}.glyphicon-circle-arrow-down:before{content:"\e134"}.glyphicon-globe:before{content:"\e135"}.glyphicon-wrench:before{content:"\e136"}.glyphicon-tasks:before{content:"\e137"}.glyphicon-filter:before{content:"\e138"}.glyphicon-briefcase:before{content:"\e139"}.glyphicon-fullscreen:before{content:"\e140"}.glyphicon-dashboard:before{content:"\e141"}.glyphicon-paperclip:before{content:"\e142"}.glyphicon-heart-empty:before{content:"\e143"}.glyphicon-link:before{content:"\e144"}.glyphicon-phone:before{content:"\e145"}.glyphicon-pushpin:before{content:"\e146"}.glyphicon-usd:before{content:"\e148"}.glyphicon-gbp:before{content:"\e149"}.glyphicon-sort:before{content:"\e150"}.glyphicon-sort-by-alphabet:before{content:"\e151"}.glyphicon-sort-by-alphabet-alt:before{content:"\e152"}.glyphicon-sort-by-order:before{content:"\e153"}.glyphicon-sort-by-order-alt:before{content:"\e154"}.glyphicon-sort-by-attributes:before{content:"\e155"}.glyphicon-sort-by-attributes-alt:before{content:"\e156"}.glyphicon-unchecked:before{content:"\e157"}.glyphicon-expand:before{content:"\e158"}.glyphicon-collapse-down:before{content:"\e159"}.glyphicon-collapse-up:before{content:"\e160"}.glyphicon-log-in:before{content:"\e161"}.glyphicon-flash:before{content:"\e162"}.glyphicon-log-out:before{content:"\e163"}.glyphicon-new-window:before{content:"\e164"}.glyphicon-record:before{content:"\e165"}.glyphicon-save:before{content:"\e166"}.glyphicon-open:before{content:"\e167"}.glyphicon-saved:before{content:"\e168"}.glyphicon-import:before{content:"\e169"}.glyphicon-export:before{content:"\e170"}.glyphicon-send:before{content:"\e171"}.glyphicon-floppy-disk:before{content:"\e172"}.glyphicon-floppy-saved:before{content:"\e173"}.glyphicon-floppy-remove:before{content:"\e174"}.glyphicon-floppy-save:before{content:"\e175"}.glyphicon-floppy-open:before{content:"\e176"}.glyphicon-credit-card:before{content:"\e177"}.glyphicon-transfer:before{content:"\e178"}.glyphicon-cutlery:before{content:"\e179"}.glyphicon-header:before{content:"\e180"}.glyphicon-compressed:before{content:"\e181"}.glyphicon-earphone:before{content:"\e182"}.glyphicon-phone-alt:before{content:"\e183"}.glyphicon-tower:before{content:"\e184"}.glyphicon-stats:before{content:"\e185"}.glyphicon-sd-video:before{content:"\e186"}.glyphicon-hd-video:before{content:"\e187"}.glyphicon-subtitles:before{content:"\e188"}.glyphicon-sound-stereo:before{content:"\e189"}.glyphicon-sound-dolby:before{content:"\e190"}.glyphicon-sound-5-1:before{content:"\e191"}.glyphicon-sound-6-1:before{content:"\e192"}.glyphicon-sound-7-1:before{content:"\e193"}.glyphicon-copyright-mark:before{content:"\e194"}.glyphicon-registration-mark:before{content:"\e195"}.glyphicon-cloud-download:before{content:"\e197"}.glyphicon-cloud-upload:before{content:"\e198"}.glyphicon-tree-conifer:before{content:"\e199"}.glyphicon-tree-deciduous:before{content:"\e200"}.glyphicon-cd:before{content:"\e201"}.glyphicon-save-file:before{content:"\e202"}.glyphicon-open-file:before{content:"\e203"}.glyphicon-level-up:before{content:"\e204"}.glyphicon-copy:before{content:"\e205"}.glyphicon-paste:before{content:"\e206"}.glyphicon-alert:before{content:"\e209"}.glyphicon-equalizer:before{content:"\e210"}.glyphicon-king:before{content:"\e211"}.glyphicon-queen:before{content:"\e212"}.glyphicon-pawn:before{content:"\e213"}.glyphicon-bishop:before{content:"\e214"}.glyphicon-knight:before{content:"\e215"}.glyphicon-baby-formula:before{content:"\e216"}.glyphicon-tent:before{content:"\26fa"}.glyphicon-blackboard:before{content:"\e218"}.glyphicon-bed:before{content:"\e219"}.glyphicon-apple:before{content:"\f8ff"}.glyphicon-erase:before{content:"\e221"}.glyphicon-hourglass:before{content:"\231b"}.glyphicon-lamp:before{content:"\e223"}.glyphicon-duplicate:before{content:"\e224"}.glyphicon-piggy-bank:before{content:"\e225"}.glyphicon-scissors:before{content:"\e226"}.glyphicon-bitcoin:before{content:"\e227"}.glyphicon-btc:before{content:"\e227"}.glyphicon-xbt:before{content:"\e227"}.glyphicon-yen:before{content:"\00a5"}.glyphicon-jpy:before{content:"\00a5"}.glyphicon-ruble:before{content:"\20bd"}.glyphicon-rub:before{content:"\20bd"}.glyphicon-scale:before{content:"\e230"}.glyphicon-ice-lolly:before{content:"\e231"}.glyphicon-ice-lolly-tasted:before{content:"\e232"}.glyphicon-education:before{content:"\e233"}.glyphicon-option-horizontal:before{content:"\e234"}.glyphicon-option-vertical:before{content:"\e235"}.glyphicon-menu-hamburger:before{content:"\e236"}.glyphicon-modal-window:before{content:"\e237"}.glyphicon-oil:before{content:"\e238"}.glyphicon-grain:before{content:"\e239"}.glyphicon-sunglasses:before{content:"\e240"}.glyphicon-text-size:before{content:"\e241"}.glyphicon-text-color:before{content:"\e242"}.glyphicon-text-background:before{content:"\e243"}.glyphicon-object-align-top:before{content:"\e244"}.glyphicon-object-align-bottom:before{content:"\e245"}.glyphicon-object-align-horizontal:before{content:"\e246"}.glyphicon-object-align-left:before{content:"\e247"}.glyphicon-object-align-vertical:before{content:"\e248"}.glyphicon-object-align-right:before{content:"\e249"}.glyphicon-triangle-right:before{content:"\e250"}.glyphicon-triangle-left:before{content:"\e251"}.glyphicon-triangle-bottom:before{content:"\e252"}.glyphicon-triangle-top:before{content:"\e253"}.glyphicon-console:before{content:"\e254"}.glyphicon-superscript:before{content:"\e255"}.glyphicon-subscript:before{content:"\e256"}.glyphicon-menu-left:before{content:"\e257"}.glyphicon-menu-right:before{content:"\e258"}.glyphicon-menu-down:before{content:"\e259"}.glyphicon-menu-up:before{content:"\e260"}*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}:after,:before{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}html{font-size:10px;-webkit-tap-highlight-color:rgba(0,0,0,0)}body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;line-height:1.42857143;color:#333;background-color:#fff}button,input,select,textarea{font-family:inherit;font-size:inherit;line-height:inherit}a{color:#337ab7;text-decoration:none}a:focus,a:hover{color:#23527c;text-decoration:underline}a:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}figure{margin:0}img{vertical-align:middle}.carousel-inner>.item>a>img,.carousel-inner>.item>img,.img-responsive,.thumbnail a>img,.thumbnail>img{display:block;max-width:100%;height:auto}.img-rounded{border-radius:6px}.img-thumbnail{display:inline-block;max-width:100%;height:auto;padding:4px;line-height:1.42857143;background-color:#fff;border:1px solid #ddd;border-radius:4px;-webkit-transition:all .2s ease-in-out;-o-transition:all .2s ease-in-out;transition:all .2s ease-in-out}.img-circle{border-radius:50%}hr{margin-top:20px;margin-bottom:20px;border:0;border-top:1px solid #eee}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}.sr-only-focusable:active,.sr-only-focusable:focus{position:static;width:auto;height:auto;margin:0;overflow:visible;clip:auto}[role=button]{cursor:pointer}.h1,.h2,.h3,.h4,.h5,.h6,h1,h2,h3,h4,h5,h6{font-family:inherit;font-weight:500;line-height:1.1;color:inherit}.h1 .small,.h1 small,.h2 .small,.h2 small,.h3 .small,.h3 small,.h4 .small,.h4 small,.h5 .small,.h5 small,.h6 .small,.h6 small,h1 .small,h1 small,h2 .small,h2 small,h3 .small,h3 small,h4 .small,h4 small,h5 .small,h5 small,h6 .small,h6 small{font-weight:400;line-height:1;color:#777}.h1,.h2,.h3,h1,h2,h3{margin-top:20px;margin-bottom:10px}.h1 .small,.h1 small,.h2 .small,.h2 small,.h3 .small,.h3 small,h1 .small,h1 small,h2 .small,h2 small,h3 .small,h3 small{font-size:65%}.h4,.h5,.h6,h4,h5,h6{margin-top:10px;margin-bottom:10px}.h4 .small,.h4 small,.h5 .small,.h5 small,.h6 .small,.h6 small,h4 .small,h4 small,h5 .small,h5 small,h6 .small,h6 small{font-size:75%}.h1,h1{font-size:36px}.h2,h2{font-size:30px}.h3,h3{font-size:24px}.h4,h4{font-size:18px}.h5,h5{font-size:14px}.h6,h6{font-size:12px}p{margin:0 0 10px}.lead{margin-bottom:20px;font-size:16px;font-weight:300;line-height:1.4}@media (min-width:768px){.lead{font-size:21px}}.small,small{font-size:85%}.mark,mark{padding:.2em;background-color:#fcf8e3}.text-left{text-align:left}.text-right{text-align:right}.text-center{text-align:center}.text-justify{text-align:justify}.text-nowrap{white-space:nowrap}.text-lowercase{text-transform:lowercase}.text-uppercase{text-transform:uppercase}.text-capitalize{text-transform:capitalize}.text-muted{color:#777}.text-primary{color:#337ab7}a.text-primary:hover{color:#286090}.text-success{color:#3c763d}a.text-success:hover{color:#2b542c}.text-info{color:#31708f}a.text-info:hover{color:#245269}.text-warning{color:#8a6d3b}a.text-warning:hover{color:#66512c}.text-danger{color:#a94442}a.text-danger:hover{color:#843534}.bg-primary{color:#fff;background-color:#337ab7}a.bg-primary:hover{background-color:#286090}.bg-success{background-color:#dff0d8}a.bg-success:hover{background-color:#c1e2b3}.bg-info{background-color:#d9edf7}a.bg-info:hover{background-color:#afd9ee}.bg-warning{background-color:#fcf8e3}a.bg-warning:hover{background-color:#f7ecb5}.bg-danger{background-color:#f2dede}a.bg-danger:hover{background-color:#e4b9b9}.page-header{padding-bottom:9px;margin:40px 0 20px;border-bottom:1px solid #eee}ol,ul{margin-top:0;margin-bottom:10px}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}.list-unstyled{padding-left:0;list-style:none}.list-inline{padding-left:0;margin-left:-5px;list-style:none}.list-inline>li{display:inline-block;padding-right:5px;padding-left:5px}dl{margin-top:0;margin-bottom:20px}dd,dt{line-height:1.42857143}dt{font-weight:700}dd{margin-left:0}@media (min-width:768px){.dl-horizontal dt{float:left;width:160px;overflow:hidden;clear:left;text-align:right;text-overflow:ellipsis;white-space:nowrap}.dl-horizontal dd{margin-left:180px}}abbr[data-original-title],abbr[title]{cursor:help;border-bottom:1px dotted #777}.initialism{font-size:90%;text-transform:uppercase}blockquote{padding:10px 20px;margin:0 0 20px;font-size:17.5px;border-left:5px solid #eee}blockquote ol:last-child,blockquote p:last-child,blockquote ul:last-child{margin-bottom:0}blockquote .small,blockquote footer,blockquote small{display:block;font-size:80%;line-height:1.42857143;color:#777}blockquote .small:before,blockquote footer:before,blockquote small:before{content:'\2014 \00A0'}.blockquote-reverse,blockquote.pull-right{padding-right:15px;padding-left:0;text-align:right;border-right:5px solid #eee;border-left:0}.blockquote-reverse .small:before,.blockquote-reverse footer:before,.blockquote-reverse small:before,blockquote.pull-right .small:before,blockquote.pull-right footer:before,blockquote.pull-right small:before{content:''}.blockquote-reverse .small:after,.blockquote-reverse footer:after,.blockquote-reverse small:after,blockquote.pull-right .small:after,blockquote.pull-right footer:after,blockquote.pull-right small:after{content:'\00A0 \2014'}address{margin-bottom:20px;font-style:normal;line-height:1.42857143}code,kbd,pre,samp{font-family:Menlo,Monaco,Consolas,"Courier New",monospace}code{padding:2px 4px;font-size:90%;color:#c7254e;background-color:#f9f2f4;border-radius:4px}kbd{padding:2px 4px;font-size:90%;color:#fff;background-color:#333;border-radius:3px;-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,.25);box-shadow:inset 0 -1px 0 rgba(0,0,0,.25)}kbd kbd{padding:0;font-size:100%;font-weight:700;-webkit-box-shadow:none;box-shadow:none}pre{display:block;padding:9.5px;margin:0 0 10px;font-size:13px;line-height:1.42857143;color:#333;word-break:break-all;word-wrap:break-word;background-color:#f5f5f5;border:1px solid #ccc;border-radius:4px}pre code{padding:0;font-size:inherit;color:inherit;white-space:pre-wrap;background-color:transparent;border-radius:0}.pre-scrollable{max-height:340px;overflow-y:scroll}.container{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}@media (min-width:768px){.container{width:750px}}@media (min-width:992px){.container{width:970px}}@media (min-width:1200px){.container{width:1170px}}.container-fluid{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}.row{margin-right:-15px;margin-left:-15px}.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9{position:relative;min-height:1px;padding-right:15px;padding-left:15px}.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9{float:left}.col-xs-12{width:100%}.col-xs-11{width:91.66666667%}.col-xs-10{width:83.33333333%}.col-xs-9{width:75%}.col-xs-8{width:66.66666667%}.col-xs-7{width:58.33333333%}.col-xs-6{width:50%}.col-xs-5{width:41.66666667%}.col-xs-4{width:33.33333333%}.col-xs-3{width:25%}.col-xs-2{width:16.66666667%}.col-xs-1{width:8.33333333%}.col-xs-pull-12{right:100%}.col-xs-pull-11{right:91.66666667%}.col-xs-pull-10{right:83.33333333%}.col-xs-pull-9{right:75%}.col-xs-pull-8{right:66.66666667%}.col-xs-pull-7{right:58.33333333%}.col-xs-pull-6{right:50%}.col-xs-pull-5{right:41.66666667%}.col-xs-pull-4{right:33.33333333%}.col-xs-pull-3{right:25%}.col-xs-pull-2{right:16.66666667%}.col-xs-pull-1{right:8.33333333%}.col-xs-pull-0{right:auto}.col-xs-push-12{left:100%}.col-xs-push-11{left:91.66666667%}.col-xs-push-10{left:83.33333333%}.col-xs-push-9{left:75%}.col-xs-push-8{left:66.66666667%}.col-xs-push-7{left:58.33333333%}.col-xs-push-6{left:50%}.col-xs-push-5{left:41.66666667%}.col-xs-push-4{left:33.33333333%}.col-xs-push-3{left:25%}.col-xs-push-2{left:16.66666667%}.col-xs-push-1{left:8.33333333%}.col-xs-push-0{left:auto}.col-xs-offset-12{margin-left:100%}.col-xs-offset-11{margin-left:91.66666667%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-0{margin-left:0}@media (min-width:768px){.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9{float:left}.col-sm-12{width:100%}.col-sm-11{width:91.66666667%}.col-sm-10{width:83.33333333%}.col-sm-9{width:75%}.col-sm-8{width:66.66666667%}.col-sm-7{width:58.33333333%}.col-sm-6{width:50%}.col-sm-5{width:41.66666667%}.col-sm-4{width:33.33333333%}.col-sm-3{width:25%}.col-sm-2{width:16.66666667%}.col-sm-1{width:8.33333333%}.col-sm-pull-12{right:100%}.col-sm-pull-11{right:91.66666667%}.col-sm-pull-10{right:83.33333333%}.col-sm-pull-9{right:75%}.col-sm-pull-8{right:66.66666667%}.col-sm-pull-7{right:58.33333333%}.col-sm-pull-6{right:50%}.col-sm-pull-5{right:41.66666667%}.col-sm-pull-4{right:33.33333333%}.col-sm-pull-3{right:25%}.col-sm-pull-2{right:16.66666667%}.col-sm-pull-1{right:8.33333333%}.col-sm-pull-0{right:auto}.col-sm-push-12{left:100%}.col-sm-push-11{left:91.66666667%}.col-sm-push-10{left:83.33333333%}.col-sm-push-9{left:75%}.col-sm-push-8{left:66.66666667%}.col-sm-push-7{left:58.33333333%}.col-sm-push-6{left:50%}.col-sm-push-5{left:41.66666667%}.col-sm-push-4{left:33.33333333%}.col-sm-push-3{left:25%}.col-sm-push-2{left:16.66666667%}.col-sm-push-1{left:8.33333333%}.col-sm-push-0{left:auto}.col-sm-offset-12{margin-left:100%}.col-sm-offset-11{margin-left:91.66666667%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-0{margin-left:0}}@media (min-width:992px){.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9{float:left}.col-md-12{width:100%}.col-md-11{width:91.66666667%}.col-md-10{width:83.33333333%}.col-md-9{width:75%}.col-md-8{width:66.66666667%}.col-md-7{width:58.33333333%}.col-md-6{width:50%}.col-md-5{width:41.66666667%}.col-md-4{width:33.33333333%}.col-md-3{width:25%}.col-md-2{width:16.66666667%}.col-md-1{width:8.33333333%}.col-md-pull-12{right:100%}.col-md-pull-11{right:91.66666667%}.col-md-pull-10{right:83.33333333%}.col-md-pull-9{right:75%}.col-md-pull-8{right:66.66666667%}.col-md-pull-7{right:58.33333333%}.col-md-pull-6{right:50%}.col-md-pull-5{right:41.66666667%}.col-md-pull-4{right:33.33333333%}.col-md-pull-3{right:25%}.col-md-pull-2{right:16.66666667%}.col-md-pull-1{right:8.33333333%}.col-md-pull-0{right:auto}.col-md-push-12{left:100%}.col-md-push-11{left:91.66666667%}.col-md-push-10{left:83.33333333%}.col-md-push-9{left:75%}.col-md-push-8{left:66.66666667%}.col-md-push-7{left:58.33333333%}.col-md-push-6{left:50%}.col-md-push-5{left:41.66666667%}.col-md-push-4{left:33.33333333%}.col-md-push-3{left:25%}.col-md-push-2{left:16.66666667%}.col-md-push-1{left:8.33333333%}.col-md-push-0{left:auto}.col-md-offset-12{margin-left:100%}.col-md-offset-11{margin-left:91.66666667%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-9{margin-left:75%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-6{margin-left:50%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-3{margin-left:25%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-0{margin-left:0}}@media (min-width:1200px){.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9{float:left}.col-lg-12{width:100%}.col-lg-11{width:91.66666667%}.col-lg-10{width:83.33333333%}.col-lg-9{width:75%}.col-lg-8{width:66.66666667%}.col-lg-7{width:58.33333333%}.col-lg-6{width:50%}.col-lg-5{width:41.66666667%}.col-lg-4{width:33.33333333%}.col-lg-3{width:25%}.col-lg-2{width:16.66666667%}.col-lg-1{width:8.33333333%}.col-lg-pull-12{right:100%}.col-lg-pull-11{right:91.66666667%}.col-lg-pull-10{right:83.33333333%}.col-lg-pull-9{right:75%}.col-lg-pull-8{right:66.66666667%}.col-lg-pull-7{right:58.33333333%}.col-lg-pull-6{right:50%}.col-lg-pull-5{right:41.66666667%}.col-lg-pull-4{right:33.33333333%}.col-lg-pull-3{right:25%}.col-lg-pull-2{right:16.66666667%}.col-lg-pull-1{right:8.33333333%}.col-lg-pull-0{right:auto}.col-lg-push-12{left:100%}.col-lg-push-11{left:91.66666667%}.col-lg-push-10{left:83.33333333%}.col-lg-push-9{left:75%}.col-lg-push-8{left:66.66666667%}.col-lg-push-7{left:58.33333333%}.col-lg-push-6{left:50%}.col-lg-push-5{left:41.66666667%}.col-lg-push-4{left:33.33333333%}.col-lg-push-3{left:25%}.col-lg-push-2{left:16.66666667%}.col-lg-push-1{left:8.33333333%}.col-lg-push-0{left:auto}.col-lg-offset-12{margin-left:100%}.col-lg-offset-11{margin-left:91.66666667%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-0{margin-left:0}}table{background-color:transparent}caption{padding-top:8px;padding-bottom:8px;color:#777;text-align:left}th{text-align:left}.table{width:100%;max-width:100%;margin-bottom:20px}.table>tbody>tr>td,.table>tbody>tr>th,.table>tfoot>tr>td,.table>tfoot>tr>th,.table>thead>tr>td,.table>thead>tr>th{padding:8px;line-height:1.42857143;vertical-align:top;border-top:1px solid #ddd}.table>thead>tr>th{vertical-align:bottom;border-bottom:2px solid #ddd}.table>caption+thead>tr:first-child>td,.table>caption+thead>tr:first-child>th,.table>colgroup+thead>tr:first-child>td,.table>colgroup+thead>tr:first-child>th,.table>thead:first-child>tr:first-child>td,.table>thead:first-child>tr:first-child>th{border-top:0}.table>tbody+tbody{border-top:2px solid #ddd}.table .table{background-color:#fff}.table-condensed>tbody>tr>td,.table-condensed>tbody>tr>th,.table-condensed>tfoot>tr>td,.table-condensed>tfoot>tr>th,.table-condensed>thead>tr>td,.table-condensed>thead>tr>th{padding:5px}.table-bordered{border:1px solid #ddd}.table-bordered>tbody>tr>td,.table-bordered>tbody>tr>th,.table-bordered>tfoot>tr>td,.table-bordered>tfoot>tr>th,.table-bordered>thead>tr>td,.table-bordered>thead>tr>th{border:1px solid #ddd}.table-bordered>thead>tr>td,.table-bordered>thead>tr>th{border-bottom-width:2px}.table-striped>tbody>tr:nth-of-type(odd){background-color:#f9f9f9}.table-hover>tbody>tr:hover{background-color:#f5f5f5}table col[class*=col-]{position:static;display:table-column;float:none}table td[class*=col-],table th[class*=col-]{position:static;display:table-cell;float:none}.table>tbody>tr.active>td,.table>tbody>tr.active>th,.table>tbody>tr>td.active,.table>tbody>tr>th.active,.table>tfoot>tr.active>td,.table>tfoot>tr.active>th,.table>tfoot>tr>td.active,.table>tfoot>tr>th.active,.table>thead>tr.active>td,.table>thead>tr.active>th,.table>thead>tr>td.active,.table>thead>tr>th.active{background-color:#f5f5f5}.table-hover>tbody>tr.active:hover>td,.table-hover>tbody>tr.active:hover>th,.table-hover>tbody>tr:hover>.active,.table-hover>tbody>tr>td.active:hover,.table-hover>tbody>tr>th.active:hover{background-color:#e8e8e8}.table>tbody>tr.success>td,.table>tbody>tr.success>th,.table>tbody>tr>td.success,.table>tbody>tr>th.success,.table>tfoot>tr.success>td,.table>tfoot>tr.success>th,.table>tfoot>tr>td.success,.table>tfoot>tr>th.success,.table>thead>tr.success>td,.table>thead>tr.success>th,.table>thead>tr>td.success,.table>thead>tr>th.success{background-color:#dff0d8}.table-hover>tbody>tr.success:hover>td,.table-hover>tbody>tr.success:hover>th,.table-hover>tbody>tr:hover>.success,.table-hover>tbody>tr>td.success:hover,.table-hover>tbody>tr>th.success:hover{background-color:#d0e9c6}.table>tbody>tr.info>td,.table>tbody>tr.info>th,.table>tbody>tr>td.info,.table>tbody>tr>th.info,.table>tfoot>tr.info>td,.table>tfoot>tr.info>th,.table>tfoot>tr>td.info,.table>tfoot>tr>th.info,.table>thead>tr.info>td,.table>thead>tr.info>th,.table>thead>tr>td.info,.table>thead>tr>th.info{background-color:#d9edf7}.table-hover>tbody>tr.info:hover>td,.table-hover>tbody>tr.info:hover>th,.table-hover>tbody>tr:hover>.info,.table-hover>tbody>tr>td.info:hover,.table-hover>tbody>tr>th.info:hover{background-color:#c4e3f3}.table>tbody>tr.warning>td,.table>tbody>tr.warning>th,.table>tbody>tr>td.warning,.table>tbody>tr>th.warning,.table>tfoot>tr.warning>td,.table>tfoot>tr.warning>th,.table>tfoot>tr>td.warning,.table>tfoot>tr>th.warning,.table>thead>tr.warning>td,.table>thead>tr.warning>th,.table>thead>tr>td.warning,.table>thead>tr>th.warning{background-color:#fcf8e3}.table-hover>tbody>tr.warning:hover>td,.table-hover>tbody>tr.warning:hover>th,.table-hover>tbody>tr:hover>.warning,.table-hover>tbody>tr>td.warning:hover,.table-hover>tbody>tr>th.warning:hover{background-color:#faf2cc}.table>tbody>tr.danger>td,.table>tbody>tr.danger>th,.table>tbody>tr>td.danger,.table>tbody>tr>th.danger,.table>tfoot>tr.danger>td,.table>tfoot>tr.danger>th,.table>tfoot>tr>td.danger,.table>tfoot>tr>th.danger,.table>thead>tr.danger>td,.table>thead>tr.danger>th,.table>thead>tr>td.danger,.table>thead>tr>th.danger{background-color:#f2dede}.table-hover>tbody>tr.danger:hover>td,.table-hover>tbody>tr.danger:hover>th,.table-hover>tbody>tr:hover>.danger,.table-hover>tbody>tr>td.danger:hover,.table-hover>tbody>tr>th.danger:hover{background-color:#ebcccc}.table-responsive{min-height:.01%;overflow-x:auto}@media screen and (max-width:767px){.table-responsive{width:100%;margin-bottom:15px;overflow-y:hidden;-ms-overflow-style:-ms-autohiding-scrollbar;border:1px solid #ddd}.table-responsive>.table{margin-bottom:0}.table-responsive>.table>tbody>tr>td,.table-responsive>.table>tbody>tr>th,.table-responsive>.table>tfoot>tr>td,.table-responsive>.table>tfoot>tr>th,.table-responsive>.table>thead>tr>td,.table-responsive>.table>thead>tr>th{white-space:nowrap}.table-responsive>.table-bordered{border:0}.table-responsive>.table-bordered>tbody>tr>td:first-child,.table-responsive>.table-bordered>tbody>tr>th:first-child,.table-responsive>.table-bordered>tfoot>tr>td:first-child,.table-responsive>.table-bordered>tfoot>tr>th:first-child,.table-responsive>.table-bordered>thead>tr>td:first-child,.table-responsive>.table-bordered>thead>tr>th:first-child{border-left:0}.table-responsive>.table-bordered>tbody>tr>td:last-child,.table-responsive>.table-bordered>tbody>tr>th:last-child,.table-responsive>.table-bordered>tfoot>tr>td:last-child,.table-responsive>.table-bordered>tfoot>tr>th:last-child,.table-responsive>.table-bordered>thead>tr>td:last-child,.table-responsive>.table-bordered>thead>tr>th:last-child{border-right:0}.table-responsive>.table-bordered>tbody>tr:last-child>td,.table-responsive>.table-bordered>tbody>tr:last-child>th,.table-responsive>.table-bordered>tfoot>tr:last-child>td,.table-responsive>.table-bordered>tfoot>tr:last-child>th{border-bottom:0}}fieldset{min-width:0;padding:0;margin:0;border:0}legend{display:block;width:100%;padding:0;margin-bottom:20px;font-size:21px;line-height:inherit;color:#333;border:0;border-bottom:1px solid #e5e5e5}label{display:inline-block;max-width:100%;margin-bottom:5px;font-weight:700}input[type=search]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}input[type=checkbox],input[type=radio]{margin:4px 0 0;margin-top:1px \9;line-height:normal}input[type=file]{display:block}input[type=range]{display:block;width:100%}select[multiple],select[size]{height:auto}input[type=file]:focus,input[type=checkbox]:focus,input[type=radio]:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}output{display:block;padding-top:7px;font-size:14px;line-height:1.42857143;color:#555}.form-control{display:block;width:100%;height:34px;padding:6px 12px;font-size:14px;line-height:1.42857143;color:#555;background-color:#fff;background-image:none;border:1px solid #ccc;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075);-webkit-transition:border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;-o-transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s;transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s}.form-control:focus{border-color:#66afe9;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)}.form-control::-moz-placeholder{color:#999;opacity:1}.form-control:-ms-input-placeholder{color:#999}.form-control::-webkit-input-placeholder{color:#999}.form-control[disabled],.form-control[readonly],fieldset[disabled] .form-control{background-color:#eee;opacity:1}.form-control[disabled],fieldset[disabled] .form-control{cursor:not-allowed}textarea.form-control{height:auto}input[type=search]{-webkit-appearance:none}@media screen and (-webkit-min-device-pixel-ratio:0){input[type=date],input[type=time],input[type=datetime-local],input[type=month]{line-height:34px}.input-group-sm input[type=date],.input-group-sm input[type=time],.input-group-sm input[type=datetime-local],.input-group-sm input[type=month],input[type=date].input-sm,input[type=time].input-sm,input[type=datetime-local].input-sm,input[type=month].input-sm{line-height:30px}.input-group-lg input[type=date],.input-group-lg input[type=time],.input-group-lg input[type=datetime-local],.input-group-lg input[type=month],input[type=date].input-lg,input[type=time].input-lg,input[type=datetime-local].input-lg,input[type=month].input-lg{line-height:46px}}.form-group{margin-bottom:15px}.checkbox,.radio{position:relative;display:block;margin-top:10px;margin-bottom:10px}.checkbox label,.radio label{min-height:20px;padding-left:20px;margin-bottom:0;font-weight:400;cursor:pointer}.checkbox input[type=checkbox],.checkbox-inline input[type=checkbox],.radio input[type=radio],.radio-inline input[type=radio]{position:absolute;margin-top:4px \9;margin-left:-20px}.checkbox+.checkbox,.radio+.radio{margin-top:-5px}.checkbox-inline,.radio-inline{position:relative;display:inline-block;padding-left:20px;margin-bottom:0;font-weight:400;vertical-align:middle;cursor:pointer}.checkbox-inline+.checkbox-inline,.radio-inline+.radio-inline{margin-top:0;margin-left:10px}fieldset[disabled] input[type=checkbox],fieldset[disabled] input[type=radio],input[type=checkbox].disabled,input[type=checkbox][disabled],input[type=radio].disabled,input[type=radio][disabled]{cursor:not-allowed}.checkbox-inline.disabled,.radio-inline.disabled,fieldset[disabled] .checkbox-inline,fieldset[disabled] .radio-inline{cursor:not-allowed}.checkbox.disabled label,.radio.disabled label,fieldset[disabled] .checkbox label,fieldset[disabled] .radio label{cursor:not-allowed}.form-control-static{min-height:34px;padding-top:7px;padding-bottom:7px;margin-bottom:0}.form-control-static.input-lg,.form-control-static.input-sm{padding-right:0;padding-left:0}.input-sm{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.input-sm{height:30px;line-height:30px}select[multiple].input-sm,textarea.input-sm{height:auto}.form-group-sm .form-control{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.form-group-sm .form-control{height:30px;line-height:30px}select[multiple].form-group-sm .form-control,textarea.form-group-sm .form-control{height:auto}.form-group-sm .form-control-static{height:30px;min-height:32px;padding:5px 10px;font-size:12px;line-height:1.5}.input-lg{height:46px;padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}select.input-lg{height:46px;line-height:46px}select[multiple].input-lg,textarea.input-lg{height:auto}.form-group-lg .form-control{height:46px;padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}select.form-group-lg .form-control{height:46px;line-height:46px}select[multiple].form-group-lg .form-control,textarea.form-group-lg .form-control{height:auto}.form-group-lg .form-control-static{height:46px;min-height:38px;padding:10px 16px;font-size:18px;line-height:1.3333333}.has-feedback{position:relative}.has-feedback .form-control{padding-right:42.5px}.form-control-feedback{position:absolute;top:0;right:0;z-index:2;display:block;width:34px;height:34px;line-height:34px;text-align:center;pointer-events:none}.input-lg+.form-control-feedback{width:46px;height:46px;line-height:46px}.input-sm+.form-control-feedback{width:30px;height:30px;line-height:30px}.has-success .checkbox,.has-success .checkbox-inline,.has-success .control-label,.has-success .help-block,.has-success .radio,.has-success .radio-inline,.has-success.checkbox label,.has-success.checkbox-inline label,.has-success.radio label,.has-success.radio-inline label{color:#3c763d}.has-success .form-control{border-color:#3c763d;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-success .form-control:focus{border-color:#2b542c;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #67b168;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #67b168}.has-success .input-group-addon{color:#3c763d;background-color:#dff0d8;border-color:#3c763d}.has-success .form-control-feedback{color:#3c763d}.has-warning .checkbox,.has-warning .checkbox-inline,.has-warning .control-label,.has-warning .help-block,.has-warning .radio,.has-warning .radio-inline,.has-warning.checkbox label,.has-warning.checkbox-inline label,.has-warning.radio label,.has-warning.radio-inline label{color:#8a6d3b}.has-warning .form-control{border-color:#8a6d3b;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-warning .form-control:focus{border-color:#66512c;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #c0a16b;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #c0a16b}.has-warning .input-group-addon{color:#8a6d3b;background-color:#fcf8e3;border-color:#8a6d3b}.has-warning .form-control-feedback{color:#8a6d3b}.has-error .checkbox,.has-error .checkbox-inline,.has-error .control-label,.has-error .help-block,.has-error .radio,.has-error .radio-inline,.has-error.checkbox label,.has-error.checkbox-inline label,.has-error.radio label,.has-error.radio-inline label{color:#a94442}.has-error .form-control{border-color:#a94442;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-error .form-control:focus{border-color:#843534;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #ce8483;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #ce8483}.has-error .input-group-addon{color:#a94442;background-color:#f2dede;border-color:#a94442}.has-error .form-control-feedback{color:#a94442}.has-feedback label~.form-control-feedback{top:25px}.has-feedback label.sr-only~.form-control-feedback{top:0}.help-block{display:block;margin-top:5px;margin-bottom:10px;color:#737373}@media (min-width:768px){.form-inline .form-group{display:inline-block;margin-bottom:0;vertical-align:middle}.form-inline .form-control{display:inline-block;width:auto;vertical-align:middle}.form-inline .form-control-static{display:inline-block}.form-inline .input-group{display:inline-table;vertical-align:middle}.form-inline .input-group .form-control,.form-inline .input-group .input-group-addon,.form-inline .input-group .input-group-btn{width:auto}.form-inline .input-group>.form-control{width:100%}.form-inline .control-label{margin-bottom:0;vertical-align:middle}.form-inline .checkbox,.form-inline .radio{display:inline-block;margin-top:0;margin-bottom:0;vertical-align:middle}.form-inline .checkbox label,.form-inline .radio label{padding-left:0}.form-inline .checkbox input[type=checkbox],.form-inline .radio input[type=radio]{position:relative;margin-left:0}.form-inline .has-feedback .form-control-feedback{top:0}}.form-horizontal .checkbox,.form-horizontal .checkbox-inline,.form-horizontal .radio,.form-horizontal .radio-inline{padding-top:7px;margin-top:0;margin-bottom:0}.form-horizontal .checkbox,.form-horizontal .radio{min-height:27px}.form-horizontal .form-group{margin-right:-15px;margin-left:-15px}@media (min-width:768px){.form-horizontal .control-label{padding-top:7px;margin-bottom:0;text-align:right}}.form-horizontal .has-feedback .form-control-feedback{right:15px}@media (min-width:768px){.form-horizontal .form-group-lg .control-label{padding-top:14.33px}}@media (min-width:768px){.form-horizontal .form-group-sm .control-label{padding-top:6px}}.btn{display:inline-block;padding:6px 12px;margin-bottom:0;font-size:14px;font-weight:400;line-height:1.42857143;text-align:center;white-space:nowrap;vertical-align:middle;-ms-touch-action:manipulation;touch-action:manipulation;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-image:none;border:1px solid transparent;border-radius:4px}.btn.active.focus,.btn.active:focus,.btn.focus,.btn:active.focus,.btn:active:focus,.btn:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}.btn.focus,.btn:focus,.btn:hover{color:#333;text-decoration:none}.btn.active,.btn:active{background-image:none;outline:0;-webkit-box-shadow:inset 0 3px 5px rgba(0,0,0,.125);box-shadow:inset 0 3px 5px rgba(0,0,0,.125)}.btn.disabled,.btn[disabled],fieldset[disabled] .btn{pointer-events:none;cursor:not-allowed;filter:alpha(opacity=65);-webkit-box-shadow:none;box-shadow:none;opacity:.65}.btn-default{color:#333;background-color:#fff;border-color:#ccc}.btn-default.active,.btn-default.focus,.btn-default:active,.btn-default:focus,.btn-default:hover,.open>.dropdown-toggle.btn-default{color:#333;background-color:#e6e6e6;border-color:#adadad}.btn-default.active,.btn-default:active,.open>.dropdown-toggle.btn-default{background-image:none}.btn-default.disabled,.btn-default.disabled.active,.btn-default.disabled.focus,.btn-default.disabled:active,.btn-default.disabled:focus,.btn-default.disabled:hover,.btn-default[disabled],.btn-default[disabled].active,.btn-default[disabled].focus,.btn-default[disabled]:active,.btn-default[disabled]:focus,.btn-default[disabled]:hover,fieldset[disabled] .btn-default,fieldset[disabled] .btn-default.active,fieldset[disabled] .btn-default.focus,fieldset[disabled] .btn-default:active,fieldset[disabled] .btn-default:focus,fieldset[disabled] .btn-default:hover{background-color:#fff;border-color:#ccc}.btn-default .badge{color:#fff;background-color:#333}.btn-primary{color:#fff;background-color:#337ab7;border-color:#2e6da4}.btn-primary.active,.btn-primary.focus,.btn-primary:active,.btn-primary:focus,.btn-primary:hover,.open>.dropdown-toggle.btn-primary{color:#fff;background-color:#286090;border-color:#204d74}.btn-primary.active,.btn-primary:active,.open>.dropdown-toggle.btn-primary{background-image:none}.btn-primary.disabled,.btn-primary.disabled.active,.btn-primary.disabled.focus,.btn-primary.disabled:active,.btn-primary.disabled:focus,.btn-primary.disabled:hover,.btn-primary[disabled],.btn-primary[disabled].active,.btn-primary[disabled].focus,.btn-primary[disabled]:active,.btn-primary[disabled]:focus,.btn-primary[disabled]:hover,fieldset[disabled] .btn-primary,fieldset[disabled] .btn-primary.active,fieldset[disabled] .btn-primary.focus,fieldset[disabled] .btn-primary:active,fieldset[disabled] .btn-primary:focus,fieldset[disabled] .btn-primary:hover{background-color:#337ab7;border-color:#2e6da4}.btn-primary .badge{color:#337ab7;background-color:#fff}.btn-success{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.btn-success.active,.btn-success.focus,.btn-success:active,.btn-success:focus,.btn-success:hover,.open>.dropdown-toggle.btn-success{color:#fff;background-color:#449d44;border-color:#398439}.btn-success.active,.btn-success:active,.open>.dropdown-toggle.btn-success{background-image:none}.btn-success.disabled,.btn-success.disabled.active,.btn-success.disabled.focus,.btn-success.disabled:active,.btn-success.disabled:focus,.btn-success.disabled:hover,.btn-success[disabled],.btn-success[disabled].active,.btn-success[disabled].focus,.btn-success[disabled]:active,.btn-success[disabled]:focus,.btn-success[disabled]:hover,fieldset[disabled] .btn-success,fieldset[disabled] .btn-success.active,fieldset[disabled] .btn-success.focus,fieldset[disabled] .btn-success:active,fieldset[disabled] .btn-success:focus,fieldset[disabled] .btn-success:hover{background-color:#5cb85c;border-color:#4cae4c}.btn-success .badge{color:#5cb85c;background-color:#fff}.btn-info{color:#fff;background-color:#5bc0de;border-color:#46b8da}.btn-info.active,.btn-info.focus,.btn-info:active,.btn-info:focus,.btn-info:hover,.open>.dropdown-toggle.btn-info{color:#fff;background-color:#31b0d5;border-color:#269abc}.btn-info.active,.btn-info:active,.open>.dropdown-toggle.btn-info{background-image:none}.btn-info.disabled,.btn-info.disabled.active,.btn-info.disabled.focus,.btn-info.disabled:active,.btn-info.disabled:focus,.btn-info.disabled:hover,.btn-info[disabled],.btn-info[disabled].active,.btn-info[disabled].focus,.btn-info[disabled]:active,.btn-info[disabled]:focus,.btn-info[disabled]:hover,fieldset[disabled] .btn-info,fieldset[disabled] .btn-info.active,fieldset[disabled] .btn-info.focus,fieldset[disabled] .btn-info:active,fieldset[disabled] .btn-info:focus,fieldset[disabled] .btn-info:hover{background-color:#5bc0de;border-color:#46b8da}.btn-info .badge{color:#5bc0de;background-color:#fff}.btn-warning{color:#fff;background-color:#f0ad4e;border-color:#eea236}.btn-warning.active,.btn-warning.focus,.btn-warning:active,.btn-warning:focus,.btn-warning:hover,.open>.dropdown-toggle.btn-warning{color:#fff;background-color:#ec971f;border-color:#d58512}.btn-warning.active,.btn-warning:active,.open>.dropdown-toggle.btn-warning{background-image:none}.btn-warning.disabled,.btn-warning.disabled.active,.btn-warning.disabled.focus,.btn-warning.disabled:active,.btn-warning.disabled:focus,.btn-warning.disabled:hover,.btn-warning[disabled],.btn-warning[disabled].active,.btn-warning[disabled].focus,.btn-warning[disabled]:active,.btn-warning[disabled]:focus,.btn-warning[disabled]:hover,fieldset[disabled] .btn-warning,fieldset[disabled] .btn-warning.active,fieldset[disabled] .btn-warning.focus,fieldset[disabled] .btn-warning:active,fieldset[disabled] .btn-warning:focus,fieldset[disabled] .btn-warning:hover{background-color:#f0ad4e;border-color:#eea236}.btn-warning .badge{color:#f0ad4e;background-color:#fff}.btn-danger{color:#fff;background-color:#d9534f;border-color:#d43f3a}.btn-danger.active,.btn-danger.focus,.btn-danger:active,.btn-danger:focus,.btn-danger:hover,.open>.dropdown-toggle.btn-danger{color:#fff;background-color:#c9302c;border-color:#ac2925}.btn-danger.active,.btn-danger:active,.open>.dropdown-toggle.btn-danger{background-image:none}.btn-danger.disabled,.btn-danger.disabled.active,.btn-danger.disabled.focus,.btn-danger.disabled:active,.btn-danger.disabled:focus,.btn-danger.disabled:hover,.btn-danger[disabled],.btn-danger[disabled].active,.btn-danger[disabled].focus,.btn-danger[disabled]:active,.btn-danger[disabled]:focus,.btn-danger[disabled]:hover,fieldset[disabled] .btn-danger,fieldset[disabled] .btn-danger.active,fieldset[disabled] .btn-danger.focus,fieldset[disabled] .btn-danger:active,fieldset[disabled] .btn-danger:focus,fieldset[disabled] .btn-danger:hover{background-color:#d9534f;border-color:#d43f3a}.btn-danger .badge{color:#d9534f;background-color:#fff}.btn-link{font-weight:400;color:#337ab7;border-radius:0}.btn-link,.btn-link.active,.btn-link:active,.btn-link[disabled],fieldset[disabled] .btn-link{background-color:transparent;-webkit-box-shadow:none;box-shadow:none}.btn-link,.btn-link:active,.btn-link:focus,.btn-link:hover{border-color:transparent}.btn-link:focus,.btn-link:hover{color:#23527c;text-decoration:underline;background-color:transparent}.btn-link[disabled]:focus,.btn-link[disabled]:hover,fieldset[disabled] .btn-link:focus,fieldset[disabled] .btn-link:hover{color:#777;text-decoration:none}.btn-group-lg>.btn,.btn-lg{padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}.btn-group-sm>.btn,.btn-sm{padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}.btn-group-xs>.btn,.btn-xs{padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px}.btn-block{display:block;width:100%}.btn-block+.btn-block{margin-top:5px}input[type=button].btn-block,input[type=reset].btn-block,input[type=submit].btn-block{width:100%}.fade{opacity:0;-webkit-transition:opacity .15s linear;-o-transition:opacity .15s linear;transition:opacity .15s linear}.fade.in{opacity:1}.collapse{display:none}.collapse.in{display:block}tr.collapse.in{display:table-row}tbody.collapse.in{display:table-row-group}.collapsing{position:relative;height:0;overflow:hidden;-webkit-transition-timing-function:ease;-o-transition-timing-function:ease;transition-timing-function:ease;-webkit-transition-duration:.35s;-o-transition-duration:.35s;transition-duration:.35s;-webkit-transition-property:height,visibility;-o-transition-property:height,visibility;transition-property:height,visibility}.caret{display:inline-block;width:0;height:0;margin-left:2px;vertical-align:middle;border-top:4px dashed;border-right:4px solid transparent;border-left:4px solid transparent}.dropdown,.dropup{position:relative}.dropdown-toggle:focus{outline:0}.dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;display:none;float:left;min-width:160px;padding:5px 0;margin:2px 0 0;font-size:14px;text-align:left;list-style:none;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.15);border-radius:4px;-webkit-box-shadow:0 6px 12px rgba(0,0,0,.175);box-shadow:0 6px 12px rgba(0,0,0,.175)}.dropdown-menu.pull-right{right:0;left:auto}.dropdown-menu .divider{height:1px;margin:9px 0;overflow:hidden;background-color:#e5e5e5}.dropdown-menu>li>a{display:block;padding:3px 20px;clear:both;font-weight:400;line-height:1.42857143;color:#333;white-space:nowrap}.dropdown-menu>li>a:focus,.dropdown-menu>li>a:hover{color:#262626;text-decoration:none;background-color:#f5f5f5}.dropdown-menu>.active>a,.dropdown-menu>.active>a:focus,.dropdown-menu>.active>a:hover{color:#fff;text-decoration:none;background-color:#337ab7;outline:0}.dropdown-menu>.disabled>a,.dropdown-menu>.disabled>a:focus,.dropdown-menu>.disabled>a:hover{color:#777}.dropdown-menu>.disabled>a:focus,.dropdown-menu>.disabled>a:hover{text-decoration:none;cursor:not-allowed;background-color:transparent;background-image:none;filter:progid:DXImageTransform.Microsoft.gradient(enabled=false)}.open>.dropdown-menu{display:block}.open>a{outline:0}.dropdown-menu-right{right:0;left:auto}.dropdown-menu-left{right:auto;left:0}.dropdown-header{display:block;padding:3px 20px;font-size:12px;line-height:1.42857143;color:#777;white-space:nowrap}.dropdown-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;z-index:990}.pull-right>.dropdown-menu{right:0;left:auto}.dropup .caret,.navbar-fixed-bottom .dropdown .caret{content:"";border-top:0;border-bottom:4px solid}.dropup .dropdown-menu,.navbar-fixed-bottom .dropdown .dropdown-menu{top:auto;bottom:100%;margin-bottom:2px}@media (min-width:768px){.navbar-right .dropdown-menu{right:0;left:auto}.navbar-right .dropdown-menu-left{right:auto;left:0}}.btn-group,.btn-group-vertical{position:relative;display:inline-block;vertical-align:middle}.btn-group-vertical>.btn,.btn-group>.btn{position:relative;float:left}.btn-group-vertical>.btn.active,.btn-group-vertical>.btn:active,.btn-group-vertical>.btn:focus,.btn-group-vertical>.btn:hover,.btn-group>.btn.active,.btn-group>.btn:active,.btn-group>.btn:focus,.btn-group>.btn:hover{z-index:2}.btn-group .btn+.btn,.btn-group .btn+.btn-group,.btn-group .btn-group+.btn,.btn-group .btn-group+.btn-group{margin-left:-1px}.btn-toolbar{margin-left:-5px}.btn-toolbar .btn-group,.btn-toolbar .input-group{float:left}.btn-toolbar>.btn,.btn-toolbar>.btn-group,.btn-toolbar>.input-group{margin-left:5px}.btn-group>.btn:not(:first-child):not(:last-child):not(.dropdown-toggle){border-radius:0}.btn-group>.btn:first-child{margin-left:0}.btn-group>.btn:first-child:not(:last-child):not(.dropdown-toggle){border-top-right-radius:0;border-bottom-right-radius:0}.btn-group>.btn:last-child:not(:first-child),.btn-group>.dropdown-toggle:not(:first-child){border-top-left-radius:0;border-bottom-left-radius:0}.btn-group>.btn-group{float:left}.btn-group>.btn-group:not(:first-child):not(:last-child)>.btn{border-radius:0}.btn-group>.btn-group:first-child:not(:last-child)>.btn:last-child,.btn-group>.btn-group:first-child:not(:last-child)>.dropdown-toggle{border-top-right-radius:0;border-bottom-right-radius:0}.btn-group>.btn-group:last-child:not(:first-child)>.btn:first-child{border-top-left-radius:0;border-bottom-left-radius:0}.btn-group .dropdown-toggle:active,.btn-group.open .dropdown-toggle{outline:0}.btn-group>.btn+.dropdown-toggle{padding-right:8px;padding-left:8px}.btn-group>.btn-lg+.dropdown-toggle{padding-right:12px;padding-left:12px}.btn-group.open .dropdown-toggle{-webkit-box-shadow:inset 0 3px 5px rgba(0,0,0,.125);box-shadow:inset 0 3px 5px rgba(0,0,0,.125)}.btn-group.open .dropdown-toggle.btn-link{-webkit-box-shadow:none;box-shadow:none}.btn .caret{margin-left:0}.btn-lg .caret{border-width:5px 5px 0;border-bottom-width:0}.dropup .btn-lg .caret{border-width:0 5px 5px}.btn-group-vertical>.btn,.btn-group-vertical>.btn-group,.btn-group-vertical>.btn-group>.btn{display:block;float:none;width:100%;max-width:100%}.btn-group-vertical>.btn-group>.btn{float:none}.btn-group-vertical>.btn+.btn,.btn-group-vertical>.btn+.btn-group,.btn-group-vertical>.btn-group+.btn,.btn-group-vertical>.btn-group+.btn-group{margin-top:-1px;margin-left:0}.btn-group-vertical>.btn:not(:first-child):not(:last-child){border-radius:0}.btn-group-vertical>.btn:first-child:not(:last-child){border-top-right-radius:4px;border-bottom-right-radius:0;border-bottom-left-radius:0}.btn-group-vertical>.btn:last-child:not(:first-child){border-top-left-radius:0;border-top-right-radius:0;border-bottom-left-radius:4px}.btn-group-vertical>.btn-group:not(:first-child):not(:last-child)>.btn{border-radius:0}.btn-group-vertical>.btn-group:first-child:not(:last-child)>.btn:last-child,.btn-group-vertical>.btn-group:first-child:not(:last-child)>.dropdown-toggle{border-bottom-right-radius:0;border-bottom-left-radius:0}.btn-group-vertical>.btn-group:last-child:not(:first-child)>.btn:first-child{border-top-left-radius:0;border-top-right-radius:0}.btn-group-justified{display:table;width:100%;table-layout:fixed;border-collapse:separate}.btn-group-justified>.btn,.btn-group-justified>.btn-group{display:table-cell;float:none;width:1%}.btn-group-justified>.btn-group .btn{width:100%}.btn-group-justified>.btn-group .dropdown-menu{left:auto}[data-toggle=buttons]>.btn input[type=checkbox],[data-toggle=buttons]>.btn input[type=radio],[data-toggle=buttons]>.btn-group>.btn input[type=checkbox],[data-toggle=buttons]>.btn-group>.btn input[type=radio]{position:absolute;clip:rect(0,0,0,0);pointer-events:none}.input-group{position:relative;display:table;border-collapse:separate}.input-group[class*=col-]{float:none;padding-right:0;padding-left:0}.input-group .form-control{position:relative;z-index:2;float:left;width:100%;margin-bottom:0}.input-group-lg>.form-control,.input-group-lg>.input-group-addon,.input-group-lg>.input-group-btn>.btn{height:46px;padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}select.input-group-lg>.form-control,select.input-group-lg>.input-group-addon,select.input-group-lg>.input-group-btn>.btn{height:46px;line-height:46px}select[multiple].input-group-lg>.form-control,select[multiple].input-group-lg>.input-group-addon,select[multiple].input-group-lg>.input-group-btn>.btn,textarea.input-group-lg>.form-control,textarea.input-group-lg>.input-group-addon,textarea.input-group-lg>.input-group-btn>.btn{height:auto}.input-group-sm>.form-control,.input-group-sm>.input-group-addon,.input-group-sm>.input-group-btn>.btn{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.input-group-sm>.form-control,select.input-group-sm>.input-group-addon,select.input-group-sm>.input-group-btn>.btn{height:30px;line-height:30px}select[multiple].input-group-sm>.form-control,select[multiple].input-group-sm>.input-group-addon,select[multiple].input-group-sm>.input-group-btn>.btn,textarea.input-group-sm>.form-control,textarea.input-group-sm>.input-group-addon,textarea.input-group-sm>.input-group-btn>.btn{height:auto}.input-group .form-control,.input-group-addon,.input-group-btn{display:table-cell}.input-group .form-control:not(:first-child):not(:last-child),.input-group-addon:not(:first-child):not(:last-child),.input-group-btn:not(:first-child):not(:last-child){border-radius:0}.input-group-addon,.input-group-btn{width:1%;white-space:nowrap;vertical-align:middle}.input-group-addon{padding:6px 12px;font-size:14px;font-weight:400;line-height:1;color:#555;text-align:center;background-color:#eee;border:1px solid #ccc;border-radius:4px}.input-group-addon.input-sm{padding:5px 10px;font-size:12px;border-radius:3px}.input-group-addon.input-lg{padding:10px 16px;font-size:18px;border-radius:6px}.input-group-addon input[type=checkbox],.input-group-addon input[type=radio]{margin-top:0}.input-group .form-control:first-child,.input-group-addon:first-child,.input-group-btn:first-child>.btn,.input-group-btn:first-child>.btn-group>.btn,.input-group-btn:first-child>.dropdown-toggle,.input-group-btn:last-child>.btn-group:not(:last-child)>.btn,.input-group-btn:last-child>.btn:not(:last-child):not(.dropdown-toggle){border-top-right-radius:0;border-bottom-right-radius:0}.input-group-addon:first-child{border-right:0}.input-group .form-control:last-child,.input-group-addon:last-child,.input-group-btn:first-child>.btn-group:not(:first-child)>.btn,.input-group-btn:first-child>.btn:not(:first-child),.input-group-btn:last-child>.btn,.input-group-btn:last-child>.btn-group>.btn,.input-group-btn:last-child>.dropdown-toggle{border-top-left-radius:0;border-bottom-left-radius:0}.input-group-addon:last-child{border-left:0}.input-group-btn{position:relative;font-size:0;white-space:nowrap}.input-group-btn>.btn{position:relative}.input-group-btn>.btn+.btn{margin-left:-1px}.input-group-btn>.btn:active,.input-group-btn>.btn:focus,.input-group-btn>.btn:hover{z-index:2}.input-group-btn:first-child>.btn,.input-group-btn:first-child>.btn-group{margin-right:-1px}.input-group-btn:last-child>.btn,.input-group-btn:last-child>.btn-group{margin-left:-1px}.nav{padding-left:0;margin-bottom:0;list-style:none}.nav>li{position:relative;display:block}.nav>li>a{position:relative;display:block;padding:10px 15px}.nav>li>a:focus,.nav>li>a:hover{text-decoration:none;background-color:#eee}.nav>li.disabled>a{color:#777}.nav>li.disabled>a:focus,.nav>li.disabled>a:hover{color:#777;text-decoration:none;cursor:not-allowed;background-color:transparent}.nav .open>a,.nav .open>a:focus,.nav .open>a:hover{background-color:#eee;border-color:#337ab7}.nav .nav-divider{height:1px;margin:9px 0;overflow:hidden;background-color:#e5e5e5}.nav>li>a>img{max-width:none}.nav-tabs{border-bottom:1px solid #ddd}.nav-tabs>li{float:left;margin-bottom:-1px}.nav-tabs>li>a{margin-right:2px;line-height:1.42857143;border:1px solid transparent;border-radius:4px 4px 0 0}.nav-tabs>li>a:hover{border-color:#eee #eee #ddd}.nav-tabs>li.active>a,.nav-tabs>li.active>a:focus,.nav-tabs>li.active>a:hover{color:#555;cursor:default;background-color:#fff;border:1px solid #ddd;border-bottom-color:transparent}.nav-tabs.nav-justified{width:100%;border-bottom:0}.nav-tabs.nav-justified>li{float:none}.nav-tabs.nav-justified>li>a{margin-bottom:5px;text-align:center}.nav-tabs.nav-justified>.dropdown .dropdown-menu{top:auto;left:auto}@media (min-width:768px){.nav-tabs.nav-justified>li{display:table-cell;width:1%}.nav-tabs.nav-justified>li>a{margin-bottom:0}}.nav-tabs.nav-justified>li>a{margin-right:0;border-radius:4px}.nav-tabs.nav-justified>.active>a,.nav-tabs.nav-justified>.active>a:focus,.nav-tabs.nav-justified>.active>a:hover{border:1px solid #ddd}@media (min-width:768px){.nav-tabs.nav-justified>li>a{border-bottom:1px solid #ddd;border-radius:4px 4px 0 0}.nav-tabs.nav-justified>.active>a,.nav-tabs.nav-justified>.active>a:focus,.nav-tabs.nav-justified>.active>a:hover{border-bottom-color:#fff}}.nav-pills>li{float:left}.nav-pills>li>a{border-radius:4px}.nav-pills>li+li{margin-left:2px}.nav-pills>li.active>a,.nav-pills>li.active>a:focus,.nav-pills>li.active>a:hover{color:#fff;background-color:#337ab7}.nav-stacked>li{float:none}.nav-stacked>li+li{margin-top:2px;margin-left:0}.nav-justified{width:100%}.nav-justified>li{float:none}.nav-justified>li>a{margin-bottom:5px;text-align:center}.nav-justified>.dropdown .dropdown-menu{top:auto;left:auto}@media (min-width:768px){.nav-justified>li{display:table-cell;width:1%}.nav-justified>li>a{margin-bottom:0}}.nav-tabs-justified{border-bottom:0}.nav-tabs-justified>li>a{margin-right:0;border-radius:4px}.nav-tabs-justified>.active>a,.nav-tabs-justified>.active>a:focus,.nav-tabs-justified>.active>a:hover{border:1px solid #ddd}@media (min-width:768px){.nav-tabs-justified>li>a{border-bottom:1px solid #ddd;border-radius:4px 4px 0 0}.nav-tabs-justified>.active>a,.nav-tabs-justified>.active>a:focus,.nav-tabs-justified>.active>a:hover{border-bottom-color:#fff}}.tab-content>.tab-pane{display:none}.tab-content>.active{display:block}.nav-tabs .dropdown-menu{margin-top:-1px;border-top-left-radius:0;border-top-right-radius:0}.navbar{position:relative;min-height:50px;margin-bottom:20px;border:1px solid transparent}@media (min-width:768px){.navbar{border-radius:4px}}@media (min-width:768px){.navbar-header{float:left}}.navbar-collapse{padding-right:15px;padding-left:15px;overflow-x:visible;-webkit-overflow-scrolling:touch;border-top:1px solid transparent;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.1);box-shadow:inset 0 1px 0 rgba(255,255,255,.1)}.navbar-collapse.in{overflow-y:auto}@media (min-width:768px){.navbar-collapse{width:auto;border-top:0;-webkit-box-shadow:none;box-shadow:none}.navbar-collapse.collapse{display:block!important;height:auto!important;padding-bottom:0;overflow:visible!important}.navbar-collapse.in{overflow-y:visible}.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse,.navbar-static-top .navbar-collapse{padding-right:0;padding-left:0}}.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse{max-height:340px}@media (max-device-width:480px)and (orientation:landscape){.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse{max-height:200px}}.container-fluid>.navbar-collapse,.container-fluid>.navbar-header,.container>.navbar-collapse,.container>.navbar-header{margin-right:-15px;margin-left:-15px}@media (min-width:768px){.container-fluid>.navbar-collapse,.container-fluid>.navbar-header,.container>.navbar-collapse,.container>.navbar-header{margin-right:0;margin-left:0}}.navbar-static-top{z-index:1000;border-width:0 0 1px}@media (min-width:768px){.navbar-static-top{border-radius:0}}.navbar-fixed-bottom,.navbar-fixed-top{position:fixed;right:0;left:0;z-index:1030}@media (min-width:768px){.navbar-fixed-bottom,.navbar-fixed-top{border-radius:0}}.navbar-fixed-top{top:0;border-width:0 0 1px}.navbar-fixed-bottom{bottom:0;margin-bottom:0;border-width:1px 0 0}.navbar-brand{float:left;height:50px;padding:15px 15px;font-size:18px;line-height:20px}.navbar-brand:focus,.navbar-brand:hover{text-decoration:none}.navbar-brand>img{display:block}@media (min-width:768px){.navbar>.container .navbar-brand,.navbar>.container-fluid .navbar-brand{margin-left:-15px}}.navbar-toggle{position:relative;float:right;padding:9px 10px;margin-top:8px;margin-right:15px;margin-bottom:8px;background-color:transparent;background-image:none;border:1px solid transparent;border-radius:4px}.navbar-toggle:focus{outline:0}.navbar-toggle .icon-bar{display:block;width:22px;height:2px;border-radius:1px}.navbar-toggle .icon-bar+.icon-bar{margin-top:4px}@media (min-width:768px){.navbar-toggle{display:none}}.navbar-nav{margin:7.5px -15px}.navbar-nav>li>a{padding-top:10px;padding-bottom:10px;line-height:20px}@media (max-width:767px){.navbar-nav .open .dropdown-menu{position:static;float:none;width:auto;margin-top:0;background-color:transparent;border:0;-webkit-box-shadow:none;box-shadow:none}.navbar-nav .open .dropdown-menu .dropdown-header,.navbar-nav .open .dropdown-menu>li>a{padding:5px 15px 5px 25px}.navbar-nav .open .dropdown-menu>li>a{line-height:20px}.navbar-nav .open .dropdown-menu>li>a:focus,.navbar-nav .open .dropdown-menu>li>a:hover{background-image:none}}@media (min-width:768px){.navbar-nav{float:left;margin:0}.navbar-nav>li{float:left}.navbar-nav>li>a{padding-top:15px;padding-bottom:15px}}.navbar-form{padding:10px 15px;margin-top:8px;margin-right:-15px;margin-bottom:8px;margin-left:-15px;border-top:1px solid transparent;border-bottom:1px solid transparent;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1);box-shadow:inset 0 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1)}@media (min-width:768px){.navbar-form .form-group{display:inline-block;margin-bottom:0;vertical-align:middle}.navbar-form .form-control{display:inline-block;width:auto;vertical-align:middle}.navbar-form .form-control-static{display:inline-block}.navbar-form .input-group{display:inline-table;vertical-align:middle}.navbar-form .input-group .form-control,.navbar-form .input-group .input-group-addon,.navbar-form .input-group .input-group-btn{width:auto}.navbar-form .input-group>.form-control{width:100%}.navbar-form .control-label{margin-bottom:0;vertical-align:middle}.navbar-form .checkbox,.navbar-form .radio{display:inline-block;margin-top:0;margin-bottom:0;vertical-align:middle}.navbar-form .checkbox label,.navbar-form .radio label{padding-left:0}.navbar-form .checkbox input[type=checkbox],.navbar-form .radio input[type=radio]{position:relative;margin-left:0}.navbar-form .has-feedback .form-control-feedback{top:0}}@media (max-width:767px){.navbar-form .form-group{margin-bottom:5px}.navbar-form .form-group:last-child{margin-bottom:0}}@media (min-width:768px){.navbar-form{width:auto;padding-top:0;padding-bottom:0;margin-right:0;margin-left:0;border:0;-webkit-box-shadow:none;box-shadow:none}}.navbar-nav>li>.dropdown-menu{margin-top:0;border-top-left-radius:0;border-top-right-radius:0}.navbar-fixed-bottom .navbar-nav>li>.dropdown-menu{margin-bottom:0;border-top-left-radius:4px;border-top-right-radius:4px;border-bottom-right-radius:0;border-bottom-left-radius:0}.navbar-btn{margin-top:8px;margin-bottom:8px}.navbar-btn.btn-sm{margin-top:10px;margin-bottom:10px}.navbar-btn.btn-xs{margin-top:14px;margin-bottom:14px}.navbar-text{margin-top:15px;margin-bottom:15px}@media (min-width:768px){.navbar-text{float:left;margin-right:15px;margin-left:15px}}@media (min-width:768px){.navbar-left{float:left!important}.navbar-right{float:right!important;margin-right:-15px}.navbar-right~.navbar-right{margin-right:0}}.navbar-default{background-color:#f8f8f8;border-color:#e7e7e7}.navbar-default .navbar-brand{color:#777}.navbar-default .navbar-brand:focus,.navbar-default .navbar-brand:hover{color:#5e5e5e;background-color:transparent}.navbar-default .navbar-text{color:#777}.navbar-default .navbar-nav>li>a{color:#777}.navbar-default .navbar-nav>li>a:focus,.navbar-default .navbar-nav>li>a:hover{color:#333;background-color:transparent}.navbar-default .navbar-nav>.active>a,.navbar-default .navbar-nav>.active>a:focus,.navbar-default .navbar-nav>.active>a:hover{color:#555;background-color:#e7e7e7}.navbar-default .navbar-nav>.disabled>a,.navbar-default .navbar-nav>.disabled>a:focus,.navbar-default .navbar-nav>.disabled>a:hover{color:#ccc;background-color:transparent}.navbar-default .navbar-toggle{border-color:#ddd}.navbar-default .navbar-toggle:focus,.navbar-default .navbar-toggle:hover{background-color:#ddd}.navbar-default .navbar-toggle .icon-bar{background-color:#888}.navbar-default .navbar-collapse,.navbar-default .navbar-form{border-color:#e7e7e7}.navbar-default .navbar-nav>.open>a,.navbar-default .navbar-nav>.open>a:focus,.navbar-default .navbar-nav>.open>a:hover{color:#555;background-color:#e7e7e7}@media (max-width:767px){.navbar-default .navbar-nav .open .dropdown-menu>li>a{color:#777}.navbar-default .navbar-nav .open .dropdown-menu>li>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>li>a:hover{color:#333;background-color:transparent}.navbar-default .navbar-nav .open .dropdown-menu>.active>a,.navbar-default .navbar-nav .open .dropdown-menu>.active>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>.active>a:hover{color:#555;background-color:#e7e7e7}.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a,.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a:hover{color:#ccc;background-color:transparent}}.navbar-default .navbar-link{color:#777}.navbar-default .navbar-link:hover{color:#333}.navbar-default .btn-link{color:#777}.navbar-default .btn-link:focus,.navbar-default .btn-link:hover{color:#333}.navbar-default .btn-link[disabled]:focus,.navbar-default .btn-link[disabled]:hover,fieldset[disabled] .navbar-default .btn-link:focus,fieldset[disabled] .navbar-default .btn-link:hover{color:#ccc}.navbar-inverse{background-color:#222;border-color:#080808}.navbar-inverse .navbar-brand{color:#9d9d9d}.navbar-inverse .navbar-brand:focus,.navbar-inverse .navbar-brand:hover{color:#fff;background-color:transparent}.navbar-inverse .navbar-text{color:#9d9d9d}.navbar-inverse .navbar-nav>li>a{color:#9d9d9d}.navbar-inverse .navbar-nav>li>a:focus,.navbar-inverse .navbar-nav>li>a:hover{color:#fff;background-color:transparent}.navbar-inverse .navbar-nav>.active>a,.navbar-inverse .navbar-nav>.active>a:focus,.navbar-inverse .navbar-nav>.active>a:hover{color:#fff;background-color:#080808}.navbar-inverse .navbar-nav>.disabled>a,.navbar-inverse .navbar-nav>.disabled>a:focus,.navbar-inverse .navbar-nav>.disabled>a:hover{color:#444;background-color:transparent}.navbar-inverse .navbar-toggle{border-color:#333}.navbar-inverse .navbar-toggle:focus,.navbar-inverse .navbar-toggle:hover{background-color:#333}.navbar-inverse .navbar-toggle .icon-bar{background-color:#fff}.navbar-inverse .navbar-collapse,.navbar-inverse .navbar-form{border-color:#101010}.navbar-inverse .navbar-nav>.open>a,.navbar-inverse .navbar-nav>.open>a:focus,.navbar-inverse .navbar-nav>.open>a:hover{color:#fff;background-color:#080808}@media (max-width:767px){.navbar-inverse .navbar-nav .open .dropdown-menu>.dropdown-header{border-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu .divider{background-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu>li>a{color:#9d9d9d}.navbar-inverse .navbar-nav .open .dropdown-menu>li>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>li>a:hover{color:#fff;background-color:transparent}.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a:hover{color:#fff;background-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a:hover{color:#444;background-color:transparent}}.navbar-inverse .navbar-link{color:#9d9d9d}.navbar-inverse .navbar-link:hover{color:#fff}.navbar-inverse .btn-link{color:#9d9d9d}.navbar-inverse .btn-link:focus,.navbar-inverse .btn-link:hover{color:#fff}.navbar-inverse .btn-link[disabled]:focus,.navbar-inverse .btn-link[disabled]:hover,fieldset[disabled] .navbar-inverse .btn-link:focus,fieldset[disabled] .navbar-inverse .btn-link:hover{color:#444}.breadcrumb{padding:8px 15px;margin-bottom:20px;list-style:none;background-color:#f5f5f5;border-radius:4px}.breadcrumb>li{display:inline-block}.breadcrumb>li+li:before{padding:0 5px;color:#ccc;content:"/\00a0"}.breadcrumb>.active{color:#777}.pagination{display:inline-block;padding-left:0;margin:20px 0;border-radius:4px}.pagination>li{display:inline}.pagination>li>a,.pagination>li>span{position:relative;float:left;padding:6px 12px;margin-left:-1px;line-height:1.42857143;color:#337ab7;text-decoration:none;background-color:#fff;border:1px solid #ddd}.pagination>li:first-child>a,.pagination>li:first-child>span{margin-left:0;border-top-left-radius:4px;border-bottom-left-radius:4px}.pagination>li:last-child>a,.pagination>li:last-child>span{border-top-right-radius:4px;border-bottom-right-radius:4px}.pagination>li>a:focus,.pagination>li>a:hover,.pagination>li>span:focus,.pagination>li>span:hover{color:#23527c;background-color:#eee;border-color:#ddd}.pagination>.active>a,.pagination>.active>a:focus,.pagination>.active>a:hover,.pagination>.active>span,.pagination>.active>span:focus,.pagination>.active>span:hover{z-index:2;color:#fff;cursor:default;background-color:#337ab7;border-color:#337ab7}.pagination>.disabled>a,.pagination>.disabled>a:focus,.pagination>.disabled>a:hover,.pagination>.disabled>span,.pagination>.disabled>span:focus,.pagination>.disabled>span:hover{color:#777;cursor:not-allowed;background-color:#fff;border-color:#ddd}.pagination-lg>li>a,.pagination-lg>li>span{padding:10px 16px;font-size:18px}.pagination-lg>li:first-child>a,.pagination-lg>li:first-child>span{border-top-left-radius:6px;border-bottom-left-radius:6px}.pagination-lg>li:last-child>a,.pagination-lg>li:last-child>span{border-top-right-radius:6px;border-bottom-right-radius:6px}.pagination-sm>li>a,.pagination-sm>li>span{padding:5px 10px;font-size:12px}.pagination-sm>li:first-child>a,.pagination-sm>li:first-child>span{border-top-left-radius:3px;border-bottom-left-radius:3px}.pagination-sm>li:last-child>a,.pagination-sm>li:last-child>span{border-top-right-radius:3px;border-bottom-right-radius:3px}.pager{padding-left:0;margin:20px 0;text-align:center;list-style:none}.pager li{display:inline}.pager li>a,.pager li>span{display:inline-block;padding:5px 14px;background-color:#fff;border:1px solid #ddd;border-radius:15px}.pager li>a:focus,.pager li>a:hover{text-decoration:none;background-color:#eee}.pager .next>a,.pager .next>span{float:right}.pager .previous>a,.pager .previous>span{float:left}.pager .disabled>a,.pager .disabled>a:focus,.pager .disabled>a:hover,.pager .disabled>span{color:#777;cursor:not-allowed;background-color:#fff}.label{display:inline;padding:.2em .6em .3em;font-size:75%;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;border-radius:.25em}a.label:focus,a.label:hover{color:#fff;text-decoration:none;cursor:pointer}.label:empty{display:none}.btn .label{position:relative;top:-1px}.label-default{background-color:#777}.label-default[href]:focus,.label-default[href]:hover{background-color:#5e5e5e}.label-primary{background-color:#337ab7}.label-primary[href]:focus,.label-primary[href]:hover{background-color:#286090}.label-success{background-color:#5cb85c}.label-success[href]:focus,.label-success[href]:hover{background-color:#449d44}.label-info{background-color:#5bc0de}.label-info[href]:focus,.label-info[href]:hover{background-color:#31b0d5}.label-warning{background-color:#f0ad4e}.label-warning[href]:focus,.label-warning[href]:hover{background-color:#ec971f}.label-danger{background-color:#d9534f}.label-danger[href]:focus,.label-danger[href]:hover{background-color:#c9302c}.badge{display:inline-block;min-width:10px;padding:3px 7px;font-size:12px;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;background-color:#777;border-radius:10px}.badge:empty{display:none}.btn .badge{position:relative;top:-1px}.btn-group-xs>.btn .badge,.btn-xs .badge{top:0;padding:1px 5px}a.badge:focus,a.badge:hover{color:#fff;text-decoration:none;cursor:pointer}.list-group-item.active>.badge,.nav-pills>.active>a>.badge{color:#337ab7;background-color:#fff}.list-group-item>.badge{float:right}.list-group-item>.badge+.badge{margin-right:5px}.nav-pills>li>a>.badge{margin-left:3px}.jumbotron{padding:30px 15px;margin-bottom:30px;color:inherit;background-color:#eee}.jumbotron .h1,.jumbotron h1{color:inherit}.jumbotron p{margin-bottom:15px;font-size:21px;font-weight:200}.jumbotron>hr{border-top-color:#d5d5d5}.container .jumbotron,.container-fluid .jumbotron{border-radius:6px}.jumbotron .container{max-width:100%}@media screen and (min-width:768px){.jumbotron{padding:48px 0}.container .jumbotron,.container-fluid .jumbotron{padding-right:60px;padding-left:60px}.jumbotron .h1,.jumbotron h1{font-size:63px}}.thumbnail{display:block;padding:4px;margin-bottom:20px;line-height:1.42857143;background-color:#fff;border:1px solid #ddd;border-radius:4px;-webkit-transition:border .2s ease-in-out;-o-transition:border .2s ease-in-out;transition:border .2s ease-in-out}.thumbnail a>img,.thumbnail>img{margin-right:auto;margin-left:auto}a.thumbnail.active,a.thumbnail:focus,a.thumbnail:hover{border-color:#337ab7}.thumbnail .caption{padding:9px;color:#333}.alert{padding:15px;margin-bottom:20px;border:1px solid transparent;border-radius:4px}.alert h4{margin-top:0;color:inherit}.alert .alert-link{font-weight:700}.alert>p,.alert>ul{margin-bottom:0}.alert>p+p{margin-top:5px}.alert-dismissable,.alert-dismissible{padding-right:35px}.alert-dismissable .close,.alert-dismissible .close{position:relative;top:-2px;right:-21px;color:inherit}.alert-success{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.alert-success hr{border-top-color:#c9e2b3}.alert-success .alert-link{color:#2b542c}.alert-info{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.alert-info hr{border-top-color:#a6e1ec}.alert-info .alert-link{color:#245269}.alert-warning{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.alert-warning hr{border-top-color:#f7e1b5}.alert-warning .alert-link{color:#66512c}.alert-danger{color:#a94442;background-color:#f2dede;border-color:#ebccd1}.alert-danger hr{border-top-color:#e4b9c0}.alert-danger .alert-link{color:#843534}@-webkit-keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}@-o-keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}@keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}.progress{height:20px;margin-bottom:20px;overflow:hidden;background-color:#f5f5f5;border-radius:4px;-webkit-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);box-shadow:inset 0 1px 2px rgba(0,0,0,.1)}.progress-bar{float:left;width:0;height:100%;font-size:12px;line-height:20px;color:#fff;text-align:center;background-color:#337ab7;-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,.15);box-shadow:inset 0 -1px 0 rgba(0,0,0,.15);-webkit-transition:width .6s ease;-o-transition:width .6s ease;transition:width .6s ease}.progress-bar-striped,.progress-striped .progress-bar{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);-webkit-background-size:40px 40px;background-size:40px 40px}.progress-bar.active,.progress.active .progress-bar{-webkit-animation:progress-bar-stripes 2s linear infinite;-o-animation:progress-bar-stripes 2s linear infinite;animation:progress-bar-stripes 2s linear infinite}.progress-bar-success{background-color:#5cb85c}.progress-striped .progress-bar-success{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-info{background-color:#5bc0de}.progress-striped .progress-bar-info{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-warning{background-color:#f0ad4e}.progress-striped .progress-bar-warning{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-danger{background-color:#d9534f}.progress-striped .progress-bar-danger{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.media{margin-top:15px}.media:first-child{margin-top:0}.media,.media-body{overflow:hidden;zoom:1}.media-body{width:10000px}.media-object{display:block}.media-right,.media>.pull-right{padding-left:10px}.media-left,.media>.pull-left{padding-right:10px}.media-body,.media-left,.media-right{display:table-cell;vertical-align:top}.media-middle{vertical-align:middle}.media-bottom{vertical-align:bottom}.media-heading{margin-top:0;margin-bottom:5px}.media-list{padding-left:0;list-style:none}.list-group{padding-left:0;margin-bottom:20px}.list-group-item{position:relative;display:block;padding:10px 15px;margin-bottom:-1px;background-color:#fff;border:1px solid #ddd}.list-group-item:first-child{border-top-left-radius:4px;border-top-right-radius:4px}.list-group-item:last-child{margin-bottom:0;border-bottom-right-radius:4px;border-bottom-left-radius:4px}a.list-group-item{color:#555}a.list-group-item .list-group-item-heading{color:#333}a.list-group-item:focus,a.list-group-item:hover{color:#555;text-decoration:none;background-color:#f5f5f5}.list-group-item.disabled,.list-group-item.disabled:focus,.list-group-item.disabled:hover{color:#777;cursor:not-allowed;background-color:#eee}.list-group-item.disabled .list-group-item-heading,.list-group-item.disabled:focus .list-group-item-heading,.list-group-item.disabled:hover .list-group-item-heading{color:inherit}.list-group-item.disabled .list-group-item-text,.list-group-item.disabled:focus .list-group-item-text,.list-group-item.disabled:hover .list-group-item-text{color:#777}.list-group-item.active,.list-group-item.active:focus,.list-group-item.active:hover{z-index:2;color:#fff;background-color:#337ab7;border-color:#337ab7}.list-group-item.active .list-group-item-heading,.list-group-item.active .list-group-item-heading>.small,.list-group-item.active .list-group-item-heading>small,.list-group-item.active:focus .list-group-item-heading,.list-group-item.active:focus .list-group-item-heading>.small,.list-group-item.active:focus .list-group-item-heading>small,.list-group-item.active:hover .list-group-item-heading,.list-group-item.active:hover .list-group-item-heading>.small,.list-group-item.active:hover .list-group-item-heading>small{color:inherit}.list-group-item.active .list-group-item-text,.list-group-item.active:focus .list-group-item-text,.list-group-item.active:hover .list-group-item-text{color:#c7ddef}.list-group-item-success{color:#3c763d;background-color:#dff0d8}a.list-group-item-success{color:#3c763d}a.list-group-item-success .list-group-item-heading{color:inherit}a.list-group-item-success:focus,a.list-group-item-success:hover{color:#3c763d;background-color:#d0e9c6}a.list-group-item-success.active,a.list-group-item-success.active:focus,a.list-group-item-success.active:hover{color:#fff;background-color:#3c763d;border-color:#3c763d}.list-group-item-info{color:#31708f;background-color:#d9edf7}a.list-group-item-info{color:#31708f}a.list-group-item-info .list-group-item-heading{color:inherit}a.list-group-item-info:focus,a.list-group-item-info:hover{color:#31708f;background-color:#c4e3f3}a.list-group-item-info.active,a.list-group-item-info.active:focus,a.list-group-item-info.active:hover{color:#fff;background-color:#31708f;border-color:#31708f}.list-group-item-warning{color:#8a6d3b;background-color:#fcf8e3}a.list-group-item-warning{color:#8a6d3b}a.list-group-item-warning .list-group-item-heading{color:inherit}a.list-group-item-warning:focus,a.list-group-item-warning:hover{color:#8a6d3b;background-color:#faf2cc}a.list-group-item-warning.active,a.list-group-item-warning.active:focus,a.list-group-item-warning.active:hover{color:#fff;background-color:#8a6d3b;border-color:#8a6d3b}.list-group-item-danger{color:#a94442;background-color:#f2dede}a.list-group-item-danger{color:#a94442}a.list-group-item-danger .list-group-item-heading{color:inherit}a.list-group-item-danger:focus,a.list-group-item-danger:hover{color:#a94442;background-color:#ebcccc}a.list-group-item-danger.active,a.list-group-item-danger.active:focus,a.list-group-item-danger.active:hover{color:#fff;background-color:#a94442;border-color:#a94442}.list-group-item-heading{margin-top:0;margin-bottom:5px}.list-group-item-text{margin-bottom:0;line-height:1.3}.panel{margin-bottom:20px;background-color:#fff;border:1px solid transparent;border-radius:4px;-webkit-box-shadow:0 1px 1px rgba(0,0,0,.05);box-shadow:0 1px 1px rgba(0,0,0,.05)}.panel-body{padding:15px}.panel-heading{padding:10px 15px;border-bottom:1px solid transparent;border-top-left-radius:3px;border-top-right-radius:3px}.panel-heading>.dropdown .dropdown-toggle{color:inherit}.panel-title{margin-top:0;margin-bottom:0;font-size:16px;color:inherit}.panel-title>.small,.panel-title>.small>a,.panel-title>a,.panel-title>small,.panel-title>small>a{color:inherit}.panel-footer{padding:10px 15px;background-color:#f5f5f5;border-top:1px solid #ddd;border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.list-group,.panel>.panel-collapse>.list-group{margin-bottom:0}.panel>.list-group .list-group-item,.panel>.panel-collapse>.list-group .list-group-item{border-width:1px 0;border-radius:0}.panel>.list-group:first-child .list-group-item:first-child,.panel>.panel-collapse>.list-group:first-child .list-group-item:first-child{border-top:0;border-top-left-radius:3px;border-top-right-radius:3px}.panel>.list-group:last-child .list-group-item:last-child,.panel>.panel-collapse>.list-group:last-child .list-group-item:last-child{border-bottom:0;border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel-heading+.list-group .list-group-item:first-child{border-top-width:0}.list-group+.panel-footer{border-top-width:0}.panel>.panel-collapse>.table,.panel>.table,.panel>.table-responsive>.table{margin-bottom:0}.panel>.panel-collapse>.table caption,.panel>.table caption,.panel>.table-responsive>.table caption{padding-right:15px;padding-left:15px}.panel>.table-responsive:first-child>.table:first-child,.panel>.table:first-child{border-top-left-radius:3px;border-top-right-radius:3px}.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child,.panel>.table:first-child>thead:first-child>tr:first-child{border-top-left-radius:3px;border-top-right-radius:3px}.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child td:first-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child th:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child td:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child th:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child td:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child th:first-child,.panel>.table:first-child>thead:first-child>tr:first-child td:first-child,.panel>.table:first-child>thead:first-child>tr:first-child th:first-child{border-top-left-radius:3px}.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child td:last-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child th:last-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child td:last-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child th:last-child,.panel>.table:first-child>tbody:first-child>tr:first-child td:last-child,.panel>.table:first-child>tbody:first-child>tr:first-child th:last-child,.panel>.table:first-child>thead:first-child>tr:first-child td:last-child,.panel>.table:first-child>thead:first-child>tr:first-child th:last-child{border-top-right-radius:3px}.panel>.table-responsive:last-child>.table:last-child,.panel>.table:last-child{border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child{border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child td:first-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child th:first-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child td:first-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child th:first-child,.panel>.table:last-child>tbody:last-child>tr:last-child td:first-child,.panel>.table:last-child>tbody:last-child>tr:last-child th:first-child,.panel>.table:last-child>tfoot:last-child>tr:last-child td:first-child,.panel>.table:last-child>tfoot:last-child>tr:last-child th:first-child{border-bottom-left-radius:3px}.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child td:last-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child th:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child td:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child th:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child td:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child th:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child td:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child th:last-child{border-bottom-right-radius:3px}.panel>.panel-body+.table,.panel>.panel-body+.table-responsive,.panel>.table+.panel-body,.panel>.table-responsive+.panel-body{border-top:1px solid #ddd}.panel>.table>tbody:first-child>tr:first-child td,.panel>.table>tbody:first-child>tr:first-child th{border-top:0}.panel>.table-bordered,.panel>.table-responsive>.table-bordered{border:0}.panel>.table-bordered>tbody>tr>td:first-child,.panel>.table-bordered>tbody>tr>th:first-child,.panel>.table-bordered>tfoot>tr>td:first-child,.panel>.table-bordered>tfoot>tr>th:first-child,.panel>.table-bordered>thead>tr>td:first-child,.panel>.table-bordered>thead>tr>th:first-child,.panel>.table-responsive>.table-bordered>tbody>tr>td:first-child,.panel>.table-responsive>.table-bordered>tbody>tr>th:first-child,.panel>.table-responsive>.table-bordered>tfoot>tr>td:first-child,.panel>.table-responsive>.table-bordered>tfoot>tr>th:first-child,.panel>.table-responsive>.table-bordered>thead>tr>td:first-child,.panel>.table-responsive>.table-bordered>thead>tr>th:first-child{border-left:0}.panel>.table-bordered>tbody>tr>td:last-child,.panel>.table-bordered>tbody>tr>th:last-child,.panel>.table-bordered>tfoot>tr>td:last-child,.panel>.table-bordered>tfoot>tr>th:last-child,.panel>.table-bordered>thead>tr>td:last-child,.panel>.table-bordered>thead>tr>th:last-child,.panel>.table-responsive>.table-bordered>tbody>tr>td:last-child,.panel>.table-responsive>.table-bordered>tbody>tr>th:last-child,.panel>.table-responsive>.table-bordered>tfoot>tr>td:last-child,.panel>.table-responsive>.table-bordered>tfoot>tr>th:last-child,.panel>.table-responsive>.table-bordered>thead>tr>td:last-child,.panel>.table-responsive>.table-bordered>thead>tr>th:last-child{border-right:0}.panel>.table-bordered>tbody>tr:first-child>td,.panel>.table-bordered>tbody>tr:first-child>th,.panel>.table-bordered>thead>tr:first-child>td,.panel>.table-bordered>thead>tr:first-child>th,.panel>.table-responsive>.table-bordered>tbody>tr:first-child>td,.panel>.table-responsive>.table-bordered>tbody>tr:first-child>th,.panel>.table-responsive>.table-bordered>thead>tr:first-child>td,.panel>.table-responsive>.table-bordered>thead>tr:first-child>th{border-bottom:0}.panel>.table-bordered>tbody>tr:last-child>td,.panel>.table-bordered>tbody>tr:last-child>th,.panel>.table-bordered>tfoot>tr:last-child>td,.panel>.table-bordered>tfoot>tr:last-child>th,.panel>.table-responsive>.table-bordered>tbody>tr:last-child>td,.panel>.table-responsive>.table-bordered>tbody>tr:last-child>th,.panel>.table-responsive>.table-bordered>tfoot>tr:last-child>td,.panel>.table-responsive>.table-bordered>tfoot>tr:last-child>th{border-bottom:0}.panel>.table-responsive{margin-bottom:0;border:0}.panel-group{margin-bottom:20px}.panel-group .panel{margin-bottom:0;border-radius:4px}.panel-group .panel+.panel{margin-top:5px}.panel-group .panel-heading{border-bottom:0}.panel-group .panel-heading+.panel-collapse>.list-group,.panel-group .panel-heading+.panel-collapse>.panel-body{border-top:1px solid #ddd}.panel-group .panel-footer{border-top:0}.panel-group .panel-footer+.panel-collapse .panel-body{border-bottom:1px solid #ddd}.panel-default{border-color:#ddd}.panel-default>.panel-heading{color:#333;background-color:#f5f5f5;border-color:#ddd}.panel-default>.panel-heading+.panel-collapse>.panel-body{border-top-color:#ddd}.panel-default>.panel-heading .badge{color:#f5f5f5;background-color:#333}.panel-default>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#ddd}.panel-primary{border-color:#337ab7}.panel-primary>.panel-heading{color:#fff;background-color:#337ab7;border-color:#337ab7}.panel-primary>.panel-heading+.panel-collapse>.panel-body{border-top-color:#337ab7}.panel-primary>.panel-heading .badge{color:#337ab7;background-color:#fff}.panel-primary>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#337ab7}.panel-success{border-color:#d6e9c6}.panel-success>.panel-heading{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.panel-success>.panel-heading+.panel-collapse>.panel-body{border-top-color:#d6e9c6}.panel-success>.panel-heading .badge{color:#dff0d8;background-color:#3c763d}.panel-success>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#d6e9c6}.panel-info{border-color:#bce8f1}.panel-info>.panel-heading{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.panel-info>.panel-heading+.panel-collapse>.panel-body{border-top-color:#bce8f1}.panel-info>.panel-heading .badge{color:#d9edf7;background-color:#31708f}.panel-info>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#bce8f1}.panel-warning{border-color:#faebcc}.panel-warning>.panel-heading{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.panel-warning>.panel-heading+.panel-collapse>.panel-body{border-top-color:#faebcc}.panel-warning>.panel-heading .badge{color:#fcf8e3;background-color:#8a6d3b}.panel-warning>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#faebcc}.panel-danger{border-color:#ebccd1}.panel-danger>.panel-heading{color:#a94442;background-color:#f2dede;border-color:#ebccd1}.panel-danger>.panel-heading+.panel-collapse>.panel-body{border-top-color:#ebccd1}.panel-danger>.panel-heading .badge{color:#f2dede;background-color:#a94442}.panel-danger>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#ebccd1}.embed-responsive{position:relative;display:block;height:0;padding:0;overflow:hidden}.embed-responsive .embed-responsive-item,.embed-responsive embed,.embed-responsive iframe,.embed-responsive object,.embed-responsive video{position:absolute;top:0;bottom:0;left:0;width:100%;height:100%;border:0}.embed-responsive-16by9{padding-bottom:56.25%}.embed-responsive-4by3{padding-bottom:75%}.well{min-height:20px;padding:19px;margin-bottom:20px;background-color:#f5f5f5;border:1px solid #e3e3e3;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.05);box-shadow:inset 0 1px 1px rgba(0,0,0,.05)}.well blockquote{border-color:#ddd;border-color:rgba(0,0,0,.15)}.well-lg{padding:24px;border-radius:6px}.well-sm{padding:9px;border-radius:3px}.close{float:right;font-size:21px;font-weight:700;line-height:1;color:#000;text-shadow:0 1px 0 #fff;filter:alpha(opacity=20);opacity:.2}.close:focus,.close:hover{color:#000;text-decoration:none;cursor:pointer;filter:alpha(opacity=50);opacity:.5}button.close{-webkit-appearance:none;padding:0;cursor:pointer;background:0 0;border:0}.modal-open{overflow:hidden}.modal{position:fixed;top:0;right:0;bottom:0;left:0;z-index:1050;display:none;overflow:hidden;-webkit-overflow-scrolling:touch;outline:0}.modal.fade .modal-dialog{-webkit-transition:-webkit-transform .3s ease-out;-o-transition:-o-transform .3s ease-out;transition:transform .3s ease-out;-webkit-transform:translate(0,-25%);-ms-transform:translate(0,-25%);-o-transform:translate(0,-25%);transform:translate(0,-25%)}.modal.in .modal-dialog{-webkit-transform:translate(0,0);-ms-transform:translate(0,0);-o-transform:translate(0,0);transform:translate(0,0)}.modal-open .modal{overflow-x:hidden;overflow-y:auto}.modal-dialog{position:relative;width:auto;margin:10px}.modal-content{position:relative;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #999;border:1px solid rgba(0,0,0,.2);border-radius:6px;outline:0;-webkit-box-shadow:0 3px 9px rgba(0,0,0,.5);box-shadow:0 3px 9px rgba(0,0,0,.5)}.modal-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;z-index:1040;background-color:#000}.modal-backdrop.fade{filter:alpha(opacity=0);opacity:0}.modal-backdrop.in{filter:alpha(opacity=50);opacity:.5}.modal-header{min-height:16.43px;padding:15px;border-bottom:1px solid #e5e5e5}.modal-header .close{margin-top:-2px}.modal-title{margin:0;line-height:1.42857143}.modal-body{position:relative;padding:15px}.modal-footer{padding:15px;text-align:right;border-top:1px solid #e5e5e5}.modal-footer .btn+.btn{margin-bottom:0;margin-left:5px}.modal-footer .btn-group .btn+.btn{margin-left:-1px}.modal-footer .btn-block+.btn-block{margin-left:0}.modal-scrollbar-measure{position:absolute;top:-9999px;width:50px;height:50px;overflow:scroll}@media (min-width:768px){.modal-dialog{width:600px;margin:30px auto}.modal-content{-webkit-box-shadow:0 5px 15px rgba(0,0,0,.5);box-shadow:0 5px 15px rgba(0,0,0,.5)}.modal-sm{width:300px}}@media (min-width:992px){.modal-lg{width:900px}}.tooltip{position:absolute;z-index:1070;display:block;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:12px;font-weight:400;line-height:1.4;filter:alpha(opacity=0);opacity:0}.tooltip.in{filter:alpha(opacity=90);opacity:.9}.tooltip.top{padding:5px 0;margin-top:-3px}.tooltip.right{padding:0 5px;margin-left:3px}.tooltip.bottom{padding:5px 0;margin-top:3px}.tooltip.left{padding:0 5px;margin-left:-3px}.tooltip-inner{max-width:200px;padding:3px 8px;color:#fff;text-align:center;text-decoration:none;background-color:#000;border-radius:4px}.tooltip-arrow{position:absolute;width:0;height:0;border-color:transparent;border-style:solid}.tooltip.top .tooltip-arrow{bottom:0;left:50%;margin-left:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.top-left .tooltip-arrow{right:5px;bottom:0;margin-bottom:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.top-right .tooltip-arrow{bottom:0;left:5px;margin-bottom:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.right .tooltip-arrow{top:50%;left:0;margin-top:-5px;border-width:5px 5px 5px 0;border-right-color:#000}.tooltip.left .tooltip-arrow{top:50%;right:0;margin-top:-5px;border-width:5px 0 5px 5px;border-left-color:#000}.tooltip.bottom .tooltip-arrow{top:0;left:50%;margin-left:-5px;border-width:0 5px 5px;border-bottom-color:#000}.tooltip.bottom-left .tooltip-arrow{top:0;right:5px;margin-top:-5px;border-width:0 5px 5px;border-bottom-color:#000}.tooltip.bottom-right .tooltip-arrow{top:0;left:5px;margin-top:-5px;border-width:0 5px 5px;border-bottom-color:#000}.popover{position:absolute;top:0;left:0;z-index:1060;display:none;max-width:276px;padding:1px;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;font-weight:400;line-height:1.42857143;text-align:left;white-space:normal;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.2);border-radius:6px;-webkit-box-shadow:0 5px 10px rgba(0,0,0,.2);box-shadow:0 5px 10px rgba(0,0,0,.2)}.popover.top{margin-top:-10px}.popover.right{margin-left:10px}.popover.bottom{margin-top:10px}.popover.left{margin-left:-10px}.popover-title{padding:8px 14px;margin:0;font-size:14px;background-color:#f7f7f7;border-bottom:1px solid #ebebeb;border-radius:5px 5px 0 0}.popover-content{padding:9px 14px}.popover>.arrow,.popover>.arrow:after{position:absolute;display:block;width:0;height:0;border-color:transparent;border-style:solid}.popover>.arrow{border-width:11px}.popover>.arrow:after{content:"";border-width:10px}.popover.top>.arrow{bottom:-11px;left:50%;margin-left:-11px;border-top-color:#999;border-top-color:rgba(0,0,0,.25);border-bottom-width:0}.popover.top>.arrow:after{bottom:1px;margin-left:-10px;content:" ";border-top-color:#fff;border-bottom-width:0}.popover.right>.arrow{top:50%;left:-11px;margin-top:-11px;border-right-color:#999;border-right-color:rgba(0,0,0,.25);border-left-width:0}.popover.right>.arrow:after{bottom:-10px;left:1px;content:" ";border-right-color:#fff;border-left-width:0}.popover.bottom>.arrow{top:-11px;left:50%;margin-left:-11px;border-top-width:0;border-bottom-color:#999;border-bottom-color:rgba(0,0,0,.25)}.popover.bottom>.arrow:after{top:1px;margin-left:-10px;content:" ";border-top-width:0;border-bottom-color:#fff}.popover.left>.arrow{top:50%;right:-11px;margin-top:-11px;border-right-width:0;border-left-color:#999;border-left-color:rgba(0,0,0,.25)}.popover.left>.arrow:after{right:1px;bottom:-10px;content:" ";border-right-width:0;border-left-color:#fff}.carousel{position:relative}.carousel-inner{position:relative;width:100%;overflow:hidden}.carousel-inner>.item{position:relative;display:none;-webkit-transition:.6s ease-in-out left;-o-transition:.6s ease-in-out left;transition:.6s ease-in-out left}.carousel-inner>.item>a>img,.carousel-inner>.item>img{line-height:1}@media all and (transform-3d),(-webkit-transform-3d){.carousel-inner>.item{-webkit-transition:-webkit-transform .6s ease-in-out;-o-transition:-o-transform .6s ease-in-out;transition:transform .6s ease-in-out;-webkit-backface-visibility:hidden;backface-visibility:hidden;-webkit-perspective:1000;perspective:1000}.carousel-inner>.item.active.right,.carousel-inner>.item.next{left:0;-webkit-transform:translate3d(100%,0,0);transform:translate3d(100%,0,0)}.carousel-inner>.item.active.left,.carousel-inner>.item.prev{left:0;-webkit-transform:translate3d(-100%,0,0);transform:translate3d(-100%,0,0)}.carousel-inner>.item.active,.carousel-inner>.item.next.left,.carousel-inner>.item.prev.right{left:0;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}}.carousel-inner>.active,.carousel-inner>.next,.carousel-inner>.prev{display:block}.carousel-inner>.active{left:0}.carousel-inner>.next,.carousel-inner>.prev{position:absolute;top:0;width:100%}.carousel-inner>.next{left:100%}.carousel-inner>.prev{left:-100%}.carousel-inner>.next.left,.carousel-inner>.prev.right{left:0}.carousel-inner>.active.left{left:-100%}.carousel-inner>.active.right{left:100%}.carousel-control{position:absolute;top:0;bottom:0;left:0;width:15%;font-size:20px;color:#fff;text-align:center;text-shadow:0 1px 2px rgba(0,0,0,.6);filter:alpha(opacity=50);opacity:.5}.carousel-control.left{background-image:-webkit-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);background-image:-o-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);background-image:-webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.5)),to(rgba(0,0,0,.0001)));background-image:linear-gradient(to right,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#00000000', GradientType=1);background-repeat:repeat-x}.carousel-control.right{right:0;left:auto;background-image:-webkit-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);background-image:-o-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);background-image:-webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.0001)),to(rgba(0,0,0,.5)));background-image:linear-gradient(to right,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#80000000', GradientType=1);background-repeat:repeat-x}.carousel-control:focus,.carousel-control:hover{color:#fff;text-decoration:none;filter:alpha(opacity=90);outline:0;opacity:.9}.carousel-control .glyphicon-chevron-left,.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next,.carousel-control .icon-prev{position:absolute;top:50%;z-index:5;display:inline-block}.carousel-control .glyphicon-chevron-left,.carousel-control .icon-prev{left:50%;margin-left:-10px}.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next{right:50%;margin-right:-10px}.carousel-control .icon-next,.carousel-control .icon-prev{width:20px;height:20px;margin-top:-10px;font-family:serif;line-height:1}.carousel-control .icon-prev:before{content:'\2039'}.carousel-control .icon-next:before{content:'\203a'}.carousel-indicators{position:absolute;bottom:10px;left:50%;z-index:15;width:60%;padding-left:0;margin-left:-30%;text-align:center;list-style:none}.carousel-indicators li{display:inline-block;width:10px;height:10px;margin:1px;text-indent:-999px;cursor:pointer;background-color:#000 \9;background-color:rgba(0,0,0,0);border:1px solid #fff;border-radius:10px}.carousel-indicators .active{width:12px;height:12px;margin:0;background-color:#fff}.carousel-caption{position:absolute;right:15%;bottom:20px;left:15%;z-index:10;padding-top:20px;padding-bottom:20px;color:#fff;text-align:center;text-shadow:0 1px 2px rgba(0,0,0,.6)}.carousel-caption .btn{text-shadow:none}@media screen and (min-width:768px){.carousel-control .glyphicon-chevron-left,.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next,.carousel-control .icon-prev{width:30px;height:30px;margin-top:-15px;font-size:30px}.carousel-control .glyphicon-chevron-left,.carousel-control .icon-prev{margin-left:-15px}.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next{margin-right:-15px}.carousel-caption{right:20%;left:20%;padding-bottom:30px}.carousel-indicators{bottom:20px}}.btn-group-vertical>.btn-group:after,.btn-group-vertical>.btn-group:before,.btn-toolbar:after,.btn-toolbar:before,.clearfix:after,.clearfix:before,.container-fluid:after,.container-fluid:before,.container:after,.container:before,.dl-horizontal dd:after,.dl-horizontal dd:before,.form-horizontal .form-group:after,.form-horizontal .form-group:before,.modal-footer:after,.modal-footer:before,.nav:after,.nav:before,.navbar-collapse:after,.navbar-collapse:before,.navbar-header:after,.navbar-header:before,.navbar:after,.navbar:before,.pager:after,.pager:before,.panel-body:after,.panel-body:before,.row:after,.row:before{display:table;content:" "}.btn-group-vertical>.btn-group:after,.btn-toolbar:after,.clearfix:after,.container-fluid:after,.container:after,.dl-horizontal dd:after,.form-horizontal .form-group:after,.modal-footer:after,.nav:after,.navbar-collapse:after,.navbar-header:after,.navbar:after,.pager:after,.panel-body:after,.row:after{clear:both}.center-block{display:block;margin-right:auto;margin-left:auto}.pull-right{float:right!important}.pull-left{float:left!important}.hide{display:none!important}.show{display:block!important}.invisible{visibility:hidden}.text-hide{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0}.hidden{display:none!important}.affix{position:fixed}@-ms-viewport{width:device-width}.visible-lg,.visible-md,.visible-sm,.visible-xs{display:none!important}.visible-lg-block,.visible-lg-inline,.visible-lg-inline-block,.visible-md-block,.visible-md-inline,.visible-md-inline-block,.visible-sm-block,.visible-sm-inline,.visible-sm-inline-block,.visible-xs-block,.visible-xs-inline,.visible-xs-inline-block{display:none!important}@media (max-width:767px){.visible-xs{display:block!important}table.visible-xs{display:table}tr.visible-xs{display:table-row!important}td.visible-xs,th.visible-xs{display:table-cell!important}}@media (max-width:767px){.visible-xs-block{display:block!important}}@media (max-width:767px){.visible-xs-inline{display:inline!important}}@media (max-width:767px){.visible-xs-inline-block{display:inline-block!important}}@media (min-width:768px)and (max-width:991px){.visible-sm{display:block!important}table.visible-sm{display:table}tr.visible-sm{display:table-row!important}td.visible-sm,th.visible-sm{display:table-cell!important}}@media (min-width:768px)and (max-width:991px){.visible-sm-block{display:block!important}}@media (min-width:768px)and (max-width:991px){.visible-sm-inline{display:inline!important}}@media (min-width:768px)and (max-width:991px){.visible-sm-inline-block{display:inline-block!important}}@media (min-width:992px)and (max-width:1199px){.visible-md{display:block!important}table.visible-md{display:table}tr.visible-md{display:table-row!important}td.visible-md,th.visible-md{display:table-cell!important}}@media (min-width:992px)and (max-width:1199px){.visible-md-block{display:block!important}}@media (min-width:992px)and (max-width:1199px){.visible-md-inline{display:inline!important}}@media (min-width:992px)and (max-width:1199px){.visible-md-inline-block{display:inline-block!important}}@media (min-width:1200px){.visible-lg{display:block!important}table.visible-lg{display:table}tr.visible-lg{display:table-row!important}td.visible-lg,th.visible-lg{display:table-cell!important}}@media (min-width:1200px){.visible-lg-block{display:block!important}}@media (min-width:1200px){.visible-lg-inline{display:inline!important}}@media (min-width:1200px){.visible-lg-inline-block{display:inline-block!important}}@media (max-width:767px){.hidden-xs{display:none!important}}@media (min-width:768px)and (max-width:991px){.hidden-sm{display:none!important}}@media (min-width:992px)and (max-width:1199px){.hidden-md{display:none!important}}@media (min-width:1200px){.hidden-lg{display:none!important}}.visible-print{display:none!important}@media print{.visible-print{display:block!important}table.visible-print{display:table}tr.visible-print{display:table-row!important}td.visible-print,th.visible-print{display:table-cell!important}}.visible-print-block{display:none!important}@media print{.visible-print-block{display:block!important}}.visible-print-inline{display:none!important}@media print{.visible-print-inline{display:inline!important}}.visible-print-inline-block{display:none!important}@media print{.visible-print-inline-block{display:inline-block!important}}@media print{.hidden-print{display:none!important}} \ No newline at end of file diff --git a/deprecated/iguana_kv.c b/deprecated/iguana_kv.c new file mode 100755 index 000000000..589a785c4 --- /dev/null +++ b/deprecated/iguana_kv.c @@ -0,0 +1,1015 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" +#include +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif + +int32_t conv_date(int32_t *secondsp,char *buf); + +uint32_t OS_conv_datenum(int32_t datenum,int32_t hour,int32_t minute,int32_t second) // datenum+H:M:S -> unix time +{ +#ifdef __PNACL + PostMessage("timegm is not implemented\n"); + return(0); +#else + struct tm t; + memset(&t,0,sizeof(t)); + t.tm_year = (datenum / 10000) - 1900, t.tm_mon = ((datenum / 100) % 100) - 1, t.tm_mday = (datenum % 100); + t.tm_hour = hour, t.tm_min = minute, t.tm_sec = second; + return((uint32_t)timegm(&t)); +#endif +} + +int32_t OS_conv_unixtime(int32_t *secondsp,time_t timestamp) // gmtime -> datenum + number of seconds +{ + struct tm t; int32_t datenum; uint32_t checktime; char buf[64]; + t = *gmtime(×tamp); + strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ",&t); //printf("%s\n",buf); + datenum = conv_date(secondsp,buf); + if ( (checktime= OS_conv_datenum(datenum,*secondsp/3600,(*secondsp%3600)/60,*secondsp%60)) != timestamp ) + { + printf("error: timestamp.%lu -> (%d + %d) -> %u\n",timestamp,datenum,*secondsp,checktime); + return(-1); + } + return(datenum); +} + +char *OS_mvstr() +{ +#ifdef __WIN32 + return("rename"); +#else + return("mv"); +#endif +} + +char *OS_rmstr() { return("rm"); } + +int32_t os_supports_mappedfiles() { return(1); } + +int32_t portable_truncate(char *fname,long filesize) { return(truncate(fname,filesize)); } + +char *iguana_compatible_path(char *str) +{ + return(str); +} + +void ensure_directory(char *dirname) +{ + FILE *fp; int32_t retval; char fname[512]; + iguana_removefile(dirname,0); + sprintf(fname,"%s/.tmpmarker",dirname); + if ( (fp= fopen(iguana_compatible_path(fname),"rb")) == 0 ) + { + if ( (fp= fopen(iguana_compatible_path(dirname),"rb")) == 0 ) + { + retval = mkdir(dirname,511); + printf("mkdir.(%s) retval.%d errno.%d %s\n",dirname,retval,errno,strerror(errno)); + } else fclose(fp), printf("dirname.(%s) exists\n",dirname); + if ( (fp= fopen(fname,"wb")) != 0 ) + fclose(fp), printf("created.(%s)\n",fname); + else printf("cant create.(%s) errno.%d %s\n",fname,errno,strerror(errno)); + } else fclose(fp), printf("%s exists\n",fname); +} + +int32_t iguana_renamefile(char *fname,char *newfname) +{ + char cmd[1024]; + sprintf(cmd,"%s %s %s",OS_mvstr(),fname,newfname); + return(system(cmd)); +} + +int32_t iguana_removefile(char *fname,int32_t scrubflag) +{ + FILE *fp; + char cmdstr[1024]; + long i,fpos; + if ( (fp= fopen(iguana_compatible_path(fname),"rb+")) != 0 ) + { + //printf("delete(%s)\n",fname); + if ( scrubflag != 0 ) + { + fseek(fp,0,SEEK_END); + fpos = ftell(fp); + rewind(fp); + for (i=0; i 0 && (len2= fread(buf2,1,sizeof(buf2),fp2)) == len ) + if ( (offset= memcmp(buf,buf2,len)) != 0 ) + printf("compare error at offset.%d: (%s) src.%ld vs. (%s) dest.%ld\n",offset,fname,ftell(fp),fname2,ftell(fp2)), errs++; + fclose(fp2); + } + fclose(fp); + } + return(errs); +} + +int64_t iguana_copyfile(char *src,char *dest,int32_t cmpflag) +{ + int64_t allocsize,len = -1; + char *buf; + FILE *srcfp,*destfp; + if ( (srcfp= fopen(iguana_compatible_path(src),"rb")) != 0 ) + { + if ( (destfp= fopen(iguana_compatible_path(dest),"wb")) != 0 ) + { + allocsize = 1024 * 1024 * 128L; + buf = mycalloc('F',1,allocsize); + while ( (len= fread(buf,1,allocsize,srcfp)) > 0 ) + if ( (long)fwrite(buf,1,len,destfp) != len ) + printf("write error at (%s) src.%ld vs. (%s) dest.%ld\n",src,ftell(srcfp),dest,ftell(destfp)); + len = ftell(destfp); + fclose(destfp); + myfree(buf,allocsize); + } + fclose(srcfp); + } + if ( len < 0 || (cmpflag != 0 && iguana_compare_files(src,dest) != 0) ) + printf("Error copying files (%s) -> (%s)\n",src,dest), len = -1; + return(len); +} + +void *map_file(char *fname,long *filesizep,int32_t enablewrite) +{ + //void *mmap64(void *addr,size_t len,int32_t prot,int32_t flags,int32_t fildes,off_t off); + int32_t fd,rwflags,flags = MAP_FILE|MAP_SHARED; + uint64_t filesize; + void *ptr = 0; + *filesizep = 0; + if ( enablewrite != 0 ) + fd = open(fname,O_RDWR); + else fd = open(fname,O_RDONLY); + if ( fd < 0 ) + { + printf("map_file: error opening enablewrite.%d %s\n",enablewrite,fname); + return(0); + } + if ( *filesizep == 0 ) + filesize = (uint64_t)lseek(fd,0,SEEK_END); + else filesize = *filesizep; + rwflags = PROT_READ; + if ( enablewrite != 0 ) + rwflags |= PROT_WRITE; + //#if __i386__ + ptr = mmap(0,filesize,rwflags,flags,fd,0); + //#else + //ptr = mmap64(0,filesize,rwflags,flags,fd,0); + //#endif + close(fd); + if ( ptr == 0 || ptr == MAP_FAILED ) + { + printf("map_file.write%d: mapping %s failed? mp %p\n",enablewrite,fname,ptr); + return(0); + } + *filesizep = filesize; + return(ptr); +} + +int32_t iguana_releasemap(void *ptr,uint64_t filesize) +{ + int32_t retval; + if ( ptr == 0 ) + { + printf("release_map_file: null ptr\n"); + return(-1); + } + retval = munmap(ptr,filesize); + if ( retval != 0 ) + printf("release_map_file: munmap error %p %llu: err %d\n",ptr,(long long)filesize,retval); + return(retval); +} + +void _iguana_closemap(struct iguana_mappedptr *mp) +{ + if ( mp->actually_allocated != 0 && mp->fileptr != 0 ) + myaligned_free(mp->fileptr,mp->allocsize); + else if ( mp->fileptr != 0 ) + iguana_releasemap(mp->fileptr,mp->allocsize); + mp->fileptr = 0; + mp->closetime = (uint32_t)time(NULL); + mp->opentime = 0; +} + +void iguana_closemap(struct iguana_mappedptr *mp) +{ + struct iguana_mappedptr tmp; + tmp = *mp; + _iguana_closemap(mp); + memset(mp,0,sizeof(*mp)); + mp->actually_allocated = tmp.actually_allocated; + mp->allocsize = tmp.allocsize; + mp->rwflag = tmp.rwflag; + strcpy(mp->fname,tmp.fname); +} + +int32_t iguana_syncmap(struct iguana_mappedptr *mp,long len) +{ + //static long Sync_total; + int32_t err = -1; + if ( mp->actually_allocated != 0 ) + return(0); + if ( mp->fileptr != 0 && mp->dirty != 0 ) + { + if ( len == 0 ) + len = mp->allocsize; + err = msync(mp->fileptr,len,MS_SYNC); + if ( err != 0 ) + printf("sync (%s) len %llu, err %d errno.%d\n",mp->fname,(long long)len,err,errno); + //Sync_total += len; + mp->dirty = 0; + } + return(err); +} + +long iguana_ensurefilesize(char *fname,long filesize,int32_t truncateflag) +{ + FILE *fp; + char *zeroes; + long i,n,allocsize = 0; + //printf("ensure_filesize.(%s) %ld %s | ",fname,filesize,mbstr(filesize)); + if ( (fp= fopen(iguana_compatible_path(fname),"rb")) != 0 ) + { + fseek(fp,0,SEEK_END); + allocsize = ftell(fp); + fclose(fp); + //printf("(%s) exists size.%ld\n",fname,allocsize); + } + else + { + //printf("try to create.(%s)\n",fname); + if ( (fp= fopen(iguana_compatible_path(fname),"wb")) != 0 ) + fclose(fp); + } + if ( allocsize < filesize ) + { + //printf("filesize.%ld is less than %ld\n",filesize,allocsize); + if ( (fp= fopen(iguana_compatible_path(fname),"ab")) != 0 ) + { + zeroes = myaligned_alloc(16L*1024*1024); + memset(zeroes,0,16*1024*1024); + n = filesize - allocsize; + while ( n > 16*1024*1024 ) + { + fwrite(zeroes,1,16*1024*1024,fp); + n -= 16*1024*1024; + fprintf(stderr,"+"); + } + for (i=0; i filesize ) + { + portable_truncate(fname,filesize); + return(filesize); + } + else return(allocsize); +} + +int32_t iguana_openmap(struct iguana_mappedptr *mp) +{ + uint64_t allocsize = mp->allocsize; + if ( mp->actually_allocated != 0 ) + { + if ( mp->fileptr == 0 ) + mp->fileptr = myaligned_alloc(mp->allocsize); + else memset(mp->fileptr,0,mp->allocsize); + return(0); + } + else + { + if ( mp->fileptr != 0 ) + { + //printf("opening already open mappedptr, pending %p\n",mp->pending); + iguana_closemap(mp); + } + mp->allocsize = allocsize; + // printf("calling map_file with expected %ld\n",mp->allocsize); + mp->fileptr = map_file(mp->fname,&mp->allocsize,mp->rwflag); + if ( mp->fileptr == 0 || mp->allocsize != allocsize ) + { + //printf("error mapping(%s) ptr %p mapped %ld vs allocsize %ld\n",mp->fname,mp->fileptr,mp->allocsize,allocsize); + return(-1); + } + mp->closetime = 0; + mp->opentime = (uint32_t)time(NULL); + } + return(0); +} + +void *iguana_mappedptr(void **ptrp,struct iguana_mappedptr *mp,uint64_t allocsize,int32_t rwflag,char *fname) +{ + uint64_t filesize; + mp->actually_allocated = !os_supports_mappedfiles(); + if ( fname != 0 ) + { + if ( strcmp(mp->fname,fname) == 0 ) + { + if ( mp->fileptr != 0 ) + { + iguana_releasemap(mp->fileptr,mp->allocsize); + mp->fileptr = 0; + } + iguana_openmap(mp); + if ( ptrp != 0 ) + (*ptrp) = mp->fileptr; + return(mp->fileptr); + } + strcpy(mp->fname,fname); + } + else mp->actually_allocated = 1; + mp->rwflag = rwflag; + mp->allocsize = allocsize; + if ( rwflag != 0 && mp->actually_allocated == 0 && allocsize != 0 ) + allocsize = iguana_ensurefilesize(fname,allocsize,0); + if ( iguana_openmap(mp) != 0 ) + { + char str[65]; + //printf("init_mappedptr %s.rwflag.%d | ",fname,rwflag); + if ( allocsize != 0 ) + printf("error mapping(%s) rwflag.%d ptr %p mapped %llu vs allocsize %llu %s\n",fname,rwflag,mp->fileptr,(long long)mp->allocsize,(long long)allocsize,mbstr(str,allocsize)); + else allocsize = mp->allocsize; + if ( rwflag != 0 && allocsize != mp->allocsize ) + { + filesize = mp->allocsize; + if ( mp->fileptr != 0 ) + iguana_releasemap(mp->fileptr,mp->allocsize); + mp->allocsize = allocsize; + mp->changedsize = (allocsize - filesize); + iguana_openmap(mp); + if ( mp->fileptr == 0 || mp->allocsize != allocsize ) + { + printf("SECOND error mapping(%s) ptr %p mapped %llu vs allocsize %llu\n",fname,mp->fileptr,(long long)mp->allocsize,(long long)allocsize); + exit(-1); + } + } + } + if ( ptrp != 0 ) + (*ptrp) = mp->fileptr; + return(mp->fileptr); +} + +//int64_t iguana_packetsallocated(struct iguana_info *coin) { return(coin->R.packetsallocated - coin->R.packetsfreed); }; + +void *filealloc(struct iguana_mappedptr *M,char *fname,struct iguana_memspace *mem,long size) +{ + //printf("mem->used %ld size.%ld | size.%ld\n",mem->used,size,mem->size); + //printf("filemalloc.(%s) new space.%ld %s\n",fname,mem->size,mbstr(size)); + memset(M,0,sizeof(*M)); + mem->totalsize = size; + if ( iguana_mappedptr(0,M,mem->totalsize,1,fname) == 0 ) + { + printf("couldnt create mapped file.(%s)\n",fname); + exit(-1); + } + mem->ptr = M->fileptr; + mem->used = 0; + return(M->fileptr); +} + +void *iguana_tmpalloc(struct iguana_info *coin,char *name,struct iguana_memspace *mem,long origsize) +{ + char fname[1024]; void *ptr; long size; +#ifdef __PNACL + return(mycalloc('T',1,origsize)); +#endif + //portable_mutex_lock(&mem->mutex); + if ( origsize != 0 && (mem->M.fileptr == 0 || (mem->used + origsize) > mem->totalsize) ) + { + coin->TMPallocated += origsize; + memset(&mem->M,0,sizeof(mem->M)); + sprintf(fname,"tmp/%s/%s.%d",coin->symbol,name,mem->counter), iguana_compatible_path(fname); + mem->counter++; + if ( mem->totalsize == 0 ) + { + //if ( strcmp(name,"recv") == 0 ) + // mem->size = IGUANA_RSPACE_SIZE * ((strcmp(coin->symbol,"BTC") == 0) ? 16 : 1); + // else +#ifdef IGUANA_MAPHASHTABLES + mem->totalsize = (1024 * 1024 * 128); +#else + mem->size = (1024 * 1024 * 16); +#endif + } + //if ( coin->R.RSPACE.size == 0 ) + // coin->R.RSPACE.size = mem->size; + if ( mem->totalsize > origsize ) + size = mem->totalsize; + else size = origsize; + fprintf(stderr,"filealloc.(%s) -> ",fname); + if ( filealloc(&mem->M,fname,mem,size) == 0 ) + { + printf("couldnt map tmpfile %s\n",fname); + return(0); + } + fprintf(stderr,"created\n"); + } + ptr = iguana_memalloc(mem,origsize,1); + //portable_mutex_unlock(&mem->mutex); + return(ptr); +} + +#ifdef oldway +void *iguana_kvfixiterator(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) +{ + int64_t offset = (int64_t)args; + if ( args != 0 && (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + { + item->hh.key = (void *)((uint64_t)item->hh.key + (int64_t)offset); + //printf("iguana_kvfixiterator rawind.%-5d: (%s)\n",item->rawind,bits256_str(*(bits256 *)item->hh.key)); + } + return(0); +} + +void *_iguana_kvensure(struct iguana_info *coin,int32_t rwflag,int32_t *maxindp,char *name,char *fname,double mult,int32_t incr,uint32_t ind,struct iguana_mappedptr *M,int32_t HDDvaluesize) +{ + long needed,prevsize = 0; + needed = (ind + 2) * HDDvaluesize; + //printf("ensure.%s ind.%d needed.%ld origptr.%p\n",kv->name,ind,needed,origptr); + if ( needed > M->allocsize ) + { + needed = (((needed * mult) + incr * HDDvaluesize) / HDDvaluesize) * HDDvaluesize; + printf("REMAP.%s %llu -> %ld [%ld] (%s) (ind.%d mult.%f incr.%d size.%d\n",name,(long long)M->allocsize,needed,(long)(needed - M->allocsize)/HDDvaluesize,fname,ind,mult,incr,HDDvaluesize); + if ( M->fileptr != 0 ) + { + iguana_syncmap(M,0); + iguana_releasemap(M->fileptr,M->allocsize); + M->fileptr = 0, prevsize = M->allocsize; + M->allocsize = 0; + } + needed = iguana_ensurefilesize(fname,needed,0); + } + if ( M->fileptr == 0 ) + { + if ( iguana_mappedptr(0,M,0,rwflag,fname) != 0 ) + { + if ( 1 && prevsize > M->allocsize ) + memset((void *)((uint64_t)M->fileptr + prevsize),0,(M->allocsize - prevsize)); + printf("%p %s maxitems.%llu (MEMsize.%ld / itemsize.%d) prevsize.%ld needed.%ld\n",M->fileptr,name,(long long)*maxindp,(long)M->allocsize,HDDvaluesize,prevsize,needed); + } + } + return(M->fileptr); +} + +void *iguana_kvensure(struct iguana_info *coin,struct iguanakv *kv,uint32_t ind) +{ + char fname[512]; void *origptr; int32_t maxitemind,n,rwflag = 1; + if ( (int32_t)ind < 0 ) + ind = 0; + origptr = kv->HDDitems; + kv->HDDitems = _iguana_kvensure(coin,rwflag,&kv->maxitemind,kv->name,kv->fname,kv->mult,kv->incr,ind,&kv->M,kv->HDDvaluesize); + if ( kv->HDDitemsp != 0 ) + *kv->HDDitemsp = kv->HDDitems; + n = (int32_t)(kv->M.allocsize / kv->HDDvaluesize); + if ( ind < n ) + ind = n; + if ( kv->valuesize3 != 0 ) + { + sprintf(fname,"%s3",kv->fname); + if ( (kv->HDDitems3= _iguana_kvensure(coin,rwflag,&maxitemind,kv->name,fname,kv->mult,kv->incr,ind,&kv->M3,kv->valuesize3)) == 0 ) + { + printf("HDDitems3 null ptr for %s\n",fname); + return(0); + } + //printf("third file\n"); + if ( kv->HDDitems3p != 0 ) + *kv->HDDitems3p = kv->HDDitems3; + } + if ( kv->valuesize2 != 0 ) + { + sprintf(fname,"%s2",kv->fname); + if ( (kv->HDDitems2= _iguana_kvensure(coin,rwflag,&maxitemind,kv->name,fname,kv->mult,kv->incr,ind,&kv->M2,kv->valuesize2)) == 0 ) + { + printf("HDDitems3 null ptr for %s\n",fname); + return(0); + } + //printf("second file\n"); + if ( kv->HDDitems2p != 0 ) + *kv->HDDitems2p = kv->HDDitems2; + } + if ( origptr != 0 && origptr != kv->M.fileptr && (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + { + if ( iguana_kviterate(coin,kv,(int64_t)((int64_t)kv->HDDitems - (int64_t)origptr),iguana_kvfixiterator) != 0 ) + printf("ERROR relinked pointers\n"); + else printf("hashtable relinked\n"); + kv->incr *= 1.25; + } + return(kv->HDDitems); +} + +void *iguana_kvsaveiterator(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) +{ + FILE *fp = (FILE *)args; + if ( args != 0 ) + { + if ( fwrite(value,1,kv->RAMvaluesize,fp) != kv->RAMvaluesize ) + { + printf("Error saving key.[%d]\n",kv->RAMvaluesize); + return(value); + } + } + return(0); +} + +long iguana_kvsave(struct iguana_info *coin,struct iguanakv *kv) +{ + FILE *fp; long retval = -1; char fname[512],oldfname[512],cmd[512]; + sprintf(fname,"%s.tmp",kv->name); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + if ( iguana_kviterate(coin,kv,(uint64_t)fp,iguana_kvsaveiterator) == 0 ) + { + printf("save %ld to HDD\n",ftell(fp)); + retval = ftell(fp); + } + else printf("error saving item at %ld\n",ftell(fp)); + fclose(fp); + } else printf("error creating(%s)\n",fname); + if ( retval > 0 ) + { + sprintf(oldfname,"%s.%u",kv->name,(uint32_t)time(NULL)); + sprintf(cmd,"%s %s %s",OS_mvstr(),kv->name,oldfname); + retval = system(cmd); + sprintf(cmd,"%s %s %s",OS_mvstr(),fname,kv->name); + retval = system(cmd); + } + return(retval); +} + +int32_t iguana_valuesize(struct iguana_info *coin,struct iguanakv *kv) +{ + int32_t valuesize = kv->RAMvaluesize; + return(valuesize); +} + +int32_t iguana_itemsize(struct iguana_info *coin,struct iguanakv *kv) +{ + int32_t itemsize = sizeof(struct iguana_kvitem); + if ( (kv->flags & IGUANA_ITEMIND_DATA) == 0 ) + itemsize += iguana_valuesize(coin,kv); + return(itemsize); +} + +void *iguana_itemvalue(struct iguana_info *coin,void **itemkeyp,struct iguanakv *kv,void *ptr,struct iguana_kvitem *item) +{ + void *itemvalue = 0; + *itemkeyp = 0; + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + { + if ( ptr != 0 ) + { + *itemkeyp = &((uint8_t *)ptr)[kv->keyoffset]; + itemvalue = ptr; + } else printf("error setting itemvalue\n"); + } + if ( *itemkeyp == 0 ) + { + *itemkeyp = item->keyvalue; + itemvalue = &item->keyvalue[kv->keysize]; + } + return(itemvalue); +} + +void *iguana_itemptr(struct iguana_info *coin,struct iguanakv *kv,uint32_t itemind) +{ + void *ptr = kv->HDDitems; + //printf("%s itemind.%d\n",kv->name,itemind); + if ( ptr == 0 || itemind >= kv->maxitemind ) + { + kv->HDDitems = iguana_kvensure(coin,kv,itemind+1); + if ( (ptr= kv->HDDitems) == 0 ) + { + printf("SECOND ERROR %s overflow? %p itemind.%llu vs max.%llu\n",kv->name,ptr,(long long)itemind,(long long)kv->maxitemind); + return(0); + } + } + ptr = (void *)((uint64_t)ptr + kv->HDDvaluesize*itemind); + return(ptr); +} + +void iguana_copy(struct iguana_info *coin,struct iguanakv *kv,int32_t rwflag,void *itemvalue,void *value,int32_t valuesize) +{ + void *src,*dest; + /*if ( (kv->flags & IGUANA_MAPPED_ITEM) != 0 && rwflag != 0 ) + { + itemvalue = (void *)(((uint64_t))itemvalue + sizeof(UT_hash_handle)); + value = (void *)(((uint64_t))value + sizeof(UT_hash_handle)); + valuesize -= sizeof(UT_hash_handle); + //printf("value.%p itemvalue.%p valuesize.%d\n",value,itemvalue,valuesize); + }*/ + if ( rwflag != 0 ) + src = value, dest = itemvalue; + else src = itemvalue, dest = value; + memcpy(dest,src,valuesize); +} + +static int32_t iguana_RWmmap(int32_t writeflag,void *value,struct iguana_info *coin,struct iguanakv *kv,uint32_t itemind) +{ + static const uint8_t zeroes[4096]; + void *ptr,*itemvalue=0; int32_t i,itemsize,valuesize,retval = 0; + itemsize = iguana_itemsize(coin,kv); + valuesize = iguana_valuesize(coin,kv); + if ( (ptr= iguana_itemptr(coin,kv,itemind)) != 0 ) + { + //itemvalue = iguana_itemvalue(coin,&itemkey,kv,ptr,0); + if ( writeflag != 0 ) + { + //struct iguana777_addrinfo *A; struct coin_offsets B,tmpB; + //itemsize = kv->RAMvaluesize; + /*if ( strcmp(sp->name,"addrinfos") == 0 ) + { + A = ptr; + itemsize = (sizeof(*A) - sizeof(A->coinaddr) + A->addrlen + A->scriptlen); + }*/ + if ( writeflag == 1 && (kv->flags & IGUANA_VOLATILE) == 0 ) + { + /*if ( strcmp(sp->name,"blocks") == 0 ) + { + memcpy(&B,ptr,sizeof(B)); + memcpy(&tmpB,value,sizeof(tmpB)); + if ( memcmp(&B.blockhash.bytes,zeroes,sizeof(B.blockhash)) == 0 && memcmp(&B.merkleroot.bytes,zeroes,sizeof(B.merkleroot)) == 0 ) + B.blockhash = tmpB.blockhash, B.merkleroot = tmpB.merkleroot, ptr = &B; + }*/ + if ( 0 && memcmp(value,itemvalue,valuesize) != 0 && valuesize <= sizeof(zeroes) ) + { + if ( memcmp(itemvalue,zeroes,valuesize) != 0 ) + { + printf("\n"); + for (i=0; iname,kv->RAMvaluesize); + for (i=0; iname,kv->RAMvaluesize,itemind,kv->M.fileptr,ptr); + } + } + } + if ( kv->fp == 0 ) + iguana_copy(coin,kv,writeflag,ptr,value,valuesize); + else // all ready for rb+ fp and readonly mapping, but need to init properly + { + /*fseek(sp->fp,(uint64_t)sp->itemsize * itemind,SEEK_SET); + fwrite(value,1,valuesize,sp->fp); + if ( memcmp(itemvalue,value,valuesize) != 0 ) + printf("FATAL: write mmap error\n"), getchar();*/ + printf("iguana_RWmmap: need to test sp->fp first\n"), exit(1); + } + } + else iguana_copy(coin,kv,writeflag,ptr,value,valuesize); + } else retval = -2; + return(retval); +} + +void iguana_kvlock(struct iguana_info *coin,struct iguanakv *kv) +{ + if ( kv->threadsafe != 0 ) + portable_mutex_lock(&kv->KVmutex); +} + +void iguana_kvunlock(struct iguana_info *coin,struct iguanakv *kv) +{ + if ( kv->threadsafe != 0 ) + portable_mutex_unlock(&kv->KVmutex); +} + +int32_t iguana_kvdelete(struct iguana_info *coin,struct iguanakv *kv,void *key) +{ + int32_t retval = -1; struct iguana_kvitem *ptr = 0; + if ( kv == 0 ) + return(-1); + iguana_kvlock(coin,kv); + HASH_FIND(hh,kv->hashtables[((uint8_t *)key)[kv->keysize>>1]],key,kv->keysize,ptr); + if ( ptr != 0 ) + { + HASH_DELETE(hh,kv->hashtables[((uint8_t *)key)[kv->keysize>>1]],ptr); + if ( (kv->flags & IGUANA_MAPPED_ITEM) == 0 ) + myfree(ptr,iguana_itemsize(coin,kv)); + retval = 0; + } + iguana_kvlock(coin,kv); + return(retval); +} + +void *_iguana_kvread(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,uint32_t *itemindp) +{ + void *itemkey,*itemvalue,*ptr=0; int32_t valuesize,itemind = 0; struct iguana_kvitem *item = 0; + if ( kv == 0 ) + { + printf("iguana_kvread: null ramkv??\n"); + return(0); + } + if ( kv->keysize == 0 ) + { + printf("kvwrite %s only supports itemind MMap access\n",kv->name); + return(0); + } + valuesize = iguana_valuesize(coin,kv); + //printf("search for [%llx] keysize.%d\n",*(long long *)key,kv->keysize); + HASH_FIND(hh,kv->hashtables[((uint8_t *)key)[kv->keysize>>1]],key,kv->keysize,item); + if ( item != 0 ) + { + if ( itemindp != 0 ) + *itemindp = itemind = item->hh.itemind; + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + ptr = iguana_itemptr(coin,kv,itemind); + if ( (itemvalue= iguana_itemvalue(coin,&itemkey,kv,ptr,item)) != 0 ) + { + //printf("itemind.%d: key.%p value.%p valuesize.%d\n",itemind,itemkey,itemvalue,valuesize); + iguana_copy(coin,kv,0,itemvalue,value,valuesize); + } + else printf("_kvread null itemvalue for itemind.%d\n",itemind); + return(value); + } + //printf("cache miss %s\n",bits256_str(*(bits256 *)key)); + if ( itemindp != 0 ) + *itemindp = 0; + return(0); +} + +void *_iguana_kvwrite(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,uint32_t *itemindp) +{ + void *ptr=0,*itemvalue=0,*itemkey=0; struct iguana_kvitem *item = 0; uint32_t itemsize,valuesize,itemind = *itemindp; + if ( kv == 0 ) + return(0); + valuesize = iguana_valuesize(coin,kv); + itemsize = iguana_itemsize(coin,kv); + if ( kv->keysize == 0 ) + { + printf("kvwrite %s only supports itemind MMap access\n",kv->name); + return(0); + } + HASH_FIND(hh,kv->hashtables[((uint8_t *)key)[kv->keysize>>1]],key,kv->keysize,item); + if ( item != 0 ) + { + if ( 0 && itemind != item->hh.itemind && itemind != (uint32_t)-1 ) + { + printf("%s override itemind %d -> %d\n",kv->name,item->hh.itemind,itemind); + item->hh.itemind = itemind; + } else itemind = item->hh.itemind; + *itemindp = itemind; + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + ptr = iguana_itemptr(coin,kv,itemind); + if ( (itemvalue= iguana_itemvalue(coin,&itemkey,kv,ptr,item)) != 0 ) + { + if ( memcmp(itemvalue,value,valuesize) != 0 ) + { + //printf("%s: item.%d updating %p\n",kv->name,item->hh.itemind,key); + iguana_copy(coin,kv,1,itemvalue,value,valuesize); + kv->updated++; + } + } else printf("kvwrite null itemvalue itemind.%d\n",itemind); + return(item); + } + else + { + kv->numkeys++; + if ( itemind == (uint32_t)-1 ) + itemind = kv->numkeys; + *itemindp = itemind; + if ( item == 0 ) + { + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + ptr = iguana_itemptr(coin,kv,itemind); + item = ((kv->flags & IGUANA_MAPPED_ITEM) == 0) ? mycalloc('I',1,itemsize) : iguana_tmpalloc(coin,kv->name,&kv->HASHPTRS,itemsize); + if ( item == 0 ) + printf("fatal out of mem error\n"), getchar(); + } + itemvalue = iguana_itemvalue(coin,&itemkey,kv,ptr,item); + } + if ( itemvalue != 0 && itemkey != 0 && item != 0 ) + { + valuesize = iguana_valuesize(coin,kv); + item->hh.itemind = itemind; + iguana_copy(coin,kv,1,itemvalue,value,valuesize); + memcpy(itemkey,key,kv->keysize); + //if ( strcmp(kv->name,"txids") == 0 ) + //printf("add.(%s) itemind.%d kv->numkeys.%d keysize.%d (%s) valuesize.%d:%d\n",kv->name,itemind,kv->numkeys,kv->keysize,bits256_str(*(bits256 *)key),kv->HDDvaluesize,kv->RAMvaluesize); + HASH_ADD_KEYPTR(hh,kv->hashtables[((uint8_t *)itemkey)[kv->keysize>>1]],itemkey,kv->keysize,item); + kv->M.dirty++; + HASH_FIND(hh,kv->hashtables[((uint8_t *)key)[kv->keysize>>1]],key,kv->keysize,item); + if ( kv->dispflag != 0 || item == 0 || item->hh.itemind != itemind ) + fprintf(stderr,">> %s found item.%p iguana_kvwrite numkeys.%d kv.(%p) table.%p write kep.%p size.%d, %p value.(%08x) size.%d itemind.%d:%d\n",kv->name,item,kv->numkeys,key,kv->hashtables[((uint8_t *)itemkey)[kv->keysize>>1]],itemkey,kv->keysize,itemvalue,itemvalue!=0?calc_crc32(0,itemvalue,valuesize):0,valuesize,item!=0?item->hh.itemind:0,itemind); + if ( item != 0 ) + return(value); + else printf("null item after find kvwrite error\n"), getchar(); + } else printf("kvwrite pointer error %p %p %p\n",itemkey,itemvalue,item), getchar(); + return(0); +} + +void *iguana_kvread(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,uint32_t *itemindp) +{ + void *retptr = 0; + // if ( strcmp(kv->name,"txids") == 0 ) + // printf("iguana_kvread.(%s) key.%p keysize.%d flag.%d itemind.%d\n",kv->name,key,kv->keysize,kv->flags,*itemindp); + portable_mutex_lock(&kv->MMmutex); + if ( key == 0 || kv->keysize == 0 ) + { + if ( iguana_RWmmap(0,value,coin,kv,*itemindp) == 0 ) + retptr = value; + else printf("%s %d vs %d RMmmap.0 error\n",kv->name,*itemindp,(int32_t)(kv->M.allocsize/kv->HDDvaluesize)); + } else retptr = _iguana_kvread(coin,kv,key,value,itemindp); + portable_mutex_unlock(&kv->MMmutex); + return(retptr); +} + +void *iguana_kvwrite(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,uint32_t *itemindp) +{ + void *retptr = 0; + portable_mutex_lock(&kv->MMmutex); + if ( key == 0 || kv->keysize == 0 ) + { + kv->M.dirty++; + if ( iguana_RWmmap(1,value,coin,kv,*itemindp) == 0 ) + retptr = value; + else printf("%s %d vs %d RMmmap.1 error\n",kv->name,*itemindp,(int32_t)(kv->M.allocsize/kv->HDDvaluesize)); + } else retptr = _iguana_kvwrite(coin,kv,key,value,itemindp); + portable_mutex_unlock(&kv->MMmutex); + return(retptr); +} + +int32_t iguana_kvchecktable(struct iguana_info *coin,struct iguanakv *kv) +{ + uint32_t itemind,checkind; int32_t err = 0; uint8_t key[8192]; + for (itemind=1; itemind<=kv->numkeys; itemind++) + { + if ( iguana_RWmmap(0,kv->space,coin,kv,itemind) == 0 ) + { + if ( kv->keysize != 0 && kv->keysize < sizeof(key) ) + { + memcpy(key,(void *)((long)kv->space + kv->keyoffset),kv->keysize); + if ( _iguana_kvread(coin,kv,key,kv->space,&checkind) == 0 || checkind != itemind ) + { + printf("kvread.%s miscompares checkind.%d vs %d\n",kv->name,checkind,itemind); + err++; + } + } + } else err++, printf("%s itemind.%d doesnt map properly\n",kv->name,itemind); + } + return(-err); +} + +void *iguana_kviterate(struct iguana_info *coin,struct iguanakv *kv,uint64_t args,void *(*iterator)(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize)) +{ + struct iguana_kvitem *item,*tmp; int32_t t; void *ptr=0,*itemvalue,*itemkey=0,*retval = 0; + if ( kv == 0 ) + return(0); + for (t=0; t<0x100; t++) + { + HASH_ITER(hh,kv->hashtables[t],item,tmp) + { + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + ptr = iguana_itemptr(coin,kv,item->hh.itemind); + if ( (itemvalue= iguana_itemvalue(coin,&itemkey,kv,ptr,item)) != 0 && itemkey != 0 ) + { + if ( (retval= (*iterator)(coin,kv,item,args,itemkey,itemvalue,kv->RAMvaluesize)) != 0 ) + return(retval); + } else return(retval); + } + } + return(0); +} + +int32_t iguana_kvtruncate(struct iguana_info *coin,struct iguanakv *kv,uint32_t maxitemind) +{ + struct iguana_kvitem *item,*tmp; int32_t t,n = 0; + if ( kv->numkeys < maxitemind ) + return(-1); + for (t=0; t<0x100; t++) + { + HASH_ITER(hh,kv->hashtables[t],item,tmp) + { + if ( item->hh.itemind >= maxitemind ) + { + HASH_DEL(kv->hashtables[t],item); + if ( (kv->flags & IGUANA_MAPPED_ITEM) == 0 ) + myfree(item,iguana_itemsize(coin,kv)); + n++; + } + } + } + printf(">>>>>>>>>> kv.%s truncated.%d to maxitemind.%d\n",kv->name,n,maxitemind); + kv->numkeys = maxitemind; + return(iguana_kvchecktable(coin,kv)); +} + +void iguana_kvfree(struct iguana_info *coin,struct iguanakv *kv) +{ + struct iguana_kvitem *ptr,*tmp; int32_t t; + if ( kv != 0 ) + { + iguana_kvlock(coin,kv); + for (t=0; t<0x100; t++) + { + HASH_ITER(hh,kv->hashtables[t],ptr,tmp) + { + HASH_DEL(kv->hashtables[t],ptr); + if ( (kv->flags & IGUANA_MAPPED_ITEM) == 0 ) + myfree(ptr,iguana_itemsize(coin,kv)); + } + } + iguana_kvunlock(coin,kv); + myfree(kv,sizeof(*kv)); + } +} + +int32_t iguana_kvclone(struct iguana_info *coin,struct iguanakv *clone,struct iguanakv *kv) +{ + void *ptr=0,*itemkey,*itemvalue; struct iguana_kvitem *item,*tmp; int32_t t,n = 0; + printf("need to add support for mapped data\n"); + if ( kv != 0 ) + { + for (t=0; t<0x100; t++) + { + HASH_ITER(hh,kv->hashtables[t],item,tmp) + { + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + ptr = iguana_itemptr(coin,kv,item->hh.itemind); + if ( (itemvalue= iguana_itemvalue(coin,&itemkey,kv,ptr,item)) != 0 && itemkey != 0 ) + { + iguana_kvwrite(coin,clone,itemkey,itemvalue,&item->hh.itemind); + n++; + } + } + } + } + return(n); +} + +int32_t iguana_kvdisp(struct iguana_info *coin,struct iguanakv *kv) +{ + struct iguana_kvitem *item,*tmp; void *ptr=0,*itemkey,*itemvalue; int32_t t,n = 0; char hexstr[8192]; + printf("iguana_kvdisp.(%s) numkeys.%d\n",kv->name,kv->numkeys); + if ( kv == 0 ) + return(0); + for (t=0; t<0x100; t++) + { + HASH_ITER(hh,kv->hashtables[t],item,tmp) + { + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + ptr = iguana_itemptr(coin,kv,item->hh.itemind); + if ( (itemvalue= iguana_itemvalue(coin,&itemkey,kv,ptr,item)) != 0 ) + { + init_hexbytes_noT(hexstr,itemvalue,kv->RAMvaluesize); + char str[65]; + bits256_str(str,*(bits256 *)itemkey); + printf("itemind.%d %s %s len.%d height.%d\n",item->hh.itemind,str,hexstr,kv->RAMvaluesize,((struct iguana_block *)itemvalue)->height); + } + n++; + } + } + printf("iguana_kvdisp.(%s) n.%d items\n",kv->name,n); + return(n); +} +#endif + diff --git a/deprecated/iguana_recv.dev b/deprecated/iguana_recv.dev new file mode 100755 index 000000000..52bb3810c --- /dev/null +++ b/deprecated/iguana_recv.dev @@ -0,0 +1,812 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +// peer context, ie massively multithreaded -> bundlesQ + +struct iguana_bundlereq *iguana_bundlereq(struct iguana_info *coin,struct iguana_peer *addr,int32_t type,int32_t datalen) +{ + struct iguana_bundlereq *req; int32_t allocsize; + allocsize = (uint32_t)sizeof(*req) + datalen; + req = mycalloc(type,1,allocsize); + req->allocsize = allocsize; + req->datalen = datalen; + req->addr = addr; + req->coin = coin; + req->type = type; + return(req); +} + +void iguana_gotblockM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,uint8_t *data,int32_t recvlen) +{ + struct iguana_bundlereq *req; struct iguana_txblock *txdata = 0; int32_t i; char fname[1024]; + if ( 0 ) + { + for (i=0; ispace[0]; i++) + if ( txdata->space[i] != 0 ) + break; + if ( i != txdata->space[0] ) + { + for (i=0; ispace[0]; i++) + printf("%02x ",txdata->space[i]); + printf("extra\n"); + } + } + req = iguana_bundlereq(coin,addr,'B',0); + txdata = origtxdata; + if ( addr != 0 ) + { + if ( addr->pendblocks > 0 ) + addr->pendblocks--; + addr->lastblockrecv = (uint32_t)time(NULL); + addr->recvblocks += 1.; + addr->recvtotal += recvlen; + origtxdata->block.ipbits = addr->ipbits; + iguana_ramchain_data(coin,addr,origtxdata,txarray,origtxdata->block.txn_count,data,recvlen); + { + txdata->block.ipbits = addr->ipbits; + if ( 0 ) + { + struct iguana_txblock *checktxdata; struct iguana_memspace checkmem; int32_t checkbundlei; + memset(&checkmem,0,sizeof(checkmem)); + iguana_meminit(&checkmem,"checkmem",0,txdata->datalen + 4096,0); + if ( (checktxdata= iguana_peertxdata(coin,&checkbundlei,fname,&checkmem,addr->ipbits,txdata->block.hash2)) != 0 ) + { + printf("check datalen.%d bundlei.%d T.%d U.%d S.%d P.%d X.%d\n",checktxdata->datalen,checkbundlei,checktxdata->numtxids,checktxdata->numunspents,checktxdata->numspends,checktxdata->numpkinds,checktxdata->numexternaltxids); + } + iguana_mempurge(&checkmem); + } + } + req->datalen = txdata->datalen; + } + //printf("datalen.%d\n",req->datalen); + if ( req->datalen == 0 ) + req->datalen = recvlen; + req->block = txdata->block; + req->ipbits = txdata->block.ipbits; + req->block.txn_count = req->numtx = txdata->block.txn_count; + coin->recvcount++; + coin->recvtime = (uint32_t)time(NULL); + req->addr = addr; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gottxidsM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *txids,int32_t n) +{ + struct iguana_bundlereq *req; + printf("got %d txids from %s\n",n,addr->ipaddr); + req = iguana_bundlereq(coin,addr,'T',0); + req->hashes = txids, req->n = n; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gotunconfirmedM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgtx *tx,uint8_t *data,int32_t datalen) +{ + struct iguana_bundlereq *req; + char str[65]; bits256_str(str,tx->txid); + printf("%s unconfirmed.%s\n",addr->ipaddr,str); + req = iguana_bundlereq(coin,addr,'U',datalen); + req->datalen = datalen; + memcpy(req->serialized,data,datalen); + //iguana_freetx(tx,1); + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n) +{ + struct iguana_bundlereq *req; + if ( addr != 0 ) + { + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + //printf("%s blocks[%d] ht.%d gotheaders pend.%d %.0f\n",addr->ipaddr,n,blocks[0].height,addr->pendhdrs,milliseconds()); + } + req = iguana_bundlereq(coin,addr,'H',0); + req->blocks = blocks, req->n = n; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashes,int32_t n) +{ + struct iguana_bundlereq *req; + if ( addr != 0 ) + { + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + } + req = iguana_bundlereq(coin,addr,'S',0); + req->hashes = blockhashes, req->n = n; + //printf("bundlesQ blockhashes.%p[%d]\n",blockhashes,n); + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_blocklink(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,struct iguana_block *prev,struct iguana_block *block) +{ + if ( block == 0 || prev == 0 ) + return; + //printf("%p prev-> %p [%d:%d] %p block %p\n",prev,prev->hh.next,bp!=0?bp->bundleheight:-1,bundlei,block->hh.prev,block); + if ( (prev->hh.next == 0 || prev->hh.next == block) && block->hh.prev == 0 ) + block->hh.prev = prev; + else if ( prev->hh.next == 0 && block->hh.prev == prev ) + prev->hh.next = block; + else if ( prev->hh.next == block && block->hh.prev == prev ) + { + /*if ( bundlei < coin->chain->bundlesize ) + { + if ( bp != 0 && iguana_bundlehash2add(coin,0,bp,bundlei,block->hash2) < 0 ) + { + if ( prev->mainchain == 0 ) + block->hh.prev = prev->hh.next = 0; + printf("clear hashes?\n"); + //memset(bp->hashes[i].bytes,0,sizeof(bp->hashes[i])); + } + } + else if ( bp != 0 && bp->bundleheight + coin->chain->bundlesize >= coin->bundlescount*coin->chain->bundlesize ) + { + char str[65]; printf("AUTOCREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,block->hash2)); + iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,block->hash2); + //for (j=2; jn; j++) + // iguana_blockQ(coin,bp,j,bp->hashes[j],0); + }*/ + } + else if ( prev->mainchain == 0 ) + block->hh.prev = prev->hh.next = 0; +} + +void iguana_recvissue(struct iguana_info *coin,struct iguana_block *origblock) +{ + int32_t height,bundlei,i; struct iguana_block *prev,*block; bits256 prevhash2; struct iguana_bundle *bp; + block = iguana_blockhashset(coin,-1,origblock->prev_block,1); + if ( block != origblock ) + iguana_blockcopy(coin,block,origblock); + for (i=0; ichain->bundlesize; i++) + { + if ( block != 0 && bits256_nonz(block->prev_block) > 0 ) + { + prev = iguana_blockhashset(coin,-1,block->prev_block,1); + bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,block->hash2); + if ( prev != 0 && prev->ipbits == 0 && bp != 0 && bp->bundleheight >= 0 ) + { + prevhash2 = iguana_blockhash(coin,bp->bundleheight + bundlei - 1); + if ( bits256_nonz(prevhash2) == 0 ) + { + height = bp->bundleheight + bundlei - 1; + //char str[65]; printf("[%d] RECVISSUE.%s %d\n",i,bits256_str(str,block->prev_block),height); + iguana_hash2set(coin,"recvadd",coin->bundles[height/coin->chain->bundlesize],height%coin->chain->bundlesize,block->prev_block); + iguana_blockQ(coin,coin->bundles[height/coin->chain->bundlesize],height%coin->chain->bundlesize,block->prev_block,1); + break; + } + } + block = prev; + } else break; + } +} + +// main context, ie single threaded +struct iguana_bundlereq *iguana_recvblockhashes(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *blockhashes,int32_t num) +{ + int32_t i,bundlei; struct iguana_bundle *bp; struct iguana_block *block,*prev = 0; + bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,blockhashes[1]); + if ( (rand() % 100) == 0 ) + { + char str[65]; printf("got %d hashes %d:%d %s\n",num,bp==0?-1:bp->bundleheight,bundlei,bits256_str(str,blockhashes[1])); + } + if ( bp == 0 || bundlei != 1 || num <= 2 ) + { + if ( num > 2 ) + iguana_blockQ(coin,0,-1,blockhashes[1],1); + return(req); + } + bp->hdrtime = (uint32_t)time(NULL); + //if ( num > coin->chain->bundlesize+1 ) + // num = coin->chain->bundlesize+1; + for (i=0; i 0 ) + { + if ( (block= iguana_blockhashset(coin,-1,blockhashes[i],1)) != 0 && prev != 0 && block->ipbits == 0 ) + { + prev->hh.next = block; + if ( prev->mainchain != 0 ) + { + //char str[65]; printf("idle issue %s %d\n",bits256_str(str,block->hash2),prev->height+1); + iguana_blockQ(coin,0,-1,block->hash2,0); + } + } + if ( block != 0 && block->ipbits == 0 && i < coin->chain->bundlesize ) + iguana_hash2set(coin,"recvhash",bp,i,blockhashes[i]); + if ( (i % coin->chain->bundlesize) == 0 || i == 1 ) + iguana_blockQ(coin,0,-1,blockhashes[i],1); + else //if ( bp->hdrsi < 100 ) + iguana_blockQ(coin,0,-1,blockhashes[i],0); + } + prev = block; + } + return(req); +} + +struct iguana_bundle *iguana_bundleset(struct iguana_info *coin,struct iguana_block **blockp,int32_t *bundleip,struct iguana_block *origblock) +{ + struct iguana_block *block,*prev = 0; struct iguana_bundle *bp = 0; int32_t bundlei = -2; + if ( origblock == 0 ) + return(0); + block = iguana_blockhashset(coin,-1,origblock->hash2,1); + *blockp = block; + if ( block != 0 ) + { + if ( bits256_nonz(origblock->prev_block) > 0 ) + { + prev = iguana_blockhashset(coin,-1,origblock->prev_block,1); + //iguana_blocklink(coin,0,-1,prev,block); + if ( prev != 0 ) + { + prev->hh.next = block, block->hh.prev = prev; + //printf("link block\n"); + } + } + if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,origblock->hash2)) != 0 ) + { + if ( bundlei < coin->chain->bundlesize ) + { + iguana_blocklink(coin,bp,bundlei,prev,block); + block->bundlei = bundlei; + block->hdrsi = bp->hdrsi; + block->havebundle = 1; + iguana_hash2set(coin,"blockadd",bp,block->bundlei,block->hash2); + } + } + else if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,origblock->prev_block)) != 0 ) + { + if ( bundlei < coin->chain->bundlesize-1 ) + { + block->bundlei = ++bundlei; + block->hdrsi = bp->hdrsi; + block->havebundle = 1; + iguana_hash2set(coin,"blockadd",bp,block->bundlei,block->hash2); + } + else if ( bundlei == coin->chain->bundlesize-1 ) + { + char str[65]; printf("CREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,origblock->hash2)); + iguana_blockQ(coin,0,-1,origblock->hash2,1); + iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,origblock->hash2); + } + } + else + { + //char str[65]; printf("can find.(%s)\n",bits256_str(str,origblock->hash2)); + return(0); + } + if ( block->havebundle != 0 && block->hdrsi < coin->bundlescount ) + { + bundlei = block->bundlei; + bp = coin->bundles[block->hdrsi]; + } + //char str[65]; printf("iguana_recvblock (%s) %d %d[%d] %p\n",bits256_str(str,block->hash2),block->havebundle,block->hdrsi,bundlei,bp); + } + *bundleip = bundlei; + return(bp); +} + +struct iguana_bundlereq *iguana_recvblockhdrs(struct iguana_info *coin,struct iguana_bundlereq *req,struct iguana_block *blocks,int32_t n,int32_t *newhwmp) +{ + int32_t i,bundlei; struct iguana_block *block; struct iguana_bundle *bp; + if ( blocks == 0 ) + { + printf("iguana_recvblockhdrs null blocks?\n"); + return(req); + } + if ( blocks != 0 && n > 0 ) + { + for (i=0; iprev_block.bytes,coin->blocks.hwmchain.hash2.bytes,sizeof(bits256)) == 0 ) + _iguana_chainlink(coin,block); + if ( bundlei == 1 ) + iguana_blockQ(coin,0,-1,blocks[i].hash2,1); + else if ( bp != 0 && bp->hdrsi < IGUANA_MAXACTIVEBUNDLES ) + iguana_blockQ(coin,0,-1,blocks[i].hash2,0); + + //fprintf(stderr,"i.%d of %d iguana_chainextend\n",i,n); + //iguana_chainextend(coin,&blocks[i]); + } + } + return(req); +} + +struct iguana_bundlereq *iguana_recvblock(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundlereq *req,struct iguana_block *origblock,int32_t numtx,int32_t datalen,int32_t *newhwmp) +{ + struct iguana_bundle *bp=0; char str[65]; int32_t tmp,bundlei = -2; + bits256 hash2; struct iguana_block *block,*prev,*ptr; double duration; + bp = iguana_bundleset(coin,&block,&bundlei,origblock); + if ( block != origblock ) + iguana_blockcopy(coin,block,origblock); + if ( memcmp(block->prev_block.bytes,coin->blocks.hwmchain.hash2.bytes,sizeof(bits256)) == 0 ) + { + _iguana_chainlink(coin,block); + iguana_bundleset(coin,&ptr,&tmp,block); + //printf("HWMCHAIN %s height.%d\n",bits256_str(str,block->hash2),coin->blocks.hwmchain.height+1); + } + else if ( (prev= iguana_blockfind(coin,block->prev_block)) != 0 ) + { + if ( prev->mainchain != 0 && prev->height >= 0 ) + { + block->mainchain = 1; + block->height = prev->height+1; + iguana_bundleset(coin,&ptr,&tmp,block); + hash2 = iguana_blockhash(coin,block->height+1); + if ( bits256_nonz(hash2) == 0 && (ptr= block->hh.next) != 0 ) + hash2 = ptr->hash2; + if ( bits256_nonz(hash2) > 0 && (ptr= iguana_blockfind(coin,hash2)) != 0 && ptr->ipbits == 0 ) + { + //printf("AUTONEXT.%d\n",block->height+1); + if ( prev->height == coin->blocks.hwmchain.height ) + { + coin->backstop = coin->blocks.hwmchain.height+1; + coin->backstophash2 = hash2; + coin->backstopmillis = milliseconds(); + } + iguana_blockQ(coin,0,-1,hash2,0); + } + } + } + if ( strcmp(coin->symbol,"BTC") != 0 ) + iguana_recvissue(coin,block); + if ( bp != 0 && bundlei >= 0 ) + { + if ( bp->bundleheight+bundlei > coin->longestchain ) + coin->longestchain = bp->bundleheight+bundlei; + if ( bp->bundleheight+bundlei == coin->blocks.hwmchain.height+1 && bp->requests[bundlei] > 99 ) + printf("recv bundlei.%d hdrs.%d reqs.[%d]\n",bundlei,bp->hdrsi,bp->requests[bundlei]); + if ( 0 && bundlei == 1 && bp->numhashes < bp->n ) + { + bits256_str(str,block->prev_block); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1); + } + if ( datalen > 0 ) + { + SETBIT(bp->recv,bundlei); + if ( bp->issued[bundlei] > 0 ) + { + duration = 1000. * ((int32_t)time(NULL) - bp->issued[bundlei]); + if ( duration < bp->avetime/10. ) + duration = bp->avetime/10.; + else if ( duration > bp->avetime*10. ) + duration = bp->avetime * 10.; + dxblend(&bp->avetime,duration,.9); + if ( bp->avetime < 100 ) + bp->avetime = 100; + dxblend(&coin->avetime,bp->avetime,.9); + if ( coin->avetime < 100 ) + coin->avetime = 100; + } + if ( bundlei >= 0 && bundlei < bp->n ) + { + //bp->blocks[bundlei] = block; + //bp->numrecv++; + } + } + if ( strcmp(coin->symbol,"BTC") != 0 && bp->bundleheight+bundlei == coin->blocks.hwmchain.height+1 ) + { + _iguana_chainlink(coin,block); + //if ( (rand() % 10) == 0 ) + { + hash2 = iguana_blockhash(coin,coin->blocks.hwmchain.height+2); + if ( bits256_nonz(hash2) > 0 ) + { + printf("AUTOBLOCK.%d\n",coin->blocks.hwmchain.height+2); + iguana_blockQ(coin,0,-1,hash2,0); + } + } + } + } + if ( block != 0 ) + { + block->recvlen = datalen; + if ( bp != 0 ) + block->ipbits = req->ipbits; + //printf("datalen.%d ipbits.%x\n",datalen,req->ipbits); + } else printf("cant create block.%llx block.%p bp.%p bundlei.%d\n",(long long)origblock->hash2.txid,block,bp,bundlei); + return(req); +} + +struct iguana_bundlereq *iguana_recvtxids(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *txids,int32_t n) +{ + return(req); +} + +struct iguana_bundlereq *iguana_recvunconfirmed(struct iguana_info *coin,struct iguana_bundlereq *req,uint8_t *data,int32_t datalen) +{ + return(req); +} + +int32_t iguana_processbundlesQ(struct iguana_info *coin,int32_t *newhwmp) // single threaded +{ + int32_t flag = 0; struct iguana_bundlereq *req; + *newhwmp = 0; + while ( flag < IGUANA_BUNDLELOOP && (req= queue_dequeue(&coin->bundlesQ,0)) != 0 ) + { + //printf("%s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n); + if ( req->type == 'B' ) // one block with all txdata + req = iguana_recvblock(coin,req->addr,req,&req->block,req->numtx,req->datalen,newhwmp); + else if ( req->type == 'H' ) // blockhdrs (doesnt have txn_count!) + { + if ( (req= iguana_recvblockhdrs(coin,req,req->blocks,req->n,newhwmp)) != 0 ) + { + if ( req->blocks != 0 ) + myfree(req->blocks,sizeof(*req->blocks) * req->n), req->blocks = 0; + } + } + else if ( req->type == 'S' ) // blockhashes + { + if ( (req= iguana_recvblockhashes(coin,req,req->hashes,req->n)) != 0 && req->hashes != 0 ) + myfree(req->hashes,sizeof(*req->hashes) * req->n), req->hashes = 0; + } + else if ( req->type == 'U' ) // unconfirmed tx + req = iguana_recvunconfirmed(coin,req,req->serialized,req->datalen); + else if ( req->type == 'T' ) // txids from inv + { + if ( (req= iguana_recvtxids(coin,req,req->hashes,req->n)) != 0 ) + myfree(req->hashes,(req->n+1) * sizeof(*req->hashes)), req->hashes = 0; + } + else printf("iguana_updatebundles unknown type.%c\n",req->type); + flag++; + //printf("done %s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n); + if ( req != 0 ) + myfree(req,req->allocsize), req = 0; + } + return(flag); +} + +/*int32_t iguana_needhdrs(struct iguana_info *coin) +{ + if ( coin->longestchain == 0 || coin->blocks.hashblocks < coin->longestchain-coin->chain->bundlesize ) + return(1); + else return(0); +}*/ + +int32_t iguana_reqhdrs(struct iguana_info *coin) +{ + int32_t i,gap,n = 0; struct iguana_bundle *bp; char hashstr[65]; + if ( queue_size(&coin->hdrsQ) == 0 ) //iguana_needhdrs(coin) > 0 && + { + if ( coin->zcount++ > 100 ) + { + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + if ( i == coin->bundlescount-1 && coin->longestchain/coin->chain->bundlesize != i ) + gap = 6; + else gap = 60; + if ( bp->emitfinish != 0 || bp->numhashes >= bp->n || time(NULL) < bp->hdrtime+gap ) + continue; + if ( bp->emitfinish == 0 && time(NULL) > bp->issuetime+gap ) + { + printf("LAG.%ld hdrsi.%d numhashes.%d:%d qsize.%d zcount.%d\n",time(NULL)-bp->hdrtime,i,bp->numhashes,bp->n,queue_size(&coin->hdrsQ),coin->zcount); + if ( bp->issuetime == 0 ) + coin->numpendings++; + char str[65]; + bits256_str(str,bp->hashes[0]); + printf("(%s %d).%d ",str,bp->bundleheight,i); + init_hexbytes_noT(hashstr,bp->hashes[0].bytes,sizeof(bits256)); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1); + n++; + bp->issuetime = (uint32_t)time(NULL); + } + } + } + if ( n > 0 ) + printf("REQ HDRS pending.%d\n",coin->numpendings); + coin->zcount = 0; + } + } else coin->zcount = 0; + return(n); +} + +struct iguana_blockreq { struct queueitem DL; bits256 hash2,*blockhashes; struct iguana_bundle *bp; int32_t n,height,bundlei; }; +int32_t iguana_blockQ(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t priority) +{ + queue_t *Q; char *str; struct iguana_blockreq *req; struct iguana_block *block; + if ( bits256_nonz(hash2) == 0 ) + { + printf("cant queue zerohash bundlei.%d\n",bundlei); + return(-1); + } + if ( priority != 0 || bp == 0 || (block= iguana_blockfind(coin,bp->hashes[bundlei])) == 0 || block->ipbits == 0 ) + { + if ( priority != 0 ) + str = "priorityQ", Q = &coin->priorityQ; + else str = "blocksQ", Q = &coin->blocksQ; + if ( Q != 0 ) + { + req = mycalloc('r',1,sizeof(*req)); + req->hash2 = hash2; + req->bp = bp; + req->bundlei = bundlei; + if ( bp != 0 && bundlei >= 0 && bundlei < bp->n ) + { + bp->issued[bundlei] = milliseconds(); + if ( bp->bundleheight >= 0 ) + req->height = (bp->bundleheight + bundlei); + } + char str[65]; + bits256_str(str,hash2); + if ( 0 && (bundlei % 250) == 0 ) + printf("%s %d %s recv.%d numranked.%d qsize.%d\n",str,req->height,str,coin->blocks.recvblocks,coin->peers.numranked,queue_size(Q)); + queue_enqueue(str,Q,&req->DL,0); + return(1); + } else printf("null Q\n"); + } //else printf("queueblock skip priority.%d bundlei.%d\n",bundlei,priority); + return(0); +} + +int32_t iguana_pollQsPT(struct iguana_info *coin,struct iguana_peer *addr) +{ + uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)]; + char *hashstr=0,hexstr[65]; bits256 hash2; uint32_t now; struct iguana_blockreq *req=0; + struct iguana_bundle *bp,*bestbp = 0; int32_t limit,refbundlei,bundlei,height=-1,datalen,flag = 0; + now = (uint32_t)time(NULL); + if ( addr->pendhdrs < IGUANA_MAXPENDHDRS ) //iguana_needhdrs(coin) != 0 && + { + //printf("%s check hdrsQ\n",addr->ipaddr); + if ( (hashstr= queue_dequeue(&coin->hdrsQ,1)) != 0 ) + { + if ( (datalen= iguana_gethdrs(coin,serialized,coin->chain->gethdrsmsg,hashstr)) > 0 ) + { + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,hash2); + if ( bits256_nonz(hash2) > 0 ) + { + if ( bp == 0 || bp->emitfinish == 0 ) + { + if ( (rand() % 100) == 0 ) + printf("%s request hdr.(%s) ht.%d\n",addr!=0?addr->ipaddr:"local",hashstr,bp!=0?bp->bundleheight:-1); + iguana_send(coin,addr,serialized,datalen); + addr->pendhdrs++; + flag++; + } + } + } else printf("datalen.%d from gethdrs\n",datalen); + free_queueitem(hashstr); + hashstr = 0; + if ( flag != 0 ) + return(flag); + } + } + if ( (limit= addr->recvblocks) > coin->MAXPENDING ) + limit = coin->MAXPENDING; + if ( limit < 1 ) + limit = 1; + if ( coin->bundlescount > 0 && (req= queue_dequeue(&coin->priorityQ,0)) == 0 && addr->pendblocks < limit ) + { + int32_t i,flag,r,diff,j,k,n; double metric,bestmetric = -1.; + for (i=n=0; ibundlescount; i++) + if ( coin->bundles[i] != 0 && coin->bundles[i]->emitfinish == 0 ) + n++; + if ( n >= coin->bundlescount-(coin->bundlescount>>3) || (addr->ipbits % 10) < 2 ) + refbundlei = (addr->ipbits % coin->bundlescount); + else + { + if ( n*2 < coin->bundlescount ) + { + for (i=refbundlei=0; iusock == coin->peers.active[i].usock ) + break; + if ( coin->peers.active[i].usock >= 0 ) + refbundlei++; + } + //printf("half done\n"); + } else refbundlei = ((addr->addrind*100) % coin->bundlescount); + } + for (i=0; ibundlescount; i++) + { + if ( (diff= (i - refbundlei)) < 0 ) + diff = -diff; + if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 ) + { + metric = (1 + diff * ((addr->addrind&1) == 0 ? 1 : diff) * (1. + bp->metric)) / (i + 1); + //printf("%f ",bp->metric); + if ( bestmetric < 0. || metric < bestmetric ) + bestmetric = metric, bestbp = bp; + } + } + if ( bestbp != 0 && bp->emitfinish == 0 ) + { + for (k=0; kbundlescount; k++) + { + i = (bestbp->hdrsi + k) % coin->bundlescount; + if ( (bp= coin->bundles[i]) == 0 || bp->emitfinish > coin->starttime ) + continue; + //printf("%.15f ref.%d addrind.%d bestbp.%d\n",bestmetric,refbundlei,addr->addrind,bp->hdrsi); + for (r=0; rchain->bundlesize && rn; r++) + { + j = (addr->addrind*3 + r) % bp->n; + hash2 = bp->hashes[j]; + if ( bits256_nonz(hash2) == 0 ) + continue; + flag = 0; + if ( (bp->numrecv >= bp->n-5 || bp->requests[j] <= bp->minrequests) && bp->ipbits[j] == 0 && (bp->issued[j] == 0 || now > bp->issued[j]+bp->avetime/1000.) ) + flag = 1; + if ( flag != 0 ) + { + init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2)); + if ( (datalen= iguana_getdata(coin,serialized,MSG_BLOCK,hexstr)) > 0 ) + { + iguana_send(coin,addr,serialized,datalen); + coin->numreqsent++; + addr->pendblocks++; + addr->pendtime = (uint32_t)time(NULL); + if ( 1 && (rand() % 1000) == 0 ) + { + char str[65]; + printf(" %s %s issue.%d %d lag.%d\n",addr->ipaddr,bits256_str(str,hash2),bp->hdrsi,j,now-bp->issued[j]); + } + bp->issued[j] = (uint32_t)time(NULL); + if ( bp->requests[j] < 100 ) + bp->requests[j]++; + return(1); + } else printf("MSG_BLOCK null datalen.%d\n",datalen); + } //else printf("null hash\n"); + } + } + } + } + if ( addr->rank != 1 && req == 0 ) + req = queue_dequeue(&coin->blocksQ,0); + if ( req != 0 ) + { + hash2 = req->hash2; + height = req->height; + if ( 0 && req->bp != 0 && req->bundlei >= 0 && req->bundlei < req->bp->n && req->bundlei < coin->chain->bundlesize && req->bp->recvlens[req->bundlei] != 0 ) + { + //printf("%p[%d] %d\n",req->bp,req->bp!=0?req->bp->bundleheight:-1,req->bundlei); + myfree(req,sizeof(*req)); + } + else if ( req->bp == 0 || (req->bp != 0 && req->bundlei >= 0) )//&& GETBIT(req->bp->recv,req->bundlei) == 0) ) + { + init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2)); + if ( (datalen= iguana_getdata(coin,serialized,MSG_BLOCK,hexstr)) > 0 ) + { + if ( 0 && queue_size(&coin->priorityQ) > 0 ) + printf("%s %s BLOCK.%d:%d bit.%d Q.(%d %d)\n",addr->ipaddr,hexstr,req->bp!=0?req->bp->hdrsi:-1,req->bundlei,req->bp!=0?GETBIT(req->bp->recv,req->bundlei):-1,queue_size(&coin->priorityQ),queue_size(&coin->blocksQ)); + iguana_send(coin,addr,serialized,datalen); + coin->numreqsent++; + addr->pendblocks++; + addr->pendtime = (uint32_t)time(NULL); + if ( (bp= req->bp) != 0 && req->bundlei >= 0 && req->bundlei < bp->n ) + { + if ( bp->requests[req->bundlei] < 100 ) + bp->requests[req->bundlei]++; + bp->issued[req->bundlei] = (uint32_t)time(NULL); + } + flag++; + myfree(req,sizeof(*req)); + return(flag); + } else printf("error constructing request %s.%d\n",hexstr,height); + } + } + return(flag); +} + +int32_t iguana_processrecv(struct iguana_info *coin) // single threaded +{ + int32_t newhwm = 0,h,lflag,i,flag = 0; bits256 hash2; struct iguana_block *next,*block,*prev; + //printf("process bundlesQ\n"); + flag += iguana_processbundlesQ(coin,&newhwm); + flag += iguana_reqhdrs(coin); + lflag = 1; + while ( 1 ) + { + h = coin->blocks.hwmchain.height; + if ( (next= iguana_blockfind(coin,iguana_blockhash(coin,coin->blocks.hwmchain.height+1))) != 0 ) + { + double lag; + _iguana_chainlink(coin,next); + lag = milliseconds() - coin->backstopmillis; + if ( (coin->blocks.hwmchain.height+1 < coin->longestchain && (lag > coin->avetime && coin->backstop != coin->blocks.hwmchain.height+1)) || lag > 10*coin->avetime ) + { + coin->backstop = coin->blocks.hwmchain.height+1; + coin->backstophash2 = next->hash2; + coin->backstopmillis = milliseconds(); + iguana_blockQ(coin,0,coin->blocks.hwmchain.height+1,next->hash2,0); + //char str[65]; printf("MAINCHAIN.%d %s avetime.%.3f lag %.3f\n",coin->blocks.hwmchain.height+1,bits256_str(str,next->hash2),coin->avetime,lag); + } + } + iguana_chainextend(coin,iguana_blockfind(coin,coin->blocks.hwmchain.hash2)); + if ( h == coin->blocks.hwmchain.height ) + break; + } + if ( strcmp(coin->symbol,"BTC") != 0 ) + for (i=coin->blocks.hwmchain.height+1; ilongestchain; i++) + { + if ( (block= iguana_blockfind(coin,iguana_blockhash(coin,i))) != 0 && block->ipbits == 0 ) + { + if ( (prev= iguana_blockfind(coin,block->prev_block)) != 0 && prev->mainchain != 0 ) + { + printf("adjacent %d\n",i); + iguana_blockQ(coin,0,-1,block->hash2,1); + } + } + } + while ( 0)//lflag != 0 ) + { + lflag = 0; + h = coin->blocks.hwmchain.height / coin->chain->bundlesize; + if ( (next= iguana_blockfind(coin,iguana_blockhash(coin,coin->blocks.hwmchain.height+1))) != 0 ) + /*{ + if ( (block= iguana_blockfind(coin,coin->blocks.hwmchain.hash2)) != 0 ) + next = block->hh.next;//, block->mainchain = 1; + } + if ( next != 0 )*/ + { + if ( _iguana_chainlink(coin,next) == 0 ) + //printf("have next\n"); + /*if ( 0 && memcmp(next->prev_block.bytes,coin->blocks.hwmchain.hash2.bytes,sizeof(bits256)) == 0 ) + { + if ( _iguana_chainlink(coin,next) != 0 ) + lflag++; + else printf("chainlink error for %d\n",coin->blocks.hwmchain.height+1);//, getchar(); + } + else if ( 0 )*/ + { + double threshold,lag = milliseconds() - coin->backstopmillis; + threshold = (10 + coin->longestchain - coin->blocksrecv); + if ( threshold < 1 ) + threshold = 1.; + threshold = 1000;//coin->avetime * sqrt(threshold) * .000777; + if ( coin->blocks.hwmchain.height+1 < coin->longestchain && (coin->backstop != coin->blocks.hwmchain.height+1 || lag > threshold) ) + { + coin->backstop = coin->blocks.hwmchain.height+1; + coin->backstophash2 = next->hash2; + coin->backstopmillis = milliseconds(); + iguana_blockQ(coin,0,coin->blocks.hwmchain.height+1,next->hash2,0); + // clear recvlens + //if ( (rand() % 100) == 0 ) + { + char str[65]; printf("MAINCHAIN.%d %s avetime %.3f lag %.3f\n",coin->blocks.hwmchain.height+1,bits256_str(str,next->hash2),coin->avetime,lag); + } + } + else if ( 0 && bits256_nonz(next->prev_block) > 0 ) + printf("next prev cmp error nonz.%d\n",bits256_nonz(next->prev_block)); + } + } + else if ( 0 ) + { + for (i=2; ichain->bundlesize; i++) + { + hash2 = iguana_blockhash(coin,coin->blocks.hwmchain.height+i); + if ( bits256_nonz(hash2) > 0 ) + { + if ( (block= iguana_blockfind(coin,hash2)) != 0 && bits256_nonz(block->prev_block) > 0 ) + { + printf("REVBACKSTOP.%d\n",coin->blocks.hwmchain.height+i-1); + iguana_blockQ(coin,0,-1,block->prev_block,1); + break; + } + } + } + } + if ( h != coin->blocks.hwmchain.height / coin->chain->bundlesize ) + iguana_savehdrs(coin); + } + return(flag); +} + diff --git a/deprecated/iguana_recvbits.c b/deprecated/iguana_recvbits.c new file mode 100755 index 000000000..af317f4c6 --- /dev/null +++ b/deprecated/iguana_recvbits.c @@ -0,0 +1,198 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +int64_t iguana_packetsallocated(struct iguana_info *coin) { return(coin->R.packetsallocated - coin->R.packetsfreed); }; + +uint8_t *iguana_decompress(struct iguana_info *coin,int32_t height,int32_t *datalenp,uint8_t *bits,int32_t numbits,int32_t origdatalen) +{ + uint32_t hdrlen,checklen; + memcpy(&hdrlen,bits,sizeof(hdrlen)); + bits = &bits[sizeof(hdrlen)]; + *datalenp = 0; + if ( (hdrlen & (1 << 31)) != 0 ) + { + hdrlen ^= (1 << 31); + if ( (hdrlen >> 3) == origdatalen ) + { + *datalenp = origdatalen; + return(bits); + } else printf("\n>>>>>>>>> iguana_decompress.%d numbits.%d %d != origlen.%d\n",height,hdrlen,hdrlen>>3,origdatalen), getchar(); + } + else if ( hconv_bitlen(hdrlen) == hconv_bitlen(numbits) ) + { + if ( (checklen= ramcoder_decompress(coin->R.decompressed,sizeof(coin->R.decompressed),bits,hdrlen,bits256_zero)) == origdatalen ) + { + //printf("DECOMPRESSED %d to %d\n",hconv_bitlen(hdrlen),checklen); + *datalenp = origdatalen; + return(coin->R.decompressed); + } + else + { + printf("\n>>>>>>>>> iguana_decompress.%d hdrlen.%d checklen.%d != origdatalen.%d\n",height,hdrlen,checklen,origdatalen); + int32_t j; + for (j=0; j>>>>>>>>> iguana_decompress.%d hdrlen.%d != numbits.%d\n",height,hdrlen,numbits); + int32_t j; + for (j=0; j<=numbits/8; j++) + printf("%02x ",bits[j]); + printf("compressed.%d\n",numbits/8); + getchar(); + } + return(0); +} + +/*struct iguana_msgtx *iguana_validpending(struct iguana_info *coin,struct iguana_pending *ptr,struct iguana_block *space) +{ + struct iguana_block *checkblock; uint8_t *data; int32_t datalen,len; struct iguana_msgtx *tx = 0; + *space = ptr->block; + if ( coin->R.recvblocks == 0 || ptr->block.height >= coin->R.numwaitingbits ) + { + printf("illegal pending height.%d vs %d\n",ptr->block.height,coin->R.numwaitingbits); + return(0); + } + if ( ptr->origdatalen > 0 && ptr->block.height < coin->longestchain && ptr->block.height < coin->blocks.hwmheight ) + { + if ( (checkblock= iguana_block(coin,space,ptr->block.height)) != 0 ) + { + if ( iguana_blockcmp(coin,checkblock,space,1) == 0 ) + { + data = iguana_decompress(coin,ptr->block.height,&datalen,ptr->data,ptr->datalen << 3,ptr->origdatalen); + //printf("parsed.%d vs max.%d height.%d data.%p\n",coin->blocks.parsedblocks,coin->R.numwaitingbits,ptr->block.height,data); + if ( data != 0 && iguana_setdependencies(coin,space) == ptr->block.height ) + { + if ( (tx= iguana_gentxarray(coin,&len,space,data,datalen)) != 0 && len == datalen ) + return(tx); + } else printf("iguana_validpending: error gentx block.%d\n",coin->blocks.parsedblocks); + } else printf("iguana_validpending: error setting vars block.%d\n",ptr->block.height); + if ( tx != 0 ) + iguana_freetx(tx,ptr->numtx); + } else printf("iguana_validpending cant get checkblock %d vs hwmheight.%d\n",ptr->block.height,coin->blocks.hwmheight); + } + return(0); +}*/ + +/*int32_t iguana_processrecv(struct iguana_info *coin) +{ + int32_t height; struct iguana_block space; struct iguana_msgtx *tx = 0; + struct iguana_pending *ptr = 0; int32_t retval = -1; + height = coin->blocks.parsedblocks; + if ( coin->R.recvblocks != 0 && height < coin->R.numwaitingbits ) + { + if ( (ptr= coin->R.recvblocks[height]) != 0 ) + { + //printf("iguana_processrecv height.%d %p\n",height,ptr); + coin->R.recvblocks[height] = 0; + if ( (tx= iguana_validpending(coin,ptr,&space)) != 0 ) + { + retval = iguana_parseblock(coin,&space,tx,ptr->numtx); + if ( space.L.numunspents+space.numvouts != coin->latest.dep.numunspents ) + printf("block->firstvout+block->numvouts (%d+%d) != %d coin->latest.deps.numunspentinds\n",space.L.numunspents,space.numvouts,coin->latest.dep.numunspents), getchar(); + if ( retval < 0 ) + printf("iguana_processrecv: error parsing block.%d tx.%p\n",ptr->block.height,tx); + if ( tx != 0 ) + iguana_freetx(tx,ptr->numtx); + } else printf("error getting pending %d %p\n",height,ptr); + if ( coin->R.maprecvdata == 0 ) + { + coin->R.packetsfreed += ptr->allocsize; + myfree(ptr,ptr->allocsize); + } + } + else if ( time(NULL) > coin->parsetime+1 ) + { + coin->parsetime = (uint32_t)time(NULL); + printf("backstop.%d %s\n",height,bits256_str(iguana_blockhash(coin,height))); + bits256 hash2 = iguana_blockhash(coin,height); + iguana_request_data(coin,coin->peers.ranked[0],&hash2,1,MSG_BLOCK,1); + iguana_waitclear(coin,height); + iguana_waitstart(coin,height); + iguana_updatewaiting(coin,height+1,100); + } + } else printf("processrecv: no recvbits!\n"); + return(retval); +} + +int32_t iguana_recvblock(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *block,struct iguana_msgtx *tx,int32_t numtx,uint8_t *data,int32_t origdatalen) +{ + struct iguana_pending *ptr; int32_t allocsize,checklen,numbits; uint32_t datalen,hdrlen; + if ( coin->R.recvblocks == 0 || coin->R.recvblocks[block->height] != 0 ) + { + coin->sleeptime++; + if ( coin->sleeptime > 10000 ) + coin->sleeptime = 10000; + if ( 0 && addr != coin->peers.localaddr ) + printf("%s recv duplicate at height.%d sleepmillis %.3f\n",addr->ipaddr,block->height,(double)coin->sleeptime/1000.); // add validation/merging + } + else + { + coin->sleeptime *= .995; + if ( coin->sleeptime < 1000 ) + coin->sleeptime = 1000; + // validate block here + datalen = origdatalen; + hdrlen = (1 << 31) | (datalen << 3); + coin->R.srcdatalen += datalen; + if ( 0 && (numbits= ramcoder_compress(coin->R.compressed,sizeof(coin->R.compressed),data,datalen,coin->R.histo,bits256_zero)) > 0 ) + { + memset(coin->R.checkbuf,0,datalen); + if ( (checklen= ramcoder_decompress(coin->R.checkbuf,sizeof(coin->R.checkbuf),coin->R.compressed,numbits,bits256_zero)) == datalen ) + { + if ( memcmp(coin->R.checkbuf,data,datalen) == 0 ) + { + hdrlen = numbits; + data = coin->R.compressed; + printf("height.%d datalen.%d -> numbits.%d %d compression ratio %.3f [%.4f]\n",block->height,datalen,numbits,hconv_bitlen(numbits),(double)datalen/hconv_bitlen(numbits),(double)coin->R.srcdatalen/(coin->R.compressedtotal+hconv_bitlen(numbits)+sizeof(hdrlen))); + datalen = hconv_bitlen(numbits); + } else printf("ramcoder data datalen.%d compare error\n",datalen), getchar(); + } + else printf("ramcoder codec error origdatalen.%d numbits.%d datalen. %d -> %d\n",origdatalen,numbits,datalen,checklen), getchar(); + } //else printf("ramcoder compress error %d -> numbits.%d\n",datalen,numbits), getchar(); + coin->R.compressedtotal += (datalen + sizeof(hdrlen)); + allocsize = (int32_t)(sizeof(*ptr) + datalen + sizeof(hdrlen)); + if ( coin->R.maprecvdata != 0 ) + { + ptr = iguana_tmpalloc(coin,"recv",&coin->R.RSPACE,allocsize); + if ( block->height > coin->R.RSPACE.maxheight ) + coin->R.RSPACE.maxheight = block->height; + ptr->next = (int32_t)((long)iguana_tmpalloc(coin,"recv",&coin->R.RSPACE,0) - (long)ptr); + } + else + { + ptr = mycalloc('P',1,allocsize); + coin->R.packetsallocated += allocsize; + } + ptr->allocsize = allocsize; + ptr->datalen = datalen; + memcpy(ptr->data,&hdrlen,sizeof(hdrlen)); + memcpy(&ptr->data[sizeof(hdrlen)],data,datalen); + ptr->ipbits = addr != 0 ? addr->ipbits : 0; + ptr->block = *block; + ptr->numtx = numtx; + ptr->origdatalen = origdatalen; + if ( (rand() % 1000) == 0 ) + printf("%s recv.%d ptr.%p datalen.%d orig.%d %.3f | parsed.%d hwm.%d longest.%d | %d/%d elapsed %.2f\n",addr != 0 ? addr->ipaddr : "local",block->height,ptr,datalen,origdatalen,(double)origdatalen/datalen,coin->blocks.parsedblocks,coin->blocks.hwmheight,coin->longestchain,iguana_updatewaiting(coin,coin->blocks.parsedblocks,coin->width*10),coin->width*10,(double)(time(NULL)-coin->starttime)/60.); + coin->R.recvblocks[block->height] = ptr; + } + return(0); +}*/ diff --git a/deprecated/obsolete.h b/deprecated/obsolete.h new file mode 100644 index 000000000..6c4c6c146 --- /dev/null +++ b/deprecated/obsolete.h @@ -0,0 +1,10171 @@ +// +// obsolete.h +// pnacl +// +// Created by jimbo laptop on 10/27/15. +// Copyright (c) 2015 jl777. All rights reserved. +// + +#ifndef pnacl_obsolete_h +#define pnacl_obsolete_h + +if ( 0 ) +{ + sleep(3); + char *str,*jsonstr = clonestr("{\"plugin\":\"relay\",\"method\":\"busdata\"}"); uint32_t nonce; + if ( (str= busdata_sync(&nonce,jsonstr,"allnodes",0)) != 0 ) + { + fprintf(stderr,"busdata.(%s)\n",str); + free(str); + } else printf("null return from busdata sync.(%s)\n",jsonstr); + getchar();exit(1); + } + +int32_t nn_stripctrl(int32_t *clenp,uint8_t *ctrl,int32_t ctrlsize,uint8_t *buf) +{ + int32_t clen,offset; + offset = 1; + if ( (clen= buf[0]) > 0 ) + { + if ( clen == 0xfd ) + { + clen = buf[1] | (buf[2] << 8); + offset += 2; + } + printf("nn_stripctrl: clen.%d offset.%d\n",clen,offset); + if ( clen > ctrlsize ) + { + printf("too much control data.%d vs %d, truncate\n",clen,(int32_t)sizeof(ctrl)); + memcpy(ctrl,&buf[offset],ctrlsize); + *clenp = ctrlsize; + errno = MSG_CTRUNC; + } + else + { + memcpy(ctrl,&buf[offset],clen); + *clenp = clen; + } + //printf("copied (%d).%d bytes of control from nbytes.%d\n",clen,offset-clen,(int32_t)nbytes); + } else *clenp = 0; + offset += clen; + return(offset); +} + +static bool nc_conn_ip_active(struct net_child_info *nci,const unsigned char *ip) +{ + unsigned int i; + for (i = 0; i < nci->conns->len; i++) + { + struct nc_conn *conn; + conn = parr_idx(nci->conns, i); + if (!memcmp(conn->peer.addr.ip, ip, 16)) + return true; + } + return false; +} + +static bool nc_conn_group_active(struct net_child_info *nci,const struct peer *peer) +{ + // FIXME + return false; + unsigned int group_len = peer->group_len; + unsigned int i; + for (i = 0; i < nci->conns->len; i++) + { + struct nc_conn *conn; + conn = parr_idx(nci->conns, i); + if ((group_len == conn->peer.group_len) && !memcmp(peer->group, conn->peer.group, group_len)) + return true; + } + return false; +} + +static struct nc_conn *nc_conn_new(const struct peer *peer) +{ + struct nc_conn *conn; + conn = calloc(1, sizeof(*conn)); + if (!conn) + return NULL; + conn->fd = -1; + peer_copy(&conn->peer, peer); + bn_address_str(conn->addr_str, sizeof(conn->addr_str), conn->peer.addr.ip); + return conn; +} + +static void nc_conn_kill(struct net_child_info *nci,struct nc_conn *conn) +{ + assert(conn->dead == false); + conn->dead = true; + event_base_loopbreak(conn->nci->eb); +} + +static void nc_conn_free(struct net_child_info *nci,struct nc_conn *conn) +{ + if (!conn) + return; + if (conn->write_q) + { + clist *tmp = conn->write_q; + while (tmp) + { + struct buffer *buf; + buf = tmp->data; + tmp = tmp->next; + free(buf->p); + free(buf); + } + clist_free(conn->write_q); + } + if (conn->ev) + { + event_del(conn->ev); + event_free(conn->ev); + } + if (conn->write_ev) + { + event_del(conn->write_ev); + event_free(conn->write_ev); + } + if (conn->fd >= 0) + close(conn->fd); + free(conn->msg.data); + memset(conn, 0, sizeof(*conn)); + free(conn); +} + +static bool nc_conn_start(struct net_child_info *nci,struct nc_conn *conn) +{ + char errpfx[64]; + /* create socket */ + printf("start connection.(%s)\n",conn->addr_str); + conn->ipv4 = is_ipv4_mapped(conn->peer.addr.ip); + conn->fd = socket(conn->ipv4 ? AF_INET : AF_INET6,SOCK_STREAM,IPPROTO_TCP); + if ( conn->fd < 0 ) + { + sprintf(errpfx, "socket %s", conn->addr_str); + perror(errpfx); + return false; + } + /* set non-blocking */ + int flags = fcntl(conn->fd,F_GETFL,0); + if ( (flags < 0) || (fcntl(conn->fd,F_SETFL,flags | O_NONBLOCK) < 0) ) + { + sprintf(errpfx, "socket fcntl %s", conn->addr_str); + perror(errpfx); + return false; + } + struct sockaddr *saddr; + struct sockaddr_in6 saddr6; + struct sockaddr_in saddr4; + socklen_t saddr_len; + /* fill out connect(2) address */ + if (conn->ipv4) + { + memset(&saddr4, 0, sizeof(saddr4)); + saddr4.sin_family = AF_INET; + memcpy(&saddr4.sin_addr.s_addr,&conn->peer.addr.ip[12],4); + saddr4.sin_port = htons(conn->peer.addr.port); + saddr = (struct sockaddr *) &saddr4; + saddr_len = sizeof(saddr4); + } + else + { + memset(&saddr6, 0, sizeof(saddr6)); + saddr6.sin6_family = AF_INET6; + memcpy(&saddr6.sin6_addr.s6_addr,&conn->peer.addr.ip[0], 16); + saddr6.sin6_port = htons(conn->peer.addr.port); + saddr = (struct sockaddr *) &saddr6; + saddr_len = sizeof(saddr6); + } + // initiate TCP connection + if ( connect(conn->fd,saddr,saddr_len) < 0 ) + { + if ( errno != EINPROGRESS ) + { + sprintf(errpfx, "socket connect %s", conn->addr_str); + perror(errpfx); + return false; + } + } + return true; +} + +static bool nc_conn_got_header(struct net_child_info *nci,struct nc_conn *conn) +{ + parse_message_hdr(&conn->msg.hdr, conn->hdrbuf); + unsigned int data_len = conn->msg.hdr.data_len; + if (data_len > (16 * 1024 * 1024)) + { + free(conn->msg.data); + conn->msg.data = NULL; + return false; + } + conn->msg.data = malloc(data_len); + /* switch to read-body state */ + conn->msg_p = conn->msg.data; + conn->expected = data_len; + conn->reading_hdr = false; + return true; +} + +static bool nc_conn_got_msg(struct net_child_info *nci,struct nc_conn *conn) +{ + if (!message_valid(&conn->msg)) { + fprintf(nci->plog, "llnet: %s invalid message\n",conn->addr_str); + return false; + } + if (!nc_conn_message(nci,conn)) + return false; + free(conn->msg.data); + conn->msg.data = NULL; + /* switch to read-header state */ + conn->msg_p = conn->hdrbuf; + conn->expected = P2P_HDR_SZ; + conn->reading_hdr = true; + return true; +} + +static void nc_conn_read_evt(int fd, short events, void *priv) +{ + struct nc_conn *conn = priv; + struct net_child_info *nci = conn->nci; + ssize_t rrc = read(fd, conn->msg_p, conn->expected); + if (rrc <= 0) + { + if (rrc < 0) + fprintf(nci->plog, "llnet: %s read: %s\n",conn->addr_str,strerror(errno)); + else fprintf(nci->plog, "llnet: %s read EOF\n", conn->addr_str); + goto err_out; + } + conn->msg_p += rrc; + conn->expected -= rrc; + /* execute our state machine at most twice */ + unsigned int i; + for (i = 0; i < 2; i++) + { + if (conn->expected == 0) + { + if (conn->reading_hdr) + { + if (!nc_conn_got_header(nci,conn)) + goto err_out; + } + else + { + if (!nc_conn_got_msg(nci,conn)) + goto err_out; + } + } + } + return; +err_out: + nc_conn_kill(nci,conn); +} + +static cstring *nc_version_build(struct net_child_info *nci) +{ + struct msg_version mv; + msg_version_init(&mv); + mv.nVersion = PROTO_VERSION; + mv.nServices = nci->blocks_fp != 0 ? NODE_NETWORK : 0; + mv.nTime = (int64_t)time(NULL); + mv.nonce = nci->instance_nonce; + sprintf(mv.strSubVer,"/nano/"); + mv.nStartingHeight = nci->db.best_chain ? nci->db.best_chain->height : 0; + cstring *rs = ser_msg_version(&mv); + msg_version_free(&mv); + return rs; +} + +static bool nc_conn_read_enable(struct net_child_info *nci,struct nc_conn *conn) +{ + if (conn->ev) + return true; + conn->ev = event_new(conn->nci->eb, conn->fd, EV_READ | EV_PERSIST,(void *)nc_conn_read_evt, conn); + if (!conn->ev) + return false; + if (event_add(conn->ev, NULL) != 0) + { + event_free(conn->ev); + conn->ev = NULL; + return false; + } + return true; +} + +static bool nc_conn_read_disable(struct net_child_info *nci,struct nc_conn *conn) +{ + if (!conn->ev) + return true; + event_del(conn->ev); + event_free(conn->ev); + conn->ev = NULL; + return true; +} + +static bool nc_conn_write_enable(struct net_child_info *nci,struct nc_conn *conn) +{ + if (conn->write_ev) + return true; + conn->write_ev = event_new(conn->nci->eb, conn->fd,EV_WRITE | EV_PERSIST,(void *)nc_conn_write_evt, conn); + if (!conn->write_ev) + return false; + if (event_add(conn->write_ev, NULL) != 0) + { + event_free(conn->write_ev); + conn->write_ev = NULL; + return false; + } + return true; +} + +static bool nc_conn_write_disable(struct net_child_info *nci,struct nc_conn *conn) +{ + if (!conn->write_ev) + return true; + event_del(conn->write_ev); + event_free(conn->write_ev); + conn->write_ev = NULL; + return true; +} + +static void nc_conn_evt_connected(int fd, short events, void *priv) +{ + struct nc_conn *conn = priv; + struct net_child_info *nci = conn->nci; + if ((events & EV_WRITE) == 0) { + fprintf(nci->plog, "net: %s connection timeout\n", conn->addr_str); + goto err_out; + } + int err = 0; + socklen_t len = sizeof(err); + /* check success of connect(2) */ + if ((getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) || (err != 0)) + { + fprintf(nci->plog, "net: connect %s failed: %s\n",conn->addr_str, strerror(err)); + goto err_out; + } + if (nci->debugging) + fprintf(nci->plog, "net: connected to %s\n", conn->addr_str); + conn->connected = true; + /* clear event used for watching connect(2) */ + event_free(conn->ev); + conn->ev = NULL; + /* build and send "version" message */ + cstring *msg_data = nc_version_build(nci); + bool rc = nc_conn_send(nci,conn, "version", msg_data->str, msg_data->len); + cstr_free(msg_data, true); + if (!rc) + { + fprintf(nci->plog, "net: %s !conn_send\n", conn->addr_str); + goto err_out; + } + /* switch to read-header state */ + conn->msg_p = conn->hdrbuf; + conn->expected = P2P_HDR_SZ; + conn->reading_hdr = true; + if (!nc_conn_read_enable(nci,conn)) + { + fprintf(nci->plog, "net: %s read not enabled\n", conn->addr_str); + goto err_out; + } + return; +err_out: + nc_conn_kill(nci,conn); +} + +static void nc_conns_gc(struct net_child_info *nci, bool free_all) +{ + clist *dead = NULL; + unsigned int n_gc = 0; + /* build list of dead connections */ + unsigned int i; + for (i = 0; i < nci->conns->len; i++) + { + struct nc_conn *conn = parr_idx(nci->conns, i); + if (free_all || conn->dead) + dead = clist_prepend(dead, conn); + } + /* remove and free dead connections */ + clist *tmp = dead; + while (tmp) + { + struct nc_conn *conn = tmp->data; + tmp = tmp->next; + + parr_remove(nci->conns, conn); + nc_conn_free(nci,conn); + n_gc++; + } + clist_free(dead); + if (nci->debugging) + fprintf(nci->plog, "net: gc'd %u connections\n", n_gc); +} + +static void nc_conns_open(struct net_child_info *nci) +{ + if (nci->debugging) + fprintf(nci->plog, "net: open connections (have %zu, want %zu more)\n",nci->conns->len,NC_MAX_CONN - nci->conns->len); + printf("nc_conns_open\n"); + while ((bp_hashtab_size(nci->peers->map_addr) > 0) && (nci->conns->len < NC_MAX_CONN)) + { + // delete peer from front of address list. it will be re-added before writing peer file, if successful + struct peer *peer = peerman_pop(nci->peers); + struct nc_conn *conn = nc_conn_new(peer); + conn->nci = nci; + peer_free(peer); + free(peer); + fprintf(stderr, "net: connecting to [%s]\n",conn->addr_str); + if (nc_conn_ip_active(nci, conn->peer.addr.ip)) // are we already connected to this IP? + { + fprintf(nci->plog, "net: already connected to %s\n",conn->addr_str); + goto err_loop; + } + if (nc_conn_group_active(nci, &conn->peer)) // are we already connected to this network group? + { + fprintf(nci->plog, "net: already grouped to %s\n",conn->addr_str); + goto err_loop; + } + if (!nc_conn_start(nci,conn)) // initiate non-blocking connect(2) + { + fprintf(nci->plog, "net: failed to start connection to %s\n",conn->addr_str); + goto err_loop; + } + // add to our list of monitored event sources + conn->ev = event_new(nci->eb, conn->fd, EV_WRITE,(void *)nc_conn_evt_connected, conn); + if ( !conn->ev ) + { + fprintf(nci->plog, "net: event_new failed on %s\n",conn->addr_str); + goto err_loop; + } + struct timeval timeout = { net_conn_timeout, }; + if (event_add(conn->ev, &timeout) != 0) + { + fprintf(nci->plog, "net: event_add failed on %s\n",conn->addr_str); + goto err_loop; + } + parr_add(nci->conns, conn); // add to our list of active connections + continue; + err_loop: + nc_conn_kill(nci,conn); + } +} + +static void nc_conns_process(struct net_child_info *nci) +{ + nc_conns_gc(nci, false); + nc_conns_open(nci); +} + +static bool parse_kvstr(const char *s, char **key, char **value) +{ + char *eql; + eql = strchr(s, '='); + if (eql) + { + uint32_t keylen = (uint32_t)((long)eql - (long)s); + *key = strndup(s, keylen); + *value = strdup(s + keylen + 1); + } + else + { + *key = strdup(s); + *value = strdup(""); + } + /* blank keys forbidden; blank values permitted */ + if (!strlen(*key)) + { + free(*key); + free(*value); + *key = NULL; + *value = NULL; + return false; + } + return true; +} + +static bool read_config_file(struct net_child_info *nci,const char *cfg_fn) +{ + FILE *cfg = fopen(cfg_fn, "r"); + if (!cfg) + return false; + bool rc = false; + char line[1024]; + while (fgets(line, sizeof(line), cfg) != NULL) + { + char *key, *value; + if (line[0] == '#') + continue; + while (line[0] && (isspace(line[strlen(line) - 1]))) + line[strlen(line) - 1] = 0; + if (!parse_kvstr(line, &key, &value)) + continue; + + bp_hashtab_put(nci->settings, key, value); + } + rc = ferror(cfg) == 0; + fclose(cfg); + return rc; +} + +static bool do_setting(struct net_child_info *nci,const char *arg) +{ + char *key, *value; + if (!parse_kvstr(arg, &key, &value)) + return false; + bp_hashtab_put(nci->settings, key, value); + // trigger special setting-specific behaviors + if (!strcmp(key, "debug")) + nci->debugging = true; + else if (!strcmp(key, "config") || !strcmp(key, "c")) + return read_config_file(nci,value); + return true; +} + +static bool preload_settings(struct net_child_info *nci) +{ + unsigned int i; + /* preload static settings */ + for (i = 0; i < ARRAY_SIZE(const_settings); i++) + if (!do_setting(nci,const_settings[i])) + return false; + return true; +} +/*unsigned int arg; + for (arg = 1; arg < argc; arg++) + { + const char *argstr = argv[arg]; + if ( do_setting(nci,argstr) == 0 ) + return 1; + }*/ +/* + * properly capture TERM and other signals + */ + +static void nc_conn_write_evt(int fd, short events, void *priv) +{ + struct nc_conn *conn = priv; + + struct iovec *iov = NULL; + unsigned int iov_len = 0; + struct net_child_info *nci = conn->nci; + /* build list of outgoing data buffers */ + nc_conn_build_iov(conn->write_q, conn->write_partial, &iov, &iov_len); + printf("send data to network\n"); + /* send data to network */ + ssize_t wrc = mywritev(conn->fd, iov, iov_len); + free(iov); + if (wrc < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK) + goto err_out; + return; + } + /* handle partially and fully completed buffers */ + nc_conn_written(conn, wrc); + /* thaw read, if write fully drained */ + if (!conn->write_q) + { + nc_conn_write_disable(nci,conn); + nc_conn_read_enable(nci,conn); + } + return; +err_out: + nc_conn_kill(nci,conn); +} + +static void nc_conn_build_iov(clist *write_q, unsigned int partial,struct iovec **iov_, unsigned int *iov_len_) +{ + *iov_ = NULL; + *iov_len_ = 0; + unsigned int i, iov_len = (uint32_t)clist_length(write_q); + struct iovec *iov = calloc(iov_len, sizeof(struct iovec)); + clist *tmp = write_q; + i = 0; + while (tmp) + { + struct buffer *buf = tmp->data; + + iov[i].iov_base = buf->p; + iov[i].iov_len = buf->len; + if (i == 0) + { + iov[0].iov_base += partial; + iov[0].iov_len -= partial; + } + tmp = tmp->next; + i++; + } + *iov_ = iov; + *iov_len_ = iov_len; +} + +static void nc_conn_written(struct nc_conn *conn, size_t bytes) +{ + while (bytes > 0) + { + clist *tmp; + struct buffer *buf; + uint32_t left; + tmp = conn->write_q; + buf = tmp->data; + left = (uint32_t)(buf->len - conn->write_partial); + /* buffer fully written; free */ + if (bytes >= left) + { + free(buf->p); + free(buf); + conn->write_partial = 0; + conn->write_q = clist_delete(tmp, tmp); + bytes -= left; + } + /* buffer partially written; store state */ + else + { + conn->write_partial += bytes; + break; + } + } +} + +ssize_t mywritev(int fildes, const struct iovec *iov, int iovcnt) +{ + int i; + int32_t bytes_written = 0; + for (i = 0; i < iovcnt; i++) + { + int len = (int32_t)send(fildes,iov[i].iov_base,iov[i].iov_len,0); + if (len < 0) + { + //DWORD err = GetLastError(); + //errno = ewin_to_posix_error(err); + bytes_written = -1; + break; + } + bytes_written += len; + } + return bytes_written; +} + +static bool nc_msg_version(struct net_child_info *nci,struct nc_conn *conn) +{ + if (conn->seen_version) + return false; + conn->seen_version = true; + struct const_buffer buf = { conn->msg.data, conn->msg.hdr.data_len }; + struct msg_version mv; + bool rc = false; + msg_version_init(&mv); + if (!deser_msg_version(&mv, &buf)) + goto out; + if (nci->debugging) + { + char fromstr[64], tostr[64]; + bn_address_str(fromstr, sizeof(fromstr), mv.addrFrom.ip); + bn_address_str(tostr, sizeof(tostr), mv.addrTo.ip); + fprintf(nci->plog, "net: %s version(%u, 0x%llx, %lld, To:%s, From:%s, %s, %u)\n", + conn->addr_str, + mv.nVersion, + (unsigned long long) mv.nServices, + (long long) mv.nTime, + tostr, + fromstr, + mv.strSubVer, + mv.nStartingHeight); + } + if (!(mv.nServices & NODE_NETWORK)) /* require NODE_NETWORK */ + goto out; + if (mv.nonce == nci->instance_nonce) /* connected to ourselves? */ + goto out; + conn->protover = (mv.nVersion < PROTO_VERSION) ? mv.nVersion : PROTO_VERSION; + /* acknowledge version receipt */ + if (!nc_conn_send(nci,conn, "verack", NULL, 0)) + goto out; + rc = true; + out: + msg_version_free(&mv); + return rc; +} + +static bool nc_conn_send(struct net_child_info *nci,struct nc_conn *conn, const char *command,const void *data, size_t data_len) +{ + /* build wire message */ + cstring *msg = message_str(nci->chain->netmagic, command, data, (uint32_t)data_len); + if (!msg) + return false; + /* buffer now owns message data */ + struct buffer *buf = calloc(1, sizeof(struct buffer)); + buf->p = msg->str; + buf->len = msg->len; + cstr_free(msg, false); + /* if write q exists, write_evt will handle output */ + if (conn->write_q) + { + conn->write_q = clist_append(conn->write_q, buf); + return true; + } + /* attempt optimistic write */ + ssize_t wrc = write(conn->fd, buf->p, buf->len); + if (wrc < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK) + { + free(buf->p); + free(buf); + return false; + } + conn->write_q = clist_append(conn->write_q, buf); + goto out_wrstart; + } + /* message fully sent */ + if (wrc == buf->len) + { + free(buf->p); + free(buf); + return true; + } + /* message partially sent; pause read; poll for writable */ + conn->write_q = clist_append(conn->write_q, buf); + conn->write_partial = (uint32_t)wrc; +out_wrstart: + nc_conn_read_disable(nci,conn); + nc_conn_write_enable(nci,conn); + return true; +} + + +static void init_daemon(struct net_child_info *nci,char *coin) +{ + strcpy(nci->coin,coin); + //init_log(nci); + //init_blkdb(nci); + //printf("utxo\n"); + //bp_utxo_set_init(&nci->uset); + //printf("init_blocks\n"); + //init_blocks(nci); + //printf("init_orphans\n"); + init_orphans(nci); + //readprep_blocks_file(nci); + init_nci(nci); +} + +int32_t iguana_verack(struct net_child_info *nci,struct bp_address *addr) +{ + struct msg_getblocks gb; int32_t rc,numsent; time_t now,cutoff; cstring *s,*msg; + if ( addr->seen_verack ) + { + printf("addr->seen_verack %d\n",addr->seen_verack); + return(-1); + } + addr->seen_verack = 1; + addr->nTime = (uint32_t)time(NULL); + addr->n_ok++; + //peerman_add(conn->nci->peers, &conn->peer,true); + if ( addr->protover >= CADDR_TIME_VERSION ) + { + msg = message_str(nci->chain->netmagic,"getaddr",NULL,0); + if ( (numsent= (int32_t)send(addr->usock,msg->str,msg->len,0)) != msg->len ) + { + cstr_free(msg,true); + return -1; + } + cstr_free(msg,true); + } else printf("protover.%d vs %d CADDR_TIME_VERSION\n",addr->protover,CADDR_TIME_VERSION); + rc = 0; + now = time(NULL); + cutoff = now - (24 * 60 * 60); + if ( nci->last_getblocks < cutoff ) + { + msg_getblocks_init(&gb); + blkdb_locator(&nci->db,NULL,&gb.locator); + s = ser_msg_getblocks(&gb); + oldsend_getblocks(nci); + msg = message_str(nci->chain->netmagic,"getblocks",s->str,(uint32_t)s->len); + cstr_free(s,true); + msg_getblocks_free(&gb); + nci->last_getblocks = now; + } + return(rc); +} + + +static void init_peers(struct net_child_info *nci) +{ + struct peer_manager *peers = 0; + peers = peerman_read(setting(nci,"peers")); + printf("init_peers.%p\n",peers); + if (!peers) + { + PostMessage( "net: initializing empty peer list\n"); + peers = peerman_seed(nci->chain->default_port,1,1);//setting(nci,"dns") != 0 ? true : false,nci->debugging); + if ( !peerman_write(nci->chain,peers,setting(nci,"peers"),nci->debugging) ) + { + PostMessage( "net: failed to write peer list\n"); + exit(1); + } + } + char *addnode = setting(nci,"addnode"); + if (addnode) + peerman_addstr(nci->chain->default_port,peers,addnode,nci->debugging); + peerman_sort(peers); + if (nci->debugging) + PostMessage( "net: have %u/%zu peers\n",bp_hashtab_size(peers->map_addr),clist_length(peers->addrlist)); + nci->peers = peers; +} + +static void init_nci(struct net_child_info *nci) +{ + nci->read_fd = -1; + nci->write_fd = -1; + //nci->blocks_fd = -1; + //init_peers(nci); + nci->conns = parr_new(NC_MAX_CONN, NULL); + //nci->eb = event_base_new(); + nci->daemon_running = true; +} + +void oldsend_getblocks(struct net_child_info *nci) +{ + struct msg_getblocks gb; + msg_getblocks_init(&gb); + blkdb_locator(&nci->db, NULL, &gb.locator); + cstring *s = ser_msg_getblocks(&gb); + printf("oldsend_getblocks\n"); + nc_conn_send(nci,0, "getblocks", s->str, s->len); + cstr_free(s, true); + msg_getblocks_free(&gb); +} + + +static bool nc_msg_inv(struct net_child_info *nci,struct nc_conn *conn) +{ + struct const_buffer buf = { conn->msg.data, conn->msg.hdr.data_len }; + struct msg_vinv mv, mv_out; + bool rc = false; + msg_vinv_init(&mv); + msg_vinv_init(&mv_out); + if (!deser_msg_vinv(&mv, &buf)) + goto out; + if (nci->debugging && mv.invs && mv.invs->len == 1) + { + struct bp_inv *inv = parr_idx(mv.invs, 0); + char hexstr[BU256_STRSZ]; + bu256_hex(hexstr, &inv->hash); + char typestr[32]; + switch (inv->type) + { + case MSG_TX: strcpy(typestr, "tx"); break; + case MSG_BLOCK: strcpy(typestr, "block"); break; + default: sprintf(typestr, "unknown 0x%x", inv->type); break; + } + PostMessage( "net: %s inv %s %s\n",conn->addr_str, typestr, hexstr); + } + else if (nci->debugging && mv.invs) + PostMessage( "net: %s inv (%zu sz)\n",conn->addr_str, mv.invs->len); + if (!mv.invs || !mv.invs->len) + goto out_ok; + /* scan incoming inv's for interesting material */ + unsigned int i; + for (i = 0; i < mv.invs->len; i++) + { + struct bp_inv *inv = parr_idx(mv.invs, i); + switch (inv->type) + { + case MSG_BLOCK: + if (!blkdb_lookup(&nci->db, &inv->hash) && !have_orphan(nci,&inv->hash)) + msg_vinv_push(&mv_out, MSG_BLOCK, &inv->hash); + break; + case MSG_TX: + default: + break; + } + } + /* send getdata, if they have anything we want */ + if (mv_out.invs && mv_out.invs->len) + { + cstring *s = ser_msg_vinv(&mv_out); + rc = nc_conn_send(nci,conn, "getdata", s->str, s->len); + cstr_free(s, true); + } +out_ok: + rc = true; + out: + msg_vinv_free(&mv); + msg_vinv_free(&mv_out); + return rc; +} + +ssize_t fwritev(FILE *fp,const struct iovec *iov,int iovcnt) +{ + int i; int32_t bytes_written = 0; + fprintf(stderr,"fwritev.%d from %ld: ",iovcnt,(long)ftell(fp)); + for (i = 0; i < iovcnt; i++) + { + int len = (int32_t)fwrite(iov[i].iov_base,1,iov[i].iov_len,fp); + if ( len != iov[i].iov_len ) + { + printf("len.%d != %d iov[i].iov_len\n",len,(int32_t)iov[i].iov_len); + //DWORD err = GetLastError(); + //errno = ewin_to_posix_error(err); + bytes_written = -1; + break; + } + bytes_written += len; + } + fprintf(stderr,"%d bytes\n",bytes_written); + return bytes_written; +} + +static bool nc_msg_verack(struct net_child_info *nci,struct nc_conn *conn) +{ + if (conn->seen_verack) + return false; + conn->seen_verack = true; + if (nci->debugging) + PostMessage( "net: %s verack\n",conn->addr_str); + /* + * When a connection attempt is made, the peer is deleted + * from the peer list. When we successfully connect, + * the peer is re-added. Thus, peers are immediately + * forgotten if they fail, on the first try. + */ + conn->peer.last_ok = time(NULL); + conn->peer.n_ok++; + conn->peer.addr.nTime = (uint32_t) conn->peer.last_ok; + peerman_add(conn->nci->peers, &conn->peer, true); + /* request peer addresses */ + if ((conn->protover >= CADDR_TIME_VERSION) && (!nc_conn_send(nci,conn, "getaddr", NULL, 0))) + return false; + /* request blocks */ + bool rc = true; + time_t now = time(NULL); + time_t cutoff = now - (24 * 60 * 60); + if (conn->nci->last_getblocks < cutoff) + { + struct msg_getblocks gb; + msg_getblocks_init(&gb); + blkdb_locator(&nci->db, NULL, &gb.locator); + cstring *s = ser_msg_getblocks(&gb); + rc = nc_conn_send(nci,conn, "getblocks", s->str, s->len); + cstr_free(s, true); + msg_getblocks_free(&gb); + conn->nci->last_getblocks = now; + } + return rc; +} +static bool nc_conn_send(struct net_child_info *nci,struct nc_conn *conn, const char *command,const void *data, size_t data_len) +{ + int32_t i; + cstring *msg = message_str(nci->chain->netmagic, command, data, (uint32_t)data_len); + for (i=0; ilen; i++) + printf("%02x ",msg->str[i] & 0xff); + printf("nc_conn_send cmd.(%s) len.%d\n",command,(int32_t)msg->len); + return(0); +} + +static bool nc_msg_addr(struct net_child_info *nci,struct nc_conn *conn) +{ + struct const_buffer buf = { conn->msg.data, conn->msg.hdr.data_len }; + struct msg_addr ma; + bool rc = false; + msg_addr_init(&ma); + if (!deser_msg_addr(conn->protover, &ma, &buf)) + goto out; + unsigned int i; + time_t cutoff = time(NULL) - (7 * 24 * 60 * 60); + if (nci->debugging) + { + unsigned int old = 0; + for (i = 0; i < ma.addrs->len; i++) + { + struct bp_address *addr = parr_idx(ma.addrs, i); + if (addr->nTime < cutoff) + old++; + } + PostMessage( "net: %s addr(%zu addresses, %u old)\n",conn->addr_str, ma.addrs->len, old); + } + /* ignore ancient addresses */ + if (conn->protover < CADDR_TIME_VERSION) + goto out_ok; + /* feed addresses to peer manager */ + for (i = 0; i < ma.addrs->len; i++) { + struct bp_address *addr = parr_idx(ma.addrs, i); + if (addr->nTime > cutoff) + peerman_add_addr(conn->nci->peers, addr, false); + } +out_ok: + rc = true; + out: + msg_addr_free(&ma); + return rc; +} +/*libevent stubs + #define EV_READ 1 + #define EV_WRITE 2 + #define EV_PERSIST 4 + struct event *event_new(struct event_base *evbase,int32_t fd,int32_t flags,void *funcp,void *conn) { return(0); } + void event_base_loopbreak(struct event_base *evbase) {} + void event_base_dispatch(struct event_base *evbase) {} + void event_base_free(struct event_base *evbase) {} + + struct event_base *event_base_new() { return(0); } + void event_del(struct event *ev) {} + void event_free(struct event *ev) {} + int32_t event_add(struct event *ev,struct timeval *tval) { return(-1); } + // end stubs */ + +enum { NC_MAX_CONN = 8, }; + +//static unsigned int net_conn_timeout = 11; +/* + static void nc_conn_kill(struct net_child_info *nci,struct nc_conn *conn); + static bool nc_conn_read_enable(struct net_child_info *nci,struct nc_conn *conn); + static bool nc_conn_read_disable(struct net_child_info *nci,struct nc_conn *conn); + static bool nc_conn_write_enable(struct net_child_info *nci,struct nc_conn *conn); + static bool nc_conn_write_disable(struct net_child_info *nci,struct nc_conn *conn);*/ + +static bool nc_conn_message(struct net_child_info *nci,struct nc_conn *conn) +{ + char *command = conn->msg.hdr.command; + /* verify correct network */ + printf("got message.(%s)\n",command); + if (memcmp(conn->msg.hdr.netmagic, nci->chain->netmagic, 4)) + { + PostMessage( "net: %s invalid network\n",conn->addr_str); + return false; + } + /* incoming message: version */ + //if ( !strncmp(command,"version",12) ) + // return nc_msg_version(nci,conn); + /* "version" must be first message */ + if (!conn->seen_version) + { + PostMessage( "net: %s 'version' not first\n",conn->addr_str); + return false; + } + /* incoming message: verack */ + if (!strncmp(command,"verack",12)) + return nc_msg_verack(nci,conn); + /* "verack" must be second message */ + if (!conn->seen_verack) + { + PostMessage( "net: %s 'verack' not second\n",conn->addr_str); + return false; + } + /* incoming message: addr */ + if (!strncmp(command, "addr", 12)) + return nc_msg_addr(nci,conn); + /* incoming message: inv */ + else if (!strncmp(command, "inv", 12)) + return nc_msg_inv(nci,conn); + /* incoming message: block */ + else if (!strncmp(command, "block", 12)) + return nc_msg_block(nci,conn); + if (nci->debugging) + PostMessage( "net: %s unknown message %s\n",conn->addr_str,command); + /* ignore unknown messages */ + return true; +} + +static void init_log(struct net_child_info *nci) +{ + char *log_fn = setting(nci,"log"); + if (!log_fn || !strcmp(log_fn, "-")) + nci->plog = stdout; + else { + nci->plog = fopen(log_fn, "a"); + if (!nci->plog) { + perror(log_fn); + exit(1); + } + } + setvbuf(nci->plog, NULL, _IONBF, BUFSIZ); +} + +static void init_blkdb(struct net_child_info *nci) +{ + if (!blkdb_init(&nci->db, nci->chain->netmagic, &nci->chain_genesis)) + { + PostMessage( "blkdb init failed\n"); + exit(1); + } + + char *blkdb_fn = 0;//setting(nci,"blkdb"); + if (!blkdb_fn) + return; + if ((access(blkdb_fn, F_OK) == 0) && !blkdb_read(nci->chain->hastimestamp,&nci->db, blkdb_fn)) + { + PostMessage( "blkdb read failed\n"); + exit(1); + } + if ( (nci->db.fp= fopen(blkdb_fn,"rb+")) == 0 ) + nci->db.fp= fopen(blkdb_fn,"wb+"); + if ( nci->db.fp == 0 ) + { + PostMessage( "blkdb file open failed: %s\n", strerror(errno)); + exit(1); + } + //nci->db.fd = open(blkdb_fn,O_WRONLY | O_APPEND | O_CREAT | O_LARGEFILE, 0666); + //if (nci->db.fd < 0) { + // PostMessage( "blkdb file open failed: %s\n", strerror(errno)); + // exit(1); + //} +} + +static void init_blocks(struct net_child_info *nci) +{ + char blocks_fn[512]; + sprintf(blocks_fn,"%s.blocks",nci->coin); + if ( (nci->blocks_fp= fopen(blocks_fn,"rb+")) == 0 )// O_RDWR | O_CREAT | O_LARGEFILE, 0666); + nci->blocks_fp = fopen(blocks_fn,"wb+"); + if ( nci->blocks_fp == 0 ) + { + PostMessage( "blocks file open failed: %s\n", strerror(errno)); + exit(1); + } + fseek(nci->blocks_fp,0,SEEK_END); + off64_t flen = ftell(nci->blocks_fp); + printf("opened.(%s) flen.%llu\n",blocks_fn,(long long)flen); + if ( flen == (off64_t)-1 ) + { + PostMessage( "blocks file lseek64 failed: %s\n", strerror(errno)); + exit(1); + } + if ( flen == 0 ) + init_block0(nci); +} + +static void shutdown_daemon(struct net_child_info *nci) +{ + if ( nci->blocks_fp != 0 ) + fclose(nci->blocks_fp); + bool rc = peerman_write(nci->chain,nci->peers,setting(nci,"peers"),nci->debugging); + PostMessage( "net: %s %u/%zu peers\n",rc ? "wrote" : "failed to write",bp_hashtab_size(nci->peers->map_addr),clist_length(nci->peers->addrlist)); + if ( nci->plog != stdout && nci->plog != stderr ) + { + fclose(nci->plog); + nci->plog = NULL; + } + if ( setting(nci,"free") ) + { + shutdown_nci(nci); + bp_hashtab_unref(nci->orphans); + bp_hashtab_unref(nci->settings); + blkdb_free(&nci->db); + bp_utxo_set_free(&nci->uset); + } +} + + +static bool nc_msg_block(struct net_child_info *nci,struct nc_conn *conn) +{ + struct const_buffer buf = { conn->msg.data, conn->msg.hdr.data_len }; + struct iovec iov[2]; char hexstr[BU256_STRSZ]; struct bp_block block; bool rc = false; + bp_block_init(&block); + if ( !deser_bp_block(nci->chain->hastimestamp,&block,&buf) ) + goto out; + bp_block_calc_sha256(&block); + bu256_hex(hexstr,&block.sha256); + if ( nci->debugging ) + PostMessage("net: %s block %s\n",conn->addr_str,hexstr); + if ( !bp_block_valid(&block) ) + { + PostMessage("net: %s invalid block %s\n",conn->addr_str,hexstr); + goto out; + } + if ( blkdb_lookup(&nci->db,&block.sha256) || have_orphan(nci,&block.sha256) ) + goto out_ok; + iov[0].iov_base = &conn->msg.hdr; + iov[0].iov_len = sizeof(conn->msg.hdr); + iov[1].iov_base = (void *)buf.p; + iov[1].iov_len = conn->msg.hdr.data_len; + printf("hdr.%d len.%d\n",(int32_t)sizeof(conn->msg.hdr),(int32_t)buf.len); + size_t total_write = iov[0].iov_len + iov[1].iov_len; + //off64_t fpos64 = lseek64(nci->blocks_fd, 0, SEEK_CUR); + //fseek(nci->blocks_fp, 0, SEEK_CUR); + off64_t fpos64 = ftell(nci->blocks_fp); + if ( fpos64 == (off64_t)-1 ) + { + PostMessage( "blocks: lseek64 failed %s\n", + strerror(errno)); + goto out; + } + //errno = 0; + ssize_t bwritten = fwritev(nci->blocks_fp,iov,ARRAY_SIZE(iov)); + if ( bwritten != total_write ) + { + PostMessage( "blocks: write failed %s\n",strerror(errno)); + goto out; + } + if ( !process_block(nci,&block,fpos64) ) + { + PostMessage( "blocks: process-block failed\n"); + goto out; + } +out_ok: + rc = true; + out: + bp_block_free(&block); + return rc; +} + +static bool spend_tx(bool script_verf,struct bp_utxo_set *uset, const struct bp_tx *tx,unsigned int tx_idx, unsigned int height) +{ + bool is_coinbase = (tx_idx == 0); + struct bp_utxo *coin; + int64_t total_in = 0, total_out = 0; + unsigned int i; + /* verify and spend this transaction's inputs */ + if (!is_coinbase) + { + for (i = 0; i < tx->vin->len; i++) + { + struct bp_txin *txin; + struct bp_txout *txout; + txin = parr_idx(tx->vin, i); + coin = bp_utxo_lookup(uset, &txin->prevout.hash); + if (!coin || !coin->vout) + return false; + if (coin->is_coinbase && ((coin->height + COINBASE_MATURITY) > height)) + return false; + txout = NULL; + if (txin->prevout.n >= coin->vout->len) + return false; + txout = parr_idx(coin->vout, txin->prevout.n); + total_in += txout->nValue; + if (script_verf && !bp_verify_sig(coin, tx, i, /* SCRIPT_VERIFY_P2SH */ 0, 0)) + return false; + if (!bp_utxo_spend(uset, &txin->prevout)) + return false; + } + } + for (i = 0; i < tx->vout->len; i++) + { + struct bp_txout *txout; + txout = parr_idx(tx->vout, i); + total_out += txout->nValue; + } + if (!is_coinbase) + { + if (total_out > total_in) + return false; + } + /* copy-and-convert a tx into a UTXO */ + coin = calloc(1, sizeof(*coin)); + bp_utxo_init(coin); + if (!bp_utxo_from_tx(coin, tx, is_coinbase, height)) + return false; + /* add unspent outputs to set */ + bp_utxo_set_add(uset, coin); + return true; +} + +static bool spend_block(struct net_child_info *nci,const struct bp_block *block,unsigned int height) +{ + struct bp_tx *tx; + unsigned int i; + for (i = 0; i < block->vtx->len; i++) + { + tx = parr_idx(block->vtx, i); + if (!spend_tx(nci->script_verf,&nci->uset, tx, i, height)) + { + char hexstr[BU256_STRSZ]; + bu256_hex(hexstr, &tx->sha256); + PostMessage( "brd: spent_block tx fail %s\n", hexstr); + return false; + } + } + return true; +} + +static bool process_block(struct net_child_info *nci,const struct bp_block *block,int64_t fpos) +{ + struct blkdb_reorg reorg; struct blkinfo *bi = bi_new(); + fprintf(stderr,"process_block sha256 %llx\n",*(long long *)&block->sha256); + bu256_copy(&bi->hash, &block->sha256); + bp_block_copy_hdr(&bi->hdr, block); + bi->n_file = 0; + bi->n_pos = fpos; + if ( blkdb_add(&nci->db,bi,&reorg) == 0 ) + { + PostMessage( "brd: blkdb add fail fpos.%ld\n",(long)fpos); + goto err_out; + } + /* FIXME: support reorg */ + assert(reorg.conn == 1); + assert(reorg.disconn == 0); + if ( bu256_equal(&nci->db.best_chain->hash,&bi->hdr.sha256) ) // if best chain, mark TX's as spent + { + if ( spend_block(nci,block,bi->height) == 0 ) + { + char hexstr[BU256_STRSZ]; + bu256_hex(hexstr, &bi->hdr.sha256); + PostMessage("brd: block spend fail %u %s\n",bi->height, hexstr); + // FIXME: bad record is now in blkdb + goto err_out; + } + } + return true; +err_out: + bi_free(bi); + return false; +} + +static bool read_block_msg(struct net_child_info *nci,struct p2p_message *msg, int64_t fpos) +{ + struct const_buffer buf = { msg->data, msg->hdr.data_len }; + struct bp_block block; bool rc = false; + // unknown records are invalid + printf("read_block_msg\n"); + if ( strncmp(msg->hdr.command,"block",sizeof(msg->hdr.command)) ) + { + printf("invalid cmd.(%s) != block\n",msg->hdr.command); + return false; + } + bp_block_init(&block); + if ( deser_bp_block(nci->chain->hastimestamp,&block,&buf) == 0 ) + { + PostMessage( "brd: block deser fail\n"); + goto out; + } + bp_block_calc_sha256(&block); + if (!bp_block_valid(&block)) + { + PostMessage( "brd: block not valid\n"); + goto out; + } + printf("call process_block\n"); + rc = process_block(nci,&block,fpos); + out: + bp_block_free(&block); + return rc; +} + +static void read_blocks(struct net_child_info *nci) +{ + //int fd = nci->blocks_fd; + int32_t n = 0; FILE *fp = nci->blocks_fp; + struct p2p_message msg = {}; + bool read_ok = true; + int64_t fpos = 0; + printf("read_blocks from pos %ld\n",(long)ftell(fp)); + while ( fread_message(fp,&msg,&read_ok) ) + { + printf("iter.%d netmagic.%x\n",n++,*(int32_t *)nci->chain->netmagic); + if ( memcmp(msg.hdr.netmagic,nci->chain->netmagic,4) ) + { + PostMessage("blocks file: invalid network magic\n"); + exit(1); + } + //strcpy(msg.hdr.command,"block"); + if ( !read_block_msg(nci,&msg,fpos) ) + exit(1); + fpos += P2P_HDR_SZ; + fpos += msg.hdr.data_len; + fpos = ftell(fp); + } + printf("read_blocks finished loop\n"); + if ( !read_ok ) + { + PostMessage("blocks file: read failed\n"); + exit(1); + } + free(msg.data); +} + +static void readprep_blocks_file(struct net_child_info *nci) +{ + // if no blk index, but blocks are present, read and index all block data (several gigabytes) + if ( nci->blocks_fp != 0 ) + { + rewind(nci->blocks_fp); + if ( nci->db.fp == 0 ) + read_blocks(nci); + else + { + printf("seek to end\n"); + // TODO: verify that blocks file offsets are present in blkdb + //if ( lseek(nci->blocks_fd, 0, SEEK_END) == (off_t)-1 ) + if ( fseek(nci->blocks_fp,0,SEEK_END) == (off_t)-1 ) + { + PostMessage( "blocks file: seek failed: %s\n",strerror(errno)); + exit(1); + } + } + } +} + +static void init_orphans(struct net_child_info *nci) +{ + nci->orphans = bp_hashtab_new_ext(bu256_hash, bu256_equal_,(bp_freefunc) bu256_free, (bp_freefunc) buffer_free); +} + +static bool have_orphan(struct net_child_info *nci,const bu256_t *v) +{ + return bp_hashtab_get(nci->orphans, v); +} + +bool add_orphan(struct net_child_info *nci,const bu256_t *hash_in, struct const_buffer *buf_in) +{ + if (have_orphan(nci,hash_in)) + return false; + bu256_t *hash = bu256_new(hash_in); + if (!hash) { + PostMessage( "OOM\n"); + return false; + } + struct buffer *buf = buffer_copy(buf_in->p, buf_in->len); + if (!buf) { + bu256_free(hash); + PostMessage( "OOM\n"); + return false; + } + bp_hashtab_put(nci->orphans, hash, buf); + return true; +} + +struct nc_conn +{ + bool dead; + int fd; + struct peer peer; + char addr_str[64]; + bool ipv4; + bool connected; + struct event *ev; + struct net_child_info *nci; + struct event *write_ev; + clist *write_q; /* of struct buffer */ + unsigned int write_partial; + struct p2p_message msg; + void *msg_p; + unsigned int expected; + bool reading_hdr; + unsigned char hdrbuf[P2P_HDR_SZ]; + bool seen_version; + bool seen_verack; + uint32_t protover; +}; + +static bool process_block(struct net_child_info *nci,const struct bp_block *block, int64_t fpos); +static bool have_orphan(struct net_child_info *nci,const bu256_t *v); +static bool add_orphan(struct net_child_info *nci,const bu256_t *hash_in, struct const_buffer *buf_in); + +/*struct bp_hashtab *settings; + //const struct chain_info *chain = NULL; + bu256_t chain_genesis; + uint64_t instance_nonce; + bool debugging = false; + FILE *plog = NULL; + + static const char *const_settings[] = + { + "net.connect.timeout=11", + "addnode=127.0.0.1", + "peers=brd.peers", + "dns=1", + //"blkdb=brd.blkdb", + "blocks=brd.blocks", + //"log=brd.log", + };*/ + +static void init_block0(struct net_child_info *nci) +{ + if ( nci->blocks_fp != 0 ) + { + cstring *msg0 = message_str(nci->chain->netmagic,"block",nci->chain->genesis_hashdata,(int32_t)sizeof(nci->chain->genesis_hashdata)); + ssize_t bwritten = fwrite(msg0->str,1,msg0->len,nci->blocks_fp); + if ( bwritten != msg0->len ) + { + PostMessage( "blocks write0 failed: %s\n", strerror(errno)); + exit(1); + } + cstr_free(msg0,true); + off64_t fpos64 = ftell(nci->blocks_fp); + if ( fpos64 == (off64_t)-1 ) + { + PostMessage( "blocks lseek0 failed: %s\n", strerror(errno)); + exit(1); + } + PostMessage("blocks: genesis block written\n"); + } +} + +static void shutdown_nci(struct net_child_info *nci) +{ + peerman_free(nci->peers); + //nc_conns_gc(nci, true); + assert(nci->conns->len == 0); + //parr_free(nci->conns, true); + //event_base_free(nci->eb); +}int32_t iguana_send(struct iguana_info *coin,void *_conn,uint8_t *serialized,char *cmd,int32_t len) +{ + return(nc_conn_send(coin,_conn,cmd,&serialized[sizeof(struct iguana_msghdr)],len)); + int32_t numsent; struct nc_conn *conn = _conn; + len = iguana_sethdr((void *)serialized,coin->chain->netmagic,cmd,&serialized[sizeof(struct iguana_msghdr)],len); + if ( (numsent= (int32_t)send(conn->addr.usock,serialized,len,0)) < 0 ) + { + printf("%s: numsent.%d vs len.%d errno.%d usock.%d\n",cmd,numsent,len,errno,conn->addr.usock); + if (errno != EAGAIN && errno != EWOULDBLOCK) + { + printf("bad errno.%d\n",errno); + return(-errno); + } + if ( 0 ) + { + /*struct buffer *buf = calloc(1, sizeof(struct buffer)); + buf->p = malloc(len), memcpy(buf->p,serialized,len); + buf->len = len; + conn->write_q = clist_append(conn->write_q,buf); + nc_conn_read_disable(coin,conn); + nc_conn_write_enable(coin,conn);*/ + } + } + else if ( numsent < len ) + { + if ( 0 ) + { + /*conn->write_q = clist_append(conn->write_q, buf); + conn->write_partial = (uint32_t)numsent; + buf->p = malloc(len), memcpy(buf->p,serialized,len); + buf->len = len; + conn->write_q = clist_append(conn->write_q,buf); + nc_conn_read_disable(coin,conn); + nc_conn_write_enable(coin,conn);*/ + } + //int32_t i; + //for (i=0; i 0 && bestheight > oldbestheight ) +{ + prevhash = iguana_prevblockhash(blocks,new_best); + if ( (prev= iguana_findblock(&space,blocks,prevhash)) != 0 && prev->height > 0 ) + { + new_best = prevhash; + bestheight = prev->height; + reorg_info->conn++; + printf("connect.%d: newbest.%s oldheight.%d newheight.%d\n",reorg_info->conn,bits256_str(new_best),oldbestheight,bestheight); + } else break; +} +// unlikely case: old best chain has greater height +while ( oldbestheight > 0 && bestheight > 0 && oldbestheight > bestheight ) +{ + old_best = iguana_prevblockhash(blocks,old_best); + if ( (prev= iguana_findblock(&space,blocks,prevhash)) != 0 && prev->height > 0 ) + { + oldbestheight = prev->height; + reorg_info->disconn++; + printf("unlikely case: disconn.%d %s\n",reorg_info->disconn,bits256_str(old_best)); + } else break; +} +// height matches, but we are still walking parallel chains +while ( oldbestheight > 0 && bestheight > 0 && memcmp(old_best.bytes,new_best.bytes,sizeof(old_best)) != 0 ) +{ + new_best = iguana_prevblockhash(blocks,new_best); + bestheight = iguana_height(blocks,new_best); + reorg_info->conn++; + old_best = iguana_prevblockhash(blocks,old_best); + oldbestheight = iguana_height(blocks,old_best); + reorg_info->disconn++; + printf("parallel case\n"); +} + +/*bits256 iguana_PoW(struct iguana_info *coin,int32_t height) + { + int32_t h; bits256 sum; struct iguana_block *ptr,space; + if ( height > 0 ) + { + h = (height / 1000); + sum = coin->blocks.PoW[h]; + h *= 1000; + while ( ++h <= height ) + { + if ( (ptr= iguana_block(&space,coin,h)) != 0 ) + sum = bits256_add(sum,bits256_from_compact(ptr->bits)); + else + { + printf("error getting block[%u]\n",h); + break; + } + } + } + else + { + iguana_block(&space,coin,0); + sum = bits256_from_compact(space.bits); + } + return(sum); + }*/ +/*BIGNUM cur_work,test; bits256 x,sum; char ystr[512],xstr[512]; + BN_init(&cur_work); + BN_init(&test); + u256_from_compact(&cur_work,0x1d00ffff); + PoW_str(ystr,sizeof(ystr),&cur_work); + printf("y.(%s) ",ystr); + BN_add(&test,&cur_work,&cur_work); + PoW_str(ystr,sizeof(ystr),&test); + printf("sum.(%s) ",ystr); + PoW_conv(&test,0x1d00ffff); + PoW_str(xstr,sizeof(xstr),&test); + x = bits256_from_compact(0x1d00ffff); + sum = bits256_add(x,x); + printf("xstr.(%s) x.(%s) sum.(%s)\n",xstr,bits256_str(x),bits256_lstr(sum)); + getchar();*/ +void u256_from_compact(BIGNUM *vo,uint32_t c); + +int32_t PoW_conv(BIGNUM *PoW,uint32_t nBits) +{ + BN_init(PoW); + u256_from_compact(PoW,nBits); + return(0); +} + +int32_t PoW_add(BIGNUM *sum,BIGNUM *a,BIGNUM *b) +{ + if ( BN_add(sum,a,b) == 0 ) + return(-1); + return(0); +} + +void PoW_free(BIGNUM *a) { BN_clear_free(a); } + +int32_t PoW_cmp(BIGNUM *test,BIGNUM *hwm) { return(BN_cmp(test,hwm)); } +void PoW_str(char *str,int32_t maxlen,BIGNUM *v); + +/*static void nc_conns_gc(struct parr *conns,bool free_all) + { + struct nc_conn *conn; clist *dead = NULL; uint32_t i,n_gc = 0; + // build list of dead connections + for (i=0; ilen; i++) + { + conn = parr_idx(conns,i); + if ( free_all || conn->dead ) + dead = clist_prepend(dead,conn); + } + // remove and free dead connections + clist *tmp = dead; + while ( tmp ) + { + struct nc_conn *conn = tmp->data; + tmp = tmp->next; + parr_remove(conns,conn); + nc_conn_free(conn); + n_gc++; + } + clist_free(dead); + fprintf(stderr,"net: gc'd %u connections\n",n_gc); + }*/ + +/*static struct nc_conn *nc_conn_new(const struct peer *peer) + { + struct nc_conn *conn; + conn = calloc(1, sizeof(*conn)); + if (!conn) + return NULL; + conn->fd = -1; + peer_copy(&conn->peer, peer); + bn_address_str(conn->addr_str, sizeof(conn->addr_str), conn->peer.addr.ip); + return conn; + }*/ + +/*static bool nc_conn_start(struct iguana_info *coin,struct nc_conn *conn) + { + char errpfx[64]; + printf("start connection.(%s)\n",conn->addr_str); + conn->ipv4 = 1;//is_ipv4_mapped(conn->peer.addr.ip); + conn->fd = socket(conn->ipv4 ? AF_INET : AF_INET6,SOCK_STREAM,IPPROTO_TCP); + if ( conn->fd < 0 ) + { + sprintf(errpfx, "socket %s", conn->addr_str); + perror(errpfx); + return false; + } + int flags = fcntl(conn->fd,F_GETFL,0); + if ( (flags < 0) || (fcntl(conn->fd,F_SETFL,flags | O_NONBLOCK) < 0) ) + { + sprintf(errpfx, "socket fcntl %s", conn->addr_str); + perror(errpfx); + return false; + } + struct sockaddr *saddr; + struct sockaddr_in6 saddr6; + struct sockaddr_in saddr4; + socklen_t saddr_len; + if (conn->ipv4) + { + memset(&saddr4, 0, sizeof(saddr4)); + saddr4.sin_family = AF_INET; + memcpy(&saddr4.sin_addr.s_addr,&conn->peer.addr.ip[12],4); + saddr4.sin_port = htons(conn->peer.addr.port); + saddr = (struct sockaddr *) &saddr4; + saddr_len = sizeof(saddr4); + } + else + { + memset(&saddr6, 0, sizeof(saddr6)); + saddr6.sin6_family = AF_INET6; + memcpy(&saddr6.sin6_addr.s6_addr,&conn->peer.addr.ip[0], 16); + saddr6.sin6_port = htons(conn->peer.addr.port); + saddr = (struct sockaddr *) &saddr6; + saddr_len = sizeof(saddr6); + } + // initiate TCP connection + if ( connect(conn->fd,saddr,saddr_len) < 0 ) + { + if ( errno != EINPROGRESS ) + { + sprintf(errpfx, "socket connect %s", conn->addr_str); + perror(errpfx); + return false; + } + } + return true; + }*/ +int32_t rc,lbsock=-1,timeout=1000,priority=1; uint8_t magic[4] = { 0xf9, 0xbe, 0xb4, 0xd9 }; +if ( 0 ) +{ + int32_t testsock,testsock2; char buf[512]; + testsock2 = nn_socket(AF_SP,NN_CRYPTO); + testsock = nn_socket(AF_SP,NN_CRYPTO); + rc = nn_setsockopt(testsock,NN_SOL_SOCKET,NN_CRYPTO_MAGIC,magic,4); + rc = nn_setsockopt(testsock2,NN_SOL_SOCKET,NN_CRYPTO_MAGIC,magic,4); + nn_bind(testsock2,"crypto://127.0.0.1:9999"); + nn_connect(testsock,"crypto://127.0.0.1:9999"); + nn_send(testsock,"hello",6,0); + nn_recv(testsock2,buf,sizeof(buf),0); + printf("bind side got.(%s)\n",buf); + nn_send(testsock2,"gotmsg",7,0); + nn_recv(testsock,buf,sizeof(buf),0); + printf("connect side got.(%s)\n",buf); + + getchar(); +} +//if ( (lbsock= nn_socket(AF_SP,NN_CRYPTO)) >= 0 ) +{ + rc = nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_CRYPTO_MAGIC,magic,4); + printf("rc.%d from NN_CRYPTO_MAGIC\n",rc); + rc = nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_SNDPRIO,&priority,sizeof(priority)); + rc = nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); + rc = nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); + printf("rc.%d from NN_SNDPRIO\n",rc); + //if ( nn_connect(lbsock,"crypto://127.0.0.1:8883") >= 0 ) + //if ( nn_connect(lbsock,"tcp://127.0.0.1:50447") >= 0 ) + { + iguana_main("bitcoin","BTC",1); //NODE_NETWORK); + getchar(); + } +} + +/*struct iguana_kvitem *iguana_kvitemptr(struct iguanakv *kv,void *value) + { + struct iguana_kvitem *item = 0; + if ( kv != 0 && value != 0 ) + { + value = (void *)((long)value - (kv)->keysize); + item = (void *)((long)value - ((long)item->keyvalue - (long)item)); + } + return(item); + }*/ + +void iguana_savepeers(struct iguana_info *coin) +{ + uint32_t peerind,itemind; struct iguana_peer *addr,space; char ipaddr[64]; + for (peerind=1; peerind<=coin->latest.maxpeers; peerind++) + { + if ( iguana_RWmmap(0,&space,coin,coin->peers,peerind) == 0 ) + { + strcpy(ipaddr,space.ipaddr); + //printf("peerind.%d -> (%s)\n",peerind,ipaddr); + if ( (addr= iguana_kvread(coin,&space,&itemind,coin->peers,space.A.ip)) != 0 ) + { + if ( peerind == itemind ) + { + if ( iguana_RWmmap(1,addr,coin,coin->peers,peerind) != 0 ) + printf("error RWmap.1 peerind.%d -> (%s)\n",peerind,ipaddr); + } else printf("mismatched peerind.%d vs itemind.%d for (%s)\n",peerind,itemind,ipaddr); + } + } else printf("error reading peerind.%d\n",peerind); + } + iguana_syncmap(&coin->peers->state.M,0); +} +if ( strcmp("peers",kv->name) == 0 ) +{ + struct iguana_peer *addr = (void *)sp->space; + addr->seen_verack = 0; + checkip[0] = 0; + addr->peerind = itemind; + if ( addr->ipaddr[0] != 0 ) + { + ipbits = (uint32_t)calc_ipbits(addr->ipaddr); + expand_ipbits(checkip,ipbits); + } + if ( addr->ipaddr[0] == 0 || strcmp(checkip,addr->ipaddr) != 0 ) + { + printf("bad record.(%s) vs (%s) %d vs %d\n",checkip,addr->ipaddr,addr->peerind,itemind); + i = keysize; + } + else printf("add n.%d skipped.%d (%s) vs (%s).%x i.%d\n",n,skipped,addr->ipaddr,checkip,ipbits,i); + } + +void *iguana_kvsavepeer(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) +{ + FILE *fp = (FILE *)args; struct iguana_peer *addr; uint32_t data[4],ipbits,flag = 0; + if ( args != 0 && (addr= value) != 0 ) + { + printf("%p %s iterarg.%d verack.%d killed.%d\n",addr,addr->ipaddr,kv->iterarg,addr->seen_verack,addr->dead); + if ( kv->iterarg == 0 && addr->seen_verack != 0 ) + flag = 1; + else if ( kv->iterarg == 1 && addr->dead != 0 ) + flag = 1; + else if ( kv->iterarg == 2 && (addr->seen_verack == 0 && addr->dead == 0) ) + flag = 1; + if ( flag != 0 ) + { + ipbits = (uint32_t)calc_ipbits(addr->ipaddr); + data[0] = ipbits; + data[1] = addr->lastcontact; + data[2] = addr->nStartingHeight; + data[3] = addr->pingtime; + if ( fwrite(data,1,sizeof(data),fp) != sizeof(data) ) + { + printf("Error saving key.[%x]\n",ipbits); + return(key); + } + } + } + return(0); +} + +long iguana_savepeers(struct iguana_info *coin) +{ + FILE *fp; long retval = -1; int32_t iter; char fname[512],*str = "good"; + for (iter=0; iter<3; iter++) + { + coin->peers->iterarg = iter; + sprintf(fname,"%s.%s",coin->peers->name,str); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + if ( iguana_kviterate(coin,coin->peers,(uint64_t)fp,iguana_kvsavepeer) == 0 ) + { + printf("save %ld to HDD\n",ftell(fp)); + retval = ftell(fp); + } + else printf("error saving item at %ld\n",ftell(fp)); + fclose(fp); + } else printf("error creating(%s)\n",fname); + if ( iter == 0 ) + str = "errpeer"; + else str = "newpeer"; + } + coin->updatedpeers = 0; + return(retval); +} + +/*void iguana_open_connection(struct iguana_info *coin,char *ipaddr) + { + int32_t i,n; struct iguana_peer addrs[10]; + memset(addrs,0,sizeof(addrs)); + n = iguana_connect(addrs,(int32_t)(sizeof(addrs)/sizeof(*addrs)),ipaddr,coin->chain->default_port); + if ( n > 0 ) + { + for (i=0; iipaddr,coin->numpeers,coin->numrelayers); + iguana_kvwrite(coin,coin->peers,addr->A.ip,addr,sizeof(*addr),(uint32_t *)&addr->peerind); + coin->updatedpeers++; +} + + +void *iguana_kvpurgepeer(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) +{ + struct iguana_peer *addr; int32_t lag; + if ( args != 0 && (addr= value) != 0 && addr->sendtime != 0 ) + { + if ( (lag= (kv->iteruarg - addr->sendtime)) == 0 ) + lag = 1; + if ( kv->iterarg == 0 || lag > kv->iterarg ) + { + kv->iterarg = lag; + strcpy((char *)args,addr->ipaddr); + } + } + return(0); +} + +void *iguana_loop(void *_coin) +{ + struct iguana_info *coin = _coin; + while ( 1 ) + { + /*if ( 1 && (ipaddr= queue_dequeue(&coin->newpeersQ,1)) != 0 ) + { + iguana_open_connection(coin,ipaddr); + printf("check newpeer.(%s)\n",ipaddr); + free_queueitem(ipaddr); + }*/ + sleep(1); + } + return(0); +} + +void shutdown_daemon(struct iguana_info *coin) +{ + if ( coin->blocks.db != 0 ) + { + //iguana_kvsave(coin->blocks.db); + iguana_kvfree(coin,coin->blocks.db); + } +} + +/*if ( coin->numpeers > 16 ) + { + coin->peers->iteruarg = (uint32_t)time(NULL); + coin->peers->iterarg = 0; + if ( iguana_kviterate(coin,coin->peers,(uint64_t)ipaddr,iguana_kvpurgepeer) == 0 ) + printf("lag.%d (%s) peer purged\n",coin->peers->iterarg,ipaddr); + else printf("error: lag.%d (%s) peer purged\n",coin->peers->iterarg,ipaddr); + } + */ +//for (iter=0; iter<2; iter++) +{ + fpos = n = m = fixed = skipped = 0; + if ( (fp= fopen(sp->fname,"rb")) != 0 ) + { + fseek(fp,fpos,SEEK_SET); + while ( fread(sp->space,2,valuesize,fp) == valuesize ) + { + //printf("m.%d\n",m); + itemind = m++; + for (i=0; ispace)[kv->keyoffset + i] != 0 ) + break; + if ( i != keysize ) + { + if ( itemind != n ) + { + fseek(fp,fpos,SEEK_SET); + memset((void *)((long)sp->space + valuesize),0,valuesize); + fwrite((void *)((long)sp->space + valuesize),1,valuesize,fp); + fseek(fp,(long)n * valuesize,SEEK_SET); + fwrite(sp->space,1,valuesize,fp); + fixed++; + //printf("itemind.%d vs n.%d skipped.%d\n",itemind,n,skipped); + itemind = n; + } + if ( iter == 1 ) + { + iguana_kvwrite(coin,kv,(void *)((long)sp->space + kv->keyoffset),sp->space,valuesize,&itemind); + } + n++; + } skipped++; + fseek(fp,(long)m * valuesize,SEEK_SET); + } + printf("iter.%d fixed.%d %s added %d items, skipped.%d keysize.%d keyoffset.%d valuesize.%d\n",iter,fixed,kv->name,n,skipped,kv->keysize,kv->keyoffset,kv->valuesize); + fclose(fp); + //getchar(); + } +} +void iguana_killpeer(struct iguana_info *coin,struct iguana_peer *addr) +{ + if ( addr->seen_verack != 0 ) + { + addr->dead = 1; + addr->seen_verack = 0; + coin->numrelayers -= addr->relayflag; + coin->numpeers--; + printf("KILL PEER.(%s) peerind.%u total.%d relayers.%d\n",addr->ipaddr,addr->peerind,coin->numpeers,coin->numrelayers); + iguana_kvwrite(coin,coin->peers,addr->A.ip,addr,sizeof(*addr),(uint32_t *)&addr->peerind); + coin->updatedpeers++; + } +} + +/*for (j=0; jnumreferrals; j++) + if ( ipbits == addr->referrals[j] ) + break; + if ( j == addr->numreferrals ) + { + if ( addr->numreferrals < sizeof(addr->referrals)/sizeof(*addr->referrals) ) + addr->referrals[addr->numreferrals++] = ipbits; + iguana_possible_peer(coin,ipaddr); + }*/ + +int32_t iguana_blockchain(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgblock *blk,uint8_t *serialized,bits256 hash2,int32_t checkpointi) +{ + struct iguana_blocks *blocks; double PoW; struct iguana_block *prev,*check,space; int32_t i,height,firsttxidind; + blocks = &coin->blocks; + /*if ( (check= iguana_findblock(coin,&space,hash2)) != 0 ) + { + if ( checkpointi >= 0 && check->height == coin->checktip_heights[checkpointi]+1 ) + { + coin->checkpointips[checkpointi] = hash2; + coin->checktip_heights[checkpointi]++; + coin->rawblocks++; + printf("iguana_blockchain: duplicate block height.%d checkpointi.%d tipheight.%d rawblocks.%d\n",check->height,checkpointi,coin->checktip_heights[checkpointi],coin->rawblocks); + } + return(check->height); + } + for (i=0; ichain->numcheckpoints; i++) + { + if ( memcmp(coin->chain->checkpoints_data[i].bytes,hash2.bytes,sizeof(hash2)) == 0 ) + { + height = coin->chain->checkblocks[i]; + coin->checktip_heights[i] = height; + printf("checkpointi.%d height.%d rawblocks.%d\n",i,height,coin->rawblocks); + iguana_addblock(coin,hash2,blk,height,-1,0.); // add to block map, orphans and all + return(0); + } + if ( memcmp(coin->chain->checkpoints_data[i].bytes,blk->H.prev_block.bytes,sizeof(blk->H.prev_block)) == 0 ) + { + height = coin->chain->checkblocks[i] + 1; + coin->checktip_heights[i] = height; + coin->checkpointips[i] = hash2; + coin->rawblocks++; + printf("checkpointi.%d height.%d <- (%s) rawblocks.%d\n",i,height,bits256_str(hash2),coin->rawblocks); + iguana_addblock(coin,hash2,blk,height,-1,0.); // add to block map, orphans and all + return(0); + } + if ( memcmp(coin->checkpointips[i].bytes,blk->H.prev_block.bytes,sizeof(blk->H.prev_block)) == 0 ) + { + height = ++coin->checktip_heights[i]; + coin->checkpointips[i] = hash2; + coin->rawblocks++; + printf("checkpointi.%d height.%d rawblocks.%d\n",i,height,coin->rawblocks); + iguana_addblock(coin,hash2,blk,height,-1,0.); // add to block map, orphans and all + return(0); + } + }*/ + if ( (prev= iguana_findblock(coin,&space,blk->H.prev_block)) == 0 ) + { + fprintf(stderr,"iguana_blockchain no prev block.(%s)\n",bits256_str(blk->H.prev_block)); + return(-1); + } + else + { + height = prev->height + 1; + PoW = (PoW_from_compact(blk->H.bits) + prev->PoW); + firsttxidind = (prev->firsttxidind + prev->txn_count); + if ( PoW <= coin->blocks.best_chain_work ) + height = 0; + } + //printf("NEWHT.%d (%s) PoW %.15f prev.%d prevPoW %.15f\n",height,bits256_str(hash2),blk->PoW,prev->height,prev->PoW); + iguana_addblock(coin,addr,hash2,blk,height,firsttxidind,PoW); // add to block map, orphans and all + if ( height == 0 ) + { + printf("%s chain not best\n",bits256_str(hash2)); + return(-1); + } + if ( memcmp(blocks->best_chain.bytes,blk->H.prev_block.bytes,sizeof(blocks->best_chain)) != 0 ) + { + printf("prev.(%s) doesnt connect to previous bestchain\n",bits256_str(blk->H.prev_block)); + printf("mark as orphans from old bestchain.(%s) till it connects to mainchain\n",bits256_str(blocks->best_chain)); + getchar(); + } + return(height); +} + +int32_t iguana_possible_peer(struct iguana_info *coin,char *ipaddr) +{ + struct iguana_peer *space,*addr,addrs[8]; uint32_t i,n,peerind = (uint32_t)-1; + if ( strncmp("0.0.0",ipaddr,5) != 0 && strcmp("0.0.255.255",ipaddr) != 0 && strcmp("1.0.0.0",ipaddr) != 0 ) + { + memset(addrs,0,sizeof(addrs)); + n = iguana_connect(addrs,(int32_t)(sizeof(addrs)/sizeof(*addrs)),ipaddr,coin->chain->default_port,0); + if ( n > 0 ) + { + for (i=0; i<1; i++) // n is almost always 1 + { + strcpy(addrs[i].coinstr,coin->name); + space = calloc(1,sizeof(*space)); + peerind = -1; + //portable_mutex_lock(&coin->netmutex); + if ( (addr= iguana_kvread(coin,space,(uint32_t *)&peerind,coin->peers,ipaddr)) == 0 ) + memcpy(space,&addrs[i],sizeof(*space)); + else if ( addr->usock >= 0 || addr->pending != 0 ) + break; + peerind = -1; + if ( iguana_kvwrite(coin,coin->peers,ipaddr,space,sizeof(*space),(uint32_t *)&peerind) != 0 ) + { + //portable_mutex_unlock(&coin->netmutex); + //printf("%p %s ADD PEER.(%s) peerind.%u max.%u total.%d relayers.%d numkeys.%d\n",&addrs[i],space->coinstr,space->ipaddr,peerind,coin->latest.maxpeers,coin->numpeers,coin->numrelayers,coin->peers->numkeys); + space->coin = coin; + if ( coin->numthreads < 3 || (coin->numthreads < IGUANA_MAXPEERS/2 && iguana_metric(space) > coin->avemetric) || (coin->numthreads >= IGUANA_MAXPEERS/2 && coin->numthreads < IGUANA_MAXPEERS) ) + { + peerind = -1; + //portable_mutex_lock(&coin->netmutex); + if ( (addr= iguana_kvread(coin,space,(uint32_t *)&peerind,coin->peers,ipaddr)) != 0 ) + { + coin->numthreads++; + //portable_mutex_unlock(&coin->netmutex); + addr->coin = coin; + if ( coin->chain->numcheckpoints > 0 ) + addr->checkpointi = (coin->nextcheckpointi++ % coin->chain->numcheckpoints); + else addr->checkpointi = -1; + portable_thread_create(iguana_startconnection,addr); + } //else portable_mutex_unlock(&coin->netmutex); + } + //printf("possible.(%s)\n",ipaddr); + } + else + { + //portable_mutex_unlock(&coin->netmutex); + printf("error writing?\n"); + } + } + } + } + return(0); +} +/*iguana_send_version(coin,addr,coin->myservices); + if ( addr->dead == 0 && addr->usock >= 0 ) + { + printf("connected and version sent to usock.%d (%s) numpings.%d\n",addr->usock,addr->ipaddr,addr->numpings); + if ( coin->chain->numcheckpoints > 0 ) + addr->checkpointi = (coin->nextcheckpointi++ % coin->chain->numcheckpoints); + printf("%s uses checkpointi.%d\n",addr->ipaddr,addr->checkpointi); + //nexti = (coin->chain->numcheckpoints/IGUANA_MAXPEERS) * addr->checkpointi; + iguana_advancechain(coin,addr,addr->checkpointi);//nexti++ % coin->chain->numcheckpoints); + }*/ + +/*iguana_send_version(coin,addr,coin->myservices); + if ( addr->dead == 0 && addr->usock >= 0 ) + { + printf("connected and version sent to usock.%d (%s) numpings.%d\n",addr->usock,addr->ipaddr,addr->numpings); + if ( coin->chain->numcheckpoints > 0 ) + addr->checkpointi = (coin->nextcheckpointi++ % coin->chain->numcheckpoints); + printf("%s uses checkpointi.%d\n",addr->ipaddr,addr->checkpointi); + //nexti = (coin->chain->numcheckpoints/IGUANA_MAXPEERS) * addr->checkpointi; + iguana_advancechain(coin,addr,addr->checkpointi);//nexti++ % coin->chain->numcheckpoints); + }*/ + + +void iguana_pollconnection(struct iguana_info *coin,struct iguana_peer *addr) +{ + /*if ( addr->last_getblocks < time(NULL) - (24 * 60 * 60) ) + { + memset(stophash.bytes,0,sizeof(stophash)); + n = iguana_locator(coin,hashes,(int32_t)(sizeof(hashes)/sizeof(*hashes))); + iguana_send_hashes(coin,addr->protover < GETHEADERS_VERSION ? "getblocks" : "getheaders",addr,stophash,hashes,n); + printf("send %s to %s\n",addr->protover < GETHEADERS_VERSION ? "getblocks" : "getheaders",addr->ipaddr); + addr->last_getblocks = time(NULL); + }*/ +} + +void iguana_checkpoint(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2,int32_t height) +{ + struct iguana_block block; + memset(&block,0,sizeof(block)); + block.prev_block = hash2; + block.height = (height + 1); + //printf("write (%s) to %d\n",bits256_str(hash2),height+1); + iguana_RWmmap(1,&block,coin,coin->blocks.db,height+1); + //iguana_syncmap(coin->blocks.db,0); +} + +int32_t iguana_addblockhash(struct iguana_info *coin,struct iguana_peer *addr,int32_t *heightp,bits256 hash2,bits256 nexthash) +{ + struct iguana_block *block,*next,space,nextspace; + *heightp = -1; + if ( (block= iguana_findblock(coin,&space,hash2)) != 0 ) + { + *heightp = block->height; + if ( (next= iguana_findblock(coin,&nextspace,nexthash)) == 0 ) + { + iguana_checkpoint(coin,addr,nexthash,block->height + 1); + //iguana_audit(coin); + return(0); + } + else if ( next->height != block->height + 1 ) + { + printf("iguana_addblockhash: mismatched next height.%d vs height.%d+1\n",next->height,block->height); + //iguana_audit(coin); + return(-1); + } + else + { + //iguana_audit(coin); + return(iguana_blockdata(coin,block)); + } + } + //iguana_audit(coin); + return(0); +} + + +int32_t iguana_advancecmp(bits256 hashes[2],int32_t n,int32_t cmpa,int32_t cmpb) +{ + //printf("n.%d cmpa.%d cmpb.%d\n",n,cmpa,cmpb); + if ( bits256_nonz(hashes[0]) != 0 && bits256_nonz(hashes[1]) == 0 && (cmpa == 0 || n < cmpa) && (cmpb == 0 || n < cmpb) ) + return(1); + //printf("failed cmp %d %d %d %d\n",bits256_nonz(hashes[0]) != 0,bits256_nonz(hashes[1]) == 0,(cmpa == 0 || n < cmpa),(cmpb == 0 || n < cmpb)); + return(0); +} + +/*void iguana_advancechain(struct iguana_info *coin,struct iguana_peer *addr,int32_t checkpointi) + { + bits256 stophash,hashes[10]; int32_t islocal,n = 0; char *cmd = ""; + memset(stophash.bytes,0,sizeof(stophash)); + islocal = (strcmp("127.0.0.1",addr->ipaddr) == 0); + //printf("blockhash %d, blockhdr.%d block.%d height.%d\n",addr->maxblockhash_height,addr->maxblockhdr_height,addr->maxblock_height,addr->height); + if ( (islocal != 0 || addr->protover < GETHEADERS_VERSION) && iguana_advancecmp(addr->maxblockhash,addr->maxblockhash_height,addr->maxblockhdr_height+500,addr->height+2000) != 0 ) + { + printf("request blockhashes islocal.%d\n",islocal); + cmd = "getblocks"; + addr->maxblockhash[1] = hashes[n++] = coin->blocks.hwmchain;//addr->maxblockhash[0]; + } + else if ( islocal != 0 && addr->protover >= GETHEADERS_VERSION && iguana_advancecmp(addr->maxblockhdr,addr->maxblockhdr_height,addr->maxblock_height+500,0) != 0 ) + { + cmd = "getheaders"; + printf("request headers islocal.%d\n",islocal); + addr->maxblockhdr[1] = hashes[n++] = addr->maxblockhdr[0]; + } + else if ( memcmp(coin->blocks.hwmchain.bytes,addr->maxblock[1].bytes,sizeof(bits256)) != 0 ) //if ( iguana_advancecmp(addr->maxblock,addr->maxblock_height,addr->height,0) != 0 ) + { + printf("request data\n"); + addr->maxblock[0] = addr->maxblock[1] = coin->blocks.hwmchain; + iguana_request_data(coin,addr,coin->blocks.hwmchain,MSG_BLOCK); + return; + } + else + { + //printf("nothing to advance %s\n",addr->ipaddr); + return; + } + n = iguana_locator(coin,hashes,(int32_t)(sizeof(hashes)/sizeof(*hashes))-1,checkpointi); + iguana_send_hashes(coin,cmd,addr,stophash,hashes,n); + }*/ + + +// got functions + +void iguana_gotblockhash(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2,bits256 nexthash,int32_t i,int32_t n) +{ + int32_t height; struct iguana_block space; bits256 hashes[2001]; + /*if ( iguana_addblockhash(coin,addr,&height,hash2,nexthash) == 0 ) + { + //if ( height > (coin->blocks.hwmheight-10) ) + // iguana_request_data(coin,addr,nexthash,MSG_BLOCK); + } + if ( height > addr->maxblockhash_height ) + { + addr->maxblockhash_height = height; + addr->maxblockhash[0] = hash2; + memset(addr->maxblockhash[1].bytes,0,sizeof(hash2)); + }*/ + //if ( i > 0 ) + //hashes[i-1] = hash2; + iguana_request_data(coin,addr,&hash2,1,MSG_BLOCK); + if ( i == n-1 ) + { + hashes[i] = nexthash; + iguana_request_data(coin,addr,&nexthash,1,MSG_BLOCK); + //iguana_request_data(coin,addr,hashes,n,MSG_BLOCK); + bits256 stophash; + memset(stophash.bytes,0,sizeof(stophash)); + iguana_send_hashes(coin,"getblocks",addr,stophash,&nexthash,1); + } + height = iguana_height(coin,hash2); + //printf("set gotblockhash.%s %d ht.%d -> %s from %s\n",bits256_str(hash2),height,iguana_height(coin,hash2),bits256_str2(nexthash),addr->ipaddr); + //iguana_audit(coin); +} + +int32_t iguana_gotblockhdr(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgblock *msg,uint8_t *serialized,int32_t len,bits256 hash2,int32_t checkpointi) +{ + int32_t n = 0,height = -1; struct iguana_block space,*block; + //printf("got gotblockhdr.%s from %s, checkpointi.%d\n",bits256_str(hash2),addr->ipaddr,checkpointi); + + if ( (block= iguana_findblock(coin,&space,hash2)) != 0 && block->height < coin->blocks.hwmheight ) + return(block->height - coin->blocks.hwmheight); + iguana_convblock(&space,msg,-1,0,0.); + if ( (height= iguana_addblock(coin,addr,hash2,&space)) > 0 ) + { + n = iguana_lookahead(coin,addr,&hash2,height + 1); + //printf("lookahead.%d\n",n); + } + /*if ( height+1+n > addr->maxblockhdr_height ) + { + printf("set new maxblockhdr.%d\n",height+1+n); + addr->maxblockhdr_height = height+1+n; + addr->maxblockhdr[0] = hash2; + memset(addr->maxblockhdr[1].bytes,0,sizeof(hash2)); + }*/ + //iguana_audit(coin); + return(height); +} +int32_t iguana_queue_ramchain(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2,int32_t txind,int32_t numtx,struct iguana_msgtx *tx,bits256 txid) +{ + int32_t height; + addr->getdatamillis = 0; + if ( addr != 0 && txind == 0 && (height= iguana_height(coin,hash2)) >= 0 )//&& height >= addr->maxblock_height ) + { + //printf("got ramchain tx.%s from %s height.%d txind.%d\n",bits256_str(txid),addr!=0?addr->ipaddr:"local",height,txind); + /*printf("set new maxblock.%d\n",height); + addr->maxblock_height = height; + addr->maxblock[0] = hash2; + memset(addr->maxblock[1].bytes,0,sizeof(hash2));*/ + } + return(0); +} +void iguana_gottxid(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2) +{ +} + +int32_t iguana_addblock(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2,struct iguana_block *newblock) +{ + int32_t h; + //,firsttxidind,txn_count,hwm=0,equivalent = 0; + //double PoW; struct iguana_block *block,space,prevspace; + //height = newblock->height, firsttxidind = newblock->firsttxidind, PoW = newblock->PoW; + //printf("iguana_addblock nBits.%x\n",newblock->bits); + /*if ( (block= iguana_findblock(coin,&space,hash2)) != 0 ) + { + if ( height >= 0 ) + { + if ( height != block->height ) + printf("iguana_addblockhdr: height.%d mismatch vs %d\n",height,block->height); + } else height = block->height; + if ( firsttxidind > 0 ) + { + if ( firsttxidind != block->firsttxidind ) + printf("iguana_addblockhdr: firsttxidind.%d mismatch vs %d\n",firsttxidind,block->firsttxidind); + } else firsttxidind = block->firsttxidind; + if ( PoW > SMALLVAL ) + { + if ( fabs(PoW - block->PoW) > SMALLVAL ) + printf("iguana_addblockhdr: PoW.%.15f mismatch vs %.15f\n",PoW,block->PoW); + } else PoW = block->PoW; + if ( (flag= iguana_blockdata(coin,block)) == 0 ) + { + printf("write out block.(%s) to %d\n",bits256_str(hash2),height); + iguana_kvwrite(coin,coin->blocks.db,hash2.bytes,newblock,sizeof(*newblock),(uint32_t *)&height); + } + else if ( flag > 0 ) + { + space2 = *block; + space2.height = newblock->height; + if ( memcmp(block,&space2,sizeof(*block)) != 0 ) + printf("newblock is different from oldblock (%d %d %f) vs (%d %d %f)\n",newblock->height,newblock->firsttxidind,newblock->PoW,block->height,block->firsttxidind,block->PoW); + else + { + equivalent = 1; + } + } + //printf("newblock (%d %d %f) vs old (%d %d %f)\n",newblock->height,newblock->firsttxidind,newblock->PoW,block->height,block->firsttxidind,block->PoW); + //iguana_audit(coin); + } + if ( memcmp(coin->chain->genesis_hashdata,hash2.bytes,sizeof(hash2)) == 0 ) + { + PoW = height = txn_count = 0; + prev = 0; + firsttxidind = 1; + hwm = 1; + block = newblock; + printf("adding genesis\n"); + } + else if ( (prev= iguana_findblock(coin,&prevspace,newblock->prev_block)) == 0 ) + { + printf("hash2.(%s) ",bits256_str(hash2)); + fprintf(stderr,"iguana_blockchain no prev block.(%s)\n",bits256_str(newblock->prev_block)); + getchar(); + return(-1); + } + else + { + if ( height >= 0 && height != prev->height + 1 ) + printf("iguana_addblock: height.%d != prev.%d+1\n",height,prev->height); + height = prev->height + 1; + PoW = prev->PoW; + firsttxidind = prev->firsttxidind; + txn_count = prev->txn_count; + }*/ + if ( (newblock->height= iguana_setchainvars(coin,addr,&newblock->firsttxidind,&newblock->PoW,hash2,newblock->prev_block,newblock->bits,newblock->txn_count)) != (uint32_t)-1 ) + { + if ( newblock->PoW > coin->blocks.hwmPoW ) + { + if ( newblock->height+1 > coin->blocks.maxblocks ) + coin->blocks.maxblocks = (newblock->height + 1); + h = newblock->height; + iguana_kvwrite(coin,coin->blocks.db,hash2.bytes,newblock,(uint32_t *)&h); + if ( addr != 0 && newblock->height > addr->height ) + addr->height = newblock->height; + coin->blocks.hwmheight = newblock->height; + coin->blocks.hwmPoW = newblock->PoW; + coin->blocks.hwmchain = hash2; + coin->latest.blockhash = hash2; + coin->latest.merkleroot = newblock->merkle_root; + coin->latest.timestamp = newblock->timestamp; + coin->latest.numblocks = coin->blocks.hwmheight+1; + coin->latest.numtxidind = newblock->firsttxidind + newblock->txn_count; + //iguana_syncmap(coin->blocks.db,0); + //printf("%s height.%d PoW %f\n",bits256_str(hash2),block->height,block->PoW); + if ( coin->initblocks != 0 ) + printf("ADD %d:%d:%d <- (%s) n.%u max.%u PoW %f\n",h,iguana_height(coin,coin->blocks.hwmchain),newblock->height,bits256_str(coin->blocks.hwmchain),coin->blocks.hwmheight+1,coin->blocks.maxblocks,newblock->PoW); + } + } + if ( memcmp(hash2.bytes,coin->blocks.hwmchain.bytes,sizeof(hash2)) != 0 ) + { + printf("ORPHAN.%s height.%d PoW %f vs best %f\n",bits256_str(hash2),newblock->height,newblock->PoW,coin->blocks.hwmPoW); + newblock->height = -1; + } + //iguana_audit(coin); + return(newblock->height); +} + +/*int32_t iguana_locator(struct iguana_info *coin,bits256 *hashes,int32_t max) + { + int32_t i,n = 0; bits256 prevhash; + hashes[n++] = coin->blocks.hwmchain; + for (i=0; iblocks.db->keyoffset + coin->blocks.db->keysize) / sizeof(struct iguana_block)); + if ( n > sizeof(blocks)/sizeof(*blocks) || coin->blocks.db->keysize != sizeof(bits256) ) + return(hash2); + for (i=0; iheight+i) == 0 ) + return(hash2); + memcpy(hash2.bytes,(void *)((long)&blocks[0] + coin->blocks.db->keyoffset),coin->blocks.db->keysize); + return(hash2); + }*/ + +int32_t iguana_blockdata(struct iguana_info *coin,struct iguana_block *block) +{ + bits256 key = iguana_blockkey(coin,block); + if ( block->height+1 >= (coin->blocks.db->state.M.allocsize / sizeof(*block)) || bits256_nonz(key) == 0 ) + return(-1); + else if ( block->height > 0 && (bits256_nonz(block->prev_block) == 0 || fabs(block->PoW) < SMALLVAL) ) + { + printf("iguana_blockdata height.%d \n",block->height); + return(0); + } + if ( block->firsttxidind > 0 ) + return(1); + return(-1); +} +/*space = mycalloc(1,sizeof(*space)); + if ( (addr= iguana_kvread(coin,space,(uint32_t *)&peerind,coin->peers,ipaddr)) == 0 ) + { + memcpy(space,&addrs[i],sizeof(*space)); + addr = space; + iguana_clear_addrstate(coin,addr); + } + else if ( addr->usock >= 0 || addr->pending != 0 || addr->dead != 0 ) + { + printf("%s usock.%d pending.%u dead.%d\n",addr->ipaddr,addr->usock,addr->pending,addr->dead); + break; + } + addr->lastcontact = (uint32_t)time(NULL); + if ( coin->numthreads < IGUANA_MAXTHREADS )//&& (coin->numactive < 3 || (coin->numactive < IGUANA_MAXPEERS/2 && iguana_metric(space) > coin->avemetric) || (coin->numactive >= IGUANA_MAXPEERS/2 && coin->numactive < IGUANA_MAXPEERS)) ) + { + addr->pending = (uint32_t)time(NULL); + addr->coin = coin; + peerind = -1; + iguana_kvwrite(coin,coin->peers,ipaddr,addr,(uint32_t *)&peerind); + portable_thread_create(iguana_startconnection,addr); + } else*/ + +{ + int32_t valuesize; void *checkptr; + valuesize = iguana_valuesize(coin,kv); + memset(kv->state.space,0,kv->RAMvaluesize); + checkptr = kv->state.space; + if ( (kv->flags & IGUANA_MAPPED_ITEM) != 0 ) + { + value = (void *)((long)value + sizeof(UT_hash_handle)); + checkptr = (void *)((long)checkptr + sizeof(UT_hash_handle)); + } + if ( iguana_RWmmap(0,kv->state.space,coin,kv,*itemindp) != 0 || memcmp(value,checkptr,valuesize) != 0 ) + { + printf("iguana_RWmmap data mismatch after kvwrite\n"); + getchar(); + } + +} +/*init_hexbytes_noT(hexstr,pk_script+1,pk_script[0]); + //printf("(%s).%02x ",hexstr,pk_script[pk_script[0]]); + if ( 1 && pk_script[1] == 4 ) + { + pk[0] = 2 + (pk_script[pk_script[0]] & 1); + memcpy(pk+1,pk_script+2,32); + init_hexbytes_noT(hexstr,pk,33); + printf("data.(%s).%d ",hexstr,pk_script[0]); + vcalc_sha256(0,sha256,pk,33); + calc_rmd160(0,rmd160,sha256,sizeof(sha256)); + init_hexbytes_noT(hexstr,rmd160,20); + printf("rmd.(%s) ",hexstr); + //decode_hex(rmd160,20,"e34498597d0d4d4be05db1bb7501da985e15aaa5"); + btc_convrmd160(coinaddr,coin->chain->addr_pubkey,rmd160); + printf("(%s)\n",coinaddr); + }*/ +/*int32_t iguana_possible_peer(struct iguana_info *coin,char *ipaddr) + { + struct iguana_peer *addr=0,addrs[8]; uint32_t i,n; + #ifdef IGUANA_DISABLEPEERS + if ( strcmp(ipaddr,"127.0.0.1") != 0 ) + return(0); + #endif + if ( strncmp("0.0.0",ipaddr,5) != 0 && strcmp("0.0.255.255",ipaddr) != 0 && strcmp("1.0.0.0",ipaddr) != 0 ) + { + //printf("possible peer.(%s)\n",ipaddr); + memset(addrs,0,sizeof(addrs)); + n = iguana_connect(addrs,(int32_t)(sizeof(addrs)/sizeof(*addrs)),ipaddr,coin->chain->default_port,0); + if ( n > 0 ) + { + for (i=0; i<1; i++) // n is almost always 1 + { + strcpy(addrs[i].coinstr,coin->name); + addr = mycalloc('p',1,sizeof(*addr)); + *addr = addrs[i]; + iguana_clear_peerstate(coin,addr); + queue_enqueue("connectionQ",&coin->peers.connectionQ,&addr->DL); + return(0); + } + } + } + if ( addr != 0 ) + myfree(addr,sizeof(*addr)); + return(0); + }*/ +/*{ + for (i=0; iname); + //if ( addr->ipv6 != 0 ) + // err = iguana_connectsocket(1,addr,(struct sockaddr *)&addr->saddr6,sizeof(addr->saddr6)); + //else err = iguana_connectsocket(1,addr,(struct sockaddr *)&addr->saddr4,sizeof(addr->saddr4)); + if ( err < 0 ) + { + fprintf(stderr,"close connect %s: %s numpings.%d\n",addr->ipaddr,strerror(-err),addr->numpings); + iguana_iAkill(coin,addr); + } + else + { + iguana_iAconnected(coin,addr); + addr->ready = (uint32_t)time(NULL); + } + } + }*/ +void iguana_clear_peerstate(struct iguana_info *coin,struct iguana_peer *addr) +{ + addr->usock = -1; + addr->pingnonce = 0; + addr->ready = addr->dead = addr->pending = 0; + addr->startsend = addr->startrecv = 0; + addr->bufsize = 0; addr->buf = 0; + strcpy(addr->symbol,coin->symbol); + strcpy(addr->coinstr,coin->name); + //memset(&addr->DL,0,sizeof(addr->DL)); + //memset(&addr->sendQ,0,sizeof(addr->sendQ)); + //memset(&addr->msgcounts,0,sizeof(addr->msgcounts)); +} + +/**/ +/* + void iguana_activate(struct iguana_info *coin,struct iguana_peer *addr) + { + int32_t i;//,peerind = -1; + if ( coin->peers.numactive > 0 ) + { + for (i=0; ipeers.numactive; i++) + if ( strcmp(coin->peers.active[i].ipaddr,addr->ipaddr) == 0 ) + break; + if ( i != coin->peers.numactive ) + { + printf("duplicate activation.%s rejected\n",addr->ipaddr); + return; + } + } + coin->peers.active[coin->peers.numactive] = *addr; + myfree(addr,sizeof(*addr)); + addr = &coin->peers.active[coin->peers.numactive++]; + iguana_send_version(coin,addr,coin->myservices); + printf("ACTIVE.%d peer.(%s) numthreads.%d\n",coin->peers.numactive,addr->ipaddr,coin->numthreads); + //if ( strcmp(addr->ipaddr,"127.0.0.1") == 0 ) + // portable_thread_create(iguana_localhost,addr); + } + + void iguana_connections(struct iguana_info *coin) + { + int32_t i,j,firsti,peerind; uint32_t ipbits; struct iguana_peer *addr; + if ( coin->numthreads < IGUANA_MAXTHREADS && (addr= queue_dequeue(&coin->peers.connectionQ,0)) != 0 ) + { + if ( addr->pending == 0 ) + { + for (i=0; ipeers.active)/sizeof(*coin->peers.active); i++) + if ( strcmp(coin->peers.active[i].ipaddr,addr->ipaddr) == 0 ) + break; + if ( i == coin->peers.numactive ) + { + if ( coin->peers.numpending < sizeof(coin->peers.pending)/sizeof(*coin->peers.pending) ) + { + ipbits = (uint32_t)calc_ipbits(addr->ipaddr); + firsti = -1; + for (i=0; ipeers.pending)/sizeof(*coin->peers.pending); i++) + { + if ( coin->peers.pending[i] == 0 ) + firsti = i; + else if ( coin->peers.pending[i] == ipbits ) + break; + } + if ( i == sizeof(coin->peers.pending)/sizeof(*coin->peers.pending) ) + { + printf("PENDING.%-16s pending.%u ready.%u numpending.%d\n",addr->ipaddr,addr->pending,addr->ready,coin->numiAddrs); + coin->peers.pending[firsti] = ipbits; + addr->pending = (uint32_t)time(NULL); + strcpy(addr->symbol,coin->symbol); + iguana_launch(coin,"connection",iguana_startconnection,addr,0); + } + } + queue_enqueue("retryQ",&coin->peers.retryQ,&addr->DL); + } + } + else + { + if ( addr->ready != 0 ) + iguana_activate(coin,addr); + else if ( addr->dead == 0 ) + queue_enqueue("retryQ",&coin->peers.retryQ,&addr->DL); + } + } + }*/ + +//printf("parsed.%d firstvout.%d+%d firstvin.%d+%d: %s got.%d %s v %d\n",coin->blocks.parsedblocks,block->firstvout,block->numvouts,block->firstvin,block->numvins,addr->ipaddr,block->height,bits256_str(block->hash2),coin->blocks.hwmheight); +/*if ( block->height == coin->blocks.parsedblocks ) + iguana_parseblock(coin,block,tx,numtx); + else + { + printf("height.%d vs parsed.%d hwm.%d\n",block->height,coin->blocks.parsedblocks,coin->blocks.hwmheight); + iguana_addpending(coin,addr->ipbits,block,tx,numtx); + }*/ + +int32_t iguana_polliter(struct iguana_info *coin) +{ + struct pollfd fds[IGUANA_MAXPEERS]; + struct iguana_peer *addr,*addrs[IGUANA_MAXPEERS]; + int32_t i,n,nonz,flag,timeout=10; + memset(fds,0,sizeof(*fds)); + memset(addrs,0,sizeof(*addrs)); + flag = 0; + for (i=n=nonz=0; ipeers.active[i]; + fds[i].fd = -1; + if ( addr->usock < 0 || addr->dead != 0 || addr->ready == 0 ) + { + if ( addr->pending == 0 ) + addrs[n++] = addr; + continue; + } + if ( addr->startrecv == 0 ) + { + fds[i].fd = addr->usock; + fds[i].events |= POLLIN; + nonz++; + } + } + if ( nonz != 0 && poll(fds,IGUANA_MAXPEERS,timeout) > 0 ) + { + for (i=0; ipeers.active[i]; + if ( addr->usock < 0 || addr->dead != 0 || addr->ready == 0 ) + continue; + //if ( addr->usock >= 0 && addr->ready > 0 ) + // printf("%d/%d %d/%d startrecv.%u usock.%d dead.%d ready.%u\n",fds[i].events,fds[i].fd,POLLIN,POLLOUT,addr->startrecv,addr->usock,addr->dead,addr->ready); + if ( addr->startrecv == 0 && (fds[i].revents & POLLIN) != 0 ) + { + void iguana_processmsg(void *ptr); + flag++; + strcpy(addr->symbol,coin->symbol); + if ( 0 ) + { + addr->startrecv = (uint32_t)time(NULL); + iguana_launch("processmsg",iguana_processmsg,addr,0); + } else iguana_processmsg(addr); + } + } + } + return(flag); +} + +int32_t oldiguana_getdata(struct iguana_info *coin,struct iguana_peer *addr) +{ + struct iguana_overlap *ov = &addr->OV; + int32_t height,flag,elapsed,j,n = 0; bits256 hash2; double reqpsec,kbpsec; + //printf("iguana_getdata.(%s) ov.%p %p\n",addr->ipaddr,ov,addr); + //printf("addr height.%d vs parsed.%d\n",addr->height,coin->blocks.parsedblocks); + if ( ov->overlap == 0 ) + { + if ( strcmp("127.0.0.1",addr->ipaddr) == 0 ) + ov->overlap = IGUANA_MAXOVERLAP/2; + else ov->overlap = IGUANA_MAXOVERLAP/8; + iguana_teststart(coin,addr); + } + if ( addr != 0 && addr->dead == 0 && addr->usock >= 0 && addr->height >= coin->blocks.parsedblocks ) + { + for (flag=0; flagoverlap; flag++) + { + if ( addr->waiting[flag] == 0 ) + { + for (height=coin->blocks.parsedblocks; heightlongestchain&&heightblocks.parsedblocks+IGUANA_MAXPENDING; height++) + { + if ( coin->recvblocks != 0 && coin->recvblocks[height] != 0 ) + continue; + if ( strcmp("127.0.0.1",addr->ipaddr) == 0 ) + { + if ( height > coin->blocks.parsedblocks+IGUANA_MAXPENDING/2 ) + { + if ( n == 0 ) + { + hash2 = iguana_blockhash(coin,coin->blocks.parsedblocks); + iguana_request_data(coin,addr,&hash2,1,MSG_BLOCK); + } + return(n); + } + } + else + { + if ( height < coin->blocks.parsedblocks+IGUANA_MAXPENDING/2 ) + continue; + } + hash2 = iguana_blockhash(coin,height); + for (j=0; jwaitinghash[j].bytes,hash2.bytes,sizeof(hash2)) == 0 ) + break; + if ( j != sizeof(addr->waitinghash)/sizeof(*addr->waitinghash) ) + continue; + if ( height < coin->numwaitingbits && GETBIT(coin->waitingbits,height) == 0 ) + { + if ( bits256_nonz(hash2) != 0 ) + { + addr->waiting[flag] = (uint32_t)time(NULL); + addr->waitinghash[flag] = hash2; + if ( ov->numreqs++ >= ov->overlap ) + { + if ( ov->numreqs == ov->overlap ) + ov->numreqs = ov->overlap; + elapsed = (uint32_t)(time(NULL) - ov->teststart) + 1; + reqpsec = (double)ov->numreqs / elapsed; + kbpsec = (double)ov->reqrecv / (1024 * elapsed); + dxblend(&ov->Rsec,reqpsec,0.99); + dxblend(&ov->KBsec,kbpsec,0.99); + if ( kbpsec*reqpsec >= (ov->Rsec * ov->KBsec) ) + ov->faster++; + else ov->slower++; + if ( ((ov->faster + ov->slower) % 1000) == 0 ) + printf("OV.%-2d i.%-2d +%-4d -%-4d | h.%d %u | %5.1f/sec %5.3f/kB vs %5.1f/sec %5.3f/kB %5.1f %s\n",ov->overlap,flag,ov->faster,ov->slower,height,addr->waiting[flag],ov->Rsec,ov->KBsec,reqpsec,kbpsec,reqpsec*kbpsec-ov->Rsec*ov->KBsec,addr->ipaddr); + if ( time(NULL) > ov->teststart+60 || (ov->faster+ov->slower > ov->overlap*2 && ov->faster > 10*ov->slower) ) + iguana_teststart(coin,addr); + elapsed = (uint32_t)(time(NULL) - coin->starttime) + 1; + reqpsec = (double)coin->totalpackets / elapsed; + kbpsec = (double)coin->totalrecv / (1024 * elapsed); + dxblend(&coin->Rsec,reqpsec,0.99); + dxblend(&coin->KBsec,kbpsec,0.99); + } + n++; + //printf("request.%d bit.%d\n",height,GETBIT(coin->waitingbits,height)); + iguana_request_data(coin,addr,&hash2,1,MSG_BLOCK); + SETBIT(coin->waitingbits,height); + break; + } + } + } + } + } + } + if ( strcmp(addr->ipaddr,"127.0.0.1") == 0 && n == 0 && coin->recvblocks != 0 && coin->recvblocks[height] == 0 ) + { + hash2 = iguana_blockhash(coin,coin->blocks.parsedblocks); + iguana_request_data(coin,addr,&hash2,1,MSG_BLOCK); + } + return(n); + //printf("full.%d numactive.%d hwm.%d\n",coin->fullblocks,coin->numactive,coin->blocks.hwmheight); +} + +void iguana_teststart(struct iguana_info *coin,struct iguana_peer *addr) +{ + static uint32_t lastdisp; + int32_t dir; struct iguana_overlap *ov = &addr->OV; + dir = (ov->overlap - ov->prevoverlap); + ov->prevoverlap = ov->overlap; + if ( dir != 0 ) + { + if ( time(NULL) > lastdisp+60 ) + { + lastdisp = (uint32_t)time(NULL); + printf("ov.%-2d M%4.1f-> %5.1f/sec %6.2f/kb M%4.1f |fast.%-3d vs slow.%-3d d.%-2d | ",ov->overlap,ov->prevmetric,ov->Rsec,ov->KBsec,ov->Rsec*ov->KBsec,ov->faster,ov->slower,dir); + printf("all %5.1f/sec, %6.2fKB %s\n",coin->Rsec,coin->KBsec,addr->ipaddr); + } + if ( ov->faster > ov->slower ) + { + if ( (dir > 0 && ov->overlap < IGUANA_MAXOVERLAP) || (dir < 0 && ov->overlap > 1) ) + ov->overlap += dir; + //else printf("max overlap\n"); + //printf("increase by dir.%d -> overlap.%d\n",dir,addr->overlap); + } + else if ( dir > 0 && ov->overlap > 1 ) + { + ov->overlap--; + //printf("since slower, reduce overlap to overlap.%d\n",addr->overlap); + } + else if ( dir < 0 && ov->overlap < IGUANA_MAXOVERLAP ) + { + ov->overlap++; + //printf("since faster, increase overlap to overlap.%d\n",addr->overlap); + } + //else printf("at lowest overlap, cant change\n"); + ov->prevmetric = (ov->Rsec * ov->KBsec); + ov->reqrecv = 0; + ov->numreqs = -ov->overlap; + ov->faster = ov->slower = 0; + } + else ov->overlap = 1; + ov->teststart = (uint32_t)time(NULL); +} + +void iguana_localhost(void *ptr) +{ + struct iguana_info *coin; struct iguana_peer *addr = ptr; + if ( addr != 0 && (coin= iguana_coin(addr->symbol)) != 0 ) + { + while ( addr->dead == 0 ) + _iguana_processmsg(coin,addr); + } +} +if ( (num= iguana_available(coin,availables)) > 0 ) +{ + if ( (addr= availables[0]) != 0 ) + { + m = iguana_needed(coin,coin->need[0],IGUANA_MAXPENDING/2,0); + n = iguana_needed(coin,coin->need[1],IGUANA_MAXPENDING/2,IGUANA_MAXPENDING/2); + if ( strcmp(addr->ipaddr,"127.0.0.1") == 0 || num == 1 ) + { + //printf("m.%d n.%d num.%d\n",m,n,num); + for (i=0; ineed[0][i]; + //printf("%d ",height); + hash2 = iguana_blockhash(coin,height); + iguana_request_data(coin,addr,&hash2,1,MSG_BLOCK); + SETBIT(coin->waitingbits,height); + } + if ( num > 1 ) + { + for (i=0; ineed[1][i]; + //printf("%d ",height); + hash2 = iguana_blockhash(coin,height); + iguana_request_data(coin,availables[(i+1) % (num-1)],&hash2,1,MSG_BLOCK); + SETBIT(coin->waitingbits,height); + } + } + } + else + { + for (i=0; ineed[0][i]; + //printf("%d ",height); + hash2 = iguana_blockhash(coin,height); + iguana_request_data(coin,availables[i % num],&hash2,1,MSG_BLOCK); + SETBIT(coin->waitingbits,height); + } + } + if ( 0 && m+n > 0 ) + printf("requests\n"); + /*if ( m == 0 ) + sleep(3); + if ( m+n == 0 ) + sleep(10);*/ + return(m+n); + } else printf("null available[0]\n"); + } + +int32_t iguana_needed(struct iguana_info *coin,int32_t *need,int32_t max,int32_t offset) +{ + int32_t nonz,m,height; + if ( coin->recvblocks == 0 ) + return(0); + nonz = m = 0; + memset(need,0,sizeof(*need) * max); + if ( (time(NULL) - coin->parsetime) > 3 ) + need[m++] = coin->blocks.parsedblocks; + for (height=coin->blocks.parsedblocks+offset; heightlongestchain&&heightblocks.parsedblocks+max+offset; height++) + { + if ( coin->recvblocks[height] != 0 ) + nonz++; + else if ( GETBIT(coin->waitingbits,height) == 0 ) + need[m++] = height; + } + return(m); +} + +int32_t iguana_available(struct iguana_info *coin,struct iguana_peer *availables[IGUANA_MAXPEERS]) +{ + int32_t j,n; struct iguana_peer *addr; + memset(availables,0,sizeof(*availables) * IGUANA_MAXPEERS); + for (j=n=0; jpeers.active[j]; + if ( addr->height < coin->blocks.parsedblocks || addr == coin->localaddr ) + continue; + if ( addr->usock >= 0 && addr->dead == 0 && addr->ready > 0 && iguana_updatewaiting(coin,addr) > 0 ) + availables[n++] = addr; + } + return(n); +} + +/*int32_t iguana_loadtx(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashp,int32_t txind,int32_t numtx,struct iguana_msgtx *tx,uint8_t *data,int32_t maxsize) + { + int32_t len; bits256 txid; + memset(tx,0,sizeof(*tx)); + len = iguana_rwtx(0,data,tx,maxsize,&txid); + if ( blockhashp != 0 ) + { + //printf("parse.(%s)\n",bits256_str(*blockhashp)); + //if ( (blocknum= iguana_height(coin,*blockhashp)) >= 0 ) + if ( iguana_queue_ramchain(coin,addr,*blockhashp,txind,numtx,tx,txid) > 0 ) + return(len); + //else printf("cant find blockhash.(%s)\n",bits256_str(*blockhashp)); + } + iguana_purgetx(tx,0); + return(len); + }*/ +/*for (i=0; iwaiting)/sizeof(*addr->waiting); i++) + { + if ( addr->waiting[i] != 0 && time(NULL) > (addr->waiting[i] + 60) ) + { + if ( (height= iguana_height(coin,addr->waitinghash[i])) >= 0 ) + { + printf("i.%d of %ld ipbits.%x timeout.%s height.%d\n",i,sizeof(addr->waiting)/sizeof(*addr->waiting),addr->ipbits,addr->ipaddr,height); + CLEARBIT(coin->waitingbits,height); + } + addr->waiting[i] = 0; + addr->waitinghash[i] = bits256_zero; + } + if ( addr->waiting[i] == 0 ) + n++; + }*/ + +void *iguana_kvmetriciterator(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) +{ + struct iguana_peer *addr = value; double *sortbuf = (double *)args; + if ( addr->numpings > 0 && addr->pingsum > SMALLVAL && item->hh.itemind < kv->numkeys ) + { + //printf("%p (%s).%d ind.%d msgs.%d pings.%d %.0fms [%.3f] last.%u lag.%d S.%llu R.%llu\n",sortbuf,addr->ipaddr,addr->usock,item->itemind,addr->numpackets,addr->numpings,addr->pingtime,addr->pingsum/addr->numpings,addr->lastcontact,kv->iteruarg - addr->lastcontact,(long long)addr->totalsent,(long long)addr->totalrecv); + sortbuf = &sortbuf[item->hh.itemind << 1]; + sortbuf[0] = iguana_metric(addr); + sortbuf[1] = item->hh.itemind; + } + return(0); +} + +int32_t iguana_sendrequests(struct iguana_info *coin,struct iguana_peer *addrs[],int32_t n,int32_t *blocks,int32_t m) +{ + int32_t i,height; bits256 hash2; + if ( n > 0 && m > 0 ) + { + for (i=0; iwaitingbits,height); + } + return(m); + } + return(0); +} +int32_t iguana_getdata(struct iguana_info *coin) +{ + int32_t reqs[IGUANA_READAHEAD],height,i,j,m,readahead,offset,numpeers,limit,n = 0; struct iguana_peer *addr,*addrs[IGUANA_MAXPEERS]; + if ( coin->R.waitingbits == 0 || coin->R.recvblocks == 0 ) + return(0); + capacity = iguana_capacity(coin,&numpeers,addrs); + if ( numpeers == 0 ) + return(0); + if ( capacity < numpeers ) + capacity = numpeers; + else if ( capacity > IGUANA_READAHEAD ) + capacity = IGUANA_READAHEAD; + readahead = (coin->longestchain - coin->blocks.parsedblocks) / numpeers; + for (j=m=0; jnumwaiting > IGUANA_MAXWAITING ) makes it worse + // break; + if ( coin->peers.numranked == 0 ) + addr = &coin->peers.active[j]; + else + { + if ( j >= coin->peers.numranked ) + break; + if ( (addr= coin->peers.ranked[j]) == 0 ) + continue; + } + if ( addr->recvblocks == 0 ) + limit = 1; + else + { + if ( addr == coin->peers.localaddr ) + limit = IGUANA_BUNDLESIZE; + else limit = addr->rank <= 0 ? 1 : (IGUANA_BUNDLESIZE / sqrt(addr->rank)); + if ( limit < 1 ) + limit = 1; + } + height = coin->blocks.parsedblocks; + if ( readahead < 1 ) + readahead = 1; + if ( readahead > IGUANA_READAHEAD ) + readahead = IGUANA_READAHEAD; + if ( addr->rank >= 0 && addr->ready > 0 && addr->usock >= 0 && addr->dead == 0 && addr->height > 0 ) + { + m++; + //printf("%s: addrht.%d %s p.%d getbit.%d rank.%d\n",addr->ipaddr,addr->height,addr->ipaddr,height,GETBIT(coin->waitingbits,height),addr->rank); + for (i=n=0; i<100000&&nrank > 0) ? addr->rank-1 : m)) * readahead; + height = (coin->blocks.parsedblocks + offset + i); + if ( height > coin->blocks.hwmheight || height > addr->height ) + { + //printf("%s: height.%d > hwm.%d || addr %d\n",addr->ipaddr,height,coin->blocks.hwmheight,addr->height); + break; + } + if ( coin->R.numwaiting > IGUANA_MAXWAITING && height > coin->blocks.parsedblocks+100 ) + break; + if ( iguana_waitstart(coin,height,addr) == 0 ) + { + //printf("%-15s request block.%-6d parsed.%-6d offset.%-4d rank.%-3d numpeers.%d numwaiting.%d\n",addr->ipaddr,height,coin->blocks.parsedblocks,offset,addr->rank,numpeers,coin->R.numwaiting); + n++; + } + } + } + } + return(n); +} + +/*else if ( time(NULL) > coin->parsetime+1 ) + { + coin->parsetime = (uint32_t)time(NULL); + printf("backstop.%d %s\n",coin->blocks.parsedblocks,bits256_str(iguana_blockhash(coin,coin->blocks.parsedblocks))); + iguana_waitclear(coin,coin->blocks.parsedblocks); + iguana_waitstart(coin,coin->blocks.parsedblocks,0); + iguana_updatewaiting(coin,coin->blocks.parsedblocks+1,100); + } + //else printf("ptr.%p height.%d\n",ptr,height);*/ + + +/*if ( coin->blocks.parsedblocks > initialheight ) + initialheight = coin->blocks.parsedblocks; + if ( coin->longestchain > initialheight ) + initialheight = coin->longestchain; + iguana_recvinit(coin,coin->R.numwaitingbits);*/ +//height = (coin->blocks.hwmheight / IGUANA_HDRSCOUNT) * IGUANA_HDRSCOUNT; +//iguana_queuehdrs(coin,height,iguana_blockhash(coin,height)); + +int32_t iguana_rwunspentind(struct iguana_info *coin,int32_t rwflag,struct iguana_unspent *U,uint32_t unspentind) +{ + if ( rwflag == 0 ) + { + memset(U,0,sizeof(*U)); + if ( iguana_kvread(coin,coin->unspents,0,U,&unspentind) != 0 ) + return(0); + else printf("error getting unspents[%u] when %d\n",unspentind,coin->latest.numunspents); + } + else if ( iguana_kvwrite(coin,coin->unspents,0,U,&unspentind) != 0 ) + return(0); + return(-1); +} +void iguana_requests(void *arg) +{ + int32_t flag,i,j,n; double sum; struct iguana_peer *addr; struct iguana_info *coin,**coins = arg; + n = (int32_t)coins[0]; + coins++; + printf("iguana_requests N.%d\n",n); + while ( 1 ) + { + for (i=0; iblocks.mutex); + //if ( iguana_avail(coin,coin->blocks.parsedblocks,10000) < 10000 ) + else printf("skip getting data max packets allocated %s\n",mbstr(sum)); + //portable_mutex_unlock(&coin->blocks.mutex); + } + } + if ( flag == 0 ) + usleep((uint32_t)coin->sleeptime + 1); + } +} +else +{ + if ( coin->peers.numranked > 0 && time(NULL) > coin->backstop ) + { + int32_t i; bits256 hash2; struct iguana_peer *addr; + i = (rand() % coin->peers.numranked); + hash2 = iguana_blockhash(coin,coin->blocks.parsedblocks); + addr = coin->peers.ranked[i]; + if ( addr != 0 && memcmp(hash2.bytes,addr->backstop.bytes,sizeof(hash2)) != 0 ) + { + iguana_waitclear(coin,coin->blocks.parsedblocks); + if ( addr != 0 ) + { + iguana_waitstart(coin,coin->blocks.parsedblocks,addr); + printf("%s BACKSTOP.%d\n",addr->ipaddr,coin->blocks.parsedblocks); + coin->backstop = (uint32_t)time(NULL); + } + } + } + /*if ( iguana_waitstart(coin,coin->blocks.parsedblocks,addr) == 0 ) + { + printf("backstop request.%d to %s\n",coin->blocks.parsedblocks,addr->ipaddr); + addr->backstop = hash2; + }*/ + //printf("%s skip %d vs %d ptr.%p\n",addr->ipaddr,coin->blocks.parsedblocks,coin->numwaitingbits,ptr); +} + +bits256 iguana_histo(struct iguana_info *coin) +{ + double sum = 0.; int32_t i; bits256 seed; + for (i=0; i<0x100; i++) + sum += coin->R.histo[i]; + sum /= i; + memset(seed.bytes,0,sizeof(seed)); + if ( sum > 0. ) + { + for (i=0; i<0x100; i++) + { + printf("%.2f ",coin->R.histo[i]/sum); + if ( coin->R.histo[i] > sum ) + SETBIT(seed.bytes,i); + } + } + printf("histo.(%s)\n",bits256_str(seed)); + return(seed); +} +struct iguana_state +{ + //char name[16]; + uint8_t sha256[256 >> 3]; struct sha256_vstate state; + //struct iguana_mappedptr M; struct iguana_space MEM; //queue_t writeQ; portable_mutex_t ; + void *table; + //FILE *fp; uint8_t *space; + //uint64_t maxitems; //uint32_t itemsize,flags; +}; +/*struct iguana_overlap + { + double KBsec,Rsec,prevmetric; + uint64_t reqrecv; + uint32_t teststart; + int32_t numreqs,overlap,faster,slower,prevoverlap; + };*/ + + +int32_t iguana_capacity(struct iguana_info *coin,int32_t *nump,struct iguana_peer *addrs[IGUANA_MAXPEERS]) +{ + struct iguana_peer *addr; int32_t i,n,capacity = 0; + for (i=n=0; ipeers.active[i]; + //if ( addr->usock >= 0 ) + // printf("%s ht.%d\n",addr->ipaddr,addr->height); + if ( addr->ready > 0 && addr->dead == 0 && addr->usock >= 0 && addr->height > coin->blocks.parsedblocks ) + { + capacity += addr->capacity; + addrs[n++] = addr; + } + } + *nump = n; + return(capacity); +} + +int32_t iguana_getdata(struct iguana_info *coin) +{ + int32_t reqs[IGUANA_READAHEAD],height,i,j,m,readahead,offset,numpeers,limit,n = 0; struct iguana_peer *addr,*addrs[IGUANA_MAXPEERS]; + if ( coin->R.waitingbits == 0 || coin->R.recvblocks == 0 ) + return(0); + for (i=numpeers=0; ipeers.active[i].usock < 0 ) + numpeers++; + if ( numpeers == 0 ) + return(0); + readahead = (coin->longestchain - coin->blocks.parsedblocks) / numpeers; + for (j=m=0; jnumwaiting > IGUANA_MAXWAITING ) makes it worse + // break; + if ( coin->peers.numranked == 0 ) + addr = &coin->peers.active[j]; + else + { + if ( j >= coin->peers.numranked ) + break; + if ( (addr= coin->peers.ranked[j]) == 0 ) + continue; + } + if ( addr->recvblocks == 0 ) + limit = 1; + else + { + if ( addr == coin->peers.localaddr ) + limit = IGUANA_EXPIREWINDOW; + else limit = addr->rank <= 0 ? 1 : (IGUANA_EXPIREWINDOW / sqrt(addr->rank)); + if ( limit < 1 ) + limit = 1; + } + height = coin->blocks.parsedblocks; + if ( readahead < 1 ) + readahead = 1; + if ( readahead > IGUANA_EXPIREWINDOW ) + readahead = IGUANA_EXPIREWINDOW; + if ( addr->rank >= 0 && addr->ready > 0 && addr->usock >= 0 && addr->dead == 0 && addr->height > 0 ) + { + m++; + //printf("%s: addrht.%d %s p.%d getbit.%d rank.%d\n",addr->ipaddr,addr->height,addr->ipaddr,height,GETBIT(coin->waitingbits,height),addr->rank); + for (i=n=0; i<100000&&nrank > 0) ? addr->rank-1 : m)) * readahead; + height = (coin->blocks.parsedblocks + offset + i); + if ( height > coin->blocks.hwmheight || height > addr->height ) + { + //printf("%s: height.%d > hwm.%d || addr %d\n",addr->ipaddr,height,coin->blocks.hwmheight,addr->height); + break; + } + //if ( coin->R.numwaiting > IGUANA_MAXWAITING && height > coin->blocks.parsedblocks+100 ) + // break; + if ( iguana_waitstart(coin,height,addr) != 0 ) + { + addr->capacity--; + //printf("%-15s request block.%-6d parsed.%-6d offset.%-4d rank.%-3d numpeers.%d numwaiting.%d\n",addr->ipaddr,height,coin->blocks.parsedblocks,offset,addr->rank,numpeers,coin->R.numwaiting); + n++; + } + } + } + } + return(n); +} + +int32_t newiguana_getdata(struct iguana_info *coin) +{ + int32_t reqs[IGUANA_READAHEAD],height,i,j,count,capacity,numpeers,n = 0; struct iguana_peer *addr,*addrs[IGUANA_MAXPEERS]; + if ( coin->R.waitingbits == 0 || coin->R.recvblocks == 0 ) + return(0); + capacity = iguana_capacity(coin,&numpeers,addrs); + if ( numpeers == 0 ) + return(0); + if ( capacity < numpeers ) + capacity = numpeers; + else if ( capacity > IGUANA_READAHEAD ) + capacity = IGUANA_READAHEAD; + if ( iguana_avail(coin,coin->blocks.parsedblocks,IGUANA_READAHEAD) == IGUANA_READAHEAD ) + n = iguana_updatewaiting(coin,reqs,capacity,coin->blocks.parsedblocks + (coin->longestchain - coin->blocks.parsedblocks)/2); + else n = iguana_updatewaiting(coin,reqs,IGUANA_READAHEAD,coin->blocks.parsedblocks); + count = 0; + height = coin->blocks.parsedblocks; + //printf("capacity.%d reqs.%d numpeers.%d\n",capacity,n,numpeers); + if ( n > 0 ) + { + for (i=0; iR.numwaiting > IGUANA_MAXWAITING )//&& height > coin->blocks.parsedblocks+100 ) + break; + for (j=0; jcapacity > 0 && addr->height >= height ) + { + count += iguana_waitstart(coin,height,addr); + break; + } + } + if ( j == numpeers ) + { + //printf("leftover.%d n.%d\n",i,n); + if ( (addr= addrs[(i+j) % numpeers]) != 0 && addr->height >= height ) + count += iguana_waitstart(coin,height,addr); + break; + } + } + } + //for (i=0; i= 0 ) + { + if ( flag == 0 ) + { + + } + printf("gotheaders flag.%d n.%d (%s) %d vs %d \n",flag,n,bits256_str(blocks[n-1].hash2),iguana_height(coin,blocks[n-1].hash2),coin->blocks.hwmheight); + if ( n > 0 && iguana_height(coin,blocks[n-1].hash2) > coin->blocks.hwmheight-1000 ) + iguana_send_hashes(coin,strcmp(coin->name,"bitcoin") != 0 ? "getblocks" : "getheaders",addr,bits256_zero,&blocks[n-1].hash2,1); + } + printf("%s gotheaders.%d height.%d flag.%d\n",addr->ipaddr,n,coin->blocks.hwmheight,flag); + //portable_mutex_unlock(&coin->blocks.mutex); + }*/ + + +//#define IGUANA_OVERLAP 64 +//#define IGUANA_MAXWAITING (2 * IGUANA_MAXPEERS * IGUANA_OVERLAP) +//#define IGUANA_EXPIREWINDOW 1000 +//#define IGUANA_READAHEAD (IGUANA_EXPIREWINDOW) + +#ifndef IGUANA_DEDICATED_THREADS +limit = 1; +if ( addr->ipbits != 0 && addr->pendhdrs < limit && (hashstr= queue_dequeue(&coin->R.hdrsQ,1)) != 0 ) +{ + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + iguana_send_hashes(coin,strcmp(coin->name,"bitcoin") != 0 ? "getblocks" : "getheaders",addr,bits256_zero,&hash2,1); + queue_enqueue("pendinghdrsQ",&coin->R.pendinghdrsQ[0],(void *)((long)hashstr - sizeof(struct queueitem)),0); + //printf("dequeue hdrsQ.(%s) -> %s pendinghdrsQ\n",hashstr,addr->ipaddr); + addr->hdrmillis = milliseconds(); + addr->pendhdrs++; + flag++; +} +if ( addr->recvblocks == 0 ) +limit = 1; +else limit = IGUANA_MAXPENDING; +if ( addr->ipbits != 0 && addr->pendblocks < limit && (hashstr= queue_dequeue(&coin->blocksQ,1)) != 0 ) +{ + //printf("dequeued.(%s) for %s\n",hashstr,addr->ipaddr); + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + if ( memcmp(hash2.bytes,coin->chain->genesis_hashdata,sizeof(hash2)) != 0 ) + { + iguana_request_data(coin,addr,&hash2,1,MSG_BLOCK,0); + addr->pendblocks++; + flag++; + } + free_queueitem(hashstr); +} +#endif + +void newiguana_updatehdrs(struct iguana_info *coin) +{ + int32_t i,j,hdri,flag,iter; char *hashstr; struct iguana_hdrs *hdrs; bits256 hash2; + portable_mutex_lock(&coin->R.hdrsmutex); + if ( iguana_needhdrs(coin) == 0 ) + return; + iguana_requesthdrs(coin,0); + return; + hdri = (coin->blocks.hwmheight / IGUANA_HDRSCOUNT); + hdrs = &coin->R.hdrs[hdri]; + if ( time(NULL) > coin->R.lasthdrtime+3 && coin->blocks.hwmheight < coin->longestchain-500 ) + { + for (iter=flag=0; iter<2; iter++) + { + while ( flag == 0 && (hashstr= queue_dequeue(&coin->R.pendinghdrsQ[iter],1)) != 0 ) + { + //printf("timeout found pending.(%s)\n",hashstr); + flag++; + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + for (j=0; j<2; j++) + iguana_send_hashes(coin,strcmp(coin->name,"bitcoin") != 0 ? "getblocks" : "getheaders",0,bits256_zero,&hash2,1); + //queue_enqueue("resubmit",&coin->R.pendinghdrsQ[iter ^ 1],(void *)((long)hashstr - sizeof(struct queueitem)),0); + coin->R.lasthdrtime = (uint32_t)time(NULL); + break; + } + } + if ( hdrs->blocks == 0 && coin->peers.numranked > 2 ) + { + hash2 = iguana_blockhash(coin,hdri * IGUANA_HDRSCOUNT); + printf("hdrs backstop %d %s\n",hdri * IGUANA_HDRSCOUNT,bits256_str(hash2)); + for (j=0; j<3; j++) + iguana_send_hashes(coin,strcmp(coin->name,"bitcoin") != 0 ? "getblocks" : "getheaders",coin->peers.ranked[j],bits256_zero,&hash2,1); + coin->R.lasthdrtime = (uint32_t)time(NULL); + } + } + if ( coin->blocks.hwmheight > 100000 && coin->R.savedhdrs < coin->blocks.hwmheight-501 ) + coin->R.savedhdrs = iguana_savehdrs(coin); + //printf("hdri.%d height.%d n.%d %p coin->R.savedhdrs.%u\n",hdri,hdrs->height,hdrs->n,hdrs->blocks,coin->R.savedhdrs); + if ( hdrs->blocks != 0 && (hdrs->height + hdrs->n) > coin->blocks.hwmheight ) + iguana_processhdrs(coin,hdrs->blocks,hdrs->n); + for (i=0; iR.hdrs[i]; + if ( coin->blocks.hwmheight >= (hdrs->height + hdrs->n) ) + { + if ( hdrs->blocks != 0 ) + { + myfree(hdrs->blocks,hdrs->n * sizeof(*hdrs->blocks)); + hdrs->blocks = 0; + } + if ( hdrs->conflictblocks != 0 ) + { + myfree(hdrs->conflictblocks,hdrs->n * sizeof(*hdrs->conflictblocks)); + hdrs->conflictblocks = 0; + } + } + } + portable_mutex_unlock(&coin->R.hdrsmutex); +} +void iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n) +{ + int32_t i,iter,flag; char hexstr[65],*hashstr; + addr->lastrequest = bits256_zero; + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + coin->R.lasthdrtime = (uint32_t)time(NULL); + iguana_processhdrs(coin,blocks,n); + return; + //printf("hdrs.(%s) n.%d from %s\n",bits256_str(blocks[0].hash2),n,addr->ipaddr); + portable_mutex_lock(&coin->R.hdrsmutex); + for (i=0; iR.numhdrs; i++) + { + if ( memcmp(coin->R.hdrs[i].hash2.bytes,blocks[0].prev_block.bytes,sizeof(bits256)) == 0 ) + { + init_hexbytes_noT(hexstr,blocks[0].prev_block.bytes,sizeof(bits256)); + for (iter=flag=0; iter<2; iter++) + { + while ( flag == 0 && (hashstr= queue_dequeue(&coin->R.pendinghdrsQ[iter],1)) != 0 ) + { + if( strcmp(hashstr,hexstr) == 0 ) + { + free_queueitem(hashstr); + //printf("found pending.(%s) hdri.%d\n",hexstr,(coin->blocks.hwmheight / IGUANA_HDRSCOUNT)); + flag++; + break; + } + queue_enqueue("requeue",&coin->R.pendinghdrsQ[iter ^ 1],(void *)((long)hashstr - sizeof(struct queueitem)),0); + } + } + if ( coin->R.hdrs[i].blocks == 0 ) + { + coin->R.hdrs[i].blocks = blocks; + coin->R.hdrs[i].n = n; + printf("got headers for %d[%d] from %s\n",coin->R.hdrs[i].height,n,addr->ipaddr); + if ( addr != 0 && coin->R.hdrs[i].height+n > addr->height ) + addr->height = coin->R.hdrs[i].height+n; + } + else if ( coin->R.hdrs[i].n == n && memcmp(coin->R.hdrs[i].blocks,blocks,n*sizeof(*blocks)) == 0 ) + { + coin->R.hdrs[i].duplicates++; + printf("duplicate.%d blocks for height.%d n.%d\n",coin->R.hdrs[i].duplicates,coin->R.hdrs[i].height,n); + myfree(blocks,sizeof(*blocks) * n); + } + else + { + if ( coin->R.hdrs[i].conflictblocks != 0 ) + { + myfree(coin->R.hdrs[i].conflictblocks,coin->R.hdrs[i].conflictblocksn * sizeof(struct iguana_block)); + } + coin->R.hdrs[i].conflicts++; + printf("conflict.%d blocks for height.%d n.%d\n",coin->R.hdrs[i].conflicts,coin->R.hdrs[i].height,n); + coin->R.hdrs[i].conflictblocks = blocks; + coin->R.hdrs[i].conflictblocksn = n; + } + portable_mutex_unlock(&coin->R.hdrsmutex); + return; + } + } + printf("got unexpected hdrs[%d] prev %s\n",n,bits256_str(blocks[0].prev_block)); + myfree(blocks,sizeof(*blocks) * n); + portable_mutex_unlock(&coin->R.hdrsmutex); +} +void iguana_connections(void *arg) +{ + FILE *fp; uint8_t serialized[sizeof(struct iguana_msghdr)]; struct iguana_info *coin,**coins = arg; + int32_t i,j,r,n,iter,flag; uint32_t now,lastrank; char fname[512]; + struct iguana_peer *addr; + n = (int32_t)coins[0]; + lastrank = (uint32_t)time(NULL); + coins++; + for (i=0; isymbol,(iter == 0) ? "peers" : "hdrs"); + if ( (fp= fopen(fname,"r")) != 0 ) + iguana_parseline(coin,iter,fp); + fclose(fp); + } + //iguana_recvinit(coin,coin->R.numwaitingbits); + } + while ( 1 ) + { + if ( time(NULL) > lastrank+60 ) + { + for (i=0; ipeers.mutex); + iguana_peermetrics(coins[i]); + portable_mutex_unlock(&coin->peers.mutex); + } + lastrank = (uint32_t)time(NULL); + } + now = (uint32_t)time(NULL); + for (i=flag=0; ipeers.active[(j + r) % IGUANA_MAXPEERS]; + if ( addr->usock >= 0 && addr->ipbits != 0 ) + { + if ( addr->dead == 0 && addr->ready > 0 ) + { + if ( now > addr->lastblockrecv+60 && addr->pendblocks > 0 ) + addr->pendblocks--; + if ( now > coin->peers.lastpeer+300 ) + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); + } + //printf("%s pend.(%d %d) dead.%u ready.%u relay.%d millis.%.0f hwm.%d\n",addr->ipaddr,addr->pendhdrs,addr->pendblocks,addr->dead,addr->ready,addr->relayflag,addr->hdrmillis,coin->blocks.hwmheight); + } + } + if ( now > coin->peers.lastpeer+300 ) + { + printf("lastpeer %u vs now.%u, startpeers\n",coin->peers.lastpeer,now); + coin->peers.lastpeer = now; + } + } + iguana_shutdownpeers(coin,0); // closes dead peers to free up open spots + } + if ( flag == 0 ) + usleep(10000); + } +} +&& ((packet= queue_dequeue(&addr->sendQ,0)) != 0 || (packet= queue_dequeue(&coin->sendQ,0)) != 0) ) +{ + //printf("%d %s: usock.%d dead.%u ready.%u\n",i,addr->ipaddr,addr->usock,addr->dead,addr->ready); + flag++; + if ( (packet->addr != 0 && packet->addr != addr) || (packet->getdatablock > 0 && packet->getdatablock < coin->blocks.parsedblocks) || coin->R.recvblocks[packet->getdatablock] != 0 ) + { + if ( coin->R.recvblocks[packet->getdatablock] == 0 ) + printf("peerloop: (%s).%d packetaddr.%p != %p || packet->getdatablock %d < %d coin->blocks.parsedblocks recv[%p]\n",packet->serialized+4,packet->datalen,packet->addr,addr,packet->getdatablock,coin->blocks.parsedblocks,coin->R.recvblocks[packet->getdatablock]); + myfree(packet,sizeof(*packet) + packet->datalen); + } + else + { + if ( 1 && addr != coin->peers.localaddr ) + { + if ( ) + { + addr->startsend = (uint32_t)time(NULL); + strcpy(addr->symbol,coin->symbol); + strcpy(addr->coinstr,coin->name); + iguana_launch("send_data",iguana_issue,packet,IGUANA_SENDTHREAD); + } else printf("need to wait for pending sends %d\n",iguana_numthreads(1<serialized,packet->datalen); + if ( packet->getdatablock > 0 ) + iguana_setwaitstart(coin,packet->getdatablock); + myfree(packet,sizeof(*packet) + packet->datalen); + } + } + void iguana_queuehdrs(struct iguana_info *coin,int32_t height,bits256 hash2) + { + char hashstr[65]; //int32_t hdrsi; + init_hexbytes_noT(hashstr,hash2.bytes,sizeof(hash2)); + queue_enqueue("hdrsQ",&coin->R.hdrsQ,queueitem(hashstr),1); + //printf("try to queue hdr.(%s) height.%d vs %d\n",hashstr,height,coin->blocks.hwmheight); + /*if ( (height % IGUANA_HDRSCOUNT) == 0 && height+1 >= coin->blocks.hwmheight ) + { + hdrsi = (height / IGUANA_HDRSCOUNT); + if ( (height == 0 || coin->R.hdrs[hdrsi].height == 0) && coin->R.hdrs[hdrsi].duplicates == 0 ) + { + coin->R.hdrs[hdrsi].hash2 = hash2; + coin->R.hdrs[hdrsi].height = height; + if ( hdrsi >= coin->R.numhdrs ) + coin->R.numhdrs = hdrsi + 1; + //printf("queued hdr.(%s)\n",hashstr); + queue_enqueue("hdrsQ",&coin->R.hdrsQ,queueitem(hashstr),1); + } + }*/ + } + + int32_t iguana_queue_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *serialized,char *cmd,int32_t len,int32_t getdatablock,int32_t forceflag) + { + struct iguana_packet *packet; int32_t datalen; + if ( addr == 0 ) + { + printf("iguana_queue_send null addr\n"); + getchar(); + return(-1); + } + datalen = iguana_sethdr((void *)serialized,coin->chain->netmagic,cmd,&serialized[sizeof(struct iguana_msghdr)],len); + if ( strcmp("getaddr",cmd) == 0 && time(NULL) < addr->lastgotaddr+300 ) + return(0); + if ( strcmp("version",cmd) == 0 ) + return(iguana_send(coin,addr,serialized,datalen)); + packet = mycalloc('S',1,sizeof(struct iguana_packet) + datalen); + packet->datalen = datalen; + packet->addr = addr; + if ( 1 && (packet->getdatablock = getdatablock) == 0 && strcmp((char *)&serialized[4],"getdata") == 0 ) + { + printf("no need to request genesis\n"); + getchar(); + } + memcpy(packet->serialized,serialized,datalen); + //printf("%p queue send.(%s) %d to (%s) %x\n",packet,serialized+4,datalen,addr->ipaddr,addr->ipbits); + queue_enqueue("sendQ",addr != 0 ? &addr->sendQ : &coin->sendQ,&packet->DL,0); + //printf("queue send.(%s) datalen.%d addr.%p %s [%d]\n",cmd,len,addr,addr!=0?addr->ipaddr:"",getdatablock); + if ( addr == 0 || (addr->dead == 0 && addr->ipbits != 0) ) + { + if ( addr == 0 && getdatablock == 0 ) + addr = iguana_choosepeer(coin); + if ( addr == coin->peers.localaddr || (getdatablock == 0 && strcmp(cmd,"getdata") != 0) ) + forceflag = 1; + if ( forceflag != 0 ) + { + if ( getdatablock >= coin->blocks.parsedblocks ) + len = iguana_send(coin,addr!=0?addr:iguana_choosepeer(coin),serialized,datalen); + else len = -1; + } + else + { + packet = mycalloc('S',1,sizeof(struct iguana_packet) + datalen); + packet->datalen = datalen; + packet->addr = addr; + if ( 1 && (packet->getdatablock = getdatablock) == 0 && strcmp((char *)&serialized[4],"getdata") == 0 ) + { + printf("no need to request genesis\n"); + getchar(); + } + memcpy(packet->serialized,serialized,datalen); + //printf("%p queue send.(%s) %d to (%s) %x\n",packet,serialized+4,datalen,addr->ipaddr,addr->ipbits); + queue_enqueue("sendQ",addr != 0 ? &addr->sendQ : &coin->sendQ,&packet->DL,0); + } + } else printf("cant send.(%s) len.%d datalen.%d to null addr or dead.%u\n",&serialized[4],len,datalen,addr->dead); + return(len); + } + while ( (packet= queue_dequeue(&addr->sendQ,0)) != 0 ) + { + if ( packet->getdatablock > 0 && packet->getdatablock < coin->blocks.parsedblocks ) + { + packet->addr = 0; + printf("recycle pending sendQ block.%d\n",packet->getdatablock); + queue_enqueue("shutdown_sendQ",&coin->blocksQ,&packet->DL,0); + } else myfree(packet,sizeof(*packet) + packet->datalen); + } + +void iguana_dedicatedrecv(void *arg) +{ + struct iguana_info *coin = 0; uint8_t *buf; int32_t bufsize; struct iguana_peer *addr = arg; + if ( addr == 0 || (coin= iguana_coin(addr->symbol)) == 0 ) + { + printf("iguana_dedicatedrecv nullptrs addr.%p coin.%p\n",addr,coin); + return; + } + printf("DEDICATED RECV %s\n",addr->ipaddr); + bufsize = IGUANA_MAXPACKETSIZE; + buf = mycalloc('r',1,bufsize); + while ( addr->usock >= 0 && addr->dead == 0 && coin->peers.shuttingdown == 0 ) + _iguana_processmsg(coin,addr,buf,bufsize); + myfree(buf,bufsize); +} + if ( packet->getdatablock > 0 && (packet->getdatablock < coin->blocks.parsedblocks || coin->R.recvblocks[packet->getdatablock] != 0) ) + { + printf("discard sendQ for getdatablock.%d parsed.%d\n",packet->getdatablock,coin->blocks.parsedblocks); + myfree(packet,sizeof(*packet) + packet->datalen); + return(1); + + void iguana_issue(void *ptr) + { + uint32_t ipbits; char ipaddr[64]; struct iguana_peer *addr; struct iguana_info *coin=0; struct iguana_packet *packet = ptr; + if ( (addr= packet->addr) == 0 || (coin= iguana_coin(addr->symbol)) == 0 || addr->dead != 0 ) + { + printf("iguana_issue: addr %p coin.%p dead.%u\n",addr,coin,addr->dead); + return; + } + ipbits = (uint32_t)calc_ipbits(addr->ipaddr); + expand_ipbits(ipaddr,ipbits); + if ( strcmp(ipaddr,addr->ipaddr) == 0 ) + { + if ( packet->getdatablock == 0 ) + iguana_send(coin,addr,packet->serialized,packet->datalen); + else if ( packet->getdatablock > 0 && packet->getdatablock >= coin->blocks.parsedblocks ) + { + //printf("req block.%d to (%s) numthreads.%d\n",packet->getdatablock,packet->addr->ipaddr,iguana_numthreads(1 << IGUANA_SENDTHREAD)); + iguana_send(coin,addr,packet->serialized,packet->datalen); + iguana_setwaitstart(coin,packet->getdatablock); + } + } + else printf("iguana_issue: ipaddr mismatch.(%s) != (%s)\n",ipaddr,addr->ipaddr), getchar(); + //printf("finished sending %d to (%s) numthreads.%d\n",packet->datalen,packet->addr->ipaddr,iguana_numthreads(-1)); + addr->startsend = 0; + myfree(packet,sizeof(*packet) + packet->datalen); + } + + uint64_t iguana_validaterecv(struct iguana_info *coin,int32_t *nump,char *fname) + { + struct iguana_pending *ptr; struct iguana_block space; struct iguana_msgtx *tx; + int32_t n = 0; struct iguana_mappedptr M; struct iguana_memspace RSPACE; uint64_t allocated = 0; + memset(&M,0,sizeof(M)); + memset(&RSPACE,0,sizeof(RSPACE)); + if ( (ptr= iguana_mappedptr(0,&M,0,0,fname)) != 0 ) + { + RSPACE.ptr = M.fileptr; + RSPACE.used = 0; + RSPACE.size = M.allocsize; + printf("process.(%s) %ld\n",fname,(long)M.allocsize); + n = 0; + while ( ptr != 0 && ((long)ptr - (long)RSPACE.ptr)+ptr->next < (RSPACE.size - sizeof(*ptr)) ) + { + //printf("ptr diff.%d next.%d\n",(int32_t)((long)ptr - (long)RSPACE.ptr),ptr->next); + if ( (tx= iguana_validpending(coin,ptr,&space)) != 0 ) + { + //printf("%d: ht.%-6d size.%d next.%d\n",n,ptr->block.height,ptr->allocsize,ptr->next); + iguana_freetx(tx,ptr->numtx); + allocated += ptr->allocsize; + n++; + coin->R.recvblocks[ptr->block.height] = ptr; + } + else + { + printf("n.%d ht.%d: tx doesnt validate\n",n,ptr->block.height); + } + if ( ptr->next != 0 ) + ptr = (void *)((long)ptr + ptr->next); + else break; + } + if ( n == 0 ) + iguana_closemap(&M); + } + *nump = n; + return(allocated); + } + for (i=maxi=skipped=total=0; skipped<10; i++) + { + if ( coin->R.maprecvdata == 0 ) + break; + sprintf(fname,"tmp/%s/recv.%d",coin->symbol,i), iguana_compatible_path(fname); + if ( (allocated= iguana_validaterecv(coin,&n,fname)) != 0 ) + combined += allocated, total += n, skipped = 0, maxi = i; + else if ( skipped++ > 10 ) + break; + } + + /*for (i=0; isymbol); + ensure_directory(dirname); + sprintf(dirname,"tmp/%s",coin->symbol); + ensure_directory(dirname); + }*/ + //iguana_launch("peers",iguana_connections,coins,IGUANA_PERMTHREAD); + //iguana_launch("requests",iguana_requests,coins,IGUANA_PERMTHREAD); + + //portable_mutex_t hdrsmutex; struct iguana_hdrs *hdrs; uint32_t savedhdrs,lasthdrtime,numhdrs; + struct iguana_hdrs + { + bits256 hash2; + struct iguana_block *blocks,*conflictblocks; + int32_t n,conflictblocksn,height,conflicts,duplicates; + }; + + + bits256 iguana_unspentmap(struct iguana_info *coin,uint32_t *spendindp,uint32_t *txidindp,char *txidstr,uint32_t unspentind) + { + struct iguana_unspent U; + memset(&U,0,sizeof(U)); + if ( iguana_rwunspentind(coin,0,&U,unspentind) == 0 ) + { + *txidindp = U.txidind; + *spendindp = U.spendind; + if ( txidstr != 0 ) + return(iguana_txidstr(coin,0,0,txidstr,U.txidind)); + } + else printf("error getting unspents[%u] when %d\n",unspentind,coin->latest.numunspents), getchar(); + return(bits256_zero); + } + + int32_t iguana_inittxid(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,int32_t itemind,int32_t itemsize,int32_t numitems) + { + char txidstr[513]; bits256 checktxid; uint32_t txidind,spendind; struct iguana_txid *tx = value; + if ( key != 0 && value != 0 && itemind > 0 ) + { + //printf("inittxid.(%s) itemind.%d (%d %d)\n",bits256_str(tx->txid),itemind,tx->firstvout,tx->firstvin); + checktxid = iguana_unspentmap(coin,&spendind,&txidind,txidstr,tx->firstvout); + if ( memcmp(checktxid.bytes,key,sizeof(checktxid)) != 0 || txidind != itemind ) + { + printf("checktxid.%s miscompares to %s, txidind.%d vs itemind.%d\n",txidstr,bits256_str(tx->txid),txidind,itemind); + getchar(); + return(-1); + } + if ( spendind >= coin->latest.numspends ) + { + //struct iguana_unspent U; + //iguana_rwunspentind(coin,0,&U,tx->firstvout); + //U.spendind = 0; + //iguana_rwunspentind(coin,1,&U,tx->firstvout); + printf("spendind.%d vs %d overflow in txid.%s txidind.%d U%d autocleared\n",spendind,coin->latest.numspends,txidstr,tx->firstvout,tx->firstvout); + } + //printf("txidind.%d: 1st.(%d %d)\n",txidind,tx->firstvout,tx->firstvin); + } + return(0); + } + + /*if ( flag != 0 && height > 0 ) + { + if ( coin->latest.numtxids != lastblock.L.firsttxidind + lastblock.txn_count && iguana_kvtruncate(coin,coin->txids,lastblock.L.firsttxidind + lastblock.txn_count) < 0 ) + err |= 1; + if ( coin->latest.numunspents != lastblock.L.firstvout + lastblock.numvouts && iguana_kvtruncate(coin,coin->unspents,lastblock.L.firstvout + lastblock.numvouts) < 0 ) + err |= 2; + if ( coin->latest.numspends != lastblock.L.firstvin + lastblock.numvins && iguana_kvtruncate(coin,coin->spends,lastblock.L.firstvin + lastblock.numvins) < 0 ) + err |= 4; + if ( coin->latest.numpkhashes != lastblock.L.numpkinds && iguana_kvtruncate(coin,coin->pkhashes,lastblock.L.numpkinds) < 0 ) + err |= 8; + } + else + { + printf("reset counters flag.%d height.%d\n",flag,height); //getchar(); + } + if ( err != 0 ) + return(-err);*/ + checktxid = iguana_txidstr(coin,0,0,txidstr,txidind); + if ( memcmp(checktxid.bytes,txid.bytes,sizeof(txid)) != 0 ) + { + int32_t i; + printf("error kvwrite/read of txid.%s vs %s txidind.%d\n",bits256_str(txid),bits256_str2(checktxid),txidind); + for (i=-10; i<10; i++) + { + iguana_rwtxidind(coin,0,&tx,txidind+i); + printf("txidind.%d %s\n",txidind+i,bits256_str(tx.txid)); + } + getchar(); + return(0); + } + checkind = iguana_txidind(coin,&checkfirstvout,&checkfirstvin,txid); + if ( checkind != txidind || checkfirstvout != firstvout || checkfirstvin != firstvin ) + { + printf("error kvwrite/read of txidind.%d:%d %s firstvout.%d vs %d firstvin.%d vs %d\n",txidind,checkind,bits256_str(txid),firstvout,checkfirstvout,checkfirstvin,firstvin); + getchar(); + return(0); + } + + int32_t iguana_recvinit(struct iguana_info *coin,int32_t initialheight) + { + //int32_t maxi,total; uint64_t allocated,combined = 0; + //portable_mutex_init(&coin->R.RSPACE.mutex); + //memset(&coin->R.RSPACE,0,sizeof(coin->R.RSPACE)); + //coin->R.RSPACE.size = 1024*1024*128; + //coin->R.RSPACE.counter = total != 0 ? maxi+1 : 0; + return(0); + } + + int32_t iguana_initpkhash(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,int32_t itemind,int32_t itemsize,int32_t numitems) + { + int64_t balance; uint64_t credits,debits; int32_t numutxo; uint32_t unspents[64]; struct iguana_pkhash *P = value; + if ( key != 0 && value != 0 && itemind > 0 ) + { + if ( (balance= iguana_balance(coin,&credits,&debits,&numutxo,unspents,sizeof(unspents)/sizeof(*unspents),P,itemind)) < 0 ) + { + printf("iguana_balance error pkind.%d %.8f vs %.8f\n",itemind,dstr(balance),dstr(P->balance)); + getchar(); + return(-1); + } + coin->latest.credits += credits; + coin->latest.debits += debits; + } + return(0); + } + /* + + int64_t iguana_balance(struct iguana_info *coin,uint64_t *creditsp,uint64_t *debitsp,int32_t *nump,uint32_t *unspents,long max,struct iguana_pkhash *P,uint32_t pkind) + { + uint32_t unspentind,spendind,lastunspentind,lastspendind,flag,n = 0; int64_t credits,debits,net = 0; + struct iguana_unspent U; struct iguana_spend S; + *creditsp = *debitsp = net = credits = debits = lastunspentind = lastspendind = flag = 0; + unspentind = P->firstunspentind; + while ( unspentind > 0 ) + { + lastunspentind = unspentind; + if ( iguana_rwunspentind(coin,0,&U,unspentind) == 0 ) + { + credits += U.value; + if ( U.spendind == 0 ) + { + net += U.value; + if ( n < max && unspents != 0 ) + unspents[n] = unspentind; + } + n++; + if ( unspentind != P->lastunspentind && U.nextunspentind > 0 && U.nextunspentind > unspentind && U.nextunspentind < coin->latest.numunspents ) + unspentind = U.nextunspentind; + else + { + if ( U.nextunspentind == 0 ) // cleared during unspents init + { + P->lastunspentind = unspentind = lastunspentind; + flag++; + } + break; + } + } else return(-1); + } + if ( unspentind == P->lastunspentind ) + { + if ( (spendind= P->firstspendind) >= coin->latest.numspends ) + { + P->firstspendind = P->lastspendind = spendind = 0; + flag++; + } + while ( spendind > 0 ) + { + lastspendind = spendind; + if ( iguana_rwspendind(coin,0,&S,spendind) == 0 ) + { + if ( S.unspentind > 0 && S.unspentind < coin->latest.numunspents && iguana_rwunspentind(coin,0,&U,S.unspentind) == 0 ) + { + debits += U.value; + if ( spendind != P->lastspendind && S.nextspendind > 0 && S.nextspendind > spendind && S.nextspendind < coin->latest.numspends ) + spendind = S.nextspendind; + } else S.nextspendind = 0; + if ( S.nextspendind == 0 ) // cleared during spends init + { + P->lastspendind = spendind = lastspendind; + flag++; + break; + } + } else return(-1); + } + if ( flag != 0 ) + { + if ( iguana_rwpkind(coin,1,P,pkind) < 0 ) + printf("error "); + printf("pkind.%d autofix\n",pkind); + P->balance = (credits - debits); + } + if ( net != (credits - debits) ) + printf("iguana_balance: total mismatch %.8f != %.8f (%.8f - %.8f)\n",dstr(net),dstr(credits)-dstr(debits),dstr(credits),dstr(debits)); + *nump = n; + *creditsp = credits; + *debitsp = debits; + return(net); + } else printf("iguana_balance error: unspentind.%u != last.%u\n",unspentind,P->lastunspentind); + *nump = 0; + return(-1); + }*/ + + int32_t iguana_processhdrs(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n) + { + bits256 hash2; int32_t i,flag=0,startheight = -1,height = -1; struct iguana_block space,*block; + if ( startheight >= 0 ) + { + printf("%s received headers %d [%d] %s\n",addr->ipaddr,startheight,n,bits256_str(blocks[0].hash2)); + if ( startheight+n < coin->blocks.hwmheight ) + return(-1); + for (i=0; iblocks.hwmheight ) + continue; + if ( (block= iguana_findblock(coin,&space,blocks[i].hash2)) == 0 || height > coin->blocks.hwmheight ) + { + if ( (height= iguana_addblock(coin,blocks[i].hash2,&blocks[i])) > 0 ) + { + iguana_gotdata(coin,0,blocks[i].height,blocks[i].hash2); + flag++; + } + } else printf("height.%d:%d %s block.%p flag.%d\n",height,blocks[i].height,bits256_str(blocks[i].hash2),block,flag); + } + if ( flag != 0 ) + { + //iguana_queuehdrs(coin,blocks[n-1].height,blocks[n-1].hash2,1); + //iguana_lookahead(coin,&hash2,coin->blocks.hwmheight + 1); + } + } + iguana_lookahead(coin,&hash2,coin->blocks.hwmheight + 1); + return(flag); + } + + /*int32_t iguana_updatewaiting(struct iguana_info *coin,int32_t starti,int32_t max) + { + int32_t i,height,gap,n = 0; uint32_t now; + now = (uint32_t)time(NULL); + height = starti; + for (i=0; iblocks.parsedblocks); + if ( gap >= 0 ) + gap = sqrt(gap); + if ( gap < 1 ) + gap = 1; + if ( height < coin->R.numwaitingbits && coin->R.recvblocks[height] == 0 && now > (coin->R.waitstart[height] + gap) ) + { + //printf("restart height.%d width.%d widthready.%d %s\n",height,coin->width,coin->widthready,bits256_str(iguana_blockhash(coin,height))); + iguana_waitclear(coin,height); + iguana_waitstart(coin,height); + } //else printf("%d %d %p %u\n",height,coin->R.numwaitingbits,coin->R.recvblocks[height],coin->R.waitstart[height]); + } + //printf("height.%d max.%d\n",starti,max); + height = starti; + for (i=0; iR.recvblocks[height] != 0 ) + n++; + return(n); + }*/ + + /*void iguana_queuehdrs(struct iguana_info *coin,int32_t height,bits256 hash2,int32_t forceflag) + { + char hashstr[65]; + if ( memcmp(bits256_zero.bytes,hash2.bytes,sizeof(hash2)) == 0 ) + { + printf("trying to queue null hash\n"); + getchar(); + } + if ( height < 0 ) + forceflag = 1; + if ( (forceflag != 0 && height > coin->blocks.hwmheight-coin->chain->bundlesize) || (height/coin->chain->bundlesize) > (coin->R.topheight/coin->chain->bundlesize) ) + { + printf("queue hdrs height.%d %s\n",height,bits256_str(hash2)); + coin->R.pendingtopheight = coin->R.topheight; + coin->R.pendingtopstart = (uint32_t)time(NULL); + init_hexbytes_noT(hashstr,hash2.bytes,sizeof(hash2)); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1); + } + }*/ + /*if ( block->height >= coin->blocks.parsedblocks ) + { + memset(&space,0,sizeof(space)); + if ( iguana_kvread(coin,coin->blocks.db,0,&space,(uint32_t *)&block->height) != 0 ) + iguana_mergeblock(&space,block); + else printf("iguana_gotblock: cant read block.%d\n",block->height); + iguana_recvblock(coin,addr,&space,txarray,numtx,data,datalen); + iguana_kvwrite(coin,coin->blocks.db,0,&space,(uint32_t *)&space.height); + } // else printf("orphan %d block.%s from gotblockM\n",block->height,bits256_str(block->hash2)); + iguana_waitclear(coin,block->height);*/ + //portable_mutex_unlock(&coin->blocks.mutex); + + /*if ( 1 && coin->R.pendingtopheight == 0 ) + { + for (checkpointi=coin->blocks.hwmheight/coin->chain->bundlesize; checkpointiR.numcheckpoints; checkpointi++) + if ( memcmp(bits256_zero.bytes,coin->R.checkpoints[checkpointi].prevhash2.bytes,sizeof(coin->R.checkpoints[checkpointi])) != 0 ) + iguana_queuehdrs(coin,coin->R.checkpoints[checkpointi].height,coin->R.checkpoints[checkpointi].prevhash2,1); + coin->R.pendingtopheight = 1; + printf("issued initial gethdrs from %d\n",(coin->blocks.hwmheight/coin->chain->bundlesize)*coin->chain->bundlesize); //getchar(); + } + if ( coin->R.topheight < 0 ) + coin->R.topheight = 0; + if ( coin->blocks.hwmheight < 0 ) + coin->blocks.hwmheight = 0; + if ( coin->R.topheight < coin->blocks.hwmheight ) + coin->R.topheight = coin->blocks.hwmheight; + if ( coin->R.topheight == 0 || coin->R.topheight >= coin->R.pendingtopheight+coin->chain->bundlesize || time(NULL) > (coin->R.lasthdrtime + 60) ) + { + memset(hash2.bytes,0,sizeof(hash2)); + if ( coin->R.pendingtopheight != coin->R.topheight ) + { + height = (coin->R.topheight/coin->chain->bundlesize) * coin->chain->bundlesize; + hash2 = coin->R.checkpoints[height / coin->chain->bundlesize].prevhash2; + printf("request new header %d vs %d %u %s\n",height,coin->R.topheight,coin->R.pendingtopstart,bits256_str(hash2)); + if ( memcmp(bits256_zero.bytes,hash2.bytes,sizeof(hash2)) == 0 ) + hash2 = iguana_blockhash(coin,height); + } + if ( memcmp(bits256_zero.bytes,hash2.bytes,sizeof(hash2)) == 0 ) + { + iguana_lookahead(coin,&hash2,1); + if ( coin->blocks.hwmheight < coin->blocks.parsedblocks ) + coin->blocks.parsedblocks = coin->blocks.hwmheight; + height = coin->blocks.parsedblocks; + hash2 = iguana_blockhash(coin,height); + if ( iguana_choosepeer(coin) != 0 ) + printf("hwmchain request new header %d vs %d %u\n",coin->R.pendingtopheight,coin->R.topheight,coin->R.pendingtopstart); + } + coin->R.lasthdrtime = (uint32_t)time(NULL); + if ( memcmp(bits256_zero.bytes,hash2.bytes,sizeof(hash2)) != 0 ) + { + iguana_queuehdrs(coin,height,hash2,1); + return(1); + } + } + if ( coin->newhdrs != 0 ) + { + coin->newhdrs = 0; + height = coin->blocks.hwmheight; + iguana_lookahead(coin,&hash2,height + 1); + if ( coin->blocks.hwmheight > height ) + return(1); + }*/ + /*for (iter=0; iter<2; iter++) + { + while ( (req= queue_dequeue(&addr->pendblocksQ[iter ^ 1],0)) != 0 ) + { + if ( memcmp(&req->hash2,hash2.bytes,sizeof(hash2)) == 0 ) + { + if ( (*heightp= req->height) >= 0 && req->checkpointi >= 0 ) + { + printf("FOUND.(%s) height.%d\n",bits256_str(req->hash2),req->height); + if ( deleteflag == 0 ) + queue_enqueue("pendblocksQ",&addr->pendblocksQ[iter ^ 1],&req->DL,0); + return(&coin->R.checkpoints[req->checkpointi]); + } else printf("height.%d checkpointi.%d\n",req->height,req->checkpointi); + } + printf("requeue.%p\n",req); + queue_enqueue("pendblocksQ",&addr->pendblocksQ[iter ^ 1],&req->DL,0); + } + } + return(0);*/ + + void iguana_queuebundle(struct iguana_info *coin,struct iguana_bundle *bundle) + { + int32_t i; + printf("queue bundle.%p %s height.%d num.%d waitingbits.%d\n",bundle,bits256_str(bundle->prevhash2),bundle->height,bundle->num,coin->R.numwaitingbits); + for (i=0; inum; i++) + { + //printf("bundle[i.%d] %d %s\n",i,bundle->height + 1 + i,bits256_str(bundle->blocks[i].hash2)); + if ( iguana_recvblock(coin,bundle->height + 1 + i) == 0 ) + { + coin->R.blockhashes[bundle->height + 1 + i] = bundle->blocks[i].hash2; + //iguana_queueblock(coin,bundle->height + 1 + i,bundle->blocks[i].hash2,0); + } + } + } + + struct iguana_bundle *iguana_bundleheight(struct iguana_info *coin,int32_t *heightp,bits256 hash2,bits256 prev_block,int32_t deleteflag) + { + //int32_t i,j,miscompare = 0; struct iguana_bundle *bundle; + *heightp = -1; + /* for (i=0; iR.numbundles; i++) + { + if ( i*coin->chain->bundlesize > coin->longestchain ) + { + // printf("i.%d %d < longestchain.%d\n",i,i*coin->chain->bundlesize,coin->longestchain); + break; + } + bundle = &coin->R.bundles[i]; + if ( bundle->height >= 0 && bundle->blocks != 0 ) + { + if ( bundle->recvstart == 0 ) + continue; + // printf("bundlei.%d recvstart.%u finish.%u\n",i,bundle->recvstart,bundle->recvfinish); + if ( memcmp(bundle->prevhash2.bytes,prev_block.bytes,sizeof(prev_block)) == 0 ) + { + *heightp = bundle->height + 1; + return(bundle); + } + for (j=0; jnum; j++) + { + if ( memcmp(bundle->blocks[j].hash2.bytes,hash2.bytes,sizeof(hash2)) == 0 ) + { + *heightp = bundle->height + 1 + j; + //printf("height.%d j.%d (%s) vs (%s) bundle.%d\n",*heightp,j,bits256_str(bundle->blocks[j].hash2),bits256_str2(hash2),bundle->height); + return(bundle); + } else miscompare++;//, printf("%x ",(uint32_t)bundle->blocks[j].hash2.uints[7]); + } + } //else printf("skip bundle.%d %p\n",bundle->height,bundle->blocks); + } + printf("cant find.(%s) miscompares.%d %x\n",bits256_str(hash2),miscompare,(uint32_t)hash2.uints[7]);*/ + return(0); + } + /* + static bits256 lasthash2; + struct iguana_blockreq *req; int32_t height; + addr->lastrequest = bits256_zero; + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + coin->R.lasthdrtime = (uint32_t)time(NULL); + if ( memcmp(lasthash2.bytes,blockhashes[0].bytes,sizeof(lasthash2)) != 0 ) + { + if ( n <= 2 ) + { + printf("gotblockhashes[%d] %s pend.%d\n",n,bits256_str(blockhashes[0]),addr->pendhdrs); + lasthash2 = blockhashes[0]; + } + } + if ( n > 2 ) + { + if ( n > coin->chain->bundlesize ) + printf("warning: %s gotheaders.%d is too many vs. %d\n",coin->symbol,n,coin->chain->bundlesize); + req = mycalloc('r',1,sizeof(*req)); + req->hash2 = blockhashes[0]; + req->blockhashes = blockhashes; + req->n = n; + iguana_bundleheight(coin,&height,blockhashes[0],bits256_zero,0); + if ( req->height >= 0 ) + { + req->bundlei = (req->height / coin->chain->bundlesize); + //printf("blocksQ.%s height.%d\n",bits256_str(blockhashes[0]),height); + //queue_enqueue("blocksQ",&coin->blocksQ,&req->DL,0); + } + else + { + req->bundlei = -1; + //printf("priorityQ.%s height.%d\n",bits256_str(blockhashes[0]),height); + //queue_enqueue("priorityQ",&coin->priorityQ,&req->DL,0); + } + printf("blocksQ.%s height.%d req->height.%d\n",bits256_str(blockhashes[0]),height,req->height); + queue_enqueue("blocksQ",&coin->blocksQ,&req->DL,0); + } else myfree(blockhashes,n * sizeof(*blockhashes)); + } + + + int32_t h,i,height; uint32_t now; bits256 prevhash2; //char hashstr[65]; + struct iguana_blockreq *req; struct iguana_bundle *bundle; + + iguana_gotdata(coin,addr,block->height,block->hash2); + now = (uint32_t)time(NULL); + bundle = iguana_bundleheight(coin,&height,block->hash2,block->prev_block,1); + //printf("%s got block.%d height.%d\n",addr!=0?addr->ipaddr:"local",block->height,height); + if ( (req= queue_dequeue(&addr->pendingQ,0)) != 0 ) // should only have depth 1! + { + if ( memcmp(req->hash2.bytes,block->hash2.bytes,sizeof(req->hash2)) == 0 ) + { + if ( req->blockhashes != 0 ) + { + iguana_gotdata(coin,addr,block->height,block->hash2); + iguana_bundleinit(coin,block->height-1,block->prev_block); + if ( (bundle= iguana_bundle(coin,block->prev_block)) != 0 ) + { + portable_mutex_lock(&bundle->mutex); + if ( bundle->blocks == 0 ) + { + bundle->blockhashes = req->blockhashes; + bundle->num = req->n; + bundle->bundlei = (block->height / coin->chain->bundlesize); + bundle->firsthash2 = block->hash2; + bundle->lasthash2 = req->blockhashes[req->n-1]; + bundle->height = block->height - 1; + bundle->blocks = mycalloc('B',req->n,sizeof(*bundle->blocks)); + bundle->blocks[0] = *block; + prevhash2 = block->prev_block; + for (i=0; in; i++) + { + height = (bundle->height + 1 + i); + bundle->blocks[i].prev_block = prevhash2; + bundle->blocks[i].hash2 = req->blockhashes[i]; + prevhash2 = req->blockhashes[i]; + if ( (height % coin->chain->bundlesize) == 0 ) + iguana_bundleinit(coin,height,req->blockhashes[i]); + } + printf("initialized bundlei.%d %d\n",bundle->bundlei,bundle->height); + } + portable_mutex_unlock(&bundle->mutex); + } else printf("couldnt find matching bundle for %s\n",bits256_str(block->prev_block)); + myfree(req->blockhashes,req->n * sizeof(*req->blockhashes)); + myfree(req,sizeof(*req)); + } else printf("unexpected missing blockhashes.%p\n",req->blockhashes); + } else printf("unexpected hash2 mismatch with height.%d\n",block->height); + } + else + { + if ( bundle == 0 ) + { + printf("cant find bundle.(%s)\n",bits256_str(block->hash2)); + return; + } + if ( height > bundle->height && height <= bundle->height+bundle->num ) + { + h = height - bundle->height - 1; + portable_mutex_lock(&bundle->mutex); + if ( bundle->numvalid < bundle->num && bundle->txdata[h] == 0 ) + { + bundle->blocks[h] = *block; + if ( iguana_recvblockptr(coin,height) == &bundle->txdata[h] && bundle->txdata[h] == 0 ) + { + bundle->txdata[h] = txarray, bundle->numtxs[h] = numtx; + coin->blocks.numblocks++; + //if ( (rand() % 100) == 0 ) + printf("GOT.%d | received.%d total.%d | %.2f minutes\n",height,coin->blocks.recvblocks,coin->blocks.numblocks,(double)(now - coin->starttime)/60.); + txarray = 0; + if ( ++bundle->numvalid == bundle->num ) + { + bundle->recvfinish = now; + bundle->lastduration = (bundle->recvfinish - bundle->recvstart); + dxblend(&coin->R.avetime,bundle->lastduration,.9); + if ( bundle->lastduration < coin->R.avetime ) + coin->R.faster++; + else coin->R.slower++; + if ( coin->R.faster > 3*coin->R.slower || coin->R.slower > 3*coin->R.faster ) + { + dir = (coin->R.maxrecvbundles - coin->R.prevmaxrecvbundles); + if ( coin->R.slower >= coin->R.faster ) + dir = -dir; + if ( dir > 0 ) + dir = 1; + else if ( coin->R.maxrecvbundles > 2 ) + dir = -1; + else dir = 0; + printf("(%d vs %f) faster.%d slower.%d -> dir.%d apply -> %d\n",bundle->lastduration,coin->R.avetime,coin->R.faster,coin->R.slower,dir,coin->R.maxrecvbundles + dir); + coin->R.prevmaxrecvbundles = coin->R.maxrecvbundles; + coin->R.maxrecvbundles += dir; + coin->R.slower = coin->R.faster = 0; + } + coin->R.finishedbundles++; + printf("submit emit.%d height.%d\n",bundle->bundlei,bundle->height); + queue_enqueue("emitQ",&coin->emitQ,&bundle->DL,0); + } + else + { + if ( coin->R.waitstart[height] > 0 ) + { + if ( bundle->firstblocktime == 0 ) + bundle->firstblocktime = now; + bundle->durationsum += (now - coin->R.waitstart[height] + 1); + bundle->aveduration = (bundle->durationsum / bundle->numvalid); + } + } + } else printf("recvblockptr error? height.%d %p %p h.%d\n",height,iguana_recvblockptr(coin,height),&bundle->txdata[h],h); + } else if ( (rand() % 1000) == 0 ) + printf("interloper! already have txs[%d] for bundlei.%d\n",h,bundle!=0?bundle->height:-1); + portable_mutex_unlock(&bundle->mutex); + } else printf("height.%d outside range of bundlei.%d %d\n",height,bundle!=0?bundle->height:-1,bundle!=0?bundle->height:-1); + } + //iguana_waitclear(coin,block->height); + if ( 1 && (rand() % 1000) == 0 ) + printf("%-15s pend.(%d %d) got block.%-6d recvblocks %-8.0f recvtotal %-10.0f\n",addr->ipaddr,addr->pendhdrs,addr->pendblocks,block->height,addr->recvblocks,addr->recvtotal); + } + if ( txarray != 0 ) + iguana_freetx(txarray,numtx); + myfree(block,sizeof(*block)); + }*/ + + + /* + if ( (bp= iguana_bundleinit(coin,-1,blocks[0].prev_block)) != 0 ) + { + if ( n > coin->chain->bundlesize ) + printf("warning: %s gotheaders.%d is too many vs. %d\n",coin->symbol,n,coin->chain->bundlesize); + portable_mutex_lock(&bundle->mutex); + if ( bundle->blocks == 0 ) + { + bundle->num = n; + bundle->blocks = blocks; + bundle->firsthash2 = blocks[0].hash2; + bundle->lasthash2 = blocks[n-1].hash2; + for (i=0; iheight + i + 1); + iguana_gotdata(coin,addr,blocks[i].height,blocks[i].hash2); + if ( (blocks[i].height % coin->chain->bundlesize) == 0 ) + iguana_bundleinit(coin,blocks[i].height,blocks[i].hash2); + } + printf("%s set bundle.%d %s\n",addr->ipaddr,bundle->height,bits256_str(blocks[0].prev_block)); + } + portable_mutex_unlock(&bundle->mutex); + } else printf("ERROR iguana_gotheaders got bundle.(%s) n.%d that cant be found?\n",bits256_str(blocks[0].prev_block),n); + }*/ + + int32_t iguana_updatehdrs(struct iguana_info *coin) + { + int32_t flag = 0; + int32_t i,j,m,height,run,flag = 0; uint32_t now; struct iguana_bundle *bundle; + if ( iguana_needhdrs(coin) == 0 ) + return(flag); + now = (uint32_t)time(NULL); + run = -1; + for (i=0; iR.numbundles; i++) + { + if ( i*coin->chain->bundlesize > coin->longestchain ) + break; + bundle = &coin->R.bundles[i]; + if ( bundle->blocks != 0 ) + { + if ( bundle->recvstart == 0 ) + { + if ( (coin->R.startedbundles - coin->R.finishedbundles) < coin->R.maxrecvbundles ) + { + iguana_queuebundle(coin,bundle); + bundle->recvstart = now; + coin->R.startedbundles++; + printf("startbundle.%d (%d - %d)\n",bundle->height,coin->R.startedbundles,coin->R.finishedbundles); + flag++; + } + } + else if ( bundle->recvfinish == 0 ) + { + for (j=m=0; jnum; j++) + { + height = bundle->height+j+1; + if ( iguana_recvblock(coin,height) != 0 ) + m++; + else if ( coin->R.waitstart[height] > 0 ) + { + duration = (now - coin->R.waitstart[height]); + if ( duration > 60 || (duration > 10 && bundle->numvalid > 13 && duration > 3.*bundle->aveduration) ) + { + if ( now > bundle->lastdisp+15 ) + printf("height.%d in bundle.%d duration.%d vs ave %.3f\n",height,bundle->height,duration,bundle->aveduration); + iguana_waitclear(coin,height); + iguana_waitstart(coin,height,bundle->blocks[j].hash2,1); + } + } + else if ( bundle->firstblocktime > 0 && (now - bundle->firstblocktime) > 60 ) + { + if ( now > bundle->lastdisp+15 ) + printf("height.%d in bundle.%d ave %.3f\n",height,bundle->height,bundle->aveduration); + iguana_waitclear(coin,height); + iguana_waitstart(coin,height,bundle->blocks[j].hash2,1); + bundle->firstblocktime = now; + } + } + if ( 0 && now > bundle->lastdisp+15 ) + { + printf("bundle.%d (%d %d) elapsed.%d (%d - %d) %d | %.2f minutes\n",bundle->bundlei,bundle->height,m,(int32_t)(now - bundle->recvstart),coin->R.startedbundles,coin->R.finishedbundles,coin->R.maxrecvbundles,(double)(now - coin->starttime)/60.); + bundle->lastdisp = now; + } + } + else if ( run == i-1 ) + run++; + } + } + //iguana_lookahead(coin,&hash2,0); + return(flag); + }*/ + + /*struct iguana_block *iguana_block(struct iguana_info *coin,struct iguana_block *space,int32_t height) + { + if ( height <= coin->blocks.hwmheight ) + { + if ( iguana_kvread(coin,coin->blocks.db,0,space,(uint32_t *)&height) != 0 ) + { + if ( bits256_nonz(space->hash2) != 0 ) + return(space); + if ( height < coin->blocks.hwmheight ) + { + printf("height.%d null blockhash? prev.%s\n",height,bits256_str(space->prev_block)); + getchar(); + } + return(0); + } else printf("error doing RWmmap\n"); + } + //printf("iguana_block hwmheight.%d vs height.%d\n",coin->blocks.hwmheight,height); + return(0); + } + + struct iguana_block *iguana_findblock(struct iguana_info *coin,struct iguana_block *space,bits256 hash2) + { + struct iguana_block *block = 0; uint32_t itemind; + if ( bits256_nonz(hash2) != 0 ) + { + block = iguana_kvread(coin,coin->blocks.db,hash2.bytes,space,&itemind); + //printf("iguana_findblock block.%p itemind.%d\n",block,itemind); + if ( block == 0 || itemind != block->height ) + { + if ( block != 0 && block->height != itemind ) + { + printf("iguana_findblock (%s) error itemind.%d vs %d block.%p\n",bits256_str(hash2),itemind,block!=0?block->height:-1,block); + getchar(); + } + return(0); + } + } + return(block); + }*/ + struct iguana_bundle *iguana_bundlefindprev(struct iguana_info *coin,int32_t *heightp,bits256 prevhash2) + { + struct iguana_block *block; + *heightp = -1; + if ( (block= iguana_blockfind(coin,prevhash2)) != 0 ) + { + *heightp = block->hh.itemind; + if ( block->bundle == 0 ) + { + if ( *heightp == 0 ) + block->bundle = coin->B[0]; + else block->bundle = coin->B[(block->hh.itemind - 1) / coin->chain->bundlesize]; + } + return(block->bundle); + } + else return(0); + } + + /*int32_t iguana_bundleset(struct iguana_info *coin,int32_t origheight,bits256 hash2) + { + int32_t bundlei,blocki,height = origheight; struct iguana_bundle *bp = 0; + //printf("bundleset.(%d %s)\n",height,bits256_str(hash2)); + if ( (height % coin->chain->bundlesize) == 0 && height > 0 ) + { + iguana_blockhashset(coin,origheight,hash2,0); + return(0); + } + if ( (bundlei= iguana_bundlei(coin,&blocki,height)) >= 0 && bundlei >= 0 && (bp= coin->B[bundlei]) != 0 ) + { + if ( height > bp->height && height < bp->height+bp->num ) + { + if ( iguana_blockhashset(coin,origheight,hash2,bp) != 0 ) + { + return(0); + } + printf("iguana_bundleset error setting bundle height.%d %s\n",height,bits256_str(hash2)); + } else printf("iguana_bundleset illegal height.%d for bundle.%d\n",height,bp->height); + } else printf("iguana_bundleset illegal height.%d bundlei.%d blocki.%d bp.%p\n",height,bundlei,blocki,bp); + return(-1); + }*/ + + /*int32_t iguana_bundlei(struct iguana_info *coin,int32_t *blockip,int32_t height) + { + int32_t bundlei; + *blockip = -1; + if ( height <= 0 || height > coin->R.numwaitingbits ) + return(-1); + height--; + *blockip = (height % coin->chain->bundlesize); + if ( (bundlei= (height / coin->chain->bundlesize)) < IGUANA_MAXBUNDLES ) + return(bundlei); + else return(-1); + } + + void **iguana_recvblockptr(struct iguana_info *coin,int32_t *blockip,int32_t height) + { + int32_t bundlei; struct iguana_bundle *bp; + if ( (bundlei= iguana_bundlei(coin,blockip,height)) >= 0 ) + { + if ( (bp= coin->B[bundlei]) != 0 ) + return(&bp->txdata[*blockip]); + } + return(0); + }*/ + + /*struct iguana_bundle *iguana_bundleinit(struct iguana_info *coin,int32_t height,bits256 hash2) + { + int32_t bundlei,blocki; struct iguana_bundle *bp = 0; + if ( height < 0 || (height % coin->chain->bundlesize) != 0 ) + { + printf("bundleinit error: height.%d %s\n",height,bits256_str(hash2)); + return(bp); + } + portable_mutex_lock(&coin->bundles_mutex); + if ( (bundlei= iguana_bundlei(coin,&blocki,height+1)) >= 0 ) + { + if ( (bp= coin->B[bundlei]) != 0 ) + { + if ( memcmp(hash2.bytes,bp->prevhash2.bytes,sizeof(hash2)) != 0 ) + { + if ( bits256_nonz(hash2) > 0 ) + { + if ( bits256_nonz(bp->prevhash2) > 0 ) + { + printf("bundleinit[%d]: %d hash conflict have %s, got %s\n",bp->bundlei,bp->height,bits256_str(bp->prevhash2),bits256_str2(hash2)); + //getchar(); + portable_mutex_unlock(&coin->bundles_mutex); + return(0); + } + bp->prevhash2 = hash2; + iguana_blockhashset(coin,height,hash2,1); + printf("bundleinit: set starting hash.(%s) for %d\n",bits256_str(hash2),bp->height); + } + } + } + else + { + bp = mycalloc('b',1,sizeof(*bp)); + coin->B[bundlei] = bp; // cant change values once set to nonzero + bp->prevhash2 = hash2; + bp->bundlei = bundlei; + bp->hasheaders = coin->chain->hasheaders; + bp->num = coin->chain->bundlesize; + bp->height = (bundlei * coin->chain->bundlesize); + bp->starttime = (uint32_t)time(NULL); + if ( bits256_nonz(hash2) > 0 ) + { + iguana_blockhashset(coin,height,hash2,1); + printf("created bundle.%d: %s coin->B[%d] <- %p\n",height,bits256_str(hash2),bundlei,bp); + } + } + } + portable_mutex_unlock(&coin->bundles_mutex); + return(bp); + }*/ + int32_t iguana_waitstart(struct iguana_info *coin,int32_t height,bits256 hash2,int32_t priority) + { + if ( height < 0 || iguana_recvblock(coin,height) == 0 ) + return(iguana_queueblock(coin,height,hash2,priority)); + else if ( height < coin->maxblockbits ) + printf("iguana_waitstart ignore height.%d < %d, %p GETBIT.%d\n",height,coin->maxblockbits,iguana_recvblock(coin,height),GETBIT(coin->R.waitingbits,height)); + return(0); + } + + int32_t iguana_waitclear(struct iguana_info *coin,int32_t height) + { + if ( height < coin->maxblockbits ) + { + //printf("%d waitclear.%d parsed.%d\n",coin->R.numwaiting,height,coin->blocks.recvblocks); + if ( coin->R.numwaiting > 0 ) + coin->R.numwaiting--; + coin->R.waitstart[height] = 0; + CLEARBIT(coin->R.waitingbits,height); + return(0); + } + return(-1); + } + + int32_t iguana_updatewaiting(struct iguana_info *coin,int32_t starti,int32_t max) + { + int32_t i,height,gap,n = 0; uint32_t now; + now = (uint32_t)time(NULL); + height = starti; + iguana_waitclear(coin,height); + iguana_waitstart(coin,height,coin->R.blockhashes[height],1); + for (i=0; iblocks.recvblocks); + if ( gap >= 0 ) + gap = sqrt(gap); + if ( gap < 13 ) + gap = 13; + if ( height < coin->maxblockbits && iguana_recvblock(coin,height) == 0 && now > (coin->R.waitstart[height] + gap) && memcmp(bits256_zero.bytes,coin->R.blockhashes[height].bytes,sizeof(bits256)) != 0 ) + { + //printf("restart height.%d width.%d widthready.%d %s\n",height,coin->width,coin->widthready,bits256_str(coin->R.blockhashes[height])); + iguana_waitclear(coin,height); + iguana_waitstart(coin,height,coin->R.blockhashes[height],0); + } //else printf("%d %d %p %u\n",height,coin->maxblockbits,coin->R.recvblocks[height],coin->R.waitstart[height]); + } + //printf("height.%d max.%d\n",starti,max); + height = starti; + for (i=0; iwidth = width = 4*sqrt(coin->longestchain - coin->blocks.recvblocks); + if ( coin->width < 0 ) + width = 500; + coin->widthready = 0; + coin->width = 5000; + //printf("width.%d\n",width); + while ( iguana_recvblock(coin,coin->blocks.recvblocks) != 0 ) + { + coin->blocks.recvblocks++; + //printf("RECV.%d\n",coin->blocks.recvblocks); + } + while ( width < (coin->longestchain - coin->blocks.recvblocks) ) + { + w = iguana_updatewaiting(coin,coin->blocks.recvblocks,width); + //printf("w%d ",w); + if ( width == coin->width ) + coin->widthready = w; + //else + break; + width <<= 1; + if ( width >= coin->longestchain-coin->blocks.recvblocks ) + width = coin->longestchain-coin->blocks.recvblocks-1; + if ( (rand() % 100) == 0 && width > (coin->width<<2) ) + printf("coin->width.%d higher width.%d all there, w.%d\n",coin->width,width,w); + } + return((uint32_t)time(NULL)); + } + int32_t iguana_connectsocket(int32_t blockflag,struct iguana_peer *A,struct sockaddr *addr,socklen_t addr_len) + { + int32_t opt,flags; struct timeval timeout; //,val = 65536*2 + if ( A->usock >= 0 ) + { + printf("iguana_connectsocket: (%s) already has usock.%d\n",A->ipaddr,A->usock); + return(-1); + } + if ( A->ipv6 != 0 ) + A->usock = socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP); + else A->usock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); + if ( A->usock >= 0 ) + { + //setsockopt(A->usock,SOL_SOCKET,SO_SNDBUF,&val,sizeof(val)); + //setsockopt(A->usock,SOL_SOCKET,SO_RCVBUF,&val,sizeof(val)); + timeout.tv_sec = 0; + timeout.tv_usec = 1000; + setsockopt(A->usock,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout,sizeof(timeout)); + setsockopt(A->usock,SOL_SOCKET,SO_SNDTIMEO,(char *)&timeout,sizeof(timeout)); + opt = 1; + setsockopt(A->usock,SOL_SOCKET,SO_REUSEADDR,(void*)&opt,sizeof(opt)); + //retval = setsockopt(A->usock,SOL_SOCKET,SO_NOSIGPIPE,&opt,sizeof(opt)); + //printf("nosigpipe retval.%d\n",retval); + if ( blockflag != 0 || ((flags= fcntl(A->usock,F_GETFL,0)) >= 0 && fcntl(A->usock,F_SETFL,flags|O_NONBLOCK) >= 0) ) + { + if ( connect(A->usock,addr,addr_len) >= 0 || errno == EINPROGRESS ) + return(A->usock); + else fprintf(stderr,"usock %s connect -> errno.%d\n",A->ipaddr,errno); + }// else fprintf(stderr,"usock %s fcntl -> flags.%d errno.%d",ipaddr,flags,errno); + } else fprintf(stderr,"usock %s -> errno.%d\n",A->ipaddr,errno); + return(-errno); + } + + int32_t iguana_connect(struct iguana_info *coin,struct iguana_peer *addrs,int32_t maxaddrs,char *ipaddr,uint16_t default_port,int32_t connectflag) + { + struct sockaddr *addr; struct sockaddr_in6 saddr6; struct sockaddr_in saddr4; uint32_t ipbits; + struct addrinfo hints,*res; socklen_t addr_len; struct addrinfo *ai; int32_t retval = -1,status,n = 0; + addrs[n].usock = -1; + memset(&hints,0,sizeof(hints)); + memset(&saddr6,0,sizeof(saddr6)); + memset(&saddr4,0,sizeof(saddr4)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + //printf("getaddrinfo\n"); + if ( getaddrinfo(ipaddr,NULL,&hints,&res)) + { + printf("cant get addrinfo for (%s)\n",ipaddr); + return(-1); + } + for (ai=res; ai!=NULL&&nai_next) + { + if ( ai->ai_family == AF_INET6 ) + { + struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)ai->ai_addr; + memcpy(&addrs[n].A.ip,&saddr->sin6_addr,16); + memset(&saddr6,0,sizeof(saddr6)); + saddr6.sin6_family = AF_INET6; + memcpy(&saddr6.sin6_addr.s6_addr,&addrs[n].A.ip[0],16); + saddr6.sin6_port = htons(default_port); + addrs[n].ipv6 = 1; + addr = (struct sockaddr *)&saddr6; + addr_len = sizeof(saddr6); + } + else if ( ai->ai_family == AF_INET ) + { + struct sockaddr_in *saddr = (struct sockaddr_in *)ai->ai_addr; + memset(&addrs[n].A.ip[0],0,10); + memset(&addrs[n].A.ip[10],0xff,2); + memcpy(&addrs[n].A.ip[12],&saddr->sin_addr,4); + memset(&saddr4,0,sizeof(saddr4)); + saddr4.sin_family = AF_INET; + memcpy(&saddr4.sin_addr.s_addr,&addrs[n].A.ip[12],4); + saddr4.sin_port = htons(default_port); + addrs[n].ipv6 = 0; + addr = (struct sockaddr *)&saddr4; + addr_len = sizeof(saddr4); + } else return(-1); + addrs[n].A.nTime = (uint32_t)(time(NULL) - (24 * 60 * 60)); + addrs[n].A.port = default_port; + strcpy(addrs[n].ipaddr,ipaddr); + addrs[n].A.nServices = 0; + n++; + if ( connectflag != 0 ) + { + ipbits = (uint32_t)calc_ipbits(ipaddr); + addrs[n].usock = -1; + addrs[n].ipbits = ipbits; + strcpy(addrs[n].ipaddr,ipaddr); + //printf("call connectsocket\n"); + if ( (addrs[n].usock= iguana_connectsocket(connectflag > 1,&addrs[n],addr,addr_len)) < 0 ) + { + status = IGUANA_PEER_KILLED; + printf("refused PEER STATUS.%d for %s usock.%d\n",status,ipaddr,retval); + iguana_iAkill(coin,&addrs[n],1); + if ( iguana_rwipbits_status(coin,1,ipbits,&status) == 0 ) + printf("error updating status.%d for %s\n",status,ipaddr); + } + else + { + status = IGUANA_PEER_READY; + printf("CONNECTED! PEER STATUS.%d for %s usock.%d\n",status,ipaddr,addrs[n].usock); + iguana_iAconnected(coin,&addrs[n]); + if ( iguana_rwipbits_status(coin,1,ipbits,&status) == 0 ) + printf("error updating status.%d for %s\n",status,ipaddr); + else retval = addrs[n].usock; + } + break; + } + } + freeaddrinfo(res); + return(retval); + } + + /*if ( (fp= fopen(fname,"r")) != 0 ) + { + if ( fgets(line,sizeof(line),fp) > 0 ) + { + line[strlen(line)-1] = 0; + if ( atoi(line) > coin->blocks.hashblocks ) + { + //printf("skip save since %s has %d\n",fname,atoi(line)); + fclose(fp); + return(0); + } + } + fclose(fp); + }*/ + + /*struct iguana_bundle + { + struct queueitem DL; portable_mutex_t mutex; + char fname[512]; struct iguana_mappedptr M; + void *txdata[_IGUANA_HDRSCOUNT]; int32_t numtxs[_IGUANA_HDRSCOUNT]; + struct iguana_counts presnapshot,postsnapshot; + int32_t bundlei,height,num,hasheaders,numvalid,havehashes; + uint32_t starttime,emitstart,emitfinish,lastdisp; + bits256 prevhash2,firsthash2,lasthash2; + //double durationsum,aveduration; + //struct iguana_block *blocks; + };*/ + + /*struct iguana_recv + { + //uint8_t compressed[IGUANA_MAXPACKETSIZE],decompressed[IGUANA_MAXPACKETSIZE],checkbuf[IGUANA_MAXPACKETSIZE]; + long srcdatalen,compressedtotal; //uint64_t histo[0x100]; + struct iguana_memspace RSPACE,*oldRSPACE; int32_t numold; + int64_t packetsallocated,packetsfreed; int32_t numwaiting,maprecvdata; + uint8_t *waitingbits; uint32_t numwaitingbits,*waitstart; //struct iguana_pending **recvblocks; + int32_t topheight,pendingtopheight; + uint32_t pendingtopstart,numbundles,lasthdrtime,startedbundles,finishedbundles; + bits256 tophash2; + //int32_t prevmaxrecvbundles,maxrecvbundles,faster,slower; double avetime; + }; + + struct iguana_pending + { + int32_t next,numtx,datalen,origdatalen; struct iguana_block block; uint32_t allocsize,ipbits; uint8_t data[]; + };*/ + //struct iguana_recv R; + //struct iguana_bundle *B[IGUANA_MAXBUNDLES]; + //struct iguana_blockhashes pendings[1024]; + /* int32_t height; + height = iguana_blockheight(coin,blockhashes[0]); + if ( n > 2 && iguana_needhdrs(coin) > 0 ) + { + //printf("got blockhashes[%d] %s height.%d\n",n,bits256_str(blockhashes[0]),height); + if ( height >= 0 ) + { + for (j=0; jchain->bundlesize && height+jlongestchain; j++) + { + iguana_bundleset(coin,height+j,blockhashes[j]); + iguana_gotdata(coin,0,height+j,blockhashes[j],j,n); + } + } + else + { + iguana_queueblock(coin,-1,blockhashes[0],1); + for (i=0; inumpendings; i++) + if ( memcmp(coin->pendings[i].blockhashes[0].bytes,blockhashes[0].bytes,sizeof(bits256)) == 0 ) + break; + if ( i == coin->numpendings ) + { + if ( coin->numpendings < sizeof(coin->pendings)/sizeof(*coin->pendings) ) + { + coin->pendings[coin->numpendings].blockhashes = blockhashes; + coin->pendings[coin->numpendings].n = n; + coin->pendings[coin->numpendings].starttime = (uint32_t)time(NULL); + coin->numpendings++; + printf("ADD to numpendings.%d priority.(%s) n.%d\n",coin->numpendings,bits256_str(blockhashes[0]),n); + blockhashes = 0; + } else printf("updatebundles: overflowed pendings\n"); + } + } + }*/ + + /* if ( iguana_bundlefindprev(coin,&height,blocks[0].prev_block) != 0 && height >= 0 ) + { + //printf(">>>>>> found %s height.%d n.%d\n",bits256_str(blocks[0].prev_block),height,n); + height++; + for (i=0; ichain->bundlesize && heightlongestchain; i++,height++) + { + //printf("i.%d height.%d\n",i,height); + iguana_bundleset(coin,height,blocks[i].hash2); + iguana_gotdata(coin,req->addr,height,blocks[i].hash2,i,n); + if ( height >= coin->blocks.hwmheight ) + { + if ( height == coin->blocks.hwmheight ) + (*newhwmp)++; + if ( (block= iguana_block(coin,height)) != 0 ) + iguana_mergeblock(block,&blocks[i]); + else printf("unexpected null block at height.%d\n",height), getchar(); + } + else + { + // verify it doesnt trigger reorg (and is recent enough!) + } + } + } else printf("unexpected bundlefind error %s height.%d\n",bits256_str(blocks[0].prev_block),height), getchar(); + */ + + /* int32_t height; + //printf("%s got block.(%s) height.%d\n",req->addr!=0?req->addr->ipaddr:"local",bits256_str(block->hash2),height); + if ( (height= iguana_bundleheight(coin,block)) > 0 ) + { + if ( (ptrp= iguana_blockptrptr(coin,&blocki,height)) != 0 ) + { + if ( (*ptrp) == 0 ) + { + //printf("height.%d tx.%p blocki.%d txarray.%p[%d] (%p[%d] %p[%d])\n",height,&txarray[0],blocki,txarray,numtx,txarray[0].vouts,txarray[0].tx_out,txarray[0].vins,txarray[0].tx_in); + (*ptrp) = (void *)txarray; + bp->numtxs[blocki] = numtx; + if ( bp->emitstart == 0 && ++bp->numvalid >= bp->num ) + { + bp->emitstart = (uint32_t)time(NULL); + iguana_emittxdata(coin,bp); + //printf("queue txarray.%p[%d]\n",txarray,numtx); + //queue_enqueue("emitQ",&coin->emitQ,&bp->DL,0); + } + //txarray = 0; + } + } + else printf("cant get ptrp.%d\n",height), getchar(); + iguana_gotdata(coin,req->addr,height,block->hash2,0,0); + if ( bp != 0 && iguana_bundleready(coin,height-1) <= 0 ) + { + printf("check for pendings.%d height.%d\n",coin->numpendings,height); + if ( height == coin->blocks.hwmheight ) + (*newhwmp)++; + for (i=0; inumpendings; i++) + if ( memcmp(coin->pendings[i].blockhashes[0].bytes,block->hash2.bytes,sizeof(block->hash2)) == 0 ) + { + blockhashes = coin->pendings[i].blockhashes; + n = coin->pendings[i].n; + printf("pending[%d].%d bundlesets[%d] %d %s\n",i,coin->numpendings,n,height,bits256_str(blockhashes[0])); + for (j=0; jchain->bundlesize && height+jlongestchain; j++) + { + iguana_bundleset(coin,height+j,blockhashes[j]); + iguana_gotdata(coin,0,height+j,blockhashes[j],j,n); + } + myfree(blockhashes,n * sizeof(*blockhashes)); + coin->pendings[i] = coin->pendings[--coin->numpendings]; + break; + } + // queue tx for processing + } + else + { + // probably new block + //printf("couldnt find.(%s)\n",bits256_str(block->hash2)); + } + }*/ + //if ( height >= 0 ) + // coin->blocks.ptrs[height] = block; + //printf("found.%s -> %s %d %p inputht.%d\n",bits256_str(hash2),bits256_str(block->hash2),block->hh.itemind,block,height); + if ( height < 0 || block->hh.itemind == height ) + { + if ( (int32_t)block->hh.itemind < 0 ) + { + //printf("found.%s -> %d %p set height.%d matches.%d\n",bits256_str(hash2),block->hh.itemind,block,height,block->matches); + if ( height >= 0 && block->matches == 0 ) + block->hh.itemind = height, block->matches = 1; + else block = 0; + } + if ( block != 0 ) + { + if ( block->matches < 100 ) + block->matches++; + //_iguana_blocklink(coin,block); + } + } + else if ( block->matches == 0 && block->hh.itemind == (uint32_t)-1 ) + { + if ( height >= 0 ) + { + if ( (rand() % 10000) == 0 ) + printf("set %s.itemind <- %d\n",bits256_str(hash2),height); + block->hh.itemind = height; + block->matches = 1; + } + else + { + printf("matches.%d itemind.%d when height.%d\n",block->matches,block->hh.itemind,height); + block = 0; + } + } + else + { + /*if ( block->matches < 100 ) + { + block->matches >>= 1; + if ( block->matches == 0 ) + { + //printf("collision with (%s) itemind.%d vs %d | matches.%d\n",bits256_str(hash2),block->hh.itemind,height,block->matches); + block->hh.itemind = -1; + for (i=0; i<1; i++) + iguana_queueblock(coin,-1,block->hash2,1); + block = 0; + //coin->blocks.recvblocks = 0; + } + } else block = 0;*/ + } + + /*int32_t iguana_blockheight(struct iguana_info *coin,struct iguana_block *block) + { + int32_t height; + if ( (height= iguana_itemheight(coin,block->hash2)) < 0 ) + { + if ( (height= iguana_itemheight(coin,block->prev_block)) < 0 ) + { + iguana_blockhashset(coin,-1,block->hash2,1); + iguana_blockhashset(coin,-1,block->prev_block,1); + } + else + { + height++; + iguana_blockhashset(coin,height,block->hash2,1); + } + } else iguana_blockhashset(coin,height,block->hash2,1); // increments matches + return(height); + }*/ + if ( (height= iguana_itemheight(coin,blockhashes[0])) >= 0 ) + { + if ( iguana_needhdrs(coin) > 0 && iguana_havetxdata(coin,h) == 0 ) + iguana_queueblock(coin,height,blockhashes[0],1); + } + else if ( iguana_needhdrs(coin) > 0 && iguana_havetxdata(coin,h) == 0 ) + iguana_queueblock(coin,-1,blockhashes[0],1); + //printf("check.%s height.%d\n",bits256_str(blockhashes[0]),height); + for (i=0; i= 0 ) + h = height++; + permblock = iguana_blockhashset(coin,h,blockhashes[i],1); + if ( h >= 0 && permblock != 0 ) + { + if ( iguana_blockptr(coin,h) == 0 ) + { + coin->blocks.ptrs[h] = permblock; + { + int32_t j,m; + for (j=m=0; jlongestchain; j++) + if ( iguana_blockptr(coin,j) != 0 ) + m++; + printf("set (%s) <- %d %p m.%d\n",bits256_str(blockhashes[i]),h,permblock,m); + } + } + if ( permblock->hh.itemind != h ) + permblock->hh.itemind = h; + } + if ( i == coin->chain->bundlesize-1 ) + { + init_hexbytes_noT(hashstr,blockhashes[i].bytes,sizeof(blockhashes[i])); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1); + } + //else //if ( h >= 0 ) printf("unexpected missing permblock for h.%d\n",h); + // iguana_queueblock(coin,-1,blockhashes[i],0); + } + for (i=m=run=0; i= 0 ) + { + if ( run == i && (i == 0 || height == height0+i) ) + run++; + if ( (block= iguana_blockfind(coin,blockhashes[i])) != 0 && block == blocks[i] && block->mainchain == 0 ) + { + if ( block->hh.itemind != height && wt > block->matches ) + { + printf("%p[%d] <- %d matches.%d wt.%d vs matches.%d\n",block,block->hh.itemind,height,block->matches,wt,block->matches); + if ( block->matches < 100 ) + { + block->matches = 1; + block->hh.itemind = height; + } + else printf("height conflict for %s\n",bits256_str(blockhashes[i])); + } + } else blocks[i] = 0; + if ( i == 0 ) + height0 = height; + height -= i; + if ( m > 0 ) + { + for (j=0; j %d\n",j,m,wt,heightwts[j][1]); + break; + } + } + } else j = 0; + if ( j == m ) + { + heightwts[m][0] = height; + heightwts[m][1] = wt; + m++; + } + } + printf("i.%d j.%d m.%d height.%d wt.%d %s\n",i,j,m,height,wt,bits256_str(blockhashes[0])); + } + nlinks = plinks = 0; + if ( m > 0 ) + { + if ( m == 1 && height0 >= 0 ) + { + wt = (heightwts[0][1] / num) + 2; + for (i=0; ihh.next == 0 || prev->matches < wt ) + prev->hh.next = blocks[0]; + } + for (i=plinks=nlinks=0; ihh.itemind < 0 || block->matches < wt ) + { + block->hh.itemind = height0 + i; + block->matches = (wt < 100) ? wt : 100; + SETBIT(coin->havehash,height0 + i); + //iguana_blockhashset(coin,height0 + i,blockhashes[i],(wt < 100) ? wt : 100); + } + if ( (block->hh.prev == 0 || block->matches < wt) && block->hh.prev != prev ) + block->hh.prev = prev, plinks++; + if ( (block->hh.next == 0 || block->matches < wt) && block->hh.next != next ) + block->hh.next = next, nlinks++; + if ( block->matches < 100 ) + block->matches++; + } else printf("recvblockhashes: blocks[%d] null\n",i); + prev = block; + } + if ( next != 0 && (next->hh.prev == 0 || next->matches < wt) ) + next->hh.prev = prev; + } else printf("recvblockhashes: i.%d != num.%d\n",i,num); + } + if ( height0 >= 0 && 0 ) + { + for (i=0; i= 0 ) + { + if ( i == 0 ) + height0 = height, block0 = block; + m++; + //printf("height %d, wt %d itemind.%d matches.%d\n",height,wt,block->hh.itemind,block->matches); + if ( (int32_t)block->hh.itemind < 0 || block->matches*3 < wt ) + { + SETBIT(coin->havehash,height); + block->hh.itemind = height; + block->matches = (wt/3) + 1; + } + if ( prev != 0 && ((int32_t)prev->hh.itemind < 0 || prev->matches*3 < wt) ) + { + SETBIT(coin->havehash,height - 1); + prev->hh.itemind = height - 1; + prev->matches = (wt/3) + 1; + } + if ( next != 0 && ((int32_t)next->hh.itemind < 0 || next->matches*3 < wt) ) + { + SETBIT(coin->havehash,height + 1); + next->hh.itemind = height + 1; + next->matches = (wt/3) + 1; + } + } + } + prev = block; + } + if ( m >= coin->chain->bundlesize && height0 >= 0 && block != 0 ) + { + printf("gothdr.%d oldgothdr.%d %p %d <- %p\n",height0,block->gothdrs,coin->blocks.ptrs[height0],coin->blocks.ptrs[height0]!=0?coin->blocks.ptrs[height0]->hh.itemind:0,block); + coin->blocks.ptrs[height0] = block; + block->gothdrs = 1; + } + int32_t i,m,height,wt,height0 = -1; struct iguana_block *block,*next,*prev,*block0 = 0; + + int32_t iguana_blockmetric(struct iguana_info *coin,int32_t *wtp,struct iguana_block *block) + { + int32_t height = -1; int64_t wt; + if ( block->mainchain != 0 && block->height >= 0 ) + { + height = block->height; + wt = ((int64_t)coin->blocks.hwmheight - block->height) * 10000; + if ( wt > (1 << 28)/_IGUANA_HDRSCOUNT ) + wt = (1 << 28) / _IGUANA_HDRSCOUNT; + (*wtp) += (int32_t)wt; + } + else if ( block->height >= 0 ) + { + height = block->height; + (*wtp) += 10; + } + else if ( (int32_t)block->hh.itemind >= 0 ) + { + height = block->hh.itemind; + (*wtp) += block->matches; + } + return(height); + } + + int32_t iguana_heightwt(struct iguana_info *coin,int32_t *wtp,struct iguana_block *prev,struct iguana_block *block,struct iguana_block *next) + { + int32_t heightwts[3][2],height,i,n = 0; + *wtp = 0; + height = -1; + memset(heightwts,0,sizeof(heightwts)); + if ( block != 0 ) + { + if ( (heightwts[1][0]= iguana_blockmetric(coin,&heightwts[1][1],block)) >= 0 ) + n++; + //printf("%s itemind.%d matches.%d ht.%d metric.%d n.%d\n",bits256_str(hash2),block->hh.itemind,block->matches,heightwts[1][0],heightwts[1][1],n); + if ( prev != 0 )//|| (prev= block->hh.prev) != 0 ) + { + if ( (heightwts[0][0]= iguana_blockmetric(coin,&heightwts[0][1],prev)) >= 0 ) + { + //printf("heightwts(%d + 1) vs %d\n",heightwts[0][0],heightwts[1][0]); + if ( heightwts[0][0]+1 == heightwts[1][0] ) + n++; + else n--, heightwts[0][0] = -1; + } + //printf("%s itemind.%d matches.%d ht.%d metric.%d n.%d\n",bits256_str(prev->hash2),prev->hh.itemind,prev->matches,heightwts[0][0],heightwts[0][1],n); + } + if ( next != 0 )//(next= block->hh.prev) != 0 && next != block ) + { + if ( (heightwts[2][0]= iguana_blockmetric(coin,&heightwts[2][1],next)) >= 0 ) + { + if ( heightwts[2][0]-1 == heightwts[1][0] ) + n++; + else n--, heightwts[2][0] = -1; + } + //printf("%s itemind.%d matches.%d ht.%d metric.%d n.%d\n",bits256_str(next->hash2),next->hh.itemind,next->matches,heightwts[2][0],heightwts[2][1],n); + } + if ( n > 0 ) + { + for (i=0; i<3; i++) + if ( heightwts[i][0] >= 0 ) + (*wtp) += heightwts[i][1]; + (*wtp) *= n; + height = heightwts[1][0]; + } + } //else printf("cant find.(%s)\n",bits256_str(hash2)); + return(height); + } + /*n = 0; + if ( 0 && n == 0 && time(NULL) > coin->hdrstime+30 ) + { + height = (coin->blocks.hashblocks / coin->chain->bundlesize) * coin->chain->bundlesize; + while ( height < (coin->longestchain - coin->chain->bundlesize - 1) ) + { + if ( (hdrs= iguana_addhdr(coin,block->hash2,bits256_zero)) != 0 && hdrs->block.gothdrs == 0 ) + { + flag++; + //printf("REQ HDR.(%s) %d\n",bits256_str(block->hash2),height); + printf("%d ",height); + n++; + init_hexbytes_noT(hashstr,hdrs->bundlehash2.bytes,sizeof(bits256)); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1); + } + height += coin->chain->bundlesize; + } + coin->hdrstime = (uint32_t)time(NULL); + } + if ( n > 0 ) + printf("REQ EXTRA HDRS\n");*/ + if ( offset == 0 ) + printf("unhandled case offset.0\n"); + else + { + struct iguana_block *block; + if ( bits256_nonz(hdrs->bundlehash2) == 0 ) + { + if ( (block= iguana_blockfind(coin,blockhashes[0])) != 0 ) + hdrs->bundlehash2 = block->prev_block; + } + } + + struct iguana_block *iguana_updatehdrs(struct iguana_info *coin,int32_t *newhwmp,struct iguana_block *block,bits256 prevhash2,bits256 hash2) + { + struct iguana_bundlereq *hdrs; int32_t i,offset,height; + if ( (hdrs= iguana_addhdr(coin,&offset,0,prevhash2,hash2)) != 0 && hdrs->bundleheight >= 0 ) + { + iguana_blockset(coin,hdrs->bundleheight,hdrs->bundlehash2); + height = (hdrs->bundleheight + offset); + if ( block != 0 ) + { + block->hh.itemind = height; + hdrs->block = *block; + prevhash2 = block->prev_block, hash2 = block->hash2; + if ( height > 0 ) + iguana_blockset(coin,height - 1,prevhash2); + if ( height <= coin->blocks.hwmheight ) + { + if ( height < coin->blocks.hwmheight || coin->blocks.dirty == 0 ) + coin->blocks.dirty = height; + (*newhwmp)++; + } + } + if ( hdrs->hashes != 0 ) + { + for (i=0; in; i++) + if ( memcmp(block->hash2.bytes,hdrs->hashes[i].bytes,sizeof(bits256)) == 0 ) + break; + printf("got block.(%s) height.%d i.%d\n",bits256_str(block->hash2),height,i); + for (; in; i++,height++) + iguana_blockset(coin,height,hdrs->hashes[i]); + } else printf("no blockhashes.%d\n",hdrs->bundleheight); + } else printf("cant find hdrs.%s %s %d\n",bits256_str(prevhash2),bits256_str2(hash2),hdrs==0?-1:hdrs->bundleheight), getchar(); + return(iguana_blockfind(coin,hash2)); + } + + struct iguana_block *iguana_blockset(struct iguana_info *coin,int32_t height,bits256 hash2) + { + struct iguana_block *block; int32_t offset,h,flag; struct iguana_bundlereq *hdrs; + //printf("blockset.%d %s\n",height,bits256_str(hash2)); + if ( height < 0 ) + getchar(); + iguana_blockhashset(coin,height,hash2,100); + if ( (block= iguana_blockfind(coin,hash2)) != 0 ) + { + SETBIT(coin->havehash,height); + block->hh.itemind = height; + coin->blocks.ptrs[height] = block; + //printf("SETBIT.%d %s %p\n",hdrs->bundleheight,bits256_str(hdrs->bundlehash2),permblock); + } + if ( (height % coin->chain->bundlesize) == 0 ) + { + if ( 0 && (hdrs= iguana_addhdr(coin,&offset,1,hash2,bits256_zero)) != 0 ) + { + if ( hdrs->bundleheight != height && hdrs->bundleheight >= 0 ) + { + printf("bundleheight.%d -> EXTENDED HDRS.%d %s\n",hdrs->bundleheight,height,bits256_str(hash2)); + hdrs->bundleheight = height; + } + } + } + if ( height >= 0 && bits256_nonz(hash2) > 0 ) + { + if ( coin->chain->hasheaders == 0 && (height % coin->chain->bundlesize) == 1 && iguana_havetxdata(coin,height) == 0 ) + iguana_queueblock(coin,height,hash2,1); + } + return(block); + } + //tmp = iguana_updatehdrs(coin,newhwmp,block,block->prev_block,block->hash2); + if ( (prev= iguana_blockfind(coin,block->prev_block)) != 0 && (int32_t)prev->hh.itemind >= 0 ) + { + //printf("recv blockset.%s %d\n",bits256_str(block->hash2),prev->hh.itemind+1); + permblock = iguana_blockset(coin,prev->hh.itemind+1,block->hash2); + //permblock = iguana_blockhashset(coin,prev->hh.itemind+1,block->hash2,1); + } + else if ( (permblock= iguana_blockfind(coin,block->hash2)) == 0 ) + printf("cant find block prev.%s\n",bits256_str(block->prev_block)); + if ( tmp != 0 && permblock == 0 ) + permblock = tmp; + /*struct iguana_block *block; int32_t flag = 0; + while ( coin->blocks.issuedblocks < coin->blocks.hashblocks && coin->blocks.issuedblocks < coin->blocks.recvblocks+coin->chain->bundlesize*IGUANA_INITIALBUNDLES ) + { + if ( (block= iguana_blockptr(coin,coin->blocks.issuedblocks)) != 0 && bits256_nonz(block->hash2) != 0 ) + iguana_queueblock(coin,coin->blocks.issuedblocks,block->hash2,0); + coin->blocks.issuedblocks++; + flag++; + } + return(flag);*/ + + /*int32_t iguana_reqblocks(struct iguana_info *coin) + { + int32_t n,height,flag = 0; struct iguana_block *block; + if ( queue_size(&coin->priorityQ) == 0 ) + { + coin->pcount++; + if ( coin->pcount > 1 && (block= iguana_blockptr(coin,coin->blocks.recvblocks)) != 0 && coin->blocks.recvblocks < coin->blocks.issuedblocks && bits256_nonz(block->hash2) > 0 ) + flag += (iguana_queueblock(coin,coin->blocks.recvblocks,block->hash2,1) > 0); + } else coin->pcount = 0; + if ( queue_size(&coin->blocksQ) == 0 ) + { + coin->bcount++; + n = 0; + if ( coin->bcount > 100 && time(NULL) > coin->recvtime+3 ) + { + for (height=coin->blocks.recvblocks+1; heightblocks.issuedblocks&&nchain->bundlesize; height++) + { + if ( (block= iguana_blockptr(coin,coin->blocks.recvblocks)) != 0 && bits256_nonz(block->hash2) > 0 ) + { + //if ( (height % 100) == 0 ) + printf("RETRY BLOCK.%d\n",height); + flag += (iguana_queueblock(coin,height,block->hash2,0) > 0); + n++; + } + } + //coin->recvtime = (uint32_t)time(NULL); + } + } else coin->bcount = 0; + return(flag); + } + + { + bundlei = (height / coin->chain->bundlesize); + if ( GETBIT(coin->emitbits,bundlei) == 0 && iguana_bundleready(coin,height) > 0 ) + { + req->bundleheight = bundlei * coin->chain->bundlesize; + SETBIT(coin->emitbits,bundlei); + req->type = 'E'; + printf("Q emit.%d\n",req->bundleheight); + queue_enqueue("emitQ",&coin->emitQ,&req->DL,0); + } + }*/ + hash2 = iguana_bundleihash2(coin,bp,bundlei); + if ( memcmp(hash2.bytes,block->hash2.bytes,sizeof(hash2)) == 0 ) + { + *hdrsp = bp; + *bundleip = bundlei; + if ( bundlei == 0 ) + { + if ( (prevbp= iguana_bundlesearch(coin,&prevbundlei,block->bundlehash2,block->prev_block,IGUANA_SEARCHNEXT)) != 0 ) + { + if ( prevbundlei == prevbp->n+1 ) + { + bp->prevbundlehash2 = prevbp->bundlehash2; + prevbp->nextbundlehash2 = block->hash2; + //printf("prev BUNDLES LINKED! (%d <-> %d) (%s <-> %s)\n",prevbp->bundleheight,bp->bundleheight,bits256_str(prevbp->bundlehash2),bits256_str2(bp->bundlehash2)); + if ( prevbp->bundleheight != bp->bundleheight-coin->chain->bundlesize ) + printf("WARNING gap in bundleheight %d != %d bundlesize\n",prevbp->bundleheight,bp->bundleheight-coin->chain->bundlesize); + } else printf("prevbundlei.%d != prevhdrs->n %d\n",prevbundlei,prevbp->n+1); + } + } + else if ( bundlei == bp->n ) + { + if ( (nextbp= iguana_bundlesearch(coin,&nextbundlei,block->bundlehash2,block->hash2,IGUANA_SEARCHPREV)) != 0 ) + { + if ( nextbundlei == 0 ) + { + bp->nextbundlehash2 = nextbp->bundlehash2; + nextbp->prevbundlehash2 = block->hash2; + //printf("next BUNDLES LINKED! (%d <-> %d) (%s <-> %s)\n",nextbp->bundleheight,bp->bundleheight,bits256_str(nextbp->bundlehash2),bits256_str2(bp->bundlehash2)); + if ( nextbp->bundleheight != bp->bundleheight+coin->chain->bundlesize ) + printf("WARNING gap in bundleheight != bundlesize\n"); + } else printf("nextbundlei.%d != nextbp->n %d\n",nextbundlei,nextbp->n); + } + } + } else printf("hdrs.%d [%d] unexpected hash2 mismatch for %s != %s\n",bp->bundleheight,bundlei,bits256_str(block->hash2),bits256_str2(hash2)); + + + /*int32_t iguana_havehash(struct iguana_info *coin,int32_t height) + { + return(GETBIT(coin->havehash,height) != 0); + }*/ + + if ( (bp= iguana_bundlefind(coin,bundleip,block->hash2)) == 0 ) + { + if ( (prevbp= iguana_bundlefind(coin,&prevbundlei,block->prev_block)) == 0 ) + { + for (j=0; jbundlescount; j++) + { + if ( (bp= coin->bundles[j]) != 0 ) + { + if ( bp->blockhashes != 0 && bp->n > 0 && (bp= iguana_bundlescan(coin,bundleip,bp,block->hash2,IGUANA_SEARCHBUNDLE)) != 0 ) + { + return(bp); + } + } + } + return(0); + } + else if ( prevbundlei == prevbp->n ) + { + printf("prev AUTOCREATE.%s\n",bits256_str(block->hash2)); + iguana_bundlecreate(coin,block->hash2,bits256_zero); + } + } + || (bp= iguana_bundlefind(coin,bundleip,hash2)) != 0 ) + { + if ( (bp= iguana_bundlescan(coin,bundleip,bp,hash2,searchmask)) != 0 ) + return(bp); + } + if ( (bp= iguana_bundlefind(coin,bundleip,hash2)) != 0 ) + return(bp); + if ( (block= iguana_blockfind(coin,hash2)) == 0 ) + iguana_blockhashset(coin,-1,hash2,1); + if ( (block= iguana_blockfind(coin,hash2)) != 0 ) + { + if ( bits256_nonz(block->bundlehash2) > 0 ) + { + if ( (bp= iguana_bundlefind(coin,&tmp,block->bundlehash2)) != 0 ) + return(iguana_bundlescan(coin,bundleip,bp,hash2,searchmask)); + } + } + + for (i=0; ichain->bundlesize+1 && iguana_bundlefind(coin,&bundlei,blocks[n - 1].hash2,0) == 0 ) + { + printf("AUTO EXTEND3.%s[%d]\n",bits256_str(blocks[n - 1].hash2),n); + iguana_bundlecreate(coin,blocks[n - 1].hash2,bits256_zero); + } + + //struct iguana_blockhashes { bits256 *blockhashes; int32_t n; uint32_t starttime; }; + + void iguana_freetx(struct iguana_msgtx *tx,int32_t n) + { + int32_t i,j; struct iguana_msgtx *origtx = tx; + return; + for (j=0; jallocsize; + if ( tx->vins != 0 ) + { + for (i=0; itx_in; i++) + if ( tx->vins[i].script != 0 ) + myfree(tx->vins[i].script,tx->vins[i].scriptlen); + myfree(tx->vins,tx->tx_in * sizeof(*tx->vins)); + } + if ( tx->vouts != 0 ) + { + for (i=0; itx_out; i++) + if ( tx->vouts[i].pk_script != 0 ) + myfree(tx->vouts[i].pk_script,tx->vouts[i].pk_scriptlen); + myfree(tx->vouts,tx->tx_out * sizeof(*tx->vouts)); + } + } + myfree(origtx,sizeof(*origtx) * n); + } + + /* + struct iguana_rawtx { bits256 txid; uint16_t numvouts,numvins; uint8_t rmd160[20]; }; + + int32_t iguana_emittx(struct iguana_info *coin,FILE *fp,struct iguana_block *block,struct iguana_msgtx *tx,int32_t txi,uint32_t *numvoutsp,uint32_t *numvinsp,int64_t *outputp) + { + int32_t blocknum,i; int64_t reward; uint16_t s; struct iguana_rawtx rawtx; uint8_t rmd160[20],buf[64]; + struct iguana_msgvin *vin; + blocknum = block->hh.itemind; + memset(&rawtx,0,sizeof(rawtx)); + rawtx.txid = tx->txid; + rawtx.numvouts = tx->tx_out, rawtx.numvins = tx->tx_in; + if ( (blocknum == 91842 || blocknum == 91880) && txi == 0 && strcmp(coin->name,"bitcoin") == 0 ) + rawtx.txid.ulongs[0] ^= blocknum; + //printf("%d: tx.%p %p[numvouts.%d] %p[numvins.%d]\n",block->hh.itemind,tx,tx->vouts,tx->tx_out,tx->vins,tx->tx_in); + if ( fwrite(&rawtx,1,sizeof(rawtx),fp) == sizeof(rawtx) ) + { + for (i=0; ivouts[i].pk_script,tx->vouts[i].pk_scriptlen,rawtx.txid); + memcpy(buf,&tx->vouts[i].value,sizeof(tx->vouts[i].value)); + memcpy(&buf[sizeof(tx->vouts[i].value)],rmd160,sizeof(rmd160)); + if ( fwrite(buf,1,sizeof(rmd160)+sizeof(tx->vouts[i].value),fp) == sizeof(rmd160)+sizeof(tx->vouts[i].value) ) + { + (*numvoutsp)++; + (*outputp) += tx->vouts[i].value; + } else printf("error writing txi.%d vout.%d\n",txi,i); + } + for (i=0; ivins[i]; + if ( bits256_nonz(vin->prev_hash) == 0 ) + { + if ( i == 0 && (int32_t)vin->prev_vout < 0 ) + { + reward = iguana_miningreward(coin,blocknum); + //printf("reward %.8f\n",dstr(reward)); + (*outputp) += reward; + } else printf("unexpected prevout.%d\n",vin->prev_vout), getchar(); + continue; + } + memcpy(buf,vin->prev_hash.bytes,sizeof(vin->prev_hash)); + s = vin->prev_vout; + memcpy(&buf[sizeof(vin->prev_hash)],&s,sizeof(s)); + //printf("do spend.%s\n",bits256_str(vin->prev_hash)); + if ( fwrite(buf,1,sizeof(bits256)+sizeof(s),fp) == sizeof(bits256)+sizeof(s) ) + (*numvinsp)++; + else printf("error writing txi.%d vin.%d\n",txi,i); + } + return(0); + } + else printf("error writing txi.%d blocknum.%d\n",txi,blocknum); + return(-1); + } + + void iguana_emittxarray(struct iguana_info *coin,FILE *fp,struct iguana_block *block,struct iguana_msgtx *txarray,int32_t numtx) + { + uint32_t i,numvouts,numvins; int64_t credits; long fpos,endpos; + if ( fp != 0 && block != 0 ) + { + //printf("%d/%d: txarray.%p, numtx.%d bp.%p\n",block->hh.itemind,block->hh.itemind,txarray,numtx,bp); + fpos = ftell(fp); + credits = numvouts = numvins = 0; + for (i=0; iL.supply = credits; + block->txn_count = numtx; + block->numvouts = numvouts, block->numvins = numvins; + block->L.numtxids = numtx, block->L.numunspents = numvouts, block->L.numspends = numvins; + if ( fwrite(block,1,sizeof(*block),fp) != sizeof(*block) ) + printf("iguana_emittxarray: error writing block.%d\n",block->height); + fseek(fp,endpos,SEEK_SET); + } + } + + int32_t iguana_maptxdata(struct iguana_info *coin,struct iguana_mappedptr *M,struct iguana_bundle *bp,char *fname) + { + void *fileptr = 0; int32_t i; uint32_t *offsets; struct iguana_block *block; + if ( (fileptr= iguana_mappedptr(0,M,0,0,fname)) != 0 ) + { + offsets = fileptr; + for (i=0; in; i++) + { + if ( (block= bp->blocks[i]) != 0 ) + { + if ( block->txdata != 0 ) + { + if ( block->mapped == 0 ) + { + printf("[%d].%d free txdata.%d %p\n",bp->hdrsi,i,((struct iguana_bundlereq *)block->txdata)->allocsize,block->txdata); + myfree(block->txdata,((struct iguana_bundlereq *)block->txdata)->allocsize); + block->txdata = 0; + block->mapped = 0; + } + } + if ( i < coin->chain->bundlesize ) + { + block->txdata = (void *)((long)fileptr + offsets[i]); + block->mapped = 1; + } + } + else if ( i < coin->chain->bundlesize ) + printf("iguana_maptxdata cant find block[%d]\n",i); + } + return(i < coin->chain->bundlesize ? i : coin->chain->bundlesize); + } + printf("error mapping (%s)\n",fname); + return(-1); + } + + void iguana_emittxdata(struct iguana_info *coin,struct iguana_bundle *emitbp) + { + FILE *fp; char fname[512];uint32_t offsets[_IGUANA_HDRSCOUNT+1]; + //uint8_t extra[256]; struct iguana_msgtx *txarray,*tx; + struct iguana_bundlereq *req; struct iguana_mappedptr M; + int32_t i,bundleheight,height,numtx,n; long len; struct iguana_block *block; + return; + if ( emitbp == 0 ) + return; + sprintf(fname,"tmp/%s/txdata.%d",coin->symbol,emitbp->bundleheight); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + bundleheight = emitbp->bundleheight; + for (i=n=0; in&&ichain->bundlesize; i++) + if ( (block= emitbp->blocks[i]) != 0 && block->txdata != 0 && block->mapped == 0 ) + n++; + if ( n != emitbp->n && n != coin->chain->bundlesize ) + printf("iguana_emittxdata: WARNING n.%d != bundlesize.%d bundlesize.%d\n",n,emitbp->n,coin->chain->bundlesize); + memset(offsets,0,sizeof(offsets)); + if ( (len= fwrite(offsets,sizeof(*offsets),n+1,fp)) != n+1 ) + printf("%s: error writing blank offsets len.%ld != %d\n",fname,len,n+1); + for (i=0; iblocks[i]) != 0 ) + { + if ( (req= block->txdata) != 0 && (numtx= block->txn_count) > 0 ) + { + if ( 0 && fwrite(req->serialized,1,req->n,fp) != req->n ) + printf("error writing serialized data.%d\n",req->n); + if ( 0 && (txarray= iguana_gentxarray(coin,&len2,block,req->serialized,req->n,extra)) != 0 ) + { + tx = txarray; + for (j=0; jvouts,tx->tx_out,tx->vins,tx->tx_in); + printf("emit.%d txarray.%p[%d]\n",i,txarray,numtx); + iguana_emittxarray(coin,fp,block,txarray,numtx); + iguana_freetx(txarray,numtx); + } + } else printf("emittxdata: unexpected missing txarray[%d]\n",i); + } else printf("emittxdata: error with recvblockptr[%d]\n",emitbp->bundleheight + i); + } + offsets[i] = (uint32_t)ftell(fp); + rewind(fp); + if ( (len= fwrite(offsets,sizeof(*offsets),n+1,fp)) != n+1 ) + printf("%s: error writing offsets len.%ld != %d\n",fname,len,n+1); + fclose(fp), fp = 0; + memset(&M,0,sizeof(M)); + //if ( iguana_maptxdata(coin,&M,emitbp,fname) != n ) + // printf("emit error mapping n.%d height.%d\n",n,bundleheight); + //else + { + //if ( emitbp->blockhashes != 0 ) + // myfree(emitbp->blockhashes,sizeof(*emitbp->blockhashes) * emitbp->n); + //emitbp->blockhashes = 0; + } + } + }*/ + //static uint64_t Tx_allocated,Tx_allocsize,Tx_freed,Tx_freesize; + + /*int64_t iguana_MEMallocated(struct iguana_info *coin) + { + int64_t total = coin->TMPallocated; + if ( Tx_allocsize > Tx_freesize ) + total += (Tx_allocsize - Tx_freesize); + //total += coin->R.RSPACE.openfiles * coin->R.RSPACE.size; + //total += iguana_packetsallocated(coin); + return(total); + }*/ + + static int32_t _sort_by_bits256(struct iguana_kvitem *a,struct iguana_kvitem *b) + { + return(bits256_cmp(*(bits256 *)a->keyvalue,*(bits256 *)b->keyvalue)); + } + + static int32_t _sort_by_revbits256(struct iguana_kvitem *a,struct iguana_kvitem *b) + { + return(bits256_revcmp(*(bits256 *)a->keyvalue,*(bits256 *)b->keyvalue)); + } + + static int32_t _sort_by_rmd160(struct iguana_kvitem *a,struct iguana_kvitem *b) + { + return(rmd160_cmp(a->keyvalue,b->keyvalue)); + } + + static int32_t _sort_by_revrmd160(struct iguana_kvitem *a,struct iguana_kvitem *b) + { + return(rmd160_revcmp(a->keyvalue,b->keyvalue)); + } + + // HASH_SORT(coin->blocks.hash,_sort_by_txid); + /* if ( bp->type == 'Q' ) + { + req = (struct iguana_bundlereq *)ptr; + //printf("START.%p save tmp txdata %p [%d].%d datalen.%d %p\n",req,req->argbp,req->argbp!=0?req->argbp->hdrsi:-1,req->argbundlei,req->datalen,req->data); + if ( fp != 0 ) + { + if ( fwrite(req->data,1,req->datalen,fp) != req->datalen ) + printf("error writing [%d].%d datalen.%d\n",req->argbp!=0?req->argbp->hdrsi:-1,req->argbundlei,req->datalen); + } + //Tx_freed++; + //Tx_freesize += req->allocsize; + if ( req->data != 0 ) + myfree(req->data,req->datalen); + if ( req->blocks != 0 ) + myfree(req->blocks,sizeof(*req->blocks)); + myfree(req,req->allocsize); + } + else if ( bp->type == 'E' ) + { + fflush(fp); + //myallocated(0,0); + //iguana_emittxdata(bp->coin,bp); + //myallocated(0,0); + } + else + { + printf("iguana_helper: unsupported type.%c %d %p\n",bp->type,bp->type,bp); + }*/ + for (j=0; jhdrsi,bundlei,itembp->emitfinish); + if ( itembp->emitfinish != 0 ) + finished++; + } + } + if ( finished == num ) + iguana_peerfilecloseHT(coin,inds[j][0],inds[j][1]); + else printf("peerdir.(%d %d) finished.%d of %d\n",inds[j][0],inds[j][1],finished,num); + } else printf("cant get peerdirptr.(%d %d)\n",inds[j][0],inds[j][1]); + } + + int32_t iguana_bundlesaveHT(struct iguana_info *coin,struct iguana_memspace *mem,struct iguana_memspace *memB,struct iguana_bundle *bp) // helper thread + { + void *ptrs[IGUANA_MAXBUNDLESIZE]; uint32_t inds[IGUANA_MAXBUNDLESIZE][2]; struct iguana_fileitem *dir; + struct iguana_bundle *itembp; int32_t addrind,bundlei,finished,fileind,i,j,maxrecv,num,flag,numdirs=0; + struct iguana_txdatabits txdatabits; struct iguana_ramchain *ramchain; uint64_t estimatedsize = 0; + struct iguana_block *block; + memset(ptrs,0,sizeof(ptrs)), memset(inds,0,sizeof(inds)); + flag = maxrecv = 0; + for (i=0; in && ichain->bundlesize; i++) + { + if ( (block= bp->blocks[i]) != 0 ) + { + txdatabits = block->txdatabits; + if ( memcmp(block->hash2.bytes,coin->chain->genesis_hashdata,sizeof(bits256)) == 0 ) + ptrs[i] = coin->chain->genesis_hashdata, flag++; + else if ( (ptrs[i]= iguana_peerfileptrHT(coin,txdatabits,1)) != 0 ) + { + if ( block->recvlen > maxrecv ) + maxrecv = block->recvlen; + estimatedsize += block->recvlen; + flag++; + } + else + { + printf("peerfileptr[%d] (%d %d %d %d) null bp.%p %d\n",i,txdatabits.addrind,txdatabits.filecount,txdatabits.fpos,txdatabits.datalen,bp,bp->hdrsi); + if ( 1 ) + { + CLEARBIT(bp->recv,i); + bp->issued[i] = 0; + memset(&block->txdatabits,0,sizeof(block->txdatabits)); + block = 0; + } + } + addrind = txdatabits.addrind, fileind = txdatabits.filecount; + if ( numdirs > 0 ) + { + for (j=0; j>>>>>>>> start MERGE.(%ld %ld) numdirs.%d i.%d flag.%d estimated.%ld maxrecv.%d\n",(long)mem->totalsize,(long)memB->totalsize,numdirs,i,flag,(long)estimatedsize,maxrecv); + if ( (ramchain= iguana_bundlemergeHT(coin,mem,memB,ptrs,i,bp)) != 0 ) + { + iguana_ramchainsave(coin,mem,ramchain); + iguana_ramchainfree(coin,mem,ramchain); + bp->emitfinish = (uint32_t)time(NULL); + } else bp->emitfinish = 0; + iguana_mempurge(mem); + iguana_mempurge(memB); + } + else + { + printf(">>>>> bundlesaveHT error: numdirs.%d i.%d flag.%d\n",numdirs,i,flag); + bp->emitfinish = 0; + } + return(flag); + } + + int32_t iguana_peerfilecloseHT(struct iguana_info *coin,uint32_t addrind,uint32_t filecount) + { + char fname[512]; int32_t i,n = 0; struct iguana_mappedptr *M; + return(0); + iguana_peerfilename(coin,fname,addrind,filecount); + printf("PEERFILECLOSE.%s\n",fname); + //portable_mutex_lock(&coin->peers.filesM_mutex); + if ( coin->peers.filesM != 0 ) + { + for (i=0; ipeers.numfilesM; i++) + { + M = &coin->peers.filesM[i]; + if ( strcmp(fname,M->fname) == 0 && M->fileptr != 0 ) + { + printf("[%d] closemap.(%s)\n",i,fname); + iguana_closemap(M); + M->closetime = (uint32_t)time(NULL); + n++; + } + } + } + //portable_mutex_unlock(&coin->peers.filesM_mutex); + return(n); + } + + void *_iguana_txdataptrHT(struct iguana_info *coin,struct iguana_mappedptr *M,char *fname,struct iguana_txdatabits txdatabits) + { + int32_t len; uint8_t *rawptr; uint32_t starttime = (uint32_t)time(NULL); + if ( M->fileptr != 0 ) + { + while ( M->allocsize < (txdatabits.fpos + txdatabits.datalen + sizeof(uint32_t)) ) + { + iguana_closemap(M); + if ( iguana_mappedptr(0,M,0,0,fname) == 0 || M->allocsize < (txdatabits.fpos + txdatabits.datalen + sizeof(uint32_t)) ) + { + if ( time(NULL) > starttime+3 ) + { + printf("too small (%s) %llu vs %ld\n",fname,(long long)M->allocsize,(txdatabits.fpos + txdatabits.datalen + sizeof(uint32_t))); + return(0); + } else sleep(1); + } + } + rawptr = (void *)((long)M->fileptr + txdatabits.fpos); + memcpy(&len,rawptr,sizeof(len)); + if ( len == IGUANA_MARKER ) + { + memcpy(&len,&rawptr[sizeof(len)],sizeof(len)); + //printf("found marker %s[%u] numblocks.%d\n",fname,(int32_t)txdatabits.fpos,len); + if ( txdatabits.isdir != 0 ) + return(&rawptr[sizeof(uint32_t)*2]); + else printf("isdir notset with IGUANA_MARKER.%x\n",IGUANA_MARKER); + } + else if ( len == txdatabits.datalen && len < IGUANA_MAXPACKETSIZE ) + { + if ( txdatabits.isdir == 0 ) + return(&rawptr[sizeof(uint32_t)]); + else printf("isdir set without IGUANA_MARKER.%x\n",IGUANA_MARKER); + } else printf("txdataptr.%s: len.%d error [%d %d %d %d] (%d %d)\n",fname,len,txdatabits.datalen,txdatabits.addrind,txdatabits.fpos,txdatabits.filecount,len == txdatabits.datalen,len < IGUANA_MAXPACKETSIZE);//, getchar(); + } //else printf("txdataptr.%s %p %ld vs %ld\n",M->fname,M->fileptr,M->allocsize,(txdatabits.fpos + txdatabits.datalen + sizeof(uint32_t))); + return(0); + } +#define IGUANA_MARKER 0x07770777 + + void iguana_peerfilename(struct iguana_info *coin,char *fname,uint32_t addrind,uint32_t filecount) + { + sprintf(fname,"tmp/%s/peer%d.%d",coin->symbol,addrind,filecount); + } + + struct iguana_txdatabits iguana_calctxidbits(uint32_t addrind,uint32_t filecount,uint32_t fpos,uint32_t datalen) + { + struct iguana_txdatabits bits; + if ( (bits.addrind= addrind) != addrind ) + printf("iguana_calctxidbits: addrind overflow.%d\n",addrind), exit(-1); + if ( (bits.filecount= filecount) != filecount ) + printf("iguana_calctxidbits: filecount overflow.%d\n",filecount), exit(-1); + if ( (bits.fpos= fpos) != fpos ) + printf("iguana_calctxidbits: fpos overflow.%d\n",fpos), exit(-1); + if ( (bits.datalen= datalen) != datalen ) + printf("iguana_calctxidbits: datalen overflow.%d\n",datalen), exit(-1); + return(bits); + } + + void *iguana_peerfileptrHT(struct iguana_info *coin,struct iguana_txdatabits txdatabits,int32_t createflag) + { + char fname[512]; int32_t i,oldesti,oldest,duration,datalen; uint64_t fpos; struct iguana_mappedptr *M = 0; void *ptr = 0; + fpos = txdatabits.fpos, datalen = txdatabits.datalen; + oldesti = -1; + oldest = 0; + iguana_peerfilename(coin,fname,txdatabits.addrind,txdatabits.filecount); + //portable_mutex_lock(&coin->peers.filesM_mutex); + if ( coin->peers.filesM != 0 ) + { + for (i=0; ipeers.numfilesM; i++) + { + M = &coin->peers.filesM[i]; + if ( strcmp(fname,M->fname) == 0 ) + { + if ( M->fileptr != 0 && (ptr= _iguana_txdataptrHT(coin,M,fname,txdatabits)) != 0 ) + { + //portable_mutex_unlock(&coin->peers.filesM_mutex); + //printf("peerfileptr.(%s) %d %d -> %p\n",fname,txdatabits.addrind,txdatabits.filecount,ptr); + return(ptr); + } + else if ( M->closetime != 0 ) + { + duration = (uint32_t)(time(NULL) - M->closetime); + if ( duration > oldest ) + oldest = duration, oldesti = i; + } + } + } + M = 0; + } + if ( createflag != 0 ) + { + if ( oldesti >= 0 && oldest > 60 ) + { + M = &coin->peers.filesM[oldesti]; + printf("oldesti.%d oldest.%d remove.(%s) recycle slot.%d\n",oldesti,oldest,M->fname,i); + iguana_removefile(M->fname,0); + memset(M,0,sizeof(*M)); + } + if ( M == 0 ) + { + coin->peers.filesM = myrealloc('m',coin->peers.filesM,coin->peers.filesM==0?0:coin->peers.numfilesM * sizeof(*coin->peers.filesM),(coin->peers.numfilesM+1) * sizeof(*coin->peers.filesM)); + M = &coin->peers.filesM[coin->peers.numfilesM]; + coin->peers.numfilesM++; + //if ( (coin->peers.numfilesM % 10) == 0 ) + printf("iguana_peerfileptr realloc filesM.%d\n",coin->peers.numfilesM); + } + if ( iguana_mappedptr(0,M,0,0,fname) != 0 ) + { + ptr = _iguana_txdataptrHT(coin,M,fname,txdatabits); + printf("mapped.(%s) size.%ld %p\n",fname,(long)M->allocsize,ptr); + } else printf("iguana_peerfileptr error mapping.(%s)\n",fname); + } + //portable_mutex_unlock(&coin->peers.filesM_mutex); + return(ptr); + } + + struct iguana_fileitem *iguana_peerdirptrHT(struct iguana_info *coin,int32_t *nump,uint32_t addrind,uint32_t filecount,int32_t createflag) + { + char fname[512]; FILE *fp; uint32_t dirpos,marker; struct iguana_txdatabits txdatabits; + *nump = 0; + if ( filecount >= coin->peers.active[addrind].filecount ) + return(0); + iguana_peerfilename(coin,fname,addrind,filecount); + if ( (fp= fopen(fname,"rb")) != 0 ) + { + fseek(fp,-sizeof(int32_t) * 3,SEEK_END); + fread(nump,1,sizeof(*nump),fp); + fread(&dirpos,1,sizeof(dirpos),fp); + fread(&marker,1,sizeof(marker),fp); + if ( marker == IGUANA_MARKER && (dirpos + sizeof(uint32_t) * 5 + *nump * sizeof(struct iguana_fileitem)) == ftell(fp) ) + { + txdatabits = iguana_calctxidbits(addrind,filecount,dirpos,(int32_t)(*nump * sizeof(struct iguana_fileitem))); + fclose(fp); + txdatabits.isdir = 1; + return(iguana_peerfileptrHT(coin,txdatabits,1)); + } + else //if ( marker == IGUANA_MARKER ) + printf("marker.%x vs %x: dirpos.%d num.%d -> %ld vs %ld\n",marker,IGUANA_MARKER,dirpos,*nump,dirpos + sizeof(uint32_t) * 4 + *nump * sizeof(struct iguana_fileitem),ftell(fp)); + fclose(fp); + } else printf("cant open dir.(%s)\n",fname); + return(0); + } + struct iguana_txdatabits iguana_peerfilePT(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2,struct iguana_txdatabits txdatabits,int32_t datalen) + { + char fname[512]; int32_t marker; uint32_t dirpos; + if ( bits256_nonz(hash2) == 0 || addr->fp == 0 || ftell(addr->fp)+datalen >= IGUANA_PEERFILESIZE-IGUANA_MAXPACKETSIZE || addr->numfilehash2 >= addr->maxfilehash2 ) + //if ( addr->fp == 0 ) + { + if ( addr->fp != 0 ) + { + dirpos = (uint32_t)ftell(addr->fp); + marker = IGUANA_MARKER; + fwrite(&marker,1,sizeof(marker),addr->fp); + fwrite(&addr->numfilehash2,1,sizeof(addr->numfilehash2),addr->fp); + fwrite(addr->filehash2,addr->numfilehash2,sizeof(*addr->filehash2),addr->fp); + fwrite(&addr->numfilehash2,1,sizeof(addr->numfilehash2),addr->fp); + fwrite(&dirpos,1,sizeof(dirpos),addr->fp); + fwrite(&marker,1,sizeof(marker),addr->fp); + fclose(addr->fp); + //iguana_flushQ(coin,addr); + //fflush(addr->fp); + } + iguana_peerfilename(coin,fname,addr->addrind,++addr->filecount); + txdatabits.filecount = addr->filecount; + addr->fp = fopen(fname,"wb"); + addr->numfilehash2 = 0; + } + if ( addr->fp == 0 ) + { + printf("error creating fileind.%d %s\n",addr->filecount,addr->ipaddr); + exit(1); + } + if ( addr->numfilehash2 < addr->maxfilehash2 ) + { + if ( addr->filehash2 == 0 ) + addr->filehash2 = mycalloc('f',addr->maxfilehash2,sizeof(*addr->filehash2)); + addr->filehash2[addr->numfilehash2].hash2 = hash2; + addr->filehash2[addr->numfilehash2].txdatabits = txdatabits; + addr->numfilehash2++; + } + return(txdatabits); + } + + + int32_t iguana_ramchainspend(struct iguana_info *coin,struct iguana_ramchain *ramchain,uint32_t spendind,uint32_t spent_txidind,uint16_t spent_vout,int32_t updateflag) + { + struct iguana_pkhash *p; struct iguana_unspent *u; struct iguana_account *acct; int32_t unspentind,pkind; + if ( spent_txidind < ramchain->numtxids ) + { + unspentind = (spent_txidind + spent_vout); + u = &ramchain->U[unspentind]; + if ( (pkind= u->pkind) < ramchain->numpkinds && pkind >= 0 ) + { + if ( updateflag != 0 ) + { + p = &ramchain->P[pkind]; + if ( ramchain->pkextras[pkind].firstspendind == 0 ) + ramchain->pkextras[pkind].firstspendind = spendind; + acct = &ramchain->accounts[pkind]; + ramchain->S[spendind].prevspendind = acct->lastspendind; + acct->lastspendind = spendind; + if ( ramchain->Uextras[unspentind].spendind != 0 ) + { + printf("double spend u.%d has spendind.%d when s.%d refers to it\n",unspentind,ramchain->Uextras[unspentind].spendind,spendind); + return(-1); + } + ramchain->Uextras[unspentind].spendind = spendind; + } + return(1); + } + } + return(0); + } + + int32_t iguana_ramchainspends(struct iguana_info *coin,struct iguana_ramchain *ramchain,int32_t updateflag) + { + struct iguana_spend *s; int32_t j,spendind,retval,spent_txidind,spent_vout,needtxidinds = 0; + spendind = 0; + for (j=0; jnumspends; j++,spendind++) + { + s = &ramchain->S[spendind]; + spent_txidind = (s->unspentind >> 16) & 0xffff; + spent_vout = (s->unspentind & 0xffff); + if ( (retval= iguana_ramchainspend(coin,ramchain,spendind,spent_txidind,spent_vout,updateflag)) < 0 ) + return(-1); + needtxidinds += retval; + } + return(needtxidinds); + } + + int32_t iguana_ramchainload(struct iguana_info *coin,struct iguana_memspace *mem,struct iguana_ramchain *ramchain) + { + int32_t i,j; uint32_t unspentind,spendind,txidind,pkind,needtxidinds = 0; + struct iguana_txid *tx; struct iguana_pkhash *p; struct iguana_unspent *u; struct iguana_account *acct; + txidind = unspentind = spendind = pkind = 0; + for (pkind=0; pkindnumpkinds; pkind++) + { + p = &ramchain->P[pkind]; + iguana_hashsetHT(ramchain->pkhashes,0,p->rmd160,sizeof(p->rmd160),pkind); + } + for (i=0; inumtxids; i++,txidind++) + { + tx = &ramchain->T[txidind]; + iguana_hashsetHT(ramchain->txids,0,tx->txid.bytes,sizeof(bits256),txidind); + for (j=0; jnumvouts; j++,unspentind++) + { + u = &ramchain->U[unspentind]; + acct = &ramchain->accounts[u->pkind]; + u->prevunspentind = acct->lastunspentind; + acct->lastunspentind = unspentind; + if ( u->txidind != txidind ) + { + printf("txidind.%d u->txidind.%d mismatch\n",txidind,u->txidind); + return(-1); + } + acct->balance += u->value; + } + } + if ( (needtxidinds= iguana_ramchainspends(coin,ramchain,0)) == 0 ) + { + if ( (needtxidinds= iguana_ramchainspends(coin,ramchain,1)) != 0 ) + printf("ramchainspends unexpected error\n"); + } + return(needtxidinds); + } + + int32_t iguana_ramchainload(struct iguana_info *coin,struct iguana_memspace *mem,struct iguana_ramchain *ramchain) + { + int32_t i,j; uint32_t unspentind,spendind,txidind,pkind,needtxidinds = 0; + struct iguana_txid *tx; struct iguana_pkhash *p; struct iguana_unspent *u; struct iguana_account *acct; + txidind = unspentind = spendind = 0; + for (i=0; inumtxids; i++,txidind++) + { + tx = &ramchain->T[txidind]; + iguana_hashsetHT(ramchain->txids,0,tx->txid.bytes,sizeof(bits256),txidind); + for (j=0; jnumvouts; j++,unspentind++) + { + } + } + if ( (needtxidinds= iguana_ramchainspends(coin,ramchain,0)) == 0 ) + { + if ( (needtxidinds= iguana_ramchainspends(coin,ramchain,1)) != 0 ) + printf("ramchainspends unexpected error\n"); + } + return(needtxidinds); + } + + + int32_t iguana_ramchainspend(struct iguana_info *coin,struct iguana_ramchain *ramchain,uint32_t spendind,uint32_t spent_txidind,uint16_t spent_vout,int32_t updateflag) + { + struct iguana_pkhash *p; struct iguana_unspent *u; struct iguana_account *acct; int32_t unspentind,pkind; + if ( spent_txidind < ramchain->numtxids ) + { + unspentind = (spent_txidind + spent_vout); + u = &ramchain->U[unspentind]; + if ( (pkind= u->pkind) < ramchain->numpkinds && pkind >= 0 ) + { + if ( updateflag != 0 ) + { + p = &ramchain->P[pkind]; + if ( ramchain->pkextras[pkind].firstspendind == 0 ) + ramchain->pkextras[pkind].firstspendind = spendind; + acct = &ramchain->accounts[pkind]; + ramchain->S[spendind].prevspendind = acct->lastspendind; + acct->lastspendind = spendind; + if ( ramchain->Uextras[unspentind].spendind != 0 ) + { + printf("double spend u.%d has spendind.%d when s.%d refers to it\n",unspentind,ramchain->Uextras[unspentind].spendind,spendind); + return(-1); + } + ramchain->Uextras[unspentind].spendind = spendind; + } + return(1); + } + } + return(0); + } + uint32_t oldiguana_rwiAddrind(struct iguana_info *coin,int32_t rwflag,struct iguana_iAddr *iA,uint32_t ind) + { + uint32_t tmpind; char ipaddr[64]; struct iguana_iAddr checkiA; + if ( rwflag == 0 ) + { + memset(iA,0,sizeof(*iA)); + if ( iguana_kvread(coin,coin->iAddrs,0,iA,&ind) != 0 ) + { + //printf("read[%d] %x -> status.%d\n",ind,iA->ipbits,iA->status); + return(ind); + } else printf("error getting pkhash[%u] when %d\n",ind,coin->numiAddrs); + } + else + { + expand_ipbits(ipaddr,iA->ipbits); + tmpind = ind; + if ( iguana_kvwrite(coin,coin->iAddrs,&iA->ipbits,iA,&tmpind) != 0 ) + { + if ( tmpind != ind ) + printf("warning: tmpind.%d != ind.%d for %s\n",tmpind,ind,ipaddr); + //printf("iA[%d] wrote status.%d\n",ind,iA->status); + if ( iguana_kvread(coin,coin->iAddrs,0,&checkiA,&tmpind) != 0 ) + { + if ( memcmp(&checkiA,iA,sizeof(checkiA)) != 0 ) + printf("compare error tmpind.%d != ind.%d\n",tmpind,ind); + } + return(iA->ipbits); + } else printf("error kvwrite (%s) ind.%d tmpind.%d\n",ipaddr,ind,tmpind); + } + printf("iA[%d] error rwflag.%d\n",ind,rwflag); + return(0); + } + struct iguana_peer *iguana_choosepeer(struct iguana_info *coin) + { + int32_t i,j,r,iter; struct iguana_peer *addr; + r = rand(); + portable_mutex_lock(&coin->peers_mutex); + if ( coin->MAXPEERS == 0 ) + coin->MAXPEERS = IGUANA_MAXPEERS; + if ( coin->peers.numranked > 0 ) + { + for (j=0; jpeers.numranked; j++) + { + i = (j + r) % coin->MAXPEERS; + if ( (addr= coin->peers.ranked[i]) != 0 && addr->pendblocks < coin->MAXPENDING && addr->dead == 0 && addr->usock >= 0 ) + { + portable_mutex_unlock(&coin->peers_mutex); + return(addr); + } + } + } + portable_mutex_unlock(&coin->peers_mutex); + for (iter=0; iter<2; iter++) + { + for (i=0; iMAXPEERS; i++) + { + addr = &coin->peers.active[(i + r) % coin->MAXPEERS]; + if ( addr->dead == 0 && addr->usock >= 0 && (iter == 1 || addr->pendblocks < coin->MAXPENDING) ) + return(addr); + } + } + return(0); + } + void iguana_shutdownpeers(struct iguana_info *coin,int32_t forceflag) + { +#ifndef IGUANA_DEDICATED_THREADS + int32_t i,skip,iter; struct iguana_peer *addr; + if ( forceflag != 0 ) + coin->peers.shuttingdown = (uint32_t)time(NULL); + for (iter=0; iter<60; iter++) + { + skip = 0; + for (i=0; iMAXPEERS; i++) + { + addr = &coin->peers.active[i]; + if ( addr->ipbits == 0 || addr->usock < 0 || (forceflag == 0 && addr->dead == 0) ) + continue; + if ( addr->startsend != 0 || addr->startrecv != 0 ) + { + skip++; + continue; + } + iguana_iAkill(coin,addr,0); + } + if ( skip == 0 ) + break; + sleep(1); + printf("iguana_shutdownpeers force.%d skipped.%d\n",forceflag,skip); + } + if ( forceflag != 0 ) + coin->peers.shuttingdown = 0; +#endif + } + + uint32_t iguana_ipbits2ind(struct iguana_info *coin,struct iguana_iAddr *iA,uint32_t ipbits,int32_t createflag) + { + char ipaddr[64]; struct iguana_kvitem *item; struct iguana_iAddr *tmp; + expand_ipbits(ipaddr,ipbits); + //printf("ipbits.%x %s to ind\n",ipbits,ipaddr); + memset(iA,0,sizeof(*iA)); + if ( (item= iguana_hashfind(coin->iAddrs,&ipbits,sizeof(ipbits))) == 0 ) + //if ( iguana_kvread(coin,coin->iAddrs,&ipbits,iA,&ind) == 0 ) + { + if ( createflag == 0 ) + return(0); + tmp = mycalloc('i',1,sizeof(*iA)); + *tmp = *iA; + iA->ind = coin->numiAddrs; + iA->ipbits = ipbits; + if ( (item= iguana_hashset(coin->iAddrs,0,&iA->ipbits,sizeof(iA->ipbits),iA->ind)) == 0 ) + { + printf("iguana_addr: cant save.(%s)\n",ipaddr); + return(0); + } + else + { + coin->numiAddrs++; + if ( iguana_rwiAddrind(coin,1,iA,iA->ind) == 0 ) + printf("error iAddr.%d: created %x %s\n",iA->ind,ipbits,ipaddr); + } + } + else *iA = *(struct iguana_iAddr *)item->keyvalue; + return(iA->ind); + } + + int32_t iguana_set_iAddrheight(struct iguana_info *coin,uint32_t ipbits,int32_t height) + { + struct iguana_iAddr iA; uint32_t ind; + if ( (ind= iguana_ipbits2ind(coin,&iA,ipbits,1)) > 0 ) + { + iA.ipbits = ipbits; + if ( (ind= iguana_rwiAddrind(coin,0,&iA,ind)) > 0 && height > iA.height ) + { + iA.height = height; + iA.ipbits = ipbits; + iguana_rwiAddrind(coin,1,&iA,ind); + } + } + return(iA.height); + } + + uint32_t iguana_rwipbits_status(struct iguana_info *coin,int32_t rwflag,uint32_t ipbits,int32_t *statusp) + { + struct iguana_iAddr iA; uint32_t ind; + if ( (ind= iguana_ipbits2ind(coin,&iA,ipbits,1)) > 0 ) + { + if ( (ind= iguana_rwiAddrind(coin,0,&iA,ind)) > 0 ) + { + if ( rwflag == 0 ) + *statusp = iA.status; + else + { + iA.status = *statusp; + iA.ipbits = ipbits; + printf("%p status.%d ipbits.%x iA.%d saved iA->ind.%d\n",&iA,iA.status,iA.ipbits,ind,iA.ind); + //printf("set status.%d for ind.%d\n",iA.status,ind); + if ( iguana_rwiAddrind(coin,1,&iA,ind) == 0 ) + { + printf("iguana_iAconnected (%x) save error\n",iA.ipbits); + return(0); + } + } + return(ind); + } else printf("iguana_rwiAstatus error getting iA[%d]\n",ind); + } else printf("error ipbits status\n"); + return(0); + } + + int32_t iguana_ramchainspends(struct iguana_info *coin,struct iguana_ramchain *ramchain,int32_t updateflag) + { + struct iguana_spend *s; int32_t j,spendind,retval,needtxidinds = 0; + spendind = 0; + for (j=0; jnumspends; j++,spendind++) + { + s = &ramchain->S[spendind]; + if ( (retval= iguana_ramchainspend(coin,ramchain,spendind,s->spendtxidind,s->vout,updateflag)) < 0 ) + return(-1); + needtxidinds += retval; + } + return(needtxidinds); + } + if ( bundlei >= coin->chain->bundlesize ) + return(block); + if ( (block->bundlei= bundlei) == 0 ) + { + iguana_hash2set(coin,"bundlehash2",&bp->blockhashes[0],block->hash2); + //iguana_blockQ(coin,bp,0,bp->bundlehash2,1); + if ( bits256_nonz(block->prev_block) > 0 ) + { + //iguana_blockQ(coin,bp,-1,block->prev_block,1); + for (i=0; ibundlescount; i++) + { + if ( (prevbp= coin->bundles[i]) != 0 && prevbp->n >= coin->chain->bundlesize ) + { + cmphash2 = iguana_bundleihash2(coin,prevbp,coin->chain->bundlesize-1); + if ( memcmp(cmphash2.bytes,block->prev_block.bytes,sizeof(bits256)) == 0 ) + { + //printf("found prev_block\n"); + iguana_hash2set(coin,"bp setprev",&bp->prevbundlehash2,prevbp->blockhashes[0]); + iguana_hash2set(coin,"prevbp setnext",&prevbp->nextbundlehash2,bp->blockhashes[0]); + //printf("prev BUNDLES LINKED! (%d <-> %d) (%s <-> %s)\n",prevbp->bundleheight,bp->bundleheight,bits256_str(prevbp->bundlehash2),bits256_str2(bp->bundlehash2)); + if ( prevbp->bundleheight != bp->bundleheight-coin->chain->bundlesize ) + printf("WARNING gap in bundleheight %d != %d bundlesize\n",prevbp->bundleheight,bp->bundleheight-coin->chain->bundlesize); + break; + } + } + } + } + } + else if ( bundlei == 1 ) + { + if ( iguana_hash2set(coin,"firstblockhash2",&bp->blockhashes[1],block->hash2) < 0 ) + return(0); + if ( bp->blockhashes != 0 ) + { + if ( bits256_nonz(block->prev_block) > 0 ) + iguana_hash2set(coin,"b blockhashes[0]",&bp->blockhashes[0],block->prev_block); + iguana_hash2set(coin,"b blockhashes[1]",&bp->blockhashes[1],block->hash2); + } + } + else if ( bundlei == bp->n-1 ) + { + if ( (nextbp= iguana_bundlefind(coin,&nextbundlei,hash2,IGUANA_SEARCHBUNDLE)) != 0 ) + { + if ( nextbundlei == 0 ) + { + iguana_hash2set(coin,"bp setnext",&bp->nextbundlehash2,nextbp->blockhashes[0]); + iguana_hash2set(coin,"next setprev",&nextbp->prevbundlehash2,bp->blockhashes[0]); + char str[65],str2[65]; + bits256_str(str,bp->blockhashes[0]), bits256_str(str2,nextbp->blockhashes[0]); + printf("next BUNDLES LINKED! (%d <-> %d) (%s <-> %s)\n",bp->bundleheight,nextbp->bundleheight,str,str2); + if ( nextbp->bundleheight != bp->bundleheight+coin->chain->bundlesize ) + printf("WARNING gap in bundleheight %d != %d bundlesize\n",nextbp->bundleheight,bp->bundleheight+coin->chain->bundlesize); + } else printf("nextbundlei.%d != 0 nextbp->n %d\n",nextbundlei,nextbp->n); + } + //iguana_hash2set(coin,"lastblockhash2",&bp->lastblockhash2,block->hash2); + } + } + + + struct iguana_bundle *iguana_bundlescan(struct iguana_info *coin,int32_t *bundleip,struct iguana_bundle *bp,bits256 hash2,int32_t searchmask) + { + int32_t i; + *bundleip = -2; + if ( (searchmask & IGUANA_SEARCHBUNDLE) != 0 ) + { + // bloom filter here + //printf("%s vs %s: %d\n",bits256_str(hash2),bits256_str2(bp->bundlehash2),memcmp(hash2.bytes,bp->bundlehash2.bytes,sizeof(hash2))); + if ( memcmp(hash2.bytes,bp->blockhashes[0].bytes,sizeof(hash2)) == 0 ) + { + *bundleip = 0; + //printf("found blockhash[0]\n"); + return(bp); + } + if ( memcmp(hash2.bytes,bp->blockhashes[1].bytes,sizeof(hash2)) == 0 ) + { + *bundleip = 1; + //printf("found blockhash[1]\n"); + return(bp); + } + for (i=2; in && ichain->bundlesize; i++) + { + if ( memcmp(hash2.bytes,bp->blockhashes[i].bytes,sizeof(hash2)) == 0 ) + { + *bundleip = i; + return(bp); + } + } + } + if ( (searchmask & IGUANA_SEARCHPREV) != 0 && memcmp(hash2.bytes,bp->prevbundlehash2.bytes,sizeof(hash2)) == 0 ) + { + *bundleip = -1; + return(bp); + } + if ( (searchmask & IGUANA_SEARCHNEXT) != 0 && memcmp(hash2.bytes,bp->nextbundlehash2.bytes,sizeof(hash2)) == 0 ) + { + *bundleip = bp->n; + return(bp); + } + return(0); + } + + struct iguana_bundle *iguana_bundlefind(struct iguana_info *coin,int32_t *bundleip,bits256 hash2,int32_t adjust) + { + int32_t i,searchmask; struct iguana_bundle *bp = 0; // struct iguana_block *block; + *bundleip = -2; + if ( bits256_nonz(hash2) > 0 ) + { + if ( adjust == 0 ) + searchmask = IGUANA_SEARCHBUNDLE; + else searchmask = IGUANA_SEARCHNOLAST; + //if ( (block= iguana_blockfind(coin,hash2)) != 0 && (bp= block->bp) != 0 && (bp= iguana_bundlescan(coin,bundleip,bp,hash2,searchmask)) != 0 ) + // return(bp); + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + if ( (bp= iguana_bundlescan(coin,bundleip,bp,hash2,searchmask)) != 0 ) + return(bp); + } + } + } + //printf("iguana_hdrsfind: cant find %s\n",bits256_str(hash2)); + return(0); + } + + int32_t iguana_bundlecheck(struct iguana_info *coin,struct iguana_bundle *bp,int32_t priorityflag) + { + int32_t i,qsize,remains,incomplete,lasti,n = 0; struct iguana_block *block; + bits256 hash2; double threshold; uint64_t datasize =0; + //printf("bp.%p bundlecheck.%d emit.%d\n",bp,bp->ramchain.hdrsi,bp->emitfinish); + if ( bp != 0 && bp->emitfinish == 0 ) + { + remains = bp->n - bp->numrecv; + qsize = queue_size(&coin->priorityQ); + if ( bp->numrecv > coin->chain->bundlesize*.98 ) + { + priorityflag = 1; + if ( bp->numrecv > coin->chain->bundlesize-3 ) + threshold = bp->avetime; + else threshold = bp->avetime * 2; + } else threshold = bp->avetime * 5; + lasti = -1; + for (i=0; in && ichain->bundlesize; i++) + { + hash2 = iguana_bundleihash2(coin,bp,i); + if ( bits256_nonz(hash2) == 0 ) + continue; + if ( (block= bp->blocks[i]) == 0 ) + block = bp->blocks[i] = iguana_blockfind(coin,hash2); + if ( block != 0 && block->ipbits != 0 ) + { + //char str[65]; + if ( block->recvlen != 0 ) + datasize += block->recvlen; + if ( block->hdrsi != bp->ramchain.hdrsi ) + block->hdrsi = bp->ramchain.hdrsi; + if ( block->bundlei != i ) + block->bundlei = i; + /* printf("%s %d[%d] != %d[%d]\n",bits256_str(str,block->hash2),block->hdrsi,block->bundlei,bp->ramchain.hdrsi,i); + CLEARBIT(bp->recv,i); + //memset(&bp->blocks[i]->txdatabits,0,sizeof(bp->blocks[i]->txdatabits)); + bp->issued[i] = milliseconds(); + iguana_blockQ(coin,bp,i,bp->blocks[i]->hash2,1); + bp->blocks[i] = 0; + } + else if ( block->bundlei != i ) + { + printf("%s %d[%d] != %d[%d]\n",bits256_str(str,block->hash2),block->hdrsi,block->bundlei,bp->ramchain.hdrsi,i); + CLEARBIT(bp->recv,i); + //memset(&bp->blocks[i]->txdatabits,0,sizeof(bp->blocks[i]->txdatabits)); + bp->issued[i] = milliseconds(); + iguana_blockQ(coin,bp,i,bp->blocks[i]->hash2,1); + bp->blocks[i] = 0; + } else */ + n++; + } + else if ( priorityflag != 0 && qsize == 0 )//&& (bp->issued[i] == 0 || milliseconds() > (bp->issued[i] + threshold)) ) + { + //if ( (rand() % 1000) == 0 ) + // printf("priorityQ submit threshold %.3f [%d].%d\n",threshold,bp->ramchain.hdrsi,i); + if ( bp->blocks[i] == 0 || bp->blocks[i]->ipbits == 0 ) + { + //CLEARBIT(bp->recv,i); + bp->issued[i] = 0;//milliseconds(); + //if ( i < 2 ) + // iguana_blockQ(coin,bp,i,hash2,1); + //iguana_blockQ(coin,bp,i,hash2,1); + //bp->blocks[i] = 0; + } + lasti = i; + } else lasti = i; + } + //if ( n == coin->chain->bundlesize-1 ) + //if ( n > 490 ) + // printf("bp.%d %d %d n.%d\n",bp->ramchain.hdrsi,bp->ramchain.bundleheight,lasti,n); + bp->numrecv = n; + bp->datasize = datasize; + if ( n > 0 ) + { + bp->estsize = ((uint64_t)datasize * coin->chain->bundlesize) / n; + //printf("estsize %d datasize.%d hdrsi.%d numrecv.%d\n",(int32_t)bp->estsize,(int32_t)datasize,bp->ramchain.hdrsi,n); + } + if ( n == coin->chain->bundlesize ) + { + printf("check %d blocks in hdrs.%d\n",n,bp->ramchain.hdrsi); + for (i=incomplete=0; iblocks[i]->hash2.bytes,bp->blocks[i+1]->prev_block.bytes,sizeof(bits256)) != 0 ) + { + if ( bits256_nonz(bp->blocks[i]->prev_block) > 0 && bits256_nonz(bp->blocks[i+1]->prev_block) > 0 && bits256_nonz(bp->blocks[i+1]->hash2) > 0 ) + { + char str[65],str2[65],str3[65]; + bits256_str(str,bp->blocks[i]->hash2); + bits256_str(str2,bp->blocks[i+1]->prev_block); + bits256_str(str3,bp->blocks[i+1]->hash2); + printf("%s ->%d %d<- %s %s ",str,i,i+1,str2,str3); + printf("broken chain in hdrs.%d %d %p <-> %p %d\n",bp->ramchain.hdrsi,i,bp->blocks[i],bp->blocks[i+1],i+1); + CLEARBIT(bp->recv,i); + //memset(&bp->blocks[i]->txdatabits,0,sizeof(bp->blocks[i]->txdatabits)); + //memset(&bp->blocks[i+1]->txdatabits,0,sizeof(bp->blocks[i+1]->txdatabits)); + bp->issued[i] = bp->issued[i+1] = milliseconds(); + //iguana_blockQ(coin,bp,i,bp->blocks[i]->hash2,1); + //iguana_blockQ(coin,bp,i+1,bp->blocks[i+1]->hash2,1); + bp->blocks[i] = bp->blocks[i+1] = 0; + break; + } + else incomplete++; + } + } + printf("i.%d n.%d incomplete.%d\n",i,n,incomplete); + if ( i == n-1 && incomplete == 0 ) + { + //if ( bp->blockhashes != 0 ) + //{ + for (i=0; iblockhashes[i],bp->blocks[i]->hash2); + // iguana_hash2set(coin,"check blockhashes[0]",&bp->blockhashes[0],bp->bundlehash2); + // iguana_hash2set(coin,"check firsthash2",&bp->blockhashes[1],bp->firstblockhash2); + //} + iguana_bundleblockadd(coin,bp,0,iguana_bundleihash2(coin,bp,0)); + iguana_bundleblockadd(coin,bp,coin->chain->bundlesize-1,iguana_bundleihash2(coin,bp,coin->chain->bundlesize-1)); + if ( bp->emitfinish <= 1 ) + iguana_emitQ(coin,bp); + if ( bp->emitfinish == 0 ) + bp->emitfinish = 1; + coin->numpendings--; + return(1); + } + } + } + return(0); + } + /****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + + // peer context, ie massively multithreaded -> bundlesQ + + struct iguana_bundlereq *iguana_bundlereq(struct iguana_info *coin,struct iguana_peer *addr,int32_t type,int32_t datalen) + { + struct iguana_bundlereq *req; int32_t allocsize; + allocsize = (uint32_t)sizeof(*req) + datalen; + req = mycalloc(type,1,allocsize); + req->allocsize = allocsize; + req->datalen = datalen; + req->addr = addr; + req->coin = coin; + req->type = type; + return(req); + } + + void iguana_gotblockM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *txdata,struct iguana_msgtx *txarray,uint8_t *data,int32_t recvlen) + { + struct iguana_bundlereq *req; int32_t i,z,fpos,bundlei; FILE *fp; char fname[1024]; + if ( 0 ) + { + for (i=0; ispace[0]; i++) + if ( txdata->space[i] != 0 ) + break; + if ( i != txdata->space[0] ) + { + for (i=0; ispace[0]; i++) + printf("%02x ",txdata->space[i]); + printf("extra\n"); + } + } + req = iguana_bundlereq(coin,addr,'B',0); + if ( addr != 0 ) + { + if ( addr->pendblocks > 0 ) + addr->pendblocks--; + addr->lastblockrecv = (uint32_t)time(NULL); + addr->recvblocks += 1.; + addr->recvtotal += recvlen; + if ( (txdata= iguana_blockramchainPT(coin,addr,txdata,txarray,txdata->block.txn_count,data,recvlen)) != 0 ) + { + //fpos = (addr->fp != 0) ? ftell(addr->fp) : 0; + //txdatabits = iguana_calctxidbits(addr->addrind,addr->filecount,(uint32_t)fpos,txdata->datalen); + //txdatabits = iguana_peerfilePT(coin,addr,txdata->block.hash2,txdatabits,txdata->datalen); + fpos = 0; + if ( (bundlei= iguana_peerfname(coin,fname,addr->ipbits,txdata->block.hash2)) < 0 ) + { + if ( (fp= fopen(fname,"wb")) != 0 ) + coin->peers.numfiles++; + } + else + { + if ( (fp= fopen(fname,"rb+")) == 0 ) + { + if ( (fp= fopen(fname,"wb")) != 0 ) + { + z = -1; + coin->peers.numfiles++; + for (i=0; ichain->bundlesize; i++) + fwrite(&z,1,sizeof(z),fp); + fclose(fp); + fp = fopen(fname,"rb+"); + } + } + if ( fp != 0 ) + { + fseek(fp,0,SEEK_END); + fpos = (int32_t)ftell(fp); + } + } + if ( fp != 0 ) + { + txdata->block.bundlei = bundlei; + //printf("fpos.%d: bundlei.%d datalen.%d\n",fpos,bundlei,txdata->datalen); + fwrite(&bundlei,1,sizeof(bundlei),fp); + fwrite(&txdata->block.hash2,1,sizeof(txdata->block.hash2),fp); + fwrite(&txdata->datalen,1,sizeof(txdata->datalen),fp); + fwrite(txdata,1,txdata->datalen,fp); + if ( bundlei >= 0 && bundlei < coin->chain->bundlesize ) + { + fseek(fp,bundlei * sizeof(bundlei),SEEK_SET); + //printf("bundlei[%d] <- fpos.%d\n",bundlei,fpos); + fwrite(&fpos,1,sizeof(fpos),fp); + } else printf("error saving with bundlei.%d vs %d\n",bundlei,coin->chain->bundlesize); + fclose(fp); + //for (i=0; inumpkinds; i++) + // printf("%016lx ",*(long *)((struct iguana_pkhash *)((long)txdata + txdata->pkoffset))[i].rmd160); + //printf("create.(%s) %d ",fname,bundlei,coin->peers.numfiles); + //printf("bundlei.%d datalen.%d T.%d U.%d S.%d P.%d X.%d\n",bundlei,txdata->datalen,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,txdata->numexternaltxids); + { + struct iguana_txblock *checktxdata; struct iguana_memspace checkmem; int32_t checkbundlei; + memset(&checkmem,0,sizeof(checkmem)); + iguana_meminit(&checkmem,"checkmem",0,txdata->block.recvlen + 4096,0); + if ( 0 && (checktxdata= iguana_peertxdata(coin,&checkbundlei,fname,&checkmem,addr->ipbits,txdata->block.hash2)) != 0 ) + { + printf("check datalen.%d bundlei.%d T.%d U.%d S.%d P.%d X.%d\n",checktxdata->datalen,checkbundlei,checktxdata->numtxids,checktxdata->numunspents,checktxdata->numspends,checktxdata->numpkinds,checktxdata->numexternaltxids); + } + } + } + req->datalen = txdata->datalen; + } + } + coin->recvcount++; + coin->recvtime = (uint32_t)time(NULL); + req->block = txdata->block; + req->addr = addr; + req->block.txn_count = req->numtx = txdata->block.txn_count; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); + } + + void iguana_gottxidsM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *txids,int32_t n) + { + struct iguana_bundlereq *req; + printf("got %d txids from %s\n",n,addr->ipaddr); + req = iguana_bundlereq(coin,addr,'T',0); + req->hashes = txids, req->n = n; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); + } + + void iguana_gotunconfirmedM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgtx *tx,uint8_t *data,int32_t datalen) + { + struct iguana_bundlereq *req; + char str[65]; bits256_str(str,tx->txid); + printf("%s unconfirmed.%s\n",addr->ipaddr,str); + req = iguana_bundlereq(coin,addr,'U',datalen); + req->datalen = datalen; + memcpy(req->serialized,data,datalen); + //iguana_freetx(tx,1); + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); + } + + void iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n) + { + struct iguana_bundlereq *req; + if ( addr != 0 ) + { + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + //printf("%s blocks[%d] ht.%d gotheaders pend.%d %.0f\n",addr->ipaddr,n,blocks[0].height,addr->pendhdrs,milliseconds()); + } + req = iguana_bundlereq(coin,addr,'H',0); + req->blocks = blocks, req->n = n; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); + } + + void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashes,int32_t n) + { + struct iguana_bundlereq *req; + if ( addr != 0 ) + { + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + } + req = iguana_bundlereq(coin,addr,'S',0); + req->hashes = blockhashes, req->n = n; + //printf("bundlesQ blockhashes.%p[%d]\n",blockhashes,n); + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); + } + + // main context, ie single threaded + + struct iguana_block *iguana_recvblockhdr(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,struct iguana_block *origblock,int32_t *newhwmp) + { + struct iguana_bundle *prevbp,*bp = 0; int32_t j,prevbundlei; struct iguana_block *block; char str[65]; + (*bpp) = 0; + *bundleip = -2; + if ( (block= iguana_blockhashset(coin,-1,origblock->hash2,1)) == 0 ) + { + printf("error getting block for %s\n",bits256_str(str,origblock->hash2)); + return(0); + } + block->prev_block = origblock->prev_block; + if ( (bp= iguana_bundlefind(coin,bundleip,block->hash2,IGUANA_SEARCHBUNDLE)) == 0 ) + { + if ( (prevbp= iguana_bundlefind(coin,&prevbundlei,block->prev_block,IGUANA_SEARCHBUNDLE)) == 0 ) + { + printf("cant find prev.%s either\n",bits256_str(str,block->prev_block)); + for (j=0; jbundlescount; j++) + { + if ( (bp= coin->bundles[j]) != 0 ) + { + if ( (bp= iguana_bundlescan(coin,bundleip,bp,block->hash2,IGUANA_SEARCHBUNDLE)) != 0 ) + { + (*bpp) = bp; + char str[65]; + bits256_str(str,block->hash2); + printf("FOUND.%s in bundle.[%d:%d] %d\n",str,bp->ramchain.hdrsi,*bundleip,bp->ramchain.bundleheight + *bundleip); + iguana_bundleblockadd(coin,bp,*bundleip,block->hash2); + return(block); + } + } + } + char str[65]; + bits256_str(str,block->hash2); + printf("CANTFIND.%s\n",str); + return(block); + } + else + { + (*bpp) = prevbp; + char str[65]; + //printf("found bp.%p prevbundlei.%d\n",prevbp,prevbundlei); + if ( prevbundlei >= 0 && prevbundlei < coin->chain->bundlesize-1 ) + { + *bundleip = prevbundlei + 1; + if ( prevbundlei == 0 ) + iguana_blockQ(coin,bp,0,block->prev_block,1); + if ( prevbp != 0 ) + { + //bits256_str(str,block->hash2); + //printf("prev FOUND.%s in bundle.[%d:%d] %d\n",str,prevbp->ramchain.hdrsi,*bundleip,prevbp->ramchain.bundleheight + *bundleip); + iguana_bundleblockadd(coin,prevbp,*bundleip,block->hash2); + } + } + if ( 0 && prevbundlei == coin->chain->bundlesize-1 ) + { + bits256 zero; + memset(zero.bytes,0,sizeof(zero)); + bits256_str(str,block->hash2); + printf("prev AUTOCREATE.%s\n",str); + iguana_bundlecreate(coin,block->hash2,zero); + } + return(block); + } + } + else + { + //char str[65],str2[65]; + (*bpp) = bp; + //printf("blockadd.%s %s %d\n",bits256_str(str,block->hash2),bits256_str(str2,origblock->hash2),*bundleip); + iguana_bundleblockadd(coin,bp,*bundleip,block->hash2); + if ( *bundleip > 0 && bits256_nonz(block->prev_block) > 0 ) + iguana_bundleblockadd(coin,bp,(*bundleip) - 1,block->prev_block); + } + return(block); + } + + struct iguana_bundlereq *iguana_recvblockhashes(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *blockhashes,int32_t num) + { + struct iguana_bundle *bp,*newbp; bits256 zero; int32_t i,j,newbundlei,missing,bundlei = -2,bundleheight = -1; + memset(zero.bytes,0,sizeof(zero)); + if ( (bp= iguana_bundlefind(coin,&bundlei,blockhashes[1],IGUANA_SEARCHBUNDLE)) != 0 ) + { + if ( bp->blockhashes == 0 ) + { + //iguana_blockQ(coin,bp,0,bp->bundlehash2,1); + bundleheight = bp->ramchain.bundleheight; + if ( num > coin->chain->bundlesize+1 ) + num = coin->chain->bundlesize+1; + //printf("GOT blockhashes.%s[%d] %d %p hdrsi.%d bundlei.%d\n",bits256_str(str,blockhashes[1]),num,bundleheight,bp->blockhashes,bp->ramchain.hdrsi,bundlei); + memcpy(bp->blockhashes,blockhashes,num * sizeof(*blockhashes)); + bp->n = num; + bp->ramchain.bundleheight = bundleheight; + if ( bundlei >= 0 && bundlei < bp->n ) + { + j = 1; + if ( bundlei != 1 ) + { + /*if ( bundlei == 0 ) + { + for (i=1; i>>>>>>>> hdrsi.%d bundlei.%d j.%d\n",bp->ramchain.hdrsi,bundlei,j); + return(req); + } + for (; jn && bundlei<=coin->chain->bundlesize; bundlei++,j++) + { + //printf("%d: bundlei.%d %s j.%d\n",bundlei % coin->chain->bundlesize,bundlei,bits256_str(str,blockhashes[j]),j); + if ( bundlei == coin->chain->bundlesize ) + { + if ( (newbp= iguana_bundlefind(coin,&newbundlei,blockhashes[j],IGUANA_SEARCHBUNDLE)) == 0 ) + { + //iguana_blockQ(coin,newbp,0,blockhashes[j],1); + if ( j < bp->n-1 ) + { + newbp = iguana_bundlecreate(coin,blockhashes[j],blockhashes[j+1]); + //iguana_blockQ(coin,newbp,1,blockhashes[j+1],1); + } + else newbp = iguana_bundlecreate(coin,blockhashes[j],zero); + if ( newbp != 0 ) + { + char str[65]; + if ( bp->ramchain.bundleheight >= 0 ) + newbp->ramchain.bundleheight = (bp->ramchain.bundleheight + coin->chain->bundlesize); + init_hexbytes_noT(str,blockhashes[j].bytes,sizeof(bits256)); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1); + } + } + } + else if ( 1 && iguana_bundleblockadd(coin,bp,bundlei,blockhashes[j]) == 0 ) + break; + } + } + //iguana_blockQ(coin,bp,1,blockhashes[1],1); + //if ( bp->n < coin->chain->bundlesize ) + // iguana_blockQ(coin,bp,bp->n-1,blockhashes[bp->n-1],1); + //else iguana_blockQ(coin,bp,coin->chain->bundlesize-1,blockhashes[coin->chain->bundlesize-1],1); + } + else + { + if ( num > 2 ) + { + for (i=missing=0; in && ichain->bundlesize; i++) + { + if ( iguana_bundlescan(coin,&bundlei,bp,blockhashes[i],IGUANA_SEARCHBUNDLE) == 0 ) + { + missing++; + } + } + if ( missing != 0 ) + { + //printf("GOT MISMATCHED %d blockhashes.%s[%d] missing.%d of %d\n",bp->ramchain.bundleheight,bits256_str(blockhashes[1]),num,missing,bp->n); + return(req); + } + if ( num > bp->n && bp->n <= coin->chain->bundlesize ) + { + /*myfree(bp->blockhashes,sizeof(*bp->blockhashes) * bp->n); + bp->blockhashes = mycalloc('h',num,sizeof(*blockhashes)); + printf("replace blockhashes.%s[%d] %d %p\n",bits256_str(blockhashes[0]),num,bp->ramchain.bundleheight,bp->blockhashes); + memcpy(bp->blockhashes,blockhashes,num * sizeof(*blockhashes)); + i = bp->n, bp->n = num; + for (; iramchain.bundleheight >= 0 && (rand() % 1000) == 0 ) + printf("GOT duplicate.%s[%d] bheight.%d\n",str,num,bp->ramchain.bundleheight); + } + } + if ( (num= bp->n) > coin->chain->bundlesize ) + num = coin->chain->bundlesize; + } + else + { + if ( num > coin->chain->bundlesize+1 ) + num = coin->chain->bundlesize+1; + //for (i=1; i 2 ) + { + char str[65]; + bits256_str(str,blockhashes[1]); + //printf("recvblockhashes cant find %s num.%d\n",str,num); + //iguana_blockQ(coin,0,-1,blockhashes[1],1); + //iguana_bundlecreate(coin,blockhashes[1],blockhashes[2]); + if ( 0 && num == coin->chain->bundlesize+1 && iguana_bundlefind(coin,&bundlei,blockhashes[num - 1],IGUANA_SEARCHBUNDLE) == 0 ) + { + bits256 zero; + memset(zero.bytes,0,sizeof(zero)); + bits256_str(str,blockhashes[num - 1]); + printf("AUTO EXTEND2.%s[%d]\n",str,num); + iguana_bundlecreate(coin,blockhashes[num - 1],zero); + } + } + } + return(req); + } + + struct iguana_bundlereq *iguana_recvblockhdrs(struct iguana_info *coin,struct iguana_bundlereq *req,struct iguana_block *blocks,int32_t n,int32_t *newhwmp) + { + int32_t i,j; struct iguana_block *block; struct iguana_bundle *bp; + if ( blocks == 0 ) + return(req); + if ( n > coin->chain->bundlesize+1 ) + n = coin->chain->bundlesize+1; + // blockhashes = mycalloc('h',n+1,sizeof(*blockhashes)); + // iguana_hash2set(coin,"recvhdrs0",&bp->blockhashes[0],blocks->prev_block); + //for (i=0; iblockhashes[i+1],blocks[i].hash2); + n++; + for (j=0; jbundlescount; j++) + { + if ( (bp= coin->bundles[j]) != 0 ) + { + if ( memcmp(blocks[0].prev_block.bytes,bp->blockhashes[0].bytes,sizeof(bits256)) == 0 ) + { + // iguana_hash2set(coin,"recvhdrs0",&bp->blockhashes[0],blocks->prev_block); + //for (i=0; iblockhashes[i+1],blocks[i].hash2); + if ( bp->blockhashes == 0 ) + { + bp->n = n < coin->chain->bundlesize ? n : coin->chain->bundlesize; + for (i=1; in; i++) + { + iguana_hash2set(coin,"blockhdrs[i]",&bp->blockhashes[i],blocks[i].hash2); + if ( (block= iguana_blockfind(coin,bp->blockhashes[i])) != 0 ) + iguana_copyblock(coin,block,&blocks[i-1]); + } + /*iguana_blockQ(coin,bp,0,bp->bundlehash2,1); + iguana_blockQ(coin,bp,1,blockhashes[1],1); + if ( bp->n < coin->chain->bundlesize ) + iguana_blockQ(coin,bp,n-1,blockhashes[n-1],1); + else iguana_blockQ(coin,bp,coin->chain->bundlesize-1,blockhashes[coin->chain->bundlesize-1],1);*/ + break; + } + else + { + //printf("free duplicate blockhashes\n"); + // myfree(blockhashes,n*sizeof(*blockhashes)); + } + } + } + } + return(req); + } + + struct iguana_bundlereq *iguana_recvblock(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundlereq *req,struct iguana_block *origblock,int32_t numtx,int32_t datalen,int32_t *newhwmp) + { + struct iguana_bundle *bp; int32_t bundlei; struct iguana_block *block; double duration = 0.; + if ( (block= iguana_recvblockhdr(coin,&bp,&bundlei,origblock,newhwmp)) != 0 ) + { + iguana_copyblock(coin,block,origblock); + //printf("recvblock.(%s) bp.%p bundlei.%d\n",bits256_str(str,block->hash2),bp,bundlei); + if ( bp != 0 && datalen > 0 ) + { + //printf("iguana_recvblock (%s) %d[%d] bit.%d recv.%d %02x %02x\n",bits256_str(str,block->hash2),bp->ramchain.hdrsi,bundlei,GETBIT(bp->recv,bundlei),bp->numrecv,bp->recv[0],bp->recv[bp->n/8]); + SETBIT(bp->recv,bundlei); + if ( bp->issued[bundlei] > 0 ) + { + duration = (int32_t)(milliseconds() - bp->issued[bundlei]); + if ( duration < bp->avetime/10. ) + duration = bp->avetime/10.; + else if ( duration > bp->avetime*10. ) + duration = bp->avetime * 10.; + dxblend(&bp->avetime,duration,.9); + dxblend(&coin->avetime,bp->avetime,.9); + } + /*if ( bundlei < 3 ) + { + if ( bundlei > 0 ) + iguana_blockQ(coin,bp,bundlei-1,block->prev_block,1); + iguana_blockQ(coin,bp,bundlei,block->hash2,1); + } + if ( bundlei == 2 ) + { + bp->firstblockhash2 = bp->blockhashes[1] = block->prev_block; + iguana_blockQ(coin,bp,bundlei,block->prev_block,1); + }*/ + if ( bundlei >= 0 && bundlei < bp->n && bundlei < coin->chain->bundlesize ) + { + if ( 0 && bundlei == 1 ) + printf("iguana_recvblock %d[%d] bit.%d recv.%d %02x %02x\n",bp->ramchain.hdrsi,bundlei,GETBIT(bp->recv,bundlei),bp->numrecv,bp->recv[0],bp->recv[bp->n/8]); + if ( req->addr != 0 && req->addr->ipbits != 0 )//&& req->addr->addrind != 0 ) + block->ipbits = req->addr->ipbits; + else block->ipbits = 0xffff, printf("null addr\n"); + block->recvlen = datalen; + bp->blocks[bundlei] = block; + bp->numrecv++; + //iguana_txdataQ(coin,req,bp,bundlei); + } + + //printf("%s hdrsi.%d recv[%d] dur.%.0f avetimes.(%.2f %.2f) numpendinds.%d %f\n",bits256_str(block->hash2),hdrs->hdrsi,bundlei,duration,hdrs->avetime,coin->avetime,coin->numpendings,hdrs->issued[bundlei]); + } + } + else //if ( (rand() % 100) == 0 ) + printf("cant create block.%llx\n",(long long)origblock->hash2.txid); + return(req); + } + + struct iguana_bundlereq *iguana_recvtxids(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *txids,int32_t n) + { + return(req); + } + + struct iguana_bundlereq *iguana_recvunconfirmed(struct iguana_info *coin,struct iguana_bundlereq *req,uint8_t *data,int32_t datalen) + { + return(req); + } + + int32_t iguana_processbundlesQ(struct iguana_info *coin,int32_t *newhwmp) // single threaded + { + int32_t flag = 0; struct iguana_bundlereq *req; + *newhwmp = 0; + while ( flag < 10000 && (req= queue_dequeue(&coin->bundlesQ,0)) != 0 ) + { + //printf("%s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n); + if ( req->type == 'B' ) // one block with all txdata + req = iguana_recvblock(coin,req->addr,req,&req->block,req->numtx,req->datalen,newhwmp); + else if ( req->type == 'H' ) // blockhdrs (doesnt have txn_count!) + { + if ( (req= iguana_recvblockhdrs(coin,req,req->blocks,req->n,newhwmp)) != 0 ) + { + if ( req->blocks != 0 ) + myfree(req->blocks,sizeof(*req->blocks) * req->n), req->blocks = 0; + } + } + else if ( req->type == 'S' ) // blockhashes + { + if ( (req= iguana_recvblockhashes(coin,req,req->hashes,req->n)) != 0 && req->hashes != 0 ) + myfree(req->hashes,sizeof(*req->hashes) * req->n), req->hashes = 0; + } + else if ( req->type == 'U' ) // unconfirmed tx + req = iguana_recvunconfirmed(coin,req,req->serialized,req->datalen); + else if ( req->type == 'T' ) // txids from inv + { + if ( (req= iguana_recvtxids(coin,req,req->hashes,req->n)) != 0 ) + myfree(req->hashes,(req->n+1) * sizeof(*req->hashes)), req->hashes = 0; + } + else printf("iguana_updatebundles unknown type.%c\n",req->type); + flag++; + if ( req != 0 ) + myfree(req,req->allocsize), req = 0; + } + return(flag); + } + + + int32_t iguana_issueloop(struct iguana_info *coin) + { + static uint32_t lastdisp; + int32_t i,closestbundle,bundlei,qsize,RTqsize,m,numactive,numwaiting,maxwaiting,lastbundle,n,dispflag = 0,flag = 0; + int64_t remaining,closest; struct iguana_bundle *bp,*prevbp,*nextbp; bits256 hash2; struct iguana_block *block; + if ( time(NULL) > lastdisp+13 ) + { + dispflag = 1; + lastdisp = (uint32_t)time(NULL); + } + qsize = queue_size(&coin->blocksQ); + if ( qsize == 0 ) + coin->bcount++; + else coin->bcount = 0; + maxwaiting = (coin->MAXBUNDLES * coin->chain->bundlesize); + numwaiting = 0; + numactive = 0; + prevbp = nextbp = 0; + lastbundle = -1; + for (i=coin->bundlescount-1; i>=0; i--) + if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 && bp->blockhashes != 0 ) + { + lastbundle = i; + break; + } + if ( lastbundle != coin->lastbundle ) + coin->lastbundletime = (uint32_t)time(NULL); + coin->lastbundle = lastbundle; + if ( 0 && time(NULL) < coin->starttime+60 ) + lastbundle = -1; + n = 0; + closest = closestbundle = -1; + for (i=0; ibundlescount; i++) + { + qsize = queue_size(&coin->blocksQ); + m = 0; + if ( (bp= coin->bundles[i]) != 0 ) + { + nextbp = (i < coin->bundlescount-1) ? coin->bundles[i+1] : 0; + if ( bp->emitfinish == 0 ) + { + //iguana_bundlecheck(coin,bp,numactive == 0 || i == coin->closestbundle || i == lastbundle); + iguana_bundlecheck(coin,bp,i == coin->closestbundle); + if ( bp->numrecv > 3 || numactive == 0 ) + { + numactive++; + remaining = (bp->estsize - bp->datasize) + (rand() % (1 + bp->estsize))/100; + if ( remaining > 0 && (closest < 0 || remaining < closest) ) + { + //printf("closest.[%d] %d -> R.%d (%d - %d)\n",closestbundle,(int)closest,(int)remaining,(int)bp->estsize,(int)bp->datasize); + closest = remaining; + closestbundle = i; + } + } + //if ( i < (coin->numemitted+coin->MAXPENDING) && numactive >= coin->MAXPENDING && i != coin->closestbundle && i != lastbundle ) + continue; + RTqsize = queue_size(&coin->blocksQ); + for (bundlei=0; bundlein && bundleichain->bundlesize; bundlei++) + { + if ( (block= bp->blocks[bundlei]) != 0 && block->ipbits != 0 ) + { + m++; + //printf("hashes.%p numrecv.%d hdrs->n.%d qsize.%d\n",bp->blockhashes,bp->numrecv,bp->n,qsize); + continue; + } + hash2 = iguana_bundleihash2(coin,bp,bundlei); + if ( bits256_nonz(hash2) > 0 ) + { + //printf("hdrsi.%d qsize.%d bcount.%d check bundlei.%d bit.%d %.3f lag %.3f ave %.3f\n",bp->ramchain.hdrsi,qsize,coin->bcount,bundlei,GETBIT(bp->recv,bundlei),bp->issued[bundlei],milliseconds() - bp->issued[bundlei],bp->avetime); + if ( (block= bp->blocks[bundlei]) == 0 || block->ipbits == 0 ) + //if ( GETBIT(bp->recv,bundlei) == 0 ) + { + if ( bp->issued[bundlei] > SMALLVAL ) + numwaiting++; + if ( numwaiting < maxwaiting && (bp->issued[bundlei] == 0 || (qsize == 0 && coin->bcount > 100 && milliseconds() > (bp->issued[bundlei] + bp->avetime*2))) )//()) ) + { + if ( RTqsize < maxwaiting && (i == lastbundle || i == coin->closestbundle) ) + { + char str[65]; + bits256_str(str,hash2); + if ( (rand() % 10000) == 0 && bp->issued[bundlei] > SMALLVAL ) + printf("issue.%d:%d of %d %s lag %f ave %f\n",bp->ramchain.hdrsi,bundlei,bp->n,str,milliseconds() - bp->issued[bundlei],bp->avetime); + bp->issued[bundlei] = milliseconds(); + n++; + flag += (iguana_blockQ(coin,bp,bundlei,hash2,0) > 0); + } + } + } + } //lse printf("skip.%d %s\n",numbundles,bits256_str(hash2)); + } + } else m = coin->chain->bundlesize; + } + prevbp = bp; + if ( dispflag != 0 && bp != 0 && bp->emitfinish == 0 && m > 0 ) + printf("%s",iguana_bundledisp(coin,prevbp,bp,nextbp,m)); + } + //if ( closestbundle >= 0 && (coin->closestbundle < 0 || coin->bundles[coin->closestbundle]->numrecv >= coin->chain->bundlesize) ) + coin->closestbundle = closestbundle; + char str[65]; + if ( dispflag != 0 ) + printf(" PENDINGBUNDLES lastbundle.%d closest.[%d] %s | %d\n",lastbundle,closestbundle,mbstr(str,closest),coin->closestbundle); + return(flag); + } + + int32_t iguana_reqhdrs(struct iguana_info *coin) + { + int32_t i,n = 0; struct iguana_bundle *bp; char hashstr[65]; + //printf("needhdrs.%d qsize.%d zcount.%d\n",iguana_needhdrs(coin),queue_size(&coin->hdrsQ),coin->zcount); + if ( iguana_needhdrs(coin) > 0 && queue_size(&coin->hdrsQ) == 0 ) + { + if ( coin->zcount++ > 10 ) + { + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + if ( time(NULL) > bp->issuetime+7 )//&& coin->numpendings < coin->MAXBUNDLES ) + { + if ( bp->issuetime == 0 ) + coin->numpendings++; + if ( bp->blockhashes == 0 || bp->n < coin->chain->bundlesize ) + { + char str[65]; + bits256_str(str,bp->blockhashes[0]); + printf("(%s %d).%d ",str,bp->ramchain.bundleheight,i); + init_hexbytes_noT(hashstr,bp->blockhashes[0].bytes,sizeof(bits256)); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1); + n++; + } + bp->issuetime = (uint32_t)time(NULL); + } + } + } + if ( n > 0 ) + printf("REQ HDRS pending.%d\n",coin->numpendings); + coin->zcount = 0; + } + } else coin->zcount = 0; + return(n); + } + + int32_t iguana_updatecounts(struct iguana_info *coin) + { + int32_t h,flag = 0; + //SETBIT(coin->havehash,0); + //while ( iguana_havetxdata(coin,coin->blocks.recvblocks) != 0 ) + // coin->blocks.recvblocks++; + //if ( coin->blocks.recvblocks < 1 ) + // coin->blocks.recvblocks = 1; + //while ( GETBIT(coin->havehash,coin->blocks.hashblocks) > 0 ) + // coin->blocks.hashblocks++; + h = coin->blocks.hwmheight - coin->chain->bundlesize; + flag = 0; + while ( 0 && iguana_bundleready(coin,h) > 0 ) + { + h += coin->chain->bundlesize; + flag++; + } + if ( flag != 0 ) + iguana_savehdrs(coin); + return(flag); + } + + int32_t iguana_processrecv(struct iguana_info *coin) // single threaded + { + int32_t newhwm = 0,flag = 0; + //printf("process bundlesQ\n"); + flag += iguana_processbundlesQ(coin,&newhwm); + //printf("iguana_updatecounts\n"); + flag += iguana_updatecounts(coin); + //printf("iguana_reqhdrs\n"); + flag += iguana_reqhdrs(coin); + //printf("iguana_issueloop\n"); + flag += iguana_issueloop(coin); + //if ( newhwm != 0 ) + // flag += iguana_lookahead(coin,&hash2,coin->blocks.hwmheight); + return(flag); + } + + + struct iguana_block *iguana_recvblockhdr(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,struct iguana_block *origblock,int32_t *newhwmp) + { + struct iguana_bundle *prevbp,*bp = 0; int32_t j,prevbundlei; struct iguana_block *block; char str[65]; + (*bpp) = 0; + *bundleip = -2; + if ( (block= iguana_blockhashset(coin,-1,origblock->hash2,1)) == 0 ) + { + printf("error getting block for %s\n",bits256_str(str,origblock->hash2)); + return(0); + } + block->prev_block = origblock->prev_block; + if ( (bp= iguana_bundlefind(coin,bundleip,block->hash2,IGUANA_SEARCHBUNDLE)) == 0 ) + { + if ( (prevbp= iguana_bundlefind(coin,&prevbundlei,block->prev_block,IGUANA_SEARCHBUNDLE)) == 0 ) + { + printf("cant find prev.%s either\n",bits256_str(str,block->prev_block)); + for (j=0; jbundlescount; j++) + { + if ( (bp= coin->bundles[j]) != 0 ) + { + if ( (bp= iguana_bundlescan(coin,bundleip,bp,block->hash2,IGUANA_SEARCHBUNDLE)) != 0 ) + { + (*bpp) = bp; + char str[65]; + bits256_str(str,block->hash2); + printf("FOUND.%s in bundle.[%d:%d] %d\n",str,bp->ramchain.hdrsi,*bundleip,bp->ramchain.bundleheight + *bundleip); + iguana_bundleblockadd(coin,bp,*bundleip,block->hash2); + return(block); + } + } + } + char str[65]; + bits256_str(str,block->hash2); + printf("CANTFIND.%s\n",str); + return(block); + } + else + { + (*bpp) = prevbp; + char str[65]; + //printf("found bp.%p prevbundlei.%d\n",prevbp,prevbundlei); + if ( prevbundlei >= 0 && prevbundlei < coin->chain->bundlesize-1 ) + { + *bundleip = prevbundlei + 1; + if ( prevbundlei == 0 ) + iguana_blockQ(coin,bp,0,block->prev_block,1); + if ( prevbp != 0 ) + { + //bits256_str(str,block->hash2); + //printf("prev FOUND.%s in bundle.[%d:%d] %d\n",str,prevbp->ramchain.hdrsi,*bundleip,prevbp->ramchain.bundleheight + *bundleip); + iguana_bundleblockadd(coin,prevbp,*bundleip,block->hash2); + } + } + if ( 0 && prevbundlei == coin->chain->bundlesize-1 ) + { + bits256 zero; + memset(zero.bytes,0,sizeof(zero)); + bits256_str(str,block->hash2); + printf("prev AUTOCREATE.%s\n",str); + iguana_bundlecreate(coin,block->hash2,zero); + } + return(block); + } + } + else + { + //char str[65],str2[65]; + (*bpp) = bp; + //printf("blockadd.%s %s %d\n",bits256_str(str,block->hash2),bits256_str(str2,origblock->hash2),*bundleip); + iguana_bundleblockadd(coin,bp,*bundleip,block->hash2); + if ( *bundleip > 0 && bits256_nonz(block->prev_block) > 0 ) + iguana_bundleblockadd(coin,bp,(*bundleip) - 1,block->prev_block); + } + return(block); + } + + /*static int32_t _sort_by_itemind(struct iguana_block *a, struct iguana_block *b) + { + if (a->hh.itemind == b->hh.itemind) return 0; + return (a->hh.itemind < b->hh.itemind) ? -1 : 1; + }*/ + + int32_t _iguana_verifysort(struct iguana_info *coin) + { + int32_t height,prevheight = -1,i = 0,run = 0; struct iguana_block *block,*tmp; + HASH_ITER(hh,coin->blocks.hash,block,tmp) + { + if ( (height= block->hh.itemind) < 0 ) + printf("sortblocks error i.%d height.%d?\n",i,height), getchar(); + if ( height <= prevheight ) + printf("sortblocks error i.%d height.%d vs prevheight.%d\n",i,height,prevheight), getchar(); + if ( height == run ) + run++; + i++; + } + printf("_iguana_verifysort: n.%d run.%d\n",i,run); + return(run); + } + + /*int32_t iguana_blocksort(struct iguana_info *coin) + { + int32_t hashblocks; + portable_mutex_lock(&coin->blocks_mutex); + HASH_SORT(coin->blocks.hash,_sort_by_itemind); + hashblocks = _iguana_verifysort(coin); + portable_mutex_unlock(&coin->blocks_mutex); + return(hashblocks); + }*/ + + int32_t _iguana_blocklink(struct iguana_info *coin,struct iguana_block *block) + { + int32_t height,n = 0; struct iguana_block *prev,*next; + if ( block == 0 ) + printf("iguana_blockslink: illegal null block %p\n",block), getchar(); + block->hh.next = 0, block->hh.prev = 0; + if ( (height= (int32_t)block->hh.itemind) > 0 && (prev= iguana_block(coin,height-1)) != 0 ) + { + prev->hh.next = block; + block->hh.prev = prev; + n++; + } + if ( (next= iguana_block(coin,height+1)) != 0 ) + { + block->hh.next = next; + next->hh.prev = block; + n++; + } + return(n); + } + + /*bits256 iguana_prevblockhash(struct iguana_info *coin,bits256 hash2) + { + struct iguana_block *block; bits256 tmp; + if ( bits256_nonz(hash2) > 0 && (block= iguana_blockfind(coin,hash2)) != 0 ) + return(block->prev_block); + else + { + memset(tmp.bytes,0,sizeof(tmp)); + return(tmp); + } + }*/ + + int32_t iguana_hash2height(struct iguana_info *coin,bits256 hash2) + { + struct iguana_block *block; + if ( (block= iguana_blockfind(coin,hash2)) != 0 ) + { + if ( block->height >= 0 ) + return(block->height); + else return(block->hh.itemind); + } + else return(-1); + } + + int32_t iguana_blockheight(struct iguana_info *coin,struct iguana_block *block) + { + struct iguana_block *prev; int32_t height; + if ( (height= iguana_hash2height(coin,block->hash2)) < 0 ) + { + if ( (prev= iguana_blockfind(coin,block->prev_block)) != 0 ) + { + if ( prev->height >= 0 ) + return(prev->height+1); + else if ( (int32_t)prev->hh.itemind >= 0 ) + return(prev->hh.itemind + 1); + } + } + return(-1); + } + + int32_t iguana_chainheight(struct iguana_info *coin,struct iguana_block *block) + { + if ( block->mainchain != 0 && block->height >= 0 ) + return(block->height); + return(-1); + } + + void *iguana_blockptr(struct iguana_info *coin,int32_t height) + { + struct iguana_block *block; + if ( height < 0 || height >= coin->blocks.maxbits ) + { + //printf("iguana_blockptr height.%d vs maxbits.%d\n",height,coin->blocks.maxbits); + return(0); + } + if ( (block= coin->blocks.ptrs[height]) != 0 ) + return(block); + return(0); + } + + /*void *iguana_bundletxdata(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei) + { + struct iguana_block *block; void *txdata = 0; + if ( bp != 0 && bundlei >= 0 && bundlei < coin->chain->bundlesize && GETBIT(bp->recv,bundlei) != 0 && (block= bp->blocks[bundlei]) != 0 ) + { + txdata = block->txdata; + } + //printf("txdata.%p\n",txdata); + return(txdata); + }*/ + + int32_t iguana_avail(struct iguana_info *coin,int32_t height,int32_t n) + { + int32_t i,nonz = 0; + for (i=0; ichain->bundlesize; + if ( GETBIT(coin->bundleready,height/num) != 0 ) + return(1); + for (i=0; ibundleready,height/num); + return(1); + } + + int32_t iguana_fixblocks(struct iguana_info *coin,int32_t startheight,int32_t endheight) + { + struct iguana_block *block,space,origblock; int32_t height,n = 0; + for (height=startheight; height<=endheight; height++) + { + if ( (block= iguana_block(coin,&space,height)) != 0 ) + { + origblock = space; + iguana_setdependencies(coin,block); + if ( memcmp(&origblock,block,sizeof(origblock)) != 0 ) + { + printf("%d ",height); + n++; + iguana_kvwrite(coin,coin->blocks.db,0,block,(uint32_t *)&block->height); + } + } + } + iguana_syncmap(&coin->blocks.db->M,0); + return(n); + } + + int32_t iguana_blockcmp(struct iguana_info *coin,struct iguana_block *A,struct iguana_block *B,int32_t fastflag) + { + struct iguana_block tmpA,tmpB; + tmpA = *A, tmpB = *B; + memset(&tmpA.L,0,sizeof(tmpA.L)), memset(&tmpB.L,0,sizeof(tmpB.L)); + memset(&tmpA.hh,0,sizeof(tmpA.hh)), memset(&tmpB.hh,0,sizeof(tmpB.hh)); + tmpA.numvouts = tmpA.numvins = tmpA.tbd = tmpB.numvouts = tmpB.numvins = tmpB.tbd = 0; + if ( memcmp(&tmpA,&tmpB,sizeof(tmpA)) != 0 ) + return(-1); + if ( fastflag == 0 ) + { + if ( iguana_setdependencies(coin,&tmpA) != iguana_setdependencies(coin,&tmpB) || memcmp(&tmpA,&tmpB,sizeof(tmpA)) == 0 ) + return(-1); + } + return(0); + }*/ + + /* + int32_t iguana_checkblock(struct iguana_info *coin,int32_t dispflag,struct iguana_block *block,bits256 hash2) + { + struct iguana_block checkspace,prevspace,*checkblock,*prev; bits256 prevhash; int32_t retval = 0; + if ( block != 0 ) + { + if ( (checkblock= iguana_block(coin,&checkspace,block->height)) == 0 ) + { + if ( dispflag != 0 ) + printf("cant find checkblock %s at %d\n",bits256_str(hash2),block->height); + return(-2); + } + if ( memcmp(block,checkblock,sizeof(*block)) != 0 ) + { + if ( dispflag != 0 ) + printf("compare error %s block.%d vs checkblock.%d\n",bits256_str(hash2),block->height,checkblock->height); + return(-3); + } + prevhash = iguana_prevblockhash(coin,hash2); + if ( bits256_nonz(prevhash) != 0 ) + { + if ( memcmp(prevhash.bytes,block->prev_block.bytes,sizeof(prevhash)) != 0 ) + { + if ( dispflag != 0 ) + { + printf("height.%d block->prev %s vs ",block->height,bits256_str(block->prev_block)); + printf("prevhash mismatch %s\n",bits256_str(prevhash)); + } + return(-4); + } + } else prevhash = block->prev_block; + if ( block->height == 0 ) + { + //printf("reached genesis! numvalid.%d from %s\n",numvalid,bits256_str(coin->blocks.best_chain)); + return(0); + } + //printf("block.%d\n",block->height); + if ( (prev= iguana_blockfind(coin,&prevspace,prevhash)) == 0 ) + { + if ( dispflag != 0 ) + printf("cant find prevhash for (%s).%d\n",bits256_str(hash2),block->height); + return(-5); + } //else printf("block->height.%d prev height.%d %s\n",block->height,prev->height,bits256_str(prevhash)); + if ( fabs(block->L.PoW - (prev->L.PoW + PoW_from_compact(block->bits,coin->chain->unitval))) > SMALLVAL ) + { + if ( dispflag != 0 ) + printf("PoW mismatch: %s %.15f != %.15f (%.15f %.15f)\n",bits256_str(hash2),block->L.PoW,(prev->L.PoW + PoW_from_compact(block->bits,coin->chain->unitval)),prev->L.PoW,PoW_from_compact(block->bits,coin->chain->unitval)); + block->L.PoW = (prev->L.PoW + PoW_from_compact(block->bits,coin->chain->unitval)); + retval = -1000; + } + if ( block->txn_count != 0 && block->L.numtxids != (prev->L.numtxids + prev->txn_count) && block->L.numunspents != (prev->L.numunspents + prev->numvouts) && block->L.numspends != (prev->L.numspends + prev->numvins) ) + { + if ( dispflag != 0 ) + printf("firsttxidind mismatch %s T%d != %d (%d + %d) || U%d != %d (%d + %d) || S%d != %d (%d + %d)\n",bits256_str(hash2),block->L.numtxids,(prev->L.numtxids + prev->txn_count),prev->L.numtxids,prev->txn_count,block->L.numunspents,(prev->L.numunspents + prev->numvouts),prev->L.numunspents,prev->numvouts,block->L.numspends,(prev->L.numspends + prev->numvins),prev->L.numspends,prev->numvins); + block->L.numtxids = (prev->L.numtxids + prev->txn_count); + block->L.numunspents = (prev->L.numunspents + prev->numvouts); + block->L.numspends = (prev->L.numspends + prev->numvins); + return(retval - 10000); + } + return(retval); + } + if ( dispflag != 0 ) + printf("iguana_checkblock: null ptr\n"); + return(-8); + } + + int32_t _iguana_audit(struct iguana_info *coin) + { + bits256 hash2; struct iguana_block *block,space; int32_t numvalid = 0; + hash2 = coin->blocks.hwmchain; + while ( (block= iguana_blockfind(coin,&space,hash2)) != 0 ) + { + if ( iguana_checkblock(coin,1,block,hash2) == 0 ) + { + numvalid++; + if ( block->height == 0 ) + return(numvalid); + hash2 = block->prev_block; + } + } + printf("iguana_audit numvalid.%d vs %d\n",numvalid,coin->blocks.hwmheight); + return(numvalid); + } + + void iguana_audit(struct iguana_info *coin) + { + int32_t numvalid; + if ( (numvalid= _iguana_audit(coin)) < 0 || numvalid != coin->blocks.hwmheight ) + { + printf("iguana_audit error.%d\n",numvalid); + iguana_kvdisp(coin,coin->blocks.db); + } + }*/ + + + /*int32_t iguana_lookahead(struct iguana_info *coin,bits256 *hash2p,int32_t height) + { + struct iguana_block space,*block; bits256 hash2; int32_t err,h,n = 0; + while ( (block= iguana_block(coin,&space,height)) != 0 ) + { + *hash2p = hash2 = iguana_blockhash(coin,height); + if ( (err= iguana_checkblock(coin,1,block,hash2)) == 0 || err <= -1000 ) + { + if ( err < 0 ) + { + h = height; + printf("fixup height.%d\n",height); + iguana_kvwrite(coin,coin->blocks.db,hash2.bytes,block,(uint32_t *)&h); + //getchar(); + } + if ( (h= iguana_addblock(coin,hash2,block)) != height ) + { + printf("height.%d h.%d n.%d didnt work\n",height,h,n); + //getchar(); + break; + } + n++; + height++; + coin->blocks.hwmheight = height; + } + else + { + printf("height.%d %s error.%d\n",height,bits256_str(hash2),err); + break; + } + } + printf("lookahead stopped at height.%d\n",height); + return(n); + } + */ + + int32_t iguana_setchainvars(struct iguana_info *coin,struct iguana_prevdep *lp,bits256 hash2,uint32_t nBits,bits256 prevhash,int32_t txn_count) // uint32_t *firsttxidindp,uint32_t *firstvoutp,uint32_t *firstvinp,double *PoWp + { + int32_t height=-1,firstvout=0,firstvin=0,firsttxidind=0; double PoW; + struct iguana_prevdep *prevlp; struct iguana_block *prev; + memset(lp,0,sizeof(*lp)); + if ( memcmp(coin->chain->genesis_hashdata,hash2.bytes,sizeof(hash2)) == 0 ) + { + PoW = PoW_from_compact(nBits,coin->chain->unitval); + height = 0; + firsttxidind = firstvout = firstvin = 1; + printf("set genesis vars nBits.%x\n",nBits); + } + else + { + if ( (prev= iguana_blockfind(coin,prevhash)) == 0 ) + { + if ( iguana_needhdrs(coin) == 0 ) + { + char str[65],str2[65]; + bits256_str(str,hash2); + bits256_str(str2,prevhash); + printf("hash2.(%s) ",str); + fprintf(stderr,"iguana_blockchain no prev block.(%s)\n",str2); + //getchar(); + } + return(-1); + } + else + { + height = prev->height + 1; + if ( (prevlp= iguana_prevdepfind(coin,prev)) != 0 ) + { + PoW = (PoW_from_compact(nBits,coin->chain->unitval) + prevlp->PoW); + if ( txn_count > 0 && prevlp->numtxids > 0 && prev->numvouts > 0 && prevlp->numunspents > 0 && prevlp->numspends > 0 ) + { + firsttxidind = prevlp->numtxids + prev->txn_count; + firstvout = prevlp->numunspents + prev->numvouts; + firstvin = prevlp->numspends + prev->numvins; + //printf("PREV.%d firsttxidind.%d firstvout.%d+%d firstvin.%d+%d (%d %d %d)\n",prev->height,prev->L.numtxids,prev->L.numunspents,prev->numvouts,prev->L.numspends,prev->numvins,firsttxidind,firstvout,firstvin); + } + } + } + } + if ( lp != 0 ) + { + lp->PoW = PoW; + lp->numtxids = firsttxidind; + lp->numunspents = firstvout; + lp->numspends = firstvin; + } + //printf("set height.%d: %d %f firstvin.%d firstvout.%d\n",height,firsttxidind,PoW,firstvin,firstvout); + return(height); + } + + int32_t iguana_setdependencies(struct iguana_info *coin,struct iguana_block *block,struct iguana_prevdep *lp) + { + int32_t h,height; + if ( block == 0 ) + return(-1); + height = block->height; + if ( (h= iguana_setchainvars(coin,lp,block->hash2,block->bits,block->prev_block,block->txn_count)) == height ) + { + // place to make sure connected to ramchain + return(height); + } + if ( height < 0 ) + block->height = h; + //printf("dependencies returned %d vs %d\n",h,height); + return(-1); + } + + int32_t iguana_chainextend(struct iguana_info *coin,struct iguana_block *newblock) + { + int32_t h; + if ( (newblock->height= iguana_setdependencies(coin,newblock,lp)) >= 0 ) + { + if ( lp->PoW > coin->blocks.hwmPoW ) + { + if ( newblock->height+1 > coin->blocks.maxblocks ) + coin->blocks.maxblocks = (newblock->height + 1); + h = newblock->height; + iguana_kvwrite(coin,coin->blocks.db,hash2.bytes,newblock,(uint32_t *)&h); + coin->blocks.hwmheight = newblock->height; + coin->blocks.hwmPoW = lp->PoW; + coin->blocks.hwmchain = hash2; + coin->latest.blockhash = hash2; + coin->latest.merkle_root = newblock->merkle_root; + coin->latest.timestamp = newblock->timestamp; + coin->latest.height = coin->blocks.hwmheight; + char str[65],str2[65]; + bits256_str(str,newblock->hash2); + bits256_str(str2,coin->blocks.hwmchain); + printf("ADD %s %d:%d <- (%s) n.%u max.%u PoW %f 1st.%d numtx.%d\n",str,h,newblock->height,str2,coin->blocks.hwmheight+1,coin->blocks.maxblocks,lp->PoW,lp->numtxids,newblock->txn_count); + } + } else printf("error from setchain.%d\n",newblock->height); + if ( memcmp(hash2.bytes,coin->blocks.hwmchain.bytes,sizeof(hash2)) != 0 ) + { + char str[65]; + bits256_str(str,hash2); + if ( iguana_needhdrs(coin) == 0 ) + printf("ORPHAN.%s height.%d PoW %f vs best %f\n",str,newblock->height,lp->PoW,coin->blocks.hwmPoW); + newblock->height = -1; + } + return(newblock->height); + } + + else if ( strcmp(H->command,"headers") == 0 ) + { + struct iguana_msgblock msg; struct iguana_block *blocks; uint32_t n; struct iguana_prevdep L; + len = iguana_rwvarint32(0,data,&n); + if ( n <= IGUANA_MAXINV ) + { + blocks = mycalloc('i',1,sizeof(*blocks) * n); + height = -1; + memset(&L,0,sizeof(L)); + for (i=0; i 0 ) + { + height++; + L.numtxids += blocks[i].txn_count; + L.PoW += PoW_from_compact(blocks[i].bits,coin->chain->unitval); + } + } + //printf("GOT HEADERS n.%d len.%d\n",n,len); + iguana_gotheadersM(coin,addr,blocks,n); + //myfree(blocks,sizeof(*blocks) * n); + if ( len == datalen && addr != 0 ) + addr->msgcounts.headers++; + } else printf("got unexpected n.%d for headers\n",n); + } + + /*int32_t iguana_chainheight(struct iguana_info *coin,struct iguana_block *origblock) + { + static const bits256 zero; struct iguana_block *next,*block = origblock; + bits256 *blockhashes; char str[65]; int32_t i,max,toofar=0,height,n=0; + next = origblock; + iguana_memreset(&coin->blockMEM); + max = (int32_t)(coin->blockMEM.totalsize / sizeof(*blockhashes)); + blockhashes = iguana_memalloc(&coin->blockMEM,max*sizeof(*blockhashes),1); + while ( memcmp(block->prev_block.bytes,zero.bytes,sizeof(bits256)) != 0 ) + { + if ( n < max-1 ) + blockhashes[n++] = block->hash2; + else toofar = 1; + if ( (block= iguana_blockfind(coin,block->prev_block)) != 0 ) + { + //printf("i.%d %s chainheight.%d mainchain.%d\n",n,bits256_str(str,block->hash2),block->height,block->mainchain); + if ( block->mainchain != 0 && (height= block->height) >= 0 ) + { + iguana_chainextend(coin,next); + //printf("%s extend.%d from %d toofar.%d\n",bits256_str(str,block->hash2),n,height,toofar); + if ( toofar == 0 ) + { + for (i=0; iheight); + } + if ( iguana_chainextend(coin,next) < 0 ) + { + //printf("%d of %d: cant extend block.%s\n",i,n,bits256_str(str,blockhashes[n-1-i])); + return(origblock->height); + } + next = block; + } + return(origblock->height); + } + //printf("toofar means neg height\n"); + return(-1); + } + next = block; + } else break; + } // reached deadend or too far to link in + //printf("out of chainheight loop\n"); + return(origblock->height); + }*/ + + + /*int32_t iguana_issueloop(struct iguana_info *coin) + { + static uint32_t lastdisp; + int32_t i,closestbundle,qsize,m,numactive,numwaiting,maxwaiting,lastbundle,n,dispflag = 0,flag = 0; + int64_t remaining,closest; struct iguana_bundle *bp,*prevbp,*nextbp; + flag = iguana_reqhdrs(coin); + if ( time(NULL) > lastdisp+13 ) + { + dispflag = 1; + lastdisp = (uint32_t)time(NULL); + } + qsize = queue_size(&coin->blocksQ); + if ( qsize == 0 ) + coin->bcount++; + else coin->bcount = 0; + maxwaiting = (coin->MAXBUNDLES * coin->chain->bundlesize); + numwaiting = 0; + numactive = 0; + prevbp = nextbp = 0; + lastbundle = -1; + for (i=coin->bundlescount-1; i>=0; i--) + if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 && bp->blockhashes != 0 ) + { + lastbundle = i; + break; + } + if ( lastbundle != coin->lastbundle ) + coin->lastbundletime = (uint32_t)time(NULL); + coin->lastbundle = lastbundle; + if ( 0 && time(NULL) < coin->starttime+60 ) + lastbundle = -1; + n = 0; + closest = closestbundle = -1; + for (i=0; ibundlescount; i++) + { + qsize = queue_size(&coin->blocksQ); + m = 0; + if ( (bp= coin->bundles[i]) != 0 ) + { + nextbp = (i < coin->bundlescount-1) ? coin->bundles[i+1] : 0; + if ( bp->emitfinish == 0 ) + { + m = (bp->n - bp->numrecv); + if ( bp->numrecv > 3 || numactive == 0 ) + { + numactive++; + remaining = (bp->estsize - bp->datasize) + (rand() % (1 + bp->estsize))/100; + if ( remaining > 0 && (closest < 0 || remaining < closest) ) + { + //printf("closest.[%d] %d -> R.%d (%d - %d)\n",closestbundle,(int)closest,(int)remaining,(int)bp->estsize,(int)bp->datasize); + closest = remaining; + closestbundle = i; + } + } + if ( dispflag != 0 ) + printf("%s",iguana_bundledisp(coin,prevbp,bp,nextbp,m)); + } + } + prevbp = bp; + } + //if ( closestbundle >= 0 && (coin->closestbundle < 0 || coin->bundles[coin->closestbundle]->numrecv >= coin->chain->bundlesize) ) + coin->closestbundle = closestbundle; + char str[65]; + if ( dispflag != 0 ) + printf(" PENDINGBUNDLES lastbundle.%d closest.[%d] %s | %d\n",lastbundle,closestbundle,mbstr(str,closest),coin->closestbundle); + return(flag); + }*/ + int32_t iguana_updatecounts(struct iguana_info *coin) + { + int32_t flag = 0; + //SETBIT(coin->havehash,0); + //while ( iguana_havetxdata(coin,coin->blocks.recvblocks) != 0 ) + // coin->blocks.recvblocks++; + //if ( coin->blocks.recvblocks < 1 ) + // coin->blocks.recvblocks = 1; + //while ( GETBIT(coin->havehash,coin->blocks.hashblocks) > 0 ) + // coin->blocks.hashblocks++; + return(flag); + } + + //printf("iguana_issueloop\n"); + //flag += iguana_issueloop(coin); + //if ( newhwm != 0 ) + // flag += iguana_lookahead(coin,&hash2,coin->blocks.hwmheight); + + /*struct iguana_block *iguana_blockadd(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,struct iguana_block *origblock) + { + struct iguana_block *checkblock,*block = 0; char str[65]; struct iguana_bundle *bp = *bpp; + int32_t setval,checki,bundlei,bundleheight,bundlesize = coin->chain->bundlesize; + bundlei = *bundleip; + *bundleip = -2; + *bpp = 0; + if ( origblock == 0 ) + return(0); + //iguana_blockhashset(coin,-1,origblock->prev_block,1); + if ( bits256_nonz(origblock->hash2) > 0 && (block= iguana_blockhashset(coin,-1,origblock->hash2,1)) != 0 ) + { + //printf("blockadd.(%s) -> %d (%s)\n",bits256_str(str,origblock->prev_block),block->height,bits256_str(str2,origblock->hash2)); + if ( bits256_nonz(block->prev_block) == 0 ) + iguana_blockcopy(coin,block,origblock); + if ( (bp= *bpp) == 0 ) + { + if ( (bp= iguana_bundlefind(coin,bpp,&bundlei,block->hash2)) == 0 ) + { + *bpp = 0, bundlei = -2; + //printf("a bundlefind.(%s) -> bundlei.%d\n",bits256_str(str,block->hash2),*bundleip); + if ( (bp= iguana_bundlefind(coin,bpp,&bundlei,block->prev_block)) == 0 ) + { + //iguana_chainheight(coin,block); + //printf("a prev bundlefind.(%s) -> bundlei.%d ht.%d\n",bits256_str(str,block->prev_block),*bundleip,block->height); + *bpp = 0; + *bundleip = -2; + return(block); + } + else + { + if ( *bundleip == bundlesize-1 ) + { + printf("b prev bundlefind.(%s) -> bundlei.%d\n",bits256_str(str,block->prev_block),*bundleip); + bundleheight = (bp->ramchain.bundleheight >= 0) ? (bp->ramchain.bundleheight + bundlesize) : -1; + printf("autocreateA: bundleheight.%d\n",bundleheight); + bundlei = -2; + *bpp = bp = iguana_bundlecreate(coin,&bundlei,bundleheight,block->hash2); + *bpp = 0; + *bundleip = -2; + return(block); + } + else if ( bundlei < coin->chain->bundlesize-1 ) + { + bundlei++; + if ( bp->n <= bundlei ) + bp->n = bundlei+1; + iguana_hash2set(coin,"add",bp,bundlei,block->hash2); + //printf("found prev.%s -> bundlei.%d\n",bits256_str(str,block->prev_block),bundlei); + } + } + } + else + { + // printf("found bp.%p bundlei.%d\n",bp,bundlei); + } + } + else if ( bundlei < -1 ) + { + bp = iguana_bundlefind(coin,bpp,&bundlei,block->hash2); + printf("c bundlefind.(%s) -> bundlei.%d\n",bits256_str(str,block->hash2),bundlei); + } else printf("last case bundleip %d\n",bundlei); + *bpp = bp; + *bundleip = bundlei; + //printf("bundlei.%d for %s\n",bundlei,bits256_str(str,block->hash2)); + if ( memcmp(bp->hashes[bundlei].bytes,block->hash2.bytes,sizeof(bits256)) != 0 ) + printf("honk? find error %s\n",bits256_str(str,bp->hashes[bundlei])), getchar(); + if ( bp == 0 || bundlei < -1 ) + { + printf("%s null bp? %p or illegal bundlei.%d block.%p\n",bits256_str(str,block->hash2),bp,bundlei,block); + return(block); + } + if ( (setval= iguana_bundlehash2add(coin,&checkblock,bp,bundlei,block->hash2)) == 0 && checkblock == block ) + { + if ( bp->blocks[bundlei] != 0 ) + { + if ( bp->blocks[bundlei] != block ) + printf("blockadd: error blocks[%d] %p %d != %d %p\n",bundlei,bp->blocks[bundlei],bp->blocks[bundlei]->height,block->height,block); + } else bp->blocks[bundlei] = block; + //iguana_bundlehash2add(coin,0,bp,bundlei-1,block->prev_block); + //printf("setval.%d bp.%p bundlei.%d\n",setval,bp,bundlei); + } + else if ( setval > 0 ) + { + if ( bundlei == bundlesize ) + { + bundleheight = (bp->ramchain.bundleheight >= 0) ? (bp->ramchain.bundleheight + bundlesize) : -1; + printf("autocreate: bundleheight.%d\n",bundleheight); + iguana_bundlecreate(coin,&checki,bundleheight,block->hash2); + } + printf("setval.%d bundlei.%d\n",setval,bundlei); + } else printf("blockadd: error.%d adding hash2, checkblock.%p vs %p\n",setval,checkblock,block); + //printf("bundleblockadd.[%d] of %d <- %s setval.%d %p\n",bundlei,bp->n,bits256_str(str,block->hash2),setval,block); + } else printf("bundleblockadd: block.%p error\n",block); + return(block); + } + + struct iguana_block *iguana_bundleblockadd(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,struct iguana_block *origblock) + { + struct iguana_block *block,*retblock; int32_t i,oldhwm; struct iguana_bundle *bp; + bits256 *hash2p,hash2; char str[65]; struct iguana_bloominds bit; + oldhwm = coin->blocks.hwmchain.height; + *bpp = 0, *bundleip = -2; + if ( (retblock= iguana_blockadd(coin,bpp,bundleip,origblock)) != 0 ) + { + block = retblock; + //iguana_chainextend(coin,block); + if ( block->height >= 0 && (hash2p= iguana_blockhashptr(coin,coin->blocks.hashblocks)) != 0 ) + *hash2p = block->hash2; + if ( oldhwm != coin->blocks.hwmchain.height ) + { + if ( oldhwm < coin->blocks.hashblocks ) + coin->blocks.hashblocks = oldhwm; + while ( coin->blocks.hashblocks < coin->blocks.hwmchain.height && (hash2p= iguana_blockhashptr(coin,coin->blocks.hashblocks)) != 0 ) + { + hash2 = *hash2p; + if ( bits256_nonz(hash2) > 0 && (block= iguana_blockfind(coin,hash2)) != 0 ) + { + if ( hash2p == 0 ) + { + printf("iguana_bundleblockadd B cant find coin->blocks.hashblocks %d\n",coin->blocks.hashblocks); + break; + } + *hash2p = hash2; + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + if ( coin->blocks.hashblocks >= bp->ramchain.bundleheight && coin->blocks.hashblocks < bp->ramchain.bundleheight+bp->n ) + { + bit = iguana_calcbloom(block->hash2); + if ( iguana_bloomfind(coin,&bp->bloom,0,bit) < 0 ) + iguana_bloomset(coin,&bp->bloom,0,bit); + break; + } + } + } + //printf("ht.%d %s %p\n",block->height,bits256_str(str,hash2),hash2p); + bp = 0; + *bundleip = -2; + iguana_blockadd(coin,&bp,bundleip,block); + bp = 0; + *bundleip = -2; + if ( iguana_bundlefind(coin,&bp,bundleip,block->hash2) == 0 ) + { + printf("iguana_bundleblockadd A cant find just added.%s bundlei.%d\n",bits256_str(str,hash2),*bundleip); + bp = 0; + *bundleip = -2; + iguana_bundlefind(coin,&bp,bundleip,block->hash2); + break; + } + coin->blocks.hashblocks++; + block = 0; + } + else + { + //printf("break loop block.%p %s coin->blocks.hashblocks %d vs %d\n",block,bits256_str(str,hash2),coin->blocks.hashblocks,coin->blocks.hwmheight); + break; + } + } + } + } else printf("iguana_bundleblockadd returns null\n"); + return(retblock); + }*/ + int64_t iguana_ramchain_compact(struct iguana_ramchain *ramchain,int32_t numpkinds,int32_t numexternaltxids) + { + int32_t i,diff; int64_t offset; bits256 *src,*dest,tmp; + diff = (ramchain->data->numpkinds - numpkinds); + src = (bits256 *)((long)ramchain->mem->ptr + (long)ramchain->data->Xoffset); + offset = ramchain->data->Poffset + (sizeof(struct iguana_pkhash) * numpkinds); + ramchain->data->Xoffset = offset + ((sizeof(struct iguana_account)+sizeof(struct iguana_pkextra)) * numpkinds); + if ( numpkinds < ramchain->data->numpkinds ) + { + ramchain->data->numpkinds = numpkinds; + dest = (bits256 *)((long)ramchain->mem->ptr + (long)offset); + for (i=0; idata->numexternaltxids = numexternaltxids; + ramchain->mem->used = offset; + return(offset); + } + + long iguana_blockramchainPT(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,int32_t txn_count,uint8_t *data,int32_t recvlen) + { + RAMCHAIN_PTRS; struct iguana_ramchain *ramchain = &addr->ramchain; + struct iguana_msgtx *tx; int32_t i,j,err,bundlei = -2; struct iguana_bundle *bp = 0; + if ( iguana_bundlefind(coin,&bp,&bundlei,origtxdata->block.hash2) == 0 ) + return(-1); + SETBIT(bp->recv,bundlei); + bp->fpos[bundlei] = -1; + bp->recvlens[bundlei] = recvlen; + if ( iguana_ramchain_init(ramchain,&addr->TXDATA,&addr->HASHMEM,0,txn_count,origtxdata->numunspents,origtxdata->numspends,0,0) == 0 ) + return(-1); + iguana_ramchain_link(ramchain,origtxdata->block.hash2,origtxdata->block.hash2,bp->hdrsi,bp->bundleheight+bundlei,1); + _iguana_ramchain_setptrs(ramchain,&T,&U,&U2,&S,&P,&P2,&A,&X); + if ( T == 0 || U == 0 || S == 0 || P == 0 || X == 0 ) + { + printf("fatal error getting txdataptrs\n"); + return(-1); + } + for (i=0; itxid,tx->tx_out,tx->tx_in); + for (j=0; jtx_out; j++) + iguana_ramchain_addunspent(ramchain,T,U,U2,S,P,P2,A,X,tx->vouts[j].value,tx->vouts[j].pk_script,tx->vouts[j].pk_scriptlen); + for (j=0; jtx_in; j++) + iguana_ramchain_addspend(ramchain,T,U,U2,S,P,P2,A,X,tx->vins[j].prev_hash,tx->vins[j].prev_vout,tx->vins[j].script,tx->vins[j].scriptlen,tx->vins[j].sequence); + } + ramchain->data->numpkinds = ramchain->pkind; + ramchain->data->numexternaltxids = ramchain->externalind; + ramchain->data->allocsize = iguana_ramchain_size(ramchain); + if ( (err= iguana_ramchainverify(ramchain)) == 0 ) + { + if ( (bp->fpos[bundlei]= iguana_ramchain_save(ramchain,addr->ipbits,bp->hashes[0],bundlei)) >= 0 ) + bp->ipbits[bundlei] = addr->ipbits; + } else printf("ramchain verification error.%d hdrsi.%d bundlei.%d\n",err,bp->hdrsi,bundlei); + iguana_ramchain_free(ramchain); + return(bp->fpos[bundlei]); + //iguana_hashfree(addr->txids,0); + //iguana_hashfree(addr->pkhashes,0); + + /*txidind = unspentind = spendind = pkind = 0; + for (i=numvouts=numpkinds=0; itxid = tx->txid, t->txidind = txidind, t->firstvout = unspentind, t->numvouts = tx->tx_out; + iguana_hashsetPT(ramchain,hashmem,'T',t->txid.bytes,txidind); + for (j=0; jtx_out; j++,numvouts++,unspentind++) + { + u = &U[unspentind]; + script = tx->vouts[j].pk_script, scriptlen = tx->vouts[j].pk_scriptlen; + iguana_calcrmd160(coin,rmd160,script,scriptlen,tx->txid); + //char str[65]; init_hexbytes_noT(str,rmd160,20), printf("pkhashes.%p %s %s new pkind.%d pkoffset.%d %d\n",addr->pkhashes,addr->ipaddr,str,numpkinds,txdata->pkoffset,(int32_t)((long)&P[numpkinds] - (long)txdata)); + if ( (ptr= iguana_hashfind(ramchain,'P',rmd160)) == 0 ) + { + memcpy(P[numpkinds].rmd160,rmd160,sizeof(rmd160)); + if ( (ptr= iguana_hashsetPT(ramchain,hashmem,'P',&P[numpkinds],numpkinds)) == 0 ) + printf("fatal error adding pkhash\n"), getchar(); + //printf("added ptr.%p\n",ptr); + numpkinds++; + } //else printf("found %p[%d] for (%s)\n",ptr,ptr->hh.itemind,str); + u->value = tx->vouts[j].value, u->txidind = txidind; + u->pkind = ptr->hh.itemind; + P[u->pkind].firstunspentind = unspentind; + // prevunspentind requires having accts, so that waits for third pass + } + } + //printf("reallocP.%p -> ",P); + if ( (txdata->numpkinds= numpkinds) > 0 ) + P = iguana_memalloc(txmem,sizeof(*P) * numpkinds,0); + //printf("%p\n",P); + externalT = iguana_memalloc(txmem,0,1); + txidind = 0; + for (i=numvins=numexternal=0; ifirstvin = spendind; + for (j=0; jtx_in; j++) + { + script = tx->vins[j].script, scriptlen = tx->vins[j].scriptlen; + s = &S[spendind]; + if ( (sequence= tx->vins[j].sequence) != (uint32_t)-1 ) + s->diffsequence = 1; + s->vout = tx->vins[j].prev_vout; + if ( s->vout != 0xffff ) + { + if ( (ptr= iguana_hashfind(ramchain,'T',tx->vins[j].prev_hash.bytes)) != 0 ) + { + if ( (s->spendtxidind= ptr->hh.itemind) >= txdata->numtxids ) + { + s->external = 1; + s->spendtxidind -= txdata->numtxids; + } + } + else + { + s->external = 1; + externalT[numexternal] = tx->vins[j].prev_hash; + iguana_hashsetPT(ramchain,hashmem,'T',externalT[numexternal].bytes,txdata->numtxids + numexternal); + s->spendtxidind = numexternal++; + } + spendind++; + numvins++; + //printf("spendind.%d\n",spendind); + } //else printf("vout.%x\n",s->vout); + // prevspendind requires having accts, so that waits for third pass + } + t->numvins = numvins; + } + if ( (txdata->numexternaltxids= numexternal) > 0 ) + externalT = iguana_memalloc(txmem,sizeof(*externalT) * numexternal,0); + txdata->datalen = (int32_t)txmem->used; + txdata->numspends = numvins; + txdata->numpkinds = numpkinds; + txdata->numtxids = txn_count; + //char str[65],buf[9999]; + //for (j=buf[0]=0; j %d\n",buf,bundlei,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,recvlen,txdata->datalen); + if ( numvouts != txdata->numunspents || i != txdata->numtxids ) + { + printf("counts mismatch: numvins %d != %d txdata->numvins || numvouts %d != %d txdata->numvouts || i %d != %d txdata->numtxids\n",numvins,txdata->numspends,numvouts,txdata->numunspents,i,txdata->numtxids); + getchar(); + exit(-1); + } + else + { + static int32_t maxrecvlen,maxdatalen,maxhashmem; static double recvsum,datasum; + recvsum += recvlen, datasum += txdata->datalen; + if ( recvlen > maxrecvlen ) + printf("[%.3f] %.0f/%.0f maxrecvlen %d -> %d\n",recvsum/datasum,recvsum,datasum,maxrecvlen,recvlen), maxrecvlen = recvlen; + if ( txdata->datalen > maxdatalen ) + printf("[%.3f] %.0f/%.0f maxdatalen %d -> %d\n",recvsum/datasum,recvsum,datasum,maxdatalen,txdata->datalen), maxdatalen = txdata->datalen; + if ( hashmem != 0 && hashmem->used > maxhashmem ) + printf("[%.3f] %.0f/%.0f maxhashmem %d -> %ld\n",recvsum/datasum,recvsum,datasum,maxhashmem,hashmem->used), maxhashmem = (int32_t)hashmem->used; + if ( (rand() % 10000) == 0 ) + printf("[%.3f] %.0f/%.0f recvlen vs datalen\n",recvsum/datasum,recvsum,datasum); + if ( origtxdata != 0 ) + { + origtxdata->numspends = txdata->numspends; + origtxdata->numpkinds = txdata->numpkinds; + origtxdata->numexternaltxids = txdata->numexternaltxids; + } + } + if ( iguana_peertxsave(coin,&hdrsi,&bundlei,fname,addr,txdata) == txdata ) + { + #ifdef __APPLE__ + int32_t checki; struct iguana_txblock *checktx; struct iguana_ramchain R,*ptr = &R; + if ( 1 && (checktx= iguana_peertxdata(coin,&checki,fname,txmem,addr->ipbits,txdata->block.hash2)) != 0 && checki == bundlei ) + { + if ( iguana_ramchainset(coin,ptr,checktx) == ptr ) + { + char str[65]; int32_t j,err; + ptr->txids = ramchain->txids; + ptr->pkhashes = ramchain->pkhashes; + if ( (err= iguana_ramchainverifyPT(coin,ptr)) != 0 ) + { + for (j=0; jnumpkinds; j++) + init_hexbytes_noT(str,ptr->P[j].rmd160,20), printf("[%d %s] ",j,str); + printf("check err.%d ramchain.%s bundlei.%d T.%d U.%d S.%d P.%d\n",err,bits256_str(str,ptr->hash2),bundlei,ptr->numtxids,ptr->numunspents,ptr->numspends,ptr->numpkinds); + } + } + } + #endif + }*/ + //printf("free addrtables %p %p\n",addr->txids,addr->pkhashes); + // printf("numpkinds.%d numspends.%d\n",txdata->numpkinds,txdata->numspends); + } + + + /*void iguana_flushQ(struct iguana_info *coin,struct iguana_peer *addr) + { + struct iguana_helper *ptr; + if ( time(NULL) > addr->lastflush+3 ) + { + ptr = mycalloc('i',1,sizeof(*ptr)); + ptr->allocsize = sizeof(*ptr); + ptr->coin = coin; + ptr->addr = addr; + ptr->type = 'F'; + //printf("FLUSH.%s %u lag.%d\n",addr->ipaddr,addr->lastflush,(int32_t)(time(NULL)-addr->lastflush)); + addr->lastflush = (uint32_t)time(NULL); + queue_enqueue("helperQ",&helperQ,&ptr->DL,0); + } + }*/ + + struct iguana_txblock *iguana_peertxsave(struct iguana_info *coin,int32_t *hdrsip,int32_t *bundleip,char *fname,struct iguana_peer *addr,struct iguana_txblock *txdata) + { + int32_t fpos,bundlei,i,z; FILE *fp; + fpos = 0; + *bundleip = bundlei = iguana_peerfname(coin,hdrsip,fname,addr->ipbits,txdata->block.hash2); + if ( bundlei < 0 || bundlei >= coin->chain->bundlesize ) + { + printf(" wont save.(%s) bundlei.%d\n",fname,bundlei); + return(0); + } + txdata->block.hdrsi = *hdrsip; + txdata->block.bundlei = bundlei; + if ( (fp= fopen(fname,"rb+")) == 0 ) + { + if ( (fp= fopen(fname,"wb")) != 0 ) + { + z = -1; + coin->peers.numfiles++; + for (i=0; ichain->bundlesize; i++) + fwrite(&z,1,sizeof(z),fp); + fclose(fp); + fp = fopen(fname,"rb+"); + } + } + if ( fp != 0 ) + { + fseek(fp,0,SEEK_END); + fpos = (int32_t)ftell(fp); + //printf("%s fpos.%d: bundlei.%d datalen.%d\n",fname,fpos,bundlei,txdata->datalen); + fwrite(&bundlei,1,sizeof(bundlei),fp); + fwrite(&txdata->block.hash2,1,sizeof(txdata->block.hash2),fp); + fwrite(&txdata->datalen,1,sizeof(txdata->datalen),fp); + fwrite(txdata,1,txdata->datalen,fp); + fseek(fp,bundlei * sizeof(bundlei),SEEK_SET); + //printf("bundlei[%d] <- fpos.%d\n",bundlei,fpos); + fwrite(&fpos,1,sizeof(fpos),fp); + fclose(fp); + //for (i=0; inumpkinds; i++) + // printf("%016lx ",*(long *)((struct iguana_pkhash *)((long)txdata + txdata->pkoffset))[i].rmd160); + //printf("create.(%s) %d ",fname,bundlei,coin->peers.numfiles); + //printf("bundlei.%d datalen.%d T.%d U.%d S.%d P.%d X.%d\n",bundlei,txdata->datalen,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,txdata->numexternaltxids); + return(txdata); + } + return(0); + } + + struct iguana_txblock *iguana_peertxdata(struct iguana_info *coin,int32_t *bundleip,char *fname,struct iguana_memspace *mem,uint32_t ipbits,bits256 hash2) + { + int32_t bundlei,datalen,checki,hdrsi,fpos; char str[65],str2[65]; FILE *fp; + bits256 checkhash2; struct iguana_txblock *txdata = 0; + if ( (bundlei= iguana_peerfname(coin,&hdrsi,fname,ipbits,hash2)) >= 0 ) + { + if ( (fp= fopen(fname,"rb")) != 0 ) + { + fseek(fp,bundlei * sizeof(bundlei),SEEK_SET); + fread(&fpos,1,sizeof(fpos),fp); + fseek(fp,fpos,SEEK_SET); + fread(&checki,1,sizeof(checki),fp); + if ( ftell(fp)-sizeof(checki) == fpos && bundlei == checki ) + { + fread(&checkhash2,1,sizeof(checkhash2),fp); + if ( memcmp(hash2.bytes,checkhash2.bytes,sizeof(hash2)) == 0 ) + { + fread(&datalen,1,sizeof(datalen),fp); + if ( datalen < (mem->totalsize - mem->used - 4) ) + { + if ( (txdata= iguana_memalloc(mem,datalen,0)) != 0 ) + { + fread(txdata,1,datalen,fp); + if ( txdata->datalen != datalen || txdata->block.bundlei != bundlei ) + { + printf("%s peertxdata txdata->datalen.%d != %d bundlei.%d vs %d\n",bits256_str(str,txdata->block.hash2),txdata->datalen,datalen,txdata->block.bundlei,bundlei); + getchar(); + txdata = 0; + iguana_memreset(mem); + } //else printf("SUCCESS txdata.%s bundlei.%d fpos.%d T.%d U.%d S.%d P.%d\n",bits256_str(str,txdata->block.hash2),bundlei,fpos,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds); + } else printf("peertxdata error allocating txdata\n"); + } else printf("mismatch peertxdata datalen %d vs %ld totalsize %ld\n",datalen,mem->totalsize - mem->used - 4,(long)mem->totalsize); + } else printf("peertxdata hash mismatch %s != %s\n",bits256_str(str,hash2),bits256_str(str2,checkhash2)); + } else printf("peertxdata bundlei.%d != checki.%d, fpos.%d ftell.%ld\n",bundlei,checki,fpos,ftell(fp)); + fclose(fp); + } else printf("cant find file.(%s)\n",fname); + } //else printf("bundlei.%d\n",bundlei); + *bundleip = bundlei; + return(txdata); + } + + /*if ( (n= ramchain->data->numtxids) > 0 ) + { + for (ramchain->txidind=ramchain->data->firsti; ramchain->txidindtxidind++) + { + tx = &T[ramchain->txidind]; + //printf("tx.%p (%d) %d txidind.%d\n",tx,(int32_t)((long)tx - (long)ramchain->mem->ptr),(int32_t)ramchain->mem->totalsize,ramchain->txidind); + iguana_ramchain_addtxid(coin,RAMCHAIN_ARG,tx->txid,tx->numvouts,tx->numvins); + //if ( (ptr= iguana_hashsetPT(ramchain,'T',&tx->txid.bytes,ramchain->txidind)) != 0 ) + { + for (j=0; jnumvouts; j++) + { + iguana_ramchain_addunspent(coin,RAMCHAIN_ARG,U[ramchain->unspentind].value,P[U[ramchain->unspentind].pkind].rmd160,-20,tx->txid,j); + } + } + ramchain->spendind += tx->numvins; + } + ramchain->externalind = ramchain->data->numexternaltxids; + } + if ( (err= iguana_ramchainverify(coin,ramchain)) != 0 ) + { + printf("iguana_ramchain_map.(%s) err.%d verifying ramchain\n",fname,err); + iguana_ramchain_free(ramchain,hashmem == 0); + munmap(ptr,filesize); + }*/ + //printf("mapped ramchain verified\n"); + +#define iguana_hashfind(hashtable,key,keylen) iguana_hashsetHT(hashtable,0,key,keylen,-1) + + struct iguana_kvitem *iguana_hashsetHT(struct iguana_kvitem *hashtable,struct iguana_memspace *mem,void *key,int32_t keylen,int32_t itemind) + { + struct iguana_kvitem *ptr = 0; int32_t allocsize; + HASH_FIND(hh,hashtable,key,keylen,ptr); + if ( ptr == 0 && itemind >= 0 ) + { + allocsize = (int32_t)(sizeof(*ptr)); + if ( mem != 0 ) + ptr = iguana_memalloc(mem,allocsize,1); + else ptr = mycalloc('t',1,allocsize); + if ( ptr == 0 ) + printf("fatal alloc error in hashset\n"), exit(-1); + //printf("ptr.%p allocsize.%d key.%p keylen.%d itemind.%d\n",ptr,allocsize,key,keylen,itemind); + ptr->hh.itemind = itemind; + HASH_ADD_KEYPTR(hh,hashtable,key,keylen,ptr); + } + if ( ptr != 0 ) + { + struct iguana_kvitem *tmp; + HASH_FIND(hh,hashtable,key,keylen,tmp); + char str[65]; + init_hexbytes_noT(str,key,keylen); + if ( tmp != ptr ) + printf("%s itemind.%d search error %p != %p\n",str,itemind,ptr,tmp); + // else printf("added.(%s) height.%d %p\n",str,itemind,ptr); + } + return(ptr); + } + + int32_t iguana_parseblock(struct iguana_info *coin,struct iguana_block *block,struct iguana_msgtx *tx,int32_t numtx) + { +#ifdef oldway + int32_t txind,pkind,i; uint16_t numvouts,numvins; + pkind = block->L.numpkinds = coin->latest.dep.numpkinds; + block->L.supply = coin->latest.dep.supply; + if ( block->L.numtxids != coin->latest.dep.numtxids || block->L.numunspents != coin->latest.dep.numunspents || block->L.numspends != coin->latest.dep.numspends || block->L.numpkinds != coin->latest.dep.numpkinds ) + { + printf("Block.(h%d t%d u%d s%d p%d) vs coin.(h%d t%d u%d s%d p%d)\n",block->height,block->L.numtxids,block->L.numunspents,block->L.numspends,block->L.numpkinds,coin->blocks.parsedblocks,coin->latest.dep.numtxids,coin->latest.dep.numunspents,coin->latest.dep.numspends,coin->latest.dep.numpkinds); + block->L.numtxids = coin->latest.dep.numtxids; + block->L.numunspents = coin->latest.dep.numunspents; + block->L.numspends = coin->latest.dep.numspends; + block->L.numpkinds = coin->latest.dep.numpkinds; + iguana_kvwrite(coin,coin->blocks.db,0,block,(uint32_t *)&block->height); + //getchar(); + } + vcalc_sha256(0,coin->latest.ledgerhash.bytes,coin->latest.lhashes[0].bytes,sizeof(coin->latest.lhashes)); + coin->LEDGER.snapshot.dep = block->L; + memcpy(&coin->LEDGER.snapshot.ledgerhash,&coin->latest.ledgerhash,sizeof(coin->latest.ledgerhash)); + memcpy(coin->LEDGER.snapshot.lhashes,coin->latest.lhashes,sizeof(coin->latest.lhashes)); + memcpy(coin->LEDGER.snapshot.states,coin->latest.states,sizeof(coin->latest.states)); + //printf("%08x Block.(h%d t%d u%d s%d p%d) vs (h%d t%d u%d s%d p%d)\n",(uint32_t)coin->latest.ledgerhash.txid,block->height,block->L.numtxids,block->L.numunspents,block->L.numspends,block->L.numpkinds,coin->blocks.parsedblocks,coin->latest.dep.numtxids,coin->latest.dep.numunspents,coin->latest.dep.numspends,coin->latest.dep.numpkinds); + if ( (coin->blocks.parsedblocks % 1000) == 0 ) + { + for (i=0; iLEDGER.snapshot.lhashes[i].txid); + char str[65]; + bits256_str(str,coin->LEDGER.snapshot.ledgerhash); + printf("-> pre parse %s ledgerhashes.%d\n",str,coin->blocks.parsedblocks); + } + coin->LEDGER.snapshot.blockhash = block->hash2; + coin->LEDGER.snapshot.merkle_root = block->merkle_root; + coin->LEDGER.snapshot.timestamp = block->timestamp; + coin->LEDGER.snapshot.credits = coin->latest.credits; + coin->LEDGER.snapshot.debits = coin->latest.debits; + coin->LEDGER.snapshot.height = block->height; + //if ( coin->blocks.parsedblocks > 0 && (coin->blocks.parsedblocks % coin->chain->bundlesize) == 0 ) + // coin->R.bundles[coin->blocks.parsedblocks / coin->chain->bundlesize].presnapshot = coin->LEDGER.snapshot; + for (txind=block->numvouts=block->numvins=0; txindtxn_count; txind++) + { + //printf("block.%d txind.%d numvouts.%d numvins.%d block->(%d %d) U%d coin.%d\n",block->height,txind,numvouts,numvins,block->numvouts,block->numvins,block->L.numunspents,coin->latest.dep.numunspents); + //fprintf(stderr,"t"); + if ( ramchain_parsetx(coin,&coin->mining,&coin->totalfees,&numvouts,&numvins,block->height,txind,&tx[txind],block->L.numtxids+txind,block->L.numunspents + block->numvouts,block->L.numspends + block->numvins) < 0 ) + return(-1); + block->numvouts += numvouts; + block->numvins += numvins; + //printf("block.%d txind.%d numvouts.%d numvins.%d block->(%d %d) 1st.(%d %d)\n",block->height,txind,numvouts,numvins,block->numvouts,block->numvins,block->L.numunspents,block->L.numspends); + } + //printf(" Block.(h%d t%d u%d s%d p%d) vs coin.(h%d t%d u%d s%d p%d)\n",block->height,block->L.numtxids,block->L.numunspents,block->L.numspends,block->L.numpkinds,coin->blocks.parsedblocks,coin->latest.dep.numtxids,coin->latest.dep.numunspents,coin->latest.dep.numspends,coin->latest.dep.numpkinds); + if ( coin->latest.dep.supply != (coin->latest.credits - coin->latest.debits) ) + { + printf("height.%d supply %.8f != %.8f (%.8f - %.8f)\n",block->height,dstr(coin->latest.dep.supply),dstr(coin->latest.credits)-dstr(coin->latest.debits),dstr(coin->latest.credits),dstr(coin->latest.debits)); + getchar(); + } +#ifdef IGUANA_VERIFYFLAG + while ( pkind < coin->latest.dep.numpkinds ) + { + int64_t err; + if ( (err= iguana_verifyaccount(coin,&coin->accounts[pkind],pkind)) < 0 ) + printf("pkind.%d err.%lld %.8f last.(U%d S%d)\n",pkind,(long long)err,dstr(coin->accounts[pkind].balance),coin->accounts[pkind].lastunspentind,coin->accounts[pkind].lastspendind), getchar(); + pkind++; + } +#endif + coin->parsetime = (uint32_t)time(NULL); + coin->parsemillis = milliseconds(); + iguana_kvwrite(coin,coin->blocks.db,0,block,(uint32_t *)&block->height); + if ( (coin->blocks.parsedblocks > coin->longestchain-100000 && (coin->blocks.parsedblocks % 100) == 0) || (coin->blocks.parsedblocks > coin->longestchain-1000 && (coin->blocks.parsedblocks % 10) == 0) || coin->blocks.parsedblocks > coin->longestchain-100 || (coin->blocks.parsedblocks % 100) == 0 ) + { + printf("PARSED.%d T.%d U.%d+%d S.%d+%d P.%d hwm.%d longest.%d | %.8f - %.8f %.8f [%.8f] M %.8f F %.8f | %.02f minutes %.2f%% %.2f%% %.2f%% avail\n",coin->blocks.parsedblocks,coin->latest.dep.numtxids,block->L.numunspents,block->numvouts,block->L.numspends,block->numvins,block->L.numpkinds,coin->blocks.hwmheight,coin->longestchain,dstr(coin->latest.credits),dstr(coin->latest.debits),dstr(coin->latest.credits)-dstr(coin->latest.debits),(dstr(coin->latest.credits)-dstr(coin->latest.debits))/coin->blocks.parsedblocks,dstr(coin->mining),dstr(coin->totalfees),((double)time(NULL)-coin->starttime)/60.,(double)iguana_avail(coin,coin->blocks.parsedblocks+1,1000)/10.,(double)iguana_avail(coin,coin->blocks.parsedblocks+1,25000)/250.,100.*(double)iguana_avail(coin,coin->blocks.parsedblocks+1,coin->longestchain-coin->blocks.parsedblocks-1)/(coin->longestchain-coin->blocks.parsedblocks)); + myallocated(0,0); + } + if ( 0 && coin->loadedLEDGER.snapshot.height == coin->blocks.parsedblocks ) + { + memcpy(&coin->latest.ledgerhash,&coin->loadedLEDGER.snapshot.ledgerhash,sizeof(coin->loadedLEDGER.snapshot.ledgerhash)); + memcpy(coin->latest.lhashes,coin->loadedLEDGER.snapshot.lhashes,sizeof(coin->loadedLEDGER.snapshot.lhashes)); + printf("restore lhashes, special alignement case\n"); + } //else printf("loaded.%d vs parsed.%d\n",coin->loadedLEDGER.snapshot.height,coin->blocks.parsedblocks); + coin->blocks.parsedblocks++; +#endif + return(0); + } + + int32_t iguana_updateramchain(struct iguana_info *coin) + { + return(0); + } + + int32_t iguana_hashfree(struct iguana_kvitem *hashtable,int32_t delitem) + { + struct iguana_kvitem *item,*tmp; int32_t n = 0; + if ( hashtable != 0 ) + { + HASH_ITER(hh,hashtable,item,tmp) + { + //printf("hashdelete.%p allocsize.%d itemind.%d delitem.%d\n",item,item->allocsize,item->hh.itemind,delitem); + if ( delitem != 0 ) + { + HASH_DEL(hashtable,item); + //if ( delitem > 1 ) + // myfree(item,item->allocsize); + } + n++; + } + } + return(n); + } + + struct iguana_txblock *iguana_ramchainptrs(struct iguana_txid **Tptrp,struct iguana_unspent20 **Uptrp,struct iguana_spend256 **Sptrp,struct iguana_pkhash **Pptrp,bits256 **externalTptrp,struct iguana_memspace *mem,struct iguana_txblock *origtxdata) + { + char str[65]; struct iguana_txblock *txdata; int32_t allocsize,extralen,rwflag = (origtxdata != 0); + iguana_memreset(mem); + allocsize = (int32_t)(sizeof(*txdata) - sizeof(txdata->space) + ((origtxdata != 0) ? origtxdata->extralen : 0)); + mem->alignflag = sizeof(uint32_t); + if ( (txdata= iguana_memalloc(mem,allocsize,0)) == 0 ) + return(0); + //printf("ptr.%p alloctxdata.%p T.%d U.%d S.%d P.%d\n",mem->ptr,txdata,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds); + extralen = (origtxdata != 0) ? origtxdata->extralen : txdata->extralen; + if ( origtxdata != 0 ) + { + //printf("copy %d bytes from %p to %p extralen.%d size.%ld T.%d U.%d S.%d P.%d \n",allocsize,origtxdata,txdata,extralen,sizeof(*txdata),txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds); + memcpy(txdata,origtxdata,allocsize); + } else iguana_memalloc(mem,txdata->extralen,0); + *Tptrp = iguana_memalloc(mem,sizeof(**Tptrp) * txdata->numtxids,rwflag); + *Uptrp = iguana_memalloc(mem,sizeof(**Uptrp) * txdata->numunspents,rwflag); + *Sptrp = iguana_memalloc(mem,sizeof(**Sptrp) * txdata->numspends,rwflag); + //printf("rwflag.%d ptr.%p alloctxdata.%p T.%d U.%d S.%d P.%d pkoffset.%ld X.%d\n",rwflag,mem->ptr,txdata,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,mem->used,txdata->numexternaltxids); + if ( externalTptrp != 0 ) + { + if ( txdata->pkoffset < (int32_t)mem->used ) + printf("allocsize.%d size.%ld %p %s (T.%d U.%d S.%d P.%d X.%d) iguana_ramchainptrs pkoffset.%d != %ld numspends.%d\n",allocsize,sizeof(*txdata),txdata,bits256_str(str,txdata->block.hash2),txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,txdata->numexternaltxids,txdata->pkoffset,mem->used,txdata->numspends), getchar(); + mem->used = txdata->pkoffset; + *Pptrp = iguana_memalloc(mem,sizeof(**Pptrp) * txdata->numpkinds,rwflag); + *externalTptrp = iguana_memalloc(mem,txdata->numexternaltxids * sizeof(**externalTptrp),rwflag); + } + else + { + txdata->pkoffset = (int32_t)mem->used; + // printf("set pkoffset.%d\n",txdata->pkoffset); + *Pptrp = iguana_memalloc(mem,0,rwflag); + } + if ( 0 && rwflag == 0 ) + printf("datalen.%d rwflag.%d origtxdat.%p allocsize.%d extralen.%d T.%d U.%d S.%d P.%d X.%p[%d]\n",(int32_t)mem->totalsize,rwflag,origtxdata,allocsize,extralen,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,externalTptrp!=0?*externalTptrp:0,txdata->numexternaltxids); + return(txdata); + } + + int32_t iguana_ramchainsave(struct iguana_info *coin,struct iguana_ramchain *ramchain) + { + FILE *fp; char fname[1024],str[65]; + sprintf(fname,"DB/%s/%s.%d",coin->symbol,bits256_str(str,ramchain->H.data->firsthash2),ramchain->H.hdrsi); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + fwrite(ramchain,1,ramchain->H.data->allocsize,fp); + fclose(fp); + } + printf("ramchainsave.%s %d[%d] %s\n",coin->symbol,ramchain->H.hdrsi,ramchain->numblocks,mbstr(str,ramchain->H.data->allocsize)); + return(0); + } + + int32_t iguana_ramchainfree(struct iguana_info *coin,struct iguana_memspace *mem,struct iguana_ramchain *ramchain) + { + if ( ramchain->txids != 0 ) + iguana_hashfree(ramchain->txids,1); + if ( ramchain->pkhashes != 0 ) + iguana_hashfree(ramchain->pkhashes,1); + iguana_mempurge(mem); + return(0); + } + + /*struct iguana_ramchain *iguana_ramchainset(struct iguana_info *coin,struct iguana_ramchain *ramchain,struct iguana_txblock *txdata) + { + struct iguana_memspace txmem; + memset(&txmem,0,sizeof(txmem)); + iguana_meminit(&txmem,"bramchain",txdata,txdata->datalen,0); + //printf("ramchainset <- txdata.%p memptr.%p T.%d U.%d S.%d P.%d X.%d\n",txdata,txmem.ptr,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,txdata->numexternaltxids); + if ( iguana_ramchainptrs(&ramchain->T,&ramchain->U,&ramchain->S,&ramchain->P,&ramchain->externalT,&txmem,0) != txdata || ramchain->T == 0 || ramchain->U == 0 || ramchain->S == 0 || ramchain->P == 0 ) + { + printf("iguana_ramchainset: cant set pointers txdata.%p\n",txdata); + return(0); + } + //int32_t i; + // for (i=0; i<344; i++) + // printf("%02x ",((uint8_t *)txdata)[i]); + //for (i=-1; i<2; i++) + // printf("%016lx ",*(long *)((struct iguana_pkhash *)((long)txdata + txdata->pkoffset))[i].rmd160); + //printf("datalen.%d T.%d U.%d S.%d P.%d X.%d | %d vs %d ramchain.%p txdata.%p\n",txdata->datalen,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds,txdata->numexternaltxids,txdata->pkoffset,(int32_t)((long)ramchain->P - (long)txdata),ramchain,txdata); + ramchain->numtxids = txdata->numtxids; + ramchain->numunspents = txdata->numunspents; + ramchain->numspends = txdata->numspends; + ramchain->numpkinds = txdata->numpkinds; + ramchain->numexternaltxids = txdata->numexternaltxids; + //printf("ramchain T.%d U.%d S.%d P.%d X.%d %p\n",ramchain->numtxids,ramchain->numunspents,ramchain->numspends,ramchain->numpkinds,ramchain->numexternaltxids,ramchain->externalT); + if ( ramchain->numexternaltxids != 0 && ramchain->externalT == 0 ) + getchar(); + ramchain->prevhash2 = txdata->block.prev_block; + ramchain->hash2 = txdata->block.hash2; + return(ramchain); + } + + int32_t iguana_ramchaintxid(struct iguana_info *coin,bits256 *txidp,struct iguana_ramchain *ramchain,struct iguana_spend *s) + { + memset(txidp,0,sizeof(*txidp)); + //printf("s.%p ramchaintxid vout.%x spendtxidind.%d numexternals.%d isext.%d numspendinds.%d\n",s,s->vout,s->spendtxidind,ramchain->numexternaltxids,s->external,ramchain->numspends); + if ( s->vout == 0xffff ) + return(0); + if ( s->external != 0 && s->spendtxidind < ramchain->numexternaltxids ) + { + *txidp = ramchain->externalT[s->spendtxidind]; + return(0); + } + else if ( s->external == 0 && s->spendtxidind < ramchain->numtxids ) + { + *txidp = ramchain->T[s->spendtxidind].txid; + return(0); + } + return(-1); + }*/ + + /*if ( ptr->type == 'F' ) + { + if ( addr != 0 && addr->fp != 0 ) + { + //printf("flush.%s %p\n",addr->ipaddr,addr->fp); + fflush(addr->fp); + } + } + else*/ + + /* struct iguana_txblock *ptr; struct iguana_ramchain *ptrs[IGUANA_MAXBUNDLESIZE],*ramchains; + struct iguana_block *block; char fname[1024]; uint64_t estimatedsize = 0; + int32_t i,maxrecv,addrind,flag,bundlei,numdirs=0; struct iguana_ramchain *ramchain; + flag = maxrecv = 0; + memset(ptrs,0,sizeof(ptrs)); + ramchains = mycalloc('p',coin->chain->bundlesize,sizeof(*ramchains)); + for (i=0; in && ichain->bundlesize; i++) + { + if ( (block= iguana_blockfind(coin,bp->hashes[i])) != 0 ) + { + iguana_meminit(&memB[i],"ramchainB",0,block->recvlen*2 + 8192,0); + if ( (ptr= iguana_peertxdata(coin,&bundlei,fname,&memB[i],block->ipbits,block->hash2)) != 0 ) + { + if ( bundlei != i || ptr->block.bundlei != i ) + printf("peertxdata.%d bundlei.%d, i.%d block->bundlei.%d\n",bp->hdrsi,bundlei,i,ptr->block.bundlei); + ptrs[i] = &ramchains[i]; + //char str[65]; + //printf("received txdata.%s bundlei.%d T.%d U.%d S.%d P.%d\n",bits256_str(str,ptr->block.hash2),bundlei,ptr->numtxids,ptr->numunspents,ptr->numspends,ptr->numpkinds); + if ( iguana_ramchainset(coin,ptrs[i],ptr) == ptrs[i] ) + { + char str[65]; int32_t err; + //for (j=0; jnumpkinds; j++) + // init_hexbytes_noT(str,ptrs[i]->P[j].rmd160,20), printf("%s ",str); + err = iguana_ramchainverifyPT(coin,ptrs[i]); + printf("conv err.%d ramchain.%s bundlei.%d T.%d U.%d S.%d P.%d\n",err,bits256_str(str,ptrs[i]->data->firsthash2),bundlei,ptrs[i]->data->numtxids,ptrs[i]->data->numunspents,ptrs[i]->data->numspends,ptrs[i]->data->numpkinds); + ptrs[i]->data->firsti = 0; + if ( block->recvlen > maxrecv ) + maxrecv = block->recvlen; + estimatedsize += block->recvlen; + flag++; + } else printf("error setting ramchain.%d\n",i); + } + else + { + printf("error (%s) hdrs.%d ptr[%d]\n",fname,bp->hdrsi,i); + CLEARBIT(bp->recv,i); + bp->issued[i] = 0; + block = 0; + } + } + } + if ( flag == i ) + { + printf("numpkinds >>>>>>>>> start MERGE.(%ld) i.%d flag.%d estimated.%ld maxrecv.%d\n",(long)mem->totalsize,i,flag,(long)estimatedsize,maxrecv); + if ( (ramchain= iguana_ramchainmergeHT(coin,mem,ptrs,i,bp)) != 0 ) + { + iguana_ramchainsave(coin,ramchain); + iguana_ramchainfree(coin,mem,ramchain); + //printf("ramchain saved\n"); + bp->emitfinish = (uint32_t)time(NULL); + for (addrind=0; addrindpeers.active[addrind].ipbits != 0 ) + { + if ( iguana_peerfile_exists(coin,&coin->peers.active[addrind],fname,bp->hashes[0]) >= 0 ) + { + //printf("remove.(%s)\n",fname); + //iguana_removefile(fname,0); + //coin->peers.numfiles--; + } + } + } + } else bp->emitfinish = 0; + } + else + { + printf(">>>>> bundlesaveHT error: numdirs.%d i.%d flag.%d\n",numdirs,i,flag); + bp->emitfinish = 0; + } + for (i=0; in && ichain->bundlesize; i++) + iguana_mempurge(&memB[i]); + myfree(ramchains,coin->chain->bundlesize * sizeof(*ramchains)); + return(flag);*/ + +#ifdef oldway + int32_t iguana_verifyiAddr(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize) + { + struct iguana_iAddr *iA = value; + if ( itemind == 0 || iA->ipbits != 0 ) + return(0); + else return(-1); + } + + int32_t iguana_initiAddr(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,int32_t itemind,int32_t itemsize,int32_t numitems) + { + struct iguana_iAddr *iA = value; + if ( key == 0 && value == 0 && itemind < 0 && numitems == 0 ) + { + } + else + { + if ( iA != 0 ) + iA->status = 0; + coin->numiAddrs++; + //printf("%x numiAddrs.%d\n",iA->ipbits,coin->numiAddrs); + } + return(0); + } + + int32_t iguana_verifyblock(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize) + { + struct iguana_block *block; + block = value; + if ( bits256_nonz(block->hash2) != 0 ) + return(0); + else return(-1); + } + + int32_t iguana_initblock(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,int32_t itemind,int32_t itemsize,int32_t numitems) + { + bits256 genesis; //struct iguana_block *block = value; + if ( key == 0 && value == 0 && itemind < 0 && numitems == 0 ) + { + if ( coin->blocks.db == 0 ) + coin->blocks.db = kv; + genesis = iguana_genesis(coin,coin->chain); + if ( bits256_nonz(genesis) == 0 ) + return(-1); + else return(0); + } + return(0); + } + + int32_t iguana_nullinit(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,int32_t itemind,int32_t itemsize,int32_t numitems) + { + if ( key != 0 && value != 0 && itemind > 0 ) + { + } + return(0); + } + + int32_t iguana_verifyunspent(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize) + { + if ( itemind < coin->latest.dep.numunspents ) + return(0); + else return(-1); + } + + int32_t iguana_verifyspend(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize) + { + if ( itemind < coin->latest.dep.numspends ) + return(0); + else return(-1); + } + + int32_t iguana_verifytxid(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize) + { + if ( itemind < coin->latest.dep.numtxids ) + return(0); + else return(-1); + } + + int32_t iguana_inittxid(struct iguana_info *coin,struct iguanakv *kv,void *key,void *value,int32_t itemind,int32_t itemsize,int32_t numitems) + { + //uint32_t checktxidind,firstvout,firstvin; struct iguana_txid *tx = value; + if ( key != 0 && value != 0 && itemind > 0 ) + { + /*printf("inittxid.(%s) itemind.%d (%d %d)\n",bits256_str(tx->txid),itemind,tx->firstvout,tx->firstvin); + checktxidind = iguana_txidind(coin,&firstvout,&firstvin,tx->txid); + if ( checktxidind != itemind ) + { + printf("init checktxidind != itemind: %s -> %d vs %d\n",bits256_str(tx->txid),checktxidind,itemind); + return(-1); + }*/ + } + return(0); + } + + int32_t iguana_verifypkhash(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize) + { + if ( itemind < coin->latest.dep.numpkinds ) + return(0); + else return(-1); + } + + struct iguanakv *iguana_kvinit(char *name,int32_t keysize,int32_t threadsafe,int32_t mapped_datasize,int32_t RAMvaluesize,int32_t keyoffset,int32_t flags,int32_t valuesize2,int32_t valuesize3) + { + struct iguanakv *kv; + printf("iguana_kvinit.(%s) keysize.%d mapped_datasize.%d keyoffset.%d\n",name,keysize,mapped_datasize,keyoffset); + kv = mycalloc('K',1,sizeof(*kv)); + portable_mutex_init(&kv->MMlock); + //portable_mutex_init(&kv->MEM.mutex); + portable_mutex_init(&kv->HASHPTRS.mutex); + portable_mutex_init(&kv->KVmutex); + strcpy(kv->name,name); + kv->flags = flags; + kv->valuesize2 = valuesize2, kv->valuesize3 = valuesize3; + kv->RAMvaluesize = RAMvaluesize; + kv->HDDvaluesize = mapped_datasize; + kv->keyoffset = keyoffset; + kv->mult = IGUANA_ALLOC_MULT; + kv->threadsafe = threadsafe; + kv->keysize = keysize; + return(kv); + } + + int32_t iguana_loadkvfile(struct iguana_info *coin,struct iguanakv *kv,int32_t valuesize,int32_t (*verifyitem)(struct iguana_info *coin,void *key,void *ptr,int32_t itemind,int32_t itemsize),int32_t (*inititem)(struct iguana_info *coin,struct iguanakv *kv,void *key,void *ptr,int32_t itemind,int32_t itemsize,int32_t numitems),int32_t maxind) + { + FILE *fp; long fpos; uint8_t *ptr; double lastdisp,factor; int32_t numitems=0,itemind,j,n,skip = 0; + factor = 1.; + if ( (fp= fopen(kv->fname,"rb")) != 0 ) + { + fseek(fp,0,SEEK_END); + fpos = ftell(fp); + numitems = (int32_t)(fpos / valuesize); + fclose(fp); + if ( kv->RAMvaluesize > 0 && kv->HDDvaluesize > 0 && kv->RAMvaluesize > kv->HDDvaluesize && numitems > 0 ) + numitems--; + iguana_kvensure(coin,kv,0); + if ( numitems > 2 || maxind > 0 ) + { + if ( maxind == 0 ) + { + for (itemind=numitems-2; itemind>0; itemind--) + { + ptr = (uint8_t *)((unsigned long)kv->M.fileptr + ((unsigned long)itemind * kv->HDDvaluesize)); + if ( (*verifyitem)(coin,(void *)&ptr[kv->keyoffset],(void *)ptr,itemind,kv->RAMvaluesize) < 0 ) + { + numitems = itemind + 1; + printf("numitems.%d\n",numitems); + break; + } + } + } else numitems = maxind; + if ( numitems > 0 ) + { + lastdisp = 0.; + for (itemind=0; itemind 1000000 && ((double)itemind / numitems) > lastdisp+.01*factor ) + { + if ( factor == 1. ) + fprintf(stderr,"%.0f%% ",100. * lastdisp); + else fprintf(stderr,"%.2f%% ",100. * lastdisp); + lastdisp = ((double)itemind / numitems); + } + ptr = (uint8_t *)((uint64_t)kv->M.fileptr + ((uint64_t)itemind * kv->HDDvaluesize)); + if ( 0 && kv->keysize > 0 ) + { + for (j=0; jkeysize; j++) + if ( ptr[j] != 0 ) + break; + if ( j != kv->keysize && iguana_kvread(coin,kv,(void *)&ptr[kv->keyoffset],kv->space,(uint32_t *)&n) != 0 ) + { + printf("%s: skip duplicate %llx itemind.%d already at %d\n",kv->name,*(long long *)&ptr[kv->keyoffset],itemind,n); + continue; + } + //printf("%s uniq item at itemind.%d\n",kv->name,itemind); + } + if ( (*verifyitem)(coin,(void *)&ptr[kv->keyoffset],(void *)ptr,itemind,kv->RAMvaluesize) == 0 ) + { + //if ( strcmp("txids",kv->name) == 0 ) + //printf("inititem.%d %p (%s)\n",itemind,ptr,bits256_str(*(bits256 *)&ptr[kv->keyoffset])); + // iguana_kvwrite(coin,kv,(void *)&ptr[kv->keyoffset],sp->space,(uint32_t *)&n); + if ( (*inititem)(coin,kv,(void *)&ptr[kv->keyoffset],(void *)ptr,itemind,kv->RAMvaluesize,numitems) == 0 ) + { + kv->numvalid++; + n = itemind; + memcpy(kv->space,ptr,kv->RAMvaluesize); + if ( kv->keysize > 0 ) + iguana_kvwrite(coin,kv,(void *)&ptr[kv->keyoffset],kv->space,(uint32_t *)&n); + else iguana_kvwrite(coin,kv,0,kv->space,(uint32_t *)&n); + } else skip++; + } else break; + } + } + } + kv->numitems = numitems; + kv->numkeys = numitems; + kv->maxitemind = (numitems > 0 ) ? numitems - 1 : 0; + printf("%s: numkeys.%d numitems.%d numvalid.%d maxitemind.%d skipped.%d ELAPSED %.2f minutes\n",kv->name,kv->numkeys,kv->numitems,kv->numvalid,kv->maxitemind,skip,(double)(time(NULL)-coin->starttime)/60.); + if ( (kv->flags & IGUANA_ITEMIND_DATA) != 0 ) + iguana_syncmap(&kv->M,0); + /*if ( strcmp(kv->name,"iAddrs") == 0 && kv->numkeys < numitems/2 ) + { + iguana_closemap(&kv->M); + printf("truncate?\n"), getchar(); + truncate(kv->fname,(kv->numkeys+100)*kv->HDDvaluesize); + }*/ + } + return(numitems); + } + + struct iguanakv *iguana_stateinit(struct iguana_info *coin,int32_t flags,char *coinstr,char *subdir,char *name,int32_t keyoffset,int32_t keysize,int32_t HDDvaluesize,int32_t RAMvaluesize,int32_t inititems,int32_t (*verifyitem)(struct iguana_info *coin,void *key,void *ptr,int32_t itemind,int32_t itemsize),int32_t (*inititem)(struct iguana_info *coin,struct iguanakv *kv,void *key,void *ptr,int32_t itemind,int32_t itemsize,int32_t numitems),int32_t valuesize2,int32_t valuesize3,int32_t maxind,int32_t initialnumitems,int32_t threadsafe) + { + struct iguanakv *kv; int32_t valuesize; + if ( maxind <= 1 ) + maxind = 0; + printf("%s MAX.%d\n",name,maxind); + if ( HDDvaluesize == 0 ) + valuesize = HDDvaluesize = RAMvaluesize; + else valuesize = HDDvaluesize; + kv = iguana_kvinit(name,keysize,threadsafe,HDDvaluesize,RAMvaluesize,keyoffset,flags,valuesize2,valuesize3); + if ( kv == 0 ) + { + printf("cant initialize kv.(%s)\n",name); + exit(-1); + } + if ( (kv->incr= inititems) == 0 ) + kv->incr = IGUANA_ALLOC_INCR; + strcpy(kv->name,name); + sprintf(kv->fname,"DB/%s/%s",coin->symbol,kv->name), iguana_compatible_path(kv->fname); + portable_mutex_init(&kv->MMmutex); + kv->space = mycalloc('K',1,RAMvaluesize + kv->keysize); + kv->maxitemind = kv->numvalid = kv->numitems = 0; + if ( strcmp("txids",kv->name) == 0 ) + coin->txids = kv; + else if ( strcmp("pkhashes",kv->name) == 0 ) + coin->pkhashes = kv; + printf("kv.%p chain.%p\n",kv,coin->chain); + (*inititem)(coin,kv,0,0,-1,valuesize,0); + iguana_loadkvfile(coin,kv,valuesize,verifyitem,inititem,maxind); + if ( initialnumitems != 0 ) + iguana_kvensure(coin,kv,initialnumitems); + return(kv); + } + + uint32_t iguana_syncs(struct iguana_info *coin) + { + FILE *fp; char fnameold[512],fnameold2[512],fname[512],fname2[512]; int32_t i,height,flag = 0; + if ( (coin->blocks.parsedblocks > coin->longestchain-1000 && (coin->blocks.parsedblocks % 100) == 1) || + (coin->blocks.parsedblocks > coin->longestchain-10000 && (coin->blocks.parsedblocks % 1000) == 1) || + (coin->blocks.parsedblocks > coin->longestchain-2000000 && (coin->blocks.parsedblocks % 10000) == 1) || + (coin->blocks.parsedblocks > coin->firstblock+100 && (coin->blocks.parsedblocks % 100000) == 1) ) + { + if ( coin->blocks.parsedblocks > coin->loadedLEDGER.snapshot.height+2 ) + flag = 1; + } + if ( flag != 0 ) + { + height = coin->blocks.parsedblocks - (coin->firstblock != 0); + for (i=0; iLEDGER.snapshot.lhashes[i].txid); + char str[65]; + bits256_str(str,coin->LEDGER.snapshot.ledgerhash); + printf("-> syncs %s ledgerhashes.%d\n",str,height); + //iguana_syncmap(&coin->iAddrs->M,0); + iguana_syncmap(&coin->blocks.db->M,0); + iguana_syncmap(&coin->unspents->M,0); + iguana_syncmap(&coin->unspents->M2,0); + iguana_syncmap(&coin->spends->M,0); + iguana_syncmap(&coin->spends->M2,0); + iguana_syncmap(&coin->txids->M,0); + iguana_syncmap(&coin->pkhashes->M,0); + iguana_syncmap(&coin->pkhashes->M2,0); + iguana_syncmap(&coin->pkhashes->M3,0); + printf("%s threads.%d iA.%d ranked.%d hwm.%u parsed.%u T.%d U.%d %.8f S.%d %.8f net %.8f P.%d\n",coin->symbol,iguana_numthreads(coin,-1),coin->numiAddrs,coin->peers.numranked,coin->blocks.hwmheight+1,height,coin->latest.dep.numtxids,coin->latest.dep.numunspents,dstr(coin->latest.credits),coin->latest.dep.numspends,dstr(coin->latest.debits),dstr(coin->latest.credits)-dstr(coin->latest.debits),coin->latest.dep.numpkinds); + sprintf(fname,"tmp/%s/ledger.%d",coin->symbol,height); + sprintf(fname2,"DB/%s/ledger",coin->symbol); + sprintf(fnameold,"tmp/%s/ledger.old",coin->symbol); + sprintf(fnameold2,"tmp/%s/ledger.old2",coin->symbol); + iguana_renamefile(fnameold,fnameold2); + iguana_renamefile(fname2,fnameold); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + if ( fwrite(coin->accounts,sizeof(*coin->accounts),coin->LEDGER.snapshot.dep.numpkinds,fp) != coin->LEDGER.snapshot.dep.numpkinds ) + printf("WARNING: error saving %s accounts[%d]\n",fname,coin->LEDGER.snapshot.dep.numpkinds); + if ( fwrite(&coin->LEDGER,1,sizeof(coin->LEDGER),fp) != sizeof(coin->LEDGER) ) + printf("WARNING: error saving %s\n",fname); + fclose(fp); + iguana_copyfile(fname,fname2,1); + } + printf("backups created\n"); + } + return((uint32_t)time(NULL)); + } + + // 480a886f78a52d94 2c16330bdd8565f2 fbfb8ba91a6cd871 d1feb1e96190d4ff b8fef8854847e7db 8d2692bcfe41c777 ec86c8502288022f 789ebb3966bb640f -> pre parse 35ee0080a9a132e88477e8809a6e2a0696a06b8c7b13fbfde2955998346dd5c8 ledgerhashes.120000 + // 9d1025feba33725a d69751b2f8d3f626 1f19457ce24411f1 76e12fd68b3b5b3c 2ad1a1e4b3b7014e a699f2904d073771 989c145c04a7a0d0 e888ab12de678518 -> syncs b8cf6b625de1d921695d1d2247ad68b86d047adf417c09562dc620ada993c47d ledgerhashes.140000 + // 53faf4c08ae7cd66 60af0f6074a4460a 8fa0f21eb4996161 7d695aa60788e52c 45a5c96ef55a1797 7b3225a83646caec d2d5788986315066 27372b0616caacf0 -> syncs c874aa3554c69038574e7da352eb624ac539fed97bf73b605d00df0c8cec4c1b ledgerhashes.200000 + // 739df50dbbaedada b83cbd69f08d2a0f 7a8ffa182706c5b7 8215ff6c7ffb9985 4d674a6d386bd759 f829283534a1804 aeb3b0644b01e07f 7ffe4899a261ca96 -> syncs fba47203d5c1d08e5cf55fa461f4deb6d0c97dcfa364ee5b51f0896ffcbcbaa7 ledgerhashes.300000 + // 739df50dbbaedada b83cbd69f08d2a0f 7a8ffa182706c5b7 8215ff6c7ffb9985 4d674a6d386bd759 f829283534a1804 b5e66cbe3a2bdbea 7ffe4899a261ca96 -> syncs 6b3620ba67fad34a29dd86cd5ec9fe6afd2a81d8a5296aa33b03da74fdd20a9b ledgerhashes.300001 + + int32_t iguana_loadledger(struct iguana_info *coin,int32_t hwmheight) + { + FILE *fp; char fname[512],mapname[512],newfname[512]; struct iguana_block *block; struct iguana_prevdep L; + struct iguana_prevdep *dep; int32_t height,i,valid = 0; + dep = &coin->latest.dep; + sprintf(fname,"DB/%s/ledger",coin->symbol); + mapname[0] = newfname[0] = 0; + if ( (fp= fopen(fname,"rb")) == 0 ) + { + sprintf(fname,"tmp/%s/ledger.old",coin->symbol); + if ( (fp= fopen(fname,"rb")) == 0 ) + { + sprintf(fname,"tmp/%s/ledger.old2",coin->symbol); + fp = fopen(fname,"rb"); + } + } + if ( fp != 0 ) + { + sprintf(mapname,"DB/%s/pkhashes2",coin->symbol); + sprintf(newfname,"DB/%s/pkhashes2.over",coin->symbol); + fseek(fp,-sizeof(coin->LEDGER),SEEK_END); + if ( fread(&coin->LEDGER,1,sizeof(coin->LEDGER),fp) != sizeof(coin->LEDGER) ) + printf("WARNING: error loading %s\n",fname); + if ( (block= iguana_blockptr(coin,coin->LEDGER.snapshot.height)) != 0 ) + { + if ( memcmp(block->hash2.bytes,coin->LEDGER.snapshot.blockhash.bytes,sizeof(block->hash2)) == 0 ) + { + fclose(fp); + iguana_renamefile(mapname,newfname); + iguana_renamefile(fname,mapname); + *dep = coin->LEDGER.snapshot.dep; + coin->loadedLEDGER = coin->LEDGER; + memcpy(&coin->latest.ledgerhash,&coin->LEDGER.snapshot.ledgerhash,sizeof(coin->LEDGER.snapshot.ledgerhash)); + memcpy(coin->latest.lhashes,coin->LEDGER.snapshot.lhashes,sizeof(coin->LEDGER.snapshot.lhashes)); + memcpy(coin->latest.states,coin->LEDGER.snapshot.states,sizeof(coin->LEDGER.snapshot.states)); + printf("found ledger height.%d loadedht.%d\n",block->height,coin->LEDGER.snapshot.height); //getchar(); + for (i=0; iLEDGER.snapshot.lhashes[i].txid); + char str[65]; + bits256_str(str,coin->LEDGER.snapshot.ledgerhash); + printf("-> %s ledgerhashes.%x\n",str,calc_crc32(0,&coin->latest.states[IGUANA_LHASH_TXIDS],sizeof(coin->latest.states[IGUANA_LHASH_TXIDS]))); + printf("loaded H.%d T%d U%d S%d P%d\n",coin->LEDGER.snapshot.height,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds); //getchar(); + coin->latest.credits = coin->LEDGER.snapshot.credits; + coin->latest.debits = coin->LEDGER.snapshot.debits; + coin->latest.dep.supply = (coin->LEDGER.snapshot.credits - coin->LEDGER.snapshot.debits); + return(block->height); + } + } + fclose(fp); + } + dep->numpkinds = dep->numtxids = dep->numunspents = dep->numspends = 1; + while ( hwmheight > 0 ) + { + if ( (block= iguana_blockptr(coin,hwmheight)) != 0 ) + { + iguana_setdependencies(coin,block,&L); + //printf("block.%d: T.%d (%d %d) U.%d S.%d A.%d\n",hwmheight,dep->numtxids,block->numvouts,block->numvins,dep->numunspents,dep->numspends,dep->numpkhashes); + if ( L.numtxids != 0 && L.numunspents != 0 && L.numspends != 0 && block->numvouts != 0 && block->txn_count != 0 && L.numpkinds != 0 ) + { + if ( valid++ > 25 ) + break; + } + } else printf("missing block.%d\n",hwmheight); + hwmheight--; + } + for (height=0; height<=hwmheight; height++) + { + if ( iguana_setdependencies(coin,iguana_blockptr(coin,height),&L) < 0 ) + break; + dep->numtxids = L.numtxids + 0*block->txn_count; + dep->numunspents = L.numunspents + 0*block->numvouts; + dep->numspends = L.numspends + 0*block->numvins; + dep->numpkinds = L.numpkinds; + } + return(hwmheight); + } + + int32_t iguana_validateramchain(struct iguana_info *coin,int64_t *netp,uint64_t *creditsp,uint64_t *debitsp,int32_t height,struct iguana_block *block,int32_t hwmheight,struct iguana_prevdep *lp) + { + uint32_t i,n,m,u,txidind,unspentind,spendind,pkind,checkind,numvins,numvouts,txind,firstvout,firstvin,nextfirstvout,nextfirstvin; struct iguana_prevdep *nextlp; + struct iguana_txid T,nextT; uint64_t credits,debits,nets; struct iguana_block *nextblock; + credits = debits = nets = *creditsp = *debitsp = *netp = numvouts = numvins = 0; + if ( block->height == height ) + { + txidind = lp->numtxids, unspentind = lp->numunspents, spendind = lp->numspends, pkind = lp->numpkinds; + //printf("validate.%d (t%d u%d s%d p%d)\n",height,txidind,unspentind,spendind,pkind); + for (txind=0; txindtxn_count; txind++,txidind++) + { + T = coin->T[txidind], nextT = coin->T[txidind+1]; + //printf("h%d i%d T.%d (%d %d) -> (%d %d)\n",height,txind,txidind,T.firstvout,T.firstvin,nextT.firstvout,nextT.firstvin); + if ( height == 0 && (T.firstvout == 0 || T.firstvin == 0) ) + return(-1); + //printf(">>>> h%d i%d T.%d (%d %d) -> (%d %d) cmp.(%d %d)\n",height,txind,txidind,T.firstvout,T.firstvin,nextT.firstvout,nextT.firstvin,height == 0,(T.firstvout == 0 || T.firstvin == 0)); + if ( (checkind= iguana_txidind(coin,&firstvout,&firstvin,T.txid)) == txidind ) + { + if ( T.firstvout != firstvout || T.firstvin != firstvin ) + { + printf("mismatched rwtxidind %d != %d, %d != %d\n",T.firstvout,firstvout,T.firstvin,firstvin); + getchar(); + return(-1); + } + if ( txind == 0 && (firstvout != unspentind || firstvin != spendind) ) + { + char str[65]; + bits256_str(str,T.txid); + printf("h.%d txind.%d txidind.%d %s firstvout.%d != U%d firstvin.%d != S%d\n",height,txind,txidind,str,firstvout,unspentind,firstvin,spendind); + iguana_txidind(coin,&firstvout,&firstvin,T.txid); + iguana_txidind(coin,&firstvout,&firstvin,T.txid); + return(-1); + } + nextfirstvout = nextT.firstvout, nextfirstvin = nextT.firstvin; + if ( nextfirstvout < unspentind || nextfirstvin < spendind ) + { + printf("h.%d txind.%d nexttxidind.%d firstvout.%d != U%d firstvin.%d != S%d\n",height,txind,txidind,nextfirstvout,unspentind,nextfirstvin,spendind); + if ( nextfirstvout == 0 && nextfirstvin == 0 ) + { + coin->T[txidind+1].firstvout = unspentind; + coin->T[txidind+1].firstvin = spendind; + printf("autofixed\n"); + } + else + { + getchar(); + return(-1); + } + } + n = (nextfirstvout - T.firstvout); + m = (nextfirstvin - T.firstvin); + //printf("height.%d n.%d m.%d U.(%d - %d) S.(%d - %d)\n",height,n,m,nextfirstvout,T.firstvout,nextfirstvin,T.firstvin); + for (i=0; iU[unspentind].value; + if ( coin->Uextras[unspentind].spendind == 0 ) + nets += coin->U[unspentind].value; + if ( coin->U[unspentind].pkind > pkind ) + pkind = coin->U[unspentind].pkind; + //printf("i.%d: unspentind.%d\n",i,unspentind); + } + for (i=0; iS[spendind].spendtxidind) > 0 && u < coin->latest.dep.numunspents ) + debits += coin->U[u].value; + else + { + printf("cant read spendind.%d or S.unspentind %d\n",spendind+i,u); + getchar(); + } + } + numvouts += n; + numvins += m; + } + else + { + char str[65]; + bits256_str(str,T.txid); + printf("height.%d txind.%d txid.%s txidind.%d != %d\n",height,txind,str,txidind,checkind); + getchar(); + return(-1); + } + } + if ( numvins != block->numvins || numvouts != block->numvouts ) + { + printf("height.%d numvins or numvouts error %d != %d || %d != %d\n",height,numvins,block->numvins,numvouts,block->numvouts); + if ( block->numvins == 0 && block->numvouts == 0 ) + { + block->numvins = numvins; + block->numvouts = numvouts; + iguana_kvwrite(coin,coin->blocks.db,0,block,(uint32_t *)&block->height); + m = 0;//iguana_fixblocks(coin,height,hwmheight); + printf("autocorrected.%d\n",m); + exit(1); + } + else + { + getchar(); + return(-1); + } + } + *creditsp = credits, *debitsp = debits, *netp = nets; + if ( (nextblock= iguana_blockptr(coin,height+1)) != 0 ) + { + nextlp = 0; + if ( 0 && lp->supply+credits-debits != nextlp->supply ) + { + printf("nextblock.%d supply mismatch %.8f (%.8f - %.8f) %.8f != %.8f\n",height+1,dstr(lp->supply),dstr(credits),dstr(debits),dstr(lp->supply+credits-debits),dstr(nextlp->supply)); + getchar(); + return(-1); + } + if ( txidind != nextlp->numtxids || unspentind != nextlp->numunspents || spendind != nextlp->numspends )//|| pkind+1 != nextlp->numpkinds ) + { + printf("Block.(h%d t%d u%d s%d p%d) vs next.(h%d t%d u%d s%d p%d)\n",block->height,txidind,unspentind,spendind,pkind,height+1,nextlp->numtxids,nextlp->numunspents,nextlp->numspends,nextlp->numpkinds); + return(-1); + } + return(0); + } + printf("cant find next block at %d\n",height+1); + //printf("block.%d %.8f (%.8f - %.8f)\n",height,dstr(nets),dstr(credits),dstr(debits)); + } else printf("height mismatch %d != %d\n",height,block->height); + //getchar(); + return(-1); + } + + int32_t iguana_fixsecondary(struct iguana_info *coin,int32_t numtxids,int32_t numunspents,int32_t numspends,int32_t numpkinds,struct iguana_Uextra *Uextras,struct iguana_pkextra *pkextras,struct iguana_account *accounts) + { + uint32_t i; int32_t m,err; + if ( numtxids < 2 || numunspents < 2 || numspends < 2 || numpkinds < 2 ) + return(0); + //struct iguana_Uextra { uint32_t spendind; }; // unspentind + //struct iguana_unspent { uint64_t value; uint32_t pkind,txidind,prevunspentind; }; + for (i=m=err=0; i= numspends ) + m++, Uextras[i].spendind = 0;//, printf("%d ",Uextras[i].spendind); + if ( coin->U[i].prevunspentind != 0 && coin->U[i].prevunspentind >= i ) + err++, printf("preverr.%d/%d ",coin->U[i].prevunspentind,i); + if ( coin->U[i].txidind >= numtxids ) + err++, printf("errtxidind.%d ",coin->U[i].txidind); + if ( coin->U[i].pkind >= numpkinds ) + err++, printf("errpkind.%d ",coin->U[i].pkind); + } + if ( (err+m) != 0 ) + iguana_syncmap(&coin->unspents->M2,0); + printf("cleared %d Uextras before numunspents.%d beyond errs.%d\n",m,numunspents,err); + if ( err != 0 ) + getchar(); + //struct iguana_pkextra { uint32_t firstspendind; }; // pkind + for (i=m=0; i= numspends ) + m++, pkextras[i].firstspendind = 0;//, printf("firstS.%d ",pkextras[i].firstspendind); + } + if ( m != 0 ) + iguana_syncmap(&coin->pkhashes->M3,0); + printf("pkextras beyond numspends.%d m.%d accounts.%p\n",numspends,m,accounts); + //struct iguana_spend { uint32_t unspentind,prevspendind; }; // dont need nextspend + /*for (i=err=m=0; iS[i].unspentind >= numunspents ) + err++, coin->S[i].unspentind = 0;//, printf("S->U%d ",coin->S[i].unspentind); + //printf("%d ",coin->S[i].prevspendind); + if ( coin->Sextras[i].prevspendind != 0 && coin->Sextras[i].prevspendind >= i ) + m++, coin->Sextras[i].prevspendind = 0, printf("preverr.%d:%d ",coin->Sextras[i].prevspendind,i); + } + printf("errs.%d in spends numspends.%d\n",err,numspends); + if ( err != 0 ) + getchar();*/ + return(0); + } + + void clearmem(void *ptr,int32_t len) + { + static const uint8_t zeroes[512]; + if ( len > sizeof(zeroes) || memcmp(ptr,zeroes,len) != 0 ) + memset(ptr,0,len); + } + + int32_t iguana_clearoverage(struct iguana_info *coin,int32_t numtxids,int32_t numunspents,int32_t numspends,int32_t numpkinds,struct iguana_Uextra *Uextras,struct iguana_pkextra *pkextras,struct iguana_account *accounts) + { + uint32_t i,n; + printf("clear txids\n"); + n = (uint32_t)((uint64_t)coin->txids->M.allocsize / coin->txids->HDDvaluesize) - 2; + for (i=numtxids+1; iT[i],sizeof(coin->T[i])); + + printf("clear pkinds\n"); + n = (uint32_t)((uint64_t)coin->pkhashes->M.allocsize / coin->pkhashes->HDDvaluesize) - 2; + for (i=numpkinds; iP[i],sizeof(coin->P[i])); + n = (uint32_t)((uint64_t)coin->pkhashes->M2.allocsize / coin->pkhashes->valuesize2) - 2; + for (i=numpkinds; ipkhashes->M3.allocsize / coin->pkhashes->valuesize3) - 2; + for (i=numpkinds; iunspents->M.allocsize / coin->unspents->HDDvaluesize) - 2; + for (i=numunspents; iU[i],sizeof(coin->U[i])); + n = (uint32_t)((uint64_t)coin->unspents->M2.allocsize / coin->unspents->valuesize2) - 2; + for (i=numunspents; ispends->M.allocsize / coin->spends->HDDvaluesize) - 2; + for (i=numspends; iS[i],sizeof(coin->S[i])); + //n = (uint32_t)((uint64_t)coin->spends->M2.allocsize / coin->spends->valuesize2) - 2; + //for (i=numspends; iSextras[i],sizeof(coin->Sextras[i])); + return(0); + } + + int64_t iguana_verifybalances(struct iguana_info *coin,int32_t fullverify) + { + int64_t err,balance = 0; int32_t i,numerrs = 0; + for (i=0; ilatest.dep.numpkinds; i++) + { + if ( fullverify != 0 ) + { + if ( (err= iguana_verifyaccount(coin,&coin->accounts[i],i)) < 0 ) + { + printf("err.%d from pkind.%d\n",(int32_t)err,i); + numerrs++; + } + } + balance += coin->accounts[i].balance; + } + printf("iguana_verifybalances %.8f numerrs.%d\n",dstr(balance),numerrs); + if ( numerrs > 0 ) + getchar(); + return(balance); + } + + int32_t iguana_initramchain(struct iguana_info *coin,int32_t hwmheight,int32_t mapflags,int32_t fullverify) + { + struct iguana_prevdep *dep; struct iguana_block *block,lastblock; double lastdisp = 0.; + // init sequence is very tricky. must be done in the right order and make sure to only use data + // that has already been initialized. and at the end all the required fields need to be correct + struct iguana_msghdr H; uint8_t buf[1024]; int32_t len,height,valid=0,flag=0; + struct iguana_prevdep L,prevL; + int64_t checkbalance,net,nets; uint64_t prevcredits,prevdebits,credit,debit,credits,debits,origsupply; + dep = &coin->latest.dep; + height = hwmheight; + if ( (height= iguana_loadledger(coin,hwmheight)) < 0 ) + { + printf("iguana_initramchain: unrecoverable loadledger error hwmheight.%d\n",hwmheight); + return(-1); + } + hwmheight = height; + printf("four ramchains start valid.%d height.%d txids.%d vouts.%d vins.%d pkhashes.%d\n",valid,hwmheight,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds); + //four ramchains start valid.0 height.316904 txids.45082870 vouts.27183907 vins.107472009 pkhashes.44807925 3.57 minutes + + coin->unspents = iguana_stateinit(coin,IGUANA_ITEMIND_DATA,coin->symbol,coin->symbol,"unspents",0,0,sizeof(struct iguana_unspent),sizeof(struct iguana_unspent),100000,iguana_verifyunspent,iguana_nullinit,sizeof(*coin->Uextras),0,dep->numunspents,2500000,0); + if ( coin->unspents == 0 ) + printf("cant create unspents\n"), exit(1); + coin->unspents->HDDitemsp = (void **)&coin->U, coin->U = coin->unspents->M.fileptr; + coin->unspents->HDDitems2p = (void **)&coin->Uextras, coin->Uextras = coin->unspents->M2.fileptr; + printf("four ramchains start valid.%d height.%d txids.%d vouts.%d vins.%d pkhashes.%d %.2f minutes\n",valid,hwmheight,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,((double)time(NULL)-coin->starttime)/60.); + + coin->spends = iguana_stateinit(coin,IGUANA_ITEMIND_DATA,coin->symbol,coin->symbol,"spends",0,0,sizeof(struct iguana_spend),sizeof(struct iguana_spend),100000,iguana_verifyspend,iguana_nullinit,0,0,dep->numspends,2500000,0); + if ( coin->spends == 0 ) + printf("cant create spends\n"), exit(1); + printf("four ramchains start valid.%d height.%d txids.%d vouts.%d vins.%d pkhashes.%d %.2f minutes\n",valid,hwmheight,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,((double)time(NULL)-coin->starttime)/60.); + coin->spends->HDDitemsp = (void **)&coin->S, coin->S = coin->spends->M.fileptr; + coin->spends->HDDitems2p = (void **)&coin->Sextras, coin->Sextras = coin->spends->M2.fileptr; + + coin->txids = iguana_stateinit(coin,IGUANA_ITEMIND_DATA|((mapflags&IGUANA_MAPTXIDITEMS)!=0)*IGUANA_MAPPED_ITEM,coin->symbol,coin->symbol,"txids",0,sizeof(bits256),sizeof(struct iguana_txid),sizeof(struct iguana_txid),100000,iguana_verifytxid,iguana_inittxid,0,0,dep->numtxids,1000000,0); + if ( coin->txids == 0 ) + printf("cant create txids\n"), exit(1); + coin->txids->HDDitemsp = (void **)&coin->T, coin->T = coin->txids->M.fileptr; + printf("four ramchains start valid.%d height.%d txids.%d vouts.%d vins.%d pkhashes.%d %.2f minutes\n",valid,hwmheight,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,((double)time(NULL)-coin->starttime)/60.); + + coin->pkhashes = iguana_stateinit(coin,IGUANA_ITEMIND_DATA|((mapflags&IGUANA_MAPPKITEMS)!=0)*IGUANA_MAPPED_ITEM,coin->symbol,coin->symbol,"pkhashes",0,20,sizeof(struct iguana_pkhash),sizeof(struct iguana_pkhash),100000,iguana_verifypkhash,iguana_nullinit,sizeof(*coin->accounts),sizeof(*coin->pkextras),dep->numpkinds,1000000,0); + if ( coin->pkhashes == 0 ) + printf("cant create pkhashes\n"), exit(1); + coin->pkhashes->HDDitemsp = (void **)&coin->P, coin->P = coin->pkhashes->M.fileptr; + coin->pkhashes->HDDitems2p = (void **)&coin->accounts, coin->accounts = coin->pkhashes->M2.fileptr; + coin->pkhashes->HDDitems3p = (void **)&coin->pkextras, coin->pkextras = coin->pkhashes->M3.fileptr; + printf("four ramchains start valid.%d height.%d txids.%d vouts.%d vins.%d pkhashes.%d %.2f minutes\n",valid,hwmheight,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,((double)time(NULL)-coin->starttime)/60.); + + iguana_kvensure(coin,coin->txids,dep->numtxids + coin->txids->incr); + iguana_kvensure(coin,coin->pkhashes,dep->numpkinds + coin->pkhashes->incr); + iguana_kvensure(coin,coin->unspents,dep->numunspents + coin->unspents->incr); + iguana_kvensure(coin,coin->spends,dep->numspends + coin->spends->incr); + coin->txids->numkeys = dep->numtxids; + coin->unspents->numkeys = dep->numunspents; + coin->spends->numkeys = dep->numspends; + coin->pkhashes->numkeys = dep->numpkinds; + iguana_fixsecondary(coin,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,coin->Uextras,coin->pkextras,coin->accounts); + printf("hwmheight.%d KV counts T.%d P.%d U.%d S.%d\n",hwmheight,coin->txids->numkeys,coin->pkhashes->numkeys,coin->unspents->numkeys,coin->spends->numkeys); + memset(&lastblock,0,sizeof(lastblock)); + origsupply = dep->supply, dep->supply = 0; + for (prevcredits=prevdebits=credits=debits=nets=height=0; height<=hwmheight; height++) + { + if ( hwmheight > 10000 && ((double)height / hwmheight) > lastdisp+.01 ) + { + fprintf(stderr,"%.0f%% ",100. * lastdisp); + lastdisp = ((double)height / hwmheight); + } + if ( (block= iguana_blockptr(coin,height)) == 0 ) + { + printf("error getting height.%d\n",height); + break; + } + lastblock = *block; + if ( height == hwmheight ) + break; + printf("need to set valid L\n"); + if ( iguana_validateramchain(coin,&net,&credit,&debit,height,block,hwmheight,&L) < 0 ) + { + printf("UNRECOVERABLE error iguana_validateramchain height.%d\n",height); + getchar(); + exit(1); + break; + } + nets += net, credits += credit, debits += debit; + if ( nets != (credits - debits) ) + { + //printf("height.%d: net %.8f != %.8f (%.8f - %.8f)\n",height,dstr(nets),dstr(credits)-dstr(debits),dstr(credits),dstr(debits)); + //break; + } + prevcredits = credits; + prevdebits = debits; + } + if ( lastblock.height == 0 ) + dep->numpkinds = dep->numspends = dep->numtxids = dep->numunspents = 1, dep->supply = 0, coin->latest.credits = coin->latest.debits = 0; + else + { + printf("set prevL\n"); + dep->numtxids = prevL.numtxids; + dep->numunspents = prevL.numunspents; + dep->numspends = prevL.numspends; + dep->numpkinds = prevL.numpkinds; + dep->supply = prevL.supply; + coin->latest.credits = prevcredits; + coin->latest.debits = prevdebits; + if ( dep->supply != (prevcredits - prevdebits) ) + { + printf("override supply %.8f (%.8f - %.8f)\n",dstr(dep->supply),dstr(prevcredits),dstr(prevdebits)); + dep->supply = (prevcredits - prevdebits); + } + checkbalance = iguana_verifybalances(coin,0); + if ( (checkbalance != dep->supply || fullverify != 0) && iguana_verifybalances(coin,1) != dep->supply ) + { + printf("balances mismatch\n"); + getchar(); + } + } + coin->txids->numkeys = dep->numtxids; + coin->unspents->numkeys = dep->numunspents; + coin->spends->numkeys = dep->numspends; + coin->pkhashes->numkeys = dep->numpkinds; + coin->blocks.parsedblocks = lastblock.height; + printf("\nhwmheight.%d KV counts T.%d P.%d U.%d S.%d %.8f (%.8f - %.8f)\n",hwmheight,coin->txids->numkeys,coin->pkhashes->numkeys,coin->unspents->numkeys,coin->spends->numkeys,dstr(coin->latest.dep.supply),dstr(coin->latest.credits),dstr(coin->latest.debits)); + printf("four ramchains start valid.%d height.%d txids.%d vouts.%d vins.%d pkhashes.%d %.2f minutes\n",valid,hwmheight,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,((double)time(NULL)-coin->starttime)/60.); + printf("height.%d after validateramchain hwmheight.%d flag.%d parsed.%d\n",height,hwmheight,flag,coin->blocks.parsedblocks); //getchar(); + if ( coin->blocks.parsedblocks == 0 ) + { + uint8_t txspace[32768]; struct iguana_memspace MEM; + len = (int32_t)strlen(coin->chain->genesis_hex)/2; + decode_hex(buf,len,(char *)coin->chain->genesis_hex); + iguana_sethdr(&H,coin->chain->netmagic,"block",buf,len); + iguana_meminit(&MEM,"genesis",txspace,sizeof(txspace),0); + iguana_parser(coin,0,&MEM,&MEM,0,&H,buf,len); + printf("coin->blocks.parsedblocks.%d KV counts T.%d P.%d U.%d S.%d\n",coin->blocks.parsedblocks,coin->txids->numkeys,coin->pkhashes->numkeys,coin->unspents->numkeys,coin->spends->numkeys); + printf("auto parse genesis\n"); //getchar(); + } + else iguana_clearoverage(coin,dep->numtxids,dep->numunspents,dep->numspends,dep->numpkinds,coin->Uextras,coin->pkextras,coin->accounts); + return(coin->blocks.parsedblocks); + } +#endif + if ( 0 && queue_size(&coin->blocksQ) == 0 ) + { + HASH_ITER(hh,coin->blocks.hash,block,tmp) + { + if ( bits256_nonz(block->prev_block) > 0 && (prev= iguana_blockfind(coin,block->prev_block)) != 0 ) + { + if ( prev->mainchain != 0 ) + { + char str[65]; printf("idle issue %s %d\n",bits256_str(str,block->hash2),prev->height+1); + iguana_blockQ(coin,0,-1,block->hash2,0); + } + } + } + } + struct iguana_ramchain *iguana_ramchainmergeHT(struct iguana_info *coin,struct iguana_memspace *mem,struct iguana_ramchain *ramchains[],int32_t n,struct iguana_bundle *bp) + { + /* uint32_t numtxids,numunspents,numspends,numpkinds,numexternaltxids,i,j,k; uint64_t allocsize = 0; + struct iguana_txid *tx; struct iguana_account *acct; struct iguana_ramchain *ramchain,*item; + struct iguana_pkhash *p,oldP; struct iguana_unspent *u; struct iguana_kvitem *ptr; + bits256 txid; uint32_t txidind,unspentind,spendind,pkind,numblocks; struct iguana_spend *s; + numtxids = numunspents = numspends = numpkinds = 1; + numexternaltxids = 1; + numblocks = 0; + for (i=0; iramchain.hdrsi,i); + return(0); + } + numtxids += item->numtxids, numunspents += item->numunspents, numspends += item->numspends; + numpkinds += item->numpkinds, numexternaltxids += item->numexternaltxids; + numblocks += item->numblocks; + } + allocsize = sizeof(*ramchain) + + (numtxids * sizeof(*ramchain->T)) + + (numunspents * (sizeof(*ramchain->U) + sizeof(*ramchain->Uextras))) + + (numspends * sizeof(*ramchain->S)) + + (numpkinds * (sizeof(*ramchain->P) + sizeof(*ramchain->pkextras) + sizeof(*ramchain->accounts))) + + (numexternaltxids * sizeof(*ramchain->externalT)); + + iguana_meminit(mem,"ramchain",0,allocsize,0); + mem->alignflag = sizeof(uint32_t); + ramchain= &bp->ramchain; //iguana_memalloc(mem,sizeof(*ramchain),1)) == 0 ) + ramchain->numblocks = numblocks; + ramchain->numtxids = numtxids, ramchain->numunspents = numunspents; + ramchain->numspends = numspends, ramchain->numpkinds = numpkinds; + ramchain->numexternaltxids = numexternaltxids; + ramchain->hdrsi = bp->ramchain.hdrsi, ramchain->bundleheight = bp->ramchain.bundleheight, ramchain->numblocks = n; + ramchain->prevbundlehash2 = bp->prevbundlehash2, ramchain->nextbundlehash2 = bp->nextbundlehash2; + ramchain->hash2 = ramchains[0]->hash2; + ramchain->prevhash2 = ramchains[0]->prevhash2, ramchain->lasthash2 = ramchains[n-1]->hash2; + ramchain->T = iguana_memalloc(mem,sizeof(*ramchain->T) * ramchain->numtxids,0); + ramchain->U = iguana_memalloc(mem,sizeof(*ramchain->U) * ramchain->numunspents,0); + if ( ramchain->numspends > 0 ) + ramchain->S = iguana_memalloc(mem,sizeof(*ramchain->S) * ramchain->numspends,0); + ramchain->Uextras = iguana_memalloc(mem,sizeof(*ramchain->Uextras) * ramchain->numunspents,1); + ramchain->P = iguana_memalloc(mem,sizeof(*ramchain->P) * ramchain->numpkinds,1); + ramchain->pkextras = iguana_memalloc(mem,sizeof(*ramchain->pkextras) * ramchain->numpkinds,1); + ramchain->accounts = iguana_memalloc(mem,sizeof(*ramchain->accounts) * ramchain->numpkinds,1); + if ( ramchain->numexternaltxids > 0 ) + ramchain->externalT = iguana_memalloc(mem,ramchain->numexternaltxids * sizeof(*ramchain->externalT),1); + if ( mem->used != allocsize ) + { + printf("error allocating ramchain %ld != %ld\n",(long)mem->used,(long)allocsize); + iguana_ramchainfree(coin,mem,ramchain); + return(0); + } + ramchain->allocsize = allocsize; + ramchain->firsti = 1; + //printf("Allocated %s for bp %d\n",mbstr(str,allocsize),bp->ramchain.bundleheight); + txidind = unspentind = numtxids = spendind = numunspents = numspends = numpkinds = ramchain->firsti; + numexternaltxids = 0; + for (i=0; ifirsti; jnumtxids; j++,txidind++) + { + tx = &ramchain->T[txidind]; + *tx = item->T[j]; + tx->txidind = txidind; + if ( (ptr= iguana_hashfind(ramchain->txids,tx->txid.bytes,sizeof(tx->txid))) != 0 ) + { + printf("unexpected duplicate txid[%d]\n",txidind); + iguana_ramchainfree(coin,mem,ramchain); + return(0); + } + iguana_hashsetHT(ramchain->txids,0,tx->txid.bytes,sizeof(bits256),txidind); + tx->firstvout = unspentind; + for (k=item->firsti; knumvouts; k++,unspentind++) + { + u = &ramchain->U[unspentind]; + *u = item->U[k]; + u->txidind = txidind; + oldP = item->P[item->U[k].pkind]; + if ( (ptr= iguana_hashfind(ramchain->pkhashes,oldP.rmd160,sizeof(oldP.rmd160))) == 0 ) + { + pkind = numpkinds++; + p = &ramchain->P[pkind]; + *p = oldP; + p->firstunspentind = unspentind; + if ( (ptr= iguana_hashsetHT(ramchain->pkhashes,0,p->rmd160,sizeof(p->rmd160),numpkinds)) == 0 ) + { + iguana_ramchainfree(coin,mem,ramchain); + printf("fatal error adding pkhash\n"); + return(0); + } + //printf("pkind.%d: %p %016lx <- %016lx\n",pkind,p,*(long *)p->rmd160,*(long *)oldP.rmd160); + } else pkind = ptr->hh.itemind; + u->pkind = pkind; + acct = &ramchain->accounts[pkind]; + u->prevunspentind = acct->lastunspentind; + acct->lastunspentind = unspentind; + acct->balance += u->value; + } + tx->firstvin = spendind; + spendind += tx->numvins; + } + numtxids += item->numtxids, numunspents += item->numunspents; + } + } + txidind = spendind = ramchain->firsti; + for (i=0; ifirsti; jnumtxids; j++,txidind++) + { + tx = &ramchain->T[j]; + for (k=item->firsti; knumvins; k++) + { + //printf("item.%p [%d] X.%p i.%d j.%d k.%d txidind.%d/%d spendind.%d/%d s->txidind.%d/v%d\n",item,item->numexternaltxids,item->externalT,i,j,k,txidind,ramchain->numtxids,spendind,ramchain->numspends,item->S[k].spendtxidind,item->S[k].vout); + if ( iguana_ramchaintxid(coin,&txid,item,&item->S[k]) < 0 ) + { + printf("i.%d j.%d k.%d error getting txid firsti.%d X.%d vout.%d spend.%d/%d numX.%d numT.%d\n",i,j,k,item->firsti,item->S[k].external,item->S[k].vout,item->S[k].spendtxidind,item->numspends,item->numexternaltxids,item->numtxids); + //iguana_ramchainfree(coin,mem,ramchain); + //return(0); + } + s = &ramchain->S[spendind]; + *s = item->S[k]; + if ( s->vout == 0xffff ) + { + // mining output + } + else if ( (ptr= iguana_hashfind(ramchain->txids,txid.bytes,sizeof(txid))) != 0 ) + { + if ( (s->spendtxidind= ptr->hh.itemind) >= ramchain->numtxids ) + { + s->external = 1; + s->spendtxidind -= ramchain->numtxids; + } + else if ( s->spendtxidind >= item->firsti && s->spendtxidind < item->numtxids ) + { + s->external = 0; + unspentind = (ramchain->T[s->spendtxidind].firstvout + s->vout); + u = &ramchain->U[unspentind]; + p = &ramchain->P[u->pkind]; + if ( ramchain->pkextras[u->pkind].firstspendind == 0 ) + ramchain->pkextras[u->pkind].firstspendind = spendind; + acct = &ramchain->accounts[u->pkind]; + s->prevspendind = acct->lastspendind; + acct->lastspendind = spendind; + if ( ramchain->Uextras[unspentind].spendind != 0 ) + { + printf("double spend u.%d has spendind.%d when s.%d refers to it\n",unspentind,ramchain->Uextras[unspentind].spendind,spendind); + iguana_ramchainfree(coin,mem,ramchain); + return(0); + } + ramchain->Uextras[unspentind].spendind = spendind; + } + spendind++; + } + else if ( numexternaltxids < ramchain->numexternaltxids ) + { + s->external = 1; + ramchain->externalT[numexternaltxids] = txid; + iguana_hashsetHT(ramchain->txids,0,ramchain->externalT[numexternaltxids].bytes,sizeof(ramchain->externalT[numexternaltxids]),ramchain->numtxids + numexternaltxids); + s->spendtxidind = numexternaltxids++; + spendind++; + } + else printf("numexternaltxids.%d >= ramchain numexternaltxids.%d\n",numexternaltxids,ramchain->numexternaltxids); + } + } + // iguana_unspent { uint64_t value; uint32_t txidind,pkind,prevunspentind; } iguana_Uextra { uint32_t spendind; } + // iguana_pkhash { uint8_t rmd160[20]; uint32_t firstunspentind,flags; } iguana_pkextra { uint32_t firstspendind; } + // iguana_account { uint64_t balance; uint32_t lastunspentind,lastspendind; } + // iguana_spend { uint32_t unspentind,prevspendind:31,diffsequence:1; } + numspends += item->numspends; + } + } + //for (i=0; iP[i],*(long *)ramchain->P[i].rmd160); + //printf("numpkinds.%d\n",numpkinds); + if ( 0 ) + { + memcpy(&ramchain->P[numpkinds],ramchain->pkextras,sizeof(*ramchain->pkextras) * numpkinds); + ramchain->pkextras = (void *)&ramchain->P[numpkinds]; + memcpy(&ramchain->pkextras[numpkinds],ramchain->accounts,sizeof(*ramchain->accounts) * numpkinds); + ramchain->accounts = (void *)&ramchain->pkextras[numpkinds]; + memcpy(&ramchain->accounts[numpkinds],ramchain->externalT,sizeof(*ramchain->externalT) * numexternaltxids); + ramchain->externalT = (void *)&ramchain->accounts[numpkinds]; + } + ramchain->allocsize -= ((ramchain->numpkinds - numpkinds) * (sizeof(*ramchain->P) + sizeof(*ramchain->pkextras) + sizeof(*ramchain->accounts))); + ramchain->allocsize -= ((ramchain->numexternaltxids - numexternaltxids) * sizeof(*ramchain->externalT)); + ramchain->numpkinds = numpkinds; + ramchain->numexternaltxids = numexternaltxids;*/ + /*vupdate_sha256(ramchain->lhashes[IGUANA_LHASH_UNSPENT].bytes,&ramchain->states[IGUANA_LHASH_UNSPENT],(void *)ramchain->U,sizeof(*ramchain->U)*ramchain->numunspents); + vupdate_sha256(ramchain->lhashes[IGUANA_LHASH_ACCOUNTS].bytes,&ramchain->states[IGUANA_LHASH_ACCOUNTS],(void *)acct,sizeof(*acct)); + vupdate_sha256(ramchain->lhashes[IGUANA_LHASH_SPENDS].bytes,&ramchain->states[IGUANA_LHASH_SPENDS],(void *)ramchain->S,sizeof(*ramchain->S)*); + vupdate_sha256(ramchain->lhashes[IGUANA_LHASH_TXIDS].bytes,&ramchain->states[IGUANA_LHASH_TXIDS],(void *)tx,sizeof(*tx));*/ + /*mem->used = (long)ramchain->allocsize; + printf("B.%d T.%d U.%d S.%d P.%d combined ramchain size.%ld\n",ramchain->numblocks,ramchain->numtxids,ramchain->numunspents,ramchain->numspends,ramchain->numpkinds,(long)ramchain->allocsize); + return(ramchain);*/ + return(0); + } + + /* + //if ( num > coin->chain->bundlesize+1 ) + // num = coin->chain->bundlesize+1; + for (i=1; i 0 ) + { + if ( (block= iguana_blockhashset(coin,-1,blockhashes[i],1)) != 0 && prev != 0 ) + { + //if ( prev->mainchain == 0 ) + // prev->hh.next = block; + /*if ( prev->hh.next == 0 && block->hh.prev == 0 ) + block->hh.prev = prev; + else if ( prev->hh.next == 0 && block->hh.prev == prev ) + prev->hh.next = block; + else if ( prev->hh.next == block && block->hh.prev == prev ) + { + if ( 0 && i < coin->chain->bundlesize ) + { + if ( iguana_bundlehash2add(coin,0,bp,i,blockhashes[i]) < 0 ) + { + if ( prev->mainchain == 0 ) + block->hh.prev = prev->hh.next = 0; + memset(bp->hashes[i].bytes,0,sizeof(bp->hashes[i])); + } + } + else if ( 0 && bp->bundleheight + coin->chain->bundlesize >= coin->bundlescount*coin->chain->bundlesize ) + { + char str[65]; printf("AUTOCREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,blockhashes[i])); + iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,blockhashes[i]); + for (j=2; jmainchain == 0 ) + block->hh.prev = prev->hh.next = 0; + } + //if ( (i % coin->chain->bundlesize) <= 1 ) + // iguana_blockQ(coin,0,-1,blockhashes[i],1); + //else //if ( bp != 0 && i < bp->n && bp->requests[i] == 0 ) + // iguana_blockQ(coin,0,-1,blockhashes[i],0); + } + prev = block; + }*/ + + int32_t iguana_ROmapchain(uint32_t *numtxidsp,uint32_t *numunspentsp,uint32_t *numspendsp,uint32_t *numpkindsp,uint32_t *numexternaltxidsp,struct iguana_ramchain *mapchain,void *ptr,long filesize,long fpos,bits256 firsthash2,bits256 lasthash2,int32_t height,int32_t numblocks,int32_t hdrsi,int32_t bundlei) + { + int32_t firsti = 1; + mapchain->fileptr = ptr; + mapchain->filesize = filesize; + mapchain->H.data = (void *)((long)ptr + fpos); + mapchain->H.ROflag = 1; + if ( iguana_ramchain_size(mapchain) != mapchain->H.data->allocsize || fpos+mapchain->H.data->allocsize > filesize ) + { + printf("iguana_bundlesaveHT size mismatch %ld vs %ld vs filesize.%ld\n",(long)iguana_ramchain_size(mapchain),(long)mapchain->H.data->allocsize,(long)filesize); + return(-1); + } + else if ( memcmp(firsthash2.bytes,mapchain->H.data->firsthash2.bytes,sizeof(bits256)) != 0 ) + { + char str[65],str2[65]; printf("iguana_bundlesaveHT hash2 mismatch %s vs %s\n",bits256_str(str,firsthash2),bits256_str(str2,mapchain->H.data->firsthash2)); + return(-1); + } + iguana_ramchain_link(mapchain,firsthash2,lasthash2,hdrsi,height,bundlei,1,firsti,1); + *numtxidsp += mapchain->H.data->numtxids; + *numunspentsp += mapchain->H.data->numunspents; + *numspendsp += mapchain->H.data->numspends; + if( mapchain->H.data->numpkinds != 0 ) + *numpkindsp += mapchain->H.data->numpkinds; + else *numpkindsp += mapchain->H.data->numunspents; + if( mapchain->H.data->numexternaltxids != 0 ) + *numexternaltxidsp += mapchain->H.data->numspends; + else *numexternaltxidsp += mapchain->H.data->numspends; + //printf("(%d %d %d) ",numtxids,numunspents,numspends); + //printf("%d ",numtxids); + return(0); + } + + bits256 iguana_lhashcalc(struct iguana_info *coin,struct iguana_ramchaindata *rdata,RAMCHAIN_FUNC) + { + bits256 sha256; + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_TXIDS].bytes,(uint8_t *)T,sizeof(struct iguana_txid)*rdata->numtxids); + if ( ramchain->expanded != 0 ) + { + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_UNSPENTS].bytes,(uint8_t *)Ux,sizeof(struct iguana_unspent)*rdata->numunspents); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_SPENDS].bytes,(uint8_t *)Sx,sizeof(struct iguana_spend)*rdata->numspends); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_PKHASHES].bytes,(uint8_t *)P,sizeof(struct iguana_pkhash)*rdata->numpkinds); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_SPENTINDS].bytes,(uint8_t *)U2,sizeof(struct iguana_Uextra)*rdata->numunspents); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_FIRSTSPENDS].bytes,(uint8_t *)P2,sizeof(struct iguana_pkextra)*rdata->numpkinds); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_ACCOUNTS].bytes,(uint8_t *)A,sizeof(struct iguana_account)*rdata->numpkinds); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_EXTERNALS].bytes,(uint8_t *)X,sizeof(bits256)*rdata->numexternaltxids); + } + else + { + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_UNSPENTS].bytes,(uint8_t *)U,sizeof(struct iguana_unspent20)*rdata->numunspents); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_SPENDS].bytes,(uint8_t *)S,sizeof(struct iguana_spend256)*rdata->numspends); + } + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_TXBITS].bytes,TXbits,(int32_t)hconv_bitlen(rdata->numtxsparse*rdata->txsparsebits)); + vcalc_sha256(0,rdata->lhashes[IGUANA_LHASH_PKBITS].bytes,PKbits,(int32_t)hconv_bitlen(rdata->numpksparse*rdata->pksparsebits)); + memset(&rdata->sha256,0,sizeof(rdata->sha256)); + vcalc_sha256(0,sha256.bytes,(void *)rdata,sizeof(*rdata)); + } + + /*struct iguana_prevdep + { + double PoW; // yes I know this is not consensus safe, it is used only for approximations locally + uint64_t supply; + uint32_t numtxids,numunspents,numspends,numpkinds; + } __attribute__((packed)); + + struct iguanakv + { + char name[63],fname[512],threadsafe; FILE *fp; + portable_mutex_t KVmutex,MMlock,MMmutex; + void *HDDitems,*HDDitems2,*HDDitems3,**HDDitemsp,**HDDitems2p,**HDDitems3p; // linear array of HDDitems; + struct iguana_kvitem *hashtables[0x100]; // of HDDitems + struct iguana_mappedptr M,M2,M3; + struct iguana_memspace HASHPTRS;//,MEM; + double mult; + uint64_t updated; + int32_t keysize,keyoffset,RAMvaluesize,HDDvaluesize,valuesize2,valuesize3; + int32_t numkeys,dispflag,flags,incr,numitems,numvalid,maxitemind; + uint32_t iteruarg; int32_t iterarg; + uint8_t *space; + };*/ + rdata->txsparsebits = hcalc_bitsize(numtxids); + rdata->numtxsparse = SPARSECOUNT(numtxids); + rdata->pksparsebits = hcalc_bitsize(numpkinds); + rdata->numpksparse = SPARSECOUNT(numpkinds); + rdata->Toffset = offset, offset += (sizeof(struct iguana_txid) * numtxids); + if ( ramchain->expanded != 0 ) + { + rdata->Uoffset = offset, offset += (sizeof(struct iguana_unspent) * numunspents); + rdata->Soffset = offset, offset += (sizeof(struct iguana_spend) * numspends); + rdata->Poffset = offset, offset += (sizeof(struct iguana_pkhash) * numpkinds); + rdata->U2offset = offset, offset += (sizeof(struct iguana_Uextra) * numunspents); + rdata->P2offset = offset, offset += (sizeof(struct iguana_pkextra) * numpkinds); + rdata->Aoffset = offset, offset += (sizeof(struct iguana_account) * numpkinds); + rdata->Xoffset = offset, offset += (sizeof(bits256) * numexternaltxids); + } + else + { + rdata->Uoffset = offset, offset += (sizeof(struct iguana_unspent20) * numunspents); + rdata->Soffset = offset, offset += (sizeof(struct iguana_spend256) * numspends); + } + + rdata->TXoffset = offset, offset += (((int64_t)rdata->numtxsparse*rdata->txsparsebits)/8 + 1); + rdata->PKoffset = offset, offset += (((int64_t)rdata->numpksparse*rdata->pksparsebits)/8 + 1); + + + tmp = *rdata; + fpos = ftell(fp); + iguana_rdata_action(0,0,&trunc,0,rdata,expanded,numtxids,numunspents,numspends,numpkinds,numexternaltxids,0,0,0,0); + + offset = sizeof(*rdata); + rdata->Toffset = offset, offset += (sizeof(struct iguana_txid) * rdata->numtxids); + if ( ramchain->expanded != 0 ) + { + rdata->Uoffset = offset, offset += (sizeof(struct iguana_unspent) * rdata->numunspents); + rdata->Soffset = offset, offset += (sizeof(struct iguana_spend) * rdata->numspends); + rdata->Poffset = offset, offset += (sizeof(struct iguana_pkhash) * rdata->numpkinds); + rdata->U2offset = offset, offset += (sizeof(struct iguana_Uextra) * rdata->numunspents); + rdata->P2offset = offset, offset += (sizeof(struct iguana_pkextra) * rdata->numpkinds); + rdata->Aoffset = offset, offset += (sizeof(struct iguana_account) * rdata->numpkinds); + rdata->Xoffset = offset, offset += (sizeof(bits256) * rdata->numexternaltxids); + } + else + { + rdata->Uoffset = offset, offset += (sizeof(struct iguana_unspent20) * rdata->numunspents); + rdata->Soffset = offset, offset += (sizeof(struct iguana_spend256) * rdata->numspends); + } + rdata->TXoffset = offset, offset += (((int64_t)rdata->numtxsparse*rdata->txsparsebits)/8 + 1); + rdata->PKoffset = offset, offset += (((int64_t)rdata->numpksparse*rdata->pksparsebits)/8 + 1); + rdata->allocsize = offset; + rdata->sha256 = iguana_lhashcalc(coin,rdata,RAMCHAIN_ARGS); + if ( iguana_ramchain_size(ramchain) != offset ) + printf("iguana_ramchain_size %ld vs %ld\n",(long)iguana_ramchain_size(ramchain),(long)offset), getchar(); + rdata->sha256 = sha256 = iguana_lhashcalc(coin,rdata,RAMCHAIN_ARG); + fwrite(rdata,1,sizeof(*rdata),fp); + *rdata = tmp; + fwrite(T,sizeof(struct iguana_txid),rdata->numtxids,fp); + if ( ramchain->expanded != 0 ) + { + fwrite(Ux,sizeof(struct iguana_unspent),rdata->numunspents,fp); + fwrite(Sx,sizeof(struct iguana_spend),rdata->numspends,fp); + fwrite(P,sizeof(struct iguana_pkhash),rdata->numpkinds,fp); + fwrite(U2,sizeof(struct iguana_Uextra),rdata->numunspents,fp); + fwrite(P2,sizeof(struct iguana_pkextra),rdata->numpkinds,fp); + fwrite(A,sizeof(struct iguana_account),rdata->numpkinds,fp); + fwrite(X,sizeof(bits256),rdata->numexternaltxids,fp); + //printf("iguana_ramchain_save.(%s): (%ld - %ld) diff.%ld vs %ld [%ld]\n",fname,ftell(fp),(long)fpos,(long)(ftell(fp) - fpos),(long)rdata->allocsize,(long)(ftell(fp) - fpos) - (long)rdata->allocsize); + } + else + { + fwrite(U,sizeof(struct iguana_unspent20),rdata->numunspents,fp); + fwrite(S,sizeof(struct iguana_spend256),rdata->numspends,fp); + } + fwrite(TXbits,1,((int64_t)rdata->numtxsparse*rdata->txsparsebits)/8 + 1,fp); + fwrite(PKbits,1,((int64_t)rdata->numpksparse*rdata->pksparsebits)/8 + 1,fp); + if ( (ftell(fp) - fpos) != rdata->allocsize ) + { + printf("(ftell.%ld - fpos.%ld) %ld vs %ld\n",ftell(fp),fpos,ftell(fp)-fpos,(long)rdata->allocsize); + fpos = -1; + } + //int32_t i; char str[65]; + //for (i=0; inumexternaltxids; i++) + // printf("X[%d] %s\n",i,bits256_str(str,X[i])); + uint32_t iguana_updatescript(struct iguana_info *coin,uint32_t blocknum,uint32_t txidind,uint32_t spendind,uint32_t unspentind,uint64_t value,uint8_t *script,int32_t scriptlen,uint32_t sequence) + { + return(0); + } + + function httpGet(theUrl)\ + {\ + var xmlhttp;\ + if ( window.XMLHttpRequest )\ + xmlhttp = new XMLHttpRequest();\ + else\ + xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\ + xmlhttp.onreadystatechange = function()\ + {\ + if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 )\ + {\ + createDiv(xmlhttp.responseText);\ + }\ + }\ + xmlhttp.open(\"GET\", theUrl, false);\ + xmlhttp.send(null);\ + }\ + var jsonstr = httpGet(\"http://127.0.0.1:7778/json/bitmap\"); \ + struct iguana_bundlereq *iguana_recvblock(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundlereq *req,struct iguana_block *origblock,int32_t numtx,int32_t datalen,int32_t recvlen,int32_t *newhwmp) + { + struct iguana_bundle *bp=0; int32_t bundlei = -2; struct iguana_block *block; double duration; + bp = iguana_bundleset(coin,&block,&bundlei,origblock); + if ( block != 0 ) + { + block->RO.recvlen = recvlen; + block->ipbits = req->ipbits; + if ( bp == 0 && req->copyflag != 0 && block->rawdata == 0 ) + { + char str[65]; printf("%s copyflag.%d %d data %d %p\n",bits256_str(str,block->RO.hash2),req->copyflag,block->height,req->recvlen,bp); + block->rawdata = mycalloc('n',1,block->RO.recvlen); + memcpy(block->rawdata,req->serialized,block->RO.recvlen); + block->copyflag = 1; + } + //printf("datalen.%d ipbits.%x\n",datalen,req->ipbits); + } else printf("cant create block.%llx block.%p bp.%p bundlei.%d\n",(long long)origblock->RO.hash2.txid,block,bp,bundlei); + if ( bp != 0 && bundlei >= 0 ) + { + //bp->ipbits[bundlei] = block->ipbits; + if ( 0 && bp->requests[bundlei] > 2 ) + printf("recv bundlei.%d hdrs.%d reqs.[%d] fpos.%d datalen.%d recvlen.(%d %d) ipbits.(%x %x %x)\n",bundlei,bp->hdrsi,bp->requests[bundlei],bp->fpos[bundlei],datalen,block->RO.recvlen,req->recvlen,block->ipbits,bp->ipbits[bundlei],req->ipbits); + if ( recvlen > 0 ) + { + SETBIT(bp->recv,bundlei); + if ( bp->issued[bundlei] > 0 ) + { + bp->durationsum += (int32_t)(time(NULL) - bp->issued[bundlei]); + bp->durationcount++; + if ( duration < bp->avetime/10. ) + duration = bp->avetime/10.; + else if ( duration > bp->avetime*10. ) + duration = bp->avetime * 10.; + dxblend(&bp->avetime,duration,.99); + dxblend(&coin->avetime,bp->avetime,.9); + } + } + if ( 0 && strcmp(coin->symbol,"BTC") != 0 && bundlei < coin->chain->bundlesize-1 && bits256_nonz(bp->hashes[bundlei+1]) != 0 && bp->fpos[bundlei+1] < 0 ) + iguana_blockQ(coin,bp,bundlei+1,bp->hashes[bundlei+1],0); + } + if ( 0 && block != 0 && strcmp(coin->symbol,"BTC") != 0 ) + { + if ( (bp = iguana_bundlefind(coin,&bp,&bundlei,block->RO.prev_block)) != 0 ) + { + if ( bp->fpos[bundlei] < 0 ) + iguana_blockQ(coin,bp,bundlei,block->RO.prev_block,0); + } + } + return(req); + } + struct iguana_bundle *iguana_bundleset(struct iguana_info *coin,struct iguana_block **blockp,int32_t *bundleip,struct iguana_block *origblock) + { + struct iguana_block *block; bits256 zero,*hashes; struct iguana_bundle *bp = 0; + int32_t bundlei = -2; + *bundleip = -2; *blockp = 0; + if ( origblock == 0 ) + return(0); + memset(zero.bytes,0,sizeof(zero)); + if ( (block= iguana_blockhashset(coin,-1,origblock->RO.hash2,1)) != 0 ) + { + if ( block != origblock ) + iguana_blockcopy(coin,block,origblock); + *blockp = block; + if ( bits256_nonz(block->RO.prev_block) > 0 ) + iguana_patch(coin,block); + if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,block->RO.hash2)) != 0 ) + { + if ( bundlei < coin->chain->bundlesize ) + { + block->bundlei = bundlei; + block->hdrsi = bp->hdrsi; + //iguana_hash2set(coin,"blockadd",bp,block->bundlei,block->hash2); + iguana_bundlehash2add(coin,0,bp,bundlei,block->RO.hash2); + if ( bundlei == 0 ) + { + if ( bp->hdrsi > 0 && (bp= coin->bundles[bp->hdrsi-1]) != 0 ) + { + //printf("add to prev hdrs.%d\n",bp->hdrsi); + iguana_bundlehash2add(coin,0,bp,coin->chain->bundlesize-1,block->RO.prev_block); + if ( 0 && bp->fpos[coin->chain->bundlesize-1] < 0 && strcmp(coin->symbol,"BTC") != 0 ) + iguana_blockQ(coin,bp,coin->chain->bundlesize-1,block->RO.prev_block,0); + } + } + else + { + //printf("prev issue.%d\n",bp->bundleheight+bundlei-1); + iguana_bundlehash2add(coin,0,bp,bundlei-1,block->RO.prev_block); + if ( 0 && bp->fpos[bundlei-1] < 0 && strcmp(coin->symbol,"BTC") != 0 ) + iguana_blockQ(coin,bp,bundlei-1,block->RO.prev_block,0); + } + } + } + if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,block->RO.prev_block)) != 0 ) + { + //printf("found prev.%d\n",bp->bundleheight+bundlei); + if ( bundlei < coin->chain->bundlesize ) + { + if ( bundlei == coin->chain->bundlesize-1 ) + { + if ( coin->bundlescount < bp->hdrsi+1 ) + { + char str[65]; printf("autoextend CREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,block->RO.hash2)); + iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,block->RO.hash2,zero); + } + } + else if ( bundlei < coin->chain->bundlesize-1 ) + { + block->bundlei = bundlei + 1; + block->hdrsi = bp->hdrsi; + iguana_bundlehash2add(coin,0,bp,bundlei+1,block->RO.hash2); + } + } + } + //char str[65]; printf("iguana_recvblock (%s) %d %d[%d] %p\n",bits256_str(str,block->hash2),block->havebundle,block->hdrsi,bundlei,bp); + } + return(iguana_bundlefind(coin,&bp,bundleip,origblock->RO.hash2)); + } + int32_t iguana_bundlemode(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei) + { + if ( bp->ipbits[bundlei] == 0 ) + return(-1); + else if ( bp->emitfinish > coin->starttime ) + { + if ( bp->ramchain.numblocks == bp->n ) + return(1); + else return(2); + } + else return(0); + } +#endif diff --git a/iguana/Makefile b/iguana/Makefile new file mode 100644 index 000000000..a3af0967a --- /dev/null +++ b/iguana/Makefile @@ -0,0 +1,56 @@ +# Copyright (c) 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# GNU Makefile based on shared rules provided by the Native Client SDK. +# See README.Makefiles for more details. + +VALID_TOOLCHAINS := pnacl newlib glibc clang-newlib mac + +NACL_SDK_ROOT ?= $(abspath $(CURDIR)) + +TARGET = iguana +TARGETI = InstantDEX +TARGETP = pangea +TARGETX = PAX + +EXTRA= -D__PNACL + +include $(NACL_SDK_ROOT)/tools/common.mk + +CHROME_ARGS += --allow-nacl-socket-api=127.0.0.1 + +DEPS = nacl_io +LIBS = crypto777 curl ssl crypto z glibc-compat nacl_spawn ppapi nacl_io ppapi_simple # cli_main ppapi_cpp ppapi_simple + +CFLAGS = -Wall -D__PNACL -fno-strict-aliasing $(EXTRA) +LFLAGS = libs + +SOURCES = main.c iguana777.c iguana_init.c iguana_json.c iguana_recv.c iguana_chains.c iguana_ramchain.c iguana_bitmap.c iguana_rpc.c iguana_bundles.c iguana_pubkeys.c iguana_msg.c iguana_html.c iguana_blocks.c iguana_peers.c + +SOURCESI = InstantDEX/main.c InstantDEX/InstantDEX_main.c InstantDEX/prices777.c + +SOURCESP = pangea/main.c pangea/cards777.c pangea/pangea777.c pangea/pangeafunds.c pangea/poker.c pangea/tourney777.c + +SOURCESX = peggy/main.c peggy/peggy777.c peggy/peggytx.c peggy/txidind777.c peggy/opreturn777.c quotes777.c + +# Build rules generated by macros from common.mk: + +$(foreach dep,$(DEPS),$(eval $(call DEPEND_RULE,$(dep)))) + +$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) + +# The PNaCl workflow uses both an unstripped and finalized/stripped binary. +# On NaCl, only produce a stripped binary for Release configs (not Debug). +ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) + +$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES) $(LOCALLIBS),$(LIBS),$(DEPS))); +$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) + +else +$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) + +endif + +$(eval $(call NMF_RULE,$(TARGET),)) + diff --git a/iguana/css/bootstrap.css b/iguana/css/bootstrap.css new file mode 100755 index 000000000..cd1c616ad --- /dev/null +++ b/iguana/css/bootstrap.css @@ -0,0 +1,5 @@ +/*! + * Bootstrap v3.3.4 (http://getbootstrap.com) + * Copyright 2011-2015 Twitter, Inc. + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + *//*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{margin:.67em 0;font-size:2em}mark{color:#000;background:#ff0}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{height:0;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{margin:0;font:inherit;color:inherit}button{overflow:visible}button,select{text-transform:none}button,html input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}input{line-height:normal}input[type=checkbox],input[type=radio]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:0}input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{height:auto}input[type=search]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}fieldset{padding:.35em .625em .75em;margin:0 2px;border:1px solid silver}legend{padding:0;border:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-spacing:0;border-collapse:collapse}td,th{padding:0}/*! Source: https://github.com/h5bp/html5-boilerplate/blob/master/src/css/main.css */@media print{*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}a,a:visited{text-decoration:underline}a[href]:after{content:" (" attr(href) ")"}abbr[title]:after{content:" (" attr(title) ")"}a[href^="javascript:"]:after,a[href^="#"]:after{content:""}blockquote,pre{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}img,tr{page-break-inside:avoid}img{max-width:100%!important}h2,h3,p{orphans:3;widows:3}h2,h3{page-break-after:avoid}select{background:#fff!important}.navbar{display:none}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table{border-collapse:collapse!important}.table td,.table th{background-color:#fff!important}.table-bordered td,.table-bordered th{border:1px solid #ddd!important}}@font-face{font-family:'Glyphicons Halflings';src:url(../fonts/glyphicons-halflings-regular.eot);src:url(../fonts/glyphicons-halflings-regular.eot?#iefix) format('embedded-opentype'),url(../fonts/glyphicons-halflings-regular.woff2) format('woff2'),url(../fonts/glyphicons-halflings-regular.woff) format('woff'),url(../fonts/glyphicons-halflings-regular.ttf) format('truetype'),url(../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular) format('svg')}.glyphicon{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:400;line-height:1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.glyphicon-asterisk:before{content:"\2a"}.glyphicon-plus:before{content:"\2b"}.glyphicon-eur:before,.glyphicon-euro:before{content:"\20ac"}.glyphicon-minus:before{content:"\2212"}.glyphicon-cloud:before{content:"\2601"}.glyphicon-envelope:before{content:"\2709"}.glyphicon-pencil:before{content:"\270f"}.glyphicon-glass:before{content:"\e001"}.glyphicon-music:before{content:"\e002"}.glyphicon-search:before{content:"\e003"}.glyphicon-heart:before{content:"\e005"}.glyphicon-star:before{content:"\e006"}.glyphicon-star-empty:before{content:"\e007"}.glyphicon-user:before{content:"\e008"}.glyphicon-film:before{content:"\e009"}.glyphicon-th-large:before{content:"\e010"}.glyphicon-th:before{content:"\e011"}.glyphicon-th-list:before{content:"\e012"}.glyphicon-ok:before{content:"\e013"}.glyphicon-remove:before{content:"\e014"}.glyphicon-zoom-in:before{content:"\e015"}.glyphicon-zoom-out:before{content:"\e016"}.glyphicon-off:before{content:"\e017"}.glyphicon-signal:before{content:"\e018"}.glyphicon-cog:before{content:"\e019"}.glyphicon-trash:before{content:"\e020"}.glyphicon-home:before{content:"\e021"}.glyphicon-file:before{content:"\e022"}.glyphicon-time:before{content:"\e023"}.glyphicon-road:before{content:"\e024"}.glyphicon-download-alt:before{content:"\e025"}.glyphicon-download:before{content:"\e026"}.glyphicon-upload:before{content:"\e027"}.glyphicon-inbox:before{content:"\e028"}.glyphicon-play-circle:before{content:"\e029"}.glyphicon-repeat:before{content:"\e030"}.glyphicon-refresh:before{content:"\e031"}.glyphicon-list-alt:before{content:"\e032"}.glyphicon-lock:before{content:"\e033"}.glyphicon-flag:before{content:"\e034"}.glyphicon-headphones:before{content:"\e035"}.glyphicon-volume-off:before{content:"\e036"}.glyphicon-volume-down:before{content:"\e037"}.glyphicon-volume-up:before{content:"\e038"}.glyphicon-qrcode:before{content:"\e039"}.glyphicon-barcode:before{content:"\e040"}.glyphicon-tag:before{content:"\e041"}.glyphicon-tags:before{content:"\e042"}.glyphicon-book:before{content:"\e043"}.glyphicon-bookmark:before{content:"\e044"}.glyphicon-print:before{content:"\e045"}.glyphicon-camera:before{content:"\e046"}.glyphicon-font:before{content:"\e047"}.glyphicon-bold:before{content:"\e048"}.glyphicon-italic:before{content:"\e049"}.glyphicon-text-height:before{content:"\e050"}.glyphicon-text-width:before{content:"\e051"}.glyphicon-align-left:before{content:"\e052"}.glyphicon-align-center:before{content:"\e053"}.glyphicon-align-right:before{content:"\e054"}.glyphicon-align-justify:before{content:"\e055"}.glyphicon-list:before{content:"\e056"}.glyphicon-indent-left:before{content:"\e057"}.glyphicon-indent-right:before{content:"\e058"}.glyphicon-facetime-video:before{content:"\e059"}.glyphicon-picture:before{content:"\e060"}.glyphicon-map-marker:before{content:"\e062"}.glyphicon-adjust:before{content:"\e063"}.glyphicon-tint:before{content:"\e064"}.glyphicon-edit:before{content:"\e065"}.glyphicon-share:before{content:"\e066"}.glyphicon-check:before{content:"\e067"}.glyphicon-move:before{content:"\e068"}.glyphicon-step-backward:before{content:"\e069"}.glyphicon-fast-backward:before{content:"\e070"}.glyphicon-backward:before{content:"\e071"}.glyphicon-play:before{content:"\e072"}.glyphicon-pause:before{content:"\e073"}.glyphicon-stop:before{content:"\e074"}.glyphicon-forward:before{content:"\e075"}.glyphicon-fast-forward:before{content:"\e076"}.glyphicon-step-forward:before{content:"\e077"}.glyphicon-eject:before{content:"\e078"}.glyphicon-chevron-left:before{content:"\e079"}.glyphicon-chevron-right:before{content:"\e080"}.glyphicon-plus-sign:before{content:"\e081"}.glyphicon-minus-sign:before{content:"\e082"}.glyphicon-remove-sign:before{content:"\e083"}.glyphicon-ok-sign:before{content:"\e084"}.glyphicon-question-sign:before{content:"\e085"}.glyphicon-info-sign:before{content:"\e086"}.glyphicon-screenshot:before{content:"\e087"}.glyphicon-remove-circle:before{content:"\e088"}.glyphicon-ok-circle:before{content:"\e089"}.glyphicon-ban-circle:before{content:"\e090"}.glyphicon-arrow-left:before{content:"\e091"}.glyphicon-arrow-right:before{content:"\e092"}.glyphicon-arrow-up:before{content:"\e093"}.glyphicon-arrow-down:before{content:"\e094"}.glyphicon-share-alt:before{content:"\e095"}.glyphicon-resize-full:before{content:"\e096"}.glyphicon-resize-small:before{content:"\e097"}.glyphicon-exclamation-sign:before{content:"\e101"}.glyphicon-gift:before{content:"\e102"}.glyphicon-leaf:before{content:"\e103"}.glyphicon-fire:before{content:"\e104"}.glyphicon-eye-open:before{content:"\e105"}.glyphicon-eye-close:before{content:"\e106"}.glyphicon-warning-sign:before{content:"\e107"}.glyphicon-plane:before{content:"\e108"}.glyphicon-calendar:before{content:"\e109"}.glyphicon-random:before{content:"\e110"}.glyphicon-comment:before{content:"\e111"}.glyphicon-magnet:before{content:"\e112"}.glyphicon-chevron-up:before{content:"\e113"}.glyphicon-chevron-down:before{content:"\e114"}.glyphicon-retweet:before{content:"\e115"}.glyphicon-shopping-cart:before{content:"\e116"}.glyphicon-folder-close:before{content:"\e117"}.glyphicon-folder-open:before{content:"\e118"}.glyphicon-resize-vertical:before{content:"\e119"}.glyphicon-resize-horizontal:before{content:"\e120"}.glyphicon-hdd:before{content:"\e121"}.glyphicon-bullhorn:before{content:"\e122"}.glyphicon-bell:before{content:"\e123"}.glyphicon-certificate:before{content:"\e124"}.glyphicon-thumbs-up:before{content:"\e125"}.glyphicon-thumbs-down:before{content:"\e126"}.glyphicon-hand-right:before{content:"\e127"}.glyphicon-hand-left:before{content:"\e128"}.glyphicon-hand-up:before{content:"\e129"}.glyphicon-hand-down:before{content:"\e130"}.glyphicon-circle-arrow-right:before{content:"\e131"}.glyphicon-circle-arrow-left:before{content:"\e132"}.glyphicon-circle-arrow-up:before{content:"\e133"}.glyphicon-circle-arrow-down:before{content:"\e134"}.glyphicon-globe:before{content:"\e135"}.glyphicon-wrench:before{content:"\e136"}.glyphicon-tasks:before{content:"\e137"}.glyphicon-filter:before{content:"\e138"}.glyphicon-briefcase:before{content:"\e139"}.glyphicon-fullscreen:before{content:"\e140"}.glyphicon-dashboard:before{content:"\e141"}.glyphicon-paperclip:before{content:"\e142"}.glyphicon-heart-empty:before{content:"\e143"}.glyphicon-link:before{content:"\e144"}.glyphicon-phone:before{content:"\e145"}.glyphicon-pushpin:before{content:"\e146"}.glyphicon-usd:before{content:"\e148"}.glyphicon-gbp:before{content:"\e149"}.glyphicon-sort:before{content:"\e150"}.glyphicon-sort-by-alphabet:before{content:"\e151"}.glyphicon-sort-by-alphabet-alt:before{content:"\e152"}.glyphicon-sort-by-order:before{content:"\e153"}.glyphicon-sort-by-order-alt:before{content:"\e154"}.glyphicon-sort-by-attributes:before{content:"\e155"}.glyphicon-sort-by-attributes-alt:before{content:"\e156"}.glyphicon-unchecked:before{content:"\e157"}.glyphicon-expand:before{content:"\e158"}.glyphicon-collapse-down:before{content:"\e159"}.glyphicon-collapse-up:before{content:"\e160"}.glyphicon-log-in:before{content:"\e161"}.glyphicon-flash:before{content:"\e162"}.glyphicon-log-out:before{content:"\e163"}.glyphicon-new-window:before{content:"\e164"}.glyphicon-record:before{content:"\e165"}.glyphicon-save:before{content:"\e166"}.glyphicon-open:before{content:"\e167"}.glyphicon-saved:before{content:"\e168"}.glyphicon-import:before{content:"\e169"}.glyphicon-export:before{content:"\e170"}.glyphicon-send:before{content:"\e171"}.glyphicon-floppy-disk:before{content:"\e172"}.glyphicon-floppy-saved:before{content:"\e173"}.glyphicon-floppy-remove:before{content:"\e174"}.glyphicon-floppy-save:before{content:"\e175"}.glyphicon-floppy-open:before{content:"\e176"}.glyphicon-credit-card:before{content:"\e177"}.glyphicon-transfer:before{content:"\e178"}.glyphicon-cutlery:before{content:"\e179"}.glyphicon-header:before{content:"\e180"}.glyphicon-compressed:before{content:"\e181"}.glyphicon-earphone:before{content:"\e182"}.glyphicon-phone-alt:before{content:"\e183"}.glyphicon-tower:before{content:"\e184"}.glyphicon-stats:before{content:"\e185"}.glyphicon-sd-video:before{content:"\e186"}.glyphicon-hd-video:before{content:"\e187"}.glyphicon-subtitles:before{content:"\e188"}.glyphicon-sound-stereo:before{content:"\e189"}.glyphicon-sound-dolby:before{content:"\e190"}.glyphicon-sound-5-1:before{content:"\e191"}.glyphicon-sound-6-1:before{content:"\e192"}.glyphicon-sound-7-1:before{content:"\e193"}.glyphicon-copyright-mark:before{content:"\e194"}.glyphicon-registration-mark:before{content:"\e195"}.glyphicon-cloud-download:before{content:"\e197"}.glyphicon-cloud-upload:before{content:"\e198"}.glyphicon-tree-conifer:before{content:"\e199"}.glyphicon-tree-deciduous:before{content:"\e200"}.glyphicon-cd:before{content:"\e201"}.glyphicon-save-file:before{content:"\e202"}.glyphicon-open-file:before{content:"\e203"}.glyphicon-level-up:before{content:"\e204"}.glyphicon-copy:before{content:"\e205"}.glyphicon-paste:before{content:"\e206"}.glyphicon-alert:before{content:"\e209"}.glyphicon-equalizer:before{content:"\e210"}.glyphicon-king:before{content:"\e211"}.glyphicon-queen:before{content:"\e212"}.glyphicon-pawn:before{content:"\e213"}.glyphicon-bishop:before{content:"\e214"}.glyphicon-knight:before{content:"\e215"}.glyphicon-baby-formula:before{content:"\e216"}.glyphicon-tent:before{content:"\26fa"}.glyphicon-blackboard:before{content:"\e218"}.glyphicon-bed:before{content:"\e219"}.glyphicon-apple:before{content:"\f8ff"}.glyphicon-erase:before{content:"\e221"}.glyphicon-hourglass:before{content:"\231b"}.glyphicon-lamp:before{content:"\e223"}.glyphicon-duplicate:before{content:"\e224"}.glyphicon-piggy-bank:before{content:"\e225"}.glyphicon-scissors:before{content:"\e226"}.glyphicon-bitcoin:before{content:"\e227"}.glyphicon-btc:before{content:"\e227"}.glyphicon-xbt:before{content:"\e227"}.glyphicon-yen:before{content:"\00a5"}.glyphicon-jpy:before{content:"\00a5"}.glyphicon-ruble:before{content:"\20bd"}.glyphicon-rub:before{content:"\20bd"}.glyphicon-scale:before{content:"\e230"}.glyphicon-ice-lolly:before{content:"\e231"}.glyphicon-ice-lolly-tasted:before{content:"\e232"}.glyphicon-education:before{content:"\e233"}.glyphicon-option-horizontal:before{content:"\e234"}.glyphicon-option-vertical:before{content:"\e235"}.glyphicon-menu-hamburger:before{content:"\e236"}.glyphicon-modal-window:before{content:"\e237"}.glyphicon-oil:before{content:"\e238"}.glyphicon-grain:before{content:"\e239"}.glyphicon-sunglasses:before{content:"\e240"}.glyphicon-text-size:before{content:"\e241"}.glyphicon-text-color:before{content:"\e242"}.glyphicon-text-background:before{content:"\e243"}.glyphicon-object-align-top:before{content:"\e244"}.glyphicon-object-align-bottom:before{content:"\e245"}.glyphicon-object-align-horizontal:before{content:"\e246"}.glyphicon-object-align-left:before{content:"\e247"}.glyphicon-object-align-vertical:before{content:"\e248"}.glyphicon-object-align-right:before{content:"\e249"}.glyphicon-triangle-right:before{content:"\e250"}.glyphicon-triangle-left:before{content:"\e251"}.glyphicon-triangle-bottom:before{content:"\e252"}.glyphicon-triangle-top:before{content:"\e253"}.glyphicon-console:before{content:"\e254"}.glyphicon-superscript:before{content:"\e255"}.glyphicon-subscript:before{content:"\e256"}.glyphicon-menu-left:before{content:"\e257"}.glyphicon-menu-right:before{content:"\e258"}.glyphicon-menu-down:before{content:"\e259"}.glyphicon-menu-up:before{content:"\e260"}*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}:after,:before{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}html{font-size:10px;-webkit-tap-highlight-color:rgba(0,0,0,0)}body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;line-height:1.42857143;color:#333;background-color:#fff}button,input,select,textarea{font-family:inherit;font-size:inherit;line-height:inherit}a{color:#337ab7;text-decoration:none}a:focus,a:hover{color:#23527c;text-decoration:underline}a:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}figure{margin:0}img{vertical-align:middle}.carousel-inner>.item>a>img,.carousel-inner>.item>img,.img-responsive,.thumbnail a>img,.thumbnail>img{display:block;max-width:100%;height:auto}.img-rounded{border-radius:6px}.img-thumbnail{display:inline-block;max-width:100%;height:auto;padding:4px;line-height:1.42857143;background-color:#fff;border:1px solid #ddd;border-radius:4px;-webkit-transition:all .2s ease-in-out;-o-transition:all .2s ease-in-out;transition:all .2s ease-in-out}.img-circle{border-radius:50%}hr{margin-top:20px;margin-bottom:20px;border:0;border-top:1px solid #eee}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}.sr-only-focusable:active,.sr-only-focusable:focus{position:static;width:auto;height:auto;margin:0;overflow:visible;clip:auto}[role=button]{cursor:pointer}.h1,.h2,.h3,.h4,.h5,.h6,h1,h2,h3,h4,h5,h6{font-family:inherit;font-weight:500;line-height:1.1;color:inherit}.h1 .small,.h1 small,.h2 .small,.h2 small,.h3 .small,.h3 small,.h4 .small,.h4 small,.h5 .small,.h5 small,.h6 .small,.h6 small,h1 .small,h1 small,h2 .small,h2 small,h3 .small,h3 small,h4 .small,h4 small,h5 .small,h5 small,h6 .small,h6 small{font-weight:400;line-height:1;color:#777}.h1,.h2,.h3,h1,h2,h3{margin-top:20px;margin-bottom:10px}.h1 .small,.h1 small,.h2 .small,.h2 small,.h3 .small,.h3 small,h1 .small,h1 small,h2 .small,h2 small,h3 .small,h3 small{font-size:65%}.h4,.h5,.h6,h4,h5,h6{margin-top:10px;margin-bottom:10px}.h4 .small,.h4 small,.h5 .small,.h5 small,.h6 .small,.h6 small,h4 .small,h4 small,h5 .small,h5 small,h6 .small,h6 small{font-size:75%}.h1,h1{font-size:36px}.h2,h2{font-size:30px}.h3,h3{font-size:24px}.h4,h4{font-size:18px}.h5,h5{font-size:14px}.h6,h6{font-size:12px}p{margin:0 0 10px}.lead{margin-bottom:20px;font-size:16px;font-weight:300;line-height:1.4}@media (min-width:768px){.lead{font-size:21px}}.small,small{font-size:85%}.mark,mark{padding:.2em;background-color:#fcf8e3}.text-left{text-align:left}.text-right{text-align:right}.text-center{text-align:center}.text-justify{text-align:justify}.text-nowrap{white-space:nowrap}.text-lowercase{text-transform:lowercase}.text-uppercase{text-transform:uppercase}.text-capitalize{text-transform:capitalize}.text-muted{color:#777}.text-primary{color:#337ab7}a.text-primary:hover{color:#286090}.text-success{color:#3c763d}a.text-success:hover{color:#2b542c}.text-info{color:#31708f}a.text-info:hover{color:#245269}.text-warning{color:#8a6d3b}a.text-warning:hover{color:#66512c}.text-danger{color:#a94442}a.text-danger:hover{color:#843534}.bg-primary{color:#fff;background-color:#337ab7}a.bg-primary:hover{background-color:#286090}.bg-success{background-color:#dff0d8}a.bg-success:hover{background-color:#c1e2b3}.bg-info{background-color:#d9edf7}a.bg-info:hover{background-color:#afd9ee}.bg-warning{background-color:#fcf8e3}a.bg-warning:hover{background-color:#f7ecb5}.bg-danger{background-color:#f2dede}a.bg-danger:hover{background-color:#e4b9b9}.page-header{padding-bottom:9px;margin:40px 0 20px;border-bottom:1px solid #eee}ol,ul{margin-top:0;margin-bottom:10px}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}.list-unstyled{padding-left:0;list-style:none}.list-inline{padding-left:0;margin-left:-5px;list-style:none}.list-inline>li{display:inline-block;padding-right:5px;padding-left:5px}dl{margin-top:0;margin-bottom:20px}dd,dt{line-height:1.42857143}dt{font-weight:700}dd{margin-left:0}@media (min-width:768px){.dl-horizontal dt{float:left;width:160px;overflow:hidden;clear:left;text-align:right;text-overflow:ellipsis;white-space:nowrap}.dl-horizontal dd{margin-left:180px}}abbr[data-original-title],abbr[title]{cursor:help;border-bottom:1px dotted #777}.initialism{font-size:90%;text-transform:uppercase}blockquote{padding:10px 20px;margin:0 0 20px;font-size:17.5px;border-left:5px solid #eee}blockquote ol:last-child,blockquote p:last-child,blockquote ul:last-child{margin-bottom:0}blockquote .small,blockquote footer,blockquote small{display:block;font-size:80%;line-height:1.42857143;color:#777}blockquote .small:before,blockquote footer:before,blockquote small:before{content:'\2014 \00A0'}.blockquote-reverse,blockquote.pull-right{padding-right:15px;padding-left:0;text-align:right;border-right:5px solid #eee;border-left:0}.blockquote-reverse .small:before,.blockquote-reverse footer:before,.blockquote-reverse small:before,blockquote.pull-right .small:before,blockquote.pull-right footer:before,blockquote.pull-right small:before{content:''}.blockquote-reverse .small:after,.blockquote-reverse footer:after,.blockquote-reverse small:after,blockquote.pull-right .small:after,blockquote.pull-right footer:after,blockquote.pull-right small:after{content:'\00A0 \2014'}address{margin-bottom:20px;font-style:normal;line-height:1.42857143}code,kbd,pre,samp{font-family:Menlo,Monaco,Consolas,"Courier New",monospace}code{padding:2px 4px;font-size:90%;color:#c7254e;background-color:#f9f2f4;border-radius:4px}kbd{padding:2px 4px;font-size:90%;color:#fff;background-color:#333;border-radius:3px;-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,.25);box-shadow:inset 0 -1px 0 rgba(0,0,0,.25)}kbd kbd{padding:0;font-size:100%;font-weight:700;-webkit-box-shadow:none;box-shadow:none}pre{display:block;padding:9.5px;margin:0 0 10px;font-size:13px;line-height:1.42857143;color:#333;word-break:break-all;word-wrap:break-word;background-color:#f5f5f5;border:1px solid #ccc;border-radius:4px}pre code{padding:0;font-size:inherit;color:inherit;white-space:pre-wrap;background-color:transparent;border-radius:0}.pre-scrollable{max-height:340px;overflow-y:scroll}.container{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}@media (min-width:768px){.container{width:750px}}@media (min-width:992px){.container{width:970px}}@media (min-width:1200px){.container{width:1170px}}.container-fluid{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}.row{margin-right:-15px;margin-left:-15px}.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9{position:relative;min-height:1px;padding-right:15px;padding-left:15px}.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9{float:left}.col-xs-12{width:100%}.col-xs-11{width:91.66666667%}.col-xs-10{width:83.33333333%}.col-xs-9{width:75%}.col-xs-8{width:66.66666667%}.col-xs-7{width:58.33333333%}.col-xs-6{width:50%}.col-xs-5{width:41.66666667%}.col-xs-4{width:33.33333333%}.col-xs-3{width:25%}.col-xs-2{width:16.66666667%}.col-xs-1{width:8.33333333%}.col-xs-pull-12{right:100%}.col-xs-pull-11{right:91.66666667%}.col-xs-pull-10{right:83.33333333%}.col-xs-pull-9{right:75%}.col-xs-pull-8{right:66.66666667%}.col-xs-pull-7{right:58.33333333%}.col-xs-pull-6{right:50%}.col-xs-pull-5{right:41.66666667%}.col-xs-pull-4{right:33.33333333%}.col-xs-pull-3{right:25%}.col-xs-pull-2{right:16.66666667%}.col-xs-pull-1{right:8.33333333%}.col-xs-pull-0{right:auto}.col-xs-push-12{left:100%}.col-xs-push-11{left:91.66666667%}.col-xs-push-10{left:83.33333333%}.col-xs-push-9{left:75%}.col-xs-push-8{left:66.66666667%}.col-xs-push-7{left:58.33333333%}.col-xs-push-6{left:50%}.col-xs-push-5{left:41.66666667%}.col-xs-push-4{left:33.33333333%}.col-xs-push-3{left:25%}.col-xs-push-2{left:16.66666667%}.col-xs-push-1{left:8.33333333%}.col-xs-push-0{left:auto}.col-xs-offset-12{margin-left:100%}.col-xs-offset-11{margin-left:91.66666667%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-0{margin-left:0}@media (min-width:768px){.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9{float:left}.col-sm-12{width:100%}.col-sm-11{width:91.66666667%}.col-sm-10{width:83.33333333%}.col-sm-9{width:75%}.col-sm-8{width:66.66666667%}.col-sm-7{width:58.33333333%}.col-sm-6{width:50%}.col-sm-5{width:41.66666667%}.col-sm-4{width:33.33333333%}.col-sm-3{width:25%}.col-sm-2{width:16.66666667%}.col-sm-1{width:8.33333333%}.col-sm-pull-12{right:100%}.col-sm-pull-11{right:91.66666667%}.col-sm-pull-10{right:83.33333333%}.col-sm-pull-9{right:75%}.col-sm-pull-8{right:66.66666667%}.col-sm-pull-7{right:58.33333333%}.col-sm-pull-6{right:50%}.col-sm-pull-5{right:41.66666667%}.col-sm-pull-4{right:33.33333333%}.col-sm-pull-3{right:25%}.col-sm-pull-2{right:16.66666667%}.col-sm-pull-1{right:8.33333333%}.col-sm-pull-0{right:auto}.col-sm-push-12{left:100%}.col-sm-push-11{left:91.66666667%}.col-sm-push-10{left:83.33333333%}.col-sm-push-9{left:75%}.col-sm-push-8{left:66.66666667%}.col-sm-push-7{left:58.33333333%}.col-sm-push-6{left:50%}.col-sm-push-5{left:41.66666667%}.col-sm-push-4{left:33.33333333%}.col-sm-push-3{left:25%}.col-sm-push-2{left:16.66666667%}.col-sm-push-1{left:8.33333333%}.col-sm-push-0{left:auto}.col-sm-offset-12{margin-left:100%}.col-sm-offset-11{margin-left:91.66666667%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-0{margin-left:0}}@media (min-width:992px){.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9{float:left}.col-md-12{width:100%}.col-md-11{width:91.66666667%}.col-md-10{width:83.33333333%}.col-md-9{width:75%}.col-md-8{width:66.66666667%}.col-md-7{width:58.33333333%}.col-md-6{width:50%}.col-md-5{width:41.66666667%}.col-md-4{width:33.33333333%}.col-md-3{width:25%}.col-md-2{width:16.66666667%}.col-md-1{width:8.33333333%}.col-md-pull-12{right:100%}.col-md-pull-11{right:91.66666667%}.col-md-pull-10{right:83.33333333%}.col-md-pull-9{right:75%}.col-md-pull-8{right:66.66666667%}.col-md-pull-7{right:58.33333333%}.col-md-pull-6{right:50%}.col-md-pull-5{right:41.66666667%}.col-md-pull-4{right:33.33333333%}.col-md-pull-3{right:25%}.col-md-pull-2{right:16.66666667%}.col-md-pull-1{right:8.33333333%}.col-md-pull-0{right:auto}.col-md-push-12{left:100%}.col-md-push-11{left:91.66666667%}.col-md-push-10{left:83.33333333%}.col-md-push-9{left:75%}.col-md-push-8{left:66.66666667%}.col-md-push-7{left:58.33333333%}.col-md-push-6{left:50%}.col-md-push-5{left:41.66666667%}.col-md-push-4{left:33.33333333%}.col-md-push-3{left:25%}.col-md-push-2{left:16.66666667%}.col-md-push-1{left:8.33333333%}.col-md-push-0{left:auto}.col-md-offset-12{margin-left:100%}.col-md-offset-11{margin-left:91.66666667%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-9{margin-left:75%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-6{margin-left:50%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-3{margin-left:25%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-0{margin-left:0}}@media (min-width:1200px){.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9{float:left}.col-lg-12{width:100%}.col-lg-11{width:91.66666667%}.col-lg-10{width:83.33333333%}.col-lg-9{width:75%}.col-lg-8{width:66.66666667%}.col-lg-7{width:58.33333333%}.col-lg-6{width:50%}.col-lg-5{width:41.66666667%}.col-lg-4{width:33.33333333%}.col-lg-3{width:25%}.col-lg-2{width:16.66666667%}.col-lg-1{width:8.33333333%}.col-lg-pull-12{right:100%}.col-lg-pull-11{right:91.66666667%}.col-lg-pull-10{right:83.33333333%}.col-lg-pull-9{right:75%}.col-lg-pull-8{right:66.66666667%}.col-lg-pull-7{right:58.33333333%}.col-lg-pull-6{right:50%}.col-lg-pull-5{right:41.66666667%}.col-lg-pull-4{right:33.33333333%}.col-lg-pull-3{right:25%}.col-lg-pull-2{right:16.66666667%}.col-lg-pull-1{right:8.33333333%}.col-lg-pull-0{right:auto}.col-lg-push-12{left:100%}.col-lg-push-11{left:91.66666667%}.col-lg-push-10{left:83.33333333%}.col-lg-push-9{left:75%}.col-lg-push-8{left:66.66666667%}.col-lg-push-7{left:58.33333333%}.col-lg-push-6{left:50%}.col-lg-push-5{left:41.66666667%}.col-lg-push-4{left:33.33333333%}.col-lg-push-3{left:25%}.col-lg-push-2{left:16.66666667%}.col-lg-push-1{left:8.33333333%}.col-lg-push-0{left:auto}.col-lg-offset-12{margin-left:100%}.col-lg-offset-11{margin-left:91.66666667%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-0{margin-left:0}}table{background-color:transparent}caption{padding-top:8px;padding-bottom:8px;color:#777;text-align:left}th{text-align:left}.table{width:100%;max-width:100%;margin-bottom:20px}.table>tbody>tr>td,.table>tbody>tr>th,.table>tfoot>tr>td,.table>tfoot>tr>th,.table>thead>tr>td,.table>thead>tr>th{padding:8px;line-height:1.42857143;vertical-align:top;border-top:1px solid #ddd}.table>thead>tr>th{vertical-align:bottom;border-bottom:2px solid #ddd}.table>caption+thead>tr:first-child>td,.table>caption+thead>tr:first-child>th,.table>colgroup+thead>tr:first-child>td,.table>colgroup+thead>tr:first-child>th,.table>thead:first-child>tr:first-child>td,.table>thead:first-child>tr:first-child>th{border-top:0}.table>tbody+tbody{border-top:2px solid #ddd}.table .table{background-color:#fff}.table-condensed>tbody>tr>td,.table-condensed>tbody>tr>th,.table-condensed>tfoot>tr>td,.table-condensed>tfoot>tr>th,.table-condensed>thead>tr>td,.table-condensed>thead>tr>th{padding:5px}.table-bordered{border:1px solid #ddd}.table-bordered>tbody>tr>td,.table-bordered>tbody>tr>th,.table-bordered>tfoot>tr>td,.table-bordered>tfoot>tr>th,.table-bordered>thead>tr>td,.table-bordered>thead>tr>th{border:1px solid #ddd}.table-bordered>thead>tr>td,.table-bordered>thead>tr>th{border-bottom-width:2px}.table-striped>tbody>tr:nth-of-type(odd){background-color:#f9f9f9}.table-hover>tbody>tr:hover{background-color:#f5f5f5}table col[class*=col-]{position:static;display:table-column;float:none}table td[class*=col-],table th[class*=col-]{position:static;display:table-cell;float:none}.table>tbody>tr.active>td,.table>tbody>tr.active>th,.table>tbody>tr>td.active,.table>tbody>tr>th.active,.table>tfoot>tr.active>td,.table>tfoot>tr.active>th,.table>tfoot>tr>td.active,.table>tfoot>tr>th.active,.table>thead>tr.active>td,.table>thead>tr.active>th,.table>thead>tr>td.active,.table>thead>tr>th.active{background-color:#f5f5f5}.table-hover>tbody>tr.active:hover>td,.table-hover>tbody>tr.active:hover>th,.table-hover>tbody>tr:hover>.active,.table-hover>tbody>tr>td.active:hover,.table-hover>tbody>tr>th.active:hover{background-color:#e8e8e8}.table>tbody>tr.success>td,.table>tbody>tr.success>th,.table>tbody>tr>td.success,.table>tbody>tr>th.success,.table>tfoot>tr.success>td,.table>tfoot>tr.success>th,.table>tfoot>tr>td.success,.table>tfoot>tr>th.success,.table>thead>tr.success>td,.table>thead>tr.success>th,.table>thead>tr>td.success,.table>thead>tr>th.success{background-color:#dff0d8}.table-hover>tbody>tr.success:hover>td,.table-hover>tbody>tr.success:hover>th,.table-hover>tbody>tr:hover>.success,.table-hover>tbody>tr>td.success:hover,.table-hover>tbody>tr>th.success:hover{background-color:#d0e9c6}.table>tbody>tr.info>td,.table>tbody>tr.info>th,.table>tbody>tr>td.info,.table>tbody>tr>th.info,.table>tfoot>tr.info>td,.table>tfoot>tr.info>th,.table>tfoot>tr>td.info,.table>tfoot>tr>th.info,.table>thead>tr.info>td,.table>thead>tr.info>th,.table>thead>tr>td.info,.table>thead>tr>th.info{background-color:#d9edf7}.table-hover>tbody>tr.info:hover>td,.table-hover>tbody>tr.info:hover>th,.table-hover>tbody>tr:hover>.info,.table-hover>tbody>tr>td.info:hover,.table-hover>tbody>tr>th.info:hover{background-color:#c4e3f3}.table>tbody>tr.warning>td,.table>tbody>tr.warning>th,.table>tbody>tr>td.warning,.table>tbody>tr>th.warning,.table>tfoot>tr.warning>td,.table>tfoot>tr.warning>th,.table>tfoot>tr>td.warning,.table>tfoot>tr>th.warning,.table>thead>tr.warning>td,.table>thead>tr.warning>th,.table>thead>tr>td.warning,.table>thead>tr>th.warning{background-color:#fcf8e3}.table-hover>tbody>tr.warning:hover>td,.table-hover>tbody>tr.warning:hover>th,.table-hover>tbody>tr:hover>.warning,.table-hover>tbody>tr>td.warning:hover,.table-hover>tbody>tr>th.warning:hover{background-color:#faf2cc}.table>tbody>tr.danger>td,.table>tbody>tr.danger>th,.table>tbody>tr>td.danger,.table>tbody>tr>th.danger,.table>tfoot>tr.danger>td,.table>tfoot>tr.danger>th,.table>tfoot>tr>td.danger,.table>tfoot>tr>th.danger,.table>thead>tr.danger>td,.table>thead>tr.danger>th,.table>thead>tr>td.danger,.table>thead>tr>th.danger{background-color:#f2dede}.table-hover>tbody>tr.danger:hover>td,.table-hover>tbody>tr.danger:hover>th,.table-hover>tbody>tr:hover>.danger,.table-hover>tbody>tr>td.danger:hover,.table-hover>tbody>tr>th.danger:hover{background-color:#ebcccc}.table-responsive{min-height:.01%;overflow-x:auto}@media screen and (max-width:767px){.table-responsive{width:100%;margin-bottom:15px;overflow-y:hidden;-ms-overflow-style:-ms-autohiding-scrollbar;border:1px solid #ddd}.table-responsive>.table{margin-bottom:0}.table-responsive>.table>tbody>tr>td,.table-responsive>.table>tbody>tr>th,.table-responsive>.table>tfoot>tr>td,.table-responsive>.table>tfoot>tr>th,.table-responsive>.table>thead>tr>td,.table-responsive>.table>thead>tr>th{white-space:nowrap}.table-responsive>.table-bordered{border:0}.table-responsive>.table-bordered>tbody>tr>td:first-child,.table-responsive>.table-bordered>tbody>tr>th:first-child,.table-responsive>.table-bordered>tfoot>tr>td:first-child,.table-responsive>.table-bordered>tfoot>tr>th:first-child,.table-responsive>.table-bordered>thead>tr>td:first-child,.table-responsive>.table-bordered>thead>tr>th:first-child{border-left:0}.table-responsive>.table-bordered>tbody>tr>td:last-child,.table-responsive>.table-bordered>tbody>tr>th:last-child,.table-responsive>.table-bordered>tfoot>tr>td:last-child,.table-responsive>.table-bordered>tfoot>tr>th:last-child,.table-responsive>.table-bordered>thead>tr>td:last-child,.table-responsive>.table-bordered>thead>tr>th:last-child{border-right:0}.table-responsive>.table-bordered>tbody>tr:last-child>td,.table-responsive>.table-bordered>tbody>tr:last-child>th,.table-responsive>.table-bordered>tfoot>tr:last-child>td,.table-responsive>.table-bordered>tfoot>tr:last-child>th{border-bottom:0}}fieldset{min-width:0;padding:0;margin:0;border:0}legend{display:block;width:100%;padding:0;margin-bottom:20px;font-size:21px;line-height:inherit;color:#333;border:0;border-bottom:1px solid #e5e5e5}label{display:inline-block;max-width:100%;margin-bottom:5px;font-weight:700}input[type=search]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}input[type=checkbox],input[type=radio]{margin:4px 0 0;margin-top:1px \9;line-height:normal}input[type=file]{display:block}input[type=range]{display:block;width:100%}select[multiple],select[size]{height:auto}input[type=file]:focus,input[type=checkbox]:focus,input[type=radio]:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}output{display:block;padding-top:7px;font-size:14px;line-height:1.42857143;color:#555}.form-control{display:block;width:100%;height:34px;padding:6px 12px;font-size:14px;line-height:1.42857143;color:#555;background-color:#fff;background-image:none;border:1px solid #ccc;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075);-webkit-transition:border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;-o-transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s;transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s}.form-control:focus{border-color:#66afe9;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)}.form-control::-moz-placeholder{color:#999;opacity:1}.form-control:-ms-input-placeholder{color:#999}.form-control::-webkit-input-placeholder{color:#999}.form-control[disabled],.form-control[readonly],fieldset[disabled] .form-control{background-color:#eee;opacity:1}.form-control[disabled],fieldset[disabled] .form-control{cursor:not-allowed}textarea.form-control{height:auto}input[type=search]{-webkit-appearance:none}@media screen and (-webkit-min-device-pixel-ratio:0){input[type=date],input[type=time],input[type=datetime-local],input[type=month]{line-height:34px}.input-group-sm input[type=date],.input-group-sm input[type=time],.input-group-sm input[type=datetime-local],.input-group-sm input[type=month],input[type=date].input-sm,input[type=time].input-sm,input[type=datetime-local].input-sm,input[type=month].input-sm{line-height:30px}.input-group-lg input[type=date],.input-group-lg input[type=time],.input-group-lg input[type=datetime-local],.input-group-lg input[type=month],input[type=date].input-lg,input[type=time].input-lg,input[type=datetime-local].input-lg,input[type=month].input-lg{line-height:46px}}.form-group{margin-bottom:15px}.checkbox,.radio{position:relative;display:block;margin-top:10px;margin-bottom:10px}.checkbox label,.radio label{min-height:20px;padding-left:20px;margin-bottom:0;font-weight:400;cursor:pointer}.checkbox input[type=checkbox],.checkbox-inline input[type=checkbox],.radio input[type=radio],.radio-inline input[type=radio]{position:absolute;margin-top:4px \9;margin-left:-20px}.checkbox+.checkbox,.radio+.radio{margin-top:-5px}.checkbox-inline,.radio-inline{position:relative;display:inline-block;padding-left:20px;margin-bottom:0;font-weight:400;vertical-align:middle;cursor:pointer}.checkbox-inline+.checkbox-inline,.radio-inline+.radio-inline{margin-top:0;margin-left:10px}fieldset[disabled] input[type=checkbox],fieldset[disabled] input[type=radio],input[type=checkbox].disabled,input[type=checkbox][disabled],input[type=radio].disabled,input[type=radio][disabled]{cursor:not-allowed}.checkbox-inline.disabled,.radio-inline.disabled,fieldset[disabled] .checkbox-inline,fieldset[disabled] .radio-inline{cursor:not-allowed}.checkbox.disabled label,.radio.disabled label,fieldset[disabled] .checkbox label,fieldset[disabled] .radio label{cursor:not-allowed}.form-control-static{min-height:34px;padding-top:7px;padding-bottom:7px;margin-bottom:0}.form-control-static.input-lg,.form-control-static.input-sm{padding-right:0;padding-left:0}.input-sm{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.input-sm{height:30px;line-height:30px}select[multiple].input-sm,textarea.input-sm{height:auto}.form-group-sm .form-control{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.form-group-sm .form-control{height:30px;line-height:30px}select[multiple].form-group-sm .form-control,textarea.form-group-sm .form-control{height:auto}.form-group-sm .form-control-static{height:30px;min-height:32px;padding:5px 10px;font-size:12px;line-height:1.5}.input-lg{height:46px;padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}select.input-lg{height:46px;line-height:46px}select[multiple].input-lg,textarea.input-lg{height:auto}.form-group-lg .form-control{height:46px;padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}select.form-group-lg .form-control{height:46px;line-height:46px}select[multiple].form-group-lg .form-control,textarea.form-group-lg .form-control{height:auto}.form-group-lg .form-control-static{height:46px;min-height:38px;padding:10px 16px;font-size:18px;line-height:1.3333333}.has-feedback{position:relative}.has-feedback .form-control{padding-right:42.5px}.form-control-feedback{position:absolute;top:0;right:0;z-index:2;display:block;width:34px;height:34px;line-height:34px;text-align:center;pointer-events:none}.input-lg+.form-control-feedback{width:46px;height:46px;line-height:46px}.input-sm+.form-control-feedback{width:30px;height:30px;line-height:30px}.has-success .checkbox,.has-success .checkbox-inline,.has-success .control-label,.has-success .help-block,.has-success .radio,.has-success .radio-inline,.has-success.checkbox label,.has-success.checkbox-inline label,.has-success.radio label,.has-success.radio-inline label{color:#3c763d}.has-success .form-control{border-color:#3c763d;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-success .form-control:focus{border-color:#2b542c;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #67b168;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #67b168}.has-success .input-group-addon{color:#3c763d;background-color:#dff0d8;border-color:#3c763d}.has-success .form-control-feedback{color:#3c763d}.has-warning .checkbox,.has-warning .checkbox-inline,.has-warning .control-label,.has-warning .help-block,.has-warning .radio,.has-warning .radio-inline,.has-warning.checkbox label,.has-warning.checkbox-inline label,.has-warning.radio label,.has-warning.radio-inline label{color:#8a6d3b}.has-warning .form-control{border-color:#8a6d3b;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-warning .form-control:focus{border-color:#66512c;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #c0a16b;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #c0a16b}.has-warning .input-group-addon{color:#8a6d3b;background-color:#fcf8e3;border-color:#8a6d3b}.has-warning .form-control-feedback{color:#8a6d3b}.has-error .checkbox,.has-error .checkbox-inline,.has-error .control-label,.has-error .help-block,.has-error .radio,.has-error .radio-inline,.has-error.checkbox label,.has-error.checkbox-inline label,.has-error.radio label,.has-error.radio-inline label{color:#a94442}.has-error .form-control{border-color:#a94442;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-error .form-control:focus{border-color:#843534;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #ce8483;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #ce8483}.has-error .input-group-addon{color:#a94442;background-color:#f2dede;border-color:#a94442}.has-error .form-control-feedback{color:#a94442}.has-feedback label~.form-control-feedback{top:25px}.has-feedback label.sr-only~.form-control-feedback{top:0}.help-block{display:block;margin-top:5px;margin-bottom:10px;color:#737373}@media (min-width:768px){.form-inline .form-group{display:inline-block;margin-bottom:0;vertical-align:middle}.form-inline .form-control{display:inline-block;width:auto;vertical-align:middle}.form-inline .form-control-static{display:inline-block}.form-inline .input-group{display:inline-table;vertical-align:middle}.form-inline .input-group .form-control,.form-inline .input-group .input-group-addon,.form-inline .input-group .input-group-btn{width:auto}.form-inline .input-group>.form-control{width:100%}.form-inline .control-label{margin-bottom:0;vertical-align:middle}.form-inline .checkbox,.form-inline .radio{display:inline-block;margin-top:0;margin-bottom:0;vertical-align:middle}.form-inline .checkbox label,.form-inline .radio label{padding-left:0}.form-inline .checkbox input[type=checkbox],.form-inline .radio input[type=radio]{position:relative;margin-left:0}.form-inline .has-feedback .form-control-feedback{top:0}}.form-horizontal .checkbox,.form-horizontal .checkbox-inline,.form-horizontal .radio,.form-horizontal .radio-inline{padding-top:7px;margin-top:0;margin-bottom:0}.form-horizontal .checkbox,.form-horizontal .radio{min-height:27px}.form-horizontal .form-group{margin-right:-15px;margin-left:-15px}@media (min-width:768px){.form-horizontal .control-label{padding-top:7px;margin-bottom:0;text-align:right}}.form-horizontal .has-feedback .form-control-feedback{right:15px}@media (min-width:768px){.form-horizontal .form-group-lg .control-label{padding-top:14.33px}}@media (min-width:768px){.form-horizontal .form-group-sm .control-label{padding-top:6px}}.btn{display:inline-block;padding:6px 12px;margin-bottom:0;font-size:14px;font-weight:400;line-height:1.42857143;text-align:center;white-space:nowrap;vertical-align:middle;-ms-touch-action:manipulation;touch-action:manipulation;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-image:none;border:1px solid transparent;border-radius:4px}.btn.active.focus,.btn.active:focus,.btn.focus,.btn:active.focus,.btn:active:focus,.btn:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}.btn.focus,.btn:focus,.btn:hover{color:#333;text-decoration:none}.btn.active,.btn:active{background-image:none;outline:0;-webkit-box-shadow:inset 0 3px 5px rgba(0,0,0,.125);box-shadow:inset 0 3px 5px rgba(0,0,0,.125)}.btn.disabled,.btn[disabled],fieldset[disabled] .btn{pointer-events:none;cursor:not-allowed;filter:alpha(opacity=65);-webkit-box-shadow:none;box-shadow:none;opacity:.65}.btn-default{color:#333;background-color:#fff;border-color:#ccc}.btn-default.active,.btn-default.focus,.btn-default:active,.btn-default:focus,.btn-default:hover,.open>.dropdown-toggle.btn-default{color:#333;background-color:#e6e6e6;border-color:#adadad}.btn-default.active,.btn-default:active,.open>.dropdown-toggle.btn-default{background-image:none}.btn-default.disabled,.btn-default.disabled.active,.btn-default.disabled.focus,.btn-default.disabled:active,.btn-default.disabled:focus,.btn-default.disabled:hover,.btn-default[disabled],.btn-default[disabled].active,.btn-default[disabled].focus,.btn-default[disabled]:active,.btn-default[disabled]:focus,.btn-default[disabled]:hover,fieldset[disabled] .btn-default,fieldset[disabled] .btn-default.active,fieldset[disabled] .btn-default.focus,fieldset[disabled] .btn-default:active,fieldset[disabled] .btn-default:focus,fieldset[disabled] .btn-default:hover{background-color:#fff;border-color:#ccc}.btn-default .badge{color:#fff;background-color:#333}.btn-primary{color:#fff;background-color:#337ab7;border-color:#2e6da4}.btn-primary.active,.btn-primary.focus,.btn-primary:active,.btn-primary:focus,.btn-primary:hover,.open>.dropdown-toggle.btn-primary{color:#fff;background-color:#286090;border-color:#204d74}.btn-primary.active,.btn-primary:active,.open>.dropdown-toggle.btn-primary{background-image:none}.btn-primary.disabled,.btn-primary.disabled.active,.btn-primary.disabled.focus,.btn-primary.disabled:active,.btn-primary.disabled:focus,.btn-primary.disabled:hover,.btn-primary[disabled],.btn-primary[disabled].active,.btn-primary[disabled].focus,.btn-primary[disabled]:active,.btn-primary[disabled]:focus,.btn-primary[disabled]:hover,fieldset[disabled] .btn-primary,fieldset[disabled] .btn-primary.active,fieldset[disabled] .btn-primary.focus,fieldset[disabled] .btn-primary:active,fieldset[disabled] .btn-primary:focus,fieldset[disabled] .btn-primary:hover{background-color:#337ab7;border-color:#2e6da4}.btn-primary .badge{color:#337ab7;background-color:#fff}.btn-success{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.btn-success.active,.btn-success.focus,.btn-success:active,.btn-success:focus,.btn-success:hover,.open>.dropdown-toggle.btn-success{color:#fff;background-color:#449d44;border-color:#398439}.btn-success.active,.btn-success:active,.open>.dropdown-toggle.btn-success{background-image:none}.btn-success.disabled,.btn-success.disabled.active,.btn-success.disabled.focus,.btn-success.disabled:active,.btn-success.disabled:focus,.btn-success.disabled:hover,.btn-success[disabled],.btn-success[disabled].active,.btn-success[disabled].focus,.btn-success[disabled]:active,.btn-success[disabled]:focus,.btn-success[disabled]:hover,fieldset[disabled] .btn-success,fieldset[disabled] .btn-success.active,fieldset[disabled] .btn-success.focus,fieldset[disabled] .btn-success:active,fieldset[disabled] .btn-success:focus,fieldset[disabled] .btn-success:hover{background-color:#5cb85c;border-color:#4cae4c}.btn-success .badge{color:#5cb85c;background-color:#fff}.btn-info{color:#fff;background-color:#5bc0de;border-color:#46b8da}.btn-info.active,.btn-info.focus,.btn-info:active,.btn-info:focus,.btn-info:hover,.open>.dropdown-toggle.btn-info{color:#fff;background-color:#31b0d5;border-color:#269abc}.btn-info.active,.btn-info:active,.open>.dropdown-toggle.btn-info{background-image:none}.btn-info.disabled,.btn-info.disabled.active,.btn-info.disabled.focus,.btn-info.disabled:active,.btn-info.disabled:focus,.btn-info.disabled:hover,.btn-info[disabled],.btn-info[disabled].active,.btn-info[disabled].focus,.btn-info[disabled]:active,.btn-info[disabled]:focus,.btn-info[disabled]:hover,fieldset[disabled] .btn-info,fieldset[disabled] .btn-info.active,fieldset[disabled] .btn-info.focus,fieldset[disabled] .btn-info:active,fieldset[disabled] .btn-info:focus,fieldset[disabled] .btn-info:hover{background-color:#5bc0de;border-color:#46b8da}.btn-info .badge{color:#5bc0de;background-color:#fff}.btn-warning{color:#fff;background-color:#f0ad4e;border-color:#eea236}.btn-warning.active,.btn-warning.focus,.btn-warning:active,.btn-warning:focus,.btn-warning:hover,.open>.dropdown-toggle.btn-warning{color:#fff;background-color:#ec971f;border-color:#d58512}.btn-warning.active,.btn-warning:active,.open>.dropdown-toggle.btn-warning{background-image:none}.btn-warning.disabled,.btn-warning.disabled.active,.btn-warning.disabled.focus,.btn-warning.disabled:active,.btn-warning.disabled:focus,.btn-warning.disabled:hover,.btn-warning[disabled],.btn-warning[disabled].active,.btn-warning[disabled].focus,.btn-warning[disabled]:active,.btn-warning[disabled]:focus,.btn-warning[disabled]:hover,fieldset[disabled] .btn-warning,fieldset[disabled] .btn-warning.active,fieldset[disabled] .btn-warning.focus,fieldset[disabled] .btn-warning:active,fieldset[disabled] .btn-warning:focus,fieldset[disabled] .btn-warning:hover{background-color:#f0ad4e;border-color:#eea236}.btn-warning .badge{color:#f0ad4e;background-color:#fff}.btn-danger{color:#fff;background-color:#d9534f;border-color:#d43f3a}.btn-danger.active,.btn-danger.focus,.btn-danger:active,.btn-danger:focus,.btn-danger:hover,.open>.dropdown-toggle.btn-danger{color:#fff;background-color:#c9302c;border-color:#ac2925}.btn-danger.active,.btn-danger:active,.open>.dropdown-toggle.btn-danger{background-image:none}.btn-danger.disabled,.btn-danger.disabled.active,.btn-danger.disabled.focus,.btn-danger.disabled:active,.btn-danger.disabled:focus,.btn-danger.disabled:hover,.btn-danger[disabled],.btn-danger[disabled].active,.btn-danger[disabled].focus,.btn-danger[disabled]:active,.btn-danger[disabled]:focus,.btn-danger[disabled]:hover,fieldset[disabled] .btn-danger,fieldset[disabled] .btn-danger.active,fieldset[disabled] .btn-danger.focus,fieldset[disabled] .btn-danger:active,fieldset[disabled] .btn-danger:focus,fieldset[disabled] .btn-danger:hover{background-color:#d9534f;border-color:#d43f3a}.btn-danger .badge{color:#d9534f;background-color:#fff}.btn-link{font-weight:400;color:#337ab7;border-radius:0}.btn-link,.btn-link.active,.btn-link:active,.btn-link[disabled],fieldset[disabled] .btn-link{background-color:transparent;-webkit-box-shadow:none;box-shadow:none}.btn-link,.btn-link:active,.btn-link:focus,.btn-link:hover{border-color:transparent}.btn-link:focus,.btn-link:hover{color:#23527c;text-decoration:underline;background-color:transparent}.btn-link[disabled]:focus,.btn-link[disabled]:hover,fieldset[disabled] .btn-link:focus,fieldset[disabled] .btn-link:hover{color:#777;text-decoration:none}.btn-group-lg>.btn,.btn-lg{padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}.btn-group-sm>.btn,.btn-sm{padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}.btn-group-xs>.btn,.btn-xs{padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px}.btn-block{display:block;width:100%}.btn-block+.btn-block{margin-top:5px}input[type=button].btn-block,input[type=reset].btn-block,input[type=submit].btn-block{width:100%}.fade{opacity:0;-webkit-transition:opacity .15s linear;-o-transition:opacity .15s linear;transition:opacity .15s linear}.fade.in{opacity:1}.collapse{display:none}.collapse.in{display:block}tr.collapse.in{display:table-row}tbody.collapse.in{display:table-row-group}.collapsing{position:relative;height:0;overflow:hidden;-webkit-transition-timing-function:ease;-o-transition-timing-function:ease;transition-timing-function:ease;-webkit-transition-duration:.35s;-o-transition-duration:.35s;transition-duration:.35s;-webkit-transition-property:height,visibility;-o-transition-property:height,visibility;transition-property:height,visibility}.caret{display:inline-block;width:0;height:0;margin-left:2px;vertical-align:middle;border-top:4px dashed;border-right:4px solid transparent;border-left:4px solid transparent}.dropdown,.dropup{position:relative}.dropdown-toggle:focus{outline:0}.dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;display:none;float:left;min-width:160px;padding:5px 0;margin:2px 0 0;font-size:14px;text-align:left;list-style:none;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.15);border-radius:4px;-webkit-box-shadow:0 6px 12px rgba(0,0,0,.175);box-shadow:0 6px 12px rgba(0,0,0,.175)}.dropdown-menu.pull-right{right:0;left:auto}.dropdown-menu .divider{height:1px;margin:9px 0;overflow:hidden;background-color:#e5e5e5}.dropdown-menu>li>a{display:block;padding:3px 20px;clear:both;font-weight:400;line-height:1.42857143;color:#333;white-space:nowrap}.dropdown-menu>li>a:focus,.dropdown-menu>li>a:hover{color:#262626;text-decoration:none;background-color:#f5f5f5}.dropdown-menu>.active>a,.dropdown-menu>.active>a:focus,.dropdown-menu>.active>a:hover{color:#fff;text-decoration:none;background-color:#337ab7;outline:0}.dropdown-menu>.disabled>a,.dropdown-menu>.disabled>a:focus,.dropdown-menu>.disabled>a:hover{color:#777}.dropdown-menu>.disabled>a:focus,.dropdown-menu>.disabled>a:hover{text-decoration:none;cursor:not-allowed;background-color:transparent;background-image:none;filter:progid:DXImageTransform.Microsoft.gradient(enabled=false)}.open>.dropdown-menu{display:block}.open>a{outline:0}.dropdown-menu-right{right:0;left:auto}.dropdown-menu-left{right:auto;left:0}.dropdown-header{display:block;padding:3px 20px;font-size:12px;line-height:1.42857143;color:#777;white-space:nowrap}.dropdown-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;z-index:990}.pull-right>.dropdown-menu{right:0;left:auto}.dropup .caret,.navbar-fixed-bottom .dropdown .caret{content:"";border-top:0;border-bottom:4px solid}.dropup .dropdown-menu,.navbar-fixed-bottom .dropdown .dropdown-menu{top:auto;bottom:100%;margin-bottom:2px}@media (min-width:768px){.navbar-right .dropdown-menu{right:0;left:auto}.navbar-right .dropdown-menu-left{right:auto;left:0}}.btn-group,.btn-group-vertical{position:relative;display:inline-block;vertical-align:middle}.btn-group-vertical>.btn,.btn-group>.btn{position:relative;float:left}.btn-group-vertical>.btn.active,.btn-group-vertical>.btn:active,.btn-group-vertical>.btn:focus,.btn-group-vertical>.btn:hover,.btn-group>.btn.active,.btn-group>.btn:active,.btn-group>.btn:focus,.btn-group>.btn:hover{z-index:2}.btn-group .btn+.btn,.btn-group .btn+.btn-group,.btn-group .btn-group+.btn,.btn-group .btn-group+.btn-group{margin-left:-1px}.btn-toolbar{margin-left:-5px}.btn-toolbar .btn-group,.btn-toolbar .input-group{float:left}.btn-toolbar>.btn,.btn-toolbar>.btn-group,.btn-toolbar>.input-group{margin-left:5px}.btn-group>.btn:not(:first-child):not(:last-child):not(.dropdown-toggle){border-radius:0}.btn-group>.btn:first-child{margin-left:0}.btn-group>.btn:first-child:not(:last-child):not(.dropdown-toggle){border-top-right-radius:0;border-bottom-right-radius:0}.btn-group>.btn:last-child:not(:first-child),.btn-group>.dropdown-toggle:not(:first-child){border-top-left-radius:0;border-bottom-left-radius:0}.btn-group>.btn-group{float:left}.btn-group>.btn-group:not(:first-child):not(:last-child)>.btn{border-radius:0}.btn-group>.btn-group:first-child:not(:last-child)>.btn:last-child,.btn-group>.btn-group:first-child:not(:last-child)>.dropdown-toggle{border-top-right-radius:0;border-bottom-right-radius:0}.btn-group>.btn-group:last-child:not(:first-child)>.btn:first-child{border-top-left-radius:0;border-bottom-left-radius:0}.btn-group .dropdown-toggle:active,.btn-group.open .dropdown-toggle{outline:0}.btn-group>.btn+.dropdown-toggle{padding-right:8px;padding-left:8px}.btn-group>.btn-lg+.dropdown-toggle{padding-right:12px;padding-left:12px}.btn-group.open .dropdown-toggle{-webkit-box-shadow:inset 0 3px 5px rgba(0,0,0,.125);box-shadow:inset 0 3px 5px rgba(0,0,0,.125)}.btn-group.open .dropdown-toggle.btn-link{-webkit-box-shadow:none;box-shadow:none}.btn .caret{margin-left:0}.btn-lg .caret{border-width:5px 5px 0;border-bottom-width:0}.dropup .btn-lg .caret{border-width:0 5px 5px}.btn-group-vertical>.btn,.btn-group-vertical>.btn-group,.btn-group-vertical>.btn-group>.btn{display:block;float:none;width:100%;max-width:100%}.btn-group-vertical>.btn-group>.btn{float:none}.btn-group-vertical>.btn+.btn,.btn-group-vertical>.btn+.btn-group,.btn-group-vertical>.btn-group+.btn,.btn-group-vertical>.btn-group+.btn-group{margin-top:-1px;margin-left:0}.btn-group-vertical>.btn:not(:first-child):not(:last-child){border-radius:0}.btn-group-vertical>.btn:first-child:not(:last-child){border-top-right-radius:4px;border-bottom-right-radius:0;border-bottom-left-radius:0}.btn-group-vertical>.btn:last-child:not(:first-child){border-top-left-radius:0;border-top-right-radius:0;border-bottom-left-radius:4px}.btn-group-vertical>.btn-group:not(:first-child):not(:last-child)>.btn{border-radius:0}.btn-group-vertical>.btn-group:first-child:not(:last-child)>.btn:last-child,.btn-group-vertical>.btn-group:first-child:not(:last-child)>.dropdown-toggle{border-bottom-right-radius:0;border-bottom-left-radius:0}.btn-group-vertical>.btn-group:last-child:not(:first-child)>.btn:first-child{border-top-left-radius:0;border-top-right-radius:0}.btn-group-justified{display:table;width:100%;table-layout:fixed;border-collapse:separate}.btn-group-justified>.btn,.btn-group-justified>.btn-group{display:table-cell;float:none;width:1%}.btn-group-justified>.btn-group .btn{width:100%}.btn-group-justified>.btn-group .dropdown-menu{left:auto}[data-toggle=buttons]>.btn input[type=checkbox],[data-toggle=buttons]>.btn input[type=radio],[data-toggle=buttons]>.btn-group>.btn input[type=checkbox],[data-toggle=buttons]>.btn-group>.btn input[type=radio]{position:absolute;clip:rect(0,0,0,0);pointer-events:none}.input-group{position:relative;display:table;border-collapse:separate}.input-group[class*=col-]{float:none;padding-right:0;padding-left:0}.input-group .form-control{position:relative;z-index:2;float:left;width:100%;margin-bottom:0}.input-group-lg>.form-control,.input-group-lg>.input-group-addon,.input-group-lg>.input-group-btn>.btn{height:46px;padding:10px 16px;font-size:18px;line-height:1.3333333;border-radius:6px}select.input-group-lg>.form-control,select.input-group-lg>.input-group-addon,select.input-group-lg>.input-group-btn>.btn{height:46px;line-height:46px}select[multiple].input-group-lg>.form-control,select[multiple].input-group-lg>.input-group-addon,select[multiple].input-group-lg>.input-group-btn>.btn,textarea.input-group-lg>.form-control,textarea.input-group-lg>.input-group-addon,textarea.input-group-lg>.input-group-btn>.btn{height:auto}.input-group-sm>.form-control,.input-group-sm>.input-group-addon,.input-group-sm>.input-group-btn>.btn{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.input-group-sm>.form-control,select.input-group-sm>.input-group-addon,select.input-group-sm>.input-group-btn>.btn{height:30px;line-height:30px}select[multiple].input-group-sm>.form-control,select[multiple].input-group-sm>.input-group-addon,select[multiple].input-group-sm>.input-group-btn>.btn,textarea.input-group-sm>.form-control,textarea.input-group-sm>.input-group-addon,textarea.input-group-sm>.input-group-btn>.btn{height:auto}.input-group .form-control,.input-group-addon,.input-group-btn{display:table-cell}.input-group .form-control:not(:first-child):not(:last-child),.input-group-addon:not(:first-child):not(:last-child),.input-group-btn:not(:first-child):not(:last-child){border-radius:0}.input-group-addon,.input-group-btn{width:1%;white-space:nowrap;vertical-align:middle}.input-group-addon{padding:6px 12px;font-size:14px;font-weight:400;line-height:1;color:#555;text-align:center;background-color:#eee;border:1px solid #ccc;border-radius:4px}.input-group-addon.input-sm{padding:5px 10px;font-size:12px;border-radius:3px}.input-group-addon.input-lg{padding:10px 16px;font-size:18px;border-radius:6px}.input-group-addon input[type=checkbox],.input-group-addon input[type=radio]{margin-top:0}.input-group .form-control:first-child,.input-group-addon:first-child,.input-group-btn:first-child>.btn,.input-group-btn:first-child>.btn-group>.btn,.input-group-btn:first-child>.dropdown-toggle,.input-group-btn:last-child>.btn-group:not(:last-child)>.btn,.input-group-btn:last-child>.btn:not(:last-child):not(.dropdown-toggle){border-top-right-radius:0;border-bottom-right-radius:0}.input-group-addon:first-child{border-right:0}.input-group .form-control:last-child,.input-group-addon:last-child,.input-group-btn:first-child>.btn-group:not(:first-child)>.btn,.input-group-btn:first-child>.btn:not(:first-child),.input-group-btn:last-child>.btn,.input-group-btn:last-child>.btn-group>.btn,.input-group-btn:last-child>.dropdown-toggle{border-top-left-radius:0;border-bottom-left-radius:0}.input-group-addon:last-child{border-left:0}.input-group-btn{position:relative;font-size:0;white-space:nowrap}.input-group-btn>.btn{position:relative}.input-group-btn>.btn+.btn{margin-left:-1px}.input-group-btn>.btn:active,.input-group-btn>.btn:focus,.input-group-btn>.btn:hover{z-index:2}.input-group-btn:first-child>.btn,.input-group-btn:first-child>.btn-group{margin-right:-1px}.input-group-btn:last-child>.btn,.input-group-btn:last-child>.btn-group{margin-left:-1px}.nav{padding-left:0;margin-bottom:0;list-style:none}.nav>li{position:relative;display:block}.nav>li>a{position:relative;display:block;padding:10px 15px}.nav>li>a:focus,.nav>li>a:hover{text-decoration:none;background-color:#eee}.nav>li.disabled>a{color:#777}.nav>li.disabled>a:focus,.nav>li.disabled>a:hover{color:#777;text-decoration:none;cursor:not-allowed;background-color:transparent}.nav .open>a,.nav .open>a:focus,.nav .open>a:hover{background-color:#eee;border-color:#337ab7}.nav .nav-divider{height:1px;margin:9px 0;overflow:hidden;background-color:#e5e5e5}.nav>li>a>img{max-width:none}.nav-tabs{border-bottom:1px solid #ddd}.nav-tabs>li{float:left;margin-bottom:-1px}.nav-tabs>li>a{margin-right:2px;line-height:1.42857143;border:1px solid transparent;border-radius:4px 4px 0 0}.nav-tabs>li>a:hover{border-color:#eee #eee #ddd}.nav-tabs>li.active>a,.nav-tabs>li.active>a:focus,.nav-tabs>li.active>a:hover{color:#555;cursor:default;background-color:#fff;border:1px solid #ddd;border-bottom-color:transparent}.nav-tabs.nav-justified{width:100%;border-bottom:0}.nav-tabs.nav-justified>li{float:none}.nav-tabs.nav-justified>li>a{margin-bottom:5px;text-align:center}.nav-tabs.nav-justified>.dropdown .dropdown-menu{top:auto;left:auto}@media (min-width:768px){.nav-tabs.nav-justified>li{display:table-cell;width:1%}.nav-tabs.nav-justified>li>a{margin-bottom:0}}.nav-tabs.nav-justified>li>a{margin-right:0;border-radius:4px}.nav-tabs.nav-justified>.active>a,.nav-tabs.nav-justified>.active>a:focus,.nav-tabs.nav-justified>.active>a:hover{border:1px solid #ddd}@media (min-width:768px){.nav-tabs.nav-justified>li>a{border-bottom:1px solid #ddd;border-radius:4px 4px 0 0}.nav-tabs.nav-justified>.active>a,.nav-tabs.nav-justified>.active>a:focus,.nav-tabs.nav-justified>.active>a:hover{border-bottom-color:#fff}}.nav-pills>li{float:left}.nav-pills>li>a{border-radius:4px}.nav-pills>li+li{margin-left:2px}.nav-pills>li.active>a,.nav-pills>li.active>a:focus,.nav-pills>li.active>a:hover{color:#fff;background-color:#337ab7}.nav-stacked>li{float:none}.nav-stacked>li+li{margin-top:2px;margin-left:0}.nav-justified{width:100%}.nav-justified>li{float:none}.nav-justified>li>a{margin-bottom:5px;text-align:center}.nav-justified>.dropdown .dropdown-menu{top:auto;left:auto}@media (min-width:768px){.nav-justified>li{display:table-cell;width:1%}.nav-justified>li>a{margin-bottom:0}}.nav-tabs-justified{border-bottom:0}.nav-tabs-justified>li>a{margin-right:0;border-radius:4px}.nav-tabs-justified>.active>a,.nav-tabs-justified>.active>a:focus,.nav-tabs-justified>.active>a:hover{border:1px solid #ddd}@media (min-width:768px){.nav-tabs-justified>li>a{border-bottom:1px solid #ddd;border-radius:4px 4px 0 0}.nav-tabs-justified>.active>a,.nav-tabs-justified>.active>a:focus,.nav-tabs-justified>.active>a:hover{border-bottom-color:#fff}}.tab-content>.tab-pane{display:none}.tab-content>.active{display:block}.nav-tabs .dropdown-menu{margin-top:-1px;border-top-left-radius:0;border-top-right-radius:0}.navbar{position:relative;min-height:50px;margin-bottom:20px;border:1px solid transparent}@media (min-width:768px){.navbar{border-radius:4px}}@media (min-width:768px){.navbar-header{float:left}}.navbar-collapse{padding-right:15px;padding-left:15px;overflow-x:visible;-webkit-overflow-scrolling:touch;border-top:1px solid transparent;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.1);box-shadow:inset 0 1px 0 rgba(255,255,255,.1)}.navbar-collapse.in{overflow-y:auto}@media (min-width:768px){.navbar-collapse{width:auto;border-top:0;-webkit-box-shadow:none;box-shadow:none}.navbar-collapse.collapse{display:block!important;height:auto!important;padding-bottom:0;overflow:visible!important}.navbar-collapse.in{overflow-y:visible}.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse,.navbar-static-top .navbar-collapse{padding-right:0;padding-left:0}}.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse{max-height:340px}@media (max-device-width:480px)and (orientation:landscape){.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse{max-height:200px}}.container-fluid>.navbar-collapse,.container-fluid>.navbar-header,.container>.navbar-collapse,.container>.navbar-header{margin-right:-15px;margin-left:-15px}@media (min-width:768px){.container-fluid>.navbar-collapse,.container-fluid>.navbar-header,.container>.navbar-collapse,.container>.navbar-header{margin-right:0;margin-left:0}}.navbar-static-top{z-index:1000;border-width:0 0 1px}@media (min-width:768px){.navbar-static-top{border-radius:0}}.navbar-fixed-bottom,.navbar-fixed-top{position:fixed;right:0;left:0;z-index:1030}@media (min-width:768px){.navbar-fixed-bottom,.navbar-fixed-top{border-radius:0}}.navbar-fixed-top{top:0;border-width:0 0 1px}.navbar-fixed-bottom{bottom:0;margin-bottom:0;border-width:1px 0 0}.navbar-brand{float:left;height:50px;padding:15px 15px;font-size:18px;line-height:20px}.navbar-brand:focus,.navbar-brand:hover{text-decoration:none}.navbar-brand>img{display:block}@media (min-width:768px){.navbar>.container .navbar-brand,.navbar>.container-fluid .navbar-brand{margin-left:-15px}}.navbar-toggle{position:relative;float:right;padding:9px 10px;margin-top:8px;margin-right:15px;margin-bottom:8px;background-color:transparent;background-image:none;border:1px solid transparent;border-radius:4px}.navbar-toggle:focus{outline:0}.navbar-toggle .icon-bar{display:block;width:22px;height:2px;border-radius:1px}.navbar-toggle .icon-bar+.icon-bar{margin-top:4px}@media (min-width:768px){.navbar-toggle{display:none}}.navbar-nav{margin:7.5px -15px}.navbar-nav>li>a{padding-top:10px;padding-bottom:10px;line-height:20px}@media (max-width:767px){.navbar-nav .open .dropdown-menu{position:static;float:none;width:auto;margin-top:0;background-color:transparent;border:0;-webkit-box-shadow:none;box-shadow:none}.navbar-nav .open .dropdown-menu .dropdown-header,.navbar-nav .open .dropdown-menu>li>a{padding:5px 15px 5px 25px}.navbar-nav .open .dropdown-menu>li>a{line-height:20px}.navbar-nav .open .dropdown-menu>li>a:focus,.navbar-nav .open .dropdown-menu>li>a:hover{background-image:none}}@media (min-width:768px){.navbar-nav{float:left;margin:0}.navbar-nav>li{float:left}.navbar-nav>li>a{padding-top:15px;padding-bottom:15px}}.navbar-form{padding:10px 15px;margin-top:8px;margin-right:-15px;margin-bottom:8px;margin-left:-15px;border-top:1px solid transparent;border-bottom:1px solid transparent;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1);box-shadow:inset 0 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1)}@media (min-width:768px){.navbar-form .form-group{display:inline-block;margin-bottom:0;vertical-align:middle}.navbar-form .form-control{display:inline-block;width:auto;vertical-align:middle}.navbar-form .form-control-static{display:inline-block}.navbar-form .input-group{display:inline-table;vertical-align:middle}.navbar-form .input-group .form-control,.navbar-form .input-group .input-group-addon,.navbar-form .input-group .input-group-btn{width:auto}.navbar-form .input-group>.form-control{width:100%}.navbar-form .control-label{margin-bottom:0;vertical-align:middle}.navbar-form .checkbox,.navbar-form .radio{display:inline-block;margin-top:0;margin-bottom:0;vertical-align:middle}.navbar-form .checkbox label,.navbar-form .radio label{padding-left:0}.navbar-form .checkbox input[type=checkbox],.navbar-form .radio input[type=radio]{position:relative;margin-left:0}.navbar-form .has-feedback .form-control-feedback{top:0}}@media (max-width:767px){.navbar-form .form-group{margin-bottom:5px}.navbar-form .form-group:last-child{margin-bottom:0}}@media (min-width:768px){.navbar-form{width:auto;padding-top:0;padding-bottom:0;margin-right:0;margin-left:0;border:0;-webkit-box-shadow:none;box-shadow:none}}.navbar-nav>li>.dropdown-menu{margin-top:0;border-top-left-radius:0;border-top-right-radius:0}.navbar-fixed-bottom .navbar-nav>li>.dropdown-menu{margin-bottom:0;border-top-left-radius:4px;border-top-right-radius:4px;border-bottom-right-radius:0;border-bottom-left-radius:0}.navbar-btn{margin-top:8px;margin-bottom:8px}.navbar-btn.btn-sm{margin-top:10px;margin-bottom:10px}.navbar-btn.btn-xs{margin-top:14px;margin-bottom:14px}.navbar-text{margin-top:15px;margin-bottom:15px}@media (min-width:768px){.navbar-text{float:left;margin-right:15px;margin-left:15px}}@media (min-width:768px){.navbar-left{float:left!important}.navbar-right{float:right!important;margin-right:-15px}.navbar-right~.navbar-right{margin-right:0}}.navbar-default{background-color:#f8f8f8;border-color:#e7e7e7}.navbar-default .navbar-brand{color:#777}.navbar-default .navbar-brand:focus,.navbar-default .navbar-brand:hover{color:#5e5e5e;background-color:transparent}.navbar-default .navbar-text{color:#777}.navbar-default .navbar-nav>li>a{color:#777}.navbar-default .navbar-nav>li>a:focus,.navbar-default .navbar-nav>li>a:hover{color:#333;background-color:transparent}.navbar-default .navbar-nav>.active>a,.navbar-default .navbar-nav>.active>a:focus,.navbar-default .navbar-nav>.active>a:hover{color:#555;background-color:#e7e7e7}.navbar-default .navbar-nav>.disabled>a,.navbar-default .navbar-nav>.disabled>a:focus,.navbar-default .navbar-nav>.disabled>a:hover{color:#ccc;background-color:transparent}.navbar-default .navbar-toggle{border-color:#ddd}.navbar-default .navbar-toggle:focus,.navbar-default .navbar-toggle:hover{background-color:#ddd}.navbar-default .navbar-toggle .icon-bar{background-color:#888}.navbar-default .navbar-collapse,.navbar-default .navbar-form{border-color:#e7e7e7}.navbar-default .navbar-nav>.open>a,.navbar-default .navbar-nav>.open>a:focus,.navbar-default .navbar-nav>.open>a:hover{color:#555;background-color:#e7e7e7}@media (max-width:767px){.navbar-default .navbar-nav .open .dropdown-menu>li>a{color:#777}.navbar-default .navbar-nav .open .dropdown-menu>li>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>li>a:hover{color:#333;background-color:transparent}.navbar-default .navbar-nav .open .dropdown-menu>.active>a,.navbar-default .navbar-nav .open .dropdown-menu>.active>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>.active>a:hover{color:#555;background-color:#e7e7e7}.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a,.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a:hover{color:#ccc;background-color:transparent}}.navbar-default .navbar-link{color:#777}.navbar-default .navbar-link:hover{color:#333}.navbar-default .btn-link{color:#777}.navbar-default .btn-link:focus,.navbar-default .btn-link:hover{color:#333}.navbar-default .btn-link[disabled]:focus,.navbar-default .btn-link[disabled]:hover,fieldset[disabled] .navbar-default .btn-link:focus,fieldset[disabled] .navbar-default .btn-link:hover{color:#ccc}.navbar-inverse{background-color:#222;border-color:#080808}.navbar-inverse .navbar-brand{color:#9d9d9d}.navbar-inverse .navbar-brand:focus,.navbar-inverse .navbar-brand:hover{color:#fff;background-color:transparent}.navbar-inverse .navbar-text{color:#9d9d9d}.navbar-inverse .navbar-nav>li>a{color:#9d9d9d}.navbar-inverse .navbar-nav>li>a:focus,.navbar-inverse .navbar-nav>li>a:hover{color:#fff;background-color:transparent}.navbar-inverse .navbar-nav>.active>a,.navbar-inverse .navbar-nav>.active>a:focus,.navbar-inverse .navbar-nav>.active>a:hover{color:#fff;background-color:#080808}.navbar-inverse .navbar-nav>.disabled>a,.navbar-inverse .navbar-nav>.disabled>a:focus,.navbar-inverse .navbar-nav>.disabled>a:hover{color:#444;background-color:transparent}.navbar-inverse .navbar-toggle{border-color:#333}.navbar-inverse .navbar-toggle:focus,.navbar-inverse .navbar-toggle:hover{background-color:#333}.navbar-inverse .navbar-toggle .icon-bar{background-color:#fff}.navbar-inverse .navbar-collapse,.navbar-inverse .navbar-form{border-color:#101010}.navbar-inverse .navbar-nav>.open>a,.navbar-inverse .navbar-nav>.open>a:focus,.navbar-inverse .navbar-nav>.open>a:hover{color:#fff;background-color:#080808}@media (max-width:767px){.navbar-inverse .navbar-nav .open .dropdown-menu>.dropdown-header{border-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu .divider{background-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu>li>a{color:#9d9d9d}.navbar-inverse .navbar-nav .open .dropdown-menu>li>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>li>a:hover{color:#fff;background-color:transparent}.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a:hover{color:#fff;background-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a:hover{color:#444;background-color:transparent}}.navbar-inverse .navbar-link{color:#9d9d9d}.navbar-inverse .navbar-link:hover{color:#fff}.navbar-inverse .btn-link{color:#9d9d9d}.navbar-inverse .btn-link:focus,.navbar-inverse .btn-link:hover{color:#fff}.navbar-inverse .btn-link[disabled]:focus,.navbar-inverse .btn-link[disabled]:hover,fieldset[disabled] .navbar-inverse .btn-link:focus,fieldset[disabled] .navbar-inverse .btn-link:hover{color:#444}.breadcrumb{padding:8px 15px;margin-bottom:20px;list-style:none;background-color:#f5f5f5;border-radius:4px}.breadcrumb>li{display:inline-block}.breadcrumb>li+li:before{padding:0 5px;color:#ccc;content:"/\00a0"}.breadcrumb>.active{color:#777}.pagination{display:inline-block;padding-left:0;margin:20px 0;border-radius:4px}.pagination>li{display:inline}.pagination>li>a,.pagination>li>span{position:relative;float:left;padding:6px 12px;margin-left:-1px;line-height:1.42857143;color:#337ab7;text-decoration:none;background-color:#fff;border:1px solid #ddd}.pagination>li:first-child>a,.pagination>li:first-child>span{margin-left:0;border-top-left-radius:4px;border-bottom-left-radius:4px}.pagination>li:last-child>a,.pagination>li:last-child>span{border-top-right-radius:4px;border-bottom-right-radius:4px}.pagination>li>a:focus,.pagination>li>a:hover,.pagination>li>span:focus,.pagination>li>span:hover{color:#23527c;background-color:#eee;border-color:#ddd}.pagination>.active>a,.pagination>.active>a:focus,.pagination>.active>a:hover,.pagination>.active>span,.pagination>.active>span:focus,.pagination>.active>span:hover{z-index:2;color:#fff;cursor:default;background-color:#337ab7;border-color:#337ab7}.pagination>.disabled>a,.pagination>.disabled>a:focus,.pagination>.disabled>a:hover,.pagination>.disabled>span,.pagination>.disabled>span:focus,.pagination>.disabled>span:hover{color:#777;cursor:not-allowed;background-color:#fff;border-color:#ddd}.pagination-lg>li>a,.pagination-lg>li>span{padding:10px 16px;font-size:18px}.pagination-lg>li:first-child>a,.pagination-lg>li:first-child>span{border-top-left-radius:6px;border-bottom-left-radius:6px}.pagination-lg>li:last-child>a,.pagination-lg>li:last-child>span{border-top-right-radius:6px;border-bottom-right-radius:6px}.pagination-sm>li>a,.pagination-sm>li>span{padding:5px 10px;font-size:12px}.pagination-sm>li:first-child>a,.pagination-sm>li:first-child>span{border-top-left-radius:3px;border-bottom-left-radius:3px}.pagination-sm>li:last-child>a,.pagination-sm>li:last-child>span{border-top-right-radius:3px;border-bottom-right-radius:3px}.pager{padding-left:0;margin:20px 0;text-align:center;list-style:none}.pager li{display:inline}.pager li>a,.pager li>span{display:inline-block;padding:5px 14px;background-color:#fff;border:1px solid #ddd;border-radius:15px}.pager li>a:focus,.pager li>a:hover{text-decoration:none;background-color:#eee}.pager .next>a,.pager .next>span{float:right}.pager .previous>a,.pager .previous>span{float:left}.pager .disabled>a,.pager .disabled>a:focus,.pager .disabled>a:hover,.pager .disabled>span{color:#777;cursor:not-allowed;background-color:#fff}.label{display:inline;padding:.2em .6em .3em;font-size:75%;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;border-radius:.25em}a.label:focus,a.label:hover{color:#fff;text-decoration:none;cursor:pointer}.label:empty{display:none}.btn .label{position:relative;top:-1px}.label-default{background-color:#777}.label-default[href]:focus,.label-default[href]:hover{background-color:#5e5e5e}.label-primary{background-color:#337ab7}.label-primary[href]:focus,.label-primary[href]:hover{background-color:#286090}.label-success{background-color:#5cb85c}.label-success[href]:focus,.label-success[href]:hover{background-color:#449d44}.label-info{background-color:#5bc0de}.label-info[href]:focus,.label-info[href]:hover{background-color:#31b0d5}.label-warning{background-color:#f0ad4e}.label-warning[href]:focus,.label-warning[href]:hover{background-color:#ec971f}.label-danger{background-color:#d9534f}.label-danger[href]:focus,.label-danger[href]:hover{background-color:#c9302c}.badge{display:inline-block;min-width:10px;padding:3px 7px;font-size:12px;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;background-color:#777;border-radius:10px}.badge:empty{display:none}.btn .badge{position:relative;top:-1px}.btn-group-xs>.btn .badge,.btn-xs .badge{top:0;padding:1px 5px}a.badge:focus,a.badge:hover{color:#fff;text-decoration:none;cursor:pointer}.list-group-item.active>.badge,.nav-pills>.active>a>.badge{color:#337ab7;background-color:#fff}.list-group-item>.badge{float:right}.list-group-item>.badge+.badge{margin-right:5px}.nav-pills>li>a>.badge{margin-left:3px}.jumbotron{padding:30px 15px;margin-bottom:30px;color:inherit;background-color:#eee}.jumbotron .h1,.jumbotron h1{color:inherit}.jumbotron p{margin-bottom:15px;font-size:21px;font-weight:200}.jumbotron>hr{border-top-color:#d5d5d5}.container .jumbotron,.container-fluid .jumbotron{border-radius:6px}.jumbotron .container{max-width:100%}@media screen and (min-width:768px){.jumbotron{padding:48px 0}.container .jumbotron,.container-fluid .jumbotron{padding-right:60px;padding-left:60px}.jumbotron .h1,.jumbotron h1{font-size:63px}}.thumbnail{display:block;padding:4px;margin-bottom:20px;line-height:1.42857143;background-color:#fff;border:1px solid #ddd;border-radius:4px;-webkit-transition:border .2s ease-in-out;-o-transition:border .2s ease-in-out;transition:border .2s ease-in-out}.thumbnail a>img,.thumbnail>img{margin-right:auto;margin-left:auto}a.thumbnail.active,a.thumbnail:focus,a.thumbnail:hover{border-color:#337ab7}.thumbnail .caption{padding:9px;color:#333}.alert{padding:15px;margin-bottom:20px;border:1px solid transparent;border-radius:4px}.alert h4{margin-top:0;color:inherit}.alert .alert-link{font-weight:700}.alert>p,.alert>ul{margin-bottom:0}.alert>p+p{margin-top:5px}.alert-dismissable,.alert-dismissible{padding-right:35px}.alert-dismissable .close,.alert-dismissible .close{position:relative;top:-2px;right:-21px;color:inherit}.alert-success{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.alert-success hr{border-top-color:#c9e2b3}.alert-success .alert-link{color:#2b542c}.alert-info{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.alert-info hr{border-top-color:#a6e1ec}.alert-info .alert-link{color:#245269}.alert-warning{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.alert-warning hr{border-top-color:#f7e1b5}.alert-warning .alert-link{color:#66512c}.alert-danger{color:#a94442;background-color:#f2dede;border-color:#ebccd1}.alert-danger hr{border-top-color:#e4b9c0}.alert-danger .alert-link{color:#843534}@-webkit-keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}@-o-keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}@keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}.progress{height:20px;margin-bottom:20px;overflow:hidden;background-color:#f5f5f5;border-radius:4px;-webkit-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);box-shadow:inset 0 1px 2px rgba(0,0,0,.1)}.progress-bar{float:left;width:0;height:100%;font-size:12px;line-height:20px;color:#fff;text-align:center;background-color:#337ab7;-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,.15);box-shadow:inset 0 -1px 0 rgba(0,0,0,.15);-webkit-transition:width .6s ease;-o-transition:width .6s ease;transition:width .6s ease}.progress-bar-striped,.progress-striped .progress-bar{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);-webkit-background-size:40px 40px;background-size:40px 40px}.progress-bar.active,.progress.active .progress-bar{-webkit-animation:progress-bar-stripes 2s linear infinite;-o-animation:progress-bar-stripes 2s linear infinite;animation:progress-bar-stripes 2s linear infinite}.progress-bar-success{background-color:#5cb85c}.progress-striped .progress-bar-success{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-info{background-color:#5bc0de}.progress-striped .progress-bar-info{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-warning{background-color:#f0ad4e}.progress-striped .progress-bar-warning{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-danger{background-color:#d9534f}.progress-striped .progress-bar-danger{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.media{margin-top:15px}.media:first-child{margin-top:0}.media,.media-body{overflow:hidden;zoom:1}.media-body{width:10000px}.media-object{display:block}.media-right,.media>.pull-right{padding-left:10px}.media-left,.media>.pull-left{padding-right:10px}.media-body,.media-left,.media-right{display:table-cell;vertical-align:top}.media-middle{vertical-align:middle}.media-bottom{vertical-align:bottom}.media-heading{margin-top:0;margin-bottom:5px}.media-list{padding-left:0;list-style:none}.list-group{padding-left:0;margin-bottom:20px}.list-group-item{position:relative;display:block;padding:10px 15px;margin-bottom:-1px;background-color:#fff;border:1px solid #ddd}.list-group-item:first-child{border-top-left-radius:4px;border-top-right-radius:4px}.list-group-item:last-child{margin-bottom:0;border-bottom-right-radius:4px;border-bottom-left-radius:4px}a.list-group-item{color:#555}a.list-group-item .list-group-item-heading{color:#333}a.list-group-item:focus,a.list-group-item:hover{color:#555;text-decoration:none;background-color:#f5f5f5}.list-group-item.disabled,.list-group-item.disabled:focus,.list-group-item.disabled:hover{color:#777;cursor:not-allowed;background-color:#eee}.list-group-item.disabled .list-group-item-heading,.list-group-item.disabled:focus .list-group-item-heading,.list-group-item.disabled:hover .list-group-item-heading{color:inherit}.list-group-item.disabled .list-group-item-text,.list-group-item.disabled:focus .list-group-item-text,.list-group-item.disabled:hover .list-group-item-text{color:#777}.list-group-item.active,.list-group-item.active:focus,.list-group-item.active:hover{z-index:2;color:#fff;background-color:#337ab7;border-color:#337ab7}.list-group-item.active .list-group-item-heading,.list-group-item.active .list-group-item-heading>.small,.list-group-item.active .list-group-item-heading>small,.list-group-item.active:focus .list-group-item-heading,.list-group-item.active:focus .list-group-item-heading>.small,.list-group-item.active:focus .list-group-item-heading>small,.list-group-item.active:hover .list-group-item-heading,.list-group-item.active:hover .list-group-item-heading>.small,.list-group-item.active:hover .list-group-item-heading>small{color:inherit}.list-group-item.active .list-group-item-text,.list-group-item.active:focus .list-group-item-text,.list-group-item.active:hover .list-group-item-text{color:#c7ddef}.list-group-item-success{color:#3c763d;background-color:#dff0d8}a.list-group-item-success{color:#3c763d}a.list-group-item-success .list-group-item-heading{color:inherit}a.list-group-item-success:focus,a.list-group-item-success:hover{color:#3c763d;background-color:#d0e9c6}a.list-group-item-success.active,a.list-group-item-success.active:focus,a.list-group-item-success.active:hover{color:#fff;background-color:#3c763d;border-color:#3c763d}.list-group-item-info{color:#31708f;background-color:#d9edf7}a.list-group-item-info{color:#31708f}a.list-group-item-info .list-group-item-heading{color:inherit}a.list-group-item-info:focus,a.list-group-item-info:hover{color:#31708f;background-color:#c4e3f3}a.list-group-item-info.active,a.list-group-item-info.active:focus,a.list-group-item-info.active:hover{color:#fff;background-color:#31708f;border-color:#31708f}.list-group-item-warning{color:#8a6d3b;background-color:#fcf8e3}a.list-group-item-warning{color:#8a6d3b}a.list-group-item-warning .list-group-item-heading{color:inherit}a.list-group-item-warning:focus,a.list-group-item-warning:hover{color:#8a6d3b;background-color:#faf2cc}a.list-group-item-warning.active,a.list-group-item-warning.active:focus,a.list-group-item-warning.active:hover{color:#fff;background-color:#8a6d3b;border-color:#8a6d3b}.list-group-item-danger{color:#a94442;background-color:#f2dede}a.list-group-item-danger{color:#a94442}a.list-group-item-danger .list-group-item-heading{color:inherit}a.list-group-item-danger:focus,a.list-group-item-danger:hover{color:#a94442;background-color:#ebcccc}a.list-group-item-danger.active,a.list-group-item-danger.active:focus,a.list-group-item-danger.active:hover{color:#fff;background-color:#a94442;border-color:#a94442}.list-group-item-heading{margin-top:0;margin-bottom:5px}.list-group-item-text{margin-bottom:0;line-height:1.3}.panel{margin-bottom:20px;background-color:#fff;border:1px solid transparent;border-radius:4px;-webkit-box-shadow:0 1px 1px rgba(0,0,0,.05);box-shadow:0 1px 1px rgba(0,0,0,.05)}.panel-body{padding:15px}.panel-heading{padding:10px 15px;border-bottom:1px solid transparent;border-top-left-radius:3px;border-top-right-radius:3px}.panel-heading>.dropdown .dropdown-toggle{color:inherit}.panel-title{margin-top:0;margin-bottom:0;font-size:16px;color:inherit}.panel-title>.small,.panel-title>.small>a,.panel-title>a,.panel-title>small,.panel-title>small>a{color:inherit}.panel-footer{padding:10px 15px;background-color:#f5f5f5;border-top:1px solid #ddd;border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.list-group,.panel>.panel-collapse>.list-group{margin-bottom:0}.panel>.list-group .list-group-item,.panel>.panel-collapse>.list-group .list-group-item{border-width:1px 0;border-radius:0}.panel>.list-group:first-child .list-group-item:first-child,.panel>.panel-collapse>.list-group:first-child .list-group-item:first-child{border-top:0;border-top-left-radius:3px;border-top-right-radius:3px}.panel>.list-group:last-child .list-group-item:last-child,.panel>.panel-collapse>.list-group:last-child .list-group-item:last-child{border-bottom:0;border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel-heading+.list-group .list-group-item:first-child{border-top-width:0}.list-group+.panel-footer{border-top-width:0}.panel>.panel-collapse>.table,.panel>.table,.panel>.table-responsive>.table{margin-bottom:0}.panel>.panel-collapse>.table caption,.panel>.table caption,.panel>.table-responsive>.table caption{padding-right:15px;padding-left:15px}.panel>.table-responsive:first-child>.table:first-child,.panel>.table:first-child{border-top-left-radius:3px;border-top-right-radius:3px}.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child,.panel>.table:first-child>thead:first-child>tr:first-child{border-top-left-radius:3px;border-top-right-radius:3px}.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child td:first-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child th:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child td:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child th:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child td:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child th:first-child,.panel>.table:first-child>thead:first-child>tr:first-child td:first-child,.panel>.table:first-child>thead:first-child>tr:first-child th:first-child{border-top-left-radius:3px}.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child td:last-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child th:last-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child td:last-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child th:last-child,.panel>.table:first-child>tbody:first-child>tr:first-child td:last-child,.panel>.table:first-child>tbody:first-child>tr:first-child th:last-child,.panel>.table:first-child>thead:first-child>tr:first-child td:last-child,.panel>.table:first-child>thead:first-child>tr:first-child th:last-child{border-top-right-radius:3px}.panel>.table-responsive:last-child>.table:last-child,.panel>.table:last-child{border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child{border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child td:first-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child th:first-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child td:first-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child th:first-child,.panel>.table:last-child>tbody:last-child>tr:last-child td:first-child,.panel>.table:last-child>tbody:last-child>tr:last-child th:first-child,.panel>.table:last-child>tfoot:last-child>tr:last-child td:first-child,.panel>.table:last-child>tfoot:last-child>tr:last-child th:first-child{border-bottom-left-radius:3px}.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child td:last-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child th:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child td:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child th:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child td:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child th:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child td:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child th:last-child{border-bottom-right-radius:3px}.panel>.panel-body+.table,.panel>.panel-body+.table-responsive,.panel>.table+.panel-body,.panel>.table-responsive+.panel-body{border-top:1px solid #ddd}.panel>.table>tbody:first-child>tr:first-child td,.panel>.table>tbody:first-child>tr:first-child th{border-top:0}.panel>.table-bordered,.panel>.table-responsive>.table-bordered{border:0}.panel>.table-bordered>tbody>tr>td:first-child,.panel>.table-bordered>tbody>tr>th:first-child,.panel>.table-bordered>tfoot>tr>td:first-child,.panel>.table-bordered>tfoot>tr>th:first-child,.panel>.table-bordered>thead>tr>td:first-child,.panel>.table-bordered>thead>tr>th:first-child,.panel>.table-responsive>.table-bordered>tbody>tr>td:first-child,.panel>.table-responsive>.table-bordered>tbody>tr>th:first-child,.panel>.table-responsive>.table-bordered>tfoot>tr>td:first-child,.panel>.table-responsive>.table-bordered>tfoot>tr>th:first-child,.panel>.table-responsive>.table-bordered>thead>tr>td:first-child,.panel>.table-responsive>.table-bordered>thead>tr>th:first-child{border-left:0}.panel>.table-bordered>tbody>tr>td:last-child,.panel>.table-bordered>tbody>tr>th:last-child,.panel>.table-bordered>tfoot>tr>td:last-child,.panel>.table-bordered>tfoot>tr>th:last-child,.panel>.table-bordered>thead>tr>td:last-child,.panel>.table-bordered>thead>tr>th:last-child,.panel>.table-responsive>.table-bordered>tbody>tr>td:last-child,.panel>.table-responsive>.table-bordered>tbody>tr>th:last-child,.panel>.table-responsive>.table-bordered>tfoot>tr>td:last-child,.panel>.table-responsive>.table-bordered>tfoot>tr>th:last-child,.panel>.table-responsive>.table-bordered>thead>tr>td:last-child,.panel>.table-responsive>.table-bordered>thead>tr>th:last-child{border-right:0}.panel>.table-bordered>tbody>tr:first-child>td,.panel>.table-bordered>tbody>tr:first-child>th,.panel>.table-bordered>thead>tr:first-child>td,.panel>.table-bordered>thead>tr:first-child>th,.panel>.table-responsive>.table-bordered>tbody>tr:first-child>td,.panel>.table-responsive>.table-bordered>tbody>tr:first-child>th,.panel>.table-responsive>.table-bordered>thead>tr:first-child>td,.panel>.table-responsive>.table-bordered>thead>tr:first-child>th{border-bottom:0}.panel>.table-bordered>tbody>tr:last-child>td,.panel>.table-bordered>tbody>tr:last-child>th,.panel>.table-bordered>tfoot>tr:last-child>td,.panel>.table-bordered>tfoot>tr:last-child>th,.panel>.table-responsive>.table-bordered>tbody>tr:last-child>td,.panel>.table-responsive>.table-bordered>tbody>tr:last-child>th,.panel>.table-responsive>.table-bordered>tfoot>tr:last-child>td,.panel>.table-responsive>.table-bordered>tfoot>tr:last-child>th{border-bottom:0}.panel>.table-responsive{margin-bottom:0;border:0}.panel-group{margin-bottom:20px}.panel-group .panel{margin-bottom:0;border-radius:4px}.panel-group .panel+.panel{margin-top:5px}.panel-group .panel-heading{border-bottom:0}.panel-group .panel-heading+.panel-collapse>.list-group,.panel-group .panel-heading+.panel-collapse>.panel-body{border-top:1px solid #ddd}.panel-group .panel-footer{border-top:0}.panel-group .panel-footer+.panel-collapse .panel-body{border-bottom:1px solid #ddd}.panel-default{border-color:#ddd}.panel-default>.panel-heading{color:#333;background-color:#f5f5f5;border-color:#ddd}.panel-default>.panel-heading+.panel-collapse>.panel-body{border-top-color:#ddd}.panel-default>.panel-heading .badge{color:#f5f5f5;background-color:#333}.panel-default>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#ddd}.panel-primary{border-color:#337ab7}.panel-primary>.panel-heading{color:#fff;background-color:#337ab7;border-color:#337ab7}.panel-primary>.panel-heading+.panel-collapse>.panel-body{border-top-color:#337ab7}.panel-primary>.panel-heading .badge{color:#337ab7;background-color:#fff}.panel-primary>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#337ab7}.panel-success{border-color:#d6e9c6}.panel-success>.panel-heading{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.panel-success>.panel-heading+.panel-collapse>.panel-body{border-top-color:#d6e9c6}.panel-success>.panel-heading .badge{color:#dff0d8;background-color:#3c763d}.panel-success>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#d6e9c6}.panel-info{border-color:#bce8f1}.panel-info>.panel-heading{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.panel-info>.panel-heading+.panel-collapse>.panel-body{border-top-color:#bce8f1}.panel-info>.panel-heading .badge{color:#d9edf7;background-color:#31708f}.panel-info>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#bce8f1}.panel-warning{border-color:#faebcc}.panel-warning>.panel-heading{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.panel-warning>.panel-heading+.panel-collapse>.panel-body{border-top-color:#faebcc}.panel-warning>.panel-heading .badge{color:#fcf8e3;background-color:#8a6d3b}.panel-warning>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#faebcc}.panel-danger{border-color:#ebccd1}.panel-danger>.panel-heading{color:#a94442;background-color:#f2dede;border-color:#ebccd1}.panel-danger>.panel-heading+.panel-collapse>.panel-body{border-top-color:#ebccd1}.panel-danger>.panel-heading .badge{color:#f2dede;background-color:#a94442}.panel-danger>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#ebccd1}.embed-responsive{position:relative;display:block;height:0;padding:0;overflow:hidden}.embed-responsive .embed-responsive-item,.embed-responsive embed,.embed-responsive iframe,.embed-responsive object,.embed-responsive video{position:absolute;top:0;bottom:0;left:0;width:100%;height:100%;border:0}.embed-responsive-16by9{padding-bottom:56.25%}.embed-responsive-4by3{padding-bottom:75%}.well{min-height:20px;padding:19px;margin-bottom:20px;background-color:#f5f5f5;border:1px solid #e3e3e3;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.05);box-shadow:inset 0 1px 1px rgba(0,0,0,.05)}.well blockquote{border-color:#ddd;border-color:rgba(0,0,0,.15)}.well-lg{padding:24px;border-radius:6px}.well-sm{padding:9px;border-radius:3px}.close{float:right;font-size:21px;font-weight:700;line-height:1;color:#000;text-shadow:0 1px 0 #fff;filter:alpha(opacity=20);opacity:.2}.close:focus,.close:hover{color:#000;text-decoration:none;cursor:pointer;filter:alpha(opacity=50);opacity:.5}button.close{-webkit-appearance:none;padding:0;cursor:pointer;background:0 0;border:0}.modal-open{overflow:hidden}.modal{position:fixed;top:0;right:0;bottom:0;left:0;z-index:1050;display:none;overflow:hidden;-webkit-overflow-scrolling:touch;outline:0}.modal.fade .modal-dialog{-webkit-transition:-webkit-transform .3s ease-out;-o-transition:-o-transform .3s ease-out;transition:transform .3s ease-out;-webkit-transform:translate(0,-25%);-ms-transform:translate(0,-25%);-o-transform:translate(0,-25%);transform:translate(0,-25%)}.modal.in .modal-dialog{-webkit-transform:translate(0,0);-ms-transform:translate(0,0);-o-transform:translate(0,0);transform:translate(0,0)}.modal-open .modal{overflow-x:hidden;overflow-y:auto}.modal-dialog{position:relative;width:auto;margin:10px}.modal-content{position:relative;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #999;border:1px solid rgba(0,0,0,.2);border-radius:6px;outline:0;-webkit-box-shadow:0 3px 9px rgba(0,0,0,.5);box-shadow:0 3px 9px rgba(0,0,0,.5)}.modal-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;z-index:1040;background-color:#000}.modal-backdrop.fade{filter:alpha(opacity=0);opacity:0}.modal-backdrop.in{filter:alpha(opacity=50);opacity:.5}.modal-header{min-height:16.43px;padding:15px;border-bottom:1px solid #e5e5e5}.modal-header .close{margin-top:-2px}.modal-title{margin:0;line-height:1.42857143}.modal-body{position:relative;padding:15px}.modal-footer{padding:15px;text-align:right;border-top:1px solid #e5e5e5}.modal-footer .btn+.btn{margin-bottom:0;margin-left:5px}.modal-footer .btn-group .btn+.btn{margin-left:-1px}.modal-footer .btn-block+.btn-block{margin-left:0}.modal-scrollbar-measure{position:absolute;top:-9999px;width:50px;height:50px;overflow:scroll}@media (min-width:768px){.modal-dialog{width:600px;margin:30px auto}.modal-content{-webkit-box-shadow:0 5px 15px rgba(0,0,0,.5);box-shadow:0 5px 15px rgba(0,0,0,.5)}.modal-sm{width:300px}}@media (min-width:992px){.modal-lg{width:900px}}.tooltip{position:absolute;z-index:1070;display:block;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:12px;font-weight:400;line-height:1.4;filter:alpha(opacity=0);opacity:0}.tooltip.in{filter:alpha(opacity=90);opacity:.9}.tooltip.top{padding:5px 0;margin-top:-3px}.tooltip.right{padding:0 5px;margin-left:3px}.tooltip.bottom{padding:5px 0;margin-top:3px}.tooltip.left{padding:0 5px;margin-left:-3px}.tooltip-inner{max-width:200px;padding:3px 8px;color:#fff;text-align:center;text-decoration:none;background-color:#000;border-radius:4px}.tooltip-arrow{position:absolute;width:0;height:0;border-color:transparent;border-style:solid}.tooltip.top .tooltip-arrow{bottom:0;left:50%;margin-left:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.top-left .tooltip-arrow{right:5px;bottom:0;margin-bottom:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.top-right .tooltip-arrow{bottom:0;left:5px;margin-bottom:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.right .tooltip-arrow{top:50%;left:0;margin-top:-5px;border-width:5px 5px 5px 0;border-right-color:#000}.tooltip.left .tooltip-arrow{top:50%;right:0;margin-top:-5px;border-width:5px 0 5px 5px;border-left-color:#000}.tooltip.bottom .tooltip-arrow{top:0;left:50%;margin-left:-5px;border-width:0 5px 5px;border-bottom-color:#000}.tooltip.bottom-left .tooltip-arrow{top:0;right:5px;margin-top:-5px;border-width:0 5px 5px;border-bottom-color:#000}.tooltip.bottom-right .tooltip-arrow{top:0;left:5px;margin-top:-5px;border-width:0 5px 5px;border-bottom-color:#000}.popover{position:absolute;top:0;left:0;z-index:1060;display:none;max-width:276px;padding:1px;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;font-weight:400;line-height:1.42857143;text-align:left;white-space:normal;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.2);border-radius:6px;-webkit-box-shadow:0 5px 10px rgba(0,0,0,.2);box-shadow:0 5px 10px rgba(0,0,0,.2)}.popover.top{margin-top:-10px}.popover.right{margin-left:10px}.popover.bottom{margin-top:10px}.popover.left{margin-left:-10px}.popover-title{padding:8px 14px;margin:0;font-size:14px;background-color:#f7f7f7;border-bottom:1px solid #ebebeb;border-radius:5px 5px 0 0}.popover-content{padding:9px 14px}.popover>.arrow,.popover>.arrow:after{position:absolute;display:block;width:0;height:0;border-color:transparent;border-style:solid}.popover>.arrow{border-width:11px}.popover>.arrow:after{content:"";border-width:10px}.popover.top>.arrow{bottom:-11px;left:50%;margin-left:-11px;border-top-color:#999;border-top-color:rgba(0,0,0,.25);border-bottom-width:0}.popover.top>.arrow:after{bottom:1px;margin-left:-10px;content:" ";border-top-color:#fff;border-bottom-width:0}.popover.right>.arrow{top:50%;left:-11px;margin-top:-11px;border-right-color:#999;border-right-color:rgba(0,0,0,.25);border-left-width:0}.popover.right>.arrow:after{bottom:-10px;left:1px;content:" ";border-right-color:#fff;border-left-width:0}.popover.bottom>.arrow{top:-11px;left:50%;margin-left:-11px;border-top-width:0;border-bottom-color:#999;border-bottom-color:rgba(0,0,0,.25)}.popover.bottom>.arrow:after{top:1px;margin-left:-10px;content:" ";border-top-width:0;border-bottom-color:#fff}.popover.left>.arrow{top:50%;right:-11px;margin-top:-11px;border-right-width:0;border-left-color:#999;border-left-color:rgba(0,0,0,.25)}.popover.left>.arrow:after{right:1px;bottom:-10px;content:" ";border-right-width:0;border-left-color:#fff}.carousel{position:relative}.carousel-inner{position:relative;width:100%;overflow:hidden}.carousel-inner>.item{position:relative;display:none;-webkit-transition:.6s ease-in-out left;-o-transition:.6s ease-in-out left;transition:.6s ease-in-out left}.carousel-inner>.item>a>img,.carousel-inner>.item>img{line-height:1}@media all and (transform-3d),(-webkit-transform-3d){.carousel-inner>.item{-webkit-transition:-webkit-transform .6s ease-in-out;-o-transition:-o-transform .6s ease-in-out;transition:transform .6s ease-in-out;-webkit-backface-visibility:hidden;backface-visibility:hidden;-webkit-perspective:1000;perspective:1000}.carousel-inner>.item.active.right,.carousel-inner>.item.next{left:0;-webkit-transform:translate3d(100%,0,0);transform:translate3d(100%,0,0)}.carousel-inner>.item.active.left,.carousel-inner>.item.prev{left:0;-webkit-transform:translate3d(-100%,0,0);transform:translate3d(-100%,0,0)}.carousel-inner>.item.active,.carousel-inner>.item.next.left,.carousel-inner>.item.prev.right{left:0;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}}.carousel-inner>.active,.carousel-inner>.next,.carousel-inner>.prev{display:block}.carousel-inner>.active{left:0}.carousel-inner>.next,.carousel-inner>.prev{position:absolute;top:0;width:100%}.carousel-inner>.next{left:100%}.carousel-inner>.prev{left:-100%}.carousel-inner>.next.left,.carousel-inner>.prev.right{left:0}.carousel-inner>.active.left{left:-100%}.carousel-inner>.active.right{left:100%}.carousel-control{position:absolute;top:0;bottom:0;left:0;width:15%;font-size:20px;color:#fff;text-align:center;text-shadow:0 1px 2px rgba(0,0,0,.6);filter:alpha(opacity=50);opacity:.5}.carousel-control.left{background-image:-webkit-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);background-image:-o-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);background-image:-webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.5)),to(rgba(0,0,0,.0001)));background-image:linear-gradient(to right,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#00000000', GradientType=1);background-repeat:repeat-x}.carousel-control.right{right:0;left:auto;background-image:-webkit-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);background-image:-o-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);background-image:-webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.0001)),to(rgba(0,0,0,.5)));background-image:linear-gradient(to right,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#80000000', GradientType=1);background-repeat:repeat-x}.carousel-control:focus,.carousel-control:hover{color:#fff;text-decoration:none;filter:alpha(opacity=90);outline:0;opacity:.9}.carousel-control .glyphicon-chevron-left,.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next,.carousel-control .icon-prev{position:absolute;top:50%;z-index:5;display:inline-block}.carousel-control .glyphicon-chevron-left,.carousel-control .icon-prev{left:50%;margin-left:-10px}.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next{right:50%;margin-right:-10px}.carousel-control .icon-next,.carousel-control .icon-prev{width:20px;height:20px;margin-top:-10px;font-family:serif;line-height:1}.carousel-control .icon-prev:before{content:'\2039'}.carousel-control .icon-next:before{content:'\203a'}.carousel-indicators{position:absolute;bottom:10px;left:50%;z-index:15;width:60%;padding-left:0;margin-left:-30%;text-align:center;list-style:none}.carousel-indicators li{display:inline-block;width:10px;height:10px;margin:1px;text-indent:-999px;cursor:pointer;background-color:#000 \9;background-color:rgba(0,0,0,0);border:1px solid #fff;border-radius:10px}.carousel-indicators .active{width:12px;height:12px;margin:0;background-color:#fff}.carousel-caption{position:absolute;right:15%;bottom:20px;left:15%;z-index:10;padding-top:20px;padding-bottom:20px;color:#fff;text-align:center;text-shadow:0 1px 2px rgba(0,0,0,.6)}.carousel-caption .btn{text-shadow:none}@media screen and (min-width:768px){.carousel-control .glyphicon-chevron-left,.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next,.carousel-control .icon-prev{width:30px;height:30px;margin-top:-15px;font-size:30px}.carousel-control .glyphicon-chevron-left,.carousel-control .icon-prev{margin-left:-15px}.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next{margin-right:-15px}.carousel-caption{right:20%;left:20%;padding-bottom:30px}.carousel-indicators{bottom:20px}}.btn-group-vertical>.btn-group:after,.btn-group-vertical>.btn-group:before,.btn-toolbar:after,.btn-toolbar:before,.clearfix:after,.clearfix:before,.container-fluid:after,.container-fluid:before,.container:after,.container:before,.dl-horizontal dd:after,.dl-horizontal dd:before,.form-horizontal .form-group:after,.form-horizontal .form-group:before,.modal-footer:after,.modal-footer:before,.nav:after,.nav:before,.navbar-collapse:after,.navbar-collapse:before,.navbar-header:after,.navbar-header:before,.navbar:after,.navbar:before,.pager:after,.pager:before,.panel-body:after,.panel-body:before,.row:after,.row:before{display:table;content:" "}.btn-group-vertical>.btn-group:after,.btn-toolbar:after,.clearfix:after,.container-fluid:after,.container:after,.dl-horizontal dd:after,.form-horizontal .form-group:after,.modal-footer:after,.nav:after,.navbar-collapse:after,.navbar-header:after,.navbar:after,.pager:after,.panel-body:after,.row:after{clear:both}.center-block{display:block;margin-right:auto;margin-left:auto}.pull-right{float:right!important}.pull-left{float:left!important}.hide{display:none!important}.show{display:block!important}.invisible{visibility:hidden}.text-hide{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0}.hidden{display:none!important}.affix{position:fixed}@-ms-viewport{width:device-width}.visible-lg,.visible-md,.visible-sm,.visible-xs{display:none!important}.visible-lg-block,.visible-lg-inline,.visible-lg-inline-block,.visible-md-block,.visible-md-inline,.visible-md-inline-block,.visible-sm-block,.visible-sm-inline,.visible-sm-inline-block,.visible-xs-block,.visible-xs-inline,.visible-xs-inline-block{display:none!important}@media (max-width:767px){.visible-xs{display:block!important}table.visible-xs{display:table}tr.visible-xs{display:table-row!important}td.visible-xs,th.visible-xs{display:table-cell!important}}@media (max-width:767px){.visible-xs-block{display:block!important}}@media (max-width:767px){.visible-xs-inline{display:inline!important}}@media (max-width:767px){.visible-xs-inline-block{display:inline-block!important}}@media (min-width:768px)and (max-width:991px){.visible-sm{display:block!important}table.visible-sm{display:table}tr.visible-sm{display:table-row!important}td.visible-sm,th.visible-sm{display:table-cell!important}}@media (min-width:768px)and (max-width:991px){.visible-sm-block{display:block!important}}@media (min-width:768px)and (max-width:991px){.visible-sm-inline{display:inline!important}}@media (min-width:768px)and (max-width:991px){.visible-sm-inline-block{display:inline-block!important}}@media (min-width:992px)and (max-width:1199px){.visible-md{display:block!important}table.visible-md{display:table}tr.visible-md{display:table-row!important}td.visible-md,th.visible-md{display:table-cell!important}}@media (min-width:992px)and (max-width:1199px){.visible-md-block{display:block!important}}@media (min-width:992px)and (max-width:1199px){.visible-md-inline{display:inline!important}}@media (min-width:992px)and (max-width:1199px){.visible-md-inline-block{display:inline-block!important}}@media (min-width:1200px){.visible-lg{display:block!important}table.visible-lg{display:table}tr.visible-lg{display:table-row!important}td.visible-lg,th.visible-lg{display:table-cell!important}}@media (min-width:1200px){.visible-lg-block{display:block!important}}@media (min-width:1200px){.visible-lg-inline{display:inline!important}}@media (min-width:1200px){.visible-lg-inline-block{display:inline-block!important}}@media (max-width:767px){.hidden-xs{display:none!important}}@media (min-width:768px)and (max-width:991px){.hidden-sm{display:none!important}}@media (min-width:992px)and (max-width:1199px){.hidden-md{display:none!important}}@media (min-width:1200px){.hidden-lg{display:none!important}}.visible-print{display:none!important}@media print{.visible-print{display:block!important}table.visible-print{display:table}tr.visible-print{display:table-row!important}td.visible-print,th.visible-print{display:table-cell!important}}.visible-print-block{display:none!important}@media print{.visible-print-block{display:block!important}}.visible-print-inline{display:none!important}@media print{.visible-print-inline{display:inline!important}}.visible-print-inline-block{display:none!important}@media print{.visible-print-inline-block{display:inline-block!important}}@media print{.hidden-print{display:none!important}} \ No newline at end of file diff --git a/iguana/iguana777.c b/iguana/iguana777.c new file mode 100755 index 000000000..28935cfea --- /dev/null +++ b/iguana/iguana777.c @@ -0,0 +1,565 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#include "iguana777.h" +const char *Hardcoded_coins[][3] = { { "BTC", "bitcoin", "0" }, { "BTCD", "BitcoinDark", "129" } }; + +struct iguana_info *iguana_coinfind(const char *symbol) +{ + int32_t i; + for (i=0; isymbol,symbol) == 0 ) + return(Coins[i]); + } + return(0); +} + +struct iguana_info *iguana_coinadd(const char *symbol) +{ + struct iguana_info *coin; int32_t i = 0; + if ( symbol == 0 ) + { + for (i=0; i %p\n",Coins[i]); + return(Coins[i]); + } return(0); + printf("i.%d (%s) vs name.(%s)\n",i,Coins[i]->name,symbol); + } + } + else + { + for (i=0; i= sizeof(Hardcoded_coins)/sizeof(*Hardcoded_coins) || Hardcoded_coins[i][0] == 0 ) + break; + if ( strcmp(symbol,Hardcoded_coins[i][0]) == 0 ) + { + if ( Coins[i] == 0 ) + Coins[i] = mycalloc('c',1,sizeof(*Coins[i])); + coin = Coins[i]; + if ( coin->chain == 0 ) + { + strcpy(coin->name,Hardcoded_coins[i][1]); + //coin->myservices = atoi(Hardcoded_coins[i][2]); + strcpy(coin->symbol,symbol); + coin->chain = iguana_chainfind(coin->symbol); + iguana_initcoin(coin); + } + return(coin); + } + } + } + return(0); +} + +struct iguana_info *iguana_coinselect() +{ + int32_t i; + for (i=0; isymbol[0] != 0 && Coins[i]->bundlescount > 0 ) + return(Coins[i]); + } + return(0); +} + +void iguana_recvalloc(struct iguana_info *coin,int32_t numitems) +{ + //coin->blocks.ptrs = myrealloc('W',coin->blocks.ptrs,coin->blocks.ptrs==0?0:coin->blocks.maxbits * sizeof(*coin->blocks.ptrs),numitems * sizeof(*coin->blocks.ptrs)); + coin->blocks.RO = myrealloc('W',coin->blocks.RO,coin->blocks.RO==0?0:coin->blocks.maxbits * sizeof(*coin->blocks.RO),numitems * sizeof(*coin->blocks.RO)); + printf("realloc waitingbits.%d -> %d\n",coin->blocks.maxbits,numitems); + coin->blocks.maxbits = numitems; +} + +static int _decreasing_double(const void *a,const void *b) +{ +#define double_a (*(double *)a) +#define double_b (*(double *)b) + if ( double_b > double_a ) + return(1); + else if ( double_b < double_a ) + return(-1); + return(0); +#undef double_a +#undef double_b +} + +static int32_t revsortds(double *buf,uint32_t num,int32_t size) +{ + qsort(buf,num,size,_decreasing_double); + return(0); +} + +double iguana_metric(struct iguana_peer *addr,uint32_t now,double decay) +{ + int32_t duration; double metric = addr->recvblocks * addr->recvtotal; + addr->recvblocks *= decay; + addr->recvtotal *= decay; + if ( now >= addr->ready && addr->ready != 0 ) + duration = (now - addr->ready + 1); + else duration = 1; + if ( metric < SMALLVAL && duration > 300 ) + metric = 0.001; + else metric /= duration; + return(metric); +} + +int32_t iguana_peermetrics(struct iguana_info *coin) +{ + int32_t i,ind,n; double *sortbuf,sum; uint32_t now; struct iguana_peer *addr,*slowest = 0; + //printf("peermetrics\n"); + sortbuf = mycalloc('s',coin->MAXPEERS,sizeof(double)*2); + coin->peers.mostreceived = 0; + now = (uint32_t)time(NULL); + for (i=n=0; iMAXPEERS; i++) + { + addr = &coin->peers.active[i]; + if ( addr->usock < 0 || addr->dead != 0 || addr->ready == 0 ) + continue; + if ( addr->recvblocks > coin->peers.mostreceived ) + coin->peers.mostreceived = addr->recvblocks; + //printf("[%.0f %.0f] ",addr->recvblocks,addr->recvtotal); + sortbuf[n*2 + 0] = iguana_metric(addr,now,1.); + sortbuf[n*2 + 1] = i; + n++; + } + if ( n > 0 ) + { + revsortds(sortbuf,n,sizeof(double)*2); + portable_mutex_lock(&coin->peers_mutex); + for (sum=i=0; iMAXPEERS ) + { + coin->peers.topmetrics[i] = sortbuf[i*2]; + ind = (int32_t)sortbuf[i*2 +1]; + coin->peers.ranked[i] = &coin->peers.active[ind]; + if ( sortbuf[i*2] > SMALLVAL && (double)i/n > .8 ) + slowest = coin->peers.ranked[i]; + //printf("(%.5f %s) ",sortbuf[i*2],coin->peers.ranked[i]->ipaddr); + coin->peers.ranked[i]->rank = i + 1; + sum += coin->peers.topmetrics[i]; + } + } + coin->peers.numranked = n; + portable_mutex_unlock(&coin->peers_mutex); + //printf("NUMRANKED.%d\n",n); + if ( i > 0 ) + { + coin->peers.avemetric = (sum / i); + if ( i >= (coin->MAXPEERS - 1) && slowest != 0 ) + { + printf("prune slowest peer.(%s) numranked.%d\n",slowest->ipaddr,n); + slowest->dead = 1; + } + } + } + myfree(sortbuf,coin->MAXPEERS * sizeof(double) * 2); + return(coin->peers.mostreceived); +} + +void *iguana_kviAddriterator(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) +{ + char ipaddr[64]; int32_t i; FILE *fp = (FILE *)args; struct iguana_peer *addr; struct iguana_iAddr *iA = value; + if ( fp != 0 && iA != 0 && iA->numconnects > 0 && iA->lastconnect > time(NULL)-IGUANA_RECENTPEER ) + { + for (i=0; ipeers.numranked; i++) + if ( (addr= coin->peers.ranked[i]) != 0 && addr->ipbits == iA->ipbits ) + break; + if ( i == coin->peers.numranked ) + { + expand_ipbits(ipaddr,iA->ipbits); + fprintf(fp,"%s\n",ipaddr); + } + } + return(0); +} + +uint32_t iguana_updatemetrics(struct iguana_info *coin) +{ + char fname[512],tmpfname[512],oldfname[512]; int32_t i; struct iguana_peer *addr; FILE *fp; + iguana_peermetrics(coin); + sprintf(fname,"%s_peers.txt",coin->symbol), OS_compatible_path(fname); + sprintf(oldfname,"%s_oldpeers.txt",coin->symbol), OS_compatible_path(oldfname); + sprintf(tmpfname,"tmp/%s/peers.txt",coin->symbol), OS_compatible_path(tmpfname); + if ( (fp= fopen(tmpfname,"w")) != 0 ) + { + for (i=0; ipeers.numranked; i++) + if ( (addr= coin->peers.ranked[i]) != 0 ) + fprintf(fp,"%s\n",addr->ipaddr); + if ( ftell(fp) > OS_filesize(fname) ) + { + printf("new peers.txt %ld vs (%s) %ld\n",ftell(fp),fname,(long)OS_filesize(fname)); + fclose(fp); + OS_renamefile(fname,oldfname); + OS_copyfile(tmpfname,fname,1); + } else fclose(fp); + } + return((uint32_t)time(NULL)); +} + +void iguana_emitQ(struct iguana_info *coin,struct iguana_bundle *bp) +{ + struct iguana_helper *ptr; + ptr = mycalloc('i',1,sizeof(*ptr)); + ptr->allocsize = sizeof(*ptr); + ptr->coin = coin; + ptr->bp = bp, ptr->hdrsi = bp->hdrsi; + ptr->type = 'E'; + ptr->starttime = (uint32_t)time(NULL); + //printf("%s EMIT.%d[%d] emitfinish.%u\n",coin->symbol,ptr->hdrsi,bp->n,bp->emitfinish); + queue_enqueue("helperQ",&helperQ,&ptr->DL,0); +} + +void iguana_mergeQ(struct iguana_info *coin,struct iguana_bundle *bp,struct iguana_bundle *nextbp) +{ + struct iguana_helper *ptr; + ptr = mycalloc('i',1,sizeof(*ptr)); + ptr->allocsize = sizeof(*ptr); + ptr->coin = coin; + ptr->bp = bp, ptr->hdrsi = bp->hdrsi; + ptr->nextbp = nextbp; + ptr->type = 'M'; + ptr->starttime = (uint32_t)time(NULL); + //printf("%s EMIT.%d[%d] emitfinish.%u\n",coin->symbol,ptr->hdrsi,bp->n,bp->emitfinish); + queue_enqueue("helperQ",&helperQ,&ptr->DL,0); +} + +int32_t iguana_helpertask(FILE *fp,struct OS_memspace *mem,struct OS_memspace *memB,struct iguana_helper *ptr) +{ + struct iguana_info *coin; struct iguana_peer *addr; struct iguana_bundle *bp,*nextbp; + coin = ptr->coin, addr = ptr->addr; + if ( 0 && ptr->type == 'M' ) + { + if ( (coin= ptr->coin) != 0 ) + { + if ( (bp= ptr->bp) != 0 && (nextbp= ptr->nextbp) != 0 ) + { + bp->mergefinish = nextbp->mergefinish = (uint32_t)time(NULL); + if ( iguana_bundlemergeHT(coin,mem,memB,bp,nextbp,ptr->starttime) < 0 ) + bp->mergefinish = nextbp->mergefinish = 0; + } + } + } + else if ( ptr->type == 'E' ) + { + //printf("emitQ coin.%p bp.%p\n",ptr->coin,ptr->bp); + if ( (coin= ptr->coin) != 0 ) + { + if ( (bp= ptr->bp) != 0 ) + { + if ( iguana_bundlesaveHT(coin,mem,memB,bp,ptr->starttime) == 0 ) + { + bp->emitfinish = (uint32_t)time(NULL); + coin->numemitted++; + } + else bp->emitfinish = 0; + } else printf("error missing bp in emit\n"); + } else printf("no coin in helper request?\n"); + } + return(0); +} + +void iguana_helper(void *arg) +{ + FILE *fp = 0; char fname[512],name[64],*helpername = 0; cJSON *argjson=0; int32_t i,flag; + struct iguana_helper *ptr; struct iguana_info *coin; struct OS_memspace MEM,*MEMB; + if ( arg != 0 && (argjson= cJSON_Parse(arg)) != 0 ) + helpername = jstr(argjson,"name"); + if ( helpername == 0 ) + { + sprintf(name,"helper.%d",rand()); + helpername = name; + } + sprintf(fname,"tmp/%s",helpername); + OS_compatible_path(fname); + fp = fopen(fname,"wb"); + if ( argjson != 0 ) + free_json(argjson); + memset(&MEM,0,sizeof(MEM)); + MEMB = mycalloc('b',IGUANA_MAXBUNDLESIZE,sizeof(*MEMB)); + while ( 1 ) + { + flag = 0; + if ( (ptr= queue_dequeue(&helperQ,0)) != 0 ) + { + if ( (coin= ptr->coin) != 0 && myallocated(0,-1) > coin->MAXMEM ) + queue_enqueue("reQ",&helperQ,&ptr->DL,0); + else + { + iguana_helpertask(fp,&MEM,MEMB,ptr); + myfree(ptr,ptr->allocsize); + } + flag++; + } + if ( flag == 0 ) + { + for (i=0; ilaunched != 0 ) + flag += iguana_rpctest(coin); + } + if ( flag == 0 ) + usleep(10000); + } + } +} + +void iguana_coinloop(void *arg) +{ + struct iguana_info *coin,**coins = arg; + struct iguana_bundle *bp; int32_t flag,i,n,bundlei; bits256 zero; char str[1024]; + uint32_t now,lastdisp = 0; + n = (int32_t)(long)coins[0]; + coins++; + printf("begin coinloop[%d]\n",n); + for (i=0; istarted == 0 ) + { + iguana_startcoin(coin,coin->initialheight,coin->mapflags); + printf("init.(%s) maxpeers.%d maxrecvcache.%s services.%llx MAXMEM.%s polltimeout.%d\n",coin->symbol,coin->MAXPEERS,mbstr(str,coin->MAXRECVCACHE),(long long)coin->myservices,mbstr(str,coin->MAXMEM),coin->polltimeout); + coin->started = coin; + coin->chain->minconfirms = coin->minconfirms; + } + } + coin = coins[0]; + iguana_rwiAddrind(coin,0,0,0); + iguana_possible_peer(coin,"127.0.0.1"); + memset(zero.bytes,0,sizeof(zero)); + if ( (bp= iguana_bundlecreate(coin,&bundlei,0,*(bits256 *)coin->chain->genesis_hashdata,zero,1)) != 0 ) + bp->bundleheight = 0; + while ( 1 ) + { + flag = 0; + for (i=0; inewramchain != 0 && now > coin->savedblocks+60 ) + { + char fname[512]; FILE *fp; + sprintf(fname,"blocks.%s",coin->symbol), OS_compatible_path(fname); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + if ( fwrite(coin->blocks.RO,sizeof(*coin->blocks.RO),coin->longestchain,fp) != coin->longestchain ) + printf("error saving blocks\n"); + else printf("%s saved\n",fname); + fclose(fp); + coin->savedblocks = (uint32_t)time(NULL); + } + } + if ( coin->isRT == 0 && now > coin->startutc+600 && coin->blocksrecv >= coin->longestchain-1 && coin->blocks.hwmchain.height >= coin->longestchain-1 ) + { + printf(">>>>>>> %s isRT blockrecv.%d vs longest.%d\n",coin->symbol,coin->blocksrecv,coin->longestchain); + coin->isRT = 1; + if ( coin->polltimeout > 100 ) + coin->polltimeout = 100; + coin->MAXPEERS = 8; + } + if ( now > coin->lastpossible ) + coin->lastpossible = iguana_possible_peer(coin,0); // tries to connect to new peers + if ( coin->active != 0 ) + { + if ( now > coin->peers.lastmetrics+6 ) + coin->peers.lastmetrics = iguana_updatemetrics(coin); // ranks peers + flag += iguana_processrecv(coin); + if ( 0 && coin->blocks.parsedblocks < coin->blocks.hwmchain.height-coin->chain->minconfirms ) + { + if ( iguana_updateramchain(coin) != 0 ) + iguana_syncs(coin), flag++; // merge ramchain fragments into full ramchain + } + //if ( now > lastdisp+1 ) + { + lastdisp = (uint32_t)now; + iguana_bundlestats(coin,str); + if ( str[0] != 0 ) + { + if ( (rand() % 30) == 0 ) + myallocated(0,0); + } + } + iguana_ramchainmerge(coin); + } + }// bp block needs mutex + } + if ( flag == 0 ) + { + //printf("IDLE\n"); + usleep(coin->polltimeout * 10000); + } + } +} + +void iguana_coinargs(char *symbol,int64_t *maxrecvcachep,int32_t *minconfirmsp,int32_t *maxpeersp,int32_t *initialheightp,uint64_t *servicesp,int32_t *maxpendingp,int32_t *maxbundlesp,cJSON *json) +{ + if ( (*maxrecvcachep= j64bits(json,"maxrecvcache")) != 0 ) + *maxrecvcachep *= 1024 * 1024 * 1024L; + *minconfirmsp = juint(json,"minconfirms"); + *maxpeersp = juint(json,"maxpeers"); + *maxpendingp = juint(json,"maxpending"); + *maxbundlesp = juint(json,"maxbundles"); + if ( (*initialheightp= juint(json,"initialheight")) == 0 ) + *initialheightp = (strcmp(symbol,"BTC") == 0) ? 400000 : 100000; + *servicesp = j64bits(json,"services"); +} + +struct iguana_info *iguana_setcoin(char *symbol,void *launched,int32_t maxpeers,int64_t maxrecvcache,uint64_t services,int32_t initialheight,int32_t maphash,int32_t minconfirms,int32_t maxpending,int32_t maxbundles,cJSON *json) +{ + struct iguana_chain *iguana_createchain(cJSON *json); + struct iguana_info *coin; int32_t j,m,mapflags; char dirname[512]; cJSON *peers; + mapflags = IGUANA_MAPRECVDATA | maphash*IGUANA_MAPTXIDITEMS | maphash*IGUANA_MAPPKITEMS | maphash*IGUANA_MAPBLOCKITEMS | maphash*IGUANA_MAPPEERITEMS; + coin = iguana_coinadd(symbol); + coin->launched = launched; + if ( (coin->MAXPEERS= maxpeers) <= 0 ) + coin->MAXPEERS = (strcmp(symbol,"BTC") == 0) ? 64 : 32; + if ( (coin->MAXRECVCACHE= maxrecvcache) == 0 ) + coin->MAXRECVCACHE = IGUANA_MAXRECVCACHE; + if ( (coin->MAXPENDING= maxpending) <= 0 ) + coin->MAXPENDING = (strcmp(symbol,"BTC") == 0) ? _IGUANA_MAXPENDING : _IGUANA_MAXPENDING*13; + if ( (coin->MAXBUNDLES= maxbundles) <= 0 ) + coin->MAXBUNDLES = (strcmp(symbol,"BTC") == 0) ? _IGUANA_MAXBUNDLES : _IGUANA_MAXBUNDLES*64; + coin->myservices = services; + sprintf(dirname,"DB/%s",symbol); + OS_ensure_directory(dirname); + sprintf(dirname,"tmp/%s",symbol); + OS_ensure_directory(dirname); + coin->initialheight = initialheight; + coin->mapflags = mapflags; + coin->MAXMEM = juint(json,"RAM"); + if ( coin->MAXMEM == 0 ) + coin->MAXMEM = IGUANA_DEFAULTRAM; + coin->MAXMEM *= (1024 * 1024 * 1024); + if ( (coin->polltimeout= juint(json,"poll")) <= 0 ) + coin->polltimeout = 10; + char str[65]; printf("MAXMEM.%s\n",mbstr(str,coin->MAXMEM)); + coin->active = juint(json,"active"); + if ( (coin->minconfirms = minconfirms) == 0 ) + coin->minconfirms = (strcmp(symbol,"BTC") == 0) ? 3 : 10; + if ( coin->chain == 0 && (coin->chain= iguana_createchain(json)) == 0 ) + { + printf("cant initialize chain.(%s)\n",jstr(json,0)); + return(0); + } + if ( (peers= jarray(&m,json,"peers")) != 0 ) + { + for (j=0; jlaunched == 0 ) + { + if ( juint(json,"GBavail") < 8 ) + maphash = IGUANA_MAPHASHTABLES; + else maphash = 0; + iguana_coinargs(symbol,&maxrecvcache,&minconfirms,&maxpeers,&initialheight,&services,&maxpending,&maxbundles,json); + coins = mycalloc('A',1+1,sizeof(*coins)); + if ( (coin= iguana_setcoin(coin->symbol,coins,maxpeers,maxrecvcache,services,initialheight,maphash,minconfirms,maxpending,maxbundles,json)) != 0 ) + { + coins[0] = (void *)((long)1); + coins[1] = coin; + printf("launch coinloop for.%s services.%llx\n",coin->symbol,(long long)services); + iguana_launch(coin,"iguana_coinloop",iguana_coinloop,coins,IGUANA_PERMTHREAD); + return(1); + } + else + { + myfree(coins,sizeof(*coins) * 2); + return(-1); + } + } + return(0); +} + +void iguana_coins(void *arg) +{ + struct iguana_info **coins,*coin; char *jsonstr,*symbol; cJSON *array,*item,*json; + int32_t i,n,maxpeers,maphash,initialheight,minconfirms,maxpending,maxbundles; + int64_t maxrecvcache; uint64_t services; + if ( (jsonstr= arg) != 0 && (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (array= jarray(&n,json,"coins")) == 0 ) + { + if ( (symbol= jstr(json,"coin")) != 0 && strncmp(symbol,"BTC",3) == 0 ) + { + coins = mycalloc('A',1+1,sizeof(*coins)); + coins[1] = iguana_setcoin(symbol,coins,0,0,0,0,0,0,0,0,json); + coins[0] = (void *)((long)1); + iguana_coinloop(coins); + } else printf("no coins[] array in JSON.(%s) only BTCD and BTC can be quicklaunched\n",jsonstr); + free_json(json); + return; + } + coins = mycalloc('A',n+1,sizeof(*coins)); + if ( juint(json,"GBavail") < 8 ) + maphash = IGUANA_MAPHASHTABLES; + else maphash = 0; + for (i=0; i 8 ) + { + printf("skip strange coin.(%s)\n",symbol); + continue; + } + iguana_coinargs(symbol,&maxrecvcache,&minconfirms,&maxpeers,&initialheight,&services,&maxpending,&maxbundles,item); + coins[1 + i] = coin = iguana_setcoin(symbol,coins,maxpeers,maxrecvcache,services,initialheight,maphash,minconfirms,maxpending,maxbundles,item); + } + coins[0] = (void *)((long)n); + iguana_coinloop(coins); + } +} + +int32_t opreturns_init(uint32_t blocknum,uint32_t blocktimestamp,char *path) +{ + printf("opreturns_init not yet\n"); + return(-1); +} + +void peggy() +{ + printf("peggy not yet\n"); +} +char *busdata_sync(uint32_t *noncep,char *jsonstr,char *broadcastmode,char *destNXTaddr) +{ + printf("busdata_sync.(%s)\n",jsonstr); + return(0); +} + diff --git a/iguana/iguana777.h b/iguana/iguana777.h new file mode 100755 index 000000000..833ffc9b1 --- /dev/null +++ b/iguana/iguana777.h @@ -0,0 +1,698 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef iguana777_net_h +#define iguana777_net_h +#include "../crypto777/OS_portable.h" + +//#define IGUANA_DISABLEPEERS + +#define IGUANA_EXCHANGEIDLE 10 +#define IGUANS_JSMILLIS 100 + +#define IGUANA_WIDTH 1024 +#define IGUANA_HEIGHT 200 + +#define IGUANA_MAXPENDHDRS 1 +#define _IGUANA_MAXPENDING 3 //64 +#define _IGUANA_MAXBUNDLES 8 +#define IGUANA_MAXACTIVEBUNDLES 32 +#define IGUANA_MAXFILES 4096 +#define IGUANA_BUNDLELOOP 100 +#define IGUANA_RPCPORT 7778 +#define IGUANA_MAXRAMCHAINSIZE ((uint64_t)1024L * 1024L * 1024L * 16) + +#define IGUANA_MAPHASHTABLES 1 +#define IGUANA_DEFAULTRAM 4 +#define IGUANA_MAXRECVCACHE ((int64_t)1024L * 1024 * 1024L) +#define IGUANA_MAXBUNDLES (5000000 / 500) +#define IGUANA_LOG2MAXPEERS 9 +#define IGUANA_LOG2PACKETSIZE 21 +#define IGUANA_LOG2PEERFILESIZE 23 + +#define IGUANA_MAXPEERS (1 << IGUANA_LOG2MAXPEERS) +#define IGUANA_MAXPACKETSIZE (1 << IGUANA_LOG2PACKETSIZE) +#define IGUANA_PEERFILESIZE (1 << IGUANA_LOG2PEERFILESIZE) +struct iguana_txdatabits { uint64_t addrind:IGUANA_LOG2MAXPEERS,filecount:10,fpos:IGUANA_LOG2PEERFILESIZE,datalen:IGUANA_LOG2PACKETSIZE,isdir:1; }; + +#define IGUANA_MAXFILEITEMS 8192 + +#define IGUANA_RECENTPEER (3600 * 24 * 7) + +#define IGUANA_PERMTHREAD 0 +#define IGUANA_CONNTHREAD 1 +#define IGUANA_SENDTHREAD 2 +#define IGUANA_RECVTHREAD 3 +#define IGUANA_HELPERTHREAD 4 +#define IGUANA_EXCHANGETHREAD 5 + +#define IGUANA_DEDICATED_THREADS +#ifdef IGUANA_DEDICATED_THREADS +#define IGUANA_MAXCONNTHREADS 64 +#define IGUANA_MAXSENDTHREADS IGUANA_MAXPEERS +#define IGUANA_MAXRECVTHREADS IGUANA_MAXPEERS +#else +#define IGUANA_MAXCONNTHREADS 64 +#define IGUANA_MAXSENDTHREADS 64 +#define IGUANA_MAXRECVTHREADS 64 +#endif + + +#include "../includes/curve25519.h" +#include "../includes/cJSON.h" +#ifdef __PNACL +void PostMessage(const char* format, ...); +#endif + +extern int32_t IGUANA_NUMHELPERS; + +#ifdef __PNACL +#define printf PostMessage +#define MS_ASYNC 1 /* Sync memory asynchronously. */ +#define MS_SYNC 4 /* Synchronous memory sync. */ +#else +#define PostMessage printf +#endif + +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0x4000 // Do not generate SIGPIPE +#endif + +#define BIP0031_VERSION 60000 +#define CADDR_TIME_VERSION 31402 +#define MIN_PROTO_VERSION 209 +#define MAX_BLOCK_SIZE 1000000 +#define COINBASE_MATURITY 100 + +#define _IGUANA_HDRSCOUNT 2000 +#define _IGUANA_BLOCKHASHES 500 +#define IGUANA_MAXBUNDLESIZE _IGUANA_HDRSCOUNT + +#define NODE_NETWORK (1 << 0) +#define NODE_GETUTXO (1 << 1) +#define NODE_BLOOM (1 << 2) + +#define PROTOCOL_VERSION 70011 +#define INIT_PROTO_VERSION 209 // initial proto version, to be increased after version/verack negotiation +#define GETHEADERS_VERSION 31800 // In this version, 'getheaders' was introduced. +#define MIN_PEER_PROTO_VERSION GETHEADERS_VERSION // disconnect from peers older than this proto version +// nTime field added to CAddress, starting with this version, if possible, avoid requesting addresses nodes older than this +#define CADDR_TIME_VERSION 31402 +// only request blocks from nodes outside this range of versions +#define NOBLKS_VERSION_START 32000 +#define NOBLKS_VERSION_END 32400 + +#define BIP0031_VERSION 60000 // BIP 0031, pong message, is enabled for all versions AFTER this one +#define MEMPOOL_GD_VERSION 60002 // "mempool" command, enhanced "getdata" behavior starts with this version +#define NO_BLOOM_VERSION 70011 // "filter*" disabled without NODE_BLOOM after and including this version + +#define MSG_TX 1 +#define MSG_BLOCK 2 +#define MSG_FILTERED_BLOCK 3 + +#define IGUANA_MAXLOCATORS 64 +#define IGUANA_MAXINV 50000 + +#define IGUANA_VOLATILE 1 +#define IGUANA_ITEMIND_DATA 2 +#define IGUANA_MAPPED_ITEM 4 +#define IGUANA_SHA256 0x80 +#define IGUANA_ALLOC_MULT 1.1 +#define IGUANA_ALLOC_INCR 1000 + +#define IGUANA_JSONTIMEOUT 10000 + +#define IGUANA_MAPRECVDATA 1 +#define IGUANA_MAPTXIDITEMS 2 +#define IGUANA_MAPPKITEMS 4 +#define IGUANA_MAPBLOCKITEMS 8 +#define IGUANA_MAPPEERITEMS 16 + +#define IGUANA_PEER_ELIGIBLE 1 +#define IGUANA_PEER_CONNECTING 2 +#define IGUANA_PEER_READY 3 +#define IGUANA_PEER_KILLED 4 + +#define CHAIN_BTCD 0 +#define CHAIN_TESTNET3 1 +#define CHAIN_BITCOIN 2 + +#define IGUANA_SEARCHBUNDLE 1 +#define IGUANA_SEARCHNOLAST (IGUANA_SEARCHBUNDLE | 2) +#define IGUANA_SEARCHPREV 4 +#define IGUANA_SEARCHNEXT 8 +#define IGUANA_SEARCHALL (IGUANA_SEARCHBUNDLE | IGUANA_SEARCHPREV | IGUANA_SEARCHNEXT) + + +typedef void (*iguana_func)(void *); +struct iguana_thread +{ + struct queueitem DL; + pthread_t handle; + struct iguana_info *coin; + char name[16]; + uint8_t type; + iguana_func funcp; + void *arg; +}; + +struct iguana_chain +{ + //const int32_t chain_id; + char name[32],symbol[8]; + uint8_t pubval,p2shval,wipval,netmagic[4]; + char *genesis_hash,*genesis_hex; // hex string + uint16_t portp2p,portrpc,hastimestamp; + uint64_t rewards[512][2]; + uint8_t genesis_hashdata[32],unitval,minconfirms; + uint16_t ramchainport,bundlesize,hasheaders; + char gethdrsmsg[16]; +}; + +struct iguana_msghdr { uint8_t netmagic[4]; char command[12]; uint8_t serdatalen[4],hash[4]; } __attribute__((packed)); + +struct iguana_msgaddress { uint32_t nTime; uint64_t nServices; uint8_t ip[16]; uint16_t port; } __attribute__((packed)); + +struct iguana_msgversion +{ + uint32_t nVersion; + uint64_t nServices; + int64_t nTime; + struct iguana_msgaddress addrTo,addrFrom; + uint64_t nonce; + char strSubVer[80]; + uint32_t nStartingHeight; + uint8_t relayflag; +}; + +struct iguana_msgblockhdr +{ + uint32_t version; + bits256 prev_block,merkle_root; + uint32_t timestamp,bits,nonce; +} __attribute__((packed)); + +struct iguana_msgblock +{ + struct iguana_msgblockhdr H; // double hashed for blockhash + uint32_t txn_count; +} __attribute__((packed)); + +struct iguana_msgvin { bits256 prev_hash; uint8_t *script; uint32_t prev_vout,scriptlen,sequence; } __attribute__((packed)); + +struct iguana_msgvout { uint64_t value; uint32_t pk_scriptlen; uint8_t *pk_script; } __attribute__((packed)); + +struct iguana_msgtx +{ + uint32_t version,tx_in,tx_out,lock_time; + struct iguana_msgvin *vins; + struct iguana_msgvout *vouts; + bits256 txid; + int32_t allocsize,timestamp; +} __attribute__((packed)); + +struct iguana_packet { struct queueitem DL; struct iguana_peer *addr; int32_t datalen,getdatablock; uint8_t serialized[]; }; + +struct msgcounts { uint32_t version,verack,getaddr,addr,inv,getdata,notfound,getblocks,getheaders,headers,tx,block,mempool,ping,pong,reject,filterload,filteradd,filterclear,merkleblock,alert; }; + +struct iguana_fileitem { bits256 hash2; struct iguana_txdatabits txdatabits; }; + +struct iguana_kvitem { UT_hash_handle hh; uint8_t keyvalue[]; } __attribute__((packed)); + +struct iguana_iAddr +{ + UT_hash_handle hh; uint32_t ipbits; + uint32_t lastkilled,lastconnect; + int32_t status,height,numkilled,numconnects; +}; + +struct iguana_cacheptr { struct queueitem DL; int32_t allocsize,recvlen; uint8_t *data; }; + +// iguana blocks +struct iguana_blockRO +{ + bits256 hash2,prev_block,merkle_root; + uint32_t timestamp,nonce,bits,version; + uint32_t firsttxidind,firstvin,firstvout,firstpkind,firstexternalind,recvlen:24,tbd:8; + uint16_t txn_count,numvouts,numvins,extra; +}; + +struct iguana_block +{ + struct iguana_blockRO RO; + double PoW; // NOT consensus safe, for estimation purposes only + int32_t height,fpos; uint32_t fpipbits; + uint16_t hdrsi,bundlei:12,mainchain:1,valid:1,queued:1,tbd:1,numrequests:8,extra:8; + UT_hash_handle hh; +} __attribute__((packed)); + + +#define IGUANA_LHASH_BLOCKS 0 +#define IGUANA_LHASH_TXIDS 1 // +#define IGUANA_LHASH_UNSPENTS 2 // +#define IGUANA_LHASH_SPENDS 3 // +#define IGUANA_LHASH_PKHASHES 4 // +#define IGUANA_LHASH_SPENTINDS 5 +//#define IGUANA_LHASH_FIRSTSPENDS 5 // +#define IGUANA_LHASH_ACCOUNTS 6 // +#define IGUANA_LHASH_EXTERNALS 7 // +#define IGUANA_LHASH_TXBITS 8 // +#define IGUANA_LHASH_PKBITS 9 // +#define IGUANA_NUMLHASHES (IGUANA_LHASH_PKBITS + 1) + +struct iguana_counts +{ + uint32_t firsttxidind,firstunspentind,firstspendind,firstpkind; + //bits256 lhashes[IGUANA_NUMAPPENDS],ledgerhash; struct sha256_vstate states[IGUANA_NUMAPPENDS]; + //bits256 blockhash,merkle_root; + uint64_t credits,debits; + //uint32_t timestamp,height; + //struct iguana_prevdep dep; + struct iguana_block block; +} __attribute__((packed)); + +struct iguana_blocks +{ + char coin[8]; + struct iguanakv *db; + struct iguana_block *hash; struct iguana_blockRO *RO; int32_t maxbits; + int32_t maxblocks,initblocks,hashblocks,issuedblocks,recvblocks,emitblocks,parsedblocks,dirty; + struct iguana_block hwmchain; +}; + +struct iguana_ledger +{ + struct iguana_counts snapshot; + //struct iguana_account accounts[]; +} __attribute__((packed)); + +// ramchain append only structs -> canonical 32bit inds and ledgerhashes +struct iguana_txid { bits256 txid; uint32_t txidind,firstvout,firstvin,locktime,version,timestamp; uint16_t numvouts,numvins; } __attribute__((packed)); + +struct iguana_unspent { uint64_t value; uint32_t txidind,pkind,prevunspentind; uint16_t hdrsi:12,type:4,vout; } __attribute__((packed)); +struct iguana_unspent20 { uint64_t value; uint32_t txidind:28,type:4; uint8_t rmd160[20]; } __attribute__((packed)); + +struct iguana_spend256 { bits256 prevhash2; int16_t prevout; uint16_t spendind:15,diffsequence:1; } __attribute__((packed)); +struct iguana_spend { uint32_t spendtxidind; int16_t prevout; uint16_t tbd:14,external:1,diffsequence:1; } __attribute__((packed)); + +struct iguana_pkhash { uint8_t rmd160[20]; uint32_t pkind,firstunspentind,flags:23,type:8,ps2h:1; } __attribute__((packed)); + +// dynamic +struct iguana_account { uint64_t balance; uint32_t lastunspentind; } __attribute__((packed)); // pkind + +// GLOBAL one zero to non-zero write (unless reorg) +struct iguana_Uextra { uint32_t spendind; uint16_t hdrsi; } __attribute__((packed)); // unspentind +//struct iguana_pkextra { uint32_t firstspendind; } __attribute__((packed)); // pkind + +struct iguana_txblock +{ + uint32_t numtxids,numunspents,numspends,extralen,recvlen; + // following set during second pass (still in peer context) + uint32_t numpkinds,numexternaltxids,datalen,pkoffset; + uint8_t space[256]; // order: extra[], T, U, S, P, external txids + struct iguana_block block; +}; + +struct iguana_ramchaindata +{ + bits256 sha256; + bits256 lhashes[IGUANA_NUMLHASHES],firsthash2,lasthash2; + int64_t allocsize,Boffset,Toffset,Uoffset,Soffset,Poffset,Aoffset,Xoffset,TXoffset,PKoffset; + int32_t numblocks,height,firsti,hdrsi,txsparsebits,pksparsebits; + uint32_t numtxids,numunspents,numspends,numpkinds,numexternaltxids,numtxsparse,numpksparse; + uint8_t rdata[]; +}; + +struct iguana_ramchain_hdr +{ + uint32_t txidind,unspentind,spendind; uint16_t hdrsi,bundlei:15,ROflag:1; + struct iguana_ramchaindata *data; +}; + +struct iguana_ramchain +{ + struct iguana_ramchain_hdr H; bits256 lasthash2; uint64_t datasize; + uint32_t numblocks:31,expanded:1,pkind,externalind,height; + struct iguana_kvitem *txids,*pkhashes; + struct OS_memspace *hashmem; long filesize; void *fileptr; + struct iguana_account *A,*roA; //struct iguana_Uextra *U2,*roU2; struct iguana_pkextra *P2,*roP2; +}; + +struct iguana_peer +{ + struct queueitem DL; + queue_t sendQ; + struct iguana_msgaddress A; + char ipaddr[64],lastcommand[16],coinstr[16],symbol[16]; + uint64_t pingnonce,totalsent,totalrecv; double pingtime,sendmillis,pingsum,getdatamillis; + uint32_t lastcontact,sendtime,ready,startsend,startrecv,pending,ipbits,lastgotaddr,lastblockrecv,pendtime,lastflush,lastpoll; + int32_t dead,addrind,usock,lastheight,protover,relayflag,numpackets,numpings,ipv6,height,rank,pendhdrs,pendblocks,recvhdrs,lastlefti; + double recvblocks,recvtotal; + int64_t allocated,freed; + struct msgcounts msgcounts; + //FILE *fp; int32_t filecount,addrind; + struct OS_memspace RAWMEM,TXDATA,HASHMEM; + struct iguana_ramchain ramchain; + //struct iguana_kvitem *txids,*pkhashes; + struct iguana_fileitem *filehash2; int32_t numfilehash2,maxfilehash2; +#ifdef IGUANA_PEERALLOC + struct OS_memspace *SEROUT[128]; +#endif +}; + +struct iguana_peers +{ + bits256 lastrequest; + struct iguana_peer active[IGUANA_MAXPEERS],*ranked[IGUANA_MAXPEERS],*localaddr; + struct iguana_thread *peersloop,*recvloop,*acceptloop; + double topmetrics[IGUANA_MAXPEERS],avemetric; + uint32_t numranked,mostreceived,shuttingdown,lastpeer,lastmetrics,numconnected; + int32_t numfiles; +}; + +struct iguana_bloom16 { uint8_t hash2bits[65536 / 8]; }; + +struct iguana_bundle +{ + struct queueitem DL; struct iguana_info *coin; struct iguana_bundle *nextbp; + struct iguana_bloom16 bloom; + uint32_t issuetime,hdrtime,emitfinish,mergefinish,purgetime; + int32_t minrequests,numhashes,numissued,numrecv,n,hdrsi,bundleheight,numtxids,numspends,numunspents; + double avetime,threshold,metric; uint64_t datasize,estsize; + struct iguana_block *blocks[IGUANA_MAXBUNDLESIZE]; + bits256 prevbundlehash2,hashes[IGUANA_MAXBUNDLESIZE+1],nextbundlehash2,allhash; + struct iguana_ramchain ramchain; uint8_t red,green,blue; +}; + +struct iguana_bundlereq +{ + struct queueitem DL; struct iguana_info *coin; int32_t type; + struct iguana_peer *addr; struct iguana_block *blocks,block; bits256 *hashes,txid; + struct iguana_txdatabits txdatabits; + struct iguana_msghdr H; + int32_t allocsize,datalen,n,recvlen,numtx; uint32_t ipbits; + uint8_t copyflag,serialized[]; +}; + +struct iguana_bitmap { int32_t width,height,amplitude; char name[52]; uint8_t data[IGUANA_WIDTH*IGUANA_HEIGHT*3]; }; + +struct iguana_info +{ + char name[64],symbol[8],statusstr[512]; + struct iguana_peers peers; + uint64_t instance_nonce,myservices,totalsize,totalrecv,totalpackets,sleeptime; + int64_t mining,totalfees,TMPallocated,MAXRECVCACHE,MAXMEM,estsize,activebundles; + int32_t MAXPEERS,MAXPENDING,MAXBUNDLES,active,closestbundle,numemitted,lastsweep,startutc,newramchain,numcached,cachefreed; + uint32_t longestchain,lastsync,parsetime,numiAddrs,firstblock,lastpossible,bundlescount,savedblocks; + struct tai starttime; double startmillis; + struct iguana_chain *chain; + struct iguana_iAddr *iAddrs; + struct iguanakv *txids,*spends,*unspents,*pkhashes; + struct iguana_txid *T; + struct iguana_unspent *U; struct iguana_Uextra *Uextras; + struct iguana_spend *S; struct iguana_Sextra *Sextras; + struct iguana_pkhash *P; struct iguana_account *accounts; struct iguana_pkextra *pkextras; + //struct iguana_counts latest; + //struct iguana_ledger LEDGER,loadedLEDGER; + + struct iguana_bitmap screen; + //struct pollfd fds[IGUANA_MAXPEERS]; struct iguana_peer bindaddr; int32_t numsocks; + struct OS_memspace TXMEM; + queue_t bundlesQ,hdrsQ,blocksQ,priorityQ,possibleQ,jsonQ,finishedQ,TerminateQ,cacheQ; + double parsemillis,avetime; uint32_t Launched[8],Terminated[8]; + portable_mutex_t peers_mutex,blocks_mutex; + struct iguana_bundle *bundles[IGUANA_MAXBUNDLES]; + int32_t numpendings,zcount,recvcount,bcount,pcount,lastbundle; uint32_t recvtime,hdrstime,backstoptime,lastbundletime,numreqsent; + double backstopmillis; bits256 backstophash2; + int32_t initialheight,mapflags,minconfirms,numrecv,isRT,backstop,blocksrecv,merging,polltimeout,numreqtxids; bits256 reqtxids[64]; + void *launched,*started; + uint64_t bloomsearches,bloomhits,bloomfalse,collisions; uint8_t blockspace[IGUANA_MAXPACKETSIZE + 8192]; struct OS_memspace blockMEM; + struct iguana_blocks blocks; +}; + +// peers +int32_t iguana_verifypeer(struct iguana_info *coin,void *key,void *value,int32_t itemind,int32_t itemsize); +int32_t iguana_peermetrics(struct iguana_info *coin); +void iguana_peersloop(void *arg); +int32_t iguana_queue_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *serialized,char *cmd,int32_t len,int32_t getdatablock,int32_t forceflag); +uint32_t iguana_ipbits2ind(struct iguana_info *coin,struct iguana_iAddr *iA,uint32_t ipbits,int32_t createflag); +uint32_t iguana_rwiAddrind(struct iguana_info *coin,int32_t rwflag,struct iguana_iAddr *iA,uint32_t ind); +//uint32_t iguana_rwipbits_status(struct iguana_info *coin,int32_t rwflag,uint32_t ipbits,int32_t *statusp); +void iguana_connections(void *arg); +uint32_t iguana_possible_peer(struct iguana_info *coin,char *ipaddr); +//int32_t iguana_set_iAddrheight(struct iguana_info *coin,uint32_t ipbits,int32_t height); +//struct iguana_peer *iguana_choosepeer(struct iguana_info *coin); +void iguana_initpeer(struct iguana_info *coin,struct iguana_peer *addr,uint32_t ipbits); +void iguana_startconnection(void *arg); +void iguana_shutdownpeers(struct iguana_info *coin,int32_t forceflag); +void iguana_acceptloop(void *args); +void iguana_recvloop(void *args); +int32_t iguana_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *serialized,int32_t len); +uint32_t iguana_updatemetrics(struct iguana_info *coin); +void *iguana_peeralloc(struct iguana_info *coin,struct iguana_peer *addr,int32_t datalen); +int64_t iguana_peerfree(struct iguana_info *coin,struct iguana_peer *addr,void *ptr,int32_t datalen); +int64_t iguana_peerallocated(struct iguana_info *coin,struct iguana_peer *addr); + +// serdes +int32_t iguana_rwmem(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp); +int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp); +int32_t iguana_rwvarint32(int32_t rwflag,uint8_t *serialized,uint32_t *int32p); +int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp); +int32_t iguana_rwblock(int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct iguana_msgblock *msg); +int32_t iguana_serialize_block(bits256 *hash2p,uint8_t serialized[sizeof(struct iguana_msgblock)],struct iguana_block *block); +void iguana_blockconv(struct iguana_block *dest,struct iguana_msgblock *msg,bits256 hash2,int32_t height); +//void iguana_freetx(struct iguana_msgtx *tx,int32_t n); +int32_t iguana_parser(struct iguana_info *coin,struct iguana_peer *addr,struct OS_memspace *rawmem,struct OS_memspace *txmem,struct OS_memspace *hashmem,struct iguana_msghdr *H,uint8_t *data,int32_t datalen); + +// send message +int32_t iguana_validatehdr(struct iguana_info *coin,struct iguana_msghdr *H); +int32_t iguana_sethdr(struct iguana_msghdr *H,const uint8_t netmagic[4],char *command,uint8_t *data,int32_t datalen); +//int32_t iguana_request_data(struct iguana_info *coin,struct iguana_peer *addr,bits256 *hashes,int32_t n,uint32_t type,int32_t forceflag); +int32_t iguana_send_version(struct iguana_info *coin,struct iguana_peer *addr,uint64_t myservices); +//int32_t iguana_send_hashes(struct iguana_info *coin,char *command,struct iguana_peer *addr,bits256 stophash,bits256 *hashes,int32_t n); +int32_t iguana_gentxarray(struct iguana_info *coin,struct OS_memspace *mem,struct iguana_txblock *txblock,int32_t *lenp,uint8_t *data,int32_t datalen); +int32_t iguana_gethdrs(struct iguana_info *coin,uint8_t *serialized,char *cmd,char *hashstr); +int32_t iguana_getdata(struct iguana_info *coin,uint8_t *serialized,int32_t type,char *hashstr); + +// ramchain +int64_t iguana_verifyaccount(struct iguana_info *coin,struct iguana_account *acct,uint32_t pkind); +int32_t iguana_initramchain(struct iguana_info *coin,int32_t initialheight,int32_t mapflags,int32_t fullverify); +void iguana_syncramchain(struct iguana_info *coin); +//int32_t iguana_validateramchain(struct iguana_info *coin,int64_t *netp,uint64_t *creditsp,uint64_t *debitsp,int32_t height,struct iguana_block *block,int32_t hwmheight,struct iguana_prevdep *lp); +int32_t iguana_calcrmd160(struct iguana_info *coin,uint8_t rmd160[20],uint8_t msigs160[16][20],int32_t *Mp,int32_t *nump,uint8_t *pk_script,int32_t pk_scriptlen,bits256 debugtxid); +uint32_t iguana_updatescript(struct iguana_info *coin,uint32_t blocknum,uint32_t txidind,uint32_t spendind,uint32_t unspentind,uint64_t value,uint8_t *script,int32_t scriptlen,uint32_t sequence); +void iguana_gotblockM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *txdata,struct iguana_msgtx *txarray,struct iguana_msghdr *H,uint8_t *data,int32_t datalen); +int32_t iguana_parseblock(struct iguana_info *coin,struct iguana_block *block,struct iguana_msgtx *tx,int32_t numtx); +uint32_t iguana_txidind(struct iguana_info *coin,uint32_t *firstvoutp,uint32_t *firstvinp,bits256 txid); +bits256 iguana_txidstr(struct iguana_info *coin,uint32_t *firstvoutp,uint32_t *firstvinp,char *txidstr,uint32_t txidind); +int32_t iguana_updateramchain(struct iguana_info *coin); +//void iguana_emittxarray(struct iguana_info *coin,FILE *fp,struct iguana_bundle *bundle,struct iguana_block *block,struct iguana_msgtx *txarray,int32_t numtx); + +// blockchain +int32_t iguana_needhdrs(struct iguana_info *coin); +//int32_t iguana_setchainvars(struct iguana_info *coin,struct iguana_prevdep *lp,bits256 hash2,uint32_t nBits,bits256 prevhash,int32_t txn_count); // uint32_t *firsttxidindp,uint32_t *firstvoutp,uint32_t *firstvinp,double *PoWp +//int32_t iguana_blockcmp(struct iguana_info *coin,struct iguana_block *A,struct iguana_block *B,int32_t fastflag); +//int32_t iguana_setdependencies(struct iguana_info *coin,struct iguana_block *block,struct iguana_prevdep *lp); +//int32_t iguana_fixblocks(struct iguana_info *coin,int32_t startheight,int32_t endheight); +struct iguana_chain *iguana_chainfind(char *name); +//int32_t iguana_numblocks(struct iguana_info *coin); +int32_t iguana_chainextend(struct iguana_info *coin,struct iguana_block *newblock); +//int32_t iguana_lookahead(struct iguana_info *coin,bits256 *hash2p,int32_t height); +uint64_t iguana_miningreward(struct iguana_info *coin,uint32_t blocknum); + +// tx +int32_t iguana_rwtx(int32_t rwflag,struct OS_memspace *mem,uint8_t *serialized,struct iguana_msgtx *msg,int32_t maxsize,bits256 *txidp,int32_t height,int32_t hastimestamp); +void iguana_gottxidsM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *txids,int32_t n); +void iguana_gotunconfirmedM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgtx *tx,uint8_t *data,int32_t datalen); +void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashes,int32_t n); + +// blocks +bits256 iguana_blockhash(struct iguana_info *coin,int32_t height); +//int32_t iguana_havehash(struct iguana_info *coin,int32_t height); +//int32_t iguana_bundleready(struct iguana_info *coin,int32_t height); +//void *iguana_bundletxdata(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei); + +//#define iguana_block(coin,height) (height >= 0 ? coin->blocks.ptrs[height] : 0) // invariant ptr +#define iguana_blockfind(coin,hash2) iguana_blockhashset(coin,-1,hash2,0) +struct iguana_block *iguana_blockhashset(struct iguana_info *coin,int32_t height,bits256 hash2,int32_t createflag); +//int32_t iguana_hash2height(struct iguana_info *coin,bits256 hash2); +//int32_t iguana_blockheight(struct iguana_info *coin,struct iguana_block *block); // partially confirmed +//int32_t iguana_chainheight(struct iguana_info *coin,struct iguana_block *block); // in the blockchain + +uint32_t iguana_syncs(struct iguana_info *coin); +//void iguana_audit(struct iguana_info *coin); +void iguana_gotdata(struct iguana_info *coin,struct iguana_peer *addr,int32_t height); +//void iguana_mergeblock(struct iguana_block *dest,struct iguana_prevdep *destlp,struct iguana_block *block,struct iguana_prevdep *srclp); +//int32_t iguana_avail(struct iguana_info *coin,int32_t height,int32_t n); +int64_t iguana_balance(struct iguana_info *coin,uint64_t *creditsp,uint64_t *debitsp,int32_t *nump,uint32_t *unspents,long max,struct iguana_pkhash *P,uint32_t pkind); +int32_t iguana_queueblock(struct iguana_info *coin,int32_t height,bits256 hash2,int32_t priority); +int32_t iguana_updatewaiting(struct iguana_info *coin,int32_t starti,int32_t max); + +// recvbits +int32_t iguana_recvinit(struct iguana_info *coin,int32_t initialheight); +int32_t ramcoder_decompress(uint8_t *data,int32_t maxlen,uint8_t *bits,uint32_t numbits,bits256 seed); +int32_t ramcoder_compress(uint8_t *bits,int32_t maxlen,uint8_t *data,int32_t datalen,uint64_t *histo,bits256 seed); +uint64_t hconv_bitlen(uint64_t bitlen); +struct iguana_block *iguana_blockptr(struct iguana_info *coin,int32_t height); +int32_t iguana_processrecv(struct iguana_info *coin); // single threaded +void iguana_recvalloc(struct iguana_info *coin,int32_t numitems); +void iguana_coins(void *arg); +int32_t iguana_savehdrs(struct iguana_info *coin); + +// hdrs +struct iguana_bundle *iguana_bundlecreate(struct iguana_info *coin,int32_t *bundleip,int32_t bundleheight,bits256 bundlehash2,bits256 allhash,int32_t issueflag); +struct iguana_block *iguana_updatehdrs(struct iguana_info *coin,int32_t *newhwmp,struct iguana_block *block,bits256 prevhash2,bits256 hash2); +void iguana_parseline(struct iguana_info *coin,int32_t iter,FILE *fp); +void iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n); +//struct iguana_bundle *iguana_bundleinit(struct iguana_info *coin,int32_t height,bits256 hash2); +void iguana_emittxdata(struct iguana_info *coin,struct iguana_bundle *bp); +int32_t iguana_pollQsPT(struct iguana_info *coin,struct iguana_peer *addr); +int32_t iguana_avail(struct iguana_info *coin,int32_t height,int32_t n); +int32_t iguana_updatebundles(struct iguana_info *coin); +//void **iguana_recvblockptrp(struct iguana_info *coin,int32_t *blockip,int32_t height); +void iguana_bundlestats(struct iguana_info *coin,char *str); + +// init +struct iguana_info *iguana_startcoin(struct iguana_info *coin,int32_t initialheight,int32_t mapflags); +void iguana_initcoin(struct iguana_info *coin); +void iguana_coinloop(void *arg); + +// utils +double PoW_from_compact(uint32_t nBits,uint8_t unitval); +void calc_rmd160(char *hexstr,uint8_t buf[20],uint8_t *msg,int32_t len); +void calc_OP_HASH160(char *hexstr,uint8_t hash160[20],char *msg); +double dxblend(double *destp,double val,double decay); + +// json +int32_t iguana_processjsonQ(struct iguana_info *coin); // reentrant, can be called during any idletime +char *iguana_JSON(char *jsonstr); + +char *mbstr(char *str,double); +int init_hexbytes_noT(char *hexbytes,unsigned char *message,long len); +int32_t decode_hex(unsigned char *bytes,int32_t n,char *hex); +char hexbyte(int32_t c); +char *clonestr(char *str); +long _stripwhite(char *buf,int accept); +int32_t myatoi(char *str,int32_t range); +int32_t safecopy(char *dest,char *src,long len); +void escape_code(char *escaped,char *str); +int32_t is_zeroes(char *str); +int64_t conv_floatstr(char *numstr); +int32_t has_backslash(char *str); + +uint64_t calc_ipbits(char *ip_port); +void expand_ipbits(char *ipaddr,uint64_t ipbits); + +bits256 bits256_doublesha256(char *hashstr,uint8_t *data,int32_t datalen); +char *bits256_str(char hexstr[65],bits256 x); +char *bits256_str2(char hexstr[65],bits256 x); +char *bits256_lstr(char hexstr[65],bits256 x); +bits256 bits256_add(bits256 a,bits256 b); +int32_t bits256_cmp(bits256 a,bits256 b); +bits256 bits256_from_compact(uint32_t c); + + +struct iguana_thread *iguana_launch(struct iguana_info *coin,char *name,iguana_func funcp,void *arg,uint8_t type); +int32_t iguana_numthreads(struct iguana_info *coin,int32_t mask); +void iguana_terminator(void *arg); + +int32_t is_hexstr(char *str,int32_t n); +void iguana_initQ(queue_t *Q,char *name); +void iguana_emitQ(struct iguana_info *coin,struct iguana_bundle *bp); +void iguana_txdataQ(struct iguana_info *coin,struct iguana_peer *addr,FILE *fp,long fpos,int32_t datalen); +void iguana_helper(void *arg); + +struct iguana_helper { struct queueitem DL; void *coin,*addr,*bp,*nextbp,*fp; long fpos; int32_t allocsize,type,hdrsi,bundlei,datalen; uint32_t starttime; }; +int32_t iguana_helpertask(FILE *fp,struct OS_memspace *mem,struct OS_memspace *memB,struct iguana_helper *ptr); +void iguana_flushQ(struct iguana_info *coin,struct iguana_peer *addr); +//struct iguana_txdatabits iguana_peerfilePT(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2,struct iguana_txdatabits txdatabits,int32_t recvlen); +struct iguana_txdatabits iguana_calctxidbits(uint32_t addrind,uint32_t filecount,uint32_t fpos,uint32_t datalen); +int32_t iguana_bundlesaveHT(struct iguana_info *coin,struct OS_memspace *mem,struct OS_memspace *memB,struct iguana_bundle *bp,uint32_t starttime); // helper thread +int32_t iguana_bundlemergeHT(struct iguana_info *coin,struct OS_memspace *mem,struct OS_memspace *memB,struct iguana_bundle *bp,struct iguana_bundle *nextbp,uint32_t starttime); // helper thread + +void iguana_peerfilename(struct iguana_info *coin,char *fname,uint32_t addrind,uint32_t filecount); + +struct iguana_txblock *iguana_ramchainptrs(struct iguana_txid **Tptrp,struct iguana_unspent20 **Uptrp,struct iguana_spend256 **Sptrp,struct iguana_pkhash **Pptrp,bits256 **externalTptrp,struct OS_memspace *mem,struct iguana_txblock *origtxdata); + +int32_t iguana_ramchainsave(struct iguana_info *coin,struct iguana_ramchain *ramchain); +int32_t iguana_ramchainfree(struct iguana_info *coin,struct OS_memspace *mem,struct iguana_ramchain *ramchain); +struct iguana_ramchain *iguana_ramchainmergeHT(struct iguana_info *coin,struct OS_memspace *mem,struct iguana_ramchain *ramchains[],int32_t n,struct iguana_bundle *bp); +void iguana_ramchainmerge(struct iguana_info *coin); + +int32_t iguana_blockQ(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t priority); +void iguana_blockcopy(struct iguana_info *coin,struct iguana_block *block,struct iguana_block *origblock); +int32_t iguana_rpctest(struct iguana_info *coin); +extern queue_t helperQ; +extern const char *Hardcoded_coins[][3]; +void iguana_main(void *arg); +extern struct iguana_info *Coins[64]; +int32_t iguana_peerfname(struct iguana_info *coin,int32_t *hdrsip,char *dirname,char *fname,uint32_t ipbits,bits256 hash2,bits256 prevhash2,int32_t numblocks); +struct iguana_txblock *iguana_peertxdata(struct iguana_info *coin,int32_t *bundleip,char *fname,struct OS_memspace *mem,uint32_t ipbits,bits256 hash2); +int32_t iguana_peerfile_exists(struct iguana_info *coin,struct iguana_peer *addr,char *dirname,char *fname,bits256 hash2,bits256 prevhash2,int32_t numblocks); +struct iguana_ramchain *iguana_ramchainset(struct iguana_info *coin,struct iguana_ramchain *ramchain,struct iguana_txblock *txdata); +void *iguana_iAddriterator(struct iguana_info *coin,struct iguana_iAddr *iA); +long iguana_ramchain_data(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,int32_t txn_count,uint8_t *data,int32_t recvlen); +int32_t iguana_bundlehash2add(struct iguana_info *coin,struct iguana_block **blockp,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2); +struct iguana_block *iguana_bundleblockadd(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,struct iguana_block *origblock); +int32_t iguana_chainextend(struct iguana_info *coin,struct iguana_block *newblock); +int32_t iguana_blockvalidate(struct iguana_info *coin,int32_t *validp,struct iguana_block *block); +char *iguana_bundledisp(struct iguana_info *coin,struct iguana_bundle *prevbp,struct iguana_bundle *bp,struct iguana_bundle *nextbp,int32_t m); +struct iguana_bundle *iguana_bundlefind(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,bits256 hash2); +//int32_t iguana_chainheight(struct iguana_info *coin,struct iguana_block *origblock); +bits256 *iguana_blockhashptr(struct iguana_info *coin,int32_t height); +int32_t iguana_hash2set(struct iguana_info *coin,char *debugstr,struct iguana_bundle *bp,int32_t bundlei,bits256 newhash2); +struct iguana_block *_iguana_chainlink(struct iguana_info *coin,struct iguana_block *newblock); +int32_t iguana_hashfree(struct iguana_kvitem *hashtable,int32_t freeitem); +int32_t iguana_processbundlesQ(struct iguana_info *coin,int32_t *newhwmp); // single threaded +int32_t iguana_ramchainverifyPT(struct iguana_info *coin,struct iguana_ramchain *ramchain); +void *map_file(char *fname,long *filesizep,int32_t enablewrite); +void iguana_rpcloop(void *args); +int32_t iguana_socket(int32_t bindflag,char *hostname,uint16_t port); +void iguana_mergeQ(struct iguana_info *coin,struct iguana_bundle *bp,struct iguana_bundle *nextbp); + +#define bits256_nonz(a) (((a).ulongs[0] | (a).ulongs[1] | (a).ulongs[2] | (a).ulongs[3]) != 0) +int32_t btc_addr2univ(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr); + +struct iguana_agent +{ + char name[32],hostname[64]; void *methods; uint16_t port; int32_t sock,nummethods; + bits256 pubkey,privkey; + char *(*parsefunc)(struct iguana_agent *agent,struct iguana_info *coin,char *method,void *json); +}; +char *iguana_txbytes(struct iguana_info *coin,bits256 *txidp,struct iguana_txid *tx,int32_t height); +void iguana_vinset(struct iguana_info *coin,int32_t height,struct iguana_msgvin *vin,struct iguana_txid *tx,int32_t i); +int32_t iguana_voutset(struct iguana_info *coin,uint8_t *scriptspace,char *asmstr,int32_t height,struct iguana_msgvout *vout,struct iguana_txid *tx,int32_t i); +int32_t btc_convrmd160(char *coinaddr,uint8_t addrtype,uint8_t rmd160[20]); +struct iguana_txid *iguana_bundletx(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,struct iguana_txid *tx,int32_t txidind); +int32_t iguana_txidreq(struct iguana_info *coin,char **retstrp,bits256 txid); +void iguana_bundleiclear(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei); +int32_t hcalc_bitsize(uint64_t x); +struct iguana_pkhash *iguana_pkhashfind(struct iguana_info *coin,struct iguana_pkhash *p,uint8_t rmd160[20]); +struct iguana_txid *iguana_txidfind(struct iguana_info *coin,int32_t *heightp,struct iguana_txid *tx,bits256 txid); +int32_t iguana_scriptgen(struct iguana_info *coin,uint8_t *script,char *asmstr,struct iguana_bundle *bp,struct iguana_pkhash *p,uint8_t type); +int32_t iguana_ramchain_spendtxid(struct iguana_info *coin,bits256 *txidp,struct iguana_txid *T,int32_t numtxids,bits256 *X,int32_t numexternaltxids,struct iguana_spend *s); +struct iguana_info *iguana_coinselect(); +//void init_InstantDEX(); +//char *InstantDEX(char *jsonstr,char *remoteaddr,int32_t localaccess); + +char *busdata_sync(uint32_t *noncep,char *jsonstr,char *broadcastmode,char *destNXTaddr); +void peggy(); +int32_t opreturns_init(uint32_t blocknum,uint32_t blocktimestamp,char *path); +struct iguana_info *iguana_coinfind(const char *symbol); +struct iguana_info *iguana_coinadd(const char *symbol); +struct iguana_ramchain *iguana_bundleload(struct iguana_info *coin,struct iguana_bundle *bp); +int32_t iguana_sendblockreq(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t iamthreadsafe); + +#endif diff --git a/iguana/iguana_bitmap.c b/iguana/iguana_bitmap.c new file mode 100755 index 000000000..1a8d57880 --- /dev/null +++ b/iguana/iguana_bitmap.c @@ -0,0 +1,1160 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" +#include "../crypto777/jpeg/jpeglib.h" + +#define NUM_COMBINED 100 + +#define LEFTMARGIN 0 +#define TIMEIND_PIXELS 256 +#define NUM_ACTIVE_PIXELS (1024 - TIMEIND_PIXELS) +#define MAX_ACTIVE_WIDTH (NUM_ACTIVE_PIXELS + TIMEIND_PIXELS) +double Display_scale = 1.; +#define TITLEBAR_HEIGHT 24 +#define MAX_USER_INTERFACE_BITMAPS 1 // not completely implemented yet +#define DP_FULLSCREEN 0 +#define DP_HALFSCREENV 1 +#define DP_HALFSCREENH 2 +#define DP_QUARTERSCREEN 3 +#define DP_SCREEN8 4 +#define DP_TOPSLICE 5 +#define DP_TOPSLICE2 6 +#define DP_64SCREEN 7 +#define LEFTMARGIN 0 +#define SCREENCLUMPSIZE 8 +#define MAX_WEEKIND NUM_WEEKINDS + + +struct bq_info +{ + float tmpbuf[IGUANA_WIDTH]; + float linebuf[IGUANA_WIDTH]; + uint32_t *bitmap; + float lastval,scale; + + int32_t jdatetime,timewindow,timescale,firstjdatetime; + int32_t rowwidth,color; + uint16_t height,width,blendflag,pad; +}; + +struct display_template +{ + char java_bitmap_fname[512]; + char title[128],lasttitle[128]; // only 128 bytes gets displayed + uint32_t *java_bitmap; + void *vp; + struct bq_info fullscreen; + struct bq_info sixtyfourscreen[64]; + struct bq_info halfscreenV[2],halfscreenH[2]; + struct bq_info quarterscreen[4]; + struct bq_info screenx8[8]; + struct bq_info topslice; + struct bq_info topslice2; + int32_t chartheight,windheight,rowwidth,updated; + int32_t microsleep,firstjdatetime,type,margin; + int32_t clumpsize,pad1,pad2,pad3; +}; +struct display_template *DISPLAY; +double Display_shift=1.; +int32_t Display_pause,Display_j,Display_k,Display_contractnum; +int32_t JDATA_end_jdatetime,JDATA_first_jdatetime,Server2_weekind,Parsed_jdatetime,Parsed_jdatetimes[NUM_COMBINED+1]; +int32_t Parsed_weekinds[NUM_COMBINED+1],paused_Parsed_jdatetimes[NUM_COMBINED+1],SharedBitmapDisplay_process; +int32_t Display_mousex,Display_mousey,Display_mouse_button,Nodisplay; +uint32_t *Display_bitmap,*Display_bitmaps[NUM_COMBINED+1]; +double Latestbids[NUM_COMBINED+1],Latestasks[NUM_COMBINED+1]; +int32_t Last_keypress_time,Disable_currency_display,Invert_currency_display,Display_mode=1; +uint64_t Reloadflag; +//struct wbits State_wbits,Parsed_wbits,Parsedbits[NUM_COMBINED+1],Processedbits[NUM_COMBINED+1],Processed_wbits,Trade_wbits,Feedback_wbits; + +void gen_ppmfile(char *fname,int32_t binaryflag,uint8_t *bitmap,int32_t width,int32_t height) +{ + int32_t x,j,red,green,blue; + FILE *fp; + /* + Each PPM image consists of the following: + + A "magic number" for identifying the file type. A ppm image's magic number is the two characters "P6". + Whitespace (blanks, TABs, CRs, LFs). + A width, formatted as ASCII characters in decimal. + Whitespace. + A height, again in ASCII decimal. + Whitespace. + The maximum color value (Maxval), again in ASCII decimal. Must be less than 65536 and more than zero. + A single whitespace character (usually a newline). + A raster of Height rows, in order from top to bottom. Each row consists of Width pixels, in order from left to right. Each pixel is a triplet of red, green, and blue samples, in that order. Each sample is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first. + */ + if ( (fp= fopen(fname,"wb")) != 0 ) + { + fprintf(fp,"P6 %d %d 255\n",width,height); + for (j=0; jwidth; i--) + // dest[i] = balanced_ave(src,i,width); + //for (i=width; i>0; i--) + // dest[i] = balanced_ave(src,i,i); + for (i=1; i>16)&0x0ff) + (float)((color>>8)&0x0ff) + (float)((color>>0)&0x0ff))/0x300); +} + +int32_t pixel_ratios(uint32_t red,uint32_t green,uint32_t blue) +{ + float max; + /*if ( red > green ) + max = red; + else + max = green; + if ( blue > max ) + max = blue;*/ + max = (red + green + blue); + if ( max == 0. ) + return(0); + if ( max > 0xff ) + { + red = (uint32_t)(((float)red / max) * 0xff); + green = (uint32_t)(((float)green / max) * 0xff); + blue = (uint32_t)(((float)blue / max) * 0xff); + } + + if ( red > 0xff ) + red = 0xff; + if ( green > 0xff ) + green = 0xff; + if ( blue > 0xff ) + blue = 0xff; + return((red << 16) | (green << 8) | blue); +} + +int32_t conv_yval_to_y(register float yval,register int32_t height) +{ + register int32_t y; + height = (height>>1) - 2; + y = (int32_t)-yval; + if ( y > height ) + y = height; + else if ( y < -height ) + y = -height; + + y += height; + if ( y < 0 ) + y = 0; + height <<= 1; + if ( y >= height-1 ) + y = height-1; + return(y); +} + +uint32_t scale_color(uint32_t color,float strength) +{ + int32_t red,green,blue; + if ( strength < 0. ) + strength = -strength; + red = (color>>16) & 0xff; + green = (color>>8) & 0xff; + blue = color & 0xff; + + red = (int32_t)((float)red * (strength/100.f)); + green = (int32_t)((float)green * (strength/100.f)); + blue = (int32_t)((float)blue * (strength/100.f)); + if ( red > 0xff ) + red = 0xff; + if ( green > 0xff ) + green = 0xff; + if ( blue > 0xff ) + blue = 0xff; + return((red<<16) | (green<<8) | blue); +} + +uint32_t pixel_blend(uint32_t pixel,uint32_t color)//,int32_t groupsize) +{ + int32_t red,green,blue,sum,n,n2,groupsize = 1; + float red2,green2,blue2,sum2; + if ( color == 0 ) + return(pixel); + if ( pixel == 0 ) + { + return((1<<24) | scale_color(color,100.f/(float)groupsize)); + } + n = (pixel>>24) & 0xff; + if ( n == 0 ) + n = 1; + pixel &= 0xffffff; + red = (pixel>>16) & 0xff; + green = (pixel>>8) & 0xff; + blue = pixel & 0xff; + sum = red + green + blue; + + n2 = (color>>24) & 0xff; + if ( n2 == 0 ) + n2 = 1; + red2 = ((float)((color>>16) & 0xff)) / groupsize; + green2 = ((float)((color>>8) & 0xff)) / groupsize; + blue2 = ((float)(color & 0xff)) / groupsize; + sum2 = (red2 + green2 + blue2); + + //printf("gs %d (%d x %d,%d,%d: %d) + (%d x %.1f,%.1f,%.1f: %.1f) = ",groupsize,n,red,green,blue,sum,n2,red2,green2,blue2,sum2); + red = (uint32_t)(((((((float)red / (float) sum) * n) + (((float)red2 / (float) sum2) * n2)) / (n+n2)) * ((sum+sum2)/2))); + green = (uint32_t)(((((((float)green / (float) sum) * n) + (((float)green2 / (float) sum2) * n2)) / (n+n2)) * ((sum+sum2)/2))); + blue = (uint32_t)(((((((float)blue / (float) sum) * n) + (((float)blue2 / (float) sum2) * n2)) / (n+n2)) * ((sum+sum2)/2))); + + n += n2; + if ( n > 0xff ) + n = 0xff; + ///printf("%x (%d,%d,%d) ",color,red,green,blue); + color = (n<<24) | pixel_ratios(red,green,blue);//pixel_overflow(&red,&green,&blue); + + //printf("%x (%d,%d,%d)\n",color,(color>>16)&0xff,(color>>8)&0xff,color&0xff); + return(color); +} + +void init_forex_colors(uint32_t *forex_colors) +{ + int32_t i; + forex_colors[0] = 0x00ff00; + forex_colors[1] = 0x0033ff; + forex_colors[2] = 0xff0000; + forex_colors[3] = 0x00ffff; + forex_colors[4] = 0xffff00; + forex_colors[5] = 0xff00ff; + forex_colors[6] = 0xffffff; + forex_colors[7] = 0xff8800; + forex_colors[8] = 0xff88ff; + for (i=9; i<16; i++) + forex_colors[i] = pixel_blend(forex_colors[i-8],0xffffff); +} + +int32_t is_primary_color(register uint32_t color) +{ + static uint32_t forex_colors[16]; + register int32_t i; + if ( forex_colors[0] == 0 ) + init_forex_colors(forex_colors); + for (i=0; i<8; i++) + if ( color == forex_colors[i] ) + return(1); + return(0); +} + +void disp_yval(register int32_t color,register float yval,register uint32_t *bitmap,register int32_t x,register int32_t rowwidth,register int32_t height) +{ + register int32_t y; + x += LEFTMARGIN; + if ( x < 0 || x >= rowwidth ) + return; + //y = conv_yval_to_y(yval,height/Display_scale) * Display_scale; + y = conv_yval_to_y(yval * Display_scale,height); + if ( 1 && is_primary_color(color) != 0 ) + { + bitmap[y*rowwidth + x] = color; + return; + } + //if ( pixelwt(color) > pixelwt(bitmap[y*rowwidth + x]) ) + bitmap[y*rowwidth + x] = pixel_blend(bitmap[y*rowwidth + x],color); + return; + if ( is_primary_color(color) != 0 || (is_primary_color(bitmap[y*rowwidth+x]) == 0 && pixelwt(color) > pixelwt(bitmap[y*rowwidth + x])) ) + bitmap[y*rowwidth + x] = color; +} + +void disp_yvalsum(register int32_t color,register float yval,register uint32_t *bitmap,register int32_t x,register int32_t rowwidth,register int32_t height) +{ + int32_t y,red,green,blue,dispcolor; + x += LEFTMARGIN; + if ( x < 0 || x >= rowwidth ) + return; + y = conv_yval_to_y(yval * Display_scale,height); + red = (color>>16) & 0xff; + green = (color>>8) & 0xff; + blue = color & 0xff; + dispcolor = bitmap[y*rowwidth + x]; + red += (dispcolor>>16) & 0xff; + green += (dispcolor>>8) & 0xff; + blue += dispcolor & 0xff; + bitmap[y*rowwidth + x] = pixel_ratios(red,green,blue); +} + +void disp_dot(register float radius,register int32_t color,register float yval,register uint32_t *bitmap,register int32_t x,register int32_t rowwidth,register int32_t height) +{ + register float i,j,sq,val; + if ( radius > 1 ) + { + sq = radius * radius; + for (i=-radius; i<=radius; i++) + { + for (j=-radius; j<=radius; j++) + { + val = ((j*j + i*i) / sq); + if ( val <= 1. ) + { + val = 1. - val; + disp_yval(scale_color(color,(100 * val * val * val * val)),yval+j,bitmap,x+i,rowwidth,height); + } + } + } + } + else disp_yval(color,yval,bitmap,x,rowwidth,height); +} + +void horizline(int32_t calclogflag,int32_t rowwidth,int32_t height,uint32_t *bitmap,double rawprice,double ave) +{ + int32_t x; + double yval; + if ( calclogflag != 0 ) + yval = _calc_pricey(log(rawprice),log(ave)); + else yval = _calc_pricey(rawprice,ave); + for (x=0; x .0000000001 ) + { + aveabs += fabs(yval); + nonz++; + if ( color != 0 ) + disp_yval(color,yval,bitmap,x,rowwidth,height); + } + } else yval = 0.; + output[x] = yval; + } + if ( nonz != 0 ) + aveabs /= nonz; + return(aveabs); + // + //printf("ave %f rowwidth.%d\n",ave,rowwidth); +} + +void output_line(int32_t calclogflag,double ave,float *buf,int32_t n,int32_t color,uint32_t *bitmap,int32_t rowwidth,int32_t height) +{ + double src[1024],dest[1024]; int32_t i; + memset(src,0,sizeof(src)); + memset(dest,0,sizeof(dest)); + if ( 1 ) + { + for (i=0; i<1024; i++) + src[1023-i] = dest[1023-i] = buf[i]; + smooth1024(dest,src,3); + for (i=0; i<1024; i++) + src[1023-i] = dest[i]; + } + else + { + for (i=0; i<1024; i++) + src[i] = buf[i]; + } + _output_line(calclogflag,ave,buf,src,1024,color,bitmap,rowwidth,height); +} + +void set_bi_fields(register struct bq_info *bi,register int32_t color,register int32_t signalid,register int32_t contractnum,register int32_t jdatetime,register int32_t timewindow,register uint32_t *bitmap,register int32_t rowwidth,register int32_t width,register int32_t height) +{ + memset(bi,0,sizeof(*bi)); + bi->color = color; + bi->jdatetime = jdatetime; + bi->timewindow = timewindow; + bi->bitmap = bitmap; + bi->rowwidth = rowwidth; + bi->width = width; + bi->height = height; +} + +void set_bi_jdatetime(register struct bq_info *bi,register int32_t endjdatetime) +{ + bi->timewindow = bi->timescale * bi->width; + bi->jdatetime = (endjdatetime - bi->timewindow); + //printf("%s <- %s - %d\n",jdatetime_str(bi->jdatetime),jdatetime_str2(endjdatetime),bi->timewindow); +} + +void init_display_template(register struct display_template *dp,register uint32_t *bitmap)//,register int32_t topslice_height) +{ + register int32_t timewindow,rowwidth,width,height,jdatetime,i,j,topslice_height=0,signalid = 0; + timewindow = 0; + signalid = 0; + jdatetime = 0; + width = dp->rowwidth; + rowwidth = width; + height = dp->windheight; + if ( topslice_height != 0 ) + { + set_bi_fields(&dp->topslice,0x00ff00,signalid,-1,jdatetime,width*60,bitmap,rowwidth,width/2,topslice_height); + set_bi_fields(&dp->topslice2,0x00ff00,signalid,-1,jdatetime,width*60,bitmap+(width/2)+1,rowwidth,width/2-1,topslice_height); + bitmap += topslice_height*rowwidth; + height -= topslice_height; + } + set_bi_fields(&dp->fullscreen,0x00ff00,signalid,-1,jdatetime,timewindow,bitmap,rowwidth,width,height); + bitmap++; + width -= 2; + height -= 2; + { + int32_t x,upperleft,w,h; + //width -= 14; + //height -= 14; + //bitmap += 14; + w = ((width-16)/8); + h = ((height-18)/8); + for (i=0; i<8; i++) + { + for (j=0; j<8; j++) + { + upperleft = ((rowwidth * (h*i + i*2 + 1)) + (w*j + 2*j + 1)); + x = (int)(((unsigned long)&bitmap[upperleft] - (unsigned long)bitmap)/sizeof(int)); + //printf("%p %d sixtyfour[%d][%d] upperleft %d:%d, width %d, height %d\n",bitmap+upperleft,x,i,j,upperleft/rowwidth,upperleft%rowwidth,width/8,height/8); + set_bi_fields(&dp->sixtyfourscreen[i*8 + j],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap + upperleft,rowwidth,w,h); + //dp->sixtyfourscreen[i*8 + j].permi = (i*8 + j); + } + } + //width += 14; + //height += 14; + //bitmap -= 14; + } + set_bi_fields(&dp->halfscreenV[0],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap,rowwidth,width/2,height); + set_bi_fields(&dp->halfscreenV[1],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(width/2)+1,rowwidth,width/2,height); + set_bi_fields(&dp->halfscreenH[0],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap,rowwidth,width,height/2); + set_bi_fields(&dp->halfscreenH[1],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2)),rowwidth,width,height/2); + + bitmap++; + width -= 2; + height -= 2; + set_bi_fields(&dp->quarterscreen[0],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap,rowwidth,width/2,height/2); + set_bi_fields(&dp->quarterscreen[1],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(width/2)+1,rowwidth,width/2,height/2); + set_bi_fields(&dp->quarterscreen[2],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2)),rowwidth,width/2,height/2); + set_bi_fields(&dp->quarterscreen[3],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2))+(width/2+1),rowwidth,width/2,height/2); + + bitmap++; + width -= 2; + height -= 2; + set_bi_fields(&dp->screenx8[0],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap,rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[1],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(width/4),rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[2],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(width/2)+1,rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[3],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(3*width/4)+2,rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[4],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2)),rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[5],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2))+(width/4),rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[6],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2))+(width/2+1),rowwidth,width/4,height/2); + set_bi_fields(&dp->screenx8[7],0x00ff00,signalid,-1,jdatetime,timewindow,bitmap+(rowwidth * (height/2))+(3*width/4)+2,rowwidth,width/4,height/2); +} + +struct display_template *create_display_template(register char *java_bitmap_fname,register int32_t chartwidth,register int32_t chartheight,register int32_t topslice_height,register int32_t microsleep) +{ + register struct display_template *display; + display = malloc(sizeof(*display)); + memset(display,0,sizeof(*display)); + display->windheight = chartheight + TITLEBAR_HEIGHT; + display->chartheight = chartheight; + display->rowwidth = chartwidth; + strcpy(display->title,java_bitmap_fname); + display->microsleep = microsleep; + return(display); +} + +struct bq_info *bi_selector(register struct display_template *dp,register int32_t sizesel,register int32_t slot) +{ + register struct bq_info *bi; + switch ( sizesel ) + { + case DP_FULLSCREEN: + bi = &dp->fullscreen; + break; + case DP_HALFSCREENV: + bi = &dp->halfscreenV[slot]; + break; + case DP_HALFSCREENH: + bi = &dp->halfscreenH[slot]; + break; + case DP_QUARTERSCREEN: + bi = &dp->quarterscreen[slot]; + break; + case DP_SCREEN8: + bi = &dp->screenx8[slot]; + break; + case DP_TOPSLICE: + bi = &dp->topslice; + break; + case DP_TOPSLICE2: + bi = &dp->topslice2; + break; + case DP_64SCREEN: + /*if ( (slot % 8) != 0 ) + slot--; + else + slot += 7;*/ + bi = &dp->sixtyfourscreen[slot]; + break; + default: + return(0); + } + if ( bi->width == 0 || bi->height == 0 ) + return(0); + return(bi); +} + +void fill_bqinfo(register struct bq_info *bi,register int32_t color) +{ + register int32_t i; + register uint32_t *src; + src = bi->bitmap; + for (i=0; iheight; i++,src+=bi->rowwidth) + memset(src,color,sizeof(*src)*bi->width); +} + +int32_t draw_bqpixel(register uint32_t *bitmap,register int32_t color,register int32_t blendflag,register int32_t x,register float yval,register int32_t height,register int32_t rowwidth) +{ + register int32_t y; + y = conv_yval_to_y(yval,height); + bitmap += y*rowwidth + x; + if ( blendflag != 0 ) + return(*bitmap = pixel_blend(*bitmap,color)); + else + return(*bitmap = color); +} + +void draw_bqbuf(register int32_t filter,register float scale,register int32_t color,register int32_t blendflag,register struct bq_info *bi,register float *buf,register int32_t firstx) +{ + register int32_t i,x,width,n; + register float val,factor; + width = bi->width; + if ( buf == 0 ) + { + for (x=0; xbitmap,color,blendflag,x,scale,bi->height,bi->rowwidth); + return; + } + n = 0; + factor = 0; + if ( filter != 0. ) + { + for (i=0; iwidth; + if ( (val= buf[x]*scale)*filter >= 0 ) + { + bi->linebuf[i] = val; + factor += fabs(val); + n++; + } + } + } + else + { + for (i=0; iwidth; + if ( (val = buf[x] * scale) != 0. ) + { + //val = xdisp_cbrtf(val); + //printf("%f ",val); + bi->linebuf[i] = val; + factor += fabs(val); + n++; + } + } + } + if ( n != 0 && factor > SMALLVAL ) + { + //printf("factor %f, -> %f, width %d\n",factor,(factor / n) * bi->height * .2f,width); + factor = (factor / n) * 2.; + for (i=0; ibitmap,color,blendflag,i,bi->linebuf[i]*factor,bi->height,bi->rowwidth); + } +} + +void draw_bq_horiz(register float yval,register int32_t color,register struct bq_info *bi) +{ + register uint32_t i,y,width,*bitmap; + y = conv_yval_to_y(yval,bi->height); + //printf("horiz yval %f -> y %d, color %x\n",yval,y,color); + width = bi->rowwidth; + bitmap = bi->bitmap + y*bi->rowwidth; + for (i=0; ibitmap + x; + rowwidth = bi->rowwidth; + for (y=0; yheight; y++,bitmap+=rowwidth) + *bitmap = color; +} + +void draw_rect(register int32_t color,register int32_t startx,register int32_t endx,register int32_t starty,register int32_t endy,register uint32_t *bitmap,register int32_t rowwidth) +{ + register int32_t y,j,width = (endx - startx + 1); + bitmap += (starty*rowwidth) + startx; + for (y=starty; y<=endy; y++,bitmap+=rowwidth) + { + for (j=0; jbitmap = Display_bitmaps[Display_contractnum]; + } + bitmap = bi->bitmap; + height = bi->height; + rowwidth = bi->rowwidth; + memset(bitmap,0,IGUANA_HEIGHT*IGUANA_WIDTH*sizeof(int)); + endind = display_weekind; + //if ( display_weekind < NUM_ACTIVE_PIXELS+MAX_LOOKAHEAD ) + // return; + while ( 0 && (startind = endind - NUM_ACTIVE_PIXELS*clumpsize) < 0 ) + { + Display_clumpsize = MIN(Display_clumpsize,(endind - startind)/NUM_ACTIVE_PIXELS); + if ( Display_clumpsize < 1 ) + { + Display_clumpsize = 1; + break; + } + clumpsize = Display_clumpsize; + } + yoffset = 0; + clumpsize = MAX(1,Display_clumpsize); + memset(polarities,0,sizeof(polarities)); + mainscale = MAX(1,(24. / Display_clumpsize))/2; + Parsedx = NUM_ACTIVE_PIXELS-1; + Processedx = -1; + //draw_mouserects(bitmap,rowwidth); + for (c=0; c= NUM_CONTRACTS && c < 36 ) + { + if ( c < NUM_CONTRACTS ) + { + if ( Contract_base[c] == contractnum-NUM_CONTRACTS ) + polarities[c] = 1.f, SETBIT(dispbits,c); + else if ( Contract_rel[c] == contractnum-NUM_CONTRACTS ) + polarities[c] = -1.f, SETBIT(dispbits,c); + } + } + } + //draw_bq_horiz(0-1,0x888888,bi); + draw_bq_horiz(0,0x444444,bi); + //draw_bq_horiz(0+1,0x888888,bi); + draw_bq_horiz(BOTTOM_LINE+150,0x444444,bi); + draw_bq_horiz(BOTTOM_LINE,0x888888,bi); + draw_bq_horiz(TOP_LINE,0x888888,bi); + draw_bq_horiz(TOP_LINE-150,0x444444,bi); + + draw_bq_horiz(0,0x444400,bi); + } + else + { + for (base=0; base<8; base++) + SETBIT(dispbits,NUM_CONTRACTS+base); + base = 0; + zerolines[NUM_CONTRACTS+base++] = 600/2; + zerolines[NUM_CONTRACTS+base++] = 425/2; + zerolines[NUM_CONTRACTS+base++] = 250/2; + zerolines[NUM_CONTRACTS+base++] = 75/2; + zerolines[NUM_CONTRACTS+base++] = -75/2; + zerolines[NUM_CONTRACTS+base++] = -250/2; + zerolines[NUM_CONTRACTS+base++] = -425/2; + zerolines[NUM_CONTRACTS+base++] = -600/2; + for (c=0; c<28; c++) + { + polarities[c] = 1.f; + zerolines[c] = zerolines[c % 8]; + } + for (base=0; base<8; base++) + { + polarities[base+28] = 1.f; + draw_bq_horiz(zerolines[base+28],scale_color(forex_colors[base],25),bi); + weekind = display_weekind + (0 - NUM_ACTIVE_PIXELS)*clumpsize; + + disp_contractvals(weekind*60+59,(weekind-display_weekind),clumpsize,weekind,c,0,-1,bitmap,rowwidth,height); + for (x=0; x display_weekind+1024 ) + break; + disp_contractvals(weekind*60+59,weekind-display_weekind,clumpsize,weekind,base+28,zerolines[base+28],x,bitmap,rowwidth,height); + } + } + } + for (c=0; c<=NUM_COMBINED; c++) + { + if ( c != Display_contractnum ) + continue; + scale = mainscale; + scale *= polarities[c]; + yoffset = zerolines[c]; + yoffset /= Display_scale; + if ( c == contractnum ) + { + weekind = display_weekind + (0 - NUM_ACTIVE_PIXELS)*clumpsize; + //printf("weekind.%d MAX_LOOKAHEAD %d\n",weekind,MAX_LOOKAHEAD); + if ( weekind < NUM_WEEKINDS-NUM_ACTIVE_PIXELS*clumpsize ) + { + //printf("parsed %d, weekind.%d clumpsize.%d\n",display_weekind,weekind,clumpsize); + disp_contractvals(weekind*60+59,weekind-display_weekind,clumpsize,weekind,c,0,-1,bitmap,rowwidth,height); + for (x=0; x display_weekind+1024 ) + break; + if ( weekind > TRADECONTAMINATION/60 ) + disp_contractvals(weekind*60+59,weekind-display_weekind,clumpsize,weekind,c,0,x,bitmap,rowwidth,height); + } + } + } + } + for (j=-height/Display_scale; j= 0 ) + disp_yval(0x008800,j,bitmap,Processedx,rowwidth,height); + */ + if ( j >= (TOP_LINE+125)/Display_scale || j <= (BOTTOM_LINE-135)/Display_scale ) + { + int32_t i; + for (i=1; i<24; i++) + { + if ( i*(60/clumpsize) < Parsedx ) + disp_yval(0x333333,j,bitmap,Parsedx - i*(60/clumpsize),rowwidth,height); + } + } + } +} + +void *display_loop(void *ptr) +{ + struct bq_info BI; + register double aveprice; + register int32_t c,tradechar,weeknum,jdatetime,actual_timeind,display_timeind,docid,pass,counter=0; + register struct display_template *display; + display = ptr; + docid = weeknum = pass = jdatetime = -1; + sleep(5); + while ( Nodisplay != 0 ) + sleep(1); + while ( 1 ) + { + char title[BUFSIZE]; + char *tradeinfo_str(register int32_t contractnum,register int32_t jdatetime); + char kstr[16]; + memset(&BI,0,sizeof(BI)); + BI.height = IGUANA_HEIGHT; + BI.width = IGUANA_WIDTH; + BI.rowwidth = IGUANA_WIDTH; + BI.timescale = display->clumpsize; + + if ( Display_bitmaps[NUM_COMBINED] == 0 ) + Display_bitmaps[NUM_COMBINED] = alloc_aligned_buffer(sizeof(*Display_bitmaps[NUM_COMBINED]) * IGUANA_WIDTH * IGUANA_HEIGHT); + if ( Display_bitmaps[Display_contractnum] == 0 ) + Display_bitmaps[Display_contractnum] = alloc_aligned_buffer(sizeof(*Display_bitmaps[Display_contractnum]) * IGUANA_WIDTH * IGUANA_HEIGHT); + BI.bitmap = Display_bitmaps[NUM_COMBINED]; + if ( Parsed_weekinds[Display_contractnum] == 0 ) + { + Parsed_weekinds[Display_contractnum] = Parsedbits[Display_contractnum].weekind;//1024+Display_clumpsize;//*1.5*NUM_ACTIVE_PIXELS; + //Display_clumpsize = 750; + } + display->clumpsize = Display_clumpsize; + BI.timewindow = BI.width*display->clumpsize; + //Parsed_jdatetime = wbits_to_jdatetime(valid_wbits(parsed_weekind),0); + //get_lockid(DISPLAY_LOCKID); + actual_timeind = jdatetime_to_timeind(actual_gmt_jdatetime() + 0*dst_adjust(actual_gmt_jdatetime())); + display_timeind = (Display_mode != 0) ? Parsed_wbits.weekind*60+Parsed_wbits.b.len : actual_timeind;//Processed_wbits.weekind;//Parsed_weekinds[Display_contractnum] : Processedbits[Display_contractnum].weekind; + //printf("Parsed_wbits.weekind %d, actual_weekind %d| actual %d 1st %d\n",Parsed_wbits.weekind,actual_weekind,actual_gmt_jdatetime(),_FIRSTJDATETIME); + update_display(&BI,display_timeind/60,Display_contractnum,LEFTMARGIN,SCREENCLUMPSIZE); + + sprintf(kstr,".d%d",Display_k); + //sprintf(title,"%d %s R%d%c %s %7.6f %7.6f %s w%d %.3f |%s",counter,CONTRACTS[Display_contractnum],Display_clumpsize,jdatetime_str(wbits_to_jdatetime(valid_wbits(Parsed_weekinds[Display_contractnum]),0)),exp(Latestbids[Display_contractnum]),exp(Dispweekaves[Display_contractnum]),Display_pause!=0?"STOP":"",Parsed_weekinds[Display_contractnum],Display_scale,dispREQ_str(Display_REQ_selector)); + if ( Display_contractnum != NUM_COMBINED ) + aveprice = Lastprices[Display_contractnum];//_Quotes_aved(refc_to_c(Display_contractnum),actual_weekind*60-60,60);//_bufaved(&SVMS[c_to_refc(Display_contractnum)]->D->aveprices[display_weekind-8],8); + //SVMS[Display_contractnum%NUM_COMBINED]->TS.pending-SVMS[Display_contractnum%NUM_COMBINED]->TS.gainsum + //if ( Dispweekaves[Display_contractnum] == 0 ) + // Dispweekaves[Display_contractnum] = _pairaved(Latestbids[Display_contractnum],Latestasks[Display_contractnum]); + tradechar = ' '; + if ( Display_contractnum < 36 ) + { + if ( SVMS[Display_contractnum]->TS.dir > 0 ) + tradechar = '+'; + else if ( SVMS[Display_contractnum]->TS.dir < 0 ) + tradechar = '-'; + } + sprintf(title,"%d %c%s.%s R%d%c %8.6f %8.6f %s w%d lag.%d %11.7f %s t%d",counter,tradechar,CONTRACTS[Display_contractnum],Display_contractnum==NUM_COMBINED?CURRENCIES[Display_k]:"",Display_clumpsize,'a'+Display_j,exp(aveprice),exp(Dispweekaves[Display_contractnum]),Display_pause!=0?"STOP":"",display_timeind/60,actual_timeind - display_timeind,Display_scale,jdatetime_str(actual_gmt_jdatetime()),actual_timeind);//,Disp_masizes[0][0],Disp_masizes[0][1],Disp_masizes[1][0],Disp_masizes[1][1]);//,dispREQ_str(Display_REQ_selector)); + strncpy(display->title,title,sizeof(display->title)-2); + display->title[sizeof(display->title)-1] = 0; + counter++; + if ( display->java_bitmap != 0 ) + { + //for (i=0; ijava_bitmap,BI.bitmap,IGUANA_HEIGHT*IGUANA_WIDTH*sizeof(int)); + while ( mmbitmap_updated(display->vp) == 0 ) + usleep(100); + mmbitmap_update(display->vp); + //release_lockid(DISPLAY_LOCKID); +#ifndef JDATA_MODE + int32_t display_idle(register int32_t microsleep); + display_idle(display->microsleep); +#endif + mmbitmap_set_title(display->vp,display->title); + //if ( display->microsleep != 0 ) + // usleep(display->microsleep); + } + /*if ( Display_pause != 0 ) + Parsed_weekinds[Display_contractnum] += Display_clumpsize;//MAX(60,Display_clumpsize)+(0*(IGUANA_WIDTH/2) * Display_clumpsize); + if ( Parsed_weekinds[Display_contractnum] > NUM_WEEKINDS-60 ) + { + Display_pause = 1; + Parsed_weekinds[Display_contractnum] = NUM_WEEKINDS-60; + }*/ + if ( 0 ) + { + if ( Parsed_weekinds[Display_contractnum] < Parsedbits[Display_contractnum].weekind ) + Parsed_weekinds[Display_contractnum] = Parsedbits[Display_contractnum].weekind;//jdatetime_to_wbits(actual_gmt_jdatetime()).weekind; + } + else + { + jdatetime = actual_gmt_jdatetime(); + Parsed_weekinds[Display_contractnum] = jdatetime_to_wbits(jdatetime).weekind + 0*(dst_adjust(jdatetime)/FASTEST_RESOLUTION); + } + for (c=0; cred == 0 ) + bp->red = rand(), bp->green = rand(), bp->blue = rand(); + red = green = blue = 0; + frac = (double)bp->n / (width * height); + for (y=0; yramchain.H.data != 0 ) + { + blue = 0xff; + if ( bp->bundleheight+(int32_t)sum > coin->blocks.hwmchain.height ) + red = 0xff, green = 0xff, blue = 0; + else green = 0xff, blue = 0, red = 0; + } + else + { + red = green = blue = 0; + if ( (block= bp->blocks[(int32_t)sum]) != 0 ) + { + blue = 0xff; + if ( block->RO.recvlen != 0 ) + green = 0xcc; + else green = 0x40; + if ( block->fpipbits != 0 ) + red = 0xcc; + else red = 0x40; + } + } + *ptr++ = red, *ptr++ = green, *ptr++ = blue; + } + } + } +} + +struct iguana_bitmap *iguana_bitmapfind(char *name) +{ + struct iguana_info *coin; int32_t width,height,n,hdrsi,x,y; + if ( (coin= iguana_coinfind(name)) != 0 || (coin= iguana_coinfind("BTCD")) != 0 ) + { + strcpy(coin->screen.name,coin->symbol); + coin->screen.amplitude = 255; + coin->screen.width = IGUANA_WIDTH; + coin->screen.height = IGUANA_HEIGHT; + memset(coin->screen.data,0xff,sizeof(coin->screen.data)); + if ( coin->bundlescount > 0 ) + { + n = 100; + while ( n > 0 ) + { + width = IGUANA_WIDTH / n; + height = IGUANA_HEIGHT / n; + //printf("n.%d -> (%d %d) rects.%d vs %d\n",n,width,height,width*height,coin->bundlescount); + if ( width*height >= coin->bundlescount ) + break; + n--; + } + for (y=hdrsi=0; y= coin->bundlescount ) + break; + iguana_bitmapbundle(coin,&coin->screen.data[3*(y*coin->screen.width*n + x*n)],coin->screen.width,n,n,coin->bundles[hdrsi]); + } + } + } + return(&coin->screen); + } + return(0); +} + +void iguana_bitmap(char *space,int32_t max,char *name) +{ + struct iguana_info *coin; struct iguana_bitmap *rect; char pixel[64],fname[512]; + uint8_t *ptr; int32_t h,w,red,green,blue,x,y,n,len = 0; + if ( name == 0 || name[0] == 0 ) + name = "BTCD"; + coin = iguana_coinfind(name); + if ( (rect= iguana_bitmapfind(name)) == 0 ) + { + strcpy(space,"{\"name\":\"nobitmap\",\"amplitude\":222,\"width\":1,\"height\":1,\"pixels\":[222,0,22]}"); + //sprintf(space,"Content-type: text/standard\r\n"); + //sprintf(space+strlen(space),"Content-Length: %ld\r\n\r\n",strlen(buf)); + //strcpy(space,buf); + //printf("bitmap.[%s]\n",space); + } + else + { + sprintf(space,"{\"name\":\"%s\",\"status\":\"%s\",\"amplitude\":%u,\"width\":%d,\"height\":%d,\"pixels\":[",name,coin!=0?coin->statusstr:"no coin",rect->amplitude,rect->width,rect->height), len = (int32_t)strlen(space); + ptr = rect->data; + h = rect->height, w = rect->width; + for (y=0; ydata,rect->width,rect->height); + } + //printf("BIGMAP.(%s)\n",space); + } +} diff --git a/iguana/iguana_blocks.c b/iguana/iguana_blocks.c new file mode 100755 index 000000000..9a169f144 --- /dev/null +++ b/iguana/iguana_blocks.c @@ -0,0 +1,424 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +#define iguana_blockfind(coin,hash2) iguana_blockhashset(coin,-1,hash2,0) + +void _iguana_blocklink(struct iguana_info *coin,struct iguana_block *prev,struct iguana_block *block) +{ + char str[65],str2[65]; struct iguana_block *next; + if ( memcmp(block->RO.prev_block.bytes,prev->RO.hash2.bytes,sizeof(bits256)) != 0 ) + { + printf("illegal blocklink mismatched hashes\n"); + exit(-1); + return; + } + block->hh.prev = prev; + if ( (next= prev->hh.next) != 0 ) + { + if ( next != block ) + { + if ( memcmp(next->RO.prev_block.bytes,prev->RO.hash2.bytes,sizeof(bits256)) != 0 ) + { + printf("illegal blocklink next mismatched hashes\n"); + return; + } + if ( memcmp(next->RO.hash2.bytes,block->RO.hash2.bytes,sizeof(bits256)) != 0 ) + printf("blocklink collision: %s vs %s\n",bits256_str(str,block->RO.hash2),bits256_str(str2,next->RO.hash2)); + else printf("blocklink corruption: identical hashes with diff ptrs %s %p %p\n",bits256_str(str,block->RO.hash2),block,next); + } + prev->hh.next = block; // could make a linked list of all at same height for multibranch + } else prev->hh.next = block; + printf("link.(%s) -> (%s)\n",bits256_str(str,prev->RO.hash2),bits256_str(str,block->RO.hash2)); +} + +struct iguana_block *iguana_blockhashset(struct iguana_info *coin,int32_t height,bits256 hash2,int32_t createflag) +{ + static int depth; + struct iguana_block *block,*prev; + if ( height > coin->blocks.maxbits || depth != 0 ) + { + printf("illegal height.%d when max.%d, depth.%d\n",height,coin->blocks.maxbits,depth); + getchar(); + return(0); + } + depth++; + //portable_mutex_lock(&coin->blocks_mutex); + HASH_FIND(hh,coin->blocks.hash,&hash2,sizeof(hash2),block); + if ( block != 0 ) + { + //portable_mutex_unlock(&coin->blocks_mutex); + depth--; + return(block); + } + if ( createflag > 0 ) + { + block = mycalloc('y',1,sizeof(*block)); + block->RO.hash2 = hash2; + block->hh.itemind = height, block->height = -1; + HASH_ADD(hh,coin->blocks.hash,RO.hash2,sizeof(hash2),block); + block->hh.next = block->hh.prev = 0; + if ( bits256_nonz(block->RO.prev_block) > 0 ) + { + HASH_FIND(hh,coin->blocks.hash,&block->RO.prev_block,sizeof(block->RO.prev_block),prev); + if ( prev != 0 ) + _iguana_blocklink(coin,prev,block); + } + //char str[65]; printf("added.(%s) height.%d (%p %p)\n",bits256_str(str,hash2),height,block->hh.prev,block->hh.next); + if ( 0 ) + { + struct iguana_block *tmp; + HASH_FIND(hh,coin->blocks.hash,&hash2,sizeof(hash2),tmp); + char str[65]; + bits256_str(str,hash2); + if ( tmp != block ) + printf("%s height.%d search error %p != %p\n",str,height,block,tmp); + } + } + //portable_mutex_unlock(&coin->blocks_mutex); + depth--; + return(block); +} + +bits256 *iguana_blockhashptr(struct iguana_info *coin,int32_t height) +{ + int32_t i; struct iguana_bundle *bp; bits256 *hashptr; + if ( height >= 0 ) + { + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + if ( height >= bp->bundleheight && height < bp->bundleheight+bp->n ) + { + hashptr = &bp->hashes[height - bp->bundleheight]; + //printf("i.%d hashptr.%p height.%d vs (%d %d)\n",i,hashptr,height,bp->bundleheight,bp->bundleheight+bp->n); + return(hashptr); + } + } + } + } + return(0); +} + +bits256 iguana_blockhash(struct iguana_info *coin,int32_t height) +{ + bits256 *hash2p; static const bits256 zero; + if ( (hash2p= iguana_blockhashptr(coin,height)) != 0 ) + return(*hash2p); + return(zero); +} + +struct iguana_block *iguana_blockptr(struct iguana_info *coin,int32_t height) +{ + static const bits256 zero; bits256 hash2 = iguana_blockhash(coin,height); + if ( memcmp(zero.bytes,hash2.bytes,sizeof(zero)) != 0 ) + return(iguana_blockfind(coin,hash2)); + return(0); +} + +int32_t iguana_blockvalidate(struct iguana_info *coin,int32_t *validp,struct iguana_block *block) +{ + bits256 hash2; uint8_t serialized[sizeof(struct iguana_msgblock) + 4096]; + *validp = 0; + iguana_serialize_block(&hash2,serialized,block); + *validp = (memcmp(hash2.bytes,block->RO.hash2.bytes,sizeof(hash2)) == 0); + block->valid = *validp; + char str[65]; char str2[65]; + if ( *validp == 0 ) + { + printf("iguana_blockvalidate: miscompare (%s) vs (%s)\n",bits256_str(str,hash2),bits256_str(str2,block->RO.hash2)); + return(-1); + } + return(0); +} + +/*void iguana_mergeprevdep(struct iguana_prevdep *destlp,struct iguana_prevdep *srclp) +{ + if ( srclp->numpkinds > destlp->numpkinds ) + destlp->numpkinds = srclp->numpkinds; + if ( srclp->numtxids > destlp->numtxids ) + destlp->numtxids = srclp->numtxids; + if ( srclp->numunspents > destlp->numunspents ) + destlp->numunspents = srclp->numunspents; + if ( srclp->numspends > destlp->numspends ) + destlp->numspends = srclp->numspends; + if ( srclp->PoW > destlp->PoW ) + destlp->PoW = srclp->PoW; +} + +void iguana_blockmerge(struct iguana_block *dest,struct iguana_prevdep *destlp,struct iguana_block *block,struct iguana_prevdep *srclp) +{ + if ( block->numvins > dest->numvins ) + dest->numvins = block->numvins; + if ( block->numvouts > dest->numvouts ) + dest->numvouts = block->numvouts; + if ( block->txn_count > dest->txn_count ) + dest->txn_count = block->txn_count; + if ( dest->height == 0 ) + dest->height = block->height; + iguana_mergeprevdep(destlp,srclp); +}*/ + +void iguana_blockconv(struct iguana_block *dest,struct iguana_msgblock *msg,bits256 hash2,int32_t height) //uint32_t numtxids,uint32_t numunspents,uint32_t numspends,double PoW) +{ + memset(dest,0,sizeof(*dest)); + dest->RO.version = msg->H.version; + dest->RO.prev_block = msg->H.prev_block; + dest->RO.merkle_root = msg->H.merkle_root; + dest->RO.timestamp = msg->H.timestamp; + dest->RO.bits = msg->H.bits; + dest->RO.nonce = msg->H.nonce; + dest->RO.txn_count = msg->txn_count; + dest->height = height; + dest->RO.hash2 = hash2; +} + +void iguana_blockcopy(struct iguana_info *coin,struct iguana_block *block,struct iguana_block *origblock) +{ + block->RO.hash2 = origblock->RO.hash2; + block->RO.prev_block = origblock->RO.prev_block; + block->RO.merkle_root = origblock->RO.merkle_root; + block->mainchain = origblock->mainchain; + if ( block->fpipbits == 0 ) + block->fpipbits = origblock->fpipbits; + if ( block->RO.timestamp == 0 ) + block->RO.timestamp = origblock->RO.timestamp; + if ( block->RO.nonce == 0 ) + block->RO.nonce = origblock->RO.nonce; + if ( block->RO.bits == 0 ) + block->RO.bits = origblock->RO.bits; + if ( block->RO.txn_count == 0 ) + block->RO.txn_count = origblock->RO.txn_count; + if ( block->RO.version == 0 ) + block->RO.version = origblock->RO.version; + if ( block->mainchain == 0 ) + block->mainchain = origblock->mainchain; + if ( block->valid == 0 ) + block->valid = origblock->valid; + if ( block->RO.recvlen == 0 ) + block->RO.recvlen = origblock->RO.recvlen; +} + +double PoW_from_compact(uint32_t nBits,uint8_t unitval) // NOT consensus safe, but most of the time will be correct +{ + uint32_t nbytes,nbits,i,n; double PoW; uint64_t mult; + nbytes = (nBits >> 24) & 0xFF; + nbits = (8 * (nbytes - 3)); + PoW = (nBits & 0xFFFFFF); + if ( nbytes > unitval ) + { + printf("illegal nBits.%x\n",nBits); + return(0.); + } + if ( (n= ((8* (unitval-3)) - nbits)) != 0 ) // 0x1d00ffff is genesis nBits so we map that to 1. + { + //printf("nbits.%d -> n.%d\n",nbits,n); + if ( n < 64 ) + PoW /= (1LL << n); + else // very rare case efficiency not issue + { + for (i=0; i %.15f diff %.15f | n.%d unitval.%d nbytes.%d\n",nBits,PoW,1./PoW,n,unitval,nbytes); + return(PoW); +} + +int32_t iguana_blockunmain(struct iguana_info *coin,struct iguana_block *block) +{ + struct iguana_block *next; int32_t n = 0; + while ( block != 0 ) + { + //printf("n.%d %p (%p %p) mainchain.%d delink %d\n",n,block,block->hh.prev,block->hh.next,block->mainchain,block->height); + block->mainchain = 0; + next = block->hh.next; + block = next; + n++; + } + if ( n > 1000 ) + printf("delinked.%d\n",n); + return(n); +} + +struct iguana_block *_iguana_chainlink(struct iguana_info *coin,struct iguana_block *newblock) +{ + int32_t valid,bundlei,height=-1; struct iguana_block *hwmchain,*block = 0,*prev=0,*next; + bits256 *hash2p=0; double prevPoW = 0.; + if ( newblock == 0 ) + return(0); + hwmchain = &coin->blocks.hwmchain; + if ( (block= iguana_blockfind(coin,newblock->RO.hash2)) != 0 ) + { + if ( memcmp(coin->chain->genesis_hashdata,block->RO.hash2.bytes,sizeof(bits256)) == 0 ) + block->PoW = PoW_from_compact(block->RO.bits,coin->chain->unitval), height = 0; + else if ( (prev= iguana_blockfind(coin,block->RO.prev_block)) != 0 ) + { + if ( memcmp(prev->RO.hash2.bytes,coin->blocks.hwmchain.RO.hash2.bytes,sizeof(bits256)) == 0 ) + prev->mainchain = 1; + if ( prev->valid != 0 && prev->mainchain != 0 && prev->height >= 0 ) + { + prevPoW = prev->PoW; + block->PoW = PoW_from_compact(block->RO.bits,coin->chain->unitval) + prevPoW; + if ( (next= prev->hh.next) != 0 ) + { + if ( next->mainchain != 0 && block->PoW < next->PoW ) + return(0); + hwmchain = next; + } + height = prev->height + 1; + } + else + { + char str[65]; printf("(%s) notready v.%d m.%d h.%d\n",bits256_str(str,prev->RO.hash2),prev->valid,prev->mainchain,prev->height); + return(0); + } + } + else + { + //char str[65]; printf("chainlink error: cant find prev.(%s)\n",bits256_str(str,block->hash2)); + //getchar(); + return(0); + } + //char str[65]; printf("extend? %s.h%d: %.15f vs %.15f ht.%d vs %d\n",bits256_str(str,block->hash2),height,block->PoW,coin->blocks.hwmchain.PoW,height,coin->blocks.hwmchain.height); + if ( iguana_blockvalidate(coin,&valid,newblock) < 0 || valid == 0 ) + return(0); + block->height = height; + block->valid = 1; + if ( block->PoW >= hwmchain->PoW ) + { + block->hh.prev = prev; + if ( prev != 0 ) + { + //if ( prev->hh.next != block ) + // iguana_blockunmain(coin,prev->hh.next); + prev->hh.next = block; + } + if ( coin->isRT != 0 || block->height == hwmchain->height ) + { + coin->blocks.maxblocks = (block->height + 1); + coin->blocks.hwmchain = *block; + //printf("[%s] <- ht.%d\n",bits256_str(str,block->hash2),coin->blocks.hwmheight); + char str[65],str2[65]; bits256 zero; + memset(&zero,0,sizeof(zero)); + bits256_str(str,newblock->RO.hash2); + if ( hash2p != 0 ) + bits256_str(str2,*hash2p); + else str2[0] = 0; + if ( block->height+1 > coin->longestchain ) + coin->longestchain = block->height+1; + if ( (block->height % 1000) == 0 ) + printf("EXTENDMAIN %s %d <- (%s) n.%u max.%u PoW %f numtx.%d valid.%d\n",str,block->height,str2,hwmchain->height+1,coin->blocks.maxblocks,block->PoW,block->RO.txn_count,block->valid); + if ( (block->height % coin->chain->bundlesize) == 0 ) + iguana_bundlecreate(coin,&bundlei,block->height,block->RO.hash2,zero,1); + else + { + struct iguana_bundle *bp; + if ( (bp= coin->bundles[block->height / coin->chain->bundlesize]) != 0 ) + { + if ( memcmp(bp->hashes[block->height % coin->chain->bundlesize].bytes,block->RO.hash2.bytes,sizeof(bits256)) != 0 ) + { + if ( bits256_nonz(bp->hashes[block->height % coin->chain->bundlesize]) > 0 ) + { + printf("ERROR: need to fix up bundle for height.%d\n",block->height); + //getchar(); + } + bp->hashes[block->height % coin->chain->bundlesize] = block->RO.hash2; + bp->blocks[block->height % coin->chain->bundlesize] = block; + } + } + } + // need to set bp->hash[] here + block->mainchain = 1; + return(block); + } + } + } else printf("chainlink error from block.%p\n",block); + return(0); +} + +void iguana_blocksetheights(struct iguana_info *coin,struct iguana_block *block) +{ + int32_t height; + if ( (height= block->height) < 0 ) + return; + while ( block != 0 && block->height != height ) + { + block->height = height; + iguana_bundlehash2add(coin,0,coin->bundles[height/coin->chain->bundlesize],height % coin->chain->bundlesize,block->RO.hash2); + block = block->hh.next, height++; + } +} + +int32_t iguana_chainextend(struct iguana_info *coin,struct iguana_block *newblock) +{ + struct iguana_block *block,*prev; int32_t valid,oldhwm; char str[65]; + if ( iguana_blockvalidate(coin,&valid,newblock) < 0 || valid == 0 ) + { + printf("chainextend: newblock.%s didnt validate\n",bits256_str(str,newblock->RO.hash2)); + return(-1); + } + else + { + block = iguana_blockhashset(coin,-1,newblock->RO.hash2,1); + if ( block != newblock ) + iguana_blockcopy(coin,block,newblock); + block->valid = 1; + if ( block->hh.prev == 0 && (prev= iguana_blockfind(coin,block->RO.prev_block)) != 0 ) + { + if ( prev->hh.next == 0 && block->hh.prev == 0 ) + prev->hh.next = block, block->hh.prev = prev; + //printf("extend newblock.%s prevm.%d\n",bits256_str(str,block->prev_block),prev->mainchain); + if ( prev->mainchain == 0 ) + { + if ( (block= iguana_blockfind(coin,coin->blocks.hwmchain.RO.hash2)) != 0 && block->mainchain == 0 ) + { + //printf("c hwmchain is not mainchain anymore?\n"); + prev->mainchain = 1; + } else return(0); + } + } + if ( memcmp(block->RO.prev_block.bytes,coin->blocks.hwmchain.RO.hash2.bytes,sizeof(bits256)) != 0 ) + return(0); + } + if ( block != 0 ) + { + oldhwm = coin->blocks.hwmchain.height; + //printf("link.%s\n",bits256_str(str,block->hash2)); + while ( block != 0 && memcmp(block->RO.hash2.bytes,coin->blocks.hwmchain.RO.hash2.bytes,sizeof(bits256)) != 0 && _iguana_chainlink(coin,block) == block && coin->blocks.hwmchain.height != oldhwm ) + { + oldhwm = coin->blocks.hwmchain.height; + block = block->hh.next; + if ( block != 0 ) + { + if ( bits256_nonz(block->RO.prev_block) == 0 ) + break; + //printf("next link.%s\n",bits256_str(str,block->hash2)); + } + } + } + if ( (block= iguana_blockfind(coin,coin->blocks.hwmchain.RO.hash2)) != 0 && block->mainchain == 0 ) + { + printf("hwmchain is not mainchain anymore?\n"); + block->mainchain = 1; + } + return(coin->blocks.hwmchain.height); +} diff --git a/iguana/iguana_bundles.c b/iguana/iguana_bundles.c new file mode 100755 index 000000000..2c22b14df --- /dev/null +++ b/iguana/iguana_bundles.c @@ -0,0 +1,588 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +static uint16_t iguana_primes[] = { 65353, 65357, 65371, 65381, 65393, 65407, 65413, 65419, 65423, 65437, 65447, 65449, 65479, 65497, 65519, 65521 }; + +struct iguana_bloominds { uint16_t inds[8]; }; + +struct iguana_bloominds iguana_calcbloom(bits256 hash2) +{ + int32_t i,j,k; struct iguana_bloominds bit; + k = (int32_t)(sizeof(bit)/sizeof(uint16_t)) - 1; + j = 15; + for (i=0; ihash2bits,bit.inds[i]) == 0 ) + SETBIT(bloom->hash2bits,bit.inds[i]); + else alreadyset++; + } + if ( alreadyset == i ) + printf("iguana_bloomset: collision\n"); + return(bit); +} + +int32_t iguana_bloomfind(struct iguana_info *coin,struct iguana_bloom16 *bloom,int32_t incr,struct iguana_bloominds bit) +{ + int32_t i; + coin->bloomsearches++; + for (i=0; ihash2bits,bit.inds[i]) == 0 ) + return(-1); + coin->bloomhits++; + return(0); +} + +int32_t iguana_bundlescan(struct iguana_info *coin,struct iguana_bundle *bp,bits256 hash2) +{ + int32_t bundlei; + for (bundlei=0; bundlein; bundlei++) + { + if ( memcmp(hash2.bytes,bp->hashes[bundlei].bytes,sizeof(hash2)) == 0 ) + { + //char str[65]; printf("hdrsi.%d scan.%s found %d of %d\n",bp->hdrsi,bits256_str(str,hash2),bundlei,bp->n); + return(bundlei); + } + } + return(-2); +} + +struct iguana_bundle *iguana_bundlefind(struct iguana_info *coin,struct iguana_bundle **bpp,int32_t *bundleip,bits256 hash2) +{ + int32_t i; struct iguana_bloominds bit; struct iguana_bundle *bp = *bpp; + bit = iguana_calcbloom(hash2); + if ( bp == 0 ) + { + for (i=coin->bundlescount-1; i>=0; i--) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + if ( iguana_bloomfind(coin,&bp->bloom,0,bit) == 0 ) + { + *bpp = bp; + if ( (*bundleip= iguana_bundlescan(coin,bp,hash2)) < 0 ) + { + //printf("bloom miss\n"); + coin->bloomfalse++; + } + else return(bp); + } //else printf("no bloom\n"); + } + } + *bundleip = -2; + *bpp = 0; + return(0); + } + else if ( iguana_bloomfind(coin,&bp->bloom,0,bit) == 0 ) + { + if ( (*bundleip= iguana_bundlescan(coin,bp,hash2)) >= 0 ) + { + *bpp = bp; + return(bp); + } else printf("scan miss\n"); + } + *bpp = 0; + *bundleip = -2; + return(0); +} + +bits256 *iguana_bundleihash2p(struct iguana_info *coin,int32_t *isinsidep,struct iguana_bundle *bp,int32_t bundlei) +{ + *isinsidep = 0; + if ( bundlei >= 0 && bundlei < coin->chain->bundlesize ) + { + *isinsidep = 1; + return(&bp->hashes[bundlei]); + } + else if ( bundlei == -1 ) + return(&bp->prevbundlehash2); + else if ( bundlei == coin->chain->bundlesize) + return(&bp->nextbundlehash2); + else return(0); +} + +int32_t iguana_hash2set(struct iguana_info *coin,char *debugstr,struct iguana_bundle *bp,int32_t bundlei,bits256 newhash2) +{ + int32_t isinside,checki,retval = -1; bits256 *orighash2p = 0; struct iguana_bundle *checkbp; char str[65]; struct iguana_bloominds bit; + if ( bp == 0 ) + return(-1); + if ( bp->n <= bundlei ) + { + printf("hash2set.%s [%d] of %d <- %s\n",debugstr,bundlei,bp->n,bits256_str(str,newhash2)); + bp->n = coin->chain->bundlesize; + } + if ( bits256_nonz(newhash2) == 0 || (orighash2p= iguana_bundleihash2p(coin,&isinside,bp,bundlei)) == 0 ) + { + printf("iguana_hash2set warning: bundlei.%d newhash2.%s orighash2p.%p\n",bundlei,bits256_str(str,newhash2),orighash2p); + *orighash2p = newhash2; + //getchar(); + return(-1); + } + if ( bits256_nonz(*orighash2p) > 0 && memcmp(newhash2.bytes,orighash2p,sizeof(bits256)) != 0 ) + { + char str2[65],str3[65]; + bits256_str(str2,*orighash2p), bits256_str(str3,newhash2); + printf("ERRRO iguana_hash2set overwrite [%s] %s with %s [%d:%d]\n",debugstr,str2,str3,bp->hdrsi,bundlei); + *orighash2p = newhash2; + //getchar(); + return(-1); + } + if ( isinside != 0 ) + { + bit = iguana_calcbloom(newhash2); + if ( iguana_bloomfind(coin,&bp->bloom,0,bit) < 0 ) + { + //printf("bloomset (%s)\n",bits256_str(str,newhash2)); + iguana_bloomset(coin,&bp->bloom,0,bit); + if ( 0 ) + { + int32_t i; + if ( iguana_bloomfind(coin,&bp->bloom,0,bit) < 0 ) + { + for (i=0; i<8; i++) + printf("%d ",bit.inds[i]); + printf("cant bloomfind just bloomset\n"); + } + else + { + *orighash2p = newhash2; + checkbp = bp, checki = -2; + if ( iguana_bundlefind(coin,&checkbp,&checki,newhash2) == 0 || checki != bundlei ) + { + printf("cant iguana_bundlefind just added.(%s) bundlei.%d %p vs checki.%d %p\n",bits256_str(str,newhash2),bundlei,bp,checki,checkbp); + } + else if ( (coin->bloomsearches % 100000) == 0 ) + printf("BLOOM SUCCESS %.2f%% FP.%d/%d collisions.%d\n",100.*(double)coin->bloomhits/coin->bloomsearches,(int32_t)coin->bloomfalse,(int32_t)coin->bloomsearches,(int32_t)coin->collisions); + } + } + } //else printf("bloom found\n"); + retval = 0; + } else retval = (bundlei >= 0 && bundlei < coin->chain->bundlesize) ? 0 : 1; + //printf("set [%d] <- %s\n",bundlei,bits256_str(str,newhash2)); + *orighash2p = newhash2; + return(retval); +} + +int32_t iguana_bundlehash2add(struct iguana_info *coin,struct iguana_block **blockp,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2) +{ + struct iguana_block *block =0; struct iguana_bundle *otherbp; // + int32_t otherbundlei,setval,bundlesize,err = 0; + if ( blockp != 0 ) + *blockp = 0; + if ( bp == 0 ) + return(-1111); + if ( bits256_nonz(hash2) > 0 && (block= iguana_blockhashset(coin,-1,hash2,1)) != 0 ) + { + /*if ( (block->hdrsi != bp->hdrsi || block->bundlei != bundlei) && (block->hdrsi != 0 || block->bundlei != 0) ) + { + return(-2); + }*/ + bundlesize = coin->chain->bundlesize; + if ( bp->n > bundlesize ) + { + printf("bp->n.%d is too big\n",bp->n); + return(0); + } + if ( bundlei >= bp->n ) + bp->n = bundlesize;//(bundlei < bundlesize-1) ? bundlesize : (bundlei + 1); + if ( (setval= iguana_hash2set(coin,"blockadd",bp,bundlei,hash2)) == 0 ) + { + if ( (block->hdrsi != bp->hdrsi || block->bundlei != bundlei) && (block->hdrsi != 0 || block->bundlei != 0) ) + { + printf("blockadd warning: %d[%d] <- %d[%d]\n",block->hdrsi,block->bundlei,bp->hdrsi,bundlei); + err |= 2; + exit(-1); + } + else + { +//char str[65]; printf(">>>>>>>>>>>>>> bundlehash2.(%s) ht.(%d %d)\n",bits256_str(str,hash2),bp->bundleheight,bundlei); + block->hdrsi = bp->hdrsi; + block->bundlei = bundlei; + bp->blocks[bundlei] = block; + otherbp = 0; + if ( (otherbp= iguana_bundlefind(coin,&otherbp,&otherbundlei,hash2)) != 0 || (bundlei % (bundlesize-1)) == 0) + { + if ( bundlei == 0 && (otherbundlei == -2 || otherbundlei == bundlesize-1) ) + { + if ( otherbp != 0 && iguana_hash2set(coin,"blockadd0_prev",bp,-1,otherbp->hashes[0]) != 0 ) + err |= 4; + if ( otherbp != 0 && iguana_hash2set(coin,"blockadd0_next",otherbp,bundlesize,bp->hashes[0]) != 0 ) + err |= 8; + } + else if ( bundlei == bundlesize-1 && (otherbundlei == -2 || otherbundlei == 0) ) + { + if ( otherbp != 0 && iguana_hash2set(coin,"blockaddL_prev",otherbp,-1,bp->hashes[0]) != 0 ) + err |= 16; + if ( otherbp != 0 && iguana_hash2set(coin,"blockaddL_next",bp,bundlesize,otherbp->hashes[0]) != 0 ) + err |= 32; + } + //else printf("blockadd warning: %d[%d] bloomfound %d[%d]\n",bp->hdrsi,bundlei,otherbp!=0?otherbp->hdrsi:-1,otherbundlei); + } + } + } + else if ( setval == 1 ) + { + if ( bundlei == -1 && iguana_hash2set(coin,"blockadd_m1",bp,-1,hash2) != 0 ) + err |= 4; + if ( bundlei == bundlesize && iguana_hash2set(coin,"blockaddL_m1",bp,bundlesize,hash2) != 0 ) + err |= 4; + } + else if ( setval < 0 ) + { + printf("neg setval error\n"); + err |= 64; + } + if ( err == 0 && blockp != 0 ) + *blockp = block; + } else err |= 128; + if ( err != 0 ) + { + printf("bundlehash2add err.%d\n",err); + while ( 1 ) + sleep(1); + exit(-1); + } + return(-err); +} + +struct iguana_bundle *iguana_bundlecreate(struct iguana_info *coin,int32_t *bundleip,int32_t bundleheight,bits256 bundlehash2,bits256 allhash,int32_t issueflag) +{ + char str[65],str2[65]; struct iguana_bundle *bp = 0; + if ( bits256_nonz(bundlehash2) > 0 ) + { + bits256_str(str,bundlehash2); + if ( iguana_bundlefind(coin,&bp,bundleip,bundlehash2) != 0 ) + { + if ( bits256_nonz(bp->allhash) == 0 ) + bp->allhash = allhash; + if ( bp->bundleheight >= 0 && bp->bundleheight != (bundleheight - *bundleip) ) + printf("bundlecreate warning: bp->bundleheight %d != %d (bundleheight %d - %d bundlei)\n",bp->bundleheight,(bundleheight - *bundleip),bundleheight,*bundleip); + if ( *bundleip == 0 && iguana_bundlehash2add(coin,0,bp,0,bundlehash2) == 0 ) + { + //if ( bp->numhashes < bp->n ) + // queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1); + } + return(bp); + } + bp = mycalloc('b',1,sizeof(*bp)); + bp->n = coin->chain->bundlesize; + bp->hdrsi = coin->bundlescount; + bp->bundleheight = bundleheight; + bp->allhash = allhash; + iguana_hash2set(coin,"create",bp,0,bundlehash2); + if ( iguana_bundlehash2add(coin,0,bp,0,bundlehash2) == 0 ) + { + bp->coin = coin; + bp->avetime = coin->avetime * 2.; + coin->bundles[coin->bundlescount] = bp; + if ( coin->bundlescount > 0 ) + coin->bundles[coin->bundlescount-1]->nextbp = bp; + *bundleip = 0; + printf("ht.%d alloc.[%d] new hdrs.%s %s\n",bp->bundleheight,coin->bundlescount,str,bits256_str(str2,allhash)); + iguana_bundlehash2add(coin,0,bp,0,bundlehash2); + if ( issueflag != 0 ) + { + iguana_blockQ(coin,bp,0,bundlehash2,1); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1); + } + coin->bundlescount++; + } + else + { + printf("error adding bundlehash2 bundleheight.%d\n",bundleheight); + myfree(bp,sizeof(*bp)); + bp = 0; + } + return(bp); + } else printf("cant create bundle with zerohash\n"); + //else printf("iguana_hdrscreate cant find hdr with %s or %s\n",bits256_str(bundlehash2),bits256_str2(firstblockhash2)); + return(0); +} + +struct iguana_txid *iguana_bundletx(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,struct iguana_txid *tx,int32_t txidind) +{ + static bits256 zero; + int32_t hdrsi; int64_t Toffset; char fname[1024]; FILE *fp; struct iguana_ramchaindata rdata; + iguana_peerfname(coin,&hdrsi,"DB",fname,0,bp->hashes[0],zero,bp->n); + if ( (fp= fopen(fname,"rb")) != 0 ) + { + fseek(fp,(long)&rdata.Toffset - (long)&rdata,SEEK_SET); + if ( fread(&Toffset,1,sizeof(Toffset),fp) == sizeof(Toffset) ) + { + fseek(fp,Toffset + sizeof(struct iguana_txid) * txidind,SEEK_SET); + if ( fread(tx,1,sizeof(*tx),fp) == sizeof(*tx) ) + { + fclose(fp); + return(tx); + } else printf("bundletx read error\n"); + } else printf("bundletx Toffset read error\n"); + fclose(fp); + } else printf("bundletx couldnt open.(%s)\n",fname); + return(0); +} + +char *iguana_bundledisp(struct iguana_info *coin,struct iguana_bundle *prevbp,struct iguana_bundle *bp,struct iguana_bundle *nextbp,int32_t m) +{ + static char line[1024]; + line[0] = 0; + if ( bp == 0 ) + return(line); + if ( prevbp != 0 ) + { + if ( memcmp(prevbp->hashes[0].bytes,bp->prevbundlehash2.bytes,sizeof(bits256)) == 0 ) + { + if ( memcmp(prevbp->nextbundlehash2.bytes,bp->hashes[0].bytes,sizeof(bits256)) == 0 ) + sprintf(line+strlen(line),"<->"); + else sprintf(line+strlen(line),"<-"); + } + else if ( memcmp(prevbp->nextbundlehash2.bytes,bp->hashes[0].bytes,sizeof(bits256)) == 0 ) + sprintf(line+strlen(line),"->"); + } + sprintf(line+strlen(line),"(%d:%d).%d ",bp->hdrsi,m,bp->numhashes); + if ( nextbp != 0 ) + { + if ( memcmp(nextbp->hashes[0].bytes,bp->nextbundlehash2.bytes,sizeof(bits256)) == 0 ) + { + if ( memcmp(nextbp->prevbundlehash2.bytes,bp->hashes[0].bytes,sizeof(bits256)) == 0 ) + sprintf(line+strlen(line),"<->"); + else sprintf(line+strlen(line),"->"); + } + else if ( memcmp(nextbp->prevbundlehash2.bytes,bp->hashes[0].bytes,sizeof(bits256)) == 0 ) + sprintf(line+strlen(line),"<-"); + } + return(line); +} + +void iguana_bundleiclear(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei) +{ + //bp->ipbits[bundlei] = 0; + //bp->issued[bundlei] = 0; + //bp->requests[bundlei] = 0; + //CLEARBIT(bp->recv,bundlei); +} + +void iguana_bundlestats(struct iguana_info *coin,char *str) +{ + static uint32_t lastdisp; static bits256 zero; + int32_t i,n,issued,dispflag,bundlei,lefti,minrequests,missing,numbundles,numdone,numrecv,totalsaved,numhashes,numcached,numsaved,numemit,numactive,firstbundle,totalrecv = 0; struct iguana_peer *addr1; + bits256 hash2; struct iguana_bundle *bp; struct iguana_block *block; int64_t datasize,estsize = 0; + //iguana_chainextend(coin,iguana_blockfind(coin,coin->blocks.hwmchain)); + //if ( queue_size(&coin->blocksQ) == 0 ) + // iguana_blockQ(coin,0,-1,coin->blocks.hwmchain.hash2,0); + if ( 0 && queue_size(&coin->blocksQ) == 0 && queue_size(&coin->priorityQ) == 0 ) + { + for (i=0; ipeers.active[i].pending = 0; + } + dispflag = (rand() % 10) == 0; + numbundles = numdone = numrecv = numhashes = numcached = totalsaved = numemit = numactive = 0; + firstbundle = -1; + issued = 0; + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + minrequests = 777; + bp->numhashes = 0; + numbundles++; + numrecv = datasize = numsaved = 0; + missing = -1; + lefti = -1; + if ( bp->numrecv >= bp->n ) + numdone++; + else + { + for (bundlei=0; bundlein; bundlei++) + { + if ( bits256_nonz(bp->hashes[bundlei]) == 0 ) + { + lefti = bundlei; + if ( missing < 0 ) + missing = bundlei; + continue; + } + if ( (block= bp->blocks[bundlei]) != 0 || (block= iguana_blockfind(coin,bp->hashes[bundlei])) != 0 ) + { + bp->blocks[bundlei] = block; + if ( block->numrequests < minrequests ) + minrequests = block->numrequests; + if ( block->fpipbits != 0 ) + numsaved++; + if ( block->RO.recvlen != 0 ) + { + datasize += block->RO.recvlen; + if ( block->queued != 0 ) + numcached++; + numrecv++; + } + if ( block->queued == 0 && block->fpipbits == 0 ) + lefti = bundlei; + } + if ( firstbundle < 0 || firstbundle == bp->hdrsi ) + firstbundle = bp->hdrsi; + if ( 0 && issued < 3 && bp->emitfinish < coin->startutc && block != 0 && block->fpipbits == 0 && (addr1= coin->peers.ranked[1]) != 0 ) + { + issued++; + iguana_sendblockreq(coin,addr1,bp,bundlei,bp->hashes[bundlei],1); + //iguana_blockQ(coin,bp,lefti,bp->hashes[lefti],1); + addr1->lastlefti = (bp->bundleheight + bundlei); + printf("addr1->lastlefti.%d\n",addr1->lastlefti); + if ( addr1->lastlefti >= bp->bundleheight+bundlei ) + addr1->lastlefti = bp->bundleheight; + } + bp->numhashes++; + } + } + //printf("%d ",numsaved); + /*if ( bp->emitfinish < coin->startutc && (bp->bundleheight+lefti <= coin->blocks.hwmchain.height || bp->numhashes == bp->n-1) && lefti >= 0 && lefti < bp->n ) + { + printf("ISSUE LAST ONE %d in %d vs %d\n",lefti,bp->bundleheight,coin->blocks.hwmchain.height); + iguana_blockQ(coin,bp,lefti,bp->hashes[lefti],1); + }*/ + if ( (bp->minrequests= minrequests) == 100 ) + { + for (i=0; in; i++) + if ( (block= bp->blocks[i]) != 0 ) + block->numrequests = 1; + } + //printf("(%d %d) ",bp->hdrsi,minrequests); + numhashes += bp->numhashes; + bp->numrecv = numrecv; + bp->datasize = datasize; + if ( bp->emitfinish != 0 ) + { + numemit++; + if ( bp->emitfinish > coin->startutc && bp->purgetime == 0 && time(NULL) > bp->emitfinish+30 ) + { + char fname[1024]; int32_t hdrsi,m,j; uint32_t ipbits; + for (j=m=0; jpeers.active)/sizeof(*coin->peers.active); j++) + { + if ( (ipbits= coin->peers.active[j].ipbits) != 0 ) + { + if ( iguana_peerfname(coin,&hdrsi,"tmp",fname,ipbits,bp->hashes[0],zero,1) >= 0 ) + { + if ( OS_removefile(fname,0) > 0 ) + coin->peers.numfiles--, m++; + } + else printf("error removing.(%s)\n",fname); + } + } + //printf("purged hdrsi.%d m.%d\n",bp->hdrsi,m); + bp->purgetime = (uint32_t)time(NULL); + } + } + else if ( numsaved > 0 ) + { + bp->estsize = ((uint64_t)datasize * bp->n) / (numrecv+1); + estsize += bp->estsize; + if ( bp->numhashes == bp->n ) + numactive++; + if ( 0 && dispflag != 0 ) + { + if ( bp->numrecv < bp->n-1 ) + printf("(%d %d) ",i,bp->numrecv); + else printf("(%d -[%d]) ",i,lefti); + } + if ( (rand() % 100) == 0 && bp->numrecv > bp->n-2 && lefti >= 0 && lefti < bp->n ) + { + //printf("remainder issue %d:%d %s\n",bp->hdrsi,lefti,bits256_str(str,bp->hashes[lefti])); + //iguana_blockQ(coin,bp,lefti,bp->hashes[lefti],1); + } + if ( numsaved >= bp->n && bp->emitfinish == 0 ) + { + //printf(">>>>>>>>>>>>>>>>>>>>>>> EMIT\n"); + bp->emitfinish = 1; + iguana_emitQ(coin,bp); + } + /*if ( numrecv > bp->n*.98 ) + { + if ( numrecv > bp->n-3 ) + bp->threshold = bp->avetime; + else bp->threshold = bp->avetime * 2; + } else*/ + bp->threshold = bp->avetime; + bp->metric = (bp->n - numsaved) / (bp->hdrsi + 1);//sqrt(abs((bp->n - bp->numrecv)) * sqrt(bp->estsize - bp->datasize)) / coin->chain->bundlesize; + } else bp->threshold = 10000., bp->metric = 0.; + totalrecv += numrecv; + totalsaved += numsaved; + } + } + coin->blocksrecv = totalrecv; + char str2[65]; uint64_t tmp; int32_t diff,p = 0; struct tai difft,t = tai_now(); + for (i=0; ipeers.active[i].usock >= 0 ) + p++; + diff = (int32_t)time(NULL) - coin->startutc; + difft.x = (t.x - coin->starttime.x), difft.millis = (t.millis - coin->starttime.millis); + tmp = (difft.millis * 1000000); + tmp %= 1000000000; + difft.millis = ((double)tmp / 1000000.); + sprintf(str,"N[%d] d.%d p.%d g.%d A.%d h.%d r.%d c.%d:%d s.%d E.%d:%d M.%d L.%d est.%d %s %d:%02d:%02d %03.3f peers.%d/%d",coin->bundlescount,numdone,coin->numpendings,numbundles,numactive,numhashes,coin->blocksrecv,coin->numcached,coin->cachefreed,totalsaved,coin->numemitted,coin->numreqsent,coin->blocks.hwmchain.height,coin->longestchain,coin->MAXBUNDLES,mbstr(str2,estsize),(int32_t)difft.x/3600,(int32_t)(difft.x/60)%60,(int32_t)difft.x%60,difft.millis,p,coin->MAXPEERS); + //sprintf(str+strlen(str),"%s.%-2d %s time %.2f files.%d Q.%d %d\n",coin->symbol,flag,str,(double)(time(NULL)-coin->starttime)/60.,coin->peers.numfiles,queue_size(&coin->priorityQ),queue_size(&coin->blocksQ)); + //if ( time(NULL) > lastdisp+10 && dispflag != 0 ) + { + printf("%s\n",str); + lastdisp = (uint32_t)time(NULL); + } + strcpy(coin->statusstr,str); + coin->activebundles = numactive; + coin->estsize = estsize; + coin->numrecv = totalrecv; + if ( 0 && queue_size(&coin->priorityQ) == 0 && coin->blocksrecv > coin->longestchain*.9 && coin->blocksrecv < coin->longestchain-1 ) + { + n = 0; + for (i=coin->lastsweep; ilongestchain-1; i++) + { + hash2 = iguana_blockhash(coin,i); + if ( bits256_nonz(hash2) > 0 && (block= iguana_blockfind(coin,hash2)) != 0 ) + { + if ( iguana_bundlefind(coin,&bp,&bundlei,hash2) == 0 || block->fpipbits ) + { + iguana_blockQ(coin,bp,bundlei,hash2,1); + n++; + printf("%d ",i); + if ( n > 1000 ) + break; + else if ( n < 10 && bp != 0 ) + iguana_bundleiclear(coin,bp,bundlei); + } + coin->lastsweep = i; + } + if ( i >= coin->longestchain-1 ) + coin->lastsweep = 0; + } + if ( n > 0 ) + printf(">>>>>>>>>>> issued.%d 90%% blocks\n",n); + } + else if ( 0 && strcmp(coin->symbol,"BTCD") == 0 && queue_size(&coin->blocksQ) == 0 ) + { + for (i=n=0; ilongestchain-1; i++) + { + hash2 = iguana_blockhash(coin,i); + if ( bits256_nonz(hash2) > 0 && (block= iguana_blockfind(coin,hash2)) != 0 && block->fpipbits == 0 ) + iguana_blockQ(coin,coin->bundles[i/coin->chain->bundlesize],i%coin->chain->bundlesize,hash2,0); + } + } +} diff --git a/iguana/iguana_chains.c b/iguana/iguana_chains.c new file mode 100755 index 000000000..3ef54fd33 --- /dev/null +++ b/iguana/iguana_chains.c @@ -0,0 +1,187 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" +#define PUBKEY_ADDRESS_BTC 0 +#define SCRIPT_ADDRESS_BTC 5 +#define PRIVKEY_ADDRESS_BTC 128 +#define PUBKEY_ADDRESS_BTCD 60 +#define SCRIPT_ADDRESS_BTCD 85 +#define PRIVKEY_ADDRESS_BTCD 188 +#define PUBKEY_ADDRESS_TEST 111 +#define SCRIPT_ADDRESS_TEST 196 +#define PRIVKEY_ADDRESS_TEST 239 + +static struct iguana_chain Chains[] = +{ + [CHAIN_TESTNET3] = + { + //CHAIN_TESTNET3, + "testnet3", "tBTC", + PUBKEY_ADDRESS_TEST, SCRIPT_ADDRESS_TEST, PRIVKEY_ADDRESS_TEST, + "\x0b\x11\x09\x07", + "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943", + "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff001d1aa4ae180101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000", + 18333,18334,0, + }, + [CHAIN_BITCOIN] = + { + //CHAIN_BITCOIN, + "bitcoin", "BTC", + 0, 5, 0x80, + "\xf9\xbe\xb4\xd9", + "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", + "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000", + 8333,8334,0, + { { 210000, (50 * SATOSHIDEN) }, { 420000, (50 * SATOSHIDEN) / 2 }, { 630000, (50 * SATOSHIDEN) / 4 },{ 840000, (50 * SATOSHIDEN) / 8 }, + } + }, + [CHAIN_BTCD] = + { + //CHAIN_BTCD, + "btcd", "BTCD", + PUBKEY_ADDRESS_BTCD, SCRIPT_ADDRESS_BTCD, PRIVKEY_ADDRESS_BTCD, + "\xe4\xc2\xd8\xe6", + "0000044966f40703b516c5af180582d53f783bfd319bb045e2dc3e05ea695d46", + "0100000000000000000000000000000000000000000000000000000000000000000000002b5b9d8cdd624d25ce670a7aa34726858388da010d4ca9ec8fd86369cc5117fd0132a253ffff0f1ec58c7f0001010000000132a253010000000000000000000000000000000000000000000000000000000000000000ffffffff4100012a3d3138204a756e652032303134202d204269746f696e20796f75722077617920746f206120646f75626c6520657370726573736f202d20636e6e2e636f6dffffffff010000000000000000000000000000", + 14631,14632,1, + }, +}; + +void iguana_chaininit(struct iguana_chain *chain,int32_t hasheaders) +{ + chain->hasheaders = hasheaders; + if ( strcmp(chain->symbol,"bitcoin") == 0 ) + { + chain->unitval = 0x1d; + } + else + { + if ( chain->unitval == 0 ) + chain->unitval = 0x1e; + } + if ( hasheaders != 0 ) + { + strcpy(chain->gethdrsmsg,"getheaders"); + chain->bundlesize = _IGUANA_HDRSCOUNT; + } + else + { + strcpy(chain->gethdrsmsg,"getblocks"); + chain->bundlesize = _IGUANA_BLOCKHASHES; + } + decode_hex((uint8_t *)chain->genesis_hashdata,32,(char *)chain->genesis_hash); + if ( chain->ramchainport == 0 ) + chain->ramchainport = chain->portp2p - 1; + if ( chain->portrpc == 0 ) + chain->portrpc = chain->portp2p + 1; +} + +struct iguana_chain *iguana_chainfind(char *name) +{ + struct iguana_chain *chain; uint32_t i; + for (i=0; igenesis_hash,chain->name,name,strcmp(name,chain->name)); + if ( chain->name[0] == 0 || chain->genesis_hash == 0 ) + continue; + if ( strcmp(name,chain->symbol) == 0 ) + { + iguana_chaininit(chain,strcmp(chain->symbol,"BTC") == 0); + return(chain); + } + } + return NULL; +} + +struct iguana_chain *iguana_findmagic(uint8_t netmagic[4]) +{ + struct iguana_chain *chain; uint8_t i; + for (i=0; iname[0] == 0 || chain->genesis_hash == 0 ) + continue; + if ( memcmp(netmagic,chain->netmagic,4) == 0 ) + return(iguana_chainfind((char *)chain->symbol)); + } + return NULL; +} + +uint64_t iguana_miningreward(struct iguana_info *coin,uint32_t blocknum) +{ + int32_t i; uint64_t reward = 50LL * SATOSHIDEN; + for (i=0; ichain->rewards)/sizeof(*coin->chain->rewards); i++) + { + //printf("%d: %u %.8f\n",i,(int32_t)coin->chain->rewards[i][0],dstr(coin->chain->rewards[i][1])); + if ( blocknum >= coin->chain->rewards[i][0] ) + reward = coin->chain->rewards[i][1]; + else break; + } + return(reward); +} + +struct iguana_chain *iguana_createchain(cJSON *json) +{ + char *symbol,*name,*hexstr; cJSON *rewards,*rpair,*item; int32_t i,m,n; struct iguana_chain *chain = 0; + if ( (symbol= jstr(json,"name")) != 0 && strlen(symbol) < 8 ) + { + chain = mycalloc('C',1,sizeof(*chain)); + strcpy(chain->symbol,symbol); + if ( (name= jstr(json,"description")) != 0 && strlen(name) < 32 ) + strcpy(chain->name,name); + if ( (hexstr= jstr(json,"pubval")) != 0 && strlen(hexstr) == 2 ) + decode_hex((uint8_t *)&chain->pubval,1,hexstr); + if ( (hexstr= jstr(json,"scriptval")) != 0 && strlen(hexstr) == 2 ) + decode_hex((uint8_t *)&chain->p2shval,1,hexstr); + if ( (hexstr= jstr(json,"wipval")) != 0 && strlen(hexstr) == 2 ) + decode_hex((uint8_t *)&chain->wipval,1,hexstr); + if ( (hexstr= jstr(json,"netmagic")) != 0 && strlen(hexstr) == 8 ) + decode_hex((uint8_t *)chain->netmagic,1,hexstr); + if ( (hexstr= jstr(json,"unitval")) != 0 && strlen(hexstr) == 2 ) + decode_hex((uint8_t *)&chain->unitval,1,hexstr); + if ( (hexstr= jstr(json,"genesishash")) != 0 ) + { + chain->genesis_hash = mycalloc('G',1,strlen(hexstr)+1); + strcpy(chain->genesis_hash,hexstr); + } + if ( (hexstr= jstr(json,"genesisblock")) != 0 ) + { + chain->genesis_hex = mycalloc('G',1,strlen(hexstr)+1); + strcpy(chain->genesis_hex,hexstr); + } + chain->portp2p = juint(json,"p2p"); + if ( (chain->ramchainport= juint(json,"ramchain")) == 0 ) + chain->ramchainport = chain->portp2p - 1; + if ( (chain->portrpc= juint(json,"rpc")) == 0 ) + chain->portrpc = chain->portp2p + 1; + chain->hastimestamp = juint(json,"hastimestamp"); + if ( (rewards= jarray(&n,json,"rewards")) != 0 ) + { + for (i=0; irewards[i][0] = j64bits(jitem(rpair,0),0); + chain->rewards[i][1] = j64bits(jitem(rpair,1),0); + } + } + } + iguana_chaininit(chain,juint(json,"hasheaders")); + } + return(chain); +} diff --git a/iguana/iguana_html.c b/iguana/iguana_html.c new file mode 100755 index 000000000..4d6709347 --- /dev/null +++ b/iguana/iguana_html.c @@ -0,0 +1,981 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" +#include "../includes/cJSON.h" + +char Default_agent[64] = { "ALL" }; +#define IGUANA_FORMS "[ \ +\ +{\"disp\":\"simple explorer\",\"agent\":\"ramchain\",\"method\":\"explore\",\"fields\":[{\"skip\":1,\"field\":\"search\",\"cols\":65,\"rows\":1}]}, \ +{\"disp\":\"block height\",\"agent\":\"ramchain\",\"method\":\"block\",\"fields\":[{\"field\":\"height\",\"cols\":10,\"rows\":1}]}, \ +{\"disp\":\"block hash\",\"agent\":\"ramchain\",\"method\":\"block\",\"fields\":[{\"field\":\"hash\",\"cols\":65,\"rows\":1}]}, \ +{\"disp\":\"txid\",\"agent\":\"ramchain\",\"method\":\"txid\",\"fields\":[{\"skip\":1,\"field\":\"hash\",\"cols\":65,\"rows\":1}]}, \ +{\"disp\":\"status\",\"agent\":\"ramchain\",\"method\":\"status\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"bundleinfo\",\"agent\":\"ramchain\",\"method\":\"bundleinfo\",\"fields\":[{\"skip\":1,\"field\":\"height\",\"cols\":12,\"rows\":1}]}, \ +\ +{\"disp\":\"addcoin\",\"agent\":\"iguana\",\"method\":\"addcoin\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1}]}, \ +{\"disp\":\"pausecoin\",\"agent\":\"iguana\",\"method\":\"pausecoin\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"startcoin\",\"agent\":\"iguana\",\"method\":\"startcoin\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"addnode\",\"agent\":\"iguana\",\"method\":\"addnode\",\"fields\":[{\"skip\":1,\"field\":\"ipaddr\",\"cols\":32,\"rows\":1}]}, \ +{\"disp\":\"maxpeers\",\"agent\":\"iguana\",\"method\":\"maxpeers\",\"fields\":[{\"skip\":1,\"field\":\"max\",\"cols\":8,\"rows\":1}]}, \ +{\"disp\":\"peers\",\"agent\":\"iguana\",\"method\":\"peers\",\"fields\":[{\"field\":\"coin\",\"cols\":16,\"rows\":1}]}, \ +{\"disp\":\"nodestatus\",\"agent\":\"iguana\",\"method\":\"nodestatus\",\"fields\":[{\"skip\":1,\"field\":\"ipaddr\",\"cols\":32,\"rows\":1}]}, \ +\ +{\"disp\":\"rates\",\"agent\":\"PAX\",\"method\":\"rates\",\"fields\":[{\"skip\":1,\"field\":\"peg\",\"cols\":16,\"rows\":1}]},\ +{\"disp\":\"prices\",\"agent\":\"PAX\",\"method\":\"prices\",\"fields\":[{\"skip\":1,\"field\":\"peg\",\"cols\":16,\"rows\":1}]},\ +{\"agent\":\"PAX\",\"method\":\"lock\",\"fields\":[{\"skip\":1,\"field\":\"peg\",\"cols\":16,\"rows\":1},{\"skip\":1,\"field\":\"lockdays\",\"cols\":6,\"rows\":1},{\"skip\":1,\"field\":\"units\",\"cols\":12,\"rows\":1}]}, \ +{\"agent\":\"PAX\",\"method\":\"redeem\",\"fields\":[{\"skip\":1,\"field\":\"txid\",\"cols\":65,\"rows\":1},{\"skip\":1,\"field\":\"dest\",\"cols\":65,\"rows\":1}]},\ +{\"disp\":\"balance\",\"agent\":\"PAX\",\"method\":\"balance\",\"fields\":[{\"skip\":1,\"field\":\"address\",\"cols\":44,\"rows\":1}]},\ +{\"agent\":\"PAX\",\"method\":\"rollover\",\"fields\":[{\"skip\":1,\"field\":\"txid\",\"cols\":16,\"rows\":1},{\"skip\":1,\"field\":\"newpeg\",\"cols\":16,\"rows\":1},{\"skip\":1,\"field\":\"newlockdays\",\"cols\":6,\"rows\":1}]},\ +{\"agent\":\"PAX\",\"method\":\"swap\",\"fields\":[{\"skip\":1,\"field\":\"txid\",\"cols\":16,\"rows\":1},{\"skip\":1,\"field\":\"othertxid\",\"cols\":16,\"rows\":1}]},\ +{\"agent\":\"PAX\",\"method\":\"bet\",\"fields\":[{\"skip\":1,\"field\":\"peg\",\"cols\":16,\"rows\":1},{\"skip\":1,\"field\":\"price\",\"cols\":16,\"rows\":1},{\"skip\":1,\"field\":\"amount\",\"cols\":16,\"rows\":1}]},\ +\ +{\"agent\":\"InstantDEX\",\"method\":\"placebid\",\"fields\":[{\"skip\":1,\"field\":\"base\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"rel\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"exchange\",\"cols\":16,\"rows\":1},{\"field\":\"price\",\"cols\":16,\"rows\":1},{\"field\":\"volume\",\"cols\":16,\"rows\":1}]}, \ +{\"agent\":\"InstantDEX\",\"method\":\"placeask\",\"fields\":[{\"skip\":1,\"field\":\"base\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"rel\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"exchange\",\"cols\":16,\"rows\":1},{\"field\":\"price\",\"cols\":16,\"rows\":1},{\"field\":\"volume\",\"cols\":16,\"rows\":1}]}, \ +{\"agent\":\"InstantDEX\",\"method\":\"orderbook\",\"fields\":[{\"skip\":1,\"field\":\"base\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"rel\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"exchange\",\"cols\":16,\"rows\":1}]}, \ +{\"disp\":\"orderstatus\",\"agent\":\"InstantDEX\",\"method\":\"orderstatus\",\"fields\":[{\"skip\":1,\"field\":\"orderid\",\"cols\":32,\"rows\":1}]}, \ +{\"disp\":\"cancelorder\",\"agent\":\"InstantDEX\",\"method\":\"cancelorder\",\"fields\":[{\"skip\":1,\"field\":\"orderid\",\"cols\":32,\"rows\":1}]}, \ +{\"disp\":\"balance\",\"agent\":\"InstantDEX\",\"method\":\"balance\",\"fields\":[{\"skip\":1,\"field\":\"exchange\",\"cols\":16,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"allorderbooks\",\"agent\":\"InstantDEX\",\"method\":\"allorderbooks\",\"fields\":[{\"skip\":1,\"field\":\"allorderbooks\",\"cols\":1,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"openorders\",\"agent\":\"InstantDEX\",\"method\":\"openorders\",\"fields\":[{\"skip\":1,\"field\":\"openorders\",\"cols\":1,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"tradehistory\",\"agent\":\"InstantDEX\",\"method\":\"tradehistory\",\"fields\":[{\"skip\":1,\"field\":\"tradehistory\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"allexchanges\",\"agent\":\"InstantDEX\",\"method\":\"allexchanges\",\"fields\":[{\"skip\":1,\"field\":\"allexchanges\",\"cols\":1,\"rows\":1}]}, \ +\ +{\"agent\":\"pangea\",\"method\":\"bet\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":24,\"rows\":1},{\"skip\":1,\"field\":\"amount\",\"cols\":24,\"rows\":1}]}, \ +{\"disp\":\"call\",\"agent\":\"pangea\",\"method\":\"call\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"fold\",\"agent\":\"pangea\",\"method\":\"fold\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"status\",\"agent\":\"pangea\",\"method\":\"status\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"hand history\",\"agent\":\"pangea\",\"method\":\"handhistory\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"history\",\"agent\":\"pangea\",\"method\":\"history\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"follow\",\"agent\":\"pangea\",\"method\":\"follow\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":24,\"rows\":1}]}, \ +{\"disp\":\"lobby\",\"agent\":\"pangea\",\"method\":\"lobby\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1}]}, \ +{\"disp\":\"join\",\"agent\":\"pangea\",\"method\":\"join\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":24,\"rows\":1}]}, \ +{\"agent\":\"pangea\",\"method\":\"buyin\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"tableid\",\"cols\":24,\"rows\":1},{\"skip\":1,\"field\":\"amount\",\"cols\":12,\"rows\":1}]}, \ +{\"agent\":\"pangea\",\"method\":\"newtournament\",\"fields\":[{\"field\":\"mintables\",\"cols\":8,\"rows\":1},{\"field\":\"maxtables\",\"cols\":4,\"rows\":1},{\"field\":\"starttime\",\"cols\":16,\"rows\":1},{\"field\":\"prizefund\",\"cols\":12,\"rows\":1},{\"field\":\"coin\",\"cols\":12,\"rows\":1}]}, \ +{\"agent\":\"pangea\",\"method\":\"newtable\",\"fields\":[{\"field\":\"minplayers\",\"cols\":4,\"rows\":1},{\"field\":\"maxplayers\",\"cols\":4,\"rows\":1},{\"field\":\"rake\",\"cols\":4,\"rows\":1},{\"field\":\"bigblind\",\"cols\":12,\"rows\":1},{\"field\":\"ante\",\"cols\":12,\"rows\":1},{\"field\":\"minbuyin\",\"cols\":12,\"rows\":1},{\"field\":\"maxbuyin\",\"cols\":12,\"rows\":1}]}, \ +{\"disp\":\"leave\",\"agent\":\"pangea\",\"method\":\"leave\",\"fields\":[{\"skip\":1,\"field\":\"tableid\",\"cols\":8,\"rows\":1}]}, \ +\ +{\"agent\":\"jumblr\",\"method\":\"send\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"amount\",\"cols\":13,\"rows\":1},{\"skip\":1,\"field\":\"address\",\"cols\":8,\"rows\":1}]}, \ +{\"agent\":\"jumblr\",\"method\":\"invoice\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"amount\",\"cols\":13,\"rows\":1},{\"skip\":1,\"field\":\"address\",\"cols\":8,\"rows\":1}]}, \ +{\"agent\":\"jumblr\",\"method\":\"shuffle\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"amount\",\"cols\":13,\"rows\":1}]}, \ +{\"agent\":\"jumblr\",\"method\":\"balance\",\"fields\":[{\"skip\":1,\"field\":\"coin\",\"cols\":8,\"rows\":1},{\"skip\":1,\"field\":\"address\",\"cols\":13,\"rows\":1}]}, \ +\ +{\"newline\":0,\"disp\":\"InstantDEX\",\"agent\":\"iguana\",\"method\":\"setagent\",\"fields\":[{\"field\":\"InstantDEX\",\"cols\":1,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"PAX\",\"agent\":\"iguana\",\"method\":\"setagent\",\"fields\":[{\"field\":\"PAX\",\"cols\":1,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"pangea\",\"agent\":\"iguana\",\"method\":\"setagent\",\"fields\":[{\"field\":\"pangea\",\"cols\":1,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"jumblr\",\"agent\":\"iguana\",\"method\":\"setagent\",\"fields\":[{\"field\":\"jumblr\",\"cols\":1,\"rows\":1}]}, \ +{\"disp\":\"ramchain\",\"agent\":\"iguana\",\"method\":\"setagent\",\"fields\":[{\"field\":\"ramchain\",\"cols\":1,\"rows\":1}]}, \ +{\"newline\":0,\"disp\":\"iguana\",\"agent\":\"iguana\",\"method\":\"setagent\",\"fields\":[{\"field\":\"iguana\",\"cols\":1,\"rows\":1}]}, \ +\ +{\"agent\":\"hash\",\"method\":\"NXT\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":100,\"rows\":1}]}, \ + {\"agent\":\"hash\",\"method\":\"curve25519\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"rmd160_sha256\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ +{\"agent\":\"hash\",\"method\":\"sha256_sha256\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ +{\"agent\":\"hash\",\"method\":\"base64_encode\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ +{\"agent\":\"hash\",\"method\":\"base64_decode\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ +{\"agent\":\"hash\",\"method\":\"crc32\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"sha512\",\"fields\":[{\"skip\":1,\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"sha384\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"sha256\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"sha224\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"rmd320\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"rmd256\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"rmd160\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"rmd128\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"sha1\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"md2\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"md4\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"md5\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"tiger\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"whirlpool\",\"fields\":[{\"skip\":1,\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_sha512\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_sha384\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_sha256\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_sha224\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_rmd320\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_rmd256\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_rmd160\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_rmd128\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_sha1\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_md2\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_md4\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_md5\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_tiger\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}, \ + {\"agent\":\"hash\",\"method\":\"hmac_whirlpool\",\"fields\":[{\"skip\":1,\"field\":\"password\",\"cols\":32,\"rows\":1},{\"field\":\"message\",\"cols\":64,\"rows\":3}]}\ +]" + +char *HTMLheader = +" \ + \ + \ + \ + \ +iguana \ + \ + \ +\ + \ +"; + +// Link + +char *HTMLfooter = +" \ +\ + \ + \ +\ + \ + \ + \ + \ + \ + \ + \ +\ + \ +"; + +#define HTML_EMIT(str) if ( (str) != 0 && (str)[0] != 0 ) strcpy(&retbuf[size],str), size += (int32_t)strlen(str) +char Prevjsonstr[1024],Currentjsonstr[1024]; + +char *iguana_rpc(char *agent,cJSON *json,char *data,int32_t datalen) +{ + //printf("agent.(%s) json.(%s) data[%d] %s\n",agent,jprint(json,0),datalen,data!=0?data:""); + if ( data == 0 ) + return(iguana_JSON(jprint(json,0))); + else return(iguana_JSON(data)); +} + +void iguana_urldecode(char *str) +{ + int32_t a,b,c; char *dest = str; + while ( (c= *str) != 0 ) + { + if ( c == '%' && (a= str[1]) != 0 && (b= str[2]) != 0 ) + *dest++ = (unhex(a)<<4) | unhex(b); + else *dest++ = c; + } + *dest = 0; +} + +char *iguana_parsebidask(char *base,char *rel,char *exchange,double *pricep,double *volumep,char *line) +{ + int32_t i; + for (i=0; i<16&&line[i]!='/'&&line[i]!=0; i++) + base[i] = line[i]; + base[i] = 0; + touppercase(base); + line += (i + 1); + for (i=0; i<16&&line[i]!='/'&&line[i]!=0; i++) + rel[i] = line[i]; + rel[i] = 0; + touppercase(rel); + line += (i + 1); + for (i=0; i<16&&line[i]!='/'&&line[i]!=0; i++) + exchange[i] = line[i]; + exchange[i] = 0; + line += (i + 1); + if ( strncmp(line,"price/",strlen("price/")) == 0 ) + { + line += strlen("price/"); + *pricep = atof(line); + if ( (line= strstr(line,"volume/")) != 0 ) + { + line += strlen("volume/"); + *volumep = atof(line); + for (i=0; i<16&&line[i]!=0; i++) + if ( line[i] == '/' ) + { + i++; + break; + } + return(line+i); + } + } + return(0); +} + +char *iguana_InstantDEX(char *jsonstr,char *path,char *method) +{ + char *str,base[64],rel[64],exchange[64]; double price,volume; + if ( (str= iguana_parsebidask(base,rel,exchange,&price,&volume,path)) != 0 ) + { + if ( price > 0. && volume > 0. ) + { + sprintf(jsonstr,"{\"agent\":\"InstantDEX\",\"method\":\"%s\",\"base\":\"%s\",\"rel\":\"%s\",\"exchange\":\"%s\",\"price\":\%0.8f,\"volume\":%0.8f}",method,base,rel,exchange,price,volume); + return(jsonstr); + } + else return(0); + } + return(0); +} + +void iguana_coinset(char *buf,char *path) +{ + int32_t i; + if ( path[0] == '/' ) + path++; + for (i=0; i<8&&path[i]!=0&&path[i]!=' '&&path[i]!='/'; i++) + buf[i] = path[i]; + buf[i] = 0; + touppercase(buf); +} + +char *iguana_ramchain_glue(struct iguana_info *coin,char *method,char *jsonstr) +{ + char *ramchain_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json); + cJSON *json; char *retstr; + json = cJSON_Parse(jsonstr); + retstr = ramchain_parser(0,coin,method,json); + free_json(json); + return(retstr); +} + +char *iguana_hashparse(char *path) +{ + int32_t i,j,len,iter,n; uint8_t databuf[512]; + char hexstr[1025],password[512],hashname[512],*name,*msg; cJSON *json; + typedef void (*hashfunc)(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); + typedef char *(*hmacfunc)(char *dest,char *key,int32_t key_size,char *message); + struct hashfunc_entry { char *name; hashfunc hashfunc; }; + struct hmacfunc_entry { char *name; hmacfunc hmacfunc; }; + struct hashfunc_entry hashes[] = { {"NXT",calc_NXTaddr}, {"curve25519",calc_curve25519_str }, {"base64_encode",calc_base64_encodestr}, {"base64_decode",calc_base64_decodestr}, {"crc32",calc_crc32str}, {"rmd160_sha256",rmd160ofsha256}, {"sha256_sha256",sha256_sha256}, {"sha256",vcalc_sha256}, {"sha512",calc_sha512}, {"sha384",calc_sha384}, {"sha224",calc_sha224}, {"rmd160",calc_rmd160}, {"rmd256",calc_rmd256}, {"rmd320",calc_rmd320}, {"rmd128",calc_rmd128}, {"sha1",calc_sha1}, {"md5",calc_md5str}, {"tiger",calc_tiger}, {"whirlpool",calc_whirlpool} }; + struct hmacfunc_entry hmacs[] = { {"hmac_sha256",hmac_sha256_str}, {"hmac_sha512",hmac_sha512_str}, {"hmac_sha384",hmac_sha384_str}, {"hmac_sha224",hmac_sha224_str}, {"hmac_rmd160",hmac_rmd160_str}, {"hmac_rmd256",hmac_rmd256_str}, {"hmac_rmd320",hmac_rmd320_str}, {"hmac_rmd128",hmac_rmd128_str}, {"hmac_sha1",hmac_sha1_str}, {"hmac_md5",hmac_md5_str}, {"hmac_tiger",hmac_tiger_str}, {"hmac_whirlpool",hmac_whirlpool_str} }; + n = (int32_t)sizeof(hashes)/sizeof(*hashes); + for (j=0; j 0 ) + { + jsonstr[i] = 0; + agent = jsonstr; + jsonstr += i; + } + jsonstr++; + json = cJSON_CreateObject(); + jaddstr(json,"agent",agent); + while ( 1 ) + { + n = (int32_t)strlen(jsonstr); + key = jsonstr; + value = 0; + for (i=0; i

"); + sprintf(buf,"
"); + //sprintf(buf,"
COIN: "); + //HTML_EMIT(buf); + if ( 0 ) + { + sprintf(formfooter,"\t","InstantDEX","iguana49_setagent"); HTML_EMIT(formfooter); + sprintf(formfooter,"\t","PAX","iguana50_setagent"); HTML_EMIT(formfooter); + sprintf(formfooter,"\t","pangea","iguana51_setagent"); HTML_EMIT(formfooter); + sprintf(formfooter,"\t","jumblr","iguana52_setagent"); HTML_EMIT(formfooter); + sprintf(formfooter,"\t","ramchain","iguana53_setagent"); HTML_EMIT(formfooter); + sprintf(formfooter,"\t","iguana","iguana54_setagent"); HTML_EMIT(formfooter); + } + HTML_EMIT(" Agent: "); HTML_EMIT(Default_agent); + + HTML_EMIT("

"); + HTML_EMIT(origjsonstr); HTML_EMIT(" -> "); + HTML_EMIT(""); + formheader[0] = formfooter[0] = 0; + if ( (array= jarray(&n,json,"forms")) != 0 ) + { + for (i=0; i function click_%s()\n{\n",clickname); + HTML_EMIT(buf); + sprintf(postjson,"%s/%s",agent,method); + //printf("form.%s button.%s [%s]\n",formname,button,postjson); + if ( (array2= jarray(&m,item,"fields")) != 0 ) + { + //sprintf(buf,"COIN = document.COIN_NAME.value;\n"); + //sprintf(postjson+strlen(postjson),"/%s/' + %s + '","coin","COIN"); + for (j=0; j (%s)\n",j,jprint(obj,0)); + sprintf(fieldindex,"%c",'A'+j); + if ( (fieldname= jstr(obj,"field")) != 0 ) + { + sprintf(buf,"%s = document.%s.%s.value;\n",fieldindex,clickname,fieldname); + HTML_EMIT(buf); + //sprintf(postjson+strlen(postjson),",\"%s\":\"' + %s + '\"",fieldname,fieldindex); + if ( juint(obj,"skip") == 0 ) + sprintf(postjson+strlen(postjson),"/%s/' + %s + '",fieldname,fieldindex); + else sprintf(postjson+strlen(postjson),"/' + %s + '",fieldindex); + } + } + //strcat(postjson,"}"); + sprintf(&retbuf[size],"location.href = '%s/%s';\n}\r\n",url,postjson), size += strlen(&retbuf[size]); + sprintf(formheader,"
",clickname,url); + HTML_EMIT(formheader); + disp = jstr(item,"disp"); + for (j=0; j",fieldname); + else sprintf(buf,"",cols,rows,fieldname,cols == 1 ? "hidden" : ""); + str = disp==0?jstr(obj,"disp"):disp; + sprintf(&retbuf[size],"\r\n",str!=0?str:fieldname,buf), size += strlen(&retbuf[size]); + } + sprintf(formfooter,"\n
%s %s
",button,clickname); + HTML_EMIT(formfooter); + } + } + } + HTML_EMIT("

"); HTML_EMIT(""); HTML_EMIT("

"); + return((int32_t)strlen(retbuf)); +} +#undef HTML_EMIT + +char *iguana_htmlresponse(char *retbuf,int32_t bufsize,int32_t *remainsp,int32_t localaccess,char *retstr,int32_t freeflag) +{ + static char *html = " %s "; + char *result=0,*error=0; int32_t n; cJSON *json,*formsjson; + retbuf[0] = 0; + /*if ( localaccess == 0 ) + sprintf(retbuf+strlen(retbuf),"Access-Control-Allow-Origin: *\r\n"); + else sprintf(retbuf+strlen(retbuf),"Access-Control-Allow-Origin: null\r\n"); + sprintf(retbuf+strlen(retbuf),"Access-Control-Allow-Credentials: true\r\n"); + sprintf(retbuf+strlen(retbuf),"Access-Control-Allow-Headers: Authorization, Content-Type\r\n"); + sprintf(retbuf+strlen(retbuf),"Access-Control-Allow-Methods: GET, POST\r\n"); + sprintf(retbuf+strlen(retbuf),"Cache-Control: no-cache, no-store, must-revalidate\r\n"); + sprintf(retbuf+strlen(retbuf),"Content-type: text/html\r\n"); + sprintf(retbuf+strlen(retbuf),"Content-Length: %8d\r\n\r\n",n);*/ + sprintf(retbuf+strlen(retbuf),"\n\r"); + n = (int32_t)strlen(retbuf); + formsjson = cJSON_Parse(IGUANA_FORMS); + if ( (json= cJSON_Parse(retstr)) == 0 ) + json = cJSON_CreateObject(); + jadd(json,"forms",formsjson); + error = jstr(json,"error"); + result = jstr(json,"result"); + //printf("process.(%s)\n",jprint(formsjson,0)); + n = iguana_htmlgen(&retbuf[n],bufsize-n,result,error,json,"iguana",Currentjsonstr); + free_json(json); + if ( n == 0 ) + { + n = (int32_t)(strlen(html) + strlen(retstr) + 1); + sprintf(retbuf+strlen(retbuf),html,retstr); + } + if ( freeflag != 0 ) + free(retstr); + if ( n > bufsize ) + { + printf("htmlresponse overflowed buffer[%d] with %d\n",bufsize,n); + exit(-1); + } + *remainsp = n; + return(retbuf); +} + +void iguana_rpcloop(void *args) +{ + int32_t recvlen,bindsock,postflag,sock,remains,numsent,len; socklen_t clilen; + char ipaddr[64],jsonbuf[8192],*buf,*retstr,*space;//,*retbuf; ,n,i,m + struct sockaddr_in cli_addr; uint32_t ipbits,i,size = IGUANA_WIDTH*IGUANA_HEIGHT*16 + 512; uint16_t port; + port = IGUANA_RPCPORT;//coin->chain->portrpc; + bindsock = iguana_socket(1,"127.0.0.1",port); + printf("iguana_rpcloop 127.0.0.1:%d bind sock.%d\n",port,bindsock); + space = calloc(1,size); + while ( bindsock >= 0 ) + { + clilen = sizeof(cli_addr); + //printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",port,bindsock); + sock = accept(bindsock,(struct sockaddr *)&cli_addr,&clilen); + if ( sock < 0 ) + { + printf("ERROR on accept usock.%d\n",sock); + continue; + } + memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); + expand_ipbits(ipaddr,ipbits); + //printf("RPC.%d for %x (%s)\n",sock,ipbits,ipaddr); + //printf("%p got.(%s) from %s | usock.%d ready.%u dead.%u\n",addr,H.command,addr->ipaddr,addr->usock,addr->ready,addr->dead); + memset(jsonbuf,0,sizeof(jsonbuf)); + remains = (int32_t)(sizeof(jsonbuf) - 1); + buf = jsonbuf; + recvlen = 0; + retstr = 0; + while ( remains > 0 ) + { + if ( (len= (int32_t)recv(sock,buf,remains,0)) < 0 ) + { + if ( errno == EAGAIN ) + { + printf("EAGAIN for len %d, remains.%d\n",len,remains); + usleep(10000); + } + break; + } + else + { + if ( len > 0 ) + { + remains -= len; + recvlen += len; + buf = &buf[len]; + } else usleep(10000); + //printf("got.(%s) %d remains.%d of total.%d\n",jsonbuf,recvlen,remains,len); + retstr = iguana_rpcparse(space,size,&postflag,jsonbuf); + break; + } + } + if ( retstr != 0 ) + { + i = 0; + if ( postflag == 0 ) + retstr = iguana_htmlresponse(space,size,&remains,1,retstr,1); + else remains = (int32_t)strlen(retstr); + //printf("RETBUF.(%s)\n",retstr); + while ( remains > 0 ) + { + if ( (numsent= (int32_t)send(sock,&retstr[i],remains,MSG_NOSIGNAL)) < 0 ) + { + if ( errno != EAGAIN && errno != EWOULDBLOCK ) + { + //printf("%s: %s numsent.%d vs remains.%d len.%d errno.%d (%s) usock.%d\n",retstr,ipaddr,numsent,remains,recvlen,errno,strerror(errno),sock); + break; + } + } + else if ( remains > 0 ) + { + remains -= numsent; + i += numsent; + if ( remains > 0 ) + printf("iguana sent.%d remains.%d of len.%d\n",numsent,remains,recvlen); + } + } + if ( retstr != space) + free(retstr); + } + if ( Currentjsonstr[0] != 0 ) + strcpy(Prevjsonstr,Currentjsonstr); + Currentjsonstr[0] = 0; + //printf("done response sock.%d\n",sock); + close(sock); + } +} \ No newline at end of file diff --git a/iguana/iguana_init.c b/iguana/iguana_init.c new file mode 100755 index 000000000..a4c916a2e --- /dev/null +++ b/iguana/iguana_init.c @@ -0,0 +1,360 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +void iguana_initQ(queue_t *Q,char *name) +{ + char *tst,*str = "need to init each Q when single threaded"; + queue_enqueue(name,Q,queueitem(str),1); + if ( (tst= queue_dequeue(Q,1)) != 0 ) + free_queueitem(tst); +} + +void iguana_initQs(struct iguana_info *coin) +{ + int32_t i; + iguana_initQ(&coin->bundlesQ,"bundlesQ"); + iguana_initQ(&coin->hdrsQ,"hdrsQ"); + iguana_initQ(&coin->blocksQ,"blocksQ"); + iguana_initQ(&coin->priorityQ,"priorityQ"); + iguana_initQ(&coin->possibleQ,"possibleQ"); + iguana_initQ(&coin->jsonQ,"jsonQ"); + iguana_initQ(&coin->finishedQ,"finishedQ"); + iguana_initQ(&coin->cacheQ,"cacheQ"); + iguana_initQ(&coin->TerminateQ,"TerminateQ"); + for (i=0; ipeers.active[i].sendQ,"addrsendQ"); +} + +void iguana_initpeer(struct iguana_info *coin,struct iguana_peer *addr,uint32_t ipbits) +{ + memset(addr,0,sizeof(*addr)); + addr->ipbits = ipbits; + addr->usock = -1; + expand_ipbits(addr->ipaddr,addr->ipbits); + //addr->pending = (uint32_t)time(NULL); + strcpy(addr->symbol,coin->symbol); + strcpy(addr->coinstr,coin->name); + iguana_initQ(&addr->sendQ,"addrsendQ"); +} + +void iguana_initcoin(struct iguana_info *coin) +{ + int32_t i; + portable_mutex_init(&coin->peers_mutex); + portable_mutex_init(&coin->blocks_mutex); + iguana_meminit(&coin->blockMEM,"blockMEM",coin->blockspace,sizeof(coin->blockspace),0); + iguana_initQs(coin); + OS_randombytes((unsigned char *)&coin->instance_nonce,sizeof(coin->instance_nonce)); + coin->startutc = (uint32_t)time(NULL); + while ( time(NULL) == coin->startutc ) + usleep(1); + coin->startmillis = OS_milliseconds(), coin->starttime = tai_now(coin->startmillis); + coin->avetime = 1 * 100; + //coin->R.maxrecvbundles = IGUANA_INITIALBUNDLES; + for (i=0; ipeers.active[i].usock = -1; + + // validate blocks!! + //for (i=0; ilatest.lhashes[i].bytes,&coin->latest.states[i],0,0); +} + +bits256 iguana_genesis(struct iguana_info *coin,struct iguana_chain *chain) +{ + struct iguana_block block,*ptr; struct iguana_msgblock msg; bits256 hash2; char str[65]; uint8_t buf[1024]; int32_t height; + decode_hex(buf,(int32_t)strlen(chain->genesis_hex)/2,(char *)chain->genesis_hex); + hash2 = bits256_doublesha256(0,buf,sizeof(struct iguana_msgblockhdr)); + iguana_rwblock(0,&hash2,buf,&msg); + if ( memcmp(hash2.bytes,chain->genesis_hashdata,sizeof(hash2)) != 0 ) + { + bits256_str(str,hash2); + printf("genesis mismatch? calculated %s vs %s\n",str,(char *)chain->genesis_hex); + memset(hash2.bytes,0,sizeof(hash2)); + return(hash2); + } + bits256_str(str,hash2); + printf("genesis.(%s) len.%d hash.%s\n",chain->genesis_hex,(int32_t)sizeof(msg.H),str); + iguana_blockconv(&block,&msg,hash2,0); + //coin->latest.dep.numtxids = + block.RO.numvouts = 1; + iguana_gotdata(coin,0,0); + if ( (ptr= iguana_blockhashset(coin,0,hash2,1)) != 0 ) + { + ptr->mainchain = 1; + iguana_blockcopy(coin,ptr,&block); + coin->blocks.RO[0] = block.RO; + if ( (height= iguana_chainextend(coin,ptr)) == 0 ) + { + block = *ptr; + coin->blocks.recvblocks = coin->blocks.issuedblocks = 1; + } + else printf("genesis block doesnt validate for %s ht.%d\n",coin->symbol,height); + } else printf("couldnt hashset genesis\n"); + if ( coin->blocks.hwmchain.height != 0 || fabs(coin->blocks.hwmchain.PoW - block.PoW) > SMALLVAL || memcmp(coin->blocks.hwmchain.RO.hash2.bytes,hash2.bytes,sizeof(hash2)) != 0 ) + { + printf("%s genesis values mismatch hwmheight.%d %.15f %.15f %s\n",coin->name,coin->blocks.hwmchain.height,coin->blocks.hwmchain.PoW,block.PoW,bits256_str(str,coin->blocks.hwmchain.RO.hash2)); + getchar(); + } + int32_t bundlei = -2; + static bits256 zero; + iguana_bundlecreate(coin,&bundlei,0,hash2,zero,1); + return(hash2); +} + +int32_t iguana_savehdrs(struct iguana_info *coin) +{ + int32_t height,iter,i,n,retval = 0; char fname[512],shastr[65],tmpfname[512],str[65],oldfname[512]; + bits256 hash2,sha256all,*hashes; FILE *fp; struct sha256_vstate shastate; + n = coin->blocks.hwmchain.height + 1; + hashes = mycalloc('h',coin->chain->bundlesize,sizeof(*hashes)); + if ( 0 ) + { + printf("start savehdrs calc\n"); + vupdate_sha256(sha256all.bytes,&shastate,0,0); + for (i=0; isymbol), OS_compatible_path(oldfname); + sprintf(tmpfname,"tmp/%s/hdrs.txt",coin->symbol), OS_compatible_path(tmpfname); + sprintf(fname,"%s_hdrs.txt",coin->symbol), OS_compatible_path(fname); + if ( (fp= fopen(tmpfname,"w")) != 0 ) + { + if ( 0 ) + { + fprintf(fp,"%d %s\n",n,bits256_str(str,sha256all)); + for (i=0; ichain->bundlesize) + { + for (i=0; ichain->bundlesize; i++) + { + hashes[i] = iguana_blockhash(coin,height+i); + if ( bits256_str(str,hashes[i]) == 0 ) + break; + } + if ( i == coin->chain->bundlesize ) + { + struct iguana_bundle *bp; + vcalc_sha256(shastr,sha256all.bytes,hashes[0].bytes,sizeof(*hashes) * coin->chain->bundlesize); + if ( (bp= coin->bundles[height/coin->chain->bundlesize]) != 0 ) + bp->allhash = sha256all; + } + else shastr[0] = 0; + for (iter=0; iter<1; iter++) + { + hash2 = iguana_blockhash(coin,height+iter); + if ( bits256_nonz(hash2) > 0 ) + { + char str[65]; + bits256_str(str,hash2); + if ( iter == 0 ) + fprintf(fp,"%d %s %s\n",height+iter,str,shastr); + else fprintf(fp,"%d %s\n",height+iter,str); + retval = height+iter; + } + if ( coin->chain->hasheaders != 0 ) + break; + } + } + } + //printf("new hdrs.txt %ld vs (%s) %ld\n",ftell(fp),fname,(long)iguana_filesize(fname)); + if ( ftell(fp) > OS_filesize(fname) ) + { + printf("new hdrs.txt %ld vs (%s) %ld\n",ftell(fp),fname,(long)OS_filesize(fname)); + fclose(fp); + OS_renamefile(fname,oldfname); + OS_copyfile(tmpfname,fname,1); + } else fclose(fp); + } + myfree(hashes,coin->chain->bundlesize * sizeof(*hashes)); + return(retval); +} + +void iguana_parseline(struct iguana_info *coin,int32_t iter,FILE *fp) +{ + int32_t j,k,m,c,height,flag,bundlei,bundleheight = -1; char checkstr[1024],line[1024]; + struct iguana_peer *addr; struct iguana_bundle *bp; bits256 allhash,hash2,zero; + struct iguana_block *block; + if ( iter == 1 ) + { + int32_t i; FILE *fp; char fname[512]; struct iguana_blockRO blockRO; + sprintf(fname,"blocks.%s",coin->symbol), OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb")) != 0 ) + { + for (i=0; i<100000000; i++) + { + if ( fread(&blockRO,1,sizeof(blockRO),fp) != sizeof(blockRO) ) + break; + if ( i > (coin->blocks.maxbits - 1000) ) + iguana_recvalloc(coin,i + 100000); + coin->blocks.RO[i] = blockRO; + char str[65]; + if ( bits256_nonz(blockRO.hash2) > 0 ) + printf("init.%d %s\n",i,bits256_str(str,blockRO.hash2)); + } + fclose(fp); + printf("validate.%d blocks that were read in\n",i); + } + } + m = flag = 0; + memset(&zero,0,sizeof(zero)); + allhash = zero; + while ( fgets(line,sizeof(line),fp) > 0 ) + { + j = (int32_t)strlen(line) - 1; + line[j] = 0; + //printf("parse line.(%s) maxpeers.%d\n",line,coin->MAXPEERS); + if ( iter == 0 ) + { + if ( m < coin->MAXPEERS )//&& m < 77.7 ) + { + if ( m == 0 ) + { + addr = &coin->peers.active[m++]; + iguana_initpeer(coin,addr,(uint32_t)calc_ipbits("127.0.0.1")); + //printf("call initpeer.(%s)\n",addr->ipaddr); + iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD); + } +#ifndef IGUANA_DISABLEPEERS + addr = &coin->peers.active[m++]; + iguana_initpeer(coin,addr,(uint32_t)calc_ipbits(line)); + //printf("call initpeer.(%s)\n",addr->ipaddr); + iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD); +#endif + } + } + else + { + for (k=height=0; k= '0' && c <= '9' ) + height = (height * 10) + (line[k] - '0'); + else break; + } + //printf("parseline: k.%d %d height.%d m.%d bundlesize.%d (%s)\n",k,line[k],height,m,coin->chain->bundlesize,&line[k+1+65]);// + strlen(line+k+1)]); + if ( line[k] == ' ' ) + { + decode_hex(hash2.bytes,sizeof(hash2),line+k+1); + if ( line[k+1 + 65] != 0 ) + { + if ( height > (coin->blocks.maxbits - 1000) ) + iguana_recvalloc(coin,height + 100000); + decode_hex(allhash.bytes,sizeof(allhash),line+k+1 + 64 + 1); + init_hexbytes_noT(checkstr,allhash.bytes,sizeof(allhash)); + if ( strcmp(checkstr,line+k+1 + 64 + 1) == 0 ) + { + init_hexbytes_noT(checkstr,hash2.bytes,sizeof(hash2)); + //char str[65],str2[65]; printf(">>>> bundle.%d got (%s)/(%s) allhash.(%s)\n",height,bits256_str(str,hash2),checkstr,bits256_str(str2,allhash)); + if ( (bp= iguana_bundlecreate(coin,&bundlei,height,hash2,allhash,0)) != 0 ) + { + bp->bundleheight = height; + if ( (block= iguana_blockfind(coin,hash2)) != 0 ) + block->mainchain = 1, block->height = height; + if ( iguana_bundleload(coin,bp) != 0 ) + { + bp->emitfinish = (uint32_t)time(NULL) + 1; + //printf("LOADED bundle.%d\n",bp->bundleheight); + } + else + { + char str[65]; + init_hexbytes_noT(str,hash2.bytes,sizeof(hash2)); + bp->emitfinish = 0; + iguana_blockQ(coin,bp,0,hash2,1); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1); + } + } + } + } + init_hexbytes_noT(checkstr,hash2.bytes,sizeof(hash2)); + if ( strncmp(checkstr,line+k+1,64) == 0 ) + { + if ( (height % coin->chain->bundlesize) == 1 ) + { + if ( (bp= coin->bundles[height/coin->chain->bundlesize]) != 0 ) + { + if ( iguana_bundlehash2add(coin,0,bp,1,hash2) == 0 ) + { + //printf("add bundle.%d:%d (%s)\n",bundleheight,bp->hdrsi,bits256_str(str,hash2)); + if ( (block= iguana_blockfind(coin,hash2)) != 0 ) + block->mainchain = 1, block->height = bundleheight+1; + } + } + } + } + } + } + } +} + +struct iguana_info *iguana_startcoin(struct iguana_info *coin,int32_t initialheight,int32_t mapflags) +{ + FILE *fp; char fname[512],*symbol; int32_t iter; + coin->sleeptime = 10000; + symbol = coin->symbol; + if ( initialheight < coin->chain->bundlesize*10 ) + initialheight = coin->chain->bundlesize*10; + iguana_recvalloc(coin,initialheight); + coin->longestchain = 1; + coin->blocks.hwmchain.height = 0; + if ( (coin->myservices & NODE_NETWORK) != 0 ) + { + printf("MYSERVICES.%llx\n",(long long)coin->myservices); + coin->peers.acceptloop = iguana_launch(coin,"acceptloop",iguana_acceptloop,coin,IGUANA_PERMTHREAD); + } + coin->firstblock = coin->blocks.parsedblocks + 1; + iguana_genesis(coin,coin->chain); + for (iter=0; iter<2; iter++) + { + sprintf(fname,"%s_%s.txt",coin->symbol,(iter == 0) ? "peers" : "hdrs"); + OS_compatible_path(fname); + printf("parsefile.%d %s\n",iter,fname); + if ( (fp= fopen(fname,"r")) != 0 ) + { + iguana_parseline(coin,iter,fp); + fclose(fp); + } + printf("done parsefile.%d\n",iter); + } +#ifndef IGUANA_DEDICATED_THREADS + coin->peers.peersloop = iguana_launch("peersloop",iguana_peersloop,coin,IGUANA_PERMTHREAD); +#endif + if ( 0 && (coin->MAXBUNDLES= coin->bundlescount / 4) < _IGUANA_MAXBUNDLES ) + coin->MAXBUNDLES = _IGUANA_MAXBUNDLES; + //coin->peers.recvloop = iguana_launch("recvloop",iguana_recvloop,coin,IGUANA_PERMTHREAD); + printf("started.%s\n",coin->symbol); + return(coin); +} diff --git a/iguana/iguana_json.c b/iguana/iguana_json.c new file mode 100755 index 000000000..d712fdf24 --- /dev/null +++ b/iguana/iguana_json.c @@ -0,0 +1,580 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +struct iguana_agent *Agents[16]; + +char *pangea_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json); +char *InstantDEX_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json); +char *jumblr_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json); +char *ramchain_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json); + +int32_t iguana_launchcoin(char *symbol,cJSON *json); +struct iguana_jsonitem { struct queueitem DL; uint32_t expired,allocsize; char **retjsonstrp; char jsonstr[]; }; + +cJSON *iguana_peerjson(struct iguana_info *coin,struct iguana_peer *addr) +{ + cJSON *array,*json = cJSON_CreateObject(); + jaddstr(json,"ipaddr",addr->ipaddr); + jaddnum(json,"protover",addr->protover); + jaddnum(json,"relay",addr->relayflag); + jaddnum(json,"height",addr->height); + jaddnum(json,"rank",addr->rank); + jaddnum(json,"usock",addr->usock); + if ( addr->dead != 0 ) + jaddnum(json,"dead",addr->dead); + jaddnum(json,"ready",addr->ready); + jaddnum(json,"recvblocks",addr->recvblocks); + jaddnum(json,"recvtotal",addr->recvtotal); + jaddnum(json,"lastcontact",addr->lastcontact); + if ( addr->numpings > 0 ) + jaddnum(json,"aveping",addr->pingsum/addr->numpings); + array = cJSON_CreateObject(); + jaddnum(array,"version",addr->msgcounts.version); + jaddnum(array,"verack",addr->msgcounts.verack); + jaddnum(array,"getaddr",addr->msgcounts.getaddr); + jaddnum(array,"addr",addr->msgcounts.addr); + jaddnum(array,"inv",addr->msgcounts.inv); + jaddnum(array,"getdata",addr->msgcounts.getdata); + jaddnum(array,"notfound",addr->msgcounts.notfound); + jaddnum(array,"getblocks",addr->msgcounts.getblocks); + jaddnum(array,"getheaders",addr->msgcounts.getheaders); + jaddnum(array,"headers",addr->msgcounts.headers); + jaddnum(array,"tx",addr->msgcounts.tx); + jaddnum(array,"block",addr->msgcounts.block); + jaddnum(array,"mempool",addr->msgcounts.mempool); + jaddnum(array,"ping",addr->msgcounts.ping); + jaddnum(array,"pong",addr->msgcounts.pong); + jaddnum(array,"reject",addr->msgcounts.reject); + jaddnum(array,"filterload",addr->msgcounts.filterload); + jaddnum(array,"filteradd",addr->msgcounts.filteradd); + jaddnum(array,"filterclear",addr->msgcounts.filterclear); + jaddnum(array,"merkleblock",addr->msgcounts.merkleblock); + jaddnum(array,"alert",addr->msgcounts.alert); + jadd(json,"msgcounts",array); + return(json); +} + +cJSON *iguana_peersjson(struct iguana_info *coin) +{ + cJSON *retjson,*array; int32_t i; struct iguana_peer *addr; + retjson = cJSON_CreateObject(); + array = cJSON_CreateArray(); + for (i=0; iMAXPEERS; i++) + { + addr = &coin->peers.active[i]; + if ( addr->usock >= 0 && addr->ipbits != 0 && addr->ipaddr[0] != 0 ) + jaddi(array,iguana_peerjson(coin,addr)); + } + jadd(retjson,"peers",array); + jaddnum(retjson,"maxpeers",coin->MAXPEERS); + jaddstr(retjson,"coin",coin->symbol); + return(retjson); +} + +cJSON *iguana_agentinfojson(struct iguana_agent *agent) +{ + cJSON *json= cJSON_CreateObject(); + jaddstr(json,"name",agent->name); + jadd(json,"methods",agent->methods); + if ( agent->port != 0 ) + jaddnum(json,"port",agent->port); + else jaddstr(json,"type","builtin"); + return(json); +} + +char *iguana_remoteparser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json) +{ + int32_t i,n,remains,numsent; char *jsonstr = 0,*retstr = 0; uint8_t hdr[128]; + if ( agent->sock < 0 ) + agent->sock = iguana_socket(0,agent->hostname,agent->port); + if ( agent->sock >= 0 ) + { + i = 0; + jsonstr = jprint(json,0); + n = (int32_t)strlen(jsonstr) + 1; + remains = n; + //printf("RETBUF.(%s)\n",retbuf); + while ( remains > 0 ) + { + if ( (numsent= (int32_t)send(agent->sock,&jsonstr[i],remains,MSG_NOSIGNAL)) < 0 ) + { + if ( errno != EAGAIN && errno != EWOULDBLOCK ) + { + printf("%s: %s numsent.%d vs remains.%d of %d errno.%d (%s) usock.%d\n",jsonstr,agent->name,numsent,remains,n,errno,strerror(errno),agent->sock); + break; + } + } + else if ( remains > 0 ) + { + remains -= numsent; + i += numsent; + if ( remains > 0 ) + printf("iguana sent.%d remains.%d of len.%d\n",numsent,remains,n); + } + } + if ( (n= (int32_t)recv(agent->sock,hdr,sizeof(hdr),0)) >= 0 ) + { + remains = (hdr[0] + ((int32_t)hdr[1] << 8) + ((int32_t)hdr[2] << 16)); + retstr = mycalloc('p',1,remains + 1); + i = 0; + while ( remains > 0 ) + { + if ( (n= (int32_t)recv(agent->sock,&retstr[i],remains,0)) < 0 ) + { + if ( errno == EAGAIN ) + { + printf("EAGAIN for len %d, remains.%d\n",n,remains); + usleep(10000); + } + break; + } + else + { + if ( n > 0 ) + { + remains -= n; + i += n; + } else usleep(10000); + } + } + } + free(jsonstr); + } + if ( retstr == 0 ) + retstr = clonestr("{\"error\":\"null return\"}"); + return(retstr); +} + +char *iguana_addagent(char *name,char *(*parsefunc)(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json),char *hostname,cJSON *methods,uint16_t port,char *pubkeystr,char *privkeystr) +{ + int32_t i; struct iguana_agent *agent; char retbuf[8192]; + for (i=0; iname,name) == 0 ) + { + if ( pubkeystr != 0 && privkeystr != 0 && strlen(pubkeystr) == 64 && strlen(privkeystr) == 64 ) + { + decode_hex(agent->pubkey.bytes,sizeof(bits256),pubkeystr); + decode_hex(agent->privkey.bytes,sizeof(bits256),privkeystr); + } + if ( port != 0 && agent->port == 0 ) + { + if ( agent->sock >= 0 ) + close(agent->sock); + agent->port = port; + strcpy(agent->hostname,hostname); + agent->sock = iguana_socket(0,agent->hostname,port); + printf("set (%s) port.%d for %s -> sock.%d\n",hostname,port,agent->name,agent->sock); + } + if ( agent->port > 0 && agent->sock < 0 && agent->hostname[0] != 0 && (agent->sock= iguana_socket(0,agent->hostname,agent->port)) < 0 ) + return(clonestr("{\"result\":\"existing agent couldnt connect to remote agent\"}")); + else return(clonestr("{\"result\":\"agent already there\"}")); + } + } + for (i=0; iname,name,sizeof(agent->name)-1); + strncpy(agent->hostname,hostname,sizeof(agent->hostname)-1); + agent->methods = methods, agent->nummethods = cJSON_GetArraySize(methods); + agent->sock = -1; + agent->port = port; + agent->parsefunc = (void *)parsefunc; + if ( pubkeystr != 0 && privkeystr != 0 && strlen(pubkeystr) == 64 && strlen(privkeystr) == 64 ) + { + decode_hex(agent->pubkey.bytes,sizeof(bits256),pubkeystr); + decode_hex(agent->privkey.bytes,sizeof(bits256),privkeystr); + } + if ( port > 0 ) + { + if ( (agent->sock= iguana_socket(0,hostname,port)) < 0 ) + return(clonestr("{\"result\":\"agent added, but couldnt connect to remote agent\"}")); + } + sprintf(retbuf,"{\"result\":\"agent added\",\"name\"\"%s\",\"methods\":%s,\"hostname\":\"%s\",\"port\":%u,\"sock\":%d}",agent->name,jprint(agent->methods,0),agent->hostname,agent->port,agent->sock); + return(clonestr(retbuf)); + } + } + return(clonestr("{\"error\":\"no more agent slots available\"}")); +} + +char *iguana_agentjson(char *name,struct iguana_info *coin,char *method,cJSON *json) +{ + cJSON *retjson,*array,*methods,*obj; int32_t i,n,j; struct iguana_agent *agent; + if ( strcmp(name,"SuperNET") != 0 ) + { + for (i=0; iname,name) == 0 ) + { + if ( agent->parsefunc != 0 ) + { + for (j=0; jnummethods; j++) + { + if ( (obj= jitem(agent->methods,j)) != 0 ) + { + if ( strcmp(method,jstr(obj,0)) == 0 ) + return((*agent->parsefunc)(agent,coin,method,json)); + } + } + return(clonestr("{\"result\":\"agent doesnt have method\"}")); + } else return(clonestr("{\"result\":\"agent doesnt have parsefunc\"}")); + } + } + } + else + { + if ( strcmp(method,"list") == 0 ) + { + retjson = cJSON_CreateObject(); + array = cJSON_CreateArray(); + for (i=0; isymbol[0] != 0 ) + jaddistr(array,Coins[i]->symbol); + } + jadd(retjson,"coins",array); + array = cJSON_CreateArray(); + for (i=0; iname[0] != 0 ) + jaddi(array,iguana_agentinfojson(Agents[i])); + } + jadd(retjson,"agents",array); + return(jprint(retjson,1)); + } + else if ( strcmp(method,"peers") == 0 ) + { + retjson = cJSON_CreateObject(); + array = cJSON_CreateArray(); + for (i=0; isymbol[0] != 0 ) + jaddi(array,iguana_peersjson(Coins[i])); + } + jadd(retjson,"allpeers",array); + return(jprint(retjson,1)); + } + else if ( strcmp(method,"addagent") == 0 ) + { + char *hostname = "127.0.0.1",*name; uint16_t port; + if ( (name= jstr(json,"name")) != 0 && (methods= jarray(&n,json,"methods")) != 0 ) + { + if ( (port= juint(json,"port")) != 0 ) + { + if ( (hostname= jstr(json,"host")) == 0 ) + { + if ( (hostname= jstr(json,"ipaddr")) == 0 ) + hostname = "127.0.0.1"; + } + if ( hostname == 0 ) + return(clonestr("{\"error\":\"no host specified for remote agent\"}")); + } + else if ( strcmp(name,"pangea") != 0 && strcmp(name,"InstantDEX") != 0 && strcmp(name,"jumblr") != 0 ) + return(clonestr("{\"error\":\"no port specified for remote agent\"}")); + return(iguana_addagent(name,iguana_remoteparser,hostname,methods,port,jstr(json,"pubkey"),jstr(json,"privkey"))); + } else return(clonestr("{\"error\":\"cant addagent without name and methods\"}")); + } + } + return(clonestr("{\"result\":\"stub processed generic json\"}")); +} + +char *iguana_coinjson(struct iguana_info *coin,char *method,cJSON *json) +{ + int32_t i,max,retval; struct iguana_peer *addr; char *ipaddr; cJSON *retjson = 0; + //printf("iguana_coinjson(%s)\n",jprint(json,0)); + if ( strcmp(method,"peers") == 0 ) + return(jprint(iguana_peersjson(coin),1)); + else if ( strcmp(method,"addnode") == 0 ) + { + if ( (ipaddr= jstr(json,"ipaddr")) != 0 ) + { + iguana_possible_peer(coin,ipaddr); + return(clonestr("{\"result\":\"addnode submitted\"}")); + } else return(clonestr("{\"error\":\"addnode needs ipaddr\"}")); + } + else if ( strcmp(method,"nodestatus") == 0 ) + { + if ( (ipaddr= jstr(json,"ipaddr")) != 0 ) + { + for (i=0; iMAXPEERS; i++) + { + addr = &coin->peers.active[i]; + if ( strcmp(addr->ipaddr,ipaddr) == 0 ) + return(jprint(iguana_peerjson(coin,addr),1)); + } + return(clonestr("{\"result\":\"nodestatus couldnt find ipaddr\"}")); + } else return(clonestr("{\"error\":\"nodestatus needs ipaddr\"}")); + } + else if ( strcmp(method,"maxpeers") == 0 ) + { + retjson = cJSON_CreateObject(); + if ( (max= juint(json,"max")) <= 0 ) + max = 1; + else if ( max > IGUANA_MAXPEERS ) + max = IGUANA_MAXPEERS; + if ( max > coin->MAXPEERS ) + { + for (i=max; iMAXPEERS; i++) + if ( (addr= coin->peers.ranked[i]) != 0 ) + addr->dead = 1; + } + coin->MAXPEERS = max; + jaddnum(retjson,"maxpeers",coin->MAXPEERS); + jaddstr(retjson,"coin",coin->symbol); + return(jprint(retjson,1)); + } + else if ( strcmp(method,"startcoin") == 0 ) + { + coin->active = 1; + return(clonestr("{\"result\":\"coin started\"}")); + } + else if ( strcmp(method,"pausecoin") == 0 ) + { + coin->active = 0; + return(clonestr("{\"result\":\"coin paused\"}")); + } + else if ( strcmp(method,"addcoin") == 0 ) + { + if ( (retval= iguana_launchcoin(coin->symbol,json)) > 0 ) + return(clonestr("{\"result\":\"coin added\"}")); + else if ( retval == 0 ) + return(clonestr("{\"result\":\"coin already there\"}")); + else return(clonestr("{\"error\":\"error adding coin\"}")); + } + return(clonestr("{\"error\":\"unhandled request\"}")); +} + +char *iguana_jsonstr(struct iguana_info *coin,char *jsonstr) +{ + cJSON *json; char *retjsonstr,*methodstr,*agentstr; + //printf("iguana_jsonstr.(%s)\n",jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (methodstr= jstr(json,"method")) != 0 ) + { + if ( (agentstr= jstr(json,"agent")) == 0 || strcmp(agentstr,"iguana") == 0 ) + retjsonstr = iguana_coinjson(coin,methodstr,json); + else retjsonstr = iguana_agentjson(agentstr,coin,methodstr,json); + } + else retjsonstr = clonestr("{\"error\":\"no method in JSON\"}"); + free_json(json); + } else retjsonstr = clonestr("{\"error\":\"cant parse JSON\"}"); + printf("iguana_jsonstr.(%s)\n",retjsonstr); + return(retjsonstr); +} + +char *iguana_genericjsonstr(char *jsonstr) +{ + cJSON *json; char *retjsonstr,*methodstr,*agentstr; + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (agentstr= jstr(json,"agent")) == 0 ) + agentstr = "SuperNET"; + if ( (methodstr= jstr(json,"method")) != 0 ) + retjsonstr = iguana_agentjson(agentstr,0,methodstr,json); + else retjsonstr = clonestr("{\"error\":\"no method in generic JSON\"}"); + free_json(json); + } else retjsonstr = clonestr("{\"error\":\"cant parse generic JSON\"}"); + return(retjsonstr); +} + +int32_t iguana_processjsonQ(struct iguana_info *coin) // reentrant, can be called during any idletime +{ + struct iguana_jsonitem *ptr; + if ( (ptr= queue_dequeue(&coin->finishedQ,0)) != 0 ) + { + if ( ptr->expired != 0 ) + { + printf("garbage collection: expired.(%s)\n",ptr->jsonstr); + myfree(ptr,ptr->allocsize); + } else queue_enqueue("finishedQ",&coin->finishedQ,&ptr->DL,0); + } + if ( (ptr= queue_dequeue(&coin->jsonQ,0)) != 0 ) + { + //printf("process.(%s)\n",ptr->jsonstr); + if ( (*ptr->retjsonstrp= iguana_jsonstr(coin,ptr->jsonstr)) == 0 ) + *ptr->retjsonstrp = clonestr("{\"error\":\"null return from iguana_jsonstr\"}"); + queue_enqueue("finishedQ",&coin->finishedQ,&ptr->DL,0); + return(1); + } + return(0); +} + +char *iguana_blockingjsonstr(struct iguana_info *coin,char *jsonstr,uint64_t tag,int32_t maxmillis) +{ + struct iguana_jsonitem *ptr; char *retjsonstr = 0; int32_t len,allocsize; double expiration = OS_milliseconds() + maxmillis; + if ( coin == 0 ) + { + //printf("no coin case.(%s)\n",jsonstr); + return(iguana_genericjsonstr(jsonstr)); + } + else + { + //printf("blocking case.(%s)\n",jsonstr); + len = (int32_t)strlen(jsonstr); + allocsize = sizeof(*ptr) + len + 1; + ptr = mycalloc('J',1,allocsize); + ptr->allocsize = allocsize; + ptr->retjsonstrp = &retjsonstr; + memcpy(ptr->jsonstr,jsonstr,len+1); + queue_enqueue("jsonQ",&coin->jsonQ,&ptr->DL,0); + while ( OS_milliseconds() < expiration ) + { + usleep(100); + if ( (retjsonstr= *ptr->retjsonstrp) != 0 ) + { + //printf("blocking retjsonstr.(%s)\n",retjsonstr); + queue_delete(&coin->finishedQ,&ptr->DL,allocsize,1); + return(retjsonstr); + } + usleep(1000); + } + printf("(%s) expired\n",jsonstr); + ptr->expired = (uint32_t)time(NULL); + return(clonestr("{\"error\":\"iguana jsonstr expired\"}")); + } +} + +char *iguana_JSON(char *jsonstr) +{ + cJSON *json,*retjson; uint64_t tag; uint32_t timeout; int32_t retval; + struct iguana_info *coin; char *method,*retjsonstr,*symbol,*retstr = 0; + printf("iguana_JSON.(%s)\n",jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (method= jstr(json,"method")) != 0 && strcmp(method,"addcoin") == 0 ) + { + if ( (retval= iguana_launchcoin(jstr(json,"coin"),json)) > 0 ) + return(clonestr("{\"result\":\"launched coin\"}")); + else if ( retval == 0 ) return(clonestr("{\"result\":\"coin already launched\"}")); + else return(clonestr("{\"error\":\"error launching coin\"}")); + } + if ( (tag= j64bits(json,"tag")) == 0 ) + OS_randombytes((uint8_t *)&tag,sizeof(tag)); + if ( (symbol= jstr(json,"coin")) != 0 ) + { + if ( (coin= iguana_coinfind(symbol)) != 0 && coin->launched == 0 ) + iguana_launchcoin(symbol,json); + } + else coin = 0; + if ( (timeout= juint(json,"timeout")) == 0 ) + timeout = IGUANA_JSONTIMEOUT; + if ( (retjsonstr= iguana_blockingjsonstr(coin,jsonstr,tag,timeout)) != 0 ) + { + //printf("retjsonstr.(%s)\n",retjsonstr); + if ( (retjson= cJSON_Parse(retjsonstr)) == 0 ) + { + retjson = cJSON_Parse("{\"error\":\"cant parse retjsonstr\"}"); + } + jdelete(retjson,"tag"); + jadd64bits(retjson,"tag",tag); + retstr = jprint(retjson,1); + //printf("retstr.(%s) retjsonstr.%p retjson.%p\n",retstr,retjsonstr,retjson); + free(retjsonstr);//,strlen(retjsonstr)+1); + } + free_json(json); + } else retstr = clonestr("{\"error\":\"cant parse JSON\"}"); + if ( retstr == 0 ) + retstr = clonestr("{\"error\":\"null return\"}"); + return(retstr); +} + +void iguana_issuejsonstrM(void *arg) +{ + cJSON *json; int32_t fd; char *retjsonstr,*jsonstr = arg; + retjsonstr = iguana_JSON(jsonstr); + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (fd= juint(json,"retdest")) > 0 ) + { + send(fd,jsonstr,(int32_t)strlen(jsonstr)+1,MSG_NOSIGNAL); + } + free_json(json); + return; + } + printf("%s\n",retjsonstr); + free(retjsonstr);//,strlen(retjsonstr)+1); + free(jsonstr);//,strlen(jsonstr)+1); +} + +void iguana_main(void *arg) +{ + char helperstr[64],*helperargs,*coinargs=0,*retstr,*secret,*jsonstr = arg; + int32_t i,len,flag; cJSON *json; uint8_t secretbuf[512]; + // portable_OS_init()? + mycalloc(0,0,0); + if ( (retstr= iguana_addagent("ramchain",ramchain_parser,"127.0.0.1",cJSON_Parse("[\"block\", \"tx\", \"txs\", \"rawtx\", \"balance\", \"totalreceived\", \"totalsent\", \"utxo\", \"status\"]"),0,0,0)) != 0 ) + printf("%s\n",retstr), free(retstr); + if ( (retstr= iguana_addagent("pangea",pangea_parser,"127.0.0.1",cJSON_Parse("[\"test\"]"),0,0,0)) != 0 ) + printf("%s\n",retstr), free(retstr); + if ( (retstr= iguana_addagent("InstantDEX",InstantDEX_parser,"127.0.0.1",cJSON_Parse("[\"test\"]"),0,0,0)) != 0 ) + printf("%s\n",retstr), free(retstr); + if ( (retstr= iguana_addagent("jumblr",jumblr_parser,"127.0.0.1",cJSON_Parse("[\"test\"]"),0,0,0)) != 0 ) + printf("%s\n",retstr), free(retstr); + iguana_initQ(&helperQ,"helperQ"); + OS_ensure_directory("DB"); + OS_ensure_directory("tmp"); + if ( jsonstr != 0 && (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( jobj(json,"numhelpers") != 0 ) + IGUANA_NUMHELPERS = juint(json,"numhelpers"); + if ( (secret= jstr(json,"secret")) != 0 ) + { + len = (int32_t)strlen(secret); + if ( is_hexstr(secret,0) != 0 && len <= (sizeof(secretbuf)<<1) ) + { + len >>= 1; + decode_hex(secretbuf,len,secret); + } else vcalc_sha256(0,secretbuf,(void *)secret,len), len = sizeof(bits256); + } + if ( jobj(json,"coins") != 0 ) + coinargs = jsonstr; + } + if ( IGUANA_NUMHELPERS == 0 ) + IGUANA_NUMHELPERS = 1; + for (i=0; isymbol[0] != 0 ) + flag += iguana_processjsonQ(Coins[i]); + if ( flag == 0 ) + usleep(100000); + } +} diff --git a/iguana/iguana_msg.c b/iguana/iguana_msg.c new file mode 100755 index 000000000..ed21961bf --- /dev/null +++ b/iguana/iguana_msg.c @@ -0,0 +1,848 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +// threadsafe +int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp) +{ + int32_t i; uint64_t x; + if ( rwflag == 0 ) + { + x = 0; + for (i=len-1; i>=0; i--) + { + x <<= 8; + x |= serialized[i]; + } + switch ( len ) + { + case 1: *(uint8_t *)endianedp = (uint8_t)x; break; + case 2: *(uint16_t *)endianedp = (uint16_t)x; break; + case 4: *(uint32_t *)endianedp = (uint32_t)x; break; + case 8: *(uint64_t *)endianedp = (uint64_t)x; break; + } + } + else + { + x = 0; + switch ( len ) + { + case 1: x = *(uint8_t *)endianedp; break; + case 2: x = *(uint16_t *)endianedp; break; + case 4: x = *(uint32_t *)endianedp; break; + case 8: x = *(uint64_t *)endianedp; break; + } + for (i=0; i>= 8) + serialized[i] = (uint8_t)(x & 0xff); + } + return(len); +} + +int32_t iguana_validatehdr(struct iguana_info *coin,struct iguana_msghdr *H) +{ + int32_t i,len; char *validcommands[] = + { + "version", "verack", "getaddr", "addr", "inv", "getdata", "notfound", "getblocks", "getheaders", + "headers", "tx", "block", "mempool", "ping", "pong", "reject", "filterload", "filteradd", "filterclear", "merkleblock", "alert" + }; + for (i=0; icommand,validcommands[i]) == 0 ) + { + iguana_rwnum(0,H->serdatalen,sizeof(H->serdatalen),(uint32_t *)&len); + if ( len > IGUANA_MAXPACKETSIZE ) + return(-1); + return(len); + } + return(-1); +} + +int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp) +{ + int32_t i; + if ( rwflag == 0 ) + { + for (i=0; inetmagic,netmagic,4); + strncpy(H->command,command,12); + iguana_rwnum(1,H->serdatalen,sizeof(int32_t),&datalen); + if ( data != 0 && datalen != 0 ) + { + hash2 = bits256_doublesha256(0,data,datalen); + iguana_rwbignum(1,tmp.bytes,sizeof(tmp),hash2.bytes); + for (i=0; i<4; i++) + H->hash[i] = tmp.bytes[i]; + } + else H->hash[0] = 0x5d, H->hash[1] = 0xf6, H->hash[2] = 0xe0, H->hash[3] = 0xe2; + return(datalen + sizeof(*H)); +} + +uint8_t *iguana_varint16(int32_t rwflag,uint8_t *serialized,uint16_t *varint16p) +{ + uint16_t n = 0; + if ( rwflag == 0 ) + { + n = *serialized++; + n |= ((int32_t)*serialized++ << 8); + *varint16p = n; + } + else + { + n = *varint16p; + *serialized++ = (uint8_t)n & 0xff; + *serialized++ = (uint8_t)(n >> 8) & 0xff; + } + return(serialized); +} + +uint8_t *iguana_varint32(int32_t rwflag,uint8_t *serialized,uint16_t *varint16p) +{ + serialized = iguana_varint16(rwflag,serialized,varint16p); + serialized = iguana_varint16(rwflag,serialized,&varint16p[1]); + return(serialized); +} + +uint8_t *iguana_varint64(int32_t rwflag,uint8_t *serialized,uint32_t *varint32p) +{ + serialized = iguana_varint32(rwflag,serialized,(uint16_t *)varint32p); + serialized = iguana_varint32(rwflag,serialized,(uint16_t *)&varint32p[1]); + return(serialized); +} + +int32_t iguana_rwvarint(int32_t rwflag,uint8_t *serialized,uint64_t *varint64p) +{ + uint64_t n; int32_t vlen = 1; + if ( rwflag == 0 ) + { + *varint64p = 0; + if ( (n= *serialized++) >= 0xfd ) + { + if ( n == 0xfd ) + { + n = 0; + iguana_varint16(rwflag,serialized,(uint16_t *)&n); + vlen += 2; + } + else if ( n == 0xfe ) + { + n = 0; + iguana_varint32(rwflag,serialized,(uint16_t *)&n); + vlen += 4; + } + else if ( n == 0xff ) + { + n = 0; + iguana_varint64(rwflag,serialized,(uint32_t *)&n); + vlen += 8; + } + } + *varint64p = n; + } + else + { + n = *varint64p; + if ( n < 0xfd ) + *serialized++ = (uint8_t)n; + else if ( n == 0xfd ) + { + *serialized++ = 0xfd; + iguana_varint16(rwflag,serialized,(uint16_t *)varint64p); + vlen += 2; + } + else if ( n == 0xfe ) + { + *serialized++ = 0xfe; + iguana_varint32(rwflag,serialized,(uint16_t *)varint64p); + vlen += 4; + } + else if ( n == 0xff ) + { + *serialized++ = 0xff; + iguana_varint64(rwflag,serialized,(uint32_t *)varint64p); + vlen += 8; + } + } + return(vlen); +} + +int32_t iguana_rwvarint32(int32_t rwflag,uint8_t *serialized,uint32_t *int32p) +{ + int32_t len; uint64_t x = 0; + if ( rwflag != 0 ) + x = *int32p; + len = iguana_rwvarint(rwflag,serialized,&x); + if ( rwflag == 0 ) + *int32p = (int32_t)x; + return(len); +} + +int32_t iguana_rwstr(int32_t rwflag,uint8_t *serialized,int32_t maxlen,char *endianedp) +{ + int32_t vlen; uint64_t n; + if ( rwflag == 0 ) + { + vlen = iguana_rwvarint(rwflag,serialized,&n); + memcpy(endianedp,&serialized[vlen],n); + ((uint8_t *)endianedp)[n] = 0; + } + else + { + n = strlen(endianedp); + if ( n > maxlen ) + n = maxlen; + vlen = iguana_rwvarint(rwflag,serialized,&n); + memcpy(&serialized[vlen],endianedp,n); + } + return((int32_t)(n + vlen)); +} + +int32_t iguana_rwmem(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp) +{ + if ( rwflag == 0 ) + memcpy(endianedp,serialized,len); + else memcpy(serialized,endianedp,len); + return(len); +} + +int32_t iguana_rwaddr(int32_t rwflag,uint8_t *serialized,struct iguana_msgaddress *addr,int32_t protover) +{ + int32_t len = 0; + if ( protover >= CADDR_TIME_VERSION ) + len += iguana_rwnum(rwflag,&serialized[len],sizeof(addr->nTime),&addr->nTime); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(addr->nServices),&addr->nServices); + len += iguana_rwmem(rwflag,&serialized[len],sizeof(addr->ip),&addr->ip); + if ( rwflag != 0 ) + len += iguana_rwnum(rwflag,&serialized[len],sizeof(addr->port),&addr->port); + else + { + addr->port = (uint16_t)serialized[len++] << 8; + addr->port += (uint16_t)serialized[len++]; + } + return(len); +} + +int32_t iguana_rwversion(int32_t rwflag,uint8_t *serialized,struct iguana_msgversion *msg,char *ipaddr) +{ + int32_t len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->nVersion),&msg->nVersion); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->nServices),&msg->nServices); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->nTime),&msg->nTime); + len += iguana_rwaddr(rwflag,&serialized[len],&msg->addrTo,MIN_PROTO_VERSION); + len += iguana_rwaddr(rwflag,&serialized[len],&msg->addrFrom,MIN_PROTO_VERSION); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->nonce),&msg->nonce); + len += iguana_rwstr(rwflag,&serialized[len],sizeof(msg->strSubVer),msg->strSubVer); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->nStartingHeight),&msg->nStartingHeight); + if ( msg->nVersion > 70000 ) + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->relayflag),&msg->relayflag); + if ( rwflag == 0 ) + printf("%-15s v.%llu srv.%llx %u ht.%llu [%s].R%d nonce.%llx\n",ipaddr,(long long)msg->nVersion,(long long)msg->nServices,(uint32_t)msg->nTime,(long long)msg->nStartingHeight,msg->strSubVer,msg->relayflag,(long long)msg->nonce); + return(len); +} + +int32_t iguana_rwblock(int32_t rwflag,bits256 *hash2p,uint8_t *serialized,struct iguana_msgblock *msg) +{ + int32_t len = 0; char blockhash[65]; uint64_t x; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.version),&msg->H.version); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(msg->H.prev_block),msg->H.prev_block.bytes); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(msg->H.merkle_root),msg->H.merkle_root.bytes); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.timestamp),&msg->H.timestamp); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.bits),&msg->H.bits); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->H.nonce),&msg->H.nonce); + *hash2p = bits256_doublesha256(blockhash,serialized,len); + //printf("len.%d: block version.%d timestamp.%u bits.%x nonce.%u prev.(%s) %llx blockhash.(%s) %llx\n",len,msg->H.version,msg->H.timestamp,msg->H.bits,msg->H.nonce,bits256_str(str,msg->H.prev_block),(long long)msg->H.merkle_root.txid,blockhash,(long long)hash2p->txid); + if ( rwflag != 0 ) + x = msg->txn_count; + len += iguana_rwvarint(rwflag,&serialized[len],&x); + if ( rwflag == 0 ) + { + char str[65]; + bits256_str(str,*hash2p); + if ( x < 65536 ) + msg->txn_count = (uint16_t)x; + else printf("txn_count overflow.%lld for %s\n",(long long)x,str); + } + // ? txns tx[] Block transactions, in format of "tx" command + return(len); +} + +int32_t iguana_serialize_block(bits256 *hash2p,uint8_t serialized[sizeof(struct iguana_msgblock)],struct iguana_block *block) +{ + struct iguana_msgblock msg; + memset(&msg,0,sizeof(msg)); + msg.H.version = block->RO.version; + msg.H.prev_block = block->RO.prev_block; + msg.H.merkle_root = block->RO.merkle_root; + msg.H.timestamp = block->RO.timestamp; + msg.H.bits = block->RO.bits; + msg.H.nonce = block->RO.nonce; + msg.txn_count = block->RO.txn_count; + return(iguana_rwblock(1,hash2p,serialized,&msg)); +} + +int32_t iguana_rwblockhash(int32_t rwflag,uint8_t *serialized,uint32_t *nVersionp,uint32_t *varintp,bits256 *hashes,bits256 *stophash) +{ + int32_t i,len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(*nVersionp),nVersionp); + len += iguana_rwvarint32(rwflag,&serialized[len],varintp); + if ( *varintp < IGUANA_MAXBUNDLESIZE+1 ) + { + for (i=0; i<*varintp; i++) + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(hashes[i]),hashes[i].bytes); + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(*stophash),stophash->bytes); + //for (i=0; igetdatamillis = milliseconds(); + len = iguana_queue_send(coin,addr,serialized,"getdata",len,iguana_height(coin,hashes[n-1]),forceflag); + return(len); +}*/ + +void iguana_gotversion(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgversion *vers) +{ + uint8_t serialized[sizeof(struct iguana_msghdr)]; + //printf("gotversion from %s\n",addr->ipaddr); + if ( (vers->nServices & NODE_NETWORK) == 0 ) + printf("other node.(%s) doesnt relay\n",addr->ipaddr); + else if ( (vers->nServices & NODE_NETWORK) != 0 )//&& vers->nonce != coin->instance_nonce ) + { + addr->protover = (vers->nVersion < PROTOCOL_VERSION) ? vers->nVersion : PROTOCOL_VERSION; + addr->relayflag = vers->relayflag; + addr->height = vers->nStartingHeight; + addr->relayflag = 1; + iguana_gotdata(coin,addr,addr->height); + iguana_queue_send(coin,addr,serialized,"verack",0,0,0); + //iguana_send_ping(coin,addr); + } else printf("nServices.%llx nonce.%llu invalid version message from.(%s)\n",(long long)vers->nServices,(long long)vers->nonce,addr->ipaddr); + if ( vers->nStartingHeight > coin->longestchain ) + coin->longestchain = vers->nStartingHeight; + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); +} + +int32_t iguana_send_version(struct iguana_info *coin,struct iguana_peer *addr,uint64_t myservices) +{ + int32_t len; struct iguana_msgversion msg; uint8_t serialized[sizeof(struct iguana_msghdr)+sizeof(msg)]; + memset(&msg,0,sizeof(msg)); + msg.nVersion = PROTOCOL_VERSION; + msg.nServices = myservices; + msg.nTime = (int64_t)time(NULL); + msg.nonce = coin->instance_nonce; + sprintf(msg.strSubVer,"/Satoshi:0.11.99/"); + msg.nStartingHeight = coin->blocks.hwmchain.height; + iguana_gotdata(coin,addr,msg.nStartingHeight); + len = iguana_rwversion(1,&serialized[sizeof(struct iguana_msghdr)],&msg,addr->ipaddr); + return(iguana_queue_send(coin,addr,serialized,"version",len,0,1)); +} + +void iguana_gotverack(struct iguana_info *coin,struct iguana_peer *addr) +{ + uint8_t serialized[sizeof(struct iguana_msghdr)]; + if ( addr != 0 ) + { + printf("gotverack from %s\n",addr->ipaddr); + addr->A.nTime = (uint32_t)time(NULL); + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); + } +} + +void iguana_gotaddr(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgaddress *A) +{ + char ipaddr[64]; uint32_t ipbits; + iguana_rwnum(0,&A->ip[12],sizeof(uint32_t),&ipbits); + expand_ipbits(ipaddr,ipbits); + iguana_possible_peer(coin,ipaddr); + //printf("gotaddr.(%s)\n",ipaddr); +} + +void iguana_gotping(struct iguana_info *coin,struct iguana_peer *addr,uint64_t nonce,uint8_t *data) +{ + int32_t len; uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(nonce)]; + len = iguana_rwnum(1,&serialized[sizeof(struct iguana_msghdr)],sizeof(uint64_t),&nonce); + if ( memcmp(data,&serialized[sizeof(struct iguana_msghdr)],sizeof(nonce)) != 0 ) + printf("ping ser error %llx != %llx\n",(long long)nonce,*(long long *)data); + iguana_queue_send(coin,addr,serialized,"pong",len,0,0); + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); +} + +int32_t iguana_send_ping(struct iguana_info *coin,struct iguana_peer *addr) +{ + int32_t len; uint64_t nonce; uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(nonce)]; + if ( (nonce= addr->pingnonce) == 0 ) + { + OS_randombytes((uint8_t *)&nonce,sizeof(nonce)); + addr->pingnonce = nonce; + addr->pingtime = (uint32_t)time(NULL); + } + printf("pingnonce.%llx\n",(long long)nonce); + len = iguana_rwnum(1,&serialized[sizeof(struct iguana_msghdr)],sizeof(uint64_t),&nonce); + return(iguana_queue_send(coin,addr,serialized,"ping",len,0,0)); +} + +void iguana_gotpong(struct iguana_info *coin,struct iguana_peer *addr,uint64_t nonce) +{ + if ( addr->sendmillis != 0 ) + { + addr->pingtime = (OS_milliseconds() - addr->sendmillis) + 1; + addr->pingsum += addr->pingtime, addr->numpings++; + printf("%s pingtime %.0f numpings.%d [%.3f] ",addr->ipaddr,addr->pingtime,addr->numpings,addr->pingsum/addr->numpings); + } + if ( nonce != addr->pingnonce ) + { + printf("pong received invalid pingnonce (%s) %llx vs %llx\n",addr->ipaddr,(long long)addr->pingnonce,(long long)nonce); + } else printf("(%s) pong verified with pingnonce.%llx\n",addr->ipaddr,(long long)addr->pingnonce); + addr->pingnonce = 0; + addr->sendmillis = 0; +} + +int32_t iguana_gethdrs(struct iguana_info *coin,uint8_t *serialized,char *cmd,char *hashstr) +{ + uint32_t len,n; bits256 hash2; bits256 zero; + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + memset(zero.bytes,0,sizeof(zero)); + n = 0; + len = iguana_rwnum(1,&serialized[sizeof(struct iguana_msghdr)],sizeof(uint32_t),&n); + n++; + len += iguana_rwvarint32(1,&serialized[sizeof(struct iguana_msghdr) + len],(uint32_t *)&n); + len += iguana_rwbignum(1,&serialized[sizeof(struct iguana_msghdr) + len],sizeof(bits256),hash2.bytes); + len += iguana_rwbignum(1,&serialized[sizeof(struct iguana_msghdr) + len],sizeof(bits256),(uint8_t *)zero.bytes); + return(iguana_sethdr((void *)serialized,coin->chain->netmagic,cmd,&serialized[sizeof(struct iguana_msghdr)],len)); +} + +int32_t iguana_getdata(struct iguana_info *coin,uint8_t *serialized,int32_t type,char *hashstr) +{ + uint32_t len,i,n=1; bits256 hash2; + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + len = iguana_rwvarint32(1,&serialized[sizeof(struct iguana_msghdr)],(uint32_t *)&n); + for (i=0; ichain->netmagic,"getdata",&serialized[sizeof(struct iguana_msghdr)],len)); +} + +int32_t iguana_rwvin(int32_t rwflag,struct OS_memspace *mem,uint8_t *serialized,struct iguana_msgvin *msg) +{ + int32_t len = 0; + len += iguana_rwbignum(rwflag,&serialized[len],sizeof(msg->prev_hash),msg->prev_hash.bytes); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->prev_vout),&msg->prev_vout); + len += iguana_rwvarint32(rwflag,&serialized[len],&msg->scriptlen); + if ( rwflag == 0 ) + msg->script = iguana_memalloc(mem,msg->scriptlen,1); + len += iguana_rwmem(rwflag,&serialized[len],msg->scriptlen,msg->script); + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->sequence),&msg->sequence); + //char str[65]; printf("MSGvin.(%s/v%d) script[%d]\n",bits256_str(str,msg->prev_hash),msg->prev_vout,msg->scriptlen); + //int i; for (i=0; iscriptlen; i++) + // printf("%02x ",msg->script[i]); + //printf(" inscriptlen.%d, prevhash.%llx prev_vout.%d | ",msg->scriptlen,(long long)msg->prev_hash.txid,msg->prev_vout); + return(len); +} + +int32_t iguana_rwvout(int32_t rwflag,struct OS_memspace *mem,uint8_t *serialized,struct iguana_msgvout *msg) +{ + int32_t len = 0; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->value),&msg->value); + len += iguana_rwvarint32(rwflag,&serialized[len],&msg->pk_scriptlen); + if ( rwflag == 0 ) + msg->pk_script = iguana_memalloc(mem,msg->pk_scriptlen,1); + len += iguana_rwmem(rwflag,&serialized[len],msg->pk_scriptlen,msg->pk_script); + //printf("(%.8f scriptlen.%d) ",dstr(msg->value),msg->pk_scriptlen); + //int i; for (i=0; ipk_scriptlen; i++) + // printf("%02x",msg->pk_script[i]); + //printf("\n"); + return(len); +} + +int32_t iguana_rwtx(int32_t rwflag,struct OS_memspace *mem,uint8_t *serialized,struct iguana_msgtx *msg,int32_t maxsize,bits256 *txidp,int32_t height,int32_t hastimestamp) +{ + int32_t i,len = 0; uint8_t *txstart = serialized; char txidstr[65]; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->version),&msg->version); + if ( hastimestamp != 0 ) + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->timestamp),&msg->timestamp); + len += iguana_rwvarint32(rwflag,&serialized[len],&msg->tx_in); + //printf("version.%d ",msg->version); + if ( msg->tx_in > 0 && msg->tx_out*100 < maxsize ) + { + if ( rwflag == 0 ) + msg->vins = iguana_memalloc(mem,msg->tx_in * sizeof(*msg->vins),1); + for (i=0; itx_in; i++) + len += iguana_rwvin(rwflag,mem,&serialized[len],&msg->vins[i]); + //printf("numvins.%d\n",msg->tx_in); + } + else + { + printf("invalid tx_in.%d\n",msg->tx_in); + return(-1); + } + len += iguana_rwvarint32(rwflag,&serialized[len],&msg->tx_out); + if ( msg->tx_out > 0 && msg->tx_out*32 < maxsize ) + { + //printf("numvouts.%d ",msg->tx_out); + if ( rwflag == 0 ) + msg->vouts = iguana_memalloc(mem,msg->tx_out * sizeof(*msg->vouts),1); + for (i=0; itx_out; i++) + len += iguana_rwvout(rwflag,mem,&serialized[len],&msg->vouts[i]); + } + else + { + printf("invalid tx_out.%d\n",msg->tx_out); + return(-1); + } + len += iguana_rwnum(rwflag,&serialized[len],sizeof(msg->lock_time),&msg->lock_time); + *txidp = bits256_doublesha256(txidstr,txstart,len); + msg->allocsize = len; + return(len); +} + +char *iguana_txbytes(struct iguana_info *coin,bits256 *txidp,struct iguana_txid *tx,int32_t height) +{ + int32_t i,rwflag=1,len = 0; uint8_t *serialized = coin->blockspace; char asmstr[512],txidstr[65],*txbytes = 0; + uint32_t numvins,numvouts; struct iguana_msgvin vin; struct iguana_msgvout vout; uint8_t space[8192]; + len += iguana_rwnum(rwflag,&serialized[len],sizeof(tx->version),&tx->version); + if ( coin->chain->hastimestamp != 0 ) + len += iguana_rwnum(rwflag,&serialized[len],sizeof(tx->timestamp),&tx->timestamp); + numvins = tx->numvins, numvouts = tx->numvouts; + len += iguana_rwvarint32(rwflag,&serialized[len],&numvins); + for (i=0; ilocktime),&tx->locktime); + *txidp = bits256_doublesha256(txidstr,serialized,len); + if ( memcmp(txidp,tx->txid.bytes,sizeof(*txidp)) != 0 ) + { + printf("error generating txbytes\n"); + return(0); + } + txbytes = mycalloc('x',1,len*2+1); + init_hexbytes_noT(txbytes,serialized,len*2+1); + return(txbytes); +} + +int32_t iguana_gentxarray(struct iguana_info *coin,struct OS_memspace *mem,struct iguana_txblock *txdata,int32_t *lenp,uint8_t *data,int32_t datalen) +{ + struct iguana_msgtx *tx; bits256 hash2; struct iguana_msgblock msg; int32_t i,n,len,numvouts,numvins; + memset(&msg,0,sizeof(msg)); + len = iguana_rwblock(0,&hash2,data,&msg); + iguana_blockconv(&txdata->block,&msg,hash2,-1); + tx = iguana_memalloc(mem,msg.txn_count*sizeof(*tx),1); + for (i=numvins=numvouts=0; iblock.height,coin->chain->hastimestamp)) < 0 ) + break; + numvouts += tx[i].tx_out; + numvins += tx[i].tx_in; + len += n; + } + if ( coin->chain->hastimestamp != 0 && len != datalen && data[len] == (datalen - len - 1) ) + { + //printf("\n>>>>>>>>>>> len.%d vs datalen.%d [%d]\n",len,datalen,data[len]); + memcpy(txdata->space,&data[len],datalen-len); + len += (datalen-len); + txdata->extralen = (datalen - len); + } else txdata->extralen = 0; + txdata->recvlen = len; + txdata->numtxids = msg.txn_count; + txdata->numunspents = numvouts; + txdata->numspends = numvins; + return(len); +} + +int32_t iguana_send_hashes(struct iguana_info *coin,char *command,struct iguana_peer *addr,bits256 stophash,bits256 *hashes,int32_t n) +{ + uint32_t len,nVersion,varint; int32_t retval = -1; uint8_t *serialized; long size; + size = sizeof(struct iguana_msghdr) + sizeof(uint64_t) + 1 + sizeof(bits256)*(n+1); + if ( (varint= n) <= IGUANA_MAXINV ) + { + serialized = mycalloc('h',1,size); + nVersion = 0; + len = iguana_rwblockhash(1,&serialized[sizeof(struct iguana_msghdr)],&nVersion,&varint,hashes,&stophash); + //printf("%s send_hashes.%d %s height.%d\n",addr->ipaddr,n,bits256_str(hashes[0]),iguana_height(coin,hashes[0])); + retval = iguana_queue_send(coin,addr,serialized,command,len,0,0); + myfree(serialized,size); + } else printf("iguana_send_hashes: unexpected n.%d\n",n); + return(retval); +} + +int32_t iguana_parser(struct iguana_info *coin,struct iguana_peer *addr,struct OS_memspace *rawmem,struct OS_memspace *txmem,struct OS_memspace *hashmem,struct iguana_msghdr *H,uint8_t *data,int32_t datalen) +{ + uint8_t serialized[512]; + int32_t i,retval,srvmsg,bloom,intvectors,len= -100; uint64_t nonce,x; uint32_t type; bits256 hash2; + bloom = intvectors = srvmsg = -1; + if ( addr != 0 ) + { + addr->lastcontact = (uint32_t)time(NULL); + strcpy(addr->lastcommand,H->command); + } + retval = 0; + //printf("%s parse.(%s)\n",addr->ipaddr,H->command); + if ( strcmp(H->command,"version") == 0 ) + { + struct iguana_msgversion recvmv; + if ( addr != 0 ) + { + len = iguana_rwversion(0,data,&recvmv,addr->ipaddr); + if ( len == datalen ) + iguana_gotversion(coin,addr,&recvmv); + //printf("deser.(%s) len.%d datalen.%d\n",recvmv.H.command,len,datalen); + addr->msgcounts.version++; + } + } + else if ( strcmp(H->command,"verack") == 0 ) + { + if ( addr != 0 ) + { + iguana_gotverack(coin,addr); + addr->msgcounts.verack++; + } + len = 0; + } + else if ( strcmp(H->command,"ping") == 0 ) + { + if ( datalen == sizeof(uint64_t) && addr != 0 ) + { + len = iguana_rwnum(0,data,sizeof(uint64_t),&nonce); + if ( addr != 0 ) + { + //printf("%u got nonce.%llx from %s\n",(uint32_t)time(NULL),(long long)nonce,addr->ipaddr); + iguana_gotping(coin,addr,nonce,data); + addr->msgcounts.ping++; + } + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); + } + } + else if ( strcmp(H->command,"pong") == 0 ) + { + len = 0; + if ( datalen == sizeof(uint64_t) ) + { + len = iguana_rwnum(0,data,sizeof(uint64_t),&nonce); + iguana_gotpong(coin,addr,nonce); + } else printf("unexpected pong datalen.%d\n",datalen); + if ( len == datalen && addr != 0 ) + addr->msgcounts.pong++; + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); + } + else if ( strcmp(H->command,"addr") == 0 ) + { + struct iguana_msgaddress A; + len = iguana_rwvarint(0,data,&x); + for (i=0; iprotover); + iguana_gotaddr(coin,addr,&A); + } + if ( len == datalen && addr != 0 ) + { + addr->lastgotaddr = (uint32_t)time(NULL); + addr->msgcounts.addr++; + } + //printf("%s -> addr datalen.%d num.%d\n",addr->ipaddr,datalen,(int32_t)x); + } + else if ( strcmp(H->command,"headers") == 0 ) + { + struct iguana_msgblock msg; struct iguana_block *blocks; uint32_t n; + len = iguana_rwvarint32(0,data,&n); + if ( n <= IGUANA_MAXINV ) + { + blocks = mycalloc('i',1,sizeof(*blocks) * n); + for (i=0; imsgcounts.headers++; + } else printf("got unexpected n.%d for headers\n",n); + } + else if ( strcmp(H->command,"tx") == 0 ) + { + struct iguana_msgtx *tx; + iguana_memreset(rawmem); + tx = iguana_memalloc(rawmem,sizeof(*tx),1);//mycalloc('u',1,sizeof(*tx)); + len = iguana_rwtx(0,rawmem,data,tx,datalen,&tx->txid,-1,coin->chain->hastimestamp); + iguana_gotunconfirmedM(coin,addr,tx,data,datalen); + printf("tx datalen.%d vs len.%d\n",datalen,len); + addr->msgcounts.tx++; + } + else if ( strcmp(H->command,"block") == 0 ) + { + struct iguana_txblock txdata; + if ( addr != 0 ) + addr->msgcounts.block++; + iguana_memreset(rawmem), iguana_memreset(txmem);//, iguana_memreset(hashmem); + memset(&txdata,0,sizeof(txdata)); + if ( (len= iguana_gentxarray(coin,rawmem,&txdata,&len,data,datalen)) == datalen ) + iguana_gotblockM(coin,addr,&txdata,rawmem->ptr,H,data,datalen); + else printf("parse error block txn_count.%d, len.%d vs datalen.%d\n",txdata.block.RO.txn_count,len,datalen); + } + else if ( strcmp(H->command,"reject") == 0 ) + { + for (i=0; imsgcounts.reject++; + } + else if ( strcmp(H->command,"alert") == 0 ) + { + for (i=0; imsgcounts.alert++; + } + else if ( addr != 0 ) + { + if ( strcmp(H->command,"inv") == 0 ) + intvectors = 'I', addr->msgcounts.inv++; + else if ( strcmp(H->command,"notfound") == 0 ) // for servers + intvectors = 'N', addr->msgcounts.notfound++; + else if ( strcmp(H->command,"getdata") == 0 ) // for servers + intvectors = srvmsg = 'D', addr->msgcounts.getdata++; + else if ( strcmp(H->command,"getblocks") == 0 ) // for servers + srvmsg = 'B', addr->msgcounts.getblocks++; + else if ( strcmp(H->command,"getheaders") == 0 ) // for servers + srvmsg = 'H', addr->msgcounts.getheaders++; + else if ( strcmp(H->command,"getaddr") == 0 ) // for servers + srvmsg = 'A', addr->msgcounts.getaddr++; + else if ( strcmp(H->command,"mempool") == 0 ) // for servers + srvmsg = 'M', addr->msgcounts.mempool++; + else if ( strcmp(H->command,"filterload") == 0 ) // for bloom + bloom = 'L', addr->msgcounts.filterload++; + else if ( strcmp(H->command,"filteradd") == 0 ) // for bloom + bloom = 'A', addr->msgcounts.filteradd++; + else if ( strcmp(H->command,"filterclear") == 0 ) // for bloom + bloom = 'C', addr->msgcounts.filterclear++; + else if ( strcmp(H->command,"merkleblock") == 0 ) // for bloom + bloom = 'M', addr->msgcounts.merkleblock++; + } + if ( bloom >= 0 || srvmsg >= 0 ) + len = datalen; // just mark as valid + if ( intvectors >= 0 ) + { + bits256 *txids=0,*blockhashes=0,hash; int32_t n,m; + len = n = m = 0; + len += iguana_rwvarint(0,&data[len],&x); + for (i=0; iipaddr,intvectors,i,(int32_t)x,(long long)hash.txid,len); + } + else if ( type == MSG_BLOCK ) + { + if ( blockhashes == 0 ) + { + blockhashes = mycalloc('f',(int32_t)x+1,sizeof(*blockhashes)); + n = 1; + } + blockhashes[n++] = hash; + } + else if ( type == MSG_FILTERED_BLOCK ) + printf("iv.%d %d of %d: merkle.%llx\n",intvectors,i,(int32_t)x,(long long)hash.txid); + else printf("what type is %d\n",type); + } + if ( intvectors == 'I' ) + { + if ( n > 0 ) + { + if ( n != x+1 ) + { + printf("n.%d != x.%d -> realloc blockhashes\n",n,(int32_t)x+1); + blockhashes = myrealloc('f',blockhashes,(int32_t)((x+1)*sizeof(*blockhashes)),n*sizeof(*blockhashes)); + } // else printf("n.%d == x.%d\n",n,(int32_t)x); + if ( 1 ) + iguana_gotblockhashesM(coin,addr,blockhashes,n), blockhashes = 0; + else iguana_send_hashes(coin,"getblocks",addr,blockhashes[0],&blockhashes[1],n); + } + if ( m > 0 ) + { + if ( m != x ) + txids = myrealloc('t',txids,(int32_t)((x+1)*sizeof(*txids)),(m+1)*sizeof(*txids)); + iguana_gottxidsM(coin,addr,txids,m), txids = 0; + } + } + if ( txids != 0 ) + myfree(txids,sizeof(*txids) * (x+1)); + if ( blockhashes != 0 ) + myfree(blockhashes,sizeof(*blockhashes) * (x+1)); + //printf("intvectors.%c datalen.%d\n",intvectors,datalen); + } + if ( len != datalen && len != datalen-1 ) + { + //printf("error.(%s) (%s): len.%d != datalen.%d\n",H->command,addr->ipaddr,len,datalen); + //for (i=0; icommand,"addr") != 0 ) + printf("%s.%s len mismatch %d != %d\n",addr!=0?addr->ipaddr:"local",H->command,len,datalen); + } + else if ( len == datalen-1 ) + { + printf("extra byte.[%02x] command.%s len.%d datalen.%d\n",data[datalen-1],H->command,len,datalen); + //retval = -1; + } + return(retval); +} + diff --git a/iguana/iguana_peers.c b/iguana/iguana_peers.c new file mode 100755 index 000000000..11e64a67a --- /dev/null +++ b/iguana/iguana_peers.c @@ -0,0 +1,1109 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" +void iguana_dedicatedloop(struct iguana_info *coin,struct iguana_peer *addr); + +#define _iguana_hashfind(coin,ipbits) _iguana_hashset(coin,ipbits,-1) +struct iguana_iAddr *iguana_iAddrhashfind(struct iguana_info *coin,uint32_t ipbits,int32_t createflag); + +struct iguana_iAddr *_iguana_hashset(struct iguana_info *coin,uint32_t ipbits,int32_t itemind) +{ + struct iguana_iAddr *ptr = 0; int32_t allocsize; char str[65]; struct OS_memspace *mem = 0; + expand_ipbits(str,ipbits); + HASH_FIND(hh,coin->iAddrs,&ipbits,sizeof(ipbits),ptr); + //printf("%p hashset.(%s) -> ptr.%p itemind.%d keylen.%ld %x\n",coin->iAddrs,str,ptr,itemind,sizeof(ipbits),ipbits); + if ( itemind >= 0 ) + { + if ( ptr == 0 ) + { + allocsize = (int32_t)(sizeof(*ptr)); + if ( mem != 0 ) + ptr = iguana_memalloc(mem,allocsize,1); + else ptr = mycalloc('t',1,allocsize); + if ( ptr == 0 ) + printf("fatal alloc error in hashset\n"), exit(-1); + //printf("ptr.%p allocsize.%d key.%p keylen.%d itemind.%d\n",ptr,allocsize,key,keylen,itemind); + ptr->hh.itemind = itemind; + ptr->ipbits = ipbits; + HASH_ADD(hh,coin->iAddrs,ipbits,sizeof(ipbits),ptr); + { + struct iguana_iAddr *tmp; + HASH_FIND(hh,coin->iAddrs,&ipbits,sizeof(ipbits),tmp); + if ( tmp != ptr ) + printf("%s itemind.%d search error %p != %p\n",str,itemind,ptr,tmp); + //else printf("%p added.(%s) ind.%d:%d %p tmp.%p %x\n",coin->iAddrs,str,itemind,ptr->hh.itemind,ptr,tmp,ipbits); + } + } + else ptr->hh.itemind = itemind; + } + return(ptr); +} + +struct iguana_iAddr *iguana_iAddrhashset(struct iguana_info *coin,struct iguana_iAddr *iA,int32_t ind) +{ + struct iguana_iAddr *tmp,*item; + if ( iA == 0 || iA->ipbits == 0 ) + { + printf("null iA.%p or ipbits.%x ind.%d status.%d\n",iA,iA!=0?iA->ipbits:0,iA!=0?iA->hh.itemind:0,iA!=0?iA->status:0); + exit(-1); + return(0); + } + portable_mutex_lock(&coin->peers_mutex); + if ( (item= _iguana_hashfind(coin,iA->ipbits)) == 0 ) + { + tmp = mycalloc('i',1,sizeof(*iA)); + *tmp = *iA; + iA = tmp; + if ( ind <= 0 ) + ind = coin->numiAddrs + 1; + //printf("coin->iAddrs.%p call set.(%x) ind.%d\n",coin->iAddrs,iA->ipbits,iA->ind); + if ( (item= _iguana_hashset(coin,iA->ipbits,ind)) != 0 && item->hh.itemind == coin->numiAddrs+1 ) + { + *item = *iA; + iA = item; + coin->numiAddrs++; + } else printf("iguana_hashset error numiAddrs.%d ind.%d\n",coin->numiAddrs,iA->hh.itemind); + } + else + { + *item = *iA; + iA = item; + iA->hh.itemind = ind; + } + portable_mutex_unlock(&coin->peers_mutex); + //printf("return iA.%p ind.%d %x\n",iA,iA->hh.itemind,iA->ipbits); + return(iA); +} + +struct iguana_iAddr *iguana_iAddrhashfind(struct iguana_info *coin,uint32_t ipbits,int32_t createflag) +{ + int32_t ind; struct iguana_iAddr *item = 0; + portable_mutex_lock(&coin->peers_mutex); + if ( ipbits != 0 ) + { + if ( (item= _iguana_hashfind(coin,ipbits)) == 0 && createflag != 0 ) + { + ind = coin->numiAddrs + 1; + _iguana_hashset(coin,ipbits,ind); + if ( (item= _iguana_hashfind(coin,ipbits)) != 0 ) + coin->numiAddrs++; + } + } + portable_mutex_unlock(&coin->peers_mutex); + return(item); +} + +uint32_t iguana_rwiAddrind(struct iguana_info *coin,int32_t rwflag,struct iguana_iAddr *iA,uint32_t ind) +{ + FILE *fp; char fname[512],hexstr[65]; int32_t i,n,m,retval = 0; struct iguana_iAddr tmp,*ptr; + sprintf(fname,"DB/%s/peers.dat",coin->symbol); + OS_compatible_path(fname); + if ( rwflag < 0 || iA == 0 ) + { + coin->numiAddrs = 0; + if ( (fp= fopen(fname,"rb+")) != 0 ) + { + fseek(fp,0,SEEK_END); + n = (int32_t)(ftell(fp) / sizeof(*iA)); + for (i=m=0; ipeers_mutex); + HASH_FIND(hh,coin->iAddrs,&tmp.ipbits,sizeof(tmp.ipbits),ptr); + if ( ptr == 0 ) + { + ptr = mycalloc('t',1,sizeof(*ptr)); + if ( ptr == 0 ) + printf("fatal alloc error in hashset\n"), exit(-1); + ptr->hh.itemind = m; + ptr->ipbits = tmp.ipbits; + HASH_ADD(hh,coin->iAddrs,ipbits,sizeof(tmp.ipbits),ptr); + if ( i != m ) + { + tmp.hh.itemind = m; + fseek(fp,m*sizeof(tmp),SEEK_SET); + fwrite(&tmp,1,sizeof(tmp),fp); + } + //printf("m.%d %x\n",m,tmp.ipbits); + m++; + coin->numiAddrs = m; + expand_ipbits(hexstr,tmp.ipbits); + iguana_possible_peer(coin,hexstr); + } + portable_mutex_unlock(&coin->peers_mutex); + } + } + fclose(fp); + printf("i.%d m.%d numiAddrs.%d\n",i,m,coin->numiAddrs); + } + return(coin->numiAddrs); + } + if ( rwflag == 0 ) + { + memset(iA,0,sizeof(*iA)); + if ( (fp= fopen(fname,"rb")) != 0 ) + { + fseek(fp,ind * sizeof(*iA),SEEK_SET); + if ( ftell(fp) == ind * sizeof(*iA) ) + { + if ( fread(iA,1,sizeof(*iA),fp) != sizeof(*iA) ) + printf("iAddr: error loading.[%d]\n",ind); + else + { + if ( (iA= iguana_iAddrhashset(coin,iA,ind)) != 0 ) + { + retval = iA->hh.itemind+1; + //printf("r %p ipbits.%x ind.%d saved iA->ind.%d retval.%d\n",iA,iA->ipbits,ind,iA->hh.itemind,retval); + } + } + } else printf("iAddr: error seeking.[%d] %ld vs %ld\n",ind,ftell(fp),ind * sizeof(*iA)); + fclose(fp); + } + } + else + { + if ( (fp= fopen(fname,"rb+")) == 0 ) + fp = fopen(fname,"wb"); + if ( fp != 0 ) + { + fseek(fp,ind * sizeof(*iA),SEEK_SET); + if ( ftell(fp) == ind * sizeof(*iA) ) + { + iA->hh.itemind = ind; + if ( fwrite(iA,1,sizeof(*iA),fp) != sizeof(*iA) ) + printf("iAddr: error saving.[%d]\n",ind); + else + { + if ( (iA= iguana_iAddrhashset(coin,iA,ind)) != 0 ) + { + retval = iA->hh.itemind+1; + //printf("W %p ipbits.%x ind.%d saved iA->ind.%d retval.%d\n",iA,iA->ipbits,ind,iA->hh. itemind,retval); + } + } + } else printf("iAddr: error seeking.[%d] %ld vs %ld\n",ind,ftell(fp),ind * sizeof(*iA)); + fclose(fp); + } else printf("error creating.(%s)\n",fname); + } + return(retval); +} + +void iguana_iAconnected(struct iguana_info *coin,struct iguana_peer *addr) +{ + struct iguana_iAddr *iA; + if ( (iA= iguana_iAddrhashfind(coin,addr->ipbits,1)) != 0 ) + { + iA->status = IGUANA_PEER_READY; + if ( addr->height > iA->height ) + iA->height = addr->height; + iA->numconnects++; + iA->lastconnect = (uint32_t)time(NULL); + if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) == 0 ) + printf("iguana_iAconnected (%s) save error iA->ind.%d\n",addr->ipaddr,iA->hh.itemind); + //else printf("iguana_iAconnected.(%s)\n",addr->ipaddr); + } else printf("iguana_iAconnected error getting iA\n"); +} + +void iguana_iAkill(struct iguana_info *coin,struct iguana_peer *addr,int32_t markflag) +{ + struct iguana_iAddr *iA; int32_t rank; char ipaddr[64]; + if ( addr->ipbits == 0 ) + { + printf("cant iAkill null ipbits\n"); + return; + } + rank = addr->rank; + strcpy(ipaddr,addr->ipaddr); + if ( addr->usock >= 0 ) + close(addr->usock), addr->usock = -1; + if ( addr == coin->peers.localaddr ) + coin->peers.localaddr = 0; + //printf("iAkill.(%s)\n",addr->ipaddr); + if ( (iA= iguana_iAddrhashfind(coin,addr->ipbits,1)) != 0 ) + { + iA->status = IGUANA_PEER_KILLED; + if ( addr->height > iA->height ) + iA->height = addr->height; + if ( markflag != 0 ) + { + iA->numkilled++; + iA->lastkilled = (uint32_t)time(NULL); + if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) == 0 ) + printf("killconnection (%s) save error\n",addr->ipaddr); + } + } else printf("killconnection cant get ind for ipaddr.%s\n",addr->ipaddr); + //memset(addr,0,sizeof(*addr)); + addr->usock = -1; + addr->pending = 0; + if ( rank > 0 ) + iguana_possible_peer(coin,ipaddr); +} + +/*int32_t pp_bind(char *hostname,uint16_t port) +{ + int32_t opt; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); + struct hostent* hostent = gethostbyname(hostname); + if (hostent == NULL) { + PostMessage("gethostbyname() returned error: %d", errno); + return -1; + } + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + memcpy(&addr.sin_addr.s_addr, hostent->h_addr_list[0], hostent->h_length); + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + printf("socket() failed: %s", strerror(errno)); + return -1; + } + opt = 1; + setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&opt,sizeof(opt)); +#ifdef __APPLE__ + setsockopt(sock,SOL_SOCKET,SO_NOSIGPIPE,&opt,sizeof(opt)); +#endif + //timeout.tv_sec = 0; + //timeout.tv_usec = 1000; + //setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout,sizeof(timeout)); + int result = bind(sock, (struct sockaddr*)&addr, addrlen); + if (result != 0) { + printf("bind() failed: %s", strerror(errno)); + close(sock); + return -1; + } + return(sock); +}*/ + +int32_t iguana_socket(int32_t bindflag,char *hostname,uint16_t port) +{ + int32_t opt,sock,result; uint32_t ipbits; char ipaddr[64]; struct timeval timeout; + struct sockaddr_in saddr; socklen_t addrlen; + addrlen = sizeof(saddr); + struct hostent *hostent = gethostbyname(hostname); + if ( hostent == NULL ) + { + printf("gethostbyname() returned error: %d",errno); + return(-1); + } + saddr.sin_family = AF_INET; + saddr.sin_port = htons(port); + memcpy(&saddr.sin_addr.s_addr,hostent->h_addr_list[0],hostent->h_length); + ipbits = (uint32_t)calc_ipbits(hostname); + //printf("ipbits.%08x vs %08x\n",ipbits,saddr.sin_addr.s_addr); + expand_ipbits(ipaddr,saddr.sin_addr.s_addr); + //if ( bindflag != 0 ) + // printf("iguana_socket.(%s:%d) bind.%d\n",ipaddr,port,bindflag), getchar(); + if ( strcmp(ipaddr,hostname) != 0 ) + printf("iguana_socket mismatch (%s) -> (%s)?\n",hostname,ipaddr); + if ( (sock= socket(AF_INET,SOCK_STREAM,0)) < 0 ) + { + if ( errno != ETIMEDOUT ) + printf("socket() failed: %s errno.%d", strerror(errno),errno); + return(-1); + } + if ( 0 && bindflag != 0 ) + { + timeout.tv_sec = 0; + timeout.tv_usec = 100000; + setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout,sizeof(timeout)); + } + opt = 1; + setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&opt,sizeof(opt)); +#ifdef __APPLE__ + setsockopt(sock,SOL_SOCKET,SO_NOSIGPIPE,&opt,sizeof(opt)); +#endif + result = (bindflag != 0) ? bind(sock,(struct sockaddr*)&saddr,addrlen) : connect(sock,(struct sockaddr *)&saddr,addrlen); + if ( result != 0 ) + { + if ( errno != ECONNRESET && errno != ENOTCONN && errno != ECONNREFUSED && errno != ETIMEDOUT && errno != EHOSTUNREACH ) + printf("connect(%s) port.%d failed: %s sock.%d. errno.%d\n",hostname,port,strerror(errno),sock,errno); + if ( sock >= 0 ) + close(sock); + return(-1); + } + if ( bindflag != 0 && listen(sock,3) != 0 ) + { + printf("listen(%s) port.%d failed: %s sock.%d. errno.%d\n",hostname,port,strerror(errno),sock,errno); + if ( sock >= 0 ) + close(sock); + return(-1); + } + return(sock); +} + +int32_t iguana_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *serialized,int32_t len) +{ + int32_t numsent,remains,usock; + if ( addr == 0 && coin->peers.numranked > 1 ) + addr = coin->peers.ranked[rand() % coin->peers.numranked]; + if ( addr == 0 ) + return(-1); + usock = addr->usock; + if ( usock < 0 || addr->dead != 0 ) + return(-1); + remains = len; + //printf(" send.(%s) %d bytes to %s\n",(char *)&serialized[4],len,addr->ipaddr);// getchar(); + if ( strcmp((char *)&serialized[4],"ping") == 0 ) + addr->sendmillis = OS_milliseconds(); + if ( len > IGUANA_MAXPACKETSIZE ) + printf("sending too big! %d\n",len); + while ( remains > 0 ) + { + if ( coin->peers.shuttingdown != 0 ) + return(-1); + if ( (numsent= (int32_t)send(usock,serialized,remains,MSG_NOSIGNAL)) < 0 ) + { + if ( errno != EAGAIN && errno != EWOULDBLOCK ) + { + printf("%s: %s numsent.%d vs remains.%d len.%d errno.%d (%s) usock.%d\n",serialized+4,addr->ipaddr,numsent,remains,len,errno,strerror(errno),addr->usock); + printf("bad errno.%d %s zombify.%p\n",errno,strerror(errno),&addr->dead); + addr->dead = (uint32_t)time(NULL); + return(-errno); + } //else usleep(*sleeptimep), *sleeptimep *= 1.1; + } + else if ( remains > 0 ) + { + remains -= numsent; + serialized += numsent; + if ( remains > 0 ) + printf("iguana sent.%d remains.%d of len.%d\n",numsent,remains,len); + } + } + addr->totalsent += len; + //printf(" sent.%d bytes to %s\n",len,addr->ipaddr);// getchar(); + return(len); +} + +int32_t iguana_queue_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *serialized,char *cmd,int32_t len,int32_t getdatablock,int32_t forceflag) +{ + struct iguana_packet *packet; int32_t datalen; + if ( addr == 0 ) + { + printf("iguana_queue_send null addr\n"); + exit(-1); + return(-1); + } + datalen = iguana_sethdr((void *)serialized,coin->chain->netmagic,cmd,&serialized[sizeof(struct iguana_msghdr)],len); + if ( strcmp("getaddr",cmd) == 0 && time(NULL) < addr->lastgotaddr+300 ) + return(0); + if ( strcmp("version",cmd) == 0 ) + return(iguana_send(coin,addr,serialized,datalen)); + packet = mycalloc('S',1,sizeof(struct iguana_packet) + datalen); + packet->datalen = datalen; + packet->addr = addr; + memcpy(packet->serialized,serialized,datalen); + //printf("%p queue send.(%s) %d to (%s) %x\n",packet,serialized+4,datalen,addr->ipaddr,addr->ipbits); + queue_enqueue("sendQ",&addr->sendQ,&packet->DL,0); + return(datalen); +} + +int32_t iguana_recv(int32_t usock,uint8_t *recvbuf,int32_t len) +{ + int32_t recvlen,remains = len; + while ( remains > 0 ) + { + if ( (recvlen= (int32_t)recv(usock,recvbuf,remains,0)) < 0 ) + { + if ( errno == EAGAIN ) + { +#ifdef IGUANA_DEDICATED_THREADS + //printf("EAGAIN for len %d, remains.%d\n",len,remains); +#endif + usleep(10000); + } + else return(-errno); + } + else + { + if ( recvlen > 0 ) + { + remains -= recvlen; + recvbuf = &recvbuf[recvlen]; + } else usleep(10000); + //if ( remains > 0 ) + //printf("got %d remains.%d of total.%d\n",recvlen,remains,len); + } + } + return(len); +} + +void iguana_parsebuf(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msghdr *H,uint8_t *buf,int32_t len) +{ + struct iguana_msghdr checkH; + memset(&checkH,0,sizeof(checkH)); + iguana_sethdr(&checkH,coin->chain->netmagic,H->command,buf,len); + if ( memcmp(&checkH,H,sizeof(checkH)) == 0 ) + { + //if ( strcmp(addr->ipaddr,"127.0.0.1") == 0 ) + //printf("%s parse.(%s) len.%d\n",addr->ipaddr,H.command,len); + //printf("addr->dead.%u\n",addr->dead); + if ( strcmp(H->command,"block") == 0 || strcmp(H->command,"tx") == 0 ) + { + if ( addr->RAWMEM.ptr == 0 ) + iguana_meminit(&addr->RAWMEM,addr->ipaddr,0,IGUANA_MAXPACKETSIZE,0); + if ( addr->TXDATA.ptr == 0 ) + iguana_meminit(&addr->TXDATA,"txdata",0,IGUANA_MAXPACKETSIZE,0); + if ( addr->HASHMEM.ptr == 0 ) + iguana_meminit(&addr->HASHMEM,"HASHPTRS",0,256,0);//IGUANA_MAXPACKETSIZE*16,0); + //printf("Init %s memory %p %p %p\n",addr->ipaddr,addr->RAWMEM.ptr,addr->TXDATA.ptr,addr->HASHMEM.ptr); + } + if ( iguana_parser(coin,addr,&addr->RAWMEM,&addr->TXDATA,&addr->HASHMEM,H,buf,len) < 0 || addr->dead != 0 ) + { + printf("%p addr->dead.%d or parser break at %u\n",&addr->dead,addr->dead,(uint32_t)time(NULL)); + addr->dead = (uint32_t)time(NULL); + } + else + { + addr->numpackets++; + addr->totalrecv += len; + coin->totalrecv += len, coin->totalpackets++; + //printf("next iter.(%s) numreferrals.%d numpings.%d\n",addr->ipaddr,addr->numreferrals,addr->numpings); + } + } else printf("header error from %s\n",addr->ipaddr); +} + +void _iguana_processmsg(struct iguana_info *coin,int32_t usock,struct iguana_peer *addr,uint8_t *_buf,int32_t maxlen) +{ + int32_t len,recvlen; void *buf = _buf; struct iguana_msghdr H; + if ( coin->peers.shuttingdown != 0 || addr->dead != 0 ) + return; + //printf("%p got.(%s) from %s | usock.%d ready.%u dead.%u\n",addr,H.command,addr->ipaddr,addr->usock,addr->ready,addr->dead); + memset(&H,0,sizeof(H)); + if ( (recvlen= (int32_t)iguana_recv(usock,(uint8_t *)&H,sizeof(H))) == sizeof(H) ) + { + //printf("%p got.(%s) recvlen.%d from %s | usock.%d ready.%u dead.%u\n",addr,H.command,recvlen,addr->ipaddr,addr->usock,addr->ready,addr->dead); + if ( coin->peers.shuttingdown != 0 || addr->dead != 0 ) + return; + if ( (len= iguana_validatehdr(coin,&H)) >= 0 ) + { + if ( len > 0 ) + { + if ( len > IGUANA_MAXPACKETSIZE ) + { + printf("buffer %d too small for %d\n",IGUANA_MAXPACKETSIZE,len); + return; + } + if ( len > maxlen ) + buf = mycalloc('p',1,len); + if ( (recvlen= iguana_recv(usock,buf,len)) < 0 ) + { + printf("recv error on (%s) len.%d errno.%d (%s)\n",H.command,len,-recvlen,strerror(-recvlen)); + if ( buf != _buf ) + myfree(buf,len); + addr->dead = (uint32_t)time(NULL); + return; + } + } + iguana_parsebuf(coin,addr,&H,buf,len); + if ( buf != _buf ) + myfree(buf,len); + return; + } + printf("invalid header received from (%s)\n",addr->ipaddr); + addr->dead = 1; + } + printf("%s recv error on hdr errno.%d (%s)\n",addr->ipaddr,-recvlen,strerror(-recvlen)); +#ifndef IGUANA_DEDICATED_THREADS + addr->dead = 1; +#endif +} + +void iguana_gotdata(struct iguana_info *coin,struct iguana_peer *addr,int32_t height) +{ + struct iguana_iAddr *iA; + if ( addr != 0 && height > addr->height && height < coin->longestchain ) + { + if ( (iA= iguana_iAddrhashfind(coin,addr->ipbits,0)) != 0 && iA->height < height ) + iA->height = height; + //iguana_set_iAddrheight(coin,addr->ipbits,height); + addr->height = height; + } +} + +int32_t iguana_iAddrheight(struct iguana_info *coin,uint32_t ipbits) +{ + struct iguana_iAddr *iA; + if ( (iA= iguana_iAddrhashfind(coin,ipbits,0)) != 0 ) + return(iA->height); + return(0); +} + +void iguana_startconnection(void *arg) +{ + int32_t i,n; char ipaddr[64]; struct iguana_peer *addr = arg; struct iguana_info *coin = 0; + if ( addr == 0 || (coin= iguana_coinfind(addr->symbol)) == 0 ) + { + printf("iguana_startconnection nullptrs addr.%p coin.%p\n",addr,coin); + return; + } + addr->addrind = (int32_t)(((long)addr - (long)&coin->peers.active[0]) / sizeof(*addr)); + if ( addr->usock >= 0 ) + { + return; + } + if ( strcmp(coin->name,addr->coinstr) != 0 ) + { + printf("iguana_startconnection.%s mismatched coin.%p (%s) vs (%s)\n",addr->ipaddr,coin,coin->symbol,addr->coinstr); + return; + } + if ( strcmp("127.0.0.1",addr->ipaddr) == 0 && (coin->myservices & NODE_NETWORK) != 0 ) + { + printf("avoid self-loopback\n"); + return; + } + printf("startconnection.(%s) pending.%u usock.%d addrind.%d\n",addr->ipaddr,addr->pending,addr->usock,addr->addrind); + addr->pending = (uint32_t)time(NULL); + if ( addr->usock < 0 ) + addr->usock = iguana_socket(0,addr->ipaddr,coin->chain->portp2p); + if ( addr->usock < 0 || coin->peers.shuttingdown != 0 ) + { + strcpy(ipaddr,addr->ipaddr); + iguana_iAkill(coin,addr,1); + printf("refused PEER KILLED. for %s:%d usock.%d\n",addr->ipaddr,coin->chain->portp2p,addr->usock); + addr->pending = 0; + addr->ipbits = 0; + addr->dead = 1; + } + else + { + addr->ready = (uint32_t)time(NULL); + addr->ipbits = (uint32_t)calc_ipbits(addr->ipaddr); + addr->dead = 0; + addr->pending = 0; + addr->height = iguana_iAddrheight(coin,addr->ipbits); + strcpy(addr->symbol,coin->symbol); + strcpy(addr->coinstr,coin->name); + coin->peers.lastpeer = (uint32_t)time(NULL); + for (i=n=0; iMAXPEERS; i++) + if ( coin->peers.active[i].usock > 0 ) + n++; + iguana_iAconnected(coin,addr); + coin->peers.numconnected++; + printf("PEER CONNECTED.%d:%d of max.%d! %s:%d usock.%d\n",coin->peers.numconnected,n,coin->MAXPEERS,addr->ipaddr,coin->chain->portp2p,addr->usock); + if ( strcmp("127.0.0.1",addr->ipaddr) == 0 ) + coin->peers.localaddr = addr; +#ifdef IGUANA_DEDICATED_THREADS + //iguana_launch("recv",iguana_dedicatedrecv,addr,IGUANA_RECVTHREAD); + iguana_dedicatedloop(coin,addr); +#endif + } +} + +struct iguana_peer *iguana_peerslot(struct iguana_info *coin,uint32_t ipbits) +{ + int32_t i; struct iguana_peer *addr; char ipaddr[64]; + expand_ipbits(ipaddr,ipbits); +#ifdef IGUANA_DISABLEPEERS + if ( strcmp("127.0.0.1",ipaddr) != 0 ) + return(0); +#endif + //portable_mutex_lock(&coin->peers_mutex); + for (i=0; iMAXPEERS && ipeers.active[i]; + addr->addrind = i; + if ( addr->usock >= 0 || addr->pending != 0 || addr->ipbits == ipbits || strcmp(ipaddr,addr->ipaddr) == 0 ) + { + //printf("skip.(%s) usock.%d pending.%d ipbits.%x vs %x lag.%ld\n",addr->ipaddr,addr->usock,addr->pending,addr->ipbits,iA->ipbits,time(NULL)-addr->pending); + continue; + } + portable_mutex_lock(&coin->peers_mutex); + if ( addr->ipbits == 0 ) + { + iguana_initpeer(coin,addr,ipbits); + //addr->pending = (uint32_t)time(NULL); + portable_mutex_unlock(&coin->peers_mutex); + return(addr); + } + portable_mutex_unlock(&coin->peers_mutex); + } + return(0); +} + +void *iguana_iAddriterator(struct iguana_info *coin,struct iguana_iAddr *iA) +{ + struct iguana_peer *addr = 0; + if ( iA != 0 && iA->ipbits != 0 && iguana_numthreads(coin,1 << IGUANA_CONNTHREAD) < IGUANA_MAXCONNTHREADS && iA->status == IGUANA_PEER_ELIGIBLE ) + { + //printf("%x\n",iA->ipbits); + //portable_mutex_unlock(&coin->peers_mutex); + if ( (addr= iguana_peerslot(coin,iA->ipbits)) != 0 )//i < coin->MAXPEERS && i < IGUANA_MAXPEERS && addr != 0 ) + { + //printf("pend.%d status.%d possible peer.(%s).%x threads %d %d %d %d\n",addr->pending,iA->status,addr->ipaddr,addr->ipbits,iguana_numthreads(coin,0),iguana_numthreads(coin,1),iguana_numthreads(coin,2),iguana_numthreads(coin,3)); + if ( addr->pending == 0 && iA->status != IGUANA_PEER_CONNECTING ) + { + iA->status = IGUANA_PEER_CONNECTING; + addr->pending = (uint32_t)time(NULL); + if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) > 0 ) + { + printf("iA.%p iguana_startconnection.(%s) status.%d pending.%d\n",iA,addr->ipaddr,iA->status,addr->pending); + iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD); + } else printf("error rwiAddrind.%d\n",iA->hh.itemind); + } + } else printf("no open peer slots left\n"); + } + //else if ( iA != 0 ) + // printf("iA->ipbits %d, %d iguana_numthreads(coin,1 << IGUANA_CONNTHREAD) status.%d\n",iA->ipbits,iguana_numthreads(coin,1 << IGUANA_CONNTHREAD),iA->status); + //else printf("connector null iA\n"); + return(0); +} + +uint32_t iguana_possible_peer(struct iguana_info *coin,char *ipaddr) +{ + char checkaddr[64]; uint32_t ipbits,now = (uint32_t)time(NULL); int32_t i; struct iguana_iAddr *iA; + if ( ipaddr != 0 ) + { + //printf("%p Q possible peer.(%s)\n",coin,ipaddr); + queue_enqueue("possibleQ",&coin->possibleQ,queueitem(ipaddr),1); + return((uint32_t)time(NULL)); + } + else if ( (ipaddr= queue_dequeue(&coin->possibleQ,1)) == 0 ) + return((uint32_t)time(NULL)); +#ifdef IGUANA_DISABLEPEERS + if ( strcmp(ipaddr,"127.0.0.1") != 0 ) + { + free_queueitem(ipaddr); + return((uint32_t)time(NULL)); + } +#endif + //printf("check possible peer.(%s)\n",ipaddr); + for (i=0; iMAXPEERS; i++) + if ( strcmp(ipaddr,coin->peers.active[i].ipaddr) == 0 ) + { + //printf("(%s) already active\n",ipaddr); + free_queueitem(ipaddr); + return((uint32_t)time(NULL)); + } + if ( strncmp("0.0.0",ipaddr,5) != 0 && strcmp("0.0.255.255",ipaddr) != 0 && strcmp("1.0.0.0",ipaddr) != 0 ) + { + if ( (ipbits= (uint32_t)calc_ipbits(ipaddr)) != 0 ) + { + expand_ipbits(checkaddr,ipbits); + if ( strcmp(checkaddr,ipaddr) == 0 ) + { + //printf("valid ipaddr.(%s) MAXPEERS.%d\n",ipaddr,coin->MAXPEERS); + if ( (iA= iguana_iAddrhashfind(coin,ipbits,1)) != 0 ) + { + if ( iA->status != IGUANA_PEER_CONNECTING && iA->status != IGUANA_PEER_READY && iA->status != IGUANA_PEER_ELIGIBLE ) + { + if ( (iA->lastconnect == 0 || iA->lastkilled == 0) || (iA->numconnects > 0 && iA->lastconnect > (now - IGUANA_RECENTPEER)) || iA->lastkilled < now-600 ) + { + iA->status = IGUANA_PEER_ELIGIBLE; + if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) == 0 ) + printf("error updating status for (%s) ind.%d\n",ipaddr,iA->hh.itemind); + iguana_iAddriterator(coin,iA); + } else printf("ignore.(%s) lastconnect.%u lastkilled.%u numconnects.%d\n",ipaddr,iA->lastconnect,iA->lastkilled,iA->numconnects); + } //else printf("skip.(%s) ind.%d status.%d\n",ipaddr,iA->hh.itemind,iA->status); + } else printf("cant find (%s) which should have been created\n",ipaddr); + } else printf("reject ipaddr.(%s)\n",ipaddr); + } + } + free_queueitem(ipaddr); + return((uint32_t)time(NULL)); +} + +void iguana_processmsg(void *ptr) +{ + struct iguana_info *coin; uint8_t buf[32768]; struct iguana_peer *addr = ptr; + if ( addr == 0 || (coin= iguana_coinfind(addr->symbol)) == 0 || addr->dead != 0 ) + { + printf("iguana_processmsg cant find addr.%p symbol.%s\n",addr,addr!=0?addr->symbol:0); + return; + } + _iguana_processmsg(coin,addr->usock,addr,buf,sizeof(buf)); + addr->startrecv = 0; +} + +int32_t iguana_pollsendQ(struct iguana_info *coin,struct iguana_peer *addr) +{ + struct iguana_packet *packet; + if ( (packet= queue_dequeue(&addr->sendQ,0)) != 0 ) + { + //printf("%s: send.(%s) usock.%d dead.%u ready.%u\n",addr->ipaddr,packet->serialized+4,addr->usock,addr->dead,addr->ready); + if ( strcmp((char *)&packet->serialized[4],"getdata") == 0 ) + { + printf("unexpected getdata for %s\n",addr->ipaddr); + myfree(packet,sizeof(*packet) + packet->datalen); + } + else + { + iguana_send(coin,addr,packet->serialized,packet->datalen); + myfree(packet,sizeof(*packet) + packet->datalen); + return(1); + } + } + return(0); +} + +int32_t iguana_pollrecv(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *buf,int32_t bufsize) +{ +#ifndef IGUANA_DEDICATED_THREADS + strcpy(addr->symbol,coin->symbol); + if ( addr != coin->peers.localaddr ) + { + addr->startrecv = (uint32_t)time(NULL); + iguana_launch("processmsg",iguana_processmsg,addr,IGUANA_RECVTHREAD); + } + else +#endif + _iguana_processmsg(coin,addr->usock,addr,buf,bufsize); + return(1); +} + +void iguana_acceptloop(void *args) +{ + socklen_t clilen; struct sockaddr_in cli_addr; uint32_t ipbits; uint16_t port; int32_t bindsock; + struct iguana_peer *addr; struct iguana_info *coin = args; + port = coin->chain->portp2p; + bindsock = iguana_socket(1,"127.0.0.1",port); + printf("iguana_bindloop 127.0.0.1:%d bind sock.%d\n",port,bindsock); + while ( bindsock >= 0 ) + { + ipbits = rand(); + while ( (addr= iguana_peerslot(coin,ipbits)) == 0 ) + { + ipbits = rand(); + sleep(1); + } + clilen = sizeof(cli_addr); + printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",port,bindsock); + addr->usock = accept(bindsock,(struct sockaddr *)&cli_addr,&clilen); + if ( addr->usock < 0 ) + { + printf("ERROR on accept bindsock.%d errno.%d (%s)\n",bindsock,errno,strerror(errno)); + continue; + } + memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); + addr->ipbits = ipbits; + expand_ipbits(addr->ipaddr,addr->ipbits); + printf("NEWSOCK.%d for %x (%s)\n",addr->usock,addr->ipbits,addr->ipaddr); + iguana_dedicatedloop(coin,addr); + } +} + +/*void iguana_recvloop(void *arg) +{ + int32_t i; uint8_t buf[32768]; struct iguana_info *coin = arg; + while ( 1 ) + { + if ( coin->numsocks == 0 ) + { + sleep(1); + continue; + } + for (i=0; inumsocks; i++) + coin->fds[i].revents = POLLIN, coin->fds[i].events = 0; + if ( poll(coin->fds,coin->numsocks,10000) > 0 ) + { + for (i=0; inumsocks; i++) + { + if ( (coin->fds[i].events & POLLIN) != 0 ) + { + printf("process message from [%d]\n",i); + _iguana_processmsg(coin,coin->fds[i].fd,&coin->bindaddr,buf,sizeof(buf)); + printf("iguana_bindloop processed.(%s)\n",buf+4); + } + } + } else printf("numsocks.%d nothing to receive\n",coin->numsocks); + } +}*/ + +#ifdef IGUANA_PEERALLOC +void *iguana_peeralloc(struct iguana_info *coin,struct iguana_peer *addr,int32_t datalen) +{ + struct OS_memspace *mem; long i,iter; int32_t j,diff,size,bestfit; void *ptr; + //printf("iguana_peeralloc.%s\n",addr->ipaddr); + while ( 1 ) + { + bestfit = -1; + for (iter=0; iter<3; iter++) + { + for (i=0; iSEROUT)/sizeof(*addr->SEROUT); i++) + { + mem = addr->SEROUT[i]; + if ( mem->threadsafe != 0 ) + portable_mutex_lock(&mem->mutex); + if ( iter < 2 && mem->availptrs > 0 ) + { + for (j=0; jnumptrs; j++) + { + if ( mem->allocsizes[j] == 0 ) + { + size = mem->maxsizes[j]; + if ( size >= datalen ) + { + diff = (size - datalen); + if ( diff == 0 || (iter == 1 && diff == bestfit) ) + { + mem->allocsizes[j] = datalen; + mem->availptrs--; + //printf("%s availptrs.%d size.%d j.%d diff.%d bestfit.%d %p.%d max.%d\n",mem->name,mem->availptrs,size,j,diff,bestfit,mem->ptrs[j],size,mem->maxsizes[j]); + if ( mem->threadsafe != 0 ) + portable_mutex_unlock(&mem->mutex); + return(mem->ptrs[j]); + } + else if ( iter == 0 && diff < (datalen >> 3) && diff < 4096 ) + bestfit = diff; + } + } + } + } + else if ( iter == 2 && (ptr= iguana_memalloc(mem,datalen,0)) != 0 ) + { + if ( mem->threadsafe != 0 ) + portable_mutex_unlock(&mem->mutex); + ///printf("alloc iter.2\n"); + return(ptr); + } + if ( mem->threadsafe != 0 ) + portable_mutex_unlock(&mem->mutex); + //printf("iter.%ld bestfit.%d\n",iter,bestfit); + } + } + printf("iguana_peeralloc: cant find memory. wait and hope...\n"); + sleep(5); + } + return(0); +} + +int64_t iguana_peerallocated(struct iguana_info *coin,struct iguana_peer *addr) +{ + int32_t i; int64_t total = 0; + for (i=0; iSEROUT)/sizeof(*addr->SEROUT); i++) + if ( addr->SEROUT[i] != 0 ) + total += iguana_memallocated(addr->SEROUT[i]); + return(total); +} + +int64_t iguana_peerfree(struct iguana_info *coin,struct iguana_peer *addr,void *ptr,int32_t datalen) +{ + struct OS_memspace *mem; long offset,i; int64_t avail = -1; + //printf("iguana_peerfree.%p %d\n",ptr,datalen); + for (i=0; iSEROUT)/sizeof(*addr->SEROUT); i++) + { + mem = addr->SEROUT[i]; + offset = ((long)ptr - (long)mem->ptr); + if ( offset >= 0 && offset+datalen < mem->totalsize ) + { + if ( iguana_memfree(mem,ptr,datalen) < 0 || (avail= iguana_peerallocated(coin,addr)) < 0 ) + { + printf("iguana_peerfree: corrupted mem avail.%lld ptr.%p %d\n",(long long)avail,ptr,datalen); + exit(-1); + } + return(avail); + } + } + printf("iguana_peerfree: cant find ptr.%p %d\n",ptr,datalen); + return(-1); +} +#else +void *iguana_peeralloc(struct iguana_info *coin,struct iguana_peer *addr,int32_t datalen) +{ + addr->allocated += datalen; + return(calloc(1,datalen)); +} + +int64_t iguana_peerfree(struct iguana_info *coin,struct iguana_peer *addr,void *ptr,int32_t datalen) +{ + addr->freed += datalen; + free(ptr); + return(1); +} + +int64_t iguana_peerallocated(struct iguana_info *coin,struct iguana_peer *addr) +{ + return(addr->allocated - addr->freed); +} +#endif + +void iguana_dedicatedloop(struct iguana_info *coin,struct iguana_peer *addr) +{ + struct pollfd fds; uint8_t *buf,serialized[64]; struct iguana_bundlereq *req; + int32_t bufsize,flag,run,timeout = coin->polltimeout == 0 ? 10 : coin->polltimeout; +#ifdef IGUANA_PEERALLOC + int32_t i; int64_t remaining; struct OS_memspace *mem[sizeof(addr->SEROUT)/sizeof(*addr->SEROUT)]; + for (i=0; iSEROUT)/sizeof(*addr->SEROUT); i++) + { + mem[i] = mycalloc('s',1,sizeof(*mem[i])); + addr->SEROUT[i] = mem[i]; + mem[i]->totalsize = IGUANA_MAXPACKETSIZE; + mem[i]->ptr = mycalloc('P',1,mem[i]->totalsize); + mem[i]->used = 0; + strcpy(mem[i]->name,addr->ipaddr); + //mem[i]->threadsafe = 1; + iguana_memreset(mem[i]); + } +#endif + addr->addrind = (int32_t)(((long)addr - (long)&coin->peers.active[0]) / sizeof(*addr)); + printf("start dedicatedloop.%s addrind.%d\n",addr->ipaddr,addr->addrind); + addr->maxfilehash2 = IGUANA_MAXFILEITEMS; + bufsize = IGUANA_MAXPACKETSIZE; + buf = mycalloc('r',1,bufsize); + //printf("send version myservices.%llu\n",(long long)coin->myservices); + iguana_send_version(coin,addr,coin->myservices); + iguana_queue_send(coin,addr,serialized,"getaddr",0,0,0); + //printf("after send version\n"); + run = 0; + while ( addr->usock >= 0 && addr->dead == 0 && coin->peers.shuttingdown == 0 ) + { + if ( 1 && (req= queue_dequeue(&coin->cacheQ,0)) != 0 ) + { + if ( req->datalen != 0 ) + { + //printf("CACHE parse[%d] %s\n",req->recvlen,req->H.command); + iguana_parsebuf(coin,addr,&req->H,req->serialized,req->recvlen); + } + coin->cachefreed++; + myfree(req,req->allocsize); + continue; + } + flag = 0; + memset(&fds,0,sizeof(fds)); + fds.fd = addr->usock; + fds.events |= (POLLOUT | POLLIN); + if ( poll(&fds,1,timeout) > 0 && (fds.revents & POLLOUT) != 0 ) + flag += iguana_pollsendQ(coin,addr); + if ( flag == 0 ) + { + //memset(&fds,0,sizeof(fds)); + //fds.fd = addr->usock; + //fds.events |= POLLIN; + //if ( poll(&fds,1,timeout) > 0 ) + if ( (fds.revents & POLLIN) != 0 ) + flag += iguana_pollrecv(coin,addr,buf,bufsize); + if ( flag == 0 ) + { + if ( time(NULL) > addr->pendtime+30 ) + { + if ( addr->pendblocks > 0 ) + addr->pendblocks--; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + addr->pendtime = 0; + } + //memset(&fds,0,sizeof(fds)); + //fds.fd = addr->usock; + //fds.events |= POLLOUT; + //if ( poll(&fds,1,timeout) > 0 ) + if ( coin->active != 0 && (fds.revents & POLLOUT) != 0 ) + flag += iguana_pollQsPT(coin,addr); + } + if ( flag == 0 ) + { + if ( run++ > 10000 ) + { + //printf("sleep\n"); + sleep(1); + } + else if ( addr->rank != 1 ) + usleep(coin->polltimeout*2500 + (rand() % (coin->polltimeout*2500))); + else usleep(100 + coin->polltimeout*1000); + } + } + if ( flag != 0 ) + run = 0; + if ( coin->isRT != 0 && addr->rank > coin->MAXPEERS && (rand() % 100) == 0 ) + { + printf("isRT and low rank.%d ",addr->rank); + addr->dead = 1; + } + } + //if ( addr->fp != 0 ) + // fclose(addr->fp); + iguana_iAkill(coin,addr,addr->dead != 0); + printf("finish dedicatedloop.%s\n",addr->ipaddr); + myfree(buf,bufsize); + if ( addr->filehash2 != 0 ) + myfree(addr->filehash2,addr->maxfilehash2*sizeof(*addr->filehash2)); + iguana_mempurge(&addr->RAWMEM); + iguana_mempurge(&addr->TXDATA); + iguana_mempurge(&addr->HASHMEM); +#ifdef IGUANA_PEERALLOC + while ( (remaining= iguana_peerallocated(coin,addr)) > 0 ) + { + char str[65]; + printf("waiting for helperQ to flush peer mem %s\n",mbstr(str,remaining)); + sleep(5); + } + for (i=0; iSEROUT)/sizeof(*addr->SEROUT); i++) + { + if ( addr->SEROUT[i] != 0 ) + { + if ( addr->SEROUT[i]->ptr != 0 ) + myfree(addr->SEROUT[i]->ptr,IGUANA_MAXPACKETSIZE); + myfree(addr->SEROUT[i],sizeof(*addr->SEROUT[i])); + } + } +#endif + coin->peers.numconnected--; +} + +void iguana_peersloop(void *ptr) +{ +#ifndef IGUANA_DEDICATED_THREADS + struct pollfd fds[IGUANA_MAXPEERS]; struct iguana_info *coin = ptr; + struct iguana_peer *addr; uint8_t *bufs[IGUANA_MAXPEERS]; + int32_t i,j,n,r,nonz,flag,bufsizes[IGUANA_MAXPEERS],timeout=1; + memset(fds,0,sizeof(fds)); + memset(bufs,0,sizeof(bufs)); + memset(bufsizes,0,sizeof(bufsizes)); + while ( 1 ) + { + while ( coin->peers.shuttingdown != 0 ) + { + printf("peers shuttingdown\n"); + sleep(3); + } + flag = 0; + r = (rand() % coin->MAXPEERS); + for (j=n=nonz=0; jMAXPEERS; j++) + { + i = (j + r) % coin->MAXPEERS; + addr = &coin->peers.active[i]; + fds[i].fd = -1; + if ( addr->usock >= 0 && addr->dead == 0 && addr->ready != 0 && (addr->startrecv+addr->startsend) != 0 ) + { + fds[i].fd = addr->usock; + fds[i].events = (addr->startrecv != 0) * POLLIN | (addr->startsend != 0) * POLLOUT; + nonz++; + } + } + if ( nonz != 0 && poll(fds,coin->MAXPEERS,timeout) > 0 ) + { + for (j=0; jMAXPEERS; j++) + { + i = (j + r) % coin->MAXPEERS; + addr = &coin->peers.active[i]; + if ( addr->usock < 0 || addr->dead != 0 || addr->ready == 0 ) + continue; + if ( addr->startrecv == 0 && (fds[i].revents & POLLIN) != 0 && iguana_numthreads(1 << IGUANA_RECVTHREAD) < IGUANA_MAXRECVTHREADS ) + { + if ( bufs[i] == 0 ) + bufsizes[i] = IGUANA_MAXPACKETSIZE, bufs[i] = mycalloc('r',1,bufsizes[i]); + flag += iguana_pollrecv(coin,addr,bufs[i],bufsizes[i]); + } + if ( addr->startsend == 0 && (fds[i].revents & POLLOUT) != 0 && iguana_numthreads(1 << IGUANA_SENDTHREAD) < IGUANA_MAXSENDTHREADS ) + { + if ( iguana_pollsendQ(coin,addr) == 0 ) + flag += iguana_poll(coin,addr); + else flag++; + } + } + } + if ( flag == 0 ) + usleep(1000); + } +#endif +} diff --git a/iguana/iguana_pubkeys.c b/iguana/iguana_pubkeys.c new file mode 100755 index 000000000..e2537afa4 --- /dev/null +++ b/iguana/iguana_pubkeys.c @@ -0,0 +1,950 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" +#include +#include +#include +#include +#include +#include + +#define SCRIPT_OP_IF 0x63 +#define SCRIPT_OP_ELSE 0x67 +#define SCRIPT_OP_DUP 0x76 +#define SCRIPT_OP_ENDIF 0x68 +#define SCRIPT_OP_TRUE 0x51 +#define SCRIPT_OP_NOP 0x61 +#define SCRIPT_OP_2 0x52 +#define SCRIPT_OP_3 0x53 +#define SCRIPT_OP_EQUALVERIFY 0x88 +#define SCRIPT_OP_HASH160 0xa9 +#define SCRIPT_OP_EQUAL 0x87 +#define SCRIPT_OP_CHECKSIG 0xac +#define SCRIPT_OP_CHECKMULTISIG 0xae +#define SCRIPT_OP_CHECKMULTISIGVERIFY 0xaf + +struct bp_key { EC_KEY *k; }; +typedef struct cstring { char *str; size_t len,alloc; } cstring; + +static const char base58_chars[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + +static bool cstr_alloc_min_sz(cstring *s, size_t sz) +{ + char *new_s; uint32_t al_sz,shift = 3; + sz++; // NUL overhead + if ( s->alloc && (s->alloc >= sz) ) + return true; + while ( (al_sz = (1 << shift)) < sz ) + shift++; + if ( (new_s= mycalloc('C',1,al_sz)) != 0 ) + { + if ( s->str != 0 ) + { + memcpy(new_s,s->str,s->len); + myfree(s->str,s->alloc); + } + s->str = new_s; + s->alloc = al_sz; + s->str[s->len] = 0; + return true; + } + return false; +} + +cstring *cstr_new_sz(size_t sz) +{ + cstring *s = mycalloc('C',1,sizeof(cstring)); + if (!s) + return NULL; + if (!cstr_alloc_min_sz(s, sz)) + { + myfree(s,sizeof(cstring)); + return NULL; + } + return s; +} + +cstring *cstr_new_buf(const void *buf, size_t sz) +{ + cstring *s = cstr_new_sz(sz); + if (!s) + return NULL; + memcpy(s->str, buf, sz); + s->len = sz; + s->str[s->len] = 0; + return s; +} + +cstring *cstr_new(const char *init_str) +{ + if ( !init_str || !*init_str ) + return cstr_new_sz(0); + size_t slen = strlen(init_str); + return cstr_new_buf(init_str, slen); +} + +void cstr_free(cstring *s, bool free_buf) +{ + if (!s) + return; + if (free_buf) + myfree(s->str,s->alloc); + memset(s, 0, sizeof(*s)); + myfree(s,sizeof(*s)); +} + +bool cstr_erase(cstring *s,size_t pos,ssize_t len) +{ + if (pos == s->len && len == 0) + return true; + if (pos >= s->len) + return false; + size_t old_tail = s->len - pos; + if ((len >= 0) && (len > old_tail)) + return false; + memmove(&s->str[pos], &s->str[pos + len], old_tail - len); + s->len -= len; + s->str[s->len] = 0; + return true; +} + +bool cstr_resize(cstring *s, size_t new_sz) +{ + // no change + if (new_sz == s->len) + return true; + // truncate string + if (new_sz <= s->len) { + s->len = new_sz; + s->str[s->len] = 0; + return true; + } + // increase string size + if (!cstr_alloc_min_sz(s, new_sz)) + return false; + // contents of string tail undefined + s->len = new_sz; + s->str[s->len] = 0; + return true; +} + +bool cstr_append_buf(cstring *s, const void *buf, size_t sz) +{ + if (!cstr_alloc_min_sz(s, s->len + sz)) + return false; + memcpy(s->str + s->len, buf, sz); + s->len += sz; + s->str[s->len] = 0; + return true; +} +static inline bool cstr_append_c(cstring *s,char ch) { return cstr_append_buf(s,&ch,1); } + +void bu_reverse_copy(uint8_t *dst, const uint8_t *src, size_t len) +{ + uint32_t i; + for (i=0; i> 24) & 0xff; + vch2[1] = (data_len >> 16) & 0xff; + vch2[2] = (data_len >> 8) & 0xff; + vch2[3] = (data_len >> 0) & 0xff; + bu_reverse_copy(vch2 + 4, data, data_len); + BN_mpi2bn(vch2, vch2_len, vo); +} + +cstring *bn_getvch(const BIGNUM *v) +{ + cstring *s_be,*s_le; uint32_t le_sz,sz = BN_bn2mpi(v,NULL); + if ( sz <= 4 ) // get MPI format size + return cstr_new(NULL); + // store bignum as MPI + s_be = cstr_new_sz(sz); + cstr_resize(s_be, sz); + BN_bn2mpi(v,(uint8_t *) s_be->str); + // copy-swap MPI to little endian, sans 32-bit size prefix + le_sz = sz - 4; + s_le = cstr_new_sz(le_sz); + cstr_resize(s_le, le_sz); + bu_reverse_copy((uint8_t *)s_le->str,(uint8_t *)s_be->str + 4, le_sz); + cstr_free(s_be,true); + return s_le; +} + +cstring *base58_encode(const void *data_, size_t data_len) +{ + uint8_t swapbuf[data_len + 1]; uint32_t i,c; BN_CTX *ctx; BIGNUM bn58,bn0,bn,dv,rem; + cstring *rs,*rs_swap; const uint8_t *data = data_; + ctx = BN_CTX_new(); + BN_init(&bn58), BN_init(&bn0), BN_init(&bn), BN_init(&dv), BN_init(&rem); + BN_set_word(&bn58,58), BN_set_word(&bn0,0); + bu_reverse_copy(swapbuf,data,data_len); + swapbuf[data_len] = 0; + bn_setvch(&bn,swapbuf,sizeof(swapbuf)); + rs = cstr_new_sz(data_len * 138 / 100 + 1); + while ( BN_cmp(&bn,&bn0) > 0 ) + { + if ( !BN_div(&dv,&rem,&bn,&bn58,ctx) ) + { + cstr_free(rs,true); + rs = NULL; + goto out; + } + BN_copy(&bn, &dv); + c = (int32_t)BN_get_word(&rem); + cstr_append_c(rs,base58_chars[c]); + } + for (i=0; ilen); + cstr_resize(rs_swap, rs->len); + bu_reverse_copy((uint8_t *)rs_swap->str,(uint8_t *)rs->str,rs->len); + cstr_free(rs,true); + rs = rs_swap; +out: + BN_clear_free(&bn58); + BN_clear_free(&bn0); + BN_clear_free(&bn); + BN_clear_free(&dv); + BN_clear_free(&rem); + BN_CTX_free(ctx); + return rs; +} + +/*void bu_Hash(unsigned char *md256, const void *data, size_t data_len) +{ + unsigned char md1[32]; + SHA256(data,data_len,md1); + SHA256(md1,32,md256); +} + +void bu_Hash4(unsigned char *md32, const void *data, size_t data_len) +{ + unsigned char md256[32]; + bu_Hash(md256,data,data_len); + memcpy(md32,md256,4); +}*/ + +cstring *base58_encode_check(uint8_t addrtype,bool have_addrtype,const void *data,size_t data_len) +{ + uint8_t i,buf[64]; bits256 hash; cstring *s_enc;//,*s = cstr_new_sz(data_len + 1 + 4); + buf[0] = addrtype; + memcpy(buf+1,data,data_len); + hash = bits256_doublesha256(0,buf,(int32_t)data_len+1); + //bu_Hash4(md32,buf,(int32_t)data_len+1); + for (i=0; i<4; i++) + { + buf[data_len+i+1] = hash.bytes[31-i]; + //printf("(%02x %02x) ",hash.bytes[31-i],md32[i]); + } + //printf("hash4 cmp\n"); + s_enc = base58_encode(buf,data_len+5); + /*if ( 0 ) + { + if ( have_addrtype ) + cstr_append_c(s,addrtype); + cstr_append_buf(s,data,data_len); + hash = bits256_doublesha256(0,(uint8_t *)s->str,(int32_t)s->len); + cstr_append_buf(s,hash.bytes,4); + //bu_Hash4(md32, s->str, s->len); + //cstr_append_buf(s, md32, 4); + s_enc = base58_encode(s->str, s->len); + cstr_free(s,true); + }*/ + return s_enc; +} + +cstring *base58_decode(const char *s_in) +{ + uint32_t leading_zero,be_sz; const char *p,*p1; BIGNUM bn58,bn,bnChar; BN_CTX *ctx; cstring *tmp_be,*tmp,*ret = NULL; + ctx = BN_CTX_new(); + BN_init(&bn58), BN_init(&bn), BN_init(&bnChar); + BN_set_word(&bn58,58), BN_set_word(&bn,0); + while ( isspace((uint32_t)*s_in) ) + s_in++; + for (p=s_in; *p; p++) + { + p1 = strchr(base58_chars,*p); + if ( !p1 ) + { + while (isspace((uint32_t)*p)) + p++; + if ( *p != '\0' ) + goto out; + break; + } + BN_set_word(&bnChar,p1 - base58_chars); + if (!BN_mul(&bn, &bn, &bn58, ctx)) + goto out; + if (!BN_add(&bn, &bn, &bnChar)) + goto out; + } + tmp = bn_getvch(&bn); + if ( (tmp->len >= 2) && (tmp->str[tmp->len - 1] == 0) && ((uint8_t)tmp->str[tmp->len - 2] >= 0x80)) + cstr_resize(tmp, tmp->len - 1); + leading_zero = 0; + for (p=s_in; *p==base58_chars[0]; p++) + leading_zero++; + be_sz = (uint32_t)tmp->len + (uint32_t)leading_zero; + tmp_be = cstr_new_sz(be_sz); + cstr_resize(tmp_be, be_sz); + memset(tmp_be->str, 0, be_sz); + bu_reverse_copy((uint8_t *)tmp_be->str + leading_zero,(uint8_t *)tmp->str,tmp->len); + cstr_free(tmp,true); + ret = tmp_be; +out: + BN_clear_free(&bn58); + BN_clear_free(&bn); + BN_clear_free(&bnChar); + BN_CTX_free(ctx); + return ret; +} + +cstring *base58_decode_check(uint8_t *addrtype,const char *s_in) +{ + bits256 hash; cstring *s = base58_decode(s_in); + if ( s != 0 ) + { + if ( s->len >= 4 ) + { + // validate with trailing hash, then remove hash + hash = bits256_doublesha256(0,(uint8_t *)s->str,(int32_t)s->len - 4); + //bu_Hash4(md32,s->str,s->len - 4); + if ( memcmp(hash.bytes,&s->str[s->len - 4],4) == 0 ) + { + cstr_resize(s,s->len - 4); + if ( addrtype ) // if addrtype requested, remove from front of data string + { + *addrtype = (uint8_t)s->str[0]; + cstr_erase(s,0,1); + } + return(s); + } + } + cstr_free(s,true); + } + return(NULL); +} + +/* Generate a private key from just the secret parameter */ +static int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key) +{ + int ok = 0; + BN_CTX *ctx = NULL; + EC_POINT *pub_key = NULL; + + if (!eckey) return 0; + + const EC_GROUP *group = EC_KEY_get0_group(eckey); + + if ((ctx = BN_CTX_new()) == NULL) + goto err; + + pub_key = EC_POINT_new(group); + + if (pub_key == NULL) + goto err; + + if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) + goto err; + + EC_KEY_set_private_key(eckey,priv_key); + EC_KEY_set_public_key(eckey,pub_key); + + ok = 1; + +err: + + if (pub_key) + EC_POINT_free(pub_key); + if (ctx != NULL) + BN_CTX_free(ctx); + + return(ok); +} + +bool bp_key_init(struct bp_key *key) +{ + memset(key, 0, sizeof(*key)); + + key->k = EC_KEY_new_by_curve_name(NID_secp256k1); + if (!key->k) + return false; + + return true; +} + +void bp_key_free(struct bp_key *key) +{ + if (key->k) { + EC_KEY_free(key->k); + key->k = NULL; + } +} + +bool bp_key_generate(struct bp_key *key) +{ + if (!key->k) + return false; + + if (!EC_KEY_generate_key(key->k)) + return false; + if (!EC_KEY_check_key(key->k)) + return false; + + EC_KEY_set_conv_form(key->k, POINT_CONVERSION_COMPRESSED); + + return true; +} + +bool bp_privkey_set(struct bp_key *key, const void *privkey_, size_t pk_len) +{ + const unsigned char *privkey = privkey_; + if (!d2i_ECPrivateKey(&key->k, &privkey, pk_len)) + return false; + if (!EC_KEY_check_key(key->k)) + return false; + + EC_KEY_set_conv_form(key->k, POINT_CONVERSION_COMPRESSED); + + return true; +} + +bool bp_pubkey_set(struct bp_key *key, const void *pubkey_, size_t pk_len) +{ + const unsigned char *pubkey = pubkey_; + if (!o2i_ECPublicKey(&key->k, &pubkey, pk_len)) + return false; + if (pk_len == 33) + EC_KEY_set_conv_form(key->k, POINT_CONVERSION_COMPRESSED); + return true; +} + +bool bp_key_secret_set(struct bp_key *key, const void *privkey_, size_t pk_len) +{ + bp_key_free(key); + + if (!privkey_ || pk_len != 32) + return false; + + const unsigned char *privkey = privkey_; + BIGNUM *bn = BN_bin2bn(privkey, 32, BN_new()); + if (!bn) + return false; + + key->k = EC_KEY_new_by_curve_name(NID_secp256k1); + if (!key->k) + goto err_out; + + if (!EC_KEY_regenerate_key(key->k, bn)) + goto err_out; + if (!EC_KEY_check_key(key->k)) + return false; + + EC_KEY_set_conv_form(key->k, POINT_CONVERSION_COMPRESSED); + + BN_clear_free(bn); + return true; + +err_out: + bp_key_free(key); + BN_clear_free(bn); + return false; +} + +bool bp_privkey_get(const struct bp_key *key, void **privkey, size_t *pk_len) +{ + if (!EC_KEY_check_key(key->k)) + return false; + + size_t sz = i2d_ECPrivateKey(key->k, 0); + unsigned char *orig_mem, *mem = mycalloc('b',1,sz); + orig_mem = mem; + i2d_ECPrivateKey(key->k, &mem); + + *privkey = orig_mem; + *pk_len = sz; + + return true; +} + +bool bp_pubkey_get(const struct bp_key *key, void **pubkey, size_t *pk_len) +{ + if (!EC_KEY_check_key(key->k)) + return false; + + size_t sz = i2o_ECPublicKey(key->k, 0); + unsigned char *orig_mem, *mem = mycalloc('b',1,sz); + orig_mem = mem; + i2o_ECPublicKey(key->k, &mem); + + *pubkey = orig_mem; + *pk_len = sz; + + return true; +} + +bool bp_key_secret_get(void *p, size_t len, const struct bp_key *key) +{ + if (!p || len < 32 || !key) + return false; + + /* zero buffer */ + memset(p, 0, len); + + /* get bignum secret */ + const BIGNUM *bn = EC_KEY_get0_private_key(key->k); + if (!bn) + return false; + int nBytes = BN_num_bytes(bn); + + /* store secret at end of buffer */ + int n = BN_bn2bin(bn, p + (len - nBytes)); + if (n != nBytes) + return false; + + return true; +} + +bool bp_sign(const struct bp_key *key, const void *data, size_t data_len,void **sig_, size_t *sig_len_) +{ + size_t sig_sz = ECDSA_size(key->k); + void *sig = mycalloc('b',1, sig_sz); + unsigned int sig_sz_out = (int32_t)sig_sz; + + int src = ECDSA_sign(0, data, (int32_t)data_len, sig, &sig_sz_out, key->k); + if (src != 1) { + myfree(sig,sig_sz); + return false; + } + + *sig_ = sig; + *sig_len_ = sig_sz_out; + + return true; +} + +bool bp_verify(const struct bp_key *key, const void *data, size_t data_len,const void *sig_, size_t sig_len) +{ + const unsigned char *sig = sig_; + ECDSA_SIG *esig; + bool b = false; + + esig = ECDSA_SIG_new(); + if (!esig) + goto out; + + if (!d2i_ECDSA_SIG(&esig, &sig, sig_len)) + goto out_free; + + b = ECDSA_do_verify(data,(int32_t) data_len, esig, key->k) == 1; + +out_free: + ECDSA_SIG_free(esig); +out: + return b; +} + +int32_t btc_getpubkey(char pubkeystr[67],uint8_t pubkeybuf[33],struct bp_key *key) +{ + void *pubkey = 0; size_t len = 0; + bp_pubkey_get(key,&pubkey,&len); + //printf("btc_getpubkey len.%ld %p\n",len,pubkey); + if ( pubkey != 0 ) + { + if ( pubkeystr != 0 ) + { + if ( len < 34 ) + { + init_hexbytes_noT(pubkeystr,pubkey,(int32_t)len); + memcpy(pubkeybuf,pubkey,len); + } + else printf("btc_getpubkey error len.%d\n",(int32_t)len), len = -1; + } + } else len = -1; + return((int32_t)len); +} + +int32_t btc_convrmd160(char *coinaddr,uint8_t addrtype,uint8_t rmd160[20]) +{ + cstring *btc_addr; + if ( (btc_addr= base58_encode_check(addrtype,true,rmd160,20)) != 0 ) + { + strcpy(coinaddr,btc_addr->str); + cstr_free(btc_addr,true); + return(0); + } + return(-1); +} + +int32_t btc_coinaddr(char *coinaddr,uint8_t addrtype,char *pubkeystr) +{ + uint8_t rmd160[20]; char hashstr[41]; + calc_OP_HASH160(hashstr,rmd160,pubkeystr); + return(btc_convrmd160(coinaddr,addrtype,rmd160)); +} + +int32_t btc_convaddr(char *hexaddr,char *addr58) +{ + uint8_t addrtype; cstring *cstr; + if ( (cstr= base58_decode_check(&addrtype,(const char *)addr58)) != 0 ) + { + sprintf(hexaddr,"%02x",addrtype); + init_hexbytes_noT(hexaddr+2,(void *)cstr->str,cstr->len); + cstr_free(cstr,true); + return(0); + } + return(-1); +} + +int32_t btc_addr2univ(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr) +{ + char hexstr[512]; uint8_t hex[21]; + if ( btc_convaddr(hexstr,coinaddr) == 0 ) + { + decode_hex(hex,21,hexstr); + *addrtypep = hex[0]; + memcpy(rmd160,hex+1,20); + return(0); + } + return(-1); +} + +int32_t btc_priv2wip(char *wipstr,uint8_t privkey[32],uint8_t addrtype) +{ + uint8_t tmp[128]; char hexstr[67]; cstring *btc_addr; + memcpy(tmp,privkey,32); + tmp[32] = 1; + init_hexbytes_noT(hexstr,tmp,32); + if ( (btc_addr= base58_encode_check(addrtype,true,tmp,33)) != 0 ) + { + strcpy(wipstr,btc_addr->str); + cstr_free(btc_addr,true); + } + printf("-> (%s) -> wip.(%s) addrtype.%02x\n",hexstr,wipstr,addrtype); + return(0); +} + +int32_t btc_wip2priv(uint8_t privkey[32],char *wipstr) +{ + uint8_t addrtype; cstring *cstr; int32_t len = -1; + if ( (cstr= base58_decode_check(&addrtype,(const char *)wipstr)) != 0 ) + { + init_hexbytes_noT((void *)privkey,(void *)cstr->str,cstr->len); + if ( cstr->str[cstr->len-1] == 0x01 ) + cstr->len--; + memcpy(privkey,cstr->str,cstr->len); + len = (int32_t)cstr->len; + char tmp[138]; + btc_priv2wip(tmp,privkey,addrtype); + printf("addrtype.%02x wipstr.(%llx) len.%d\n",addrtype,*(long long *)privkey,len); + cstr_free(cstr,true); + } + return(len); +} + +int32_t btc_setprivkey(struct bp_key *key,char *privkeystr) +{ + uint8_t privkey[512]; int32_t len = btc_wip2priv(privkey,privkeystr); + if ( len < 0 || bp_key_init(key) == 0 || bp_key_secret_set(key,privkey,len) == 0 ) + { + printf("error setting privkey\n"); + return(-1); + } + return(0); +} + +void btc_freekey(void *key) +{ + bp_key_free(key); + myfree(key,sizeof(struct bp_key)); +} + +int32_t btc_priv2pub(uint8_t pubkey[33],uint8_t privkey[32]) +{ + size_t len; void *pub = 0; int32_t retval = -1; + struct bp_key *key = mycalloc('b',1,sizeof(*key)); + if ( key != 0 && bp_key_init(key) != 0 && bp_key_secret_set(key,privkey,32) != 0 ) + { + bp_pubkey_get(key,&pub,&len); + bp_key_free(key); + if ( len == 33 ) + memcpy(pubkey,pub,33); + if ( pub != 0 ) + myfree(pub,len); + return(retval); + } + if ( key != 0 ) + bp_key_free(key); + return(retval); +} + +int32_t btc_pub2rmd(uint8_t rmd160[20],uint8_t pubkey[33]) +{ + char pubkeystr[67],hashstr[41]; + init_hexbytes_noT(pubkeystr,pubkey,33); + calc_OP_HASH160(hashstr,rmd160,pubkeystr); + return(0); +} + +int32_t create_MofN(uint8_t addrtype,char *redeemScript,char *scriptPubKey,char *p2shaddr,char *pubkeys[],int32_t M,int32_t N) +{ + cstring *btc_addr; uint8_t pubkey[33],tmpbuf[24],hex[4096]; int32_t i,n = 0; + hex[n++] = 0x50 + M; + for (i=0; i>4) & 0xf); + redeemScript[i*2 + 1] = hexbyte(hex[i] & 0xf); + //fprintf(stderr,"%02x",hex[i]); + } + //fprintf(stderr," n.%d\n",n); + redeemScript[n*2] = 0; + calc_OP_HASH160(0,tmpbuf+2,redeemScript); + //printf("op160.(%s)\n",redeemScript); + tmpbuf[0] = SCRIPT_OP_HASH160; + tmpbuf[1] = 20; + tmpbuf[22] = SCRIPT_OP_EQUAL; + init_hexbytes_noT(scriptPubKey,tmpbuf,23); + p2shaddr[0] = 0; + if ( (btc_addr= base58_encode_check(addrtype,true,tmpbuf+2,20)) != 0 ) + { + if ( strlen(btc_addr->str) < 36 ) + strcpy(p2shaddr,btc_addr->str); + cstr_free(btc_addr,true); + } + return(n); +} + +int32_t btc_pub65toaddr(char *coinaddr,uint8_t addrtype,char pubkey[131],uint8_t *pk) +{ + int32_t retval = -1; char pubkeystr[67]; uint8_t *ptr; size_t len; + EC_KEY *key; + key = EC_KEY_new_by_curve_name(NID_secp256k1); + if ( key != 0 ) + { + if (!EC_KEY_generate_key(key)) + { + printf("generate error\n"); + return(-1); + } + if (!EC_KEY_check_key(key)) + { + printf("key check error0\n"); + return(-1); + } + pubkeystr[0] = 0; + const EC_GROUP *group = EC_KEY_get0_group(key); + EC_POINT *pkey = EC_POINT_new(group); + EC_POINT_hex2point(group,pubkey,pkey,NULL); + if (!EC_KEY_check_key(key)) + { + printf("key check error\n"); + return(-1); + } + retval = EC_KEY_set_public_key(key,pkey); + if (!EC_KEY_check_key(key)) + { + printf("key check error2\n"); + return(-1); + } + len = i2o_ECPublicKey(key,0); + ptr = mycalloc('b',1,len); + i2o_ECPublicKey(key,&ptr); + printf("btc_getpubkey len.%ld %p\n",(long)len,ptr); + EC_KEY_set_conv_form(key,POINT_CONVERSION_COMPRESSED); + EC_KEY_free(key); + } + return(retval); +} + +#define IGUANA_SCRIPT_NULL 0 +#define IGUANA_SCRIPT_76AC 1 +#define IGUANA_SCRIPT_7688AC 2 +#define IGUANA_SCRIPT_P2SH 3 +#define IGUANA_SCRIPT_OPRETURN 4 +#define IGUANA_SCRIPT_3of3 5 +#define IGUANA_SCRIPT_2of3 6 +#define IGUANA_SCRIPT_1of3 7 +#define IGUANA_SCRIPT_2of2 8 +#define IGUANA_SCRIPT_1of2 9 +#define IGUANA_SCRIPT_MSIG 10 +#define IGUANA_SCRIPT_DATA 11 +#define IGUANA_SCRIPT_STRANGE 15 + +int32_t iguana_calcrmd160(struct iguana_info *coin,uint8_t rmd160[20],uint8_t msigs160[16][20],int32_t *Mp,int32_t *nump,uint8_t *pk_script,int32_t pk_scriptlen,bits256 debugtxid) +{ + static uint8_t zero_rmd160[20]; + char hexstr[8192]; uint8_t sha256[32],*script,type; int32_t i,n,m,plen; + if ( nump != 0 ) + *nump = 0; + type = IGUANA_SCRIPT_STRANGE; + if ( pk_scriptlen == 0 ) + { + if ( zero_rmd160[0] == 0 ) + { + vcalc_sha256(0,sha256,pk_script,pk_scriptlen); // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 + calc_rmd160(0,zero_rmd160,sha256,sizeof(sha256)); // b472a266d0bd89c13706a4132ccfb16f7c3b9fcb + init_hexbytes_noT(hexstr,zero_rmd160,20); + char str[65]; printf("iguana_calcrmd160 zero len %s -> %s\n",bits256_str(str,*(bits256 *)sha256),hexstr); + } + memcpy(rmd160,zero_rmd160,sizeof(zero_rmd160)); + return(IGUANA_SCRIPT_NULL); + } + else if ( pk_script[0] == 0x6a ) + type = IGUANA_SCRIPT_OPRETURN; + else if ( pk_script[0] == 0x76 && pk_script[1] == 0xa9 && pk_script[pk_script[2]+3] == 0x88 && pk_script[pk_script[2]+4] == 0xac ) + { + vcalc_sha256(0,sha256,&pk_script[3],pk_script[2]); + calc_rmd160(0,rmd160,sha256,sizeof(sha256)); + if ( (plen= pk_script[2]+4) < pk_scriptlen ) + { + while ( plen < pk_scriptlen ) + if ( pk_script[plen++] != 0x61 ) // nop + return(IGUANA_SCRIPT_STRANGE); + } + return(IGUANA_SCRIPT_7688AC); + } + else if ( pk_script[0] > 0 && pk_script[0] < 76 && pk_script[pk_scriptlen-1] == 0xac && pk_script[0] == pk_scriptlen-2 ) + { + //printf("minus2\n"); + vcalc_sha256(0,sha256,&pk_script[1],pk_script[0]); + calc_rmd160(0,rmd160,sha256,sizeof(sha256)); + return(IGUANA_SCRIPT_76AC); + } + else if ( pk_script[0] == 0xa9 && pk_script[1] == 0x14 && pk_scriptlen == 23 && pk_script[22] == 0x87 ) + { + memcpy(rmd160,pk_script+2,20); + return(IGUANA_SCRIPT_P2SH); + } + else if ( pk_scriptlen > 34 && pk_script[pk_scriptlen-1] == 0xae && (n= pk_script[pk_scriptlen-2]) >= 0x51 && n <= 0x60 && (m= pk_script[0]) >= 0x51 && m <= n ) // m of n multisig + { + m -= 0x50, n -= 0x50; + if ( msigs160 != 0 && nump != 0 && *Mp != 0 ) + { + script = pk_script+1; + for (i=0; ichain->pubval; + else addrtype = coin->chain->p2shval; + btc_convrmd160(coinaddr,addrtype,p->rmd160); + switch ( type ) + { + case IGUANA_SCRIPT_NULL: strcpy(asmstr,"coinbase"); break; + case IGUANA_SCRIPT_76AC: + sprintf(asmstr,"OP_DUP %s OP_CHECKSIG",coinaddr); + break; + case IGUANA_SCRIPT_7688AC: + sprintf(asmstr,"OP_DUP %s OP_EQUALVERIFY OP_CHECKSIG",coinaddr); + break; + case IGUANA_SCRIPT_P2SH: + script[0] = 0xa9, script[1] = 0x14; + memcpy(&script[2],p->rmd160,20); + script[22] = 0x87; + sprintf(asmstr,"OP_HASH160 %s OP_EQUAL",coinaddr); + scriptlen = 23; + break; + case IGUANA_SCRIPT_OPRETURN: strcpy(asmstr,"OP_RETURN"); break; + case IGUANA_SCRIPT_3of3: strcpy(asmstr,"3 of 3 MSIG"); break; + case IGUANA_SCRIPT_2of3: strcpy(asmstr,"2 of 3 MSIG"); break; + case IGUANA_SCRIPT_1of3: strcpy(asmstr,"1 of 3 MSIG"); break; + case IGUANA_SCRIPT_2of2: strcpy(asmstr,"2 of 2 MSIG"); break; + case IGUANA_SCRIPT_1of2: strcpy(asmstr,"1 of 2 MSIG"); break; + case IGUANA_SCRIPT_MSIG: strcpy(asmstr,"NON-STANDARD MSIG"); break; + case IGUANA_SCRIPT_DATA: strcpy(asmstr,"DATA ONLY"); break; + case IGUANA_SCRIPT_STRANGE: strcpy(asmstr,"STRANGE SCRIPT"); break; + default: printf("unexpected script type\n"); break; + } + return(0); +} diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c new file mode 100755 index 000000000..4760cedb1 --- /dev/null +++ b/iguana/iguana_ramchain.c @@ -0,0 +1,1983 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + + +#define uthash_malloc(size) ((ramchain->hashmem == 0) ? mycalloc('u',1,size) : iguana_memalloc(ramchain->hashmem,size,1)) +#define uthash_free(ptr,size) ((ramchain->hashmem == 0) ? myfree(ptr,size) : 0) + +//#define HASH_BLOOM 16 +//#define HASH_INITIAL_NUM_BUCKETS_LOG2 5 + +#include "iguana777.h" +//void iguana_stub(void *ptr,int size) { }//printf("uthash_free ptr.%p %d\n",ptr,size); } + +#define iguana_hashfind(ramchain,selector,key) iguana_hashsetPT(ramchain,selector,key,0) + +struct iguana_kvitem *iguana_hashsetPT(struct iguana_ramchain *ramchain,int32_t selector,void *key,uint32_t itemind) +{ + struct iguana_kvitem *tmp,*ptr = 0; int32_t allocsize,keylen; char str[65]; + allocsize = (int32_t)(sizeof(*ptr)); + if ( selector == 'T' ) + { + keylen = sizeof(bits256); + HASH_FIND(hh,ramchain->txids,key,keylen,ptr); + } + else if ( selector == 'P' ) + { + keylen = 20; + HASH_FIND(hh,ramchain->pkhashes,key,keylen,ptr); + } + else return(0); + init_hexbytes_noT(str,key,keylen); + if ( ptr == 0 && itemind != 0 ) + { + if ( ramchain->hashmem != 0 ) + ptr = iguana_memalloc(ramchain->hashmem,allocsize,1); + else ptr = mycalloc('e',1,allocsize); + if ( ptr == 0 ) + printf("fatal alloc error in hashset\n"), exit(-1); + if ( 0 && ramchain->expanded && selector == 'T' ) + printf("hashmem.%p selector.%c added.(%s) itemind.%x ptr.%p\n",ramchain->hashmem,selector,str,itemind,ptr); + ptr->hh.itemind = itemind; + if ( selector == 'T' ) + HASH_ADD_KEYPTR(hh,ramchain->txids,key,keylen,ptr); + else HASH_ADD_KEYPTR(hh,ramchain->pkhashes,key,keylen,ptr); + if ( selector == 'T' ) + HASH_FIND(hh,ramchain->txids,key,keylen,tmp); + else HASH_FIND(hh,ramchain->pkhashes,key,keylen,tmp); + //if ( strcmp(str,"0000000000000000000000000000000000000000000000000000000000000000") == 0 ) + // printf("added null txid?\n"), getchar(); + if ( 0 && ramchain->expanded && selector == 'T' ) + printf("selector.%c added.(%s) itemind.%x ptr.%p tmp.%p\n",selector,str,itemind,ptr,tmp); + if ( itemind == 0 ) + printf("negative itemind\n"), exit(-1); + if ( tmp != ptr ) + { + printf("(%s) hashmem.%p selector.%c %s search error %p != %p itemind.%x\n",str,ramchain->hashmem,selector,str,ptr,tmp,itemind), exit(-1); + } + } + return(ptr); +} + +uint32_t iguana_sparseadd(uint8_t *bits,uint32_t ind,int32_t width,uint32_t tablesize,uint8_t *key,int32_t keylen,uint32_t setind,void *refdata,int32_t refsize) +{ + static long sparsesearches,sparseiters,sparsehits,sparsemax; + static uint8_t masks[8] = { 1, 2, 4, 8, 16, 32, 64, 128 }; + int32_t i,j,x,n,modval; int64_t bitoffset; uint8_t *ptr; + if ( tablesize == 0 ) + { + printf("iguana_sparseadd tablesize zero illegal\n"); + return(0); + } + if ( 0 && setind == 0 ) + { + char str[65]; + for (i=n=0; i> 3]; + modval = (bitoffset & 7); + for (x=j=0; j= 8 ) + ptr++, modval = 0; + x <<= 1; + x |= (*ptr & masks[modval]) >> modval; + } + if ( x != 0 ) + printf("%s ",bits256_str(str,*(bits256 *)(refdata + x*refsize))), n++; + } + printf("tableentries.%d\n",n); + } + bitoffset = (ind * width); + sparsesearches++; + if ( 0 && setind == 0 ) + printf("tablesize.%d width.%d bitoffset.%d\n",tablesize,width,(int32_t)bitoffset); + for (i=0; i= tablesize ) + { + ind = 0; + bitoffset = 0; + } + ptr = &bits[bitoffset >> 3]; + modval = (bitoffset & 7); + if ( 0 && setind == 0 ) + printf("tablesize.%d width.%d bitoffset.%d modval.%d i.%d\n",tablesize,width,(int32_t)bitoffset,modval,i); + for (x=j=0; j= 8 ) + ptr++, modval = 0; + x <<= 1; + x |= (*ptr & masks[modval]) >> modval; + } + if ( 0 && setind == 0 ) + printf("x.%d\n",x); + if ( x == 0 ) + { + if ( (x= setind) == 0 ) + return(0); + ptr = &bits[(bitoffset+width-1) >> 3]; + modval = ((bitoffset+width-1) & 7); + for (j=0; j>=1,modval--) + { + if ( modval < 0 ) + ptr--, modval = 7; + if ( (x & 1) != 0 ) + *ptr |= masks[modval]; + } + if ( 0 ) + { + for (x=j=0; j sparsemax ) + sparsemax = i; + return(setind); + } + else if ( memcmp((void *)((long)refdata + x*refsize),key,keylen) == 0 ) + { + if ( setind == 0 ) + sparsehits++; + else if ( setind != x ) + printf("sparseadd index collision setind.%d != x.%d\n",setind,x); + return(x); + } + } + return(0); +} + +uint32_t iguana_sparseaddtx(uint8_t *bits,int32_t width,uint32_t tablesize,bits256 txid,struct iguana_txid *T,uint32_t txidind) +{ + uint32_t ind; + ind = (txid.ulongs[0] ^ txid.ulongs[1] ^ txid.ulongs[2] ^ txid.ulongs[3]) % tablesize; + return(iguana_sparseadd(bits,ind,width,tablesize,txid.bytes,sizeof(txid),txidind,T,sizeof(*T))); +} + +uint32_t iguana_sparseaddpk(uint8_t *bits,int32_t width,uint32_t tablesize,uint8_t rmd160[20],struct iguana_pkhash *P,uint32_t pkind) +{ + uint32_t ind,key2; uint64_t key0,key1; + memcpy(&key0,rmd160,sizeof(key0)); + memcpy(&key1,&rmd160[sizeof(key0)],sizeof(key1)); + memcpy(&key2,&rmd160[sizeof(key0) + sizeof(key1)],sizeof(key2)); + ind = (key0 ^ key1 ^ key2) % tablesize; + return(iguana_sparseadd(bits,ind,width,tablesize,rmd160,20,pkind,P,sizeof(*P))); +} + +void iguana_blocksetcounters(struct iguana_info *coin,struct iguana_block *block,struct iguana_ramchain * ramchain) +{ + block->RO.firsttxidind = ramchain->H.txidind; + block->RO.firstvout = ramchain->H.unspentind; + block->RO.firstvin = ramchain->H.spendind; + block->RO.firstpkind = ramchain->pkind; + block->RO.firstexternalind = ramchain->externalind; +} + +struct iguana_txid *iguana_txidfind(struct iguana_info *coin,int32_t *heightp,struct iguana_txid *tx,bits256 txid) +{ + uint8_t *TXbits; struct iguana_txid *T; uint32_t txidind,i,j; + struct iguana_bundle *bp; struct iguana_ramchain *ramchain; struct iguana_block *block; + *heightp = -1; + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish > coin->startutc ) + { + ramchain = &bp->ramchain; + if ( ramchain->H.data != 0 ) + { + TXbits = (void *)((long)ramchain->H.data + ramchain->H.data->TXoffset); + T = (void *)((long)ramchain->H.data + ramchain->H.data->Toffset); + //printf("search bp.%p TXbits.%p T.%p %d %d\n",bp,TXbits,T,(int32_t)ramchain->H.data->TXoffset,(int32_t)ramchain->H.data->Toffset); + if ( (txidind= iguana_sparseaddtx(TXbits,ramchain->H.data->txsparsebits,ramchain->H.data->numtxsparse,txid,T,0)) > 0 ) + { + //printf("found txidind.%d\n",txidind); + for (j=0; jn; j++) + if ( (block= bp->blocks[j]) != 0 && txidind >= block->RO.firsttxidind && txidind < block->RO.firsttxidind+block->RO.txn_count ) + break; + *heightp = bp->bundleheight + j; + //printf("found height.%d\n",*heightp); + *tx = T[txidind]; + return(tx); + } + } + } + } + return(0); +} + +struct iguana_pkhash *iguana_pkhashfind(struct iguana_info *coin,struct iguana_pkhash *p,uint8_t rmd160[20]) +{ + uint8_t *PKbits; struct iguana_pkhash *P; uint32_t pkind,i; struct iguana_bundle *bp; struct iguana_ramchain *ramchain; + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + ramchain = &bp->ramchain; + PKbits = (void *)((long)ramchain->H.data + ramchain->H.data->PKoffset); + P = (void *)((long)ramchain->H.data + ramchain->H.data->Poffset); + if ( (pkind= iguana_sparseaddpk(PKbits,ramchain->H.data->pksparsebits,ramchain->H.data->numpksparse,rmd160,P,0)) > 0 ) + { + *p = P[pkind]; + return(p); + } + } + } + return(0); +} + +int32_t iguana_peerfname(struct iguana_info *coin,int32_t *hdrsip,char *dirname,char *fname,uint32_t ipbits,bits256 hash2,bits256 prevhash2,int32_t numblocks) +{ + struct iguana_bundle *bp = 0; int32_t bundlei = -2; char str[65]; + *hdrsip = -1; + //if ( ipbits == 0 ) + // printf("illegal ipbits.%d\n",ipbits), getchar(); + if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,hash2)) == 0 ) + { + if ( bits256_nonz(prevhash2) == 0 || (bp= iguana_bundlefind(coin,&bp,&bundlei,prevhash2)) == 0 || bundlei >= coin->chain->bundlesize-1 ) + return(-2); + else bundlei++; + } + hash2 = bp->hashes[0], *hdrsip = bp->hdrsi; + if ( numblocks == 1 ) + sprintf(fname,"%s/%s/%s.%u",dirname,coin->symbol,bits256_str(str,hash2),ipbits!=0?ipbits:*hdrsip); + else sprintf(fname,"%s/%s/%s_%d.%u",dirname,coin->symbol,bits256_str(str,hash2),numblocks,ipbits!=0?ipbits:*hdrsip); + OS_compatible_path(fname); + return(bundlei); +} + +int32_t iguana_peerfile_exists(struct iguana_info *coin,struct iguana_peer *addr,char *dirname,char *fname,bits256 hash2,bits256 prevhash2,int32_t numblocks) +{ + FILE *fp; int32_t bundlei,hdrsi; + if ( (bundlei= iguana_peerfname(coin,&hdrsi,dirname,fname,addr!=0?addr->ipbits:0,hash2,prevhash2,numblocks)) >= 0 ) + { + OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb")) == 0 ) + bundlei = -1; + else fclose(fp); + } + return(bundlei); +} + +#define RAMCHAIN_FUNC struct iguana_ramchain *ramchain,struct iguana_blockRO *B,struct iguana_txid *T,struct iguana_unspent20 *U,struct iguana_spend256 *S,struct iguana_pkhash *P,struct iguana_account *A,bits256 *X,struct iguana_unspent *Ux,struct iguana_spend *Sx,uint8_t *TXbits,uint8_t *PKbits +#define RAMCHAIN_PTRPS struct iguana_ramchain *ramchain,struct iguana_blockRO **B,struct iguana_txid **T,struct iguana_unspent20 **U,struct iguana_spend256 **S,struct iguana_pkhash **P,struct iguana_account **A,bits256 **X,struct iguana_unspent **Ux,struct iguana_spend **Sx,uint8_t **TXbits,uint8_t **PKbits + +#define _RAMCHAIN_ARG B,T,U,S,P,A,X,Ux,Sx,TXbits,PKbits +#define RAMCHAIN_ARG ramchain,_RAMCHAIN_ARG +#define MAPCHAIN_ARG mapchain,_RAMCHAIN_ARG +#define RAMCHAIN_PTRS ramchain,&B,&T,&U,&S,&P,&A,&X,&Ux,&Sx,&TXbits,&PKbits +#define RAMCHAIN_DECLARE struct iguana_blockRO *B; struct iguana_txid *T; struct iguana_unspent20 *U; struct iguana_spend256 *S; struct iguana_pkhash *P; struct iguana_account *A; bits256 *X; struct iguana_unspent *Ux; struct iguana_spend *Sx; uint8_t *TXbits,*PKbits; + +#define RAMCHAIN_DESTARG dest,destB,destT,destU,destS,destP,destA,destX,destUx,destSx,destTXbits,destPKbits +#define RAMCHAIN_DESTPTRS dest,&destB,&destT,&destU,&destS,&destP,&destA,&destX,&destUx,&destSx,&destTXbits,&destPKbits +#define RAMCHAIN_DESTDECLARE struct iguana_blockRO *destB; struct iguana_txid *destT; struct iguana_unspent20 *destU; struct iguana_spend256 *destS; struct iguana_pkhash *destP; struct iguana_account *destA; bits256 *destX; struct iguana_unspent *destUx; struct iguana_spend *destSx; uint8_t *destTXbits,*destPKbits; + +uint32_t iguana_ramchain_addtxid(struct iguana_info *coin,RAMCHAIN_FUNC,bits256 txid,int32_t numvouts,int32_t numvins,uint32_t locktime,uint32_t version,uint32_t timestamp) +{ + uint32_t txidind; struct iguana_txid *t; struct iguana_kvitem *ptr; + txidind = ramchain->H.txidind; + t = &T[txidind]; + if ( ramchain->H.ROflag != 0 ) + { + if ( t->txidind != txidind || memcmp(t->txid.bytes,txid.bytes,sizeof(bits256)) != 0 || t->numvouts != numvouts || t->numvins != numvins || t->firstvout != ramchain->H.unspentind || t->firstvin != ramchain->H.spendind || t->locktime != locktime || t->version != version || t->timestamp != timestamp ) + { + printf("iguana_ramchain_addtxid: addtxid mismatch (%d %d %d %d %d) vs. (%d %d %d %d %d)\n",t->txidind,t->numvouts,t->numvins,t->firstvout,t->firstvin,txidind,numvouts,numvins,ramchain->H.unspentind,ramchain->H.spendind); + exit(-1); + return(0); + } + } + else + { + //if ( ramchain->expanded != 0 ) + // printf("T.%p txidind.%d numvouts.%d numvins.%d\n",T,txidind,numvouts,numvins); + t->txidind = txidind, t->txid = txid, t->numvouts = numvouts, t->numvins = numvins; + t->firstvout = ramchain->H.unspentind, t->firstvin = ramchain->H.spendind; + t->locktime = locktime, t->version = version, t->timestamp = timestamp; + if ( ramchain->expanded != 0 ) + iguana_sparseaddtx(TXbits,ramchain->H.data->txsparsebits,ramchain->H.data->numtxsparse,txid,T,txidind); + //if ( txidind <= 2 ) + // printf("%p TXID.[%d] firstvout.%d/%d firstvin.%d/%d\n",t,txidind,ramchain->unspentind,numvouts,ramchain->spendind,numvins); + } + if ( ramchain->expanded != 0 ) + { + if ( (ptr= iguana_hashsetPT(ramchain,'T',t->txid.bytes,txidind)) == 0 ) + { + printf("iguana_ramchain_addtxid error adding txidind\n"); + return(0); + } + } + return(txidind); +} + +uint32_t iguana_ramchain_addpkhash(struct iguana_info *coin,RAMCHAIN_FUNC,uint8_t rmd160[20],int32_t flags,uint32_t unspentind) +{ + struct iguana_kvitem *ptr; uint32_t pkind = 0; + if ( ramchain->expanded != 0 && (ptr= iguana_hashfind(ramchain,'P',rmd160)) == 0 ) + { + pkind = ramchain->pkind++; + if ( ramchain->H.ROflag != 0 ) + { + if ( P[pkind].flags != flags || P[pkind].firstunspentind != unspentind || P[pkind].pkind != pkind ) + { + printf("iguana_ramchain_addpkhash pkind.%d vs %d error mismatched flags.(%x %x) firstunspentind.%x vs %x\n",pkind,P[pkind].pkind,P[pkind].flags,flags,P[pkind].firstunspentind,unspentind); + exit(-1); + return(0); + } + if ( memcmp(P[pkind].rmd160,rmd160,sizeof(P[pkind].rmd160)) != 0 ) + { + printf("iguana_ramchain_addpkhash error mismatched rmd160\n"); + return(0); + } + } + else + { + P[pkind].flags = flags; + P[pkind].pkind = pkind; + P[pkind].firstunspentind = unspentind; + //printf("%p P[%d] <- firstunspent.%d\n",&P[pkind],pkind,unspentind); + memcpy(P[pkind].rmd160,rmd160,sizeof(P[pkind].rmd160)); + if ( ramchain->expanded != 0 ) + iguana_sparseaddpk(PKbits,ramchain->H.data->pksparsebits,ramchain->H.data->numpksparse,rmd160,P,pkind); + } + if ( (ptr= iguana_hashsetPT(ramchain,'P',&P[pkind],pkind)) == 0 ) + { + printf("iguana_ramchain_addpkhash error adding pkhash\n"); + return(0); + } + } + return(pkind); +} + +uint32_t iguana_ramchain_addunspent20(struct iguana_info *coin,RAMCHAIN_FUNC,uint64_t value,uint8_t *script,int32_t scriptlen,bits256 txid,int32_t vout,uint8_t type) +{ + //struct iguana_unspent { uint64_t value; uint32_t txidind,pkind,prevunspentind; } __attribute__((packed)); + uint8_t rmd160[20],msigs160[16][20]; int32_t M,N; uint32_t unspentind; struct iguana_unspent20 *u; + unspentind = ramchain->H.unspentind++; + u = &U[unspentind]; + if ( scriptlen == -20 ) + memcpy(rmd160,script,20); + else type = iguana_calcrmd160(coin,rmd160,msigs160,&M,&N,script,scriptlen,txid); + if ( ramchain->H.ROflag != 0 ) + { + //printf("%p U[%d] txidind.%d pkind.%d\n",u,unspentind,ramchain->txidind,pkind); + if ( u->txidind != ramchain->H.txidind || u->value != value || memcmp(u->rmd160,rmd160,sizeof(rmd160)) != 0 ) + { + printf("iguana_ramchain_addunspent20: mismatched values.(%.8f %d) vs (%.8f %d)\n",dstr(u->value),u->txidind,dstr(value),ramchain->H.txidind); + return(0); + } + } + else + { + u->value = value; + u->type = type; + u->txidind = ramchain->H.txidind; + memcpy(u->rmd160,rmd160,sizeof(rmd160)); + } + return(unspentind); +} + +uint32_t iguana_ramchain_addunspent(struct iguana_info *coin,RAMCHAIN_FUNC,uint64_t value,uint16_t hdrsi,uint8_t *rmd160,uint16_t vout,uint8_t type) +{ + uint32_t unspentind; struct iguana_unspent *u; struct iguana_kvitem *ptr; int32_t pkind; + unspentind = ramchain->H.unspentind++; + u = &Ux[unspentind]; + if ( (ptr= iguana_hashfind(ramchain,'P',rmd160)) == 0 ) + pkind = iguana_ramchain_addpkhash(coin,RAMCHAIN_ARG,rmd160,0,unspentind); + else pkind = ptr->hh.itemind; + if ( pkind == 0 ) + return(0); + if ( ramchain->H.ROflag != 0 ) + { + //printf("%p U[%d] txidind.%d pkind.%d\n",u,unspentind,ramchain->txidind,pkind); + if ( u->value != value || u->pkind != pkind || u->value != value || u->txidind != ramchain->H.txidind || (pkind != 0 && u->prevunspentind != A[pkind].lastunspentind) || u->vout != vout || u->hdrsi != hdrsi ) + { + printf("iguana_ramchain_addunspent: mismatched values.(%d %.8f %d %d %d %d) vs (%d %.8f %d %d %d %d)\n",u->pkind,dstr(u->value),u->txidind,u->prevunspentind,u->vout,u->hdrsi,pkind,dstr(value),ramchain->H.txidind,A[pkind].lastunspentind,vout,hdrsi); + exit(-1); + return(0); + } + } + else + { + u->value = value; + u->type = type; + u->vout = vout, u->hdrsi = hdrsi; + u->txidind = ramchain->H.txidind, u->pkind = pkind; + u->prevunspentind = A[pkind].lastunspentind; + } + //printf("%p A[%d] last <- U%d\n",&A[pkind],pkind,unspentind); + A[pkind].balance += value; + A[pkind].lastunspentind = unspentind; + return(unspentind); +} + +uint32_t iguana_ramchain_addspend256(struct iguana_info *coin,RAMCHAIN_FUNC,bits256 prev_hash,int32_t prev_vout,uint8_t *script,int32_t scriptlen,uint32_t sequence)//,int32_t hdrsi,int32_t bundlei) +{ + struct iguana_spend256 *s; uint32_t spendind; + spendind = ramchain->H.spendind++; + s = &S[spendind]; + if ( ramchain->H.ROflag != 0 ) + { + if ( (s->diffsequence == 0 && sequence != 0xffffffff) || (s->diffsequence != 0 && sequence == 0xffffffff) || memcmp(s->prevhash2.bytes,prev_hash.bytes,sizeof(bits256)) != 0 || s->prevout != prev_vout ) //|| s->hdrsi != hdrsi + { + char str[65],str2[65]; printf("check addspend.%d v %d RO value mismatch diffseq.%d seq.%x prev_vout(%d vs %d) %s vs %s\n",spendind,s->spendind,s->diffsequence,sequence,s->prevout,prev_vout,bits256_str(str,s->prevhash2),bits256_str(str2,prev_hash)); + //printf("check addspend.%d vs %d RO value mismatch (%d %d:%d) vs (%d %d:%d)\n",spendind,s->spendind,s->prevout,s->hdrsi,s->bundlei,prev_vout,hdrsi,bundlei); + exit(-1); + return(0); + } + //printf(" READ.%p spendind.%d vs %d prevout.%d hdrsi.%d:%d\n",s,spendind,s->spendind,s->prevout,s->hdrsi,s->bundlei); + } + else + { + if ( sequence != 0xffffffff ) + s->diffsequence = 1; + s->prevhash2 = prev_hash, s->prevout = prev_vout; + //s->hdrsi = hdrsi, s->bundlei = bundlei; + s->spendind = spendind; + //char str[65]; printf("W.%p s.%d vout.%d/%d %d:%d %s\n",s,spendind,s->prevout,prev_vout,s->hdrsi,s->bundlei,bits256_str(str,prev_hash)); + } + return(spendind); +} + +int32_t iguana_ramchain_spendtxid(struct iguana_info *coin,bits256 *txidp,struct iguana_txid *T,int32_t numtxids,bits256 *X,int32_t numexternaltxids,struct iguana_spend *s) +{ + uint32_t ind,external; + memset(txidp,0,sizeof(*txidp)); + //printf("s.%p ramchaintxid vout.%x spendtxidind.%d numexternals.%d isext.%d numspendinds.%d\n",s,s->vout,s->spendtxidind,ramchain->numexternaltxids,s->external,ramchain->numspends); + if ( s->prevout < 0 ) + return(-1); + ind = s->spendtxidind; + external = (ind >> 31) & 1; + ind &= ~(1 << 31); + if ( s->external != 0 && s->external == external && ind < numexternaltxids ) + { + //printf("ind.%d externalind.%d X[%d]\n",ind,ramchain->externalind,ramchain->H.data->numexternaltxids); + *txidp = X[ind]; + return(s->prevout); + } + else if ( s->external == 0 && s->external == external && ind < numtxids ) + { + *txidp = T[ind].txid; + return(s->prevout); + } + return(-2); +} + +int32_t iguana_ramchain_txid(struct iguana_info *coin,RAMCHAIN_FUNC,bits256 *txidp,struct iguana_spend *s) +{ + uint32_t ind,external; + memset(txidp,0,sizeof(*txidp)); + //printf("s.%p ramchaintxid vout.%x spendtxidind.%d numexternals.%d isext.%d numspendinds.%d\n",s,s->vout,s->spendtxidind,ramchain->numexternaltxids,s->external,ramchain->numspends); + if ( s->prevout < 0 ) + return(-1); + ind = s->spendtxidind; + external = (ind >> 31) & 1; + ind &= ~(1 << 31); + if ( s->external != 0 && s->external == external && ind < ramchain->H.data->numexternaltxids ) + { + //printf("ind.%d externalind.%d X[%d]\n",ind,ramchain->externalind,ramchain->H.data->numexternaltxids); + *txidp = X[ind]; + return(s->prevout); + } + else if ( s->external == 0 && s->external == external && ind < ramchain->H.txidind ) + { + *txidp = T[ind].txid; + return(s->prevout); + } + return(-2); +} + +uint32_t iguana_ramchain_addspend(struct iguana_info *coin,RAMCHAIN_FUNC,bits256 prev_hash,int32_t prev_vout,uint32_t sequence)//,int32_t hdrsi,int32_t bundlei) +{ + struct iguana_spend *s; struct iguana_kvitem *ptr; bits256 txid; + uint32_t spendind,unspentind,txidind,pkind,external; uint64_t value = 0; + spendind = ramchain->H.spendind++; + s = &Sx[spendind]; + pkind = unspentind = 0; + if ( (ptr= iguana_hashfind(ramchain,'T',prev_hash.bytes)) == 0 ) + { + external = 1; + txidind = ramchain->externalind++; + if ( 0 && ramchain->expanded != 0 ) + { char str[65]; printf("%p X[%d] <- %s\n",X,txidind,bits256_str(str,prev_hash)); } + if ( ramchain->H.ROflag != 0 ) + { + if ( memcmp(X[txidind].bytes,prev_hash.bytes,sizeof(prev_hash)) != 0 ) + { + char str[65],str2[65]; printf("iguana_ramchain_addspend X[%d] of %d cmperror %s vs %s\n",txidind,ramchain->H.data->numexternaltxids,bits256_str(str,X[txidind]),bits256_str(str2,prev_hash)); + return(0); + } + } else X[txidind] = prev_hash; + if ( (ptr= iguana_hashsetPT(ramchain,'T',&X[txidind].bytes,txidind | (1 << 31))) == 0 ) + { + printf("iguana_ramchain_addspend error adding external\n"); + return(0); + } + txidind |= (1 << 31); + } else txidind = ptr->hh.itemind; + if ( (external= ((txidind >> 31) & 1)) == 0 ) + { + if ( txidind > 0 && txidind < ramchain->H.data->numtxids ) + { + if ( (unspentind= T[txidind].firstvout + prev_vout) > 0 && unspentind < ramchain->H.data->numunspents ) + { + value = Ux[unspentind].value; + if ( (pkind= Ux[unspentind].pkind) == 0 || pkind >= ramchain->H.data->numpkinds ) + { + printf("spendind.%d -> unspendind.%d %.8f -> pkind.0x%x\n",spendind,unspentind,dstr(value),pkind); + return(0); + } + } else printf("addspend illegal unspentind.%d vs %d\n",unspentind,ramchain->H.data->numunspents); + } else printf("addspend illegal txidind.%d vs %d\n",txidind,ramchain->H.data->numtxids), exit(-1); + } + if ( ramchain->H.ROflag != 0 ) + { + iguana_ramchain_txid(coin,RAMCHAIN_ARG,&txid,s); + if ( (s->diffsequence == 0 && sequence != 0xffffffff) || (s->diffsequence != 0 && sequence == 0xffffffff) || memcmp(txid.bytes,prev_hash.bytes,sizeof(bits256)) != 0 || s->prevout != prev_vout )// || s->hdrsi != hdrsi + { + char str[65],str2[65]; printf("ramchain_addspend RO value mismatch diffseq.%d v %x (%d) vs (%d) %s vs %s\n",s->diffsequence,sequence,s->prevout,prev_vout,bits256_str(str,txid),bits256_str(str2,prev_hash)); + return(0); + } + } + else + { + if ( sequence != 0xffffffff ) + s->diffsequence = 1; + s->external = external, s->spendtxidind = txidind, + s->prevout = prev_vout; + //s->hdrsi = hdrsi; + //s->bundlei = bundlei; + //char str[65]; printf("%s set prevout.%d -> %d\n",bits256_str(str,prev_hash),prev_vout,s->prevout); + //if ( pkind != 0 ) + // s->prevspendind = A[pkind].lastspendind; + } + if ( pkind != 0 ) + { + A[pkind].balance -= value; + //A[pkind].lastspendind = spendind; + //if ( P2[pkind].firstspendind == 0 ) + // P2[pkind].firstspendind = spendind; + } + /*if ( unspentind != 0 ) + { + if ( U2[unspentind].spendind == 0 ) + U2[unspentind].spendind = spendind; + }*/ + return(spendind); +} + +int64_t iguana_hashmemsize(uint32_t numtxids,uint32_t numunspents,uint32_t numspends,uint32_t numpkinds,uint32_t numexternaltxids) +{ + int64_t allocsize = 0; + allocsize += (65536*4 + ((numtxids + numpkinds) * (sizeof(UT_hash_handle)*2)) + (((sizeof(struct iguana_account)) * 2 * numpkinds)) + (2 * numunspents * sizeof(struct iguana_Uextra))); + //printf("iguana_hashmemsize T.%d U.%d S.%d P.%d X.%d -> %ld\n",numtxids,numunspents,numspends,numpkinds,numexternaltxids,(long)allocsize); + return(allocsize); +} + +void _iguana_ramchain_setptrs(RAMCHAIN_PTRPS,struct iguana_ramchaindata *rdata) +{ + *B = (void *)((long)rdata + (long)rdata->Boffset); + *T = (void *)((long)rdata + (long)rdata->Toffset); + if ( ramchain->expanded != 0 ) + { + *Ux = (void *)((long)rdata + (long)rdata->Uoffset); + *Sx = (void *)((long)rdata + (long)rdata->Soffset); + *P = (void *)((long)rdata + (long)rdata->Poffset); + *X = (void *)((long)rdata + (long)rdata->Xoffset); + //ramchain->roU2 = (void *)((long)rdata + (long)rdata->U2offset); + //ramchain->roP2 = (void *)((long)rdata + (long)rdata->P2offset); + ramchain->roA = (void *)((long)rdata + (long)rdata->Aoffset); + //if ( (*U2= ramchain->U2) == 0 ) + // *U2 = ramchain->U2 = ramchain->roU2; + //if ( (*P2= ramchain->P2) == 0 ) + // *P2 = ramchain->P2 = ramchain->roP2; + if ( (*A= ramchain->A) == 0 ) + *A = ramchain->A = ramchain->roA; + //printf("T.%p Ux.%p Sx.%p P.%p\n",*T,*Ux,*Sx,*P); + *TXbits = (void *)((long)rdata + (long)rdata->TXoffset); + *PKbits = (void *)((long)rdata + (long)rdata->PKoffset); + *U = 0, *S = 0; + } + else + { + *U = (void *)((long)rdata + (long)rdata->Uoffset); + *S = (void *)((long)rdata + (long)rdata->Soffset); + *Ux = 0, *Sx = 0, *P = 0, *X = 0, *A = 0, *TXbits = 0, *PKbits = 0; //*U2 = 0, *P2 = 0, + } +} + +void *iguana_ramchain_offset(void *dest,uint8_t *lhash,FILE *fp,uint64_t fpos,void *srcptr,uint64_t *offsetp,uint64_t len,uint64_t srcsize) +{ + void *destptr = (void *)((long)dest + *offsetp); + if ( (lhash != 0 || fp != 0) && (*offsetp + len) > srcsize ) + { + printf("ramchain_offset overflow (%p %p) offset.%ld + len.%ld %ld > %ld srcsize\n",fp,lhash,(long)*offsetp,(long)len,(long)(*offsetp + len),(long)srcsize); + exit(-1); + } + if ( lhash != 0 ) + { + //fprintf(stderr,"lhash.%p memptr.%p offset.%ld len.%ld avail.%ld srcsize.%ld\n",lhash,srcptr,(long)*offsetp,(long)len,(long)(srcsize - (*offsetp + len)),(long)srcsize); + vcalc_sha256(0,lhash,srcptr,(uint32_t)len); + } + else if ( fp != 0 ) + { + if ( fwrite(srcptr,1,len,fp) != len ) + printf("iguana_ramchain_sizefunc: error writing len.%ld to fp.%p\n",(long)len,fp); + //else printf("fp.(%ld <- %d) ",ftell(fp),(int32_t)len); + } + (*offsetp) += len; + return((void *)((long)destptr + fpos)); +} + +int64_t _iguana_rdata_action(FILE *fp,bits256 lhashes[IGUANA_NUMLHASHES],void *destptr,uint64_t fpos,uint32_t expanded,uint32_t numtxids,uint32_t numunspents,uint32_t numspends,uint32_t numpkinds,uint32_t numexternaltxids,uint32_t txsparsebits,uint64_t numtxsparse,uint32_t pksparsebits,uint64_t numpksparse,uint64_t srcsize,RAMCHAIN_FUNC,int32_t numblocks) +{ +#define RAMCHAIN_LARG(ind) ((lhashes == 0) ? 0 : lhashes[ind].bytes) +#define SPARSECOUNT(x) ((x) << 2) + FILE *fparg = 0; int32_t iter; uint64_t txbits,pkbits,offset = 0; struct iguana_ramchaindata *rdata = destptr; + if ( expanded != 0 ) + { + if( txsparsebits == 0 || numtxsparse == 0 ) + { + txsparsebits = hcalc_bitsize(numtxids); + numtxsparse = SPARSECOUNT(numtxids); + } + if ( pksparsebits == 0 || numpksparse == 0 ) + { + pksparsebits = hcalc_bitsize(numpkinds); + numpksparse = SPARSECOUNT(numpkinds); + } + txbits = numtxsparse * txsparsebits; pkbits = numpksparse * pksparsebits; + } else txbits = pkbits = numtxsparse = txsparsebits = numpksparse = pksparsebits = 0; + for (iter=0; iter<2; iter++) + { + if ( iter == 0 && lhashes == 0 ) + { + fparg = fp; + continue; + } + offset = sizeof(struct iguana_ramchaindata); + B = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_BLOCKS),fparg,fpos,B,&offset,(sizeof(struct iguana_blockRO) * numblocks),srcsize); + T = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_TXIDS),fparg,fpos,T,&offset,(sizeof(struct iguana_txid) * numtxids),srcsize); + if ( expanded != 0 ) + { + U = destptr, S = destptr; + Ux = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_UNSPENTS),fparg,fpos,Ux,&offset,(sizeof(struct iguana_unspent) * numunspents),srcsize); + Sx = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_SPENDS),fparg,fpos,Sx,&offset,(sizeof(struct iguana_spend) * numspends),srcsize); + P = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_PKHASHES),fparg,fpos,P,&offset,(sizeof(struct iguana_pkhash) * numpkinds),srcsize); + //U2 = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_SPENTINDS),fparg,fpos,U2,&offset,(sizeof(struct iguana_Uextra) * numunspents),srcsize); + //P2 = 0;//iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_FIRSTSPENDS),fparg,fpos,P2,&offset,(sizeof(struct iguana_pkextra) * numpkinds),srcsize); + A = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_ACCOUNTS),fparg,fpos,A,&offset,(sizeof(struct iguana_account) * numpkinds),srcsize); + char str[65]; + if ( 0 && X != 0 ) + printf("%p X[1] -> %s\n",&X[1],bits256_str(str,X[1])); + X = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_EXTERNALS),fparg,fpos,X,&offset,(sizeof(bits256) * numexternaltxids),srcsize); + TXbits = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_TXBITS),fparg,fpos,TXbits,&offset,hconv_bitlen(txbits),srcsize); + PKbits = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_PKBITS),fparg,fpos,PKbits,&offset,hconv_bitlen(pkbits),srcsize); + } + else + { + Ux = destptr, Sx = destptr, P = destptr, A = destptr, X = destptr, TXbits = destptr, PKbits = destptr; //U2 = destptr, P2 = destptr, + U = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_UNSPENTS),fparg,fpos,U,&offset,(sizeof(struct iguana_unspent20) * numunspents),srcsize); + if ( 0 && lhashes != 0 ) + printf("iter.%d lhashes.%p offset.%ld destptr.%p len.%ld fparg.%p fpos.%ld srcsize.%ld\n",iter,RAMCHAIN_LARG(IGUANA_LHASH_SPENDS),(long)offset,destptr,(long)sizeof(struct iguana_spend256) * numspends,fparg,(long)fpos,(long)srcsize); + S = iguana_ramchain_offset(rdata,RAMCHAIN_LARG(IGUANA_LHASH_SPENDS),fparg,fpos,S,&offset,(sizeof(struct iguana_spend256) * numspends),srcsize); + } + if ( (fparg= fp) == 0 ) + break; + lhashes = 0; + } + if ( rdata != 0 ) + { + rdata->allocsize = offset; + rdata->Boffset = (uint64_t)((long)B - (long)destptr); + rdata->Toffset = (uint64_t)((long)T - (long)destptr); + if ( expanded != 0 ) + { + rdata->Uoffset = (uint64_t)((long)Ux - (long)destptr); + rdata->Soffset = (uint64_t)((long)Sx - (long)destptr); + } + else + { + rdata->Uoffset = (uint64_t)((long)U - (long)destptr); + rdata->Soffset = (uint64_t)((long)S - (long)destptr); + } + rdata->Poffset = (uint64_t)((long)P - (long)destptr); + //rdata->U2offset = (uint64_t)((long)U2 - (long)destptr); + //rdata->P2offset = (uint64_t)((long)P2 - (long)destptr); + rdata->Aoffset = (uint64_t)((long)A - (long)destptr); + rdata->Xoffset = (uint64_t)((long)X - (long)destptr); + rdata->TXoffset = (uint64_t)((long)TXbits - (long)destptr); + rdata->PKoffset = (uint64_t)((long)PKbits - (long)destptr); + rdata->numtxids = numtxids; + rdata->numunspents = numunspents; + rdata->numspends = numspends; + rdata->numpkinds = numpkinds; + rdata->numexternaltxids = numexternaltxids; + rdata->txsparsebits = txsparsebits, rdata->numtxsparse = (uint32_t)numtxsparse; + rdata->pksparsebits = pksparsebits, rdata->numpksparse = (uint32_t)numpksparse; + } + return(offset); +#undef RAMCHAIN_LARG +#undef SPARSECOUNT +} + +int64_t iguana_ramchain_action(RAMCHAIN_FUNC,FILE *fp,bits256 lhashes[IGUANA_NUMLHASHES],struct iguana_ramchaindata *destdata,uint64_t fpos,struct iguana_ramchaindata *srcdata,int32_t numblocks) +{ + if ( 0 && ramchain->expanded != 0 ) + printf("action.%p (%p %p %p) %ld allocated.%ld [%d:%d %d:%d]\n",srcdata,fp,lhashes,destdata,(long)fpos,(long)srcdata->allocsize,srcdata->txsparsebits,srcdata->numtxsparse,srcdata->pksparsebits,srcdata->numpksparse); + return(_iguana_rdata_action(fp,lhashes,destdata,fpos,ramchain->expanded,srcdata->numtxids,srcdata->numunspents,srcdata->numspends,srcdata->numpkinds,srcdata->numexternaltxids,srcdata->txsparsebits,srcdata->numtxsparse,srcdata->pksparsebits,srcdata->numpksparse,srcdata->allocsize,RAMCHAIN_ARG,numblocks)); +} + +int64_t iguana_ramchain_size(RAMCHAIN_FUNC,int32_t numblocks) +{ + return(iguana_ramchain_action(RAMCHAIN_ARG,0,0,0,0,ramchain->H.data,numblocks)); +} + +long iguana_ramchain_setsize(struct iguana_ramchain *ramchain,struct iguana_ramchaindata *srcdata,int32_t numblocks) +{ + RAMCHAIN_DECLARE; struct iguana_ramchaindata *rdata = ramchain->H.data; + B = 0, Ux = 0, Sx = 0, P = 0, A = 0, X = 0, TXbits = 0, PKbits = 0, U = 0, S = 0, T = 0; //U2 = 0, P2 = 0, + rdata->numtxids = ramchain->H.txidind; + rdata->numunspents = ramchain->H.unspentind; + rdata->numspends = ramchain->H.spendind; + rdata->numpkinds = ramchain->pkind; + rdata->numexternaltxids = ramchain->externalind; + rdata->allocsize = iguana_ramchain_size(RAMCHAIN_ARG,numblocks); + ramchain->datasize = rdata->allocsize; + return((long)rdata->allocsize); +} + +int64_t iguana_ramchain_compact(RAMCHAIN_FUNC,struct iguana_ramchaindata *destdata,struct iguana_ramchaindata *srcdata,int32_t numblocks) +{ + //iguana_ramchain_setsize(ramchain,srcdata); + return(iguana_ramchain_action(RAMCHAIN_ARG,0,0,destdata,0,srcdata,numblocks)); +} + +bits256 iguana_ramchain_lhashes(RAMCHAIN_FUNC,struct iguana_ramchaindata *destdata,struct iguana_ramchaindata *srcdata,int32_t numblocks) +{ + iguana_ramchain_action(RAMCHAIN_ARG,0,destdata->lhashes,0,0,srcdata,numblocks); + memset(&destdata->sha256,0,sizeof(destdata->sha256)); + vcalc_sha256(0,destdata->sha256.bytes,(void *)destdata,sizeof(*destdata)); + return(destdata->sha256); +} + +int64_t iguana_ramchain_saveaction(RAMCHAIN_FUNC,FILE *fp,struct iguana_ramchaindata *rdata,int32_t numblocks) +{ + long before,after; + before = ftell(fp); + iguana_ramchain_action(RAMCHAIN_ARG,fp,0,rdata,0,rdata,numblocks); + after = ftell(fp); + if ( 0 && ramchain->expanded != 0 ) + { + char str[65]; + printf("SAVEACTION: rdata.%ld DEST T.%d U.%d S.%d P.%d X.%d -> size.%ld %ld vs %ld %p %s\n",sizeof(*rdata),rdata->numtxids,rdata->numunspents,rdata->numspends,rdata->numpkinds,rdata->numexternaltxids,(long)rdata->allocsize,(long)iguana_ramchain_size(RAMCHAIN_ARG,numblocks),after-before+sizeof(*rdata),&X[1],bits256_str(str,X[1])); + } + //printf("before.%ld after.%ld allocsize.%ld [%ld] %ld expanded.%d\n",before,after,(long)srcdata->allocsize,(long)ramchain->H.data->allocsize,(long)iguana_ramchain_size(ramchain),ramchain->expanded); + return(after - before); +} + +int64_t iguana_ramchain_init(struct iguana_ramchain *ramchain,struct OS_memspace *mem,struct OS_memspace *hashmem,int32_t firsti,int32_t numtxids,int32_t numunspents,int32_t numspends,int32_t numpkinds,int32_t numexternaltxids,int32_t expanded,int32_t numblocks) +{ + RAMCHAIN_DECLARE; int64_t offset = 0; struct iguana_ramchaindata *rdata; + B = 0, Ux = 0, Sx = 0, P = 0, A = 0, X = 0, TXbits = 0, PKbits = 0, U = 0, S = 0, T = 0; //U2 = 0, P2 = 0, + if ( mem == 0 ) + return(0); + memset(ramchain,0,sizeof(*ramchain)); + ramchain->expanded = (expanded != 0); + if ( (ramchain->hashmem= hashmem) != 0 ) + iguana_memreset(hashmem); + rdata = ramchain->H.data = mem->ptr, offset += sizeof(struct iguana_ramchaindata); + if ( (rdata->firsti= firsti) != 0 ) + { + numtxids++, numunspents++, numspends++; + if ( numpkinds != 0 ) + numpkinds++; + } + if ( numexternaltxids == 0 ) + numexternaltxids = numspends; + if ( numpkinds == 0 ) + numpkinds = numunspents; + if ( 0 && expanded != 0 ) + printf("init T.%d U.%d S.%d P.%d X.%d -> %ld\n",numtxids,numunspents,numspends,numpkinds,numexternaltxids,(long)offset); + _iguana_rdata_action(0,0,rdata,0,expanded,numtxids,numunspents,numspends,numpkinds,numexternaltxids,0,0,0,0,0,RAMCHAIN_ARG,numblocks); + if ( rdata->allocsize != iguana_ramchain_size(RAMCHAIN_ARG,numblocks) ) + { + printf("offset.%ld vs memsize.%ld\n",(long)offset,(long)iguana_ramchain_size(RAMCHAIN_ARG,numblocks)); + exit(-1); + } + if ( offset < mem->totalsize ) + iguana_memreset(mem); + else + { + printf("offset.%ld vs memsize.%ld\n",(long)offset,(long)iguana_ramchain_size(RAMCHAIN_ARG,numblocks)); + printf("NEED %ld realloc for %ld\n",(long)offset,(long)mem->totalsize); + exit(-1); + iguana_mempurge(mem); + iguana_meminit(mem,"ramchain",0,offset,0); + } + //printf("init.(%d %d %d %d %d)\n",numtxids,numunspents,numspends,numpkinds,numexternaltxids); + return(offset); +} + +int32_t iguana_ramchain_alloc(struct iguana_info *coin,struct iguana_ramchain *ramchain,struct OS_memspace *mem,struct OS_memspace *hashmem,uint32_t numtxids,uint32_t numunspents,uint32_t numspends,uint32_t numpkinds,uint32_t numexternaltxids,int32_t height,int32_t numblocks) +{ + RAMCHAIN_DECLARE; int64_t hashsize,allocsize,x; + B = 0, Ux = 0, Sx = 0, P = 0, A = 0, X = 0, TXbits = 0, PKbits = 0, U = 0, S = 0, T = 0; //U2 = 0, P2 = 0, + memset(ramchain,0,sizeof(*ramchain)); + ramchain->height = height; + allocsize = _iguana_rdata_action(0,0,0,0,1,numtxids,numunspents,numspends,numpkinds,numexternaltxids,0,0,0,0,0,RAMCHAIN_ARG,numblocks); + //printf("T.%d U.%d S.%d P.%d X.%d -> %ld\n",numtxids,numunspents,numspends,numpkinds,numexternaltxids,(long)allocsize); + memset(mem,0,sizeof(*mem)); + memset(hashmem,0,sizeof(*hashmem)); + hashsize = iguana_hashmemsize(numtxids,numunspents,numspends,numpkinds,numexternaltxids); + while ( (x= (myallocated(0,-1)+hashsize+allocsize)) > coin->MAXMEM ) + { + char str[65],str2[65]; fprintf(stderr,"ht.%d wait for allocated %s < MAXMEM %s | elapsed %.2f minutes\n",height,mbstr(str,hashsize+allocsize),mbstr(str2,coin->MAXMEM),(double)(time(NULL)-coin->startutc)/60.); + sleep(3); + } + iguana_meminit(hashmem,"ramhashmem",0,hashsize + 4096,0); + iguana_meminit(mem,"ramchain",0,allocsize + 4096,0); + mem->alignflag = sizeof(uint32_t); + hashmem->alignflag = sizeof(uint32_t); + if ( iguana_ramchain_init(ramchain,mem,hashmem,1,numtxids,numunspents,numspends,numpkinds,numexternaltxids,1,numblocks) == 0 ) + return(-1); + return(0); +} + +long iguana_ramchain_save(struct iguana_info *coin,RAMCHAIN_FUNC,uint32_t ipbits,bits256 hash2,bits256 prevhash2,int32_t bundlei,struct iguana_bundle *bp) +{ + struct iguana_ramchaindata *rdata,tmp; + char fname[1024]; long fpos = -1; int32_t hdrsi,checki; FILE *fp; + if ( (rdata= ramchain->H.data) == 0 ) + { + printf("ramchainsave no data ptr\n"); + return(-1); + } + if ( (checki= iguana_peerfname(coin,&hdrsi,ipbits==0?"DB":"tmp",fname,ipbits,hash2,prevhash2,ramchain->numblocks)) != bundlei || bundlei < 0 || bundlei >= coin->chain->bundlesize ) + { + printf(" wont save.(%s) bundlei.%d != checki.%d\n",fname,bundlei,checki); + return(-1); + } + OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb+")) == 0 ) + { + if ( (fp= fopen(fname,"wb")) != 0 ) + coin->peers.numfiles++; + } + else if ( ipbits != 0 ) + fseek(fp,0,SEEK_END); + else + { + fclose(fp); + fp = fopen(fname,"wb"); + } + if ( fp != 0 ) + { + fpos = ftell(fp); + iguana_ramchain_lhashes(RAMCHAIN_ARG,rdata,rdata,bp!=0?bp->n:1); + tmp = *rdata; + iguana_ramchain_compact(RAMCHAIN_ARG,&tmp,rdata,bp!=0?bp->n:1); + fwrite(&tmp,1,sizeof(tmp),fp); + iguana_ramchain_saveaction(RAMCHAIN_ARG,fp,rdata,bp!=0?bp->n:1); + *rdata = tmp; + fclose(fp); + } + return(fpos); +} + +int32_t iguana_ramchain_verify(struct iguana_info *coin,struct iguana_ramchain *ramchain) +{ + RAMCHAIN_DECLARE; struct iguana_txid *t; struct iguana_unspent *u; struct iguana_pkhash *p; + struct iguana_ramchaindata *rdata; int32_t k,pkind,vout; struct iguana_kvitem *ptr; bits256 txid; + // iguana_txid { bits256 txid; uint32_t txidind,firstvout,firstvin; uint16_t numvouts,numvins;} + if ( (rdata= ramchain->H.data) == 0 ) + return(-100); + _iguana_ramchain_setptrs(RAMCHAIN_PTRS,rdata); + ramchain->pkind = ramchain->H.unspentind = ramchain->H.spendind = rdata->firsti; + ramchain->externalind = 0; + for (ramchain->H.txidind=rdata->firsti; ramchain->H.txidindnumtxids; ramchain->H.txidind++) + { + t = &T[ramchain->H.txidind]; + if ( t->txidind != ramchain->H.txidind ) + { + printf("firsti.%d t->txidind.%d != txidind.%d\n",rdata->firsti,t->txidind,ramchain->H.txidind); + return(-1); + } + if ( t->firstvout != ramchain->H.unspentind ) + { + printf("%p txidind.%d firstvout.%d != unspentind.%d\n",t,ramchain->H.txidind,t->firstvout,ramchain->H.unspentind); + exit(-1); + return(-4); + } + if ( t->firstvin != ramchain->H.spendind ) + { + printf("t[%d] firstvin.%d vs spendind.%d\n",t->txidind,t->firstvin,ramchain->H.spendind); + return(-5); + } + if ( ramchain->expanded != 0 ) + { + if ( (ptr= iguana_hashfind(ramchain,'T',t->txid.bytes)) == 0 ) + return(-2); + if ( ptr->hh.itemind != ramchain->H.txidind ) + { + if ( strcmp(coin->symbol,"BTC") == 0 ) + { + bits256 duptxid,duptxid2; + decode_hex(duptxid.bytes,sizeof(duptxid),"e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468"); // BTC.tx0: 91722, 91880 + decode_hex(duptxid2.bytes,sizeof(duptxid2),"d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599"); // BTC.tx0 91812, 91842 + if ( memcmp(duptxid.bytes,t->txid.bytes,sizeof(duptxid)) == 0 || memcmp(duptxid2.bytes,t->txid.bytes,sizeof(duptxid2)) == 0 ) + printf("BIP 0 detected\n"); + else + { + char str[65]; printf("error -3: %s itemind.%d vs txidind.%d | num.%d\n",bits256_str(str,t->txid),ptr->hh.itemind,ramchain->H.txidind,ramchain->H.data->numtxids); + return(-3); + } + } + else + { + char str[65]; printf("error -3: %s itemind.%d vs txidind.%d | num.%d\n",bits256_str(str,t->txid),ptr->hh.itemind,ramchain->H.txidind,ramchain->H.data->numtxids); + exit(-1); + return(-3); + } + } + for (k=0; knumvouts; k++,ramchain->H.unspentind++) + { + u = &Ux[ramchain->H.unspentind]; + if ( u->txidind != ramchain->H.txidind ) + { + printf("ramchain_verify k.%d %p U.%d u->txidind.%x != txidind.%d\n",k,u,ramchain->H.unspentind,u->txidind,ramchain->H.txidind); + exit(-1); + return(-6); + } + if ( (pkind= u->pkind) < 0 || pkind >= rdata->numpkinds ) + { + printf("k.%d unspentind.%d pkind.%d numpkinds.%d\n",k,ramchain->H.unspentind,pkind,rdata->numpkinds); + return(-7); + } + p = &P[pkind]; + if ( (ptr= iguana_hashfind(ramchain,'P',p->rmd160)) == 0 ) + return(-8); + if ( ptr->hh.itemind == pkind && p->firstunspentind > ramchain->H.unspentind ) + { + printf("%p itemind.%d pkind.%d firstunspent.%d != %d unspentind?\n",p,ptr->hh.itemind,pkind,p->firstunspentind,ramchain->H.unspentind); + return(-9); + } + } + } + else + { + for (k=0; knumvouts; k++,ramchain->H.unspentind++) + { + if ( U[ramchain->H.unspentind].txidind != ramchain->H.txidind ) + { + printf(" k.%d U.%d u->txidind.%x != txidind.%d\n",k,ramchain->H.unspentind,U[ramchain->H.unspentind].txidind,ramchain->H.txidind); + return(-6); + } + } + } + ramchain->H.spendind += t->numvins; + } + ramchain->H.spendind = rdata->firsti; + for (ramchain->H.txidind=rdata->firsti; ramchain->H.txidindnumtxids; ramchain->H.txidind++) + { + t = &T[ramchain->H.txidind]; + for (k=0; knumvins; k++,ramchain->H.spendind++) + { + if ( ramchain->expanded != 0 ) + { + //printf("item.%p [%d] X.%p k.%d txidind.%d/%d spendind.%d/%d s->txidind.%x/v%d\n",rdata,rdata->numexternaltxids,X,k,ramchain->txidind,rdata->numtxids,spendind,rdata->numspends,s->spendtxidind,s->vout); + if ( (vout= iguana_ramchain_txid(coin,RAMCHAIN_ARG,&txid,&Sx[ramchain->H.spendind])) < -1 ) + { + printf("txidind.%d k.%d error getting txid firsti.%d X.%d vout.%d spend.%x/%d numX.%d numT.%d\n",ramchain->H.txidind,k,rdata->firsti,ramchain->externalind,vout,Sx[ramchain->H.spendind].spendtxidind,rdata->numspends,rdata->numexternaltxids,rdata->numtxids); + return(-10); + } + if ( vout == -1 ) + { + // mining output + } + else + { + if ( (ptr= iguana_hashfind(ramchain,'T',txid.bytes)) == 0 ) + { + char str[65]; printf("cant find vout.%d %s\n",vout,bits256_str(str,txid)); + return(-11); + } + } + } + } + } + if ( ramchain->expanded != 0 && ramchain->A != ramchain->roA ) + { + for (k=rdata->firsti; knumpkinds; k++) + { + if ( memcmp(&ramchain->A[k],&ramchain->roA[k],sizeof(ramchain->A[k])) != 0 ) + { + printf("k.%d balance.(%.8f vs %.8f) lastU.(%d %d)\n",k,dstr(ramchain->A[k].balance),dstr(ramchain->roA[k].balance),ramchain->A[k].lastunspentind,ramchain->roA[k].lastunspentind); + return(-14); + } + //if ( memcmp(&ramchain->P2[k],&ramchain->roP2[k],sizeof(ramchain->P2[k])) != 0 ) + // return(-15); + } + //for (k=rdata->firsti; knumunspents; k++) + // if ( memcmp(&ramchain->U2[k],&ramchain->roU2[k],sizeof(ramchain->U2[k])) != 0 ) + // return(-16); + } + return(0); +} + +int32_t iguana_ramchain_free(struct iguana_ramchain *ramchain,int32_t deleteflag) +{ + struct iguana_kvitem *item,*tmp; + if ( ramchain->H.ROflag != 0 && ramchain->hashmem == 0 ) + { + //printf("Free A %p %p, U2, P2\n",ramchain->A,ramchain->roA); + if ( ramchain->A != ramchain->roA ) + myfree(ramchain->A,sizeof(*ramchain->A) * ramchain->H.data->numpkinds), ramchain->A = 0; + //if ( ramchain->U2 != ramchain->roU2 ) + // myfree(ramchain->U2,sizeof(*ramchain->U2) * ramchain->H.data->numunspents), ramchain->U2 = 0; + //if ( ramchain->P2 != ramchain->roP2 ) + // myfree(ramchain->P2,sizeof(*ramchain->P2) * ramchain->H.data->numpkinds), ramchain->P2 = 0; + } + if ( deleteflag != 0 ) + { + if ( ramchain->txids != 0 ) + { + HASH_ITER(hh,ramchain->txids,item,tmp) + { + HASH_DEL(ramchain->txids,item); + if ( ramchain->hashmem == 0 ) + myfree(item,sizeof(*item)); + } + } + if ( ramchain->pkhashes != 0 ) + { + HASH_ITER(hh,ramchain->pkhashes,item,tmp) + { + HASH_DEL(ramchain->pkhashes,item); + if ( ramchain->hashmem == 0 ) + myfree(item,sizeof(*item)); + } + } + } + if ( ramchain->hashmem != 0 ) + iguana_memreset(ramchain->hashmem); + if ( ramchain->filesize != 0 ) + munmap(ramchain->fileptr,ramchain->filesize); + memset(ramchain,0,sizeof(*ramchain)); + return(0); +} + +void iguana_ramchain_extras(struct iguana_ramchain *ramchain,struct OS_memspace *hashmem) +{ + RAMCHAIN_DECLARE; + if ( ramchain->expanded != 0 ) + { + _iguana_ramchain_setptrs(RAMCHAIN_PTRS,ramchain->H.data); + if ( (ramchain->hashmem= hashmem) != 0 ) + iguana_memreset(hashmem); + ramchain->A = (hashmem != 0) ? iguana_memalloc(hashmem,sizeof(struct iguana_account) * ramchain->H.data->numpkinds,1) : mycalloc('p',ramchain->H.data->numpkinds,sizeof(struct iguana_account)); + //ramchain->P2 = (hashmem != 0) ? iguana_memalloc(hashmem,sizeof(struct iguana_pkextra) * ramchain->H.data->numpkinds,1) : mycalloc('2',ramchain->H.data->numpkinds,sizeof(struct iguana_pkextra)); + ///ramchain->U2 = (hashmem != 0) ? iguana_memalloc(hashmem,sizeof(struct iguana_Uextra) * ramchain->H.data->numunspents,1) : mycalloc('3',ramchain->H.data->numunspents,sizeof(struct iguana_Uextra)); + //printf("iguana_ramchain_extras A.%p:%p U2.%p:%p P2.%p:%p\n",ramchain->A,ramchain->roA,ramchain->U2,ramchain->roU2,ramchain->P2,ramchain->roP2); + //memcpy(ramchain->U2,ramchain->roU2,sizeof(*ramchain->U2) * ramchain->H.data->numunspents); + //memcpy(ramchain->P2,ramchain->roP2,sizeof(*ramchain->P2) * ramchain->H.data->numpkinds); + } +} + +struct iguana_ramchain *iguana_ramchain_map(struct iguana_info *coin,char *fname,struct iguana_bundle *bp,int32_t numblocks,struct iguana_ramchain *ramchain,struct OS_memspace *hashmem,uint32_t ipbits,bits256 hash2,bits256 prevhash2,int32_t bundlei,long fpos,int32_t allocextras,int32_t expanded) +{ + RAMCHAIN_DECLARE; int32_t valid,i,checki,hdrsi; + char str[65],str2[65]; long filesize; void *ptr; struct iguana_block *block; + if ( ramchain->fileptr == 0 || ramchain->filesize <= 0 ) + { + if ( (checki= iguana_peerfname(coin,&hdrsi,ipbits==0?"DB":"tmp",fname,ipbits,hash2,prevhash2,numblocks)) != bundlei || bundlei < 0 || bundlei >= coin->chain->bundlesize ) + { + printf("iguana_ramchain_map.(%s) illegal hdrsi.%d bundlei.%d %s\n",fname,hdrsi,bundlei,bits256_str(str,hash2)); + return(0); + } + memset(ramchain,0,sizeof(*ramchain)); + if ( (ptr= OS_mapfile(fname,&filesize,0)) == 0 ) + return(0); + ramchain->fileptr = ptr; + ramchain->filesize = (long)filesize; + if ( (ramchain->hashmem= hashmem) != 0 ) + iguana_memreset(hashmem); + } + if ( ramchain->fileptr != 0 && ramchain->filesize > 0 ) + { + // verify hashes + ramchain->H.data = (void *)((long)ramchain->fileptr + fpos); + ramchain->H.ROflag = 1; + ramchain->expanded = expanded; + ramchain->numblocks = (bp == 0) ? 1 : bp->n; + //printf("ptr.%p %p mapped P[%d] fpos.%d + %ld -> %ld vs %ld\n",ptr,ramchain->H.data,(int32_t)ramchain->H.data->Poffset,(int32_t)fpos,(long)ramchain->H.data->allocsize,(long)(fpos + ramchain->H.data->allocsize),ramchain->filesize); + if ( 0 && bp != 0 ) + { + /*blocksRO = (struct iguana_blockRO *)ramchain->H.data; + for (i=0; in; i++) + { + printf("%p ",&blocksRO[i]); + bp->hashes[i] = blocksRO[i].hash2; + if ( (bp->blocks[i]= iguana_blockhashset(coin,-1,blocksRO[i].hash2,1)) == 0 ) + { + printf("Error getting blockptr\n"); + return(0); + } + bp->blocks[i]->RO = blocksRO[i]; + } + ramchain->H.data = (void *)&blocksRO[bp->n];*/ + for (valid=0,i=bp->n=1; i>=0; i--) + { + if ( (block= bp->blocks[i]) != 0 ) + { + if ( memcmp(block->RO.hash2.bytes,bp->hashes[i].bytes,sizeof(block->RO.hash2)) == 0 ) + { + if ( i == 0 || memcmp(block->RO.prev_block.bytes,bp->hashes[i-1].bytes,sizeof(block->RO.prev_block)) == 0 ) + valid++; + } + } + } + if ( valid != bp->n ) + { + printf("valid.%d != bp->n.%d, reject mapped ramchain\n",valid,bp->n); + return(0); + } + } + _iguana_ramchain_setptrs(RAMCHAIN_PTRS,ramchain->H.data); + if ( iguana_ramchain_size(RAMCHAIN_ARG,ramchain->numblocks) != ramchain->H.data->allocsize || fpos+ramchain->H.data->allocsize > filesize ) + { + printf("iguana_ramchain_map.(%s) size mismatch %ld vs %ld vs filesize.%ld numblocks.%d expanded.%d fpos.%d sum %ld\n",fname,(long)iguana_ramchain_size(RAMCHAIN_ARG,ramchain->numblocks),(long)ramchain->H.data->allocsize,(long)filesize,ramchain->numblocks,expanded,(int32_t)fpos,(long)(fpos+ramchain->H.data->allocsize)); + exit(-1); + //munmap(ramchain->fileptr,ramchain->filesize); + return(0); + } + else if ( memcmp(hash2.bytes,ramchain->H.data->firsthash2.bytes,sizeof(bits256)) != 0 ) + { + printf("iguana_ramchain_map.(%s) hash2 mismatch %s vs %s\n",fname,bits256_str(str,hash2),bits256_str(str2,ramchain->H.data->firsthash2)); + //munmap(ramchain->fileptr,ramchain->filesize); + return(0); + } + else if ( ramchain->expanded != 0 ) + { + if ( allocextras > 0 ) + iguana_ramchain_extras(ramchain,ramchain->hashmem); + } + if ( B != 0 ) + { + for (i=0; in; i++) + { + if ( (bp->blocks[i]= iguana_blockhashset(coin,-1,B[i].hash2,1)) == 0 ) + { + printf("Error getting blockptr\n"); + return(0); + } + bp->blocks[i]->RO = B[i];//coin->blocks.RO[bp->bundleheight + i]; + coin->blocks.RO[bp->bundleheight+i] = B[i]; + } + + } + return(ramchain); + } else printf("iguana_ramchain_map.(%s) cant map file\n",fname); + return(0); +} + +void iguana_ramchain_link(struct iguana_ramchain *ramchain,bits256 firsthash2,bits256 lasthash2,int32_t hdrsi,int32_t height,int32_t bundlei,int32_t numblocks,int32_t firsti,int32_t ROflag) +{ + if ( ROflag == 0 ) + { + ramchain->H.data->firsthash2 = firsthash2; + ramchain->H.data->lasthash2 = lasthash2; + ramchain->H.data->hdrsi = hdrsi; + ramchain->H.data->height = height; + ramchain->H.data->numblocks = numblocks; + } + ramchain->H.hdrsi = hdrsi; + ramchain->H.bundlei = bundlei; + ramchain->height = height; + ramchain->numblocks = numblocks; + ramchain->lasthash2 = lasthash2; + ramchain->H.txidind = ramchain->H.unspentind = ramchain->H.spendind = ramchain->pkind = firsti; + ramchain->externalind = 0; +} + +int32_t iguana_ramchain_cmp(struct iguana_ramchain *A,struct iguana_ramchain *B,int32_t deepflag) +{ + int32_t i; char str[65],str2[65]; + struct iguana_txid *Ta,*Tb; struct iguana_unspent20 *Ua,*Ub; struct iguana_spend256 *Sa,*Sb; + struct iguana_pkhash *Pa,*Pb; bits256 *Xa,*Xb; struct iguana_blockRO *Ba,*Bb; + struct iguana_account *ACCTa,*ACCTb; struct iguana_unspent *Uxa,*Uxb; + struct iguana_spend *Sxa,*Sxb; uint8_t *TXbitsa,*TXbitsb,*PKbitsa,*PKbitsb; + + if ( A->H.data != 0 && B->H.data != 0 && A->H.data->numblocks == B->H.data->numblocks && memcmp(A->H.data->firsthash2.bytes,B->H.data->firsthash2.bytes,sizeof(A->H.data->firsthash2)) == 0 ) + { + if ( A->H.data->firsti == B->H.data->firsti && A->H.data->numtxids == B->H.data->numtxids && A->H.data->numunspents == B->H.data->numunspents && A->H.data->numspends == B->H.data->numspends && A->H.data->numpkinds == B->H.data->numpkinds && A->H.data->numexternaltxids == B->H.data->numexternaltxids ) + { + _iguana_ramchain_setptrs(A,&Ba,&Ta,&Ua,&Sa,&Pa,&ACCTa,&Xa,&Uxa,&Sxa,&TXbitsa,&PKbitsa,A->H.data); + _iguana_ramchain_setptrs(B,&Bb,&Tb,&Ub,&Sb,&Pb,&ACCTb,&Xb,&Uxb,&Sxb,&TXbitsb,&PKbitsb,B->H.data); + for (i=A->H.data->firsti; iH.data->numtxids; i++) + if ( memcmp(&Ta[i],&Tb[i],sizeof(Ta[i])) != 0 ) + return(-2); + if ( A->numblocks > 1 ) + { + for (i=A->H.data->firsti; iH.data->numspends; i++) + if ( memcmp(&Sxa[i],&Sxb[i],sizeof(Sxa[i])) != 0 ) + return(-3); + for (i=A->H.data->firsti; iH.data->numunspents; i++) + { + if ( memcmp(&Uxa[i],&Uxb[i],sizeof(Uxa[i])) != 0 ) + return(-4); + //if ( memcmp(&U2a[i],&U2b[i],sizeof(U2a[i])) != 0 ) + // return(-5); + } + for (i=A->H.data->firsti; iH.data->numpkinds; i++) + { + //if ( memcmp(&P2a[i],&P2b[i],sizeof(P2a[i])) != 0 ) + // return(-6); + if ( memcmp(&ACCTa[i],&ACCTb[i],sizeof(ACCTa[i])) != 0 ) + return(-7); + } + for (i=0; iH.data->numexternaltxids; i++) + if ( memcmp(&Xa[i],&Xb[i],sizeof(Xa[i])) != 0 ) + { + bits256_str(str2,Xb[i]); + bits256_str(str,Xa[i]); + printf("X[%d] A.%s B.%s\n",i,str,str2); + return(-8); + } + } + else + { + for (i=A->H.data->firsti; iH.data->numspends; i++) + if ( memcmp(&Sa[i],&Sb[i],sizeof(Sa[i])) != 0 ) + return(-9); + for (i=A->H.data->firsti; iH.data->numunspents; i++) + if ( memcmp(&Ua[i],&Ub[i],sizeof(Ua[i])) != 0 ) + return(-10); + } + } + return(0); + } + printf("cmp %p %p, numblocks %d:%d %d:%d %s %s\n",A->H.data,B->H.data,A->numblocks,A->H.data->numblocks,B->numblocks,B->H.data->numblocks,bits256_str(str,A->H.data->firsthash2),bits256_str(str2,B->H.data->firsthash2)); + return(-1); +} + +int32_t iguana_ramchain_iterate(struct iguana_info *coin,struct iguana_ramchain *dest,struct iguana_ramchain *ramchain) +{ + RAMCHAIN_DECLARE; RAMCHAIN_DESTDECLARE; + int32_t j,hdrsi,prevout; uint32_t sequence,destspendind=0,desttxidind=0; + bits256 prevhash; uint64_t value; uint8_t type; + struct iguana_txid *tx; struct iguana_ramchaindata *rdata; uint8_t *rmd160; struct iguana_unspent *u; + if ( dest != 0 ) + _iguana_ramchain_setptrs(RAMCHAIN_DESTPTRS,dest->H.data); + _iguana_ramchain_setptrs(RAMCHAIN_PTRS,ramchain->H.data); + if ( (rdata= ramchain->H.data) == 0 ) + { + printf("iguana_ramchain_iterate cant iterate without data\n"); + return(-1); + } + ramchain->H.ROflag = 1; + ramchain->H.unspentind = ramchain->H.spendind = ramchain->pkind = rdata->firsti; + ramchain->externalind = 0; + if ( dest != 0 ) + { + desttxidind = dest->H.txidind; + destspendind = dest->H.spendind; + } + for (ramchain->H.txidind=rdata->firsti; ramchain->H.txidindnumtxids; ramchain->H.txidind++) + { + if ( 0 && ramchain->expanded != 0 ) + printf("ITER TXID.%d -> dest.%p desttxid.%d dest->hashmem.%p\n",ramchain->H.txidind,dest,dest!=0?dest->H.txidind:0,dest!=0?dest->hashmem:0); + tx = &T[ramchain->H.txidind]; + if ( iguana_ramchain_addtxid(coin,RAMCHAIN_ARG,tx->txid,tx->numvouts,tx->numvins,tx->locktime,tx->version,tx->timestamp) == 0 ) + return(-1); + if ( dest != 0 ) + { + char str[65]; + if ( 0 && ramchain->expanded != 0 ) + printf("add hdrsi.%d dest.%p txidind.%d %s\n",dest->H.hdrsi,ramchain,dest->H.txidind,bits256_str(str,tx->txid)); + if ( iguana_ramchain_addtxid(coin,RAMCHAIN_DESTARG,tx->txid,tx->numvouts,tx->numvins,tx->locktime,tx->version,tx->timestamp) == 0 ) + return(-2); + } + for (j=0; jnumvouts; j++) + { + if ( 0 && ramchain->expanded != 0 ) + printf("unspentind.%d pkind.%d Ux.%p\n",ramchain->H.unspentind,Ux[ramchain->H.unspentind].pkind,Ux); + if ( ramchain->H.unspentind < rdata->numunspents ) + { + if ( ramchain->expanded != 0 ) + { + u = &Ux[ramchain->H.unspentind]; + value = u->value; + hdrsi = u->hdrsi; + type = u->type; + if ( u->pkind < rdata->numpkinds ) + { + rmd160 = P[u->pkind].rmd160; + if ( iguana_ramchain_addunspent(coin,RAMCHAIN_ARG,value,hdrsi,rmd160,j,type) == 0 ) + return(-3); + } + } + else + { + hdrsi = rdata->hdrsi; + value = U[ramchain->H.unspentind].value; + rmd160 = U[ramchain->H.unspentind].rmd160; + type = U[ramchain->H.unspentind].type; + if ( iguana_ramchain_addunspent20(coin,RAMCHAIN_ARG,value,rmd160,-20,tx->txid,j,type) == 0 ) + return(-4); + } + if ( dest != 0 ) + { + if ( iguana_ramchain_addunspent(coin,RAMCHAIN_DESTARG,value,hdrsi,rmd160,j,type) == 0 ) + return(-5); + } + } else return(-6); + } + ramchain->H.spendind += tx->numvins; + if ( dest != 0 ) + { + dest->H.txidind++; + dest->H.spendind += tx->numvins; + } + } + if ( dest != 0 ) + { + dest->H.txidind = desttxidind; + dest->H.spendind = destspendind; + } + ramchain->H.txidind = ramchain->H.spendind = rdata->firsti; + for (ramchain->H.txidind=rdata->firsti; ramchain->H.txidindnumtxids; ramchain->H.txidind++) + { + tx = &T[ramchain->H.txidind]; + for (j=0; jnumvins; j++) + { + if ( ramchain->expanded != 0 ) + { + sequence = (Sx[ramchain->H.spendind].diffsequence == 0) ? 0xffffffff : 0; + prevout = iguana_ramchain_txid(coin,RAMCHAIN_ARG,&prevhash,&Sx[ramchain->H.spendind]); + //bundlei = Sx[ramchain->H.spendind].bundlei; + //hdrsi = Sx[ramchain->H.spendind].hdrsi; + if ( iguana_ramchain_addspend(coin,RAMCHAIN_ARG,prevhash,prevout,sequence) == 0 ) + { + char str[65]; int32_t i; + printf("txidind.%d spendind.%d spendtxid.%x %d vin.%d %s vout.%d\n",ramchain->H.txidind,ramchain->H.spendind,Sx[ramchain->H.spendind].spendtxidind,Sx[ramchain->H.spendind].spendtxidind&0xfffffff,j,bits256_str(str,prevhash),prevout); + for (i=0; iH.data->numexternaltxids; i++) + printf("%p X[%d] %s\n",X,i,bits256_str(str,X[i])); + exit(-1); + iguana_ramchain_txid(coin,RAMCHAIN_ARG,&prevhash,&Sx[ramchain->H.spendind]); + return(-7); + } + } + else + { + //spendind = (tx->firstvin + j); + sequence = (S[ramchain->H.spendind].diffsequence == 0) ? 0xffffffff : 0; + prevhash = S[ramchain->H.spendind].prevhash2; + prevout = S[ramchain->H.spendind].prevout; + //bundlei = S[ramchain->H.spendind].bundlei; + //hdrsi = S[ramchain->H.spendind].hdrsi; + if ( iguana_ramchain_addspend256(coin,RAMCHAIN_ARG,prevhash,prevout,0,0,sequence) == 0 ) + return(-8); + } + if ( dest != 0 ) + { + if ( iguana_ramchain_addspend(coin,RAMCHAIN_DESTARG,prevhash,prevout,sequence) == 0 ) + return(-9); + } + } + if ( dest != 0 ) + dest->H.txidind++; + } + return(0); +} + +long iguana_ramchain_data(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,int32_t txn_count,uint8_t *data,int32_t recvlen) +{ + int32_t verifyflag = 0; + RAMCHAIN_DECLARE; long fsize; void *ptr; struct iguana_ramchain R,*mapchain,*ramchain = &addr->ramchain; + struct iguana_msgtx *tx; int32_t i,j,fpos,firsti=1,err,flag,bundlei = -2; char fname[1024]; + struct iguana_bundle *bp = 0; struct iguana_block *block; + if ( iguana_bundlefind(coin,&bp,&bundlei,origtxdata->block.RO.hash2) == 0 ) + { + if ( iguana_bundlefind(coin,&bp,&bundlei,origtxdata->block.RO.prev_block) == 0 ) + return(-1); + else if ( bundlei < coin->chain->bundlesize-1 ) + bundlei++; + else + { + printf("error finding block\n"); + return(-1); + } + } + if ( bits256_nonz(bp->hashes[bundlei]) == 0 ) + bp->hashes[bundlei] = origtxdata->block.RO.hash2; + if ( (block= bp->blocks[bundlei]) == 0 ) + { + //char str[65]; printf("%d:%d has no block ptr %s\n",bp->hdrsi,bundlei,bits256_str(str,bp->hashes[bundlei])); + return(-1); + } + if ( block->fpipbits != 0 ) + { + //printf("ramchaindata have %d:%d at %d\n",bp->hdrsi,bundlei,bp->fpos[bundlei]); + return(block->fpos); + } + //SETBIT(bp->recv,bundlei); + fpos = -1; + //bp->recvlens[bundlei] = recvlen; + //bp->firsttxidinds[bundlei] = firsti; + if ( iguana_ramchain_init(ramchain,&addr->TXDATA,&addr->HASHMEM,1,txn_count,origtxdata->numunspents,origtxdata->numspends,0,0,0,1) == 0 ) + return(-1); + iguana_ramchain_link(ramchain,origtxdata->block.RO.hash2,origtxdata->block.RO.hash2,bp->hdrsi,bp->bundleheight+bundlei,bundlei,1,firsti,0); + _iguana_ramchain_setptrs(RAMCHAIN_PTRS,ramchain->H.data); + if ( T == 0 || U == 0 || S == 0 )// P == 0//|| X == 0 || A == 0 || U2 == 0 || P2 == 0 ) + { + printf("fatal error getting txdataptrs\n"); + return(-1); + } + for (i=0; iH.txidind++) + { + tx = &txarray[i]; + iguana_ramchain_addtxid(coin,RAMCHAIN_ARG,tx->txid,tx->tx_out,tx->tx_in,tx->lock_time,tx->version,tx->timestamp); + for (j=0; jtx_out; j++) + { + iguana_ramchain_addunspent20(coin,RAMCHAIN_ARG,tx->vouts[j].value,tx->vouts[j].pk_script,tx->vouts[j].pk_scriptlen,tx->txid,j,0); + } + ramchain->H.spendind += tx->tx_in; + } + ramchain->H.txidind = ramchain->H.spendind = ramchain->H.data->firsti; + for (i=0; iH.txidind++) + { + tx = &txarray[i]; + for (j=0; jtx_in; j++) + { + //char str[65]; printf("PT vin.%d %s vout.%d\n",j,bits256_str(str,tx->vins[j].prev_hash),tx->vins[j].prev_vout); + iguana_ramchain_addspend256(coin,RAMCHAIN_ARG,tx->vins[j].prev_hash,tx->vins[j].prev_vout,tx->vins[j].script,tx->vins[j].scriptlen,tx->vins[j].sequence);//,bp->hdrsi,bundlei); + } + } + //char str[65]; printf("before height.%d num.%d:%d T.%d U.%d S.%d P.%d X.%d %s\n",ramchain->height,ramchain->numblocks,ramchain->H.data->numblocks,ramchain->H.txidind,ramchain->H.unspentind,ramchain->H.spendind,ramchain->pkind,ramchain->externalind,bits256_str(str,ramchain->H.data->firsthash2)); + iguana_ramchain_setsize(ramchain,ramchain->H.data,1); + flag = 0; + if ( ramchain->H.txidind != ramchain->H.data->numtxids || ramchain->H.unspentind != ramchain->H.data->numunspents || ramchain->H.spendind != ramchain->H.data->numspends ) + { + printf("error creating PT ramchain: ramchain->txidind %d != %d ramchain->data->numtxids || ramchain->unspentind %d != %d ramchain->data->numunspents || ramchain->spendind %d != %d ramchain->data->numspends\n",ramchain->H.txidind,ramchain->H.data->numtxids,ramchain->H.unspentind,ramchain->H.data->numunspents,ramchain->H.spendind,ramchain->H.data->numspends); + } + else + { + if ( (err= iguana_ramchain_verify(coin,ramchain)) == 0 ) + { + if ( (fpos= (int32_t)iguana_ramchain_save(coin,RAMCHAIN_ARG,addr->ipbits,origtxdata->block.RO.hash2,origtxdata->block.RO.prev_block,bundlei,0)) >= 0 ) + { + //printf("set fpos.%d\n",fpos); + //bp->ipbits[bundlei] = addr->ipbits; + origtxdata->datalen = (int32_t)ramchain->H.data->allocsize; + ramchain->H.ROflag = 0; + flag = 1; + memset(&R,0,sizeof(R)); + if ( verifyflag != 0 && (mapchain= iguana_ramchain_map(coin,fname,0,1,&R,0,addr->ipbits,origtxdata->block.RO.hash2,origtxdata->block.RO.prev_block,bundlei,fpos,1,0)) != 0 ) + { + //printf("mapped Soffset.%ld\n",(long)mapchain->data->Soffset); + iguana_ramchain_link(&R,origtxdata->block.RO.hash2,origtxdata->block.RO.hash2,bp->hdrsi,bp->bundleheight+bundlei,bundlei,1,firsti,1); + if ( 0 ) // crashes unix + { + if ( (err= iguana_ramchain_cmp(ramchain,mapchain,0)) != 0 ) + printf("error.%d comparing ramchains\n",err); + ptr = mapchain->fileptr; fsize = mapchain->filesize; + mapchain->fileptr = 0, mapchain->filesize = 0; + iguana_ramchain_free(mapchain,1); + memset(&R,0,sizeof(R)); + R.H.data = (void *)((long)ptr + fpos), R.filesize = fsize; + iguana_ramchain_link(&R,origtxdata->block.RO.hash2,origtxdata->block.RO.hash2,bp->hdrsi,bp->bundleheight+bundlei,bundlei,1,firsti,1); + } + if ( (err= iguana_ramchain_cmp(ramchain,&R,0)) != 0 ) + { + fpos = -1; + printf("error.%d comparing REMAP ramchains\n",err); + } + else + { + iguana_ramchain_extras(&R,0); + if ( (err= iguana_ramchain_iterate(coin,0,&R)) != 0 ) + printf("err.%d iterate ",err); + //printf("SUCCESS REMAP\n"); + bp->numtxids += ramchain->H.data->numtxids; + bp->numunspents += ramchain->H.data->numunspents; + bp->numspends += ramchain->H.data->numspends; + } + iguana_ramchain_free(&R,1); + } + else + { + bp->numtxids += ramchain->H.data->numtxids; + bp->numunspents += ramchain->H.data->numunspents; + bp->numspends += ramchain->H.data->numspends; + } + if ( fpos >= 0 ) + block->fpos = fpos, block->fpipbits = addr->ipbits; + } + } else printf("ramchain verification error.%d hdrsi.%d bundlei.%d\n",err,bp->hdrsi,bundlei); + } + ramchain->H.ROflag = 0; + iguana_ramchain_free(ramchain,0); + return(fpos); +} + +// two passes to check data size + +void iguana_ramchain_disp(struct iguana_ramchain *ramchain) +{ + RAMCHAIN_DECLARE; int32_t j; uint32_t txidind,unspentind,spendind; struct iguana_txid *tx; char str[65]; + _iguana_ramchain_setptrs(RAMCHAIN_PTRS,ramchain->H.data); + if ( ramchain->H.data != 0 ) + { + unspentind = spendind = ramchain->H.data->firsti; + for (txidind=ramchain->H.data->firsti; txidindH.data->numtxids; txidind++) + { + tx = &T[txidind]; + for (j=0; jnumvins; j++, spendind++) + printf("%s/v%d ",bits256_str(str,S[spendind].prevhash2),S[spendind].prevout); + for (j=0; jnumvouts; j++,unspentind++) + { + init_hexbytes_noT(str,U[unspentind].rmd160,20); + printf("(%.8f %s) ",dstr(U[unspentind].value),str); + } + printf("txid.[%d] %s (%d:%d %d:%d)\n",txidind,bits256_str(str,tx->txid),tx->firstvout,tx->numvouts,tx->firstvin,tx->numvins); + } + } +} + +int32_t iguana_bundlefiles(struct iguana_info *coin,uint32_t *ipbits,void **ptrs,long *filesizes,struct iguana_bundle *bp) +{ + static bits256 zero; + int32_t j,bundlei,num,hdrsi,checki; struct iguana_block *block; uint32_t fpipbits; char fname[1024]; + for (bundlei=num=0; bundlein; bundlei++) + { + if ( (block= bp->blocks[bundlei]) != 0 ) + fpipbits = block->fpipbits; + else return(0); + if ( num > 0 ) + { + for (j=0; jhashes[bundlei],bundlei>0?bp->hashes[bundlei-1]:zero,1)) != bundlei || bundlei < 0 || bundlei >= coin->chain->bundlesize ) + { + printf("B iguana_ramchain_map.(%s) illegal hdrsi.%d bundlei.%d checki.%d\n",fname,hdrsi,bundlei,checki); + return(0); + } + if ( (ptrs[num]= OS_mapfile(fname,&filesizes[num],0)) == 0 ) + { + printf("error mapping bundlei.%d\n",bundlei); + return(0); + } + //printf("%s mapped ptrs[%d] filesize.%ld bundlei.%d ipbits.%x fpos.%d\n",fname,num,(long)filesizes[num],bundlei,fpipbits,bp->fpos[bundlei]); + num++; + } + } + return(num); +} + +void iguana_bundlemapfree(struct OS_memspace *mem,struct OS_memspace *hashmem,uint32_t *ipbits,void **ptrs,long *filesizes,int32_t num,struct iguana_ramchain *R,int32_t n) +{ + int32_t j; + for (j=0; jH.data->firsthash2, lasthash2 = ramchain->H.data->lasthash2; + height = ramchain->height, firsti = ramchain->H.data->firsti, hdrsi = ramchain->H.hdrsi, numblocks = ramchain->numblocks; + //printf("Apresave T.%d U.%d S.%d P.%d X.%d -> size.%ld firsti.%d\n",ramchain->H.data->numtxids,ramchain->H.data->numunspents,ramchain->H.data->numspends,ramchain->H.data->numpkinds,ramchain->H.data->numexternaltxids,(long)ramchain->H.data->allocsize,firsti); + iguana_ramchain_setsize(ramchain,ramchain->H.data,bp->n); + *newchain = *ramchain; + //memcpy(ramchain->roU2,ramchain->U2,sizeof(*ramchain->U2) * ramchain->H.data->numunspents); + //memcpy(ramchain->roP2,ramchain->P2,sizeof(*ramchain->P2) * ramchain->H.data->numpkinds); + memcpy(ramchain->roA,ramchain->A,sizeof(*ramchain->A) * ramchain->H.data->numpkinds); + memset(ramchain->A,0,sizeof(*ramchain->A) * ramchain->H.data->numpkinds); + //printf("presave T.%d U.%d S.%d P.%d X.%d -> size.%ld firsti.%d\n",ramchain->H.data->numtxids,ramchain->H.data->numunspents,ramchain->H.data->numspends,ramchain->H.data->numpkinds,ramchain->H.data->numexternaltxids,(long)ramchain->H.data->allocsize,firsti); + if ( (err= iguana_ramchain_iterate(coin,0,ramchain)) != 0 ) + printf("ERROR.%d iterating presave ramchain hdrsi.%d\n",err,hdrsi); + else if ( (err= iguana_ramchain_verify(coin,ramchain)) != 0 ) + printf("ERROR.%d verifying presave ramchain hdrsi.%d\n",err,hdrsi); + else retval = 0; + printf("postiterateA T.%d U.%d S.%d P.%d X.%d -> size.%ld firsti.%d\n",ramchain->H.data->numtxids,ramchain->H.data->numunspents,ramchain->H.data->numspends,ramchain->H.data->numpkinds,ramchain->H.data->numexternaltxids,(long)ramchain->H.data->allocsize,firsti); + if ( iguana_ramchain_save(coin,RAMCHAIN_ARG,0,firsthash2,zero,0,bp) < 0 ) + printf("ERROR saving ramchain hdrsi.%d\n",hdrsi); + else + { + //printf("DEST T.%d U.%d S.%d P.%d X.%d -> size.%ld Xoffset.%d\n",ramchain->H.data->numtxids,ramchain->H.data->numunspents,ramchain->H.data->numspends,ramchain->H.data->numpkinds,ramchain->H.data->numexternaltxids,(long)ramchain->H.data->allocsize,(int32_t)ramchain->H.data->Xoffset); + //printf("free dest hdrs.%d retval.%d\n",bp->hdrsi,retval); + memset(&checkR,0,sizeof(checkR)); + bundlei = 0; + if ( cmpflag == 0 ) + iguana_memreset(hashmem); + if ( (mapchain= iguana_ramchain_map(coin,fname,bp,numblocks,&checkR,cmpflag==0?hashmem:0,0,firsthash2,zero,bundlei,0,1,1)) != 0 ) + { + iguana_ramchain_link(mapchain,firsthash2,lasthash2,hdrsi,height,0,numblocks,firsti,1); + iguana_ramchain_extras(mapchain,hashmem); + //printf("MAP T.%d U.%d S.%d P.%d X.%d -> size.%ld Xoffset.%d\n",mapchain->H.data->numtxids,mapchain->H.data->numunspents,mapchain->H.data->numspends,mapchain->H.data->numpkinds,mapchain->H.data->numexternaltxids,(long)mapchain->H.data->allocsize,(int32_t)mapchain->H.data->Xoffset); + if ( (err= iguana_ramchain_iterate(coin,0,mapchain)) != 0 ) + printf("err.%d iterate mapped dest\n",err); + else if ( cmpflag != 0 ) + { + if ( (err= iguana_ramchain_cmp(mapchain,ramchain,0)) != 0 ) + printf("err.%d cmp mapchain.%d vs ramchain\n",err,height); + else + { + printf("BUNDLE.%d iterated and compared\n",height); + retval = 0; + } + } + int32_t i; for (i=0; iH.data->lhashes[i].uints[0]); + printf("%llx ht.%d\n",(long long)mapchain->H.data->sha256.txid,mapchain->height); + iguana_ramchain_free(mapchain,cmpflag); + } + iguana_mempurge(hashmem); + } + return(retval); +} + +struct iguana_ramchain *iguana_bundleload(struct iguana_info *coin,struct iguana_bundle *bp) +{ + static bits256 zero; + struct iguana_blockRO *B; struct iguana_txid *T; int32_t i,firsti = 1; char fname[512]; + struct iguana_block *block; struct iguana_ramchain *mapchain; + memset(&bp->ramchain,0,sizeof(bp->ramchain)); + if ( (mapchain= iguana_ramchain_map(coin,fname,bp,bp->n,&bp->ramchain,0,0,bp->hashes[0],zero,0,0,0,1)) != 0 ) + { + iguana_ramchain_link(mapchain,bp->hashes[0],bp->ramchain.lasthash2,bp->hdrsi,bp->bundleheight,0,bp->ramchain.numblocks,firsti,1); + //char str[65]; printf("bp.%d: T.%d U.%d S.%d P%d X.%d MAPPED %s %p\n",bp->hdrsi,bp->ramchain.H.data->numtxids,bp->ramchain.H.data->numunspents,bp->ramchain.H.data->numspends,bp->ramchain.H.data->numpkinds,bp->ramchain.H.data->numexternaltxids,mbstr(str,bp->ramchain.H.data->allocsize),bp->ramchain.H.data); + B = (void *)((long)mapchain->H.data + mapchain->H.data->Boffset); + T = (void *)((long)mapchain->H.data + mapchain->H.data->Toffset); + for (i=0; in; i++) + { + if ( (block= bp->blocks[i]) != 0 || (block= iguana_blockhashset(coin,bp->bundleheight+i,bp->hashes[i],1)) != 0 ) + { + block->queued = 1; + block->height = bp->bundleheight + i; + block->hdrsi = bp->hdrsi; + block->bundlei = i; + block->fpipbits = (uint32_t)calc_ipbits("127.0.0.1"); + + if ( bp->blocks[i] == 0 ) + bp->blocks[i] = block; + if ( bits256_nonz(bp->hashes[i]) == 0 ) + bp->hashes[i] = B[i].hash2; + _iguana_chainlink(coin,block); + } + } + bp->emitfinish = (uint32_t)time(NULL) + 1; + /*for (i=1; iH.data->numtxids; i++) + {break; + if ( iguana_txidfind(coin,&height,&tx,T[i].txid) == 0 ) + printf("error couldnt find T[%d] %s\n",i,bits256_str(str,T[i].txid)); + else if ( memcmp(&tx,&T[i],sizeof(T[i])) != 0 ) + printf("compare error T[%d] %s\n",i,bits256_str(str,T[i].txid)); + }*/ + } + if ( mapchain != 0 ) + coin->newramchain++; + return(mapchain); +} + +// helper threads: NUM_HELPERS +int32_t iguana_bundlesaveHT(struct iguana_info *coin,struct OS_memspace *mem,struct OS_memspace *memB,struct iguana_bundle *bp,uint32_t starttime) // helper thread +{ + static int depth; static bits256 zero; + RAMCHAIN_DESTDECLARE; RAMCHAIN_DECLARE; + void **ptrs,*ptr; long *filesizes,filesize; uint32_t *ipbits; char fname[1024]; + struct iguana_ramchain *R,*mapchain,*dest,newchain; uint32_t fpipbits,now = (uint32_t)time(NULL); + int32_t numtxids,numunspents,numspends,numpkinds,numexternaltxids,fpos; struct iguana_block *block; + struct OS_memspace HASHMEM; int32_t err,j,num,hdrsi,bundlei,firsti= 1,retval = -1; + B = 0, Ux = 0, Sx = 0, P = 0, A = 0, X = 0, TXbits = 0, PKbits = 0, U = 0, S = 0, T = 0;//U2 = 0, P2 = 0, + R = mycalloc('s',bp->n,sizeof(*R)); + ptrs = mycalloc('w',bp->n,sizeof(*ptrs)); + ipbits = mycalloc('w',bp->n,sizeof(*ipbits)); + filesizes = mycalloc('f',bp->n,sizeof(*filesizes)); + if ( (num= iguana_bundlefiles(coin,ipbits,ptrs,filesizes,bp)) == 0 ) + { + iguana_bundlemapfree(0,0,ipbits,ptrs,filesizes,bp->n,R,bp->n); + return(-1); + } + for (bundlei=numtxids=numunspents=numspends=0; bundlein; bundlei++) + { + if ( (block= bp->blocks[bundlei]) != 0 ) + fpipbits = block->fpipbits, fpos = block->fpos; + else fpipbits = fpos = 0; + mapchain = &R[bundlei]; + for (j=0; jfileptr = ptr; + mapchain->filesize = filesize; + mapchain->H.data = (void *)((long)ptr + fpos); + mapchain->H.ROflag = 1; + if ( fpos+mapchain->H.data->allocsize > filesize || iguana_ramchain_size(MAPCHAIN_ARG,1) != mapchain->H.data->allocsize ) + { + printf("iguana_bundlesaveHT ipbits.%x size mismatch %ld vs %ld vs filesize.%ld fpos.%ld bundlei.%d expanded.%d\n",fpipbits,(long)iguana_ramchain_size(MAPCHAIN_ARG,1),(long)mapchain->H.data->allocsize,(long)filesize,(long)fpos,bundlei,mapchain->expanded); + //getchar(); + break; + } + else if ( memcmp(bp->hashes[bundlei].bytes,mapchain->H.data->firsthash2.bytes,sizeof(bits256)) != 0 ) + { + char str[65],str2[65]; printf("iguana_bundlesaveHT hash2 mismatch %s vs %s\n",bits256_str(str,bp->hashes[bundlei]),bits256_str(str2,mapchain->H.data->firsthash2)); + break; + } + iguana_ramchain_link(mapchain,bp->hashes[bundlei],bp->hashes[bundlei],bp->hdrsi,bp->bundleheight+bundlei,bundlei,1,firsti,1); + numtxids += (mapchain->H.data->numtxids - 1); + numunspents += (mapchain->H.data->numunspents - 1); + numspends += (mapchain->H.data->numspends - 1); + //printf("(%d %d %d) ",numtxids,numunspents,numspends); + //printf("%d ",numtxids); + } + if ( bundlei != bp->n ) + { + iguana_bundlemapfree(0,0,ipbits,ptrs,filesizes,num,R,bp->n); + printf("error mapping hdrsi.%d bundlei.%d\n",bp->hdrsi,bundlei); + return(-1); + } + //printf("-> total (%d %d %d)\n",numtxids,numunspents,numspends); + numpkinds = numunspents; + numexternaltxids = numspends; + dest = &bp->ramchain; + //printf("E.%d depth.%d start bundle ramchain %d at %u started.%u lag.%d\n",coin->numemitted,depth,bp->bundleheight,now,starttime,now-starttime); + depth++; + if ( iguana_ramchain_alloc(coin,dest,mem,&HASHMEM,numtxids,numunspents,numspends,numpkinds,numexternaltxids,bp->bundleheight,bp->n) < 0 ) + { + iguana_bundlemapfree(mem,&HASHMEM,ipbits,ptrs,filesizes,num,R,bp->n); + return(-1); + } + iguana_ramchain_link(dest,bp->hashes[0],bp->hashes[bp->n-1],bp->hdrsi,bp->bundleheight,0,bp->n,firsti,0); + _iguana_ramchain_setptrs(RAMCHAIN_DESTPTRS,dest->H.data); + iguana_ramchain_extras(dest,&HASHMEM); + dest->H.txidind = dest->H.unspentind = dest->H.spendind = dest->pkind = dest->H.data->firsti; + dest->externalind = 0; + //printf("\n"); + for (bundlei=0; bundlein; bundlei++) + { + if ( (block= bp->blocks[bundlei]) != 0 ) + { + iguana_blocksetcounters(coin,block,dest); + coin->blocks.RO[bp->bundleheight+bundlei] = block->RO; + } + //printf("(%d %d) ",R[bundlei].H.data->numtxids,dest->H.txidind); + if ( (err= iguana_ramchain_iterate(coin,dest,&R[bundlei])) != 0 ) + { + printf("error ramchain_iterate hdrs.%d bundlei.%d\n",bp->hdrsi,bundlei); + break; + } + } + depth--; + if ( bundlei == bp->n && iguana_ramchain_expandedsave(coin,RAMCHAIN_DESTARG,&newchain,&HASHMEM,0,bp) == 0 ) + { + char str[65],str2[65]; printf("%s d.%d ht.%d %s saved lag.%d elapsed.%ld\n",bits256_str(str2,newchain.H.data->sha256),depth,dest->height,mbstr(str,dest->H.data->allocsize),now-starttime,time(NULL)-now); + retval = 0; + } + iguana_bundlemapfree(mem,&HASHMEM,ipbits,ptrs,filesizes,num,R,bp->n); + if ( retval == 0 ) + { + //printf("delete %d files hdrs.%d retval.%d\n",num,bp->hdrsi,retval); + for (j=0; jhashes[0],zero,1) >= 0 ) + coin->peers.numfiles -= OS_removefile(fname,0); + else printf("error removing.(%s)\n",fname); + } + } + iguana_ramchain_free(dest,0); + bp->ramchain = newchain; + if ( 0 ) + { + bp->ramchain.hashmem = 0; + bp->ramchain.txids = 0; + bp->ramchain.pkhashes = 0; + bp->ramchain.fileptr = 0; + bp->ramchain.filesize = 0; + } + else iguana_bundleload(coin,bp); + return(retval); +} + +void iguana_mergefree(struct OS_memspace *mem,struct iguana_ramchain *A,struct iguana_ramchain *B,struct OS_memspace *hashmem,struct OS_memspace *hashmemA,struct OS_memspace *hashmemB) +{ + if ( A != 0 ) + iguana_ramchain_free(A,0); + if ( B != 0 ) + iguana_ramchain_free(B,0); + if ( mem != 0 ) + iguana_mempurge(mem); + if ( hashmemA != 0 ) + iguana_mempurge(hashmemA); + if ( hashmemB != 0 ) + iguana_mempurge(hashmemB); +} + +int32_t iguana_bundlemergeHT(struct iguana_info *coin,struct OS_memspace *mem,struct OS_memspace *memB,struct iguana_bundle *bp,struct iguana_bundle *nextbp,uint32_t starttime) +{ + static int32_t depth; static bits256 zero; + RAMCHAIN_DESTDECLARE; struct OS_memspace HASHMEM,HASHMEMA,HASHMEMB; + uint32_t now = (uint32_t)time(NULL); char str[65],fnameA[1024],fnameB[1024]; + struct iguana_ramchain _Achain,_Bchain,*A,*B,R,newchain,*dest = &R; int32_t err,retval = -1,firsti = 1; + memset(mem,0,sizeof(*mem)); + memset(&HASHMEMA,0,sizeof(HASHMEMA)); + iguana_meminit(&HASHMEMA,"hashmemA",0,iguana_hashmemsize(bp->ramchain.H.txidind,bp->ramchain.H.unspentind,bp->ramchain.H.spendind,bp->ramchain.pkind,bp->ramchain.externalind) + 4096,0); + memset(&HASHMEMB,0,sizeof(HASHMEMB)); + iguana_meminit(&HASHMEMB,"hashmemB",0,iguana_hashmemsize(nextbp->ramchain.H.txidind,nextbp->ramchain.H.unspentind,nextbp->ramchain.H.spendind,nextbp->ramchain.pkind,nextbp->ramchain.externalind) + 4096,0); + memset(&_Achain,0,sizeof(_Achain)); A = &_Achain; + memset(&_Bchain,0,sizeof(_Bchain)); B = &_Bchain; + if ( (A= iguana_ramchain_map(coin,fnameA,bp,bp->ramchain.numblocks,A,&HASHMEMA,0,bp->hashes[0],zero,0,0,1,1)) != 0 ) + { + iguana_ramchain_link(A,bp->hashes[0],bp->ramchain.lasthash2,bp->hdrsi,bp->bundleheight,0,bp->ramchain.numblocks,firsti,1); + } + if ( (B= iguana_ramchain_map(coin,fnameB,bp,nextbp->ramchain.numblocks,B,&HASHMEMB,0,nextbp->hashes[0],zero,0,0,1,1)) != 0 ) + { + iguana_ramchain_link(B,bp->hashes[0],nextbp->ramchain.lasthash2,nextbp->hdrsi,nextbp->bundleheight,0,nextbp->ramchain.numblocks,firsti,1); + } + if ( A == 0 || B == 0 || A->H.data == 0 || B->H.data == 0 || (A->H.data->allocsize + B->H.data->allocsize) > IGUANA_MAXRAMCHAINSIZE ) + { + printf("MERGE error %d[%d] %d[%d]\n",A->height,A->numblocks,B->height,B->numblocks); + iguana_mergefree(mem,A,B,&HASHMEM,&HASHMEMA,&HASHMEMB); + return(-1); + } + if ( A->H.data != 0 && B->H.data != 0 && B->height == A->height+A->numblocks ) + { + if ( iguana_ramchain_alloc(coin,dest,mem,&HASHMEM,(A->H.data->numtxids+B->H.data->numtxids),(A->H.data->numunspents+B->H.data->numunspents),(A->H.data->numspends+B->H.data->numspends),(A->H.data->numpkinds+B->H.data->numpkinds),(A->H.data->numexternaltxids+B->H.data->numexternaltxids),A->height,A->numblocks + B->numblocks) < 0 ) + { + printf("depth.%d ht.%d fsize.%s ERROR alloc lag.%d elapsed.%ld\n",depth,dest->height,mbstr(str,dest->H.data->allocsize),now-starttime,time(NULL)-now); + iguana_mergefree(mem,A,B,&HASHMEM,&HASHMEMA,&HASHMEMB); + return(-1); + } + depth++; + iguana_ramchain_link(dest,A->H.data->firsthash2,B->H.data->lasthash2,A->H.hdrsi,A->height,0,A->numblocks+B->numblocks,firsti,0); + _iguana_ramchain_setptrs(RAMCHAIN_DESTPTRS,dest->H.data); + iguana_ramchain_extras(dest,&HASHMEM); + dest->H.txidind = dest->H.unspentind = dest->H.spendind = dest->pkind = dest->H.data->firsti; + dest->externalind = 0; + if ( (err= iguana_ramchain_iterate(coin,dest,A)) != 0 ) + printf("error.%d ramchain_iterate A.%d\n",err,A->height); + else if ( (err= iguana_ramchain_iterate(coin,dest,B)) != 0 ) + printf("error.%d ramchain_iterate B.%d\n",err,B->height); + else if ( iguana_ramchain_expandedsave(coin,RAMCHAIN_DESTARG,&newchain,&HASHMEM,0,0) == 0 ) + { + printf("merging isnt setup to save the blockROs\n"); + printf("depth.%d ht.%d fsize.%s MERGED %d[%d] and %d[%d] lag.%d elapsed.%ld bp.%d -> %d\n",depth,dest->height,mbstr(str,dest->H.data->allocsize),A->height,A->numblocks,B->height,B->numblocks,now-starttime,time(NULL)-now,bp->bundleheight,nextbp->bundleheight); + iguana_mergefree(mem,A,B,&HASHMEM,&HASHMEMA,&HASHMEMB); + bp->mergefinish = 0; + nextbp->mergefinish = (uint32_t)time(NULL); + bp->nextbp = nextbp->nextbp; + newchain.hashmem = 0; + retval = 0; + nextbp->ramchain = bp->ramchain = newchain; + OS_removefile(fnameA,0); + OS_removefile(fnameB,0); + } + else + { + bp->mergefinish = nextbp->mergefinish = 0; + iguana_mergefree(mem,A,B,&HASHMEM,&HASHMEMA,&HASHMEMB); + } + iguana_ramchain_free(dest,0); + depth--; + } else printf("error merging A.%d [%d] and B.%d [%d]\n",A->height,A->numblocks,B->height,B->numblocks); + coin->merging--; + return(retval); +} + +void iguana_ramchainmerge(struct iguana_info *coin) // jl777: verify prev/next hash2 +{ + struct iguana_bundle *bp,*nextbp,*A,*B; int64_t total = 0; int32_t n,flag = 0; + if ( coin->bundlescount <= 0 || coin->merging > 0 ) + return; + A = B = 0; + n = 0; + bp = coin->bundles[0]; + while ( bp != 0 && (nextbp= bp->nextbp) != 0 ) + { + n++; + if ( nextbp != 0 && bp != 0 && bp->emitfinish > coin->startutc && nextbp->emitfinish > coin->startutc && bp->mergefinish == 0 && nextbp->mergefinish == 0 && bp->ramchain.datasize + nextbp->ramchain.datasize < IGUANA_MAXRAMCHAINSIZE ) + { + if ( total == 0 || (bp->ramchain.datasize + nextbp->ramchain.datasize) < total ) + { + total = (bp->ramchain.datasize + nextbp->ramchain.datasize); + A = bp, B = nextbp; + } + } + bp = nextbp; + } + if ( A != 0 && B != 0 ) + { + bp = A, nextbp = B; + bp->mergefinish = nextbp->mergefinish = 1; + flag++; + char str[65]; printf("start merge %d[%d] + %d[%d] %s\n",bp->bundleheight,bp->ramchain.numblocks,nextbp->bundleheight,nextbp->ramchain.numblocks,mbstr(str,bp->ramchain.datasize + nextbp->ramchain.datasize)); + coin->merging++; + iguana_mergeQ(coin,bp,nextbp); + } + if ( flag != 0 ) + { + bp = coin->bundles[0]; + while ( bp != 0 && (nextbp= bp->nextbp) != 0 ) + { + printf("%d[%d].%d ",bp->bundleheight,bp->ramchain.numblocks,bp->mergefinish); + bp = nextbp; + } + printf("bundles.%d\n",n); + } +} diff --git a/iguana/iguana_recv.c b/iguana/iguana_recv.c new file mode 100755 index 000000000..417c38193 --- /dev/null +++ b/iguana/iguana_recv.c @@ -0,0 +1,878 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +// peer context, ie massively multithreaded -> bundlesQ + +struct iguana_bundlereq *iguana_bundlereq(struct iguana_info *coin,struct iguana_peer *addr,int32_t type,int32_t datalen) +{ + struct iguana_bundlereq *req; int32_t allocsize; + allocsize = (uint32_t)sizeof(*req) + datalen; + req = mycalloc(type,1,allocsize); + req->allocsize = allocsize; + req->datalen = datalen; + req->addr = addr; + req->coin = coin; + req->type = type; + return(req); +} + +/*struct iguana_block *iguana_blockrequest(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,uint32_t now,int32_t iamthreadsafe) +{ + struct iguana_block *block = 0; + if( bp != 0 && bundlei >= 0 && bundlei < bp->n ) + block = bp->blocks[bundlei]; + if ( block == 0 && iamthreadsafe != 0 ) + block = iguana_blockfind(coin,hash2); + if ( block != 0 ) + { + //block->issued = now; + if ( block->numrequests < 100 ) + block->numrequests++; + } + return(block); +}*/ + +int32_t iguana_sendblockreq(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t iamthreadsafe) +{ + int32_t len; uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)]; + char hexstr[65]; init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2)); + if ( (len= iguana_getdata(coin,serialized,MSG_BLOCK,hexstr)) > 0 ) + { + iguana_send(coin,addr,serialized,len); + coin->numreqsent++; + addr->pendblocks++; + addr->pendtime = (uint32_t)time(NULL); + //iguana_blockrequest(coin,bp,bundlei,hash2,addr->pendtime,iamthreadsafe); + //printf("REQ.%s bundlei.%d hdrsi.%d\n",bits256_str(hexstr,hash2),bundlei,bp!=0?bp->hdrsi:-1); + } else printf("MSG_BLOCK null datalen.%d\n",len); + return(len); +} + +int32_t iguana_sendtxidreq(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2) +{ + uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)]; + int32_t len,i,r,j; char hexstr[65]; init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2)); + if ( (len= iguana_getdata(coin,serialized,MSG_TX,hexstr)) > 0 ) + { + if ( addr == 0 ) + { + r = rand(); + for (i=0; iMAXPEERS; i++) + { + j = (i + r) % coin->MAXPEERS; + addr = &coin->peers.active[j]; + if ( coin->peers.active[j].usock >= 0 && coin->peers.active[j].dead == 0 ) + { + iguana_send(coin,addr,serialized,len); + break; + } + } + } else iguana_send(coin,addr,serialized,len); + } else printf("MSG_TX null datalen.%d\n",len); + printf("send MSG_TX.%d\n",len); + return(len); +} + +int32_t iguana_txidreq(struct iguana_info *coin,char **retstrp,bits256 txid) +{ + int32_t i; + while ( coin->numreqtxids >= sizeof(coin->reqtxids)/sizeof(*coin->reqtxids) ) + { + printf("txidreq full, wait\n"); + sleep(1); + } + char str[65]; printf("txidreq.%s\n",bits256_str(str,txid)); + coin->reqtxids[coin->numreqtxids++] = txid; + for (i=0; iMAXPEERS; i++) + if ( coin->peers.active[i].usock >= 0 ) + iguana_sendtxidreq(coin,coin->peers.ranked[i],txid); + return(0); +} + +void iguana_gotunconfirmedM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgtx *tx,uint8_t *data,int32_t datalen) +{ + struct iguana_bundlereq *req; + char str[65]; printf("%s unconfirmed.%s\n",addr->ipaddr,bits256_str(str,tx->txid)); + req = iguana_bundlereq(coin,addr,'U',datalen); + req->datalen = datalen; + req->txid = tx->txid; + memcpy(req->serialized,data,datalen); + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gotblockM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,struct iguana_msghdr *H,uint8_t *data,int32_t recvlen) +{ + struct iguana_bundlereq *req; struct iguana_txblock *txdata = 0; int32_t i,j,copyflag; char fname[1024]; + if ( 0 ) + { + for (i=0; ispace[0]; i++) + if ( txdata->space[i] != 0 ) + break; + if ( i != txdata->space[0] ) + { + for (i=0; ispace[0]; i++) + printf("%02x ",txdata->space[i]); + printf("extra\n"); + } + } + if ( coin->numreqtxids > 0 ) + { + for (i=0; iblock.RO.txn_count; i++) + { + for (j=0; jnumreqtxids; j++) + { + if ( memcmp(coin->reqtxids[j].bytes,txarray[i].txid.bytes,sizeof(bits256)) == 0 ) + { + char str[65]; printf("i.%d j.%d found txid.%s\n",i,j,bits256_str(str,coin->reqtxids[j])); + } + } + } + } + copyflag = 1 * (strcmp(coin->symbol,"BTC") != 0); + req = iguana_bundlereq(coin,addr,'B',copyflag * recvlen); + req->recvlen = recvlen; + if ( copyflag != 0 && recvlen != 0 ) + { + //printf("copy %p serialized[%d]\n",req->serialized,req->recvlen); + req->H = *H; + memcpy(req->serialized,data,recvlen), req->copyflag = 1; + } + txdata = origtxdata; + if ( addr != 0 ) + { + if ( addr->pendblocks > 0 ) + addr->pendblocks--; + addr->lastblockrecv = (uint32_t)time(NULL); + addr->recvblocks += 1.; + addr->recvtotal += recvlen; + if ( iguana_ramchain_data(coin,addr,origtxdata,txarray,origtxdata->block.RO.txn_count,data,recvlen) >= 0 ) + { + txdata->block.fpipbits = addr->ipbits; + req->datalen = txdata->datalen; + req->ipbits = txdata->block.fpipbits; + if ( 0 ) + { + struct iguana_txblock *checktxdata; struct OS_memspace checkmem; int32_t checkbundlei; + memset(&checkmem,0,sizeof(checkmem)); + iguana_meminit(&checkmem,"checkmem",0,txdata->datalen + 4096,0); + if ( (checktxdata= iguana_peertxdata(coin,&checkbundlei,fname,&checkmem,addr->ipbits,txdata->block.RO.hash2)) != 0 ) + { + printf("check datalen.%d bundlei.%d T.%d U.%d S.%d P.%d X.%d\n",checktxdata->datalen,checkbundlei,checktxdata->numtxids,checktxdata->numunspents,checktxdata->numspends,checktxdata->numpkinds,checktxdata->numexternaltxids); + } + iguana_mempurge(&checkmem); + } + } + } + //printf("recvlen.%d\n",req->recvlen); + req->block = txdata->block; + req->block.RO.txn_count = req->numtx = txdata->block.RO.txn_count; + coin->recvcount++; + coin->recvtime = (uint32_t)time(NULL); + req->addr = addr; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gottxidsM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *txids,int32_t n) +{ + struct iguana_bundlereq *req; + printf("got %d txids from %s\n",n,addr->ipaddr); + req = iguana_bundlereq(coin,addr,'T',0); + req->hashes = txids, req->n = n; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n) +{ + struct iguana_bundlereq *req; + if ( addr != 0 ) + { + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + //printf("%s blocks[%d] ht.%d gotheaders pend.%d %.0f\n",addr->ipaddr,n,blocks[0].height,addr->pendhdrs,milliseconds()); + } + req = iguana_bundlereq(coin,addr,'H',0); + req->blocks = blocks, req->n = n; + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashes,int32_t n) +{ + struct iguana_bundlereq *req; + if ( addr != 0 ) + { + addr->recvhdrs++; + if ( addr->pendhdrs > 0 ) + addr->pendhdrs--; + } + req = iguana_bundlereq(coin,addr,'S',0); + req->hashes = blockhashes, req->n = n; + //printf("bundlesQ blockhashes.%p[%d]\n",blockhashes,n); + queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0); +} + +void iguana_patch(struct iguana_info *coin,struct iguana_block *block) +{ + int32_t i,j,origheight,height; struct iguana_block *prev,*next; struct iguana_bundle *bp; + prev = iguana_blockhashset(coin,-1,block->RO.prev_block,1); + block->hh.prev = prev; + if ( prev != 0 ) + { + if ( prev->mainchain != 0 ) + { + prev->hh.next = block; + if ( memcmp(block->RO.prev_block.bytes,coin->blocks.hwmchain.RO.hash2.bytes,sizeof(bits256)) == 0 ) + { + _iguana_chainlink(coin,block); + //printf("link block %d\n",block->height); + } + if ( (next= block->hh.next) != 0 && bits256_nonz(next->RO.hash2) > 0 ) + { + next->height = block->height + 1; + //printf("autoreq %d\n",next->height); + if ( 0 && strcmp(coin->symbol,"BTC") != 0 ) + iguana_blockQ(coin,coin->bundles[(block->height+1)/coin->chain->bundlesize],(block->height+1)%coin->chain->bundlesize,next->RO.hash2,0); + } + } + else if ( block->height < 0 ) + { + for (i=0; i<1; i++) + { + if ( (prev= prev->hh.prev) == 0 ) + break; + if ( prev->mainchain != 0 && prev->height >= 0 ) + { + j = i; + origheight = (prev->height + i + 2); + prev = block->hh.prev; + height = (origheight - 1); + while ( i > 0 && prev != 0 ) + { + if ( prev->mainchain != 0 && prev->height != height ) + { + printf("mainchain height mismatch j.%d at i.%d %d != %d\n",j,i,prev->height,height); + break; + } + prev = prev->hh.prev; + height--; + } + if ( i == 0 ) + { + //printf("SET HEIGHT.%d j.%d\n",origheight,j); + if ( (bp= coin->bundles[origheight / coin->chain->bundlesize]) != 0 ) + { + iguana_bundlehash2add(coin,0,bp,origheight % coin->chain->bundlesize,block->RO.hash2); + block->height = origheight; + block->mainchain = 1; + prev = block->hh.prev; + prev->hh.next = block; + } + } //else printf("break at i.%d for j.%d origheight.%d\n",i,j,origheight); + break; + } + } + } + } +} + +int32_t iguana_allhashcmp(struct iguana_info *coin,struct iguana_bundle *bp,bits256 *blockhashes,int32_t num) +{ + bits256 allhash; int32_t err,i,n; struct iguana_block *block; + if ( bits256_nonz(bp->allhash) > 0 && num >= coin->chain->bundlesize ) + { + for (i=0; in; i++) + if ( bits256_nonz(blockhashes[i]) == 0 ) + blockhashes[i] = bp->hashes[i]; + vcalc_sha256(0,allhash.bytes,blockhashes[0].bytes,coin->chain->bundlesize * sizeof(*blockhashes)); + if ( memcmp(allhash.bytes,bp->allhash.bytes,sizeof(allhash)) == 0 ) + { + for (i=n=0; ichain->bundlesize&&in; i++) + { + if ( (err= iguana_bundlehash2add(coin,0,bp,i,blockhashes[i])) < 0 ) + return(err); + if ( bp->emitfinish == 0 && (block= bp->blocks[i]) != 0 && (block->queued == 0 && block->fpipbits == 0) && block->numrequests < 3 ) + iguana_blockQ(coin,bp,i,block->RO.hash2,0), n++; + } + printf("ALLHASHES FOUND! %d requested.%d\n",bp->bundleheight,n); + return(i); + } + } + return(-1); +} + +// main context, ie single threaded +struct iguana_bundle *iguana_bundleset(struct iguana_info *coin,struct iguana_block **blockp,int32_t *bundleip,struct iguana_block *origblock) +{ + struct iguana_block *block; bits256 zero; struct iguana_bundle *bp = 0; + int32_t bundlei = -2; + *bundleip = -2; *blockp = 0; + if ( origblock == 0 ) + return(0); + memset(zero.bytes,0,sizeof(zero)); + if ( (block= iguana_blockhashset(coin,-1,origblock->RO.hash2,1)) != 0 ) + { + if ( block != origblock ) + iguana_blockcopy(coin,block,origblock); + *blockp = block; + //if ( bits256_nonz(block->RO.prev_block) > 0 ) + // iguana_patch(coin,block); + if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,block->RO.hash2)) != 0 ) + { + if ( bundlei < coin->chain->bundlesize ) + { + block->bundlei = bundlei; + block->hdrsi = bp->hdrsi; + //iguana_hash2set(coin,"blockadd",bp,block->bundlei,block->hash2); + iguana_bundlehash2add(coin,0,bp,bundlei,block->RO.hash2); + if ( bundlei > 0 ) + { + //char str[65],str2[65]; printf("call hash2add %d:[%d -1] %s prev.%s\n",bp->hdrsi,bundlei,bits256_str(str2,block->RO.hash2),bits256_str(str,block->RO.prev_block)); + iguana_bundlehash2add(coin,0,bp,bundlei-1,block->RO.prev_block); + } + else if ( bp->hdrsi > 0 && (bp= coin->bundles[bp->hdrsi-1]) != 0 ) + iguana_bundlehash2add(coin,0,bp,coin->chain->bundlesize-1,block->RO.prev_block); + } + } + if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,block->RO.prev_block)) != 0 ) + { + //printf("found prev.%d\n",bp->bundleheight+bundlei); + if ( bundlei < coin->chain->bundlesize ) + { + if ( bundlei == coin->chain->bundlesize-1 ) + { + //if ( coin->bundlescount < bp->hdrsi+1 ) + { + //char str[65]; printf("autoextend CREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,block->RO.hash2)); + iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,block->RO.hash2,zero,1); + } + } + else if ( bundlei < coin->chain->bundlesize-1 ) + iguana_bundlehash2add(coin,0,bp,bundlei+1,block->RO.hash2); + } + } + //char str[65]; printf("iguana_recvblock (%s) %d %d[%d] %p\n",bits256_str(str,block->hash2),block->havebundle,block->hdrsi,bundlei,bp); + } else printf("iguana_bundleset: error adding blockhash\n"); + return(iguana_bundlefind(coin,&bp,bundleip,origblock->RO.hash2)); +} + +struct iguana_bundlereq *iguana_recvblockhdrs(struct iguana_info *coin,struct iguana_bundlereq *req,struct iguana_block *blocks,int32_t n,int32_t *newhwmp) +{ + int32_t i,bundlei; struct iguana_block *block; struct iguana_bundle *bp; + if ( blocks == 0 ) + { + printf("iguana_recvblockhdrs null blocks?\n"); + return(req); + } + if ( blocks != 0 && n > 0 ) + { + for (i=0; ihdrsi < IGUANA_MAXACTIVEBUNDLES ) + { + //if ( 0 && i < bp->n && bp->requests[i] == 0 ) + // iguana_blockQ(coin,bp,bundlei,blocks[i].RO.hash2,0); + } + } + } + return(req); +} + +struct iguana_bundlereq *iguana_recvblockhashes(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *blockhashes,int32_t num) +{ + int32_t bundlei,i,n = 0; struct iguana_block *block; struct iguana_bundle *bp;// char str[65]; + bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,blockhashes[1]); + if ( bp != 0 ) + { + blockhashes[0] = bp->hashes[0]; + if ( num >= coin->chain->bundlesize ) + { + bp->hdrtime = (uint32_t)time(NULL); + if ( iguana_allhashcmp(coin,bp,blockhashes,num) == 0 ) + return(req); + } + for (i=0; i 0 && (block= iguana_blockhashset(coin,-1,blockhashes[i],1)) != 0 ) + { + if ( block->hdrsi == bp->hdrsi && block->bundlei == i ) + n++; + } + } + //printf("got [%d] num.%d matched hashes\n",n,num); + } + else + { + //char str[65]; printf("blockhashes[%d] %s\n",num,bits256_str(str,blockhashes[1])); + iguana_blockQ(coin,0,-1,blockhashes[1],1); + } + //iguana_blockQ(coin,0,-1,blockhashes[num-1],1); + /*if ( (block= iguana_blockhashset(coin,-1,blockhashes[1],1)) != 0 && num > 2 ) + { + if ( block->rawdata != 0 ) + { + if ( block->copyflag != 0 ) + myfree(block->rawdata,block->RO.recvlen), block->copyflag = 0; + else myfree(block->rawdata,block->numhashes * sizeof(bits256)); + } + //char str[65]; printf("got %d unmatched hashes %d:%d %s\n",num,bp==0?-1:bp->bundleheight,bundlei,bits256_str(str,blockhashes[1])); + block->rawdata = blockhashes, block->numhashes = num, block->havehashes = 1; + req->hashes = 0; + } + if ( 0 && num >= coin->chain->bundlesize+1 ) + { + char str[65]; bits256_str(str,blockhashes[coin->chain->bundlesize]); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1); + }*/ + return(req); +} + +struct iguana_bundlereq *iguana_recvblock(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundlereq *req,struct iguana_block *origblock,int32_t numtx,int32_t datalen,int32_t recvlen,int32_t *newhwmp) +{ + struct iguana_bundle *prevbp=0,*bp=0; int32_t prevbundlei=-2,bundlei = -2; struct iguana_block *prevblock,*block; + bp = iguana_bundleset(coin,&block,&bundlei,origblock); + //char str[65]; printf("RECV %s [%d:%d] block.%p\n",bits256_str(str,origblock->RO.hash2),bp!=0?bp->hdrsi:-1,bundlei,block); + iguana_bundlefind(coin,&prevbp,&prevbundlei,origblock->RO.prev_block); + if ( prevbp != 0 && prevbundlei >= 0 && prevbp->blocks[prevbundlei] == 0 && (prevblock= iguana_blockfind(coin,origblock->RO.prev_block)) != 0 ) + { + prevbp->blocks[prevbundlei] = prevblock; + //printf("PREV %s prevbp.%p[%d]\n",bits256_str(str,origblock->RO.prev_block),prevbp,prevbundlei); + } + if ( block != 0 ) + { + if ( bp != 0 && bundlei >= 0 ) + bp->blocks[bundlei] = block; + block->RO.recvlen = recvlen; + if ( req->copyflag != 0 && block->queued == 0 )//block->rawdata == 0 ) + { + //char str[65]; printf("%s copyflag.%d %d data %d %d\n",bits256_str(str,block->RO.hash2),req->copyflag,block->height,req->recvlen,recvlen); + //block->rawdata = mycalloc('n',1,block->RO.recvlen); + //memcpy(block->rawdata,req->serialized,block->RO.recvlen); + //block->copyflag = 1; + coin->numcached++; + block->queued = 1; + queue_enqueue("cacheQ",&coin->cacheQ,&req->DL,0); + return(0); + } + //printf("datalen.%d ipbits.%x\n",datalen,req->ipbits); + } else printf("cant create block.%llx block.%p bp.%p bundlei.%d\n",(long long)origblock->RO.hash2.txid,block,bp,bundlei); + return(req); +} + +struct iguana_bundlereq *iguana_recvtxids(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *txids,int32_t n) +{ + return(req); +} + +struct iguana_bundlereq *iguana_recvunconfirmed(struct iguana_info *coin,struct iguana_bundlereq *req,uint8_t *data,int32_t datalen) +{ + int32_t i; + for (i=0; inumreqtxids; i++) + { + if ( memcmp(req->txid.bytes,coin->reqtxids[i].bytes,sizeof(req->txid)) == 0 ) + { + char str[65]; printf("got reqtxid.%s datalen.%d | numreqs.%d\n",bits256_str(str,req->txid),req->datalen,coin->numreqtxids); + coin->reqtxids[i] = coin->reqtxids[--coin->numreqtxids]; + } + } + return(req); +} + +int32_t iguana_processbundlesQ(struct iguana_info *coin,int32_t *newhwmp) // single threaded +{ + int32_t flag = 0; struct iguana_bundlereq *req; + *newhwmp = 0; + while ( flag < IGUANA_BUNDLELOOP && (req= queue_dequeue(&coin->bundlesQ,0)) != 0 ) + { + //printf("%s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n); + if ( req->type == 'B' ) // one block with all txdata + req = iguana_recvblock(coin,req->addr,req,&req->block,req->numtx,req->datalen,req->recvlen,newhwmp); + else if ( req->type == 'H' ) // blockhdrs (doesnt have txn_count!) + { + if ( (req= iguana_recvblockhdrs(coin,req,req->blocks,req->n,newhwmp)) != 0 ) + { + if ( req->blocks != 0 ) + myfree(req->blocks,sizeof(*req->blocks) * req->n), req->blocks = 0; + } + } + else if ( req->type == 'S' ) // blockhashes + { + if ( (req= iguana_recvblockhashes(coin,req,req->hashes,req->n)) != 0 && req->hashes != 0 ) + myfree(req->hashes,sizeof(*req->hashes) * req->n), req->hashes = 0; + } + else if ( req->type == 'U' ) // unconfirmed tx + req = iguana_recvunconfirmed(coin,req,req->serialized,req->datalen); + else if ( req->type == 'T' ) // txids from inv + { + if ( (req= iguana_recvtxids(coin,req,req->hashes,req->n)) != 0 ) + myfree(req->hashes,(req->n+1) * sizeof(*req->hashes)), req->hashes = 0; + } + else printf("iguana_updatebundles unknown type.%c\n",req->type); + flag++; + //printf("done %s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n); + if ( req != 0 ) + myfree(req,req->allocsize), req = 0; + } + return(flag); +} + +int32_t iguana_needhdrs(struct iguana_info *coin) +{ + if ( coin->longestchain == 0 || coin->blocks.hashblocks < coin->longestchain-coin->chain->bundlesize ) + return(1); + else return(0); +} + +int32_t iguana_reqhdrs(struct iguana_info *coin) +{ + int32_t i,lag,n = 0; struct iguana_bundle *bp; char hashstr[65]; + if ( iguana_needhdrs(coin) > 0 && queue_size(&coin->hdrsQ) == 0 ) + { + if ( coin->zcount++ > 1 ) + { + for (i=0; ibundlescount; i++) + { + if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish < coin->startutc ) + { + if ( i == coin->bundlescount-1 ) + lag = 5; + else lag = 30 + (rand() % 30); + if ( i < coin->bundlescount-1 && (bp->numhashes >= (rand() % bp->n) || time(NULL) < bp->hdrtime+lag) ) + continue; + if ( bp->numhashes < bp->n && bp->bundleheight+bp->numhashes < coin->longestchain && time(NULL) > bp->issuetime+lag ) + { + //printf("LAG.%ld hdrsi.%d numhashes.%d:%d needhdrs.%d qsize.%d zcount.%d\n",time(NULL)-bp->hdrtime,i,bp->numhashes,bp->n,iguana_needhdrs(coin),queue_size(&coin->hdrsQ),coin->zcount); + if ( bp->issuetime == 0 ) + coin->numpendings++; + char str[65]; + bits256_str(str,bp->hashes[0]); + //printf("(%s %d).%d ",str,bp->bundleheight,i); + printf("%d ",bp->bundleheight); + init_hexbytes_noT(hashstr,bp->hashes[0].bytes,sizeof(bits256)); + queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1); + /*if ( strcmp(coin->symbol,"BTC") != 0 && bits256_nonz(bp->hashes[1]) > 0 ) + { + if ( (block= iguana_blockfind(coin,bp->hashes[1])) != 0 ) + { + if ( block->havehashes != 0 && block->rawdata != 0 ) + iguana_allhashcmp(coin,bp,block->rawdata,block->numhashes); + //iguana_blockQ(coin,bp,1,bp->hashes[1],1); + } + }*/ + n++; + bp->hdrtime = bp->issuetime = (uint32_t)time(NULL); + } + } + } + if ( n > 0 ) + printf("REQ HDRS pending.%d\n",n); + coin->zcount = 0; + } + } else coin->zcount = 0; + return(n); +} + +struct iguana_blockreq { struct queueitem DL; bits256 hash2,*blockhashes; struct iguana_bundle *bp; int32_t n,height,bundlei; }; + +int32_t iguana_blockQ(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t priority) +{ + queue_t *Q; char *str; struct iguana_blockreq *req; struct iguana_block *block = 0; + if ( bits256_nonz(hash2) == 0 ) + { + printf("cant queue zerohash bundlei.%d\n",bundlei); + return(-1); + } + if ( (bp != 0 && (block= iguana_blockfind(coin,bp->hashes[bundlei])) == 0) || priority != 0 || bp == 0 ) + { + if ( block != 0 ) + { + if ( block->fpipbits != 0 || block->queued != 0 ) + return(0); + /*if ( block->rawdata != 0 && block->RO.recvlen != 0 ) + { + printf("free cached copy recvlen.%d need to process it here\n",block->RO.recvlen); + myfree(block->rawdata,block->RO.recvlen); + block->rawdata = 0; + block->RO.recvlen = 0; + }*/ + } + if ( priority != 0 ) + str = "priorityQ", Q = &coin->priorityQ; + else str = "blocksQ", Q = &coin->blocksQ; + if ( Q != 0 ) + { + req = mycalloc('r',1,sizeof(*req)); + req->hash2 = hash2; + req->bp = bp; + req->bundlei = bundlei; + if ( bp != 0 && bundlei >= 0 && bundlei < bp->n ) + { + //bp->issued[bundlei] = (uint32_t)time(NULL); + if ( bp->bundleheight >= 0 ) + req->height = (bp->bundleheight + bundlei); + } + char str[65]; + bits256_str(str,hash2); + if ( 0 && (bundlei % 250) == 0 ) + printf("%s %d %s recv.%d numranked.%d qsize.%d\n",str,req->height,str,coin->blocks.recvblocks,coin->peers.numranked,queue_size(Q)); + queue_enqueue(str,Q,&req->DL,0); + return(1); + } else printf("null Q\n"); + } //else printf("queueblock skip priority.%d bundlei.%d\n",bundlei,priority); + return(0); +} + +int32_t iguana_pollQsPT(struct iguana_info *coin,struct iguana_peer *addr) +{ + uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)]; + char *hashstr=0; bits256 hash2; uint32_t now; struct iguana_block *block; struct iguana_blockreq *req=0; + int32_t i,r,diff,j,k,n,m; double metric,bestmetric = -1.; struct iguana_bundle *bp,*bestbp = 0; + int32_t limit,refbundlei,height=-1,incr,datalen,flag = 0; double val; + now = (uint32_t)time(NULL); + if ( iguana_needhdrs(coin) != 0 && addr->pendhdrs < IGUANA_MAXPENDHDRS ) + { + //printf("%s check hdrsQ\n",addr->ipaddr); + if ( (hashstr= queue_dequeue(&coin->hdrsQ,1)) != 0 ) + { + if ( (datalen= iguana_gethdrs(coin,serialized,coin->chain->gethdrsmsg,hashstr)) > 0 ) + { + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + if ( bits256_nonz(hash2) > 0 ) + { + //printf("%s request hdr.(%s)\n",addr!=0?addr->ipaddr:"local",hashstr); + iguana_send(coin,addr,serialized,datalen); + addr->pendhdrs++; + flag++; + } + free_queueitem(hashstr); + return(flag); + } else printf("datalen.%d from gethdrs\n",datalen); + free_queueitem(hashstr); + hashstr = 0; + } + } + if ( (limit= addr->recvblocks) > coin->MAXPENDING ) + limit = coin->MAXPENDING; + if ( limit < 1 ) + limit = 1; + //if ( addr->pendblocks >= limit ) + // printf("%s %d overlimit.%d\n",addr->ipaddr,addr->pendblocks,limit); + if ( coin->bundlescount > 0 && (req= queue_dequeue(&coin->priorityQ,0)) == 0 && addr->pendblocks < limit )//&& now > addr->lastpoll ) + { + if ( 1 )//strcmp("BTC",coin->symbol) != 0 ) + { + int32_t bundlei; + incr = coin->peers.numranked == 0 ? coin->MAXPEERS : coin->peers.numranked; + if ( (rand() % 10) < 3 ) + { + height = (addr->lastheight + 1); + if ( height >= coin->longestchain-coin->chain->bundlesize ) + height = coin->blocks.hwmchain.height + addr->rank*incr*_IGUANA_MAXPENDING; + } + else if ( (rand() % 10) < 9 ) + height = addr->rank * _IGUANA_MAXPENDING; + else height = coin->longestchain - (rand() % incr); + for (; heightbundlescount*coin->chain->bundlesize; height+=incr) + { + if ( height > addr->lastheight ) + addr->lastheight = height; + if ( (bp= coin->bundles[height/coin->chain->bundlesize]) != 0 && bp->emitfinish == 0 ) + { + bundlei = (height % coin->chain->bundlesize); + if ( bundlei < bp->n && bits256_nonz(bp->hashes[bundlei]) > 0 && (block= bp->blocks[bundlei]) != 0 && block->numrequests <= bp->minrequests && block->fpipbits == 0 )//&& block->queued == 0 )//(bp->issued[bundlei] == 0 || now > bp->issued[bundlei]+10) ) + { + if ( block->numrequests < 100 ) + block->numrequests++; + //block->issued = (uint32_t)time(NULL);; + if ( (rand() % 100) == 0 ) + printf("%s Send auto blockreq.%d [%d] minreq.%d\n",addr->ipaddr,bp->bundleheight+bundlei,block->numrequests,bp->minrequests); + iguana_sendblockreq(coin,addr,bp,bundlei,bp->hashes[bundlei],0); + return(1); + } + } + } + } + else + { + //printf("%s lastpoll.%u %u\n",addr->ipaddr,addr->lastpoll,now); + addr->lastpoll = now; + for (i=n=0; ibundlescount; i++) + if ( coin->bundles[i] != 0 && coin->bundles[i]->emitfinish == 0 ) + n++; + if ( n >= coin->bundlescount-(coin->bundlescount>>3) || (addr->ipbits % 10) < 5 ) + refbundlei = (addr->ipbits % coin->bundlescount); + else + { + if ( n*2 < coin->bundlescount ) + { + for (i=refbundlei=0; iusock == coin->peers.active[i].usock ) + break; + if ( coin->peers.active[i].usock >= 0 ) + refbundlei++; + } + //printf("half done\n"); + } else refbundlei = ((addr->addrind*100) % coin->bundlescount); + } + for (i=0; ibundlescount; i++) + { + if ( (diff= (i - refbundlei)) < 0 ) + diff = -diff; + if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 ) + { + metric = (1 + diff * ((addr->addrind&1) == 0 ? 1 : 1) * (1. + bp->metric));// / (i*((addr->addrind&1) != 0 ? 1 : i) + 1); + //printf("%f ",bp->metric); + if ( bestmetric < 0. || metric < bestmetric ) + bestmetric = metric, bestbp = bp; + } + } + if ( bestbp != 0 && bp->emitfinish == 0 ) + { + for (k=0; kbundlescount; k++) + { + i = (bestbp->hdrsi + k) % coin->bundlescount; + if ( (bp= coin->bundles[i]) == 0 || bp->emitfinish != 0 ) + continue; + printf("%.15f ref.%d addrind.%d bestbp.%d\n",bestmetric,refbundlei,addr->addrind,bp->hdrsi); + m = coin->chain->bundlesize; + if ( bp->n < m ) + m = bp->n; + j = (addr->addrind*3 + 0) % m; + val = (bp->threshold / 1000.); + for (r=0; r= m ) + j = 0; + if ( (block= bp->blocks[j]) != 0 && block->fpipbits == 0 && block->queued == 0 && block->numrequests <= bp->minrequests ) + { + if ( block->numrequests < 100 ) + block->numrequests++; + //block->issued = (uint32_t)time(NULL);; + printf("%s Send auto blockreq.%d\n",addr->ipaddr,bp->bundleheight+j); + iguana_sendblockreq(coin,addr,bp,j,hash2,0); + return(1); + } + } + } + } + } + } + int32_t priority; + if ( addr->rank != 1 && req == 0 ) + { + priority = 0; + req = queue_dequeue(&coin->blocksQ,0); + } else priority = 1; + if ( req != 0 ) + { + hash2 = req->hash2; + height = req->height; + block = 0; + if ( priority == 0 && (bp= req->bp) != 0 && req->bundlei >= 0 && req->bundlei < bp->n && req->bundlei < coin->chain->bundlesize && (block= bp->blocks[req->bundlei]) != 0 && (block->fpipbits != 0 || block->queued != 0) ) + { + //if ( 0 && priority != 0 ) + printf("SKIP %p[%d] %d\n",bp,bp!=0?bp->bundleheight:-1,req->bundlei); + } + else + { + char str[65]; + if ( 0 && priority != 0 ) + printf(" issue.%s\n",bits256_str(str,hash2)); + if ( block != 0 && block->numrequests < 100 ) + block->numrequests++; + iguana_sendblockreq(coin,addr,req->bp,req->bundlei,hash2,0); + } + flag++; + myfree(req,sizeof(*req)); + } + return(flag); +} + +int32_t iguana_processrecv(struct iguana_info *coin) // single threaded +{ + int32_t newhwm = 0,h,lflag,bundlei,flag = 0; bits256 hash2; struct iguana_block *next,*block; struct iguana_bundle *bp; + //printf("process bundlesQ\n"); + flag += iguana_processbundlesQ(coin,&newhwm); + flag += iguana_reqhdrs(coin); + lflag = 1; + while ( lflag != 0 ) + { + lflag = 0; + h = coin->blocks.hwmchain.height / coin->chain->bundlesize; + if ( (next= iguana_blockfind(coin,iguana_blockhash(coin,coin->blocks.hwmchain.height+1))) == 0 ) + { + if ( (block= iguana_blockfind(coin,coin->blocks.hwmchain.RO.hash2)) != 0 ) + next = block->hh.next, block->mainchain = 1; + } + if ( next != 0 ) + { + //printf("have next\n"); + if ( memcmp(next->RO.prev_block.bytes,coin->blocks.hwmchain.RO.hash2.bytes,sizeof(bits256)) == 0 ) + { + if ( _iguana_chainlink(coin,next) != 0 ) + lflag++; + //else printf("chainlink error for %d\n",coin->blocks.hwmchain.height+1); + } + else if ( 1 ) + { + double threshold,lag = OS_milliseconds() - coin->backstopmillis; + threshold = (10 + coin->longestchain - coin->blocksrecv); + if ( threshold < 1 ) + threshold = 1.; + if ( (bp= coin->bundles[(coin->blocks.hwmchain.height+1)/coin->chain->bundlesize]) != 0 ) + threshold = (bp->avetime + coin->avetime) * .5; + else threshold = coin->avetime; + threshold *= 100. * sqrt(threshold) * .000777; + if ( strcmp(coin->symbol,"BTC") != 0 ) + threshold = 400; + else threshold = 1000; + if ( coin->blocks.hwmchain.height+1 < coin->longestchain && (coin->backstop != coin->blocks.hwmchain.height+1 || lag > threshold) ) + { + coin->backstop = coin->blocks.hwmchain.height+1; + hash2 = iguana_blockhash(coin,coin->backstop); + if ( bits256_nonz(hash2) > 0 ) + { + bp = coin->bundles[(coin->blocks.hwmchain.height+1)/coin->chain->bundlesize]; + bundlei = (coin->blocks.hwmchain.height+1) % coin->chain->bundlesize; + if ( bp != 0 ) + { + coin->backstopmillis = OS_milliseconds(); + iguana_blockQ(coin,bp,bundlei,iguana_blockhash(coin,coin->backstop),1); + //iguana_blockrequest(coin,bp,bundlei,hash2,(uint32_t)time(NULL),1); + /*if ( (bp= coin->bundles[(coin->blocks.hwmchain.height+1)/coin->chain->bundlesize]) == 0 || bp->fpos[bundlei] >= 0 ) + { + if ( bp != 0 && coin->backstop == coin->blocks.hwmchain.height+1 ) + { + iguana_bundleiclear(coin,bp,bundlei); + } + iguana_blockQ(coin,bp,bundlei,next->RO.hash2,1); + }*/ + if ( (rand() % 100) == 0 ) + printf("MAINCHAIN.%d threshold %.3f %.3f lag %.3f\n",coin->blocks.hwmchain.height+1,threshold,coin->backstopmillis,lag); + } + } + } + else if ( 0 && bits256_nonz(next->RO.prev_block) > 0 ) + printf("next prev cmp error nonz.%d\n",bits256_nonz(next->RO.prev_block)); + } + } + if ( h != coin->blocks.hwmchain.height / coin->chain->bundlesize ) + iguana_savehdrs(coin); + } + return(flag); +} diff --git a/iguana/iguana_rpc.c b/iguana/iguana_rpc.c new file mode 100755 index 000000000..7e2ca1a93 --- /dev/null +++ b/iguana/iguana_rpc.c @@ -0,0 +1,501 @@ +/****************************************************************************** + * Copyright © 2014-2015 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +int32_t iguana_rpctestvector(struct iguana_info *coin,char *checkstr,char *jsonstr,int32_t maxlen,int32_t testi) +{ + int32_t len,checklen; + sprintf(jsonstr,"{\"rpc.%s testvector.%d\"}",coin->symbol,testi); + sprintf(checkstr,"{\"rpc.%s testvector.%d checkstr should have all info needed to verify the rpc request\"}",coin->symbol,testi); + len = (int32_t)strlen(jsonstr); + checklen = (int32_t)strlen(checkstr); + if ( len > maxlen || checklen > maxlen ) + printf("iguana_rpctestvector: i was bad and overflowed buffer len.%d checklen.%d\n",len,checklen), exit(-1); + if ( checklen > len ) + len = checklen; + return(len); +} + +int32_t iguana_rpctestcheck(struct iguana_info *coin,char *jsonstr,char *retjsonstr) +{ + if ( (rand() % 100) == 0 ) // 1% failure rate + return(-1); + else return(0); +} + +int32_t iguana_rpctest(struct iguana_info *coin) +{ +/* static int32_t testi,good,bad; + char *retjsonstr,jsonstr[4096],checkstr[sizeof(jsonstr)]; // should be big enough + //if ( (rand() % 1000) < 999 ) // if no test active, just return 0 + return(0); + if ( iguana_rpctestvector(coin,checkstr,jsonstr,sizeof(jsonstr),testi++) > 0 ) + { + retjsonstr = iguana_rpc(coin,jsonstr); + if ( iguana_rpctestcheck(coin,jsonstr,retjsonstr) < 0 ) + bad++, printf("rpctestcheck.%s error: (%s) -> (%s) | good.%d bad.%d %.2f%%\n",coin->symbol,jsonstr,retjsonstr,good,bad,100.*(double)good/(good+bad)); + else good++; + free(retjsonstr); + return(1); // indicates was active + }*/ + return(0); +} + +char *pangea_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json) +{ + return(clonestr("{\"error\":\"pangea API is not yet\"}")); +} + +char *InstantDEX_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json) +{ + return(clonestr("{\"error\":\"InstantDEX API is not yet\"}")); +} + +char *jumblr_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json) +{ + return(clonestr("{\"error\":\"jumblr API is not yet\"}")); +} + +struct iguana_txid *iguana_blocktx(struct iguana_info *coin,struct iguana_txid *tx,struct iguana_block *block,int32_t i) +{ + struct iguana_bundle *bp; uint32_t txidind; + if ( i >= 0 && i < block->RO.txn_count ) + { + if ( block->height >= 0 ) // + { + if ( (bp= coin->bundles[block->hdrsi]) != 0 ) + { + if ( (txidind= block->RO.firsttxidind) > 0 )//bp->firsttxidinds[block->bundlei]) > 0 ) + { + if ( iguana_bundletx(coin,bp,block->bundlei,tx,txidind+i) == tx ) + return(tx); + printf("error getting txidind.%d + i.%d from hdrsi.%d\n",txidind,i,block->hdrsi); + return(0); + } else printf("iguana_blocktx null txidind\n"); + } else printf("iguana_blocktx no bp\n"); + } + } else printf("i.%d vs txn_count.%d\n",i,block->RO.txn_count); + return(0); +} + +cJSON *iguana_blockjson(struct iguana_info *coin,struct iguana_block *block,int32_t txidsflag) +{ + char str[65]; int32_t i; struct iguana_txid *tx,T; cJSON *array,*json = cJSON_CreateObject(); + jaddstr(json,"blockhash",bits256_str(str,block->RO.hash2)); + jaddnum(json,"height",block->height); + jaddnum(json,"ipbits",block->fpipbits); + jaddstr(json,"merkle_root",bits256_str(str,block->RO.merkle_root)); + jaddstr(json,"prev_block",bits256_str(str,block->RO.prev_block)); + jaddnum(json,"timestamp",block->RO.timestamp); + jaddnum(json,"nonce",block->RO.nonce); + jaddnum(json,"nBits",block->RO.bits); + jaddnum(json,"version",block->RO.version); + jaddnum(json,"numvouts",block->RO.numvouts); + jaddnum(json,"numvins",block->RO.numvins); + jaddnum(json,"recvlen",block->RO.recvlen); + jaddnum(json,"hdrsi",block->hdrsi); + jaddnum(json,"PoW",block->PoW); + jaddnum(json,"bundlei",block->bundlei); + jaddnum(json,"mainchain",block->mainchain); + jaddnum(json,"valid",block->valid); + jaddnum(json,"txn_count",block->RO.txn_count); + if ( txidsflag != 0 ) + { + array = cJSON_CreateArray(); + for (i=0; iRO.txn_count; i++) + { + if ( (tx= iguana_blocktx(coin,&T,block,i)) != 0 ) + jaddistr(array,bits256_str(str,tx->txid)); + } + jadd(json,"txids",array); + //printf("add txids[%d]\n",block->txn_count); + } + return(json); +} + +cJSON *iguana_voutjson(struct iguana_info *coin,struct iguana_msgvout *vout,char *asmstr) +{ + static bits256 zero; + char scriptstr[8192+1],coinaddr[65]; int32_t i,M,N; uint8_t rmd160[20],msigs160[16][20],addrtype; + cJSON *addrs,*json = cJSON_CreateObject(); + jaddnum(json,"value",dstr(vout->value)); + if ( asmstr[0] != 0 ) + jaddstr(json,"asm",asmstr); + if ( vout->pk_script != 0 && vout->pk_scriptlen*2+1 < sizeof(scriptstr) ) + { + if ( iguana_calcrmd160(coin,rmd160,msigs160,&M,&N,vout->pk_script,vout->pk_scriptlen,zero) > 0 ) + addrtype = coin->chain->p2shval; + else addrtype = coin->chain->pubval; + btc_convrmd160(coinaddr,addrtype,rmd160); + jaddstr(json,"address",coinaddr); + init_hexbytes_noT(scriptstr,vout->pk_script,vout->pk_scriptlen); + jaddstr(json,"payscript",scriptstr); + if ( N != 0 ) + { + jaddnum(json,"M",M); + jaddnum(json,"N",N); + addrs = cJSON_CreateArray(); + for (i=0; ichain->pubval,msigs160[i]); + jaddistr(addrs,coinaddr); + } + jadd(json,"addrs",addrs); + } + } + return(json); +} + +cJSON *iguana_vinjson(struct iguana_info *coin,struct iguana_msgvin *vin) +{ + char scriptstr[8192+1],str[65]; cJSON *json = cJSON_CreateObject(); + jaddstr(json,"prev_hash",bits256_str(str,vin->prev_hash)); + jaddnum(json,"prev_vout",vin->prev_vout); + jaddnum(json,"sequence",vin->sequence); + if ( vin->script != 0 && vin->scriptlen*2+1 < sizeof(scriptstr) ) + { + init_hexbytes_noT(scriptstr,vin->script,vin->scriptlen); + jaddstr(json,"sigscript",scriptstr); + } + return(json); +} + +//struct iguana_txid { bits256 txid; uint32_t txidind,firstvout,firstvin,locktime,version,timestamp; uint16_t numvouts,numvins; } __attribute__((packed)); +//struct iguana_msgvin { bits256 prev_hash; uint8_t *script; uint32_t prev_vout,scriptlen,sequence; } __attribute__((packed)); + +//struct iguana_spend { uint32_t spendtxidind; int16_t prevout; uint16_t tbd:14,external:1,diffsequence:1; } __attribute__((packed)); + +void iguana_vinset(struct iguana_info *coin,int32_t height,struct iguana_msgvin *vin,struct iguana_txid *tx,int32_t i) +{ + struct iguana_spend *s,*S; uint32_t spendind; struct iguana_bundle *bp; + struct iguana_ramchaindata *rdata; struct iguana_txid *T; bits256 *X; + memset(vin,0,sizeof(*vin)); + if ( height >= 0 && height < coin->chain->bundlesize*coin->bundlescount && (bp= coin->bundles[height / coin->chain->bundlesize]) != 0 && (rdata= bp->ramchain.H.data) != 0 ) + { + S = (void *)((long)rdata + rdata->Soffset); + X = (void *)((long)rdata + rdata->Xoffset); + T = (void *)((long)rdata + rdata->Toffset); + spendind = (tx->firstvin + i); + s = &S[spendind]; + if ( s->diffsequence == 0 ) + vin->sequence = 0xffffffff; + vin->prev_vout = s->prevout; + iguana_ramchain_spendtxid(coin,&vin->prev_hash,T,rdata->numtxids,X,rdata->numexternaltxids,s); + } +} + +int32_t iguana_voutset(struct iguana_info *coin,uint8_t *scriptspace,char *asmstr,int32_t height,struct iguana_msgvout *vout,struct iguana_txid *tx,int32_t i) +{ + struct iguana_unspent *u,*U; uint32_t unspentind,scriptlen = 0; struct iguana_bundle *bp; + struct iguana_ramchaindata *rdata; struct iguana_pkhash *P,*p; + memset(vout,0,sizeof(*vout)); + if ( height >= 0 && height < coin->chain->bundlesize*coin->bundlescount && (bp= coin->bundles[height / coin->chain->bundlesize]) != 0 && (rdata= bp->ramchain.H.data) != 0 ) + { + U = (void *)((long)rdata + rdata->Uoffset); + P = (void *)((long)rdata + rdata->Poffset); + unspentind = (tx->firstvout + i); + u = &U[unspentind]; + if ( u->txidind != tx->txidind || u->vout != i || u->hdrsi != height / coin->chain->bundlesize ) + printf("iguana_voutset: txidind mismatch %d vs %d || %d vs %d || (%d vs %d)\n",u->txidind,u->txidind,u->vout,i,u->hdrsi,height / coin->chain->bundlesize); + p = &P[u->pkind]; + vout->value = u->value; + scriptlen = iguana_scriptgen(coin,scriptspace,asmstr,bp,p,u->type); + } + vout->pk_scriptlen = scriptlen; + return(scriptlen); +} + +cJSON *iguana_txjson(struct iguana_info *coin,struct iguana_txid *tx,int32_t height) +{ + struct iguana_msgvin vin; struct iguana_msgvout vout; int32_t i; char asmstr[512],str[65]; uint8_t space[8192]; + cJSON *vouts,*vins,*json; + json = cJSON_CreateObject(); + jaddstr(json,"txid",bits256_str(str,tx->txid)); + if ( height >= 0 ) + jaddnum(json,"height",height); + jaddnum(json,"version",tx->version); + jaddnum(json,"timestamp",tx->timestamp); + jaddnum(json,"locktime",tx->locktime); + vins = cJSON_CreateArray(); + vouts = cJSON_CreateArray(); + for (i=0; inumvouts; i++) + { + iguana_voutset(coin,space,asmstr,height,&vout,tx,i); + jaddi(vouts,iguana_voutjson(coin,&vout,asmstr)); + } + jadd(json,"vouts",vouts); + for (i=0; inumvins; i++) + { + iguana_vinset(coin,height,&vin,tx,i); + jaddi(vins,iguana_vinjson(coin,&vin)); + } + jadd(json,"vins",vins); + return(json); +} + +char *ramchain_coinparser(struct iguana_info *coin,char *method,cJSON *json) +{ + char *hashstr,*txidstr,*coinaddr,*txbytes,rmd160str[41],str[65]; int32_t height,i,n,valid = 0; + cJSON *addrs,*retjson,*retitem; uint8_t rmd160[20],addrtype; bits256 hash2,checktxid; + memset(&hash2,0,sizeof(hash2)); struct iguana_txid *tx,T; struct iguana_block *block = 0; + if ( coin == 0 && (coin= iguana_coinselect()) == 0 ) + return(clonestr("{\"error\":\"ramchain_coinparser needs coin\"}")); + if ( (coinaddr= jstr(json,"address")) != 0 ) + { + if ( btc_addr2univ(&addrtype,rmd160,coinaddr) == 0 ) + { + if ( addrtype == coin->chain->pubval || addrtype == coin->chain->p2shval ) + valid = 1; + else return(clonestr("{\"error\":\"invalid addrtype\"}")); + } else return(clonestr("{\"error\":\"cant convert address to rmd160\"}")); + } + if ( strcmp(method,"block") == 0 ) + { + height = -1; + if ( ((hashstr= jstr(json,"blockhash")) != 0 || (hashstr= jstr(json,"hash")) != 0) && strlen(hashstr) == sizeof(bits256)*2 ) + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + else + { + height = juint(json,"height"); + hash2 = iguana_blockhash(coin,height); + } + retitem = cJSON_CreateObject(); + if ( (block= iguana_blockfind(coin,hash2)) != 0 ) + { + if ( (height >= 0 && block->height == height) || memcmp(hash2.bytes,block->RO.hash2.bytes,sizeof(hash2)) == 0 ) + { + char str[65],str2[65]; printf("hash2.(%s) -> %s\n",bits256_str(str,hash2),bits256_str(str2,block->RO.hash2)); + return(jprint(iguana_blockjson(coin,block,juint(json,"txids")),1)); + } + } + else return(clonestr("{\"error\":\"cant find block\"}")); + } + else if ( strcmp(method,"tx") == 0 ) + { + if ( ((txidstr= jstr(json,"txid")) != 0 || (txidstr= jstr(json,"hash")) != 0) && strlen(txidstr) == sizeof(bits256)*2 ) + { + retitem = cJSON_CreateObject(); + decode_hex(hash2.bytes,sizeof(hash2),txidstr); + if ( (tx= iguana_txidfind(coin,&height,&T,hash2)) != 0 ) + { + jadd(retitem,"tx",iguana_txjson(coin,tx,height)); + return(jprint(retitem,1)); + } + return(clonestr("{\"error\":\"cant find txid\"}")); + } + else return(clonestr("{\"error\":\"invalid txid\"}")); + } + else if ( strcmp(method,"rawtx") == 0 ) + { + if ( ((txidstr= jstr(json,"txid")) != 0 || (txidstr= jstr(json,"hash")) != 0) && strlen(txidstr) == sizeof(bits256)*2 ) + { + decode_hex(hash2.bytes,sizeof(hash2),txidstr); + if ( (tx= iguana_txidfind(coin,&height,&T,hash2)) != 0 ) + { + if ( (txbytes= iguana_txbytes(coin,&checktxid,tx,height)) != 0 ) + { + retitem = cJSON_CreateObject(); + jaddstr(retitem,"txid",bits256_str(str,hash2)); + jaddnum(retitem,"height",height); + jaddstr(retitem,"rawtx",txbytes); + myfree(txbytes,strlen(txbytes)+1); + return(jprint(retitem,1)); + } else return(clonestr("{\"error\":\"couldnt generate txbytes\"}")); + } + return(clonestr("{\"error\":\"cant find txid\"}")); + } + else return(clonestr("{\"error\":\"invalid txid\"}")); + } + else if ( strcmp(method,"txs") == 0 ) + { + if ( ((hashstr= jstr(json,"block")) != 0 || (hashstr= jstr(json,"blockhash")) != 0) && strlen(hashstr) == sizeof(bits256)*2 ) + { + decode_hex(hash2.bytes,sizeof(hash2),hashstr); + if ( (block= iguana_blockfind(coin,hash2)) == 0 ) + return(clonestr("{\"error\":\"cant find blockhash\"}")); + } + else if ( jobj(json,"height") != 0 ) + { + height = juint(json,"height"); + hash2 = iguana_blockhash(coin,height); + if ( (block= iguana_blockfind(coin,hash2)) == 0 ) + return(clonestr("{\"error\":\"cant find block at height\"}")); + } + else if ( valid == 0 ) + return(clonestr("{\"error\":\"txs needs blockhash or height or address\"}")); + retitem = cJSON_CreateArray(); + if ( block != 0 ) + { + for (i=0; iRO.txn_count; i++) + { + if ( (tx= iguana_blocktx(coin,&T,block,i)) != 0 ) + jaddi(retitem,iguana_txjson(coin,tx,-1)); + } + } + else + { + init_hexbytes_noT(rmd160str,rmd160,20); + jaddnum(retitem,"addrtype",addrtype); + jaddstr(retitem,"rmd160",rmd160str); + jaddstr(retitem,"txlist","get list of all tx for this address"); + } + return(jprint(retitem,1)); + } + else if ( strcmp(method,"status") == 0 ) + { + retitem = cJSON_CreateObject(); + jaddstr(retitem,"status","coin status"); + return(jprint(retitem,1)); + } + else + { + n = 0; + if ( valid == 0 ) + { + if ( (addrs= jarray(&n,json,"addrs")) == 0 ) + return(clonestr("{\"error\":\"need address or addrs\"}")); + } + for (i=0; i<=n; i++) + { + retitem = cJSON_CreateObject(); + if ( i > 0 ) + retjson = cJSON_CreateArray(); + if ( i > 0 ) + { + if ( (coinaddr= jstr(jitem(addrs,i-1),0)) == 0 ) + return(clonestr("{\"error\":\"missing address in addrs\"}")); + if ( btc_addr2univ(&addrtype,rmd160,coinaddr) < 0 ) + { + free_json(retjson); + return(clonestr("{\"error\":\"illegal address in addrs\"}")); + } + if ( addrtype != coin->chain->pubval && addrtype != coin->chain->p2shval ) + return(clonestr("{\"error\":\"invalid addrtype in addrs\"}")); + } + if ( strcmp(method,"utxo") == 0 ) + { + jaddstr(retitem,"utxo","utxo entry"); + } + else if ( strcmp(method,"unconfirmed") == 0 ) + { + jaddstr(retitem,"unconfirmed","unconfirmed entry"); + } + else if ( strcmp(method,"balance") == 0 ) + { + jaddstr(retitem,"balance","balance entry"); + } + else if ( strcmp(method,"totalreceived") == 0 ) + { + jaddstr(retitem,"totalreceived","totalreceived entry"); + } + else if ( strcmp(method,"totalsent") == 0 ) + { + jaddstr(retitem,"totalsent","totalsent entry"); + } + if ( n == 0 ) + return(jprint(retitem,1)); + else jaddi(retjson,retitem); + } + return(jprint(retjson,1)); + } + return(clonestr("{\"error\":\"illegal ramchain method or missing coin\"}")); +} + +char *iguana_jsoncheck(char *retstr,int32_t freeflag) +{ + cJSON *retjson; char *errstr; + if ( retstr != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (errstr= jstr(retjson,"error")) == 0 ) + { + free_json(retjson); + return(retstr); + } + free_json(retjson); + } + if ( freeflag != 0 ) + free(retstr); + } + return(0); +} + +char *ramchain_parser(struct iguana_agent *agent,struct iguana_info *coin,char *method,cJSON *json) +{ + char *symbol,*str,*retstr; int32_t height; cJSON *argjson,*obj; + /*{"agent":"ramchain","method":"block","coin":"BTCD","hash":""} + {"agent":"ramchain","method":"block","coin":"BTCD","height":345600} + {"agent":"ramchain","method":"tx","coin":"BTCD","txid":""} + {"agent":"ramchain","method":"rawtx","coin":"BTCD","txid":""} + {"agent":"ramchain","method":"balance","coin":"BTCD","address":""} + {"agent":"ramchain","method":"balance","coin":"BTCD","addrs":["",...]} + {"agent":"ramchain","method":"totalreceived","coin":"BTCD","address":""} + {"agent":"ramchain","method":"totalsent","coin":"BTCD","address":""} + {"agent":"ramchain","method":"unconfirmed","coin":"BTCD","address":""} + {"agent":"ramchain","method":"utxo","coin":"BTCD","address":""} + {"agent":"ramchain","method":"utxo","coin":"BTCD","addrs":["", "",...]} + {"agent":"ramchain","method":"txs","coin":"BTCD","block":""} + {"agent":"ramchain","method":"txs","coin":"BTCD","height":12345} + {"agent":"ramchain","method":"txs","coin":"BTCD","address":""} + {"agent":"ramchain","method":"status","coin":"BTCD"}*/ + if ( (symbol= jstr(json,"coin")) != 0 && symbol[0] != 0 ) + { + if ( coin == 0 ) + coin = iguana_coinfind(symbol); + else if ( strcmp(symbol,coin->symbol) != 0 ) + return(clonestr("{\"error\":\"mismatched coin symbol\"}")); + } + if ( strcmp(method,"explore") == 0 ) + { + obj = jobj(json,"search"); + if ( coin != 0 && obj != 0 ) + { + argjson = cJSON_CreateObject(); + jaddstr(argjson,"agent","ramchain"); + jaddstr(argjson,"method","block"); + jaddnum(argjson,"txids",1); + if ( is_cJSON_Number(obj) != 0 ) + { + height = juint(obj,0); + jaddnum(argjson,"height",height); + } + else if ( (str= jstr(obj,0)) != 0 ) + jaddstr(argjson,"hash",str); + else return(clonestr("{\"error\":\"need number or string to search\"}")); + if ( (retstr= iguana_jsoncheck(ramchain_coinparser(coin,"block",argjson),1)) != 0 ) + { + free_json(argjson); + return(retstr); + } + free_json(argjson); + argjson = cJSON_CreateObject(); + jaddstr(argjson,"agent","ramchain"); + jaddstr(argjson,"method","tx"); + jaddstr(argjson,"txid",str); + if ( (retstr= iguana_jsoncheck(ramchain_coinparser(coin,"tx",argjson),1)) != 0 ) + { + free_json(argjson); + return(retstr); + } + free_json(argjson); + return(clonestr("{\"result\":\"explore search cant find height, blockhash, txid\"}")); + } + return(clonestr("{\"result\":\"explore no coin or search\"}")); + } + return(ramchain_coinparser(coin,method,json)); +} + diff --git a/iguana/index.html b/iguana/index.html new file mode 100644 index 000000000..e2c7c20ad --- /dev/null +++ b/iguana/index.html @@ -0,0 +1,284 @@ + + + + + + iguana + + + + +
+ +
+
+
+ +
Welcome
+
+

iguana

+

+ iguana is fast. +

+

+ iguana does all bitcoin compatibles. +

+

+ iguana is fast. +

+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ Panel Title +

+
+
+
+
+
+
+
+ JSONExample +
+
+ +
+
+
+
+
+
+ Required input fields are marked green +
+
+
+
+
+
+
+
+ + Response
+
JSON response
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ JSON API test page +

+
+
+
+
+
+
+
+

+ +
+
+
+
+
+
+
+
+
+
+
+
JSON response
+ +
+
+
+
+
+
+
+
+
+
+
+ + +
+
+
+
+ +
+
+ +
+ +
+
+
+

+ Json posting form +

+
+
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+                                                        JSON response
+                                                    
+ +
+
+
+
+ +

Submitted JSON history + +

+
+
+ + + + +
+ +
+
+ +
+
+
+
+
+
+ +
+
+
+

Settings

+
+
+
+
+
+
+
+
+ + +
+
+
+
+
+ +
+
+
+
+
+
+
+
+ +
+
+
+
+
+ + + + + + + + diff --git a/iguana/js/api.js b/iguana/js/api.js new file mode 100755 index 000000000..58de38dde --- /dev/null +++ b/iguana/js/api.js @@ -0,0 +1,67 @@ +var SPNAPI = (function(SPNAPI, $, undefined) { + + SPNAPI.methods = {}; + SPNAPI.pages = ["Settings", "eyedea", "iguana","Debug","Wallet"]; + SPNAPI.pageContent = {}; + SPNAPI.page = "welcome"; + $(document).ready(function() { + + //load Pages into the navbar + $.each(SPNAPI.pages, function( index, value ) { + $("#welcome").after(''); + }); + $(".navigation").on("click", function () { + + var page = $(this).data("page"); + $(".navigation").removeClass("active"); + $(".hljs").html("JSON response"); + SPNAPI.loadSite(page); + }); + $(".page").hide(); + $("#welcome_page").show(); + $(".submit_api_request").on("click", function () { + + SPNAPI.submitRequest(); + + }); + + $(".clear-response").on("click", function () { + + $(".hljs").html("JSON response"); + + }); + + + + }); + + SPNAPI.submitRequest = function(e) { + +//code added in order to be able to receive input from or ",k.noCloneChecked=!!b.cloneNode(!0).lastChild.defaultValue}();var U="undefined";k.focusinBubbles="onfocusin"in a;var V=/^key/,W=/^(?:mouse|pointer|contextmenu)|click/,X=/^(?:focusinfocus|focusoutblur)$/,Y=/^([^.]*)(?:\.(.+)|)$/;function Z(){return!0}function $(){return!1}function _(){try{return l.activeElement}catch(a){}}n.event={global:{},add:function(a,b,c,d,e){var f,g,h,i,j,k,l,m,o,p,q,r=L.get(a);if(r){c.handler&&(f=c,c=f.handler,e=f.selector),c.guid||(c.guid=n.guid++),(i=r.events)||(i=r.events={}),(g=r.handle)||(g=r.handle=function(b){return typeof n!==U&&n.event.triggered!==b.type?n.event.dispatch.apply(a,arguments):void 0}),b=(b||"").match(E)||[""],j=b.length;while(j--)h=Y.exec(b[j])||[],o=q=h[1],p=(h[2]||"").split(".").sort(),o&&(l=n.event.special[o]||{},o=(e?l.delegateType:l.bindType)||o,l=n.event.special[o]||{},k=n.extend({type:o,origType:q,data:d,handler:c,guid:c.guid,selector:e,needsContext:e&&n.expr.match.needsContext.test(e),namespace:p.join(".")},f),(m=i[o])||(m=i[o]=[],m.delegateCount=0,l.setup&&l.setup.call(a,d,p,g)!==!1||a.addEventListener&&a.addEventListener(o,g,!1)),l.add&&(l.add.call(a,k),k.handler.guid||(k.handler.guid=c.guid)),e?m.splice(m.delegateCount++,0,k):m.push(k),n.event.global[o]=!0)}},remove:function(a,b,c,d,e){var f,g,h,i,j,k,l,m,o,p,q,r=L.hasData(a)&&L.get(a);if(r&&(i=r.events)){b=(b||"").match(E)||[""],j=b.length;while(j--)if(h=Y.exec(b[j])||[],o=q=h[1],p=(h[2]||"").split(".").sort(),o){l=n.event.special[o]||{},o=(d?l.delegateType:l.bindType)||o,m=i[o]||[],h=h[2]&&new RegExp("(^|\\.)"+p.join("\\.(?:.*\\.|)")+"(\\.|$)"),g=f=m.length;while(f--)k=m[f],!e&&q!==k.origType||c&&c.guid!==k.guid||h&&!h.test(k.namespace)||d&&d!==k.selector&&("**"!==d||!k.selector)||(m.splice(f,1),k.selector&&m.delegateCount--,l.remove&&l.remove.call(a,k));g&&!m.length&&(l.teardown&&l.teardown.call(a,p,r.handle)!==!1||n.removeEvent(a,o,r.handle),delete i[o])}else for(o in i)n.event.remove(a,o+b[j],c,d,!0);n.isEmptyObject(i)&&(delete r.handle,L.remove(a,"events"))}},trigger:function(b,c,d,e){var f,g,h,i,k,m,o,p=[d||l],q=j.call(b,"type")?b.type:b,r=j.call(b,"namespace")?b.namespace.split("."):[];if(g=h=d=d||l,3!==d.nodeType&&8!==d.nodeType&&!X.test(q+n.event.triggered)&&(q.indexOf(".")>=0&&(r=q.split("."),q=r.shift(),r.sort()),k=q.indexOf(":")<0&&"on"+q,b=b[n.expando]?b:new n.Event(q,"object"==typeof b&&b),b.isTrigger=e?2:3,b.namespace=r.join("."),b.namespace_re=b.namespace?new RegExp("(^|\\.)"+r.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,b.result=void 0,b.target||(b.target=d),c=null==c?[b]:n.makeArray(c,[b]),o=n.event.special[q]||{},e||!o.trigger||o.trigger.apply(d,c)!==!1)){if(!e&&!o.noBubble&&!n.isWindow(d)){for(i=o.delegateType||q,X.test(i+q)||(g=g.parentNode);g;g=g.parentNode)p.push(g),h=g;h===(d.ownerDocument||l)&&p.push(h.defaultView||h.parentWindow||a)}f=0;while((g=p[f++])&&!b.isPropagationStopped())b.type=f>1?i:o.bindType||q,m=(L.get(g,"events")||{})[b.type]&&L.get(g,"handle"),m&&m.apply(g,c),m=k&&g[k],m&&m.apply&&n.acceptData(g)&&(b.result=m.apply(g,c),b.result===!1&&b.preventDefault());return b.type=q,e||b.isDefaultPrevented()||o._default&&o._default.apply(p.pop(),c)!==!1||!n.acceptData(d)||k&&n.isFunction(d[q])&&!n.isWindow(d)&&(h=d[k],h&&(d[k]=null),n.event.triggered=q,d[q](),n.event.triggered=void 0,h&&(d[k]=h)),b.result}},dispatch:function(a){a=n.event.fix(a);var b,c,e,f,g,h=[],i=d.call(arguments),j=(L.get(this,"events")||{})[a.type]||[],k=n.event.special[a.type]||{};if(i[0]=a,a.delegateTarget=this,!k.preDispatch||k.preDispatch.call(this,a)!==!1){h=n.event.handlers.call(this,a,j),b=0;while((f=h[b++])&&!a.isPropagationStopped()){a.currentTarget=f.elem,c=0;while((g=f.handlers[c++])&&!a.isImmediatePropagationStopped())(!a.namespace_re||a.namespace_re.test(g.namespace))&&(a.handleObj=g,a.data=g.data,e=((n.event.special[g.origType]||{}).handle||g.handler).apply(f.elem,i),void 0!==e&&(a.result=e)===!1&&(a.preventDefault(),a.stopPropagation()))}return k.postDispatch&&k.postDispatch.call(this,a),a.result}},handlers:function(a,b){var c,d,e,f,g=[],h=b.delegateCount,i=a.target;if(h&&i.nodeType&&(!a.button||"click"!==a.type))for(;i!==this;i=i.parentNode||this)if(i.disabled!==!0||"click"!==a.type){for(d=[],c=0;h>c;c++)f=b[c],e=f.selector+" ",void 0===d[e]&&(d[e]=f.needsContext?n(e,this).index(i)>=0:n.find(e,this,null,[i]).length),d[e]&&d.push(f);d.length&&g.push({elem:i,handlers:d})}return h]*)\/>/gi,ba=/<([\w:]+)/,ca=/<|&#?\w+;/,da=/<(?:script|style|link)/i,ea=/checked\s*(?:[^=]|=\s*.checked.)/i,fa=/^$|\/(?:java|ecma)script/i,ga=/^true\/(.*)/,ha=/^\s*\s*$/g,ia={option:[1,""],thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};ia.optgroup=ia.option,ia.tbody=ia.tfoot=ia.colgroup=ia.caption=ia.thead,ia.th=ia.td;function ja(a,b){return n.nodeName(a,"table")&&n.nodeName(11!==b.nodeType?b:b.firstChild,"tr")?a.getElementsByTagName("tbody")[0]||a.appendChild(a.ownerDocument.createElement("tbody")):a}function ka(a){return a.type=(null!==a.getAttribute("type"))+"/"+a.type,a}function la(a){var b=ga.exec(a.type);return b?a.type=b[1]:a.removeAttribute("type"),a}function ma(a,b){for(var c=0,d=a.length;d>c;c++)L.set(a[c],"globalEval",!b||L.get(b[c],"globalEval"))}function na(a,b){var c,d,e,f,g,h,i,j;if(1===b.nodeType){if(L.hasData(a)&&(f=L.access(a),g=L.set(b,f),j=f.events)){delete g.handle,g.events={};for(e in j)for(c=0,d=j[e].length;d>c;c++)n.event.add(b,e,j[e][c])}M.hasData(a)&&(h=M.access(a),i=n.extend({},h),M.set(b,i))}}function oa(a,b){var c=a.getElementsByTagName?a.getElementsByTagName(b||"*"):a.querySelectorAll?a.querySelectorAll(b||"*"):[];return void 0===b||b&&n.nodeName(a,b)?n.merge([a],c):c}function pa(a,b){var c=b.nodeName.toLowerCase();"input"===c&&T.test(a.type)?b.checked=a.checked:("input"===c||"textarea"===c)&&(b.defaultValue=a.defaultValue)}n.extend({clone:function(a,b,c){var d,e,f,g,h=a.cloneNode(!0),i=n.contains(a.ownerDocument,a);if(!(k.noCloneChecked||1!==a.nodeType&&11!==a.nodeType||n.isXMLDoc(a)))for(g=oa(h),f=oa(a),d=0,e=f.length;e>d;d++)pa(f[d],g[d]);if(b)if(c)for(f=f||oa(a),g=g||oa(h),d=0,e=f.length;e>d;d++)na(f[d],g[d]);else na(a,h);return g=oa(h,"script"),g.length>0&&ma(g,!i&&oa(a,"script")),h},buildFragment:function(a,b,c,d){for(var e,f,g,h,i,j,k=b.createDocumentFragment(),l=[],m=0,o=a.length;o>m;m++)if(e=a[m],e||0===e)if("object"===n.type(e))n.merge(l,e.nodeType?[e]:e);else if(ca.test(e)){f=f||k.appendChild(b.createElement("div")),g=(ba.exec(e)||["",""])[1].toLowerCase(),h=ia[g]||ia._default,f.innerHTML=h[1]+e.replace(aa,"<$1>")+h[2],j=h[0];while(j--)f=f.lastChild;n.merge(l,f.childNodes),f=k.firstChild,f.textContent=""}else l.push(b.createTextNode(e));k.textContent="",m=0;while(e=l[m++])if((!d||-1===n.inArray(e,d))&&(i=n.contains(e.ownerDocument,e),f=oa(k.appendChild(e),"script"),i&&ma(f),c)){j=0;while(e=f[j++])fa.test(e.type||"")&&c.push(e)}return k},cleanData:function(a){for(var b,c,d,e,f=n.event.special,g=0;void 0!==(c=a[g]);g++){if(n.acceptData(c)&&(e=c[L.expando],e&&(b=L.cache[e]))){if(b.events)for(d in b.events)f[d]?n.event.remove(c,d):n.removeEvent(c,d,b.handle);L.cache[e]&&delete L.cache[e]}delete M.cache[c[M.expando]]}}}),n.fn.extend({text:function(a){return J(this,function(a){return void 0===a?n.text(this):this.empty().each(function(){(1===this.nodeType||11===this.nodeType||9===this.nodeType)&&(this.textContent=a)})},null,a,arguments.length)},append:function(){return this.domManip(arguments,function(a){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var b=ja(this,a);b.appendChild(a)}})},prepend:function(){return this.domManip(arguments,function(a){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var b=ja(this,a);b.insertBefore(a,b.firstChild)}})},before:function(){return this.domManip(arguments,function(a){this.parentNode&&this.parentNode.insertBefore(a,this)})},after:function(){return this.domManip(arguments,function(a){this.parentNode&&this.parentNode.insertBefore(a,this.nextSibling)})},remove:function(a,b){for(var c,d=a?n.filter(a,this):this,e=0;null!=(c=d[e]);e++)b||1!==c.nodeType||n.cleanData(oa(c)),c.parentNode&&(b&&n.contains(c.ownerDocument,c)&&ma(oa(c,"script")),c.parentNode.removeChild(c));return this},empty:function(){for(var a,b=0;null!=(a=this[b]);b++)1===a.nodeType&&(n.cleanData(oa(a,!1)),a.textContent="");return this},clone:function(a,b){return a=null==a?!1:a,b=null==b?a:b,this.map(function(){return n.clone(this,a,b)})},html:function(a){return J(this,function(a){var b=this[0]||{},c=0,d=this.length;if(void 0===a&&1===b.nodeType)return b.innerHTML;if("string"==typeof a&&!da.test(a)&&!ia[(ba.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(aa,"<$1>");try{for(;d>c;c++)b=this[c]||{},1===b.nodeType&&(n.cleanData(oa(b,!1)),b.innerHTML=a);b=0}catch(e){}}b&&this.empty().append(a)},null,a,arguments.length)},replaceWith:function(){var a=arguments[0];return this.domManip(arguments,function(b){a=this.parentNode,n.cleanData(oa(this)),a&&a.replaceChild(b,this)}),a&&(a.length||a.nodeType)?this:this.remove()},detach:function(a){return this.remove(a,!0)},domManip:function(a,b){a=e.apply([],a);var c,d,f,g,h,i,j=0,l=this.length,m=this,o=l-1,p=a[0],q=n.isFunction(p);if(q||l>1&&"string"==typeof p&&!k.checkClone&&ea.test(p))return this.each(function(c){var d=m.eq(c);q&&(a[0]=p.call(this,c,d.html())),d.domManip(a,b)});if(l&&(c=n.buildFragment(a,this[0].ownerDocument,!1,this),d=c.firstChild,1===c.childNodes.length&&(c=d),d)){for(f=n.map(oa(c,"script"),ka),g=f.length;l>j;j++)h=c,j!==o&&(h=n.clone(h,!0,!0),g&&n.merge(f,oa(h,"script"))),b.call(this[j],h,j);if(g)for(i=f[f.length-1].ownerDocument,n.map(f,la),j=0;g>j;j++)h=f[j],fa.test(h.type||"")&&!L.access(h,"globalEval")&&n.contains(i,h)&&(h.src?n._evalUrl&&n._evalUrl(h.src):n.globalEval(h.textContent.replace(ha,"")))}return this}}),n.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(a,b){n.fn[a]=function(a){for(var c,d=[],e=n(a),g=e.length-1,h=0;g>=h;h++)c=h===g?this:this.clone(!0),n(e[h])[b](c),f.apply(d,c.get());return this.pushStack(d)}});var qa,ra={};function sa(b,c){var d,e=n(c.createElement(b)).appendTo(c.body),f=a.getDefaultComputedStyle&&(d=a.getDefaultComputedStyle(e[0]))?d.display:n.css(e[0],"display");return e.detach(),f}function ta(a){var b=l,c=ra[a];return c||(c=sa(a,b),"none"!==c&&c||(qa=(qa||n("