]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/Misc.c
8ae21450c4978247828a3e46b5957dca5b477b9c
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / Misc.c
1 /** @file
2 Implementation of various string and line routines
3
4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "HexEditor.h"
16
17 extern BOOLEAN HEditorMouseAction;
18
19 VOID
20 HEditorClearLine (
21 IN UINTN Row
22 )
23 /*++
24
25 Routine Description:
26
27 Clear line at Row
28
29 Arguments:
30
31 Row -- row number to be cleared ( start from 1 )
32
33 Returns:
34
35 EFI_SUCCESS
36
37 --*/
38 {
39 CHAR16 Line[200];
40 UINTN Index;
41 UINTN Limit;
42 UINTN StartCol;
43
44 if (HEditorMouseAction) {
45 Limit = 3 * 0x10;
46 StartCol = 10;
47 } else {
48 Limit = HMainEditor.ScreenSize.Column;
49 StartCol = 1;
50 }
51 //
52 // prepare a blank line
53 //
54 for (Index = 0; Index < Limit; Index++) {
55 Line[Index] = ' ';
56 }
57
58 if (Row == HMainEditor.ScreenSize.Row && Limit == HMainEditor.ScreenSize.Column) {
59 //
60 // if '\0' is still at position 80, it will cause first line error
61 //
62 Line[Limit - 1] = '\0';
63 } else {
64 Line[Limit] = '\0';
65 }
66 //
67 // print out the blank line
68 //
69 ShellPrintEx ((INT32)StartCol - 1, (INT32)Row - 1, Line);
70 }
71
72 HEFI_EDITOR_LINE *
73 HLineDup (
74 IN HEFI_EDITOR_LINE *Src
75 )
76 /*++
77
78 Routine Description:
79
80 Duplicate a line
81
82 Arguments:
83
84 Src -- line to be duplicated
85
86 Returns:
87
88 NULL -- wrong
89 Not NULL -- line created
90
91 --*/
92 {
93 HEFI_EDITOR_LINE *Dest;
94
95 //
96 // allocate for the line structure
97 //
98 Dest = AllocateZeroPool (sizeof (HEFI_EDITOR_LINE));
99 if (Dest == NULL) {
100 return NULL;
101 }
102
103 Dest->Signature = EFI_EDITOR_LINE_LIST;
104 Dest->Size = Src->Size;
105
106 CopyMem (Dest->Buffer, Src->Buffer, 0x10);
107
108 Dest->Link = Src->Link;
109
110 return Dest;
111 }
112
113 VOID
114 HLineFree (
115 IN HEFI_EDITOR_LINE *Src
116 )
117 /*++
118
119 Routine Description:
120
121 Free a line and it's internal buffer
122
123 Arguments:
124
125 Src -- line to be freed
126
127 Returns:
128
129 None
130
131 --*/
132 {
133 if (Src == NULL) {
134 return ;
135 }
136
137 SHELL_FREE_NON_NULL (Src);
138
139 }
140
141 HEFI_EDITOR_LINE *
142 _HLineAdvance (
143 IN UINTN Count
144 )
145 /*++
146
147 Routine Description:
148
149 Advance to the next Count lines
150
151 Arguments:
152
153 Count -- line number to advance
154
155 Returns:
156
157 NULL -- wrong
158 Not NULL -- line after advance
159
160 --*/
161 {
162 UINTN Index;
163 HEFI_EDITOR_LINE *Line;
164
165 Line = HMainEditor.BufferImage->CurrentLine;
166 if (Line == NULL) {
167 return NULL;
168 }
169
170 for (Index = 0; Index < Count; Index++) {
171 //
172 // if already last line
173 //
174 if (Line->Link.ForwardLink == HMainEditor.BufferImage->ListHead) {
175 return NULL;
176 }
177
178 Line = CR (Line->Link.ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
179 }
180
181 return Line;
182 }
183
184 HEFI_EDITOR_LINE *
185 _HLineRetreat (
186 IN UINTN Count
187 )
188 /*++
189
190 Routine Description:
191
192 Retreat to the previous Count lines
193
194 Arguments:
195
196 Count -- line number to retreat
197
198 Returns:
199
200 NULL -- wrong
201 Not NULL -- line after retreat
202
203 --*/
204 {
205 UINTN Index;
206 HEFI_EDITOR_LINE *Line;
207
208 Line = HMainEditor.BufferImage->CurrentLine;
209 if (Line == NULL) {
210 return NULL;
211 }
212
213 for (Index = 0; Index < Count; Index++) {
214 //
215 // already the first line
216 //
217 if (Line->Link.BackLink == HMainEditor.BufferImage->ListHead) {
218 return NULL;
219 }
220
221 Line = CR (Line->Link.BackLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
222 }
223
224 return Line;
225 }
226
227 HEFI_EDITOR_LINE *
228 HMoveLine (
229 IN INTN Count
230 )
231 /*++
232
233 Routine Description:
234
235 Advance/Retreat lines
236
237 Arguments:
238
239 Count -- line number to advance/retreat
240 >0 : advance
241 <0: retreat
242
243 Returns:
244
245 NULL -- wrong
246 Not NULL -- line after advance
247
248 --*/
249 {
250 HEFI_EDITOR_LINE *Line;
251 UINTN AbsCount;
252
253 //
254 // difference with MoveCurrentLine
255 // just return Line
256 // do not set currentline to Line
257 //
258 if (Count <= 0) {
259 AbsCount = -Count;
260 Line = _HLineRetreat (AbsCount);
261 } else {
262 Line = _HLineAdvance (Count);
263 }
264
265 return Line;
266 }
267
268 HEFI_EDITOR_LINE *
269 HMoveCurrentLine (
270 IN INTN Count
271 )
272 /*++
273
274 Routine Description:
275
276 Advance/Retreat lines and set CurrentLine in BufferImage to it
277
278 Arguments:
279
280 Count -- line number to advance/retreat
281 >0 : advance
282 <0: retreat
283
284 Returns:
285
286 NULL -- wrong
287 Not NULL -- line after advance
288
289
290 --*/
291 {
292 HEFI_EDITOR_LINE *Line;
293 UINTN AbsCount;
294
295 //
296 // <0: retreat
297 // >0: advance
298 //
299 if (Count <= 0) {
300 AbsCount = -Count;
301 Line = _HLineRetreat (AbsCount);
302 } else {
303 Line = _HLineAdvance (Count);
304 }
305
306 if (Line == NULL) {
307 return NULL;
308 }
309
310 HMainEditor.BufferImage->CurrentLine = Line;
311
312 return Line;
313 }
314
315
316 EFI_STATUS
317 HFreeLines (
318 IN LIST_ENTRY *ListHead,
319 IN HEFI_EDITOR_LINE *Lines
320 )
321 /*++
322
323 Routine Description:
324
325 Free all the lines in HBufferImage
326 Fields affected:
327 Lines
328 CurrentLine
329 NumLines
330 ListHead
331
332 Arguments:
333
334 ListHead - The list head
335 Lines - The lines
336
337 Returns:
338
339 EFI_SUCCESS
340
341 --*/
342 {
343 LIST_ENTRY *Link;
344 HEFI_EDITOR_LINE *Line;
345
346 //
347 // release all the lines
348 //
349 if (Lines != NULL) {
350
351 Line = Lines;
352 Link = &(Line->Link);
353 do {
354 Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
355 Link = Link->ForwardLink;
356 HLineFree (Line);
357 } while (Link != ListHead);
358 }
359
360 ListHead->ForwardLink = ListHead;
361 ListHead->BackLink = ListHead;
362
363 return EFI_SUCCESS;
364 }
365
366 UINTN
367 HStrStr (
368 IN CHAR16 *Str,
369 IN CHAR16 *Pat
370 )
371 /*++
372
373 Routine Description:
374
375 Search Pat in Str
376
377 Arguments:
378
379 Str -- mother string
380 Pat -- search pattern
381
382
383 Returns:
384
385 0 : not found
386 >= 1 : found position + 1
387
388 --*/
389 {
390 INTN *Failure;
391 INTN i;
392 INTN j;
393 INTN Lenp;
394 INTN Lens;
395
396 //
397 // this function copies from some lib
398 //
399 Lenp = StrLen (Pat);
400 Lens = StrLen (Str);
401
402 Failure = AllocateZeroPool (Lenp * sizeof (INTN));
403 Failure[0] = -1;
404 for (j = 1; j < Lenp; j++) {
405 i = Failure[j - 1];
406 while ((Pat[j] != Pat[i + 1]) && (i >= 0)) {
407 i = Failure[i];
408 }
409
410 if (Pat[j] == Pat[i + 1]) {
411 Failure[j] = i + 1;
412 } else {
413 Failure[j] = -1;
414 }
415 }
416
417 i = 0;
418 j = 0;
419 while (i < Lens && j < Lenp) {
420 if (Str[i] == Pat[j]) {
421 i++;
422 j++;
423 } else if (j == 0) {
424 i++;
425 } else {
426 j = Failure[j - 1] + 1;
427 }
428 }
429
430 FreePool (Failure);
431
432 //
433 // 0: not found
434 // >=1 : found position + 1
435 //
436 return ((j == Lenp) ? (i - Lenp) : -1) + 1;
437
438 }
439
440 INT32
441 HGetTextX (
442 IN INT32 GuidX
443 )
444 {
445 INT32 Gap;
446
447 HMainEditor.MouseAccumulatorX += GuidX;
448 Gap = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
449 HMainEditor.MouseAccumulatorX = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
450 HMainEditor.MouseAccumulatorX = HMainEditor.MouseAccumulatorX / (INT32) HMainEditor.ScreenSize.Column;
451 return Gap;
452 }
453
454 INT32
455 HGetTextY (
456 IN INT32 GuidY
457 )
458 {
459 INT32 Gap;
460
461 HMainEditor.MouseAccumulatorY += GuidY;
462 Gap = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
463 HMainEditor.MouseAccumulatorY = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
464 HMainEditor.MouseAccumulatorY = HMainEditor.MouseAccumulatorY / (INT32) HMainEditor.ScreenSize.Row;
465
466 return Gap;
467 }
468
469 EFI_STATUS
470 HXtoi (
471 IN CHAR16 *Str,
472 OUT UINTN *Value
473 )
474 /*++
475 Routine Description:
476
477 convert hex string to uint
478
479 Arguments:
480
481 Str - The string
482 Value - The value
483
484 Returns:
485
486
487 --*/
488 {
489 UINT64 u;
490 CHAR16 c;
491 UINTN Size;
492
493 Size = sizeof (UINTN);
494
495 //
496 // skip leading white space
497 //
498 while (*Str && *Str == ' ') {
499 Str += 1;
500 }
501
502 if (StrLen (Str) > Size * 2) {
503 return EFI_LOAD_ERROR;
504 }
505 //
506 // convert hex digits
507 //
508 u = 0;
509 c = *Str;
510 while (c) {
511 c = *Str;
512 Str++;
513
514 if (c == 0) {
515 break;
516 }
517 //
518 // not valid char
519 //
520 if (!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9') || (c == '\0'))) {
521 return EFI_LOAD_ERROR;
522 }
523
524 if (c >= 'a' && c <= 'f') {
525 c -= 'a' - 'A';
526 }
527
528 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
529 u = LShiftU64 (u, 4) + (c - (c >= 'A' ? 'A' - 10 : '0'));
530 } else {
531 //
532 // '\0'
533 //
534 break;
535 }
536 }
537
538 *Value = (UINTN) u;
539
540 return EFI_SUCCESS;
541 }