encodeS wbuf True  set setH n bs = do
    let origLen = BS.length bs
        expectedLen = (origLen `div` 10) * 8 -- 80%: decided by examples
        expectedIntLen = integerLength n expectedLen
    ff wbuf expectedIntLen
    len <- encodeH wbuf bs
    let intLen = integerLength n len
    if origLen < len then do
        ff wbuf (negate (expectedIntLen + len))
        encodeI wbuf set n origLen
        copyByteString wbuf bs
      else if intLen == expectedIntLen then do
        ff wbuf (negate (expectedIntLen + len))
        encodeI wbuf (set . setH) n len
        ff wbuf len
      else do
        let gap = intLen - expectedIntLen
        shiftLastN wbuf gap len
        ff wbuf (negate (intLen + len))
        encodeI wbuf (set . setH) n  len
        ff wbuf len


encodeS wbuf True  set setH n bs = do
    let origLen = BS.length bs
        expectedLen = (origLen `div` 10) * 8 -- 80%: decided by examples
        expectedIntLen = integerLength n expectedLen
    ff wbuf expectedIntLen
    len <- encodeH wbuf bs
    let intLen = integerLength n len
    if origLen < len then do
        ff wbuf (negate (expectedIntLen + len))
        encodeI wbuf set n origLen
        copyByteString wbuf bs
      else if intLen == expectedIntLen then do
        ff wbuf (negate (expectedIntLen + len))
        encodeI wbuf (set . setH) n len
        ff wbuf len
      else do
        let gap = intLen - expectedIntLen
        shiftLastN wbuf gap len
        ff wbuf (negate (intLen + len))
        encodeI wbuf (set . setH) n  len
        ff wbuf len


encodeS wbuf True  set setH n bs = do
    let origLen = BS.length bs
        expectedLen = (origLen `div` 10) * 8 -- 80%: decided by examples
        expectedIntLen = integerLength n expectedLen
    ff wbuf expectedIntLen
    len <- encodeH wbuf bs
    let intLen = integerLength n len
    if origLen < len then do
        ff wbuf (negate (expectedIntLen + len))
        encodeI wbuf set n origLen
        copyByteString wbuf bs
      else if intLen == expectedIntLen then do
        ff wbuf (negate (expectedIntLen + len))
        encodeI wbuf (set . setH) n len
        ff wbuf len
      else do
        let gap = intLen - expectedIntLen
        shiftLastN wbuf gap len
        ff wbuf (negate (intLen + len))
        encodeI wbuf (set . setH) n  len
        ff wbuf len

