MetaFileUtil.cc 5.1 KB

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