Word Unperfect
public
Read
Owner: themaster
Branch: main
Commits: 0
Git CLI clone URL
git clone https://www.xt-emporium.com/git/word-unperfect.git
Fullscreen desktop URL
Code
Commits
History
Branches
Bug Reports
Discussions
Compare
Settings
word-unperfect
/
rev
/
wp_compact_text.c
File editor
#include "wp_compact_text.h" #include <string.h> static uint16_t wp_compact_le16(const uint8_t *p) { return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); } void wp_compact_stats_clear(WpCompactStats *stats) { if (stats != NULL) { memset(stats, 0, sizeof(*stats)); } } static bool wp_compact_write_token(uint16_t *out, size_t out_capacity, size_t *count, uint16_t value, WpCompactStats *stats) { if (count == NULL) { return false; } if (*count >= out_capacity || out == NULL) { if (stats != NULL) { stats->output_truncated = true; } return false; } out[(*count)++] = value; if (stats != NULL) { stats->tokens_written++; } return true; } bool wp_compact_decode_token_stream(const uint8_t *src, size_t src_len, uint16_t *out, size_t out_capacity, size_t *out_count, WpCompactStats *stats) { size_t pos = 0; size_t count = 0; bool ok = true; if (out_count != NULL) { *out_count = 0; } wp_compact_stats_clear(stats); if (src == NULL || out_count == NULL || (out_capacity > 0U && out == NULL)) { if (stats != NULL) { stats->input_truncated = true; } return false; } while (pos < src_len) { uint8_t b = src[pos]; uint16_t token; if (b == 0U) { pos++; if (stats != NULL) { stats->hit_terminator = true; } /* The original stores a trailing zero word after stopping. Keep * that sentinel outside the caller-visible token count. */ if (out != NULL && count < out_capacity) { out[count] = 0; } else if (out_capacity == 0U || out == NULL) { if (stats != NULL) { stats->output_truncated = true; } ok = false; } break; } if (b == 0xC0U) { if (pos + 3U >= src_len) { pos = src_len; if (stats != NULL) { stats->input_truncated = true; } ok = false; break; } token = wp_compact_le16(src + pos + 1U); pos += 4U; if (stats != NULL) { stats->c0_word_tokens++; } } else { pos++; if (b == 0xFFU) { token = 0x007EU; if (stats != NULL) { stats->ff_tilde_escapes++; } } else if (b == 0x7EU) { token = 0xFC3AU; if (stats != NULL) { stats->tilde_markers++; } } else { token = b; } } if (!wp_compact_write_token(out, out_capacity, &count, token, stats)) { ok = false; break; } } if (pos >= src_len && (stats == NULL || !stats->hit_terminator) && ok) { if (stats != NULL) { stats->input_truncated = true; } ok = false; } if (stats != NULL) { stats->input_bytes_consumed = pos; } *out_count = count; return ok && stats != NULL ? (!stats->input_truncated && !stats->output_truncated) : ok; } static bool wp_compact_write_byte(uint8_t *out, size_t out_capacity, size_t *count, uint8_t value, WpCompactStats *stats) { if (count == NULL) { return false; } if (*count >= out_capacity || out == NULL) { if (stats != NULL) { stats->output_truncated = true; } return false; } out[(*count)++] = value; if (stats != NULL) { stats->bytes_written++; } return true; } bool wp_compact_expand_c0_escape_tokens(const uint8_t *src, size_t src_len, uint8_t *out, size_t out_capacity, size_t *out_len, WpCompactStats *stats) { size_t pos = 0; size_t count = 0; bool ok = true; if (out_len != NULL) { *out_len = 0; } wp_compact_stats_clear(stats); if (src == NULL || out_len == NULL || (out_capacity > 0U && out == NULL)) { if (stats != NULL) { stats->input_truncated = true; } return false; } while (pos < src_len) { uint8_t b = src[pos]; if (b == 0xC0U) { if (pos + 3U >= src_len) { pos = src_len; if (stats != NULL) { stats->input_truncated = true; } ok = false; break; } if (out == NULL || count + 4U > out_capacity) { if (stats != NULL) { stats->output_truncated = true; } ok = false; break; } out[count++] = 0xC0U; out[count++] = src[pos + 1U]; out[count++] = src[pos + 2U]; out[count++] = 0xC0U; if (stats != NULL) { stats->bytes_written += 4U; } pos += 4U; if (stats != NULL) { stats->c0_word_tokens++; } continue; } if (!wp_compact_write_byte(out, out_capacity, &count, b, stats)) { ok = false; break; } pos++; if (b == 0U) { if (stats != NULL) { stats->hit_terminator = true; } break; } } if (pos >= src_len && (stats == NULL || !stats->hit_terminator) && ok) { if (stats != NULL) { stats->input_truncated = true; } ok = false; } if (stats != NULL) { stats->input_bytes_consumed = pos; } *out_len = count; return ok && stats != NULL ? (!stats->input_truncated && !stats->output_truncated) : ok; }
Commit message
This repository is read-only for this account.
Repository snapshot
Current branch
main
Visibility
public
Your access
Read
Remote
None
File activity
View file history