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