summaryrefslogtreecommitdiff
path: root/contrib/marc/marc.c
blob: 4c736ec83378303e7f1e13036a32c5de8ac8d65a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//run this on the file from a wget http://marc.ucis.ano/?get=0&version=3

#define M_TYPE_NULL 0
#define M_TYPE_STRING 1
#define M_TYPE_LIST 2
#define M_TYPE_DICT 3

char *type[]={"NULL","string","list","dict"};

int indent;

void pi() {
 int i;
 for(i=0;i<indent;i++) {
  printf("%s","  ");
 }
}

int marc_decode(unsigned char *data,int from_index,int length) {
 char *s;
 unsigned int cur_len=0;
 unsigned char m_type=data[from_index];
 from_index++;
 char *key;
 if(!length) return printf("dafuq? no length???"),-1;
 pi();
 printf("length: %d\n",length);
 pi();
 switch(m_type) {
  case M_TYPE_NULL:
   printf("[null]\n");
   break;
  case M_TYPE_STRING:
   s=malloc(length);
   memcpy(s,data+from_index,length-1);
   s[length-1]=0;
   printf("string: %s\n",s);
   break;
  case M_TYPE_LIST:
   printf("list:\n");
   indent++;
   while(from_index < length) {
    cur_len=data[from_index+3]+(data[from_index+2]<<1)+(data[from_index+1]<<2)+(data[from_index+0]<<3);
    from_index+=4;
    marc_decode(data,from_index,cur_len);
    from_index+=cur_len;
   }
   indent--;
   break;
  case M_TYPE_DICT:
   printf("dict:\n");
   indent++;
   while(from_index < length) {
    cur_len=data[from_index];
    pi();
    printf("len: %d\n",cur_len);
    from_index++;
    if(cur_len == 0) break;
    key=malloc(cur_len+1);
    memcpy(key,data+from_index,cur_len);
    key[cur_len]=0;
    from_index+=cur_len;
    pi();
    printf("key: %s\n",key,cur_len);
    cur_len=data[from_index+3]+(data[from_index+2]<<1)+(data[from_index+1]<<2)+(data[from_index+0]<<3);
    from_index+=4;
    indent++;
    marc_decode(data,from_index,cur_len);
    indent--;
    from_index+=cur_len;
   }
   indent--;
   break;
  default:
   printf("oh fuck. dahell is this!?!? %d\n",m_type);
   break;
 }
 return 0;
}

void printhex(unsigned char *data,int length) {
 for(;length;data++,length--) {
  printf("%02x",*data);
 }
}

void update_message_decode(unsigned char *data,int from_index,int length) {
 printf("update_message version: %d len: %d\n",data[from_index],length);
 if(data[from_index] != 2) return printf("this program only handles version 2 update messages.\n");
 from_index++;
 char pkey[32];
 int i;
 char *label;
 unsigned int thedate=0;
 unsigned char label_len=0,ext_type,num_extensions;
 short ext_data_len;
 memcpy(pkey,data+from_index,32);
 from_index+=32;
 char sig[64];
 memcpy(sig,data+from_index,64);
 from_index+=64;
 printf("pkey: 0x");
 printhex(pkey,32);
 printf("\n");
 printf("sig: 0x");
 printhex(sig,64);
 printf("\n");
 //for(;from_index < length;) {
  //timestamp, 4 bytes
  thedate=(data[from_index]<<24)+(data[from_index+1]<<16)+(data[from_index+2]<<8)+(data[from_index+3]);
  printf("thedate: %d\n",thedate);
  from_index+=4;
  //label length, 1 byte
  label_len=data[from_index];
  from_index++;
  printf("label len: %d\n",label_len);
  label=malloc(label_len);
  memcpy(label,data+from_index,label_len);
  label[label_len]=0;
  printf("label: ");
  printhex(label,label_len);
  printf("\n");
  from_index+=label_len;
  num_extensions=data[from_index];
  printf("num of extensions: %d\n",num_extensions);
  from_index++;
  for(i=0;i<num_extensions;i++) {
   ext_type=data[from_index];
   from_index++;
   ext_data_len=(data[from_index]<<8) + (data[from_index+1]);
   from_index+=2;
   from_index+=ext_data_len;//skip this for now... fuck it.
   printf("  ext %d type: %d len: %d\n",i,ext_type,ext_data_len);
   if(ext_type != 1 && ext_type != 4) return printf("fuckfuckfuckfuck...\n"),0;
  }
  marc_decode(data+from_index,0,length);
 //}
 return;
}

void interactive_http_sync(unsigned char *data,int from_index,int length) {
 int i;
 unsigned int update_length;
 unsigned char ext_type;
 unsigned short ext_data_len;
 unsigned char version=data[from_index];
 from_index++;
 unsigned char num_extensions=data[from_index];
 from_index++;
 printf("interactive_http_sync version: %d\n",version);
 printf("num_extensions: %d\n",num_extensions);
 for(i=0;i<num_extensions;i++) {
  ext_type=data[from_index];
  from_index++;
  ext_data_len=(data[from_index]<<8)+data[from_index+1];
  from_index+=2;
  printf("extension: type:%d data_len:%d\n",ext_type,ext_data_len);
  from_index+=ext_data_len;
 }
 for(;from_index < length;) {
  printf("from_index: %d\n",from_index);
  update_length=(data[from_index]<<24)+(data[from_index+1]<<16)+(data[from_index+2]<<8)+(data[from_index+3]);
  from_index+=4;
  if(update_length == 0) return printf("ohfuck.\n");
  update_message_decode(data,from_index,update_length);
  from_index += update_length;
 }
}

int main(int argc,char *argv[]) {
 indent=0;
 if(argc < 2) return printf("usage: %s filename\n",argv[0]),0;
 FILE *fp=fopen(argv[1],"r");
 fseek(fp,0L,SEEK_END);
 long len=ftell(fp);
 fseek(fp,0L,SEEK_SET);
 unsigned char *db;
 db=malloc(len+1);
 if((int)db == -1) return printf("failed to malloc(%d)",len),0;
 fread(db,1,len,fp);
 interactive_http_sync(db,0,len);
/*
 marc_decode("\x01\x41\x41\x41",0,4);
 printf("\n");
 marc_decode("\x02\x00\x00\x00\x04\x01\x41\x41\x41\x00\x00\x00\x04\x01\x42\x41\x42",0,17);
 printf("\n");
//                 \x04BLAH == KLAH dafuq?
 marc_decode("\x03\x04\x42LAH\x00\x00\x00\x05\x01""foOo\x04NoPe\x00\x00\x00\x07\x01wtfftw",0,31);
*/
 return 0;
}