getpart.pm 8.3 KB

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