Skip to content

Commit

Permalink
fixed #4
Browse files Browse the repository at this point in the history
embed compressed data into WAV files and improve data extraction handling
  • Loading branch information
scott-mescudi committed Jan 5, 2025
1 parent 92918e0 commit 51f5401
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
41 changes: 38 additions & 3 deletions audio.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package stegano

import (
"fmt"

u "github.com/scott-mescudi/stegano/pkg"
c "github.com/scott-mescudi/stegano/compression"
)

// EmbedDataIntoWAVWithDepth embeds compressed data into a WAV file with a specified bit depth.
func (s *AudioEmbedHandler) EmbedDataIntoWAVWithDepth(audioFilename, outputFilename string, data []byte, bitDepth uint8) ( error) {
if bitDepth >= 8 {
return ErrDepthOutOfRange
}

nd, err := c.CompressZSTD(data)
if err != nil {
return err
}


decoder := GetAudioData(audioFilename)
buffer, err := decoder.FullPCMBuffer()
if err != nil {
return err
}


buffer = u.EmbedDataWithDepthAudio(buffer, data, bitDepth)
buffer = u.EmbedDataWithDepthAudio(buffer, nd, bitDepth)

err = WriteAudioFile(outputFilename, decoder, buffer)
if err != nil {
Expand All @@ -40,8 +48,35 @@ func (s *AudioExtractHandler) ExtractDataFromWAVWithDepth(audioFilename string,
}

data := u.ExtractDataWithDepthAudio(buffer, bitDepth)


return data, nil
lenData, err := u.GetlenOfData(data)
if err != nil {
return nil, err
}


var moddedData = make([]byte, 0, lenData)
defer func() {
if r := recover(); r != nil {
moddedData = nil
err = fmt.Errorf("fatal error: %v", r)
}
}()

for i := 4; i < lenData+4; i++ {
if i >= len(data) {
return nil, fmt.Errorf("index out of range while accessing data: %d", i)
}
moddedData = append(moddedData, data[i])
}

nd, err := c.DecompressZSTD(moddedData)
if err != nil {
return nil, err
}

return nd, nil
}

// EmbedDataIntoWAVAtDepth embeds compressed data into a WAV file at a specified bit depth.
Expand Down
6 changes: 2 additions & 4 deletions pkg/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func ExtractDataAtDepthAudio(buffer *audio.IntBuffer, depth uint8) []byte {
}
}

leng, _ := GetlenOfData(data)
return data[4:(leng*2)+1]
return data
}


Expand Down Expand Up @@ -90,7 +89,6 @@ func ExtractDataWithDepthAudio(buffer *audio.IntBuffer, depth uint8) []byte {
}


leng, _ := GetlenOfData(byteSlice)
return byteSlice[4:(leng*2)+1]
return byteSlice
}

0 comments on commit 51f5401

Please sign in to comment.