getpart.pm 8.1 KB

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