This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <stdint.h>
#define FLASH_EXE 0x5A4D
#define FLASH_VER 0xFF000000
#define FLASH_MSK 0x00FFFFFF
#define FLASH_FWS 0x00535746
#define FLASH_CWS 0x00535743
#define FLASH_ZWS 0x0053575A
#define FLASH_END 0xFA123456
#ifdef __HAVE_BUILTIN_SETJMP__
#include <setjmp.h>
#undef setjmp
#define setjmp __builtin_setjmp
#undef longjmp
#define longjmp __builtin_longjmp
#endif
/*
To compile this source code you'll need "puff.c" and "puff.h" files from zlib package.
Get this package from official site: http://zlib.net/
Required files can be found in /contrib/puff/ folder inside archive.
*/
#include "puff.h"
#include "puff.c"
#pragma pack(push, 1)
typedef struct {
uint32_t sign;
uint32_t size;
} flashead;
#pragma pack(pop)
char *basename(char *s) {
char *r;
if (s) {
for (r = s; *r; r++) {
if ((*r == '/') || (*r == '\\')) {
s = &r[1];
}
}
}
return(s);
}
void dumpdata(char *name, void *data, uint32_t size) {
FILE *f;
if (name) {
f = fopen(name, "wb");
if (f) {
if (data && size) {
fwrite(data, size, 1, f);
}
fclose(f);
}
}
}
void readdata(FILE *fl, void *data, uint32_t size) {
if (data && size) {
memset(data, 0, size);
if (fl) {
fread(data, size, 1, fl);
}
}
}
int main(int argc, char *argv[]) {
unsigned long ps, us;
uint32_t i, size;
flashead head;
uint8_t *p;
FILE *fl;
char m;
printf("Macromedia/Adobe Shockwave Flash extractor v1.1\n(c) CTPAX-X Team 2008,2023\nhttp://www.CTPAX-X.org/\n\n");
if (argc != 4) {
printf(
"Usage: flashext <#> <input.ext> <output.ext>\n\n"
"Where <#> mode:\n"
" u - unpack CWS flash movie to FWS\n"
" p - extract projector player from .EXE\n"
" f - extract flash movie from .EXE\n\n"
"Examples:\n"
" flashext u movie.swf unpacked.swf\n"
" flashext p movie.exe SAFlashPlayer.exe\n"
" flashext f movie.exe movie.swf\n\n"
);
return(1);
}
m = argv[1][1] ? 0 : argv[1][0];
m += ((m >= 'A') && (m <= 'Z')) ? ('a' - 'A') : 0;
do {
if (m == 'u') { m = 1; break; }
if (m == 'p') { m = 2; break; }
if (m == 'f') { m = 3; break; }
m = 0;
} while (0);
if (!m) {
printf("Error: unknown mode \"%s\".\n\n", argv[1]);
return(2);
}
m--;
fl = fopen(argv[2], "rb");
if (!fl) {
printf("Error: can't open input file.\n\n");
return(3);
}
/* get size */
fseek(fl, 0, SEEK_END);
size = ftell(fl);
if (size <= sizeof(head)) {
fclose(fl);
printf("Error: input file too small.\n\n");
return(4);
}
fseek(fl, 0, SEEK_SET);
if (!m) {
/* flash */
readdata(fl, &head, sizeof(head));
i = head.sign & FLASH_MSK;
if (((i != FLASH_FWS) && (i != FLASH_CWS) && (i != FLASH_ZWS)) || (!head.size)) {
fclose(fl);
printf("Error: invalid input file.\n\n");
return(5);
}
if (i == FLASH_FWS) {
fclose(fl);
printf("Error: input file already unpacked.\n\n");
return(6);
}
if (i == FLASH_ZWS) {
fclose(fl);
printf("Error: unpacking of LZMA compression not supported.\n\n");
return(7);
}
/* allocate memory: packed + unpacked */
p = (uint8_t *) malloc(size + head.size);
if (!p) {
fclose(fl);
printf("Error: not enough memory for unpacking.\n\n");
return(8);
}
printf("%s -> %s\n", basename(argv[2]), basename(argv[3]));
/* read file to memory */
readdata(fl, p, size - sizeof(head));
/* unpack zlib stream */
us = head.size;
ps = size - sizeof(head);
puff(&p[size], &us, &p[2], &ps);
/* update header */
head.sign = (head.sign & FLASH_VER) | FLASH_FWS;
memcpy(&p[size - sizeof(head)], &head, sizeof(head));
/* dump to disk */
dumpdata(argv[3], &p[size - sizeof(head)], head.size);
free(p);
} else {
/* player + flash */
memset(&head, 0, sizeof(head));
ps = 0;
i = 0;
readdata(fl, &i, 2);
if (i == FLASH_EXE) {
i = size - sizeof(head);
fseek(fl, i, SEEK_SET);
readdata(fl, &head, sizeof(head));
if (head.sign == FLASH_END) {
i -= head.size;
ps = head.size;
fseek(fl, i, SEEK_SET);
readdata(fl, &head, sizeof(head));
} else {
i = 0;
}
} else {
i = 0;
}
i = i ? (head.sign & FLASH_MSK) : 0;
if (((i != FLASH_FWS) && (i != FLASH_CWS) && (i != FLASH_ZWS)) || (!head.size)) {
fclose(fl);
printf("Error: input file not executable or doesn't have flash movie inside.\n\n");
return(9);
}
if (m == 1) {
/* player */
fseek(fl, 0, SEEK_SET);
size -= (sizeof(head) + ps);
} else {
/* flash */
fseek(fl, size - ps - sizeof(head), SEEK_SET);
size = ps;
}
p = (uint8_t *) malloc(size);
if (!p) {
fclose(fl);
printf("Error: not enough memory for extracting.\n\n");
return(10);
}
printf("%s -> %s\n", basename(argv[2]), basename(argv[3]));
/* read block */
readdata(fl, p, size);
/* dump to disk */
dumpdata(argv[3], p, size);
free(p);
}
fclose(fl);
printf("\ndone\n\n");
return(0);
}