@ -1,4 +1,3 @@
/* eslint-disable no-constant-condition */
/ * T h i s s o f t w a r e i s l i c e n s e d u n d e r t h e M I T L i c e n s e .
Copyright ( c ) 2016 desudesutalk
@ -112,7 +111,7 @@ import { BitstreamWriter, revbyte } from "./bitstream";
// Standard Huffman tables for coder initialization
// ===========================================================================================================
function * f5get ( ) : Generator < void , Buffer , number > {
function * f5get() {
let extrBit = 0 ;
//var pm = this.#stegShuffle(coeff),
@ -132,7 +131,8 @@ function* f5get(): Generator<void, Buffer, number> {
let len = 0 ;
while ( toread -- ) {
len = len * 2 + ( yield ) ;
const b = ( yield ) ;
len = len * 2 + b ;
}
const b = yield ;
toread += 8 ;
@ -141,12 +141,13 @@ function* f5get(): Generator<void, Buffer, number> {
else
len *= 2 ;
while ( toread -- ) {
len = len * 2 + ( yield ) ;
const b = ( yield ) ;
len = len * 2 + b ;
}
le t rlen = revbyte ( len , b ? 23 : 16 ) ;
cons t rlen = revbyte ( len , b ? 23 : 16 ) ;
len = rlen ;
if ( len >= 256 ) // don't know if length decoding is correct so let's be safe and trim
len = rlen = 256 ;
if ( len > 256 )
throw new Error ( "Too big for Smash" ) ;
len *= 8 ; // bytes to bits
// k must be 1
const chunks : Uint8Array [ ] = [ ] ;
@ -348,7 +349,7 @@ export class f5stego {
_examined = 0 ,
_thrown = 0 ,
shuffled_index = 0 ,
i , ii : number ;
i , n , ii : number ;
//let { gamma, pm } = this.#stegShuffle(coeff_count);
//let gammaI = 0;
@ -358,7 +359,7 @@ export class f5stego {
data_idx = 0 ,
available_bits_to_embed = 0 ;
const n = ( 1 << k ) - 1 ;
n = ( 1 << k ) - 1 ;
byte_to_embed = k - 1 ;
byte_to_embed ^= 0 ; // nop
@ -447,8 +448,8 @@ export class f5stego {
_examined += n ;
while ( true ) {
let vhash = 0 ;
let extracted_bit ;
var vhash = 0 ,
extracted_bit ;
for ( i = 0 ; i < code_word . length ; i ++ ) {
if ( coeff [ code_word [ i ] ] > 0 ) {
@ -564,21 +565,21 @@ export class f5stego {
coeff . set ( comp . blocks ) ;
let pos = - 1 ,
extrBit = 0 ;
const cCount = coeff . length - 1 ;
extrBit = 0 ,
cCount = coeff . length - 1 ;
//var pm = this.#stegShuffle(coeff),
// gamma = pm.gamma,
// gammaI = 0;
let k = 0 ;
let n , k = 0 ;
cons t out = new Uint8Array ( ( coeff . length / 8 ) | 0 ) ;
let extrByte = 0 ;
let outPos = 0 ;
let bitsAvail = 0 ;
let code = 0 ;
let hash = 0 ;
le t out = new Uint8Array ( ( coeff . length / 8 ) | 0 ) ,
extrByte = 0 ,
outPos = 0 ,
bitsAvail = 0 ,
code = 0 ,
hash = 0 ;
while ( bitsAvail < 4 ) {
pos ++ ;
@ -598,7 +599,7 @@ export class f5stego {
//k = (k ^ gamma[gammaI++] & 15) + 1;
k = ( k & 15 ) + 1 ;
const n = ( 1 << k ) - 1 ;
n = ( 1 << k ) - 1 ;
bitsAvail = 0 ;
@ -677,15 +678,15 @@ export class f5stego {
return out . subarray ( s , s + l ) ;
}
gengen ! : Generator < void , Buffer , number > ;
gengen : Generator < void , Buffer , number > ;
parse ( data : Uint8Array , tolerant = false ) {
let offset = 0 ;
function _buildHuffmanTable ( nrcodes : Uint8Array , values : Uint8Array ) {
let codevalue = 0 ;
let pos_in_table = 0 ;
const HT = new Uint16Array ( 65536 ) ;
let codevalue = 0 ,
pos_in_table = 0 ,
HT = new Uint16Array ( 65536 ) ;
for ( let k = 0 ; k < 16 ; k ++ ) {
for ( let j = 0 ; j < nrcodes [ k ] ; j ++ ) {
for ( let i = codevalue << ( 15 - k ) , cntTo = ( ( codevalue + 1 ) << ( 15 - k ) ) ; i < cntTo ; i ++ ) {
@ -709,13 +710,14 @@ export class f5stego {
successivePrev : number ,
successive : number ) = > {
cons t startOffset = offset ;
let bitsData = 0 ,
le t startOffset = offset ,
bitsData = 0 ,
bitsCount = 0 ,
eobrun = 0 ;
const p1 = 1 << successive ; /* 1 in the bit position being coded */
const m1 = - 1 << successive ; /* -1 in the bit position being coded */
eobrun = 0 ,
p1 = 1 << successive , /* 1 in the bit position being coded */
m1 = - 1 << successive ; /* -1 in the bit position being coded */
const prevpos = 0 ;
const decodeBaseline = ( component : typeof components [ 0 ] , pos : number ) = > {
while ( bitsCount < 16 ) {
bitsData = ( bitsData << 8 ) + ( data [ offset ] | 0 ) ;
@ -770,19 +772,8 @@ export class f5stego {
if ( ( bitsData & 0xff ) == 0xFF ) offset ++ ;
bitsCount += 8 ;
}
if ( component . componentId == 1 ) {
// EMIT BITS HERE
component . blocks [ pos + k ] = ( bitsData >>> ( bitsCount - s ) ) & ( ( 1 << s ) - 1 ) ;
if ( component . blocks [ pos + k ] < 1 << ( s - 1 ) ) component . blocks [ pos + k ] += ( - 1 << s ) + 1 ;
if ( this . gengen && component . blocks [ pos + k ] != 0 ) {
const v = ( component . blocks [ pos + k ] < 0 ) ? 1 - ( component . blocks [ pos + k ] & 1 ) : ( component . blocks [ pos + k ] & 1 ) ;
const it = this . gengen . next ( v ) ;
if ( it . done ) {
throw it . value ;
}
}
}
component . blocks [ pos + k ] = ( bitsData >>> ( bitsCount - s ) ) & ( ( 1 << s ) - 1 ) ;
if ( component . blocks [ pos + k ] < 1 << ( s - 1 ) ) component . blocks [ pos + k ] += ( - 1 << s ) + 1 ;
bitsCount -= s ;
k ++ ;
}
@ -972,6 +963,23 @@ export class f5stego {
let marker , mcuExpected , i , j , k , n , mcusPerLine , mcusPerRow , x , y ;
let lastflushidx = 0 ;
const flushBits = ( ) = > {
const component = components . find ( e = > e . componentId == 1 ) ! ;
// EMIT BITS HERE
while ( component . blocks [ lastflushidx + 1 ] !== undefined ) {
const blk = component . blocks [ lastflushidx ] ;
if ( blk != 0 ) {
const v = ( blk < 0 ) ? 1 - ( blk & 1 ) : ( blk & 1 ) ;
const it = this . gengen . next ( v ) ;
if ( it . done ) {
throw it . value ;
}
}
lastflushidx ++ ;
}
} ;
if ( components . length == 1 ) {
mcusPerLine = components [ 0 ] . blocksPerLine ;
mcusPerRow = components [ 0 ] . blocksPerColumn ;
@ -1010,6 +1018,7 @@ export class f5stego {
}
}
flushBits ( ) ;
}
} else {
mcusPerLine = frame . mcusPerLine ;
@ -1052,6 +1061,7 @@ export class f5stego {
}
}
}
flushBits ( ) ;
}
}
offset -= ( bitsCount / 8 ) | 0 ;
@ -1129,8 +1139,8 @@ export class f5stego {
if ( this . # frame . scanLines * this . # frame . samplesPerLine > this . maxPixels ) throw "Image is too big." ;
const componentsCount = data [ offset ++ ] ;
let componentId ;
var componentsCount = data [ offset ++ ] ,
componentId ;
let maxH = 0 ,
maxV = 0 ;
for ( i = 0 ; i < componentsCount ; i ++ ) {
@ -1647,4 +1657,5 @@ export class f5stego {
return byteout . slice ( 0 , outpos ) ;
}
}