This commit is contained in:
sin 2014-11-17 15:41:44 +00:00
parent 85df198b59
commit 56709a2414
6 changed files with 2279 additions and 6 deletions

View File

@ -9,14 +9,22 @@ HDR =\
fs.h\ fs.h\
md5.h\ md5.h\
queue.h\ queue.h\
runetypebody.h\
sha1.h\ sha1.h\
sha256.h\ sha256.h\
sha512.h\ sha512.h\
text.h\ text.h\
utf.h\
util.h util.h
LIB = libutil.a LIBUTF = libutf.a
LIBSRC =\ LIBUTFSRC =\
libutf/rune.c\
libutf/runetype.c\
libutf/utf.c
LIBUTIL = libutil.a
LIBUTILSRC =\
util/agetcwd.c\ util/agetcwd.c\
util/agetline.c\ util/agetline.c\
util/apathmax.c\ util/apathmax.c\
@ -43,6 +51,8 @@ LIBSRC =\
util/strlcat.c\ util/strlcat.c\
util/strlcpy.c util/strlcpy.c
LIB = $(LIBUTF) $(LIBUTIL)
BIN =\ BIN =\
basename\ basename\
cal\ cal\
@ -121,8 +131,9 @@ BIN =\
xargs\ xargs\
yes yes
LIBOBJ = $(LIBSRC:.c=.o) LIBUTFOBJ = $(LIBUTFSRC:.c=.o)
OBJ = $(BIN:=.o) $(LIBOBJ) LIBUTILOBJ = $(LIBUTILSRC:.c=.o)
OBJ = $(BIN:=.o) $(LIBUTFOBJ) $(LIBUTILOBJ)
SRC = $(BIN:=.c) SRC = $(BIN:=.c)
MAN = $(BIN:=.1) MAN = $(BIN:=.1)
@ -138,7 +149,7 @@ $(OBJ): $(HDR) config.mk
.c.o: .c.o:
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
$(LIB): $(LIBOBJ) $(LIB): $(LIBUTFOBJ) $(LIBUTILOBJ)
$(AR) -r -c $@ $? $(AR) -r -c $@ $?
$(RANLIB) $@ $(RANLIB) $@
@ -180,7 +191,7 @@ sbase-box: $(LIB) $(SRC)
rm -r build rm -r build
clean: clean:
rm -f $(BIN) $(OBJ) $(LIBOBJ) $(LIB) sbase-box sbase-$(VERSION).tar.gz rm -f $(BIN) $(OBJ) $(LIBUTFOBJ) $(LIBUTIL) $(LIB) sbase-box sbase-$(VERSION).tar.gz
.PHONY: .PHONY:
all install uninstall dist sbase-box clean all install uninstall dist sbase-box clean

148
libutf/rune.c Normal file
View File

@ -0,0 +1,148 @@
/* MIT/X Consortium Copyright (c) 2012 Connor Lane Smith <cls@lubutu.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "../utf.h"
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define UTFSEQ(x) ((((x) & 0x80) == 0x00) ? 1 /* 0xxxxxxx */ \
: (((x) & 0xC0) == 0x80) ? 0 /* 10xxxxxx */ \
: (((x) & 0xE0) == 0xC0) ? 2 /* 110xxxxx */ \
: (((x) & 0xF0) == 0xE0) ? 3 /* 1110xxxx */ \
: (((x) & 0xF8) == 0xF0) ? 4 /* 11110xxx */ \
: (((x) & 0xFC) == 0xF8) ? 5 /* 111110xx */ \
: (((x) & 0xFE) == 0xFC) ? 6 /* 1111110x */ \
: 0 )
#define BADRUNE(x) ((x) < 0 || (x) > Runemax \
|| ((x) & 0xFFFE) == 0xFFFE \
|| ((x) >= 0xD800 && (x) <= 0xDFFF) \
|| ((x) >= 0xFDD0 && (x) <= 0xFDEF))
int
runetochar(char *s, const Rune *p)
{
Rune r = *p;
switch(runelen(r)) {
case 1: /* 0aaaaaaa */
s[0] = r;
return 1;
case 2: /* 00000aaa aabbbbbb */
s[0] = 0xC0 | ((r & 0x0007C0) >> 6); /* 110aaaaa */
s[1] = 0x80 | (r & 0x00003F); /* 10bbbbbb */
return 2;
case 3: /* aaaabbbb bbcccccc */
s[0] = 0xE0 | ((r & 0x00F000) >> 12); /* 1110aaaa */
s[1] = 0x80 | ((r & 0x000FC0) >> 6); /* 10bbbbbb */
s[2] = 0x80 | (r & 0x00003F); /* 10cccccc */
return 3;
case 4: /* 000aaabb bbbbcccc ccdddddd */
s[0] = 0xF0 | ((r & 0x1C0000) >> 18); /* 11110aaa */
s[1] = 0x80 | ((r & 0x03F000) >> 12); /* 10bbbbbb */
s[2] = 0x80 | ((r & 0x000FC0) >> 6); /* 10cccccc */
s[3] = 0x80 | (r & 0x00003F); /* 10dddddd */
return 4;
default:
return 0; /* error */
}
}
int
chartorune(Rune *p, const char *s)
{
return charntorune(p, s, UTFmax);
}
int
charntorune(Rune *p, const char *s, size_t len)
{
unsigned int i, n;
Rune r;
if(len == 0) /* can't even look at s[0] */
return 0;
switch((n = UTFSEQ(s[0]))) {
case 1: r = s[0]; break; /* 0xxxxxxx */
case 2: r = s[0] & 0x1F; break; /* 110xxxxx */
case 3: r = s[0] & 0x0F; break; /* 1110xxxx */
case 4: r = s[0] & 0x07; break; /* 11110xxx */
case 5: r = s[0] & 0x03; break; /* 111110xx */
case 6: r = s[0] & 0x01; break; /* 1111110x */
default: /* invalid sequence */
*p = Runeerror;
return 1;
}
/* add values from continuation bytes */
for(i = 1; i < MIN(n, len); i++)
if((s[i] & 0xC0) == 0x80) {
/* add bits from continuation byte to rune value
* cannot overflow: 6 byte sequences contain 31 bits */
r = (r << 6) | (s[i] & 0x3F); /* 10xxxxxx */
}
else { /* expected continuation */
*p = Runeerror;
return i;
}
if(i < n) /* must have reached len limit */
return 0;
/* reject invalid or overlong sequences */
if(BADRUNE(r) || runelen(r) < (int)n)
r = Runeerror;
*p = r;
return n;
}
int
runelen(Rune r)
{
if(BADRUNE(r))
return 0; /* error */
else if(r <= 0x7F)
return 1;
else if(r <= 0x07FF)
return 2;
else if(r <= 0xFFFF)
return 3;
else
return 4;
}
size_t
runenlen(const Rune *p, size_t len)
{
size_t i, n = 0;
for(i = 0; i < len; i++)
n += runelen(p[i]);
return n;
}
int
fullrune(const char *s, size_t len)
{
Rune r;
return charntorune(&r, s, len) > 0;
}

