summaryrefslogtreecommitdiff
path: root/src/MetaFileUtil.cc
blob: f64d72b30ec0601449d1ffe9f24de6214aed0680 (plain)
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
/* <!-- copyright */
/*
 * aria2 - a simple utility for downloading files faster
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/* copyright --> */
#include "MetaFileUtil.h"
#include "File.h"
#include "DlAbortEx.h"
#include "message.h"
#include <string.h>
#include <stdlib.h>

MetaEntry* MetaFileUtil::parseMetaFile(const string& file) {
  File f(file);
  int len = f.size();
  char* buf = new char[len];
  FILE* fp = fopen(file.c_str(), "r+");
  try {
    if(fp == NULL) {
      throw new DlAbortEx("cannot open metainfo file");
    }
    if(fread(buf, len, 1, fp) != 1) {
      fclose(fp);
      throw new DlAbortEx("cannot read metainfo");
    }
    fclose(fp);
    fp = NULL;
    MetaEntry* entry = bdecoding(buf, len);
    delete [] buf;
    return entry;
  } catch(Exception* ex) {
    delete [] buf;
    if(fp != NULL) {
      fclose(fp);
    }
    throw;
  }
}

MetaEntry* MetaFileUtil::bdecoding(const char* buf, int len) {
  MetaEntry* entry = NULL;
  try{
    const char* p = buf;
    const char* end = buf+len;
    entry = bdecodingR(&p, end);
    return entry;
  } catch(DlAbortEx* ex) {
    if(entry != NULL) {
      delete entry;
    }
    throw;
  }
}

MetaEntry* MetaFileUtil::bdecodingR(const char** pp, const char* end) {
  if(*pp >= end) {
    throw new DlAbortEx("mulformed metainfo");
  }
  MetaEntry* e;
  switch(**pp) {
  case 'd':
    (*pp)++;
    e = parseDictionaryTree(pp, end);
    break;
  case 'l':
    (*pp)++;
    e = parseListTree(pp, end);
    break;
  case 'i':
    (*pp)++;
    e = decodeInt(pp, end);
    break;
  default:
    e = decodeWord(pp, end);
  }
  return e;
}

Dictionary* MetaFileUtil::parseDictionaryTree(const char** pp, const char* end) {
  if(*pp >= end) {
    throw new DlAbortEx("mulformed metainfo");
  }
  Dictionary* dic = new Dictionary();
  try {
    while(1) {
      if(**pp == 'e') {
	(*pp)++;
	break;
      }
      string name = decodeWordAsString(pp, end);
      MetaEntry* e = bdecodingR(pp, end);
      dic->put(name, e);
    }
    return dic;
  } catch(Exception* ex) {
    delete dic;
    throw;
  }
}

List* MetaFileUtil::parseListTree(const char** pp, const char* end) {
  if(*pp >= end) {
    throw new DlAbortEx("mulformed metainfo");
  }
  List* lis = new List();
  try {
    while(1) {
      if(**pp == 'e') {
	(*pp)++;
	break;
      }
      MetaEntry* e = bdecodingR(pp, end);
      lis->add(e);
    }
    return lis;
  } catch(Exception* ex) {
    delete lis;
    throw;
  }
}

Data* MetaFileUtil::decodeInt(const char** pp, const char* end) {
  if(*pp >= end) {
    throw new DlAbortEx(EX_MULFORMED_META_INFO);
  }
  char* endTerm = (char*)memchr(*pp, 'e', end-*pp);
  // TODO if endTerm is null
  if(endTerm == NULL) {
    throw new DlAbortEx(EX_MULFORMED_META_INFO);
  }
  int numSize = endTerm-*pp;

  Data* data = new Data(*pp, numSize, true);
  *pp += numSize+1;
  return data;
}

Data* MetaFileUtil::decodeWord(const char** pp, const char* end) {
  if(*pp >= end) {
    throw new DlAbortEx("mulformed metainfo");
  }
  char* delim = (char*)memchr(*pp, ':', end-*pp);
  // TODO if delim is null
  if(delim == *pp || delim == NULL) {
    throw new DlAbortEx(EX_MULFORMED_META_INFO);
  }
  int numSize = delim-*pp;
  char* temp = new char[numSize+1];
  memcpy(temp, *pp, numSize);
  temp[numSize] = '\0';
  char* endptr;
  int size = strtol(temp, &endptr, 10);
  if(*endptr != '\0') {
    delete [] temp;
    throw new DlAbortEx(EX_MULFORMED_META_INFO);
  }    
  delete [] temp;

  if(delim+1+size > end) {
    throw new DlAbortEx(EX_MULFORMED_META_INFO);
  }

  Data* data = new Data(delim+1, size);
  *pp = delim+1+size;
  return data;
}

string MetaFileUtil::decodeWordAsString(const char** pp, const char* end) {
  Data* data = decodeWord(pp, end);
  string str = data->toString();
  delete data;
  return str;
}