-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCHexarc.cpp
465 lines (326 loc) · 9.41 KB
/
CHexarc.cpp
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// CHexarc.cpp
//
// CHexarc class
// Copyright (c) 2012 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define TYPE_AEON_BINARY CONSTLIT("AEON2011:binary:v1")
#define TYPE_AEON_ERROR CONSTLIT("AEON2011:hexeError:v1")
#define TYPE_AEON_IPINTEGER CONSTLIT("AEON2011:ipInteger:v1")
#define ERR_PASSWORD_MUST_BE_LONGER CONSTLIT("Passwords must be at least 6 characters.")
bool CHexarc::ConvertIPIntegerToString (const CJSONValue &Value, CString *retsValue)
// ConvertIPIntegerToString
//
// Converts an AEON CIPInteger encoded in JSON to a string buffer of raw bytes.
{
if (!strEquals(Value.GetElement(0).AsString(), TYPE_AEON_IPINTEGER))
return false;
const CString &sEncoded = Value.GetElement(1).AsString();
CMemoryReadStream Stream(sEncoded.GetASCIIZPointer(), sEncoded.GetLength());
if (Stream.Open() != NOERROR)
return false;
CBase64Decoder Decoder(&Stream);
if (Decoder.Open() != NOERROR)
return false;
// The first DWORD is a signature (which we can ignore)
DWORD dwLoad;
Decoder.Read((char *)&dwLoad, sizeof(DWORD));
// Next is the size
DWORD dwSize;
Decoder.Read((char *)&dwSize, sizeof(DWORD));
// Allocate enough room on the string
char *pDest = retsValue->GetWritePointer(dwSize);
// Read
Decoder.Read(pDest, dwSize);
// Done
return true;
}
bool CHexarc::ConvertToDigest (const CJSONValue &Value, CDigest *retDigest)
// ConvertToDigest
//
// Converts an AEON CIPInteger encoded in JSON to a CDigest object.
{
CIntegerIP Result;
if (!ConvertToIntegerIP(Value, &Result))
return false;
// Make sure we are the right size
if (Result.GetLength() != 20)
return false;
// Move to digest
retDigest->TakeHandoff(Result);
// Done
return true;
}
bool CHexarc::ConvertToIntegerIP (const CJSONValue &Value, CIntegerIP *retValue)
// ConvertToIntegerIP
//
// Converts an AEON CIPInteger encoded in JSON to a CIntegerIP object.
{
if (!strEquals(Value.GetElement(0).AsString(), TYPE_AEON_IPINTEGER))
return false;
const CString &sEncoded = Value.GetElement(1).AsString();
CMemoryReadStream Stream(sEncoded.GetASCIIZPointer(), sEncoded.GetLength());
if (Stream.Open() != NOERROR)
return false;
CBase64Decoder Decoder(&Stream);
if (Decoder.Open() != NOERROR)
return false;
// The first DWORD is a signature (which we can ignore)
DWORD dwLoad;
Decoder.Read((char *)&dwLoad, sizeof(DWORD));
// Next is the size
DWORD dwSize;
Decoder.Read((char *)&dwSize, sizeof(DWORD));
// Load the bytes
CIntegerIP Result(dwSize);
Decoder.Read((char *)Result.GetBytes(), dwSize);
// Return
retValue->TakeHandoff(Result);
// Done
return true;
}
bool CHexarc::ConvertToJSON (const CIntegerIP &Value, CJSONValue *retValue)
// ConvertToJSON
//
// Converts a CIntegerIP to an AEON CIPInteger encoded in JSON
{
// First we generate the encoded CIPInteger
CMemoryWriteStream Buffer;
if (Buffer.Create() != NOERROR)
return false;
CBase64Encoder Encoder(&Buffer);
if (Encoder.Create() != NOERROR)
return false;
// Signature
DWORD dwSave = 'IP1+';
Encoder.Write((char *)&dwSave, sizeof(DWORD));
// Size
dwSave = Value.GetLength();
Encoder.Write((char *)&dwSave, sizeof(DWORD));
// Bytes
Encoder.Write((char *)Value.GetBytes(), dwSave);
// Done
Encoder.Close();
// Create the array
CJSONValue IPInteger(CJSONValue::typeArray);
IPInteger.InsertHandoff(CJSONValue(TYPE_AEON_IPINTEGER));
IPInteger.InsertHandoff(CJSONValue(CString(Buffer.GetPointer(), Buffer.GetLength())));
// Done
retValue->TakeHandoff(IPInteger);
return true;
}
bool CHexarc::CreateCredentials (const CString &sUsername, const CString &sPassword, CJSONValue *retValue)
// CreateCredentials
//
// Creates credentials for signing in to Hexarc
{
return CHexarc::ConvertToJSON(CDigest(CBufferReadBlock(strPatternSubst(CONSTLIT("%s:HEXARC01:%s"), strToLower(sUsername), sPassword))), retValue);
}
bool CHexarc::CreateCredentials (const CString &sUsername, const CString &sPassword, CString *retsValue)
// CreateCredentials
//
// Creates credentials for signing in to Hexarc
{
CDigest PasswordHash(CBufferReadBlock(strPatternSubst("%s:HEXARC01:%s", strToLower(sUsername), sPassword)));
*retsValue = CString((char *)PasswordHash.GetBytes(), PasswordHash.GetLength());
return true;
}
CString CHexarc::GetFilenameFromFilePath (const CString &sFilePath)
// GetFilenameFromFilePath
//
// Parses a Multiverse filePath and returns the filename.
{
char *pPosStart = sFilePath.GetASCIIZPointer();
char *pPos = pPosStart + sFilePath.GetLength() - 1;
// Back up until we see the first slash.
while (pPos >= pPosStart && *pPos != '/')
pPos--;
// Found
if (*pPos == '/')
return CString(pPos + 1);
// Never found slash; return entire string
return sFilePath;
}
bool CHexarc::HasSpecialAeonChars (const CString &sValue)
// HasSpecialAeonChars
//
// Returns TRUE if the string has characters that are considered special by
// Aeon. [If so, then the string needs to be quoted.]
{
char *pPos = sValue.GetASCIIZPointer();
char *pEndPos = pPos + sValue.GetLength();
// If we start with a number, then we need quotes
if (strIsDigit(pPos))
return true;
// Check for reserved characters
while (pPos < pEndPos)
{
// We check for alpha first because it is faster (and most common)
if (strIsASCIIAlpha(pPos))
;
// Underscores and periods are OK
else if (*pPos == '_' || *pPos == '.')
;
// Otherwise...
else if (*pPos == ' ' || strIsASCIIControl(pPos) || strIsASCIISymbol(pPos))
return true;
pPos++;
}
return false;
}
bool CHexarc::IsBinary (const CJSONValue &Value, CString *retsData)
// IsBinary
//
// Returns TRUE if the JSON value is a binary value (and optionally returns
// it).
{
if (!strEquals(Value.GetElement(0).AsString(), TYPE_AEON_BINARY))
return false;
if (retsData == NULL)
return true;
const CString &sEncoded = Value.GetElement(1).AsString();
CMemoryReadStream Stream(sEncoded.GetASCIIZPointer(), sEncoded.GetLength());
if (Stream.Open() != NOERROR)
return false;
CBase64Decoder Decoder(&Stream);
if (Decoder.Open() != NOERROR)
return false;
// Read the length
DWORD dwLength;
Decoder.Read((char *)&dwLength, sizeof(DWORD));
if (dwLength == 0)
{
*retsData = NULL_STR;
return true;
}
// Read the buffer
char *pPos = retsData->GetWritePointer(dwLength);
Decoder.Read(pPos, dwLength);
// Done
return true;
}
bool CHexarc::IsError (const CJSONValue &Value, CString *retsError, CString *retsDesc)
// IsError
//
// Returns TRUE if the JSON value is an error
{
if (!strEquals(Value.GetElement(0).AsString(), TYPE_AEON_ERROR))
return false;
const CString &sEncoded = Value.GetElement(1).AsString();
CMemoryReadStream Stream(sEncoded.GetASCIIZPointer(), sEncoded.GetLength());
if (Stream.Open() != NOERROR)
return false;
CBase64Decoder Decoder(&Stream);
if (Decoder.Open() != NOERROR)
return false;
// Read the strings
if (retsError)
retsError->ReadFromStream(&Decoder);
if (retsDesc)
{
if (retsError == NULL)
{
CString sDummy;
sDummy.ReadFromStream(&Decoder);
}
retsDesc->ReadFromStream(&Decoder);
}
// Done
return true;
}
bool CHexarc::ValidatePasswordComplexity (const CString &sPassword, CString *retsResult)
// ValidatePasswordComplexity
//
// Make sure that the password is complex enough.
{
if (sPassword.GetLength() < 6)
{
if (retsResult)
*retsResult = ERR_PASSWORD_MUST_BE_LONGER;
return false;
}
return true;
}
void CHexarc::WriteAsAeon (const CJSONValue &Value, IWriteStream &Stream)
// WriteAsAeon
//
// Write in AEON format
{
int i;
switch (Value.GetType())
{
case CJSONValue::typeString:
{
// NOTE: When we convert an empty string to JSON it gets encoded
// as a JSON empty string (not as null). This means that when we
// serialize, we serialize to an empty string.
//
// At deserialize time, Hexarc will convert the empty string into
// an empty string datum (not nil).
//
// Thus if we're trying to write out an empty JSON string to
// serialized AEON format, we need to write out an empty string.
CString sValue = Value.AsString();
if (sValue.IsBlank())
Stream.Write("\"\"", 2);
else
{
bool bQuote = HasSpecialAeonChars(sValue);
if (bQuote)
Stream.Write("\"", 1);
CJSONValue::SerializeString(&Stream, strANSIToUTF8(sValue));
if (bQuote)
Stream.Write("\"", 1);
}
break;
}
case CJSONValue::typeNumber:
{
CString sValue;
double rValue = Value.AsDouble();
int iInt = (int)rValue;
if ((double)iInt == rValue)
sValue = strFromInt(iInt);
else
sValue = strFromDouble(rValue);
Stream.Write(sValue.GetASCIIZPointer(), sValue.GetLength());
break;
}
case CJSONValue::typeObject:
{
Stream.Write("{", 1);
for (i = 0; i < Value.GetCount(); i++)
{
if (i != 0)
Stream.Write(" ", 1);
// Write the key
CJSONValue Key(Value.GetKey(i));
WriteAsAeon(Key, Stream);
// Separator
Stream.Write(":", 1);
// Write the value
WriteAsAeon(Value.GetElement(i), Stream);
}
Stream.Write("}", 1);
break;
}
case CJSONValue::typeArray:
{
Stream.Write("(", 1);
for (i = 0; i < Value.GetCount(); i++)
{
if (i != 0)
Stream.Write(" ", 1);
WriteAsAeon(Value.GetElement(i), Stream);
}
Stream.Write(")", 1);
break;
}
case CJSONValue::typeTrue:
Stream.Write("true", 4);
break;
case CJSONValue::typeNull:
case CJSONValue::typeFalse:
Stream.Write("nil", 3);
break;
}
}