helper.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/helper.h>
  30. static puint32_t opcHelperEncodeFilename(const xmlChar *name, char *buf, int buf_len, opc_bool_t rels_segment) {
  31. int name_len=xmlStrlen(name);
  32. int len=name_len;
  33. int buf_ofs=0;
  34. int rels_ofs=name_len;
  35. if (rels_segment) {
  36. while(rels_ofs>0 && name[--rels_ofs]!='/'); // find last "/"
  37. if (name[rels_ofs]!='/') {
  38. buf_ofs+=snprintf(buf+buf_ofs, buf_len-buf_ofs, "_rels/");
  39. }
  40. }
  41. const xmlChar *rel_ch=name+rels_ofs;
  42. int ch=0;
  43. while(name_len>0 && 0!=(ch=xmlGetUTF8Char(name, &len)) && len>0 && len<=name_len && (NULL==buf || buf_ofs<buf_len)) {
  44. switch(ch) {
  45. case '/':
  46. if (name==rel_ch) {
  47. buf_ofs+=snprintf(buf+buf_ofs, buf_len-buf_ofs, "/_rels");
  48. }
  49. case ':':
  50. case '@':
  51. case '-':
  52. case '.':
  53. case '_':
  54. case '~':
  55. case '!':
  56. case '$':
  57. case '&':
  58. case '\'':
  59. case '(':
  60. case ')':
  61. case '*':
  62. case '+':
  63. case ',':
  64. case ';':
  65. case '=':
  66. case '[': // for internal use only
  67. case ']': // for internal use only
  68. if (NULL!=buf && buf_ofs<buf_len) buf[buf_ofs]=ch; buf_ofs++;
  69. break;
  70. default:
  71. if ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9')) {
  72. if (NULL!=buf) buf[buf_ofs]=ch; buf_ofs++;
  73. } else if (NULL==buf || (buf_ofs+3<=buf_len && 3==snprintf(buf+buf_ofs, 3, "%%%02X", ch))) {
  74. buf_ofs+=3;
  75. } else {
  76. buf_ofs=0; buf_len=0; // indicate error
  77. }
  78. }
  79. name_len-=len;
  80. name+=len;
  81. }
  82. if (rels_segment) {
  83. buf_ofs+=snprintf(buf+buf_ofs, buf_len-buf_ofs, ".rels");
  84. }
  85. return buf_ofs;
  86. }
  87. static puint32_t opcHelperFilenameAppendPiece(opc_uint32_t segment_number, opc_bool_t last_segment, char *buf, int buf_ofs, int buf_len) {
  88. if (buf_ofs>0) { // only append to non-empty filenames
  89. int len=0;
  90. if (0==segment_number && last_segment) {
  91. // only one segment, do not append anything...
  92. } else if (last_segment) {
  93. OPC_ASSERT(segment_number>0); // we have a last segment
  94. if (NULL==buf || (buf_ofs<buf_len && (len=snprintf(buf+buf_ofs, buf_len-buf_ofs, "/[%i].last.piece", segment_number))>14)) {
  95. buf_ofs+=len;
  96. buf_len-=len;
  97. }
  98. } else {
  99. OPC_ASSERT(!last_segment); // we have a segment with more to come...
  100. if (NULL==buf || (buf_ofs<buf_len && (len=snprintf(buf+buf_ofs, buf_len-buf_ofs, "/[%i].piece", segment_number))>9)) {
  101. buf_ofs+=len;
  102. buf_len-=len;
  103. }
  104. }
  105. }
  106. return buf_ofs;
  107. }
  108. opc_uint16_t opcHelperAssembleSegmentName(char *out, opc_uint16_t out_size, const xmlChar *name, opc_uint32_t segment_number, opc_uint32_t next_segment_id, opc_bool_t rels_segment, opc_uint16_t *out_max) {
  109. opc_uint16_t out_length=opcHelperEncodeFilename(name, out, out_size, rels_segment);
  110. opc_uint16_t const _out_max=out_length+24; // file+|/[4294967296].last.piece|=file+24
  111. // _rels/file.rels=|_rels/|+|.rels|=6+5=11
  112. out_length=opcHelperFilenameAppendPiece(segment_number, next_segment_id, out, out_length, out_size);
  113. OPC_ASSERT(out_length<=out_size);
  114. OPC_ASSERT(out_length<=_out_max);
  115. if (NULL!=out_max) *out_max=_out_max;
  116. return out_length;
  117. }
  118. opc_error_t opcHelperSplitFilename(opc_uint8_t *filename, opc_uint32_t filename_length, opc_uint32_t *segment_number, opc_bool_t *last_segment, opc_bool_t *rel_segment) {
  119. opc_error_t ret=OPC_ERROR_STREAM;
  120. if (NULL!=segment_number) *segment_number=0;
  121. if (NULL!=last_segment) *last_segment=OPC_TRUE;
  122. if (NULL!=rel_segment) *rel_segment=OPC_FALSE;
  123. if (filename_length>7 // "].piece" suffix
  124. && filename[filename_length-7]==']'
  125. && filename[filename_length-6]=='.'
  126. && filename[filename_length-5]=='p'
  127. && filename[filename_length-4]=='i'
  128. && filename[filename_length-3]=='e'
  129. && filename[filename_length-2]=='c'
  130. && filename[filename_length-1]=='e') {
  131. opc_uint32_t i=filename_length-7;
  132. filename[i--]='\0';
  133. while(i>0 && filename[i]>='0' && filename[i]<='9') {
  134. i--;
  135. }
  136. if (i>2 && filename[i-2]=='/' && filename[i-1]=='[' && '\0'!=filename[i]) {
  137. if (NULL!=segment_number) *segment_number=atoi((char*)(filename+i));
  138. if (NULL!=last_segment) *last_segment=OPC_FALSE;
  139. filename[i-2]='\0';
  140. ret=OPC_ERROR_NONE;
  141. }
  142. } else if (filename_length>12 // "].last.piece" suffix
  143. && filename[filename_length-12]==']'
  144. && filename[filename_length-11]=='.'
  145. && filename[filename_length-10]=='l'
  146. && filename[filename_length-9]=='a'
  147. && filename[filename_length-8]=='s'
  148. && filename[filename_length-7]=='t'
  149. && filename[filename_length-6]=='.'
  150. && filename[filename_length-5]=='p'
  151. && filename[filename_length-4]=='i'
  152. && filename[filename_length-3]=='e'
  153. && filename[filename_length-2]=='c'
  154. && filename[filename_length-1]=='e') {
  155. opc_uint32_t i=filename_length-12;
  156. filename[i--]='\0';
  157. while(i>0 && filename[i]>='0' && filename[i]<='9') {
  158. i--;
  159. }
  160. if (i>2 && filename[i-2]=='/' && filename[i-1]=='[' && '\0'!=filename[i]) {
  161. if (NULL!=segment_number) *segment_number=atoi((char*)(filename+i));
  162. if (NULL!=last_segment) *last_segment=OPC_TRUE;
  163. filename[i-2]='\0';
  164. ret=OPC_ERROR_NONE;
  165. }
  166. } else if (filename_length>5 // ".rels" suffix
  167. && filename[filename_length-5]=='.'
  168. && filename[filename_length-4]=='r'
  169. && filename[filename_length-3]=='e'
  170. && filename[filename_length-2]=='l'
  171. && filename[filename_length-1]=='s') {
  172. opc_uint32_t i=filename_length-5;
  173. while(i>0 && filename[i-1]!='/') i--;
  174. //"_rels/"
  175. if (i>=6
  176. && filename[i-6]=='_'
  177. && filename[i-5]=='r'
  178. && filename[i-4]=='e'
  179. && filename[i-3]=='l'
  180. && filename[i-2]=='s'
  181. && filename[i-1]=='/') {
  182. opc_uint32_t j=i;
  183. for(;j<filename_length-5;j++) {
  184. filename[j-6]=filename[j];
  185. }
  186. filename[j-6]=0;
  187. if (NULL!=rel_segment) *rel_segment=OPC_TRUE;
  188. }
  189. ret=OPC_ERROR_NONE;
  190. } else {
  191. ret=OPC_ERROR_NONE;
  192. }
  193. return ret;
  194. }