Files @ 51344a25b55d
Branch filter:

Location: CryptoJS/src/util.js

Laman
array padding refactored into util
export const MASK=0xffffffff;

export function zeroPad(arr,length){
	return arr.concat((new Array(length)).fill(0)).slice(0,length);
}

export function bytes2int32(arr){
	return arr.reduce((acc,b,i)=>acc|b<<(i*8));
}

export function bytes2int32s(arr){
	let res=[];
	for(let i=0;i<arr.length;i+=4){
		res.push(bytes2int32(arr.slice(i,i+4)));
	}
	return res;
}

export function int322bytes(x){
	let res=[];
	for(let i=0;i<4;i++){
		res.push(x&0xff);
		x>>>=8;
	}
	return res;
}

export function int32s2bytes(arr){
	return arr.map(int322bytes).reduce((acc,bytes)=>acc.concat(bytes));
}

export function bytes2hex(arr){
	return arr.map(x=>x.toString(16).padStart(2,"0")).join("");
}

export function str2utf8(s){
	let res=[];
	let c=s.codePointAt(0);
	for(let i=0; c!==undefined; i++,c=s.codePointAt(i)){
		if(c<0x80){res.push(c);}
		else if(c<0x800){
			res.push(0b11000000|(c>>>6));
			res.push(0b10000000|(c&0b111111));
		}
		else if(c<0x10000){
			res.push(0b11100000|(c>>>12));
			res.push(0b10000000|((c>>>6)&0b111111));
			res.push(0b10000000|(c&0b111111));
		}
		else{
			res.push(0b11110000|(c>>>18));
			res.push(0b10000000|((c>>>12)&0b111111));
			res.push(0b10000000|((c>>>6)&0b111111));
			res.push(0b10000000|(c&0b111111));
		}
		if(c>0xffff){i++;} // skip surrogate
	}
	return res;
}

export function utf82str(arr){
	let res=[];
	for(let i=0;i<arr.length;i++){
		let x=arr[i];
		if(x<=0b1111111){res.push(x);}
		else if(x<=0b11011111){
			let a=x&0b11111;
			let b=arr[++i]&0b111111;
			res.push(a<<6|b);
		}
		else if(x<=0b11101111){
			let a=x&0b1111;
			let b=arr[++i]&0b111111;
			let c=arr[++i]&0b111111;
			res.push(a<<12|b<<6|c);
		}
		else{
			let a=x&0b111;
			let b=arr[++i]&0b111111;
			let c=arr[++i]&0b111111;
			let d=arr[++i]&0b111111;
			res.push(a<<18|b<<12|c<<6|d);
		}
	}
	return res.map(x=>String.fromCodePoint(x)).join("");
}