diff --git a/extract_screenshot.c b/extract_screenshot.c new file mode 100644 index 0000000..7486701 --- /dev/null +++ b/extract_screenshot.c @@ -0,0 +1,67 @@ +//read vbox screenshoot + +#include +#include + +uint32_t readf(FILE *f) +{ + uint32_t r; + fread(&r, 4, 1, f); + return r; +} + +char imgdata[1024*1024]; + +void writeimg(int width, int height, char *data) +{ + + FILE *out = fopen("out.ppm", "w"); + fprintf(out, "P3\n%d %d\n255\n", width, height); + int r,c,g; + unsigned char *u = data; + for (r = 0; r < height; r++) { + for (c=0; c + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifndef LZFP_h +#define LZFP_h + +#define STANDALONE 1 /* at the moment, this is ok. */ + +#ifndef STANDALONE +# include "lzf.h" +#endif + +/* + * Size of hashtable is (1 << HLOG) * sizeof (char *) + * decompression is independent of the hash table size + * the difference between 15 and 14 is very small + * for small blocks (and 14 is usually a bit faster). + * For a low-memory/faster configuration, use HLOG == 13; + * For best compression, use 15 or 16 (or more, up to 22). + */ +#ifndef HLOG +# define HLOG 16 +#endif + +/* + * Sacrifice very little compression quality in favour of compression speed. + * This gives almost the same compression as the default code, and is + * (very roughly) 15% faster. This is the preferred mode of operation. + */ +#ifndef VERY_FAST +# define VERY_FAST 1 +#endif + +/* + * Sacrifice some more compression quality in favour of compression speed. + * (roughly 1-2% worse compression for large blocks and + * 9-10% for small, redundant, blocks and >>20% better speed in both cases) + * In short: when in need for speed, enable this for binary data, + * possibly disable this for text data. + */ +#ifndef ULTRA_FAST +# define ULTRA_FAST 0 +#endif + +/* + * Unconditionally aligning does not cost very much, so do it if unsure + */ +#ifndef STRICT_ALIGN +# define STRICT_ALIGN !(defined(__i386) || defined (__amd64)) +#endif + +/* + * You may choose to pre-set the hash table (might be faster on some + * modern cpus and large (>>64k) blocks, and also makes compression + * deterministic/repeatable when the configuration otherwise is the same). + */ +#ifndef INIT_HTAB +# define INIT_HTAB 0 +#endif + +/* + * Avoid assigning values to errno variable? for some embedding purposes + * (linux kernel for example), this is necessary. NOTE: this breaks + * the documentation in lzf.h. Avoiding errno has no speed impact. + */ +#ifndef AVOID_ERRNO +# define AVOID_ERRNO 0 +#endif + +/* + * Whether to pass the LZF_STATE variable as argument, or allocate it + * on the stack. For small-stack environments, define this to 1. + * NOTE: this breaks the prototype in lzf.h. + */ +#ifndef LZF_STATE_ARG +# define LZF_STATE_ARG 0 +#endif + +/* + * Whether to add extra checks for input validity in lzf_decompress + * and return EINVAL if the input stream has been corrupted. This + * only shields against overflowing the input buffer and will not + * detect most corrupted streams. + * This check is not normally noticeable on modern hardware + * (<1% slowdown), but might slow down older cpus considerably. + */ +#ifndef CHECK_INPUT +# define CHECK_INPUT 1 +#endif + +/* + * Whether to store pointers or offsets inside the hash table. On + * 64 bit architetcures, pointers take up twice as much space, + * and might also be slower. Default is to autodetect. + */ +/*#define LZF_USER_OFFSETS autodetect */ + +/*****************************************************************************/ +/* nothing should be changed below */ + +#ifdef __cplusplus +# include +# include +using namespace std; +#else +# include +# include +#endif + +#ifndef LZF_USE_OFFSETS +# if defined (WIN32) +# define LZF_USE_OFFSETS defined(_M_X64) +# else +# if __cplusplus > 199711L +# include +# else +# include +# endif +# define LZF_USE_OFFSETS (UINTPTR_MAX > 0xffffffffU) +# endif +#endif + +typedef unsigned char u8; + +#if LZF_USE_OFFSETS +# define LZF_HSLOT_BIAS ((const u8 *)in_data) + typedef unsigned int LZF_HSLOT; +#else +# define LZF_HSLOT_BIAS 0 + typedef const u8 *LZF_HSLOT; +#endif + +typedef LZF_HSLOT LZF_STATE[1 << (HLOG)]; + +#if !STRICT_ALIGN +/* for unaligned accesses we need a 16 bit datatype. */ +# if USHRT_MAX == 65535 + typedef unsigned short u16; +# elif UINT_MAX == 65535 + typedef unsigned int u16; +# else +# undef STRICT_ALIGN +# define STRICT_ALIGN 1 +# endif +#endif + +#if ULTRA_FAST +# undef VERY_FAST +#endif + +#endif + diff --git a/lzf_d.c b/lzf_d.c new file mode 100644 index 0000000..8433b8f --- /dev/null +++ b/lzf_d.c @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2000-2010 Marc Alexander Lehmann + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#include "lzfP.h" + +#if AVOID_ERRNO +# define SET_ERRNO(n) +#else +# include +# define SET_ERRNO(n) errno = (n) +#endif + +#if USE_REP_MOVSB /* small win on amd, big loss on intel */ +#if (__i386 || __amd64) && __GNUC__ >= 3 +# define lzf_movsb(dst, src, len) \ + asm ("rep movsb" \ + : "=D" (dst), "=S" (src), "=c" (len) \ + : "0" (dst), "1" (src), "2" (len)); +#endif +#endif + +unsigned int +lzf_decompress (const void *const in_data, unsigned int in_len, + void *out_data, unsigned int out_len) +{ + u8 const *ip = (const u8 *)in_data; + u8 *op = (u8 *)out_data; + u8 const *const in_end = ip + in_len; + u8 *const out_end = op + out_len; + + do + { + unsigned int ctrl = *ip++; + + if (ctrl < (1 << 5)) /* literal run */ + { + ctrl++; + + if (op + ctrl > out_end) + { + SET_ERRNO (E2BIG); + return 0; + } + +#if CHECK_INPUT + if (ip + ctrl > in_end) + { + SET_ERRNO (EINVAL); + return 0; + } +#endif + +#ifdef lzf_movsb + lzf_movsb (op, ip, ctrl); +#else + switch (ctrl) + { + case 32: *op++ = *ip++; case 31: *op++ = *ip++; case 30: *op++ = *ip++; case 29: *op++ = *ip++; + case 28: *op++ = *ip++; case 27: *op++ = *ip++; case 26: *op++ = *ip++; case 25: *op++ = *ip++; + case 24: *op++ = *ip++; case 23: *op++ = *ip++; case 22: *op++ = *ip++; case 21: *op++ = *ip++; + case 20: *op++ = *ip++; case 19: *op++ = *ip++; case 18: *op++ = *ip++; case 17: *op++ = *ip++; + case 16: *op++ = *ip++; case 15: *op++ = *ip++; case 14: *op++ = *ip++; case 13: *op++ = *ip++; + case 12: *op++ = *ip++; case 11: *op++ = *ip++; case 10: *op++ = *ip++; case 9: *op++ = *ip++; + case 8: *op++ = *ip++; case 7: *op++ = *ip++; case 6: *op++ = *ip++; case 5: *op++ = *ip++; + case 4: *op++ = *ip++; case 3: *op++ = *ip++; case 2: *op++ = *ip++; case 1: *op++ = *ip++; + } +#endif + } + else /* back reference */ + { + unsigned int len = ctrl >> 5; + + u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; + +#if CHECK_INPUT + if (ip >= in_end) + { + SET_ERRNO (EINVAL); + return 0; + } +#endif + if (len == 7) + { + len += *ip++; +#if CHECK_INPUT + if (ip >= in_end) + { + SET_ERRNO (EINVAL); + return 0; + } +#endif + } + + ref -= *ip++; + + if (op + len + 2 > out_end) + { + SET_ERRNO (E2BIG); + return 0; + } + + if (ref < (u8 *)out_data) + { + SET_ERRNO (EINVAL); + return 0; + } + +#ifdef lzf_movsb + len += 2; + lzf_movsb (op, ref, len); +#else + switch (len) + { + default: + len += 2; + + if (op >= ref + len) + { + /* disjunct areas */ + memcpy (op, ref, len); + op += len; + } + else + { + /* overlapping, use octte by octte copying */ + do + *op++ = *ref++; + while (--len); + } + + break; + + case 9: *op++ = *ref++; + case 8: *op++ = *ref++; + case 7: *op++ = *ref++; + case 6: *op++ = *ref++; + case 5: *op++ = *ref++; + case 4: *op++ = *ref++; + case 3: *op++ = *ref++; + case 2: *op++ = *ref++; + case 1: *op++ = *ref++; + case 0: *op++ = *ref++; /* two octets more */ + *op++ = *ref++; + } +#endif + } + } + while (ip < in_end); + + return op - (u8 *)out_data; +} + diff --git a/parsevbox.c b/parsevbox.c new file mode 100644 index 0000000..aa17a34 --- /dev/null +++ b/parsevbox.c @@ -0,0 +1,205 @@ +#include +#include +#include + +typedef struct SSMFILEHDR +{ + /** Magic string which identifies this file as a version of VBox saved state + * file format (SSMFILEHDR_MAGIC_V2_0). */ + char szMagic[32]; + /** The major version number. */ + uint16_t u16VerMajor; + /** The minor version number. */ + uint16_t u16VerMinor; + /** The build number. */ + uint32_t u32VerBuild; + /** The SVN revision. */ + uint32_t u32SvnRev; + /** 32 or 64 depending on the host. */ + uint8_t cHostBits; + /** The size of RTGCPHYS. */ + uint8_t cbGCPhys; + /** The size of RTGCPTR. */ + uint8_t cbGCPtr; + /** Reserved header space - must be zero. */ + uint8_t u8Reserved; + /** The number of units that (may) have stored data in the file. */ + uint32_t cUnits; + /** Flags, see SSMFILEHDR_FLAGS_XXX. */ + uint32_t fFlags; + /** The maximum size of decompressed data. */ + uint32_t cbMaxDecompr; + /** The checksum of this header. + * This field is set to zero when calculating the checksum. */ + uint32_t u32CRC; +} __attribute__((packed)) SSMFILEHDR; + + +/** + * Data unit header. + */ +typedef struct SSMFILEUNITHDRV2 +{ + /** Magic (SSMFILEUNITHDR_MAGIC or SSMFILEUNITHDR_END). */ + char szMagic[8]; + /** The offset in the saved state stream of the start of this unit. + * This is mainly intended for sanity checking. */ + uint64_t offStream; + /** The CRC-in-progress value this unit starts at. */ + uint32_t u32CurStreamCRC; + /** The checksum of this structure, including the whole name. + * Calculated with this field set to zero. */ + uint32_t u32CRC; + /** Data version. */ + uint32_t u32Version; + /** Instance number. */ + uint32_t u32Instance; + /** Data pass number. */ + uint32_t u32Pass; + /** Flags reserved for future extensions. Must be zero. */ + uint32_t fFlags; + /** Size of the data unit name including the terminator. (bytes) */ + uint32_t cbName; + /** Data unit name, variable size. */ + //char szName[SSM_MAX_NAME_SIZE]; +} __attribute__((packed)) SSMFILEUNITHDRV2; + + +static const uint8_t UTF8ExtraBytes[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 +}; + +static const uint8_t FirstByteBits[7] = { + 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC +}; + +static const unsigned long FirstByteMask[6] = { + 0xFF, 0x1F, 0x0F, 0x07, 0x03, 0x03 +}; + + +enum { + Low6Bits = 0x3F, /* 00111111 */ + High2Bits = 0xC0, /* 11000000 */ + ByteMask = 0x00BF, /* 10111111 */ + ContinueBits = 0x80 /* 10xxxxxx */ +}; + + +long read_length(FILE *f) +{ + unsigned char ch; + fread(&ch, 1, 1, f); + if ((ch & 0x80)==0) { + return ch; + } else { + int extra_bytes = UTF8ExtraBytes[ch]; + char extra[256]; + fread(extra, extra_bytes, 1, f); + long res = ch & FirstByteMask[extra_bytes]; + unsigned char *src = extra; + do { + res <<= 6; + res |= ((*src++) & Low6Bits); + if (--extra_bytes == 0) + break; + } while (1); + return res; + } +} + +char buf[16*1024*1024]; +char outbuf[32*1024*1024]; + +unsigned int +lzf_decompress (const void *const in_data, unsigned int in_len, + void *out_data, unsigned int out_len); + +int main(int argc, char *argv[]) +{ + SSMFILEHDR head; + char outname[1024]; + + unsigned char c; + FILE *f = fopen(argv[1], "rb"); + if (!f) { + printf("cant open file\n"); + return 0; + } + + fread(&head, sizeof(SSMFILEHDR), 1, f); + printf("%s %ld\n", head.szMagic, head.cbMaxDecompr); + SSMFILEUNITHDRV2 unit; + + while (!feof(f) && !ferror(f)) { + fread(&unit, sizeof(SSMFILEUNITHDRV2), 1, f); + printf("Magic %s streampos : %lld cbsize: %d\n", unit.szMagic, unit.offStream, unit.cbName); + memset(buf, 0, sizeof(buf)); + fread(buf, unit.cbName, 1, f); + char nn[512]; + memset(nn, 0, sizeof(nn)); + memcpy(nn, buf, unit.cbName); + + printf("CBNAME: %s\n", nn); + + sprintf(outname, "%s-%s.out", argv[1], nn); + + printf("outname: %s\n", outname); + + FILE *fout = fopen(outname, "wb"); + + int rectype; + while (!feof(f) && !ferror(f)) { + fread(&c, 1, 1, f); + printf("C=> %02x\n", c); + rectype = c & 0xf; + printf("RECORD TYPE %02x\n", rectype); + if (rectype==1 || rectype==2 || rectype==3 || rectype==4) { + long r = read_length(f); + printf("len %ld\n", r); + + if (rectype==4) { + int n = fread(buf, 1, 1, f); + printf("Zero count: %d\n", buf[1]); + } else { + buf[r+1] = '\0'; + int n = fread(buf, 1, r, f); + printf("READ = %d\n", n); + //printf("BUF: %s\n", buf); + if (rectype==2) { + fwrite(buf, r, 1, fout); + } + + if (rectype==3) { + int outlen = buf[0]*1024; + int decres = lzf_decompress(buf+1, r-1, outbuf, outlen); + printf("Decompress result: %d\n", decres); + fwrite(outbuf, outlen, 1, fout); + fflush(fout); + } + } + + if (rectype==1) { + break; + } + } + if (rectype==00) { + printf("INVALID TYPE\n"); + break; + } + } + fclose(fout); + if (rectype==00) { + break; + } + } + + return 0; +}