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
|
/* $Id: separate.c,v 1.3 2003/08/22 15:27:04 twogood Exp $ */
#define _BSD_SOURCE 1
#include "liborange_internal.h"
#include <synce_log.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
static uint8_t* orange_memstr(const void *haystack, const char *needle, size_t size)/*{{{*/
{
const void *next = haystack;
size_t input_size = size;
size_t needle_size = strlen(needle);
for (;;)
{
const void *p = memchr(next, needle[0], input_size);
if (!p)
break;
input_size -= (p - next);
if (input_size < needle_size)
break;
if (0 == memcmp(p, needle, needle_size))
return (uint8_t*)p;
next = p + 1;
input_size--;
}
return NULL;
}/*}}}*/
#define MSCF_SIGNATURE "MSCF"
#define MSCE_SIGNATURE "MSCE"
#define MSCF_SIZE 0x08
#define MSCF_MSCE_HEADER 0x24
#define MSCE_HEADER_TO_DATA 0x08
#define MSCE_SIZE 0x08
#define MSCE_PROCESSOR 0x14
bool orange_get_installable_cab_info2(/*{{{*/
uint8_t* input_buffer,
size_t input_size,
CabInfo* cab_info)
{
bool success = false;
uint8_t *msce;
size_t msce_offset;
if (!cab_info)
{
synce_error("CabInfo parameter is NULL");
goto exit;
}
memset(cab_info, 0, sizeof(CabInfo));
if (input_size < (MSCF_MSCE_HEADER + sizeof(uint32_t)))
{
synce_error("Input buffer is too small");
goto exit;
}
if (0 != strncmp((char*)input_buffer, MSCF_SIGNATURE, strlen(MSCF_SIGNATURE)))
{
synce_error("Not a Microsoft Cabinet file");
goto exit;
}
cab_info->size = letoh32(*(uint32_t*)(input_buffer + MSCF_SIZE));
msce_offset = letoh32(*(uint32_t*)(input_buffer + MSCF_MSCE_HEADER)) + MSCE_HEADER_TO_DATA;
if (input_size < (msce_offset + MSCE_PROCESSOR + sizeof(uint32_t)))
{
synce_trace("Invalid or not installable");
goto exit;
}
msce = input_buffer + msce_offset;
if (0 != strncmp((char*)msce, MSCE_SIGNATURE, strlen(MSCE_SIGNATURE)))
{
synce_trace("Not installable");
goto exit;
}
cab_info->processor = letoh32(*(uint32_t*)(msce + MSCE_PROCESSOR));
success = true;
exit:
return success;
}/*}}}*/
bool orange_get_installable_cab_info(/*{{{*/
const char* input_filename,
CabInfo* cab_info)
{
bool success = false;
FILE* input = fopen(input_filename, "r");
size_t input_size;
uint8_t* input_buffer = NULL;
if (!input)
{
synce_error("Failed to open file for reading: '%s'", input_filename);
goto exit;
}
/* don't need the whole file */
input_size = MIN(FSIZE(input), 0x8000);
input_buffer = (uint8_t*)malloc(input_size);
if (!input_buffer)
{
synce_error("Failed to allocate %i bytes", input_size);
goto exit;
}
if (input_size != fread(input_buffer, 1, input_size, input))
{
synce_error("Failed to read %i bytes from file '%s'", input_size, input_filename);
goto exit;
}
success = orange_get_installable_cab_info2(input_buffer, input_size, cab_info);
exit:
FCLOSE(input);
FREE(input_buffer);
return success;
}/*}}}*/
typedef struct
{
const char* output_directory;
char* basename;
} SeparationCookie;
static bool orange_separate_callback(/*{{{*/
const uint8_t* buffer,
size_t size,
CabInfo* info,
void* cookie)
{
bool success = false;
SeparationCookie* sc = (SeparationCookie*)cookie;
char cabfile[256];
const char* processor_name = NULL;
switch (info->processor)
{
case 0:
processor_name = "UnspecifiedProcessor";
break;
case 2577:
processor_name = "StrongARM";
break;
case 10003:
processor_name = "HitachiSH3";
break;
case 4000:
processor_name = "MipsR4000";
break;
}
if (processor_name)
snprintf(cabfile, sizeof(cabfile), "%s.%s.cab", sc->basename, processor_name);
else
snprintf(cabfile, sizeof(cabfile), "%s.%i.cab", sc->basename, info->processor);
if (!orange_write(buffer, size, sc->output_directory, cabfile))
{
synce_error("Failed to write Microsoft Cabinet File to directory '%s'", sc->output_directory);
goto exit;
}
success = true;
exit:
return success;
}/*}}}*/
bool orange_separate2(/*{{{*/
uint8_t* input_buffer,
size_t input_size,
orange_buffer_callback callback,
void* cookie)
{
bool success = false;
uint8_t* last = NULL;
uint8_t* mscf = NULL;
int cab_count = 0;
last = input_buffer;
while ( (mscf = orange_memstr(last, MSCF_SIGNATURE, input_size)) )
{
CabInfo cab_info;
input_size -= (mscf - last);
if (orange_get_installable_cab_info2(mscf, input_size, &cab_info))
{
cab_count++;
if (!callback(mscf, cab_info.size, &cab_info, cookie))
goto exit;
input_size -= cab_info.size;
last = mscf + cab_info.size;
}
else
{
last = mscf + 1;
input_size--;
}
}
success = cab_count > 0;
exit:
return success;
}/*}}}*/
bool orange_separate(/*{{{*/
const char* input_filename,
const char* output_directory)
{
bool success = false;
FILE* input = fopen(input_filename, "r");
size_t input_size;
uint8_t* input_buffer = NULL;
char* p = NULL;
SeparationCookie cookie;
if (!input)
{
synce_error("Failed to open file for reading: '%s'", input_filename);
goto exit;
}
input_size = FSIZE(input);
input_buffer = (uint8_t*)malloc(input_size);
if (!input_buffer)
{
synce_error("Failed to allocate %i bytes", input_size);
goto exit;
}
if (input_size != fread(input_buffer, 1, input_size, input))
{
synce_error("Failed to read %i bytes from file '%s'", input_size, input_filename);
goto exit;
}
/* create cookie */
cookie.output_directory = output_directory;
p = strrchr(input_filename, '/');
if (p)
cookie.basename = strdup(p+1);
else
cookie.basename = strdup(input_filename);
p = strrchr(cookie.basename, '.');
if (p)
*p = '\0';
success = orange_separate2(
input_buffer,
input_size,
orange_separate_callback,
(void*)&cookie);
FREE(cookie.basename);
exit:
FCLOSE(input);
FREE(input_buffer);
return success;
}/*}}}*/
|