Make redbean serialization deterministic

This commit is contained in:
Justine Tunney 2022-07-09 04:09:51 -07:00
parent 85aecbda67
commit c9e68b0ebc
15 changed files with 452 additions and 150 deletions

View file

@ -17,25 +17,32 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/log/rop.h"
#include "libc/stdio/append.internal.h"
#include "libc/str/str.h"
#include "third_party/lua/cosmo.h"
#include "third_party/lua/lua.h"
void EscapeLuaString(char *s, size_t len, char **buf) {
appendw(buf, '"');
for (size_t i = 0; i < len; i++) {
int EscapeLuaString(char *s, size_t len, char **buf) {
int rc;
size_t i;
RETURN_ON_ERROR(appendw(buf, '"'));
for (i = 0; i < len; i++) {
if (' ' <= s[i] && s[i] <= 0x7e) {
appendw(buf, s[i]);
RETURN_ON_ERROR(appendw(buf, s[i]));
} else if (s[i] == '\n') {
appendw(buf, '\\' | 'n' << 8);
RETURN_ON_ERROR(appendw(buf, '\\' | 'n' << 8));
} else if (s[i] == '\\' || s[i] == '\'' || s[i] == '\"') {
appendw(buf, '\\' | s[i] << 8);
RETURN_ON_ERROR(appendw(buf, '\\' | s[i] << 8));
} else {
appendw(buf, '\\' | 'x' << 010 |
"0123456789abcdef"[(s[i] & 0xF0) >> 4] << 020 |
"0123456789abcdef"[(s[i] & 0x0F) >> 0] << 030);
RETURN_ON_ERROR(
appendw(buf, '\\' | 'x' << 010 |
"0123456789abcdef"[(s[i] & 0xF0) >> 4] << 020 |
"0123456789abcdef"[(s[i] & 0x0F) >> 0] << 030));
}
}
appendw(buf, '"');
RETURN_ON_ERROR(appendw(buf, '"'));
return 0;
OnError:
return -1;
}