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.

17001 lines
490 KiB

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. ok = 1;
  4279. else
  4280. { unsigned char *tmp = NULL;
  4281. ASN1_STRING_to_UTF8(&tmp, name);
  4282. if (tmp)
  4283. { if (!soap_tag_cmp(host, (const char*)tmp))
  4284. ok = 1;
  4285. else if (tmp[0] == '*') /* wildcard domain */
  4286. { const char *t = strchr(host, '.');
  4287. if (t && !soap_tag_cmp(t, (const char*)tmp+1))
  4288. ok = 1;
  4289. }
  4290. OPENSSL_free(tmp);
  4291. }
  4292. }
  4293. }
  4294. } while (!ok);
  4295. }
  4296. X509_free(peer);
  4297. if (!ok)
  4298. { soap_set_sender_error(soap, "SSL/TLS error", "SSL/TLS certificate host name mismatch in tcp_connect()", SOAP_SSL_ERROR);
  4299. soap->fclosesocket(soap, sk);
  4300. return SOAP_INVALID_SOCKET;
  4301. }
  4302. }
  4303. }
  4304. #endif
  4305. #ifdef WITH_GNUTLS
  4306. soap->ssl_flags |= SOAP_SSL_CLIENT;
  4307. if (!soap->session && (soap->error = soap->fsslauth(soap)))
  4308. { soap->fclosesocket(soap, sk);
  4309. return SOAP_INVALID_SOCKET;
  4310. }
  4311. gnutls_transport_set_ptr(soap->session, (gnutls_transport_ptr_t)(long)sk);
  4312. /* Set SSL sockets to non-blocking */
  4313. if (soap->connect_timeout)
  4314. { SOAP_SOCKNONBLOCK(sk)
  4315. retries = 10*soap->connect_timeout;
  4316. }
  4317. else
  4318. SOAP_SOCKBLOCK(sk)
  4319. if (retries <= 0)
  4320. retries = 100; /* timeout: 10 sec retries, 100 times 0.1 sec */
  4321. while ((r = gnutls_handshake(soap->session)))
  4322. { int s;
  4323. /* GNUTLS repeat handhake when GNUTLS_E_AGAIN */
  4324. if (retries-- <= 0)
  4325. break;
  4326. if (r == GNUTLS_E_AGAIN || r == GNUTLS_E_INTERRUPTED)
  4327. { if (!gnutls_record_get_direction(soap->session))
  4328. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  4329. else
  4330. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  4331. if (s < 0)
  4332. break;
  4333. }
  4334. else
  4335. { soap->errnum = soap_socket_errno(sk);
  4336. break;
  4337. }
  4338. }
  4339. if (r)
  4340. { soap_set_sender_error(soap, soap_ssl_error(soap, r), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  4341. soap->fclosesocket(soap, sk);
  4342. return SOAP_INVALID_SOCKET;
  4343. }
  4344. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  4345. { const char *err = ssl_verify(soap, host);
  4346. if (err)
  4347. { soap->fclosesocket(soap, sk);
  4348. soap->error = soap_set_sender_error(soap, "SSL/TLS error", err, SOAP_SSL_ERROR);
  4349. return SOAP_INVALID_SOCKET;
  4350. }
  4351. }
  4352. #endif
  4353. #else
  4354. soap->fclosesocket(soap, sk);
  4355. soap->error = SOAP_SSL_ERROR;
  4356. return SOAP_INVALID_SOCKET;
  4357. #endif
  4358. }
  4359. if (soap->recv_timeout || soap->send_timeout)
  4360. SOAP_SOCKNONBLOCK(sk)
  4361. else
  4362. SOAP_SOCKBLOCK(sk)
  4363. return sk;
  4364. }
  4365. #endif
  4366. #endif
  4367. /******************************************************************************/
  4368. #ifndef WITH_NOIO
  4369. #ifndef PALM_1
  4370. static int
  4371. tcp_select(struct soap *soap, SOAP_SOCKET sk, int flags, int timeout)
  4372. { int r;
  4373. struct timeval tv;
  4374. fd_set fd[3], *rfd, *sfd, *efd;
  4375. int retries = 0;
  4376. int eintr = SOAP_MAXEINTR;
  4377. soap->errnum = 0;
  4378. #ifndef WIN32
  4379. #if !defined(FD_SETSIZE) || defined(__QNX__) || defined(QNX)
  4380. /* no FD_SETSIZE or select() is not MT safe on some QNX: always poll */
  4381. if (1)
  4382. #else
  4383. /* if fd max set size exceeded, use poll() */
  4384. if ((int)sk >= (int)FD_SETSIZE)
  4385. #endif
  4386. #ifdef HAVE_POLL
  4387. { struct pollfd pollfd;
  4388. pollfd.fd = (int)sk;
  4389. pollfd.events = 0;
  4390. if (flags & SOAP_TCP_SELECT_RCV)
  4391. pollfd.events |= POLLIN;
  4392. if (flags & SOAP_TCP_SELECT_SND)
  4393. pollfd.events |= POLLOUT;
  4394. if (flags & SOAP_TCP_SELECT_ERR)
  4395. pollfd.events |= POLLERR;
  4396. if (timeout <= 0)
  4397. timeout /= -1000; /* -usec -> ms */
  4398. else
  4399. { retries = timeout - 1;
  4400. timeout = 1000;
  4401. }
  4402. do
  4403. { r = poll(&pollfd, 1, timeout);
  4404. if (r < 0 && (soap->errnum = soap_socket_errno(sk)) == SOAP_EINTR && eintr--)
  4405. continue;
  4406. } while (r == 0 && retries--);
  4407. if (r > 0)
  4408. { r = 0;
  4409. if ((flags & SOAP_TCP_SELECT_RCV) && (pollfd.revents & POLLIN))
  4410. r |= SOAP_TCP_SELECT_RCV;
  4411. if ((flags & SOAP_TCP_SELECT_SND) && (pollfd.revents & POLLOUT))
  4412. r |= SOAP_TCP_SELECT_SND;
  4413. if ((flags & SOAP_TCP_SELECT_ERR) && (pollfd.revents & POLLERR))
  4414. r |= SOAP_TCP_SELECT_ERR;
  4415. }
  4416. return r;
  4417. }
  4418. #else
  4419. { soap->error = SOAP_FD_EXCEEDED;
  4420. return -1;
  4421. }
  4422. #endif
  4423. #endif
  4424. if (timeout > 0)
  4425. retries = timeout - 1;
  4426. do
  4427. { rfd = sfd = efd = NULL;
  4428. if (flags & SOAP_TCP_SELECT_RCV)
  4429. { rfd = &fd[0];
  4430. FD_ZERO(rfd);
  4431. FD_SET(sk, rfd);
  4432. }
  4433. if (flags & SOAP_TCP_SELECT_SND)
  4434. { sfd = &fd[1];
  4435. FD_ZERO(sfd);
  4436. FD_SET(sk, sfd);
  4437. }
  4438. if (flags & SOAP_TCP_SELECT_ERR)
  4439. { efd = &fd[2];
  4440. FD_ZERO(efd);
  4441. FD_SET(sk, efd);
  4442. }
  4443. if (timeout <= 0)
  4444. { tv.tv_sec = -timeout / 1000000;
  4445. tv.tv_usec = -timeout % 1000000;
  4446. }
  4447. else
  4448. { tv.tv_sec = 1;
  4449. tv.tv_usec = 0;
  4450. }
  4451. r = select((int)sk + 1, rfd, sfd, efd, &tv);
  4452. if (r < 0 && (soap->errnum = soap_socket_errno(sk)) == SOAP_EINTR && eintr--)
  4453. continue;
  4454. } while (r == 0 && retries--);
  4455. if (r > 0)
  4456. { r = 0;
  4457. if ((flags & SOAP_TCP_SELECT_RCV) && FD_ISSET(sk, rfd))
  4458. r |= SOAP_TCP_SELECT_RCV;
  4459. if ((flags & SOAP_TCP_SELECT_SND) && FD_ISSET(sk, sfd))
  4460. r |= SOAP_TCP_SELECT_SND;
  4461. if ((flags & SOAP_TCP_SELECT_ERR) && FD_ISSET(sk, efd))
  4462. r |= SOAP_TCP_SELECT_ERR;
  4463. }
  4464. return r;
  4465. }
  4466. #endif
  4467. #endif
  4468. /******************************************************************************/
  4469. #ifndef WITH_NOIO
  4470. #ifndef PALM_1
  4471. static SOAP_SOCKET
  4472. tcp_accept(struct soap *soap, SOAP_SOCKET s, struct sockaddr *a, int *n)
  4473. { SOAP_SOCKET sk;
  4474. (void)soap;
  4475. sk = accept(s, a, (SOAP_SOCKLEN_T*)n); /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h */
  4476. #ifdef SOCKET_CLOSE_ON_EXEC
  4477. #ifdef WIN32
  4478. #ifndef UNDER_CE
  4479. SetHandleInformation((HANDLE)sk, HANDLE_FLAG_INHERIT, 0);
  4480. #endif
  4481. #else
  4482. fcntl(sk, F_SETFD, FD_CLOEXEC);
  4483. #endif
  4484. #endif
  4485. return sk;
  4486. }
  4487. #endif
  4488. #endif
  4489. /******************************************************************************/
  4490. #ifndef WITH_NOIO
  4491. #ifndef PALM_1
  4492. static int
  4493. tcp_disconnect(struct soap *soap)
  4494. {
  4495. #ifdef WITH_OPENSSL
  4496. if (soap->ssl)
  4497. { int r, s = 0;
  4498. if (soap->session)
  4499. { SSL_SESSION_free(soap->session);
  4500. soap->session = NULL;
  4501. }
  4502. if (*soap->host)
  4503. { soap->session = SSL_get1_session(soap->ssl);
  4504. if (soap->session)
  4505. { strcpy(soap->session_host, soap->host);
  4506. soap->session_port = soap->port;
  4507. }
  4508. }
  4509. r = SSL_shutdown(soap->ssl);
  4510. /* SSL shutdown does not work when reads are pending, non-blocking */
  4511. if (r == 0)
  4512. { while (SSL_want_read(soap->ssl))
  4513. { if (SSL_read(soap->ssl, NULL, 0)
  4514. || soap_socket_errno(soap->socket) != SOAP_EAGAIN)
  4515. { r = SSL_shutdown(soap->ssl);
  4516. break;
  4517. }
  4518. }
  4519. }
  4520. if (r == 0)
  4521. { if (soap_valid_socket(soap->socket))
  4522. { if (!soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR))
  4523. {
  4524. #if !defined(WITH_LEAN) && !defined(WIN32)
  4525. /*
  4526. wait up to 5 seconds for close_notify to be sent by peer (if peer not
  4527. present, this avoids calling SSL_shutdown() which has a lengthy return
  4528. timeout)
  4529. */
  4530. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, 5);
  4531. if (r <= 0)
  4532. { soap->errnum = 0;
  4533. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connection lost...\n"));
  4534. soap->fclosesocket(soap, soap->socket);
  4535. soap->socket = SOAP_INVALID_SOCKET;
  4536. ERR_remove_state(0);
  4537. SSL_free(soap->ssl);
  4538. soap->ssl = NULL;
  4539. return SOAP_OK;
  4540. }
  4541. #else
  4542. r = SSL_shutdown(soap->ssl);
  4543. #endif
  4544. }
  4545. }
  4546. }
  4547. if (r != 1)
  4548. { s = ERR_get_error();
  4549. if (s)
  4550. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shutdown failed: %d\n", SSL_get_error(soap->ssl, r)));
  4551. if (soap_valid_socket(soap->socket) && !(soap->omode & SOAP_IO_UDP))
  4552. { soap->fclosesocket(soap, soap->socket);
  4553. soap->socket = SOAP_INVALID_SOCKET;
  4554. }
  4555. }
  4556. }
  4557. SSL_free(soap->ssl);
  4558. soap->ssl = NULL;
  4559. if (s)
  4560. return SOAP_SSL_ERROR;
  4561. ERR_remove_state(0);
  4562. }
  4563. #endif
  4564. #ifdef WITH_GNUTLS
  4565. if (soap->session)
  4566. { gnutls_bye(soap->session, GNUTLS_SHUT_RDWR);
  4567. gnutls_deinit(soap->session);
  4568. soap->session = NULL;
  4569. }
  4570. #endif
  4571. if (soap_valid_socket(soap->socket) && !(soap->omode & SOAP_IO_UDP))
  4572. { soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_RDWR);
  4573. soap->fclosesocket(soap, soap->socket);
  4574. soap->socket = SOAP_INVALID_SOCKET;
  4575. }
  4576. return SOAP_OK;
  4577. }
  4578. #endif
  4579. #endif
  4580. /******************************************************************************/
  4581. #ifndef WITH_NOIO
  4582. #ifndef PALM_1
  4583. static int
  4584. tcp_closesocket(struct soap *soap, SOAP_SOCKET sk)
  4585. { (void)soap;
  4586. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Close socket=%d\n", (int)sk));
  4587. return soap_closesocket(sk);
  4588. }
  4589. #endif
  4590. #endif
  4591. /******************************************************************************/
  4592. #ifndef WITH_NOIO
  4593. #ifndef PALM_1
  4594. static int
  4595. tcp_shutdownsocket(struct soap *soap, SOAP_SOCKET sk, int how)
  4596. { (void)soap;
  4597. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shutdown socket=%d how=%d\n", (int)sk, how));
  4598. return shutdown(sk, how);
  4599. }
  4600. #endif
  4601. #endif
  4602. /******************************************************************************/
  4603. #ifndef WITH_NOIO
  4604. #ifndef PALM_1
  4605. SOAP_FMAC1
  4606. SOAP_SOCKET
  4607. SOAP_FMAC2
  4608. soap_bind(struct soap *soap, const char *host, int port, int backlog)
  4609. {
  4610. #ifdef WITH_IPV6
  4611. struct addrinfo *addrinfo = NULL;
  4612. struct addrinfo hints;
  4613. struct addrinfo res;
  4614. int err;
  4615. #ifdef WITH_NO_IPV6_V6ONLY
  4616. int unset = 0;
  4617. #endif
  4618. #endif
  4619. #ifndef WITH_LEAN
  4620. #ifndef WIN32
  4621. int len = SOAP_BUFLEN;
  4622. #else
  4623. int len = SOAP_BUFLEN + 1; /* speeds up windows xfer */
  4624. #endif
  4625. int set = 1;
  4626. #endif
  4627. if (soap_valid_socket(soap->master))
  4628. { soap->fclosesocket(soap, soap->master);
  4629. soap->master = SOAP_INVALID_SOCKET;
  4630. }
  4631. soap->socket = SOAP_INVALID_SOCKET;
  4632. soap->errmode = 1;
  4633. if (tcp_init(soap))
  4634. { soap_set_receiver_error(soap, tcp_error(soap), "TCP init failed in soap_bind()", SOAP_TCP_ERROR);
  4635. return SOAP_INVALID_SOCKET;
  4636. }
  4637. #ifdef WITH_IPV6
  4638. memset((void*)&hints, 0, sizeof(hints));
  4639. hints.ai_family = PF_UNSPEC;
  4640. #ifndef WITH_LEAN
  4641. if ((soap->omode & SOAP_IO_UDP))
  4642. hints.ai_socktype = SOCK_DGRAM;
  4643. else
  4644. #endif
  4645. hints.ai_socktype = SOCK_STREAM;
  4646. hints.ai_flags = AI_PASSIVE;
  4647. soap->errmode = 2;
  4648. err = getaddrinfo(host, soap_int2s(soap, port), &hints, &addrinfo);
  4649. if (err || !addrinfo)
  4650. { soap_set_receiver_error(soap, SOAP_GAI_STRERROR(err), "getaddrinfo failed in soap_bind()", SOAP_TCP_ERROR);
  4651. return SOAP_INVALID_SOCKET;
  4652. }
  4653. res = *addrinfo;
  4654. memcpy(&soap->peer, addrinfo->ai_addr, addrinfo->ai_addrlen);
  4655. soap->peerlen = addrinfo->ai_addrlen;
  4656. res.ai_addr = (struct sockaddr*)&soap->peer;
  4657. res.ai_addrlen = soap->peerlen;
  4658. freeaddrinfo(addrinfo);
  4659. soap->master = (int)socket(res.ai_family, res.ai_socktype, res.ai_protocol);
  4660. #else
  4661. #ifndef WITH_LEAN
  4662. if ((soap->omode & SOAP_IO_UDP))
  4663. soap->master = (int)socket(AF_INET, SOCK_DGRAM, 0);
  4664. else
  4665. #endif
  4666. soap->master = (int)socket(AF_INET, SOCK_STREAM, 0);
  4667. #endif
  4668. soap->errmode = 0;
  4669. if (!soap_valid_socket(soap->master))
  4670. { soap->errnum = soap_socket_errno(soap->master);
  4671. soap_set_receiver_error(soap, tcp_error(soap), "socket failed in soap_bind()", SOAP_TCP_ERROR);
  4672. return SOAP_INVALID_SOCKET;
  4673. }
  4674. soap->port = port;
  4675. #ifndef WITH_LEAN
  4676. if ((soap->omode & SOAP_IO_UDP))
  4677. soap->socket = soap->master;
  4678. #endif
  4679. #ifdef SOCKET_CLOSE_ON_EXEC
  4680. #ifdef WIN32
  4681. #ifndef UNDER_CE
  4682. SetHandleInformation((HANDLE)soap->master, HANDLE_FLAG_INHERIT, 0);
  4683. #endif
  4684. #else
  4685. fcntl(soap->master, F_SETFD, 1);
  4686. #endif
  4687. #endif
  4688. #ifndef WITH_LEAN
  4689. if (soap->bind_flags && setsockopt(soap->master, SOL_SOCKET, soap->bind_flags, (char*)&set, sizeof(int)))
  4690. { soap->errnum = soap_socket_errno(soap->master);
  4691. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in soap_bind()", SOAP_TCP_ERROR);
  4692. return SOAP_INVALID_SOCKET;
  4693. }
  4694. 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)))
  4695. { soap->errnum = soap_socket_errno(soap->master);
  4696. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in soap_bind()", SOAP_TCP_ERROR);
  4697. return SOAP_INVALID_SOCKET;
  4698. }
  4699. if (setsockopt(soap->master, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
  4700. { soap->errnum = soap_socket_errno(soap->master);
  4701. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in soap_bind()", SOAP_TCP_ERROR);
  4702. return SOAP_INVALID_SOCKET;
  4703. }
  4704. if (setsockopt(soap->master, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
  4705. { soap->errnum = soap_socket_errno(soap->master);
  4706. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in soap_bind()", SOAP_TCP_ERROR);
  4707. return SOAP_INVALID_SOCKET;
  4708. }
  4709. #ifdef TCP_NODELAY
  4710. if (!(soap->omode & SOAP_IO_UDP) && setsockopt(soap->master, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
  4711. { soap->errnum = soap_socket_errno(soap->master);
  4712. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in soap_bind()", SOAP_TCP_ERROR);
  4713. return SOAP_INVALID_SOCKET;
  4714. }
  4715. #endif
  4716. #endif
  4717. #ifdef WITH_IPV6
  4718. #ifdef WITH_IPV6_V6ONLY
  4719. if (setsockopt(soap->master, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&set, sizeof(int)))
  4720. { soap->errnum = soap_socket_errno(soap->master);
  4721. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt set IPV6_V6ONLY failed in soap_bind()", SOAP_TCP_ERROR);
  4722. return SOAP_INVALID_SOCKET;
  4723. }
  4724. #endif
  4725. #ifdef WITH_NO_IPV6_V6ONLY
  4726. if (setsockopt(soap->master, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&unset, sizeof(int)))
  4727. { soap->errnum = soap_socket_errno(soap->master);
  4728. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt unset IPV6_V6ONLY failed in soap_bind()", SOAP_TCP_ERROR);
  4729. return SOAP_INVALID_SOCKET;
  4730. }
  4731. #endif
  4732. soap->errmode = 0;
  4733. if (bind(soap->master, res.ai_addr, (int)res.ai_addrlen))
  4734. { soap->errnum = soap_socket_errno(soap->master);
  4735. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host\n"));
  4736. soap_closesock(soap);
  4737. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in soap_bind()", SOAP_TCP_ERROR);
  4738. return SOAP_INVALID_SOCKET;
  4739. }
  4740. #else
  4741. soap->peerlen = sizeof(soap->peer);
  4742. memset((void*)&soap->peer, 0, sizeof(soap->peer));
  4743. soap->peer.sin_family = AF_INET;
  4744. soap->errmode = 2;
  4745. if (host)
  4746. { if (soap->fresolve(soap, host, &soap->peer.sin_addr))
  4747. { soap_set_receiver_error(soap, tcp_error(soap), "get host by name failed in soap_bind()", SOAP_TCP_ERROR);
  4748. return SOAP_INVALID_SOCKET;
  4749. }
  4750. }
  4751. else
  4752. soap->peer.sin_addr.s_addr = htonl(INADDR_ANY);
  4753. soap->peer.sin_port = htons((short)port);
  4754. soap->errmode = 0;
  4755. if (bind(soap->master, (struct sockaddr*)&soap->peer, (int)soap->peerlen))
  4756. { soap->errnum = soap_socket_errno(soap->master);
  4757. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host\n"));
  4758. soap_closesock(soap);
  4759. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in soap_bind()", SOAP_TCP_ERROR);
  4760. return SOAP_INVALID_SOCKET;
  4761. }
  4762. #endif
  4763. if (!(soap->omode & SOAP_IO_UDP) && listen(soap->master, backlog))
  4764. { soap->errnum = soap_socket_errno(soap->master);
  4765. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host\n"));
  4766. soap_closesock(soap);
  4767. soap_set_receiver_error(soap, tcp_error(soap), "listen failed in soap_bind()", SOAP_TCP_ERROR);
  4768. return SOAP_INVALID_SOCKET;
  4769. }
  4770. return soap->master;
  4771. }
  4772. #endif
  4773. #endif
  4774. /******************************************************************************/
  4775. #ifndef WITH_NOIO
  4776. #ifndef PALM_1
  4777. SOAP_FMAC1
  4778. int
  4779. SOAP_FMAC2
  4780. soap_poll(struct soap *soap)
  4781. {
  4782. #ifndef WITH_LEAN
  4783. register int r;
  4784. if (soap_valid_socket(soap->socket))
  4785. { r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_ALL, 0);
  4786. if (r > 0 && (r & SOAP_TCP_SELECT_ERR))
  4787. r = -1;
  4788. }
  4789. else if (soap_valid_socket(soap->master))
  4790. r = tcp_select(soap, soap->master, SOAP_TCP_SELECT_SND, 0);
  4791. else
  4792. return SOAP_OK; /* OK when no socket! */
  4793. if (r > 0)
  4794. {
  4795. #ifdef WITH_OPENSSL
  4796. if (soap->imode & SOAP_ENC_SSL)
  4797. {
  4798. if (soap_valid_socket(soap->socket)
  4799. && (r & SOAP_TCP_SELECT_SND)
  4800. && (!(r & SOAP_TCP_SELECT_RCV)
  4801. || SSL_peek(soap->ssl, soap->tmpbuf, 1) > 0))
  4802. return SOAP_OK;
  4803. }
  4804. else
  4805. #endif
  4806. { int t;
  4807. if (soap_valid_socket(soap->socket)
  4808. && (r & SOAP_TCP_SELECT_SND)
  4809. && (!(r & SOAP_TCP_SELECT_RCV)
  4810. || recv(soap->socket, (char*)&t, 1, MSG_PEEK) > 0))
  4811. return SOAP_OK;
  4812. }
  4813. }
  4814. else if (r < 0)
  4815. { if ((soap_valid_socket(soap->master) || soap_valid_socket(soap->socket)) && soap_socket_errno(soap->master) != SOAP_EINTR)
  4816. { soap_set_receiver_error(soap, tcp_error(soap), "select failed in soap_poll()", SOAP_TCP_ERROR);
  4817. return soap->error = SOAP_TCP_ERROR;
  4818. }
  4819. }
  4820. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Polling: other end down on socket=%d select=%d\n", soap->socket, r));
  4821. return SOAP_EOF;
  4822. #else
  4823. return SOAP_OK;
  4824. #endif
  4825. }
  4826. #endif
  4827. #endif
  4828. /******************************************************************************/
  4829. #ifndef WITH_NOIO
  4830. #ifndef PALM_1
  4831. SOAP_FMAC1
  4832. SOAP_SOCKET
  4833. SOAP_FMAC2
  4834. soap_accept(struct soap *soap)
  4835. { int n = (int)sizeof(soap->peer);
  4836. register int err;
  4837. #ifndef WITH_LEAN
  4838. #ifndef WIN32
  4839. int len = SOAP_BUFLEN;
  4840. #else
  4841. int len = SOAP_BUFLEN + 1; /* speeds up windows xfer */
  4842. #endif
  4843. int set = 1;
  4844. #endif
  4845. soap->error = SOAP_OK;
  4846. memset((void*)&soap->peer, 0, sizeof(soap->peer));
  4847. soap->socket = SOAP_INVALID_SOCKET;
  4848. soap->errmode = 0;
  4849. soap->keep_alive = 0;
  4850. if (!soap_valid_socket(soap->master))
  4851. { soap->errnum = 0;
  4852. soap_set_receiver_error(soap, tcp_error(soap), "no master socket in soap_accept()", SOAP_TCP_ERROR);
  4853. return SOAP_INVALID_SOCKET;
  4854. }
  4855. #ifndef WITH_LEAN
  4856. if ((soap->omode & SOAP_IO_UDP))
  4857. return soap->socket = soap->master;
  4858. #endif
  4859. for (;;)
  4860. { if (soap->accept_timeout || soap->send_timeout || soap->recv_timeout)
  4861. { for (;;)
  4862. { register int r;
  4863. r = tcp_select(soap, soap->master, SOAP_TCP_SELECT_ALL, soap->accept_timeout ? soap->accept_timeout : 60);
  4864. if (r > 0)
  4865. break;
  4866. if (!r && soap->accept_timeout)
  4867. { soap_set_receiver_error(soap, "Timeout", "accept failed in soap_accept()", SOAP_TCP_ERROR);
  4868. return SOAP_INVALID_SOCKET;
  4869. }
  4870. if (r < 0)
  4871. { r = soap->errnum;
  4872. if (r != SOAP_EINTR)
  4873. { soap_closesock(soap);
  4874. soap_set_sender_error(soap, tcp_error(soap), "accept failed in soap_accept()", SOAP_TCP_ERROR);
  4875. return SOAP_INVALID_SOCKET;
  4876. }
  4877. }
  4878. }
  4879. }
  4880. if (soap->accept_timeout)
  4881. SOAP_SOCKNONBLOCK(soap->master)
  4882. else
  4883. SOAP_SOCKBLOCK(soap->master)
  4884. soap->socket = soap->faccept(soap, soap->master, (struct sockaddr*)&soap->peer, &n);
  4885. soap->peerlen = (size_t)n;
  4886. if (soap_valid_socket(soap->socket))
  4887. {
  4888. #ifdef WITH_IPV6
  4889. unsigned int ip1, ip2, ip3, ip4;
  4890. char port[16];
  4891. getnameinfo((struct sockaddr*)&soap->peer, n, soap->host, sizeof(soap->host), port, 16, NI_NUMERICHOST | NI_NUMERICSERV);
  4892. sscanf(soap->host, "%u.%u.%u.%u", &ip1, &ip2, &ip3, &ip4);
  4893. soap->ip = (unsigned long)ip1 << 24 | (unsigned long)ip2 << 16 | (unsigned long)ip3 << 8 | (unsigned long)ip4;
  4894. soap->port = soap_strtol(port, NULL, 10);
  4895. #else
  4896. soap->ip = ntohl(soap->peer.sin_addr.s_addr);
  4897. #ifdef HAVE_SNPRINTF
  4898. 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);
  4899. #else
  4900. 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);
  4901. #endif
  4902. soap->port = (int)ntohs(soap->peer.sin_port); /* does not return port number on some systems */
  4903. #endif
  4904. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Accept socket=%d at port=%d from IP='%s'\n", soap->socket, soap->port, soap->host));
  4905. #ifndef WITH_LEAN
  4906. if (soap->accept_flags == SO_LINGER)
  4907. { struct linger linger;
  4908. memset((void*)&linger, 0, sizeof(linger));
  4909. linger.l_onoff = 1;
  4910. linger.l_linger = soap->linger_time;
  4911. if (setsockopt(soap->socket, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(struct linger)))
  4912. { soap->errnum = soap_socket_errno(soap->socket);
  4913. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_LINGER failed in soap_accept()", SOAP_TCP_ERROR);
  4914. soap_closesock(soap);
  4915. return SOAP_INVALID_SOCKET;
  4916. }
  4917. }
  4918. else if (soap->accept_flags && setsockopt(soap->socket, SOL_SOCKET, soap->accept_flags, (char*)&set, sizeof(int)))
  4919. { soap->errnum = soap_socket_errno(soap->socket);
  4920. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in soap_accept()", SOAP_TCP_ERROR);
  4921. soap_closesock(soap);
  4922. return SOAP_INVALID_SOCKET;
  4923. }
  4924. if (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) && setsockopt(soap->socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&set, sizeof(int)))
  4925. { soap->errnum = soap_socket_errno(soap->socket);
  4926. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in soap_accept()", SOAP_TCP_ERROR);
  4927. soap_closesock(soap);
  4928. return SOAP_INVALID_SOCKET;
  4929. }
  4930. if (setsockopt(soap->socket, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
  4931. { soap->errnum = soap_socket_errno(soap->socket);
  4932. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in soap_accept()", SOAP_TCP_ERROR);
  4933. soap_closesock(soap);
  4934. return SOAP_INVALID_SOCKET;
  4935. }
  4936. if (setsockopt(soap->socket, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
  4937. { soap->errnum = soap_socket_errno(soap->socket);
  4938. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in soap_accept()", SOAP_TCP_ERROR);
  4939. soap_closesock(soap);
  4940. return SOAP_INVALID_SOCKET;
  4941. }
  4942. #ifdef TCP_NODELAY
  4943. if (setsockopt(soap->socket, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
  4944. { soap->errnum = soap_socket_errno(soap->socket);
  4945. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in soap_accept()", SOAP_TCP_ERROR);
  4946. soap_closesock(soap);
  4947. return SOAP_INVALID_SOCKET;
  4948. }
  4949. #endif
  4950. #endif
  4951. soap->keep_alive = (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) != 0);
  4952. if (soap->send_timeout || soap->recv_timeout)
  4953. SOAP_SOCKNONBLOCK(soap->socket)
  4954. else
  4955. SOAP_SOCKBLOCK(soap->socket)
  4956. return soap->socket;
  4957. }
  4958. err = soap_socket_errno(soap->socket);
  4959. if (err != 0 && err != SOAP_EINTR && err != SOAP_EAGAIN && err != SOAP_EWOULDBLOCK)
  4960. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Accept failed from %s\n", soap->host));
  4961. soap->errnum = err;
  4962. soap_set_receiver_error(soap, tcp_error(soap), "accept failed in soap_accept()", SOAP_TCP_ERROR);
  4963. soap_closesock(soap);
  4964. return SOAP_INVALID_SOCKET;
  4965. }
  4966. }
  4967. }
  4968. #endif
  4969. #endif
  4970. /******************************************************************************/
  4971. #ifndef PALM_1
  4972. SOAP_FMAC1
  4973. int
  4974. SOAP_FMAC2
  4975. soap_closesock(struct soap *soap)
  4976. { register int status = soap->error;
  4977. #ifndef WITH_LEANER
  4978. if (status) /* close on error: attachment state is not to be trusted */
  4979. { soap->mime.first = NULL;
  4980. soap->mime.last = NULL;
  4981. soap->dime.first = NULL;
  4982. soap->dime.last = NULL;
  4983. }
  4984. #endif
  4985. if (soap->fdisconnect && (soap->error = soap->fdisconnect(soap)))
  4986. return soap->error;
  4987. if (status == SOAP_EOF || status == SOAP_TCP_ERROR || status == SOAP_SSL_ERROR || !soap->keep_alive)
  4988. { if (soap->fclose && (soap->error = soap->fclose(soap)))
  4989. return soap->error;
  4990. soap->keep_alive = 0;
  4991. }
  4992. #ifdef WITH_ZLIB
  4993. if (!(soap->mode & SOAP_MIME_POSTCHECK))
  4994. { if (soap->zlib_state == SOAP_ZLIB_DEFLATE)
  4995. deflateEnd(soap->d_stream);
  4996. else if (soap->zlib_state == SOAP_ZLIB_INFLATE)
  4997. inflateEnd(soap->d_stream);
  4998. soap->zlib_state = SOAP_ZLIB_NONE;
  4999. }
  5000. #endif
  5001. return soap->error = status;
  5002. }
  5003. #endif
  5004. /******************************************************************************/
  5005. #ifndef WITH_NOIO
  5006. #ifndef PALM_1
  5007. SOAP_FMAC1
  5008. int
  5009. SOAP_FMAC2
  5010. soap_force_closesock(struct soap *soap)
  5011. { soap->keep_alive = 0;
  5012. if (soap_valid_socket(soap->socket))
  5013. return soap_closesocket(soap->socket);
  5014. return SOAP_OK;
  5015. }
  5016. #endif
  5017. #endif
  5018. /******************************************************************************/
  5019. #ifndef WITH_NOIO
  5020. #ifndef PALM_2
  5021. SOAP_FMAC1
  5022. void
  5023. SOAP_FMAC2
  5024. soap_cleanup(struct soap *soap)
  5025. { soap_done(soap);
  5026. #ifdef WIN32
  5027. if (!tcp_done)
  5028. return;
  5029. tcp_done = 0;
  5030. WSACleanup();
  5031. #endif
  5032. }
  5033. #endif
  5034. #endif
  5035. /******************************************************************************/
  5036. #ifndef PALM_1
  5037. SOAP_FMAC1
  5038. void
  5039. SOAP_FMAC2
  5040. soap_done(struct soap *soap)
  5041. {
  5042. #ifdef SOAP_DEBUG
  5043. int i;
  5044. #endif
  5045. if (soap_check_state(soap))
  5046. return;
  5047. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Done with context%s\n", soap->state == SOAP_COPY ? " copy" : ""));
  5048. soap_free_temp(soap);
  5049. while (soap->clist)
  5050. { struct soap_clist *p = soap->clist->next;
  5051. SOAP_FREE(soap, soap->clist);
  5052. soap->clist = p;
  5053. }
  5054. if (soap->state == SOAP_INIT)
  5055. soap->omode &= ~SOAP_IO_UDP; /* to force close the socket */
  5056. soap->keep_alive = 0; /* to force close the socket */
  5057. if (soap->master == soap->socket) /* do not close twice */
  5058. soap->master = SOAP_INVALID_SOCKET;
  5059. soap_closesock(soap);
  5060. #ifdef WITH_COOKIES
  5061. soap_free_cookies(soap);
  5062. #endif
  5063. while (soap->plugins)
  5064. { register struct soap_plugin *p = soap->plugins->next;
  5065. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Removing plugin '%s'\n", soap->plugins->id));
  5066. if (soap->plugins->fcopy || soap->state == SOAP_INIT)
  5067. soap->plugins->fdelete(soap, soap->plugins);
  5068. SOAP_FREE(soap, soap->plugins);
  5069. soap->plugins = p;
  5070. }
  5071. soap->fplugin = fplugin;
  5072. soap->fmalloc = NULL;
  5073. #ifndef WITH_NOHTTP
  5074. soap->fpost = http_post;
  5075. soap->fget = http_get;
  5076. soap->fput = http_405;
  5077. soap->fdel = http_405;
  5078. soap->fopt = http_200;
  5079. soap->fhead = http_200;
  5080. soap->fform = NULL;
  5081. soap->fposthdr = http_post_header;
  5082. soap->fresponse = http_response;
  5083. soap->fparse = http_parse;
  5084. soap->fparsehdr = http_parse_header;
  5085. #endif
  5086. soap->fheader = NULL;
  5087. #ifndef WITH_NOIO
  5088. #ifndef WITH_IPV6
  5089. soap->fresolve = tcp_gethost;
  5090. #else
  5091. soap->fresolve = NULL;
  5092. #endif
  5093. soap->faccept = tcp_accept;
  5094. soap->fopen = tcp_connect;
  5095. soap->fclose = tcp_disconnect;
  5096. soap->fclosesocket = tcp_closesocket;
  5097. soap->fshutdownsocket = tcp_shutdownsocket;
  5098. soap->fsend = fsend;
  5099. soap->frecv = frecv;
  5100. soap->fpoll = soap_poll;
  5101. #else
  5102. soap->fopen = NULL;
  5103. soap->fclose = NULL;
  5104. soap->fpoll = NULL;
  5105. #endif
  5106. #ifndef WITH_LEANER
  5107. soap->feltbegin = NULL;
  5108. soap->feltendin = NULL;
  5109. soap->feltbegout = NULL;
  5110. soap->feltendout = NULL;
  5111. soap->fprepareinitsend = NULL;
  5112. soap->fprepareinitrecv = NULL;
  5113. soap->fpreparesend = NULL;
  5114. soap->fpreparerecv = NULL;
  5115. soap->fpreparefinalsend = NULL;
  5116. soap->fpreparefinalrecv = NULL;
  5117. soap->ffiltersend = NULL;
  5118. soap->ffilterrecv = NULL;
  5119. #endif
  5120. soap->fseterror = NULL;
  5121. soap->fignore = NULL;
  5122. soap->fserveloop = NULL;
  5123. #ifdef WITH_OPENSSL
  5124. if (soap->session)
  5125. { SSL_SESSION_free(soap->session);
  5126. soap->session = NULL;
  5127. }
  5128. #endif
  5129. if (soap->state == SOAP_INIT)
  5130. { if (soap_valid_socket(soap->master))
  5131. { soap->fclosesocket(soap, soap->master);
  5132. soap->master = SOAP_INVALID_SOCKET;
  5133. }
  5134. }
  5135. #ifdef WITH_OPENSSL
  5136. if (soap->ssl)
  5137. { SSL_free(soap->ssl);
  5138. soap->ssl = NULL;
  5139. }
  5140. if (soap->state == SOAP_INIT)
  5141. { if (soap->ctx)
  5142. { SSL_CTX_free(soap->ctx);
  5143. soap->ctx = NULL;
  5144. }
  5145. }
  5146. ERR_remove_state(0);
  5147. #endif
  5148. #ifdef WITH_GNUTLS
  5149. if (soap->state == SOAP_INIT)
  5150. { if (soap->xcred)
  5151. { gnutls_certificate_free_credentials(soap->xcred);
  5152. soap->xcred = NULL;
  5153. }
  5154. if (soap->acred)
  5155. { gnutls_anon_free_client_credentials(soap->acred);
  5156. soap->acred = NULL;
  5157. }
  5158. if (soap->cache)
  5159. { gnutls_priority_deinit(soap->cache);
  5160. soap->cache = NULL;
  5161. }
  5162. if (soap->dh_params)
  5163. { gnutls_dh_params_deinit(soap->dh_params);
  5164. soap->dh_params = NULL;
  5165. }
  5166. if (soap->rsa_params)
  5167. { gnutls_rsa_params_deinit(soap->rsa_params);
  5168. soap->rsa_params = NULL;
  5169. }
  5170. }
  5171. if (soap->session)
  5172. { gnutls_deinit(soap->session);
  5173. soap->session = NULL;
  5174. }
  5175. #endif
  5176. #ifdef WITH_C_LOCALE
  5177. # ifdef WIN32
  5178. _free_locale(soap->c_locale);
  5179. # else
  5180. freelocale(soap->c_locale);
  5181. # endif
  5182. #endif
  5183. #ifdef WITH_ZLIB
  5184. if (soap->d_stream)
  5185. { SOAP_FREE(soap, (void*)soap->d_stream);
  5186. soap->d_stream = NULL;
  5187. }
  5188. if (soap->z_buf)
  5189. { SOAP_FREE(soap, (void*)soap->z_buf);
  5190. soap->z_buf = NULL;
  5191. }
  5192. #endif
  5193. #ifdef SOAP_DEBUG
  5194. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free logfiles\n"));
  5195. for (i = 0; i < SOAP_MAXLOGS; i++)
  5196. { if (soap->logfile[i])
  5197. { SOAP_FREE(soap, (void*)soap->logfile[i]);
  5198. soap->logfile[i] = NULL;
  5199. }
  5200. soap_close_logfile(soap, i);
  5201. }
  5202. soap->state = SOAP_NONE;
  5203. #endif
  5204. #ifdef SOAP_MEM_DEBUG
  5205. soap_free_mht(soap);
  5206. #endif
  5207. }
  5208. #endif
  5209. /******************************************************************************\
  5210. *
  5211. * HTTP
  5212. *
  5213. \******************************************************************************/
  5214. /******************************************************************************/
  5215. #ifndef WITH_NOHTTP
  5216. #ifndef PALM_1
  5217. static int
  5218. http_parse(struct soap *soap)
  5219. { char header[SOAP_HDRLEN], *s;
  5220. unsigned short httpcmd = 0;
  5221. int status = 0;
  5222. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Waiting for HTTP request/response...\n"));
  5223. *soap->endpoint = '\0';
  5224. #ifdef WITH_NTLM
  5225. if (!soap->ntlm_challenge)
  5226. #endif
  5227. { soap->userid = NULL;
  5228. soap->passwd = NULL;
  5229. soap->authrealm = NULL;
  5230. }
  5231. #ifdef WITH_NTLM
  5232. soap->ntlm_challenge = NULL;
  5233. #endif
  5234. soap->proxy_from = NULL;
  5235. do
  5236. { soap->length = 0;
  5237. soap->http_content = NULL;
  5238. soap->action = NULL;
  5239. soap->status = 0;
  5240. soap->body = 1;
  5241. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  5242. { if (soap->error == SOAP_EOF)
  5243. return SOAP_EOF;
  5244. return soap->error = 414;
  5245. }
  5246. if ((s = strchr(soap->msgbuf, ' ')))
  5247. { soap->status = (unsigned short)soap_strtoul(s, &s, 10);
  5248. if (!soap_blank((soap_wchar)*s))
  5249. soap->status = 0;
  5250. }
  5251. else
  5252. soap->status = 0;
  5253. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP status: %s\n", soap->msgbuf));
  5254. for (;;)
  5255. { if (soap_getline(soap, header, SOAP_HDRLEN))
  5256. { if (soap->error == SOAP_EOF)
  5257. { soap->error = SOAP_OK;
  5258. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "EOF in HTTP header, continue anyway\n"));
  5259. break;
  5260. }
  5261. return soap->error;
  5262. }
  5263. if (!*header)
  5264. break;
  5265. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP header: %s\n", header));
  5266. s = strchr(header, ':');
  5267. if (s)
  5268. { char *t;
  5269. *s = '\0';
  5270. do s++;
  5271. while (*s && *s <= 32);
  5272. if (*s == '"')
  5273. s++;
  5274. t = s + strlen(s) - 1;
  5275. while (t > s && *t <= 32)
  5276. t--;
  5277. if (t >= s && *t == '"')
  5278. t--;
  5279. t[1] = '\0';
  5280. if ((soap->error = soap->fparsehdr(soap, header, s)))
  5281. { if (soap->error < SOAP_STOP)
  5282. return soap->error;
  5283. status = soap->error;
  5284. soap->error = SOAP_OK;
  5285. }
  5286. }
  5287. }
  5288. } while (soap->status == 100);
  5289. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Finished HTTP header parsing, status = %d\n", soap->status));
  5290. s = strstr(soap->msgbuf, "HTTP/");
  5291. if (s && s[7] != '1')
  5292. { if (soap->keep_alive == 1)
  5293. soap->keep_alive = 0;
  5294. if (soap->status == 0 && (soap->omode & SOAP_IO) == SOAP_IO_CHUNK) /* soap->status == 0 for HTTP request */
  5295. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE; /* HTTP 1.0 does not support chunked transfers */
  5296. }
  5297. if (soap->keep_alive < 0)
  5298. soap->keep_alive = 1;
  5299. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Keep alive connection = %d\n", soap->keep_alive));
  5300. if (soap->status == 0)
  5301. { size_t l = 0;
  5302. if (s)
  5303. { if (!strncmp(soap->msgbuf, "POST ", l = 5))
  5304. httpcmd = 1;
  5305. else if (!strncmp(soap->msgbuf, "PUT ", l = 4))
  5306. httpcmd = 2;
  5307. else if (!strncmp(soap->msgbuf, "GET ", l = 4))
  5308. httpcmd = 3;
  5309. else if (!strncmp(soap->msgbuf, "DELETE ", l = 7))
  5310. httpcmd = 4;
  5311. else if (!strncmp(soap->msgbuf, "OPTIONS ", l = 8))
  5312. httpcmd = 5;
  5313. else if (!strncmp(soap->msgbuf, "HEAD ", l = 5))
  5314. httpcmd = 6;
  5315. }
  5316. if (s && httpcmd)
  5317. { size_t m = strlen(soap->endpoint);
  5318. size_t n = m + (s - soap->msgbuf) - l - 1;
  5319. size_t k;
  5320. if (n >= sizeof(soap->endpoint))
  5321. n = sizeof(soap->endpoint) - 1;
  5322. if (m > n)
  5323. m = n;
  5324. k = n - m + 1;
  5325. if (k > sizeof(soap->path))
  5326. k = sizeof(soap->path);
  5327. strncpy(soap->path, soap->msgbuf + l, k);
  5328. soap->path[k - 1] = '\0';
  5329. if (*soap->path && *soap->path != '/')
  5330. *soap->endpoint = '\0';
  5331. strcat(soap->endpoint, soap->path);
  5332. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Target endpoint='%s'\n", soap->endpoint));
  5333. if (httpcmd > 1)
  5334. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP %s handler\n", soap->msgbuf));
  5335. switch (httpcmd)
  5336. { case 2: soap->error = soap->fput(soap); break;
  5337. case 3: soap->error = soap->fget(soap); break;
  5338. case 4: soap->error = soap->fdel(soap); break;
  5339. case 5: soap->error = soap->fopt(soap); break;
  5340. case 6: soap->error = soap->fhead(soap); break;
  5341. default: soap->error = 405; break;
  5342. }
  5343. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP handler return = %d\n", soap->error));
  5344. if (soap->error == SOAP_OK)
  5345. soap->error = SOAP_STOP; /* prevents further processing */
  5346. return soap->error;
  5347. }
  5348. if (status)
  5349. return soap->error = status;
  5350. }
  5351. else if (status)
  5352. return soap->error = status;
  5353. else if (s)
  5354. return soap->error = 405;
  5355. return SOAP_OK;
  5356. }
  5357. #if 0
  5358. if (soap->length > 0 || (soap->http_content && (!soap->keep_alive || soap->recv_timeout)) || (soap->imode & SOAP_IO) == SOAP_IO_CHUNK)
  5359. #endif
  5360. if (soap->body)
  5361. { if ((soap->status >= 200 && soap->status <= 299) /* OK, Accepted, etc */
  5362. || soap->status == 400 /* Bad Request */
  5363. || soap->status == 500) /* Internal Server Error */
  5364. return SOAP_OK;
  5365. /* force close afterwards in soap_closesock() */
  5366. soap->keep_alive = 0;
  5367. #ifndef WITH_LEAN
  5368. /* read HTTP body for error details */
  5369. s = soap_get_http_body(soap, NULL);
  5370. if (s)
  5371. return soap_set_receiver_error(soap, soap->msgbuf, s, soap->status);
  5372. #endif
  5373. }
  5374. else if (soap->status >= 200 && soap->status <= 299)
  5375. return soap->error = soap->status;
  5376. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "HTTP error %d\n", soap->status));
  5377. return soap_set_receiver_error(soap, "HTTP Error", soap->msgbuf, soap->status);
  5378. }
  5379. #endif
  5380. #endif
  5381. /******************************************************************************/
  5382. #ifndef WITH_NOHTTP
  5383. #ifndef PALM_1
  5384. static int
  5385. http_parse_header(struct soap *soap, const char *key, const char *val)
  5386. { if (!soap_tag_cmp(key, "Host"))
  5387. {
  5388. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  5389. if (soap->imode & SOAP_ENC_SSL)
  5390. strcpy(soap->endpoint, "https://");
  5391. else
  5392. #endif
  5393. strcpy(soap->endpoint, "http://");
  5394. strncat(soap->endpoint, val, sizeof(soap->endpoint) - 8);
  5395. }
  5396. #ifndef WITH_LEANER
  5397. else if (!soap_tag_cmp(key, "Content-Type"))
  5398. { const char *action;
  5399. soap->http_content = soap_strdup(soap, val);
  5400. if (soap_get_header_attribute(soap, val, "application/dime"))
  5401. soap->imode |= SOAP_ENC_DIME;
  5402. else if (soap_get_header_attribute(soap, val, "multipart/related")
  5403. || soap_get_header_attribute(soap, val, "multipart/form-data"))
  5404. { soap->mime.boundary = soap_strdup(soap, soap_get_header_attribute(soap, val, "boundary"));
  5405. soap->mime.start = soap_strdup(soap, soap_get_header_attribute(soap, val, "start"));
  5406. soap->imode |= SOAP_ENC_MIME;
  5407. }
  5408. action = soap_get_header_attribute(soap, val, "action");
  5409. if (action)
  5410. { if (*action == '"')
  5411. { soap->action = soap_strdup(soap, action + 1);
  5412. if (*soap->action)
  5413. soap->action[strlen(soap->action) - 1] = '\0';
  5414. }
  5415. else
  5416. soap->action = soap_strdup(soap, action);
  5417. }
  5418. }
  5419. #endif
  5420. else if (!soap_tag_cmp(key, "Content-Length"))
  5421. { soap->length = soap_strtoul(val, NULL, 10);
  5422. if (!soap->length)
  5423. soap->body = 0;
  5424. }
  5425. else if (!soap_tag_cmp(key, "Content-Encoding"))
  5426. { if (!soap_tag_cmp(val, "deflate"))
  5427. #ifdef WITH_ZLIB
  5428. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  5429. #else
  5430. return SOAP_ZLIB_ERROR;
  5431. #endif
  5432. else if (!soap_tag_cmp(val, "gzip"))
  5433. #ifdef WITH_GZIP
  5434. soap->zlib_in = SOAP_ZLIB_GZIP;
  5435. #else
  5436. return SOAP_ZLIB_ERROR;
  5437. #endif
  5438. }
  5439. #ifdef WITH_ZLIB
  5440. else if (!soap_tag_cmp(key, "Accept-Encoding"))
  5441. {
  5442. #ifdef WITH_GZIP
  5443. if (strchr(val, '*') || soap_get_header_attribute(soap, val, "gzip"))
  5444. soap->zlib_out = SOAP_ZLIB_GZIP;
  5445. else
  5446. #endif
  5447. if (strchr(val, '*') || soap_get_header_attribute(soap, val, "deflate"))
  5448. soap->zlib_out = SOAP_ZLIB_DEFLATE;
  5449. else
  5450. soap->zlib_out = SOAP_ZLIB_NONE;
  5451. }
  5452. #endif
  5453. else if (!soap_tag_cmp(key, "Transfer-Encoding"))
  5454. { soap->imode &= ~SOAP_IO;
  5455. if (!soap_tag_cmp(val, "chunked"))
  5456. soap->imode |= SOAP_IO_CHUNK;
  5457. }
  5458. else if (!soap_tag_cmp(key, "Connection"))
  5459. { if (!soap_tag_cmp(val, "keep-alive"))
  5460. soap->keep_alive = -soap->keep_alive;
  5461. else if (!soap_tag_cmp(val, "close"))
  5462. soap->keep_alive = 0;
  5463. }
  5464. #ifndef WITH_LEAN
  5465. else if (!soap_tag_cmp(key, "Authorization") || !soap_tag_cmp(key, "Proxy-Authorization"))
  5466. {
  5467. #ifdef WITH_NTLM
  5468. if (!soap_tag_cmp(val, "NTLM*"))
  5469. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  5470. else
  5471. #endif
  5472. if (!soap_tag_cmp(val, "Basic *"))
  5473. { int n;
  5474. char *s;
  5475. soap_base642s(soap, val + 6, soap->tmpbuf, sizeof(soap->tmpbuf) - 1, &n);
  5476. soap->tmpbuf[n] = '\0';
  5477. if ((s = strchr(soap->tmpbuf, ':')))
  5478. { *s = '\0';
  5479. soap->userid = soap_strdup(soap, soap->tmpbuf);
  5480. soap->passwd = soap_strdup(soap, s + 1);
  5481. }
  5482. }
  5483. }
  5484. else if (!soap_tag_cmp(key, "WWW-Authenticate") || !soap_tag_cmp(key, "Proxy-Authenticate"))
  5485. {
  5486. #ifdef WITH_NTLM
  5487. if (!soap_tag_cmp(val, "NTLM*"))
  5488. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  5489. else
  5490. #endif
  5491. soap->authrealm = soap_strdup(soap, soap_get_header_attribute(soap, val + 6, "realm"));
  5492. }
  5493. else if (!soap_tag_cmp(key, "Expect"))
  5494. { if (!soap_tag_cmp(val, "100-continue"))
  5495. { if ((soap->error = soap->fposthdr(soap, "HTTP/1.1 100 Continue", NULL))
  5496. || (soap->error = soap->fposthdr(soap, NULL, NULL)))
  5497. return soap->error;
  5498. }
  5499. }
  5500. #endif
  5501. else if (!soap_tag_cmp(key, "SOAPAction"))
  5502. { if (*val == '"')
  5503. { soap->action = soap_strdup(soap, val + 1);
  5504. if (*soap->action)
  5505. soap->action[strlen(soap->action) - 1] = '\0';
  5506. }
  5507. else
  5508. soap->action = soap_strdup(soap, val);
  5509. }
  5510. else if (!soap_tag_cmp(key, "Location"))
  5511. { strncpy(soap->endpoint, val, sizeof(soap->endpoint));
  5512. soap->endpoint[sizeof(soap->endpoint) - 1] = '\0';
  5513. }
  5514. else if (!soap_tag_cmp(key, "X-Forwarded-For"))
  5515. { soap->proxy_from = soap_strdup(soap, val);
  5516. }
  5517. #ifdef WITH_COOKIES
  5518. else if (!soap_tag_cmp(key, "Cookie")
  5519. || !soap_tag_cmp(key, "Cookie2")
  5520. || !soap_tag_cmp(key, "Set-Cookie")
  5521. || !soap_tag_cmp(key, "Set-Cookie2"))
  5522. { soap_getcookies(soap, val);
  5523. }
  5524. #endif
  5525. return SOAP_OK;
  5526. }
  5527. #endif
  5528. #endif
  5529. /******************************************************************************/
  5530. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5531. #ifndef PALM_1
  5532. SOAP_FMAC1
  5533. const char*
  5534. SOAP_FMAC2
  5535. soap_get_header_attribute(struct soap *soap, const char *line, const char *key)
  5536. { register const char *s = line;
  5537. if (s)
  5538. { while (*s)
  5539. { register short flag;
  5540. s = soap_decode_key(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  5541. flag = soap_tag_cmp(soap->tmpbuf, key);
  5542. s = soap_decode_val(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  5543. if (!flag)
  5544. return soap->tmpbuf;
  5545. }
  5546. }
  5547. return NULL;
  5548. }
  5549. #endif
  5550. #endif
  5551. /******************************************************************************/
  5552. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5553. #ifndef PALM_1
  5554. SOAP_FMAC1
  5555. const char*
  5556. SOAP_FMAC2
  5557. soap_decode_key(char *buf, size_t len, const char *val)
  5558. { return soap_decode(buf, len, val, "=,;");
  5559. }
  5560. #endif
  5561. #endif
  5562. /******************************************************************************/
  5563. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5564. #ifndef PALM_1
  5565. SOAP_FMAC1
  5566. const char*
  5567. SOAP_FMAC2
  5568. soap_decode_val(char *buf, size_t len, const char *val)
  5569. { if (*val != '=')
  5570. { *buf = '\0';
  5571. return val;
  5572. }
  5573. return soap_decode(buf, len, val + 1, ",;");
  5574. }
  5575. #endif
  5576. #endif
  5577. /******************************************************************************/
  5578. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  5579. #ifndef PALM_1
  5580. static const char*
  5581. soap_decode(char *buf, size_t len, const char *val, const char *sep)
  5582. { const char *s;
  5583. char *t = buf;
  5584. size_t i = len;
  5585. for (s = val; *s; s++)
  5586. if (*s != ' ' && *s != '\t' && !strchr(sep, *s))
  5587. break;
  5588. if (len > 0)
  5589. { if (*s == '"')
  5590. { s++;
  5591. while (*s && *s != '"' && --i)
  5592. *t++ = *s++;
  5593. }
  5594. else
  5595. { while (*s && !soap_blank((soap_wchar)*s) && !strchr(sep, *s) && --i)
  5596. { if (*s == '%' && s[1] && s[2])
  5597. { *t++ = ((s[1] >= 'A' ? (s[1] & 0x7) + 9 : s[1] - '0') << 4)
  5598. + (s[2] >= 'A' ? (s[2] & 0x7) + 9 : s[2] - '0');
  5599. s += 3;
  5600. }
  5601. else
  5602. *t++ = *s++;
  5603. }
  5604. }
  5605. buf[len - 1] = '\0'; /* appease */
  5606. }
  5607. *t = '\0';
  5608. while (*s && !strchr(sep, *s))
  5609. s++;
  5610. return s;
  5611. }
  5612. #endif
  5613. #endif
  5614. /******************************************************************************/
  5615. #ifndef WITH_NOHTTP
  5616. #ifndef PALM_1
  5617. static const char*
  5618. http_error(struct soap *soap, int status)
  5619. { register const char *msg = SOAP_STR_EOS;
  5620. (void)soap;
  5621. #ifndef WITH_LEAN
  5622. msg = soap_code_str(h_http_error_codes, status);
  5623. if (!msg)
  5624. msg = SOAP_STR_EOS;
  5625. #endif
  5626. return msg;
  5627. }
  5628. #endif
  5629. #endif
  5630. /******************************************************************************/
  5631. #ifndef WITH_NOHTTP
  5632. #ifndef PALM_1
  5633. static int
  5634. http_get(struct soap *soap)
  5635. { (void)soap;
  5636. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP GET request\n"));
  5637. return SOAP_GET_METHOD;
  5638. }
  5639. #endif
  5640. #endif
  5641. /******************************************************************************/
  5642. #ifndef WITH_NOHTTP
  5643. #ifndef PALM_1
  5644. static int
  5645. http_405(struct soap *soap)
  5646. { (void)soap;
  5647. return 405;
  5648. }
  5649. #endif
  5650. #endif
  5651. /******************************************************************************/
  5652. #ifndef WITH_NOHTTP
  5653. #ifndef PALM_1
  5654. static int
  5655. http_200(struct soap *soap)
  5656. { return soap_send_empty_response(soap, 200);
  5657. }
  5658. #endif
  5659. #endif
  5660. /******************************************************************************/
  5661. #ifndef WITH_NOHTTP
  5662. #ifndef PALM_1
  5663. static int
  5664. http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action, size_t count)
  5665. { register const char *s;
  5666. register int err;
  5667. switch (soap->status)
  5668. { case SOAP_GET:
  5669. s = "GET";
  5670. break;
  5671. case SOAP_PUT:
  5672. s = "PUT";
  5673. break;
  5674. case SOAP_DEL:
  5675. s = "DELETE";
  5676. break;
  5677. case SOAP_CONNECT:
  5678. s = "CONNECT";
  5679. break;
  5680. default:
  5681. s = "POST";
  5682. }
  5683. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP %s to %s\n", s, endpoint ? endpoint : "(null)"));
  5684. #ifdef PALM
  5685. 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))
  5686. #else
  5687. if (!endpoint || (soap_tag_cmp(endpoint, "http:*") && soap_tag_cmp(endpoint, "https:*") && strncmp(endpoint, "httpg:", 6)))
  5688. #endif
  5689. return SOAP_OK;
  5690. if (strlen(endpoint) + strlen(soap->http_version) > sizeof(soap->tmpbuf) - 80
  5691. || strlen(host) + strlen(soap->http_version) > sizeof(soap->tmpbuf) - 80)
  5692. return soap->error = SOAP_EOM; /* prevent overrun (note that 'host' and 'soap->host' are substrings of 'endpoint') */
  5693. if (soap->status == SOAP_CONNECT)
  5694. {
  5695. #ifdef HAVE_SNPRINTF
  5696. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s %s:%d HTTP/%s", s, soap->host, soap->port, soap->http_version);
  5697. #else
  5698. sprintf(soap->tmpbuf, "%s %s:%d HTTP/%s", s, soap->host, soap->port, soap->http_version);
  5699. #endif
  5700. }
  5701. else if (soap->proxy_host && endpoint)
  5702. {
  5703. #ifdef HAVE_SNPRINTF
  5704. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s %s HTTP/%s", s, endpoint, soap->http_version);
  5705. #else
  5706. sprintf(soap->tmpbuf, "%s %s HTTP/%s", s, endpoint, soap->http_version);
  5707. #endif
  5708. }
  5709. else
  5710. {
  5711. #ifdef HAVE_SNPRINTF
  5712. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s /%s HTTP/%s", s, (*path == '/' ? path + 1 : path), soap->http_version);
  5713. #else
  5714. sprintf(soap->tmpbuf, "%s /%s HTTP/%s", s, (*path == '/' ? path + 1 : path), soap->http_version);
  5715. #endif
  5716. }
  5717. if ((err = soap->fposthdr(soap, soap->tmpbuf, NULL)))
  5718. return err;
  5719. #ifdef WITH_OPENSSL
  5720. if ((soap->ssl && port != 443) || (!soap->ssl && port != 80))
  5721. #else
  5722. if (port != 80)
  5723. #endif
  5724. {
  5725. #ifdef WITH_IPV6
  5726. if (*host != '[' && strchr(host, ':'))
  5727. {
  5728. #ifdef HAVE_SNPRINTF
  5729. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "[%s]:%d", host, port); /* RFC 2732 */
  5730. #else
  5731. sprintf(soap->tmpbuf, "[%s]:%d", host, port); /* RFC 2732 */
  5732. #endif
  5733. }
  5734. else
  5735. #endif
  5736. {
  5737. #ifdef HAVE_SNPRINTF
  5738. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s:%d", host, port);
  5739. #else
  5740. sprintf(soap->tmpbuf, "%s:%d", host, port);
  5741. #endif
  5742. }
  5743. }
  5744. else
  5745. {
  5746. #ifdef WITH_IPV6
  5747. if (*host != '[' && strchr(host, ':'))
  5748. {
  5749. #ifdef HAVE_SNPRINTF
  5750. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "[%s]", host); /* RFC 2732 */
  5751. #else
  5752. sprintf(soap->tmpbuf, "[%s]", host); /* RFC 2732 */
  5753. #endif
  5754. }
  5755. else
  5756. #endif
  5757. strcpy(soap->tmpbuf, host);
  5758. }
  5759. if ((err = soap->fposthdr(soap, "Host", soap->tmpbuf)))
  5760. return err;
  5761. if ((err = soap->fposthdr(soap, "User-Agent", "gSOAP/2.8")))
  5762. return err;
  5763. if ((err = soap_puthttphdr(soap, SOAP_OK, count)))
  5764. return err;
  5765. #ifdef WITH_ZLIB
  5766. #ifdef WITH_GZIP
  5767. if ((err = soap->fposthdr(soap, "Accept-Encoding", "gzip, deflate")))
  5768. #else
  5769. if ((err = soap->fposthdr(soap, "Accept-Encoding", "deflate")))
  5770. #endif
  5771. return err;
  5772. #endif
  5773. #ifndef WITH_LEAN
  5774. #ifdef WITH_NTLM
  5775. if (soap->ntlm_challenge && strlen(soap->ntlm_challenge) + 6 < sizeof(soap->tmpbuf))
  5776. { if (*soap->ntlm_challenge)
  5777. {
  5778. #ifdef HAVE_SNPRINTF
  5779. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "NTLM %s", soap->ntlm_challenge);
  5780. #else
  5781. sprintf(soap->tmpbuf, "NTLM %s", soap->ntlm_challenge);
  5782. #endif
  5783. if (soap->proxy_host)
  5784. { if ((err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf)))
  5785. return err;
  5786. }
  5787. else if ((err = soap->fposthdr(soap, "Authorization", soap->tmpbuf)))
  5788. return err;
  5789. }
  5790. }
  5791. else
  5792. {
  5793. #endif
  5794. if (soap->userid && soap->passwd && strlen(soap->userid) + strlen(soap->passwd) < 761)
  5795. { strcpy(soap->tmpbuf, "Basic ");
  5796. #ifdef HAVE_SNPRINTF
  5797. soap_snprintf(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262, "%s:%s", soap->userid, soap->passwd);
  5798. #else
  5799. sprintf(soap->tmpbuf + 262, "%s:%s", soap->userid, soap->passwd);
  5800. #endif
  5801. soap_s2base64(soap, (const unsigned char*)(soap->tmpbuf + 262), soap->tmpbuf + 6, (int)strlen(soap->tmpbuf + 262));
  5802. if ((err = soap->fposthdr(soap, "Authorization", soap->tmpbuf)))
  5803. return err;
  5804. }
  5805. if (soap->proxy_userid && soap->proxy_passwd && strlen(soap->proxy_userid) + strlen(soap->proxy_passwd) < 761)
  5806. { strcpy(soap->tmpbuf, "Basic ");
  5807. #ifdef HAVE_SNPRINTF
  5808. soap_snprintf(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262, "%s:%s", soap->proxy_userid, soap->proxy_passwd);
  5809. #else
  5810. sprintf(soap->tmpbuf + 262, "%s:%s", soap->proxy_userid, soap->proxy_passwd);
  5811. #endif
  5812. soap_s2base64(soap, (const unsigned char*)(soap->tmpbuf + 262), soap->tmpbuf + 6, (int)strlen(soap->tmpbuf + 262));
  5813. if ((err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf)))
  5814. return err;
  5815. }
  5816. #ifdef WITH_NTLM
  5817. }
  5818. #endif
  5819. #endif
  5820. #ifdef WITH_COOKIES
  5821. #ifdef WITH_OPENSSL
  5822. if (soap_putcookies(soap, host, path, soap->ssl != NULL))
  5823. return soap->error;
  5824. #else
  5825. if (soap_putcookies(soap, host, path, 0))
  5826. return soap->error;
  5827. #endif
  5828. #endif
  5829. if (action && soap->status != SOAP_GET && soap->status != SOAP_DEL)
  5830. {
  5831. #ifdef HAVE_SNPRINTF
  5832. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "\"%s\"", action);
  5833. #else
  5834. sprintf(soap->tmpbuf, "\"%s\"", strlen(action) < sizeof(soap->tmpbuf) - 3 ? action : SOAP_STR_EOS);
  5835. #endif
  5836. if ((err = soap->fposthdr(soap, "SOAPAction", soap->tmpbuf)))
  5837. return err;
  5838. }
  5839. return soap->fposthdr(soap, NULL, NULL);
  5840. }
  5841. #endif
  5842. #endif
  5843. /******************************************************************************/
  5844. #ifndef WITH_NOHTTP
  5845. #ifndef PALM_1
  5846. static int
  5847. http_send_header(struct soap *soap, const char *s)
  5848. { register const char *t;
  5849. do
  5850. { t = strchr(s, '\n'); /* disallow \n in HTTP headers */
  5851. if (!t)
  5852. t = s + strlen(s);
  5853. if (soap_send_raw(soap, s, t - s))
  5854. return soap->error;
  5855. s = t + 1;
  5856. } while (*t);
  5857. return SOAP_OK;
  5858. }
  5859. #endif
  5860. #endif
  5861. /******************************************************************************/
  5862. #ifndef WITH_NOHTTP
  5863. #ifndef PALM_1
  5864. static int
  5865. http_post_header(struct soap *soap, const char *key, const char *val)
  5866. { if (key)
  5867. { if (http_send_header(soap, key))
  5868. return soap->error;
  5869. if (val && (soap_send_raw(soap, ": ", 2) || http_send_header(soap, val)))
  5870. return soap->error;
  5871. }
  5872. return soap_send_raw(soap, "\r\n", 2);
  5873. }
  5874. #endif
  5875. #endif
  5876. /******************************************************************************/
  5877. #ifndef WITH_NOHTTP
  5878. #ifndef PALM_1
  5879. static int
  5880. http_response(struct soap *soap, int status, size_t count)
  5881. { register int err;
  5882. char http[10];
  5883. int code = status;
  5884. const char *line;
  5885. #ifdef WMW_RPM_IO
  5886. if (soap->rpmreqid)
  5887. httpOutputEnable(soap->rpmreqid);
  5888. #endif
  5889. if (!soap->http_version || strlen(soap->http_version) > 4)
  5890. return soap->error = SOAP_EOM;
  5891. #ifdef WMW_RPM_IO
  5892. if (soap->rpmreqid || soap_valid_socket(soap->master) || soap_valid_socket(soap->socket)) /* RPM behaves as if standalone */
  5893. #else
  5894. if (soap_valid_socket(soap->master) || soap_valid_socket(soap->socket)) /* standalone application (socket) or CGI (stdin/out)? */
  5895. #endif
  5896. {
  5897. #ifdef HAVE_SNPRINTF
  5898. soap_snprintf(http, sizeof(http), "HTTP/%s", soap->http_version);
  5899. #else
  5900. sprintf(http, "HTTP/%s", soap->http_version);
  5901. #endif
  5902. }
  5903. else
  5904. strcpy(http, "Status:");
  5905. if (!status || status == SOAP_HTML || status == SOAP_FILE)
  5906. { if (count || ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK))
  5907. code = 200;
  5908. else
  5909. code = 202;
  5910. }
  5911. else if (status < 200 || status >= 600)
  5912. { const char *s = *soap_faultcode(soap);
  5913. if (status >= SOAP_GET_METHOD && status <= SOAP_HTTP_METHOD)
  5914. code = 405;
  5915. else if (soap->version == 2 && (!s || !strcmp(s, "SOAP-ENV:Sender")))
  5916. code = 400;
  5917. else
  5918. code = 500;
  5919. }
  5920. line = http_error(soap, code);
  5921. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP Status = %d %s\n", code, line));
  5922. #ifdef HAVE_SNPRINTF
  5923. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%s %d %s", http, code, line);
  5924. #else
  5925. sprintf(soap->tmpbuf, "%s %d %s", http, code, line);
  5926. #endif
  5927. if ((err = soap->fposthdr(soap, soap->tmpbuf, NULL)))
  5928. return err;
  5929. #ifndef WITH_LEAN
  5930. if (status == 401)
  5931. {
  5932. #ifdef HAVE_SNPRINTF
  5933. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "Basic realm=\"%s\"", (soap->authrealm && strlen(soap->authrealm) < sizeof(soap->tmpbuf) - 14) ? soap->authrealm : "gSOAP Web Service");
  5934. #else
  5935. sprintf(soap->tmpbuf, "Basic realm=\"%s\"", (soap->authrealm && strlen(soap->authrealm) < sizeof(soap->tmpbuf) - 14) ? soap->authrealm : "gSOAP Web Service");
  5936. #endif
  5937. if ((err = soap->fposthdr(soap, "WWW-Authenticate", soap->tmpbuf)))
  5938. return err;
  5939. }
  5940. else if ((status >= 301 && status <= 303) || status == 307)
  5941. { if ((err = soap->fposthdr(soap, "Location", soap->endpoint)))
  5942. return err;
  5943. }
  5944. #endif
  5945. if ((err = soap->fposthdr(soap, "Server", "gSOAP/2.8"))
  5946. || (err = soap_puthttphdr(soap, status, count)))
  5947. return err;
  5948. #ifdef WITH_COOKIES
  5949. if (soap_putsetcookies(soap))
  5950. return soap->error;
  5951. #endif
  5952. return soap->fposthdr(soap, NULL, NULL);
  5953. }
  5954. #endif
  5955. #endif
  5956. /******************************************************************************/
  5957. #ifndef PALM_1
  5958. SOAP_FMAC1
  5959. int
  5960. SOAP_FMAC2
  5961. soap_response(struct soap *soap, int status)
  5962. { register size_t count;
  5963. if (!(soap->omode & (SOAP_ENC_XML | SOAP_IO_STORE /* this tests for chunking too */))
  5964. && (status == SOAP_HTML || status == SOAP_FILE))
  5965. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE;
  5966. soap->status = status;
  5967. count = soap_count_attachments(soap);
  5968. if (soap_begin_send(soap))
  5969. return soap->error;
  5970. #ifndef WITH_NOHTTP
  5971. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_XML))
  5972. { register int n = soap->mode;
  5973. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  5974. if ((n & SOAP_IO) != SOAP_IO_FLUSH)
  5975. soap->mode |= SOAP_IO_BUFFER;
  5976. if ((soap->error = soap->fresponse(soap, status, count)))
  5977. return soap->error;
  5978. #ifndef WITH_LEANER
  5979. if ((n & SOAP_IO) == SOAP_IO_CHUNK)
  5980. { if (soap_flush(soap))
  5981. return soap->error;
  5982. }
  5983. #endif
  5984. soap->mode = n;
  5985. }
  5986. #endif
  5987. return SOAP_OK;
  5988. }
  5989. #endif
  5990. /******************************************************************************/
  5991. #ifndef PALM_1
  5992. SOAP_FMAC1
  5993. const char*
  5994. SOAP_FMAC2
  5995. soap_url(struct soap *soap, const char *s, const char *t)
  5996. { if (!t || (*t != '/' && *t != '?') || strlen(s) + strlen(t) >= sizeof(soap->msgbuf))
  5997. return s;
  5998. strcpy(soap->msgbuf, s);
  5999. strcat(soap->msgbuf, t);
  6000. return soap->msgbuf;
  6001. }
  6002. #endif
  6003. /******************************************************************************/
  6004. #ifndef PALM_1
  6005. SOAP_FMAC1
  6006. size_t
  6007. SOAP_FMAC2
  6008. soap_encode_url(const char *s, char *t, size_t len)
  6009. { register int c;
  6010. register size_t n = len;
  6011. while ((c = *s++) && --n > 0)
  6012. { if (c > ' ' && c < 128 && !strchr("()<>@,;:\\\"/[]?={}#!$&'*+", c))
  6013. *t++ = c;
  6014. else if (n > 2)
  6015. { *t++ = '%';
  6016. *t++ = (c >> 4) + (c > 159 ? '7' : '0');
  6017. c &= 0xF;
  6018. *t++ = c + (c > 9 ? '7' : '0');
  6019. n -= 2;
  6020. }
  6021. else
  6022. break;
  6023. }
  6024. *t = '\0';
  6025. return len - n;
  6026. }
  6027. #endif
  6028. /******************************************************************************/
  6029. #ifndef PALM_1
  6030. SOAP_FMAC1
  6031. const char*
  6032. SOAP_FMAC2
  6033. soap_encode_url_string(struct soap *soap, const char *s)
  6034. { if (s)
  6035. { size_t n = 3*strlen(s)+1;
  6036. char *t = (char*)soap_malloc(soap, n);
  6037. if (t)
  6038. { soap_encode_url(s, t, n);
  6039. return t;
  6040. }
  6041. }
  6042. return SOAP_STR_EOS;
  6043. }
  6044. #endif
  6045. /******************************************************************************\
  6046. *
  6047. * HTTP Cookies
  6048. *
  6049. \******************************************************************************/
  6050. #ifdef WITH_COOKIES
  6051. /******************************************************************************/
  6052. SOAP_FMAC1
  6053. struct soap_cookie*
  6054. SOAP_FMAC2
  6055. soap_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  6056. { struct soap_cookie *p;
  6057. if (!domain)
  6058. domain = soap->cookie_domain;
  6059. if (!path)
  6060. path = soap->cookie_path;
  6061. if (!path)
  6062. path = SOAP_STR_EOS;
  6063. else if (*path == '/')
  6064. path++;
  6065. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Search cookie='%s' domain='%s' path='%s'\n", name, domain ? domain : "(null)", path ? path : "(null)"));
  6066. for (p = soap->cookies; p; p = p->next)
  6067. { 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));
  6068. if (!strcmp(p->name, name)
  6069. && p->domain
  6070. && p->path
  6071. && !strcmp(p->domain, domain)
  6072. && (!*p->path || !strncmp(p->path, path, strlen(p->path))))
  6073. break;
  6074. }
  6075. return p;
  6076. }
  6077. /******************************************************************************/
  6078. SOAP_FMAC1
  6079. struct soap_cookie*
  6080. SOAP_FMAC2
  6081. soap_set_cookie(struct soap *soap, const char *name, const char *value, const char *domain, const char *path)
  6082. { struct soap_cookie **p, *q;
  6083. int n;
  6084. if (!domain)
  6085. domain = soap->cookie_domain;
  6086. if (!path)
  6087. path = soap->cookie_path;
  6088. if (!path)
  6089. path = SOAP_STR_EOS;
  6090. else if (*path == '/')
  6091. path++;
  6092. q = soap_cookie(soap, name, domain, path);
  6093. 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)"));
  6094. if (!q)
  6095. { if ((q = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie))))
  6096. { if ((q->name = (char*)SOAP_MALLOC(soap, strlen(name)+1)))
  6097. strcpy(q->name, name);
  6098. q->value = NULL;
  6099. q->domain = NULL;
  6100. q->path = NULL;
  6101. q->expire = 0;
  6102. q->maxage = -1;
  6103. q->version = 1;
  6104. q->secure = 0;
  6105. q->modified = 0;
  6106. for (p = &soap->cookies, n = soap->cookie_max; *p && n; p = &(*p)->next, n--)
  6107. if (!strcmp((*p)->name, name) && (*p)->path && path && strcmp((*p)->path, path) < 0)
  6108. break;
  6109. if (n)
  6110. { q->next = *p;
  6111. *p = q;
  6112. }
  6113. else
  6114. { SOAP_FREE(soap, q->name);
  6115. SOAP_FREE(soap, q);
  6116. q = NULL;
  6117. }
  6118. }
  6119. }
  6120. else
  6121. q->modified = 1;
  6122. if (q)
  6123. { if (q->value)
  6124. { if (!value || strcmp(value, q->value))
  6125. { SOAP_FREE(soap, q->value);
  6126. q->value = NULL;
  6127. }
  6128. }
  6129. if (value && *value && !q->value && (q->value = (char*)SOAP_MALLOC(soap, strlen(value)+1)))
  6130. strcpy(q->value, value);
  6131. if (q->domain)
  6132. { if (!domain || strcmp(domain, q->domain))
  6133. { SOAP_FREE(soap, q->domain);
  6134. q->domain = NULL;
  6135. }
  6136. }
  6137. if (domain && !q->domain && (q->domain = (char*)SOAP_MALLOC(soap, strlen(domain)+1)))
  6138. strcpy(q->domain, domain);
  6139. if (q->path)
  6140. { if (!path || strncmp(path, q->path, strlen(q->path)))
  6141. { SOAP_FREE(soap, q->path);
  6142. q->path = NULL;
  6143. }
  6144. }
  6145. if (path && !q->path && (q->path = (char*)SOAP_MALLOC(soap, strlen(path)+1)))
  6146. strcpy(q->path, path);
  6147. q->session = 1;
  6148. q->env = 0;
  6149. }
  6150. return q;
  6151. }
  6152. /******************************************************************************/
  6153. SOAP_FMAC1
  6154. void
  6155. SOAP_FMAC2
  6156. soap_clr_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  6157. { struct soap_cookie **p, *q;
  6158. if (!domain)
  6159. domain = soap->cookie_domain;
  6160. if (!domain)
  6161. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in clear cookie='%s': cookie domain not set\n", name ? name : "(null)"));
  6162. return;
  6163. }
  6164. if (!path)
  6165. path = soap->cookie_path;
  6166. if (!path)
  6167. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in clear cookie='%s': cookie path not set\n", name ? name : "(null)"));
  6168. return;
  6169. }
  6170. if (*path == '/')
  6171. path++;
  6172. for (p = &soap->cookies, q = *p; q; q = *p)
  6173. { if (!strcmp(q->name, name) && !strcmp(q->domain, domain) && !strncmp(q->path, path, strlen(q->path)))
  6174. { if (q->value)
  6175. SOAP_FREE(soap, q->value);
  6176. if (q->domain)
  6177. SOAP_FREE(soap, q->domain);
  6178. if (q->path)
  6179. SOAP_FREE(soap, q->path);
  6180. *p = q->next;
  6181. SOAP_FREE(soap, q);
  6182. }
  6183. else
  6184. p = &q->next;
  6185. }
  6186. }
  6187. /******************************************************************************/
  6188. SOAP_FMAC1
  6189. char *
  6190. SOAP_FMAC2
  6191. soap_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  6192. { struct soap_cookie *p;
  6193. if ((p = soap_cookie(soap, name, domain, path)))
  6194. return p->value;
  6195. return NULL;
  6196. }
  6197. /******************************************************************************/
  6198. SOAP_FMAC1
  6199. char *
  6200. SOAP_FMAC2
  6201. soap_env_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  6202. { struct soap_cookie *p;
  6203. if ((p = soap_cookie(soap, name, domain, path)) && p->env)
  6204. return p->value;
  6205. return NULL;
  6206. }
  6207. /******************************************************************************/
  6208. SOAP_FMAC1
  6209. time_t
  6210. SOAP_FMAC2
  6211. soap_cookie_expire(struct soap *soap, const char *name, const char *domain, const char *path)
  6212. { struct soap_cookie *p;
  6213. if ((p = soap_cookie(soap, name, domain, path)))
  6214. return p->expire;
  6215. return -1;
  6216. }
  6217. /******************************************************************************/
  6218. SOAP_FMAC1
  6219. int
  6220. SOAP_FMAC2
  6221. soap_set_cookie_expire(struct soap *soap, const char *name, long expire, const char *domain, const char *path)
  6222. { struct soap_cookie *p;
  6223. 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)"));
  6224. if ((p = soap_cookie(soap, name, domain, path)))
  6225. { p->maxage = expire;
  6226. p->modified = 1;
  6227. return SOAP_OK;
  6228. }
  6229. return SOAP_ERR;
  6230. }
  6231. /******************************************************************************/
  6232. SOAP_FMAC1
  6233. int
  6234. SOAP_FMAC2
  6235. soap_set_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  6236. { struct soap_cookie *p;
  6237. if ((p = soap_cookie(soap, name, domain, path)))
  6238. { p->session = 1;
  6239. p->modified = 1;
  6240. return SOAP_OK;
  6241. }
  6242. return SOAP_ERR;
  6243. }
  6244. /******************************************************************************/
  6245. SOAP_FMAC1
  6246. int
  6247. SOAP_FMAC2
  6248. soap_clr_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  6249. { struct soap_cookie *p;
  6250. if ((p = soap_cookie(soap, name, domain, path)))
  6251. { p->session = 0;
  6252. p->modified = 1;
  6253. return SOAP_OK;
  6254. }
  6255. return SOAP_ERR;
  6256. }
  6257. /******************************************************************************/
  6258. SOAP_FMAC1
  6259. int
  6260. SOAP_FMAC2
  6261. soap_putsetcookies(struct soap *soap)
  6262. { struct soap_cookie *p;
  6263. char *s, tmp[4096];
  6264. const char *t;
  6265. for (p = soap->cookies; p; p = p->next)
  6266. {
  6267. if (p->modified
  6268. #ifdef WITH_OPENSSL
  6269. || (!p->env && !soap->ssl == !p->secure)
  6270. #endif
  6271. )
  6272. { s = tmp;
  6273. if (p->name)
  6274. s += soap_encode_url(p->name, s, tmp-s+4064);
  6275. if (p->value && *p->value)
  6276. { *s++ = '=';
  6277. s += soap_encode_url(p->value, s, tmp-s+4064);
  6278. }
  6279. if (p->domain && (int)strlen(p->domain) < tmp-s+4064)
  6280. { strcpy(s, ";Domain=");
  6281. strcat(s, p->domain);
  6282. }
  6283. else if (soap->cookie_domain && (int)strlen(soap->cookie_domain) < tmp-s+4064)
  6284. { strcpy(s, ";Domain=");
  6285. strcat(s, soap->cookie_domain);
  6286. }
  6287. strcat(s, ";Path=/");
  6288. s += strlen(s);
  6289. if (p->path)
  6290. t = p->path;
  6291. else
  6292. t = soap->cookie_path;
  6293. if (t)
  6294. { if (*t == '/')
  6295. t++;
  6296. if ((int)strlen(t) < tmp-s+4064)
  6297. { if (strchr(t, '%')) /* already URL encoded? */
  6298. { strcpy(s, t);
  6299. s += strlen(s);
  6300. }
  6301. else
  6302. s += soap_encode_url(t, s, tmp-s+4064);
  6303. }
  6304. }
  6305. if (p->version > 0 && s-tmp < 4060)
  6306. {
  6307. #ifdef HAVE_SNPRINTF
  6308. soap_snprintf(s, 4096 - (s-tmp), ";Version=%u", p->version);
  6309. #else
  6310. sprintf(s, ";Version=%u", p->version);
  6311. #endif
  6312. s += strlen(s);
  6313. }
  6314. if (p->maxage >= 0 && s-tmp < 4060)
  6315. {
  6316. #ifdef HAVE_SNPRINTF
  6317. soap_snprintf(s, 4096 - (s-tmp), ";Max-Age=%ld", p->maxage);
  6318. #else
  6319. sprintf(s, ";Max-Age=%ld", p->maxage);
  6320. #endif
  6321. s += strlen(s);
  6322. }
  6323. if (s-tmp < 4073
  6324. && (p->secure
  6325. #ifdef WITH_OPENSSL
  6326. || soap->ssl
  6327. #endif
  6328. ))
  6329. strcpy(s, ";Secure");
  6330. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set-Cookie: %s\n", tmp));
  6331. if ((soap->error = soap->fposthdr(soap, "Set-Cookie", tmp)))
  6332. return soap->error;
  6333. }
  6334. }
  6335. return SOAP_OK;
  6336. }
  6337. /******************************************************************************/
  6338. SOAP_FMAC1
  6339. int
  6340. SOAP_FMAC2
  6341. soap_putcookies(struct soap *soap, const char *domain, const char *path, int secure)
  6342. { struct soap_cookie **p, *q;
  6343. unsigned int version = 0;
  6344. time_t now = time(NULL);
  6345. char *s, tmp[4096];
  6346. if (!domain || !path)
  6347. return SOAP_OK;
  6348. s = tmp;
  6349. p = &soap->cookies;
  6350. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending cookies for domain='%s' path='%s'\n", domain, path));
  6351. if (*path == '/')
  6352. path++;
  6353. while ((q = *p))
  6354. { if (q->expire && now > q->expire)
  6355. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie %s expired\n", q->name));
  6356. SOAP_FREE(soap, q->name);
  6357. if (q->value)
  6358. SOAP_FREE(soap, q->value);
  6359. if (q->domain)
  6360. SOAP_FREE(soap, q->domain);
  6361. if (q->path)
  6362. SOAP_FREE(soap, q->path);
  6363. *p = q->next;
  6364. SOAP_FREE(soap, q);
  6365. }
  6366. else
  6367. { int flag;
  6368. char *t = q->domain;
  6369. size_t n = 0;
  6370. if (!t)
  6371. flag = 1;
  6372. else
  6373. { const char *r = strchr(t, ':');
  6374. if (r)
  6375. n = r - t;
  6376. else
  6377. n = strlen(t);
  6378. flag = !strncmp(t, domain, n);
  6379. }
  6380. /* domain-level cookies, cannot compile when WITH_NOIO set */
  6381. #ifndef WITH_NOIO
  6382. if (!flag)
  6383. { struct hostent *hostent = gethostbyname((char*)domain);
  6384. if (hostent)
  6385. { const char *r = strchr(hostent->h_name, '.');
  6386. if (!r)
  6387. r = hostent->h_name;
  6388. flag = !strncmp(t, r, n);
  6389. }
  6390. }
  6391. #endif
  6392. if (flag
  6393. && (!q->path || !strncmp(q->path, path, strlen(q->path)))
  6394. && (!q->secure || secure))
  6395. { size_t n = 12;
  6396. if (q->name)
  6397. n += 3*strlen(q->name);
  6398. if (q->value && *q->value)
  6399. n += 3*strlen(q->value) + 1;
  6400. if (q->path && *q->path)
  6401. n += strlen(q->path) + 9;
  6402. if (q->domain)
  6403. n += strlen(q->domain) + 11;
  6404. if (tmp - s + n > sizeof(tmp))
  6405. { if (s == tmp)
  6406. return SOAP_OK; /* HTTP header size overflow */
  6407. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie: %s\n", tmp));
  6408. if ((soap->error = soap->fposthdr(soap, "Cookie", tmp)))
  6409. return soap->error;
  6410. s = tmp;
  6411. }
  6412. else if (s != tmp)
  6413. { strcat(s, " ");
  6414. s++;
  6415. }
  6416. if (q->version != version && s-tmp < 4060)
  6417. {
  6418. #ifdef HAVE_SNPRINTF
  6419. soap_snprintf(s, 4096 - (s-tmp), "$Version=%u;", q->version);
  6420. #else
  6421. sprintf(s, "$Version=%u;", q->version);
  6422. #endif
  6423. version = q->version;
  6424. s += strlen(s);
  6425. }
  6426. if (q->name)
  6427. s += soap_encode_url(q->name, s, tmp+sizeof(tmp)-s-16);
  6428. if (q->value && *q->value)
  6429. { *s++ = '=';
  6430. s += soap_encode_url(q->value, s, tmp+sizeof(tmp)-s-16);
  6431. }
  6432. if (q->path && (s-tmp) + strlen(q->path) < 4060)
  6433. {
  6434. #ifdef HAVE_SNPRINTF
  6435. soap_snprintf(s, 4096 - (s-tmp), ";$Path=\"/%s\"", (*q->path == '/' ? q->path + 1 : q->path));
  6436. #else
  6437. sprintf(s, ";$Path=\"/%s\"", (*q->path == '/' ? q->path + 1 : q->path));
  6438. #endif
  6439. s += strlen(s);
  6440. }
  6441. if (q->domain && (s-tmp) + strlen(q->domain) < 4060)
  6442. {
  6443. #ifdef HAVE_SNPRINTF
  6444. soap_snprintf(s, 4096 - (s-tmp), ";$Domain=\"%s\"", q->domain);
  6445. #else
  6446. sprintf(s, ";$Domain=\"%s\"", q->domain);
  6447. #endif
  6448. s += strlen(s);
  6449. }
  6450. }
  6451. p = &q->next;
  6452. }
  6453. }
  6454. if (s != tmp)
  6455. if ((soap->error = soap->fposthdr(soap, "Cookie", tmp)))
  6456. return soap->error;
  6457. return SOAP_OK;
  6458. }
  6459. /******************************************************************************/
  6460. SOAP_FMAC1
  6461. void
  6462. SOAP_FMAC2
  6463. soap_getcookies(struct soap *soap, const char *val)
  6464. { struct soap_cookie *p = NULL, *q;
  6465. const char *s;
  6466. char *t, tmp[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  6467. char *domain = NULL;
  6468. char *path = NULL;
  6469. unsigned int version = 0;
  6470. time_t now = time(NULL);
  6471. if (!val)
  6472. return;
  6473. s = val;
  6474. while (*s)
  6475. { s = soap_decode_key(tmp, sizeof(tmp), s);
  6476. if (!soap_tag_cmp(tmp, "$Version"))
  6477. { if ((s = soap_decode_val(tmp, sizeof(tmp), s)))
  6478. { if (p)
  6479. p->version = (int)soap_strtol(tmp, NULL, 10);
  6480. else
  6481. version = (int)soap_strtol(tmp, NULL, 10);
  6482. }
  6483. }
  6484. else if (!soap_tag_cmp(tmp, "$Path"))
  6485. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6486. if (*tmp)
  6487. { if ((t = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6488. strcpy(t, tmp);
  6489. }
  6490. else
  6491. t = NULL;
  6492. if (p)
  6493. { if (p->path)
  6494. SOAP_FREE(soap, p->path);
  6495. p->path = t;
  6496. }
  6497. else
  6498. { if (path)
  6499. SOAP_FREE(soap, path);
  6500. path = t;
  6501. }
  6502. }
  6503. else if (!soap_tag_cmp(tmp, "$Domain"))
  6504. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6505. if (*tmp)
  6506. { if ((t = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6507. strcpy(t, tmp);
  6508. }
  6509. else
  6510. t = NULL;
  6511. if (p)
  6512. { if (p->domain)
  6513. SOAP_FREE(soap, p->domain);
  6514. p->domain = t;
  6515. }
  6516. else
  6517. { if (domain)
  6518. SOAP_FREE(soap, domain);
  6519. domain = t;
  6520. }
  6521. }
  6522. else if (p && !soap_tag_cmp(tmp, "Path"))
  6523. { if (p->path)
  6524. SOAP_FREE(soap, p->path);
  6525. s = soap_decode_val(tmp, sizeof(tmp), s);
  6526. if (*tmp)
  6527. { if ((p->path = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6528. strcpy(p->path, tmp);
  6529. }
  6530. else
  6531. p->path = NULL;
  6532. }
  6533. else if (p && !soap_tag_cmp(tmp, "Domain"))
  6534. { if (p->domain)
  6535. SOAP_FREE(soap, p->domain);
  6536. s = soap_decode_val(tmp, sizeof(tmp), s);
  6537. if (*tmp)
  6538. { if ((p->domain = (char*)SOAP_MALLOC(soap, strlen(tmp)+1)))
  6539. strcpy(p->domain, tmp);
  6540. }
  6541. else
  6542. p->domain = NULL;
  6543. }
  6544. else if (p && !soap_tag_cmp(tmp, "Version"))
  6545. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6546. p->version = (unsigned int)soap_strtoul(tmp, NULL, 10);
  6547. }
  6548. else if (p && !soap_tag_cmp(tmp, "Max-Age"))
  6549. { s = soap_decode_val(tmp, sizeof(tmp), s);
  6550. p->expire = now + soap_strtol(tmp, NULL, 10);
  6551. }
  6552. else if (p && !soap_tag_cmp(tmp, "Expires"))
  6553. { struct tm T;
  6554. char a[3];
  6555. static const char mns[] = "anebarprayunulugepctovec";
  6556. s = soap_decode_val(tmp, sizeof(tmp), s);
  6557. if (strlen(tmp) > 20)
  6558. { memset((void*)&T, 0, sizeof(T));
  6559. a[0] = tmp[4];
  6560. a[1] = tmp[5];
  6561. a[2] = '\0';
  6562. T.tm_mday = (int)soap_strtol(a, NULL, 10);
  6563. a[0] = tmp[8];
  6564. a[1] = tmp[9];
  6565. T.tm_mon = (int)(strstr(mns, a) - mns) / 2;
  6566. a[0] = tmp[11];
  6567. a[1] = tmp[12];
  6568. T.tm_year = 100 + (int)soap_strtol(a, NULL, 10);
  6569. a[0] = tmp[13];
  6570. a[1] = tmp[14];
  6571. T.tm_hour = (int)soap_strtol(a, NULL, 10);
  6572. a[0] = tmp[16];
  6573. a[1] = tmp[17];
  6574. T.tm_min = (int)soap_strtol(a, NULL, 10);
  6575. a[0] = tmp[19];
  6576. a[1] = tmp[20];
  6577. T.tm_sec = (int)soap_strtol(a, NULL, 10);
  6578. p->expire = soap_timegm(&T);
  6579. }
  6580. }
  6581. else if (p && !soap_tag_cmp(tmp, "Secure"))
  6582. p->secure = 1;
  6583. else
  6584. { if (p)
  6585. { 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));
  6586. if ((q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path)))
  6587. { q->version = p->version;
  6588. q->expire = p->expire;
  6589. q->secure = p->secure;
  6590. q->env = 1;
  6591. }
  6592. if (p->name)
  6593. SOAP_FREE(soap, p->name);
  6594. if (p->value)
  6595. SOAP_FREE(soap, p->value);
  6596. if (p->domain)
  6597. SOAP_FREE(soap, p->domain);
  6598. if (p->path)
  6599. SOAP_FREE(soap, p->path);
  6600. SOAP_FREE(soap, p);
  6601. }
  6602. if ((p = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie))))
  6603. { p->name = (char*)SOAP_MALLOC(soap, strlen(tmp)+1);
  6604. strcpy(p->name, tmp);
  6605. s = soap_decode_val(tmp, sizeof(tmp), s);
  6606. if (*tmp)
  6607. { p->value = (char*)SOAP_MALLOC(soap, strlen(tmp)+1);
  6608. strcpy(p->value, tmp);
  6609. }
  6610. else
  6611. p->value = NULL;
  6612. if (domain)
  6613. p->domain = domain;
  6614. else if (*soap->host)
  6615. { p->domain = (char*)SOAP_MALLOC(soap, strlen(soap->host)+1);
  6616. strcpy(p->domain, soap->host);
  6617. }
  6618. else
  6619. p->domain = NULL;
  6620. if (path)
  6621. p->path = path;
  6622. else if (soap->path && *soap->path)
  6623. { p->path = (char*)SOAP_MALLOC(soap, strlen(soap->path)+1);
  6624. strcpy(p->path, soap->path);
  6625. }
  6626. else
  6627. { p->path = (char*)SOAP_MALLOC(soap, 2);
  6628. strcpy(p->path, "/");
  6629. }
  6630. p->expire = 0;
  6631. p->secure = 0;
  6632. p->version = version;
  6633. }
  6634. }
  6635. }
  6636. if (p)
  6637. { 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));
  6638. if ((q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path)))
  6639. { q->version = p->version;
  6640. q->expire = p->expire;
  6641. q->secure = p->secure;
  6642. q->env = 1;
  6643. }
  6644. if (p->name)
  6645. SOAP_FREE(soap, p->name);
  6646. if (p->value)
  6647. SOAP_FREE(soap, p->value);
  6648. if (p->domain)
  6649. SOAP_FREE(soap, p->domain);
  6650. if (p->path)
  6651. SOAP_FREE(soap, p->path);
  6652. SOAP_FREE(soap, p);
  6653. }
  6654. if (domain)
  6655. SOAP_FREE(soap, domain);
  6656. if (path)
  6657. SOAP_FREE(soap, path);
  6658. }
  6659. /******************************************************************************/
  6660. SOAP_FMAC1
  6661. int
  6662. SOAP_FMAC2
  6663. soap_getenv_cookies(struct soap *soap)
  6664. { struct soap_cookie *p;
  6665. const char *s;
  6666. char key[4096], val[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  6667. if (!(s = getenv("HTTP_COOKIE")))
  6668. return SOAP_ERR;
  6669. do
  6670. { s = soap_decode_key(key, sizeof(key), s);
  6671. s = soap_decode_val(val, sizeof(val), s);
  6672. p = soap_set_cookie(soap, key, val, NULL, NULL);
  6673. if (p)
  6674. p->env = 1;
  6675. } while (*s);
  6676. return SOAP_OK;
  6677. }
  6678. /******************************************************************************/
  6679. SOAP_FMAC1
  6680. struct soap_cookie*
  6681. SOAP_FMAC2
  6682. soap_copy_cookies(struct soap *copy, const struct soap *soap)
  6683. { struct soap_cookie *p, **q, *r;
  6684. q = &r;
  6685. for (p = soap->cookies; p; p = p->next)
  6686. { if (!(*q = (struct soap_cookie*)SOAP_MALLOC(copy, sizeof(struct soap_cookie))))
  6687. return r;
  6688. **q = *p;
  6689. if (p->name)
  6690. { if (((*q)->name = (char*)SOAP_MALLOC(copy, strlen(p->name)+1)))
  6691. strcpy((*q)->name, p->name);
  6692. }
  6693. if (p->value)
  6694. { if (((*q)->value = (char*)SOAP_MALLOC(copy, strlen(p->value)+1)))
  6695. strcpy((*q)->value, p->value);
  6696. }
  6697. if (p->domain)
  6698. { if (((*q)->domain = (char*)SOAP_MALLOC(copy, strlen(p->domain)+1)))
  6699. strcpy((*q)->domain, p->domain);
  6700. }
  6701. if (p->path)
  6702. { if (((*q)->path = (char*)SOAP_MALLOC(copy, strlen(p->path)+1)))
  6703. strcpy((*q)->path, p->path);
  6704. }
  6705. q = &(*q)->next;
  6706. }
  6707. *q = NULL;
  6708. return r;
  6709. }
  6710. /******************************************************************************/
  6711. SOAP_FMAC1
  6712. void
  6713. SOAP_FMAC2
  6714. soap_free_cookies(struct soap *soap)
  6715. { struct soap_cookie *p;
  6716. for (p = soap->cookies; p; p = soap->cookies)
  6717. { soap->cookies = p->next;
  6718. SOAP_FREE(soap, p->name);
  6719. if (p->value)
  6720. SOAP_FREE(soap, p->value);
  6721. if (p->domain)
  6722. SOAP_FREE(soap, p->domain);
  6723. if (p->path)
  6724. SOAP_FREE(soap, p->path);
  6725. SOAP_FREE(soap, p);
  6726. }
  6727. }
  6728. /******************************************************************************/
  6729. #endif /* WITH_COOKIES */
  6730. /******************************************************************************/
  6731. #ifndef WITH_NOIDREF
  6732. #ifndef PALM_2
  6733. SOAP_FMAC1
  6734. size_t
  6735. SOAP_FMAC2
  6736. soap_hash(register const char *s)
  6737. { register size_t h = 0;
  6738. while (*s)
  6739. h = 65599*h + *s++;
  6740. return h % SOAP_IDHASH;
  6741. }
  6742. #endif
  6743. #endif
  6744. /******************************************************************************/
  6745. #ifndef WITH_NOIDREF
  6746. #ifndef PALM_1
  6747. static void
  6748. soap_init_pht(struct soap *soap)
  6749. { register int i;
  6750. soap->pblk = NULL;
  6751. soap->pidx = 0;
  6752. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  6753. soap->pht[i] = NULL;
  6754. }
  6755. #endif
  6756. #endif
  6757. /******************************************************************************/
  6758. #ifndef PALM_1
  6759. SOAP_FMAC1
  6760. struct soap*
  6761. SOAP_FMAC2
  6762. soap_versioning(soap_new)(soap_mode imode, soap_mode omode)
  6763. { struct soap *soap = (struct soap*)malloc(sizeof(struct soap));
  6764. if (soap)
  6765. soap_versioning(soap_init)(soap, imode, omode);
  6766. return soap;
  6767. }
  6768. #endif
  6769. /******************************************************************************/
  6770. #ifndef PALM_1
  6771. SOAP_FMAC1
  6772. void
  6773. SOAP_FMAC2
  6774. soap_free(struct soap *soap)
  6775. { soap_done(soap);
  6776. free(soap);
  6777. }
  6778. #endif
  6779. /******************************************************************************/
  6780. #ifndef PALM_1
  6781. SOAP_FMAC1
  6782. void
  6783. SOAP_FMAC2
  6784. soap_del(struct soap *soap)
  6785. { free(soap);
  6786. }
  6787. #endif
  6788. /******************************************************************************/
  6789. #ifndef WITH_NOIDREF
  6790. #ifndef PALM_1
  6791. static void
  6792. soap_free_pht(struct soap *soap)
  6793. { register struct soap_pblk *pb, *next;
  6794. register int i;
  6795. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free pointer hashtable\n"));
  6796. for (pb = soap->pblk; pb; pb = next)
  6797. { next = pb->next;
  6798. SOAP_FREE(soap, pb);
  6799. }
  6800. soap->pblk = NULL;
  6801. soap->pidx = 0;
  6802. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  6803. soap->pht[i] = NULL;
  6804. }
  6805. #endif
  6806. #endif
  6807. /******************************************************************************/
  6808. #ifndef WITH_NOIDREF
  6809. #ifndef PALM_2
  6810. SOAP_FMAC1
  6811. int
  6812. SOAP_FMAC2
  6813. soap_embed(struct soap *soap, const void *p, const struct soap_array *a, int n, const char *tag, int type)
  6814. { register int i;
  6815. struct soap_plist *pp;
  6816. (void)soap;
  6817. if (soap->version == 2)
  6818. soap->encoding = 1;
  6819. if (a)
  6820. i = soap_array_pointer_lookup(soap, p, a, n, type, &pp);
  6821. else
  6822. i = soap_pointer_lookup(soap, p, type, &pp);
  6823. if (i)
  6824. { if (soap_is_embedded(soap, pp)
  6825. || soap_is_single(soap, pp))
  6826. return 0;
  6827. soap_set_embedded(soap, pp);
  6828. }
  6829. return i;
  6830. }
  6831. #endif
  6832. #endif
  6833. /******************************************************************************/
  6834. #ifndef WITH_NOIDREF
  6835. #ifndef PALM_2
  6836. SOAP_FMAC1
  6837. int
  6838. SOAP_FMAC2
  6839. soap_pointer_lookup(struct soap *soap, const void *p, int type, struct soap_plist **ppp)
  6840. { register struct soap_plist *pp;
  6841. *ppp = NULL;
  6842. if (p)
  6843. { for (pp = soap->pht[soap_hash_ptr(p)]; pp; pp = pp->next)
  6844. { if (pp->ptr == p && pp->type == type)
  6845. { *ppp = pp;
  6846. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d id=%d\n", p, type, pp->id));
  6847. return pp->id;
  6848. }
  6849. }
  6850. }
  6851. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d: not found\n", p, type));
  6852. return 0;
  6853. }
  6854. #endif
  6855. #endif
  6856. /******************************************************************************/
  6857. #ifndef WITH_NOIDREF
  6858. #ifndef PALM_2
  6859. SOAP_FMAC1
  6860. int
  6861. SOAP_FMAC2
  6862. soap_pointer_enter(struct soap *soap, const void *p, const struct soap_array *a, int n, int type, struct soap_plist **ppp)
  6863. { register size_t h;
  6864. register struct soap_plist *pp;
  6865. (void)n;
  6866. if (!soap->pblk || soap->pidx >= SOAP_PTRBLK)
  6867. { register struct soap_pblk *pb = (struct soap_pblk*)SOAP_MALLOC(soap, sizeof(struct soap_pblk));
  6868. if (!pb)
  6869. { soap->error = SOAP_EOM;
  6870. return 0;
  6871. }
  6872. pb->next = soap->pblk;
  6873. soap->pblk = pb;
  6874. soap->pidx = 0;
  6875. }
  6876. *ppp = pp = &soap->pblk->plist[soap->pidx++];
  6877. if (a)
  6878. h = soap_hash_ptr(a->__ptr);
  6879. else
  6880. h = soap_hash_ptr(p);
  6881. 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));
  6882. pp->next = soap->pht[h];
  6883. pp->type = type;
  6884. pp->mark1 = 0;
  6885. pp->mark2 = 0;
  6886. pp->ptr = p;
  6887. pp->array = a;
  6888. soap->pht[h] = pp;
  6889. pp->id = ++soap->idnum;
  6890. return pp->id;
  6891. }
  6892. #endif
  6893. #endif
  6894. /******************************************************************************/
  6895. #ifndef WITH_NOIDREF
  6896. #ifndef PALM_2
  6897. SOAP_FMAC1
  6898. int
  6899. SOAP_FMAC2
  6900. soap_array_pointer_lookup(struct soap *soap, const void *p, const struct soap_array *a, int n, int type, struct soap_plist **ppp)
  6901. { register struct soap_plist *pp;
  6902. *ppp = NULL;
  6903. if (!p || !a->__ptr)
  6904. return 0;
  6905. for (pp = soap->pht[soap_hash_ptr(a->__ptr)]; pp; pp = pp->next)
  6906. { if (pp->type == type && pp->array && pp->array->__ptr == a->__ptr)
  6907. { register int i;
  6908. for (i = 0; i < n; i++)
  6909. if (((const int*)&pp->array->__size)[i] != ((const int*)&a->__size)[i])
  6910. break;
  6911. if (i == n)
  6912. { *ppp = pp;
  6913. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d id=%d\n", a->__ptr, type, pp->id));
  6914. return pp->id;
  6915. }
  6916. }
  6917. }
  6918. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d: not found\n", a->__ptr, type));
  6919. return 0;
  6920. }
  6921. #endif
  6922. #endif
  6923. /******************************************************************************/
  6924. #ifndef PALM_1
  6925. SOAP_FMAC1
  6926. int
  6927. SOAP_FMAC2
  6928. soap_begin_count(struct soap *soap)
  6929. { soap_free_ns(soap);
  6930. #ifndef WITH_LEANER
  6931. if ((soap->mode & SOAP_ENC_DIME) || (soap->omode & SOAP_ENC_DIME))
  6932. soap->mode = soap->omode | SOAP_IO_LENGTH | SOAP_ENC_DIME;
  6933. else
  6934. #endif
  6935. { soap->mode = soap->omode;
  6936. if ((soap->mode & SOAP_IO_UDP))
  6937. soap->mode |= SOAP_ENC_XML;
  6938. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE
  6939. || (((soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_ENC_XML))
  6940. #ifndef WITH_LEANER
  6941. && !soap->fpreparesend
  6942. #endif
  6943. ))
  6944. soap->mode &= ~SOAP_IO_LENGTH;
  6945. else
  6946. soap->mode |= SOAP_IO_LENGTH;
  6947. }
  6948. #ifdef WITH_ZLIB
  6949. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  6950. { if (!(soap->mode & SOAP_ENC_DIME))
  6951. soap->mode &= ~SOAP_IO_LENGTH;
  6952. if (soap->mode & SOAP_ENC_XML)
  6953. soap->mode |= SOAP_IO_BUFFER;
  6954. else
  6955. soap->mode |= SOAP_IO_STORE;
  6956. }
  6957. #endif
  6958. #ifndef WITH_LEANER
  6959. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME))
  6960. soap->mode |= SOAP_ENC_MIME;
  6961. else if (!(soap->mode & SOAP_ENC_MIME))
  6962. soap->mode &= ~SOAP_ENC_MTOM;
  6963. if (soap->mode & SOAP_ENC_MIME)
  6964. soap_select_mime_boundary(soap);
  6965. soap->dime.list = soap->dime.last; /* keep track of last DIME attachment */
  6966. #endif
  6967. soap->count = 0;
  6968. soap->ns = 0;
  6969. soap->null = 0;
  6970. soap->position = 0;
  6971. soap->mustUnderstand = 0;
  6972. soap->encoding = 0;
  6973. soap->part = SOAP_BEGIN;
  6974. soap->event = 0;
  6975. soap->evlev = 0;
  6976. soap->idnum = 0;
  6977. soap_clr_attr(soap);
  6978. soap_set_local_namespaces(soap);
  6979. #ifndef WITH_LEANER
  6980. soap->dime.count = 0; /* count # of attachments */
  6981. soap->dime.size = 0; /* accumulate total size of attachments */
  6982. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) != SOAP_IO_STORE && (soap->error = soap->fprepareinitsend(soap)))
  6983. return soap->error;
  6984. #endif
  6985. 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));
  6986. return SOAP_OK;
  6987. }
  6988. #endif
  6989. /******************************************************************************/
  6990. #ifndef PALM_1
  6991. SOAP_FMAC1
  6992. int
  6993. SOAP_FMAC2
  6994. soap_end_count(struct soap *soap)
  6995. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of count phase\n"));
  6996. #ifndef WITH_LEANER
  6997. if ((soap->mode & SOAP_IO_LENGTH))
  6998. { if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)))
  6999. return soap->error;
  7000. }
  7001. #endif
  7002. return SOAP_OK;
  7003. }
  7004. #endif
  7005. /******************************************************************************/
  7006. #ifndef PALM_1
  7007. SOAP_FMAC1
  7008. int
  7009. SOAP_FMAC2
  7010. soap_begin_send(struct soap *soap)
  7011. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Initializing for output to socket=%d/fd=%d\n", soap->socket, soap->sendfd));
  7012. soap_free_ns(soap);
  7013. soap->error = SOAP_OK;
  7014. soap->mode = soap->omode | (soap->mode & (SOAP_IO_LENGTH | SOAP_ENC_DIME));
  7015. #ifdef WITH_ZLIB
  7016. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  7017. { if (soap->mode & SOAP_ENC_XML)
  7018. soap->mode |= SOAP_IO_BUFFER;
  7019. else
  7020. soap->mode |= SOAP_IO_STORE;
  7021. }
  7022. #endif
  7023. #ifndef WITH_LEAN
  7024. if ((soap->mode & SOAP_IO_UDP))
  7025. { soap->mode |= SOAP_ENC_XML;
  7026. if (soap->count > SOAP_BUFLEN)
  7027. return soap->error = SOAP_UDP_ERROR;
  7028. }
  7029. #endif
  7030. if ((soap->mode & SOAP_IO) == SOAP_IO_FLUSH && soap_valid_socket(soap->socket))
  7031. { if (soap->count || (soap->mode & SOAP_IO_LENGTH) || (soap->mode & SOAP_ENC_XML))
  7032. soap->mode |= SOAP_IO_BUFFER;
  7033. else
  7034. soap->mode |= SOAP_IO_STORE;
  7035. }
  7036. soap->mode &= ~SOAP_IO_LENGTH;
  7037. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE)
  7038. if (soap_new_block(soap) == NULL)
  7039. return soap->error;
  7040. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  7041. soap->keep_alive = 0;
  7042. #ifndef WITH_LEANER
  7043. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME))
  7044. { soap->mode |= SOAP_ENC_MIME;
  7045. soap->mode &= ~SOAP_ENC_DIME;
  7046. }
  7047. else if (!(soap->mode & SOAP_ENC_MIME))
  7048. soap->mode &= ~SOAP_ENC_MTOM;
  7049. if (soap->mode & SOAP_ENC_MIME)
  7050. soap_select_mime_boundary(soap);
  7051. #ifdef WIN32
  7052. #ifndef UNDER_CE
  7053. #ifndef WITH_FASTCGI
  7054. if (!soap_valid_socket(soap->socket) && !soap->os) /* Set win32 stdout or soap->sendfd to BINARY, e.g. to support DIME */
  7055. #ifdef __BORLANDC__
  7056. setmode(soap->sendfd, _O_BINARY);
  7057. #else
  7058. _setmode(soap->sendfd, _O_BINARY);
  7059. #endif
  7060. #endif
  7061. #endif
  7062. #endif
  7063. #endif
  7064. if (soap->mode & SOAP_IO)
  7065. { soap->bufidx = 0;
  7066. soap->buflen = 0;
  7067. }
  7068. soap->chunksize = 0;
  7069. soap->ns = 0;
  7070. soap->null = 0;
  7071. soap->position = 0;
  7072. soap->mustUnderstand = 0;
  7073. soap->encoding = 0;
  7074. soap->idnum = 0;
  7075. soap->level = 0;
  7076. soap_clr_attr(soap);
  7077. soap_set_local_namespaces(soap);
  7078. #ifdef WITH_ZLIB
  7079. soap->z_ratio_out = 1.0;
  7080. if ((soap->mode & SOAP_ENC_ZLIB) && soap->zlib_state != SOAP_ZLIB_DEFLATE)
  7081. { if (!soap->z_buf)
  7082. soap->z_buf = (char*)SOAP_MALLOC(soap, SOAP_BUFLEN);
  7083. soap->d_stream->next_out = (Byte*)soap->z_buf;
  7084. soap->d_stream->avail_out = SOAP_BUFLEN;
  7085. #ifdef WITH_GZIP
  7086. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  7087. { memcpy(soap->z_buf, "\37\213\10\0\0\0\0\0\0\377", 10);
  7088. soap->d_stream->next_out = (Byte*)soap->z_buf + 10;
  7089. soap->d_stream->avail_out = SOAP_BUFLEN - 10;
  7090. soap->z_crc = crc32(0L, NULL, 0);
  7091. soap->zlib_out = SOAP_ZLIB_GZIP;
  7092. if (soap->z_dict)
  7093. *((Byte*)soap->z_buf + 2) = 0xff;
  7094. if (deflateInit2(soap->d_stream, soap->z_level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)
  7095. return soap->error = SOAP_ZLIB_ERROR;
  7096. }
  7097. else
  7098. #endif
  7099. if (deflateInit(soap->d_stream, soap->z_level) != Z_OK)
  7100. return soap->error = SOAP_ZLIB_ERROR;
  7101. if (soap->z_dict)
  7102. { if (deflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  7103. return soap->error = SOAP_ZLIB_ERROR;
  7104. }
  7105. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflate initialized\n"));
  7106. soap->zlib_state = SOAP_ZLIB_DEFLATE;
  7107. }
  7108. #endif
  7109. #ifdef WITH_OPENSSL
  7110. if (soap->ssl)
  7111. ERR_clear_error();
  7112. #endif
  7113. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin send phase (socket=%d mode=0x%x count=%lu)\n", soap->socket, soap->mode, (unsigned long)soap->count));
  7114. soap->part = SOAP_BEGIN;
  7115. #ifndef WITH_LEANER
  7116. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) == SOAP_IO_STORE && (soap->error = soap->fprepareinitsend(soap)))
  7117. return soap->error;
  7118. #endif
  7119. return SOAP_OK;
  7120. }
  7121. #endif
  7122. /******************************************************************************/
  7123. #ifndef WITH_NOIDREF
  7124. #ifndef PALM_2
  7125. SOAP_FMAC1
  7126. void
  7127. SOAP_FMAC2
  7128. soap_embedded(struct soap *soap, const void *p, int t)
  7129. { struct soap_plist *pp;
  7130. if (soap_pointer_lookup(soap, p, t, &pp))
  7131. { pp->mark1 = 1;
  7132. pp->mark2 = 1;
  7133. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded %p type=%d mark set to 1\n", p, t));
  7134. }
  7135. }
  7136. #endif
  7137. #endif
  7138. /******************************************************************************/
  7139. #ifndef WITH_NOIDREF
  7140. #ifndef PALM_2
  7141. SOAP_FMAC1
  7142. int
  7143. SOAP_FMAC2
  7144. soap_reference(struct soap *soap, const void *p, int t)
  7145. { struct soap_plist *pp;
  7146. if (!p || (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME|SOAP_ENC_MIME|SOAP_ENC_MTOM|SOAP_XML_GRAPH))) || (soap->omode & SOAP_XML_TREE))
  7147. return 1;
  7148. if (soap_pointer_lookup(soap, p, t, &pp))
  7149. { if (pp->mark1 == 0)
  7150. { pp->mark1 = 2;
  7151. pp->mark2 = 2;
  7152. }
  7153. }
  7154. else if (!soap_pointer_enter(soap, p, NULL, 0, t, &pp))
  7155. return 1;
  7156. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reference %p type=%d (%d %d)\n", p, t, (int)pp->mark1, (int)pp->mark2));
  7157. return pp->mark1;
  7158. }
  7159. #endif
  7160. #endif
  7161. /******************************************************************************/
  7162. #ifndef WITH_NOIDREF
  7163. #ifndef PALM_2
  7164. SOAP_FMAC1
  7165. int
  7166. SOAP_FMAC2
  7167. soap_array_reference(struct soap *soap, const void *p, const struct soap_array *a, int n, int t)
  7168. { struct soap_plist *pp;
  7169. 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))
  7170. return 1;
  7171. if (soap_array_pointer_lookup(soap, p, a, n, t, &pp))
  7172. { if (pp->mark1 == 0)
  7173. { pp->mark1 = 2;
  7174. pp->mark2 = 2;
  7175. }
  7176. }
  7177. else if (!soap_pointer_enter(soap, p, a, n, t, &pp))
  7178. return 1;
  7179. 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));
  7180. return pp->mark1;
  7181. }
  7182. #endif
  7183. #endif
  7184. /******************************************************************************/
  7185. #ifndef WITH_NOIDREF
  7186. #ifndef PALM_2
  7187. SOAP_FMAC1
  7188. int
  7189. SOAP_FMAC2
  7190. soap_embedded_id(struct soap *soap, int id, const void *p, int t)
  7191. { struct soap_plist *pp = NULL;
  7192. if (!id || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  7193. return id;
  7194. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id %p type=%d id=%d\n", p, t, id));
  7195. if (soap->version == 1 && soap->part != SOAP_IN_HEADER)
  7196. { if (id < 0)
  7197. { id = soap_pointer_lookup(soap, p, t, &pp);
  7198. if (id)
  7199. { if (soap->mode & SOAP_IO_LENGTH)
  7200. pp->mark1 = 2;
  7201. else
  7202. pp->mark2 = 2;
  7203. 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));
  7204. }
  7205. return -1;
  7206. }
  7207. return id;
  7208. }
  7209. if (id < 0)
  7210. id = soap_pointer_lookup(soap, p, t, &pp);
  7211. else if (id && !soap_pointer_lookup(soap, p, t, &pp))
  7212. return 0;
  7213. if (id && pp)
  7214. { if (soap->mode & SOAP_IO_LENGTH)
  7215. pp->mark1 = 1;
  7216. else
  7217. pp->mark2 = 1;
  7218. 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));
  7219. }
  7220. return id;
  7221. }
  7222. #endif
  7223. #endif
  7224. /******************************************************************************/
  7225. #ifndef WITH_NOIDREF
  7226. #ifndef PALM_2
  7227. SOAP_FMAC1
  7228. int
  7229. SOAP_FMAC2
  7230. soap_is_embedded(struct soap *soap, struct soap_plist *pp)
  7231. { if (!pp)
  7232. return 0;
  7233. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Is embedded? %d %d\n", (int)pp->mark1, (int)pp->mark2));
  7234. if (soap->version == 1 && soap->encodingStyle && !(soap->mode & SOAP_XML_GRAPH) && soap->part != SOAP_IN_HEADER)
  7235. { if (soap->mode & SOAP_IO_LENGTH)
  7236. return pp->mark1 != 0;
  7237. return pp->mark2 != 0;
  7238. }
  7239. if (soap->mode & SOAP_IO_LENGTH)
  7240. return pp->mark1 == 1;
  7241. return pp->mark2 == 1;
  7242. }
  7243. #endif
  7244. #endif
  7245. /******************************************************************************/
  7246. #ifndef WITH_NOIDREF
  7247. #ifndef PALM_2
  7248. SOAP_FMAC1
  7249. int
  7250. SOAP_FMAC2
  7251. soap_is_single(struct soap *soap, struct soap_plist *pp)
  7252. { if (soap->part == SOAP_IN_HEADER)
  7253. return 1;
  7254. if (!pp)
  7255. return 0;
  7256. if (soap->mode & SOAP_IO_LENGTH)
  7257. return pp->mark1 == 0;
  7258. return pp->mark2 == 0;
  7259. }
  7260. #endif
  7261. #endif
  7262. /******************************************************************************/
  7263. #ifndef WITH_NOIDREF
  7264. #ifndef PALM_2
  7265. SOAP_FMAC1
  7266. void
  7267. SOAP_FMAC2
  7268. soap_set_embedded(struct soap *soap, struct soap_plist *pp)
  7269. { if (!pp)
  7270. return;
  7271. if (soap->mode & SOAP_IO_LENGTH)
  7272. pp->mark1 = 1;
  7273. else
  7274. pp->mark2 = 1;
  7275. }
  7276. #endif
  7277. #endif
  7278. /******************************************************************************/
  7279. #ifndef WITH_LEANER
  7280. #ifndef PALM_1
  7281. SOAP_FMAC1
  7282. int
  7283. SOAP_FMAC2
  7284. 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)
  7285. {
  7286. #ifndef WITH_NOIDREF
  7287. struct soap_plist *pp;
  7288. int i;
  7289. if (!p || !a->__ptr || (!aid && !atype))
  7290. return soap_element_id(soap, tag, id, p, a, n, type, t);
  7291. 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));
  7292. i = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  7293. if (!i)
  7294. { i = soap_pointer_enter(soap, p, a, n, t, &pp);
  7295. if (!i)
  7296. { soap->error = SOAP_EOM;
  7297. return -1;
  7298. }
  7299. }
  7300. if (id <= 0)
  7301. id = i;
  7302. if (!aid)
  7303. {
  7304. #ifdef HAVE_SNPRINTF
  7305. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), soap->dime_id_format, id);
  7306. #else
  7307. sprintf(soap->tmpbuf, soap->dime_id_format, id);
  7308. #endif
  7309. aid = soap_strdup(soap, soap->tmpbuf);
  7310. }
  7311. /* Add MTOM xop:Include element when necessary */
  7312. /* TODO: this code to be obsoleted with new import/xop.h conventions */
  7313. if ((soap->mode & SOAP_ENC_MTOM) && strcmp(tag, "xop:Include"))
  7314. { if (soap_element_begin_out(soap, tag, 0, type)
  7315. || soap_element_href(soap, "xop:Include", 0, "xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" href", aid)
  7316. || soap_element_end_out(soap, tag))
  7317. return soap->error;
  7318. }
  7319. else if (soap_element_href(soap, tag, 0, "href", aid))
  7320. return soap->error;
  7321. if (soap->mode & SOAP_IO_LENGTH)
  7322. { if (pp->mark1 != 3)
  7323. { struct soap_multipart *content;
  7324. if (soap->mode & SOAP_ENC_MTOM)
  7325. content = soap_new_multipart(soap, &soap->mime.first, &soap->mime.last, (char*)a->__ptr, a->__size);
  7326. else
  7327. content = soap_new_multipart(soap, &soap->dime.first, &soap->dime.last, (char*)a->__ptr, a->__size);
  7328. if (!content)
  7329. { soap->error = SOAP_EOM;
  7330. return -1;
  7331. }
  7332. if (!strncmp(aid, "cid:", 4)) /* RFC 2111 */
  7333. { if (soap->mode & SOAP_ENC_MTOM)
  7334. { char *s = (char*)soap_malloc(soap, strlen(aid) - 1);
  7335. if (s)
  7336. { *s = '<';
  7337. strcpy(s + 1, aid + 4);
  7338. strcat(s, ">");
  7339. content->id = s;
  7340. }
  7341. }
  7342. else
  7343. content->id = aid + 4;
  7344. }
  7345. else
  7346. content->id = aid;
  7347. content->type = atype;
  7348. content->options = aoptions;
  7349. content->encoding = SOAP_MIME_BINARY;
  7350. pp->mark1 = 3;
  7351. }
  7352. }
  7353. else
  7354. pp->mark2 = 3;
  7355. #endif
  7356. return -1;
  7357. }
  7358. #endif
  7359. #endif
  7360. /******************************************************************************/
  7361. #ifndef WITH_NOIDREF
  7362. #ifndef PALM_1
  7363. static void
  7364. soap_init_iht(struct soap *soap)
  7365. { register int i;
  7366. for (i = 0; i < SOAP_IDHASH; i++)
  7367. soap->iht[i] = NULL;
  7368. }
  7369. #endif
  7370. #endif
  7371. /******************************************************************************/
  7372. #ifndef WITH_NOIDREF
  7373. #ifndef PALM_1
  7374. static void
  7375. soap_free_iht(struct soap *soap)
  7376. { register int i;
  7377. register struct soap_ilist *ip = NULL, *p = NULL;
  7378. register struct soap_flist *fp = NULL, *fq = NULL;
  7379. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free ID hashtable\n"));
  7380. for (i = 0; i < SOAP_IDHASH; i++)
  7381. { for (ip = soap->iht[i]; ip; ip = p)
  7382. { for (fp = ip->flist; fp; fp = fq)
  7383. { fq = fp->next;
  7384. SOAP_FREE(soap, fp);
  7385. }
  7386. p = ip->next;
  7387. SOAP_FREE(soap, ip);
  7388. }
  7389. soap->iht[i] = NULL;
  7390. }
  7391. }
  7392. #endif
  7393. #endif
  7394. /******************************************************************************/
  7395. #ifndef WITH_NOIDREF
  7396. #ifndef PALM_2
  7397. SOAP_FMAC1
  7398. struct soap_ilist *
  7399. SOAP_FMAC2
  7400. soap_lookup(struct soap *soap, const char *id)
  7401. { register struct soap_ilist *ip = NULL;
  7402. for (ip = soap->iht[soap_hash(id)]; ip; ip = ip->next)
  7403. if (!strcmp(ip->id, id))
  7404. return ip;
  7405. return NULL;
  7406. }
  7407. #endif
  7408. #endif
  7409. /******************************************************************************/
  7410. #ifndef WITH_NOIDREF
  7411. #ifndef PALM_2
  7412. SOAP_FMAC1
  7413. struct soap_ilist *
  7414. SOAP_FMAC2
  7415. soap_enter(struct soap *soap, const char *id)
  7416. { register size_t h;
  7417. register struct soap_ilist *ip;
  7418. ip = (struct soap_ilist*)SOAP_MALLOC(soap, sizeof(struct soap_ilist) + strlen(id));
  7419. if (ip)
  7420. { strcpy((char*)ip->id, id);
  7421. h = soap_hash(id); /* h = (HASH(id) % SOAP_IDHASH) so soap->iht[h] is safe */
  7422. ip->next = soap->iht[h];
  7423. soap->iht[h] = ip;
  7424. }
  7425. return ip;
  7426. }
  7427. #endif
  7428. #endif
  7429. /******************************************************************************/
  7430. #ifndef PALM_2
  7431. SOAP_FMAC1
  7432. void*
  7433. SOAP_FMAC2
  7434. soap_malloc(struct soap *soap, size_t n)
  7435. { register char *p;
  7436. if (!n)
  7437. return (void*)SOAP_NON_NULL;
  7438. if (!soap)
  7439. return SOAP_MALLOC(soap, n);
  7440. if (soap->fmalloc)
  7441. p = (char*)soap->fmalloc(soap, n);
  7442. else
  7443. { n += sizeof(short);
  7444. n += (-(long)n) & (sizeof(void*)-1); /* align at 4-, 8- or 16-byte boundary */
  7445. if (!(p = (char*)SOAP_MALLOC(soap, n + sizeof(void*) + sizeof(size_t))))
  7446. { soap->error = SOAP_EOM;
  7447. return NULL;
  7448. }
  7449. /* set the canary to detect corruption */
  7450. *(unsigned short*)(p + n - sizeof(unsigned short)) = (unsigned short)SOAP_CANARY;
  7451. /* keep chain of alloced cells for destruction */
  7452. *(void**)(p + n) = soap->alist;
  7453. *(size_t*)(p + n + sizeof(void*)) = n;
  7454. soap->alist = p + n;
  7455. }
  7456. soap->alloced = 1;
  7457. return p;
  7458. }
  7459. #endif
  7460. /******************************************************************************/
  7461. #ifdef SOAP_MEM_DEBUG
  7462. static void
  7463. soap_init_mht(struct soap *soap)
  7464. { register int i;
  7465. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  7466. soap->mht[i] = NULL;
  7467. }
  7468. #endif
  7469. /******************************************************************************/
  7470. #ifdef SOAP_MEM_DEBUG
  7471. static void
  7472. soap_free_mht(struct soap *soap)
  7473. { register int i;
  7474. register struct soap_mlist *mp, *mq;
  7475. for (i = 0; i < (int)SOAP_PTRHASH; i++)
  7476. { for (mp = soap->mht[i]; mp; mp = mq)
  7477. { mq = mp->next;
  7478. if (mp->live)
  7479. fprintf(stderr, "%s(%d): malloc() = %p not freed (memory leak or forgot to call soap_end()?)\n", mp->file, mp->line, mp->ptr);
  7480. free(mp);
  7481. }
  7482. soap->mht[i] = NULL;
  7483. }
  7484. }
  7485. #endif
  7486. /******************************************************************************/
  7487. #ifdef SOAP_MEM_DEBUG
  7488. SOAP_FMAC1
  7489. void*
  7490. SOAP_FMAC2
  7491. soap_track_malloc(struct soap *soap, const char *file, int line, size_t size)
  7492. { register void *p = malloc(size);
  7493. if (soap)
  7494. { register size_t h = soap_hash_ptr(p);
  7495. register struct soap_mlist *mp = (struct soap_mlist*)malloc(sizeof(struct soap_mlist));
  7496. if (soap->fdebug[SOAP_INDEX_TEST])
  7497. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): malloc(%lu) = %p\n", file, line, (unsigned long)size, p));
  7498. }
  7499. mp->next = soap->mht[h];
  7500. mp->ptr = p;
  7501. mp->file = file;
  7502. mp->line = line;
  7503. mp->live = 1;
  7504. soap->mht[h] = mp;
  7505. }
  7506. return p;
  7507. }
  7508. #endif
  7509. /******************************************************************************/
  7510. #ifdef SOAP_MEM_DEBUG
  7511. SOAP_FMAC1
  7512. void
  7513. SOAP_FMAC2
  7514. soap_track_free(struct soap *soap, const char *file, int line, void *p)
  7515. { register size_t h = soap_hash_ptr(p);
  7516. register struct soap_mlist *mp;
  7517. for (mp = soap->mht[h]; mp; mp = mp->next)
  7518. if (mp->ptr == p)
  7519. break;
  7520. if (mp)
  7521. { if (mp->live)
  7522. { free(p);
  7523. if (soap->fdebug[SOAP_INDEX_TEST])
  7524. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): free(%p)\n", file, line, p));
  7525. }
  7526. mp->live = 0;
  7527. }
  7528. else
  7529. fprintf(stderr, "%s(%d): free(%p) double free of pointer malloced at %s(%d)\n", file, line, p, mp->file, mp->line);
  7530. }
  7531. else
  7532. fprintf(stderr, "%s(%d): free(%p) pointer not malloced\n", file, line, p);
  7533. }
  7534. #endif
  7535. /******************************************************************************/
  7536. #ifdef SOAP_MEM_DEBUG
  7537. static void
  7538. soap_track_unlink(struct soap *soap, const void *p)
  7539. { register size_t h = soap_hash_ptr(p);
  7540. register struct soap_mlist *mp;
  7541. for (mp = soap->mht[h]; mp; mp = mp->next)
  7542. if (mp->ptr == p)
  7543. break;
  7544. if (mp)
  7545. mp->live = 0;
  7546. }
  7547. #endif
  7548. /******************************************************************************/
  7549. #ifndef PALM_2
  7550. SOAP_FMAC1
  7551. void
  7552. SOAP_FMAC2
  7553. soap_dealloc(struct soap *soap, void *p)
  7554. { if (soap_check_state(soap))
  7555. return;
  7556. if (p)
  7557. { register char **q;
  7558. for (q = (char**)&soap->alist; *q; q = *(char***)q)
  7559. {
  7560. if (*(unsigned short*)(char*)(*q - sizeof(unsigned short)) != (unsigned short)SOAP_CANARY)
  7561. {
  7562. #ifdef SOAP_MEM_DEBUG
  7563. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  7564. #endif
  7565. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  7566. DBGHEX(TEST, *q - 200, 200);
  7567. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  7568. soap->error = SOAP_MOE;
  7569. return;
  7570. }
  7571. if (p == (void*)(*q - *(size_t*)(*q + sizeof(void*))))
  7572. { *q = **(char***)q;
  7573. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Freed data at %p\n", p));
  7574. SOAP_FREE(soap, p);
  7575. return;
  7576. }
  7577. }
  7578. soap_delete(soap, p);
  7579. }
  7580. else
  7581. { register char *q;
  7582. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free all soap_malloc() data\n"));
  7583. while (soap->alist)
  7584. { q = (char*)soap->alist;
  7585. if (*(unsigned short*)(char*)(q - sizeof(unsigned short)) != (unsigned short)SOAP_CANARY)
  7586. {
  7587. #ifdef SOAP_MEM_DEBUG
  7588. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  7589. #endif
  7590. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  7591. DBGHEX(TEST, q - 200, 200);
  7592. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  7593. soap->error = SOAP_MOE;
  7594. return;
  7595. }
  7596. soap->alist = *(void**)q;
  7597. q -= *(size_t*)(q + sizeof(void*));
  7598. SOAP_FREE(soap, q);
  7599. }
  7600. /* we must assume these were deallocated: */
  7601. soap->http_content = NULL;
  7602. soap->action = NULL;
  7603. soap->fault = NULL;
  7604. soap->header = NULL;
  7605. soap->userid = NULL;
  7606. soap->passwd = NULL;
  7607. soap->authrealm = NULL;
  7608. #ifdef WITH_NTLM
  7609. soap->ntlm_challenge = NULL;
  7610. #endif
  7611. #ifndef WITH_LEANER
  7612. soap_clr_mime(soap);
  7613. #endif
  7614. }
  7615. }
  7616. #endif
  7617. /******************************************************************************/
  7618. #ifndef PALM_2
  7619. SOAP_FMAC1
  7620. void
  7621. SOAP_FMAC2
  7622. soap_delete(struct soap *soap, void *p)
  7623. { register struct soap_clist **cp;
  7624. if (soap_check_state(soap))
  7625. return;
  7626. cp = &soap->clist;
  7627. if (p)
  7628. { while (*cp)
  7629. { if (p == (*cp)->ptr)
  7630. { register struct soap_clist *q = *cp;
  7631. *cp = q->next;
  7632. if (q->fdelete(q))
  7633. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: deletion callback failed for object type %d\n", q->ptr, q->type));
  7634. #ifdef SOAP_MEM_DEBUG
  7635. fprintf(stderr, "new(object type = %d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  7636. #endif
  7637. }
  7638. SOAP_FREE(soap, q);
  7639. return;
  7640. }
  7641. cp = &(*cp)->next;
  7642. }
  7643. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: address not in list\n", p));
  7644. }
  7645. else
  7646. { while (*cp)
  7647. { register struct soap_clist *q = *cp;
  7648. *cp = q->next;
  7649. if (q->fdelete(q))
  7650. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: deletion callback failed for object type %d\n", q->ptr, q->type));
  7651. #ifdef SOAP_MEM_DEBUG
  7652. fprintf(stderr, "new(object type = %d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  7653. #endif
  7654. }
  7655. SOAP_FREE(soap, q);
  7656. }
  7657. }
  7658. soap->fault = NULL; /* this was possibly deallocated */
  7659. soap->header = NULL; /* this was possibly deallocated */
  7660. }
  7661. #endif
  7662. /******************************************************************************/
  7663. #ifndef PALM_2
  7664. SOAP_FMAC1
  7665. void
  7666. SOAP_FMAC2
  7667. soap_delegate_deletion(struct soap *soap, struct soap *soap_to)
  7668. { register struct soap_clist *cp;
  7669. register char **q;
  7670. #ifdef SOAP_MEM_DEBUG
  7671. register void *p;
  7672. register struct soap_mlist **mp, *mq;
  7673. size_t h;
  7674. #endif
  7675. for (q = (char**)&soap->alist; *q; q = *(char***)q)
  7676. {
  7677. if (*(unsigned short*)(char*)(*q - sizeof(unsigned short)) != (unsigned short)SOAP_CANARY)
  7678. {
  7679. #ifdef SOAP_MEM_DEBUG
  7680. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  7681. #endif
  7682. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  7683. DBGHEX(TEST, *q - 200, 200);
  7684. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  7685. soap->error = SOAP_MOE;
  7686. return;
  7687. }
  7688. #ifdef SOAP_MEM_DEBUG
  7689. p = (void*)(*q - *(size_t*)(*q + sizeof(void*)));
  7690. h = soap_hash_ptr(p);
  7691. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next)
  7692. { if ((*mp)->ptr == p)
  7693. { mq = *mp;
  7694. *mp = mq->next;
  7695. mq->next = soap_to->mht[h];
  7696. soap_to->mht[h] = mq;
  7697. break;
  7698. }
  7699. }
  7700. #endif
  7701. }
  7702. *q = (char*)soap_to->alist;
  7703. soap_to->alist = soap->alist;
  7704. soap->alist = NULL;
  7705. #ifdef SOAP_MEM_DEBUG
  7706. cp = soap->clist;
  7707. while (cp)
  7708. { h = soap_hash_ptr(cp);
  7709. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next)
  7710. { if ((*mp)->ptr == cp)
  7711. { mq = *mp;
  7712. *mp = mq->next;
  7713. mq->next = soap_to->mht[h];
  7714. soap_to->mht[h] = mq;
  7715. break;
  7716. }
  7717. }
  7718. cp = cp->next;
  7719. }
  7720. #endif
  7721. cp = soap_to->clist;
  7722. if (cp)
  7723. { while (cp->next)
  7724. cp = cp->next;
  7725. cp->next = soap->clist;
  7726. }
  7727. else
  7728. soap_to->clist = soap->clist;
  7729. soap->clist = NULL;
  7730. }
  7731. #endif
  7732. /******************************************************************************/
  7733. #ifndef PALM_2
  7734. SOAP_FMAC1
  7735. struct soap_clist *
  7736. SOAP_FMAC2
  7737. soap_link(struct soap *soap, void *p, int t, int n, int (*fdelete)(struct soap_clist*))
  7738. { register struct soap_clist *cp;
  7739. if ((cp = (struct soap_clist*)SOAP_MALLOC(soap, sizeof(struct soap_clist))))
  7740. { cp->next = soap->clist;
  7741. cp->type = t;
  7742. cp->size = n;
  7743. cp->ptr = p;
  7744. cp->fdelete = fdelete;
  7745. soap->clist = cp;
  7746. }
  7747. return cp;
  7748. }
  7749. #endif
  7750. /******************************************************************************/
  7751. #ifndef PALM_2
  7752. SOAP_FMAC1
  7753. int
  7754. SOAP_FMAC2
  7755. soap_unlink(struct soap *soap, const void *p)
  7756. { register char **q;
  7757. register struct soap_clist **cp;
  7758. if (soap && p)
  7759. { for (q = (char**)&soap->alist; *q; q = *(char***)q)
  7760. { if (p == (void*)(*q - *(size_t*)(*q + sizeof(void*))))
  7761. { *q = **(char***)q;
  7762. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked data %p\n", p));
  7763. #ifdef SOAP_MEM_DEBUG
  7764. soap_track_unlink(soap, p);
  7765. #endif
  7766. return SOAP_OK; /* found and removed from dealloc chain */
  7767. }
  7768. }
  7769. for (cp = &soap->clist; *cp; cp = &(*cp)->next)
  7770. { if (p == (*cp)->ptr)
  7771. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked class instance %p\n", p));
  7772. q = (char**)*cp;
  7773. *cp = (*cp)->next;
  7774. SOAP_FREE(soap, q);
  7775. return SOAP_OK; /* found and removed from dealloc chain */
  7776. }
  7777. }
  7778. }
  7779. return SOAP_ERR;
  7780. }
  7781. #endif
  7782. /******************************************************************************/
  7783. #ifndef WITH_NOIDREF
  7784. #ifndef PALM_2
  7785. SOAP_FMAC1
  7786. int
  7787. SOAP_FMAC2
  7788. soap_lookup_type(struct soap *soap, const char *id)
  7789. { register struct soap_ilist *ip;
  7790. if (id && *id)
  7791. { ip = soap_lookup(soap, id);
  7792. if (ip)
  7793. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup id='%s' type=%d\n", id, ip->type));
  7794. return ip->type;
  7795. }
  7796. }
  7797. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "lookup type id='%s' NOT FOUND! Need to get it from xsi:type\n", id));
  7798. return 0;
  7799. }
  7800. #endif
  7801. #endif
  7802. /******************************************************************************/
  7803. #ifndef WITH_NOIDREF
  7804. #ifndef PALM_2
  7805. SOAP_FMAC1
  7806. void*
  7807. SOAP_FMAC2
  7808. soap_id_lookup(struct soap *soap, const char *id, void **p, int t, size_t n, unsigned int k)
  7809. { struct soap_ilist *ip;
  7810. void **q;
  7811. if (!p || !id || !*id)
  7812. return p;
  7813. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  7814. if (!ip)
  7815. { if (!(ip = soap_enter(soap, id))) /* new hash table entry for string id */
  7816. return NULL;
  7817. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding first href='%s' type=%d %p (%u bytes)\n", id, t, p, (unsigned int)n));
  7818. ip->type = t;
  7819. ip->size = n;
  7820. ip->link = p;
  7821. ip->copy = NULL;
  7822. ip->flist = NULL;
  7823. ip->ptr = NULL;
  7824. ip->level = k;
  7825. *p = NULL;
  7826. }
  7827. else if (ip->ptr)
  7828. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolved href='%s' type=%d location=%p (%u bytes)\n", id, t, ip->ptr, (unsigned int)n));
  7829. if (ip->type != t)
  7830. { strcpy(soap->id, id);
  7831. soap->error = SOAP_HREF;
  7832. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Type incompatibility: href='%s' id-type=%d href-type=%d\n", id, ip->type, t));
  7833. return NULL;
  7834. }
  7835. while (ip->level < k)
  7836. { q = (void**)soap_malloc(soap, sizeof(void*));
  7837. if (!q)
  7838. return NULL;
  7839. *p = (void*)q;
  7840. p = q;
  7841. k--;
  7842. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level...\n"));
  7843. }
  7844. *p = ip->ptr;
  7845. }
  7846. else if (ip->level > k)
  7847. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving level %u pointers to href='%s'\n", ip->level, id));
  7848. while (ip->level > k)
  7849. { void *s, **r = &ip->link;
  7850. q = (void**)ip->link;
  7851. while (q)
  7852. { *r = (void*)soap_malloc(soap, sizeof(void*));
  7853. if (!*r)
  7854. return NULL;
  7855. s = *q;
  7856. *q = *r;
  7857. r = (void**)*r;
  7858. q = (void**)s;
  7859. }
  7860. *r = NULL;
  7861. ip->size = n;
  7862. ip->copy = NULL;
  7863. ip->level = ip->level - 1;
  7864. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level...\n"));
  7865. }
  7866. q = (void**)ip->link;
  7867. ip->link = p;
  7868. *p = (void*)q;
  7869. }
  7870. else
  7871. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarded href='%s' type=%d location=%p (%u bytes)\n", id, t, p, (unsigned int)n));
  7872. while (ip->level < k)
  7873. { q = (void**)soap_malloc(soap, sizeof(void*));
  7874. if (!q)
  7875. return NULL;
  7876. *p = q;
  7877. p = q;
  7878. k--;
  7879. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Descending one level...\n"));
  7880. }
  7881. q = (void**)ip->link;
  7882. ip->link = p;
  7883. *p = (void*)q;
  7884. }
  7885. return p;
  7886. }
  7887. #endif
  7888. #endif
  7889. /******************************************************************************/
  7890. #ifndef WITH_NOIDREF
  7891. #ifndef PALM_2
  7892. SOAP_FMAC1
  7893. void*
  7894. SOAP_FMAC2
  7895. 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))
  7896. { struct soap_ilist *ip;
  7897. if (!p || !href || !*href)
  7898. return p;
  7899. ip = soap_lookup(soap, href); /* lookup pointer to hash table entry for string id */
  7900. if (!ip)
  7901. { if (!(ip = soap_enter(soap, href))) /* new hash table entry for string id */
  7902. return NULL;
  7903. ip->type = st;
  7904. ip->size = n;
  7905. ip->link = NULL;
  7906. ip->copy = NULL;
  7907. ip->ptr = NULL;
  7908. ip->level = 0;
  7909. ip->flist = NULL;
  7910. 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));
  7911. }
  7912. else if (ip->type != st || (ip->level == k && ip->size != n))
  7913. { 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));
  7914. strcpy(soap->id, href);
  7915. soap->error = SOAP_HREF;
  7916. return NULL;
  7917. }
  7918. if (fcopy || n < sizeof(void*) || *href != '#')
  7919. { register struct soap_flist *fp = (struct soap_flist*)SOAP_MALLOC(soap, sizeof(struct soap_flist));
  7920. if (!fp)
  7921. { soap->error = SOAP_EOM;
  7922. return NULL;
  7923. }
  7924. fp->next = ip->flist;
  7925. fp->type = tt;
  7926. fp->ptr = p;
  7927. fp->level = k;
  7928. fp->len = len;
  7929. if (fcopy)
  7930. fp->fcopy = fcopy;
  7931. else
  7932. fp->fcopy = soap_fcopy;
  7933. ip->flist = fp;
  7934. 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));
  7935. }
  7936. else
  7937. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding copying address %p for type=%d href='%s'\n", p, st, href));
  7938. *(void**)p = ip->copy;
  7939. ip->copy = p;
  7940. }
  7941. return p;
  7942. }
  7943. #endif
  7944. #endif
  7945. /******************************************************************************/
  7946. #ifndef PALM_2
  7947. SOAP_FMAC1
  7948. void*
  7949. SOAP_FMAC2
  7950. 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*))
  7951. {
  7952. #ifndef WITH_NOIDREF
  7953. struct soap_ilist *ip;
  7954. #endif
  7955. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Enter id='%s' type=%d loc=%p size=%lu level=%u\n", id, t, p, (unsigned long)n, k));
  7956. soap->alloced = 0;
  7957. if (!p)
  7958. { if (finstantiate)
  7959. p = finstantiate(soap, t, type, arrayType, &n);
  7960. else
  7961. p = soap_malloc(soap, n);
  7962. if (p)
  7963. soap->alloced = 1;
  7964. }
  7965. #ifndef WITH_NOIDREF
  7966. if (!id || !*id)
  7967. #endif
  7968. return p;
  7969. #ifndef WITH_NOIDREF
  7970. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  7971. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Lookup entry id='%s for location=%p'\n", id, p));
  7972. if (!ip)
  7973. { if (!(ip = soap_enter(soap, id))) /* new hash table entry for string id */
  7974. return NULL;
  7975. ip->type = t;
  7976. ip->link = NULL;
  7977. ip->copy = NULL;
  7978. ip->flist = NULL;
  7979. ip->size = n;
  7980. ip->ptr = p;
  7981. ip->level = k;
  7982. 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));
  7983. }
  7984. else if ((ip->type != t || (ip->level == k && ip->size != n)) && (ip->copy || ip->flist))
  7985. { 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));
  7986. strcpy(soap->id, id);
  7987. soap->error = SOAP_HREF;
  7988. return NULL;
  7989. }
  7990. else if (ip->ptr)
  7991. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Multiply defined id='%s'\n", id));
  7992. strcpy(soap->id, id);
  7993. soap->error = SOAP_DUPLICATE_ID;
  7994. return NULL;
  7995. }
  7996. else
  7997. { ip->size = n;
  7998. ip->ptr = p;
  7999. ip->level = k;
  8000. 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));
  8001. }
  8002. return ip->ptr;
  8003. #endif
  8004. }
  8005. #endif
  8006. /******************************************************************************/
  8007. #ifndef PALM_2
  8008. SOAP_FMAC1
  8009. void
  8010. SOAP_FMAC2
  8011. soap_fcopy(struct soap *soap, int st, int tt, void *p, size_t len, const void *q, size_t n)
  8012. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Copying data type=%d (target type=%d) %p -> %p (%lu bytes)\n", st, tt, q, p, (unsigned long)n));
  8013. memcpy(p, q, n);
  8014. (void)soap; (void)st; (void)tt; (void)len;
  8015. }
  8016. #endif
  8017. /******************************************************************************/
  8018. #ifndef PALM_1
  8019. SOAP_FMAC1
  8020. int
  8021. SOAP_FMAC2
  8022. soap_end_send(struct soap *soap)
  8023. {
  8024. #ifndef WITH_LEANER
  8025. int err;
  8026. if (soap->dime.list)
  8027. { /* SOAP body referenced attachments must appear first */
  8028. soap->dime.last->next = soap->dime.first;
  8029. soap->dime.first = soap->dime.list->next;
  8030. soap->dime.list->next = NULL;
  8031. soap->dime.last = soap->dime.list;
  8032. }
  8033. if (!(err = soap_putdime(soap)))
  8034. err = soap_putmime(soap);
  8035. soap->mime.list = NULL;
  8036. soap->mime.first = NULL;
  8037. soap->mime.last = NULL;
  8038. soap->dime.list = NULL;
  8039. soap->dime.first = NULL;
  8040. soap->dime.last = NULL;
  8041. if (err)
  8042. return err;
  8043. #endif
  8044. return soap_end_send_flush(soap);
  8045. }
  8046. #endif
  8047. /******************************************************************************/
  8048. #ifndef PALM_1
  8049. SOAP_FMAC1
  8050. int
  8051. SOAP_FMAC2
  8052. soap_end_send_flush(struct soap *soap)
  8053. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End send mode=0x%x\n", soap->mode));
  8054. if (soap->mode & SOAP_IO) /* need to flush the remaining data in buffer */
  8055. { if (soap_flush(soap))
  8056. #ifdef WITH_ZLIB
  8057. { if (soap->mode & SOAP_ENC_ZLIB && soap->zlib_state == SOAP_ZLIB_DEFLATE)
  8058. { soap->zlib_state = SOAP_ZLIB_NONE;
  8059. deflateEnd(soap->d_stream);
  8060. }
  8061. return soap->error;
  8062. }
  8063. #else
  8064. return soap->error;
  8065. #endif
  8066. #ifdef WITH_ZLIB
  8067. if (soap->mode & SOAP_ENC_ZLIB)
  8068. { int r;
  8069. soap->d_stream->avail_in = 0;
  8070. do
  8071. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflating remainder\n"));
  8072. r = deflate(soap->d_stream, Z_FINISH);
  8073. if (soap->d_stream->avail_out != SOAP_BUFLEN)
  8074. { if (soap_flush_raw(soap, soap->z_buf, SOAP_BUFLEN - soap->d_stream->avail_out))
  8075. { soap->zlib_state = SOAP_ZLIB_NONE;
  8076. deflateEnd(soap->d_stream);
  8077. return soap->error;
  8078. }
  8079. soap->d_stream->next_out = (Byte*)soap->z_buf;
  8080. soap->d_stream->avail_out = SOAP_BUFLEN;
  8081. }
  8082. } while (r == Z_OK);
  8083. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflated total %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  8084. soap->z_ratio_out = (float)soap->d_stream->total_out / (float)soap->d_stream->total_in;
  8085. soap->mode &= ~SOAP_ENC_ZLIB;
  8086. soap->zlib_state = SOAP_ZLIB_NONE;
  8087. if (deflateEnd(soap->d_stream) != Z_OK || r != Z_STREAM_END)
  8088. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to end deflate: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  8089. return soap->error = SOAP_ZLIB_ERROR;
  8090. }
  8091. #ifdef WITH_GZIP
  8092. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  8093. { soap->z_buf[0] = soap->z_crc & 0xFF;
  8094. soap->z_buf[1] = (soap->z_crc >> 8) & 0xFF;
  8095. soap->z_buf[2] = (soap->z_crc >> 16) & 0xFF;
  8096. soap->z_buf[3] = (soap->z_crc >> 24) & 0xFF;
  8097. soap->z_buf[4] = soap->d_stream->total_in & 0xFF;
  8098. soap->z_buf[5] = (soap->d_stream->total_in >> 8) & 0xFF;
  8099. soap->z_buf[6] = (soap->d_stream->total_in >> 16) & 0xFF;
  8100. soap->z_buf[7] = (soap->d_stream->total_in >> 24) & 0xFF;
  8101. if (soap_flush_raw(soap, soap->z_buf, 8))
  8102. return soap->error;
  8103. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip crc32=%lu\n", (unsigned long)soap->z_crc));
  8104. }
  8105. #endif
  8106. }
  8107. #endif
  8108. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE)
  8109. { char *p;
  8110. #ifndef WITH_NOHTTP
  8111. if (!(soap->mode & SOAP_ENC_XML))
  8112. { soap->mode--;
  8113. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending buffered message of length %u\n", (unsigned int)soap->blist->size));
  8114. if (soap->status >= SOAP_POST)
  8115. soap->error = soap->fpost(soap, soap->endpoint, soap->host, soap->port, soap->path, soap->action, soap->blist->size);
  8116. else if (soap->status != SOAP_STOP)
  8117. soap->error = soap->fresponse(soap, soap->status, soap->blist->size);
  8118. if (soap->error || soap_flush(soap))
  8119. return soap->error;
  8120. soap->mode++;
  8121. }
  8122. #endif
  8123. for (p = soap_first_block(soap, NULL); p; p = soap_next_block(soap, NULL))
  8124. { DBGMSG(SENT, p, soap_block_size(soap, NULL));
  8125. 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));
  8126. if ((soap->error = soap->fsend(soap, p, soap_block_size(soap, NULL))))
  8127. { soap_end_block(soap, NULL);
  8128. return soap->error;
  8129. }
  8130. }
  8131. soap_end_block(soap, NULL);
  8132. if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)))
  8133. return soap->error;
  8134. }
  8135. #ifndef WITH_LEANER
  8136. else if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  8137. { DBGMSG(SENT, "\r\n0\r\n\r\n", 7);
  8138. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send 7 bytes to socket=%d/fd=%d\n", soap->socket, soap->sendfd));
  8139. if ((soap->error = soap->fsend(soap, "\r\n0\r\n\r\n", 7)))
  8140. return soap->error;
  8141. }
  8142. #endif
  8143. }
  8144. #ifdef WITH_TCPFIN
  8145. #ifdef WITH_OPENSSL
  8146. if (!soap->ssl && soap_valid_socket(soap->socket) && !soap->keep_alive && !(soap->omode & SOAP_IO_UDP))
  8147. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR); /* Send TCP FIN */
  8148. #else
  8149. if (soap_valid_socket(soap->socket) && !soap->keep_alive && !(soap->omode & SOAP_IO_UDP))
  8150. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR); /* Send TCP FIN */
  8151. #endif
  8152. #endif
  8153. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of send phase\n"));
  8154. soap->omode &= ~SOAP_SEC_WSUID;
  8155. soap->count = 0;
  8156. soap->part = SOAP_END;
  8157. return SOAP_OK;
  8158. }
  8159. #endif
  8160. /******************************************************************************/
  8161. #ifndef PALM_1
  8162. SOAP_FMAC1
  8163. int
  8164. SOAP_FMAC2
  8165. soap_end_recv(struct soap *soap)
  8166. { soap->part = SOAP_END;
  8167. #ifndef WITH_LEAN
  8168. soap->wsuid = NULL; /* reset before next send */
  8169. soap->c14nexclude = NULL; /* reset before next send */
  8170. #endif
  8171. #ifndef WITH_LEANER
  8172. soap->ffilterrecv = NULL;
  8173. if ((soap->mode & SOAP_ENC_DIME) && soap_getdime(soap))
  8174. { soap->dime.first = NULL;
  8175. soap->dime.last = NULL;
  8176. return soap->error;
  8177. }
  8178. soap->dime.list = soap->dime.first;
  8179. soap->dime.first = NULL;
  8180. soap->dime.last = NULL;
  8181. /* Check if MIME attachments and mime-post-check flag is set, if so call soap_resolve() and return */
  8182. if (soap->mode & SOAP_ENC_MIME)
  8183. { if (soap->mode & SOAP_MIME_POSTCHECK)
  8184. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Post checking MIME attachments\n"));
  8185. if (!soap->keep_alive)
  8186. soap->keep_alive = -1;
  8187. #ifndef WITH_NOIDREF
  8188. soap_resolve(soap);
  8189. #endif
  8190. return SOAP_OK;
  8191. }
  8192. if (soap_getmime(soap))
  8193. return soap->error;
  8194. }
  8195. soap->mime.list = soap->mime.first;
  8196. soap->mime.first = NULL;
  8197. soap->mime.last = NULL;
  8198. soap->mime.boundary = NULL;
  8199. if (soap->xlist)
  8200. { struct soap_multipart *content;
  8201. for (content = soap->mime.list; content; content = content->next)
  8202. soap_resolve_attachment(soap, content);
  8203. }
  8204. #endif
  8205. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "End of receive message ok\n"));
  8206. #ifdef WITH_ZLIB
  8207. if (soap->mode & SOAP_ENC_ZLIB)
  8208. { /* Make sure end of compressed content is reached */
  8209. while (soap->d_stream->next_out != Z_NULL)
  8210. if ((int)soap_get1(soap) == EOF)
  8211. break;
  8212. soap->mode &= ~SOAP_ENC_ZLIB;
  8213. memcpy(soap->buf, soap->z_buf, SOAP_BUFLEN);
  8214. soap->bufidx = (char*)soap->d_stream->next_in - soap->z_buf;
  8215. soap->buflen = soap->z_buflen;
  8216. soap->zlib_state = SOAP_ZLIB_NONE;
  8217. if (inflateEnd(soap->d_stream) != Z_OK)
  8218. return soap->error = SOAP_ZLIB_ERROR;
  8219. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Inflate end ok\n"));
  8220. #ifdef WITH_GZIP
  8221. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  8222. { soap_wchar c;
  8223. short i;
  8224. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Inflate gzip crc check\n"));
  8225. for (i = 0; i < 8; i++)
  8226. { if ((int)(c = soap_get1(soap)) == EOF)
  8227. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Gzip error: unable to read crc value\n"));
  8228. return soap->error = SOAP_ZLIB_ERROR;
  8229. }
  8230. soap->z_buf[i] = (char)c;
  8231. }
  8232. 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)))
  8233. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Gzip inflate error: crc check failed, message corrupted? (crc32=%lu)\n", (unsigned long)soap->z_crc));
  8234. return soap->error = SOAP_ZLIB_ERROR;
  8235. }
  8236. 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)))
  8237. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Gzip inflate error: incorrect message length\n"));
  8238. return soap->error = SOAP_ZLIB_ERROR;
  8239. }
  8240. }
  8241. soap->zlib_in = SOAP_ZLIB_NONE;
  8242. #endif
  8243. }
  8244. #endif
  8245. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  8246. while (soap->ahead != EOF && !soap_recv_raw(soap))
  8247. ;
  8248. #ifndef WITH_NOIDREF
  8249. if (soap_resolve(soap))
  8250. return soap->error;
  8251. #endif
  8252. #ifndef WITH_LEANER
  8253. if (soap->xlist)
  8254. { if (soap->mode & SOAP_ENC_MTOM)
  8255. return soap->error = SOAP_MIME_HREF;
  8256. return soap->error = SOAP_DIME_HREF;
  8257. }
  8258. #endif
  8259. soap_free_ns(soap);
  8260. #ifndef WITH_LEANER
  8261. if (soap->fpreparefinalrecv)
  8262. return soap->error = soap->fpreparefinalrecv(soap);
  8263. #endif
  8264. return SOAP_OK;
  8265. }
  8266. #endif
  8267. /******************************************************************************/
  8268. #ifndef PALM_1
  8269. SOAP_FMAC1
  8270. void
  8271. SOAP_FMAC2
  8272. soap_free_temp(struct soap *soap)
  8273. { register struct soap_attribute *tp, *tq;
  8274. register struct Namespace *ns;
  8275. soap_free_ns(soap);
  8276. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free any remaining temp blocks\n"));
  8277. while (soap->blist)
  8278. soap_end_block(soap, NULL);
  8279. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute storage\n"));
  8280. for (tp = soap->attributes; tp; tp = tq)
  8281. { tq = tp->next;
  8282. if (tp->value)
  8283. SOAP_FREE(soap, tp->value);
  8284. SOAP_FREE(soap, tp);
  8285. }
  8286. soap->attributes = NULL;
  8287. #ifdef WITH_FAST
  8288. if (soap->labbuf)
  8289. SOAP_FREE(soap, soap->labbuf);
  8290. soap->labbuf = NULL;
  8291. soap->lablen = 0;
  8292. soap->labidx = 0;
  8293. #endif
  8294. ns = soap->local_namespaces;
  8295. if (ns)
  8296. { for (; ns->id; ns++)
  8297. { if (ns->out)
  8298. { SOAP_FREE(soap, ns->out);
  8299. ns->out = NULL;
  8300. }
  8301. }
  8302. SOAP_FREE(soap, soap->local_namespaces);
  8303. soap->local_namespaces = NULL;
  8304. }
  8305. #ifndef WITH_LEANER
  8306. while (soap->xlist)
  8307. { struct soap_xlist *xp = soap->xlist->next;
  8308. SOAP_FREE(soap, soap->xlist);
  8309. soap->xlist = xp;
  8310. }
  8311. #endif
  8312. #ifndef WITH_NOIDREF
  8313. soap_free_pht(soap);
  8314. soap_free_iht(soap);
  8315. #endif
  8316. }
  8317. #endif
  8318. /******************************************************************************/
  8319. #ifndef PALM_1
  8320. static void
  8321. soap_free_ns(struct soap *soap)
  8322. { register struct soap_nlist *np, *nq;
  8323. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free namespace stack\n"));
  8324. for (np = soap->nlist; np; np = nq)
  8325. { nq = np->next;
  8326. SOAP_FREE(soap, np);
  8327. }
  8328. soap->nlist = NULL;
  8329. }
  8330. #endif
  8331. /******************************************************************************/
  8332. #ifndef PALM_1
  8333. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8334. static void
  8335. soap_init_logs(struct soap *soap)
  8336. { int i;
  8337. for (i = 0; i < SOAP_MAXLOGS; i++)
  8338. { soap->logfile[i] = NULL;
  8339. soap->fdebug[i] = NULL;
  8340. }
  8341. }
  8342. #endif
  8343. #endif
  8344. /******************************************************************************/
  8345. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8346. SOAP_FMAC1
  8347. void
  8348. SOAP_FMAC2
  8349. soap_open_logfile(struct soap *soap, int i)
  8350. { if (soap->logfile[i])
  8351. soap->fdebug[i] = fopen(soap->logfile[i], i < 2 ? "ab" : "a");
  8352. }
  8353. #endif
  8354. /******************************************************************************/
  8355. #ifdef SOAP_DEBUG
  8356. static void
  8357. soap_close_logfile(struct soap *soap, int i)
  8358. { if (soap->fdebug[i])
  8359. { fclose(soap->fdebug[i]);
  8360. soap->fdebug[i] = NULL;
  8361. }
  8362. }
  8363. #endif
  8364. /******************************************************************************/
  8365. #ifdef SOAP_DEBUG
  8366. SOAP_FMAC1
  8367. void
  8368. SOAP_FMAC2
  8369. soap_close_logfiles(struct soap *soap)
  8370. { int i;
  8371. for (i = 0; i < SOAP_MAXLOGS; i++)
  8372. soap_close_logfile(soap, i);
  8373. }
  8374. #endif
  8375. /******************************************************************************/
  8376. #ifdef SOAP_DEBUG
  8377. static void
  8378. soap_set_logfile(struct soap *soap, int i, const char *logfile)
  8379. { const char *s;
  8380. char *t = NULL;
  8381. soap_close_logfile(soap, i);
  8382. s = soap->logfile[i];
  8383. soap->logfile[i] = logfile;
  8384. if (s)
  8385. SOAP_FREE(soap, (void*)s);
  8386. if (logfile)
  8387. if ((t = (char*)SOAP_MALLOC(soap, strlen(logfile) + 1)))
  8388. strcpy(t, logfile);
  8389. soap->logfile[i] = t;
  8390. }
  8391. #endif
  8392. /******************************************************************************/
  8393. SOAP_FMAC1
  8394. void
  8395. SOAP_FMAC2
  8396. soap_set_recv_logfile(struct soap *soap, const char *logfile)
  8397. {
  8398. #ifdef SOAP_DEBUG
  8399. soap_set_logfile(soap, SOAP_INDEX_RECV, logfile);
  8400. #endif
  8401. }
  8402. /******************************************************************************/
  8403. SOAP_FMAC1
  8404. void
  8405. SOAP_FMAC2
  8406. soap_set_sent_logfile(struct soap *soap, const char *logfile)
  8407. {
  8408. #ifdef SOAP_DEBUG
  8409. soap_set_logfile(soap, SOAP_INDEX_SENT, logfile);
  8410. #endif
  8411. }
  8412. /******************************************************************************/
  8413. SOAP_FMAC1
  8414. void
  8415. SOAP_FMAC2
  8416. soap_set_test_logfile(struct soap *soap, const char *logfile)
  8417. {
  8418. #ifdef SOAP_DEBUG
  8419. soap_set_logfile(soap, SOAP_INDEX_TEST, logfile);
  8420. #endif
  8421. }
  8422. /******************************************************************************/
  8423. #ifndef PALM_1
  8424. SOAP_FMAC1
  8425. struct soap*
  8426. SOAP_FMAC2
  8427. soap_copy(const struct soap *soap)
  8428. { return soap_copy_context((struct soap*)malloc(sizeof(struct soap)), soap);
  8429. }
  8430. #endif
  8431. /******************************************************************************/
  8432. #ifndef PALM_1
  8433. SOAP_FMAC1
  8434. struct soap*
  8435. SOAP_FMAC2
  8436. soap_copy_context(struct soap *copy, const struct soap *soap)
  8437. { if (copy == soap)
  8438. return copy;
  8439. if (soap_check_state(soap))
  8440. return NULL;
  8441. if (copy)
  8442. { register struct soap_plugin *p = NULL;
  8443. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying context\n"));
  8444. #ifdef __cplusplus
  8445. *copy = *soap;
  8446. #else
  8447. memcpy(copy, soap, sizeof(struct soap));
  8448. #endif
  8449. copy->state = SOAP_COPY;
  8450. copy->error = SOAP_OK;
  8451. copy->userid = NULL;
  8452. copy->passwd = NULL;
  8453. #ifdef WITH_NTLM
  8454. copy->ntlm_challenge = NULL;
  8455. #endif
  8456. copy->nlist = NULL;
  8457. copy->blist = NULL;
  8458. copy->clist = NULL;
  8459. copy->alist = NULL;
  8460. copy->attributes = NULL;
  8461. copy->labbuf = NULL;
  8462. copy->lablen = 0;
  8463. copy->labidx = 0;
  8464. #ifdef SOAP_MEM_DEBUG
  8465. soap_init_mht(copy);
  8466. #endif
  8467. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8468. soap_init_logs(copy);
  8469. #endif
  8470. #ifdef SOAP_DEBUG
  8471. soap_set_test_logfile(copy, soap->logfile[SOAP_INDEX_TEST]);
  8472. soap_set_sent_logfile(copy, soap->logfile[SOAP_INDEX_SENT]);
  8473. soap_set_recv_logfile(copy, soap->logfile[SOAP_INDEX_RECV]);
  8474. #endif
  8475. copy->namespaces = soap->local_namespaces;
  8476. copy->local_namespaces = NULL;
  8477. soap_set_local_namespaces(copy); /* copy content of soap->local_namespaces */
  8478. copy->namespaces = soap->namespaces; /* point to shared read-only namespaces table */
  8479. #ifdef WITH_C_LOCALE
  8480. # ifdef WIN32
  8481. copy->c_locale = _create_locale(LC_ALL, "C");
  8482. # else
  8483. copy->c_locale = duplocale(soap->c_locale);
  8484. # endif
  8485. #else
  8486. copy->c_locale = NULL;
  8487. #endif
  8488. #ifdef WITH_OPENSSL
  8489. copy->bio = NULL;
  8490. copy->ssl = NULL;
  8491. copy->session = NULL;
  8492. #endif
  8493. #ifdef WITH_GNUTLS
  8494. copy->session = NULL;
  8495. #endif
  8496. #ifdef WITH_ZLIB
  8497. copy->d_stream = (z_stream*)SOAP_MALLOC(copy, sizeof(z_stream));
  8498. copy->d_stream->zalloc = Z_NULL;
  8499. copy->d_stream->zfree = Z_NULL;
  8500. copy->d_stream->opaque = Z_NULL;
  8501. copy->z_buf = NULL;
  8502. #endif
  8503. #ifndef WITH_NOIDREF
  8504. soap_init_iht(copy);
  8505. soap_init_pht(copy);
  8506. #endif
  8507. copy->header = NULL;
  8508. copy->fault = NULL;
  8509. copy->action = NULL;
  8510. #ifndef WITH_LEAN
  8511. #ifdef WITH_COOKIES
  8512. copy->cookies = soap_copy_cookies(copy, soap);
  8513. #else
  8514. copy->cookies = NULL;
  8515. #endif
  8516. #endif
  8517. copy->plugins = NULL;
  8518. for (p = soap->plugins; p; p = p->next)
  8519. { register struct soap_plugin *q = (struct soap_plugin*)SOAP_MALLOC(copy, sizeof(struct soap_plugin));
  8520. if (!q)
  8521. return NULL;
  8522. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying plugin '%s'\n", p->id));
  8523. *q = *p;
  8524. if (p->fcopy && p->fcopy(copy, q, p))
  8525. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not copy plugin '%s'\n", p->id));
  8526. SOAP_FREE(copy, q);
  8527. return NULL;
  8528. }
  8529. q->next = copy->plugins;
  8530. copy->plugins = q;
  8531. }
  8532. }
  8533. return copy;
  8534. }
  8535. #endif
  8536. /******************************************************************************/
  8537. #ifndef PALM_1
  8538. SOAP_FMAC1
  8539. void
  8540. SOAP_FMAC2
  8541. soap_copy_stream(struct soap *copy, struct soap *soap)
  8542. { struct soap_attribute *tp = NULL, *tq;
  8543. if (copy == soap)
  8544. return;
  8545. copy->header = soap->header;
  8546. copy->mode = soap->mode;
  8547. copy->imode = soap->imode;
  8548. copy->omode = soap->omode;
  8549. copy->master = soap->master;
  8550. copy->socket = soap->socket;
  8551. copy->sendsk = soap->sendsk;
  8552. copy->recvsk = soap->recvsk;
  8553. copy->recv_timeout = soap->recv_timeout;
  8554. copy->send_timeout = soap->send_timeout;
  8555. #if defined(__cplusplus) && !defined(WITH_LEAN)
  8556. copy->os = soap->os;
  8557. copy->is = soap->is;
  8558. #endif
  8559. copy->sendfd = soap->sendfd;
  8560. copy->recvfd = soap->recvfd;
  8561. copy->bufidx = soap->bufidx;
  8562. copy->buflen = soap->buflen;
  8563. copy->ahead = soap->ahead;
  8564. copy->cdata = soap->cdata;
  8565. copy->chunksize = soap->chunksize;
  8566. copy->chunkbuflen = soap->chunkbuflen;
  8567. copy->keep_alive = soap->keep_alive;
  8568. copy->tcp_keep_alive = soap->tcp_keep_alive;
  8569. copy->tcp_keep_idle = soap->tcp_keep_idle;
  8570. copy->tcp_keep_intvl = soap->tcp_keep_intvl;
  8571. copy->tcp_keep_cnt = soap->tcp_keep_cnt;
  8572. copy->max_keep_alive = soap->max_keep_alive;
  8573. #ifndef WITH_NOIO
  8574. copy->peer = soap->peer;
  8575. copy->peerlen = soap->peerlen;
  8576. copy->ip = soap->ip;
  8577. copy->port = soap->port;
  8578. memcpy(copy->host, soap->host, sizeof(soap->host));
  8579. memcpy(copy->endpoint, soap->endpoint, sizeof(soap->endpoint));
  8580. #endif
  8581. #ifdef WITH_OPENSSL
  8582. copy->bio = soap->bio;
  8583. copy->ctx = soap->ctx;
  8584. copy->ssl = soap->ssl;
  8585. #endif
  8586. #ifdef WITH_GNUTLS
  8587. copy->session = soap->session;
  8588. #endif
  8589. #ifdef WITH_ZLIB
  8590. copy->zlib_state = soap->zlib_state;
  8591. copy->zlib_in = soap->zlib_in;
  8592. copy->zlib_out = soap->zlib_out;
  8593. if (!copy->d_stream)
  8594. copy->d_stream = (z_stream*)SOAP_MALLOC(copy, sizeof(z_stream));
  8595. if (copy->d_stream)
  8596. memcpy(copy->d_stream, soap->d_stream, sizeof(z_stream));
  8597. copy->z_crc = soap->z_crc;
  8598. copy->z_ratio_in = soap->z_ratio_in;
  8599. copy->z_ratio_out = soap->z_ratio_out;
  8600. copy->z_buf = NULL;
  8601. copy->z_buflen = soap->z_buflen;
  8602. copy->z_level = soap->z_level;
  8603. if (soap->z_buf && soap->zlib_state != SOAP_ZLIB_NONE)
  8604. { copy->z_buf = (char*)SOAP_MALLOC(copy, SOAP_BUFLEN);
  8605. if (copy->z_buf)
  8606. memcpy(copy->z_buf, soap->z_buf, SOAP_BUFLEN);
  8607. }
  8608. copy->z_dict = soap->z_dict;
  8609. copy->z_dict_len = soap->z_dict_len;
  8610. #endif
  8611. memcpy(copy->buf, soap->buf, sizeof(soap->buf));
  8612. /* copy XML parser state */
  8613. soap_free_ns(copy);
  8614. soap_set_local_namespaces(copy);
  8615. copy->version = soap->version;
  8616. if (soap->nlist && soap->local_namespaces)
  8617. { register struct soap_nlist *np = NULL, *nq;
  8618. /* copy reversed nlist */
  8619. for (nq = soap->nlist; nq; nq = nq->next)
  8620. { register struct soap_nlist *nr = np;
  8621. size_t n = sizeof(struct soap_nlist) + strlen(nq->id);
  8622. np = (struct soap_nlist*)SOAP_MALLOC(copy, n);
  8623. if (!np)
  8624. break;
  8625. memcpy(np, nq, n);
  8626. np->next = nr;
  8627. }
  8628. while (np)
  8629. { register const char *s = np->ns;
  8630. copy->level = np->level; /* preserve element nesting level */
  8631. if (!s && np->index >= 0)
  8632. { s = soap->local_namespaces[np->index].out;
  8633. if (!s)
  8634. s = soap->local_namespaces[np->index].ns;
  8635. }
  8636. if (s && soap_push_namespace(copy, np->id, s) == NULL)
  8637. break;
  8638. nq = np;
  8639. np = np->next;
  8640. SOAP_FREE(copy, nq);
  8641. }
  8642. }
  8643. memcpy(copy->tag, soap->tag, sizeof(copy->tag));
  8644. memcpy(copy->id, soap->id, sizeof(copy->id));
  8645. memcpy(copy->href, soap->href, sizeof(copy->href));
  8646. memcpy(copy->type, soap->type, sizeof(copy->type));
  8647. copy->other = soap->other;
  8648. copy->root = soap->root;
  8649. copy->null = soap->null;
  8650. copy->body = soap->body;
  8651. copy->part = soap->part;
  8652. copy->mustUnderstand = soap->mustUnderstand;
  8653. copy->level = soap->level;
  8654. copy->peeked = soap->peeked;
  8655. /* copy attributes */
  8656. for (tq = soap->attributes; tq; tq = tq->next)
  8657. { struct soap_attribute *tr = tp;
  8658. size_t n = sizeof(struct soap_attribute) + strlen(tq->name);
  8659. tp = (struct soap_attribute*)SOAP_MALLOC(copy, n);
  8660. memcpy(tp, tq, n);
  8661. if (tp->size)
  8662. { tp->value = (char*)SOAP_MALLOC(copy, tp->size);
  8663. if (tp->value)
  8664. strcpy(tp->value, tq->value);
  8665. }
  8666. tp->ns = NULL;
  8667. tp->next = tr;
  8668. }
  8669. copy->attributes = tp;
  8670. }
  8671. #endif
  8672. /******************************************************************************/
  8673. #ifndef PALM_1
  8674. SOAP_FMAC1
  8675. void
  8676. SOAP_FMAC2
  8677. soap_free_stream(struct soap *soap)
  8678. { soap->socket = SOAP_INVALID_SOCKET;
  8679. soap->sendsk = SOAP_INVALID_SOCKET;
  8680. soap->recvsk = SOAP_INVALID_SOCKET;
  8681. #ifdef WITH_OPENSSL
  8682. soap->bio = NULL;
  8683. soap->ctx = NULL;
  8684. soap->ssl = NULL;
  8685. #endif
  8686. #ifdef WITH_GNUTLS
  8687. soap->xcred = NULL;
  8688. soap->acred = NULL;
  8689. soap->cache = NULL;
  8690. soap->session = NULL;
  8691. soap->dh_params = NULL;
  8692. soap->rsa_params = NULL;
  8693. #endif
  8694. #ifdef WITH_ZLIB
  8695. if (soap->z_buf)
  8696. SOAP_FREE(soap, soap->z_buf);
  8697. soap->z_buf = NULL;
  8698. #endif
  8699. }
  8700. #endif
  8701. /******************************************************************************/
  8702. #ifndef PALM_1
  8703. SOAP_FMAC1
  8704. void
  8705. SOAP_FMAC2
  8706. soap_initialize(struct soap *soap)
  8707. { soap_versioning(soap_init)(soap, SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);
  8708. }
  8709. #endif
  8710. /******************************************************************************/
  8711. #ifndef PALM_1
  8712. SOAP_FMAC1
  8713. void
  8714. SOAP_FMAC2
  8715. soap_versioning(soap_init)(struct soap *soap, soap_mode imode, soap_mode omode)
  8716. { size_t i;
  8717. soap->state = SOAP_INIT;
  8718. #ifdef SOAP_MEM_DEBUG
  8719. soap_init_mht(soap);
  8720. #endif
  8721. #if !defined(WITH_LEAN) || defined(SOAP_DEBUG)
  8722. soap_init_logs(soap);
  8723. #endif
  8724. #ifdef SOAP_DEBUG
  8725. #ifdef TANDEM_NONSTOP
  8726. soap_set_test_logfile(soap, "TESTLOG");
  8727. soap_set_sent_logfile(soap, "SENTLOG");
  8728. soap_set_recv_logfile(soap, "RECVLOG");
  8729. #else
  8730. soap_set_test_logfile(soap, "TEST.log");
  8731. soap_set_sent_logfile(soap, "SENT.log");
  8732. soap_set_recv_logfile(soap, "RECV.log");
  8733. #endif
  8734. #endif
  8735. soap->version = 0;
  8736. soap_mode(soap, imode);
  8737. soap_imode(soap, imode);
  8738. soap_omode(soap, omode);
  8739. soap->plugins = NULL;
  8740. soap->user = NULL;
  8741. for (i = 0; i < sizeof(soap->data)/sizeof(*soap->data); i++)
  8742. soap->data[i] = NULL;
  8743. soap->userid = NULL;
  8744. soap->passwd = NULL;
  8745. soap->authrealm = NULL;
  8746. #ifdef WITH_NTLM
  8747. soap->ntlm_challenge = NULL;
  8748. #endif
  8749. #ifndef WITH_NOHTTP
  8750. soap->fpost = http_post;
  8751. soap->fget = http_get;
  8752. soap->fput = http_405;
  8753. soap->fdel = http_405;
  8754. soap->fopt = http_200;
  8755. soap->fhead = http_200;
  8756. soap->fform = NULL;
  8757. soap->fposthdr = http_post_header;
  8758. soap->fresponse = http_response;
  8759. soap->fparse = http_parse;
  8760. soap->fparsehdr = http_parse_header;
  8761. #endif
  8762. soap->fheader = NULL;
  8763. soap->fconnect = NULL;
  8764. soap->fdisconnect = NULL;
  8765. #ifndef WITH_NOIO
  8766. soap->ipv6_multicast_if = 0; /* in_addr_t value */
  8767. soap->ipv4_multicast_if = NULL; /* points to struct in_addr or in_addr_t */
  8768. soap->ipv4_multicast_ttl = 0; /* 0: use default */
  8769. #ifndef WITH_IPV6
  8770. soap->fresolve = tcp_gethost;
  8771. #else
  8772. soap->fresolve = NULL;
  8773. #endif
  8774. soap->faccept = tcp_accept;
  8775. soap->fopen = tcp_connect;
  8776. soap->fclose = tcp_disconnect;
  8777. soap->fclosesocket = tcp_closesocket;
  8778. soap->fshutdownsocket = tcp_shutdownsocket;
  8779. soap->fsend = fsend;
  8780. soap->frecv = frecv;
  8781. soap->fpoll = soap_poll;
  8782. #else
  8783. soap->fopen = NULL;
  8784. soap->fclose = NULL;
  8785. soap->fpoll = NULL;
  8786. #endif
  8787. soap->fseterror = NULL;
  8788. soap->fignore = NULL;
  8789. soap->fserveloop = NULL;
  8790. soap->fplugin = fplugin;
  8791. soap->fmalloc = NULL;
  8792. #ifndef WITH_LEANER
  8793. soap->feltbegin = NULL;
  8794. soap->feltendin = NULL;
  8795. soap->feltbegout = NULL;
  8796. soap->feltendout = NULL;
  8797. soap->fprepareinitsend = NULL;
  8798. soap->fprepareinitrecv = NULL;
  8799. soap->fpreparesend = NULL;
  8800. soap->fpreparerecv = NULL;
  8801. soap->fpreparefinalsend = NULL;
  8802. soap->fpreparefinalrecv = NULL;
  8803. soap->ffiltersend = NULL;
  8804. soap->ffilterrecv = NULL;
  8805. soap->fdimereadopen = NULL;
  8806. soap->fdimewriteopen = NULL;
  8807. soap->fdimereadclose = NULL;
  8808. soap->fdimewriteclose = NULL;
  8809. soap->fdimeread = NULL;
  8810. soap->fdimewrite = NULL;
  8811. soap->fmimereadopen = NULL;
  8812. soap->fmimewriteopen = NULL;
  8813. soap->fmimereadclose = NULL;
  8814. soap->fmimewriteclose = NULL;
  8815. soap->fmimeread = NULL;
  8816. soap->fmimewrite = NULL;
  8817. #endif
  8818. soap->float_format = "%.9G"; /* Alternative: use "%G" */
  8819. soap->double_format = "%.17lG"; /* Alternative: use "%lG" */
  8820. soap->dime_id_format = "cid:id%d"; /* default DIME id format */
  8821. soap->http_version = "1.1";
  8822. soap->proxy_http_version = "1.0";
  8823. soap->http_content = NULL;
  8824. soap->actor = NULL;
  8825. soap->lang = "en";
  8826. soap->keep_alive = 0;
  8827. soap->tcp_keep_alive = 0;
  8828. soap->tcp_keep_idle = 0;
  8829. soap->tcp_keep_intvl = 0;
  8830. soap->tcp_keep_cnt = 0;
  8831. soap->max_keep_alive = SOAP_MAXKEEPALIVE;
  8832. soap->recv_timeout = 0;
  8833. soap->send_timeout = 0;
  8834. soap->connect_timeout = 0;
  8835. soap->accept_timeout = 0;
  8836. soap->socket_flags = 0;
  8837. soap->connect_flags = 0;
  8838. soap->bind_flags = 0;
  8839. soap->accept_flags = 0;
  8840. soap->linger_time = 0;
  8841. soap->ip = 0;
  8842. soap->labbuf = NULL;
  8843. soap->lablen = 0;
  8844. soap->labidx = 0;
  8845. soap->encodingStyle = NULL;
  8846. #ifndef WITH_NONAMESPACES
  8847. soap->namespaces = namespaces;
  8848. #else
  8849. soap->namespaces = NULL;
  8850. #endif
  8851. soap->local_namespaces = NULL;
  8852. soap->nlist = NULL;
  8853. soap->blist = NULL;
  8854. soap->clist = NULL;
  8855. soap->alist = NULL;
  8856. soap->attributes = NULL;
  8857. soap->header = NULL;
  8858. soap->fault = NULL;
  8859. soap->master = SOAP_INVALID_SOCKET;
  8860. soap->socket = SOAP_INVALID_SOCKET;
  8861. soap->sendsk = SOAP_INVALID_SOCKET;
  8862. soap->recvsk = SOAP_INVALID_SOCKET;
  8863. soap->os = NULL;
  8864. soap->is = NULL;
  8865. #ifndef WITH_LEANER
  8866. soap->dom = NULL;
  8867. soap->dime.list = NULL;
  8868. soap->dime.first = NULL;
  8869. soap->dime.last = NULL;
  8870. soap->mime.list = NULL;
  8871. soap->mime.first = NULL;
  8872. soap->mime.last = NULL;
  8873. soap->mime.boundary = NULL;
  8874. soap->mime.start = NULL;
  8875. soap->xlist = NULL;
  8876. #endif
  8877. #ifndef UNDER_CE
  8878. soap->recvfd = 0;
  8879. soap->sendfd = 1;
  8880. #else
  8881. soap->recvfd = stdin;
  8882. soap->sendfd = stdout;
  8883. #endif
  8884. soap->host[0] = '\0';
  8885. soap->port = 0;
  8886. soap->action = NULL;
  8887. soap->proxy_host = NULL;
  8888. soap->proxy_port = 8080;
  8889. soap->proxy_userid = NULL;
  8890. soap->proxy_passwd = NULL;
  8891. soap->prolog = NULL;
  8892. #ifdef WITH_ZLIB
  8893. soap->zlib_state = SOAP_ZLIB_NONE;
  8894. soap->zlib_in = SOAP_ZLIB_NONE;
  8895. soap->zlib_out = SOAP_ZLIB_NONE;
  8896. soap->d_stream = (z_stream*)SOAP_MALLOC(soap, sizeof(z_stream));
  8897. soap->d_stream->zalloc = Z_NULL;
  8898. soap->d_stream->zfree = Z_NULL;
  8899. soap->d_stream->opaque = Z_NULL;
  8900. soap->z_buf = NULL;
  8901. soap->z_level = 6;
  8902. soap->z_dict = NULL;
  8903. soap->z_dict_len = 0;
  8904. #endif
  8905. #ifndef WITH_LEAN
  8906. soap->wsuid = NULL;
  8907. soap->c14nexclude = NULL;
  8908. soap->cookies = NULL;
  8909. soap->cookie_domain = NULL;
  8910. soap->cookie_path = NULL;
  8911. soap->cookie_max = 32;
  8912. #endif
  8913. #ifdef WMW_RPM_IO
  8914. soap->rpmreqid = NULL;
  8915. #endif
  8916. #ifdef PALM
  8917. palmNetLibOpen();
  8918. #endif
  8919. #ifndef WITH_NOIDREF
  8920. soap_init_iht(soap);
  8921. soap_init_pht(soap);
  8922. #endif
  8923. #ifdef WITH_OPENSSL
  8924. if (!soap_ssl_init_done)
  8925. soap_ssl_init();
  8926. soap->fsslauth = ssl_auth_init;
  8927. soap->fsslverify = ssl_verify_callback;
  8928. soap->bio = NULL;
  8929. soap->ssl = NULL;
  8930. soap->ctx = NULL;
  8931. soap->session = NULL;
  8932. soap->ssl_flags = SOAP_SSL_DEFAULT;
  8933. soap->keyfile = NULL;
  8934. soap->keyid = NULL;
  8935. soap->password = NULL;
  8936. soap->cafile = NULL;
  8937. soap->capath = NULL;
  8938. soap->crlfile = NULL;
  8939. soap->dhfile = NULL;
  8940. soap->randfile = NULL;
  8941. #endif
  8942. #ifdef WITH_GNUTLS
  8943. if (!soap_ssl_init_done)
  8944. soap_ssl_init();
  8945. soap->fsslauth = ssl_auth_init;
  8946. soap->fsslverify = NULL;
  8947. soap->xcred = NULL;
  8948. soap->acred = NULL;
  8949. soap->cache = NULL;
  8950. soap->session = NULL;
  8951. soap->ssl_flags = SOAP_SSL_DEFAULT;
  8952. soap->keyfile = NULL;
  8953. soap->keyid = NULL;
  8954. soap->password = NULL;
  8955. soap->cafile = NULL;
  8956. soap->capath = NULL;
  8957. soap->crlfile = NULL;
  8958. soap->dh_params = NULL;
  8959. soap->rsa_params = NULL;
  8960. #endif
  8961. #ifdef WITH_C_LOCALE
  8962. # ifdef WIN32
  8963. soap->c_locale = _create_locale(LC_ALL, "C");
  8964. # else
  8965. soap->c_locale = newlocale(LC_ALL_MASK, "C", NULL);
  8966. # endif
  8967. #else
  8968. soap->c_locale = NULL;
  8969. #endif
  8970. soap->buflen = 0;
  8971. soap->bufidx = 0;
  8972. #ifndef WITH_LEANER
  8973. soap->dime.chunksize = 0;
  8974. soap->dime.buflen = 0;
  8975. #endif
  8976. soap->null = 0;
  8977. soap->position = 0;
  8978. soap->encoding = 0;
  8979. soap->mustUnderstand = 0;
  8980. soap->ns = 0;
  8981. soap->part = SOAP_END;
  8982. soap->event = 0;
  8983. soap->evlev = 0;
  8984. soap->alloced = 0;
  8985. soap->count = 0;
  8986. soap->length = 0;
  8987. soap->cdata = 0;
  8988. soap->peeked = 0;
  8989. soap->ahead = 0;
  8990. soap->idnum = 0;
  8991. soap->level = 0;
  8992. soap->endpoint[0] = '\0';
  8993. soap->error = SOAP_OK;
  8994. }
  8995. #endif
  8996. /******************************************************************************/
  8997. #ifndef PALM_2
  8998. SOAP_FMAC1
  8999. void
  9000. SOAP_FMAC2
  9001. soap_begin(struct soap *soap)
  9002. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reinitializing context\n"));
  9003. if (!soap->keep_alive)
  9004. { soap->buflen = 0;
  9005. soap->bufidx = 0;
  9006. }
  9007. soap->null = 0;
  9008. soap->position = 0;
  9009. soap->encoding = 0;
  9010. soap->mustUnderstand = 0;
  9011. soap->mode = 0;
  9012. soap->ns = 0;
  9013. soap->part = SOAP_END;
  9014. soap->event = 0;
  9015. soap->evlev = 0;
  9016. soap->alloced = 0;
  9017. soap->count = 0;
  9018. soap->length = 0;
  9019. soap->cdata = 0;
  9020. soap->error = SOAP_OK;
  9021. soap->peeked = 0;
  9022. soap->ahead = 0;
  9023. soap->idnum = 0;
  9024. soap->level = 0;
  9025. soap->endpoint[0] = '\0';
  9026. soap->encodingStyle = SOAP_STR_EOS;
  9027. #ifndef WITH_LEANER
  9028. soap->dime.chunksize = 0;
  9029. soap->dime.buflen = 0;
  9030. #endif
  9031. soap_free_temp(soap);
  9032. }
  9033. #endif
  9034. /******************************************************************************/
  9035. #ifndef PALM_2
  9036. SOAP_FMAC1
  9037. void
  9038. SOAP_FMAC2
  9039. soap_end(struct soap *soap)
  9040. { if (soap_check_state(soap))
  9041. return;
  9042. soap_free_temp(soap);
  9043. soap_dealloc(soap, NULL);
  9044. while (soap->clist)
  9045. { register struct soap_clist *cp = soap->clist->next;
  9046. SOAP_FREE(soap, soap->clist);
  9047. soap->clist = cp;
  9048. }
  9049. soap_closesock(soap);
  9050. #ifdef SOAP_DEBUG
  9051. soap_close_logfiles(soap);
  9052. #endif
  9053. #ifdef PALM
  9054. palmNetLibClose();
  9055. #endif
  9056. }
  9057. #endif
  9058. /******************************************************************************/
  9059. #ifndef PALM_1
  9060. SOAP_FMAC1
  9061. void
  9062. SOAP_FMAC2
  9063. soap_set_version(struct soap *soap, short version)
  9064. { soap_set_local_namespaces(soap);
  9065. if (soap->version != version)
  9066. { if (version == 1)
  9067. { soap->local_namespaces[0].ns = soap_env1;
  9068. soap->local_namespaces[1].ns = soap_enc1;
  9069. }
  9070. else if (version == 2)
  9071. { soap->local_namespaces[0].ns = soap_env2;
  9072. soap->local_namespaces[1].ns = soap_enc2;
  9073. }
  9074. soap->version = version;
  9075. }
  9076. }
  9077. #endif
  9078. /******************************************************************************/
  9079. #ifndef PALM_1
  9080. SOAP_FMAC1
  9081. int
  9082. SOAP_FMAC2
  9083. soap_set_namespaces(struct soap *soap, const struct Namespace *p)
  9084. { register struct Namespace *ns = soap->local_namespaces;
  9085. register struct soap_nlist *np, *nq, *nr;
  9086. register unsigned int level = soap->level;
  9087. soap->namespaces = p;
  9088. soap->local_namespaces = NULL;
  9089. soap_set_local_namespaces(soap);
  9090. /* reverse the namespace list */
  9091. np = soap->nlist;
  9092. soap->nlist = NULL;
  9093. if (np)
  9094. { nq = np->next;
  9095. np->next = NULL;
  9096. while (nq)
  9097. { nr = nq->next;
  9098. nq->next = np;
  9099. np = nq;
  9100. nq = nr;
  9101. }
  9102. }
  9103. /* then push on new stack */
  9104. while (np)
  9105. { register const char *s;
  9106. soap->level = np->level; /* preserve element nesting level */
  9107. s = np->ns;
  9108. if (!s && np->index >= 0 && ns)
  9109. { s = ns[np->index].out;
  9110. if (!s)
  9111. s = ns[np->index].ns;
  9112. }
  9113. if (s && soap_push_namespace(soap, np->id, s) == NULL)
  9114. return soap->error;
  9115. nq = np;
  9116. np = np->next;
  9117. SOAP_FREE(soap, nq);
  9118. }
  9119. if (ns)
  9120. { register int i;
  9121. for (i = 0; ns[i].id; i++)
  9122. { if (ns[i].out)
  9123. { SOAP_FREE(soap, ns[i].out);
  9124. ns[i].out = NULL;
  9125. }
  9126. }
  9127. SOAP_FREE(soap, ns);
  9128. }
  9129. soap->level = level; /* restore level */
  9130. return SOAP_OK;
  9131. }
  9132. #endif
  9133. /******************************************************************************/
  9134. #ifndef PALM_1
  9135. SOAP_FMAC1
  9136. void
  9137. SOAP_FMAC2
  9138. soap_set_local_namespaces(struct soap *soap)
  9139. { if (soap->namespaces && !soap->local_namespaces)
  9140. { register const struct Namespace *ns1;
  9141. register struct Namespace *ns2;
  9142. register size_t n = 1;
  9143. for (ns1 = soap->namespaces; ns1->id; ns1++)
  9144. n++;
  9145. n *= sizeof(struct Namespace);
  9146. ns2 = (struct Namespace*)SOAP_MALLOC(soap, n);
  9147. if (ns2)
  9148. { memcpy(ns2, soap->namespaces, n);
  9149. if (ns2[0].ns)
  9150. { if (!strcmp(ns2[0].ns, soap_env1))
  9151. soap->version = 1;
  9152. else if (!strcmp(ns2[0].ns, soap_env2))
  9153. soap->version = 2;
  9154. }
  9155. soap->local_namespaces = ns2;
  9156. for (; ns2->id; ns2++)
  9157. ns2->out = NULL;
  9158. }
  9159. }
  9160. }
  9161. #endif
  9162. /******************************************************************************/
  9163. #ifndef WITH_LEAN
  9164. #ifndef PALM_1
  9165. SOAP_FMAC1
  9166. const char *
  9167. SOAP_FMAC2
  9168. soap_tagsearch(const char *big, const char *little)
  9169. { if (little)
  9170. { register size_t n = strlen(little);
  9171. register const char *s = big;
  9172. while (s)
  9173. { register const char *t = s;
  9174. register size_t i;
  9175. for (i = 0; i < n; i++, t++)
  9176. { if (*t != little[i])
  9177. break;
  9178. }
  9179. if (*t == '\0' || *t == ' ')
  9180. { if (i == n || (i && little[i-1] == ':'))
  9181. return s;
  9182. }
  9183. s = strchr(t, ' ');
  9184. if (s)
  9185. s++;
  9186. }
  9187. }
  9188. return NULL;
  9189. }
  9190. #endif
  9191. #endif
  9192. /******************************************************************************/
  9193. #ifndef WITH_LEAN
  9194. #ifndef PALM_1
  9195. SOAP_FMAC1
  9196. struct soap_nlist *
  9197. SOAP_FMAC2
  9198. soap_lookup_ns(struct soap *soap, const char *tag, size_t n)
  9199. { register struct soap_nlist *np;
  9200. for (np = soap->nlist; np; np = np->next)
  9201. { if (!strncmp(np->id, tag, n) && !np->id[n])
  9202. return np;
  9203. }
  9204. return NULL;
  9205. }
  9206. #endif
  9207. #endif
  9208. /******************************************************************************/
  9209. #ifndef WITH_LEAN
  9210. static struct soap_nlist *
  9211. soap_push_ns(struct soap *soap, const char *id, const char *ns, short utilized)
  9212. { register struct soap_nlist *np;
  9213. size_t n, k;
  9214. if (soap_tagsearch(soap->c14nexclude, id))
  9215. return NULL;
  9216. if (!utilized)
  9217. { for (np = soap->nlist; np; np = np->next)
  9218. { if (!strcmp(np->id, id) && (!np->ns || !strcmp(np->ns, ns)))
  9219. break;
  9220. }
  9221. if (np)
  9222. { if ((np->level < soap->level || !np->ns) && np->index == 1)
  9223. utilized = 1;
  9224. else
  9225. return NULL;
  9226. }
  9227. }
  9228. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Adding namespace binding (level=%u) '%s' '%s' utilized=%d\n", soap->level, id, ns ? ns : "(null)", utilized));
  9229. n = strlen(id);
  9230. if (ns)
  9231. k = strlen(ns);
  9232. else
  9233. k = 0;
  9234. np = (struct soap_nlist*)SOAP_MALLOC(soap, sizeof(struct soap_nlist) + n + k + 1);
  9235. if (!np)
  9236. { soap->error = SOAP_EOM;
  9237. return NULL;
  9238. }
  9239. np->next = soap->nlist;
  9240. soap->nlist = np;
  9241. strcpy((char*)np->id, id);
  9242. if (ns)
  9243. np->ns = strcpy((char*)np->id + n + 1, ns);
  9244. else
  9245. np->ns = NULL;
  9246. np->level = soap->level;
  9247. np->index = utilized;
  9248. return np;
  9249. }
  9250. #endif
  9251. /******************************************************************************/
  9252. #ifndef WITH_LEAN
  9253. static void
  9254. soap_utilize_ns(struct soap *soap, const char *tag)
  9255. { register struct soap_nlist *np;
  9256. size_t n = 0;
  9257. const char *t = strchr(tag, ':');
  9258. if (t)
  9259. { n = t - tag;
  9260. if (n >= sizeof(soap->tmpbuf))
  9261. n = sizeof(soap->tmpbuf) - 1;
  9262. }
  9263. np = soap_lookup_ns(soap, tag, n);
  9264. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Utilizing namespace of '%s'\n", tag));
  9265. if (np)
  9266. { if (np->index <= 0)
  9267. soap_push_ns(soap, np->id, np->ns, 1);
  9268. }
  9269. else if (strncmp(tag, "xml", 3))
  9270. { strncpy(soap->tmpbuf, tag, n);
  9271. soap->tmpbuf[n] = '\0';
  9272. soap_push_ns(soap, soap->tmpbuf, NULL, 1);
  9273. }
  9274. }
  9275. #endif
  9276. /******************************************************************************/
  9277. #ifndef PALM_2
  9278. SOAP_FMAC1
  9279. int
  9280. SOAP_FMAC2
  9281. soap_element(struct soap *soap, const char *tag, int id, const char *type)
  9282. {
  9283. #ifndef WITH_LEAN
  9284. register const char *s;
  9285. #endif
  9286. 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));
  9287. soap->level++;
  9288. #ifdef WITH_DOM
  9289. #ifndef WITH_LEAN
  9290. if (soap->wsuid && soap_tagsearch(soap->wsuid, tag))
  9291. { size_t i;
  9292. for (s = tag, i = 0; *s && i < sizeof(soap->tag) - 1; s++, i++)
  9293. soap->tag[i] = *s == ':' ? '-' : *s;
  9294. soap->tag[i] = '\0';
  9295. if (soap_set_attr(soap, "wsu:Id", soap->tag, 1))
  9296. return soap->error;
  9297. }
  9298. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_DOM_ASIS))
  9299. { if (soap->evlev >= soap->level)
  9300. soap->evlev = 0;
  9301. if (soap->event == SOAP_SEC_BEGIN && !soap->evlev)
  9302. { register struct soap_nlist *np;
  9303. /* non-nested wsu:Id found: clear xmlns, re-emit them for exc-c14n */
  9304. for (np = soap->nlist; np; np = np->next)
  9305. { if (np->index == 2)
  9306. { struct soap_nlist *np1 = soap_push_ns(soap, np->id, np->ns, 1);
  9307. if (np1)
  9308. np1->index = 0;
  9309. }
  9310. }
  9311. soap->evlev = soap->level;
  9312. }
  9313. }
  9314. #endif
  9315. if (soap->mode & SOAP_XML_DOM)
  9316. { register struct soap_dom_element *elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  9317. if (!elt)
  9318. return soap->error;
  9319. elt->soap = soap;
  9320. elt->next = NULL;
  9321. elt->prnt = soap->dom;
  9322. elt->name = soap_strdup(soap, tag);
  9323. elt->elts = NULL;
  9324. elt->atts = NULL;
  9325. elt->nstr = NULL;
  9326. elt->data = NULL;
  9327. elt->wide = NULL;
  9328. elt->node = NULL;
  9329. elt->type = 0;
  9330. elt->head = NULL;
  9331. elt->tail = NULL;
  9332. if (soap->dom)
  9333. { struct soap_dom_element *p = soap->dom->elts;
  9334. if (p)
  9335. { while (p->next)
  9336. p = p->next;
  9337. p->next = elt;
  9338. }
  9339. else
  9340. soap->dom->elts = elt;
  9341. }
  9342. soap->dom = elt;
  9343. }
  9344. else
  9345. {
  9346. #endif
  9347. #ifndef WITH_LEAN
  9348. if (!soap->ns)
  9349. { if (!(soap->mode & SOAP_XML_CANONICAL)
  9350. && soap_send(soap, soap->prolog ? soap->prolog : "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))
  9351. return soap->error;
  9352. }
  9353. else if (soap->mode & SOAP_XML_INDENT)
  9354. { if (soap->ns == 1 && soap_send_raw(soap, soap_indent, soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  9355. return soap->error;
  9356. soap->body = 1;
  9357. }
  9358. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tag, ':')))
  9359. { struct Namespace *ns = soap->local_namespaces;
  9360. size_t n = s - tag;
  9361. if (soap_send_raw(soap, "<", 1)
  9362. || soap_send(soap, s + 1))
  9363. return soap->error;
  9364. if (soap->nlist && !strncmp(soap->nlist->id, tag, n) && !soap->nlist->id[n])
  9365. ns = NULL;
  9366. for (; ns && ns->id; ns++)
  9367. //{ if (*ns->id && (ns->out || ns->ns) && !strncmp(ns->id, tag, n) && !ns->id[n]) // Gagua
  9368. { if (ns->id && (ns->out || ns->ns) && !strncmp(ns->id, tag, n) && !ns->id[n])
  9369. { soap_push_ns(soap, ns->id, ns->out ? ns->out : ns->ns, 0);
  9370. if (soap_attribute(soap, "xmlns", ns->out ? ns->out : ns->ns))
  9371. return soap->error;
  9372. break;
  9373. }
  9374. }
  9375. }
  9376. else
  9377. #endif
  9378. if (soap_send_raw(soap, "<", 1)
  9379. || soap_send(soap, tag))
  9380. return soap->error;
  9381. #ifdef WITH_DOM
  9382. }
  9383. #endif
  9384. if (!soap->ns)
  9385. { struct Namespace *ns = soap->local_namespaces;
  9386. int k = -1;
  9387. if (ns)
  9388. {
  9389. #ifndef WITH_LEAN
  9390. if ((soap->mode & SOAP_XML_DEFAULTNS))
  9391. { if (soap->version)
  9392. k = 4; /* first four required entries */
  9393. else if (!(soap->mode & SOAP_XML_NOTYPE) || (soap->mode & SOAP_XML_NIL))
  9394. { ns += 2;
  9395. k = 2; /* next two entries */
  9396. }
  9397. else
  9398. k = 0; /* no entries */
  9399. }
  9400. #endif
  9401. while (k-- && ns->id)
  9402. //{ if (*ns->id && (ns->out || ns->ns)) // Gagua
  9403. { if (ns->id && (ns->out || ns->ns))
  9404. {
  9405. #ifdef HAVE_SNPRINTF
  9406. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns:%s", ns->id);
  9407. #else
  9408. sprintf(soap->tmpbuf, "xmlns:%s", ns->id);
  9409. #endif
  9410. if (soap_attribute(soap, soap->tmpbuf, ns->out ? ns->out : ns->ns))
  9411. return soap->error;
  9412. }
  9413. ns++;
  9414. }
  9415. }
  9416. }
  9417. soap->ns = 1; /* namespace table control: ns = 0 or 2 to start, then 1 to stop dumping the table */
  9418. #ifndef WITH_LEAN
  9419. if (soap->mode & SOAP_XML_CANONICAL)
  9420. soap_utilize_ns(soap, tag);
  9421. #endif
  9422. if (id > 0)
  9423. {
  9424. #ifdef HAVE_SNPRINTF
  9425. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "_%d", id);
  9426. #else
  9427. sprintf(soap->tmpbuf, "_%d", id);
  9428. #endif
  9429. if (soap->version == 2)
  9430. { if (soap_attribute(soap, "SOAP-ENC:id", soap->tmpbuf))
  9431. return soap->error;
  9432. }
  9433. else if (soap_attribute(soap, "id", soap->tmpbuf))
  9434. return soap->error;
  9435. }
  9436. if (type && *type && !(soap->mode & SOAP_XML_NOTYPE) && soap->part != SOAP_IN_HEADER)
  9437. { const char *t = type;
  9438. #ifndef WITH_LEAN
  9439. if (soap->mode & SOAP_XML_DEFAULTNS)
  9440. { t = strchr(type, ':');
  9441. if (t)
  9442. t++;
  9443. else
  9444. t = type;
  9445. }
  9446. #endif
  9447. if (soap->attributes ? soap_set_attr(soap, "xsi:type", t, 1) : soap_attribute(soap, "xsi:type", t))
  9448. return soap->error;
  9449. #ifndef WITH_LEAN
  9450. if (soap->mode & SOAP_XML_CANONICAL)
  9451. soap_utilize_ns(soap, type);
  9452. #endif
  9453. }
  9454. if (soap->null && soap->position > 0)
  9455. { register int i;
  9456. #ifdef HAVE_SNPRINTF
  9457. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf) - 1, "[%d", soap->positions[0]);
  9458. #else
  9459. sprintf(soap->tmpbuf, "[%d", soap->positions[0]);
  9460. #endif
  9461. for (i = 1; i < soap->position; i++)
  9462. { register size_t l = strlen(soap->tmpbuf);
  9463. #ifdef HAVE_SNPRINTF
  9464. soap_snprintf(soap->tmpbuf + l, sizeof(soap->tmpbuf)-l-1, ",%d", soap->positions[i]);
  9465. #else
  9466. if (l + 13 < sizeof(soap->tmpbuf))
  9467. sprintf(soap->tmpbuf + l, ",%d", soap->positions[i]);
  9468. #endif
  9469. }
  9470. strcat(soap->tmpbuf, "]");
  9471. if (soap_attribute(soap, "SOAP-ENC:position", soap->tmpbuf))
  9472. return soap->error;
  9473. }
  9474. if (soap->mustUnderstand)
  9475. { if (soap->actor && *soap->actor)
  9476. { if (soap_attribute(soap, soap->version == 2 ? "SOAP-ENV:role" : "SOAP-ENV:actor", soap->actor))
  9477. return soap->error;
  9478. }
  9479. if (soap_attribute(soap, "SOAP-ENV:mustUnderstand", soap->version == 2 ? "true" : "1"))
  9480. return soap->error;
  9481. soap->mustUnderstand = 0;
  9482. }
  9483. if (soap->encoding)
  9484. { if (soap->encodingStyle && soap->local_namespaces && soap->local_namespaces[0].id && soap->local_namespaces[1].id)
  9485. { if (!*soap->encodingStyle)
  9486. { if (soap->local_namespaces[1].out)
  9487. soap->encodingStyle = soap->local_namespaces[1].out;
  9488. else
  9489. soap->encodingStyle = soap->local_namespaces[1].ns;
  9490. }
  9491. if (soap->encodingStyle && soap_attribute(soap, "SOAP-ENV:encodingStyle", soap->encodingStyle))
  9492. return soap->error;
  9493. }
  9494. else
  9495. soap->encodingStyle = NULL;
  9496. soap->encoding = 0;
  9497. }
  9498. soap->null = 0;
  9499. soap->position = 0;
  9500. if (soap->event == SOAP_SEC_BEGIN)
  9501. soap->event = 0;
  9502. return SOAP_OK;
  9503. }
  9504. #endif
  9505. /******************************************************************************/
  9506. #ifndef PALM_2
  9507. SOAP_FMAC1
  9508. int
  9509. SOAP_FMAC2
  9510. soap_element_begin_out(struct soap *soap, const char *tag, int id, const char *type)
  9511. { if (*tag == '-')
  9512. return SOAP_OK;
  9513. if (soap_element(soap, tag, id, type))
  9514. return soap->error;
  9515. #ifdef WITH_DOM
  9516. if (soap_element_start_end_out(soap, NULL))
  9517. return soap->error;
  9518. if (soap->feltbegout)
  9519. return soap->error = soap->feltbegout(soap, tag);
  9520. return SOAP_OK;
  9521. #else
  9522. return soap_element_start_end_out(soap, NULL);
  9523. #endif
  9524. }
  9525. #endif
  9526. /******************************************************************************/
  9527. #ifndef PALM_2
  9528. #ifndef HAVE_STRRCHR
  9529. SOAP_FMAC1
  9530. char*
  9531. SOAP_FMAC2
  9532. soap_strrchr(const char *s, int t)
  9533. { register char *r = NULL;
  9534. while (*s)
  9535. if (*s++ == t)
  9536. r = (char*)s - 1;
  9537. return r;
  9538. }
  9539. #endif
  9540. #endif
  9541. /******************************************************************************/
  9542. #ifndef PALM_2
  9543. #ifndef HAVE_STRTOL
  9544. SOAP_FMAC1
  9545. long
  9546. SOAP_FMAC2
  9547. soap_strtol(const char *s, char **t, int b)
  9548. { register long n = 0;
  9549. register int c;
  9550. while (*s > 0 && *s <= 32)
  9551. s++;
  9552. if (b == 10)
  9553. { short neg = 0;
  9554. if (*s == '-')
  9555. { s++;
  9556. neg = 1;
  9557. }
  9558. else if (*s == '+')
  9559. s++;
  9560. while ((c = *s) && c >= '0' && c <= '9')
  9561. { if (n >= 214748364 && (n > 214748364 || c >= '8'))
  9562. break;
  9563. n *= 10;
  9564. n += c - '0';
  9565. s++;
  9566. }
  9567. if (neg)
  9568. n = -n;
  9569. }
  9570. else /* assume b == 16 and value is always positive */
  9571. { while ((c = *s))
  9572. { if (c >= '0' && c <= '9')
  9573. c -= '0';
  9574. else if (c >= 'A' && c <= 'F')
  9575. c -= 'A' - 10;
  9576. else if (c >= 'a' && c <= 'f')
  9577. c -= 'a' - 10;
  9578. if (n > 0x07FFFFFF)
  9579. break;
  9580. n <<= 4;
  9581. n += c;
  9582. s++;
  9583. }
  9584. }
  9585. if (t)
  9586. *t = (char*)s;
  9587. return n;
  9588. }
  9589. #endif
  9590. #endif
  9591. /******************************************************************************/
  9592. #ifndef PALM_2
  9593. #ifndef HAVE_STRTOUL
  9594. SOAP_FMAC1
  9595. unsigned long
  9596. SOAP_FMAC2
  9597. soap_strtoul(const char *s, char **t, int b)
  9598. { unsigned long n = 0;
  9599. register int c;
  9600. while (*s > 0 && *s <= 32)
  9601. s++;
  9602. if (b == 10)
  9603. { if (*s == '+')
  9604. s++;
  9605. while ((c = *s) && c >= '0' && c <= '9')
  9606. { if (n >= 429496729 && (n > 429496729 || c >= '6'))
  9607. break;
  9608. n *= 10;
  9609. n += c - '0';
  9610. s++;
  9611. }
  9612. }
  9613. else /* b == 16 */
  9614. { while ((c = *s))
  9615. { if (c >= '0' && c <= '9')
  9616. c -= '0';
  9617. else if (c >= 'A' && c <= 'F')
  9618. c -= 'A' - 10;
  9619. else if (c >= 'a' && c <= 'f')
  9620. c -= 'a' - 10;
  9621. if (n > 0x0FFFFFFF)
  9622. break;
  9623. n <<= 4;
  9624. n += c;
  9625. s++;
  9626. }
  9627. }
  9628. if (t)
  9629. *t = (char*)s;
  9630. return n;
  9631. }
  9632. #endif
  9633. #endif
  9634. /******************************************************************************/
  9635. #ifndef PALM_1
  9636. SOAP_FMAC1
  9637. int
  9638. SOAP_FMAC2
  9639. soap_array_begin_out(struct soap *soap, const char *tag, int id, const char *type, const char *offset)
  9640. { if (!type || !*type)
  9641. return soap_element_begin_out(soap, tag, id, NULL);
  9642. if (soap_element(soap, tag, id, "SOAP-ENC:Array"))
  9643. return soap->error;
  9644. if (soap->version == 2)
  9645. { const char *s;
  9646. s = soap_strrchr(type, '[');
  9647. if (s && (size_t)(s - type) < sizeof(soap->tmpbuf))
  9648. { strncpy(soap->tmpbuf, type, s - type);
  9649. soap->tmpbuf[s - type] = '\0';
  9650. if (soap_attribute(soap, "SOAP-ENC:itemType", soap->tmpbuf))
  9651. return soap->error;
  9652. s++;
  9653. if (*s)
  9654. { strncpy(soap->tmpbuf, s, sizeof(soap->tmpbuf));
  9655. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0';
  9656. soap->tmpbuf[strlen(soap->tmpbuf) - 1] = '\0';
  9657. if (soap_attribute(soap, "SOAP-ENC:arraySize", soap->tmpbuf))
  9658. return soap->error;
  9659. }
  9660. }
  9661. }
  9662. else
  9663. { if (offset && soap_attribute(soap, "SOAP-ENC:offset", offset))
  9664. return soap->error;
  9665. if (soap_attribute(soap, "SOAP-ENC:arrayType", type))
  9666. return soap->error;
  9667. }
  9668. #ifndef WITH_LEAN
  9669. if ((soap->mode & SOAP_XML_CANONICAL))
  9670. soap_utilize_ns(soap, type);
  9671. #endif
  9672. return soap_element_start_end_out(soap, NULL);
  9673. }
  9674. #endif
  9675. /******************************************************************************/
  9676. #ifndef PALM_1
  9677. SOAP_FMAC1
  9678. int
  9679. SOAP_FMAC2
  9680. soap_element_start_end_out(struct soap *soap, const char *tag)
  9681. { register struct soap_attribute *tp;
  9682. #ifndef WITH_LEAN
  9683. if (soap->mode & SOAP_XML_CANONICAL)
  9684. { struct soap_nlist *np;
  9685. for (tp = soap->attributes; tp; tp = tp->next)
  9686. { if (tp->visible && tp->name)
  9687. soap_utilize_ns(soap, tp->name);
  9688. }
  9689. for (np = soap->nlist; np; np = np->next)
  9690. { if (np->index == 1 && np->ns)
  9691. { if (*(np->id))
  9692. {
  9693. #ifdef HAVE_SNPRINTF
  9694. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns:%s", np->id);
  9695. #else
  9696. sprintf(soap->tmpbuf, "xmlns:%s", np->id);
  9697. #endif
  9698. }
  9699. else
  9700. strcpy(soap->tmpbuf, "xmlns");
  9701. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enabling utilized binding (level=%u) %s='%s'\n", np->level, soap->tmpbuf, np->ns));
  9702. soap_set_attr(soap, soap->tmpbuf, np->ns, 1);
  9703. np->index = 2;
  9704. }
  9705. }
  9706. }
  9707. #endif
  9708. #ifdef WITH_DOM
  9709. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  9710. { register struct soap_dom_attribute **att;
  9711. att = &soap->dom->atts;
  9712. for (tp = soap->attributes; tp; tp = tp->next)
  9713. { if (tp->visible)
  9714. { *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  9715. if (!*att)
  9716. return soap->error;
  9717. (*att)->next = NULL;
  9718. (*att)->nstr = NULL;
  9719. (*att)->name = soap_strdup(soap, tp->name);
  9720. (*att)->data = soap_strdup(soap, tp->value);
  9721. (*att)->wide = NULL;
  9722. (*att)->soap = soap;
  9723. att = &(*att)->next;
  9724. tp->visible = 0;
  9725. }
  9726. }
  9727. return SOAP_OK;
  9728. }
  9729. #endif
  9730. for (tp = soap->attributes; tp; tp = tp->next)
  9731. { if (tp->visible)
  9732. {
  9733. #ifndef WITH_LEAN
  9734. const char *s;
  9735. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tp->name, ':')))
  9736. { size_t n = s - tp->name;
  9737. if (soap->nlist && !strncmp(soap->nlist->id, tp->name, n) && !soap->nlist->id[n])
  9738. s++;
  9739. else
  9740. s = tp->name;
  9741. if (soap_send(soap, " ") || soap_send(soap, s))
  9742. return soap->error;
  9743. }
  9744. else
  9745. #endif
  9746. if (soap_send(soap, " ") || soap_send(soap, tp->name))
  9747. return soap->error;
  9748. if (tp->visible == 2 && tp->value)
  9749. if (soap_send_raw(soap, "=\"", 2)
  9750. || soap_string_out(soap, tp->value, tp->flag)
  9751. || soap_send_raw(soap, "\"", 1))
  9752. return soap->error;
  9753. tp->visible = 0;
  9754. }
  9755. }
  9756. if (tag)
  9757. {
  9758. #ifndef WITH_LEAN
  9759. if (soap->mode & SOAP_XML_CANONICAL)
  9760. { if (soap_send_raw(soap, ">", 1)
  9761. || soap_element_end_out(soap, tag))
  9762. return soap->error;
  9763. return SOAP_OK;
  9764. }
  9765. #endif
  9766. soap->level--; /* decrement level just before /> */
  9767. return soap_send_raw(soap, "/>", 2);
  9768. }
  9769. return soap_send_raw(soap, ">", 1);
  9770. }
  9771. #endif
  9772. /******************************************************************************/
  9773. #ifndef PALM_1
  9774. SOAP_FMAC1
  9775. int
  9776. SOAP_FMAC2
  9777. soap_element_end_out(struct soap *soap, const char *tag)
  9778. {
  9779. #ifndef WITH_LEAN
  9780. const char *s;
  9781. #endif
  9782. if (*tag == '-')
  9783. return SOAP_OK;
  9784. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element ending tag='%s'\n", tag));
  9785. #ifdef WITH_DOM
  9786. if (soap->feltendout && (soap->error = soap->feltendout(soap, tag)))
  9787. return soap->error;
  9788. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  9789. { if (soap->dom->prnt)
  9790. soap->dom = soap->dom->prnt;
  9791. return SOAP_OK;
  9792. }
  9793. #endif
  9794. #ifndef WITH_LEAN
  9795. if (soap->mode & SOAP_XML_CANONICAL)
  9796. soap_pop_namespace(soap);
  9797. if (soap->mode & SOAP_XML_INDENT)
  9798. { if (!soap->body)
  9799. { if (soap_send_raw(soap, soap_indent, soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  9800. return soap->error;
  9801. }
  9802. soap->body = 0;
  9803. }
  9804. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tag, ':')))
  9805. { soap_pop_namespace(soap);
  9806. tag = s + 1;
  9807. }
  9808. #endif
  9809. if (soap_send_raw(soap, "</", 2)
  9810. || soap_send(soap, tag))
  9811. return soap->error;
  9812. soap->level--; /* decrement level just before > */
  9813. return soap_send_raw(soap, ">", 1);
  9814. }
  9815. #endif
  9816. /******************************************************************************/
  9817. #ifndef PALM_1
  9818. SOAP_FMAC1
  9819. int
  9820. SOAP_FMAC2
  9821. soap_element_ref(struct soap *soap, const char *tag, int id, int href)
  9822. { register const char *s = "ref";
  9823. register int n = 1;
  9824. if (soap->version == 1)
  9825. { s = "href";
  9826. n = 0;
  9827. }
  9828. else if (soap->version == 2)
  9829. s = "SOAP-ENC:ref";
  9830. #ifdef HAVE_SNPRINTF
  9831. soap_snprintf(soap->href, sizeof(soap->href), "#_%d", href);
  9832. #else
  9833. sprintf(soap->href, "#_%d", href);
  9834. #endif
  9835. return soap_element_href(soap, tag, id, s, soap->href + n);
  9836. }
  9837. #endif
  9838. /******************************************************************************/
  9839. #ifndef PALM_1
  9840. SOAP_FMAC1
  9841. int
  9842. SOAP_FMAC2
  9843. soap_element_href(struct soap *soap, const char *tag, int id, const char *ref, const char *val)
  9844. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element '%s' reference %s='%s'\n", tag, ref, val));
  9845. if (soap_element(soap, tag, id, NULL)
  9846. || soap_attribute(soap, ref, val)
  9847. || soap_element_start_end_out(soap, tag))
  9848. return soap->error;
  9849. return SOAP_OK;
  9850. }
  9851. #endif
  9852. /******************************************************************************/
  9853. #ifndef PALM_1
  9854. SOAP_FMAC1
  9855. int
  9856. SOAP_FMAC2
  9857. soap_element_null(struct soap *soap, const char *tag, int id, const char *type)
  9858. { struct soap_attribute *tp = NULL;
  9859. for (tp = soap->attributes; tp; tp = tp->next)
  9860. if (tp->visible)
  9861. break;
  9862. if (tp || (soap->version == 2 && soap->position > 0) || id > 0 || (soap->mode & SOAP_XML_NIL))
  9863. { if (soap_element(soap, tag, id, type)
  9864. || (!tp && soap_attribute(soap, "xsi:nil", "true")))
  9865. return soap->error;
  9866. return soap_element_start_end_out(soap, tag);
  9867. }
  9868. soap->null = 1;
  9869. soap->position = 0;
  9870. soap->mustUnderstand = 0;
  9871. return SOAP_OK;
  9872. }
  9873. #endif
  9874. /******************************************************************************/
  9875. #ifndef PALM_1
  9876. SOAP_FMAC1
  9877. int
  9878. SOAP_FMAC2
  9879. soap_element_nil(struct soap *soap, const char *tag)
  9880. { if (soap_element(soap, tag, -1, NULL)
  9881. || ((soap->mode & SOAP_XML_NIL) && soap_attribute(soap, "xsi:nil", "true")))
  9882. return soap->error;
  9883. return soap_element_start_end_out(soap, tag);
  9884. }
  9885. #endif
  9886. /******************************************************************************/
  9887. #ifndef PALM_1
  9888. SOAP_FMAC1
  9889. int
  9890. SOAP_FMAC2
  9891. 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)
  9892. { if (!p)
  9893. { soap->error = soap_element_null(soap, tag, id, type);
  9894. return -1;
  9895. }
  9896. #ifndef WITH_NOIDREF
  9897. if ((!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  9898. return 0;
  9899. if (id < 0)
  9900. { struct soap_plist *pp;
  9901. if (a)
  9902. id = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  9903. else
  9904. id = soap_pointer_lookup(soap, p, t, &pp);
  9905. if (id)
  9906. { if (soap_is_embedded(soap, pp))
  9907. { soap_element_ref(soap, tag, 0, id);
  9908. return -1;
  9909. }
  9910. if (soap_is_single(soap, pp))
  9911. return 0;
  9912. soap_set_embedded(soap, pp);
  9913. }
  9914. }
  9915. return id;
  9916. #else
  9917. return 0;
  9918. #endif
  9919. }
  9920. #endif
  9921. /******************************************************************************/
  9922. #ifndef PALM_1
  9923. SOAP_FMAC1
  9924. int
  9925. SOAP_FMAC2
  9926. soap_element_result(struct soap *soap, const char *tag)
  9927. { if (soap->version == 2 && soap->encodingStyle)
  9928. { if (soap_element(soap, "SOAP-RPC:result", 0, NULL)
  9929. || soap_attribute(soap, "xmlns:SOAP-RPC", soap_rpc)
  9930. || soap_element_start_end_out(soap, NULL)
  9931. || soap_string_out(soap, tag, 0)
  9932. || soap_element_end_out(soap, "SOAP-RPC:result"))
  9933. return soap->error;
  9934. }
  9935. return SOAP_OK;
  9936. }
  9937. #endif
  9938. /******************************************************************************/
  9939. #ifndef PALM_1
  9940. SOAP_FMAC1
  9941. void
  9942. SOAP_FMAC2
  9943. soap_check_result(struct soap *soap, const char *tag)
  9944. { if (soap->version == 2 && soap->encodingStyle)
  9945. { soap_instring(soap, ":result", NULL, NULL, 0, 2, -1, -1);
  9946. /* just ignore content for compliance reasons, but should compare tag to element's QName value? */
  9947. }
  9948. (void)tag;
  9949. }
  9950. #endif
  9951. /******************************************************************************/
  9952. #ifndef PALM_2
  9953. SOAP_FMAC1
  9954. int
  9955. SOAP_FMAC2
  9956. soap_attribute(struct soap *soap, const char *name, const char *value)
  9957. {
  9958. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attribute '%s'='%s'\n", name, value));
  9959. #ifdef WITH_DOM
  9960. if ((soap->mode & SOAP_XML_DOM) && !(soap->mode & SOAP_XML_CANONICAL) && soap->dom)
  9961. { register struct soap_dom_attribute *a = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  9962. if (!a)
  9963. return soap->error;
  9964. a->next = soap->dom->atts;
  9965. a->nstr = NULL;
  9966. a->name = soap_strdup(soap, name);
  9967. a->data = soap_strdup(soap, value);
  9968. a->wide = NULL;
  9969. a->soap = soap;
  9970. soap->dom->atts = a;
  9971. return SOAP_OK;
  9972. }
  9973. #endif
  9974. #ifndef WITH_LEAN
  9975. if (soap->mode & SOAP_XML_CANONICAL)
  9976. { /* push namespace */
  9977. if (!strncmp(name, "xmlns", 5) && (name[5] == ':' || name[5] == '\0'))
  9978. soap_push_ns(soap, name + 5 + (name[5] == ':'), value, 0);
  9979. else if (soap_set_attr(soap, name, value, 1))
  9980. return soap->error;
  9981. }
  9982. else
  9983. #endif
  9984. { if (soap_send(soap, " ") || soap_send(soap, name))
  9985. return soap->error;
  9986. if (value)
  9987. if (soap_send_raw(soap, "=\"", 2)
  9988. || soap_string_out(soap, value, 1)
  9989. || soap_send_raw(soap, "\"", 1))
  9990. return soap->error;
  9991. }
  9992. return SOAP_OK;
  9993. }
  9994. #endif
  9995. /******************************************************************************/
  9996. #ifndef PALM_2
  9997. SOAP_FMAC1
  9998. int
  9999. SOAP_FMAC2
  10000. soap_element_begin_in(struct soap *soap, const char *tag, int nillable, const char *type)
  10001. { if (!soap_peek_element(soap))
  10002. { if (soap->other)
  10003. return soap->error = SOAP_TAG_MISMATCH;
  10004. if (tag && *tag == '-')
  10005. return SOAP_OK;
  10006. if (!(soap->error = soap_match_tag(soap, soap->tag, tag)))
  10007. { soap->peeked = 0;
  10008. if (type && *soap->type && soap_match_tag(soap, soap->type, type))
  10009. return soap->error = SOAP_TYPE;
  10010. if (!nillable && soap->null && (soap->mode & SOAP_XML_STRICT))
  10011. return soap->error = SOAP_NULL;
  10012. if (soap->body)
  10013. soap->level++;
  10014. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin element found (level=%u) '%s'='%s'\n", soap->level, soap->tag, tag ? tag : SOAP_STR_EOS ));
  10015. soap->error = SOAP_OK;
  10016. }
  10017. }
  10018. else if (soap->error == SOAP_NO_TAG && tag && *tag == '-')
  10019. soap->error = SOAP_OK;
  10020. return soap->error;
  10021. }
  10022. #endif
  10023. /******************************************************************************/
  10024. #ifndef PALM_2
  10025. SOAP_FMAC1
  10026. int
  10027. SOAP_FMAC2
  10028. soap_element_end_in(struct soap *soap, const char *tag)
  10029. { register soap_wchar c;
  10030. register char *s;
  10031. register int n = 0;
  10032. if (tag && *tag == '-')
  10033. return SOAP_OK;
  10034. if (soap->error == SOAP_NO_TAG)
  10035. soap->error = SOAP_OK;
  10036. #ifdef WITH_DOM
  10037. /* this whitespace or mixed content is significant for DOM */
  10038. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  10039. { if (!soap->peeked && !soap_string_in(soap, 3, -1, -1))
  10040. return soap->error;
  10041. if (soap->dom->prnt)
  10042. soap->dom = soap->dom->prnt;
  10043. }
  10044. #endif
  10045. if (soap->peeked)
  10046. { if (*soap->tag)
  10047. n++;
  10048. soap->peeked = 0;
  10049. }
  10050. do
  10051. { while (((c = soap_get(soap)) != SOAP_TT))
  10052. { if ((int)c == EOF)
  10053. return soap->error = SOAP_CHK_EOF;
  10054. if (c == SOAP_LT)
  10055. n++;
  10056. else if (c == '/')
  10057. { c = soap_get(soap);
  10058. if (c == SOAP_GT)
  10059. n--;
  10060. else
  10061. soap_unget(soap, c);
  10062. }
  10063. }
  10064. } while (n--);
  10065. s = soap->tag;
  10066. n = sizeof(soap->tag);
  10067. while (soap_notblank(c = soap_get(soap)))
  10068. { if (--n > 0)
  10069. *s++ = (char)c;
  10070. }
  10071. *s = '\0';
  10072. if ((int)c == EOF)
  10073. return soap->error = SOAP_CHK_EOF;
  10074. while (soap_blank(c))
  10075. c = soap_get(soap);
  10076. if (c != SOAP_GT)
  10077. return soap->error = SOAP_SYNTAX_ERROR;
  10078. #ifndef WITH_LEAN
  10079. #ifdef WITH_DOM
  10080. if (soap->feltendin)
  10081. { soap->level--;
  10082. return soap->error = soap->feltendin(soap, soap->tag, tag);
  10083. }
  10084. #endif
  10085. if (tag && (soap->mode & SOAP_XML_STRICT))
  10086. { soap_pop_namespace(soap);
  10087. if (soap_match_tag(soap, soap->tag, tag))
  10088. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End element tag '%s' does not match '%s'\n", soap->tag, tag ? tag : SOAP_STR_EOS));
  10089. return soap->error = SOAP_SYNTAX_ERROR;
  10090. }
  10091. }
  10092. #endif
  10093. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End element found (level=%u) '%s'='%s'\n", soap->level, soap->tag, tag ? tag : SOAP_STR_EOS));
  10094. soap->level--;
  10095. return SOAP_OK;
  10096. }
  10097. #endif
  10098. /******************************************************************************/
  10099. #ifndef PALM_2
  10100. SOAP_FMAC1
  10101. const char *
  10102. SOAP_FMAC2
  10103. soap_attr_value(struct soap *soap, const char *name, int flag)
  10104. { register struct soap_attribute *tp;
  10105. if (*name == '-')
  10106. return SOAP_STR_EOS;
  10107. for (tp = soap->attributes; tp; tp = tp->next)
  10108. { if (tp->visible && !soap_match_tag(soap, tp->name, name))
  10109. break;
  10110. }
  10111. if (tp)
  10112. { if (flag == 2 && (soap->mode & SOAP_XML_STRICT))
  10113. soap->error = SOAP_PROHIBITED;
  10114. else
  10115. return tp->value;
  10116. }
  10117. else if (flag == 1 && (soap->mode & SOAP_XML_STRICT))
  10118. soap->error = SOAP_REQUIRED;
  10119. else
  10120. soap->error = SOAP_OK;
  10121. return NULL;
  10122. }
  10123. #endif
  10124. /******************************************************************************/
  10125. #ifndef PALM_2
  10126. SOAP_FMAC1
  10127. int
  10128. SOAP_FMAC2
  10129. soap_set_attr(struct soap *soap, const char *name, const char *value, int flag)
  10130. { register struct soap_attribute *tp;
  10131. if (*name == '-')
  10132. return SOAP_OK;
  10133. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set attribute %s='%s'\n", name, value ? value : SOAP_STR_EOS));
  10134. for (tp = soap->attributes; tp; tp = tp->next)
  10135. { if (!strcmp(tp->name, name))
  10136. break;
  10137. }
  10138. if (!tp)
  10139. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute %s\n", name));
  10140. if (!(tp = (struct soap_attribute*)SOAP_MALLOC(soap, sizeof(struct soap_attribute) + strlen(name))))
  10141. return soap->error = SOAP_EOM;
  10142. tp->ns = NULL;
  10143. #ifndef WITH_LEAN
  10144. if ((soap->mode & SOAP_XML_CANONICAL))
  10145. { struct soap_attribute **tpp = &soap->attributes;
  10146. const char *s = strchr(name, ':');
  10147. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inserting attribute %s for c14n\n", name));
  10148. if (!strncmp(name, "xmlns", 5))
  10149. { for (; *tpp; tpp = &(*tpp)->next)
  10150. if (strncmp((*tpp)->name, "xmlns", 5) || strcmp((*tpp)->name + 5, name + 5) > 0)
  10151. break;
  10152. }
  10153. else if (!s)
  10154. { for (; *tpp; tpp = &(*tpp)->next)
  10155. if (strncmp((*tpp)->name, "xmlns", 5) && ((*tpp)->ns || strcmp((*tpp)->name, name) > 0))
  10156. break;
  10157. }
  10158. else
  10159. { struct soap_nlist *np = soap_lookup_ns(soap, name, s - name);
  10160. if (np)
  10161. tp->ns = np->ns;
  10162. else
  10163. { struct soap_attribute *tq;
  10164. for (tq = soap->attributes; tq; tq = tq->next)
  10165. { if (!strncmp(tq->name, "xmlns:", 6) && !strncmp(tq->name + 6, name, s - name) && !tq->name[6 + s - name])
  10166. { tp->ns = tq->ns;
  10167. break;
  10168. }
  10169. }
  10170. }
  10171. for (; *tpp; tpp = &(*tpp)->next)
  10172. { int k;
  10173. if (strncmp((*tpp)->name, "xmlns", 5) && (*tpp)->ns && tp->ns && ((k = strcmp((*tpp)->ns, tp->ns)) > 0 || (!k && strcmp((*tpp)->name, name) > 0)))
  10174. break;
  10175. }
  10176. }
  10177. tp->next = *tpp;
  10178. *tpp = tp;
  10179. }
  10180. else
  10181. #endif
  10182. { tp->next = soap->attributes;
  10183. soap->attributes = tp;
  10184. }
  10185. strcpy((char*)tp->name, name);
  10186. tp->value = NULL;
  10187. }
  10188. else if (tp->visible)
  10189. { return SOAP_OK;
  10190. }
  10191. else if (value && tp->value && tp->size <= strlen(value))
  10192. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute value of %s (free %p)\n", name, tp->value));
  10193. SOAP_FREE(soap, tp->value);
  10194. tp->value = NULL;
  10195. tp->ns = NULL;
  10196. }
  10197. if (value)
  10198. { if (!tp->value)
  10199. { tp->size = strlen(value) + 1;
  10200. if (!(tp->value = (char*)SOAP_MALLOC(soap, tp->size)))
  10201. return soap->error = SOAP_EOM;
  10202. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute value for %s (%p)\n", tp->name, tp->value));
  10203. }
  10204. strcpy(tp->value, value);
  10205. if (!strncmp(tp->name, "xmlns:", 6))
  10206. tp->ns = tp->value;
  10207. tp->visible = 2;
  10208. tp->flag = (short)flag;
  10209. #ifndef WITH_LEAN
  10210. if (!strcmp(name, "wsu:Id"))
  10211. { soap->event = SOAP_SEC_BEGIN;
  10212. strncpy(soap->id, value, sizeof(soap->id));
  10213. soap->id[sizeof(soap->id) - 1] = '\0';
  10214. }
  10215. #endif
  10216. }
  10217. else
  10218. tp->visible = 1;
  10219. return SOAP_OK;
  10220. }
  10221. #endif
  10222. /******************************************************************************/
  10223. #ifndef PALM_2
  10224. SOAP_FMAC1
  10225. void
  10226. SOAP_FMAC2
  10227. soap_clr_attr(struct soap *soap)
  10228. { register struct soap_attribute *tp;
  10229. #ifndef WITH_LEAN
  10230. if ((soap->mode & SOAP_XML_CANONICAL))
  10231. { while (soap->attributes)
  10232. { tp = soap->attributes->next;
  10233. if (soap->attributes->value)
  10234. SOAP_FREE(soap, soap->attributes->value);
  10235. SOAP_FREE(soap, soap->attributes);
  10236. soap->attributes = tp;
  10237. }
  10238. }
  10239. else
  10240. #endif
  10241. { for (tp = soap->attributes; tp; tp = tp->next)
  10242. tp->visible = 0;
  10243. }
  10244. }
  10245. #endif
  10246. /******************************************************************************/
  10247. #ifndef PALM_2
  10248. static int
  10249. soap_getattrval(struct soap *soap, char *s, size_t n, soap_wchar d)
  10250. { register size_t i;
  10251. for (i = 0; i < n; i++)
  10252. { register soap_wchar c = soap_get(soap);
  10253. switch (c)
  10254. {
  10255. case SOAP_TT:
  10256. *s++ = '<';
  10257. soap_unget(soap, '/');
  10258. break;
  10259. case SOAP_LT:
  10260. *s++ = '<';
  10261. break;
  10262. case SOAP_GT:
  10263. if (d == ' ')
  10264. { soap_unget(soap, c);
  10265. *s = '\0';
  10266. return SOAP_OK;
  10267. }
  10268. *s++ = '>';
  10269. break;
  10270. case SOAP_QT:
  10271. if (c == d)
  10272. { *s = '\0';
  10273. return SOAP_OK;
  10274. }
  10275. *s++ = '"';
  10276. break;
  10277. case SOAP_AP:
  10278. if (c == d)
  10279. { *s = '\0';
  10280. return SOAP_OK;
  10281. }
  10282. *s++ = '\'';
  10283. break;
  10284. case '\t':
  10285. case '\n':
  10286. case '\r':
  10287. case ' ':
  10288. case '/':
  10289. if (d == ' ')
  10290. { soap_unget(soap, c);
  10291. *s = '\0';
  10292. return SOAP_OK;
  10293. }
  10294. default:
  10295. if ((int)c == EOF)
  10296. { *s = '\0';
  10297. return soap->error = SOAP_CHK_EOF;
  10298. }
  10299. *s++ = (char)c;
  10300. }
  10301. }
  10302. return soap->error = SOAP_EOM;
  10303. }
  10304. #endif
  10305. /******************************************************************************/
  10306. #ifdef WITH_FAST
  10307. #ifndef PALM_2
  10308. SOAP_FMAC1
  10309. int
  10310. SOAP_FMAC2
  10311. soap_store_lab(struct soap *soap, const char *s, size_t n)
  10312. { soap->labidx = 0;
  10313. return soap_append_lab(soap, s, n);
  10314. }
  10315. #endif
  10316. #endif
  10317. /******************************************************************************/
  10318. #ifdef WITH_FAST
  10319. #ifndef PALM_2
  10320. SOAP_FMAC1
  10321. int
  10322. SOAP_FMAC2
  10323. soap_append_lab(struct soap *soap, const char *s, size_t n)
  10324. { if (soap->labidx + n >= soap->lablen)
  10325. { register char *t = soap->labbuf;
  10326. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging look-aside buffer to append data, size=%lu\n", (unsigned long)soap->lablen));
  10327. if (soap->lablen == 0)
  10328. soap->lablen = SOAP_LABLEN;
  10329. while (soap->labidx + n >= soap->lablen)
  10330. soap->lablen <<= 1;
  10331. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New look-aside buffer size=%lu\n", (unsigned long)soap->lablen));
  10332. soap->labbuf = (char*)SOAP_MALLOC(soap, soap->lablen);
  10333. if (!soap->labbuf)
  10334. { if (t)
  10335. SOAP_FREE(soap, t);
  10336. return soap->error = SOAP_EOM;
  10337. }
  10338. if (t)
  10339. { memcpy(soap->labbuf, t, soap->labidx);
  10340. SOAP_FREE(soap, t);
  10341. }
  10342. }
  10343. if (s)
  10344. { memcpy(soap->labbuf + soap->labidx, s, n);
  10345. soap->labidx += n;
  10346. }
  10347. return SOAP_OK;
  10348. }
  10349. #endif
  10350. #endif
  10351. /******************************************************************************/
  10352. #ifndef PALM_2
  10353. SOAP_FMAC1
  10354. int
  10355. SOAP_FMAC2
  10356. soap_peek_element(struct soap *soap)
  10357. {
  10358. #ifdef WITH_DOM
  10359. register struct soap_dom_attribute **att = NULL;
  10360. register char *lead = NULL;
  10361. #endif
  10362. register struct soap_attribute *tp, *tq = NULL;
  10363. register const char *t;
  10364. register char *s;
  10365. register soap_wchar c;
  10366. register int i;
  10367. if (soap->peeked)
  10368. { if (!*soap->tag)
  10369. return soap->error = SOAP_NO_TAG;
  10370. return SOAP_OK;
  10371. }
  10372. soap->peeked = 1;
  10373. soap->id[0] = '\0';
  10374. soap->href[0] = '\0';
  10375. soap->type[0] = '\0';
  10376. soap->arrayType[0] = '\0';
  10377. soap->arraySize[0] = '\0';
  10378. soap->arrayOffset[0] = '\0';
  10379. soap->other = 0;
  10380. soap->root = -1;
  10381. soap->position = 0;
  10382. soap->null = 0;
  10383. soap->mustUnderstand = 0;
  10384. /* UTF-8 BOM? */
  10385. c = soap_getchar(soap);
  10386. if (c == 0xEF && soap_get0(soap) == 0xBB)
  10387. { c = soap_get1(soap);
  10388. if ((c = soap_get1(soap)) == 0xBF)
  10389. soap->mode &= ~SOAP_ENC_LATIN;
  10390. else
  10391. soap_unget(soap, (0x0F << 12) | (0xBB << 6) | (c & 0x3F)); /* UTF-8 */
  10392. }
  10393. else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  10394. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  10395. return soap->error = SOAP_UTF_ERROR;
  10396. else
  10397. soap_unget(soap, c);
  10398. c = soap_get(soap);
  10399. #ifdef WITH_DOM
  10400. /* whitespace leading to tag is not insignificant for DOM */
  10401. if (soap_blank(c))
  10402. { soap->labidx = 0;
  10403. do
  10404. { if (soap_append_lab(soap, NULL, 0))
  10405. return soap->error;
  10406. s = soap->labbuf + soap->labidx;
  10407. i = soap->lablen - soap->labidx;
  10408. soap->labidx = soap->lablen;
  10409. while (soap_blank(c) && i--)
  10410. { *s++ = c;
  10411. c = soap_get(soap);
  10412. }
  10413. }
  10414. while (soap_blank(c));
  10415. *s = '\0';
  10416. lead = soap->labbuf;
  10417. }
  10418. #else
  10419. /* skip space */
  10420. while (soap_blank(c))
  10421. c = soap_get(soap);
  10422. #endif
  10423. if (c != SOAP_LT)
  10424. { *soap->tag = '\0';
  10425. if ((int)c == EOF)
  10426. return soap->error = SOAP_CHK_EOF;
  10427. soap_unget(soap, c);
  10428. #ifdef WITH_DOM
  10429. /* whitespace leading to end tag is significant for DOM */
  10430. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  10431. { if (lead && *lead)
  10432. soap->dom->tail = soap_strdup(soap, lead);
  10433. else
  10434. soap->dom->tail = (char*)SOAP_STR_EOS;
  10435. }
  10436. #endif
  10437. return soap->error = SOAP_NO_TAG;
  10438. }
  10439. do c = soap_get1(soap);
  10440. while (soap_blank(c));
  10441. s = soap->tag;
  10442. i = sizeof(soap->tag);
  10443. while (c != '>' && c != '/' && soap_notblank(c) && (int)c != EOF)
  10444. { if (--i > 0)
  10445. *s++ = (char)c;
  10446. c = soap_get1(soap);
  10447. }
  10448. *s = '\0';
  10449. while (soap_blank(c))
  10450. c = soap_get1(soap);
  10451. #ifdef WITH_DOM
  10452. if (soap->mode & SOAP_XML_DOM)
  10453. { register struct soap_dom_element *elt;
  10454. elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  10455. if (!elt)
  10456. return soap->error;
  10457. elt->next = NULL;
  10458. elt->nstr = NULL;
  10459. elt->name = soap_strdup(soap, soap->tag);
  10460. elt->prnt = soap->dom;
  10461. elt->elts = NULL;
  10462. elt->atts = NULL;
  10463. elt->data = NULL;
  10464. elt->wide = NULL;
  10465. elt->type = 0;
  10466. elt->node = NULL;
  10467. elt->head = soap_strdup(soap, lead);
  10468. elt->tail = NULL;
  10469. elt->soap = soap;
  10470. if (soap->dom)
  10471. { struct soap_dom_element *p = soap->dom->elts;
  10472. if (p)
  10473. { while (p->next)
  10474. p = p->next;
  10475. p->next = elt;
  10476. }
  10477. else
  10478. soap->dom->elts = elt;
  10479. }
  10480. soap->dom = elt;
  10481. att = &elt->atts;
  10482. }
  10483. #endif
  10484. soap_pop_namespace(soap);
  10485. for (tp = soap->attributes; tp; tp = tp->next)
  10486. tp->visible = 0;
  10487. while ((int)c != EOF && c != '>' && c != '/')
  10488. { s = soap->tmpbuf;
  10489. i = sizeof(soap->tmpbuf);
  10490. while (c != '=' && c != '>' && c != '/' && soap_notblank(c) && (int)c != EOF)
  10491. { if (--i > 0)
  10492. *s++ = (char)c;
  10493. c = soap_get1(soap);
  10494. }
  10495. *s = '\0';
  10496. if (i == sizeof(soap->tmpbuf))
  10497. return soap->error = SOAP_SYNTAX_ERROR;
  10498. #ifdef WITH_DOM
  10499. /* add attribute name to dom */
  10500. if (att)
  10501. { *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  10502. if (!*att)
  10503. return soap->error;
  10504. (*att)->next = NULL;
  10505. (*att)->nstr = NULL;
  10506. (*att)->name = soap_strdup(soap, soap->tmpbuf);
  10507. (*att)->data = NULL;
  10508. (*att)->wide = NULL;
  10509. (*att)->soap = soap;
  10510. }
  10511. #endif
  10512. if (!strncmp(soap->tmpbuf, "xmlns", 5))
  10513. { if (soap->tmpbuf[5] == ':')
  10514. t = soap->tmpbuf + 6;
  10515. else if (soap->tmpbuf[5])
  10516. t = NULL;
  10517. else
  10518. t = SOAP_STR_EOS;
  10519. }
  10520. else
  10521. t = NULL;
  10522. tq = NULL;
  10523. for (tp = soap->attributes; tp; tq = tp, tp = tp->next)
  10524. { if (!SOAP_STRCMP(tp->name, soap->tmpbuf))
  10525. break;
  10526. }
  10527. if (!tp)
  10528. { tp = (struct soap_attribute*)SOAP_MALLOC(soap, sizeof(struct soap_attribute) + strlen(soap->tmpbuf));
  10529. if (!tp)
  10530. return soap->error = SOAP_EOM;
  10531. strcpy((char*)tp->name, soap->tmpbuf);
  10532. tp->value = NULL;
  10533. tp->size = 0;
  10534. tp->ns = NULL;
  10535. /* if attribute name is qualified, append it to the end of the list */
  10536. if (tq && strchr(soap->tmpbuf, ':'))
  10537. { tq->next = tp;
  10538. tp->next = NULL;
  10539. }
  10540. else
  10541. { tp->next = soap->attributes;
  10542. soap->attributes = tp;
  10543. }
  10544. }
  10545. while (soap_blank(c))
  10546. c = soap_get1(soap);
  10547. if (c == '=')
  10548. { do c = soap_getutf8(soap);
  10549. while (soap_blank(c));
  10550. if (c != SOAP_QT && c != SOAP_AP)
  10551. { soap_unget(soap, c);
  10552. c = ' '; /* blank delimiter */
  10553. }
  10554. if (soap_getattrval(soap, tp->value, tp->size, c))
  10555. {
  10556. #ifdef WITH_FAST
  10557. if (soap->error != SOAP_EOM)
  10558. return soap->error;
  10559. soap->error = SOAP_OK;
  10560. if (soap_store_lab(soap, tp->value, tp->size))
  10561. return soap->error;
  10562. if (tp->value)
  10563. SOAP_FREE(soap, tp->value);
  10564. tp->value = NULL;
  10565. for (;;)
  10566. { if (soap_getattrval(soap, soap->labbuf + soap->labidx, soap->lablen - soap->labidx, c))
  10567. { if (soap->error != SOAP_EOM)
  10568. return soap->error;
  10569. soap->error = SOAP_OK;
  10570. soap->labidx = soap->lablen;
  10571. if (soap_append_lab(soap, NULL, 0))
  10572. return soap->error;
  10573. }
  10574. else
  10575. break;
  10576. }
  10577. if (soap->labidx)
  10578. tp->size = soap->lablen;
  10579. else
  10580. { tp->size = strlen(soap->labbuf) + 1;
  10581. if (tp->size < SOAP_LABLEN)
  10582. tp->size = SOAP_LABLEN;
  10583. }
  10584. if (!(tp->value = (char*)SOAP_MALLOC(soap, tp->size)))
  10585. return soap->error = SOAP_EOM;
  10586. strcpy(tp->value, soap->labbuf);
  10587. #else
  10588. size_t n;
  10589. if (soap->error != SOAP_EOM)
  10590. return soap->error;
  10591. soap->error = SOAP_OK;
  10592. if (soap_new_block(soap) == NULL)
  10593. return soap->error;
  10594. for (;;)
  10595. { if (!(s = (char*)soap_push_block(soap, NULL, SOAP_BLKLEN)))
  10596. return soap->error;
  10597. if (soap_getattrval(soap, s, SOAP_BLKLEN, c))
  10598. { if (soap->error != SOAP_EOM)
  10599. return soap->error;
  10600. soap->error = SOAP_OK;
  10601. }
  10602. else
  10603. break;
  10604. }
  10605. n = tp->size + soap->blist->size;
  10606. if (!(s = (char*)SOAP_MALLOC(soap, n)))
  10607. return soap->error = SOAP_EOM;
  10608. if (tp->value)
  10609. { memcpy(s, tp->value, tp->size);
  10610. SOAP_FREE(soap, tp->value);
  10611. }
  10612. soap_save_block(soap, NULL, s + tp->size, 0);
  10613. tp->value = s;
  10614. tp->size = n;
  10615. #endif
  10616. }
  10617. do c = soap_get1(soap);
  10618. while (soap_blank(c));
  10619. tp->visible = 2; /* seen this attribute w/ value */
  10620. #ifdef WITH_DOM
  10621. if (att)
  10622. (*att)->data = soap_strdup(soap, tp->value);
  10623. #endif
  10624. }
  10625. else
  10626. tp->visible = 1; /* seen this attribute w/o value */
  10627. #ifdef WITH_DOM
  10628. if (att)
  10629. att = &(*att)->next;
  10630. #endif
  10631. if (t && tp->value)
  10632. { if (soap_push_namespace(soap, t, tp->value) == NULL)
  10633. return soap->error;
  10634. }
  10635. }
  10636. #ifdef WITH_DOM
  10637. if (att)
  10638. { soap->dom->nstr = soap_current_namespace(soap, soap->tag);
  10639. for (att = &soap->dom->atts; *att; att = &(*att)->next)
  10640. (*att)->nstr = soap_current_namespace(soap, (*att)->name);
  10641. }
  10642. #endif
  10643. if ((int)c == EOF)
  10644. return soap->error = SOAP_CHK_EOF;
  10645. if (!(soap->body = (c != '/')))
  10646. do c = soap_get1(soap);
  10647. while (soap_blank(c));
  10648. #ifdef WITH_DOM
  10649. if (soap->mode & SOAP_XML_DOM)
  10650. { if (!soap->body && soap->dom->prnt)
  10651. soap->dom = soap->dom->prnt;
  10652. }
  10653. #endif
  10654. for (tp = soap->attributes; tp; tp = tp->next)
  10655. { if (tp->visible && tp->value)
  10656. {
  10657. #ifndef WITH_NOIDREF
  10658. if (!strcmp(tp->name, "id"))
  10659. { if ((soap->version > 0 && !(soap->imode & SOAP_XML_TREE))
  10660. || (soap->mode & SOAP_XML_GRAPH))
  10661. { *soap->id = '#';
  10662. strncpy(soap->id + 1, tp->value, sizeof(soap->id) - 2);
  10663. soap->id[sizeof(soap->id) - 1] = '\0';
  10664. }
  10665. }
  10666. else if (!strcmp(tp->name, "href"))
  10667. { if ((soap->version == 1 && !(soap->imode & SOAP_XML_TREE))
  10668. || (soap->mode & SOAP_XML_GRAPH)
  10669. || (soap->mode & SOAP_ENC_MTOM)
  10670. || (soap->mode & SOAP_ENC_DIME))
  10671. { strncpy(soap->href, tp->value, sizeof(soap->href) - 1);
  10672. soap->href[sizeof(soap->href) - 1] = '\0';
  10673. }
  10674. }
  10675. else if (!strcmp(tp->name, "ref"))
  10676. { if ((soap->version == 2 && !(soap->imode & SOAP_XML_TREE))
  10677. || (soap->mode & SOAP_XML_GRAPH))
  10678. { *soap->href = '#';
  10679. strncpy(soap->href + (*tp->value != '#'), tp->value, sizeof(soap->href) - 2);
  10680. soap->href[sizeof(soap->href) - 1] = '\0';
  10681. }
  10682. }
  10683. else
  10684. #endif
  10685. if (!soap_match_tag(soap, tp->name, "xsi:type"))
  10686. { strncpy(soap->type, tp->value, sizeof(soap->type) - 1);
  10687. soap->type[sizeof(soap->type) - 1] = '\0';
  10688. }
  10689. else if ((!soap_match_tag(soap, tp->name, "xsi:null")
  10690. || !soap_match_tag(soap, tp->name, "xsi:nil"))
  10691. && (!strcmp(tp->value, "1")
  10692. || !strcmp(tp->value, "true")))
  10693. { soap->null = 1;
  10694. }
  10695. else if (soap->version == 1)
  10696. { if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arrayType"))
  10697. { s = soap_strrchr(tp->value, '[');
  10698. if (s && (size_t)(s - tp->value) < sizeof(soap->arrayType))
  10699. { strncpy(soap->arrayType, tp->value, s - tp->value);
  10700. soap->arrayType[s - tp->value] = '\0';
  10701. strncpy(soap->arraySize, s, sizeof(soap->arraySize));
  10702. }
  10703. else
  10704. strncpy(soap->arrayType, tp->value, sizeof(soap->arrayType));
  10705. soap->arraySize[sizeof(soap->arraySize) - 1] = '\0';
  10706. soap->arrayType[sizeof(soap->arrayType) - 1] = '\0';
  10707. }
  10708. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:offset"))
  10709. strncpy(soap->arrayOffset, tp->value, sizeof(soap->arrayOffset));
  10710. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:position"))
  10711. soap->position = soap_getposition(tp->value, soap->positions);
  10712. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:root"))
  10713. soap->root = ((!strcmp(tp->value, "1") || !strcmp(tp->value, "true")));
  10714. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  10715. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true")))
  10716. soap->mustUnderstand = 1;
  10717. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:actor"))
  10718. { if ((!soap->actor || strcmp(soap->actor, tp->value))
  10719. && strcmp(tp->value, "http://schemas.xmlsoap.org/soap/actor/next"))
  10720. soap->other = 1;
  10721. }
  10722. }
  10723. else if (soap->version == 2)
  10724. {
  10725. #ifndef WITH_NOIDREF
  10726. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:id"))
  10727. { *soap->id = '#';
  10728. strncpy(soap->id + 1, tp->value, sizeof(soap->id) - 2);
  10729. soap->id[sizeof(soap->id) - 1] = '\0';
  10730. }
  10731. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:ref"))
  10732. { *soap->href = '#';
  10733. strncpy(soap->href + (*tp->value != '#'), tp->value, sizeof(soap->href) - 2);
  10734. soap->href[sizeof(soap->href) - 1] = '\0';
  10735. }
  10736. else
  10737. #endif
  10738. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:itemType"))
  10739. strncpy(soap->arrayType, tp->value, sizeof(soap->arrayType) - 1);
  10740. else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arraySize"))
  10741. strncpy(soap->arraySize, tp->value, sizeof(soap->arraySize) - 1);
  10742. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  10743. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true")))
  10744. soap->mustUnderstand = 1;
  10745. else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:role"))
  10746. { if ((!soap->actor || strcmp(soap->actor, tp->value))
  10747. && strcmp(tp->value, "http://www.w3.org/2003/05/soap-envelope/role/next"))
  10748. soap->other = 1;
  10749. }
  10750. }
  10751. else
  10752. { if (!soap_match_tag(soap, tp->name, "wsdl:required") && !strcmp(tp->value, "true"))
  10753. soap->mustUnderstand = 1;
  10754. }
  10755. }
  10756. }
  10757. #ifdef WITH_DOM
  10758. if (soap->feltbegin)
  10759. return soap->error = soap->feltbegin(soap, soap->tag);
  10760. #endif
  10761. return soap->error = SOAP_OK;
  10762. }
  10763. #endif
  10764. /******************************************************************************/
  10765. #ifndef PALM_2
  10766. SOAP_FMAC1
  10767. void
  10768. SOAP_FMAC2
  10769. soap_retry(struct soap *soap)
  10770. { soap->error = SOAP_OK;
  10771. soap_revert(soap);
  10772. }
  10773. #endif
  10774. /******************************************************************************/
  10775. #ifndef PALM_2
  10776. SOAP_FMAC1
  10777. void
  10778. SOAP_FMAC2
  10779. soap_revert(struct soap *soap)
  10780. { if (!soap->peeked)
  10781. { soap->peeked = 1;
  10782. if (soap->body)
  10783. soap->level--;
  10784. }
  10785. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reverting to last element '%s' (level=%u)\n", soap->tag, soap->level));
  10786. }
  10787. #endif
  10788. /******************************************************************************/
  10789. #ifndef PALM_2
  10790. SOAP_FMAC1
  10791. int
  10792. SOAP_FMAC2
  10793. soap_string_out(struct soap *soap, const char *s, int flag)
  10794. { register const char *t;
  10795. register soap_wchar c;
  10796. register soap_wchar mask = (soap_wchar)0xFFFFFF80UL;
  10797. #ifdef WITH_DOM
  10798. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  10799. { soap->dom->data = soap_strdup(soap, s);
  10800. return SOAP_OK;
  10801. }
  10802. #endif
  10803. if (flag == 2 || soap->mode & SOAP_C_UTFSTRING)
  10804. mask = 0;
  10805. t = s;
  10806. while ((c = *t++))
  10807. { switch (c)
  10808. {
  10809. case 0x09:
  10810. if (flag)
  10811. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#x9;", 5))
  10812. return soap->error;
  10813. s = t;
  10814. }
  10815. break;
  10816. case 0x0A:
  10817. if (flag || !(soap->mode & SOAP_XML_CANONICAL))
  10818. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#xA;", 5))
  10819. return soap->error;
  10820. s = t;
  10821. }
  10822. break;
  10823. case 0x0D:
  10824. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#xD;", 5))
  10825. return soap->error;
  10826. s = t;
  10827. break;
  10828. case '&':
  10829. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&amp;", 5))
  10830. return soap->error;
  10831. s = t;
  10832. break;
  10833. case '<':
  10834. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&lt;", 4))
  10835. return soap->error;
  10836. s = t;
  10837. break;
  10838. case '>':
  10839. if (!flag)
  10840. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&gt;", 4))
  10841. return soap->error;
  10842. s = t;
  10843. }
  10844. break;
  10845. case '"':
  10846. if (flag)
  10847. { if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&quot;", 6))
  10848. return soap->error;
  10849. s = t;
  10850. }
  10851. break;
  10852. default:
  10853. #ifndef WITH_LEANER
  10854. #ifdef HAVE_MBTOWC
  10855. if (soap->mode & SOAP_C_MBSTRING)
  10856. { wchar_t wc;
  10857. register int m = mbtowc(&wc, t - 1, MB_CUR_MAX);
  10858. if (m > 0 && !((soap_wchar)wc == c && m == 1 && c < 0x80))
  10859. { if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned long)wc))
  10860. return soap->error;
  10861. s = t += m - 1;
  10862. continue;
  10863. }
  10864. }
  10865. #endif
  10866. #endif
  10867. #ifndef WITH_NOSTRINGTOUTF8
  10868. if ((c & mask) || !(c & 0xFFFFFFE0UL))
  10869. { if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned char)c))
  10870. return soap->error;
  10871. s = t;
  10872. }
  10873. #endif
  10874. }
  10875. }
  10876. return soap_send_raw(soap, s, t - s - 1);
  10877. }
  10878. #endif
  10879. /******************************************************************************/
  10880. #ifndef PALM_2
  10881. SOAP_FMAC1
  10882. char *
  10883. SOAP_FMAC2
  10884. soap_string_in(struct soap *soap, int flag, long minlen, long maxlen)
  10885. { register char *s;
  10886. char *t = NULL;
  10887. register size_t i;
  10888. register long l = 0;
  10889. register int n = 0, f = 0, m = 0;
  10890. register soap_wchar c;
  10891. #if !defined(WITH_LEANER) && defined(HAVE_WCTOMB)
  10892. char buf[MB_LEN_MAX > 8 ? MB_LEN_MAX : 8];
  10893. #else
  10894. char buf[8];
  10895. #endif
  10896. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Reading string content, flag=%d\n", flag));
  10897. if (soap->peeked && *soap->tag)
  10898. {
  10899. #ifndef WITH_LEAN
  10900. struct soap_attribute *tp;
  10901. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String content includes tag '%s' and attributes\n", soap->tag));
  10902. t = soap->tmpbuf;
  10903. *t = '<';
  10904. strncpy(t + 1, soap->tag, sizeof(soap->tmpbuf) - 2);
  10905. t[sizeof(soap->tmpbuf) - 1] = '\0';
  10906. t += strlen(t);
  10907. for (tp = soap->attributes; tp; tp = tp->next)
  10908. { if (tp->visible)
  10909. { if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  10910. break;
  10911. *t++ = ' ';
  10912. strcpy(t, tp->name);
  10913. t += strlen(t);
  10914. if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  10915. break; /* too many or large attribute values */
  10916. if (tp->value)
  10917. { *t++ = '=';
  10918. *t++ = '"';
  10919. strcpy(t, tp->value);
  10920. t += strlen(t);
  10921. *t++ = '"';
  10922. }
  10923. }
  10924. }
  10925. if (!soap->body)
  10926. *t++ = '/';
  10927. *t++ = '>';
  10928. *t = '\0';
  10929. t = soap->tmpbuf;
  10930. m = (int)strlen(soap->tmpbuf);
  10931. #endif
  10932. if (soap->body)
  10933. n = 1;
  10934. f = 1;
  10935. soap->peeked = 0;
  10936. }
  10937. #ifdef WITH_CDATA
  10938. if (!flag)
  10939. { register int state = 0;
  10940. #ifdef WITH_FAST
  10941. soap->labidx = 0; /* use look-aside buffer */
  10942. #else
  10943. if (soap_new_block(soap) == NULL)
  10944. return NULL;
  10945. #endif
  10946. for (;;)
  10947. {
  10948. #ifdef WITH_FAST
  10949. register size_t k;
  10950. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  10951. return NULL;
  10952. s = soap->labbuf + soap->labidx; /* space to populate */
  10953. k = soap->lablen - soap->labidx; /* number of bytes available */
  10954. soap->labidx = soap->lablen; /* claim this space */
  10955. #else
  10956. register size_t k = SOAP_BLKLEN;
  10957. if (!(s = (char*)soap_push_block(soap, NULL, k)))
  10958. return NULL;
  10959. #endif
  10960. for (i = 0; i < k; i++)
  10961. { if (m > 0)
  10962. { *s++ = *t++; /* copy multibyte characters */
  10963. m--;
  10964. continue;
  10965. }
  10966. c = soap_getchar(soap);
  10967. if ((int)c == EOF)
  10968. goto end;
  10969. if ((c >= 0x80 || c < SOAP_AP) && state != 1 && !(soap->mode & SOAP_ENC_LATIN))
  10970. { if ((c & 0x7FFFFFFF) >= 0x80)
  10971. { soap_unget(soap, c);
  10972. c = soap_getutf8(soap);
  10973. }
  10974. if ((c & 0x7FFFFFFF) >= 0x80 && (!flag || (soap->mode & SOAP_C_UTFSTRING)))
  10975. { c &= 0x7FFFFFFF;
  10976. t = buf;
  10977. if (c < 0x0800)
  10978. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  10979. else
  10980. { if (c < 0x010000)
  10981. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  10982. else
  10983. { if (c < 0x200000)
  10984. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  10985. else
  10986. { if (c < 0x04000000)
  10987. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  10988. else
  10989. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  10990. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  10991. }
  10992. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  10993. }
  10994. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  10995. }
  10996. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  10997. }
  10998. *t++ = (char)(0x80 | (c & 0x3F));
  10999. m = (int)(t - buf) - 1;
  11000. t = buf;
  11001. *s++ = *t++;
  11002. continue;
  11003. }
  11004. }
  11005. switch (state)
  11006. { case 1:
  11007. if (c == ']')
  11008. state = 4;
  11009. *s++ = (char)c;
  11010. continue;
  11011. case 2:
  11012. if (c == '-')
  11013. state = 6;
  11014. *s++ = (char)c;
  11015. continue;
  11016. case 3:
  11017. if (c == '?')
  11018. state = 8;
  11019. *s++ = (char)c;
  11020. continue;
  11021. /* CDATA */
  11022. case 4:
  11023. if (c == ']')
  11024. state = 5;
  11025. else
  11026. state = 1;
  11027. *s++ = (char)c;
  11028. continue;
  11029. case 5:
  11030. if (c == '>')
  11031. state = 0;
  11032. else if (c != ']')
  11033. state = 1;
  11034. *s++ = (char)c;
  11035. continue;
  11036. /* comment */
  11037. case 6:
  11038. if (c == '-')
  11039. state = 7;
  11040. else
  11041. state = 2;
  11042. *s++ = (char)c;
  11043. continue;
  11044. case 7:
  11045. if (c == '>')
  11046. state = 0;
  11047. else if (c != '-')
  11048. state = 2;
  11049. *s++ = (char)c;
  11050. continue;
  11051. /* PI */
  11052. case 8:
  11053. if (c == '>')
  11054. state = 0;
  11055. else if (c != '?')
  11056. state = 3;
  11057. *s++ = (char)c;
  11058. continue;
  11059. }
  11060. switch (c)
  11061. {
  11062. case SOAP_TT:
  11063. if (n == 0)
  11064. goto end;
  11065. n--;
  11066. *s++ = '<';
  11067. t = (char*)"/";
  11068. m = 1;
  11069. break;
  11070. case SOAP_LT:
  11071. if (f && n == 0)
  11072. goto end;
  11073. n++;
  11074. *s++ = '<';
  11075. break;
  11076. case SOAP_GT:
  11077. *s++ = '>';
  11078. break;
  11079. case SOAP_QT:
  11080. *s++ = '"';
  11081. break;
  11082. case SOAP_AP:
  11083. *s++ = '\'';
  11084. break;
  11085. case '/':
  11086. if (n > 0)
  11087. { c = soap_getchar(soap);
  11088. if (c == '>')
  11089. n--;
  11090. soap_unget(soap, c);
  11091. }
  11092. *s++ = '/';
  11093. break;
  11094. case '<':
  11095. c = soap_getchar(soap);
  11096. if (c == '/')
  11097. { if (n == 0)
  11098. { c = SOAP_TT;
  11099. goto end;
  11100. }
  11101. n--;
  11102. }
  11103. else if (c == '!')
  11104. { c = soap_getchar(soap);
  11105. if (c == '[')
  11106. { do c = soap_getchar(soap);
  11107. while ((int)c != EOF && c != '[');
  11108. if ((int)c == EOF)
  11109. goto end;
  11110. t = (char*)"![CDATA[";
  11111. m = 8;
  11112. state = 1;
  11113. }
  11114. else if (c == '-')
  11115. { if ((c = soap_getchar(soap)) == '-')
  11116. state = 2;
  11117. t = (char*)"!-";
  11118. m = 2;
  11119. soap_unget(soap, c);
  11120. }
  11121. else
  11122. { t = (char*)"!";
  11123. m = 1;
  11124. soap_unget(soap, c);
  11125. }
  11126. *s++ = '<';
  11127. break;
  11128. }
  11129. else if (c == '?')
  11130. state = 3;
  11131. else if (f && n == 0)
  11132. { soap_revget1(soap);
  11133. c = '<';
  11134. goto end;
  11135. }
  11136. else
  11137. n++;
  11138. soap_unget(soap, c);
  11139. *s++ = '<';
  11140. break;
  11141. case '>':
  11142. *s++ = '>';
  11143. break;
  11144. case '"':
  11145. *s++ = '"';
  11146. break;
  11147. default:
  11148. #ifndef WITH_LEANER
  11149. #ifdef HAVE_WCTOMB
  11150. if (soap->mode & SOAP_C_MBSTRING)
  11151. { m = wctomb(buf, (wchar_t)(c & 0x7FFFFFFF));
  11152. if (m >= 1 && m <= (int)MB_CUR_MAX)
  11153. { t = buf;
  11154. *s++ = *t++;
  11155. m--;
  11156. }
  11157. else
  11158. { *s++ = SOAP_UNKNOWN_CHAR;
  11159. m = 0;
  11160. }
  11161. }
  11162. else
  11163. #endif
  11164. #endif
  11165. *s++ = (char)(c & 0xFF);
  11166. }
  11167. l++;
  11168. if (maxlen >= 0 && l > maxlen)
  11169. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  11170. soap->error = SOAP_LENGTH;
  11171. return NULL;
  11172. }
  11173. }
  11174. }
  11175. }
  11176. #endif
  11177. #ifdef WITH_FAST
  11178. soap->labidx = 0; /* use look-aside buffer */
  11179. #else
  11180. if (soap_new_block(soap) == NULL)
  11181. return NULL;
  11182. #endif
  11183. for (;;)
  11184. {
  11185. #ifdef WITH_FAST
  11186. register size_t k;
  11187. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  11188. return NULL;
  11189. s = soap->labbuf + soap->labidx; /* space to populate */
  11190. k = soap->lablen - soap->labidx; /* number of bytes available */
  11191. soap->labidx = soap->lablen; /* claim this space */
  11192. #else
  11193. register size_t k = SOAP_BLKLEN;
  11194. if (!(s = (char*)soap_push_block(soap, NULL, k)))
  11195. return NULL;
  11196. #endif
  11197. for (i = 0; i < k; i++)
  11198. { if (m > 0)
  11199. { *s++ = *t++; /* copy multibyte characters */
  11200. m--;
  11201. continue;
  11202. }
  11203. #ifndef WITH_CDATA
  11204. if (!flag)
  11205. c = soap_getchar(soap);
  11206. else
  11207. #endif
  11208. if ((soap->mode & SOAP_C_UTFSTRING))
  11209. { if (((c = soap_get(soap)) & 0x80000000) && c >= -0x7FFFFF80 && c < SOAP_AP)
  11210. { c &= 0x7FFFFFFF;
  11211. t = buf;
  11212. if (c < 0x0800)
  11213. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  11214. else
  11215. { if (c < 0x010000)
  11216. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  11217. else
  11218. { if (c < 0x200000)
  11219. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  11220. else
  11221. { if (c < 0x04000000)
  11222. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  11223. else
  11224. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  11225. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  11226. }
  11227. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  11228. }
  11229. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  11230. }
  11231. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  11232. }
  11233. *t++ = (char)(0x80 | (c & 0x3F));
  11234. m = (int)(t - buf) - 1;
  11235. t = buf;
  11236. *s++ = *t++;
  11237. continue;
  11238. }
  11239. }
  11240. else
  11241. c = soap_getutf8(soap);
  11242. switch (c)
  11243. {
  11244. case SOAP_TT:
  11245. if (n == 0)
  11246. goto end;
  11247. n--;
  11248. *s++ = '<';
  11249. t = (char*)"/";
  11250. m = 1;
  11251. break;
  11252. case SOAP_LT:
  11253. if (f && n == 0)
  11254. goto end;
  11255. n++;
  11256. *s++ = '<';
  11257. break;
  11258. case SOAP_GT:
  11259. *s++ = '>';
  11260. break;
  11261. case SOAP_QT:
  11262. *s++ = '"';
  11263. break;
  11264. case SOAP_AP:
  11265. *s++ = '\'';
  11266. break;
  11267. case '/':
  11268. if (n > 0)
  11269. { if (!flag)
  11270. { c = soap_getchar(soap);
  11271. if (c == '>')
  11272. n--;
  11273. }
  11274. else
  11275. { c = soap_get(soap);
  11276. if (c == SOAP_GT)
  11277. n--;
  11278. }
  11279. soap_unget(soap, c);
  11280. }
  11281. *s++ = '/';
  11282. break;
  11283. case (soap_wchar)('<' | 0x80000000):
  11284. if (flag)
  11285. *s++ = '<';
  11286. else
  11287. { *s++ = '&';
  11288. t = (char*)"lt;";
  11289. m = 3;
  11290. }
  11291. break;
  11292. case (soap_wchar)('>' | 0x80000000):
  11293. if (flag)
  11294. *s++ = '>';
  11295. else
  11296. { *s++ = '&';
  11297. t = (char*)"gt;";
  11298. m = 3;
  11299. }
  11300. break;
  11301. case (soap_wchar)('&' | 0x80000000):
  11302. if (flag)
  11303. *s++ = '&';
  11304. else
  11305. { *s++ = '&';
  11306. t = (char*)"amp;";
  11307. m = 4;
  11308. }
  11309. break;
  11310. case (soap_wchar)('"' | 0x80000000):
  11311. if (flag)
  11312. *s++ = '"';
  11313. else
  11314. { *s++ = '&';
  11315. t = (char*)"quot;";
  11316. m = 5;
  11317. }
  11318. break;
  11319. case (soap_wchar)('\'' | 0x80000000):
  11320. if (flag)
  11321. *s++ = '\'';
  11322. else
  11323. { *s++ = '&';
  11324. t = (char*)"apos;";
  11325. m = 5;
  11326. }
  11327. break;
  11328. default:
  11329. if ((int)c == EOF)
  11330. goto end;
  11331. #ifndef WITH_CDATA
  11332. if (c == '<' && !flag)
  11333. { if (f && n == 0)
  11334. goto end;
  11335. c = soap_getchar(soap);
  11336. soap_unget(soap, c);
  11337. if (c == '/')
  11338. { c = SOAP_TT;
  11339. if (n == 0)
  11340. goto end;
  11341. n--;
  11342. }
  11343. else
  11344. n++;
  11345. *s++ = '<';
  11346. break;
  11347. }
  11348. else
  11349. #endif
  11350. #ifndef WITH_LEANER
  11351. #ifdef HAVE_WCTOMB
  11352. if (soap->mode & SOAP_C_MBSTRING)
  11353. { m = wctomb(buf, (wchar_t)(c & 0x7FFFFFFF));
  11354. if (m >= 1 && m <= (int)MB_CUR_MAX)
  11355. { t = buf;
  11356. *s++ = *t++;
  11357. m--;
  11358. }
  11359. else
  11360. { *s++ = SOAP_UNKNOWN_CHAR;
  11361. m = 0;
  11362. }
  11363. }
  11364. else
  11365. #endif
  11366. #endif
  11367. *s++ = (char)(c & 0xFF);
  11368. }
  11369. l++;
  11370. if (maxlen >= 0 && l > maxlen)
  11371. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  11372. soap->error = SOAP_LENGTH;
  11373. return NULL;
  11374. }
  11375. }
  11376. }
  11377. end:
  11378. soap_unget(soap, c);
  11379. *s = '\0';
  11380. #ifdef WITH_FAST
  11381. t = soap_strdup(soap, soap->labbuf);
  11382. #else
  11383. soap_size_block(soap, NULL, i + 1);
  11384. t = soap_save_block(soap, NULL, 0);
  11385. #endif
  11386. if (l < minlen)
  11387. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too short: %ld chars, minlen=%ld\n", l, minlen));
  11388. soap->error = SOAP_LENGTH;
  11389. return NULL;
  11390. }
  11391. #ifdef WITH_DOM
  11392. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  11393. { if (flag == 3)
  11394. soap->dom->tail = t;
  11395. else
  11396. soap->dom->data = t;
  11397. }
  11398. #endif
  11399. if (flag == 2)
  11400. if (soap_s2QName(soap, t, &t, minlen, maxlen))
  11401. return NULL;
  11402. return t;
  11403. }
  11404. #endif
  11405. /******************************************************************************/
  11406. #ifndef WITH_LEANER
  11407. #ifndef PALM_2
  11408. SOAP_FMAC1
  11409. int
  11410. SOAP_FMAC2
  11411. soap_wstring_out(struct soap *soap, const wchar_t *s, int flag)
  11412. { const char *t;
  11413. char tmp;
  11414. register soap_wchar c;
  11415. #ifdef WITH_DOM
  11416. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  11417. { wchar_t *r = (wchar_t*)s;
  11418. int n = 1;
  11419. while (*r++)
  11420. n++;
  11421. soap->dom->wide = r = (wchar_t*)soap_malloc(soap, n * sizeof(wchar_t));
  11422. while (n--)
  11423. *r++ = *s++;
  11424. return SOAP_OK;
  11425. }
  11426. #endif
  11427. while ((c = *s++))
  11428. { switch (c)
  11429. {
  11430. case 0x09:
  11431. if (flag)
  11432. t = "&#x9;";
  11433. else
  11434. t = "\t";
  11435. break;
  11436. case 0x0A:
  11437. if (flag || !(soap->mode & SOAP_XML_CANONICAL))
  11438. t = "&#xA;";
  11439. else
  11440. t = "\n";
  11441. break;
  11442. case 0x0D:
  11443. t = "&#xD;";
  11444. break;
  11445. case '&':
  11446. t = "&amp;";
  11447. break;
  11448. case '<':
  11449. t = "&lt;";
  11450. break;
  11451. case '>':
  11452. if (flag)
  11453. t = ">";
  11454. else
  11455. t = "&gt;";
  11456. break;
  11457. case '"':
  11458. if (flag)
  11459. t = "&quot;";
  11460. else
  11461. t = "\"";
  11462. break;
  11463. default:
  11464. if (c >= 0x20 && c < 0x80)
  11465. { tmp = (char)c;
  11466. if (soap_send_raw(soap, &tmp, 1))
  11467. return soap->error;
  11468. }
  11469. else
  11470. { /* check for UTF16 encoding when wchar_t is too small to hold UCS */
  11471. if (sizeof(wchar_t) < 4 && (c & 0xFC00) == 0xD800)
  11472. { register soap_wchar d = *s++;
  11473. if ((d & 0xFC00) == 0xDC00)
  11474. c = ((c - 0xD800) << 10) + (d - 0xDC00) + 0x10000;
  11475. else
  11476. c = 0xFFFD; /* Malformed */
  11477. }
  11478. if (soap_pututf8(soap, (unsigned long)c))
  11479. return soap->error;
  11480. }
  11481. continue;
  11482. }
  11483. if (soap_send(soap, t))
  11484. return soap->error;
  11485. }
  11486. return SOAP_OK;
  11487. }
  11488. #endif
  11489. #endif
  11490. /******************************************************************************/
  11491. #ifndef WITH_LEANER
  11492. #ifndef PALM_2
  11493. SOAP_FMAC1
  11494. wchar_t *
  11495. SOAP_FMAC2
  11496. soap_wstring_in(struct soap *soap, int flag, long minlen, long maxlen)
  11497. { wchar_t *s;
  11498. register int i, n = 0, f = 0;
  11499. register long l = 0;
  11500. register soap_wchar c;
  11501. char *t = NULL;
  11502. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Reading wide string content\n"));
  11503. if (soap->peeked)
  11504. { if (*soap->tag)
  11505. {
  11506. #ifndef WITH_LEAN
  11507. struct soap_attribute *tp;
  11508. t = soap->tmpbuf;
  11509. *t = '<';
  11510. strncpy(t + 1, soap->tag, sizeof(soap->tmpbuf) - 2);
  11511. t[sizeof(soap->tmpbuf) - 1] = '\0';
  11512. t += strlen(t);
  11513. for (tp = soap->attributes; tp; tp = tp->next)
  11514. { if (tp->visible)
  11515. { if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  11516. break;
  11517. *t++ = ' ';
  11518. strcpy(t, tp->name);
  11519. t += strlen(t);
  11520. if (t >= soap->tmpbuf + sizeof(soap->tmpbuf) - 2)
  11521. break;
  11522. if (tp->value)
  11523. { *t++ = '=';
  11524. *t++ = '"';
  11525. strcpy(t, tp->value);
  11526. t += strlen(t);
  11527. *t++ = '"';
  11528. }
  11529. }
  11530. }
  11531. if (!soap->body)
  11532. *t++ = '/';
  11533. *t++ = '>';
  11534. *t = '\0';
  11535. t = soap->tmpbuf;
  11536. #endif
  11537. if (soap->body)
  11538. n = 1;
  11539. f = 1;
  11540. soap->peeked = 0;
  11541. }
  11542. }
  11543. if (soap_new_block(soap) == NULL)
  11544. return NULL;
  11545. for (;;)
  11546. { if (!(s = (wchar_t*)soap_push_block(soap, NULL, sizeof(wchar_t)*SOAP_BLKLEN)))
  11547. return NULL;
  11548. for (i = 0; i < SOAP_BLKLEN; i++)
  11549. { if (t)
  11550. { *s++ = (wchar_t)*t++;
  11551. if (!*t)
  11552. t = NULL;
  11553. continue;
  11554. }
  11555. c = soap_getutf8(soap);
  11556. switch (c)
  11557. {
  11558. case SOAP_TT:
  11559. if (n == 0)
  11560. goto end;
  11561. n--;
  11562. *s++ = '<';
  11563. soap_unget(soap, '/');
  11564. break;
  11565. case SOAP_LT:
  11566. if (f && n == 0)
  11567. goto end;
  11568. n++;
  11569. *s++ = '<';
  11570. break;
  11571. case SOAP_GT:
  11572. *s++ = '>';
  11573. break;
  11574. case SOAP_QT:
  11575. *s++ = '"';
  11576. break;
  11577. case SOAP_AP:
  11578. *s++ = '\'';
  11579. break;
  11580. case '/':
  11581. if (n > 0)
  11582. { c = soap_getutf8(soap);
  11583. if (c == SOAP_GT)
  11584. n--;
  11585. soap_unget(soap, c);
  11586. }
  11587. *s++ = '/';
  11588. break;
  11589. case '<':
  11590. if (flag)
  11591. *s++ = (soap_wchar)'<';
  11592. else
  11593. { *s++ = (soap_wchar)'&';
  11594. t = (char*)"lt;";
  11595. }
  11596. break;
  11597. case '>':
  11598. if (flag)
  11599. *s++ = (soap_wchar)'>';
  11600. else
  11601. { *s++ = (soap_wchar)'&';
  11602. t = (char*)"gt;";
  11603. }
  11604. break;
  11605. case '"':
  11606. if (flag)
  11607. *s++ = (soap_wchar)'"';
  11608. else
  11609. { *s++ = (soap_wchar)'&';
  11610. t = (char*)"quot;";
  11611. }
  11612. break;
  11613. default:
  11614. if ((int)c == EOF)
  11615. goto end;
  11616. /* use UTF16 encoding when wchar_t is too small to hold UCS */
  11617. if (sizeof(wchar_t) < 4 && c > 0xFFFF)
  11618. { register soap_wchar c1, c2;
  11619. c1 = 0xD800 - (0x10000 >> 10) + (c >> 10);
  11620. c2 = 0xDC00 + (c & 0x3FF);
  11621. c = c1;
  11622. soap_unget(soap, c2);
  11623. }
  11624. *s++ = (wchar_t)c & 0x7FFFFFFF;
  11625. }
  11626. l++;
  11627. if (maxlen >= 0 && l > maxlen)
  11628. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  11629. soap->error = SOAP_LENGTH;
  11630. return NULL;
  11631. }
  11632. }
  11633. }
  11634. end:
  11635. soap_unget(soap, c);
  11636. *s = '\0';
  11637. soap_size_block(soap, NULL, sizeof(wchar_t) * (i + 1));
  11638. if (l < minlen)
  11639. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "String too short: %ld chars, minlen=%ld\n", l, minlen));
  11640. soap->error = SOAP_LENGTH;
  11641. return NULL;
  11642. }
  11643. s = (wchar_t*)soap_save_block(soap, NULL, NULL, 0);
  11644. #ifdef WITH_DOM
  11645. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  11646. soap->dom->wide = s;
  11647. #endif
  11648. return s;
  11649. }
  11650. #endif
  11651. #endif
  11652. /******************************************************************************/
  11653. #ifndef PALM_2
  11654. SOAP_FMAC1
  11655. const char*
  11656. SOAP_FMAC2
  11657. soap_int2s(struct soap *soap, int n)
  11658. { return soap_long2s(soap, (long)n);
  11659. }
  11660. #endif
  11661. /******************************************************************************/
  11662. #ifndef PALM_2
  11663. SOAP_FMAC1
  11664. int
  11665. SOAP_FMAC2
  11666. soap_outint(struct soap *soap, const char *tag, int id, const int *p, const char *type, int n)
  11667. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11668. || soap_string_out(soap, soap_long2s(soap, (long)*p), 0))
  11669. return soap->error;
  11670. return soap_element_end_out(soap, tag);
  11671. }
  11672. #endif
  11673. /******************************************************************************/
  11674. #ifndef PALM_2
  11675. SOAP_FMAC1
  11676. int
  11677. SOAP_FMAC2
  11678. soap_s2int(struct soap *soap, const char *s, int *p)
  11679. { if (s)
  11680. { long n;
  11681. char *r;
  11682. #ifndef WITH_NOIO
  11683. #ifndef WITH_LEAN
  11684. soap_reset_errno;
  11685. #endif
  11686. #endif
  11687. n = soap_strtol(s, &r, 10);
  11688. if (s == r || *r
  11689. #ifndef WITH_LEAN
  11690. || n != (int)n
  11691. #endif
  11692. #ifndef WITH_NOIO
  11693. #ifndef WITH_LEAN
  11694. || soap_errno == SOAP_ERANGE
  11695. #endif
  11696. #endif
  11697. )
  11698. soap->error = SOAP_TYPE;
  11699. *p = (int)n;
  11700. }
  11701. return soap->error;
  11702. }
  11703. #endif
  11704. /******************************************************************************/
  11705. #ifndef PALM_2
  11706. SOAP_FMAC1
  11707. int *
  11708. SOAP_FMAC2
  11709. soap_inint(struct soap *soap, const char *tag, int *p, const char *type, int t)
  11710. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11711. return NULL;
  11712. #ifndef WITH_LEAN
  11713. if (*soap->type
  11714. && soap_match_tag(soap, soap->type, type)
  11715. && soap_match_tag(soap, soap->type, ":int")
  11716. && soap_match_tag(soap, soap->type, ":short")
  11717. && soap_match_tag(soap, soap->type, ":byte"))
  11718. { soap->error = SOAP_TYPE;
  11719. soap_revert(soap);
  11720. return NULL;
  11721. }
  11722. #endif
  11723. p = (int*)soap_id_enter(soap, soap->id, p, t, sizeof(int), 0, NULL, NULL, NULL);
  11724. if (*soap->href)
  11725. p = (int*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(int), 0, NULL);
  11726. else if (p)
  11727. { if (soap_s2int(soap, soap_value(soap), p))
  11728. return NULL;
  11729. }
  11730. if (soap->body && soap_element_end_in(soap, tag))
  11731. return NULL;
  11732. return p;
  11733. }
  11734. #endif
  11735. /******************************************************************************/
  11736. #ifndef PALM_2
  11737. SOAP_FMAC1
  11738. const char*
  11739. SOAP_FMAC2
  11740. soap_long2s(struct soap *soap, long n)
  11741. {
  11742. #ifdef HAVE_SNPRINTF
  11743. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%ld", n);
  11744. #else
  11745. sprintf(soap->tmpbuf, "%ld", n);
  11746. #endif
  11747. return soap->tmpbuf;
  11748. }
  11749. #endif
  11750. /******************************************************************************/
  11751. #ifndef PALM_2
  11752. SOAP_FMAC1
  11753. int
  11754. SOAP_FMAC2
  11755. soap_outlong(struct soap *soap, const char *tag, int id, const long *p, const char *type, int n)
  11756. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11757. || soap_string_out(soap, soap_long2s(soap, *p), 0))
  11758. return soap->error;
  11759. return soap_element_end_out(soap, tag);
  11760. }
  11761. #endif
  11762. /******************************************************************************/
  11763. #ifndef PALM_2
  11764. SOAP_FMAC1
  11765. int
  11766. SOAP_FMAC2
  11767. soap_s2long(struct soap *soap, const char *s, long *p)
  11768. { if (s)
  11769. { char *r;
  11770. #ifndef WITH_NOIO
  11771. #ifndef WITH_LEAN
  11772. soap_reset_errno;
  11773. #endif
  11774. #endif
  11775. *p = soap_strtol(s, &r, 10);
  11776. if (s == r || *r
  11777. #ifndef WITH_NOIO
  11778. #ifndef WITH_LEAN
  11779. || soap_errno == SOAP_ERANGE
  11780. #endif
  11781. #endif
  11782. )
  11783. soap->error = SOAP_TYPE;
  11784. }
  11785. return soap->error;
  11786. }
  11787. #endif
  11788. /******************************************************************************/
  11789. #ifndef PALM_2
  11790. SOAP_FMAC1
  11791. long *
  11792. SOAP_FMAC2
  11793. soap_inlong(struct soap *soap, const char *tag, long *p, const char *type, int t)
  11794. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11795. return NULL;
  11796. #ifndef WITH_LEAN
  11797. if (*soap->type
  11798. && soap_match_tag(soap, soap->type, type)
  11799. && soap_match_tag(soap, soap->type, ":int")
  11800. && soap_match_tag(soap, soap->type, ":short")
  11801. && soap_match_tag(soap, soap->type, ":byte"))
  11802. { soap->error = SOAP_TYPE;
  11803. soap_revert(soap);
  11804. return NULL;
  11805. }
  11806. #endif
  11807. p = (long*)soap_id_enter(soap, soap->id, p, t, sizeof(long), 0, NULL, NULL, NULL);
  11808. if (*soap->href)
  11809. p = (long*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(long), 0, NULL);
  11810. else if (p)
  11811. { if (soap_s2long(soap, soap_value(soap), p))
  11812. return NULL;
  11813. }
  11814. if (soap->body && soap_element_end_in(soap, tag))
  11815. return NULL;
  11816. return p;
  11817. }
  11818. #endif
  11819. /******************************************************************************/
  11820. #ifndef WITH_LEAN
  11821. SOAP_FMAC1
  11822. const char*
  11823. SOAP_FMAC2
  11824. soap_LONG642s(struct soap *soap, LONG64 n)
  11825. {
  11826. #ifdef HAVE_SNPRINTF
  11827. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), SOAP_LONG_FORMAT, n);
  11828. #else
  11829. sprintf(soap->tmpbuf, SOAP_LONG_FORMAT, n);
  11830. #endif
  11831. return soap->tmpbuf;
  11832. }
  11833. #endif
  11834. /******************************************************************************/
  11835. #ifndef WITH_LEAN
  11836. SOAP_FMAC1
  11837. int
  11838. SOAP_FMAC2
  11839. soap_outLONG64(struct soap *soap, const char *tag, int id, const LONG64 *p, const char *type, int n)
  11840. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11841. || soap_string_out(soap, soap_LONG642s(soap, *p), 0))
  11842. return soap->error;
  11843. return soap_element_end_out(soap, tag);
  11844. }
  11845. #endif
  11846. /******************************************************************************/
  11847. #ifndef WITH_LEAN
  11848. SOAP_FMAC1
  11849. int
  11850. SOAP_FMAC2
  11851. soap_s2LONG64(struct soap *soap, const char *s, LONG64 *p)
  11852. { if (s)
  11853. {
  11854. #ifdef HAVE_STRTOLL
  11855. char *r;
  11856. #ifndef WITH_NOIO
  11857. #ifndef WITH_LEAN
  11858. soap_reset_errno;
  11859. #endif
  11860. #endif
  11861. *p = soap_strtoll(s, &r, 10);
  11862. if (s == r || *r
  11863. #ifndef WITH_NOIO
  11864. #ifndef WITH_LEAN
  11865. || soap_errno == SOAP_ERANGE
  11866. #endif
  11867. #endif
  11868. )
  11869. #else
  11870. # ifdef HAVE_SSCANF
  11871. if (sscanf(s, SOAP_LONG_FORMAT, p) != 1)
  11872. # endif
  11873. #endif
  11874. soap->error = SOAP_TYPE;
  11875. }
  11876. return soap->error;
  11877. }
  11878. #endif
  11879. /******************************************************************************/
  11880. #ifndef WITH_LEAN
  11881. SOAP_FMAC1
  11882. LONG64 *
  11883. SOAP_FMAC2
  11884. soap_inLONG64(struct soap *soap, const char *tag, LONG64 *p, const char *type, int t)
  11885. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11886. return NULL;
  11887. #ifndef WITH_LEAN
  11888. if (*soap->type
  11889. && soap_match_tag(soap, soap->type, type)
  11890. && soap_match_tag(soap, soap->type, ":integer")
  11891. && soap_match_tag(soap, soap->type, ":positiveInteger")
  11892. && soap_match_tag(soap, soap->type, ":negativeInteger")
  11893. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  11894. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  11895. && soap_match_tag(soap, soap->type, ":long")
  11896. && soap_match_tag(soap, soap->type, ":int")
  11897. && soap_match_tag(soap, soap->type, ":short")
  11898. && soap_match_tag(soap, soap->type, ":byte"))
  11899. { soap->error = SOAP_TYPE;
  11900. soap_revert(soap);
  11901. return NULL;
  11902. }
  11903. #endif
  11904. p = (LONG64*)soap_id_enter(soap, soap->id, p, t, sizeof(LONG64), 0, NULL, NULL, NULL);
  11905. if (*soap->href)
  11906. p = (LONG64*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(LONG64), 0, NULL);
  11907. else if (p)
  11908. { if (soap_s2LONG64(soap, soap_value(soap), p))
  11909. return NULL;
  11910. }
  11911. if (soap->body && soap_element_end_in(soap, tag))
  11912. return NULL;
  11913. return p;
  11914. }
  11915. #endif
  11916. /******************************************************************************/
  11917. #ifndef PALM_2
  11918. SOAP_FMAC1
  11919. const char*
  11920. SOAP_FMAC2
  11921. soap_byte2s(struct soap *soap, char n)
  11922. { return soap_long2s(soap, (long)n);
  11923. }
  11924. #endif
  11925. /******************************************************************************/
  11926. #ifndef PALM_2
  11927. SOAP_FMAC1
  11928. int
  11929. SOAP_FMAC2
  11930. soap_outbyte(struct soap *soap, const char *tag, int id, const char *p, const char *type, int n)
  11931. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11932. || soap_string_out(soap, soap_long2s(soap, (long)*p), 0))
  11933. return soap->error;
  11934. return soap_element_end_out(soap, tag);
  11935. }
  11936. #endif
  11937. /******************************************************************************/
  11938. #ifndef PALM_2
  11939. SOAP_FMAC1
  11940. int
  11941. SOAP_FMAC2
  11942. soap_s2byte(struct soap *soap, const char *s, char *p)
  11943. { if (s)
  11944. { long n;
  11945. char *r;
  11946. n = soap_strtol(s, &r, 10);
  11947. if (s == r || *r || n < -128 || n > 127)
  11948. soap->error = SOAP_TYPE;
  11949. *p = (char)n;
  11950. }
  11951. return soap->error;
  11952. }
  11953. #endif
  11954. /******************************************************************************/
  11955. #ifndef PALM_2
  11956. SOAP_FMAC1
  11957. char *
  11958. SOAP_FMAC2
  11959. soap_inbyte(struct soap *soap, const char *tag, char *p, const char *type, int t)
  11960. { if (soap_element_begin_in(soap, tag, 0, NULL))
  11961. return NULL;
  11962. #ifndef WITH_LEAN
  11963. if (*soap->type
  11964. && soap_match_tag(soap, soap->type, type)
  11965. && soap_match_tag(soap, soap->type, ":byte"))
  11966. { soap->error = SOAP_TYPE;
  11967. soap_revert(soap);
  11968. return NULL;
  11969. }
  11970. #endif
  11971. p = (char*)soap_id_enter(soap, soap->id, p, t, sizeof(char), 0, NULL, NULL, NULL);
  11972. if (*soap->href)
  11973. p = (char*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(char), 0, NULL);
  11974. else if (p)
  11975. { if (soap_s2byte(soap, soap_value(soap), p))
  11976. return NULL;
  11977. }
  11978. if (soap->body && soap_element_end_in(soap, tag))
  11979. return NULL;
  11980. return p;
  11981. }
  11982. #endif
  11983. /******************************************************************************/
  11984. #ifndef PALM_2
  11985. SOAP_FMAC1
  11986. const char*
  11987. SOAP_FMAC2
  11988. soap_short2s(struct soap *soap, short n)
  11989. { return soap_long2s(soap, (long)n);
  11990. }
  11991. #endif
  11992. /******************************************************************************/
  11993. #ifndef PALM_2
  11994. SOAP_FMAC1
  11995. int
  11996. SOAP_FMAC2
  11997. soap_outshort(struct soap *soap, const char *tag, int id, const short *p, const char *type, int n)
  11998. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  11999. || soap_string_out(soap, soap_long2s(soap, (long)*p), 0))
  12000. return soap->error;
  12001. return soap_element_end_out(soap, tag);
  12002. }
  12003. #endif
  12004. /******************************************************************************/
  12005. #ifndef PALM_2
  12006. SOAP_FMAC1
  12007. int
  12008. SOAP_FMAC2
  12009. soap_s2short(struct soap *soap, const char *s, short *p)
  12010. { if (s)
  12011. { long n;
  12012. char *r;
  12013. n = soap_strtol(s, &r, 10);
  12014. if (s == r || *r || n < -32768 || n > 32767)
  12015. soap->error = SOAP_TYPE;
  12016. *p = (short)n;
  12017. }
  12018. return soap->error;
  12019. }
  12020. #endif
  12021. /******************************************************************************/
  12022. #ifndef PALM_2
  12023. SOAP_FMAC1
  12024. short *
  12025. SOAP_FMAC2
  12026. soap_inshort(struct soap *soap, const char *tag, short *p, const char *type, int t)
  12027. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12028. return NULL;
  12029. #ifndef WITH_LEAN
  12030. if (*soap->type
  12031. && soap_match_tag(soap, soap->type, type)
  12032. && soap_match_tag(soap, soap->type, ":short")
  12033. && soap_match_tag(soap, soap->type, ":byte"))
  12034. { soap->error = SOAP_TYPE;
  12035. soap_revert(soap);
  12036. return NULL;
  12037. }
  12038. #endif
  12039. p = (short*)soap_id_enter(soap, soap->id, p, t, sizeof(short), 0, NULL, NULL, NULL);
  12040. if (*soap->href)
  12041. p = (short*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(short), 0, NULL);
  12042. else if (p)
  12043. { if (soap_s2short(soap, soap_value(soap), p))
  12044. return NULL;
  12045. }
  12046. if (soap->body && soap_element_end_in(soap, tag))
  12047. return NULL;
  12048. return p;
  12049. }
  12050. #endif
  12051. /******************************************************************************/
  12052. #ifndef PALM_2
  12053. SOAP_FMAC1
  12054. const char*
  12055. SOAP_FMAC2
  12056. soap_float2s(struct soap *soap, float n)
  12057. { char *s;
  12058. if (soap_isnan((double)n))
  12059. return "NaN";
  12060. if (soap_ispinff(n))
  12061. return "INF";
  12062. if (soap_isninff(n))
  12063. return "-INF";
  12064. #if defined(HAVE_SPRINTF_L)
  12065. # ifdef WIN32
  12066. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->float_format, soap->c_locale, n);
  12067. # else
  12068. sprintf_l(soap->tmpbuf, soap->c_locale, soap->float_format, n);
  12069. # endif
  12070. #else
  12071. # ifdef HAVE_SNPRINTF
  12072. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), soap->float_format, n);
  12073. # else
  12074. sprintf(soap->tmpbuf, soap->float_format, n);
  12075. # endif
  12076. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  12077. if (s)
  12078. *s = '.';
  12079. #endif
  12080. return soap->tmpbuf;
  12081. }
  12082. #endif
  12083. /******************************************************************************/
  12084. #ifndef PALM_2
  12085. SOAP_FMAC1
  12086. int
  12087. SOAP_FMAC2
  12088. soap_outfloat(struct soap *soap, const char *tag, int id, const float *p, const char *type, int n)
  12089. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12090. || soap_string_out(soap, soap_float2s(soap, *p), 0))
  12091. return soap->error;
  12092. return soap_element_end_out(soap, tag);
  12093. }
  12094. #endif
  12095. /******************************************************************************/
  12096. #ifndef PALM_2
  12097. SOAP_FMAC1
  12098. int
  12099. SOAP_FMAC2
  12100. soap_s2float(struct soap *soap, const char *s, float *p)
  12101. { if (s)
  12102. { if (!*s)
  12103. return soap->error = SOAP_TYPE;
  12104. if (!soap_tag_cmp(s, "INF"))
  12105. *p = FLT_PINFTY;
  12106. else if (!soap_tag_cmp(s, "+INF"))
  12107. *p = FLT_PINFTY;
  12108. else if (!soap_tag_cmp(s, "-INF"))
  12109. *p = FLT_NINFTY;
  12110. else if (!soap_tag_cmp(s, "NaN"))
  12111. *p = FLT_NAN;
  12112. else
  12113. {
  12114. /* On some systems strtof requires -std=c99 or does not even link: so we try to use strtod first */
  12115. #if defined(HAVE_STRTOD_L)
  12116. char *r;
  12117. # ifdef WIN32
  12118. *p = (float)_strtod_l(s, &r, soap->c_locale);
  12119. # else
  12120. *p = (float)strtod_l(s, &r, soap->c_locale);
  12121. # endif
  12122. if (*r)
  12123. #elif defined(HAVE_STRTOD)
  12124. char *r;
  12125. *p = (float)strtod(s, &r);
  12126. if (*r)
  12127. #elif defined(HAVE_STRTOF_L)
  12128. char *r;
  12129. *p = strtof_l((char*)s, &r, soap->c_locale);
  12130. if (*r)
  12131. #elif defined(HAVE_STRTOF)
  12132. char *r;
  12133. *p = strtof((char*)s, &r);
  12134. if (*r)
  12135. #endif
  12136. {
  12137. #if defined(HAVE_SSCANF_L) && !defined(HAVE_STRTOF_L) && !defined(HAVE_STRTOD_L)
  12138. if (sscanf_l(s, soap->c_locale, "%f", p) != 1)
  12139. soap->error = SOAP_TYPE;
  12140. #elif defined(HAVE_SSCANF)
  12141. if (sscanf(s, "%f", p) != 1)
  12142. soap->error = SOAP_TYPE;
  12143. #else
  12144. soap->error = SOAP_TYPE;
  12145. #endif
  12146. }
  12147. }
  12148. }
  12149. return soap->error;
  12150. }
  12151. #endif
  12152. /******************************************************************************/
  12153. #ifndef WITH_LEAN
  12154. static int soap_isnumeric(struct soap *soap, const char *type)
  12155. { if (soap_match_tag(soap, soap->type, type)
  12156. && soap_match_tag(soap, soap->type, ":float")
  12157. && soap_match_tag(soap, soap->type, ":double")
  12158. && soap_match_tag(soap, soap->type, ":decimal")
  12159. && soap_match_tag(soap, soap->type, ":integer")
  12160. && soap_match_tag(soap, soap->type, ":positiveInteger")
  12161. && soap_match_tag(soap, soap->type, ":negativeInteger")
  12162. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  12163. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  12164. && soap_match_tag(soap, soap->type, ":long")
  12165. && soap_match_tag(soap, soap->type, ":int")
  12166. && soap_match_tag(soap, soap->type, ":short")
  12167. && soap_match_tag(soap, soap->type, ":byte")
  12168. && soap_match_tag(soap, soap->type, ":unsignedLong")
  12169. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12170. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12171. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12172. { soap->error = SOAP_TYPE;
  12173. soap_revert(soap);
  12174. return SOAP_ERR;
  12175. }
  12176. return SOAP_OK;
  12177. }
  12178. #endif
  12179. /******************************************************************************/
  12180. #ifndef PALM_2
  12181. SOAP_FMAC1
  12182. float *
  12183. SOAP_FMAC2
  12184. soap_infloat(struct soap *soap, const char *tag, float *p, const char *type, int t)
  12185. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12186. return NULL;
  12187. #ifndef WITH_LEAN
  12188. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  12189. return NULL;
  12190. #endif
  12191. p = (float*)soap_id_enter(soap, soap->id, p, t, sizeof(float), 0, NULL, NULL, NULL);
  12192. if (*soap->href)
  12193. p = (float*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(float), 0, NULL);
  12194. else if (p)
  12195. { if (soap_s2float(soap, soap_value(soap), p))
  12196. return NULL;
  12197. }
  12198. if (soap->body && soap_element_end_in(soap, tag))
  12199. return NULL;
  12200. return p;
  12201. }
  12202. #endif
  12203. /******************************************************************************/
  12204. #ifndef PALM_2
  12205. SOAP_FMAC1
  12206. const char*
  12207. SOAP_FMAC2
  12208. soap_double2s(struct soap *soap, double n)
  12209. { char *s;
  12210. if (soap_isnan(n))
  12211. return "NaN";
  12212. if (soap_ispinfd(n))
  12213. return "INF";
  12214. if (soap_isninfd(n))
  12215. return "-INF";
  12216. #if defined(HAVE_SPRINTF_L)
  12217. # ifdef WIN32
  12218. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->double_format, soap->c_locale, n);
  12219. # else
  12220. sprintf_l(soap->tmpbuf, soap->c_locale, soap->double_format, n);
  12221. # endif
  12222. #else
  12223. # ifdef HAVE_SNPRINTF
  12224. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), soap->double_format, n);
  12225. #else
  12226. sprintf(soap->tmpbuf, soap->double_format, n);
  12227. #endif
  12228. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  12229. if (s)
  12230. *s = '.';
  12231. #endif
  12232. return soap->tmpbuf;
  12233. }
  12234. #endif
  12235. /******************************************************************************/
  12236. #ifndef PALM_2
  12237. SOAP_FMAC1
  12238. int
  12239. SOAP_FMAC2
  12240. soap_outdouble(struct soap *soap, const char *tag, int id, const double *p, const char *type, int n)
  12241. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12242. || soap_string_out(soap, soap_double2s(soap, *p), 0))
  12243. return soap->error;
  12244. return soap_element_end_out(soap, tag);
  12245. }
  12246. #endif
  12247. /******************************************************************************/
  12248. #ifndef PALM_2
  12249. SOAP_FMAC1
  12250. int
  12251. SOAP_FMAC2
  12252. soap_s2double(struct soap *soap, const char *s, double *p)
  12253. { if (s)
  12254. { if (!*s)
  12255. return soap->error = SOAP_TYPE;
  12256. if (!soap_tag_cmp(s, "INF"))
  12257. *p = DBL_PINFTY;
  12258. else if (!soap_tag_cmp(s, "+INF"))
  12259. *p = DBL_PINFTY;
  12260. else if (!soap_tag_cmp(s, "-INF"))
  12261. *p = DBL_NINFTY;
  12262. else if (!soap_tag_cmp(s, "NaN"))
  12263. *p = DBL_NAN;
  12264. else
  12265. {
  12266. #if defined(HAVE_STRTOD_L)
  12267. char *r;
  12268. # ifdef WIN32
  12269. *p = _strtod_l(s, &r, soap->c_locale);
  12270. # else
  12271. *p = strtod_l(s, &r, soap->c_locale);
  12272. # endif
  12273. if (*r)
  12274. #elif defined(HAVE_STRTOD)
  12275. char *r;
  12276. *p = strtod(s, &r);
  12277. if (*r)
  12278. #endif
  12279. {
  12280. #if defined(HAVE_SSCANF_L) && !defined(HAVE_STRTOF_L) && !defined(HAVE_STRTOD_L)
  12281. if (sscanf_l(s, soap->c_locale, "%lf", p) != 1)
  12282. soap->error = SOAP_TYPE;
  12283. #elif defined(HAVE_SSCANF)
  12284. if (sscanf(s, "%lf", p) != 1)
  12285. soap->error = SOAP_TYPE;
  12286. #else
  12287. soap->error = SOAP_TYPE;
  12288. #endif
  12289. }
  12290. }
  12291. }
  12292. return soap->error;
  12293. }
  12294. #endif
  12295. /******************************************************************************/
  12296. #ifndef PALM_2
  12297. SOAP_FMAC1
  12298. double *
  12299. SOAP_FMAC2
  12300. soap_indouble(struct soap *soap, const char *tag, double *p, const char *type, int t)
  12301. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12302. return NULL;
  12303. #ifndef WITH_LEAN
  12304. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  12305. return NULL;
  12306. #endif
  12307. p = (double*)soap_id_enter(soap, soap->id, p, t, sizeof(double), 0, NULL, NULL, NULL);
  12308. if (*soap->href)
  12309. p = (double*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(double), 0, NULL);
  12310. else if (p)
  12311. { if (soap_s2double(soap, soap_value(soap), p))
  12312. return NULL;
  12313. }
  12314. if (soap->body && soap_element_end_in(soap, tag))
  12315. return NULL;
  12316. return p;
  12317. }
  12318. #endif
  12319. /******************************************************************************/
  12320. #ifndef PALM_2
  12321. SOAP_FMAC1
  12322. const char*
  12323. SOAP_FMAC2
  12324. soap_unsignedByte2s(struct soap *soap, unsigned char n)
  12325. { return soap_unsignedLong2s(soap, (unsigned long)n);
  12326. }
  12327. #endif
  12328. /******************************************************************************/
  12329. #ifndef PALM_2
  12330. SOAP_FMAC1
  12331. int
  12332. SOAP_FMAC2
  12333. soap_outunsignedByte(struct soap *soap, const char *tag, int id, const unsigned char *p, const char *type, int n)
  12334. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12335. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long)*p), 0))
  12336. return soap->error;
  12337. return soap_element_end_out(soap, tag);
  12338. }
  12339. #endif
  12340. /******************************************************************************/
  12341. #ifndef PALM_2
  12342. SOAP_FMAC1
  12343. int
  12344. SOAP_FMAC2
  12345. soap_s2unsignedByte(struct soap *soap, const char *s, unsigned char *p)
  12346. { if (s)
  12347. { unsigned long n;
  12348. char *r;
  12349. n = soap_strtoul(s, &r, 10);
  12350. if (s == r || *r || n > 255)
  12351. soap->error = SOAP_TYPE;
  12352. *p = (unsigned char)n;
  12353. }
  12354. return soap->error;
  12355. }
  12356. #endif
  12357. /******************************************************************************/
  12358. #ifndef PALM_2
  12359. SOAP_FMAC1
  12360. unsigned char *
  12361. SOAP_FMAC2
  12362. soap_inunsignedByte(struct soap *soap, const char *tag, unsigned char *p, const char *type, int t)
  12363. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12364. return NULL;
  12365. #ifndef WITH_LEAN
  12366. if (*soap->type
  12367. && soap_match_tag(soap, soap->type, type)
  12368. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12369. { soap->error = SOAP_TYPE;
  12370. soap_revert(soap);
  12371. return NULL;
  12372. }
  12373. #endif
  12374. p = (unsigned char*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned char), 0, NULL, NULL, NULL);
  12375. if (*soap->href)
  12376. p = (unsigned char*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned char), 0, NULL);
  12377. else if (p)
  12378. { if (soap_s2unsignedByte(soap, soap_value(soap), p))
  12379. return NULL;
  12380. }
  12381. if (soap->body && soap_element_end_in(soap, tag))
  12382. return NULL;
  12383. return p;
  12384. }
  12385. #endif
  12386. /******************************************************************************/
  12387. #ifndef PALM_2
  12388. SOAP_FMAC1
  12389. const char*
  12390. SOAP_FMAC2
  12391. soap_unsignedShort2s(struct soap *soap, unsigned short n)
  12392. { return soap_unsignedLong2s(soap, (unsigned long)n);
  12393. }
  12394. #endif
  12395. /******************************************************************************/
  12396. #ifndef PALM_2
  12397. SOAP_FMAC1
  12398. int
  12399. SOAP_FMAC2
  12400. soap_outunsignedShort(struct soap *soap, const char *tag, int id, const unsigned short *p, const char *type, int n)
  12401. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12402. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long)*p), 0))
  12403. return soap->error;
  12404. return soap_element_end_out(soap, tag);
  12405. }
  12406. #endif
  12407. /******************************************************************************/
  12408. #ifndef PALM_2
  12409. SOAP_FMAC1
  12410. int
  12411. SOAP_FMAC2
  12412. soap_s2unsignedShort(struct soap *soap, const char *s, unsigned short *p)
  12413. { if (s)
  12414. { unsigned long n;
  12415. char *r;
  12416. n = soap_strtoul(s, &r, 10);
  12417. if (s == r || *r || n > 65535)
  12418. soap->error = SOAP_TYPE;
  12419. *p = (unsigned short)n;
  12420. }
  12421. return soap->error;
  12422. }
  12423. #endif
  12424. /******************************************************************************/
  12425. #ifndef PALM_2
  12426. SOAP_FMAC1
  12427. unsigned short *
  12428. SOAP_FMAC2
  12429. soap_inunsignedShort(struct soap *soap, const char *tag, unsigned short *p, const char *type, int t)
  12430. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12431. return NULL;
  12432. #ifndef WITH_LEAN
  12433. if (*soap->type
  12434. && soap_match_tag(soap, soap->type, type)
  12435. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12436. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12437. { soap->error = SOAP_TYPE;
  12438. soap_revert(soap);
  12439. return NULL;
  12440. }
  12441. #endif
  12442. p = (unsigned short*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned short), 0, NULL, NULL, NULL);
  12443. if (*soap->href)
  12444. p = (unsigned short*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned short), 0, NULL);
  12445. else if (p)
  12446. { if (soap_s2unsignedShort(soap, soap_value(soap), p))
  12447. return NULL;
  12448. }
  12449. if (soap->body && soap_element_end_in(soap, tag))
  12450. return NULL;
  12451. return p;
  12452. }
  12453. #endif
  12454. /******************************************************************************/
  12455. #ifndef PALM_2
  12456. SOAP_FMAC1
  12457. const char*
  12458. SOAP_FMAC2
  12459. soap_unsignedInt2s(struct soap *soap, unsigned int n)
  12460. { return soap_unsignedLong2s(soap, (unsigned long)n);
  12461. }
  12462. #endif
  12463. /******************************************************************************/
  12464. #ifndef PALM_2
  12465. SOAP_FMAC1
  12466. int
  12467. SOAP_FMAC2
  12468. soap_outunsignedInt(struct soap *soap, const char *tag, int id, const unsigned int *p, const char *type, int n)
  12469. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12470. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long)*p), 0))
  12471. return soap->error;
  12472. return soap_element_end_out(soap, tag);
  12473. }
  12474. #endif
  12475. /******************************************************************************/
  12476. #ifndef PALM_2
  12477. SOAP_FMAC1
  12478. int
  12479. SOAP_FMAC2
  12480. soap_s2unsignedInt(struct soap *soap, const char *s, unsigned int *p)
  12481. { if (s)
  12482. { char *r;
  12483. #ifndef WITH_NOIO
  12484. #ifndef WITH_LEAN
  12485. soap_reset_errno;
  12486. #endif
  12487. #endif
  12488. *p = (unsigned int)soap_strtoul(s, &r, 10);
  12489. if ((s == r && (soap->mode & SOAP_XML_STRICT)) || *r
  12490. #ifndef WITH_NOIO
  12491. #ifndef WITH_LEAN
  12492. || soap_errno == SOAP_ERANGE
  12493. #endif
  12494. #endif
  12495. )
  12496. soap->error = SOAP_TYPE;
  12497. }
  12498. return soap->error;
  12499. }
  12500. #endif
  12501. /******************************************************************************/
  12502. #ifndef PALM_2
  12503. SOAP_FMAC1
  12504. unsigned int *
  12505. SOAP_FMAC2
  12506. soap_inunsignedInt(struct soap *soap, const char *tag, unsigned int *p, const char *type, int t)
  12507. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12508. return NULL;
  12509. #ifndef WITH_LEAN
  12510. if (*soap->type
  12511. && soap_match_tag(soap, soap->type, type)
  12512. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12513. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12514. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12515. { soap->error = SOAP_TYPE;
  12516. soap_revert(soap);
  12517. return NULL;
  12518. }
  12519. #endif
  12520. p = (unsigned int*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned int), 0, NULL, NULL, NULL);
  12521. if (*soap->href)
  12522. p = (unsigned int*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned int), 0, NULL);
  12523. else if (p)
  12524. { if (soap_s2unsignedInt(soap, soap_value(soap), p))
  12525. return NULL;
  12526. }
  12527. if (soap->body && soap_element_end_in(soap, tag))
  12528. return NULL;
  12529. return p;
  12530. }
  12531. #endif
  12532. /******************************************************************************/
  12533. #ifndef PALM_2
  12534. SOAP_FMAC1
  12535. const char*
  12536. SOAP_FMAC2
  12537. soap_unsignedLong2s(struct soap *soap, unsigned long n)
  12538. {
  12539. #ifdef HAVE_SNPRINTF
  12540. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%lu", n);
  12541. #else
  12542. sprintf(soap->tmpbuf, "%lu", n);
  12543. #endif
  12544. return soap->tmpbuf;
  12545. }
  12546. #endif
  12547. /******************************************************************************/
  12548. #ifndef PALM_2
  12549. SOAP_FMAC1
  12550. int
  12551. SOAP_FMAC2
  12552. soap_outunsignedLong(struct soap *soap, const char *tag, int id, const unsigned long *p, const char *type, int n)
  12553. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12554. || soap_string_out(soap, soap_unsignedLong2s(soap, *p), 0))
  12555. return soap->error;
  12556. return soap_element_end_out(soap, tag);
  12557. }
  12558. #endif
  12559. /******************************************************************************/
  12560. #ifndef PALM_2
  12561. SOAP_FMAC1
  12562. int
  12563. SOAP_FMAC2
  12564. soap_s2unsignedLong(struct soap *soap, const char *s, unsigned long *p)
  12565. { if (s)
  12566. { char *r;
  12567. #ifndef WITH_NOIO
  12568. #ifndef WITH_LEAN
  12569. soap_reset_errno;
  12570. #endif
  12571. #endif
  12572. *p = soap_strtoul(s, &r, 10);
  12573. if ((s == r && (soap->mode & SOAP_XML_STRICT)) || *r
  12574. #ifndef WITH_NOIO
  12575. #ifndef WITH_LEAN
  12576. || soap_errno == SOAP_ERANGE
  12577. #endif
  12578. #endif
  12579. )
  12580. soap->error = SOAP_TYPE;
  12581. }
  12582. return soap->error;
  12583. }
  12584. #endif
  12585. /******************************************************************************/
  12586. #ifndef PALM_2
  12587. SOAP_FMAC1
  12588. unsigned long *
  12589. SOAP_FMAC2
  12590. soap_inunsignedLong(struct soap *soap, const char *tag, unsigned long *p, const char *type, int t)
  12591. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12592. return NULL;
  12593. #ifndef WITH_LEAN
  12594. if (*soap->type
  12595. && soap_match_tag(soap, soap->type, type)
  12596. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12597. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12598. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12599. { soap->error = SOAP_TYPE;
  12600. soap_revert(soap);
  12601. return NULL;
  12602. }
  12603. #endif
  12604. p = (unsigned long*)soap_id_enter(soap, soap->id, p, t, sizeof(unsigned long), 0, NULL, NULL, NULL);
  12605. if (*soap->href)
  12606. p = (unsigned long*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(unsigned long), 0, NULL);
  12607. else if (p)
  12608. { if (soap_s2unsignedLong(soap, soap_value(soap), p))
  12609. return NULL;
  12610. }
  12611. if (soap->body && soap_element_end_in(soap, tag))
  12612. return NULL;
  12613. return p;
  12614. }
  12615. #endif
  12616. /******************************************************************************/
  12617. #ifndef WITH_LEAN
  12618. SOAP_FMAC1
  12619. const char*
  12620. SOAP_FMAC2
  12621. soap_ULONG642s(struct soap *soap, ULONG64 n)
  12622. {
  12623. #ifdef HAVE_SNPRINTF
  12624. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), SOAP_ULONG_FORMAT, n);
  12625. #else
  12626. sprintf(soap->tmpbuf, SOAP_ULONG_FORMAT, n);
  12627. #endif
  12628. return soap->tmpbuf;
  12629. }
  12630. #endif
  12631. /******************************************************************************/
  12632. #ifndef WITH_LEAN
  12633. SOAP_FMAC1
  12634. int
  12635. SOAP_FMAC2
  12636. soap_outULONG64(struct soap *soap, const char *tag, int id, const ULONG64 *p, const char *type, int n)
  12637. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  12638. || soap_string_out(soap, soap_ULONG642s(soap, *p), 0))
  12639. return soap->error;
  12640. return soap_element_end_out(soap, tag);
  12641. }
  12642. #endif
  12643. /******************************************************************************/
  12644. #ifndef WITH_LEAN
  12645. SOAP_FMAC1
  12646. int
  12647. SOAP_FMAC2
  12648. soap_s2ULONG64(struct soap *soap, const char *s, ULONG64 *p)
  12649. { if (s)
  12650. {
  12651. #ifdef HAVE_STRTOULL
  12652. char *r;
  12653. #ifndef WITH_NOIO
  12654. #ifndef WITH_LEAN
  12655. soap_reset_errno;
  12656. #endif
  12657. #endif
  12658. *p = soap_strtoull(s, &r, 10);
  12659. if ((s == r && (soap->mode & SOAP_XML_STRICT)) || *r
  12660. #ifndef WITH_NOIO
  12661. #ifndef WITH_LEAN
  12662. || soap_errno == SOAP_ERANGE
  12663. #endif
  12664. #endif
  12665. )
  12666. #else
  12667. #ifdef HAVE_SSCANF
  12668. if (sscanf(s, SOAP_ULONG_FORMAT, p) != 1)
  12669. #endif
  12670. #endif
  12671. soap->error = SOAP_TYPE;
  12672. }
  12673. return soap->error;
  12674. }
  12675. #endif
  12676. /******************************************************************************/
  12677. #ifndef WITH_LEAN
  12678. SOAP_FMAC1
  12679. ULONG64 *
  12680. SOAP_FMAC2
  12681. soap_inULONG64(struct soap *soap, const char *tag, ULONG64 *p, const char *type, int t)
  12682. { if (soap_element_begin_in(soap, tag, 0, NULL))
  12683. return NULL;
  12684. if (*soap->type
  12685. && soap_match_tag(soap, soap->type, type)
  12686. && soap_match_tag(soap, soap->type, ":positiveInteger")
  12687. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  12688. && soap_match_tag(soap, soap->type, ":unsignedLong")
  12689. && soap_match_tag(soap, soap->type, ":unsignedInt")
  12690. && soap_match_tag(soap, soap->type, ":unsignedShort")
  12691. && soap_match_tag(soap, soap->type, ":unsignedByte"))
  12692. { soap->error = SOAP_TYPE;
  12693. soap_revert(soap);
  12694. return NULL;
  12695. }
  12696. p = (ULONG64*)soap_id_enter(soap, soap->id, p, t, sizeof(ULONG64), 0, NULL, NULL, NULL);
  12697. if (*soap->href)
  12698. p = (ULONG64*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(ULONG64), 0, NULL);
  12699. else if (p)
  12700. { if (soap_s2ULONG64(soap, soap_value(soap), p))
  12701. return NULL;
  12702. }
  12703. if (soap->body && soap_element_end_in(soap, tag))
  12704. return NULL;
  12705. return p;
  12706. }
  12707. #endif
  12708. /******************************************************************************/
  12709. #ifndef PALM_2
  12710. SOAP_FMAC1
  12711. int
  12712. SOAP_FMAC2
  12713. soap_s2string(struct soap *soap, const char *s, char **t, long minlen, long maxlen)
  12714. { if (s)
  12715. { long l = (long)strlen(s);
  12716. if ((maxlen >= 0 && l > maxlen) || l < minlen)
  12717. return soap->error = SOAP_LENGTH;
  12718. if (!(*t = soap_strdup(soap, s)))
  12719. return soap->error = SOAP_EOM;
  12720. if (!(soap->mode & (SOAP_ENC_LATIN | SOAP_C_UTFSTRING)))
  12721. { char *r = *t;
  12722. /* remove non-ASCII chars */
  12723. for (s = *t; *s; s++)
  12724. if (!(*s & 0x80))
  12725. *r++ = *s;
  12726. *r = '\0';
  12727. }
  12728. }
  12729. return soap->error;
  12730. }
  12731. #endif
  12732. /******************************************************************************/
  12733. #ifndef PALM_2
  12734. SOAP_FMAC1
  12735. int
  12736. SOAP_FMAC2
  12737. soap_s2QName(struct soap *soap, const char *s, char **t, long minlen, long maxlen)
  12738. { if (s)
  12739. { long l = (long)strlen(s);
  12740. if ((maxlen >= 0 && l > maxlen) || l < minlen)
  12741. return soap->error = SOAP_LENGTH;
  12742. soap->labidx = 0;
  12743. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Normalized namespace(s) of QNames '%s'", s));
  12744. /* convert (by prefix normalize prefix) all QNames in s */
  12745. for (;;)
  12746. { size_t n;
  12747. struct soap_nlist *np;
  12748. register const char *p;
  12749. /* skip blanks */
  12750. while (*s && soap_blank((soap_wchar)*s))
  12751. s++;
  12752. if (!*s)
  12753. break;
  12754. /* find next QName */
  12755. n = 1;
  12756. while (s[n] && !soap_blank((soap_wchar)s[n]))
  12757. n++;
  12758. np = soap->nlist;
  12759. /* if there is no namespace stack, or prefix is "#" or "xml" then copy string */
  12760. if (!np || *s == '#' || !strncmp(s, "xml:", 4))
  12761. { soap_append_lab(soap, s, n);
  12762. }
  12763. else /* we normalize the QName by replacing its prefix */
  12764. { const char *q;
  12765. for (p = s; *p && p < s + n; p++)
  12766. if (*p == ':')
  12767. break;
  12768. if (*p == ':')
  12769. { size_t k = p - s;
  12770. while (np && (strncmp(np->id, s, k) || np->id[k]))
  12771. np = np->next;
  12772. p++;
  12773. }
  12774. else
  12775. { while (np && *np->id)
  12776. np = np->next;
  12777. p = s;
  12778. }
  12779. /* replace prefix */
  12780. if (np)
  12781. { if (np->index >= 0 && soap->local_namespaces && (q = soap->local_namespaces[np->index].id))
  12782. { size_t k = strlen(q);
  12783. if (q[k-1] != '_')
  12784. soap_append_lab(soap, q, k);
  12785. else
  12786. { soap_append_lab(soap, "\"", 1);
  12787. soap_append_lab(soap, soap->local_namespaces[np->index].ns, strlen(soap->local_namespaces[np->index].ns));
  12788. soap_append_lab(soap, "\"", 1);
  12789. }
  12790. }
  12791. else if (np->ns)
  12792. { soap_append_lab(soap, "\"", 1);
  12793. soap_append_lab(soap, np->ns, strlen(np->ns));
  12794. soap_append_lab(soap, "\"", 1);
  12795. }
  12796. else
  12797. { 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));
  12798. return soap->error = SOAP_NAMESPACE;
  12799. }
  12800. }
  12801. else if (s[n]) /* no namespace, part of string */
  12802. { soap_append_lab(soap, s, n);
  12803. }
  12804. else /* no namespace: assume "" namespace */
  12805. { soap_append_lab(soap, "\"\"", 2);
  12806. }
  12807. soap_append_lab(soap, ":", 1);
  12808. soap_append_lab(soap, p, n - (p-s));
  12809. }
  12810. /* advance to next and add spacing */
  12811. s += n;
  12812. if (*s)
  12813. soap_append_lab(soap, " ", 1);
  12814. }
  12815. soap_append_lab(soap, SOAP_STR_EOS, 1);
  12816. *t = soap_strdup(soap, soap->labbuf);
  12817. DBGLOG(TEST, SOAP_MESSAGE(fdebug, " into '%s'\n", *t));
  12818. }
  12819. return soap->error;
  12820. }
  12821. #endif
  12822. /******************************************************************************/
  12823. #ifndef PALM_2
  12824. SOAP_FMAC1
  12825. const char*
  12826. SOAP_FMAC2
  12827. soap_QName2s(struct soap *soap, const char *s)
  12828. { const char *t = NULL;
  12829. if (s)
  12830. { soap->labidx = 0;
  12831. for (;;)
  12832. { size_t n;
  12833. /* skip blanks */
  12834. while (*s && soap_blank((soap_wchar)*s))
  12835. s++;
  12836. if (!*s)
  12837. break;
  12838. /* find next QName */
  12839. n = 1;
  12840. while (s[n] && !soap_blank((soap_wchar)s[n]))
  12841. n++;
  12842. /* normal prefix: pass string as is */
  12843. if (*s != '"')
  12844. {
  12845. #ifndef WITH_LEAN
  12846. if ((soap->mode & SOAP_XML_CANONICAL))
  12847. soap_utilize_ns(soap, s);
  12848. if ((soap->mode & SOAP_XML_DEFAULTNS))
  12849. { const char *r = strchr(s, ':');
  12850. if (r && soap->nlist && !strncmp(soap->nlist->id, s, r-s) && !soap->nlist->id[r-s])
  12851. { n -= r-s + 1;
  12852. s = r + 1;
  12853. }
  12854. }
  12855. #endif
  12856. soap_append_lab(soap, s, n);
  12857. }
  12858. else /* URL-based string prefix */
  12859. { const char *q;
  12860. s++;
  12861. q = strchr(s, '"');
  12862. if (q)
  12863. { struct Namespace *p = soap->local_namespaces;
  12864. if (p)
  12865. { for (; p->id; p++)
  12866. { if (p->ns)
  12867. if (!soap_tag_cmp(s, p->ns))
  12868. break;
  12869. if (p->in)
  12870. if (!soap_tag_cmp(s, p->in))
  12871. break;
  12872. }
  12873. }
  12874. /* URL is in the namespace table? */
  12875. if (p && p->id)
  12876. { const char *r = p->id;
  12877. #ifndef WITH_LEAN
  12878. if ((soap->mode & SOAP_XML_DEFAULTNS) && soap->nlist && !strcmp(soap->nlist->id, r))
  12879. q++;
  12880. else
  12881. #endif
  12882. soap_append_lab(soap, r, strlen(r));
  12883. }
  12884. else /* not in namespace table: create xmlns binding */
  12885. { char *r = soap_strdup(soap, s);
  12886. r[q-s] = '\0';
  12887. #ifdef HAVE_SNPRINTF
  12888. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns:_%d", soap->idnum++);
  12889. #else
  12890. sprintf(soap->tmpbuf, "xmlns:_%d", soap->idnum++);
  12891. #endif
  12892. soap_set_attr(soap, soap->tmpbuf, r, 1);
  12893. soap_append_lab(soap, soap->tmpbuf + 6, strlen(soap->tmpbuf + 6));
  12894. }
  12895. soap_append_lab(soap, q + 1, n - (q-s) - 1);
  12896. }
  12897. }
  12898. /* advance to next and add spacing */
  12899. s += n;
  12900. if (*s)
  12901. soap_append_lab(soap, " ", 1);
  12902. }
  12903. soap_append_lab(soap, SOAP_STR_EOS, 1);
  12904. t = soap_strdup(soap, soap->labbuf);
  12905. }
  12906. return t;
  12907. }
  12908. #endif
  12909. /******************************************************************************/
  12910. #ifndef WITH_LEAN
  12911. SOAP_FMAC1
  12912. int
  12913. SOAP_FMAC2
  12914. soap_s2wchar(struct soap *soap, const char *s, wchar_t **t, long minlen, long maxlen)
  12915. { if (s)
  12916. { long l;
  12917. wchar_t *r;
  12918. *t = r = (wchar_t*)soap_malloc(soap, sizeof(wchar_t) * (strlen(s) + 1));
  12919. if (!r)
  12920. return soap->error = SOAP_EOM;
  12921. if (soap->mode & SOAP_ENC_LATIN)
  12922. { while (*s)
  12923. *r++ = (wchar_t)*s++;
  12924. }
  12925. else
  12926. { /* Convert UTF8 to wchar */
  12927. while (*s)
  12928. { register soap_wchar c, c1, c2, c3, c4;
  12929. c = (unsigned char)*s++;
  12930. if (c < 0x80)
  12931. *r++ = (wchar_t)c;
  12932. else
  12933. { c1 = (soap_wchar)*s++ & 0x3F;
  12934. if (c < 0xE0)
  12935. *r++ = (wchar_t)(((soap_wchar)(c & 0x1F) << 6) | c1);
  12936. else
  12937. { c2 = (soap_wchar)*s++ & 0x3F;
  12938. if (c < 0xF0)
  12939. *r++ = (wchar_t)(((soap_wchar)(c & 0x0F) << 12) | (c1 << 6) | c2);
  12940. else
  12941. { c3 = (soap_wchar)*s++ & 0x3F;
  12942. if (c < 0xF8)
  12943. *r++ = (wchar_t)(((soap_wchar)(c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3);
  12944. else
  12945. { c4 = (soap_wchar)*s++ & 0x3F;
  12946. if (c < 0xFC)
  12947. *r++ = (wchar_t)(((soap_wchar)(c & 0x03) << 24) | (c1 << 18) | (c2 << 12) | (c3 << 6) | c4);
  12948. else
  12949. *r++ = (wchar_t)(((soap_wchar)(c & 0x01) << 30) | (c1 << 24) | (c2 << 18) | (c3 << 12) | (c4 << 6) | (soap_wchar)(*s++ & 0x3F));
  12950. }
  12951. }
  12952. }
  12953. }
  12954. }
  12955. }
  12956. *r = L'\0';
  12957. l = (long)(r - *t);
  12958. if ((maxlen >= 0 && l > maxlen) || l < minlen)
  12959. return soap->error = SOAP_LENGTH;
  12960. }
  12961. return soap->error;
  12962. }
  12963. #endif
  12964. /******************************************************************************/
  12965. #ifndef WITH_LEAN
  12966. SOAP_FMAC1
  12967. const char*
  12968. SOAP_FMAC2
  12969. soap_wchar2s(struct soap *soap, const wchar_t *s)
  12970. { register soap_wchar c;
  12971. register char *r, *t;
  12972. const wchar_t *q = s;
  12973. size_t n = 0;
  12974. while ((c = *q++))
  12975. { if (c > 0 && c < 0x80)
  12976. n++;
  12977. else
  12978. n += 6;
  12979. }
  12980. r = t = (char*)soap_malloc(soap, n + 1);
  12981. if (r)
  12982. { /* Convert wchar to UTF8 */
  12983. while ((c = *s++))
  12984. { if (c > 0 && c < 0x80)
  12985. *t++ = (char)c;
  12986. else
  12987. { if (c < 0x0800)
  12988. *t++ = (char)(0xC0 | ((c >> 6) & 0x1F));
  12989. else
  12990. { if (c < 0x010000)
  12991. *t++ = (char)(0xE0 | ((c >> 12) & 0x0F));
  12992. else
  12993. { if (c < 0x200000)
  12994. *t++ = (char)(0xF0 | ((c >> 18) & 0x07));
  12995. else
  12996. { if (c < 0x04000000)
  12997. *t++ = (char)(0xF8 | ((c >> 24) & 0x03));
  12998. else
  12999. { *t++ = (char)(0xFC | ((c >> 30) & 0x01));
  13000. *t++ = (char)(0x80 | ((c >> 24) & 0x3F));
  13001. }
  13002. *t++ = (char)(0x80 | ((c >> 18) & 0x3F));
  13003. }
  13004. *t++ = (char)(0x80 | ((c >> 12) & 0x3F));
  13005. }
  13006. *t++ = (char)(0x80 | ((c >> 6) & 0x3F));
  13007. }
  13008. *t++ = (char)(0x80 | (c & 0x3F));
  13009. }
  13010. }
  13011. *t = '\0';
  13012. }
  13013. return r;
  13014. }
  13015. #endif
  13016. /******************************************************************************/
  13017. #ifndef PALM_2
  13018. SOAP_FMAC1
  13019. int
  13020. SOAP_FMAC2
  13021. soap_outstring(struct soap *soap, const char *tag, int id, char *const*p, const char *type, int n)
  13022. { id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n);
  13023. if (id < 0)
  13024. return soap->error;
  13025. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  13026. return soap_element_null(soap, tag, id, type);
  13027. if (soap_element_begin_out(soap, tag, id, type)
  13028. || soap_string_out(soap, *p, 0)
  13029. || soap_element_end_out(soap, tag))
  13030. return soap->error;
  13031. return SOAP_OK;
  13032. }
  13033. #endif
  13034. /******************************************************************************/
  13035. #ifndef PALM_2
  13036. SOAP_FMAC1
  13037. char **
  13038. SOAP_FMAC2
  13039. soap_instring(struct soap *soap, const char *tag, char **p, const char *type, int t, int flag, long minlen, long maxlen)
  13040. { (void)type;
  13041. if (soap_element_begin_in(soap, tag, 1, NULL))
  13042. { if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  13043. return NULL;
  13044. soap->error = SOAP_OK;
  13045. }
  13046. if (!p)
  13047. { if (!(p = (char**)soap_malloc(soap, sizeof(char*))))
  13048. return NULL;
  13049. }
  13050. if (soap->null)
  13051. *p = NULL;
  13052. else if (soap->body)
  13053. { *p = soap_string_in(soap, flag, minlen, maxlen);
  13054. if (!*p || !(char*)soap_id_enter(soap, soap->id, *p, t, sizeof(char*), 0, NULL, NULL, NULL))
  13055. return NULL;
  13056. if (!**p && tag && *tag == '-')
  13057. { soap->error = SOAP_NO_TAG;
  13058. return NULL;
  13059. }
  13060. }
  13061. else if (tag && *tag == '-')
  13062. { soap->error = SOAP_NO_TAG;
  13063. return NULL;
  13064. }
  13065. else if (!*soap->href && minlen > 0)
  13066. { soap->error = SOAP_LENGTH;
  13067. return NULL;
  13068. }
  13069. else
  13070. *p = soap_strdup(soap, SOAP_STR_EOS);
  13071. if (*soap->href)
  13072. p = (char**)soap_id_lookup(soap, soap->href, (void**)p, t, sizeof(char**), 0);
  13073. if (soap->body && soap_element_end_in(soap, tag))
  13074. return NULL;
  13075. return p;
  13076. }
  13077. #endif
  13078. /******************************************************************************/
  13079. #ifndef WITH_LEANER
  13080. #ifndef PALM_2
  13081. SOAP_FMAC1
  13082. int
  13083. SOAP_FMAC2
  13084. soap_outwstring(struct soap *soap, const char *tag, int id, wchar_t *const*p, const char *type, int n)
  13085. { id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n);
  13086. if (id < 0)
  13087. return soap->error;
  13088. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  13089. return soap_element_null(soap, tag, id, type);
  13090. if (soap_element_begin_out(soap, tag, id, type)
  13091. || soap_wstring_out(soap, *p, 0)
  13092. || soap_element_end_out(soap, tag))
  13093. return soap->error;
  13094. return SOAP_OK;
  13095. }
  13096. #endif
  13097. #endif
  13098. /******************************************************************************/
  13099. #ifndef WITH_LEANER
  13100. #ifndef PALM_2
  13101. SOAP_FMAC1
  13102. wchar_t **
  13103. SOAP_FMAC2
  13104. soap_inwstring(struct soap *soap, const char *tag, wchar_t **p, const char *type, int t, long minlen, long maxlen)
  13105. { (void)type;
  13106. if (soap_element_begin_in(soap, tag, 1, NULL))
  13107. { if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  13108. return NULL;
  13109. soap->error = SOAP_OK;
  13110. }
  13111. if (!p)
  13112. { if (!(p = (wchar_t**)soap_malloc(soap, sizeof(wchar_t*))))
  13113. return NULL;
  13114. }
  13115. if (soap->body)
  13116. { *p = soap_wstring_in(soap, 1, minlen, maxlen);
  13117. if (!*p || !(wchar_t*)soap_id_enter(soap, soap->id, *p, t, sizeof(wchar_t*), 0, NULL, NULL, NULL))
  13118. return NULL;
  13119. if (!**p && tag && *tag == '-')
  13120. { soap->error = SOAP_NO_TAG;
  13121. return NULL;
  13122. }
  13123. }
  13124. else if (tag && *tag == '-')
  13125. { soap->error = SOAP_NO_TAG;
  13126. return NULL;
  13127. }
  13128. else if (soap->null)
  13129. *p = NULL;
  13130. else
  13131. *p = soap_wstrdup(soap, (wchar_t*)SOAP_STR_EOS);
  13132. if (*soap->href)
  13133. p = (wchar_t**)soap_id_lookup(soap, soap->href, (void**)p, t, sizeof(wchar_t**), 0);
  13134. if (soap->body && soap_element_end_in(soap, tag))
  13135. return NULL;
  13136. return p;
  13137. }
  13138. #endif
  13139. #endif
  13140. /******************************************************************************/
  13141. #ifndef WITH_LEAN
  13142. SOAP_FMAC1
  13143. time_t
  13144. SOAP_FMAC2
  13145. soap_timegm(struct tm *T)
  13146. {
  13147. #if defined(HAVE_TIMEGM)
  13148. return timegm(T);
  13149. #else
  13150. time_t t, g, z;
  13151. struct tm tm;
  13152. t = mktime(T);
  13153. if (t == (time_t)-1)
  13154. return (time_t)-1;
  13155. #ifdef HAVE_GMTIME_R
  13156. gmtime_r(&t, &tm);
  13157. #else
  13158. tm = *gmtime(&t);
  13159. #endif
  13160. tm.tm_isdst = 0;
  13161. g = mktime(&tm);
  13162. if (g == (time_t)-1)
  13163. return (time_t)-1;
  13164. z = g - t;
  13165. return t - z;
  13166. #endif
  13167. }
  13168. #endif
  13169. /******************************************************************************/
  13170. #ifndef WITH_LEAN
  13171. SOAP_FMAC1
  13172. const char*
  13173. SOAP_FMAC2
  13174. soap_dateTime2s(struct soap *soap, time_t n)
  13175. { struct tm T, *pT = &T;
  13176. #if defined(HAVE_GMTIME_R)
  13177. if (gmtime_r(&n, pT))
  13178. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  13179. #elif defined(HAVE_GMTIME)
  13180. if ((pT = gmtime(&n)))
  13181. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  13182. #elif defined(HAVE_TM_GMTOFF) || defined(HAVE_STRUCT_TM_TM_GMTOFF) || defined(HAVE_STRUCT_TM___TM_GMTOFF)
  13183. #if defined(HAVE_LOCALTIME_R)
  13184. if (localtime_r(&n, pT))
  13185. { strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  13186. memmove(soap->tmpbuf + 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  13187. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  13188. }
  13189. #else
  13190. if ((pT = localtime(&n)))
  13191. { strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  13192. memmove(soap->tmpbuf + 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  13193. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  13194. }
  13195. #endif
  13196. #elif defined(HAVE_GETTIMEOFDAY)
  13197. struct timezone tz;
  13198. memset((void*)&tz, 0, sizeof(tz));
  13199. #if defined(HAVE_LOCALTIME_R)
  13200. if (localtime_r(&n, pT))
  13201. { struct timeval tv;
  13202. gettimeofday(&tv, &tz);
  13203. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13204. #ifdef HAVE_SNPRINTF
  13205. 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);
  13206. #else
  13207. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  13208. #endif
  13209. }
  13210. #else
  13211. if ((pT = localtime(&n)))
  13212. { struct timeval tv;
  13213. gettimeofday(&tv, &tz);
  13214. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13215. #ifdef HAVE_SNPRINTF
  13216. 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);
  13217. #else
  13218. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  13219. #endif
  13220. }
  13221. #endif
  13222. #elif defined(HAVE_FTIME)
  13223. struct timeb t;
  13224. memset((void*)&t, 0, sizeof(t));
  13225. #if defined(HAVE_LOCALTIME_R)
  13226. if (localtime_r(&n, pT))
  13227. {
  13228. #ifdef __BORLANDC__
  13229. ::ftime(&t);
  13230. #else
  13231. ftime(&t);
  13232. #endif
  13233. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13234. #ifdef HAVE_SNPRINTF
  13235. 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);
  13236. #else
  13237. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  13238. #endif
  13239. }
  13240. #else
  13241. if ((pT = localtime(&n)))
  13242. {
  13243. #ifdef __BORLANDC__
  13244. ::ftime(&t);
  13245. #else
  13246. ftime(&t);
  13247. #endif
  13248. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13249. #ifdef HAVE_SNPRINTF
  13250. 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);
  13251. #else
  13252. sprintf(soap->tmpbuf + strlen(soap->tmpbuf), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  13253. #endif
  13254. }
  13255. #endif
  13256. #elif defined(HAVE_LOCALTIME_R)
  13257. if (localtime_r(&n, pT))
  13258. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13259. #else
  13260. if ((pT = localtime(&n)))
  13261. strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  13262. #endif
  13263. else
  13264. strcpy(soap->tmpbuf, "1969-12-31T23:59:59Z");
  13265. return soap->tmpbuf;
  13266. }
  13267. #endif
  13268. /******************************************************************************/
  13269. #ifndef WITH_LEAN
  13270. SOAP_FMAC1
  13271. int
  13272. SOAP_FMAC2
  13273. soap_outdateTime(struct soap *soap, const char *tag, int id, const time_t *p, const char *type, int n)
  13274. { if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  13275. || soap_string_out(soap, soap_dateTime2s(soap, *p), 0))
  13276. return soap->error;
  13277. return soap_element_end_out(soap, tag);
  13278. }
  13279. #endif
  13280. /******************************************************************************/
  13281. #ifndef WITH_LEAN
  13282. SOAP_FMAC1
  13283. int
  13284. SOAP_FMAC2
  13285. soap_s2dateTime(struct soap *soap, const char *s, time_t *p)
  13286. { if (s)
  13287. { char zone[32];
  13288. struct tm T;
  13289. const char *t;
  13290. *zone = '\0';
  13291. memset((void*)&T, 0, sizeof(T));
  13292. if (strchr(s, '-'))
  13293. t = "%d-%d-%dT%d:%d:%d%31s";
  13294. else if (strchr(s, ':'))
  13295. t = "%4d%2d%2dT%d:%d:%d%31s";
  13296. else /* parse non-XSD-standard alternative ISO 8601 format */
  13297. t = "%4d%2d%2dT%2d%2d%2d%31s";
  13298. if (sscanf(s, t, &T.tm_year, &T.tm_mon, &T.tm_mday, &T.tm_hour, &T.tm_min, &T.tm_sec, zone) < 6)
  13299. return soap->error = SOAP_TYPE;
  13300. if (T.tm_year == 1)
  13301. T.tm_year = 70;
  13302. else
  13303. T.tm_year -= 1900;
  13304. T.tm_mon--;
  13305. if (*zone == '.')
  13306. { for (s = zone + 1; *s; s++)
  13307. if (*s < '0' || *s > '9')
  13308. break;
  13309. }
  13310. else
  13311. s = zone;
  13312. if (*s)
  13313. {
  13314. #ifndef WITH_NOZONE
  13315. if (*s == '+' || *s == '-')
  13316. { int h = 0, m = 0;
  13317. if (s[3] == ':')
  13318. { /* +hh:mm */
  13319. sscanf(s, "%d:%d", &h, &m);
  13320. if (h < 0)
  13321. m = -m;
  13322. }
  13323. else /* +hhmm */
  13324. { m = (int)soap_strtol(s, NULL, 10);
  13325. h = m / 100;
  13326. m = m % 100;
  13327. }
  13328. T.tm_min -= m;
  13329. T.tm_hour -= h;
  13330. /* put hour and min in range */
  13331. T.tm_hour += T.tm_min / 60;
  13332. T.tm_min %= 60;
  13333. if (T.tm_min < 0)
  13334. { T.tm_min += 60;
  13335. T.tm_hour--;
  13336. }
  13337. T.tm_mday += T.tm_hour / 24;
  13338. T.tm_hour %= 24;
  13339. if (T.tm_hour < 0)
  13340. { T.tm_hour += 24;
  13341. T.tm_mday--;
  13342. }
  13343. /* note: day of the month may be out of range, timegm() handles it */
  13344. }
  13345. #endif
  13346. *p = soap_timegm(&T);
  13347. }
  13348. else /* no UTC or timezone, so assume we got a localtime */
  13349. { T.tm_isdst = -1;
  13350. *p = mktime(&T);
  13351. }
  13352. }
  13353. return soap->error;
  13354. }
  13355. #endif
  13356. /******************************************************************************/
  13357. #ifndef WITH_LEAN
  13358. SOAP_FMAC1
  13359. time_t *
  13360. SOAP_FMAC2
  13361. soap_indateTime(struct soap *soap, const char *tag, time_t *p, const char *type, int t)
  13362. { if (soap_element_begin_in(soap, tag, 0, NULL))
  13363. return NULL;
  13364. if (*soap->type
  13365. && soap_match_tag(soap, soap->type, type)
  13366. && soap_match_tag(soap, soap->type, ":dateTime"))
  13367. { soap->error = SOAP_TYPE;
  13368. soap_revert(soap);
  13369. return NULL;
  13370. }
  13371. p = (time_t*)soap_id_enter(soap, soap->id, p, t, sizeof(time_t), 0, NULL, NULL, NULL);
  13372. if (*soap->href)
  13373. p = (time_t*)soap_id_forward(soap, soap->href, p, 0, t, 0, sizeof(time_t), 0, NULL);
  13374. else if (p)
  13375. { if (soap_s2dateTime(soap, soap_value(soap), p))
  13376. return NULL;
  13377. }
  13378. if (soap->body && soap_element_end_in(soap, tag))
  13379. return NULL;
  13380. return p;
  13381. }
  13382. #endif
  13383. /******************************************************************************/
  13384. #ifndef PALM_2
  13385. SOAP_FMAC1
  13386. int
  13387. SOAP_FMAC2
  13388. soap_outliteral(struct soap *soap, const char *tag, char *const*p, const char *type)
  13389. { int i;
  13390. const char *t = NULL;
  13391. if (tag && *tag != '-')
  13392. { if (soap->local_namespaces && (t = strchr(tag, ':')))
  13393. { size_t n = t - tag;
  13394. if (n >= sizeof(soap->tmpbuf))
  13395. n = sizeof(soap->tmpbuf) - 1;
  13396. strncpy(soap->tmpbuf, tag, n);
  13397. soap->tmpbuf[n] = '\0';
  13398. for (i = 0; soap->local_namespaces[i].id; i++)
  13399. if (!strcmp(soap->tmpbuf, soap->local_namespaces[i].id))
  13400. break;
  13401. t++;
  13402. if (soap_element(soap, t, 0, type)
  13403. || soap_attribute(soap, "xmlns", soap->local_namespaces[i].ns ? soap->local_namespaces[i].ns : SOAP_STR_EOS)
  13404. || soap_element_start_end_out(soap, NULL))
  13405. return soap->error;
  13406. }
  13407. else
  13408. { t = tag;
  13409. if (soap_element_begin_out(soap, t, 0, type))
  13410. return soap->error;
  13411. }
  13412. }
  13413. if (p && *p)
  13414. { if (soap_send(soap, *p)) /* send as-is */
  13415. return soap->error;
  13416. }
  13417. if (t)
  13418. return soap_element_end_out(soap, t);
  13419. return SOAP_OK;
  13420. }
  13421. #endif
  13422. /******************************************************************************/
  13423. #ifndef PALM_2
  13424. SOAP_FMAC1
  13425. char **
  13426. SOAP_FMAC2
  13427. soap_inliteral(struct soap *soap, const char *tag, char **p)
  13428. { if (soap_element_begin_in(soap, tag, 1, NULL))
  13429. { if (soap->error != SOAP_NO_TAG || soap_unget(soap, soap_get(soap)) == SOAP_TT)
  13430. return NULL;
  13431. soap->error = SOAP_OK;
  13432. }
  13433. if (!p)
  13434. { if (!(p = (char**)soap_malloc(soap, sizeof(char*))))
  13435. return NULL;
  13436. }
  13437. if (soap->body || (tag && *tag == '-'))
  13438. { *p = soap_string_in(soap, 0, -1, -1);
  13439. if (!*p)
  13440. return NULL;
  13441. if (!**p && tag && *tag == '-')
  13442. { soap->error = SOAP_NO_TAG;
  13443. return NULL;
  13444. }
  13445. }
  13446. else if (soap->null)
  13447. *p = NULL;
  13448. else
  13449. *p = soap_strdup(soap, SOAP_STR_EOS);
  13450. if (soap->body && soap_element_end_in(soap, tag))
  13451. return NULL;
  13452. return p;
  13453. }
  13454. #endif
  13455. /******************************************************************************/
  13456. #ifndef WITH_LEANER
  13457. #ifndef PALM_2
  13458. SOAP_FMAC1
  13459. int
  13460. SOAP_FMAC2
  13461. soap_outwliteral(struct soap *soap, const char *tag, wchar_t *const*p, const char *type)
  13462. { int i;
  13463. const char *t = NULL;
  13464. if (tag && *tag != '-')
  13465. { if (soap->local_namespaces && (t = strchr(tag, ':')))
  13466. { size_t n = t - tag;
  13467. if (n >= sizeof(soap->tmpbuf))
  13468. n = sizeof(soap->tmpbuf) - 1;
  13469. strncpy(soap->tmpbuf, tag, n);
  13470. soap->tmpbuf[n] = '\0';
  13471. for (i = 0; soap->local_namespaces[i].id; i++)
  13472. if (!strcmp(soap->tmpbuf, soap->local_namespaces[i].id))
  13473. break;
  13474. t++;
  13475. if (soap_element(soap, t, 0, type)
  13476. || soap_attribute(soap, "xmlns", soap->local_namespaces[i].ns ? soap->local_namespaces[i].ns : SOAP_STR_EOS)
  13477. || soap_element_start_end_out(soap, NULL))
  13478. return soap->error;
  13479. }
  13480. else
  13481. { t = tag;
  13482. if (soap_element_begin_out(soap, t, 0, type))
  13483. return soap->error;
  13484. }
  13485. }
  13486. if (p)
  13487. { wchar_t c;
  13488. const wchar_t *s = *p;
  13489. while ((c = *s++))
  13490. { if (soap_pututf8(soap, (unsigned long)c)) /* send as-is in UTF8 */
  13491. return soap->error;
  13492. }
  13493. }
  13494. if (t)
  13495. return soap_element_end_out(soap, t);
  13496. return SOAP_OK;
  13497. }
  13498. #endif
  13499. #endif
  13500. /******************************************************************************/
  13501. #ifndef WITH_LEANER
  13502. #ifndef PALM_2
  13503. SOAP_FMAC1
  13504. wchar_t **
  13505. SOAP_FMAC2
  13506. soap_inwliteral(struct soap *soap, const char *tag, wchar_t **p)
  13507. { if (soap_element_begin_in(soap, tag, 1, NULL))
  13508. { if (soap->error != SOAP_NO_TAG || soap_unget(soap, soap_get(soap)) == SOAP_TT)
  13509. return NULL;
  13510. soap->error = SOAP_OK;
  13511. }
  13512. if (!p)
  13513. { if (!(p = (wchar_t**)soap_malloc(soap, sizeof(wchar_t*))))
  13514. return NULL;
  13515. }
  13516. if (soap->body)
  13517. { *p = soap_wstring_in(soap, 0, -1, -1);
  13518. if (!*p)
  13519. return NULL;
  13520. if (!**p && tag && *tag == '-')
  13521. { soap->error = SOAP_NO_TAG;
  13522. return NULL;
  13523. }
  13524. }
  13525. else if (tag && *tag == '-')
  13526. { soap->error = SOAP_NO_TAG;
  13527. return NULL;
  13528. }
  13529. else if (soap->null)
  13530. *p = NULL;
  13531. else
  13532. *p = soap_wstrdup(soap, (wchar_t*)SOAP_STR_EOS);
  13533. if (soap->body && soap_element_end_in(soap, tag))
  13534. return NULL;
  13535. return p;
  13536. }
  13537. #endif
  13538. #endif
  13539. /******************************************************************************/
  13540. #ifndef PALM_2
  13541. SOAP_FMAC1
  13542. const char *
  13543. SOAP_FMAC2
  13544. soap_value(struct soap *soap)
  13545. { register size_t i;
  13546. register soap_wchar c = 0;
  13547. register char *s = soap->tmpbuf;
  13548. if (!soap->body)
  13549. return SOAP_STR_EOS;
  13550. do c = soap_get(soap);
  13551. while (soap_blank(c));
  13552. for (i = 0; i < sizeof(soap->tmpbuf) - 1; i++)
  13553. { if (c == SOAP_TT || c == SOAP_LT || (int)c == EOF)
  13554. break;
  13555. *s++ = (char)c;
  13556. c = soap_get(soap);
  13557. }
  13558. for (s--; i > 0; i--, s--)
  13559. { if (!soap_blank((soap_wchar)*s))
  13560. break;
  13561. }
  13562. s[1] = '\0';
  13563. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0'; /* appease */
  13564. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element content value='%s'\n", soap->tmpbuf));
  13565. if (c == SOAP_TT || c == SOAP_LT || (int)c == EOF)
  13566. soap_unget(soap, c);
  13567. else if (soap->mode & SOAP_XML_STRICT)
  13568. { soap->error = SOAP_LENGTH;
  13569. return NULL;
  13570. }
  13571. #ifdef WITH_DOM
  13572. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  13573. soap->dom->data = soap_strdup(soap, soap->tmpbuf);
  13574. #endif
  13575. return soap->tmpbuf; /* return non-null pointer */
  13576. }
  13577. #endif
  13578. /******************************************************************************/
  13579. #if !defined(WITH_LEANER) || !defined(WITH_NOHTTP)
  13580. #ifndef PALM_2
  13581. SOAP_FMAC1
  13582. int
  13583. SOAP_FMAC2
  13584. soap_getline(struct soap *soap, char *s, int len)
  13585. { int i = len;
  13586. soap_wchar c = 0;
  13587. for (;;)
  13588. { while (--i > 0)
  13589. { c = soap_getchar(soap);
  13590. if (c == '\r' || c == '\n')
  13591. break;
  13592. if ((int)c == EOF)
  13593. return soap->error = SOAP_CHK_EOF;
  13594. *s++ = (char)c;
  13595. }
  13596. *s = '\0';
  13597. if (c != '\n')
  13598. c = soap_getchar(soap); /* got \r or something else, now get \n */
  13599. if (c == '\n')
  13600. { if (i + 1 == len) /* empty line: end of HTTP/MIME header */
  13601. break;
  13602. c = soap_get0(soap);
  13603. if (c != ' ' && c != '\t') /* HTTP line continuation? */
  13604. break;
  13605. }
  13606. else if ((int)c == EOF)
  13607. return soap->error = SOAP_CHK_EOF;
  13608. if (i <= 0)
  13609. return soap->error = SOAP_HDR;
  13610. }
  13611. return SOAP_OK;
  13612. }
  13613. #endif
  13614. #endif
  13615. /******************************************************************************/
  13616. #ifndef PALM_1
  13617. static size_t
  13618. soap_count_attachments(struct soap *soap)
  13619. {
  13620. #ifndef WITH_LEANER
  13621. register struct soap_multipart *content;
  13622. register size_t count = soap->count;
  13623. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the message size with attachments, current count=%lu\n", (unsigned long)count));
  13624. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  13625. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of DIME attachments\n"));
  13626. for (content = soap->dime.first; content; content = content->next)
  13627. { count += 12 + ((content->size+3)&(~3));
  13628. if (content->id)
  13629. count += ((strlen(content->id)+3)&(~3));
  13630. if (content->type)
  13631. count += ((strlen(content->type)+3)&(~3));
  13632. if (content->options)
  13633. count += ((((unsigned char)content->options[2] << 8) | ((unsigned char)content->options[3]))+7)&(~3);
  13634. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of DIME attachment content is %lu bytes\n", (unsigned long)content->size));
  13635. }
  13636. }
  13637. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary)
  13638. { register size_t n = strlen(soap->mime.boundary);
  13639. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of MIME attachments\n"));
  13640. for (content = soap->mime.first; content; content = content->next)
  13641. { register const char *s;
  13642. /* count \r\n--boundary\r\n */
  13643. count += 6 + n;
  13644. /* count Content-Type: ...\r\n */
  13645. if (content->type)
  13646. count += 16 + strlen(content->type);
  13647. /* count Content-Transfer-Encoding: ...\r\n */
  13648. s = soap_code_str(mime_codes, content->encoding);
  13649. if (s)
  13650. count += 29 + strlen(s);
  13651. /* count Content-ID: ...\r\n */
  13652. if (content->id)
  13653. count += 14 + strlen(content->id);
  13654. /* count Content-Location: ...\r\n */
  13655. if (content->location)
  13656. count += 20 + strlen(content->location);
  13657. /* count Content-Description: ...\r\n */
  13658. if (content->description)
  13659. count += 23 + strlen(content->description);
  13660. /* count \r\n...content */
  13661. count += 2 + content->size;
  13662. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of MIME attachment content is %lu bytes\n", (unsigned long)content->size));
  13663. }
  13664. /* count \r\n--boundary-- */
  13665. count += 6 + n;
  13666. }
  13667. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New count=%lu\n", (unsigned long)count));
  13668. return count;
  13669. #else
  13670. return soap->count;
  13671. #endif
  13672. }
  13673. #endif
  13674. /******************************************************************************/
  13675. #ifndef WITH_LEANER
  13676. #ifndef PALM_1
  13677. static int
  13678. soap_putdimefield(struct soap *soap, const char *s, size_t n)
  13679. { if (soap_send_raw(soap, s, n))
  13680. return soap->error;
  13681. return soap_send_raw(soap, SOAP_STR_PADDING, -(long)n&3);
  13682. }
  13683. #endif
  13684. #endif
  13685. /******************************************************************************/
  13686. #ifndef WITH_LEANER
  13687. #ifndef PALM_1
  13688. SOAP_FMAC1
  13689. char *
  13690. SOAP_FMAC2
  13691. soap_dime_option(struct soap *soap, unsigned short optype, const char *option)
  13692. { size_t n;
  13693. char *s = NULL;
  13694. if (option)
  13695. { n = strlen(option);
  13696. s = (char*)soap_malloc(soap, n + 5);
  13697. if (s)
  13698. { s[0] = (char)(optype >> 8);
  13699. s[1] = (char)(optype & 0xFF);
  13700. s[2] = (char)(n >> 8);
  13701. s[3] = (char)(n & 0xFF);
  13702. strcpy(s + 4, option);
  13703. }
  13704. }
  13705. return s;
  13706. }
  13707. #endif
  13708. #endif
  13709. /******************************************************************************/
  13710. #ifndef WITH_LEANER
  13711. #ifndef PALM_1
  13712. SOAP_FMAC1
  13713. int
  13714. SOAP_FMAC2
  13715. soap_putdimehdr(struct soap *soap)
  13716. { unsigned char tmp[12];
  13717. size_t optlen = 0, idlen = 0, typelen = 0;
  13718. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Put DIME header id='%s'\n", soap->dime.id ? soap->dime.id : SOAP_STR_EOS));
  13719. if (soap->dime.options)
  13720. optlen = (((unsigned char)soap->dime.options[2] << 8) | ((unsigned char)soap->dime.options[3])) + 4;
  13721. if (soap->dime.id)
  13722. { idlen = strlen(soap->dime.id);
  13723. if (idlen > 0x0000FFFF)
  13724. idlen = 0x0000FFFF;
  13725. }
  13726. if (soap->dime.type)
  13727. { typelen = strlen(soap->dime.type);
  13728. if (typelen > 0x0000FFFF)
  13729. typelen = 0x0000FFFF;
  13730. }
  13731. tmp[0] = SOAP_DIME_VERSION | (soap->dime.flags & 0x7);
  13732. tmp[1] = soap->dime.flags & 0xF0;
  13733. tmp[2] = (char)(optlen >> 8);
  13734. tmp[3] = (char)(optlen & 0xFF);
  13735. tmp[4] = (char)(idlen >> 8);
  13736. tmp[5] = (char)(idlen & 0xFF);
  13737. tmp[6] = (char)(typelen >> 8);
  13738. tmp[7] = (char)(typelen & 0xFF);
  13739. tmp[8] = (char)(soap->dime.size >> 24);
  13740. tmp[9] = (char)((soap->dime.size >> 16) & 0xFF);
  13741. tmp[10] = (char)((soap->dime.size >> 8) & 0xFF);
  13742. tmp[11] = (char)(soap->dime.size & 0xFF);
  13743. if (soap_send_raw(soap, (char*)tmp, 12)
  13744. || soap_putdimefield(soap, soap->dime.options, optlen)
  13745. || soap_putdimefield(soap, soap->dime.id, idlen)
  13746. || soap_putdimefield(soap, soap->dime.type, typelen))
  13747. return soap->error;
  13748. return SOAP_OK;
  13749. }
  13750. #endif
  13751. #endif
  13752. /******************************************************************************/
  13753. #ifndef WITH_LEANER
  13754. #ifndef PALM_1
  13755. SOAP_FMAC1
  13756. int
  13757. SOAP_FMAC2
  13758. soap_putdime(struct soap *soap)
  13759. { struct soap_multipart *content;
  13760. if (!(soap->mode & SOAP_ENC_DIME))
  13761. return SOAP_OK;
  13762. for (content = soap->dime.first; content; content = content->next)
  13763. { void *handle;
  13764. soap->dime.size = content->size;
  13765. soap->dime.id = content->id;
  13766. soap->dime.type = content->type;
  13767. soap->dime.options = content->options;
  13768. soap->dime.flags = SOAP_DIME_VERSION | SOAP_DIME_MEDIA;
  13769. if (soap->fdimereadopen && ((handle = soap->fdimereadopen(soap, (void*)content->ptr, content->id, content->type, content->options)) || soap->error))
  13770. { size_t size = content->size;
  13771. if (!handle)
  13772. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadopen failed\n"));
  13773. return soap->error;
  13774. }
  13775. if (!size && ((soap->mode & SOAP_ENC_XML) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_IO) == SOAP_IO_STORE))
  13776. { size_t chunksize = sizeof(soap->tmpbuf);
  13777. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming DIME\n"));
  13778. do
  13779. { size = soap->fdimeread(soap, handle, soap->tmpbuf, chunksize);
  13780. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimeread returned %lu bytes\n", (unsigned long)size));
  13781. if (size < chunksize)
  13782. { soap->dime.flags &= ~SOAP_DIME_CF;
  13783. if (!content->next)
  13784. soap->dime.flags |= SOAP_DIME_ME;
  13785. }
  13786. else
  13787. soap->dime.flags |= SOAP_DIME_CF;
  13788. soap->dime.size = size;
  13789. if (soap_putdimehdr(soap)
  13790. || soap_putdimefield(soap, soap->tmpbuf, size))
  13791. break;
  13792. if (soap->dime.id)
  13793. { soap->dime.flags &= ~(SOAP_DIME_MB | SOAP_DIME_MEDIA);
  13794. soap->dime.id = NULL;
  13795. soap->dime.type = NULL;
  13796. soap->dime.options = NULL;
  13797. }
  13798. } while (size >= chunksize);
  13799. }
  13800. else
  13801. { if (!content->next)
  13802. soap->dime.flags |= SOAP_DIME_ME;
  13803. if (soap_putdimehdr(soap))
  13804. return soap->error;
  13805. do
  13806. { size_t bufsize;
  13807. if (size < sizeof(soap->tmpbuf))
  13808. bufsize = size;
  13809. else
  13810. bufsize = sizeof(soap->tmpbuf);
  13811. if (!(bufsize = soap->fdimeread(soap, handle, soap->tmpbuf, bufsize)))
  13812. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n", (unsigned long)size, (unsigned long)content->size));
  13813. soap->error = SOAP_CHK_EOF;
  13814. break;
  13815. }
  13816. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  13817. break;
  13818. size -= bufsize;
  13819. } while (size);
  13820. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  13821. soap_send_raw(soap, SOAP_STR_PADDING, -(long)soap->dime.size&3);
  13822. }
  13823. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  13824. if (soap->fdimereadclose)
  13825. soap->fdimereadclose(soap, handle);
  13826. }
  13827. else
  13828. { if (!content->next)
  13829. soap->dime.flags |= SOAP_DIME_ME;
  13830. if (soap_putdimehdr(soap)
  13831. || soap_putdimefield(soap, (char*)content->ptr, content->size))
  13832. return soap->error;
  13833. }
  13834. }
  13835. return SOAP_OK;
  13836. }
  13837. #endif
  13838. #endif
  13839. /******************************************************************************/
  13840. #ifndef WITH_LEANER
  13841. #ifndef PALM_1
  13842. static char *
  13843. soap_getdimefield(struct soap *soap, size_t n)
  13844. { register soap_wchar c;
  13845. register size_t i;
  13846. register char *s;
  13847. register char *p = NULL;
  13848. if (n)
  13849. { p = (char*)soap_malloc(soap, n + 1);
  13850. if (p)
  13851. { s = p;
  13852. for (i = n; i > 0; i--)
  13853. { if ((int)(c = soap_get1(soap)) == EOF)
  13854. { soap->error = SOAP_CHK_EOF;
  13855. return NULL;
  13856. }
  13857. *s++ = (char)c;
  13858. }
  13859. *s = '\0';
  13860. if ((soap->error = soap_move(soap, (size_t)(-(long)n&3))))
  13861. return NULL;
  13862. }
  13863. else
  13864. soap->error = SOAP_EOM;
  13865. }
  13866. return p;
  13867. }
  13868. #endif
  13869. #endif
  13870. /******************************************************************************/
  13871. #ifndef WITH_LEANER
  13872. #ifndef PALM_1
  13873. SOAP_FMAC1
  13874. int
  13875. SOAP_FMAC2
  13876. soap_getdimehdr(struct soap *soap)
  13877. { register soap_wchar c;
  13878. register char *s;
  13879. register int i;
  13880. unsigned char tmp[12];
  13881. size_t optlen, idlen, typelen;
  13882. if (!(soap->mode & SOAP_ENC_DIME))
  13883. return soap->error = SOAP_DIME_END;
  13884. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME header\n"));
  13885. if (soap->dime.buflen || soap->dime.chunksize)
  13886. { if (soap_move(soap, soap->dime.size - soap_tell(soap)))
  13887. return soap->error = SOAP_CHK_EOF;
  13888. soap_unget(soap, soap_getchar(soap)); /* skip padding and get hdr */
  13889. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... From chunked\n"));
  13890. return SOAP_OK;
  13891. }
  13892. s = (char*)tmp;
  13893. for (i = 12; i > 0; i--)
  13894. { if ((int)(c = soap_getchar(soap)) == EOF)
  13895. return soap->error = SOAP_CHK_EOF;
  13896. *s++ = (char)c;
  13897. }
  13898. if ((tmp[0] & 0xF8) != SOAP_DIME_VERSION)
  13899. return soap->error = SOAP_DIME_MISMATCH;
  13900. soap->dime.flags = (tmp[0] & 0x7) | (tmp[1] & 0xF0);
  13901. optlen = (tmp[2] << 8) | tmp[3];
  13902. idlen = (tmp[4] << 8) | tmp[5];
  13903. typelen = (tmp[6] << 8) | tmp[7];
  13904. soap->dime.size = ((size_t)tmp[8] << 24) | ((size_t)tmp[9] << 16) | ((size_t)tmp[10] << 8) | ((size_t)tmp[11]);
  13905. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu flags=0x%X\n", (unsigned long)soap->dime.size, soap->dime.flags));
  13906. if (!(soap->dime.options = soap_getdimefield(soap, optlen)) && soap->error)
  13907. return soap->error;
  13908. if (!(soap->dime.id = soap_getdimefield(soap, idlen)) && soap->error)
  13909. return soap->error;
  13910. if (!(soap->dime.type = soap_getdimefield(soap, typelen)) && soap->error)
  13911. return soap->error;
  13912. 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));
  13913. if (soap->dime.flags & SOAP_DIME_ME)
  13914. soap->mode &= ~SOAP_ENC_DIME;
  13915. return SOAP_OK;
  13916. }
  13917. #endif
  13918. #endif
  13919. /******************************************************************************/
  13920. #ifndef WITH_LEANER
  13921. #ifndef PALM_1
  13922. SOAP_FMAC1
  13923. int
  13924. SOAP_FMAC2
  13925. soap_getdime(struct soap *soap)
  13926. { while (soap->dime.flags & SOAP_DIME_CF)
  13927. { if (soap_getdimehdr(soap))
  13928. return soap->error;
  13929. if (soap_move(soap, soap->dime.size))
  13930. return soap->error = SOAP_EOF;
  13931. }
  13932. if (soap_move(soap, (size_t)(((soap->dime.size+3)&(~3)) - soap_tell(soap))))
  13933. return soap->error = SOAP_EOF;
  13934. for (;;)
  13935. { register struct soap_multipart *content;
  13936. if (soap_getdimehdr(soap))
  13937. break;
  13938. if (soap->fdimewriteopen && ((soap->dime.ptr = (char*)soap->fdimewriteopen(soap, soap->dime.id, soap->dime.type, soap->dime.options)) || soap->error))
  13939. { const char *id, *type, *options;
  13940. size_t size, n;
  13941. if (!soap->dime.ptr)
  13942. return soap->error;
  13943. id = soap->dime.id;
  13944. type = soap->dime.type;
  13945. options = soap->dime.options;
  13946. for (;;)
  13947. { size = soap->dime.size;
  13948. for (;;)
  13949. { n = soap->buflen - soap->bufidx;
  13950. if (size < n)
  13951. n = size;
  13952. if ((soap->error = soap->fdimewrite(soap, (void*)soap->dime.ptr, soap->buf + soap->bufidx, n)))
  13953. break;
  13954. size -= n;
  13955. if (!size)
  13956. { soap->bufidx += n;
  13957. break;
  13958. }
  13959. if (soap_recv(soap))
  13960. { soap->error = SOAP_EOF;
  13961. goto end;
  13962. }
  13963. }
  13964. if (soap_move(soap, (size_t)(-(long)soap->dime.size&3)))
  13965. { soap->error = SOAP_EOF;
  13966. break;
  13967. }
  13968. if (!(soap->dime.flags & SOAP_DIME_CF))
  13969. break;
  13970. if (soap_getdimehdr(soap))
  13971. break;
  13972. }
  13973. end:
  13974. if (soap->fdimewriteclose)
  13975. soap->fdimewriteclose(soap, (void*)soap->dime.ptr);
  13976. soap->dime.size = 0;
  13977. soap->dime.id = id;
  13978. soap->dime.type = type;
  13979. soap->dime.options = options;
  13980. }
  13981. else if (soap->dime.flags & SOAP_DIME_CF)
  13982. { const char *id, *type, *options;
  13983. id = soap->dime.id;
  13984. type = soap->dime.type;
  13985. options = soap->dime.options;
  13986. if (soap_new_block(soap) == NULL)
  13987. return SOAP_EOM;
  13988. for (;;)
  13989. { register soap_wchar c;
  13990. register size_t i;
  13991. register char *s;
  13992. if (soap->dime.size > SOAP_MAXDIMESIZE)
  13993. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu exceeds SOAP_MAXDIMESIZE=%lu\n", (unsigned long)soap->dime.size, (unsigned long)SOAP_MAXDIMESIZE));
  13994. return soap->error = SOAP_DIME_ERROR;
  13995. }
  13996. s = (char*)soap_push_block(soap, NULL, soap->dime.size);
  13997. if (!s)
  13998. return soap->error = SOAP_EOM;
  13999. for (i = soap->dime.size; i > 0; i--)
  14000. { if ((int)(c = soap_get1(soap)) == EOF)
  14001. return soap->error = SOAP_EOF;
  14002. *s++ = (char)c;
  14003. }
  14004. if (soap_move(soap, (size_t)(-(long)soap->dime.size&3)))
  14005. return soap->error = SOAP_EOF;
  14006. if (!(soap->dime.flags & SOAP_DIME_CF))
  14007. break;
  14008. if (soap_getdimehdr(soap))
  14009. return soap->error;
  14010. }
  14011. soap->dime.size = soap->blist->size++; /* allocate one more byte in blist for the terminating '\0' */
  14012. if (!(soap->dime.ptr = soap_save_block(soap, NULL, NULL, 0)))
  14013. return soap->error;
  14014. soap->dime.ptr[soap->dime.size] = '\0'; /* make 0-terminated */
  14015. soap->dime.id = id;
  14016. soap->dime.type = type;
  14017. soap->dime.options = options;
  14018. }
  14019. else
  14020. soap->dime.ptr = soap_getdimefield(soap, soap->dime.size);
  14021. content = soap_new_multipart(soap, &soap->dime.first, &soap->dime.last, soap->dime.ptr, soap->dime.size);
  14022. if (!content)
  14023. return soap->error = SOAP_EOM;
  14024. content->id = soap->dime.id;
  14025. content->type = soap->dime.type;
  14026. content->options = soap->dime.options;
  14027. if (soap->error)
  14028. return soap->error;
  14029. soap_resolve_attachment(soap, content);
  14030. }
  14031. if (soap->error != SOAP_DIME_END)
  14032. return soap->error;
  14033. return soap->error = SOAP_OK;
  14034. }
  14035. #endif
  14036. #endif
  14037. /******************************************************************************/
  14038. #ifndef WITH_LEANER
  14039. #ifndef PALM_1
  14040. SOAP_FMAC1
  14041. int
  14042. SOAP_FMAC2
  14043. soap_getmimehdr(struct soap *soap)
  14044. { struct soap_multipart *content;
  14045. do
  14046. { if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  14047. return soap->error;
  14048. }
  14049. while (!*soap->msgbuf);
  14050. if (soap->msgbuf[0] == '-' && soap->msgbuf[1] == '-')
  14051. { char *s = soap->msgbuf + strlen(soap->msgbuf) - 1;
  14052. /* remove white space */
  14053. while (soap_blank((soap_wchar)*s))
  14054. s--;
  14055. s[1] = '\0';
  14056. if (soap->mime.boundary)
  14057. { if (strcmp(soap->msgbuf + 2, soap->mime.boundary))
  14058. return soap->error = SOAP_MIME_ERROR;
  14059. }
  14060. else
  14061. soap->mime.boundary = soap_strdup(soap, soap->msgbuf + 2);
  14062. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  14063. return soap->error;
  14064. }
  14065. if (soap_set_mime_attachment(soap, NULL, 0, SOAP_MIME_NONE, NULL, NULL, NULL, NULL))
  14066. return soap->error = SOAP_EOM;
  14067. content = soap->mime.last;
  14068. for (;;)
  14069. { register char *key = soap->msgbuf;
  14070. register char *val;
  14071. if (!*key)
  14072. break;
  14073. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "MIME header: %s\n", key));
  14074. val = strchr(soap->msgbuf, ':');
  14075. if (val)
  14076. { *val = '\0';
  14077. do val++;
  14078. while (*val && *val <= 32);
  14079. if (!soap_tag_cmp(key, "Content-ID"))
  14080. content->id = soap_strdup(soap, val);
  14081. else if (!soap_tag_cmp(key, "Content-Location"))
  14082. content->location = soap_strdup(soap, val);
  14083. else if (!soap_tag_cmp(key, "Content-Disposition"))
  14084. content->id = soap_strdup(soap, soap_get_header_attribute(soap, val, "name"));
  14085. else if (!soap_tag_cmp(key, "Content-Type"))
  14086. content->type = soap_strdup(soap, val);
  14087. else if (!soap_tag_cmp(key, "Content-Description"))
  14088. content->description = soap_strdup(soap, val);
  14089. else if (!soap_tag_cmp(key, "Content-Transfer-Encoding"))
  14090. content->encoding = (enum soap_mime_encoding)soap_code_int(mime_codes, val, (long)SOAP_MIME_NONE);
  14091. }
  14092. if (soap_getline(soap, key, sizeof(soap->msgbuf)))
  14093. return soap->error;
  14094. }
  14095. return SOAP_OK;
  14096. }
  14097. #endif
  14098. #endif
  14099. /******************************************************************************/
  14100. #ifndef WITH_LEANER
  14101. #ifndef PALM_1
  14102. SOAP_FMAC1
  14103. int
  14104. SOAP_FMAC2
  14105. soap_getmime(struct soap *soap)
  14106. { while (soap_get_mime_attachment(soap, NULL))
  14107. ;
  14108. return soap->error;
  14109. }
  14110. #endif
  14111. #endif
  14112. /******************************************************************************/
  14113. #ifndef WITH_LEANER
  14114. #ifndef PALM_1
  14115. SOAP_FMAC1
  14116. void
  14117. SOAP_FMAC2
  14118. soap_post_check_mime_attachments(struct soap *soap)
  14119. { soap->imode |= SOAP_MIME_POSTCHECK;
  14120. }
  14121. #endif
  14122. #endif
  14123. /******************************************************************************/
  14124. #ifndef WITH_LEANER
  14125. #ifndef PALM_1
  14126. SOAP_FMAC1
  14127. int
  14128. SOAP_FMAC2
  14129. soap_check_mime_attachments(struct soap *soap)
  14130. { if (soap->mode & SOAP_MIME_POSTCHECK)
  14131. return soap_get_mime_attachment(soap, NULL) != NULL;
  14132. return SOAP_OK;
  14133. }
  14134. #endif
  14135. #endif
  14136. /******************************************************************************/
  14137. #ifndef WITH_LEANER
  14138. #ifndef PALM_1
  14139. SOAP_FMAC1
  14140. struct soap_multipart *
  14141. SOAP_FMAC2
  14142. soap_get_mime_attachment(struct soap *soap, void *handle)
  14143. { register soap_wchar c = 0;
  14144. register size_t i, m = 0;
  14145. register char *s, *t = NULL;
  14146. register struct soap_multipart *content;
  14147. register short flag = 0;
  14148. if (!(soap->mode & SOAP_ENC_MIME))
  14149. return NULL;
  14150. content = soap->mime.last;
  14151. if (!content)
  14152. { if (soap_getmimehdr(soap))
  14153. return NULL;
  14154. content = soap->mime.last;
  14155. }
  14156. else if (content != soap->mime.first)
  14157. { if (soap->fmimewriteopen && ((content->ptr = (char*)soap->fmimewriteopen(soap, (void*)handle, content->id, content->type, content->description, content->encoding)) || soap->error))
  14158. { if (!content->ptr)
  14159. return NULL;
  14160. }
  14161. }
  14162. 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));
  14163. if (!content->ptr && soap_new_block(soap) == NULL)
  14164. { soap->error = SOAP_EOM;
  14165. return NULL;
  14166. }
  14167. for (;;)
  14168. { if (content->ptr)
  14169. s = soap->tmpbuf;
  14170. else if (!(s = (char*)soap_push_block(soap, NULL, sizeof(soap->tmpbuf))))
  14171. { soap->error = SOAP_EOM;
  14172. return NULL;
  14173. }
  14174. for (i = 0; i < sizeof(soap->tmpbuf); i++)
  14175. { if (m > 0)
  14176. { *s++ = *t++;
  14177. m--;
  14178. }
  14179. else
  14180. { if (!flag)
  14181. { c = soap_get1(soap);
  14182. if ((int)c == EOF)
  14183. { if (content->ptr && soap->fmimewriteclose)
  14184. soap->fmimewriteclose(soap, (void*)content->ptr);
  14185. soap->error = SOAP_CHK_EOF;
  14186. return NULL;
  14187. }
  14188. }
  14189. if (flag || c == '\r')
  14190. { t = soap->msgbuf;
  14191. memset(t, 0, sizeof(soap->msgbuf));
  14192. strcpy(t, "\n--");
  14193. if (soap->mime.boundary)
  14194. strncat(t, soap->mime.boundary, sizeof(soap->msgbuf)-4);
  14195. do c = soap_getchar(soap);
  14196. while (c == *t++);
  14197. if ((int)c == EOF)
  14198. { if (content->ptr && soap->fmimewriteclose)
  14199. soap->fmimewriteclose(soap, (void*)content->ptr);
  14200. soap->error = SOAP_CHK_EOF;
  14201. return NULL;
  14202. }
  14203. if (!*--t)
  14204. goto end;
  14205. *t = (char)c;
  14206. flag = (c == '\r');
  14207. m = t - soap->msgbuf + 1 - flag;
  14208. t = soap->msgbuf;
  14209. c = '\r';
  14210. }
  14211. *s++ = (char)c;
  14212. }
  14213. }
  14214. if (content->ptr && soap->fmimewrite)
  14215. { if ((soap->error = soap->fmimewrite(soap, (void*)content->ptr, soap->tmpbuf, i)))
  14216. break;
  14217. }
  14218. }
  14219. end:
  14220. *s = '\0'; /* make 0-terminated */
  14221. if (content->ptr)
  14222. { if (!soap->error && soap->fmimewrite)
  14223. soap->error = soap->fmimewrite(soap, (void*)content->ptr, soap->tmpbuf, i);
  14224. if (soap->fmimewriteclose)
  14225. soap->fmimewriteclose(soap, (void*)content->ptr);
  14226. if (soap->error)
  14227. return NULL;
  14228. }
  14229. else
  14230. { content->size = soap_size_block(soap, NULL, i+1) - 1; /* last block with '\0' */
  14231. content->ptr = soap_save_block(soap, NULL, NULL, 0);
  14232. }
  14233. soap_resolve_attachment(soap, content);
  14234. if (c == '-' && soap_getchar(soap) == '-')
  14235. { soap->mode &= ~SOAP_ENC_MIME;
  14236. if ((soap->mode & SOAP_MIME_POSTCHECK) && soap_end_recv(soap))
  14237. { if (soap->keep_alive < 0)
  14238. soap->keep_alive = 0;
  14239. soap_closesock(soap);
  14240. return NULL;
  14241. }
  14242. }
  14243. else
  14244. { while (c != '\r' && (int)c != EOF && soap_blank(c))
  14245. c = soap_getchar(soap);
  14246. if (c != '\r' || soap_getchar(soap) != '\n')
  14247. { soap->error = SOAP_MIME_ERROR;
  14248. return NULL;
  14249. }
  14250. if (soap_getmimehdr(soap))
  14251. return NULL;
  14252. }
  14253. return content;
  14254. }
  14255. #endif
  14256. #endif
  14257. /******************************************************************************/
  14258. #ifndef WITH_LEANER
  14259. #ifndef PALM_1
  14260. SOAP_FMAC1
  14261. int
  14262. SOAP_FMAC2
  14263. soap_match_cid(struct soap *soap, const char *s, const char *t)
  14264. { register size_t n;
  14265. if (!s)
  14266. return 1;
  14267. if (!strcmp(s, t))
  14268. return 0;
  14269. if (!strncmp(s, "cid:", 4))
  14270. s += 4;
  14271. n = strlen(t);
  14272. if (*t == '<')
  14273. { t++;
  14274. n -= 2;
  14275. }
  14276. if (!strncmp(s, t, n) && !s[n])
  14277. return 0;
  14278. soap_decode(soap->tmpbuf, sizeof(soap->tmpbuf), s, SOAP_STR_EOS);
  14279. if (!strncmp(soap->tmpbuf, t, n) && !soap->tmpbuf[n])
  14280. return 0;
  14281. return 1;
  14282. }
  14283. #endif
  14284. #endif
  14285. /******************************************************************************/
  14286. #ifndef WITH_LEANER
  14287. #ifndef PALM_1
  14288. static void
  14289. soap_resolve_attachment(struct soap *soap, struct soap_multipart *content)
  14290. { if (content->id)
  14291. { register struct soap_xlist **xp = &soap->xlist;
  14292. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving attachment data for id='%s'\n", content->id));
  14293. while (*xp)
  14294. { register struct soap_xlist *xq = *xp;
  14295. if (!soap_match_cid(soap, xq->id, content->id))
  14296. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Found matching attachment id='%s' for content id='%s'\n", xq->id, content->id));
  14297. *xp = xq->next;
  14298. *xq->ptr = (unsigned char*)content->ptr;
  14299. *xq->size = (int)content->size;
  14300. *xq->type = (char*)content->type;
  14301. if (content->options)
  14302. *xq->options = (char*)content->options;
  14303. else
  14304. *xq->options = (char*)content->description;
  14305. SOAP_FREE(soap, xq);
  14306. }
  14307. else
  14308. xp = &(*xp)->next;
  14309. }
  14310. }
  14311. }
  14312. #endif
  14313. #endif
  14314. /******************************************************************************/
  14315. #ifndef WITH_LEANER
  14316. #ifndef PALM_1
  14317. SOAP_FMAC1
  14318. int
  14319. SOAP_FMAC2
  14320. soap_putmimehdr(struct soap *soap, struct soap_multipart *content)
  14321. { const char *s;
  14322. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MIME attachment type='%s'\n", content->type ? content->type : SOAP_STR_EOS));
  14323. if (soap_send3(soap, "\r\n--", soap->mime.boundary, "\r\n"))
  14324. return soap->error;
  14325. if (content->type && soap_send3(soap, "Content-Type: ", content->type, "\r\n"))
  14326. return soap->error;
  14327. s = soap_code_str(mime_codes, content->encoding);
  14328. if (s && soap_send3(soap, "Content-Transfer-Encoding: ", s, "\r\n"))
  14329. return soap->error;
  14330. if (content->id && soap_send3(soap, "Content-ID: ", content->id, "\r\n"))
  14331. return soap->error;
  14332. if (content->location && soap_send3(soap, "Content-Location: ", content->location, "\r\n"))
  14333. return soap->error;
  14334. if (content->description && soap_send3(soap, "Content-Description: ", content->description, "\r\n"))
  14335. return soap->error;
  14336. return soap_send_raw(soap, "\r\n", 2);
  14337. }
  14338. #endif
  14339. #endif
  14340. /******************************************************************************/
  14341. #ifndef WITH_LEANER
  14342. #ifndef PALM_1
  14343. SOAP_FMAC1
  14344. int
  14345. SOAP_FMAC2
  14346. soap_putmime(struct soap *soap)
  14347. { struct soap_multipart *content;
  14348. if (!(soap->mode & SOAP_ENC_MIME) || !soap->mime.boundary)
  14349. return SOAP_OK;
  14350. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending MIME attachments\n"));
  14351. for (content = soap->mime.first; content; content = content->next)
  14352. { void *handle;
  14353. if (soap->fmimereadopen && ((handle = soap->fmimereadopen(soap, (void*)content->ptr, content->id, content->type, content->description)) || soap->error))
  14354. { size_t size = content->size;
  14355. if (!handle)
  14356. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimereadopen failed\n"));
  14357. return soap->error;
  14358. }
  14359. if (soap_putmimehdr(soap, content))
  14360. return soap->error;
  14361. if (!size)
  14362. { if ((soap->mode & SOAP_ENC_XML) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_IO) == SOAP_IO_STORE)
  14363. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming MIME\n"));
  14364. do
  14365. { size = soap->fmimeread(soap, handle, soap->tmpbuf, sizeof(soap->tmpbuf));
  14366. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimeread returned %lu bytes\n", (unsigned long)size));
  14367. if (soap_send_raw(soap, soap->tmpbuf, size))
  14368. break;
  14369. } while (size);
  14370. }
  14371. else
  14372. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error: cannot chunk streaming MIME (no HTTP chunking)\n"));
  14373. }
  14374. }
  14375. else
  14376. { do
  14377. { size_t bufsize;
  14378. if (size < sizeof(soap->tmpbuf))
  14379. bufsize = size;
  14380. else
  14381. bufsize = sizeof(soap->tmpbuf);
  14382. if (!(bufsize = soap->fmimeread(soap, handle, soap->tmpbuf, bufsize)))
  14383. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n", (unsigned long)size, (unsigned long)content->size));
  14384. soap->error = SOAP_EOF;
  14385. break;
  14386. }
  14387. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  14388. break;
  14389. size -= bufsize;
  14390. } while (size);
  14391. }
  14392. if (soap->fmimereadclose)
  14393. soap->fmimereadclose(soap, handle);
  14394. }
  14395. else
  14396. { if (soap_putmimehdr(soap, content)
  14397. || soap_send_raw(soap, content->ptr, content->size))
  14398. return soap->error;
  14399. }
  14400. }
  14401. return soap_send3(soap, "\r\n--", soap->mime.boundary, "--");
  14402. }
  14403. #endif
  14404. #endif
  14405. /******************************************************************************/
  14406. #ifndef WITH_LEANER
  14407. #ifndef PALM_1
  14408. SOAP_FMAC1
  14409. void
  14410. SOAP_FMAC2
  14411. soap_set_dime(struct soap *soap)
  14412. { soap->omode |= SOAP_ENC_DIME;
  14413. soap->dime.first = NULL;
  14414. soap->dime.last = NULL;
  14415. }
  14416. #endif
  14417. #endif
  14418. /******************************************************************************/
  14419. #ifndef WITH_LEANER
  14420. #ifndef PALM_1
  14421. SOAP_FMAC1
  14422. void
  14423. SOAP_FMAC2
  14424. soap_set_mime(struct soap *soap, const char *boundary, const char *start)
  14425. { soap->omode |= SOAP_ENC_MIME;
  14426. soap->mime.first = NULL;
  14427. soap->mime.last = NULL;
  14428. soap->mime.boundary = soap_strdup(soap, boundary);
  14429. soap->mime.start = soap_strdup(soap, start);
  14430. }
  14431. #endif
  14432. #endif
  14433. /******************************************************************************/
  14434. #ifndef WITH_LEANER
  14435. #ifndef PALM_1
  14436. SOAP_FMAC1
  14437. void
  14438. SOAP_FMAC2
  14439. soap_clr_dime(struct soap *soap)
  14440. { soap->omode &= ~SOAP_ENC_DIME;
  14441. soap->dime.first = NULL;
  14442. soap->dime.last = NULL;
  14443. }
  14444. #endif
  14445. #endif
  14446. /******************************************************************************/
  14447. #ifndef WITH_LEANER
  14448. #ifndef PALM_1
  14449. SOAP_FMAC1
  14450. void
  14451. SOAP_FMAC2
  14452. soap_clr_mime(struct soap *soap)
  14453. { soap->omode &= ~SOAP_ENC_MIME;
  14454. soap->mime.first = NULL;
  14455. soap->mime.last = NULL;
  14456. soap->mime.boundary = NULL;
  14457. soap->mime.start = NULL;
  14458. }
  14459. #endif
  14460. #endif
  14461. /******************************************************************************/
  14462. #ifndef WITH_LEANER
  14463. #ifndef PALM_1
  14464. static struct soap_multipart*
  14465. soap_new_multipart(struct soap *soap, struct soap_multipart **first, struct soap_multipart **last, char *ptr, size_t size)
  14466. { struct soap_multipart *content;
  14467. content = (struct soap_multipart*)soap_malloc(soap, sizeof(struct soap_multipart));
  14468. if (content)
  14469. { content->next = NULL;
  14470. content->ptr = ptr;
  14471. content->size = size;
  14472. content->id = NULL;
  14473. content->type = NULL;
  14474. content->options = NULL;
  14475. content->encoding = SOAP_MIME_NONE;
  14476. content->location = NULL;
  14477. content->description = NULL;
  14478. if (!*first)
  14479. *first = content;
  14480. if (*last)
  14481. (*last)->next = content;
  14482. *last = content;
  14483. }
  14484. return content;
  14485. }
  14486. #endif
  14487. #endif
  14488. /******************************************************************************/
  14489. #ifndef WITH_LEANER
  14490. #ifndef PALM_1
  14491. SOAP_FMAC1
  14492. int
  14493. SOAP_FMAC2
  14494. soap_set_dime_attachment(struct soap *soap, char *ptr, size_t size, const char *type, const char *id, unsigned short optype, const char *option)
  14495. { struct soap_multipart *content = soap_new_multipart(soap, &soap->dime.first, &soap->dime.last, ptr, size);
  14496. if (!content)
  14497. return SOAP_EOM;
  14498. content->id = soap_strdup(soap, id);
  14499. content->type = soap_strdup(soap, type);
  14500. content->options = soap_dime_option(soap, optype, option);
  14501. return SOAP_OK;
  14502. }
  14503. #endif
  14504. #endif
  14505. /******************************************************************************/
  14506. #ifndef WITH_LEANER
  14507. #ifndef PALM_1
  14508. SOAP_FMAC1
  14509. int
  14510. SOAP_FMAC2
  14511. 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)
  14512. { struct soap_multipart *content = soap_new_multipart(soap, &soap->mime.first, &soap->mime.last, ptr, size);
  14513. if (!content)
  14514. return SOAP_EOM;
  14515. content->id = soap_strdup(soap, id);
  14516. content->type = soap_strdup(soap, type);
  14517. content->encoding = encoding;
  14518. content->location = soap_strdup(soap, location);
  14519. content->description = soap_strdup(soap, description);
  14520. return SOAP_OK;
  14521. }
  14522. #endif
  14523. #endif
  14524. /******************************************************************************/
  14525. #ifndef WITH_LEANER
  14526. #ifndef PALM_1
  14527. SOAP_FMAC1
  14528. struct soap_multipart*
  14529. SOAP_FMAC2
  14530. soap_next_multipart(struct soap_multipart *content)
  14531. { if (content)
  14532. return content->next;
  14533. return NULL;
  14534. }
  14535. #endif
  14536. #endif
  14537. /******************************************************************************/
  14538. #ifndef WITH_LEANER
  14539. #ifndef PALM_1
  14540. static void
  14541. soap_select_mime_boundary(struct soap *soap)
  14542. { while (!soap->mime.boundary || soap_valid_mime_boundary(soap))
  14543. { register char *s = soap->mime.boundary;
  14544. register size_t n = 0;
  14545. if (s)
  14546. n = strlen(s);
  14547. if (n < 16)
  14548. { n = 64;
  14549. s = soap->mime.boundary = (char*)soap_malloc(soap, n + 1);
  14550. if (!s)
  14551. return;
  14552. }
  14553. strcpy(s, "==");
  14554. s += 2;
  14555. n -= 4;
  14556. while (n)
  14557. { *s++ = soap_base64o[soap_random & 0x3F];
  14558. n--;
  14559. }
  14560. strcpy(s, "==");
  14561. }
  14562. if (!soap->mime.start)
  14563. soap->mime.start = "<SOAP-ENV:Envelope>";
  14564. }
  14565. #endif
  14566. #endif
  14567. /******************************************************************************/
  14568. #ifndef WITH_LEANER
  14569. #ifndef PALM_1
  14570. static int
  14571. soap_valid_mime_boundary(struct soap *soap)
  14572. { register struct soap_multipart *content;
  14573. register size_t k;
  14574. if (soap->fmimeread)
  14575. return SOAP_OK;
  14576. k = strlen(soap->mime.boundary);
  14577. for (content = soap->mime.first; content; content = content->next)
  14578. { if (content->ptr && content->size >= k)
  14579. { register const char *p = (const char*)content->ptr;
  14580. register size_t i;
  14581. for (i = 0; i < content->size - k; i++, p++)
  14582. { if (!strncmp(p, soap->mime.boundary, k))
  14583. return SOAP_ERR;
  14584. }
  14585. }
  14586. }
  14587. return SOAP_OK;
  14588. }
  14589. #endif
  14590. #endif
  14591. /******************************************************************************/
  14592. #ifdef WITH_GZIP
  14593. #ifndef PALM_1
  14594. static int
  14595. soap_getgziphdr(struct soap *soap)
  14596. { int i;
  14597. soap_wchar c = 0, f = 0;
  14598. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get gzip header\n"));
  14599. for (i = 0; i < 9; i++)
  14600. { if ((int)(c = soap_get1(soap) == EOF))
  14601. return soap->error = SOAP_ZLIB_ERROR;
  14602. if (i == 1 && c == 8)
  14603. soap->z_dict = 0;
  14604. if (i == 2)
  14605. f = c;
  14606. }
  14607. if (f & 0x04) /* FEXTRA */
  14608. { for (i = soap_get1(soap) | (soap_get1(soap) << 8); i; i--)
  14609. { if ((int)soap_get1(soap) == EOF)
  14610. return soap->error = SOAP_ZLIB_ERROR;
  14611. }
  14612. }
  14613. if (f & 0x08) /* skip FNAME */
  14614. { do
  14615. c = soap_get1(soap);
  14616. while (c && (int)c != EOF);
  14617. }
  14618. if ((int)c != EOF && (f & 0x10)) /* skip FCOMMENT */
  14619. { do
  14620. c = soap_get1(soap);
  14621. while (c && (int)c != EOF);
  14622. }
  14623. if ((int)c != EOF && (f & 0x02)) /* skip FHCRC (CRC32 is used) */
  14624. { if ((int)(c = soap_get1(soap)) != EOF)
  14625. c = soap_get1(soap);
  14626. }
  14627. if ((int)c == EOF)
  14628. return soap->error = SOAP_ZLIB_ERROR;
  14629. return SOAP_OK;
  14630. }
  14631. #endif
  14632. #endif
  14633. /******************************************************************************/
  14634. #ifndef PALM_1
  14635. SOAP_FMAC1
  14636. int
  14637. SOAP_FMAC2
  14638. soap_begin_serve(struct soap *soap)
  14639. {
  14640. #ifdef WITH_FASTCGI
  14641. if (FCGI_Accept() < 0)
  14642. { soap->error = SOAP_EOF;
  14643. return soap_send_fault(soap);
  14644. }
  14645. #endif
  14646. soap_begin(soap);
  14647. if (soap_begin_recv(soap)
  14648. || soap_envelope_begin_in(soap)
  14649. || soap_recv_header(soap)
  14650. || soap_body_begin_in(soap))
  14651. { if (soap->error < SOAP_STOP)
  14652. {
  14653. #ifdef WITH_FASTCGI
  14654. soap_send_fault(soap);
  14655. #else
  14656. return soap_send_fault(soap);
  14657. #endif
  14658. }
  14659. return soap_closesock(soap);
  14660. }
  14661. return SOAP_OK;
  14662. }
  14663. #endif
  14664. /******************************************************************************/
  14665. #ifndef PALM_1
  14666. SOAP_FMAC1
  14667. int
  14668. SOAP_FMAC2
  14669. soap_begin_recv(struct soap *soap)
  14670. { register soap_wchar c;
  14671. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Initializing for input from socket=%d/fd=%d\n", soap->socket, soap->recvfd));
  14672. soap->error = SOAP_OK;
  14673. soap->filterstop = SOAP_OK;
  14674. soap_free_temp(soap);
  14675. soap_set_local_namespaces(soap);
  14676. soap->version = 0; /* don't assume we're parsing SOAP content by default */
  14677. #ifndef WITH_NOIDREF
  14678. soap_free_iht(soap);
  14679. #endif
  14680. if ((soap->imode & SOAP_IO) == SOAP_IO_CHUNK)
  14681. soap->omode |= SOAP_IO_CHUNK;
  14682. soap->imode &= ~(SOAP_IO | SOAP_ENC_MIME);
  14683. soap->mode = soap->imode;
  14684. if (!soap->keep_alive)
  14685. { soap->buflen = 0;
  14686. soap->bufidx = 0;
  14687. }
  14688. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  14689. soap->keep_alive = 0;
  14690. soap->ahead = 0;
  14691. soap->peeked = 0;
  14692. soap->level = 0;
  14693. soap->part = SOAP_BEGIN;
  14694. soap->alloced = 0;
  14695. soap->body = 1;
  14696. soap->count = 0;
  14697. soap->length = 0;
  14698. soap->cdata = 0;
  14699. *soap->endpoint = '\0';
  14700. soap->action = NULL;
  14701. soap->header = NULL;
  14702. soap->fault = NULL;
  14703. soap->status = 0;
  14704. soap->fform = NULL;
  14705. #ifndef WITH_LEANER
  14706. soap->dom = NULL;
  14707. soap->dime.chunksize = 0;
  14708. soap->dime.buflen = 0;
  14709. soap->dime.list = NULL;
  14710. soap->dime.first = NULL;
  14711. soap->dime.last = NULL;
  14712. soap->mime.list = NULL;
  14713. soap->mime.first = NULL;
  14714. soap->mime.last = NULL;
  14715. soap->mime.boundary = NULL;
  14716. soap->mime.start = NULL;
  14717. #endif
  14718. #ifdef WIN32
  14719. #ifndef UNDER_CE
  14720. #ifndef WITH_FASTCGI
  14721. if (!soap_valid_socket(soap->socket) && !soap->is) /* Set win32 stdout or soap->sendfd to BINARY, e.g. to support DIME */
  14722. #ifdef __BORLANDC__
  14723. setmode(soap->recvfd, _O_BINARY);
  14724. #else
  14725. _setmode(soap->recvfd, _O_BINARY);
  14726. #endif
  14727. #endif
  14728. #endif
  14729. #endif
  14730. #ifdef WITH_ZLIB
  14731. soap->mode &= ~SOAP_ENC_ZLIB;
  14732. soap->zlib_in = SOAP_ZLIB_NONE;
  14733. soap->zlib_out = SOAP_ZLIB_NONE;
  14734. soap->d_stream->next_in = Z_NULL;
  14735. soap->d_stream->avail_in = 0;
  14736. soap->d_stream->next_out = (Byte*)soap->buf;
  14737. soap->d_stream->avail_out = SOAP_BUFLEN;
  14738. soap->z_ratio_in = 1.0;
  14739. #endif
  14740. #ifdef WITH_OPENSSL
  14741. if (soap->ssl)
  14742. ERR_clear_error();
  14743. #endif
  14744. #ifndef WITH_LEANER
  14745. if (soap->fprepareinitrecv && (soap->error = soap->fprepareinitrecv(soap)))
  14746. return soap->error;
  14747. #endif
  14748. c = soap_getchar(soap);
  14749. #ifdef WITH_GZIP
  14750. if (c == 0x1F)
  14751. { if (soap_getgziphdr(soap))
  14752. return soap->error;
  14753. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  14754. return soap->error = SOAP_ZLIB_ERROR;
  14755. if (soap->z_dict)
  14756. { if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  14757. return soap->error = SOAP_ZLIB_ERROR;
  14758. }
  14759. soap->zlib_state = SOAP_ZLIB_INFLATE;
  14760. soap->mode |= SOAP_ENC_ZLIB;
  14761. soap->zlib_in = SOAP_ZLIB_GZIP;
  14762. soap->z_crc = crc32(0L, NULL, 0);
  14763. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  14764. if (!soap->z_buf)
  14765. soap->z_buf = (char*)SOAP_MALLOC(soap, SOAP_BUFLEN);
  14766. memcpy(soap->z_buf, soap->buf, SOAP_BUFLEN);
  14767. /* should not chunk over plain transport, so why bother to check? */
  14768. /* if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) */
  14769. /* soap->z_buflen = soap->bufidx; */
  14770. /* else */
  14771. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  14772. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  14773. soap->z_buflen = soap->buflen;
  14774. soap->buflen = soap->bufidx;
  14775. c = ' ';
  14776. }
  14777. #endif
  14778. while (soap_blank(c))
  14779. c = soap_getchar(soap);
  14780. #ifndef WITH_LEANER
  14781. if (c == '-' && soap_get0(soap) == '-')
  14782. soap->mode |= SOAP_ENC_MIME;
  14783. else if ((c & 0xFFFC) == (SOAP_DIME_VERSION | SOAP_DIME_MB) && (soap_get0(soap) & 0xFFF0) == 0x20)
  14784. soap->mode |= SOAP_ENC_DIME;
  14785. else
  14786. #endif
  14787. { /* skip BOM */
  14788. if (c == 0xEF && soap_get0(soap) == 0xBB)
  14789. { c = soap_get1(soap);
  14790. if ((c = soap_get1(soap)) == 0xBF)
  14791. { soap->mode &= ~SOAP_ENC_LATIN;
  14792. c = soap_getchar(soap);
  14793. }
  14794. else
  14795. c = (0x0F << 12) | (0xBB << 6) | (c & 0x3F); /* UTF-8 */
  14796. }
  14797. else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  14798. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  14799. return soap->error = SOAP_UTF_ERROR;
  14800. /* skip space */
  14801. while (soap_blank(c))
  14802. c = soap_getchar(soap);
  14803. }
  14804. if ((int)c == EOF)
  14805. return soap->error = SOAP_CHK_EOF;
  14806. soap_unget(soap, c);
  14807. #ifndef WITH_NOHTTP
  14808. /* if not XML/MIME/DIME/ZLIB, assume HTTP method or status line */
  14809. if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && !(soap->mode & (SOAP_ENC_MIME | SOAP_ENC_DIME | SOAP_ENC_ZLIB | SOAP_ENC_XML)))
  14810. { soap_mode m = soap->imode;
  14811. soap->mode &= ~SOAP_IO;
  14812. soap->error = soap->fparse(soap);
  14813. if (soap->error && soap->error < SOAP_STOP)
  14814. { soap->keep_alive = 0; /* force close later */
  14815. return soap->error;
  14816. }
  14817. if (soap->error == SOAP_STOP)
  14818. { if (soap->fform)
  14819. { soap->error = soap->fform(soap);
  14820. if (soap->error == SOAP_OK)
  14821. soap->error = SOAP_STOP; /* prevents further processing */
  14822. }
  14823. return soap->error;
  14824. }
  14825. soap->mode = soap->imode; /* if imode is changed, effectuate */
  14826. soap->imode = m; /* restore imode */
  14827. #ifdef WITH_ZLIB
  14828. soap->mode &= ~SOAP_ENC_ZLIB;
  14829. #endif
  14830. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  14831. { soap->chunkbuflen = soap->buflen;
  14832. soap->buflen = soap->bufidx;
  14833. soap->chunksize = 0;
  14834. }
  14835. /* Note: fparse should not use soap_unget to push back last char */
  14836. #if 0
  14837. if (soap->status > 200 && soap->length == 0 && !(soap->http_content && (!soap->keep_alive || soap->recv_timeout)) && (soap->imode & SOAP_IO) != SOAP_IO_CHUNK)
  14838. #endif
  14839. if (soap->status && !soap->body)
  14840. return soap->error = soap->status;
  14841. #ifdef WITH_ZLIB
  14842. if (soap->zlib_in != SOAP_ZLIB_NONE)
  14843. {
  14844. #ifdef WITH_GZIP
  14845. if (soap->zlib_in != SOAP_ZLIB_DEFLATE)
  14846. { c = soap_get1(soap);
  14847. if (c == (int)EOF)
  14848. return soap->error = SOAP_EOF;
  14849. if (c == 0x1F)
  14850. { if (soap_getgziphdr(soap))
  14851. return soap->error;
  14852. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  14853. return soap->error = SOAP_ZLIB_ERROR;
  14854. soap->z_crc = crc32(0L, NULL, 0);
  14855. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  14856. }
  14857. else
  14858. { soap_revget1(soap);
  14859. if (inflateInit(soap->d_stream) != Z_OK)
  14860. return soap->error = SOAP_ZLIB_ERROR;
  14861. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  14862. }
  14863. }
  14864. else
  14865. #endif
  14866. if (inflateInit(soap->d_stream) != Z_OK)
  14867. return soap->error = SOAP_ZLIB_ERROR;
  14868. if (soap->z_dict)
  14869. { if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  14870. return soap->error = SOAP_ZLIB_ERROR;
  14871. }
  14872. soap->zlib_state = SOAP_ZLIB_INFLATE;
  14873. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate initialized\n"));
  14874. soap->mode |= SOAP_ENC_ZLIB;
  14875. if (!soap->z_buf)
  14876. soap->z_buf = (char*)SOAP_MALLOC(soap, SOAP_BUFLEN);
  14877. memcpy(soap->z_buf, soap->buf, SOAP_BUFLEN);
  14878. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  14879. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  14880. soap->z_buflen = soap->buflen;
  14881. soap->buflen = soap->bufidx;
  14882. }
  14883. #endif
  14884. #ifndef WITH_LEANER
  14885. if (soap->fpreparerecv && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK && soap->buflen > soap->bufidx)
  14886. { int r;
  14887. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking fpreparerecv\n"));
  14888. if ((r = soap->fpreparerecv(soap, soap->buf + soap->bufidx, soap->buflen - soap->bufidx)))
  14889. return soap->error = r;
  14890. }
  14891. #endif
  14892. if (soap_get0(soap) == (int)EOF)
  14893. { if (soap->status == 0 || soap->status == 200)
  14894. return soap->error = SOAP_NO_DATA; /* HTTP OK: always expect data */
  14895. return soap->error = soap->status;
  14896. }
  14897. if (soap->error)
  14898. { if (soap->error == SOAP_FORM && soap->fform)
  14899. { soap->error = soap->fform(soap);
  14900. if (soap->error == SOAP_OK)
  14901. soap->error = SOAP_STOP; /* prevents further processing */
  14902. }
  14903. return soap->error;
  14904. }
  14905. }
  14906. #endif
  14907. #ifndef WITH_LEANER
  14908. if (soap->mode & SOAP_ENC_MIME)
  14909. { do /* skip preamble */
  14910. { if ((int)(c = soap_getchar(soap)) == EOF)
  14911. return soap->error = SOAP_CHK_EOF;
  14912. } while (c != '-' || soap_get0(soap) != '-');
  14913. soap_unget(soap, c);
  14914. if (soap_getmimehdr(soap))
  14915. return soap->error;
  14916. if (soap->mime.start)
  14917. { do
  14918. { if (!soap->mime.last->id)
  14919. break;
  14920. if (!soap_match_cid(soap, soap->mime.start, soap->mime.last->id))
  14921. break;
  14922. } while (soap_get_mime_attachment(soap, NULL));
  14923. }
  14924. if (soap_get_header_attribute(soap, soap->mime.first->type, "application/dime"))
  14925. soap->mode |= SOAP_ENC_DIME;
  14926. }
  14927. if (soap->mode & SOAP_ENC_DIME)
  14928. { if (soap_getdimehdr(soap))
  14929. return soap->error;
  14930. if (soap->dime.flags & SOAP_DIME_CF)
  14931. { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked DIME SOAP message\n"));
  14932. soap->dime.chunksize = soap->dime.size;
  14933. if (soap->buflen - soap->bufidx >= soap->dime.chunksize)
  14934. { soap->dime.buflen = soap->buflen;
  14935. soap->buflen = soap->bufidx + soap->dime.chunksize;
  14936. }
  14937. else
  14938. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  14939. }
  14940. soap->count = soap->buflen - soap->bufidx;
  14941. }
  14942. #endif
  14943. return SOAP_OK;
  14944. }
  14945. #endif
  14946. /******************************************************************************/
  14947. #ifndef PALM_2
  14948. SOAP_FMAC1
  14949. int
  14950. SOAP_FMAC2
  14951. soap_envelope_begin_out(struct soap *soap)
  14952. {
  14953. #ifndef WITH_LEANER
  14954. size_t n = 0;
  14955. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary && soap->mime.start && strlen(soap->mime.boundary) + strlen(soap->mime.start) < sizeof(soap->tmpbuf) - 80 )
  14956. { const char *s;
  14957. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  14958. s = "application/dime";
  14959. else if (soap->version == 2)
  14960. { if (soap->mode & SOAP_ENC_MTOM)
  14961. s = "application/xop+xml; charset=utf-8; type=\"application/soap+xml\"";
  14962. else
  14963. s = "application/soap+xml; charset=utf-8";
  14964. }
  14965. else if (soap->mode & SOAP_ENC_MTOM)
  14966. s = "application/xop+xml; charset=utf-8; type=\"text/xml\"";
  14967. else
  14968. s = "text/xml; charset=utf-8";
  14969. #ifdef HAVE_SNPRINTF
  14970. 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);
  14971. #else
  14972. 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);
  14973. #endif
  14974. n = strlen(soap->tmpbuf);
  14975. if (soap_send_raw(soap, soap->tmpbuf, n))
  14976. return soap->error;
  14977. }
  14978. if (soap->mode & SOAP_IO_LENGTH)
  14979. soap->dime.size = soap->count; /* DIME in MIME correction */
  14980. if (!(soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME))
  14981. { if (soap_putdimehdr(soap))
  14982. return soap->error;
  14983. }
  14984. #endif
  14985. if (soap->version == 0)
  14986. return SOAP_OK;
  14987. soap->part = SOAP_IN_ENVELOPE;
  14988. return soap_element_begin_out(soap, "SOAP-ENV:Envelope", 0, NULL);
  14989. }
  14990. #endif
  14991. /******************************************************************************/
  14992. #ifndef PALM_2
  14993. SOAP_FMAC1
  14994. int
  14995. SOAP_FMAC2
  14996. soap_envelope_end_out(struct soap *soap)
  14997. { if (soap->version == 0)
  14998. return SOAP_OK;
  14999. if (soap_element_end_out(soap, "SOAP-ENV:Envelope")
  15000. || soap_send_raw(soap, "\r\n", 2)) /* 2.8: always emit \r\n */
  15001. return soap->error;
  15002. #ifndef WITH_LEANER
  15003. if ((soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  15004. { soap->dime.size = soap->count - soap->dime.size; /* DIME in MIME correction */
  15005. #ifdef HAVE_SNPRINTF
  15006. soap_snprintf(soap->id, sizeof(soap->id), soap->dime_id_format, 0);
  15007. #else
  15008. sprintf(soap->id, soap->dime_id_format, 0);
  15009. #endif
  15010. soap->dime.id = soap->id;
  15011. if (soap->local_namespaces)
  15012. { if (soap->local_namespaces[0].out)
  15013. soap->dime.type = (char*)soap->local_namespaces[0].out;
  15014. else
  15015. soap->dime.type = (char*)soap->local_namespaces[0].ns;
  15016. }
  15017. soap->dime.options = NULL;
  15018. soap->dime.flags = SOAP_DIME_MB | SOAP_DIME_ABSURI;
  15019. if (!soap->dime.first)
  15020. soap->dime.flags |= SOAP_DIME_ME;
  15021. soap->count += 12 + ((strlen(soap->dime.id)+3)&(~3)) + (soap->dime.type ? ((strlen(soap->dime.type)+3)&(~3)) : 0);
  15022. }
  15023. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  15024. return soap_send_raw(soap, SOAP_STR_PADDING, -(long)soap->dime.size&3);
  15025. #endif
  15026. soap->part = SOAP_END_ENVELOPE;
  15027. return SOAP_OK;
  15028. }
  15029. #endif
  15030. /******************************************************************************/
  15031. #ifndef WITH_LEAN
  15032. #ifndef PALM_1
  15033. SOAP_FMAC1
  15034. char*
  15035. SOAP_FMAC2
  15036. soap_get_http_body(struct soap *soap, size_t *len)
  15037. { if (len)
  15038. *len = 0;
  15039. #ifndef WITH_LEAN
  15040. register size_t l = 0, n = 0;
  15041. register char *s;
  15042. /* get HTTP body length */
  15043. if (!(soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK)
  15044. { n = soap->length;
  15045. if (!n)
  15046. return NULL;
  15047. }
  15048. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Parsing HTTP body (mode=0x%x,len=%lu)\n", soap->mode, (unsigned long)n));
  15049. #ifdef WITH_FAST
  15050. soap->labidx = 0; /* use look-aside buffer */
  15051. #else
  15052. if (soap_new_block(soap) == NULL)
  15053. return NULL;
  15054. #endif
  15055. for (;;)
  15056. {
  15057. #ifdef WITH_FAST
  15058. register size_t i, k;
  15059. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  15060. return NULL;
  15061. s = soap->labbuf + soap->labidx; /* space to populate */
  15062. k = soap->lablen - soap->labidx; /* number of bytes available */
  15063. soap->labidx = soap->lablen; /* claim this space */
  15064. #else
  15065. register size_t i, k = SOAP_BLKLEN;
  15066. if (!(s = (char*)soap_push_block(soap, NULL, k)))
  15067. return NULL;
  15068. #endif
  15069. for (i = 0; i < k; i++)
  15070. { register soap_wchar c;
  15071. l++;
  15072. if (n > 0 && l > n)
  15073. goto end;
  15074. c = soap_get1(soap);
  15075. if ((int)c == EOF)
  15076. goto end;
  15077. *s++ = (char)(c & 0xFF);
  15078. }
  15079. }
  15080. end:
  15081. *s = '\0';
  15082. if (len)
  15083. *len = l - 1; /* len excludes terminating \0 */
  15084. #ifdef WITH_FAST
  15085. if ((s = (char*)soap_malloc(soap, l)))
  15086. memcpy(s, soap->labbuf, l);
  15087. #else
  15088. soap_size_block(soap, NULL, i+1);
  15089. s = soap_save_block(soap, NULL, 0);
  15090. #endif
  15091. return s;
  15092. #else
  15093. return NULL;
  15094. #endif
  15095. }
  15096. #endif
  15097. #endif
  15098. /******************************************************************************/
  15099. #ifndef PALM_1
  15100. SOAP_FMAC1
  15101. int
  15102. SOAP_FMAC2
  15103. soap_envelope_begin_in(struct soap *soap)
  15104. { register struct Namespace *p;
  15105. soap->part = SOAP_IN_ENVELOPE;
  15106. if (soap_element_begin_in(soap, "SOAP-ENV:Envelope", 0, NULL))
  15107. { if (soap->error == SOAP_TAG_MISMATCH)
  15108. { if (!soap_element_begin_in(soap, "Envelope", 0, NULL))
  15109. soap->error = SOAP_VERSIONMISMATCH;
  15110. else if (soap->status == 0 || (soap->status >= 200 && soap->status <= 299))
  15111. return SOAP_OK; /* allow non-SOAP XML content to be captured */
  15112. soap->error = soap->status;
  15113. }
  15114. else if (soap->status)
  15115. soap->error = soap->status;
  15116. return soap->error;
  15117. }
  15118. p = soap->local_namespaces;
  15119. if (p)
  15120. { const char *ns = p[0].out;
  15121. if (!ns)
  15122. ns = p[0].ns;
  15123. if (!strcmp(ns, soap_env1))
  15124. { soap->version = 1; /* make sure we use SOAP 1.1 */
  15125. if (p[1].out)
  15126. SOAP_FREE(soap, p[1].out);
  15127. if ((p[1].out = (char*)SOAP_MALLOC(soap, sizeof(soap_enc1))))
  15128. strcpy(p[1].out, soap_enc1);
  15129. }
  15130. else if (!strcmp(ns, soap_env2))
  15131. { soap->version = 2; /* make sure we use SOAP 1.2 */
  15132. if (p[1].out)
  15133. SOAP_FREE(soap, p[1].out);
  15134. if ((p[1].out = (char*)SOAP_MALLOC(soap, sizeof(soap_enc2))))
  15135. strcpy(p[1].out, soap_enc2);
  15136. }
  15137. }
  15138. return SOAP_OK;
  15139. }
  15140. #endif
  15141. /******************************************************************************/
  15142. #ifndef PALM_1
  15143. SOAP_FMAC1
  15144. int
  15145. SOAP_FMAC2
  15146. soap_envelope_end_in(struct soap *soap)
  15147. { if (soap->version == 0)
  15148. return SOAP_OK;
  15149. soap->part = SOAP_END_ENVELOPE;
  15150. return soap_element_end_in(soap, "SOAP-ENV:Envelope");
  15151. }
  15152. #endif
  15153. /******************************************************************************/
  15154. #ifndef PALM_2
  15155. SOAP_FMAC1
  15156. int
  15157. SOAP_FMAC2
  15158. soap_body_begin_out(struct soap *soap)
  15159. { if (soap->version == 1)
  15160. soap->encoding = 1;
  15161. #ifndef WITH_LEAN
  15162. if ((soap->mode & SOAP_SEC_WSUID) && soap_set_attr(soap, "wsu:Id", "Body", 1))
  15163. return soap->error;
  15164. #endif
  15165. if (soap->version == 0)
  15166. return SOAP_OK;
  15167. soap->part = SOAP_IN_BODY;
  15168. return soap_element_begin_out(soap, "SOAP-ENV:Body", 0, NULL);
  15169. }
  15170. #endif
  15171. /******************************************************************************/
  15172. #ifndef PALM_2
  15173. SOAP_FMAC1
  15174. int
  15175. SOAP_FMAC2
  15176. soap_body_end_out(struct soap *soap)
  15177. { if (soap->version == 0)
  15178. return SOAP_OK;
  15179. if (soap_element_end_out(soap, "SOAP-ENV:Body"))
  15180. return soap->error;
  15181. soap->part = SOAP_END_BODY;
  15182. return SOAP_OK;
  15183. }
  15184. #endif
  15185. /******************************************************************************/
  15186. #ifndef PALM_2
  15187. SOAP_FMAC1
  15188. int
  15189. SOAP_FMAC2
  15190. soap_body_begin_in(struct soap *soap)
  15191. { if (soap->version == 0)
  15192. return SOAP_OK;
  15193. soap->part = SOAP_IN_BODY;
  15194. if (soap_element_begin_in(soap, "SOAP-ENV:Body", 0, NULL))
  15195. return soap->error;
  15196. if (!soap->body)
  15197. soap->part = SOAP_NO_BODY;
  15198. return SOAP_OK;
  15199. }
  15200. #endif
  15201. /******************************************************************************/
  15202. #ifndef PALM_2
  15203. SOAP_FMAC1
  15204. int
  15205. SOAP_FMAC2
  15206. soap_body_end_in(struct soap *soap)
  15207. { if (soap->version == 0)
  15208. return SOAP_OK;
  15209. if (soap->part == SOAP_NO_BODY)
  15210. return soap->error = SOAP_OK;
  15211. soap->part = SOAP_END_BODY;
  15212. return soap_element_end_in(soap, "SOAP-ENV:Body");
  15213. }
  15214. #endif
  15215. /******************************************************************************/
  15216. #ifndef PALM_2
  15217. SOAP_FMAC1
  15218. int
  15219. SOAP_FMAC2
  15220. soap_recv_header(struct soap *soap)
  15221. { if (soap_getheader(soap) && soap->error == SOAP_TAG_MISMATCH)
  15222. soap->error = SOAP_OK;
  15223. if (soap->error == SOAP_OK && soap->fheader)
  15224. soap->error = soap->fheader(soap);
  15225. return soap->error;
  15226. }
  15227. #endif
  15228. /******************************************************************************/
  15229. #ifndef PALM_1
  15230. SOAP_FMAC1
  15231. void
  15232. SOAP_FMAC2
  15233. soap_set_endpoint(struct soap *soap, const char *endpoint)
  15234. { register const char *s;
  15235. register size_t i, n;
  15236. soap->endpoint[0] = '\0';
  15237. soap->host[0] = '\0';
  15238. soap->path[0] = '/';
  15239. soap->path[1] = '\0';
  15240. soap->port = 80;
  15241. if (!endpoint || !*endpoint)
  15242. return;
  15243. #ifdef WITH_OPENSSL
  15244. if (!soap_tag_cmp(endpoint, "https:*"))
  15245. soap->port = 443;
  15246. #endif
  15247. strncpy(soap->endpoint, endpoint, sizeof(soap->endpoint));
  15248. soap->endpoint[sizeof(soap->endpoint) - 1] = '\0';
  15249. s = strchr(endpoint, ':');
  15250. if (s && s[1] == '/' && s[2] == '/')
  15251. s += 3;
  15252. else
  15253. s = endpoint;
  15254. n = strlen(s);
  15255. if (n >= sizeof(soap->host))
  15256. n = sizeof(soap->host) - 1;
  15257. #ifdef WITH_IPV6
  15258. if (s[0] == '[')
  15259. { s++;
  15260. for (i = 0; i < n; i++)
  15261. { if (s[i] == ']')
  15262. { s++;
  15263. --n;
  15264. break;
  15265. }
  15266. soap->host[i] = s[i];
  15267. }
  15268. }
  15269. else
  15270. { for (i = 0; i < n; i++)
  15271. { soap->host[i] = s[i];
  15272. if (s[i] == '/' || s[i] == ':')
  15273. break;
  15274. }
  15275. }
  15276. #else
  15277. for (i = 0; i < n; i++)
  15278. { soap->host[i] = s[i];
  15279. if (s[i] == '/' || s[i] == ':')
  15280. break;
  15281. }
  15282. #endif
  15283. soap->host[i] = '\0';
  15284. if (s[i] == ':')
  15285. { soap->port = (int)soap_strtol(s + i + 1, NULL, 10);
  15286. for (i++; i < n; i++)
  15287. if (s[i] == '/')
  15288. break;
  15289. }
  15290. if (i < n && s[i])
  15291. { strncpy(soap->path, s + i, sizeof(soap->path));
  15292. soap->path[sizeof(soap->path) - 1] = '\0';
  15293. }
  15294. }
  15295. #endif
  15296. /******************************************************************************/
  15297. #ifndef PALM_1
  15298. SOAP_FMAC1
  15299. int
  15300. SOAP_FMAC2
  15301. soap_connect(struct soap *soap, const char *endpoint, const char *action)
  15302. { return soap_connect_command(soap, SOAP_POST, endpoint, action);
  15303. }
  15304. #endif
  15305. /******************************************************************************/
  15306. #ifndef PALM_1
  15307. SOAP_FMAC1
  15308. int
  15309. SOAP_FMAC2
  15310. soap_connect_command(struct soap *soap, int http_command, const char *endpoints, const char *action)
  15311. { char *endpoint;
  15312. const char *s;
  15313. if (endpoints && (s = strchr(endpoints, ' ')))
  15314. { endpoint = (char*)SOAP_MALLOC(soap, strlen(endpoints) + 1);
  15315. for (;;)
  15316. { strncpy(endpoint, endpoints, s - endpoints);
  15317. endpoint[s - endpoints] = '\0';
  15318. if (soap_try_connect_command(soap, http_command, endpoint, action) != SOAP_TCP_ERROR)
  15319. break;
  15320. if (!*s)
  15321. break;
  15322. soap->error = SOAP_OK;
  15323. while (*s == ' ')
  15324. s++;
  15325. endpoints = s;
  15326. s = strchr(endpoints, ' ');
  15327. if (!s)
  15328. s = endpoints + strlen(endpoints);
  15329. }
  15330. SOAP_FREE(soap, endpoint);
  15331. }
  15332. else
  15333. soap_try_connect_command(soap, http_command, endpoints, action);
  15334. return soap->error;
  15335. }
  15336. #endif
  15337. /******************************************************************************/
  15338. #ifndef PALM_1
  15339. static int
  15340. soap_try_connect_command(struct soap *soap, int http_command, const char *endpoint, const char *action)
  15341. { char host[sizeof(soap->host)];
  15342. int port;
  15343. size_t count;
  15344. soap->error = SOAP_OK;
  15345. strcpy(host, soap->host); /* save previous host name: if != then reconnect */
  15346. port = soap->port; /* save previous port to compare */
  15347. soap->status = http_command;
  15348. soap_set_endpoint(soap, endpoint);
  15349. #ifndef WITH_LEANER
  15350. if (soap->fconnect)
  15351. { if ((soap->error = soap->fconnect(soap, endpoint, soap->host, soap->port)))
  15352. return soap->error;
  15353. }
  15354. else
  15355. #endif
  15356. soap->action = soap_strdup(soap, action);
  15357. if (soap->fopen && *soap->host)
  15358. { if (!soap->keep_alive || !soap_valid_socket(soap->socket) || strcmp(soap->host, host) || soap->port != port || !soap->fpoll || soap->fpoll(soap))
  15359. { soap->error = SOAP_OK;
  15360. #ifndef WITH_LEAN
  15361. if (!strncmp(endpoint, "soap.udp:", 9))
  15362. soap->omode |= SOAP_IO_UDP;
  15363. else
  15364. #endif
  15365. { soap->keep_alive = 0; /* to force close */
  15366. soap->omode &= ~SOAP_IO_UDP; /* to force close */
  15367. }
  15368. soap_closesock(soap);
  15369. 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));
  15370. if (!soap->keep_alive || !soap_valid_socket(soap->socket))
  15371. { soap->socket = soap->fopen(soap, endpoint, soap->host, soap->port);
  15372. if (soap->error)
  15373. return soap->error;
  15374. soap->keep_alive = ((soap->omode & SOAP_IO_KEEPALIVE) != 0);
  15375. }
  15376. }
  15377. }
  15378. #ifdef WITH_NTLM
  15379. if (soap_ntlm_handshake(soap, SOAP_GET, endpoint, soap->host, soap->port))
  15380. return soap->error;
  15381. #endif
  15382. count = soap_count_attachments(soap);
  15383. if (soap_begin_send(soap))
  15384. return soap->error;
  15385. if (http_command == SOAP_GET)
  15386. { soap->mode &= ~SOAP_IO;
  15387. soap->mode |= SOAP_IO_BUFFER;
  15388. }
  15389. #ifndef WITH_NOHTTP
  15390. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_XML) && endpoint)
  15391. { unsigned int k = soap->mode;
  15392. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  15393. if ((k & SOAP_IO) != SOAP_IO_FLUSH)
  15394. soap->mode |= SOAP_IO_BUFFER;
  15395. if ((soap->error = soap->fpost(soap, endpoint, soap->host, soap->port, soap->path, action, count)))
  15396. return soap->error;
  15397. #ifndef WITH_LEANER
  15398. if ((k & SOAP_IO) == SOAP_IO_CHUNK)
  15399. { if (soap_flush(soap))
  15400. return soap->error;
  15401. }
  15402. #endif
  15403. soap->mode = k;
  15404. }
  15405. if (http_command == SOAP_GET || http_command == SOAP_DEL)
  15406. return soap_end_send_flush(soap);
  15407. #endif
  15408. return SOAP_OK;
  15409. }
  15410. #endif
  15411. /******************************************************************************/
  15412. #ifdef WITH_NTLM
  15413. #ifndef PALM_1
  15414. static int
  15415. soap_ntlm_handshake(struct soap *soap, int command, const char *endpoint, const char *host, int port)
  15416. { /* requires libntlm from http://www.nongnu.org/libntlm/ */
  15417. const char *userid = (soap->proxy_userid ? soap->proxy_userid : soap->userid);
  15418. const char *passwd = (soap->proxy_passwd ? soap->proxy_passwd : soap->passwd);
  15419. struct SOAP_ENV__Header *oldheader;
  15420. if (soap->ntlm_challenge && userid && passwd && soap->authrealm)
  15421. { tSmbNtlmAuthRequest req;
  15422. tSmbNtlmAuthResponse res;
  15423. tSmbNtlmAuthChallenge ch;
  15424. short k = soap->keep_alive;
  15425. size_t l = soap->length;
  15426. size_t c = soap->count;
  15427. soap_mode m = soap->mode, o = soap->omode;
  15428. int s = soap->status;
  15429. char *a = soap->action;
  15430. short v = soap->version;
  15431. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM '%s'\n", soap->ntlm_challenge));
  15432. if (!*soap->ntlm_challenge)
  15433. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM S->C Type 1: received NTLM authentication challenge from server\n"));
  15434. /* S -> C 401 Unauthorized
  15435. WWW-Authenticate: NTLM
  15436. */
  15437. buildSmbNtlmAuthRequest(&req, userid, soap->authrealm);
  15438. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)&req, NULL, SmbLength(&req));
  15439. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM C->S Type 2: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  15440. /* C -> S GET ...
  15441. Authorization: NTLM TlRMTVNTUAABAAAAA7IAAAoACgApAAAACQAJACAAAABMSUdIVENJVFlVUlNBLU1JTk9S
  15442. */
  15443. soap->omode = SOAP_IO_BUFFER;
  15444. if (soap_begin_send(soap))
  15445. return soap->error;
  15446. soap->keep_alive = 1;
  15447. soap->status = command;
  15448. if (soap->fpost(soap, endpoint, host, port, soap->path, soap->action, 0)
  15449. || soap_end_send_flush(soap))
  15450. return soap->error;
  15451. soap->mode = m;
  15452. soap->keep_alive = k;
  15453. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM S->C Type 2: waiting on server NTLM response\n"));
  15454. oldheader = soap->header;
  15455. if (soap_begin_recv(soap))
  15456. if (soap->error == SOAP_EOF)
  15457. return soap->error;
  15458. soap_end_recv(soap);
  15459. soap->header = oldheader;
  15460. soap->length = l;
  15461. if (soap->status != 401 && soap->status != 407)
  15462. return soap->error = SOAP_NTLM_ERROR;
  15463. soap->error = SOAP_OK;
  15464. }
  15465. /* S -> C 401 Unauthorized
  15466. WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==
  15467. */
  15468. soap_base642s(soap, soap->ntlm_challenge, (char*)&ch, sizeof(tSmbNtlmAuthChallenge), NULL);
  15469. buildSmbNtlmAuthResponse(&ch, &res, userid, passwd);
  15470. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)&res, NULL, SmbLength(&res));
  15471. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "NTLM C->S Type 3: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  15472. /* C -> S GET ...
  15473. Authorization: NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABQAFABAAAAADAAMAFQAAAASABIAYAAAAAAAAACiAAAAAYIAAFUAUgBTAEEALQBNAEkATgBPAFIAWgBhAHAAaABvAGQATABJAEcASABUAEMASQBUAFkArYfKbe/jRoW5xDxHeoxC1gBmfWiS5+iX4OAN4xBKG/IFPwfH3agtPEia6YnhsADT
  15474. */
  15475. soap->userid = NULL;
  15476. soap->passwd = NULL;
  15477. soap->proxy_userid = NULL;
  15478. soap->proxy_passwd = NULL;
  15479. soap->keep_alive = k;
  15480. soap->length = l;
  15481. soap->count = c;
  15482. soap->mode = m;
  15483. soap->omode = o;
  15484. soap->status = s;
  15485. soap->action = a;
  15486. soap->version = v;
  15487. }
  15488. return SOAP_OK;
  15489. }
  15490. #endif
  15491. #endif
  15492. /******************************************************************************/
  15493. #ifndef WITH_LEAN
  15494. SOAP_FMAC1
  15495. char*
  15496. SOAP_FMAC2
  15497. soap_s2base64(struct soap *soap, const unsigned char *s, char *t, int n)
  15498. { register int i;
  15499. register unsigned long m;
  15500. register char *p;
  15501. if (!t)
  15502. t = (char*)soap_malloc(soap, (n + 2) / 3 * 4 + 1);
  15503. if (!t)
  15504. return NULL;
  15505. p = t;
  15506. t[0] = '\0';
  15507. if (!s)
  15508. return p;
  15509. for (; n > 2; n -= 3, s += 3)
  15510. { m = s[0];
  15511. m = (m << 8) | s[1];
  15512. m = (m << 8) | s[2];
  15513. for (i = 4; i > 0; m >>= 6)
  15514. t[--i] = soap_base64o[m & 0x3F];
  15515. t += 4;
  15516. }
  15517. t[0] = '\0';
  15518. if (n > 0) /* 0 < n <= 2 implies that t[0..4] is allocated (base64 scaling formula) */
  15519. { m = 0;
  15520. for (i = 0; i < n; i++)
  15521. m = (m << 8) | *s++;
  15522. for (; i < 3; i++)
  15523. m <<= 8;
  15524. for (i = 4; i > 0; m >>= 6)
  15525. t[--i] = soap_base64o[m & 0x3F];
  15526. for (i = 3; i > n; i--)
  15527. t[i] = '=';
  15528. t[4] = '\0';
  15529. }
  15530. return p;
  15531. }
  15532. #endif
  15533. /******************************************************************************/
  15534. #ifndef WITH_LEAN
  15535. SOAP_FMAC1
  15536. const char*
  15537. SOAP_FMAC2
  15538. soap_base642s(struct soap *soap, const char *s, char *t, size_t l, int *n)
  15539. { register size_t i, j;
  15540. register soap_wchar c;
  15541. register unsigned long m;
  15542. register const char *p;
  15543. if (!s || !*s)
  15544. { if (n)
  15545. *n = 0;
  15546. if (soap->error)
  15547. return NULL;
  15548. return SOAP_NON_NULL;
  15549. }
  15550. if (!t)
  15551. { l = (strlen(s) + 3) / 4 * 3 + 1; /* make sure enough space for \0 */
  15552. t = (char*)soap_malloc(soap, l);
  15553. }
  15554. if (!t)
  15555. return NULL;
  15556. p = t;
  15557. if (n)
  15558. *n = 0;
  15559. for (i = 0; ; i += 3, l -= 3)
  15560. { m = 0;
  15561. j = 0;
  15562. while (j < 4)
  15563. { c = *s++;
  15564. if (c == '=' || !c)
  15565. { if (l >= j - 1)
  15566. { switch (j)
  15567. { case 2:
  15568. *t++ = (char)((m >> 4) & 0xFF);
  15569. i++;
  15570. l--;
  15571. break;
  15572. case 3:
  15573. *t++ = (char)((m >> 10) & 0xFF);
  15574. *t++ = (char)((m >> 2) & 0xFF);
  15575. i += 2;
  15576. l -= 2;
  15577. }
  15578. }
  15579. if (n)
  15580. *n = (int)i;
  15581. if (l)
  15582. *t = '\0';
  15583. return p;
  15584. }
  15585. c -= '+';
  15586. if (c >= 0 && c <= 79)
  15587. { int b = soap_base64i[c];
  15588. if (b >= 64)
  15589. { soap->error = SOAP_TYPE;
  15590. return NULL;
  15591. }
  15592. m = (m << 6) + b;
  15593. j++;
  15594. }
  15595. else if (!soap_blank(c + '+'))
  15596. { soap->error = SOAP_TYPE;
  15597. return NULL;
  15598. }
  15599. }
  15600. if (l < 3)
  15601. { if (n)
  15602. *n = (int)i;
  15603. if (l)
  15604. *t = '\0';
  15605. return p;
  15606. }
  15607. *t++ = (char)((m >> 16) & 0xFF);
  15608. *t++ = (char)((m >> 8) & 0xFF);
  15609. *t++ = (char)(m & 0xFF);
  15610. }
  15611. }
  15612. #endif
  15613. /******************************************************************************/
  15614. #ifndef WITH_LEAN
  15615. SOAP_FMAC1
  15616. char*
  15617. SOAP_FMAC2
  15618. soap_s2hex(struct soap *soap, const unsigned char *s, char *t, int n)
  15619. { register char *p;
  15620. if (!t)
  15621. t = (char*)soap_malloc(soap, 2 * n + 1);
  15622. if (!t)
  15623. return NULL;
  15624. p = t;
  15625. t[0] = '\0';
  15626. if (s)
  15627. { for (; n > 0; n--)
  15628. { register int m = *s++;
  15629. *t++ = (char)((m >> 4) + (m > 159 ? 'a' - 10 : '0'));
  15630. m &= 0x0F;
  15631. *t++ = (char)(m + (m > 9 ? 'a' - 10 : '0'));
  15632. }
  15633. }
  15634. *t++ = '\0';
  15635. return p;
  15636. }
  15637. #endif
  15638. /******************************************************************************/
  15639. #ifndef WITH_LEAN
  15640. SOAP_FMAC1
  15641. const char*
  15642. SOAP_FMAC2
  15643. soap_hex2s(struct soap *soap, const char *s, char *t, size_t l, int *n)
  15644. { register const char *p;
  15645. if (!s || !*s)
  15646. { if (n)
  15647. *n = 0;
  15648. if (soap->error)
  15649. return NULL;
  15650. return SOAP_NON_NULL;
  15651. }
  15652. if (!t)
  15653. { l = strlen(s) / 2 + 1; /* make sure enough space for \0 */
  15654. t = (char*)soap_malloc(soap, l);
  15655. }
  15656. if (!t)
  15657. return NULL;
  15658. p = t;
  15659. while (l)
  15660. { register int d1, d2;
  15661. d1 = *s++;
  15662. if (!d1)
  15663. break;
  15664. d2 = *s++;
  15665. if (!d2)
  15666. break;
  15667. *t++ = (char)(((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0'));
  15668. l--;
  15669. }
  15670. if (n)
  15671. *n = (int)(t - p);
  15672. if (l)
  15673. *t = '\0';
  15674. return p;
  15675. }
  15676. #endif
  15677. /******************************************************************************/
  15678. #ifndef WITH_NOHTTP
  15679. #ifndef PALM_1
  15680. SOAP_FMAC1
  15681. int
  15682. SOAP_FMAC2
  15683. soap_puthttphdr(struct soap *soap, int status, size_t count)
  15684. { if (soap->status != SOAP_GET && soap->status != SOAP_DEL && soap->status != SOAP_CONNECT)
  15685. { register const char *s = "text/xml; charset=utf-8";
  15686. register int err = SOAP_OK;
  15687. #ifndef WITH_LEANER
  15688. register const char *r = NULL;
  15689. #endif
  15690. if ((status == SOAP_FILE || soap->status == SOAP_PUT || soap->status == SOAP_POST_FILE) && soap->http_content && !strchr(s, 10) && !strchr(s, 13))
  15691. s = soap->http_content;
  15692. else if (status == SOAP_HTML)
  15693. s = "text/html; charset=utf-8";
  15694. else if (count || ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK))
  15695. { if (soap->version == 2)
  15696. s = "application/soap+xml; charset=utf-8";
  15697. }
  15698. #ifndef WITH_LEANER
  15699. if (soap->mode & (SOAP_ENC_DIME | SOAP_ENC_MTOM))
  15700. { if (soap->mode & SOAP_ENC_MTOM)
  15701. { if (soap->version == 2)
  15702. r = "application/soap+xml";
  15703. else
  15704. r = "text/xml";
  15705. s = "application/xop+xml";
  15706. }
  15707. else
  15708. s = "application/dime";
  15709. }
  15710. 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)
  15711. { register const char *t;
  15712. #ifdef HAVE_SNPRINTF
  15713. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "multipart/related; charset=utf-8; boundary=\"%s\"; type=\"", soap->mime.boundary);
  15714. #else
  15715. sprintf(soap->tmpbuf, "multipart/related; charset=utf-8; boundary=\"%s\"; type=\"", soap->mime.boundary);
  15716. #endif
  15717. t = strchr(s, ';');
  15718. if (t)
  15719. strncat(soap->tmpbuf, s, t - s);
  15720. else
  15721. strcat(soap->tmpbuf, s);
  15722. if (soap->mime.start && strlen(soap->tmpbuf) + strlen(soap->mime.start) + 11 < sizeof(soap->tmpbuf))
  15723. { strcat(soap->tmpbuf, "\"; start=\"");
  15724. strcat(soap->tmpbuf, soap->mime.start);
  15725. }
  15726. strcat(soap->tmpbuf, "\"");
  15727. if (r && strlen(soap->tmpbuf) + strlen(r) + 15 < sizeof(soap->tmpbuf))
  15728. { strcat(soap->tmpbuf, "; start-info=\"");
  15729. strcat(soap->tmpbuf, r);
  15730. strcat(soap->tmpbuf, "\"");
  15731. }
  15732. }
  15733. else
  15734. strncpy(soap->tmpbuf, s, sizeof(soap->tmpbuf));
  15735. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0';
  15736. s = soap->tmpbuf;
  15737. if (status == SOAP_OK && soap->version == 2 && soap->action && strlen(soap->action) + strlen(s) < sizeof(soap->tmpbuf) - 80)
  15738. {
  15739. #ifdef HAVE_SNPRINTF
  15740. size_t l = strlen(s);
  15741. soap_snprintf(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, "; action=\"%s\"", soap->action);
  15742. #else
  15743. sprintf(soap->tmpbuf + strlen(s), "; action=\"%s\"", soap->action);
  15744. #endif
  15745. }
  15746. #endif
  15747. if ((err = soap->fposthdr(soap, "Content-Type", s)))
  15748. return err;
  15749. #ifdef WITH_ZLIB
  15750. if ((soap->omode & SOAP_ENC_ZLIB))
  15751. {
  15752. #ifdef WITH_GZIP
  15753. err = soap->fposthdr(soap, "Content-Encoding", soap->zlib_out == SOAP_ZLIB_DEFLATE ? "deflate" : "gzip");
  15754. #else
  15755. err = soap->fposthdr(soap, "Content-Encoding", "deflate");
  15756. #endif
  15757. if (err)
  15758. return err;
  15759. }
  15760. #endif
  15761. #ifndef WITH_LEANER
  15762. if ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK)
  15763. err = soap->fposthdr(soap, "Transfer-Encoding", "chunked");
  15764. else
  15765. #endif
  15766. if (s)
  15767. {
  15768. #ifdef HAVE_SNPRINTF
  15769. soap_snprintf(soap->tmpbuf, sizeof(soap->tmpbuf), "%lu", (unsigned long)count);
  15770. #else
  15771. sprintf(soap->tmpbuf, "%lu", (unsigned long)count);
  15772. #endif
  15773. err = soap->fposthdr(soap, "Content-Length", soap->tmpbuf);
  15774. }
  15775. if (err)
  15776. return err;
  15777. }
  15778. return soap->fposthdr(soap, "Connection", soap->keep_alive ? "keep-alive" : "close");
  15779. }
  15780. #endif
  15781. #endif
  15782. /******************************************************************************/
  15783. #ifndef WITH_LEAN
  15784. static const char*
  15785. soap_set_validation_fault(struct soap *soap, const char *s, const char *t)
  15786. { if (!t)
  15787. t = SOAP_STR_EOS;
  15788. if (*soap->tag)
  15789. {
  15790. #ifdef HAVE_SNPRINTF
  15791. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Validation constraint violation: %s%s in element '%s'", s, t ? t : SOAP_STR_EOS, soap->tag);
  15792. #else
  15793. if (strlen(soap->tag) + strlen(t) < sizeof(soap->msgbuf) - 100)
  15794. sprintf(soap->msgbuf, "Validation constraint violation: %s%s in element '%s'", s, t, soap->tag);
  15795. else
  15796. sprintf(soap->msgbuf, "Validation constraint violation: %s", s);
  15797. #endif
  15798. }
  15799. else
  15800. {
  15801. #ifdef HAVE_SNPRINTF
  15802. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Validation constraint violation: %s%s", s, t ? t : SOAP_STR_EOS);
  15803. #else
  15804. if (strlen(soap->tag) + strlen(t) < sizeof(soap->msgbuf) - 100)
  15805. sprintf(soap->msgbuf, "Validation constraint violation: %s%s", s, t);
  15806. else
  15807. sprintf(soap->msgbuf, "Validation constraint violation: %s", s);
  15808. #endif
  15809. }
  15810. return soap->msgbuf;
  15811. }
  15812. #endif
  15813. /******************************************************************************/
  15814. #ifndef PALM_1
  15815. SOAP_FMAC1
  15816. void
  15817. SOAP_FMAC2
  15818. soap_set_fault(struct soap *soap)
  15819. { const char **c = soap_faultcode(soap);
  15820. const char **s = soap_faultstring(soap);
  15821. if (soap->fseterror)
  15822. soap->fseterror(soap, c, s);
  15823. if (!*c)
  15824. { if (soap->version == 2)
  15825. *c = "SOAP-ENV:Sender";
  15826. else
  15827. *c = "SOAP-ENV:Client";
  15828. }
  15829. if (*s)
  15830. return;
  15831. switch (soap->error)
  15832. {
  15833. #ifndef WITH_LEAN
  15834. case SOAP_CLI_FAULT:
  15835. *s = "Client fault";
  15836. break;
  15837. case SOAP_SVR_FAULT:
  15838. *s = "Server fault";
  15839. break;
  15840. case SOAP_TAG_MISMATCH:
  15841. *s = soap_set_validation_fault(soap, "tag name or namespace mismatch", NULL);
  15842. break;
  15843. case SOAP_TYPE:
  15844. *s = soap_set_validation_fault(soap, "data type mismatch ", soap->type);
  15845. break;
  15846. case SOAP_SYNTAX_ERROR:
  15847. *s = "Well-formedness violation";
  15848. break;
  15849. case SOAP_NO_TAG:
  15850. *s = "No tag: no XML root element or missing SOAP message body element";
  15851. break;
  15852. case SOAP_MUSTUNDERSTAND:
  15853. *c = "SOAP-ENV:MustUnderstand";
  15854. #ifdef HAVE_SNPRINTF
  15855. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "The data in element '%s' must be understood but cannot be handled", soap->tag);
  15856. #else
  15857. strncpy(soap->msgbuf, soap->tag, sizeof(soap->msgbuf));
  15858. soap->msgbuf[sizeof(soap->msgbuf) - 1] = '\0';
  15859. #endif
  15860. *s = soap->msgbuf;
  15861. break;
  15862. case SOAP_VERSIONMISMATCH:
  15863. *c = "SOAP-ENV:VersionMismatch";
  15864. *s = "Invalid SOAP message or SOAP version mismatch";
  15865. break;
  15866. case SOAP_DATAENCODINGUNKNOWN:
  15867. *c = "SOAP-ENV:DataEncodingUnknown";
  15868. *s = "Unsupported SOAP data encoding";
  15869. break;
  15870. case SOAP_NAMESPACE:
  15871. *s = soap_set_validation_fault(soap, "namespace error", NULL);
  15872. break;
  15873. case SOAP_USER_ERROR:
  15874. *s = "User data error";
  15875. break;
  15876. case SOAP_FATAL_ERROR:
  15877. *s = "Fatal error";
  15878. break;
  15879. case SOAP_NO_METHOD:
  15880. #ifdef HAVE_SNPRINTF
  15881. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Method '%s' not implemented: method name or namespace not recognized", soap->tag);
  15882. #else
  15883. sprintf(soap->msgbuf, "Method '%s' not implemented: method name or namespace not recognized", soap->tag);
  15884. #endif
  15885. *s = soap->msgbuf;
  15886. break;
  15887. case SOAP_NO_DATA:
  15888. *s = "Data required for operation";
  15889. break;
  15890. case SOAP_GET_METHOD:
  15891. *s = "HTTP GET method not implemented";
  15892. break;
  15893. case SOAP_PUT_METHOD:
  15894. *s = "HTTP PUT method not implemented";
  15895. break;
  15896. case SOAP_HTTP_METHOD:
  15897. *s = "HTTP method not implemented";
  15898. break;
  15899. case SOAP_EOM:
  15900. *s = "Out of memory";
  15901. break;
  15902. case SOAP_MOE:
  15903. *s = "Memory overflow or memory corruption error";
  15904. break;
  15905. case SOAP_HDR:
  15906. *s = "Header line too long";
  15907. break;
  15908. case SOAP_IOB:
  15909. *s = "Array index out of bounds";
  15910. break;
  15911. case SOAP_NULL:
  15912. *s = soap_set_validation_fault(soap, "nil not allowed", NULL);
  15913. break;
  15914. case SOAP_DUPLICATE_ID:
  15915. *s = soap_set_validation_fault(soap, "multiple elements (use the SOAP_XML_TREE flag) with duplicate id ", soap->id);
  15916. if (soap->version == 2)
  15917. *soap_faultsubcode(soap) = "SOAP-ENC:DuplicateID";
  15918. break;
  15919. case SOAP_MISSING_ID:
  15920. *s = soap_set_validation_fault(soap, "missing id for ref ", soap->id);
  15921. if (soap->version == 2)
  15922. *soap_faultsubcode(soap) = "SOAP-ENC:MissingID";
  15923. break;
  15924. case SOAP_HREF:
  15925. *s = soap_set_validation_fault(soap, "incompatible object type id-ref ", soap->id);
  15926. break;
  15927. case SOAP_FAULT:
  15928. break;
  15929. #ifndef WITH_NOIO
  15930. case SOAP_UDP_ERROR:
  15931. *s = "Message too large for UDP packet";
  15932. break;
  15933. case SOAP_TCP_ERROR:
  15934. *s = tcp_error(soap);
  15935. break;
  15936. #endif
  15937. case SOAP_HTTP_ERROR:
  15938. *s = "An HTTP processing error occurred";
  15939. break;
  15940. case SOAP_NTLM_ERROR:
  15941. *s = "An HTTP NTLM authentication error occurred";
  15942. break;
  15943. case SOAP_SSL_ERROR:
  15944. #ifdef WITH_OPENSSL
  15945. *s = "SSL/TLS error";
  15946. #else
  15947. *s = "OpenSSL not installed: recompile with -DWITH_OPENSSL";
  15948. #endif
  15949. break;
  15950. case SOAP_PLUGIN_ERROR:
  15951. *s = "Plugin registry error";
  15952. break;
  15953. case SOAP_DIME_ERROR:
  15954. *s = "DIME format error or max DIME size exceeds SOAP_MAXDIMESIZE";
  15955. break;
  15956. case SOAP_DIME_HREF:
  15957. *s = "DIME href to missing attachment";
  15958. break;
  15959. case SOAP_DIME_MISMATCH:
  15960. *s = "DIME version/transmission error";
  15961. break;
  15962. case SOAP_DIME_END:
  15963. *s = "End of DIME error";
  15964. break;
  15965. case SOAP_MIME_ERROR:
  15966. *s = "MIME format error";
  15967. break;
  15968. case SOAP_MIME_HREF:
  15969. *s = "MIME href to missing attachment";
  15970. break;
  15971. case SOAP_MIME_END:
  15972. *s = "End of MIME error";
  15973. break;
  15974. case SOAP_ZLIB_ERROR:
  15975. #ifdef WITH_ZLIB
  15976. #ifdef HAVE_SNPRINTF
  15977. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Zlib/gzip error: '%s'", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS);
  15978. #else
  15979. sprintf(soap->msgbuf, "Zlib/gzip error: '%s'", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS);
  15980. #endif
  15981. *s = soap->msgbuf;
  15982. #else
  15983. *s = "Zlib/gzip not installed for (de)compression: recompile with -DWITH_GZIP";
  15984. #endif
  15985. break;
  15986. case SOAP_REQUIRED:
  15987. *s = soap_set_validation_fault(soap, "missing required attribute", NULL);
  15988. break;
  15989. case SOAP_PROHIBITED:
  15990. *s = soap_set_validation_fault(soap, "prohibited attribute present", NULL);
  15991. break;
  15992. case SOAP_OCCURS:
  15993. *s = soap_set_validation_fault(soap, "occurrence violation", NULL);
  15994. break;
  15995. case SOAP_LENGTH:
  15996. *s = soap_set_validation_fault(soap, "content range or length violation", NULL);
  15997. break;
  15998. case SOAP_FD_EXCEEDED:
  15999. *s = "Maximum number of open connections was reached (no define HAVE_POLL): increase FD_SETSIZE";
  16000. break;
  16001. case SOAP_UTF_ERROR:
  16002. *s = "UTF content encoding error";
  16003. break;
  16004. case SOAP_STOP:
  16005. *s = "Stopped: no response sent or received (informative)";
  16006. break;
  16007. #endif
  16008. case SOAP_EOF:
  16009. #ifndef WITH_NOIO
  16010. *s = soap_strerror(soap); /* *s = soap->msgbuf */
  16011. #ifndef WITH_LEAN
  16012. if (strlen(soap->msgbuf) + 25 < sizeof(soap->msgbuf))
  16013. { memmove(soap->msgbuf + 25, soap->msgbuf, strlen(soap->msgbuf) + 1);
  16014. memcpy(soap->msgbuf, "End of file or no input: ", 25);
  16015. }
  16016. #endif
  16017. break;
  16018. #else
  16019. *s = "End of file or no input";
  16020. break;
  16021. #endif
  16022. default:
  16023. #ifndef WITH_NOHTTP
  16024. #ifndef WITH_LEAN
  16025. if (soap->error > 200 && soap->error < 600)
  16026. {
  16027. #ifdef HAVE_SNPRINTF
  16028. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "HTTP Error: %d %s", soap->error, http_error(soap, soap->error));
  16029. #else
  16030. sprintf(soap->msgbuf, "HTTP Error: %d %s", soap->error, http_error(soap, soap->error));
  16031. #endif
  16032. *s = soap->msgbuf;
  16033. }
  16034. else
  16035. #endif
  16036. #endif
  16037. {
  16038. #ifdef HAVE_SNPRINTF
  16039. soap_snprintf(soap->msgbuf, sizeof(soap->msgbuf), "Error %d", soap->error);
  16040. #else
  16041. sprintf(soap->msgbuf, "Error %d", soap->error);
  16042. #endif
  16043. *s = soap->msgbuf;
  16044. }
  16045. }
  16046. }
  16047. #endif
  16048. /******************************************************************************/
  16049. #ifndef PALM_1
  16050. SOAP_FMAC1
  16051. int
  16052. SOAP_FMAC2
  16053. soap_send_fault(struct soap *soap)
  16054. { register int status = soap->error;
  16055. if (status == SOAP_OK || status == SOAP_STOP)
  16056. return soap_closesock(soap);
  16057. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Sending back fault struct for error code %d\n", soap->error));
  16058. soap->keep_alive = 0; /* to terminate connection */
  16059. soap_set_fault(soap);
  16060. if (soap->error < 200 && soap->error != SOAP_FAULT)
  16061. soap->header = NULL;
  16062. if (status != SOAP_EOF || (!soap->recv_timeout && !soap->send_timeout))
  16063. { register int r = 1;
  16064. #ifndef WITH_NOIO
  16065. if (soap->fpoll && soap->fpoll(soap))
  16066. r = 0;
  16067. #ifndef WITH_LEAN
  16068. else if (soap_valid_socket(soap->socket))
  16069. { r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_SND, 0);
  16070. if (r > 0)
  16071. { int t;
  16072. if (!(r & SOAP_TCP_SELECT_SND)
  16073. || ((r & SOAP_TCP_SELECT_RCV)
  16074. && recv(soap->socket, (char*)&t, 1, MSG_PEEK) < 0))
  16075. r = 0;
  16076. }
  16077. }
  16078. #endif
  16079. #endif
  16080. if (r > 0)
  16081. { soap->error = SOAP_OK;
  16082. soap->encodingStyle = NULL; /* no encodingStyle in Faults */
  16083. soap_serializeheader(soap);
  16084. soap_serializefault(soap);
  16085. soap_begin_count(soap);
  16086. if (soap->mode & SOAP_IO_LENGTH)
  16087. { soap_envelope_begin_out(soap);
  16088. soap_putheader(soap);
  16089. soap_body_begin_out(soap);
  16090. soap_putfault(soap);
  16091. soap_body_end_out(soap);
  16092. soap_envelope_end_out(soap);
  16093. }
  16094. soap_end_count(soap);
  16095. if (soap_response(soap, status)
  16096. || soap_envelope_begin_out(soap)
  16097. || soap_putheader(soap)
  16098. || soap_body_begin_out(soap)
  16099. || soap_putfault(soap)
  16100. || soap_body_end_out(soap)
  16101. || soap_envelope_end_out(soap))
  16102. return soap_closesock(soap);
  16103. soap_end_send(soap);
  16104. }
  16105. }
  16106. soap->error = status;
  16107. return soap_closesock(soap);
  16108. }
  16109. #endif
  16110. /******************************************************************************/
  16111. #ifndef PALM_1
  16112. SOAP_FMAC1
  16113. int
  16114. SOAP_FMAC2
  16115. soap_recv_fault(struct soap *soap, int check)
  16116. { register int status = soap->error;
  16117. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Check if receiving SOAP Fault\n"));
  16118. if (!check)
  16119. { /* try getfault when no tag or tag mismatched at level 2, otherwise ret */
  16120. if (soap->error != SOAP_NO_TAG
  16121. && (soap->error != SOAP_TAG_MISMATCH || soap->level != 2))
  16122. return soap->error;
  16123. }
  16124. else if (soap->version == 0) /* check == 1 but no SOAP: do not parse SOAP Fault */
  16125. return SOAP_OK;
  16126. soap->error = SOAP_OK;
  16127. if (soap_getfault(soap))
  16128. { /* check flag set: check if SOAP Fault is present, if not just return */
  16129. if (check && soap->error == SOAP_TAG_MISMATCH && soap->level == 2)
  16130. return soap->error = SOAP_OK;
  16131. DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Error: soap_get_soapfault() failed at level %u tag '%s'\n", soap->level, soap->tag));
  16132. *soap_faultcode(soap) = (soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client");
  16133. soap->error = status;
  16134. soap_set_fault(soap);
  16135. }
  16136. else
  16137. { register const char *s = *soap_faultcode(soap);
  16138. if (!soap_match_tag(soap, s, "SOAP-ENV:Server") || !soap_match_tag(soap, s, "SOAP-ENV:Receiver"))
  16139. status = SOAP_SVR_FAULT;
  16140. else if (!soap_match_tag(soap, s, "SOAP-ENV:Client") || !soap_match_tag(soap, s, "SOAP-ENV:Sender"))
  16141. status = SOAP_CLI_FAULT;
  16142. else if (!soap_match_tag(soap, s, "SOAP-ENV:MustUnderstand"))
  16143. status = SOAP_MUSTUNDERSTAND;
  16144. else if (!soap_match_tag(soap, s, "SOAP-ENV:VersionMismatch"))
  16145. status = SOAP_VERSIONMISMATCH;
  16146. else
  16147. { DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Received SOAP Fault code %s\n", s));
  16148. status = SOAP_FAULT;
  16149. }
  16150. if (!soap_body_end_in(soap))
  16151. soap_envelope_end_in(soap);
  16152. }
  16153. soap_end_recv(soap);
  16154. soap->error = status;
  16155. return soap_closesock(soap);
  16156. }
  16157. #endif
  16158. /******************************************************************************/
  16159. #ifndef WITH_NOHTTP
  16160. #ifndef PALM_1
  16161. SOAP_FMAC1
  16162. int
  16163. SOAP_FMAC2
  16164. soap_send_empty_response(struct soap *soap, int httpstatuscode)
  16165. { register soap_mode m = soap->omode;
  16166. if (!(m & SOAP_IO_UDP))
  16167. { soap->count = 0;
  16168. if ((m & SOAP_IO) == SOAP_IO_CHUNK)
  16169. soap->omode = (m & ~SOAP_IO) | SOAP_IO_BUFFER;
  16170. soap_response(soap, httpstatuscode);
  16171. soap_end_send(soap); /* force end of sends */
  16172. soap->error = SOAP_STOP; /* stops the server (from returning a response) */
  16173. soap->omode = m;
  16174. }
  16175. return soap_closesock(soap);
  16176. }
  16177. #endif
  16178. #endif
  16179. /******************************************************************************/
  16180. #ifndef WITH_NOHTTP
  16181. #ifndef PALM_1
  16182. SOAP_FMAC1
  16183. int
  16184. SOAP_FMAC2
  16185. soap_recv_empty_response(struct soap *soap)
  16186. { if (!(soap->omode & SOAP_IO_UDP))
  16187. { if (!soap_begin_recv(soap))
  16188. {
  16189. #ifndef WITH_LEAN
  16190. if (soap->body)
  16191. soap_get_http_body(soap, NULL); /* read (empty?) HTTP body and discard */
  16192. #endif
  16193. soap_end_recv(soap);
  16194. }
  16195. else if (soap->error == SOAP_NO_DATA || soap->error == 202)
  16196. soap->error = SOAP_OK;
  16197. }
  16198. return soap_closesock(soap);
  16199. }
  16200. #endif
  16201. #endif
  16202. /******************************************************************************/
  16203. #ifndef WITH_NOIO
  16204. #ifndef PALM_1
  16205. static const char*
  16206. soap_strerror(struct soap *soap)
  16207. { register int err = soap->errnum;
  16208. *soap->msgbuf = '\0';
  16209. if (err)
  16210. {
  16211. #ifndef WIN32
  16212. # ifdef HAVE_STRERROR_R
  16213. # ifdef _GNU_SOURCE
  16214. return strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* GNU-specific */
  16215. # else
  16216. strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* XSI-compliant */
  16217. # endif
  16218. # else
  16219. return strerror(err);
  16220. # endif
  16221. #else
  16222. #ifndef UNDER_CE
  16223. DWORD len;
  16224. *soap->msgbuf = '\0';
  16225. 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);
  16226. #else
  16227. DWORD i, len;
  16228. *soap->msgbuf = '\0';
  16229. len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, (LPTSTR)soap->msgbuf, (DWORD)(sizeof(soap->msgbuf)/sizeof(TCHAR)), NULL);
  16230. for (i = 0; i <= len; i++)
  16231. { if (((TCHAR*)soap->msgbuf)[i] < 0x80)
  16232. soap->msgbuf[i] = (char)((TCHAR*)soap->msgbuf)[i];
  16233. else
  16234. soap->msgbuf[i] = '?';
  16235. }
  16236. #endif
  16237. #endif
  16238. }
  16239. else
  16240. { char *s = soap->msgbuf;
  16241. #ifndef WITH_LEAN
  16242. int rt = soap->recv_timeout, st = soap->send_timeout;
  16243. int ru = ' ', su = ' ';
  16244. #endif
  16245. strcpy(s, "Operation interrupted or timed out");
  16246. #ifndef WITH_LEAN
  16247. if (rt < 0)
  16248. { rt = -rt;
  16249. ru = 'u';
  16250. }
  16251. if (st < 0)
  16252. { st = -st;
  16253. su = 'u';
  16254. }
  16255. if (rt)
  16256. {
  16257. #ifdef HAVE_SNPRINTF
  16258. size_t l = strlen(s);
  16259. soap_snprintf(s + l, sizeof(soap->msgbuf) - l, " (%d%cs recv delay)", rt, ru);
  16260. #else
  16261. sprintf(s + strlen(s), " (%d%cs recv delay)", rt, ru);
  16262. #endif
  16263. }
  16264. if (st)
  16265. {
  16266. #ifdef HAVE_SNPRINTF
  16267. size_t l = strlen(s);
  16268. soap_snprintf(s + l, sizeof(soap->msgbuf) - l, " (%d%cs send delay)", st, su);
  16269. #else
  16270. sprintf(s + strlen(s), " (%d%cs send delay)", st, su);
  16271. #endif
  16272. }
  16273. #endif
  16274. }
  16275. return soap->msgbuf;
  16276. }
  16277. #endif
  16278. #endif
  16279. /******************************************************************************/
  16280. #ifndef PALM_2
  16281. static int
  16282. soap_set_error(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML, int soaperror)
  16283. { *soap_faultcode(soap) = faultcode;
  16284. if (faultsubcodeQName)
  16285. *soap_faultsubcode(soap) = faultsubcodeQName;
  16286. *soap_faultstring(soap) = faultstring;
  16287. if (faultdetailXML && *faultdetailXML)
  16288. { register const char **s = soap_faultdetail(soap);
  16289. if (s)
  16290. *s = faultdetailXML;
  16291. }
  16292. return soap->error = soaperror;
  16293. }
  16294. #endif
  16295. /******************************************************************************/
  16296. #ifndef PALM_2
  16297. SOAP_FMAC1
  16298. int
  16299. SOAP_FMAC2
  16300. soap_set_sender_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror)
  16301. { return soap_set_error(soap, soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client", NULL, faultstring, faultdetailXML, soaperror);
  16302. }
  16303. #endif
  16304. /******************************************************************************/
  16305. #ifndef PALM_2
  16306. SOAP_FMAC1
  16307. int
  16308. SOAP_FMAC2
  16309. soap_set_receiver_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror)
  16310. { return soap_set_error(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : "SOAP-ENV:Server", NULL, faultstring, faultdetailXML, soaperror);
  16311. }
  16312. #endif
  16313. /******************************************************************************/
  16314. #ifndef PALM_2
  16315. static int
  16316. soap_copy_fault(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML)
  16317. { char *r = NULL, *s = NULL, *t = NULL;
  16318. if (faultsubcodeQName)
  16319. r = soap_strdup(soap, faultsubcodeQName);
  16320. if (faultstring)
  16321. s = soap_strdup(soap, faultstring);
  16322. if (faultdetailXML)
  16323. t = soap_strdup(soap, faultdetailXML);
  16324. return soap_set_error(soap, faultcode, r, s, t, SOAP_FAULT);
  16325. }
  16326. #endif
  16327. /******************************************************************************/
  16328. #ifndef PALM_2
  16329. SOAP_FMAC1
  16330. int
  16331. SOAP_FMAC2
  16332. soap_sender_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML)
  16333. { return soap_sender_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  16334. }
  16335. #endif
  16336. /******************************************************************************/
  16337. #ifndef PALM_2
  16338. SOAP_FMAC1
  16339. int
  16340. SOAP_FMAC2
  16341. soap_sender_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML)
  16342. { return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client", faultsubcodeQName, faultstring, faultdetailXML);
  16343. }
  16344. #endif
  16345. /******************************************************************************/
  16346. #ifndef PALM_2
  16347. SOAP_FMAC1
  16348. int
  16349. SOAP_FMAC2
  16350. soap_receiver_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML)
  16351. { return soap_receiver_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  16352. }
  16353. #endif
  16354. /******************************************************************************/
  16355. #ifndef PALM_2
  16356. SOAP_FMAC1
  16357. int
  16358. SOAP_FMAC2
  16359. soap_receiver_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring, const char *faultdetailXML)
  16360. { return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : "SOAP-ENV:Server", faultsubcodeQName, faultstring, faultdetailXML);
  16361. }
  16362. #endif
  16363. /******************************************************************************/
  16364. #ifndef PALM_2
  16365. #ifndef WITH_NOSTDLIB
  16366. SOAP_FMAC1
  16367. void
  16368. SOAP_FMAC2
  16369. soap_print_fault(struct soap *soap, FILE *fd)
  16370. { if (soap_check_state(soap))
  16371. fprintf(fd, "Error: soap struct state not initialized\n");
  16372. else if (soap->error)
  16373. { const char **c, *v = NULL, *s, *d;
  16374. c = soap_faultcode(soap);
  16375. if (!*c)
  16376. soap_set_fault(soap);
  16377. if (soap->version == 2)
  16378. v = soap_check_faultsubcode(soap);
  16379. s = *soap_faultstring(soap);
  16380. d = soap_check_faultdetail(soap);
  16381. 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]");
  16382. }
  16383. }
  16384. #endif
  16385. #endif
  16386. /******************************************************************************/
  16387. #ifdef __cplusplus
  16388. #ifndef WITH_LEAN
  16389. #ifndef WITH_NOSTDLIB
  16390. #ifndef WITH_COMPAT
  16391. SOAP_FMAC1
  16392. void
  16393. SOAP_FMAC2
  16394. soap_stream_fault(struct soap *soap, std::ostream& os)
  16395. { if (soap_check_state(soap))
  16396. os << "Error: soap struct state not initialized\n";
  16397. else if (soap->error)
  16398. { const char **c, *v = NULL, *s, *d;
  16399. c = soap_faultcode(soap);
  16400. if (!*c)
  16401. soap_set_fault(soap);
  16402. if (soap->version == 2)
  16403. v = soap_check_faultsubcode(soap);
  16404. s = *soap_faultstring(soap);
  16405. d = soap_check_faultdetail(soap);
  16406. os << (soap->version ? "SOAP 1." : "Error ")
  16407. << (soap->version ? (int)soap->version : soap->error)
  16408. << " fault: " << *c
  16409. << "[" << (v ? v : "no subcode") << "]"
  16410. << std::endl
  16411. << "\"" << (s ? s : "[no reason]") << "\""
  16412. << std::endl
  16413. << "Detail: " << (d ? d : "[no detail]")
  16414. << std::endl;
  16415. }
  16416. }
  16417. #endif
  16418. #endif
  16419. #endif
  16420. #endif
  16421. /******************************************************************************/
  16422. #ifndef WITH_LEAN
  16423. #ifndef WITH_NOSTDLIB
  16424. SOAP_FMAC1
  16425. char*
  16426. SOAP_FMAC2
  16427. soap_sprint_fault(struct soap *soap, char *buf, size_t len)
  16428. { if (soap_check_state(soap))
  16429. { strncpy(buf, "Error: soap struct not initialized", len);
  16430. buf[len - 1] = '\0';
  16431. }
  16432. else if (soap->error)
  16433. { const char **c, *v = NULL, *s, *d;
  16434. c = soap_faultcode(soap);
  16435. if (!*c)
  16436. soap_set_fault(soap);
  16437. if (soap->version == 2)
  16438. v = *soap_faultsubcode(soap);
  16439. s = *soap_faultstring(soap);
  16440. d = soap_check_faultdetail(soap);
  16441. #ifdef HAVE_SNPRINTF
  16442. 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]");
  16443. #else
  16444. if (len > 40 + (v ? strlen(v) : 0) + (s ? strlen(s) : 0) + (d ? strlen(d) : 0))
  16445. 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]");
  16446. else if (len > 40)
  16447. sprintf(buf, "%s%d fault: %s\n", soap->version ? "SOAP 1." : "Error ", soap->version ? (int)soap->version : soap->error, *c);
  16448. else
  16449. buf[0] = '\0';
  16450. #endif
  16451. }
  16452. return buf;
  16453. }
  16454. #endif
  16455. #endif
  16456. /******************************************************************************/
  16457. #ifndef PALM_1
  16458. #ifndef WITH_NOSTDLIB
  16459. SOAP_FMAC1
  16460. void
  16461. SOAP_FMAC2
  16462. soap_print_fault_location(struct soap *soap, FILE *fd)
  16463. {
  16464. #ifndef WITH_LEAN
  16465. int i, j, c1, c2;
  16466. if (soap->error && soap->error != SOAP_STOP && soap->bufidx <= soap->buflen && soap->buflen > 0 && soap->buflen <= SOAP_BUFLEN)
  16467. { i = (int)soap->bufidx - 1;
  16468. if (i <= 0)
  16469. i = 0;
  16470. c1 = soap->buf[i];
  16471. soap->buf[i] = '\0';
  16472. if ((int)soap->buflen >= i + 1024)
  16473. j = i + 1023;
  16474. else
  16475. j = (int)soap->buflen - 1;
  16476. c2 = soap->buf[j];
  16477. soap->buf[j] = '\0';
  16478. fprintf(fd, "%s%c\n<!-- ** HERE ** -->\n", soap->buf, c1);
  16479. if (soap->bufidx < soap->buflen)
  16480. fprintf(fd, "%s\n", soap->buf + soap->bufidx);
  16481. soap->buf[i] = (char)c1;
  16482. soap->buf[j] = (char)c2;
  16483. }
  16484. #endif
  16485. }
  16486. #endif
  16487. #endif
  16488. /******************************************************************************/
  16489. #ifndef PALM_1
  16490. SOAP_FMAC1
  16491. int
  16492. SOAP_FMAC2
  16493. soap_register_plugin_arg(struct soap *soap, int (*fcreate)(struct soap*, struct soap_plugin*, void*), void *arg)
  16494. { register struct soap_plugin *p;
  16495. register int r;
  16496. if (!(p = (struct soap_plugin*)SOAP_MALLOC(soap, sizeof(struct soap_plugin))))
  16497. return soap->error = SOAP_EOM;
  16498. p->id = NULL;
  16499. p->data = NULL;
  16500. p->fcopy = NULL;
  16501. p->fdelete = NULL;
  16502. r = fcreate(soap, p, arg);
  16503. if (!r && p->fdelete)
  16504. { p->next = soap->plugins;
  16505. soap->plugins = p;
  16506. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Registered '%s' plugin\n", p->id));
  16507. return SOAP_OK;
  16508. }
  16509. 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));
  16510. SOAP_FREE(soap, p);
  16511. return r;
  16512. }
  16513. #endif
  16514. /******************************************************************************/
  16515. #ifndef PALM_1
  16516. static void *
  16517. fplugin(struct soap *soap, const char *id)
  16518. { register struct soap_plugin *p;
  16519. for (p = soap->plugins; p; p = p->next)
  16520. if (p->id == id || !strcmp(p->id, id))
  16521. return p->data;
  16522. return NULL;
  16523. }
  16524. #endif
  16525. /******************************************************************************/
  16526. #ifndef PALM_2
  16527. SOAP_FMAC1
  16528. void *
  16529. SOAP_FMAC2
  16530. soap_lookup_plugin(struct soap *soap, const char *id)
  16531. { return soap->fplugin(soap, id);
  16532. }
  16533. #endif
  16534. /******************************************************************************/
  16535. #ifdef __cplusplus
  16536. }
  16537. #endif
  16538. /******************************************************************************\
  16539. *
  16540. * C++ soap struct methods
  16541. *
  16542. \******************************************************************************/
  16543. #ifdef __cplusplus
  16544. soap::soap()
  16545. { soap_init(this);
  16546. }
  16547. #endif
  16548. /******************************************************************************/
  16549. #ifdef __cplusplus
  16550. soap::soap(soap_mode m)
  16551. { soap_init1(this, m);
  16552. }
  16553. #endif
  16554. /******************************************************************************/
  16555. #ifdef __cplusplus
  16556. soap::soap(soap_mode im, soap_mode om)
  16557. { soap_init2(this, im, om);
  16558. }
  16559. #endif
  16560. /******************************************************************************/
  16561. #ifdef __cplusplus
  16562. soap::soap(const struct soap& soap)
  16563. { soap_copy_context(this, &soap);
  16564. }
  16565. #endif
  16566. /******************************************************************************/
  16567. #ifdef __cplusplus
  16568. soap::~soap()
  16569. { soap_destroy(this);
  16570. soap_end(this);
  16571. soap_done(this);
  16572. }
  16573. #endif
  16574. /******************************************************************************/