~ubuntu-branches/ubuntu/precise/arj/precise-security

« back to all changes in this revision

Viewing changes to register.c

  • Committer: Bazaar Package Importer
  • Author(s): Guillem Jover
  • Date: 2004-06-27 08:07:09 UTC
  • Revision ID: james.westby@ubuntu.com-20040627080709-1gkxm72ex66gkwe4
Tags: upstream-3.10.21
ImportĀ upstreamĀ versionĀ 3.10.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: register.c,v 1.7 2004/04/21 07:04:10 andrew_belov Exp $
 
3
 * ---------------------------------------------------------------------------
 
4
 * This optional utility writes registration data to ARJ.EXE (or not .EXE).
 
5
 *
 
6
 */
 
7
 
 
8
#include "arj.h"
 
9
 
 
10
#include <stdlib.h>
 
11
 
 
12
/* Local definitions */
 
13
 
 
14
#define REG_BLOCK_SIZE          8192    /* Sequental processing block */
 
15
 
 
16
/* Global registration types */
 
17
 
 
18
#define REG_TYPE_DEFAULT           0    /* Registers a single user-specified
 
19
                                           file */
 
20
#define REG_TYPE_ARJ               1    /* Registers ARJ or ARJ/2 package */
 
21
#define REG_TYPE_ARJ32             2    /* Registers ARJ32 package */
 
22
 
 
23
DEBUGHDR(__FILE__)                      /* Debug information block */
 
24
 
 
25
/* Local variables */
 
26
 
 
27
static char reg_fname[]="register.dat"; /* User registration data */
 
28
static char end_marker[]="";
 
29
static char *reg_list[]={"arj" EXE_EXTENSION,
 
30
                         "arjs" EXE_EXTENSION,
 
31
                         "dearj" EXE_EXTENSION,
 
32
                         "rearj" EXE_EXTENSION,
 
33
                          end_marker};
 
34
static char *reg_list_32[]={"arj32" EXE_EXTENSION,
 
35
                            "arjs32" EXE_EXTENSION,
 
36
                            "rearj32" EXE_EXTENSION,
 
37
                            end_marker};
 
38
static char integrity_pattern[]={0xB1, 0x03, 0xB0, 0x02, 0xB0, 0x03, 0xB0,
 
39
                                 0x04, 0xB0, 0x05, 0};
 
40
static char reg_pattern[]="aRj sOfTwArE";
 
41
static char reg_pad[STD_REG_LEN];
 
42
static char proc_block[REG_BLOCK_SIZE];
 
43
static unsigned int file_ctr;
 
44
 
 
45
/* Writes four bytes to the file */
 
46
 
 
47
static int _fput_dword(unsigned long l, FILE *stream)
 
48
{
 
49
#ifdef WORDS_BIGENDIAN
 
50
       fputc(l&0xFF, stream);
 
51
       fputc(l>>8,   stream);
 
52
       fputc(l>>16,  stream);
 
53
return fputc(l>>24,  stream)==EOF;
 
54
#else
 
55
return fwrite(&l,4,1,stream)!=4;
 
56
#endif
 
57
}
 
58
 
 
59
static void _mput_dword(unsigned long d, char *p)
 
60
{
 
61
#ifdef WORDS_BIGENDIAN
 
62
#define _mput_byte(b,p) (*(p)=(b)&0xFF)
 
63
_mput_byte(d   ,  p  );
 
64
_mput_byte(d>>8,  p+1);
 
65
_mput_byte(d>>16, p+2);
 
66
_mput_byte(d>>24, p+3);
 
67
#undef _mput_byte
 
68
#else
 
69
*(unsigned long *)p=d;
 
70
#endif
 
71
}
 
72
 
 
73
/* Writes registration data to the given file */
 
74
 
 
75
static void write_reg_data(char *name)
 
