cbor2 1.1.2

Full-featured CBOR (RFC 8949) for serde: async item I/O, canonical encoding, no_std, Value/RawValue, simple values, tags, COSE keys, well-formedness checks and diagnostics.
Documentation
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
param(
    [string]$ReleaseDir = "release",
    [string]$Target = "windows-x86_64",
    [string]$OutputName = "Cbor2CliSetup-windows-x86_64.exe"
)

$ErrorActionPreference = "Stop"

function Fail($Message) {
    Write-Error $Message
    exit 1
}

$releasePath = (Resolve-Path -LiteralPath $ReleaseDir).ProviderPath
$cborAsset = Join-Path $releasePath "cbor-$Target.exe"
$outputPath = Join-Path $releasePath $OutputName

if (!(Test-Path -LiteralPath $cborAsset -PathType Leaf)) { Fail "Missing $cborAsset" }

$staging = Join-Path $env:TEMP ("cbor2-cli-installer-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Force -Path $staging | Out-Null

try {
Copy-Item -LiteralPath $cborAsset -Destination (Join-Path $staging "cbor.exe") -Force

$setPathScript = @'
param(
    [Parameter(Mandatory=$true)]
    [string]$InstallDir
)

$ErrorActionPreference = "Stop"

function Add-PathPrefix($PathValue, $Directory) {
    $normalizedDirectory = [Environment]::ExpandEnvironmentVariables($Directory).TrimEnd("\")
    $entries = @()
    if (-not [string]::IsNullOrWhiteSpace($PathValue)) {
        foreach ($entry in ($PathValue -split ";")) {
            if ([string]::IsNullOrWhiteSpace($entry)) {
                continue
            }

            $normalizedEntry = [Environment]::ExpandEnvironmentVariables($entry).TrimEnd("\")
            if ([string]::Equals($normalizedEntry, $normalizedDirectory, [StringComparison]::OrdinalIgnoreCase)) {
                continue
            }

            $entries += $entry
        }
    }

    return (@($Directory) + $entries) -join ";"
}

function Send-EnvironmentChanged {
    try {
        if (-not ("Cbor2Cli.NativeMethods" -as [type])) {
            $signature = @"
using System;
using System.Runtime.InteropServices;

namespace Cbor2Cli {
    public static class NativeMethods {
        [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
        public static extern IntPtr SendMessageTimeout(
            IntPtr hWnd,
            UInt32 Msg,
            IntPtr wParam,
            string lParam,
            UInt32 fuFlags,
            UInt32 uTimeout,
            out IntPtr lpdwResult);
    }
}
"@
            Add-Type -TypeDefinition $signature | Out-Null
        }

        $result = [IntPtr]::Zero
        [Cbor2Cli.NativeMethods]::SendMessageTimeout(
            [IntPtr]0xffff,
            0x1a,
            [IntPtr]::Zero,
            "Environment",
            0x0002,
            5000,
            [ref]$result) | Out-Null
    } catch {
    }
}

$processPath = [Environment]::GetEnvironmentVariable("Path", "Process")
$updatedProcessPath = Add-PathPrefix $processPath $InstallDir
if ($updatedProcessPath -ne $processPath) {
    [Environment]::SetEnvironmentVariable("Path", $updatedProcessPath, "Process")
}

$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$updatedUserPath = Add-PathPrefix $userPath $InstallDir
if ($updatedUserPath -ne $userPath) {
    [Environment]::SetEnvironmentVariable("Path", $updatedUserPath, "User")
    Send-EnvironmentChanged
}
'@

Set-Content -LiteralPath (Join-Path $staging "set-user-path.ps1") -Value $setPathScript -Encoding ASCII

$removePathScript = @'
param(
    [Parameter(Mandatory=$true)]
    [string]$InstallDir
)

$ErrorActionPreference = "Stop"

function Remove-PathEntry($PathValue, $Directory) {
    $normalizedDirectory = [Environment]::ExpandEnvironmentVariables($Directory).TrimEnd("\")
    $entries = @()
    if (-not [string]::IsNullOrWhiteSpace($PathValue)) {
        foreach ($entry in ($PathValue -split ";")) {
            if ([string]::IsNullOrWhiteSpace($entry)) {
                continue
            }

            $normalizedEntry = [Environment]::ExpandEnvironmentVariables($entry).TrimEnd("\")
            if ([string]::Equals($normalizedEntry, $normalizedDirectory, [StringComparison]::OrdinalIgnoreCase)) {
                continue
            }

            $entries += $entry
        }
    }

    return $entries -join ";"
}

function Send-EnvironmentChanged {
    try {
        if (-not ("Cbor2Cli.NativeMethods" -as [type])) {
            $signature = @"
using System;
using System.Runtime.InteropServices;

namespace Cbor2Cli {
    public static class NativeMethods {
        [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
        public static extern IntPtr SendMessageTimeout(
            IntPtr hWnd,
            UInt32 Msg,
            IntPtr wParam,
            string lParam,
            UInt32 fuFlags,
            UInt32 uTimeout,
            out IntPtr lpdwResult);
    }
}
"@
            Add-Type -TypeDefinition $signature | Out-Null
        }

        $result = [IntPtr]::Zero
        [Cbor2Cli.NativeMethods]::SendMessageTimeout(
            [IntPtr]0xffff,
            0x1a,
            [IntPtr]::Zero,
            "Environment",
            0x0002,
            5000,
            [ref]$result) | Out-Null
    } catch {
    }
}

$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$updatedUserPath = Remove-PathEntry $userPath $InstallDir
if ($updatedUserPath -ne $userPath) {
    [Environment]::SetEnvironmentVariable("Path", $updatedUserPath, "User")
    Send-EnvironmentChanged
}
'@

Set-Content -LiteralPath (Join-Path $staging "remove-user-path.ps1") -Value $removePathScript -Encoding ASCII

$installScript = @'
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$ScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$InstallDir = Join-Path $env:LOCALAPPDATA "Programs\cbor2-cli"
$StartMenuDir = Join-Path ([Environment]::GetFolderPath("Programs")) "cbor2-cli"
$PowerShellExe = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe"
if (!(Test-Path -LiteralPath $PowerShellExe)) {
    $PowerShellExe = "powershell.exe"
}

function New-InstallerForm {
    $form = New-Object System.Windows.Forms.Form
    $form.Text = "cbor2-cli Setup"
    $form.StartPosition = "CenterScreen"
    $form.FormBorderStyle = "FixedDialog"
    $form.MaximizeBox = $false
    $form.MinimizeBox = $false
    $form.ClientSize = New-Object System.Drawing.Size(460, 132)

    $label = New-Object System.Windows.Forms.Label
    $label.AutoSize = $false
    $label.Left = 18
    $label.Top = 18
    $label.Width = 424
    $label.Height = 42
    $label.Text = "Preparing cbor2-cli..."

    $progress = New-Object System.Windows.Forms.ProgressBar
    $progress.Left = 18
    $progress.Top = 72
    $progress.Width = 424
    $progress.Height = 22
    $progress.Minimum = 0
    $progress.Maximum = 100
    $progress.Style = "Continuous"

    $form.Controls.Add($label)
    $form.Controls.Add($progress)
    $form.Show()
    [System.Windows.Forms.Application]::DoEvents()

    return @{
        Form = $form
        Label = $label
        Progress = $progress
    }
}

function Set-InstallProgress($Value, $Message) {
    $value = [Math]::Max(0, [Math]::Min(100, [int]$Value))
    $script:InstallUi.Progress.Value = $value
    $script:InstallUi.Label.Text = $Message
    [System.Windows.Forms.Application]::DoEvents()
}

function Quote-ProcessArgument($Value) {
    $text = [string]$Value
    if ($text.Length -eq 0) {
        return '""'
    }
    if ($text -notmatch '[\s"]') {
        return $text
    }

    $quoted = '"'
    $backslashes = 0
    foreach ($ch in $text.ToCharArray()) {
        if ($ch -eq '\') {
            $backslashes += 1
            continue
        }
        if ($ch -eq '"') {
            $quoted += ('\' * ($backslashes * 2 + 1))
            $quoted += '"'
            $backslashes = 0
            continue
        }
        if ($backslashes -gt 0) {
            $quoted += ('\' * $backslashes)
            $backslashes = 0
        }
        $quoted += $ch
    }
    if ($backslashes -gt 0) {
        $quoted += ('\' * ($backslashes * 2))
    }
    $quoted += '"'
    return $quoted
}

function Start-HiddenProcess($FilePath, [string[]]$ArgumentList = @(), [switch]$IgnoreExitCode) {
    $psi = New-Object System.Diagnostics.ProcessStartInfo
    $psi.FileName = $FilePath
    $psi.Arguments = ($ArgumentList | ForEach-Object { Quote-ProcessArgument $_ }) -join " "
    $psi.UseShellExecute = $false
    $psi.CreateNoWindow = $true
    $psi.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

    $process = [System.Diagnostics.Process]::Start($psi)
    $process.WaitForExit()
    if (!$IgnoreExitCode -and $process.ExitCode -ne 0) {
        throw "$FilePath failed with exit code $($process.ExitCode)."
    }
}

function Create-Shortcut($Path, $TargetPath, $WorkingDirectory, $Arguments = "") {
    $shell = New-Object -ComObject WScript.Shell
    $shortcut = $shell.CreateShortcut($Path)
    $shortcut.TargetPath = $TargetPath
    $shortcut.Arguments = $Arguments
    $shortcut.WorkingDirectory = $WorkingDirectory
    $shortcut.WindowStyle = 1
    $shortcut.Save()
}

function Write-Uninstaller {
    $uninstall = Join-Path $InstallDir "uninstall.cmd"
    $lines = @(
        '@echo off',
        'setlocal EnableExtensions',
        'set "INSTALL_DIR=%LOCALAPPDATA%\Programs\cbor2-cli"',
        'set "START_MENU_DIR=%APPDATA%\Microsoft\Windows\Start Menu\Programs\cbor2-cli"',
        'set "POWERSHELL=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"',
        'if not exist "%POWERSHELL%" set "POWERSHELL=powershell.exe"',
        'if exist "%INSTALL_DIR%\remove-user-path.ps1" "%POWERSHELL%" -NoProfile -ExecutionPolicy Bypass -File "%INSTALL_DIR%\remove-user-path.ps1" -InstallDir "%INSTALL_DIR%"',
        'if exist "%START_MENU_DIR%" rmdir /S /Q "%START_MENU_DIR%"',
        'cd /D "%TEMP%"',
        'rmdir /S /Q "%INSTALL_DIR%"',
        'echo cbor2-cli has been uninstalled.',
        'pause'
    )
    Set-Content -Path $uninstall -Value $lines -Encoding ASCII
    return $uninstall
}

function Create-Shortcuts {
    New-Item -ItemType Directory -Force -Path $StartMenuDir | Out-Null
    $uninstall = Write-Uninstaller
    Create-Shortcut (Join-Path $StartMenuDir "Uninstall cbor2-cli.lnk") $uninstall $InstallDir
}

$script:InstallUi = New-InstallerForm

try {
    Set-InstallProgress 10 "Preparing folders..."
    New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null

    Set-InstallProgress 35 "Installing cbor.exe..."
    Copy-Item -Force -LiteralPath (Join-Path $ScriptRoot "cbor.exe") -Destination (Join-Path $InstallDir "cbor.exe")
    Copy-Item -Force -LiteralPath (Join-Path $ScriptRoot "remove-user-path.ps1") -Destination (Join-Path $InstallDir "remove-user-path.ps1")

    Set-InstallProgress 65 "Updating PATH..."
    Start-HiddenProcess $PowerShellExe @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", (Join-Path $ScriptRoot "set-user-path.ps1"), "-InstallDir", $InstallDir)

    Set-InstallProgress 85 "Creating uninstall shortcut..."
    Create-Shortcuts

    Set-InstallProgress 100 "cbor2-cli has been installed."
    [System.Windows.Forms.MessageBox]::Show("cbor2-cli has been installed. Open a new terminal and run 'cbor --help'.", "cbor2-cli Setup", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
    $script:InstallUi.Form.Close()
    exit 0
} catch {
    Set-InstallProgress 100 "Installation failed."
    [System.Windows.Forms.MessageBox]::Show($_.Exception.Message, "cbor2-cli Setup", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
    $script:InstallUi.Form.Close()
    exit 1
}
'@

Set-Content -LiteralPath (Join-Path $staging "install.ps1") -Value $installScript -Encoding ASCII

$sedPath = Join-Path $staging "cbor2-cli.sed"
$sed = @"
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=0
HideExtractAnimation=0
UseLongFileName=1
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=
DisplayLicense=
FinishMessage=
TargetName=$outputPath
FriendlyName=cbor2-cli Installer
AppLaunched=powershell.exe -NoProfile -STA -ExecutionPolicy Bypass -WindowStyle Hidden -File install.ps1
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
SourceFiles=SourceFiles
[Strings]
FILE0=cbor.exe
FILE1=install.ps1
FILE2=set-user-path.ps1
FILE3=remove-user-path.ps1
[SourceFiles]
SourceFiles0=$staging
[SourceFiles0]
%FILE0%=
%FILE1%=
%FILE2%=
%FILE3%=
"@

Set-Content -LiteralPath $sedPath -Value $sed -Encoding ASCII
Remove-Item -LiteralPath $outputPath -Force -ErrorAction SilentlyContinue

$iexpress = Join-Path $env:WINDIR "System32\iexpress.exe"
if (!(Test-Path -LiteralPath $iexpress -PathType Leaf)) { Fail "iexpress.exe not found" }

$process = Start-Process -FilePath $iexpress -ArgumentList @("/N", "/Q", $sedPath) -Wait -PassThru
$exitCode = $process.ExitCode
if ($null -ne $exitCode -and $exitCode -ne 0) {
    Fail "iexpress.exe failed with exit code $exitCode"
}

for ($i = 0; $i -lt 10 -and !(Test-Path -LiteralPath $outputPath -PathType Leaf); $i++) {
    Start-Sleep -Milliseconds 500
}

if (!(Test-Path -LiteralPath $outputPath -PathType Leaf)) { Fail "Installer was not created: $outputPath" }

Write-Host "Created $outputPath"
} finally {
    Remove-Item -LiteralPath $staging -Recurse -Force -ErrorAction SilentlyContinue
}