You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

17002 lines
490 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. stdsoap2.c[pp] 2.8.17r
  3. gSOAP runtime engine
  4. gSOAP XML Web services tools
  5. Copyright (C) 2000-2013, Robert van Engelen, Genivia Inc., All Rights Reserved.
  6. This part of the software is released under ONE of the following licenses:
  7. GPL, or the gSOAP public license, or Genivia's license for commercial use.
  8. --------------------------------------------------------------------------------
  9. Contributors:
  10. Wind River Systems Inc., for the following additions under gSOAP public license:
  11. - vxWorks compatible options
  12. --------------------------------------------------------------------------------
  13. gSOAP public license.
  14. The contents of this file are subject to the gSOAP Public License Version 1.3
  15. (the "License"); you may not use this file except in compliance with the
  16. License. You may obtain a copy of the License at
  17. http://www.cs.fsu.edu/~engelen/soaplicense.html
  18. Software distributed under the License is distributed on an "AS IS" basis,
  19. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  20. for the specific language governing rights and limitations under the License.
  21. The Initial Developer of the Original Code is Robert A. van Engelen.
  22. Copyright (C) 2000-2013, Robert van Engelen, Genivia Inc., All Rights Reserved.
  23. --------------------------------------------------------------------------------
  24. GPL license.
  25. This program is free software; you can redistribute it and/or modify it under
  26. the terms of the GNU General Public License as published by the Free Software
  27. Foundation; either version 2 of the License, or (at your option) any later
  28. version.
  29. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  30. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  31. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  32. You should have received a copy of the GNU General Public License along with
  33. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  34. Place, Suite 330, Boston, MA 02111-1307 USA
  35. Author contact information:
  36. engelen@genivia.com / engelen@acm.org
  37. This program is released under the GPL with the additional exemption that
  38. compiling, linking, and/or using OpenSSL is allowed.
  39. --------------------------------------------------------------------------------
  40. A commercial use license is available from Genivia, Inc., contact@genivia.com
  41. --------------------------------------------------------------------------------
  42. */
  43. #define GSOAP_LIB_VERSION 20817
  44. #ifdef AS400
  45. # pragma convert(819) /* EBCDIC to ASCII */
  46. #endif
  47. #include "stdsoap2.h"
  48. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  49. #include <ipcom_key_db.h>
  50. #endif
  51. #if GSOAP_VERSION != GSOAP_LIB_VERSION
  52. # error "GSOAP VERSION MISMATCH IN LIBRARY: PLEASE REINSTALL PACKAGE"
  53. #endif
  54. #ifdef __BORLANDC__
  55. # pragma warn -8060
  56. #else
  57. # ifdef WIN32
  58. # ifdef UNDER_CE
  59. # pragma comment(lib, "ws2.lib") /* WinCE */
  60. # else
  61. # pragma comment(lib, "ws2_32.lib")
  62. # endif
  63. # pragma warning(disable : 4996) /* disable deprecation warnings */
  64. # endif
  65. #endif
  66. #ifdef __cplusplus
  67. SOAP_SOURCE_STAMP("@(#) stdsoap2.cpp ver 2.8.17r 2013-12-18 00:00:00 GMT")
  68. extern "C" {
  69. #else
  70. SOAP_SOURCE_STAMP("@(#) stdsoap2.c ver 2.8.17r 2013-12-18 00:00:00 GMT")
  71. #endif
  72. /* 8bit character representing unknown/nonrepresentable character data (e.g. not supported by current locale with multibyte support enabled) */
  73. #ifndef SOAP_UNKNOWN_CHAR
  74. #define SOAP_UNKNOWN_CHAR (127)
  75. #endif
  76. /* EOF=-1 */
  77. #define SOAP_LT (soap_wchar)(-2) /* XML-specific '<' */
  78. #define SOAP_TT (soap_wchar)(-3) /* XML-specific '</' */
  79. #define SOAP_GT (soap_wchar)(-4) /* XML-specific '>' */
  80. #define SOAP_QT (soap_wchar)(-5) /* XML-specific '"' */
  81. #define SOAP_AP (soap_wchar)(-6) /* XML-specific ''' */
  82. #define soap_blank(c) ((c)+1 > 0 && (c) <= 32)
  83. #define soap_notblank(c) ((c) > 32)
  84. #if defined(WIN32) && !defined(UNDER_CE)
  85. #define soap_hash_ptr(p) ((PtrToUlong(p) >> 3) & (SOAP_PTRHASH - 1))
  86. #else
  87. #define soap_hash_ptr(p) ((size_t)(((unsigned long)(p) >> 3) & (SOAP_PTRHASH-1)))
  88. #endif
  89. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  90. static void soap_init_logs(struct soap*);
  91. #endif
  92. #ifdef SOAP_DEBUG
  93. static void soap_close_logfile(struct soap*, int);
  94. static void soap_set_logfile(struct soap*, int, const char*);
  95. #endif
  96. #ifdef SOAP_MEM_DEBUG
  97. static void soap_init_mht(struct soap*);
  98. static void soap_free_mht(struct soap*);
  99. static void soap_track_unlink(struct soap*, const void*);
  100. #endif
  101. #ifndef PALM_2
  102. static int soap_set_error(struct soap*, const char*, const char*, const char*, const char*, int);
  103. static int soap_copy_fault(struct soap*, const char*, const char*, const char*, const char*);
  104. static int soap_getattrval(struct soap*, char*, size_t, soap_wchar);
  105. #endif
  106. #ifndef PALM_1
  107. static void soap_free_ns(struct soap *soap);
  108. static soap_wchar soap_char(struct soap*);
  109. static soap_wchar soap_get_pi(struct soap*);
  110. static int soap_isxdigit(int);
  111. static void *fplugin(struct soap*, const char*);
  112. static size_t soap_count_attachments(struct soap *soap);
  113. static int soap_try_connect_command(struct soap*, int http_command, const char *endpoint, const char *action);
  114. #ifdef WITH_NTLM
  115. static int soap_ntlm_handshake(struct soap *soap, int command, const char *endpoint, const char *host, int port);
  116. #endif
  117. #ifndef WITH_NOIDREF
  118. static int soap_has_copies(struct soap*, const char*, const char*);
  119. static void soap_init_iht(struct soap*);
  120. static void soap_free_iht(struct soap*);
  121. static void soap_init_pht(struct soap*);
  122. static void soap_free_pht(struct soap*);
  123. #endif
  124. #endif
  125. #ifndef WITH_LEAN
  126. static const char *soap_set_validation_fault(struct soap*, const char*, const char*);
  127. static int soap_isnumeric(struct soap*, const char*);
  128. static struct soap_nlist *soap_push_ns(struct soap *soap, const char *id, const char *ns, short utilized);
  129. static void soap_utilize_ns(struct soap *soap, const char *tag);
  130. #endif
  131. #ifndef WITH_LEANER
  132. #ifndef PALM_1
  133. static struct soap_multipart *soap_new_multipart(struct soap*, struct soap_multipart**, struct soap_multipart**, char*, size_t);
  134. static int soap_putdimefield(struct soap*, const char*, size_t);
  135. static char *soap_getdimefield(struct soap*, size_t);
  136. static void soap_select_mime_boundary(struct soap*);
  137. static int soap_valid_mime_boundary(struct soap*);
  138. static void soap_resolve_attachment(struct soap*, struct soap_multipart*);
  139. #endif
  140. #endif
  141. #ifdef WITH_GZIP
  142. static int soap_getgziphdr(struct soap*);
  143. #endif
  144. #ifdef WITH_OPENSSL
  145. # ifndef SOAP_SSL_RSA_BITS
  146. # define SOAP_SSL_RSA_BITS 2048
  147. # endif
  148. static int soap_ssl_init_done = 0;
  149. static int ssl_auth_init(struct soap*);
  150. static int ssl_verify_callback(int, X509_STORE_CTX*);
  151. static int ssl_verify_callback_allow_expired_certificate(int, X509_STORE_CTX*);
  152. static int ssl_password(char*, int, int, void *);
  153. #endif
  154. #ifdef WITH_GNUTLS
  155. # ifndef SOAP_SSL_RSA_BITS
  156. # define SOAP_SSL_RSA_BITS 2048
  157. # endif
  158. static int soap_ssl_init_done = 0;
  159. static const char *ssl_verify(struct soap *soap, const char *host);
  160. # if defined(HAVE_PTHREAD_H)
  161. # include <pthread.h>
  162. /* make GNUTLS thread safe with pthreads */
  163. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  164. # elif defined(HAVE_PTH_H)
  165. #include <pth.h>
  166. /* make GNUTLS thread safe with PTH */
  167. GCRY_THREAD_OPTION_PTH_IMPL;
  168. # endif
  169. #endif
  170. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  171. #ifndef PALM_1
  172. static const char *soap_decode(char*, size_t, const char*, const char*);
  173. #endif
  174. #endif
  175. #ifndef WITH_NOHTTP
  176. #ifndef PALM_1
  177. static soap_wchar soap_getchunkchar(struct soap*);
  178. static const char *http_error(struct soap*, int);
  179. static int http_get(struct soap*);
  180. static int http_405(struct soap*);
  181. static int http_200(struct soap*);
  182. static int http_post(struct soap*, const char*, const char*, int, const char*, const char*, size_t);
  183. static int http_send_header(struct soap*, const char*);
  184. static int http_post_header(struct soap*, const char*, const char*);
  185. static int http_response(struct soap*, int, size_t);
  186. static int http_parse(struct soap*);
  187. static int http_parse_header(struct soap*, const char*, const char*);
  188. #endif
  189. #endif
  190. #ifndef WITH_NOIO
  191. #ifndef PALM_1
  192. static int fsend(struct soap*, const char*, size_t);
  193. static size_t frecv(struct soap*, char*, size_t);
  194. static int tcp_init(struct soap*);
  195. static const char *tcp_error(struct soap*);
  196. #ifndef WITH_IPV6
  197. static int tcp_gethost(struct soap*, const char *addr, struct in_addr *inaddr);
  198. #endif
  199. static SOAP_SOCKET tcp_connect(struct soap*, const char *endpoint, const char *host, int port);
  200. static SOAP_SOCKET tcp_accept(struct soap*, SOAP_SOCKET, struct sockaddr*, int*);
  201. static int tcp_select(struct soap*, SOAP_SOCKET, int, int);
  202. static int tcp_disconnect(struct soap*);
  203. static int tcp_closesocket(struct soap*, SOAP_SOCKET);
  204. static int tcp_shutdownsocket(struct soap*, SOAP_SOCKET, int);
  205. static const char *soap_strerror(struct soap*);
  206. #endif
  207. #define SOAP_TCP_SELECT_RCV 0x1
  208. #define SOAP_TCP_SELECT_SND 0x2
  209. #define SOAP_TCP_SELECT_ERR 0x4
  210. #define SOAP_TCP_SELECT_ALL 0x7
  211. #if defined(WIN32)
  212. #define SOAP_SOCKBLOCK(fd) \
  213. { u_long blocking = 0; \
  214. ioctlsocket(fd, FIONBIO, &blocking); \
  215. }
  216. #define SOAP_SOCKNONBLOCK(fd) \
  217. { u_long nonblocking = 1; \
  218. ioctlsocket(fd, FIONBIO, &nonblocking); \
  219. }
  220. #elif defined(VXWORKS)
  221. #define SOAP_SOCKBLOCK(fd) \
  222. { u_long blocking = 0; \
  223. ioctl(fd, FIONBIO, (int)(&blocking)); \
  224. }
  225. #define SOAP_SOCKNONBLOCK(fd) \
  226. { u_long nonblocking = 1; \
  227. ioctl(fd, FIONBIO, (int)(&nonblocking)); \
  228. }
  229. #elif defined(__VMS)
  230. #define SOAP_SOCKBLOCK(fd) \
  231. { int blocking = 0; \
  232. ioctl(fd, FIONBIO, &blocking); \
  233. }
  234. #define SOAP_SOCKNONBLOCK(fd) \
  235. { int nonblocking = 1; \
  236. ioctl(fd, FIONBIO, &nonblocking); \
  237. }
  238. #elif defined(PALM)
  239. #define SOAP_SOCKBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0)&~O_NONBLOCK);
  240. #define SOAP_SOCKNONBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0)|O_NONBLOCK);
  241. #elif defined(SYMBIAN)
  242. #define SOAP_SOCKBLOCK(fd) \
  243. { long blocking = 0; \
  244. ioctl(fd, 0/*FIONBIO*/, &blocking); \
  245. }
  246. #define SOAP_SOCKNONBLOCK(fd) \
  247. { long nonblocking = 1; \
  248. ioctl(fd, 0/*FIONBIO*/, &nonblocking); \
  249. }
  250. #else
  251. #define SOAP_SOCKBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)&~O_NONBLOCK);
  252. #define SOAP_SOCKNONBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
  253. #endif
  254. #endif
  255. #if defined(PALM) && !defined(PALM_2)
  256. unsigned short errno;
  257. #endif
  258. #ifndef PALM_1
  259. static const char soap_env1[42] = "http://schemas.xmlsoap.org/soap/envelope/";
  260. static const char soap_enc1[42] = "http://schemas.xmlsoap.org/soap/encoding/";
  261. static const char soap_env2[40] = "http://www.w3.org/2003/05/soap-envelope";
  262. static const char soap_enc2[40] = "http://www.w3.org/2003/05/soap-encoding";
  263. static const char soap_rpc[35] = "http://www.w3.org/2003/05/soap-rpc";
  264. #endif
  265. #ifndef PALM_1
  266. const union soap_double_nan soap_double_nan = {{0xFFFFFFFF, 0xFFFFFFFF}};
  267. const char soap_base64o[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  268. const char soap_base64i[81] = "\76XXX\77\64\65\66\67\70\71\72\73\74\75XXXXXXX\00\01\02\03\04\05\06\07\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31XXXXXX\32\33\34\35\36\37\40\41\42\43\44\45\46\47\50\51\52\53\54\55\56\57\60\61\62\63";
  269. #endif
  270. #ifndef WITH_LEAN
  271. static const char soap_indent[11] = "\n\t\t\t\t\t\t\t\t\t";
  272. /* Alternative indentation form for SOAP_XML_INDENT:
  273. static const char soap_indent[21] = "\n ";
  274. */
  275. #endif
  276. #ifndef SOAP_CANARY
  277. # define SOAP_CANARY (0xC0DE)
  278. #endif
  279. static const char soap_padding[4] = "\0\0\0";
  280. #define SOAP_STR_PADDING (soap_padding)
  281. #define SOAP_STR_EOS (soap_padding)
  282. #define SOAP_NON_NULL (soap_padding)
  283. #ifndef WITH_LEAN
  284. static const struct soap_code_map html_entity_codes[] = /* entities for XHTML parsing */
  285. { { 160, "nbsp" },
  286. { 161, "iexcl" },
  287. { 162, "cent" },
  288. { 163, "pound" },
  289. { 164, "curren" },
  290. { 165, "yen" },
  291. { 166, "brvbar" },
  292. { 167, "sect" },
  293. { 168, "uml" },
  294. { 169, "copy" },
  295. { 170, "ordf" },
  296. { 171, "laquo" },
  297. { 172, "not" },
  298. { 173, "shy" },
  299. { 174, "reg" },
  300. { 175, "macr" },
  301. { 176, "deg" },
  302. { 177, "plusmn" },
  303. { 178, "sup2" },
  304. { 179, "sup3" },
  305. { 180, "acute" },
  306. { 181, "micro" },
  307. { 182, "para" },
  308. { 183, "middot" },
  309. { 184, "cedil" },
  310. { 185, "sup1" },
  311. { 186, "ordm" },
  312. { 187, "raquo" },
  313. { 188, "frac14" },
  314. { 189, "frac12" },
  315. { 190, "frac34" },
  316. { 191, "iquest" },
  317. { 192, "Agrave" },
  318. { 193, "Aacute" },
  319. { 194, "Acirc" },
  320. { 195, "Atilde" },
  321. { 196, "Auml" },
  322. { 197, "Aring" },
  323. { 198, "AElig" },
  324. { 199, "Ccedil" },
  325. { 200, "Egrave" },
  326. { 201, "Eacute" },
  327. { 202, "Ecirc" },
  328. { 203, "Euml" },
  329. { 204, "Igrave" },
  330. { 205, "Iacute" },
  331. { 206, "Icirc" },
  332. { 207, "Iuml" },
  333. { 208, "ETH" },
  334. { 209, "Ntilde" },
  335. { 210, "Ograve" },
  336. { 211, "Oacute" },
  337. { 212, "Ocirc" },
  338. { 213, "Otilde" },
  339. { 214, "Ouml" },
  340. { 215, "times" },
  341. { 216, "Oslash" },
  342. { 217, "Ugrave" },
  343. { 218, "Uacute" },
  344. { 219, "Ucirc" },
  345. { 220, "Uuml" },
  346. { 221, "Yacute" },
  347. { 222, "THORN" },
  348. { 223, "szlig" },
  349. { 224, "agrave" },
  350. { 225, "aacute" },
  351. { 226, "acirc" },
  352. { 227, "atilde" },
  353. { 228, "auml" },
  354. { 229, "aring" },
  355. { 230, "aelig" },
  356. { 231, "ccedil" },
  357. { 232, "egrave" },
  358. { 233, "eacute" },
  359. { 234, "ecirc" },
  360. { 235, "euml" },
  361. { 236, "igrave" },
  362. { 237, "iacute" },
  363. { 238, "icirc" },
  364. { 239, "iuml" },
  365. { 240, "eth" },
  366. { 241, "ntilde" },
  367. { 242, "ograve" },
  368. { 243, "oacute" },
  369. { 244, "ocirc" },
  370. { 245, "otilde" },
  371. { 246, "ouml" },
  372. { 247, "divide" },
  373. { 248, "oslash" },
  374. { 249, "ugrave" },
  375. { 250, "uacute" },
  376. { 251, "ucirc" },
  377. { 252, "uuml" },
  378. { 253, "yacute" },
  379. { 254, "thorn" },
  380. { 255, "yuml" },
  381. { 0, NULL }
  382. };
  383. #endif
  384. #ifndef WITH_NOIO
  385. #ifndef WITH_LEAN
  386. static const struct soap_code_map h_error_codes[] =
  387. {
  388. #ifdef HOST_NOT_FOUND
  389. { HOST_NOT_FOUND, "Host not found" },
  390. #endif
  391. #ifdef TRY_AGAIN
  392. { TRY_AGAIN, "Try Again" },
  393. #endif
  394. #ifdef NO_RECOVERY
  395. { NO_RECOVERY, "No Recovery" },
  396. #endif
  397. #ifdef NO_DATA
  398. { NO_DATA, "No Data" },
  399. #endif
  400. #ifdef NO_ADDRESS
  401. { NO_ADDRESS, "No Address" },
  402. #endif
  403. { 0, NULL }
  404. };
  405. #endif
  406. #endif
  407. #ifndef WITH_NOHTTP
  408. #ifndef WITH_LEAN
  409. static const struct soap_code_map h_http_error_codes[] =
  410. { { 200, "OK" },
  411. { 201, "Created" },
  412. { 202, "Accepted" },
  413. { 203, "Non-Authoritative Information" },
  414. { 204, "No Content" },
  415. { 205, "Reset Content" },
  416. { 206, "Partial Content" },
  417. { 300, "Multiple Choices" },
  418. { 301, "Moved Permanently" },
  419. { 302, "Found" },
  420. { 303, "See Other" },
  421. { 304, "Not Modified" },
  422. { 305, "Use Proxy" },
  423. { 307, "Temporary Redirect" },
  424. { 400, "Bad Request" },
  425. { 401, "Unauthorized" },
  426. { 402, "Payment Required" },
  427. { 403, "Forbidden" },
  428. { 404, "Not Found" },
  429. { 405, "Method Not Allowed" },
  430. { 406, "Not Acceptable" },
  431. { 407, "Proxy Authentication Required" },
  432. { 408, "Request Time-out" },
  433. { 409, "Conflict" },
  434. { 410, "Gone" },
  435. { 411, "Length Required" },
  436. { 412, "Precondition Failed" },
  437. { 413, "Request Entity Too Large" },
  438. { 414, "Request-URI Too Large" },
  439. { 415, "Unsupported Media Type" },
  440. { 416, "Requested range not satisfiable" },
  441. { 417, "Expectation Failed" },
  442. { 500, "Internal Server Error" },
  443. { 501, "Not Implemented" },
  444. { 502, "Bad Gateway" },
  445. { 503, "Service Unavailable" },
  446. { 504, "Gateway Time-out" },
  447. { 505, "HTTP Version not supported" },
  448. { 0, NULL }
  449. };
  450. #endif
  451. #endif
  452. #ifdef WITH_OPENSSL
  453. static const struct soap_code_map h_ssl_error_codes[] =
  454. {
  455. #define _SSL_ERROR(e) { e, #e }
  456. _SSL_ERROR(SSL_ERROR_SSL),
  457. _SSL_ERROR(SSL_ERROR_ZERO_RETURN),
  458. _SSL_ERROR(SSL_ERROR_WANT_READ),
  459. _SSL_ERROR(SSL_ERROR_WANT_WRITE),
  460. _SSL_ERROR(SSL_ERROR_WANT_CONNECT),
  461. _SSL_ERROR(SSL_ERROR_WANT_X509_LOOKUP),
  462. _SSL_ERROR(SSL_ERROR_SYSCALL),
  463. { 0, NULL }
  464. };
  465. #endif
  466. #ifndef WITH_LEANER
  467. static const struct soap_code_map mime_codes[] =
  468. { { SOAP_MIME_7BIT, "7bit" },
  469. { SOAP_MIME_8BIT, "8bit" },
  470. { SOAP_MIME_BINARY, "binary" },
  471. { SOAP_MIME_QUOTED_PRINTABLE, "quoted-printable" },
  472. { SOAP_MIME_BASE64, "base64" },
  473. { SOAP_MIME_IETF_TOKEN, "ietf-token" },
  474. { SOAP_MIME_X_TOKEN, "x-token" },
  475. { 0, NULL }
  476. };
  477. #endif
  478. #ifdef WIN32
  479. static int tcp_done = 0;
  480. #endif
  481. #if defined(HP_UX) && defined(HAVE_GETHOSTBYNAME_R)
  482. extern int h_errno;
  483. #endif
  484. /******************************************************************************/
  485. #ifndef WITH_NOIO
  486. #ifndef PALM_1
  487. static int
  488. fsend(struct soap *soap, const char *s, size_t n)
  489. { register int nwritten, err;
  490. SOAP_SOCKET sk;
  491. #if defined(__cplusplus) && !defined(WITH_LEAN) && !defined(WITH_COMPAT)
  492. if (soap->os)
  493. { soap->os->write(s, (std::streamsize)n);
  494. if (soap->os->good())
  495. return SOAP_OK;
  496. soap->errnum = 0;
  497. return SOAP_EOF;
  498. }
  499. #endif
  500. sk = soap->sendsk;
  501. if (!soap_valid_socket(sk))
  502. sk = soap->socket;
  503. while (n)
  504. { if (soap_valid_socket(sk))
  505. {
  506. if (soap->send_timeout)
  507. { for (;;)
  508. { register int r;
  509. #ifdef WITH_OPENSSL
  510. if (soap->ssl)
  511. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL, soap->send_timeout);
  512. else
  513. #endif
  514. #ifdef WITH_GNUTLS
  515. if (soap->session)
  516. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL, soap->send_timeout);
  517. else
  518. #endif
  519. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->send_timeout);
  520. if (r > 0)
  521. break;
  522. if (!r)
  523. return SOAP_EOF;
  524. err = soap->errnum;
  525. if (!err)
  526. return soap->error;
  527. if (err != SOAP_EAGAIN && err != SOAP_EWOULDBLOCK)
  528. return SOAP_EOF;
  529. }
  530. }
  531. #ifdef WITH_OPENSSL
  532. if (soap->ssl)
  533. nwritten = SSL_write(soap->ssl, s, (int)n);
  534. else if (soap->bio)
  535. nwritten = BIO_write(soap->bio, s, (int)n);
  536. else
  537. #endif
  538. #ifdef WITH_GNUTLS
  539. if (soap->session)
  540. nwritten = gnutls_record_send(soap->session, s, n);
  541. else
  542. #endif
  543. #ifndef WITH_LEAN
  544. if ((soap->omode & SOAP_IO_UDP))
  545. { if (soap->peerlen)
  546. nwritten = sendto(sk, (char*)s, (SOAP_WINSOCKINT)n, soap->socket_flags, (struct sockaddr*)&soap->peer, (SOAP_WINSOCKINT)soap->peerlen);
  547. else
  548. nwritten = send(sk, s, (SOAP_WINSOCKINT)n, soap->socket_flags);
  549. /* retry and back-off algorithm */
  550. /* TODO: this is not very clear from specs so verify and limit conditions under which we should loop (e.g. ENOBUFS) */
  551. if (nwritten < 0)
  552. { int udp_repeat;
  553. int udp_delay;
  554. if ((soap->connect_flags & SO_BROADCAST))
  555. udp_repeat = 2; /* SOAP-over-UDP MULTICAST_UDP_REPEAT - 1 */
  556. else
  557. udp_repeat = 1; /* SOAP-over-UDP UNICAST_UDP_REPEAT - 1 */
  558. udp_delay = ((unsigned int)soap_random % 201) + 50; /* UDP_MIN_DELAY .. UDP_MAX_DELAY */
  559. do
  560. { tcp_select(soap, sk, SOAP_TCP_SELECT_ERR, -1000 * udp_delay);
  561. if (soap->peerlen)
  562. nwritten = sendto(sk, (char*)s, (SOAP_WINSOCKINT)n, soap->socket_flags, (struct sockaddr*)&soap->peer, (SOAP_WINSOCKINT)soap->peerlen);
  563. else
  564. nwritten = send(sk, s, (SOAP_WINSOCKINT)n, soap->socket_flags);
  565. udp_delay <<= 1;
  566. if (udp_delay > 500) /* UDP_UPPER_DELAY */
  567. udp_delay = 500;
  568. }
  569. while (nwritten < 0 && --udp_repeat > 0);
  570. }
  571. if (nwritten < 0)
  572. { err = soap_socket_errno(sk);
  573. if (err && err != SOAP_EINTR)
  574. { soap->errnum = err;
  575. return SOAP_EOF;
  576. }
  577. nwritten = 0; /* and call write() again */
  578. }
  579. }
  580. else
  581. #endif
  582. #if !defined(PALM) && !defined(AS400)
  583. nwritten = send(sk, s, (int)n, soap->socket_flags);
  584. #else
  585. nwritten = send(sk, (void*)s, n, soap->socket_flags);
  586. #endif
  587. if (nwritten <= 0)
  588. {
  589. register int r = 0;
  590. err = soap_socket_errno(sk);
  591. #ifdef WITH_OPENSSL
  592. if (soap->ssl && (r = SSL_get_error(soap->ssl, nwritten)) != SSL_ERROR_NONE && r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
  593. { soap->errnum = err;
  594. return SOAP_EOF;
  595. }
  596. #endif
  597. #ifdef WITH_GNUTLS
  598. if (soap->session)
  599. { if (nwritten == GNUTLS_E_INTERRUPTED)
  600. err = SOAP_EINTR;
  601. else if (nwritten == GNUTLS_E_AGAIN)
  602. err = SOAP_EAGAIN;
  603. }
  604. #endif
  605. if (err == SOAP_EWOULDBLOCK || err == SOAP_EAGAIN)
  606. {
  607. #if defined(WITH_OPENSSL)
  608. if (soap->ssl && r == SSL_ERROR_WANT_READ)
  609. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
  610. else
  611. #elif defined(WITH_GNUTLS)
  612. if (soap->session && !gnutls_record_get_direction(soap->session))
  613. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
  614. else
  615. #endif
  616. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
  617. if (!r && soap->send_timeout)
  618. return SOAP_EOF;
  619. if (r < 0)
  620. return SOAP_EOF;
  621. }
  622. else if (err && err != SOAP_EINTR)
  623. { soap->errnum = err;
  624. return SOAP_EOF;
  625. }
  626. nwritten = 0; /* and call write() again */
  627. }
  628. }
  629. else
  630. {
  631. #ifdef WITH_FASTCGI
  632. nwritten = fwrite((void*)s, 1, n, stdout);
  633. fflush(stdout);
  634. #else
  635. #ifdef UNDER_CE
  636. nwritten = fwrite(s, 1, n, soap->sendfd);
  637. #else
  638. #ifdef VXWORKS
  639. #ifdef WMW_RPM_IO
  640. if (soap->rpmreqid)
  641. nwritten = (httpBlockPut(soap->rpmreqid, (char*)s, n) == 0) ? n : -1;
  642. else
  643. #endif
  644. nwritten = fwrite(s, sizeof(char), n, fdopen(soap->sendfd, "w"));
  645. #else
  646. #ifdef WIN32
  647. nwritten = _write(soap->sendfd, s, (unsigned int)n);
  648. #else
  649. nwritten = write(soap->sendfd, s, (unsigned int)n);
  650. #endif
  651. #endif
  652. #endif
  653. #endif
  654. if (nwritten <= 0)
  655. {
  656. #ifndef WITH_FASTCGI
  657. err = soap_errno;
  658. #else
  659. err = EOF;
  660. #endif
  661. if (err && err != SOAP_EINTR && err != SOAP_EWOULDBLOCK && err != SOAP_EAGAIN)
  662. { soap->errnum = err;
  663. return SOAP_EOF;
  664. }
  665. nwritten = 0; /* and call write() again */
  666. }
  667. }
  668. n -= nwritten;
  669. s += nwritten;
  670. }
  671. return SOAP_OK;
  672. }
  673. #endif
  674. #endif
  675. /******************************************************************************/
  676. #ifndef PALM_1
  677. SOAP_FMAC1
  678. int
  679. SOAP_FMAC2
  680. soap_send_raw(struct soap *soap, const char *s, size_t n)
  681. { if (!n)
  682. return SOAP_OK;
  683. #ifndef WITH_LEANER
  684. if (soap->fpreparesend && (soap->mode & SOAP_IO) != SOAP_IO_STORE && (soap->mode & SOAP_IO_LENGTH) && (soap->error = soap->fpreparesend(soap, s, n)))
  685. return soap->error;
  686. if (soap->ffiltersend && (soap->error = soap->ffiltersend(soap, &s, &n)))
  687. return soap->error;
  688. #endif
  689. if (soap->mode & SOAP_IO_LENGTH)
  690. soap->count += n;
  691. else if (soap->mode & SOAP_IO)
  692. { register size_t i = SOAP_BUFLEN - soap->bufidx;
  693. while (n >= i)
  694. { memcpy(soap->buf + soap->bufidx, s, i);
  695. soap->bufidx = SOAP_BUFLEN;
  696. if (soap_flush(soap))
  697. return soap->error;
  698. s += i;
  699. n -= i;
  700. i = SOAP_BUFLEN;
  701. }
  702. memcpy(soap->buf + soap->bufidx, s, n);
  703. soap->bufidx += n;
  704. }
  705. else
  706. return soap_flush_raw(soap, s, n);
  707. return SOAP_OK;
  708. }
  709. #endif
  710. /******************************************************************************/
  711. #ifndef PALM_1
  712. SOAP_FMAC1
  713. int
  714. SOAP_FMAC2
  715. soap_flush(struct soap *soap)
  716. { register size_t n = soap->bufidx;
  717. if (n)
  718. {
  719. #ifndef WITH_LEANER
  720. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE)
  721. { register int r;
  722. if (soap->fpreparesend && (r = soap->fpreparesend(soap, soap->buf, n)))
  723. return soap->error = r;
  724. }
  725. #endif
  726. soap->bufidx = 0;
  727. #ifdef WITH_ZLIB
  728. if (soap->mode & SOAP_ENC_ZLIB)
  729. { soap->d_stream->next_in = (Byte*)soap->buf;
  730. soap->d_stream->avail_in = (unsigned int)n;
  731. #ifdef WITH_GZIP
  732. soap->z_crc = crc32(soap->z_crc, (Byte*)soap->buf, (unsigned int)n);
  733. #endif
  734. do
  735. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflating %u bytes\n", soap->d_stream->avail_in));
  736. if (deflate(soap->d_stream, Z_NO_FLUSH) != Z_OK)
  737. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to deflate: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  738. return soap->error = SOAP_ZLIB_ERROR;
  739. }
  740. if (!soap->d_stream->avail_out)
  741. { if (soap_flush_raw(soap, soap->z_buf, SOAP_BUFLEN))
  742. return soap->error;
  743. soap->d_stream->next_out = (Byte*)soap->z_buf;
  744. soap->d_stream->avail_out = SOAP_BUFLEN;
  745. }
  746. } while (soap->d_stream->avail_in);
  747. }
  748. else
  749. #endif
  750. return soap_flush_raw(soap, soap->buf, n);
  751. }
  752. return SOAP_OK;
  753. }
  754. #endif
  755. /******************************************************************************/
  756. #ifndef PALM_1
  757. SOAP_FMAC1
  758. int
  759. SOAP_FMAC2
  760. soap_flush_raw(struct soap *soap, const char *s, size_t n)
  761. { if ((soap->mode & SOAP_IO) == SOAP_IO_STORE)
  762. { register char *t;
  763. if (!(t = (char*)soap_push_block(soap, NULL, n)))
  764. return soap->error = SOAP_EOM;
  765. memcpy(t, s, n);
  766. return SOAP_OK;
  767. }
  768. #ifndef WITH_LEANER
  769. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  770. { char t[16];
  771. #ifdef HAVE_SNPRINTF
  772. soap_snprintf(t, sizeof(t), &"\r\n%lX\r\n"[soap->chunksize ? 0 : 2], (unsigned long)n);
  773. #else
  774. sprintf(t, &"\r\n%lX\r\n"[soap->chunksize ? 0 : 2], (unsigned long)n);
  775. #endif
  776. DBGMSG(SENT, t, strlen(t));
  777. if ((soap->error = soap->fsend(soap, t, strlen(t))))
  778. return soap->error;
  779. soap->chunksize += n;
  780. }
  781. DBGMSG(SENT, s, n);
  782. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send %u bytes to socket=%d/fd=%d\n", (unsigned int)n, soap->socket, soap->sendfd));
  783. #endif
  784. return soap->error = soap->fsend(soap, s, n);
  785. }
  786. #endif
  787. /******************************************************************************/
  788. #ifndef PALM_1
  789. SOAP_FMAC1
  790. int
  791. SOAP_FMAC2
  792. soap_send(struct soap *soap, const char *s)
  793. { if (s)
  794. return soap_send_raw(soap, s, strlen(s));
  795. return SOAP_OK;
  796. }
  797. #endif
  798. /******************************************************************************/
  799. #ifndef WITH_LEANER
  800. #ifndef PALM_1
  801. SOAP_FMAC1
  802. int
  803. SOAP_FMAC2
  804. soap_send2(struct soap *soap, const char *s1, const char *s2)
  805. { if (soap_send(soap, s1))
  806. return soap->error;
  807. return soap_send(soap, s2);
  808. }
  809. #endif
  810. #endif
  811. /******************************************************************************/
  812. #ifndef WITH_LEANER
  813. #ifndef PALM_1
  814. SOAP_FMAC1
  815. int
  816. SOAP_FMAC2
  817. soap_send3(struct soap *soap, const char *s1, const char *s2, const char *s3)
  818. { if (soap_send(soap, s1)
  819. || soap_send(soap, s2))
  820. return soap->error;
  821. return soap_send(soap, s3);
  822. }
  823. #endif
  824. #endif
  825. /******************************************************************************/
  826. #ifndef WITH_NOIO
  827. #ifndef PALM_1
  828. static size_t
  829. frecv(struct soap *soap, char *s, size_t n)
  830. { register int r;
  831. register int retries = 100; /* max 100 retries with non-blocking sockets */
  832. SOAP_SOCKET sk;
  833. soap->errnum = 0;
  834. #if defined(__cplusplus) && !defined(WITH_LEAN) && !defined(WITH_COMPAT)
  835. if (soap->is)
  836. { if (soap->is->good())
  837. return soap->is->read(s, (std::streamsize)n).gcount();
  838. return 0;
  839. }
  840. #endif
  841. sk = soap->recvsk;
  842. if (!soap_valid_socket(sk))
  843. sk = soap->socket;
  844. if (soap_valid_socket(sk))
  845. { for (;;)
  846. {
  847. #ifdef WITH_OPENSSL
  848. register int err = 0;
  849. #endif
  850. #ifdef WITH_OPENSSL
  851. if (soap->recv_timeout && !soap->ssl) /* SSL: sockets are nonblocking */
  852. #else
  853. if (soap->recv_timeout)
  854. #endif
  855. { for (;;)
  856. { r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->recv_timeout);
  857. if (r > 0)
  858. break;
  859. if (!r)
  860. return 0;
  861. r = soap->errnum;
  862. if (r != SOAP_EAGAIN && r != SOAP_EWOULDBLOCK)
  863. return 0;
  864. }
  865. }
  866. #ifdef WITH_OPENSSL
  867. if (soap->ssl)
  868. { r = SSL_read(soap->ssl, s, (int)n);
  869. if (r > 0)
  870. return (size_t)r;
  871. err = SSL_get_error(soap->ssl, r);
  872. if (err != SSL_ERROR_NONE && err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
  873. return 0;
  874. }
  875. else if (soap->bio)
  876. { r = BIO_read(soap->bio, s, (int)n);
  877. if (r > 0)
  878. return (size_t)r;
  879. return 0;
  880. }
  881. else
  882. #endif
  883. #ifdef WITH_GNUTLS
  884. if (soap->session)
  885. { r = (int)gnutls_record_recv(soap->session, s, n);
  886. if (r >= 0)
  887. return (size_t)r;
  888. }
  889. else
  890. #endif
  891. {
  892. #ifndef WITH_LEAN
  893. if ((soap->omode & SOAP_IO_UDP))
  894. { SOAP_SOCKLEN_T k = (SOAP_SOCKLEN_T)sizeof(soap->peer);
  895. memset((void*)&soap->peer, 0, sizeof(soap->peer));
  896. r = recvfrom(sk, s, (SOAP_WINSOCKINT)n, soap->socket_flags, (struct sockaddr*)&soap->peer, &k); /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h */
  897. soap->peerlen = (size_t)k;
  898. #ifndef WITH_IPV6
  899. soap->ip = ntohl(soap->peer.sin_addr.s_addr);
  900. #endif
  901. }
  902. else
  903. #endif
  904. r = recv(sk, s, (int)n, soap->socket_flags);
  905. #ifdef PALM
  906. /* CycleSyncDisplay(curStatusMsg); */
  907. #endif
  908. if (r >= 0)
  909. return (size_t)r;
  910. r = soap_socket_errno(sk);
  911. if (r != SOAP_EINTR && r != SOAP_EAGAIN && r != SOAP_EWOULDBLOCK)
  912. { soap->errnum = r;
  913. return 0;
  914. }
  915. }
  916. #if defined(WITH_OPENSSL)
  917. if (soap->ssl && err == SSL_ERROR_WANT_WRITE)
  918. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->recv_timeout ? soap->recv_timeout : 5);
  919. else
  920. #elif defined(WITH_GNUTLS)
  921. if (soap->session && gnutls_record_get_direction(soap->session))
  922. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->recv_timeout ? soap->recv_timeout : 5);
  923. else
  924. #endif
  925. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->recv_timeout ? soap->recv_timeout : 5);
  926. if (!r && soap->recv_timeout)
  927. return 0;
  928. if (r < 0)
  929. { r = soap->errnum;
  930. if (r != SOAP_EAGAIN && r != SOAP_EWOULDBLOCK)
  931. return 0;
  932. }
  933. if (retries-- <= 0)
  934. return 0;
  935. #ifdef PALM
  936. r = soap_socket_errno(sk);
  937. if (r != SOAP_EINTR && retries-- <= 0)
  938. { soap->errnum = r;
  939. return 0;
  940. }
  941. #endif
  942. }
  943. }
  944. #ifdef WITH_FASTCGI
  945. return fread(s, 1, n, stdin);
  946. #else
  947. #ifdef UNDER_CE
  948. return fread(s, 1, n, soap->recvfd);
  949. #else
  950. #ifdef WMW_RPM_IO
  951. if (soap->rpmreqid)
  952. r = httpBlockRead(soap->rpmreqid, s, n);
  953. else
  954. #endif
  955. #ifdef WIN32
  956. r = _read(soap->recvfd, s, (unsigned int)n);
  957. #else
  958. r = read(soap->recvfd, s, (unsigned int)n);
  959. #endif
  960. if (r >= 0)
  961. return (size_t)r;
  962. soap->errnum = soap_errno;
  963. return 0;
  964. #endif
  965. #endif
  966. }
  967. #endif
  968. #endif
  969. /******************************************************************************/
  970. #ifndef WITH_NOHTTP
  971. #ifndef PALM_1
  972. static soap_wchar
  973. soap_getchunkchar(struct soap *soap)
  974. { if (soap->bufidx < soap->buflen)
  975. return soap->buf[soap->bufidx++];
  976. soap->bufidx = 0;
  977. soap->buflen = soap->chunkbuflen = soap->frecv(soap, soap->buf, SOAP_BUFLEN);
  978. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Read %u bytes from socket=%d/fd=%d\n", (unsigned int)soap->buflen, soap->socket, soap->recvfd));
  979. DBGMSG(RECV, soap->buf, soap->buflen);
  980. if (soap->buflen)
  981. return soap->buf[soap->bufidx++];
  982. return EOF;
  983. }
  984. #endif
  985. #endif
  986. /******************************************************************************/
  987. #ifndef PALM_1
  988. static int
  989. soap_isxdigit(int c)
  990. { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
  991. }
  992. #endif
  993. /******************************************************************************/
  994. #ifndef PALM_1
  995. SOAP_FMAC1
  996. int
  997. SOAP_FMAC2
  998. soap_recv_raw(struct soap *soap)
  999. { register size_t ret;
  1000. #if !defined(WITH_LEANER) || defined(WITH_ZLIB)
  1001. register int r;
  1002. #endif
  1003. #ifdef WITH_ZLIB
  1004. if (soap->mode & SOAP_ENC_ZLIB)
  1005. { if (soap->d_stream->next_out == Z_NULL)
  1006. { soap->bufidx = soap->buflen = 0;
  1007. return EOF;
  1008. }
  1009. if (soap->d_stream->avail_in || !soap->d_stream->avail_out)
  1010. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflating\n"));
  1011. soap->d_stream->next_out = (Byte*)soap->buf;
  1012. soap->d_stream->avail_out = SOAP_BUFLEN;
  1013. r = inflate(soap->d_stream, Z_NO_FLUSH);
  1014. if (r == Z_NEED_DICT && soap->z_dict)
  1015. r = inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len);
  1016. if (r == Z_OK || r == Z_STREAM_END)
  1017. { soap->bufidx = 0;
  1018. ret = soap->buflen = SOAP_BUFLEN - soap->d_stream->avail_out;
  1019. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  1020. soap->z_crc = crc32(soap->z_crc, (Byte*)soap->buf, (unsigned int)ret);
  1021. if (r == Z_STREAM_END)
  1022. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflated %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  1023. soap->z_ratio_in = (float)soap->d_stream->total_in / (float)soap->d_stream->total_out;
  1024. soap->d_stream->next_out = Z_NULL;
  1025. }
  1026. if (ret)
  1027. { soap->count += ret;
  1028. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n---- decompressed ----\n"));
  1029. DBGMSG(RECV, soap->buf, ret);
  1030. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n----\n"));
  1031. #ifndef WITH_LEANER
  1032. if (soap->fpreparerecv && (r = soap->fpreparerecv(soap, soap->buf, ret)))
  1033. return soap->error = r;
  1034. #endif
  1035. return SOAP_OK;
  1036. }
  1037. }
  1038. else if (r != Z_BUF_ERROR)
  1039. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate error: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  1040. soap->d_stream->next_out = Z_NULL;
  1041. return soap->error = SOAP_ZLIB_ERROR;
  1042. }
  1043. }
  1044. zlib_again:
  1045. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK && !soap->chunksize)
  1046. { memcpy(soap->buf, soap->z_buf, SOAP_BUFLEN);
  1047. soap->buflen = soap->z_buflen;
  1048. }
  1049. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n---- compressed ----\n"));
  1050. }
  1051. #endif
  1052. #ifndef WITH_NOHTTP
  1053. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) /* read HTTP chunked transfer */
  1054. { for (;;)
  1055. { register soap_wchar c;
  1056. char *t, tmp[17];
  1057. if (soap->chunksize)
  1058. { soap->buflen = ret = soap->frecv(soap, soap->buf, soap->chunksize > SOAP_BUFLEN ? SOAP_BUFLEN : soap->chunksize);
  1059. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Getting chunk: read %u bytes\n", (unsigned int)ret));
  1060. DBGMSG(RECV, soap->buf, ret);
  1061. soap->bufidx = 0;
  1062. soap->chunksize -= ret;
  1063. break;
  1064. }
  1065. t = tmp;
  1066. if (!soap->chunkbuflen)
  1067. { soap->chunkbuflen = ret = soap->frecv(soap, soap->buf, SOAP_BUFLEN);
  1068. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Read %u bytes (chunked) from socket=%d\n", (unsigned int)ret, soap->socket));
  1069. DBGMSG(RECV, soap->buf, ret);
  1070. soap->bufidx = 0;
  1071. if (!ret)
  1072. { soap->ahead = EOF;
  1073. return EOF;
  1074. }
  1075. }
  1076. else
  1077. soap->bufidx = soap->buflen;
  1078. soap->buflen = soap->chunkbuflen;
  1079. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Getting chunk size (idx=%u len=%u)\n", (unsigned int)soap->bufidx, (unsigned int)soap->buflen));
  1080. while (!soap_isxdigit((int)(c = soap_getchunkchar(soap))))
  1081. { if ((int)c == EOF)
  1082. { soap->ahead = EOF;
  1083. return EOF;
  1084. }
  1085. }
  1086. do
  1087. *t++ = (char)c;
  1088. while (soap_isxdigit((int)(c = soap_getchunkchar(soap))) && (size_t)(t - tmp) < sizeof(tmp)-1);
  1089. while ((int)c != EOF && c != '\n')
  1090. c = soap_getchunkchar(soap);
  1091. if ((int)c == EOF)
  1092. { soap->ahead = EOF;
  1093. return EOF;
  1094. }
  1095. *t = '\0';
  1096. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunk size = %s (hex)\n", tmp));
  1097. soap->chunksize = (size_t)soap_strtoul(tmp, &t, 16);
  1098. if (!soap->chunksize)
  1099. { soap->bufidx = soap->buflen = soap->chunkbuflen = 0;
  1100. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of chunked message\n"));
  1101. while ((int)c != EOF && c != '\n')
  1102. c = soap_getchunkchar(soap);
  1103. ret = 0;
  1104. soap->ahead = EOF;
  1105. break;
  1106. }
  1107. soap->buflen = soap->bufidx + soap->chunksize;
  1108. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Moving buf len to idx=%u len=%u (%s)\n", (unsigned int)soap->bufidx, (unsigned int)soap->buflen, tmp));
  1109. if (soap->buflen > soap->chunkbuflen)
  1110. { soap->buflen = soap->chunkbuflen;
  1111. soap->chunksize -= soap->buflen - soap->bufidx;
  1112. soap->chunkbuflen = 0;
  1113. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Passed end of buffer for chunked HTTP (%u bytes left)\n", (unsigned int)(soap->buflen - soap->bufidx)));
  1114. }
  1115. else if (soap->chunkbuflen)
  1116. soap->chunksize = 0;
  1117. ret = soap->buflen - soap->bufidx;
  1118. if (ret)
  1119. break;
  1120. }
  1121. }
  1122. else
  1123. #endif
  1124. { soap->bufidx = 0;
  1125. soap->buflen = ret = soap->frecv(soap, soap->buf, SOAP_BUFLEN);
  1126. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Read %u bytes from socket=%d/fd=%d\n", (unsigned int)ret, soap->socket, soap->recvfd));
  1127. DBGMSG(RECV, soap->buf, ret);
  1128. }
  1129. #ifdef WITH_ZLIB
  1130. if (soap->mode & SOAP_ENC_ZLIB)
  1131. { memcpy(soap->z_buf, soap->buf, SOAP_BUFLEN);
  1132. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  1133. soap->d_stream->avail_in = (unsigned int)ret;
  1134. soap->d_stream->next_out = (Byte*)soap->buf;
  1135. soap->d_stream->avail_out = SOAP_BUFLEN;
  1136. r = inflate(soap->d_stream, Z_NO_FLUSH);
  1137. if (r == Z_NEED_DICT && soap->z_dict)
  1138. r = inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len);
  1139. if (r == Z_OK || r == Z_STREAM_END)
  1140. { soap->bufidx = 0;
  1141. soap->z_buflen = soap->buflen;
  1142. soap->buflen = SOAP_BUFLEN - soap->d_stream->avail_out;
  1143. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  1144. soap->z_crc = crc32(soap->z_crc, (Byte*)soap->buf, (unsigned int)soap->buflen);
  1145. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflated %u bytes\n", (unsigned int)soap->buflen));
  1146. if (ret && !soap->buflen && r != Z_STREAM_END)
  1147. goto zlib_again;
  1148. ret = soap->buflen;
  1149. if (r == Z_STREAM_END)
  1150. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflated total %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  1151. soap->z_ratio_in = (float)soap->d_stream->total_in / (float)soap->d_stream->total_out;
  1152. soap->d_stream->next_out = Z_NULL;
  1153. }
  1154. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n---- decompressed ----\n"));
  1155. DBGMSG(RECV, soap->buf, ret);
  1156. #ifndef WITH_LEANER
  1157. if (soap->fpreparerecv && (r = soap->fpreparerecv(soap, soap->buf, ret)))
  1158. return soap->error = r;
  1159. #endif
  1160. }
  1161. else
  1162. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to inflate: (%d) %s\n", r, soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  1163. soap->d_stream->next_out = Z_NULL;
  1164. return soap->error = SOAP_ZLIB_ERROR;
  1165. }
  1166. }
  1167. #endif
  1168. #ifndef WITH_LEANER
  1169. if (soap->fpreparerecv
  1170. #ifdef WITH_ZLIB
  1171. && soap->zlib_in == SOAP_ZLIB_NONE
  1172. #endif
  1173. && (r = soap->fpreparerecv(soap, soap->buf + soap->bufidx, ret)))
  1174. return soap->error = r;
  1175. #endif
  1176. soap->count += ret;
  1177. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Read count=%lu (+%lu)\n", (unsigned long)soap->count, (unsigned long)ret));
  1178. return !ret;
  1179. }
  1180. #endif
  1181. /******************************************************************************/
  1182. #ifndef PALM_1
  1183. SOAP_FMAC1
  1184. int
  1185. SOAP_FMAC2
  1186. soap_recv(struct soap *soap)
  1187. {
  1188. #ifndef WITH_LEANER
  1189. if (soap->mode & SOAP_ENC_DIME)
  1190. { if (soap->dime.buflen)
  1191. { char *s;
  1192. int i;
  1193. unsigned char tmp[12];
  1194. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME hdr for chunked DIME is in buffer\n"));
  1195. soap->count += soap->dime.buflen - soap->buflen;
  1196. soap->buflen = soap->dime.buflen;
  1197. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Skip padding (%ld bytes)\n", -(long)soap->dime.size&3));
  1198. for (i = -(long)soap->dime.size&3; i > 0; i--)
  1199. { soap->bufidx++;
  1200. if (soap->bufidx >= soap->buflen)
  1201. if (soap_recv_raw(soap))
  1202. return EOF;
  1203. }
  1204. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME hdr for next chunk\n"));
  1205. s = (char*)tmp;
  1206. for (i = 12; i > 0; i--)
  1207. { *s++ = soap->buf[soap->bufidx++];
  1208. if (soap->bufidx >= soap->buflen)
  1209. if (soap_recv_raw(soap))
  1210. return EOF;
  1211. }
  1212. soap->dime.flags = tmp[0] & 0x7;
  1213. soap->dime.size = ((size_t)tmp[8] << 24) | ((size_t)tmp[9] << 16) | ((size_t)tmp[10] << 8) | ((size_t)tmp[11]);
  1214. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME chunk (%u bytes)\n", (unsigned int)soap->dime.size));
  1215. if (soap->dime.flags & SOAP_DIME_CF)
  1216. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "More chunking\n"));
  1217. soap->dime.chunksize = soap->dime.size;
  1218. if (soap->buflen - soap->bufidx >= soap->dime.size)
  1219. { soap->dime.buflen = soap->buflen;
  1220. soap->buflen = soap->bufidx + soap->dime.chunksize;
  1221. }
  1222. else
  1223. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  1224. }
  1225. else
  1226. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Last chunk\n"));
  1227. soap->dime.buflen = 0;
  1228. soap->dime.chunksize = 0;
  1229. }
  1230. soap->count = soap->buflen - soap->bufidx;
  1231. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%u bytes remaining\n", (unsigned int)soap->count));
  1232. return SOAP_OK;
  1233. }
  1234. if (soap->dime.chunksize)
  1235. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get next DIME hdr for chunked DIME (%u bytes chunk)\n", (unsigned int)soap->dime.chunksize));
  1236. if (soap_recv_raw(soap))
  1237. return EOF;
  1238. if (soap->buflen - soap->bufidx >= soap->dime.chunksize)
  1239. { soap->dime.buflen = soap->buflen;
  1240. soap->count -= soap->buflen - soap->bufidx - soap->dime.chunksize;
  1241. soap->buflen = soap->bufidx + soap->dime.chunksize;
  1242. }
  1243. else
  1244. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  1245. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%lu bytes remaining, count=%lu\n", (unsigned long)(soap->buflen-soap->bufidx), (unsigned long)soap->count));
  1246. return SOAP_OK;
  1247. }
  1248. }
  1249. while (soap->ffilterrecv)
  1250. { int err, last = soap->filterstop;
  1251. if (last)
  1252. soap->bufidx = soap->buflen = 0;
  1253. if ((err = soap->ffilterrecv(soap, soap->buf, &soap->buflen, sizeof(soap->buf))))
  1254. return soap->error = err;
  1255. if (soap->buflen)
  1256. { soap->bufidx = 0;
  1257. soap->filterstop = last;
  1258. return SOAP_OK;
  1259. }
  1260. if (last)
  1261. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Returning postponed error %d\n", last));
  1262. soap->filterstop = SOAP_OK;
  1263. return last;
  1264. }
  1265. soap->filterstop = soap_recv_raw(soap); /* do not call again after EOF */
  1266. }
  1267. #endif
  1268. return soap_recv_raw(soap);
  1269. }
  1270. #endif
  1271. /******************************************************************************/
  1272. #ifndef PALM_1
  1273. SOAP_FMAC1
  1274. soap_wchar
  1275. SOAP_FMAC2
  1276. soap_getchar(struct soap *soap)
  1277. { register soap_wchar c;
  1278. c = soap->ahead;
  1279. if (c)
  1280. { if (c != EOF)
  1281. soap->ahead = 0;
  1282. return c;
  1283. }
  1284. return soap_get1(soap);
  1285. }
  1286. #endif
  1287. /******************************************************************************/
  1288. #ifndef PALM_1
  1289. SOAP_FMAC1
  1290. const struct soap_code_map*
  1291. SOAP_FMAC2
  1292. soap_code(const struct soap_code_map *code_map, const char *str)
  1293. { if (code_map && str)
  1294. { while (code_map->string)
  1295. { if (!strcmp(str, code_map->string)) /* case sensitive */
  1296. return code_map;
  1297. code_map++;
  1298. }
  1299. }
  1300. return NULL;
  1301. }
  1302. #endif
  1303. /******************************************************************************/
  1304. #ifndef PALM_1
  1305. SOAP_FMAC1
  1306. long
  1307. SOAP_FMAC2
  1308. soap_code_int(const struct soap_code_map *code_map, const char *str, long other)
  1309. { if (code_map)
  1310. { while (code_map->string)
  1311. { if (!soap_tag_cmp(str, code_map->string)) /* case insensitive */
  1312. return code_map->code;
  1313. code_map++;
  1314. }
  1315. }
  1316. return other;
  1317. }
  1318. #endif
  1319. /******************************************************************************/
  1320. #ifndef PALM_1
  1321. SOAP_FMAC1
  1322. const char*
  1323. SOAP_FMAC2
  1324. soap_code_str(const struct soap_code_map *code_map, long code)
  1325. { if (!code_map)
  1326. return NULL;
  1327. while (code_map->code != code && code_map->string)
  1328. code_map++;
  1329. return code_map->string;
  1330. }
  1331. #endif
  1332. /******************************************************************************/
  1333. #ifndef PALM_1
  1334. SOAP_FMAC1
  1335. long
  1336. SOAP_FMAC2
  1337. soap_code_bits(const struct soap_code_map *code_map, const char *str)
  1338. { register long bits = 0;
  1339. if (code_map)
  1340. { while (str && *str)
  1341. { const struct soap_code_map *p;
  1342. for (p = code_map; p->string; p++)
  1343. { register size_t n = strlen(p->string);
  1344. if (!strncmp(p->string, str, n) && soap_blank((soap_wchar)str[n]))
  1345. { bits |= p->code;
  1346. str += n;
  1347. while (*str > 0 && *str <= 32)
  1348. str++;
  1349. break;
  1350. }
  1351. }
  1352. if (!p->string)
  1353. return 0;
  1354. }
  1355. }
  1356. return bits;
  1357. }
  1358. #endif
  1359. /******************************************************************************/
  1360. #ifndef PALM_1
  1361. SOAP_FMAC1
  1362. const char*
  1363. SOAP_FMAC2
  1364. soap_code_list(struct soap *soap, const struct soap_code_map *code_map, long code)
  1365. { register char *t = soap->tmpbuf;
  1366. if (code_map)
  1367. { while (code_map->string)
  1368. { if (code_map->code & code)
  1369. { register const char *s = code_map->string;
  1370. if (t != soap->tmpbuf)
  1371. *t++ = ' ';
  1372. while (*s && t < soap->tmpbuf + sizeof(soap->tmpbuf) - 1)
  1373. *t++ = *s++;
  1374. if (t == soap->tmpbuf + sizeof(soap->tmpbuf) - 1)
  1375. break;
  1376. }
  1377. code_map++;
  1378. }
  1379. }
  1380. *t = '\0';
  1381. return soap->tmpbuf;
  1382. }
  1383. #endif
  1384. /******************************************************************************/
  1385. #ifndef PALM_1
  1386. static soap_wchar
  1387. soap_char(struct soap *soap)
  1388. { char tmp[8];
  1389. register int i;
  1390. register soap_wchar c;
  1391. register char *s = tmp;
  1392. for (i = 0; i < 7; i++)
  1393. { c = soap_get1(soap);
  1394. if (c == ';' || (int)c == EOF)
  1395. break;
  1396. *s++ = (char)c;
  1397. }
  1398. *s = '\0';
  1399. if (*tmp == '#')
  1400. { if (tmp[1] == 'x' || tmp[1] == 'X')
  1401. return (soap_wchar)soap_strtol(tmp + 2, NULL, 16);
  1402. return (soap_wchar)soap_strtol(tmp + 1, NULL, 10);
  1403. }
  1404. if (!strcmp(tmp, "lt"))
  1405. return '<';
  1406. if (!strcmp(tmp, "gt"))
  1407. return '>';
  1408. if (!strcmp(tmp, "amp"))
  1409. return '&';
  1410. if (!strcmp(tmp, "quot"))
  1411. return '"';
  1412. if (!strcmp(tmp, "apos"))
  1413. return '\'';
  1414. #ifndef WITH_LEAN
  1415. return (soap_wchar)soap_code_int(html_entity_codes, tmp, SOAP_UNKNOWN_CHAR);
  1416. #else
  1417. return SOAP_UNKNOWN_CHAR; /* use this to represent unknown code */
  1418. #endif
  1419. }
  1420. #endif
  1421. /******************************************************************************/
  1422. #ifdef WITH_LEAN
  1423. #ifndef PALM_1
  1424. soap_wchar
  1425. soap_get0(struct soap *soap)
  1426. { if (soap->bufidx >= soap->buflen && soap_recv(soap))
  1427. return EOF;
  1428. return (unsigned char)soap->buf[soap->bufidx];
  1429. }
  1430. #endif
  1431. #endif
  1432. /******************************************************************************/
  1433. #ifdef WITH_LEAN
  1434. #ifndef PALM_1
  1435. soap_wchar
  1436. soap_get1(struct soap *soap)
  1437. { if (soap->bufidx >= soap->buflen && soap_recv(soap))
  1438. return EOF;
  1439. return (unsigned char)soap->buf[soap->bufidx++];
  1440. }
  1441. #endif
  1442. #endif
  1443. /******************************************************************************/
  1444. #ifndef PALM_1
  1445. SOAP_FMAC1
  1446. soap_wchar
  1447. SOAP_FMAC2
  1448. soap_get(struct soap *soap)
  1449. { register soap_wchar c;
  1450. c = soap->ahead;
  1451. if (c)
  1452. { if ((int)c != EOF)
  1453. soap->ahead = 0;
  1454. }
  1455. else
  1456. c = soap_get1(soap);
  1457. while ((int)c != EOF)
  1458. { if (soap->cdata)
  1459. { if (c == ']')
  1460. { c = soap_get1(soap);
  1461. if (c == ']')
  1462. { c = soap_get0(soap);
  1463. if (c == '>')
  1464. { soap->cdata = 0;
  1465. c = soap_get1(soap);
  1466. c = soap_get1(soap);
  1467. }
  1468. else
  1469. { soap_unget(soap, ']');
  1470. return ']';
  1471. }
  1472. }
  1473. else
  1474. { soap_revget1(soap);
  1475. return ']';
  1476. }
  1477. }
  1478. else
  1479. return c;
  1480. }
  1481. switch (c)
  1482. { case '<':
  1483. do c = soap_get1(soap);
  1484. while (soap_blank(c));
  1485. if (c == '!' || c == '?' || c == '%')
  1486. { register int k = 1;
  1487. if (c == '!')
  1488. { c = soap_get1(soap);
  1489. if (c == '[')
  1490. { do c = soap_get1(soap);
  1491. while ((int)c != EOF && c != '[');
  1492. if ((int)c == EOF)
  1493. break;
  1494. soap->cdata = 1;
  1495. c = soap_get1(soap);
  1496. continue;
  1497. }
  1498. if (c == '-' && (c = soap_get1(soap)) == '-')
  1499. { do
  1500. { c = soap_get1(soap);
  1501. if (c == '-' && (c = soap_get1(soap)) == '-')
  1502. break;
  1503. } while ((int)c != EOF);
  1504. }
  1505. }
  1506. else if (c == '?')
  1507. c = soap_get_pi(soap);
  1508. while ((int)c != EOF)
  1509. { if (c == '<')
  1510. k++;
  1511. else if (c == '>')
  1512. { if (--k <= 0)
  1513. break;
  1514. }
  1515. c = soap_get1(soap);
  1516. }
  1517. if ((int)c == EOF)
  1518. break;
  1519. c = soap_get1(soap);
  1520. continue;
  1521. }
  1522. if (c == '/')
  1523. return SOAP_TT;
  1524. soap_revget1(soap);
  1525. return SOAP_LT;
  1526. case '>':
  1527. return SOAP_GT;
  1528. case '"':
  1529. return SOAP_QT;
  1530. case '\'':
  1531. return SOAP_AP;
  1532. case '&':
  1533. return soap_char(soap) | 0x80000000;
  1534. }
  1535. break;
  1536. }
  1537. return c;
  1538. }
  1539. #endif
  1540. /******************************************************************************/
  1541. #ifndef PALM_1
  1542. static soap_wchar
  1543. soap_get_pi(struct soap *soap)
  1544. { char buf[64];
  1545. register char *s = buf;
  1546. register int i = sizeof(buf);
  1547. register soap_wchar c = soap_getchar(soap);
  1548. /* This is a quick way to parse XML PI and we could use a callback instead to
  1549. * enable applications to intercept processing instructions */
  1550. while ((int)c != EOF && c != '?')
  1551. { if (--i > 0)
  1552. { if (soap_blank(c))
  1553. c = ' ';
  1554. *s++ = (char)c;
  1555. }
  1556. c = soap_getchar(soap);
  1557. }
  1558. *s = '\0';
  1559. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "XML PI <?%s?>\n", buf));
  1560. if (!strncmp(buf, "xml ", 4))
  1561. { s = strstr(buf, " encoding=");
  1562. if (s && s[10])
  1563. { if (!soap_tag_cmp(s + 11, "iso-8859-1*")
  1564. || !soap_tag_cmp(s + 11, "latin1*"))
  1565. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Switching to latin1 encoding\n"));
  1566. soap->mode |= SOAP_ENC_LATIN;
  1567. }
  1568. else if (!soap_tag_cmp(s + 11, "utf-8*"))
  1569. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Switching to utf-8 encoding\n"));
  1570. soap->mode &= ~SOAP_ENC_LATIN;
  1571. }
  1572. }
  1573. }
  1574. if ((int)c != EOF)
  1575. c = soap_getchar(soap);
  1576. return c;
  1577. }
  1578. #endif
  1579. /******************************************************************************/
  1580. #ifndef WITH_LEANER
  1581. #ifndef PALM_1
  1582. SOAP_FMAC1
  1583. int
  1584. SOAP_FMAC2
  1585. soap_move(struct soap *soap, size_t n)
  1586. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Moving %lu bytes forward\n", (unsigned long)n));
  1587. for (; n; n--)
  1588. if ((int)soap_getchar(soap) == EOF)
  1589. return SOAP_EOF;
  1590. return SOAP_OK;
  1591. }
  1592. #endif
  1593. #endif
  1594. /******************************************************************************/
  1595. #ifndef WITH_LEANER
  1596. #ifndef PALM_1
  1597. SOAP_FMAC1
  1598. size_t
  1599. SOAP_FMAC2
  1600. soap_tell(struct soap *soap)
  1601. { return soap->count - soap->buflen + soap->bufidx - (soap->ahead != 0);
  1602. }
  1603. #endif
  1604. #endif
  1605. /******************************************************************************/
  1606. #ifndef PALM_1
  1607. SOAP_FMAC1
  1608. int
  1609. SOAP_FMAC2
  1610. soap_pututf8(struct soap *soap, register unsigned long c)
  1611. { char tmp[16];
  1612. if (c < 0x80 && c > 0)
  1613. { *tmp = (char)c;
  1614. return soap_send_raw(soap, tmp, 1);
  1615. }
  1616. #ifndef WITH_LEAN
  1617. if (c >= 0x80)
  1618. { register char *t = tmp;
  1619. if (c < 0x0800)
  1620. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  1621. else
  1622. { if (c < 0x010000)
  1623. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  1624. else
  1625. { if (c < 0x200000)
  1626. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  1627. else
  1628. { if (c < 0x04000000)
  1629. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  1630. else
  1631. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  1632. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  1633. }
  1634. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  1635. }
  1636. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  1637. }
  1638. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  1639. }
  1640. *t++ = (char)(0x80 | (c & 0x3F));
  1641. *t = '\0';
  1642. }
  1643. else
  1644. #endif
  1645. #ifdef HAVE_SNPRINTF
  1646. soap_snprintf(tmp, sizeof(tmp), "&#%lu;", c);
  1647. #else
  1648. sprintf(tmp, "&#%lu;", c);
  1649. #endif
  1650. return soap_send(soap, tmp);
  1651. }
  1652. #endif
  1653. /******************************************************************************/
  1654. #ifndef PALM_1
  1655. SOAP_FMAC1
  1656. soap_wchar
  1657. SOAP_FMAC2
  1658. soap_getutf8(struct soap *soap)
  1659. { register soap_wchar c, c1, c2, c3, c4;
  1660. c = soap->ahead;
  1661. if (c >= 0x80)
  1662. soap->ahead = 0;
  1663. else
  1664. c = soap_get(soap);
  1665. if (c < 0x80 || c > 0xFF || (soap->mode & SOAP_ENC_LATIN))
  1666. return c;
  1667. c1 = soap_get1(soap);
  1668. if (c1 < 0x80)
  1669. { soap_revget1(soap); /* doesn't look like this is UTF8 */
  1670. return c;
  1671. }
  1672. c1 &= 0x3F;
  1673. if (c < 0xE0)
  1674. return ((soap_wchar)(c & 0x1F) << 6) | c1;
  1675. c2 = (soap_wchar)soap_get1(soap) & 0x3F;
  1676. if (c < 0xF0)
  1677. return ((soap_wchar)(c & 0x0F) << 12) | (c1 << 6) | c2;
  1678. c3 = (soap_wchar)soap_get1(soap) & 0x3F;
  1679. if (c < 0xF8)
  1680. return ((soap_wchar)(c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3;
  1681. c4 = (soap_wchar)soap_get1(soap) & 0x3F;
  1682. if (c < 0xFC)
  1683. return ((soap_wchar)(c & 0x03) << 24) | (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
  1684. return ((soap_wchar)(c & 0x01) << 30) | (c1 << 24) | (c2 << 18) | (c3 << 12) | (c4 << 6) | (soap_wchar)(soap_get1(soap) & 0x3F);
  1685. }
  1686. #endif
  1687. /******************************************************************************/
  1688. #ifndef PALM_1
  1689. SOAP_FMAC1
  1690. int
  1691. SOAP_FMAC2
  1692. soap_puthex(struct soap *soap, const unsigned char *s, int n)
  1693. { char d[2];
  1694. register int i;
  1695. #ifdef WITH_DOM
  1696. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  1697. { if (!(soap->dom->data = soap_s2hex(soap, s, NULL, n)))
  1698. return soap->error;
  1699. return SOAP_OK;
  1700. }
  1701. #endif
  1702. for (i = 0; i < n; i++)
  1703. { register int m = *s++;
  1704. d[0] = (char)((m >> 4) + (m > 159 ? '7' : '0'));
  1705. m &= 0x0F;
  1706. d[1] = (char)(m + (m > 9 ? '7' : '0'));
  1707. if (soap_send_raw(soap, d, 2))
  1708. return soap->error;
  1709. }
  1710. return SOAP_OK;
  1711. }
  1712. #endif
  1713. /******************************************************************************/
  1714. #ifndef PALM_1
  1715. SOAP_FMAC1
  1716. unsigned char*
  1717. SOAP_FMAC2
  1718. soap_gethex(struct soap *soap, int *n)
  1719. {
  1720. #ifdef WITH_DOM
  1721. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  1722. { soap->dom->data = soap_string_in(soap, 0, -1, -1);
  1723. return (unsigned char*)soap_hex2s(soap, soap->dom->data, NULL, 0, n);
  1724. }
  1725. #endif
  1726. #ifdef WITH_FAST
  1727. soap->labidx = 0;
  1728. for (;;)
  1729. { register char *s;
  1730. register size_t i, k;
  1731. if (soap_append_lab(soap, NULL, 0))
  1732. return NULL;
  1733. s = soap->labbuf + soap->labidx;
  1734. k = soap->lablen - soap->labidx;
  1735. soap->labidx = soap->lablen;
  1736. for (i = 0; i < k; i++)
  1737. { register char d1, d2;
  1738. register soap_wchar c;
  1739. c = soap_get(soap);
  1740. if (soap_isxdigit(c))
  1741. { d1 = (char)c;
  1742. c = soap_get(soap);
  1743. if (soap_isxdigit(c))
  1744. d2 = (char)c;
  1745. else
  1746. { soap->error = SOAP_TYPE;
  1747. return NULL;
  1748. }
  1749. }
  1750. else
  1751. { unsigned char *p;
  1752. soap_unget(soap, c);
  1753. if (n)
  1754. *n = (int)(soap->lablen + i - k);
  1755. p = (unsigned char*)soap_malloc(soap, soap->lablen + i - k);
  1756. if (p)
  1757. memcpy(p, soap->labbuf, soap->lablen + i - k);
  1758. return p;
  1759. }
  1760. *s++ = (char)(((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0'));
  1761. }
  1762. }
  1763. #else
  1764. if (soap_new_block(soap) == NULL)
  1765. return NULL;
  1766. for (;;)
  1767. { register int i;
  1768. register char *s = (char*)soap_push_block(soap, NULL, SOAP_BLKLEN);
  1769. if (!s)
  1770. { soap_end_block(soap, NULL);
  1771. return NULL;
  1772. }
  1773. for (i = 0; i < SOAP_BLKLEN; i++)
  1774. { register char d1, d2;
  1775. register soap_wchar c = soap_get(soap);
  1776. if (soap_isxdigit(c))
  1777. { d1 = (char)c;
  1778. c = soap_get(soap);
  1779. if (soap_isxdigit(c))
  1780. d2 = (char)c;
  1781. else
  1782. { soap_end_block(soap, NULL);
  1783. soap->error = SOAP_TYPE;
  1784. return NULL;
  1785. }
  1786. }
  1787. else
  1788. { unsigned char *p;
  1789. soap_unget(soap, c);
  1790. if (n)
  1791. *n = (int)soap_size_block(soap, NULL, i);
  1792. p = (unsigned char*)soap_save_block(soap, NULL, 0);
  1793. return p;
  1794. }
  1795. *s++ = ((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0');
  1796. }
  1797. }
  1798. #endif
  1799. }
  1800. #endif
  1801. /******************************************************************************/
  1802. #ifndef PALM_1
  1803. SOAP_FMAC1
  1804. int
  1805. SOAP_FMAC2
  1806. soap_putbase64(struct soap *soap, const unsigned char *s, int n)
  1807. { register int i;
  1808. register unsigned long m;
  1809. char d[4];
  1810. if (!s)
  1811. return SOAP_OK;
  1812. #ifdef WITH_DOM
  1813. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  1814. { if (!(soap->dom->data = soap_s2base64(soap, s, NULL, n)))
  1815. return soap->error;
  1816. return SOAP_OK;
  1817. }
  1818. #endif
  1819. for (; n > 2; n -= 3, s += 3)
  1820. { m = s[0];
  1821. m = (m << 8) | s[1];
  1822. m = (m << 8) | s[2];
  1823. for (i = 4; i > 0; m >>= 6)
  1824. d[--i] = soap_base64o[m & 0x3F];
  1825. if (soap_send_raw(soap, d, 4))
  1826. return soap->error;
  1827. }
  1828. if (n > 0)
  1829. { m = 0;
  1830. for (i = 0; i < n; i++)
  1831. m = (m << 8) | *s++;
  1832. for (; i < 3; i++)
  1833. m <<= 8;
  1834. for (i++; i > 0; m >>= 6)
  1835. d[--i] = soap_base64o[m & 0x3F];
  1836. for (i = 3; i > n; i--)
  1837. d[i] = '=';
  1838. if (soap_send_raw(soap, d, 4))
  1839. return soap->error;
  1840. }
  1841. return SOAP_OK;
  1842. }
  1843. #endif
  1844. /******************************************************************************/
  1845. #ifndef PALM_1
  1846. SOAP_FMAC1
  1847. unsigned char*
  1848. SOAP_FMAC2
  1849. soap_getbase64(struct soap *soap, int *n, int malloc_flag)
  1850. { (void)malloc_flag;
  1851. #ifdef WITH_DOM
  1852. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  1853. { soap->dom->data = soap_string_in(soap, 0, -1, -1);
  1854. return (unsigned char*)soap_base642s(soap, soap->dom->data, NULL, 0, n);
  1855. }
  1856. #endif
  1857. #ifdef WITH_FAST
  1858. soap->labidx = 0;
  1859. for (;;)
  1860. { register size_t i, k;
  1861. register char *s;
  1862. if (soap_append_lab(soap, NULL, 2))
  1863. return NULL;
  1864. s = soap->labbuf + soap->labidx;
  1865. k = soap->lablen - soap->labidx;
  1866. soap->labidx = 3 * (soap->lablen / 3);
  1867. if (!s)
  1868. return NULL;
  1869. if (k > 2)
  1870. { for (i = 0; i < k - 2; i += 3)
  1871. { register unsigned long m = 0;
  1872. register int j = 0;
  1873. do
  1874. { register soap_wchar c = soap_get(soap);
  1875. if (c < SOAP_AP)
  1876. c &= 0x7FFFFFFF;
  1877. if (c == '=' || c < 0)
  1878. { unsigned char *p;
  1879. switch (j)
  1880. { case 2:
  1881. *s++ = (char)((m >> 4) & 0xFF);
  1882. i++;
  1883. break;
  1884. case 3:
  1885. *s++ = (char)((m >> 10) & 0xFF);
  1886. *s++ = (char)((m >> 2) & 0xFF);
  1887. i += 2;
  1888. }
  1889. if (n)
  1890. *n = (int)(soap->lablen + i - k);
  1891. p = (unsigned char*)soap_malloc(soap, soap->lablen + i - k);
  1892. if (p)
  1893. memcpy(p, soap->labbuf, soap->lablen + i - k);
  1894. if (c >= 0)
  1895. { while ((int)((c = soap_get(soap)) != EOF) && c != SOAP_LT && c != SOAP_TT)
  1896. ;
  1897. }
  1898. soap_unget(soap, c);
  1899. return p;
  1900. }
  1901. c -= '+';
  1902. if (c >= 0 && c <= 79)
  1903. { register int b = soap_base64i[c];
  1904. if (b >= 64)
  1905. { soap->error = SOAP_TYPE;
  1906. return NULL;
  1907. }
  1908. m = (m << 6) + b;
  1909. j++;
  1910. }
  1911. else if (!soap_blank(c + '+'))
  1912. { soap->error = SOAP_TYPE;
  1913. return NULL;
  1914. }
  1915. } while (j < 4);
  1916. *s++ = (char)((m >> 16) & 0xFF);
  1917. *s++ = (char)((m >> 8) & 0xFF);
  1918. *s++ = (char)(m & 0xFF);
  1919. }
  1920. }
  1921. }
  1922. #else
  1923. if (soap_new_block(soap) == NULL)
  1924. return NULL;
  1925. for (;;)
  1926. { register int i;
  1927. register char *s = (char*)soap_push_block(soap, NULL, 3 * SOAP_BLKLEN); /* must be multiple of 3 */
  1928. if (!s)
  1929. { soap_end_block(soap, NULL);
  1930. return NULL;
  1931. }
  1932. for (i = 0; i < SOAP_BLKLEN; i++)
  1933. { register unsigned long m = 0;
  1934. register int j = 0;
  1935. do
  1936. { register soap_wchar c = soap_get(soap);
  1937. if (c == '=' || c < 0)
  1938. { unsigned char *p;
  1939. i *= 3;
  1940. switch (j)
  1941. { case 2:
  1942. *s++ = (char)((m >> 4) & 0xFF);
  1943. i++;
  1944. break;
  1945. case 3:
  1946. *s++ = (char)((m >> 10) & 0xFF);
  1947. *s++ = (char)((m >> 2) & 0xFF);
  1948. i += 2;
  1949. }
  1950. if (n)
  1951. *n = (int)soap_size_block(soap, NULL, i);
  1952. p = (unsigned char*)soap_save_block(soap, NULL, 0);
  1953. if (c >= 0)
  1954. { while ((int)((c = soap_get(soap)) != EOF) && c != SOAP_LT && c != SOAP_TT)
  1955. ;
  1956. }
  1957. soap_unget(soap, c);
  1958. return p;
  1959. }
  1960. c -= '+';
  1961. if (c >= 0 && c <= 79)
  1962. { int b = soap_base64i[c];
  1963. if (b >= 64)
  1964. { soap->error = SOAP_TYPE;
  1965. return NULL;
  1966. }
  1967. m = (m << 6) + b;
  1968. j++;
  1969. }
  1970. else if (!soap_blank(c))
  1971. { soap->error = SOAP_TYPE;
  1972. return NULL;
  1973. }
  1974. } while (j < 4);
  1975. *s++ = (char)((m >> 16) & 0xFF);
  1976. *s++ = (char)((m >> 8) & 0xFF);
  1977. *s++ = (char)(m & 0xFF);
  1978. }
  1979. }
  1980. #endif
  1981. }
  1982. #endif
  1983. /******************************************************************************/
  1984. #ifndef WITH_LEANER
  1985. #ifndef PALM_1
  1986. SOAP_FMAC1
  1987. int
  1988. SOAP_FMAC2
  1989. soap_xop_forward(struct soap *soap, unsigned char **ptr, int *size, char **id, char **type, char **options)
  1990. { /* Check MTOM xop:Include element (within hex/base64Binary) */
  1991. /* TODO: this code to be obsoleted with new import/xop.h conventions */
  1992. short body = soap->body; /* should save type too? */
  1993. if (!soap_peek_element(soap))
  1994. { if (!soap_element_begin_in(soap, "xop:Include", 0, NULL))
  1995. { if (soap_dime_forward(soap, ptr, size, id, type, options)
  1996. || (soap->body && soap_element_end_in(soap, "xop:Include")))
  1997. return soap->error;
  1998. }
  1999. }
  2000. soap->body = body;
  2001. return SOAP_OK;
  2002. }
  2003. #endif
  2004. #endif
  2005. /******************************************************************************/
  2006. #ifndef WITH_LEANER
  2007. #ifndef PALM_1
  2008. SOAP_FMAC1
  2009. int
  2010. SOAP_FMAC2
  2011. soap_dime_forward(struct soap *soap, unsigned char **ptr, int *size, char **id, char **type, char **options)
  2012. { struct soap_xlist *xp;
  2013. *ptr = NULL;
  2014. *size = 0;
  2015. *id = NULL;
  2016. *type = NULL;
  2017. *options = NULL;
  2018. if (!*soap->href)
  2019. return SOAP_OK;
  2020. *id = soap_strdup(soap, soap->href);
  2021. xp = (struct soap_xlist*)SOAP_MALLOC(soap, sizeof(struct soap_xlist));
  2022. if (!xp)
  2023. return soap->error = SOAP_EOM;
  2024. xp->next = soap->xlist;
  2025. xp->ptr = ptr;
  2026. xp->size = size;
  2027. xp->id = *id;
  2028. xp->type = type;
  2029. xp->options = options;
  2030. soap->xlist = xp;
  2031. return SOAP_OK;
  2032. }
  2033. #endif
  2034. #endif
  2035. /******************************************************************************/
  2036. #ifndef PALM_1
  2037. SOAP_FMAC1
  2038. char *
  2039. SOAP_FMAC2
  2040. soap_strdup(struct soap *soap, const char *s)
  2041. { char *t = NULL;
  2042. if (s && (t = (char*)soap_malloc(soap, strlen(s) + 1)))
  2043. strcpy(t, s);
  2044. return t;
  2045. }
  2046. #endif
  2047. /******************************************************************************/
  2048. #ifndef PALM_1
  2049. SOAP_FMAC1
  2050. wchar_t *
  2051. SOAP_FMAC2
  2052. soap_wstrdup(struct soap *soap, const wchar_t *s)
  2053. { wchar_t *t = NULL;
  2054. if (s)
  2055. { size_t n = 0;
  2056. while (s[n])
  2057. n++;
  2058. if ((t = (wchar_t*)soap_malloc(soap, sizeof(wchar_t)*(n+1))))
  2059. memcpy(t, s, sizeof(wchar_t)*(n+1));
  2060. }
  2061. return t;
  2062. }
  2063. #endif
  2064. /******************************************************************************/
  2065. #ifndef PALM_1
  2066. SOAP_FMAC1
  2067. struct soap_blist*
  2068. SOAP_FMAC2
  2069. soap_new_block(struct soap *soap)
  2070. { struct soap_blist *p;
  2071. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New block sequence (prev=%p)\n", soap->blist));
  2072. if (!(p = (struct soap_blist*)SOAP_MALLOC(soap, sizeof(struct soap_blist))))
  2073. { soap->error = SOAP_EOM;
  2074. return NULL;
  2075. }
  2076. p->next = soap->blist;
  2077. p->ptr = NULL;
  2078. p->size = 0;
  2079. soap->blist = p;
  2080. return p;
  2081. }
  2082. #endif
  2083. /******************************************************************************/
  2084. #ifndef PALM_1
  2085. SOAP_FMAC1
  2086. void*
  2087. SOAP_FMAC2
  2088. soap_push_block(struct soap *soap, struct soap_blist *b, size_t n)
  2089. { char *p;
  2090. if (!b)
  2091. b = soap->blist;
  2092. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Push block of %u bytes (%u bytes total)\n", (unsigned int)n, (unsigned int)b->size + (unsigned int)n));
  2093. if (!(p = (char*)SOAP_MALLOC(soap, n + sizeof(char*) + sizeof(size_t))))
  2094. { soap->error = SOAP_EOM;
  2095. return NULL;
  2096. }
  2097. *(char**)p = b->ptr;
  2098. *(size_t*)(p + sizeof(char*)) = n;
  2099. b->ptr = p;
  2100. b->size += n;
  2101. return p + sizeof(char*) + sizeof(size_t);
  2102. }
  2103. #endif
  2104. /******************************************************************************/
  2105. #ifndef PALM_1
  2106. SOAP_FMAC1
  2107. void
  2108. SOAP_FMAC2
  2109. soap_pop_block(struct soap *soap, struct soap_blist *b)
  2110. { char *p;
  2111. if (!b)
  2112. b = soap->blist;
  2113. if (!b->ptr)
  2114. return;
  2115. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Pop block\n"));
  2116. p = b->ptr;
  2117. b->size -= *(size_t*)(p + sizeof(char*));
  2118. b->ptr = *(char**)p;
  2119. SOAP_FREE(soap, p);
  2120. }
  2121. #endif
  2122. /******************************************************************************/
  2123. #ifndef PALM_1
  2124. SOAP_FMAC1
  2125. void
  2126. SOAP_FMAC2
  2127. soap_update_pointers(struct soap *soap, char *start, char *end, char *p1, char *p2)
  2128. {
  2129. #ifndef WITH_NOIDREF
  2130. int i;
  2131. register struct soap_ilist *ip = NULL;
  2132. register struct soap_flist *fp = NULL;
  2133. #ifndef WITH_LEANER
  2134. register struct soap_xlist *xp = NULL;
  2135. #endif
  2136. register void *p, **q;
  2137. for (i = 0; i < SOAP_IDHASH; i++)
  2138. { for (ip = soap->iht[i]; ip; ip = ip->next)
  2139. { if (ip->ptr && (char*)ip->ptr >= start && (char*)ip->ptr < end)
  2140. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update id='%s' %p -> %p\n", ip->id, ip->ptr, (char*)ip->ptr + (p1-p2)));
  2141. ip->ptr = (char*)ip->ptr + (p1-p2);
  2142. }
  2143. for (q = &ip->link; q; q = (void**)p)
  2144. { p = *q;
  2145. if (p && (char*)p >= start && (char*)p < end)
  2146. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Link update id='%s' %p\n", ip->id, p));
  2147. *q = (char*)p + (p1-p2);
  2148. }
  2149. }
  2150. for (q = &ip->copy; q; q = (void**)p)
  2151. { p = *q;
  2152. if (p && (char*)p >= start && (char*)p < end)
  2153. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copy chain update id='%s' %p\n", ip->id, p));
  2154. *q = (char*)p + (p1-p2);
  2155. }
  2156. }
  2157. for (fp = ip->flist; fp; fp = fp->next)
  2158. { if ((char*)fp->ptr >= start && (char*)fp->ptr < end)
  2159. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copy list update id='%s' %p\n", ip->id, fp));
  2160. fp->ptr = (char*)fp->ptr + (p1-p2);
  2161. }
  2162. }
  2163. }
  2164. }
  2165. #ifndef WITH_LEANER
  2166. for (xp = soap->xlist; xp; xp = xp->next)
  2167. { if (xp->ptr && (char*)xp->ptr >= start && (char*)xp->ptr < end)
  2168. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update id='%s' %p -> %p\n", xp->id ? xp->id : SOAP_STR_EOS, xp->ptr, (char*)xp->ptr + (p1-p2)));
  2169. xp->ptr = (unsigned char**)((char*)xp->ptr + (p1-p2));
  2170. xp->size = (int*)((char*)xp->size + (p1-p2));
  2171. xp->type = (char**)((char*)xp->type + (p1-p2));
  2172. xp->options = (char**)((char*)xp->options + (p1-p2));
  2173. }
  2174. }
  2175. #endif
  2176. #else
  2177. (void)soap; (void)start; (void)end; (void)p1; (void)p2;
  2178. #endif
  2179. }
  2180. #endif
  2181. /******************************************************************************/
  2182. #ifndef WITH_NOIDREF
  2183. #ifndef PALM_1
  2184. static int
  2185. soap_has_copies(struct soap *soap, register const char *start, register const char *end)
  2186. { register int i;
  2187. register struct soap_ilist *ip = NULL;
  2188. register struct soap_flist *fp = NULL;
  2189. register const char *p;
  2190. for (i = 0; i < SOAP_IDHASH; i++)
  2191. { for (ip = soap->iht[i]; ip; ip = ip->next)
  2192. { for (p = (const char*)ip->copy; p; p = *(const char**)p)
  2193. if (p >= start && p < end)
  2194. return SOAP_ERR;
  2195. for (fp = ip->flist; fp; fp = fp->next)
  2196. if ((const char*)fp->ptr >= start && (const char*)fp->ptr < end)
  2197. return SOAP_ERR;
  2198. }
  2199. }
  2200. return SOAP_OK;
  2201. }
  2202. #endif
  2203. #endif
  2204. /******************************************************************************/
  2205. #ifndef WITH_NOIDREF
  2206. #ifndef PALM_1
  2207. SOAP_FMAC1
  2208. int
  2209. SOAP_FMAC2
  2210. soap_resolve(struct soap *soap)
  2211. { register int i;
  2212. register struct soap_ilist *ip = NULL;
  2213. register struct soap_flist *fp = NULL;
  2214. short flag;
  2215. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving forwarded data\n"));
  2216. for (i = 0; i < SOAP_IDHASH; i++)
  2217. { for (ip = soap->iht[i]; ip; ip = ip->next)
  2218. { if (ip->ptr)
  2219. { register void *p, **q, *r;
  2220. q = (void**)ip->link;
  2221. ip->link = NULL;
  2222. r = ip->ptr;
  2223. DBGLOG(TEST, if (q) SOAP_MESSAGE(fdebug, "Traversing link chain to resolve id='%s'\n", ip->id));
  2224. while (q)
  2225. { p = *q;
  2226. *q = r;
  2227. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "... link %p -> %p\n", q, r));
  2228. q = (void**)p;
  2229. }
  2230. }
  2231. else if (*ip->id == '#')
  2232. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Missing data for id='%s'\n", ip->id));
  2233. strcpy(soap->id, ip->id + 1);
  2234. return soap->error = SOAP_MISSING_ID;
  2235. }
  2236. }
  2237. }
  2238. do
  2239. { flag = 0;
  2240. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolution phase\n"));
  2241. for (i = 0; i < SOAP_IDHASH; i++)
  2242. { for (ip = soap->iht[i]; ip; ip = ip->next)
  2243. { if (ip->ptr && !soap_has_copies(soap, (const char*)ip->ptr, (const char*)ip->ptr + ip->size))
  2244. { if (ip->copy)
  2245. { register void *p, **q = (void**)ip->copy;
  2246. DBGLOG(TEST, if (q) SOAP_MESSAGE(fdebug, "Traversing copy chain to resolve id='%s'\n", ip->id));
  2247. ip->copy = NULL;
  2248. do
  2249. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... copy %p -> %p (%u bytes)\n", ip->ptr, q, (unsigned int)ip->size));
  2250. p = *q;
  2251. memcpy(q, ip->ptr, ip->size);
  2252. q = (void**)p;
  2253. } while (q);
  2254. flag = 1;
  2255. }
  2256. for (fp = ip->flist; fp; fp = ip->flist)
  2257. { register unsigned int k = fp->level;
  2258. register void *p = ip->ptr;
  2259. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving forwarded data type=%d location=%p level=%u,%u id='%s'\n", ip->type, p, ip->level, fp->level, ip->id));
  2260. while (ip->level < k)
  2261. { register void **q = (void**)soap_malloc(soap, sizeof(void*));
  2262. if (!q)
  2263. return soap->error;
  2264. *q = p;
  2265. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level, new location=%p holds=%p...\n", q, *q));
  2266. p = (void*)q;
  2267. k--;
  2268. }
  2269. if (fp->fcopy)
  2270. fp->fcopy(soap, ip->type, fp->type, fp->ptr, fp->len, p, ip->size);
  2271. else
  2272. soap_fcopy(soap, ip->type, fp->type, fp->ptr, fp->len, p, ip->size);
  2273. ip->flist = fp->next;
  2274. SOAP_FREE(soap, fp);
  2275. flag = 1;
  2276. }
  2277. }
  2278. }
  2279. }
  2280. } while (flag);
  2281. #ifdef SOAP_DEBUG
  2282. for (i = 0; i < SOAP_IDHASH; i++)
  2283. { for (ip = soap->iht[i]; ip; ip = ip->next)
  2284. { if (ip->copy || ip->flist)
  2285. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolution error: forwarded data for id='%s' could not be propagated, please report this problem to the developers\n", ip->id));
  2286. }
  2287. }
  2288. }
  2289. #endif
  2290. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolution done\n"));
  2291. return SOAP_OK;
  2292. }
  2293. #endif
  2294. #endif
  2295. /******************************************************************************/
  2296. #ifndef PALM_1
  2297. SOAP_FMAC1
  2298. size_t
  2299. SOAP_FMAC2
  2300. soap_size_block(struct soap *soap, struct soap_blist *b, size_t n)
  2301. { if (!b)
  2302. b = soap->blist;
  2303. if (b->ptr)
  2304. { b->size -= *(size_t*)(b->ptr + sizeof(char*)) - n;
  2305. *(size_t*)(b->ptr + sizeof(char*)) = n;
  2306. }
  2307. return b->size;
  2308. }
  2309. #endif
  2310. /******************************************************************************/
  2311. #ifndef PALM_1
  2312. SOAP_FMAC1
  2313. char*
  2314. SOAP_FMAC2
  2315. soap_first_block(struct soap *soap, struct soap_blist *b)
  2316. { char *p, *q, *r;
  2317. if (!b)
  2318. b = soap->blist;
  2319. p = b->ptr;
  2320. if (!p)
  2321. return NULL;
  2322. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "First block\n"));
  2323. r = NULL;
  2324. do
  2325. { q = *(char**)p;
  2326. *(char**)p = r;
  2327. r = p;
  2328. p = q;
  2329. } while (p);
  2330. b->ptr = r;
  2331. return r + sizeof(char*) + sizeof(size_t);
  2332. }
  2333. #endif
  2334. /******************************************************************************/
  2335. #ifndef PALM_1
  2336. SOAP_FMAC1
  2337. char*
  2338. SOAP_FMAC2
  2339. soap_next_block(struct soap *soap, struct soap_blist *b)
  2340. { char *p;
  2341. if (!b)
  2342. b = soap->blist;
  2343. p = b->ptr;
  2344. if (p)
  2345. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Next block\n"));
  2346. b->ptr = *(char**)p;
  2347. SOAP_FREE(soap, p);
  2348. if (b->ptr)
  2349. return b->ptr + sizeof(char*) + sizeof(size_t);
  2350. }
  2351. return NULL;
  2352. }
  2353. #endif
  2354. /******************************************************************************/
  2355. #ifndef PALM_1
  2356. SOAP_FMAC1
  2357. size_t
  2358. SOAP_FMAC2
  2359. soap_block_size(struct soap *soap, struct soap_blist *b)
  2360. { if (!b)
  2361. b = soap->blist;
  2362. return *(size_t*)(b->ptr + sizeof(char*));
  2363. }
  2364. #endif
  2365. /******************************************************************************/
  2366. #ifndef PALM_1
  2367. SOAP_FMAC1
  2368. void
  2369. SOAP_FMAC2
  2370. soap_end_block(struct soap *soap, struct soap_blist *b)
  2371. { char *p, *q;
  2372. if (!b)
  2373. b = soap->blist;
  2374. if (b)
  2375. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of block sequence, free all remaining blocks\n"));
  2376. for (p = b->ptr; p; p = q)
  2377. { q = *(char**)p;
  2378. SOAP_FREE(soap, p);
  2379. }
  2380. if (soap->blist == b)
  2381. soap->blist = b->next;
  2382. else
  2383. { struct soap_blist *bp;
  2384. for (bp = soap->blist; bp; bp = bp->next)
  2385. { if (bp->next == b)
  2386. { bp->next = b->next;
  2387. break;
  2388. }
  2389. }
  2390. }
  2391. SOAP_FREE(soap, b);
  2392. }
  2393. DBGLOG(TEST, if (soap->blist) SOAP_MESSAGE(fdebug, "Restore previous block sequence\n"));
  2394. }
  2395. #endif
  2396. /******************************************************************************/
  2397. #ifndef PALM_1
  2398. SOAP_FMAC1
  2399. char*
  2400. SOAP_FMAC2
  2401. soap_save_block(struct soap *soap, struct soap_blist *b, char *p, int flag)
  2402. { register size_t n;
  2403. register char *q, *s;
  2404. if (!b)
  2405. b = soap->blist;
  2406. if (b->size)
  2407. { if (!p)
  2408. p = (char*)soap_malloc(soap, b->size);
  2409. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Save all blocks in contiguous memory space of %u bytes (%p->%p)\n", (unsigned int)b->size, b->ptr, p));
  2410. if (p)
  2411. { for (s = p, q = soap_first_block(soap, b); q; q = soap_next_block(soap, b))
  2412. { n = soap_block_size(soap, b);
  2413. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copy %u bytes from %p to %p\n", (unsigned int)n, q, s));
  2414. #ifndef WITH_NOIDREF
  2415. if (flag)
  2416. soap_update_pointers(soap, q, q + n, s, q);
  2417. #endif
  2418. memcpy(s, q, n);
  2419. s += n;
  2420. }
  2421. }
  2422. else
  2423. soap->error = SOAP_EOM;
  2424. }
  2425. soap_end_block(soap, b);
  2426. return p;
  2427. }
  2428. #endif
  2429. /******************************************************************************/
  2430. #ifndef PALM_2
  2431. SOAP_FMAC1
  2432. char *
  2433. SOAP_FMAC2
  2434. soap_putsize(struct soap *soap, const char *type, int size)
  2435. { return soap_putsizes(soap, type, &size, 1);
  2436. }
  2437. #endif
  2438. /******************************************************************************/
  2439. #ifndef PALM_2
  2440. SOAP_FMAC1
  2441. char *
  2442. SOAP_FMAC2
  2443. soap_putsizes(struct soap *soap, const char *type, const int *size, int dim)
  2444. { return soap_putsizesoffsets(soap, type, size, NULL, dim);
  2445. }
  2446. #endif
  2447. /******************************************************************************/
  2448. #ifndef PALM_2
  2449. SOAP_FMAC1
  2450. char *
  2451. SOAP_FMAC2
  2452. soap_putsizesoffsets(struct soap *soap, const char *type, const int *size, const int *offset, int dim)
  2453. { register int i;
  2454. register size_t l;
  2455. if (!type || strlen(type) + 13 > sizeof(soap->type)) /* prevent overruns */
  2456. return NULL;
  2457. if (soap->version == 2)
  2458. {
  2459. #ifdef HAVE_SNPRINTF
  2460. soap_snprintf(soap->type, sizeof(soap->type) - 1, "%s[%d", type, size[0]);
  2461. #else
  2462. sprintf(soap->type, "%s[%d", type, size[0]);
  2463. #endif
  2464. for (i = 1; i < dim; i++)
  2465. {
  2466. #ifdef HAVE_SNPRINTF
  2467. l = strlen(soap->type);
  2468. soap_snprintf(soap->type + l, sizeof(soap->type) - l - 1, " %d", size[i]);
  2469. #else
  2470. if ((l = strlen(soap->type)) + 13 > sizeof(soap->type))
  2471. return NULL;
  2472. sprintf(soap->type + l, " %d", size[i]);
  2473. #endif
  2474. }
  2475. }
  2476. else
  2477. { if (offset)
  2478. {
  2479. #ifdef HAVE_SNPRINTF
  2480. soap_snprintf(soap->type, sizeof(soap->type) - 1, "%s[%d", type, size[0] + offset[0]);
  2481. #else
  2482. sprintf(soap->type, "%s[%d", type, size[0] + offset[0]);
  2483. #endif
  2484. for (i = 1; i < dim; i++)
  2485. {
  2486. #ifdef HAVE_SNPRINTF
  2487. l = strlen(soap->type);
  2488. soap_snprintf(soap->type + l, sizeof(soap->type) - l - 1, ",%d", size[i] + offset[i]);
  2489. #else
  2490. if ((l = strlen(soap->type)) + 13 > sizeof(soap->type))
  2491. return NULL;
  2492. sprintf(soap->type + l, ",%d", size[i] + offset[i]);
  2493. #endif
  2494. }
  2495. }
  2496. else
  2497. {
  2498. #ifdef HAVE_SNPRINTF
  2499. soap_snprintf(soap->type, sizeof(soap->type) - 1, "%s[%d", type, size[0]);
  2500. #else
  2501. sprintf(soap->type, "%s[%d", type, size[0]);
  2502. #endif
  2503. for (i = 1; i < dim; i++)
  2504. {
  2505. #ifdef HAVE_SNPRINTF
  2506. l = strlen(soap->type);
  2507. soap_snprintf(soap->type + l, sizeof(soap->type) - l - 1, ",%d", size[i]);
  2508. #else
  2509. if ((l = strlen(soap->type)) + 13 > sizeof(soap->type))
  2510. return NULL;
  2511. sprintf(soap->type + l, ",%d", size[i]);
  2512. #endif
  2513. }
  2514. }
  2515. }
  2516. strcat(soap->type, "]");
  2517. return soap->type;
  2518. }
  2519. #endif
  2520. /******************************************************************************/
  2521. #ifndef PALM_2
  2522. SOAP_FMAC1
  2523. char *
  2524. SOAP_FMAC2
  2525. soap_putoffset(struct soap *soap, int offset)
  2526. { return soap_putoffsets(soap, &offset, 1);
  2527. }
  2528. #endif
  2529. /******************************************************************************/
  2530. #ifndef PALM_2
  2531. SOAP_FMAC1
  2532. char *
  2533. SOAP_FMAC2
  2534. soap_putoffsets(struct soap *soap, const int *offset, int dim)
  2535. { register int i;
  2536. register size_t l;
  2537. #ifdef HAVE_SNPRINTF
  2538. soap_snprintf(soap->arrayOffset, sizeof(soap->arrayOffset) - 1, "[%d", offset[0]);
  2539. #else
  2540. if (sizeof(soap->arrayOffset) < 13) /* prevent overruns */
  2541. return NULL;
  2542. sprintf(soap->arrayOffset, "[%d", offset[0]);
  2543. #endif
  2544. for (i = 1; i < dim; i++)
  2545. {
  2546. #ifdef HAVE_SNPRINTF
  2547. l = strlen(soap->arrayOffset);
  2548. soap_snprintf(soap->arrayOffset + l, sizeof(soap->arrayOffset) - l - 1, ",%d", offset[i]);
  2549. #else
  2550. if ((l = strlen(soap->arrayOffset)) + 13 > sizeof(soap->arrayOffset))
  2551. return NULL;
  2552. sprintf(soap->arrayOffset + l, ",%d", offset[i]);
  2553. #endif
  2554. }
  2555. strcat(soap->arrayOffset, "]");
  2556. return soap->arrayOffset;
  2557. }
  2558. #endif
  2559. /******************************************************************************/
  2560. #ifndef PALM_2
  2561. SOAP_FMAC1
  2562. int
  2563. SOAP_FMAC2
  2564. soap_size(const int *size, int dim)
  2565. { register int i, n = size[0];
  2566. for (i = 1; i < dim; i++)
  2567. n *= size[i];
  2568. return n;
  2569. }
  2570. #endif
  2571. /******************************************************************************/
  2572. #ifndef PALM_2
  2573. SOAP_FMAC1
  2574. int
  2575. SOAP_FMAC2
  2576. soap_getoffsets(const char *attr, const int *size, int *offset, int dim)
  2577. { register int i, j = 0;
  2578. if (offset)
  2579. for (i = 0; i < dim && attr && *attr; i++)
  2580. { attr++;
  2581. j *= size[i];
  2582. j += offset[i] = (int)soap_strtol(attr, NULL, 10);
  2583. attr = strchr(attr, ',');
  2584. }
  2585. else
  2586. for (i = 0; i < dim && attr && *attr; i++)
  2587. { attr++;
  2588. j *= size[i];
  2589. j += (int)soap_strtol(attr, NULL, 10);
  2590. attr = strchr(attr, ',');
  2591. }
  2592. return j;
  2593. }
  2594. #endif
  2595. /******************************************************************************/
  2596. #ifndef PALM_2
  2597. SOAP_FMAC1
  2598. int
  2599. SOAP_FMAC2
  2600. soap_getsize(const char *attr1, const char *attr2, int *j)
  2601. { register int n, k;
  2602. char *s;
  2603. *j = 0;
  2604. if (!*attr1)
  2605. return -1;
  2606. if (*attr1 == '[')
  2607. attr1++;
  2608. n = 1;
  2609. for (;;)
  2610. { k = (int)soap_strtol(attr1, &s, 10);
  2611. n *= k;
  2612. if (k < 0 || n > SOAP_MAXARRAYSIZE || s == attr1)
  2613. return -1;
  2614. attr1 = strchr(s, ',');
  2615. if (!attr1)
  2616. attr1 = strchr(s, ' ');
  2617. if (attr2 && *attr2)
  2618. { attr2++;
  2619. *j *= k;
  2620. k = (int)soap_strtol(attr2, &s, 10);
  2621. *j += k;
  2622. if (k < 0)
  2623. return -1;
  2624. attr2 = s;
  2625. }
  2626. if (!attr1)
  2627. break;
  2628. attr1++;
  2629. }
  2630. return n - *j;
  2631. }
  2632. #endif
  2633. /******************************************************************************/
  2634. #ifndef PALM_2
  2635. SOAP_FMAC1
  2636. int
  2637. SOAP_FMAC2
  2638. soap_getsizes(const char *attr, int *size, int dim)
  2639. { register int i, k, n;
  2640. if (!*attr)
  2641. return -1;
  2642. i = (int)strlen(attr);
  2643. n = 1;
  2644. do
  2645. { for (i = i-1; i >= 0; i--)
  2646. if (attr[i] == '[' || attr[i] == ',' || attr[i] == ' ')
  2647. break;
  2648. k = (int)soap_strtol(attr + i + 1, NULL, 10);
  2649. n *= size[--dim] = k;
  2650. if (k < 0 || n > SOAP_MAXARRAYSIZE)
  2651. return -1;
  2652. } while (i >= 0 && attr[i] != '[');
  2653. return n;
  2654. }
  2655. #endif
  2656. /******************************************************************************/
  2657. #ifndef PALM_2
  2658. SOAP_FMAC1
  2659. int
  2660. SOAP_FMAC2
  2661. soap_getposition(const char *attr, int *pos)
  2662. { register int i, n;
  2663. if (!*attr)
  2664. return -1;
  2665. n = 0;
  2666. i = 1;
  2667. do
  2668. { pos[n++] = (int)soap_strtol(attr + i, NULL, 10);
  2669. while (attr[i] && attr[i] != ',' && attr[i] != ']')
  2670. i++;
  2671. if (attr[i] == ',')
  2672. i++;
  2673. } while (n < SOAP_MAXDIMS && attr[i] && attr[i] != ']');
  2674. return n;
  2675. }
  2676. #endif
  2677. /******************************************************************************/
  2678. #ifndef PALM_2
  2679. SOAP_FMAC1
  2680. struct soap_nlist *
  2681. SOAP_FMAC2
  2682. soap_push_namespace(struct soap *soap, const char *id, const char *ns)
  2683. { register struct soap_nlist *np;
  2684. register struct Namespace *p;
  2685. register short i = -1;
  2686. register size_t n, k;
  2687. n = strlen(id);
  2688. k = strlen(ns) + 1;
  2689. p = soap->local_namespaces;
  2690. if (p)
  2691. { for (i = 0; p->id; p++, i++)
  2692. { if (p->ns && !strcmp(ns, p->ns))
  2693. break;
  2694. if (p->out)
  2695. { if (!strcmp(ns, p->out))
  2696. break;
  2697. }
  2698. else if (p->in)
  2699. { if (!soap_tag_cmp(ns, p->in))
  2700. { if ((p->out = (char*)SOAP_MALLOC(soap, k)))
  2701. strcpy(p->out, ns);
  2702. break;
  2703. }
  2704. }
  2705. }
  2706. if (!p || !p->id)
  2707. i = -1;
  2708. }
  2709. if (i >= 0)
  2710. k = 0;
  2711. np = (struct soap_nlist*)SOAP_MALLOC(soap, sizeof(struct soap_nlist) + n + k);
  2712. if (!np)
  2713. { soap->error = SOAP_EOM;
  2714. return NULL;
  2715. }
  2716. np->next = soap->nlist;
  2717. soap->nlist = np;
  2718. np->level = soap->level;
  2719. np->index = i;
  2720. strcpy((char*)np->id, id);
  2721. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Push namespace binding (level=%u) '%s' '%s'\n", soap->level, id, ns));
  2722. if (i < 0)
  2723. { np->ns = strcpy((char*)np->id + n + 1, ns);
  2724. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Push NOT OK: no match found for '%s' in namespace mapping table (added to stack anyway)\n", ns));
  2725. }
  2726. else
  2727. { np->ns = NULL;
  2728. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Push OK ('%s' matches '%s' in namespace table)\n", id, p->id));
  2729. }
  2730. return np;
  2731. }
  2732. #endif
  2733. /******************************************************************************/
  2734. #ifndef PALM_2
  2735. SOAP_FMAC1
  2736. void
  2737. SOAP_FMAC2
  2738. soap_pop_namespace(struct soap *soap)
  2739. { register struct soap_nlist *np, *nq;
  2740. for (np = soap->nlist; np && np->level >= soap->level; np = nq)
  2741. { nq = np->next;
  2742. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Pop namespace binding (level=%u) '%s'\n", soap->level, np->id));
  2743. SOAP_FREE(soap, np);
  2744. }
  2745. soap->nlist = np;
  2746. }
  2747. #endif
  2748. /******************************************************************************/
  2749. #ifndef PALM_2
  2750. SOAP_FMAC1
  2751. int
  2752. SOAP_FMAC2
  2753. soap_match_namespace(struct soap *soap, const char *id1, const char *id2, size_t n1, size_t n2)
  2754. { register struct soap_nlist *np = soap->nlist;
  2755. const char *s;
  2756. while (np && (strncmp(np->id, id1, n1) || np->id[n1]))
  2757. np = np->next;
  2758. if (np)
  2759. { if (!(soap->mode & SOAP_XML_IGNORENS))
  2760. if (np->index < 0
  2761. || ((s = soap->local_namespaces[np->index].id) && (strncmp(s, id2, n2) || (s[n2] && s[n2] != '_'))))
  2762. return SOAP_NAMESPACE;
  2763. return SOAP_OK;
  2764. }
  2765. if (n1 == 0)
  2766. return (soap->mode & SOAP_XML_IGNORENS) ? SOAP_OK : SOAP_NAMESPACE;
  2767. if ((n1 == 3 && n1 == n2 && !strncmp(id1, "xml", 3) && !strncmp(id1, id2, 3))
  2768. || (soap->mode & SOAP_XML_IGNORENS))
  2769. return SOAP_OK;
  2770. return soap->error = SOAP_SYNTAX_ERROR;
  2771. }
  2772. #endif
  2773. /******************************************************************************/
  2774. #ifndef PALM_2
  2775. SOAP_FMAC1
  2776. const char*
  2777. SOAP_FMAC2
  2778. soap_current_namespace(struct soap *soap, const char *tag)
  2779. { register struct soap_nlist *np;
  2780. register const char *s;
  2781. if (!tag || !strncmp(tag, "xml", 3))
  2782. return NULL;
  2783. np = soap->nlist;
  2784. if (!(s = strchr(tag, ':')))
  2785. { while (np && *np->id) /* find default namespace, if present */
  2786. np = np->next;
  2787. }
  2788. else
  2789. { while (np && (strncmp(np->id, tag, s - tag) || np->id[s - tag]))
  2790. np = np->next;
  2791. if (!np)
  2792. soap->error = SOAP_NAMESPACE;
  2793. }
  2794. if (np)
  2795. { if (np->index >= 0)
  2796. return soap->namespaces[np->index].ns;
  2797. if (np->ns)
  2798. return soap_strdup(soap, np->ns);
  2799. }
  2800. return NULL;
  2801. }
  2802. #endif
  2803. /******************************************************************************/
  2804. #ifndef PALM_2
  2805. SOAP_FMAC1
  2806. int
  2807. SOAP_FMAC2
  2808. soap_tag_cmp(const char *s, const char *t)
  2809. { for (;;)
  2810. { register int c1 = *s;
  2811. register int c2 = *t;
  2812. if (!c1 || c1 == '"')
  2813. break;
  2814. if (c2 != '-')
  2815. { if (c1 != c2)
  2816. { if (c1 >= 'A' && c1 <= 'Z')
  2817. c1 += 'a' - 'A';
  2818. if (c2 >= 'A' && c2 <= 'Z')
  2819. c2 += 'a' - 'A';
  2820. }
  2821. if (c1 != c2)
  2822. { if (c2 != '*')
  2823. return 1;
  2824. c2 = *++t;
  2825. if (!c2)
  2826. return 0;
  2827. if (c2 >= 'A' && c2 <= 'Z')
  2828. c2 += 'a' - 'A';
  2829. for (;;)
  2830. { c1 = *s;
  2831. if (!c1 || c1 == '"')
  2832. break;
  2833. if (c1 >= 'A' && c1 <= 'Z')
  2834. c1 += 'a' - 'A';
  2835. if (c1 == c2 && !soap_tag_cmp(s + 1, t + 1))
  2836. return 0;
  2837. s++;
  2838. }
  2839. break;
  2840. }
  2841. }
  2842. s++;
  2843. t++;
  2844. }
  2845. if (*t == '*' && !t[1])
  2846. return 0;
  2847. return *t;
  2848. }
  2849. #endif
  2850. /******************************************************************************/
  2851. #ifndef PALM_2
  2852. SOAP_FMAC1
  2853. int
  2854. SOAP_FMAC2
  2855. soap_match_tag(struct soap *soap, const char *tag1, const char *tag2)
  2856. { register const char *s, *t;
  2857. register int err;
  2858. if (!tag1 || !tag2 || !*tag2)
  2859. return SOAP_OK;
  2860. s = strchr(tag1, ':');
  2861. t = strchr(tag2, ':');
  2862. if (t)
  2863. { if (s)
  2864. { if (t[1] && SOAP_STRCMP(s + 1, t + 1))
  2865. return SOAP_TAG_MISMATCH;
  2866. if (t != tag2 && (err = soap_match_namespace(soap, tag1, tag2, s - tag1, t - tag2)))
  2867. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags '%s' and '%s' match but namespaces differ\n", tag1, tag2));
  2868. if (err == SOAP_NAMESPACE)
  2869. return SOAP_TAG_MISMATCH;
  2870. return err;
  2871. }
  2872. }
  2873. else if (!t[1])
  2874. { err = soap_match_namespace(soap, tag1, tag2, 0, t - tag2);
  2875. if (err == SOAP_NAMESPACE)
  2876. return SOAP_TAG_MISMATCH;
  2877. }
  2878. else if (SOAP_STRCMP(tag1, t + 1))
  2879. { return SOAP_TAG_MISMATCH;
  2880. }
  2881. else if (t != tag2 && (err = soap_match_namespace(soap, tag1, tag2, 0, t - tag2)))
  2882. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags '%s' and '%s' match but namespaces differ\n", tag1, tag2));
  2883. if (err == SOAP_NAMESPACE)
  2884. return SOAP_TAG_MISMATCH;
  2885. return err;
  2886. }
  2887. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags and (default) namespaces match: '%s' '%s'\n", tag1, tag2));
  2888. return SOAP_OK;
  2889. }
  2890. if (s)
  2891. { if (SOAP_STRCMP(s + 1, tag2))
  2892. return SOAP_TAG_MISMATCH;
  2893. }
  2894. else if (SOAP_STRCMP(tag1, tag2))
  2895. return SOAP_TAG_MISMATCH;
  2896. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags match: '%s' '%s'\n", tag1, tag2));
  2897. return SOAP_OK;
  2898. }
  2899. #endif
  2900. /******************************************************************************/
  2901. #ifndef PALM_2
  2902. SOAP_FMAC1
  2903. int
  2904. SOAP_FMAC2
  2905. soap_match_array(struct soap *soap, const char *type)
  2906. { if (*soap->arrayType)
  2907. if (soap_match_tag(soap, soap->arrayType, type)
  2908. && soap_match_tag(soap, soap->arrayType, "xsd:anyType")
  2909. && soap_match_tag(soap, soap->arrayType, "xsd:ur-type")
  2910. )
  2911. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array type mismatch: '%s' '%s'\n", soap->arrayType, type));
  2912. return SOAP_TAG_MISMATCH;
  2913. }
  2914. return SOAP_OK;
  2915. }
  2916. #endif
  2917. /******************************************************************************\
  2918. *
  2919. * SSL/TLS
  2920. *
  2921. \******************************************************************************/
  2922. /******************************************************************************/
  2923. #ifdef WITH_OPENSSL
  2924. #ifndef PALM_2
  2925. SOAP_FMAC1
  2926. int
  2927. SOAP_FMAC2
  2928. soap_rand()
  2929. { unsigned char buf[4];
  2930. if (!soap_ssl_init_done)
  2931. soap_ssl_init();
  2932. RAND_pseudo_bytes(buf, 4);
  2933. return *(int*)buf;
  2934. }
  2935. #endif
  2936. #endif
  2937. /******************************************************************************/
  2938. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  2939. #ifndef PALM_2
  2940. SOAP_FMAC1
  2941. int
  2942. SOAP_FMAC2
  2943. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  2944. soap_ssl_server_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *keyid, const char *password, const char *cafile, const char *capath, const char *dhfile, const char *randfile, const char *sid)
  2945. #else
  2946. soap_ssl_server_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *password, const char *cafile, const char *capath, const char *dhfile, const char *randfile, const char *sid)
  2947. #endif
  2948. { int err;
  2949. soap->keyfile = keyfile;
  2950. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  2951. soap->keyid = keyid;
  2952. #endif
  2953. soap->password = password;
  2954. soap->cafile = cafile;
  2955. soap->capath = capath;
  2956. soap->crlfile = NULL;
  2957. #ifdef WITH_OPENSSL
  2958. soap->dhfile = dhfile;
  2959. soap->randfile = randfile;
  2960. #endif
  2961. soap->ssl_flags = flags | (dhfile == NULL ? SOAP_SSL_RSA : 0);
  2962. #ifdef WITH_GNUTLS
  2963. if (dhfile)
  2964. { char *s;
  2965. int n = (int)soap_strtoul(dhfile, &s, 10);
  2966. if (!soap->dh_params)
  2967. gnutls_dh_params_init(&soap->dh_params);
  2968. /* if dhfile is numeric, treat it as a key length to generate DH params which can take a while */
  2969. if (n >= 512 && s && *s == '\0')
  2970. gnutls_dh_params_generate2(soap->dh_params, (unsigned int)n);
  2971. else
  2972. { unsigned int dparams_len;
  2973. unsigned char dparams_buf[1024];
  2974. FILE *fd = fopen(dhfile, "r");
  2975. if (!fd)
  2976. return soap_set_receiver_error(soap, "SSL/TLS error", "Invalid DH file", SOAP_SSL_ERROR);
  2977. dparams_len = (unsigned int)fread(dparams_buf, 1, sizeof(dparams_buf), fd);
  2978. fclose(fd);
  2979. gnutls_datum_t dparams = { dparams_buf, dparams_len };
  2980. if (gnutls_dh_params_import_pkcs3(soap->dh_params, &dparams, GNUTLS_X509_FMT_PEM))
  2981. return soap_set_receiver_error(soap, "SSL/TLS error", "Invalid DH file", SOAP_SSL_ERROR);
  2982. }
  2983. }
  2984. else
  2985. { if (!soap->rsa_params)
  2986. gnutls_rsa_params_init(&soap->rsa_params);
  2987. gnutls_rsa_params_generate2(soap->rsa_params, SOAP_SSL_RSA_BITS);
  2988. }
  2989. if (soap->session)
  2990. { gnutls_deinit(soap->session);
  2991. soap->session = NULL;
  2992. }
  2993. if (soap->xcred)
  2994. { gnutls_certificate_free_credentials(soap->xcred);
  2995. soap->xcred = NULL;
  2996. }
  2997. #endif
  2998. err = soap->fsslauth(soap);
  2999. #ifdef WITH_OPENSSL
  3000. if (!err)
  3001. { if (sid)
  3002. SSL_CTX_set_session_id_context(soap->ctx, (unsigned char*)sid, (unsigned int)strlen(sid));
  3003. else
  3004. SSL_CTX_set_session_cache_mode(soap->ctx, SSL_SESS_CACHE_OFF);
  3005. }
  3006. #endif
  3007. return err;
  3008. }
  3009. #endif
  3010. #endif
  3011. /******************************************************************************/
  3012. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3013. #ifndef PALM_2
  3014. SOAP_FMAC1
  3015. int
  3016. SOAP_FMAC2
  3017. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3018. soap_ssl_client_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *keyid, const char *password, const char *cafile, const char *capath, const char *randfile)
  3019. #else
  3020. soap_ssl_client_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *password, const char *cafile, const char *capath, const char *randfile)
  3021. #endif
  3022. { soap->keyfile = keyfile;
  3023. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3024. soap->keyid = keyid;
  3025. #endif
  3026. soap->password = password;
  3027. soap->cafile = cafile;
  3028. soap->capath = capath;
  3029. soap->ssl_flags = SOAP_SSL_CLIENT | flags;
  3030. #ifdef WITH_OPENSSL
  3031. soap->dhfile = NULL;
  3032. soap->randfile = randfile;
  3033. soap->fsslverify = (flags & SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE) == 0 ? ssl_verify_callback : ssl_verify_callback_allow_expired_certificate;
  3034. #endif
  3035. #ifdef WITH_GNUTLS
  3036. if (soap->session)
  3037. { gnutls_deinit(soap->session);
  3038. soap->session = NULL;
  3039. }
  3040. if (soap->xcred)
  3041. { gnutls_certificate_free_credentials(soap->xcred);
  3042. soap->xcred = NULL;
  3043. }
  3044. #endif
  3045. return soap->fsslauth(soap);
  3046. }
  3047. #endif
  3048. #endif
  3049. /******************************************************************************/
  3050. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3051. #ifndef PALM_2
  3052. SOAP_FMAC1
  3053. void
  3054. SOAP_FMAC2
  3055. soap_ssl_init()
  3056. { /* Note: for MT systems, the main program MUST call soap_ssl_init() before any threads are started */
  3057. if (!soap_ssl_init_done)
  3058. { soap_ssl_init_done = 1;
  3059. #ifdef WITH_OPENSSL
  3060. SSL_library_init();
  3061. OpenSSL_add_all_algorithms(); /* 2.8.1 change (wsseapi.c) */
  3062. OpenSSL_add_all_digests();
  3063. #ifndef WITH_LEAN
  3064. SSL_load_error_strings();
  3065. #endif
  3066. if (!RAND_load_file("/dev/urandom", 1024))
  3067. { char buf[1024];
  3068. RAND_seed(buf, sizeof(buf));
  3069. while (!RAND_status())
  3070. { int r = rand();
  3071. RAND_seed(&r, sizeof(int));
  3072. }
  3073. }
  3074. #endif
  3075. #ifdef WITH_GNUTLS
  3076. # if defined(HAVE_PTHREAD_H)
  3077. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  3078. # elif defined(HAVE_PTH_H)
  3079. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
  3080. # endif
  3081. gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  3082. gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
  3083. gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); /* libgcrypt init done */
  3084. gnutls_global_init();
  3085. #endif
  3086. }
  3087. }
  3088. #endif
  3089. #endif
  3090. /******************************************************************************/
  3091. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3092. #ifndef PALM_1
  3093. SOAP_FMAC1
  3094. const char *
  3095. SOAP_FMAC2
  3096. soap_ssl_error(struct soap *soap, int ret)
  3097. {
  3098. #ifdef WITH_OPENSSL
  3099. int err = SSL_get_error(soap->ssl, ret);
  3100. const char *msg = soap_code_str(h_ssl_error_codes, err);
  3101. if (msg)
  3102. strcpy(soap->msgbuf, msg);
  3103. else
  3104. return ERR_error_string(err, soap->msgbuf);
  3105. if (ERR_peek_error())
  3106. { unsigned long r;
  3107. strcat(soap->msgbuf, "\n");
  3108. while ((r = ERR_get_error()))
  3109. ERR_error_string_n(r, soap->msgbuf + strlen(soap->msgbuf), sizeof(soap->msgbuf) - strlen(soap->msgbuf));
  3110. }
  3111. else
  3112. { switch (ret)
  3113. { case 0:
  3114. strcpy(soap->msgbuf, "EOF was observed that violates the SSL/TLS protocol. The client probably provided invalid authentication information.");
  3115. break;
  3116. case -1:
  3117. #ifdef HAVE_SNPRINTF
  3118. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Error observed by underlying SSL/TLS BIO: %s", strerror(errno));
  3119. #else
  3120. { const char *s = strerror(errno);
  3121. size_t l = strlen(s);
  3122. sprintf(soap->msgbuf, "Error observed by underlying SSL/TLS BIO: %s", l + 44 < sizeof(soap->msgbuf) ? s : SOAP_STR_EOS);
  3123. }
  3124. #endif
  3125. break;
  3126. }
  3127. }
  3128. return soap->msgbuf;
  3129. #endif
  3130. #ifdef WITH_GNUTLS
  3131. return gnutls_strerror(ret);
  3132. #endif
  3133. }
  3134. #endif
  3135. #endif
  3136. /******************************************************************************/
  3137. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3138. #ifndef PALM_1
  3139. static int
  3140. ssl_auth_init(struct soap *soap)
  3141. {
  3142. #ifdef WITH_OPENSSL
  3143. long flags;
  3144. int mode;
  3145. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3146. EVP_PKEY* pkey;
  3147. #endif
  3148. if (!soap_ssl_init_done)
  3149. soap_ssl_init();
  3150. ERR_clear_error();
  3151. if (!soap->ctx)
  3152. { if (!(soap->ctx = SSL_CTX_new(SSLv23_method())))
  3153. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't setup context", SOAP_SSL_ERROR);
  3154. /* The following alters the behavior of SSL read/write: */
  3155. #if 0
  3156. SSL_CTX_set_mode(soap->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_AUTO_RETRY);
  3157. #endif
  3158. }
  3159. if (soap->randfile)
  3160. { if (!RAND_load_file(soap->randfile, -1))
  3161. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't load randomness", SOAP_SSL_ERROR);
  3162. }
  3163. if (soap->cafile || soap->capath)
  3164. { if (!SSL_CTX_load_verify_locations(soap->ctx, soap->cafile, soap->capath))
  3165. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CA file", SOAP_SSL_ERROR);
  3166. if (soap->cafile && (soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3167. SSL_CTX_set_client_CA_list(soap->ctx, SSL_load_client_CA_file(soap->cafile));
  3168. }
  3169. if (!(soap->ssl_flags & SOAP_SSL_NO_DEFAULT_CA_PATH))
  3170. { if (!SSL_CTX_set_default_verify_paths(soap->ctx))
  3171. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read default CA file and/or directory", SOAP_SSL_ERROR);
  3172. }
  3173. /* This code assumes a typical scenario, see alternative code below */
  3174. if (soap->keyfile)
  3175. { if (!SSL_CTX_use_certificate_chain_file(soap->ctx, soap->keyfile))
  3176. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read certificate key file", SOAP_SSL_ERROR);
  3177. if (soap->password)
  3178. { SSL_CTX_set_default_passwd_cb_userdata(soap->ctx, (void*)soap->password);
  3179. SSL_CTX_set_default_passwd_cb(soap->ctx, ssl_password);
  3180. }
  3181. if (!SSL_CTX_use_PrivateKey_file(soap->ctx, soap->keyfile, SSL_FILETYPE_PEM))
  3182. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read key file", SOAP_SSL_ERROR);
  3183. #ifndef WM_SECURE_KEY_STORAGE
  3184. if (!SSL_CTX_use_PrivateKey_file(soap->ctx, soap->keyfile, SSL_FILETYPE_PEM))
  3185. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read key file", SOAP_SSL_ERROR);
  3186. #endif
  3187. }
  3188. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3189. if (NULL == (pkey = ipcom_key_db_pkey_get(soap->keyid)))
  3190. return soap_set_receiver_error(soap, "SSL error", "Can't find key", SOAP_SSL_ERROR);
  3191. if (0 == SSL_CTX_use_PrivateKey(soap->ctx, pkey))
  3192. return soap_set_receiver_error(soap, "SSL error", "Can't read key", SOAP_SSL_ERROR);
  3193. #endif
  3194. /* Suggested alternative approach to check the key file for certs (cafile=NULL):*/
  3195. #if 0
  3196. if (soap->password)
  3197. { SSL_CTX_set_default_passwd_cb_userdata(soap->ctx, (void*)soap->password);
  3198. SSL_CTX_set_default_passwd_cb(soap->ctx, ssl_password);
  3199. }
  3200. if (!soap->cafile || !SSL_CTX_use_certificate_chain_file(soap->ctx, soap->cafile))
  3201. { if (soap->keyfile)
  3202. { if (!SSL_CTX_use_certificate_chain_file(soap->ctx, soap->keyfile))
  3203. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read certificate or key file", SOAP_SSL_ERROR);
  3204. if (!SSL_CTX_use_PrivateKey_file(soap->ctx, soap->keyfile, SSL_FILETYPE_PEM))
  3205. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read key file", SOAP_SSL_ERROR);
  3206. }
  3207. }
  3208. #endif
  3209. if ((soap->ssl_flags & SOAP_SSL_RSA))
  3210. { RSA *rsa = RSA_generate_key(SOAP_SSL_RSA_BITS, RSA_F4, NULL, NULL);
  3211. if (!SSL_CTX_set_tmp_rsa(soap->ctx, rsa))
  3212. { if (rsa)
  3213. RSA_free(rsa);
  3214. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set RSA key", SOAP_SSL_ERROR);
  3215. }
  3216. RSA_free(rsa);
  3217. }
  3218. else if (soap->dhfile)
  3219. { DH *dh = 0;
  3220. char *s;
  3221. int n = (int)soap_strtoul(soap->dhfile, &s, 10);
  3222. /* if dhfile is numeric, treat it as a key length to generate DH params which can take a while */
  3223. if (n >= 512 && s && *s == '\0')
  3224. #if defined(VXWORKS)
  3225. DH_generate_parameters_ex(dh, n, 2/*or 5*/, NULL);
  3226. #else
  3227. dh = DH_generate_parameters(n, 2/*or 5*/, NULL, NULL);
  3228. #endif
  3229. else
  3230. { BIO *bio;
  3231. bio = BIO_new_file(soap->dhfile, "r");
  3232. if (!bio)
  3233. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read DH file", SOAP_SSL_ERROR);
  3234. dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  3235. BIO_free(bio);
  3236. }
  3237. if (!dh || DH_check(dh, &n) != 1 || SSL_CTX_set_tmp_dh(soap->ctx, dh) < 0)
  3238. { if (dh)
  3239. DH_free(dh);
  3240. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set DH parameters", SOAP_SSL_ERROR);
  3241. }
  3242. DH_free(dh);
  3243. }
  3244. flags = (SSL_OP_ALL | SSL_OP_NO_SSLv2); /* disable SSL v2 */
  3245. if ((soap->ssl_flags & SOAP_SSLv3))
  3246. flags |= SSL_OP_NO_TLSv1;
  3247. if ((soap->ssl_flags & SOAP_TLSv1))
  3248. flags |= SSL_OP_NO_SSLv3;
  3249. #ifdef SSL_OP_NO_TICKET
  3250. /* TLS extension is enabled by default in OPENSSL v0.9.8k
  3251. Disable it by adding SSL_OP_NO_TICKET */
  3252. flags |= SSL_OP_NO_TICKET;
  3253. #endif
  3254. SSL_CTX_set_options(soap->ctx, flags);
  3255. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3256. mode = (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  3257. else if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  3258. mode = SSL_VERIFY_PEER;
  3259. else
  3260. mode = SSL_VERIFY_NONE;
  3261. SSL_CTX_set_verify(soap->ctx, mode, soap->fsslverify);
  3262. #if (OPENSSL_VERSION_NUMBER < 0x00905100L)
  3263. SSL_CTX_set_verify_depth(soap->ctx, 1);
  3264. #else
  3265. SSL_CTX_set_verify_depth(soap->ctx, 9);
  3266. #endif
  3267. #endif
  3268. #ifdef WITH_GNUTLS
  3269. int ret;
  3270. if (!soap_ssl_init_done)
  3271. soap_ssl_init();
  3272. if (!soap->xcred)
  3273. { gnutls_certificate_allocate_credentials(&soap->xcred);
  3274. if (soap->cafile)
  3275. { if (gnutls_certificate_set_x509_trust_file(soap->xcred, soap->cafile, GNUTLS_X509_FMT_PEM) < 0)
  3276. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CA file", SOAP_SSL_ERROR);
  3277. }
  3278. if (soap->crlfile)
  3279. { if (gnutls_certificate_set_x509_crl_file(soap->xcred, soap->crlfile, GNUTLS_X509_FMT_PEM) < 0)
  3280. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CRL file", SOAP_SSL_ERROR);
  3281. }
  3282. if (soap->keyfile)
  3283. { if (gnutls_certificate_set_x509_key_file(soap->xcred, soap->keyfile, soap->keyfile, GNUTLS_X509_FMT_PEM) < 0) /* TODO: GNUTLS need to concat cert and key in single key file */
  3284. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read key file", SOAP_SSL_ERROR);
  3285. }
  3286. }
  3287. if ((soap->ssl_flags & SOAP_SSL_CLIENT))
  3288. { gnutls_init(&soap->session, GNUTLS_CLIENT);
  3289. if (soap->cafile || soap->crlfile || soap->keyfile)
  3290. { ret = gnutls_priority_set_direct(soap->session, "PERFORMANCE", NULL);
  3291. if (ret < 0)
  3292. return soap_set_receiver_error(soap, soap_ssl_error(soap, ret), "SSL/TLS set priority error", SOAP_SSL_ERROR);
  3293. gnutls_credentials_set(soap->session, GNUTLS_CRD_CERTIFICATE, soap->xcred);
  3294. }
  3295. else
  3296. { if (!soap->acred)
  3297. gnutls_anon_allocate_client_credentials(&soap->acred);
  3298. gnutls_init(&soap->session, GNUTLS_CLIENT);
  3299. gnutls_priority_set_direct(soap->session, "PERFORMANCE:+ANON-DH:!ARCFOUR-128", NULL);
  3300. gnutls_credentials_set(soap->session, GNUTLS_CRD_ANON, soap->acred);
  3301. }
  3302. }
  3303. else
  3304. { if (!soap->keyfile)
  3305. return soap_set_receiver_error(soap, "SSL/TLS error", "No key file: anonymous server authentication not supported in this release", SOAP_SSL_ERROR);
  3306. if ((soap->ssl_flags & SOAP_SSL_RSA) && soap->rsa_params)
  3307. gnutls_certificate_set_rsa_export_params(soap->xcred, soap->rsa_params);
  3308. else if (soap->dh_params)
  3309. gnutls_certificate_set_dh_params(soap->xcred, soap->dh_params);
  3310. if (!soap->cache)
  3311. gnutls_priority_init(&soap->cache, "NORMAL", NULL);
  3312. gnutls_init(&soap->session, GNUTLS_SERVER);
  3313. gnutls_priority_set(soap->session, soap->cache);
  3314. gnutls_credentials_set(soap->session, GNUTLS_CRD_CERTIFICATE, soap->xcred);
  3315. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3316. gnutls_certificate_server_set_request(soap->session, GNUTLS_CERT_REQUEST);
  3317. gnutls_session_enable_compatibility_mode(soap->session);
  3318. if ((soap->ssl_flags & SOAP_TLSv1))
  3319. { int protocol_priority[] = { GNUTLS_TLS1_0, 0 };
  3320. if (gnutls_protocol_set_priority(soap->session, protocol_priority) != GNUTLS_E_SUCCESS)
  3321. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set TLS v1.0 protocol", SOAP_SSL_ERROR);
  3322. }
  3323. }
  3324. #endif
  3325. return SOAP_OK;
  3326. }
  3327. #endif
  3328. #endif
  3329. /******************************************************************************/
  3330. #ifdef WITH_OPENSSL
  3331. #ifndef PALM_1
  3332. static int
  3333. ssl_password(char *buf, int num, int rwflag, void *userdata)
  3334. { if (num < (int)strlen((char*)userdata) + 1)
  3335. return 0;
  3336. return (int)strlen(strcpy(buf, (char*)userdata));
  3337. }
  3338. #endif
  3339. #endif
  3340. /******************************************************************************/
  3341. #ifdef WITH_OPENSSL
  3342. #ifndef PALM_1
  3343. static int
  3344. ssl_verify_callback(int ok, X509_STORE_CTX *store)
  3345. {
  3346. #ifdef SOAP_DEBUG
  3347. if (!ok)
  3348. { char buf[1024];
  3349. int err = X509_STORE_CTX_get_error(store);
  3350. X509 *cert = X509_STORE_CTX_get_current_cert(store);
  3351. fprintf(stderr, "SSL verify error or warning with certificate at depth %d: %s\n", X509_STORE_CTX_get_error_depth(store), X509_verify_cert_error_string(err));
  3352. X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf));
  3353. fprintf(stderr, "certificate issuer %s\n", buf);
  3354. X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
  3355. fprintf(stderr, "certificate subject %s\n", buf);
  3356. /* accept self signed certificates and certificates out of date */
  3357. switch (err)
  3358. { case X509_V_ERR_CERT_NOT_YET_VALID:
  3359. case X509_V_ERR_CERT_HAS_EXPIRED:
  3360. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  3361. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  3362. X509_STORE_CTX_set_error(store, X509_V_OK);
  3363. ok = 1;
  3364. }
  3365. }
  3366. #endif
  3367. /* Note: return 1 to continue, but unsafe progress will be terminated by OpenSSL */
  3368. return ok;
  3369. }
  3370. #endif
  3371. #endif
  3372. /******************************************************************************/
  3373. #ifdef WITH_OPENSSL
  3374. #ifndef PALM_1
  3375. static int
  3376. ssl_verify_callback_allow_expired_certificate(int ok, X509_STORE_CTX *store)
  3377. { ok = ssl_verify_callback(ok, store);
  3378. if (!ok)
  3379. { /* accept self signed certificates and certificates out of date */
  3380. switch (X509_STORE_CTX_get_error(store))
  3381. { case X509_V_ERR_CERT_NOT_YET_VALID:
  3382. case X509_V_ERR_CERT_HAS_EXPIRED:
  3383. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  3384. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  3385. X509_STORE_CTX_set_error(store, X509_V_OK);
  3386. ok = 1;
  3387. }
  3388. }
  3389. /* Note: return 1 to continue, but unsafe progress will be terminated by SSL */
  3390. return ok;
  3391. }
  3392. #endif
  3393. #endif
  3394. /******************************************************************************/
  3395. #ifdef WITH_GNUTLS
  3396. static const char *
  3397. ssl_verify(struct soap *soap, const char *host)
  3398. { unsigned int status;
  3399. const char *err = NULL;
  3400. int r = gnutls_certificate_verify_peers2(soap->session, &status);
  3401. if (r < 0)
  3402. err = "Certificate verify error";
  3403. else if ((status & GNUTLS_CERT_INVALID))
  3404. err = "The certificate is not trusted";
  3405. else if ((status & GNUTLS_CERT_SIGNER_NOT_FOUND))
  3406. err = "The certificate hasn't got a known issuer";
  3407. else if ((status & GNUTLS_CERT_REVOKED))
  3408. err = "The certificate has been revoked";
  3409. else if (gnutls_certificate_type_get(soap->session) == GNUTLS_CRT_X509)
  3410. { gnutls_x509_crt_t cert;
  3411. const gnutls_datum_t *cert_list;
  3412. unsigned int cert_list_size;
  3413. if (gnutls_x509_crt_init(&cert) < 0)
  3414. err = "Could not get X509 certificates";
  3415. else if ((cert_list = gnutls_certificate_get_peers(soap->session, &cert_list_size)) == NULL)
  3416. err = "Could not get X509 certificates";
  3417. else if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
  3418. err = "Error parsing X509 certificate";
  3419. else if (!(soap->ssl_flags & SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE) && gnutls_x509_crt_get_expiration_time(cert) < time(NULL))
  3420. err = "The certificate has expired";
  3421. else if (!(soap->ssl_flags & SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE) && gnutls_x509_crt_get_activation_time(cert) > time(NULL))
  3422. err = "The certificate is not yet activated";
  3423. else if (host && !(soap->ssl_flags & SOAP_SSL_SKIP_HOST_CHECK))
  3424. { if (!gnutls_x509_crt_check_hostname(cert, host))
  3425. err = "Certificate host name mismatch";
  3426. }
  3427. gnutls_x509_crt_deinit(cert);
  3428. }
  3429. return err;
  3430. }
  3431. #endif
  3432. /******************************************************************************/
  3433. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3434. #ifndef WITH_NOIO
  3435. #ifndef PALM_1
  3436. SOAP_FMAC1
  3437. int
  3438. SOAP_FMAC2
  3439. soap_ssl_accept(struct soap *soap)
  3440. { SOAP_SOCKET sk = soap->socket;
  3441. #ifdef WITH_OPENSSL
  3442. BIO *bio;
  3443. int retries, r, s;
  3444. if (!soap_valid_socket(sk))
  3445. return soap_set_receiver_error(soap, "SSL/TLS error", "No socket in soap_ssl_accept()", SOAP_SSL_ERROR);
  3446. soap->ssl_flags &= ~SOAP_SSL_CLIENT;
  3447. if (!soap->ctx && (soap->error = soap->fsslauth(soap)))
  3448. return soap->error;
  3449. if (!soap->ssl)
  3450. { soap->ssl = SSL_new(soap->ctx);
  3451. if (!soap->ssl)
  3452. return soap_set_receiver_error(soap, "SSL/TLS error", "SSL_new() failed in soap_ssl_accept()", SOAP_SSL_ERROR);
  3453. }
  3454. else
  3455. SSL_clear(soap->ssl);
  3456. bio = BIO_new_socket((int)sk, BIO_NOCLOSE);
  3457. SSL_set_bio(soap->ssl, bio, bio);
  3458. /* Set SSL sockets to non-blocking */
  3459. retries = 0;
  3460. if (soap->accept_timeout)
  3461. { SOAP_SOCKNONBLOCK(sk)
  3462. retries = 10*soap->accept_timeout;
  3463. }
  3464. if (retries <= 0)
  3465. retries = 100; /* timeout: 10 sec retries, 100 times 0.1 sec */
  3466. while ((r = SSL_accept(soap->ssl)) <= 0)
  3467. { int err;
  3468. if (retries-- <= 0)
  3469. break;
  3470. err = SSL_get_error(soap->ssl, r);
  3471. if (err == SSL_ERROR_WANT_ACCEPT || err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
  3472. { if (err == SSL_ERROR_WANT_READ)
  3473. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  3474. else
  3475. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  3476. if (s < 0)
  3477. break;
  3478. }
  3479. else
  3480. { soap->errnum = soap_socket_errno(sk);
  3481. break;
  3482. }
  3483. }
  3484. if (r <= 0)
  3485. { soap_set_receiver_error(soap, soap_ssl_error(soap, r), "SSL_accept() failed in soap_ssl_accept()", SOAP_SSL_ERROR);
  3486. soap_closesock(soap);
  3487. return SOAP_SSL_ERROR;
  3488. }
  3489. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3490. { X509 *peer;
  3491. int err;
  3492. if ((err = SSL_get_verify_result(soap->ssl)) != X509_V_OK)
  3493. { soap_closesock(soap);
  3494. return soap_set_sender_error(soap, X509_verify_cert_error_string(err), "SSL certificate presented by peer cannot be verified in soap_ssl_accept()", SOAP_SSL_ERROR);
  3495. }
  3496. peer = SSL_get_peer_certificate(soap->ssl);
  3497. if (!peer)
  3498. { soap_closesock(soap);
  3499. return soap_set_sender_error(soap, "SSL/TLS error", "No SSL certificate was presented by the peer in soap_ssl_accept()", SOAP_SSL_ERROR);
  3500. }
  3501. X509_free(peer);
  3502. }
  3503. #endif
  3504. #ifdef WITH_GNUTLS
  3505. int retries = 0, r;
  3506. if (!soap_valid_socket(sk))
  3507. return soap_set_receiver_error(soap, "SSL/TLS error", "No socket in soap_ssl_accept()", SOAP_SSL_ERROR);
  3508. soap->ssl_flags &= ~SOAP_SSL_CLIENT;
  3509. if (!soap->session && (soap->error = soap->fsslauth(soap)))
  3510. { soap_closesock(soap);
  3511. return soap->error;
  3512. }
  3513. gnutls_transport_set_ptr(soap->session, (gnutls_transport_ptr_t)(long)sk);
  3514. /* Set SSL sockets to non-blocking */
  3515. if (soap->accept_timeout)
  3516. { SOAP_SOCKNONBLOCK(sk)
  3517. retries = 10*soap->accept_timeout;
  3518. }
  3519. if (retries <= 0)
  3520. retries = 100; /* timeout: 10 sec retries, 100 times 0.1 sec */
  3521. while ((r = gnutls_handshake(soap->session)))
  3522. { int s;
  3523. /* GNUTLS repeat handhake when GNUTLS_E_AGAIN */
  3524. if (retries-- <= 0)
  3525. break;
  3526. if (r == GNUTLS_E_AGAIN || r == GNUTLS_E_INTERRUPTED)
  3527. { if (!gnutls_record_get_direction(soap->session))
  3528. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  3529. else
  3530. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  3531. if (s < 0)
  3532. break;
  3533. }
  3534. else
  3535. { soap->errnum = soap_socket_errno(sk);
  3536. break;
  3537. }
  3538. }
  3539. if (r)
  3540. { soap_closesock(soap);
  3541. return soap_set_receiver_error(soap, soap_ssl_error(soap, r), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  3542. }
  3543. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3544. { const char *err = ssl_verify(soap, NULL);
  3545. if (err)
  3546. { soap_closesock(soap);
  3547. return soap_set_receiver_error(soap, "SSL/TLS error", err, SOAP_SSL_ERROR);
  3548. }
  3549. }
  3550. #endif
  3551. if (soap->recv_timeout || soap->send_timeout)
  3552. SOAP_SOCKNONBLOCK(sk)
  3553. else
  3554. SOAP_SOCKBLOCK(sk)
  3555. soap->imode |= SOAP_ENC_SSL;
  3556. soap->omode |= SOAP_ENC_SSL;
  3557. return SOAP_OK;
  3558. }
  3559. #endif
  3560. #endif
  3561. #endif
  3562. /******************************************************************************\
  3563. *
  3564. * TCP/UDP [SSL/TLS] IPv4 and IPv6
  3565. *
  3566. \******************************************************************************/
  3567. /******************************************************************************/
  3568. #ifndef WITH_NOIO
  3569. #ifndef PALM_1
  3570. static int
  3571. tcp_init(struct soap *soap)
  3572. { soap->errmode = 1;
  3573. #ifdef WIN32
  3574. if (tcp_done)
  3575. return 0;
  3576. else
  3577. { WSADATA w;
  3578. if (WSAStartup(MAKEWORD(1, 1), &w))
  3579. return -1;
  3580. tcp_done = 1;
  3581. }
  3582. #endif
  3583. return 0;
  3584. }
  3585. #endif
  3586. #endif
  3587. /******************************************************************************/
  3588. #ifndef WITH_NOIO
  3589. #ifndef PALM_1
  3590. static const char*
  3591. tcp_error(struct soap *soap)
  3592. { register const char *msg = NULL;
  3593. switch (soap->errmode)
  3594. { case 0:
  3595. msg = soap_strerror(soap);
  3596. break;
  3597. case 1:
  3598. msg = "WSAStartup failed";
  3599. break;
  3600. case 2:
  3601. {
  3602. #ifndef WITH_LEAN
  3603. msg = soap_code_str(h_error_codes, soap->errnum);
  3604. if (!msg)
  3605. #endif
  3606. {
  3607. #ifdef HAVE_SNPRINTF
  3608. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "TCP/UDP IP error %d", soap->errnum);
  3609. #else
  3610. sprintf(soap->msgbuf, "TCP/UDP IP error %d", soap->errnum);
  3611. #endif
  3612. msg = soap->msgbuf;
  3613. }
  3614. }
  3615. }
  3616. return msg;
  3617. }
  3618. #endif
  3619. #endif
  3620. /******************************************************************************/
  3621. #ifndef WITH_IPV6
  3622. #ifndef WITH_NOIO
  3623. #ifndef PALM_1
  3624. static int
  3625. tcp_gethost(struct soap *soap, const char *addr, struct in_addr *inaddr)
  3626. { soap_int32 iadd = -1;
  3627. struct hostent hostent, *host = &hostent;
  3628. #ifdef VXWORKS
  3629. int hostint;
  3630. /* inet_addr(), and hostGetByName() expect "char *"; addr is a "const char *". */
  3631. iadd = inet_addr((char*)addr);
  3632. #else
  3633. #if defined(_AIX43) || ((defined(TRU64) || defined(HP_UX)) && defined(HAVE_GETHOSTBYNAME_R))
  3634. struct hostent_data ht_data;
  3635. #endif
  3636. #ifdef AS400
  3637. iadd = inet_addr((void*)addr);
  3638. #else
  3639. iadd = inet_addr(addr);
  3640. #endif
  3641. #endif
  3642. if (iadd != -1)
  3643. { memcpy(inaddr, &iadd, sizeof(iadd));
  3644. return SOAP_OK;
  3645. }
  3646. #if defined(__GLIBC__) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(FREEBSD) || defined(__FreeBSD__))) || defined(__ANDROID__)
  3647. if (gethostbyname_r(addr, &hostent, soap->buf, SOAP_BUFLEN, &host, &soap->errnum) < 0)
  3648. host = NULL;
  3649. #elif defined(_AIX43) || ((defined(TRU64) || defined(HP_UX)) && defined(HAVE_GETHOSTBYNAME_R))
  3650. memset((void*)&ht_data, 0, sizeof(ht_data));
  3651. if (gethostbyname_r(addr, &hostent, &ht_data) < 0)
  3652. { host = NULL;
  3653. soap->errnum = h_errno;
  3654. }
  3655. #elif defined(HAVE_GETHOSTBYNAME_R)
  3656. host = gethostbyname_r(addr, &hostent, soap->buf, SOAP_BUFLEN, &soap->errnum);
  3657. #elif defined(VXWORKS)
  3658. /* If the DNS resolver library resolvLib has been configured in the vxWorks
  3659. * image, a query for the host IP address is sent to the DNS server, if the
  3660. * name was not found in the local host table. */
  3661. hostint = hostGetByName((char*)addr);
  3662. if (hostint == ERROR)
  3663. { host = NULL;
  3664. soap->errnum = soap_errno;
  3665. }
  3666. #else
  3667. #ifdef AS400
  3668. if (!(host = gethostbyname((void*)addr)))
  3669. soap->errnum = h_errno;
  3670. #else
  3671. if (!(host = gethostbyname(addr)))
  3672. soap->errnum = h_errno;
  3673. #endif
  3674. #endif
  3675. if (!host)
  3676. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Host name not found\n"));
  3677. return SOAP_ERR;
  3678. }
  3679. #ifdef VXWORKS
  3680. inaddr->s_addr = hostint;
  3681. #else
  3682. memcpy(inaddr, host->h_addr, host->h_length);
  3683. #endif
  3684. return SOAP_OK;
  3685. }
  3686. #endif
  3687. #endif
  3688. #endif
  3689. /******************************************************************************/
  3690. #ifndef WITH_NOIO
  3691. #ifndef PALM_1
  3692. static SOAP_SOCKET
  3693. tcp_connect(struct soap *soap, const char *endpoint, const char *host, int port)
  3694. {
  3695. #ifdef WITH_IPV6
  3696. struct addrinfo hints, *res, *ressave;
  3697. #endif
  3698. SOAP_SOCKET sk;
  3699. int err = 0;
  3700. #ifndef WITH_LEAN
  3701. #ifndef WIN32
  3702. int len = SOAP_BUFLEN;
  3703. #else
  3704. int len = SOAP_BUFLEN + 1; /* speeds up windows xfer */
  3705. #endif
  3706. int set = 1;
  3707. #endif
  3708. #if !defined(WITH_LEAN) || defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3709. int retries;
  3710. #endif
  3711. if (soap_valid_socket(soap->socket))
  3712. soap->fclosesocket(soap, soap->socket);
  3713. soap->socket = SOAP_INVALID_SOCKET;
  3714. if (tcp_init(soap))
  3715. { soap->errnum = 0;
  3716. soap_set_sender_error(soap, tcp_error(soap), "TCP init failed in tcp_connect()", SOAP_TCP_ERROR);
  3717. return SOAP_INVALID_SOCKET;
  3718. }
  3719. soap->errmode = 0;
  3720. #ifdef WITH_IPV6
  3721. memset((void*)&hints, 0, sizeof(hints));
  3722. hints.ai_family = PF_UNSPEC;
  3723. #ifndef WITH_LEAN
  3724. if ((soap->omode & SOAP_IO_UDP))
  3725. hints.ai_socktype = SOCK_DGRAM;
  3726. else
  3727. #endif
  3728. hints.ai_socktype = SOCK_STREAM;
  3729. soap->errmode = 2;
  3730. if (soap->proxy_host)
  3731. err = getaddrinfo(soap->proxy_host, soap_int2s(soap, soap->proxy_port), &hints, &res);
  3732. else
  3733. err = getaddrinfo(host, soap_int2s(soap, port), &hints, &res);
  3734. if (err)
  3735. { soap_set_sender_error(soap, SOAP_GAI_STRERROR(err), "getaddrinfo failed in tcp_connect()", SOAP_TCP_ERROR);
  3736. return SOAP_INVALID_SOCKET;
  3737. }
  3738. ressave = res;
  3739. again:
  3740. sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  3741. soap->errmode = 0;
  3742. #else
  3743. #ifndef WITH_LEAN
  3744. again:
  3745. #endif
  3746. #ifndef WITH_LEAN
  3747. if ((soap->omode & SOAP_IO_UDP))
  3748. sk = socket(AF_INET, SOCK_DGRAM, 0);
  3749. else
  3750. #endif
  3751. sk = socket(AF_INET, SOCK_STREAM, 0);
  3752. #endif
  3753. if (!soap_valid_socket(sk))
  3754. {
  3755. #ifdef WITH_IPV6
  3756. if (res->ai_next)
  3757. { res = res->ai_next;
  3758. goto again;
  3759. }
  3760. #endif
  3761. soap->errnum = soap_socket_errno(sk);
  3762. soap_set_sender_error(soap, tcp_error(soap), "socket failed in tcp_connect()", SOAP_TCP_ERROR);
  3763. #ifdef WITH_IPV6
  3764. freeaddrinfo(ressave);
  3765. #endif
  3766. return SOAP_INVALID_SOCKET;
  3767. }
  3768. #ifdef SOCKET_CLOSE_ON_EXEC
  3769. #ifdef WIN32
  3770. #ifndef UNDER_CE
  3771. SetHandleInformation((HANDLE)sk, HANDLE_FLAG_INHERIT, 0);
  3772. #endif
  3773. #else
  3774. fcntl(sk, F_SETFD, 1);
  3775. #endif
  3776. #endif
  3777. #ifndef WITH_LEAN
  3778. if (soap->connect_flags == SO_LINGER)
  3779. { struct linger linger;
  3780. memset((void*)&linger, 0, sizeof(linger));
  3781. linger.l_onoff = 1;
  3782. linger.l_linger = soap->linger_time;
  3783. if (setsockopt(sk, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(struct linger)))
  3784. { soap->errnum = soap_socket_errno(sk);
  3785. soap_set_sender_error(soap, tcp_error(soap), "setsockopt SO_LINGER failed in tcp_connect()", SOAP_TCP_ERROR);
  3786. soap->fclosesocket(soap, sk);
  3787. #ifdef WITH_IPV6
  3788. freeaddrinfo(ressave);
  3789. #endif
  3790. return SOAP_INVALID_SOCKET;
  3791. }
  3792. }
  3793. else if (soap->connect_flags && setsockopt(sk, SOL_SOCKET, soap->connect_flags, (char*)&set, sizeof(int)))
  3794. { soap->errnum = soap_socket_errno(sk);
  3795. soap_set_sender_error(soap, tcp_error(soap), "setsockopt failed in tcp_connect()", SOAP_TCP_ERROR);
  3796. soap->fclosesocket(soap, sk);
  3797. #ifdef WITH_IPV6
  3798. freeaddrinfo(ressave);
  3799. #endif
  3800. return SOAP_INVALID_SOCKET;
  3801. }
  3802. if ((soap->keep_alive || soap->tcp_keep_alive) && setsockopt(sk, SOL_SOCKET, SO_KEEPALIVE, (char*)&set, sizeof(int)))
  3803. { soap->errnum = soap_socket_errno(sk);
  3804. soap_set_sender_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in tcp_connect()", SOAP_TCP_ERROR);
  3805. soap->fclosesocket(soap, sk);
  3806. #ifdef WITH_IPV6
  3807. freeaddrinfo(ressave);
  3808. #endif
  3809. return SOAP_INVALID_SOCKET;
  3810. }
  3811. if (setsockopt(sk, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
  3812. { soap->errnum = soap_socket_errno(sk);
  3813. soap_set_sender_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in tcp_connect()", SOAP_TCP_ERROR);
  3814. soap->fclosesocket(soap, sk);
  3815. #ifdef WITH_IPV6
  3816. freeaddrinfo(ressave);
  3817. #endif
  3818. return SOAP_INVALID_SOCKET;
  3819. }
  3820. if (setsockopt(sk, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
  3821. { soap->errnum = soap_socket_errno(sk);
  3822. soap_set_sender_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in tcp_connect()", SOAP_TCP_ERROR);
  3823. soap->fclosesocket(soap, sk);
  3824. #ifdef WITH_IPV6
  3825. freeaddrinfo(ressave);
  3826. #endif
  3827. return SOAP_INVALID_SOCKET;
  3828. }
  3829. #ifdef TCP_KEEPIDLE
  3830. if (soap->tcp_keep_idle && setsockopt((SOAP_SOCKET)sk, IPPROTO_TCP, TCP_KEEPIDLE, (char*)&(soap->tcp_keep_idle), sizeof(int)))
  3831. { soap->errnum = soap_socket_errno(sk);
  3832. soap_set_sender_error(soap, tcp_error(soap), "setsockopt TCP_KEEPIDLE failed in tcp_connect()", SOAP_TCP_ERROR);
  3833. soap->fclosesocket(soap, (SOAP_SOCKET)sk);
  3834. #ifdef WITH_IPV6
  3835. freeaddrinfo(ressave);
  3836. #endif
  3837. return SOAP_INVALID_SOCKET;
  3838. }
  3839. #endif
  3840. #ifdef TCP_KEEPINTVL
  3841. if (soap->tcp_keep_intvl && setsockopt((SOAP_SOCKET)sk, IPPROTO_TCP, TCP_KEEPINTVL, (char*)&(soap->tcp_keep_intvl), sizeof(int)))
  3842. { soap->errnum = soap_socket_errno(sk);
  3843. soap_set_sender_error(soap, tcp_error(soap), "setsockopt TCP_KEEPINTVL failed in tcp_connect()", SOAP_TCP_ERROR);
  3844. soap->fclosesocket(soap, (SOAP_SOCKET)sk);
  3845. #ifdef WITH_IPV6
  3846. freeaddrinfo(ressave);
  3847. #endif
  3848. return SOAP_INVALID_SOCKET;
  3849. }
  3850. #endif
  3851. #ifdef TCP_KEEPCNT
  3852. if (soap->tcp_keep_cnt && setsockopt((SOAP_SOCKET)sk, IPPROTO_TCP, TCP_KEEPCNT, (char*)&(soap->tcp_keep_cnt), sizeof(int)))
  3853. { soap->errnum = soap_socket_errno(sk);
  3854. soap_set_sender_error(soap, tcp_error(soap), "setsockopt TCP_KEEPCNT failed in tcp_connect()", SOAP_TCP_ERROR);
  3855. soap->fclosesocket(soap, (SOAP_SOCKET)sk);
  3856. #ifdef WITH_IPV6
  3857. freeaddrinfo(ressave);
  3858. #endif
  3859. return SOAP_INVALID_SOCKET;
  3860. }
  3861. #endif
  3862. #ifdef TCP_NODELAY
  3863. if (!(soap->omode & SOAP_IO_UDP) && setsockopt(sk, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
  3864. { soap->errnum = soap_socket_errno(sk);
  3865. soap_set_sender_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in tcp_connect()", SOAP_TCP_ERROR);
  3866. soap->fclosesocket(soap, sk);
  3867. #ifdef WITH_IPV6
  3868. freeaddrinfo(ressave);
  3869. #endif
  3870. return SOAP_INVALID_SOCKET;
  3871. }
  3872. #endif
  3873. #ifdef WITH_IPV6
  3874. if ((soap->omode & SOAP_IO_UDP) && soap->ipv6_multicast_if)
  3875. { struct sockaddr_in6 *in6addr = (struct sockaddr_in6*)res->ai_addr;
  3876. in6addr->sin6_scope_id = soap->ipv6_multicast_if;
  3877. }
  3878. #endif
  3879. #ifdef IP_MULTICAST_TTL
  3880. if ((soap->omode & SOAP_IO_UDP))
  3881. { if (soap->ipv4_multicast_ttl)
  3882. { unsigned char ttl = soap->ipv4_multicast_ttl;
  3883. if (setsockopt(sk, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, sizeof(ttl)))
  3884. { soap->errnum = soap_socket_errno(sk);
  3885. soap_set_sender_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_TTL failed in tcp_connect()", SOAP_TCP_ERROR);
  3886. soap->fclosesocket(soap, sk);
  3887. return SOAP_INVALID_SOCKET;
  3888. }
  3889. }
  3890. if ((soap->omode & SOAP_IO_UDP) && soap->ipv4_multicast_if && !soap->ipv6_multicast_if)
  3891. { if (setsockopt(sk, IPPROTO_IP, IP_MULTICAST_IF, (char*)soap->ipv4_multicast_if, sizeof(struct in_addr)))
  3892. #ifndef WINDOWS
  3893. { soap->errnum = soap_socket_errno(sk);
  3894. soap_set_sender_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_IF failed in tcp_connect()", SOAP_TCP_ERROR);
  3895. soap->fclosesocket(soap, sk);
  3896. return SOAP_INVALID_SOCKET;
  3897. }
  3898. #else
  3899. #ifndef IP_MULTICAST_IF
  3900. #define IP_MULTICAST_IF 2
  3901. #endif
  3902. if (setsockopt(sk, IPPROTO_IP, IP_MULTICAST_IF, (char*)soap->ipv4_multicast_if, sizeof(struct in_addr)))
  3903. { soap->errnum = soap_socket_errno(sk);
  3904. soap_set_sender_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_IF failed in tcp_connect()", SOAP_TCP_ERROR);
  3905. soap->fclosesocket(soap, sk);
  3906. return SOAP_INVALID_SOCKET;
  3907. }
  3908. #endif
  3909. }
  3910. }
  3911. #endif
  3912. #endif
  3913. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Opening socket=%d to host='%s' port=%d\n", sk, host, port));
  3914. #ifndef WITH_IPV6
  3915. soap->peerlen = sizeof(soap->peer);
  3916. memset((void*)&soap->peer, 0, sizeof(soap->peer));
  3917. soap->peer.sin_family = AF_INET;
  3918. soap->errmode = 2;
  3919. if (soap->proxy_host)
  3920. { if (soap->fresolve(soap, soap->proxy_host, &soap->peer.sin_addr))
  3921. { soap_set_sender_error(soap, tcp_error(soap), "get proxy host by name failed in tcp_connect()", SOAP_TCP_ERROR);
  3922. soap->fclosesocket(soap, sk);
  3923. return SOAP_INVALID_SOCKET;
  3924. }
  3925. soap->peer.sin_port = htons((short)soap->proxy_port);
  3926. }
  3927. else
  3928. { if (soap->fresolve(soap, host, &soap->peer.sin_addr))
  3929. { soap_set_sender_error(soap, tcp_error(soap), "get host by name failed in tcp_connect()", SOAP_TCP_ERROR);
  3930. soap->fclosesocket(soap, sk);
  3931. return SOAP_INVALID_SOCKET;
  3932. }
  3933. soap->peer.sin_port = htons((short)port);
  3934. }
  3935. soap->errmode = 0;
  3936. #ifndef WITH_LEAN
  3937. if ((soap->omode & SOAP_IO_UDP))
  3938. return sk;
  3939. #endif
  3940. #else
  3941. if ((soap->omode & SOAP_IO_UDP))
  3942. { memcpy(&soap->peer, res->ai_addr, res->ai_addrlen);
  3943. soap->peerlen = res->ai_addrlen;
  3944. freeaddrinfo(ressave);
  3945. return sk;
  3946. }
  3947. #endif
  3948. #ifndef WITH_LEAN
  3949. if (soap->connect_timeout)
  3950. SOAP_SOCKNONBLOCK(sk)
  3951. else
  3952. SOAP_SOCKBLOCK(sk)
  3953. retries = 10;
  3954. #endif
  3955. for (;;)
  3956. {
  3957. #ifdef WITH_IPV6
  3958. if (connect(sk, res->ai_addr, (int)res->ai_addrlen))
  3959. #else
  3960. if (connect(sk, (struct sockaddr*)&soap->peer, sizeof(soap->peer)))
  3961. #endif
  3962. { err = soap_socket_errno(sk);
  3963. #ifndef WITH_LEAN
  3964. if (err == SOAP_EADDRINUSE)
  3965. { soap->fclosesocket(soap, sk);
  3966. if (retries-- > 0)
  3967. goto again;
  3968. }
  3969. else if (soap->connect_timeout && (err == SOAP_EINPROGRESS || err == SOAP_EAGAIN || err == SOAP_EWOULDBLOCK))
  3970. {
  3971. SOAP_SOCKLEN_T k;
  3972. for (;;)
  3973. { register int r;
  3974. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND, soap->connect_timeout);
  3975. if (r > 0)
  3976. break;
  3977. if (!r)
  3978. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connect timeout\n"));
  3979. soap_set_sender_error(soap, "Timeout", "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  3980. soap->fclosesocket(soap, sk);
  3981. #ifdef WITH_IPV6
  3982. freeaddrinfo(ressave);
  3983. #endif
  3984. return SOAP_INVALID_SOCKET;
  3985. }
  3986. r = soap->errnum = soap_socket_errno(sk);
  3987. if (r != SOAP_EINTR)
  3988. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));
  3989. soap_set_sender_error(soap, tcp_error(soap), "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  3990. soap->fclosesocket(soap, sk);
  3991. #ifdef WITH_IPV6
  3992. freeaddrinfo(ressave);
  3993. #endif
  3994. return SOAP_INVALID_SOCKET;
  3995. }
  3996. }
  3997. k = (SOAP_SOCKLEN_T)sizeof(soap->errnum);
  3998. if (!getsockopt(sk, SOL_SOCKET, SO_ERROR, (char*)&soap->errnum, &k) && !soap->errnum) /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h */
  3999. break;
  4000. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));
  4001. if (!soap->errnum)
  4002. soap->errnum = soap_socket_errno(sk);
  4003. soap_set_sender_error(soap, tcp_error(soap), "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  4004. soap->fclosesocket(soap, sk);
  4005. #ifdef WITH_IPV6
  4006. freeaddrinfo(ressave);
  4007. #endif
  4008. return SOAP_INVALID_SOCKET;
  4009. }
  4010. #endif
  4011. #ifdef WITH_IPV6
  4012. if (res->ai_next)
  4013. { res = res->ai_next;
  4014. soap->fclosesocket(soap, sk);
  4015. goto again;
  4016. }
  4017. #endif
  4018. if (err && err != SOAP_EINTR)
  4019. { soap->errnum = err;
  4020. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));
  4021. soap_set_sender_error(soap, tcp_error(soap), "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  4022. soap->fclosesocket(soap, sk);
  4023. #ifdef WITH_IPV6
  4024. freeaddrinfo(ressave);
  4025. #endif
  4026. return SOAP_INVALID_SOCKET;
  4027. }
  4028. }
  4029. else
  4030. break;
  4031. }
  4032. #ifdef WITH_IPV6
  4033. soap->peerlen = 0; /* IPv6: already connected so use send() */
  4034. freeaddrinfo(ressave);
  4035. #endif
  4036. soap->socket = sk;
  4037. soap->imode &= ~SOAP_ENC_SSL;
  4038. soap->omode &= ~SOAP_ENC_SSL;
  4039. if (!soap_tag_cmp(endpoint, "https:*"))
  4040. {
  4041. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  4042. #ifdef WITH_OPENSSL
  4043. BIO *bio;
  4044. #endif
  4045. int r;
  4046. if (soap->proxy_host)
  4047. { soap_mode m = soap->mode; /* preserve settings */
  4048. soap_mode om = soap->omode; /* make sure we only parse HTTP */
  4049. size_t n = soap->count; /* save the content length */
  4050. const char *userid, *passwd;
  4051. int status = soap->status; /* save the current status/command */
  4052. short keep_alive = soap->keep_alive; /* save the KA status */
  4053. soap->omode &= ~SOAP_ENC; /* mask IO and ENC */
  4054. soap->omode |= SOAP_IO_BUFFER;
  4055. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connecting to %s proxy server %s for destination endpoint %s\n", soap->proxy_http_version, soap->proxy_host, endpoint));
  4056. #ifdef WITH_NTLM
  4057. if (soap->ntlm_challenge)
  4058. { if (soap_ntlm_handshake(soap, SOAP_CONNECT, endpoint, host, port))
  4059. return soap->error;
  4060. }
  4061. #endif
  4062. if (soap_begin_send(soap))
  4063. { soap->fclosesocket(soap, sk);
  4064. return SOAP_INVALID_SOCKET;
  4065. }
  4066. soap->status = SOAP_CONNECT;
  4067. soap->keep_alive = 1;
  4068. if ((soap->error = soap->fpost(soap, endpoint, host, port, NULL, NULL, 0))
  4069. || soap_end_send_flush(soap))
  4070. { soap->fclosesocket(soap, sk);
  4071. return SOAP_INVALID_SOCKET;
  4072. }
  4073. soap->keep_alive = keep_alive;
  4074. soap->omode = om;
  4075. om = soap->imode;
  4076. soap->imode &= ~SOAP_ENC; /* mask IO and ENC */
  4077. userid = soap->userid; /* preserve */
  4078. passwd = soap->passwd; /* preserve */
  4079. if ((soap->error = soap->fparse(soap)))
  4080. { soap->fclosesocket(soap, sk);
  4081. return SOAP_INVALID_SOCKET;
  4082. }
  4083. soap->status = status; /* restore */
  4084. soap->userid = userid; /* restore */
  4085. soap->passwd = passwd; /* restore */
  4086. soap->imode = om; /* restore */
  4087. soap->count = n; /* restore */
  4088. if (soap_begin_send(soap))
  4089. { soap->fclosesocket(soap, sk);
  4090. return SOAP_INVALID_SOCKET;
  4091. }
  4092. if (endpoint)
  4093. { strncpy(soap->endpoint, endpoint, sizeof(soap->endpoint)); /* restore */
  4094. soap->endpoint[sizeof(soap->endpoint) - 1] = '\0';
  4095. }
  4096. soap->mode = m;
  4097. }
  4098. #ifdef WITH_OPENSSL
  4099. soap->ssl_flags |= SOAP_SSL_CLIENT;
  4100. if (!soap->ctx && (soap->error = soap->fsslauth(soap)))
  4101. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL required, but no ctx set\n"));
  4102. soap->fclosesocket(soap, sk);
  4103. soap->error = SOAP_SSL_ERROR;
  4104. return SOAP_INVALID_SOCKET;
  4105. }
  4106. if (!soap->ssl)
  4107. { soap->ssl = SSL_new(soap->ctx);
  4108. if (!soap->ssl)
  4109. { soap->fclosesocket(soap, sk);
  4110. soap->error = SOAP_SSL_ERROR;
  4111. return SOAP_INVALID_SOCKET;
  4112. }
  4113. }
  4114. else
  4115. SSL_clear(soap->ssl);
  4116. if (soap->session)
  4117. { if (!strcmp(soap->session_host, host) && soap->session_port == port)
  4118. SSL_set_session(soap->ssl, soap->session);
  4119. SSL_SESSION_free(soap->session);
  4120. soap->session = NULL;
  4121. }
  4122. soap->imode |= SOAP_ENC_SSL;
  4123. soap->omode |= SOAP_ENC_SSL;
  4124. bio = BIO_new_socket((int)sk, BIO_NOCLOSE);
  4125. SSL_set_bio(soap->ssl, bio, bio);
  4126. /* Connect timeout: set SSL sockets to non-blocking */
  4127. retries = 0;
  4128. if (soap->connect_timeout)
  4129. { SOAP_SOCKNONBLOCK(sk)
  4130. retries = 10*soap->connect_timeout;
  4131. }
  4132. else
  4133. SOAP_SOCKBLOCK(sk)
  4134. if (retries <= 0)
  4135. retries = 100; /* timeout: 10 sec retries, 100 times 0.1 sec */
  4136. /* Try connecting until success or timeout (when nonblocking) */
  4137. do
  4138. { if ((r = SSL_connect(soap->ssl)) <= 0)
  4139. { int err = SSL_get_error(soap->ssl, r);
  4140. if (err == SSL_ERROR_WANT_CONNECT || err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
  4141. { register int s;
  4142. if (err == SSL_ERROR_WANT_READ)
  4143. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  4144. else
  4145. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  4146. if (s < 0)
  4147. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL_connect/select error in tcp_connect\n"));
  4148. soap_set_sender_error(soap, soap_ssl_error(soap, r), "SSL_connect failed in tcp_connect()", SOAP_TCP_ERROR);
  4149. soap->fclosesocket(soap, sk);
  4150. return SOAP_INVALID_SOCKET;
  4151. }
  4152. if (s == 0 && retries-- <= 0)
  4153. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL/TLS connect timeout\n"));
  4154. soap_set_sender_error(soap, "Timeout", "SSL_connect failed in tcp_connect()", SOAP_TCP_ERROR);
  4155. soap->fclosesocket(soap, sk);
  4156. return SOAP_INVALID_SOCKET;
  4157. }
  4158. }
  4159. else
  4160. { soap_set_sender_error(soap, soap_ssl_error(soap, r), "SSL_connect error in tcp_connect()", SOAP_SSL_ERROR);
  4161. soap->fclosesocket(soap, sk);
  4162. return SOAP_INVALID_SOCKET;
  4163. }
  4164. }
  4165. } while (!SSL_is_init_finished(soap->ssl));
  4166. /* Set SSL sockets to nonblocking */
  4167. SOAP_SOCKNONBLOCK(sk)
  4168. /* Check server credentials when required */
  4169. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  4170. { int err;
  4171. if ((err = SSL_get_verify_result(soap->ssl)) != X509_V_OK)
  4172. { soap_set_sender_error(soap, X509_verify_cert_error_string(err), "SSL/TLS certificate presented by peer cannot be verified in tcp_connect()", SOAP_SSL_ERROR);
  4173. soap->fclosesocket(soap, sk);
  4174. return SOAP_INVALID_SOCKET;
  4175. }
  4176. if (!(soap->ssl_flags & SOAP_SSL_SKIP_HOST_CHECK))
  4177. { X509_NAME *subj;
  4178. STACK_OF(CONF_VALUE) *val = NULL;
  4179. #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
  4180. GENERAL_NAMES *names = NULL;
  4181. #else
  4182. int ext_count;
  4183. #endif
  4184. int ok = 0;
  4185. X509 *peer = SSL_get_peer_certificate(soap->ssl);
  4186. if (!peer)
  4187. { soap_set_sender_error(soap, "SSL/TLS error", "No SSL/TLS certificate was presented by the peer in tcp_connect()", SOAP_SSL_ERROR);
  4188. soap->fclosesocket(soap, sk);
  4189. return SOAP_INVALID_SOCKET;
  4190. }
  4191. #if (OPENSSL_VERSION_NUMBER < 0x0090800fL)
  4192. ext_count = X509_get_ext_count(peer);
  4193. if (ext_count > 0)
  4194. { int i;
  4195. for (i = 0; i < ext_count; i++)
  4196. { X509_EXTENSION *ext = X509_get_ext(peer, i);
  4197. const char *ext_str = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));
  4198. if (ext_str && !strcmp(ext_str, "subjectAltName"))
  4199. { X509V3_EXT_METHOD *meth = (X509V3_EXT_METHOD*)X509V3_EXT_get(ext);
  4200. unsigned char *data;
  4201. if (!meth)
  4202. break;
  4203. data = ext->value->data;
  4204. if (data)
  4205. {
  4206. #if (OPENSSL_VERSION_NUMBER > 0x00907000L)
  4207. void *ext_data;
  4208. if (meth->it)
  4209. ext_data = ASN1_item_d2i(NULL, &data, ext->value->length, ASN1_ITEM_ptr(meth->it));
  4210. else
  4211. { /* OpenSSL is not portable at this point (?):
  4212. Some compilers appear to prefer
  4213. meth->d2i(NULL, (const unsigned char**)&data, ...
  4214. and others prefer
  4215. meth->d2i(NULL, &data, ext->value->length);
  4216. */
  4217. ext_data = meth->d2i(NULL, &data, ext->value->length);
  4218. }
  4219. if (ext_data)
  4220. val = meth->i2v(meth, ext_data, NULL);
  4221. else
  4222. val = NULL;
  4223. if (meth->it)
  4224. ASN1_item_free((ASN1_VALUE*)ext_data, ASN1_ITEM_ptr(meth->it));
  4225. else
  4226. meth->ext_free(ext_data);
  4227. #else
  4228. void *ext_data = meth->d2i(NULL, &data, ext->value->length);
  4229. if (ext_data)
  4230. val = meth->i2v(meth, ext_data, NULL);
  4231. meth->ext_free(ext_data);
  4232. #endif
  4233. if (val)
  4234. { int j;
  4235. for (j = 0; j < sk_CONF_VALUE_num(val); j++)
  4236. { CONF_VALUE *nval = sk_CONF_VALUE_value(val, j);
  4237. if (nval && !strcmp(nval->name, "DNS") && !strcmp(nval->value, host))
  4238. { ok = 1;
  4239. break;
  4240. }
  4241. }
  4242. sk_CONF_VALUE_pop_free(val, X509V3_conf_free);
  4243. }
  4244. }
  4245. }
  4246. if (ok)
  4247. break;
  4248. }
  4249. }
  4250. #else
  4251. names = (GENERAL_NAMES*)X509_get_ext_d2i(peer, NID_subject_alt_name, NULL, NULL);
  4252. if (names)
  4253. { val = i2v_GENERAL_NAMES(NULL, names, val);
  4254. sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
  4255. }
  4256. if (val)
  4257. { int j;
  4258. for (j = 0; j < sk_CONF_VALUE_num(val); j++)
  4259. { CONF_VALUE *nval = sk_CONF_VALUE_value(val, j);
  4260. if (nval && !strcmp(nval->name, "DNS") && !strcmp(nval->value, host))
  4261. { ok = 1;
  4262. break;
  4263. }
  4264. }
  4265. sk_CONF_VALUE_pop_free(val, X509V3_conf_free);
  4266. }
  4267. #endif
  4268. if (!ok && (subj = X509_get_subject_name(peer)))
  4269. { int i = -1;
  4270. do
  4271. { ASN1_STRING *name;
  4272. i = X509_NAME_get_index_by_NID(subj, NID_commonName, i);
  4273. if (i == -1)
  4274. break;
  4275. name = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subj, i));
  4276. if (name)
  4277. //{ if (!soap_tag_cmp(host, (const char*)M_ASN1_STRING_data(name)))
  4278. { if (!soap_tag_cmp(host, (const char*)ASN1_STRING_data(name)))
  4279. ok = 1;
  4280. else
  4281. { unsigned char *tmp = NULL;
  4282. ASN1_STRING_to_UTF8(&tmp, name);
  4283. if (tmp)
  4284. { if (!soap_tag_cmp(host, (const char*)tmp))
  4285. ok = 1;
  4286. else if (tmp[0] == '*') /* wildcard domain */
  4287. { const char *t = strchr(host, '.');
  4288. if (t && !soap_tag_cmp(t, (const char*)tmp+1))
  4289. ok = 1;
  4290. }
  4291. OPENSSL_free(tmp);
  4292. }
  4293. }
  4294. }
  4295. } while (!ok);
  4296. }
  4297. X509_free(peer);
  4298. if (!ok)
  4299. { soap_set_sender_error(soap, "SSL/TLS error", "SSL/TLS certificate host name mismatch in tcp_connect()", SOAP_SSL_ERROR);
  4300. soap->fclosesocket(soap, sk);
  4301. return SOAP_INVALID_SOCKET;
  4302. }
  4303. }
  4304. }
  4305. #endif
  4306. #ifdef WITH_GNUTLS
  4307. soap->ssl_flags |= SOAP_SSL_CLIENT;
  4308. if (!soap->session && (soap->error = soap->fsslauth(soap)))
  4309. { soap->fclosesocket(soap, sk);
  4310. return SOAP_INVALID_SOCKET;
  4311. }
  4312. gnutls_transport_set_ptr(soap->session, (gnutls_transport_ptr_t)(long)sk);
  4313. /* Set SSL sockets to non-blocking */
  4314. if (soap->connect_timeout)
  4315. { SOAP_SOCKNONBLOCK(sk)
  4316. retries = 10*soap->connect_timeout;
  4317. }
  4318. else
  4319. SOAP_SOCKBLOCK(sk)
  4320. if (retries <= 0)
  4321. retries = 100; /* timeout: 10 sec retries, 100 times 0.1 sec */
  4322. while ((r = gnutls_handshake(soap->session)))
  4323. { int s;
  4324. /* GNUTLS repeat handhake when GNUTLS_E_AGAIN */
  4325. if (retries-- <= 0)
  4326. break;
  4327. if (r == GNUTLS_E_AGAIN || r == GNUTLS_E_INTERRUPTED)
  4328. { if (!gnutls_record_get_direction(soap->session))
  4329. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  4330. else
  4331. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  4332. if (s < 0)
  4333. break;
  4334. }
  4335. else
  4336. { soap->errnum = soap_socket_errno(sk);
  4337. break;
  4338. }
  4339. }
  4340. if (r)
  4341. { soap_set_sender_error(soap, soap_ssl_error(soap, r), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  4342. soap->fclosesocket(soap, sk);
  4343. return SOAP_INVALID_SOCKET;
  4344. }
  4345. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  4346. { const char *err = ssl_verify(soap, host);
  4347. if (err)
  4348. { soap->fclosesocket(soap, sk);
  4349. soap->error = soap_set_sender_error(soap, "SSL/TLS error", err, SOAP_SSL_ERROR);
  4350. return SOAP_INVALID_SOCKET;
  4351. }
  4352. }
  4353. #endif
  4354. #else
  4355. soap->fclosesocket(soap, sk);
  4356. soap->error = SOAP_SSL_ERROR;
  4357. return SOAP_INVALID_SOCKET;
  4358. #endif
  4359. }
  4360. if (soap->recv_timeout || soap->send_timeout)
  4361. SOAP_SOCKNONBLOCK(sk)
  4362. else
  4363. SOAP_SOCKBLOCK(sk)
  4364. return sk;
  4365. }
  4366. #endif
  4367. #endif
  4368. /******************************************************************************/
  4369. #ifndef WITH_NOIO
  4370. #ifndef PALM_1
  4371. static int
  4372. tcp_select(struct soap *soap, SOAP_SOCKET sk, int flags, int timeout)
  4373. { int r;
  4374. struct timeval tv;
  4375. fd_set fd[3], *rfd, *sfd, *efd;
  4376. int retries = 0;
  4377. int eintr = SOAP_MAXEINTR;
  4378. soap->errnum = 0;
  4379. #ifndef WIN32
  4380. #if !defined(FD_SETSIZE) || defined(__QNX__) || defined(QNX)
  4381. /* no FD_SETSIZE or select() is not MT safe on some QNX: always poll */
  4382. if (1)
  4383. #else
  4384. /* if fd max set size exceeded, use poll() */
  4385. if ((int)sk >= (int)FD_SETSIZE)
  4386. #endif
  4387. #ifdef HAVE_POLL
  4388. { struct pollfd pollfd;
  4389. pollfd.fd = (int)sk;
  4390. pollfd.events = 0;
  4391. if (flags & SOAP_TCP_SELECT_RCV)
  4392. pollfd.events |= POLLIN;
  4393. if (flags & SOAP_TCP_SELECT_SND)
  4394. pollfd.events |= POLLOUT;
  4395. if (flags & SOAP_TCP_SELECT_ERR)
  4396. pollfd.events |= POLLERR;
  4397. if (timeout <= 0)
  4398. timeout /= -1000; /* -usec -> ms */
  4399. else
  4400. { retries = timeout - 1;
  4401. timeout = 1000;
  4402. }
  4403. do
  4404. { r = poll(&pollfd, 1, timeout);
  4405. if (r < 0 && (soap->errnum = soap_socket_errno(sk)) == SOAP_EINTR && eintr--)
  4406. continue;
  4407. } while (r == 0 && retries--);
  4408. if (r > 0)
  4409. { r = 0;
  4410. if ((flags & SOAP_TCP_SELECT_RCV) && (pollfd.revents & POLLIN))
  4411. r |= SOAP_TCP_SELECT_RCV;
  4412. if ((flags & SOAP_TCP_SELECT_SND) && (pollfd.revents & POLLOUT))
  4413. r |= SOAP_TCP_SELECT_SND;
  4414. if ((flags & SOAP_TCP_SELECT_ERR) && (pollfd.revents & POLLERR))
  4415. r |= SOAP_TCP_SELECT_ERR;
  4416. }
  4417. return r;
  4418. }
  4419. #else
  4420. { soap->error = SOAP_FD_EXCEEDED;
  4421. return -1;
  4422. }
  4423. #endif
  4424. #endif
  4425. if (timeout > 0)
  4426. retries = timeout - 1;
  4427. do
  4428. { rfd = sfd = efd = NULL;
  4429. if (flags & SOAP_TCP_SELECT_RCV)
  4430. { rfd = &fd[0];
  4431. FD_ZERO(rfd);
  4432. FD_SET(sk, rfd);
  4433. }
  4434. if (flags & SOAP_TCP_SELECT_SND)
  4435. { sfd = &fd[1];
  4436. FD_ZERO(sfd);
  4437. FD_SET(sk, sfd);
  4438. }
  4439. if (flags & SOAP_TCP_SELECT_ERR)
  4440. { efd = &fd[2];
  4441. FD_ZERO(efd);
  4442. FD_SET(sk, efd);
  4443. }
  4444. if (timeout <= 0)
  4445. { tv.tv_sec = -timeout / 1000000;
  4446. tv.tv_usec = -timeout % 1000000;
  4447. }
  4448. else
  4449. { tv.tv_sec = 1;
  4450. tv.tv_usec = 0;
  4451. }
  4452. r = select((int)sk + 1, rfd, sfd, efd, &tv);
  4453. if (r < 0 && (soap->errnum = soap_socket_errno(sk)) == SOAP_EINTR && eintr--)
  4454. continue;
  4455. } while (r == 0 && retries--);
  4456. if (r > 0)
  4457. { r = 0;
  4458. if ((flags & SOAP_TCP_SELECT_RCV) && FD_ISSET(sk, rfd))
  4459. r |= SOAP_TCP_SELECT_RCV;
  4460. if ((flags & SOAP_TCP_SELECT_SND) && FD_ISSET(sk, sfd))
  4461. r |= SOAP_TCP_SELECT_SND;
  4462. if ((flags & SOAP_TCP_SELECT_ERR) && FD_ISSET(sk, efd))
  4463. r |= SOAP_TCP_SELECT_ERR;
  4464. }
  4465. return r;
  4466. }
  4467. #endif
  4468. #endif
  4469. /******************************************************************************/
  4470. #ifndef WITH_NOIO
  4471. #ifndef PALM_1
  4472. static SOAP_SOCKET
  4473. tcp_accept(struct soap *soap, SOAP_SOCKET s, struct sockaddr *a, int *n)
  4474. { SOAP_SOCKET sk;
  4475. (void)soap;
  4476. sk = accept(s, a, (SOAP_SOCKLEN_T*)n); /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h */
  4477. #ifdef SOCKET_CLOSE_ON_EXEC
  4478. #ifdef WIN32
  4479. #ifndef UNDER_CE
  4480. SetHandleInformation((HANDLE)sk, HANDLE_FLAG_INHERIT, 0);
  4481. #endif
  4482. #else
  4483. fcntl(sk, F_SETFD, FD_CLOEXEC);
  4484. #endif
  4485. #endif
  4486. return sk;
  4487. }
  4488. #endif
  4489. #endif
  4490. /******************************************************************************/
  4491. #ifndef WITH_NOIO
  4492. #ifndef PALM_1
  4493. static int
  4494. tcp_disconnect(struct soap *soap)
  4495. {
  4496. #ifdef WITH_OPENSSL
  4497. if (soap->ssl)
  4498. { int r, s = 0;
  4499. if (soap->session)
  4500. { SSL_SESSION_free(soap->session);
  4501. soap->session = NULL;
  4502. }
  4503. if (*soap->host)
  4504. { soap->session = SSL_get1_session(soap->ssl);
  4505. if (soap->session)
  4506. { strcpy(soap->session_host, soap->host);
  4507. soap->session_port = soap->port;
  4508. }
  4509. }
  4510. r = SSL_shutdown(soap->ssl);
  4511. /* SSL shutdown does not work when reads are pending, non-blocking */
  4512. if (r == 0)
  4513. { while (SSL_want_read(soap->ssl))
  4514. { if (SSL_read(soap->ssl, NULL, 0)
  4515. || soap_socket_errno(soap->socket) != SOAP_EAGAIN)
  4516. { r = SSL_shutdown(soap->ssl);
  4517. break;
  4518. }
  4519. }
  4520. }
  4521. if (r == 0)
  4522. { if (soap_valid_socket(soap->socket))
  4523. { if (!soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR))
  4524. {
  4525. #if !defined(WITH_LEAN) && !defined(WIN32)
  4526. /*
  4527. wait up to 5 seconds for close_notify to be sent by peer (if peer not
  4528. present, this avoids calling SSL_shutdown() which has a lengthy return
  4529. timeout)
  4530. */
  4531. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, 5);
  4532. if (r <= 0)
  4533. { soap->errnum = 0;
  4534. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connection lost...\n"));
  4535. soap->fclosesocket(soap, soap->socket);
  4536. soap->socket = SOAP_INVALID_SOCKET;
  4537. ERR_remove_state(0);
  4538. SSL_free(soap->ssl);
  4539. soap->ssl = NULL;
  4540. return SOAP_OK;
  4541. }
  4542. #else
  4543. r = SSL_shutdown(soap->ssl);
  4544. #endif
  4545. }
  4546. }
  4547. }
  4548. if (r != 1)
  4549. { s = ERR_get_error();
  4550. if (s)
  4551. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shutdown failed: %d\n", SSL_get_error(soap->ssl, r)));
  4552. if (soap_valid_socket(soap->socket) && !(soap->omode & SOAP_IO_UDP))
  4553. { soap->fclosesocket(soap, soap->socket);
  4554. soap->socket = SOAP_INVALID_SOCKET;
  4555. }
  4556. }
  4557. }
  4558. SSL_free(soap->ssl);
  4559. soap->ssl = NULL;
  4560. if (s)
  4561. return SOAP_SSL_ERROR;
  4562. ERR_remove_state(0);
  4563. }
  4564. #endif
  4565. #ifdef WITH_GNUTLS
  4566. if (soap->session)
  4567. { gnutls_bye(soap->session, GNUTLS_SHUT_RDWR);
  4568. gnutls_deinit(soap->session);
  4569. soap->session = NULL;
  4570. }
  4571. #endif
  4572. if (soap_valid_socket(soap->socket) && !(soap->omode & SOAP_IO_UDP))
  4573. { soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_RDWR);
  4574. soap->fclosesocket(soap, soap->socket);
  4575. soap->socket = SOAP_INVALID_SOCKET;
  4576. }
  4577. return SOAP_OK;
  4578. }
  4579. #endif
  4580. #endif
  4581. /******************************************************************************/
  4582. #ifndef WITH_NOIO
  4583. #ifndef PALM_1
  4584. static int
  4585. tcp_closesocket(struct soap *soap, SOAP_SOCKET sk)
  4586. { (void)soap;
  4587. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Close socket=%d\n", (int)sk));
  4588. return soap_closesocket(sk);
  4589. }
  4590. #endif
  4591. #endif
  4592. /******************************************************************************/
  4593. #ifndef WITH_NOIO
  4594. #ifndef PALM_1
  4595. static int
  4596. tcp_shutdownsocket(struct soap *soap, SOAP_SOCKET sk, int how)
  4597. { (void)soap;
  4598. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shutdown socket=%d how=%d\n", (int)sk, how));
  4599. return shutdown(sk, how);
  4600. }
  4601. #endif
  4602. #endif
  4603. /******************************************************************************/
  4604. #ifndef WITH_NOIO
  4605. #ifndef PALM_1
  4606. SOAP_FMAC1
  4607. SOAP_SOCKET
  4608. SOAP_FMAC2
  4609. soap_bind(struct soap *soap, const char *host, int port, int backlog)
  4610. {
  4611. #ifdef WITH_IPV6
  4612. struct addrinfo *addrinfo = NULL;
  4613. struct addrinfo hints;
  4614. struct addrinfo res;
  4615. int err;
  4616. #ifdef WITH_NO_IPV6_V6ONLY
  4617. int unset = 0;
  4618. #endif
  4619. #endif
  4620. #ifndef WITH_LEAN
  4621. #ifndef WIN32
  4622. int len = SOAP_BUFLEN;
  4623. #else
  4624. int len = SOAP_BUFLEN + 1; /* speeds up windows xfer */
  4625. #endif
  4626. int set = 1;
  4627. #endif
  4628. if (soap_valid_socket(soap->master))
  4629. { soap->fclosesocket(soap, soap->master);
  4630. soap->master = SOAP_INVALID_SOCKET;
  4631. }
  4632. soap->socket = SOAP_INVALID_SOCKET;
  4633. soap->errmode = 1;
  4634. if (tcp_init(soap))
  4635. { soap_set_receiver_error(soap, tcp_error(soap), "TCP init failed in soap_bind()", SOAP_TCP_ERROR);
  4636. return SOAP_INVALID_SOCKET;
  4637. }
  4638. #ifdef WITH_IPV6
  4639. memset((void*)&hints, 0, sizeof(hints));
  4640. hints.ai_family = PF_UNSPEC;
  4641. #ifndef WITH_LEAN
  4642. if ((soap->omode & SOAP_IO_UDP))
  4643. hints.ai_socktype = SOCK_DGRAM;
  4644. else
  4645. #endif
  4646. hints.ai_socktype = SOCK_STREAM;
  4647. hints.ai_flags = AI_PASSIVE;
  4648. soap->errmode = 2;
  4649. err = getaddrinfo(host, soap_int2s(soap, port), &hints, &addrinfo);
  4650. if (err || !addrinfo)
  4651. { soap_set_receiver_error(soap, SOAP_GAI_STRERROR(err), "getaddrinfo failed in soap_bind()", SOAP_TCP_ERROR);
  4652. return SOAP_INVALID_SOCKET;
  4653. }
  4654. res = *addrinfo;
  4655. memcpy(&soap->peer, addrinfo->ai_addr, addrinfo->ai_addrlen);
  4656. soap->peerlen = addrinfo->ai_addrlen;
  4657. res.ai_addr = (struct sockaddr*)&soap->peer;
  4658. res.ai_addrlen = soap->peerlen;
  4659. freeaddrinfo(addrinfo);
  4660. soap->master = (int)socket(res.ai_family, res.ai_socktype, res.ai_protocol);
  4661. #else
  4662. #ifndef WITH_LEAN
  4663. if ((soap->omode & SOAP_IO_UDP))
  4664. soap->master = (int)socket(AF_INET, SOCK_DGRAM, 0);
  4665. else
  4666. #endif
  4667. soap->master = (int)socket(AF_INET, SOCK_STREAM, 0);
  4668. #endif
  4669. soap->errmode = 0;
  4670. if (!soap_valid_socket(soap->master))
  4671. { soap->errnum = soap_socket_errno(soap->master);
  4672. soap_set_receiver_error(soap, tcp_error(soap), "socket failed in soap_bind()", SOAP_TCP_ERROR);
  4673. return SOAP_INVALID_SOCKET;
  4674. }
  4675. soap->port = port;
  4676. #ifndef WITH_LEAN
  4677. if ((soap->omode & SOAP_IO_UDP))
  4678. soap->socket = soap->master;
  4679. #endif
  4680. #ifdef SOCKET_CLOSE_ON_EXEC
  4681. #ifdef WIN32
  4682. #ifndef UNDER_CE
  4683. SetHandleInformation((HANDLE)soap->master, HANDLE_FLAG_INHERIT, 0);
  4684. #endif
  4685. #else
  4686. fcntl(soap->master, F_SETFD, 1);
  4687. #endif
  4688. #endif
  4689. #ifndef WITH_LEAN
  4690. if (soap->bind_flags && setsockopt(soap->master, SOL_SOCKET, soap->bind_flags, (char*)&set, sizeof(int)))
  4691. { soap->errnum = soap_socket_errno(soap->master);
  4692. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in soap_bind()", SOAP_TCP_ERROR);
  4693. return SOAP_INVALID_SOCKET;
  4694. }
  4695. if (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) && (!((soap->imode | soap->omode) & SOAP_IO_UDP)) && setsockopt(soap->master, SOL_SOCKET, SO_KEEPALIVE, (char*)&set, sizeof(int)))
  4696. { soap->errnum = soap_socket_errno(soap->master);
  4697. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in soap_bind()", SOAP_TCP_ERROR);
  4698. return SOAP_INVALID_SOCKET;
  4699. }
  4700. if (setsockopt(soap->master, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
  4701. { soap->errnum = soap_socket_errno(soap->master);
  4702. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in soap_bind()", SOAP_TCP_ERROR);
  4703. return SOAP_INVALID_SOCKET;
  4704. }
  4705. if (setsockopt(soap->master, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
  4706. { soap->errnum = soap_socket_errno(soap->master);
  4707. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in soap_bind()", SOAP_TCP_ERROR);
  4708. return SOAP_INVALID_SOCKET;
  4709. }
  4710. #ifdef TCP_NODELAY
  4711. if (!(soap->omode & SOAP_IO_UDP) && setsockopt(soap->master, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
  4712. { soap->errnum = soap_socket_errno(soap->master);
  4713. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in soap_bind()", SOAP_TCP_ERROR);
  4714. return SOAP_INVALID_SOCKET;
  4715. }
  4716. #endif
  4717. #endif
  4718. #ifdef WITH_IPV6
  4719. #ifdef WITH_IPV6_V6ONLY
  4720. if (setsockopt(soap->master, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&set, sizeof(int)))
  4721. { soap->errnum = soap_socket_errno(soap->master);
  4722. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt set IPV6_V6ONLY failed in soap_bind()", SOAP_TCP_ERROR);
  4723. return SOAP_INVALID_SOCKET;
  4724. }
  4725. #endif
  4726. #ifdef WITH_NO_IPV6_V6ONLY
  4727. if (setsockopt(soap->master, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&unset, sizeof(int)))
  4728. { soap->errnum = soap_socket_errno(soap->master);
  4729. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt unset IPV6_V6ONLY failed in soap_bind()", SOAP_TCP_ERROR);
  4730. return SOAP_INVALID_SOCKET;
  4731. }
  4732. #endif
  4733. soap->errmode = 0;
  4734. if (bind(soap->master, res.ai_addr, (int)res.ai_addrlen))
  4735. { soap->errnum = soap_socket_errno(soap->master);
  4736. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host\n"));
  4737. soap_closesock(soap);
  4738. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in soap_bind()", SOAP_TCP_ERROR);
  4739. return SOAP_INVALID_SOCKET;
  4740. }
  4741. #else
  4742. soap->peerlen = sizeof(soap->peer);
  4743. memset((void*)&soap->peer, 0, sizeof(soap->peer));
  4744. soap->peer.sin_family = AF_INET;
  4745. soap->errmode = 2;
  4746. if (host)
  4747. { if (soap->fresolve(soap, host, &soap->peer.sin_addr))
  4748. { soap_set_receiver_error(soap, tcp_error(soap), "get host by name failed in soap_bind()", SOAP_TCP_ERROR);
  4749. return SOAP_INVALID_SOCKET;
  4750. }
  4751. }
  4752. else
  4753. soap->peer.sin_addr.s_addr = htonl(INADDR_ANY);
  4754. soap->peer.sin_port = htons((short)port);
  4755. soap->errmode = 0;
  4756. if (bind(soap->master, (struct sockaddr*)&soap->peer, (int)soap->peerlen))
  4757. { soap->errnum = soap_socket_errno(soap->master);
  4758. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host\n"));
  4759. soap_closesock(soap);
  4760. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in soap_bind()", SOAP_TCP_ERROR);
  4761. return SOAP_INVALID_SOCKET;
  4762. }
  4763. #endif
  4764. if (!(soap->omode & SOAP_IO_UDP) && listen(soap->master, backlog))
  4765. { soap->errnum = soap_socket_errno(soap->master);
  4766. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host\n"));
  4767. soap_closesock(soap);
  4768. soap_set_receiver_error(soap, tcp_error(soap), "listen failed in soap_bind()", SOAP_TCP_ERROR);
  4769. return SOAP_INVALID_SOCKET;
  4770. }
  4771. return soap->master;
  4772. }
  4773. #endif
  4774. #endif
  4775. /******************************************************************************/
  4776. #ifndef WITH_NOIO
  4777. #ifndef PALM_1
  4778. SOAP_FMAC1
  4779. int
  4780. SOAP_FMAC2
  4781. soap_poll(struct soap *soap)
  4782. {
  4783. #ifndef WITH_LEAN
  4784. register int r;
  4785. if (soap_valid_socket(soap->socket))
  4786. { r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_ALL, 0);
  4787. if (r > 0 && (r & SOAP_TCP_SELECT_ERR))
  4788. r = -1;
  4789. }
  4790. else if (soap_valid_socket(soap->master))
  4791. r = tcp_select(soap, soap->master, SOAP_TCP_SELECT_SND, 0);
  4792. else
  4793. return SOAP_OK; /* OK when no socket! */
  4794. if (r > 0)
  4795. {
  4796. #ifdef WITH_OPENSSL
  4797. if (soap->imode & SOAP_ENC_SSL)
  4798. {
  4799. if (soap_valid_socket(soap->socket)
  4800. && (r & SOAP_TCP_SELECT_SND)
  4801. && (!(r & SOAP_TCP_SELECT_RCV)
  4802. || SSL_peek(soap->ssl, soap->tmpbuf, 1) > 0))
  4803. return SOAP_OK;
  4804. }
  4805. else
  4806. #endif
  4807. { int t;
  4808. if (soap_valid_socket(soap->socket)
  4809. && (r & SOAP_TCP_SELECT_SND)
  4810. && (!(r & SOAP_TCP_SELECT_RCV)
  4811. || recv(soap->socket, (char*)&t, 1, MSG_PEEK) > 0))
  4812. return SOAP_OK;
  4813. }
  4814. }
  4815. else if (r < 0)
  4816. { if ((soap_valid_socket(soap->master) || soap_valid_socket(soap->socket)) && soap_socket_errno(soap->master) != SOAP_EINTR)
  4817. { soap_set_receiver_error(soap, tcp_error(soap), "select failed in soap_poll()", SOAP_TCP_ERROR);
  4818. return soap->error = SOAP_TCP_ERROR;
  4819. }
  4820. }
  4821. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Polling: other end down on socket=%d select=%d\n", soap->socket, r));
  4822. return SOAP_EOF;
  4823. #else
  4824. return SOAP_OK;
  4825. #endif
  4826. }
  4827. #endif
  4828. #endif
  4829. /******************************************************************************/
  4830. #ifndef WITH_NOIO
  4831. #ifndef PALM_1
  4832. SOAP_FMAC1
  4833. SOAP_SOCKET
  4834. SOAP_FMAC2
  4835. soap_accept(struct soap *soap)
  4836. { int n = (int)sizeof(soap->peer);
  4837. register int err;
  4838. #ifndef WITH_LEAN
  4839. #ifndef WIN32
  4840. int len = SOAP_BUFLEN;
  4841. #else
  4842. int len = SOAP_BUFLEN + 1; /* speeds up windows xfer */
  4843. #endif
  4844. int set = 1;
  4845. #endif
  4846. soap->error = SOAP_OK;
  4847. memset((void*)&soap->peer, 0, sizeof(soap->peer));
  4848. soap->socket = SOAP_INVALID_SOCKET;
  4849. soap->errmode = 0;
  4850. soap->keep_alive = 0;
  4851. if (!soap_valid_socket(soap->master))
  4852. { soap->errnum = 0;
  4853. soap_set_receiver_error(soap, tcp_error(soap), "no master socket in soap_accept()", SOAP_TCP_ERROR);
  4854. return SOAP_INVALID_SOCKET;
  4855. }
  4856. #ifndef WITH_LEAN
  4857. if ((soap->omode & SOAP_IO_UDP))
  4858. return soap->socket = soap->master;
  4859. #endif
  4860. for (;;)
  4861. { if (soap->accept_timeout || soap->send_timeout || soap->recv_timeout)
  4862. { for (;;)
  4863. { register int r;
  4864. r = tcp_select(soap, soap->master, SOAP_TCP_SELECT_ALL, soap->accept_timeout ? soap->accept_timeout : 60);
  4865. if (r > 0)
  4866. break;
  4867. if (!r && soap->accept_timeout)
  4868. { soap_set_receiver_error(soap, "Timeout", "accept failed in soap_accept()", SOAP_TCP_ERROR);
  4869. return SOAP_INVALID_SOCKET;
  4870. }
  4871. if (r < 0)
  4872. { r = soap->errnum;
  4873. if (r != SOAP_EINTR)
  4874. { soap_closesock(soap);
  4875. soap_set_sender_error(soap, tcp_error(soap), "accept failed in soap_accept()", SOAP_TCP_ERROR);
  4876. return SOAP_INVALID_SOCKET;
  4877. }
  4878. }
  4879. }
  4880. }
  4881. if (soap->accept_timeout)
  4882. SOAP_SOCKNONBLOCK(soap->master)
  4883. else
  4884. SOAP_SOCKBLOCK(soap->master)
  4885. soap->socket = soap->faccept(soap, soap->master, (struct sockaddr*)&soap->peer, &n);
  4886. soap->peerlen = (size_t)n;
  4887. if (soap_valid_socket(soap->socket))
  4888. {
  4889. #ifdef WITH_IPV6
  4890. unsigned int ip1, ip2, ip3, ip4;
  4891. char port[16];
  4892. getnameinfo((struct sockaddr*)&soap->peer, n, soap->host, sizeof(soap->host), port, 16, NI_NUMERICHOST | NI_NUMERICSERV);
  4893. sscanf(soap->host, "%u.%u.%u.%u", &ip1, &ip2, &ip3, &ip4);
  4894. soap->ip = (unsigned long)ip1 << 24 | (unsigned long)ip2 << 16 | (unsigned long)ip3 << 8 | (unsigned long)ip4;
  4895. soap->port = soap_strtol(port, NULL, 10);
  4896. #else
  4897. soap->ip = ntohl(soap->peer.sin_addr.s_addr);
  4898. #ifdef HAVE_SNPRINTF
  4899. soap_snprintf(soap->host, sizeof(soap->host), "%u.%u.%u.%u", (int)(soap->ip>>24)&0xFF, (int)(soap->ip>>16)&0xFF, (int)(soap->ip>>8)&0xFF, (int)soap->ip&0xFF);
  4900. #else
  4901. sprintf(soap->host, "%u.%u.%u.%u", (int)(soap->ip>>24)&0xFF, (int)(soap->ip>>16)&0xFF, (int)(soap->ip>>8)&0xFF, (int)soap->ip&0xFF);
  4902. #endif
  4903. soap->port = (int)ntohs(soap->peer.sin_port); /* does not return port number on some systems */
  4904. #endif
  4905. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Accept socket=%d at port=%d from IP='%s'\n", soap->socket, soap->port, soap->host));
  4906. #ifndef WITH_LEAN
  4907. if (soap->accept_flags == SO_LINGER)
  4908. { struct linger linger;
  4909. memset((void*)&linger, 0, sizeof(linger));
  4910. linger.l_onoff = 1;
  4911. linger.l_linger = soap->linger_time;
  4912. if (setsockopt(soap->socket, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(struct linger)))
  4913. { soap->errnum = soap_socket_errno(soap->socket);
  4914. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_LINGER failed in soap_accept()", SOAP_TCP_ERROR);
  4915. soap_closesock(soap);
  4916. return SOAP_INVALID_SOCKET;
  4917. }
  4918. }
  4919. else if (soap->accept_flags && setsockopt(soap->socket, SOL_SOCKET, soap->accept_flags, (char*)&set, sizeof(int)))
  4920. { soap->errnum = soap_socket_errno(soap->socket);
  4921. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in soap_accept()", SOAP_TCP_ERROR);
  4922. soap_closesock(soap);
  4923. return SOAP_INVALID_SOCKET;
  4924. }
  4925. if (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) && setsockopt(soap->socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&set, sizeof(int)))
  4926. { soap->errnum = soap_socket_errno(soap->socket);
  4927. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in soap_accept()", SOAP_TCP_ERROR);
  4928. soap_closesock(soap);
  4929. return SOAP_INVALID_SOCKET;
  4930. }
  4931. if (setsockopt(soap->socket, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
  4932. { soap->errnum = soap_socket_errno(soap->socket);
  4933. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in soap_accept()", SOAP_TCP_ERROR);
  4934. soap_closesock(soap);
  4935. return SOAP_INVALID_SOCKET;
  4936. }
  4937. if (setsockopt(soap->socket, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
  4938. { soap->errnum = soap_socket_errno(soap->socket);
  4939. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in soap_accept()", SOAP_TCP_ERROR);
  4940. soap_closesock(soap);
  4941. return SOAP_INVALID_SOCKET;
  4942. }
  4943. #ifdef TCP_NODELAY
  4944. if (setsockopt(soap->socket, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
  4945. { soap->errnum = soap_socket_errno(soap->socket);
  4946. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in soap_accept()", SOAP_TCP_ERROR);
  4947. soap_closesock(soap);
  4948. return SOAP_INVALID_SOCKET;
  4949. }
  4950. #endif
  4951. #endif
  4952. soap->keep_alive = (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) != 0);
  4953. if (soap->send_timeout || soap->recv_timeout)
  4954. SOAP_SOCKNONBLOCK(soap->socket)
  4955. else
  4956. SOAP_SOCKBLOCK(soap->socket)
  4957. return soap->socket;
  4958. }
  4959. err = soap_socket_errno(soap->socket);
  4960. if (err != 0 && err != SOAP_EINTR && err != SOAP_EAGAIN && err != SOAP_EWOULDBLOCK)
  4961. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Accept failed from %s\n", soap->host));
  4962. soap->errnum = err;
  4963. soap_set_receiver_error(soap, tcp_error(soap), "accept failed in soap_accept()", SOAP_TCP_ERROR);
  4964. soap_closesock(soap);
  4965. return SOAP_INVALID_SOCKET;
  4966. }
  4967. }
  4968. }
  4969. #endif
  4970. #endif
  4971. /******************************************************************************/
  4972. #ifndef PALM_1
  4973. SOAP_FMAC1
  4974. int
  4975. SOAP_FMAC2
  4976. soap_closesock(struct soap *soap)
  4977. { register int status = soap->error;
  4978. #ifndef WITH_LEANER
  4979. if (status) /* close on error: attachment state is not to be trusted */
  4980. { soap->mime.first = NULL;
  4981. soap->mime.last = NULL;
  4982. soap->dime.first = NULL;
  4983. soap->dime.last = NULL;
  4984. }
  4985. #endif
  4986. if (soap->fdisconnect && (soap->error = soap->fdisconnect(soap)))
  4987. return soap->error;
  4988. if (status == SOAP_EOF || status == SOAP_TCP_ERROR || status == SOAP_SSL_ERROR || !soap->keep_alive)
  4989. { if (soap->fclose && (soap->error = soap->fclose(soap)))
  4990. return soap->error;
  4991. soap->keep_alive = 0;
  4992. }
  4993. #ifdef WITH_ZLIB
  4994. if (!(soap->mode & SOAP_MIME_POSTCHECK))
  4995. { if (soap->zlib_state == SOAP_ZLIB_DEFLATE)
  4996. deflateEnd(soap->d_stream);
  4997. else if (soap->zlib_state == SOAP_ZLIB_INFLATE)
  4998. inflateEnd(soap->d_stream);
  4999. soap->zlib_state = SOAP_ZLIB_NONE;
  5000. }
  5001. #endif
  5002. return soap->error = status;
  5003. }
  5004. #endif
  5005. /******************************************************************************/
  5006. #ifndef WITH_NOIO
  5007. #ifndef PALM_1
  5008. SOAP_FMAC1
  5009. int
  5010. SOAP_FMAC2
  5011. soap_force_closesock(struct soap *soap)
  5012. { soap->keep_alive = 0;
  5013. if (soap_valid_socket(soap->socket))
  5014. return soap_closesocket(soap->socket);
  5015. return SOAP_OK;
  5016. }
  5017. #endif
  5018. #endif
  5019. /******************************************************************************/
  5020. #ifndef WITH_NOIO
  5021. #ifndef PALM_2
  5022. SOAP_FMAC1
  5023. void
  5024. SOAP_FMAC2
  5025. soap_cleanup(struct soap *soap)
  5026. { soap_done(soap);
  5027. #ifdef WIN32
  5028. if (!tcp_done)
  5029. return;
  5030. tcp_done = 0;
  5031. WSACleanup();
  5032. #endif
  5033. }
  5034. #endif
  5035. #endif
  5036. /******************************************************************************/
  5037. #ifndef PALM_1
  5038. SOAP_FMAC1
  5039. void
  5040. SOAP_FMAC2
  5041. soap_done(struct soap *soap)
  5042. {
  5043. #ifdef SOAP_DEBUG
  5044. int i;
  5045. #endif
  5046. if (soap_check_state(soap))
  5047. return;
  5048. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Done with context%s\n", soap->state == SOAP_COPY ? " copy" : ""));
  5049. soap_free_temp(soap);
  5050. while (soap->clist)
  5051. { struct soap_clist *p = soap->clist->next;
  5052. SOAP_FREE(soap, soap->clist);
  5053. soap->clist = p;
  5054. }
  5055. if (soap->state == SOAP_INIT)
  5056. soap->omode &= ~SOAP_IO_UDP; /* to force close the socket */
  5057. soap->keep_alive = 0; /* to force close the socket */
  5058. if (soap->master == soap->socket) /* do not close twice */
  5059. soap->master = SOAP_INVALID_SOCKET;
  5060. soap_closesock(soap);
  5061. #ifdef WITH_COOKIES
  5062. soap_free_cookies(soap);
  5063. #endif
  5064. while (soap->plugins)
  5065. { register struct soap_plugin *p = soap->plugins->next;
  5066. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Removing plugin '%s'\n", soap->plugins->id));
  5067. if (soap->plugins->fcopy || soap->state == SOAP_INIT)
  5068. soap->plugins->fdelete(soap, soap->plugins);
  5069. SOAP_FREE(soap, soap->plugins);
  5070. soap->plugins = p;
  5071. }
  5072. soap->fplugin = fplugin;
  5073. soap->fmalloc = NULL;
  5074. #ifndef WITH_NOHTTP
  5075. soap->fpost = http_post;
  5076. soap->fget = http_get;
  5077. soap->fput = http_405;
  5078. soap->fdel = http_405;
  5079. soap->fopt = http_200;
  5080. soap->fhead = http_200;
  5081. soap->fform = NULL;
  5082. soap->fposthdr = http_post_header;
  5083. soap->fresponse = http_response;
  5084. soap->fparse = http_parse;
  5085. soap->fparsehdr = http_parse_header;
  5086. #endif
  5087. soap->fheader = NULL;
  5088. #ifndef WITH_NOIO
  5089. #ifndef WITH_IPV6
  5090. soap->fresolve = tcp_gethost;
  5091. #else
  5092. soap->fresolve = NULL;
  5093. #endif
  5094. soap->faccept = tcp_accept;
  5095. soap->fopen = tcp_connect;
  5096. soap->fclose = tcp_disconnect;
  5097. soap->fclosesocket = tcp_closesocket;
  5098. soap->fshutdownsocket = tcp_shutdownsocket;
  5099. soap->fsend = fsend;
  5100. soap->frecv = frecv;
  5101. soap->fpoll = soap_poll;
  5102. #else
  5103. soap->fopen = NULL;
  5104. soap->fclose = NULL;
  5105. soap->fpoll = NULL;
  5106. #endif
  5107. #ifndef WITH_LEANER
  5108. soap->feltbegin = NULL;
  5109. soap->feltendin = NULL;
  5110. soap->feltbegout = NULL;
  5111. soap->feltendout = NULL;
  5112. soap->fprepareinitsend = NULL;
  5113. soap->fprepareinitrecv = NULL;
  5114. soap->fpreparesend = NULL;
  5115. soap->fpreparerecv = NULL;
  5116. soap->fpreparefinalsend = NULL;
  5117. soap->fpreparefinalrecv = NULL;
  5118. soap->ffiltersend = NULL;
  5119. soap->ffilterrecv = NULL;
  5120. #endif
  5121. soap->fseterror = NULL;
  5122. soap->fignore = NULL;
  5123. soap->fserveloop = NULL;
  5124. #ifdef WITH_OPENSSL
  5125. if (soap->session)
  5126. { SSL_SESSION_free(soap->session);
  5127. soap->session = NULL;
  5128. }
  5129. #endif
  5130. if (soap->state == SOAP_INIT)
  5131. { if (soap_valid_socket(soap->master))
  5132. { soap->fclosesocket(soap, soap->master);
  5133. soap->master = SOAP_INVALID_SOCKET;
  5134. }
  5135. }
  5136. #ifdef WITH_OPENSSL
  5137. if (soap->ssl)
  5138. { SSL_free(soap->ssl);
  5139. soap->ssl = NULL;
  5140. }
  5141. if (soap->state == SOAP_INIT)
  5142. { if (soap->ctx)
  5143. { SSL_CTX_free(soap->ctx);
  5144. soap->ctx = NULL;
  5145. }
  5146. }
  5147. ERR_remove_state(0);
  5148. #endif
  5149. #ifdef WITH_GNUTLS
  5150. if (soap->state == SOAP_INIT)
  5151. { if (soap->xcred)
  5152. { gnutls_certificate_free_credentials(soap->xcred);
  5153. soap->xcred = NULL;
  5154. }
  5155. if (soap->acred)
  5156. { gnutls_anon_free_client_credentials(soap->acred);
  5157. soap->acred = NULL;
  5158. }
  5159. if (soap->cache)
  5160. { gnutls_priority_deinit(soap->cache);
  5161. soap->cache = NULL;
  5162. }
  5163. if (soap->dh_params)
  5164. { gnutls_dh_params_deinit(soap->dh_params);
  5165. soap->dh_params = NULL;
  5166. }
  5167. if (soap->rsa_params)
  5168. { gnutls_rsa_params_deinit(soap->rsa_params);
  5169. soap->rsa_params = NULL;
  5170. }
  5171. }
  5172. if (soap->session)
  5173. { gnutls_deinit(soap->session);
  5174. soap->session = NULL;
  5175. }
  5176. #endif
  5177. #ifdef WITH_C_LOCALE
  5178. # ifdef WIN32
  5179. _free_locale(soap->c_locale);
  5180. # else
  5181. freelocale(soap->c_locale);
  5182. # endif
  5183. #endif
  5184. #ifdef WITH_ZLIB
  5185. if (soap->d_stream)
  5186. { SOAP_FREE(soap, (void*)soap->d_stream);
  5187. soap->d_stream = NULL;
  5188. }
  5189. if (soap->z_buf)
  5190. { SOAP_FREE(soap, (void*)soap->z_buf);
  5191. soap->z_buf = NULL;
  5192. }
  5193. #endif
  5194. #ifdef SOAP_DEBUG
  5195. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free logfiles\n"));
  5196. for (i = 0; i < SOAP_MAXLOGS; i++)
  5197. { if (soap->logfile[i])
  5198. { SOAP_FREE(soap, (void*)soap->logfile[i]);
  5199. soap->logfile[i] = NULL;
  5200. }
  5201. soap_close_logfile(soap, i);
  5202. }
  5203. soap->state = SOAP_NONE;
  5204. #endif
  5205. #ifdef SOAP_MEM_DEBUG
  5206. soap_free_mht(soap);
  5207. #endif
  5208. }
  5209. #endif
  5210. /******************************************************************************\
  5211. *
  5212. * HTTP
  5213. *
  5214. \******************************************************************************/
  5215. /******************************************************************************/
  5216. #ifndef WITH_NOHTTP
  5217. #ifndef PALM_1
  5218. static int
  5219. http_parse(struct soap *soap)
  5220. { char header[SOAP_HDRLEN], *s;
  5221. unsigned short httpcmd = 0;
  5222. int status = 0;
  5223. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Waiting for HTTP request/response...\n"));
  5224. *soap->endpoint = '\0';
  5225. #ifdef WITH_NTLM
  5226. if (!soap->ntlm_challenge)
  5227. #endif
  5228. { soap->userid = NULL;
  5229. soap->passwd = NULL;
  5230. soap->authrealm = NULL;
  5231. }
  5232. #ifdef WITH_NTLM
  5233. soap->ntlm_challenge = NULL;
  5234. #endif
  5235. soap->proxy_from = NULL;
  5236. do
  5237. { soap->length = 0;
  5238. soap->http_content = NULL;
  5239. soap->action = NULL;
  5240. soap->status = 0;
  5241. soap->body = 1;
  5242. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  5243. { if (soap->error == SOAP_EOF)
  5244. return SOAP_EOF;
  5245. return soap->error = 414;
  5246. }
  5247. if ((s = strchr(soap->msgbuf, ' ')))
  5248. { soap->status = (unsigned short)soap_strtoul(s, &s, 10);
  5249. if (!soap_blank((soap_wchar)*s))
  5250. soap->status = 0;
  5251. }
  5252. else
  5253. soap->status = 0;
  5254. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP status: %s\n", soap->msgbuf));
  5255. for (;;)
  5256. { if (soap_getline(soap, header, SOAP_HDRLEN))
  5257. { if (soap->error == SOAP_EOF)
  5258. { soap->error = SOAP_OK;
  5259. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "EOF in HTTP header, continue anyway\n"));
  5260. break;
  5261. }
  5262. return soap->error;
  5263. }
  5264. if (!*header)
  5265. break;
  5266. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP header: %s\n", header));
  5267. s = strchr(header, ':');
  5268. if (s)
  5269. { char *t;
  5270. *s = '\0';
  5271. do s++;
  5272. while (*s && *s <= 32);
  5273. if (*s == '"')
  5274. s++;
  5275. t = s + strlen(s) - 1;
  5276. while (t > s && *t <= 32)
  5277. t--;
  5278. if (t >= s && *t == '"')
  5279. t--;
  5280. t[1] = '\0';
  5281. if ((soap->error = soap->fparsehdr(soap, header, s)))
  5282. { if (soap->error < SOAP_STOP)
  5283. return soap->error;
  5284. status = soap->error;
  5285. soap->error = SOAP_OK;
  5286. }
  5287. }
  5288. }
  5289. } while (soap->status == 100);
  5290. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Finished HTTP header parsing, status = %d\n", soap->status));
  5291. s = strstr(soap->msgbuf, "HTTP/");
  5292. if (s && s[7] != '1')
  5293. { if (soap->keep_alive == 1)
  5294. soap->keep_alive = 0;
  5295. if (soap->status == 0 && (soap->omode & SOAP_IO) == SOAP_IO_CHUNK) /* soap->status == 0 for HTTP request */
  5296. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE; /* HTTP 1.0 does not support chunked transfers */
  5297. }
  5298. if (soap->keep_alive < 0)
  5299. soap->keep_alive = 1;
  5300. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Keep alive connection = %d\n", soap->keep_alive));
  5301. if (soap->status == 0)
  5302. { size_t l = 0;
  5303. if (s)
  5304. { if (!strncmp(soap->msgbuf, "POST ", l = 5))
  5305. httpcmd = 1;
  5306. else if (!strncmp(soap->msgbuf, "PUT ", l = 4))
  5307. httpcmd = 2;
  5308. else if (!strncmp(soap->msgbuf, "GET ", l = 4))
  5309. httpcmd = 3;
  5310. else if (!strncmp(soap->msgbuf, "DELETE ", l = 7))
  5311. httpcmd = 4;
  5312. else if (!strncmp(soap->msgbuf, "OPTIONS ", l = 8))
  5313. httpcmd = 5;
  5314. else if (!strncmp(soap->msgbuf, "HEAD ", l = 5))
  5315. httpcmd = 6;
  5316. }
  5317. if (s && httpcmd)
  5318. { size_t m = strlen(soap->endpoint);
  5319. size_t n = m + (s - soap->msgbuf) - l - 1;
  5320. size_t k;
  5321. if (n >= sizeof(soap->endpoint))
  5322. n = sizeof(soap->endpoint) - 1;
  5323. if (m > n)
  5324. m = n;
  5325. k = n - m + 1;
  5326. if (k > sizeof(soap->path))
  5327. k = sizeof(soap->path);
  5328. strncpy(soap->path, soap->msgbuf + l, k);
  5329. soap->path[k - 1] = '\0';
  5330. if (*soap->path && *soap->path != '/')
  5331. *soap->endpoint = '\0';
  5332. strcat(soap->endpoint, soap->path);
  5333. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Target endpoint='%s'\n", soap->endpoint));
  5334. if (httpcmd > 1)
  5335. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP %s handler\n", soap->msgbuf));
  5336. switch (httpcmd)
  5337. { case 2: soap->error = soap->fput(soap); break;
  5338. case 3: soap->error = soap->fget(soap); break;
  5339. case 4: soap->error = soap->fdel(soap); break;
  5340. case 5: soap->error = soap->fopt(soap); break;
  5341. case 6: soap->error = soap->fhead(soap); break;
  5342. default: soap->error = 405; break;
  5343. }
  5344. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP handler return = %d\n", soap->error));
  5345. if (soap->error == SOAP_OK)
  5346. soap->error = SOAP_STOP; /* prevents further processing */
  5347. return soap->error;
  5348. }
  5349. if (status)
  5350. return soap->error = status;
  5351. }
  5352. else if (status)
  5353. return soap->error = status;
  5354. else if (s)
  5355. return soap->error = 405;
  5356. return SOAP_OK;
  5357. }
  5358. #if 0
  5359. if (soap->length > 0 || (soap->http_content && (!soap->keep_alive || soap->recv_timeout)) || (soap->imode & SOAP_IO) == SOAP_IO_CHUNK)
  5360. #endif
  5361. if (soap->body)
  5362. { if ((soap->status >= 200 && soap->status <= 299) /* OK, Accepted, etc */
  5363. || soap->status == 400 /* Bad Request */
  5364. || soap->status == 500) /* Internal Server Error */
  5365. return SOAP_OK;
  5366. /* force close afterwards in soap_closesock() */
  5367. soap->keep_alive = 0;
  5368. #ifndef WITH_LEAN
  5369. /* read HTTP body for error details */
  5370. s = soap_get_http_body(soap, NULL);
  5371. if (s)
  5372. return soap_set_receiver_error(soap, soap->msgbuf, s, soap->status);
  5373. #endif
  5374. }
  5375. else if (soap->status >= 200 && soap->status <= 299)
  5376. return soap->error = soap->status;
  5377. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP error %d\n", soap->status));
  5378. return soap_set_receiver_error(soap, "HTTP Error", soap->msgbuf, soap->status);
  5379. }
  5380. #endif
  5381. #endif
  5382. /******************************************************************************/
  5383. #ifndef WITH_NOHTTP
  5384. #ifndef PALM_1
  5385. static int
  5386. http_parse_header(struct soap *soap, const char *key, const char *val)
  5387. { if (!soap_tag_cmp(key, "Host"))
  5388. {
  5389. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  5390. if (soap->imode & SOAP_ENC_SSL)
  5391. strcpy(soap->endpoint, "https://");
  5392. else
  5393. #endif
  5394. strcpy(soap->endpoint, "http://");
  5395. strncat(soap->endpoint, val, sizeof(soap->endpoint) - 8);
  5396. }
  5397. #ifndef WITH_LEANER
  5398. else if (!soap_tag_cmp(key, "Content-Type"))
  5399. { const char *action;
  5400. soap->http_content = soap_strdup(soap, val);
  5401. if (soap_get_header_attribute(soap, val, "application/dime"))
  5402. soap->imode |= SOAP_ENC_DIME;
  5403. else if (soap_get_header_attribute(soap, val, "multipart/related")
  5404. || soap_get_header_attribute(soap, val, "multipart/form-data"))
  5405. { soap->mime.boundary = soap_strdup(soap, soap_get_header_attribute(soap, val, "boundary"));
  5406. soap->mime.start = soap_strdup(soap, soap_get_header_attribute(soap, val, "start"));
  5407. soap->imode |= SOAP_ENC_MIME;
  5408. }
  5409. action = soap_get_header_attribute(soap, val, "action");
  5410. if (action)
  5411. { if (*action == '"')
  5412. { soap->action = soap_strdup(soap, action + 1);
  5413. if (*soap->action)
  5414. soap->action[strlen(soap->action) - 1] = '\0';
  5415. }
  5416. else
  5417. soap->action = soap_strdup(soap, action);
  5418. }
  5419. }
  5420. #endif
  5421. else if (!soap_tag_cmp(key, "Content-Length"))
  5422. { soap->length = soap_strtoul(val, NULL, 10);
  5423. if (!soap->length)
  5424. soap->body = 0;
  5425. }
  5426. else if (!soap_tag_cmp(key, "Content-Encoding"))
  5427. { if (!soap_tag_cmp(val, "deflate"))
  5428. #ifdef WITH_ZLIB
  5429. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  5430. #else
  5431. return SOAP_ZLIB_ERROR;
  5432. #endif
  5433. else if (!soap_tag_cmp(val, "gzip"))
  5434. #ifdef WITH_GZIP
  5435. soap->zlib_in = SOAP_ZLIB_GZIP;
  5436. #else
  5437. return SOAP_ZLIB_ERROR;
  5438. #endif
  5439. }
  5440. #ifdef WITH_ZLIB
  5441. else if (!soap_tag_cmp(key, "Accept-Encoding"))
  5442. {
  5443. #ifdef WITH_GZIP
  5444. if (strchr(val, '*') || soap_get_header_attribute(soap, val, "gzip"))
  5445. soap->zlib_out = SOAP_ZLIB_GZIP;
  5446. else
  5447. #endif
  5448. if (strchr(val, '*') || soap_get_header_attribute(soap, val, "deflate"))
  5449. soap->zlib_out = SOAP_ZLIB_DEFLATE;
  5450. else
  5451. soap->zlib_out = SOAP_ZLIB_NONE;
  5452. }
  5453. #endif
  5454. else if (!soap_tag_cmp(key, "Transfer-Encoding"))
  5455. { soap->imode &= ~SOAP_IO;
  5456. if (!soap_tag_cmp(val, "chunked"))
  5457. soap->imode |= SOAP_IO_CHUNK;
  5458. }
  5459. else if (!soap_tag_cmp(key, "Connection"))
  5460. { if (!soap_tag_cmp(val, "keep-alive"))
  5461. soap->keep_alive = -soap->keep_alive;
  5462. else if (!soap_tag_cmp(val, "close"))
  5463. soap->keep_alive = 0;
  5464. }
  5465. #ifndef WITH_LEAN
  5466. else if (!soap_tag_cmp(key, "Authorization") || !soap_tag_cmp(key, "Proxy-Authorization"))
  5467. {
  5468. #ifdef WITH_NTLM
  5469. if (!soap_tag_cmp(val, "NTLM*"))
  5470. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  5471. else
  5472. #endif
  5473. if (!soap_tag_cmp(val, "Basic *"))
  5474. { int n;
  5475. char *s;
  5476. soap_base642s(soap, val + 6, soap->tmpbuf, sizeof(soap->tmpbuf) - 1, &n);
  5477. soap->tmpbuf[n] = '\0';
  5478. if ((s = strchr(soap->tmpbuf, ':')))
  5479. { *s = '\0';
  5480. soap->userid = soap_strdup(soap, soap->tmpbuf);
  5481. soap->passwd = soap_strdup(soap, s + 1);
  5482. }
  5483. }
  5484. }
  5485. else if (!soap_tag_cmp(key, "WWW-Authenticate") || !soap_tag_cmp(key, "Proxy-Authenticate"))
  5486. {
  5487. #ifdef WITH_NTLM
  5488. if (!soap_tag_cmp(val, "NTLM*"))
  5489. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  5490. else
  5491. #endif
  5492. soap->authrealm = soap_strdup(soap, soap_get_header_attribute(soap, val + 6, "realm"));
  5493. }
  5494. else if (!soap_tag_cmp(key, "Expect"))
  5495. { if (!soap_tag_cmp(val, "100-continue"))
  5496. { if ((soap->error = soap->fposthdr(soap, "HTTP/1.1 100 Continue", NULL))
  5497. || (soap->error = soap->fposthdr(soap, NULL, NULL)))
  5498. return soap->error;
  5499. }
  5500. }
  5501. #endif
  5502. else if (!soap_tag_cmp(key, "SOAPAction"))
  5503. { if (*val == '"')
  5504. { soap->action = soap_strdup(soap, val + 1);
  5505. if (*soap->action)
  5506. soap->action[strlen(soap->action) - 1] = '\0';
  5507. }
  5508. else
  5509. soap->action = soap_strdup(soap, val);
  5510. }
  5511. else if (!soap_tag_cmp(key, "Location"))
  5512. { strncpy(soap->endpoint, val, sizeof(soap->endpoint));
  5513. soap->endpoint[sizeof(soap->endpoint) - 1] = '\0';
  5514. }
  5515. else if (!soap_tag_cmp(key, "X-Forwarded-For"))
  5516. { soap->proxy_from = soap_strdup(soap, val);
  5517. }
  5518. #ifdef WITH_COOKIES
  5519. else if (!soap_tag_cmp(key, "Cookie")
  5520. || !soap_tag_cmp(key, "Cookie2")
  5521. || !soap_tag_cmp(key, "Set-Cookie")
  5522. || !soap_tag_cmp(key, "Set-Cookie2"))
  5523. { soap_getcookies(soap, val);
  5524. }
  5525. #endif
  5526. return SOAP_OK;
  5527. }
  5528. #endif
  5529. #endif
  5530. /******************************************************************************/
  5531. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5532. #ifndef PALM_1
  5533. SOAP_FMAC1
  5534. const char*
  5535. SOAP_FMAC2
  5536. soap_get_header_attribute(struct soap *soap, const char *line, const char *key)
  5537. { register const char *s = line;
  5538. if (s)
  5539. { while (*s)
  5540. { register short flag;
  5541. s = soap_decode_key(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  5542. flag = soap_tag_cmp(soap->tmpbuf, key);
  5543. s = soap_decode_val(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  5544. if (!flag)
  5545. return soap->tmpbuf;
  5546. }
  5547. }
  5548. return NULL;
  5549. }
  5550. #endif
  5551. #endif
  5552. /******************************************************************************/
  5553. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5554. #ifndef PALM_1
  5555. SOAP_FMAC1
  5556. const char*
  5557. SOAP_FMAC2
  5558. soap_decode_key(char *buf, size_t len, const char *val)
  5559. { return soap_decode(buf, len, val, "=,;");
  5560. }
  5561. #endif
  5562. #endif
  5563. /******************************************************************************/
  5564. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5565. #ifndef PALM_1
  5566. SOAP_FMAC1
  5567. const char*
  5568. SOAP_FMAC2
  5569. soap_decode_val(char *buf, size_t len, const char *val)
  5570. { if (*val != '=')
  5571. { *buf = '\0';
  5572. return val;
  5573. }
  5574. return soap_decode(buf, len, val + 1, ",;");
  5575. }
  5576. #endif
  5577. #endif
  5578. /******************************************************************************/
  5579. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5580. #ifndef PALM_1
  5581. static const char*
  5582. soap_decode(char *buf, size_t len, const char *val, const char *sep)
  5583. { const char *s;
  5584. char *t = buf;
  5585. size_t i = len;
  5586. for (s = val; *s; s++)
  5587. if (*s != ' ' && *s != '\t' && !strchr(sep, *s))
  5588. break;
  5589. if (len > 0)
  5590. { if (*s == '"')
  5591. { s++;
  5592. while (*s && *s != '"' && --i)
  5593. *t++ = *s++;
  5594. }
  5595. else
  5596. { while (*s && !soap_blank((soap_wchar)*s) && !strchr(sep, *s) && --i)
  5597. { if (*s == '%' && s[1] && s[2])
  5598. { *t++ = ((s[1] >= 'A' ? (s[1] & 0x7) + 9 : s[1] - '0') << 4)
  5599. + (s[2] >= 'A' ? (s[2] & 0x7) + 9 : s[2] - '0');
  5600. s += 3;
  5601. }
  5602. else
  5603. *t++ = *s++;
  5604. }
  5605. }
  5606. buf[len - 1] = '\0'; /* appease */
  5607. }
  5608. *t = '\0';
  5609. while (*s && !strchr(sep, *s))
  5610. s++;
  5611. return s;
  5612. }
  5613. #endif
  5614. #endif
  5615. /******************************************************************************/
  5616. #ifndef WITH_NOHTTP
  5617. #ifndef PALM_1
  5618. static const char*
  5619. http_error(struct soap *soap, int status)
  5620. { register const char *msg = SOAP_STR_EOS;
  5621. (void)soap;
  5622. #ifndef WITH_LEAN
  5623. msg = soap_code_str(h_http_error_codes, status);
  5624. if (!msg)
  5625. msg = SOAP_STR_EOS;
  5626. #endif
  5627. return msg;
  5628. }
  5629. #endif
  5630. #endif
  5631. /******************************************************************************/
  5632. #ifndef WITH_NOHTTP
  5633. #ifndef PALM_1
  5634. static int
  5635. http_get(struct soap *soap)
  5636. { (void)soap;
  5637. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP GET request\n"));
  5638. return SOAP_GET_METHOD;
  5639. }
  5640. #endif
  5641. #endif
  5642. /******************************************************************************/
  5643. #ifndef WITH_NOHTTP
  5644. #ifndef PALM_1
  5645. static int
  5646. http_405(struct soap *soap)
  5647. { (void)soap;
  5648. return 405;
  5649. }
  5650. #endif
  5651. #endif
  5652. /******************************************************************************/
  5653. #ifndef WITH_NOHTTP
  5654. #ifndef PALM_1
  5655. static int
  5656. http_200(struct soap *soap)
  5657. { return soap_send_empty_response(soap, 200);
  5658. }
  5659. #endif
  5660. #endif
  5661. /******************************************************************************/
  5662. #ifndef WITH_NOHTTP
  5663. #ifndef PALM_1
  5664. static int
  5665. http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action, size_t count)
  5666. { register const char *s;
  5667. register int err;
  5668. switch (soap->status)
  5669. { case SOAP_GET:
  5670. s = "GET";
  5671. break;
  5672. case SOAP_PUT:
  5673. s = "PUT";
  5674. break;
  5675. case SOAP_DEL:
  5676. s = "DELETE";
  5677. break;
  5678. case SOAP_CONNECT:
  5679. s = "CONNECT";
  5680. break;
  5681. default:
  5682. s = "POST";
  5683. }
  5684. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP %s to %s\n", s, endpoint ? endpoint : "(null)"));
  5685. #ifdef PALM
  5686. if (!endpoint || (soap_tag_cmp(endpoint, "http:*") && soap_tag_cmp(endpoint, "https:*") && strncmp(endpoint, "httpg:", 6)) && strncmp(endpoint, "_beam:", 6) && strncmp(endpoint, "_local:", 7) && strncmp(endpoint, "_btobex:", 8))
  5687. #else
  5688. if (!endpoint || (soap_tag_cmp(endpoint, "http:*") && soap_tag_cmp(endpoint, "https:*") && strncmp(endpoint, "httpg:", 6)))
  5689. #endif
  5690. return SOAP_OK;
  5691. if (strlen(endpoint) + strlen(soap->http_version) > sizeof(soap->tmpbuf) - 80
  5692. || strlen(host) + strlen(soap->http_version) > sizeof(soap->tmpbuf) - 80)
  5693. return soap->error = SOAP_EOM; /* prevent overrun (note that 'host' and 'soap->host' are substrings of 'endpoint') */
  5694. if (soap->status == SOAP_CONNECT)
  5695. {
  5696. #ifdef HAVE_SNPRINTF
  5697. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s %s:%d HTTP/%s", s, soap->host, soap->port, soap->http_version);
  5698. #else
  5699. sprintf(soap->tmpbuf, "%s %s:%d HTTP/%s", s, soap->host, soap->port, soap->http_version);
  5700. #endif
  5701. }
  5702. else if (soap->proxy_host && endpoint)
  5703. {
  5704. #ifdef HAVE_SNPRINTF
  5705. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s %s HTTP/%s", s, endpoint, soap->http_version);
  5706. #else
  5707. sprintf(soap->tmpbuf, "%s %s HTTP/%s", s, endpoint, soap->http_version);
  5708. #endif
  5709. }
  5710. else
  5711. {
  5712. #ifdef HAVE_SNPRINTF
  5713. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s /%s HTTP/%s", s, (*path == '/' ? path + 1 : path), soap->http_version);
  5714. #else
  5715. sprintf(soap->tmpbuf, "%s /%s HTTP/%s", s, (*path == '/' ? path + 1 : path), soap->http_version);
  5716. #endif
  5717. }
  5718. if ((err = soap->fposthdr(soap, soap->tmpbuf, NULL)))
  5719. return err;
  5720. #ifdef WITH_OPENSSL
  5721. if ((soap->ssl && port != 443) || (!soap->ssl && port != 80))
  5722. #else
  5723. if (port != 80)
  5724. #endif
  5725. {
  5726. #ifdef WITH_IPV6
  5727. if (*host != '[' && strchr(host, ':'))
  5728. {
  5729. #ifdef HAVE_SNPRINTF
  5730. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "[%s]:%d", host, port); /* RFC 2732 */
  5731. #else
  5732. sprintf(soap->tmpbuf, "[%s]:%d", host, port); /* RFC 2732 */
  5733. #endif
  5734. }
  5735. else
  5736. #endif
  5737. {
  5738. #ifdef HAVE_SNPRINTF
  5739. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s:%d", host, port);
  5740. #else
  5741. sprintf(soap->tmpbuf, "%s:%d", host, port);
  5742. #endif
  5743. }
  5744. }
  5745. else
  5746. {
  5747. #ifdef WITH_IPV6
  5748. if (*host != '[' && strchr(host, ':'))
  5749. {
  5750. #ifdef HAVE_SNPRINTF
  5751. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "[%s]", host); /* RFC 2732 */
  5752. #else
  5753. sprintf(soap->tmpbuf, "[%s]", host); /* RFC 2732 */
  5754. #endif
  5755. }
  5756. else
  5757. #endif
  5758. strcpy(soap->tmpbuf, host);
  5759. }
  5760. if ((err = soap->fposthdr(soap, "Host", soap->tmpbuf)))
  5761. return err;
  5762. if ((err = soap->fposthdr(soap, "User-Agent", "gSOAP/2.8")))
  5763. return err;
  5764. if ((err = soap_puthttphdr(soap, SOAP_OK, count)))
  5765. return err;
  5766. #ifdef WITH_ZLIB
  5767. #ifdef WITH_GZIP
  5768. if ((err = soap->fposthdr(soap, "Accept-Encoding", "gzip, deflate")))
  5769. #else
  5770. if ((err = soap->fposthdr(soap, "Accept-Encoding", "deflate")))
  5771. #endif
  5772. return err;
  5773. #endif
  5774. #ifndef WITH_LEAN
  5775. #ifdef WITH_NTLM
  5776. if (soap->ntlm_challenge && strlen(soap->ntlm_challenge) + 6 < sizeof(soap->tmpbuf))
  5777. { if (*soap->ntlm_challenge)
  5778. {
  5779. #ifdef HAVE_SNPRINTF
  5780. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "NTLM %s", soap->ntlm_challenge);
  5781. #else
  5782. sprintf(soap->tmpbuf, "NTLM %s", soap->ntlm_challenge);
  5783. #endif
  5784. if (soap->proxy_host)
  5785. { if ((err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf)))
  5786. return err;
  5787. }
  5788. else if ((err = soap->fposthdr(soap, "Authorization", soap->tmpbuf)))
  5789. return err;
  5790. }
  5791. }
  5792. else
  5793. {
  5794. #endif
  5795. if (soap->userid && soap->passwd && strlen(soap->userid) + strlen(soap->passwd) < 761)
  5796. { strcpy(soap->tmpbuf, "Basic ");
  5797. #ifdef HAVE_SNPRINTF
  5798. soap_snprintf(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262, "%s:%s", soap->userid, soap->passwd);
  5799. #else
  5800. sprintf(soap->tmpbuf + 262, "%s:%s", soap->userid, soap->passwd);
  5801. #endif
  5802. soap_s2base64(soap, (const unsigned char*)(soap->tmpbuf + 262), soap->tmpbuf + 6, (int)strlen(soap->tmpbuf + 262));
  5803. if ((err = soap->fposthdr(soap, "Authorization", soap->tmpbuf)))
  5804. return err;
  5805. }
  5806. if (soap->proxy_userid && soap->proxy_passwd && strlen(soap->proxy_userid) + strlen(soap->proxy_passwd) < 761)
  5807. { strcpy(soap->tmpbuf, "Basic ");
  5808. #ifdef HAVE_SNPRINTF
  5809. soap_snprintf(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262, "%s:%s", soap->proxy_userid, soap->proxy_passwd);
  5810. #else
  5811. sprintf(soap->tmpbuf + 262, "%s:%s", soap->proxy_userid, soap->proxy_passwd);
  5812. #endif
  5813. soap_s2base64(soap, (const unsigned char*)(soap->tmpbuf + 262), soap->tmpbuf + 6, (int)strlen(soap->tmpbuf + 262));
  5814. if ((err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf)))
  5815. return err;
  5816. }
  5817. #ifdef WITH_NTLM
  5818. }
  5819. #endif
  5820. #endif
  5821. #ifdef WITH_COOKIES
  5822. #ifdef WITH_OPENSSL
  5823. if (soap_putcookies(soap, host, path, soap->ssl != NULL))
  5824. return soap->error;
  5825. #else
  5826. if (soap_putcookies(soap, host, path, 0))
  5827. return soap->error;
  5828. #endif
  5829. #endif
  5830. if (action && soap->status != SOAP_GET && soap->status != SOAP_DEL)
  5831. {
  5832. #ifdef HAVE_SNPRINTF
  5833. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "\"%s\"", action);
  5834. #else
  5835. sprintf(soap->tmpbuf, "\"%s\"", strlen(action) < sizeof(soap->tmpbuf) - 3 ? action : SOAP_STR_EOS);
  5836. #endif
  5837. if ((err = soap->fposthdr(soap, "SOAPAction", soap->tmpbuf)))
  5838. return err;
  5839. }
  5840. return soap->fposthdr(soap, NULL, NULL);
  5841. }
  5842. #endif
  5843. #endif
  5844. /******************************************************************************/
  5845. #ifndef WITH_NOHTTP
  5846. #ifndef PALM_1
  5847. static int
  5848. http_send_header(struct soap *soap, const char *s)
  5849. { register const char *t;
  5850. do
  5851. { t = strchr(s, '\n'); /* disallow \n in HTTP headers */
  5852. if (!t)
  5853. t = s + strlen(s);
  5854. if (soap_send_raw(soap, s, t - s))
  5855. return soap->error;
  5856. s = t + 1;
  5857. } while (*t);
  5858. return SOAP_OK;
  5859. }
  5860. #endif
  5861. #endif
  5862. /******************************************************************************/
  5863. #ifndef WITH_NOHTTP
  5864. #ifndef PALM_1
  5865. static int
  5866. http_post_header(struct soap *soap, const char *key, const char *val)
  5867. { if (key)
  5868. { if (http_send_header(soap, key))
  5869. return soap->error;
  5870. if (val && (soap_send_raw(soap, ": ", 2) || http_send_header(soap, val)))
  5871. return soap->error;
  5872. }
  5873. return soap_send_raw(soap, "\r\n", 2);
  5874. }
  5875. #endif
  5876. #endif
  5877. /******************************************************************************/
  5878. #ifndef WITH_NOHTTP
  5879. #ifndef PALM_1
  5880. static int
  5881. http_response(struct soap *soap, int status, size_t count)
  5882. { register int err;
  5883. char http[10];
  5884. int code = status;
  5885. const char *line;
  5886. #ifdef WMW_RPM_IO
  5887. if (soap->rpmreqid)
  5888. httpOutputEnable(soap->rpmreqid);
  5889. #endif
  5890. if (!soap->http_version || strlen(soap->http_version) > 4)
  5891. return soap->error = SOAP_EOM;
  5892. #ifdef WMW_RPM_IO
  5893. if (soap->rpmreqid || soap_valid_socket(soap->master) || soap_valid_socket(soap->socket)) /* RPM behaves as if standalone */
  5894. #else
  5895. if (soap_valid_socket(soap->master) || soap_valid_socket(soap->socket)) /* standalone application (socket) or CGI (stdin/out)? */
  5896. #endif
  5897. {
  5898. #ifdef HAVE_SNPRINTF
  5899. soap_snprintf(http, sizeof(http), "HTTP/%s", soap->http_version);
  5900. #else
  5901. sprintf(http, "HTTP/%s", soap->http_version);
  5902. #endif
  5903. }
  5904. else
  5905. strcpy(http, "Status:");
  5906. if (!status || status == SOAP_HTML || status == SOAP_FILE)
  5907. { if (count || ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK))
  5908. code = 200;
  5909. else
  5910. code = 202;
  5911. }
  5912. else if (status < 200 || status >= 600)
  5913. { const char *s = *soap_faultcode(soap);
  5914. if (status >= SOAP_GET_METHOD && status <= SOAP_HTTP_METHOD)
  5915. code = 405;
  5916. else if (soap->version == 2 && (!s || !strcmp(s, "SOAP-ENV:Sender")))
  5917. code = 400;
  5918. else
  5919. code = 500;
  5920. }
  5921. line = http_error(soap, code);
  5922. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP Status = %d %s\n", code, line));
  5923. #ifdef HAVE_SNPRINTF
  5924. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s %d %s", http, code, line);
  5925. #else
  5926. sprintf(soap->tmpbuf, "%s %d %s", http, code, line);
  5927. #endif
  5928. if ((err = soap->fposthdr(soap, soap->tmpbuf, NULL)))
  5929. return err;
  5930. #ifndef WITH_LEAN
  5931. if (status == 401)
  5932. {
  5933. #ifdef HAVE_SNPRINTF
  5934. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "Basic realm=\"%s\"", (soap->authrealm && strlen(soap->authrealm) < sizeof(soap->tmpbuf) - 14) ? soap->authrealm : "gSOAP Web Service");
  5935. #else
  5936. sprintf(soap->tmpbuf, "Basic realm=\"%s\"", (soap->authrealm && strlen(soap->authrealm) < sizeof(soap->tmpbuf) - 14) ? soap->authrealm : "gSOAP Web Service");
  5937. #endif
  5938. if ((err = soap->fposthdr(soap, "WWW-Authenticate", soap->tmpbuf)))
  5939. return err;
  5940. }
  5941. else if ((status >= 301 && status <= 303) || status == 307)
  5942. { if ((err = soap->fposthdr(soap, "Location", soap->endpoint)))
  5943. return err;
  5944. }
  5945. #endif
  5946. if ((err = soap->fposthdr(soap, "Server", "gSOAP/2.8"))
  5947. || (err = soap_puthttphdr(soap, status, count)))
  5948. return err;
  5949. #ifdef WITH_COOKIES
  5950. if (soap_putsetcookies(soap))
  5951. return soap->error;
  5952. #endif
  5953. return soap->fposthdr(soap, NULL, NULL);
  5954. }
  5955. #endif
  5956. #endif
  5957. /******************************************************************************/
  5958. #ifndef PALM_1
  5959. SOAP_FMAC1
  5960. int
  5961. SOAP_FMAC2
  5962. soap_response(struct soap *soap, int status)
  5963. { register size_t count;
  5964. if (!(soap->omode & (SOAP_ENC_XML | SOAP_IO_STORE /* this tests for chunking too */))
  5965. && (status == SOAP_HTML || status == SOAP_FILE))
  5966. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE;
  5967. soap->status = status;
  5968. count = soap_count_attachments(soap);
  5969. if (soap_begin_send(soap))
  5970. return soap->error;
  5971. #ifndef WITH_NOHTTP
  5972. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_XML))
  5973. { register int n = soap->mode;
  5974. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  5975. if ((n & SOAP_IO) != SOAP_IO_FLUSH)
  5976. soap->mode |= SOAP_IO_BUFFER;
  5977. if ((soap->error = soap->fresponse(soap, status, count)))
  5978. return soap->error;
  5979. #ifndef WITH_LEANER
  5980. if ((n & SOAP_IO) == SOAP_IO_CHUNK)
  5981. { if (soap_flush(soap))
  5982. return soap->error;
  5983. }
  5984. #endif
  5985. soap->mode = n;
  5986. }
  5987. #endif
  5988. return SOAP_OK;
  5989. }
  5990. #endif
  5991. /******************************************************************************/
  5992. #ifndef PALM_1
  5993. SOAP_FMAC1
  5994. const char*
  5995. SOAP_FMAC2
  5996. soap_url(struct soap *soap, const char *s, const char *t)
  5997. { if (!t || (*t != '/' && *t != '?') || strlen(s) + strlen(t) >= sizeof(soap->msgbuf))
  5998. return s;
  5999. strcpy(soap->msgbuf, s);
  6000. strcat(soap->msgbuf, t);
  6001. return soap->msgbuf;
  6002. }
  6003. #endif
  6004. /******************************************************************************/
  6005. #ifndef PALM_1
  6006. SOAP_FMAC1
  6007. size_t
  6008. SOAP_FMAC2
  6009. soap_encode_url(const char *s, char *t, size_t len)
  6010. { register int c;
  6011. register size_t n = len;
  6012. while ((c = *s++) && --n > 0)
  6013. { if (c > ' ' && c < 128 && !strchr("()<>@,;:\\\"/[]?={}#!$&'*+", c))
  6014. *t++ = c;
  6015. else if (n > 2)
  6016. { *t++ = '%';
  6017. *t++ = (c >> 4) + (c > 159 ? '7' : '0');
  6018. c &= 0xF;
  6019. *t++ = c + (c > 9 ? '7' : '0');
  6020. n -= 2;
  6021. }
  6022. else
  6023. break;
  6024. }
  6025. *t = '\0';
  6026. return len - n;
  6027. }
  6028. #endif
  6029. /******************************************************************************/
  6030. #ifndef PALM_1
  6031. SOAP_FMAC1
  6032. const char*
  6033. SOAP_FMAC2
  6034. soap_encode_url_string(struct soap *soap, const char *s)
  6035. { if (s)
  6036. { size_t n = 3*strlen(s)+1;
  6037. char *t = (char*)soap_malloc(soap, n);
  6038. if (t)
  6039. { soap_encode_url(s, t, n);
  6040. return t;
  6041. }
  6042. }
  6043. return SOAP_STR_EOS;
  6044. }
  6045. #endif
  6046. /******************************************************************************\
  6047. *
  6048. * HTTP Cookies
  6049. *
  6050. \******************************************************************************/
  6051. #ifdef WITH_COOKIES
  6052. /******************************************************************************/
  6053. SOAP_FMAC1
  6054. struct soap_cookie*
  6055. SOAP_FMAC2
  6056. soap_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  6057. { struct soap_cookie *p;
  6058. if (!domain)
  6059. domain = soap->cookie_domain;
  6060. if (!path)
  6061. path = soap->cookie_path;
  6062. if (!path)
  6063. path = SOAP_STR_EOS;
  6064. else if (*path == '/')
  6065. path++;
  6066. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Search cookie='%s' domain='%s' path='%s'\n", name, domain ? domain : "(null)", path ? path : "(null)"));
  6067. for (p = soap->cookies; p; p = p->next)
  6068. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie in database: %s='%s' domain='%s' path='%s' env=%hd\n", p->name, p->value ? p->value : "(null)", p->domain ? p->domain : "(null)", p->path ? p->path : "(null)", p->env));
  6069. if (!strcmp(p->name, name)
  6070. && p->domain
  6071. && p->path
  6072. && !strcmp(p->domain, domain)
  6073. && (!*p->path || !strncmp(p->path, path, strlen(p->path))))
  6074. break;
  6075. }
  6076. return p;
  6077. }
  6078. /******************************************************************************/
  6079. SOAP_FMAC1
  6080. struct soap_cookie*
  6081. SOAP_FMAC2
  6082. soap_set_cookie(struct soap *soap, const char *name, const char *value, const char *domain, const char *path)
  6083. { struct soap_cookie **p, *q;
  6084. int n;
  6085. if (!domain)
  6086. domain = soap->cookie_domain;
  6087. if (!path)
  6088. path = soap->cookie_path;
  6089. if (!path)
  6090. path = SOAP_STR_EOS;
  6091. else if (*path == '/')
  6092. path++;
  6093. q = soap_cookie(soap, name, domain, path);
  6094. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set %scookie: %s='%s' domain='%s' path='%s'\n", q ? SOAP_STR_EOS : "new ", name, value ? value : "(null)", domain ? domain : "(null)", path ? path : "(null)"));
  6095. if (!q)
  6096. { if ((q = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie))))
  6097. { if ((q->name = (char*)SOAP_MALLOC(soap, strlen(name)+1)))
  6098. strcpy(q->name, name);
  6099. q->value = NULL;
  6100. q->domain = NULL;
  6101. q->path = NULL;
  6102. q->expire = 0;
  6103. q->maxage = -1;
  6104. q->version = 1;
  6105. q->secure = 0;
  6106. q->modified = 0;
  6107. for (p = &soap->cookies, n = soap->cookie_max; *p && n; p = &(*p)->next, n--)
  6108. if (!strcmp((*p)->name, name) && (*p)->path && path && strcmp((*p)->path, path) < 0)
  6109. break;
  6110. if (n)
  6111. { q->next = *p;
  6112. *p = q;
  6113. }
  6114. else
  6115. { SOAP_FREE(soap, q->name);
  6116. SOAP_FREE(soap, q);
  6117. q = NULL;
  6118. }
  6119. }
  6120. }
  6121. else
  6122. q->modified = 1;
  6123. if (q)
  6124. { if (q->value)
  6125. { if (!value || strcmp(value, q->value))
  6126. { SOAP_FREE(soap, q->value);
  6127. q->value = NULL;
  6128. }
  6129. }
  6130. if (value && *value && !q->value && (q->value = (char*)SOAP_MALLOC(soap, strlen(value)+1)))
  6131. strcpy(q->value, value);
  6132. if (q->domain)
  6133. { if (!domain || strcmp(domain, q->domain))
  6134. { SOAP_FREE(soap, q->domain);
  6135. q->domain = NULL;
  6136. }
  6137. }
  6138. if (domain && !q->domain && (q->domain = (char*)SOAP_MALLOC(soap, strlen(domain)+1)))
  6139. strcpy(q->domain, domain);
  6140. if (q->path)
  6141. { if (!path || strncmp(path, q->path, strlen(q->path)))
  6142. { SOAP_FREE(soap, q->path);
  6143. q->path = NULL;
  6144. }
  6145. }
  6146. if (path && !q->path && (q->path = (char*)SOAP_MALLOC(soap, strlen(path)+1)))
  6147. strcpy(q->path, path);
  6148. q->session = 1;
  6149. q->env = 0;
  6150. }
  6151. return q;
  6152. }
  6153. /******************************************************************************/
  6154. SOAP_FMAC1
  6155. void
  6156. SOAP_FMAC2
  6157. soap_clr_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  6158. { struct soap_cookie **p, *q;
  6159. if (!domain)
  6160. domain = soap->cookie_domain;
  6161. if (!domain)
  6162. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in clear cookie='%s': cookie domain not set\n", name ? name : "(null)"));
  6163. return;
  6164. }
  6165. if (!path)
  6166. path = soap->cookie_path;
  6167. if (!path)
  6168. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in clear cookie='%s': cookie path not set\n", name ? name : "(null)"));
  6169. return;
  6170. }
  6171. if (*path == '/')
  6172. path++;
  6173. for (p = &soap->cookies, q = *p; q; q = *p)
  6174. { if (!strcmp(q->name, name) && !strcmp(q->domain, domain) && !strncmp(q->path, path, strlen(q->path)))
  6175. { if (q->value)
  6176. SOAP_FREE(soap, q->value);
  6177. if (q->domain)
  6178. SOAP_FREE(soap, q->domain);
  6179. if (q->path)
  6180. SOAP_FREE(soap, q->path);
  6181. *p = q->next;
  6182. SOAP_FREE(soap, q);
  6183. }
  6184. else
  6185. p = &q->next;
  6186. }
  6187. }
  6188. /******************************************************************************/
  6189. SOAP_FMAC1
  6190. char *
  6191. SOAP_FMAC2
  6192. soap_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  6193. { struct soap_cookie *p;
  6194. if ((p = soap_cookie(soap, name, domain, path)))
  6195. return p->value;
  6196. return NULL;
  6197. }
  6198. /******************************************************************************/
  6199. SOAP_FMAC1
  6200. char *
  6201. SOAP_FMAC2
  6202. soap_env_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  6203. { struct soap_cookie *p;
  6204. if ((p = soap_cookie(soap, name, domain, path)) && p->env)
  6205. return p->value;
  6206. return NULL;
  6207. }
  6208. /******************************************************************************/
  6209. SOAP_FMAC1
  6210. time_t
  6211. SOAP_FMAC2
  6212. soap_cookie_expire(struct soap *soap, const char *name, const char *domain, const char *path)
  6213. { struct soap_cookie *p;
  6214. if ((p = soap_cookie(soap, name, domain, path)))
  6215. return p->expire;
  6216. return -1;
  6217. }
  6218. /******************************************************************************/
  6219. SOAP_FMAC1
  6220. int
  6221. SOAP_FMAC2
  6222. soap_set_cookie_expire(struct soap *soap, const char *name, long expire, const char *domain, const char *path)
  6223. { struct soap_cookie *p;
  6224. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set cookie expiration max-age=%ld: cookie='%s' domain='%s' path='%s'\n", expire, name, domain ? domain : "(null)", path ? path : "(null)"));
  6225. if ((p = soap_cookie(soap, name, domain, path)))
  6226. { p->maxage = expire;
  6227. p->modified = 1;
  6228. return SOAP_OK;
  6229. }
  6230. return SOAP_ERR;
  6231. }
  6232. /******************************************************************************/
  6233. SOAP_FMAC1
  6234. int
  6235. SOAP_FMAC2
  6236. soap_set_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  6237. { struct soap_cookie *p;
  6238. if ((p = soap_cookie(soap, name, domain, path)))
  6239. { p->session = 1;
  6240. p->modified = 1;
  6241. return SOAP_OK;
  6242. }
  6243. return SOAP_ERR;
  6244. }
  6245. /******************************************************************************/
  6246. SOAP_FMAC1
  6247. int
  6248. SOAP_FMAC2
  6249. soap_clr_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  6250. { struct soap_cookie *p;
  6251. if ((p = soap_cookie(soap, name, domain, path)))
  6252. { p->session = 0;
  6253. p->modified = 1;
  6254. return SOAP_OK;
  6255. }
  6256. return SOAP_ERR;
  6257. }
  6258. /******************************************************************************/
  6259. SOAP_FMAC1
  6260. int
  6261. SOAP_FMAC2
  6262. soap_putsetcookies(struct soap *soap)
  6263. { struct soap_cookie *p;
  6264. char *s, tmp[4096];
  6265. const char *t;
  6266. for (p = soap->cookies; p; p = p->next)
  6267. {
  6268. if (p->modified
  6269. #ifdef WITH_OPENSSL
  6270. || (!p->env && !soap->ssl == !p->secure)
  6271. #endif
  6272. )
  6273. { s = tmp;
  6274. if (p->name)
  6275. s += soap_encode_url(p->name, s, tmp-s+4064);
  6276. if (p->value && *p->value)
  6277. { *s++ = '=';
  6278. s += soap_encode_url(p->value, s, tmp-s+4064);
  6279. }
  6280. if (p->domain && (int)strlen(p->domain) < tmp-s+4064)
  6281. { strcpy(s, ";Domain=");
  6282. strcat(s, p->domain);
  6283. }
  6284. else if (soap->cookie_domain && (int)strlen(soap->cookie_domain) < tmp-s+4064)
  6285. { strcpy(s, ";Domain=");
  6286. strcat(s, soap->cookie_domain);
  6287. }
  6288. strcat(s, ";Path=/");
  6289. s += strlen(s);
  6290. if (p->path)
  6291. t = p->path;
  6292. else
  6293. t = soap->cookie_path;
  6294. if (t)
  6295. { if (*t == '/')
  6296. t++;
  6297. if ((int)strlen(t) < tmp-s+4064)
  6298. { if (strchr(t, '%')) /* already URL encoded? */
  6299. { strcpy(s, t);
  6300. s += strlen(s);
  6301. }
  6302. else
  6303. s += soap_encode_url(t, s, tmp-s+4064);
  6304. }
  6305. }
  6306. if (p->version > 0 && s-tmp < 4060)
  6307. {
  6308. #ifdef HAVE_SNPRINTF
  6309. soap_snprintf(s, 4096 - (s-tmp), ";Version=%u", p->version);
  6310. #else
  6311. sprintf(s, ";Version=%u", p->version);
  6312. #endif
  6313. s += strlen(s);
  6314. }
  6315. if (p->maxage >= 0 && s-tmp < 4060)
  6316. {
  6317. #ifdef HAVE_SNPRINTF
  6318. soap_snprintf(s, 4096 - (s-tmp), ";Max-Age=%ld", p->maxage);
  6319. #else
  6320. sprintf(s, ";Max-Age=%ld", p->maxage);
  6321. #endif
  6322. s += strlen(s);
  6323. }
  6324. if (s-tmp < 4073
  6325. && (p->secure
  6326. #ifdef WITH_OPENSSL
  6327. || soap->ssl
  6328. #endif
  6329. ))
  6330. strcpy(s, ";Secure");
  6331. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set-Cookie: %s\n", tmp));
  6332. if ((soap->error = soap->fposthdr(soap, "Set-Cookie", tmp)))
  6333. return soap->error;
  6334. }
  6335. }
  6336. return SOAP_OK;
  6337. }
  6338. /******************************************************************************/
  6339. SOAP_FMAC1
  6340. int
  6341. SOAP_FMAC2
  6342. soap_putcookies(struct soap *soap, const char *domain, const char *path, int secure)
  6343. { struct soap_cookie **p, *q;
  6344. unsigned int version = 0;
  6345. time_t now = time(NULL);
  6346. char *s, tmp[4096];
  6347. if (!domain || !path)
  6348. return SOAP_OK;
  6349. s = tmp;
  6350. p = &soap->cookies;
  6351. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending cookies for domain='%s' path='%s'\n", domain, path));
  6352. if (*path == '/')
  6353. path++;
  6354. while ((q = *p))
  6355. { if (q->expire && now > q->expire)
  6356. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie %s expired\n", q->name));
  6357. SOAP_FREE(soap, q->name);
  6358. if (q->value)
  6359. SOAP_FREE(soap, q->value);
  6360. if (q->domain)
  6361. SOAP_FREE(soap, q->domain);
  6362. if (q->path)
  6363. SOAP_FREE(soap, q->path);
  6364. *p = q->next;
  6365. SOAP_FREE(soap, q);
  6366. }
  6367. else
  6368. { int flag;
  6369. char *t = q->domain;
  6370. size_t n = 0;
  6371. if (!t)
  6372. flag = 1;
  6373. else
  6374. { const char *r = strchr(t, ':');
  6375. if (r)
  6376. n = r - t;
  6377. else
  6378. n = strlen(t);
  6379. flag = !strncmp(t, domain, n);
  6380. }
  6381. /* domain-level cookies, cannot compile when WITH_NOIO set */
  6382. #ifndef WITH_NOIO
  6383. if (!flag)
  6384. { struct hostent *hostent = gethostbyname((char*)domain);
  6385. if (hostent)
  6386. { const char *r = strchr(hostent->h_name, '.');
  6387. if (!r)
  6388. r = hostent->h_name;
  6389. flag = !strncmp(t, r, n);
  6390. }
  6391. }
  6392. #endif
  6393. if (flag
  6394. && (!q->path || !strncmp(q->path, path, strlen(q->path)))
  6395. && (!q->secure || secure))
  6396. { size_t n = 12;
  6397. if (q->name)
  6398. n += 3*strlen(q->name);
  6399. if (q->value && *q->value)
  6400. n += 3*strlen(q->value) + 1;
  6401. if (q->path && *q->path)
  6402. n += strlen(q->path) + 9;
  6403. if (q->domain)
  6404. n += strlen(q->domain) + 11;
  6405. if (tmp - s + n > sizeof(tmp))
  6406. { if (s == tmp)
  6407. return SOAP_OK; /* HTTP header size overflow */
  6408. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie: %s\n", tmp));
  6409. if ((soap->error = soap->fposthdr(soap, "Cookie", tmp)))
  6410. return soap->error;
  6411. s = tmp;
  6412. }
  6413. else if (s != tmp)
  6414. { strcat(s, " ");
  6415. s++;
  6416. }
  6417. if (q->version != version && s-tmp < 4060)
  6418. {
  6419. #ifdef HAVE_SNPRINTF
  6420. soap_snprintf(s, 4096 - (s-tmp), "$Version=%u;", q->version);
  6421. #else
  6422. sprintf(s, "$Version=%u;", q->version);
  6423. #endif
  6424. version = q->version;
  6425. s += strlen(s);
  6426. }
  6427. if (q->name)
  6428. s += soap_encode_url(q->name, s, tmp+sizeof(tmp)-s-16);
  6429. if (q->value && *q->value)
  6430. { *s++ = '=';
  6431. s += soap_encode_url(q->value, s, tmp+sizeof(tmp)-s-16);
  6432. }
  6433. if (q->path && (s-tmp) + strlen(q->path) < 4060)
  6434. {
  6435. #ifdef HAVE_SNPRINTF
  6436. soap_snprintf(s, 4096 - (s-tmp), ";$Path=\"/%s\"", (*q->path == '/' ? q->path + 1 : q->path));
  6437. #else
  6438. sprintf(s, ";$Path=\"/%s\"", (*q->path == '/' ? q->path + 1 : q->path));
  6439. #endif
  6440. s += strlen(s);
  6441. }
  6442. if (q->domain && (s-tmp) + strlen(q->domain) < 4060)
  6443. {
  6444. #ifdef HAVE_SNPRINTF
  6445. soap_snprintf(s, 4096 - (s-tmp), ";$Domain=\"%s\"", q->domain);
  6446. #else
  6447. sprintf(s, ";$Domain=\"%s\"", q->domain);
  6448. #endif
  6449. s += strlen(s);
  6450. }
  6451. }
  6452. p = &q->next;
  6453. }
  6454. }
  6455. if (s != tmp)
  6456. if ((soap->error = soap->fposthdr(soap, "Cookie", tmp)))
  6457. return soap->error;
  6458. return SOAP_OK;
  6459. }
  6460. /******************************************************************************/
  6461. SOAP_FMAC1
  6462. void
  6463. SOAP_FMAC2
  6464. soap_getcookies(struct soap *soap, const char *val)
  6465. { struct soap_cookie *p = NULL, *q;
  6466. const char *s;
  6467. char *t, tmp[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  6468. char *domain = NULL;
  6469. char *path = NULL;
  6470. unsigned int version = 0;
  6471. time_t now = time(NULL);
  6472. if (!val)
  6473. return;
  6474. s = val;
  6475. while (*s)
  6476. { s = soap_decode_key(tmp, sizeof(tmp), s);
  6477. if (!soap_tag_cmp(tmp, "$Version"))
  6478. { if ((s = soap_decode_val(tmp, sizeof(tmp), s)))
  6479. { if (p)
  6480. p->version = (int)soap_strtol(tmp, NULL, 10);
  6481. else
  6482. version = (int)soap_strtol(tmp, NULL, 10);
  6483. }
  6484. }
  6485. else if (!soap_tag_cmp(tmp, "$Path"))
  6486. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6487. if (*tmp)
  6488. { if ((t = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6489. strcpy(t, tmp);
  6490. }
  6491. else
  6492. t = NULL;
  6493. if (p)
  6494. { if (p->path)
  6495. SOAP_FREE(soap, p->path);
  6496. p->path = t;
  6497. }
  6498. else
  6499. { if (path)
  6500. SOAP_FREE(soap, path);
  6501. path = t;
  6502. }
  6503. }
  6504. else if (!soap_tag_cmp(tmp, "$Domain"))
  6505. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6506. if (*tmp)
  6507. { if ((t = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6508. strcpy(t, tmp);
  6509. }
  6510. else
  6511. t = NULL;
  6512. if (p)
  6513. { if (p->domain)
  6514. SOAP_FREE(soap, p->domain);
  6515. p->domain = t;
  6516. }
  6517. else
  6518. { if (domain)
  6519. SOAP_FREE(soap, domain);
  6520. domain = t;
  6521. }
  6522. }
  6523. else if (p && !soap_tag_cmp(tmp, "Path"))
  6524. { if (p->path)
  6525. SOAP_FREE(soap, p->path);
  6526. s = soap_decode_val(tmp, sizeof(tmp), s);
  6527. if (*tmp)
  6528. { if ((p->path = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6529. strcpy(p->path, tmp);
  6530. }
  6531. else
  6532. p->path = NULL;
  6533. }
  6534. else if (p && !soap_tag_cmp(tmp, "Domain"))
  6535. { if (p->domain)
  6536. SOAP_FREE(soap, p->domain);
  6537. s = soap_decode_val(tmp, sizeof(tmp), s);
  6538. if (*tmp)
  6539. { if ((p->domain = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6540. strcpy(p->domain, tmp);
  6541. }
  6542. else
  6543. p->domain = NULL;
  6544. }
  6545. else if (p && !soap_tag_cmp(tmp, "Version"))
  6546. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6547. p->version = (unsigned int)soap_strtoul(tmp, NULL, 10);
  6548. }
  6549. else if (p && !soap_tag_cmp(tmp, "Max-Age"))
  6550. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6551. p->expire = now + soap_strtol(tmp, NULL, 10);
  6552. }
  6553. else if (p && !soap_tag_cmp(tmp, "Expires"))
  6554. { struct tm T;
  6555. char a[3];
  6556. static const char mns[] = "anebarprayunulugepctovec";
  6557. s = soap_decode_val(tmp, sizeof(tmp), s);
  6558. if (strlen(tmp) > 20)
  6559. { memset((void*)&T, 0, sizeof(T));
  6560. a[0] = tmp[4];
  6561. a[1] = tmp[5];
  6562. a[2] = '\0';
  6563. T.tm_mday = (int)soap_strtol(a, NULL, 10);
  6564. a[0] = tmp[8];
  6565. a[1] = tmp[9];
  6566. T.tm_mon = (int)(strstr(mns, a) - mns) / 2;
  6567. a[0] = tmp[11];
  6568. a[1] = tmp[12];
  6569. T.tm_year = 100 + (int)soap_strtol(a, NULL, 10);
  6570. a[0] = tmp[13];
  6571. a[1] = tmp[14];
  6572. T.tm_hour = (int)soap_strtol(a, NULL, 10);
  6573. a[0] = tmp[16];
  6574. a[1] = tmp[17];
  6575. T.tm_min = (int)soap_strtol(a, NULL, 10);
  6576. a[0] = tmp[19];
  6577. a[1] = tmp[20];
  6578. T.tm_sec = (int)soap_strtol(a, NULL, 10);
  6579. p->expire = soap_timegm(&T);
  6580. }
  6581. }
  6582. else if (p && !soap_tag_cmp(tmp, "Secure"))
  6583. p->secure = 1;
  6584. else
  6585. { if (p)
  6586. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Got environment cookie='%s' value='%s' domain='%s' path='%s' expire=%ld secure=%d\n", p->name, p->value ? p->value : "(null)", p->domain ? p->domain : "(null)", p->path ? p->path : "(null)", p->expire, p->secure));
  6587. if ((q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path)))
  6588. { q->version = p->version;
  6589. q->expire = p->expire;
  6590. q->secure = p->secure;
  6591. q->env = 1;
  6592. }
  6593. if (p->name)
  6594. SOAP_FREE(soap, p->name);
  6595. if (p->value)
  6596. SOAP_FREE(soap, p->value);
  6597. if (p->domain)
  6598. SOAP_FREE(soap, p->domain);
  6599. if (p->path)
  6600. SOAP_FREE(soap, p->path);
  6601. SOAP_FREE(soap, p);
  6602. }
  6603. if ((p = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie))))
  6604. { p->name = (char*)SOAP_MALLOC(soap, strlen(tmp)+1);
  6605. strcpy(p->name, tmp);
  6606. s = soap_decode_val(tmp, sizeof(tmp), s);
  6607. if (*tmp)
  6608. { p->value = (char*)SOAP_MALLOC(soap, strlen(tmp)+1);
  6609. strcpy(p->value, tmp);
  6610. }
  6611. else
  6612. p->value = NULL;
  6613. if (domain)
  6614. p->domain = domain;
  6615. else if (*soap->host)
  6616. { p->domain = (char*)SOAP_MALLOC(soap, strlen(soap->host)+1);
  6617. strcpy(p->domain, soap->host);
  6618. }
  6619. else
  6620. p->domain = NULL;
  6621. if (path)
  6622. p->path = path;
  6623. else if (soap->path && *soap->path)
  6624. { p->path = (char*)SOAP_MALLOC(soap, strlen(soap->path)+1);
  6625. strcpy(p->path, soap->path);
  6626. }
  6627. else
  6628. { p->path = (char*)SOAP_MALLOC(soap, 2);
  6629. strcpy(p->path, "/");
  6630. }
  6631. p->expire = 0;
  6632. p->secure = 0;
  6633. p->version = version;
  6634. }
  6635. }
  6636. }
  6637. if (p)
  6638. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Got environment cookie='%s' value='%s' domain='%s' path='%s' expire=%ld secure=%d\n", p->name, p->value ? p->value : "(null)", p->domain ? p->domain : "(null)", p->path ? p->path : "(null)", p->expire, p->secure));
  6639. if ((q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path)))
  6640. { q->version = p->version;
  6641. q->expire = p->expire;
  6642. q->secure = p->secure;
  6643. q->env = 1;
  6644. }
  6645. if (p->name)
  6646. SOAP_FREE(soap, p->name);
  6647. if (p->value)
  6648. SOAP_FREE(soap, p->value);
  6649. if (p->domain)
  6650. SOAP_FREE(soap, p->domain);
  6651. if (p->path)
  6652. SOAP_FREE(soap, p->path);
  6653. SOAP_FREE(soap, p);
  6654. }
  6655. if (domain)
  6656. SOAP_FREE(soap, domain);
  6657. if (path)
  6658. SOAP_FREE(soap, path);
  6659. }
  6660. /******************************************************************************/
  6661. SOAP_FMAC1
  6662. int
  6663. SOAP_FMAC2
  6664. soap_getenv_cookies(struct soap *soap)
  6665. { struct soap_cookie *p;
  6666. const char *s;
  6667. char key[4096], val[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  6668. if (!(s = getenv("HTTP_COOKIE")))
  6669. return SOAP_ERR;
  6670. do
  6671. { s = soap_decode_key(key, sizeof(key), s);
  6672. s = soap_decode_val(val, sizeof(val), s);
  6673. p = soap_set_cookie(soap, key, val, NULL, NULL);
  6674. if (p)
  6675. p->env = 1;
  6676. } while (*s);
  6677. return SOAP_OK;
  6678. }
  6679. /******************************************************************************/
  6680. SOAP_FMAC1
  6681. struct soap_cookie*
  6682. SOAP_FMAC2
  6683. soap_copy_cookies(struct soap *copy, const struct soap *soap)
  6684. { struct soap_cookie *p, **q, *r;
  6685. q = &r;
  6686. for (p = soap->cookies; p; p = p->next)
  6687. { if (!(*q = (struct soap_cookie*)SOAP_MALLOC(copy, sizeof(struct soap_cookie))))
  6688. return r;
  6689. **q = *p;
  6690. if (p->name)
  6691. { if (((*q)->name = (char*)SOAP_MALLOC(copy, strlen(p->name)+1)))
  6692. strcpy((*q)->name, p->name);
  6693. }
  6694. if (p->value)
  6695. { if (((*q)->value = (char*)SOAP_MALLOC(copy, strlen(p->value)+1)))
  6696. strcpy((*q)->value, p->value);
  6697. }
  6698. if (p->domain)
  6699. { if (((*q)->domain = (char*)SOAP_MALLOC(copy, strlen(p->domain)+1)))
  6700. strcpy((*q)->domain, p->domain);
  6701. }
  6702. if (p->path)
  6703. { if (((*q)->path = (char*)SOAP_MALLOC(copy, strlen(p->path)+1)))
  6704. strcpy((*q)->path, p->path);
  6705. }
  6706. q = &(*q)->next;
  6707. }
  6708. *q = NULL;
  6709. return r;
  6710. }
  6711. /******************************************************************************/
  6712. SOAP_FMAC1
  6713. void
  6714. SOAP_FMAC2
  6715. soap_free_cookies(struct soap *soap)
  6716. { struct soap_cookie *p;
  6717. for (p = soap->cookies; p; p = soap->cookies)
  6718. { soap->cookies = p->next;
  6719. SOAP_FREE(soap, p->name);
  6720. if (p->value)
  6721. SOAP_FREE(soap, p->value);
  6722. if (p->domain)
  6723. SOAP_FREE(soap, p->domain);
  6724. if (p->path)
  6725. SOAP_FREE(soap, p->path);
  6726. SOAP_FREE(soap, p);
  6727. }
  6728. }
  6729. /******************************************************************************/
  6730. #endif /* WITH_COOKIES */
  6731. /******************************************************************************/
  6732. #ifndef WITH_NOIDREF
  6733. #ifndef PALM_2
  6734. SOAP_FMAC1
  6735. size_t
  6736. SOAP_FMAC2
  6737. soap_hash(register const char *s)
  6738. { register size_t h = 0;
  6739. while (*s)
  6740. h = 65599*h + *s++;
  6741. return h % SOAP_IDHASH;
  6742. }
  6743. #endif
  6744. #endif
  6745. /******************************************************************************/
  6746. #ifndef WITH_NOIDREF
  6747. #ifndef PALM_1
  6748. static void
  6749. soap_init_pht(struct soap *soap)
  6750. { register int i;
  6751. soap->pblk = NULL;
  6752. soap->pidx = 0;
  6753. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  6754. soap->pht[i] = NULL;
  6755. }
  6756. #endif
  6757. #endif
  6758. /******************************************************************************/
  6759. #ifndef PALM_1
  6760. SOAP_FMAC1
  6761. struct soap*
  6762. SOAP_FMAC2
  6763. soap_versioning(soap_new)(soap_mode imode, soap_mode omode)
  6764. { struct soap *soap = (struct soap*)malloc(sizeof(struct soap));
  6765. if (soap)
  6766. soap_versioning(soap_init)(soap, imode, omode);
  6767. return soap;
  6768. }
  6769. #endif
  6770. /******************************************************************************/
  6771. #ifndef PALM_1
  6772. SOAP_FMAC1
  6773. void
  6774. SOAP_FMAC2
  6775. soap_free(struct soap *soap)
  6776. { soap_done(soap);
  6777. free(soap);
  6778. }
  6779. #endif
  6780. /******************************************************************************/
  6781. #ifndef PALM_1
  6782. SOAP_FMAC1
  6783. void
  6784. SOAP_FMAC2
  6785. soap_del(struct soap *soap)
  6786. { free(soap);
  6787. }
  6788. #endif
  6789. /******************************************************************************/
  6790. #ifndef WITH_NOIDREF
  6791. #ifndef PALM_1
  6792. static void
  6793. soap_free_pht(struct soap *soap)
  6794. { register struct soap_pblk *pb, *next;
  6795. register int i;
  6796. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free pointer hashtable\n"));
  6797. for (pb = soap->pblk; pb; pb = next)
  6798. { next = pb->next;
  6799. SOAP_FREE(soap, pb);
  6800. }
  6801. soap->pblk = NULL;
  6802. soap->pidx = 0;
  6803. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  6804. soap->pht[i] = NULL;
  6805. }
  6806. #endif
  6807. #endif
  6808. /******************************************************************************/
  6809. #ifndef WITH_NOIDREF
  6810. #ifndef PALM_2
  6811. SOAP_FMAC1
  6812. int
  6813. SOAP_FMAC2
  6814. soap_embed(struct soap *soap, const void *p, const struct soap_array *a, int n, const char *tag, int type)
  6815. { register int i;
  6816. struct soap_plist *pp;
  6817. (void)soap;
  6818. if (soap->version == 2)
  6819. soap->encoding = 1;
  6820. if (a)
  6821. i = soap_array_pointer_lookup(soap, p, a, n, type, &pp);
  6822. else
  6823. i = soap_pointer_lookup(soap, p, type, &pp);
  6824. if (i)
  6825. { if (soap_is_embedded(soap, pp)
  6826. || soap_is_single(soap, pp))
  6827. return 0;
  6828. soap_set_embedded(soap, pp);
  6829. }
  6830. return i;
  6831. }
  6832. #endif
  6833. #endif
  6834. /******************************************************************************/
  6835. #ifndef WITH_NOIDREF
  6836. #ifndef PALM_2
  6837. SOAP_FMAC1
  6838. int
  6839. SOAP_FMAC2
  6840. soap_pointer_lookup(struct soap *soap, const void *p, int type, struct soap_plist **ppp)
  6841. { register struct soap_plist *pp;
  6842. *ppp = NULL;
  6843. if (p)
  6844. { for (pp = soap->pht[soap_hash_ptr(p)]; pp; pp = pp->next)
  6845. { if (pp->ptr == p && pp->type == type)
  6846. { *ppp = pp;
  6847. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d id=%d\n", p, type, pp->id));
  6848. return pp->id;
  6849. }
  6850. }
  6851. }
  6852. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d: not found\n", p, type));
  6853. return 0;
  6854. }
  6855. #endif
  6856. #endif
  6857. /******************************************************************************/
  6858. #ifndef WITH_NOIDREF
  6859. #ifndef PALM_2
  6860. SOAP_FMAC1
  6861. int
  6862. SOAP_FMAC2
  6863. soap_pointer_enter(struct soap *soap, const void *p, const struct soap_array *a, int n, int type, struct soap_plist **ppp)
  6864. { register size_t h;
  6865. register struct soap_plist *pp;
  6866. (void)n;
  6867. if (!soap->pblk || soap->pidx >= SOAP_PTRBLK)
  6868. { register struct soap_pblk *pb = (struct soap_pblk*)SOAP_MALLOC(soap, sizeof(struct soap_pblk));
  6869. if (!pb)
  6870. { soap->error = SOAP_EOM;
  6871. return 0;
  6872. }
  6873. pb->next = soap->pblk;
  6874. soap->pblk = pb;
  6875. soap->pidx = 0;
  6876. }
  6877. *ppp = pp = &soap->pblk->plist[soap->pidx++];
  6878. if (a)
  6879. h = soap_hash_ptr(a->__ptr);
  6880. else
  6881. h = soap_hash_ptr(p);
  6882. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Pointer enter location=%p array=%p size=%d dim=%d type=%d id=%d\n", p, a ? a->__ptr : NULL, a ? a->__size : 0, n, type, soap->idnum+1));
  6883. pp->next = soap->pht[h];
  6884. pp->type = type;
  6885. pp->mark1 = 0;
  6886. pp->mark2 = 0;
  6887. pp->ptr = p;
  6888. pp->array = a;
  6889. soap->pht[h] = pp;
  6890. pp->id = ++soap->idnum;
  6891. return pp->id;
  6892. }
  6893. #endif
  6894. #endif
  6895. /******************************************************************************/
  6896. #ifndef WITH_NOIDREF
  6897. #ifndef PALM_2
  6898. SOAP_FMAC1
  6899. int
  6900. SOAP_FMAC2
  6901. soap_array_pointer_lookup(struct soap *soap, const void *p, const struct soap_array *a, int n, int type, struct soap_plist **ppp)
  6902. { register struct soap_plist *pp;
  6903. *ppp = NULL;
  6904. if (!p || !a->__ptr)
  6905. return 0;
  6906. for (pp = soap->pht[soap_hash_ptr(a->__ptr)]; pp; pp = pp->next)
  6907. { if (pp->type == type && pp->array && pp->array->__ptr == a->__ptr)
  6908. { register int i;
  6909. for (i = 0; i < n; i++)
  6910. if (((const int*)&pp->array->__size)[i] != ((const int*)&a->__size)[i])
  6911. break;
  6912. if (i == n)
  6913. { *ppp = pp;
  6914. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d id=%d\n", a->__ptr, type, pp->id));
  6915. return pp->id;
  6916. }
  6917. }
  6918. }
  6919. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d: not found\n", a->__ptr, type));
  6920. return 0;
  6921. }
  6922. #endif
  6923. #endif
  6924. /******************************************************************************/
  6925. #ifndef PALM_1
  6926. SOAP_FMAC1
  6927. int
  6928. SOAP_FMAC2
  6929. soap_begin_count(struct soap *soap)
  6930. { soap_free_ns(soap);
  6931. #ifndef WITH_LEANER
  6932. if ((soap->mode & SOAP_ENC_DIME) || (soap->omode & SOAP_ENC_DIME))
  6933. soap->mode = soap->omode | SOAP_IO_LENGTH | SOAP_ENC_DIME;
  6934. else
  6935. #endif
  6936. { soap->mode = soap->omode;
  6937. if ((soap->mode & SOAP_IO_UDP))
  6938. soap->mode |= SOAP_ENC_XML;
  6939. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE
  6940. || (((soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_ENC_XML))
  6941. #ifndef WITH_LEANER
  6942. && !soap->fpreparesend
  6943. #endif
  6944. ))
  6945. soap->mode &= ~SOAP_IO_LENGTH;
  6946. else
  6947. soap->mode |= SOAP_IO_LENGTH;
  6948. }
  6949. #ifdef WITH_ZLIB
  6950. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  6951. { if (!(soap->mode & SOAP_ENC_DIME))
  6952. soap->mode &= ~SOAP_IO_LENGTH;
  6953. if (soap->mode & SOAP_ENC_XML)
  6954. soap->mode |= SOAP_IO_BUFFER;
  6955. else
  6956. soap->mode |= SOAP_IO_STORE;
  6957. }
  6958. #endif
  6959. #ifndef WITH_LEANER
  6960. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME))
  6961. soap->mode |= SOAP_ENC_MIME;
  6962. else if (!(soap->mode & SOAP_ENC_MIME))
  6963. soap->mode &= ~SOAP_ENC_MTOM;
  6964. if (soap->mode & SOAP_ENC_MIME)
  6965. soap_select_mime_boundary(soap);
  6966. soap->dime.list = soap->dime.last; /* keep track of last DIME attachment */
  6967. #endif
  6968. soap->count = 0;
  6969. soap->ns = 0;
  6970. soap->null = 0;
  6971. soap->position = 0;
  6972. soap->mustUnderstand = 0;
  6973. soap->encoding = 0;
  6974. soap->part = SOAP_BEGIN;
  6975. soap->event = 0;
  6976. soap->evlev = 0;
  6977. soap->idnum = 0;
  6978. soap_clr_attr(soap);
  6979. soap_set_local_namespaces(soap);
  6980. #ifndef WITH_LEANER
  6981. soap->dime.count = 0; /* count # of attachments */
  6982. soap->dime.size = 0; /* accumulate total size of attachments */
  6983. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) != SOAP_IO_STORE && (soap->error = soap->fprepareinitsend(soap)))
  6984. return soap->error;
  6985. #endif
  6986. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin count phase (socket=%d mode=0x%x count=%lu)\n", soap->socket, (unsigned int)soap->mode, (unsigned long)soap->count));
  6987. return SOAP_OK;
  6988. }
  6989. #endif
  6990. /******************************************************************************/
  6991. #ifndef PALM_1
  6992. SOAP_FMAC1
  6993. int
  6994. SOAP_FMAC2
  6995. soap_end_count(struct soap *soap)
  6996. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of count phase\n"));
  6997. #ifndef WITH_LEANER
  6998. if ((soap->mode & SOAP_IO_LENGTH))
  6999. { if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)))
  7000. return soap->error;
  7001. }
  7002. #endif
  7003. return SOAP_OK;
  7004. }
  7005. #endif
  7006. /******************************************************************************/
  7007. #ifndef PALM_1
  7008. SOAP_FMAC1
  7009. int
  7010. SOAP_FMAC2
  7011. soap_begin_send(struct soap *soap)
  7012. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Initializing for output to socket=%d/fd=%d\n", soap->socket, soap->sendfd));
  7013. soap_free_ns(soap);
  7014. soap->error = SOAP_OK;
  7015. soap->mode = soap->omode | (soap->mode & (SOAP_IO_LENGTH | SOAP_ENC_DIME));
  7016. #ifdef WITH_ZLIB
  7017. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  7018. { if (soap->mode & SOAP_ENC_XML)
  7019. soap->mode |= SOAP_IO_BUFFER;
  7020. else
  7021. soap->mode |= SOAP_IO_STORE;
  7022. }
  7023. #endif
  7024. #ifndef WITH_LEAN
  7025. if ((soap->mode & SOAP_IO_UDP))
  7026. { soap->mode |= SOAP_ENC_XML;
  7027. if (soap->count > SOAP_BUFLEN)
  7028. return soap->error = SOAP_UDP_ERROR;
  7029. }
  7030. #endif
  7031. if ((soap->mode & SOAP_IO) == SOAP_IO_FLUSH && soap_valid_socket(soap->socket))
  7032. { if (soap->count || (soap->mode & SOAP_IO_LENGTH) || (soap->mode & SOAP_ENC_XML))
  7033. soap->mode |= SOAP_IO_BUFFER;
  7034. else
  7035. soap->mode |= SOAP_IO_STORE;
  7036. }
  7037. soap->mode &= ~SOAP_IO_LENGTH;
  7038. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE)
  7039. if (soap_new_block(soap) == NULL)
  7040. return soap->error;
  7041. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  7042. soap->keep_alive = 0;
  7043. #ifndef WITH_LEANER
  7044. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME))
  7045. { soap->mode |= SOAP_ENC_MIME;
  7046. soap->mode &= ~SOAP_ENC_DIME;
  7047. }
  7048. else if (!(soap->mode & SOAP_ENC_MIME))
  7049. soap->mode &= ~SOAP_ENC_MTOM;
  7050. if (soap->mode & SOAP_ENC_MIME)
  7051. soap_select_mime_boundary(soap);
  7052. #ifdef WIN32
  7053. #ifndef UNDER_CE
  7054. #ifndef WITH_FASTCGI
  7055. if (!soap_valid_socket(soap->socket) && !soap->os) /* Set win32 stdout or soap->sendfd to BINARY, e.g. to support DIME */
  7056. #ifdef __BORLANDC__
  7057. setmode(soap->sendfd, _O_BINARY);
  7058. #else
  7059. _setmode(soap->sendfd, _O_BINARY);
  7060. #endif
  7061. #endif
  7062. #endif
  7063. #endif
  7064. #endif
  7065. if (soap->mode & SOAP_IO)
  7066. { soap->bufidx = 0;
  7067. soap->buflen = 0;
  7068. }
  7069. soap->chunksize = 0;
  7070. soap->ns = 0;
  7071. soap->null = 0;
  7072. soap->position = 0;
  7073. soap->mustUnderstand = 0;
  7074. soap->encoding = 0;
  7075. soap->idnum = 0;
  7076. soap->level = 0;
  7077. soap_clr_attr(soap);
  7078. soap_set_local_namespaces(soap);
  7079. #ifdef WITH_ZLIB
  7080. soap->z_ratio_out = 1.0;
  7081. if ((soap->mode & SOAP_ENC_ZLIB) && soap->zlib_state != SOAP_ZLIB_DEFLATE)
  7082. { if (!soap->z_buf)
  7083. soap->z_buf = (char*)SOAP_MALLOC(soap, SOAP_BUFLEN);
  7084. soap->d_stream->next_out = (Byte*)soap->z_buf;
  7085. soap->d_stream->avail_out = SOAP_BUFLEN;
  7086. #ifdef WITH_GZIP
  7087. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  7088. { memcpy(soap->z_buf, "\37\213\10\0\0\0\0\0\0\377", 10);
  7089. soap->d_stream->next_out = (Byte*)soap->z_buf + 10;
  7090. soap->d_stream->avail_out = SOAP_BUFLEN - 10;
  7091. soap->z_crc = crc32(0L, NULL, 0);
  7092. soap->zlib_out = SOAP_ZLIB_GZIP;
  7093. if (soap->z_dict)
  7094. *((Byte*)soap->z_buf + 2) = 0xff;
  7095. if (deflateInit2(soap->d_stream, soap->z_level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)
  7096. return soap->error = SOAP_ZLIB_ERROR;
  7097. }
  7098. else
  7099. #endif
  7100. if (deflateInit(soap->d_stream, soap->z_level) != Z_OK)
  7101. return soap->error = SOAP_ZLIB_ERROR;
  7102. if (soap->z_dict)
  7103. { if (deflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  7104. return soap->error = SOAP_ZLIB_ERROR;
  7105. }
  7106. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflate initialized\n"));
  7107. soap->zlib_state = SOAP_ZLIB_DEFLATE;
  7108. }
  7109. #endif
  7110. #ifdef WITH_OPENSSL
  7111. if (soap->ssl)
  7112. ERR_clear_error();
  7113. #endif
  7114. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin send phase (socket=%d mode=0x%x count=%lu)\n", soap->socket, soap->mode, (unsigned long)soap->count));
  7115. soap->part = SOAP_BEGIN;
  7116. #ifndef WITH_LEANER
  7117. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) == SOAP_IO_STORE && (soap->error = soap->fprepareinitsend(soap)))
  7118. return soap->error;
  7119. #endif
  7120. return SOAP_OK;
  7121. }
  7122. #endif
  7123. /******************************************************************************/
  7124. #ifndef WITH_NOIDREF
  7125. #ifndef PALM_2
  7126. SOAP_FMAC1
  7127. void
  7128. SOAP_FMAC2
  7129. soap_embedded(struct soap *soap, const void *p, int t)
  7130. { struct soap_plist *pp;
  7131. if (soap_pointer_lookup(soap, p, t, &pp))
  7132. { pp->mark1 = 1;
  7133. pp->mark2 = 1;
  7134. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded %p type=%d mark set to 1\n", p, t));
  7135. }
  7136. }
  7137. #endif
  7138. #endif
  7139. /******************************************************************************/
  7140. #ifndef WITH_NOIDREF
  7141. #ifndef PALM_2
  7142. SOAP_FMAC1
  7143. int
  7144. SOAP_FMAC2
  7145. soap_reference(struct soap *soap, const void *p, int t)
  7146. { struct soap_plist *pp;
  7147. if (!p || (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME|SOAP_ENC_MIME|SOAP_ENC_MTOM|SOAP_XML_GRAPH))) || (soap->omode & SOAP_XML_TREE))
  7148. return 1;
  7149. if (soap_pointer_lookup(soap, p, t, &pp))
  7150. { if (pp->mark1 == 0)
  7151. { pp->mark1 = 2;
  7152. pp->mark2 = 2;
  7153. }
  7154. }
  7155. else if (!soap_pointer_enter(soap, p, NULL, 0, t, &pp))
  7156. return 1;
  7157. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reference %p type=%d (%d %d)\n", p, t, (int)pp->mark1, (int)pp->mark2));
  7158. return pp->mark1;
  7159. }
  7160. #endif
  7161. #endif
  7162. /******************************************************************************/
  7163. #ifndef WITH_NOIDREF
  7164. #ifndef PALM_2
  7165. SOAP_FMAC1
  7166. int
  7167. SOAP_FMAC2
  7168. soap_array_reference(struct soap *soap, const void *p, const struct soap_array *a, int n, int t)
  7169. { struct soap_plist *pp;
  7170. if (!p || !a->__ptr || (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME|SOAP_ENC_MIME|SOAP_ENC_MTOM|SOAP_XML_GRAPH))) || (soap->omode & SOAP_XML_TREE))
  7171. return 1;
  7172. if (soap_array_pointer_lookup(soap, p, a, n, t, &pp))
  7173. { if (pp->mark1 == 0)
  7174. { pp->mark1 = 2;
  7175. pp->mark2 = 2;
  7176. }
  7177. }
  7178. else if (!soap_pointer_enter(soap, p, a, n, t, &pp))
  7179. return 1;
  7180. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array reference %p ptr=%p dim=%d type=%d (%d %d)\n", p, a->__ptr, n, t, (int)pp->mark1, (int)pp->mark2));
  7181. return pp->mark1;
  7182. }
  7183. #endif
  7184. #endif
  7185. /******************************************************************************/
  7186. #ifndef WITH_NOIDREF
  7187. #ifndef PALM_2
  7188. SOAP_FMAC1
  7189. int
  7190. SOAP_FMAC2
  7191. soap_embedded_id(struct soap *soap, int id, const void *p, int t)
  7192. { struct soap_plist *pp = NULL;
  7193. if (!id || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  7194. return id;
  7195. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id %p type=%d id=%d\n", p, t, id));
  7196. if (soap->version == 1 && soap->part != SOAP_IN_HEADER)
  7197. { if (id < 0)
  7198. { id = soap_pointer_lookup(soap, p, t, &pp);
  7199. if (id)
  7200. { if (soap->mode & SOAP_IO_LENGTH)
  7201. pp->mark1 = 2;
  7202. else
  7203. pp->mark2 = 2;
  7204. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id multiref id=%d %p type=%d = (%d %d)\n", id, p, t, (int)pp->mark1, (int)pp->mark2));
  7205. }
  7206. return -1;
  7207. }
  7208. return id;
  7209. }
  7210. if (id < 0)
  7211. id = soap_pointer_lookup(soap, p, t, &pp);
  7212. else if (id && !soap_pointer_lookup(soap, p, t, &pp))
  7213. return 0;
  7214. if (id && pp)
  7215. { if (soap->mode & SOAP_IO_LENGTH)
  7216. pp->mark1 = 1;
  7217. else
  7218. pp->mark2 = 1;
  7219. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id embedded ref id=%d %p type=%d = (%d %d)\n", id, p, t, (int)pp->mark1, (int)pp->mark2));
  7220. }
  7221. return id;
  7222. }
  7223. #endif
  7224. #endif
  7225. /******************************************************************************/
  7226. #ifndef WITH_NOIDREF
  7227. #ifndef PALM_2
  7228. SOAP_FMAC1
  7229. int
  7230. SOAP_FMAC2
  7231. soap_is_embedded(struct soap *soap, struct soap_plist *pp)
  7232. { if (!pp)
  7233. return 0;
  7234. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Is embedded? %d %d\n", (int)pp->mark1, (int)pp->mark2));
  7235. if (soap->version == 1 && soap->encodingStyle && !(soap->mode & SOAP_XML_GRAPH) && soap->part != SOAP_IN_HEADER)
  7236. { if (soap->mode & SOAP_IO_LENGTH)
  7237. return pp->mark1 != 0;
  7238. return pp->mark2 != 0;
  7239. }
  7240. if (soap->mode & SOAP_IO_LENGTH)
  7241. return pp->mark1 == 1;
  7242. return pp->mark2 == 1;
  7243. }
  7244. #endif
  7245. #endif
  7246. /******************************************************************************/
  7247. #ifndef WITH_NOIDREF
  7248. #ifndef PALM_2
  7249. SOAP_FMAC1
  7250. int
  7251. SOAP_FMAC2
  7252. soap_is_single(struct soap *soap, struct soap_plist *pp)
  7253. { if (soap->part == SOAP_IN_HEADER)
  7254. return 1;
  7255. if (!pp)
  7256. return 0;
  7257. if (soap->mode & SOAP_IO_LENGTH)
  7258. return pp->mark1 == 0;
  7259. return pp->mark2 == 0;
  7260. }
  7261. #endif
  7262. #endif
  7263. /******************************************************************************/
  7264. #ifndef WITH_NOIDREF
  7265. #ifndef PALM_2
  7266. SOAP_FMAC1
  7267. void
  7268. SOAP_FMAC2
  7269. soap_set_embedded(struct soap *soap, struct soap_plist *pp)
  7270. { if (!pp)
  7271. return;
  7272. if (soap->mode & SOAP_IO_LENGTH)
  7273. pp->mark1 = 1;
  7274. else
  7275. pp->mark2 = 1;
  7276. }
  7277. #endif
  7278. #endif
  7279. /******************************************************************************/
  7280. #ifndef WITH_LEANER
  7281. #ifndef PALM_1
  7282. SOAP_FMAC1
  7283. int
  7284. SOAP_FMAC2
  7285. soap_attachment(struct soap *soap, const char *tag, int id, const void *p, const struct soap_array *a, const char *aid, const char *atype, const char *aoptions, int n, const char *type, int t)
  7286. {
  7287. #ifndef WITH_NOIDREF
  7288. struct soap_plist *pp;
  7289. int i;
  7290. if (!p || !a->__ptr || (!aid && !atype))
  7291. return soap_element_id(soap, tag, id, p, a, n, type, t);
  7292. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attachment tag='%s' id='%s' (%d) type='%s'\n", tag, aid ? aid : SOAP_STR_EOS, id, atype ? atype : SOAP_STR_EOS));
  7293. i = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  7294. if (!i)
  7295. { i = soap_pointer_enter(soap, p, a, n, t, &pp);
  7296. if (!i)
  7297. { soap->error = SOAP_EOM;
  7298. return -1;
  7299. }
  7300. }
  7301. if (id <= 0)
  7302. id = i;
  7303. if (!aid)
  7304. {
  7305. #ifdef HAVE_SNPRINTF
  7306. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), soap->dime_id_format, id);
  7307. #else
  7308. sprintf(soap->tmpbuf, soap->dime_id_format, id);
  7309. #endif
  7310. aid = soap_strdup(soap, soap->tmpbuf);
  7311. }
  7312. /* Add MTOM xop:Include element when necessary */
  7313. /* TODO: this code to be obsoleted with new import/xop.h conventions */
  7314. if ((soap->mode & SOAP_ENC_MTOM) && strcmp(tag, "xop:Include"))
  7315. { if (soap_element_begin_out(soap, tag, 0, type)
  7316. || soap_element_href(soap, "xop:Include", 0, "xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" href", aid)
  7317. || soap_element_end_out(soap, tag))
  7318. return soap->error;
  7319. }
  7320. else if (soap_element_href(soap, tag, 0, "href", aid))
  7321. return soap->error;
  7322. if (soap->mode & SOAP_IO_LENGTH)
  7323. { if (pp->mark1 != 3)
  7324. { struct soap_multipart *content;
  7325. if (soap->mode & SOAP_ENC_MTOM)
  7326. content = soap_new_multipart(soap, &soap->mime.first, &soap->mime.last, (char*)a->__ptr, a->__size);
  7327. else
  7328. content = soap_new_multipart(soap, &soap->dime.first, &soap->dime.last, (char*)a->__ptr, a->__size);
  7329. if (!content)
  7330. { soap->error = SOAP_EOM;
  7331. return -1;
  7332. }
  7333. if (!strncmp(aid, "cid:", 4)) /* RFC 2111 */
  7334. { if (soap->mode & SOAP_ENC_MTOM)
  7335. { char *s = (char*)soap_malloc(soap, strlen(aid) - 1);
  7336. if (s)
  7337. { *s = '<';
  7338. strcpy(s + 1, aid + 4);
  7339. strcat(s, ">");
  7340. content->id = s;
  7341. }
  7342. }
  7343. else
  7344. content->id = aid + 4;
  7345. }
  7346. else
  7347. content->id = aid;
  7348. content->type = atype;
  7349. content->options = aoptions;
  7350. content->encoding = SOAP_MIME_BINARY;
  7351. pp->mark1 = 3;
  7352. }
  7353. }
  7354. else
  7355. pp->mark2 = 3;
  7356. #endif
  7357. return -1;
  7358. }
  7359. #endif
  7360. #endif
  7361. /******************************************************************************/
  7362. #ifndef WITH_NOIDREF
  7363. #ifndef PALM_1
  7364. static void
  7365. soap_init_iht(struct soap *soap)
  7366. { register int i;
  7367. for (i = 0; i < SOAP_IDHASH; i++)
  7368. soap->iht[i] = NULL;
  7369. }
  7370. #endif
  7371. #endif
  7372. /******************************************************************************/
  7373. #ifndef WITH_NOIDREF
  7374. #ifndef PALM_1
  7375. static void
  7376. soap_free_iht(struct soap *soap)
  7377. { register int i;
  7378. register struct soap_ilist *ip = NULL, *p = NULL;
  7379. register struct soap_flist *fp = NULL, *fq = NULL;
  7380. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free ID hashtable\n"));
  7381. for (i = 0; i < SOAP_IDHASH; i++)
  7382. { for (ip = soap->iht[i]; ip; ip = p)
  7383. { for (fp = ip->flist; fp; fp = fq)
  7384. { fq = fp->next;
  7385. SOAP_FREE(soap, fp);
  7386. }
  7387. p = ip->next;
  7388. SOAP_FREE(soap, ip);
  7389. }
  7390. soap->iht[i] = NULL;
  7391. }
  7392. }
  7393. #endif
  7394. #endif
  7395. /******************************************************************************/
  7396. #ifndef WITH_NOIDREF
  7397. #ifndef PALM_2
  7398. SOAP_FMAC1
  7399. struct soap_ilist *
  7400. SOAP_FMAC2
  7401. soap_lookup(struct soap *soap, const char *id)
  7402. { register struct soap_ilist *ip = NULL;
  7403. for (ip = soap->iht[soap_hash(id)]; ip; ip = ip->next)
  7404. if (!strcmp(ip->id, id))
  7405. return ip;
  7406. return NULL;
  7407. }
  7408. #endif
  7409. #endif
  7410. /******************************************************************************/
  7411. #ifndef WITH_NOIDREF
  7412. #ifndef PALM_2
  7413. SOAP_FMAC1
  7414. struct soap_ilist *
  7415. SOAP_FMAC2
  7416. soap_enter(struct soap *soap, const char *id)
  7417. { register size_t h;
  7418. register struct soap_ilist *ip;
  7419. ip = (struct soap_ilist*)SOAP_MALLOC(soap, sizeof(struct soap_ilist) + strlen(id));
  7420. if (ip)
  7421. { strcpy((char*)ip->id, id);
  7422. h = soap_hash(id); /* h = (HASH(id) % SOAP_IDHASH) so soap->iht[h] is safe */
  7423. ip->next = soap->iht[h];
  7424. soap->iht[h] = ip;
  7425. }
  7426. return ip;
  7427. }
  7428. #endif
  7429. #endif
  7430. /******************************************************************************/
  7431. #ifndef PALM_2
  7432. SOAP_FMAC1
  7433. void*
  7434. SOAP_FMAC2
  7435. soap_malloc(struct soap *soap, size_t n)
  7436. { register char *p;
  7437. if (!n)
  7438. return (void*)SOAP_NON_NULL;
  7439. if (!soap)
  7440. return SOAP_MALLOC(soap, n);
  7441. if (soap->fmalloc)
  7442. p = (char*)soap->fmalloc(soap, n);
  7443. else
  7444. { n += sizeof(short);
  7445. n += (-(long)n) & (sizeof(void*)-1); /* align at 4-, 8- or 16-byte boundary */
  7446. if (!(p = (char*)SOAP_MALLOC(soap, n + sizeof(void*) + sizeof(size_t))))
  7447. { soap->error = SOAP_EOM;
  7448. return NULL;
  7449. }
  7450. /* set the canary to detect corruption */
  7451. *(unsigned short*)(p + n - sizeof(unsigned short)) = (unsigned short)SOAP_CANARY;
  7452. /* keep chain of alloced cells for destruction */
  7453. *(void**)(p + n) = soap->alist;
  7454. *(size_t*)(p + n + sizeof(void*)) = n;
  7455. soap->alist = p + n;
  7456. }
  7457. soap->alloced = 1;
  7458. return p;
  7459. }
  7460. #endif
  7461. /******************************************************************************/
  7462. #ifdef SOAP_MEM_DEBUG
  7463. static void
  7464. soap_init_mht(struct soap *soap)
  7465. { register int i;
  7466. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  7467. soap->mht[i] = NULL;
  7468. }
  7469. #endif
  7470. /******************************************************************************/
  7471. #ifdef SOAP_MEM_DEBUG
  7472. static void
  7473. soap_free_mht(struct soap *soap)
  7474. { register int i;
  7475. register struct soap_mlist *mp, *mq;
  7476. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  7477. { for (mp = soap->mht[i]; mp; mp = mq)
  7478. { mq = mp->next;
  7479. if (mp->live)
  7480. fprintf(stderr, "%s(%d): malloc() = %p not freed (memory leak or forgot to call soap_end()?)\n", mp->file, mp->line, mp->ptr);
  7481. free(mp);
  7482. }
  7483. soap->mht[i] = NULL;
  7484. }
  7485. }
  7486. #endif
  7487. /******************************************************************************/
  7488. #ifdef SOAP_MEM_DEBUG
  7489. SOAP_FMAC1
  7490. void*
  7491. SOAP_FMAC2
  7492. soap_track_malloc(struct soap *soap, const char *file, int line, size_t size)
  7493. { register void *p = malloc(size);
  7494. if (soap)
  7495. { register size_t h = soap_hash_ptr(p);
  7496. register struct soap_mlist *mp = (struct soap_mlist*)malloc(sizeof(struct soap_mlist));
  7497. if (soap->fdebug[SOAP_INDEX_TEST])
  7498. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): malloc(%lu) = %p\n", file, line, (unsigned long)size, p));
  7499. }
  7500. mp->next = soap->mht[h];
  7501. mp->ptr = p;
  7502. mp->file = file;
  7503. mp->line = line;
  7504. mp->live = 1;
  7505. soap->mht[h] = mp;
  7506. }
  7507. return p;
  7508. }
  7509. #endif
  7510. /******************************************************************************/
  7511. #ifdef SOAP_MEM_DEBUG
  7512. SOAP_FMAC1
  7513. void
  7514. SOAP_FMAC2
  7515. soap_track_free(struct soap *soap, const char *file, int line, void *p)
  7516. { register size_t h = soap_hash_ptr(p);
  7517. register struct soap_mlist *mp;
  7518. for (mp = soap->mht[h]; mp; mp = mp->next)
  7519. if (mp->ptr == p)
  7520. break;
  7521. if (mp)
  7522. { if (mp->live)
  7523. { free(p);
  7524. if (soap->fdebug[SOAP_INDEX_TEST])
  7525. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): free(%p)\n", file, line, p));
  7526. }
  7527. mp->live = 0;
  7528. }
  7529. else
  7530. fprintf(stderr, "%s(%d): free(%p) double free of pointer malloced at %s(%d)\n", file, line, p, mp->file, mp->line);
  7531. }
  7532. else
  7533. fprintf(stderr, "%s(%d): free(%p) pointer not malloced\n", file, line, p);
  7534. }
  7535. #endif
  7536. /******************************************************************************/
  7537. #ifdef SOAP_MEM_DEBUG
  7538. static void
  7539. soap_track_unlink(struct soap *soap, const void *p)
  7540. { register size_t h = soap_hash_ptr(p);
  7541. register struct soap_mlist *mp;
  7542. for (mp = soap->mht[h]; mp; mp = mp->next)
  7543. if (mp->ptr == p)
  7544. break;
  7545. if (mp)
  7546. mp->live = 0;
  7547. }
  7548. #endif
  7549. /******************************************************************************/
  7550. #ifndef PALM_2
  7551. SOAP_FMAC1
  7552. void
  7553. SOAP_FMAC2
  7554. soap_dealloc(struct soap *soap, void *p)
  7555. { if (soap_check_state(soap))
  7556. return;
  7557. if (p)
  7558. { register char **q;
  7559. for (q = (char**)&soap->alist; *q; q = *(char***)q)
  7560. {
  7561. if (*(unsigned short*)(char*)(*q - sizeof(unsigned short)) != (unsigned short)SOAP_CANARY)
  7562. {
  7563. #ifdef SOAP_MEM_DEBUG
  7564. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  7565. #endif
  7566. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  7567. DBGHEX(TEST, *q - 200, 200);
  7568. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  7569. soap->error = SOAP_MOE;
  7570. return;
  7571. }
  7572. if (p == (void*)(*q - *(size_t*)(*q + sizeof(void*))))
  7573. { *q = **(char***)q;
  7574. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Freed data at %p\n", p));
  7575. SOAP_FREE(soap, p);
  7576. return;
  7577. }
  7578. }
  7579. soap_delete(soap, p);
  7580. }
  7581. else
  7582. { register char *q;
  7583. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free all soap_malloc() data\n"));
  7584. while (soap->alist)
  7585. { q = (char*)soap->alist;
  7586. if (*(unsigned short*)(char*)(q - sizeof(unsigned short)) != (unsigned short)SOAP_CANARY)
  7587. {
  7588. #ifdef SOAP_MEM_DEBUG
  7589. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  7590. #endif
  7591. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  7592. DBGHEX(TEST, q - 200, 200);
  7593. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  7594. soap->error = SOAP_MOE;
  7595. return;
  7596. }
  7597. soap->alist = *(void**)q;
  7598. q -= *(size_t*)(q + sizeof(void*));
  7599. SOAP_FREE(soap, q);
  7600. }
  7601. /* we must assume these were deallocated: */
  7602. soap->http_content = NULL;
  7603. soap->action = NULL;
  7604. soap->fault = NULL;
  7605. soap->header = NULL;
  7606. soap->userid = NULL;
  7607. soap->passwd = NULL;
  7608. soap->authrealm = NULL;
  7609. #ifdef WITH_NTLM
  7610. soap->ntlm_challenge = NULL;
  7611. #endif
  7612. #ifndef WITH_LEANER
  7613. soap_clr_mime(soap);
  7614. #endif
  7615. }
  7616. }
  7617. #endif
  7618. /******************************************************************************/
  7619. #ifndef PALM_2
  7620. SOAP_FMAC1
  7621. void
  7622. SOAP_FMAC2
  7623. soap_delete(struct soap *soap, void *p)
  7624. { register struct soap_clist **cp;
  7625. if (soap_check_state(soap))
  7626. return;
  7627. cp = &soap->clist;
  7628. if (p)
  7629. { while (*cp)
  7630. { if (p == (*cp)->ptr)
  7631. { register struct soap_clist *q = *cp;
  7632. *cp = q->next;
  7633. if (q->fdelete(q))
  7634. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: deletion callback failed for object type %d\n", q->ptr, q->type));
  7635. #ifdef SOAP_MEM_DEBUG
  7636. fprintf(stderr, "new(object type = %d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  7637. #endif
  7638. }
  7639. SOAP_FREE(soap, q);
  7640. return;
  7641. }
  7642. cp = &(*cp)->next;
  7643. }
  7644. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: address not in list\n", p));
  7645. }
  7646. else
  7647. { while (*cp)
  7648. { register struct soap_clist *q = *cp;
  7649. *cp = q->next;
  7650. if (q->fdelete(q))
  7651. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: deletion callback failed for object type %d\n", q->ptr, q->type));
  7652. #ifdef SOAP_MEM_DEBUG
  7653. fprintf(stderr, "new(object type = %d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  7654. #endif
  7655. }
  7656. SOAP_FREE(soap, q);
  7657. }
  7658. }
  7659. soap->fault = NULL; /* this was possibly deallocated */
  7660. soap->header = NULL; /* this was possibly deallocated */
  7661. }
  7662. #endif
  7663. /******************************************************************************/
  7664. #ifndef PALM_2
  7665. SOAP_FMAC1
  7666. void
  7667. SOAP_FMAC2
  7668. soap_delegate_deletion(struct soap *soap, struct soap *soap_to)
  7669. { register struct soap_clist *cp;
  7670. register char **q;
  7671. #ifdef SOAP_MEM_DEBUG
  7672. register void *p;
  7673. register struct soap_mlist **mp, *mq;
  7674. size_t h;
  7675. #endif
  7676. for (q = (char**)&soap->alist; *q; q = *(char***)q)
  7677. {
  7678. if (*(unsigned short*)(char*)(*q - sizeof(unsigned short)) != (unsigned short)SOAP_CANARY)
  7679. {
  7680. #ifdef SOAP_MEM_DEBUG
  7681. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  7682. #endif
  7683. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  7684. DBGHEX(TEST, *q - 200, 200);
  7685. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  7686. soap->error = SOAP_MOE;
  7687. return;
  7688. }
  7689. #ifdef SOAP_MEM_DEBUG
  7690. p = (void*)(*q - *(size_t*)(*q + sizeof(void*)));
  7691. h = soap_hash_ptr(p);
  7692. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next)
  7693. { if ((*mp)->ptr == p)
  7694. { mq = *mp;
  7695. *mp = mq->next;
  7696. mq->next = soap_to->mht[h];
  7697. soap_to->mht[h] = mq;
  7698. break;
  7699. }
  7700. }
  7701. #endif
  7702. }
  7703. *q = (char*)soap_to->alist;
  7704. soap_to->alist = soap->alist;
  7705. soap->alist = NULL;
  7706. #ifdef SOAP_MEM_DEBUG
  7707. cp = soap->clist;
  7708. while (cp)
  7709. { h = soap_hash_ptr(cp);
  7710. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next)
  7711. { if ((*mp)->ptr == cp)
  7712. { mq = *mp;
  7713. *mp = mq->next;
  7714. mq->next = soap_to->mht[h];
  7715. soap_to->mht[h] = mq;
  7716. break;
  7717. }
  7718. }
  7719. cp = cp->next;
  7720. }
  7721. #endif
  7722. cp = soap_to->clist;
  7723. if (cp)
  7724. { while (cp->next)
  7725. cp = cp->next;
  7726. cp->next = soap->clist;
  7727. }
  7728. else
  7729. soap_to->clist = soap->clist;
  7730. soap->clist = NULL;
  7731. }
  7732. #endif
  7733. /******************************************************************************/
  7734. #ifndef PALM_2
  7735. SOAP_FMAC1
  7736. struct soap_clist *
  7737. SOAP_FMAC2
  7738. soap_link(struct soap *soap, void *p, int t, int n, int (*fdelete)(struct soap_clist*))
  7739. { register struct soap_clist *cp;
  7740. if ((cp = (struct soap_clist*)SOAP_MALLOC(soap, sizeof(struct soap_clist))))
  7741. { cp->next = soap->clist;
  7742. cp->type = t;
  7743. cp->size = n;
  7744. cp->ptr = p;
  7745. cp->fdelete = fdelete;
  7746. soap->clist = cp;
  7747. }
  7748. return cp;
  7749. }
  7750. #endif
  7751. /******************************************************************************/
  7752. #ifndef PALM_2
  7753. SOAP_FMAC1
  7754. int
  7755. SOAP_FMAC2
  7756. soap_unlink(struct soap *soap, const void *p)
  7757. { register char **q;
  7758. register struct soap_clist **cp;
  7759. if (soap && p)
  7760. { for (q = (char**)&soap->alist; *q; q = *(char***)q)
  7761. { if (p == (void*)(*q - *(size_t*)(*q + sizeof(void*))))
  7762. { *q = **(char***)q;
  7763. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked data %p\n", p));
  7764. #ifdef SOAP_MEM_DEBUG
  7765. soap_track_unlink(soap, p);
  7766. #endif
  7767. return SOAP_OK; /* found and removed from dealloc chain */
  7768. }
  7769. }
  7770. for (cp = &soap->clist; *cp; cp = &(*cp)->next)
  7771. { if (p == (*cp)->ptr)
  7772. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked class instance %p\n", p));
  7773. q = (char**)*cp;
  7774. *cp = (*cp)->next;
  7775. SOAP_FREE(soap, q);
  7776. return SOAP_OK; /* found and removed from dealloc chain */
  7777. }
  7778. }
  7779. }
  7780. return SOAP_ERR;
  7781. }
  7782. #endif
  7783. /******************************************************************************/
  7784. #ifndef WITH_NOIDREF
  7785. #ifndef PALM_2
  7786. SOAP_FMAC1
  7787. int
  7788. SOAP_FMAC2
  7789. soap_lookup_type(struct soap *soap, const char *id)
  7790. { register struct soap_ilist *ip;
  7791. if (id && *id)
  7792. { ip = soap_lookup(soap, id);
  7793. if (ip)
  7794. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup id='%s' type=%d\n", id, ip->type));
  7795. return ip->type;
  7796. }
  7797. }
  7798. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "lookup type id='%s' NOT FOUND! Need to get it from xsi:type\n", id));
  7799. return 0;
  7800. }
  7801. #endif
  7802. #endif
  7803. /******************************************************************************/
  7804. #ifndef WITH_NOIDREF
  7805. #ifndef PALM_2
  7806. SOAP_FMAC1
  7807. void*
  7808. SOAP_FMAC2
  7809. soap_id_lookup(struct soap *soap, const char *id, void **p, int t, size_t n, unsigned int k)
  7810. { struct soap_ilist *ip;
  7811. void **q;
  7812. if (!p || !id || !*id)
  7813. return p;
  7814. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  7815. if (!ip)
  7816. { if (!(ip = soap_enter(soap, id))) /* new hash table entry for string id */
  7817. return NULL;
  7818. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding first href='%s' type=%d %p (%u bytes)\n", id, t, p, (unsigned int)n));
  7819. ip->type = t;
  7820. ip->size = n;
  7821. ip->link = p;
  7822. ip->copy = NULL;
  7823. ip->flist = NULL;
  7824. ip->ptr = NULL;
  7825. ip->level = k;
  7826. *p = NULL;
  7827. }
  7828. else if (ip->ptr)
  7829. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolved href='%s' type=%d location=%p (%u bytes)\n", id, t, ip->ptr, (unsigned int)n));
  7830. if (ip->type != t)
  7831. { strcpy(soap->id, id);
  7832. soap->error = SOAP_HREF;
  7833. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Type incompatibility: href='%s' id-type=%d href-type=%d\n", id, ip->type, t));
  7834. return NULL;
  7835. }
  7836. while (ip->level < k)
  7837. { q = (void**)soap_malloc(soap, sizeof(void*));
  7838. if (!q)
  7839. return NULL;
  7840. *p = (void*)q;
  7841. p = q;
  7842. k--;
  7843. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level...\n"));
  7844. }
  7845. *p = ip->ptr;
  7846. }
  7847. else if (ip->level > k)
  7848. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving level %u pointers to href='%s'\n", ip->level, id));
  7849. while (ip->level > k)
  7850. { void *s, **r = &ip->link;
  7851. q = (void**)ip->link;
  7852. while (q)
  7853. { *r = (void*)soap_malloc(soap, sizeof(void*));
  7854. if (!*r)
  7855. return NULL;
  7856. s = *q;
  7857. *q = *r;
  7858. r = (void**)*r;
  7859. q = (void**)s;
  7860. }
  7861. *r = NULL;
  7862. ip->size = n;
  7863. ip->copy = NULL;
  7864. ip->level = ip->level - 1;
  7865. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level...\n"));
  7866. }
  7867. q = (void**)ip->link;
  7868. ip->link = p;
  7869. *p = (void*)q;
  7870. }
  7871. else
  7872. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarded href='%s' type=%d location=%p (%u bytes)\n", id, t, p, (unsigned int)n));
  7873. while (ip->level < k)
  7874. { q = (void**)soap_malloc(soap, sizeof(void*));
  7875. if (!q)
  7876. return NULL;
  7877. *p = q;
  7878. p = q;
  7879. k--;
  7880. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level...\n"));
  7881. }
  7882. q = (void**)ip->link;
  7883. ip->link = p;
  7884. *p = (void*)q;
  7885. }
  7886. return p;
  7887. }
  7888. #endif
  7889. #endif
  7890. /******************************************************************************/
  7891. #ifndef WITH_NOIDREF
  7892. #ifndef PALM_2
  7893. SOAP_FMAC1
  7894. void*
  7895. SOAP_FMAC2
  7896. soap_id_forward(struct soap *soap, const char *href, void *p, size_t len, int st, int tt, size_t n, unsigned int k, void (*fcopy)(struct soap*, int, int, void*, size_t, const void*, size_t))
  7897. { struct soap_ilist *ip;
  7898. if (!p || !href || !*href)
  7899. return p;
  7900. ip = soap_lookup(soap, href); /* lookup pointer to hash table entry for string id */
  7901. if (!ip)
  7902. { if (!(ip = soap_enter(soap, href))) /* new hash table entry for string id */
  7903. return NULL;
  7904. ip->type = st;
  7905. ip->size = n;
  7906. ip->link = NULL;
  7907. ip->copy = NULL;
  7908. ip->ptr = NULL;
  7909. ip->level = 0;
  7910. ip->flist = NULL;
  7911. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "New entry href='%s' type=%d size=%lu level=%d location=%p\n", href, st, (unsigned long)n, k, p));
  7912. }
  7913. else if (ip->type != st || (ip->level == k && ip->size != n))
  7914. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Type incompatibility id='%s' expect type=%d size=%lu level=%u got type=%d size=%lu\n", href, ip->type, (unsigned long)ip->size, k, st, (unsigned long)n));
  7915. strcpy(soap->id, href);
  7916. soap->error = SOAP_HREF;
  7917. return NULL;
  7918. }
  7919. if (fcopy || n < sizeof(void*) || *href != '#')
  7920. { register struct soap_flist *fp = (struct soap_flist*)SOAP_MALLOC(soap, sizeof(struct soap_flist));
  7921. if (!fp)
  7922. { soap->error = SOAP_EOM;
  7923. return NULL;
  7924. }
  7925. fp->next = ip->flist;
  7926. fp->type = tt;
  7927. fp->ptr = p;
  7928. fp->level = k;
  7929. fp->len = len;
  7930. if (fcopy)
  7931. fp->fcopy = fcopy;
  7932. else
  7933. fp->fcopy = soap_fcopy;
  7934. ip->flist = fp;
  7935. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding type=%d (target type=%d) size=%lu location=%p level=%u len=%lu href='%s'\n", st, tt, (unsigned long)n, p, k, (unsigned long)len, href));
  7936. }
  7937. else
  7938. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding copying address %p for type=%d href='%s'\n", p, st, href));
  7939. *(void**)p = ip->copy;
  7940. ip->copy = p;
  7941. }
  7942. return p;
  7943. }
  7944. #endif
  7945. #endif
  7946. /******************************************************************************/
  7947. #ifndef PALM_2
  7948. SOAP_FMAC1
  7949. void*
  7950. SOAP_FMAC2
  7951. soap_id_enter(struct soap *soap, const char *id, void *p, int t, size_t n, unsigned int k, const char *type, const char *arrayType, void *(*finstantiate)(struct soap*, int, const char*, const char*, size_t*))
  7952. {
  7953. #ifndef WITH_NOIDREF
  7954. struct soap_ilist *ip;
  7955. #endif
  7956. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Enter id='%s' type=%d loc=%p size=%lu level=%u\n", id, t, p, (unsigned long)n, k));
  7957. soap->alloced = 0;
  7958. if (!p)
  7959. { if (finstantiate)
  7960. p = finstantiate(soap, t, type, arrayType, &n);
  7961. else
  7962. p = soap_malloc(soap, n);
  7963. if (p)
  7964. soap->alloced = 1;
  7965. }
  7966. #ifndef WITH_NOIDREF
  7967. if (!id || !*id)
  7968. #endif
  7969. return p;
  7970. #ifndef WITH_NOIDREF
  7971. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  7972. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Lookup entry id='%s for location=%p'\n", id, p));
  7973. if (!ip)
  7974. { if (!(ip = soap_enter(soap, id))) /* new hash table entry for string id */
  7975. return NULL;
  7976. ip->type = t;
  7977. ip->link = NULL;
  7978. ip->copy = NULL;
  7979. ip->flist = NULL;
  7980. ip->size = n;
  7981. ip->ptr = p;
  7982. ip->level = k;
  7983. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "New entry id='%s' type=%d size=%lu level=%u location=%p\n", id, t, (unsigned long)n, k, p));
  7984. }
  7985. else if ((ip->type != t || (ip->level == k && ip->size != n)) && (ip->copy || ip->flist))
  7986. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Type incompatibility id='%s' expect type=%d size=%lu level=%u got type=%d size=%lu\n", id, ip->type, (unsigned long)ip->size, k, t, (unsigned long)n));
  7987. strcpy(soap->id, id);
  7988. soap->error = SOAP_HREF;
  7989. return NULL;
  7990. }
  7991. else if (ip->ptr)
  7992. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Multiply defined id='%s'\n", id));
  7993. strcpy(soap->id, id);
  7994. soap->error = SOAP_DUPLICATE_ID;
  7995. return NULL;
  7996. }
  7997. else
  7998. { ip->size = n;
  7999. ip->ptr = p;
  8000. ip->level = k;
  8001. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update entry id='%s' type=%d location=%p size=%lu level=%u\n", id, t, p, (unsigned long)n, k));
  8002. }
  8003. return ip->ptr;
  8004. #endif
  8005. }
  8006. #endif
  8007. /******************************************************************************/
  8008. #ifndef PALM_2
  8009. SOAP_FMAC1
  8010. void
  8011. SOAP_FMAC2
  8012. soap_fcopy(struct soap *soap, int st, int tt, void *p, size_t len, const void *q, size_t n)
  8013. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Copying data type=%d (target type=%d) %p -> %p (%lu bytes)\n", st, tt, q, p, (unsigned long)n));
  8014. memcpy(p, q, n);
  8015. (void)soap; (void)st; (void)tt; (void)len;
  8016. }
  8017. #endif
  8018. /******************************************************************************/
  8019. #ifndef PALM_1
  8020. SOAP_FMAC1
  8021. int
  8022. SOAP_FMAC2
  8023. soap_end_send(struct soap *soap)
  8024. {
  8025. #ifndef WITH_LEANER
  8026. int err;
  8027. if (soap->dime.list)
  8028. { /* SOAP body referenced attachments must appear first */
  8029. soap->dime.last->next = soap->dime.first;
  8030. soap->dime.first = soap->dime.list->next;
  8031. soap->dime.list->next = NULL;
  8032. soap->dime.last = soap->dime.list;
  8033. }
  8034. if (!(err = soap_putdime(soap)))
  8035. err = soap_putmime(soap);
  8036. soap->mime.list = NULL;
  8037. soap->mime.first = NULL;
  8038. soap->mime.last = NULL;
  8039. soap->dime.list = NULL;
  8040. soap->dime.first = NULL;
  8041. soap->dime.last = NULL;
  8042. if (err)
  8043. return err;
  8044. #endif
  8045. return soap_end_send_flush(soap);
  8046. }
  8047. #endif
  8048. /******************************************************************************/
  8049. #ifndef PALM_1
  8050. SOAP_FMAC1
  8051. int
  8052. SOAP_FMAC2
  8053. soap_end_send_flush(struct soap *soap)
  8054. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End send mode=0x%x\n", soap->mode));
  8055. if (soap->mode & SOAP_IO) /* need to flush the remaining data in buffer */
  8056. { if (soap_flush(soap))
  8057. #ifdef WITH_ZLIB
  8058. { if (soap->mode & SOAP_ENC_ZLIB && soap->zlib_state == SOAP_ZLIB_DEFLATE)
  8059. { soap->zlib_state = SOAP_ZLIB_NONE;
  8060. deflateEnd(soap->d_stream);
  8061. }
  8062. return soap->error;
  8063. }
  8064. #else
  8065. return soap->error;
  8066. #endif
  8067. #ifdef WITH_ZLIB
  8068. if (soap->mode & SOAP_ENC_ZLIB)
  8069. { int r;
  8070. soap->d_stream->avail_in = 0;
  8071. do
  8072. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflating remainder\n"));
  8073. r = deflate(soap->d_stream, Z_FINISH);
  8074. if (soap->d_stream->avail_out != SOAP_BUFLEN)
  8075. { if (soap_flush_raw(soap, soap->z_buf, SOAP_BUFLEN - soap->d_stream->avail_out))
  8076. { soap->zlib_state = SOAP_ZLIB_NONE;
  8077. deflateEnd(soap->d_stream);
  8078. return soap->error;
  8079. }
  8080. soap->d_stream->next_out = (Byte*)soap->z_buf;
  8081. soap->d_stream->avail_out = SOAP_BUFLEN;
  8082. }
  8083. } while (r == Z_OK);
  8084. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflated total %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  8085. soap->z_ratio_out = (float)soap->d_stream->total_out / (float)soap->d_stream->total_in;
  8086. soap->mode &= ~SOAP_ENC_ZLIB;
  8087. soap->zlib_state = SOAP_ZLIB_NONE;
  8088. if (deflateEnd(soap->d_stream) != Z_OK || r != Z_STREAM_END)
  8089. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to end deflate: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  8090. return soap->error = SOAP_ZLIB_ERROR;
  8091. }
  8092. #ifdef WITH_GZIP
  8093. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  8094. { soap->z_buf[0] = soap->z_crc & 0xFF;
  8095. soap->z_buf[1] = (soap->z_crc >> 8) & 0xFF;
  8096. soap->z_buf[2] = (soap->z_crc >> 16) & 0xFF;
  8097. soap->z_buf[3] = (soap->z_crc >> 24) & 0xFF;
  8098. soap->z_buf[4] = soap->d_stream->total_in & 0xFF;
  8099. soap->z_buf[5] = (soap->d_stream->total_in >> 8) & 0xFF;
  8100. soap->z_buf[6] = (soap->d_stream->total_in >> 16) & 0xFF;
  8101. soap->z_buf[7] = (soap->d_stream->total_in >> 24) & 0xFF;
  8102. if (soap_flush_raw(soap, soap->z_buf, 8))
  8103. return soap->error;
  8104. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip crc32=%lu\n", (unsigned long)soap->z_crc));
  8105. }
  8106. #endif
  8107. }
  8108. #endif
  8109. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE)
  8110. { char *p;
  8111. #ifndef WITH_NOHTTP
  8112. if (!(soap->mode & SOAP_ENC_XML))
  8113. { soap->mode--;
  8114. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending buffered message of length %u\n", (unsigned int)soap->blist->size));
  8115. if (soap->status >= SOAP_POST)
  8116. soap->error = soap->fpost(soap, soap->endpoint, soap->host, soap->port, soap->path, soap->action, soap->blist->size);
  8117. else if (soap->status != SOAP_STOP)
  8118. soap->error = soap->fresponse(soap, soap->status, soap->blist->size);
  8119. if (soap->error || soap_flush(soap))
  8120. return soap->error;
  8121. soap->mode++;
  8122. }
  8123. #endif
  8124. for (p = soap_first_block(soap, NULL); p; p = soap_next_block(soap, NULL))
  8125. { DBGMSG(SENT, p, soap_block_size(soap, NULL));
  8126. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send %u bytes to socket=%d/fd=%d\n", (unsigned int)soap_block_size(soap, NULL), soap->socket, soap->sendfd));
  8127. if ((soap->error = soap->fsend(soap, p, soap_block_size(soap, NULL))))
  8128. { soap_end_block(soap, NULL);
  8129. return soap->error;
  8130. }
  8131. }
  8132. soap_end_block(soap, NULL);
  8133. if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)))
  8134. return soap->error;
  8135. }
  8136. #ifndef WITH_LEANER
  8137. else if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  8138. { DBGMSG(SENT, "\r\n0\r\n\r\n", 7);
  8139. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send 7 bytes to socket=%d/fd=%d\n", soap->socket, soap->sendfd));
  8140. if ((soap->error = soap->fsend(soap, "\r\n0\r\n\r\n", 7)))
  8141. return soap->error;
  8142. }
  8143. #endif
  8144. }
  8145. #ifdef WITH_TCPFIN
  8146. #ifdef WITH_OPENSSL
  8147. if (!soap->ssl && soap_valid_socket(soap->socket) && !soap->keep_alive && !(soap->omode & SOAP_IO_UDP))
  8148. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR); /* Send TCP FIN */
  8149. #else
  8150. if (soap_valid_socket(soap->socket) && !soap->keep_alive && !(soap->omode & SOAP_IO_UDP))
  8151. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR); /* Send TCP FIN */
  8152. #endif
  8153. #endif
  8154. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of send phase\n"));
  8155. soap->omode &= ~SOAP_SEC_WSUID;
  8156. soap->count = 0;
  8157. soap->part = SOAP_END;
  8158. return SOAP_OK;
  8159. }
  8160. #endif
  8161. /******************************************************************************/
  8162. #ifndef PALM_1
  8163. SOAP_FMAC1
  8164. int
  8165. SOAP_FMAC2
  8166. soap_end_recv(struct soap *soap)
  8167. { soap->part = SOAP_END;
  8168. #ifndef WITH_LEAN
  8169. soap->wsuid = NULL; /* reset before next send */
  8170. soap->c14nexclude = NULL; /* reset before next send */
  8171. #endif
  8172. #ifndef WITH_LEANER
  8173. soap->ffilterrecv = NULL;
  8174. if ((soap->mode & SOAP_ENC_DIME) && soap_getdime(soap))
  8175. { soap->dime.first = NULL;
  8176. soap->dime.last = NULL;
  8177. return soap->error;
  8178. }
  8179. soap->dime.list = soap->dime.first;
  8180. soap->dime.first = NULL;
  8181. soap->dime.last = NULL;
  8182. /* Check if MIME attachments and mime-post-check flag is set, if so call soap_resolve() and return */
  8183. if (soap->mode & SOAP_ENC_MIME)
  8184. { if (soap->mode & SOAP_MIME_POSTCHECK)
  8185. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Post checking MIME attachments\n"));
  8186. if (!soap->keep_alive)
  8187. soap->keep_alive = -1;
  8188. #ifndef WITH_NOIDREF
  8189. soap_resolve(soap);
  8190. #endif
  8191. return SOAP_OK;
  8192. }
  8193. if (soap_getmime(soap))
  8194. return soap->error;
  8195. }
  8196. soap->mime.list = soap->mime.first;
  8197. soap->mime.first = NULL;
  8198. soap->mime.last = NULL;
  8199. soap->mime.boundary = NULL;
  8200. if (soap->xlist)
  8201. { struct soap_multipart *content;
  8202. for (content = soap->mime.list; content; content = content->next)
  8203. soap_resolve_attachment(soap, content);
  8204. }
  8205. #endif
  8206. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "End of receive message ok\n"));
  8207. #ifdef WITH_ZLIB
  8208. if (soap->mode & SOAP_ENC_ZLIB)
  8209. { /* Make sure end of compressed content is reached */
  8210. while (soap->d_stream->next_out != Z_NULL)
  8211. if ((int)soap_get1(soap) == EOF)
  8212. break;
  8213. soap->mode &= ~SOAP_ENC_ZLIB;
  8214. memcpy(soap->buf, soap->z_buf, SOAP_BUFLEN);
  8215. soap->bufidx = (char*)soap->d_stream->next_in - soap->z_buf;
  8216. soap->buflen = soap->z_buflen;
  8217. soap->zlib_state = SOAP_ZLIB_NONE;
  8218. if (inflateEnd(soap->d_stream) != Z_OK)
  8219. return soap->error = SOAP_ZLIB_ERROR;
  8220. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Inflate end ok\n"));
  8221. #ifdef WITH_GZIP
  8222. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  8223. { soap_wchar c;
  8224. short i;
  8225. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Inflate gzip crc check\n"));
  8226. for (i = 0; i < 8; i++)
  8227. { if ((int)(c = soap_get1(soap)) == EOF)
  8228. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Gzip error: unable to read crc value\n"));
  8229. return soap->error = SOAP_ZLIB_ERROR;
  8230. }
  8231. soap->z_buf[i] = (char)c;
  8232. }
  8233. if (soap->z_crc != ((uLong)(unsigned char)soap->z_buf[0] | ((uLong)(unsigned char)soap->z_buf[1] << 8) | ((uLong)(unsigned char)soap->z_buf[2] << 16) | ((uLong)(unsigned char)soap->z_buf[3] << 24)))
  8234. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Gzip inflate error: crc check failed, message corrupted? (crc32=%lu)\n", (unsigned long)soap->z_crc));
  8235. return soap->error = SOAP_ZLIB_ERROR;
  8236. }
  8237. if (soap->d_stream->total_out != ((uLong)(unsigned char)soap->z_buf[4] | ((uLong)(unsigned char)soap->z_buf[5] << 8) | ((uLong)(unsigned char)soap->z_buf[6] << 16) | ((uLong)(unsigned char)soap->z_buf[7] << 24)))
  8238. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Gzip inflate error: incorrect message length\n"));
  8239. return soap->error = SOAP_ZLIB_ERROR;
  8240. }
  8241. }
  8242. soap->zlib_in = SOAP_ZLIB_NONE;
  8243. #endif
  8244. }
  8245. #endif
  8246. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  8247. while (soap->ahead != EOF && !soap_recv_raw(soap))
  8248. ;
  8249. #ifndef WITH_NOIDREF
  8250. if (soap_resolve(soap))
  8251. return soap->error;
  8252. #endif
  8253. #ifndef WITH_LEANER
  8254. if (soap->xlist)
  8255. { if (soap->mode & SOAP_ENC_MTOM)
  8256. return soap->error = SOAP_MIME_HREF;
  8257. return soap->error = SOAP_DIME_HREF;
  8258. }
  8259. #endif
  8260. soap_free_ns(soap);
  8261. #ifndef WITH_LEANER
  8262. if (soap->fpreparefinalrecv)
  8263. return soap->error = soap->fpreparefinalrecv(soap);
  8264. #endif
  8265. return SOAP_OK;
  8266. }
  8267. #endif
  8268. /******************************************************************************/
  8269. #ifndef PALM_1
  8270. SOAP_FMAC1
  8271. void
  8272. SOAP_FMAC2
  8273. soap_free_temp(struct soap *soap)
  8274. { register struct soap_attribute *tp, *tq;
  8275. register struct Namespace *ns;
  8276. soap_free_ns(soap);
  8277. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free any remaining temp blocks\n"));
  8278. while (soap->blist)
  8279. soap_end_block(soap, NULL);
  8280. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute storage\n"));
  8281. for (tp = soap->attributes; tp; tp = tq)
  8282. { tq = tp->next;
  8283. if (tp->value)
  8284. SOAP_FREE(soap, tp->value);
  8285. SOAP_FREE(soap, tp);
  8286. }
  8287. soap->attributes = NULL;
  8288. #ifdef WITH_FAST
  8289. if (soap->labbuf)
  8290. SOAP_FREE(soap, soap->labbuf);
  8291. soap->labbuf = NULL;
  8292. soap->lablen = 0;
  8293. soap->labidx = 0;
  8294. #endif
  8295. ns = soap->local_namespaces;
  8296. if (ns)
  8297. { for (; ns->id; ns++)
  8298. { if (ns->out)
  8299. { SOAP_FREE(soap, ns->out);
  8300. ns->out = NULL;
  8301. }
  8302. }
  8303. SOAP_FREE(soap, soap->local_namespaces);
  8304. soap->local_namespaces = NULL;
  8305. }
  8306. #ifndef WITH_LEANER
  8307. while (soap->xlist)
  8308. { struct soap_xlist *xp = soap->xlist->next;
  8309. SOAP_FREE(soap, soap->xlist);
  8310. soap->xlist = xp;
  8311. }
  8312. #endif
  8313. #ifndef WITH_NOIDREF
  8314. soap_free_pht(soap);
  8315. soap_free_iht(soap);
  8316. #endif
  8317. }
  8318. #endif
  8319. /******************************************************************************/
  8320. #ifndef PALM_1
  8321. static void
  8322. soap_free_ns(struct soap *soap)
  8323. { register struct soap_nlist *np, *nq;
  8324. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free namespace stack\n"));
  8325. for (np = soap->nlist; np; np = nq)
  8326. { nq = np->next;
  8327. SOAP_FREE(soap, np);
  8328. }
  8329. soap->nlist = NULL;
  8330. }
  8331. #endif
  8332. /******************************************************************************/
  8333. #ifndef PALM_1
  8334. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8335. static void
  8336. soap_init_logs(struct soap *soap)
  8337. { int i;
  8338. for (i = 0; i < SOAP_MAXLOGS; i++)
  8339. { soap->logfile[i] = NULL;
  8340. soap->fdebug[i] = NULL;
  8341. }
  8342. }
  8343. #endif
  8344. #endif
  8345. /******************************************************************************/
  8346. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8347. SOAP_FMAC1
  8348. void
  8349. SOAP_FMAC2
  8350. soap_open_logfile(struct soap *soap, int i)
  8351. { if (soap->logfile[i])
  8352. soap->fdebug[i] = fopen(soap->logfile[i], i < 2 ? "ab" : "a");
  8353. }
  8354. #endif
  8355. /******************************************************************************/
  8356. #ifdef SOAP_DEBUG
  8357. static void
  8358. soap_close_logfile(struct soap *soap, int i)
  8359. { if (soap->fdebug[i])
  8360. { fclose(soap->fdebug[i]);
  8361. soap->fdebug[i] = NULL;
  8362. }
  8363. }
  8364. #endif
  8365. /******************************************************************************/
  8366. #ifdef SOAP_DEBUG
  8367. SOAP_FMAC1
  8368. void
  8369. SOAP_FMAC2
  8370. soap_close_logfiles(struct soap *soap)
  8371. { int i;
  8372. for (i = 0; i < SOAP_MAXLOGS; i++)
  8373. soap_close_logfile(soap, i);
  8374. }
  8375. #endif
  8376. /******************************************************************************/
  8377. #ifdef SOAP_DEBUG
  8378. static void
  8379. soap_set_logfile(struct soap *soap, int i, const char *logfile)
  8380. { const char *s;
  8381. char *t = NULL;
  8382. soap_close_logfile(soap, i);
  8383. s = soap->logfile[i];
  8384. soap->logfile[i] = logfile;
  8385. if (s)
  8386. SOAP_FREE(soap, (void*)s);
  8387. if (logfile)
  8388. if ((t = (char*)SOAP_MALLOC(soap, strlen(logfile) + 1)))
  8389. strcpy(t, logfile);
  8390. soap->logfile[i] = t;
  8391. }
  8392. #endif
  8393. /******************************************************************************/
  8394. SOAP_FMAC1
  8395. void
  8396. SOAP_FMAC2
  8397. soap_set_recv_logfile(struct soap *soap, const char *logfile)
  8398. {
  8399. #ifdef SOAP_DEBUG
  8400. soap_set_logfile(soap, SOAP_INDEX_RECV, logfile);
  8401. #endif
  8402. }
  8403. /******************************************************************************/
  8404. SOAP_FMAC1
  8405. void
  8406. SOAP_FMAC2
  8407. soap_set_sent_logfile(struct soap *soap, const char *logfile)
  8408. {
  8409. #ifdef SOAP_DEBUG
  8410. soap_set_logfile(soap, SOAP_INDEX_SENT, logfile);
  8411. #endif
  8412. }
  8413. /******************************************************************************/
  8414. SOAP_FMAC1
  8415. void
  8416. SOAP_FMAC2
  8417. soap_set_test_logfile(struct soap *soap, const char *logfile)
  8418. {
  8419. #ifdef SOAP_DEBUG
  8420. soap_set_logfile(soap, SOAP_INDEX_TEST, logfile);
  8421. #endif
  8422. }
  8423. /******************************************************************************/
  8424. #ifndef PALM_1
  8425. SOAP_FMAC1
  8426. struct soap*
  8427. SOAP_FMAC2
  8428. soap_copy(const struct soap *soap)
  8429. { return soap_copy_context((struct soap*)malloc(sizeof(struct soap)), soap);
  8430. }
  8431. #endif
  8432. /******************************************************************************/
  8433. #ifndef PALM_1
  8434. SOAP_FMAC1
  8435. struct soap*
  8436. SOAP_FMAC2
  8437. soap_copy_context(struct soap *copy, const struct soap *soap)
  8438. { if (copy == soap)
  8439. return copy;
  8440. if (soap_check_state(soap))
  8441. return NULL;
  8442. if (copy)
  8443. { register struct soap_plugin *p = NULL;
  8444. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying context\n"));
  8445. #ifdef __cplusplus
  8446. *copy = *soap;
  8447. #else
  8448. memcpy(copy, soap, sizeof(struct soap));
  8449. #endif
  8450. copy->state = SOAP_COPY;
  8451. copy->error = SOAP_OK;
  8452. copy->userid = NULL;
  8453. copy->passwd = NULL;
  8454. #ifdef WITH_NTLM
  8455. copy->ntlm_challenge = NULL;
  8456. #endif
  8457. copy->nlist = NULL;
  8458. copy->blist = NULL;
  8459. copy->clist = NULL;
  8460. copy->alist = NULL;
  8461. copy->attributes = NULL;
  8462. copy->labbuf = NULL;
  8463. copy->lablen = 0;
  8464. copy->labidx = 0;
  8465. #ifdef SOAP_MEM_DEBUG
  8466. soap_init_mht(copy);
  8467. #endif
  8468. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8469. soap_init_logs(copy);
  8470. #endif
  8471. #ifdef SOAP_DEBUG
  8472. soap_set_test_logfile(copy, soap->logfile[SOAP_INDEX_TEST]);
  8473. soap_set_sent_logfile(copy, soap->logfile[SOAP_INDEX_SENT]);
  8474. soap_set_recv_logfile(copy, soap->logfile[SOAP_INDEX_RECV]);
  8475. #endif
  8476. copy->namespaces = soap->local_namespaces;
  8477. copy->local_namespaces = NULL;
  8478. soap_set_local_namespaces(copy); /* copy content of soap->local_namespaces */
  8479. copy->namespaces = soap->namespaces; /* point to shared read-only namespaces table */
  8480. #ifdef WITH_C_LOCALE
  8481. # ifdef WIN32
  8482. copy->c_locale = _create_locale(LC_ALL, "C");
  8483. # else
  8484. copy->c_locale = duplocale(soap->c_locale);
  8485. # endif
  8486. #else
  8487. copy->c_locale = NULL;
  8488. #endif
  8489. #ifdef WITH_OPENSSL
  8490. copy->bio = NULL;
  8491. copy->ssl = NULL;
  8492. copy->session = NULL;
  8493. #endif
  8494. #ifdef WITH_GNUTLS
  8495. copy->session = NULL;
  8496. #endif
  8497. #ifdef WITH_ZLIB
  8498. copy->d_stream = (z_stream*)SOAP_MALLOC(copy, sizeof(z_stream));
  8499. copy->d_stream->zalloc = Z_NULL;
  8500. copy->d_stream->zfree = Z_NULL;
  8501. copy->d_stream->opaque = Z_NULL;
  8502. copy->z_buf = NULL;
  8503. #endif
  8504. #ifndef WITH_NOIDREF
  8505. soap_init_iht(copy);
  8506. soap_init_pht(copy);
  8507. #endif
  8508. copy->header = NULL;
  8509. copy->fault = NULL;
  8510. copy->action = NULL;
  8511. #ifndef WITH_LEAN
  8512. #ifdef WITH_COOKIES
  8513. copy->cookies = soap_copy_cookies(copy, soap);
  8514. #else
  8515. copy->cookies = NULL;
  8516. #endif
  8517. #endif
  8518. copy->plugins = NULL;
  8519. for (p = soap->plugins; p; p = p->next)
  8520. { register struct soap_plugin *q = (struct soap_plugin*)SOAP_MALLOC(copy, sizeof(struct soap_plugin));
  8521. if (!q)
  8522. return NULL;
  8523. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying plugin '%s'\n", p->id));
  8524. *q = *p;
  8525. if (p->fcopy && p->fcopy(copy, q, p))
  8526. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not copy plugin '%s'\n", p->id));
  8527. SOAP_FREE(copy, q);
  8528. return NULL;
  8529. }
  8530. q->next = copy->plugins;
  8531. copy->plugins = q;
  8532. }
  8533. }
  8534. return copy;
  8535. }
  8536. #endif
  8537. /******************************************************************************/
  8538. #ifndef PALM_1
  8539. SOAP_FMAC1
  8540. void
  8541. SOAP_FMAC2
  8542. soap_copy_stream(struct soap *copy, struct soap *soap)
  8543. { struct soap_attribute *tp = NULL, *tq;
  8544. if (copy == soap)
  8545. return;
  8546. copy->header = soap->header;
  8547. copy->mode = soap->mode;
  8548. copy->imode = soap->imode;
  8549. copy->omode = soap->omode;
  8550. copy->master = soap->master;
  8551. copy->socket = soap->socket;
  8552. copy->sendsk = soap->sendsk;
  8553. copy->recvsk = soap->recvsk;
  8554. copy->recv_timeout = soap->recv_timeout;
  8555. copy->send_timeout = soap->send_timeout;
  8556. #if defined(__cplusplus) && !defined(WITH_LEAN)
  8557. copy->os = soap->os;
  8558. copy->is = soap->is;
  8559. #endif
  8560. copy->sendfd = soap->sendfd;
  8561. copy->recvfd = soap->recvfd;
  8562. copy->bufidx = soap->bufidx;
  8563. copy->buflen = soap->buflen;
  8564. copy->ahead = soap->ahead;
  8565. copy->cdata = soap->cdata;
  8566. copy->chunksize = soap->chunksize;
  8567. copy->chunkbuflen = soap->chunkbuflen;
  8568. copy->keep_alive = soap->keep_alive;
  8569. copy->tcp_keep_alive = soap->tcp_keep_alive;
  8570. copy->tcp_keep_idle = soap->tcp_keep_idle;
  8571. copy->tcp_keep_intvl = soap->tcp_keep_intvl;
  8572. copy->tcp_keep_cnt = soap->tcp_keep_cnt;
  8573. copy->max_keep_alive = soap->max_keep_alive;
  8574. #ifndef WITH_NOIO
  8575. copy->peer = soap->peer;
  8576. copy->peerlen = soap->peerlen;
  8577. copy->ip = soap->ip;
  8578. copy->port = soap->port;
  8579. memcpy(copy->host, soap->host, sizeof(soap->host));
  8580. memcpy(copy->endpoint, soap->endpoint, sizeof(soap->endpoint));
  8581. #endif
  8582. #ifdef WITH_OPENSSL
  8583. copy->bio = soap->bio;
  8584. copy->ctx = soap->ctx;
  8585. copy->ssl = soap->ssl;
  8586. #endif
  8587. #ifdef WITH_GNUTLS
  8588. copy->session = soap->session;
  8589. #endif
  8590. #ifdef WITH_ZLIB
  8591. copy->zlib_state = soap->zlib_state;
  8592. copy->zlib_in = soap->zlib_in;
  8593. copy->zlib_out = soap->zlib_out;
  8594. if (!copy->d_stream)
  8595. copy->d_stream = (z_stream*)SOAP_MALLOC(copy, sizeof(z_stream));
  8596. if (copy->d_stream)
  8597. memcpy(copy->d_stream, soap->d_stream, sizeof(z_stream));
  8598. copy->z_crc = soap->z_crc;
  8599. copy->z_ratio_in = soap->z_ratio_in;
  8600. copy->z_ratio_out = soap->z_ratio_out;
  8601. copy->z_buf = NULL;
  8602. copy->z_buflen = soap->z_buflen;
  8603. copy->z_level = soap->z_level;
  8604. if (soap->z_buf && soap->zlib_state != SOAP_ZLIB_NONE)
  8605. { copy->z_buf = (char*)SOAP_MALLOC(copy, SOAP_BUFLEN);
  8606. if (copy->z_buf)
  8607. memcpy(copy->z_buf, soap->z_buf, SOAP_BUFLEN);
  8608. }
  8609. copy->z_dict = soap->z_dict;
  8610. copy->z_dict_len = soap->z_dict_len;
  8611. #endif
  8612. memcpy(copy->buf, soap->buf, sizeof(soap->buf));
  8613. /* copy XML parser state */
  8614. soap_free_ns(copy);
  8615. soap_set_local_namespaces(copy);
  8616. copy->version = soap->version;
  8617. if (soap->nlist && soap->local_namespaces)
  8618. { register struct soap_nlist *np = NULL, *nq;
  8619. /* copy reversed nlist */
  8620. for (nq = soap->nlist; nq; nq = nq->next)
  8621. { register struct soap_nlist *nr = np;
  8622. size_t n = sizeof(struct soap_nlist) + strlen(nq->id);
  8623. np = (struct soap_nlist*)SOAP_MALLOC(copy, n);
  8624. if (!np)
  8625. break;
  8626. memcpy(np, nq, n);
  8627. np->next = nr;
  8628. }
  8629. while (np)
  8630. { register const char *s = np->ns;
  8631. copy->level = np->level; /* preserve element nesting level */
  8632. if (!s && np->index >= 0)
  8633. { s = soap->local_namespaces[np->index].out;
  8634. if (!s)
  8635. s = soap->local_namespaces[np->index].ns;
  8636. }
  8637. if (s && soap_push_namespace(copy, np->id, s) == NULL)
  8638. break;
  8639. nq = np;
  8640. np = np->next;
  8641. SOAP_FREE(copy, nq);
  8642. }
  8643. }
  8644. memcpy(copy->tag, soap->tag, sizeof(copy->tag));
  8645. memcpy(copy->id, soap->id, sizeof(copy->id));
  8646. memcpy(copy->href, soap->href, sizeof(copy->href));
  8647. memcpy(copy->type, soap->type, sizeof(copy->type));
  8648. copy->other = soap->other;
  8649. copy->root = soap->root;
  8650. copy->null = soap->null;
  8651. copy->body = soap->body;
  8652. copy->part = soap->part;
  8653. copy->mustUnderstand = soap->mustUnderstand;
  8654. copy->level = soap->level;
  8655. copy->peeked = soap->peeked;
  8656. /* copy attributes */
  8657. for (tq = soap->attributes; tq; tq = tq->next)
  8658. { struct soap_attribute *tr = tp;
  8659. size_t n = sizeof(struct soap_attribute) + strlen(tq->name);
  8660. tp = (struct soap_attribute*)SOAP_MALLOC(copy, n);
  8661. memcpy(tp, tq, n);
  8662. if (tp->size)
  8663. { tp->value = (char*)SOAP_MALLOC(copy, tp->size);
  8664. if (tp->value)
  8665. strcpy(tp->value, tq->value);
  8666. }
  8667. tp->ns = NULL;
  8668. tp->next = tr;
  8669. }
  8670. copy->attributes = tp;
  8671. }
  8672. #endif
  8673. /******************************************************************************/
  8674. #ifndef PALM_1
  8675. SOAP_FMAC1
  8676. void
  8677. SOAP_FMAC2
  8678. soap_free_stream(struct soap *soap)
  8679. { soap->socket = SOAP_INVALID_SOCKET;
  8680. soap->sendsk = SOAP_INVALID_SOCKET;
  8681. soap->recvsk = SOAP_INVALID_SOCKET;
  8682. #ifdef WITH_OPENSSL
  8683. soap->bio = NULL;
  8684. soap->ctx = NULL;
  8685. soap->ssl = NULL;
  8686. #endif
  8687. #ifdef WITH_GNUTLS
  8688. soap->xcred = NULL;
  8689. soap->acred = NULL;
  8690. soap->cache = NULL;
  8691. soap->session = NULL;
  8692. soap->dh_params = NULL;
  8693. soap->rsa_params = NULL;
  8694. #endif
  8695. #ifdef WITH_ZLIB
  8696. if (soap->z_buf)
  8697. SOAP_FREE(soap, soap->z_buf);
  8698. soap->z_buf = NULL;
  8699. #endif
  8700. }
  8701. #endif
  8702. /******************************************************************************/
  8703. #ifndef PALM_1
  8704. SOAP_FMAC1
  8705. void
  8706. SOAP_FMAC2
  8707. soap_initialize(struct soap *soap)
  8708. { soap_versioning(soap_init)(soap, SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);
  8709. }
  8710. #endif
  8711. /******************************************************************************/
  8712. #ifndef PALM_1
  8713. SOAP_FMAC1
  8714. void
  8715. SOAP_FMAC2
  8716. soap_versioning(soap_init)(struct soap *soap, soap_mode imode, soap_mode omode)
  8717. { size_t i;
  8718. soap->state = SOAP_INIT;
  8719. #ifdef SOAP_MEM_DEBUG
  8720. soap_init_mht(soap);
  8721. #endif
  8722. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8723. soap_init_logs(soap);
  8724. #endif
  8725. #ifdef SOAP_DEBUG
  8726. #ifdef TANDEM_NONSTOP
  8727. soap_set_test_logfile(soap, "TESTLOG");
  8728. soap_set_sent_logfile(soap, "SENTLOG");
  8729. soap_set_recv_logfile(soap, "RECVLOG");
  8730. #else
  8731. soap_set_test_logfile(soap, "TEST.log");
  8732. soap_set_sent_logfile(soap, "SENT.log");
  8733. soap_set_recv_logfile(soap, "RECV.log");
  8734. #endif
  8735. #endif
  8736. soap->version = 0;
  8737. soap_mode(soap, imode);
  8738. soap_imode(soap, imode);
  8739. soap_omode(soap, omode);
  8740. soap->plugins = NULL;
  8741. soap->user = NULL;
  8742. for (i = 0; i < sizeof(soap->data)/sizeof(*soap->data); i++)
  8743. soap->data[i] = NULL;
  8744. soap->userid = NULL;
  8745. soap->passwd = NULL;
  8746. soap->authrealm = NULL;
  8747. #ifdef WITH_NTLM
  8748. soap->ntlm_challenge = NULL;
  8749. #endif
  8750. #ifndef WITH_NOHTTP
  8751. soap->fpost = http_post;
  8752. soap->fget = http_get;
  8753. soap->fput = http_405;
  8754. soap->fdel = http_405;
  8755. soap->fopt = http_200;
  8756. soap->fhead = http_200;
  8757. soap->fform = NULL;
  8758. soap->fposthdr = http_post_header;
  8759. soap->fresponse = http_response;
  8760. soap->fparse = http_parse;
  8761. soap->fparsehdr = http_parse_header;
  8762. #endif
  8763. soap->fheader = NULL;
  8764. soap->fconnect = NULL;
  8765. soap->fdisconnect = NULL;
  8766. #ifndef WITH_NOIO
  8767. soap->ipv6_multicast_if = 0; /* in_addr_t value */
  8768. soap->ipv4_multicast_if = NULL; /* points to struct in_addr or in_addr_t */
  8769. soap->ipv4_multicast_ttl = 0; /* 0: use default */
  8770. #ifndef WITH_IPV6
  8771. soap->fresolve = tcp_gethost;
  8772. #else
  8773. soap->fresolve = NULL;
  8774. #endif
  8775. soap->faccept = tcp_accept;
  8776. soap->fopen = tcp_connect;
  8777. soap->fclose = tcp_disconnect;
  8778. soap->fclosesocket = tcp_closesocket;
  8779. soap->fshutdownsocket = tcp_shutdownsocket;
  8780. soap->fsend = fsend;
  8781. soap->frecv = frecv;
  8782. soap->fpoll = soap_poll;
  8783. #else
  8784. soap->fopen = NULL;
  8785. soap->fclose = NULL;
  8786. soap->fpoll = NULL;
  8787. #endif
  8788. soap->fseterror = NULL;
  8789. soap->fignore = NULL;
  8790. soap->fserveloop = NULL;
  8791. soap->fplugin = fplugin;
  8792. soap->fmalloc = NULL;
  8793. #ifndef WITH_LEANER
  8794. soap->feltbegin = NULL;
  8795. soap->feltendin = NULL;
  8796. soap->feltbegout = NULL;
  8797. soap->feltendout = NULL;
  8798. soap->fprepareinitsend = NULL;
  8799. soap->fprepareinitrecv = NULL;
  8800. soap->fpreparesend = NULL;
  8801. soap->fpreparerecv = NULL;
  8802. soap->fpreparefinalsend = NULL;
  8803. soap->fpreparefinalrecv = NULL;
  8804. soap->ffiltersend = NULL;
  8805. soap->ffilterrecv = NULL;
  8806. soap->fdimereadopen = NULL;
  8807. soap->fdimewriteopen = NULL;
  8808. soap->fdimereadclose = NULL;
  8809. soap->fdimewriteclose = NULL;
  8810. soap->fdimeread = NULL;
  8811. soap->fdimewrite = NULL;
  8812. soap->fmimereadopen = NULL;
  8813. soap->fmimewriteopen = NULL;
  8814. soap->fmimereadclose = NULL;
  8815. soap->fmimewriteclose = NULL;
  8816. soap->fmimeread = NULL;
  8817. soap->fmimewrite = NULL;
  8818. #endif
  8819. soap->float_format = "%.9G"; /* Alternative: use "%G" */
  8820. soap->double_format = "%.17lG"; /* Alternative: use "%lG" */
  8821. soap->dime_id_format = "cid:id%d"; /* default DIME id format */
  8822. soap->http_version = "1.1";
  8823. soap->proxy_http_version = "1.0";
  8824. soap->http_content = NULL;
  8825. soap->actor = NULL;
  8826. soap->lang = "en";
  8827. soap->keep_alive = 0;
  8828. soap->tcp_keep_alive = 0;
  8829. soap->tcp_keep_idle = 0;
  8830. soap->tcp_keep_intvl = 0;
  8831. soap->tcp_keep_cnt = 0;
  8832. soap->max_keep_alive = SOAP_MAXKEEPALIVE;
  8833. soap->recv_timeout = 0;
  8834. soap->send_timeout = 0;
  8835. soap->connect_timeout = 0;
  8836. soap->accept_timeout = 0;
  8837. soap->socket_flags = 0;
  8838. soap->connect_flags = 0;
  8839. soap->bind_flags = 0;
  8840. soap->accept_flags = 0;
  8841. soap->linger_time = 0;
  8842. soap->ip = 0;
  8843. soap->labbuf = NULL;
  8844. soap->lablen = 0;
  8845. soap->labidx = 0;
  8846. soap->encodingStyle = NULL;
  8847. #ifndef WITH_NONAMESPACES
  8848. soap->namespaces = namespaces;
  8849. #else
  8850. soap->namespaces = NULL;
  8851. #endif
  8852. soap->local_namespaces = NULL;
  8853. soap->nlist = NULL;
  8854. soap->blist = NULL;
  8855. soap->clist = NULL;
  8856. soap->alist = NULL;
  8857. soap->attributes = NULL;
  8858. soap->header = NULL;
  8859. soap->fault = NULL;
  8860. soap->master = SOAP_INVALID_SOCKET;
  8861. soap->socket = SOAP_INVALID_SOCKET;
  8862. soap->sendsk = SOAP_INVALID_SOCKET;
  8863. soap->recvsk = SOAP_INVALID_SOCKET;
  8864. soap->os = NULL;
  8865. soap->is = NULL;
  8866. #ifndef WITH_LEANER
  8867. soap->dom = NULL;
  8868. soap->dime.list = NULL;
  8869. soap->dime.first = NULL;
  8870. soap->dime.last = NULL;
  8871. soap->mime.list = NULL;
  8872. soap->mime.first = NULL;
  8873. soap->mime.last = NULL;
  8874. soap->mime.boundary = NULL;
  8875. soap->mime.start = NULL;
  8876. soap->xlist = NULL;
  8877. #endif
  8878. #ifndef UNDER_CE
  8879. soap->recvfd = 0;
  8880. soap->sendfd = 1;
  8881. #else
  8882. soap->recvfd = stdin;
  8883. soap->sendfd = stdout;
  8884. #endif
  8885. soap->host[0] = '\0';
  8886. soap->port = 0;
  8887. soap->action = NULL;
  8888. soap->proxy_host = NULL;
  8889. soap->proxy_port = 8080;
  8890. soap->proxy_userid = NULL;
  8891. soap->proxy_passwd = NULL;
  8892. soap->prolog = NULL;
  8893. #ifdef WITH_ZLIB
  8894. soap->zlib_state = SOAP_ZLIB_NONE;
  8895. soap->zlib_in = SOAP_ZLIB_NONE;
  8896. soap->zlib_out = SOAP_ZLIB_NONE;
  8897. soap->d_stream = (z_stream*)SOAP_MALLOC(soap, sizeof(z_stream));
  8898. soap->d_stream->zalloc = Z_NULL;
  8899. soap->d_stream->zfree = Z_NULL;
  8900. soap->d_stream->opaque = Z_NULL;
  8901. soap->z_buf = NULL;
  8902. soap->z_level = 6;
  8903. soap->z_dict = NULL;
  8904. soap->z_dict_len = 0;
  8905. #endif
  8906. #ifndef WITH_LEAN
  8907. soap->wsuid = NULL;
  8908. soap->c14nexclude = NULL;
  8909. soap->cookies = NULL;
  8910. soap->cookie_domain = NULL;
  8911. soap->cookie_path = NULL;
  8912. soap->cookie_max = 32;
  8913. #endif
  8914. #ifdef WMW_RPM_IO
  8915. soap->rpmreqid = NULL;
  8916. #endif
  8917. #ifdef PALM
  8918. palmNetLibOpen();
  8919. #endif
  8920. #ifndef WITH_NOIDREF
  8921. soap_init_iht(soap);
  8922. soap_init_pht(soap);
  8923. #endif
  8924. #ifdef WITH_OPENSSL
  8925. if (!soap_ssl_init_done)
  8926. soap_ssl_init();
  8927. soap->fsslauth = ssl_auth_init;
  8928. soap->fsslverify = ssl_verify_callback;
  8929. soap->bio = NULL;
  8930. soap->ssl = NULL;
  8931. soap->ctx = NULL;
  8932. soap->session = NULL;
  8933. soap->ssl_flags = SOAP_SSL_DEFAULT;
  8934. soap->keyfile = NULL;
  8935. soap->keyid = NULL;
  8936. soap->password = NULL;
  8937. soap->cafile = NULL;
  8938. soap->capath = NULL;
  8939. soap->crlfile = NULL;
  8940. soap->dhfile = NULL;
  8941. soap->randfile = NULL;
  8942. #endif
  8943. #ifdef WITH_GNUTLS
  8944. if (!soap_ssl_init_done)
  8945. soap_ssl_init();
  8946. soap->fsslauth = ssl_auth_init;
  8947. soap->fsslverify = NULL;
  8948. soap->xcred = NULL;
  8949. soap->acred = NULL;
  8950. soap->cache = NULL;
  8951. soap->session = NULL;
  8952. soap->ssl_flags = SOAP_SSL_DEFAULT;
  8953. soap->keyfile = NULL;
  8954. soap->keyid = NULL;
  8955. soap->password = NULL;
  8956. soap->cafile = NULL;
  8957. soap->capath = NULL;
  8958. soap->crlfile = NULL;
  8959. soap->dh_params = NULL;
  8960. soap->rsa_params = NULL;
  8961. #endif
  8962. #ifdef WITH_C_LOCALE
  8963. # ifdef WIN32
  8964. soap->c_locale = _create_locale(LC_ALL, "C");
  8965. # else
  8966. soap->c_locale = newlocale(LC_ALL_MASK, "C", NULL);
  8967. # endif
  8968. #else
  8969. soap->c_locale = NULL;
  8970. #endif
  8971. soap->buflen = 0;
  8972. soap->bufidx = 0;
  8973. #ifndef WITH_LEANER
  8974. soap->dime.chunksize = 0;
  8975. soap->dime.buflen = 0;
  8976. #endif
  8977. soap->null = 0;
  8978. soap->position = 0;
  8979. soap->encoding = 0;
  8980. soap->mustUnderstand = 0;
  8981. soap->ns = 0;
  8982. soap->part = SOAP_END;
  8983. soap->event = 0;
  8984. soap->evlev = 0;
  8985. soap->alloced = 0;
  8986. soap->count = 0;
  8987. soap->length = 0;
  8988. soap->cdata = 0;
  8989. soap->peeked = 0;
  8990. soap->ahead = 0;
  8991. soap->idnum = 0;
  8992. soap->level = 0;
  8993. soap->endpoint[0] = '\0';
  8994. soap->error = SOAP_OK;
  8995. }
  8996. #endif
  8997. /******************************************************************************/
  8998. #ifndef PALM_2
  8999. SOAP_FMAC1
  9000. void
  9001. SOAP_FMAC2
  9002. soap_begin(struct soap *soap)
  9003. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reinitializing context\n"));
  9004. if (!soap->keep_alive)
  9005. { soap->buflen = 0;
  9006. soap->bufidx = 0;
  9007. }
  9008. soap->null = 0;
  9009. soap->position = 0;
  9010. soap->encoding = 0;
  9011. soap->mustUnderstand = 0;
  9012. soap->mode = 0;
  9013. soap->ns = 0;
  9014. soap->part = SOAP_END;
  9015. soap->event = 0;
  9016. soap->evlev = 0;
  9017. soap->alloced = 0;
  9018. soap->count = 0;
  9019. soap->length = 0;
  9020. soap->cdata = 0;
  9021. soap->error = SOAP_OK;
  9022. soap->peeked = 0;
  9023. soap->ahead = 0;
  9024. soap->idnum = 0;
  9025. soap->level = 0;
  9026. soap->endpoint[0] = '\0';
  9027. soap->encodingStyle = SOAP_STR_EOS;
  9028. #ifndef WITH_LEANER
  9029. soap->dime.chunksize = 0;
  9030. soap->dime.buflen = 0;
  9031. #endif
  9032. soap_free_temp(soap);
  9033. }
  9034. #endif
  9035. /******************************************************************************/
  9036. #ifndef PALM_2
  9037. SOAP_FMAC1
  9038. void
  9039. SOAP_FMAC2
  9040. soap_end(struct soap *soap)
  9041. { if (soap_check_state(soap))
  9042. return;
  9043. soap_free_temp(soap);
  9044. soap_dealloc(soap, NULL);
  9045. while (soap->clist)
  9046. { register struct soap_clist *cp = soap->clist->next;
  9047. SOAP_FREE(soap, soap->clist);
  9048. soap->clist = cp;
  9049. }
  9050. soap_closesock(soap);
  9051. #ifdef SOAP_DEBUG
  9052. soap_close_logfiles(soap);
  9053. #endif
  9054. #ifdef PALM
  9055. palmNetLibClose();
  9056. #endif
  9057. }
  9058. #endif
  9059. /******************************************************************************/
  9060. #ifndef PALM_1
  9061. SOAP_FMAC1
  9062. void
  9063. SOAP_FMAC2
  9064. soap_set_version(struct soap *soap, short version)
  9065. { soap_set_local_namespaces(soap);
  9066. if (soap->version != version)
  9067. { if (version == 1)
  9068. { soap->local_namespaces[0].ns = soap_env1;
  9069. soap->local_namespaces[1].ns = soap_enc1;
  9070. }
  9071. else if (version == 2)
  9072. { soap->local_namespaces[0].ns = soap_env2;
  9073. soap->local_namespaces[1].ns = soap_enc2;
  9074. }
  9075. soap->version = version;
  9076. }
  9077. }
  9078. #endif
  9079. /******************************************************************************/
  9080. #ifndef PALM_1
  9081. SOAP_FMAC1
  9082. int
  9083. SOAP_FMAC2
  9084. soap_set_namespaces(struct soap *soap, const struct Namespace *p)
  9085. { register struct Namespace *ns = soap->local_namespaces;
  9086. register struct soap_nlist *np, *nq, *nr;
  9087. register unsigned int level = soap->level;
  9088. soap->namespaces = p;
  9089. soap->local_namespaces = NULL;
  9090. soap_set_local_namespaces(soap);
  9091. /* reverse the namespace list */
  9092. np = soap->nlist;
  9093. soap->nlist = NULL;
  9094. if (np)
  9095. { nq = np->next;
  9096. np->next = NULL;
  9097. while (nq)
  9098. { nr = nq->next;
  9099. nq->next = np;
  9100. np = nq;
  9101. nq = nr;
  9102. }
  9103. }
  9104. /* then push on new stack */
  9105. while (np)
  9106. { register const char *s;
  9107. soap->level = np->level; /* preserve element nesting level */
  9108. s = np->ns;
  9109. if (!s && np->index >= 0 && ns)
  9110. { s = ns[np->index].out;
  9111. if (!s)
  9112. s = ns[np->index].ns;
  9113. }
  9114. if (s && soap_push_namespace(soap, np->id, s) == NULL)
  9115. return soap->error;
  9116. nq = np;
  9117. np = np->next;
  9118. SOAP_FREE(soap, nq);
  9119. }
  9120. if (ns)
  9121. { register int i;
  9122. for (i = 0; ns[i].id; i++)
  9123. { if (ns[i].out)
  9124. { SOAP_FREE(soap, ns[i].out);
  9125. ns[i].out = NULL;
  9126. }
  9127. }
  9128. SOAP_FREE(soap, ns);
  9129. }
  9130. soap->level = level; /* restore level */
  9131. return SOAP_OK;
  9132. }
  9133. #endif
  9134. /******************************************************************************/
  9135. #ifndef PALM_1
  9136. SOAP_FMAC1
  9137. void
  9138. SOAP_FMAC2
  9139. soap_set_local_namespaces(struct soap *soap)
  9140. { if (soap->namespaces && !soap->local_namespaces)
  9141. { register const struct Namespace *ns1;
  9142. register struct Namespace *ns2;
  9143. register size_t n = 1;
  9144. for (ns1 = soap->namespaces; ns1->id; ns1++)
  9145. n++;
  9146. n *= sizeof(struct Namespace);
  9147. ns2 = (struct Namespace*)SOAP_MALLOC(soap, n);
  9148. if (ns2)
  9149. { memcpy(ns2, soap->namespaces, n);
  9150. if (ns2[0].ns)
  9151. { if (!strcmp(ns2[0].ns, soap_env1))
  9152. soap->version = 1;
  9153. else if (!strcmp(ns2[0].ns, soap_env2))
  9154. soap->version = 2;
  9155. }
  9156. soap->local_namespaces = ns2;
  9157. for (; ns2->id; ns2++)
  9158. ns2->out = NULL;
  9159. }
  9160. }
  9161. }
  9162. #endif
  9163. /******************************************************************************/
  9164. #ifndef WITH_LEAN
  9165. #ifndef PALM_1
  9166. SOAP_FMAC1
  9167. const char *
  9168. SOAP_FMAC2
  9169. soap_tagsearch(const char *big, const char *little)
  9170. { if (little)
  9171. { register size_t n = strlen(little);
  9172. register const char *s = big;
  9173. while (s)
  9174. { register const char *t = s;
  9175. register size_t i;
  9176. for (i = 0; i < n; i++, t++)
  9177. { if (*t != little[i])
  9178. break;
  9179. }
  9180. if (*t == '\0' || *t == ' ')
  9181. { if (i == n || (i && little[i-1] == ':'))
  9182. return s;
  9183. }
  9184. s = strchr(t, ' ');
  9185. if (s)
  9186. s++;
  9187. }
  9188. }
  9189. return NULL;
  9190. }
  9191. #endif
  9192. #endif
  9193. /******************************************************************************/
  9194. #ifndef WITH_LEAN
  9195. #ifndef PALM_1
  9196. SOAP_FMAC1
  9197. struct soap_nlist *
  9198. SOAP_FMAC2
  9199. soap_lookup_ns(struct soap *soap, const char *tag, size_t n)
  9200. { register struct soap_nlist *np;
  9201. for (np = soap->nlist; np; np = np->next)
  9202. { if (!strncmp(np->id, tag, n) && !np->id[n])
  9203. return np;
  9204. }
  9205. return NULL;
  9206. }
  9207. #endif
  9208. #endif
  9209. /******************************************************************************/
  9210. #ifndef WITH_LEAN
  9211. static struct soap_nlist *
  9212. soap_push_ns(struct soap *soap, const char *id, const char *ns, short utilized)
  9213. { register struct soap_nlist *np;
  9214. size_t n, k;
  9215. if (soap_tagsearch(soap->c14nexclude, id))
  9216. return NULL;
  9217. if (!utilized)
  9218. { for (np = soap->nlist; np; np = np->next)
  9219. { if (!strcmp(np->id, id) && (!np->ns || !strcmp(np->ns, ns)))
  9220. break;
  9221. }
  9222. if (np)
  9223. { if ((np->level < soap->level || !np->ns) && np->index == 1)
  9224. utilized = 1;
  9225. else
  9226. return NULL;
  9227. }
  9228. }
  9229. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Adding namespace binding (level=%u) '%s' '%s' utilized=%d\n", soap->level, id, ns ? ns : "(null)", utilized));
  9230. n = strlen(id);
  9231. if (ns)
  9232. k = strlen(ns);
  9233. else
  9234. k = 0;
  9235. np = (struct soap_nlist*)SOAP_MALLOC(soap, sizeof(struct soap_nlist) + n + k + 1);
  9236. if (!np)
  9237. { soap->error = SOAP_EOM;
  9238. return NULL;
  9239. }
  9240. np->next = soap->nlist;
  9241. soap->nlist = np;
  9242. strcpy((char*)np->id, id);
  9243. if (ns)
  9244. np->ns = strcpy((char*)np->id + n + 1, ns);
  9245. else
  9246. np->ns = NULL;
  9247. np->level = soap->level;
  9248. np->index = utilized;
  9249. return np;
  9250. }
  9251. #endif
  9252. /******************************************************************************/
  9253. #ifndef WITH_LEAN
  9254. static void
  9255. soap_utilize_ns(struct soap *soap, const char *tag)
  9256. { register struct soap_nlist *np;
  9257. size_t n = 0;
  9258. const char *t = strchr(tag, ':');
  9259. if (t)
  9260. { n = t - tag;
  9261. if (n >= sizeof(soap->tmpbuf))
  9262. n = sizeof(soap->tmpbuf) - 1;
  9263. }
  9264. np = soap_lookup_ns(soap, tag, n);
  9265. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Utilizing namespace of '%s'\n", tag));
  9266. if (np)
  9267. { if (np->index <= 0)
  9268. soap_push_ns(soap, np->id, np->ns, 1);
  9269. }
  9270. else if (strncmp(tag, "xml", 3))
  9271. { strncpy(soap->tmpbuf, tag, n);
  9272. soap->tmpbuf[n] = '\0';
  9273. soap_push_ns(soap, soap->tmpbuf, NULL, 1);
  9274. }
  9275. }
  9276. #endif
  9277. /******************************************************************************/
  9278. #ifndef PALM_2
  9279. SOAP_FMAC1
  9280. int
  9281. SOAP_FMAC2
  9282. soap_element(struct soap *soap, const char *tag, int id, const char *type)
  9283. {
  9284. #ifndef WITH_LEAN
  9285. register const char *s;
  9286. #endif
  9287. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element begin tag='%s' level='%u' id='%d' type='%s'\n", tag, soap->level, id, type ? type : SOAP_STR_EOS));
  9288. soap->level++;
  9289. #ifdef WITH_DOM
  9290. #ifndef WITH_LEAN
  9291. if (soap->wsuid && soap_tagsearch(soap->wsuid, tag))
  9292. { size_t i;
  9293. for (s = tag, i = 0; *s && i < sizeof(soap->tag) - 1; s++, i++)
  9294. soap->tag[i] = *s == ':' ? '-' : *s;
  9295. soap->tag[i] = '\0';
  9296. if (soap_set_attr(soap, "wsu:Id", soap->tag, 1))
  9297. return soap->error;
  9298. }
  9299. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_DOM_ASIS))
  9300. { if (soap->evlev >= soap->level)
  9301. soap->evlev = 0;
  9302. if (soap->event == SOAP_SEC_BEGIN && !soap->evlev)
  9303. { register struct soap_nlist *np;
  9304. /* non-nested wsu:Id found: clear xmlns, re-emit them for exc-c14n */
  9305. for (np = soap->nlist; np; np = np->next)
  9306. { if (np->index == 2)
  9307. { struct soap_nlist *np1 = soap_push_ns(soap, np->id, np->ns, 1);
  9308. if (np1)
  9309. np1->index = 0;
  9310. }
  9311. }
  9312. soap->evlev = soap->level;
  9313. }
  9314. }
  9315. #endif
  9316. if (soap->mode & SOAP_XML_DOM)
  9317. { register struct soap_dom_element *elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  9318. if (!elt)
  9319. return soap->error;
  9320. elt->soap = soap;
  9321. elt->next = NULL;
  9322. elt->prnt = soap->dom;
  9323. elt->name = soap_strdup(soap, tag);
  9324. elt->elts = NULL;
  9325. elt->atts = NULL;
  9326. elt->nstr = NULL;
  9327. elt->data = NULL;
  9328. elt->wide = NULL;
  9329. elt->node = NULL;
  9330. elt->type = 0;
  9331. elt->head = NULL;
  9332. elt->tail = NULL;
  9333. if (soap->dom)
  9334. { struct soap_dom_element *p = soap->dom->elts;
  9335. if (p)
  9336. { while (p->next)
  9337. p = p->next;
  9338. p->next = elt;
  9339. }
  9340. else
  9341. soap->dom->elts = elt;
  9342. }
  9343. soap->dom = elt;
  9344. }
  9345. else
  9346. {
  9347. #endif
  9348. #ifndef WITH_LEAN
  9349. if (!soap->ns)
  9350. { if (!(soap->mode & SOAP_XML_CANONICAL)
  9351. && soap_send(soap, soap->prolog ? soap->prolog : "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))
  9352. return soap->error;
  9353. }
  9354. else if (soap->mode & SOAP_XML_INDENT)
  9355. { if (soap->ns == 1 && soap_send_raw(soap, soap_indent, soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  9356. return soap->error;
  9357. soap->body = 1;
  9358. }
  9359. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tag, ':')))
  9360. { struct Namespace *ns = soap->local_namespaces;
  9361. size_t n = s - tag;
  9362. if (soap_send_raw(soap, "<", 1)
  9363. || soap_send(soap, s + 1))
  9364. return soap->error;
  9365. if (soap->nlist && !strncmp(soap->nlist->id, tag, n) && !soap->nlist->id[n])
  9366. ns = NULL;
  9367. for (; ns && ns->id; ns++)
  9368. //{ if (*ns->id && (ns->out || ns->ns) && !strncmp(ns->id, tag, n) && !ns->id[n]) // Gagua
  9369. { if (ns->id && (ns->out || ns->ns) && !strncmp(ns->id, tag, n) && !ns->id[n])
  9370. { soap_push_ns(soap, ns->id, ns->out ? ns->out : ns->ns, 0);
  9371. if (soap_attribute(soap, "xmlns", ns->out ? ns->out : ns->ns))
  9372. return soap->error;
  9373. break;
  9374. }
  9375. }
  9376. }
  9377. else
  9378. #endif
  9379. if (soap_send_raw(soap, "<", 1)
  9380. || soap_send(soap, tag))
  9381. return soap->error;
  9382. #ifdef WITH_DOM
  9383. }
  9384. #endif
  9385. if (!soap->ns)
  9386. { struct Namespace *ns = soap->local_namespaces;
  9387. int k = -1;
  9388. if (ns)
  9389. {
  9390. #ifndef WITH_LEAN
  9391. if ((soap->mode & SOAP_XML_DEFAULTNS))
  9392. { if (soap->version)
  9393. k = 4; /* first four required entries */
  9394. else if (!(soap->mode & SOAP_XML_NOTYPE) || (soap->mode & SOAP_XML_NIL))
  9395. { ns += 2;
  9396. k = 2; /* next two entries */
  9397. }
  9398. else
  9399. k = 0; /* no entries */
  9400. }
  9401. #endif
  9402. while (k-- && ns->id)
  9403. //{ if (*ns->id && (ns->out || ns->ns)) // Gagua
  9404. { if (ns->id && (ns->out || ns->ns))
  9405. {
  9406. #ifdef HAVE_SNPRINTF
  9407. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns:%s", ns->id);
  9408. #else
  9409. sprintf(soap->tmpbuf, "xmlns:%s", ns->id);
  9410. #endif
  9411. if (soap_attribute(soap, soap->tmpbuf, ns->out ? ns->out : ns->ns))
  9412. return soap->error;
  9413. }
  9414. ns++;
  9415. }
  9416. }
  9417. }
  9418. soap->ns = 1; /* namespace table control: ns = 0 or 2 to start, then 1 to stop dumping the table */
  9419. #ifndef WITH_LEAN
  9420. if (soap->mode & SOAP_XML_CANONICAL)
  9421. soap_utilize_ns(soap, tag);
  9422. #endif
  9423. if (id > 0)
  9424. {
  9425. #ifdef HAVE_SNPRINTF
  9426. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "_%d", id);
  9427. #else
  9428. sprintf(soap->tmpbuf, "_%d", id);
  9429. #endif
  9430. if (soap->version == 2)
  9431. { if (soap_attribute(soap, "SOAP-ENC:id", soap->tmpbuf))
  9432. return soap->error;
  9433. }
  9434. else if (soap_attribute(soap, "id", soap->tmpbuf))
  9435. return soap->error;
  9436. }
  9437. if (type && *type && !(soap->mode & SOAP_XML_NOTYPE) && soap->part != SOAP_IN_HEADER)
  9438. { const char *t = type;
  9439. #ifndef WITH_LEAN
  9440. if (soap->mode & SOAP_XML_DEFAULTNS)
  9441. { t = strchr(type, ':');
  9442. if (t)
  9443. t++;
  9444. else
  9445. t = type;
  9446. }
  9447. #endif
  9448. if (soap->attributes ? soap_set_attr(soap, "xsi:type", t, 1) : soap_attribute(soap, "xsi:type", t))
  9449. return soap->error;
  9450. #ifndef WITH_LEAN
  9451. if (soap->mode & SOAP_XML_CANONICAL)
  9452. soap_utilize_ns(soap, type);
  9453. #endif
  9454. }
  9455. if (soap->null && soap->position > 0)
  9456. { register int i;
  9457. #ifdef HAVE_SNPRINTF
  9458. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf) - 1, "[%d", soap->positions[0]);
  9459. #else
  9460. sprintf(soap->tmpbuf, "[%d", soap->positions[0]);
  9461. #endif
  9462. for (i = 1; i < soap->position; i++)
  9463. { register size_t l = strlen(soap->tmpbuf);
  9464. #ifdef HAVE_SNPRINTF
  9465. soap_snprintf(soap->tmpbuf + l, sizeof(soap->tmpbuf)-l-1, ",%d", soap->positions[i]);
  9466. #else
  9467. if (l + 13 < sizeof(soap->tmpbuf))
  9468. sprintf(soap->tmpbuf + l, ",%d", soap->positions[i]);
  9469. #endif
  9470. }
  9471. strcat(soap->tmpbuf, "]");
  9472. if (soap_attribute(soap, "SOAP-ENC:position", soap->tmpbuf))
  9473. return soap->error;
  9474. }
  9475. if (soap->mustUnderstand)
  9476. { if (soap->actor && *soap->actor)
  9477. { if (soap_attribute(soap, soap->version == 2 ? "SOAP-ENV:role" : "SOAP-ENV:actor", soap->actor))
  9478. return soap->error;
  9479. }
  9480. if (soap_attribute(soap, "SOAP-ENV:mustUnderstand", soap->version == 2 ? "true" : "1"))
  9481. return soap->error;
  9482. soap->mustUnderstand = 0;
  9483. }
  9484. if (soap->encoding)
  9485. { if (soap->encodingStyle && soap->local_namespaces && soap->local_namespaces[0].id && soap->local_namespaces[1].id)
  9486. { if (!*soap->encodingStyle)
  9487. { if (soap->local_namespaces[1].out)
  9488. soap->encodingStyle = soap->local_namespaces[1].out;
  9489. else
  9490. soap->encodingStyle = soap->local_namespaces[1].ns;
  9491. }
  9492. if (soap->encodingStyle && soap_attribute(soap, "SOAP-ENV:encodingStyle", soap->encodingStyle))
  9493. return soap->error;
  9494. }
  9495. else
  9496. soap->encodingStyle = NULL;
  9497. soap->encoding = 0;
  9498. }
  9499. soap->null = 0;
  9500. soap->position = 0;
  9501. if (soap->event == SOAP_SEC_BEGIN)
  9502. soap->event = 0;
  9503. return SOAP_OK;
  9504. }
  9505. #endif
  9506. /******************************************************************************/
  9507. #ifndef PALM_2
  9508. SOAP_FMAC1
  9509. int
  9510. SOAP_FMAC2
  9511. soap_element_begin_out(struct soap *soap, const char *tag, int id, const char *type)
  9512. { if (*tag == '-')
  9513. return SOAP_OK;
  9514. if (soap_element(soap, tag, id, type))
  9515. return soap->error;
  9516. #ifdef WITH_DOM
  9517. if (soap_element_start_end_out(soap, NULL))
  9518. return soap->error;
  9519. if (soap->feltbegout)
  9520. return soap->error = soap->feltbegout(soap, tag);
  9521. return SOAP_OK;
  9522. #else
  9523. return soap_element_start_end_out(soap, NULL);
  9524. #endif
  9525. }
  9526. #endif
  9527. /******************************************************************************/
  9528. #ifndef PALM_2
  9529. #ifndef HAVE_STRRCHR
  9530. SOAP_FMAC1
  9531. char*
  9532. SOAP_FMAC2
  9533. soap_strrchr(const char *s, int t)
  9534. { register char *r = NULL;
  9535. while (*s)
  9536. if (*s++ == t)
  9537. r = (char*)s - 1;
  9538. return r;
  9539. }
  9540. #endif
  9541. #endif
  9542. /******************************************************************************/
  9543. #ifndef PALM_2
  9544. #ifndef HAVE_STRTOL
  9545. SOAP_FMAC1
  9546. long
  9547. SOAP_FMAC2
  9548. soap_strtol(const char *s, char **t, int b)
  9549. { register long n = 0;
  9550. register int c;
  9551. while (*s > 0 && *s <= 32)
  9552. s++;
  9553. if (b == 10)
  9554. { short neg = 0;
  9555. if (*s == '-')
  9556. { s++;
  9557. neg = 1;
  9558. }
  9559. else if (*s == '+')
  9560. s++;
  9561. while ((c = *s) && c >= '0' && c <= '9')
  9562. { if (n >= 214748364 && (n > 214748364 || c >= '8'))
  9563. break;
  9564. n *= 10;
  9565. n += c - '0';
  9566. s++;
  9567. }
  9568. if (neg)
  9569. n = -n;
  9570. }
  9571. else /* assume b == 16 and value is always positive */
  9572. { while ((c = *s))
  9573. { if (c >= '0' && c <= '9')
  9574. c -= '0';
  9575. else if (c >= 'A' && c <= 'F')
  9576. c -= 'A' - 10;
  9577. else if (c >= 'a' && c <= 'f')
  9578. c -= 'a' - 10;
  9579. if (n > 0x07FFFFFF)
  9580. break;
  9581. n <<= 4;
  9582. n += c;
  9583. s++;
  9584. }
  9585. }
  9586. if (t)
  9587. *t = (char*)s;
  9588. return n;
  9589. }
  9590. #endif
  9591. #endif
  9592. /******************************************************************************/
  9593. #ifndef PALM_2
  9594. #ifndef HAVE_STRTOUL
  9595. SOAP_FMAC1
  9596. unsigned long
  9597. SOAP_FMAC2
  9598. soap_strtoul(const char *s, char **t, int b)
  9599. { unsigned long n = 0;
  9600. register int c;
  9601. while (*s > 0 && *s <= 32)
  9602. s++;
  9603. if (b == 10)
  9604. { if (*s == '+')
  9605. s++;
  9606. while ((c = *s) && c >= '0' && c <= '9')
  9607. { if (n >= 429496729 && (n > 429496729 || c >= '6'))
  9608. break;
  9609. n *= 10;
  9610. n += c - '0';
  9611. s++;
  9612. }
  9613. }
  9614. else /* b == 16 */
  9615. { while ((c = *s))
  9616. { if (c >= '0' && c <= '9')
  9617. c -= '0';
  9618. else if (c >= 'A' && c <= 'F')
  9619. c -= 'A' - 10;
  9620. else if (c >= 'a' && c <= 'f')
  9621. c -= 'a' - 10;
  9622. if (n > 0x0FFFFFFF)
  9623. break;
  9624. n <<= 4;
  9625. n += c;
  9626. s++;
  9627. }
  9628. }
  9629. if (t)
  9630. *t = (char*)s;
  9631. return n;
  9632. }
  9633. #endif
  9634. #endif
  9635. /******************************************************************************/
  9636. #ifndef PALM_1
  9637. SOAP_FMAC1
  9638. int
  9639. SOAP_FMAC2
  9640. soap_array_begin_out(struct soap *soap, const char *tag, int id, const char *type, const char *offset)
  9641. { if (!type || !*type)
  9642. return soap_element_begin_out(soap, tag, id, NULL);
  9643. if (soap_element(soap, tag, id, "SOAP-ENC:Array"))
  9644. return soap->error;
  9645. if (soap->version == 2)
  9646. { const char *s;
  9647. s = soap_strrchr(type, '[');
  9648. if (s && (size_t)(s - type) < sizeof(soap->tmpbuf))
  9649. { strncpy(soap->tmpbuf, type, s - type);
  9650. soap->tmpbuf[s - type] = '\0';
  9651. if (soap_attribute(soap, "SOAP-ENC:itemType", soap->tmpbuf))
  9652. return soap->error;
  9653. s++;
  9654. if (*s)
  9655. { strncpy(soap->tmpbuf, s, sizeof(soap->tmpbuf));
  9656. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0';
  9657. soap->tmpbuf[strlen(soap->tmpbuf) - 1] = '\0';
  9658. if (soap_attribute(soap, "SOAP-ENC:arraySize", soap->tmpbuf))
  9659. return soap->error;
  9660. }
  9661. }
  9662. }
  9663. else
  9664. { if (offset && soap_attribute(soap, "SOAP-ENC:offset", offset))
  9665. return soap->error;
  9666. if (soap_attribute(soap, "SOAP-ENC:arrayType", type))
  9667. return soap->error;
  9668. }
  9669. #ifndef WITH_LEAN
  9670. if ((soap->mode & SOAP_XML_CANONICAL))
  9671. soap_utilize_ns(soap, type);
  9672. #endif
  9673. return soap_element_start_end_out(soap, NULL);
  9674. }
  9675. #endif
  9676. /******************************************************************************/
  9677. #ifndef PALM_1
  9678. SOAP_FMAC1
  9679. int
  9680. SOAP_FMAC2
  9681. soap_element_start_end_out(struct soap *soap, const char *tag)
  9682. { register struct soap_attribute *tp;
  9683. #ifndef WITH_LEAN
  9684. if (soap->mode & SOAP_XML_CANONICAL)
  9685. { struct soap_nlist *np;
  9686. for (tp = soap->attributes; tp; tp = tp->next)
  9687. { if (tp->visible && tp->name)
  9688. soap_utilize_ns(soap, tp->name);
  9689. }
  9690. for (np = soap->nlist; np; np = np->next)
  9691. { if (np->index == 1 && np->ns)
  9692. { if (*(np->id))
  9693. {
  9694. #ifdef HAVE_SNPRINTF
  9695. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns:%s", np->id);
  9696. #else
  9697. sprintf(soap->tmpbuf, "xmlns:%s", np->id);
  9698. #endif
  9699. }
  9700. else
  9701. strcpy(soap->tmpbuf, "xmlns");
  9702. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enabling utilized binding (level=%u) %s='%s'\n", np->level, soap->tmpbuf, np->ns));
  9703. soap_set_attr(soap, soap->tmpbuf, np->ns, 1);
  9704. np->index = 2;
  9705. }
  9706. }
  9707. }
  9708. #endif
  9709. #ifdef WITH_DOM
  9710. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  9711. { register struct soap_dom_attribute **att;
  9712. att = &soap->dom->atts;
  9713. for (tp = soap->attributes; tp; tp = tp->next)
  9714. { if (tp->visible)
  9715. { *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  9716. if (!*att)
  9717. return soap->error;
  9718. (*att)->next = NULL;
  9719. (*att)->nstr = NULL;
  9720. (*att)->name = soap_strdup(soap, tp->name);
  9721. (*att)->data = soap_strdup(soap, tp->value);
  9722. (*att)->wide = NULL;
  9723. (*att)->soap = soap;
  9724. att = &(*att)->next;
  9725. tp->visible = 0;
  9726. }
  9727. }
  9728. return SOAP_OK;
  9729. }
  9730. #endif
  9731. for (tp = soap->attributes; tp; tp = tp->next)
  9732. { if (tp->visible)
  9733. {
  9734. #ifndef WITH_LEAN
  9735. const char *s;
  9736. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tp->name, ':')))
  9737. { size_t n = s - tp->name;
  9738. if (soap->nlist && !strncmp(soap->nlist->id, tp->name, n) && !soap->nlist->id[n])
  9739. s++;
  9740. else
  9741. s = tp->name;
  9742. if (soap_send(soap, " ") || soap_send(soap, s))
  9743. return soap->error;
  9744. }
  9745. else
  9746. #endif
  9747. if (soap_send(soap, " ") || soap_send(soap, tp->name))
  9748. return soap->error;
  9749. if (tp->visible == 2 && tp->value)
  9750. if (soap_send_raw(soap, "=\"", 2)
  9751. || soap_string_out(soap, tp->value, tp->flag)
  9752. || soap_send_raw(soap, "\"", 1))
  9753. return soap->error;
  9754. tp->visible = 0;
  9755. }
  9756. }
  9757. if (tag)
  9758. {
  9759. #ifndef WITH_LEAN
  9760. if (soap->mode & SOAP_XML_CANONICAL)
  9761. { if (soap_send_raw(soap, ">", 1)
  9762. || soap_element_end_out(soap, tag))
  9763. return soap->error;
  9764. return SOAP_OK;
  9765. }
  9766. #endif
  9767. soap->level--; /* decrement level just before /> */
  9768. return soap_send_raw(soap, "/>", 2);
  9769. }
  9770. return soap_send_raw(soap, ">", 1);
  9771. }
  9772. #endif
  9773. /******************************************************************************/
  9774. #ifndef PALM_1
  9775. SOAP_FMAC1
  9776. int
  9777. SOAP_FMAC2
  9778. soap_element_end_out(struct soap *soap, const char *tag)
  9779. {
  9780. #ifndef WITH_LEAN
  9781. const char *s;
  9782. #endif
  9783. if (*tag == '-')
  9784. return SOAP_OK;
  9785. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element ending tag='%s'\n", tag));
  9786. #ifdef WITH_DOM
  9787. if (soap->feltendout && (soap->error = soap->feltendout(soap, tag)))
  9788. return soap->error;
  9789. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  9790. { if (soap->dom->prnt)
  9791. soap->dom = soap->dom->prnt;
  9792. return SOAP_OK;
  9793. }
  9794. #endif
  9795. #ifndef WITH_LEAN
  9796. if (soap->mode & SOAP_XML_CANONICAL)
  9797. soap_pop_namespace(soap);
  9798. if (soap->mode & SOAP_XML_INDENT)
  9799. { if (!soap->body)
  9800. { if (soap_send_raw(soap, soap_indent, soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  9801. return soap->error;
  9802. }
  9803. soap->body = 0;
  9804. }
  9805. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tag, ':')))
  9806. { soap_pop_namespace(soap);
  9807. tag = s + 1;
  9808. }
  9809. #endif
  9810. if (soap_send_raw(soap, "</", 2)
  9811. || soap_send(soap, tag))
  9812. return soap->error;
  9813. soap->level--; /* decrement level just before > */
  9814. return soap_send_raw(soap, ">", 1);
  9815. }
  9816. #endif
  9817. /******************************************************************************/
  9818. #ifndef PALM_1
  9819. SOAP_FMAC1
  9820. int
  9821. SOAP_FMAC2
  9822. soap_element_ref(struct soap *soap, const char *tag, int id, int href)
  9823. { register const char *s = "ref";
  9824. register int n = 1;
  9825. if (soap->version == 1)
  9826. { s = "href";
  9827. n = 0;
  9828. }
  9829. else if (soap->version == 2)
  9830. s = "SOAP-ENC:ref";
  9831. #ifdef HAVE_SNPRINTF
  9832. soap_snprintf(soap->href, sizeof(soap->href), "#_%d", href);
  9833. #else
  9834. sprintf(soap->href, "#_%d", href);
  9835. #endif
  9836. return soap_element_href(soap, tag, id, s, soap->href + n);
  9837. }
  9838. #endif
  9839. /******************************************************************************/
  9840. #ifndef PALM_1
  9841. SOAP_FMAC1
  9842. int
  9843. SOAP_FMAC2
  9844. soap_element_href(struct soap *soap, const char *tag, int id, const char *ref, const char *val)
  9845. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element '%s' reference %s='%s'\n", tag, ref, val));
  9846. if (soap_element(soap, tag, id, NULL)
  9847. || soap_attribute(soap, ref, val)
  9848. || soap_element_start_end_out(soap, tag))
  9849. return soap->error;
  9850. return SOAP_OK;
  9851. }
  9852. #endif
  9853. /******************************************************************************/
  9854. #ifndef PALM_1
  9855. SOAP_FMAC1
  9856. int
  9857. SOAP_FMAC2
  9858. soap_element_null(struct soap *soap, const char *tag, int id, const char *type)
  9859. { struct soap_attribute *tp = NULL;
  9860. for (tp = soap->attributes; tp; tp = tp->next)
  9861. if (tp->visible)
  9862. break;
  9863. if (tp || (soap->version == 2 && soap->position > 0) || id > 0 || (soap->mode & SOAP_XML_NIL))
  9864. { if (soap_element(soap, tag, id, type)
  9865. || (!tp && soap_attribute(soap, "xsi:nil", "true")))
  9866. return soap->error;
  9867. return soap_element_start_end_out(soap, tag);
  9868. }
  9869. soap->null = 1;
  9870. soap->position = 0;
  9871. soap->mustUnderstand = 0;
  9872. return SOAP_OK;
  9873. }
  9874. #endif
  9875. /******************************************************************************/
  9876. #ifndef PALM_1
  9877. SOAP_FMAC1
  9878. int
  9879. SOAP_FMAC2
  9880. soap_element_nil(struct soap *soap, const char *tag)
  9881. { if (soap_element(soap, tag, -1, NULL)
  9882. || ((soap->mode & SOAP_XML_NIL) && soap_attribute(soap, "xsi:nil", "true")))
  9883. return soap->error;
  9884. return soap_element_start_end_out(soap, tag);
  9885. }
  9886. #endif
  9887. /******************************************************************************/
  9888. #ifndef PALM_1
  9889. SOAP_FMAC1
  9890. int
  9891. SOAP_FMAC2
  9892. soap_element_id(struct soap *soap, const char *tag, int id, const void *p, const struct soap_array *a, int n, const char *type, int t)
  9893. { if (!p)
  9894. { soap->error = soap_element_null(soap, tag, id, type);
  9895. return -1;
  9896. }
  9897. #ifndef WITH_NOIDREF
  9898. if ((!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  9899. return 0;
  9900. if (id < 0)
  9901. { struct soap_plist *pp;
  9902. if (a)
  9903. id = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  9904. else
  9905. id = soap_pointer_lookup(soap, p, t, &pp);
  9906. if (id)
  9907. { if (soap_is_embedded(soap, pp))
  9908. { soap_element_ref(soap, tag, 0, id);
  9909. return -1;
  9910. }
  9911. if (soap_is_single(soap, pp))
  9912. return 0;
  9913. soap_set_embedded(soap, pp);
  9914. }
  9915. }
  9916. return id;
  9917. #else
  9918. return 0;
  9919. #endif
  9920. }
  9921. #endif
  9922. /******************************************************************************/
  9923. #ifndef PALM_1
  9924. SOAP_FMAC1
  9925. int
  9926. SOAP_FMAC2
  9927. soap_element_result(struct soap *soap, const char *tag)
  9928. { if (soap->version == 2 && soap->encodingStyle)
  9929. { if (soap_element(soap, "SOAP-RPC:result", 0, NULL)
  9930. || soap_attribute(soap, "xmlns:SOAP-RPC", soap_rpc)
  9931. || soap_element_start_end_out(soap, NULL)
  9932. || soap_string_out(soap, tag, 0)
  9933. || soap_element_end_out(soap, "SOAP-RPC:result"))
  9934. return soap->error;
  9935. }
  9936. return SOAP_OK;
  9937. }
  9938. #endif
  9939. /******************************************************************************/
  9940. #ifndef PALM_1
  9941. SOAP_FMAC1
  9942. void
  9943. SOAP_FMAC2
  9944. soap_check_result(struct soap *soap, const char *tag)
  9945. { if (soap->version == 2 && soap->encodingStyle)
  9946. { soap_instring(soap, ":result", NULL, NULL, 0, 2, -1, -1);
  9947. /* just ignore content for compliance reasons, but should compare tag to element's QName value? */
  9948. }
  9949. (void)tag;
  9950. }
  9951. #endif
  9952. /******************************************************************************/
  9953. #ifndef PALM_2
  9954. SOAP_FMAC1
  9955. int
  9956. SOAP_FMAC2
  9957. soap_attribute(struct soap *soap, const char *name, const char *value)
  9958. {
  9959. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attribute '%s'='%s'\n", name, value));
  9960. #ifdef WITH_DOM
  9961. if ((soap->mode & SOAP_XML_DOM) && !(soap->mode & SOAP_XML_CANONICAL) && soap->dom)
  9962. { register struct soap_dom_attribute *a = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  9963. if (!a)
  9964. return soap->error;
  9965. a->next = soap->dom->atts;
  9966. a->nstr = NULL;
  9967. a->name = soap_strdup(soap, name);
  9968. a->data = soap_strdup(soap, value);
  9969. a->wide = NULL;
  9970. a->soap = soap;
  9971. soap->dom->atts = a;
  9972. return SOAP_OK;
  9973. }
  9974. #endif
  9975. #ifndef WITH_LEAN
  9976. if (soap->mode & SOAP_XML_CANONICAL)
  9977. { /* push namespace */
  9978. if (!strncmp(name, "xmlns", 5) && (name[5] == ':' || name[5] == '\0'))
  9979. soap_push_ns(soap, name + 5 + (name[5] == ':'), value, 0);
  9980. else if (soap_set_attr(soap, name, value, 1))
  9981. return soap->error;
  9982. }
  9983. else
  9984. #endif
  9985. { if (soap_send(soap, " ") || soap_send(soap, name))
  9986. return soap->error;
  9987. if (value)
  9988. if (soap_send_raw(soap, "=\"", 2)
  9989. || soap_string_out(soap, value, 1)
  9990. || soap_send_raw(soap, "\"", 1))
  9991. return soap->error;
  9992. }
  9993. return SOAP_OK;
  9994. }
  9995. #endif
  9996. /******************************************************************************/
  9997. #ifndef PALM_2
  9998. SOAP_FMAC1
  9999. int
  10000. SOAP_FMAC2
  10001. soap_element_begin_in(struct soap *soap, const char *tag, int nillable, const char *type)
  10002. { if (!soap_peek_element(soap))
  10003. { if (soap->other)
  10004. return soap->error = SOAP_TAG_MISMATCH;
  10005. if (tag && *tag == '-')
  10006. return SOAP_OK;
  10007. if (!(soap->error = soap_match_tag(soap, soap->tag, tag)))
  10008. { soap->peeked = 0;
  10009. if (type && *soap->type && soap_match_tag(soap, soap->type, type))
  10010. return soap->error = SOAP_TYPE;
  10011. if (!nillable && soap->null && (soap->mode & SOAP_XML_STRICT))
  10012. return soap->error = SOAP_NULL;
  10013. if (soap->body)
  10014. soap->level++;
  10015. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin element found (level=%u) '%s'='%s'\n", soap->level, soap->tag, tag ? tag : SOAP_STR_EOS ));
  10016. soap->error = SOAP_OK;
  10017. }
  10018. }
  10019. else if (soap->error == SOAP_NO_TAG && tag && *tag == '-')
  10020. soap->error = SOAP_OK;
  10021. return soap->error;
  10022. }
  10023. #endif
  10024. /******************************************************************************/
  10025. #ifndef PALM_2
  10026. SOAP_FMAC1
  10027. int
  10028. SOAP_FMAC2
  10029. soap_element_end_in(struct soap *soap, const char *tag)
  10030. { register soap_wchar c;
  10031. register char *s;
  10032. register int n = 0;
  10033. if (tag && *tag == '-')
  10034. return SOAP_OK;
  10035. if (soap->error == SOAP_NO_TAG)
  10036. soap->error = SOAP_OK;
  10037. #ifdef WITH_DOM
  10038. /* this whitespace or mixed content is significant for DOM */
  10039. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  10040. { if (!soap->peeked && !soap_string_in(soap, 3, -1, -1))
  10041. return soap->error;
  10042. if (soap->dom->prnt)
  10043. soap->dom = soap->dom->prnt;
  10044. }
  10045. #endif
  10046. if (soap->peeked)
  10047. { if (*soap->tag)
  10048. n++;
  10049. soap->peeked = 0;
  10050. }
  10051. do
  10052. { while (((c = soap_get(soap)) != SOAP_TT))
  10053. { if ((int)c == EOF)
  10054. return soap->error = SOAP_CHK_EOF;
  10055. if (c == SOAP_LT)
  10056. n++;
  10057. else if (c == '/')
  10058. { c = soap_get(soap);
  10059. if (c == SOAP_GT)
  10060. n--;
  10061. else
  10062. soap_unget(soap, c);
  10063. }
  10064. }
  10065. } while (n--);
  10066. s = soap->tag;
  10067. n = sizeof(soap->tag);
  10068. while (soap_notblank(c = soap_get(soap)))
  10069. { if (--n > 0)
  10070. *s++ = (char)c;
  10071. }
  10072. *s = '\0';
  10073. if ((int)c == EOF)
  10074. return soap->error = SOAP_CHK_EOF;
  10075. while (soap_blank(c))
  10076. c = soap_get(soap);
  10077. if (c != SOAP_GT)
  10078. return soap->error = SOAP_SYNTAX_ERROR;
  10079. #ifndef WITH_LEAN
  10080. #ifdef WITH_DOM
  10081. if (soap->feltendin)
  10082. { soap->level--;
  10083. return soap->error = soap->feltendin(soap, soap->tag, tag);
  10084. }
  10085. #endif
  10086. if (tag && (soap->mode & SOAP_XML_STRICT))
  10087. { soap_pop_namespace(soap);
  10088. if (soap_match_tag(soap, soap->tag, tag))
  10089. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End element tag '%s' does not match '%s'\n", soap->tag, tag ? tag : SOAP_STR_EOS));
  10090. return soap->error = SOAP_SYNTAX_ERROR;
  10091. }
  10092. }
  10093. #endif
  10094. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End element found (level=%u) '%s'='%s'\n", soap->level, soap->tag, tag ? tag : SOAP_STR_EOS));
  10095. soap->level--;
  10096. return SOAP_OK;
  10097. }
  10098. #endif
  10099. /******************************************************************************/
  10100. #ifndef PALM_2
  10101. SOAP_FMAC1
  10102. const char *
  10103. SOAP_FMAC2
  10104. soap_attr_value(struct soap *soap, const char *name, int flag)
  10105. { register struct soap_attribute *tp;
  10106. if (*name == '-')
  10107. return SOAP_STR_EOS;
  10108. for (tp = soap->attributes; tp; tp = tp->next)
  10109. { if (tp->visible && !soap_match_tag(soap, tp->name, name))
  10110. break;
  10111. }
  10112. if (tp)
  10113. { if (flag == 2 && (soap->mode & SOAP_XML_STRICT))
  10114. soap->error = SOAP_PROHIBITED;
  10115. else
  10116. return tp->value;
  10117. }
  10118. else if (flag == 1 && (soap->mode & SOAP_XML_STRICT))
  10119. soap->error = SOAP_REQUIRED;
  10120. else
  10121. soap->error = SOAP_OK;
  10122. return NULL;
  10123. }
  10124. #endif
  10125. /******************************************************************************/
  10126. #ifndef PALM_2
  10127. SOAP_FMAC1
  10128. int
  10129. SOAP_FMAC2
  10130. soap_set_attr(struct soap *soap, const char *name, const char *value, int flag)
  10131. { register struct soap_attribute *tp;
  10132. if (*name == '-')
  10133. return SOAP_OK;
  10134. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set attribute %s='%s'\n", name, value ? value : SOAP_STR_EOS));
  10135. for (tp = soap->attributes; tp; tp = tp->next)
  10136. { if (!strcmp(tp->name, name))
  10137. break;
  10138. }
  10139. if (!tp)
  10140. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute %s\n", name));
  10141. if (!(tp = (struct soap_attribute*)SOAP_MALLOC(soap, sizeof(struct soap_attribute) + strlen(name))))
  10142. return soap->error = SOAP_EOM;
  10143. tp->ns = NULL;
  10144. #ifndef WITH_LEAN
  10145. if ((soap->mode & SOAP_XML_CANONICAL))
  10146. { struct soap_attribute **tpp = &soap->attributes;
  10147. const char *s = strchr(name, ':');
  10148. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inserting attribute %s for c14n\n", name));
  10149. if (!strncmp(name, "xmlns", 5))
  10150. { for (; *tpp; tpp = &(*tpp)->next)
  10151. if (strncmp((*tpp)->name, "xmlns", 5) || strcmp((*tpp)->name + 5, name + 5) > 0)
  10152. break;
  10153. }
  10154. else if (!s)
  10155. { for (; *tpp; tpp = &(*tpp)->next)
  10156. if (strncmp((*tpp)->name, "xmlns", 5) && ((*tpp)->ns || strcmp((*tpp)->name, name) > 0))
  10157. break;
  10158. }
  10159. else
  10160. { struct soap_nlist *np = soap_lookup_ns(soap, name, s - name);
  10161. if (np)
  10162. tp->ns = np->ns;
  10163. else
  10164. { struct soap_attribute *tq;
  10165. for (tq = soap->attributes; tq; tq = tq->next)
  10166. { if (!strncmp(tq->name, "xmlns:", 6) && !strncmp(tq->name + 6, name, s - name) && !tq->name[6 + s - name])
  10167. { tp->ns = tq->ns;
  10168. break;
  10169. }
  10170. }
  10171. }
  10172. for (; *tpp; tpp = &(*tpp)->next)
  10173. { int k;
  10174. if (strncmp((*tpp)->name, "xmlns", 5) && (*tpp)->ns && tp->ns && ((k = strcmp((*tpp)->ns, tp->ns)) > 0 || (!k && strcmp((*tpp)->name, name) > 0)))
  10175. break;
  10176. }
  10177. }
  10178. tp->next = *tpp;
  10179. *tpp = tp;
  10180. }
  10181. else
  10182. #endif
  10183. { tp->next = soap->attributes;
  10184. soap->attributes = tp;
  10185. }
  10186. strcpy((char*)tp->name, name);
  10187. tp->value = NULL;
  10188. }
  10189. else if (tp->visible)
  10190. { return SOAP_OK;
  10191. }
  10192. else if (value && tp->value && tp->size <= strlen(value))
  10193. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute value of %s (free %p)\n", name, tp->value));
  10194. SOAP_FREE(soap, tp->value);
  10195. tp->value = NULL;
  10196. tp->ns = NULL;
  10197. }
  10198. if (value)
  10199. { if (!tp->value)
  10200. { tp->size = strlen(value) + 1;
  10201. if (!(tp->value = (char*)SOAP_MALLOC(soap, tp->size)))
  10202. return soap->error = SOAP_EOM;
  10203. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute value for %s (%p)\n", tp->name, tp->value));
  10204. }
  10205. strcpy(tp->value, value);
  10206. if (!strncmp(tp->name, "xmlns:", 6))
  10207. tp->ns = tp->value;
  10208. tp->visible = 2;
  10209. tp->flag = (short)flag;
  10210. #ifndef WITH_LEAN
  10211. if (!strcmp(name, "wsu:Id"))
  10212. { soap->event = SOAP_SEC_BEGIN;
  10213. strncpy(soap->id, value, sizeof(soap->id));
  10214. soap->id[sizeof(soap->id) - 1] = '\0';
  10215. }
  10216. #endif
  10217. }
  10218. else
  10219. tp->visible = 1;
  10220. return SOAP_OK;
  10221. }
  10222. #endif
  10223. /******************************************************************************/
  10224. #ifndef PALM_2
  10225. SOAP_FMAC1
  10226. void
  10227. SOAP_FMAC2
  10228. soap_clr_attr(struct soap *soap)
  10229. { register struct soap_attribute *tp;
  10230. #ifndef WITH_LEAN
  10231. if ((soap->mode & SOAP_XML_CANONICAL))
  10232. { while (soap->attributes)
  10233. { tp = soap->attributes->next;
  10234. if (soap->attributes->value)
  10235. SOAP_FREE(soap, soap->attributes->value);
  10236. SOAP_FREE(soap, soap->attributes);
  10237. soap->attributes = tp;
  10238. }
  10239. }
  10240. else
  10241. #endif
  10242. { for (tp = soap->attributes; tp; tp = tp->next)
  10243. tp->visible = 0;
  10244. }
  10245. }
  10246. #endif
  10247. /******************************************************************************/
  10248. #ifndef PALM_2
  10249. static int
  10250. soap_getattrval(struct soap *soap, char *s, size_t n, soap_wchar d)
  10251. { register size_t i;
  10252. for (i = 0; i < n; i++)
  10253. { register soap_wchar c = soap_get(soap);
  10254. switch (c)
  10255. {
  10256. case SOAP_TT:
  10257. *s++ = '<';
  10258. soap_unget(soap, '/');
  10259. break;
  10260. case SOAP_LT:
  10261. *s++ = '<';
  10262. break;
  10263. case SOAP_GT:
  10264. if (d == ' ')
  10265. { soap_unget(soap, c);
  10266. *s = '\0';
  10267. return SOAP_OK;
  10268. }
  10269. *s++ = '>';
  10270. break;
  10271. case SOAP_QT:
  10272. if (c == d)
  10273. { *s = '\0';
  10274. return SOAP_OK;
  10275. }
  10276. *s++ = '"';
  10277. break;
  10278. case SOAP_AP:
  10279. if (c == d)
  10280. { *s = '\0';
  10281. return SOAP_OK;
  10282. }
  10283. *s++ = '\'';
  10284. break;
  10285. case '\t':
  10286. case '\n':
  10287. case '\r':
  10288. case ' ':
  10289. case '/':
  10290. if (d == ' ')
  10291. { soap_unget(soap, c);
  10292. *s = '\0';
  10293. return SOAP_OK;
  10294. }
  10295. default:
  10296. if ((int)c == EOF)
  10297. { *s = '\0';
  10298. return soap->error = SOAP_CHK_EOF;
  10299. }
  10300. *s++ = (char)c;
  10301. }
  10302. }
  10303. return soap->error = SOAP_EOM;
  10304. }
  10305. #endif
  10306. /******************************************************************************/
  10307. #ifdef WITH_FAST
  10308. #ifndef PALM_2
  10309. SOAP_FMAC1
  10310. int
  10311. SOAP_FMAC2
  10312. soap_store_lab(struct soap *soap, const char *s, size_t n)
  10313. { soap->labidx = 0;
  10314. return soap_append_lab(soap, s, n);
  10315. }
  10316. #endif
  10317. #endif
  10318. /******************************************************************************/
  10319. #ifdef WITH_FAST
  10320. #ifndef PALM_2
  10321. SOAP_FMAC1
  10322. int
  10323. SOAP_FMAC2
  10324. soap_append_lab(struct soap *soap, const char *s, size_t n)
  10325. { if (soap->labidx + n >= soap->lablen)
  10326. { register char *t = soap->labbuf;
  10327. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging look-aside buffer to append data, size=%lu\n", (unsigned long)soap->lablen));
  10328. if (soap->lablen == 0)
  10329. soap->lablen = SOAP_LABLEN;
  10330. while (soap->labidx + n >= soap->lablen)
  10331. soap->lablen <<= 1;
  10332. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New look-aside buffer size=%lu\n", (unsigned long)soap->lablen));
  10333. soap->labbuf = (char*)SOAP_MALLOC(soap, soap->lablen);
  10334. if (!soap->labbuf)
  10335. { if (t)
  10336. SOAP_FREE(soap, t);
  10337. return soap->error = SOAP_EOM;
  10338. }
  10339. if (t)
  10340. { memcpy(soap->labbuf, t, soap->labidx);
  10341. SOAP_FREE(soap, t);
  10342. }
  10343. }
  10344. if (s)
  10345. { memcpy(soap->labbuf + soap->labidx, s, n);
  10346. soap->labidx += n;
  10347. }
  10348. return SOAP_OK;
  10349. }
  10350. #endif
  10351. #endif
  10352. /******************************************************************************/
  10353. #ifndef PALM_2
  10354. SOAP_FMAC1
  10355. int
  10356. SOAP_FMAC2
  10357. soap_peek_element(struct soap *soap)
  10358. {
  10359. #ifdef WITH_DOM
  10360. register struct soap_dom_attribute **att = NULL;
  10361. register char *lead = NULL;
  10362. #endif
  10363. register struct soap_attribute *tp, *tq = NULL;
  10364. register const char *t;
  10365. register char *s;
  10366. register soap_wchar c;
  10367. register int i;
  10368. if (soap->peeked)
  10369. { if (!*soap->tag)
  10370. return soap->error = SOAP_NO_TAG;
  10371. return SOAP_OK;
  10372. }
  10373. soap->peeked = 1;
  10374. soap->id[0] = '\0';
  10375. soap->href[0] = '\0';
  10376. soap->type[0] = '\0';
  10377. soap->arrayType[0] = '\0';
  10378. soap->arraySize[0] = '\0';
  10379. soap->arrayOffset[0] = '\0';
  10380. soap->other = 0;
  10381. soap->root = -1;
  10382. soap->position = 0;
  10383. soap->null = 0;
  10384. soap->mustUnderstand = 0;
  10385. /* UTF-8 BOM? */
  10386. c = soap_getchar(soap);
  10387. if (c == 0xEF && soap_get0(soap) == 0xBB)
  10388. { c = soap_get1(soap);
  10389. if ((c = soap_get1(soap)) == 0xBF)
  10390. soap->mode &= ~SOAP_ENC_LATIN;
  10391. else
  10392. soap_unget(soap, (0x0F << 12) | (0xBB << 6) | (c & 0x3F)); /* UTF-8 */
  10393. }
  10394. else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  10395. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  10396. return soap->error = SOAP_UTF_ERROR;
  10397. else
  10398. soap_unget(soap, c);
  10399. c = soap_get(soap);
  10400. #ifdef WITH_DOM
  10401. /* whitespace leading to tag is not insignificant for DOM */
  10402. if (soap_blank(c))
  10403. { soap->labidx = 0;
  10404. do
  10405. { if (soap_append_lab(soap, NULL, 0))
  10406. return soap->error;
  10407. s = soap->labbuf + soap->labidx;
  10408. i = soap->lablen - soap->labidx;
  10409. soap->labidx = soap->lablen;
  10410. while (soap_blank(c) && i--)
  10411. { *s++ = c;
  10412. c = soap_get(soap);
  10413. }
  10414. }
  10415. while (soap_blank(c));
  10416. *s = '\0';
  10417. lead = soap->labbuf;
  10418. }
  10419. #else
  10420. /* skip space */
  10421. while (soap_blank(c))
  10422. c = soap_get(soap);
  10423. #endif
  10424. if (c != SOAP_LT)
  10425. { *soap->tag = '\0';
  10426. if ((int)c == EOF)
  10427. return soap->error = SOAP_CHK_EOF;
  10428. soap_unget(soap, c);
  10429. #ifdef WITH_DOM
  10430. /* whitespace leading to end tag is significant for DOM */
  10431. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  10432. { if (lead && *lead)
  10433. soap->dom->tail = soap_strdup(soap, lead);
  10434. else
  10435. soap->dom->tail = (char*)SOAP_STR_EOS;
  10436. }
  10437. #endif
  10438. return soap->error = SOAP_NO_TAG;
  10439. }
  10440. do c = soap_get1(soap);
  10441. while (soap_blank(c));
  10442. s = soap->tag;
  10443. i = sizeof(soap->tag);
  10444. while (c != '>' && c != '/' && soap_notblank(c) && (int)c != EOF)
  10445. { if (--i > 0)
  10446. *s++ = (char)c;
  10447. c = soap_get1(soap);
  10448. }
  10449. *s = '\0';
  10450. while (soap_blank(c))
  10451. c = soap_get1(soap);
  10452. #ifdef WITH_DOM
  10453. if (soap->mode & SOAP_XML_DOM)
  10454. { register struct soap_dom_element *elt;
  10455. elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  10456. if (!elt)
  10457. return soap->error;
  10458. elt->next = NULL;
  10459. elt->nstr = NULL;
  10460. elt->name = soap_strdup(soap, soap->tag);
  10461. elt->prnt = soap->dom;
  10462. elt->elts = NULL;
  10463. elt->atts = NULL;
  10464. elt->data = NULL;
  10465. elt->wide = NULL;
  10466. elt->type = 0;
  10467. elt->node = NULL;
  10468. elt->head = soap_strdup(soap, lead);
  10469. elt->tail = NULL;
  10470. elt->soap = soap;
  10471. if (soap->dom)
  10472. { struct soap_dom_element *p = soap->dom->elts;
  10473. if (p)
  10474. { while (p->next)
  10475. p = p->next;
  10476. p->next = elt;
  10477. }
  10478. else
  10479. soap->dom->elts = elt;
  10480. }
  10481. soap->dom = elt;
  10482. att = &elt->atts;
  10483. }
  10484. #endif
  10485. soap_pop_namespace(soap);
  10486. for (tp = soap->attributes; tp; tp = tp->next)
  10487. tp->visible = 0;
  10488. while ((int)c != EOF && c != '>' && c != '/')
  10489. { s = soap->tmpbuf;
  10490. i = sizeof(soap->tmpbuf);
  10491. while (c != '=' && c != '>' && c != '/' && soap_notblank(c) && (int)c != EOF)
  10492. { if (--i > 0)
  10493. *s++ = (char)c;
  10494. c = soap_get1(soap);
  10495. }
  10496. *s = '\0';
  10497. if (i == sizeof(soap->tmpbuf))
  10498. return soap->error = SOAP_SYNTAX_ERROR;
  10499. #ifdef WITH_DOM
  10500. /* add attribute name to dom */
  10501. if (att)
  10502. { *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  10503. if (!*att)
  10504. return soap->error;
  10505. (*att)->next = NULL;
  10506. (*att)->nstr = NULL;
  10507. (*att)->name = soap_strdup(soap, soap->tmpbuf);
  10508. (*att)->data = NULL;
  10509. (*att)->wide = NULL;
  10510. (*att)->soap = soap;
  10511. }
  10512. #endif
  10513. if (!strncmp(soap->tmpbuf, "xmlns", 5))
  10514. { if (soap->tmpbuf[5] == ':')
  10515. t = soap->tmpbuf + 6;
  10516. else if (soap->tmpbuf[5])
  10517. t = NULL;
  10518. else
  10519. t = SOAP_STR_EOS;
  10520. }
  10521. else
  10522. t = NULL;
  10523. tq = NULL;
  10524. for (tp = soap->attributes; tp; tq = tp, tp = tp->next)
  10525. { if (!SOAP_STRCMP(tp->name, soap->tmpbuf))
  10526. break;
  10527. }
  10528. if (!tp)
  10529. { tp = (struct soap_attribute*)SOAP_MALLOC(soap, sizeof(struct soap_attribute) + strlen(soap->tmpbuf));
  10530. if (!tp)
  10531. return soap->error = SOAP_EOM;
  10532. strcpy((char*)tp->name, soap->tmpbuf);
  10533. tp->value = NULL;
  10534. tp->size = 0;
  10535. tp->ns = NULL;
  10536. /* if attribute name is qualified, append it to the end of the list */
  10537. if (tq && strchr(soap->tmpbuf, ':'))
  10538. { tq->next = tp;
  10539. tp->next = NULL;
  10540. }
  10541. else
  10542. { tp->next = soap->attributes;
  10543. soap->attributes = tp;
  10544. }
  10545. }
  10546. while (soap_blank(c))
  10547. c = soap_get1(soap);
  10548. if (c == '=')
  10549. { do c = soap_getutf8(soap);
  10550. while (soap_blank(c));
  10551. if (c != SOAP_QT && c != SOAP_AP)
  10552. { soap_unget(soap, c);
  10553. c = ' '; /* blank delimiter */
  10554. }
  10555. if (soap_getattrval(soap, tp->value, tp->size, c))
  10556. {
  10557. #ifdef WITH_FAST
  10558. if (soap->error != SOAP_EOM)
  10559. return soap->error;
  10560. soap->error = SOAP_OK;
  10561. if (soap_store_lab(soap, tp->value, tp->size))
  10562. return soap->error;
  10563. if (tp->value)
  10564. SOAP_FREE(soap, tp->value);
  10565. tp->value = NULL;
  10566. for (;;)
  10567. { if (soap_getattrval(soap, soap->labbuf + soap->labidx, soap->lablen - soap->labidx, c))
  10568. { if (soap->error != SOAP_EOM)
  10569. return soap->error;
  10570. soap->error = SOAP_OK;
  10571. soap->labidx = soap->lablen;
  10572. if (soap_append_lab(soap, NULL, 0))
  10573. return soap->error;
  10574. }
  10575. else
  10576. break;
  10577. }
  10578. if (soap->labidx)
  10579. tp->size = soap->lablen;
  10580. else
  10581. { tp->size = strlen(soap->labbuf) + 1;
  10582. if (tp->size < SOAP_LABLEN)
  10583. tp->size = SOAP_LABLEN;
  10584. }
  10585. if (!(tp->value = (char*)SOAP_MALLOC(soap, tp->size)))
  10586. return soap->error = SOAP_EOM;
  10587. strcpy(tp->value, soap->labbuf);
  10588. #else
  10589. size_t n;
  10590. if (soap->error != SOAP_EOM)
  10591. return soap->error;
  10592. soap->error = SOAP_OK;
  10593. if (soap_new_block(soap) == NULL)
  10594. return soap->error;
  10595. for (;;)
  10596. { if (!(s = (char*)soap_push_block(soap, NULL, SOAP_BLKLEN)))
  10597. return soap->error;
  10598. if (soap_getattrval(soap, s, SOAP_BLKLEN, c))
  10599. { if (soap->error != SOAP_EOM)
  10600. return soap->error;
  10601. soap->error = SOAP_OK;
  10602. }
  10603. else
  10604. break;
  10605. }
  10606. n = tp->size + soap->blist->size;
  10607. if (!(s = (char*)SOAP_MALLOC(soap, n)))
  10608. return soap->error = SOAP_EOM;
  10609. if (tp->value)
  10610. { memcpy(s, tp->value, tp->size);
  10611. SOAP_FREE(soap, tp->value);
  10612. }
  10613. soap_save_block(soap, NULL, s + tp->size, 0);
  10614. tp->value = s;
  10615. tp->size = n;
  10616. #endif
  10617. }
  10618. do c = soap_get1(soap);
  10619. while (soap_blank(c));
  10620. tp->visible = 2; /* seen this attribute w/ value */
  10621. #ifdef WITH_DOM
  10622. if (att)
  10623. (*att)->data = soap_strdup(soap, tp->value);
  10624. #endif
  10625. }
  10626. else
  10627. tp->visible = 1; /* seen this attribute w/o value */
  10628. #ifdef WITH_DOM
  10629. if (att)
  10630. att = &(*att)->next;
  10631. #endif
  10632. if (t && tp->value)
  10633. { if (soap_push_namespace(soap, t, tp->value) == NULL)
  10634. return soap->error;
  10635. }
  10636. }
  10637. #ifdef WITH_DOM
  10638. if (att)
  10639. { soap->dom->nstr = soap_current_namespace(soap, soap->tag);
  10640. for (att = &soap->dom->atts; *att; att = &(*att)->next)
  10641. (*att)->nstr = soap_current_namespace(soap, (*att)->name);
  10642. }
  10643. #endif
  10644. if ((int)c == EOF)
  10645. return soap->error = SOAP_CHK_EOF;
  10646. if (!(soap->body = (c != '/')))
  10647. do c = soap_get1(soap);
  10648. while (soap_blank(c));
  10649. #ifdef WITH_DOM
  10650. if (soap->mode & SOAP_XML_DOM)
  10651. { if (!soap->body && soap->dom->prnt)
  10652. soap->dom = soap->dom->prnt;
  10653. }
  10654. #endif
  10655. for (tp = soap->attributes; tp; tp = tp->next)
  10656. { if (tp->visible && tp->value)
  10657. {
  10658. #ifndef WITH_NOIDREF
  10659. if (!strcmp(tp->name, "id"))
  10660. { if ((soap->version > 0 && !(soap->imode & SOAP_XML_TREE))
  10661. || (soap->mode & SOAP_XML_GRAPH))
  10662. { *soap->id = '#';
  10663. strncpy(soap->id + 1, tp->value, sizeof(soap->id) - 2);
  10664. soap->id[sizeof(soap->id) - 1] = '\0';
  10665. }
  10666. }
  10667. else if (!strcmp(tp->name, "href"))
  10668. { if ((soap->version == 1 && !(soap->imode & SOAP_XML_TREE))
  10669. || (soap->mode & SOAP_XML_GRAPH)
  10670. || (soap->mode & SOAP_ENC_MTOM)
  10671. || (soap->mode & SOAP_ENC_DIME))
  10672. { strncpy(soap->href, tp->value, sizeof(soap->href) - 1);
  10673. soap->href[sizeof(soap->href) - 1] = '\0';
  10674. }
  10675. }
  10676. else if (!strcmp(tp->name, "ref"))
  10677. { if ((soap->version == 2 && !(soap->imode & SOAP_XML_TREE))
  10678. || (soap->mode & SOAP_XML_GRAPH))
  10679. { *soap->href = '#';
  10680. strncpy(soap->href + (*tp->value != '#'), tp->value, sizeof(soap->href) - 2);
  10681. soap->href[sizeof(soap->href) - 1] = '\0';
  10682. }
  10683. }
  10684. else
  10685. #endif
  10686. if (!soap_match_tag(soap, tp->name, "xsi:type"))
  10687. { strncpy(soap->type, tp->value, sizeof(soap->type) - 1);
  10688. soap->type[sizeof(soap->type) - 1] = '\0';
  10689. }
  10690. else if ((!soap_match_tag(soap, tp->name, "xsi:null")
  10691. || !soap_match_tag(soap, tp->name, "xsi:nil"))
  10692. && (!strcmp(tp->value, "1")
  10693. || !strcmp(tp->value, "true")))
  10694. { soap->null = 1;
  10695. }
  10696. else if (soap->version == 1)
  10697. { if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arrayType"))
  10698. { s = soap_strrchr(tp->value, '[');
  10699. if (s && (size_t)(s - tp->value) < sizeof(soap->arrayType))
  10700. { strncpy(soap->arrayType, tp->value, s - tp->value);
  10701. soap->arrayType[s - tp->value] = '\0';
  10702. strncpy(soap->arraySize, s, sizeof(soap->arraySize));
  10703. }
  10704. else
  10705. strncpy(soap->arrayType, tp->value, sizeof(soap->arrayType));
  10706. soap->arraySize[sizeof(soap->arraySize) - 1] = '\0';
  10707. soap->arrayType[sizeof(soap->arrayType) - 1] = '\0';
  10708. }
  10709. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:offset"))
  10710. strncpy(soap->arrayOffset, tp->value, sizeof(soap->arrayOffset));
  10711. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:position"))
  10712. soap->position = soap_getposition(tp->value, soap->positions);
  10713. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:root"))
  10714. soap->root = ((!strcmp(tp->value, "1") || !strcmp(tp->value, "true")));
  10715. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  10716. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true")))
  10717. soap->mustUnderstand = 1;
  10718. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:actor"))
  10719. { if ((!soap->actor || strcmp(soap->actor, tp->value))
  10720. && strcmp(tp->value, "http://schemas.xmlsoap.org/soap/actor/next"))
  10721. soap->other = 1;
  10722. }
  10723. }
  10724. else if (soap->version == 2)
  10725. {
  10726. #ifndef WITH_NOIDREF
  10727. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:id"))
  10728. { *soap->id = '#';
  10729. strncpy(soap->id + 1, tp->value, sizeof(soap->id) - 2);
  10730. soap->id[sizeof(soap->id) - 1] = '\0';
  10731. }
  10732. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:ref"))
  10733. { *soap->href = '#';
  10734. strncpy(soap->href + (*tp->value != '#'), tp->value, sizeof(soap->href) - 2);
  10735. soap->href[sizeof(soap->href) - 1] = '\0';
  10736. }
  10737. else
  10738. #endif
  10739. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:itemType"))
  10740. strncpy(soap->arrayType, tp->value, sizeof(soap->arrayType) - 1);
  10741. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arraySize"))
  10742. strncpy(soap->arraySize, tp->value, sizeof(soap->arraySize) - 1);
  10743. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  10744. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true")))
  10745. soap->mustUnderstand = 1;
  10746. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:role"))
  10747. { if ((!soap->actor || strcmp(soap->actor, tp->value))
  10748. && strcmp(tp->value, "http://www.w3.org/2003/05/soap-envelope/role/next"))
  10749. soap->other = 1;
  10750. }
  10751. }
  10752. else
  10753. { if (!soap_match_tag(soap, tp->name, "wsdl:required") && !strcmp(tp->value, "true"))
  10754. soap->mustUnderstand = 1;
  10755. }
  10756. }
  10757. }
  10758. #ifdef WITH_DOM
  10759. if (soap->feltbegin)
  10760. return soap->error = soap->feltbegin(soap, soap->tag);
  10761. #endif
  10762. return soap->error = SOAP_OK;
  10763. }
  10764. #endif
  10765. /******************************************************************************/
  10766. #ifndef PALM_2
  10767. SOAP_FMAC1
  10768. void
  10769. SOAP_FMAC2
  10770. soap_retry(struct soap *soap)
  10771. { soap->error = SOAP_OK;
  10772. soap_revert(soap);
  10773. }
  10774. #endif
  10775. /******************************************************************************/
  10776. #ifndef PALM_2
  10777. SOAP_FMAC1
  10778. void
  10779. SOAP_FMAC2
  10780. soap_revert(struct soap *soap)
  10781. { if (!soap->peeked)
  10782. { soap->peeked = 1;
  10783. if (soap->body)
  10784. soap->level--;
  10785. }
  10786. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reverting to last element '%s' (level=%u)\n", soap->tag, soap->level));
  10787. }
  10788. #endif
  10789. /******************************************************************************/
  10790. #ifndef PALM_2
  10791. SOAP_FMAC1
  10792. int
  10793. SOAP_FMAC2
  10794. soap_string_out(struct soap *soap, const char *s, int flag)
  10795. { register const char *t;
  10796. register soap_wchar c;
  10797. register soap_wchar mask = (soap_wchar)0xFFFFFF80UL;
  10798. #ifdef WITH_DOM
  10799. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  10800. { soap->dom->data = soap_strdup(soap, s);
  10801. return SOAP_OK;
  10802. }
  10803. #endif
  10804. if (flag == 2 || soap->mode & SOAP_C_UTFSTRING)
  10805. mask = 0;
  10806. t = s;
  10807. while ((c = *t++))
  10808. { switch (c)
  10809. {
  10810. case 0x09:
  10811. if (flag)
  10812. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#x9;", 5))
  10813. return soap->error;
  10814. s = t;
  10815. }
  10816. break;
  10817. case 0x0A:
  10818. if (flag || !(soap->mode & SOAP_XML_CANONICAL))
  10819. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#xA;", 5))
  10820. return soap->error;
  10821. s = t;
  10822. }
  10823. break;
  10824. case 0x0D:
  10825. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#xD;", 5))
  10826. return soap->error;
  10827. s = t;
  10828. break;
  10829. case '&':
  10830. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&amp;", 5))
  10831. return soap->error;
  10832. s = t;
  10833. break;
  10834. case '<':
  10835. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&lt;", 4))
  10836. return soap->error;
  10837. s = t;
  10838. break;
  10839. case '>':
  10840. if (!flag)
  10841. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&gt;", 4))
  10842. return soap->error;
  10843. s = t;
  10844. }
  10845. break;
  10846. case '"':
  10847. if (flag)
  10848. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&quot;", 6))
  10849. return soap->error;
  10850. s = t;
  10851. }
  10852. break;
  10853. default:
  10854. #ifndef WITH_LEANER
  10855. #ifdef HAVE_MBTOWC
  10856. if (soap->mode & SOAP_C_MBSTRING)
  10857. { wchar_t wc;
  10858. register int m = mbtowc(&wc, t - 1, MB_CUR_MAX);
  10859. if (m > 0 && !((soap_wchar)wc == c && m == 1 && c < 0x80))
  10860. { if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned long)wc))
  10861. return soap->error;
  10862. s = t += m - 1;
  10863. continue;
  10864. }
  10865. }
  10866. #endif
  10867. #endif
  10868. #ifndef WITH_NOSTRINGTOUTF8
  10869. if ((c & mask) || !(c & 0xFFFFFFE0UL))
  10870. { if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned char)c))
  10871. return soap->error;
  10872. s = t;
  10873. }
  10874. #endif
  10875. }
  10876. }
  10877. return soap_send_raw(soap, s, t - s - 1);
  10878. }
  10879. #endif
  10880. /******************************************************************************/
  10881. #ifndef PALM_2
  10882. SOAP_FMAC1
  10883. char *
  10884. SOAP_FMAC2
  10885. soap_string_in(struct soap *soap, int flag, long minlen, long maxlen)
  10886. { register char *s;
  10887. char *t = NULL;
  10888. register size_t i;
  10889. register long l = 0;
  10890. register int n = 0, f = 0, m = 0;
  10891. register soap_wchar c;
  10892. #if !defined(WITH_LEANER) && defined(HAVE_WCTOMB)
  10893. char buf[MB_LEN_MAX > 8 ? MB_LEN_MAX : 8];
  10894. #else
  10895. char buf[8];
  10896. #endif
  10897. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Reading string content, flag=%d\n", flag));
  10898. if (soap->peeked && *soap->tag)
  10899. {
  10900. #ifndef WITH_LEAN
  10901. struct soap_attribute *tp;
  10902. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String content includes tag '%s' and attributes\n", soap->tag));
  10903. t = soap->tmpbuf;
  10904. *t = '<';
  10905. strncpy(t + 1, soap->tag, sizeof(soap->tmpbuf) - 2);
  10906. t[sizeof(soap->tmpbuf) - 1] = '\0';
  10907. t += strlen(t);
  10908. for (tp = soap->attributes; tp; tp = tp->next)
  10909. { if (tp->visible)
  10910. { if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  10911. break;
  10912. *t++ = ' ';
  10913. strcpy(t, tp->name);
  10914. t += strlen(t);
  10915. if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  10916. break; /* too many or large attribute values */
  10917. if (tp->value)
  10918. { *t++ = '=';
  10919. *t++ = '"';
  10920. strcpy(t, tp->value);
  10921. t += strlen(t);
  10922. *t++ = '"';
  10923. }
  10924. }
  10925. }
  10926. if (!soap->body)
  10927. *t++ = '/';
  10928. *t++ = '>';
  10929. *t = '\0';
  10930. t = soap->tmpbuf;
  10931. m = (int)strlen(soap->tmpbuf);
  10932. #endif
  10933. if (soap->body)
  10934. n = 1;
  10935. f = 1;
  10936. soap->peeked = 0;
  10937. }
  10938. #ifdef WITH_CDATA
  10939. if (!flag)
  10940. { register int state = 0;
  10941. #ifdef WITH_FAST
  10942. soap->labidx = 0; /* use look-aside buffer */
  10943. #else
  10944. if (soap_new_block(soap) == NULL)
  10945. return NULL;
  10946. #endif
  10947. for (;;)
  10948. {
  10949. #ifdef WITH_FAST
  10950. register size_t k;
  10951. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  10952. return NULL;
  10953. s = soap->labbuf + soap->labidx; /* space to populate */
  10954. k = soap->lablen - soap->labidx; /* number of bytes available */
  10955. soap->labidx = soap->lablen; /* claim this space */
  10956. #else
  10957. register size_t k = SOAP_BLKLEN;
  10958. if (!(s = (char*)soap_push_block(soap, NULL, k)))
  10959. return NULL;
  10960. #endif
  10961. for (i = 0; i < k; i++)
  10962. { if (m > 0)
  10963. { *s++ = *t++; /* copy multibyte characters */
  10964. m--;
  10965. continue;
  10966. }
  10967. c = soap_getchar(soap);
  10968. if ((int)c == EOF)
  10969. goto end;
  10970. if ((c >= 0x80 || c < SOAP_AP) && state != 1 && !(soap->mode & SOAP_ENC_LATIN))
  10971. { if ((c & 0x7FFFFFFF) >= 0x80)
  10972. { soap_unget(soap, c);
  10973. c = soap_getutf8(soap);
  10974. }
  10975. if ((c & 0x7FFFFFFF) >= 0x80 && (!flag || (soap->mode & SOAP_C_UTFSTRING)))
  10976. { c &= 0x7FFFFFFF;
  10977. t = buf;
  10978. if (c < 0x0800)
  10979. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  10980. else
  10981. { if (c < 0x010000)
  10982. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  10983. else
  10984. { if (c < 0x200000)
  10985. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  10986. else
  10987. { if (c < 0x04000000)
  10988. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  10989. else
  10990. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  10991. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  10992. }
  10993. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  10994. }
  10995. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  10996. }
  10997. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  10998. }
  10999. *t++ = (char)(0x80 | (c & 0x3F));
  11000. m = (int)(t - buf) - 1;
  11001. t = buf;
  11002. *s++ = *t++;
  11003. continue;
  11004. }
  11005. }
  11006. switch (state)
  11007. { case 1:
  11008. if (c == ']')
  11009. state = 4;
  11010. *s++ = (char)c;
  11011. continue;
  11012. case 2:
  11013. if (c == '-')
  11014. state = 6;
  11015. *s++ = (char)c;
  11016. continue;
  11017. case 3:
  11018. if (c == '?')
  11019. state = 8;
  11020. *s++ = (char)c;
  11021. continue;
  11022. /* CDATA */
  11023. case 4:
  11024. if (c == ']')
  11025. state = 5;
  11026. else
  11027. state = 1;
  11028. *s++ = (char)c;
  11029. continue;
  11030. case 5:
  11031. if (c == '>')
  11032. state = 0;
  11033. else if (c != ']')
  11034. state = 1;
  11035. *s++ = (char)c;
  11036. continue;
  11037. /* comment */
  11038. case 6:
  11039. if (c == '-')
  11040. state = 7;
  11041. else
  11042. state = 2;
  11043. *s++ = (char)c;
  11044. continue;
  11045. case 7:
  11046. if (c == '>')
  11047. state = 0;
  11048. else if (c != '-')
  11049. state = 2;
  11050. *s++ = (char)c;
  11051. continue;
  11052. /* PI */
  11053. case 8:
  11054. if (c == '>')
  11055. state = 0;
  11056. else if (c != '?')
  11057. state = 3;
  11058. *s++ = (char)c;
  11059. continue;
  11060. }
  11061. switch (c)
  11062. {
  11063. case SOAP_TT:
  11064. if (n == 0)
  11065. goto end;
  11066. n--;
  11067. *s++ = '<';
  11068. t = (char*)"/";
  11069. m = 1;
  11070. break;
  11071. case SOAP_LT:
  11072. if (f && n == 0)
  11073. goto end;
  11074. n++;
  11075. *s++ = '<';
  11076. break;
  11077. case SOAP_GT:
  11078. *s++ = '>';
  11079. break;
  11080. case SOAP_QT:
  11081. *s++ = '"';
  11082. break;
  11083. case SOAP_AP:
  11084. *s++ = '\'';
  11085. break;
  11086. case '/':
  11087. if (n > 0)
  11088. { c = soap_getchar(soap);
  11089. if (c == '>')
  11090. n--;
  11091. soap_unget(soap, c);
  11092. }
  11093. *s++ = '/';
  11094. break;
  11095. case '<':
  11096. c = soap_getchar(soap);
  11097. if (c == '/')
  11098. { if (n == 0)
  11099. { c = SOAP_TT;
  11100. goto end;
  11101. }
  11102. n--;
  11103. }
  11104. else if (c == '!')
  11105. { c = soap_getchar(soap);
  11106. if (c == '[')
  11107. { do c = soap_getchar(soap);
  11108. while ((int)c != EOF && c != '[');
  11109. if ((int)c == EOF)
  11110. goto end;
  11111. t = (char*)"![CDATA[";
  11112. m = 8;
  11113. state = 1;
  11114. }
  11115. else if (c == '-')
  11116. { if ((c = soap_getchar(soap)) == '-')
  11117. state = 2;
  11118. t = (char*)"!-";
  11119. m = 2;
  11120. soap_unget(soap, c);
  11121. }
  11122. else
  11123. { t = (char*)"!";
  11124. m = 1;
  11125. soap_unget(soap, c);
  11126. }
  11127. *s++ = '<';
  11128. break;
  11129. }
  11130. else if (c == '?')
  11131. state = 3;
  11132. else if (f && n == 0)
  11133. { soap_revget1(soap);
  11134. c = '<';
  11135. goto end;
  11136. }
  11137. else
  11138. n++;
  11139. soap_unget(soap, c);
  11140. *s++ = '<';
  11141. break;
  11142. case '>':
  11143. *s++ = '>';
  11144. break;
  11145. case '"':
  11146. *s++ = '"';
  11147. break;
  11148. default:
  11149. #ifndef WITH_LEANER
  11150. #ifdef HAVE_WCTOMB
  11151. if (soap->mode & SOAP_C_MBSTRING)
  11152. { m = wctomb(buf, (wchar_t)(c & 0x7FFFFFFF));
  11153. if (m >= 1 && m <= (int)MB_CUR_MAX)
  11154. { t = buf;
  11155. *s++ = *t++;
  11156. m--;
  11157. }
  11158. else
  11159. { *s++ = SOAP_UNKNOWN_CHAR;
  11160. m = 0;
  11161. }
  11162. }
  11163. else
  11164. #endif
  11165. #endif
  11166. *s++ = (char)(c & 0xFF);
  11167. }
  11168. l++;
  11169. if (maxlen >= 0 && l > maxlen)
  11170. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  11171. soap->error = SOAP_LENGTH;
  11172. return NULL;
  11173. }
  11174. }
  11175. }
  11176. }
  11177. #endif
  11178. #ifdef WITH_FAST
  11179. soap->labidx = 0; /* use look-aside buffer */
  11180. #else
  11181. if (soap_new_block(soap) == NULL)
  11182. return NULL;
  11183. #endif
  11184. for (;;)
  11185. {
  11186. #ifdef WITH_FAST
  11187. register size_t k;
  11188. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  11189. return NULL;
  11190. s = soap->labbuf + soap->labidx; /* space to populate */
  11191. k = soap->lablen - soap->labidx; /* number of bytes available */
  11192. soap->labidx = soap->lablen; /* claim this space */
  11193. #else
  11194. register size_t k = SOAP_BLKLEN;
  11195. if (!(s = (char*)soap_push_block(soap, NULL, k)))
  11196. return NULL;
  11197. #endif
  11198. for (i = 0; i < k; i++)
  11199. { if (m > 0)
  11200. { *s++ = *t++; /* copy multibyte characters */
  11201. m--;
  11202. continue;
  11203. }
  11204. #ifndef WITH_CDATA
  11205. if (!flag)
  11206. c = soap_getchar(soap);
  11207. else
  11208. #endif
  11209. if ((soap->mode & SOAP_C_UTFSTRING))
  11210. { if (((c = soap_get(soap)) & 0x80000000) && c >= -0x7FFFFF80 && c < SOAP_AP)
  11211. { c &= 0x7FFFFFFF;
  11212. t = buf;
  11213. if (c < 0x0800)
  11214. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  11215. else
  11216. { if (c < 0x010000)
  11217. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  11218. else
  11219. { if (c < 0x200000)
  11220. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  11221. else
  11222. { if (c < 0x04000000)
  11223. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  11224. else
  11225. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  11226. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  11227. }
  11228. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  11229. }
  11230. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  11231. }
  11232. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  11233. }
  11234. *t++ = (char)(0x80 | (c & 0x3F));
  11235. m = (int)(t - buf) - 1;
  11236. t = buf;
  11237. *s++ = *t++;
  11238. continue;
  11239. }
  11240. }
  11241. else
  11242. c = soap_getutf8(soap);
  11243. switch (c)
  11244. {
  11245. case SOAP_TT:
  11246. if (n == 0)
  11247. goto end;
  11248. n--;
  11249. *s++ = '<';
  11250. t = (char*)"/";
  11251. m = 1;
  11252. break;
  11253. case SOAP_LT:
  11254. if (f && n == 0)
  11255. goto end;
  11256. n++;
  11257. *s++ = '<';
  11258. break;
  11259. case SOAP_GT:
  11260. *s++ = '>';
  11261. break;
  11262. case SOAP_QT:
  11263. *s++ = '"';
  11264. break;
  11265. case SOAP_AP:
  11266. *s++ = '\'';
  11267. break;
  11268. case '/':
  11269. if (n > 0)
  11270. { if (!flag)
  11271. { c = soap_getchar(soap);
  11272. if (c == '>')
  11273. n--;
  11274. }
  11275. else
  11276. { c = soap_get(soap);
  11277. if (c == SOAP_GT)
  11278. n--;
  11279. }
  11280. soap_unget(soap, c);
  11281. }
  11282. *s++ = '/';
  11283. break;
  11284. case (soap_wchar)('<' | 0x80000000):
  11285. if (flag)
  11286. *s++ = '<';
  11287. else
  11288. { *s++ = '&';
  11289. t = (char*)"lt;";
  11290. m = 3;
  11291. }
  11292. break;
  11293. case (soap_wchar)('>' | 0x80000000):
  11294. if (flag)
  11295. *s++ = '>';
  11296. else
  11297. { *s++ = '&';
  11298. t = (char*)"gt;";
  11299. m = 3;
  11300. }
  11301. break;
  11302. case (soap_wchar)('&' | 0x80000000):
  11303. if (flag)
  11304. *s++ = '&';
  11305. else
  11306. { *s++ = '&';
  11307. t = (char*)"amp;";
  11308. m = 4;
  11309. }
  11310. break;
  11311. case (soap_wchar)('"' | 0x80000000):
  11312. if (flag)
  11313. *s++ = '"';
  11314. else
  11315. { *s++ = '&';
  11316. t = (char*)"quot;";
  11317. m = 5;
  11318. }
  11319. break;
  11320. case (soap_wchar)('\'' | 0x80000000):
  11321. if (flag)
  11322. *s++ = '\'';
  11323. else
  11324. { *s++ = '&';
  11325. t = (char*)"apos;";
  11326. m = 5;
  11327. }
  11328. break;
  11329. default:
  11330. if ((int)c == EOF)
  11331. goto end;
  11332. #ifndef WITH_CDATA
  11333. if (c == '<' && !flag)
  11334. { if (f && n == 0)
  11335. goto end;
  11336. c = soap_getchar(soap);
  11337. soap_unget(soap, c);
  11338. if (c == '/')
  11339. { c = SOAP_TT;
  11340. if (n == 0)
  11341. goto end;
  11342. n--;
  11343. }
  11344. else
  11345. n++;
  11346. *s++ = '<';
  11347. break;
  11348. }
  11349. else
  11350. #endif
  11351. #ifndef WITH_LEANER
  11352. #ifdef HAVE_WCTOMB
  11353. if (soap->mode & SOAP_C_MBSTRING)
  11354. { m = wctomb(buf, (wchar_t)(c & 0x7FFFFFFF));
  11355. if (m >= 1 && m <= (int)MB_CUR_MAX)
  11356. { t = buf;
  11357. *s++ = *t++;
  11358. m--;
  11359. }
  11360. else
  11361. { *s++ = SOAP_UNKNOWN_CHAR;
  11362. m = 0;
  11363. }
  11364. }
  11365. else
  11366. #endif
  11367. #endif
  11368. *s++ = (char)(c & 0xFF);
  11369. }
  11370. l++;
  11371. if (maxlen >= 0 && l > maxlen)
  11372. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  11373. soap->error = SOAP_LENGTH;
  11374. return NULL;
  11375. }
  11376. }
  11377. }
  11378. end:
  11379. soap_unget(soap, c);
  11380. *s = '\0';
  11381. #ifdef WITH_FAST
  11382. t = soap_strdup(soap, soap->labbuf);
  11383. #else
  11384. soap_size_block(soap, NULL, i + 1);
  11385. t = soap_save_block(soap, NULL, 0);
  11386. #endif
  11387. if (l < minlen)
  11388. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too short: %ld chars, minlen=%ld\n", l, minlen));
  11389. soap->error = SOAP_LENGTH;
  11390. return NULL;
  11391. }
  11392. #ifdef WITH_DOM
  11393. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  11394. { if (flag == 3)
  11395. soap->dom->tail = t;
  11396. else
  11397. soap->dom->data = t;
  11398. }
  11399. #endif
  11400. if (flag == 2)
  11401. if (soap_s2QName(soap, t, &t, minlen, maxlen))
  11402. return NULL;
  11403. return t;
  11404. }
  11405. #endif
  11406. /******************************************************************************/
  11407. #ifndef WITH_LEANER
  11408. #ifndef PALM_2
  11409. SOAP_FMAC1
  11410. int
  11411. SOAP_FMAC2
  11412. soap_wstring_out(struct soap *soap, const wchar_t *s, int flag)
  11413. { const char *t;
  11414. char tmp;
  11415. register soap_wchar c;
  11416. #ifdef WITH_DOM
  11417. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  11418. { wchar_t *r = (wchar_t*)s;
  11419. int n = 1;
  11420. while (*r++)
  11421. n++;
  11422. soap->dom->wide = r = (wchar_t*)soap_malloc(soap, n * sizeof(wchar_t));
  11423. while (n--)
  11424. *r++ = *s++;
  11425. return SOAP_OK;
  11426. }
  11427. #endif
  11428. while ((c = *s++))
  11429. { switch (c)
  11430. {
  11431. case 0x09:
  11432. if (flag)
  11433. t = "&#x9;";
  11434. else
  11435. t = "\t";
  11436. break;
  11437. case 0x0A:
  11438. if (flag || !(soap->mode & SOAP_XML_CANONICAL))
  11439. t = "&#xA;";
  11440. else
  11441. t = "\n";
  11442. break;
  11443. case 0x0D:
  11444. t = "&#xD;";
  11445. break;
  11446. case '&':
  11447. t = "&amp;";
  11448. break;
  11449. case '<':
  11450. t = "&lt;";
  11451. break;
  11452. case '>':
  11453. if (flag)
  11454. t = ">";
  11455. else
  11456. t = "&gt;";
  11457. break;
  11458. case '"':
  11459. if (flag)
  11460. t = "&quot;";
  11461. else
  11462. t = "\"";
  11463. break;
  11464. default:
  11465. if (c >= 0x20 && c < 0x80)
  11466. { tmp = (char)c;
  11467. if (soap_send_raw(soap, &tmp, 1))
  11468. return soap->error;
  11469. }
  11470. else
  11471. { /* check for UTF16 encoding when wchar_t is too small to hold UCS */
  11472. if (sizeof(wchar_t) < 4 && (c & 0xFC00) == 0xD800)
  11473. { register soap_wchar d = *s++;
  11474. if ((d & 0xFC00) == 0xDC00)
  11475. c = ((c - 0xD800) << 10) + (d - 0xDC00) + 0x10000;
  11476. else
  11477. c = 0xFFFD; /* Malformed */
  11478. }
  11479. if (soap_pututf8(soap, (unsigned long)c))
  11480. return soap->error;
  11481. }
  11482. continue;
  11483. }
  11484. if (soap_send(soap, t))
  11485. return soap->error;
  11486. }
  11487. return SOAP_OK;
  11488. }
  11489. #endif
  11490. #endif
  11491. /******************************************************************************/
  11492. #ifndef WITH_LEANER
  11493. #ifndef PALM_2
  11494. SOAP_FMAC1
  11495. wchar_t *
  11496. SOAP_FMAC2
  11497. soap_wstring_in(struct soap *soap, int flag, long minlen, long maxlen)
  11498. { wchar_t *s;
  11499. register int i, n = 0, f = 0;
  11500. register long l = 0;
  11501. register soap_wchar c;
  11502. char *t = NULL;
  11503. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Reading wide string content\n"));
  11504. if (soap->peeked)
  11505. { if (*soap->tag)
  11506. {
  11507. #ifndef WITH_LEAN
  11508. struct soap_attribute *tp;
  11509. t = soap->tmpbuf;
  11510. *t = '<';
  11511. strncpy(t + 1, soap->tag, sizeof(soap->tmpbuf) - 2);
  11512. t[sizeof(soap->tmpbuf) - 1] = '\0';
  11513. t += strlen(t);
  11514. for (tp = soap->attributes; tp; tp = tp->next)
  11515. { if (tp->visible)
  11516. { if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  11517. break;
  11518. *t++ = ' ';
  11519. strcpy(t, tp->name);
  11520. t += strlen(t);
  11521. if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  11522. break;
  11523. if (tp->value)
  11524. { *t++ = '=';
  11525. *t++ = '"';
  11526. strcpy(t, tp->value);
  11527. t += strlen(t);
  11528. *t++ = '"';
  11529. }
  11530. }
  11531. }
  11532. if (!soap->body)
  11533. *t++ = '/';
  11534. *t++ = '>';
  11535. *t = '\0';
  11536. t = soap->tmpbuf;
  11537. #endif
  11538. if (soap->body)
  11539. n = 1;
  11540. f = 1;
  11541. soap->peeked = 0;
  11542. }
  11543. }
  11544. if (soap_new_block(soap) == NULL)
  11545. return NULL;
  11546. for (;;)
  11547. { if (!(s = (wchar_t*)soap_push_block(soap, NULL, sizeof(wchar_t)*SOAP_BLKLEN)))
  11548. return NULL;
  11549. for (i = 0; i < SOAP_BLKLEN; i++)
  11550. { if (t)
  11551. { *s++ = (wchar_t)*t++;
  11552. if (!*t)
  11553. t = NULL;
  11554. continue;
  11555. }
  11556. c = soap_getutf8(soap);
  11557. switch (c)
  11558. {
  11559. case SOAP_TT:
  11560. if (n == 0)
  11561. goto end;
  11562. n--;
  11563. *s++ = '<';
  11564. soap_unget(soap, '/');
  11565. break;
  11566. case SOAP_LT:
  11567. if (f && n == 0)
  11568. goto end;
  11569. n++;
  11570. *s++ = '<';
  11571. break;
  11572. case SOAP_GT:
  11573. *s++ = '>';
  11574. break;
  11575. case SOAP_QT:
  11576. *s++ = '"';
  11577. break;
  11578. case SOAP_AP:
  11579. *s++ = '\'';
  11580. break;
  11581. case '/':
  11582. if (n > 0)
  11583. { c = soap_getutf8(soap);
  11584. if (c == SOAP_GT)
  11585. n--;
  11586. soap_unget(soap, c);
  11587. }
  11588. *s++ = '/';
  11589. break;
  11590. case '<':
  11591. if (flag)
  11592. *s++ = (soap_wchar)'<';
  11593. else
  11594. { *s++ = (soap_wchar)'&';
  11595. t = (char*)"lt;";
  11596. }
  11597. break;
  11598. case '>':
  11599. if (flag)
  11600. *s++ = (soap_wchar)'>';
  11601. else
  11602. { *s++ = (soap_wchar)'&';
  11603. t = (char*)"gt;";
  11604. }
  11605. break;
  11606. case '"':
  11607. if (flag)
  11608. *s++ = (soap_wchar)'"';
  11609. else
  11610. { *s++ = (soap_wchar)'&';
  11611. t = (char*)"quot;";
  11612. }
  11613. break;
  11614. default:
  11615. if ((int)c == EOF)
  11616. goto end;
  11617. /* use UTF16 encoding when wchar_t is too small to hold UCS */
  11618. if (sizeof(wchar_t) < 4 && c > 0xFFFF)
  11619. { register soap_wchar c1, c2;
  11620. c1 = 0xD800 - (0x10000 >> 10) + (c >> 10);
  11621. c2 = 0xDC00 + (c & 0x3FF);
  11622. c = c1;
  11623. soap_unget(soap, c2);
  11624. }
  11625. *s++ = (wchar_t)c & 0x7FFFFFFF;
  11626. }
  11627. l++;
  11628. if (maxlen >= 0 && l > maxlen)
  11629. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  11630. soap->error = SOAP_LENGTH;
  11631. return NULL;
  11632. }
  11633. }
  11634. }
  11635. end:
  11636. soap_unget(soap, c);
  11637. *s = '\0';
  11638. soap_size_block(soap, NULL, sizeof(wchar_t) * (i + 1));
  11639. if (l < minlen)
  11640. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too short: %ld chars, minlen=%ld\n", l, minlen));
  11641. soap->error = SOAP_LENGTH;
  11642. return NULL;
  11643. }
  11644. s = (wchar_t*)soap_save_block(soap, NULL, NULL, 0);
  11645. #ifdef WITH_DOM
  11646. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  11647. soap->dom->wide = s;
  11648. #endif
  11649. return s;
  11650. }
  11651. #endif
  11652. #endif
  11653. /******************************************************************************/
  11654. #ifndef PALM_2
  11655. SOAP_FMAC1
  11656. const char*
  11657. SOAP_FMAC2
  11658. soap_int2s(struct soap *soap, int n)
  11659. { return soap_long2s(soap, (long)n);
  11660. }
  11661. #endif
  11662. /******************************************************************************/
  11663. #ifndef PALM_2
  11664. SOAP_FMAC1
  11665. int
  11666. SOAP_FMAC2
  11667. soap_outint(struct soap *soap, const char *tag, int id, const int *p, const char *type, int n)
  11668. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11669. || soap_string_out(soap, soap_long2s(soap, (long)*p), 0))
  11670. return soap->error;
  11671. return soap_element_end_out(soap, tag);
  11672. }
  11673. #endif
  11674. /******************************************************************************/
  11675. #ifndef PALM_2
  11676. SOAP_FMAC1
  11677. int
  11678. SOAP_FMAC2
  11679. soap_s2int(struct soap *soap, const char *s, int *p)
  11680. { if (s)
  11681. { long n;
  11682. char *r;
  11683. #ifndef WITH_NOIO
  11684. #ifndef WITH_LEAN
  11685. soap_reset_errno;
  11686. #endif
  11687. #endif
  11688. n = soap_strtol(s, &r, 10);
  11689. if (s == r || *r
  11690. #ifndef WITH_LEAN
  11691. || n != (int)n
  11692. #endif
  11693. #ifndef WITH_NOIO
  11694. #ifndef WITH_LEAN
  11695. || soap_errno == SOAP_ERANGE
  11696. #endif
  11697. #endif
  11698. )
  11699. soap->error = SOAP_TYPE;
  11700. *p = (int)n;
  11701. }
  11702. return soap->error;
  11703. }
  11704. #endif
  11705. /******************************************************************************/
  11706. #ifndef PALM_2
  11707. SOAP_FMAC1
  11708. int *
  11709. SOAP_FMAC2
  11710. soap_inint(struct soap *soap, const char *tag, int *p, const char *type, int t)
  11711. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11712. return NULL;
  11713. #ifndef WITH_LEAN
  11714. if (*soap->type
  11715. && soap_match_tag(soap, soap->type, type)
  11716. && soap_match_tag(soap, soap->type, ":int")
  11717. && soap_match_tag(soap, soap->type, ":short")
  11718. && soap_match_tag(soap, soap->type, ":byte"))
  11719. { soap->error = SOAP_TYPE;
  11720. soap_revert(soap);
  11721. return NULL;
  11722. }
  11723. #endif
  11724. p = (int*)soap_id_enter(soap, soap->id, p, t, sizeof(int), 0, NULL, NULL, NULL);
  11725. if (*soap->href)
  11726. p = (int*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(int), 0, NULL);
  11727. else if (p)
  11728. { if (soap_s2int(soap, soap_value(soap), p))
  11729. return NULL;
  11730. }
  11731. if (soap->body && soap_element_end_in(soap, tag))
  11732. return NULL;
  11733. return p;
  11734. }
  11735. #endif
  11736. /******************************************************************************/
  11737. #ifndef PALM_2
  11738. SOAP_FMAC1
  11739. const char*
  11740. SOAP_FMAC2
  11741. soap_long2s(struct soap *soap, long n)
  11742. {
  11743. #ifdef HAVE_SNPRINTF
  11744. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%ld", n);
  11745. #else
  11746. sprintf(soap->tmpbuf, "%ld", n);
  11747. #endif
  11748. return soap->tmpbuf;
  11749. }
  11750. #endif
  11751. /******************************************************************************/
  11752. #ifndef PALM_2
  11753. SOAP_FMAC1
  11754. int
  11755. SOAP_FMAC2
  11756. soap_outlong(struct soap *soap, const char *tag, int id, const long *p, const char *type, int n)
  11757. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11758. || soap_string_out(soap, soap_long2s(soap, *p), 0))
  11759. return soap->error;
  11760. return soap_element_end_out(soap, tag);
  11761. }
  11762. #endif
  11763. /******************************************************************************/
  11764. #ifndef PALM_2
  11765. SOAP_FMAC1
  11766. int
  11767. SOAP_FMAC2
  11768. soap_s2long(struct soap *soap, const char *s, long *p)
  11769. { if (s)
  11770. { char *r;
  11771. #ifndef WITH_NOIO
  11772. #ifndef WITH_LEAN
  11773. soap_reset_errno;
  11774. #endif
  11775. #endif
  11776. *p = soap_strtol(s, &r, 10);
  11777. if (s == r || *r
  11778. #ifndef WITH_NOIO
  11779. #ifndef WITH_LEAN
  11780. || soap_errno == SOAP_ERANGE
  11781. #endif
  11782. #endif
  11783. )
  11784. soap->error = SOAP_TYPE;
  11785. }
  11786. return soap->error;
  11787. }
  11788. #endif
  11789. /******************************************************************************/
  11790. #ifndef PALM_2
  11791. SOAP_FMAC1
  11792. long *
  11793. SOAP_FMAC2
  11794. soap_inlong(struct soap *soap, const char *tag, long *p, const char *type, int t)
  11795. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11796. return NULL;
  11797. #ifndef WITH_LEAN
  11798. if (*soap->type
  11799. && soap_match_tag(soap, soap->type, type)
  11800. && soap_match_tag(soap, soap->type, ":int")
  11801. && soap_match_tag(soap, soap->type, ":short")
  11802. && soap_match_tag(soap, soap->type, ":byte"))
  11803. { soap->error = SOAP_TYPE;
  11804. soap_revert(soap);
  11805. return NULL;
  11806. }
  11807. #endif
  11808. p = (long*)soap_id_enter(soap, soap->id, p, t, sizeof(long), 0, NULL, NULL, NULL);
  11809. if (*soap->href)
  11810. p = (long*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(long), 0, NULL);
  11811. else if (p)
  11812. { if (soap_s2long(soap, soap_value(soap), p))
  11813. return NULL;
  11814. }
  11815. if (soap->body && soap_element_end_in(soap, tag))
  11816. return NULL;
  11817. return p;
  11818. }
  11819. #endif
  11820. /******************************************************************************/
  11821. #ifndef WITH_LEAN
  11822. SOAP_FMAC1
  11823. const char*
  11824. SOAP_FMAC2
  11825. soap_LONG642s(struct soap *soap, LONG64 n)
  11826. {
  11827. #ifdef HAVE_SNPRINTF
  11828. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), SOAP_LONG_FORMAT, n);
  11829. #else
  11830. sprintf(soap->tmpbuf, SOAP_LONG_FORMAT, n);
  11831. #endif
  11832. return soap->tmpbuf;
  11833. }
  11834. #endif
  11835. /******************************************************************************/
  11836. #ifndef WITH_LEAN
  11837. SOAP_FMAC1
  11838. int
  11839. SOAP_FMAC2
  11840. soap_outLONG64(struct soap *soap, const char *tag, int id, const LONG64 *p, const char *type, int n)
  11841. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11842. || soap_string_out(soap, soap_LONG642s(soap, *p), 0))
  11843. return soap->error;
  11844. return soap_element_end_out(soap, tag);
  11845. }
  11846. #endif
  11847. /******************************************************************************/
  11848. #ifndef WITH_LEAN
  11849. SOAP_FMAC1
  11850. int
  11851. SOAP_FMAC2
  11852. soap_s2LONG64(struct soap *soap, const char *s, LONG64 *p)
  11853. { if (s)
  11854. {
  11855. #ifdef HAVE_STRTOLL
  11856. char *r;
  11857. #ifndef WITH_NOIO
  11858. #ifndef WITH_LEAN
  11859. soap_reset_errno;
  11860. #endif
  11861. #endif
  11862. *p = soap_strtoll(s, &r, 10);
  11863. if (s == r || *r
  11864. #ifndef WITH_NOIO
  11865. #ifndef WITH_LEAN
  11866. || soap_errno == SOAP_ERANGE
  11867. #endif
  11868. #endif
  11869. )
  11870. #else
  11871. # ifdef HAVE_SSCANF
  11872. if (sscanf(s, SOAP_LONG_FORMAT, p) != 1)
  11873. # endif
  11874. #endif
  11875. soap->error = SOAP_TYPE;
  11876. }
  11877. return soap->error;
  11878. }
  11879. #endif
  11880. /******************************************************************************/
  11881. #ifndef WITH_LEAN
  11882. SOAP_FMAC1
  11883. LONG64 *
  11884. SOAP_FMAC2
  11885. soap_inLONG64(struct soap *soap, const char *tag, LONG64 *p, const char *type, int t)
  11886. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11887. return NULL;
  11888. #ifndef WITH_LEAN
  11889. if (*soap->type
  11890. && soap_match_tag(soap, soap->type, type)
  11891. && soap_match_tag(soap, soap->type, ":integer")
  11892. && soap_match_tag(soap, soap->type, ":positiveInteger")
  11893. && soap_match_tag(soap, soap->type, ":negativeInteger")
  11894. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  11895. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  11896. && soap_match_tag(soap, soap->type, ":long")
  11897. && soap_match_tag(soap, soap->type, ":int")
  11898. && soap_match_tag(soap, soap->type, ":short")
  11899. && soap_match_tag(soap, soap->type, ":byte"))
  11900. { soap->error = SOAP_TYPE;
  11901. soap_revert(soap);
  11902. return NULL;
  11903. }
  11904. #endif
  11905. p = (LONG64*)soap_id_enter(soap, soap->id, p, t, sizeof(LONG64), 0, NULL, NULL, NULL);
  11906. if (*soap->href)
  11907. p = (LONG64*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(LONG64), 0, NULL);
  11908. else if (p)
  11909. { if (soap_s2LONG64(soap, soap_value(soap), p))
  11910. return NULL;
  11911. }
  11912. if (soap->body && soap_element_end_in(soap, tag))
  11913. return NULL;
  11914. return p;
  11915. }
  11916. #endif
  11917. /******************************************************************************/
  11918. #ifndef PALM_2
  11919. SOAP_FMAC1
  11920. const char*
  11921. SOAP_FMAC2
  11922. soap_byte2s(struct soap *soap, char n)
  11923. { return soap_long2s(soap, (long)n);
  11924. }
  11925. #endif
  11926. /******************************************************************************/
  11927. #ifndef PALM_2
  11928. SOAP_FMAC1
  11929. int
  11930. SOAP_FMAC2
  11931. soap_outbyte(struct soap *soap, const char *tag, int id, const char *p, const char *type, int n)
  11932. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11933. || soap_string_out(soap, soap_long2s(soap, (long)*p), 0))
  11934. return soap->error;
  11935. return soap_element_end_out(soap, tag);
  11936. }
  11937. #endif
  11938. /******************************************************************************/
  11939. #ifndef PALM_2
  11940. SOAP_FMAC1
  11941. int
  11942. SOAP_FMAC2
  11943. soap_s2byte(struct soap *soap, const char *s, char *p)
  11944. { if (s)
  11945. { long n;
  11946. char *r;
  11947. n = soap_strtol(s, &r, 10);
  11948. if (s == r || *r || n < -128 || n > 127)
  11949. soap->error = SOAP_TYPE;
  11950. *p = (char)n;
  11951. }
  11952. return soap->error;
  11953. }
  11954. #endif
  11955. /******************************************************************************/
  11956. #ifndef PALM_2
  11957. SOAP_FMAC1
  11958. char *
  11959. SOAP_FMAC2
  11960. soap_inbyte(struct soap *soap, const char *tag, char *p, const char *type, int t)
  11961. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11962. return NULL;
  11963. #ifndef WITH_LEAN
  11964. if (*soap->type
  11965. && soap_match_tag(soap, soap->type, type)
  11966. && soap_match_tag(soap, soap->type, ":byte"))
  11967. { soap->error = SOAP_TYPE;
  11968. soap_revert(soap);
  11969. return NULL;
  11970. }
  11971. #endif
  11972. p = (char*)soap_id_enter(soap, soap->id, p, t, sizeof(char), 0, NULL, NULL, NULL);
  11973. if (*soap->href)
  11974. p = (char*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(char), 0, NULL);
  11975. else if (p)
  11976. { if (soap_s2byte(soap, soap_value(soap), p))
  11977. return NULL;
  11978. }
  11979. if (soap->body && soap_element_end_in(soap, tag))
  11980. return NULL;
  11981. return p;
  11982. }
  11983. #endif
  11984. /******************************************************************************/
  11985. #ifndef PALM_2
  11986. SOAP_FMAC1
  11987. const char*
  11988. SOAP_FMAC2
  11989. soap_short2s(struct soap *soap, short n)
  11990. { return soap_long2s(soap, (long)n);
  11991. }
  11992. #endif
  11993. /******************************************************************************/
  11994. #ifndef PALM_2
  11995. SOAP_FMAC1
  11996. int
  11997. SOAP_FMAC2
  11998. soap_outshort(struct soap *soap, const char *tag, int id, const short *p, const char *type, int n)
  11999. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12000. || soap_string_out(soap, soap_long2s(soap, (long)*p), 0))
  12001. return soap->error;
  12002. return soap_element_end_out(soap, tag);
  12003. }
  12004. #endif
  12005. /******************************************************************************/
  12006. #ifndef PALM_2
  12007. SOAP_FMAC1
  12008. int
  12009. SOAP_FMAC2
  12010. soap_s2short(struct soap *soap, const char *s, short *p)
  12011. { if (s)
  12012. { long n;
  12013. char *r;
  12014. n = soap_strtol(s, &r, 10);
  12015. if (s == r || *r || n < -32768 || n > 32767)
  12016. soap->error = SOAP_TYPE;
  12017. *p = (short)n;
  12018. }
  12019. return soap->error;
  12020. }
  12021. #endif
  12022. /******************************************************************************/
  12023. #ifndef PALM_2
  12024. SOAP_FMAC1
  12025. short *
  12026. SOAP_FMAC2
  12027. soap_inshort(struct soap *soap, const char *tag, short *p, const char *type, int t)
  12028. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12029. return NULL;
  12030. #ifndef WITH_LEAN
  12031. if (*soap->type
  12032. && soap_match_tag(soap, soap->type, type)
  12033. && soap_match_tag(soap, soap->type, ":short")
  12034. && soap_match_tag(soap, soap->type, ":byte"))
  12035. { soap->error = SOAP_TYPE;
  12036. soap_revert(soap);
  12037. return NULL;
  12038. }
  12039. #endif
  12040. p = (short*)soap_id_enter(soap, soap->id, p, t, sizeof(short), 0, NULL, NULL, NULL);
  12041. if (*soap->href)
  12042. p = (short*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(short), 0, NULL);
  12043. else if (p)
  12044. { if (soap_s2short(soap, soap_value(soap), p))
  12045. return NULL;
  12046. }
  12047. if (soap->body && soap_element_end_in(soap, tag))
  12048. return NULL;
  12049. return p;
  12050. }
  12051. #endif
  12052. /******************************************************************************/
  12053. #ifndef PALM_2
  12054. SOAP_FMAC1
  12055. const char*
  12056. SOAP_FMAC2
  12057. soap_float2s(struct soap *soap, float n)
  12058. { char *s;
  12059. if (soap_isnan((double)n))
  12060. return "NaN";
  12061. if (soap_ispinff(n))
  12062. return "INF";
  12063. if (soap_isninff(n))
  12064. return "-INF";
  12065. #if defined(HAVE_SPRINTF_L)
  12066. # ifdef WIN32
  12067. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->float_format, soap->c_locale, n);
  12068. # else
  12069. sprintf_l(soap->tmpbuf, soap->c_locale, soap->float_format, n);
  12070. # endif
  12071. #else
  12072. # ifdef HAVE_SNPRINTF
  12073. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), soap->float_format, n);
  12074. # else
  12075. sprintf(soap->tmpbuf, soap->float_format, n);
  12076. # endif
  12077. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  12078. if (s)
  12079. *s = '.';
  12080. #endif
  12081. return soap->tmpbuf;
  12082. }
  12083. #endif
  12084. /******************************************************************************/
  12085. #ifndef PALM_2
  12086. SOAP_FMAC1
  12087. int
  12088. SOAP_FMAC2
  12089. soap_outfloat(struct soap *soap, const char *tag, int id, const float *p, const char *type, int n)
  12090. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12091. || soap_string_out(soap, soap_float2s(soap, *p), 0))
  12092. return soap->error;
  12093. return soap_element_end_out(soap, tag);
  12094. }
  12095. #endif
  12096. /******************************************************************************/
  12097. #ifndef PALM_2
  12098. SOAP_FMAC1
  12099. int
  12100. SOAP_FMAC2
  12101. soap_s2float(struct soap *soap, const char *s, float *p)
  12102. { if (s)
  12103. { if (!*s)
  12104. return soap->error = SOAP_TYPE;
  12105. if (!soap_tag_cmp(s, "INF"))
  12106. *p = FLT_PINFTY;
  12107. else if (!soap_tag_cmp(s, "+INF"))
  12108. *p = FLT_PINFTY;
  12109. else if (!soap_tag_cmp(s, "-INF"))
  12110. *p = FLT_NINFTY;
  12111. else if (!soap_tag_cmp(s, "NaN"))
  12112. *p = FLT_NAN;
  12113. else
  12114. {
  12115. /* On some systems strtof requires -std=c99 or does not even link: so we try to use strtod first */
  12116. #if defined(HAVE_STRTOD_L)
  12117. char *r;
  12118. # ifdef WIN32
  12119. *p = (float)_strtod_l(s, &r, soap->c_locale);
  12120. # else
  12121. *p = (float)strtod_l(s, &r, soap->c_locale);
  12122. # endif
  12123. if (*r)
  12124. #elif defined(HAVE_STRTOD)
  12125. char *r;
  12126. *p = (float)strtod(s, &r);
  12127. if (*r)
  12128. #elif defined(HAVE_STRTOF_L)
  12129. char *r;
  12130. *p = strtof_l((char*)s, &r, soap->c_locale);
  12131. if (*r)
  12132. #elif defined(HAVE_STRTOF)
  12133. char *r;
  12134. *p = strtof((char*)s, &r);
  12135. if (*r)
  12136. #endif
  12137. {
  12138. #if defined(HAVE_SSCANF_L) && !defined(HAVE_STRTOF_L) && !defined(HAVE_STRTOD_L)
  12139. if (sscanf_l(s, soap->c_locale, "%f", p) != 1)
  12140. soap->error = SOAP_TYPE;
  12141. #elif defined(HAVE_SSCANF)
  12142. if (sscanf(s, "%f", p) != 1)
  12143. soap->error = SOAP_TYPE;
  12144. #else
  12145. soap->error = SOAP_TYPE;
  12146. #endif
  12147. }
  12148. }
  12149. }
  12150. return soap->error;
  12151. }
  12152. #endif
  12153. /******************************************************************************/
  12154. #ifndef WITH_LEAN
  12155. static int soap_isnumeric(struct soap *soap, const char *type)
  12156. { if (soap_match_tag(soap, soap->type, type)
  12157. && soap_match_tag(soap, soap->type, ":float")
  12158. && soap_match_tag(soap, soap->type, ":double")
  12159. && soap_match_tag(soap, soap->type, ":decimal")
  12160. && soap_match_tag(soap, soap->type, ":integer")
  12161. && soap_match_tag(soap, soap->type, ":positiveInteger")
  12162. && soap_match_tag(soap, soap->type, ":negativeInteger")
  12163. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  12164. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  12165. && soap_match_tag(soap, soap->type, ":long")
  12166. && soap_match_tag(soap, soap->type, ":int")
  12167. && soap_match_tag(soap, soap->type, ":short")
  12168. && soap_match_tag(soap, soap->type, ":byte")
  12169. && soap_match_tag(soap, soap->type, ":unsignedLong")
  12170. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12171. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12172. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12173. { soap->error = SOAP_TYPE;
  12174. soap_revert(soap);
  12175. return SOAP_ERR;
  12176. }
  12177. return SOAP_OK;
  12178. }
  12179. #endif
  12180. /******************************************************************************/
  12181. #ifndef PALM_2
  12182. SOAP_FMAC1
  12183. float *
  12184. SOAP_FMAC2
  12185. soap_infloat(struct soap *soap, const char *tag, float *p, const char *type, int t)
  12186. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12187. return NULL;
  12188. #ifndef WITH_LEAN
  12189. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  12190. return NULL;
  12191. #endif
  12192. p = (float*)soap_id_enter(soap, soap->id, p, t, sizeof(float), 0, NULL, NULL, NULL);
  12193. if (*soap->href)
  12194. p = (float*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(float), 0, NULL);
  12195. else if (p)
  12196. { if (soap_s2float(soap, soap_value(soap), p))
  12197. return NULL;
  12198. }
  12199. if (soap->body && soap_element_end_in(soap, tag))
  12200. return NULL;
  12201. return p;
  12202. }
  12203. #endif
  12204. /******************************************************************************/
  12205. #ifndef PALM_2
  12206. SOAP_FMAC1
  12207. const char*
  12208. SOAP_FMAC2
  12209. soap_double2s(struct soap *soap, double n)
  12210. { char *s;
  12211. if (soap_isnan(n))
  12212. return "NaN";
  12213. if (soap_ispinfd(n))
  12214. return "INF";
  12215. if (soap_isninfd(n))
  12216. return "-INF";
  12217. #if defined(HAVE_SPRINTF_L)
  12218. # ifdef WIN32
  12219. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->double_format, soap->c_locale, n);
  12220. # else
  12221. sprintf_l(soap->tmpbuf, soap->c_locale, soap->double_format, n);
  12222. # endif
  12223. #else
  12224. # ifdef HAVE_SNPRINTF
  12225. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), soap->double_format, n);
  12226. #else
  12227. sprintf(soap->tmpbuf, soap->double_format, n);
  12228. #endif
  12229. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  12230. if (s)
  12231. *s = '.';
  12232. #endif
  12233. return soap->tmpbuf;
  12234. }
  12235. #endif
  12236. /******************************************************************************/
  12237. #ifndef PALM_2
  12238. SOAP_FMAC1
  12239. int
  12240. SOAP_FMAC2
  12241. soap_outdouble(struct soap *soap, const char *tag, int id, const double *p, const char *type, int n)
  12242. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12243. || soap_string_out(soap, soap_double2s(soap, *p), 0))
  12244. return soap->error;
  12245. return soap_element_end_out(soap, tag);
  12246. }
  12247. #endif
  12248. /******************************************************************************/
  12249. #ifndef PALM_2
  12250. SOAP_FMAC1
  12251. int
  12252. SOAP_FMAC2
  12253. soap_s2double(struct soap *soap, const char *s, double *p)
  12254. { if (s)
  12255. { if (!*s)
  12256. return soap->error = SOAP_TYPE;
  12257. if (!soap_tag_cmp(s, "INF"))
  12258. *p = DBL_PINFTY;
  12259. else if (!soap_tag_cmp(s, "+INF"))
  12260. *p = DBL_PINFTY;
  12261. else if (!soap_tag_cmp(s, "-INF"))
  12262. *p = DBL_NINFTY;
  12263. else if (!soap_tag_cmp(s, "NaN"))
  12264. *p = DBL_NAN;
  12265. else
  12266. {
  12267. #if defined(HAVE_STRTOD_L)
  12268. char *r;
  12269. # ifdef WIN32
  12270. *p = _strtod_l(s, &r, soap->c_locale);
  12271. # else
  12272. *p = strtod_l(s, &r, soap->c_locale);
  12273. # endif
  12274. if (*r)
  12275. #elif defined(HAVE_STRTOD)
  12276. char *r;
  12277. *p = strtod(s, &r);
  12278. if (*r)
  12279. #endif
  12280. {
  12281. #if defined(HAVE_SSCANF_L) && !defined(HAVE_STRTOF_L) && !defined(HAVE_STRTOD_L)
  12282. if (sscanf_l(s, soap->c_locale, "%lf", p) != 1)
  12283. soap->error = SOAP_TYPE;
  12284. #elif defined(HAVE_SSCANF)
  12285. if (sscanf(s, "%lf", p) != 1)
  12286. soap->error = SOAP_TYPE;
  12287. #else
  12288. soap->error = SOAP_TYPE;
  12289. #endif
  12290. }
  12291. }
  12292. }
  12293. return soap->error;
  12294. }
  12295. #endif
  12296. /******************************************************************************/
  12297. #ifndef PALM_2
  12298. SOAP_FMAC1
  12299. double *
  12300. SOAP_FMAC2
  12301. soap_indouble(struct soap *soap, const char *tag, double *p, const char *type, int t)
  12302. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12303. return NULL;
  12304. #ifndef WITH_LEAN
  12305. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  12306. return NULL;
  12307. #endif
  12308. p = (double*)soap_id_enter(soap, soap->id, p, t, sizeof(double), 0, NULL, NULL, NULL);
  12309. if (*soap->href)
  12310. p = (double*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(double), 0, NULL);
  12311. else if (p)
  12312. { if (soap_s2double(soap, soap_value(soap), p))
  12313. return NULL;
  12314. }
  12315. if (soap->body && soap_element_end_in(soap, tag))
  12316. return NULL;
  12317. return p;
  12318. }
  12319. #endif
  12320. /******************************************************************************/
  12321. #ifndef PALM_2
  12322. SOAP_FMAC1
  12323. const char*
  12324. SOAP_FMAC2
  12325. soap_unsignedByte2s(struct soap *soap, unsigned char n)
  12326. { return soap_unsignedLong2s(soap, (unsigned long)n);
  12327. }
  12328. #endif
  12329. /******************************************************************************/
  12330. #ifndef PALM_2
  12331. SOAP_FMAC1
  12332. int
  12333. SOAP_FMAC2
  12334. soap_outunsignedByte(struct soap *soap, const char *tag, int id, const unsigned char *p, const char *type, int n)
  12335. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12336. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long)*p), 0))
  12337. return soap->error;
  12338. return soap_element_end_out(soap, tag);
  12339. }
  12340. #endif
  12341. /******************************************************************************/
  12342. #ifndef PALM_2
  12343. SOAP_FMAC1
  12344. int
  12345. SOAP_FMAC2
  12346. soap_s2unsignedByte(struct soap *soap, const char *s, unsigned char *p)
  12347. { if (s)
  12348. { unsigned long n;
  12349. char *r;
  12350. n = soap_strtoul(s, &r, 10);
  12351. if (s == r || *r || n > 255)
  12352. soap->error = SOAP_TYPE;
  12353. *p = (unsigned char)n;
  12354. }
  12355. return soap->error;
  12356. }
  12357. #endif
  12358. /******************************************************************************/
  12359. #ifndef PALM_2
  12360. SOAP_FMAC1
  12361. unsigned char *
  12362. SOAP_FMAC2
  12363. soap_inunsignedByte(struct soap *soap, const char *tag, unsigned char *p, const char *type, int t)
  12364. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12365. return NULL;
  12366. #ifndef WITH_LEAN
  12367. if (*soap->type
  12368. && soap_match_tag(soap, soap->type, type)
  12369. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12370. { soap->error = SOAP_TYPE;
  12371. soap_revert(soap);
  12372. return NULL;
  12373. }
  12374. #endif
  12375. p = (unsigned char*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned char), 0, NULL, NULL, NULL);
  12376. if (*soap->href)
  12377. p = (unsigned char*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned char), 0, NULL);
  12378. else if (p)
  12379. { if (soap_s2unsignedByte(soap, soap_value(soap), p))
  12380. return NULL;
  12381. }
  12382. if (soap->body && soap_element_end_in(soap, tag))
  12383. return NULL;
  12384. return p;
  12385. }
  12386. #endif
  12387. /******************************************************************************/
  12388. #ifndef PALM_2
  12389. SOAP_FMAC1
  12390. const char*
  12391. SOAP_FMAC2
  12392. soap_unsignedShort2s(struct soap *soap, unsigned short n)
  12393. { return soap_unsignedLong2s(soap, (unsigned long)n);
  12394. }
  12395. #endif
  12396. /******************************************************************************/
  12397. #ifndef PALM_2
  12398. SOAP_FMAC1
  12399. int
  12400. SOAP_FMAC2
  12401. soap_outunsignedShort(struct soap *soap, const char *tag, int id, const unsigned short *p, const char *type, int n)
  12402. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12403. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long)*p), 0))
  12404. return soap->error;
  12405. return soap_element_end_out(soap, tag);
  12406. }
  12407. #endif
  12408. /******************************************************************************/
  12409. #ifndef PALM_2
  12410. SOAP_FMAC1
  12411. int
  12412. SOAP_FMAC2
  12413. soap_s2unsignedShort(struct soap *soap, const char *s, unsigned short *p)
  12414. { if (s)
  12415. { unsigned long n;
  12416. char *r;
  12417. n = soap_strtoul(s, &r, 10);
  12418. if (s == r || *r || n > 65535)
  12419. soap->error = SOAP_TYPE;
  12420. *p = (unsigned short)n;
  12421. }
  12422. return soap->error;
  12423. }
  12424. #endif
  12425. /******************************************************************************/
  12426. #ifndef PALM_2
  12427. SOAP_FMAC1
  12428. unsigned short *
  12429. SOAP_FMAC2
  12430. soap_inunsignedShort(struct soap *soap, const char *tag, unsigned short *p, const char *type, int t)
  12431. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12432. return NULL;
  12433. #ifndef WITH_LEAN
  12434. if (*soap->type
  12435. && soap_match_tag(soap, soap->type, type)
  12436. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12437. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12438. { soap->error = SOAP_TYPE;
  12439. soap_revert(soap);
  12440. return NULL;
  12441. }
  12442. #endif
  12443. p = (unsigned short*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned short), 0, NULL, NULL, NULL);
  12444. if (*soap->href)
  12445. p = (unsigned short*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned short), 0, NULL);
  12446. else if (p)
  12447. { if (soap_s2unsignedShort(soap, soap_value(soap), p))
  12448. return NULL;
  12449. }
  12450. if (soap->body && soap_element_end_in(soap, tag))
  12451. return NULL;
  12452. return p;
  12453. }
  12454. #endif
  12455. /******************************************************************************/
  12456. #ifndef PALM_2
  12457. SOAP_FMAC1
  12458. const char*
  12459. SOAP_FMAC2
  12460. soap_unsignedInt2s(struct soap *soap, unsigned int n)
  12461. { return soap_unsignedLong2s(soap, (unsigned long)n);
  12462. }
  12463. #endif
  12464. /******************************************************************************/
  12465. #ifndef PALM_2
  12466. SOAP_FMAC1
  12467. int
  12468. SOAP_FMAC2
  12469. soap_outunsignedInt(struct soap *soap, const char *tag, int id, const unsigned int *p, const char *type, int n)
  12470. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12471. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long)*p), 0))
  12472. return soap->error;
  12473. return soap_element_end_out(soap, tag);
  12474. }
  12475. #endif
  12476. /******************************************************************************/
  12477. #ifndef PALM_2
  12478. SOAP_FMAC1
  12479. int
  12480. SOAP_FMAC2
  12481. soap_s2unsignedInt(struct soap *soap, const char *s, unsigned int *p)
  12482. { if (s)
  12483. { char *r;
  12484. #ifndef WITH_NOIO
  12485. #ifndef WITH_LEAN
  12486. soap_reset_errno;
  12487. #endif
  12488. #endif
  12489. *p = (unsigned int)soap_strtoul(s, &r, 10);
  12490. if ((s == r && (soap->mode & SOAP_XML_STRICT)) || *r
  12491. #ifndef WITH_NOIO
  12492. #ifndef WITH_LEAN
  12493. || soap_errno == SOAP_ERANGE
  12494. #endif
  12495. #endif
  12496. )
  12497. soap->error = SOAP_TYPE;
  12498. }
  12499. return soap->error;
  12500. }
  12501. #endif
  12502. /******************************************************************************/
  12503. #ifndef PALM_2
  12504. SOAP_FMAC1
  12505. unsigned int *
  12506. SOAP_FMAC2
  12507. soap_inunsignedInt(struct soap *soap, const char *tag, unsigned int *p, const char *type, int t)
  12508. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12509. return NULL;
  12510. #ifndef WITH_LEAN
  12511. if (*soap->type
  12512. && soap_match_tag(soap, soap->type, type)
  12513. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12514. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12515. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12516. { soap->error = SOAP_TYPE;
  12517. soap_revert(soap);
  12518. return NULL;
  12519. }
  12520. #endif
  12521. p = (unsigned int*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned int), 0, NULL, NULL, NULL);
  12522. if (*soap->href)
  12523. p = (unsigned int*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned int), 0, NULL);
  12524. else if (p)
  12525. { if (soap_s2unsignedInt(soap, soap_value(soap), p))
  12526. return NULL;
  12527. }
  12528. if (soap->body && soap_element_end_in(soap, tag))
  12529. return NULL;
  12530. return p;
  12531. }
  12532. #endif
  12533. /******************************************************************************/
  12534. #ifndef PALM_2
  12535. SOAP_FMAC1
  12536. const char*
  12537. SOAP_FMAC2
  12538. soap_unsignedLong2s(struct soap *soap, unsigned long n)
  12539. {
  12540. #ifdef HAVE_SNPRINTF
  12541. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%lu", n);
  12542. #else
  12543. sprintf(soap->tmpbuf, "%lu", n);
  12544. #endif
  12545. return soap->tmpbuf;
  12546. }
  12547. #endif
  12548. /******************************************************************************/
  12549. #ifndef PALM_2
  12550. SOAP_FMAC1
  12551. int
  12552. SOAP_FMAC2
  12553. soap_outunsignedLong(struct soap *soap, const char *tag, int id, const unsigned long *p, const char *type, int n)
  12554. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12555. || soap_string_out(soap, soap_unsignedLong2s(soap, *p), 0))
  12556. return soap->error;
  12557. return soap_element_end_out(soap, tag);
  12558. }
  12559. #endif
  12560. /******************************************************************************/
  12561. #ifndef PALM_2
  12562. SOAP_FMAC1
  12563. int
  12564. SOAP_FMAC2
  12565. soap_s2unsignedLong(struct soap *soap, const char *s, unsigned long *p)
  12566. { if (s)
  12567. { char *r;
  12568. #ifndef WITH_NOIO
  12569. #ifndef WITH_LEAN
  12570. soap_reset_errno;
  12571. #endif
  12572. #endif
  12573. *p = soap_strtoul(s, &r, 10);
  12574. if ((s == r && (soap->mode & SOAP_XML_STRICT)) || *r
  12575. #ifndef WITH_NOIO
  12576. #ifndef WITH_LEAN
  12577. || soap_errno == SOAP_ERANGE
  12578. #endif
  12579. #endif
  12580. )
  12581. soap->error = SOAP_TYPE;
  12582. }
  12583. return soap->error;
  12584. }
  12585. #endif
  12586. /******************************************************************************/
  12587. #ifndef PALM_2
  12588. SOAP_FMAC1
  12589. unsigned long *
  12590. SOAP_FMAC2
  12591. soap_inunsignedLong(struct soap *soap, const char *tag, unsigned long *p, const char *type, int t)
  12592. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12593. return NULL;
  12594. #ifndef WITH_LEAN
  12595. if (*soap->type
  12596. && soap_match_tag(soap, soap->type, type)
  12597. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12598. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12599. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12600. { soap->error = SOAP_TYPE;
  12601. soap_revert(soap);
  12602. return NULL;
  12603. }
  12604. #endif
  12605. p = (unsigned long*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned long), 0, NULL, NULL, NULL);
  12606. if (*soap->href)
  12607. p = (unsigned long*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned long), 0, NULL);
  12608. else if (p)
  12609. { if (soap_s2unsignedLong(soap, soap_value(soap), p))
  12610. return NULL;
  12611. }
  12612. if (soap->body && soap_element_end_in(soap, tag))
  12613. return NULL;
  12614. return p;
  12615. }
  12616. #endif
  12617. /******************************************************************************/
  12618. #ifndef WITH_LEAN
  12619. SOAP_FMAC1
  12620. const char*
  12621. SOAP_FMAC2
  12622. soap_ULONG642s(struct soap *soap, ULONG64 n)
  12623. {
  12624. #ifdef HAVE_SNPRINTF
  12625. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), SOAP_ULONG_FORMAT, n);
  12626. #else
  12627. sprintf(soap->tmpbuf, SOAP_ULONG_FORMAT, n);
  12628. #endif
  12629. return soap->tmpbuf;
  12630. }
  12631. #endif
  12632. /******************************************************************************/
  12633. #ifndef WITH_LEAN
  12634. SOAP_FMAC1
  12635. int
  12636. SOAP_FMAC2
  12637. soap_outULONG64(struct soap *soap, const char *tag, int id, const ULONG64 *p, const char *type, int n)
  12638. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12639. || soap_string_out(soap, soap_ULONG642s(soap, *p), 0))
  12640. return soap->error;
  12641. return soap_element_end_out(soap, tag);
  12642. }
  12643. #endif
  12644. /******************************************************************************/
  12645. #ifndef WITH_LEAN
  12646. SOAP_FMAC1
  12647. int
  12648. SOAP_FMAC2
  12649. soap_s2ULONG64(struct soap *soap, const char *s, ULONG64 *p)
  12650. { if (s)
  12651. {
  12652. #ifdef HAVE_STRTOULL
  12653. char *r;
  12654. #ifndef WITH_NOIO
  12655. #ifndef WITH_LEAN
  12656. soap_reset_errno;
  12657. #endif
  12658. #endif
  12659. *p = soap_strtoull(s, &r, 10);
  12660. if ((s == r && (soap->mode & SOAP_XML_STRICT)) || *r
  12661. #ifndef WITH_NOIO
  12662. #ifndef WITH_LEAN
  12663. || soap_errno == SOAP_ERANGE
  12664. #endif
  12665. #endif
  12666. )
  12667. #else
  12668. #ifdef HAVE_SSCANF
  12669. if (sscanf(s, SOAP_ULONG_FORMAT, p) != 1)
  12670. #endif
  12671. #endif
  12672. soap->error = SOAP_TYPE;
  12673. }
  12674. return soap->error;
  12675. }
  12676. #endif
  12677. /******************************************************************************/
  12678. #ifndef WITH_LEAN
  12679. SOAP_FMAC1
  12680. ULONG64 *
  12681. SOAP_FMAC2
  12682. soap_inULONG64(struct soap *soap, const char *tag, ULONG64 *p, const char *type, int t)
  12683. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12684. return NULL;
  12685. if (*soap->type
  12686. && soap_match_tag(soap, soap->type, type)
  12687. && soap_match_tag(soap, soap->type, ":positiveInteger")
  12688. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  12689. && soap_match_tag(soap, soap->type, ":unsignedLong")
  12690. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12691. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12692. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12693. { soap->error = SOAP_TYPE;
  12694. soap_revert(soap);
  12695. return NULL;
  12696. }
  12697. p = (ULONG64*)soap_id_enter(soap, soap->id, p, t, sizeof(ULONG64), 0, NULL, NULL, NULL);
  12698. if (*soap->href)
  12699. p = (ULONG64*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(ULONG64), 0, NULL);
  12700. else if (p)
  12701. { if (soap_s2ULONG64(soap, soap_value(soap), p))
  12702. return NULL;
  12703. }
  12704. if (soap->body && soap_element_end_in(soap, tag))
  12705. return NULL;
  12706. return p;
  12707. }
  12708. #endif
  12709. /******************************************************************************/
  12710. #ifndef PALM_2
  12711. SOAP_FMAC1
  12712. int
  12713. SOAP_FMAC2
  12714. soap_s2string(struct soap *soap, const char *s, char **t, long minlen, long maxlen)
  12715. { if (s)
  12716. { long l = (long)strlen(s);
  12717. if ((maxlen >= 0 && l > maxlen) || l < minlen)
  12718. return soap->error = SOAP_LENGTH;
  12719. if (!(*t = soap_strdup(soap, s)))
  12720. return soap->error = SOAP_EOM;
  12721. if (!(soap->mode & (SOAP_ENC_LATIN | SOAP_C_UTFSTRING)))
  12722. { char *r = *t;
  12723. /* remove non-ASCII chars */
  12724. for (s = *t; *s; s++)
  12725. if (!(*s & 0x80))
  12726. *r++ = *s;
  12727. *r = '\0';
  12728. }
  12729. }
  12730. return soap->error;
  12731. }
  12732. #endif
  12733. /******************************************************************************/
  12734. #ifndef PALM_2
  12735. SOAP_FMAC1
  12736. int
  12737. SOAP_FMAC2
  12738. soap_s2QName(struct soap *soap, const char *s, char **t, long minlen, long maxlen)
  12739. { if (s)
  12740. { long l = (long)strlen(s);
  12741. if ((maxlen >= 0 && l > maxlen) || l < minlen)
  12742. return soap->error = SOAP_LENGTH;
  12743. soap->labidx = 0;
  12744. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Normalized namespace(s) of QNames '%s'", s));
  12745. /* convert (by prefix normalize prefix) all QNames in s */
  12746. for (;;)
  12747. { size_t n;
  12748. struct soap_nlist *np;
  12749. register const char *p;
  12750. /* skip blanks */
  12751. while (*s && soap_blank((soap_wchar)*s))
  12752. s++;
  12753. if (!*s)
  12754. break;
  12755. /* find next QName */
  12756. n = 1;
  12757. while (s[n] && !soap_blank((soap_wchar)s[n]))
  12758. n++;
  12759. np = soap->nlist;
  12760. /* if there is no namespace stack, or prefix is "#" or "xml" then copy string */
  12761. if (!np || *s == '#' || !strncmp(s, "xml:", 4))
  12762. { soap_append_lab(soap, s, n);
  12763. }
  12764. else /* we normalize the QName by replacing its prefix */
  12765. { const char *q;
  12766. for (p = s; *p && p < s + n; p++)
  12767. if (*p == ':')
  12768. break;
  12769. if (*p == ':')
  12770. { size_t k = p - s;
  12771. while (np && (strncmp(np->id, s, k) || np->id[k]))
  12772. np = np->next;
  12773. p++;
  12774. }
  12775. else
  12776. { while (np && *np->id)
  12777. np = np->next;
  12778. p = s;
  12779. }
  12780. /* replace prefix */
  12781. if (np)
  12782. { if (np->index >= 0 && soap->local_namespaces && (q = soap->local_namespaces[np->index].id))
  12783. { size_t k = strlen(q);
  12784. if (q[k-1] != '_')
  12785. soap_append_lab(soap, q, k);
  12786. else
  12787. { soap_append_lab(soap, "\"", 1);
  12788. soap_append_lab(soap, soap->local_namespaces[np->index].ns, strlen(soap->local_namespaces[np->index].ns));
  12789. soap_append_lab(soap, "\"", 1);
  12790. }
  12791. }
  12792. else if (np->ns)
  12793. { soap_append_lab(soap, "\"", 1);
  12794. soap_append_lab(soap, np->ns, strlen(np->ns));
  12795. soap_append_lab(soap, "\"", 1);
  12796. }
  12797. else
  12798. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\nNamespace prefix of '%s' not defined (index=%d, URI='%s')\n", s, np->index, np->ns ? np->ns : SOAP_STR_EOS));
  12799. return soap->error = SOAP_NAMESPACE;
  12800. }
  12801. }
  12802. else if (s[n]) /* no namespace, part of string */
  12803. { soap_append_lab(soap, s, n);
  12804. }
  12805. else /* no namespace: assume "" namespace */
  12806. { soap_append_lab(soap, "\"\"", 2);
  12807. }
  12808. soap_append_lab(soap, ":", 1);
  12809. soap_append_lab(soap, p, n - (p-s));
  12810. }
  12811. /* advance to next and add spacing */
  12812. s += n;
  12813. if (*s)
  12814. soap_append_lab(soap, " ", 1);
  12815. }
  12816. soap_append_lab(soap, SOAP_STR_EOS, 1);
  12817. *t = soap_strdup(soap, soap->labbuf);
  12818. DBGLOG(TEST, SOAP_MESSAGE(fdebug, " into '%s'\n", *t));
  12819. }
  12820. return soap->error;
  12821. }
  12822. #endif
  12823. /******************************************************************************/
  12824. #ifndef PALM_2
  12825. SOAP_FMAC1
  12826. const char*
  12827. SOAP_FMAC2
  12828. soap_QName2s(struct soap *soap, const char *s)
  12829. { const char *t = NULL;
  12830. if (s)
  12831. { soap->labidx = 0;
  12832. for (;;)
  12833. { size_t n;
  12834. /* skip blanks */
  12835. while (*s && soap_blank((soap_wchar)*s))
  12836. s++;
  12837. if (!*s)
  12838. break;
  12839. /* find next QName */
  12840. n = 1;
  12841. while (s[n] && !soap_blank((soap_wchar)s[n]))
  12842. n++;
  12843. /* normal prefix: pass string as is */
  12844. if (*s != '"')
  12845. {
  12846. #ifndef WITH_LEAN
  12847. if ((soap->mode & SOAP_XML_CANONICAL))
  12848. soap_utilize_ns(soap, s);
  12849. if ((soap->mode & SOAP_XML_DEFAULTNS))
  12850. { const char *r = strchr(s, ':');
  12851. if (r && soap->nlist && !strncmp(soap->nlist->id, s, r-s) && !soap->nlist->id[r-s])
  12852. { n -= r-s + 1;
  12853. s = r + 1;
  12854. }
  12855. }
  12856. #endif
  12857. soap_append_lab(soap, s, n);
  12858. }
  12859. else /* URL-based string prefix */
  12860. { const char *q;
  12861. s++;
  12862. q = strchr(s, '"');
  12863. if (q)
  12864. { struct Namespace *p = soap->local_namespaces;
  12865. if (p)
  12866. { for (; p->id; p++)
  12867. { if (p->ns)
  12868. if (!soap_tag_cmp(s, p->ns))
  12869. break;
  12870. if (p->in)
  12871. if (!soap_tag_cmp(s, p->in))
  12872. break;
  12873. }
  12874. }
  12875. /* URL is in the namespace table? */
  12876. if (p && p->id)
  12877. { const char *r = p->id;
  12878. #ifndef WITH_LEAN
  12879. if ((soap->mode & SOAP_XML_DEFAULTNS) && soap->nlist && !strcmp(soap->nlist->id, r))
  12880. q++;
  12881. else
  12882. #endif
  12883. soap_append_lab(soap, r, strlen(r));
  12884. }
  12885. else /* not in namespace table: create xmlns binding */
  12886. { char *r = soap_strdup(soap, s);
  12887. r[q-s] = '\0';
  12888. #ifdef HAVE_SNPRINTF
  12889. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns:_%d", soap->idnum++);
  12890. #else
  12891. sprintf(soap->tmpbuf, "xmlns:_%d", soap->idnum++);
  12892. #endif
  12893. soap_set_attr(soap, soap->tmpbuf, r, 1);
  12894. soap_append_lab(soap, soap->tmpbuf + 6, strlen(soap->tmpbuf + 6));
  12895. }
  12896. soap_append_lab(soap, q + 1, n - (q-s) - 1);
  12897. }
  12898. }
  12899. /* advance to next and add spacing */
  12900. s += n;
  12901. if (*s)
  12902. soap_append_lab(soap, " ", 1);
  12903. }
  12904. soap_append_lab(soap, SOAP_STR_EOS, 1);
  12905. t = soap_strdup(soap, soap->labbuf);
  12906. }
  12907. return t;
  12908. }
  12909. #endif
  12910. /******************************************************************************/
  12911. #ifndef WITH_LEAN
  12912. SOAP_FMAC1
  12913. int
  12914. SOAP_FMAC2
  12915. soap_s2wchar(struct soap *soap, const char *s, wchar_t **t, long minlen, long maxlen)
  12916. { if (s)
  12917. { long l;
  12918. wchar_t *r;
  12919. *t = r = (wchar_t*)soap_malloc(soap, sizeof(wchar_t) * (strlen(s) + 1));
  12920. if (!r)
  12921. return soap->error = SOAP_EOM;
  12922. if (soap->mode & SOAP_ENC_LATIN)
  12923. { while (*s)
  12924. *r++ = (wchar_t)*s++;
  12925. }
  12926. else
  12927. { /* Convert UTF8 to wchar */
  12928. while (*s)
  12929. { register soap_wchar c, c1, c2, c3, c4;
  12930. c = (unsigned char)*s++;
  12931. if (c < 0x80)
  12932. *r++ = (wchar_t)c;
  12933. else
  12934. { c1 = (soap_wchar)*s++ & 0x3F;
  12935. if (c < 0xE0)
  12936. *r++ = (wchar_t)(((soap_wchar)(c & 0x1F) << 6) | c1);
  12937. else
  12938. { c2 = (soap_wchar)*s++ & 0x3F;
  12939. if (c < 0xF0)
  12940. *r++ = (wchar_t)(((soap_wchar)(c & 0x0F) << 12) | (c1 << 6) | c2);
  12941. else
  12942. { c3 = (soap_wchar)*s++ & 0x3F;
  12943. if (c < 0xF8)
  12944. *r++ = (wchar_t)(((soap_wchar)(c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3);
  12945. else
  12946. { c4 = (soap_wchar)*s++ & 0x3F;
  12947. if (c < 0xFC)
  12948. *r++ = (wchar_t)(((soap_wchar)(c & 0x03) << 24) | (c1 << 18) | (c2 << 12) | (c3 << 6) | c4);
  12949. else
  12950. *r++ = (wchar_t)(((soap_wchar)(c & 0x01) << 30) | (c1 << 24) | (c2 << 18) | (c3 << 12) | (c4 << 6) | (soap_wchar)(*s++ & 0x3F));
  12951. }
  12952. }
  12953. }
  12954. }
  12955. }
  12956. }
  12957. *r = L'\0';
  12958. l = (long)(r - *t);
  12959. if ((maxlen >= 0 && l > maxlen) || l < minlen)
  12960. return soap->error = SOAP_LENGTH;
  12961. }
  12962. return soap->error;
  12963. }
  12964. #endif
  12965. /******************************************************************************/
  12966. #ifndef WITH_LEAN
  12967. SOAP_FMAC1
  12968. const char*
  12969. SOAP_FMAC2
  12970. soap_wchar2s(struct soap *soap, const wchar_t *s)
  12971. { register soap_wchar c;
  12972. register char *r, *t;
  12973. const wchar_t *q = s;
  12974. size_t n = 0;
  12975. while ((c = *q++))
  12976. { if (c > 0 && c < 0x80)
  12977. n++;
  12978. else
  12979. n += 6;
  12980. }
  12981. r = t = (char*)soap_malloc(soap, n + 1);
  12982. if (r)
  12983. { /* Convert wchar to UTF8 */
  12984. while ((c = *s++))
  12985. { if (c > 0 && c < 0x80)
  12986. *t++ = (char)c;
  12987. else
  12988. { if (c < 0x0800)
  12989. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  12990. else
  12991. { if (c < 0x010000)
  12992. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  12993. else
  12994. { if (c < 0x200000)
  12995. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  12996. else
  12997. { if (c < 0x04000000)
  12998. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  12999. else
  13000. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  13001. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  13002. }
  13003. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  13004. }
  13005. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  13006. }
  13007. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  13008. }
  13009. *t++ = (char)(0x80 | (c & 0x3F));
  13010. }
  13011. }
  13012. *t = '\0';
  13013. }
  13014. return r;
  13015. }
  13016. #endif
  13017. /******************************************************************************/
  13018. #ifndef PALM_2
  13019. SOAP_FMAC1
  13020. int
  13021. SOAP_FMAC2
  13022. soap_outstring(struct soap *soap, const char *tag, int id, char *const*p, const char *type, int n)
  13023. { id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n);
  13024. if (id < 0)
  13025. return soap->error;
  13026. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  13027. return soap_element_null(soap, tag, id, type);
  13028. if (soap_element_begin_out(soap, tag, id, type)
  13029. || soap_string_out(soap, *p, 0)
  13030. || soap_element_end_out(soap, tag))
  13031. return soap->error;
  13032. return SOAP_OK;
  13033. }
  13034. #endif
  13035. /******************************************************************************/
  13036. #ifndef PALM_2
  13037. SOAP_FMAC1
  13038. char **
  13039. SOAP_FMAC2
  13040. soap_instring(struct soap *soap, const char *tag, char **p, const char *type, int t, int flag, long minlen, long maxlen)
  13041. { (void)type;
  13042. if (soap_element_begin_in(soap, tag, 1, NULL))
  13043. { if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  13044. return NULL;
  13045. soap->error = SOAP_OK;
  13046. }
  13047. if (!p)
  13048. { if (!(p = (char**)soap_malloc(soap, sizeof(char*))))
  13049. return NULL;
  13050. }
  13051. if (soap->null)
  13052. *p = NULL;
  13053. else if (soap->body)
  13054. { *p = soap_string_in(soap, flag, minlen, maxlen);
  13055. if (!*p || !(char*)soap_id_enter(soap, soap->id, *p, t, sizeof(char*), 0, NULL, NULL, NULL))
  13056. return NULL;
  13057. if (!**p && tag && *tag == '-')
  13058. { soap->error = SOAP_NO_TAG;
  13059. return NULL;
  13060. }
  13061. }
  13062. else if (tag && *tag == '-')
  13063. { soap->error = SOAP_NO_TAG;
  13064. return NULL;
  13065. }
  13066. else if (!*soap->href && minlen > 0)
  13067. { soap->error = SOAP_LENGTH;
  13068. return NULL;
  13069. }
  13070. else
  13071. *p = soap_strdup(soap, SOAP_STR_EOS);
  13072. if (*soap->href)
  13073. p = (char**)soap_id_lookup(soap, soap->href, (void**)p, t, sizeof(char**), 0);
  13074. if (soap->body && soap_element_end_in(soap, tag))
  13075. return NULL;
  13076. return p;
  13077. }
  13078. #endif
  13079. /******************************************************************************/
  13080. #ifndef WITH_LEANER
  13081. #ifndef PALM_2
  13082. SOAP_FMAC1
  13083. int
  13084. SOAP_FMAC2
  13085. soap_outwstring(struct soap *soap, const char *tag, int id, wchar_t *const*p, const char *type, int n)
  13086. { id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n);
  13087. if (id < 0)
  13088. return soap->error;
  13089. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  13090. return soap_element_null(soap, tag, id, type);
  13091. if (soap_element_begin_out(soap, tag, id, type)
  13092. || soap_wstring_out(soap, *p, 0)
  13093. || soap_element_end_out(soap, tag))
  13094. return soap->error;
  13095. return SOAP_OK;
  13096. }
  13097. #endif
  13098. #endif
  13099. /******************************************************************************/
  13100. #ifndef WITH_LEANER
  13101. #ifndef PALM_2
  13102. SOAP_FMAC1
  13103. wchar_t **
  13104. SOAP_FMAC2
  13105. soap_inwstring(struct soap *soap, const char *tag, wchar_t **p, const char *type, int t, long minlen, long maxlen)
  13106. { (void)type;
  13107. if (soap_element_begin_in(soap, tag, 1, NULL))
  13108. { if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  13109. return NULL;
  13110. soap->error = SOAP_OK;
  13111. }
  13112. if (!p)
  13113. { if (!(p = (wchar_t**)soap_malloc(soap, sizeof(wchar_t*))))
  13114. return NULL;
  13115. }
  13116. if (soap->body)
  13117. { *p = soap_wstring_in(soap, 1, minlen, maxlen);
  13118. if (!*p || !(wchar_t*)soap_id_enter(soap, soap->id, *p, t, sizeof(wchar_t*), 0, NULL, NULL, NULL))
  13119. return NULL;
  13120. if (!**p && tag && *tag == '-')
  13121. { soap->error = SOAP_NO_TAG;
  13122. return NULL;
  13123. }
  13124. }
  13125. else if (tag && *tag == '-')
  13126. { soap->error = SOAP_NO_TAG;
  13127. return NULL;
  13128. }
  13129. else if (soap->null)
  13130. *p = NULL;
  13131. else
  13132. *p = soap_wstrdup(soap, (wchar_t*)SOAP_STR_EOS);
  13133. if (*soap->href)
  13134. p = (wchar_t**)soap_id_lookup(soap, soap->href, (void**)p, t, sizeof(wchar_t**), 0);
  13135. if (soap->body && soap_element_end_in(soap, tag))
  13136. return NULL;
  13137. return p;
  13138. }
  13139. #endif
  13140. #endif
  13141. /******************************************************************************/
  13142. #ifndef WITH_LEAN
  13143. SOAP_FMAC1
  13144. time_t
  13145. SOAP_FMAC2
  13146. soap_timegm(struct tm *T)
  13147. {
  13148. #if defined(HAVE_TIMEGM)
  13149. return timegm(T);
  13150. #else
  13151. time_t t, g, z;
  13152. struct tm tm;
  13153. t = mktime(T);
  13154. if (t == (time_t)-1)
  13155. return (time_t)-1;
  13156. #ifdef HAVE_GMTIME_R
  13157. gmtime_r(&t, &tm);
  13158. #else
  13159. tm = *gmtime(&t);
  13160. #endif
  13161. tm.tm_isdst = 0;
  13162. g = mktime(&tm);
  13163. if (g == (time_t)-1)
  13164. return (time_t)-1;
  13165. z = g - t;
  13166. return t - z;
  13167. #endif
  13168. }
  13169. #endif
  13170. /******************************************************************************/
  13171. #ifndef WITH_LEAN
  13172. SOAP_FMAC1
  13173. const char*
  13174. SOAP_FMAC2
  13175. soap_dateTime2s(struct soap *soap, time_t n)
  13176. { struct tm T, *pT = &T;
  13177. #if defined(HAVE_GMTIME_R)
  13178. if (gmtime_r(&n, pT))
  13179. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  13180. #elif defined(HAVE_GMTIME)
  13181. if ((pT = gmtime(&n)))
  13182. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  13183. #elif defined(HAVE_TM_GMTOFF) || defined(HAVE_STRUCT_TM_TM_GMTOFF) || defined(HAVE_STRUCT_TM___TM_GMTOFF)
  13184. #if defined(HAVE_LOCALTIME_R)
  13185. if (localtime_r(&n, pT))
  13186. { strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  13187. memmove(soap->tmpbuf + 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  13188. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  13189. }
  13190. #else
  13191. if ((pT = localtime(&n)))
  13192. { strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  13193. memmove(soap->tmpbuf + 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  13194. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  13195. }
  13196. #endif
  13197. #elif defined(HAVE_GETTIMEOFDAY)
  13198. struct timezone tz;
  13199. memset((void*)&tz, 0, sizeof(tz));
  13200. #if defined(HAVE_LOCALTIME_R)
  13201. if (localtime_r(&n, pT))
  13202. { struct timeval tv;
  13203. gettimeofday(&tv, &tz);
  13204. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13205. #ifdef HAVE_SNPRINTF
  13206. soap_snprintf(soap->tmpbuf + strlen(soap->tmpbuf), sizeof(soap->tmpbuf) - strlen(soap->tmpbuf), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  13207. #else
  13208. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  13209. #endif
  13210. }
  13211. #else
  13212. if ((pT = localtime(&n)))
  13213. { struct timeval tv;
  13214. gettimeofday(&tv, &tz);
  13215. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13216. #ifdef HAVE_SNPRINTF
  13217. soap_snprintf(soap->tmpbuf + strlen(soap->tmpbuf), sizeof(soap->tmpbuf) - strlen(soap->tmpbuf), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  13218. #else
  13219. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  13220. #endif
  13221. }
  13222. #endif
  13223. #elif defined(HAVE_FTIME)
  13224. struct timeb t;
  13225. memset((void*)&t, 0, sizeof(t));
  13226. #if defined(HAVE_LOCALTIME_R)
  13227. if (localtime_r(&n, pT))
  13228. {
  13229. #ifdef __BORLANDC__
  13230. ::ftime(&t);
  13231. #else
  13232. ftime(&t);
  13233. #endif
  13234. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13235. #ifdef HAVE_SNPRINTF
  13236. soap_snprintf(soap->tmpbuf + strlen(soap->tmpbuf), sizeof(soap->tmpbuf) - strlen(soap->tmpbuf), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  13237. #else
  13238. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  13239. #endif
  13240. }
  13241. #else
  13242. if ((pT = localtime(&n)))
  13243. {
  13244. #ifdef __BORLANDC__
  13245. ::ftime(&t);
  13246. #else
  13247. ftime(&t);
  13248. #endif
  13249. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13250. #ifdef HAVE_SNPRINTF
  13251. soap_snprintf(soap->tmpbuf + strlen(soap->tmpbuf), sizeof(soap->tmpbuf) - strlen(soap->tmpbuf), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  13252. #else
  13253. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  13254. #endif
  13255. }
  13256. #endif
  13257. #elif defined(HAVE_LOCALTIME_R)
  13258. if (localtime_r(&n, pT))
  13259. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13260. #else
  13261. if ((pT = localtime(&n)))
  13262. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13263. #endif
  13264. else
  13265. strcpy(soap->tmpbuf, "1969-12-31T23:59:59Z");
  13266. return soap->tmpbuf;
  13267. }
  13268. #endif
  13269. /******************************************************************************/
  13270. #ifndef WITH_LEAN
  13271. SOAP_FMAC1
  13272. int
  13273. SOAP_FMAC2
  13274. soap_outdateTime(struct soap *soap, const char *tag, int id, const time_t *p, const char *type, int n)
  13275. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  13276. || soap_string_out(soap, soap_dateTime2s(soap, *p), 0))
  13277. return soap->error;
  13278. return soap_element_end_out(soap, tag);
  13279. }
  13280. #endif
  13281. /******************************************************************************/
  13282. #ifndef WITH_LEAN
  13283. SOAP_FMAC1
  13284. int
  13285. SOAP_FMAC2
  13286. soap_s2dateTime(struct soap *soap, const char *s, time_t *p)
  13287. { if (s)
  13288. { char zone[32];
  13289. struct tm T;
  13290. const char *t;
  13291. *zone = '\0';
  13292. memset((void*)&T, 0, sizeof(T));
  13293. if (strchr(s, '-'))
  13294. t = "%d-%d-%dT%d:%d:%d%31s";
  13295. else if (strchr(s, ':'))
  13296. t = "%4d%2d%2dT%d:%d:%d%31s";
  13297. else /* parse non-XSD-standard alternative ISO 8601 format */
  13298. t = "%4d%2d%2dT%2d%2d%2d%31s";
  13299. if (sscanf(s, t, &T.tm_year, &T.tm_mon, &T.tm_mday, &T.tm_hour, &T.tm_min, &T.tm_sec, zone) < 6)
  13300. return soap->error = SOAP_TYPE;
  13301. if (T.tm_year == 1)
  13302. T.tm_year = 70;
  13303. else
  13304. T.tm_year -= 1900;
  13305. T.tm_mon--;
  13306. if (*zone == '.')
  13307. { for (s = zone + 1; *s; s++)
  13308. if (*s < '0' || *s > '9')
  13309. break;
  13310. }
  13311. else
  13312. s = zone;
  13313. if (*s)
  13314. {
  13315. #ifndef WITH_NOZONE
  13316. if (*s == '+' || *s == '-')
  13317. { int h = 0, m = 0;
  13318. if (s[3] == ':')
  13319. { /* +hh:mm */
  13320. sscanf(s, "%d:%d", &h, &m);
  13321. if (h < 0)
  13322. m = -m;
  13323. }
  13324. else /* +hhmm */
  13325. { m = (int)soap_strtol(s, NULL, 10);
  13326. h = m / 100;
  13327. m = m % 100;
  13328. }
  13329. T.tm_min -= m;
  13330. T.tm_hour -= h;
  13331. /* put hour and min in range */
  13332. T.tm_hour += T.tm_min / 60;
  13333. T.tm_min %= 60;
  13334. if (T.tm_min < 0)
  13335. { T.tm_min += 60;
  13336. T.tm_hour--;
  13337. }
  13338. T.tm_mday += T.tm_hour / 24;
  13339. T.tm_hour %= 24;
  13340. if (T.tm_hour < 0)
  13341. { T.tm_hour += 24;
  13342. T.tm_mday--;
  13343. }
  13344. /* note: day of the month may be out of range, timegm() handles it */
  13345. }
  13346. #endif
  13347. *p = soap_timegm(&T);
  13348. }
  13349. else /* no UTC or timezone, so assume we got a localtime */
  13350. { T.tm_isdst = -1;
  13351. *p = mktime(&T);
  13352. }
  13353. }
  13354. return soap->error;
  13355. }
  13356. #endif
  13357. /******************************************************************************/
  13358. #ifndef WITH_LEAN
  13359. SOAP_FMAC1
  13360. time_t *
  13361. SOAP_FMAC2
  13362. soap_indateTime(struct soap *soap, const char *tag, time_t *p, const char *type, int t)
  13363. { if (soap_element_begin_in(soap, tag, 0, NULL))
  13364. return NULL;
  13365. if (*soap->type
  13366. && soap_match_tag(soap, soap->type, type)
  13367. && soap_match_tag(soap, soap->type, ":dateTime"))
  13368. { soap->error = SOAP_TYPE;
  13369. soap_revert(soap);
  13370. return NULL;
  13371. }
  13372. p = (time_t*)soap_id_enter(soap, soap->id, p, t, sizeof(time_t), 0, NULL, NULL, NULL);
  13373. if (*soap->href)
  13374. p = (time_t*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(time_t), 0, NULL);
  13375. else if (p)
  13376. { if (soap_s2dateTime(soap, soap_value(soap), p))
  13377. return NULL;
  13378. }
  13379. if (soap->body && soap_element_end_in(soap, tag))
  13380. return NULL;
  13381. return p;
  13382. }
  13383. #endif
  13384. /******************************************************************************/
  13385. #ifndef PALM_2
  13386. SOAP_FMAC1
  13387. int
  13388. SOAP_FMAC2
  13389. soap_outliteral(struct soap *soap, const char *tag, char *const*p, const char *type)
  13390. { int i;
  13391. const char *t = NULL;
  13392. if (tag && *tag != '-')
  13393. { if (soap->local_namespaces && (t = strchr(tag, ':')))
  13394. { size_t n = t - tag;
  13395. if (n >= sizeof(soap->tmpbuf))
  13396. n = sizeof(soap->tmpbuf) - 1;
  13397. strncpy(soap->tmpbuf, tag, n);
  13398. soap->tmpbuf[n] = '\0';
  13399. for (i = 0; soap->local_namespaces[i].id; i++)
  13400. if (!strcmp(soap->tmpbuf, soap->local_namespaces[i].id))
  13401. break;
  13402. t++;
  13403. if (soap_element(soap, t, 0, type)
  13404. || soap_attribute(soap, "xmlns", soap->local_namespaces[i].ns ? soap->local_namespaces[i].ns : SOAP_STR_EOS)
  13405. || soap_element_start_end_out(soap, NULL))
  13406. return soap->error;
  13407. }
  13408. else
  13409. { t = tag;
  13410. if (soap_element_begin_out(soap, t, 0, type))
  13411. return soap->error;
  13412. }
  13413. }
  13414. if (p && *p)
  13415. { if (soap_send(soap, *p)) /* send as-is */
  13416. return soap->error;
  13417. }
  13418. if (t)
  13419. return soap_element_end_out(soap, t);
  13420. return SOAP_OK;
  13421. }
  13422. #endif
  13423. /******************************************************************************/
  13424. #ifndef PALM_2
  13425. SOAP_FMAC1
  13426. char **
  13427. SOAP_FMAC2
  13428. soap_inliteral(struct soap *soap, const char *tag, char **p)
  13429. { if (soap_element_begin_in(soap, tag, 1, NULL))
  13430. { if (soap->error != SOAP_NO_TAG || soap_unget(soap, soap_get(soap)) == SOAP_TT)
  13431. return NULL;
  13432. soap->error = SOAP_OK;
  13433. }
  13434. if (!p)
  13435. { if (!(p = (char**)soap_malloc(soap, sizeof(char*))))
  13436. return NULL;
  13437. }
  13438. if (soap->body || (tag && *tag == '-'))
  13439. { *p = soap_string_in(soap, 0, -1, -1);
  13440. if (!*p)
  13441. return NULL;
  13442. if (!**p && tag && *tag == '-')
  13443. { soap->error = SOAP_NO_TAG;
  13444. return NULL;
  13445. }
  13446. }
  13447. else if (soap->null)
  13448. *p = NULL;
  13449. else
  13450. *p = soap_strdup(soap, SOAP_STR_EOS);
  13451. if (soap->body && soap_element_end_in(soap, tag))
  13452. return NULL;
  13453. return p;
  13454. }
  13455. #endif
  13456. /******************************************************************************/
  13457. #ifndef WITH_LEANER
  13458. #ifndef PALM_2
  13459. SOAP_FMAC1
  13460. int
  13461. SOAP_FMAC2
  13462. soap_outwliteral(struct soap *soap, const char *tag, wchar_t *const*p, const char *type)
  13463. { int i;
  13464. const char *t = NULL;
  13465. if (tag && *tag != '-')
  13466. { if (soap->local_namespaces && (t = strchr(tag, ':')))
  13467. { size_t n = t - tag;
  13468. if (n >= sizeof(soap->tmpbuf))
  13469. n = sizeof(soap->tmpbuf) - 1;
  13470. strncpy(soap->tmpbuf, tag, n);
  13471. soap->tmpbuf[n] = '\0';
  13472. for (i = 0; soap->local_namespaces[i].id; i++)
  13473. if (!strcmp(soap->tmpbuf, soap->local_namespaces[i].id))
  13474. break;
  13475. t++;
  13476. if (soap_element(soap, t, 0, type)
  13477. || soap_attribute(soap, "xmlns", soap->local_namespaces[i].ns ? soap->local_namespaces[i].ns : SOAP_STR_EOS)
  13478. || soap_element_start_end_out(soap, NULL))
  13479. return soap->error;
  13480. }
  13481. else
  13482. { t = tag;
  13483. if (soap_element_begin_out(soap, t, 0, type))
  13484. return soap->error;
  13485. }
  13486. }
  13487. if (p)
  13488. { wchar_t c;
  13489. const wchar_t *s = *p;
  13490. while ((c = *s++))
  13491. { if (soap_pututf8(soap, (unsigned long)c)) /* send as-is in UTF8 */
  13492. return soap->error;
  13493. }
  13494. }
  13495. if (t)
  13496. return soap_element_end_out(soap, t);
  13497. return SOAP_OK;
  13498. }
  13499. #endif
  13500. #endif
  13501. /******************************************************************************/
  13502. #ifndef WITH_LEANER
  13503. #ifndef PALM_2
  13504. SOAP_FMAC1
  13505. wchar_t **
  13506. SOAP_FMAC2
  13507. soap_inwliteral(struct soap *soap, const char *tag, wchar_t **p)
  13508. { if (soap_element_begin_in(soap, tag, 1, NULL))
  13509. { if (soap->error != SOAP_NO_TAG || soap_unget(soap, soap_get(soap)) == SOAP_TT)
  13510. return NULL;
  13511. soap->error = SOAP_OK;
  13512. }
  13513. if (!p)
  13514. { if (!(p = (wchar_t**)soap_malloc(soap, sizeof(wchar_t*))))
  13515. return NULL;
  13516. }
  13517. if (soap->body)
  13518. { *p = soap_wstring_in(soap, 0, -1, -1);
  13519. if (!*p)
  13520. return NULL;
  13521. if (!**p && tag && *tag == '-')
  13522. { soap->error = SOAP_NO_TAG;
  13523. return NULL;
  13524. }
  13525. }
  13526. else if (tag && *tag == '-')
  13527. { soap->error = SOAP_NO_TAG;
  13528. return NULL;
  13529. }
  13530. else if (soap->null)
  13531. *p = NULL;
  13532. else
  13533. *p = soap_wstrdup(soap, (wchar_t*)SOAP_STR_EOS);
  13534. if (soap->body && soap_element_end_in(soap, tag))
  13535. return NULL;
  13536. return p;
  13537. }
  13538. #endif
  13539. #endif
  13540. /******************************************************************************/
  13541. #ifndef PALM_2
  13542. SOAP_FMAC1
  13543. const char *
  13544. SOAP_FMAC2
  13545. soap_value(struct soap *soap)
  13546. { register size_t i;
  13547. register soap_wchar c = 0;
  13548. register char *s = soap->tmpbuf;
  13549. if (!soap->body)
  13550. return SOAP_STR_EOS;
  13551. do c = soap_get(soap);
  13552. while (soap_blank(c));
  13553. for (i = 0; i < sizeof(soap->tmpbuf) - 1; i++)
  13554. { if (c == SOAP_TT || c == SOAP_LT || (int)c == EOF)
  13555. break;
  13556. *s++ = (char)c;
  13557. c = soap_get(soap);
  13558. }
  13559. for (s--; i > 0; i--, s--)
  13560. { if (!soap_blank((soap_wchar)*s))
  13561. break;
  13562. }
  13563. s[1] = '\0';
  13564. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0'; /* appease */
  13565. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element content value='%s'\n", soap->tmpbuf));
  13566. if (c == SOAP_TT || c == SOAP_LT || (int)c == EOF)
  13567. soap_unget(soap, c);
  13568. else if (soap->mode & SOAP_XML_STRICT)
  13569. { soap->error = SOAP_LENGTH;
  13570. return NULL;
  13571. }
  13572. #ifdef WITH_DOM
  13573. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  13574. soap->dom->data = soap_strdup(soap, soap->tmpbuf);
  13575. #endif
  13576. return soap->tmpbuf; /* return non-null pointer */
  13577. }
  13578. #endif
  13579. /******************************************************************************/
  13580. #if !defined(WITH_LEANER) || !defined(WITH_NOHTTP)
  13581. #ifndef PALM_2
  13582. SOAP_FMAC1
  13583. int
  13584. SOAP_FMAC2
  13585. soap_getline(struct soap *soap, char *s, int len)
  13586. { int i = len;
  13587. soap_wchar c = 0;
  13588. for (;;)
  13589. { while (--i > 0)
  13590. { c = soap_getchar(soap);
  13591. if (c == '\r' || c == '\n')
  13592. break;
  13593. if ((int)c == EOF)
  13594. return soap->error = SOAP_CHK_EOF;
  13595. *s++ = (char)c;
  13596. }
  13597. *s = '\0';
  13598. if (c != '\n')
  13599. c = soap_getchar(soap); /* got \r or something else, now get \n */
  13600. if (c == '\n')
  13601. { if (i + 1 == len) /* empty line: end of HTTP/MIME header */
  13602. break;
  13603. c = soap_get0(soap);
  13604. if (c != ' ' && c != '\t') /* HTTP line continuation? */
  13605. break;
  13606. }
  13607. else if ((int)c == EOF)
  13608. return soap->error = SOAP_CHK_EOF;
  13609. if (i <= 0)
  13610. return soap->error = SOAP_HDR;
  13611. }
  13612. return SOAP_OK;
  13613. }
  13614. #endif
  13615. #endif
  13616. /******************************************************************************/
  13617. #ifndef PALM_1
  13618. static size_t
  13619. soap_count_attachments(struct soap *soap)
  13620. {
  13621. #ifndef WITH_LEANER
  13622. register struct soap_multipart *content;
  13623. register size_t count = soap->count;
  13624. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the message size with attachments, current count=%lu\n", (unsigned long)count));
  13625. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  13626. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of DIME attachments\n"));
  13627. for (content = soap->dime.first; content; content = content->next)
  13628. { count += 12 + ((content->size+3)&(~3));
  13629. if (content->id)
  13630. count += ((strlen(content->id)+3)&(~3));
  13631. if (content->type)
  13632. count += ((strlen(content->type)+3)&(~3));
  13633. if (content->options)
  13634. count += ((((unsigned char)content->options[2] << 8) | ((unsigned char)content->options[3]))+7)&(~3);
  13635. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of DIME attachment content is %lu bytes\n", (unsigned long)content->size));
  13636. }
  13637. }
  13638. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary)
  13639. { register size_t n = strlen(soap->mime.boundary);
  13640. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of MIME attachments\n"));
  13641. for (content = soap->mime.first; content; content = content->next)
  13642. { register const char *s;
  13643. /* count \r\n--boundary\r\n */
  13644. count += 6 + n;
  13645. /* count Content-Type: ...\r\n */
  13646. if (content->type)
  13647. count += 16 + strlen(content->type);
  13648. /* count Content-Transfer-Encoding: ...\r\n */
  13649. s = soap_code_str(mime_codes, content->encoding);
  13650. if (s)
  13651. count += 29 + strlen(s);
  13652. /* count Content-ID: ...\r\n */
  13653. if (content->id)
  13654. count += 14 + strlen(content->id);
  13655. /* count Content-Location: ...\r\n */
  13656. if (content->location)
  13657. count += 20 + strlen(content->location);
  13658. /* count Content-Description: ...\r\n */
  13659. if (content->description)
  13660. count += 23 + strlen(content->description);
  13661. /* count \r\n...content */
  13662. count += 2 + content->size;
  13663. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of MIME attachment content is %lu bytes\n", (unsigned long)content->size));
  13664. }
  13665. /* count \r\n--boundary-- */
  13666. count += 6 + n;
  13667. }
  13668. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New count=%lu\n", (unsigned long)count));
  13669. return count;
  13670. #else
  13671. return soap->count;
  13672. #endif
  13673. }
  13674. #endif
  13675. /******************************************************************************/
  13676. #ifndef WITH_LEANER
  13677. #ifndef PALM_1
  13678. static int
  13679. soap_putdimefield(struct soap *soap, const char *s, size_t n)
  13680. { if (soap_send_raw(soap, s, n))
  13681. return soap->error;
  13682. return soap_send_raw(soap, SOAP_STR_PADDING, -(long)n&3);
  13683. }
  13684. #endif
  13685. #endif
  13686. /******************************************************************************/
  13687. #ifndef WITH_LEANER
  13688. #ifndef PALM_1
  13689. SOAP_FMAC1
  13690. char *
  13691. SOAP_FMAC2
  13692. soap_dime_option(struct soap *soap, unsigned short optype, const char *option)
  13693. { size_t n;
  13694. char *s = NULL;
  13695. if (option)
  13696. { n = strlen(option);
  13697. s = (char*)soap_malloc(soap, n + 5);
  13698. if (s)
  13699. { s[0] = (char)(optype >> 8);
  13700. s[1] = (char)(optype & 0xFF);
  13701. s[2] = (char)(n >> 8);
  13702. s[3] = (char)(n & 0xFF);
  13703. strcpy(s + 4, option);
  13704. }
  13705. }
  13706. return s;
  13707. }
  13708. #endif
  13709. #endif
  13710. /******************************************************************************/
  13711. #ifndef WITH_LEANER
  13712. #ifndef PALM_1
  13713. SOAP_FMAC1
  13714. int
  13715. SOAP_FMAC2
  13716. soap_putdimehdr(struct soap *soap)
  13717. { unsigned char tmp[12];
  13718. size_t optlen = 0, idlen = 0, typelen = 0;
  13719. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Put DIME header id='%s'\n", soap->dime.id ? soap->dime.id : SOAP_STR_EOS));
  13720. if (soap->dime.options)
  13721. optlen = (((unsigned char)soap->dime.options[2] << 8) | ((unsigned char)soap->dime.options[3])) + 4;
  13722. if (soap->dime.id)
  13723. { idlen = strlen(soap->dime.id);
  13724. if (idlen > 0x0000FFFF)
  13725. idlen = 0x0000FFFF;
  13726. }
  13727. if (soap->dime.type)
  13728. { typelen = strlen(soap->dime.type);
  13729. if (typelen > 0x0000FFFF)
  13730. typelen = 0x0000FFFF;
  13731. }
  13732. tmp[0] = SOAP_DIME_VERSION | (soap->dime.flags & 0x7);
  13733. tmp[1] = soap->dime.flags & 0xF0;
  13734. tmp[2] = (char)(optlen >> 8);
  13735. tmp[3] = (char)(optlen & 0xFF);
  13736. tmp[4] = (char)(idlen >> 8);
  13737. tmp[5] = (char)(idlen & 0xFF);
  13738. tmp[6] = (char)(typelen >> 8);
  13739. tmp[7] = (char)(typelen & 0xFF);
  13740. tmp[8] = (char)(soap->dime.size >> 24);
  13741. tmp[9] = (char)((soap->dime.size >> 16) & 0xFF);
  13742. tmp[10] = (char)((soap->dime.size >> 8) & 0xFF);
  13743. tmp[11] = (char)(soap->dime.size & 0xFF);
  13744. if (soap_send_raw(soap, (char*)tmp, 12)
  13745. || soap_putdimefield(soap, soap->dime.options, optlen)
  13746. || soap_putdimefield(soap, soap->dime.id, idlen)
  13747. || soap_putdimefield(soap, soap->dime.type, typelen))
  13748. return soap->error;
  13749. return SOAP_OK;
  13750. }
  13751. #endif
  13752. #endif
  13753. /******************************************************************************/
  13754. #ifndef WITH_LEANER
  13755. #ifndef PALM_1
  13756. SOAP_FMAC1
  13757. int
  13758. SOAP_FMAC2
  13759. soap_putdime(struct soap *soap)
  13760. { struct soap_multipart *content;
  13761. if (!(soap->mode & SOAP_ENC_DIME))
  13762. return SOAP_OK;
  13763. for (content = soap->dime.first; content; content = content->next)
  13764. { void *handle;
  13765. soap->dime.size = content->size;
  13766. soap->dime.id = content->id;
  13767. soap->dime.type = content->type;
  13768. soap->dime.options = content->options;
  13769. soap->dime.flags = SOAP_DIME_VERSION | SOAP_DIME_MEDIA;
  13770. if (soap->fdimereadopen && ((handle = soap->fdimereadopen(soap, (void*)content->ptr, content->id, content->type, content->options)) || soap->error))
  13771. { size_t size = content->size;
  13772. if (!handle)
  13773. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadopen failed\n"));
  13774. return soap->error;
  13775. }
  13776. if (!size && ((soap->mode & SOAP_ENC_XML) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_IO) == SOAP_IO_STORE))
  13777. { size_t chunksize = sizeof(soap->tmpbuf);
  13778. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming DIME\n"));
  13779. do
  13780. { size = soap->fdimeread(soap, handle, soap->tmpbuf, chunksize);
  13781. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimeread returned %lu bytes\n", (unsigned long)size));
  13782. if (size < chunksize)
  13783. { soap->dime.flags &= ~SOAP_DIME_CF;
  13784. if (!content->next)
  13785. soap->dime.flags |= SOAP_DIME_ME;
  13786. }
  13787. else
  13788. soap->dime.flags |= SOAP_DIME_CF;
  13789. soap->dime.size = size;
  13790. if (soap_putdimehdr(soap)
  13791. || soap_putdimefield(soap, soap->tmpbuf, size))
  13792. break;
  13793. if (soap->dime.id)
  13794. { soap->dime.flags &= ~(SOAP_DIME_MB | SOAP_DIME_MEDIA);
  13795. soap->dime.id = NULL;
  13796. soap->dime.type = NULL;
  13797. soap->dime.options = NULL;
  13798. }
  13799. } while (size >= chunksize);
  13800. }
  13801. else
  13802. { if (!content->next)
  13803. soap->dime.flags |= SOAP_DIME_ME;
  13804. if (soap_putdimehdr(soap))
  13805. return soap->error;
  13806. do
  13807. { size_t bufsize;
  13808. if (size < sizeof(soap->tmpbuf))
  13809. bufsize = size;
  13810. else
  13811. bufsize = sizeof(soap->tmpbuf);
  13812. if (!(bufsize = soap->fdimeread(soap, handle, soap->tmpbuf, bufsize)))
  13813. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n", (unsigned long)size, (unsigned long)content->size));
  13814. soap->error = SOAP_CHK_EOF;
  13815. break;
  13816. }
  13817. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  13818. break;
  13819. size -= bufsize;
  13820. } while (size);
  13821. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  13822. soap_send_raw(soap, SOAP_STR_PADDING, -(long)soap->dime.size&3);
  13823. }
  13824. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  13825. if (soap->fdimereadclose)
  13826. soap->fdimereadclose(soap, handle);
  13827. }
  13828. else
  13829. { if (!content->next)
  13830. soap->dime.flags |= SOAP_DIME_ME;
  13831. if (soap_putdimehdr(soap)
  13832. || soap_putdimefield(soap, (char*)content->ptr, content->size))
  13833. return soap->error;
  13834. }
  13835. }
  13836. return SOAP_OK;
  13837. }
  13838. #endif
  13839. #endif
  13840. /******************************************************************************/
  13841. #ifndef WITH_LEANER
  13842. #ifndef PALM_1
  13843. static char *
  13844. soap_getdimefield(struct soap *soap, size_t n)
  13845. { register soap_wchar c;
  13846. register size_t i;
  13847. register char *s;
  13848. register char *p = NULL;
  13849. if (n)
  13850. { p = (char*)soap_malloc(soap, n + 1);
  13851. if (p)
  13852. { s = p;
  13853. for (i = n; i > 0; i--)
  13854. { if ((int)(c = soap_get1(soap)) == EOF)
  13855. { soap->error = SOAP_CHK_EOF;
  13856. return NULL;
  13857. }
  13858. *s++ = (char)c;
  13859. }
  13860. *s = '\0';
  13861. if ((soap->error = soap_move(soap, (size_t)(-(long)n&3))))
  13862. return NULL;
  13863. }
  13864. else
  13865. soap->error = SOAP_EOM;
  13866. }
  13867. return p;
  13868. }
  13869. #endif
  13870. #endif
  13871. /******************************************************************************/
  13872. #ifndef WITH_LEANER
  13873. #ifndef PALM_1
  13874. SOAP_FMAC1
  13875. int
  13876. SOAP_FMAC2
  13877. soap_getdimehdr(struct soap *soap)
  13878. { register soap_wchar c;
  13879. register char *s;
  13880. register int i;
  13881. unsigned char tmp[12];
  13882. size_t optlen, idlen, typelen;
  13883. if (!(soap->mode & SOAP_ENC_DIME))
  13884. return soap->error = SOAP_DIME_END;
  13885. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME header\n"));
  13886. if (soap->dime.buflen || soap->dime.chunksize)
  13887. { if (soap_move(soap, soap->dime.size - soap_tell(soap)))
  13888. return soap->error = SOAP_CHK_EOF;
  13889. soap_unget(soap, soap_getchar(soap)); /* skip padding and get hdr */
  13890. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... From chunked\n"));
  13891. return SOAP_OK;
  13892. }
  13893. s = (char*)tmp;
  13894. for (i = 12; i > 0; i--)
  13895. { if ((int)(c = soap_getchar(soap)) == EOF)
  13896. return soap->error = SOAP_CHK_EOF;
  13897. *s++ = (char)c;
  13898. }
  13899. if ((tmp[0] & 0xF8) != SOAP_DIME_VERSION)
  13900. return soap->error = SOAP_DIME_MISMATCH;
  13901. soap->dime.flags = (tmp[0] & 0x7) | (tmp[1] & 0xF0);
  13902. optlen = (tmp[2] << 8) | tmp[3];
  13903. idlen = (tmp[4] << 8) | tmp[5];
  13904. typelen = (tmp[6] << 8) | tmp[7];
  13905. soap->dime.size = ((size_t)tmp[8] << 24) | ((size_t)tmp[9] << 16) | ((size_t)tmp[10] << 8) | ((size_t)tmp[11]);
  13906. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu flags=0x%X\n", (unsigned long)soap->dime.size, soap->dime.flags));
  13907. if (!(soap->dime.options = soap_getdimefield(soap, optlen)) && soap->error)
  13908. return soap->error;
  13909. if (!(soap->dime.id = soap_getdimefield(soap, idlen)) && soap->error)
  13910. return soap->error;
  13911. if (!(soap->dime.type = soap_getdimefield(soap, typelen)) && soap->error)
  13912. return soap->error;
  13913. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME id='%s', type='%s', options='%s'\n", soap->dime.id ? soap->dime.id : SOAP_STR_EOS, soap->dime.type ? soap->dime.type : "", soap->dime.options ? soap->dime.options+4 : SOAP_STR_EOS));
  13914. if (soap->dime.flags & SOAP_DIME_ME)
  13915. soap->mode &= ~SOAP_ENC_DIME;
  13916. return SOAP_OK;
  13917. }
  13918. #endif
  13919. #endif
  13920. /******************************************************************************/
  13921. #ifndef WITH_LEANER
  13922. #ifndef PALM_1
  13923. SOAP_FMAC1
  13924. int
  13925. SOAP_FMAC2
  13926. soap_getdime(struct soap *soap)
  13927. { while (soap->dime.flags & SOAP_DIME_CF)
  13928. { if (soap_getdimehdr(soap))
  13929. return soap->error;
  13930. if (soap_move(soap, soap->dime.size))
  13931. return soap->error = SOAP_EOF;
  13932. }
  13933. if (soap_move(soap, (size_t)(((soap->dime.size+3)&(~3)) - soap_tell(soap))))
  13934. return soap->error = SOAP_EOF;
  13935. for (;;)
  13936. { register struct soap_multipart *content;
  13937. if (soap_getdimehdr(soap))
  13938. break;
  13939. if (soap->fdimewriteopen && ((soap->dime.ptr = (char*)soap->fdimewriteopen(soap, soap->dime.id, soap->dime.type, soap->dime.options)) || soap->error))
  13940. { const char *id, *type, *options;
  13941. size_t size, n;
  13942. if (!soap->dime.ptr)
  13943. return soap->error;
  13944. id = soap->dime.id;
  13945. type = soap->dime.type;
  13946. options = soap->dime.options;
  13947. for (;;)
  13948. { size = soap->dime.size;
  13949. for (;;)
  13950. { n = soap->buflen - soap->bufidx;
  13951. if (size < n)
  13952. n = size;
  13953. if ((soap->error = soap->fdimewrite(soap, (void*)soap->dime.ptr, soap->buf + soap->bufidx, n)))
  13954. break;
  13955. size -= n;
  13956. if (!size)
  13957. { soap->bufidx += n;
  13958. break;
  13959. }
  13960. if (soap_recv(soap))
  13961. { soap->error = SOAP_EOF;
  13962. goto end;
  13963. }
  13964. }
  13965. if (soap_move(soap, (size_t)(-(long)soap->dime.size&3)))
  13966. { soap->error = SOAP_EOF;
  13967. break;
  13968. }
  13969. if (!(soap->dime.flags & SOAP_DIME_CF))
  13970. break;
  13971. if (soap_getdimehdr(soap))
  13972. break;
  13973. }
  13974. end:
  13975. if (soap->fdimewriteclose)
  13976. soap->fdimewriteclose(soap, (void*)soap->dime.ptr);
  13977. soap->dime.size = 0;
  13978. soap->dime.id = id;
  13979. soap->dime.type = type;
  13980. soap->dime.options = options;
  13981. }
  13982. else if (soap->dime.flags & SOAP_DIME_CF)
  13983. { const char *id, *type, *options;
  13984. id = soap->dime.id;
  13985. type = soap->dime.type;
  13986. options = soap->dime.options;
  13987. if (soap_new_block(soap) == NULL)
  13988. return SOAP_EOM;
  13989. for (;;)
  13990. { register soap_wchar c;
  13991. register size_t i;
  13992. register char *s;
  13993. if (soap->dime.size > SOAP_MAXDIMESIZE)
  13994. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu exceeds SOAP_MAXDIMESIZE=%lu\n", (unsigned long)soap->dime.size, (unsigned long)SOAP_MAXDIMESIZE));
  13995. return soap->error = SOAP_DIME_ERROR;
  13996. }
  13997. s = (char*)soap_push_block(soap, NULL, soap->dime.size);
  13998. if (!s)
  13999. return soap->error = SOAP_EOM;
  14000. for (i = soap->dime.size; i > 0; i--)
  14001. { if ((int)(c = soap_get1(soap)) == EOF)
  14002. return soap->error = SOAP_EOF;
  14003. *s++ = (char)c;
  14004. }
  14005. if (soap_move(soap, (size_t)(-(long)soap->dime.size&3)))
  14006. return soap->error = SOAP_EOF;
  14007. if (!(soap->dime.flags & SOAP_DIME_CF))
  14008. break;
  14009. if (soap_getdimehdr(soap))
  14010. return soap->error;
  14011. }
  14012. soap->dime.size = soap->blist->size++; /* allocate one more byte in blist for the terminating '\0' */
  14013. if (!(soap->dime.ptr = soap_save_block(soap, NULL, NULL, 0)))
  14014. return soap->error;
  14015. soap->dime.ptr[soap->dime.size] = '\0'; /* make 0-terminated */
  14016. soap->dime.id = id;
  14017. soap->dime.type = type;
  14018. soap->dime.options = options;
  14019. }
  14020. else
  14021. soap->dime.ptr = soap_getdimefield(soap, soap->dime.size);
  14022. content = soap_new_multipart(soap, &soap->dime.first, &soap->dime.last, soap->dime.ptr, soap->dime.size);
  14023. if (!content)
  14024. return soap->error = SOAP_EOM;
  14025. content->id = soap->dime.id;
  14026. content->type = soap->dime.type;
  14027. content->options = soap->dime.options;
  14028. if (soap->error)
  14029. return soap->error;
  14030. soap_resolve_attachment(soap, content);
  14031. }
  14032. if (soap->error != SOAP_DIME_END)
  14033. return soap->error;
  14034. return soap->error = SOAP_OK;
  14035. }
  14036. #endif
  14037. #endif
  14038. /******************************************************************************/
  14039. #ifndef WITH_LEANER
  14040. #ifndef PALM_1
  14041. SOAP_FMAC1
  14042. int
  14043. SOAP_FMAC2
  14044. soap_getmimehdr(struct soap *soap)
  14045. { struct soap_multipart *content;
  14046. do
  14047. { if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  14048. return soap->error;
  14049. }
  14050. while (!*soap->msgbuf);
  14051. if (soap->msgbuf[0] == '-' && soap->msgbuf[1] == '-')
  14052. { char *s = soap->msgbuf + strlen(soap->msgbuf) - 1;
  14053. /* remove white space */
  14054. while (soap_blank((soap_wchar)*s))
  14055. s--;
  14056. s[1] = '\0';
  14057. if (soap->mime.boundary)
  14058. { if (strcmp(soap->msgbuf + 2, soap->mime.boundary))
  14059. return soap->error = SOAP_MIME_ERROR;
  14060. }
  14061. else
  14062. soap->mime.boundary = soap_strdup(soap, soap->msgbuf + 2);
  14063. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  14064. return soap->error;
  14065. }
  14066. if (soap_set_mime_attachment(soap, NULL, 0, SOAP_MIME_NONE, NULL, NULL, NULL, NULL))
  14067. return soap->error = SOAP_EOM;
  14068. content = soap->mime.last;
  14069. for (;;)
  14070. { register char *key = soap->msgbuf;
  14071. register char *val;
  14072. if (!*key)
  14073. break;
  14074. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "MIME header: %s\n", key));
  14075. val = strchr(soap->msgbuf, ':');
  14076. if (val)
  14077. { *val = '\0';
  14078. do val++;
  14079. while (*val && *val <= 32);
  14080. if (!soap_tag_cmp(key, "Content-ID"))
  14081. content->id = soap_strdup(soap, val);
  14082. else if (!soap_tag_cmp(key, "Content-Location"))
  14083. content->location = soap_strdup(soap, val);
  14084. else if (!soap_tag_cmp(key, "Content-Disposition"))
  14085. content->id = soap_strdup(soap, soap_get_header_attribute(soap, val, "name"));
  14086. else if (!soap_tag_cmp(key, "Content-Type"))
  14087. content->type = soap_strdup(soap, val);
  14088. else if (!soap_tag_cmp(key, "Content-Description"))
  14089. content->description = soap_strdup(soap, val);
  14090. else if (!soap_tag_cmp(key, "Content-Transfer-Encoding"))
  14091. content->encoding = (enum soap_mime_encoding)soap_code_int(mime_codes, val, (long)SOAP_MIME_NONE);
  14092. }
  14093. if (soap_getline(soap, key, sizeof(soap->msgbuf)))
  14094. return soap->error;
  14095. }
  14096. return SOAP_OK;
  14097. }
  14098. #endif
  14099. #endif
  14100. /******************************************************************************/
  14101. #ifndef WITH_LEANER
  14102. #ifndef PALM_1
  14103. SOAP_FMAC1
  14104. int
  14105. SOAP_FMAC2
  14106. soap_getmime(struct soap *soap)
  14107. { while (soap_get_mime_attachment(soap, NULL))
  14108. ;
  14109. return soap->error;
  14110. }
  14111. #endif
  14112. #endif
  14113. /******************************************************************************/
  14114. #ifndef WITH_LEANER
  14115. #ifndef PALM_1
  14116. SOAP_FMAC1
  14117. void
  14118. SOAP_FMAC2
  14119. soap_post_check_mime_attachments(struct soap *soap)
  14120. { soap->imode |= SOAP_MIME_POSTCHECK;
  14121. }
  14122. #endif
  14123. #endif
  14124. /******************************************************************************/
  14125. #ifndef WITH_LEANER
  14126. #ifndef PALM_1
  14127. SOAP_FMAC1
  14128. int
  14129. SOAP_FMAC2
  14130. soap_check_mime_attachments(struct soap *soap)
  14131. { if (soap->mode & SOAP_MIME_POSTCHECK)
  14132. return soap_get_mime_attachment(soap, NULL) != NULL;
  14133. return SOAP_OK;
  14134. }
  14135. #endif
  14136. #endif
  14137. /******************************************************************************/
  14138. #ifndef WITH_LEANER
  14139. #ifndef PALM_1
  14140. SOAP_FMAC1
  14141. struct soap_multipart *
  14142. SOAP_FMAC2
  14143. soap_get_mime_attachment(struct soap *soap, void *handle)
  14144. { register soap_wchar c = 0;
  14145. register size_t i, m = 0;
  14146. register char *s, *t = NULL;
  14147. register struct soap_multipart *content;
  14148. register short flag = 0;
  14149. if (!(soap->mode & SOAP_ENC_MIME))
  14150. return NULL;
  14151. content = soap->mime.last;
  14152. if (!content)
  14153. { if (soap_getmimehdr(soap))
  14154. return NULL;
  14155. content = soap->mime.last;
  14156. }
  14157. else if (content != soap->mime.first)
  14158. { if (soap->fmimewriteopen && ((content->ptr = (char*)soap->fmimewriteopen(soap, (void*)handle, content->id, content->type, content->description, content->encoding)) || soap->error))
  14159. { if (!content->ptr)
  14160. return NULL;
  14161. }
  14162. }
  14163. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Parsing MIME content id='%s' type='%s'\n", content->id ? content->id : SOAP_STR_EOS, content->type ? content->type : SOAP_STR_EOS));
  14164. if (!content->ptr && soap_new_block(soap) == NULL)
  14165. { soap->error = SOAP_EOM;
  14166. return NULL;
  14167. }
  14168. for (;;)
  14169. { if (content->ptr)
  14170. s = soap->tmpbuf;
  14171. else if (!(s = (char*)soap_push_block(soap, NULL, sizeof(soap->tmpbuf))))
  14172. { soap->error = SOAP_EOM;
  14173. return NULL;
  14174. }
  14175. for (i = 0; i < sizeof(soap->tmpbuf); i++)
  14176. { if (m > 0)
  14177. { *s++ = *t++;
  14178. m--;
  14179. }
  14180. else
  14181. { if (!flag)
  14182. { c = soap_get1(soap);
  14183. if ((int)c == EOF)
  14184. { if (content->ptr && soap->fmimewriteclose)
  14185. soap->fmimewriteclose(soap, (void*)content->ptr);
  14186. soap->error = SOAP_CHK_EOF;
  14187. return NULL;
  14188. }
  14189. }
  14190. if (flag || c == '\r')
  14191. { t = soap->msgbuf;
  14192. memset(t, 0, sizeof(soap->msgbuf));
  14193. strcpy(t, "\n--");
  14194. if (soap->mime.boundary)
  14195. strncat(t, soap->mime.boundary, sizeof(soap->msgbuf)-4);
  14196. do c = soap_getchar(soap);
  14197. while (c == *t++);
  14198. if ((int)c == EOF)
  14199. { if (content->ptr && soap->fmimewriteclose)
  14200. soap->fmimewriteclose(soap, (void*)content->ptr);
  14201. soap->error = SOAP_CHK_EOF;
  14202. return NULL;
  14203. }
  14204. if (!*--t)
  14205. goto end;
  14206. *t = (char)c;
  14207. flag = (c == '\r');
  14208. m = t - soap->msgbuf + 1 - flag;
  14209. t = soap->msgbuf;
  14210. c = '\r';
  14211. }
  14212. *s++ = (char)c;
  14213. }
  14214. }
  14215. if (content->ptr && soap->fmimewrite)
  14216. { if ((soap->error = soap->fmimewrite(soap, (void*)content->ptr, soap->tmpbuf, i)))
  14217. break;
  14218. }
  14219. }
  14220. end:
  14221. *s = '\0'; /* make 0-terminated */
  14222. if (content->ptr)
  14223. { if (!soap->error && soap->fmimewrite)
  14224. soap->error = soap->fmimewrite(soap, (void*)content->ptr, soap->tmpbuf, i);
  14225. if (soap->fmimewriteclose)
  14226. soap->fmimewriteclose(soap, (void*)content->ptr);
  14227. if (soap->error)
  14228. return NULL;
  14229. }
  14230. else
  14231. { content->size = soap_size_block(soap, NULL, i+1) - 1; /* last block with '\0' */
  14232. content->ptr = soap_save_block(soap, NULL, NULL, 0);
  14233. }
  14234. soap_resolve_attachment(soap, content);
  14235. if (c == '-' && soap_getchar(soap) == '-')
  14236. { soap->mode &= ~SOAP_ENC_MIME;
  14237. if ((soap->mode & SOAP_MIME_POSTCHECK) && soap_end_recv(soap))
  14238. { if (soap->keep_alive < 0)
  14239. soap->keep_alive = 0;
  14240. soap_closesock(soap);
  14241. return NULL;
  14242. }
  14243. }
  14244. else
  14245. { while (c != '\r' && (int)c != EOF && soap_blank(c))
  14246. c = soap_getchar(soap);
  14247. if (c != '\r' || soap_getchar(soap) != '\n')
  14248. { soap->error = SOAP_MIME_ERROR;
  14249. return NULL;
  14250. }
  14251. if (soap_getmimehdr(soap))
  14252. return NULL;
  14253. }
  14254. return content;
  14255. }
  14256. #endif
  14257. #endif
  14258. /******************************************************************************/
  14259. #ifndef WITH_LEANER
  14260. #ifndef PALM_1
  14261. SOAP_FMAC1
  14262. int
  14263. SOAP_FMAC2
  14264. soap_match_cid(struct soap *soap, const char *s, const char *t)
  14265. { register size_t n;
  14266. if (!s)
  14267. return 1;
  14268. if (!strcmp(s, t))
  14269. return 0;
  14270. if (!strncmp(s, "cid:", 4))
  14271. s += 4;
  14272. n = strlen(t);
  14273. if (*t == '<')
  14274. { t++;
  14275. n -= 2;
  14276. }
  14277. if (!strncmp(s, t, n) && !s[n])
  14278. return 0;
  14279. soap_decode(soap->tmpbuf, sizeof(soap->tmpbuf), s, SOAP_STR_EOS);
  14280. if (!strncmp(soap->tmpbuf, t, n) && !soap->tmpbuf[n])
  14281. return 0;
  14282. return 1;
  14283. }
  14284. #endif
  14285. #endif
  14286. /******************************************************************************/
  14287. #ifndef WITH_LEANER
  14288. #ifndef PALM_1
  14289. static void
  14290. soap_resolve_attachment(struct soap *soap, struct soap_multipart *content)
  14291. { if (content->id)
  14292. { register struct soap_xlist **xp = &soap->xlist;
  14293. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving attachment data for id='%s'\n", content->id));
  14294. while (*xp)
  14295. { register struct soap_xlist *xq = *xp;
  14296. if (!soap_match_cid(soap, xq->id, content->id))
  14297. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Found matching attachment id='%s' for content id='%s'\n", xq->id, content->id));
  14298. *xp = xq->next;
  14299. *xq->ptr = (unsigned char*)content->ptr;
  14300. *xq->size = (int)content->size;
  14301. *xq->type = (char*)content->type;
  14302. if (content->options)
  14303. *xq->options = (char*)content->options;
  14304. else
  14305. *xq->options = (char*)content->description;
  14306. SOAP_FREE(soap, xq);
  14307. }
  14308. else
  14309. xp = &(*xp)->next;
  14310. }
  14311. }
  14312. }
  14313. #endif
  14314. #endif
  14315. /******************************************************************************/
  14316. #ifndef WITH_LEANER
  14317. #ifndef PALM_1
  14318. SOAP_FMAC1
  14319. int
  14320. SOAP_FMAC2
  14321. soap_putmimehdr(struct soap *soap, struct soap_multipart *content)
  14322. { const char *s;
  14323. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MIME attachment type='%s'\n", content->type ? content->type : SOAP_STR_EOS));
  14324. if (soap_send3(soap, "\r\n--", soap->mime.boundary, "\r\n"))
  14325. return soap->error;
  14326. if (content->type && soap_send3(soap, "Content-Type: ", content->type, "\r\n"))
  14327. return soap->error;
  14328. s = soap_code_str(mime_codes, content->encoding);
  14329. if (s && soap_send3(soap, "Content-Transfer-Encoding: ", s, "\r\n"))
  14330. return soap->error;
  14331. if (content->id && soap_send3(soap, "Content-ID: ", content->id, "\r\n"))
  14332. return soap->error;
  14333. if (content->location && soap_send3(soap, "Content-Location: ", content->location, "\r\n"))
  14334. return soap->error;
  14335. if (content->description && soap_send3(soap, "Content-Description: ", content->description, "\r\n"))
  14336. return soap->error;
  14337. return soap_send_raw(soap, "\r\n", 2);
  14338. }
  14339. #endif
  14340. #endif
  14341. /******************************************************************************/
  14342. #ifndef WITH_LEANER
  14343. #ifndef PALM_1
  14344. SOAP_FMAC1
  14345. int
  14346. SOAP_FMAC2
  14347. soap_putmime(struct soap *soap)
  14348. { struct soap_multipart *content;
  14349. if (!(soap->mode & SOAP_ENC_MIME) || !soap->mime.boundary)
  14350. return SOAP_OK;
  14351. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending MIME attachments\n"));
  14352. for (content = soap->mime.first; content; content = content->next)
  14353. { void *handle;
  14354. if (soap->fmimereadopen && ((handle = soap->fmimereadopen(soap, (void*)content->ptr, content->id, content->type, content->description)) || soap->error))
  14355. { size_t size = content->size;
  14356. if (!handle)
  14357. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimereadopen failed\n"));
  14358. return soap->error;
  14359. }
  14360. if (soap_putmimehdr(soap, content))
  14361. return soap->error;
  14362. if (!size)
  14363. { if ((soap->mode & SOAP_ENC_XML) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_IO) == SOAP_IO_STORE)
  14364. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming MIME\n"));
  14365. do
  14366. { size = soap->fmimeread(soap, handle, soap->tmpbuf, sizeof(soap->tmpbuf));
  14367. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimeread returned %lu bytes\n", (unsigned long)size));
  14368. if (soap_send_raw(soap, soap->tmpbuf, size))
  14369. break;
  14370. } while (size);
  14371. }
  14372. else
  14373. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error: cannot chunk streaming MIME (no HTTP chunking)\n"));
  14374. }
  14375. }
  14376. else
  14377. { do
  14378. { size_t bufsize;
  14379. if (size < sizeof(soap->tmpbuf))
  14380. bufsize = size;
  14381. else
  14382. bufsize = sizeof(soap->tmpbuf);
  14383. if (!(bufsize = soap->fmimeread(soap, handle, soap->tmpbuf, bufsize)))
  14384. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n", (unsigned long)size, (unsigned long)content->size));
  14385. soap->error = SOAP_EOF;
  14386. break;
  14387. }
  14388. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  14389. break;
  14390. size -= bufsize;
  14391. } while (size);
  14392. }
  14393. if (soap->fmimereadclose)
  14394. soap->fmimereadclose(soap, handle);
  14395. }
  14396. else
  14397. { if (soap_putmimehdr(soap, content)
  14398. || soap_send_raw(soap, content->ptr, content->size))
  14399. return soap->error;
  14400. }
  14401. }
  14402. return soap_send3(soap, "\r\n--", soap->mime.boundary, "--");
  14403. }
  14404. #endif
  14405. #endif
  14406. /******************************************************************************/
  14407. #ifndef WITH_LEANER
  14408. #ifndef PALM_1
  14409. SOAP_FMAC1
  14410. void
  14411. SOAP_FMAC2
  14412. soap_set_dime(struct soap *soap)
  14413. { soap->omode |= SOAP_ENC_DIME;
  14414. soap->dime.first = NULL;
  14415. soap->dime.last = NULL;
  14416. }
  14417. #endif
  14418. #endif
  14419. /******************************************************************************/
  14420. #ifndef WITH_LEANER
  14421. #ifndef PALM_1
  14422. SOAP_FMAC1
  14423. void
  14424. SOAP_FMAC2
  14425. soap_set_mime(struct soap *soap, const char *boundary, const char *start)
  14426. { soap->omode |= SOAP_ENC_MIME;
  14427. soap->mime.first = NULL;
  14428. soap->mime.last = NULL;
  14429. soap->mime.boundary = soap_strdup(soap, boundary);
  14430. soap->mime.start = soap_strdup(soap, start);
  14431. }
  14432. #endif
  14433. #endif
  14434. /******************************************************************************/
  14435. #ifndef WITH_LEANER
  14436. #ifndef PALM_1
  14437. SOAP_FMAC1
  14438. void
  14439. SOAP_FMAC2
  14440. soap_clr_dime(struct soap *soap)
  14441. { soap->omode &= ~SOAP_ENC_DIME;
  14442. soap->dime.first = NULL;
  14443. soap->dime.last = NULL;
  14444. }
  14445. #endif
  14446. #endif
  14447. /******************************************************************************/
  14448. #ifndef WITH_LEANER
  14449. #ifndef PALM_1
  14450. SOAP_FMAC1
  14451. void
  14452. SOAP_FMAC2
  14453. soap_clr_mime(struct soap *soap)
  14454. { soap->omode &= ~SOAP_ENC_MIME;
  14455. soap->mime.first = NULL;
  14456. soap->mime.last = NULL;
  14457. soap->mime.boundary = NULL;
  14458. soap->mime.start = NULL;
  14459. }
  14460. #endif
  14461. #endif
  14462. /******************************************************************************/
  14463. #ifndef WITH_LEANER
  14464. #ifndef PALM_1
  14465. static struct soap_multipart*
  14466. soap_new_multipart(struct soap *soap, struct soap_multipart **first, struct soap_multipart **last, char *ptr, size_t size)
  14467. { struct soap_multipart *content;
  14468. content = (struct soap_multipart*)soap_malloc(soap, sizeof(struct soap_multipart));
  14469. if (content)
  14470. { content->next = NULL;
  14471. content->ptr = ptr;
  14472. content->size = size;
  14473. content->id = NULL;
  14474. content->type = NULL;
  14475. content->options = NULL;
  14476. content->encoding = SOAP_MIME_NONE;
  14477. content->location = NULL;
  14478. content->description = NULL;
  14479. if (!*first)
  14480. *first = content;
  14481. if (*last)
  14482. (*last)->next = content;
  14483. *last = content;
  14484. }
  14485. return content;
  14486. }
  14487. #endif
  14488. #endif
  14489. /******************************************************************************/
  14490. #ifndef WITH_LEANER
  14491. #ifndef PALM_1
  14492. SOAP_FMAC1
  14493. int
  14494. SOAP_FMAC2
  14495. soap_set_dime_attachment(struct soap *soap, char *ptr, size_t size, const char *type, const char *id, unsigned short optype, const char *option)
  14496. { struct soap_multipart *content = soap_new_multipart(soap, &soap->dime.first, &soap->dime.last, ptr, size);
  14497. if (!content)
  14498. return SOAP_EOM;
  14499. content->id = soap_strdup(soap, id);
  14500. content->type = soap_strdup(soap, type);
  14501. content->options = soap_dime_option(soap, optype, option);
  14502. return SOAP_OK;
  14503. }
  14504. #endif
  14505. #endif
  14506. /******************************************************************************/
  14507. #ifndef WITH_LEANER
  14508. #ifndef PALM_1
  14509. SOAP_FMAC1
  14510. int
  14511. SOAP_FMAC2
  14512. soap_set_mime_attachment(struct soap *soap, char *ptr, size_t size, enum soap_mime_encoding encoding, const char *type, const char *id, const char *location, const char *description)
  14513. { struct soap_multipart *content = soap_new_multipart(soap, &soap->mime.first, &soap->mime.last, ptr, size);
  14514. if (!content)
  14515. return SOAP_EOM;
  14516. content->id = soap_strdup(soap, id);
  14517. content->type = soap_strdup(soap, type);
  14518. content->encoding = encoding;
  14519. content->location = soap_strdup(soap, location);
  14520. content->description = soap_strdup(soap, description);
  14521. return SOAP_OK;
  14522. }
  14523. #endif
  14524. #endif
  14525. /******************************************************************************/
  14526. #ifndef WITH_LEANER
  14527. #ifndef PALM_1
  14528. SOAP_FMAC1
  14529. struct soap_multipart*
  14530. SOAP_FMAC2
  14531. soap_next_multipart(struct soap_multipart *content)
  14532. { if (content)
  14533. return content->next;
  14534. return NULL;
  14535. }
  14536. #endif
  14537. #endif
  14538. /******************************************************************************/
  14539. #ifndef WITH_LEANER
  14540. #ifndef PALM_1
  14541. static void
  14542. soap_select_mime_boundary(struct soap *soap)
  14543. { while (!soap->mime.boundary || soap_valid_mime_boundary(soap))
  14544. { register char *s = soap->mime.boundary;
  14545. register size_t n = 0;
  14546. if (s)
  14547. n = strlen(s);
  14548. if (n < 16)
  14549. { n = 64;
  14550. s = soap->mime.boundary = (char*)soap_malloc(soap, n + 1);
  14551. if (!s)
  14552. return;
  14553. }
  14554. strcpy(s, "==");
  14555. s += 2;
  14556. n -= 4;
  14557. while (n)
  14558. { *s++ = soap_base64o[soap_random & 0x3F];
  14559. n--;
  14560. }
  14561. strcpy(s, "==");
  14562. }
  14563. if (!soap->mime.start)
  14564. soap->mime.start = "<SOAP-ENV:Envelope>";
  14565. }
  14566. #endif
  14567. #endif
  14568. /******************************************************************************/
  14569. #ifndef WITH_LEANER
  14570. #ifndef PALM_1
  14571. static int
  14572. soap_valid_mime_boundary(struct soap *soap)
  14573. { register struct soap_multipart *content;
  14574. register size_t k;
  14575. if (soap->fmimeread)
  14576. return SOAP_OK;
  14577. k = strlen(soap->mime.boundary);
  14578. for (content = soap->mime.first; content; content = content->next)
  14579. { if (content->ptr && content->size >= k)
  14580. { register const char *p = (const char*)content->ptr;
  14581. register size_t i;
  14582. for (i = 0; i < content->size - k; i++, p++)
  14583. { if (!strncmp(p, soap->mime.boundary, k))
  14584. return SOAP_ERR;
  14585. }
  14586. }
  14587. }
  14588. return SOAP_OK;
  14589. }
  14590. #endif
  14591. #endif
  14592. /******************************************************************************/
  14593. #ifdef WITH_GZIP
  14594. #ifndef PALM_1
  14595. static int
  14596. soap_getgziphdr(struct soap *soap)
  14597. { int i;
  14598. soap_wchar c = 0, f = 0;
  14599. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get gzip header\n"));
  14600. for (i = 0; i < 9; i++)
  14601. { if ((int)(c = soap_get1(soap) == EOF))
  14602. return soap->error = SOAP_ZLIB_ERROR;
  14603. if (i == 1 && c == 8)
  14604. soap->z_dict = 0;
  14605. if (i == 2)
  14606. f = c;
  14607. }
  14608. if (f & 0x04) /* FEXTRA */
  14609. { for (i = soap_get1(soap) | (soap_get1(soap) << 8); i; i--)
  14610. { if ((int)soap_get1(soap) == EOF)
  14611. return soap->error = SOAP_ZLIB_ERROR;
  14612. }
  14613. }
  14614. if (f & 0x08) /* skip FNAME */
  14615. { do
  14616. c = soap_get1(soap);
  14617. while (c && (int)c != EOF);
  14618. }
  14619. if ((int)c != EOF && (f & 0x10)) /* skip FCOMMENT */
  14620. { do
  14621. c = soap_get1(soap);
  14622. while (c && (int)c != EOF);
  14623. }
  14624. if ((int)c != EOF && (f & 0x02)) /* skip FHCRC (CRC32 is used) */
  14625. { if ((int)(c = soap_get1(soap)) != EOF)
  14626. c = soap_get1(soap);
  14627. }
  14628. if ((int)c == EOF)
  14629. return soap->error = SOAP_ZLIB_ERROR;
  14630. return SOAP_OK;
  14631. }
  14632. #endif
  14633. #endif
  14634. /******************************************************************************/
  14635. #ifndef PALM_1
  14636. SOAP_FMAC1
  14637. int
  14638. SOAP_FMAC2
  14639. soap_begin_serve(struct soap *soap)
  14640. {
  14641. #ifdef WITH_FASTCGI
  14642. if (FCGI_Accept() < 0)
  14643. { soap->error = SOAP_EOF;
  14644. return soap_send_fault(soap);
  14645. }
  14646. #endif
  14647. soap_begin(soap);
  14648. if (soap_begin_recv(soap)
  14649. || soap_envelope_begin_in(soap)
  14650. || soap_recv_header(soap)
  14651. || soap_body_begin_in(soap))
  14652. { if (soap->error < SOAP_STOP)
  14653. {
  14654. #ifdef WITH_FASTCGI
  14655. soap_send_fault(soap);
  14656. #else
  14657. return soap_send_fault(soap);
  14658. #endif
  14659. }
  14660. return soap_closesock(soap);
  14661. }
  14662. return SOAP_OK;
  14663. }
  14664. #endif
  14665. /******************************************************************************/
  14666. #ifndef PALM_1
  14667. SOAP_FMAC1
  14668. int
  14669. SOAP_FMAC2
  14670. soap_begin_recv(struct soap *soap)
  14671. { register soap_wchar c;
  14672. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Initializing for input from socket=%d/fd=%d\n", soap->socket, soap->recvfd));
  14673. soap->error = SOAP_OK;
  14674. soap->filterstop = SOAP_OK;
  14675. soap_free_temp(soap);
  14676. soap_set_local_namespaces(soap);
  14677. soap->version = 0; /* don't assume we're parsing SOAP content by default */
  14678. #ifndef WITH_NOIDREF
  14679. soap_free_iht(soap);
  14680. #endif
  14681. if ((soap->imode & SOAP_IO) == SOAP_IO_CHUNK)
  14682. soap->omode |= SOAP_IO_CHUNK;
  14683. soap->imode &= ~(SOAP_IO | SOAP_ENC_MIME);
  14684. soap->mode = soap->imode;
  14685. if (!soap->keep_alive)
  14686. { soap->buflen = 0;
  14687. soap->bufidx = 0;
  14688. }
  14689. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  14690. soap->keep_alive = 0;
  14691. soap->ahead = 0;
  14692. soap->peeked = 0;
  14693. soap->level = 0;
  14694. soap->part = SOAP_BEGIN;
  14695. soap->alloced = 0;
  14696. soap->body = 1;
  14697. soap->count = 0;
  14698. soap->length = 0;
  14699. soap->cdata = 0;
  14700. *soap->endpoint = '\0';
  14701. soap->action = NULL;
  14702. soap->header = NULL;
  14703. soap->fault = NULL;
  14704. soap->status = 0;
  14705. soap->fform = NULL;
  14706. #ifndef WITH_LEANER
  14707. soap->dom = NULL;
  14708. soap->dime.chunksize = 0;
  14709. soap->dime.buflen = 0;
  14710. soap->dime.list = NULL;
  14711. soap->dime.first = NULL;
  14712. soap->dime.last = NULL;
  14713. soap->mime.list = NULL;
  14714. soap->mime.first = NULL;
  14715. soap->mime.last = NULL;
  14716. soap->mime.boundary = NULL;
  14717. soap->mime.start = NULL;
  14718. #endif
  14719. #ifdef WIN32
  14720. #ifndef UNDER_CE
  14721. #ifndef WITH_FASTCGI
  14722. if (!soap_valid_socket(soap->socket) && !soap->is) /* Set win32 stdout or soap->sendfd to BINARY, e.g. to support DIME */
  14723. #ifdef __BORLANDC__
  14724. setmode(soap->recvfd, _O_BINARY);
  14725. #else
  14726. _setmode(soap->recvfd, _O_BINARY);
  14727. #endif
  14728. #endif
  14729. #endif
  14730. #endif
  14731. #ifdef WITH_ZLIB
  14732. soap->mode &= ~SOAP_ENC_ZLIB;
  14733. soap->zlib_in = SOAP_ZLIB_NONE;
  14734. soap->zlib_out = SOAP_ZLIB_NONE;
  14735. soap->d_stream->next_in = Z_NULL;
  14736. soap->d_stream->avail_in = 0;
  14737. soap->d_stream->next_out = (Byte*)soap->buf;
  14738. soap->d_stream->avail_out = SOAP_BUFLEN;
  14739. soap->z_ratio_in = 1.0;
  14740. #endif
  14741. #ifdef WITH_OPENSSL
  14742. if (soap->ssl)
  14743. ERR_clear_error();
  14744. #endif
  14745. #ifndef WITH_LEANER
  14746. if (soap->fprepareinitrecv && (soap->error = soap->fprepareinitrecv(soap)))
  14747. return soap->error;
  14748. #endif
  14749. c = soap_getchar(soap);
  14750. #ifdef WITH_GZIP
  14751. if (c == 0x1F)
  14752. { if (soap_getgziphdr(soap))
  14753. return soap->error;
  14754. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  14755. return soap->error = SOAP_ZLIB_ERROR;
  14756. if (soap->z_dict)
  14757. { if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  14758. return soap->error = SOAP_ZLIB_ERROR;
  14759. }
  14760. soap->zlib_state = SOAP_ZLIB_INFLATE;
  14761. soap->mode |= SOAP_ENC_ZLIB;
  14762. soap->zlib_in = SOAP_ZLIB_GZIP;
  14763. soap->z_crc = crc32(0L, NULL, 0);
  14764. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  14765. if (!soap->z_buf)
  14766. soap->z_buf = (char*)SOAP_MALLOC(soap, SOAP_BUFLEN);
  14767. memcpy(soap->z_buf, soap->buf, SOAP_BUFLEN);
  14768. /* should not chunk over plain transport, so why bother to check? */
  14769. /* if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) */
  14770. /* soap->z_buflen = soap->bufidx; */
  14771. /* else */
  14772. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  14773. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  14774. soap->z_buflen = soap->buflen;
  14775. soap->buflen = soap->bufidx;
  14776. c = ' ';
  14777. }
  14778. #endif
  14779. while (soap_blank(c))
  14780. c = soap_getchar(soap);
  14781. #ifndef WITH_LEANER
  14782. if (c == '-' && soap_get0(soap) == '-')
  14783. soap->mode |= SOAP_ENC_MIME;
  14784. else if ((c & 0xFFFC) == (SOAP_DIME_VERSION | SOAP_DIME_MB) && (soap_get0(soap) & 0xFFF0) == 0x20)
  14785. soap->mode |= SOAP_ENC_DIME;
  14786. else
  14787. #endif
  14788. { /* skip BOM */
  14789. if (c == 0xEF && soap_get0(soap) == 0xBB)
  14790. { c = soap_get1(soap);
  14791. if ((c = soap_get1(soap)) == 0xBF)
  14792. { soap->mode &= ~SOAP_ENC_LATIN;
  14793. c = soap_getchar(soap);
  14794. }
  14795. else
  14796. c = (0x0F << 12) | (0xBB << 6) | (c & 0x3F); /* UTF-8 */
  14797. }
  14798. else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  14799. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  14800. return soap->error = SOAP_UTF_ERROR;
  14801. /* skip space */
  14802. while (soap_blank(c))
  14803. c = soap_getchar(soap);
  14804. }
  14805. if ((int)c == EOF)
  14806. return soap->error = SOAP_CHK_EOF;
  14807. soap_unget(soap, c);
  14808. #ifndef WITH_NOHTTP
  14809. /* if not XML/MIME/DIME/ZLIB, assume HTTP method or status line */
  14810. if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && !(soap->mode & (SOAP_ENC_MIME | SOAP_ENC_DIME | SOAP_ENC_ZLIB | SOAP_ENC_XML)))
  14811. { soap_mode m = soap->imode;
  14812. soap->mode &= ~SOAP_IO;
  14813. soap->error = soap->fparse(soap);
  14814. if (soap->error && soap->error < SOAP_STOP)
  14815. { soap->keep_alive = 0; /* force close later */
  14816. return soap->error;
  14817. }
  14818. if (soap->error == SOAP_STOP)
  14819. { if (soap->fform)
  14820. { soap->error = soap->fform(soap);
  14821. if (soap->error == SOAP_OK)
  14822. soap->error = SOAP_STOP; /* prevents further processing */
  14823. }
  14824. return soap->error;
  14825. }
  14826. soap->mode = soap->imode; /* if imode is changed, effectuate */
  14827. soap->imode = m; /* restore imode */
  14828. #ifdef WITH_ZLIB
  14829. soap->mode &= ~SOAP_ENC_ZLIB;
  14830. #endif
  14831. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  14832. { soap->chunkbuflen = soap->buflen;
  14833. soap->buflen = soap->bufidx;
  14834. soap->chunksize = 0;
  14835. }
  14836. /* Note: fparse should not use soap_unget to push back last char */
  14837. #if 0
  14838. if (soap->status > 200 && soap->length == 0 && !(soap->http_content && (!soap->keep_alive || soap->recv_timeout)) && (soap->imode & SOAP_IO) != SOAP_IO_CHUNK)
  14839. #endif
  14840. if (soap->status && !soap->body)
  14841. return soap->error = soap->status;
  14842. #ifdef WITH_ZLIB
  14843. if (soap->zlib_in != SOAP_ZLIB_NONE)
  14844. {
  14845. #ifdef WITH_GZIP
  14846. if (soap->zlib_in != SOAP_ZLIB_DEFLATE)
  14847. { c = soap_get1(soap);
  14848. if (c == (int)EOF)
  14849. return soap->error = SOAP_EOF;
  14850. if (c == 0x1F)
  14851. { if (soap_getgziphdr(soap))
  14852. return soap->error;
  14853. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  14854. return soap->error = SOAP_ZLIB_ERROR;
  14855. soap->z_crc = crc32(0L, NULL, 0);
  14856. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  14857. }
  14858. else
  14859. { soap_revget1(soap);
  14860. if (inflateInit(soap->d_stream) != Z_OK)
  14861. return soap->error = SOAP_ZLIB_ERROR;
  14862. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  14863. }
  14864. }
  14865. else
  14866. #endif
  14867. if (inflateInit(soap->d_stream) != Z_OK)
  14868. return soap->error = SOAP_ZLIB_ERROR;
  14869. if (soap->z_dict)
  14870. { if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  14871. return soap->error = SOAP_ZLIB_ERROR;
  14872. }
  14873. soap->zlib_state = SOAP_ZLIB_INFLATE;
  14874. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate initialized\n"));
  14875. soap->mode |= SOAP_ENC_ZLIB;
  14876. if (!soap->z_buf)
  14877. soap->z_buf = (char*)SOAP_MALLOC(soap, SOAP_BUFLEN);
  14878. memcpy(soap->z_buf, soap->buf, SOAP_BUFLEN);
  14879. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  14880. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  14881. soap->z_buflen = soap->buflen;
  14882. soap->buflen = soap->bufidx;
  14883. }
  14884. #endif
  14885. #ifndef WITH_LEANER
  14886. if (soap->fpreparerecv && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK && soap->buflen > soap->bufidx)
  14887. { int r;
  14888. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking fpreparerecv\n"));
  14889. if ((r = soap->fpreparerecv(soap, soap->buf + soap->bufidx, soap->buflen - soap->bufidx)))
  14890. return soap->error = r;
  14891. }
  14892. #endif
  14893. if (soap_get0(soap) == (int)EOF)
  14894. { if (soap->status == 0 || soap->status == 200)
  14895. return soap->error = SOAP_NO_DATA; /* HTTP OK: always expect data */
  14896. return soap->error = soap->status;
  14897. }
  14898. if (soap->error)
  14899. { if (soap->error == SOAP_FORM && soap->fform)
  14900. { soap->error = soap->fform(soap);
  14901. if (soap->error == SOAP_OK)
  14902. soap->error = SOAP_STOP; /* prevents further processing */
  14903. }
  14904. return soap->error;
  14905. }
  14906. }
  14907. #endif
  14908. #ifndef WITH_LEANER
  14909. if (soap->mode & SOAP_ENC_MIME)
  14910. { do /* skip preamble */
  14911. { if ((int)(c = soap_getchar(soap)) == EOF)
  14912. return soap->error = SOAP_CHK_EOF;
  14913. } while (c != '-' || soap_get0(soap) != '-');
  14914. soap_unget(soap, c);
  14915. if (soap_getmimehdr(soap))
  14916. return soap->error;
  14917. if (soap->mime.start)
  14918. { do
  14919. { if (!soap->mime.last->id)
  14920. break;
  14921. if (!soap_match_cid(soap, soap->mime.start, soap->mime.last->id))
  14922. break;
  14923. } while (soap_get_mime_attachment(soap, NULL));
  14924. }
  14925. if (soap_get_header_attribute(soap, soap->mime.first->type, "application/dime"))
  14926. soap->mode |= SOAP_ENC_DIME;
  14927. }
  14928. if (soap->mode & SOAP_ENC_DIME)
  14929. { if (soap_getdimehdr(soap))
  14930. return soap->error;
  14931. if (soap->dime.flags & SOAP_DIME_CF)
  14932. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked DIME SOAP message\n"));
  14933. soap->dime.chunksize = soap->dime.size;
  14934. if (soap->buflen - soap->bufidx >= soap->dime.chunksize)
  14935. { soap->dime.buflen = soap->buflen;
  14936. soap->buflen = soap->bufidx + soap->dime.chunksize;
  14937. }
  14938. else
  14939. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  14940. }
  14941. soap->count = soap->buflen - soap->bufidx;
  14942. }
  14943. #endif
  14944. return SOAP_OK;
  14945. }
  14946. #endif
  14947. /******************************************************************************/
  14948. #ifndef PALM_2
  14949. SOAP_FMAC1
  14950. int
  14951. SOAP_FMAC2
  14952. soap_envelope_begin_out(struct soap *soap)
  14953. {
  14954. #ifndef WITH_LEANER
  14955. size_t n = 0;
  14956. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary && soap->mime.start && strlen(soap->mime.boundary) + strlen(soap->mime.start) < sizeof(soap->tmpbuf) - 80 )
  14957. { const char *s;
  14958. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  14959. s = "application/dime";
  14960. else if (soap->version == 2)
  14961. { if (soap->mode & SOAP_ENC_MTOM)
  14962. s = "application/xop+xml; charset=utf-8; type=\"application/soap+xml\"";
  14963. else
  14964. s = "application/soap+xml; charset=utf-8";
  14965. }
  14966. else if (soap->mode & SOAP_ENC_MTOM)
  14967. s = "application/xop+xml; charset=utf-8; type=\"text/xml\"";
  14968. else
  14969. s = "text/xml; charset=utf-8";
  14970. #ifdef HAVE_SNPRINTF
  14971. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "--%s\r\nContent-Type: %s\r\nContent-Transfer-Encoding: binary\r\nContent-ID: %s\r\n\r\n", soap->mime.boundary, s, soap->mime.start);
  14972. #else
  14973. sprintf(soap->tmpbuf, "--%s\r\nContent-Type: %s\r\nContent-Transfer-Encoding: binary\r\nContent-ID: %s\r\n\r\n", soap->mime.boundary, s, soap->mime.start);
  14974. #endif
  14975. n = strlen(soap->tmpbuf);
  14976. if (soap_send_raw(soap, soap->tmpbuf, n))
  14977. return soap->error;
  14978. }
  14979. if (soap->mode & SOAP_IO_LENGTH)
  14980. soap->dime.size = soap->count; /* DIME in MIME correction */
  14981. if (!(soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME))
  14982. { if (soap_putdimehdr(soap))
  14983. return soap->error;
  14984. }
  14985. #endif
  14986. if (soap->version == 0)
  14987. return SOAP_OK;
  14988. soap->part = SOAP_IN_ENVELOPE;
  14989. return soap_element_begin_out(soap, "SOAP-ENV:Envelope", 0, NULL);
  14990. }
  14991. #endif
  14992. /******************************************************************************/
  14993. #ifndef PALM_2
  14994. SOAP_FMAC1
  14995. int
  14996. SOAP_FMAC2
  14997. soap_envelope_end_out(struct soap *soap)
  14998. { if (soap->version == 0)
  14999. return SOAP_OK;
  15000. if (soap_element_end_out(soap, "SOAP-ENV:Envelope")
  15001. || soap_send_raw(soap, "\r\n", 2)) /* 2.8: always emit \r\n */
  15002. return soap->error;
  15003. #ifndef WITH_LEANER
  15004. if ((soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  15005. { soap->dime.size = soap->count - soap->dime.size; /* DIME in MIME correction */
  15006. #ifdef HAVE_SNPRINTF
  15007. soap_snprintf(soap->id, sizeof(soap->id), soap->dime_id_format, 0);
  15008. #else
  15009. sprintf(soap->id, soap->dime_id_format, 0);
  15010. #endif
  15011. soap->dime.id = soap->id;
  15012. if (soap->local_namespaces)
  15013. { if (soap->local_namespaces[0].out)
  15014. soap->dime.type = (char*)soap->local_namespaces[0].out;
  15015. else
  15016. soap->dime.type = (char*)soap->local_namespaces[0].ns;
  15017. }
  15018. soap->dime.options = NULL;
  15019. soap->dime.flags = SOAP_DIME_MB | SOAP_DIME_ABSURI;
  15020. if (!soap->dime.first)
  15021. soap->dime.flags |= SOAP_DIME_ME;
  15022. soap->count += 12 + ((strlen(soap->dime.id)+3)&(~3)) + (soap->dime.type ? ((strlen(soap->dime.type)+3)&(~3)) : 0);
  15023. }
  15024. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  15025. return soap_send_raw(soap, SOAP_STR_PADDING, -(long)soap->dime.size&3);
  15026. #endif
  15027. soap->part = SOAP_END_ENVELOPE;
  15028. return SOAP_OK;
  15029. }
  15030. #endif
  15031. /******************************************************************************/
  15032. #ifndef WITH_LEAN
  15033. #ifndef PALM_1
  15034. SOAP_FMAC1
  15035. char*
  15036. SOAP_FMAC2
  15037. soap_get_http_body(struct soap *soap, size_t *len)
  15038. { if (len)
  15039. *len = 0;
  15040. #ifndef WITH_LEAN
  15041. register size_t l = 0, n = 0;
  15042. register char *s;
  15043. /* get HTTP body length */
  15044. if (!(soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK)
  15045. { n = soap->length;
  15046. if (!n)
  15047. return NULL;
  15048. }
  15049. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Parsing HTTP body (mode=0x%x,len=%lu)\n", soap->mode, (unsigned long)n));
  15050. #ifdef WITH_FAST
  15051. soap->labidx = 0; /* use look-aside buffer */
  15052. #else
  15053. if (soap_new_block(soap) == NULL)
  15054. return NULL;
  15055. #endif
  15056. for (;;)
  15057. {
  15058. #ifdef WITH_FAST
  15059. register size_t i, k;
  15060. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  15061. return NULL;
  15062. s = soap->labbuf + soap->labidx; /* space to populate */
  15063. k = soap->lablen - soap->labidx; /* number of bytes available */
  15064. soap->labidx = soap->lablen; /* claim this space */
  15065. #else
  15066. register size_t i, k = SOAP_BLKLEN;
  15067. if (!(s = (char*)soap_push_block(soap, NULL, k)))
  15068. return NULL;
  15069. #endif
  15070. for (i = 0; i < k; i++)
  15071. { register soap_wchar c;
  15072. l++;
  15073. if (n > 0 && l > n)
  15074. goto end;
  15075. c = soap_get1(soap);
  15076. if ((int)c == EOF)
  15077. goto end;
  15078. *s++ = (char)(c & 0xFF);
  15079. }
  15080. }
  15081. end:
  15082. *s = '\0';
  15083. if (len)
  15084. *len = l - 1; /* len excludes terminating \0 */
  15085. #ifdef WITH_FAST
  15086. if ((s = (char*)soap_malloc(soap, l)))
  15087. memcpy(s, soap->labbuf, l);
  15088. #else
  15089. soap_size_block(soap, NULL, i+1);
  15090. s = soap_save_block(soap, NULL, 0);
  15091. #endif
  15092. return s;
  15093. #else
  15094. return NULL;
  15095. #endif
  15096. }
  15097. #endif
  15098. #endif
  15099. /******************************************************************************/
  15100. #ifndef PALM_1
  15101. SOAP_FMAC1
  15102. int
  15103. SOAP_FMAC2
  15104. soap_envelope_begin_in(struct soap *soap)
  15105. { register struct Namespace *p;
  15106. soap->part = SOAP_IN_ENVELOPE;
  15107. if (soap_element_begin_in(soap, "SOAP-ENV:Envelope", 0, NULL))
  15108. { if (soap->error == SOAP_TAG_MISMATCH)
  15109. { if (!soap_element_begin_in(soap, "Envelope", 0, NULL))
  15110. soap->error = SOAP_VERSIONMISMATCH;
  15111. else if (soap->status == 0 || (soap->status >= 200 && soap->status <= 299))
  15112. return SOAP_OK; /* allow non-SOAP XML content to be captured */
  15113. soap->error = soap->status;
  15114. }
  15115. else if (soap->status)
  15116. soap->error = soap->status;
  15117. return soap->error;
  15118. }
  15119. p = soap->local_namespaces;
  15120. if (p)
  15121. { const char *ns = p[0].out;
  15122. if (!ns)
  15123. ns = p[0].ns;
  15124. if (!strcmp(ns, soap_env1))
  15125. { soap->version = 1; /* make sure we use SOAP 1.1 */
  15126. if (p[1].out)
  15127. SOAP_FREE(soap, p[1].out);
  15128. if ((p[1].out = (char*)SOAP_MALLOC(soap, sizeof(soap_enc1))))
  15129. strcpy(p[1].out, soap_enc1);
  15130. }
  15131. else if (!strcmp(ns, soap_env2))
  15132. { soap->version = 2; /* make sure we use SOAP 1.2 */
  15133. if (p[1].out)
  15134. SOAP_FREE(soap, p[1].out);
  15135. if ((p[1].out = (char*)SOAP_MALLOC(soap, sizeof(soap_enc2))))
  15136. strcpy(p[1].out, soap_enc2);
  15137. }
  15138. }
  15139. return SOAP_OK;
  15140. }
  15141. #endif
  15142. /******************************************************************************/
  15143. #ifndef PALM_1
  15144. SOAP_FMAC1
  15145. int
  15146. SOAP_FMAC2
  15147. soap_envelope_end_in(struct soap *soap)
  15148. { if (soap->version == 0)
  15149. return SOAP_OK;
  15150. soap->part = SOAP_END_ENVELOPE;
  15151. return soap_element_end_in(soap, "SOAP-ENV:Envelope");
  15152. }
  15153. #endif
  15154. /******************************************************************************/
  15155. #ifndef PALM_2
  15156. SOAP_FMAC1
  15157. int
  15158. SOAP_FMAC2
  15159. soap_body_begin_out(struct soap *soap)
  15160. { if (soap->version == 1)
  15161. soap->encoding = 1;
  15162. #ifndef WITH_LEAN
  15163. if ((soap->mode & SOAP_SEC_WSUID) && soap_set_attr(soap, "wsu:Id", "Body", 1))
  15164. return soap->error;
  15165. #endif
  15166. if (soap->version == 0)
  15167. return SOAP_OK;
  15168. soap->part = SOAP_IN_BODY;
  15169. return soap_element_begin_out(soap, "SOAP-ENV:Body", 0, NULL);
  15170. }
  15171. #endif
  15172. /******************************************************************************/
  15173. #ifndef PALM_2
  15174. SOAP_FMAC1
  15175. int
  15176. SOAP_FMAC2
  15177. soap_body_end_out(struct soap *soap)
  15178. { if (soap->version == 0)
  15179. return SOAP_OK;
  15180. if (soap_element_end_out(soap, "SOAP-ENV:Body"))
  15181. return soap->error;
  15182. soap->part = SOAP_END_BODY;
  15183. return SOAP_OK;
  15184. }
  15185. #endif
  15186. /******************************************************************************/
  15187. #ifndef PALM_2
  15188. SOAP_FMAC1
  15189. int
  15190. SOAP_FMAC2
  15191. soap_body_begin_in(struct soap *soap)
  15192. { if (soap->version == 0)
  15193. return SOAP_OK;
  15194. soap->part = SOAP_IN_BODY;
  15195. if (soap_element_begin_in(soap, "SOAP-ENV:Body", 0, NULL))
  15196. return soap->error;
  15197. if (!soap->body)
  15198. soap->part = SOAP_NO_BODY;
  15199. return SOAP_OK;
  15200. }
  15201. #endif
  15202. /******************************************************************************/
  15203. #ifndef PALM_2
  15204. SOAP_FMAC1
  15205. int
  15206. SOAP_FMAC2
  15207. soap_body_end_in(struct soap *soap)
  15208. { if (soap->version == 0)
  15209. return SOAP_OK;
  15210. if (soap->part == SOAP_NO_BODY)
  15211. return soap->error = SOAP_OK;
  15212. soap->part = SOAP_END_BODY;
  15213. return soap_element_end_in(soap, "SOAP-ENV:Body");
  15214. }
  15215. #endif
  15216. /******************************************************************************/
  15217. #ifndef PALM_2
  15218. SOAP_FMAC1
  15219. int
  15220. SOAP_FMAC2
  15221. soap_recv_header(struct soap *soap)
  15222. { if (soap_getheader(soap) && soap->error == SOAP_TAG_MISMATCH)
  15223. soap->error = SOAP_OK;
  15224. if (soap->error == SOAP_OK && soap->fheader)
  15225. soap->error = soap->fheader(soap);
  15226. return soap->error;
  15227. }
  15228. #endif
  15229. /******************************************************************************/
  15230. #ifndef PALM_1
  15231. SOAP_FMAC1
  15232. void
  15233. SOAP_FMAC2
  15234. soap_set_endpoint(struct soap *soap, const char *endpoint)
  15235. { register const char *s;
  15236. register size_t i, n;
  15237. soap->endpoint[0] = '\0';
  15238. soap->host[0] = '\0';
  15239. soap->path[0] = '/';
  15240. soap->path[1] = '\0';
  15241. soap->port = 80;
  15242. if (!endpoint || !*endpoint)
  15243. return;
  15244. #ifdef WITH_OPENSSL
  15245. if (!soap_tag_cmp(endpoint, "https:*"))
  15246. soap->port = 443;
  15247. #endif
  15248. strncpy(soap->endpoint, endpoint, sizeof(soap->endpoint));
  15249. soap->endpoint[sizeof(soap->endpoint) - 1] = '\0';
  15250. s = strchr(endpoint, ':');
  15251. if (s && s[1] == '/' && s[2] == '/')
  15252. s += 3;
  15253. else
  15254. s = endpoint;
  15255. n = strlen(s);
  15256. if (n >= sizeof(soap->host))
  15257. n = sizeof(soap->host) - 1;
  15258. #ifdef WITH_IPV6
  15259. if (s[0] == '[')
  15260. { s++;
  15261. for (i = 0; i < n; i++)
  15262. { if (s[i] == ']')
  15263. { s++;
  15264. --n;
  15265. break;
  15266. }
  15267. soap->host[i] = s[i];
  15268. }
  15269. }
  15270. else
  15271. { for (i = 0; i < n; i++)
  15272. { soap->host[i] = s[i];
  15273. if (s[i] == '/' || s[i] == ':')
  15274. break;
  15275. }
  15276. }
  15277. #else
  15278. for (i = 0; i < n; i++)
  15279. { soap->host[i] = s[i];
  15280. if (s[i] == '/' || s[i] == ':')
  15281. break;
  15282. }
  15283. #endif
  15284. soap->host[i] = '\0';
  15285. if (s[i] == ':')
  15286. { soap->port = (int)soap_strtol(s + i + 1, NULL, 10);
  15287. for (i++; i < n; i++)
  15288. if (s[i] == '/')
  15289. break;
  15290. }
  15291. if (i < n && s[i])
  15292. { strncpy(soap->path, s + i, sizeof(soap->path));
  15293. soap->path[sizeof(soap->path) - 1] = '\0';
  15294. }
  15295. }
  15296. #endif
  15297. /******************************************************************************/
  15298. #ifndef PALM_1
  15299. SOAP_FMAC1
  15300. int
  15301. SOAP_FMAC2
  15302. soap_connect(struct soap *soap, const char *endpoint, const char *action)
  15303. { return soap_connect_command(soap, SOAP_POST, endpoint, action);
  15304. }
  15305. #endif
  15306. /******************************************************************************/
  15307. #ifndef PALM_1
  15308. SOAP_FMAC1
  15309. int
  15310. SOAP_FMAC2
  15311. soap_connect_command(struct soap *soap, int http_command, const char *endpoints, const char *action)
  15312. { char *endpoint;
  15313. const char *s;
  15314. if (endpoints && (s = strchr(endpoints, ' ')))
  15315. { endpoint = (char*)SOAP_MALLOC(soap, strlen(endpoints) + 1);
  15316. for (;;)
  15317. { strncpy(endpoint, endpoints, s - endpoints);
  15318. endpoint[s - endpoints] = '\0';
  15319. if (soap_try_connect_command(soap, http_command, endpoint, action) != SOAP_TCP_ERROR)
  15320. break;
  15321. if (!*s)
  15322. break;
  15323. soap->error = SOAP_OK;
  15324. while (*s == ' ')
  15325. s++;
  15326. endpoints = s;
  15327. s = strchr(endpoints, ' ');
  15328. if (!s)
  15329. s = endpoints + strlen(endpoints);
  15330. }
  15331. SOAP_FREE(soap, endpoint);
  15332. }
  15333. else
  15334. soap_try_connect_command(soap, http_command, endpoints, action);
  15335. return soap->error;
  15336. }
  15337. #endif
  15338. /******************************************************************************/
  15339. #ifndef PALM_1
  15340. static int
  15341. soap_try_connect_command(struct soap *soap, int http_command, const char *endpoint, const char *action)
  15342. { char host[sizeof(soap->host)];
  15343. int port;
  15344. size_t count;
  15345. soap->error = SOAP_OK;
  15346. strcpy(host, soap->host); /* save previous host name: if != then reconnect */
  15347. port = soap->port; /* save previous port to compare */
  15348. soap->status = http_command;
  15349. soap_set_endpoint(soap, endpoint);
  15350. #ifndef WITH_LEANER
  15351. if (soap->fconnect)
  15352. { if ((soap->error = soap->fconnect(soap, endpoint, soap->host, soap->port)))
  15353. return soap->error;
  15354. }
  15355. else
  15356. #endif
  15357. soap->action = soap_strdup(soap, action);
  15358. if (soap->fopen && *soap->host)
  15359. { if (!soap->keep_alive || !soap_valid_socket(soap->socket) || strcmp(soap->host, host) || soap->port != port || !soap->fpoll || soap->fpoll(soap))
  15360. { soap->error = SOAP_OK;
  15361. #ifndef WITH_LEAN
  15362. if (!strncmp(endpoint, "soap.udp:", 9))
  15363. soap->omode |= SOAP_IO_UDP;
  15364. else
  15365. #endif
  15366. { soap->keep_alive = 0; /* to force close */
  15367. soap->omode &= ~SOAP_IO_UDP; /* to force close */
  15368. }
  15369. soap_closesock(soap);
  15370. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Connect/reconnect to '%s' host='%s' path='%s' port=%d\n", endpoint?endpoint:"(null)", soap->host, soap->path, soap->port));
  15371. if (!soap->keep_alive || !soap_valid_socket(soap->socket))
  15372. { soap->socket = soap->fopen(soap, endpoint, soap->host, soap->port);
  15373. if (soap->error)
  15374. return soap->error;
  15375. soap->keep_alive = ((soap->omode & SOAP_IO_KEEPALIVE) != 0);
  15376. }
  15377. }
  15378. }
  15379. #ifdef WITH_NTLM
  15380. if (soap_ntlm_handshake(soap, SOAP_GET, endpoint, soap->host, soap->port))
  15381. return soap->error;
  15382. #endif
  15383. count = soap_count_attachments(soap);
  15384. if (soap_begin_send(soap))
  15385. return soap->error;
  15386. if (http_command == SOAP_GET)
  15387. { soap->mode &= ~SOAP_IO;
  15388. soap->mode |= SOAP_IO_BUFFER;
  15389. }
  15390. #ifndef WITH_NOHTTP
  15391. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_XML) && endpoint)
  15392. { unsigned int k = soap->mode;
  15393. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  15394. if ((k & SOAP_IO) != SOAP_IO_FLUSH)
  15395. soap->mode |= SOAP_IO_BUFFER;
  15396. if ((soap->error = soap->fpost(soap, endpoint, soap->host, soap->port, soap->path, action, count)))
  15397. return soap->error;
  15398. #ifndef WITH_LEANER
  15399. if ((k & SOAP_IO) == SOAP_IO_CHUNK)
  15400. { if (soap_flush(soap))
  15401. return soap->error;
  15402. }
  15403. #endif
  15404. soap->mode = k;
  15405. }
  15406. if (http_command == SOAP_GET || http_command == SOAP_DEL)
  15407. return soap_end_send_flush(soap);
  15408. #endif
  15409. return SOAP_OK;
  15410. }
  15411. #endif
  15412. /******************************************************************************/
  15413. #ifdef WITH_NTLM
  15414. #ifndef PALM_1
  15415. static int
  15416. soap_ntlm_handshake(struct soap *soap, int command, const char *endpoint, const char *host, int port)
  15417. { /* requires libntlm from http://www.nongnu.org/libntlm/ */
  15418. const char *userid = (soap->proxy_userid ? soap->proxy_userid : soap->userid);
  15419. const char *passwd = (soap->proxy_passwd ? soap->proxy_passwd : soap->passwd);
  15420. struct SOAP_ENV__Header *oldheader;
  15421. if (soap->ntlm_challenge && userid && passwd && soap->authrealm)
  15422. { tSmbNtlmAuthRequest req;
  15423. tSmbNtlmAuthResponse res;
  15424. tSmbNtlmAuthChallenge ch;
  15425. short k = soap->keep_alive;
  15426. size_t l = soap->length;
  15427. size_t c = soap->count;
  15428. soap_mode m = soap->mode, o = soap->omode;
  15429. int s = soap->status;
  15430. char *a = soap->action;
  15431. short v = soap->version;
  15432. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM '%s'\n", soap->ntlm_challenge));
  15433. if (!*soap->ntlm_challenge)
  15434. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM S->C Type 1: received NTLM authentication challenge from server\n"));
  15435. /* S -> C 401 Unauthorized
  15436. WWW-Authenticate: NTLM
  15437. */
  15438. buildSmbNtlmAuthRequest(&req, userid, soap->authrealm);
  15439. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)&req, NULL, SmbLength(&req));
  15440. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM C->S Type 2: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  15441. /* C -> S GET ...
  15442. Authorization: NTLM TlRMTVNTUAABAAAAA7IAAAoACgApAAAACQAJACAAAABMSUdIVENJVFlVUlNBLU1JTk9S
  15443. */
  15444. soap->omode = SOAP_IO_BUFFER;
  15445. if (soap_begin_send(soap))
  15446. return soap->error;
  15447. soap->keep_alive = 1;
  15448. soap->status = command;
  15449. if (soap->fpost(soap, endpoint, host, port, soap->path, soap->action, 0)
  15450. || soap_end_send_flush(soap))
  15451. return soap->error;
  15452. soap->mode = m;
  15453. soap->keep_alive = k;
  15454. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM S->C Type 2: waiting on server NTLM response\n"));
  15455. oldheader = soap->header;
  15456. if (soap_begin_recv(soap))
  15457. if (soap->error == SOAP_EOF)
  15458. return soap->error;
  15459. soap_end_recv(soap);
  15460. soap->header = oldheader;
  15461. soap->length = l;
  15462. if (soap->status != 401 && soap->status != 407)
  15463. return soap->error = SOAP_NTLM_ERROR;
  15464. soap->error = SOAP_OK;
  15465. }
  15466. /* S -> C 401 Unauthorized
  15467. WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==
  15468. */
  15469. soap_base642s(soap, soap->ntlm_challenge, (char*)&ch, sizeof(tSmbNtlmAuthChallenge), NULL);
  15470. buildSmbNtlmAuthResponse(&ch, &res, userid, passwd);
  15471. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)&res, NULL, SmbLength(&res));
  15472. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM C->S Type 3: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  15473. /* C -> S GET ...
  15474. Authorization: NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABQAFABAAAAADAAMAFQAAAASABIAYAAAAAAAAACiAAAAAYIAAFUAUgBTAEEALQBNAEkATgBPAFIAWgBhAHAAaABvAGQATABJAEcASABUAEMASQBUAFkArYfKbe/jRoW5xDxHeoxC1gBmfWiS5+iX4OAN4xBKG/IFPwfH3agtPEia6YnhsADT
  15475. */
  15476. soap->userid = NULL;
  15477. soap->passwd = NULL;
  15478. soap->proxy_userid = NULL;
  15479. soap->proxy_passwd = NULL;
  15480. soap->keep_alive = k;
  15481. soap->length = l;
  15482. soap->count = c;
  15483. soap->mode = m;
  15484. soap->omode = o;
  15485. soap->status = s;
  15486. soap->action = a;
  15487. soap->version = v;
  15488. }
  15489. return SOAP_OK;
  15490. }
  15491. #endif
  15492. #endif
  15493. /******************************************************************************/
  15494. #ifndef WITH_LEAN
  15495. SOAP_FMAC1
  15496. char*
  15497. SOAP_FMAC2
  15498. soap_s2base64(struct soap *soap, const unsigned char *s, char *t, int n)
  15499. { register int i;
  15500. register unsigned long m;
  15501. register char *p;
  15502. if (!t)
  15503. t = (char*)soap_malloc(soap, (n + 2) / 3 * 4 + 1);
  15504. if (!t)
  15505. return NULL;
  15506. p = t;
  15507. t[0] = '\0';
  15508. if (!s)
  15509. return p;
  15510. for (; n > 2; n -= 3, s += 3)
  15511. { m = s[0];
  15512. m = (m << 8) | s[1];
  15513. m = (m << 8) | s[2];
  15514. for (i = 4; i > 0; m >>= 6)
  15515. t[--i] = soap_base64o[m & 0x3F];
  15516. t += 4;
  15517. }
  15518. t[0] = '\0';
  15519. if (n > 0) /* 0 < n <= 2 implies that t[0..4] is allocated (base64 scaling formula) */
  15520. { m = 0;
  15521. for (i = 0; i < n; i++)
  15522. m = (m << 8) | *s++;
  15523. for (; i < 3; i++)
  15524. m <<= 8;
  15525. for (i = 4; i > 0; m >>= 6)
  15526. t[--i] = soap_base64o[m & 0x3F];
  15527. for (i = 3; i > n; i--)
  15528. t[i] = '=';
  15529. t[4] = '\0';
  15530. }
  15531. return p;
  15532. }
  15533. #endif
  15534. /******************************************************************************/
  15535. #ifndef WITH_LEAN
  15536. SOAP_FMAC1
  15537. const char*
  15538. SOAP_FMAC2
  15539. soap_base642s(struct soap *soap, const char *s, char *t, size_t l, int *n)
  15540. { register size_t i, j;
  15541. register soap_wchar c;
  15542. register unsigned long m;
  15543. register const char *p;
  15544. if (!s || !*s)
  15545. { if (n)
  15546. *n = 0;
  15547. if (soap->error)
  15548. return NULL;
  15549. return SOAP_NON_NULL;
  15550. }
  15551. if (!t)
  15552. { l = (strlen(s) + 3) / 4 * 3 + 1; /* make sure enough space for \0 */
  15553. t = (char*)soap_malloc(soap, l);
  15554. }
  15555. if (!t)
  15556. return NULL;
  15557. p = t;
  15558. if (n)
  15559. *n = 0;
  15560. for (i = 0; ; i += 3, l -= 3)
  15561. { m = 0;
  15562. j = 0;
  15563. while (j < 4)
  15564. { c = *s++;
  15565. if (c == '=' || !c)
  15566. { if (l >= j - 1)
  15567. { switch (j)
  15568. { case 2:
  15569. *t++ = (char)((m >> 4) & 0xFF);
  15570. i++;
  15571. l--;
  15572. break;
  15573. case 3:
  15574. *t++ = (char)((m >> 10) & 0xFF);
  15575. *t++ = (char)((m >> 2) & 0xFF);
  15576. i += 2;
  15577. l -= 2;
  15578. }
  15579. }
  15580. if (n)
  15581. *n = (int)i;
  15582. if (l)
  15583. *t = '\0';
  15584. return p;
  15585. }
  15586. c -= '+';
  15587. if (c >= 0 && c <= 79)
  15588. { int b = soap_base64i[c];
  15589. if (b >= 64)
  15590. { soap->error = SOAP_TYPE;
  15591. return NULL;
  15592. }
  15593. m = (m << 6) + b;
  15594. j++;
  15595. }
  15596. else if (!soap_blank(c + '+'))
  15597. { soap->error = SOAP_TYPE;
  15598. return NULL;
  15599. }
  15600. }
  15601. if (l < 3)
  15602. { if (n)
  15603. *n = (int)i;
  15604. if (l)
  15605. *t = '\0';
  15606. return p;
  15607. }
  15608. *t++ = (char)((m >> 16) & 0xFF);
  15609. *t++ = (char)((m >> 8) & 0xFF);
  15610. *t++ = (char)(m & 0xFF);
  15611. }
  15612. }
  15613. #endif
  15614. /******************************************************************************/
  15615. #ifndef WITH_LEAN
  15616. SOAP_FMAC1
  15617. char*
  15618. SOAP_FMAC2
  15619. soap_s2hex(struct soap *soap, const unsigned char *s, char *t, int n)
  15620. { register char *p;
  15621. if (!t)
  15622. t = (char*)soap_malloc(soap, 2 * n + 1);
  15623. if (!t)
  15624. return NULL;
  15625. p = t;
  15626. t[0] = '\0';
  15627. if (s)
  15628. { for (; n > 0; n--)
  15629. { register int m = *s++;
  15630. *t++ = (char)((m >> 4) + (m > 159 ? 'a' - 10 : '0'));
  15631. m &= 0x0F;
  15632. *t++ = (char)(m + (m > 9 ? 'a' - 10 : '0'));
  15633. }
  15634. }
  15635. *t++ = '\0';
  15636. return p;
  15637. }
  15638. #endif
  15639. /******************************************************************************/
  15640. #ifndef WITH_LEAN
  15641. SOAP_FMAC1
  15642. const char*
  15643. SOAP_FMAC2
  15644. soap_hex2s(struct soap *soap, const char *s, char *t, size_t l, int *n)
  15645. { register const char *p;
  15646. if (!s || !*s)
  15647. { if (n)
  15648. *n = 0;
  15649. if (soap->error)
  15650. return NULL;
  15651. return SOAP_NON_NULL;
  15652. }
  15653. if (!t)
  15654. { l = strlen(s) / 2 + 1; /* make sure enough space for \0 */
  15655. t = (char*)soap_malloc(soap, l);
  15656. }
  15657. if (!t)
  15658. return NULL;
  15659. p = t;
  15660. while (l)
  15661. { register int d1, d2;
  15662. d1 = *s++;
  15663. if (!d1)
  15664. break;
  15665. d2 = *s++;
  15666. if (!d2)
  15667. break;
  15668. *t++ = (char)(((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0'));
  15669. l--;
  15670. }
  15671. if (n)
  15672. *n = (int)(t - p);
  15673. if (l)
  15674. *t = '\0';
  15675. return p;
  15676. }
  15677. #endif
  15678. /******************************************************************************/
  15679. #ifndef WITH_NOHTTP
  15680. #ifndef PALM_1
  15681. SOAP_FMAC1
  15682. int
  15683. SOAP_FMAC2
  15684. soap_puthttphdr(struct soap *soap, int status, size_t count)
  15685. { if (soap->status != SOAP_GET && soap->status != SOAP_DEL && soap->status != SOAP_CONNECT)
  15686. { register const char *s = "text/xml; charset=utf-8";
  15687. register int err = SOAP_OK;
  15688. #ifndef WITH_LEANER
  15689. register const char *r = NULL;
  15690. #endif
  15691. if ((status == SOAP_FILE || soap->status == SOAP_PUT || soap->status == SOAP_POST_FILE) && soap->http_content && !strchr(s, 10) && !strchr(s, 13))
  15692. s = soap->http_content;
  15693. else if (status == SOAP_HTML)
  15694. s = "text/html; charset=utf-8";
  15695. else if (count || ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK))
  15696. { if (soap->version == 2)
  15697. s = "application/soap+xml; charset=utf-8";
  15698. }
  15699. #ifndef WITH_LEANER
  15700. if (soap->mode & (SOAP_ENC_DIME | SOAP_ENC_MTOM))
  15701. { if (soap->mode & SOAP_ENC_MTOM)
  15702. { if (soap->version == 2)
  15703. r = "application/soap+xml";
  15704. else
  15705. r = "text/xml";
  15706. s = "application/xop+xml";
  15707. }
  15708. else
  15709. s = "application/dime";
  15710. }
  15711. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary && strlen(soap->mime.boundary) + strlen(soap->mime.start ? soap->mime.start : SOAP_STR_EOS) < sizeof(soap->tmpbuf) - 80)
  15712. { register const char *t;
  15713. #ifdef HAVE_SNPRINTF
  15714. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "multipart/related; charset=utf-8; boundary=\"%s\"; type=\"", soap->mime.boundary);
  15715. #else
  15716. sprintf(soap->tmpbuf, "multipart/related; charset=utf-8; boundary=\"%s\"; type=\"", soap->mime.boundary);
  15717. #endif
  15718. t = strchr(s, ';');
  15719. if (t)
  15720. strncat(soap->tmpbuf, s, t - s);
  15721. else
  15722. strcat(soap->tmpbuf, s);
  15723. if (soap->mime.start && strlen(soap->tmpbuf) + strlen(soap->mime.start) + 11 < sizeof(soap->tmpbuf))
  15724. { strcat(soap->tmpbuf, "\"; start=\"");
  15725. strcat(soap->tmpbuf, soap->mime.start);
  15726. }
  15727. strcat(soap->tmpbuf, "\"");
  15728. if (r && strlen(soap->tmpbuf) + strlen(r) + 15 < sizeof(soap->tmpbuf))
  15729. { strcat(soap->tmpbuf, "; start-info=\"");
  15730. strcat(soap->tmpbuf, r);
  15731. strcat(soap->tmpbuf, "\"");
  15732. }
  15733. }
  15734. else
  15735. strncpy(soap->tmpbuf, s, sizeof(soap->tmpbuf));
  15736. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0';
  15737. s = soap->tmpbuf;
  15738. if (status == SOAP_OK && soap->version == 2 && soap->action && strlen(soap->action) + strlen(s) < sizeof(soap->tmpbuf) - 80)
  15739. {
  15740. #ifdef HAVE_SNPRINTF
  15741. size_t l = strlen(s);
  15742. soap_snprintf(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, "; action=\"%s\"", soap->action);
  15743. #else
  15744. sprintf(soap->tmpbuf + strlen(s), "; action=\"%s\"", soap->action);
  15745. #endif
  15746. }
  15747. #endif
  15748. if ((err = soap->fposthdr(soap, "Content-Type", s)))
  15749. return err;
  15750. #ifdef WITH_ZLIB
  15751. if ((soap->omode & SOAP_ENC_ZLIB))
  15752. {
  15753. #ifdef WITH_GZIP
  15754. err = soap->fposthdr(soap, "Content-Encoding", soap->zlib_out == SOAP_ZLIB_DEFLATE ? "deflate" : "gzip");
  15755. #else
  15756. err = soap->fposthdr(soap, "Content-Encoding", "deflate");
  15757. #endif
  15758. if (err)
  15759. return err;
  15760. }
  15761. #endif
  15762. #ifndef WITH_LEANER
  15763. if ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK)
  15764. err = soap->fposthdr(soap, "Transfer-Encoding", "chunked");
  15765. else
  15766. #endif
  15767. if (s)
  15768. {
  15769. #ifdef HAVE_SNPRINTF
  15770. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%lu", (unsigned long)count);
  15771. #else
  15772. sprintf(soap->tmpbuf, "%lu", (unsigned long)count);
  15773. #endif
  15774. err = soap->fposthdr(soap, "Content-Length", soap->tmpbuf);
  15775. }
  15776. if (err)
  15777. return err;
  15778. }
  15779. return soap->fposthdr(soap, "Connection", soap->keep_alive ? "keep-alive" : "close");
  15780. }
  15781. #endif
  15782. #endif
  15783. /******************************************************************************/
  15784. #ifndef WITH_LEAN
  15785. static const char*
  15786. soap_set_validation_fault(struct soap *soap, const char *s, const char *t)
  15787. { if (!t)
  15788. t = SOAP_STR_EOS;
  15789. if (*soap->tag)
  15790. {
  15791. #ifdef HAVE_SNPRINTF
  15792. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Validation constraint violation: %s%s in element '%s'", s, t ? t : SOAP_STR_EOS, soap->tag);
  15793. #else
  15794. if (strlen(soap->tag) + strlen(t) < sizeof(soap->msgbuf) - 100)
  15795. sprintf(soap->msgbuf, "Validation constraint violation: %s%s in element '%s'", s, t, soap->tag);
  15796. else
  15797. sprintf(soap->msgbuf, "Validation constraint violation: %s", s);
  15798. #endif
  15799. }
  15800. else
  15801. {
  15802. #ifdef HAVE_SNPRINTF
  15803. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Validation constraint violation: %s%s", s, t ? t : SOAP_STR_EOS);
  15804. #else
  15805. if (strlen(soap->tag) + strlen(t) < sizeof(soap->msgbuf) - 100)
  15806. sprintf(soap->msgbuf, "Validation constraint violation: %s%s", s, t);
  15807. else
  15808. sprintf(soap->msgbuf, "Validation constraint violation: %s", s);
  15809. #endif
  15810. }
  15811. return soap->msgbuf;
  15812. }
  15813. #endif
  15814. /******************************************************************************/
  15815. #ifndef PALM_1
  15816. SOAP_FMAC1
  15817. void
  15818. SOAP_FMAC2
  15819. soap_set_fault(struct soap *soap)
  15820. { const char **c = soap_faultcode(soap);
  15821. const char **s = soap_faultstring(soap);
  15822. if (soap->fseterror)
  15823. soap->fseterror(soap, c, s);
  15824. if (!*c)
  15825. { if (soap->version == 2)
  15826. *c = "SOAP-ENV:Sender";
  15827. else
  15828. *c = "SOAP-ENV:Client";
  15829. }
  15830. if (*s)
  15831. return;
  15832. switch (soap->error)
  15833. {
  15834. #ifndef WITH_LEAN
  15835. case SOAP_CLI_FAULT:
  15836. *s = "Client fault";
  15837. break;
  15838. case SOAP_SVR_FAULT:
  15839. *s = "Server fault";
  15840. break;
  15841. case SOAP_TAG_MISMATCH:
  15842. *s = soap_set_validation_fault(soap, "tag name or namespace mismatch", NULL);
  15843. break;
  15844. case SOAP_TYPE:
  15845. *s = soap_set_validation_fault(soap, "data type mismatch ", soap->type);
  15846. break;
  15847. case SOAP_SYNTAX_ERROR:
  15848. *s = "Well-formedness violation";
  15849. break;
  15850. case SOAP_NO_TAG:
  15851. *s = "No tag: no XML root element or missing SOAP message body element";
  15852. break;
  15853. case SOAP_MUSTUNDERSTAND:
  15854. *c = "SOAP-ENV:MustUnderstand";
  15855. #ifdef HAVE_SNPRINTF
  15856. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "The data in element '%s' must be understood but cannot be handled", soap->tag);
  15857. #else
  15858. strncpy(soap->msgbuf, soap->tag, sizeof(soap->msgbuf));
  15859. soap->msgbuf[sizeof(soap->msgbuf) - 1] = '\0';
  15860. #endif
  15861. *s = soap->msgbuf;
  15862. break;
  15863. case SOAP_VERSIONMISMATCH:
  15864. *c = "SOAP-ENV:VersionMismatch";
  15865. *s = "Invalid SOAP message or SOAP version mismatch";
  15866. break;
  15867. case SOAP_DATAENCODINGUNKNOWN:
  15868. *c = "SOAP-ENV:DataEncodingUnknown";
  15869. *s = "Unsupported SOAP data encoding";
  15870. break;
  15871. case SOAP_NAMESPACE:
  15872. *s = soap_set_validation_fault(soap, "namespace error", NULL);
  15873. break;
  15874. case SOAP_USER_ERROR:
  15875. *s = "User data error";
  15876. break;
  15877. case SOAP_FATAL_ERROR:
  15878. *s = "Fatal error";
  15879. break;
  15880. case SOAP_NO_METHOD:
  15881. #ifdef HAVE_SNPRINTF
  15882. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Method '%s' not implemented: method name or namespace not recognized", soap->tag);
  15883. #else
  15884. sprintf(soap->msgbuf, "Method '%s' not implemented: method name or namespace not recognized", soap->tag);
  15885. #endif
  15886. *s = soap->msgbuf;
  15887. break;
  15888. case SOAP_NO_DATA:
  15889. *s = "Data required for operation";
  15890. break;
  15891. case SOAP_GET_METHOD:
  15892. *s = "HTTP GET method not implemented";
  15893. break;
  15894. case SOAP_PUT_METHOD:
  15895. *s = "HTTP PUT method not implemented";
  15896. break;
  15897. case SOAP_HTTP_METHOD:
  15898. *s = "HTTP method not implemented";
  15899. break;
  15900. case SOAP_EOM:
  15901. *s = "Out of memory";
  15902. break;
  15903. case SOAP_MOE:
  15904. *s = "Memory overflow or memory corruption error";
  15905. break;
  15906. case SOAP_HDR:
  15907. *s = "Header line too long";
  15908. break;
  15909. case SOAP_IOB:
  15910. *s = "Array index out of bounds";
  15911. break;
  15912. case SOAP_NULL:
  15913. *s = soap_set_validation_fault(soap, "nil not allowed", NULL);
  15914. break;
  15915. case SOAP_DUPLICATE_ID:
  15916. *s = soap_set_validation_fault(soap, "multiple elements (use the SOAP_XML_TREE flag) with duplicate id ", soap->id);
  15917. if (soap->version == 2)
  15918. *soap_faultsubcode(soap) = "SOAP-ENC:DuplicateID";
  15919. break;
  15920. case SOAP_MISSING_ID:
  15921. *s = soap_set_validation_fault(soap, "missing id for ref ", soap->id);
  15922. if (soap->version == 2)
  15923. *soap_faultsubcode(soap) = "SOAP-ENC:MissingID";
  15924. break;
  15925. case SOAP_HREF:
  15926. *s = soap_set_validation_fault(soap, "incompatible object type id-ref ", soap->id);
  15927. break;
  15928. case SOAP_FAULT:
  15929. break;
  15930. #ifndef WITH_NOIO
  15931. case SOAP_UDP_ERROR:
  15932. *s = "Message too large for UDP packet";
  15933. break;
  15934. case SOAP_TCP_ERROR:
  15935. *s = tcp_error(soap);
  15936. break;
  15937. #endif
  15938. case SOAP_HTTP_ERROR:
  15939. *s = "An HTTP processing error occurred";
  15940. break;
  15941. case SOAP_NTLM_ERROR:
  15942. *s = "An HTTP NTLM authentication error occurred";
  15943. break;
  15944. case SOAP_SSL_ERROR:
  15945. #ifdef WITH_OPENSSL
  15946. *s = "SSL/TLS error";
  15947. #else
  15948. *s = "OpenSSL not installed: recompile with -DWITH_OPENSSL";
  15949. #endif
  15950. break;
  15951. case SOAP_PLUGIN_ERROR:
  15952. *s = "Plugin registry error";
  15953. break;
  15954. case SOAP_DIME_ERROR:
  15955. *s = "DIME format error or max DIME size exceeds SOAP_MAXDIMESIZE";
  15956. break;
  15957. case SOAP_DIME_HREF:
  15958. *s = "DIME href to missing attachment";
  15959. break;
  15960. case SOAP_DIME_MISMATCH:
  15961. *s = "DIME version/transmission error";
  15962. break;
  15963. case SOAP_DIME_END:
  15964. *s = "End of DIME error";
  15965. break;
  15966. case SOAP_MIME_ERROR:
  15967. *s = "MIME format error";
  15968. break;
  15969. case SOAP_MIME_HREF:
  15970. *s = "MIME href to missing attachment";
  15971. break;
  15972. case SOAP_MIME_END:
  15973. *s = "End of MIME error";
  15974. break;
  15975. case SOAP_ZLIB_ERROR:
  15976. #ifdef WITH_ZLIB
  15977. #ifdef HAVE_SNPRINTF
  15978. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Zlib/gzip error: '%s'", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS);
  15979. #else
  15980. sprintf(soap->msgbuf, "Zlib/gzip error: '%s'", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS);
  15981. #endif
  15982. *s = soap->msgbuf;
  15983. #else
  15984. *s = "Zlib/gzip not installed for (de)compression: recompile with -DWITH_GZIP";
  15985. #endif
  15986. break;
  15987. case SOAP_REQUIRED:
  15988. *s = soap_set_validation_fault(soap, "missing required attribute", NULL);
  15989. break;
  15990. case SOAP_PROHIBITED:
  15991. *s = soap_set_validation_fault(soap, "prohibited attribute present", NULL);
  15992. break;
  15993. case SOAP_OCCURS:
  15994. *s = soap_set_validation_fault(soap, "occurrence violation", NULL);
  15995. break;
  15996. case SOAP_LENGTH:
  15997. *s = soap_set_validation_fault(soap, "content range or length violation", NULL);
  15998. break;
  15999. case SOAP_FD_EXCEEDED:
  16000. *s = "Maximum number of open connections was reached (no define HAVE_POLL): increase FD_SETSIZE";
  16001. break;
  16002. case SOAP_UTF_ERROR:
  16003. *s = "UTF content encoding error";
  16004. break;
  16005. case SOAP_STOP:
  16006. *s = "Stopped: no response sent or received (informative)";
  16007. break;
  16008. #endif
  16009. case SOAP_EOF:
  16010. #ifndef WITH_NOIO
  16011. *s = soap_strerror(soap); /* *s = soap->msgbuf */
  16012. #ifndef WITH_LEAN
  16013. if (strlen(soap->msgbuf) + 25 < sizeof(soap->msgbuf))
  16014. { memmove(soap->msgbuf + 25, soap->msgbuf, strlen(soap->msgbuf) + 1);
  16015. memcpy(soap->msgbuf, "End of file or no input: ", 25);
  16016. }
  16017. #endif
  16018. break;
  16019. #else
  16020. *s = "End of file or no input";
  16021. break;
  16022. #endif
  16023. default:
  16024. #ifndef WITH_NOHTTP
  16025. #ifndef WITH_LEAN
  16026. if (soap->error > 200 && soap->error < 600)
  16027. {
  16028. #ifdef HAVE_SNPRINTF
  16029. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "HTTP Error: %d %s", soap->error, http_error(soap, soap->error));
  16030. #else
  16031. sprintf(soap->msgbuf, "HTTP Error: %d %s", soap->error, http_error(soap, soap->error));
  16032. #endif
  16033. *s = soap->msgbuf;
  16034. }
  16035. else
  16036. #endif
  16037. #endif
  16038. {
  16039. #ifdef HAVE_SNPRINTF
  16040. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Error %d", soap->error);
  16041. #else
  16042. sprintf(soap->msgbuf, "Error %d", soap->error);
  16043. #endif
  16044. *s = soap->msgbuf;
  16045. }
  16046. }
  16047. }
  16048. #endif
  16049. /******************************************************************************/
  16050. #ifndef PALM_1
  16051. SOAP_FMAC1
  16052. int
  16053. SOAP_FMAC2
  16054. soap_send_fault(struct soap *soap)
  16055. { register int status = soap->error;
  16056. if (status == SOAP_OK || status == SOAP_STOP)
  16057. return soap_closesock(soap);
  16058. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Sending back fault struct for error code %d\n", soap->error));
  16059. soap->keep_alive = 0; /* to terminate connection */
  16060. soap_set_fault(soap);
  16061. if (soap->error < 200 && soap->error != SOAP_FAULT)
  16062. soap->header = NULL;
  16063. if (status != SOAP_EOF || (!soap->recv_timeout && !soap->send_timeout))
  16064. { register int r = 1;
  16065. #ifndef WITH_NOIO
  16066. if (soap->fpoll && soap->fpoll(soap))
  16067. r = 0;
  16068. #ifndef WITH_LEAN
  16069. else if (soap_valid_socket(soap->socket))
  16070. { r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_SND, 0);
  16071. if (r > 0)
  16072. { int t;
  16073. if (!(r & SOAP_TCP_SELECT_SND)
  16074. || ((r & SOAP_TCP_SELECT_RCV)
  16075. && recv(soap->socket, (char*)&t, 1, MSG_PEEK) < 0))
  16076. r = 0;
  16077. }
  16078. }
  16079. #endif
  16080. #endif
  16081. if (r > 0)
  16082. { soap->error = SOAP_OK;
  16083. soap->encodingStyle = NULL; /* no encodingStyle in Faults */
  16084. soap_serializeheader(soap);
  16085. soap_serializefault(soap);
  16086. soap_begin_count(soap);
  16087. if (soap->mode & SOAP_IO_LENGTH)
  16088. { soap_envelope_begin_out(soap);
  16089. soap_putheader(soap);
  16090. soap_body_begin_out(soap);
  16091. soap_putfault(soap);
  16092. soap_body_end_out(soap);
  16093. soap_envelope_end_out(soap);
  16094. }
  16095. soap_end_count(soap);
  16096. if (soap_response(soap, status)
  16097. || soap_envelope_begin_out(soap)
  16098. || soap_putheader(soap)
  16099. || soap_body_begin_out(soap)
  16100. || soap_putfault(soap)
  16101. || soap_body_end_out(soap)
  16102. || soap_envelope_end_out(soap))
  16103. return soap_closesock(soap);
  16104. soap_end_send(soap);
  16105. }
  16106. }
  16107. soap->error = status;
  16108. return soap_closesock(soap);
  16109. }
  16110. #endif
  16111. /******************************************************************************/
  16112. #ifndef PALM_1
  16113. SOAP_FMAC1
  16114. int
  16115. SOAP_FMAC2
  16116. soap_recv_fault(struct soap *soap, int check)
  16117. { register int status = soap->error;
  16118. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Check if receiving SOAP Fault\n"));
  16119. if (!check)
  16120. { /* try getfault when no tag or tag mismatched at level 2, otherwise ret */
  16121. if (soap->error != SOAP_NO_TAG
  16122. && (soap->error != SOAP_TAG_MISMATCH || soap->level != 2))
  16123. return soap->error;
  16124. }
  16125. else if (soap->version == 0) /* check == 1 but no SOAP: do not parse SOAP Fault */
  16126. return SOAP_OK;
  16127. soap->error = SOAP_OK;
  16128. if (soap_getfault(soap))
  16129. { /* check flag set: check if SOAP Fault is present, if not just return */
  16130. if (check && soap->error == SOAP_TAG_MISMATCH && soap->level == 2)
  16131. return soap->error = SOAP_OK;
  16132. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Error: soap_get_soapfault() failed at level %u tag '%s'\n", soap->level, soap->tag));
  16133. *soap_faultcode(soap) = (soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client");
  16134. soap->error = status;
  16135. soap_set_fault(soap);
  16136. }
  16137. else
  16138. { register const char *s = *soap_faultcode(soap);
  16139. if (!soap_match_tag(soap, s, "SOAP-ENV:Server") || !soap_match_tag(soap, s, "SOAP-ENV:Receiver"))
  16140. status = SOAP_SVR_FAULT;
  16141. else if (!soap_match_tag(soap, s, "SOAP-ENV:Client") || !soap_match_tag(soap, s, "SOAP-ENV:Sender"))
  16142. status = SOAP_CLI_FAULT;
  16143. else if (!soap_match_tag(soap, s, "SOAP-ENV:MustUnderstand"))
  16144. status = SOAP_MUSTUNDERSTAND;
  16145. else if (!soap_match_tag(soap, s, "SOAP-ENV:VersionMismatch"))
  16146. status = SOAP_VERSIONMISMATCH;
  16147. else
  16148. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Received SOAP Fault code %s\n", s));
  16149. status = SOAP_FAULT;
  16150. }
  16151. if (!soap_body_end_in(soap))
  16152. soap_envelope_end_in(soap);
  16153. }
  16154. soap_end_recv(soap);
  16155. soap->error = status;
  16156. return soap_closesock(soap);
  16157. }
  16158. #endif
  16159. /******************************************************************************/
  16160. #ifndef WITH_NOHTTP
  16161. #ifndef PALM_1
  16162. SOAP_FMAC1
  16163. int
  16164. SOAP_FMAC2
  16165. soap_send_empty_response(struct soap *soap, int httpstatuscode)
  16166. { register soap_mode m = soap->omode;
  16167. if (!(m & SOAP_IO_UDP))
  16168. { soap->count = 0;
  16169. if ((m & SOAP_IO) == SOAP_IO_CHUNK)
  16170. soap->omode = (m & ~SOAP_IO) | SOAP_IO_BUFFER;
  16171. soap_response(soap, httpstatuscode);
  16172. soap_end_send(soap); /* force end of sends */
  16173. soap->error = SOAP_STOP; /* stops the server (from returning a response) */
  16174. soap->omode = m;
  16175. }
  16176. return soap_closesock(soap);
  16177. }
  16178. #endif
  16179. #endif
  16180. /******************************************************************************/
  16181. #ifndef WITH_NOHTTP
  16182. #ifndef PALM_1
  16183. SOAP_FMAC1
  16184. int
  16185. SOAP_FMAC2
  16186. soap_recv_empty_response(struct soap *soap)
  16187. { if (!(soap->omode & SOAP_IO_UDP))
  16188. { if (!soap_begin_recv(soap))
  16189. {
  16190. #ifndef WITH_LEAN
  16191. if (soap->body)
  16192. soap_get_http_body(soap, NULL); /* read (empty?) HTTP body and discard */
  16193. #endif
  16194. soap_end_recv(soap);
  16195. }
  16196. else if (soap->error == SOAP_NO_DATA || soap->error == 202)
  16197. soap->error = SOAP_OK;
  16198. }
  16199. return soap_closesock(soap);
  16200. }
  16201. #endif
  16202. #endif
  16203. /******************************************************************************/
  16204. #ifndef WITH_NOIO
  16205. #ifndef PALM_1
  16206. static const char*
  16207. soap_strerror(struct soap *soap)
  16208. { register int err = soap->errnum;
  16209. *soap->msgbuf = '\0';
  16210. if (err)
  16211. {
  16212. #ifndef WIN32
  16213. # ifdef HAVE_STRERROR_R
  16214. # ifdef _GNU_SOURCE
  16215. return strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* GNU-specific */
  16216. # else
  16217. strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* XSI-compliant */
  16218. # endif
  16219. # else
  16220. return strerror(err);
  16221. # endif
  16222. #else
  16223. #ifndef UNDER_CE
  16224. DWORD len;
  16225. *soap->msgbuf = '\0';
  16226. len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)soap->msgbuf, (DWORD)sizeof(soap->msgbuf), NULL);
  16227. #else
  16228. DWORD i, len;
  16229. *soap->msgbuf = '\0';
  16230. len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, (LPTSTR)soap->msgbuf, (DWORD)(sizeof(soap->msgbuf)/sizeof(TCHAR)), NULL);
  16231. for (i = 0; i <= len; i++)
  16232. { if (((TCHAR*)soap->msgbuf)[i] < 0x80)
  16233. soap->msgbuf[i] = (char)((TCHAR*)soap->msgbuf)[i];
  16234. else
  16235. soap->msgbuf[i] = '?';
  16236. }
  16237. #endif
  16238. #endif
  16239. }
  16240. else
  16241. { char *s = soap->msgbuf;
  16242. #ifndef WITH_LEAN
  16243. int rt = soap->recv_timeout, st = soap->send_timeout;
  16244. int ru = ' ', su = ' ';
  16245. #endif
  16246. strcpy(s, "Operation interrupted or timed out");
  16247. #ifndef WITH_LEAN
  16248. if (rt < 0)
  16249. { rt = -rt;
  16250. ru = 'u';
  16251. }
  16252. if (st < 0)
  16253. { st = -st;
  16254. su = 'u';
  16255. }
  16256. if (rt)
  16257. {
  16258. #ifdef HAVE_SNPRINTF
  16259. size_t l = strlen(s);
  16260. soap_snprintf(s + l, sizeof(soap->msgbuf) - l, " (%d%cs recv delay)", rt, ru);
  16261. #else
  16262. sprintf(s + strlen(s), " (%d%cs recv delay)", rt, ru);
  16263. #endif
  16264. }
  16265. if (st)
  16266. {
  16267. #ifdef HAVE_SNPRINTF
  16268. size_t l = strlen(s);
  16269. soap_snprintf(s + l, sizeof(soap->msgbuf) - l, " (%d%cs send delay)", st, su);
  16270. #else
  16271. sprintf(s + strlen(s), " (%d%cs send delay)", st, su);
  16272. #endif
  16273. }
  16274. #endif
  16275. }
  16276. return soap->msgbuf;
  16277. }
  16278. #endif
  16279. #endif
  16280. /******************************************************************************/
  16281. #ifndef PALM_2
  16282. static int
  16283. soap_set_error(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML, int soaperror)
  16284. { *soap_faultcode(soap) = faultcode;
  16285. if (faultsubcodeQName)
  16286. *soap_faultsubcode(soap) = faultsubcodeQName;
  16287. *soap_faultstring(soap) = faultstring;
  16288. if (faultdetailXML && *faultdetailXML)
  16289. { register const char **s = soap_faultdetail(soap);
  16290. if (s)
  16291. *s = faultdetailXML;
  16292. }
  16293. return soap->error = soaperror;
  16294. }
  16295. #endif
  16296. /******************************************************************************/
  16297. #ifndef PALM_2
  16298. SOAP_FMAC1
  16299. int
  16300. SOAP_FMAC2
  16301. soap_set_sender_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror)
  16302. { return soap_set_error(soap, soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client", NULL, faultstring, faultdetailXML, soaperror);
  16303. }
  16304. #endif
  16305. /******************************************************************************/
  16306. #ifndef PALM_2
  16307. SOAP_FMAC1
  16308. int
  16309. SOAP_FMAC2
  16310. soap_set_receiver_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror)
  16311. { return soap_set_error(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : "SOAP-ENV:Server", NULL, faultstring, faultdetailXML, soaperror);
  16312. }
  16313. #endif
  16314. /******************************************************************************/
  16315. #ifndef PALM_2
  16316. static int
  16317. soap_copy_fault(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML)
  16318. { char *r = NULL, *s = NULL, *t = NULL;
  16319. if (faultsubcodeQName)
  16320. r = soap_strdup(soap, faultsubcodeQName);
  16321. if (faultstring)
  16322. s = soap_strdup(soap, faultstring);
  16323. if (faultdetailXML)
  16324. t = soap_strdup(soap, faultdetailXML);
  16325. return soap_set_error(soap, faultcode, r, s, t, SOAP_FAULT);
  16326. }
  16327. #endif
  16328. /******************************************************************************/
  16329. #ifndef PALM_2
  16330. SOAP_FMAC1
  16331. int
  16332. SOAP_FMAC2
  16333. soap_sender_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML)
  16334. { return soap_sender_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  16335. }
  16336. #endif
  16337. /******************************************************************************/
  16338. #ifndef PALM_2
  16339. SOAP_FMAC1
  16340. int
  16341. SOAP_FMAC2
  16342. soap_sender_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML)
  16343. { return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client", faultsubcodeQName, faultstring, faultdetailXML);
  16344. }
  16345. #endif
  16346. /******************************************************************************/
  16347. #ifndef PALM_2
  16348. SOAP_FMAC1
  16349. int
  16350. SOAP_FMAC2
  16351. soap_receiver_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML)
  16352. { return soap_receiver_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  16353. }
  16354. #endif
  16355. /******************************************************************************/
  16356. #ifndef PALM_2
  16357. SOAP_FMAC1
  16358. int
  16359. SOAP_FMAC2
  16360. soap_receiver_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML)
  16361. { return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : "SOAP-ENV:Server", faultsubcodeQName, faultstring, faultdetailXML);
  16362. }
  16363. #endif
  16364. /******************************************************************************/
  16365. #ifndef PALM_2
  16366. #ifndef WITH_NOSTDLIB
  16367. SOAP_FMAC1
  16368. void
  16369. SOAP_FMAC2
  16370. soap_print_fault(struct soap *soap, FILE *fd)
  16371. { if (soap_check_state(soap))
  16372. fprintf(fd, "Error: soap struct state not initialized\n");
  16373. else if (soap->error)
  16374. { const char **c, *v = NULL, *s, *d;
  16375. c = soap_faultcode(soap);
  16376. if (!*c)
  16377. soap_set_fault(soap);
  16378. if (soap->version == 2)
  16379. v = soap_check_faultsubcode(soap);
  16380. s = *soap_faultstring(soap);
  16381. d = soap_check_faultdetail(soap);
  16382. fprintf(fd, "%s%d fault: %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ", soap->version ? (int)soap->version : soap->error, *c, v ? v : "no subcode", s ? s : "[no reason]", d ? d : "[no detail]");
  16383. }
  16384. }
  16385. #endif
  16386. #endif
  16387. /******************************************************************************/
  16388. #ifdef __cplusplus
  16389. #ifndef WITH_LEAN
  16390. #ifndef WITH_NOSTDLIB
  16391. #ifndef WITH_COMPAT
  16392. SOAP_FMAC1
  16393. void
  16394. SOAP_FMAC2
  16395. soap_stream_fault(struct soap *soap, std::ostream& os)
  16396. { if (soap_check_state(soap))
  16397. os << "Error: soap struct state not initialized\n";
  16398. else if (soap->error)
  16399. { const char **c, *v = NULL, *s, *d;
  16400. c = soap_faultcode(soap);
  16401. if (!*c)
  16402. soap_set_fault(soap);
  16403. if (soap->version == 2)
  16404. v = soap_check_faultsubcode(soap);
  16405. s = *soap_faultstring(soap);
  16406. d = soap_check_faultdetail(soap);
  16407. os << (soap->version ? "SOAP 1." : "Error ")
  16408. << (soap->version ? (int)soap->version : soap->error)
  16409. << " fault: " << *c
  16410. << "[" << (v ? v : "no subcode") << "]"
  16411. << std::endl
  16412. << "\"" << (s ? s : "[no reason]") << "\""
  16413. << std::endl
  16414. << "Detail: " << (d ? d : "[no detail]")
  16415. << std::endl;
  16416. }
  16417. }
  16418. #endif
  16419. #endif
  16420. #endif
  16421. #endif
  16422. /******************************************************************************/
  16423. #ifndef WITH_LEAN
  16424. #ifndef WITH_NOSTDLIB
  16425. SOAP_FMAC1
  16426. char*
  16427. SOAP_FMAC2
  16428. soap_sprint_fault(struct soap *soap, char *buf, size_t len)
  16429. { if (soap_check_state(soap))
  16430. { strncpy(buf, "Error: soap struct not initialized", len);
  16431. buf[len - 1] = '\0';
  16432. }
  16433. else if (soap->error)
  16434. { const char **c, *v = NULL, *s, *d;
  16435. c = soap_faultcode(soap);
  16436. if (!*c)
  16437. soap_set_fault(soap);
  16438. if (soap->version == 2)
  16439. v = *soap_faultsubcode(soap);
  16440. s = *soap_faultstring(soap);
  16441. d = soap_check_faultdetail(soap);
  16442. #ifdef HAVE_SNPRINTF
  16443. soap_snprintf(buf, len, "%s%d fault: %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ", soap->version ? (int)soap->version : soap->error, *c, v ? v : "no subcode", s ? s : "[no reason]", d ? d : "[no detail]");
  16444. #else
  16445. if (len > 40 + (v ? strlen(v) : 0) + (s ? strlen(s) : 0) + (d ? strlen(d) : 0))
  16446. sprintf(buf, "%s%d fault: %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ", soap->version ? (int)soap->version : soap->error, *c, v ? v : "no subcode", s ? s : "[no reason]", d ? d : "[no detail]");
  16447. else if (len > 40)
  16448. sprintf(buf, "%s%d fault: %s\n", soap->version ? "SOAP 1." : "Error ", soap->version ? (int)soap->version : soap->error, *c);
  16449. else
  16450. buf[0] = '\0';
  16451. #endif
  16452. }
  16453. return buf;
  16454. }
  16455. #endif
  16456. #endif
  16457. /******************************************************************************/
  16458. #ifndef PALM_1
  16459. #ifndef WITH_NOSTDLIB
  16460. SOAP_FMAC1
  16461. void
  16462. SOAP_FMAC2
  16463. soap_print_fault_location(struct soap *soap, FILE *fd)
  16464. {
  16465. #ifndef WITH_LEAN
  16466. int i, j, c1, c2;
  16467. if (soap->error && soap->error != SOAP_STOP && soap->bufidx <= soap->buflen && soap->buflen > 0 && soap->buflen <= SOAP_BUFLEN)
  16468. { i = (int)soap->bufidx - 1;
  16469. if (i <= 0)
  16470. i = 0;
  16471. c1 = soap->buf[i];
  16472. soap->buf[i] = '\0';
  16473. if ((int)soap->buflen >= i + 1024)
  16474. j = i + 1023;
  16475. else
  16476. j = (int)soap->buflen - 1;
  16477. c2 = soap->buf[j];
  16478. soap->buf[j] = '\0';
  16479. fprintf(fd, "%s%c\n<!-- ** HERE ** -->\n", soap->buf, c1);
  16480. if (soap->bufidx < soap->buflen)
  16481. fprintf(fd, "%s\n", soap->buf + soap->bufidx);
  16482. soap->buf[i] = (char)c1;
  16483. soap->buf[j] = (char)c2;
  16484. }
  16485. #endif
  16486. }
  16487. #endif
  16488. #endif
  16489. /******************************************************************************/
  16490. #ifndef PALM_1
  16491. SOAP_FMAC1
  16492. int
  16493. SOAP_FMAC2
  16494. soap_register_plugin_arg(struct soap *soap, int (*fcreate)(struct soap*, struct soap_plugin*, void*), void *arg)
  16495. { register struct soap_plugin *p;
  16496. register int r;
  16497. if (!(p = (struct soap_plugin*)SOAP_MALLOC(soap, sizeof(struct soap_plugin))))
  16498. return soap->error = SOAP_EOM;
  16499. p->id = NULL;
  16500. p->data = NULL;
  16501. p->fcopy = NULL;
  16502. p->fdelete = NULL;
  16503. r = fcreate(soap, p, arg);
  16504. if (!r && p->fdelete)
  16505. { p->next = soap->plugins;
  16506. soap->plugins = p;
  16507. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Registered '%s' plugin\n", p->id));
  16508. return SOAP_OK;
  16509. }
  16510. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not register plugin '%s': plugin returned error %d (or fdelete callback not set)\n", p->id ? p->id : "?", r));
  16511. SOAP_FREE(soap, p);
  16512. return r;
  16513. }
  16514. #endif
  16515. /******************************************************************************/
  16516. #ifndef PALM_1
  16517. static void *
  16518. fplugin(struct soap *soap, const char *id)
  16519. { register struct soap_plugin *p;
  16520. for (p = soap->plugins; p; p = p->next)
  16521. if (p->id == id || !strcmp(p->id, id))
  16522. return p->data;
  16523. return NULL;
  16524. }
  16525. #endif
  16526. /******************************************************************************/
  16527. #ifndef PALM_2
  16528. SOAP_FMAC1
  16529. void *
  16530. SOAP_FMAC2
  16531. soap_lookup_plugin(struct soap *soap, const char *id)
  16532. { return soap->fplugin(soap, id);
  16533. }
  16534. #endif
  16535. /******************************************************************************/
  16536. #ifdef __cplusplus
  16537. }
  16538. #endif
  16539. /******************************************************************************\
  16540. *
  16541. * C++ soap struct methods
  16542. *
  16543. \******************************************************************************/
  16544. #ifdef __cplusplus
  16545. soap::soap()
  16546. { soap_init(this);
  16547. }
  16548. #endif
  16549. /******************************************************************************/
  16550. #ifdef __cplusplus
  16551. soap::soap(soap_mode m)
  16552. { soap_init1(this, m);
  16553. }
  16554. #endif
  16555. /******************************************************************************/
  16556. #ifdef __cplusplus
  16557. soap::soap(soap_mode im, soap_mode om)
  16558. { soap_init2(this, im, om);
  16559. }
  16560. #endif
  16561. /******************************************************************************/
  16562. #ifdef __cplusplus
  16563. soap::soap(const struct soap& soap)
  16564. { soap_copy_context(this, &soap);
  16565. }
  16566. #endif
  16567. /******************************************************************************/
  16568. #ifdef __cplusplus
  16569. soap::~soap()
  16570. { soap_destroy(this);
  16571. soap_end(this);
  16572. soap_done(this);
  16573. }
  16574. #endif
  16575. /******************************************************************************/