MetaFileUtil.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. namespace aria2 {
  44. MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
  45. File f(file);
  46. size_t len = f.size();
  47. unsigned char* buf = new unsigned char[len];
  48. FILE* fp = fopen(file.c_str(), "r+b");
  49. try {
  50. if(!fp) {
  51. throw new DlAbortEx("cannot open metainfo file");
  52. }
  53. if(fread(buf, len, 1, fp) != 1) {
  54. fclose(fp);
  55. throw new DlAbortEx("cannot read metainfo");
  56. }
  57. fclose(fp);
  58. fp = 0;
  59. MetaEntry* entry = bdecoding(buf, len);
  60. delete [] buf;
  61. return entry;
  62. } catch(RecoverableException* ex) {
  63. delete [] buf;
  64. if(fp) {
  65. fclose(fp);
  66. }
  67. throw;
  68. }
  69. }
  70. MetaEntry* MetaFileUtil::bdecoding(const unsigned char* buf, size_t len)
  71. {
  72. const unsigned char* p = buf;
  73. const unsigned char* end = buf+len;
  74. return bdecodingR(&p, end);
  75. }
  76. MetaEntry*
  77. MetaFileUtil::bdecodingR(const unsigned char** pp, const unsigned char* end)
  78. {
  79. if(*pp >= end) {
  80. throw new DlAbortEx("Malformed metainfo");
  81. }
  82. MetaEntry* e;
  83. switch(**pp) {
  84. case 'd':
  85. (*pp)++;
  86. e = parseDictionaryTree(pp, end);
  87. break;
  88. case 'l':
  89. (*pp)++;
  90. e = parseListTree(pp, end);
  91. break;
  92. case 'i':
  93. (*pp)++;
  94. e = decodeInt(pp, end);
  95. break;
  96. default:
  97. e = decodeWord(pp, end);
  98. }
  99. return e;
  100. }
  101. Dictionary*
  102. MetaFileUtil::parseDictionaryTree(const unsigned char** pp, const unsigned char* end)
  103. {
  104. if(*pp >= end) {
  105. throw new DlAbortEx("Malformed metainfo");
  106. }
  107. Dictionary* dic = new Dictionary();
  108. try {
  109. while(1) {
  110. if(**pp == 'e') {
  111. (*pp)++;
  112. break;
  113. }
  114. std::string name = decodeWordAsString(pp, end);
  115. MetaEntry* e = bdecodingR(pp, end);
  116. dic->put(name, e);
  117. }
  118. return dic;
  119. } catch(RecoverableException* ex) {
  120. delete dic;
  121. throw;
  122. }
  123. }
  124. List*
  125. MetaFileUtil::parseListTree(const unsigned char** pp, const unsigned char* end)
  126. {
  127. if(*pp >= end) {
  128. throw new DlAbortEx("Malformed metainfo");
  129. }
  130. List* lis = new List();
  131. try {
  132. while(1) {
  133. if(**pp == 'e') {
  134. (*pp)++;
  135. break;
  136. }
  137. MetaEntry* e = bdecodingR(pp, end);
  138. lis->add(e);
  139. }
  140. return lis;
  141. } catch(RecoverableException* ex) {
  142. delete lis;
  143. throw;
  144. }
  145. }
  146. Data*
  147. MetaFileUtil::decodeInt(const unsigned char** pp, const unsigned char* end)
  148. {
  149. if(*pp >= end) {
  150. throw new DlAbortEx(EX_MALFORMED_META_INFO);
  151. }
  152. unsigned char* endTerm = reinterpret_cast<unsigned char*>(memchr(*pp, 'e', end-*pp));
  153. // TODO if endTerm is null
  154. if(!endTerm) {
  155. throw new DlAbortEx(EX_MALFORMED_META_INFO);
  156. }
  157. size_t numSize = endTerm-*pp;
  158. Data* data = new Data(*pp, numSize, true);
  159. *pp += numSize+1;
  160. return data;
  161. }
  162. Data*
  163. MetaFileUtil::decodeWord(const unsigned char** pp, const unsigned char* end)
  164. {
  165. if(*pp >= end) {
  166. throw new DlAbortEx("Malformed metainfo");
  167. }
  168. unsigned char* delim = reinterpret_cast<unsigned char*>(memchr(*pp, ':', end-*pp));
  169. // TODO if delim is null
  170. if(delim == *pp || !delim) {
  171. throw new DlAbortEx(EX_MALFORMED_META_INFO);
  172. }
  173. size_t numSize = delim-*pp;
  174. unsigned char* temp = new unsigned char[numSize+1];
  175. memcpy(temp, *pp, numSize);
  176. temp[numSize] = '\0';
  177. char* endptr;
  178. unsigned long int size = strtoul(reinterpret_cast<const char*>(temp),
  179. &endptr, 10);
  180. if(*endptr != '\0') {
  181. delete [] temp;
  182. throw new DlAbortEx(EX_MALFORMED_META_INFO);
  183. }
  184. delete [] temp;
  185. if(delim+1+size > end) {
  186. throw new DlAbortEx(EX_MALFORMED_META_INFO);
  187. }
  188. Data* data = new Data(delim+1, size);
  189. *pp = delim+1+size;
  190. return data;
  191. }
  192. std::string
  193. MetaFileUtil::decodeWordAsString(const unsigned char** pp, const unsigned char* end)
  194. {
  195. Data* data = decodeWord(pp, end);
  196. std::string str = data->toString();
  197. delete data;
  198. return str;
  199. }
  200. } // namespace aria2