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.

508 lines
10 KiB

  1. /*++
  2. Copyright (c) 1999 Intel Corporation
  3. Module Name:
  4. libMisc.c
  5. Abstract:
  6. Defines various routines for Line handling
  7. --*/
  8. #ifndef _LIB_MISC
  9. #define _LIB_MISC
  10. #include "libMisc.h"
  11. VOID
  12. EditorError (
  13. IN EFI_STATUS Status,
  14. IN CHAR16 *Msg
  15. )
  16. {
  17. CHAR16 *Str;
  18. CHAR16 *Error;
  19. Error = AllocatePool(255);
  20. StatusToString(Error,Status);
  21. Str = PoolPrint(L"%s: %s",Msg,Error);
  22. MainEditor.StatusBar->SetStatusString(Str);
  23. FreePool(Str);
  24. FreePool(Error);
  25. }
  26. EE_LINE*
  27. LineDup (
  28. IN EE_LINE *Src
  29. )
  30. {
  31. EE_LINE *Dest;
  32. UINTN i;
  33. Dest = AllocatePool(sizeof(EE_LINE));
  34. Dest->Signature = EE_LINE_LIST;
  35. Dest->Size = Src->Size;
  36. for (i = 0; i < 0x10; i++) {
  37. Dest->Buffer[i] = Src->Buffer[i];
  38. }
  39. Dest->Link = Src->Link;
  40. return Dest;
  41. }
  42. VOID
  43. LineSplit (
  44. IN EE_LINE *Src,
  45. IN UINTN Pos,
  46. OUT EE_LINE *Dest
  47. )
  48. {
  49. UINTN i;
  50. Dest->Size = Src->Size - Pos;
  51. for (i = 0; (i+Pos) < Src->Size; i++) {
  52. Dest->Buffer[i] = Src->Buffer[Pos+i];
  53. }
  54. }
  55. VOID
  56. LineMerge (
  57. IN OUT EE_LINE* Line1,
  58. IN UINTN Line1Pos,
  59. IN EE_LINE* Line2,
  60. IN UINTN Line2Pos
  61. )
  62. {
  63. UINTN Size;
  64. UINTN i;
  65. Size = Line1Pos + Line2->Size - Line2Pos;
  66. Line1->Size = Size;
  67. for (i = 0; i + Line2Pos < 0x10; i++) {
  68. Line1->Buffer[Line1Pos+i] = Line2->Buffer[Line2Pos+i];
  69. }
  70. }
  71. EE_LINE*
  72. LineFirst (
  73. VOID
  74. )
  75. {
  76. MainEditor.FileBuffer->CurrentLine = MainEditor.BufferImage->ListHead->Flink;
  77. return LineCurrent();
  78. }
  79. EE_LINE*
  80. LineLast (
  81. VOID
  82. )
  83. {
  84. MainEditor.FileBuffer->CurrentLine = MainEditor.BufferImage->ListHead->Blink;
  85. return LineCurrent();
  86. }
  87. EE_LINE*
  88. LineNext (
  89. VOID
  90. )
  91. {
  92. MainEditor.FileBuffer->CurrentLine = MainEditor.FileBuffer->CurrentLine->Flink;
  93. return LineCurrent();
  94. }
  95. EE_LINE*
  96. LinePrevious (
  97. VOID
  98. )
  99. {
  100. MainEditor.FileBuffer->CurrentLine = MainEditor.FileBuffer->CurrentLine->Blink;
  101. return LineCurrent();
  102. }
  103. EE_LINE*
  104. LineAdvance (
  105. IN UINTN Count
  106. )
  107. {
  108. UINTN i;
  109. for (i = 0; i < Count && (MainEditor.FileBuffer->CurrentLine->Flink != MainEditor.BufferImage->ListHead); i++ ) {
  110. MainEditor.FileBuffer->CurrentLine = MainEditor.FileBuffer->CurrentLine->Flink;
  111. }
  112. return LineCurrent();
  113. }
  114. EE_LINE*
  115. LineRetreat (
  116. IN UINTN Count
  117. )
  118. {
  119. UINTN i;
  120. for (i = 0; i < Count && (MainEditor.FileBuffer->CurrentLine->Blink != MainEditor.BufferImage->ListHead); i++ ) {
  121. MainEditor.FileBuffer->CurrentLine = MainEditor.FileBuffer->CurrentLine->Blink;
  122. }
  123. return LineCurrent();
  124. }
  125. EE_LINE*
  126. LineCurrent (VOID)
  127. {
  128. return CR(MainEditor.FileBuffer->CurrentLine,EE_LINE,Link,EE_LINE_LIST);
  129. }
  130. VOID
  131. LineDeleteAt (
  132. LIST_ENTRY* Link,
  133. UINTN Pos,
  134. UINTN Num
  135. )
  136. {
  137. EE_LINE *Line;
  138. EE_LINE *Next;
  139. UINTN LinePos;
  140. UINTN NextPos;
  141. LIST_ENTRY *NextLink;
  142. LIST_ENTRY *Blank;
  143. NextLink = Link;
  144. MainEditor.BufferImage->NumBytes -= Num;
  145. Num += Pos;
  146. while (Num > 0x10) {
  147. NextLink = NextLink->Flink;
  148. Num -= 0x10;
  149. }
  150. Blank = MainEditor.BufferImage->ListHead->Blink;
  151. Line = CR(Link,EE_LINE,Link,EE_LINE_LIST);
  152. Next = CR(NextLink,EE_LINE,Link,EE_LINE_LIST);
  153. NextPos = Num;
  154. LinePos = Pos;
  155. while (NextLink != Blank) {
  156. while (LinePos < Line->Size && NextPos < Next->Size) {
  157. Line->Buffer[LinePos] = Next->Buffer[NextPos];
  158. ++LinePos;
  159. ++NextPos;
  160. }
  161. if (NextPos == Next->Size) {
  162. NextLink = NextLink->Flink;
  163. Next = CR(NextLink,EE_LINE,Link,EE_LINE_LIST);
  164. NextPos = 0;
  165. }
  166. if (LinePos == Line->Size) {
  167. Link = Link->Flink;
  168. Line = CR(Link,EE_LINE,Link,EE_LINE_LIST);
  169. LinePos = 0;
  170. }
  171. }
  172. Line->Size = LinePos;
  173. Link = Link->Flink;
  174. while (Link != Blank) {
  175. Line = CR(Link,EE_LINE,Link,EE_LINE_LIST);
  176. NextLink = Link->Flink;
  177. RemoveEntryList(Link);
  178. FreePool(Line);
  179. Link = NextLink;
  180. }
  181. }
  182. VOID
  183. LinePrint (
  184. LIST_ENTRY* Link,
  185. UINTN Offset,
  186. UINTN Row
  187. )
  188. {
  189. EE_LINE *Line;
  190. UINTN j;
  191. UINTN Pos;
  192. if (Row > DISP_MAX_ROWS) {
  193. return;
  194. }
  195. MainEditor.FileBuffer->ClearLine (Row);
  196. Line = CR(Link,EE_LINE,Link,EE_LINE_LIST);
  197. PrintAt(0,Row,L"%X",Offset);
  198. if (Line->Size == 0) {
  199. return;
  200. }
  201. for (j = 0; j < 0x08 && j < Line->Size; j++) {
  202. Pos = HEX_POSITION + (j*3);
  203. if (Line->Buffer[j] < 0x10) {
  204. PrintAt(Pos,Row,L"0");
  205. ++Pos;
  206. }
  207. PrintAt(Pos,Row,L"%x ",Line->Buffer[j]);
  208. }
  209. while (j < 0x10 && j < Line->Size) {
  210. Pos = HEX_POSITION + (j*3) + 1;
  211. if (Line->Buffer[j] < 0x10) {
  212. PrintAt(Pos,Row,L"0");
  213. ++Pos;
  214. }
  215. PrintAt(Pos,Row,L"%x ",Line->Buffer[j]);
  216. ++j;
  217. }
  218. for (j = 0; j < 0x10 && j < Line->Size; j++) {
  219. Pos = ASCII_POSITION+j;
  220. if (IsValidChar(Line->Buffer[j])) {
  221. PrintAt(Pos,Row,L"%c",(CHAR16)Line->Buffer[j]);
  222. } else {
  223. PrintAt(Pos,Row,L"%c",'.');
  224. }
  225. }
  226. }
  227. VOID
  228. DigitPrint (
  229. IN LIST_ENTRY *Link,
  230. IN UINTN Digit,
  231. IN UINTN Row
  232. )
  233. {
  234. EE_LINE *Line;
  235. UINTN Pos;
  236. Line = CR(Link,EE_LINE,Link,EE_LINE_LIST);
  237. Pos = HEX_POSITION + (Digit*3);
  238. if (Digit > 0x07) {
  239. Pos++;
  240. }
  241. if (Line->Buffer[Digit] < 0x10) {
  242. PrintAt(Pos,Row,L"0");
  243. ++Pos;
  244. }
  245. PrintAt(Pos,Row,L"%x ",Line->Buffer[Digit]);
  246. Pos = ASCII_POSITION+Digit;
  247. if (IsValidChar(Line->Buffer[Digit])) {
  248. PrintAt(Pos,Row,L"%c",(CHAR16)Line->Buffer[Digit]);
  249. } else {
  250. PrintAt(Pos,Row,L"%c",'.');
  251. }
  252. }
  253. UINTN
  254. StrStr (
  255. IN CHAR16 *Str,
  256. IN CHAR16 *Pat
  257. )
  258. {
  259. INTN *Failure;
  260. INTN i,j;
  261. INTN Lenp;
  262. INTN Lens;
  263. Lenp = StrLen(Pat);
  264. Lens = StrLen(Str);
  265. Failure = AllocatePool(Lenp*sizeof(INTN));
  266. Failure[0] = -1;
  267. for (j=1; j< Lenp; j++ ) {
  268. i = Failure[j-1];
  269. while ( (Pat[j] != Pat[i+1]) && (i >= 0)) {
  270. i = Failure[i];
  271. }
  272. if ( Pat[i] == Pat[i+1]) {
  273. Failure[j] = i+1;
  274. } else {
  275. Failure[j] = -1;
  276. }
  277. }
  278. i = 0;
  279. j = 0;
  280. while (i < Lens && j < Lenp) {
  281. if (Str[i] == Pat[j]) {
  282. i++;
  283. j++;
  284. } else if (j == 0) {
  285. i++;
  286. } else {
  287. j = Failure[j-1] + 1;
  288. }
  289. }
  290. FreePool(Failure);
  291. return ((j == Lenp) ? (i - Lenp) : -1)+1;
  292. }
  293. VOID
  294. FileModification (
  295. VOID
  296. )
  297. {
  298. if ( !MainEditor.FileModified ) {
  299. MainEditor.StatusBar->SetStatusString(L"File Modified");
  300. MainEditor.FileModified = TRUE;
  301. }
  302. }
  303. EFI_STATUS
  304. Nothing (
  305. VOID
  306. )
  307. {
  308. return EFI_SUCCESS;
  309. }
  310. STATIC
  311. VOID
  312. LineDeleteAll (
  313. IN LIST_ENTRY* Head
  314. )
  315. {
  316. EE_LINE *Line;
  317. LIST_ENTRY *Item;
  318. Item = Head->Flink;
  319. while (Item != Head->Blink) {
  320. RemoveEntryList(Item);
  321. Line = CR(Item,EE_LINE,Link,EE_LINE_LIST);
  322. FreePool (Line);
  323. Item = Head->Flink;
  324. }
  325. MainEditor.FileBuffer->CurrentLine = Head->Flink;
  326. }
  327. EE_LINE*
  328. LineCreateNode (
  329. IN LIST_ENTRY* Head
  330. )
  331. {
  332. EE_LINE *Line;
  333. UINTN i;
  334. Line = AllocatePool (sizeof(EE_LINE));
  335. if ( Line == NULL ) {
  336. MainEditor.StatusBar->SetStatusString(L"LineCreateNode: Could not allocate Node");
  337. return NULL;
  338. }
  339. Line->Signature = EE_LINE_LIST;
  340. Line->Size = 0;
  341. for (i = 0; i < 0x10; i++) {
  342. Line->Buffer[i] = 0x00;
  343. }
  344. InsertTailList(Head,&Line->Link);
  345. return Line;
  346. }
  347. VOID
  348. BufferToList (
  349. OUT LIST_ENTRY *Head,
  350. IN UINTN Size,
  351. IN VOID *Buffer
  352. )
  353. {
  354. EE_LINE *Line = NULL;
  355. UINTN i = 0;
  356. UINTN LineSize;
  357. UINT8 *UintBuffer;
  358. LIST_ENTRY *Blank;
  359. LineDeleteAll(Head);
  360. Blank = Head->Flink;
  361. RemoveEntryList (Blank);
  362. UintBuffer = Buffer;
  363. while (i < Size) {
  364. Line = LineCreateNode (Head);
  365. if (Line == NULL || Line == (EE_LINE*)BAD_POINTER) {
  366. EditorError(EFI_OUT_OF_RESOURCES,L"BufferToList: Could not allocate another line");
  367. break;
  368. }
  369. for (LineSize = 0; LineSize < 0x10 && i < Size; LineSize++) {
  370. Line->Buffer[LineSize] = UintBuffer[i];
  371. i++;
  372. }
  373. Line->Size = LineSize;
  374. }
  375. InsertTailList(Head,Blank);
  376. MainEditor.BufferImage->NumBytes = Size;
  377. MainEditor.FileBuffer->Offset = 0x00;
  378. MainEditor.StatusBar->SetOffset(0x00);
  379. MainEditor.FileBuffer->CurrentLine = MainEditor.BufferImage->ListHead->Flink;
  380. }
  381. EFI_STATUS
  382. ListToBuffer (
  383. IN LIST_ENTRY *Head,
  384. IN OUT UINTN *Size,
  385. OUT VOID **Buffer
  386. )
  387. {
  388. EE_LINE *Line;
  389. UINTN i;
  390. UINTN LineSize;
  391. UINT8 *UintBuffer;
  392. LIST_ENTRY *Link;
  393. LIST_ENTRY *Blank;
  394. i = 0;
  395. Blank = Head->Blink;
  396. RemoveEntryList(Blank);
  397. *Size = MainEditor.BufferImage->NumBytes;
  398. *Buffer = AllocatePool(*Size);
  399. if (*Buffer == NULL) {
  400. return EFI_OUT_OF_RESOURCES;
  401. }
  402. UintBuffer = *Buffer;
  403. for(Link = Head->Flink; Link != Head; Link = Link->Flink) {
  404. Line = CR(Link,EE_LINE,Link,EE_LINE_LIST);
  405. for (LineSize = 0; LineSize < Line->Size; LineSize++) {
  406. UintBuffer[i] = Line->Buffer[LineSize];
  407. ++i;
  408. }
  409. }
  410. *Size = i;
  411. InsertTailList(Head,Blank);
  412. return EFI_SUCCESS;
  413. }
  414. #endif /* _LIB_MISC */