Permalink
Browse files

o redacting MD5.

  • Loading branch information...
1 parent 7e27dab commit f8cb9e52dc92fa2eb226f22ea30e8685a06763bb @hzeller committed Sep 12, 2012
Showing with 40 additions and 30 deletions.
  1. +5 −6 conversion-buffer.cc
  2. +5 −3 conversion-buffer.h
  3. +30 −21 convolver.cc
View
11 conversion-buffer.cc
@@ -48,12 +48,14 @@ sf_count_t ConversionBuffer::SndWrite(const void *ptr, sf_count_t count,
}
// These callbacks we don't care about.
-static sf_count_t DummySeek(sf_count_t offset, int whence, void *user_data) {
+static sf_count_t DummySeek(sf_count_t offset, int whence, void *userdata) {
// This seems to be called after we're closing, probably to modify the
// header. It actually attempts to write that end up at the end of the
// file. We don't care, it is not accessed for reading anymore.
// TODO(hzeller): Suppress writing after close() and really warn
- fprintf(stderr, "DummySeek called %ld\n", offset);
+ if (reinterpret_cast<ConversionBuffer*>(userdata)->sndfile_writes_enabled()) {
+ fprintf(stderr, "DummySeek called %ld\n", offset);
+ }
return 0;
}
static sf_count_t DummyRead(void *ptr, sf_count_t count, void *user_data) {
@@ -89,10 +91,7 @@ ssize_t ConversionBuffer::Append(const void *data, size_t count) {
}
ssize_t ConversionBuffer::SndAppend(const void *data, size_t count) {
- if (!snd_writing_enabled_) {
- fprintf(stderr, "Skipping %ld\n", count);
- return count;
- }
+ if (!snd_writing_enabled_) return count;
return Append(data, count);
}
View
8 conversion-buffer.h
@@ -57,10 +57,12 @@ class ConversionBuffer {
// but can be used to write raw data as well.
ssize_t Append(const void *data, size_t count);
- // Enable writing. If set to 'false', no writing through the SNDFILE is
- // making it through. To be used to suppress writing of the header or
+ // Enable writing through the SNDFILE.
+ // If set to 'false', writes via the SNDFILE are ignored.
+ // To be used to suppress writing of the header or
// footer if we want to handle that on our own.
- void allow_sndfile_writes(bool b) { snd_writing_enabled_ = b; }
+ void set_sndfile_writes_enabled(bool b) { snd_writing_enabled_ = b; }
+ bool sndfile_writes_enabled() const { return snd_writing_enabled_; }
private:
static sf_count_t SndTell(void *userdata);
View
51 convolver.cc
@@ -111,6 +111,7 @@ class SndFileFilter :
}
virtual int Close() {
+ output_buffer_->set_sndfile_writes_enabled(false);
if (snd_in_) sf_close(snd_in_);
if (snd_out_) sf_close(snd_out_);
return close(filedes_) == -1 ? -errno : 0;
@@ -163,19 +164,19 @@ class SndFileFilter :
output_buffer_ = new ConversionBuffer(this, out_info);
}
- virtual void SetOutputSoundfile(ConversionBuffer *parent,
+ virtual void SetOutputSoundfile(ConversionBuffer *out_buffer,
SNDFILE *sndfile) {
snd_out_ = sndfile;
if (snd_out_ == NULL) {
error_ = true;
fprintf(stderr, "Opening output: %s\n", sf_strerror(NULL));
return;
}
+ out_buffer->set_sndfile_writes_enabled(false);
if (copy_flac_header_) {
- parent->allow_sndfile_writes(false);
- CopyFlacHeader(parent);
+ CopyFlacHeader(out_buffer);
} else {
- parent->allow_sndfile_writes(true);
+ out_buffer->set_sndfile_writes_enabled(true);
// Copy strings. Everything else that follows will be stream bytes.
for (int i = SF_STR_FIRST; i <= SF_STR_LAST; ++i) {
const char *s = sf_get_string(snd_in_, i);
@@ -187,8 +188,9 @@ class SndFileFilter :
// Now flush the header: that way if someone only reads the metadata, then
// our AddMoreSoundData() is never called.
sf_command(snd_out_, SFC_UPDATE_HEADER_NOW, NULL, 0);
- parent->allow_sndfile_writes(true);
fprintf(stderr, "Header init done.\n");
+
+ out_buffer->set_sndfile_writes_enabled(true); // ready for sound-stream.
}
virtual bool AddMoreSoundData() {
@@ -256,38 +258,45 @@ class SndFileFilter :
return pos;
}
- void CopyFlacHeader(ConversionBuffer *parent) {
+ void CopyFlacHeader(ConversionBuffer *out_buffer) {
fprintf(stderr, "Copy raw flac header\n");
- parent->Append("fLaC", 4);
+ out_buffer->Append("fLaC", 4);
off_t pos = 4;
unsigned char header[4];
- bool need_padding = false;
+ bool need_finish_padding = false;
while (pread(filedes_, header, sizeof(header), pos) == sizeof(header)) {
pos += sizeof(header);
bool is_last = header[0] & 0x80;
unsigned int type = header[0] & 0x7F;
unsigned int byte_len = (header[1] << 16) + (header[2] << 8) + header[3];
- fprintf(stderr, " type: %d, len: %u %s ", type,
- byte_len, is_last ? "(last-header)" : "(cont)");
- // The SEEKTABLE header we skip, because it is bogus after encoding.
- if (type == FLAC__METADATA_TYPE_SEEKTABLE) {
- fprintf(stderr, " (this is the seektable; skipped)\n");
- pos += byte_len;
- need_padding = is_last; // if we were last, force finish block.
- } else {
- parent->Append(&header, sizeof(header));
- pos = CopyBytes(filedes_, pos, parent, byte_len);
- need_padding = false;
+ fprintf(stderr, " type: %d, len: %6u %s ", type,
+ byte_len, is_last ? "(last)" : "(cont)");
+ need_finish_padding = false;
+ if (type == FLAC__METADATA_TYPE_STREAMINFO && byte_len == 34) {
+ out_buffer->Append(&header, sizeof(header));
+ CopyBytes(filedes_, pos, out_buffer, byte_len - 16);
+ for (int i = 0; i < 16; ++i) out_buffer->Append("\0", 1);
+ fprintf(stderr, " (copy streaminfo, but redacted MD5)\n");
+ }
+ else if (type == FLAC__METADATA_TYPE_SEEKTABLE) {
+ // The SEEKTABLE header we skip, because it is bogus after encoding.
+ fprintf(stderr, " (skip the seektable)\n");
+ need_finish_padding = is_last; // if we were last, force finish block.
+ }
+ else {
+ out_buffer->Append(&header, sizeof(header));
+ CopyBytes(filedes_, pos, out_buffer, byte_len);
fprintf(stderr, " (ok)\n");
}
+ pos += byte_len;
if (is_last)
break;
}
- if (need_padding) { // if the last block was not is_last: pad.
+ if (need_finish_padding) { // if the last block was not is_last: pad.
fprintf(stderr, "write padding\n");
memset(&header, 0, sizeof(header));
header[0] = 0x80 /* is last */ | FLAC__METADATA_TYPE_PADDING;
- parent->Append(&header, sizeof(header));
+ out_buffer->Append(&header, sizeof(header));
}
}

0 comments on commit f8cb9e5

Please sign in to comment.