MetaFileUtil.cc 5.3 KB

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