Cai
6 days ago 129e0ebd2ca859b3463ad2c31ae335c72bace22d
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
param(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateSet('--mode=EXIT0', '--mode=EXIT7', '--mode=SLEEP_TIMEOUT', '--mode=COPYFAIL', '--mode=SIDECAR_CONFLICT')]
    [string]$Mode
)
 
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
 
function Get-AnaSemiArgvFingerprint {
    param([string[]]$LogicalArgv)
    $domain = [Text.Encoding]::UTF8.GetBytes('ANA-SEMI-ARGV-V001')
    $stream = New-Object IO.MemoryStream
    try {
        $stream.Write($domain, 0, $domain.Length)
        foreach ($token in $LogicalArgv) {
            $stream.WriteByte(0)
            $bytes = [Text.Encoding]::UTF8.GetBytes($token)
            $stream.Write($bytes, 0, $bytes.Length)
        }
        $sha = [Security.Cryptography.SHA256]::Create()
        try {
            return ([BitConverter]::ToString($sha.ComputeHash($stream.ToArray())).Replace('-', '').ToLowerInvariant())
        }
        finally {
            $sha.Dispose()
        }
    }
    finally {
        $stream.Dispose()
    }
}
 
function Write-AnaSemiNativeBytes {
    param(
        [IO.Stream]$Stream,
        [byte[]]$Bytes
    )
    $Stream.Write($Bytes, 0, $Bytes.Length)
    $Stream.Flush()
}
 
$actualArgv = [Environment]::GetCommandLineArgs()
$fingerprint = Get-AnaSemiArgvFingerprint -LogicalArgv $actualArgv
$echo = [Text.Encoding]::UTF8.GetBytes(('ARGV_ECHO_V001' + [char]9 + $actualArgv.Length + [char]9 + $fingerprint + [char]10))
$stdout = [Console]::OpenStandardOutput()
$stderr = [Console]::OpenStandardError()
try {
    Write-AnaSemiNativeBytes -Stream $stdout -Bytes $echo
    switch -CaseSensitive ($Mode) {
        '--mode=EXIT0' {
            Write-AnaSemiNativeBytes -Stream $stdout -Bytes ([Text.Encoding]::UTF8.GetBytes("OUT0`n"))
            exit 0
        }
        '--mode=EXIT7' {
            Write-AnaSemiNativeBytes -Stream $stdout -Bytes ([Text.Encoding]::UTF8.GetBytes("OUT7`n"))
            Write-AnaSemiNativeBytes -Stream $stderr -Bytes ([Text.Encoding]::UTF8.GetBytes("ERR7`n"))
            exit 7
        }
        '--mode=SLEEP_TIMEOUT' {
            Write-AnaSemiNativeBytes -Stream $stdout -Bytes ([Text.Encoding]::UTF8.GetBytes("SLEEP`n"))
            [Threading.Thread]::Sleep(60000)
            exit 0
        }
        '--mode=COPYFAIL' {
            Write-AnaSemiNativeBytes -Stream $stdout -Bytes ([Text.Encoding]::UTF8.GetBytes("COPYFAIL`n"))
            exit 0
        }
        '--mode=SIDECAR_CONFLICT' {
            Write-AnaSemiNativeBytes -Stream $stdout -Bytes ([Text.Encoding]::UTF8.GetBytes("OUT0`n"))
            exit 0
        }
    }
}
finally {
    $stdout.Dispose()
    $stderr.Dispose()
}