123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /* eslint-disable no-bitwise */
- /* eslint-disable no-mixed-operators */
-
- /**
- * Generates a SAS composed of decimal numbers.
- * Borrowed from the Matrix JS SDK.
- *
- * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
- * @returns Array<number>
- */
- function generateDecimalSas(sasBytes) {
- /**
- * +--------+--------+--------+--------+--------+
- * | Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
- * +--------+--------+--------+--------+--------+
- * bits: 87654321 87654321 87654321 87654321 87654321
- * \____________/\_____________/\____________/
- * 1st number 2nd number 3rd number
- */
- return [
- (sasBytes[0] << 5 | sasBytes[1] >> 3) + 1000,
- ((sasBytes[1] & 0x7) << 10 | sasBytes[2] << 2 | sasBytes[3] >> 6) + 1000,
- ((sasBytes[3] & 0x3f) << 7 | sasBytes[4] >> 1) + 1000
- ];
- }
-
- const emojiMapping = [
- [ '๐ถ', 'dog' ],
- [ '๐ฑ', 'cat' ],
- [ '๐ฆ', 'lion' ],
- [ '๐', 'horse' ],
- [ '๐ฆ', 'unicorn' ],
- [ '๐ท', 'pig' ],
- [ '๐', 'elephant' ],
- [ '๐ฐ', 'rabbit' ],
- [ '๐ผ', 'panda' ],
- [ '๐', 'rooster' ],
- [ '๐ง', 'penguin' ],
- [ '๐ข', 'turtle' ],
- [ '๐', 'fish' ],
- [ '๐', 'octopus' ],
- [ '๐ฆ', 'butterfly' ],
- [ '๐ท', 'flower' ],
- [ '๐ณ', 'tree' ],
- [ '๐ต', 'cactus' ],
- [ '๐', 'mushroom' ],
- [ '๐', 'globe' ],
- [ '๐', 'moon' ],
- [ 'โ๏ธ', 'cloud' ],
- [ '๐ฅ', 'fire' ],
- [ '๐', 'banana' ],
- [ '๐', 'apple' ],
- [ '๐', 'strawberry' ],
- [ '๐ฝ', 'corn' ],
- [ '๐', 'pizza' ],
- [ '๐', 'cake' ],
- [ 'โค๏ธ', 'heart' ],
- [ '๐', 'smiley' ],
- [ '๐ค', 'robot' ],
- [ '๐ฉ', 'hat' ],
- [ '๐', 'glasses' ],
- [ '๐ง', 'spanner' ],
- [ '๐
', 'santa' ],
- [ '๐', 'thumbs up' ],
- [ 'โ๏ธ', 'umbrella' ],
- [ 'โ', 'hourglass' ],
- [ 'โฐ', 'clock' ],
- [ '๐', 'gift' ],
- [ '๐ก', 'light bulb' ],
- [ '๐', 'book' ],
- [ 'โ๏ธ', 'pencil' ],
- [ '๐', 'paperclip' ],
- [ 'โ๏ธ', 'scissors' ],
- [ '๐', 'lock' ],
- [ '๐', 'key' ],
- [ '๐จ', 'hammer' ],
- [ 'โ๏ธ', 'telephone' ],
- [ '๐', 'flag' ],
- [ '๐', 'train' ],
- [ '๐ฒ', 'bicycle' ],
- [ 'โ๏ธ', 'aeroplane' ],
- [ '๐', 'rocket' ],
- [ '๐', 'trophy' ],
- [ 'โฝ', 'ball' ],
- [ '๐ธ', 'guitar' ],
- [ '๐บ', 'trumpet' ],
- [ '๐', 'bell' ],
- [ 'โ๏ธ', 'anchor' ],
- [ '๐ง', 'headphones' ],
- [ '๐', 'folder' ],
- [ '๐', 'pin' ]
- ];
-
- /**
- * Generates a SAS composed of defimal numbers.
- * Borrowed from the Matrix JS SDK.
- *
- * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
- * @returns Array<number>
- */
- function generateEmojiSas(sasBytes) {
- // Just like base64.
- const emojis = [
- sasBytes[0] >> 2,
- (sasBytes[0] & 0x3) << 4 | sasBytes[1] >> 4,
- (sasBytes[1] & 0xf) << 2 | sasBytes[2] >> 6,
- sasBytes[2] & 0x3f,
- sasBytes[3] >> 2,
- (sasBytes[3] & 0x3) << 4 | sasBytes[4] >> 4,
- (sasBytes[4] & 0xf) << 2 | sasBytes[5] >> 6
- ];
-
- return emojis.map(num => emojiMapping[num]);
- }
-
- const sasGenerators = {
- decimal: generateDecimalSas,
- emoji: generateEmojiSas
- };
-
- /**
- * Generates multiple SAS for the given bytes.
- *
- * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
- * @returns {object}
- */
- export function generateSas(sasBytes) {
- const sas = {};
-
- for (const method in sasGenerators) {
- if (sasGenerators.hasOwnProperty(method)) {
- sas[method] = sasGenerators[method](sasBytes);
- }
- }
-
- return sas;
- }
|