blob: 565c1a9b9de4e51c8eda82cad33cd2d7df6b3564 [file] [log] [blame]
Meki Cherkaouib0365a72012-02-18 00:59:57 -08001// JavaScript Document
2/*
3 @author: Meki Cherkaoui
4*/
5var DTAGS = require('./TAG').d;
6var TT = require('./TAG').tt;
7
8
9var Encoder = function Encoder(){
10
11
12};
13/*
14 TT bits specify the type of nodes
15
16*/
17var CCN_TT_BITS=3;
18var CCN_TT_MASK=((1 << CCN_TT_BITS) - 1);
19var CCN_MAX_TINY=((1 << (7-CCN_TT_BITS)) - 1);
20var CCN_TT_HBIT=((1 << 7));
21
22Encoder.prototype.encodeContentObject = function(_ContentObject){
23
24 //var slit = function splitNumber(number){
25 //};
26 var i = 0;
27
28 var addToBuffer = function addToBuffer(buffer,number,tt){
29
30 var sizeOfBuffer = 1+8*((sizeof(number)+6)/7);
31
32 splitNumbers = new Buffer(sizeOfBuffer);
33
34 number >>= 7-CCN_TT_BITS;
35
36 splitNumbers.write((CCN_TT_HBIT & ~require('./TAG').d.CCN_CLOSE) |
37 ((number & CCN_MAX_TINY) << CCN_TT_BITS) |
38 (CCN_TT_MASK & tt));
39
40 var index = 1;
41 while(number>0){
42 splitNumbers.write( number & ~CCN_TT_HBIT |require('./TAG').d.CCN_CLOSE);
43 index++;
44 number>>=7;
45 }
46
47 for( i=0, j=sizeofBuffer-1;i <sizeOfBuffer;i++,j--) {
48
49 buffer[index+i] = splitNumbers[j];
50 }
51
52 };
53 //NOTE NUMBETR= SIZE
54 var addToBufferData = function addToBuffer(buffer,data,tt){
55
56 var number = sizeof(data);
57 var sizeOfBuffer = 1+8*((sizeof(number)+6)/7);
58
59 splitNumbers = new Buffer(sizeOfBuffer);
60
61 number >>= 7-CCN_TT_BITS;
62
63 splitNumbers.write((CCN_TT_HBIT & ~require('./TAG').d.CCN_CLOSE) |
64 ((number & CCN_MAX_TINY) << CCN_TT_BITS) |
65 (CCN_TT_MASK & tt));
66
67
68 while(number>0){
69 splitNumbers.write( number & ~CCN_TT_HBIT |require('./TAG').d.CCN_CLOSE);
70 index++;
71 number>>=7;
72 }
73
74 for( i=0, j=sizeofBuffer-1;i <sizeOfBuffer;i++,j--) {
75
76 buffer[index+i] = splitNumbers[j];
77 }
78
79 for(i=0;i<10;i++){
80 buffer[index+i] = data[i];
81 }
82
83 };
84
85 //NOTE NUMBETR= SIZE
86 var addName = function addToBuffer(buffer,name){
87
88 for(i=0;i<name.size;i++){
89 tt = require('./TAG').tt.CCN_BLOB ;
90 addToBufferData(buffer,_ContentObject.moSignature.SignatureBits,tt);
91 addCloseTag(buffer);
92 }
93
94 };
95
96
97 var addCloseTag = function addCloseTag(buffer){
98 var tag = DTAGS.CCN_CLOSE;
99 buffer.write(tag);
100 };
101
102 var buffer = new Buffer(1000);
103
104 var tag = DTAGS.CCN_DTAG_ContentObject;
105 var tt = TT.CCN_DTAG ;
106 addToBuffer(buffer,tag,tt);
107
108
109 tag = DTAGS.CCN_Signature;
110 tt = TT.CCN_DTAG ;
111 addToBuffer(buffer,tag,tt);
112
113
114 tag = DTAGS.CCN_SignatureBits;
115 tt = TT.CCN_DTAG ;
116 addToBuffer(buffer,tag,tt);
117
118 tt = TT.CCN_BLOB ;
119 addToBufferData(buffer,_ContentObject.moSignature.SignatureBits,tt);
120
121 addCloseTag(buffer);
122
123 addCloseTag(buffer);
124
125 tag = DTAGS.CCN_Name;
126 tt = TT.CCN_DTAG ;
127 addToBuffer(buffer,tag,tt);
128
129
130 addName(buffer,_ContentObject.moName,tt);
131
132 addCloseTag(buffer);
133
134 tag = DTAGS.CCN_DTAG_SignedInfo;
135 tt = TT.CCN_DTAG ;
136 addToBuffer(buffer,tag,tt);
137
138 tag = DTAGS.CCN_DTAG_PublisherPublicKeyDigest;
139 tt = TT.CCN_DTAG ;
140 addToBuffer(buffer,tag,tt);
141
142 addToBufferData(buffer,_ContentObject.moSignedInfo.msPublisherPublicKeyDigest ,tt);
143
144 addCloseTag(buffer);
145
146 tag = DTAGS.CCN_DTAG_Timestamp;
147 tt = TT.CCN_DTAG ;
148 addToBuffer(buffer,tag,tt);
149
150 addToBufferData(buffer,_ContentObject.moSignedInfo.msTimestamp ,tt);
151
152 addCloseTag(buffer);
153
154 tag = DTAGS.CCN_DTAG_KeyLocator;
155 tt = TT.CCN_DTAG ;
156 addToBuffer(buffer,tag,tt);
157
158 tag = DTAGS.CCN_DTAG_key;
159 tt = TT.CCN_DTAG ;
160 addToBuffer(buffer,tag,tt);
161
162 addToBufferData(buffer,_ContentObject.moSignedInfo.msKeyLocator ,tt);
163
164 addCloseTag(buffer);
165
166 addCloseTag(buffer);
167
168 tag = DTAGS.CCN_DTAG_Content;
169 tt = TT.CCN_DTAG ;
170 addToBuffer(buffer,tag,tt);
171
172 addToBufferData(buffer,_ContentObject.msContent,tt);
173
174 addCloseTag(buffer);
175
176 addCloseTag(buffer);
177
178}
179
180
181Encoder.prototype.encodeContentObjectToFile = function(_filename){
182
183
184 var content = "hello world";
185
186 var name = new require('./Name').Name(['hello']);
187
188 var sigInfo = new require('./SignatureInfo').SignatureInfo(new Buffer('h7exThsbBBViA/knAnWd2lMdaAgW/lcI6EIAK6ln4Ut8/owZ0tobVQ4YJ3lHkl3qwzldAqLT8RayMU8cxpqrT6sa/nHFatbiObj2GrAAKbsJRSiV/ESzeVXPUHdbNJ75iDc5wjqvC4aJL8tGJt8vAOjJ7fy4MWD+mgFBYqn+1bw='));
189
190 var sig = new require('./Signature').Signature(new Buffer("IEh0BFqfrJjrt9+txLvRqxWm5AZTScbIOoRghE6rMYU=",'base64'), new Buffer('BPMrnKOi','base64'), new Buffer('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9lWVVnBeVwEml703J0byLmBtmCX6iU/XQ9Q8aHfkIrE5pp5fa3SwFkJRKboSeH7tzccU9/dvngCZW4dX0SbhTDZx9qNar69kOBhJNkn8vbFch8dDknn9qEWfK+WDZb/Wvqi0lZzN3cqlEyObOuFmyWiMdnsqHNXT/mCJchT847wIDAQAB','base64'));
191
192 sig.generateSignature(name,sigInfo,content);
193
194 var co = new require('./ContentObject').ContentObject(sig,name,sigInfo,content);
195
196 var buffer = this.encodeContentObject(co);
197
198 fs.writeFile(_filename);
199
200}
201
202
203var Encoder = new Encoder();
204
205var file = Encoder.decodeFile('/Users/eastman/Desktop/CCN/ccnx-0.5.0rc1/hello');
206
207