archivelib-sys 0.2.0

An implementaton(in C++) of the Greenleaf ArchiveLib compression/decompression algorithm
Documentation
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Copyright 1990-2008 Light Infocon Tecnologia S/A

Este arquivo é parte do programa LightBase - Banco de Dados Textual Documental

O LightBase é um software livre; você pode redistribui-lo e/ou modifica-lo dentro
dos termos da Licença Pública Geral GNU como publicada pela Fundação do Software
Livre (FSF); na versão 2 da Licença.

Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou APLICAÇÃO
EM PARTICULAR. Veja a Licença Pública Geral GNU para maiores detalhes.

Você deve ter recebido uma cópia da Licença Pública Geral GNU versao 2, sob o
título "LICENCA.txt", junto com este programa, se não, escreva para a Fundação do
Software Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include "arclib.h"
#pragma hdrstop

#include <time.h>
#include "timedate.h"

//
// void * ALTimeDate::operator new( size_t size )
//
// ARGUMENTS:
//
//  size  :  The number of bytes needed to create a new ALTimeDate object.
//
// RETURNS
//
//  A pointer to the newly allocated storage area, or 0 if no storage
//  was available.
//
// DESCRIPTION
//
//  When using a DLL, it is easy to get into a dangerous situation when
//  creating objects whose ctor and dtor are both in the DLL.  The problem
//  arises because when you create an object using new, the memory for
//  the object will be allocated from the EXE.  However, when you destroy
//  the object using delete, the memory is freed inside the DLL.  Since
//  the DLL doesn't really own that memory, bad things can happen.
//
//  But, you say, won't the space just go back to the Windows heap regardless
//  of who tries to free it?  Maybe, but maybe not.  If the DLL is using
//  a subsegment allocation scheme, it might do some sort of local free
//  before returning the space to the windows heap.  That is the point where
//  you could conceivably cook your heap.
//
//  By providing our own version of operator new inside this class, we
//  ensure that all memory allocation for the class will be done from
//  inside the DLL, not the EXE calling the DLL.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

#if defined( AL_BUILDING_DLL )
void AL_DLL_FAR * AL_PROTO ALTimeDate::operator new( size_t size )
{
    return ::new char[ size ];
}
#endif

//
// ALTimeDate::ALTimeDate()
//
// ARGUMENTS:
//
//  None.
//
// RETURNS
//
//  None.
//
// DESCRIPTION
//
//  All the constructor does is initialize the data members.  By
//  setting the year to an invalid value of 0, we can always see
//  that the time date stamp for a file hasn't been initialized.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

AL_PROTO ALTimeDate::ALTimeDate()
{
    miYear = 0;  //This is an illegal year, means it is uninitialized
    miMonth = 0;
    miDate = 0;
    miHour = 0;
    miMinute = 0;
    miSecond = 0;
}

//
// ALTimeDate::~ALTimeDate()
//
// ARGUMENTS:
//
//  None.
//
// RETURNS
//
//  None.
//
// DESCRIPTION
//
//  The destructor has nothing to do.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

AL_PROTO ALTimeDate::~ALTimeDate()
{
}

//
// long ALTimeDate::ToJulian()
//
// ARGUMENTS:
//
//  None.
//
// RETURNS
//
//  A Julian day.
//
// DESCRIPTION
//
//   This function is used to make a Julian day number from a normal
//   month/day/year thing.  We need a Julian day in order to make a
//   UNIX style time stamp.  The UNIX time stamp is used to store
//   time stamps in Archive directories.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

long AL_PROTO ALTimeDate::ToJulian()
{
    return (long)( miDate - 32076)
          + 1461L * ( miYear + 4800L + ( miMonth - 14) / 12) / 4
          + 367 * ( miMonth - 2 - ( miMonth - 14) / 12 * 12) / 12
          - 3 * (( miYear + 4900L + ( miMonth - 14) / 12) / 100) / 4
          + 1;
}


