1 // ByteSwap.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../Common/MyCom.h"
6 
7 #include "../ICoder.h"
8 
9 #include "../Common/RegisterCodec.h"
10 
11 class CByteSwap2:
12   public ICompressFilter,
13   public CMyUnknownImp
14 {
15 public:
16   MY_UNKNOWN_IMP
17   STDMETHOD(Init)();
18   STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
19 };
20 
21 class CByteSwap4:
22   public ICompressFilter,
23   public CMyUnknownImp
24 {
25 public:
26   MY_UNKNOWN_IMP
27   STDMETHOD(Init)();
28   STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
29 };
30 
Init()31 STDMETHODIMP CByteSwap2::Init() { return S_OK; }
32 
STDMETHODIMP_(UInt32)33 STDMETHODIMP_(UInt32) CByteSwap2::Filter(Byte *data, UInt32 size)
34 {
35   const UInt32 kStep = 2;
36   UInt32 i;
37   for (i = 0; i + kStep <= size; i += kStep)
38   {
39     Byte b = data[i];
40     data[i] = data[i + 1];
41     data[i + 1] = b;
42   }
43   return i;
44 }
45 
Init()46 STDMETHODIMP CByteSwap4::Init() { return S_OK; }
47 
STDMETHODIMP_(UInt32)48 STDMETHODIMP_(UInt32) CByteSwap4::Filter(Byte *data, UInt32 size)
49 {
50   const UInt32 kStep = 4;
51   UInt32 i;
52   for (i = 0; i + kStep <= size; i += kStep)
53   {
54     Byte b0 = data[i];
55     Byte b1 = data[i + 1];
56     data[i] = data[i + 3];
57     data[i + 1] = data[i + 2];
58     data[i + 2] = b1;
59     data[i + 3] = b0;
60   }
61   return i;
62 }
63 
CreateCodec2()64 static void *CreateCodec2() { return (void *)(ICompressFilter *)(new CByteSwap2); }
CreateCodec4()65 static void *CreateCodec4() { return (void *)(ICompressFilter *)(new CByteSwap4); }
66 
67 static CCodecInfo g_CodecsInfo[] =
68 {
69   { CreateCodec2, CreateCodec2, 0x020302, L"Swap2", 1, true },
70   { CreateCodec4, CreateCodec4, 0x020304, L"Swap4", 1, true }
71 };
72 
73 REGISTER_CODECS(ByteSwap)
74