Skip to content

Commit

Permalink
bin type cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Jun 15, 2014
1 parent d24810f commit dfea427
Showing 1 changed file with 26 additions and 46 deletions.
72 changes: 26 additions & 46 deletions lib/js-yaml/type/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,28 @@ var BASE64_CHARTABLE =


function validateYamlBinary(data) {
var value, code, idx = 0, result = [], leftbits, leftdata;
var code, idx = 0, len = data.length, leftbits;

leftbits = 0; // number of bits decoded, but yet to be appended
leftdata = 0; // bits decoded, but yet to be appended

// Convert one by one.
for (idx = 0; idx < data.length; idx += 1) {
for (idx = 0; idx < len; idx += 1) {
code = data.charCodeAt(idx);
value = BASE64_BINTABLE[code & 0x7F];

// Skip LF(NL) || CR
if (0x0A !== code && 0x0D !== code) {
// Fail on illegal characters
if (-1 === value) {
return false;
}

// Collect data into leftdata, update bitcount
leftdata = (leftdata << 6) | value;
leftbits += 6;
if (0x0A === code || 0x0D === code) { continue; }

// If we have 8 or more bits, append 8 bits to the result
if (leftbits >= 8) {
leftbits -= 8;
// Fail on illegal characters
if (-1 === BASE64_BINTABLE[code & 0x7F]) {
return false;
}

// Append if not padding.
if (BASE64_PADDING !== data.charAt(idx)) {
result.push((leftdata >> leftbits) & 0xFF);
}
// update bitcount
leftbits += 6;

leftdata &= (1 << leftbits) - 1;
}
// If we have 8 or more bits, append 8 bits to the result
if (leftbits >= 8) {
leftbits -= 8;
}
}

Expand All @@ -73,44 +63,34 @@ function validateYamlBinary(data) {
}

function resolveYamlBinary(data) {
var value, code, idx = 0, result = [], leftbits, leftdata;
var value, code, idx = 0, len = data.length, result = [], leftbits, leftdata;

leftbits = 0; // number of bits decoded, but yet to be appended
leftdata = 0; // bits decoded, but yet to be appended

// Convert one by one.
for (idx = 0; idx < data.length; idx += 1) {
for (idx = 0; idx < len; idx += 1) {
code = data.charCodeAt(idx);
value = BASE64_BINTABLE[code & 0x7F];

// Skip LF(NL) || CR
if (0x0A !== code && 0x0D !== code) {
// Fail on illegal characters
if (-1 === value) {
throw new Error('birany resolve error');
}
if (0x0A === code || 0x0D === code) { continue; }

// Collect data into leftdata, update bitcount
leftdata = (leftdata << 6) | value;
leftbits += 6;
// Collect data into leftdata, update bitcount
leftdata = (leftdata << 6) | value;
leftbits += 6;

// If we have 8 or more bits, append 8 bits to the result
if (leftbits >= 8) {
leftbits -= 8;
// If we have 8 or more bits, append 8 bits to the result
if (leftbits >= 8) {
leftbits -= 8;

// Append if not padding.
if (BASE64_PADDING !== data.charAt(idx)) {
result.push((leftdata >> leftbits) & 0xFF);
}

leftdata &= (1 << leftbits) - 1;
// Append if not padding.
if (BASE64_PADDING !== data.charAt(idx)) {
result.push((leftdata >> leftbits) & 0xFF);
}
}
}

// If there are any bits left, the base64 string was corrupted
if (leftbits) {
throw new Error('birany resolve error');
leftdata &= (1 << leftbits) - 1;
}
}

// Wrap into Buffer for NodeJS and leave Array for browser
Expand Down

0 comments on commit dfea427

Please sign in to comment.