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.

1783 lines
42 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. Copyright 2006 - 2011 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef MICROSTACK_NO_STDAFX
  14. #include "stdafx.h"
  15. #endif
  16. #include <windows.h>
  17. #include <math.h>
  18. #include <winioctl.h>
  19. #include <winbase.h>
  20. #include <winerror.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <malloc.h>
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #include <time.h>
  29. #include "ILibParsers.h"
  30. #define DEBUGSTATEMENT(x)
  31. #define UPNP_MAX_WAIT 2000000000
  32. #define sem_t HANDLE
  33. #define sem_init(x,y,z) *x=CreateSemaphore(NULL,z,FD_SETSIZE,NULL)
  34. #define sem_destroy(x) (CloseHandle(*x)==0?1:0)
  35. #define sem_wait(x) WaitForSingleObject(*x,INFINITE)
  36. #define sem_trywait(x) ((WaitForSingleObject(*x,0)==WAIT_OBJECT_0)?0:1)
  37. #define sem_post(x) ReleaseSemaphore(*x,1,NULL)
  38. extern int errno;
  39. static sem_t ILibChainLock = NULL;
  40. static int ILibChainLock_RefCounter = 0;
  41. static int malloc_counter = 0;
  42. void* dbg_malloc(int sz)
  43. {
  44. ++malloc_counter;
  45. return((void*)malloc(sz));
  46. }
  47. void dbg_free(void* ptr)
  48. {
  49. --malloc_counter;
  50. free(ptr);
  51. }
  52. int dbg_GetCount()
  53. {
  54. return(malloc_counter);
  55. }
  56. struct ILibStackNode
  57. {
  58. void *Data;
  59. struct ILibStackNode *Next;
  60. };
  61. struct HashNode
  62. {
  63. struct HashNode *Next;
  64. struct HashNode *Prev;
  65. int KeyHash;
  66. char *KeyValue;
  67. int KeyLength;
  68. void *Data;
  69. };
  70. #define strncasecmp(x,y,z) _strnicmp(x,y,z)
  71. #define gettimeofday(x,y) (x)->tv_sec = GetTickCount()/1000
  72. int ILibGetLocalIPAddressList(int** pp_int)
  73. {
  74. int i;
  75. char buffer[64];
  76. DWORD bufferSize;
  77. SOCKET TempSocket = socket(AF_INET,SOCK_DGRAM,0);
  78. WSAIoctl(TempSocket,SIO_ADDRESS_LIST_QUERY,NULL,0,buffer,64,&bufferSize,NULL,NULL);
  79. *pp_int = (int*)MALLOC(sizeof(int)*((SOCKET_ADDRESS_LIST*)buffer)->iAddressCount);
  80. for (i = 0;i < ((SOCKET_ADDRESS_LIST*)buffer)->iAddressCount;++i)
  81. {
  82. (*pp_int)[i] = ((struct sockaddr_in*)(((SOCKET_ADDRESS_LIST*)buffer)->Address[i].lpSockaddr))->sin_addr.s_addr;
  83. }
  84. closesocket(TempSocket);
  85. return(((SOCKET_ADDRESS_LIST*)buffer)->iAddressCount);
  86. }
  87. struct ILibChain
  88. {
  89. void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime);
  90. void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset);
  91. void (*Destroy)(void* object);
  92. };
  93. struct LifeTimeMonitorData
  94. {
  95. long ExpirationTick;
  96. void *data;
  97. void (*CallbackPtr)(void *data);
  98. void (*DestroyPtr)(void *data);
  99. struct LifeTimeMonitorData *Next;
  100. };
  101. struct ILibLifeTime
  102. {
  103. void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime);
  104. void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset);
  105. void (*Destroy)(void* object);
  106. struct LifeTimeMonitorData *LM;
  107. void *Chain;
  108. sem_t SyncLock;
  109. };
  110. struct ILibBaseChain
  111. {
  112. SOCKET Terminate;
  113. int TerminateFlag;
  114. void *Object;
  115. void *Next;
  116. };
  117. void *ILibCreateChain()
  118. {
  119. struct ILibBaseChain *RetVal = (struct ILibBaseChain*)MALLOC(sizeof(struct ILibBaseChain));
  120. WORD wVersionRequested;
  121. WSADATA wsaData;
  122. wVersionRequested = MAKEWORD( 1, 1 );
  123. if (WSAStartup( wVersionRequested, &wsaData ) != 0) {exit(1);}
  124. RetVal->Object = NULL;
  125. RetVal->Next = NULL;
  126. RetVal->Terminate = socket(AF_INET, SOCK_DGRAM, 0);
  127. RetVal->TerminateFlag = 0;
  128. if(ILibChainLock_RefCounter==0)
  129. {
  130. sem_init(&ILibChainLock,0,1);
  131. }
  132. return(RetVal);
  133. }
  134. void ILibAddToChain(void *Chain, void *object)
  135. {
  136. struct ILibBaseChain *chain = (struct ILibBaseChain*)Chain;
  137. while(chain->Next!=NULL)
  138. {
  139. chain = chain->Next;
  140. }
  141. if(chain->Object!=NULL)
  142. {
  143. chain->Next = (struct ILibBaseChain*)MALLOC(sizeof(struct ILibBaseChain));
  144. chain = chain->Next;
  145. }
  146. chain->Object = object;
  147. chain->Next = NULL;
  148. }
  149. void ILibForceUnBlockChain(void *Chain)
  150. {
  151. struct ILibBaseChain *c = (struct ILibBaseChain*)Chain;
  152. SOCKET temp;
  153. sem_wait(&ILibChainLock);
  154. temp = c->Terminate;
  155. c->Terminate = ~0;
  156. closesocket(temp);
  157. sem_post(&ILibChainLock);
  158. }
  159. void ILibStartChain(void *Chain)
  160. {
  161. struct ILibBaseChain *c = (struct ILibBaseChain*)Chain;
  162. struct ILibBaseChain *temp;
  163. fd_set readset;
  164. fd_set errorset;
  165. fd_set writeset;
  166. struct timeval tv;
  167. int slct;
  168. srand((unsigned int)time(NULL));
  169. FD_ZERO(&readset);
  170. FD_ZERO(&errorset);
  171. FD_ZERO(&writeset);
  172. while(((struct ILibBaseChain*)Chain)->TerminateFlag==0)
  173. {
  174. slct = 0;
  175. FD_ZERO(&readset);
  176. FD_ZERO(&errorset);
  177. FD_ZERO(&writeset);
  178. tv.tv_sec = UPNP_MAX_WAIT;
  179. tv.tv_usec = 0;
  180. sem_wait(&ILibChainLock);
  181. if(((struct ILibBaseChain*)Chain)->Terminate==~0)
  182. {
  183. slct = -1;
  184. }
  185. else
  186. {
  187. FD_SET(((struct ILibBaseChain*)Chain)->Terminate,&errorset);
  188. }
  189. sem_post(&ILibChainLock);
  190. c = (struct ILibBaseChain*)Chain;
  191. while(c!=NULL && c->Object!=NULL)
  192. {
  193. if(((struct ILibChain*)c->Object)->PreSelect!=NULL)
  194. {
  195. ((struct ILibChain*)c->Object)->PreSelect(c->Object,&readset,&writeset,&errorset,(int*)&tv.tv_sec);
  196. }
  197. c = c->Next;
  198. }
  199. if(slct!=0)
  200. {
  201. tv.tv_sec = 0;
  202. }
  203. slct = select(FD_SETSIZE,&readset,&writeset,&errorset,&tv);
  204. if(slct==-1)
  205. {
  206. FD_ZERO(&readset);
  207. FD_ZERO(&writeset);
  208. FD_ZERO(&errorset);
  209. }
  210. if(((struct ILibBaseChain*)Chain)->Terminate==~0)
  211. {
  212. ((struct ILibBaseChain*)Chain)->Terminate = socket(AF_INET,SOCK_DGRAM,0);
  213. }
  214. c = (struct ILibBaseChain*)Chain;
  215. while(c!=NULL && c->Object!=NULL)
  216. {
  217. if(((struct ILibChain*)c->Object)->PostSelect!=NULL)
  218. {
  219. ((struct ILibChain*)c->Object)->PostSelect(c->Object,slct,&readset,&writeset,&errorset);
  220. }
  221. c = c->Next;
  222. }
  223. }
  224. c = (struct ILibBaseChain*)Chain;
  225. while(c!=NULL && c->Object!=NULL)
  226. {
  227. if(((struct ILibChain*)c->Object)->Destroy!=NULL)
  228. {
  229. ((struct ILibChain*)c->Object)->Destroy(c->Object);
  230. }
  231. FREE(c->Object);
  232. c = c->Next;
  233. }
  234. c = (struct ILibBaseChain*)Chain;
  235. while(c!=NULL)
  236. {
  237. temp = c->Next;
  238. FREE(c);
  239. c = temp;
  240. }
  241. WSACleanup();
  242. if(ILibChainLock_RefCounter==1)
  243. {
  244. sem_destroy(&ILibChainLock);
  245. }
  246. --ILibChainLock_RefCounter;
  247. }
  248. void ILibStopChain(void *Chain)
  249. {
  250. ((struct ILibBaseChain*)Chain)->TerminateFlag = 1;
  251. ILibForceUnBlockChain(Chain);
  252. }
  253. void ILibDestructXMLNodeList(struct ILibXMLNode *node)
  254. {
  255. struct ILibXMLNode *temp;
  256. while(node!=NULL)
  257. {
  258. temp = node->Next;
  259. FREE(node);
  260. node = temp;
  261. }
  262. }
  263. void ILibDestructXMLAttributeList(struct ILibXMLAttribute *attribute)
  264. {
  265. struct ILibXMLAttribute *temp;
  266. while(attribute!=NULL)
  267. {
  268. temp = attribute->Next;
  269. FREE(attribute);
  270. attribute = temp;
  271. }
  272. }
  273. int ILibProcessXMLNodeList(struct ILibXMLNode *nodeList)
  274. {
  275. int RetVal = 0;
  276. struct ILibXMLNode *current = nodeList;
  277. struct ILibXMLNode *temp;
  278. void *TagStack;
  279. ILibCreateStack(&TagStack);
  280. while(current!=NULL)
  281. {
  282. if(current->StartTag!=0)
  283. {
  284. // Start Tag
  285. current->Parent = ILibPeekStack(&TagStack);
  286. ILibPushStack(&TagStack,current);
  287. }
  288. else
  289. {
  290. // Close Tag
  291. temp = (struct ILibXMLNode*)ILibPopStack(&TagStack);
  292. if(temp!=NULL)
  293. {
  294. if(temp->NameLength==current->NameLength && memcmp(temp->Name,current->Name,current->NameLength)==0)
  295. {
  296. if(current->Next!=NULL)
  297. {
  298. if(current->Next->StartTag!=0)
  299. {
  300. temp->Peer = current->Next;
  301. }
  302. }
  303. temp->ClosingTag = current;
  304. }
  305. else
  306. {
  307. // Illegal Close Tag Order
  308. RetVal = -2;
  309. break;
  310. }
  311. }
  312. else
  313. {
  314. // Illegal Close Tag
  315. RetVal = -1;
  316. break;
  317. }
  318. }
  319. current = current->Next;
  320. }
  321. if(TagStack!=NULL)
  322. {
  323. // Incomplete XML
  324. RetVal = -3;
  325. }
  326. return(RetVal);
  327. }
  328. int ILibReadInnerXML(struct ILibXMLNode *node, char **RetVal)
  329. {
  330. struct ILibXMLNode *x = node;
  331. int length = 0;
  332. void *TagStack;
  333. ILibCreateStack(&TagStack);
  334. do
  335. {
  336. if(x->StartTag!=0) {ILibPushStack(&TagStack,x);}
  337. x = x->Next;
  338. }while(!(x->StartTag==0 && ILibPopStack(&TagStack)==node && x->NameLength==node->NameLength && memcmp(x->Name,node->Name,node->NameLength)==0));
  339. length = (int)((char*)x->Reserved - (char*)node->Reserved - 1);
  340. if(length<0) {length=0;}
  341. *RetVal = (char*)node->Reserved;
  342. return(length);
  343. }
  344. struct ILibXMLAttribute *ILibGetXMLAttributes(struct ILibXMLNode *node)
  345. {
  346. struct ILibXMLAttribute *RetVal = NULL;
  347. struct ILibXMLAttribute *current = NULL;
  348. char *c;
  349. int EndReserved = (node->EmptyTag==0)?1:2;
  350. struct parser_result *xml;
  351. struct parser_result_field *field;
  352. struct parser_result *temp2;
  353. struct parser_result *temp3;
  354. c = (char*)node->Reserved - 1;
  355. while(*c!='<')
  356. {
  357. c = c -1;
  358. }
  359. c = c +1;
  360. xml = ILibParseStringAdv(c,0,(int)((char*)node->Reserved - c -EndReserved)," ",1);
  361. field = xml->FirstResult;
  362. if(field!=NULL) {field = field->NextResult;}
  363. while(field!=NULL)
  364. {
  365. if(RetVal==NULL)
  366. {
  367. RetVal = (struct ILibXMLAttribute*)MALLOC(sizeof(struct ILibXMLAttribute));
  368. RetVal->Next = NULL;
  369. }
  370. else
  371. {
  372. current = (struct ILibXMLAttribute*)MALLOC(sizeof(struct ILibXMLAttribute));
  373. current->Next = RetVal;
  374. RetVal = current;
  375. }
  376. temp2 = ILibParseStringAdv(field->data,0,field->datalength,":",1);
  377. if(temp2->NumResults==1)
  378. {
  379. RetVal->Prefix = NULL;
  380. RetVal->PrefixLength = 0;
  381. temp3 = ILibParseStringAdv(field->data,0,field->datalength,"=",1);
  382. }
  383. else
  384. {
  385. RetVal->Prefix = temp2->FirstResult->data;
  386. RetVal->PrefixLength = temp2->FirstResult->datalength;
  387. temp3 = ILibParseStringAdv(field->data,RetVal->PrefixLength+1,field->datalength-RetVal->PrefixLength-1,"=",1);
  388. }
  389. ILibDestructParserResults(temp2);
  390. RetVal->Name = temp3->FirstResult->data;
  391. RetVal->NameLength = temp3->FirstResult->datalength;
  392. RetVal->Value = temp3->LastResult->data;
  393. RetVal->ValueLength = temp3->LastResult->datalength;
  394. ILibDestructParserResults(temp3);
  395. field = field->NextResult;
  396. }
  397. ILibDestructParserResults(xml);
  398. return(RetVal);
  399. }
  400. struct ILibXMLNode *ILibParseXML(char *buffer, int offset, int length)
  401. {
  402. struct parser_result *xml;
  403. struct parser_result_field *field;
  404. struct parser_result *temp;
  405. struct parser_result *temp2;
  406. struct parser_result *temp3;
  407. char* TagName;
  408. int TagNameLength;
  409. int StartTag;
  410. int EmptyTag;
  411. int i;
  412. struct ILibXMLNode *RetVal = NULL;
  413. struct ILibXMLNode *current = NULL;
  414. struct ILibXMLNode *x = NULL;
  415. char *NSTag;
  416. int NSTagLength;
  417. xml = ILibParseString(buffer,offset,length,"<",1);
  418. field = xml->FirstResult;
  419. while(field!=NULL)
  420. {
  421. if(memcmp(field->data,"?",1)!=0)
  422. {
  423. EmptyTag = 0;
  424. if(memcmp(field->data,"/",1)==0)
  425. {
  426. StartTag = 0;
  427. field->data = field->data+1;
  428. field->datalength -= 1;
  429. temp2 = ILibParseString(field->data,0,field->datalength,">",1);
  430. }
  431. else
  432. {
  433. StartTag = -1;
  434. temp2 = ILibParseString(field->data,0,field->datalength,">",1);
  435. if(temp2->FirstResult->data[temp2->FirstResult->datalength-1]=='/')
  436. {
  437. EmptyTag = -1;
  438. }
  439. }
  440. temp = ILibParseString(temp2->FirstResult->data,0,temp2->FirstResult->datalength," ",1);
  441. temp3 = ILibParseString(temp->FirstResult->data,0,temp->FirstResult->datalength,":",1);
  442. if(temp3->NumResults==1)
  443. {
  444. NSTag = NULL;
  445. NSTagLength = 0;
  446. TagName = temp3->FirstResult->data;
  447. TagNameLength = temp3->FirstResult->datalength;
  448. }
  449. else
  450. {
  451. NSTag = temp3->FirstResult->data;
  452. NSTagLength = temp3->FirstResult->datalength;
  453. TagName = temp3->FirstResult->NextResult->data;
  454. TagNameLength = temp3->FirstResult->NextResult->datalength;
  455. }
  456. ILibDestructParserResults(temp3);
  457. for(i=0;i<TagNameLength;++i)
  458. {
  459. if( (TagName[i]==' ')||(TagName[i]=='/')||(TagName[i]=='>')||(TagName[i]=='\r')||(TagName[i]=='\n') )
  460. {
  461. if(i!=0)
  462. {
  463. if(TagName[i]=='/')
  464. {
  465. EmptyTag = -1;
  466. }
  467. TagNameLength = i;
  468. break;
  469. }
  470. }
  471. }
  472. if(TagNameLength!=0)
  473. {
  474. x = (struct ILibXMLNode*)MALLOC(sizeof(struct ILibXMLNode));
  475. x->Next = NULL;
  476. x->Name = TagName;
  477. x->NameLength = TagNameLength;
  478. x->StartTag = StartTag;
  479. x->NSTag = NSTag;
  480. x->NSLength = NSTagLength;
  481. x->Parent = NULL;
  482. x->Peer = NULL;
  483. x->ClosingTag = NULL;
  484. x->EmptyTag = 0;
  485. if(StartTag==0)
  486. {
  487. x->Reserved = field->data;
  488. do
  489. {
  490. (char*)x->Reserved -= 1;
  491. }while(*((char*)x->Reserved)=='<');
  492. }
  493. else
  494. {
  495. x->Reserved = temp2->LastResult->data;
  496. }
  497. if(RetVal==NULL)
  498. {
  499. RetVal = x;
  500. }
  501. else
  502. {
  503. current->Next = x;
  504. }
  505. current = x;
  506. if(EmptyTag!=0)
  507. {
  508. x = (struct ILibXMLNode*)MALLOC(sizeof(struct ILibXMLNode));
  509. x->Next = NULL;
  510. x->Name = TagName;
  511. x->NameLength = TagNameLength;
  512. x->StartTag = 0;
  513. x->NSTag = NSTag;
  514. x->NSLength = NSTagLength;
  515. x->Parent = NULL;
  516. x->Peer = NULL;
  517. x->ClosingTag = NULL;
  518. x->Reserved = current->Reserved;
  519. current->EmptyTag = -1;
  520. current->Next = x;
  521. current = x;
  522. }
  523. }
  524. ILibDestructParserResults(temp2);
  525. ILibDestructParserResults(temp);
  526. }
  527. field = field->NextResult;
  528. }
  529. ILibDestructParserResults(xml);
  530. return(RetVal);
  531. }
  532. void ILibCreateStack(void **TheStack)
  533. {
  534. *TheStack = NULL;
  535. }
  536. void ILibPushStack(void **TheStack, void *data)
  537. {
  538. struct ILibStackNode *RetVal = (struct ILibStackNode*)MALLOC(sizeof(struct ILibStackNode));
  539. RetVal->Data = data;
  540. RetVal->Next = *TheStack;
  541. *TheStack = RetVal;
  542. }
  543. void *ILibPopStack(void **TheStack)
  544. {
  545. void *RetVal = NULL;
  546. void *Temp;
  547. if(*TheStack!=NULL)
  548. {
  549. RetVal = ((struct ILibStackNode*)*TheStack)->Data;
  550. Temp = *TheStack;
  551. *TheStack = ((struct ILibStackNode*)*TheStack)->Next;
  552. FREE(Temp);
  553. }
  554. return(RetVal);
  555. }
  556. void *ILibPeekStack(void **TheStack)
  557. {
  558. void *RetVal = NULL;
  559. if(*TheStack!=NULL)
  560. {
  561. RetVal = ((struct ILibStackNode*)*TheStack)->Data;
  562. }
  563. return(RetVal);
  564. }
  565. void ILibClearStack(void **TheStack)
  566. {
  567. void *Temp = *TheStack;
  568. do
  569. {
  570. ILibPopStack(&Temp);
  571. }while(Temp!=NULL);
  572. *TheStack = NULL;
  573. }
  574. void ILibDestroyHashTree(void *tree)
  575. {
  576. struct HashNode *c = (struct HashNode*)tree;
  577. struct HashNode *n;
  578. while(c!=NULL)
  579. {
  580. n = c->Next;
  581. if(c->KeyValue!=NULL) {FREE(c->KeyValue);}
  582. FREE(c);
  583. c = n;
  584. }
  585. }
  586. void* ILibInitHashTree()
  587. {
  588. struct HashNode *RetVal = (struct HashNode*)MALLOC(sizeof(struct HashNode));
  589. memset(RetVal,0,sizeof(struct HashNode));
  590. return(RetVal);
  591. }
  592. int ILibGetHashValue(void *key, int keylength)
  593. {
  594. int HashValue=0;
  595. char TempValue[4];
  596. if(keylength<=4)
  597. {
  598. memset(TempValue,0,4);
  599. memcpy(TempValue,key,keylength);
  600. HashValue = *((int*)TempValue);
  601. }
  602. else
  603. {
  604. memcpy(TempValue,key,4);
  605. HashValue = *((int*)TempValue);
  606. memcpy(TempValue,(char*)key+(keylength-4),4);
  607. HashValue = HashValue^(*((int*)TempValue));
  608. if(keylength>=10)
  609. {
  610. memcpy(TempValue,(char*)key+(keylength/2),4);
  611. HashValue = HashValue^(*((int*)TempValue));
  612. }
  613. }
  614. return(HashValue);
  615. }
  616. struct HashNode* ILibFindEntry(void *hashtree, void *key, int keylength, int create)
  617. {
  618. struct HashNode *current = (struct HashNode*)hashtree;
  619. int HashValue = ILibGetHashValue(key,keylength);
  620. int done = 0;
  621. while(done==0)
  622. {
  623. if(current->KeyHash==HashValue)
  624. {
  625. if(current->KeyLength==keylength && memcmp(current->KeyValue,key,keylength)==0)
  626. {
  627. return(current);
  628. }
  629. }
  630. if(current->Next!=NULL)
  631. {
  632. current = current->Next;
  633. }
  634. else if(create!=0)
  635. {
  636. current->Next = (struct HashNode*)MALLOC(sizeof(struct HashNode));
  637. memset(current->Next,0,sizeof(struct HashNode));
  638. current->Next->Prev = current;
  639. current->Next->KeyHash = HashValue;
  640. current->Next->KeyValue = (void*)MALLOC(keylength);
  641. memcpy(current->Next->KeyValue,key,keylength);
  642. current->Next->KeyLength = keylength;
  643. return(current->Next);
  644. }
  645. else
  646. {
  647. return(NULL);
  648. }
  649. }
  650. return(NULL);
  651. }
  652. void ILibAddEntry(void* hashtree, char* key, int keylength, void *value)
  653. {
  654. struct HashNode* n = ILibFindEntry(hashtree,key,keylength,1);
  655. n->Data = value;
  656. }
  657. void* ILibGetEntry(void *hashtree, char* key, int keylength)
  658. {
  659. struct HashNode* n = ILibFindEntry(hashtree,key,keylength,0);
  660. if(n==NULL)
  661. {
  662. return(NULL);
  663. }
  664. else
  665. {
  666. return(n->Data);
  667. }
  668. }
  669. void ILibDeleteEntry(void *hashtree, char* key, int keylength)
  670. {
  671. struct HashNode* n = ILibFindEntry(hashtree,key,keylength,0);
  672. if(n!=NULL)
  673. {
  674. n->Prev->Next = n->Next;
  675. if(n->Next!=NULL)
  676. {
  677. n->Next->Prev = n->Prev;
  678. }
  679. FREE(n->KeyValue);
  680. FREE(n);
  681. }
  682. }
  683. int ILibGetLong(char *TestValue, int TestValueLength, long* NumericValue)
  684. {
  685. char* StopString;
  686. char* TempBuffer2 = (char*)MALLOC(1+sizeof(char)*19);
  687. char* TempBuffer = (char*)MALLOC(1+sizeof(char)*TestValueLength);
  688. memcpy(TempBuffer,TestValue,TestValueLength);
  689. TempBuffer[TestValueLength] = '\0';
  690. *NumericValue = strtol(TempBuffer,&StopString,10);
  691. if(*StopString!='\0')
  692. {
  693. FREE(TempBuffer);
  694. FREE(TempBuffer2);
  695. return(-1);
  696. }
  697. else
  698. {
  699. FREE(TempBuffer);
  700. FREE(TempBuffer2);
  701. if(errno!=ERANGE)
  702. {
  703. return(0);
  704. }
  705. else
  706. {
  707. return(-1);
  708. }
  709. }
  710. }
  711. int ILibGetULong(const char *TestValue, const int TestValueLength, unsigned long* NumericValue){
  712. char* StopString;
  713. char* TempBuffer2 = (char*)MALLOC(1+sizeof(char)*19);
  714. char* TempBuffer = (char*)MALLOC(1+sizeof(char)*TestValueLength);
  715. memcpy(TempBuffer,TestValue,TestValueLength);
  716. TempBuffer[TestValueLength] = '\0';
  717. *NumericValue = strtoul(TempBuffer,&StopString,10);
  718. if(*StopString!='\0')
  719. {
  720. FREE(TempBuffer);
  721. FREE(TempBuffer2);
  722. return(-1);
  723. }
  724. else
  725. {
  726. FREE(TempBuffer);
  727. FREE(TempBuffer2);
  728. if(errno!=ERANGE)
  729. {
  730. if(memcmp(TestValue,"-",1)==0)
  731. {
  732. return(-1);
  733. }
  734. return(0);
  735. }
  736. else
  737. {
  738. return(-1);
  739. }
  740. }
  741. }
  742. int ILibIsDelimiter(char* buffer, int offset, int buffersize, char* Delimiter, int DelimiterLength)
  743. {
  744. int i=0;
  745. int RetVal = 1;
  746. if(offset+DelimiterLength>buffersize)
  747. {
  748. return(0);
  749. }
  750. for(i=0;i<DelimiterLength;++i)
  751. {
  752. if(buffer[offset+i]!=Delimiter[i])
  753. {
  754. RetVal = 0;
  755. break;
  756. }
  757. }
  758. return(RetVal);
  759. }
  760. struct parser_result* ILibParseStringAdv(char* buffer, int offset, int length, char* Delimiter, int DelimiterLength)
  761. {
  762. struct parser_result* RetVal = (struct parser_result*)MALLOC(sizeof(struct parser_result));
  763. int i=0;
  764. char* Token = NULL;
  765. int TokenLength = 0;
  766. struct parser_result_field *p_resultfield;
  767. int Ignore = 0;
  768. char StringDelimiter=0;
  769. RetVal->FirstResult = NULL;
  770. RetVal->NumResults = 0;
  771. Token = buffer + offset;
  772. for(i=offset;i<length;++i)
  773. {
  774. if(StringDelimiter==0)
  775. {
  776. if(buffer[i]=='"')
  777. {
  778. StringDelimiter='"';
  779. Ignore=1;
  780. }
  781. else
  782. {
  783. if(buffer[i]=='\'')
  784. {
  785. StringDelimiter='\'';
  786. Ignore=1;
  787. }
  788. }
  789. }
  790. else
  791. {
  792. if(buffer[i]==StringDelimiter)
  793. {
  794. Ignore=((Ignore==0)?1:0);
  795. }
  796. }
  797. if(Ignore==0 && ILibIsDelimiter(buffer,i,length,Delimiter,DelimiterLength))
  798. {
  799. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  800. p_resultfield->data = Token;
  801. p_resultfield->datalength = TokenLength;
  802. p_resultfield->NextResult = NULL;
  803. if(RetVal->FirstResult != NULL)
  804. {
  805. RetVal->LastResult->NextResult = p_resultfield;
  806. RetVal->LastResult = p_resultfield;
  807. }
  808. else
  809. {
  810. RetVal->FirstResult = p_resultfield;
  811. RetVal->LastResult = p_resultfield;
  812. }
  813. ++RetVal->NumResults;
  814. i = i + DelimiterLength -1;
  815. Token = Token + TokenLength + DelimiterLength;
  816. TokenLength = 0;
  817. }
  818. else
  819. {
  820. ++TokenLength;
  821. }
  822. }
  823. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  824. p_resultfield->data = Token;
  825. p_resultfield->datalength = TokenLength;
  826. p_resultfield->NextResult = NULL;
  827. if(RetVal->FirstResult != NULL)
  828. {
  829. RetVal->LastResult->NextResult = p_resultfield;
  830. RetVal->LastResult = p_resultfield;
  831. }
  832. else
  833. {
  834. RetVal->FirstResult = p_resultfield;
  835. RetVal->LastResult = p_resultfield;
  836. }
  837. ++RetVal->NumResults;
  838. return(RetVal);
  839. }
  840. struct parser_result* ILibParseString(char* buffer, int offset, int length, char* Delimiter, int DelimiterLength)
  841. {
  842. struct parser_result* RetVal = (struct parser_result*)MALLOC(sizeof(struct parser_result));
  843. int i=0;
  844. char* Token = NULL;
  845. int TokenLength = 0;
  846. struct parser_result_field *p_resultfield;
  847. RetVal->FirstResult = NULL;
  848. RetVal->NumResults = 0;
  849. Token = buffer + offset;
  850. for(i=offset;i<length;++i)
  851. {
  852. if(ILibIsDelimiter(buffer,i,length,Delimiter,DelimiterLength))
  853. {
  854. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  855. p_resultfield->data = Token;
  856. p_resultfield->datalength = TokenLength;
  857. p_resultfield->NextResult = NULL;
  858. if(RetVal->FirstResult != NULL)
  859. {
  860. RetVal->LastResult->NextResult = p_resultfield;
  861. RetVal->LastResult = p_resultfield;
  862. }
  863. else
  864. {
  865. RetVal->FirstResult = p_resultfield;
  866. RetVal->LastResult = p_resultfield;
  867. }
  868. ++RetVal->NumResults;
  869. i = i + DelimiterLength -1;
  870. Token = Token + TokenLength + DelimiterLength;
  871. TokenLength = 0;
  872. }
  873. else
  874. {
  875. ++TokenLength;
  876. }
  877. }
  878. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  879. p_resultfield->data = Token;
  880. p_resultfield->datalength = TokenLength;
  881. p_resultfield->NextResult = NULL;
  882. if(RetVal->FirstResult != NULL)
  883. {
  884. RetVal->LastResult->NextResult = p_resultfield;
  885. RetVal->LastResult = p_resultfield;
  886. }
  887. else
  888. {
  889. RetVal->FirstResult = p_resultfield;
  890. RetVal->LastResult = p_resultfield;
  891. }
  892. ++RetVal->NumResults;
  893. return(RetVal);
  894. }
  895. void ILibDestructParserResults(struct parser_result *result)
  896. {
  897. struct parser_result_field *node = result->FirstResult;
  898. struct parser_result_field *temp;
  899. while(node!=NULL)
  900. {
  901. temp = node->NextResult;
  902. FREE(node);
  903. node = temp;
  904. }
  905. FREE(result);
  906. }
  907. void ILibDestructPacket(struct packetheader *packet)
  908. {
  909. struct packetheader_field_node *node = packet->FirstField;
  910. struct packetheader_field_node *nextnode;
  911. while(node!=NULL)
  912. {
  913. nextnode = node->NextField;
  914. if(node->UserAllocStrings!=0)
  915. {
  916. FREE(node->Field);
  917. FREE(node->FieldData);
  918. }
  919. FREE(node);
  920. node = nextnode;
  921. }
  922. if(packet->UserAllocStrings!=0)
  923. {
  924. if(packet->StatusData!=NULL) {FREE(packet->StatusData);}
  925. if(packet->Directive!=NULL) {FREE(packet->Directive);}
  926. if(packet->DirectiveObj!=NULL) {FREE(packet->DirectiveObj);}
  927. if(packet->Body!=NULL) FREE(packet->Body);
  928. }
  929. FREE(packet);
  930. }
  931. struct packetheader* ILibParsePacketHeader(char* buffer, int offset, int length)
  932. {
  933. struct packetheader *RetVal = (struct packetheader*)MALLOC(sizeof(struct packetheader));
  934. struct parser_result *_packet;
  935. struct parser_result *p;
  936. struct parser_result *StartLine;
  937. struct parser_result_field *HeaderLine;
  938. struct parser_result_field *f;
  939. char* tempbuffer;
  940. struct packetheader_field_node *node;
  941. int i=0;
  942. int FLNWS = -1;
  943. int FTNWS = -1;
  944. RetVal->UserAllocStrings = 0;
  945. RetVal->Directive = NULL;
  946. RetVal->DirectiveLength = 0;
  947. RetVal->Body = NULL;
  948. RetVal->BodyLength = 0;
  949. RetVal->FirstField = NULL;
  950. RetVal->LastField = NULL;
  951. RetVal->Source = NULL;
  952. p = (struct parser_result*)ILibParseString(buffer,offset,length,"\r\n",2);
  953. _packet = p;
  954. f = p->FirstResult;
  955. StartLine = (struct parser_result*)ILibParseString(f->data,0,f->datalength," ",1);
  956. HeaderLine = f->NextResult;
  957. if(memcmp(StartLine->FirstResult->data,
  958. "HTTP/",
  959. 5)==0)
  960. {
  961. /* Response Packet */
  962. p = (struct parser_result*)ILibParseString(StartLine->FirstResult->data,
  963. 0,
  964. StartLine->FirstResult->datalength,
  965. "/",1);
  966. RetVal->Version = p->LastResult->data;
  967. RetVal->VersionLength = p->LastResult->datalength;
  968. ILibDestructParserResults(p);
  969. tempbuffer = (char*)MALLOC(1+sizeof(char)*(StartLine->FirstResult->NextResult->datalength));
  970. memcpy(tempbuffer,StartLine->FirstResult->NextResult->data,
  971. StartLine->FirstResult->NextResult->datalength);
  972. tempbuffer[StartLine->FirstResult->NextResult->datalength] = '\0';
  973. RetVal->StatusCode = (int)atoi(tempbuffer);
  974. FREE(tempbuffer);
  975. RetVal->StatusData = StartLine->FirstResult->NextResult->NextResult->data;
  976. RetVal->StatusDataLength = StartLine->FirstResult->NextResult->NextResult->datalength;
  977. }
  978. else
  979. {
  980. /* Request Packet */
  981. RetVal->Directive = StartLine->FirstResult->data;
  982. RetVal->DirectiveLength = StartLine->FirstResult->datalength;
  983. RetVal->DirectiveObj = StartLine->FirstResult->NextResult->data;
  984. RetVal->DirectiveObjLength = StartLine->FirstResult->NextResult->datalength;
  985. RetVal->StatusCode = -1;
  986. p = (struct parser_result*)ILibParseString(StartLine->LastResult->data,
  987. 0,
  988. StartLine->LastResult->datalength,
  989. "/",1);
  990. RetVal->Version = p->LastResult->data;
  991. RetVal->VersionLength = p->LastResult->datalength;
  992. ILibDestructParserResults(p);
  993. RetVal->Directive[RetVal->DirectiveLength] = '\0';
  994. RetVal->DirectiveObj[RetVal->DirectiveObjLength] = '\0';
  995. }
  996. while(HeaderLine!=NULL)
  997. {
  998. if(HeaderLine->datalength==0)
  999. {
  1000. break;
  1001. }
  1002. node = (struct packetheader_field_node*)MALLOC(sizeof(struct packetheader_field_node));
  1003. memset(node,0,sizeof(struct packetheader_field_node));
  1004. for(i=0;i<HeaderLine->datalength;++i)
  1005. {
  1006. if(*((HeaderLine->data)+i)==':')
  1007. {
  1008. node->Field = HeaderLine->data;
  1009. node->FieldLength = i;
  1010. node->FieldData = HeaderLine->data + i + 1;
  1011. node->FieldDataLength = (HeaderLine->datalength)-i-1;
  1012. break;
  1013. }
  1014. }
  1015. if(node->Field==NULL)
  1016. {
  1017. FREE(RetVal);
  1018. RetVal = NULL;
  1019. break;
  1020. }
  1021. FLNWS = 0;
  1022. FTNWS = node->FieldDataLength-1;
  1023. for(i=0;i<node->FieldDataLength;++i)
  1024. {
  1025. if(*((node->FieldData)+i)!=' ')
  1026. {
  1027. FLNWS = i;
  1028. break;
  1029. }
  1030. }
  1031. for(i=(node->FieldDataLength)-1;i>=0;--i)
  1032. {
  1033. if(*((node->FieldData)+i)!=' ')
  1034. {
  1035. FTNWS = i;
  1036. break;
  1037. }
  1038. }
  1039. node->FieldData = (node->FieldData) + FLNWS;
  1040. node->FieldDataLength = (FTNWS - FLNWS)+1;
  1041. node->Field[node->FieldLength] = '\0';
  1042. node->FieldData[node->FieldDataLength] = '\0';
  1043. node->UserAllocStrings = 0;
  1044. node->NextField = NULL;
  1045. if(RetVal->FirstField==NULL)
  1046. {
  1047. RetVal->FirstField = node;
  1048. RetVal->LastField = node;
  1049. }
  1050. else
  1051. {
  1052. RetVal->LastField->NextField = node;
  1053. }
  1054. RetVal->LastField = node;
  1055. HeaderLine = HeaderLine->NextResult;
  1056. }
  1057. ILibDestructParserResults(_packet);
  1058. ILibDestructParserResults(StartLine);
  1059. return(RetVal);
  1060. }
  1061. int ILibGetRawPacket(struct packetheader* packet,char **RetVal)
  1062. {
  1063. int i;
  1064. int BufferSize = 0;
  1065. char* Buffer;
  1066. struct packetheader_field_node *node;
  1067. if(packet->StatusCode!=-1)
  1068. {
  1069. BufferSize = 12 + packet->VersionLength + packet->StatusDataLength;
  1070. /* HTTP/1.1 200 OK\r\n */
  1071. }
  1072. else
  1073. {
  1074. BufferSize = packet->DirectiveLength + packet->DirectiveObjLength + 12;
  1075. /* GET / HTTP/1.1\r\n */
  1076. }
  1077. node = packet->FirstField;
  1078. while(node!=NULL)
  1079. {
  1080. BufferSize += node->FieldLength + node->FieldDataLength + 4;
  1081. node = node->NextField;
  1082. }
  1083. BufferSize += (3+packet->BodyLength);
  1084. *RetVal = (char*)MALLOC(BufferSize);
  1085. Buffer = *RetVal;
  1086. if(packet->StatusCode!=-1)
  1087. {
  1088. memcpy(Buffer,"HTTP/",5);
  1089. memcpy(Buffer+5,packet->Version,packet->VersionLength);
  1090. i = 5+packet->VersionLength;
  1091. i+=sprintf(Buffer+i," %d ",packet->StatusCode);
  1092. memcpy(Buffer+i,packet->StatusData,packet->StatusDataLength);
  1093. i+=packet->StatusDataLength;
  1094. memcpy(Buffer+i,"\r\n",2);
  1095. i+=2;
  1096. /* HTTP/1.1 200 OK\r\n */
  1097. }
  1098. else
  1099. {
  1100. memcpy(Buffer,packet->Directive,packet->DirectiveLength);
  1101. i = packet->DirectiveLength;
  1102. memcpy(Buffer+i," ",1);
  1103. i+=1;
  1104. memcpy(Buffer+i,packet->DirectiveObj,packet->DirectiveObjLength);
  1105. i+=packet->DirectiveObjLength;
  1106. memcpy(Buffer+i," HTTP/",6);
  1107. i+=6;
  1108. memcpy(Buffer+i,packet->Version,packet->VersionLength);
  1109. i+=packet->VersionLength;
  1110. memcpy(Buffer+i,"\r\n",2);
  1111. i+=2;
  1112. /* GET / HTTP/1.1\r\n */
  1113. }
  1114. node = packet->FirstField;
  1115. while(node!=NULL)
  1116. {
  1117. memcpy(Buffer+i,node->Field,node->FieldLength);
  1118. i+=node->FieldLength;
  1119. memcpy(Buffer+i,": ",2);
  1120. i+=2;
  1121. memcpy(Buffer+i,node->FieldData,node->FieldDataLength);
  1122. i+=node->FieldDataLength;
  1123. memcpy(Buffer+i,"\r\n",2);
  1124. i+=2;
  1125. BufferSize += node->FieldLength + node->FieldDataLength + 4;
  1126. node = node->NextField;
  1127. }
  1128. memcpy(Buffer+i,"\r\n",2);
  1129. i+=2;
  1130. memcpy(Buffer+i,packet->Body,packet->BodyLength);
  1131. i+=packet->BodyLength;
  1132. Buffer[i] = '\0';
  1133. return(i);
  1134. }
  1135. unsigned short ILibGetDGramSocket(int local, SOCKET *TheSocket)
  1136. {
  1137. unsigned short PortNum = -1;
  1138. struct sockaddr_in addr;
  1139. memset((char *)&(addr), 0, sizeof(addr));
  1140. addr.sin_family = AF_INET;
  1141. addr.sin_addr.s_addr = local;
  1142. *TheSocket = socket(AF_INET, SOCK_DGRAM, 0);
  1143. do
  1144. {
  1145. PortNum = (unsigned short)(50000 + ((unsigned short)rand() % 15000));
  1146. addr.sin_port = htons(PortNum);
  1147. }
  1148. while(bind(*TheSocket, (struct sockaddr *) &(addr), sizeof(addr)) < 0);
  1149. return(PortNum);
  1150. }
  1151. unsigned short ILibGetStreamSocket(int local, SOCKET *TheSocket)
  1152. {
  1153. unsigned short PortNum = -1;
  1154. struct sockaddr_in addr;
  1155. memset((char *)&(addr), 0, sizeof(addr));
  1156. addr.sin_family = AF_INET;
  1157. addr.sin_addr.s_addr = local;
  1158. *TheSocket = socket(AF_INET, SOCK_STREAM, 0);
  1159. do
  1160. {
  1161. PortNum = (unsigned short)(50000 + ((unsigned short)rand() % 15000));
  1162. addr.sin_port = htons(PortNum);
  1163. }
  1164. while(bind(*TheSocket, (struct sockaddr *) &(addr), sizeof(addr)) < 0);
  1165. return(PortNum);
  1166. }
  1167. struct packetheader *ILibCreateEmptyPacket()
  1168. {
  1169. struct packetheader *RetVal = (struct packetheader*)MALLOC(sizeof(struct packetheader));
  1170. RetVal->UserAllocStrings = -1;
  1171. RetVal->Directive = NULL;
  1172. RetVal->DirectiveLength = 0;
  1173. RetVal->DirectiveObj = NULL;
  1174. RetVal->DirectiveObjLength = 0;
  1175. RetVal->StatusCode = -1;
  1176. RetVal->StatusData = NULL;
  1177. RetVal->StatusDataLength = 0;
  1178. RetVal->Version = "1.0";
  1179. RetVal->VersionLength = 3;
  1180. RetVal->Body = NULL;
  1181. RetVal->BodyLength = 0;
  1182. RetVal->FirstField = NULL;
  1183. RetVal->LastField = NULL;
  1184. RetVal->Source = NULL;
  1185. RetVal->ReceivingAddress = 0;
  1186. RetVal->UserAllocStrings = 0;
  1187. return(RetVal);
  1188. }
  1189. void ILibSetStatusCode(struct packetheader *packet, int StatusCode, char *StatusData, int StatusDataLength)
  1190. {
  1191. packet->StatusCode = StatusCode;
  1192. packet->StatusData = (char*)MALLOC(StatusDataLength+1);
  1193. memcpy(packet->StatusData,StatusData,StatusDataLength);
  1194. packet->StatusData[StatusDataLength] = '\0';
  1195. packet->StatusDataLength = StatusDataLength;
  1196. }
  1197. void ILibSetDirective(struct packetheader *packet, char* Directive, int DirectiveLength, char* DirectiveObj, int DirectiveObjLength)
  1198. {
  1199. packet->Directive = (char*)MALLOC(DirectiveLength+1);
  1200. memcpy(packet->Directive,Directive,DirectiveLength);
  1201. packet->Directive[DirectiveLength] = '\0';
  1202. packet->DirectiveLength = DirectiveLength;
  1203. packet->DirectiveObj = (char*)MALLOC(DirectiveObjLength+1);
  1204. memcpy(packet->DirectiveObj,DirectiveObj,DirectiveObjLength);
  1205. packet->DirectiveObj[DirectiveObjLength] = '\0';
  1206. packet->DirectiveObjLength = DirectiveObjLength;
  1207. packet->UserAllocStrings = -1;
  1208. }
  1209. void ILibAddHeaderLine(struct packetheader *packet, char* FieldName, int FieldNameLength, char* FieldData, int FieldDataLength)
  1210. {
  1211. struct packetheader_field_node *node;
  1212. node = (struct packetheader_field_node*)MALLOC(sizeof(struct packetheader_field_node));
  1213. node->UserAllocStrings = -1;
  1214. node->Field = (char*)MALLOC(FieldNameLength+1);
  1215. memcpy(node->Field,FieldName,FieldNameLength);
  1216. node->Field[FieldNameLength] = '\0';
  1217. node->FieldLength = FieldNameLength;
  1218. node->FieldData = (char*)MALLOC(FieldDataLength+1);
  1219. memcpy(node->FieldData,FieldData,FieldDataLength);
  1220. node->FieldData[FieldDataLength] = '\0';
  1221. node->FieldDataLength = FieldDataLength;
  1222. node->NextField = NULL;
  1223. if(packet->LastField!=NULL)
  1224. {
  1225. packet->LastField->NextField = node;
  1226. packet->LastField = node;
  1227. }
  1228. else
  1229. {
  1230. packet->LastField = node;
  1231. packet->FirstField = node;
  1232. }
  1233. }
  1234. char* ILibGetHeaderLine(struct packetheader *packet, char* FieldName, int FieldNameLength)
  1235. {
  1236. char* RetVal = NULL;
  1237. struct packetheader_field_node *node = packet->FirstField;
  1238. int i;
  1239. while(node!=NULL)
  1240. {
  1241. if(strncasecmp(FieldName,node->Field,FieldNameLength)==0)
  1242. {
  1243. RetVal = (char*)MALLOC(node->FieldDataLength+1);
  1244. for(i=0;i<node->FieldDataLength;++i)
  1245. {
  1246. if(node->FieldData[i]!=' ') {break;}
  1247. }
  1248. if(i==node->FieldDataLength-1) {i = 0;}
  1249. memcpy(RetVal,node->FieldData+i,node->FieldDataLength-i);
  1250. RetVal[node->FieldDataLength-i] = '\0';
  1251. break;
  1252. }
  1253. node = node->NextField;
  1254. }
  1255. return(RetVal);
  1256. }
  1257. static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  1258. static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
  1259. /* encode 3 8-bit binary bytes as 4 '6-bit' characters */
  1260. void ILibencodeblock( unsigned char in[3], unsigned char out[4], int len )
  1261. {
  1262. out[0] = cb64[ in[0] >> 2 ];
  1263. out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
  1264. out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
  1265. out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
  1266. }
  1267. /* Base64 encode a stream adding padding and line breaks as per spec. */
  1268. int ILibBase64Encode(unsigned char* input, const int inputlen, unsigned char** output)
  1269. {
  1270. unsigned char* out;
  1271. unsigned char* in;
  1272. *output = (unsigned char*)malloc(((inputlen * 4) / 3) + 5);
  1273. out = *output;
  1274. in = input;
  1275. if (input == NULL || inputlen == 0)
  1276. {
  1277. *output = NULL;
  1278. return 0;
  1279. }
  1280. while ((in+3) <= (input+inputlen))
  1281. {
  1282. ILibencodeblock(in, out, 3);
  1283. in += 3;
  1284. out += 4;
  1285. }
  1286. if ((input+inputlen)-in == 1)
  1287. {
  1288. ILibencodeblock(in, out, 1);
  1289. out += 4;
  1290. }
  1291. else
  1292. if ((input+inputlen)-in == 2)
  1293. {
  1294. ILibencodeblock(in, out, 2);
  1295. out += 4;
  1296. }
  1297. *out = 0;
  1298. return (int)(out-*output);
  1299. }
  1300. /* Decode 4 '6-bit' characters into 3 8-bit binary bytes */
  1301. void ILibdecodeblock( unsigned char in[4], unsigned char out[3] )
  1302. {
  1303. out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
  1304. out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
  1305. out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
  1306. }
  1307. /* decode a base64 encoded stream discarding padding, line breaks and noise */
  1308. int ILibBase64Decode(unsigned char* input, const int inputlen, unsigned char** output)
  1309. {
  1310. unsigned char* inptr;
  1311. unsigned char* out;
  1312. unsigned char v;
  1313. unsigned char in[4];
  1314. int i, len;
  1315. if (input == NULL || inputlen == 0)
  1316. {
  1317. *output = NULL;
  1318. return 0;
  1319. }
  1320. *output = (unsigned char*)malloc(((inputlen * 3) / 4) + 4);
  1321. out = *output;
  1322. inptr = input;
  1323. while( inptr <= (input+inputlen) )
  1324. {
  1325. for( len = 0, i = 0; i < 4 && inptr <= (input+inputlen); i++ )
  1326. {
  1327. v = 0;
  1328. while( inptr <= (input+inputlen) && v == 0 ) {
  1329. v = (unsigned char) *inptr;
  1330. inptr++;
  1331. v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
  1332. if( v ) {
  1333. v = (unsigned char) ((v == '$') ? 0 : v - 61);
  1334. }
  1335. }
  1336. if( inptr <= (input+inputlen) ) {
  1337. len++;
  1338. if( v ) {
  1339. in[ i ] = (unsigned char) (v - 1);
  1340. }
  1341. }
  1342. else {
  1343. in[i] = 0;
  1344. }
  1345. }
  1346. if( len )
  1347. {
  1348. ILibdecodeblock( in, out );
  1349. out += len-1;
  1350. }
  1351. }
  1352. *out = 0;
  1353. return (int)(out-*output);
  1354. }
  1355. int ILibInPlaceXmlUnEscape(char* data)
  1356. {
  1357. char* end = data+strlen(data);
  1358. char* i = data; /* src */
  1359. char* j = data; /* dest */
  1360. while (j < end)
  1361. {
  1362. if (j[0] == '&' && j[1] == 'q' && j[2] == 'u' && j[3] == 'o' && j[4] == 't' && j[5] == ';') // &quot;
  1363. {
  1364. i[0] = '"';
  1365. j += 5;
  1366. }
  1367. else if (j[0] == '&' && j[1] == 'a' && j[2] == 'p' && j[3] == 'o' && j[4] == 's' && j[5] == ';') // &apos;
  1368. {
  1369. i[0] = '\'';
  1370. j += 5;
  1371. }
  1372. else if (j[0] == '&' && j[1] == 'a' && j[2] == 'm' && j[3] == 'p' && j[4] == ';') // &amp;
  1373. {
  1374. i[0] = '&';
  1375. j += 4;
  1376. }
  1377. else if (j[0] == '&' && j[1] == 'l' && j[2] == 't' && j[3] == ';') // &lt;
  1378. {
  1379. i[0] = '<';
  1380. j += 3;
  1381. }
  1382. else if (j[0] == '&' && j[1] == 'g' && j[2] == 't' && j[3] == ';') // &gt;
  1383. {
  1384. i[0] = '>';
  1385. j += 3;
  1386. }
  1387. else
  1388. {
  1389. i[0] = j[0];
  1390. }
  1391. i++;
  1392. j++;
  1393. }
  1394. i[0] = '\0';
  1395. return (int)(i - data);
  1396. }
  1397. int ILibXmlEscapeLength(const char* data)
  1398. {
  1399. int i = 0, j = 0;
  1400. while (data[i] != 0)
  1401. {
  1402. switch (data[i])
  1403. {
  1404. case '"':
  1405. j += 6;
  1406. break;
  1407. case '\'':
  1408. j += 6;
  1409. break;
  1410. case '<':
  1411. j += 4;
  1412. break;
  1413. case '>':
  1414. j += 4;
  1415. break;
  1416. case '&':
  1417. j += 5;
  1418. break;
  1419. default:
  1420. j++;
  1421. }
  1422. i++;
  1423. }
  1424. return j;
  1425. }
  1426. int ILibXmlEscape(char* outdata, const char* indata)
  1427. {
  1428. int i=0;
  1429. int inlen;
  1430. char* out;
  1431. out = outdata;
  1432. inlen = (int)strlen(indata);
  1433. for (i=0; i < inlen; i++)
  1434. {
  1435. if (indata[i] == '"')
  1436. {
  1437. memcpy(out, "&quot;", 6);
  1438. out = out + 6;
  1439. }
  1440. else
  1441. if (indata[i] == '\'')
  1442. {
  1443. memcpy(out, "&apos;", 6);
  1444. out = out + 6;
  1445. }
  1446. else
  1447. if (indata[i] == '<')
  1448. {
  1449. memcpy(out, "&lt;", 4);
  1450. out = out + 4;
  1451. }
  1452. else
  1453. if (indata[i] == '>')
  1454. {
  1455. memcpy(out, "&gt;", 4);
  1456. out = out + 4;
  1457. }
  1458. else
  1459. if (indata[i] == '&')
  1460. {
  1461. memcpy(out, "&amp;", 5);
  1462. out = out + 5;
  1463. }
  1464. else
  1465. {
  1466. out[0] = indata[i];
  1467. out++;
  1468. }
  1469. }
  1470. out[0] = 0;
  1471. return (int)(out - outdata);
  1472. }
  1473. void ILibLifeTime_Add(void *LifetimeMonitorObject,void *data, int seconds, void* Callback, void* Destroy)
  1474. {
  1475. int NeedUnBlock = 0;
  1476. struct timeval tv;
  1477. struct LifeTimeMonitorData *temp,*temp2;
  1478. struct LifeTimeMonitorData *ltms = (struct LifeTimeMonitorData*)MALLOC(sizeof(struct LifeTimeMonitorData));
  1479. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifetimeMonitorObject;
  1480. gettimeofday(&tv,NULL);
  1481. ltms->data = data;
  1482. ltms->ExpirationTick = tv.tv_sec + seconds;
  1483. ltms->CallbackPtr = Callback;
  1484. ltms->DestroyPtr = Destroy;
  1485. ltms->Next = NULL;
  1486. sem_wait(&(UPnPLifeTime->SyncLock));
  1487. if(UPnPLifeTime->LM==NULL)
  1488. {
  1489. UPnPLifeTime->LM = ltms;
  1490. NeedUnBlock = 1;
  1491. }
  1492. else
  1493. {
  1494. temp = UPnPLifeTime->LM;
  1495. temp2 = temp;
  1496. while(temp!=NULL)
  1497. {
  1498. if(ltms->ExpirationTick<=temp->ExpirationTick){break;}
  1499. temp2 = temp;
  1500. temp = temp->Next;
  1501. }
  1502. ltms->Next = temp;
  1503. if(temp!=temp2)
  1504. {
  1505. temp2->Next = ltms;
  1506. }
  1507. else
  1508. {
  1509. UPnPLifeTime->LM = ltms;
  1510. NeedUnBlock = 1;
  1511. }
  1512. }
  1513. if(NeedUnBlock!=0) {ILibForceUnBlockChain(UPnPLifeTime->Chain);}
  1514. sem_post(&(UPnPLifeTime->SyncLock));
  1515. }
  1516. void ILibLifeTime_Check(void *LifeTimeMonitorObject,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
  1517. {
  1518. struct timeval tv;
  1519. long CurrentTick;
  1520. struct LifeTimeMonitorData *Temp,*EVT,*Last=NULL;
  1521. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeMonitorObject;
  1522. int nexttick;
  1523. EVT = NULL;
  1524. sem_wait(&(UPnPLifeTime->SyncLock));
  1525. if(UPnPLifeTime->LM!=NULL)
  1526. {
  1527. gettimeofday(&tv,NULL);
  1528. CurrentTick = tv.tv_sec;
  1529. Temp = UPnPLifeTime->LM;
  1530. while(Temp!=NULL && Temp->ExpirationTick<=CurrentTick)
  1531. {
  1532. EVT = UPnPLifeTime->LM;
  1533. Last = Temp;
  1534. Temp = Temp->Next;
  1535. }
  1536. if(EVT != NULL)
  1537. {
  1538. if(Temp!=NULL)
  1539. {
  1540. UPnPLifeTime->LM = Temp;
  1541. Last->Next = NULL;
  1542. }
  1543. else
  1544. {
  1545. UPnPLifeTime->LM = NULL;
  1546. }
  1547. }
  1548. sem_post(&(UPnPLifeTime->SyncLock));
  1549. while(EVT!=NULL)
  1550. {
  1551. EVT->CallbackPtr(EVT->data);
  1552. Temp = EVT;
  1553. EVT = EVT->Next;
  1554. FREE(Temp);
  1555. }
  1556. if(UPnPLifeTime->LM!=NULL)
  1557. {
  1558. nexttick = UPnPLifeTime->LM->ExpirationTick-CurrentTick;
  1559. if(nexttick<*blocktime) {*blocktime=nexttick;}
  1560. }
  1561. }
  1562. else
  1563. {
  1564. sem_post(&(UPnPLifeTime->SyncLock));
  1565. }
  1566. }
  1567. void ILibLifeTime_Remove(void *LifeTimeToken, void *data)
  1568. {
  1569. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeToken;
  1570. struct LifeTimeMonitorData *first,*second;
  1571. sem_wait(&(UPnPLifeTime->SyncLock));
  1572. first = UPnPLifeTime->LM;
  1573. if(first==NULL)
  1574. {
  1575. sem_post(&(UPnPLifeTime->SyncLock));
  1576. return;
  1577. }
  1578. second = first->Next;
  1579. if(first->data==data)
  1580. {
  1581. UPnPLifeTime->LM = first->Next;
  1582. if(first->DestroyPtr!=NULL) {first->DestroyPtr(first->data);}
  1583. FREE(first);
  1584. }
  1585. else
  1586. {
  1587. while(second!=NULL)
  1588. {
  1589. if(second->data==data)
  1590. {
  1591. first->Next = second->Next;
  1592. if(second->DestroyPtr!=NULL) {second->DestroyPtr(second->data);}
  1593. FREE(second);
  1594. second = first->Next;
  1595. }
  1596. else
  1597. {
  1598. first = first->Next;
  1599. second = second->Next;
  1600. }
  1601. }
  1602. }
  1603. sem_post(&(UPnPLifeTime->SyncLock));
  1604. }
  1605. void ILibLifeTime_Flush(void *LifeTimeToken)
  1606. {
  1607. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeToken;
  1608. struct LifeTimeMonitorData *temp,*temp2;
  1609. sem_wait(&(UPnPLifeTime->SyncLock));
  1610. temp = UPnPLifeTime->LM;
  1611. while(temp!=NULL)
  1612. {
  1613. temp2 = temp->Next;
  1614. if(temp->DestroyPtr!=NULL) {temp->DestroyPtr(temp->data);}
  1615. FREE(temp);
  1616. temp = temp2;
  1617. }
  1618. UPnPLifeTime->LM = NULL;
  1619. sem_post(&(UPnPLifeTime->SyncLock));
  1620. }
  1621. void ILibLifeTime_Destroy(void *LifeTimeToken)
  1622. {
  1623. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeToken;
  1624. ILibLifeTime_Flush(LifeTimeToken);
  1625. sem_destroy(&(UPnPLifeTime->SyncLock));
  1626. }
  1627. void *ILibCreateLifeTime(void *Chain)
  1628. {
  1629. struct ILibLifeTime *RetVal = (struct ILibLifeTime*)MALLOC(sizeof(struct ILibLifeTime));
  1630. RetVal->LM = NULL;
  1631. RetVal->PreSelect = &ILibLifeTime_Check;
  1632. RetVal->PostSelect = NULL;
  1633. RetVal->Destroy = &ILibLifeTime_Destroy;
  1634. RetVal->Chain = Chain;
  1635. sem_init(&(RetVal->SyncLock),0,1);
  1636. ILibAddToChain(Chain,RetVal);
  1637. return((void*)RetVal);
  1638. }
  1639. int ILibFindEntryInTable(char *Entry, char **Table)
  1640. {
  1641. int i = 0;
  1642. while(Table[i]!=NULL)
  1643. {
  1644. if(strcmp(Entry,Table[i])==0)
  1645. {
  1646. return(i);
  1647. }
  1648. ++i;
  1649. }
  1650. return(-1);
  1651. }