Switching to crashmail Jamlib

This commit is contained in:
Andrew Pamment
2016-03-25 10:58:57 +10:00
parent a8e28ee751
commit de14c1ba69
82 changed files with 1920 additions and 30144 deletions

504
jamlib/LICENSE Normal file
View File

@@ -0,0 +1,504 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!

29
jamlib/Makefile.linux Normal file
View File

@@ -0,0 +1,29 @@
CC = gcc $(CPPFLAGS) $(CFLAGS) -D__LINUX__ $(LDFLAGS) -Wall
AR = ar -ru
RM = rm -f
OBJS = crc32.o mbase.o message.o lastread.o subpack.o structrw.o
jamlib.a : $(OBJS)
$(AR) jamlib.a $(OBJS)
crc32.o: crc32.c jam.h
$(CC) -c crc32.c -o crc32.o
mbase.o: mbase.c jam.h
$(CC) -c mbase.c -o mbase.o
message.o: message.c jam.h
$(CC) -c message.c -o message.o
lastread.o: lastread.c jam.h
$(CC) -c lastread.c -o lastread.o
subpack.o: subpack.c jam.h
$(CC) -c subpack.c -o subpack.o
structrw.o: structrw.c jam.h
$(CC) -c structrw.c -o structrw.o
clean:
$(RM) *.o *.a

30
jamlib/Makefile.os2 Normal file
View File

@@ -0,0 +1,30 @@
CC = gcc -Wall -D__OS2__ -DINCL_DOSPROCESS
AR = ar -ru
RM = del
OBJS = crc32.o mbase.o message.o lastread.o subpack.o structrw.o
jamlib.a: $(OBJS)
$(AR) jamlib.a $(OBJS)
crc32.o: crc32.c jam.h
$(CC) -c crc32.c -o crc32.o
mbase.o: mbase.c jam.h
$(CC) -c mbase.c -o mbase.o
message.o: message.c jam.h
$(CC) -c message.c -o message.o
lastread.o: lastread.c jam.h
$(CC) -c lastread.c -o lastread.o
subpack.o: subpack.c jam.h
$(CC) -c subpack.c -o subpack.o
structrw.o: structrw.c jam.h
$(CC) -c structrw.c -o structrw.o
clean:
$(RM) *.o *.a

29
jamlib/Makefile.win32 Normal file
View File

@@ -0,0 +1,29 @@
CC = gcc -Wall
AR = ar -ru
RM = del
OBJS = crc32.o mbase.o message.o lastread.o subpack.o structrw.o
jamlib.a : $(OBJS)
$(AR) jamlib.a $(OBJS)
crc32.o: crc32.c jam.h
$(CC) -c crc32.c -o crc32.o
mbase.o: mbase.c jam.h
$(CC) -c mbase.c -o mbase.o
message.o: message.c jam.h
$(CC) -c message.c -o message.o
lastread.o: lastread.c jam.h
$(CC) -c lastread.c -o lastread.o
subpack.o: subpack.c jam.h
$(CC) -c subpack.c -o subpack.o
structrw.o: structrw.c jam.h
$(CC) -c structrw.c -o structrw.o
clean:
$(RM) *.o *.a

78
jamlib/crc32.c Normal file
View File

