]>
Commit | Line | Data |
---|---|---|
3d04d29e PN |
1 | #define ZIP_BIGLEN 254 |
2 | #define ZIP_END 255 | |
3 | ||
29b14d5f PN |
4 | /* Entry encoding */ |
5 | #define ZIP_ENC_RAW 0 | |
6 | #define ZIP_ENC_SHORT 1 | |
7 | #define ZIP_ENC_INT 2 | |
8 | #define ZIP_ENC_LLONG 3 | |
9 | #define ZIP_ENCODING(p) ((p)[0] >> 6) | |
3d04d29e | 10 | |
29b14d5f PN |
11 | /* Length encoding for raw entries */ |
12 | #define ZIP_LEN_INLINE 0 | |
13 | #define ZIP_LEN_UINT16 1 | |
14 | #define ZIP_LEN_UINT32 2 | |
3d04d29e | 15 | |
29b14d5f PN |
16 | static unsigned int zipEncodingSize(char encoding) { |
17 | if (encoding == ZIP_ENC_SHORT) { | |
18 | return sizeof(short int); | |
19 | } else if (encoding == ZIP_ENC_INT) { | |
20 | return sizeof(int); | |
21 | } else if (encoding == ZIP_ENC_LLONG) { | |
22 | return sizeof(long long); | |
23 | } | |
24 | assert(NULL); | |
25 | } | |
26 | ||
27 | /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is | |
28 | * provided, it is set to the number of bytes required to encode the length. */ | |
29 | static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) { | |
30 | unsigned char encoding = ZIP_ENCODING(p), lenenc; | |
31 | unsigned int len; | |
32 | ||
33 | if (encoding == ZIP_ENC_RAW) { | |
34 | lenenc = (p[0] >> 4) & 0x3; | |
35 | if (lenenc == ZIP_LEN_INLINE) { | |
36 | len = p[0] & 0xf; | |
37 | if (lensize) *lensize = 1; | |
38 | } else if (lenenc == ZIP_LEN_UINT16) { | |
39 | len = p[1] | (p[2] << 8); | |
40 | if (lensize) *lensize = 3; | |
41 | } else { | |
42 | len = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24); | |
43 | if (lensize) *lensize = 5; | |
44 | } | |
45 | } else { | |
46 | len = zipEncodingSize(encoding); | |
47 | if (lensize) *lensize = 1; | |
48 | } | |
3d04d29e PN |
49 | return len; |
50 | } | |
51 | ||
52 | /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns | |
53 | * the amount of bytes required to encode such a length. */ | |
29b14d5f PN |
54 | static unsigned int zipEncodeLength(unsigned char *p, char encoding, unsigned int rawlen) { |
55 | unsigned char len = 1, lenenc, buf[5]; | |
56 | if (encoding == ZIP_ENC_RAW) { | |
57 | if (rawlen <= 0xf) { | |
58 | if (!p) return len; | |
59 | lenenc = ZIP_LEN_INLINE; | |
60 | buf[0] = rawlen; | |
61 | } else if (rawlen <= 0xffff) { | |
62 | len += 2; | |
63 | if (!p) return len; | |
64 | lenenc = ZIP_LEN_UINT16; | |
65 | buf[1] = (rawlen ) & 0xff; | |
66 | buf[2] = (rawlen >> 8) & 0xff; | |
3d04d29e | 67 | } else { |
29b14d5f PN |
68 | len += 4; |
69 | if (!p) return len; | |
70 | lenenc = ZIP_LEN_UINT32; | |
71 | buf[1] = (rawlen ) & 0xff; | |
72 | buf[2] = (rawlen >> 8) & 0xff; | |
73 | buf[3] = (rawlen >> 16) & 0xff; | |
74 | buf[4] = (rawlen >> 24) & 0xff; | |
3d04d29e | 75 | } |
29b14d5f PN |
76 | buf[0] = (lenenc << 4) | (buf[0] & 0xf); |
77 | } | |
78 | if (!p) return len; | |
79 | ||
80 | /* Apparently we need to store the length in 'p' */ | |
81 | buf[0] = (encoding << 6) | (buf[0] & 0x3f); | |
82 | memcpy(p,buf,len); | |
83 | return len; | |
84 | } | |
85 | ||
86 | /* Check if string pointed to by 'entry' can be encoded as an integer. | |
87 | * Stores the integer value in 'v' and its encoding in 'encoding'. | |
88 | * Warning: this function requires a NULL-terminated string! */ | |
89 | static int zipTryEncoding(unsigned char *entry, long long *v, char *encoding) { | |
90 | long long value; | |
91 | char *eptr; | |
92 | ||
93 | if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) { | |
94 | value = strtoll(entry,&eptr,10); | |
95 | if (eptr[0] != '\0') return 0; | |
96 | if (value >= SHRT_MIN && value <= SHRT_MAX) { | |
97 | *encoding = ZIP_ENC_SHORT; | |
98 | } else if (value >= INT_MIN && value <= INT_MAX) { | |
99 | *encoding = ZIP_ENC_INT; | |
100 | } else { | |
101 | *encoding = ZIP_ENC_LLONG; | |
102 | } | |
103 | *v = value; | |
104 | return 1; | |
105 | } | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static void zipSaveInteger(unsigned char *p, long long value, char encoding) { | |
110 | short int s; | |
111 | int i; | |
112 | long long l; | |
113 | if (encoding == ZIP_ENC_SHORT) { | |
114 | s = value; | |
115 | memcpy(p,&s,sizeof(s)); | |
116 | } else if (encoding == ZIP_ENC_INT) { | |
117 | i = value; | |
118 | memcpy(p,&i,sizeof(i)); | |
119 | } else if (encoding == ZIP_ENC_LLONG) { | |
120 | l = value; | |
121 | memcpy(p,&l,sizeof(l)); | |
122 | } else { | |
123 | assert(NULL); | |
124 | } | |
125 | } | |
126 | ||
127 | static long long zipLoadInteger(unsigned char *p, char encoding) { | |
128 | short int s; | |
129 | int i; | |
130 | long long l, ret; | |
131 | if (encoding == ZIP_ENC_SHORT) { | |
132 | memcpy(&s,p,sizeof(s)); | |
133 | ret = s; | |
134 | } else if (encoding == ZIP_ENC_INT) { | |
135 | memcpy(&i,p,sizeof(i)); | |
136 | ret = i; | |
137 | } else if (encoding == ZIP_ENC_LLONG) { | |
138 | memcpy(&l,p,sizeof(l)); | |
139 | ret = l; | |
140 | } else { | |
141 | assert(NULL); | |
3d04d29e | 142 | } |
29b14d5f | 143 | return ret; |
3d04d29e PN |
144 | } |
145 | ||
146 | /* Return the total amount used by an entry (encoded length + payload). */ | |
147 | static unsigned int zipRawEntryLength(unsigned char *p) { | |
29b14d5f PN |
148 | unsigned int lensize, len; |
149 | len = zipDecodeLength(p, &lensize); | |
150 | return lensize + len; | |
3d04d29e PN |
151 | } |
152 | ||
153 | /* Resize the zip* structure. */ | |
154 | static unsigned char *zipResize(unsigned char *z, unsigned int len) { | |
155 | z = zrealloc(z,len); | |
156 | z[len-1] = ZIP_END; | |
157 | return z; | |
158 | } |