76
{
 
77
 FILE *stream;
 
78
 int rp_len;
 
79
 long cur_pos, wr_pos;
 
80
 int bytes_read, byte_ctr;
 
81
 char *pb_ptr;
 
82
 long fsize;
 
83
 int c;
 
84
 
 
85
 if((stream=fopen(name, m_rbp))==NULL)
 
86
  error(M_CANTOPEN, name);
 
87
 rp_len=strlen(reg_pattern);
 
88
 cur_pos=0L;
 
89
 while(1)
 
90
 {
 
91
  fseek(stream, cur_pos, SEEK_SET);
 
92
  if((bytes_read=fread(proc_block, 1, REG_BLOCK_SIZE, stream))==0)
 
93
   error(M_PATCH_NOT_FOUND);
 
94
  bytes_read-=rp_len;
 
95
  pb_ptr=proc_block;
 
96
  byte_ctr=0;
 
97
  while(byte_ctr<bytes_read)
 
98
  {
 
99
   if(!memcmp(pb_ptr, reg_pattern, rp_len))
 
100
    break;
 
101
   byte_ctr++;
 
102
   pb_ptr++;
 
103
  }
 
104
  if(byte_ctr<bytes_read)
 
105
   break;
 
106
  cur_pos+=(long)REG_BLOCK_SIZE/2;      /* Dirty hack */
 
107
 }
 
108
 wr_pos=cur_pos+(long)byte_ctr+(long)REG_HDR_SHIFT;
 
109
 rp_len=STD_REG_LEN;
 
110
 if(fseek(stream, wr_pos, SEEK_SET))
 
111
  error(M_CANT_SEEK, name);
 
112
 if(fwrite(reg_pad, 1, rp_len, stream)!=rp_len)
 
113
  error(M_CANT_WRITE, name);
 
114
 fseek(stream, 0L, SEEK_END);
 
115
 fsize=ftell(stream);
 
116
 fseek(stream, 0L, SEEK_SET);
 
117
 rp_len=strlen(integrity_pattern);
 
118
 cur_pos=0L;
 
119
 while(1)
 
120
 {
 
121
  fseek(stream, cur_pos, SEEK_SET);
 
122
  if((bytes_read=fread(proc_block, 1, REG_BLOCK_SIZE, stream))==0)
 
123
   error(M_PATCH_NOT_FOUND);
 
124
  bytes_read-=rp_len;
 
125
  pb_ptr=proc_block;
 
126
  byte_ctr=0;
 
127
  while(byte_ctr<bytes_read)
 
128
  {
 
129
   if(!memcmp(pb_ptr, integrity_pattern, rp_len))
 
130
    break;
 
131
   byte_ctr++;
 
132
   pb_ptr++;
 
133
  }
 
134
  if(byte_ctr<bytes_read)
 
135
   break;
 
136
  cur_pos+=(long)REG_BLOCK_SIZE/2;      /* Dirty hack II */
 
137
 }
 
138
 wr_pos=(long)byte_ctr+cur_pos+rp_len;
 
139
 crc32term=CRC_MASK;
 
140
 fseek(stream, 0L, SEEK_SET);
 
141
 for(cur_pos=0L; cur_pos<wr_pos; cur_pos++)
 
142
 {
 
143
  if((c=fgetc(stream))==-1)
 
144
   error(M_CANTREAD, name);
 
145
  crc32term=crc32_for_char(crc32term, (unsigned char)c);
 
146
 }
 
147
 cur_pos+=8L;
 
148
 fseek(stream, cur_pos, SEEK_SET);
 
149
 while(cur_pos<fsize)
 
150
 {
 
151
  if((c=fgetc(stream))==-1)
 
152
   error(M_CANTREAD, name);
 
153
  crc32term=crc32_for_char(crc32term, (unsigned char)c);
 
154
  cur_pos++;
 
155
 }
 
156
 fsize+=2L;
 
157
 if(fseek(stream, wr_pos, SEEK_SET))
 
158
  error(M_CANT_SEEK, name);
 
159
 _fput_dword(crc32term, stream);
 
160
 _fput_dword(fsize, stream);
 
161
 fclose(stream);
 
162
 printf(M_REGISTER_STAMPED, name);
 
163
 printf(M_VERIFY_REGISTRATION, name);
 
164
 printf("\n");
 
165
 file_ctr++;
 
166
}
 
167
 
 
168
/* The registration procedure itself */
 
169
 
 
170
static void register_proc(char *block)
 