//
// void ALTimeDate::FromJulian( long jdn )
//
// ARGUMENTS:
//
//  jdn  :  A julian date number, ideally one produced by ToJulian().
//
// RETURNS
//
//  Nothing.
//
// DESCRIPTION
//
//   This function is used to convert a julian date to a normal
//   year/month/day.  Time/date stamps are stored in Archives in
//   UNIX format.  This function is needed to convert a UNIX
//   time stamp to a normal mm/dd/yy.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

void AL_PROTO ALTimeDate::FromJulian( long jdn )
{
        long x;
        long z;
        long m;
        long d;
        long y;
        const long daysPer400Years = 146097L;
        const long fudgedDaysPer4000Years = 1460970L + 31;

        x = jdn + 68569L;
        z = 4 * x / daysPer400Years;
        x = x - (daysPer400Years * z + 3) / 4;
        y = 4000 * (x + 1) / fudgedDaysPer4000Years;
        x = x - 1461 * y / 4 + 31;
        m = 80 * x / 2447;
        d = x - 2447 * m / 80;
        x = m / 11;
        m = m + 2 - 12 * x;
        y = 100 * (z - 49) + y + x;
//
// I don't know whether or not we could eliminate these temporary longs
//
        miYear = (short int) y;
        miMonth = (short int) m;
        miDate = (short int) d;
}


//
// long ALTimeDate::GetUnixTime()
//
// ARGUMENTS:
//
//  None.
//
// RETURNS
//
//  A UNIX time, converted from the internal m/d/y h:m:s data.
//
// DESCRIPTION
//
//  This function is used to convert the m/d/y h:m:s time stamp for a file
//  into a UNIX time stamp.  The UNIX time stamp is a 32 bit long that
//  is used to store time stamps in an Archive.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

long AL_PROTO ALTimeDate::GetUnixTime()
{
    const long UnixFirstDay = 2440588L;
    long result;

    result = ToJulian();
    result -= UnixFirstDay;
    if ( result >= 0L ) {
        result *= 3600L * 24;
        result += 3600L * miHour;
        result += 60L * miMinute;
        result += miSecond;
    } else
        result = 0L;
    return result;
}

//
// void ALTimeDate::SetTimeDate( long unix_time )
//
// ARGUMENTS:
//
//  unix_time  :  A long integer in UNIX timestamp format.
//
// RETURNS
//
//  Nothing.
//
// DESCRIPTION
//
//  This function is called when we are reading a directory in from
//  an archive.  It is used to set the internal data members of an
//  ALTimeDate object, after converting from unix time.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

void AL_PROTO ALTimeDate::SetTimeDate( long unix_time )
{
    const long UnixFirstDay = 2440588L;

    long jd = unix_time / ( 3600L * 24 );
    long hms = unix_time % ( 3600L * 24 );
    FromJulian( jd + UnixFirstDay );
    miHour = (short int) ( hms / 3600 );
    hms -= 3600L * miHour;
    miMinute = (short int) ( hms / 60 );
    miSecond = (short int) ( hms - ( miMinute * 60 ) );
}

//
// void ALTimeDate::SetTimeDate( struct tm *tblock )
//
// ARGUMENTS:
//
//  tblock :  A time date stamp as used by the C run time library.
//
// RETURNS
//
//  Nothing.
//
// DESCRIPTION
//
//  When working with DOS files, time stamps are read in to a structure
//  in the struct tm format.  This function provides an easy way to convert
//  the structure into our internal format.  When a DOS file is opened
//  using Open(), this function is called.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

void AL_PROTO ALTimeDate::SetTimeDate( struct tm AL_DLL_FAR *tblock )
{
    AL_ASSERT( tblock != 0, "SetTimeDate: passing illegal null parameter" );
    miYear = (short int) ( tblock->tm_year + 1900 );
    miMonth = (short int) ( tblock->tm_mon + 1 );
    miDate = (short int) tblock->tm_mday;
    miHour = (short int) tblock->tm_hour;
    miMinute = (short int) tblock->tm_min;
    miSecond = (short int) tblock->tm_sec;
}

