-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (144 loc) · 5.79 KB
/
Copy pathProgram.cs
File metadata and controls
158 lines (144 loc) · 5.79 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Text;
using System.Text.Json;
namespace QuickSheetB64;
class Program
{
static void Main()
{
string? line;
while ((line = Console.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
try
{
using var doc = JsonDocument.Parse(line);
var root = doc.RootElement;
string type = root.TryGetProperty("type", out var t) ? t.GetString() ?? "" : "";
if (type == "init")
{
var resp = new { type = "register", name = "quicksheet-b64", version = "1.0.0", prefix = "b64" };
Console.WriteLine(JsonSerializer.Serialize(resp));
Console.Out.Flush();
}
else if (type == "activate")
{
string id = root.TryGetProperty("id", out var idEl) ? idEl.GetString() ?? "" : "";
string param = "";
if (root.TryGetProperty("params", out var paramsEl) && paramsEl.ValueKind == JsonValueKind.Array)
{
var arr = paramsEl.EnumerateArray();
if (arr.MoveNext()) param = arr.Current.GetString() ?? "";
}
// Use 0-based relative coordinates — host adds anchor offset
var cells = Process(param.Trim(), 0, 0);
var response = new { type = "write", id, cells };
Console.WriteLine(JsonSerializer.Serialize(response));
Console.Out.Flush();
}
}
catch { }
}
}
static List<object> Process(string input, int r, int c)
{
var cells = new List<object>();
if (string.IsNullOrEmpty(input))
{
cells.Add(new { r, c = c + 1, v = "Usage: b64: <text> or b64: decode <base64string>" });
return cells;
}
// Check for explicit mode prefix
bool decodeMode = false;
string data = input;
if (input.StartsWith("decode ", StringComparison.OrdinalIgnoreCase) ||
input.StartsWith("dec ", StringComparison.OrdinalIgnoreCase) ||
input.StartsWith("d ", StringComparison.OrdinalIgnoreCase))
{
decodeMode = true;
int spaceIdx = input.IndexOf(' ');
data = input[(spaceIdx + 1)..].Trim();
}
else if (input.StartsWith("encode ", StringComparison.OrdinalIgnoreCase) ||
input.StartsWith("enc ", StringComparison.OrdinalIgnoreCase) ||
input.StartsWith("e ", StringComparison.OrdinalIgnoreCase))
{
decodeMode = false;
int spaceIdx = input.IndexOf(' ');
data = input[(spaceIdx + 1)..].Trim();
}
else
{
// Auto-detect: try to decode first
decodeMode = IsLikelyBase64(data);
}
try
{
if (decodeMode)
{
// Pad if needed
string padded = data;
int mod = padded.Length % 4;
if (mod > 0) padded += new string('=', 4 - mod);
byte[] bytes = Convert.FromBase64String(padded);
string decoded = Encoding.UTF8.GetString(bytes);
cells.Add(new { r, c, v = "🔓 DECODED" });
// Split on newlines for multi-line output
string[] lines = decoded.Split('\n');
for (int i = 0; i < Math.Min(lines.Length, 20); i++)
{
string line = lines[i].TrimEnd('\r');
if (line.Length > 200) line = line[..197] + "...";
cells.Add(new { r = r + i, c = c + 1, v = line });
}
cells.Add(new { r = r + Math.Min(lines.Length, 20), c = c + 1, v = $"({bytes.Length} bytes)" });
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
string encoded = Convert.ToBase64String(bytes);
cells.Add(new { r, c, v = "🔒 ENCODED" });
// Split long base64 into 76-char lines (MIME standard)
int row = 0;
for (int i = 0; i < encoded.Length; i += 76)
{
string chunk = encoded.Substring(i, Math.Min(76, encoded.Length - i));
cells.Add(new { r = r + row, c = c + 1, v = chunk });
row++;
if (row >= 20) break;
}
cells.Add(new { r = r + row, c = c + 1, v = $"({bytes.Length} bytes → {encoded.Length} chars)" });
}
}
catch (FormatException)
{
cells.Add(new { r, c = c + 1, v = "⚠ Invalid base64 input" });
}
catch (Exception ex)
{
cells.Add(new { r, c = c + 1, v = $"⚠ {ex.Message}" });
}
return cells;
}
static bool IsLikelyBase64(string s)
{
if (s.Length < 4) return false;
// Base64 chars: A-Z, a-z, 0-9, +, /, =
// If it contains spaces or non-base64 chars, it's probably plain text
foreach (char ch in s)
{
if (!char.IsLetterOrDigit(ch) && ch != '+' && ch != '/' && ch != '=' && ch != '\n' && ch != '\r')
return false;
}
// Try to decode
try
{
string padded = s;
int mod = padded.Length % 4;
if (mod > 0) padded += new string('=', 4 - mod);
Convert.FromBase64String(padded);
return true;
}
catch { return false; }
}
}