From c3dc6f2ff9169c309d1cbf47dcb9e4528d509b2f Mon Sep 17 00:00:00 2001 From: LoGin Date: Fri, 8 Mar 2024 23:22:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=80=E4=BA=9B=E8=BF=87?= =?UTF-8?q?=E6=97=B6=E7=9A=84C=E4=BB=A3=E7=A0=81=20(#565)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 删除C版本的crc库 * 删除lockref * 删除过时的libc文档以及wait.c * 删除过时的C版本kfifo代码及文档 * 移除未用到的lz4库 * 删除内核的stdlib.c * 删除fabs.c * fmt * 使得put_string系统调用能够通过tty输出颜色 暂且这样改,这一部分应该是用户层面提供的功能,后续删除 --------- Co-authored-by: GnoCiYeH --- .vscode/settings.json | 26 - docs/index.rst | 1 - docs/introduction/features.md | 7 - docs/kernel/core_api/data_structures.md | 256 -- docs/kernel/core_api/index.rst | 1 - docs/kernel/core_api/kernel_api.md | 33 - docs/kernel/ktest/index.rst | 1 - docs/kernel/ktest/ktest-framework.md | 114 - docs/kernel/locking/index.rst | 1 - docs/kernel/locking/lockref.md | 181 -- docs/userland/libc/apis/api-list/ctype.md | 38 - docs/userland/libc/apis/api-list/dirent.md | 59 - docs/userland/libc/apis/api-list/errno.md | 185 -- docs/userland/libc/apis/api-list/fcntl.md | 44 - docs/userland/libc/apis/api-list/math.md | 21 - docs/userland/libc/apis/api-list/printf.md | 3 - docs/userland/libc/apis/api-list/stddef.md | 11 - docs/userland/libc/apis/api-list/stdio.md | 52 - docs/userland/libc/apis/api-list/stdlib.md | 20 - docs/userland/libc/apis/api-list/string.md | 38 - docs/userland/libc/apis/api-list/time.md | 32 - docs/userland/libc/apis/api-list/unistd.md | 56 - docs/userland/libc/apis/index.rst | 21 - docs/userland/libc/design/index.rst | 9 - docs/userland/libc/index.rst | 10 - docs/userland/libc/intro.md | 4 - kernel/src/common/crc16.h | 12 - kernel/src/common/crc32.h | 12 - kernel/src/common/crc64.h | 12 - kernel/src/common/crc7.h | 12 - kernel/src/common/crc8.h | 12 - kernel/src/common/kfifo.h | 153 - kernel/src/common/lockref.h | 104 - kernel/src/common/lz4.h | 771 ----- kernel/src/common/math/fabs.c | 30 - kernel/src/common/numa.h | 3 - kernel/src/common/stdlib.h | 9 - kernel/src/common/sys/wait.h | 12 - kernel/src/include/DragonOS/refcount.h | 12 - kernel/src/include/bindings/wrapper.h | 8 - kernel/src/libs/Makefile | 2 +- kernel/src/libs/crc16.c | 41 - kernel/src/libs/crc32.c | 56 - kernel/src/libs/crc64.c | 57 - kernel/src/libs/crc7.c | 38 - kernel/src/libs/crc8.c | 41 - kernel/src/libs/kfifo.c | 151 - kernel/src/libs/lockref.c | 249 -- kernel/src/libs/lz4.c | 3160 -------------------- kernel/src/libs/printk.rs | 12 - kernel/src/libs/stdlib.c | 29 - kernel/src/libs/sys/Makefile | 14 - kernel/src/libs/sys/wait.c | 15 - kernel/src/syscall/mod.rs | 16 +- kernel/src/syscall/syscall.c | 34 +- 55 files changed, 20 insertions(+), 6281 deletions(-) delete mode 100644 docs/kernel/core_api/data_structures.md delete mode 100644 docs/kernel/ktest/ktest-framework.md delete mode 100644 docs/kernel/locking/lockref.md delete mode 100644 docs/userland/libc/apis/api-list/ctype.md delete mode 100644 docs/userland/libc/apis/api-list/dirent.md delete mode 100644 docs/userland/libc/apis/api-list/errno.md delete mode 100644 docs/userland/libc/apis/api-list/fcntl.md delete mode 100644 docs/userland/libc/apis/api-list/math.md delete mode 100644 docs/userland/libc/apis/api-list/printf.md delete mode 100644 docs/userland/libc/apis/api-list/stddef.md delete mode 100644 docs/userland/libc/apis/api-list/stdio.md delete mode 100644 docs/userland/libc/apis/api-list/stdlib.md delete mode 100644 docs/userland/libc/apis/api-list/string.md delete mode 100644 docs/userland/libc/apis/api-list/time.md delete mode 100644 docs/userland/libc/apis/api-list/unistd.md delete mode 100644 docs/userland/libc/apis/index.rst delete mode 100644 docs/userland/libc/design/index.rst delete mode 100644 docs/userland/libc/index.rst delete mode 100644 docs/userland/libc/intro.md delete mode 100644 kernel/src/common/crc16.h delete mode 100644 kernel/src/common/crc32.h delete mode 100644 kernel/src/common/crc64.h delete mode 100644 kernel/src/common/crc7.h delete mode 100644 kernel/src/common/crc8.h delete mode 100644 kernel/src/common/kfifo.h delete mode 100644 kernel/src/common/lockref.h delete mode 100644 kernel/src/common/lz4.h delete mode 100644 kernel/src/common/math/fabs.c delete mode 100644 kernel/src/common/numa.h delete mode 100644 kernel/src/common/stdlib.h delete mode 100644 kernel/src/common/sys/wait.h delete mode 100644 kernel/src/include/DragonOS/refcount.h delete mode 100644 kernel/src/libs/crc16.c delete mode 100644 kernel/src/libs/crc32.c delete mode 100644 kernel/src/libs/crc64.c delete mode 100644 kernel/src/libs/crc7.c delete mode 100644 kernel/src/libs/crc8.c delete mode 100644 kernel/src/libs/kfifo.c delete mode 100644 kernel/src/libs/lockref.c delete mode 100644 kernel/src/libs/lz4.c delete mode 100644 kernel/src/libs/stdlib.c delete mode 100644 kernel/src/libs/sys/Makefile delete mode 100644 kernel/src/libs/sys/wait.c diff --git a/.vscode/settings.json b/.vscode/settings.json index c1a9f417..42f592d2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,5 @@ { "files.associations": { - "stdlib.h": "c", "stdbool.h": "c", "printk.h": "c", "stdarg.h": "c", @@ -12,11 +11,8 @@ "memory.h": "c", "multiboot2.h": "c", "kprint.h": "c", - "8259a.h": "c", "ptrace.h": "c", "mouse.h": "c", - "keyboard.h": "c", - "ps2_keyboard.h": "c", "algorithm": "c", "array": "c", "atomic": "c", @@ -95,22 +91,17 @@ "spinlock.h": "c", "stat.h": "c", "video.h": "c", - "libm.h": "c", "ahci.h": "c", "slab.h": "c", "boot_info.h": "c", "pci.h": "c", "time.h": "c", - "ia64_msi.h": "c", "errno.h": "c", "bug.h": "c", "sched.h": "c", "preempt.h": "c", - "screen_manager.h": "c", "textui.h": "c", "atomic.h": "c", - "uart.h": "c", - "fat_ent.h": "c", "semaphore.h": "c", "mm-types.h": "c", "current.h": "c", @@ -120,27 +111,12 @@ "mutex.h": "c", "mount.h": "c", "internal.h": "c", - "devfs.h": "c", - "devfs-types.h": "c", - "chardev.h": "c", - "rootfs.h": "c", - "tty.h": "c", - "idr.h": "c", - "ktest_utils.h": "c", - "kthread.h": "c", - "lockref.h": "c", "compiler_attributes.h": "c", "timer.h": "c", "hid.h": "c", - "proc.h": "c", "compiler.h": "c", - "hpet.h": "c", - "numa.h": "c", - "procfs.h": "c", "err.h": "c", - "rtc.h": "c", "list.h": "c", - "fat32.h": "c", "irqflags.h": "c", "dirent.h": "c", "cmd_help.h": "c", @@ -155,7 +131,6 @@ "cmd.h": "c", "sleep.h": "c", "net.h": "c", - "lz4.h": "c", "cmd_test.h": "c", "cmpxchg.h": "c", "mman.h": "c", @@ -165,7 +140,6 @@ "charconv": "c", "printf.h": "c", "klog.h": "c", - "sqlite3ext.h": "c", "malloc.h": "c", "*.o": "c", "k_log.h": "c" diff --git a/docs/index.rst b/docs/index.rst index 6901002e..be25163d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -38,7 +38,6 @@ :caption: 应用层 userland/appdev/index - userland/libc/index .. toctree:: :maxdepth: 1 diff --git a/docs/introduction/features.md b/docs/introduction/features.md index 23bd951a..30fe9935 100644 --- a/docs/introduction/features.md +++ b/docs/introduction/features.md @@ -73,16 +73,9 @@ - [x] softirq 软中断 - [x] 内核栈traceback -### 内核数据结构 - -- [x] 普通二叉树 -- [x] kfifo缓冲区 -- [x] 循环链表 -- [x] IDR ### 内核实用库 -- [x] LZ4压缩库(1.9.3) - [x] 字符串操作库 - [x] ELF可执行文件支持 - [x] printk diff --git a/docs/kernel/core_api/data_structures.md b/docs/kernel/core_api/data_structures.md deleted file mode 100644 index 822ddcd1..00000000 --- a/docs/kernel/core_api/data_structures.md +++ /dev/null @@ -1,256 +0,0 @@ -# 内核数据结构 - -  内核中实现了常用的几种数据结构,这里是他们的api文档。 - --------------- -## kfifo先进先出缓冲区 - -  kfifo先进先出缓冲区定义于`common/kfifo.h`中。您可以使用它,创建指定大小的fifo缓冲区(最大大小为4GB) - -### kfifo_alloc - -`int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved)` - -#### 描述 - -  通过动态方式初始化kfifo缓冲队列。fifo缓冲区的buffer将由该函数进行申请。 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -**size** - -  缓冲区大小(单位:bytes) - -**reserved** - -  当前字段保留,请将其置为0 - -#### 返回值 - -  当返回值为0时,表示正常初始化成功,否则返回对应的errno - -### kfifo_init - -`void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size)` - -#### 描述 - -  使用指定的缓冲区来初始化kfifo缓冲队列 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -**buffer** - -  缓冲区基地址指针 - -**size** - -  缓冲区大小(单位:bytes) - -### kfifo_free_alloc - -`void kfifo_free_alloc(struct kfifo_t* fifo)` - -#### 描述 - -  释放通过kfifo_alloc创建的fifo缓冲区. 请勿通过该函数释放其他方式创建的kfifo缓冲区。 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -### kfifo_in - -`uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size)` - -#### 描述 - -  向kfifo缓冲区推入指定大小的数据。当队列中空间不足时,则不推入数据。 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -**from** - -  源数据基地址指针 - -**size** - -  数据大小(单位:bytes) - -#### 返回值 - -  返回成功被推入的数据的大小。 - -### kfifo_out - -`uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size)` - -#### 描述 - -  从kfifo缓冲区取出数据,并从队列中删除数据。当队列中数据量不足时,则不取出。 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -**to** - -  目标缓冲区基地址指针 - -**size** - -  数据大小(单位:bytes) - -#### 返回值 - -  返回成功被取出的数据的大小。 - -### kfifo_out_peek - -`uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size)` - -#### 描述 - -  从kfifo缓冲区取出数据,但是不从队列中删除数据。当队列中数据量不足时,则不取出。 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -**to** - -  目标缓冲区基地址指针 - -**size** - -  数据大小(单位:bytes) - -#### 返回值 - -  返回成功被取出的数据的大小。 - -### kfifo_reset - -`kfifo_reset(fifo)` - -#### 描述 - -  忽略kfifo队列中的所有内容,并把输入和输出偏移量都归零 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -### kfifo_reset_out - -`kfifo_reset_out(fifo)` - -#### 描述 - -  忽略kfifo队列中的所有内容,并将输入偏移量赋值给输出偏移量 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -### kfifo_total_size - -`kfifo_total_size(fifo)` - -#### 描述 - -  获取kfifo缓冲区的最大大小 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -#### 返回值 - -  缓冲区最大大小 - -### kfifo_size - -`kfifo_size(fifo)` - -#### 描述 - -  获取kfifo缓冲区当前已使用的大小 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -#### 返回值 - -  缓冲区当前已使用的大小 - -### kfifo_empty - -`kfifo_empty(fifo)` - -#### 描述 - -  判断kfifo缓冲区当前是否为空 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -#### 返回值 - -| 情况 | 返回值 | -| ----------------------- | --- | -| 空 | 1 | -| 非空 | 0 | - -### kfifo_full - -`kfifo_full(fifo)` - -#### 描述 - -  判断kfifo缓冲区当前是否为满 - -#### 参数 - -**fifo** - -  kfifo队列结构体的指针 - -#### 返回值 - -| 情况 | 返回值 | -| ------| --- | -| 满 | 1 | -| 不满 | 0 | - ------------------- diff --git a/docs/kernel/core_api/index.rst b/docs/kernel/core_api/index.rst index 19431fad..a27f1a9a 100644 --- a/docs/kernel/core_api/index.rst +++ b/docs/kernel/core_api/index.rst @@ -10,7 +10,6 @@ kernel_api atomic - data_structures casting notifier_chain softirq diff --git a/docs/kernel/core_api/kernel_api.md b/docs/kernel/core_api/kernel_api.md index 837bc189..f45d3a42 100644 --- a/docs/kernel/core_api/kernel_api.md +++ b/docs/kernel/core_api/kernel_api.md @@ -693,36 +693,3 @@ **size**   待拷贝的数据大小 - -## CRC函数 - -### 函数列表 - -**`uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len)`** - -**`uint8_t crc8(uint8_t crc, const uint8_t *buffer, size_t len)`** - -**`uint16_t crc16(uint16_t crc, uint8_t const *buffer, size_t len)`** - -**`uint32_t crc32(uint32_t crc, uint8_t const *buffer, size_t len)`** - -**`uint64_t crc64(uint64_t crc, uint8_t const *buffer, size_t len)`** - -### 描述 - -  用于计算循环冗余校验码 - -### 参数说明 - -**crc** - -  传入的CRC初始值 - -**buffer** - -  待处理的数据缓冲区 - -**len** - -  缓冲区大小(字节) - diff --git a/docs/kernel/ktest/index.rst b/docs/kernel/ktest/index.rst index 515f35e9..bd73448b 100644 --- a/docs/kernel/ktest/index.rst +++ b/docs/kernel/ktest/index.rst @@ -17,4 +17,3 @@ :maxdepth: 1 :caption: 目录 - ktest-framework diff --git a/docs/kernel/ktest/ktest-framework.md b/docs/kernel/ktest/ktest-framework.md deleted file mode 100644 index 1a7f1cbf..00000000 --- a/docs/kernel/ktest/ktest-framework.md +++ /dev/null @@ -1,114 +0,0 @@ -# 内核测试框架 - -  DragonOS提供了一个测试框架,旨在对内核的一些模块进行自动化测试。内核测试框架位于`ktest/`下。 - -  我们可以使用这个测试框架,按照规范编写测试代码,然后在合适的地方使用`ktest_start()`创建一个全新的内核线程并发起测试。 - -## 使用方法 - -### 创建自动测试程序 - -  假如您要对kfifo模块进行自动测试,您可以在`ktest/`下,创建一个名为`test-kfifo.c`的测试文件,并编写Makefile。 - -  在`test-kfifo.c`中,包含`ktest_utils.h`和`ktest.h`这两个头文件。 - -  您需要像下面这样,在`test-kfifo.c`中,创建一个测试用例函数表,并把测试用例函数填写到其中: -```c -static ktest_case_table kt_kfifo_func_table[] = { - ktest_kfifo_case0_1, -}; -``` - -  然后创建一个函数,作为kfifo测试的主函数。请注意,您需要将它的声明添加到`ktest.h`中。 - -```c -uint64_t ktest_test_kfifo(uint64_t arg) -{ - kTEST("Testing kfifo..."); - for (int i = 0; i < sizeof(kt_kfifo_func_table) / sizeof(ktest_case_table); ++i) - { - kTEST("Testing case %d", i); - kt_kfifo_func_table[i](i, 0); - } - kTEST("kfifo Test done."); - return 0; -} -``` - - -### 编写测试用例 - -  您可以创建一个或多个测试用例,命名为:`ktest_kfifo_case_xxxxx`. 在这个例子中,我创建了一个测试用例,命名为:`ktest_kfifo_case0_1`.如下所示: - -```c -static long ktest_kfifo_case0_1(uint64_t arg0, uint64_t arg1) -``` - -  这里最多允许我们传递两个参数到测试函数里面。 - -  那么,我们该如何编写测试用例呢? - -  我们主要是需要设置一些情节,以便能测试到目标组件的每个情况。为了检验模块的行为是否符合预期,我们需要使用`assert(condition)`宏函数,对目标`condition`进行校验。若`condition`为1,则表明测试通过。否则,将会输出一行assert failed信息到屏幕上。 - -### 发起测试 - -  我们可以在pid≥1的内核线程中发起测试。由于DragonOS目前尚不完善,您可以在`process/process.c`中的`initial_kernel_thread()`函数内,发起内核自动测试。具体的代码如下: - -```c -ktest_start(ktest_test_kfifo, 0); -``` - -  这样就发起了一个内核测试,它会创建一个新的内核线程进行自动测试,您不必担心第一个内核线程会被阻塞。 -   - -## API文档 - -### ktest_start - -`pid_t ktest_start(uint64_t (*func)(uint64_t arg), uint64_t arg)` - -#### 描述 - -  开启一个新的内核线程以进行测试 - -#### 参数 - -**func** - -  测试函数. 新的测试线程将会执行该函数,以进行测试。 - -**arg** - -  传递给测试函数的参数 - -#### 返回值 - -  测试线程的pid - -### assert - -`#define assert(condition)` - -#### 描述 - -  判定condition是否为1,若不为1,则输出一行错误日志信息: - -``` -[ kTEST FAILED ] Ktest Assertion Failed, file:%s, Line:%d -``` - -### kTEST - -```#define kTEST(...) ``` - -#### 描述 - -  格式化输出一行以`[ kTEST ] file:%s, Line:%d`开头的日志信息。 - - -### ktest_case_table - -`typedef long (*ktest_case_table)(uint64_t arg0, uint64_t arg1)` - -#### 描述 -  ktest用例函数的类型定义。 diff --git a/docs/kernel/locking/index.rst b/docs/kernel/locking/index.rst index 227bd421..bc33b229 100644 --- a/docs/kernel/locking/index.rst +++ b/docs/kernel/locking/index.rst @@ -9,7 +9,6 @@ locks spinlock - lockref mutex rwlock diff --git a/docs/kernel/locking/lockref.md b/docs/kernel/locking/lockref.md deleted file mode 100644 index a3c36a69..00000000 --- a/docs/kernel/locking/lockref.md +++ /dev/null @@ -1,181 +0,0 @@ -(_lockref)= -# lockref - -  lockref是将自旋锁与引用计数变量融合在连续、对齐的8字节内的一种技术。 - -  目前,DragonOS中,通过C、Rust各实现了一个版本的lockref。请注意,二者不兼容。对于新的功能模块,请使用Rust版本的lockref。随着代码重构工作的进行,我们将会删除C版本的lockref。 - -## 1. lockref结构 - -### 1.1. Rust版本 -```rust -/// 仅在x86_64架构下使用cmpxchg -#[cfg(target_arch = "x86_64")] -/// 由于需要cmpxchg,所以整个lockref按照8字节对齐 -#[repr(align(8))] -#[derive(Debug)] -pub struct LockRef { - pub lock: RawSpinlock, - pub count: i32, -} - -/// 除了x86_64以外的架构,不使用cmpxchg进行优化 -#[cfg(not(target_arch = "x86_64"))] -pub struct LockRef { - lock: RawSpinlock, - count: i32, -} -``` - -### 1.2. C版本 -```c -struct lockref -{ - union - { -#ifdef __LOCKREF_ENABLE_CMPXCHG__ - aligned_u64 lock_count; // 通过该变量的声明,使得整个lockref的地址按照8字节对齐 -#endif - struct - { - spinlock_t lock; - int count; - }; - }; -}; -``` - -## 2. 特性描述 -  由于在高负载的情况下,系统会频繁的执行“锁定-改变引用变量-解锁”的操作,这期间很可能出现spinlock和引用计数跨缓存行的情况,这将会大大降低性能。lockref通过强制对齐,尽可能的降低缓存行的占用数量,使得性能得到提升。 - -  并且,在x64体系结构下,还通过cmpxchg()指令,实现了无锁快速路径。不需要对自旋锁加锁即可更改引用计数的值,进一步提升性能。当快速路径不存在(对于未支持的体系结构)或者尝试超时后,将会退化成“锁定-改变引用变量-解锁”的操作。此时由于lockref强制对齐,只涉及到1个缓存行,因此性能比原先的spinlock+ref_count的模式要高。 - -## 3. 关于cmpxchg_loop - -  在改变引用计数时,cmpxchg先确保没有别的线程持有锁,然后改变引用计数,同时通过`lock cmpxchg`指令验证在更改发生时,没有其他线程持有锁,并且当前的目标lockref的值与old变量中存储的一致,从而将新值存储到目标lockref。这种无锁操作能极大的提升性能。如果不符合上述条件,在多次尝试后,将退化成传统的加锁方式来更改引用计数。 - -## 4. Rust版本的API - -### 4.1. 引用计数自增 - -- `pub fn inc(&mut self)` -- `pub fn inc_not_zero(&mut self) -> Result` -- `pub fn inc_not_dead(&mut self) -> Result` - -#### 4.1.1. inc - -##### 说明 - -  原子的将引用计数加1。 - -##### 返回值 - -  无 - -#### 4.1.2. inc_not_zero - -##### 说明 - -  原子地将引用计数加1.如果原来的count≤0,则操作失败。 - -##### 返回值 - -| 返回值 | 说明 | -| :--- | :--- | -| Ok(self.count) | 成功,返回新的引用计数 | -| Err(SystemError::EPERM) | 失败,返回EPERM | - -#### 4.1.3. inc_not_dead - -##### 说明 - -  引用计数自增1。(除非该lockref已经被标记为死亡) - -##### 返回值 - -| 返回值 | 说明 | -| :--- | :--- | -| Ok(self.count) | 成功,返回新的引用计数 | -| Err(SystemError::EPERM) | 失败,返回EPERM | - -### 4.2. 引用计数自减 -- `pub fn dec(&mut self) -> Result` -- `pub fn dec_return(&mut self) -> Result` -- `pub fn dec_not_zero(&mut self) -> Result` -- `pub fn dec_or_lock_not_zero(&mut self) -> Result` - -#### 4.2.1. dec - -##### 说明 - -  原子地将引用计数-1。如果已处于count≤0的状态,则返回Err(SystemError::EPERM) - -  本函数与`lockref_dec_return()`的区别在于,当在`cmpxchg()`中检测到`count<=0`或已加锁,本函数会再次尝试通过加锁来执行操作,而`lockref_dec_return()`会直接返回错误 - -##### 返回值 - -| 返回值 | 说明 | -| :--- | :--- | -| Ok(self.count) | 成功,返回新的引用计数 | -| Err(SystemError::EPERM) | 失败,返回EPERM | - -#### 4.2.2. dec_return - -  原子地将引用计数减1。如果处于已加锁或count≤0的状态,则返回SystemError::EPERM - -  本函数与`lockref_dec()`的区别在于,当在`cmpxchg()`中检测到`count<=0`或已加锁,本函数会直接返回错误,而`lockref_dec()`会再次尝试通过加锁来执行操作. - -:::{note} -若当前处理器架构不支持cmpxchg,则退化为`self.dec()` -::: - -##### 返回值 - -| 返回值 | 说明 | -| :--- | :--- | -| Ok(self.count) | 成功,返回新的引用计数 | -| Err(SystemError::EPERM) | 失败,返回EPERM | - -#### 4.2.3. dec_not_zero - -##### 说明 - -  原子地将引用计数减1。若当前的引用计数≤1,则操作失败. - -  该函数与`lockref_dec_or_lock_not_zero()`的区别在于,当`cmpxchg()`时发现`old.count≤1`时,该函数会直接返回`Err(-1)`,而`lockref_dec_or_lock_not_zero()`在这种情况下,会尝试加锁来进行操作。 - -##### 返回值 - -| 返回值 | 说明 | -| :--- | :--- | -| Ok(self.count) | 成功,返回新的引用计数 | -| Err(SystemError::EPERM) | 失败,返回EPERM | - - -#### 4.2.4. dec_or_lock_not_zero - -##### 说明 - -  原子地将引用计数减1。若当前的引用计数≤1,则操作失败. - -  该函数与`lockref_dec_not_zero()`的区别在于,当cmpxchg()时发现`old.count≤1`时,该函数会尝试加锁来进行操作,而`lockref_dec_not_zero()`在这种情况下,会直接返回`Err(SystemError::EPERM)`. - -##### 返回值 - -| 返回值 | 说明 | -| :--- | :--- | -| Ok(self.count) | 成功,返回新的引用计数 | -| Err(SystemError::EPERM) | 失败,返回EPERM | - -### 4.3. 其他 -- `pub fn mark_dead(&mut self)` - -#### 4.3.1. mark_dead - -##### 说明 - -  将引用计数原子地标记为死亡状态. - -## 参考资料 - -  [Introducing lockrefs - LWN.net, Jonathan Corbet](https://lwn.net/Articles/565734/) diff --git a/docs/userland/libc/apis/api-list/ctype.md b/docs/userland/libc/apis/api-list/ctype.md deleted file mode 100644 index 35cb3725..00000000 --- a/docs/userland/libc/apis/api-list/ctype.md +++ /dev/null @@ -1,38 +0,0 @@ -# ctype.h -## 函数列表(这里只列出已实现的函数): - - ``int isprint(int c)`` : 传入一个字符,判断是否可以被输出 - - ``int islower(int c)`` : 传入一个字符,判断是否是小写字母 - - ``int isupper(int c)`` : 传入一个字符,判断是否是大写字母 - - ``int isalpha(int c)`` : 传入一个字符,判断是否是字母 - - ``int isdigit(int c)`` : 传入一个字符,判断是否是数字 - - ``int toupper(int c)`` : 传入一个小写字母字符,返回这个字母的大写形式 - - ``int tolower(int c)`` : 传入一个大写字母字符,返回这个字母的小写形式 - - ``int isspace(int c)`` : 传入一个字符,判断是否是空白字符 - -## 宏定义: - - ### 暂无用处 - - ``#define _U 01`` - - ``#define _L 02`` - - ``#define _N 04`` - - ``#define _S 010`` - - ``#define _P 020`` - - ``#define _C 040`` - - ``#define _X 0100`` - - ``#define _B 0200`` \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/dirent.md b/docs/userland/libc/apis/api-list/dirent.md deleted file mode 100644 index ced3ec17..00000000 --- a/docs/userland/libc/apis/api-list/dirent.md +++ /dev/null @@ -1,59 +0,0 @@ -# dirent.h -## 简介 - 与文件夹有关的头文件。 - -## 结构体列表: - - ``struct DIR`` : - - 变量列表: - - ``int fd`` : 文件夹id(不推荐修改) - - ``int buf_pos`` : 文件夹缓冲区指针的位置 - - ``int buf_len`` : 文件夹缓冲区的大小(默认为256) - - ``struct dirent`` : - - 变量列表: - - ``ino_t(see libc/sys/types.h) ino`` : 文件序列号(不推荐修改) - - ``off_t d_off`` : dir偏移量(不推荐修改) - - ``unsigned short d_reclen`` : 文件夹中的记录数 - - ``unsigned char d_type`` : 目标的类型(有可能是文件,文件夹,磁盘) - - ``char d_name[]`` : 目标的名字 - -## 函数列表(这里只列出已实现的函数): - - ``DIR opendir(const char *path)`` - - 传入文件夹的路径,返回文件夹结构体 - - ``int closedir(DIR *dirp)`` - - 传入文件夹结构体,关闭文件夹,释放内存 - - 若失败,返回-1 - - ``dirent readdir(DIR *dir)`` - - 传入文件夹结构体,读入文件夹里的内容,并打包为dirent结构体返回 - -## 宏定义: - - 文件夹类型: - - ``#define VFS_IF_FILE (1UL << 0)`` - - ``#define VFS_IF_DIR (1UL << 1)`` - - ``#define VFS_IF_DEVICE (1UL << 2)`` - - 缓冲区长度的默认值 - - ``#define DIR_BUF_SIZE 256`` diff --git a/docs/userland/libc/apis/api-list/errno.md b/docs/userland/libc/apis/api-list/errno.md deleted file mode 100644 index 017c7450..00000000 --- a/docs/userland/libc/apis/api-list/errno.md +++ /dev/null @@ -1,185 +0,0 @@ -# errno.h - -## 简介: - - 共享错误号码 -## 属性: - - ``extern int errno`` : 通用错误代码 - -## 宏定义(复制自代码,了解即可): - - #define E2BIG 1 /* 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.*/ - - #define EACCES 2 /* 访问被拒绝 Permission denied*/ - - #define EADDRINUSE 3 /* 地址正在被使用 Address in use.*/ - - #define EADDRNOTAVAIL 4 /* 地址不可用 Address not available.*/ - - #define EAFNOSUPPORT 5 /* 地址family不支持 Address family not supported.*/ - - #define EAGAIN 6 /* 资源不可用,请重试。 Resource unavailable, try again (may be the same value as [EWOULDBLOCK]).*/ - - #define EALREADY 7 /* 连接已经在处理 Connection already in progress.*/ - - #define EBADF 8 /* 错误的文件描述符 Bad file descriptor.*/ - - #define EBADMSG 9 /* 错误的消息 Bad message.*/ - - - #define EBUSY 10 /* 设备或资源忙 Device or resource busy.*/ - - #define ECANCELED 11 /* 操作被取消 Operation canceled.*/ - - #define ECHILD 12 /* 没有子进程 No child processes.*/ - - #define ECONNABORTED 13 /* 连接已断开 Connection aborted.*/ - - #define ECONNREFUSED 14 /* 连接被拒绝 Connection refused.*/ - - #define ECONNRESET 15 /* 连接被重置 Connection reset.*/ - - #define EDEADLK 16 /* 资源死锁将要发生 Resource deadlock would occur.*/ - - #define EDESTADDRREQ 17 /* 需要目标地址 Destination address required.*/ - - #define EDOM 18 /* 数学参数超出作用域 Mathematics argument out of domain of function.*/ - - #define EDQUOT 19 /* 保留使用 Reserved*/ - - - - #define EEXIST 20 /* 文件已存在 File exists.*/ - - #define EFAULT 21 /* 错误的地址 Bad address*/ - - #define EFBIG 22 /* 文件太大 File too large.*/ - - #define EHOSTUNREACH 23 /* 主机不可达 Host is unreachable.*/ - - #define EIDRM 24 /* 标志符被移除 Identifier removed.*/ - - #define EILSEQ 25 /* 不合法的字符序列 Illegal byte sequence.*/ - - #define EINPROGRESS 26 /* 操作正在处理 Operation in progress.*/ - - #define EINTR 27 /* 被中断的函数 Interrupted function.*/ - - #define EINVAL 28 /* 不可用的参数 Invalid argument.*/ - - #define EIO 29 /* I/O错误 I/O error.*/ - - - - #define EISCONN 30 /* 套接字已连接 Socket is connected.*/ - - #define EISDIR 31 /* 是一个目录 Is a directory*/ - - #define ELOOP 32 /* 符号链接级别过多 Too many levels of symbolic links.*/ - - #define EMFILE 33 /* 文件描述符的值过大 File descriptor value too large.*/ - - #define EMLINK 34 /* 链接数过多 Too many links.*/ - - #define EMSGSIZE 35 /* 消息过大 Message too large.*/ - - #define EMULTIHOP 36 /* 保留使用 Reserved.*/ - - #define ENAMETOOLONG 37 /* 文件名过长 Filename too long.*/ - - #define ENETDOWN 38 /* 网络已关闭 Network is down.*/ - - #define ENETRESET 39 /* 网络连接已断开 Connection aborted by network.*/ - - #define ENETUNREACH 40 /* 网络不可达 Network unreachable.*/ - - #define ENFILE 41 /* 系统中打开的文件过多 Too many files open in system.*/ - - #define ENOBUFS 42 /* 缓冲区空间不足 No buffer space available.*/ - - #define ENODATA 43 /* 队列头没有可读取的消息 No message is available on the STREAM head read queue.*/ - - #define ENODEV 44 /* 没有指定的设备 No such device.*/ - - #define ENOENT 45 /* 没有指定的文件或目录 No such file or directory.*/ - - #define ENOEXEC 46 /* 可执行文件格式错误 Executable file format error.*/ - - #define ENOLCK 47 /* 没有可用的锁 No locks available.*/ - - #define ENOLINK 48 /* 保留 Reserved.*/ - - - #define ENOMEM 49 /* 没有足够的空间 Not enough space.*/ - - - #define ENOMSG 50 /* 没有期待类型的消息 No message of the desired type.*/ - - #define ENOPROTOOPT 51 /* 协议不可用 Protocol not available.*/ - - #define ENOSPC 52 /* 设备上没有空间 No space left on device.*/ - - #define ENOSR 53 /* 没有STREAM资源 No STREAM resources.*/ - - #define ENOSTR 54 /* 不是STREAM Not a STREAM*/ - - #define ENOSYS 55 /* 功能不支持 Function not supported.*/ - - #define ENOTCONN 56 /* 套接字未连接 The socket is not connected.*/ - - #define ENOTDIR 57 /* 不是目录 Not a directory.*/ - - #define ENOTEMPTY 58 /* 目录非空 Directory not empty.*/ - - #define ENOTRECOVERABLE 59 /* 状态不可覆盖 State not recoverable.*/ - - - - #define ENOTSOCK 60 /* 不是一个套接字 Not a socket.*/ - - #define ENOTSUP 61 /* 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).*/ - - #define ENOTTY 62 /* 不正确的I/O控制操作 Inappropriate I/O control operation.*/ - - #define ENXIO 63 /* 没有这样的设备或地址 No such device or address.*/ - - #define EOPNOTSUPP 64 /* 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).*/ - - #define EOVERFLOW 65 /* 数值过大,产生溢出 Value too large to be stored in data type.*/ - - #define EOWNERDEAD 66 /* 之前的拥有者挂了 Previous owner died.*/ - - #define EPERM 67 /* 操作不被允许 Operation not permitted.*/ - - #define EPIPE 68 /* 断开的管道 Broken pipe.*/ - - #define EPROTO 69 /* 协议错误 Protocol error.*/ - - - - #define EPROTONOSUPPORT 70 /* 协议不被支持 Protocol not supported.*/ - - #define EPROTOTYPE 71 /* 对于套接字而言,错误的协议 Protocol wrong type for socket.*/ - - #define ERANGE 72 /* 结果过大 Result too large.*/ - - #define EROFS 73 /* 只读的文件系统 Read-only file system.*/ - - #define ESPIPE 74 /* 错误的寻道 Invalid seek.*/ - - #define ESRCH 75 /* 没有这样的进程 No such process.*/ - - #define ESTALE 76 /* 保留 Reserved.*/ - - #define ETIME 77 /* 流式ioctl()超时 Stream ioctl() timeout*/ - - #define ETIMEDOUT 78 /* 连接超时 Connection timed out.*/ - - #define ETXTBSY 79 /* 文本文件忙 Text file busy.*/ - - - - #define EWOULDBLOCK 80 /* 操作将被禁止 Operation would block (may be the same value as [EAGAIN]).*/ - - #define EXDEV 81 /* 跨设备连接 Cross-device link.*/ diff --git a/docs/userland/libc/apis/api-list/fcntl.md b/docs/userland/libc/apis/api-list/fcntl.md deleted file mode 100644 index 3c8cb6dd..00000000 --- a/docs/userland/libc/apis/api-list/fcntl.md +++ /dev/null @@ -1,44 +0,0 @@ -# fcntl.h - -## 简介 - - 文件操作 -## 函数列表: - - ``int open(const char * path,int options, ...)`` - - 传入文件路径,和文件类型(详细请看下面的宏定义),将文件打开并返回文件id。 - -## 宏定义(粘贴自代码,了解即可): - - #define O_RDONLY 00000000 // Open Read-only - - #define O_WRONLY 00000001 // Open Write-only - - #define O_RDWR 00000002 // Open read/write - - #define O_ACCMODE 00000003 // Mask for file access modes - - - #define O_CREAT 00000100 // Create file if it does not exist - - #define O_EXCL 00000200 // Fail if file already exists - - #define O_NOCTTY 00000400 // Do not assign controlling terminal - - #define O_TRUNC 00001000 // 文件存在且是普通文件,并以O_RDWR或O_WRONLY打开,则它会被清空 - - #define O_APPEND 00002000 // 文件指针会被移动到文件末尾 - - #define O_NONBLOCK 00004000 // 非阻塞式IO模式 - - - #define O_EXEC 00010000 // 以仅执行的方式打开(非目录文件) - - #define O_SEARCH 00020000 // Open the directory for search only - - #define O_DIRECTORY 00040000 // 打开的必须是一个目录 - - #define O_NOFOLLOW 00100000 // Do not follow symbolic links - - diff --git a/docs/userland/libc/apis/api-list/math.md b/docs/userland/libc/apis/api-list/math.md deleted file mode 100644 index 69abc25d..00000000 --- a/docs/userland/libc/apis/api-list/math.md +++ /dev/null @@ -1,21 +0,0 @@ -# math.h - -## 简介: - - 数学库 - -## 函数列表: - - ``double fabs(double x)`` : 返回 x 的绝对值 - - ``float fabsf(float x)`` : 返回 x 的绝对值 - - ``long double fabsl(long double x)``: 返回 x 的绝对值 - - ``double round(double x)`` 四舍五入 x - - ``float roundf(float x)`` 四舍五入 x - - ``long double roundl(long double x)`` 四舍五入 x - - ``int64_t pow(int64_t x,int y)`` 返回 x 的 y 次方 \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/printf.md b/docs/userland/libc/apis/api-list/printf.md deleted file mode 100644 index 04103add..00000000 --- a/docs/userland/libc/apis/api-list/printf.md +++ /dev/null @@ -1,3 +0,0 @@ -# printf.h - -不建议引用,需要 ``printf`` 函数请引用 ``stdio.h`` \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/stddef.md b/docs/userland/libc/apis/api-list/stddef.md deleted file mode 100644 index b9c445ad..00000000 --- a/docs/userland/libc/apis/api-list/stddef.md +++ /dev/null @@ -1,11 +0,0 @@ -# stddef.h - -## 简介: - - 定义了关于指针的常用类型 - -## 定义: - - ``typedef __PTDIFF_TYPE__ ptrdiff_t`` : 两个指针相减的结果类型 - - ``NULL ((void *) 0)`` : 空指针 \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/stdio.md b/docs/userland/libc/apis/api-list/stdio.md deleted file mode 100644 index becd2039..00000000 --- a/docs/userland/libc/apis/api-list/stdio.md +++ /dev/null @@ -1,52 +0,0 @@ -# stdio.h - -## 简介: - - 向标准输入输出里操作 -## 函数列表: - - ``int64_t put_string(char *str, uint64_t front_color, uint64_t bg_color)`` - - 输出字符串(带有前景色,背景色) - - ``int printf(const char *fmt, ...)`` - - 就是正常的 ``printf`` - - ``int sprintf(char *buf,const char *fmt,...)``` - - 就是正常的 ``sprintf`` - - ``int vsprintf(char *buf,const char *fmt,va_list args)`` - - 格式化,不建议调用,请用 printf 或 sprintf 替代。 -## 宏定义 - - ### 字体颜色的宏定义 - - ``#define COLOR_WHITE 0x00ffffff //白`` - - ``#define COLOR_BLACK 0x00000000 //黑`` - - ``#define COLOR_RED 0x00ff0000 //红`` - - ``#define COLOR_ORANGE 0x00ff8000 //橙`` - - ``#define COLOR_YELLOW 0x00ffff00 //黄`` - - ``#define COLOR_GREEN 0x0000ff00 //绿`` - - ``#define COLOR_BLUE 0x000000ff //蓝`` - - ``#define COLOR_INDIGO 0x0000ffff //靛`` - - ``#define COLOR_PURPLE 0x008000ff //紫`` - ### 无需使用 - - ``#define SEEK_SET 0 /* Seek relative to start-of-file */`` - - ``#define SEEK_CUR 1 /* Seek relative to current position */`` - - ``#define SEEK_END 2 /* Seek relative to end-of-file */`` - - ``#define SEEK_MAX 3`` \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/stdlib.md b/docs/userland/libc/apis/api-list/stdlib.md deleted file mode 100644 index afa75093..00000000 --- a/docs/userland/libc/apis/api-list/stdlib.md +++ /dev/null @@ -1,20 +0,0 @@ -# stdlib.h - -## 简介: - - 一些常用函数 -## 函数列表: - - ``void *malloc(ssize_t size)`` : 普通的 ``malloc`` - - ``void free(void *ptr)`` : 释放内存 - - ``int abs(int x)`` : x 的绝对值 - - ``long labs(long x)`` : x 的绝对值 - - ``long long llabs(long long x)`` : x 的绝对值 - - ``int atoi(const char *str)`` 字符串转数字 - - ``void exit(int status)`` : 普通的 ``exit`` \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/string.md b/docs/userland/libc/apis/api-list/string.md deleted file mode 100644 index 19e85dfa..00000000 --- a/docs/userland/libc/apis/api-list/string.md +++ /dev/null @@ -1,38 +0,0 @@ -# string.h - -## 简介: - - 字符串操作 - -## 函数列表: - - ``size_t strlen(const char *s)`` : 获取字符串的长度 - - ``int strcmp(const char *a,const char *b)`` : 比较字符串的字典序 - - ``char *strncpy(char *dst, const char *src, size_t count)`` - - 拷贝制定字节数的字符串 - dst: 目标地址 - src: 原字符串 - count: 字节数 - - ``char *strcpy(char *dst,const char *src)`` : 复制整个字符串 - - ``char *strcat(char *dest,const char* src)`` : 拼接两个字符串 - - ``char *strtok(char *str, const char *delim)`` : 分割字符串 - - ``char *strtok_r(char *str, const char *delim, char **saveptr)`` : 分割字符串 - -**以下函数没有经过检验,不确保正常工作** - - ``size_t strspn(const char *str1, const char *str2)`` : 检索字符串 str1 中第一个不在字符串 str2 中出现的字符下标 - - ``size_t strcspn(const char *str1, const char *str2)`` : 检索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符 - - ``char *strpbrk(const char *str1, const char *str2)`` : 检索字符串 str1 中第一个匹配字符串 str2 中字符的字符 - - ``char *strchr(const char *str, int c)`` : 在字符串中查找第一次出现的字符 - - ``char *strrchr(const char *str, int c)`` : 在字符串中查找最后一次出现的字符 \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/time.md b/docs/userland/libc/apis/api-list/time.md deleted file mode 100644 index c2f49386..00000000 --- a/docs/userland/libc/apis/api-list/time.md +++ /dev/null @@ -1,32 +0,0 @@ -# time.h - -## 简介: - - 时间相关 - -时刻以纳秒为单位 - -## 结构体: - - ``struct timespec`` : 时间戳 - - ### 变量列表: - - ``long int tv_sec`` : 秒 - - ``long int tv_nsec`` : 纳秒 -## 宏定义: - - ``#define CLOCKS_PER_SEC 1000000`` 每一秒有1000000个时刻(纳秒) - -## 函数列表: - - ``int nanosleep(const struct timespec *rdtp,struct timespec *rmtp)`` - - 休眠指定时间 - - rdtp : 指定休眠的时间 - - rmtp : 返回剩余时间 - - ``clock_t clock()`` : 获得当前系统时间 \ No newline at end of file diff --git a/docs/userland/libc/apis/api-list/unistd.md b/docs/userland/libc/apis/api-list/unistd.md deleted file mode 100644 index e6cdacde..00000000 --- a/docs/userland/libc/apis/api-list/unistd.md +++ /dev/null @@ -1,56 +0,0 @@ -# unistd.h - -## 简介: - - 也是一些常用函数 - -## 函数列表: - - ``int close(int fd)`` : 关闭文件 - - ``ssize_t read(int fd,void *buf,size_t count)`` : 从文件读取 - - 传入文件id,缓冲区,以及字节数 - - 返回成功读取的字节数 - - ``ssize_t write(int fd,void const *buf,size_t count)`` : 写入文件 - - 传入文件id,缓冲区,字节数 - - 返回成功写入的字节数 - - ``off_t lseek(int fd,off_t offset,int whence)`` : 调整文件访问位置 - - 传入文件id,偏移量,调整模式 - - 返回结束后的文件访问位置 - - ``pid_t fork(void)`` : fork 当前进程 - - ``pid_t vfork(void)`` : fork 当前进程,与父进程共享 VM,flags,fd - - ``uint64_t brk(uint64_t end_brk)`` : 将堆内存调整为end_brk - - 若end_brk 为 -1,返回堆区域的起始地址 - - 若end_brk 为 -2,返回堆区域的结束地址 - - 否则调整堆区的结束地址域,并返回错误码 - - ``void *sbrk(int64_t increment)`` : - - 将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程) - - increment : 偏移量 - - ``int64_t chdir(char *dest_path)`` - - 切换工作目录(传入目录路径) - - ``int execv(const char* path,char * const argv[])`` : 执行文件 - path : 路径 - argv : 执行参数列表 - - ``extern int usleep(useconds_t usec)`` : 睡眠usec微秒 - diff --git a/docs/userland/libc/apis/index.rst b/docs/userland/libc/apis/index.rst deleted file mode 100644 index 83667c87..00000000 --- a/docs/userland/libc/apis/index.rst +++ /dev/null @@ -1,21 +0,0 @@ -API文档 -==================================== -.. toctree:: - :maxdepth: 1 - :caption: 目录 - - api-list/ctype - api-list/dirent - api-list/errno - api-list/fcntl - api-list/math - api-list/stdio - api-list/printf - api-list/stddef - api-list/stdlib - api-list/string - api-list/time - api-list/unistd - -这里是所有libc头文件的集合,在代码里可以这样引用: -``#include`` diff --git a/docs/userland/libc/design/index.rst b/docs/userland/libc/design/index.rst deleted file mode 100644 index e28e749b..00000000 --- a/docs/userland/libc/design/index.rst +++ /dev/null @@ -1,9 +0,0 @@ -设计文档 -==================================== - -.. toctree:: - :maxdepth: 1 - :caption: 目录 - - -[内容待完善] diff --git a/docs/userland/libc/index.rst b/docs/userland/libc/index.rst deleted file mode 100644 index 3b1d9fb8..00000000 --- a/docs/userland/libc/index.rst +++ /dev/null @@ -1,10 +0,0 @@ -LibC文档 -==================================== - -.. toctree:: - :maxdepth: 1 - :caption: 目录 - - intro - apis/index - design/index diff --git a/docs/userland/libc/intro.md b/docs/userland/libc/intro.md deleted file mode 100644 index bfdd6a1c..00000000 --- a/docs/userland/libc/intro.md +++ /dev/null @@ -1,4 +0,0 @@ -# 简介 -  - LibC是连接用户程序和操作系统的纽带,LibC为应用程序提供了一系列标准库函数。应用程序可以通过DragonOS的LibC,快速地与操作系统进行交互。 - DragonOS的LibC主要依照POSIX 2008规范实现,与Linux下的glibC具有相似之处。 \ No newline at end of file diff --git a/kernel/src/common/crc16.h b/kernel/src/common/crc16.h deleted file mode 100644 index 6e854ee9..00000000 --- a/kernel/src/common/crc16.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -/** - * @brief 计算crc16 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint16_t crc - */ -uint16_t crc16(uint16_t crc, const uint8_t *buffer, size_t len); \ No newline at end of file diff --git a/kernel/src/common/crc32.h b/kernel/src/common/crc32.h deleted file mode 100644 index 6d110882..00000000 --- a/kernel/src/common/crc32.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -/** - * @brief 计算crc32 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint32_t crc - */ -uint32_t crc32(uint32_t crc, const uint8_t *buffer, size_t len); \ No newline at end of file diff --git a/kernel/src/common/crc64.h b/kernel/src/common/crc64.h deleted file mode 100644 index c44573bd..00000000 --- a/kernel/src/common/crc64.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -/** - * @brief 计算crc64 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint64_t crc - */ -uint64_t crc64(uint64_t crc, const uint8_t *buffer, size_t len); \ No newline at end of file diff --git a/kernel/src/common/crc7.h b/kernel/src/common/crc7.h deleted file mode 100644 index 1a146849..00000000 --- a/kernel/src/common/crc7.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -/** - * @brief 计算crc7 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint8_t crc - */ -uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len); \ No newline at end of file diff --git a/kernel/src/common/crc8.h b/kernel/src/common/crc8.h deleted file mode 100644 index 61429489..00000000 --- a/kernel/src/common/crc8.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -/** - * @brief 计算crc8 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint8_t crc - */ -uint8_t crc8(uint8_t crc, const uint8_t *buffer, size_t len); \ No newline at end of file diff --git a/kernel/src/common/kfifo.h b/kernel/src/common/kfifo.h deleted file mode 100644 index 583e4616..00000000 --- a/kernel/src/common/kfifo.h +++ /dev/null @@ -1,153 +0,0 @@ -#pragma once - -#include -#include - -struct kfifo_t -{ - uint32_t total_size; // 缓冲区总空间 - uint32_t size; // 元素所占的字节数 - uint32_t in_offset; // 入口偏移 - uint32_t out_offset; // 出口偏移 - void *buffer; // 缓冲区 -} __attribute__((aligned(sizeof(long)))); - -/** - * @brief 忽略kfifo队列中的所有内容,并把输入和输出偏移量都归零 - * - */ -#define kfifo_reset(fifo) (void)({ \ - (fifo)->size = 0; \ - (fifo)->in_offset = 0; \ - (fifo)->out_offset = 0; \ -}) - -/** - * @brief 忽略kfifo队列中的所有内容,并将输入偏移量赋值给输出偏移量 - * - */ -#define kfifo_reset_out(fifo) (void)({ \ - (fifo)->size = 0; \ - (fifo)->out_offset = (fifo)->in_offset; \ -}) - -/** - * @brief 获取kfifo缓冲区的最大大小 - * - * @param fifo 队列结构体 - * @return uint32_t 缓冲区最大大小 - */ -#define kfifo_total_size(fifo) ((fifo)->total_size) -/** - * @brief 获取kfifo缓冲区当前已使用的大小 - * - * @param fifo 队列结构体 - * @return uint32_t 缓冲区当前已使用的大小 - */ -#define kfifo_size(fifo) ((fifo)->size) - -/** - * @brief 判断kfifo缓冲区当前是否为空 - * - * @param fifo 队列结构体 - * @return uint32_t 0->非空, 1->空 - */ -#define kfifo_empty(fifo) (((fifo)->size == 0) ? 1 : 0) - -/** - * @brief 判断kfifo缓冲区当前是否为满 - * - * @param fifo 队列结构体 - * @return uint32_t 0->不满, 1->满 - */ -#define kfifo_full(fifo) (((fifo)->size == (fifo)->total_size) ? 1 : 0) - -/** - * @brief 通过动态方式初始化kfifo缓冲队列 - * - * @param fifo 队列结构体 - * @param size 缓冲区大小 - * @param reserved 暂时保留,请置为0 - * @return int 错误码:成功->0 - */ -int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved); - -/** - * @brief 释放通过kfifo_alloc创建的fifo缓冲区 - * - * @param fifo fifo队列结构体 - */ -void kfifo_free_alloc(struct kfifo_t *fifo); - -/** - * @brief 使用指定的缓冲区来初始化kfifo缓冲队列 - * - * @param fifo 队列结构体 - * @param buffer 缓冲区 - * @param size 缓冲区大小 - */ -void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size); - -/** - * @brief 向kfifo缓冲区推入指定大小的数据 - * - * @param fifo 队列结构体 - * @param from 来源数据地址 - * @param size 数据大小(字节数) - * @return uint32_t 推入的数据大小 - */ -uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size); - -/** - * @brief 从kfifo缓冲区取出数据,并从队列中删除数据 - * - * @param fifo 队列结构体 - * @param to 拷贝目标地址 - * @param size 数据大小(字节数) - * @return uint32_t 取出的数据大小 - */ -uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size); - -/** - * @brief 从kfifo缓冲区取出数据,但是不从队列中删除数据 - * - * @param fifo 队列结构体 - * @param to 拷贝目标地址 - * @param size 数据大小(字节数) - * @return uint32_t 取出的数据大小 - */ -uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size); - -/** - * @brief 向kfifo缓冲区推入指定大小的数据并在过程加锁 - * - * @param fifo 队列结构体 - * @param from 来源数据地址 - * @param size 数据大小(字节数) - * @param lock 自旋锁 - * @return uint32_t 推入的数据大小 - */ -uint32_t __always_inline kfifo_in_locked(struct kfifo_t *fifo, const void *from, uint32_t size, spinlock_t *lock) -{ - spin_lock(lock); - uint32_t retval = kfifo_in(fifo, from, size); - spin_unlock(lock); - return retval; -} - -/** - * @brief 从kfifo缓冲区取出数据,并从队列中删除数据,并在过程加锁 - * - * @param fifo 队列结构体 - * @param to 拷贝目标地址 - * @param size 数据大小(字节数) - * @param lock 自旋锁 - * @return uint32_t 取出的数据大小 - */ -uint32_t __always_inline kfifo_out_locked(struct kfifo_t *fifo, void *to, uint32_t size, spinlock_t *lock) -{ - spin_lock(lock); - uint32_t retval = kfifo_out(fifo, to, size); - spin_unlock(lock); - return retval; -} diff --git a/kernel/src/common/lockref.h b/kernel/src/common/lockref.h deleted file mode 100644 index 75a31bba..00000000 --- a/kernel/src/common/lockref.h +++ /dev/null @@ -1,104 +0,0 @@ -#pragma once - -#include -#include - -#if ARCH(X86_64) -// 仅在x64架构下启用cmpxchg -#define __LOCKREF_ENABLE_CMPXCHG__ -#endif -struct lockref -{ - union - { -#ifdef __LOCKREF_ENABLE_CMPXCHG__ - aligned_u64 lock_count; // 通过该变量的声明,使得整个lockref按照8字节对齐 -#endif - struct - { - spinlock_t lock; - int count; - }; - }; -}; - -/** - * @brief 原子的将引用计数加1 - * - * @param lock_ref 要被操作的lockref变量 - */ -void lockref_inc(struct lockref *lock_ref); - -/** - * @brief 原子地将引用计数加1.如果原来的count≤0,则操作失败。 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return bool 操作成功=>true - * 操作失败=>false - */ -bool lockref_inc_not_zero(struct lockref *lock_ref); - -/** - * @brief 原子地减少引用计数。如果已处于count≤0的状态,则返回-1 - * - * 本函数与lockref_dec_return()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会再次尝试通过加锁来执行操作 - * 而后者会直接返回错误 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return int 操作成功 => 返回新的引用变量值 - * lockref处于count≤0的状态 => 返回-1 - */ -int lockref_dec(struct lockref *lock_ref); - -/** - * @brief 原子地减少引用计数。如果处于已加锁或count≤0的状态,则返回-1 - * - * 本函数与lockref_dec()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会直接返回错误 - * 而后者会再次尝试通过加锁来执行操作 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return int 操作成功 => 返回新的引用变量值 - * lockref处于已加锁或count≤0的状态 => 返回-1 - */ -int lockref_dec_return(struct lockref *lock_ref); - - -/** - * @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败 - * - * 该函数与lockref_dec_or_lock_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会直接返回false. - * 而后者在这种情况下,会尝试加锁来进行操作。 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return true 成功将引用计数减1 - * @return false 如果当前的引用计数≤1,操作失败 - */ -bool lockref_dec_not_zero(struct lockref *lock_ref); - -/** - * @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败 - * - * 该函数与lockref_dec_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会尝试加锁来进行操作。 - * 而后者在这种情况下,会直接返回false. - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return true 成功将引用计数减1 - * @return false 如果当前的引用计数≤1,操作失败 - */ -bool lockref_dec_or_lock_not_zero(struct lockref *lock_ref); - -/** - * @brief 将lockref变量标记为已经死亡(将count设置为负值) - * - * @param lock_ref 指向要被操作的lockref变量的指针 - */ -void lockref_mark_dead(struct lockref * lock_ref); - -/** - * @brief 自增引用计数。(除非该lockref已经死亡) - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return true 操作成功 - * @return false 操作失败,lockref已死亡 - */ -bool lockref_inc_not_dead(struct lockref *lock_ref); \ No newline at end of file diff --git a/kernel/src/common/lz4.h b/kernel/src/common/lz4.h deleted file mode 100644 index 26525304..00000000 --- a/kernel/src/common/lz4.h +++ /dev/null @@ -1,771 +0,0 @@ -/* - * LZ4 - Fast LZ compression algorithm - * Header File - * Copyright (C) 2011-present, Yann Collet. - - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - LZ4 homepage : http://www.lz4.org - - LZ4 source repository : https://github.com/lz4/lz4 -*/ -#if defined(__cplusplus) -extern "C" -{ -#endif - -#ifndef LZ4_H_2983827168210 -#define LZ4_H_2983827168210 - -/* --- Dependency --- */ -#include /* size_t */ - -/** - Introduction - - LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core, - scalable with multi-cores CPU. It features an extremely fast decoder, with speed in - multiple GB/s per core, typically reaching RAM speed limits on multi-core systems. - - The LZ4 compression library provides in-memory compression and decompression functions. - It gives full buffer control to user. - Compression can be done in: - - a single step (described as Simple Functions) - - a single step, reusing a context (described in Advanced Functions) - - unbounded multiple steps (described as Streaming compression) - - lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md). - Decompressing such a compressed block requires additional metadata. - Exact metadata depends on exact decompression function. - For the typical case of LZ4_decompress_safe(), - metadata includes block's compressed size, and maximum bound of decompressed size. - Each application is free to encode and pass such metadata in whichever way it wants. - - lz4.h only handle blocks, it can not generate Frames. - - Blocks are different from Frames (doc/lz4_Frame_format.md). - Frames bundle both blocks and metadata in a specified manner. - Embedding metadata is required for compressed data to be self-contained and portable. - Frame format is delivered through a companion API, declared in lz4frame.h. - The `lz4` CLI can only manage frames. -*/ - -/*^*************************************************************** - * Export parameters - *****************************************************************/ -/* - * LZ4_DLL_EXPORT : - * Enable exporting of functions when building a Windows DLL - * LZ4LIB_VISIBILITY : - * Control library symbols visibility. - */ -#ifndef LZ4LIB_VISIBILITY -#if defined(__GNUC__) && (__GNUC__ >= 4) -#define LZ4LIB_VISIBILITY __attribute__((visibility("default"))) -#else -#define LZ4LIB_VISIBILITY -#endif -#endif -#if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT == 1) -#define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY -#elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT == 1) -#define LZ4LIB_API __declspec(dllimport) LZ4LIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ -#else -#define LZ4LIB_API LZ4LIB_VISIBILITY -#endif - -/*------ Version ------*/ -#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ -#define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */ -#define LZ4_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */ - -#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR * 100 * 100 + LZ4_VERSION_MINOR * 100 + LZ4_VERSION_RELEASE) - -#define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE -#define LZ4_QUOTE(str) #str -#define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str) -#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) - - LZ4LIB_API int LZ4_versionNumber(void); /**< library version number; useful to check dll version */ - LZ4LIB_API const char *LZ4_versionString(void); /**< library version string; useful to check dll version */ - -/*-************************************ - * Tuning parameter - **************************************/ -/*! - * LZ4_MEMORY_USAGE : - * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) - * Increasing memory usage improves compression ratio. - * Reduced memory usage may improve speed, thanks to better cache locality. - * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache - */ -#ifndef LZ4_MEMORY_USAGE -#define LZ4_MEMORY_USAGE 14 -#endif - - /*-************************************ - * Simple Functions - **************************************/ - /*! LZ4_compress_default() : - * Compresses 'srcSize' bytes from buffer 'src' - * into already allocated 'dst' buffer of size 'dstCapacity'. - * Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize). - * It also runs faster, so it's a recommended setting. - * If the function cannot compress 'src' into a more limited 'dst' budget, - * compression stops *immediately*, and the function result is zero. - * In which case, 'dst' content is undefined (invalid). - * srcSize : max supported value is LZ4_MAX_INPUT_SIZE. - * dstCapacity : size of buffer 'dst' (which must be already allocated) - * @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) - * or 0 if compression fails - * Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). - */ - LZ4LIB_API int LZ4_compress_default(const char *src, char *dst, int srcSize, int dstCapacity); - - /*! LZ4_decompress_safe() : - * compressedSize : is the exact complete size of the compressed block. - * dstCapacity : is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size. - * @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) - * If destination buffer is not large enough, decoding will stop and output an error code (negative value). - * If the source stream is detected malformed, the function will stop decoding and return a negative result. - * Note 1 : This function is protected against malicious data packets : - * it will never writes outside 'dst' buffer, nor read outside 'source' buffer, - * even if the compressed block is maliciously modified to order the decoder to do these actions. - * In such case, the decoder stops immediately, and considers the compressed block malformed. - * Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them. - * The implementation is free to send / store / derive this information in whichever way is most beneficial. - * If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead. - */ - LZ4LIB_API int LZ4_decompress_safe(const char *src, char *dst, int compressedSize, int dstCapacity); - -/*-************************************ - * Advanced Functions - **************************************/ -#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ -#define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize) / 255) + 16) - - /*! LZ4_compressBound() : - Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) - This function is primarily useful for memory allocation purposes (destination buffer size). - Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). - Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize) - inputSize : max supported value is LZ4_MAX_INPUT_SIZE - return : maximum output size in a "worst case" scenario - or 0, if input size is incorrect (too large or negative) - */ - LZ4LIB_API int LZ4_compressBound(int inputSize); - - /*! LZ4_compress_fast() : - Same as LZ4_compress_default(), but allows selection of "acceleration" factor. - The larger the acceleration value, the faster the algorithm, but also the lesser the compression. - It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. - An acceleration value of "1" is the same as regular LZ4_compress_default() - Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). - Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c). - */ - LZ4LIB_API int LZ4_compress_fast(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration); - - /*! LZ4_compress_fast_extState() : - * Same as LZ4_compress_fast(), using an externally allocated memory space for its state. - * Use LZ4_sizeofState() to know how much memory must be allocated, - * and allocate it on 8-bytes boundaries (using `malloc()` typically). - * Then, provide this buffer as `void* state` to compression function. - */ - LZ4LIB_API int LZ4_sizeofState(void); - LZ4LIB_API int LZ4_compress_fast_extState(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration); - - /*! LZ4_compress_destSize() : - * Reverse the logic : compresses as much data as possible from 'src' buffer - * into already allocated buffer 'dst', of size >= 'targetDestSize'. - * This function either compresses the entire 'src' content into 'dst' if it's large enough, - * or fill 'dst' buffer completely with as much data as possible from 'src'. - * note: acceleration parameter is fixed to "default". - * - * *srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'. - * New value is necessarily <= input value. - * @return : Nb bytes written into 'dst' (necessarily <= targetDestSize) - * or 0 if compression fails. - * - * Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+): - * the produced compressed content could, in specific circumstances, - * require to be decompressed into a destination buffer larger - * by at least 1 byte than the content to decompress. - * If an application uses `LZ4_compress_destSize()`, - * it's highly recommended to update liblz4 to v1.9.2 or better. - * If this can't be done or ensured, - * the receiving decompression function should provide - * a dstCapacity which is > decompressedSize, by at least 1 byte. - * See https://github.com/lz4/lz4/issues/859 for details - */ - LZ4LIB_API int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize); - - /*! LZ4_decompress_safe_partial() : - * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', - * into destination buffer 'dst' of size 'dstCapacity'. - * Up to 'targetOutputSize' bytes will be decoded. - * The function stops decoding on reaching this objective. - * This can be useful to boost performance - * whenever only the beginning of a block is required. - * - * @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) - * If source stream is detected malformed, function returns a negative result. - * - * Note 1 : @return can be < targetOutputSize, if compressed block contains less data. - * - * Note 2 : targetOutputSize must be <= dstCapacity - * - * Note 3 : this function effectively stops decoding on reaching targetOutputSize, - * so dstCapacity is kind of redundant. - * This is because in older versions of this function, - * decoding operation would still write complete sequences. - * Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, - * it could write more bytes, though only up to dstCapacity. - * Some "margin" used to be required for this operation to work properly. - * Thankfully, this is no longer necessary. - * The function nonetheless keeps the same signature, in an effort to preserve API compatibility. - * - * Note 4 : If srcSize is the exact size of the block, - * then targetOutputSize can be any value, - * including larger than the block's decompressed size. - * The function will, at most, generate block's decompressed size. - * - * Note 5 : If srcSize is _larger_ than block's compressed size, - * then targetOutputSize **MUST** be <= block's decompressed size. - * Otherwise, *silent corruption will occur*. - */ - LZ4LIB_API int LZ4_decompress_safe_partial(const char *src, char *dst, int srcSize, int targetOutputSize, int dstCapacity); - - /*-********************************************* - * Streaming Compression Functions - ***********************************************/ - typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */ - - LZ4LIB_API LZ4_stream_t *LZ4_createStream(void); - LZ4LIB_API int LZ4_freeStream(LZ4_stream_t *streamPtr); - - /*! LZ4_resetStream_fast() : v1.9.0+ - * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks - * (e.g., LZ4_compress_fast_continue()). - * - * An LZ4_stream_t must be initialized once before usage. - * This is automatically done when created by LZ4_createStream(). - * However, should the LZ4_stream_t be simply declared on stack (for example), - * it's necessary to initialize it first, using LZ4_initStream(). - * - * After init, start any new stream with LZ4_resetStream_fast(). - * A same LZ4_stream_t can be re-used multiple times consecutively - * and compress multiple streams, - * provided that it starts each new stream with LZ4_resetStream_fast(). - * - * LZ4_resetStream_fast() is much faster than LZ4_initStream(), - * but is not compatible with memory regions containing garbage data. - * - * Note: it's only useful to call LZ4_resetStream_fast() - * in the context of streaming compression. - * The *extState* functions perform their own resets. - * Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive. - */ - LZ4LIB_API void LZ4_resetStream_fast(LZ4_stream_t *streamPtr); - - /*! LZ4_loadDict() : - * Use this function to reference a static dictionary into LZ4_stream_t. - * The dictionary must remain available during compression. - * LZ4_loadDict() triggers a reset, so any previous data will be forgotten. - * The same dictionary will have to be loaded on decompression side for successful decoding. - * Dictionary are useful for better compression of small data (KB range). - * While LZ4 accept any input as dictionary, - * results are generally better when using Zstandard's Dictionary Builder. - * Loading a size of 0 is allowed, and is the same as reset. - * @return : loaded dictionary size, in bytes (necessarily <= 64 KB) - */ - LZ4LIB_API int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary, int dictSize); - - /*! LZ4_compress_fast_continue() : - * Compress 'src' content using data from previously compressed blocks, for better compression ratio. - * 'dst' buffer must be already allocated. - * If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. - * - * @return : size of compressed block - * or 0 if there is an error (typically, cannot fit into 'dst'). - * - * Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block. - * Each block has precise boundaries. - * Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. - * It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together. - * - * Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory ! - * - * Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. - * Make sure that buffers are separated, by at least one byte. - * This construction ensures that each block only depends on previous block. - * - * Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB. - * - * Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed. - */ - LZ4LIB_API int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration); - - /*! LZ4_saveDict() : - * If last 64KB data cannot be guaranteed to remain available at its current memory location, - * save it into a safer place (char* safeBuffer). - * This is schematically equivalent to a memcpy() followed by LZ4_loadDict(), - * but is much faster, because LZ4_saveDict() doesn't need to rebuild tables. - * @return : saved dictionary size in bytes (necessarily <= maxDictSize), or 0 if error. - */ - LZ4LIB_API int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int maxDictSize); - - /*-********************************************** - * Streaming Decompression Functions - * Bufferless synchronous API - ************************************************/ - typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */ - - /*! LZ4_createStreamDecode() and LZ4_freeStreamDecode() : - * creation / destruction of streaming decompression tracking context. - * A tracking context can be re-used multiple times. - */ - LZ4LIB_API LZ4_streamDecode_t *LZ4_createStreamDecode(void); - LZ4LIB_API int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream); - - /*! LZ4_setStreamDecode() : - * An LZ4_streamDecode_t context can be allocated once and re-used multiple times. - * Use this function to start decompression of a new stream of blocks. - * A dictionary can optionally be set. Use NULL or size 0 for a reset order. - * Dictionary is presumed stable : it must remain accessible and unmodified during next decompression. - * @return : 1 if OK, 0 if error - */ - LZ4LIB_API int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize); - - /*! LZ4_decoderRingBufferSize() : v1.8.2+ - * Note : in a ring buffer scenario (optional), - * blocks are presumed decompressed next to each other - * up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize), - * at which stage it resumes from beginning of ring buffer. - * When setting such a ring buffer for streaming decompression, - * provides the minimum size of this ring buffer - * to be compatible with any source respecting maxBlockSize condition. - * @return : minimum ring buffer size, - * or 0 if there is an error (invalid maxBlockSize). - */ - LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize); -#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */ - - /*! LZ4_decompress_*_continue() : - * These decoding functions allow decompression of consecutive blocks in "streaming" mode. - * A block is an unsplittable entity, it must be presented entirely to a decompression function. - * Decompression functions only accepts one block at a time. - * The last 64KB of previously decoded data *must* remain available and unmodified at the memory position where they were decoded. - * If less than 64KB of data has been decoded, all the data must be present. - * - * Special : if decompression side sets a ring buffer, it must respect one of the following conditions : - * - Decompression buffer size is _at least_ LZ4_decoderRingBufferSize(maxBlockSize). - * maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes. - * In which case, encoding and decoding buffers do not need to be synchronized. - * Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize. - * - Synchronized mode : - * Decompression buffer size is _exactly_ the same as compression buffer size, - * and follows exactly same update rule (block boundaries at same positions), - * and decoding function is provided with exact decompressed size of each block (exception for last block of the stream), - * _then_ decoding & encoding ring buffer can have any size, including small ones ( < 64 KB). - * - Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes. - * In which case, encoding and decoding buffers do not need to be synchronized, - * and encoding ring buffer can have any size, including small ones ( < 64 KB). - * - * Whenever these conditions are not possible, - * save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression, - * then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block. - */ - LZ4LIB_API int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int srcSize, int dstCapacity); - - /*! LZ4_decompress_*_usingDict() : - * These decoding functions work the same as - * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue() - * They are stand-alone, and don't need an LZ4_streamDecode_t structure. - * Dictionary is presumed stable : it must remain accessible and unmodified during decompression. - * Performance tip : Decompression speed can be substantially increased - * when dst == dictStart + dictSize. - */ - LZ4LIB_API int LZ4_decompress_safe_usingDict(const char *src, char *dst, int srcSize, int dstCapcity, const char *dictStart, int dictSize); - -#endif /* LZ4_H_2983827168210 */ - - /*^************************************* - * !!!!!! STATIC LINKING ONLY !!!!!! - ***************************************/ - - /*-**************************************************************************** - * Experimental section - * - * Symbols declared in this section must be considered unstable. Their - * signatures or semantics may change, or they may be removed altogether in the - * future. They are therefore only safe to depend on when the caller is - * statically linked against the library. - * - * To protect against unsafe usage, not only are the declarations guarded, - * the definitions are hidden by default - * when building LZ4 as a shared/dynamic library. - * - * In order to access these declarations, - * define LZ4_STATIC_LINKING_ONLY in your application - * before including LZ4's headers. - * - * In order to make their implementations accessible dynamically, you must - * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library. - ******************************************************************************/ - -#ifdef LZ4_STATIC_LINKING_ONLY - -#ifndef LZ4_STATIC_3504398509 -#define LZ4_STATIC_3504398509 - -#ifdef LZ4_PUBLISH_STATIC_FUNCTIONS -#define LZ4LIB_STATIC_API LZ4LIB_API -#else -#define LZ4LIB_STATIC_API -#endif - - /*! LZ4_compress_fast_extState_fastReset() : - * A variant of LZ4_compress_fast_extState(). - * - * Using this variant avoids an expensive initialization step. - * It is only safe to call if the state buffer is known to be correctly initialized already - * (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized"). - * From a high level, the difference is that - * this function initializes the provided state with a call to something like LZ4_resetStream_fast() - * while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream(). - */ - LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration); - - /*! LZ4_attach_dictionary() : - * This is an experimental API that allows - * efficient use of a static dictionary many times. - * - * Rather than re-loading the dictionary buffer into a working context before - * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a - * working LZ4_stream_t, this function introduces a no-copy setup mechanism, - * in which the working stream references the dictionary stream in-place. - * - * Several assumptions are made about the state of the dictionary stream. - * Currently, only streams which have been prepared by LZ4_loadDict() should - * be expected to work. - * - * Alternatively, the provided dictionaryStream may be NULL, - * in which case any existing dictionary stream is unset. - * - * If a dictionary is provided, it replaces any pre-existing stream history. - * The dictionary contents are the only history that can be referenced and - * logically immediately precede the data compressed in the first subsequent - * compression call. - * - * The dictionary will only remain attached to the working stream through the - * first compression call, at the end of which it is cleared. The dictionary - * stream (and source buffer) must remain in-place / accessible / unchanged - * through the completion of the first compression call on the stream. - */ - LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t *workingStream, const LZ4_stream_t *dictionaryStream); - - /*! In-place compression and decompression - * - * It's possible to have input and output sharing the same buffer, - * for highly contrained memory environments. - * In both cases, it requires input to lay at the end of the buffer, - * and decompression to start at beginning of the buffer. - * Buffer size must feature some margin, hence be larger than final size. - * - * |<------------------------buffer--------------------------------->| - * |<-----------compressed data--------->| - * |<-----------decompressed size------------------>| - * |<----margin---->| - * - * This technique is more useful for decompression, - * since decompressed size is typically larger, - * and margin is short. - * - * In-place decompression will work inside any buffer - * which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize). - * This presumes that decompressedSize > compressedSize. - * Otherwise, it means compression actually expanded data, - * and it would be more efficient to store such data with a flag indicating it's not compressed. - * This can happen when data is not compressible (already compressed, or encrypted). - * - * For in-place compression, margin is larger, as it must be able to cope with both - * history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX, - * and data expansion, which can happen when input is not compressible. - * As a consequence, buffer size requirements are much higher, - * and memory savings offered by in-place compression are more limited. - * - * There are ways to limit this cost for compression : - * - Reduce history size, by modifying LZ4_DISTANCE_MAX. - * Note that it is a compile-time constant, so all compressions will apply this limit. - * Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, - * so it's a reasonable trick when inputs are known to be small. - * - Require the compressor to deliver a "maximum compressed size". - * This is the `dstCapacity` parameter in `LZ4_compress*()`. - * When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, - * in which case, the return code will be 0 (zero). - * The caller must be ready for these cases to happen, - * and typically design a backup scheme to send data uncompressed. - * The combination of both techniques can significantly reduce - * the amount of margin required for in-place compression. - * - * In-place compression can work in any buffer - * which size is >= (maxCompressedSize) - * with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success. - * LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX, - * so it's possible to reduce memory requirements by playing with them. - */ - -#define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize) (((compressedSize) >> 8) + 32) -#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)) /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */ - -#ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */ -#define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ -#endif - -#define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32) /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */ -#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN) /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */ - -#endif /* LZ4_STATIC_3504398509 */ -#endif /* LZ4_STATIC_LINKING_ONLY */ - -#ifndef LZ4_H_98237428734687 -#define LZ4_H_98237428734687 - -/*-************************************************************ - * Private Definitions - ************************************************************** - * Do not use these definitions directly. - * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. - * Accessing members will expose user code to API and/or ABI break in future versions of the library. - **************************************************************/ -#define LZ4_HASHLOG (LZ4_MEMORY_USAGE - 2) -#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) -#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */ - -#if defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) -#include - typedef int8_t LZ4_i8; - typedef uint8_t LZ4_byte; - typedef uint16_t LZ4_u16; - typedef uint32_t LZ4_u32; -#else - typedef signed char LZ4_i8; - typedef unsigned char LZ4_byte; - typedef unsigned short LZ4_u16; - typedef unsigned int LZ4_u32; -#endif - - typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; - struct LZ4_stream_t_internal - { - LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]; - LZ4_u32 currentOffset; - LZ4_u32 tableType; - const LZ4_byte *dictionary; - const LZ4_stream_t_internal *dictCtx; - LZ4_u32 dictSize; - }; - - typedef struct - { - const LZ4_byte *externalDict; - size_t extDictSize; - const LZ4_byte *prefixEnd; - size_t prefixSize; - } LZ4_streamDecode_t_internal; - -/*! LZ4_stream_t : - * Do not use below internal definitions directly ! - * Declare or allocate an LZ4_stream_t instead. - * LZ4_stream_t can also be created using LZ4_createStream(), which is recommended. - * The structure definition can be convenient for static allocation - * (on stack, or as part of larger structure). - * Init this structure with LZ4_initStream() before first use. - * note : only use this definition in association with static linking ! - * this definition is not API/ABI safe, and may change in future versions. - */ -#define LZ4_STREAMSIZE 16416 /* static size, for inter-version compatibility */ -#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void *)) - union LZ4_stream_u - { - void *table[LZ4_STREAMSIZE_VOIDP]; - LZ4_stream_t_internal internal_donotuse; - }; /* previously typedef'd to LZ4_stream_t */ - - /*! LZ4_initStream() : v1.9.0+ - * An LZ4_stream_t structure must be initialized at least once. - * This is automatically done when invoking LZ4_createStream(), - * but it's not when the structure is simply declared on stack (for example). - * - * Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. - * It can also initialize any arbitrary buffer of sufficient size, - * and will @return a pointer of proper type upon initialization. - * - * Note : initialization fails if size and alignment conditions are not respected. - * In which case, the function will @return NULL. - * Note2: An LZ4_stream_t structure guarantees correct alignment and size. - * Note3: Before v1.9.0, use LZ4_resetStream() instead - */ - LZ4LIB_API LZ4_stream_t *LZ4_initStream(void *buffer, size_t size); - -/*! LZ4_streamDecode_t : - * information structure to track an LZ4 stream during decompression. - * init this structure using LZ4_setStreamDecode() before first use. - * note : only use in association with static linking ! - * this definition is not API/ABI safe, - * and may change in a future version ! - */ -#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void *) == 16) ? 2 : 0) /*AS-400*/) -#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) - union LZ4_streamDecode_u - { - unsigned long long table[LZ4_STREAMDECODESIZE_U64]; - LZ4_streamDecode_t_internal internal_donotuse; - }; /* previously typedef'd to LZ4_streamDecode_t */ - -/*-************************************ - * Obsolete Functions - **************************************/ - -/*! Deprecation warnings - * - * Deprecated functions make the compiler generate a warning when invoked. - * This is meant to invite users to update their source code. - * Should deprecation warnings be a problem, it is generally possible to disable them, - * typically with -Wno-deprecated-declarations for gcc - * or _CRT_SECURE_NO_WARNINGS in Visual. - * - * Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS - * before including the header file. - */ -#ifdef LZ4_DISABLE_DEPRECATE_WARNINGS -#define LZ4_DEPRECATED(message) /* disable deprecation warnings */ -#else -#if defined(__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ -#define LZ4_DEPRECATED(message) [[deprecated(message)]] -#elif defined(_MSC_VER) -#define LZ4_DEPRECATED(message) __declspec(deprecated(message)) -#elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45)) -#define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) -#elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31) -#define LZ4_DEPRECATED(message) __attribute__((deprecated)) -#else -#pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler") -#define LZ4_DEPRECATED(message) /* disabled */ -#endif -#endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ - - /*! Obsolete compression functions (since v1.7.3) */ - LZ4_DEPRECATED("use LZ4_compress_default() instead") - LZ4LIB_API int LZ4_compress(const char *src, char *dest, int srcSize); - LZ4_DEPRECATED("use LZ4_compress_default() instead") - LZ4LIB_API int LZ4_compress_limitedOutput(const char *src, char *dest, int srcSize, int maxOutputSize); - LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") - LZ4LIB_API int LZ4_compress_withState(void *state, const char *source, char *dest, int inputSize); - LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") - LZ4LIB_API int LZ4_compress_limitedOutput_withState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize); - LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") - LZ4LIB_API int LZ4_compress_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize); - LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") - LZ4LIB_API int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize, int maxOutputSize); - - /*! Obsolete decompression functions (since v1.8.0) */ - LZ4_DEPRECATED("use LZ4_decompress_fast() instead") - LZ4LIB_API int LZ4_uncompress(const char *source, char *dest, int outputSize); - LZ4_DEPRECATED("use LZ4_decompress_safe() instead") - LZ4LIB_API int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize, int maxOutputSize); - - /* Obsolete streaming functions (since v1.7.0) - * degraded functionality; do not use! - * - * In order to perform streaming compression, these functions depended on data - * that is no longer tracked in the state. They have been preserved as well as - * possible: using them will still produce a correct output. However, they don't - * actually retain any history between compression calls. The compression ratio - * achieved will therefore be no better than compressing each chunk - * independently. - */ - LZ4_DEPRECATED("Use LZ4_createStream() instead") - LZ4LIB_API void *LZ4_create(char *inputBuffer); - LZ4_DEPRECATED("Use LZ4_createStream() instead") - LZ4LIB_API int LZ4_sizeofStreamState(void); - LZ4_DEPRECATED("Use LZ4_resetStream() instead") - LZ4LIB_API int LZ4_resetStreamState(void *state, char *inputBuffer); - LZ4_DEPRECATED("Use LZ4_saveDict() instead") - LZ4LIB_API char *LZ4_slideInputBuffer(void *state); - - /*! Obsolete streaming decoding functions (since v1.7.0) */ - LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") - LZ4LIB_API int LZ4_decompress_safe_withPrefix64k(const char *src, char *dst, int compressedSize, int maxDstSize); - LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") - LZ4LIB_API int LZ4_decompress_fast_withPrefix64k(const char *src, char *dst, int originalSize); - - /*! Obsolete LZ4_decompress_fast variants (since v1.9.0) : - * These functions used to be faster than LZ4_decompress_safe(), - * but this is no longer the case. They are now slower. - * This is because LZ4_decompress_fast() doesn't know the input size, - * and therefore must progress more cautiously into the input buffer to not read beyond the end of block. - * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. - * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. - * - * The last remaining LZ4_decompress_fast() specificity is that - * it can decompress a block without knowing its compressed size. - * Such functionality can be achieved in a more secure manner - * by employing LZ4_decompress_safe_partial(). - * - * Parameters: - * originalSize : is the uncompressed size to regenerate. - * `dst` must be already allocated, its size must be >= 'originalSize' bytes. - * @return : number of bytes read from source buffer (== compressed size). - * The function expects to finish at block's end exactly. - * If the source stream is detected malformed, the function stops decoding and returns a negative result. - * note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. - * However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds. - * Also, since match offsets are not validated, match reads from 'src' may underflow too. - * These issues never happen if input (compressed) data is correct. - * But they may happen if input data is invalid (error or intentional tampering). - * As a consequence, use these functions in trusted environments with trusted data **only**. - */ - LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") - LZ4LIB_API int LZ4_decompress_fast(const char *src, char *dst, int originalSize); - LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") - LZ4LIB_API int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int originalSize); - LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") - LZ4LIB_API int LZ4_decompress_fast_usingDict(const char *src, char *dst, int originalSize, const char *dictStart, int dictSize); - - /*! LZ4_resetStream() : - * An LZ4_stream_t structure must be initialized at least once. - * This is done with LZ4_initStream(), or LZ4_resetStream(). - * Consider switching to LZ4_initStream(), - * invoking LZ4_resetStream() will trigger deprecation warnings in the future. - */ - LZ4LIB_API void LZ4_resetStream(LZ4_stream_t *streamPtr); - -#endif /* LZ4_H_98237428734687 */ - -#if defined(__cplusplus) -} -#endif \ No newline at end of file diff --git a/kernel/src/common/math/fabs.c b/kernel/src/common/math/fabs.c deleted file mode 100644 index 9eb5374c..00000000 --- a/kernel/src/common/math/fabs.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include "libm.h" - -double fabs(double x) -{ - union - { - double f; - uint64_t i; - } u = {x}; - u.i &= -1ULL / 2; - return u.f; -} - - -#if __LDBL_MANT_DIG__ == 53 && __LDBL_MAX_EXP__ == 1024 -long double fabsl(long double x) -{ - return fabs(x); -} -#elif (__LDBL_MANT_DIG__ == 64 || __LDBL_MANT_DIG__ == 113) && __LDBL_MAX_EXP__ == 16384 -long double fabsl(long double x) -{ - union ldshape u = {x}; - - u.i.se &= 0x7fff; - return u.f; -} -#endif \ No newline at end of file diff --git a/kernel/src/common/numa.h b/kernel/src/common/numa.h deleted file mode 100644 index 0394db59..00000000 --- a/kernel/src/common/numa.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#define NUMA_NO_NODE (-1) \ No newline at end of file diff --git a/kernel/src/common/stdlib.h b/kernel/src/common/stdlib.h deleted file mode 100644 index 68bfc045..00000000 --- a/kernel/src/common/stdlib.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -/** - * @brief 将长整型转换为字符串 - * - * @param input 输入的数据 - * @return const char* 结果字符串 - */ -const char* ltoa(long input); \ No newline at end of file diff --git a/kernel/src/common/sys/wait.h b/kernel/src/common/sys/wait.h deleted file mode 100644 index 8afb9a42..00000000 --- a/kernel/src/common/sys/wait.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "types.h" - -/** - * @brief 等待指定pid的子进程退出 - * - * @param pid 子进程的pid - * @param stat_loc 返回的子进程结束状态 - * @param options 额外的控制选项 - * @return pid_t - */ -pid_t waitpid(pid_t pid, int *stat_loc, int options); \ No newline at end of file diff --git a/kernel/src/include/DragonOS/refcount.h b/kernel/src/include/DragonOS/refcount.h deleted file mode 100644 index 64dcbafa..00000000 --- a/kernel/src/include/DragonOS/refcount.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#if ARCH(I386) || ARCH(X86_64) - -#include - -// 该结构体需要与libs/refcount.rs的保持一致,且以rust版本为准 -typedef struct refcount_struct { - atomic_t refs; -} refcount_t; - -#endif \ No newline at end of file diff --git a/kernel/src/include/bindings/wrapper.h b/kernel/src/include/bindings/wrapper.h index 1b03fdde..386aa1a8 100644 --- a/kernel/src/include/bindings/wrapper.h +++ b/kernel/src/include/bindings/wrapper.h @@ -11,14 +11,7 @@ #pragma once -#include -#include -#include -#include -#include #include -#include -#include #include #include #include @@ -26,7 +19,6 @@ #include #include #include -#include #include #include #include diff --git a/kernel/src/libs/Makefile b/kernel/src/libs/Makefile index ddcc98b7..c6544118 100644 --- a/kernel/src/libs/Makefile +++ b/kernel/src/libs/Makefile @@ -1,7 +1,7 @@ CFLAGS += -I . -kernel_lib_subdirs:= sys +kernel_lib_subdirs:= kernel_lib_objs:= $(shell find ./*.c) diff --git a/kernel/src/libs/crc16.c b/kernel/src/libs/crc16.c deleted file mode 100644 index ac21ddd2..00000000 --- a/kernel/src/libs/crc16.c +++ /dev/null @@ -1,41 +0,0 @@ -#include - -// Polynomial=0x8005 -// Initial Value=0x0 -// Final Xor Value=0x0 - -/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */ -uint16_t const crc16_table[256] = { - 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011, 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022, - 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072, 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041, - 0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2, 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1, - 0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1, 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082, - 0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192, 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1, - 0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1, 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2, - 0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151, 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162, - 0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132, 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101, - 0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312, 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321, - 0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371, 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342, - 0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1, 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2, - 0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2, 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381, - 0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291, 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2, - 0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2, 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1, - 0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252, 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261, - 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231, 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202}; - -/** - * @brief 计算crc16 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint16_t crc - */ -uint16_t crc16(uint16_t crc, uint8_t const *buffer, size_t len) -{ - while (len--) - { - crc = (crc << 8) ^ crc16_table[((crc >> 8) ^ *buffer++) & 0xff]; - } - return crc; -} diff --git a/kernel/src/libs/crc32.c b/kernel/src/libs/crc32.c deleted file mode 100644 index 14bb13e8..00000000 --- a/kernel/src/libs/crc32.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -// Polynomial=0x4C11DB7 -// Initial Value=0x0 -// Final Xor Value:0x0 - -/** CRC table for the CRC-32. The poly is 0x4C11DB7 */ -uint32_t const crc32_table[256] = { - 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005, - 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61, 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, - 0x4C11DB70, 0x48D0C6C7, 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75, - 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3, 0x709F7B7A, 0x745E66CD, - 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039, 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, - 0xBE2B5B58, 0xBAEA46EF, 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D, - 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB, 0xCEB42022, 0xCA753D95, - 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1, 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, - 0x34867077, 0x30476DC0, 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072, - 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4, 0x0808D07D, 0x0CC9CDCA, - 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE, 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, - 0x5E9F46BF, 0x5A5E5B08, 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA, - 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC, 0xB6238B25, 0xB2E29692, - 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6, 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, - 0xE0B41DE7, 0xE4750050, 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2, - 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34, 0xDC3ABDED, 0xD8FBA05A, - 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637, 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, - 0x4F040D56, 0x4BC510E1, 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53, - 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5, 0x3F9B762C, 0x3B5A6B9B, - 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF, 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, - 0xF12F560E, 0xF5EE4BB9, 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B, - 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD, 0xCDA1F604, 0xC960EBB3, - 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7, 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, - 0x9B3660C6, 0x9FF77D71, 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3, - 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2, 0x470CDD2B, 0x43CDC09C, - 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8, 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, - 0x119B4BE9, 0x155A565E, 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC, - 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A, 0x2D15EBE3, 0x29D4F654, - 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0, 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, - 0xE3A1CBC1, 0xE760D676, 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4, - 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662, 0x933EB0BB, 0x97FFAD0C, - 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4}; - -/** - * @brief 计算crc32 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint32_t crc - */ -uint32_t crc32(uint32_t crc, uint8_t const *buffer, size_t len) -{ - while (len--) - { - crc = (crc << 8) ^ (crc32_table[((crc >> 24) ^ *buffer++) & 0xff]); - } - return crc; -} diff --git a/kernel/src/libs/crc64.c b/kernel/src/libs/crc64.c deleted file mode 100644 index 9d966d42..00000000 --- a/kernel/src/libs/crc64.c +++ /dev/null @@ -1,57 +0,0 @@ -#include - -// Polynomial =0x42F0E1EBA9EA3693 -// Initial Value=0x0 -// Final Xor Value=0x0 -uint64_t const crc64_table[256] = { - 0x0000000000000000, 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, 0xC711223CFA3E5BB5, 0x493366450E42ECDF, 0x0BC387AEA7A8DA4C, 0xCCD2A5925D9681F9, 0x8E224479F47CB76A, - 0x9266CC8A1C85D9BE, 0xD0962D61B56FEF2D, 0x17870F5D4F51B498, 0x5577EEB6E6BB820B, 0xDB55AACF12C73561, 0x99A54B24BB2D03F2, 0x5EB4691841135847, 0x1C4488F3E8F96ED4, - 0x663D78FF90E185EF, 0x24CD9914390BB37C, 0xE3DCBB28C335E8C9, 0xA12C5AC36ADFDE5A, 0x2F0E1EBA9EA36930, 0x6DFEFF5137495FA3, 0xAAEFDD6DCD770416, 0xE81F3C86649D3285, - 0xF45BB4758C645C51, 0xB6AB559E258E6AC2, 0x71BA77A2DFB03177, 0x334A9649765A07E4, 0xBD68D2308226B08E, 0xFF9833DB2BCC861D, 0x388911E7D1F2DDA8, 0x7A79F00C7818EB3B, - 0xCC7AF1FF21C30BDE, 0x8E8A101488293D4D, 0x499B3228721766F8, 0x0B6BD3C3DBFD506B, 0x854997BA2F81E701, 0xC7B97651866BD192, 0x00A8546D7C558A27, 0x4258B586D5BFBCB4, - 0x5E1C3D753D46D260, 0x1CECDC9E94ACE4F3, 0xDBFDFEA26E92BF46, 0x990D1F49C77889D5, 0x172F5B3033043EBF, 0x55DFBADB9AEE082C, 0x92CE98E760D05399, 0xD03E790CC93A650A, - 0xAA478900B1228E31, 0xE8B768EB18C8B8A2, 0x2FA64AD7E2F6E317, 0x6D56AB3C4B1CD584, 0xE374EF45BF6062EE, 0xA1840EAE168A547D, 0x66952C92ECB40FC8, 0x2465CD79455E395B, - 0x3821458AADA7578F, 0x7AD1A461044D611C, 0xBDC0865DFE733AA9, 0xFF3067B657990C3A, 0x711223CFA3E5BB50, 0x33E2C2240A0F8DC3, 0xF4F3E018F031D676, 0xB60301F359DBE0E5, - 0xDA050215EA6C212F, 0x98F5E3FE438617BC, 0x5FE4C1C2B9B84C09, 0x1D14202910527A9A, 0x93366450E42ECDF0, 0xD1C685BB4DC4FB63, 0x16D7A787B7FAA0D6, 0x5427466C1E109645, - 0x4863CE9FF6E9F891, 0x0A932F745F03CE02, 0xCD820D48A53D95B7, 0x8F72ECA30CD7A324, 0x0150A8DAF8AB144E, 0x43A04931514122DD, 0x84B16B0DAB7F7968, 0xC6418AE602954FFB, - 0xBC387AEA7A8DA4C0, 0xFEC89B01D3679253, 0x39D9B93D2959C9E6, 0x7B2958D680B3FF75, 0xF50B1CAF74CF481F, 0xB7FBFD44DD257E8C, 0x70EADF78271B2539, 0x321A3E938EF113AA, - 0x2E5EB66066087D7E, 0x6CAE578BCFE24BED, 0xABBF75B735DC1058, 0xE94F945C9C3626CB, 0x676DD025684A91A1, 0x259D31CEC1A0A732, 0xE28C13F23B9EFC87, 0xA07CF2199274CA14, - 0x167FF3EACBAF2AF1, 0x548F120162451C62, 0x939E303D987B47D7, 0xD16ED1D631917144, 0x5F4C95AFC5EDC62E, 0x1DBC74446C07F0BD, 0xDAAD56789639AB08, 0x985DB7933FD39D9B, - 0x84193F60D72AF34F, 0xC6E9DE8B7EC0C5DC, 0x01F8FCB784FE9E69, 0x43081D5C2D14A8FA, 0xCD2A5925D9681F90, 0x8FDAB8CE70822903, 0x48CB9AF28ABC72B6, 0x0A3B7B1923564425, - 0x70428B155B4EAF1E, 0x32B26AFEF2A4998D, 0xF5A348C2089AC238, 0xB753A929A170F4AB, 0x3971ED50550C43C1, 0x7B810CBBFCE67552, 0xBC902E8706D82EE7, 0xFE60CF6CAF321874, - 0xE224479F47CB76A0, 0xA0D4A674EE214033, 0x67C58448141F1B86, 0x253565A3BDF52D15, 0xAB1721DA49899A7F, 0xE9E7C031E063ACEC, 0x2EF6E20D1A5DF759, 0x6C0603E6B3B7C1CA, - 0xF6FAE5C07D3274CD, 0xB40A042BD4D8425E, 0x731B26172EE619EB, 0x31EBC7FC870C2F78, 0xBFC9838573709812, 0xFD39626EDA9AAE81, 0x3A28405220A4F534, 0x78D8A1B9894EC3A7, - 0x649C294A61B7AD73, 0x266CC8A1C85D9BE0, 0xE17DEA9D3263C055, 0xA38D0B769B89F6C6, 0x2DAF4F0F6FF541AC, 0x6F5FAEE4C61F773F, 0xA84E8CD83C212C8A, 0xEABE6D3395CB1A19, - 0x90C79D3FEDD3F122, 0xD2377CD44439C7B1, 0x15265EE8BE079C04, 0x57D6BF0317EDAA97, 0xD9F4FB7AE3911DFD, 0x9B041A914A7B2B6E, 0x5C1538ADB04570DB, 0x1EE5D94619AF4648, - 0x02A151B5F156289C, 0x4051B05E58BC1E0F, 0x87409262A28245BA, 0xC5B073890B687329, 0x4B9237F0FF14C443, 0x0962D61B56FEF2D0, 0xCE73F427ACC0A965, 0x8C8315CC052A9FF6, - 0x3A80143F5CF17F13, 0x7870F5D4F51B4980, 0xBF61D7E80F251235, 0xFD913603A6CF24A6, 0x73B3727A52B393CC, 0x31439391FB59A55F, 0xF652B1AD0167FEEA, 0xB4A25046A88DC879, - 0xA8E6D8B54074A6AD, 0xEA16395EE99E903E, 0x2D071B6213A0CB8B, 0x6FF7FA89BA4AFD18, 0xE1D5BEF04E364A72, 0xA3255F1BE7DC7CE1, 0x64347D271DE22754, 0x26C49CCCB40811C7, - 0x5CBD6CC0CC10FAFC, 0x1E4D8D2B65FACC6F, 0xD95CAF179FC497DA, 0x9BAC4EFC362EA149, 0x158E0A85C2521623, 0x577EEB6E6BB820B0, 0x906FC95291867B05, 0xD29F28B9386C4D96, - 0xCEDBA04AD0952342, 0x8C2B41A1797F15D1, 0x4B3A639D83414E64, 0x09CA82762AAB78F7, 0x87E8C60FDED7CF9D, 0xC51827E4773DF90E, 0x020905D88D03A2BB, 0x40F9E43324E99428, - 0x2CFFE7D5975E55E2, 0x6E0F063E3EB46371, 0xA91E2402C48A38C4, 0xEBEEC5E96D600E57, 0x65CC8190991CB93D, 0x273C607B30F68FAE, 0xE02D4247CAC8D41B, 0xA2DDA3AC6322E288, - 0xBE992B5F8BDB8C5C, 0xFC69CAB42231BACF, 0x3B78E888D80FE17A, 0x7988096371E5D7E9, 0xF7AA4D1A85996083, 0xB55AACF12C735610, 0x724B8ECDD64D0DA5, 0x30BB6F267FA73B36, - 0x4AC29F2A07BFD00D, 0x08327EC1AE55E69E, 0xCF235CFD546BBD2B, 0x8DD3BD16FD818BB8, 0x03F1F96F09FD3CD2, 0x41011884A0170A41, 0x86103AB85A2951F4, 0xC4E0DB53F3C36767, - 0xD8A453A01B3A09B3, 0x9A54B24BB2D03F20, 0x5D45907748EE6495, 0x1FB5719CE1045206, 0x919735E51578E56C, 0xD367D40EBC92D3FF, 0x1476F63246AC884A, 0x568617D9EF46BED9, - 0xE085162AB69D5E3C, 0xA275F7C11F7768AF, 0x6564D5FDE549331A, 0x279434164CA30589, 0xA9B6706FB8DFB2E3, 0xEB46918411358470, 0x2C57B3B8EB0BDFC5, 0x6EA7525342E1E956, - 0x72E3DAA0AA188782, 0x30133B4B03F2B111, 0xF7021977F9CCEAA4, 0xB5F2F89C5026DC37, 0x3BD0BCE5A45A6B5D, 0x79205D0E0DB05DCE, 0xBE317F32F78E067B, 0xFCC19ED95E6430E8, - 0x86B86ED5267CDBD3, 0xC4488F3E8F96ED40, 0x0359AD0275A8B6F5, 0x41A94CE9DC428066, 0xCF8B0890283E370C, 0x8D7BE97B81D4019F, 0x4A6ACB477BEA5A2A, 0x089A2AACD2006CB9, - 0x14DEA25F3AF9026D, 0x562E43B4931334FE, 0x913F6188692D6F4B, 0xD3CF8063C0C759D8, 0x5DEDC41A34BBEEB2, 0x1F1D25F19D51D821, 0xD80C07CD676F8394, 0x9AFCE626CE85B507 - -}; - -/** - * @brief 计算crc64 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint64_t crc - */ -uint64_t crc64(uint64_t crc, uint8_t const *buffer, size_t len) -{ - while (len--) - { - crc = (crc << 8) ^ (crc64_table[((crc >> 56) ^ *buffer++) & 0xff]); - } - return crc; -} diff --git a/kernel/src/libs/crc7.c b/kernel/src/libs/crc7.c deleted file mode 100644 index 2bb47fa6..00000000 --- a/kernel/src/libs/crc7.c +++ /dev/null @@ -1,38 +0,0 @@ -#include - -/** CRC table for the CRC-7. The poly is 0X09 */ -// crc7 generator polynomial : G(x)=x7+x3+1 -const uint8_t crc7_table[256] = { - 0X00, 0X09, 0X12, 0X1B, 0X24, 0X2D, 0X36, 0X3F, 0X48, 0X41, 0X5A, 0X53, 0X6C, 0X65, 0X7E, 0X77, - 0X19, 0X10, 0X0B, 0X02, 0X3D, 0X34, 0X2F, 0X26, 0X51, 0X58, 0X43, 0X4A, 0X75, 0X7C, 0X67, 0X6E, - 0X32, 0X3B, 0X20, 0X29, 0X16, 0X1F, 0X04, 0X0D, 0X7A, 0X73, 0X68, 0X61, 0X5E, 0X57, 0X4C, 0X45, - 0X2B, 0X22, 0X39, 0X30, 0X0F, 0X06, 0X1D, 0X14, 0X63, 0X6A, 0X71, 0X78, 0X47, 0X4E, 0X55, 0X5C, - 0X64, 0X6D, 0X76, 0X7F, 0X40, 0X49, 0X52, 0X5B, 0X2C, 0X25, 0X3E, 0X37, 0X08, 0X01, 0X1A, 0X13, - 0X7D, 0X74, 0X6F, 0X66, 0X59, 0X50, 0X4B, 0X42, 0X35, 0X3C, 0X27, 0X2E, 0X11, 0X18, 0X03, 0X0A, - 0X56, 0X5F, 0X44, 0X4D, 0X72, 0X7B, 0X60, 0X69, 0X1E, 0X17, 0X0C, 0X05, 0X3A, 0X33, 0X28, 0X21, - 0X4F, 0X46, 0X5D, 0X54, 0X6B, 0X62, 0X79, 0X70, 0X07, 0X0E, 0X15, 0X1C, 0X23, 0X2A, 0X31, 0X38, - 0X41, 0X48, 0X53, 0X5A, 0X65, 0X6C, 0X77, 0X7E, 0X09, 0X00, 0X1B, 0X12, 0X2D, 0X24, 0X3F, 0X36, - 0X58, 0X51, 0X4A, 0X43, 0X7C, 0X75, 0X6E, 0X67, 0X10, 0X19, 0X02, 0X0B, 0X34, 0X3D, 0X26, 0X2F, - 0X73, 0X7A, 0X61, 0X68, 0X57, 0X5E, 0X45, 0X4C, 0X3B, 0X32, 0X29, 0X20, 0X1F, 0X16, 0X0D, 0X04, - 0X6A, 0X63, 0X78, 0X71, 0X4E, 0X47, 0X5C, 0X55, 0X22, 0X2B, 0X30, 0X39, 0X06, 0X0F, 0X14, 0X1D, - 0X25, 0X2C, 0X37, 0X3E, 0X01, 0X08, 0X13, 0X1A, 0X6D, 0X64, 0X7F, 0X76, 0X49, 0X40, 0X5B, 0X52, - 0X3C, 0X35, 0X2E, 0X27, 0X18, 0X11, 0X0A, 0X03, 0X74, 0X7D, 0X66, 0X6F, 0X50, 0X59, 0X42, 0X4B, - 0X17, 0X1E, 0X05, 0X0C, 0X33, 0X3A, 0X21, 0X28, 0X5F, 0X56, 0X4D, 0X44, 0X7B, 0X72, 0X69, 0X60, - 0X0E, 0X07, 0X1C, 0X15, 0X2A, 0X23, 0X38, 0X31, 0X46, 0X4F, 0X54, 0X5D, 0X62, 0X6B, 0X70, 0X79}; - -/** - * @brief 计算crc7 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint8_t crc - */ -uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len) -{ - while (len--) - { - crc = crc7_table[(crc << 1) ^ *buffer++]; - } - return (crc); -} diff --git a/kernel/src/libs/crc8.c b/kernel/src/libs/crc8.c deleted file mode 100644 index f58cee63..00000000 --- a/kernel/src/libs/crc8.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -// Polynomial=0x31 -// Initial Value=0x0 -// Final Xor Value=0x0 - -/** CRC table for the CRC-8. The poly is 0x31 */ -const uint8_t crc8_table[256] = { - 0x00, 0x31, 0x62, 0x53, 0xc4, 0xf5, 0xa6, 0x97, 0xb9, 0x88, 0xdb, 0xea, 0x7d, 0x4c, 0x1f, 0x2e, - 0x43, 0x72, 0x21, 0x10, 0x87, 0xb6, 0xe5, 0xd4, 0xfa, 0xcb, 0x98, 0xa9, 0x3e, 0x0f, 0x5c, 0x6d, - 0x86, 0xb7, 0xe4, 0xd5, 0x42, 0x73, 0x20, 0x11, 0x3f, 0x0e, 0x5d, 0x6c, 0xfb, 0xca, 0x99, 0xa8, - 0xc5, 0xf4, 0xa7, 0x96, 0x01, 0x30, 0x63, 0x52, 0x7c, 0x4d, 0x1e, 0x2f, 0xb8, 0x89, 0xda, 0xeb, - 0x3d, 0x0c, 0x5f, 0x6e, 0xf9, 0xc8, 0x9b, 0xaa, 0x84, 0xb5, 0xe6, 0xd7, 0x40, 0x71, 0x22, 0x13, - 0x7e, 0x4f, 0x1c, 0x2d, 0xba, 0x8b, 0xd8, 0xe9, 0xc7, 0xf6, 0xa5, 0x94, 0x03, 0x32, 0x61, 0x50, - 0xbb, 0x8a, 0xd9, 0xe8, 0x7f, 0x4e, 0x1d, 0x2c, 0x02, 0x33, 0x60, 0x51, 0xc6, 0xf7, 0xa4, 0x95, - 0xf8, 0xc9, 0x9a, 0xab, 0x3c, 0x0d, 0x5e, 0x6f, 0x41, 0x70, 0x23, 0x12, 0x85, 0xb4, 0xe7, 0xd6, - 0x7a, 0x4b, 0x18, 0x29, 0xbe, 0x8f, 0xdc, 0xed, 0xc3, 0xf2, 0xa1, 0x90, 0x07, 0x36, 0x65, 0x54, - 0x39, 0x08, 0x5b, 0x6a, 0xfd, 0xcc, 0x9f, 0xae, 0x80, 0xb1, 0xe2, 0xd3, 0x44, 0x75, 0x26, 0x17, - 0xfc, 0xcd, 0x9e, 0xaf, 0x38, 0x09, 0x5a, 0x6b, 0x45, 0x74, 0x27, 0x16, 0x81, 0xb0, 0xe3, 0xd2, - 0xbf, 0x8e, 0xdd, 0xec, 0x7b, 0x4a, 0x19, 0x28, 0x06, 0x37, 0x64, 0x55, 0xc2, 0xf3, 0xa0, 0x91, - 0x47, 0x76, 0x25, 0x14, 0x83, 0xb2, 0xe1, 0xd0, 0xfe, 0xcf, 0x9c, 0xad, 0x3a, 0x0b, 0x58, 0x69, - 0x04, 0x35, 0x66, 0x57, 0xc0, 0xf1, 0xa2, 0x93, 0xbd, 0x8c, 0xdf, 0xee, 0x79, 0x48, 0x1b, 0x2a, - 0xc1, 0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1a, 0x2b, 0xbc, 0x8d, 0xde, 0xef, - 0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, 0x3b, 0x0a, 0x59, 0x68, 0xff, 0xce, 0x9d, 0xac}; - -/** - * @brief 计算crc8 - * - * @param crc crc初始值 - * @param buffer 输入缓冲区 - * @param len buffer大小(bytes) - * @return uint8_t crc - */ -uint8_t crc8(uint8_t crc, uint8_t const *buffer, size_t len) -{ - while (len--) - { - // printf("%04x\n",(crc ^ *buffer)&0xff); - crc = crc8_table[crc ^ *buffer++]; - } - return (crc); -} diff --git a/kernel/src/libs/kfifo.c b/kernel/src/libs/kfifo.c deleted file mode 100644 index 30be88d7..00000000 --- a/kernel/src/libs/kfifo.c +++ /dev/null @@ -1,151 +0,0 @@ -#include -#include -#include -#include -#include - -/** - * @brief 通过动态方式初始化kfifo缓冲队列 - * - * @param fifo 队列结构体 - * @param size 缓冲区大小 - * @param reserved 暂时保留,请置为0 - * @return int 错误码: NOMEM; 成功->0 - */ -int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved) -{ - memset(fifo, 0, sizeof(struct kfifo_t)); - fifo->buffer = kmalloc(size, 0); - if (fifo->buffer == NULL) - goto failed; - - fifo->total_size = size; - return 0; -failed:; - return -ENOMEM; -} - -/** - * @brief 使用指定的缓冲区来初始化kfifo缓冲队列 - * - * @param fifo 队列结构体 - * @param buffer 缓冲区 - * @param size 缓冲区大小 - */ -void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size) -{ - memset(fifo, 0, sizeof(struct kfifo_t)); - - fifo->buffer = buffer; - fifo->total_size = size; -} - -/** - * @brief 向kfifo缓冲区推入数据 - * - * @param fifo 队列结构体 - * @param from 来源数据地址 - * @param size 数据大小(字节数) - * @return uint32_t 推入的数据大小 - */ -uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size) -{ - // 判断空间是否够 - if (unlikely(fifo->size + size > fifo->total_size)) - return 0; - if (unlikely(from == NULL)) - return 0; - - // 分两种情况,一种是要发生回环,另一种不发生回环 - if (fifo->in_offset + size > fifo->total_size) // 发生回环 - { - uint32_t tmp = fifo->total_size - fifo->in_offset; - memcpy(fifo->buffer + fifo->in_offset, from, tmp); - memcpy(fifo->buffer, from + tmp, size - tmp); - fifo->in_offset = size - tmp; - } - else // 不发生回环 - { - memcpy(fifo->buffer + fifo->in_offset, from, size); - fifo->in_offset += size; - } - - fifo->size += size; - - return size; -} - -/** - * @brief 从kfifo缓冲区取出数据,并从队列中删除数据 - * - * @param fifo 队列结构体 - * @param to 拷贝目标地址 - * @param size 数据大小(字节数) - * @return uint32_t 取出的数据大小 - */ -uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size) -{ - if (unlikely(to == NULL)) // 判断目标地址是否为空 - return 0; - if (unlikely(size > fifo->size)) // 判断队列中是否有这么多数据 - return 0; - - // 判断是否会发生回环 - if (fifo->out_offset + size > fifo->total_size) // 发生回环 - { - uint32_t tmp = fifo->total_size - fifo->out_offset; - memcpy(to, fifo->buffer + fifo->out_offset, tmp); - memcpy(to + tmp, fifo->buffer, size - tmp); - fifo->out_offset = size - tmp; - } - else // 未发生回环 - { - memcpy(to, fifo->buffer + fifo->out_offset, size); - fifo->out_offset += size; - } - - fifo->size -= size; - - return size; -} - -/** - * @brief 从kfifo缓冲区取出数据,但是不从队列中删除数据 - * - * @param fifo 队列结构体 - * @param to 拷贝目标地址 - * @param size 数据大小(字节数) - * @return uint32_t 取出的数据大小 - */ -uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size) -{ - if (unlikely(to == NULL)) // 判断目标地址是否为空 - return 0; - if (unlikely(size > fifo->size)) // 判断队列中是否有这么多数据 - return 0; - - // 判断是否会发生回环 - if (fifo->out_offset + size > fifo->total_size) // 发生回环 - { - uint32_t tmp = fifo->total_size - fifo->out_offset; - memcpy(to, fifo->buffer + fifo->out_offset, tmp); - memcpy(to + tmp, fifo->buffer, size - tmp); - } - else // 未发生回环 - { - memcpy(to, fifo->buffer + fifo->out_offset, size); - } - - return size; -} - -/** - * @brief 释放通过kfifo_alloc创建的fifo缓冲区 - * - * @param fifo fifo队列结构体 - */ -void kfifo_free_alloc(struct kfifo_t *fifo) -{ - kfree(fifo->buffer); - memset(fifo, 0, sizeof(struct kfifo_t)); -} \ No newline at end of file diff --git a/kernel/src/libs/lockref.c b/kernel/src/libs/lockref.c deleted file mode 100644 index 2e4f49ff..00000000 --- a/kernel/src/libs/lockref.c +++ /dev/null @@ -1,249 +0,0 @@ -#include - -#if ARCH(I386) || ARCH(X86_64) - -#include -#include - -#ifdef __LOCKREF_ENABLE_CMPXCHG__ -#include - -#define CMPXCHG_LOOP(__lock_ref, CODE, SUCCESS) \ - { \ - int retry = 100; \ - struct lockref old; \ - BUILD_BUG_ON(sizeof(old) != sizeof(uint64_t)); \ - old.lock_count = READ_ONCE(__lock_ref->lock_count); \ - while (likely(!spin_is_locked(&old.lock))) \ - { \ - struct lockref new = old; \ - CODE; \ - if (likely(arch_try_cmpxchg(&__lock_ref->lock_count, &old.lock_count, new.lock_count))) \ - { \ - SUCCESS; \ - } \ - if (!--retry) \ - break; \ - pause(); \ - } \ - } -#else - -#define CMPXCHG_LOOP(__lock_ref, CODE, SUCCESS) \ - do \ - { \ - } while (0) - -#endif - -/** - * @brief 原子的将引用计数加1 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - */ -void lockref_inc(struct lockref *lock_ref) -{ - // 先尝试使用cmpxchg进行无锁操作,若成功则返回 - CMPXCHG_LOOP(lock_ref, ++new.count;, return;); - - // 无锁操作超时,或当前是上锁的状态,则退化为有锁操作 - spin_lock(&lock_ref->lock); - ++lock_ref->count; - spin_unlock(&lock_ref->lock); -} - -/** - * @brief 原子地将引用计数加1.如果原来的count≤0,则操作失败。 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return bool 操作成功=>true - * 操作失败=>false - */ -bool lockref_inc_not_zero(struct lockref *lock_ref) -{ - CMPXCHG_LOOP( - lock_ref, - { - if (old.count <= 0) - return false; - ++new.count; - }, - { return true; }) - - bool retval; - spin_lock(&lock_ref->lock); - retval = false; - if (lock_ref->count > 0) - { - ++lock_ref->count; - retval = true; - } - spin_unlock(&lock_ref->lock); - return retval; -} - -/** - * @brief 原子地减少引用计数。如果已处于count≤0的状态,则返回-1 - * - * 本函数与lockref_dec_return()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会再次尝试通过加锁来执行操作 - * 而后者会直接返回错误 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return int 操作成功 => 返回新的引用变量值 - * lockref处于count≤0的状态 => 返回-1 - */ -int lockref_dec(struct lockref *lock_ref) -{ - CMPXCHG_LOOP( - lock_ref, - { - if (old.count <= 0) - break; - --new.count; - }, - { return new.count; }) - - // 如果xchg时,处于已加锁的状态或者检测到old.count <= 0,则采取加锁处理 - int retval = -1; - spin_lock(&lock_ref->lock); - if (lock_ref->count > 0) - { - --lock_ref->count; - retval = lock_ref->count; - } - spin_unlock(&lock_ref->lock); - - return retval; -} - -/** - * @brief 原子地减少引用计数。如果处于已加锁或count≤0的状态,则返回-1 - * - * 本函数与lockref_dec()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会直接返回错误 - * 而后者会再次尝试通过加锁来执行操作 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return int 操作成功 => 返回新的引用变量值 - * lockref处于已加锁或count≤0的状态 => 返回-1 - */ -int lockref_dec_return(struct lockref *lock_ref) -{ - CMPXCHG_LOOP( - lock_ref, - { - if (old.count <= 0) - return -1; - --new.count; - }, - { return new.count; }) - - return -1; -} - -/** - * @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败 - * - * 该函数与lockref_dec_or_lock_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会直接返回false. - * 而后者在这种情况下,会尝试加锁来进行操作。 - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return true 成功将引用计数减1 - * @return false 如果当前的引用计数≤1,操作失败 - */ -bool lockref_dec_not_zero(struct lockref *lock_ref) -{ - CMPXCHG_LOOP( - lock_ref, - { - if (old.count <= 1) - return false; - --new.count; - }, - { return true; }) - - bool retval = false; - spin_lock(&lock_ref->lock); - if (lock_ref->count > 1) - { - --lock_ref->count; - retval = true; - } - spin_unlock(&lock_ref->lock); - return retval; -} - -/** - * @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败 - * - * 该函数与lockref_dec_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会尝试加锁来进行操作。 - * 而后者在这种情况下,会直接返回false. - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return true 成功将引用计数减1 - * @return false 如果当前的引用计数≤1,操作失败 - */ -bool lockref_dec_or_lock_not_zero(struct lockref *lock_ref) -{ - CMPXCHG_LOOP( - lock_ref, - { - if (old.count <= 1) - break; - --new.count; - }, - { return true; }); - - bool retval = false; - spin_lock(&lock_ref->lock); - if (lock_ref->count > 1) - { - --lock_ref->count; - retval = true; - } - spin_unlock(&lock_ref->lock); - return retval; -} - -/** - * @brief 将lockref变量标记为已经死亡(将count设置为负值) - * - * @param lock_ref 指向要被操作的lockref变量的指针 - */ -void lockref_mark_dead(struct lockref *lock_ref) -{ - // 需要自旋锁先被加锁,若没有被加锁,则会抛出错误信息 - assert_spin_locked(&lock_ref->lock); - lock_ref->count = -128; -} - -/** - * @brief 自增引用计数。(除非该lockref已经死亡) - * - * @param lock_ref 指向要被操作的lockref变量的指针 - * @return true 操作成功 - * @return false 操作失败,lockref已死亡 - */ -bool lockref_inc_not_dead(struct lockref *lock_ref) -{ - CMPXCHG_LOOP( - lock_ref, - { - if (old.count < 0) - return false; - ++new.count; - }, - { return true; }) - - bool retval = false; - // 快捷路径操作失败,尝试加锁 - spin_lock(&lock_ref->lock); - if (lock_ref->count >= 0) - { - ++lock_ref->count; - retval = true; - } - spin_unlock(&lock_ref->lock); - return retval; -} - -#endif \ No newline at end of file diff --git a/kernel/src/libs/lz4.c b/kernel/src/libs/lz4.c deleted file mode 100644 index 4afadd98..00000000 --- a/kernel/src/libs/lz4.c +++ /dev/null @@ -1,3160 +0,0 @@ -/* - LZ4 - Fast LZ compression algorithm - Copyright (C) 2011-present, Yann Collet. - - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - LZ4 homepage : http://www.lz4.org - - LZ4 source repository : https://github.com/lz4/lz4 -*/ - -/*-************************************ - * Tuning parameters - **************************************/ -/* - * LZ4_HEAPMODE : - * Select how default compression functions will allocate memory for their hash table, - * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). - */ -#include -#if ARCH(I386) || ARCH(X86_64) - -#if ARCH(I386) || ARCH(X86_64) -#include -#else -#error Arch not supported. -#endif - -#ifndef LZ4_HEAPMODE -#define LZ4_HEAPMODE 0 -#endif - -/* - * LZ4_ACCELERATION_DEFAULT : - * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0 - */ -#define LZ4_ACCELERATION_DEFAULT 1 -/* - * LZ4_ACCELERATION_MAX : - * Any "acceleration" value higher than this threshold - * get treated as LZ4_ACCELERATION_MAX instead (fix #876) - */ -#define LZ4_ACCELERATION_MAX 65537 - -/*-************************************ - * CPU Feature Detection - **************************************/ -/* LZ4_FORCE_MEMORY_ACCESS - * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. - * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. - * The below switch allow to select different access method for improved performance. - * Method 0 (default) : use `memcpy()`. Safe and portable. - * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). - * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. - * Method 2 : direct access. This method is portable but violate C standard. - * It can generate buggy code on targets which assembly generation depends on alignment. - * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) - * See https://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. - * Prefer these methods in priority order (0 > 1 > 2) - */ -#ifndef LZ4_FORCE_MEMORY_ACCESS /* can be defined externally */ -#if defined(__GNUC__) && \ - (defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__)) -#define LZ4_FORCE_MEMORY_ACCESS 2 -#elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__) -#define LZ4_FORCE_MEMORY_ACCESS 1 -#endif -#endif - -/* - * LZ4_FORCE_SW_BITCOUNT - * Define this parameter if your target system or compiler does not support hardware bit count - */ -#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */ -#undef LZ4_FORCE_SW_BITCOUNT /* avoid double def */ -#define LZ4_FORCE_SW_BITCOUNT -#endif - -/*-************************************ - * Dependency - **************************************/ -/* - * LZ4_SRC_INCLUDED: - * Amalgamation flag, whether lz4.c is included - */ -#ifndef LZ4_SRC_INCLUDED -#define LZ4_SRC_INCLUDED 1 -#endif - -#ifndef LZ4_STATIC_LINKING_ONLY -#define LZ4_STATIC_LINKING_ONLY -#endif - -#ifndef LZ4_DISABLE_DEPRECATE_WARNINGS -#define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */ -#endif - -#define LZ4_STATIC_LINKING_ONLY /* LZ4_DISTANCE_MAX */ -#include -#include -/* see also "memory routines" below */ - -/*-************************************ - * Compiler Options - **************************************/ -#if defined(_MSC_VER) && (_MSC_VER >= 1400) /* Visual Studio 2005+ */ -#include /* only present in VS2005+ */ -#pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -#endif /* _MSC_VER */ - -#ifndef LZ4_FORCE_INLINE -#ifdef _MSC_VER /* Visual Studio */ -#define LZ4_FORCE_INLINE static __forceinline -#else -#if defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ -#ifdef __GNUC__ -#define LZ4_FORCE_INLINE static inline __attribute__((always_inline)) -#else -#define LZ4_FORCE_INLINE static inline -#endif -#else -#define LZ4_FORCE_INLINE static -#endif /* __STDC_VERSION__ */ -#endif /* _MSC_VER */ -#endif /* LZ4_FORCE_INLINE */ - -/* LZ4_FORCE_O2 and LZ4_FORCE_INLINE - * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8, - * together with a simple 8-byte copy loop as a fall-back path. - * However, this optimization hurts the decompression speed by >30%, - * because the execution does not go to the optimized loop - * for typical compressible data, and all of the preamble checks - * before going to the fall-back path become useless overhead. - * This optimization happens only with the -O3 flag, and -O2 generates - * a simple 8-byte copy loop. - * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy8 - * functions are annotated with __attribute__((optimize("O2"))), - * and also LZ4_wildCopy8 is forcibly inlined, so that the O2 attribute - * of LZ4_wildCopy8 does not affect the compression speed. - */ -#if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__) -#define LZ4_FORCE_O2 __attribute__((optimize("O2"))) -#undef LZ4_FORCE_INLINE -#define LZ4_FORCE_INLINE static __inline __attribute__((optimize("O2"), always_inline)) -#else -#define LZ4_FORCE_O2 -#endif - -#if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) -#define expect(expr, value) (__builtin_expect((expr), (value))) -#else -#define expect(expr, value) (expr) -#endif - -#ifndef likely -#define likely(expr) expect((expr) != 0, 1) -#endif -#ifndef unlikely -#define unlikely(expr) expect((expr) != 0, 0) -#endif - -/* Should the alignment test prove unreliable, for some reason, - * it can be disabled by setting LZ4_ALIGN_TEST to 0 */ -#ifndef LZ4_ALIGN_TEST /* can be externally provided */ -#define LZ4_ALIGN_TEST 1 -#endif - -/*-************************************ - * Memory routines - **************************************/ -#ifdef LZ4_USER_MEMORY_FUNCTIONS -/* memory management functions can be customized by user project. - * Below functions must exist somewhere in the Project - * and be available at link time */ -void *LZ4_malloc(size_t s); -void *LZ4_calloc(size_t n, size_t s); -void LZ4_free(void *p); -#define ALLOC(s) LZ4_malloc(s) -#define ALLOC_AND_ZERO(s) LZ4_calloc(1, s) -#define FREEMEM(p) LZ4_free(p) -#else -#include /* malloc, calloc, free */ -#define ALLOC(s) kmalloc(s, 0) -#define ALLOC_AND_ZERO(s) kzalloc(s, 0) -#define FREEMEM(p) kfree(p) -#endif - -#include /* memset, memcpy */ -#define MEM_INIT(p, v, s) memset((p), (v), (s)) - -/*-************************************ - * Common Constants - **************************************/ -#define MINMATCH 4 - -#define WILDCOPYLENGTH 8 -#define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ -#define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ -#define MATCH_SAFEGUARD_DISTANCE ((2 * WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */ -#define FASTLOOP_SAFE_DISTANCE 64 -static const int LZ4_minLength = (MFLIMIT + 1); - -#define KB *(1 << 10) -#define MB *(1 << 20) -#define GB *(1U << 30) - -#define LZ4_DISTANCE_ABSOLUTE_MAX 65535 -#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */ -#error "LZ4_DISTANCE_MAX is too big : must be <= 65535" -#endif - -#define ML_BITS 4 -#define ML_MASK ((1U << ML_BITS) - 1) -#define RUN_BITS (8 - ML_BITS) -#define RUN_MASK ((1U << RUN_BITS) - 1) - -/*-************************************ - * Error detection - **************************************/ -#if defined(LZ4_DEBUG) && (LZ4_DEBUG >= 1) -#include -#else -#ifndef assert -#define assert(condition) ((void)0) -#endif -#endif - -#define LZ4_STATIC_ASSERT(c) \ - { \ - enum \ - { \ - LZ4_static_assert = 1 / (int)(!!(c)) \ - }; \ - } /* use after variable declarations */ - -#if defined(LZ4_DEBUG) && (LZ4_DEBUG >= 2) -#include -static int g_debuglog_enable = 1; -#define DEBUGLOG(l, ...) \ - { \ - if ((g_debuglog_enable) && (l <= LZ4_DEBUG)) \ - { \ - fprintf(stderr, __FILE__ ": "); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, " \n"); \ - } \ - } -#else -#define DEBUGLOG(l, ...) \ - { \ - } /* disabled */ -#endif - -static int LZ4_isAligned(const void *ptr, size_t alignment) -{ - return ((size_t)ptr & (alignment - 1)) == 0; -} - -/*-************************************ - * Types - **************************************/ -#include -#if defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) -#include -typedef uint8_t BYTE; -typedef uint16_t U16; -typedef uint32_t U32; -typedef int32_t S32; -typedef uint64_t U64; -typedef uintptr_t uptrval; -#else -#if UINT_MAX != 4294967295UL -#error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4" -#endif -typedef unsigned char BYTE; -typedef unsigned short U16; -typedef unsigned int U32; -typedef signed int S32; -typedef unsigned long long U64; -typedef size_t uptrval; /* generally true, except OpenVMS-64 */ -#endif - -#if defined(__x86_64__) -typedef U64 reg_t; /* 64-bits in x32 mode */ -#else -typedef size_t reg_t; /* 32-bits in x32 mode */ -#endif - -typedef enum -{ - notLimited = 0, - limitedOutput = 1, - fillOutput = 2 -} limitedOutput_directive; - -/*-************************************ - * Reading and writing into memory - **************************************/ - -/** - * LZ4 relies on memcpy with a constant size being inlined. In freestanding - * environments, the compiler can't assume the implementation of memcpy() is - * standard compliant, so it can't apply its specialized memcpy() inlining - * logic. When possible, use __builtin_memcpy() to tell the compiler to analyze - * memcpy() as if it were standard compliant, so it can inline it in freestanding - * environments. This is needed when decompressing the Linux Kernel, for example. - */ -#define LZ4_memcpy(dst, src, size) memcpy(dst, src, size) - -static unsigned LZ4_isLittleEndian(void) -{ - const union - { - U32 u; - BYTE c[4]; - } one = {1}; /* don't use static : performance detrimental */ - return one.c[0]; -} - -#if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS == 2) -/* lie to the compiler about data alignment; use with caution */ - -static U16 LZ4_read16(const void *memPtr) { return *(const U16 *)memPtr; } -static U32 LZ4_read32(const void *memPtr) { return *(const U32 *)memPtr; } -static reg_t LZ4_read_ARCH(const void *memPtr) { return *(const reg_t *)memPtr; } - -static void LZ4_write16(void *memPtr, U16 value) { *(U16 *)memPtr = value; } -static void LZ4_write32(void *memPtr, U32 value) { *(U32 *)memPtr = value; } - -#elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS == 1) - -/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ -/* currently only defined for gcc and icc */ -typedef union -{ - U16 u16; - U32 u32; - reg_t uArch; -} __attribute__((packed)) unalign; - -static U16 LZ4_read16(const void *ptr) { return ((const unalign *)ptr)->u16; } -static U32 LZ4_read32(const void *ptr) { return ((const unalign *)ptr)->u32; } -static reg_t LZ4_read_ARCH(const void *ptr) { return ((const unalign *)ptr)->uArch; } - -static void LZ4_write16(void *memPtr, U16 value) { ((unalign *)memPtr)->u16 = value; } -static void LZ4_write32(void *memPtr, U32 value) { ((unalign *)memPtr)->u32 = value; } - -#else /* safe and portable access using memcpy() */ - -static U16 LZ4_read16(const void *memPtr) -{ - U16 val; - LZ4_memcpy(&val, memPtr, sizeof(val)); - return val; -} - -static U32 LZ4_read32(const void *memPtr) -{ - U32 val; - LZ4_memcpy(&val, memPtr, sizeof(val)); - return val; -} - -static reg_t LZ4_read_ARCH(const void *memPtr) -{ - reg_t val; - LZ4_memcpy(&val, memPtr, sizeof(val)); - return val; -} - -static void LZ4_write16(void *memPtr, U16 value) -{ - LZ4_memcpy(memPtr, &value, sizeof(value)); -} - -static void LZ4_write32(void *memPtr, U32 value) -{ - LZ4_memcpy(memPtr, &value, sizeof(value)); -} - -#endif /* LZ4_FORCE_MEMORY_ACCESS */ - -static U16 LZ4_readLE16(const void *memPtr) -{ - if (LZ4_isLittleEndian()) - { - return LZ4_read16(memPtr); - } - else - { - const BYTE *p = (const BYTE *)memPtr; - return (U16)((U16)p[0] + (p[1] << 8)); - } -} - -static void LZ4_writeLE16(void *memPtr, U16 value) -{ - if (LZ4_isLittleEndian()) - { - LZ4_write16(memPtr, value); - } - else - { - BYTE *p = (BYTE *)memPtr; - p[0] = (BYTE)value; - p[1] = (BYTE)(value >> 8); - } -} - -/* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */ -LZ4_FORCE_INLINE -void LZ4_wildCopy8(void *dstPtr, const void *srcPtr, void *dstEnd) -{ - BYTE *d = (BYTE *)dstPtr; - const BYTE *s = (const BYTE *)srcPtr; - BYTE *const e = (BYTE *)dstEnd; - - do - { - LZ4_memcpy(d, s, 8); - d += 8; - s += 8; - } while (d < e); -} - -static const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; -static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; - -#ifndef LZ4_FAST_DEC_LOOP -#if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64 -#define LZ4_FAST_DEC_LOOP 1 -#elif defined(__aarch64__) && !defined(__clang__) -/* On aarch64, we disable this optimization for clang because on certain - * mobile chipsets, performance is reduced with clang. For information - * refer to https://github.com/lz4/lz4/pull/707 */ -#define LZ4_FAST_DEC_LOOP 1 -#else -#define LZ4_FAST_DEC_LOOP 0 -#endif -#endif - -#if LZ4_FAST_DEC_LOOP - -LZ4_FORCE_INLINE void -LZ4_memcpy_using_offset_base(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, const size_t offset) -{ - assert(srcPtr + offset == dstPtr); - if (offset < 8) - { - LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */ - dstPtr[0] = srcPtr[0]; - dstPtr[1] = srcPtr[1]; - dstPtr[2] = srcPtr[2]; - dstPtr[3] = srcPtr[3]; - srcPtr += inc32table[offset]; - LZ4_memcpy(dstPtr + 4, srcPtr, 4); - srcPtr -= dec64table[offset]; - dstPtr += 8; - } - else - { - LZ4_memcpy(dstPtr, srcPtr, 8); - dstPtr += 8; - srcPtr += 8; - } - - LZ4_wildCopy8(dstPtr, srcPtr, dstEnd); -} - -/* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd - * this version copies two times 16 bytes (instead of one time 32 bytes) - * because it must be compatible with offsets >= 16. */ -LZ4_FORCE_INLINE void -LZ4_wildCopy32(void *dstPtr, const void *srcPtr, void *dstEnd) -{ - BYTE *d = (BYTE *)dstPtr; - const BYTE *s = (const BYTE *)srcPtr; - BYTE *const e = (BYTE *)dstEnd; - - do - { - LZ4_memcpy(d, s, 16); - LZ4_memcpy(d + 16, s + 16, 16); - d += 32; - s += 32; - } while (d < e); -} - -/* LZ4_memcpy_using_offset() presumes : - * - dstEnd >= dstPtr + MINMATCH - * - there is at least 8 bytes available to write after dstEnd */ -LZ4_FORCE_INLINE void -LZ4_memcpy_using_offset(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, const size_t offset) -{ - BYTE v[8]; - - assert(dstEnd >= dstPtr + MINMATCH); - - switch (offset) - { - case 1: - MEM_INIT(v, *srcPtr, 8); - break; - case 2: - LZ4_memcpy(v, srcPtr, 2); - LZ4_memcpy(&v[2], srcPtr, 2); - LZ4_memcpy(&v[4], v, 4); - break; - case 4: - LZ4_memcpy(v, srcPtr, 4); - LZ4_memcpy(&v[4], srcPtr, 4); - break; - default: - LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset); - return; - } - - LZ4_memcpy(dstPtr, v, 8); - dstPtr += 8; - while (dstPtr < dstEnd) - { - LZ4_memcpy(dstPtr, v, 8); - dstPtr += 8; - } -} -#endif - -/*-************************************ - * Common functions - **************************************/ -static unsigned LZ4_NbCommonBytes(reg_t val) -{ - assert(val != 0); - if (LZ4_isLittleEndian()) - { - if (sizeof(val) == 8) - { -#if defined(_MSC_VER) && (_MSC_VER >= 1800) && defined(_M_AMD64) && !defined(LZ4_FORCE_SW_BITCOUNT) - /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */ - return (unsigned)_tzcnt_u64(val) >> 3; -#elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) - unsigned long r = 0; - _BitScanForward64(&r, (U64)val); - return (unsigned)r >> 3; -#elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ - ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ - !defined(LZ4_FORCE_SW_BITCOUNT) - return (unsigned)__builtin_ctzll((U64)val) >> 3; -#else - const U64 m = 0x0101010101010101ULL; - val ^= val - 1; - return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56); -#endif - } - else /* 32 bits */ - { -#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT) - unsigned long r; - _BitScanForward(&r, (U32)val); - return (unsigned)r >> 3; -#elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ - ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ - !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) - return (unsigned)__builtin_ctz((U32)val) >> 3; -#else - const U32 m = 0x01010101; - return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24; -#endif - } - } - else /* Big Endian CPU */ - { - if (sizeof(val) == 8) - { -#if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ - ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ - !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) - return (unsigned)__clzll((U64)val) >> 3; -#else -#if 1 - /* this method is probably faster, - * but adds a 128 bytes lookup table */ - static const unsigned char ctz7_tab[128] = { - 7, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 4, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 5, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 4, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 6, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 4, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 5, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 4, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - 3, - 0, - 1, - 0, - 2, - 0, - 1, - 0, - }; - U64 const mask = 0x0101010101010101ULL; - U64 const t = (((val >> 8) - mask) | val) & mask; - return ctz7_tab[(t * 0x0080402010080402ULL) >> 57]; -#else - /* this method doesn't consume memory space like the previous one, - * but it contains several branches, - * that may end up slowing execution */ - static const U32 by32 = sizeof(val) * 4; /* 32 on 64 bits (goal), 16 on 32 bits. - Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. - Note that this code path is never triggered in 32-bits mode. */ - unsigned r; - if (!(val >> by32)) - { - r = 4; - } - else - { - r = 0; - val >>= by32; - } - if (!(val >> 16)) - { - r += 2; - val >>= 8; - } - else - { - val >>= 24; - } - r += (!val); - return r; -#endif -#endif - } - else /* 32 bits */ - { -#if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ - ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ - !defined(LZ4_FORCE_SW_BITCOUNT) - return (unsigned)__clz((U32)val) >> 3; -#else - val >>= 8; - val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) | - (val + 0x00FF0000)) >> - 24; - return (unsigned)val ^ 3; -#endif - } - } -} - -#define STEPSIZE sizeof(reg_t) -LZ4_FORCE_INLINE -unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit) -{ - const BYTE *const pStart = pIn; - - if (likely(pIn < pInLimit - (STEPSIZE - 1))) - { - reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); - if (!diff) - { - pIn += STEPSIZE; - pMatch += STEPSIZE; - } - else - { - return LZ4_NbCommonBytes(diff); - } - } - - while (likely(pIn < pInLimit - (STEPSIZE - 1))) - { - reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); - if (!diff) - { - pIn += STEPSIZE; - pMatch += STEPSIZE; - continue; - } - pIn += LZ4_NbCommonBytes(diff); - return (unsigned)(pIn - pStart); - } - - if ((STEPSIZE == 8) && (pIn < (pInLimit - 3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) - { - pIn += 4; - pMatch += 4; - } - if ((pIn < (pInLimit - 1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) - { - pIn += 2; - pMatch += 2; - } - if ((pIn < pInLimit) && (*pMatch == *pIn)) - pIn++; - return (unsigned)(pIn - pStart); -} - -#ifndef LZ4_COMMONDEFS_ONLY -/*-************************************ - * Local Constants - **************************************/ -static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT - 1)); -static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */ - -/*-************************************ - * Local Structures and types - **************************************/ -typedef enum -{ - clearedTable = 0, - byPtr, - byU32, - byU16 -} tableType_t; - -/** - * This enum distinguishes several different modes of accessing previous - * content in the stream. - * - * - noDict : There is no preceding content. - * - withPrefix64k : Table entries up to ctx->dictSize before the current blob - * blob being compressed are valid and refer to the preceding - * content (of length ctx->dictSize), which is available - * contiguously preceding in memory the content currently - * being compressed. - * - usingExtDict : Like withPrefix64k, but the preceding content is somewhere - * else in memory, starting at ctx->dictionary with length - * ctx->dictSize. - * - usingDictCtx : Like usingExtDict, but everything concerning the preceding - * content is in a separate context, pointed to by - * ctx->dictCtx. ctx->dictionary, ctx->dictSize, and table - * entries in the current context that refer to positions - * preceding the beginning of the current compression are - * ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx - * ->dictSize describe the location and size of the preceding - * content, and matches are found by looking in the ctx - * ->dictCtx->hashTable. - */ -typedef enum -{ - noDict = 0, - withPrefix64k, - usingExtDict, - usingDictCtx -} dict_directive; -typedef enum -{ - noDictIssue = 0, - dictSmall -} dictIssue_directive; - -/*-************************************ - * Local Utils - **************************************/ -int LZ4_versionNumber(void) { return LZ4_VERSION_NUMBER; } -const char *LZ4_versionString(void) { return LZ4_VERSION_STRING; } -int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } -int LZ4_sizeofState(void) { return LZ4_STREAMSIZE; } - -/*-************************************ - * Internal Definitions used in Tests - **************************************/ -#if defined(__cplusplus) -extern "C" -{ -#endif - - int LZ4_compress_forceExtDict(LZ4_stream_t *LZ4_dict, const char *source, char *dest, int srcSize); - - int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, - int compressedSize, int maxOutputSize, - const void *dictStart, size_t dictSize); - -#if defined(__cplusplus) -} -#endif - -/*-****************************** - * Compression functions - ********************************/ -LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType) -{ - if (tableType == byU16) - return ((sequence * 2654435761U) >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1))); - else - return ((sequence * 2654435761U) >> ((MINMATCH * 8) - LZ4_HASHLOG)); -} - -LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType) -{ - const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG + 1 : LZ4_HASHLOG; - if (LZ4_isLittleEndian()) - { - const U64 prime5bytes = 889523592379ULL; - return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); - } - else - { - const U64 prime8bytes = 11400714785074694791ULL; - return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); - } -} - -LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void *const p, tableType_t const tableType) -{ - if ((sizeof(reg_t) == 8) && (tableType != byU16)) - return LZ4_hash5(LZ4_read_ARCH(p), tableType); - return LZ4_hash4(LZ4_read32(p), tableType); -} - -LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void *tableBase, tableType_t const tableType) -{ - switch (tableType) - { - default: /* fallthrough */ - case clearedTable: - { /* illegal! */ - assert(0); - return; - } - case byPtr: - { - const BYTE **hashTable = (const BYTE **)tableBase; - hashTable[h] = NULL; - return; - } - case byU32: - { - U32 *hashTable = (U32 *)tableBase; - hashTable[h] = 0; - return; - } - case byU16: - { - U16 *hashTable = (U16 *)tableBase; - hashTable[h] = 0; - return; - } - } -} - -LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void *tableBase, tableType_t const tableType) -{ - switch (tableType) - { - default: /* fallthrough */ - case clearedTable: /* fallthrough */ - case byPtr: - { /* illegal! */ - assert(0); - return; - } - case byU32: - { - U32 *hashTable = (U32 *)tableBase; - hashTable[h] = idx; - return; - } - case byU16: - { - U16 *hashTable = (U16 *)tableBase; - assert(idx < 65536); - hashTable[h] = (U16)idx; - return; - } - } -} - -LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE *p, U32 h, - void *tableBase, tableType_t const tableType, - const BYTE *srcBase) -{ - switch (tableType) - { - case clearedTable: - { /* illegal! */ - assert(0); - return; - } - case byPtr: - { - const BYTE **hashTable = (const BYTE **)tableBase; - hashTable[h] = p; - return; - } - case byU32: - { - U32 *hashTable = (U32 *)tableBase; - hashTable[h] = (U32)(p - srcBase); - return; - } - case byU16: - { - U16 *hashTable = (U16 *)tableBase; - hashTable[h] = (U16)(p - srcBase); - return; - } - } -} - -LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase) -{ - U32 const h = LZ4_hashPosition(p, tableType); - LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); -} - -/* LZ4_getIndexOnHash() : - * Index of match position registered in hash table. - * hash position must be calculated by using base+index, or dictBase+index. - * Assumption 1 : only valid if tableType == byU32 or byU16. - * Assumption 2 : h is presumed valid (within limits of hash table) - */ -LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void *tableBase, tableType_t tableType) -{ - LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2); - if (tableType == byU32) - { - const U32 *const hashTable = (const U32 *)tableBase; - assert(h < (1U << (LZ4_MEMORY_USAGE - 2))); - return hashTable[h]; - } - if (tableType == byU16) - { - const U16 *const hashTable = (const U16 *)tableBase; - assert(h < (1U << (LZ4_MEMORY_USAGE - 1))); - return hashTable[h]; - } - assert(0); - return 0; /* forbidden case */ -} - -static const BYTE *LZ4_getPositionOnHash(U32 h, const void *tableBase, tableType_t tableType, const BYTE *srcBase) -{ - if (tableType == byPtr) - { - const BYTE *const *hashTable = (const BYTE *const *)tableBase; - return hashTable[h]; - } - if (tableType == byU32) - { - const U32 *const hashTable = (const U32 *)tableBase; - return hashTable[h] + srcBase; - } - { - const U16 *const hashTable = (const U16 *)tableBase; - return hashTable[h] + srcBase; - } /* default, to ensure a return */ -} - -LZ4_FORCE_INLINE const BYTE * -LZ4_getPosition(const BYTE *p, - const void *tableBase, tableType_t tableType, - const BYTE *srcBase) -{ - U32 const h = LZ4_hashPosition(p, tableType); - return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); -} - -LZ4_FORCE_INLINE void -LZ4_prepareTable(LZ4_stream_t_internal *const cctx, - const int inputSize, - const tableType_t tableType) -{ - /* If the table hasn't been used, it's guaranteed to be zeroed out, and is - * therefore safe to use no matter what mode we're in. Otherwise, we figure - * out if it's safe to leave as is or whether it needs to be reset. - */ - if ((tableType_t)cctx->tableType != clearedTable) - { - assert(inputSize >= 0); - if ((tableType_t)cctx->tableType != tableType || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU) || ((tableType == byU32) && cctx->currentOffset > 1 GB) || tableType == byPtr || inputSize >= 4 KB) - { - DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx); - MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE); - cctx->currentOffset = 0; - cctx->tableType = (U32)clearedTable; - } - else - { - DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)"); - } - } - - /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, is faster - * than compressing without a gap. However, compressing with - * currentOffset == 0 is faster still, so we preserve that case. - */ - if (cctx->currentOffset != 0 && tableType == byU32) - { - DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset"); - cctx->currentOffset += 64 KB; - } - - /* Finally, clear history */ - cctx->dictCtx = NULL; - cctx->dictionary = NULL; - cctx->dictSize = 0; -} - -/** LZ4_compress_generic() : - * inlined, to ensure branches are decided at compilation time. - * Presumed already validated at this stage: - * - source != NULL - * - inputSize > 0 - */ -LZ4_FORCE_INLINE int LZ4_compress_generic_validated( - LZ4_stream_t_internal *const cctx, - const char *const source, - char *const dest, - const int inputSize, - int *inputConsumed, /* only written when outputDirective == fillOutput */ - const int maxOutputSize, - const limitedOutput_directive outputDirective, - const tableType_t tableType, - const dict_directive dictDirective, - const dictIssue_directive dictIssue, - const int acceleration) -{ - int result; - const BYTE *ip = (const BYTE *)source; - - U32 const startIndex = cctx->currentOffset; - const BYTE *base = (const BYTE *)source - startIndex; - const BYTE *lowLimit; - - const LZ4_stream_t_internal *dictCtx = (const LZ4_stream_t_internal *)cctx->dictCtx; - const BYTE *const dictionary = - dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary; - const U32 dictSize = - dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize; - const U32 dictDelta = (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with index in current context */ - - int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx); - U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */ - const BYTE *const dictEnd = dictionary ? dictionary + dictSize : dictionary; - const BYTE *anchor = (const BYTE *)source; - const BYTE *const iend = ip + inputSize; - const BYTE *const mflimitPlusOne = iend - MFLIMIT + 1; - const BYTE *const matchlimit = iend - LASTLITERALS; - - /* the dictCtx currentOffset is indexed on the start of the dictionary, - * while a dictionary in the current context precedes the currentOffset */ - const BYTE *dictBase = !dictionary ? NULL : (dictDirective == usingDictCtx) ? dictionary + dictSize - dictCtx->currentOffset - : dictionary + dictSize - startIndex; - - BYTE *op = (BYTE *)dest; - BYTE *const olimit = op + maxOutputSize; - - U32 offset = 0; - U32 forwardH; - - DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType); - assert(ip != NULL); - /* If init conditions are not met, we don't have to mark stream - * as having dirty context, since no action was taken yet */ - if (outputDirective == fillOutput && maxOutputSize < 1) - { - return 0; - } /* Impossible to store anything */ - if ((tableType == byU16) && (inputSize >= LZ4_64Klimit)) - { - return 0; - } /* Size too large (not within 64K limit) */ - if (tableType == byPtr) - assert(dictDirective == noDict); /* only supported use case with byPtr */ - assert(acceleration >= 1); - - lowLimit = (const BYTE *)source - (dictDirective == withPrefix64k ? dictSize : 0); - - /* Update context state */ - if (dictDirective == usingDictCtx) - { - /* Subsequent linked blocks can't use the dictionary. */ - /* Instead, they use the block we just compressed. */ - cctx->dictCtx = NULL; - cctx->dictSize = (U32)inputSize; - } - else - { - cctx->dictSize += (U32)inputSize; - } - cctx->currentOffset += (U32)inputSize; - cctx->tableType = (U32)tableType; - - if (inputSize < LZ4_minLength) - goto _last_literals; /* Input too small, no compression (all literals) */ - - /* First Byte */ - LZ4_putPosition(ip, cctx->hashTable, tableType, base); - ip++; - forwardH = LZ4_hashPosition(ip, tableType); - - /* Main Loop */ - for (;;) - { - const BYTE *match; - BYTE *token; - const BYTE *filledIp; - - /* Find a match */ - if (tableType == byPtr) - { - const BYTE *forwardIp = ip; - int step = 1; - int searchMatchNb = acceleration << LZ4_skipTrigger; - do - { - U32 const h = forwardH; - ip = forwardIp; - forwardIp += step; - step = (searchMatchNb++ >> LZ4_skipTrigger); - - if (unlikely(forwardIp > mflimitPlusOne)) - goto _last_literals; - assert(ip < mflimitPlusOne); - - match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base); - forwardH = LZ4_hashPosition(forwardIp, tableType); - LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base); - - } while ((match + LZ4_DISTANCE_MAX < ip) || (LZ4_read32(match) != LZ4_read32(ip))); - } - else - { /* byU32, byU16 */ - - const BYTE *forwardIp = ip; - int step = 1; - int searchMatchNb = acceleration << LZ4_skipTrigger; - do - { - U32 const h = forwardH; - U32 const current = (U32)(forwardIp - base); - U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); - assert(matchIndex <= current); - assert(forwardIp - base < (ptrdiff_t)(2 GB - 1)); - ip = forwardIp; - forwardIp += step; - step = (searchMatchNb++ >> LZ4_skipTrigger); - - if (unlikely(forwardIp > mflimitPlusOne)) - goto _last_literals; - assert(ip < mflimitPlusOne); - - if (dictDirective == usingDictCtx) - { - if (matchIndex < startIndex) - { - /* there was no match, try the dictionary */ - assert(tableType == byU32); - matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); - match = dictBase + matchIndex; - matchIndex += dictDelta; /* make dictCtx index comparable with current context */ - lowLimit = dictionary; - } - else - { - match = base + matchIndex; - lowLimit = (const BYTE *)source; - } - } - else if (dictDirective == usingExtDict) - { - if (matchIndex < startIndex) - { - DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex); - assert(startIndex - matchIndex >= MINMATCH); - match = dictBase + matchIndex; - lowLimit = dictionary; - } - else - { - match = base + matchIndex; - lowLimit = (const BYTE *)source; - } - } - else - { /* single continuous memory segment */ - match = base + matchIndex; - } - forwardH = LZ4_hashPosition(forwardIp, tableType); - LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); - - DEBUGLOG(7, "candidate at pos=%u (offset=%u \n", matchIndex, current - matchIndex); - if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) - { - continue; - } /* match outside of valid area */ - assert(matchIndex < current); - if (((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX)) && (matchIndex + LZ4_DISTANCE_MAX < current)) - { - continue; - } /* too far */ - assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* match now expected within distance */ - - if (LZ4_read32(match) == LZ4_read32(ip)) - { - if (maybe_extMem) - offset = current - matchIndex; - break; /* match found */ - } - - } while (1); - } - - /* Catch up */ - filledIp = ip; - while (((ip > anchor) & (match > lowLimit)) && (unlikely(ip[-1] == match[-1]))) - { - ip--; - match--; - } - - /* Encode Literals */ - { - unsigned const litLength = (unsigned)(ip - anchor); - token = op++; - if ((outputDirective == limitedOutput) && /* Check output buffer overflow */ - (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength / 255) > olimit))) - { - return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ - } - if ((outputDirective == fillOutput) && - (unlikely(op + (litLength + 240) / 255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) - { - op--; - goto _last_literals; - } - if (litLength >= RUN_MASK) - { - int len = (int)(litLength - RUN_MASK); - *token = (RUN_MASK << ML_BITS); - for (; len >= 255; len -= 255) - *op++ = 255; - *op++ = (BYTE)len; - } - else - *token = (BYTE)(litLength << ML_BITS); - - /* Copy Literals */ - LZ4_wildCopy8(op, anchor, op + litLength); - op += litLength; - DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", - (int)(anchor - (const BYTE *)source), litLength, (int)(ip - (const BYTE *)source)); - } - - _next_match: - /* at this stage, the following variables must be correctly set : - * - ip : at start of LZ operation - * - match : at start of previous pattern occurence; can be within current prefix, or within extDict - * - offset : if maybe_ext_memSegment==1 (constant) - * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise - * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written - */ - - if ((outputDirective == fillOutput) && - (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) - { - /* the match was too close to the end, rewind and go to last literals */ - op = token; - goto _last_literals; - } - - /* Encode Offset */ - if (maybe_extMem) - { /* static test */ - DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE *)source)); - assert(offset <= LZ4_DISTANCE_MAX && offset > 0); - LZ4_writeLE16(op, (U16)offset); - op += 2; - } - else - { - DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match)); - assert(ip - match <= LZ4_DISTANCE_MAX); - LZ4_writeLE16(op, (U16)(ip - match)); - op += 2; - } - - /* Encode MatchLength */ - { - unsigned matchCode; - - if ((dictDirective == usingExtDict || dictDirective == usingDictCtx) && (lowLimit == dictionary) /* match within extDict */) - { - const BYTE *limit = ip + (dictEnd - match); - assert(dictEnd > match); - if (limit > matchlimit) - limit = matchlimit; - matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, limit); - ip += (size_t)matchCode + MINMATCH; - if (ip == limit) - { - unsigned const more = LZ4_count(limit, (const BYTE *)source, matchlimit); - matchCode += more; - ip += more; - } - DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode + MINMATCH); - } - else - { - matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, matchlimit); - ip += (size_t)matchCode + MINMATCH; - DEBUGLOG(6, " with matchLength=%u", matchCode + MINMATCH); - } - - if ((outputDirective) && /* Check output buffer overflow */ - (unlikely(op + (1 + LASTLITERALS) + (matchCode + 240) / 255 > olimit))) - { - if (outputDirective == fillOutput) - { - /* Match description too long : reduce it */ - U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255; - ip -= matchCode - newMatchCode; - assert(newMatchCode < matchCode); - matchCode = newMatchCode; - if (unlikely(ip <= filledIp)) - { - /* We have already filled up to filledIp so if ip ends up less than filledIp - * we have positions in the hash table beyond the current position. This is - * a problem if we reuse the hash table. So we have to remove these positions - * from the hash table. - */ - const BYTE *ptr; - DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip)); - for (ptr = ip; ptr <= filledIp; ++ptr) - { - U32 const h = LZ4_hashPosition(ptr, tableType); - LZ4_clearHash(h, cctx->hashTable, tableType); - } - } - } - else - { - assert(outputDirective == limitedOutput); - return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ - } - } - if (matchCode >= ML_MASK) - { - *token += ML_MASK; - matchCode -= ML_MASK; - LZ4_write32(op, 0xFFFFFFFF); - while (matchCode >= 4 * 255) - { - op += 4; - LZ4_write32(op, 0xFFFFFFFF); - matchCode -= 4 * 255; - } - op += matchCode / 255; - *op++ = (BYTE)(matchCode % 255); - } - else - *token += (BYTE)(matchCode); - } - /* Ensure we have enough space for the last literals. */ - assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit)); - - anchor = ip; - - /* Test end of chunk */ - if (ip >= mflimitPlusOne) - break; - - /* Fill table */ - LZ4_putPosition(ip - 2, cctx->hashTable, tableType, base); - - /* Test next position */ - if (tableType == byPtr) - { - - match = LZ4_getPosition(ip, cctx->hashTable, tableType, base); - LZ4_putPosition(ip, cctx->hashTable, tableType, base); - if ((match + LZ4_DISTANCE_MAX >= ip) && (LZ4_read32(match) == LZ4_read32(ip))) - { - token = op++; - *token = 0; - goto _next_match; - } - } - else - { /* byU32, byU16 */ - - U32 const h = LZ4_hashPosition(ip, tableType); - U32 const current = (U32)(ip - base); - U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); - assert(matchIndex < current); - if (dictDirective == usingDictCtx) - { - if (matchIndex < startIndex) - { - /* there was no match, try the dictionary */ - matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); - match = dictBase + matchIndex; - lowLimit = dictionary; /* required for match length counter */ - matchIndex += dictDelta; - } - else - { - match = base + matchIndex; - lowLimit = (const BYTE *)source; /* required for match length counter */ - } - } - else if (dictDirective == usingExtDict) - { - if (matchIndex < startIndex) - { - match = dictBase + matchIndex; - lowLimit = dictionary; /* required for match length counter */ - } - else - { - match = base + matchIndex; - lowLimit = (const BYTE *)source; /* required for match length counter */ - } - } - else - { /* single memory segment */ - match = base + matchIndex; - } - LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); - assert(matchIndex < current); - if (((dictIssue == dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) && (((tableType == byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex + LZ4_DISTANCE_MAX >= current)) && (LZ4_read32(match) == LZ4_read32(ip))) - { - token = op++; - *token = 0; - if (maybe_extMem) - offset = current - matchIndex; - DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", - (int)(anchor - (const BYTE *)source), 0, (int)(ip - (const BYTE *)source)); - goto _next_match; - } - } - - /* Prepare next loop */ - forwardH = LZ4_hashPosition(++ip, tableType); - } - -_last_literals: - /* Encode Last Literals */ - { - size_t lastRun = (size_t)(iend - anchor); - if ((outputDirective) && /* Check output buffer overflow */ - (op + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > olimit)) - { - if (outputDirective == fillOutput) - { - /* adapt lastRun to fill 'dst' */ - assert(olimit >= op); - lastRun = (size_t)(olimit - op) - 1 /*token*/; - lastRun -= (lastRun + 256 - RUN_MASK) / 256; /*additional length tokens*/ - } - else - { - assert(outputDirective == limitedOutput); - return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ - } - } - DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun); - if (lastRun >= RUN_MASK) - { - size_t accumulator = lastRun - RUN_MASK; - *op++ = RUN_MASK << ML_BITS; - for (; accumulator >= 255; accumulator -= 255) - *op++ = 255; - *op++ = (BYTE)accumulator; - } - else - { - *op++ = (BYTE)(lastRun << ML_BITS); - } - LZ4_memcpy(op, anchor, lastRun); - ip = anchor + lastRun; - op += lastRun; - } - - if (outputDirective == fillOutput) - { - *inputConsumed = (int)(((const char *)ip) - source); - } - result = (int)(((char *)op) - dest); - assert(result > 0); - DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result); - return result; -} - -/** LZ4_compress_generic() : - * inlined, to ensure branches are decided at compilation time; - * takes care of src == (NULL, 0) - * and forward the rest to LZ4_compress_generic_validated */ -LZ4_FORCE_INLINE int LZ4_compress_generic( - LZ4_stream_t_internal *const cctx, - const char *const src, - char *const dst, - const int srcSize, - int *inputConsumed, /* only written when outputDirective == fillOutput */ - const int dstCapacity, - const limitedOutput_directive outputDirective, - const tableType_t tableType, - const dict_directive dictDirective, - const dictIssue_directive dictIssue, - const int acceleration) -{ - DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i", - srcSize, dstCapacity); - - if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) - { - return 0; - } /* Unsupported srcSize, too large (or negative) */ - if (srcSize == 0) - { /* src == NULL supported if srcSize == 0 */ - if (outputDirective != notLimited && dstCapacity <= 0) - return 0; /* no output, can't write anything */ - DEBUGLOG(5, "Generating an empty block"); - assert(outputDirective == notLimited || dstCapacity >= 1); - assert(dst != NULL); - dst[0] = 0; - if (outputDirective == fillOutput) - { - assert(inputConsumed != NULL); - *inputConsumed = 0; - } - return 1; - } - assert(src != NULL); - - return LZ4_compress_generic_validated(cctx, src, dst, srcSize, - inputConsumed, /* only written into if outputDirective == fillOutput */ - dstCapacity, outputDirective, - tableType, dictDirective, dictIssue, acceleration); -} - -int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration) -{ - LZ4_stream_t_internal *const ctx = &LZ4_initStream(state, sizeof(LZ4_stream_t))->internal_donotuse; - assert(ctx != NULL); - if (acceleration < 1) - acceleration = LZ4_ACCELERATION_DEFAULT; - if (acceleration > LZ4_ACCELERATION_MAX) - acceleration = LZ4_ACCELERATION_MAX; - if (maxOutputSize >= LZ4_compressBound(inputSize)) - { - if (inputSize < LZ4_64Klimit) - { - return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); - } - else - { - const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; - return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); - } - } - else - { - if (inputSize < LZ4_64Klimit) - { - return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); - } - else - { - const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; - return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, noDict, noDictIssue, acceleration); - } - } -} - -/** - * LZ4_compress_fast_extState_fastReset() : - * A variant of LZ4_compress_fast_extState(). - * - * Using this variant avoids an expensive initialization step. It is only safe - * to call if the state buffer is known to be correctly initialized already - * (see comment in lz4.h on LZ4_resetStream_fast() for a definition of - * "correctly initialized"). - */ -int LZ4_compress_fast_extState_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration) -{ - LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse; - if (acceleration < 1) - acceleration = LZ4_ACCELERATION_DEFAULT; - if (acceleration > LZ4_ACCELERATION_MAX) - acceleration = LZ4_ACCELERATION_MAX; - - if (dstCapacity >= LZ4_compressBound(srcSize)) - { - if (srcSize < LZ4_64Klimit) - { - const tableType_t tableType = byU16; - LZ4_prepareTable(ctx, srcSize, tableType); - if (ctx->currentOffset) - { - return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration); - } - else - { - return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); - } - } - else - { - const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; - LZ4_prepareTable(ctx, srcSize, tableType); - return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); - } - } - else - { - if (srcSize < LZ4_64Klimit) - { - const tableType_t tableType = byU16; - LZ4_prepareTable(ctx, srcSize, tableType); - if (ctx->currentOffset) - { - return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration); - } - else - { - return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); - } - } - else - { - const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; - LZ4_prepareTable(ctx, srcSize, tableType); - return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); - } - } -} - -int LZ4_compress_fast(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration) -{ - int result; -#if (LZ4_HEAPMODE) - LZ4_stream_t *ctxPtr = ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ - if (ctxPtr == NULL) - return 0; -#else - LZ4_stream_t ctx; - LZ4_stream_t *const ctxPtr = &ctx; -#endif - result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration); - -#if (LZ4_HEAPMODE) - FREEMEM(ctxPtr); -#endif - return result; -} - -int LZ4_compress_default(const char *src, char *dst, int srcSize, int maxOutputSize) -{ - return LZ4_compress_fast(src, dst, srcSize, maxOutputSize, 1); -} - -/* Note!: This function leaves the stream in an unclean/broken state! - * It is not safe to subsequently use the same state with a _fastReset() or - * _continue() call without resetting it. */ -static int LZ4_compress_destSize_extState(LZ4_stream_t *state, const char *src, char *dst, int *srcSizePtr, int targetDstSize) -{ - void *const s = LZ4_initStream(state, sizeof(*state)); - assert(s != NULL); - (void)s; - - if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) - { /* compression success is guaranteed */ - return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); - } - else - { - if (*srcSizePtr < LZ4_64Klimit) - { - return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1); - } - else - { - tableType_t const addrMode = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; - return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1); - } - } -} - -int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize) -{ -#if (LZ4_HEAPMODE) - LZ4_stream_t *ctx = (LZ4_stream_t *)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ - if (ctx == NULL) - return 0; -#else - LZ4_stream_t ctxBody; - LZ4_stream_t *ctx = &ctxBody; -#endif - - int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); - -#if (LZ4_HEAPMODE) - FREEMEM(ctx); -#endif - return result; -} - -/*-****************************** - * Streaming functions - ********************************/ - -LZ4_stream_t *LZ4_createStream(void) -{ - LZ4_stream_t *const lz4s = (LZ4_stream_t *)ALLOC(sizeof(LZ4_stream_t)); - LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ - DEBUGLOG(4, "LZ4_createStream %p", lz4s); - if (lz4s == NULL) - return NULL; - LZ4_initStream(lz4s, sizeof(*lz4s)); - return lz4s; -} - -static size_t LZ4_stream_t_alignment(void) -{ -#if LZ4_ALIGN_TEST - typedef struct - { - char c; - LZ4_stream_t t; - } t_a; - return sizeof(t_a) - sizeof(LZ4_stream_t); -#else - return 1; /* effectively disabled */ -#endif -} - -LZ4_stream_t *LZ4_initStream(void *buffer, size_t size) -{ - DEBUGLOG(5, "LZ4_initStream"); - if (buffer == NULL) - { - return NULL; - } - if (size < sizeof(LZ4_stream_t)) - { - return NULL; - } - if (!LZ4_isAligned(buffer, LZ4_stream_t_alignment())) - return NULL; - MEM_INIT(buffer, 0, sizeof(LZ4_stream_t_internal)); - return (LZ4_stream_t *)buffer; -} - -/* resetStream is now deprecated, - * prefer initStream() which is more general */ -void LZ4_resetStream(LZ4_stream_t *LZ4_stream) -{ - DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); - MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t_internal)); -} - -void LZ4_resetStream_fast(LZ4_stream_t *ctx) -{ - LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32); -} - -int LZ4_freeStream(LZ4_stream_t *LZ4_stream) -{ - if (!LZ4_stream) - return 0; /* support free on NULL */ - DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream); - FREEMEM(LZ4_stream); - return (0); -} - -#define HASH_UNIT sizeof(reg_t) -int LZ4_loadDict(LZ4_stream_t *LZ4_dict, const char *dictionary, int dictSize) -{ - LZ4_stream_t_internal *dict = &LZ4_dict->internal_donotuse; - const tableType_t tableType = byU32; - const BYTE *p = (const BYTE *)dictionary; - const BYTE *const dictEnd = p + dictSize; - const BYTE *base; - - DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict); - - /* It's necessary to reset the context, - * and not just continue it with prepareTable() - * to avoid any risk of generating overflowing matchIndex - * when compressing using this dictionary */ - LZ4_resetStream(LZ4_dict); - - /* We always increment the offset by 64 KB, since, if the dict is longer, - * we truncate it to the last 64k, and if it's shorter, we still want to - * advance by a whole window length so we can provide the guarantee that - * there are only valid offsets in the window, which allows an optimization - * in LZ4_compress_fast_continue() where it uses noDictIssue even when the - * dictionary isn't a full 64k. */ - dict->currentOffset += 64 KB; - - if (dictSize < (int)HASH_UNIT) - { - return 0; - } - - if ((dictEnd - p) > 64 KB) - p = dictEnd - 64 KB; - base = dictEnd - dict->currentOffset; - dict->dictionary = p; - dict->dictSize = (U32)(dictEnd - p); - dict->tableType = (U32)tableType; - - while (p <= dictEnd - HASH_UNIT) - { - LZ4_putPosition(p, dict->hashTable, tableType, base); - p += 3; - } - - return (int)dict->dictSize; -} - -void LZ4_attach_dictionary(LZ4_stream_t *workingStream, const LZ4_stream_t *dictionaryStream) -{ - const LZ4_stream_t_internal *dictCtx = dictionaryStream == NULL ? NULL : &(dictionaryStream->internal_donotuse); - - DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)", - workingStream, dictionaryStream, - dictCtx != NULL ? dictCtx->dictSize : 0); - - if (dictCtx != NULL) - { - /* If the current offset is zero, we will never look in the - * external dictionary context, since there is no value a table - * entry can take that indicate a miss. In that case, we need - * to bump the offset to something non-zero. - */ - if (workingStream->internal_donotuse.currentOffset == 0) - { - workingStream->internal_donotuse.currentOffset = 64 KB; - } - - /* Don't actually attach an empty dictionary. - */ - if (dictCtx->dictSize == 0) - { - dictCtx = NULL; - } - } - workingStream->internal_donotuse.dictCtx = dictCtx; -} - -static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict, int nextSize) -{ - assert(nextSize >= 0); - if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) - { /* potential ptrdiff_t overflow (32-bits mode) */ - /* rescale hash table */ - U32 const delta = LZ4_dict->currentOffset - 64 KB; - const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; - int i; - DEBUGLOG(4, "LZ4_renormDictT"); - for (i = 0; i < LZ4_HASH_SIZE_U32; i++) - { - if (LZ4_dict->hashTable[i] < delta) - LZ4_dict->hashTable[i] = 0; - else - LZ4_dict->hashTable[i] -= delta; - } - LZ4_dict->currentOffset = 64 KB; - if (LZ4_dict->dictSize > 64 KB) - LZ4_dict->dictSize = 64 KB; - LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize; - } -} - -int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, - const char *source, char *dest, - int inputSize, int maxOutputSize, - int acceleration) -{ - const tableType_t tableType = byU32; - LZ4_stream_t_internal *streamPtr = &LZ4_stream->internal_donotuse; - const BYTE *dictEnd = streamPtr->dictionary + streamPtr->dictSize; - - DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize); - - LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ - if (acceleration < 1) - acceleration = LZ4_ACCELERATION_DEFAULT; - if (acceleration > LZ4_ACCELERATION_MAX) - acceleration = LZ4_ACCELERATION_MAX; - - /* invalidate tiny dictionaries */ - if ((streamPtr->dictSize - 1 < 4 - 1) /* intentional underflow */ - && (dictEnd != (const BYTE *)source)) - { - DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary); - streamPtr->dictSize = 0; - streamPtr->dictionary = (const BYTE *)source; - dictEnd = (const BYTE *)source; - } - - /* Check overlapping input/dictionary space */ - { - const BYTE *sourceEnd = (const BYTE *)source + inputSize; - if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) - { - streamPtr->dictSize = (U32)(dictEnd - sourceEnd); - if (streamPtr->dictSize > 64 KB) - streamPtr->dictSize = 64 KB; - if (streamPtr->dictSize < 4) - streamPtr->dictSize = 0; - streamPtr->dictionary = dictEnd - streamPtr->dictSize; - } - } - - /* prefix mode : source data follows dictionary */ - if (dictEnd == (const BYTE *)source) - { - if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) - return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration); - else - return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration); - } - - /* external dictionary mode */ - { - int result; - if (streamPtr->dictCtx) - { - /* We depend here on the fact that dictCtx'es (produced by - * LZ4_loadDict) guarantee that their tables contain no references - * to offsets between dictCtx->currentOffset - 64 KB and - * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe - * to use noDictIssue even when the dict isn't a full 64 KB. - */ - if (inputSize > 4 KB) - { - /* For compressing large blobs, it is faster to pay the setup - * cost to copy the dictionary's tables into the active context, - * so that the compression loop is only looking into one table. - */ - LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr)); - result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); - } - else - { - result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration); - } - } - else - { - if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) - { - result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration); - } - else - { - result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); - } - } - streamPtr->dictionary = (const BYTE *)source; - streamPtr->dictSize = (U32)inputSize; - return result; - } -} - -/* Hidden debug function, to force-test external dictionary mode */ -int LZ4_compress_forceExtDict(LZ4_stream_t *LZ4_dict, const char *source, char *dest, int srcSize) -{ - LZ4_stream_t_internal *streamPtr = &LZ4_dict->internal_donotuse; - int result; - - LZ4_renormDictT(streamPtr, srcSize); - - if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) - { - result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, dictSmall, 1); - } - else - { - result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, noDictIssue, 1); - } - - streamPtr->dictionary = (const BYTE *)source; - streamPtr->dictSize = (U32)srcSize; - - return result; -} - -/*! LZ4_saveDict() : - * If previously compressed data block is not guaranteed to remain available at its memory location, - * save it into a safer place (char* safeBuffer). - * Note : you don't need to call LZ4_loadDict() afterwards, - * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue(). - * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error. - */ -int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize) -{ - LZ4_stream_t_internal *const dict = &LZ4_dict->internal_donotuse; - const BYTE *const previousDictEnd = dict->dictionary + dict->dictSize; - - if ((U32)dictSize > 64 KB) - { - dictSize = 64 KB; - } /* useless to define a dictionary > 64 KB */ - if ((U32)dictSize > dict->dictSize) - { - dictSize = (int)dict->dictSize; - } - - if (safeBuffer == NULL) - assert(dictSize == 0); - if (dictSize > 0) - memmove(safeBuffer, previousDictEnd - dictSize, dictSize); - - dict->dictionary = (const BYTE *)safeBuffer; - dict->dictSize = (U32)dictSize; - - return dictSize; -} - -/*-******************************* - * Decompression functions - ********************************/ - -typedef enum -{ - endOnOutputSize = 0, - endOnInputSize = 1 -} endCondition_directive; -typedef enum -{ - decode_full_block = 0, - partial_decode = 1 -} earlyEnd_directive; - -#undef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -/* Read the variable-length literal or match length. - * - * ip - pointer to use as input. - * lencheck - end ip. Return an error if ip advances >= lencheck. - * loop_check - check ip >= lencheck in body of loop. Returns loop_error if so. - * initial_check - check ip >= lencheck before start of loop. Returns initial_error if so. - * error (output) - error code. Should be set to 0 before call. - */ -typedef enum -{ - loop_error = -2, - initial_error = -1, - ok = 0 -} variable_length_error; -LZ4_FORCE_INLINE unsigned -read_variable_length(const BYTE **ip, const BYTE *lencheck, - int loop_check, int initial_check, - variable_length_error *error) -{ - U32 length = 0; - U32 s; - if (initial_check && unlikely((*ip) >= lencheck)) - { /* overflow detection */ - *error = initial_error; - return length; - } - do - { - s = **ip; - (*ip)++; - length += s; - if (loop_check && unlikely((*ip) >= lencheck)) - { /* overflow detection */ - *error = loop_error; - return length; - } - } while (s == 255); - - return length; -} - -/*! LZ4_decompress_generic() : - * This generic decompression function covers all use cases. - * It shall be instantiated several times, using different sets of directives. - * Note that it is important for performance that this function really get inlined, - * in order to remove useless branches during compilation optimization. - */ -LZ4_FORCE_INLINE int -LZ4_decompress_generic( - const char *const src, - char *const dst, - int srcSize, - int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */ - - endCondition_directive endOnInput, /* endOnOutputSize, endOnInputSize */ - earlyEnd_directive partialDecoding, /* full, partial */ - dict_directive dict, /* noDict, withPrefix64k, usingExtDict */ - const BYTE *const lowPrefix, /* always <= dst, == dst when no prefix */ - const BYTE *const dictStart, /* only if dict==usingExtDict */ - const size_t dictSize /* note : = 0 if noDict */ -) -{ - if (src == NULL) - { - return -1; - } - - { - const BYTE *ip = (const BYTE *)src; - const BYTE *const iend = ip + srcSize; - - BYTE *op = (BYTE *)dst; - BYTE *const oend = op + outputSize; - BYTE *cpy; - - const BYTE *const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize; - - const int safeDecode = (endOnInput == endOnInputSize); - const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); - - /* Set up the "end" pointers for the shortcut. */ - const BYTE *const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; - const BYTE *const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; - - const BYTE *match; - size_t offset; - unsigned token; - size_t length; - - DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize); - - /* Special cases */ - assert(lowPrefix <= op); - if ((endOnInput) && (unlikely(outputSize == 0))) - { - /* Empty output buffer */ - if (partialDecoding) - return 0; - return ((srcSize == 1) && (*ip == 0)) ? 0 : -1; - } - if ((!endOnInput) && (unlikely(outputSize == 0))) - { - return (*ip == 0 ? 1 : -1); - } - if ((endOnInput) && unlikely(srcSize == 0)) - { - return -1; - } - - /* Currently the fast loop shows a regression on qualcomm arm chips. */ -#if LZ4_FAST_DEC_LOOP - if ((oend - op) < FASTLOOP_SAFE_DISTANCE) - { - DEBUGLOG(6, "skip fast decode loop"); - goto safe_decode; - } - - /* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */ - while (1) - { - /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ - assert(oend - op >= FASTLOOP_SAFE_DISTANCE); - if (endOnInput) - { - assert(ip < iend); - } - token = *ip++; - length = token >> ML_BITS; /* literal length */ - - assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ - - /* decode literal length */ - if (length == RUN_MASK) - { - variable_length_error error = ok; - length += read_variable_length(&ip, iend - RUN_MASK, (int)endOnInput, (int)endOnInput, &error); - if (error == initial_error) - { - goto _output_error; - } - if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)(op))) - { - goto _output_error; - } /* overflow detection */ - if ((safeDecode) && unlikely((uptrval)(ip) + length < (uptrval)(ip))) - { - goto _output_error; - } /* overflow detection */ - - /* copy literals */ - cpy = op + length; - LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); - if (endOnInput) - { /* LZ4_decompress_safe() */ - if ((cpy > oend - 32) || (ip + length > iend - 32)) - { - goto safe_literal_copy; - } - LZ4_wildCopy32(op, ip, cpy); - } - else - { /* LZ4_decompress_fast() */ - if (cpy > oend - 8) - { - goto safe_literal_copy; - } - LZ4_wildCopy8(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : - * it doesn't know input length, and only relies on end-of-block properties */ - } - ip += length; - op = cpy; - } - else - { - cpy = op + length; - if (endOnInput) - { /* LZ4_decompress_safe() */ - DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length); - /* We don't need to check oend, since we check it once for each loop below */ - if (ip > iend - (16 + 1 /*max lit + offset + nextToken*/)) - { - goto safe_literal_copy; - } - /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ - LZ4_memcpy(op, ip, 16); - } - else - { /* LZ4_decompress_fast() */ - /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : - * it doesn't know input length, and relies on end-of-block properties */ - LZ4_memcpy(op, ip, 8); - if (length > 8) - { - LZ4_memcpy(op + 8, ip + 8, 8); - } - } - ip += length; - op = cpy; - } - - /* get offset */ - offset = LZ4_readLE16(ip); - ip += 2; - match = op - offset; - assert(match <= op); - - /* get matchlength */ - length = token & ML_MASK; - - if (length == ML_MASK) - { - variable_length_error error = ok; - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) - { - goto _output_error; - } /* Error : offset outside buffers */ - length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error); - if (error != ok) - { - goto _output_error; - } - if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)op)) - { - goto _output_error; - } /* overflow detection */ - length += MINMATCH; - if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) - { - goto safe_match_copy; - } - } - else - { - length += MINMATCH; - if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) - { - goto safe_match_copy; - } - - /* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */ - if ((dict == withPrefix64k) || (match >= lowPrefix)) - { - if (offset >= 8) - { - assert(match >= lowPrefix); - assert(match <= op); - assert(op + 18 <= oend); - - LZ4_memcpy(op, match, 8); - LZ4_memcpy(op + 8, match + 8, 8); - LZ4_memcpy(op + 16, match + 16, 2); - op += length; - continue; - } - } - } - - if (checkOffset && (unlikely(match + dictSize < lowPrefix))) - { - goto _output_error; - } /* Error : offset outside buffers */ - /* match starting within external dictionary */ - if ((dict == usingExtDict) && (match < lowPrefix)) - { - if (unlikely(op + length > oend - LASTLITERALS)) - { - if (partialDecoding) - { - DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd"); - length = MIN(length, (size_t)(oend - op)); - } - else - { - goto _output_error; /* end-of-block condition violated */ - } - } - - if (length <= (size_t)(lowPrefix - match)) - { - /* match fits entirely within external dictionary : just copy */ - memmove(op, dictEnd - (lowPrefix - match), length); - op += length; - } - else - { - /* match stretches into both external dictionary and current block */ - size_t const copySize = (size_t)(lowPrefix - match); - size_t const restSize = length - copySize; - LZ4_memcpy(op, dictEnd - copySize, copySize); - op += copySize; - if (restSize > (size_t)(op - lowPrefix)) - { /* overlap copy */ - BYTE *const endOfMatch = op + restSize; - const BYTE *copyFrom = lowPrefix; - while (op < endOfMatch) - { - *op++ = *copyFrom++; - } - } - else - { - LZ4_memcpy(op, lowPrefix, restSize); - op += restSize; - } - } - continue; - } - - /* copy match within block */ - cpy = op + length; - - assert((op <= oend) && (oend - op >= 32)); - if (unlikely(offset < 16)) - { - LZ4_memcpy_using_offset(op, match, cpy, offset); - } - else - { - LZ4_wildCopy32(op, match, cpy); - } - - op = cpy; /* wildcopy correction */ - } - safe_decode: -#endif - - /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ - while (1) - { - token = *ip++; - length = token >> ML_BITS; /* literal length */ - - assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ - - /* A two-stage shortcut for the most common case: - * 1) If the literal length is 0..14, and there is enough space, - * enter the shortcut and copy 16 bytes on behalf of the literals - * (in the fast mode, only 8 bytes can be safely copied this way). - * 2) Further if the match length is 4..18, copy 18 bytes in a similar - * manner; but we ensure that there's enough space in the output for - * those 18 bytes earlier, upon entering the shortcut (in other words, - * there is a combined check for both stages). - */ - if ((endOnInput ? length != RUN_MASK : length <= 8) - /* strictly "less than" on input, to re-enter the loop with at least one byte */ - && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend))) - { - /* Copy the literals */ - LZ4_memcpy(op, ip, endOnInput ? 16 : 8); - op += length; - ip += length; - - /* The second stage: prepare for match copying, decode full info. - * If it doesn't work out, the info won't be wasted. */ - length = token & ML_MASK; /* match length */ - offset = LZ4_readLE16(ip); - ip += 2; - match = op - offset; - assert(match <= op); /* check overflow */ - - /* Do not deal with overlapping matches. */ - if ((length != ML_MASK) && (offset >= 8) && (dict == withPrefix64k || match >= lowPrefix)) - { - /* Copy the match. */ - LZ4_memcpy(op + 0, match + 0, 8); - LZ4_memcpy(op + 8, match + 8, 8); - LZ4_memcpy(op + 16, match + 16, 2); - op += length + MINMATCH; - /* Both stages worked, load the next token. */ - continue; - } - - /* The second stage didn't work out, but the info is ready. - * Propel it right to the point of match copying. */ - goto _copy_match; - } - - /* decode literal length */ - if (length == RUN_MASK) - { - variable_length_error error = ok; - length += read_variable_length(&ip, iend - RUN_MASK, (int)endOnInput, (int)endOnInput, &error); - if (error == initial_error) - { - goto _output_error; - } - if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)(op))) - { - goto _output_error; - } /* overflow detection */ - if ((safeDecode) && unlikely((uptrval)(ip) + length < (uptrval)(ip))) - { - goto _output_error; - } /* overflow detection */ - } - - /* copy literals */ - cpy = op + length; -#if LZ4_FAST_DEC_LOOP - safe_literal_copy: -#endif - LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); - if (((endOnInput) && ((cpy > oend - MFLIMIT) || (ip + length > iend - (2 + 1 + LASTLITERALS)))) || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) - { - /* We've either hit the input parsing restriction or the output parsing restriction. - * In the normal scenario, decoding a full block, it must be the last sequence, - * otherwise it's an error (invalid input or dimensions). - * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow. - */ - if (partialDecoding) - { - /* Since we are partial decoding we may be in this block because of the output parsing - * restriction, which is not valid since the output buffer is allowed to be undersized. - */ - assert(endOnInput); - DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end") - DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length); - DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op)); - DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip)); - /* Finishing in the middle of a literals segment, - * due to lack of input. - */ - if (ip + length > iend) - { - length = (size_t)(iend - ip); - cpy = op + length; - } - /* Finishing in the middle of a literals segment, - * due to lack of output space. - */ - if (cpy > oend) - { - cpy = oend; - assert(op <= oend); - length = (size_t)(oend - op); - } - } - else - { - /* We must be on the last sequence because of the parsing limitations so check - * that we exactly regenerate the original size (must be exact when !endOnInput). - */ - if ((!endOnInput) && (cpy != oend)) - { - goto _output_error; - } - /* We must be on the last sequence (or invalid) because of the parsing limitations - * so check that we exactly consume the input and don't overrun the output buffer. - */ - if ((endOnInput) && ((ip + length != iend) || (cpy > oend))) - { - DEBUGLOG(6, "should have been last run of literals") - DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip + length, iend); - DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend); - goto _output_error; - } - } - memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */ - ip += length; - op += length; - /* Necessarily EOF when !partialDecoding. - * When partialDecoding, it is EOF if we've either - * filled the output buffer or - * can't proceed with reading an offset for following match. - */ - if (!partialDecoding || (cpy == oend) || (ip >= (iend - 2))) - { - break; - } - } - else - { - LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ - ip += length; - op = cpy; - } - - /* get offset */ - offset = LZ4_readLE16(ip); - ip += 2; - match = op - offset; - - /* get matchlength */ - length = token & ML_MASK; - - _copy_match: - if (length == ML_MASK) - { - variable_length_error error = ok; - length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error); - if (error != ok) - goto _output_error; - if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)op)) - goto _output_error; /* overflow detection */ - } - length += MINMATCH; - -#if LZ4_FAST_DEC_LOOP - safe_match_copy: -#endif - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) - goto _output_error; /* Error : offset outside buffers */ - /* match starting within external dictionary */ - if ((dict == usingExtDict) && (match < lowPrefix)) - { - if (unlikely(op + length > oend - LASTLITERALS)) - { - if (partialDecoding) - length = MIN(length, (size_t)(oend - op)); - else - goto _output_error; /* doesn't respect parsing restriction */ - } - - if (length <= (size_t)(lowPrefix - match)) - { - /* match fits entirely within external dictionary : just copy */ - memmove(op, dictEnd - (lowPrefix - match), length); - op += length; - } - else - { - /* match stretches into both external dictionary and current block */ - size_t const copySize = (size_t)(lowPrefix - match); - size_t const restSize = length - copySize; - LZ4_memcpy(op, dictEnd - copySize, copySize); - op += copySize; - if (restSize > (size_t)(op - lowPrefix)) - { /* overlap copy */ - BYTE *const endOfMatch = op + restSize; - const BYTE *copyFrom = lowPrefix; - while (op < endOfMatch) - *op++ = *copyFrom++; - } - else - { - LZ4_memcpy(op, lowPrefix, restSize); - op += restSize; - } - } - continue; - } - assert(match >= lowPrefix); - - /* copy match within block */ - cpy = op + length; - - /* partialDecoding : may end anywhere within the block */ - assert(op <= oend); - if (partialDecoding && (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) - { - size_t const mlen = MIN(length, (size_t)(oend - op)); - const BYTE *const matchEnd = match + mlen; - BYTE *const copyEnd = op + mlen; - if (matchEnd > op) - { /* overlap copy */ - while (op < copyEnd) - { - *op++ = *match++; - } - } - else - { - LZ4_memcpy(op, match, mlen); - } - op = copyEnd; - if (op == oend) - { - break; - } - continue; - } - - if (unlikely(offset < 8)) - { - LZ4_write32(op, 0); /* silence msan warning when offset==0 */ - op[0] = match[0]; - op[1] = match[1]; - op[2] = match[2]; - op[3] = match[3]; - match += inc32table[offset]; - LZ4_memcpy(op + 4, match, 4); - match -= dec64table[offset]; - } - else - { - LZ4_memcpy(op, match, 8); - match += 8; - } - op += 8; - - if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) - { - BYTE *const oCopyLimit = oend - (WILDCOPYLENGTH - 1); - if (cpy > oend - LASTLITERALS) - { - goto _output_error; - } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ - if (op < oCopyLimit) - { - LZ4_wildCopy8(op, match, oCopyLimit); - match += oCopyLimit - op; - op = oCopyLimit; - } - while (op < cpy) - { - *op++ = *match++; - } - } - else - { - LZ4_memcpy(op, match, 8); - if (length > 16) - { - LZ4_wildCopy8(op + 8, match + 8, cpy); - } - } - op = cpy; /* wildcopy correction */ - } - - /* end of decoding */ - if (endOnInput) - { - DEBUGLOG(5, "decoded %i bytes", (int)(((char *)op) - dst)); - return (int)(((char *)op) - dst); /* Nb of output bytes decoded */ - } - else - { - return (int)(((const char *)ip) - src); /* Nb of input bytes read */ - } - - /* Overflow error detected */ - _output_error: - return (int)(-(((const char *)ip) - src)) - 1; - } -} - -/*===== Instantiate the API decoding functions. =====*/ - -LZ4_FORCE_O2 -int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, - endOnInputSize, decode_full_block, noDict, - (BYTE *)dest, NULL, 0); -} - -LZ4_FORCE_O2 -int LZ4_decompress_safe_partial(const char *src, char *dst, int compressedSize, int targetOutputSize, int dstCapacity) -{ - dstCapacity = MIN(targetOutputSize, dstCapacity); - return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, - endOnInputSize, partial_decode, - noDict, (BYTE *)dst, NULL, 0); -} - -LZ4_FORCE_O2 -int LZ4_decompress_fast(const char *source, char *dest, int originalSize) -{ - return LZ4_decompress_generic(source, dest, 0, originalSize, - endOnOutputSize, decode_full_block, withPrefix64k, - (BYTE *)dest - 64 KB, NULL, 0); -} - -/*===== Instantiate a few more decoding cases, used more than once. =====*/ - -LZ4_FORCE_O2 /* Exported, an obsolete API function. */ - int - LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, int compressedSize, int maxOutputSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, withPrefix64k, - (BYTE *)dest - 64 KB, NULL, 0); -} - -/* Another obsolete API function, paired with the previous one. */ -int LZ4_decompress_fast_withPrefix64k(const char *source, char *dest, int originalSize) -{ - /* LZ4_decompress_fast doesn't validate match offsets, - * and thus serves well with any prefixed dictionary. */ - return LZ4_decompress_fast(source, dest, originalSize); -} - -LZ4_FORCE_O2 -static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest, int compressedSize, int maxOutputSize, - size_t prefixSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, noDict, - (BYTE *)dest - prefixSize, NULL, 0); -} - -LZ4_FORCE_O2 -int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, - int compressedSize, int maxOutputSize, - const void *dictStart, size_t dictSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, usingExtDict, - (BYTE *)dest, (const BYTE *)dictStart, dictSize); -} - -LZ4_FORCE_O2 -static int LZ4_decompress_fast_extDict(const char *source, char *dest, int originalSize, - const void *dictStart, size_t dictSize) -{ - return LZ4_decompress_generic(source, dest, 0, originalSize, - endOnOutputSize, decode_full_block, usingExtDict, - (BYTE *)dest, (const BYTE *)dictStart, dictSize); -} - -/* The "double dictionary" mode, for use with e.g. ring buffers: the first part - * of the dictionary is passed as prefix, and the second via dictStart + dictSize. - * These routines are used only once, in LZ4_decompress_*_continue(). - */ -LZ4_FORCE_INLINE -int LZ4_decompress_safe_doubleDict(const char *source, char *dest, int compressedSize, int maxOutputSize, - size_t prefixSize, const void *dictStart, size_t dictSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, usingExtDict, - (BYTE *)dest - prefixSize, (const BYTE *)dictStart, dictSize); -} - -LZ4_FORCE_INLINE -int LZ4_decompress_fast_doubleDict(const char *source, char *dest, int originalSize, - size_t prefixSize, const void *dictStart, size_t dictSize) -{ - return LZ4_decompress_generic(source, dest, 0, originalSize, - endOnOutputSize, decode_full_block, usingExtDict, - (BYTE *)dest - prefixSize, (const BYTE *)dictStart, dictSize); -} - -/*===== streaming decompression functions =====*/ - -LZ4_streamDecode_t *LZ4_createStreamDecode(void) -{ - LZ4_streamDecode_t *lz4s = (LZ4_streamDecode_t *)ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); - LZ4_STATIC_ASSERT(LZ4_STREAMDECODESIZE >= sizeof(LZ4_streamDecode_t_internal)); /* A compilation error here means LZ4_STREAMDECODESIZE is not large enough */ - return lz4s; -} - -int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream) -{ - if (LZ4_stream == NULL) - { - return 0; - } /* support free on NULL */ - FREEMEM(LZ4_stream); - return 0; -} - -/*! LZ4_setStreamDecode() : - * Use this function to instruct where to find the dictionary. - * This function is not necessary if previous data is still available where it was decoded. - * Loading a size of 0 is allowed (same effect as no dictionary). - * @return : 1 if OK, 0 if error - */ -int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize) -{ - LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse; - lz4sd->prefixSize = (size_t)dictSize; - lz4sd->prefixEnd = (const BYTE *)dictionary + dictSize; - lz4sd->externalDict = NULL; - lz4sd->extDictSize = 0; - return 1; -} - -/*! LZ4_decoderRingBufferSize() : - * when setting a ring buffer for streaming decompression (optional scenario), - * provides the minimum size of this ring buffer - * to be compatible with any source respecting maxBlockSize condition. - * Note : in a ring buffer scenario, - * blocks are presumed decompressed next to each other. - * When not enough space remains for next block (remainingSize < maxBlockSize), - * decoding resumes from beginning of ring buffer. - * @return : minimum ring buffer size, - * or 0 if there is an error (invalid maxBlockSize). - */ -int LZ4_decoderRingBufferSize(int maxBlockSize) -{ - if (maxBlockSize < 0) - return 0; - if (maxBlockSize > LZ4_MAX_INPUT_SIZE) - return 0; - if (maxBlockSize < 16) - maxBlockSize = 16; - return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize); -} - -/* -*_continue() : - These decoding functions allow decompression of multiple blocks in "streaming" mode. - Previously decoded blocks must still be available at the memory position where they were decoded. - If it's not possible, save the relevant part of decoded data into a safe buffer, - and indicate where it stands using LZ4_setStreamDecode() -*/ -LZ4_FORCE_O2 -int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int compressedSize, int maxOutputSize) -{ - LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse; - int result; - - if (lz4sd->prefixSize == 0) - { - /* The first call, no dictionary yet. */ - assert(lz4sd->extDictSize == 0); - result = LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); - if (result <= 0) - return result; - lz4sd->prefixSize = (size_t)result; - lz4sd->prefixEnd = (BYTE *)dest + result; - } - else if (lz4sd->prefixEnd == (BYTE *)dest) - { - /* They're rolling the current segment. */ - if (lz4sd->prefixSize >= 64 KB - 1) - result = LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); - else if (lz4sd->extDictSize == 0) - result = LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, - lz4sd->prefixSize); - else - result = LZ4_decompress_safe_doubleDict(source, dest, compressedSize, maxOutputSize, - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize += (size_t)result; - lz4sd->prefixEnd += result; - } - else - { - /* The buffer wraps around, or they're switching to another buffer. */ - lz4sd->extDictSize = lz4sd->prefixSize; - lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; - result = LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, - lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize = (size_t)result; - lz4sd->prefixEnd = (BYTE *)dest + result; - } - - return result; -} - -LZ4_FORCE_O2 -int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int originalSize) -{ - LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse; - int result; - assert(originalSize >= 0); - - if (lz4sd->prefixSize == 0) - { - assert(lz4sd->extDictSize == 0); - result = LZ4_decompress_fast(source, dest, originalSize); - if (result <= 0) - return result; - lz4sd->prefixSize = (size_t)originalSize; - lz4sd->prefixEnd = (BYTE *)dest + originalSize; - } - else if (lz4sd->prefixEnd == (BYTE *)dest) - { - if (lz4sd->prefixSize >= 64 KB - 1 || lz4sd->extDictSize == 0) - result = LZ4_decompress_fast(source, dest, originalSize); - else - result = LZ4_decompress_fast_doubleDict(source, dest, originalSize, - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize += (size_t)originalSize; - lz4sd->prefixEnd += originalSize; - } - else - { - lz4sd->extDictSize = lz4sd->prefixSize; - lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; - result = LZ4_decompress_fast_extDict(source, dest, originalSize, - lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize = (size_t)originalSize; - lz4sd->prefixEnd = (BYTE *)dest + originalSize; - } - - return result; -} - -/* -Advanced decoding functions : -*_usingDict() : - These decoding functions work the same as "_continue" ones, - the dictionary must be explicitly provided within parameters -*/ - -int LZ4_decompress_safe_usingDict(const char *source, char *dest, int compressedSize, int maxOutputSize, const char *dictStart, int dictSize) -{ - if (dictSize == 0) - return LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); - if (dictStart + dictSize == dest) - { - if (dictSize >= 64 KB - 1) - { - return LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); - } - assert(dictSize >= 0); - return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, (size_t)dictSize); - } - assert(dictSize >= 0); - return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize); -} - -int LZ4_decompress_fast_usingDict(const char *source, char *dest, int originalSize, const char *dictStart, int dictSize) -{ - if (dictSize == 0 || dictStart + dictSize == dest) - return LZ4_decompress_fast(source, dest, originalSize); - assert(dictSize >= 0); - return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize); -} - -/*=************************************************* - * Obsolete Functions - ***************************************************/ -/* obsolete compression functions */ -int LZ4_compress_limitedOutput(const char *source, char *dest, int inputSize, int maxOutputSize) -{ - return LZ4_compress_default(source, dest, inputSize, maxOutputSize); -} -int LZ4_compress(const char *src, char *dest, int srcSize) -{ - return LZ4_compress_default(src, dest, srcSize, LZ4_compressBound(srcSize)); -} -int LZ4_compress_limitedOutput_withState(void *state, const char *src, char *dst, int srcSize, int dstSize) -{ - return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1); -} -int LZ4_compress_withState(void *state, const char *src, char *dst, int srcSize) -{ - return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1); -} -int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_stream, const char *src, char *dst, int srcSize, int dstCapacity) -{ - return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1); -} -int LZ4_compress_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize) -{ - return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1); -} - -/* -These decompression functions are deprecated and should no longer be used. -They are only provided here for compatibility with older user programs. -- LZ4_uncompress is totally equivalent to LZ4_decompress_fast -- LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe -*/ -int LZ4_uncompress(const char *source, char *dest, int outputSize) -{ - return LZ4_decompress_fast(source, dest, outputSize); -} -int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize, int maxOutputSize) -{ - return LZ4_decompress_safe(source, dest, isize, maxOutputSize); -} - -/* Obsolete Streaming functions */ - -int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; } - -int LZ4_resetStreamState(void *state, char *inputBuffer) -{ - (void)inputBuffer; - LZ4_resetStream((LZ4_stream_t *)state); - return 0; -} - -void *LZ4_create(char *inputBuffer) -{ - (void)inputBuffer; - return LZ4_createStream(); -} - -char *LZ4_slideInputBuffer(void *state) -{ - /* avoid const char * -> char * conversion warning */ - return (char *)(uptrval)((LZ4_stream_t *)state)->internal_donotuse.dictionary; -} - -#endif /* LZ4_COMMONDEFS_ONLY */ - -#else - -#endif \ No newline at end of file diff --git a/kernel/src/libs/printk.rs b/kernel/src/libs/printk.rs index 45b5bb3a..ad2ae4cc 100644 --- a/kernel/src/libs/printk.rs +++ b/kernel/src/libs/printk.rs @@ -32,18 +32,6 @@ macro_rules! println { ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); } -/// 指定颜色,彩色输出 -/// @param FRcolor 前景色 -/// @param BKcolor 背景色 -#[macro_export] -macro_rules! printk_color { - - ($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => { - use alloc; - $crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str()) - }; -} - #[macro_export] macro_rules! kdebug { ($($arg:tt)*) => { diff --git a/kernel/src/libs/stdlib.c b/kernel/src/libs/stdlib.c deleted file mode 100644 index 6e03bd45..00000000 --- a/kernel/src/libs/stdlib.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -/** - * @brief 将长整型转换为字符串 - * - * @param input 输入的数据 - * @return const char* 结果字符串 - */ -const char *ltoa(long input) -{ - /* large enough for -9223372036854775808 */ - static char buffer[21] = {0}; - char *pos = buffer + sizeof(buffer) - 1; - int neg = input < 0; - unsigned long n = neg ? -input : input; - - *pos-- = '\0'; - do - { - *pos-- = '0' + n % 10; - n /= 10; - if (pos < buffer) - return pos + 1; - } while (n); - - if (neg) - *pos-- = '-'; - return pos + 1; -} \ No newline at end of file diff --git a/kernel/src/libs/sys/Makefile b/kernel/src/libs/sys/Makefile deleted file mode 100644 index bf90c8ea..00000000 --- a/kernel/src/libs/sys/Makefile +++ /dev/null @@ -1,14 +0,0 @@ - -CFLAGS += -I . - -kernel_lib_sys_objs:= $(shell find ./*.c) - -ECHO: - @echo "$@" - -$(kernel_lib_sys_objs): ECHO - $(CC) $(CFLAGS) -c $@ -o $@.o - -all: $(kernel_lib_sys_objs) - @echo $(kernel_lib_sys_objs) - diff --git a/kernel/src/libs/sys/wait.c b/kernel/src/libs/sys/wait.c deleted file mode 100644 index 71d142c6..00000000 --- a/kernel/src/libs/sys/wait.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -/** - * @brief 等待指定pid的子进程退出 - * - * @param pid 子进程的pid - * @param stat_loc 返回的子进程结束状态 - * @param options 额外的控制选项 - * @return pid_t - */ -pid_t waitpid(pid_t pid, int *stat_loc, int options) -{ - return (pid_t)enter_syscall_int(SYS_WAIT4, (uint64_t)pid, (uint64_t)stat_loc, options, 0, 0, 0); -} \ No newline at end of file diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 1e9bcfbc..7a89cecd 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -14,6 +14,7 @@ use crate::{ resource::{RLimit64, RUsage}, ProcessManager, }, + syscall::user_access::check_and_clone_cstr, }; use num_traits::FromPrimitive; @@ -58,10 +59,6 @@ pub const SYS_SCHED: usize = 100003; #[derive(Debug)] pub struct Syscall; -extern "C" { - fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize; -} - impl Syscall { /// 初始化系统调用 #[inline(never)] @@ -1047,7 +1044,16 @@ impl Syscall { front_color: u32, back_color: u32, ) -> Result { - return Ok(unsafe { do_put_string(s, front_color, back_color) }); + // todo: 删除这个系统调用 + let s = check_and_clone_cstr(s, Some(4096))?; + let fr = (front_color & 0x00ff0000) >> 16; + let fg = (front_color & 0x0000ff00) >> 8; + let fb = front_color & 0x000000ff; + let br = (back_color & 0x00ff0000) >> 16; + let bg = (back_color & 0x0000ff00) >> 8; + let bb = back_color & 0x000000ff; + print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); + return Ok(s.len()); } pub fn reboot() -> Result { diff --git a/kernel/src/syscall/syscall.c b/kernel/src/syscall/syscall.c index 7ecc43eb..7dffe970 100644 --- a/kernel/src/syscall/syscall.c +++ b/kernel/src/syscall/syscall.c @@ -1,11 +1,11 @@ #include "syscall.h" +#include #include #include #include #include #include #include