Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/woip/c/bzipreader.h
blob: 0477b680cd489ea74703d60fc213459cd3a41952 (plain)
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
/*
   Do stuff to bzip2 files. Originally based on bzip2recover, which
   is part of the standard bzip2 distribution.
   Author: Patrick Collison <patrick@collison.ie>
   Original author: Julian Seward <jseward@bzip.org>
*/

/* Caveat: I've a feeling this won't work unmodified on big-endian architectures */

#ifndef __BZIPREADER_H__
#define __BZIPREADER_H__

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <bzlib.h>
#include "debug.h"
#include "safe.h"

#define bytes(x) ((x) >> 3)

#define BLOCK_HEADER_HI  0x00003141UL
#define BLOCK_HEADER_LO  0x59265359UL

#define BLOCK_ENDMARK_HI 0x00001772UL
#define BLOCK_ENDMARK_LO 0x45385090UL

#define BIT_EOF 2

/* Increase if necessary.  However, a .bz2 file with > 50000 blocks
   would have an uncompressed size of at least 40GB, so the chances
   are low you'll need to up this. */
#define BZ_MAX_HANDLED_BLOCKS 500000

/* 900kb */
/* FIXME */
#define BZ_MAX_BLOCK (1100 * 1024)

#define BZ_MAX_FILENAME 2000

/* Header bytes */

#define BZ_HDR_B 0x42                         /* 'B' */
#define BZ_HDR_Z 0x5a                         /* 'Z' */
#define BZ_HDR_h 0x68                         /* 'h' */
#define BZ_HDR_0 0x30                         /* '0' */

typedef struct {
  FILE *handle;
  int32_t  buffer;
  int32_t  buffLive;
  char   mode;
} BitStream;

typedef struct {
  char  *buff;
  int32_t  buffer;
  int32_t  buffLive;
  uint64_t pos;
  int32_t crc; /* logically, this doesn't belong here, but RubyInline... */
} BitBuffer;

FILE *xfopen(const char *path, const char *mode);
void xfclose(FILE *fp);

BitBuffer *bbOfSize(uint64_t size);
void bbClose(BitBuffer *bb);

int computeBoundaries(FILE *inFile);
int decompressBlock(char *src, uint32_t srcLen, char *dest, uint32_t *destLen);

uint64_t readBlock(FILE *inFile, uint64_t bitOffset, BitBuffer *bbOut);
uint64_t fixedOffset(uint64_t offset);

#endif