The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

#define  LEAVE {UPDATE return inflate_flush(s,z,r);}

#define  LEAVE {UPDATE return inflate_flush(s,z,r);}

Does this code look familiar? Has anyone here used zlib? Has anyone looked into it? If you google for that you’ll find hundreds of sites hosting that line of code.

(Like these for example: and and

And what is UPDATE?



#define  UPDBITS {s->bitb=b;s->bitk=k;}
#define  UPDIN {z->avail_in=n;z->total_in+=(uLong)(p-z->next_in);z->next_in=p;}
#define  UPDOUT {s->write=q;}

and uLong?

typedef unsigned long  uLong;

The code utilizing that ugly early-basic-like constructs looks even better:

      if (n == 0)
      t = s->sub.left;

(IBM_xxx has nothing to do with IBM, it stands for “inflate block mode”)

Few weeks ago, there was a thread here, “Handy C++ macros/functions” [] Seems like everybody agrees that such code is stupid and it was one of the funniest threads at JoS in quite some time. Well, we all had a good laugh, but every time I look at some open source project I see the same problems.

Why is that?!
Saturday, April 29, 2006
The secret of using zlib, is:

1. Download the code
2. Read the license file
3. Read the document with a list of APIs
4. Call the APIs
5. Link zlib
6. But whatever you do, don't read the zlib code!
S. Tanna
Saturday, April 29, 2006
Don't forget that the zlib project was started a long time ago - you'll find similar gems in the POV-Ray code, for example. Many open source projects with a fresher code base don't have those problems.
nickelplate Send private email
Saturday, April 29, 2006
Unfortunately, "projects with a fresher code base" are not that interesting. Zlib is fantastic code and at the same time it really su**s. That's why I need to break this toy before I can play with it. :)

Anyway, I found this function in zlib:

// My own strcmpi / strcasecmp
int strcmpcasenosensitive_internal (const char* fileName1, const char *fileName2)
    for (;;)
        char c1=*(fileName1++);
        char c2=*(fileName2++);
        if ((c1>='a') && (c1<='z'))
            c1 -= (char)0x20;
        if ((c2>='a') && (c2<='z'))
            c2 -= (char)0x20;
        if (c1=='\0')
            return ((c2=='\0') ? 0 : -1);
        if (c2=='\0')
            return 1;
        if (c1<c2)
            return -1;
        if (c1>c2)
            return 1;

Why don't they use stricmp like everybody else? What about Unicode? Has Ben Bryant ever used zlib? Googling for "Zip Format Unicode" pointed me to Michael Kaplan's blog where he wrote about it:

Should I care? Would adding Unicode support break the format? I think I saw somewhere something about Apple extending Zip format with support for resource fork found on Macs. Wasn't that some sort of breach of compatibility?

Any recommendations about it? Should I switch to some RAR library?
Saturday, April 29, 2006
I didn't read the code

but I'm guessing

typedef unsigned long  uLong;

may not be completely stupid

Perhaps uLong is their type, which must be 32 bits unsigned (in case the library is ported), the ideal being to change the typedef if a 32 bit unsigned is something else on another platform.

I've written code myself, which uses definitions like

S. Tanna
Sunday, April 30, 2006
"Why don't they use stricmp like everybody else?"

Maybe not all platforms have stricmp..  zlib is in everything; even embedded platforms.

"Would adding Unicode support break the format?"

This seems very likely.  The format itself is pretty well defined.  Even if one tool supported unicode names all the old existing tools would not.

"I think I saw somewhere something about Apple extending Zip format with support for resource fork found on Macs. Wasn't that some sort of breach of compatibility?"

Resource forks are added to the zip archive as extra files.  This doesn't break the format (except that you see the resource forks in the zip).
Almost H. Anonymous Send private email
Sunday, April 30, 2006
>> The secret of using zlib, is:
>> 6. But whatever you do, don't read the zlib code!
>> …
>> I didn't read the code

So, you stick to your rules…

>> typedef unsigned long  uLong;
>> may not be completely stupid

uLong was not headline of this story. It’s the LEAVE macro.

But then again, uLong is something from unzip headers. In zip part of the library, there is something else:

typedef unsigned long  ulg;

Zlib seem to be work of (at least) two programmers. And since it is a very successful library, a proof that everyone should stick to his own habits. My productivity would probably fall at least 10% if someone forced me to change  style or my indentation rules.

I just wondered why every time I look into one of the widespread and really useful libraries created by all the smart people I find such nasty code!
Sunday, April 30, 2006
For all the problems with old code, there's a couple of conflicting rules:

1) If it's stupid, don't do it
2) If it works, don't rewrite it just for the sake of your personal view of "purity"

A lot of open source code contains some nasty crap because rewriting working code to be pretty is generally just not done. A lot of closed source code is exactly the same, it's just not publicised.

Sunday, April 30, 2006
About stricmp(): first of all, it's non-standard (use strcasecmp instead).  Second: are there any systems for which stricmp is (perhaps wrongly) dependent on locale?  If so, then you have an environmental influence.  If the ZLib code is being used in a security-sensitive environment, then such an influence might become an avenue for attack.  Using your own function eliminates this possibility.
David Jones Send private email
Monday, May 01, 2006
Considering the age of the code, I expect that lots of those macros are in there as an optimization in the face of stupid compilers.

Things may have gotten better since, but why bother changing something that still works?
Chris Tavares Send private email
Thursday, May 04, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz