Skip to content

Commit

Permalink
Review: break hexstring convertion into 2 separate tools methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Samsel committed Sep 25, 2017
1 parent 701aaf3 commit 97082cd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 30 deletions.
51 changes: 31 additions & 20 deletions core/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -1518,10 +1518,27 @@
},

/**
* Converts hexstring to base64 coode.
* Convert hex string to array contained 1 byte in each cell. Bytes are represent as a Integer number.
*
* Hexstring contains `8bit` numbers, where base64 use `6bit` to code.
* We need to process `3 * 8bit` chunks of hexstring into `4 * 6bit` characters of base64.
* @since 4.8.0
* @param {String} hexString Contains input string which represent bytes, e.g. `"08A11D8ADA2B"`
* @return {Array} Bytes stored in a form o Integer numebrs, e.g. `[ 8, 161, 29, 138, 218, 43 ]`
*/
convertHexStringToBytes: function( hexString ) {
var bytesArray = [],
bytesArrayLength = hexString.length / 2,
i;

for ( i = 0; i < bytesArrayLength; i++ ) {
bytesArray.push( parseInt( hexString.substr( i * 2, 2 ), 16 ) );
}
return bytesArray;
},

/**
* Convert bytes array into base64 code.
*
* Bytes are `8bit` numbers, where base64 use `6bit` to store data. That's why we process 3 Bytes into 4 charcters represent by base64.
*
* Algorithm:
*
Expand All @@ -1533,29 +1550,23 @@
*
* Example:
*
* * Hex: `08A11D8ADA2B` -> binary: `0000 1000 1010 0001 0001 1101 1000 1010 1101 1010 0010 1011`.
* * `|` (pipe) shows where base64 will cut bits during transform.
* * Binary: `0000 10|00 1010| 0001 00|01 1101| 1000 10|10 1101| 1010 00|10 1011`.
* * Now we have 6bit numbers (decimal values below), which are translated to indexes in `base64characters` array.
* * Decimal: `2 10 4 29 34 45 40 43` -> base64: `CKEditor`.
* * Bytes Array: [ 8, 161, 29, 138, 218, 43 ] -> binary: `0000 1000 1010 0001 0001 1101 1000 1010 1101 1010 0010 1011`.
* * Binary: `0000 10|00 1010| 0001 00|01 1101| 1000 10|10 1101| 1010 00|10 1011` ← `|` (pipe) shows where base64 will cut bits during transformation.
* * Now we have 6bit numbers (written in decimal values), which are translated to indexes in `base64characters` array.<br />
* Decimal: `2 10 4 29 34 45 40 43` → base64: `CKEditor`.
*
* @since 4.8.0
* @param {String} hexString String contained only hexadecimal values, e.g. `0AB7`.
* @returns {String} Base64 string represnet input value.
* @param bytesArray Array which stores 1 byte in each cell as Integer number.
* @returns Base64 string which reprepsents input bytes.
*/
convertHexstringToBase64: function( hexString ) {
var binaryArray = [],
base64characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
convertBytesToBase64: function( bytesArray ) {
var base64characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
base64string = '',
binArrLength = hexString.length / 2,
bytesArrayLength = bytesArray.length,
i;

for ( i = 0; i < binArrLength; i++ ) {
binaryArray.push( parseInt( hexString.substr( i * 2, 2 ), 16 ) );
}

for ( i = 0; i < binArrLength; i += 3 ) {
var array3 = binaryArray.slice( i, i + 3 ),
for ( i = 0; i < bytesArrayLength; i += 3 ) {
var array3 = bytesArray.slice( i, i + 3 ),
array3length = array3.length,
array4 = [],
j;
Expand Down
64 changes: 54 additions & 10 deletions tests/core/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,41 +778,85 @@
},

// #662
'test hex2base64 converter': function() {
var hex = [
'test hexstring to bytes converter': function() {
var testCases = [
{
hex: '00',
base64: 'AA=='
bytes: [ 0 ]
},
{
hex: '000000',
base64: 'AAAA'
bytes: [ 0, 0, 0 ]
},
{
hex: '011001',
base64: 'ARAB'
bytes: [ 1, 16, 1 ]
},
{
hex: '0123456789ABCDEF',
bytes: [ 1, 35, 69, 103, 137, 171, 205, 239 ]
},
{
hex: 'FFFFFFFF',
bytes: [ 255, 255, 255, 255 ]
},
{
hex: 'fc0fc0',
bytes: [ 252, 15, 192 ]
},
{
hex: '08A11D8ADA2B',
bytes: [ 8, 161, 29, 138, 218, 43 ]
}
];
CKEDITOR.tools.array.forEach( testCases, function( test ) {
assert.isTrue( CKEDITOR.tools.arrayCompare( test.bytes, CKEDITOR.tools.convertHexStringToBytes( test.hex ) ) );
} );
},

// #662
'test bytes to base64 converter': function() {
var testCases = [
{
bytes: [ 0 ],
base64: 'AA=='
},
{
bytes: [ 0, 0, 0 ],
base64: 'AAAA'
},
{
bytes: [ 1, 16, 1 ],
base64: 'ARAB'
},
{
bytes: [ 1, 35, 69, 103, 137, 171, 205, 239 ],
base64: 'ASNFZ4mrze8='
},
{
hex: 'FFFFFF',
bytes: [ 255, 255, 255 ],
base64: '////'
},
{
hex: 'fc0fc0',
bytes: [ 252, 15, 192 ],
base64: '/A/A'
},
{
hex: '08A11D8ADA2B',
bytes: [ 8, 161, 29, 138, 218, 43 ],
base64: 'CKEditor'
},
{
// jscs:disable
bytes: [ 0, 16, 131, 16, 81, 135, 32, 146, 139, 48, 211, 143, 65, 20, 147, 81, 85, 151, 97, 150, 155, 113, 215, 159, 130, 24, 163, 146, 89, 167, 162, 154, 171, 178, 219, 175, 195, 28, 179, 211, 93, 183, 227, 158, 187, 243, 223, 191 ],
// jscs:enable
base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
}
];

CKEDITOR.tools.array.forEach( hex, function( item ) {
assert.areSame( item.base64, CKEDITOR.tools.hexstring2base64( item.hex ) );
CKEDITOR.tools.array.forEach( testCases, function( test ) {
assert.areSame( test.base64, CKEDITOR.tools.convertBytesToBase64( test.bytes ) );
} );
}

} );
} )();

0 comments on commit 97082cd

Please sign in to comment.