48
libutf/runetype.c Normal file
View File

@ -0,0 +1,48 @@
/* MIT/X Consortium Copyright (c) 2012 Connor Lane Smith <cls@lubutu.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include "../utf.h"
#define nelem(x) (sizeof (x) / sizeof *(x))
static int rune1cmp(const void *, const void *);
static int rune2cmp(const void *, const void *);
#include "../runetypebody.h"
int
rune1cmp(const void *v1, const void *v2)
{
Rune r1 = *(Rune *)v1, r2 = *(Rune *)v2;
return r1 - r2;
}
int
rune2cmp(const void *v1, const void *v2)
{
Rune r = *(Rune *)v1, *p = (Rune *)v2;
if(r >= p[0] && r <= p[1])
return 0;
else
return r - p[0];
}

129
libutf/utf.c Normal file
View File

@ -0,0 +1,129 @@
/* MIT/X Consortium Copyright (c) 2012 Connor Lane Smith <cls@lubutu.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include "../utf.h"
char *
utfecpy(char *to, char *end, const char *from)
{
Rune r = Runeerror;
size_t i, n;
/* seek through to find final full rune */
for(i = 0; r != '\0' && (n = charntorune(&r, &from[i], end - &to[i])); i += n)
;
memcpy(to, from, i); /* copy over bytes up to this rune */
if(i > 0 && r != '\0')
to[i] = '\0'; /* terminate if unterminated */
return &to[i];
}
size_t
utflen(const char *s)
{
const char *p = s;
size_t i;
Rune r;
for(i = 0; *p != '\0'; i++)
p += chartorune(&r, p);
return i;
}
size_t
utfnlen(const char *s, size_t len)
{
const char *p = s;
size_t i;
Rune r;
int n;
for(i = 0; (n = charntorune(&r, p, len-(p-s))) && r != '\0'; i++)
p += n;
return i;
}
char *
utfrune(const char *s, Rune r)
{
if(r < Runeself) {
return strchr(s, r);
}
else if(r == Runeerror) {
Rune r0;
int n;
for(; *s != '\0'; s += n) {
n = chartorune(&r0, s);
if(r == r0)
return (char *)s;
}
}
else {
char buf[UTFmax+1];
int n;
if(!(n = runetochar(buf, &r)))
return NULL;
buf[n] = '\0';
return strstr(s, buf);
}
return NULL;
}
char *
utfrrune(const char *s, Rune r)
{
const char *p = NULL;
Rune r0;
int n;
if(r < Runeself)
return strrchr(s, r);
for(; *s != '\0'; s += n) {
n = chartorune(&r0, s);
if(r == r0)
p = s;
}
return (char *)p;
}
char *
utfutf(const char *s, const char *t)
{
const char *p, *q;
Rune r0, r1, r2;
int n, m;
for(chartorune(&r0, t); (s = utfrune(s, r0)); s++) {
for(p = s, q = t; *q && *p; p += n, q += m) {
n = chartorune(&r1, p);
m = chartorune(&r2, q);
if(r1 != r2)
break;
}
if(!*q)
return (char *)s;
}
return NULL;
}

1886
runetypebody.h Normal file

File diff suppressed because it is too large Load Diff

51
utf.h Normal file
View File

@ -0,0 +1,51 @@
/* MIT/X Consortium Copyright (c) 2012 Connor Lane Smith <cls@lubutu.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stddef.h>
typedef int Rune;
enum {
UTFmax = 6, /* maximum bytes per rune */
Runeself = 0x80, /* rune and utf are equal (<) */
Runeerror = 0xFFFD, /* decoding error in utf */
Runemax = 0x10FFFF /* maximum rune value */
};
int runetochar(char *, const Rune *);
int chartorune(Rune *, const char *);
int charntorune(Rune *, const char *, size_t);
int runelen(const Rune);
size_t runenlen(const Rune *, size_t);
int fullrune(const char *, size_t);
char *utfecpy(char *, char *, const char *);
size_t utflen(const char *);
size_t utfnlen(const char *, size_t);
char *utfrune(const char *, Rune);
char *utfrrune(const char *, Rune);
char *utfutf(const char *, const char *);
int isalpharune(Rune);
int islowerrune(Rune);
int isspacerune(Rune);
int istitlerune(Rune);
int isupperrune(Rune);
int isdigitrune(Rune);