getpart.pm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. #use strict;
  25. my @xml;
  26. my $xmlfile;
  27. my $warning=0;
  28. my $trace=0;
  29. use MIME::Base64;
  30. sub decode_hex {
  31. my $s = $_;
  32. # remove everything not hex
  33. $s =~ s/[^A-Fa-f0-9]//g;
  34. # encode everything
  35. $s =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;
  36. return $s;
  37. }
  38. sub getpartattr {
  39. # if $part is undefined (ie only one argument) then
  40. # return the attributes of the section
  41. my ($section, $part)=@_;
  42. my %hash;
  43. my $inside=0;
  44. # print "Section: $section, part: $part\n";
  45. for(@xml) {
  46. # print "$inside: $_";
  47. if(!$inside && ($_ =~ /^ *\<$section/)) {
  48. $inside++;
  49. }
  50. if((1 ==$inside) && ( ($_ =~ /^ *\<$part ([^>]*)/) ||
  51. !(defined($part)) )
  52. ) {
  53. $inside++;
  54. my $attr=$1;
  55. while($attr =~ s/ *([^=]*)= *(\"([^\"]*)\"|([^\> ]*))//) {
  56. my ($var, $cont)=($1, $2);
  57. $cont =~ s/^\"(.*)\"$/$1/;
  58. $hash{$var}=$cont;
  59. }
  60. last;
  61. }
  62. # detect end of section when part wasn't found
  63. elsif((1 ==$inside) && ($_ =~ /^ *\<\/$section\>/)) {
  64. last;
  65. }
  66. elsif((2 ==$inside) && ($_ =~ /^ *\<\/$part/)) {
  67. $inside--;
  68. }
  69. }
  70. return %hash;
  71. }
  72. sub getpart {
  73. my ($section, $part)=@_;
  74. my @this;
  75. my $inside=0;
  76. my $base64=0;
  77. my $hex=0;
  78. my $line;
  79. for(@xml) {
  80. $line++;
  81. if(!$inside && ($_ =~ /^ *\<$section/)) {
  82. $inside++;
  83. }
  84. elsif(($inside >= 1) && ($_ =~ /^ *\<$part[ \>]/)) {
  85. if($inside > 1) {
  86. push @this, $_;
  87. }
  88. elsif($_ =~ /$part [^>]*base64=/) {
  89. # attempt to detect our base64 encoded part
  90. $base64=1;
  91. }
  92. elsif($_ =~ /$part [^>]*hex=/) {
  93. # attempt to detect a hex-encoded part
  94. $hex=1;
  95. }
  96. $inside++;
  97. }
  98. elsif(($inside >= 2) && ($_ =~ /^ *\<\/$part[ \>]/)) {
  99. if($inside > 2) {
  100. push @this, $_;
  101. }
  102. $inside--;
  103. }
  104. elsif(($inside >= 1) && ($_ =~ /^ *\<\/$section/)) {
  105. if($inside > 1) {
  106. print STDERR "$xmlfile:$line:1: error: missing </$part> tag before </$section>\n";
  107. @this = ("format error in $xmlfile");
  108. }
  109. if($trace && @this) {
  110. print STDERR "*** getpart.pm: $section/$part returned data!\n";
  111. }
  112. if($warning && !@this) {
  113. print STDERR "*** getpart.pm: $section/$part returned empty!\n";
  114. }
  115. if($base64) {
  116. # decode the whole array before returning it!
  117. for(@this) {
  118. my $decoded = decode_base64($_);
  119. $_ = $decoded;
  120. }
  121. }
  122. elsif($hex) {
  123. # decode the whole array before returning it!
  124. for(@this) {
  125. my $decoded = decode_hex($_);
  126. $_ = $decoded;
  127. }
  128. }
  129. return @this;
  130. }
  131. elsif($inside >= 2) {
  132. push @this, $_;
  133. }
  134. }
  135. if($trace && @this) {
  136. # section/part has data but end of section not detected,
  137. # end of file implies end of section.
  138. print STDERR "*** getpart.pm: $section/$part returned data!\n";
  139. }
  140. if($warning && !@this) {
  141. # section/part does not exist or has no data without an end of
  142. # section; end of file implies end of section.
  143. print STDERR "*** getpart.pm: $section/$part returned empty!\n";
  144. }
  145. return @this;
  146. }
  147. sub partexists {
  148. my ($section, $part)=@_;
  149. my $inside = 0;
  150. for(@xml) {
  151. if(!$inside && ($_ =~ /^ *\<$section/)) {
  152. $inside++;
  153. }
  154. elsif((1 == $inside) && ($_ =~ /^ *\<$part[ \>]/)) {
  155. return 1; # exists
  156. }
  157. elsif((1 == $inside) && ($_ =~ /^ *\<\/$section/)) {
  158. return 0; # does not exist
  159. }
  160. }
  161. return 0; # does not exist
  162. }
  163. # Return entire document as list of lines
  164. sub getall {
  165. return @xml;
  166. }
  167. sub loadtest {
  168. my ($file)=@_;
  169. undef @xml;
  170. $xmlfile = $file;
  171. if(open(XML, "<$file")) {
  172. binmode XML; # for crapage systems, use binary
  173. while(<XML>) {
  174. push @xml, $_;
  175. }
  176. close(XML);
  177. }
  178. else {
  179. # failure
  180. if($warning) {
  181. print STDERR "file $file wouldn't open!\n";
  182. }
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. sub fulltest {
  188. return @xml;
  189. }
  190. # write the test to the given file
  191. sub savetest {
  192. my ($file)=@_;
  193. if(open(XML, ">$file")) {
  194. binmode XML; # for crapage systems, use binary
  195. for(@xml) {
  196. print XML $_;
  197. }
  198. close(XML);
  199. }
  200. else {
  201. # failure
  202. if($warning) {
  203. print STDERR "file $file wouldn't open!\n";
  204. }
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. #
  210. # Strip off all lines that match the specified pattern and return
  211. # the new array.
  212. #
  213. sub striparray {
  214. my ($pattern, $arrayref) = @_;
  215. my @array;
  216. for(@$arrayref) {
  217. if($_ !~ /$pattern/) {
  218. push @array, $_;
  219. }
  220. }
  221. return @array;
  222. }
  223. #
  224. # pass array *REFERENCES* !
  225. #
  226. sub compareparts {
  227. my ($firstref, $secondref)=@_;
  228. my $first = join("", @$firstref);
  229. my $second = join("", @$secondref);
  230. # we cannot compare arrays index per index since with the base64 chunks,
  231. # they may not be "evenly" distributed
  232. # NOTE: this no longer strips off carriage returns from the arrays. Is that
  233. # really necessary? It ruins the testing of newlines. I believe it was once
  234. # added to enable tests on win32.
  235. if($first ne $second) {
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. #
  241. # Write a given array to the specified file
  242. #
  243. sub writearray {
  244. my ($filename, $arrayref)=@_;
  245. open(TEMP, ">$filename");
  246. binmode(TEMP,":raw"); # cygwin fix by Kevin Roth
  247. for(@$arrayref) {
  248. print TEMP $_;
  249. }
  250. close(TEMP);
  251. }
  252. #
  253. # Load a specified file and return it as an array
  254. #
  255. sub loadarray {
  256. my ($filename)=@_;
  257. my @array;
  258. open(TEMP, "<$filename");
  259. while(<TEMP>) {
  260. push @array, $_;
  261. }
  262. close(TEMP);
  263. return @array;
  264. }
  265. # Given two array references, this function will store them in two temporary
  266. # files, run 'diff' on them, store the result and return the diff output!
  267. sub showdiff {
  268. my ($logdir, $firstref, $secondref)=@_;
  269. my $file1="$logdir/check-generated";
  270. my $file2="$logdir/check-expected";
  271. open(TEMP, ">$file1");
  272. for(@$firstref) {
  273. my $l = $_;
  274. $l =~ s/\r/[CR]/g;
  275. $l =~ s/\n/[LF]/g;
  276. $l =~ s/([^\x20-\x7f])/sprintf "%%%02x", ord $1/eg;
  277. print TEMP $l;
  278. print TEMP "\n";
  279. }
  280. close(TEMP);
  281. open(TEMP, ">$file2");
  282. for(@$secondref) {
  283. my $l = $_;
  284. $l =~ s/\r/[CR]/g;
  285. $l =~ s/\n/[LF]/g;
  286. $l =~ s/([^\x20-\x7f])/sprintf "%%%02x", ord $1/eg;
  287. print TEMP $l;
  288. print TEMP "\n";
  289. }
  290. close(TEMP);
  291. my @out = `diff -u $file2 $file1 2>/dev/null`;
  292. if(!$out[0]) {
  293. @out = `diff -c $file2 $file1 2>/dev/null`;
  294. }
  295. return @out;
  296. }
  297. 1;