Skip to content

Commit

Permalink
fix: chunkSize for odd number of bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rochars committed Jun 11, 2018
1 parent 476f82f commit 7fa550f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 54 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 4.0.6 (2018-06-10)
- fix: chunkSize for odd number of bytes.

## 4.0.5 (2018-05-13)
- Better packaging.

Expand Down
24 changes: 12 additions & 12 deletions dist/riff-chunks-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun May 13 2018 23:30:47 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 11 2018 01:48:14 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h2>riffChunks.write()</h2><pre class="prettyprint source lang-javascript"><code
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun May 13 2018 23:30:47 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 11 2018 01:48:14 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
25 changes: 5 additions & 20 deletions docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ <h1 class="page-title">index.js</h1>
*/
function getSubChunks_(buffer) {
let chunks = [];
let i = fixIndex_(buffer, 12);
let i = 12;
while(i &lt;= buffer.length - 8) {
chunks.push(getSubChunk_(buffer, i));
i += 8 + chunks[chunks.length - 1]["chunkSize"];
i = i % 2 ? i + 1 : i;
}
return chunks;
}
Expand All @@ -136,7 +137,6 @@ <h1 class="page-title">index.js</h1>
* @private
*/
function getSubChunk_(buffer, index) {
index = fixIndex_(buffer, index);
let chunk = {
"chunkId": getChunkId_(buffer, index),
"chunkSize": getChunkSize_(buffer, index),
Expand All @@ -146,28 +146,13 @@ <h1 class="page-title">index.js</h1>
buffer.slice(index + 8, index + 12), fourCC_);
chunk["subChunks"] = getSubChunks_(buffer.slice(index));
} else {
let slc = chunk["chunkSize"] % 2 ? chunk["chunkSize"] + 1 : chunk["chunkSize"];
chunk["chunkData"] = buffer.slice(
index + 8, index + 8 + chunk["chunkSize"]);
index + 8, index + 8 + slc);
}
return chunk;
}

/**
* Fix the index for reading the chunkId for files
* with broken size descriptions.
* @param {!Uint8Array|!Array&lt;number>} buffer The buffer.
* @param {number} i The start index of the chunk.
* @return {number} The new index.
* @private
*/
function fixIndex_(buffer, i) {
while (buffer[i] == 0 || buffer[i+1] == 0 ||
buffer[i+2] == 0 || buffer[i+3] == 0) {
i++;
}
return i;
}

/**
* Return the fourCC_ of a chunk.
* @param {!Uint8Array|!Array&lt;number>} buffer the RIFF file bytes.
Expand Down Expand Up @@ -204,7 +189,7 @@ <h1 class="page-title">index.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun May 13 2018 23:30:47 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 11 2018 01:48:14 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
23 changes: 4 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ function writeSubChunks_(chunks) {
*/
function getSubChunks_(buffer) {
let chunks = [];
let i = fixIndex_(buffer, 12);
let i = 12;
while(i <= buffer.length - 8) {
chunks.push(getSubChunk_(buffer, i));
i += 8 + chunks[chunks.length - 1]["chunkSize"];
i = i % 2 ? i + 1 : i;
}
return chunks;
}
Expand All @@ -97,7 +98,6 @@ function getSubChunks_(buffer) {
* @private
*/
function getSubChunk_(buffer, index) {
index = fixIndex_(buffer, index);
let chunk = {
"chunkId": getChunkId_(buffer, index),
"chunkSize": getChunkSize_(buffer, index),
Expand All @@ -107,28 +107,13 @@ function getSubChunk_(buffer, index) {
buffer.slice(index + 8, index + 12), fourCC_);
chunk["subChunks"] = getSubChunks_(buffer.slice(index));
} else {
let slc = chunk["chunkSize"] % 2 ? chunk["chunkSize"] + 1 : chunk["chunkSize"];
chunk["chunkData"] = buffer.slice(
index + 8, index + 8 + chunk["chunkSize"]);
index + 8, index + 8 + slc);
}
return chunk;
}

/**
* Fix the index for reading the chunkId for files
* with broken size descriptions.
* @param {!Uint8Array|!Array<number>} buffer The buffer.
* @param {number} i The start index of the chunk.
* @return {number} The new index.
* @private
*/
function fixIndex_(buffer, i) {
while (buffer[i] == 0 || buffer[i+1] == 0 ||
buffer[i+2] == 0 || buffer[i+3] == 0) {
i++;
}
return i;
}

/**
* Return the fourCC_ of a chunk.
* @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "riff-chunks",
"version": "4.0.5",
"version": "4.0.6",
"description": "Read and write the chunks of RIFF and RIFX files.",
"homepage": "https://github.com/rochars/riff-chunks",
"author": "Rafael da Silva Rocha <[email protected]>",
Expand Down

0 comments on commit 7fa550f

Please sign in to comment.