1173
|
1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 #include <string.h> |
|
4 |
|
5 //run this on the file from a wget http://marc.ucis.ano/?get=0&version=3 |
|
6 |
|
7 #define M_TYPE_NULL 0 |
|
8 #define M_TYPE_STRING 1 |
|
9 #define M_TYPE_LIST 2 |
|
10 #define M_TYPE_DICT 3 |
|
11 |
|
12 char *type[]={"NULL","string","list","dict"}; |
|
13 |
|
14 int indent; |
|
15 |
|
16 void pi() { |
|
17 int i; |
|
18 for(i=0;i<indent;i++) { |
|
19 printf("%s"," "); |
|
20 } |
|
21 } |
|
22 |
|
23 int marc_decode(unsigned char *data,int from_index,int length) { |
|
24 char *s; |
|
25 unsigned int cur_len=0; |
|
26 unsigned char m_type=data[from_index]; |
|
27 from_index++; |
|
28 char *key; |
|
29 if(!length) return printf("dafuq? no length???"),-1; |
|
30 pi(); |
|
31 printf("length: %d\n",length); |
|
32 pi(); |
|
33 switch(m_type) { |
|
34 case M_TYPE_NULL: |
|
35 printf("[null]\n"); |
|
36 break; |
|
37 case M_TYPE_STRING: |
|
38 s=malloc(length); |
|
39 memcpy(s,data+from_index,length-1); |
|
40 s[length-1]=0; |
|
41 printf("string: %s\n",s); |
|
42 break; |
|
43 case M_TYPE_LIST: |
|
44 printf("list:\n"); |
|
45 indent++; |
|
46 while(from_index < length) { |
|
47 cur_len=data[from_index+3]+(data[from_index+2]<<1)+(data[from_index+1]<<2)+(data[from_index+0]<<3); |
|
48 from_index+=4; |
|
49 marc_decode(data,from_index,cur_len); |
|
50 from_index+=cur_len; |
|
51 } |
|
52 indent--; |
|
53 break; |
|
54 case M_TYPE_DICT: |
|
55 printf("dict:\n"); |
|
56 indent++; |
|
57 while(from_index < length) { |
|
58 cur_len=data[from_index]; |
|
59 pi(); |
|
60 printf("len: %d\n",cur_len); |
|
61 from_index++; |
|
62 if(cur_len == 0) break; |
|
63 key=malloc(cur_len+1); |
|
64 memcpy(key,data+from_index,cur_len); |
|
65 key[cur_len]=0; |
|
66 from_index+=cur_len; |
|
67 pi(); |
|
68 printf("key: %s\n",key,cur_len); |
|
69 cur_len=data[from_index+3]+(data[from_index+2]<<1)+(data[from_index+1]<<2)+(data[from_index+0]<<3); |
|
70 from_index+=4; |
|
71 indent++; |
|
72 marc_decode(data,from_index,cur_len); |
|
73 indent--; |
|
74 from_index+=cur_len; |
|
75 } |
|
76 indent--; |
|
77 break; |
|
78 default: |
|
79 printf("oh fuck. dahell is this!?!? %d\n",m_type); |
|
80 break; |
|
81 } |
|
82 return 0; |
|
83 } |
|
84 |
|
85 void printhex(unsigned char *data,int length) { |
|
86 for(;length;data++,length--) { |
|
87 printf("%02x",*data); |
|
88 } |
|
89 } |
|
90 |
|
91 void update_message_decode(unsigned char *data,int from_index,int length) { |
|
92 printf("update_message version: %d len: %d\n",data[from_index],length); |
|
93 if(data[from_index] != 2) return printf("this program only handles version 2 update messages.\n"); |
|
94 from_index++; |
|
95 char pkey[32]; |
|
96 int i; |
|
97 char *label; |
|
98 unsigned int thedate=0; |
|
99 unsigned char label_len=0,ext_type,num_extensions; |
|
100 short ext_data_len; |
|
101 memcpy(pkey,data+from_index,32); |
|
102 from_index+=32; |
|
103 char sig[64]; |
|
104 memcpy(sig,data+from_index,64); |
|
105 from_index+=64; |
|
106 printf("pkey: 0x"); |
|
107 printhex(pkey,32); |
|
108 printf("\n"); |
|
109 printf("sig: 0x"); |
|
110 printhex(sig,64); |
|
111 printf("\n"); |
|
112 //for(;from_index < length;) { |
|
113 //timestamp, 4 bytes |
|
114 thedate=(data[from_index]<<24)+(data[from_index+1]<<16)+(data[from_index+2]<<8)+(data[from_index+3]); |
|
115 printf("thedate: %d\n",thedate); |
|
116 from_index+=4; |
|
117 //label length, 1 byte |
|
118 label_len=data[from_index]; |
|
119 from_index++; |
|
120 printf("label len: %d\n",label_len); |
|
121 label=malloc(label_len); |
|
122 memcpy(label,data+from_index,label_len); |
|
123 label[label_len]=0; |
|
124 printf("label: "); |
|
125 printhex(label,label_len); |
|
126 printf("\n"); |
|
127 from_index+=label_len; |
|
128 num_extensions=data[from_index]; |
|
129 printf("num of extensions: %d\n",num_extensions); |
|
130 from_index++; |
|
131 for(i=0;i<num_extensions;i++) { |
|
132 ext_type=data[from_index]; |
|
133 from_index++; |
|
134 ext_data_len=(data[from_index]<<8) + (data[from_index+1]); |
|
135 from_index+=2; |
|
136 from_index+=ext_data_len;//skip this for now... fuck it. |
|
137 printf(" ext %d type: %d len: %d\n",i,ext_type,ext_data_len); |
|
138 if(ext_type != 1 && ext_type != 4) return printf("fuckfuckfuckfuck...\n"),0; |
|
139 } |
|
140 marc_decode(data+from_index,0,length); |
|
141 //} |
|
142 return; |
|
143 } |
|
144 |
|
145 void interactive_http_sync(unsigned char *data,int from_index,int length) { |
|
146 int i; |
|
147 unsigned int update_length; |
|
148 unsigned char ext_type; |
|
149 unsigned short ext_data_len; |
|
150 unsigned char version=data[from_index]; |
|
151 from_index++; |
|
152 unsigned char num_extensions=data[from_index]; |
|
153 from_index++; |
|
154 printf("interactive_http_sync version: %d\n",version); |
|
155 printf("num_extensions: %d\n",num_extensions); |
|
156 for(i=0;i<num_extensions;i++) { |
|
157 ext_type=data[from_index]; |
|
158 from_index++; |
|
159 ext_data_len=(data[from_index]<<8)+data[from_index+1]; |
|
160 from_index+=2; |
|
161 printf("extension: type:%d data_len:%d\n",ext_type,ext_data_len); |
|
162 from_index+=ext_data_len; |
|
163 } |
|
164 for(;from_index < length;) { |
|
165 printf("from_index: %d\n",from_index); |
|
166 update_length=(data[from_index]<<24)+(data[from_index+1]<<16)+(data[from_index+2]<<8)+(data[from_index+3]); |
|
167 from_index+=4; |
|
168 if(update_length == 0) return printf("ohfuck.\n"); |
|
169 update_message_decode(data,from_index,update_length); |
|
170 from_index += update_length; |
|
171 } |
|
172 } |
|
173 |
|
174 int main(int argc,char *argv[]) { |
|
175 indent=0; |
|
176 if(argc < 2) return printf("usage: %s filename\n",argv[0]),0; |
|
177 FILE *fp=fopen(argv[1],"r"); |
|
178 fseek(fp,0L,SEEK_END); |
|
179 long len=ftell(fp); |
|
180 fseek(fp,0L,SEEK_SET); |
|
181 unsigned char *db; |
|
182 db=malloc(len+1); |
|
183 if((int)db == -1) return printf("failed to malloc(%d)",len),0; |
|
184 fread(db,1,len,fp); |
|
185 interactive_http_sync(db,0,len); |
|
186 /* |
|
187 marc_decode("\x01\x41\x41\x41",0,4); |
|
188 printf("\n"); |
|
189 marc_decode("\x02\x00\x00\x00\x04\x01\x41\x41\x41\x00\x00\x00\x04\x01\x42\x41\x42",0,17); |
|
190 printf("\n"); |
|
191 // \x04BLAH == KLAH dafuq? |
|
192 marc_decode("\x03\x04\x42LAH\x00\x00\x00\x05\x01""foOo\x04NoPe\x00\x00\x00\x07\x01wtfftw",0,31); |
|
193 */ |
|
194 return 0; |
|
195 } |