MetaFileUtil.cc 4.4 KB

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