@@ -32,6 +32,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3232**/
3333
3434var JPAK = {
35+ Constants : {
36+ verbosity : 3 // 0 - Error, 1 - Warning, 2 - Info, 3 - Debug
37+ } ,
3538 Generics : { } ,
3639 Loader : { } ,
3740 Classes : { } ,
@@ -62,6 +65,20 @@ var JPAK = {
6265 } ;
6366 }
6467
68+
69+ /**
70+ * Clean all deletedValue from array
71+ */
72+ Array . prototype . clean = function ( deleteValue ) {
73+ for ( var i = 0 ; i < this . length ; i ++ ) {
74+ if ( this [ i ] === deleteValue ) {
75+ this . splice ( i , 1 ) ;
76+ i -- ;
77+ }
78+ }
79+ return this ;
80+ } ;
81+
6582 /*
6683 * Extends the Uint8Array to be able to be converted to a string
6784 */
@@ -134,5 +151,117 @@ var JPAK = {
134151 this . fromObject ( JSON . parse ( json ) ) ;
135152 } ;
136153
154+ JPAK . Constants . Base64_Encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' ;
155+
156+ /**
157+ * Returns a Base64 String from an ArrayBuffer
158+ * Modified version from https://gist.github.com/jonleighton/958841
159+ */
160+ JPAK . Tools . ArrayBufferToBase64 = function ( arrayBuffer ) {
161+ var base64 = '' ;
162+
163+ var bytes = new Uint8Array ( arrayBuffer ) ;
164+ var byteLength = bytes . byteLength ;
165+ var byteRemainder = byteLength % 3 ;
166+ var mainLength = byteLength - byteRemainder ;
167+
168+ var a , b , c , d ;
169+ var chunk ;
170+
171+ // Main loop deals with bytes in chunks of 3
172+ for ( var i = 0 ; i < mainLength ; i = i + 3 ) {
173+ // Combine the three bytes into a single integer
174+ chunk = ( bytes [ i ] << 16 ) | ( bytes [ i + 1 ] << 8 ) | bytes [ i + 2 ] ;
175+
176+ // Use bitmasks to extract 6-bit segments from the triplet
177+ a = ( chunk & 16515072 ) >> 18 ; // 16515072 = (2^6 - 1) << 18
178+ b = ( chunk & 258048 ) >> 12 ; // 258048 = (2^6 - 1) << 12
179+ c = ( chunk & 4032 ) >> 6 ; // 4032 = (2^6 - 1) << 6
180+ d = chunk & 63 ; // 63 = 2^6 - 1
181+
182+ // Convert the raw binary segments to the appropriate ASCII encoding
183+ base64 += JPAK . Constants . Base64_Encoding [ a ] + JPAK . Constants . Base64_Encoding [ b ] + JPAK . Constants . Base64_Encoding [ c ] + JPAK . Constants . Base64_Encoding [ d ] ;
184+ }
185+
186+ // Deal with the remaining bytes and padding
187+ if ( byteRemainder === 1 ) {
188+ chunk = bytes [ mainLength ] ;
189+
190+ a = ( chunk & 252 ) >> 2 ; // 252 = (2^6 - 1) << 2
191+
192+ // Set the 4 least significant bits to zero
193+ b = ( chunk & 3 ) << 4 ; // 3 = 2^2 - 1
194+
195+ base64 += JPAK . Constants . Base64_Encoding [ a ] + JPAK . Constants . Base64_Encoding [ b ] + '==' ;
196+ } else if ( byteRemainder === 2 ) {
197+ chunk = ( bytes [ mainLength ] << 8 ) | bytes [ mainLength + 1 ] ;
198+
199+ a = ( chunk & 64512 ) >> 10 ; // 64512 = (2^6 - 1) << 10
200+ b = ( chunk & 1008 ) >> 4 ; // 1008 = (2^6 - 1) << 4
201+
202+ // Set the 2 least significant bits to zero
203+ c = ( chunk & 15 ) << 2 ; // 15 = 2^4 - 1
204+
205+ base64 += JPAK . Constants . Base64_Encoding [ a ] + JPAK . Constants . Base64_Encoding [ b ] + JPAK . Constants . Base64_Encoding [ c ] + '=' ;
206+ }
207+
208+ return base64 ;
209+ } ;
210+
211+ JPAK . Tools . debug = function ( ) {
212+ if ( JPAK . Constants . verbosity >= 3 ) {
213+ [ ] . splice . call ( arguments , 0 , 0 , "(JPAK Debug)" ) ;
214+ if ( console . debug )
215+ console . debug . apply ( console , arguments ) ;
216+ else
217+ console . log . apply ( console , arguments ) ;
218+ }
219+ } ;
220+
221+ JPAK . Tools . error = function ( ) {
222+ if ( JPAK . Constants . verbosity >= 0 ) {
223+ [ ] . splice . call ( arguments , 0 , 0 , "(JPAK Error)" ) ;
224+ if ( console . error )
225+ console . error . apply ( console , arguments ) ;
226+ else
227+ console . log . apply ( console , arguments ) ;
228+ }
229+ } ;
230+
231+ JPAK . Tools . warning = function ( ) {
232+ if ( JPAK . Constants . verbosity >= 1 ) {
233+ [ ] . splice . call ( arguments , 0 , 0 , "(JPAK Warning)" ) ;
234+ console . log . apply ( console , arguments ) ;
235+ }
236+ } ;
237+
238+ JPAK . Tools . info = function ( ) {
239+ if ( JPAK . Constants . verbosity >= 2 ) {
240+ [ ] . splice . call ( arguments , 0 , 0 , "(JPAK Info)" ) ;
241+ if ( console . info )
242+ console . info . apply ( console , arguments ) ;
243+ else
244+ console . log . apply ( console , arguments ) ;
245+ }
246+ } ;
247+
248+ JPAK . Tools . d = JPAK . Tools . debug ;
249+ JPAK . Tools . e = JPAK . Tools . error ;
250+ JPAK . Tools . w = JPAK . Tools . warning ;
251+ JPAK . Tools . i = JPAK . Tools . info ;
252+ JPAK . Tools . l = JPAK . Tools . info ;
253+ JPAK . Tools . log = JPAK . Tools . info ;
254+
255+ JPAK . Constants . MAGIC_TYPE = {
256+ "JPAK1" : 0 ,
257+ "JMS1" : 1 ,
258+ "JDS1" : 2
259+ } ;
260+
261+ JPAK . Constants . REVERSE_MAGIC_TYPE = {
262+ 0 : "JPAK1" ,
263+ 1 : "JMS1" ,
264+ 2 : "JDS1"
265+ } ;
137266
138267} ) ( ) ;
0 commit comments