summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c')
-rw-r--r--gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c254
1 files changed, 164 insertions, 90 deletions
diff --git a/gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c b/gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c
index bc6eefcba34..7f41c68782f 100644
--- a/gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c
+++ b/gnu/usr.bin/lynx/WWW/Library/Implementation/HTChunk.c
@@ -1,7 +1,7 @@
/* Chunk handling: Flexible arrays
-** ===============================
-**
-*/
+ * ===============================
+ *
+ */
#include <HTUtils.h>
#include <HTChunk.h>
@@ -9,9 +9,9 @@
#include <LYLeaks.h>
/*
-** Initialize a chunk with a certain allocation unit
-*/
-PUBLIC void HTChunkInit ARGS2 (HTChunk *,ch, int,grow)
+ * Initialize a chunk with a certain allocation unit
+ */
+void HTChunkInit(HTChunk *ch, int grow)
{
ch->data = 0;
ch->growby = grow;
@@ -20,21 +20,23 @@ PUBLIC void HTChunkInit ARGS2 (HTChunk *,ch, int,grow)
}
/* Create a chunk with a certain allocation unit
-** --------------
-*/
-PUBLIC HTChunk * HTChunkCreate ARGS1 (int,grow)
+ * --------------
+ */
+HTChunk *HTChunkCreate(int grow)
{
- HTChunk * ch = typecalloc(HTChunk);
+ HTChunk *ch = typecalloc(HTChunk);
+
if (ch == NULL)
outofmem(__FILE__, "creation of chunk");
- HTChunkInit (ch, grow);
+ HTChunkInit(ch, grow);
return ch;
}
-PUBLIC HTChunk * HTChunkCreateMayFail ARGS2 (int,grow, int,failok)
+HTChunk *HTChunkCreateMayFail(int grow, int failok)
{
- HTChunk * ch = typecalloc(HTChunk);
+ HTChunk *ch = typecalloc(HTChunk);
+
if (ch == NULL) {
if (!failok) {
outofmem(__FILE__, "creation of chunk");
@@ -42,65 +44,73 @@ PUBLIC HTChunk * HTChunkCreateMayFail ARGS2 (int,grow, int,failok)
return ch;
}
}
- HTChunkInit (ch, grow);
+ HTChunkInit(ch, grow);
ch->failok = failok;
return ch;
}
/* Create a chunk with a certain allocation unit and ensured size
-** --------------
-*/
-PUBLIC HTChunk * HTChunkCreate2 ARGS2 (int,grow, size_t, needed)
+ * --------------
+ */
+HTChunk *HTChunkCreate2(int grow, size_t needed)
{
- HTChunk * ch = typecalloc(HTChunk);
+ HTChunk *ch = typecalloc(HTChunk);
+
if (ch == NULL)
outofmem(__FILE__, "HTChunkCreate2");
- HTChunkInit (ch, grow);
+ HTChunkInit(ch, grow);
if (needed > 0) {
- ch->allocated = needed-1 - ((needed-1) % ch->growby)
- + ch->growby; /* Round up */
+ ch->allocated = needed - 1 - ((needed - 1) % ch->growby)
+ + ch->growby; /* Round up */
CTRACE((tfp, "HTChunkCreate2: requested %d, allocate %d\n",
- (int) needed, ch->allocated));
+ (int) needed, ch->allocated));
ch->data = typecallocn(char, ch->allocated);
+
if (!ch->data)
outofmem(__FILE__, "HTChunkCreate2 data");
}
return ch;
}
-
/* Clear a chunk of all data
-** --------------------------
-*/
-PUBLIC void HTChunkClear ARGS1 (HTChunk *,ch)
+ * --------------------------
+ */
+void HTChunkClear(HTChunk *ch)
{
FREE(ch->data);
ch->size = 0;
ch->allocated = 0;
}
-
-/* Free a chunk
-** ------------
-*/
-PUBLIC void HTChunkFree ARGS1 (HTChunk *,ch)
+/* Free a chunk (and it's chain, if any)
+ * -------------------------------------
+ */
+void HTChunkFree(HTChunk *ch)
{
- FREE(ch->data);
- FREE(ch);
-}
+ HTChunk *next;
+ do {
+ next = ch->next;
+ FREE(ch->data);
+ FREE(ch);
+ ch = next;
+ } while (ch != NULL);
+}
/* Realloc the chunk
-** -----------------
-*/
-PUBLIC BOOL HTChunkRealloc ARGS2 (HTChunk *,ch, int,growby)
+ * -----------------
+ */
+BOOL HTChunkRealloc(HTChunk *ch, int growby)
{
char *data;
+
ch->allocated = ch->allocated + growby;
- data = ch->data ? (char *)realloc(ch->data, ch->allocated)
- : typecallocn(char, ch->allocated);
+ data = (ch->data
+ ? (char *) realloc(ch->data, ch->allocated)
+ : typecallocn(char, ch->allocated));
+
if (data) {
ch->data = data;
} else if (ch->failok) {
@@ -112,13 +122,10 @@ PUBLIC BOOL HTChunkRealloc ARGS2 (HTChunk *,ch, int,growby)
return TRUE;
}
-
/* Append a character
-** ------------------
-*/
-/* Warning: the code of this function is defined as macro in SGML.c. Change
- the macro or undefine it in SGML.c when changing this function. -VH */
-PUBLIC void HTChunkPutc ARGS2 (HTChunk *,ch, char,c)
+ * ------------------
+ */
+void HTChunkPutc(HTChunk *ch, char c)
{
if (ch->size >= ch->allocated) {
if (!HTChunkRealloc(ch, ch->growby))
@@ -127,26 +134,47 @@ PUBLIC void HTChunkPutc ARGS2 (HTChunk *,ch, char,c)
ch->data[ch->size++] = c;
}
+/* like above but no realloc: extend to another chunk if necessary */
+HTChunk *HTChunkPutc2(HTChunk *ch, char c)
+{
+ if (ch->size >= ch->allocated) {
+ HTChunk *chunk = HTChunkCreateMayFail(ch->growby, ch->failok);
+
+ ch->next = chunk;
+ HTChunkPutc(chunk, c);
+ return chunk;
+ }
+ ch->data[ch->size++] = c;
+ return ch;
+}
/* Ensure a certain size
-** ---------------------
-*/
-PUBLIC void HTChunkEnsure ARGS2 (HTChunk *,ch, int,needed)
-{
- if (needed <= ch->allocated) return;
- ch->allocated = needed-1 - ((needed-1) % ch->growby)
- + ch->growby; /* Round up */
- ch->data = ch->data ? (char *)realloc(ch->data, ch->allocated)
- : typecallocn(char, ch->allocated);
+ * ---------------------
+ */
+void HTChunkEnsure(HTChunk *ch, int needed)
+{
+ if (needed <= ch->allocated)
+ return;
+ ch->allocated = needed - 1 - ((needed - 1) % ch->growby)
+ + ch->growby; /* Round up */
+ ch->data = (ch->data
+ ? (char *) realloc(ch->data, ch->allocated)
+ : typecallocn(char, ch->allocated));
+
if (ch->data == NULL)
outofmem(__FILE__, "HTChunkEnsure");
}
-PUBLIC void HTChunkPutb ARGS3 (HTChunk *,ch, CONST char *,b, int,l)
+/*
+ * Append a block of characters.
+ */
+void HTChunkPutb(HTChunk *ch, const char *b, int l)
{
- if (l <= 0) return;
+ if (l <= 0)
+ return;
if (ch->size + l > ch->allocated) {
- int growby = l - (l % ch->growby) + ch->growby; /* Round up */
+ int growby = l - (l % ch->growby) + ch->growby; /* Round up */
+
if (!HTChunkRealloc(ch, growby))
return;
}
@@ -154,32 +182,56 @@ PUBLIC void HTChunkPutb ARGS3 (HTChunk *,ch, CONST char *,b, int,l)
ch->size += l;
}
-#define PUTC(code) ch->data[ch->size++] = (char)(code)
+/* like above but no realloc: extend to another chunk if necessary */
+HTChunk *HTChunkPutb2(HTChunk *ch, const char *b, int l)
+{
+ if (l <= 0)
+ return ch;
+ if (ch->size + l > ch->allocated) {
+ HTChunk *chunk;
+ int m = ch->allocated - ch->size;
+
+ memcpy(ch->data + ch->size, b, m);
+ ch->size += m;
+
+ chunk = HTChunkCreateMayFail(ch->growby, ch->failok);
+ ch->next = chunk;
+ HTChunkPutb(chunk, b + m, l - m);
+ return chunk;
+ }
+ memcpy(ch->data + ch->size, b, l);
+ ch->size += l;
+ return ch;
+}
+
+#define PUTC(code) ch->data[ch->size++] = (char)(code)
#define PUTC2(code) ch->data[ch->size++] = (char)(0x80|(0x3f &(code)))
-PUBLIC void HTChunkPutUtf8Char ARGS2(
- HTChunk *, ch,
- UCode_t, code)
+/*
+ * Append a character encoded as UTF-8.
+ */
+void HTChunkPutUtf8Char(HTChunk *ch, UCode_t code)
{
int utflen;
if (TOASCII(code) < 128)
utflen = 1;
- else if (code < 0x800L) {
+ else if (code < 0x800L) {
utflen = 2;
- } else if (code < 0x10000L) {
+ } else if (code < 0x10000L) {
utflen = 3;
- } else if (code < 0x200000L) {
+ } else if (code < 0x200000L) {
utflen = 4;
} else if (code < 0x4000000L) {
utflen = 5;
- } else if (code<=0x7fffffffL) {
+ } else if (code <= 0x7fffffffL) {
utflen = 6;
} else
utflen = 0;
if (ch->size + utflen > ch->allocated) {
int growby = (ch->growby >= utflen) ? ch->growby : utflen;
+
if (!HTChunkRealloc(ch, growby))
return;
}
@@ -188,36 +240,36 @@ PUBLIC void HTChunkPutUtf8Char ARGS2(
case 0:
return;
case 1:
- ch->data[ch->size++] = (char)code;
+ ch->data[ch->size++] = (char) code;
return;
case 2:
- PUTC(0xc0 | (code>>6));
+ PUTC(0xc0 | (code >> 6));
break;
case 3:
- PUTC(0xe0 | (code>>12));
+ PUTC(0xe0 | (code >> 12));
break;
case 4:
- PUTC(0xf0 | (code>>18));
+ PUTC(0xf0 | (code >> 18));
break;
case 5:
- PUTC(0xf8 | (code>>24));
+ PUTC(0xf8 | (code >> 24));
break;
case 6:
- PUTC(0xfc | (code>>30));
+ PUTC(0xfc | (code >> 30));
break;
}
switch (utflen) {
case 6:
- PUTC2(code>>24);
+ PUTC2(code >> 24);
/* FALLTHRU */
case 5:
- PUTC2(code>>18);
+ PUTC2(code >> 18);
/* FALLTHRU */
case 4:
- PUTC2(code>>12);
+ PUTC2(code >> 12);
/* FALLTHRU */
case 3:
- PUTC2(code>>6);
+ PUTC2(code >> 6);
/* FALLTHRU */
case 2:
PUTC2(code);
@@ -226,25 +278,47 @@ PUBLIC void HTChunkPutUtf8Char ARGS2(
}
/* Terminate a chunk
-** -----------------
-*/
-PUBLIC void HTChunkTerminate ARGS1 (HTChunk *,ch)
+ * -----------------
+ */
+void HTChunkTerminate(HTChunk *ch)
{
- HTChunkPutc(ch, (char)0);
+ HTChunkPutc(ch, (char) 0);
}
-
/* Append a string
-** ---------------
-*/
-PUBLIC void HTChunkPuts ARGS2 (HTChunk *,ch, CONST char *,s)
-{
- CONST char * p;
- for (p = s; *p; p++) {
- if (ch->size >= ch->allocated) {
- if (!HTChunkRealloc(ch, ch->growby))
- return;
+ * ---------------
+ */
+void HTChunkPuts(HTChunk *ch, const char *s)
+{
+ const char *p;
+
+ if (s != NULL) {
+ for (p = s; *p; p++) {
+ if (ch->size >= ch->allocated) {
+ if (!HTChunkRealloc(ch, ch->growby))
+ return;
+ }
+ ch->data[ch->size++] = *p;
+ }
+ }
+}
+
+/* like above but no realloc: extend to another chunk if necessary */
+HTChunk *HTChunkPuts2(HTChunk *ch, const char *s)
+{
+ const char *p;
+
+ if (s != NULL) {
+ for (p = s; *p; p++) {
+ if (ch->size >= ch->allocated) {
+ HTChunk *chunk = HTChunkCreateMayFail(ch->growby, ch->failok);
+
+ ch->next = chunk;
+ HTChunkPuts(chunk, p);
+ return chunk;
+ }
+ ch->data[ch->size++] = *p;
}
- ch->data[ch->size++] = *p;
}
+ return ch;
}