container.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. /*
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <opc/opc.h>
  30. #include "internal.h"
  31. static void* ensureItem(void **array_, puint32_t items, puint32_t item_size) {
  32. *array_=xmlRealloc(*array_, (items+1)*item_size);
  33. return *array_;
  34. }
  35. static opcContainerPart* ensurePart(opcContainer *container) {
  36. return ((opcContainerPart*)ensureItem((void**)&container->part_array, container->part_items, sizeof(opcContainerPart)))+container->part_items;
  37. }
  38. static opcContainerRelPrefix* ensureRelPrefix(opcContainer *container) {
  39. return ((opcContainerRelPrefix*)ensureItem((void**)&container->relprefix_array, container->relprefix_items, sizeof(opcContainerRelPrefix)))+container->relprefix_items;
  40. }
  41. static opcContainerType* ensureType(opcContainer *container) {
  42. return ((opcContainerType*)ensureItem((void**)&container->type_array, container->type_items, sizeof(opcContainerType)))+container->type_items;
  43. }
  44. static opcContainerExtension* ensureExtension(opcContainer *container) {
  45. return ((opcContainerExtension*)ensureItem((void**)&container->extension_array, container->extension_items, sizeof(opcContainerExtension)))+container->extension_items;
  46. }
  47. static opcContainerRelationType* ensureRelationType(opcContainer *container) {
  48. return ((opcContainerRelationType*)ensureItem((void**)&container->relationtype_array, container->relationtype_items, sizeof(opcContainerRelationType)))+container->relationtype_items;
  49. }
  50. static opcContainerExternalRelation* ensureExternalRelation(opcContainer *container) {
  51. return ((opcContainerExternalRelation*)ensureItem((void**)&container->externalrelation_array, container->externalrelation_items, sizeof(opcContainerRelationType)))+container->externalrelation_items;
  52. }
  53. static opc_bool_t findItem(void *array_, opc_uint32_t items, const void *key1, opc_uint32_t key2, int (*cmp_fct)(const void *key1, opc_uint32_t key2, const void *array_, opc_uint32_t item), opc_uint32_t *pos) {
  54. opc_uint32_t i=0;
  55. opc_uint32_t j=items;
  56. while(i<j) {
  57. opc_uint32_t m=i+(j-i)/2;
  58. OPC_ASSERT(i<=m && m<j);
  59. int cmp=cmp_fct(key1, key2, array_, m);
  60. if (cmp<0) {
  61. j=m;
  62. } else if (cmp>0) {
  63. i=m+1;
  64. } else {
  65. *pos=m;
  66. return OPC_TRUE;
  67. }
  68. }
  69. OPC_ASSERT(i==j);
  70. *pos=i;
  71. return OPC_FALSE;
  72. }
  73. #define ensureGap(array_, items_, i) \
  74. {\
  75. for (opc_uint32_t k=items_;k>i;k--) { \
  76. (array_)[k]=(array_)[k-1];\
  77. }\
  78. (items_)++;\
  79. OPC_ASSERT(i>=0 && i<(items_));\
  80. opc_bzero_mem(&(array_)[i], sizeof((array_)[i]));\
  81. }
  82. static inline int part_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  83. return xmlStrcmp((xmlChar*)key, ((opcContainerPart*)array_)[item].name);
  84. }
  85. opcContainerPart *opcContainerInsertPart(opcContainer *container, const xmlChar *name, opc_bool_t insert) {
  86. opc_uint32_t i=0;
  87. if (findItem(container->part_array, container->part_items, name, 0, part_cmp_fct, &i)) {
  88. return &container->part_array[i];
  89. } else if (insert && NULL!=ensurePart(container)) {
  90. ensureGap(container->part_array, container->part_items, i);
  91. container->part_array[i].first_segment_id=-1;
  92. container->part_array[i].last_segment_id=-1;
  93. container->part_array[i].name=xmlStrdup(name);
  94. container->part_array[i].rel_segment_id=-1;
  95. return &container->part_array[i];
  96. } else {
  97. return NULL;
  98. }
  99. }
  100. #define deleteItem(array_, items_, i) \
  101. {\
  102. for(opc_uint32_t k=i+1;k<items_;k++) {\
  103. array_[k-1]=array_[k];\
  104. }\
  105. items_--;\
  106. }
  107. static opc_error_t opcContainerDeleteAllRelationsToPart(opcContainer *container, opcPart part, opcContainerRelation **relation_array, opc_uint32_t *relation_items) {
  108. for(opc_uint32_t i=0;i<*relation_items;) {
  109. if (0==(*relation_array)[i].target_mode && part==(*relation_array)[i].target_ptr) {
  110. deleteItem((*relation_array), (*relation_items), i);
  111. } else {
  112. i++;
  113. }
  114. }
  115. return OPC_ERROR_NONE;
  116. }
  117. opc_error_t opcContainerDeletePart(opcContainer *container, const xmlChar *name) {
  118. opc_error_t ret=OPC_ERROR_NONE;
  119. opc_uint32_t i=0;
  120. if (findItem(container->part_array, container->part_items, name, 0, part_cmp_fct, &i)) {
  121. if (-1!=container->part_array[i].first_segment_id) {
  122. opcContainerDeletePartEx(container, name, OPC_FALSE);
  123. }
  124. if (-1!=container->part_array[i].rel_segment_id) {
  125. opcContainerDeletePartEx(container, name, OPC_TRUE);
  126. }
  127. OPC_ENSURE(OPC_ERROR_NONE==opcContainerDeleteAllRelationsToPart(container, container->part_array[i].name, &container->relation_array, &container->relation_items));
  128. for(opc_uint32_t j=0;j<container->part_items;j++) {
  129. OPC_ENSURE(OPC_ERROR_NONE==opcContainerDeleteAllRelationsToPart(container, container->part_array[i].name, &container->part_array[j].relation_array, &container->part_array[j].relation_items));
  130. }
  131. if (NULL!=container->part_array[i].relation_array){
  132. xmlFree(container->part_array[i].relation_array);
  133. }
  134. if (NULL!=container->part_array[i].name){
  135. xmlFree(container->part_array[i].name);
  136. }
  137. deleteItem(container->part_array, container->part_items, i);
  138. }
  139. return ret;
  140. }
  141. #define OPC_MAX_UINT16 65535
  142. static opc_uint32_t insertRelPrefix(opcContainer *container, const xmlChar *relPrefix) {
  143. opc_uint32_t i=container->relprefix_items;
  144. for(;i>0 && 0!=xmlStrcmp(container->relprefix_array[i-1].prefix, relPrefix);) {
  145. i--;
  146. };
  147. if (i>0) {
  148. OPC_ASSERT(0==xmlStrcmp(container->relprefix_array[i-1].prefix, relPrefix));
  149. return i-1;
  150. } else {
  151. if (container->relprefix_items<OPC_MAX_UINT16 && NULL!=ensureRelPrefix(container)) {
  152. i=container->relprefix_items++;
  153. container->relprefix_array[i].prefix=xmlStrdup(relPrefix);
  154. return i;
  155. } else {
  156. return -1; // error
  157. }
  158. }
  159. }
  160. static opc_uint32_t findRelPrefix(opcContainer *container, const xmlChar *relPrefix, opc_uint32_t relPrefixLen) {
  161. for(opc_uint32_t i=0;i<=container->relprefix_items;i++) {
  162. if (0==xmlStrncmp(container->relprefix_array[i].prefix, relPrefix, relPrefixLen) && 0==container->relprefix_array[i].prefix[relPrefixLen]) {
  163. return i;
  164. }
  165. }
  166. return -1; // not found
  167. }
  168. static opc_uint32_t splitRelPrefix(opcContainer *container, const xmlChar *rel, opc_uint32_t *counter) {
  169. opc_uint32_t len=xmlStrlen(rel);
  170. while(len>0 && rel[len-1]>='0' && rel[len-1]<='9') len--;
  171. if (NULL!=counter) {
  172. if (rel[len]!=0) {
  173. *counter=atoi((char*)(rel+len));
  174. } else {
  175. *counter=-1; // no counter
  176. }
  177. }
  178. return len;
  179. }
  180. static opc_uint32_t assembleRelId(opc_uint32_t prefix, opc_uint16_t relCounter) {
  181. opc_uint32_t ret=relCounter;
  182. if (-1!=prefix) {
  183. ret|=prefix<<16;
  184. }
  185. return ret;
  186. }
  187. static opc_uint32_t createRelId(opcContainer *container, const xmlChar *relPrefix, opc_uint16_t relCounter) {
  188. opc_uint32_t prefix=insertRelPrefix(container, relPrefix);
  189. opc_uint32_t ret=assembleRelId(prefix, relCounter);
  190. return ret;
  191. }
  192. static inline int type_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  193. return xmlStrcmp((xmlChar*)key, ((opcContainerType*)array_)[item].type);
  194. }
  195. opcContainerType *insertType(opcContainer *container, const xmlChar *type, opc_bool_t insert) {
  196. opc_uint32_t i=0;
  197. if (findItem(container->type_array, container->type_items, type, 0, type_cmp_fct, &i)) {
  198. return &container->type_array[i];
  199. } else if (insert && NULL!=ensureType(container)) {
  200. ensureGap(container->type_array, container->type_items, i);
  201. container->type_array[i].type=xmlStrdup(type);
  202. return &container->type_array[i];
  203. } else {
  204. return NULL;
  205. }
  206. }
  207. static inline int extension_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  208. return xmlStrcmp((xmlChar*)key, ((opcContainerExtension*)array_)[item].extension);
  209. }
  210. opcContainerExtension *opcContainerInsertExtension(opcContainer *container, const xmlChar *extension, opc_bool_t insert) {
  211. opc_uint32_t i=0;
  212. if (findItem(container->extension_array, container->extension_items, extension, 0, extension_cmp_fct, &i)) {
  213. return &container->extension_array[i];
  214. } else if (insert && NULL!=ensureExtension(container)) {
  215. ensureGap(container->extension_array, container->extension_items, i);
  216. container->extension_array[i].extension=xmlStrdup(extension);
  217. return &container->extension_array[i];
  218. } else {
  219. return NULL;
  220. }
  221. }
  222. static inline int relationtype_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  223. return xmlStrcmp((xmlChar*)key, ((opcContainerRelationType*)array_)[item].type);
  224. }
  225. opcContainerRelationType *opcContainerInsertRelationType(opcContainer *container, const xmlChar *type, opc_bool_t insert) {
  226. opc_uint32_t i=0;
  227. if (findItem(container->relationtype_array, container->relationtype_items, type, 0, relationtype_cmp_fct, &i)) {
  228. return &container->relationtype_array[i];
  229. } else if (insert && NULL!=ensureRelationType(container)) {
  230. ensureGap(container->relationtype_array, container->relationtype_items, i);
  231. container->relationtype_array[i].type=xmlStrdup(type);
  232. return &container->relationtype_array[i];
  233. } else {
  234. return NULL;
  235. }
  236. }
  237. static inline int externalrelation_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  238. return xmlStrcmp((xmlChar*)key, ((opcContainerExternalRelation*)array_)[item].target);
  239. }
  240. opcContainerExternalRelation*insertExternalRelation(opcContainer *container, const xmlChar *target, opc_bool_t insert) {
  241. opc_uint32_t i=0;
  242. if (findItem(container->externalrelation_array, container->externalrelation_items, target, 0, externalrelation_cmp_fct, &i)) {
  243. return &container->externalrelation_array[i];
  244. } else if (insert && NULL!=ensureExternalRelation(container)) {
  245. ensureGap(container->externalrelation_array, container->externalrelation_items, i);
  246. container->externalrelation_array[i].target=xmlStrdup(target);
  247. return &container->externalrelation_array[i];
  248. } else {
  249. return NULL;
  250. }
  251. }
  252. static inline int relation_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  253. opcRelation r1=v;
  254. opcRelation r2=((opcContainerRelation*)array_)[item].relation_id;
  255. if (OPC_CONTAINER_RELID_PREFIX(r1)==OPC_CONTAINER_RELID_PREFIX(r2)) {
  256. if (OPC_CONTAINER_RELID_COUNTER_NONE==OPC_CONTAINER_RELID_COUNTER(r1)) {
  257. return (OPC_CONTAINER_RELID_COUNTER_NONE==OPC_CONTAINER_RELID_COUNTER(r2)?0:-1);
  258. } else if (OPC_CONTAINER_RELID_COUNTER_NONE==OPC_CONTAINER_RELID_COUNTER(r2)) {
  259. OPC_ASSERT(OPC_CONTAINER_RELID_COUNTER_NONE!=OPC_CONTAINER_RELID_COUNTER(r1));
  260. return 1;
  261. } else {
  262. OPC_ASSERT(OPC_CONTAINER_RELID_COUNTER_NONE!=OPC_CONTAINER_RELID_COUNTER(r1) && OPC_CONTAINER_RELID_COUNTER_NONE!=OPC_CONTAINER_RELID_COUNTER(r2));
  263. return OPC_CONTAINER_RELID_COUNTER(r1)-OPC_CONTAINER_RELID_COUNTER(r2);
  264. }
  265. } else {
  266. return OPC_CONTAINER_RELID_PREFIX(r1)-OPC_CONTAINER_RELID_PREFIX(r2);
  267. }
  268. }
  269. opcContainerRelation *opcContainerInsertRelation(opcContainerRelation **relation_array, opc_uint32_t *relation_items,
  270. opc_uint32_t relation_id,
  271. xmlChar *relation_type,
  272. opc_uint32_t target_mode, xmlChar *target_ptr) {
  273. OPC_ASSERT(NULL!=relation_items);
  274. opc_uint32_t i=0;
  275. if (*relation_items>0) {
  276. opc_bool_t ret=findItem(*relation_array, *relation_items, NULL, relation_id, relation_cmp_fct, &i);
  277. if (ret) { // error, relation already exists!
  278. return NULL;
  279. }
  280. }
  281. if (NULL!=ensureItem((void**)relation_array, *relation_items, sizeof(opcContainerRelation))) {
  282. for (opc_uint32_t k=(*relation_items);k>i;k--) {
  283. (*relation_array)[k]=(*relation_array)[k-1];
  284. }
  285. (*relation_items)++;
  286. OPC_ASSERT(i>=0 && i<(*relation_items));\
  287. opc_bzero_mem(&(*relation_array)[i], sizeof((*relation_array)[i]));\
  288. (*relation_array)[i].relation_id=relation_id;
  289. (*relation_array)[i].relation_type=relation_type;
  290. (*relation_array)[i].target_mode=target_mode;
  291. (*relation_array)[i].target_ptr=target_ptr;
  292. return &(*relation_array)[i];
  293. } else {
  294. return NULL; // memory error!
  295. }
  296. }
  297. opcContainerRelation *opcContainerFindRelation(opcContainer *container, opcContainerRelation *relation_array, opc_uint32_t relation_items, opcRelation relation) {
  298. opc_uint32_t i=0;
  299. opc_bool_t ret=findItem(relation_array, relation_items, NULL, relation, relation_cmp_fct, &i);
  300. return (ret?&relation_array[i]:NULL);
  301. }
  302. opc_error_t opcContainerDeleteRelation(opcContainer *container, opcContainerRelation **relation_array, opc_uint32_t *relation_items, opcRelation relation) {
  303. opc_error_t err=OPC_ERROR_NONE;
  304. opc_uint32_t i=0;
  305. opc_bool_t ret=findItem(*relation_array, *relation_items, NULL, relation, relation_cmp_fct, &i);
  306. if (ret) {
  307. deleteItem((*relation_array), (*relation_items), i);
  308. }
  309. return err;
  310. }
  311. opcContainerRelation *opcContainerFindRelationById(opcContainer *container, opcContainerRelation *relation_array, opc_uint32_t relation_items, const xmlChar *relation_id) {
  312. opc_uint32_t counter=-1;
  313. opc_uint32_t id_len=splitRelPrefix(container, relation_id, &counter);
  314. opc_uint32_t rel=-1;
  315. if (id_len>0) {
  316. opc_uint32_t prefix=findRelPrefix(container, relation_id, id_len);
  317. if (-1!=prefix) {
  318. rel=assembleRelId(prefix, counter);
  319. }
  320. } else {
  321. rel=assembleRelId(-1, counter);
  322. }
  323. opcContainerRelation *ret=(-1!=rel?opcContainerFindRelation(container, relation_array, relation_items, rel):NULL);
  324. return ret;
  325. }
  326. static void opc_container_normalize_part_to_helper_buffer(xmlChar *buf, int buf_len,
  327. const xmlChar *base,
  328. const xmlChar *name) {
  329. int j=xmlStrlen(base);
  330. int i=0;
  331. OPC_ASSERT(j<=buf_len);
  332. if (j>0) {
  333. memcpy(buf, base, j*sizeof(xmlChar));
  334. }
  335. while(j>0 && buf[j-1]!='/') j--; // so make sure base has a trailing "/"
  336. while(name[i]!=0) {
  337. if (name[i]=='/') {
  338. j=0; /* absolute path */
  339. while (name[i]=='/') i++;
  340. } else if (name[i]=='.' && name[i+1]=='/') {
  341. /* skip */
  342. i+=1;
  343. while (name[i]=='/') i++;
  344. } else if (name[i]=='.' && name[i+1]=='.' && name[i+2]=='/') {
  345. while(j>0 && buf[j-1]=='/') j--; /* skip base '/' */
  346. while(j>0 && buf[j-1]!='/') j--; /* navigate one dir up */
  347. i+=2;
  348. while (name[i]=='/') i++;
  349. } else {
  350. /* copy step */
  351. OPC_ASSERT(j+1<=buf_len);
  352. while(j+1<buf_len && name[i]!=0 && name[i]!='/') {
  353. buf[j++]=name[i++];
  354. }
  355. if (name[i]=='/' && j+1<buf_len) {
  356. buf[j++]='/';
  357. }
  358. while (name[i]=='/') i++;
  359. }
  360. }
  361. OPC_ASSERT(j+1<buf_len);
  362. buf[j]=0;
  363. }
  364. static void opcConstainerParseRels(opcContainer *c, const xmlChar *partName, opcContainerRelation **relation_array, opc_uint32_t *relation_items) {
  365. mceTextReader_t reader;
  366. if (OPC_ERROR_NONE==opcXmlReaderOpenEx(c, &reader, partName, OPC_TRUE, NULL, NULL, 0)) {
  367. static const char ns[]="http://schemas.openxmlformats.org/package/2006/relationships";
  368. mce_start_document(&reader) {
  369. mce_start_element(&reader, _X(ns), _X("Relationships")) {
  370. mce_skip_attributes(&reader);
  371. mce_start_children(&reader) {
  372. mce_start_element(&reader, NULL, _X("Relationship")) {
  373. const xmlChar *id=NULL;
  374. const xmlChar *type=NULL;
  375. const xmlChar *target=NULL;
  376. const xmlChar *mode=NULL;
  377. mce_start_attributes(&reader) {
  378. mce_start_attribute(&reader, NULL, _X("Id")) {
  379. id=xmlTextReaderConstValue(reader.reader);
  380. } mce_end_attribute(&reader);
  381. mce_start_attribute(&reader, NULL, _X("Type")) {
  382. type=xmlTextReaderConstValue(reader.reader);
  383. } mce_end_attribute(&reader);
  384. mce_start_attribute(&reader, NULL, _X("Target")) {
  385. target=xmlTextReaderConstValue(reader.reader);
  386. } mce_end_attribute(&reader);
  387. mce_start_attribute(&reader, NULL, _X("TargetMode")) {
  388. mode=xmlTextReaderConstValue(reader.reader);
  389. }
  390. } mce_end_attributes(&reader);
  391. mce_error_guard_start(&reader) {
  392. mce_error(&reader, NULL==id || id[0]==0, MCE_ERROR_VALIDATION, "Missing @Id attribute!");
  393. mce_error(&reader, NULL==type || type[0]==0, MCE_ERROR_VALIDATION, "Missing @Type attribute!");
  394. mce_error(&reader, NULL==target || target[0]==0, MCE_ERROR_VALIDATION, "Missing @Id attribute!");
  395. opcContainerRelationType *rel_type=opcContainerInsertRelationType(c, type, OPC_TRUE);
  396. mce_error(&reader, NULL==rel_type, MCE_ERROR_MEMORY, NULL);
  397. opc_uint32_t counter=-1;
  398. opc_uint32_t id_len=splitRelPrefix(c, id, &counter);
  399. ((xmlChar *)id)[id_len]=0;
  400. opc_uint32_t rel_id=createRelId(c, id, counter);
  401. if (NULL==mode || 0==xmlStrcasecmp(mode, _X("Internal"))) {
  402. xmlChar target_part_name[OPC_MAX_PATH];
  403. opc_container_normalize_part_to_helper_buffer(target_part_name, sizeof(target_part_name), partName, target);
  404. // printf("%s (%s;%s)\n", target_part_name, base, target);
  405. opcContainerPart *target_part=opcContainerInsertPart(c, target_part_name, OPC_FALSE);
  406. mce_errorf(&reader, NULL==target_part, MCE_ERROR_VALIDATION, "Referenced part %s (%s;%s) does not exists!", target_part_name, partName, target);
  407. // printf("%s %i %s %s\n", id, counter, rel_type->type, target_part->name);
  408. opcContainerRelation *rel=opcContainerInsertRelation(relation_array, relation_items, rel_id, rel_type->type, 0, target_part->name);
  409. OPC_ASSERT(NULL!=rel);
  410. } else if (0==xmlStrcasecmp(mode, _X("External"))) {
  411. opcContainerExternalRelation *ext_rel=insertExternalRelation(c, target, OPC_TRUE);
  412. mce_error(&reader, NULL==ext_rel, MCE_ERROR_MEMORY, NULL);
  413. opcContainerRelation *rel=opcContainerInsertRelation(relation_array, relation_items, rel_id, rel_type->type, 1, ext_rel->target);
  414. OPC_ASSERT(NULL!=rel);
  415. } else {
  416. mce_errorf(&reader, OPC_TRUE, MCE_ERROR_VALIDATION, "TargetMode %s unknown!\n", mode);
  417. }
  418. } mce_error_guard_end(reader);
  419. mce_skip_children(&reader);
  420. } mce_end_element(&reader);
  421. } mce_end_children(&reader);
  422. } mce_end_element(&reader);
  423. } mce_end_document(reader);
  424. OPC_ENSURE(0==mceTextReaderCleanup(&reader));
  425. }
  426. }
  427. static const xmlChar OPC_SEGMENT_CONTENTTYPES[]={'[', 'C', 'o', 'n', 't', 'e', 'n', 't', '_', 'T', 'y', 'p', 'e', 's', ']', '.', 'x', 'm', 'l', 0};
  428. static const xmlChar OPC_SEGMENT_ROOTRELS[]={0};
  429. static opc_error_t opcContainerFree(opcContainer *c) {
  430. if (NULL!=c) {
  431. for(opc_uint32_t i=0;i<c->extension_items;i++) {
  432. xmlFree(c->extension_array[i].extension);
  433. }
  434. for(opc_uint32_t i=0;i<c->type_items;i++) {
  435. xmlFree(c->type_array[i].type);
  436. }
  437. for(opc_uint32_t i=0;i<c->relationtype_items;i++) {
  438. xmlFree(c->relationtype_array[i].type);
  439. }
  440. for(opc_uint32_t i=0;i<c->externalrelation_items;i++) {
  441. xmlFree(c->externalrelation_array[i].target);
  442. }
  443. for(opc_uint32_t i=0;i<c->part_items;i++) {
  444. xmlFree(c->part_array[i].relation_array);
  445. xmlFree(c->part_array[i].name);
  446. }
  447. for(opc_uint32_t i=0;i<c->relprefix_items;i++) {
  448. xmlFree(c->relprefix_array[i].prefix);
  449. }
  450. if (NULL!=c->part_array) xmlFree(c->part_array);
  451. if (NULL!=c->relprefix_array) xmlFree(c->relprefix_array);
  452. if (NULL!=c->type_array) xmlFree(c->type_array);
  453. if (NULL!=c->extension_array) xmlFree(c->extension_array);
  454. if (NULL!=c->relationtype_array) xmlFree(c->relationtype_array);
  455. if (NULL!=c->externalrelation_array) xmlFree(c->externalrelation_array);
  456. if (NULL!=c->relation_array) xmlFree(c->relation_array);
  457. opcZipClose(c->storage, NULL);
  458. xmlFree(c);
  459. }
  460. return OPC_ERROR_NONE;
  461. }
  462. static void opcContainerDumpString(FILE *out, const xmlChar *str, opc_uint32_t max_len, opc_bool_t new_line) {
  463. opc_uint32_t len=(NULL!=str?xmlStrlen(str):0);
  464. if (len<=max_len) {
  465. if (NULL!=str) fputs((const char *)str, out);
  466. for(opc_uint32_t i=len;i<max_len;i++) fputc(' ', out);
  467. } else {
  468. static const char prefix[]="...";
  469. static opc_uint32_t prefix_len=sizeof(prefix)-1;
  470. opc_uint32_t ofs=len-max_len;
  471. if (ofs+prefix_len<len) ofs+=prefix_len; else ofs=len;
  472. fputs(prefix, out);
  473. fputs((const char *)(str+ofs), out);
  474. }
  475. if (new_line) {
  476. fputc('\n', out);
  477. }
  478. }
  479. static void opcContainerDumpLine(FILE *out, const xmlChar line_char, opc_uint32_t max_len, opc_bool_t new_line) {
  480. for(opc_uint32_t i=0;i<max_len;i++) {
  481. fputc(line_char, out);
  482. }
  483. if (new_line) {
  484. fputc('\n', out);
  485. }
  486. }
  487. static void opcContainerRelCalcMax(opcContainer *c,
  488. const xmlChar *part_name,
  489. opcContainerRelation *relation_array, opc_uint32_t relation_items,
  490. opc_uint32_t *max_rel_src,
  491. opc_uint32_t *max_rel_id,
  492. opc_uint32_t *max_rel_dest,
  493. opc_uint32_t *max_rel_type) {
  494. if (relation_items>0) {
  495. opc_uint32_t const src_len=xmlStrlen(part_name);
  496. if (src_len>*max_rel_src) *max_rel_src=src_len;
  497. for(opc_uint32_t j=0;j<relation_items;j++) {
  498. const xmlChar *prefix=NULL;
  499. opc_uint32_t counter=-1;
  500. const xmlChar *type=NULL;
  501. char buf[20]="";
  502. opcRelationGetInformation(c, (opcPart)part_name, relation_array[j].relation_id, &prefix, &counter, &type);
  503. if (-1!=counter) {
  504. sprintf(buf, "%i", counter);
  505. }
  506. opc_uint32_t const type_len=xmlStrlen(type);
  507. if (type_len>*max_rel_type) *max_rel_type=type_len;
  508. opc_uint32_t const id_len=xmlStrlen(prefix)+xmlStrlen(_X(buf));
  509. if (id_len>*max_rel_id) *max_rel_id=id_len;
  510. opc_uint32_t const dest_len=xmlStrlen(relation_array[j].target_ptr);
  511. if (dest_len>*max_rel_dest) *max_rel_dest=dest_len;
  512. }
  513. }
  514. }
  515. static void opcContainerRelDump(opcContainer *c,
  516. FILE *out,
  517. const xmlChar *part_name,
  518. opcContainerRelation *relation_array, opc_uint32_t relation_items,
  519. opc_uint32_t max_rel_src,
  520. opc_uint32_t max_rel_id,
  521. opc_uint32_t max_rel_dest,
  522. opc_uint32_t max_rel_type) {
  523. for(opc_uint32_t j=0;j<relation_items;j++) {
  524. opcContainerDumpString(out, (part_name==NULL?_X("[root]"):part_name), max_rel_src, OPC_FALSE); fputc('|', out);
  525. const xmlChar *prefix=NULL;
  526. opc_uint32_t counter=-1;
  527. const xmlChar *type=NULL;
  528. char buf[20]="";
  529. opcRelationGetInformation(c, (opcPart)part_name, relation_array[j].relation_id, &prefix, &counter, &type);
  530. if (-1!=counter) {
  531. sprintf(buf, "%i", counter);
  532. }
  533. opc_uint32_t prefix_len=xmlStrlen(prefix);
  534. opcContainerDumpString(out, prefix, prefix_len, OPC_FALSE);
  535. OPC_ASSERT(xmlStrlen(_X(buf))+prefix_len<=max_rel_id);
  536. opcContainerDumpString(out, _X(buf), max_rel_id-prefix_len, OPC_FALSE); fputc('|', out);
  537. opcContainerDumpString(out, relation_array[j].target_ptr, max_rel_dest, OPC_FALSE); fputc('|', out);
  538. opcContainerDumpString(out, type, max_rel_type, OPC_TRUE);
  539. }
  540. }
  541. opc_error_t opcContainerDump(opcContainer *c, FILE *out) {
  542. opc_uint32_t max_content_type_len=xmlStrlen(_X("Content Types"));
  543. for(opc_uint32_t i=0;i<c->type_items;i++) {
  544. opc_uint32_t const len=xmlStrlen(c->type_array[i].type);
  545. if (len>max_content_type_len) max_content_type_len=len;
  546. }
  547. opc_uint32_t max_extension_len=xmlStrlen(_X("Extension"));
  548. opc_uint32_t max_extension_type_len=xmlStrlen(_X("Type"));
  549. for(opc_uint32_t i=0;i<c->extension_items;i++) {
  550. opc_uint32_t const len=xmlStrlen(c->extension_array[i].extension);
  551. if (len>max_extension_len) max_extension_len=len;
  552. opc_uint32_t const type_len=xmlStrlen(c->extension_array[i].type);
  553. if (type_len>max_extension_type_len) max_extension_type_len=type_len;
  554. }
  555. opc_uint32_t max_rel_type_len=xmlStrlen(_X("Relation Types"));
  556. for(opc_uint32_t i=0;i<c->relationtype_items;i++) {
  557. opc_uint32_t const len=xmlStrlen(c->relationtype_array[i].type);
  558. if (len>max_rel_type_len) max_rel_type_len=len;
  559. }
  560. opc_uint32_t max_ext_rel_len=xmlStrlen(_X("External Relations"));
  561. for(opc_uint32_t i=0;i<c->externalrelation_items;i++) {
  562. opc_uint32_t const len=xmlStrlen(c->externalrelation_array[i].target);
  563. if (len>max_ext_rel_len) max_ext_rel_len=len;
  564. }
  565. opc_uint32_t max_part_name=xmlStrlen(_X("Part"));
  566. opc_uint32_t max_part_type=xmlStrlen(_X("Type"));
  567. for(opc_uint32_t i=0;i<c->part_items;i++) {
  568. if (-1!=c->part_array[i].first_segment_id) { // deleted?
  569. opc_uint32_t const name_len=xmlStrlen(c->part_array[i].name);
  570. if (name_len>max_part_name) max_part_name=name_len;
  571. opc_uint32_t const type_len=xmlStrlen(opcPartGetType(c, c->part_array[i].name));
  572. if (type_len>max_part_type) max_part_type=type_len;
  573. }
  574. }
  575. opc_uint32_t max_rel_src=xmlStrlen(_X("Source"));
  576. opc_uint32_t max_rel_id=xmlStrlen(_X("Id"));
  577. opc_uint32_t max_rel_dest=xmlStrlen(_X("Destination"));
  578. opc_uint32_t max_rel_type=xmlStrlen(_X("Type"));
  579. if (c->relationtype_items>0) {
  580. opc_uint32_t const src_len=xmlStrlen(_X("[root]"));
  581. if (src_len>max_rel_src) max_rel_src=src_len;
  582. opcContainerRelCalcMax(c, NULL, c->relation_array, c->relation_items, &max_rel_src, &max_rel_id, &max_rel_dest, &max_rel_type);
  583. }
  584. for(opc_uint32_t i=0;i<c->part_items;i++) {
  585. if (c->part_array[i].relation_items>0) {
  586. opcContainerRelCalcMax(c,
  587. c->part_array[i].name,
  588. c->part_array[i].relation_array, c->part_array[i].relation_items,
  589. &max_rel_src,
  590. &max_rel_id,
  591. &max_rel_dest,
  592. &max_rel_type);
  593. }
  594. }
  595. opcContainerDumpString(out, _X("Content Types"), max_content_type_len, OPC_TRUE);
  596. opcContainerDumpLine(out, '-', max_content_type_len, OPC_TRUE);
  597. for(opc_uint32_t i=0;i<c->type_items;i++) {
  598. opcContainerDumpString(out, c->type_array[i].type, max_content_type_len, OPC_TRUE);
  599. }
  600. opcContainerDumpLine(out, '-', max_content_type_len, OPC_TRUE);
  601. opcContainerDumpString(out, _X(""), 0, OPC_TRUE);
  602. opcContainerDumpString(out, _X("Extension"), max_extension_len, OPC_FALSE); fputc('|', out);
  603. opcContainerDumpString(out, _X("Type"), max_extension_type_len, OPC_TRUE);
  604. opcContainerDumpLine(out, '-', max_extension_len, OPC_FALSE); fputc('|', out);
  605. opcContainerDumpLine(out, '-', max_extension_type_len, OPC_TRUE);
  606. for(opc_uint32_t i=0;i<c->extension_items;i++) {
  607. opcContainerDumpString(out, c->extension_array[i].extension, max_extension_len, OPC_FALSE); fputc('|', out);
  608. opcContainerDumpString(out, c->extension_array[i].type, max_extension_type_len, OPC_TRUE);
  609. }
  610. opcContainerDumpLine(out, '-', max_extension_len, OPC_FALSE); fputc('|', out);
  611. opcContainerDumpLine(out, '-', max_extension_type_len, OPC_TRUE);
  612. opcContainerDumpString(out, _X(""), 0, OPC_TRUE);
  613. opcContainerDumpString(out, _X("Relation Types"), max_rel_type_len, OPC_TRUE);
  614. opcContainerDumpLine(out, '-', max_rel_type_len, OPC_TRUE);
  615. for(opc_uint32_t i=0;i<c->relationtype_items;i++) {
  616. opcContainerDumpString(out, c->relationtype_array[i].type, max_rel_type_len, OPC_TRUE);
  617. }
  618. opcContainerDumpLine(out, '-', max_rel_type_len, OPC_TRUE);
  619. opcContainerDumpString(out, _X(""), 0, OPC_TRUE);
  620. opcContainerDumpString(out, _X("External Relations"), max_ext_rel_len, OPC_TRUE);
  621. opcContainerDumpLine(out, '-', max_ext_rel_len, OPC_TRUE);
  622. for(opc_uint32_t i=0;i<c->externalrelation_items;i++) {
  623. opcContainerDumpString(out, c->externalrelation_array[i].target, max_ext_rel_len, OPC_TRUE);
  624. }
  625. opcContainerDumpLine(out, '-', max_ext_rel_len, OPC_TRUE);
  626. opcContainerDumpString(out, _X(""), 0, OPC_TRUE);
  627. opcContainerDumpString(out, _X("Part"), max_part_name, OPC_FALSE); fputc('|', out);
  628. opcContainerDumpString(out, _X("Type"), max_part_type, OPC_TRUE);
  629. opcContainerDumpLine(out, '-', max_part_name, OPC_FALSE); fputc('|', out);
  630. opcContainerDumpLine(out, '-', max_part_type, OPC_TRUE);
  631. for(opc_uint32_t i=0;i<c->part_items;i++) {
  632. if (-1!=c->part_array[i].first_segment_id) { // deleted?
  633. opcContainerDumpString(out, c->part_array[i].name, max_part_name, OPC_FALSE); fputc('|', out);
  634. opcContainerDumpString(out, opcPartGetType(c, c->part_array[i].name), max_part_type, OPC_TRUE);
  635. }
  636. }
  637. opcContainerDumpLine(out, '-', max_part_name, OPC_FALSE); fputc('|', out);
  638. opcContainerDumpLine(out, '-', max_part_type, OPC_TRUE);
  639. opcContainerDumpString(out, _X(""), 0, OPC_TRUE);
  640. opcContainerDumpString(out, _X("Source"), max_rel_src, OPC_FALSE); fputc('|', out);
  641. opcContainerDumpString(out, _X("Id"), max_rel_id, OPC_FALSE); fputc('|', out);
  642. opcContainerDumpString(out, _X("Destination"), max_rel_dest, OPC_FALSE); fputc('|', out);
  643. opcContainerDumpString(out, _X("Type"), max_rel_type, OPC_TRUE);
  644. opcContainerDumpLine(out, '-', max_rel_src, OPC_FALSE); fputc('|', out);
  645. opcContainerDumpLine(out, '-', max_rel_id, OPC_FALSE); fputc('|', out);
  646. opcContainerDumpLine(out, '-', max_rel_dest, OPC_FALSE); fputc('|', out);
  647. opcContainerDumpLine(out, '-', max_rel_type, OPC_TRUE);
  648. if (c->relation_items>0) {
  649. opcContainerRelDump(c,
  650. out,
  651. NULL,
  652. c->relation_array,
  653. c->relation_items,
  654. max_rel_src,
  655. max_rel_id,
  656. max_rel_dest,
  657. max_rel_type);
  658. }
  659. for(opc_uint32_t i=0;i<c->part_items;i++) {
  660. if (-1!=c->part_array[i].first_segment_id && c->part_array[i].relation_items>0) {
  661. opcContainerRelDump(c,
  662. out,
  663. c->part_array[i].name,
  664. c->part_array[i].relation_array, c->part_array[i].relation_items,
  665. max_rel_src,
  666. max_rel_id,
  667. max_rel_dest,
  668. max_rel_type);
  669. }
  670. }
  671. opcContainerDumpLine(out, '-', max_rel_src, OPC_FALSE); fputc('|', out);
  672. opcContainerDumpLine(out, '-', max_rel_id, OPC_FALSE); fputc('|', out);
  673. opcContainerDumpLine(out, '-', max_rel_dest, OPC_FALSE); fputc('|', out);
  674. opcContainerDumpLine(out, '-', max_rel_type, OPC_TRUE);
  675. return OPC_ERROR_NONE;
  676. }
  677. static opc_error_t opcContainerZipLoaderLoadSegment(void *iocontext,
  678. void *userctx,
  679. opcZipSegmentInfo_t *info,
  680. opcZipLoaderOpenCallback *open,
  681. opcZipLoaderReadCallback *read,
  682. opcZipLoaderCloseCallback *close,
  683. opcZipLoaderSkipCallback *skip) {
  684. opc_error_t err=OPC_ERROR_NONE;
  685. opcContainer *c=(opcContainer *)userctx;
  686. OPC_ENSURE(0==skip(iocontext));
  687. if (info->rels_segment) {
  688. if (info->name[0]==0) {
  689. OPC_ASSERT(-1==c->rels_segment_id); // loaded twice??
  690. c->rels_segment_id=opcZipLoadSegment(c->storage, OPC_SEGMENT_ROOTRELS, info->rels_segment, info);
  691. OPC_ASSERT(-1!=c->rels_segment_id); // not loaded??
  692. } else {
  693. opcContainerPart *part=opcContainerInsertPart(c, info->name, OPC_TRUE);
  694. if (NULL!=part) {
  695. OPC_ASSERT(-1==part->rel_segment_id); // loaded twice??
  696. OPC_ASSERT(NULL!=part->name); // no name given???
  697. part->rel_segment_id=opcZipLoadSegment(c->storage, part->name, info->rels_segment, info);
  698. OPC_ASSERT(-1!=part->rel_segment_id); // not loaded???
  699. } else {
  700. err=OPC_ERROR_MEMORY;
  701. }
  702. }
  703. } else if (xmlStrcmp(info->name, OPC_SEGMENT_CONTENTTYPES)==0) {
  704. OPC_ASSERT(-1==c->content_types_segment_id); // loaded twice??
  705. c->content_types_segment_id=opcZipLoadSegment(c->storage, OPC_SEGMENT_CONTENTTYPES, info->rels_segment, info);
  706. OPC_ASSERT(-1!=c->content_types_segment_id); // not loaded??
  707. } else {
  708. opcContainerPart *part=opcContainerInsertPart(c, info->name, OPC_TRUE);
  709. if (NULL!=part) {
  710. OPC_ASSERT(-1==part->first_segment_id); // loaded twice???
  711. OPC_ASSERT(NULL!=part->name); // no name given???
  712. part->first_segment_id=opcZipLoadSegment(c->storage, part->name, info->rels_segment, info);
  713. OPC_ASSERT(-1!=part->first_segment_id); // not loaded???
  714. part->last_segment_id=part->first_segment_id;
  715. if (info->name_len>0 && ('/'==info->name[info->name_len-1] || '\\'==info->name[info->name_len-1])) {
  716. // it is a directoy, get rid of it...
  717. opcContainerDeletePartEx(c, part->name, info->rels_segment);
  718. OPC_ASSERT(-1==part->first_segment_id && -1==part->last_segment_id);
  719. part=NULL; // no longer valid
  720. }
  721. } else {
  722. err=OPC_ERROR_MEMORY;
  723. }
  724. }
  725. return err;
  726. }
  727. static void opcContainerGetOutputPartSegment(opcContainer *container, const xmlChar *name, opc_bool_t rels_segment, opc_uint32_t **first_segment_ref, opc_uint32_t **last_segment_ref) {
  728. if (OPC_SEGMENT_CONTENTTYPES==name) {
  729. OPC_ASSERT(!rels_segment);
  730. *first_segment_ref=&container->content_types_segment_id;
  731. *last_segment_ref=NULL;
  732. } else if (OPC_SEGMENT_ROOTRELS==name) {
  733. OPC_ASSERT(rels_segment);
  734. *first_segment_ref=&container->rels_segment_id;
  735. *last_segment_ref=NULL;
  736. } else {
  737. opcContainerPart *part=opcContainerInsertPart(container, name, OPC_FALSE);
  738. if (NULL!=part) {
  739. if (rels_segment) {
  740. *first_segment_ref=&part->rel_segment_id;
  741. *last_segment_ref=NULL;
  742. } else {
  743. *first_segment_ref=&part->first_segment_id;
  744. *last_segment_ref=&part->last_segment_id;
  745. }
  746. } else {
  747. OPC_ASSERT(OPC_FALSE); // should not happen
  748. *first_segment_ref=NULL;
  749. *last_segment_ref=NULL;
  750. }
  751. }
  752. }
  753. opcContainerInputStream* opcContainerOpenInputStreamEx(opcContainer *container, const xmlChar *name, opc_bool_t rels_segment) {
  754. opcContainerInputStream* ret=NULL;
  755. opc_uint32_t *first_segment=NULL;
  756. opc_uint32_t *last_segment=NULL;
  757. opcContainerGetOutputPartSegment(container, name, rels_segment, &first_segment, &last_segment);
  758. OPC_ASSERT(NULL!=first_segment);
  759. if (NULL!=first_segment) {
  760. ret=(opcContainerInputStream*)xmlMalloc(sizeof(opcContainerInputStream));
  761. if (NULL!=ret) {
  762. opc_bzero_mem(ret, sizeof(*ret));
  763. ret->container=container;
  764. ret->stream=opcZipOpenInputStream(container->storage, *first_segment);
  765. if (NULL==ret->stream) {
  766. xmlFree(ret); ret=NULL; // error
  767. }
  768. }
  769. }
  770. return ret;
  771. }
  772. opcContainerInputStream* opcContainerOpenInputStream(opcContainer *container, const xmlChar *name) {
  773. return opcContainerOpenInputStreamEx(container, name, OPC_FALSE);
  774. }
  775. opc_uint32_t opcContainerReadInputStream(opcContainerInputStream* stream, opc_uint8_t *buffer, opc_uint32_t buffer_len) {
  776. return opcZipReadInputStream(stream->container->storage, stream->stream, buffer, buffer_len);
  777. }
  778. opc_error_t opcContainerCloseInputStream(opcContainerInputStream* stream) {
  779. opc_error_t ret=opcZipCloseInputStream(stream->container->storage, stream->stream);
  780. xmlFree(stream);
  781. return ret;
  782. }
  783. opcCompressionOption_t opcContainerGetInputStreamCompressionOption(opcContainerInputStream* stream) {
  784. opcCompressionOption_t ret=OPC_COMPRESSIONOPTION_NONE;
  785. if (8==stream->stream->inflateState.compression_method) {
  786. // for now its just enough to know that we have a compression...
  787. ret=OPC_COMPRESSIONOPTION_NORMAL; //@TODO look at stream to figure out real compression i.e. NORMAL, FAST, etc...
  788. }
  789. return ret;
  790. }
  791. opcContainerOutputStream* opcContainerCreateOutputStreamEx(opcContainer *container, const xmlChar *name, opc_bool_t rels_segment, opcCompressionOption_t compression_option) {
  792. opcContainerOutputStream* ret=NULL;
  793. opc_uint32_t *first_segment=NULL;
  794. opc_uint32_t *last_segment=NULL;
  795. opcContainerGetOutputPartSegment(container, name, rels_segment, &first_segment, &last_segment);
  796. OPC_ASSERT(NULL!=first_segment);
  797. if (NULL!=first_segment) {
  798. ret=(opcContainerOutputStream*)xmlMalloc(sizeof(opcContainerOutputStream));
  799. if (NULL!=ret) {
  800. opc_bzero_mem(ret, sizeof(*ret));
  801. ret->container=container;
  802. opc_uint16_t compression_method=0; // no compression by default
  803. opc_uint16_t bit_flag=0;
  804. switch(compression_option) {
  805. case OPC_COMPRESSIONOPTION_NONE:
  806. OPC_ASSERT(0==compression_method);
  807. OPC_ASSERT(0==bit_flag);
  808. break;
  809. case OPC_COMPRESSIONOPTION_NORMAL:
  810. compression_method=8;
  811. bit_flag|=0<<1;
  812. break;
  813. case OPC_COMPRESSIONOPTION_MAXIMUM:
  814. compression_method=8;
  815. bit_flag|=1<<1;
  816. break;
  817. case OPC_COMPRESSIONOPTION_FAST:
  818. compression_method=8;
  819. bit_flag|=2<<1;
  820. break;
  821. case OPC_COMPRESSIONOPTION_SUPERFAST:
  822. compression_method=8;
  823. bit_flag|=3<<1;
  824. break;
  825. }
  826. ret->stream=opcZipCreateOutputStream(container->storage, first_segment, name, rels_segment, 0, 0, compression_method, bit_flag);
  827. ret->partName=name;
  828. ret->rels_segment=rels_segment;
  829. if (NULL==ret->stream) {
  830. xmlFree(ret); ret=NULL; // error
  831. }
  832. }
  833. }
  834. return ret;
  835. }
  836. opcContainerOutputStream* opcContainerCreateOutputStream(opcContainer *container, const xmlChar *name, opcCompressionOption_t compression_option) {
  837. return opcContainerCreateOutputStreamEx(container, name, OPC_FALSE, compression_option);
  838. }
  839. opc_uint32_t opcContainerWriteOutputStream(opcContainerOutputStream* stream, const opc_uint8_t *buffer, opc_uint32_t buffer_len) {
  840. return opcZipWriteOutputStream(stream->container->storage, stream->stream, buffer, buffer_len);
  841. }
  842. opc_error_t opcContainerCloseOutputStream(opcContainerOutputStream* stream) {
  843. opc_error_t ret=OPC_ERROR_MEMORY;
  844. opc_uint32_t *first_segment=NULL;
  845. opc_uint32_t *last_segment=NULL;
  846. opcContainerGetOutputPartSegment(stream->container, stream->partName, stream->rels_segment, &first_segment, &last_segment);
  847. OPC_ASSERT(NULL!=first_segment);
  848. if (NULL!=first_segment) {
  849. ret=opcZipCloseOutputStream(stream->container->storage, stream->stream, first_segment);
  850. if (NULL!=last_segment) {
  851. *last_segment=*first_segment;
  852. }
  853. xmlFree(stream);
  854. }
  855. return ret;
  856. }
  857. static opc_error_t opcContainerInit(opcContainer *c, opcContainerOpenMode mode, void *userContext) {
  858. opc_bzero_mem(c, sizeof(*c));
  859. c->content_types_segment_id=-1;
  860. c->rels_segment_id=-1;
  861. c->mode=mode;
  862. c->userContext=userContext;
  863. return OPC_ERROR_NONE;
  864. }
  865. static opcContainer *opcContainerLoadFromZip(opcContainer *c) {
  866. OPC_ASSERT(NULL==c->storage); // loaded twice??
  867. c->storage=opcZipCreate(&c->io);
  868. if (NULL!=c->storage) {
  869. if (OPC_ERROR_NONE==opcZipLoader(&c->io, c, opcContainerZipLoaderLoadSegment)) {
  870. // successfull loaded!
  871. OPC_ENSURE(OPC_ERROR_NONE==opcZipGC(c->storage));
  872. if (-1!=c->content_types_segment_id) {
  873. mceTextReader_t reader;
  874. if (OPC_ERROR_NONE==opcXmlReaderOpenEx(c, &reader, OPC_SEGMENT_CONTENTTYPES, OPC_FALSE, NULL, NULL, 0)) {
  875. static const char ns[]="http://schemas.openxmlformats.org/package/2006/content-types";
  876. mce_start_document(&reader) {
  877. mce_start_element(&reader, _X(ns), _X("Types")) {
  878. mce_skip_attributes(&reader);
  879. mce_start_children(&reader) {
  880. mce_start_element(&reader, NULL, _X("Default")) {
  881. const xmlChar *ext=NULL;
  882. const xmlChar *type=NULL;
  883. mce_start_attributes(&reader) {
  884. mce_start_attribute(&reader, NULL, _X("Extension")) {
  885. ext=xmlTextReaderConstValue(reader.reader);
  886. } mce_end_attribute(&reader);
  887. mce_start_attribute(&reader, NULL, _X("ContentType")) {
  888. type=xmlTextReaderConstValue(reader.reader);
  889. } mce_end_attribute(&reader);
  890. } mce_end_attributes(&reader);
  891. mce_error_guard_start(&reader) {
  892. mce_error(&reader, NULL==ext || ext[0]==0, MCE_ERROR_VALIDATION, "Missing @Extension attribute!");
  893. mce_error(&reader, NULL==type || type[0]==0, MCE_ERROR_VALIDATION, "Missing @ContentType attribute!");
  894. opcContainerType *ct=insertType(c, type, OPC_TRUE);
  895. mce_error(&reader, NULL==ct, MCE_ERROR_MEMORY, NULL);
  896. opcContainerExtension *ce=opcContainerInsertExtension(c, ext, OPC_TRUE);
  897. mce_error(&reader, NULL==ce, MCE_ERROR_MEMORY, NULL);
  898. mce_errorf(&reader, NULL!=ce->type && 0!=xmlStrcmp(ce->type, type), MCE_ERROR_VALIDATION, "Extension \"%s\" is mapped to type \"%s\" as well as \"%s\"", ext, type, ce->type);
  899. ce->type=ct->type;
  900. } mce_error_guard_end(&reader);
  901. mce_skip_children(&reader);
  902. } mce_end_element(&reader);
  903. mce_start_element(&reader, NULL, _X("Override")) {
  904. const xmlChar *name=NULL;
  905. const xmlChar *type=NULL;
  906. mce_start_attributes(&reader) {
  907. mce_start_attribute(&reader, NULL, _X("PartName")) {
  908. name=xmlTextReaderConstValue(reader.reader);
  909. } mce_end_attribute(&reader);
  910. mce_start_attribute(&reader, NULL, _X("ContentType")) {
  911. type=xmlTextReaderConstValue(reader.reader);
  912. } mce_end_attribute(&reader);
  913. } mce_end_attributes(&reader);
  914. mce_error_guard_start(&reader) {
  915. mce_error(&reader, NULL==name, MCE_ERROR_XML, "Attribute @PartName not given!");
  916. mce_error(&reader, NULL==type, MCE_ERROR_XML, "Attribute @ContentType not given!");
  917. opcContainerType*ct=insertType(c, type, OPC_TRUE);
  918. mce_error(&reader, NULL==ct, MCE_ERROR_MEMORY, NULL);
  919. mce_error_strictf(&reader, '/'!=name[0], MCE_ERROR_MEMORY, "Part %s MUST start with a '/'", name);
  920. opcContainerPart *part=opcContainerInsertPart(c, (name[0]=='/'?name+1:name), OPC_FALSE);
  921. mce_error_strictf(&reader, NULL==part, MCE_ERROR_MEMORY, "Part %s does not exist.", name);
  922. if (NULL!=part) {
  923. part->type=ct->type;
  924. }
  925. } mce_error_guard_end(&reader);
  926. mce_skip_children(&reader);
  927. } mce_end_element(&reader);
  928. mce_start_text(&reader) {
  929. //@TODO ensure whitespaces...
  930. } mce_end_text(&reader);
  931. } mce_end_children(&reader);
  932. } mce_end_element(&reader);
  933. } mce_end_document(&reader);
  934. OPC_ENSURE(0==mceTextReaderCleanup(&reader));
  935. }
  936. }
  937. if (NULL!=c && -1!=c->rels_segment_id) {
  938. opcConstainerParseRels(c, OPC_SEGMENT_ROOTRELS, &c->relation_array, &c->relation_items);
  939. }
  940. for(opc_uint32_t i=0;NULL!=c && i<c->part_items;i++) {
  941. opcContainerPart *part=&c->part_array[i];
  942. if (-1!=part->rel_segment_id) {
  943. opcConstainerParseRels(c, part->name, &part->relation_array, &part->relation_items);
  944. }
  945. }
  946. } else {
  947. opcFileCleanupIO(&c->io); // error loading
  948. opcZipClose(c->storage, NULL);
  949. xmlFree(c); c=NULL;
  950. }
  951. } else {
  952. opcFileCleanupIO(&c->io); // error creating zip
  953. xmlFree(c); c=NULL;
  954. }
  955. return c;
  956. }
  957. static opc_uint32_t opcContainerGenerateFileFlags(opcContainerOpenMode mode) {
  958. opc_uint32_t flags=(OPC_OPEN_READ_ONLY!=mode?OPC_FILE_WRITE | OPC_FILE_READ:OPC_FILE_READ);
  959. if (OPC_OPEN_WRITE_ONLY==mode) flags=flags | OPC_FILE_TRUNC;
  960. return flags;
  961. }
  962. opcContainer* opcContainerOpen(const xmlChar *fileName,
  963. opcContainerOpenMode mode,
  964. void *userContext,
  965. const xmlChar *destName) {
  966. opcContainer*c=(opcContainer*)xmlMalloc(sizeof(opcContainer));
  967. if (NULL!=c) {
  968. OPC_ENSURE(OPC_ERROR_NONE==opcContainerInit(c, mode, userContext));
  969. if (OPC_ERROR_NONE==opcFileInitIOFile(&c->io, fileName, opcContainerGenerateFileFlags(mode))) {
  970. c=opcContainerLoadFromZip(c);
  971. } else {
  972. xmlFree(c); c=NULL; // error init io
  973. }
  974. }
  975. return c;
  976. }
  977. opcContainer* opcContainerOpenMem(const opc_uint8_t *data, opc_uint32_t data_len,
  978. opcContainerOpenMode mode,
  979. void *userContext) {
  980. opcContainer*c=(opcContainer*)xmlMalloc(sizeof(opcContainer));
  981. if (NULL!=c) {
  982. OPC_ENSURE(OPC_ERROR_NONE==opcContainerInit(c, mode, userContext));
  983. if (OPC_ERROR_NONE==opcFileInitIOMemory(&c->io, data, data_len, opcContainerGenerateFileFlags(mode))) {
  984. c=opcContainerLoadFromZip(c);
  985. } else {
  986. xmlFree(c); c=NULL; // error init io
  987. }
  988. }
  989. return c;
  990. }
  991. opcContainer* opcContainerOpenIO(opcFileReadCallback *ioread,
  992. opcFileWriteCallback *iowrite,
  993. opcFileCloseCallback *ioclose,
  994. opcFileSeekCallback *ioseek,
  995. opcFileTrimCallback *iotrim,
  996. opcFileFlushCallback *ioflush,
  997. void *iocontext,
  998. pofs_t file_size,
  999. opcContainerOpenMode mode,
  1000. void *userContext) {
  1001. opcContainer*c=(opcContainer*)xmlMalloc(sizeof(opcContainer));
  1002. if (NULL!=c) {
  1003. OPC_ENSURE(OPC_ERROR_NONE==opcContainerInit(c, mode, userContext));
  1004. if (OPC_ERROR_NONE==opcFileInitIO(&c->io, ioread, iowrite, ioclose, ioseek, iotrim, ioflush, iocontext, file_size, opcContainerGenerateFileFlags(mode))) {
  1005. c=opcContainerLoadFromZip(c);
  1006. } else {
  1007. xmlFree(c); c=NULL; // error init io
  1008. }
  1009. }
  1010. return c;
  1011. }
  1012. static void opcContainerWriteUtf8Raw(opcContainerOutputStream *out, const xmlChar *str) {
  1013. opc_uint32_t str_len=xmlStrlen(str);
  1014. OPC_ENSURE(str_len==opcContainerWriteOutputStream(out, str, str_len));
  1015. }
  1016. static void opcContainerWriteUtf8(opcContainerOutputStream *out, const xmlChar *str) {
  1017. for(;0!=*str; str++) {
  1018. switch(*str) {
  1019. case '"':
  1020. OPC_ENSURE(6==opcContainerWriteOutputStream(out, _X("&quot;"), 6));
  1021. break;
  1022. case '\'':
  1023. OPC_ENSURE(6==opcContainerWriteOutputStream(out, _X("&apos;"), 6));
  1024. break;
  1025. case '&':
  1026. OPC_ENSURE(5==opcContainerWriteOutputStream(out, _X("&amp;"), 5));
  1027. break;
  1028. case '<':
  1029. OPC_ENSURE(4==opcContainerWriteOutputStream(out, _X("&lt;"), 4));
  1030. break;
  1031. case '>':
  1032. OPC_ENSURE(4==opcContainerWriteOutputStream(out, _X("&gt;"), 4));
  1033. break;
  1034. default:
  1035. OPC_ENSURE(1==opcContainerWriteOutputStream(out, str, 1));
  1036. break;
  1037. }
  1038. }
  1039. }
  1040. static void opcContainerWriteContentTypes(opcContainer *c) {
  1041. opcContainerOutputStream *out=opcContainerCreateOutputStreamEx(c, OPC_SEGMENT_CONTENTTYPES, OPC_FALSE, OPC_COMPRESSIONOPTION_NORMAL);
  1042. if (NULL!=out) {
  1043. opcContainerWriteUtf8Raw(out, _X("<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">"));
  1044. for(opc_uint32_t i=0;i<c->extension_items;i++) {
  1045. opcContainerWriteUtf8Raw(out, _X("<Default Extension=\""));
  1046. opcContainerWriteUtf8(out, c->extension_array[i].extension);
  1047. opcContainerWriteUtf8Raw(out, _X("\" ContentType=\""));
  1048. opcContainerWriteUtf8(out, c->extension_array[i].type);
  1049. opcContainerWriteUtf8Raw(out, _X("\"/>"));
  1050. }
  1051. for(opc_uint32_t i=0;i<c->part_items;i++) {
  1052. if (NULL!=c->part_array[i].type) {
  1053. opcContainerWriteUtf8Raw(out, _X("<Override PartName=\"/"));
  1054. opcContainerWriteUtf8(out, c->part_array[i].name);
  1055. opcContainerWriteUtf8Raw(out, _X("\" ContentType=\""));
  1056. opcContainerWriteUtf8(out, c->part_array[i].type);
  1057. opcContainerWriteUtf8Raw(out, _X("\"/>"));
  1058. }
  1059. }
  1060. opcContainerWriteUtf8Raw(out, _X("</Types>"));
  1061. opcContainerCloseOutputStream(out);
  1062. }
  1063. }
  1064. static void opcHelperCalcRelPath(xmlChar *rel_path, opc_uint32_t rel_path_max, const xmlChar *base, const xmlChar *path) {
  1065. opc_uint32_t base_pos=0;
  1066. opc_uint32_t path_pos=0;
  1067. opc_uint32_t rel_pos=0;
  1068. while(0!=base[base_pos]) {
  1069. opc_uint32_t base_next=base_pos; while(0!=base[base_next] && '/'!=base[base_next]) base_next++;
  1070. opc_uint32_t path_next=path_pos; while(0!=base[path_next] && '/'!=base[path_next]) path_next++;
  1071. if ('/'==base[base_next]) {
  1072. if (base_next==path_next && 0==xmlStrncmp(base+base_pos, path+path_pos, base_next-base_pos)) {
  1073. base_pos=base_next+1;
  1074. path_pos=path_next+1;
  1075. } else {
  1076. base_pos=base_next+1;
  1077. strncpy((char *)(rel_path+rel_pos), "../", rel_path_max-rel_pos);
  1078. rel_pos+=3;
  1079. }
  1080. } else {
  1081. OPC_ASSERT(0==base[base_next]);
  1082. base_pos=base_next;
  1083. OPC_ASSERT(0==base[base_pos]);
  1084. }
  1085. }
  1086. strncpy((char *)(rel_path+rel_pos), (const char *)(path+path_pos), rel_path_max-rel_pos);
  1087. #if 0 // for debugging only...
  1088. xmlChar helper[OPC_MAX_PATH];
  1089. opc_container_normalize_part_to_helper_buffer(helper, sizeof(helper), base, rel_path);
  1090. OPC_ASSERT(0==xmlStrcmp(path, helper));
  1091. #endif
  1092. }
  1093. static void opcContainerWriteRels(opcContainer *c, const xmlChar *part_name, opcContainerRelation *relation_array, opc_uint32_t relation_items) {
  1094. opcContainerOutputStream *out=opcContainerCreateOutputStreamEx(c, part_name, OPC_TRUE, OPC_COMPRESSIONOPTION_NORMAL);
  1095. if (NULL!=out) {
  1096. opcContainerWriteUtf8Raw(out, _X("<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"));
  1097. for(opc_uint32_t i=0;i<relation_items;i++) {
  1098. opcContainerWriteUtf8Raw(out, _X("<Relationship Id=\""));
  1099. opcContainerWriteUtf8(out, c->relprefix_array[OPC_CONTAINER_RELID_PREFIX(relation_array[i].relation_id)].prefix);
  1100. if (OPC_CONTAINER_RELID_COUNTER_NONE!=OPC_CONTAINER_RELID_COUNTER(relation_array[i].relation_id)) {
  1101. char buf[20];
  1102. snprintf(buf, sizeof(buf), "%i", OPC_CONTAINER_RELID_COUNTER(relation_array[i].relation_id));
  1103. opcContainerWriteUtf8Raw(out, _X(buf));
  1104. }
  1105. opcContainerWriteUtf8Raw(out, _X("\" Type=\""));
  1106. opcContainerWriteUtf8(out, relation_array[i].relation_type);
  1107. if (0==relation_array[i].target_mode) {
  1108. opcContainerWriteUtf8Raw(out, _X("\" Target=\""));
  1109. xmlChar rel_path[OPC_MAX_PATH];
  1110. opcHelperCalcRelPath(rel_path, sizeof(rel_path), part_name, relation_array[i].target_ptr);
  1111. opcContainerWriteUtf8(out, rel_path);
  1112. } else {
  1113. OPC_ASSERT(1==relation_array[i].target_mode);
  1114. opcContainerWriteUtf8Raw(out, _X("\" TargetMode=\"External\" Target=\""));
  1115. opcContainerWriteUtf8(out, relation_array[i].target_ptr);
  1116. }
  1117. opcContainerWriteUtf8Raw(out, _X("\"/>"));
  1118. }
  1119. opcContainerWriteUtf8Raw(out, _X("</Relationships>"));
  1120. opcContainerCloseOutputStream(out);
  1121. }
  1122. }
  1123. static void opcContainerWriteAllRels(opcContainer *c) {
  1124. if (c->relation_items>0) {
  1125. opcContainerWriteRels(c, OPC_SEGMENT_ROOTRELS, c->relation_array, c->relation_items);
  1126. }
  1127. for(opc_uint32_t i=0;i<c->part_items;i++) {
  1128. if (c->part_array[i].relation_items>0) {
  1129. opcContainerWriteRels(c, c->part_array[i].name, c->part_array[i].relation_array, c->part_array[i].relation_items);
  1130. }
  1131. }
  1132. }
  1133. opc_error_t opcContainerCommit(opcContainer *c, opc_bool_t trim) {
  1134. opc_error_t ret=OPC_ERROR_NONE;
  1135. if (OPC_OPEN_READ_ONLY!=c->mode) {
  1136. opcContainerWriteContentTypes(c);
  1137. opcContainerWriteAllRels(c);
  1138. ret=opcZipCommit(c->storage, trim);
  1139. }
  1140. return ret;
  1141. }
  1142. opc_error_t opcContainerClose(opcContainer *c, opcContainerCloseMode mode) {
  1143. opc_bool_t trim=(mode!=OPC_CLOSE_NOW);
  1144. opc_error_t ret=opcContainerCommit(c, trim);
  1145. opcZipClose(c->storage, NULL); c->storage=NULL;
  1146. opcContainerFree(c);
  1147. return ret;
  1148. }
  1149. opc_bool_t opcContainerDeletePartEx(opcContainer *container, const xmlChar *partName, opc_bool_t rels_segment) {
  1150. opc_bool_t ret=OPC_FALSE;
  1151. if (OPC_SEGMENT_CONTENTTYPES==partName) {
  1152. OPC_ASSERT(!rels_segment);
  1153. ret=opcZipSegmentDelete(container->storage, &container->content_types_segment_id, NULL, NULL);
  1154. } else if (OPC_SEGMENT_ROOTRELS==partName) {
  1155. OPC_ASSERT(rels_segment);
  1156. ret=opcZipSegmentDelete(container->storage, &container->rels_segment_id, NULL, NULL);
  1157. } else {
  1158. opcContainerPart *part=opcContainerInsertPart(container, partName, OPC_FALSE);
  1159. if (NULL!=part) {
  1160. if (rels_segment) {
  1161. ret=opcZipSegmentDelete(container->storage, &part->rel_segment_id, NULL, NULL);
  1162. } else {
  1163. ret=opcZipSegmentDelete(container->storage, &part->first_segment_id, &part->last_segment_id, NULL);
  1164. }
  1165. }
  1166. }
  1167. return ret;
  1168. }
  1169. const xmlChar *opcContentTypeFirst(opcContainer *container) {
  1170. if (container->type_items>0) {
  1171. return container->type_array[0].type;
  1172. } else {
  1173. return NULL;
  1174. }
  1175. }
  1176. const xmlChar *opcContentTypeNext(opcContainer *container, const xmlChar *type) {
  1177. opcContainerType *t=insertType(container, type, OPC_FALSE);
  1178. if (NULL!=t && t>=container->type_array && t+1<container->type_array+container->type_items) {
  1179. return (t+1)->type;
  1180. } else {
  1181. return NULL;
  1182. }
  1183. }
  1184. const xmlChar *opcExtensionFirst(opcContainer *container) {
  1185. if (container->extension_items>0) {
  1186. return container->extension_array[0].extension;
  1187. } else {
  1188. return NULL;
  1189. }
  1190. }
  1191. const xmlChar *opcExtensionNext(opcContainer *container, const xmlChar *ext) {
  1192. opcContainerExtension *e=opcContainerInsertExtension(container, ext, OPC_FALSE);
  1193. if (NULL!=e && e>=container->extension_array && e+1<container->extension_array+container->extension_items) {
  1194. return (e+1)->extension;
  1195. } else {
  1196. return NULL;
  1197. }
  1198. }
  1199. const xmlChar *opcExtensionGetType(opcContainer *container, const xmlChar *ext) {
  1200. opcContainerExtension *e=opcContainerInsertExtension(container, ext, OPC_FALSE);
  1201. if (NULL!=e && e>=container->extension_array && e<container->extension_array+container->extension_items) {
  1202. return e->type;
  1203. } else {
  1204. return NULL;
  1205. }
  1206. }
  1207. const xmlChar *opcExtensionRegister(opcContainer *container, const xmlChar *ext, const xmlChar *type) {
  1208. opcContainerType *_type=insertType(container, type, OPC_TRUE);
  1209. opcContainerExtension *_ext=opcContainerInsertExtension(container, ext, OPC_TRUE);
  1210. if (_ext!=NULL && _type!=NULL) {
  1211. OPC_ASSERT(NULL==_ext->type);
  1212. _ext->type=_type->type;
  1213. return _ext->extension;
  1214. } else {
  1215. return NULL;
  1216. }
  1217. }
  1218. opc_uint32_t opcRelationAdd(opcContainer *container, opcPart src, const xmlChar *rid, opcPart dest, const xmlChar *type) {
  1219. opc_uint32_t ret=-1;
  1220. opcContainerRelation **relation_array=NULL;
  1221. opc_uint32_t *relation_items=NULL;
  1222. if (OPC_PART_INVALID==src) {
  1223. relation_array=&container->relation_array;
  1224. relation_items=&container->relation_items;
  1225. } else {
  1226. opcContainerPart *src_part=opcContainerInsertPart(container, src, OPC_FALSE);
  1227. if (NULL!=src_part) {
  1228. relation_array=&src_part->relation_array;
  1229. relation_items=&src_part->relation_items;
  1230. }
  1231. }
  1232. opcContainerPart *dest_part=opcContainerInsertPart(container, dest, OPC_FALSE);
  1233. char buf[OPC_MAX_PATH];
  1234. strncpy(buf, (const char *)rid, OPC_MAX_PATH);
  1235. opc_uint32_t counter=-1;
  1236. opc_uint32_t id_len=splitRelPrefix(container, _X(buf), &counter);
  1237. buf[id_len]=0;
  1238. opc_uint32_t rel_id=createRelId(container, _X(buf), counter);
  1239. if (NULL!=relation_array && NULL!=dest_part) {
  1240. opcContainerRelationType *rel_type=(NULL!=type?opcContainerInsertRelationType(container, type, OPC_TRUE):NULL);
  1241. opcContainerRelation *rel=opcContainerInsertRelation(relation_array, relation_items, rel_id, (NULL!=rel_type?rel_type->type:NULL), 0, dest_part->name);
  1242. if (NULL!=rel) {
  1243. OPC_ASSERT(rel>=*relation_array && rel<*relation_array+*relation_items);
  1244. OPC_ASSERT(0==rel->target_mode);
  1245. ret=rel_id;
  1246. }
  1247. }
  1248. return ret;
  1249. }
  1250. opc_uint32_t opcRelationAddExternal(opcContainer *container, opcPart src, const xmlChar *rid, const xmlChar *target, const xmlChar *type) {
  1251. opc_uint32_t ret=-1;
  1252. opcContainerRelation **relation_array=NULL;
  1253. opc_uint32_t *relation_items=NULL;
  1254. if (OPC_PART_INVALID==src) {
  1255. relation_array=&container->relation_array;
  1256. relation_items=&container->relation_items;
  1257. } else {
  1258. opcContainerPart *src_part=opcContainerInsertPart(container, src, OPC_FALSE);
  1259. if (NULL!=src_part) {
  1260. relation_array=&src_part->relation_array;
  1261. relation_items=&src_part->relation_items;
  1262. }
  1263. }
  1264. opcContainerExternalRelation *_target=insertExternalRelation(container, target, OPC_TRUE);
  1265. char buf[OPC_MAX_PATH];
  1266. strncpy(buf, (const char *)rid, OPC_MAX_PATH);
  1267. opc_uint32_t counter=-1;
  1268. opc_uint32_t id_len=splitRelPrefix(container, _X(buf), &counter);
  1269. buf[id_len]=0;
  1270. opc_uint32_t rel_id=createRelId(container, _X(buf), counter);
  1271. if (NULL!=relation_array && NULL!=_target) {
  1272. opcContainerRelationType *rel_type=(NULL!=type?opcContainerInsertRelationType(container, type, OPC_TRUE):NULL);
  1273. opcContainerRelation *rel=opcContainerInsertRelation(relation_array, relation_items, rel_id, (NULL!=rel_type?rel_type->type:NULL), 0, _target->target);
  1274. if (NULL!=rel) {
  1275. OPC_ASSERT(rel>=*relation_array && rel<*relation_array+*relation_items);
  1276. rel->target_mode=1;
  1277. ret=rel_id;
  1278. }
  1279. }
  1280. return ret;
  1281. }
  1282. static inline int qname_level_cmp_fct(const void *key, opc_uint32_t v, const void *array_, opc_uint32_t item) {
  1283. opcQNameLevel_t *q1=(opcQNameLevel_t*)key;
  1284. opcQNameLevel_t *q2=&((opcQNameLevel_t*)array_)[item];
  1285. int const ns_cmp=(NULL==q1->ns?(NULL==q2->ns?0:-1):(NULL==q2->ns?+1:xmlStrcmp(q1->ns, q2->ns)));
  1286. return (0==ns_cmp?xmlStrcmp(q1->ln, q2->ln):ns_cmp);
  1287. }
  1288. opc_error_t opcQNameLevelAdd(opcQNameLevel_t **list_array, opc_uint32_t *list_items, opcQNameLevel_t *item) {
  1289. opc_uint32_t i=0;
  1290. opc_error_t ret=OPC_ERROR_NONE;
  1291. if (!findItem(*list_array, *list_items, item, 0, qname_level_cmp_fct, &i)) {
  1292. if (NULL!=ensureItem((void**)list_array, *list_items, sizeof(opcQNameLevel_t))) {
  1293. ensureGap(*list_array, *list_items, i);
  1294. (*list_array)[i]=*item;
  1295. } else {
  1296. ret=OPC_ERROR_MEMORY;
  1297. }
  1298. }
  1299. return ret;
  1300. }
  1301. opcQNameLevel_t* opcQNameLevelLookup(opcQNameLevel_t *list_array, opc_uint32_t list_items, const xmlChar *ns, const xmlChar *ln) {
  1302. opcQNameLevel_t item;
  1303. item.level=0;
  1304. item.ln=(xmlChar *)ln;
  1305. item.ns=ns;
  1306. opc_uint32_t i=0;
  1307. opc_bool_t ret=NULL!=list_array && list_items>0 && findItem(list_array, list_items, &item, 0, qname_level_cmp_fct, &i);
  1308. return (ret?list_array+i:NULL);
  1309. }
  1310. opc_error_t opcQNameLevelCleanup(opcQNameLevel_t *list_array, opc_uint32_t *list_items, opc_uint32_t level, opc_uint32_t *max_level) {
  1311. opc_uint32_t i=0;
  1312. for(opc_uint32_t j=0;j<*list_items;j++) {
  1313. if (list_array[j].level>=level) {
  1314. OPC_ASSERT(list_array[j].level==level); // cleanup should be called for every level...
  1315. if (NULL!=list_array[j].ln) xmlFree(list_array[j].ln);
  1316. // list_array[j].ns is managed by ther parser...
  1317. } else {
  1318. if (NULL!=max_level && list_array[j].level>*max_level) *max_level=list_array[j].level;
  1319. list_array[i++]=list_array[j];
  1320. }
  1321. }
  1322. *list_items=i;
  1323. return OPC_ERROR_NONE;
  1324. }
  1325. opc_error_t opcQNameLevelPush(opcQNameLevel_t **list_array, opc_uint32_t *list_items, opcQNameLevel_t *item) {
  1326. opc_error_t ret=OPC_ERROR_NONE;
  1327. if (NULL!=(ensureItem((void**)list_array, *list_items, sizeof(opcQNameLevel_t)))) {
  1328. (*list_array)[*list_items]=*item;
  1329. (*list_items)++;
  1330. } else {
  1331. ret=OPC_ERROR_MEMORY;
  1332. }
  1333. return ret;
  1334. }
  1335. opc_bool_t opcQNameLevelPopIfMatch(opcQNameLevel_t *list_array, opc_uint32_t *list_items, const xmlChar *ns, const xmlChar *ln, opc_uint32_t level) {
  1336. opc_bool_t ret=*list_items>0 && list_array[(*list_items)-1].level==level;
  1337. if (ret) {
  1338. OPC_ASSERT(0==xmlStrcmp(list_array[(*list_items)-1].ln, ln) && 0==xmlStrcmp(list_array[(*list_items)-1].ns, ns));
  1339. OPC_ASSERT(*list_items>0);
  1340. if (NULL!=list_array[(*list_items)-1].ln) xmlFree(list_array[(*list_items)-1].ln);
  1341. (*list_items)--;
  1342. }
  1343. return ret;
  1344. }
  1345. const xmlChar *opcRelationTypeFirst(opcContainer *container) {
  1346. if (container->relationtype_items>0) {
  1347. return container->relationtype_array[0].type;
  1348. } else {
  1349. return NULL;
  1350. }
  1351. }
  1352. const xmlChar *opcRelationTypeNext(opcContainer *container, const xmlChar *type) {
  1353. opcContainerRelationType* t=opcContainerInsertRelationType(container, type, OPC_FALSE);
  1354. if (NULL!=t && t>=container->relationtype_array && t+1<container->relationtype_array+container->relationtype_items) {
  1355. return (t+1)->type;
  1356. } else {
  1357. return NULL;
  1358. }
  1359. }
  1360. const xmlChar *opcExternalTargetFirst(opcContainer *container) {
  1361. if (container->externalrelation_items>0) {
  1362. return container->externalrelation_array[0].target;
  1363. } else {
  1364. return NULL;
  1365. }
  1366. }
  1367. const xmlChar *opcExternalTargetNext(opcContainer *container, const xmlChar *target) {
  1368. opcContainerExternalRelation*e=insertExternalRelation(container, target, OPC_FALSE);
  1369. if (NULL!=e && e>=container->externalrelation_array && e+1<container->externalrelation_array+container->externalrelation_items) {
  1370. return (e+1)->target;
  1371. } else {
  1372. return NULL;
  1373. }
  1374. }