decoder.js
function decodePrimitive( bundle, database ) {
	var op = bundle.u8[bundle.i8++];
	if ((op & 0x80) === 0x00) { // Array
		return decodeArray(bundle, database,
			(op & 0x7F) );

	} else if ((op & 0xC0) === 0x80) { // Object
		return decodeObject(bundle, database,
			(op & 0x3F) );

	} else if ((op & 0xE0) === 0xC0) { // Buffer
		return decodeBuffer(bundle,
			(op & 0x18) >> 3,
			(op & 0x07) );

	} else if ((op & 0xF0) === 0xE0) { // I-Ref
		var id = bundle.u16[bundle.i16++];
		id = ((op & 0x0F) << 16) | id;
		if (id >= bundle.iref_table.length)
			throw new Errors.IRefError('Invalid IREF #'+id+'!');
		return DEBUG
			? __debugMeta( bundle.iref_table[id], 'object.iref', { 'id': id } )
			: bundle.iref_table[id];

	} else if ((op & 0xF8) === 0xF0) { // Number
		switch (op & 0x07) {
			case 0: return bundle.u8[bundle.i8++];
			case 1: return bundle.s8[bundle.i8++];
			case 2: return bundle.u16[bundle.i16++];
			case 3: return bundle.s16[bundle.i16++];
			case 4: return bundle.u32[bundle.i32++];
			case 5: return bundle.s32[bundle.i32++];
			case 6: return bundle.f32[bundle.i32++];
			case 7: return bundle.f64[bundle.i64++];
		}

	} else if ((op & 0xFC) === 0xF8) { // Simple
		return PRIM_SIMPLE[ op & 0x03 ];

	} else if ((op & 0xFE) === 0xFC) { // Simple_EX
		return PRIM_SIMPLE_EX[ op & 0x02 ];

	} else if ((op & 0xFF) === 0xFE) { // Import
		var name = bundle.readStringLT();
		if (database[name] === undefined) 
			throw new Errors.XRefError('Cannot import undefined external reference '+name+'!');
		return DEBUG
			? __debugMeta( database[name], 'object.string', { 'key': name } )
			: database[name];

	} else if ((op & 0xFF) === 0xFF) { // Extended
		throw new Errors.AssertError('Encountered RESERVED primitive operator!');

	}
}
function decodePrimitive( bundle, database ) {
	var op = bundle.u8[bundle.i8++];
	if ((op & 0x80) === 0x00) { // Array
		return decodeArray(bundle, database,
			(op & 0x7F) );

	} else if ((op & 0xC0) === 0x80) { // Object
		return decodeObject(bundle, database,
			(op & 0x3F) );

	} else if ((op & 0xE0) === 0xC0) { // Buffer
		return decodeBuffer(bundle,
			(op & 0x18) >> 3,
			(op & 0x07) );

	} else if ((op & 0xF0) === 0xE0) { // I-Ref
		var id = bundle.u16[bundle.i16++];
		id = ((op & 0x0F) << 16) | id;
		if (id >= bundle.iref_table.length)
			throw new Errors.IRefError('Invalid IREF #'+id+'!');
		return DEBUG
			? __debugMeta( bundle.iref_table[id], 'object.iref', { 'id': id } )
			: bundle.iref_table[id];

	} else if ((op & 0xF8) === 0xF0) { // Number
		switch (op & 0x07) {
			case 0: return bundle.u8[bundle.i8++];
			case 1: return bundle.s8[bundle.i8++];
			case 2: return bundle.u16[bundle.i16++];
			case 3: return bundle.s16[bundle.i16++];
			case 4: return bundle.u32[bundle.i32++];
			case 5: return bundle.s32[bundle.i32++];
			case 6: return bundle.f32[bundle.i32++];
			case 7: return bundle.f64[bundle.i64++];
		}

	} else if ((op & 0xFC) === 0xF8) { // Simple
		return PRIM_SIMPLE[ op & 0x03 ];

	} else if ((op & 0xFE) === 0xFC) { // Simple_EX
		return PRIM_SIMPLE_EX[ op & 0x02 ];

	} else if ((op & 0xFF) === 0xFE) { // Import
		var name = bundle.readStringLT();
		if (database[name] === undefined) 
			throw new Errors.XRefError('Cannot import undefined external reference '+name+'!');
		return DEBUG
			? __debugMeta( database[name], 'object.string', { 'key': name } )
			: database[name];

	} else if ((op & 0xFF) === 0xFF) { // Extended
		throw new Errors.AssertError('Encountered RESERVED primitive operator!');

	}
}
function decodePrimitive ( bundle , database ) { var op = bundle . u8 [ bundle . i8 ++ ] ; if ( ( op & 0x80 ) === 0x00 ) { // Array return decodeArray ( bundle , database , ( op & 0x7F ) ) ; } else if ( ( op & 0xC0 ) === 0x80 ) { // Object return decodeObject ( bundle , database , ( op & 0x3F ) ) ; } else if ( ( op & 0xE0 ) === 0xC0 ) { // Buffer return decodeBuffer ( bundle , ( op & 0x18 ) >> 3 , ( op & 0x07 ) ) ; } else if ( ( op & 0xF0 ) === 0xE0 ) { // I-Ref var id = bundle . u16 [ bundle . i16 ++ ] ; id = ( ( op & 0x0F ) << 16 ) | id ; if ( id >= bundle . iref_table . length ) throw new Errors . IRefError ( 'Invalid IREF #' + id + '!' ) ; return DEBUG ? __debugMeta ( bundle . iref_table [ id ] , 'object.iref' , { 'id' : id } ) : bundle . iref_table [ id ] ; } else if ( ( op & 0xF8 ) === 0xF0 ) { // Number switch ( op & 0x07 ) { case 0 : return bundle . u8 [ bundle . i8 ++ ] ; case 1 : return bundle . s8 [ bundle . i8 ++ ] ; case 2 : return bundle . u16 [ bundle . i16 ++ ] ; case 3 : return bundle . s16 [ bundle . i16 ++ ] ; case 4 : return bundle . u32 [ bundle . i32 ++ ] ; case 5 : return bundle . s32 [ bundle . i32 ++ ] ; case 6 : return bundle . f32 [ bundle . i32 ++ ] ; case 7 : return bundle . f64 [ bundle . i64 ++ ] ; } } else if ( ( op & 0xFC ) === 0xF8 ) { // Simple return PRIM_SIMPLE [ op & 0x03 ] ; } else if ( ( op & 0xFE ) === 0xFC ) { // Simple_EX return PRIM_SIMPLE_EX [ op & 0x02 ] ; } else if ( ( op & 0xFF ) === 0xFE ) { // Import var name = bundle . readStringLT ( ) ; if ( database [ name ] === undefined ) throw new Errors . XRefError ( 'Cannot import undefined external reference ' + name + '!' ) ; return DEBUG ? __debugMeta ( database [ name ] , 'object.string' , { 'key' : name } ) : database [ name ] ; } else if ( ( op & 0xFF ) === 0xFF ) { // Extended throw new Errors . AssertError ( 'Encountered RESERVED primitive operator!' ) ; } }
Read a primitive from the bundle
Read a primitive from the bundle