@@ -0,0 +1,78 @@
/*
Crc32 - CRC32-calculation for JAM
Copyright (C) 2000 Johan Billing
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2003-10-22
- Fixed comparison between signed and unsigned variable in JAM_Crc32()
*/
#include "jam.h"
uint32_t crc32tab[] = { /* CRC polynomial 0xedb88320 */
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
/***********************************************************************
**
** JAM_Crc32 - Calculates CRC-32
**
***********************************************************************/
uint32_t JAM_Crc32(char *Buffer_PC, uint32_t Length_I)
{
uint32_t Crc_I;
uint32_t c;
Crc_I=0xffffffff;
for(c=0;c < Length_I; c++)
Crc_I=(Crc_I>>8) ^ crc32tab[(char)Crc_I ^ tolower(Buffer_PC[c])];
return(Crc_I);
}

326
jamlib/jam.h Normal file
View File

@@ -0,0 +1,326 @@
/*
JAMLIB - A JAM subroutine library
Copyright (C) 1999 Bj<42>rn Stenberg
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2000-04-16:
- Added #defines for JAM_NO_MESSAGE and JAM_CORRUPT_MSG
- Added #ifndef linux around typedefs for uint16_t and uint32_t
- Added prototype for JAM_AddEmptyMessage()
Backported changes from JAMLIB 1.4.7 made by Johan Billing 2003-10-26
- Added prototype for JAM_DeleteMessage()
*/
/***********************************************************************
**
** JAM Definitions
**
***********************************************************************/
#ifndef __JAM_H__
#define __JAM_H__
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
/*
** Error codes
*/
#define JAM_BAD_PARAM 1 /* one or more parameters are wrong */
#define JAM_IO_ERROR 2 /* i/o error. check JAM_Errno() for details */
#define JAM_LOCK_FAILED 3 /* lock could not be set */
#define JAM_NOT_LOCKED 4 /* the message base was not locked before writing */
#define JAM_NO_MEMORY 5 /* out of memory! */
#define JAM_NO_USER 6 /* user not found */
#define JAM_NO_MESSAGE 7 /* message has been deleted */
#define JAM_CORRUPT_MSG 8 /* message header is corrupt */
/*
** CRC definitions
*/
#define JAM_NO_CRC 0xffffffff
/*
** File extensions
*/
#define EXT_HDRFILE ".jhr"
#define EXT_TXTFILE ".jdt"
#define EXT_IDXFILE ".jdx"
#define EXT_LRDFILE ".jlr"
/*
** Revision level and header signature
*/
#define CURRENTREVLEV 1
#define HEADERSIGNATURE "JAM\0"
/*
** Header file information block, stored first in all .JHR files
*/
typedef struct {
char Signature[4]; /* <J><A><M> followed by <NUL> */
uint32_t DateCreated; /* Creation date */
uint32_t ModCounter; /* Last processed counter */
uint32_t ActiveMsgs; /* Number of active (not deleted) msgs */
uint32_t PasswordCRC; /* CRC-32 of password to access */
uint32_t BaseMsgNum; /* Lowest message number in index file */
char RSRVD[1000]; /* Reserved space */
} s_JamBaseHeader;
/*
** Message status bits
*/
#define MSG_LOCAL 0x00000001L /* Msg created locally */
#define MSG_INTRANSIT 0x00000002L /* Msg is in-transit */
#define MSG_PRIVATE 0x00000004L /* Private */
#define MSG_READ 0x00000008L /* Read by addressee */
#define MSG_SENT 0x00000010L /* Sent to remote */
#define MSG_KILLSENT 0x00000020L /* Kill when sent */
#define MSG_ARCHIVESENT 0x00000040L /* Archive when sent */
#define MSG_HOLD 0x00000080L /* Hold for pick-up */
#define MSG_CRASH 0x00000100L /* Crash */
#define MSG_IMMEDIATE 0x00000200L /* Send Msg now, ignore restrictions */
#define MSG_DIRECT 0x00000400L /* Send directly to destination */
#define MSG_GATE 0x00000800L /* Send via gateway */
#define MSG_FILEREQUEST 0x00001000L /* File request */
#define MSG_FILEATTACH 0x00002000L /* File(s) attached to Msg */
#define MSG_TRUNCFILE 0x00004000L /* Truncate file(s) when sent */
#define MSG_KILLFILE 0x00008000L /* Delete file(s) when sent */
#define MSG_RECEIPTREQ 0x00010000L /* Return receipt requested */
#define MSG_CONFIRMREQ 0x00020000L /* Confirmation receipt requested */
#define MSG_ORPHAN 0x00040000L /* Unknown destination */
#define MSG_ENCRYPT 0x00080000L /* Msg text is encrypted */
#define MSG_COMPRESS 0x00100000L /* Msg text is compressed */
#define MSG_ESCAPED 0x00200000L /* Msg text is seven bit ASCII */
#define MSG_FPU 0x00400000L /* Force pickup */
#define MSG_TYPELOCAL 0x00800000L /* Msg is for local use only (no export) */
#define MSG_TYPEECHO 0x01000000L /* Msg is for conference distribution */
#define MSG_TYPENET 0x02000000L /* Msg is direct network mail */
#define MSG_NODISP 0x20000000L /* Msg may not be displayed to user */
#define MSG_LOCKED 0x40000000L /* Msg is locked, no editing possible */
#define MSG_DELETED 0x80000000L /* Msg is deleted */
/*
** Message header
*/
typedef struct {
char Signature[4]; /* <J><A><M> followed by <NUL> */
uint16_t Revision; /* CURRENTREVLEV */
uint16_t ReservedWord; /* Reserved */
uint32_t SubfieldLen; /* Length of Subfields */
uint32_t TimesRead; /* Number of times message read */
uint32_t MsgIdCRC; /* CRC-32 of MSGID line */
uint32_t ReplyCRC; /* CRC-32 of REPLY line */
uint32_t ReplyTo; /* This msg is a reply to.. */
uint32_t Reply1st; /* First reply to this msg */
uint32_t ReplyNext; /* Next msg in reply chain */
uint32_t DateWritten; /* When msg was written */
uint32_t DateReceived; /* When msg was received/read */
uint32_t DateProcessed; /* When msg was processed by packer */
uint32_t MsgNum; /* Message number (1-based) */
uint32_t Attribute; /* Msg attribute, see "Status bits" */
uint32_t Attribute2; /* Reserved for future use */
uint32_t TxtOffset; /* Offset of text in text file */
uint32_t TxtLen; /* Length of message text */
uint32_t PasswordCRC; /* CRC-32 of password to access msg */
uint32_t Cost; /* Cost of message */
} s_JamMsgHeader;
/*
** Message header Subfield types
*/
#define JAMSFLD_OADDRESS 0
#define JAMSFLD_DADDRESS 1
#define JAMSFLD_SENDERNAME 2
#define JAMSFLD_RECVRNAME 3
#define JAMSFLD_MSGID 4
#define JAMSFLD_REPLYID 5
#define JAMSFLD_SUBJECT 6
#define JAMSFLD_PID 7
#define JAMSFLD_TRACE 8
#define JAMSFLD_ENCLFILE 9
#define JAMSFLD_ENCLFWALIAS 10
#define JAMSFLD_ENCLFREQ 11
#define JAMSFLD_ENCLFILEWC 12
#define JAMSFLD_ENCLINDFILE 13
#define JAMSFLD_EMBINDAT 1000
#define JAMSFLD_FTSKLUDGE 2000
#define JAMSFLD_SEENBY2D 2001
#define JAMSFLD_PATH2D 2002
#define JAMSFLD_FLAGS 2003
#define JAMSFLD_TZUTCINFO 2004
#define JAMSFLD_UNKNOWN 0xffff
/*
** Message header Subfield
*/
typedef struct {
uint16_t LoID; /* Field ID, 0 - 0xffff */
uint16_t HiID; /* Reserved for future use */
uint32_t DatLen; /* Length of buffer that follows */
char* Buffer; /* DatLen bytes of data */
} s_JamSubfield;
typedef struct {
uint16_t LoID; /* Field ID, 0 - 0xffff */
uint16_t HiID; /* Reserved for future use */
uint32_t DatLen; /* Length of buffer that follows */
} s_JamSaveSubfield;
/*
** Message index record
*/
typedef struct {
uint32_t UserCRC; /* CRC-32 of destination username */
uint32_t HdrOffset; /* Offset of header in .JHR file */
} s_JamIndex;
/*
** Lastread structure, one per user
*/
typedef struct {
uint32_t UserCRC; /* CRC-32 of user name (lowercase) */
uint32_t UserID; /* Unique UserID */
uint32_t LastReadMsg; /* Last read message number */
uint32_t HighReadMsg; /* Highest read message number */
} s_JamLastRead;
/*
** JAMLIB message base handle
*/
typedef struct {
FILE* HdrFile_PS; /* File handle for .JHR file */
FILE* TxtFile_PS; /* File handle for .JDT file */
FILE* IdxFile_PS; /* File handle for .JDX file */
FILE* LrdFile_PS; /* File handle for .JLR file */
int Errno_I; /* last i/o error */
int Locked_I; /* is area locked? */
uint32_t LastUserPos_I; /* last position of lastread record */
uint32_t LastUserId_I; /* userid for the last read lastread record */
} s_JamBase;
/*
** JAMLIB subfield packet
*/
typedef struct {
s_JamSubfield** Fields;
uint32_t NumFields;
uint32_t NumAlloc;
} s_JamSubPacket;
/*
** JAMLIB function declarations
*/
/* mbase.c */
int JAM_OpenMB ( char* Basename_PC,
s_JamBase** NewArea_PPS );
int JAM_CloseMB ( s_JamBase* Area_PS );
int JAM_CreateMB ( char* Basename_PC,
uint32_t BaseMsg_I,
s_JamBase** NewArea_PPS );
int JAM_RemoveMB ( s_JamBase* Area_PS,
char* Basename_PC );
int JAM_LockMB ( s_JamBase* Area_PS,
int Timeout_I );
int JAM_UnlockMB ( s_JamBase* Area_PS );
int JAM_ReadMBHeader ( s_JamBase* Area_PS,
s_JamBaseHeader* Header_PS );
int JAM_WriteMBHeader ( s_JamBase* Area_PS,
s_JamBaseHeader* Header_PS );
int JAM_FindUser ( s_JamBase* Area_PS,
uint32_t UserCrc_I,
uint32_t StartMsg_I,
uint32_t* MsgNo_PI );
int JAM_GetMBSize ( s_JamBase* Area_PS,
uint32_t* Messages_PI );
/* message.c */
int JAM_ReadMsgHeader ( s_JamBase* Area_PS,
uint32_t MsgNo_I,
s_JamMsgHeader* Header_PS,
s_JamSubPacket** SubfieldPack_PPS );
int JAM_ReadMsgText ( s_JamBase* Area_PS,
uint32_t Offset_I,
uint32_t Length_I,
char* Buffer_PC );
int JAM_AddMessage ( s_JamBase* Area_PS,
s_JamMsgHeader* Header_PS,
s_JamSubPacket* SubPack_PS,
char* Text_PC,
uint32_t TextLen_I );
int JAM_AddEmptyMessage ( s_JamBase* Area_PS );
int JAM_DeleteMessage ( s_JamBase* Base_PS,
uint32_t MsgNo_I );
int JAM_ChangeMsgHeader ( s_JamBase* Area_PS,
uint32_t MsgNo_I,
s_JamMsgHeader* Header_PS );
int JAM_ClearMsgHeader ( s_JamMsgHeader* Header_PS );
int JAM_Errno ( s_JamBase* Area_PS );
/* lastread.c */
int JAM_ReadLastRead ( s_JamBase* Area_PS,
uint32_t User_I,
s_JamLastRead* Record_PS );
int JAM_WriteLastRead ( s_JamBase* Area_PS,
uint32_t User_I,
s_JamLastRead* Record_PS );
/* subpacket.c */
s_JamSubPacket* JAM_NewSubPacket ( void );
int JAM_DelSubPacket ( s_JamSubPacket* SubPack_PS );
s_JamSubfield* JAM_GetSubfield ( s_JamSubPacket* SubPack_PS );
s_JamSubfield* JAM_GetSubfield_R ( s_JamSubPacket* SubPack_PS ,
uint32_t* Count_PI);
int JAM_PutSubfield ( s_JamSubPacket* SubPack_PS,
s_JamSubfield* Field_PS );
/* crc32.c */
uint32_t JAM_Crc32 ( char* Buffer_PC, uint32_t Length_I );
#endif

1420
jamlib/jamlib.doc Normal file

File diff suppressed because it is too large Load Diff

174
jamlib/lastread.c Normal file
View File

@@ -0,0 +1,174 @@
/*
JAMLIB - A JAM subroutine library
Copyright (C) 1999 Bj<42>rn Stenberg
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2000-04-16:
- Changed source to use feof() instead of errno == EPASTEOF
- Changed source to use structrw to read and write structures
*/
/***********************************************************************
**
** LASTREAD.C -- Lastread pointer handling
**
** Author: Bj<42>rn Stenberg (bjorn.stenberg@sth.frontec.se)
**
***********************************************************************/
#include <stdio.h>
#include <errno.h>
#include "jam.h"
#include "structrw.h"
/***********************************************************************
**
** File global variables
**
***********************************************************************/
/***********************************************************************
**
** JAM_ReadLastRead - Read LastRead record
**
***********************************************************************/
int JAM_ReadLastRead( s_JamBase* Base_PS,
uint32_t User_I,
s_JamLastRead* Record_PS )
{
s_JamLastRead Record_S;
int Pos_I;
if (!Record_PS)
return JAM_BAD_PARAM;
if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
for ( Pos_I = 0; ; Pos_I++ ) {
if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
if ( feof(Base_PS->LrdFile_PS) )
return JAM_NO_USER;
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( Record_S.UserID == User_I ) {
Base_PS->LastUserPos_I = Pos_I;
Base_PS->LastUserId_I = User_I;
*Record_PS = Record_S;
return 0;
}
}
return 0;
}
/***********************************************************************
**
** JAM_WriteLastRead - Write LastRead record
**
***********************************************************************/
int JAM_WriteLastRead( s_JamBase* Base_PS,
uint32_t User_I,
s_JamLastRead* Record_PS )
{
s_JamLastRead Record_S;
int Pos_I;
if (!Record_PS)
return JAM_BAD_PARAM;
/* if the last read is stored */
if ( User_I == Base_PS->LastUserId_I ) {
Pos_I = Base_PS->LastUserPos_I * sizeof( s_JamLastRead );
if ( fseek( Base_PS->LrdFile_PS, Pos_I, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* be safe, check it */
if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* is it where we expected it to be? */
if ( User_I == Record_S.UserID ) {
if ( fseek( Base_PS->LrdFile_PS, Pos_I, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( 1 > fwritejamlastread(Base_PS->LrdFile_PS,Record_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
fflush(Base_PS -> LrdFile_PS);
return 0;
}
}
/* no last position, or position incorrect */
if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
for ( Pos_I = 0; ; Pos_I++ ) {
if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
if ( feof(Base_PS->LrdFile_PS) ) {
/* user not in file, append a new record */
if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
break;
}
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* found the user? */
if ( Record_S.UserID == User_I ) {
if ( fseek( Base_PS->LrdFile_PS, Pos_I * sizeof(s_JamLastRead),
SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
break;
}
}
if ( 1 > fwritejamlastread(Base_PS->LrdFile_PS,Record_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
fflush( Base_PS->LrdFile_PS );
return 0;
}

591
jamlib/mbase.c Normal file
View File

@@ -0,0 +1,591 @@
/*
JAMLIB - A JAM subroutine library
Copyright (C) 1999 Bj<42>rn Stenberg
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2000-04-16:
- Added support for Win32 and Linux
- Changed JAM_OpenMB to open files in binary mode
- Changed source to use feof() instead of errno == EPASTEOF
- Changed source to use structrw to read and write structures
- Fixed broken JAM_FindUser()
- #includes string.h and stdlib.h instead of memory.h
Backported changes from JAMLIB 1.4.7 made by Johan Billing 2003-10-26
- Now uses calloc instead of malloc/memset
- (*NewArea_PPS) will be set to zero even if calloc() failed in
JAM_OpenMB() and JAM_CreateMB()
- JAM_CreateMB() no longer attempts to forever to lock the newly created
messagebase. If the first attempt fails, it will return an error.
- jam_Lock() now sets Base_PS->Errno under Linux
Other changes made by Johan Billing 2003-10-26
- Fixed comparison between signed and unsigned variable in JAM_GetMBSize()
- JAM_CreateMB() would not unlock and close the newly created messagebase
upon failure.
Changes made by Johan Billing 2004-07-10
- Updated the Win32-specific parts of the code to make it compatible with
newer versions of MinGW (tested with 3.1.0-1):
* Now uses Sleep() instead of sleep()
* Changed _LK_UNLOCK to _LK_UNLCK in jam_Lock()
*/
/***********************************************************************
**
** MBASE.C -- Message base handling
**
** Author: Bj<42>rn Stenberg (bjorn.stenberg@sth.frontec.se)
**
***********************************************************************/
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "jam.h"
#include "structrw.h"
#if defined( __OS2__ )
#include <os2.h> /* ANSI C does not include file locking :-( */
#endif
#if defined( __WIN32__ )
#include <sys/locking.h>
#include <io.h>
#include <windows.h>
#if !defined( _LK_UNLCK ) && defined ( _LK_UNLOCK )
#define _LK_UNLCK _LK_UNLOCK /* For backwards compatibility */
#endif
#endif
#if defined( __LINUX__ )
#include <sys/file.h>
#include <unistd.h>
#endif
#define OS_ERROR_OFFSET 10000
#if defined( __OS2__ )
#define JAM_Sleep( _x_ ) DosSleep( _x_*1000 )
#endif
#if defined( __WIN32__ )
#define JAM_Sleep(x) Sleep(x*1000)
#endif
#if defined( __LINUX__ )
#define JAM_Sleep(x) sleep(x)
#endif
/*************************************<**********************************
**
** File-global functions
**
***********************************************************************/
int jam_Open( s_JamBase* Base_PS, char* Filename_PC, char* Mode_PC );
int jam_Lock( s_JamBase* Base_PS, int DoLock_I );
/***********************************************************************
**
** JAM_OpenMB - Open message base
**
***********************************************************************/
int JAM_OpenMB( char* Basename_PC, s_JamBase** NewArea_PPS )
{
s_JamBase* Base_PS;
int Status_I;
if ( !NewArea_PPS )
return JAM_BAD_PARAM;
*NewArea_PPS = NULL;
Base_PS = (s_JamBase*) calloc( 1, sizeof( s_JamBase ) );
if (!Base_PS)
return JAM_NO_MEMORY;
*NewArea_PPS = Base_PS;
Status_I = jam_Open( Base_PS, Basename_PC, "r+b" );
if ( Status_I ) {
return Status_I;
}
return 0;
}
/***********************************************************************
**
** JAM_CreateMB - Create a new message base
**
***********************************************************************/
int JAM_CreateMB( char* Basename_PC,
uint32_t BaseMsg_I,
s_JamBase** NewArea_PPS )
{
s_JamBaseHeader Base_S;
int Status_I;
s_JamBase* Base_PS;
if ( !NewArea_PPS || !BaseMsg_I )
return JAM_BAD_PARAM;
*NewArea_PPS = NULL;
Base_PS = (s_JamBase*) calloc( 1, sizeof( s_JamBase ) );
if (!Base_PS)
return JAM_NO_MEMORY;
*NewArea_PPS = Base_PS;
Status_I = jam_Open( Base_PS, Basename_PC, "w+b" );
if ( Status_I )
return Status_I;
Base_S.DateCreated = time(NULL);
Base_S.ModCounter = 0;
Base_S.ActiveMsgs = 0;
Base_S.PasswordCRC = 0xffffffff;
Base_S.BaseMsgNum = BaseMsg_I;
memset( &Base_S.RSRVD, 0, sizeof( Base_S.RSRVD ) );
Status_I = JAM_LockMB( Base_PS, 0 ); /* If the new base cannot be locked directly, something is seriously wrong */
if ( Status_I ) {
JAM_CloseMB(Base_PS);
return Status_I;
}
Status_I = JAM_WriteMBHeader( Base_PS, &Base_S );
if ( Status_I ) {
JAM_UnlockMB( Base_PS );
JAM_CloseMB(Base_PS);
return Status_I;
}
JAM_UnlockMB( Base_PS );
return 0;
}
/***********************************************************************
**
** JAM_CloseMB - Close message base
**
***********************************************************************/
int JAM_CloseMB( s_JamBase* Base_PS )
{
if ( Base_PS->Locked_I ) {
int Status_I = JAM_UnlockMB( Base_PS );
if ( Status_I )
return Status_I;
}
if ( Base_PS->HdrFile_PS ) {
fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
fclose( Base_PS->IdxFile_PS ); Base_PS->IdxFile_PS = NULL;
fclose( Base_PS->LrdFile_PS ); Base_PS->LrdFile_PS = NULL;
}
Base_PS->Locked_I = 0;
return 0;
}
/***********************************************************************
**
** JAM_RemoveMB - Remove a message base
**
***********************************************************************/
int JAM_RemoveMB( s_JamBase* Base_PS, char* Basename_PC )
{
char Filename_AC[250];
int Status_AI[4];
/* .JHR file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_HDRFILE );
Status_AI[0] = remove( Filename_AC );
if ( Status_AI[0] )
Base_PS->Errno_I = errno;
/* .JDT file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_TXTFILE );
Status_AI[1] = remove( Filename_AC );
if ( Status_AI[1] )
Base_PS->Errno_I = errno;
/* .JDX file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_IDXFILE );
Status_AI[2] = remove( Filename_AC );
if ( Status_AI[2] )
Base_PS->Errno_I = errno;
/* .JLR file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_LRDFILE );
Status_AI[3] = remove( Filename_AC );
if ( Status_AI[3] )
Base_PS->Errno_I = errno;
if (Status_AI[0] || Status_AI[1] || Status_AI[2] || Status_AI[3])
return JAM_IO_ERROR;
return 0;
}
/***********************************************************************
**
** JAM_GetMBSize - Get the number of messages in message base
**
***********************************************************************/
int JAM_GetMBSize( s_JamBase* Base_PS, uint32_t* Messages_PI )
{
long Offset_I;
/* go to end of index file */
if ( fseek( Base_PS->IdxFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
Offset_I = ftell( Base_PS->IdxFile_PS );
if ( Offset_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
*Messages_PI = Offset_I / sizeof( s_JamIndex );
return 0;
}
/***********************************************************************
**
** JAM_LockMB - Lock message base
**
***********************************************************************/
int JAM_LockMB( s_JamBase* Base_PS, int Timeout_I )
{
if ( Base_PS->Locked_I )
return 0;
switch ( Timeout_I )
{
/* unlimited timeout */
case -1:
while ( jam_Lock( Base_PS, 1 ) == JAM_LOCK_FAILED )
JAM_Sleep( 1 );
return 0;
/* no timeout */
case 0:
return jam_Lock( Base_PS, 1 );
/* X seconds timeout */
default:
{
time_t Time_I = time(NULL) + Timeout_I;
while ( time(NULL) < Time_I )
{
int Result_I;
Result_I = jam_Lock( Base_PS, 1 );
if ( Result_I == JAM_LOCK_FAILED )
JAM_Sleep( 1 );
else
return Result_I;
}
return JAM_LOCK_FAILED;
}
}
}
/***********************************************************************
**
** JAM_UnlockMB - Flush all writes and unlock message base
**
***********************************************************************/
int JAM_UnlockMB( s_JamBase* Base_PS )
{
fflush( Base_PS->HdrFile_PS );
fflush( Base_PS->TxtFile_PS );
fflush( Base_PS->IdxFile_PS );
fflush( Base_PS->LrdFile_PS );
return jam_Lock( Base_PS, 0 );
}
/***********************************************************************
**
** JAM_ReadMBHeader - Read message base header
**
***********************************************************************/
int JAM_ReadMBHeader( s_JamBase* Base_PS, s_JamBaseHeader* Header_PS )
{
if ( !Header_PS || !Base_PS )
return JAM_BAD_PARAM;
if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( 1 > freadjambaseheader(Base_PS->HdrFile_PS,Header_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
return 0;
}
/***********************************************************************
**
** JAM_WriteMBHeader - Write message base header
**
***********************************************************************/
int JAM_WriteMBHeader( s_JamBase* Base_PS, s_JamBaseHeader* Header_PS )
{
if ( !Header_PS || !Base_PS )
return JAM_BAD_PARAM;
if ( !Base_PS->Locked_I )
return JAM_NOT_LOCKED;
if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* ensure header looks right */
memcpy( Header_PS->Signature, HEADERSIGNATURE, 4 );
Header_PS->ModCounter++;
if ( 1 > fwritejambaseheader(Base_PS->HdrFile_PS,Header_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
fflush( Base_PS->HdrFile_PS );
return 0;
}
/***********************************************************************
**
** JAM_FindUser - Scan scan file for messages to a user
**
***********************************************************************/
int JAM_FindUser( s_JamBase* Base_PS,
uint32_t UserCrc_I,
uint32_t StartMsg_I,
uint32_t* MsgNo_PI )
{
uint32_t MsgNo_I;
/* go to start message */
if ( fseek( Base_PS->IdxFile_PS, StartMsg_I * sizeof( s_JamIndex ),
SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* scan file */
for ( MsgNo_I = StartMsg_I; ; MsgNo_I++ ) {
s_JamIndex Index_S;
if ( 1 > freadjamindex(Base_PS->IdxFile_PS,&Index_S) ) {
if ( feof(Base_PS->IdxFile_PS) )
return JAM_NO_USER;
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( Index_S.UserCRC == UserCrc_I )
break;
}
*MsgNo_PI = MsgNo_I;
return 0;
}
/***********************************************************************
**
** jam_Lock - Lock/unlock a message base
**
***********************************************************************/
int jam_Lock( s_JamBase* Base_PS, int DoLock_I )
{
#if defined(__OS2__)
FILELOCK Area_S;
APIRET Status_I;
ULONG Timeout_I = 0;
int Handle_I;
Handle_I = fileno( Base_PS->HdrFile_PS );
if ( Handle_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
Area_S.lOffset = 0;
Area_S.lRange = 1;
if ( DoLock_I )
Status_I = DosSetFileLocks( Handle_I, NULL, &Area_S, Timeout_I, 0 );
else
Status_I = DosSetFileLocks( Handle_I, &Area_S, NULL, Timeout_I, 0 );
if ( Status_I ) {
if ( 232 == Status_I )
return JAM_LOCK_FAILED;
Base_PS->Errno_I = Status_I + OS_ERROR_OFFSET;
return JAM_IO_ERROR;
}
if ( DoLock_I )
Base_PS->Locked_I = 1;
else
Base_PS->Locked_I = 0;
return 0;
#elif defined(__WIN32__)
int Handle_I,Status_I;
fseek(Base_PS->HdrFile_PS,0,SEEK_SET); /* Lock from start of file */
Handle_I = fileno( Base_PS->HdrFile_PS );
if ( Handle_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( DoLock_I )
Status_I = _locking(Handle_I,_LK_NBLCK,1);
else
Status_I = _locking(Handle_I,_LK_UNLCK,1);
if ( Status_I )
return JAM_LOCK_FAILED;
if ( DoLock_I )
Base_PS->Locked_I = 1;
else
Base_PS->Locked_I = 0;
return 0;
#elif defined(__LINUX__)
int Handle_I,Status_I;
struct flock fl;
fseek(Base_PS->HdrFile_PS,0,SEEK_SET); /* Lock from start of file */
Handle_I = fileno( Base_PS->HdrFile_PS );
if ( Handle_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if(DoLock_I) fl.l_type=F_WRLCK;
else fl.l_type=F_UNLCK;
fl.l_whence=SEEK_SET;
fl.l_start=0;
fl.l_len=1;
fl.l_pid=getpid();
Status_I=fcntl(Handle_I,F_SETLK,&fl);
if ( Status_I ) {
Base_PS->Errno_I = errno;
return JAM_LOCK_FAILED;
}
if ( DoLock_I )
Base_PS->Locked_I = 1;
else
Base_PS->Locked_I = 0;
return 0;
#else
#error Unsupported platform
#endif
}
/***********************************************************************
**
** jam_Open - Open/create message base files
**
***********************************************************************/
int jam_Open( s_JamBase* Base_PS, char* Basename_PC, char* Mode_PC )
{
char Filename_AC[250];
/* .JHR file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_HDRFILE );
Base_PS->HdrFile_PS = fopen( Filename_AC, Mode_PC );
if (!Base_PS->HdrFile_PS) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* .JDT file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_TXTFILE );
Base_PS->TxtFile_PS = fopen( Filename_AC, Mode_PC );
if (!Base_PS->TxtFile_PS) {
Base_PS->Errno_I = errno;
fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
return JAM_IO_ERROR;
}
/* .JDX file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_IDXFILE );
Base_PS->IdxFile_PS = fopen( Filename_AC, Mode_PC );
if (!Base_PS->IdxFile_PS) {
Base_PS->Errno_I = errno;
fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
return JAM_IO_ERROR;
}
/* .JLR file */
sprintf( Filename_AC, "%s%s", Basename_PC, EXT_LRDFILE );
Base_PS->LrdFile_PS = fopen( Filename_AC, Mode_PC );
if (!Base_PS->LrdFile_PS) {
Base_PS->Errno_I = errno;
fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
fclose( Base_PS->IdxFile_PS ); Base_PS->IdxFile_PS = NULL;
return JAM_IO_ERROR;
}
return 0;
}

625
jamlib/message.c Normal file
View File

@@ -0,0 +1,625 @@
/*
JAMLIB - A JAM subroutine library
Copyright (C) 1999 Bj<42>rn Stenberg
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2000-04-16:
- Changed source to use feof() instead of errno == EPASTEOF
- Changed source to use structrw to read and write structures
- Added JAM_AddEmptyMessage()
- Added error messages JAM_NO_MESSAGE and JAM_CORRUPT_MSG for
JAM_ReadMsgHeader()
- #includes stdlib.h instead of malloc.h and memory.h
- Fixed a bug that caused JAM_AddMessage() to fail when trying to
add an empty message to the messagebase under Linux.
Backported changes from JAMLIB 1.4.7 made by Johan Billing 2003-10-26
- Fixed memory leaks that would occur if JAM_ReadMsgHeader() failed
- Added JAM_DeleteMessage()
Other changes made by Johan Billing 2003-10-26
- Fixed comparison between signed and unsigned variable in JAM_AddMessage()
- Improved handling of ActiveMsgs counter. JAM_AddMessage() now only
increases ActiveMsgs if the added message does not have MSG_DELETED set.
JAM_ChangeMsgHeader() decreases ActiveMsgs if MSG_DELETED is set and the
message wasn't already deleted. JAM_DeleteMessage() now only decreases
ActiveMsgs if the message wasn't already deleted.
*/
/***********************************************************************
**
** Message.C -- Message handling
**
** Author: Bj<42>rn Stenberg (bjorn.stenberg@sth.frontec.se)
**
***********************************************************************/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "jam.h"
#include "structrw.h"
/***********************************************************************
**
** JAM_ReadMsgHeader - Read message header
**
***********************************************************************/
int JAM_ReadMsgHeader( s_JamBase* Base_PS,
uint32_t MsgNo_I,
s_JamMsgHeader* Header_PS,
s_JamSubPacket** SubfieldPack_PPS )
{
s_JamIndex Index_S;
if ( !Base_PS || !Header_PS )
return JAM_BAD_PARAM;
/* find index record */
if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ), SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* read index record */
if ( 1 > freadjamindex(Base_PS->IdxFile_PS,&Index_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* message is not there */
if(Index_S.HdrOffset == 0xffffffff && Index_S.UserCRC == 0xffffffff)
{
return JAM_NO_MESSAGE;
}
/* find header */
if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* read header */
if ( 1 > freadjammsgheader(Base_PS->HdrFile_PS,Header_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* are Subfields requested? */
if ( SubfieldPack_PPS && Header_PS->SubfieldLen ) {
s_JamSubPacket* SubPacket_PS;
s_JamSubfield Subfield_S;
char* Buf_PC;
char* Ptr_PC;
char* Roof_PC;
int BufSize_I = Header_PS->SubfieldLen;
Buf_PC = (void*) malloc( BufSize_I );
if ( !Buf_PC )
return JAM_NO_MEMORY;
/* read all subfields */
if ( 1 > fread( Buf_PC, BufSize_I, 1, Base_PS->HdrFile_PS ) ) {
Base_PS->Errno_I = errno;
free (Buf_PC);
return JAM_IO_ERROR;
}
SubPacket_PS = JAM_NewSubPacket();
if ( !SubPacket_PS ) {
free (Buf_PC);
return JAM_NO_MEMORY;
}
Roof_PC = Buf_PC + BufSize_I;
/* cut out the subfields */
for ( Ptr_PC = Buf_PC;
Ptr_PC < Roof_PC;
Ptr_PC += Subfield_S.DatLen + SIZE_JAMSAVESUBFIELD ) {
int Status_I;
getjamsubfield(Ptr_PC,&Subfield_S);
if((char *)Subfield_S.Buffer + Subfield_S.DatLen > Roof_PC) {
JAM_DelSubPacket( SubPacket_PS );
free (Buf_PC);
return JAM_CORRUPT_MSG;
}
Status_I = JAM_PutSubfield( SubPacket_PS, &Subfield_S );
if ( Status_I ) {
JAM_DelSubPacket( SubPacket_PS );
free (Buf_PC);
return Status_I;
}
}
free( Buf_PC );
*SubfieldPack_PPS = SubPacket_PS;
}
else
if ( SubfieldPack_PPS )
/* fields requested but none found */
/* return an empty packet */
*SubfieldPack_PPS = JAM_NewSubPacket();
return 0;
}
/***********************************************************************
**
** JAM_ReadMsgText - Read message text
**
***********************************************************************/
int JAM_ReadMsgText( s_JamBase* Base_PS,
uint32_t Offset_I,
uint32_t Length_I,
char* Buffer_PC )
{
if ( !Base_PS || !Buffer_PC )
return JAM_BAD_PARAM;
if ( !Length_I )
return 0;
if ( fseek( Base_PS->TxtFile_PS, Offset_I, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( 1 > fread( Buffer_PC, Length_I, 1, Base_PS->TxtFile_PS ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
return 0;
}
/***********************************************************************
**
** JAM_ChangeMsgHeader - Change a message header
**
***********************************************************************/
int JAM_ChangeMsgHeader( s_JamBase* Base_PS,
uint32_t MsgNo_I,
s_JamMsgHeader* Header_PS )
{
s_JamBaseHeader BaseHeader_S;
s_JamMsgHeader OldHeader_S;
s_JamIndex Index_S;
int Status_I;
if ( !Base_PS )
return JAM_BAD_PARAM;
if ( !Base_PS->Locked_I )
return JAM_NOT_LOCKED;
/* read message base header */
Status_I = JAM_ReadMBHeader( Base_PS, &BaseHeader_S );
if ( Status_I )
return Status_I;
/* find index record */
if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ),
SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* read index record */
if ( 1 > freadjamindex(Base_PS->IdxFile_PS,&Index_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* find header */
if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* read old message header */
if ( 1 > freadjammsgheader( Base_PS->HdrFile_PS, &OldHeader_S ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* find header */
if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* write header */
if ( 1 > fwritejammsgheader(Base_PS->HdrFile_PS,Header_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if( ( Header_PS->Attribute & MSG_DELETED ) && !(OldHeader_S.Attribute & MSG_DELETED) ) {
/* message is deleted now but wasn't before */
BaseHeader_S.ActiveMsgs--;
}
Status_I = JAM_WriteMBHeader( Base_PS, &BaseHeader_S );
if ( Status_I )
return Status_I;
return 0;
}
/***********************************************************************
**
** JAM_AddMessage - Add a message to a message base
**
***********************************************************************/
int JAM_AddMessage( s_JamBase* Base_PS,
s_JamMsgHeader* Header_PS,
s_JamSubPacket* SubPack_PS,
char* Text_PC,
uint32_t TextLen_I )
{
s_JamBaseHeader BaseHeader_S;
s_JamIndex Index_S;
long Offset_I;
int Status_I;
uint32_t TotLen_I;
if ( !Base_PS )
return JAM_BAD_PARAM;
if ( !Base_PS->Locked_I )
return JAM_NOT_LOCKED;
/* read message base header */
Status_I = JAM_ReadMBHeader( Base_PS, &BaseHeader_S );
if ( Status_I )
return Status_I;
/*
** Add text if any
*/
Header_PS->TxtOffset = 0;
Header_PS->TxtLen = 0;
if(Text_PC && TextLen_I!=0)
{
/* go to end of text file */
if ( fseek( Base_PS->TxtFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* store text offset (for header) */
Offset_I = ftell( Base_PS->TxtFile_PS );
if ( Offset_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
Header_PS->TxtOffset = Offset_I;
Header_PS->TxtLen = TextLen_I;
/* write text */
if ( 1 > fwrite( Text_PC, TextLen_I, 1, Base_PS->TxtFile_PS ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
}
/*
** Add header
*/
/* go to end of header file */
if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* calculate the size of all Subfields */
TotLen_I = 0;
if ( SubPack_PS ) {
s_JamSubfield* Subfield_PS;
for ( Subfield_PS = JAM_GetSubfield( SubPack_PS ); Subfield_PS;
Subfield_PS = JAM_GetSubfield( NULL ) )
TotLen_I += sizeof( s_JamSaveSubfield ) + Subfield_PS->DatLen;
}
Header_PS->SubfieldLen = TotLen_I;
/* go to end of index file */
if ( fseek( Base_PS->IdxFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* find out new message number (for message header) */
Offset_I = ftell( Base_PS->IdxFile_PS );
if ( Offset_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* update header */
Header_PS->MsgNum = Offset_I / sizeof( s_JamIndex ) +
BaseHeader_S.BaseMsgNum;
memcpy( Header_PS->Signature, HEADERSIGNATURE, 4 );
Header_PS->Revision = CURRENTREVLEV;
/* go to end of header file */
if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* find out new header offset (for index record) */
Offset_I = ftell( Base_PS->HdrFile_PS );
if ( Offset_I == -1 ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
Index_S.HdrOffset = Offset_I;
/* write new header */
if ( 1 > fwritejammsgheader(Base_PS->HdrFile_PS,Header_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* write Subfields */
if ( SubPack_PS ) {
s_JamSubfield* Subfield_PS;
char User_AC[101];
/* clear username */
User_AC[0] = 0;
for ( Subfield_PS = JAM_GetSubfield( SubPack_PS ); Subfield_PS;
Subfield_PS = JAM_GetSubfield( NULL ) ) {
/* first, save Subfield header */
if ( 1 > fwritejamsavesubfield(Base_PS->HdrFile_PS,(s_JamSaveSubfield *)Subfield_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* then, save Subfield data if any*/
if(Subfield_PS->DatLen) {
if ( 1 > fwrite( Subfield_PS->Buffer, Subfield_PS->DatLen,
1, Base_PS->HdrFile_PS ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
}
/* store username for index file */
if ( Subfield_PS->LoID == JAMSFLD_RECVRNAME ) {
memcpy( User_AC, Subfield_PS->Buffer, Subfield_PS->DatLen );
User_AC[ Subfield_PS->DatLen ] = 0;
}
}
/* update index record */
if ( User_AC[0] )
Index_S.UserCRC = JAM_Crc32( User_AC, strlen( User_AC ) );
else
Index_S.UserCRC = JAM_NO_CRC;
}
else
/* update index record */
Index_S.UserCRC = JAM_NO_CRC;
/*
** Add index
*/
/* write index record */
if ( 1 > fwritejamindex(Base_PS->IdxFile_PS,&Index_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if(!(Header_PS->Attribute & MSG_DELETED))
BaseHeader_S.ActiveMsgs++; /* Only increase ActiveMsgs if MSG_DELETED not set */
/* write message base header */
Status_I = JAM_WriteMBHeader( Base_PS, &BaseHeader_S );
if ( Status_I )
return Status_I;
return 0;
}
/***********************************************************************
**
** JAM_AddEmptyMessage - Add a empty message entry to a message base
**
***********************************************************************/
int JAM_AddEmptyMessage( s_JamBase* Base_PS)
{
s_JamIndex Index_S;
if ( !Base_PS )
return JAM_BAD_PARAM;
if ( !Base_PS->Locked_I )
return JAM_NOT_LOCKED;
/* go to end of index file */
if ( fseek( Base_PS->IdxFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/*
** Add index
*/
Index_S.HdrOffset = 0xffffffff;
Index_S.UserCRC = 0xffffffff;
/* write index record */
if ( 1 > fwritejamindex(Base_PS->IdxFile_PS,&Index_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
return 0;
}
/***********************************************************************
**
** JAM_DeleteMessage - Delete message from messagebase
**
***********************************************************************/
int JAM_DeleteMessage( s_JamBase* Base_PS,
uint32_t MsgNo_I )
{
s_JamBaseHeader BaseHeader_S;
s_JamMsgHeader Header_S;
s_JamIndex Index_S;
int Status_I;
uint32_t OldAttribute_I;
if ( !Base_PS )
return JAM_BAD_PARAM;
if ( !Base_PS->Locked_I )
return JAM_NOT_LOCKED;
/* read message base header */
Status_I = JAM_ReadMBHeader( Base_PS, &BaseHeader_S );
if ( Status_I )
return Status_I;
/* find index record */
if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ), SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* read index record */
if ( 1 > freadjamindex( Base_PS->IdxFile_PS, &Index_S ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* message is not there */
if(Index_S.HdrOffset == 0xffffffff && Index_S.UserCRC == 0xffffffff)
{
return JAM_NO_MESSAGE;
}
/* find header */
if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* read header */
if ( 1 > freadjammsgheader( Base_PS->HdrFile_PS, &Header_S ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
OldAttribute_I = Header_S.Attribute;
Header_S.Attribute |= MSG_DELETED;
/* find header */
if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* write header */
if ( 1 > fwritejammsgheader( Base_PS->HdrFile_PS, &Header_S ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* find index record */
if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ), SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
Index_S.HdrOffset = 0xffffffff;
Index_S.UserCRC = 0xffffffff;
/* write index record */
if ( 1 > fwritejamindex(Base_PS->IdxFile_PS,&Index_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if(!(OldAttribute_I & MSG_DELETED))
BaseHeader_S.ActiveMsgs--; /* decrease ActiveMsgs if the message wasn't already deleted */
/* write message base header */
Status_I = JAM_WriteMBHeader( Base_PS, &BaseHeader_S );
if ( Status_I )
return Status_I;
return 0;
}
/***********************************************************************
**
** JAM_Errno - Report the latest C library error code
**
***********************************************************************/
int JAM_Errno( s_JamBase* Base_PS )
{
return Base_PS->Errno_I;
}
/***********************************************************************
**
** JAM_ClearMsgHeader - Clear a message header
**
***********************************************************************/
int JAM_ClearMsgHeader( s_JamMsgHeader* Header_PS )
{
if (!Header_PS)
return JAM_BAD_PARAM;
memset( Header_PS, 0, sizeof( s_JamMsgHeader ) );
memcpy( Header_PS->Signature, HEADERSIGNATURE, 4 );
Header_PS->Revision = CURRENTREVLEV;
Header_PS->MsgIdCRC = JAM_NO_CRC;
Header_PS->ReplyCRC = JAM_NO_CRC;
Header_PS->PasswordCRC = JAM_NO_CRC;
return 0;
}

239
jamlib/structrw.c Normal file
View File

@@ -0,0 +1,239 @@
/*
structrw - Platform-independent reading and writing of JAM structs
Copyright (C) 1999 Johan Billing
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2003-10-22
- Added #include <string.h>
*/
#include <stdio.h>
#include <string.h>
#include "jam.h"
#include "structrw.h"
uint16_t jamgetuword(char *buf,uint32_t offset)
{
return (uint16_t) buf[offset]+
buf[offset+1]*256;
}
void jamputuword(char *buf,uint32_t offset,uint16_t num)
{
buf[offset]=num%256;
buf[offset+1]=num/256;
}
void jamputuint32_t(char *buf,uint32_t offset,uint32_t num)
{
buf[offset]=num%256;
buf[offset+1]=(num / 256) % 256;
buf[offset+2]=(num / 256 / 256) % 256;
buf[offset+3]=(num / 256 / 256 / 256) % 256;
}
uint32_t jamgetuint32_t(char *buf,uint32_t offset)
{
return (uint32_t) buf[offset]+
buf[offset+1]*256+
buf[offset+2]*256*256+
buf[offset+3]*256*256*256;
}
int freadjambaseheader(FILE *fp,s_JamBaseHeader *s_JamBaseHeader)
{
char buf[SIZE_JAMBASEHEADER];
if(fread(buf,SIZE_JAMBASEHEADER,1,fp) != 1)
return 0;
memcpy(s_JamBaseHeader->Signature,&buf[JAMBASEHEADER_SIGNATURE],4);
s_JamBaseHeader->DateCreated = jamgetuint32_t(buf,JAMBASEHEADER_DATECREATED);
s_JamBaseHeader->ModCounter = jamgetuint32_t(buf,JAMBASEHEADER_MODCOUNTER);
s_JamBaseHeader->ActiveMsgs = jamgetuint32_t(buf,JAMBASEHEADER_ACTIVEMSGS);
s_JamBaseHeader->PasswordCRC = jamgetuint32_t(buf,JAMBASEHEADER_PASSWORDCRC);
s_JamBaseHeader->BaseMsgNum = jamgetuint32_t(buf,JAMBASEHEADER_BASEMSGNUM);
memcpy(s_JamBaseHeader->RSRVD,&buf[JAMBASEHEADER_RSRVD],1000);
return 1;
}
int fwritejambaseheader(FILE *fp,s_JamBaseHeader *s_JamBaseHeader)
{
char buf[SIZE_JAMBASEHEADER];
memcpy(&buf[JAMBASEHEADER_SIGNATURE],s_JamBaseHeader->Signature,4);
jamputuint32_t(buf,JAMBASEHEADER_DATECREATED, s_JamBaseHeader->DateCreated);
jamputuint32_t(buf,JAMBASEHEADER_MODCOUNTER, s_JamBaseHeader->ModCounter);
jamputuint32_t(buf,JAMBASEHEADER_ACTIVEMSGS, s_JamBaseHeader->ActiveMsgs);
jamputuint32_t(buf,JAMBASEHEADER_PASSWORDCRC, s_JamBaseHeader->PasswordCRC );
jamputuint32_t(buf,JAMBASEHEADER_BASEMSGNUM, s_JamBaseHeader->BaseMsgNum);
memcpy(&buf[JAMBASEHEADER_RSRVD],s_JamBaseHeader->RSRVD,1000);
if(fwrite(buf,SIZE_JAMBASEHEADER,1,fp) != 1)
return 0;
return 1;
}
int freadjammsgheader(FILE *fp,s_JamMsgHeader *s_JamMsgHeader)
{
char buf[SIZE_JAMMSGHEADER];
if(fread(buf,SIZE_JAMMSGHEADER,1,fp) != 1)
return 0;
memcpy(s_JamMsgHeader->Signature,&buf[JAMMSGHEADER_SIGNATURE],4);
s_JamMsgHeader->Revision = jamgetuword(buf,JAMMSGHEADER_REVISION);
s_JamMsgHeader->ReservedWord = jamgetuword(buf,JAMMSGHEADER_RESERVEDWORD);
s_JamMsgHeader->SubfieldLen = jamgetuint32_t(buf,JAMMSGHEADER_SUBFIELDLEN);
s_JamMsgHeader->TimesRead = jamgetuint32_t(buf,JAMMSGHEADER_TIMESREAD);
s_JamMsgHeader->MsgIdCRC = jamgetuint32_t(buf,JAMMSGHEADER_MSGIDCRC);
s_JamMsgHeader->ReplyCRC = jamgetuint32_t(buf,JAMMSGHEADER_REPLYCRC);
s_JamMsgHeader->ReplyTo = jamgetuint32_t(buf,JAMMSGHEADER_REPLYTO);
s_JamMsgHeader->Reply1st = jamgetuint32_t(buf,JAMMSGHEADER_REPLY1ST);
s_JamMsgHeader->ReplyNext = jamgetuint32_t(buf,JAMMSGHEADER_REPLYNEXT);
s_JamMsgHeader->DateWritten = jamgetuint32_t(buf,JAMMSGHEADER_DATEWRITTEN);
s_JamMsgHeader->DateReceived = jamgetuint32_t(buf,JAMMSGHEADER_DATERECEIVED);
s_JamMsgHeader->DateProcessed = jamgetuint32_t(buf,JAMMSGHEADER_DATEPROCESSED);
s_JamMsgHeader->MsgNum = jamgetuint32_t(buf,JAMMSGHEADER_MSGNUM);
s_JamMsgHeader->Attribute = jamgetuint32_t(buf,JAMMSGHEADER_ATTRIBUTE);
s_JamMsgHeader->Attribute2 = jamgetuint32_t(buf,JAMMSGHEADER_ATTRIBUTE2);
s_JamMsgHeader->TxtOffset = jamgetuint32_t(buf,JAMMSGHEADER_TXTOFFSET);
s_JamMsgHeader->TxtLen = jamgetuint32_t(buf,JAMMSGHEADER_TXTLEN);
s_JamMsgHeader->PasswordCRC = jamgetuint32_t(buf,JAMMSGHEADER_PASSWORDCRC);
s_JamMsgHeader->Cost = jamgetuint32_t(buf,JAMMSGHEADER_COST);
return 1;
}
int fwritejammsgheader(FILE *fp,s_JamMsgHeader *s_JamMsgHeader)
{
char buf[SIZE_JAMMSGHEADER];
memcpy(&buf[JAMMSGHEADER_SIGNATURE],s_JamMsgHeader->Signature,4);
jamputuword(buf,JAMMSGHEADER_REVISION, s_JamMsgHeader->Revision);
jamputuword(buf,JAMMSGHEADER_RESERVEDWORD, s_JamMsgHeader->ReservedWord);
jamputuint32_t(buf,JAMMSGHEADER_SUBFIELDLEN, s_JamMsgHeader->SubfieldLen);
jamputuint32_t(buf,JAMMSGHEADER_TIMESREAD, s_JamMsgHeader->TimesRead);
jamputuint32_t(buf,JAMMSGHEADER_MSGIDCRC, s_JamMsgHeader->MsgIdCRC);
jamputuint32_t(buf,JAMMSGHEADER_REPLYCRC, s_JamMsgHeader->ReplyCRC );
jamputuint32_t(buf,JAMMSGHEADER_REPLYTO, s_JamMsgHeader->ReplyTo);
jamputuint32_t(buf,JAMMSGHEADER_REPLY1ST, s_JamMsgHeader->Reply1st);
jamputuint32_t(buf,JAMMSGHEADER_REPLYNEXT, s_JamMsgHeader->ReplyNext);
jamputuint32_t(buf,JAMMSGHEADER_DATEWRITTEN, s_JamMsgHeader->DateWritten);
jamputuint32_t(buf,JAMMSGHEADER_DATERECEIVED, s_JamMsgHeader->DateReceived );
jamputuint32_t(buf,JAMMSGHEADER_DATEPROCESSED, s_JamMsgHeader->DateProcessed);
jamputuint32_t(buf,JAMMSGHEADER_MSGNUM, s_JamMsgHeader->MsgNum);
jamputuint32_t(buf,JAMMSGHEADER_ATTRIBUTE, s_JamMsgHeader->Attribute);
jamputuint32_t(buf,JAMMSGHEADER_ATTRIBUTE2, s_JamMsgHeader->Attribute2);
jamputuint32_t(buf,JAMMSGHEADER_TXTOFFSET, s_JamMsgHeader->TxtOffset);
jamputuint32_t(buf,JAMMSGHEADER_TXTLEN, s_JamMsgHeader->TxtLen);
jamputuint32_t(buf,JAMMSGHEADER_PASSWORDCRC, s_JamMsgHeader->PasswordCRC);
jamputuint32_t(buf,JAMMSGHEADER_COST, s_JamMsgHeader->Cost);
if(fwrite(buf,SIZE_JAMMSGHEADER,1,fp) != 1)
return 0;
return 1;
}
int freadjamindex(FILE *fp,s_JamIndex *s_JamIndex)
{
char buf[SIZE_JAMINDEX];
if(fread(buf,SIZE_JAMINDEX,1,fp) != 1)
return 0;
s_JamIndex->UserCRC = jamgetuint32_t(buf,JAMINDEX_USERCRC);
s_JamIndex->HdrOffset = jamgetuint32_t(buf,JAMINDEX_HDROFFSET);
return 1;
}
int fwritejamindex(FILE *fp,s_JamIndex *s_JamIndex)
{
char buf[SIZE_JAMINDEX];
jamputuint32_t(buf,JAMINDEX_USERCRC, s_JamIndex->UserCRC);
jamputuint32_t(buf,JAMINDEX_HDROFFSET, s_JamIndex->HdrOffset);
if(fwrite(buf,SIZE_JAMINDEX,1,fp) != 1)
return 0;
return 1;
}
int freadjamlastread(FILE *fp,s_JamLastRead *s_JamLastRead)
{
char buf[SIZE_JAMLASTREAD];
if(fread(buf,SIZE_JAMLASTREAD,1,fp) != 1)
return 0;
s_JamLastRead->UserCRC = jamgetuint32_t(buf,JAMLASTREAD_USERCRC);
s_JamLastRead->UserID = jamgetuint32_t(buf,JAMLASTREAD_USERID);
s_JamLastRead->LastReadMsg = jamgetuint32_t(buf,JAMLASTREAD_LASTREADMSG);
s_JamLastRead->HighReadMsg = jamgetuint32_t(buf,JAMLASTREAD_HIGHREADMSG);
return 1;
}
int fwritejamlastread(FILE *fp,s_JamLastRead *s_JamLastRead)
{
char buf[SIZE_JAMLASTREAD];
jamputuint32_t(buf,JAMLASTREAD_USERCRC,s_JamLastRead->UserCRC);
jamputuint32_t(buf,JAMLASTREAD_USERID,s_JamLastRead->UserID);
jamputuint32_t(buf,JAMLASTREAD_LASTREADMSG,s_JamLastRead->LastReadMsg);
jamputuint32_t(buf,JAMLASTREAD_HIGHREADMSG,s_JamLastRead->HighReadMsg);
if(fwrite(buf,SIZE_JAMLASTREAD,1,fp) != 1)
return 0;
return 1;
}
int fwritejamsavesubfield(FILE *fp,s_JamSaveSubfield *s_JamSaveSubfield)
{
char buf[SIZE_JAMLASTREAD];
jamputuword(buf,JAMSAVESUBFIELD_LOID, s_JamSaveSubfield->LoID);
jamputuword(buf,JAMSAVESUBFIELD_HIID, s_JamSaveSubfield->HiID);
jamputuint32_t(buf,JAMSAVESUBFIELD_DATLEN, s_JamSaveSubfield->DatLen);
if(fwrite(buf,SIZE_JAMSAVESUBFIELD,1,fp) != 1)
return 0;
return 1;
}
void getjamsubfield(char *buf,s_JamSubfield *Subfield_S)
{
Subfield_S->LoID = jamgetuword(buf,JAMSAVESUBFIELD_LOID);
Subfield_S->HiID = jamgetuword(buf,JAMSAVESUBFIELD_HIID);
Subfield_S->DatLen = jamgetuint32_t(buf,JAMSAVESUBFIELD_DATLEN);
Subfield_S->Buffer = (char *) buf + SIZE_JAMSAVESUBFIELD;
}

86
jamlib/structrw.h Normal file
View File

@@ -0,0 +1,86 @@
/*
structrw - Platform-independent reading and writing of JAM structs
Copyright (C) 1999 Johan Billing
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define JAMBASEHEADER_SIGNATURE 0
#define JAMBASEHEADER_DATECREATED 4
#define JAMBASEHEADER_MODCOUNTER 8
#define JAMBASEHEADER_ACTIVEMSGS 12
#define JAMBASEHEADER_PASSWORDCRC 16
#define JAMBASEHEADER_BASEMSGNUM 20
#define JAMBASEHEADER_RSRVD 24
#define SIZE_JAMBASEHEADER 1024
#define JAMMSGHEADER_SIGNATURE 0
#define JAMMSGHEADER_REVISION 4
#define JAMMSGHEADER_RESERVEDWORD 6
#define JAMMSGHEADER_SUBFIELDLEN 8
#define JAMMSGHEADER_TIMESREAD 12
#define JAMMSGHEADER_MSGIDCRC 16
#define JAMMSGHEADER_REPLYCRC 20
#define JAMMSGHEADER_REPLYTO 24
#define JAMMSGHEADER_REPLY1ST 28
#define JAMMSGHEADER_REPLYNEXT 32
#define JAMMSGHEADER_DATEWRITTEN 36
#define JAMMSGHEADER_DATERECEIVED 40
#define JAMMSGHEADER_DATEPROCESSED 44
#define JAMMSGHEADER_MSGNUM 48
#define JAMMSGHEADER_ATTRIBUTE 52
#define JAMMSGHEADER_ATTRIBUTE2 56
#define JAMMSGHEADER_TXTOFFSET 60
#define JAMMSGHEADER_TXTLEN 64
#define JAMMSGHEADER_PASSWORDCRC 68
#define JAMMSGHEADER_COST 72
#define SIZE_JAMMSGHEADER 76
#define JAMINDEX_USERCRC 0
#define JAMINDEX_HDROFFSET 4
#define SIZE_JAMINDEX 8
#define JAMLASTREAD_USERCRC 0
#define JAMLASTREAD_USERID 4
#define JAMLASTREAD_LASTREADMSG 8
#define JAMLASTREAD_HIGHREADMSG 12
#define SIZE_JAMLASTREAD 16
#define JAMSAVESUBFIELD_LOID 0
#define JAMSAVESUBFIELD_HIID 2
#define JAMSAVESUBFIELD_DATLEN 4
#define SIZE_JAMSAVESUBFIELD 8
int freadjambaseheader(FILE *fp,s_JamBaseHeader *s_JamBaseHeader);
int fwritejambaseheader(FILE *fp,s_JamBaseHeader *s_JamBaseHeader);
int freadjammsgheader(FILE *fp,s_JamMsgHeader *s_JamMsgHeader);
int fwritejammsgheader(FILE *fp,s_JamMsgHeader *s_JamMsgHeader);
int freadjamindex(FILE *fp,s_JamIndex *s_JamIndex);
int fwritejamindex(FILE *fp,s_JamIndex *s_JamIndex);
int freadjamlastread(FILE *fp,s_JamLastRead *s_JamLastRead);
int fwritejamlastread(FILE *fp,s_JamLastRead *s_JamLastRead);
int fwritejamsavesubfield(FILE *fp,s_JamSaveSubfield *s_JamSaveSubfield);
void getjamsubfield(char *buf,s_JamSubfield *Subfield_S);

201
jamlib/subpack.c Normal file
View File

@@ -0,0 +1,201 @@
/*
JAMLIB - A JAM subroutine library
Copyright (C) 1999 Bj<42>rn Stenberg
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes made by Johan Billing 2000-04-16:
- Fixed broken JAM_GetSubfield()
- #includes stdlib.h instead of malloc.h and memory.h
Changes made by Johan Billing 2000-09-17:
- Added JAM_GetSubfield_R()
Backported changes from JAMLIB 1.4.7 made by Johan Billing 2003-10-22
- JAM_NewSubPacket() and JAM_PutSubField() would give memory leaks under
low memory conditions. Fixed.
Other changes made by Johan Billing 2003-10-22
- Fixed comparison between signed and unsigned variable in
JAM_DelSubPacket() and JAM_GetSubField()
*/
/***********************************************************************
**
** SUBPACKET.C -- Subfield packet handling
**
** Author: Bj<42>rn Stenberg (bjorn.stenberg@sth.frontec.se)
**
***********************************************************************/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "jam.h"
/***********************************************************************
**
** JAM_NewSubPacket - Create a new subfield packet
**
***********************************************************************/
s_JamSubPacket* JAM_NewSubPacket( void )
{
s_JamSubPacket* Sub_PS;
/* allocate packet struct */
Sub_PS = (s_JamSubPacket*) malloc( sizeof( s_JamSubPacket ) );
if ( !Sub_PS )
return NULL;
Sub_PS->NumAlloc = 20;
Sub_PS->NumFields = 0;
/* allocate pointer array */
Sub_PS->Fields = (s_JamSubfield**) calloc( Sub_PS->NumAlloc,
sizeof( s_JamSubfield* ) );
if ( !Sub_PS->Fields ) {
free (Sub_PS);
return NULL;
}
return Sub_PS;
}
/***********************************************************************
**
** JAM_DelSubPacket - Free the data associated with a subfield packet
**
***********************************************************************/
int JAM_DelSubPacket( s_JamSubPacket* SubPack_PS )
{
uint32_t i;
if (!SubPack_PS)
return JAM_BAD_PARAM;
for ( i=0; i < SubPack_PS->NumFields; i++ ) {
s_JamSubfield* Field_PS = SubPack_PS->Fields[i];
if ( Field_PS->Buffer )
free( Field_PS->Buffer );
free( Field_PS );
}
free( SubPack_PS->Fields );
free( SubPack_PS );
return 0;
}
/***********************************************************************
**
** JAM_GetSubfield -- Get first/next subfield from a subfield packet
** (not reentrant)
**
***********************************************************************/
s_JamSubfield* JAM_GetSubfield( s_JamSubPacket* SubPack_PS )
{
static s_JamSubPacket* LastPack_PS = NULL;
static uint32_t NextIndex_I = 0;
if ( SubPack_PS ) {
LastPack_PS = SubPack_PS;
NextIndex_I = 0;
}
if ( NextIndex_I < LastPack_PS->NumFields )
return LastPack_PS->Fields[ NextIndex_I++ ];
return NULL;
}
/***********************************************************************
**
** JAM_GetSubfield_R -- Get first/next subfield from a subfield packet
** (reentrant)
**
***********************************************************************/
s_JamSubfield* JAM_GetSubfield_R( s_JamSubPacket* SubPack_PS , uint32_t* Count_PI)
{
if ( *Count_PI < SubPack_PS->NumFields )
return SubPack_PS->Fields[ (*Count_PI)++ ];
return NULL;
}
/***********************************************************************
**
** JAM_PutSubfield -- Add a subfield to a subfield packet
**
***********************************************************************/
int JAM_PutSubfield( s_JamSubPacket* SubPack_PS, s_JamSubfield* Field_PS )
{
s_JamSubfield* NewField_PS;
char* NewBuf_PC;
/* do we have to expand the array? */
if ( SubPack_PS->NumFields == SubPack_PS->NumAlloc ) {
s_JamSubfield** Fields_PPS;
SubPack_PS->NumAlloc *= 2;
Fields_PPS = (s_JamSubfield**) realloc( SubPack_PS->Fields,
SubPack_PS->NumAlloc *
sizeof( s_JamSubfield* ) );
if ( !Fields_PPS )
return JAM_NO_MEMORY;
SubPack_PS->Fields=Fields_PPS;
}
/*
** Copy the passed subfield
*/
/* allocate a new subfield */
NewField_PS = (s_JamSubfield*) malloc( sizeof( s_JamSubfield ) );
if ( !NewField_PS )
return JAM_NO_MEMORY;
/* allocate a new buffer */
if ( Field_PS->DatLen ) {
NewBuf_PC = (char*) malloc( Field_PS->DatLen );
if ( !NewBuf_PC ) {
free (NewField_PS);
return JAM_NO_MEMORY;
}
memcpy( NewBuf_PC, Field_PS->Buffer, Field_PS->DatLen );
}
else
NewBuf_PC = NULL;
/* copy field struct */
NewField_PS->LoID = Field_PS->LoID;
NewField_PS->HiID = Field_PS->HiID;
NewField_PS->DatLen = Field_PS->DatLen;
NewField_PS->Buffer = NewBuf_PC;
/*
** Update subfield packet
*/
SubPack_PS->Fields[ SubPack_PS->NumFields ] = NewField_PS;
SubPack_PS->NumFields++;
return 0;
}