171
{
 
172
 char *nptr;
 
173
 int i;
 
174
 
 
175
 strip_lf(block);
 
176
 nptr=block;
 
177
 for(i=0; i<8; i++)
 
178
 {
 
179
  nptr=ltrim(nptr);
 
180
  _mput_dword(strtoul(nptr, &nptr, 10), &reg_pad[i<<2]);
 
181
 }
 
182
 nptr=ltrim(nptr);
 
183
 for(i=0; *nptr!=' '&&*nptr!='\0'&&i<REG_KEY1_LEN; i++)
 
184
  reg_pad[REG_KEY1_SHIFT-REG_HDR_SHIFT+i]=*nptr++;
 
185
 reg_pad[REG_KEY1_SHIFT-REG_HDR_SHIFT+i]='\0';
 
186
 nptr=ltrim(nptr);
 
187
 for(i=0; *nptr!=' '&&*nptr!='\0'&&i<REG_KEY2_LEN; i++)
 
188
  reg_pad[REG_KEY2_SHIFT-REG_HDR_SHIFT+i]=*nptr++;
 
189
 reg_pad[REG_KEY2_SHIFT-REG_HDR_SHIFT+i]='\0';
 
190
 nptr=ltrim(nptr);
 
191
 for(i=0; *nptr!='\0'&&i<REG_NAME_LEN; i++)
 
192
  reg_pad[REG_NAME_SHIFT-REG_HDR_SHIFT+i]=*nptr++;
 
193
 reg_pad[REG_NAME_SHIFT-REG_HDR_SHIFT+i]='\0';
 
194
 alltrim(reg_pad+REG_KEY1_SHIFT-REG_HDR_SHIFT);
 
195
 if(reg_pad[REG_KEY1_SHIFT-REG_HDR_SHIFT]=='\0')
 
196
  error(M_IMPROPER_REG_FMT);
 
197
}
 
198
 
 
199
/* Main routine */
 
200
 
 
201
int main(int argc, char **argv)
 
202
{
 
203
 int global_reg;
 
204
 FILE *stream;
 
205
 char reg_source[200];
 
206
 int i;
 
207
 
 
208
 printf(M_REGISTER_BANNER);
 
209
 integrity_pattern[0]--;
 
210
 build_crc32_table();
 
211
 if(argc!=2)
 
212
  error(M_REGISTER_HELP);
 
213
 global_reg=REG_TYPE_DEFAULT;
 
214
 file_ctr=0;
 
215
 if(!stricmp(argv[1], "-arj"))
 
216
  global_reg=REG_TYPE_ARJ;
 
217
 else if(!stricmp(argv[1], "-arj32"))
 
218
  global_reg=REG_TYPE_ARJ32;
 
219
 if((stream=fopen(reg_fname, m_r))==NULL)
 
220
  error(M_MISSING_REG_FILE);
 
221
 if(fgets(reg_source, sizeof(reg_source), stream)==NULL)
 
222
  error(M_CANTREAD, reg_fname);
 
223
 fclose(stream);
 
224
 register_proc(reg_source);
 
225
 if(global_reg==REG_TYPE_DEFAULT)
 
226
  write_reg_data(argv[1]);
 
227
 else if(global_reg==REG_TYPE_ARJ)
 
228
 {
 
229
  for(i=0; reg_list[i][0]!='\0'; i++)
 
230
  {
 
231
   if(file_exists(reg_list[i]))
 
232
    write_reg_data(reg_list[i]);
 
233
  }
 
234
 }
 
235
 else if(global_reg==REG_TYPE_ARJ32)
 
236
 {
 
237
  for(i=0; reg_list_32[i][0]!='\0'; i++)
 
238
  {
 
239
   if(file_exists(reg_list_32[i]))
 
240
    write_reg_data(reg_list_32[i]);
 
241
  }
 
242
 }
 
243
 if(file_ctr==0)
 
244
  error(M_REG_FAILED);
 
245
 if(file_ctr>1)
 
246
  printf(M_REG_TOTALS, file_ctr);
 
247
 return(REGISTER_ERL_SUCCESS);
 
248
}