Skip to content

Commit

Permalink
Simplify new function
Browse files Browse the repository at this point in the history
Skip ]] more explicitly and put the string to append into a char array.
  • Loading branch information
Vogtinator committed Sep 24, 2024
1 parent cf57893 commit a9924a9
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions luna.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,25 @@ void *escape_unicode(char *in_buf, size_t header_size, size_t footer_size, size_

/* sub-routine of xml_compress() to fix occurrences of CDATA end sequence "]]>" in Lua scripts
* by spitting them between two CDATA sections. Returns the new in_buf, or NULL if out of memory. */
static const char cdata_restart[] = "]]><![CDATA[";
void *fix_cdata_end_seq(char *in_buf, size_t header_size, size_t in_size, size_t *obuf_size) {
for (size_t offset = header_size; offset < header_size + in_size - 2; offset++) {
if (!memcmp(in_buf + offset, "]]>", 3)) {
*obuf_size += 12; // ]]><![CDATA[
char *tmp_in_buf;
if (!(tmp_in_buf = realloc(in_buf, *obuf_size))) {
// Skip "]]"
offset += 2;
// Insert "]]><![CDATA["
*obuf_size += sizeof(cdata_restart) - 1;
char *new_in_buf;
if (!(new_in_buf = realloc(in_buf, *obuf_size))) {
puts("can't realloc in_buf");
free(in_buf);
return NULL;
}
in_buf = tmp_in_buf;
memmove(in_buf + offset + 14, in_buf + offset + 2, header_size + in_size - (offset + 2));
memcpy(in_buf + offset + 2, "]]><![CDATA[", 12);
in_size += 12;
offset += 14;
in_buf = new_in_buf;
memmove(in_buf + offset + sizeof(cdata_restart) - 1, in_buf + offset, header_size + in_size - offset);
memcpy(in_buf + offset, cdata_restart, sizeof(cdata_restart) - 1);
in_size += sizeof(cdata_restart) - 1;
offset += sizeof(cdata_restart) - 1;
}
}
return in_buf;
Expand Down

0 comments on commit a9924a9

Please sign in to comment.