MetaFileUtil.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "MetaFileUtil.h"
  36. #include "Data.h"
  37. #include "Dictionary.h"
  38. #include "List.h"
  39. #include "File.h"
  40. #include "DlAbortEx.h"
  41. #include "message.h"
  42. #include <cstring>
  43. #include <cstdlib> // <-- TODO remove this if strtoul is replaced with Util::parseUInt()
  44. namespace aria2 {
  45. MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
  46. File f(file);
  47. size_t len = f.size();
  48. unsigned char* buf = new unsigned char[len];
  49. FILE* fp = fopen(file.c_str(), "r+b");
  50. try {
  51. if(!fp) {
  52. throw DlAbortEx("cannot open metainfo file");
  53. }
  54. if(fread(buf, len, 1, fp) != 1) {
  55. fclose(fp);
  56. throw DlAbortEx("cannot read metainfo");
  57. }
  58. fclose(fp);
  59. fp = 0;
  60. MetaEntry* entry = bdecoding(buf, len);
  61. delete [] buf;
  62. return entry;
  63. } catch(RecoverableException& ex) {
  64. delete [] buf;
  65. if(fp) {
  66. fclose(fp);
  67. }
  68. throw;
  69. }
  70. }
  71. MetaEntry* MetaFileUtil::bdecoding(const unsigned char* buf, size_t len)
  72. {
  73. const unsigned char* p = buf;
  74. const unsigned char* end = buf+len;
  75. return bdecodingR(&p, end);
  76. }
  77. MetaEntry*
  78. MetaFileUtil::bdecodingR(const unsigned char** pp, const unsigned char* end)
  79. {
  80. if(*pp >= end) {
  81. throw DlAbortEx("Malformed metainfo");
  82. }
  83. MetaEntry* e;
  84. switch(**pp) {
  85. case 'd':
  86. (*pp)++;
  87. e = parseDictionaryTree(pp, end);
  88. break;
  89. case 'l':
  90. (*pp)++;
  91. e = parseListTree(pp, end);
  92. break;
  93. case 'i':
  94. (*pp)++;
  95. e = decodeInt(pp, end);
  96. break;
  97. default:
  98. e = decodeWord(pp, end);
  99. }
  100. return e;
  101. }
  102. Dictionary*
  103. MetaFileUtil::parseDictionaryTree(const unsigned char** pp, const unsigned char* end)
  104. {
  105. if(*pp >= end) {
  106. throw DlAbortEx("Malformed metainfo");
  107. }
  108. Dictionary* dic = new Dictionary();
  109. try {
  110. while(1) {
  111. if(**pp == 'e') {
  112. (*pp)++;
  113. break;
  114. }
  115. std::string name = decodeWordAsString(pp, end);
  116. MetaEntry* e = bdecodingR(pp, end);
  117. dic->put(name, e);
  118. }
  119. return dic;
  120. } catch(RecoverableException& ex) {
  121. delete dic;
  122. throw;
  123. }
  124. }
  125. List*
  126. MetaFileUtil::parseListTree(const unsigned char** pp, const unsigned char* end)
  127. {
  128. if(*pp >= end) {
  129. throw DlAbortEx("Malformed metainfo");
  130. }
  131. List* lis = new List();
  132. try {
  133. while(1) {
  134. if(**pp == 'e') {
  135. (*pp)++;
  136. break;
  137. }
  138. MetaEntry* e = bdecodingR(pp, end);
  139. lis->add(e);
  140. }
  141. return lis;
  142. } catch(RecoverableException& ex) {
  143. delete lis;
  144. throw;
  145. }
  146. }
  147. Data*
  148. MetaFileUtil::decodeInt(const unsigned char** pp, const unsigned char* end)
  149. {
  150. if(*pp >= end) {
  151. throw DlAbortEx(EX_MALFORMED_META_INFO);
  152. }
  153. unsigned char* endTerm = reinterpret_cast<unsigned char*>(memchr(*pp, 'e', end-*pp));
  154. // TODO if endTerm is null
  155. if(!endTerm) {
  156. throw DlAbortEx(EX_MALFORMED_META_INFO);
  157. }
  158. size_t numSize = endTerm-*pp;
  159. Data* data = new Data(*pp, numSize, true);
  160. *pp += numSize+1;
  161. return data;
  162. }
  163. Data*
  164. MetaFileUtil::decodeWord(const unsigned char** pp, const unsigned char* end)
  165. {
  166. if(*pp >= end) {
  167. throw DlAbortEx("Malformed metainfo");
  168. }
  169. unsigned char* delim = reinterpret_cast<unsigned char*>(memchr(*pp, ':', end-*pp));
  170. // TODO if delim is null
  171. if(delim == *pp || !delim) {
  172. throw DlAbortEx(EX_MALFORMED_META_INFO);
  173. }
  174. size_t numSize = delim-*pp;
  175. unsigned char* temp = new unsigned char[numSize+1];
  176. memcpy(temp, *pp, numSize);
  177. temp[numSize] = '\0';
  178. char* endptr;
  179. unsigned long int size = strtoul(reinterpret_cast<const char*>(temp),
  180. &endptr, 10);
  181. if(*endptr != '\0') {
  182. delete [] temp;
  183. throw DlAbortEx(EX_MALFORMED_META_INFO);
  184. }
  185. delete [] temp;
  186. if(delim+1+size > end) {
  187. throw DlAbortEx(EX_MALFORMED_META_INFO);
  188. }
  189. Data* data = new Data(delim+1, size);
  190. *pp = delim+1+size;
  191. return data;
  192. }
  193. std::string
  194. MetaFileUtil::decodeWordAsString(const unsigned char** pp, const unsigned char* end)
  195. {
  196. Data* data = decodeWord(pp, end);
  197. std::string str = data->toString();
  198. delete data;
  199. return str;
  200. }
  201. } // namespace aria2