forked from MacPaw/XADMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZSS.h
97 lines (66 loc) · 2.82 KB
/
LZSS.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* LZSS.h
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifndef __LZSS_H__
#define __LZSS_H__
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct LZSS
{
uint8_t *window;
size_t mask;
int64_t position;
} LZSS;
bool InitializeLZSS(LZSS *self,size_t windowsize);
void CleanupLZSS(LZSS *self);
void RestartLZSS(LZSS *self);
static inline int64_t LZSSPosition(LZSS *self) { return self->position; }
static inline size_t LZSSWindowMask(LZSS *self) { return self->mask; }
static inline size_t LZSSWindowSize(LZSS *self) { return self->mask+1; }
static inline uint8_t *LZSSWindowPointer(LZSS *self) { return self->window; }
static inline size_t LZSSWindowOffsetForPosition(LZSS *self,int64_t pos) { return pos&self->mask; }
static inline uint8_t *LZSSWindowPointerForPosition(LZSS *self,int64_t pos) { return &self->window[LZSSWindowOffsetForPosition(self,pos)]; }
static inline size_t CurrentLZSSWindowOffset(LZSS *self) { return LZSSWindowOffsetForPosition(self,self->position); }
static inline uint8_t *CurrentLZSSWindowPointer(LZSS *self) { return LZSSWindowPointerForPosition(self,self->position); }
static inline int64_t NextLZSSWindowEdgeAfterPosition(LZSS *self,int64_t pos) { return (pos+LZSSWindowSize(self))&~(int64_t)LZSSWindowMask(self); }
static inline int64_t NextLZSSWindowEdge(LZSS *self) { return NextLZSSWindowEdgeAfterPosition(self,self->position); }
static inline uint8_t GetByteFromLZSSWindow(LZSS *self,int64_t pos)
{
return *LZSSWindowPointerForPosition(self,pos);
}
void CopyBytesFromLZSSWindow(LZSS *self,uint8_t *buffer,int64_t startpos,int length);
static inline void EmitLZSSLiteral(LZSS *self,uint8_t literal)
{
*CurrentLZSSWindowPointer(self)=literal;
// self->window[(self->position)&self->mask]=literal;
self->position++;
}
static inline void EmitLZSSMatch(LZSS *self,int offset,int length)
{
int windowoffs=CurrentLZSSWindowOffset(self);
for(int i=0;i<length;i++)
{
self->window[(windowoffs+i)&LZSSWindowMask(self)]=
self->window[(windowoffs+i-offset)&LZSSWindowMask(self)];
}
self->position+=length;
}
#endif