Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

40
src/libc/string/atoi.c Normal file
View file

@ -0,0 +1,40 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int atoi(const char *nptr);
// long int atol(const char *nptr);
///////////////////////////////////////
// Exports to apps:
// atoi, atol
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <pblibc_private.h>
intmax_t strtoX_core(const char * restrict nptr, char ** restrict endptr, int base, bool do_errors,
intmax_t max, intmax_t min);
int atoi(const char *nptr) {
return strtoX_core(nptr, NULL, 10, false, INT_MAX, INT_MIN);
}
long int atol(const char *nptr) {
return strtoX_core(nptr, NULL, 10, false, INT_MAX, INT_MIN);
}

33
src/libc/string/memchr.c Normal file
View file

@ -0,0 +1,33 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// void *memchr(const void *s, int c, size_t n);
#include <stddef.h>
#include <pblibc_private.h>
void *memchr(const void *s, int c, size_t n) {
const unsigned char *p = (const unsigned char*)s;
unsigned char ch = (unsigned char)c;
for (size_t i = 0; i < n; i++) {
if (p[i] == ch) {
return (void*)&p[i];
}
}
return NULL;
}

36
src/libc/string/memcmp.c Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int memcmp(const void *s1, const void *s2, size_t n);
#include <stddef.h>
#include <pblibc_private.h>
int memcmp(const void *s1, const void *s2, size_t n) {
const unsigned char *p1 = (const unsigned char*)s1;
const unsigned char *p2 = (const unsigned char*)s2;
while (n--) {
int diff = *p1 - *p2;
if (diff) {
return diff;
}
p1++;
p2++;
}
return 0;
}

54
src/libc/string/memcpy.c Normal file
View file

@ -0,0 +1,54 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// void *memcpy(void *s1, const void *s2, size_t n);
// void *memmove(void *s1, const void *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// memcpy, memmove
#include <stddef.h>
#include <stdint.h>
#include <pblibc_assembly.h>
#include <pblibc_private.h>
#if !MEMCPY_IMPLEMENTED_IN_ASSEMBLY
void *memcpy(void * restrict s1, const void * restrict s2, size_t n) {
char *dest = (char*)s1;
const char *src = (const char*)s2;
while (n--) {
*dest++ = *src++;
}
return s1;
}
#endif
void *memmove(void * restrict s1, const void * restrict s2, size_t n) {
char *dest = (char*)s1;
const char *src = (const char*)s2;
if (dest <= src) {
while (n--) {
*dest++ = *src++;
}
} else {
while (n--) {
dest[n] = src[n];
}
}
return s1;
}

36
src/libc/string/memset.c Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// void *memset(void *s, int c, size_t n);
///////////////////////////////////////
// Exports to apps:
// memset
#include <stddef.h>
#include <pblibc_assembly.h>
#include <pblibc_private.h>
#if !MEMSET_IMPLEMENTED_IN_ASSEMBLY
void *memset(void *s, int c, size_t n) {
unsigned char *p = (unsigned char*)s;
while (n--) {
*p++ = (unsigned char)c;
}
return s;
}
#endif

49
src/libc/string/strcat.c Normal file
View file

@ -0,0 +1,49 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strcat(char *s1, const char *s2);
// char *strncat(char *s1, const char *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// strcat, strncat
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <string.h>
#include <pblibc_private.h>
char *strcat(char * restrict s1, const char * restrict s2) {
char *rc = s1;
s1 += strlen(s1);
strcpy(s1, s2);
return rc;
}
char *strncat(char * restrict s1, const char * restrict s2, size_t n) {
char * rc = s1;
s1 += strlen(s1);
size_t strn = strlen(s2);
if (strn > n) {
strn = n;
}
memcpy(s1, s2, strn);
s1[strn] = '\0';
return rc;
}

43
src/libc/string/strchr.c Normal file
View file

@ -0,0 +1,43 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strchr(const char *s1, int c);
// char *strrchr(const char *s1, int c);
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
char *strchr(const char *s, int c) {
size_t n = strlen(s) + 1;
return memchr(s, c, n);
}
char *strrchr(const char *s, int c) {
char ch = (char)c;
size_t i = strlen(s);
do {
if (s[i] == ch) {
return (char*)&s[i];
}
} while (i--);
return NULL;
}

43
src/libc/string/strcmp.c Normal file
View file

