buffa.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. //require('bufferjs');
  2. var crypto=require('crypto');
  3. var Buffer = require("./buffer").Buffer;
  4. console.log(typeof(Buffer));
  5. exports.Buffer = Buffer;
  6. exports.dump = hexdump;
  7. exports.hexdump = hexdump;
  8. Buffer.prototype.XOR = function(buf){
  9. var nn = new Buffer(this.length);
  10. var mi = buf.length;
  11. var fi = 0;
  12. var n = this.length;
  13. for(var i=0;i<n;i++){
  14. if(i>mi){
  15. fi = 0;
  16. }
  17. nn[i] = this[i] ^ (buf[fi]);
  18. fi ++;
  19. }
  20. return nn;
  21. }
  22. Buffer.prototype.AND = function(buf){
  23. var nn = new Buffer(this.length);
  24. var mi = buf.length;
  25. var fi = 0;
  26. var n = this.length;
  27. for(var i=0;i<n;i++){
  28. if(i>mi){
  29. fi = 0;
  30. }
  31. nn[i] = this[i] & (buf[fi]);
  32. fi ++;
  33. }
  34. return nn;
  35. }
  36. Buffer.prototype.OR = function(buf){
  37. var nn = new Buffer(this.length);
  38. var mi = buf.length;
  39. var fi = 0;
  40. var n = this.length;
  41. for(var i=0;i<n;i++){
  42. if(i>mi){
  43. fi = 0;
  44. }
  45. nn[i] = this[i] | (buf[fi]);
  46. fi ++;
  47. }
  48. return nn;
  49. }
  50. Buffer.prototype.clone = function(){
  51. var n = new Buffer(this.length);
  52. for(var i=0;i<this.length;i++){
  53. n[i] = this[i];
  54. }
  55. return n;
  56. }
  57. Buffer.prototype.append = function(buf,baselength){
  58. var nlen = this.length + buf.length;
  59. if(baselength){
  60. nlen = Math.ceil( (nlen) / baselength) * baselength;
  61. }
  62. var tmp = new Buffer( nlen );
  63. tmp.fill(0);
  64. for(var i=0;i<this.length;i++){
  65. tmp[i]=this[i];
  66. }
  67. for(var i=0;i<buf.length;i++){
  68. tmp[this.length+i]=buf[i];
  69. }
  70. return tmp;
  71. }
  72. var BASE =
  73. ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","_","=","/","!","#","$","%","&","'","(",")","*","+",",","-",".",":",";","<",">","@","[","]","^","`","{","|","}","~","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ"];
  74. function getnum(num,base){
  75. var ss = ""
  76. while(num > 0){
  77. var t = num % base;
  78. ss = BASE[t] + ss;
  79. num = Math.floor(num/base);
  80. }
  81. return ss;
  82. }
  83. var bigi = require("../bigint");
  84. Buffer.prototype.toExpand = function(string,base){
  85. var asa = bigi.str2bigInt(string,base);
  86. var ss = bigi.bigInt2str(asa,16);
  87. var re = new Buffer(ss.length%2===0?ss:"0"+ss,'hex');
  88. return re;
  89. }
  90. Buffer.prototype.toBase = function(base){
  91. var ss = this.toString("hex");
  92. var asa = bigi.str2bigInt(ss,16);
  93. var s = bigi.bigInt2str(asa,base);
  94. return s;
  95. console.log(base,"sass",asa,s,ss);
  96. return ;
  97. var nn = this.clone();
  98. var nwords = Math.floor(nn.length/4);
  99. var padding = [0,1,2,3][nn.length % 4];
  100. var rest = parseInt(nn.slice(nn.length-padding).toString("hex"),16);
  101. var s = "";
  102. var at = 0;
  103. while(at < nwords){
  104. var num = nn.readUInt32BE(at*4);
  105. s += getnum(num,base);
  106. s += ss+" ";
  107. at += 1;
  108. }
  109. console.log(rest);
  110. s += getnum(rest,base);
  111. return s;
  112. }
  113. Buffer.prototype.prepend = function(buf){
  114. var tmp = new Buffer(this.length+buf.length);
  115. for(var i=0;i<buf.length;i++){
  116. tmp[i]=buf[i];
  117. }
  118. for(var i=0;i<this.length;i++){
  119. tmp[buf.length+i]=this[i];
  120. }
  121. return tmp;
  122. }
  123. Buffer.prototype.SHA1= function(encoding){
  124. var encoding = encoding || null;
  125. var h=crypto.createHash('sha1');
  126. h.update(this);
  127. return h.digest(encoding);
  128. }
  129. Buffer.prototype.SHA224 = function(encoding){
  130. var encoding = encoding || null;
  131. var h=crypto.createHash('sha224');
  132. h.update(this);
  133. return h.digest(encoding);
  134. }
  135. Buffer.prototype.SHA256 = function(encoding){
  136. var encoding = encoding || null;
  137. var h=crypto.createHash('sha256');
  138. h.update(this);
  139. return h.digest(encoding);
  140. }
  141. Buffer.prototype.SHA384 = function(encoding){
  142. var encoding = encoding || null;
  143. var h=crypto.createHash('sha384');
  144. h.update(this);
  145. return h.digest(encoding);
  146. }
  147. Buffer.prototype.SHA512 = function(encoding){
  148. var encoding = encoding || null;
  149. var h=crypto.createHash('sha512');
  150. h.update(this);
  151. return h.digest(encoding);
  152. }
  153. Buffer.prototype.MD5= function(encoding){
  154. var encoding = encoding || null;
  155. var h=crypto.createHash('md5');
  156. h.update(this);
  157. return h.digest(encoding);
  158. }
  159. Buffer.prototype.RIPE= function(encoding){
  160. var encoding = encoding || null;
  161. var h=crypto.createHash('ripemd');
  162. h.update(this);
  163. return h.digest(encoding);
  164. }
  165. Buffer.prototype.RIPE160= function(encoding){
  166. var encoding = encoding || null;
  167. var h=crypto.createHash('ripemd160');
  168. h.update(this);
  169. return h.digest(encoding);
  170. }
  171. Buffer.prototype.WHIRLPOOL= function(encoding){
  172. var encoding = encoding || null;
  173. var h=crypto.createHash('whirlpool');
  174. h.update(this);
  175. return h.digest(encoding);
  176. }
  177. Buffer.prototype.cmp = function(cmp){
  178. return !this.cmpNotSame.apply(this,[cmp]);
  179. }
  180. Buffer.prototype.cmpNotSame = function(cmp){
  181. var f=false;
  182. var nmax = this.length > cmp.length ? cmp.length : this.length;
  183. var n = 0;
  184. while(!f){
  185. if(cmp[n]===this[n]){
  186. n++
  187. }else{
  188. f=true;
  189. }
  190. if(n>nmax){
  191. return f;
  192. }
  193. }
  194. return f;
  195. }
  196. //console.log(crypto.getHashes());
  197. //console.log(new Buffer("10"));
  198. /*
  199. Buffer.prototype.scan = function(chunksize,each,end){
  200. var end = end || function(){};
  201. var each = each || function(){};
  202. var maxi = this.length;
  203. var items = 0;
  204. if( maxi % chunksize === 0){
  205. items = maxi/chunksize;
  206. }
  207. var indexat = 0;
  208. var self = this;
  209. var count = 0;
  210. function chunk(){
  211. var pp = (indexat+chunksize);
  212. pp = (pp > maxi ? maxi : pp);
  213. each.apply(each,[self.slice(indexat,pp),count]);
  214. count ++;
  215. if(indexat < self.length-chunksize){
  216. indexat += chunksize;
  217. process.nextTick(chunk);
  218. }else{
  219. end.apply(end,[count]);
  220. }
  221. }
  222. chunk();
  223. }
  224. */
  225. /*
  226. Buffer.prototype.extract = function(obj){
  227. var o = {};
  228. for(var prop in obj){
  229. if(obj[prop].pos){
  230. o[prop] = obj[prop].type(this.slice(obj[prop].pos[0],obj[prop].pos[1]));
  231. }else{
  232. if(typeof(obj[prop]) === "object"){
  233. o[prop] = {};
  234. for(var subprop in obj[prop]){
  235. if(obj[prop][subprop].pos){
  236. o[prop][subprop] = obj[prop][subprop].type(this.slice(obj[prop][subprop].pos[0],obj[prop][subprop].pos[1]));
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return o;
  243. }
  244. Buffer.prototype.chksum = function(encoding){
  245. var encoding = encoding || 'base64';
  246. var hash=crypto.createHash('sha256');
  247. hash.update(this);
  248. var r = hash.digest(encoding);
  249. if(encoding==="binary"){
  250. var rr = new Buffer(32);
  251. for(var i=0;i<32;i++){
  252. rr[i] = r.charCodeAt(i);
  253. }
  254. r = rr;
  255. }
  256. return r;
  257. }
  258. Buffer.prototype.chksuma = function(encoding){
  259. var encoding = encoding | 'base64';
  260. var hash=crypto.createHash('sha1');
  261. hash.update(this);
  262. return hash.digest(encoding);
  263. }
  264. Buffer.prototype.toHex = function(){
  265. var s = "";
  266. for(var i=0;i<this.length;i++){
  267. var byte = this[i].toString(16);
  268. byte = (byte.length<2) ? '0'+byte : byte;
  269. s += byte;
  270. }
  271. return s;
  272. }
  273. Buffer.prototype.toInt = function(){
  274. var l=this.length-1;
  275. var i=this[l];
  276. l--;
  277. var b = 256;
  278. while(l>-1){
  279. i += this[l]*b;
  280. l--;
  281. b *= 256;
  282. }
  283. return i;
  284. }
  285. Buffer.prototype.indexOf = function(bytes,start){
  286. var i=start||0;
  287. var len=this.length-bytes.length,found=false;
  288. while(!found && i<len){
  289. var a = this.slice(i,i+bytes.length);
  290. if(a.toString()===bytes.toString()){
  291. return i;
  292. }
  293. i++;
  294. }
  295. return false;
  296. };
  297. Buffer.prototype.fill = function(a){
  298. if(typeof(a)==="function"){
  299. for(var i=0;i<this.length;i++){
  300. this[i] = a.apply(this,[i]);
  301. }
  302. }else{
  303. for(var i=0;i<this.length;i++){
  304. this[i] = a;
  305. }
  306. }
  307. }
  308. Buffer.prototype.filla = function(a,ii,is){
  309. if(typeof(a)==="function"){
  310. for(var i=ii;i<is;i++){
  311. this[i] = a.apply(this,[i]);
  312. }
  313. }else{
  314. for(var i=ii;i<is;i++){
  315. this[i] = a;
  316. }
  317. }
  318. }
  319. function random_byte(){
  320. return Math.floor(Math.random()*255);
  321. }
  322. function add_byte(i){
  323. this[i] = this[i]+1;
  324. }
  325. */
  326. function hexdump(buf,showascii,perline,space,padit,linenums,showtype){
  327. var showtype = showtype || false;
  328. var s = "";
  329. if(showtype){
  330. s += "type: "+typeof(buf)+" "+((buf instanceof Buffer))+"\n";
  331. }
  332. if(typeof(buf) !== "object"){
  333. buf = new Buffer(buf);
  334. }
  335. var usebuf;
  336. var perline = (perline===0||perline) ? perline : 32;
  337. var space = (space===0||space) ? space : 8;
  338. var showascii = showascii || false;
  339. var linenums = linenums || false;
  340. if(perline === 0){
  341. perline = buf.length;
  342. }
  343. usebuf = buf;
  344. if(padit){
  345. var shouldbelength = Math.ceil(buf.length/perline)*perline;
  346. var nbuf = new Buffer(shouldbelength);
  347. nbuf.fill(0);
  348. buf.copy(nbuf,0,0,buf.length);
  349. usebuf = nbuf;
  350. }
  351. var tl = Math.ceil(buf.length/perline);
  352. for(var i=0;i<tl;i++){
  353. var mx = (i*perline)+perline;
  354. if(mx > usebuf.length){
  355. mx = usebuf.length;
  356. }
  357. if(linenums){
  358. s += intToHex(i*perline,3)+" ";
  359. }
  360. var a = "";
  361. var t = usebuf.slice(i*perline,mx);
  362. for(var y=0;y<t.length;y++){
  363. s += int2hex(t[y]);
  364. if((t[y] > 31) && (t[y] !== 127)){
  365. a += String.fromCharCode(t[y]);
  366. }else{
  367. a += ".";
  368. }
  369. if(y % space === (space-1)){
  370. s += " ";
  371. a += " ";
  372. }
  373. }
  374. if(showascii){
  375. s += " | " +a;
  376. }
  377. if(tl>1){
  378. s += "\n";
  379. }
  380. }
  381. return s;
  382. }
  383. function int2hex(integer) {
  384. integer = integer.toString(16);
  385. if (integer.length % 2 !== 0) {
  386. integer = '0' + integer;
  387. }
  388. return integer;
  389. };
  390. function int2word(integer) {
  391. integer = integer.toString(16);
  392. if (integer.length % 8 !== 0) {
  393. while(integer.length<8){
  394. integer = '0' + integer;
  395. }
  396. }
  397. return integer;
  398. };
  399. function int2longword(integer) {
  400. integer = integer.toString(16);
  401. if (integer.length % 16 !== 0) {
  402. while(integer.length<16){
  403. integer = '0' + integer;
  404. }
  405. }
  406. return integer;
  407. };
  408. exports.intToHex = intToHex;
  409. exports.hexToBytes = hexToBytes;
  410. exports.hexToBuffer = hexToBuffer;
  411. function hexToBuffer (hexa) {
  412. return new Buffer(hexToBytes(hexa));
  413. }
  414. function hexToBytes (hexa) {
  415. for (var bytes = [], c = 0; c < hexa.length/2; c ++)
  416. bytes.push(parseInt(hexa.substr(c*2, 2), 16));
  417. return bytes;
  418. };
  419. function intToHex (integera,bytes) {
  420. var bytes = bytes || 1;
  421. integera = integera.toString(16);
  422. if (integera.length % (bytes*2) !== 0) {
  423. while(integera.length<bytes*2){
  424. integera = '0' + integera;
  425. }
  426. }
  427. return integera;
  428. };
  429. exports.bufferToInt = function (arr){
  430. var l=arr.length-1;
  431. var i=arr[l];
  432. l--;
  433. var b = 256;
  434. while(l>-1){
  435. i += arr[l]*b;
  436. l--;
  437. b *= 256;
  438. }
  439. return i;
  440. }
  441. exports.intToBuffer = function (integer,bytesa){
  442. var hex = intToHex(integer,bytesa);
  443. var bb = hexToBytes(hex);
  444. return new Buffer(bb);
  445. }
  446. String.prototype.lpad = function(l,ww){
  447. var w = ww || " ";
  448. var s = this;
  449. while(s.length<l){
  450. s = w + s;
  451. }
  452. return s;
  453. }
  454. String.prototype.rpad = function(l,ww){
  455. var w = ww || " ";
  456. var s = this;
  457. while(s.length<l){
  458. s = s+w;
  459. }
  460. return s;
  461. }
  462. /*
  463. function hexdump(buf,showascii,perline,space,padit,linenums,showtype){
  464. var showtype = showtype || false;
  465. var s = "";
  466. if(showtype){
  467. s += "type: "+typeof(buf)+" "+((buf instanceof Buffer))+"\n";
  468. }
  469. if(typeof(buf) !== "object"){
  470. buf = new Buffer(buf);
  471. }
  472. var usebuf;
  473. var perline = (perline===0||perline) ? perline : 32;
  474. var space = (space===0||space) ? space : 8;
  475. var showascii = showascii || false;
  476. var linenums = linenums || false;
  477. if(perline === 0){
  478. perline = buf.length;
  479. }
  480. usebuf = buf;
  481. if(padit){
  482. var shouldbelength = Math.ceil(buf.length/perline)*perline;
  483. var nbuf = new Buffer(shouldbelength);
  484. nbuf.fill(0);
  485. buf.copy(nbuf,0,0,buf.length);
  486. usebuf = nbuf;
  487. }
  488. var tl = Math.ceil(buf.length/perline);
  489. for(var i=0;i<tl;i++){
  490. var mx = (i*perline)+perline;
  491. if(mx > usebuf.length){
  492. mx = usebuf.length;
  493. }
  494. if(linenums){
  495. s += intToHex(i*perline,3)+" ";
  496. }
  497. var a = "";
  498. var t = usebuf.slice(i*perline,mx);
  499. for(var y=0;y<t.length;y++){
  500. s += int2hex(t[y]);
  501. if((t[y] > 31) && (t[y] < 127 || t[y] > 127) && (t[y] !== 129 && t[y] !== 143 && t[y] !== 144 && t[y] !== 157 && t[y] !== 160 && t[y] !== 173 ) ){
  502. a += String.fromCharCode(t[y]);
  503. }else{
  504. a += ".";
  505. }
  506. if(y % space === (space-1)){
  507. s += " ";
  508. a += " ";
  509. }
  510. }
  511. if(showascii){
  512. s += " | " +a;
  513. }
  514. if(tl>1){
  515. s += "\n";
  516. }
  517. }
  518. return s;
  519. }
  520. */