summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/nativent/ndkutils.pas
blob: 84cb8a662a37f4fb83b5b9ba0abb847eb48201f0 (plain)
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
{
    FPC Utility Functions for Native NT applications

    This file is part of the Free Pascal run time library.
    Copyright (c) 2009 by Sven Barth

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

unit NDKUtils;

{$mode objfpc}{$H+}

interface

uses
  NDK;

// Helpers for converting Pascal string types to NT's UNICODE_STRING
procedure ShortStrToNTStr(aStr: ShortString; var aNTStr: UNICODE_STRING);
procedure AnsiStrToNTStr(const aStr: String; var aNTStr: UNICODE_STRING);
procedure UnicodeStrToNtStr(const aStr: UnicodeString;
    var aNTStr: UNICODE_STRING);
procedure PCharToNTStr(aStr: PChar; aLen: Cardinal; var aNTStr: UNICODE_STRING);
procedure FreeNTStr(var aNTStr: UNICODE_STRING);

// Wraps NtDisplayString for use with Write(Ln)
procedure AssignDisplayString(var aFile: Text; aUtf8: Boolean);

implementation

uses
  SysUtils;

procedure ShortStrToNTStr(aStr: ShortString; var aNTStr: UNICODE_STRING);
var
  buf: Pointer;
  i: Integer;
begin
  aNTStr.Length := Length(aStr) * 2;
  aNTStr.buffer := GetMem(aNTStr.Length);
  buf := aNTStr.buffer;
  for i := 1 to Length(aStr) do begin
    PWord(buf)^ := Word(aStr[i]);
    buf := Pointer(PtrUInt(buf) + SizeOf(Word));
  end;
  aNTStr.MaximumLength := aNTStr.Length;
end;

procedure AnsiStrToNTStr(const aStr: String; var aNTStr: UNICODE_STRING);
var
  buf: PWideChar;
  i: Integer;
begin
  aNTStr.Length := Length(aStr) * 2;
  aNTStr.Buffer := GetMem(aNTStr.Length);
  buf := aNTStr.buffer;
  for i := 1 to Length(aStr) do begin
    buf^ := WideChar(Word(aStr[i]));
    Inc(buf);
  end;
  aNTStr.MaximumLength := aNTStr.Length;
end;

procedure UnicodeStrToNtStr(const aStr: UnicodeString;
    var aNTStr: UNICODE_STRING);
var
  buf: PWideChar;
begin
  { TODO : check why this prints garbage }
  aNTStr.Length := Length(aStr) * 2;
  aNTStr.Buffer := GetMem(aNTStr.Length);
  if Length(aStr) > 0 then
    Move(aStr[1], aNTStr.Buffer^, aNTStr.Length);
  aNTStr.MaximumLength := aNTStr.Length;
end;

procedure PCharToNTStr(aStr: PChar; aLen: Cardinal; var aNTStr: UNICODE_STRING);
var
  i: Integer;
begin
  if (aLen = 0) and (aStr <> Nil) and (aStr^ <> #0) then
    aLen := StrLen(aStr);
  aNtStr.Length := aLen * SizeOf(WideChar);
  aNtStr.MaximumLength := aNtStr.Length;
  aNtStr.Buffer := GetMem(aNtStr.Length);
  for i := 0 to aLen do
    aNtStr.Buffer[i] := aStr[i];
end;

procedure FreeNTStr(var aNTStr: UNICODE_STRING);
begin
  if aNTStr.Buffer <> Nil then
    FreeMem(aNTStr.Buffer);
  FillChar(aNTStr, SizeOf(UNICODE_STRING), 0);
end;

function DisplayStringWriteFunc(var aFile: TTextRec ): LongInt;
var
  ntstr: TNtUnicodeString;
  len: SizeUInt;
begin
  Result := 0;
  with aFile do
    if (BufPos>0) then begin
      if Boolean(UserData[1]) then begin
        { TODO : check why UTF8 prints garbage }
        {len := Utf8ToUnicode(Nil, 0, PChar(BufPtr), BufPos);
        ntstr.Length := len * 2;
        ntstr.MaximumLength := ntstr.Length;
        ntstr.Buffer := GetMem(ntstr.Length);
        Utf8ToUnicode(ntstr.Buffer, len, PChar(BufPtr), BufPos);}
        PCharToNtStr(PChar(BufPtr), BufPos, ntstr);
      end else
        PCharToNtStr(PChar(BufPtr), BufPos, ntstr);
      NtDisplayString(@ntstr);
      // FreeNTStr uses FreeMem, so we don't need an If here
      FreeNtStr(ntstr);
      BufPos := 0;
    end;
end;

function DisplayStringCloseFunc(var aFile: TTextRec): LongInt;
begin
  Result := 0;
end;


function DisplayStringOpenFunc(var aFile: TTextRec ): LongInt;
begin
  Result := 0;
end;

procedure AssignDisplayString(var aFile: Text; aUtf8: Boolean);
begin
  FillChar(aFile, SizeOf(TextRec), 0);
{ only set things that are not zero }
  TextRec(aFile).Handle := UnusedHandle;
  TextRec(aFile).mode := fmOutput;
  TextRec(aFile).BufSize := TextRecBufSize;
  TextRec(aFile).Bufptr := @TextRec(aFile).Buffer;
  TextRec(aFile).OpenFunc := @DisplayStringOpenFunc;
  case DefaultTextLineBreakStyle of
    tlbsLF:
      TextRec(aFile).LineEnd := #10;
    tlbsCRLF:
      TextRec(aFile).LineEnd := #13#10;
    tlbsCR:
      TextRec(aFile).LineEnd := #13;
  end;
  TextRec(aFile).Closefunc := @DisplayStringCloseFunc;
  TextRec(aFile).InOutFunc := @DisplayStringWriteFunc;
  TextRec(aFile).FlushFunc := @DisplayStringWriteFunc;
  TextRec(aFile).UserData[1] := Ord(aUTF8);
end;

end.