File: bencode.cpp

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (261 lines) | stat: -rw-r--r-- 5,792 bytes parent folder | download | duplicates (2)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "./def.h"

#include "bencode.h"

#ifndef WINDOWS
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#ifndef HAVE_SNPRINTF
#include "compat.h"
#endif

static const char* next_key(const char *keylist)
{
  for(;*keylist && *keylist != KEY_SP; keylist++);
  if(*keylist) keylist++;
  return keylist;
}

static size_t compare_key(const char *key,size_t keylen,const char *keylist)
{
  for(;keylen && *keylist && *key==*keylist;keylen--,key++,keylist++) ;
  if(!keylen) if(*keylist && *keylist!=KEY_SP) return 1;
  return keylen;
}

size_t buf_long(const char *b,size_t len,char beginchar,char endchar,int64_t *pi)
{
  const char *p = b;
  const char *psave;

  if(2 > len) return 0;	/* buffer too small */

  if( beginchar ){ 
    if(*p != beginchar) return 0; 
    p++; len--;
  }

  for(psave = p; len && isdigit(*p); p++,len--) ;

  if(!len || MAX_INT_SIZ < (p - psave) || *p != endchar) return 0;

  if( pi ){
    if( beginchar ) *pi = strtoll(b + 1,(char**) 0,10);
    else  *pi=strtoll(b,(char**) 0,10);
  }
  return (size_t)( p - b + 1 );
}

size_t buf_int(const char *b,size_t len,char beginchar,char endchar,size_t *pi)
{
  size_t r;

  if( pi ){
    int64_t pl;
    r = buf_long(b,len,beginchar,endchar,&pl);
    *pi = (size_t) pl;
  }else{
    r = buf_long(b,len,beginchar,endchar,(int64_t*) 0);
  }
  return r;
}

size_t buf_str(const char *b,size_t len,const char **pstr,size_t* slen)
{
  size_t rl,sl;

  rl = buf_int(b,len,0,':',&sl);

  if( !rl ) return 0;

  if(len < rl + sl) return 0;
  if(pstr) *pstr = b + rl;
  if(slen) *slen = sl;

  return( rl + sl );
}

size_t decode_int(const char *b,size_t len)
{
  return(buf_long(b,len,'i','e',(int64_t*) 0));
}

size_t decode_str(const char *b,size_t len)
{
  return (buf_str(b,len,(const char**) 0,(size_t*) 0));
}

size_t decode_dict(const char *b,size_t len,const char *keylist)
{
  size_t rl,dl,nl;
  const char *pkey;
  dl = 0;
  if(2 > len || *b != 'd') return 0;

  dl++; len--;
  for(;len && *(b + dl) != 'e';){
    rl = buf_str(b + dl,len,&pkey,&nl);

    if( !rl || KEYNAME_SIZ < nl) return 0;
    dl += rl;
    len -= rl;

    if(keylist && compare_key(pkey,nl,keylist) == 0){
      pkey = next_key(keylist);
      if(! *pkey ) return dl;
      rl = decode_dict(b + dl,len, pkey);
      if( !rl ) return 0;
      return dl + rl;
    }

    rl = decode_rev(b + dl,len,(const char*) 0);
    if( !rl ) return 0;

    dl += rl;len -= rl;
  }
  if( !len || keylist) return 0;
  return dl + 1;	/* add the last char 'e' */
}

size_t decode_list(const char *b,size_t len,const char *keylist)
{
  size_t ll,rl;
  ll = 0;
  if(2 > len || *b != 'l') return 0;
  len--; ll++;
  for(;len && *(b + ll) != 'e';){
    rl = decode_rev(b + ll,len,keylist);
    if( !rl ) return 0;

    ll += rl; len -= rl;
  }
  if( !len ) return 0;
  return ll + 1;  /* add last char 'e' */
}

size_t decode_rev(const char *b,size_t len,const char *keylist)
{
  if( !b ) return 0;
  switch( *b ){
  case 'i': return decode_int(b,len);
  case 'd': return decode_dict(b,len,keylist);
  case 'l': return decode_list(b,len,keylist);
  default: return decode_str(b,len);
  }
}

size_t decode_query(const char *b,size_t len,const char *keylist,const char **ps,size_t *pi,int64_t *pl,int method)
{
  size_t pos;
  char kl[KEYNAME_LISTSIZ];
  strcpy(kl,keylist);
  pos = decode_rev(b, len, kl);
  if( !pos ) return 0;
  switch(method){
  case QUERY_STR: return(buf_str(b + pos,len - pos, ps, pi));
  case QUERY_INT: return(buf_int(b + pos,len - pos, 'i', 'e', pi));
  case QUERY_POS:
          if(pi) *pi = decode_rev(b + pos, len - pos, (const char*) 0);
          return pos;
  case QUERY_LONG: return(buf_long(b + pos,len - pos, 'i', 'e', pl));
  default: return 0;
  }
}

size_t bencode_buf(const char *buf,size_t len,FILE *fp)
{
  char slen[MAX_INT_SIZ];

  if( MAX_INT_SIZ <= snprintf(slen, MAX_INT_SIZ, "%d:", (int)len) ) return 0;
  if( fwrite( slen, strlen(slen), 1, fp) != 1) return 0;
  if( fwrite(buf, len, 1, fp) != 1 ) return 0;
  return 1;
}

size_t bencode_str(const char *str, FILE *fp)
{
  return bencode_buf(str, strlen(str), fp);
}

size_t bencode_int(const uint64_t integer, FILE *fp)
{
  char buf[MAX_INT_SIZ];
  if( EOF == fputc('i', fp)) return 0;
  if( MAX_INT_SIZ <=
      snprintf(buf, MAX_INT_SIZ, "%llu", (unsigned long long)integer) )
    return 0;
  if( fwrite(buf, strlen(buf), 1, fp) != 1 ) return 0;
  return (EOF == fputc('e', fp)) ? 0: 1;
}

size_t bencode_begin_dict(FILE *fp)
{
  return (EOF == fputc('d',fp)) ? 0 : 1;
}

size_t bencode_begin_list(FILE *fp)
{
  return (EOF == fputc('l',fp)) ? 0 : 1;
}

size_t bencode_end_dict_list(FILE *fp)
{
  return (EOF == fputc('e',fp)) ? 0 : 1;
}

size_t bencode_path2list(const char *pathname, FILE *fp)
{
  const char *pn;
  const char *p = pathname;
  
  if( bencode_begin_list(fp) != 1 ) return 0;
  
  for(; *p;){
    pn = strchr(p, PATH_SP);
    if( pn ){
      if( bencode_buf(p, pn - p, fp) != 1) return 0;
      p = pn + 1;
    }else{
      if( bencode_str(p, fp) != 1) return 0;
      break;
    }
  }
  
  return bencode_end_dict_list(fp);
}

size_t decode_list2path(const char *b, size_t n, char *pathname, size_t maxlen)
{
  const char *pb = b;
  const char *s = (char *)0;
  const char *endmax = pathname + maxlen - 1;
  size_t r, q;

  if( 'l' != *pb ) return 0;
  pb++;
  n--;
  if( !n ) return 0;
  while( n && pathname < endmax ){
   if( !(r = buf_str(pb, n, &s, &q)) ) return 0;
   if( q >= maxlen ) return 0;
    memcpy(pathname, s, q);
    pathname += q;
    maxlen -= q;
    pb += r;
    n -= r;
    if( 'e' == *pb ) break;
    if( pathname >= endmax ) return 0;
    *pathname++ = PATH_SP;
  }
  *pathname = '\0';
  return (pb - b + 1);
}