//
// void ALTimeDate::GetTimeDate( struct tm *tblock )
//
// ARGUMENTS:
//
//  tblock  : A structure in the format used by the C runtime library for
//            storing time and date stamps.
//
// RETURNS
//
//  Nothing.
//
// DESCRIPTION
//
//  This function provides the reverse of SetTimeDate().  You would think
//  that we could just set the appropriate members of struct tm, but
//  there is a problem with that.  struct tm has one element that is
//  supposed to be the day of the week, and another that is supposed
//  to be the number of the day within the year.  We could try to
//  figure those out using the julian day function, but since gmtime()
//  will figure them out for us, we'll use that instead.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//

void AL_PROTO ALTimeDate::GetTimeDate( struct tm AL_DLL_FAR *tblock )
{
    AL_ASSERT( tblock != 0, "GetTimeDate: passing illegal null parameter" );
    long unix_time = GetUnixTime();
    struct tm *result = gmtime( (const time_t *) &unix_time );
    if ( result ) {
        *tblock = *result;
        tblock->tm_isdst = 0;
    } else {    //This should never happen!
        tblock->tm_year = miYear - 1900;
        tblock->tm_mon = miMonth - 1;
        tblock->tm_mday = miDate;
        tblock->tm_hour = miHour;
        tblock->tm_min = miMinute;
        tblock->tm_sec = miSecond;
        tblock->tm_wday = 0;
        tblock->tm_yday = 0;
        tblock->tm_isdst = 0;
    }
}

//
// unsigned short int ALTimeDate::GetDosTime()
//
// ARGUMENTS:
//
//  None.
//
// RETURNS
//
//  This function returns the time stored in this.  The bits of the
//  time are packed into the form that is needed by the _dos_setftime()
//  format.
//
// DESCRIPTION
//
//  When we close a file that needs to have its time and date stamp
//  set, we normally use the _dos_setftime() function to do the
//  work.  It expects to see the time packed into a particular
//  sequence of bits in an unsigned short.  That is what this
//  function does.  It packs the bits just the way you want them.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A   : First release
//
//   August 10, 1994 1.0B : Had to make sure this got turned off when
//                          running under UNIX.  It wouldn't actually
//                          hurt anything, but it isn't particularly
//                          useful either.

#if !defined( AL_WIN32S ) && !defined( AL_UNIX )

unsigned short int AL_PROTO ALTimeDate::GetDosTime()
{
    int result;
    result = miSecond / 2;
    result |= miMinute << 5;
    result |= miHour << 11;
    return (unsigned short int) result;
}

#endif //#if !defined( AL_WIN32S ) ....


//
// unsigned short int ALTimeDate::GetDosDate()
//
// ARGUMENTS:
//
//  None.
//
// RETURNS
//
//  This function returns the date stored in this object.  The bits of the
//  date are packed into the form that is needed by the _dos_setftime()
//  format.
//
// DESCRIPTION
//
//  When we close a file that needs to have its time and date stamp
//  set, we normally use the _dos_setftime() function to do the
//  work.  It expects to see the date packed into a particular
//  sequence of bits in an unsigned short.  That is what this
//  function does.  It packs the bits just the way you want them.
//
// REVISION HISTORY
//
//   May 26, 1994  1.0A  : First release
//
//   August 10, 1994 1.0B : Had to make sure this got turned off when
//                          running under UNIX.  It wouldn't actually
//                          hurt anything, but it isn't particularly
//                          useful either.

#if !defined( AL_WIN32S ) && !defined( AL_UNIX )

unsigned short int AL_PROTO ALTimeDate::GetDosDate()
{
    int result;
    result = miDate;
    result |= miMonth << 5;
    result |= (miYear-1980) << 9;
    return (unsigned short int ) result;
}
#endif //#if !defined( AL_WIN32S )