llnotecard.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @file llnotecard.cpp
  3. * @brief LLNotecard class definition
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llnotecard.h"
  34. #include "llstreamtools.h"
  35. LLNotecard::LLNotecard(S32 max_text)
  36. : mMaxText(max_text),
  37. mVersion(0),
  38. mEmbeddedVersion(0)
  39. {
  40. }
  41. bool LLNotecard::importEmbeddedItemsStream(std::istream& str)
  42. {
  43. // Version 1 format:
  44. // LLEmbeddedItems version 1
  45. // {
  46. // count <number of entries being used and not deleted>
  47. // {
  48. // ext char index <index>
  49. // <InventoryItem chunk>
  50. // }
  51. // }
  52. S32 i;
  53. S32 count = 0;
  54. str >> std::ws >> "LLEmbeddedItems version" >> mEmbeddedVersion >> "\n";
  55. if (str.fail())
  56. {
  57. llwarns << "Invalid Linden text file header" << llendl;
  58. goto import_file_failed;
  59. }
  60. if (1 != mEmbeddedVersion)
  61. {
  62. llwarns << "Invalid LLEmbeddedItems version: " << mEmbeddedVersion
  63. << llendl;
  64. goto import_file_failed;
  65. }
  66. str >> std::ws >> "{\n";
  67. if (str.fail())
  68. {
  69. llwarns << "Invalid Linden text file format: missing {" << llendl;
  70. goto import_file_failed;
  71. }
  72. str >> std::ws >> "count " >> count >> "\n";
  73. if (str.fail())
  74. {
  75. llwarns << "Invalid LLEmbeddedItems count" << llendl;
  76. goto import_file_failed;
  77. }
  78. if ((count < 0))
  79. {
  80. llwarns << "Invalid LLEmbeddedItems count value: " << count << llendl;
  81. goto import_file_failed;
  82. }
  83. for(i = 0; i < count; i++)
  84. {
  85. str >> std::ws >> "{\n";
  86. if (str.fail())
  87. {
  88. llwarns << "Invalid LLEmbeddedItems file format: missing {"
  89. << llendl;
  90. goto import_file_failed;
  91. }
  92. U32 index = 0;
  93. str >> std::ws >> "ext char index " >> index >> "\n";
  94. if (str.fail())
  95. {
  96. llwarns << "Invalid LLEmbeddedItems file format: missing ext char index"
  97. << llendl;
  98. goto import_file_failed;
  99. }
  100. str >> std::ws >> "inv_item\t0\n";
  101. if (str.fail())
  102. {
  103. llwarns << "Invalid LLEmbeddedItems file format: missing inv_item"
  104. << llendl;
  105. goto import_file_failed;
  106. }
  107. LLPointer<LLInventoryItem> item = new LLInventoryItem;
  108. if (!item->importLegacyStream(str))
  109. {
  110. llinfos << "notecard import failed" << llendl;
  111. goto import_file_failed;
  112. }
  113. mItems.push_back(item);
  114. str >> std::ws >> "}\n";
  115. if (str.fail())
  116. {
  117. llwarns << "Invalid LLEmbeddedItems file format: missing }"
  118. << llendl;
  119. goto import_file_failed;
  120. }
  121. }
  122. str >> std::ws >> "}\n";
  123. if (str.fail())
  124. {
  125. llwarns << "Invalid LLEmbeddedItems file format: missing }" << llendl;
  126. goto import_file_failed;
  127. }
  128. return true;
  129. import_file_failed:
  130. return false;
  131. }
  132. bool LLNotecard::importStream(std::istream& str)
  133. {
  134. // Version 1 format:
  135. // Linden text version 1
  136. // {
  137. // <EmbeddedItemList chunk>
  138. // Text length
  139. // <ASCII text; 0x80 | index = embedded item>
  140. // }
  141. // Version 2 format: (NOTE: Imports identically to version 1)
  142. // Linden text version 2
  143. // {
  144. // <EmbeddedItemList chunk>
  145. // Text length
  146. // <UTF8 text; FIRST_EMBEDDED_CHAR + index = embedded item>
  147. // }
  148. str >> std::ws >> "Linden text version " >> mVersion >> "\n";
  149. if (str.fail())
  150. {
  151. llwarns << "Invalid Linden text file header " << llendl;
  152. return false;
  153. }
  154. if (1 != mVersion && 2 != mVersion)
  155. {
  156. llwarns << "Invalid Linden text file version: " << mVersion << llendl;
  157. return false;
  158. }
  159. str >> std::ws >> "{\n";
  160. if (str.fail())
  161. {
  162. llwarns << "Invalid Linden text file format" << llendl;
  163. return false;
  164. }
  165. if (!importEmbeddedItemsStream(str))
  166. {
  167. return false;
  168. }
  169. char line_buf[STD_STRING_BUF_SIZE];
  170. str.getline(line_buf, STD_STRING_BUF_SIZE);
  171. if (str.fail())
  172. {
  173. llwarns << "Invalid Linden text length field" << llendl;
  174. return false;
  175. }
  176. line_buf[STD_STRING_STR_LEN] = '\0';
  177. S32 text_len = 0;
  178. if (1 != sscanf(line_buf, "Text length %d", &text_len))
  179. {
  180. llwarns << "Invalid Linden text length field" << llendl;
  181. return false;
  182. }
  183. if (text_len > mMaxText || text_len < 0)
  184. {
  185. llwarns << "Invalid Linden text length: " << text_len << llendl;
  186. return false;
  187. }
  188. bool success = true;
  189. char* text = new char[text_len + 1];
  190. fullread(str, text, text_len);
  191. if (str.fail())
  192. {
  193. llwarns << "Invalid Linden text: text shorter than text length: "
  194. << text_len << llendl;
  195. success = false;
  196. }
  197. text[text_len] = '\0';
  198. if (success)
  199. {
  200. // Actually set the text
  201. mText = std::string(text);
  202. }
  203. delete[] text;
  204. return success;
  205. }
  206. ////////////////////////////////////////////////////////////////////////////
  207. bool LLNotecard::exportEmbeddedItemsStream(std::ostream& out_stream)
  208. {
  209. out_stream << "LLEmbeddedItems version 1\n";
  210. out_stream << "{\n";
  211. S32 count = mItems.size();
  212. out_stream << llformat("count %d\n", count);
  213. for (S32 i = 0; i < count; ++i)
  214. {
  215. LLInventoryItem* item = mItems[i];
  216. if (item)
  217. {
  218. out_stream << "{\n";
  219. out_stream << llformat("ext char index %d\n", i);
  220. if (!item->exportLegacyStream(out_stream))
  221. {
  222. return false;
  223. }
  224. out_stream << "}\n";
  225. }
  226. }
  227. out_stream << "}\n";
  228. return true;
  229. }
  230. bool LLNotecard::exportStream(std::ostream& out_stream)
  231. {
  232. out_stream << "Linden text version 2\n";
  233. out_stream << "{\n";
  234. if (!exportEmbeddedItemsStream(out_stream))
  235. {
  236. return false;
  237. }
  238. out_stream << llformat("Text length %d\n", mText.length());
  239. out_stream << mText;
  240. out_stream << "}\n";
  241. return true;
  242. }