Leaked source code of windows server 2003
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.

423 lines
11 KiB

  1. /***********************************************************************
  2. * Microsoft (R) Windows (R) Resource Compiler
  3. *
  4. * Copyright (c) Microsoft Corporation. All rights reserved.
  5. *
  6. * File Comments:
  7. *
  8. *
  9. ***********************************************************************/
  10. #include "rc.h"
  11. /*---------------------------------------------------------------------------*/
  12. /* */
  13. /* MyAlloc() - */
  14. /* */
  15. /*---------------------------------------------------------------------------*/
  16. // HACK Alert. Allocate an extra longlong and return past it (to allow for PREVCH()
  17. // to store a byte before the allocation block and to maintain 8 byte alignment).
  18. void *MyAlloc(size_t nbytes)
  19. {
  20. void *pv = HeapAlloc(hHeap, HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY, nbytes + 8);
  21. if (pv == NULL) {
  22. fatal(1120, nbytes + 8);
  23. }
  24. return(((BYTE *) pv) + 8);
  25. }
  26. /*---------------------------------------------------------------------------*/
  27. /* */
  28. /* MyFree() - */
  29. /* */
  30. /*---------------------------------------------------------------------------*/
  31. void MyFree(void *pv)
  32. {
  33. if (pv != NULL) {
  34. HeapFree(hHeap, HEAP_NO_SERIALIZE, ((BYTE *) pv) - 8);
  35. }
  36. }
  37. /*---------------------------------------------------------------------------*/
  38. /* */
  39. /* MyMakeStr() - */
  40. /* */
  41. /*---------------------------------------------------------------------------*/
  42. WCHAR *
  43. MyMakeStr(
  44. const wchar_t *s
  45. )
  46. {
  47. wchar_t *s1;
  48. if (s != NULL) {
  49. s1 = (wchar_t *) MyAlloc((wcslen(s) + 1) * sizeof(wchar_t)); /* allocate buffer */
  50. wcscpy(s1, s); /* copy string */
  51. } else {
  52. s1 = NULL;
  53. }
  54. return(s1);
  55. }
  56. /*---------------------------------------------------------------------------*/
  57. /* */
  58. /* MyRead() - */
  59. /* */
  60. /*---------------------------------------------------------------------------*/
  61. size_t
  62. MyRead(
  63. FILE *fh,
  64. VOID *p,
  65. size_t n
  66. )
  67. {
  68. size_t n1;
  69. n1 = fread(p, 1, n, fh);
  70. if (ferror(fh)) {
  71. fatal(1121);
  72. }
  73. return(n1);
  74. }
  75. /*---------------------------------------------------------------------------*/
  76. /* */
  77. /* MyWrite() - */
  78. /* */
  79. /*---------------------------------------------------------------------------*/
  80. size_t
  81. MyWrite(
  82. FILE *fh,
  83. const void *p,
  84. size_t n
  85. )
  86. {
  87. size_t n1;
  88. if ((n1 = fwrite(p, 1, n, fh)) != n) {
  89. quit(L"RC : fatal error RW1022: I/O error writing file.");
  90. }
  91. return(n1);
  92. }
  93. /*---------------------------------------------------------------------------*/
  94. /* */
  95. /* MyAlign() - */
  96. /* */
  97. /*---------------------------------------------------------------------------*/
  98. UINT
  99. MyAlign(
  100. PFILE fh
  101. )
  102. {
  103. DWORD t0 = 0;
  104. DWORD ib;
  105. /* align file to dword */
  106. ib = MySeek(fh, 0, SEEK_CUR);
  107. if (ib % 4) {
  108. ib = 4 - ib % 4;
  109. MyWrite(fh, (PVOID)&t0, (UINT)ib);
  110. return(ib);
  111. }
  112. return(0);
  113. }
  114. /*---------------------------------------------------------------------------*/
  115. /* */
  116. /* MySeek() - */
  117. /* */
  118. /*---------------------------------------------------------------------------*/
  119. LONG
  120. MySeek(
  121. FILE *fh,
  122. LONG pos,
  123. int cmd
  124. )
  125. {
  126. if (fseek(fh, pos, cmd))
  127. quit(L"RC : fatal error RW1023: I/O error seeking in file");
  128. if ((pos = ftell (fh)) == -1L)
  129. quit(L"RC : fatal error RW1023: I/O error seeking in file");
  130. return(pos);
  131. }
  132. /*---------------------------------------------------------------------------*/
  133. /* */
  134. /* MyCopy() - */
  135. /* */
  136. /*---------------------------------------------------------------------------*/
  137. size_t
  138. MyCopy(
  139. FILE *srcfh,
  140. FILE *dstfh,
  141. size_t nbytes
  142. )
  143. {
  144. void *buffer = MyAlloc(BUFSIZE);
  145. size_t n = 0;
  146. while (nbytes) {
  147. if (nbytes <= BUFSIZE)
  148. n = nbytes;
  149. else
  150. n = BUFSIZE;
  151. nbytes -= n;
  152. MyRead(srcfh, buffer, n);
  153. MyWrite(dstfh, buffer, n);
  154. }
  155. MyFree(buffer);
  156. return(n);
  157. }
  158. /*---------------------------------------------------------------------------*/
  159. /* */
  160. /* MyCopyAll() - */
  161. /* */
  162. /*---------------------------------------------------------------------------*/
  163. int
  164. MyCopyAll(
  165. FILE *srcfh,
  166. PFILE dstfh
  167. )
  168. {
  169. PCHAR buffer = (PCHAR) MyAlloc(BUFSIZE);
  170. UINT n;
  171. while ((n = fread(buffer, 1, BUFSIZE, srcfh)) != 0)
  172. MyWrite(dstfh, buffer, n);
  173. MyFree(buffer);
  174. return TRUE;
  175. }
  176. /*---------------------------------------------------------------------------*/
  177. /* */
  178. /* strpre() - */
  179. /* */
  180. /*---------------------------------------------------------------------------*/
  181. /* strpre: return -1 if pch1 is a prefix of pch2, 0 otherwise.
  182. * compare is case insensitive.
  183. */
  184. int
  185. strpre(
  186. const wchar_t *pch1,
  187. const wchar_t *pch2
  188. )
  189. {
  190. while (*pch1) {
  191. if (!*pch2)
  192. return 0;
  193. else if (towupper(*pch1) == towupper(*pch2))
  194. pch1++, pch2++;
  195. else
  196. return 0;
  197. }
  198. return - 1;
  199. }
  200. /*---------------------------------------------------------------------------*/
  201. /* */
  202. /* iswhite() - */
  203. /* */
  204. /*---------------------------------------------------------------------------*/
  205. int
  206. iswhite (
  207. WCHAR c
  208. )
  209. {
  210. /* returns true for whitespace and linebreak characters */
  211. switch (c) {
  212. case L' ':
  213. case L'\t':
  214. case L'\r':
  215. case L'\n':
  216. case EOF:
  217. return(-1);
  218. break;
  219. default:
  220. return(0);
  221. break;
  222. }
  223. }
  224. /*---------------------------------------------------------------------------*/
  225. /* */
  226. /* IsSwitchChar() - */
  227. /* */
  228. /*---------------------------------------------------------------------------*/
  229. BOOL
  230. IsSwitchChar(
  231. wchar_t c
  232. )
  233. {
  234. /* true for switch characters */
  235. return (c == L'/' || c == L'-');
  236. }
  237. /*---------------------------------------------------------------------------*/
  238. /* */
  239. /* ExtractFileName(szFullName, szFileName) - */
  240. /* */
  241. /* This routine is used to extract just the file name from a string */
  242. /* that may or may not contain a full or partial path name. */
  243. /* */
  244. /*---------------------------------------------------------------------------*/
  245. void
  246. ExtractFileName(
  247. const wchar_t *szFullName,
  248. wchar_t *szFileName
  249. )
  250. {
  251. int iLen;
  252. PWCHAR pCh;
  253. iLen = wcslen(szFullName);
  254. /* Goto the last character of the full name; */
  255. pCh = (PWCHAR)(szFullName + iLen);
  256. pCh--;
  257. /* Look for '/', '\\' or ':' character */
  258. while (iLen--) {
  259. if ((*pCh == L'\\') || (*pCh == L'/') || (*pCh == L':'))
  260. break;
  261. pCh--;
  262. }
  263. wcscpy(szFileName, ++pCh);
  264. }
  265. DWORD
  266. wcsatoi(
  267. const wchar_t *s
  268. )
  269. {
  270. DWORD t = 0;
  271. while (*s) {
  272. t = 10 * t + (DWORD)((CHAR)*s - '0');
  273. s++;
  274. }
  275. return t;
  276. }
  277. WCHAR *
  278. wcsitow(
  279. LONG v,
  280. WCHAR *s,
  281. DWORD r
  282. )
  283. {
  284. DWORD cb = 0;
  285. DWORD t;
  286. DWORD tt = v;
  287. while (tt) {
  288. t = tt % r;
  289. cb++;
  290. tt /= r;
  291. }
  292. s += cb;
  293. *s-- = 0;
  294. while (v) {
  295. t = v % r;
  296. *s-- = (WCHAR)((CHAR)t + '0');
  297. v /= r;
  298. }
  299. return ++s;
  300. }
  301. // ----------------------------------------------------------------------------
  302. //
  303. // PreBeginParse
  304. //
  305. // ----------------------------------------------------------------------------
  306. VOID
  307. PreBeginParse(
  308. PRESINFO pRes,
  309. int id
  310. )
  311. {
  312. while (token.type != BEGIN) {
  313. switch (token.type) {
  314. case TKLANGUAGE:
  315. pRes->language = GetLanguage();
  316. break;
  317. case TKVERSION:
  318. GetToken(FALSE);
  319. if (token.type != NUMLIT)
  320. ParseError1(2139);
  321. pRes->version = token.longval;
  322. break;
  323. case TKCHARACTERISTICS:
  324. GetToken(FALSE);
  325. if (token.type != NUMLIT)
  326. ParseError1(2140);
  327. pRes->characteristics = token.longval;
  328. break;
  329. default:
  330. ParseError1(id);
  331. break;
  332. }
  333. GetToken(FALSE);
  334. }
  335. if (token.type != BEGIN)
  336. ParseError1(id);
  337. GetToken(TRUE);
  338. }