forked from auduchinok/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.fs
More file actions
136 lines (104 loc) · 4.27 KB
/
Copy pathbytes.fs
File metadata and controls
136 lines (104 loc) · 4.27 KB
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
/// Byte arrays
namespace FSharp.Compiler.AbstractIL.Internal
open System.IO
open Internal.Utilities
open FSharp.Compiler.AbstractIL
open FSharp.Compiler.AbstractIL.Internal
module internal Bytes =
let b0 n = (n &&& 0xFF)
let b1 n = ((n >>> 8) &&& 0xFF)
let b2 n = ((n >>> 16) &&& 0xFF)
let b3 n = ((n >>> 24) &&& 0xFF)
let dWw1 n = int32 ((n >>> 32) &&& 0xFFFFFFFFL)
let dWw0 n = int32 (n &&& 0xFFFFFFFFL)
let get (b:byte[]) n = int32 (Array.get b n)
let zeroCreate n : byte[] = Array.zeroCreate n
let sub ( b:byte[]) s l = Array.sub b s l
let blit (a:byte[]) b c d e = Array.blit a b c d e
let ofInt32Array (arr:int[]) = Array.init arr.Length (fun i -> byte arr.[i])
let stringAsUtf8NullTerminated (s:string) =
Array.append (System.Text.Encoding.UTF8.GetBytes s) (ofInt32Array [| 0x0 |])
let stringAsUnicodeNullTerminated (s:string) =
Array.append (System.Text.Encoding.Unicode.GetBytes s) (ofInt32Array [| 0x0;0x0 |])
type internal ByteStream =
{ bytes: byte[]
mutable pos: int
max: int }
member b.ReadByte() =
if b.pos >= b.max then failwith "end of stream"
let res = b.bytes.[b.pos]
b.pos <- b.pos + 1
res
member b.ReadUtf8String n =
let res = System.Text.Encoding.UTF8.GetString(b.bytes,b.pos,n)
b.pos <- b.pos + n; res
static member FromBytes (b:byte[],n,len) =
if n < 0 || (n+len) > b.Length then failwith "FromBytes"
{ bytes = b; pos = n; max = n+len }
member b.ReadBytes n =
if b.pos + n > b.max then failwith "ReadBytes: end of stream"
let res = Bytes.sub b.bytes b.pos n
b.pos <- b.pos + n
res
member b.Position = b.pos
#if LAZY_UNPICKLE
member b.CloneAndSeek = { bytes=b.bytes; pos=pos; max=b.max }
member b.Skip = b.pos <- b.pos + n
#endif
type internal ByteBuffer =
{ mutable bbArray: byte[]
mutable bbCurrent: int }
member buf.Ensure newSize =
let oldBufSize = buf.bbArray.Length
if newSize > oldBufSize then
let old = buf.bbArray
buf.bbArray <- Bytes.zeroCreate (max newSize (oldBufSize * 2))
Bytes.blit old 0 buf.bbArray 0 buf.bbCurrent
member buf.Close () = Bytes.sub buf.bbArray 0 buf.bbCurrent
member buf.EmitIntAsByte (i:int) =
let newSize = buf.bbCurrent + 1
buf.Ensure newSize
buf.bbArray.[buf.bbCurrent] <- byte i
buf.bbCurrent <- newSize
member buf.EmitByte (b:byte) = buf.EmitIntAsByte (int b)
member buf.EmitIntsAsBytes (arr:int[]) =
let n = arr.Length
let newSize = buf.bbCurrent + n
buf.Ensure newSize
let bbarr = buf.bbArray
let bbbase = buf.bbCurrent
for i = 0 to n - 1 do
bbarr.[bbbase + i] <- byte arr.[i]
buf.bbCurrent <- newSize
member bb.FixupInt32 pos n =
bb.bbArray.[pos] <- (Bytes.b0 n |> byte)
bb.bbArray.[pos + 1] <- (Bytes.b1 n |> byte)
bb.bbArray.[pos + 2] <- (Bytes.b2 n |> byte)
bb.bbArray.[pos + 3] <- (Bytes.b3 n |> byte)
member buf.EmitInt32 n =
let newSize = buf.bbCurrent + 4
buf.Ensure newSize
buf.FixupInt32 buf.bbCurrent n
buf.bbCurrent <- newSize
member buf.EmitBytes (i:byte[]) =
let n = i.Length
let newSize = buf.bbCurrent + n
buf.Ensure newSize
Bytes.blit i 0 buf.bbArray buf.bbCurrent n
buf.bbCurrent <- newSize
member buf.EmitInt32AsUInt16 n =
let newSize = buf.bbCurrent + 2
buf.Ensure newSize
buf.bbArray.[buf.bbCurrent] <- (Bytes.b0 n |> byte)
buf.bbArray.[buf.bbCurrent + 1] <- (Bytes.b1 n |> byte)
buf.bbCurrent <- newSize
member buf.EmitBoolAsByte (b:bool) = buf.EmitIntAsByte (if b then 1 else 0)
member buf.EmitUInt16 (x:uint16) = buf.EmitInt32AsUInt16 (int32 x)
member buf.EmitInt64 x =
buf.EmitInt32 (Bytes.dWw0 x)
buf.EmitInt32 (Bytes.dWw1 x)
member buf.Position = buf.bbCurrent
static member Create sz =
{ bbArray=Bytes.zeroCreate sz
bbCurrent = 0 }