File size: 5,331 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

extern U8 *FAT32FileRead(CDrv *dv,U8 *cur_dir,U8 *filename,
        I64 *_size,I64 *_attr);
extern I64 FAT32FileWrite(CDrv *dv,U8 *cur_dir,U8 *name,U8 *buf,I64 size,
        CDate cdt,I64 attr);

#define CT_NONE         0
#define CT_7_BIT        1
#define CT_8_BIT        2

public class CArcCompress
{
  I64   compressed_size,expanded_size;
  U8    compression_type,pad[3];
  U0    body;
};

U8 *ExpandBuf(CArcCompress *arc,CTask *mem_task=NULL)
{//See ::/Demo/Dsk/SerializeTree.HC.
  CArcCtrl *c;
  U8 *res=MAlloc(arc->expanded_size+1,mem_task);
  res[arc->expanded_size]=0; //terminate
  if (arc->compression_type==CT_NONE)
    MemCpy(res,&arc->body,arc->expanded_size);
  else {
    c=ArcCtrlNew(TRUE,arc->compression_type+1);
    c->src_size=arc->compressed_size<<3;
    c->src_pos=sizeof(CArcCompress)<<3;
    c->src_buf=arc;
    c->dst_size=arc->expanded_size;
    c->dst_buf=res;
    c->dst_pos=0;
    ArcExpandBuf(c);
    ArcCtrlDel(c);
  }
  return res;
}

U8 *FileRead(U8 *filename,I64 *_size=NULL,I64 *_attr=NULL)
{//Read whole file from disk.
  U8 *absname,*curname,*res=NULL;
  I64 size=0,attr=0;
  CDirContext *dirc;
  CArcCompress *arc;
  absname=FileNameAbs(filename);
  curname=absname;
  if (dirc=DirContextNew(curname)) {
    switch (dirc->dv->fs_type) {
      case FSt_FAT32:
        res=FAT32FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
        break;
      default:
        PrintErr("File System Not Supported\n");
    }
    DirContextDel(dirc);
  }
  if (!res)
    PrintErr("File not found: \"%s\".\n",filename);
  if (res && attr & RS_ATTR_COMPRESSED) {
    arc=res;
    size=arc->expanded_size;
    res=ExpandBuf(arc);
    Free(arc);
  }
  if (_attr) *_attr=attr;
  if (_size) *_size=size;
  Free(absname);
  return res;
}

#define CT_NONE         1
#define CT_7_BIT        2
#define CT_8_BIT        3

public class CArcCompress
{
  I64   compressed_size,expanded_size;
  U8    compression_type;
  U0    body;
};

I64 ArcDetermineCompressionType(U8 *src,I64 size)
{
  while (size--)
    if (*src++&0x80)
      return CT_8_BIT;
  return CT_7_BIT;
}

CArcCompress *CompressBuf(U8 *src,I64 size,CTask *mem_task=NULL)
{//See ::/Demo/Dsk/SerializeTree.HC.
  CArcCompress *arc;
  I64 size_out,compression_type=ArcDetermineCompressionType(src,size);
  CArcCtrl *c=ArcCtrlNew(FALSE,compression_type);
  c->src_size=size;
  c->src_buf=src;
  c->dst_size=(size+sizeof(CArcCompress))<<3;
  c->dst_buf=CAlloc(c->dst_size>>3);
  c->dst_pos=sizeof(CArcCompress)<<3;
  ArcCompressBuf(c);
  if (ArcFinishCompression(c) && c->src_pos==c->src_size) {
    size_out=(c->dst_pos+7)>>3;
    arc=MAlloc(size_out,mem_task);
    MemCpy(arc,c->dst_buf,size_out);
    arc->compression_type=compression_type;
    arc->compressed_size=size_out;
  } else {
    arc=MAlloc(size+sizeof(CArcCompress),mem_task);
    MemCpy(&arc->body,src,size);
    arc->compression_type=CT_NONE;
    arc->compressed_size=size+sizeof(CArcCompress);
  }
  arc->expanded_size=size;
  Free(c->dst_buf);
  ArcCtrlDel(c);
  return arc;
}

I64 FileWrite(U8 *filename,U8 *fbuf,
   I64 size,CDate cdt=0,I64 attr=0)
{//Write whole file to disk.
  I64 c=0;
  CHashGeneric *tmph;
  CDirContext *dirc;
  U8 *fbuf2,*absname=FileNameAbs(filename);
  if (dirc=DirContextNew(filename,FALSE,TRUE)) {
    attr=FileAttr(dirc->mask,attr);
    if (attr&RS_ATTR_COMPRESSED) {
      fbuf=CompressBuf(fbuf,size);
      size=fbuf(CArcCompress *)->compressed_size;
      fbuf2=fbuf;
    } else
      fbuf2=NULL;
    if (!cdt) cdt=Now;
    switch (dirc->dv->fs_type) {
      case FSt_FAT32:
        c=FAT32FileWrite(dirc->dv,Fs->cur_dir,dirc->mask,
              fbuf,size,cdt,attr);
        break;
      default:
        PrintErr("File System Not Supported\n");
    }
    if (tmph=HashFind(absname,adam_task->hash_table,HTT_FILE)) {
      if (attr & RS_ATTR_RESIDENT) {
        Free(tmph->user_data0);
        tmph->user_data0=AMAllocIdent(fbuf);
        tmph->user_data1=size;
      } else
        HashRemDel(tmph,adam_task->hash_table);
    } else if (attr & RS_ATTR_RESIDENT)
      HashGenericAdd(absname,HTT_FILE,AMAllocIdent(fbuf),size,0,adam_task);
    Free(fbuf2);
    DirContextDel(dirc);
  }
  Free(absname);
  return c;
}

Bool CopySingleZ(U8 *f1,U8 *f2) //Just one file
{
  U8 *file_buf=NULL;
  I64 size,attr=0,c;
  CDirEntry de;
  if (FileFind(f1,&de,FUF_JUST_FILES)) {
    Free(de.full_name);
    file_buf=FileRead(f1,&size,&attr);
    attr=FileAttr(f2,attr);
    if (file_buf) {
      "Copying %s to %s\n",f1,f2;
      c=FileWrite(f2,file_buf,size,de.datetime,attr);
      Free(file_buf);
      return ToBool(c);
    } else
      PrintErr("File not found: \"%s\".\n",f1);
  } else
    PrintErr("File not found: \"%s\".\n",f1);
  return FALSE;
}

public I64 Rezip(U8 *files_find_mask="*.Z",U8 *fu_flags=NULL)
{
  U8 *st;
  CDirEntry *tmpde,*tmpde1;
  I64 res=0,fuf_flags=0;
  ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),"+r+f+F+O");
  ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);
  tmpde=tmpde1=FilesFind(files_find_mask,fuf_flags);
  while (tmpde) {
    if (IsDotZ(tmpde->full_name)) {
      st=StrNew(tmpde->full_name);
      StrLastRem(st,".");
      res+=CopySingleZ(tmpde->full_name,st);
      CopySingleZ(st,tmpde->full_name);
      Del(st);
      Free(st);
    }
    tmpde=tmpde->next;
  }
  DirTreeDel(tmpde1);
  return res;
}