1fdf47deb4fd5839faa99c6fba21fe8828252483
[platform/framework/web/crosswalk-tizen.git] /
1 'use strict';
2
3
4 // A trick for browserified version.
5 // Since we make browserifier to ignore `buffer` module, NodeBuffer will be undefined
6 var NodeBuffer = require('buffer').Buffer;
7 var Type       = require('../type');
8
9
10 // [ 64, 65, 66 ] -> [ padding, CR, LF ]
11 var BASE64_MAP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r';
12
13
14 function resolveYamlBinary(data) {
15   if (null === data) {
16     return false;
17   }
18
19   var code, idx, bitlen = 0, len = 0, max = data.length, map = BASE64_MAP;
20
21   // Convert one by one.
22   for (idx = 0; idx < max; idx ++) {
23     code = map.indexOf(data.charAt(idx));
24
25     // Skip CR/LF
26     if (code > 64) { continue; }
27
28     // Fail on illegal characters
29     if (code < 0) { return false; }
30
31     bitlen += 6;
32   }
33
34   // If there are any bits left, source was corrupted
35   return (bitlen % 8) === 0;
36 }
37
38 function constructYamlBinary(data) {
39   var code, idx, tailbits,
40       input = data.replace(/[\r\n=]/g, ''), // remove CR/LF & padding to simplify scan
41       max = input.length,
42       map = BASE64_MAP,
43       bits = 0,
44       result = [];
45
46   // Collect by 6*4 bits (3 bytes)
47
48   for (idx = 0; idx < max; idx++) {
49     if ((idx % 4 === 0) && idx) {
50       result.push((bits >> 16) & 0xFF);
51       result.push((bits >> 8) & 0xFF);
52       result.push(bits & 0xFF);
53     }
54
55     bits = (bits << 6) | map.indexOf(input.charAt(idx));
56   }
57
58   // Dump tail
59
60   tailbits = (max % 4)*6;
61
62   if (tailbits === 0) {
63     result.push((bits >> 16) & 0xFF);
64     result.push((bits >> 8) & 0xFF);
65     result.push(bits & 0xFF);
66   } else if (tailbits === 18) {
67     result.push((bits >> 10) & 0xFF);
68     result.push((bits >> 2) & 0xFF);
69   } else if (tailbits === 12) {
70     result.push((bits >> 4) & 0xFF);
71   }
72
73   // Wrap into Buffer for NodeJS and leave Array for browser
74   if (NodeBuffer) {
75     return new NodeBuffer(result);
76   }
77
78   return result;
79 }
80
81 function representYamlBinary(object /*, style*/) {
82   var result = '', bits = 0, idx, tail,
83       max = object.length,
84       map = BASE64_MAP;
85
86   // Convert every three bytes to 4 ASCII characters.
87
88   for (idx = 0; idx < max; idx++) {
89     if ((idx % 3 === 0) && idx) {
90       result += map[(bits >> 18) & 0x3F];
91       result += map[(bits >> 12) & 0x3F];
92       result += map[(bits >> 6) & 0x3F];
93       result += map[bits & 0x3F];
94     }
95
96     bits = (bits << 8) + object[idx];
97   }
98
99   // Dump tail
100
101   tail = max % 3;
102
103   if (tail === 0) {
104     result += map[(bits >> 18) & 0x3F];
105     result += map[(bits >> 12) & 0x3F];
106     result += map[(bits >> 6) & 0x3F];
107     result += map[bits & 0x3F];
108   } else if (tail === 2) {
109     result += map[(bits >> 10) & 0x3F];
110     result += map[(bits >> 4) & 0x3F];
111     result += map[(bits << 2) & 0x3F];
112     result += map[64];
113   } else if (tail === 1) {
114     result += map[(bits >> 2) & 0x3F];
115     result += map[(bits << 4) & 0x3F];
116     result += map[64];
117     result += map[64];
118   }
119
120   return result;
121 }
122
123 function isBinary(object) {
124   return NodeBuffer && NodeBuffer.isBuffer(object);
125 }
126
127 module.exports = new Type('tag:yaml.org,2002:binary', {
128   kind: 'scalar',
129   resolve: resolveYamlBinary,
130   construct: constructYamlBinary,
131   predicate: isBinary,
132   represent: representYamlBinary
133 });