PageRenderTime 132ms CodeModel.GetById 80ms app.highlight 10ms RepoModel.GetById 32ms app.codeStats 9ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/util/der/ObjectIdentifier.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 112 lines | 91 code | 10 blank | 11 comment | 17 complexity | 41271312d681fa1c7285bc8947519e44 MD5 | raw file
  1/**
  2 * ObjectIdentifier
  3 * 
  4 * An ASN1 type for an ObjectIdentifier
  5 * We store the oid in an Array.
  6 * Copyright (c) 2007 Henri Torgemane
  7 * 
  8 * See LICENSE.txt for full license information.
  9 */
 10package com.hurlant.util.der
 11{
 12	import flash.utils.ByteArray;
 13	
 14	public class ObjectIdentifier implements IAsn1Type
 15	{
 16		private var type:uint;
 17		private var len:uint;
 18		private var oid:Array;
 19		
 20		public function ObjectIdentifier(type:uint, length:uint, b:*) {
 21			this.type = type;
 22			this.len = length;
 23			if (b is ByteArray) {
 24				parse(b as ByteArray);
 25			} else if (b is String) {
 26				generate(b as String);
 27			} else {
 28				throw new Error("Invalid call to new ObjectIdentifier");
 29			}
 30		}
 31		
 32		private function generate(s:String):void {
 33			oid = s.split(".");
 34		}
 35		
 36		private function parse(b:ByteArray):void {
 37			// parse stuff
 38			// first byte = 40*value1 + value2
 39			var o:uint = b.readUnsignedByte();
 40			var a:Array = []
 41			a.push(uint(o/40));
 42			a.push(uint(o%40));
 43			var v:uint = 0;
 44			while (b.bytesAvailable>0) {
 45				o = b.readUnsignedByte();
 46				var last:Boolean = (o&0x80)==0;
 47				o &= 0x7f;
 48				v = v*128 + o;
 49				if (last) {
 50					a.push(v);
 51					v = 0;
 52				}
 53			}
 54			oid = a;
 55		}
 56		
 57		public function getLength():uint
 58		{
 59			return len;
 60		}
 61		
 62		public function getType():uint
 63		{
 64			return type;
 65		}
 66		
 67		public function toDER():ByteArray {
 68			var tmp:Array = [];
 69			tmp[0] = oid[0]*40 + oid[1];
 70			for (var i:int=2;i<oid.length;i++) {
 71				var v:int = parseInt(oid[i]);
 72				if (v<128) {
 73					tmp.push(v);
 74				} else if (v<128*128) {
 75					tmp.push( (v>>7)|0x80 );
 76					tmp.push( v&0x7f );
 77				} else if (v<128*128*128) {
 78					tmp.push( (v>>14)|0x80 );
 79					tmp.push( (v>>7)&0x7f | 0x80 );
 80					tmp.push( v&0x7f);
 81				} else if (v<128*128*128*128) {
 82					tmp.push( (v>>21)|0x80 );
 83					tmp.push( (v>>14) & 0x7f | 0x80 );
 84					tmp.push( (v>>7) & 0x7f | 0x80 );
 85					tmp.push( v & 0x7f );
 86				} else {
 87					throw new Error("OID element bigger than we thought. :(");
 88				}
 89			}
 90			len = tmp.length;
 91			if (type==0) {
 92				type = 6;
 93			}
 94			tmp.unshift(len); // assume length is small enough to fit here.
 95			tmp.unshift(type);
 96			var b:ByteArray = new ByteArray;
 97			for (i=0;i<tmp.length;i++) {
 98				b[i] = tmp[i];
 99			}
100			return b;
101		}
102
103		public function toString():String {
104			return DER.indent+oid.join(".");
105		}
106		
107		public function dump():String {
108			return "OID["+type+"]["+len+"]["+toString()+"]";
109		}
110		
111	}
112}