ReactOS  0.4.13-dev-698-g77671f0
drawdib.c
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Akihiro Sagawa
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #define WIN32_LEAN_AND_MEAN
20 
21 #include <windows.h>
22 #include <vfw.h>
23 #include <wincrypt.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "wine/test.h"
28 
29 #define WIDTH 16
30 #define HEIGHT 12
31 
33 
34 static inline DWORD get_stride(const BITMAPINFO *bmi)
35 {
36  return ((bmi->bmiHeader.biBitCount * bmi->bmiHeader.biWidth + 31) >> 3) & ~3;
37 }
38 
39 static inline DWORD get_dib_size(const BITMAPINFO *bmi)
40 {
41  return get_stride(bmi) * abs(bmi->bmiHeader.biHeight);
42 }
43 
44 static char *hash_dib(const BITMAPINFO *bmi, const void *bits)
45 {
46  DWORD dib_size = get_dib_size(bmi);
48  char *buf;
49  BYTE hash_buf[20];
50  DWORD hash_size = sizeof(hash_buf);
51  int i;
52  static const char *hex = "0123456789abcdef";
53 
54  if(!crypt_prov) return NULL;
55 
56  if(!CryptCreateHash(crypt_prov, CALG_SHA1, 0, 0, &hash)) return NULL;
57 
58  CryptHashData(hash, bits, dib_size, 0);
59 
60  CryptGetHashParam(hash, HP_HASHVAL, NULL, &hash_size, 0);
61  if(hash_size != sizeof(hash_buf)) return NULL;
62 
63  CryptGetHashParam(hash, HP_HASHVAL, hash_buf, &hash_size, 0);
65 
66  buf = HeapAlloc(GetProcessHeap(), 0, hash_size * 2 + 1);
67 
68  for(i = 0; i < hash_size; i++)
69  {
70  buf[i * 2] = hex[hash_buf[i] >> 4];
71  buf[i * 2 + 1] = hex[hash_buf[i] & 0xf];
72  }
73  buf[i * 2] = '\0';
74 
75  return buf;
76 }
77 
79 {
80  memset(bmi, 0, sizeof(*bmi));
81  bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
82  bmi->bmiHeader.biWidth = width;
83  bmi->bmiHeader.biHeight = height;
84  bmi->bmiHeader.biPlanes = 1;
85  bmi->bmiHeader.biBitCount = 32;
87  bmi->bmiHeader.biSizeImage = size;
88 }
89 
90 static void test_DrawDib_sizeimage(void)
91 {
92  const struct {
93  LONG width, height;
94  DWORD size;
95  char hash[41];
96  } test_data[] = {
97  /* [0] correct size */
98  { WIDTH, HEIGHT, WIDTH * HEIGHT * sizeof(RGBQUAD), "bc943d5ab024b8b0118d0a80aa283055d39942b8" },
99  /* [1] zero size */
100  { WIDTH, HEIGHT, 0, "bc943d5ab024b8b0118d0a80aa283055d39942b8" },
101  /* error patterns */
102  { WIDTH, -HEIGHT, 0, "" },
103  { -WIDTH, HEIGHT, 0, "" },
104  { -WIDTH, -HEIGHT, 0, "" },
105  { 0, 0, 0, "" },
106  { 0, HEIGHT, 0, "" },
107  { WIDTH, 0, 0, "" },
108  /* [8] zero size (to compare [9], [10] ) */
109  { WIDTH, HEIGHT/2, 0, "8b75bf6d54a8645380114fe77505ee0699ffffaa" },
110  /* [9] insufficient size */
111  { WIDTH, HEIGHT/2, sizeof(RGBQUAD), "8b75bf6d54a8645380114fe77505ee0699ffffaa" },
112  /* [10] too much size */
113  { WIDTH, HEIGHT/2, WIDTH * HEIGHT * sizeof(RGBQUAD), "8b75bf6d54a8645380114fe77505ee0699ffffaa" },
114  };
115  HDC hdc;
116  DWORD src_dib_size, dst_dib_size;
117  BOOL r;
118  HBITMAP dib;
119  BITMAPINFO src_info, dst_info;
120  RGBQUAD *src_bits = NULL, *dst_bits;
121  HDRAWDIB hdd;
122  unsigned int i;
123 
125 
126  init_bmi(&dst_info, WIDTH, HEIGHT, 0);
127  dib = CreateDIBSection(NULL, &dst_info, DIB_RGB_COLORS, (void **)&dst_bits, NULL, 0);
128  dst_dib_size = get_dib_size(&dst_info);
129  ok(dib != NULL, "CreateDIBSection failed\n");
130  SelectObject(hdc, dib);
131 
132  init_bmi(&src_info, WIDTH, HEIGHT, 0);
133  src_dib_size = get_dib_size(&src_info);
134  src_bits = HeapAlloc(GetProcessHeap(), 0, src_dib_size);
135  ok(src_bits != NULL, "Can't allocate memory\n");
136  memset(src_bits, 0x88, src_dib_size);
137 
138  hdd = DrawDibOpen();
139  ok(hdd != NULL, "DrawDibOpen failed\n");
140 
141  for (i = 0; i < ARRAY_SIZE(test_data); i++) {
142  char *hash;
143  memset(dst_bits, 0xff, dst_dib_size);
145  r = DrawDibDraw(hdd, hdc,
146  0, 0, -1, -1, &src_info.bmiHeader, src_bits,
147  0, 0, test_data[i].width, test_data[i].height, 0);
148  if (test_data[i].hash[0])
149  ok(r, "[%u] DrawDibDraw failed, expected success\n", i);
150  else
151  ok(!r, "[%u] DrawDibDraw succeeded, expected failed\n", i);
152  if (!r || !test_data[i].hash[0])
153  continue;
154 
155  hash = hash_dib(&dst_info, dst_bits);
156  if (!hash) {
157  win_skip("This platform doesn't support SHA-1 hash\n");
158  continue;
159  }
160  ok(strcmp(hash, test_data[i].hash) == 0,
161  "[%u] got %s, expected %s\n",
162  i, hash, test_data[i].hash);
164  }
165 
166  r = DrawDibClose(hdd);
167  ok(r, "DrawDibClose failed\n");
168 
169  HeapFree(GetProcessHeap(), 0, src_bits);
170 
171  DeleteDC(hdc);
172 }
173 
174 START_TEST(drawdib)
175 {
179 }
#define abs(i)
Definition: fconv.c:206
static DWORD get_stride(const BITMAPINFO *bmi)
Definition: drawdib.c:34
GLint GLint GLsizei width
Definition: gl.h:1546
static char * hash_dib(const BITMAPINFO *bmi, const void *bits)
Definition: drawdib.c:44
HDRAWDIB VFWAPI DrawDibOpen(void)
Definition: drawdib.c:80
BITMAPINFOHEADER bmiHeader
Definition: wingdi.h:1454
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
struct tagBITMAPINFOHEADER BITMAPINFOHEADER
static HDC
Definition: imagelist.c:92
static void init_bmi(BITMAPINFO *bmi, LONG width, LONG height, DWORD size)
Definition: drawdib.c:78
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
static HCRYPTPROV crypt_prov
Definition: drawdib.c:32
static const char hex[16]
Definition: profile.c:123
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
static void test_DrawDib_sizeimage(void)
Definition: drawdib.c:90
BOOL WINAPI CryptAcquireContextW(HCRYPTPROV *phProv, LPCWSTR pszContainer, LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
Definition: crypt.c:358
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
BOOL WINAPI CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD *pdwDataLen, DWORD dwFlags)
Definition: crypt.c:1615
int hash
Definition: main.c:58
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
DWORD biCompression
Definition: amvideo.idl:35
#define ok(value,...)
BOOL VFWAPI DrawDibDraw(HDRAWDIB hdd, HDC hdc, INT xDst, INT yDst, INT dxDst, INT dyDst, LPBITMAPINFOHEADER lpbi, LPVOID lpBits, INT xSrc, INT ySrc, INT dxSrc, INT dySrc, UINT wFlags)
Definition: drawdib.c:308
#define WIDTH
Definition: drawdib.c:29
smooth NULL
Definition: ftsmooth.c:416
struct tagRGBQUAD RGBQUAD
GLsizeiptr size
Definition: glext.h:5919
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD biSizeImage
Definition: amvideo.idl:36
BOOL WINAPI CryptDestroyHash(HCRYPTHASH hHash)
Definition: crypt.c:895
HDC hdc
Definition: main.c:9
static DWORD get_dib_size(const BITMAPINFO *bmi)
Definition: drawdib.c:39
unsigned char BYTE
Definition: mem.h:68
BOOL WINAPI CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH *phHash)
Definition: crypt.c:745
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
#define CRYPT_VERIFYCONTEXT
Definition: wincrypt.h:2069
ULONG_PTR HCRYPTHASH
Definition: wincrypt.h:50
#define HEIGHT
Definition: drawdib.c:30
BOOL WINAPI DeleteDC(_In_ HDC)
ULONG_PTR HCRYPTPROV
Definition: wincrypt.h:46
#define ARRAY_SIZE(a)
Definition: main.h:24
HBITMAP WINAPI CreateDIBSection(HDC hDC, CONST BITMAPINFO *BitmapInfo, UINT Usage, VOID **Bits, HANDLE hSection, DWORD dwOffset)
Definition: bitmap.c:197
BOOL WINAPI CryptHashData(HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
Definition: crypt.c:1776
#define CALG_SHA1
Definition: wincrypt.h:1807
START_TEST(drawdib)
Definition: drawdib.c:174
static const BYTE dib[]
Definition: ole2.c:201
BOOL WINAPI CryptReleaseContext(HCRYPTPROV hProv, ULONG_PTR dwFlags)
Definition: crypt.c:651
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
static HBITMAP
Definition: button.c:44
#define DIB_RGB_COLORS
Definition: wingdi.h:366
#define HP_HASHVAL
Definition: wincrypt.h:2183
Definition: _hash_fun.h:40
#define memset(x, y, z)
Definition: compat.h:39
#define win_skip
Definition: test.h:141
#define BI_RGB
Definition: precomp.h:34
#define HeapFree(x, y, z)
Definition: compat.h:394
#define PROV_RSA_FULL
Definition: wincrypt.h:2039
BOOL VFWAPI DrawDibClose(HDRAWDIB hdd)
Definition: drawdib.c:101