@ -0,0 +1,43 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int strcmp(const char *s1, const char *s2);
// int strncmp(const char *s1, const char *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// strcmp, strncmp
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
int strcmp(const char *s1, const char *s2) {
size_t n = strlen(s1) + 1;
return memcmp(s1, s2, n);
}
int strncmp(const char *s1, const char *s2, size_t n) {
size_t strn = strlen(s1) + 1;
if (strn < n) {
n = strn;
}
return memcmp(s1, s2, n);
}

48
src/libc/string/strcpy.c Normal file
View file

@ -0,0 +1,48 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strcpy(char *s1, const char *s2);
// char *strncpy(char *s1, const char *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// strcpy, strncpy
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
char *strcpy(char * restrict s1, const char * restrict s2) {
size_t n = strlen(s2) + 1;
return memcpy(s1, s2, n);
}
char *strncpy(char * restrict s1, const char * restrict s2, size_t n) {
char *rc = s1;
size_t strn = strlen(s2) + 1;
if (strn > n) {
strn = n;
}
memcpy(s1, s2, strn);
if (n > strn) {
memset(s1 + strn, '\0', n - strn);
}
return rc;
}

42
src/libc/string/strlen.c Normal file
View file

@ -0,0 +1,42 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// size_t strlen(const char *s);
// size_t strnlen(const char *s, size_t maxlen);
///////////////////////////////////////
// Exports to apps:
// strlen
#include <stddef.h>
#include <pblibc_private.h>
size_t strlen(const char *s) {
size_t len = 0;
while (*s++) {
len++;
}
return len;
}
size_t strnlen(const char *s, size_t maxlen) {
size_t len = 0;
while (*s++ && maxlen--) {
len++;
}
return len;
}

58
src/libc/string/strspn.c Normal file
View file

@ -0,0 +1,58 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// size_t strcspn(const char *s1, const char *s2);
// size_t strspn(const char *s1, const char *s2);
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
size_t strcspn(const char *s1, const char *s2) {
size_t len = 0;
const char *p;
while (s1[len]) {
p = s2;
while (*p) {
if (s1[len] == *p++) {
return len;
}
}
len++;
}
return len;
}
size_t strspn(const char *s1, const char *s2) {
size_t len = 0;
const char *p;
while (s1[len]) {
p = s2;
while (*p) {
if (s1[len] == *p) {
break;
}
p++;
}
if (!*p) {
return len;
}
len++;
}
return len;
}

37
src/libc/string/strstr.c Normal file
View file

@ -0,0 +1,37 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strstr(const char *s1, const char *s2);
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
char *strstr(const char *s1, const char *s2) {
size_t len = strlen(s2);
while (*s1) {
if (strncmp(s1, s2, len) == 0) {
return (char*)s1;
}
s1++;
}
return NULL;
}

88
src/libc/string/strtol.c Normal file
View file

@ -0,0 +1,88 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// long int strtol(const char *nptr, char **endptr, int base);
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
#include <pblibc_private.h>
intmax_t strtoX_core(const char * restrict nptr, char ** restrict endptr, int base, bool do_errors,
intmax_t max, intmax_t min) {
intmax_t value = 0;
int sign = 1;
while (isspace((int)*nptr)) {
nptr++;
}
switch (*nptr) {
case '+': sign = 1; nptr++; break;
case '-': sign = -1; nptr++; break;
}
if (nptr[0] == '0' && nptr[1] == 'x' && (base == 0 || base == 16)) {
base = 16;
nptr += 2;
} else if (nptr[0] == '0' && (base == 0 || base == 8)) {
base = 8;
nptr++;
} else if (base == 0) {
base = 10;
}
// We break on '\0' anyways
for (;;) {
char ch = *nptr;
if (ch >= '0' && ch <= '9') {
ch = ch - '0';
} else if (ch >= 'A' && ch <= 'Z') {
ch = ch - 'A' + 10;
} else if (ch >= 'a' && ch <= 'z') {
ch = ch - 'a' + 10;
} else { // This will catch '\0'
break;
}
if (ch >= base) {
break;
}
value *= base;
value += ch;
if (do_errors) {
if ((sign > 0) && (value > max)) {
value = max;
} else if ((sign < 0) && (-value < min)) {
value = -min;
}
}
nptr++;
}
if (endptr) {
*endptr = (char*)nptr;
}
return value * sign;
}
long int strtol(const char * restrict nptr, char ** restrict endptr, int base) {
// INT_* because unit tests
return strtoX_core(nptr, endptr, base, true, INT_MAX, INT_MIN);
}
unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base) {
// UINT_* because unit tests
return strtoX_core(nptr, endptr, base, true, UINT_MAX, 0);
}