mirror of
https://git.code.sf.net/p/quake/nuq
synced 2024-11-21 19:51:09 +00:00
bring over the glmesh stuff from newtree
This commit is contained in:
parent
b3048ed9f4
commit
c9ef370e77
4 changed files with 323 additions and 254 deletions
|
@ -30,9 +30,9 @@
|
|||
#ifndef _MDFOUR_H
|
||||
#define _MDFOUR_H
|
||||
|
||||
#ifndef _UINT32_H
|
||||
#include <uint32.h>
|
||||
#endif
|
||||
#include "uint32.h"
|
||||
|
||||
#define MDFOUR_DIGEST_BYTES 16
|
||||
|
||||
struct mdfour {
|
||||
uint32 A, B, C, D;
|
||||
|
|
345
source/gl_mesh.c
345
source/gl_mesh.c
|
@ -27,97 +27,82 @@
|
|||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <string.h>
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "qendian.h"
|
||||
#include "quakefs.h"
|
||||
#include "bspfile.h" // needed by: glquake.h
|
||||
#include "vid.h"
|
||||
#include "sys.h"
|
||||
#include "mathlib.h" // needed by: protocol.h, render.h, client.h,
|
||||
// modelgen.h, glmodel.h
|
||||
#include "wad.h"
|
||||
#include "draw.h"
|
||||
#include "cvar.h"
|
||||
#include "net.h" // needed by: client.h
|
||||
#include "protocol.h" // needed by: client.h
|
||||
#include "cmd.h"
|
||||
#include "sbar.h"
|
||||
#include "render.h" // needed by: client.h, model.h, glquake.h
|
||||
#include "client.h" // need cls in this file
|
||||
#include "model.h" // needed by: glquake.h
|
||||
#include "console.h"
|
||||
#include "glquake.h"
|
||||
#include "mdfour.h"
|
||||
#include "model.h"
|
||||
#include "quakefs.h"
|
||||
|
||||
/*
|
||||
=================================================================
|
||||
|
||||
ALIAS MODEL DISPLAY LIST GENERATION
|
||||
|
||||
=================================================================
|
||||
ALIAS MODEL DISPLAY LIST GENERATION
|
||||
*/
|
||||
|
||||
model_t *aliasmodel;
|
||||
aliashdr_t *paliashdr;
|
||||
model_t *aliasmodel;
|
||||
aliashdr_t *paliashdr;
|
||||
|
||||
qboolean used[8192];
|
||||
qboolean used[8192];
|
||||
|
||||
// the command list holds counts and s/t values that are valid for
|
||||
// every frame
|
||||
int commands[8192];
|
||||
int numcommands;
|
||||
int commands[8192];
|
||||
int numcommands;
|
||||
|
||||
// all frames will have their vertexes rearranged and expanded
|
||||
// so they are in the order expected by the command list
|
||||
int vertexorder[8192];
|
||||
int numorder;
|
||||
int vertexorder[8192];
|
||||
int numorder;
|
||||
|
||||
int allverts, alltris;
|
||||
int allverts, alltris;
|
||||
|
||||
int stripverts[128];
|
||||
int striptris[128];
|
||||
int stripcount;
|
||||
int stripverts[128];
|
||||
int striptris[128];
|
||||
int stripcount;
|
||||
|
||||
/*
|
||||
================
|
||||
StripLength
|
||||
================
|
||||
StripLength
|
||||
*/
|
||||
int StripLength (int starttri, int startv)
|
||||
int
|
||||
StripLength (int starttri, int startv)
|
||||
{
|
||||
int m1, m2;
|
||||
int j;
|
||||
mtriangle_t *last, *check;
|
||||
int k;
|
||||
int m1, m2;
|
||||
int j;
|
||||
mtriangle_t *last, *check;
|
||||
int k;
|
||||
|
||||
used[starttri] = 2;
|
||||
|
||||
last = &triangles[starttri];
|
||||
|
||||
stripverts[0] = last->vertindex[(startv)%3];
|
||||
stripverts[1] = last->vertindex[(startv+1)%3];
|
||||
stripverts[2] = last->vertindex[(startv+2)%3];
|
||||
stripverts[0] = last->vertindex[(startv) % 3];
|
||||
stripverts[1] = last->vertindex[(startv + 1) % 3];
|
||||
stripverts[2] = last->vertindex[(startv + 2) % 3];
|
||||
|
||||
striptris[0] = starttri;
|
||||
stripcount = 1;
|
||||
|
||||
m1 = last->vertindex[(startv+2)%3];
|
||||
m2 = last->vertindex[(startv+1)%3];
|
||||
m1 = last->vertindex[(startv + 2) % 3];
|
||||
m2 = last->vertindex[(startv + 1) % 3];
|
||||
|
||||
// look for a matching triangle
|
||||
nexttri:
|
||||
for (j=starttri+1, check=&triangles[starttri+1] ; j<pheader->mdl.numtris ; j++, check++)
|
||||
{
|
||||
for (j = starttri + 1, check = &triangles[starttri + 1];
|
||||
j < pheader->mdl.numtris; j++, check++) {
|
||||
if (check->facesfront != last->facesfront)
|
||||
continue;
|
||||
for (k=0 ; k<3 ; k++)
|
||||
{
|
||||
for (k = 0; k < 3; k++) {
|
||||
if (check->vertindex[k] != m1)
|
||||
continue;
|
||||
if (check->vertindex[ (k+1)%3 ] != m2)
|
||||
if (check->vertindex[(k + 1) % 3] != m2)
|
||||
continue;
|
||||
|
||||
// this is the next part of the fan
|
||||
|
@ -128,11 +113,11 @@ nexttri:
|
|||
|
||||
// the new edge
|
||||
if (stripcount & 1)
|
||||
m2 = check->vertindex[ (k+2)%3 ];
|
||||
m2 = check->vertindex[(k + 2) % 3];
|
||||
else
|
||||
m1 = check->vertindex[ (k+2)%3 ];
|
||||
m1 = check->vertindex[(k + 2) % 3];
|
||||
|
||||
stripverts[stripcount+2] = check->vertindex[ (k+2)%3 ];
|
||||
stripverts[stripcount + 2] = check->vertindex[(k + 2) % 3];
|
||||
striptris[stripcount] = j;
|
||||
stripcount++;
|
||||
|
||||
|
@ -143,7 +128,7 @@ nexttri:
|
|||
done:
|
||||
|
||||
// clear the temp used flags
|
||||
for (j=starttri+1 ; j<pheader->mdl.numtris ; j++)
|
||||
for (j = starttri + 1; j < pheader->mdl.numtris; j++)
|
||||
if (used[j] == 2)
|
||||
used[j] = 0;
|
||||
|
||||
|
@ -151,43 +136,41 @@ done:
|
|||
}
|
||||
|
||||
/*
|
||||
===========
|
||||
FanLength
|
||||
===========
|
||||
FanLength
|
||||
*/
|
||||
int FanLength (int starttri, int startv)
|
||||
int
|
||||
FanLength (int starttri, int startv)
|
||||
{
|
||||
int m1, m2;
|
||||
int j;
|
||||
mtriangle_t *last, *check;
|
||||
int k;
|
||||
int m1, m2;
|
||||
int j;
|
||||
mtriangle_t *last, *check;
|
||||
int k;
|
||||
|
||||
used[starttri] = 2;
|
||||
|
||||
last = &triangles[starttri];
|
||||
|
||||
stripverts[0] = last->vertindex[(startv)%3];
|
||||
stripverts[1] = last->vertindex[(startv+1)%3];
|
||||
stripverts[2] = last->vertindex[(startv+2)%3];
|
||||
stripverts[0] = last->vertindex[(startv) % 3];
|
||||
stripverts[1] = last->vertindex[(startv + 1) % 3];
|
||||
stripverts[2] = last->vertindex[(startv + 2) % 3];
|
||||
|
||||
striptris[0] = starttri;
|
||||
stripcount = 1;
|
||||
|
||||
m1 = last->vertindex[(startv+0)%3];
|
||||
m2 = last->vertindex[(startv+2)%3];
|
||||
m1 = last->vertindex[(startv + 0) % 3];
|
||||
m2 = last->vertindex[(startv + 2) % 3];
|
||||
|
||||
|
||||
// look for a matching triangle
|
||||
nexttri:
|
||||
for (j=starttri+1, check=&triangles[starttri+1] ; j<pheader->mdl.numtris ; j++, check++)
|
||||
{
|
||||
nexttri:
|
||||
for (j = starttri + 1, check = &triangles[starttri + 1];
|
||||
j < pheader->mdl.numtris; j++, check++) {
|
||||
if (check->facesfront != last->facesfront)
|
||||
continue;
|
||||
for (k=0 ; k<3 ; k++)
|
||||
{
|
||||
for (k = 0; k < 3; k++) {
|
||||
if (check->vertindex[k] != m1)
|
||||
continue;
|
||||
if (check->vertindex[ (k+1)%3 ] != m2)
|
||||
if (check->vertindex[(k + 1) % 3] != m2)
|
||||
continue;
|
||||
|
||||
// this is the next part of the fan
|
||||
|
@ -197,9 +180,9 @@ nexttri:
|
|||
goto done;
|
||||
|
||||
// the new edge
|
||||
m2 = check->vertindex[ (k+2)%3 ];
|
||||
m2 = check->vertindex[(k + 2) % 3];
|
||||
|
||||
stripverts[stripcount+2] = m2;
|
||||
stripverts[stripcount + 2] = m2;
|
||||
striptris[stripcount] = j;
|
||||
stripcount++;
|
||||
|
||||
|
@ -207,10 +190,10 @@ nexttri:
|
|||
goto nexttri;
|
||||
}
|
||||
}
|
||||
done:
|
||||
done:
|
||||
|
||||
// clear the temp used flags
|
||||
for (j=starttri+1 ; j<pheader->mdl.numtris ; j++)
|
||||
for (j = starttri + 1; j < pheader->mdl.numtris; j++)
|
||||
if (used[j] == 2)
|
||||
used[j] = 0;
|
||||
|
||||
|
@ -219,68 +202,63 @@ done:
|
|||
|
||||
|
||||
/*
|
||||
================
|
||||
BuildTris
|
||||
BuildTris
|
||||
|
||||
Generate a list of trifans or strips
|
||||
for the model, which holds for all frames
|
||||
================
|
||||
Generate a list of trifans or strips
|
||||
for the model, which holds for all frames
|
||||
*/
|
||||
void BuildTris (void)
|
||||
void
|
||||
BuildTris (void)
|
||||
{
|
||||
int i, j, k;
|
||||
int startv;
|
||||
float s, t;
|
||||
int len, bestlen, besttype = 0;
|
||||
int bestverts[1024];
|
||||
int besttris[1024];
|
||||
int type;
|
||||
int i, j, k;
|
||||
int startv;
|
||||
float s, t;
|
||||
int len, bestlen, besttype = 0;
|
||||
int bestverts[1024];
|
||||
int besttris[1024];
|
||||
int type;
|
||||
|
||||
//
|
||||
//
|
||||
// build tristrips
|
||||
//
|
||||
//
|
||||
numorder = 0;
|
||||
numcommands = 0;
|
||||
memset (used, 0, sizeof(used));
|
||||
for (i=0 ; i<pheader->mdl.numtris ; i++)
|
||||
{
|
||||
memset (used, 0, sizeof (used));
|
||||
for (i = 0; i < pheader->mdl.numtris; i++) {
|
||||
// pick an unused triangle and start the trifan
|
||||
if (used[i])
|
||||
continue;
|
||||
|
||||
bestlen = 0;
|
||||
for (type = 0 ; type < 2 ; type++)
|
||||
// type = 1;
|
||||
for (type = 0; type < 2; type++)
|
||||
// type = 1;
|
||||
{
|
||||
for (startv =0 ; startv < 3 ; startv++)
|
||||
{
|
||||
for (startv = 0; startv < 3; startv++) {
|
||||
if (type == 1)
|
||||
len = StripLength (i, startv);
|
||||
else
|
||||
len = FanLength (i, startv);
|
||||
if (len > bestlen)
|
||||
{
|
||||
if (len > bestlen) {
|
||||
besttype = type;
|
||||
bestlen = len;
|
||||
for (j=0 ; j<bestlen+2 ; j++)
|
||||
for (j = 0; j < bestlen + 2; j++)
|
||||
bestverts[j] = stripverts[j];
|
||||
for (j=0 ; j<bestlen ; j++)
|
||||
for (j = 0; j < bestlen; j++)
|
||||
besttris[j] = striptris[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mark the tris on the best strip as used
|
||||
for (j=0 ; j<bestlen ; j++)
|
||||
for (j = 0; j < bestlen; j++)
|
||||
used[besttris[j]] = 1;
|
||||
|
||||
if (besttype == 1)
|
||||
commands[numcommands++] = (bestlen+2);
|
||||
commands[numcommands++] = (bestlen + 2);
|
||||
else
|
||||
commands[numcommands++] = -(bestlen+2);
|
||||
commands[numcommands++] = -(bestlen + 2);
|
||||
|
||||
for (j=0 ; j<bestlen+2 ; j++)
|
||||
{
|
||||
for (j = 0; j < bestlen + 2; j++) {
|
||||
// emit a vertex into the reorder buffer
|
||||
k = bestverts[j];
|
||||
vertexorder[numorder++] = k;
|
||||
|
@ -293,14 +271,15 @@ void BuildTris (void)
|
|||
s = (s + 0.5) / pheader->mdl.skinwidth;
|
||||
t = (t + 0.5) / pheader->mdl.skinheight;
|
||||
|
||||
*(float *)&commands[numcommands++] = s;
|
||||
*(float *)&commands[numcommands++] = t;
|
||||
*(float *) &commands[numcommands++] = s;
|
||||
*(float *) &commands[numcommands++] = t;
|
||||
}
|
||||
}
|
||||
|
||||
commands[numcommands++] = 0; // end of list marker
|
||||
|
||||
Con_DPrintf ("%3i tri %3i vert %3i cmd\n", pheader->mdl.numtris, numorder, numcommands);
|
||||
Con_DPrintf ("%3i tri %3i vert %3i cmd\n", pheader->mdl.numtris, numorder,
|
||||
numcommands);
|
||||
|
||||
allverts += numorder;
|
||||
alltris += pheader->mdl.numtris;
|
||||
|
@ -308,65 +287,107 @@ void BuildTris (void)
|
|||
|
||||
|
||||
/*
|
||||
================
|
||||
GL_MakeAliasModelDisplayLists
|
||||
================
|
||||
GL_MakeAliasModelDisplayLists
|
||||
*/
|
||||
void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr)
|
||||
void
|
||||
GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr, void *_m, int _s)
|
||||
{
|
||||
int i, j;
|
||||
int *cmds;
|
||||
trivertx_t *verts;
|
||||
char cache[MAX_QPATH], fullpath[MAX_OSPATH];
|
||||
QFile *f;
|
||||
int i, j;
|
||||
int *cmds;
|
||||
trivertx_t *verts;
|
||||
char cache[MAX_QPATH], fullpath[MAX_OSPATH];
|
||||
QFile *f;
|
||||
unsigned char model_digest[MDFOUR_DIGEST_BYTES];
|
||||
unsigned char mesh_digest[MDFOUR_DIGEST_BYTES];
|
||||
qboolean remesh = true;
|
||||
|
||||
aliasmodel = m;
|
||||
paliashdr = hdr; // (aliashdr_t *)Mod_Extradata (m);
|
||||
paliashdr = hdr; // (aliashdr_t *)Mod_Extradata (m);
|
||||
|
||||
//
|
||||
mdfour (model_digest, (unsigned char*)_m, _s);
|
||||
|
||||
//
|
||||
// look for a cached version
|
||||
//
|
||||
//
|
||||
strcpy (cache, "glquake/");
|
||||
COM_StripExtension (m->name+strlen("progs/"), cache+strlen("glquake/"));
|
||||
strcat (cache, ".ms2");
|
||||
COM_StripExtension (m->name + strlen ("progs/"),
|
||||
cache + strlen ("glquake/"));
|
||||
strncat (cache, ".ms2", sizeof (cache) - strlen (cache));
|
||||
|
||||
COM_FOpenFile (cache, &f);
|
||||
if (f)
|
||||
{
|
||||
Qread (f, &numcommands, 4);
|
||||
Qread (f, &numorder, 4);
|
||||
Qread (f, &commands, numcommands * sizeof(commands[0]));
|
||||
Qread (f, &vertexorder, numorder * sizeof(vertexorder[0]));
|
||||
Qclose (f);
|
||||
COM_FOpenFile (cache, &f);
|
||||
if (f) {
|
||||
unsigned char d1[MDFOUR_DIGEST_BYTES];
|
||||
unsigned char d2[MDFOUR_DIGEST_BYTES];
|
||||
struct mdfour md;
|
||||
int c[8192];
|
||||
int nc;
|
||||
int vo[8192];
|
||||
int no;
|
||||
|
||||
memset (d1, 0, sizeof (d1));
|
||||
memset (d2, 0, sizeof (d2));
|
||||
Qread (f, &nc, 4);
|
||||
Qread (f, &no, 4);
|
||||
if (nc <= 8192 && no <= 8192) {
|
||||
Qread (f, &c, nc * sizeof (c[0]));
|
||||
Qread (f, &vo, no * sizeof (vo[0]));
|
||||
Qread (f, d1, MDFOUR_DIGEST_BYTES);
|
||||
Qread (f, d2, MDFOUR_DIGEST_BYTES);
|
||||
Qclose (f);
|
||||
|
||||
mdfour_begin (&md);
|
||||
mdfour_update (&md, (unsigned char*)&nc, 4);
|
||||
mdfour_update (&md, (unsigned char*)&no, 4);
|
||||
mdfour_update (&md, (unsigned char*)&c, nc * sizeof (c[0]));
|
||||
mdfour_update (&md, (unsigned char*)&vo, no * sizeof (vo[0]));
|
||||
mdfour_update (&md, d1, MDFOUR_DIGEST_BYTES);
|
||||
mdfour_result (&md, mesh_digest);
|
||||
|
||||
if (memcmp (d2, mesh_digest, MDFOUR_DIGEST_BYTES) == 0 && memcmp (d1, model_digest, MDFOUR_DIGEST_BYTES) == 0) {
|
||||
remesh = false;
|
||||
numcommands = nc;
|
||||
numorder = no;
|
||||
memcpy (commands, c, numcommands * sizeof (c[0]));
|
||||
memcpy (vertexorder, vo, numorder * sizeof (vo[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
if (remesh) {
|
||||
//
|
||||
// build it from scratch
|
||||
//
|
||||
Con_Printf ("meshing %s...\n",m->name);
|
||||
//
|
||||
Con_Printf ("meshing %s...\n", m->name);
|
||||
|
||||
BuildTris (); // trifans or lists
|
||||
BuildTris (); // trifans or lists
|
||||
|
||||
//
|
||||
//
|
||||
// save out the cached version
|
||||
//
|
||||
snprintf (fullpath, sizeof(fullpath), "%s/%s", com_gamedir, cache);
|
||||
f = Qopen (fullpath, "wb");
|
||||
//
|
||||
snprintf (fullpath, sizeof (fullpath), "%s/%s", com_gamedir, cache);
|
||||
f = Qopen (fullpath, "wbz9");
|
||||
if (!f) {
|
||||
char gldir[MAX_OSPATH];
|
||||
|
||||
snprintf (gldir, sizeof(gldir), "%s/glquake", com_gamedir);
|
||||
Sys_mkdir (gldir);
|
||||
COM_CreatePath (fullpath);
|
||||
f = Qopen (fullpath, "wb");
|
||||
}
|
||||
|
||||
if (f)
|
||||
{
|
||||
if (f) {
|
||||
struct mdfour md;
|
||||
|
||||
mdfour_begin (&md);
|
||||
mdfour_update (&md, (unsigned char*)&numcommands, 4);
|
||||
mdfour_update (&md, (unsigned char*)&numorder, 4);
|
||||
mdfour_update (&md, (unsigned char*)&commands, numcommands * sizeof (commands[0]));
|
||||
mdfour_update (&md, (unsigned char*)&vertexorder,
|
||||
numorder * sizeof (vertexorder[0]));
|
||||
mdfour_update (&md, model_digest, MDFOUR_DIGEST_BYTES);
|
||||
mdfour_result (&md, mesh_digest);
|
||||
|
||||
Qwrite (f, &numcommands, 4);
|
||||
Qwrite (f, &numorder, 4);
|
||||
Qwrite (f, &commands, numcommands * sizeof(commands[0]));
|
||||
Qwrite (f, &vertexorder, numorder * sizeof(vertexorder[0]));
|
||||
Qwrite (f, &commands, numcommands * sizeof (commands[0]));
|
||||
Qwrite (f, &vertexorder, numorder * sizeof (vertexorder[0]));
|
||||
Qwrite (f, model_digest, MDFOUR_DIGEST_BYTES);
|
||||
Qwrite (f, mesh_digest, MDFOUR_DIGEST_BYTES);
|
||||
Qclose (f);
|
||||
}
|
||||
}
|
||||
|
@ -377,14 +398,14 @@ void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr)
|
|||
paliashdr->poseverts = numorder;
|
||||
|
||||
cmds = Hunk_Alloc (numcommands * 4);
|
||||
paliashdr->commands = (byte *)cmds - (byte *)paliashdr;
|
||||
paliashdr->commands = (byte *) cmds - (byte *) paliashdr;
|
||||
memcpy (cmds, commands, numcommands * 4);
|
||||
|
||||
verts = Hunk_Alloc (paliashdr->numposes * paliashdr->poseverts
|
||||
* sizeof(trivertx_t) );
|
||||
paliashdr->posedata = (byte *)verts - (byte *)paliashdr;
|
||||
for (i=0 ; i<paliashdr->numposes ; i++)
|
||||
for (j=0 ; j<numorder ; j++)
|
||||
verts = Hunk_Alloc (paliashdr->numposes * paliashdr->poseverts
|
||||
|
||||
* sizeof (trivertx_t));
|
||||
paliashdr->posedata = (byte *) verts - (byte *) paliashdr;
|
||||
for (i = 0; i < paliashdr->numposes; i++)
|
||||
for (j = 0; j < numorder; j++)
|
||||
*verts++ = poseverts[i][vertexorder[j]];
|
||||
}
|
||||
|
||||
|
|
225
source/mdfour.c
225
source/mdfour.c
|
@ -28,17 +28,17 @@
|
|||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
# include "config.h"
|
||||
#endif
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
#endif
|
||||
#include <string.h> /* XoXus: needed for memset call */
|
||||
|
||||
#ifndef _UINT32_H
|
||||
#include <uint32.h>
|
||||
#endif // _UINT32_H
|
||||
|
||||
#ifndef _MDFOUR_H
|
||||
#include <mdfour.h>
|
||||
#endif // _MDFOUR_H
|
||||
#include "mdfour.h"
|
||||
#include "uint32.h"
|
||||
|
||||
/* NOTE: This code makes no attempt to be fast!
|
||||
|
||||
|
@ -50,10 +50,11 @@ static struct mdfour *m;
|
|||
#define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
|
||||
#define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
|
||||
#define H(X,Y,Z) ((X)^(Y)^(Z))
|
||||
|
||||
#ifdef LARGE_INT32
|
||||
#define lshift(x,s) ((((x)<<(s))&0xFFFFFFFF) | (((x)>>(32-(s)))&0xFFFFFFFF))
|
||||
# define lshift(x,s) ((((x)<<(s))&0xFFFFFFFF) | (((x)>>(32-(s)))&0xFFFFFFFF))
|
||||
#else
|
||||
#define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
|
||||
# define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
|
||||
#endif
|
||||
|
||||
#define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
|
||||
|
@ -61,77 +62,119 @@ static struct mdfour *m;
|
|||
#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
|
||||
|
||||
/* this applies md4 to 64 byte chunks */
|
||||
static void mdfour64(uint32 *M)
|
||||
static void
|
||||
mdfour64 (uint32 * M)
|
||||
{
|
||||
int j;
|
||||
uint32 AA, BB, CC, DD;
|
||||
uint32 X[16];
|
||||
uint32 A,B,C,D;
|
||||
int j;
|
||||
uint32 AA, BB, CC, DD;
|
||||
uint32 X[16];
|
||||
uint32 A, B, C, D;
|
||||
|
||||
for (j=0;j<16;j++)
|
||||
for (j = 0; j < 16; j++)
|
||||
X[j] = M[j];
|
||||
|
||||
A = m->A; B = m->B; C = m->C; D = m->D;
|
||||
AA = A; BB = B; CC = C; DD = D;
|
||||
A = m->A;
|
||||
B = m->B;
|
||||
C = m->C;
|
||||
D = m->D;
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
|
||||
ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
|
||||
ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
|
||||
ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
|
||||
ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
|
||||
ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
|
||||
ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
|
||||
ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
|
||||
ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
|
||||
ROUND1 (A, B, C, D, 0, 3);
|
||||
ROUND1 (D, A, B, C, 1, 7);
|
||||
ROUND1 (C, D, A, B, 2, 11);
|
||||
ROUND1 (B, C, D, A, 3, 19);
|
||||
ROUND1 (A, B, C, D, 4, 3);
|
||||
ROUND1 (D, A, B, C, 5, 7);
|
||||
ROUND1 (C, D, A, B, 6, 11);
|
||||
ROUND1 (B, C, D, A, 7, 19);
|
||||
ROUND1 (A, B, C, D, 8, 3);
|
||||
ROUND1 (D, A, B, C, 9, 7);
|
||||
ROUND1 (C, D, A, B, 10, 11);
|
||||
ROUND1 (B, C, D, A, 11, 19);
|
||||
ROUND1 (A, B, C, D, 12, 3);
|
||||
ROUND1 (D, A, B, C, 13, 7);
|
||||
ROUND1 (C, D, A, B, 14, 11);
|
||||
ROUND1 (B, C, D, A, 15, 19);
|
||||
|
||||
ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
|
||||
ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
|
||||
ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
|
||||
ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
|
||||
ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
|
||||
ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
|
||||
ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
|
||||
ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
|
||||
ROUND2 (A, B, C, D, 0, 3);
|
||||
ROUND2 (D, A, B, C, 4, 5);
|
||||
ROUND2 (C, D, A, B, 8, 9);
|
||||
ROUND2 (B, C, D, A, 12, 13);
|
||||
ROUND2 (A, B, C, D, 1, 3);
|
||||
ROUND2 (D, A, B, C, 5, 5);
|
||||
ROUND2 (C, D, A, B, 9, 9);
|
||||
ROUND2 (B, C, D, A, 13, 13);
|
||||
ROUND2 (A, B, C, D, 2, 3);
|
||||
ROUND2 (D, A, B, C, 6, 5);
|
||||
ROUND2 (C, D, A, B, 10, 9);
|
||||
ROUND2 (B, C, D, A, 14, 13);
|
||||
ROUND2 (A, B, C, D, 3, 3);
|
||||
ROUND2 (D, A, B, C, 7, 5);
|
||||
ROUND2 (C, D, A, B, 11, 9);
|
||||
ROUND2 (B, C, D, A, 15, 13);
|
||||
|
||||
ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
|
||||
ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
|
||||
ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
|
||||
ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
|
||||
ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
|
||||
ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
|
||||
ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
|
||||
ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
|
||||
ROUND3 (A, B, C, D, 0, 3);
|
||||
ROUND3 (D, A, B, C, 8, 9);
|
||||
ROUND3 (C, D, A, B, 4, 11);
|
||||
ROUND3 (B, C, D, A, 12, 15);
|
||||
ROUND3 (A, B, C, D, 2, 3);
|
||||
ROUND3 (D, A, B, C, 10, 9);
|
||||
ROUND3 (C, D, A, B, 6, 11);
|
||||
ROUND3 (B, C, D, A, 14, 15);
|
||||
ROUND3 (A, B, C, D, 1, 3);
|
||||
ROUND3 (D, A, B, C, 9, 9);
|
||||
ROUND3 (C, D, A, B, 5, 11);
|
||||
ROUND3 (B, C, D, A, 13, 15);
|
||||
ROUND3 (A, B, C, D, 3, 3);
|
||||
ROUND3 (D, A, B, C, 11, 9);
|
||||
ROUND3 (C, D, A, B, 7, 11);
|
||||
ROUND3 (B, C, D, A, 15, 15);
|
||||
|
||||
A += AA; B += BB; C += CC; D += DD;
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
|
||||
#ifdef LARGE_INT32
|
||||
A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
|
||||
C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
|
||||
A &= 0xFFFFFFFF;
|
||||
B &= 0xFFFFFFFF;
|
||||
C &= 0xFFFFFFFF;
|
||||
D &= 0xFFFFFFFF;
|
||||
#endif
|
||||
|
||||
for (j=0;j<16;j++)
|
||||
for (j = 0; j < 16; j++)
|
||||
X[j] = 0;
|
||||
|
||||
m->A = A; m->B = B; m->C = C; m->D = D;
|
||||
m->A = A;
|
||||
m->B = B;
|
||||
m->C = C;
|
||||
m->D = D;
|
||||
}
|
||||
|
||||
static void copy64(uint32 *M, unsigned char *in)
|
||||
static void
|
||||
copy64 (uint32 * M, unsigned char *in)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
for (i=0;i<16;i++)
|
||||
M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
|
||||
(in[i*4+1]<<8) | (in[i*4+0]<<0);
|
||||
for (i = 0; i < 16; i++)
|
||||
M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) |
|
||||
(in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
|
||||
}
|
||||
|
||||
static void copy4(unsigned char *out,uint32 x)
|
||||
static void
|
||||
copy4 (unsigned char *out, uint32 x)
|
||||
{
|
||||
out[0] = x&0xFF;
|
||||
out[1] = (x>>8)&0xFF;
|
||||
out[2] = (x>>16)&0xFF;
|
||||
out[3] = (x>>24)&0xFF;
|
||||
out[0] = x & 0xFF;
|
||||
out[1] = (x >> 8) & 0xFF;
|
||||
out[2] = (x >> 16) & 0xFF;
|
||||
out[3] = (x >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
void mdfour_begin(struct mdfour *md)
|
||||
void
|
||||
mdfour_begin (struct mdfour *md)
|
||||
{
|
||||
md->A = 0x67452301;
|
||||
md->B = 0xefcdab89;
|
||||
|
@ -141,69 +184,75 @@ void mdfour_begin(struct mdfour *md)
|
|||
}
|
||||
|
||||
|
||||
static void mdfour_tail(unsigned char *in, int n)
|
||||
static void
|
||||
mdfour_tail (unsigned char *in, int n)
|
||||
{
|
||||
unsigned char buf[128];
|
||||
uint32 M[16];
|
||||
uint32 b;
|
||||
uint32 M[16];
|
||||
uint32 b;
|
||||
|
||||
m->totalN += n;
|
||||
|
||||
b = m->totalN * 8;
|
||||
|
||||
memset(buf, 0, 128);
|
||||
if (n) memcpy(buf, in, n);
|
||||
memset (buf, 0, 128);
|
||||
if (n)
|
||||
memcpy (buf, in, n);
|
||||
buf[n] = 0x80;
|
||||
|
||||
if (n <= 55) {
|
||||
copy4(buf+56, b);
|
||||
copy64(M, buf);
|
||||
mdfour64(M);
|
||||
copy4 (buf + 56, b);
|
||||
copy64 (M, buf);
|
||||
mdfour64 (M);
|
||||
} else {
|
||||
copy4(buf+120, b);
|
||||
copy64(M, buf);
|
||||
mdfour64(M);
|
||||
copy64(M, buf+64);
|
||||
mdfour64(M);
|
||||
copy4 (buf + 120, b);
|
||||
copy64 (M, buf);
|
||||
mdfour64 (M);
|
||||
copy64 (M, buf + 64);
|
||||
mdfour64 (M);
|
||||
}
|
||||
}
|
||||
|
||||
void mdfour_update(struct mdfour *md, unsigned char *in, int n)
|
||||
void
|
||||
mdfour_update (struct mdfour *md, unsigned char *in, int n)
|
||||
{
|
||||
uint32 M[16];
|
||||
uint32 M[16];
|
||||
|
||||
if (n == 0) mdfour_tail(in, n);
|
||||
if (n == 0)
|
||||
mdfour_tail (in, n);
|
||||
|
||||
m = md;
|
||||
|
||||
while (n >= 64) {
|
||||
copy64(M, in);
|
||||
mdfour64(M);
|
||||
copy64 (M, in);
|
||||
mdfour64 (M);
|
||||
in += 64;
|
||||
n -= 64;
|
||||
m->totalN += 64;
|
||||
}
|
||||
|
||||
mdfour_tail(in, n);
|
||||
mdfour_tail (in, n);
|
||||
}
|
||||
|
||||
|
||||
void mdfour_result(struct mdfour *md, unsigned char *out)
|
||||
void
|
||||
mdfour_result (struct mdfour *md, unsigned char *out)
|
||||
{
|
||||
m = md;
|
||||
|
||||
copy4(out, m->A);
|
||||
copy4(out+4, m->B);
|
||||
copy4(out+8, m->C);
|
||||
copy4(out+12, m->D);
|
||||
copy4 (out, m->A);
|
||||
copy4 (out + 4, m->B);
|
||||
copy4 (out + 8, m->C);
|
||||
copy4 (out + 12, m->D);
|
||||
}
|
||||
|
||||
|
||||
void mdfour(unsigned char *out, unsigned char *in, int n)
|
||||
void
|
||||
mdfour (unsigned char *out, unsigned char *in, int n)
|
||||
{
|
||||
struct mdfour md;
|
||||
mdfour_begin(&md);
|
||||
mdfour_update(&md, in, n);
|
||||
mdfour_result(&md, out);
|
||||
}
|
||||
|
||||
mdfour_begin (&md);
|
||||
mdfour_update (&md, in, n);
|
||||
mdfour_result (&md, out);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,6 @@
|
|||
|
||||
#include "console.h"
|
||||
#include "qargs.h"
|
||||
#include "qtypes.h"
|
||||
#include "sound.h"
|
||||
#include "va.h"
|
||||
|
||||
|
|
Loading…
Reference in a new issue