]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/Misc.c
da11789625635ff730d806e0eb17d25480dd7564
[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 = (UINTN)ABS(Count);
260 Line = _HLineRetreat (AbsCount);
261 } else {
262 Line = _HLineAdvance ((UINTN)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 = (UINTN)ABS(Count);
301 Line = _HLineRetreat (AbsCount);
302 } else {
303 Line = _HLineAdvance ((UINTN)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 ((UINTN)(Lenp * sizeof (INTN)));
403 if (Failure == NULL) {
404 return 0;
405 }
406 Failure[0] = -1;
407 for (j = 1; j < Lenp; j++) {
408 i = Failure[j - 1];
409 while ((Pat[j] != Pat[i + 1]) && (i >= 0)) {
410 i = Failure[i];
411 }
412
413 if (Pat[j] == Pat[i + 1]) {
414 Failure[j] = i + 1;
415 } else {
416 Failure[j] = -1;
417 }
418 }
419
420 i = 0;
421 j = 0;
422 while (i < Lens && j < Lenp) {
423 if (Str[i] == Pat[j]) {
424 i++;
425 j++;
426 } else if (j == 0) {
427 i++;
428 } else {
429 j = Failure[j - 1] + 1;
430 }
431 }
432
433 FreePool (Failure);
434
435 //
436 // 0: not found
437 // >=1 : found position + 1
438 //
439 return ((j == Lenp) ? (i - Lenp) : -1) + 1;
440
441 }
442
443 INT32
444 HGetTextX (
445 IN INT32 GuidX
446 )
447 {
448 INT32 Gap;
449
450 HMainEditor.MouseAccumulatorX += GuidX;
451 Gap = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
452 HMainEditor.MouseAccumulatorX = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
453 HMainEditor.MouseAccumulatorX = HMainEditor.MouseAccumulatorX / (INT32) HMainEditor.ScreenSize.Column;
454 return Gap;
455 }
456
457 INT32
458 HGetTextY (
459 IN INT32 GuidY
460 )
461 {
462 INT32 Gap;
463
464 HMainEditor.MouseAccumulatorY += GuidY;
465 Gap = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
466 HMainEditor.MouseAccumulatorY = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
467 HMainEditor.MouseAccumulatorY = HMainEditor.MouseAccumulatorY / (INT32) HMainEditor.ScreenSize.Row;
468
469 return Gap;
470 }
471
472 EFI_STATUS
473 HXtoi (
474 IN CHAR16 *Str,
475 OUT UINTN *Value
476 )
477 /*++
478 Routine Description:
479
480 convert hex string to uint
481
482 Arguments:
483
484 Str - The string
485 Value - The value
486
487 Returns:
488
489
490 --*/
491 {
492 UINT64 u;
493 CHAR16 c;
494 UINTN Size;
495
496 Size = sizeof (UINTN);
497
498 //
499 // skip leading white space
500 //
501 while (*Str && *Str == ' ') {
502 Str += 1;
503 }
504
505 if (StrLen (Str) > Size * 2) {
506 return EFI_LOAD_ERROR;
507 }
508 //
509 // convert hex digits
510 //
511 u = 0;
512 c = *Str;
513 while (c) {
514 c = *Str;
515 Str++;
516
517 if (c == 0) {
518 break;
519 }
520 //
521 // not valid char
522 //
523 if (!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9') || (c == '\0'))) {
524 return EFI_LOAD_ERROR;
525 }
526
527 if (c >= 'a' && c <= 'f') {
528 c -= 'a' - 'A';
529 }
530
531 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
532 u = LShiftU64 (u, 4) + (c - (c >= 'A' ? 'A' - 10 : '0'));
533 } else {
534 //
535 // '\0'
536 //
537 break;
538 }
539 }
540
541 *Value = (UINTN) u;
542
543 return EFI_SUCCESS;
544 }